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
, LPDIRECT3DBASETEXTURE9 tex
,
50 int face
, UINT level
, LPDIRECT3DSURFACE9
*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(LPDIRECT3DBASETEXTURE9 texture
,
65 CONST PALETTEENTRY
*palette
,
73 TRACE("(%p, %p, %d, %d)\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
>= IDirect3DBaseTexture9_GetLevelCount(texture
))
82 return D3DERR_INVALIDCALL
;
84 switch (type
= IDirect3DBaseTexture9_GetType(texture
))
86 case D3DRTYPE_TEXTURE
:
87 case D3DRTYPE_CUBETEXTURE
:
89 IDirect3DSurface9
*topsurf
, *mipsurf
;
93 if (type
== D3DRTYPE_TEXTURE
)
96 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
101 IDirect3DCubeTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
104 if (filter
== D3DX_DEFAULT
)
106 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
))
107 filter
= D3DX_FILTER_BOX
;
109 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
112 for (i
= 0; i
< numfaces
; i
++)
114 level
= srclevel
+ 1;
115 hr
= get_surface(type
, texture
, i
, srclevel
, &topsurf
);
118 return D3DERR_INVALIDCALL
;
120 while (get_surface(type
, texture
, i
, level
, &mipsurf
) == D3D_OK
)
122 hr
= D3DXLoadSurfaceFromSurface(mipsurf
, palette
, NULL
, topsurf
, palette
, NULL
, filter
, 0);
123 IDirect3DSurface9_Release(topsurf
);
132 IDirect3DSurface9_Release(topsurf
);
141 FIXME("Implement volume texture filtering\n");
146 HRESULT WINAPI
D3DXCheckTextureRequirements(LPDIRECT3DDEVICE9 device
,
154 UINT w
= (width
&& *width
) ? *width
: 1;
155 UINT h
= (height
&& *height
) ? *height
: 1;
157 D3DDEVICE_CREATION_PARAMETERS params
;
158 IDirect3D9
*d3d
= NULL
;
161 D3DFORMAT usedformat
= D3DFMT_UNKNOWN
;
163 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, miplevels
, usage
, format
, pool
);
166 return D3DERR_INVALIDCALL
;
169 if (usage
== D3DX_DEFAULT
)
171 if (usage
& (D3DUSAGE_WRITEONLY
| D3DUSAGE_DONOTCLIP
| D3DUSAGE_POINTS
| D3DUSAGE_RTPATCHES
| D3DUSAGE_NPATCHES
))
172 return D3DERR_INVALIDCALL
;
175 if ((pool
!= D3DPOOL_DEFAULT
) && (pool
!= D3DPOOL_MANAGED
) && (pool
!= D3DPOOL_SYSTEMMEM
) && (pool
!= D3DPOOL_SCRATCH
))
176 return D3DERR_INVALIDCALL
;
178 /* width and height */
179 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
180 return D3DERR_INVALIDCALL
;
182 /* 256 x 256 default width/height */
183 if ((w
== D3DX_DEFAULT
) && (h
== D3DX_DEFAULT
))
185 else if (w
== D3DX_DEFAULT
)
186 w
= (height
? h
: 256);
187 else if (h
== D3DX_DEFAULT
)
188 h
= (width
? w
: 256);
190 /* ensure width/height is power of 2 */
191 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(w
)))
194 if (w
> caps
.MaxTextureWidth
)
195 w
= caps
.MaxTextureWidth
;
197 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(h
)))
200 if (h
> caps
.MaxTextureHeight
)
201 h
= caps
.MaxTextureHeight
;
203 /* texture must be square? */
204 if (caps
.TextureCaps
& D3DPTEXTURECAPS_SQUAREONLY
)
221 UINT max_mipmaps
= 1;
223 if (!width
&& !height
)
224 max_mipmaps
= 9; /* number of mipmaps in a 256x256 texture */
227 UINT max_dimen
= max(w
, h
);
229 while (max_dimen
> 1)
236 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
237 *miplevels
= max_mipmaps
;
243 TRACE("Requested format %x\n", *format
);
244 usedformat
= *format
;
247 hr
= IDirect3DDevice9_GetDirect3D(device
, &d3d
);
252 hr
= IDirect3DDevice9_GetCreationParameters(device
, ¶ms
);
257 hr
= IDirect3DDevice9_GetDisplayMode(device
, 0, &mode
);
262 if ((usedformat
== D3DFMT_UNKNOWN
) || (usedformat
== D3DX_DEFAULT
))
263 usedformat
= D3DFMT_A8R8G8B8
;
265 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
, mode
.Format
,
266 usage
, D3DRTYPE_TEXTURE
, usedformat
);
270 /* Heuristic to choose the fallback format */
271 const PixelFormatDesc
*fmt
= get_format_info(usedformat
);
273 int bestscore
= INT_MIN
, i
= 0, j
;
274 unsigned int channels
;
275 const PixelFormatDesc
*curfmt
;
279 FIXME("Pixel format %x not handled\n", usedformat
);
283 allow_24bits
= fmt
->bytes_per_pixel
== 3;
284 channels
= (fmt
->bits
[0] ? 1 : 0) + (fmt
->bits
[1] ? 1 : 0)
285 + (fmt
->bits
[2] ? 1 : 0) + (fmt
->bits
[3] ? 1 : 0);
286 usedformat
= D3DFMT_UNKNOWN
;
288 while ((curfmt
= get_format_info_idx(i
)))
290 unsigned int curchannels
= (curfmt
->bits
[0] ? 1 : 0) + (curfmt
->bits
[1] ? 1 : 0)
291 + (curfmt
->bits
[2] ? 1 : 0) + (curfmt
->bits
[3] ? 1 : 0);
296 if (curchannels
< channels
)
298 if (curfmt
->bytes_per_pixel
== 3 && !allow_24bits
)
301 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
,
302 mode
.Format
, usage
, D3DRTYPE_TEXTURE
, curfmt
->format
);
306 /* This format can be used, let's evaluate it.
307 Weights chosen quite arbitrarily... */
308 score
= 16 - 4 * (curchannels
- channels
);
310 for (j
= 0; j
< 4; j
++)
312 int diff
= curfmt
->bits
[j
] - fmt
->bits
[j
];
313 score
+= 16 - (diff
< 0 ? -diff
* 4 : diff
);
316 if (score
> bestscore
)
319 usedformat
= curfmt
->format
;
328 IDirect3D9_Release(d3d
);
333 if (usedformat
== D3DFMT_UNKNOWN
)
335 WARN("Couldn't find a suitable pixel format\n");
336 return D3DERR_NOTAVAILABLE
;
339 TRACE("Format chosen: %x\n", usedformat
);
341 *format
= usedformat
;
346 HRESULT WINAPI
D3DXCheckCubeTextureRequirements(LPDIRECT3DDEVICE9 device
,
354 UINT s
= (size
&& *size
) ? *size
: 256;
357 TRACE("(%p, %p, %p, %u, %p, %u)\n", device
, size
, miplevels
, usage
, format
, pool
);
359 if (s
== D3DX_DEFAULT
)
362 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
363 return D3DERR_INVALIDCALL
;
365 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP
))
366 return D3DERR_NOTAVAILABLE
;
368 /* ensure width/height is power of 2 */
369 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP_POW2
) && (!is_pow2(s
)))
372 hr
= D3DXCheckTextureRequirements(device
, &s
, &s
, miplevels
, usage
, format
, pool
);
374 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPCUBEMAP
))
386 HRESULT WINAPI
D3DXCheckVolumeTextureRequirements(LPDIRECT3DDEVICE9 device
,
396 UINT w
= width
? *width
: D3DX_DEFAULT
;
397 UINT h
= height
? *height
: D3DX_DEFAULT
;
398 UINT d
= (depth
&& *depth
) ? *depth
: 1;
401 TRACE("(%p, %p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, depth
, miplevels
,
402 usage
, format
, pool
);
404 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
405 return D3DERR_INVALIDCALL
;
407 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP
))
408 return D3DERR_NOTAVAILABLE
;
410 hr
= D3DXCheckTextureRequirements(device
, &w
, &h
, NULL
, usage
, format
, pool
);
411 if (d
== D3DX_DEFAULT
)
414 /* ensure width/height is power of 2 */
415 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP_POW2
) &&
416 (!is_pow2(w
) || !is_pow2(h
) || !is_pow2(d
)))
423 if (w
> caps
.MaxVolumeExtent
)
424 w
= caps
.MaxVolumeExtent
;
425 if (h
> caps
.MaxVolumeExtent
)
426 h
= caps
.MaxVolumeExtent
;
427 if (d
> caps
.MaxVolumeExtent
)
428 d
= caps
.MaxVolumeExtent
;
432 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPVOLUMEMAP
))
436 UINT max_mipmaps
= 1;
437 UINT max_dimen
= max(max(w
, h
), d
);
439 while (max_dimen
> 1)
445 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
446 *miplevels
= max_mipmaps
;
460 HRESULT WINAPI
D3DXCreateTexture(LPDIRECT3DDEVICE9 pDevice
,
467 LPDIRECT3DTEXTURE9
*ppTexture
)
471 TRACE("(%p, %u, %u, %u, %x, %x, %x, %p)\n", pDevice
, width
, height
, miplevels
, usage
, format
,
474 if (!pDevice
|| !ppTexture
)
475 return D3DERR_INVALIDCALL
;
477 hr
= D3DXCheckTextureRequirements(pDevice
, &width
, &height
, &miplevels
, usage
, &format
, pool
);
482 return IDirect3DDevice9_CreateTexture(pDevice
, width
, height
, miplevels
, usage
, format
, pool
, ppTexture
, NULL
);
485 HRESULT WINAPI
D3DXCreateTextureFromFileInMemoryEx(LPDIRECT3DDEVICE9 device
,
497 D3DXIMAGE_INFO
* srcinfo
,
498 PALETTEENTRY
* palette
,
499 LPDIRECT3DTEXTURE9
* texture
)
501 IDirect3DTexture9
**texptr
;
502 IDirect3DTexture9
*buftex
;
503 IDirect3DSurface9
*surface
;
504 BOOL file_width
= FALSE
, file_height
= FALSE
;
505 BOOL file_format
= FALSE
, file_miplevels
= FALSE
;
506 D3DXIMAGE_INFO imginfo
;
510 TRACE("(%p, %p, %u, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p)\n", device
, srcdata
, srcdatasize
, width
,
511 height
, miplevels
, usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
513 /* check for invalid parameters */
514 if (!device
|| !texture
|| !srcdata
|| !srcdatasize
)
515 return D3DERR_INVALIDCALL
;
517 hr
= D3DXGetImageInfoFromFileInMemory(srcdata
, srcdatasize
, &imginfo
);
525 /* handle default values */
526 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
527 width
= imginfo
.Width
;
529 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
530 height
= imginfo
.Height
;
532 if (width
== D3DX_DEFAULT
)
533 width
= make_pow2(imginfo
.Width
);
535 if (height
== D3DX_DEFAULT
)
536 height
= make_pow2(imginfo
.Height
);
538 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
539 format
= imginfo
.Format
;
541 if (width
== D3DX_FROM_FILE
)
544 width
= imginfo
.Width
;
547 if (height
== D3DX_FROM_FILE
)
550 height
= imginfo
.Height
;
553 if (format
== D3DFMT_FROM_FILE
)
556 format
= imginfo
.Format
;
559 if (miplevels
== D3DX_FROM_FILE
)
561 file_miplevels
= TRUE
;
562 miplevels
= imginfo
.MipLevels
;
565 /* fix texture creation parameters */
566 hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
);
574 if (((file_width
) && (width
!= imginfo
.Width
)) ||
575 ((file_height
) && (height
!= imginfo
.Height
)) ||
576 ((file_format
) && (format
!= imginfo
.Format
)) ||
577 ((file_miplevels
) && (miplevels
!= imginfo
.MipLevels
)))
579 return D3DERR_NOTAVAILABLE
;
582 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
583 return D3DERR_INVALIDCALL
;
585 /* Create the to-be-filled texture */
586 if ((caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (pool
!= D3DPOOL_DEFAULT
) && (usage
!= D3DUSAGE_DYNAMIC
))
588 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
593 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
604 IDirect3DTexture9_GetSurfaceLevel(*texptr
, 0, &surface
);
605 hr
= D3DXLoadSurfaceFromFileInMemory(surface
, palette
, NULL
, srcdata
, srcdatasize
, NULL
, filter
, colorkey
, NULL
);
606 IDirect3DSurface9_Release(surface
);
610 IDirect3DTexture9_Release(*texptr
);
615 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) *texptr
, palette
, 0, mipfilter
);
619 IDirect3DTexture9_Release(*texptr
);
624 /* Move the data to the actual texture if necessary */
625 if (texptr
== &buftex
)
627 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
631 IDirect3DTexture9_Release(buftex
);
636 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)(*texture
));
637 IDirect3DTexture9_Release(buftex
);
646 HRESULT WINAPI
D3DXCreateTextureFromFileInMemory(LPDIRECT3DDEVICE9 device
,
649 LPDIRECT3DTEXTURE9
*texture
)
651 TRACE("(%p, %p, %d, %p)\n", device
, srcdata
, srcdatasize
, texture
);
653 return D3DXCreateTextureFromFileInMemoryEx(device
, srcdata
, srcdatasize
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
654 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
657 HRESULT WINAPI
D3DXCreateTextureFromFileExW(LPDIRECT3DDEVICE9 device
,
668 D3DXIMAGE_INFO
*srcinfo
,
669 PALETTEENTRY
*palette
,
670 LPDIRECT3DTEXTURE9
*texture
)
676 TRACE("(%p, %s, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): relay\n",
677 device
, debugstr_w(srcfile
), width
, height
, miplevels
, usage
, format
, pool
, filter
,
678 mipfilter
, colorkey
, srcinfo
, palette
, texture
);
681 return D3DERR_INVALIDCALL
;
683 hr
= map_view_of_file(srcfile
, &buffer
, &size
);
685 return D3DXERR_INVALIDDATA
;
687 hr
= D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
, usage
, format
, pool
,
688 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
690 UnmapViewOfFile(buffer
);
695 HRESULT WINAPI
D3DXCreateTextureFromFileExA(LPDIRECT3DDEVICE9 device
,
706 D3DXIMAGE_INFO
*srcinfo
,
707 PALETTEENTRY
*palette
,
708 LPDIRECT3DTEXTURE9
*texture
)
714 TRACE("(%p, %s, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): relay\n",
715 device
, debugstr_a(srcfile
), width
, height
, miplevels
, usage
, format
, pool
, filter
,
716 mipfilter
, colorkey
, srcinfo
, palette
, texture
);
718 if (!device
|| !srcfile
|| !texture
)
719 return D3DERR_INVALIDCALL
;
721 len
= MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, NULL
, 0);
722 widename
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
));
723 MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, widename
, len
);
725 hr
= D3DXCreateTextureFromFileExW(device
, widename
, width
, height
, miplevels
,
726 usage
, format
, pool
, filter
, mipfilter
,
727 colorkey
, srcinfo
, palette
, texture
);
729 HeapFree(GetProcessHeap(), 0, widename
);
733 HRESULT WINAPI
D3DXCreateTextureFromFileA(LPDIRECT3DDEVICE9 device
,
735 LPDIRECT3DTEXTURE9
*texture
)
737 TRACE("(%p, %s, %p)\n", device
, debugstr_a(srcfile
), texture
);
739 return D3DXCreateTextureFromFileExA(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
740 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
743 HRESULT WINAPI
D3DXCreateTextureFromFileW(LPDIRECT3DDEVICE9 device
,
745 LPDIRECT3DTEXTURE9
*texture
)
747 TRACE("(%p, %s, %p)\n", device
, debugstr_w(srcfile
), texture
);
749 return D3DXCreateTextureFromFileExW(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
750 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
754 HRESULT WINAPI
D3DXCreateTextureFromResourceA(LPDIRECT3DDEVICE9 device
,
757 LPDIRECT3DTEXTURE9
*texture
)
759 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_a(resource
));
761 return D3DXCreateTextureFromResourceExA(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
762 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
765 HRESULT WINAPI
D3DXCreateTextureFromResourceW(LPDIRECT3DDEVICE9 device
,
768 LPDIRECT3DTEXTURE9
*texture
)
770 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_w(resource
));
772 return D3DXCreateTextureFromResourceExW(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
773 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
776 HRESULT WINAPI
D3DXCreateTextureFromResourceExA(LPDIRECT3DDEVICE9 device
,
788 D3DXIMAGE_INFO
*srcinfo
,
789 PALETTEENTRY
*palette
,
790 LPDIRECT3DTEXTURE9
*texture
)
794 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_a(resource
));
796 if (!device
|| !texture
)
797 return D3DERR_INVALIDCALL
;
799 resinfo
= FindResourceA(srcmodule
, resource
, (LPCSTR
) RT_RCDATA
);
807 hr
= load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
);
810 return D3DXERR_INVALIDDATA
;
812 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
,
813 height
, miplevels
, usage
, format
,
814 pool
, filter
, mipfilter
, colorkey
,
815 srcinfo
, palette
, texture
);
818 /* Try loading the resource as bitmap data */
819 resinfo
= FindResourceA(srcmodule
, resource
, (LPCSTR
) RT_BITMAP
);
823 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
827 return D3DXERR_INVALIDDATA
;
830 HRESULT WINAPI
D3DXCreateTextureFromResourceExW(LPDIRECT3DDEVICE9 device
,
842 D3DXIMAGE_INFO
*srcinfo
,
843 PALETTEENTRY
*palette
,
844 LPDIRECT3DTEXTURE9
*texture
)
848 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_w(resource
));
850 if (!device
|| !texture
)
851 return D3DERR_INVALIDCALL
;
853 resinfo
= FindResourceW(srcmodule
, resource
, (LPCWSTR
) RT_RCDATA
);
861 hr
= load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
);
864 return D3DXERR_INVALIDDATA
;
866 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
,
867 height
, miplevels
, usage
, format
,
868 pool
, filter
, mipfilter
, colorkey
,
869 srcinfo
, palette
, texture
);
872 /* Try loading the resource as bitmap data */
873 resinfo
= FindResourceW(srcmodule
, resource
, (LPCWSTR
) RT_BITMAP
);
877 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
881 return D3DXERR_INVALIDDATA
;
884 HRESULT WINAPI
D3DXCreateCubeTexture(LPDIRECT3DDEVICE9 device
,
890 LPDIRECT3DCUBETEXTURE9
*texture
)
894 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device
, size
, miplevels
, usage
, format
,
897 if (!device
|| !texture
)
898 return D3DERR_INVALIDCALL
;
900 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &miplevels
, usage
, &format
, pool
);
904 TRACE("D3DXCheckCubeTextureRequirements failed\n");
908 return IDirect3DDevice9_CreateCubeTexture(device
, size
, miplevels
, usage
, format
, pool
, texture
, NULL
);
911 HRESULT WINAPI
D3DXCreateVolumeTexture(LPDIRECT3DDEVICE9 device
,
919 LPDIRECT3DVOLUMETEXTURE9
*texture
)
923 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device
, width
, height
, depth
,
924 miplevels
, usage
, format
, pool
, texture
);
926 if (!device
|| !texture
)
927 return D3DERR_INVALIDCALL
;
929 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
,
930 &miplevels
, usage
, &format
, pool
);
934 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
938 return IDirect3DDevice9_CreateVolumeTexture(device
, width
, height
, depth
, miplevels
,
939 usage
, format
, pool
, texture
, NULL
);
942 HRESULT WINAPI
D3DXFillTexture(LPDIRECT3DTEXTURE9 texture
,
943 LPD3DXFILL2D function
,
947 DWORD m
, i
, x
, y
, c
, v
;
948 D3DSURFACE_DESC desc
;
949 D3DLOCKED_RECT lock_rect
;
951 D3DXVECTOR2 coord
, size
;
952 const PixelFormatDesc
*format
;
957 if (texture
== NULL
|| function
== NULL
)
958 return D3DERR_INVALIDCALL
;
960 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
962 for (m
= 0; m
< miplevels
; m
++)
964 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture
, m
, &desc
)))
965 return D3DERR_INVALIDCALL
;
967 format
= get_format_info(desc
.Format
);
968 if (format
->format
== D3DFMT_UNKNOWN
)
970 FIXME("Unsupported texture format %#x\n", desc
.Format
);
971 return D3DERR_INVALIDCALL
;
974 if (FAILED(IDirect3DTexture9_LockRect(texture
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
975 return D3DERR_INVALIDCALL
;
977 size
.x
= 1.0f
/ desc
.Width
;
978 size
.y
= 1.0f
/ desc
.Height
;
980 data
= lock_rect
.pBits
;
982 for (y
= 0; y
< desc
.Height
; y
++)
984 /* The callback function expects the coordinates of the center
986 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
988 for (x
= 0; x
< desc
.Width
; x
++)
990 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
992 function(&value
, &coord
, &size
, funcdata
);
994 pos
= data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
;
996 for (i
= 0; i
< format
->bytes_per_pixel
; i
++)
999 for (c
= 0; c
< 4; c
++)
1004 comp_value
= value
.w
;
1007 comp_value
= value
.x
;
1010 comp_value
= value
.y
;
1013 comp_value
= value
.z
;
1017 v
= comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
;
1019 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1021 mask
= ((1 << format
->bits
[c
]) - 1) << format
->shift
[c
] >> i
;
1022 byte
= (v
<< format
->shift
[c
] >> i
) & mask
;
1028 IDirect3DTexture9_UnlockRect(texture
, m
);
1034 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemoryEx(LPDIRECT3DDEVICE9 pDevice
, LPCVOID pSrcData
, UINT SrcDataSize
,
1035 UINT Size
, UINT MipLevels
, DWORD Usage
, D3DFORMAT Format
, D3DPOOL Pool
, DWORD Filter
, DWORD MipFilter
, D3DCOLOR ColorKey
,
1036 D3DXIMAGE_INFO
*pSrcInfo
, PALETTEENTRY
*pPalette
, LPDIRECT3DCUBETEXTURE9
*ppCubeTexture
)
1038 FIXME("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): stub\n", pDevice
, pSrcData
, SrcDataSize
, Size
, MipLevels
,
1039 Usage
, Format
, Pool
, Filter
, MipFilter
, ColorKey
, pSrcInfo
, pPalette
, ppCubeTexture
);
1054 static float get_cube_coord(enum cube_coord coord
, unsigned int x
, unsigned int y
, unsigned int size
)
1061 return size
- x
- 0.5f
;
1065 return size
- y
- 0.5f
;
1071 ERR("Unexpected coordinate value\n");
1076 HRESULT WINAPI
D3DXFillCubeTexture(LPDIRECT3DCUBETEXTURE9 texture
,
1077 LPD3DXFILL3D function
,
1081 DWORD m
, i
, x
, y
, c
, f
, v
;
1082 D3DSURFACE_DESC desc
;
1083 D3DLOCKED_RECT lock_rect
;
1085 D3DXVECTOR3 coord
, size
;
1086 const PixelFormatDesc
*format
;
1090 static const enum cube_coord coordmap
[6][3] =
1092 {ONE
, YCOORDINV
, XCOORDINV
},
1093 {ZERO
, YCOORDINV
, XCOORD
},
1094 {XCOORD
, ONE
, YCOORD
},
1095 {XCOORD
, ZERO
, YCOORDINV
},
1096 {XCOORD
, YCOORDINV
, ONE
},
1097 {XCOORDINV
, YCOORDINV
, ZERO
}
1100 if (texture
== NULL
|| function
== NULL
)
1101 return D3DERR_INVALIDCALL
;
1103 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1105 for (m
= 0; m
< miplevels
; m
++)
1107 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1108 return D3DERR_INVALIDCALL
;
1110 format
= get_format_info(desc
.Format
);
1111 if (format
->format
== D3DFMT_UNKNOWN
)
1113 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1114 return D3DERR_INVALIDCALL
;
1117 for (f
= 0; f
< 6; f
++)
1119 if (FAILED(IDirect3DCubeTexture9_LockRect(texture
, f
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1120 return D3DERR_INVALIDCALL
;
1122 size
.x
= (f
== 0) || (f
== 1) ? 0.0f
: 2.0f
/ desc
.Width
;
1123 size
.y
= (f
== 2) || (f
== 3) ? 0.0f
: 2.0f
/ desc
.Width
;
1124 size
.z
= (f
== 4) || (f
== 5) ? 0.0f
: 2.0f
/ desc
.Width
;
1126 data
= lock_rect
.pBits
;
1128 for (y
= 0; y
< desc
.Height
; y
++)
1130 for (x
= 0; x
< desc
.Width
; x
++)
1132 coord
.x
= get_cube_coord(coordmap
[f
][0], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1133 coord
.y
= get_cube_coord(coordmap
[f
][1], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1134 coord
.z
= get_cube_coord(coordmap
[f
][2], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1136 function(&value
, &coord
, &size
, funcdata
);
1138 pos
= data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
;
1140 for (i
= 0; i
< format
->bytes_per_pixel
; i
++)
1143 for (c
= 0; c
< 4; c
++)
1148 comp_value
= value
.w
;
1151 comp_value
= value
.x
;
1154 comp_value
= value
.y
;
1157 comp_value
= value
.z
;
1161 v
= comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
;
1163 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1165 mask
= ((1 << format
->bits
[c
]) - 1) << format
->shift
[c
] >> i
;
1166 byte
= (v
<< format
->shift
[c
] >> i
) & mask
;
1172 IDirect3DCubeTexture9_UnlockRect(texture
, f
, m
);
1179 HRESULT WINAPI
D3DXFillVolumeTexture(LPDIRECT3DVOLUMETEXTURE9 texture
,
1180 LPD3DXFILL3D function
,
1184 DWORD m
, i
, x
, y
, z
, c
, v
;
1185 D3DVOLUME_DESC desc
;
1186 D3DLOCKED_BOX lock_box
;
1188 D3DXVECTOR3 coord
, size
;
1189 const PixelFormatDesc
*format
;
1194 if (texture
== NULL
|| function
== NULL
)
1195 return D3DERR_INVALIDCALL
;
1197 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1199 for (m
= 0; m
< miplevels
; m
++)
1201 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1202 return D3DERR_INVALIDCALL
;
1204 format
= get_format_info(desc
.Format
);
1205 if (format
->format
== D3DFMT_UNKNOWN
)
1207 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1208 return D3DERR_INVALIDCALL
;
1211 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture
, m
, &lock_box
, NULL
, D3DLOCK_DISCARD
)))
1212 return D3DERR_INVALIDCALL
;
1214 size
.x
= 1.0f
/ desc
.Width
;
1215 size
.y
= 1.0f
/ desc
.Height
;
1216 size
.z
= 1.0f
/ desc
.Depth
;
1218 data
= lock_box
.pBits
;
1220 for (z
= 0; z
< desc
.Depth
; z
++)
1222 /* The callback function expects the coordinates of the center
1224 coord
.z
= (z
+ 0.5f
) / desc
.Depth
;
1226 for (y
= 0; y
< desc
.Height
; y
++)
1228 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1230 for (x
= 0; x
< desc
.Width
; x
++)
1232 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1234 function(&value
, &coord
, &size
, funcdata
);
1236 pos
= data
+ z
* lock_box
.SlicePitch
+ y
* lock_box
.RowPitch
+ x
* format
->bytes_per_pixel
;
1238 for (i
= 0; i
< format
->bytes_per_pixel
; i
++)
1241 for (c
= 0; c
< 4; c
++)
1246 comp_value
= value
.w
;
1249 comp_value
= value
.x
;
1252 comp_value
= value
.y
;
1255 comp_value
= value
.z
;
1259 v
= comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
;
1261 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1263 mask
= ((1 << format
->bits
[c
]) - 1) << format
->shift
[c
] >> i
;
1264 byte
= (v
<< format
->shift
[c
] >> i
) & mask
;
1271 IDirect3DVolumeTexture9_UnlockBox(texture
, m
);