2 * Copyright 2009 Tony Wasserka
3 * Copyright 2010 Christian Costa
4 * Copyright 2010 Owen Rudge for CodeWeavers
5 * Copyright 2010 Matteo Bruni for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/unicode.h"
23 #include "wine/debug.h"
24 #include "d3dx9_36_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
28 /* Returns TRUE if num is a power of 2, FALSE if not, or if 0 */
29 static BOOL
is_pow2(UINT num
)
31 return !(num
& (num
- 1));
34 /* Returns the smallest power of 2 which is greater than or equal to num */
35 static UINT
make_pow2(UINT num
)
39 /* In the unlikely event somebody passes a large value, make sure we don't enter an infinite loop */
40 if (num
>= 0x80000000)
49 static HRESULT
get_surface(D3DRESOURCETYPE type
, struct IDirect3DBaseTexture9
*tex
,
50 int face
, UINT level
, struct IDirect3DSurface9
**surf
)
54 case D3DRTYPE_TEXTURE
:
55 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9
*) tex
, level
, surf
);
56 case D3DRTYPE_CUBETEXTURE
:
57 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9
*) tex
, face
, level
, surf
);
59 ERR("Unexpected texture type\n");
64 HRESULT WINAPI
D3DXFilterTexture(IDirect3DBaseTexture9
*texture
,
65 const PALETTEENTRY
*palette
,
73 TRACE("(%p, %p, %u, %#x)\n", texture
, palette
, srclevel
, filter
);
76 return D3DERR_INVALIDCALL
;
78 if ((filter
& 0xFFFF) > D3DX_FILTER_BOX
&& filter
!= D3DX_DEFAULT
)
79 return D3DERR_INVALIDCALL
;
81 if (srclevel
== D3DX_DEFAULT
)
83 else if (srclevel
>= IDirect3DBaseTexture9_GetLevelCount(texture
))
84 return D3DERR_INVALIDCALL
;
86 switch (type
= IDirect3DBaseTexture9_GetType(texture
))
88 case D3DRTYPE_TEXTURE
:
89 case D3DRTYPE_CUBETEXTURE
:
91 IDirect3DSurface9
*topsurf
, *mipsurf
;
95 if (type
== D3DRTYPE_TEXTURE
)
98 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
103 IDirect3DCubeTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
106 if (filter
== D3DX_DEFAULT
)
108 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
))
109 filter
= D3DX_FILTER_BOX
;
111 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
114 for (i
= 0; i
< numfaces
; i
++)
116 level
= srclevel
+ 1;
117 hr
= get_surface(type
, texture
, i
, srclevel
, &topsurf
);
120 return D3DERR_INVALIDCALL
;
122 while (get_surface(type
, texture
, i
, level
, &mipsurf
) == D3D_OK
)
124 hr
= D3DXLoadSurfaceFromSurface(mipsurf
, palette
, NULL
, topsurf
, palette
, NULL
, filter
, 0);
125 IDirect3DSurface9_Release(topsurf
);
134 IDirect3DSurface9_Release(topsurf
);
142 case D3DRTYPE_VOLUMETEXTURE
:
145 int level
, level_count
;
146 IDirect3DVolume9
*top_volume
, *mip_volume
;
147 IDirect3DVolumeTexture9
*volume_texture
= (IDirect3DVolumeTexture9
*) texture
;
149 IDirect3DVolumeTexture9_GetLevelDesc(volume_texture
, srclevel
, &desc
);
151 if (filter
== D3DX_DEFAULT
)
153 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
) && is_pow2(desc
.Depth
))
154 filter
= D3DX_FILTER_BOX
;
156 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
159 hr
= IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, srclevel
, &top_volume
);
163 level_count
= IDirect3DVolumeTexture9_GetLevelCount(volume_texture
);
164 for (level
= srclevel
+ 1; level
< level_count
; level
++)
166 IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, level
, &mip_volume
);
167 hr
= D3DXLoadVolumeFromVolume(mip_volume
, palette
, NULL
, top_volume
, palette
, NULL
, filter
, 0);
168 IDirect3DVolume9_Release(top_volume
);
169 top_volume
= mip_volume
;
175 IDirect3DVolume9_Release(top_volume
);
183 return D3DERR_INVALIDCALL
;
187 HRESULT WINAPI
D3DXCheckTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
188 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
190 UINT w
= (width
&& *width
) ? *width
: 1;
191 UINT h
= (height
&& *height
) ? *height
: 1;
193 D3DDEVICE_CREATION_PARAMETERS params
;
194 IDirect3D9
*d3d
= NULL
;
197 D3DFORMAT usedformat
= D3DFMT_UNKNOWN
;
199 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, miplevels
, usage
, format
, pool
);
202 return D3DERR_INVALIDCALL
;
205 if (usage
== D3DX_DEFAULT
)
207 if (usage
& (D3DUSAGE_WRITEONLY
| D3DUSAGE_DONOTCLIP
| D3DUSAGE_POINTS
| D3DUSAGE_RTPATCHES
| D3DUSAGE_NPATCHES
))
208 return D3DERR_INVALIDCALL
;
211 if ((pool
!= D3DPOOL_DEFAULT
) && (pool
!= D3DPOOL_MANAGED
) && (pool
!= D3DPOOL_SYSTEMMEM
) && (pool
!= D3DPOOL_SCRATCH
))
212 return D3DERR_INVALIDCALL
;
214 /* width and height */
215 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
216 return D3DERR_INVALIDCALL
;
218 /* 256 x 256 default width/height */
219 if ((w
== D3DX_DEFAULT
) && (h
== D3DX_DEFAULT
))
221 else if (w
== D3DX_DEFAULT
)
222 w
= (height
? h
: 256);
223 else if (h
== D3DX_DEFAULT
)
224 h
= (width
? w
: 256);
226 /* ensure width/height is power of 2 */
227 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(w
)))
230 if (w
> caps
.MaxTextureWidth
)
231 w
= caps
.MaxTextureWidth
;
233 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(h
)))
236 if (h
> caps
.MaxTextureHeight
)
237 h
= caps
.MaxTextureHeight
;
239 /* texture must be square? */
240 if (caps
.TextureCaps
& D3DPTEXTURECAPS_SQUAREONLY
)
255 if (miplevels
&& (usage
& D3DUSAGE_AUTOGENMIPMAP
))
262 UINT max_mipmaps
= 1;
264 if (!width
&& !height
)
265 max_mipmaps
= 9; /* number of mipmaps in a 256x256 texture */
268 UINT max_dimen
= max(w
, h
);
270 while (max_dimen
> 1)
277 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
278 *miplevels
= max_mipmaps
;
284 TRACE("Requested format %x\n", *format
);
285 usedformat
= *format
;
288 hr
= IDirect3DDevice9_GetDirect3D(device
, &d3d
);
293 hr
= IDirect3DDevice9_GetCreationParameters(device
, ¶ms
);
298 hr
= IDirect3DDevice9_GetDisplayMode(device
, 0, &mode
);
303 if ((usedformat
== D3DFMT_UNKNOWN
) || (usedformat
== D3DX_DEFAULT
))
304 usedformat
= D3DFMT_A8R8G8B8
;
306 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
, mode
.Format
,
307 usage
, D3DRTYPE_TEXTURE
, usedformat
);
311 /* Heuristic to choose the fallback format */
312 const struct pixel_format_desc
*fmt
= get_format_info(usedformat
);
314 int bestscore
= INT_MIN
, i
= 0, j
;
315 unsigned int channels
;
316 const struct pixel_format_desc
*curfmt
;
320 FIXME("Pixel format %x not handled\n", usedformat
);
324 allow_24bits
= fmt
->bytes_per_pixel
== 3;
325 channels
= (fmt
->bits
[0] ? 1 : 0) + (fmt
->bits
[1] ? 1 : 0)
326 + (fmt
->bits
[2] ? 1 : 0) + (fmt
->bits
[3] ? 1 : 0);
327 usedformat
= D3DFMT_UNKNOWN
;
329 while ((curfmt
= get_format_info_idx(i
)))
331 unsigned int curchannels
= (curfmt
->bits
[0] ? 1 : 0) + (curfmt
->bits
[1] ? 1 : 0)
332 + (curfmt
->bits
[2] ? 1 : 0) + (curfmt
->bits
[3] ? 1 : 0);
337 if (curchannels
< channels
)
339 if (curfmt
->bytes_per_pixel
== 3 && !allow_24bits
)
342 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
,
343 mode
.Format
, usage
, D3DRTYPE_TEXTURE
, curfmt
->format
);
347 /* This format can be used, let's evaluate it.
348 Weights chosen quite arbitrarily... */
349 score
= 16 - 4 * (curchannels
- channels
);
351 for (j
= 0; j
< 4; j
++)
353 int diff
= curfmt
->bits
[j
] - fmt
->bits
[j
];
354 score
+= 16 - (diff
< 0 ? -diff
* 4 : diff
);
357 if (score
> bestscore
)
360 usedformat
= curfmt
->format
;
369 IDirect3D9_Release(d3d
);
374 if (usedformat
== D3DFMT_UNKNOWN
)
376 WARN("Couldn't find a suitable pixel format\n");
377 return D3DERR_NOTAVAILABLE
;
380 TRACE("Format chosen: %x\n", usedformat
);
382 *format
= usedformat
;
387 HRESULT WINAPI
D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*size
,
388 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
391 UINT s
= (size
&& *size
) ? *size
: 256;
394 TRACE("(%p, %p, %p, %u, %p, %u)\n", device
, size
, miplevels
, usage
, format
, pool
);
396 if (s
== D3DX_DEFAULT
)
399 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
400 return D3DERR_INVALIDCALL
;
402 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP
))
403 return D3DERR_NOTAVAILABLE
;
405 /* ensure width/height is power of 2 */
406 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP_POW2
) && (!is_pow2(s
)))
409 hr
= D3DXCheckTextureRequirements(device
, &s
, &s
, miplevels
, usage
, format
, pool
);
411 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPCUBEMAP
))
423 HRESULT WINAPI
D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
424 UINT
*depth
, UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
427 UINT w
= width
? *width
: D3DX_DEFAULT
;
428 UINT h
= height
? *height
: D3DX_DEFAULT
;
429 UINT d
= (depth
&& *depth
) ? *depth
: 1;
432 TRACE("(%p, %p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, depth
, miplevels
,
433 usage
, format
, pool
);
435 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
436 return D3DERR_INVALIDCALL
;
438 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP
))
439 return D3DERR_NOTAVAILABLE
;
441 hr
= D3DXCheckTextureRequirements(device
, &w
, &h
, NULL
, usage
, format
, pool
);
442 if (d
== D3DX_DEFAULT
)
445 /* ensure width/height is power of 2 */
446 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP_POW2
) &&
447 (!is_pow2(w
) || !is_pow2(h
) || !is_pow2(d
)))
454 if (w
> caps
.MaxVolumeExtent
)
455 w
= caps
.MaxVolumeExtent
;
456 if (h
> caps
.MaxVolumeExtent
)
457 h
= caps
.MaxVolumeExtent
;
458 if (d
> caps
.MaxVolumeExtent
)
459 d
= caps
.MaxVolumeExtent
;
463 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPVOLUMEMAP
))
465 else if ((usage
& D3DUSAGE_AUTOGENMIPMAP
))
472 UINT max_mipmaps
= 1;
473 UINT max_dimen
= max(max(w
, h
), d
);
475 while (max_dimen
> 1)
481 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
482 *miplevels
= max_mipmaps
;
496 HRESULT WINAPI
D3DXCreateTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
,
497 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DTexture9
**texture
)
501 TRACE("device %p, width %u, height %u, miplevels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
502 device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
504 if (!device
|| !texture
)
505 return D3DERR_INVALIDCALL
;
507 if (FAILED(hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
)))
510 return IDirect3DDevice9_CreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
, NULL
);
513 HRESULT WINAPI
D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9
*device
, const void *srcdata
,
514 UINT srcdatasize
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
515 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
516 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
518 IDirect3DTexture9
**texptr
;
519 IDirect3DTexture9
*buftex
;
520 IDirect3DSurface9
*surface
;
521 BOOL file_width
= FALSE
, file_height
= FALSE
;
522 BOOL file_format
= FALSE
, file_miplevels
= FALSE
;
523 BOOL dynamic_texture
;
524 D3DXIMAGE_INFO imginfo
;
525 UINT loaded_miplevels
;
529 TRACE("(%p, %p, %u, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p)\n", device
, srcdata
, srcdatasize
, width
,
530 height
, miplevels
, usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
532 /* check for invalid parameters */
533 if (!device
|| !texture
|| !srcdata
|| !srcdatasize
)
534 return D3DERR_INVALIDCALL
;
536 hr
= D3DXGetImageInfoFromFileInMemory(srcdata
, srcdatasize
, &imginfo
);
544 /* handle default values */
545 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
546 width
= imginfo
.Width
;
548 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
549 height
= imginfo
.Height
;
551 if (width
== D3DX_DEFAULT
)
552 width
= make_pow2(imginfo
.Width
);
554 if (height
== D3DX_DEFAULT
)
555 height
= make_pow2(imginfo
.Height
);
557 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
558 format
= imginfo
.Format
;
560 if (width
== D3DX_FROM_FILE
)
563 width
= imginfo
.Width
;
566 if (height
== D3DX_FROM_FILE
)
569 height
= imginfo
.Height
;
572 if (format
== D3DFMT_FROM_FILE
)
575 format
= imginfo
.Format
;
578 if (miplevels
== D3DX_FROM_FILE
)
580 file_miplevels
= TRUE
;
581 miplevels
= imginfo
.MipLevels
;
584 /* fix texture creation parameters */
585 hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
);
593 if (imginfo
.MipLevels
< miplevels
&& (D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
))
595 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet\n");
596 miplevels
= imginfo
.MipLevels
;
599 if (((file_width
) && (width
!= imginfo
.Width
)) ||
600 ((file_height
) && (height
!= imginfo
.Height
)) ||
601 ((file_format
) && (format
!= imginfo
.Format
)) ||
602 ((file_miplevels
) && (miplevels
!= imginfo
.MipLevels
)))
604 return D3DERR_NOTAVAILABLE
;
607 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
608 return D3DERR_INVALIDCALL
;
610 /* Create the to-be-filled texture */
611 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
612 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
614 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
619 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
630 if (imginfo
.ImageFileFormat
!= D3DXIFF_DDS
)
632 IDirect3DTexture9_GetSurfaceLevel(*texptr
, 0, &surface
);
633 hr
= D3DXLoadSurfaceFromFileInMemory(surface
, palette
, NULL
, srcdata
, srcdatasize
, NULL
, filter
, colorkey
, NULL
);
634 IDirect3DSurface9_Release(surface
);
638 hr
= load_texture_from_dds(*texptr
, srcdata
, palette
, filter
, colorkey
, &imginfo
);
643 IDirect3DTexture9_Release(*texptr
);
648 loaded_miplevels
= min(IDirect3DTexture9_GetLevelCount(*texptr
), imginfo
.MipLevels
);
649 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) *texptr
, palette
, loaded_miplevels
- 1, mipfilter
);
653 IDirect3DTexture9_Release(*texptr
);
658 /* Move the data to the actual texture if necessary */
659 if (texptr
== &buftex
)
661 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
665 IDirect3DTexture9_Release(buftex
);
670 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)(*texture
));
671 IDirect3DTexture9_Release(buftex
);
680 HRESULT WINAPI
D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
681 const void *srcdata
, UINT srcdatasize
, struct IDirect3DTexture9
**texture
)
683 TRACE("(%p, %p, %d, %p)\n", device
, srcdata
, srcdatasize
, texture
);
685 return D3DXCreateTextureFromFileInMemoryEx(device
, srcdata
, srcdatasize
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
686 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
689 HRESULT WINAPI
D3DXCreateTextureFromFileExW(struct IDirect3DDevice9
*device
, const WCHAR
*srcfile
,
690 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
691 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
692 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
698 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
699 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
700 device
, debugstr_w(srcfile
), width
, height
, miplevels
, usage
, format
,
701 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
704 return D3DERR_INVALIDCALL
;
706 hr
= map_view_of_file(srcfile
, &buffer
, &size
);
708 return D3DXERR_INVALIDDATA
;
710 hr
= D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
, usage
, format
, pool
,
711 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
713 UnmapViewOfFile(buffer
);
718 HRESULT WINAPI
D3DXCreateTextureFromFileExA(struct IDirect3DDevice9
*device
, const char *srcfile
,
719 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
720 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
721 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
727 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
728 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
729 device
, debugstr_a(srcfile
), width
, height
, miplevels
, usage
, format
,
730 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
732 if (!device
|| !srcfile
|| !texture
)
733 return D3DERR_INVALIDCALL
;
735 len
= MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, NULL
, 0);
736 widename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*widename
));
737 MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, widename
, len
);
739 hr
= D3DXCreateTextureFromFileExW(device
, widename
, width
, height
, miplevels
,
740 usage
, format
, pool
, filter
, mipfilter
,
741 colorkey
, srcinfo
, palette
, texture
);
743 HeapFree(GetProcessHeap(), 0, widename
);
747 HRESULT WINAPI
D3DXCreateTextureFromFileA(struct IDirect3DDevice9
*device
,
748 const char *srcfile
, struct IDirect3DTexture9
**texture
)
750 TRACE("(%p, %s, %p)\n", device
, debugstr_a(srcfile
), texture
);
752 return D3DXCreateTextureFromFileExA(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
753 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
756 HRESULT WINAPI
D3DXCreateTextureFromFileW(struct IDirect3DDevice9
*device
,
757 const WCHAR
*srcfile
, struct IDirect3DTexture9
**texture
)
759 TRACE("(%p, %s, %p)\n", device
, debugstr_w(srcfile
), texture
);
761 return D3DXCreateTextureFromFileExW(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
762 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
766 HRESULT WINAPI
D3DXCreateTextureFromResourceA(struct IDirect3DDevice9
*device
,
767 HMODULE srcmodule
, const char *resource
, struct IDirect3DTexture9
**texture
)
769 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_a(resource
));
771 return D3DXCreateTextureFromResourceExA(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
772 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
775 HRESULT WINAPI
D3DXCreateTextureFromResourceW(struct IDirect3DDevice9
*device
,
776 HMODULE srcmodule
, const WCHAR
*resource
, struct IDirect3DTexture9
**texture
)
778 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_w(resource
));
780 return D3DXCreateTextureFromResourceExW(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
781 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
784 HRESULT WINAPI
D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
785 const char *resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
786 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
787 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
793 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
794 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
795 device
, srcmodule
, debugstr_a(resource
), width
, height
, miplevels
, usage
, format
,
796 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
798 if (!device
|| !texture
)
799 return D3DERR_INVALIDCALL
;
801 if (!(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_RCDATA
))
802 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
803 && !(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_BITMAP
)))
804 return D3DXERR_INVALIDDATA
;
806 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
807 return D3DXERR_INVALIDDATA
;
809 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
810 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
813 HRESULT WINAPI
D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
814 const WCHAR
*resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
815 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
816 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
822 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
823 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
824 device
, srcmodule
, debugstr_w(resource
), width
, height
, miplevels
, usage
, format
,
825 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
827 if (!device
|| !texture
)
828 return D3DERR_INVALIDCALL
;
830 if (!(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_RCDATA
))
831 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
832 && !(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_BITMAP
)))
833 return D3DXERR_INVALIDDATA
;
835 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
836 return D3DXERR_INVALIDDATA
;
838 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
839 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
842 HRESULT WINAPI
D3DXCreateCubeTexture(struct IDirect3DDevice9
*device
, UINT size
, UINT miplevels
,
843 DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DCubeTexture9
**texture
)
847 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device
, size
, miplevels
, usage
, format
,
850 if (!device
|| !texture
)
851 return D3DERR_INVALIDCALL
;
853 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &miplevels
, usage
, &format
, pool
);
857 TRACE("D3DXCheckCubeTextureRequirements failed\n");
861 return IDirect3DDevice9_CreateCubeTexture(device
, size
, miplevels
, usage
, format
, pool
, texture
, NULL
);
864 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
865 const void *data
, UINT datasize
, struct IDirect3DCubeTexture9
**texture
)
867 TRACE("(%p, %p, %u, %p)\n", device
, data
, datasize
, texture
);
869 return D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, datasize
, D3DX_DEFAULT
, D3DX_DEFAULT
,
870 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
873 HRESULT WINAPI
D3DXCreateVolumeTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
, UINT depth
,
874 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DVolumeTexture9
**texture
)
878 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device
, width
, height
, depth
,
879 miplevels
, usage
, format
, pool
, texture
);
881 if (!device
|| !texture
)
882 return D3DERR_INVALIDCALL
;
884 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
,
885 &miplevels
, usage
, &format
, pool
);
889 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
893 return IDirect3DDevice9_CreateVolumeTexture(device
, width
, height
, depth
, miplevels
,
894 usage
, format
, pool
, texture
, NULL
);
897 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9
*device
,
898 const char *filename
,
899 IDirect3DVolumeTexture9
**volume_texture
)
907 TRACE("(%p, %s, %p): relay\n",
908 device
, debugstr_a(filename
), volume_texture
);
910 if (!filename
) return D3DERR_INVALIDCALL
;
912 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
913 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
914 if (!filenameW
) return E_OUTOFMEMORY
;
915 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
917 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
918 HeapFree(GetProcessHeap(), 0, filenameW
);
919 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
921 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
922 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
924 UnmapViewOfFile(data
);
928 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9
*device
,
929 const WCHAR
*filename
,
930 IDirect3DVolumeTexture9
**volume_texture
)
936 TRACE("(%p, %s, %p): relay\n",
937 device
, debugstr_w(filename
), volume_texture
);
939 if (!filename
) return D3DERR_INVALIDCALL
;
941 hr
= map_view_of_file(filename
, &data
, &data_size
);
942 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
944 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
945 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
947 UnmapViewOfFile(data
);
951 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9
*device
,
952 const char *filename
,
963 D3DXIMAGE_INFO
*src_info
,
964 PALETTEENTRY
*palette
,
965 IDirect3DVolumeTexture9
**volume_texture
)
973 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
974 device
, debugstr_a(filename
), width
, height
, depth
, mip_levels
,
975 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
976 palette
, volume_texture
);
978 if (!filename
) return D3DERR_INVALIDCALL
;
980 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
981 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
982 if (!filenameW
) return E_OUTOFMEMORY
;
983 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
985 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
986 HeapFree(GetProcessHeap(), 0, filenameW
);
987 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
989 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
990 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
993 UnmapViewOfFile(data
);
997 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9
*device
,
998 const WCHAR
*filename
,
1009 D3DXIMAGE_INFO
*src_info
,
1010 PALETTEENTRY
*palette
,
1011 IDirect3DVolumeTexture9
**volume_texture
)
1017 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1018 device
, debugstr_w(filename
), width
, height
, depth
, mip_levels
,
1019 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1020 palette
, volume_texture
);
1022 if (!filename
) return D3DERR_INVALIDCALL
;
1024 hr
= map_view_of_file(filename
, &data
, &data_size
);
1025 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1027 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1028 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1031 UnmapViewOfFile(data
);
1035 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9
*device
,
1038 IDirect3DVolumeTexture9
**volume_texture
)
1040 TRACE("(%p, %p, %u, %p): relay\n", device
, data
, data_size
, volume_texture
);
1042 return D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1043 D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1044 0, NULL
, NULL
, volume_texture
);
1047 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1060 D3DXIMAGE_INFO
*info
,
1061 PALETTEENTRY
*palette
,
1062 IDirect3DVolumeTexture9
**volume_texture
)
1066 D3DXIMAGE_INFO image_info
;
1067 BOOL dynamic_texture
;
1068 BOOL file_width
= FALSE
;
1069 BOOL file_height
= FALSE
;
1070 BOOL file_depth
= FALSE
;
1071 BOOL file_format
= FALSE
;
1072 BOOL file_mip_levels
= FALSE
;
1073 IDirect3DVolumeTexture9
*tex
, *buftex
;
1075 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1076 device
, data
, data_size
, width
, height
, depth
, mip_levels
, usage
, format
, pool
,
1077 filter
, mip_filter
, color_key
, info
, palette
, volume_texture
);
1079 if (!device
|| !data
|| !data_size
|| !volume_texture
)
1080 return D3DERR_INVALIDCALL
;
1082 hr
= D3DXGetImageInfoFromFileInMemory(data
, data_size
, &image_info
);
1083 if (FAILED(hr
)) return hr
;
1085 if (image_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1086 return D3DXERR_INVALIDDATA
;
1088 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
1089 width
= image_info
.Width
;
1090 if (width
== D3DX_DEFAULT
)
1091 width
= make_pow2(image_info
.Width
);
1093 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
1094 height
= image_info
.Height
;
1095 if (height
== D3DX_DEFAULT
)
1096 height
= make_pow2(image_info
.Height
);
1098 if (depth
== 0 || depth
== D3DX_DEFAULT_NONPOW2
)
1099 depth
= image_info
.Depth
;
1100 if (depth
== D3DX_DEFAULT
)
1101 depth
= make_pow2(image_info
.Depth
);
1103 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1104 format
= image_info
.Format
;
1106 if (width
== D3DX_FROM_FILE
)
1109 width
= image_info
.Width
;
1112 if (height
== D3DX_FROM_FILE
)
1115 height
= image_info
.Height
;
1118 if (depth
== D3DX_FROM_FILE
)
1121 depth
= image_info
.Depth
;
1124 if (format
== D3DFMT_FROM_FILE
)
1127 format
= image_info
.Format
;
1130 if (mip_levels
== D3DX_FROM_FILE
)
1132 file_mip_levels
= TRUE
;
1133 mip_levels
= image_info
.MipLevels
;
1136 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
, &mip_levels
, usage
, &format
, pool
);
1137 if (FAILED(hr
)) return hr
;
1139 if ((file_width
&& width
!= image_info
.Width
)
1140 || (file_height
&& height
!= image_info
.Height
)
1141 || (file_depth
&& depth
!= image_info
.Depth
)
1142 || (file_format
&& format
!= image_info
.Format
)
1143 || (file_mip_levels
&& mip_levels
!= image_info
.MipLevels
))
1144 return D3DERR_NOTAVAILABLE
;
1146 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1148 return D3DERR_INVALIDCALL
;
1150 if (mip_levels
> image_info
.MipLevels
)
1152 FIXME("Generation of mipmaps for volume textures is not implemented yet\n");
1153 mip_levels
= image_info
.MipLevels
;
1156 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1157 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1159 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1164 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1168 if (FAILED(hr
)) return hr
;
1170 hr
= load_volume_texture_from_dds(tex
, data
, palette
, filter
, color_key
, &image_info
);
1173 IDirect3DVolumeTexture9_Release(tex
);
1179 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1182 IDirect3DVolumeTexture9_Release(buftex
);
1186 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1187 IDirect3DVolumeTexture9_Release(buftex
);
1193 *volume_texture
= tex
;
1197 static inline void fill_texture(const struct pixel_format_desc
*format
, BYTE
*pos
, const D3DXVECTOR4
*value
)
1201 for (c
= 0; c
< format
->bytes_per_pixel
; c
++)
1204 for (c
= 0; c
< 4; c
++)
1207 DWORD i
, v
= 0, mask32
= format
->bits
[c
] == 32 ? ~0U : ((1 << format
->bits
[c
]) - 1);
1212 comp_value
= value
->w
;
1215 comp_value
= value
->x
;
1218 comp_value
= value
->y
;
1221 comp_value
= value
->z
;
1225 if (format
->type
== FORMAT_ARGBF16
)
1226 v
= float_32_to_16(comp_value
);
1227 else if (format
->type
== FORMAT_ARGBF
)
1228 v
= *(DWORD
*)&comp_value
;
1229 else if (format
->type
== FORMAT_ARGB
)
1230 v
= comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
;
1232 FIXME("Unhandled format type %#x\n", format
->type
);
1234 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1238 if (format
->shift
[c
] > i
)
1240 mask
= mask32
<< (format
->shift
[c
] - i
);
1241 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1245 mask
= mask32
>> (i
- format
->shift
[c
]);
1246 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1253 HRESULT WINAPI
D3DXFillTexture(struct IDirect3DTexture9
*texture
, LPD3DXFILL2D function
, void *funcdata
)
1257 D3DSURFACE_DESC desc
;
1258 D3DLOCKED_RECT lock_rect
;
1260 D3DXVECTOR2 coord
, size
;
1261 const struct pixel_format_desc
*format
;
1264 if (texture
== NULL
|| function
== NULL
)
1265 return D3DERR_INVALIDCALL
;
1267 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1269 for (m
= 0; m
< miplevels
; m
++)
1271 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture
, m
, &desc
)))
1272 return D3DERR_INVALIDCALL
;
1274 format
= get_format_info(desc
.Format
);
1275 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1277 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1278 return D3DERR_INVALIDCALL
;
1281 if (FAILED(IDirect3DTexture9_LockRect(texture
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1282 return D3DERR_INVALIDCALL
;
1284 size
.x
= 1.0f
/ desc
.Width
;
1285 size
.y
= 1.0f
/ desc
.Height
;
1287 data
= lock_rect
.pBits
;
1289 for (y
= 0; y
< desc
.Height
; y
++)
1291 /* The callback function expects the coordinates of the center
1293 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1295 for (x
= 0; x
< desc
.Width
; x
++)
1297 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1299 function(&value
, &coord
, &size
, funcdata
);
1301 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1304 IDirect3DTexture9_UnlockRect(texture
, m
);
1310 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1311 const void *src_data
,
1321 D3DXIMAGE_INFO
*src_info
,
1322 PALETTEENTRY
*palette
,
1323 IDirect3DCubeTexture9
**cube_texture
)
1327 UINT loaded_miplevels
;
1328 D3DXIMAGE_INFO img_info
;
1329 BOOL dynamic_texture
;
1330 BOOL file_size
= FALSE
;
1331 BOOL file_format
= FALSE
;
1332 BOOL file_mip_levels
= FALSE
;
1333 IDirect3DCubeTexture9
*tex
, *buftex
;
1335 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device
,
1336 src_data
, src_data_size
, size
, mip_levels
, usage
, format
, pool
, filter
, mip_filter
,
1337 color_key
, src_info
, palette
, cube_texture
);
1339 if (!device
|| !cube_texture
|| !src_data
|| !src_data_size
)
1340 return D3DERR_INVALIDCALL
;
1342 hr
= D3DXGetImageInfoFromFileInMemory(src_data
, src_data_size
, &img_info
);
1346 if (img_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1347 return D3DXERR_INVALIDDATA
;
1349 if (img_info
.Width
!= img_info
.Height
)
1350 return D3DXERR_INVALIDDATA
;
1352 if (size
== 0 || size
== D3DX_DEFAULT_NONPOW2
)
1353 size
= img_info
.Width
;
1354 if (size
== D3DX_DEFAULT
)
1355 size
= make_pow2(img_info
.Width
);
1357 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1358 format
= img_info
.Format
;
1360 if (size
== D3DX_FROM_FILE
)
1363 size
= img_info
.Width
;
1366 if (format
== D3DFMT_FROM_FILE
)
1369 format
= img_info
.Format
;
1372 if (mip_levels
== D3DX_FROM_FILE
)
1374 file_mip_levels
= TRUE
;
1375 mip_levels
= img_info
.MipLevels
;
1378 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &mip_levels
, usage
, &format
, pool
);
1382 if ((file_size
&& size
!= img_info
.Width
)
1383 || (file_format
&& format
!= img_info
.Format
)
1384 || (file_mip_levels
&& mip_levels
!= img_info
.MipLevels
))
1385 return D3DERR_NOTAVAILABLE
;
1387 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1389 return D3DERR_INVALIDCALL
;
1391 if (mip_levels
> img_info
.MipLevels
&& (D3DFMT_DXT1
<= img_info
.Format
&& img_info
.Format
<= D3DFMT_DXT5
))
1393 FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
1394 mip_levels
= img_info
.MipLevels
;
1397 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1398 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1400 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1405 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1411 hr
= load_cube_texture_from_dds(tex
, src_data
, palette
, filter
, color_key
, &img_info
);
1414 IDirect3DCubeTexture9_Release(tex
);
1418 loaded_miplevels
= min(IDirect3DCubeTexture9_GetLevelCount(tex
), img_info
.MipLevels
);
1419 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) tex
, palette
, loaded_miplevels
- 1, mip_filter
);
1422 IDirect3DCubeTexture9_Release(tex
);
1428 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1431 IDirect3DCubeTexture9_Release(buftex
);
1435 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1436 IDirect3DCubeTexture9_Release(buftex
);
1440 *src_info
= img_info
;
1442 *cube_texture
= tex
;
1447 HRESULT WINAPI
D3DXCreateCubeTextureFromFileA(IDirect3DDevice9
*device
,
1448 const char *src_filename
,
1449 IDirect3DCubeTexture9
**cube_texture
)
1457 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_a(src_filename
), cube_texture
);
1459 if (!src_filename
) return D3DERR_INVALIDCALL
;
1461 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1462 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1463 if (!filename
) return E_OUTOFMEMORY
;
1464 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1466 hr
= map_view_of_file(filename
, &data
, &data_size
);
1469 HeapFree(GetProcessHeap(), 0, filename
);
1470 return D3DXERR_INVALIDDATA
;
1473 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1474 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1476 UnmapViewOfFile(data
);
1477 HeapFree(GetProcessHeap(), 0, filename
);
1481 HRESULT WINAPI
D3DXCreateCubeTextureFromFileW(IDirect3DDevice9
*device
,
1482 const WCHAR
*src_filename
,
1483 IDirect3DCubeTexture9
**cube_texture
)
1489 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_w(src_filename
), cube_texture
);
1491 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1492 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1494 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1495 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1497 UnmapViewOfFile(data
);
1501 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9
*device
,
1502 const char *src_filename
,
1511 D3DXIMAGE_INFO
*image_info
,
1512 PALETTEENTRY
*palette
,
1513 IDirect3DCubeTexture9
**cube_texture
)
1521 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1522 device
, wine_dbgstr_a(src_filename
), size
, mip_levels
, usage
, format
,
1523 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1525 if (!src_filename
) return D3DERR_INVALIDCALL
;
1527 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1528 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1529 if (!filename
) return E_OUTOFMEMORY
;
1530 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1532 hr
= map_view_of_file(filename
, &data
, &data_size
);
1535 HeapFree(GetProcessHeap(), 0, filename
);
1536 return D3DXERR_INVALIDDATA
;
1539 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1540 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1542 UnmapViewOfFile(data
);
1543 HeapFree(GetProcessHeap(), 0, filename
);
1547 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9
*device
,
1548 const WCHAR
*src_filename
,
1557 D3DXIMAGE_INFO
*image_info
,
1558 PALETTEENTRY
*palette
,
1559 IDirect3DCubeTexture9
**cube_texture
)
1565 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1566 device
, wine_dbgstr_w(src_filename
), size
, mip_levels
, usage
, format
,
1567 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1569 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1570 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1572 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1573 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1575 UnmapViewOfFile(data
);
1589 static float get_cube_coord(enum cube_coord coord
, unsigned int x
, unsigned int y
, unsigned int size
)
1596 return size
- x
- 0.5f
;
1600 return size
- y
- 0.5f
;
1606 ERR("Unexpected coordinate value\n");
1611 HRESULT WINAPI
D3DXFillCubeTexture(struct IDirect3DCubeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1615 D3DSURFACE_DESC desc
;
1616 D3DLOCKED_RECT lock_rect
;
1618 D3DXVECTOR3 coord
, size
;
1619 const struct pixel_format_desc
*format
;
1621 static const enum cube_coord coordmap
[6][3] =
1623 {ONE
, YCOORDINV
, XCOORDINV
},
1624 {ZERO
, YCOORDINV
, XCOORD
},
1625 {XCOORD
, ONE
, YCOORD
},
1626 {XCOORD
, ZERO
, YCOORDINV
},
1627 {XCOORD
, YCOORDINV
, ONE
},
1628 {XCOORDINV
, YCOORDINV
, ZERO
}
1631 if (texture
== NULL
|| function
== NULL
)
1632 return D3DERR_INVALIDCALL
;
1634 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1636 for (m
= 0; m
< miplevels
; m
++)
1638 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1639 return D3DERR_INVALIDCALL
;
1641 format
= get_format_info(desc
.Format
);
1642 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1644 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1645 return D3DERR_INVALIDCALL
;
1648 for (f
= 0; f
< 6; f
++)
1650 if (FAILED(IDirect3DCubeTexture9_LockRect(texture
, f
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1651 return D3DERR_INVALIDCALL
;
1653 size
.x
= (f
== 0) || (f
== 1) ? 0.0f
: 2.0f
/ desc
.Width
;
1654 size
.y
= (f
== 2) || (f
== 3) ? 0.0f
: 2.0f
/ desc
.Width
;
1655 size
.z
= (f
== 4) || (f
== 5) ? 0.0f
: 2.0f
/ desc
.Width
;
1657 data
= lock_rect
.pBits
;
1659 for (y
= 0; y
< desc
.Height
; y
++)
1661 for (x
= 0; x
< desc
.Width
; x
++)
1663 coord
.x
= get_cube_coord(coordmap
[f
][0], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1664 coord
.y
= get_cube_coord(coordmap
[f
][1], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1665 coord
.z
= get_cube_coord(coordmap
[f
][2], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1667 function(&value
, &coord
, &size
, funcdata
);
1669 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1672 IDirect3DCubeTexture9_UnlockRect(texture
, f
, m
);
1679 HRESULT WINAPI
D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1683 D3DVOLUME_DESC desc
;
1684 D3DLOCKED_BOX lock_box
;
1686 D3DXVECTOR3 coord
, size
;
1687 const struct pixel_format_desc
*format
;
1690 if (texture
== NULL
|| function
== NULL
)
1691 return D3DERR_INVALIDCALL
;
1693 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1695 for (m
= 0; m
< miplevels
; m
++)
1697 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1698 return D3DERR_INVALIDCALL
;
1700 format
= get_format_info(desc
.Format
);
1701 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1703 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1704 return D3DERR_INVALIDCALL
;
1707 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture
, m
, &lock_box
, NULL
, D3DLOCK_DISCARD
)))
1708 return D3DERR_INVALIDCALL
;
1710 size
.x
= 1.0f
/ desc
.Width
;
1711 size
.y
= 1.0f
/ desc
.Height
;
1712 size
.z
= 1.0f
/ desc
.Depth
;
1714 data
= lock_box
.pBits
;
1716 for (z
= 0; z
< desc
.Depth
; z
++)
1718 /* The callback function expects the coordinates of the center
1720 coord
.z
= (z
+ 0.5f
) / desc
.Depth
;
1722 for (y
= 0; y
< desc
.Height
; y
++)
1724 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1726 for (x
= 0; x
< desc
.Width
; x
++)
1728 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1730 function(&value
, &coord
, &size
, funcdata
);
1732 fill_texture(format
, data
+ z
* lock_box
.SlicePitch
+ y
* lock_box
.RowPitch
1733 + x
* format
->bytes_per_pixel
, &value
);
1737 IDirect3DVolumeTexture9_UnlockBox(texture
, m
);
1743 HRESULT WINAPI
D3DXSaveTextureToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1744 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1749 ID3DXBuffer
*buffer
;
1751 TRACE("(%s, %#x, %p, %p): relay\n",
1752 wine_dbgstr_a(dst_filename
), file_format
, src_texture
, src_palette
);
1754 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1756 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1757 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1758 if (!filename
) return E_OUTOFMEMORY
;
1759 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1761 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1764 hr
= write_buffer_to_file(filename
, buffer
);
1765 ID3DXBuffer_Release(buffer
);
1768 HeapFree(GetProcessHeap(), 0, filename
);
1772 HRESULT WINAPI
D3DXSaveTextureToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1773 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1776 ID3DXBuffer
*buffer
;
1778 TRACE("(%s, %#x, %p, %p): relay\n",
1779 wine_dbgstr_w(dst_filename
), file_format
, src_texture
, src_palette
);
1781 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1783 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1786 hr
= write_buffer_to_file(dst_filename
, buffer
);
1787 ID3DXBuffer_Release(buffer
);
1793 HRESULT WINAPI
D3DXSaveTextureToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1794 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1797 D3DRESOURCETYPE type
;
1798 IDirect3DSurface9
*surface
;
1800 TRACE("(%p, %#x, %p, %p)\n",
1801 dst_buffer
, file_format
, src_texture
, src_palette
);
1803 if (!dst_buffer
|| !src_texture
) return D3DERR_INVALIDCALL
;
1805 if (file_format
== D3DXIFF_DDS
)
1807 FIXME("DDS file format isn't supported yet\n");
1811 type
= IDirect3DBaseTexture9_GetType(src_texture
);
1814 case D3DRTYPE_TEXTURE
:
1815 case D3DRTYPE_CUBETEXTURE
:
1816 hr
= get_surface(type
, src_texture
, D3DCUBEMAP_FACE_POSITIVE_X
, 0, &surface
);
1818 case D3DRTYPE_VOLUMETEXTURE
:
1819 FIXME("Volume textures aren't supported yet\n");
1822 return D3DERR_INVALIDCALL
;
1827 hr
= D3DXSaveSurfaceToFileInMemory(dst_buffer
, file_format
, surface
, src_palette
, NULL
);
1828 IDirect3DSurface9_Release(surface
);