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;
507 UINT loaded_miplevels;
511 TRACE("(%p, %p, %u, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p)\n", device, srcdata, srcdatasize, width,
512 height, miplevels, usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
514 /* check for invalid parameters */
515 if (!device || !texture || !srcdata || !srcdatasize)
516 return D3DERR_INVALIDCALL;
518 hr = D3DXGetImageInfoFromFileInMemory(srcdata, srcdatasize, &imginfo);
526 /* handle default values */
527 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
528 width = imginfo.Width;
530 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
531 height = imginfo.Height;
533 if (width == D3DX_DEFAULT)
534 width = make_pow2(imginfo.Width);
536 if (height == D3DX_DEFAULT)
537 height = make_pow2(imginfo.Height);
539 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
540 format = imginfo.Format;
542 if (width == D3DX_FROM_FILE)
545 width = imginfo.Width;
548 if (height == D3DX_FROM_FILE)
551 height = imginfo.Height;
554 if (format == D3DFMT_FROM_FILE)
557 format = imginfo.Format;
560 if (miplevels == D3DX_FROM_FILE)
562 file_miplevels = TRUE;
563 miplevels = imginfo.MipLevels;
566 /* fix texture creation parameters */
567 hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool);
575 if (imginfo.MipLevels < miplevels && (D3DFMT_DXT1 <= imginfo.Format && imginfo.Format <= D3DFMT_DXT5))
577 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet\n");
578 miplevels = imginfo.MipLevels;
581 if (((file_width) && (width != imginfo.Width)) ||
582 ((file_height) && (height != imginfo.Height)) ||
583 ((file_format) && (format != imginfo.Format)) ||
584 ((file_miplevels) && (miplevels != imginfo.MipLevels)))
586 return D3DERR_NOTAVAILABLE;
589 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
590 return D3DERR_INVALIDCALL;
592 /* Create the to-be-filled texture */
593 if (pool == D3DPOOL_DEFAULT && !((caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && usage == D3DUSAGE_DYNAMIC))
595 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
600 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
611 if (imginfo.ImageFileFormat != D3DXIFF_DDS)
613 IDirect3DTexture9_GetSurfaceLevel(*texptr, 0, &surface);
614 hr = D3DXLoadSurfaceFromFileInMemory(surface, palette, NULL, srcdata, srcdatasize, NULL, filter, colorkey, NULL);
615 IDirect3DSurface9_Release(surface);
619 hr = load_texture_from_dds(*texptr, srcdata, palette, filter, colorkey, &imginfo);
624 IDirect3DTexture9_Release(*texptr);
629 loaded_miplevels = min(miplevels, imginfo.MipLevels);
630 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) *texptr, palette, loaded_miplevels - 1, mipfilter);
634 IDirect3DTexture9_Release(*texptr);
639 /* Move the data to the actual texture if necessary */
640 if (texptr == &buftex)
642 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
646 IDirect3DTexture9_Release(buftex);
651 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9*)buftex, (IDirect3DBaseTexture9*)(*texture));
652 IDirect3DTexture9_Release(buftex);
661 HRESULT WINAPI D3DXCreateTextureFromFileInMemory(LPDIRECT3DDEVICE9 device,
664 LPDIRECT3DTEXTURE9 *texture)
666 TRACE("(%p, %p, %d, %p)\n", device, srcdata, srcdatasize, texture);
668 return D3DXCreateTextureFromFileInMemoryEx(device, srcdata, srcdatasize, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
669 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
672 HRESULT WINAPI D3DXCreateTextureFromFileExW(LPDIRECT3DDEVICE9 device,
683 D3DXIMAGE_INFO *srcinfo,
684 PALETTEENTRY *palette,
685 LPDIRECT3DTEXTURE9 *texture)
691 TRACE("(%p, %s, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): relay\n",
692 device, debugstr_w(srcfile), width, height, miplevels, usage, format, pool, filter,
693 mipfilter, colorkey, srcinfo, palette, texture);
696 return D3DERR_INVALIDCALL;
698 hr = map_view_of_file(srcfile, &buffer, &size);
700 return D3DXERR_INVALIDDATA;
702 hr = D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels, usage, format, pool,
703 filter, mipfilter, colorkey, srcinfo, palette, texture);
705 UnmapViewOfFile(buffer);
710 HRESULT WINAPI D3DXCreateTextureFromFileExA(LPDIRECT3DDEVICE9 device,
721 D3DXIMAGE_INFO *srcinfo,
722 PALETTEENTRY *palette,
723 LPDIRECT3DTEXTURE9 *texture)
729 TRACE("(%p, %s, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): relay\n",
730 device, debugstr_a(srcfile), width, height, miplevels, usage, format, pool, filter,
731 mipfilter, colorkey, srcinfo, palette, texture);
733 if (!device || !srcfile || !texture)
734 return D3DERR_INVALIDCALL;
736 len = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
737 widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR));
738 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, widename, len);
740 hr = D3DXCreateTextureFromFileExW(device, widename, width, height, miplevels,
741 usage, format, pool, filter, mipfilter,
742 colorkey, srcinfo, palette, texture);
744 HeapFree(GetProcessHeap(), 0, widename);
748 HRESULT WINAPI D3DXCreateTextureFromFileA(LPDIRECT3DDEVICE9 device,
750 LPDIRECT3DTEXTURE9 *texture)
752 TRACE("(%p, %s, %p)\n", device, debugstr_a(srcfile), texture);
754 return D3DXCreateTextureFromFileExA(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
755 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
758 HRESULT WINAPI D3DXCreateTextureFromFileW(LPDIRECT3DDEVICE9 device,
760 LPDIRECT3DTEXTURE9 *texture)
762 TRACE("(%p, %s, %p)\n", device, debugstr_w(srcfile), texture);
764 return D3DXCreateTextureFromFileExW(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
765 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
769 HRESULT WINAPI D3DXCreateTextureFromResourceA(LPDIRECT3DDEVICE9 device,
772 LPDIRECT3DTEXTURE9 *texture)
774 TRACE("(%p, %s): relay\n", srcmodule, debugstr_a(resource));
776 return D3DXCreateTextureFromResourceExA(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
777 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
780 HRESULT WINAPI D3DXCreateTextureFromResourceW(LPDIRECT3DDEVICE9 device,
783 LPDIRECT3DTEXTURE9 *texture)
785 TRACE("(%p, %s): relay\n", srcmodule, debugstr_w(resource));
787 return D3DXCreateTextureFromResourceExW(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
788 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
791 HRESULT WINAPI D3DXCreateTextureFromResourceExA(LPDIRECT3DDEVICE9 device,
803 D3DXIMAGE_INFO *srcinfo,
804 PALETTEENTRY *palette,
805 LPDIRECT3DTEXTURE9 *texture)
809 TRACE("(%p, %s): relay\n", srcmodule, debugstr_a(resource));
811 if (!device || !texture)
812 return D3DERR_INVALIDCALL;
814 resinfo = FindResourceA(srcmodule, resource, (LPCSTR) RT_RCDATA);
822 hr = load_resource_into_memory(srcmodule, resinfo, &buffer, &size);
825 return D3DXERR_INVALIDDATA;
827 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width,
828 height, miplevels, usage, format,
829 pool, filter, mipfilter, colorkey,
830 srcinfo, palette, texture);
833 /* Try loading the resource as bitmap data */
834 resinfo = FindResourceA(srcmodule, resource, (LPCSTR) RT_BITMAP);
838 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
842 return D3DXERR_INVALIDDATA;
845 HRESULT WINAPI D3DXCreateTextureFromResourceExW(LPDIRECT3DDEVICE9 device,
857 D3DXIMAGE_INFO *srcinfo,
858 PALETTEENTRY *palette,
859 LPDIRECT3DTEXTURE9 *texture)
863 TRACE("(%p, %s): relay\n", srcmodule, debugstr_w(resource));
865 if (!device || !texture)
866 return D3DERR_INVALIDCALL;
868 resinfo = FindResourceW(srcmodule, resource, (LPCWSTR) RT_RCDATA);
876 hr = load_resource_into_memory(srcmodule, resinfo, &buffer, &size);
879 return D3DXERR_INVALIDDATA;
881 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width,
882 height, miplevels, usage, format,
883 pool, filter, mipfilter, colorkey,
884 srcinfo, palette, texture);
887 /* Try loading the resource as bitmap data */
888 resinfo = FindResourceW(srcmodule, resource, (LPCWSTR) RT_BITMAP);
892 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
896 return D3DXERR_INVALIDDATA;
899 HRESULT WINAPI D3DXCreateCubeTexture(LPDIRECT3DDEVICE9 device,
905 LPDIRECT3DCUBETEXTURE9 *texture)
909 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device, size, miplevels, usage, format,
912 if (!device || !texture)
913 return D3DERR_INVALIDCALL;
915 hr = D3DXCheckCubeTextureRequirements(device, &size, &miplevels, usage, &format, pool);
919 TRACE("D3DXCheckCubeTextureRequirements failed\n");
923 return IDirect3DDevice9_CreateCubeTexture(device, size, miplevels, usage, format, pool, texture, NULL);
926 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemory(LPDIRECT3DDEVICE9 device,
929 LPDIRECT3DCUBETEXTURE9 *texture)
931 TRACE("(%p, %p, %u, %p)\n", device, data, datasize, texture);
933 return D3DXCreateCubeTextureFromFileInMemoryEx(device, data, datasize, D3DX_DEFAULT, D3DX_DEFAULT,
934 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
937 HRESULT WINAPI D3DXCreateVolumeTexture(LPDIRECT3DDEVICE9 device,
945 LPDIRECT3DVOLUMETEXTURE9 *texture)
949 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device, width, height, depth,
950 miplevels, usage, format, pool, texture);
952 if (!device || !texture)
953 return D3DERR_INVALIDCALL;
955 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth,
956 &miplevels, usage, &format, pool);
960 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
964 return IDirect3DDevice9_CreateVolumeTexture(device, width, height, depth, miplevels,
965 usage, format, pool, texture, NULL);
968 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemory(LPDIRECT3DDEVICE9 device,
971 LPDIRECT3DVOLUMETEXTURE9 *texture)
973 TRACE("(%p, %p, %u, %p)\n", device, data, datasize, texture);
975 return D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, datasize, D3DX_DEFAULT, D3DX_DEFAULT,
976 D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
977 0, NULL, NULL, texture);
980 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemoryEx(LPDIRECT3DDEVICE9 device,
993 D3DXIMAGE_INFO *imageinfo,
994 PALETTEENTRY *palette,
995 LPDIRECT3DVOLUMETEXTURE9 *texture)
997 FIXME("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p) : stub\n",
998 device, data, datasize, width, height, depth, miplevels, usage, format, pool,
999 filter, mipfilter, colorkey, imageinfo, palette, texture);
1004 HRESULT WINAPI D3DXFillTexture(LPDIRECT3DTEXTURE9 texture,
1005 LPD3DXFILL2D function,
1009 DWORD m, i, x, y, c, v;
1010 D3DSURFACE_DESC desc;
1011 D3DLOCKED_RECT lock_rect;
1013 D3DXVECTOR2 coord, size;
1014 const PixelFormatDesc *format;
1019 if (texture == NULL || function == NULL)
1020 return D3DERR_INVALIDCALL;
1022 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1024 for (m = 0; m < miplevels; m++)
1026 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture, m, &desc)))
1027 return D3DERR_INVALIDCALL;
1029 format = get_format_info(desc.Format);
1030 if (format->format == D3DFMT_UNKNOWN)
1032 FIXME("Unsupported texture format %#x\n", desc.Format);
1033 return D3DERR_INVALIDCALL;
1036 if (FAILED(IDirect3DTexture9_LockRect(texture, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1037 return D3DERR_INVALIDCALL;
1039 size.x = 1.0f / desc.Width;
1040 size.y = 1.0f / desc.Height;
1042 data = lock_rect.pBits;
1044 for (y = 0; y < desc.Height; y++)
1046 /* The callback function expects the coordinates of the center
1048 coord.y = (y + 0.5f) / desc.Height;
1050 for (x = 0; x < desc.Width; x++)
1052 coord.x = (x + 0.5f) / desc.Width;
1054 function(&value, &coord, &size, funcdata);
1056 pos = data + y * lock_rect.Pitch + x * format->bytes_per_pixel;
1058 for (i = 0; i < format->bytes_per_pixel; i++)
1061 for (c = 0; c < 4; c++)
1066 comp_value = value.w;
1069 comp_value = value.x;
1072 comp_value = value.y;
1075 comp_value = value.z;
1079 v = comp_value * ((1 << format->bits[c]) - 1) + 0.5f;
1081 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1083 mask = ((1 << format->bits[c]) - 1) << format->shift[c] >> i;
1084 byte = (v << format->shift[c] >> i) & mask;
1090 IDirect3DTexture9_UnlockRect(texture, m);
1096 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
1097 const void *src_data,
1107 D3DXIMAGE_INFO *src_info,
1108 PALETTEENTRY *palette,
1109 IDirect3DCubeTexture9 **cube_texture)
1113 UINT loaded_miplevels;
1114 D3DXIMAGE_INFO img_info;
1115 BOOL file_size = FALSE;
1116 BOOL file_format = FALSE;
1117 BOOL file_mip_levels = FALSE;
1118 IDirect3DCubeTexture9 *tex, *buftex;
1120 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device,
1121 src_data, src_data_size, size, mip_levels, usage, format, pool, filter, mip_filter,
1122 color_key, src_info, palette, cube_texture);
1124 if (!device || !cube_texture || !src_data || !src_data_size)
1125 return D3DERR_INVALIDCALL;
1127 hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &img_info);
1131 if (img_info.ImageFileFormat != D3DXIFF_DDS)
1132 return D3DXERR_INVALIDDATA;
1134 if (img_info.Width != img_info.Height)
1135 return D3DXERR_INVALIDDATA;
1137 if (size == 0 || size == D3DX_DEFAULT_NONPOW2)
1138 size = img_info.Width;
1139 if (size == D3DX_DEFAULT)
1140 size = make_pow2(img_info.Width);
1142 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1143 format = img_info.Format;
1145 if (size == D3DX_FROM_FILE)
1148 size = img_info.Width;
1151 if (format == D3DFMT_FROM_FILE)
1154 format = img_info.Format;
1157 if (mip_levels == D3DX_FROM_FILE)
1159 file_mip_levels = TRUE;
1160 mip_levels = img_info.MipLevels;
1163 hr = D3DXCheckCubeTextureRequirements(device, &size, &mip_levels, usage, &format, pool);
1167 if ((file_size && size != img_info.Width)
1168 || (file_format && format != img_info.Format)
1169 || (file_mip_levels && mip_levels != img_info.MipLevels))
1170 return D3DERR_NOTAVAILABLE;
1172 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1174 return D3DERR_INVALIDCALL;
1176 if (mip_levels > img_info.MipLevels && (D3DFMT_DXT1 <= img_info.Format && img_info.Format <= D3DFMT_DXT5))
1178 FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
1179 mip_levels = img_info.MipLevels;
1182 if (pool == D3DPOOL_DEFAULT && !((caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && usage == D3DUSAGE_DYNAMIC))
1184 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
1189 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1195 hr = load_cube_texture_from_dds(tex, src_data, palette, filter, color_key, &img_info);
1198 IDirect3DCubeTexture9_Release(tex);
1202 loaded_miplevels = min(mip_levels, img_info.MipLevels);
1203 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, palette, loaded_miplevels - 1, mip_filter);
1206 IDirect3DCubeTexture9_Release(tex);
1212 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1215 IDirect3DCubeTexture9_Release(buftex);
1219 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1220 IDirect3DCubeTexture9_Release(buftex);
1224 *src_info = img_info;
1226 *cube_texture = tex;
1231 HRESULT WINAPI D3DXCreateCubeTextureFromFileA(IDirect3DDevice9 *device,
1232 const char *src_filename,
1233 IDirect3DCubeTexture9 **cube_texture)
1241 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_a(src_filename), cube_texture);
1243 if (!src_filename) return D3DERR_INVALIDCALL;
1245 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1246 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1247 if (!filename) return E_OUTOFMEMORY;
1248 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1250 hr = map_view_of_file(filename, &data, &data_size);
1253 HeapFree(GetProcessHeap(), 0, filename);
1254 return D3DXERR_INVALIDDATA;
1257 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1258 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1260 UnmapViewOfFile(data);
1261 HeapFree(GetProcessHeap(), 0, filename);
1265 HRESULT WINAPI D3DXCreateCubeTextureFromFileW(IDirect3DDevice9 *device,
1266 const WCHAR *src_filename,
1267 IDirect3DCubeTexture9 **cube_texture)
1273 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_w(src_filename), cube_texture);
1275 hr = map_view_of_file(src_filename, &data, &data_size);
1276 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1278 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1279 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1281 UnmapViewOfFile(data);
1285 HRESULT WINAPI D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9 *device,
1286 const char *src_filename,
1295 D3DXIMAGE_INFO *image_info,
1296 PALETTEENTRY *palette,
1297 IDirect3DCubeTexture9 **cube_texture)
1305 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1306 device, wine_dbgstr_a(src_filename), size, mip_levels, usage, format,
1307 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1309 if (!src_filename) return D3DERR_INVALIDCALL;
1311 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1312 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1313 if (!filename) return E_OUTOFMEMORY;
1314 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1316 hr = map_view_of_file(filename, &data, &data_size);
1319 HeapFree(GetProcessHeap(), 0, filename);
1320 return D3DXERR_INVALIDDATA;
1323 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1324 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1326 UnmapViewOfFile(data);
1327 HeapFree(GetProcessHeap(), 0, filename);
1331 HRESULT WINAPI D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9 *device,
1332 const WCHAR *src_filename,
1341 D3DXIMAGE_INFO *image_info,
1342 PALETTEENTRY *palette,
1343 IDirect3DCubeTexture9 **cube_texture)
1349 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1350 device, wine_dbgstr_w(src_filename), size, mip_levels, usage, format,
1351 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1353 hr = map_view_of_file(src_filename, &data, &data_size);
1354 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1356 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1357 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1359 UnmapViewOfFile(data);
1373 static float get_cube_coord(enum cube_coord coord, unsigned int x, unsigned int y, unsigned int size)
1380 return size - x - 0.5f;
1384 return size - y - 0.5f;
1390 ERR("Unexpected coordinate value\n");
1395 HRESULT WINAPI D3DXFillCubeTexture(LPDIRECT3DCUBETEXTURE9 texture,
1396 LPD3DXFILL3D function,
1400 DWORD m, i, x, y, c, f, v;
1401 D3DSURFACE_DESC desc;
1402 D3DLOCKED_RECT lock_rect;
1404 D3DXVECTOR3 coord, size;
1405 const PixelFormatDesc *format;
1409 static const enum cube_coord coordmap[6][3] =
1411 {ONE, YCOORDINV, XCOORDINV},
1412 {ZERO, YCOORDINV, XCOORD},
1413 {XCOORD, ONE, YCOORD},
1414 {XCOORD, ZERO, YCOORDINV},
1415 {XCOORD, YCOORDINV, ONE},
1416 {XCOORDINV, YCOORDINV, ZERO}
1419 if (texture == NULL || function == NULL)
1420 return D3DERR_INVALIDCALL;
1422 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1424 for (m = 0; m < miplevels; m++)
1426 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture, m, &desc)))
1427 return D3DERR_INVALIDCALL;
1429 format = get_format_info(desc.Format);
1430 if (format->format == D3DFMT_UNKNOWN)
1432 FIXME("Unsupported texture format %#x\n", desc.Format);
1433 return D3DERR_INVALIDCALL;
1436 for (f = 0; f < 6; f++)
1438 if (FAILED(IDirect3DCubeTexture9_LockRect(texture, f, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1439 return D3DERR_INVALIDCALL;
1441 size.x = (f == 0) || (f == 1) ? 0.0f : 2.0f / desc.Width;
1442 size.y = (f == 2) || (f == 3) ? 0.0f : 2.0f / desc.Width;
1443 size.z = (f == 4) || (f == 5) ? 0.0f : 2.0f / desc.Width;
1445 data = lock_rect.pBits;
1447 for (y = 0; y < desc.Height; y++)
1449 for (x = 0; x < desc.Width; x++)
1451 coord.x = get_cube_coord(coordmap[f][0], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1452 coord.y = get_cube_coord(coordmap[f][1], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1453 coord.z = get_cube_coord(coordmap[f][2], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1455 function(&value, &coord, &size, funcdata);
1457 pos = data + y * lock_rect.Pitch + x * format->bytes_per_pixel;
1459 for (i = 0; i < format->bytes_per_pixel; i++)
1462 for (c = 0; c < 4; c++)
1467 comp_value = value.w;
1470 comp_value = value.x;
1473 comp_value = value.y;
1476 comp_value = value.z;
1480 v = comp_value * ((1 << format->bits[c]) - 1) + 0.5f;
1482 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1484 mask = ((1 << format->bits[c]) - 1) << format->shift[c] >> i;
1485 byte = (v << format->shift[c] >> i) & mask;
1491 IDirect3DCubeTexture9_UnlockRect(texture, f, m);
1498 HRESULT WINAPI D3DXFillVolumeTexture(LPDIRECT3DVOLUMETEXTURE9 texture,
1499 LPD3DXFILL3D function,
1503 DWORD m, i, x, y, z, c, v;
1504 D3DVOLUME_DESC desc;
1505 D3DLOCKED_BOX lock_box;
1507 D3DXVECTOR3 coord, size;
1508 const PixelFormatDesc *format;
1513 if (texture == NULL || function == NULL)
1514 return D3DERR_INVALIDCALL;
1516 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1518 for (m = 0; m < miplevels; m++)
1520 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture, m, &desc)))
1521 return D3DERR_INVALIDCALL;
1523 format = get_format_info(desc.Format);
1524 if (format->format == D3DFMT_UNKNOWN)
1526 FIXME("Unsupported texture format %#x\n", desc.Format);
1527 return D3DERR_INVALIDCALL;
1530 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture, m, &lock_box, NULL, D3DLOCK_DISCARD)))
1531 return D3DERR_INVALIDCALL;
1533 size.x = 1.0f / desc.Width;
1534 size.y = 1.0f / desc.Height;
1535 size.z = 1.0f / desc.Depth;
1537 data = lock_box.pBits;
1539 for (z = 0; z < desc.Depth; z++)
1541 /* The callback function expects the coordinates of the center
1543 coord.z = (z + 0.5f) / desc.Depth;
1545 for (y = 0; y < desc.Height; y++)
1547 coord.y = (y + 0.5f) / desc.Height;
1549 for (x = 0; x < desc.Width; x++)
1551 coord.x = (x + 0.5f) / desc.Width;
1553 function(&value, &coord, &size, funcdata);
1555 pos = data + z * lock_box.SlicePitch + y * lock_box.RowPitch + x * format->bytes_per_pixel;
1557 for (i = 0; i < format->bytes_per_pixel; i++)
1560 for (c = 0; c < 4; c++)
1565 comp_value = value.w;
1568 comp_value = value.x;
1571 comp_value = value.y;
1574 comp_value = value.z;
1578 v = comp_value * ((1 << format->bits[c]) - 1) + 0.5f;
1580 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1582 mask = ((1 << format->bits[c]) - 1) << format->shift[c] >> i;
1583 byte = (v << format->shift[c] >> i) & mask;
1590 IDirect3DVolumeTexture9_UnlockBox(texture, m);