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("(%p, %s, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): relay\n",
699 device, debugstr_w(srcfile), width, height, miplevels, usage, format, pool, filter,
700 mipfilter, colorkey, srcinfo, palette, texture);
703 return D3DERR_INVALIDCALL;
705 hr = map_view_of_file(srcfile, &buffer, &size);
707 return D3DXERR_INVALIDDATA;
709 hr = D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels, usage, format, pool,
710 filter, mipfilter, colorkey, srcinfo, palette, texture);
712 UnmapViewOfFile(buffer);
717 HRESULT WINAPI D3DXCreateTextureFromFileExA(struct IDirect3DDevice9 *device, const char *srcfile,
718 UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
719 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
720 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
726 TRACE("(%p, %s, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): relay\n",
727 device, debugstr_a(srcfile), width, height, miplevels, usage, format, pool, filter,
728 mipfilter, colorkey, srcinfo, palette, texture);
730 if (!device || !srcfile || !texture)
731 return D3DERR_INVALIDCALL;
733 len = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
734 widename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*widename));
735 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, widename, len);
737 hr = D3DXCreateTextureFromFileExW(device, widename, width, height, miplevels,
738 usage, format, pool, filter, mipfilter,
739 colorkey, srcinfo, palette, texture);
741 HeapFree(GetProcessHeap(), 0, widename);
745 HRESULT WINAPI D3DXCreateTextureFromFileA(struct IDirect3DDevice9 *device,
746 const char *srcfile, struct IDirect3DTexture9 **texture)
748 TRACE("(%p, %s, %p)\n", device, debugstr_a(srcfile), texture);
750 return D3DXCreateTextureFromFileExA(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
751 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
754 HRESULT WINAPI D3DXCreateTextureFromFileW(struct IDirect3DDevice9 *device,
755 const WCHAR *srcfile, struct IDirect3DTexture9 **texture)
757 TRACE("(%p, %s, %p)\n", device, debugstr_w(srcfile), texture);
759 return D3DXCreateTextureFromFileExW(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
760 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
764 HRESULT WINAPI D3DXCreateTextureFromResourceA(struct IDirect3DDevice9 *device,
765 HMODULE srcmodule, const char *resource, struct IDirect3DTexture9 **texture)
767 TRACE("(%p, %s): relay\n", srcmodule, debugstr_a(resource));
769 return D3DXCreateTextureFromResourceExA(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
770 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
773 HRESULT WINAPI D3DXCreateTextureFromResourceW(struct IDirect3DDevice9 *device,
774 HMODULE srcmodule, const WCHAR *resource, struct IDirect3DTexture9 **texture)
776 TRACE("(%p, %s): relay\n", srcmodule, debugstr_w(resource));
778 return D3DXCreateTextureFromResourceExW(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
779 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
782 HRESULT WINAPI D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9 *device, HMODULE srcmodule,
783 const char *resource, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
784 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
785 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
789 TRACE("(%p, %s): relay\n", srcmodule, debugstr_a(resource));
791 if (!device || !texture)
792 return D3DERR_INVALIDCALL;
794 resinfo = FindResourceA(srcmodule, resource, (LPCSTR) RT_RCDATA);
802 hr = load_resource_into_memory(srcmodule, resinfo, &buffer, &size);
805 return D3DXERR_INVALIDDATA;
807 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width,
808 height, miplevels, usage, format,
809 pool, filter, mipfilter, colorkey,
810 srcinfo, palette, texture);
813 /* Try loading the resource as bitmap data */
814 resinfo = FindResourceA(srcmodule, resource, (LPCSTR) RT_BITMAP);
818 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
822 return D3DXERR_INVALIDDATA;
825 HRESULT WINAPI D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9 *device, HMODULE srcmodule,
826 const WCHAR *resource, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
827 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
828 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
832 TRACE("(%p, %s): relay\n", srcmodule, debugstr_w(resource));
834 if (!device || !texture)
835 return D3DERR_INVALIDCALL;
837 resinfo = FindResourceW(srcmodule, resource, (LPCWSTR) RT_RCDATA);
845 hr = load_resource_into_memory(srcmodule, resinfo, &buffer, &size);
848 return D3DXERR_INVALIDDATA;
850 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width,
851 height, miplevels, usage, format,
852 pool, filter, mipfilter, colorkey,
853 srcinfo, palette, texture);
856 /* Try loading the resource as bitmap data */
857 resinfo = FindResourceW(srcmodule, resource, (LPCWSTR) RT_BITMAP);
861 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
865 return D3DXERR_INVALIDDATA;
868 HRESULT WINAPI D3DXCreateCubeTexture(struct IDirect3DDevice9 *device, UINT size, UINT miplevels,
869 DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DCubeTexture9 **texture)
873 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device, size, miplevels, usage, format,
876 if (!device || !texture)
877 return D3DERR_INVALIDCALL;
879 hr = D3DXCheckCubeTextureRequirements(device, &size, &miplevels, usage, &format, pool);
883 TRACE("D3DXCheckCubeTextureRequirements failed\n");
887 return IDirect3DDevice9_CreateCubeTexture(device, size, miplevels, usage, format, pool, texture, NULL);
890 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9 *device,
891 const void *data, UINT datasize, struct IDirect3DCubeTexture9 **texture)
893 TRACE("(%p, %p, %u, %p)\n", device, data, datasize, texture);
895 return D3DXCreateCubeTextureFromFileInMemoryEx(device, data, datasize, D3DX_DEFAULT, D3DX_DEFAULT,
896 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
899 HRESULT WINAPI D3DXCreateVolumeTexture(struct IDirect3DDevice9 *device, UINT width, UINT height, UINT depth,
900 UINT miplevels, DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DVolumeTexture9 **texture)
904 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device, width, height, depth,
905 miplevels, usage, format, pool, texture);
907 if (!device || !texture)
908 return D3DERR_INVALIDCALL;
910 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth,
911 &miplevels, usage, &format, pool);
915 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
919 return IDirect3DDevice9_CreateVolumeTexture(device, width, height, depth, miplevels,
920 usage, format, pool, texture, NULL);
923 HRESULT WINAPI D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9 *device,
924 const char *filename,
925 IDirect3DVolumeTexture9 **volume_texture)
933 TRACE("(%p, %s, %p): relay\n",
934 device, debugstr_a(filename), volume_texture);
936 if (!filename) return D3DERR_INVALIDCALL;
938 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
939 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
940 if (!filenameW) return E_OUTOFMEMORY;
941 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
943 hr = map_view_of_file(filenameW, &data, &data_size);
944 HeapFree(GetProcessHeap(), 0, filenameW);
945 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
947 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
948 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
950 UnmapViewOfFile(data);
954 HRESULT WINAPI D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9 *device,
955 const WCHAR *filename,
956 IDirect3DVolumeTexture9 **volume_texture)
962 TRACE("(%p, %s, %p): relay\n",
963 device, debugstr_w(filename), volume_texture);
965 if (!filename) return D3DERR_INVALIDCALL;
967 hr = map_view_of_file(filename, &data, &data_size);
968 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
970 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
971 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
973 UnmapViewOfFile(data);
977 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9 *device,
978 const char *filename,
989 D3DXIMAGE_INFO *src_info,
990 PALETTEENTRY *palette,
991 IDirect3DVolumeTexture9 **volume_texture)
999 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1000 device, debugstr_a(filename), width, height, depth, mip_levels,
1001 usage, format, pool, filter, mip_filter, color_key, src_info,
1002 palette, volume_texture);
1004 if (!filename) return D3DERR_INVALIDCALL;
1006 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
1007 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1008 if (!filenameW) return E_OUTOFMEMORY;
1009 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
1011 hr = map_view_of_file(filenameW, &data, &data_size);
1012 HeapFree(GetProcessHeap(), 0, filenameW);
1013 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1015 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1016 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1019 UnmapViewOfFile(data);
1023 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9 *device,
1024 const WCHAR *filename,
1035 D3DXIMAGE_INFO *src_info,
1036 PALETTEENTRY *palette,
1037 IDirect3DVolumeTexture9 **volume_texture)
1043 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1044 device, debugstr_w(filename), width, height, depth, mip_levels,
1045 usage, format, pool, filter, mip_filter, color_key, src_info,
1046 palette, volume_texture);
1048 if (!filename) return D3DERR_INVALIDCALL;
1050 hr = map_view_of_file(filename, &data, &data_size);
1051 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1053 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1054 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1057 UnmapViewOfFile(data);
1061 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9 *device,
1064 IDirect3DVolumeTexture9 **volume_texture)
1066 TRACE("(%p, %p, %u, %p): relay\n", device, data, data_size, volume_texture);
1068 return D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1069 D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
1070 0, NULL, NULL, volume_texture);
1073 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
1086 D3DXIMAGE_INFO *info,
1087 PALETTEENTRY *palette,
1088 IDirect3DVolumeTexture9 **volume_texture)
1092 D3DXIMAGE_INFO image_info;
1093 BOOL dynamic_texture;
1094 BOOL file_width = FALSE;
1095 BOOL file_height = FALSE;
1096 BOOL file_depth = FALSE;
1097 BOOL file_format = FALSE;
1098 BOOL file_mip_levels = FALSE;
1099 IDirect3DVolumeTexture9 *tex, *buftex;
1101 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1102 device, data, data_size, width, height, depth, mip_levels, usage, format, pool,
1103 filter, mip_filter, color_key, info, palette, volume_texture);
1105 if (!device || !data || !data_size || !volume_texture)
1106 return D3DERR_INVALIDCALL;
1108 hr = D3DXGetImageInfoFromFileInMemory(data, data_size, &image_info);
1109 if (FAILED(hr)) return hr;
1111 if (image_info.ImageFileFormat != D3DXIFF_DDS)
1112 return D3DXERR_INVALIDDATA;
1114 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
1115 width = image_info.Width;
1116 if (width == D3DX_DEFAULT)
1117 width = make_pow2(image_info.Width);
1119 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
1120 height = image_info.Height;
1121 if (height == D3DX_DEFAULT)
1122 height = make_pow2(image_info.Height);
1124 if (depth == 0 || depth == D3DX_DEFAULT_NONPOW2)
1125 depth = image_info.Depth;
1126 if (depth == D3DX_DEFAULT)
1127 depth = make_pow2(image_info.Depth);
1129 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1130 format = image_info.Format;
1132 if (width == D3DX_FROM_FILE)
1135 width = image_info.Width;
1138 if (height == D3DX_FROM_FILE)
1141 height = image_info.Height;
1144 if (depth == D3DX_FROM_FILE)
1147 depth = image_info.Depth;
1150 if (format == D3DFMT_FROM_FILE)
1153 format = image_info.Format;
1156 if (mip_levels == D3DX_FROM_FILE)
1158 file_mip_levels = TRUE;
1159 mip_levels = image_info.MipLevels;
1162 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, &mip_levels, usage, &format, pool);
1163 if (FAILED(hr)) return hr;
1165 if ((file_width && width != image_info.Width)
1166 || (file_height && height != image_info.Height)
1167 || (file_depth && depth != image_info.Depth)
1168 || (file_format && format != image_info.Format)
1169 || (file_mip_levels && mip_levels != image_info.MipLevels))
1170 return D3DERR_NOTAVAILABLE;
1172 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1174 return D3DERR_INVALIDCALL;
1176 if (mip_levels > image_info.MipLevels)
1178 FIXME("Generation of mipmaps for volume textures is not implemented yet\n");
1179 mip_levels = image_info.MipLevels;
1182 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1183 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1185 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
1190 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1194 if (FAILED(hr)) return hr;
1196 hr = load_volume_texture_from_dds(tex, data, palette, filter, color_key, &image_info);
1199 IDirect3DVolumeTexture9_Release(tex);
1205 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1208 IDirect3DVolumeTexture9_Release(buftex);
1212 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1213 IDirect3DVolumeTexture9_Release(buftex);
1219 *volume_texture = tex;
1223 HRESULT WINAPI D3DXFillTexture(struct IDirect3DTexture9 *texture, LPD3DXFILL2D function, void *funcdata)
1226 DWORD m, i, x, y, c, v;
1227 D3DSURFACE_DESC desc;
1228 D3DLOCKED_RECT lock_rect;
1230 D3DXVECTOR2 coord, size;
1231 const struct pixel_format_desc *format;
1236 if (texture == NULL || function == NULL)
1237 return D3DERR_INVALIDCALL;
1239 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1241 for (m = 0; m < miplevels; m++)
1243 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture, m, &desc)))
1244 return D3DERR_INVALIDCALL;
1246 format = get_format_info(desc.Format);
1247 if (format->type != FORMAT_ARGB)
1249 FIXME("Unsupported texture format %#x\n", desc.Format);
1250 return D3DERR_INVALIDCALL;
1253 if (FAILED(IDirect3DTexture9_LockRect(texture, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1254 return D3DERR_INVALIDCALL;
1256 size.x = 1.0f / desc.Width;
1257 size.y = 1.0f / desc.Height;
1259 data = lock_rect.pBits;
1261 for (y = 0; y < desc.Height; y++)
1263 /* The callback function expects the coordinates of the center
1265 coord.y = (y + 0.5f) / desc.Height;
1267 for (x = 0; x < desc.Width; x++)
1269 coord.x = (x + 0.5f) / desc.Width;
1271 function(&value, &coord, &size, funcdata);
1273 pos = data + y * lock_rect.Pitch + x * format->bytes_per_pixel;
1275 for (i = 0; i < format->bytes_per_pixel; i++)
1278 for (c = 0; c < 4; c++)
1283 comp_value = value.w;
1286 comp_value = value.x;
1289 comp_value = value.y;
1292 comp_value = value.z;
1296 v = comp_value * ((1 << format->bits[c]) - 1) + 0.5f;
1298 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1300 mask = ((1 << format->bits[c]) - 1) << format->shift[c] >> i;
1301 byte = (v << format->shift[c] >> i) & mask;
1307 IDirect3DTexture9_UnlockRect(texture, m);
1313 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
1314 const void *src_data,
1324 D3DXIMAGE_INFO *src_info,
1325 PALETTEENTRY *palette,
1326 IDirect3DCubeTexture9 **cube_texture)
1330 UINT loaded_miplevels;
1331 D3DXIMAGE_INFO img_info;
1332 BOOL dynamic_texture;
1333 BOOL file_size = FALSE;
1334 BOOL file_format = FALSE;
1335 BOOL file_mip_levels = FALSE;
1336 IDirect3DCubeTexture9 *tex, *buftex;
1338 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device,
1339 src_data, src_data_size, size, mip_levels, usage, format, pool, filter, mip_filter,
1340 color_key, src_info, palette, cube_texture);
1342 if (!device || !cube_texture || !src_data || !src_data_size)
1343 return D3DERR_INVALIDCALL;
1345 hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &img_info);
1349 if (img_info.ImageFileFormat != D3DXIFF_DDS)
1350 return D3DXERR_INVALIDDATA;
1352 if (img_info.Width != img_info.Height)
1353 return D3DXERR_INVALIDDATA;
1355 if (size == 0 || size == D3DX_DEFAULT_NONPOW2)
1356 size = img_info.Width;
1357 if (size == D3DX_DEFAULT)
1358 size = make_pow2(img_info.Width);
1360 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1361 format = img_info.Format;
1363 if (size == D3DX_FROM_FILE)
1366 size = img_info.Width;
1369 if (format == D3DFMT_FROM_FILE)
1372 format = img_info.Format;
1375 if (mip_levels == D3DX_FROM_FILE)
1377 file_mip_levels = TRUE;
1378 mip_levels = img_info.MipLevels;
1381 hr = D3DXCheckCubeTextureRequirements(device, &size, &mip_levels, usage, &format, pool);
1385 if ((file_size && size != img_info.Width)
1386 || (file_format && format != img_info.Format)
1387 || (file_mip_levels && mip_levels != img_info.MipLevels))
1388 return D3DERR_NOTAVAILABLE;
1390 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1392 return D3DERR_INVALIDCALL;
1394 if (mip_levels > img_info.MipLevels && (D3DFMT_DXT1 <= img_info.Format && img_info.Format <= D3DFMT_DXT5))
1396 FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
1397 mip_levels = img_info.MipLevels;
1400 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1401 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1403 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
1408 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1414 hr = load_cube_texture_from_dds(tex, src_data, palette, filter, color_key, &img_info);
1417 IDirect3DCubeTexture9_Release(tex);
1421 loaded_miplevels = min(IDirect3DCubeTexture9_GetLevelCount(tex), img_info.MipLevels);
1422 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, palette, loaded_miplevels - 1, mip_filter);
1425 IDirect3DCubeTexture9_Release(tex);
1431 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1434 IDirect3DCubeTexture9_Release(buftex);
1438 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1439 IDirect3DCubeTexture9_Release(buftex);
1443 *src_info = img_info;
1445 *cube_texture = tex;
1450 HRESULT WINAPI D3DXCreateCubeTextureFromFileA(IDirect3DDevice9 *device,
1451 const char *src_filename,
1452 IDirect3DCubeTexture9 **cube_texture)
1460 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_a(src_filename), cube_texture);
1462 if (!src_filename) return D3DERR_INVALIDCALL;
1464 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1465 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1466 if (!filename) return E_OUTOFMEMORY;
1467 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1469 hr = map_view_of_file(filename, &data, &data_size);
1472 HeapFree(GetProcessHeap(), 0, filename);
1473 return D3DXERR_INVALIDDATA;
1476 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1477 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1479 UnmapViewOfFile(data);
1480 HeapFree(GetProcessHeap(), 0, filename);
1484 HRESULT WINAPI D3DXCreateCubeTextureFromFileW(IDirect3DDevice9 *device,
1485 const WCHAR *src_filename,
1486 IDirect3DCubeTexture9 **cube_texture)
1492 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_w(src_filename), cube_texture);
1494 hr = map_view_of_file(src_filename, &data, &data_size);
1495 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1497 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1498 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1500 UnmapViewOfFile(data);
1504 HRESULT WINAPI D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9 *device,
1505 const char *src_filename,
1514 D3DXIMAGE_INFO *image_info,
1515 PALETTEENTRY *palette,
1516 IDirect3DCubeTexture9 **cube_texture)
1524 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1525 device, wine_dbgstr_a(src_filename), size, mip_levels, usage, format,
1526 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1528 if (!src_filename) return D3DERR_INVALIDCALL;
1530 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1531 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1532 if (!filename) return E_OUTOFMEMORY;
1533 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1535 hr = map_view_of_file(filename, &data, &data_size);
1538 HeapFree(GetProcessHeap(), 0, filename);
1539 return D3DXERR_INVALIDDATA;
1542 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1543 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1545 UnmapViewOfFile(data);
1546 HeapFree(GetProcessHeap(), 0, filename);
1550 HRESULT WINAPI D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9 *device,
1551 const WCHAR *src_filename,
1560 D3DXIMAGE_INFO *image_info,
1561 PALETTEENTRY *palette,
1562 IDirect3DCubeTexture9 **cube_texture)
1568 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1569 device, wine_dbgstr_w(src_filename), size, mip_levels, usage, format,
1570 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1572 hr = map_view_of_file(src_filename, &data, &data_size);
1573 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1575 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1576 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1578 UnmapViewOfFile(data);
1592 static float get_cube_coord(enum cube_coord coord, unsigned int x, unsigned int y, unsigned int size)
1599 return size - x - 0.5f;
1603 return size - y - 0.5f;
1609 ERR("Unexpected coordinate value\n");
1614 HRESULT WINAPI D3DXFillCubeTexture(struct IDirect3DCubeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1617 DWORD m, i, x, y, c, f, v;
1618 D3DSURFACE_DESC desc;
1619 D3DLOCKED_RECT lock_rect;
1621 D3DXVECTOR3 coord, size;
1622 const struct pixel_format_desc *format;
1626 static const enum cube_coord coordmap[6][3] =
1628 {ONE, YCOORDINV, XCOORDINV},
1629 {ZERO, YCOORDINV, XCOORD},
1630 {XCOORD, ONE, YCOORD},
1631 {XCOORD, ZERO, YCOORDINV},
1632 {XCOORD, YCOORDINV, ONE},
1633 {XCOORDINV, YCOORDINV, ZERO}
1636 if (texture == NULL || function == NULL)
1637 return D3DERR_INVALIDCALL;
1639 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1641 for (m = 0; m < miplevels; m++)
1643 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture, m, &desc)))
1644 return D3DERR_INVALIDCALL;
1646 format = get_format_info(desc.Format);
1647 if (format->type != FORMAT_ARGB)
1649 FIXME("Unsupported texture format %#x\n", desc.Format);
1650 return D3DERR_INVALIDCALL;
1653 for (f = 0; f < 6; f++)
1655 if (FAILED(IDirect3DCubeTexture9_LockRect(texture, f, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1656 return D3DERR_INVALIDCALL;
1658 size.x = (f == 0) || (f == 1) ? 0.0f : 2.0f / desc.Width;
1659 size.y = (f == 2) || (f == 3) ? 0.0f : 2.0f / desc.Width;
1660 size.z = (f == 4) || (f == 5) ? 0.0f : 2.0f / desc.Width;
1662 data = lock_rect.pBits;
1664 for (y = 0; y < desc.Height; y++)
1666 for (x = 0; x < desc.Width; x++)
1668 coord.x = get_cube_coord(coordmap[f][0], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1669 coord.y = get_cube_coord(coordmap[f][1], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1670 coord.z = get_cube_coord(coordmap[f][2], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1672 function(&value, &coord, &size, funcdata);
1674 pos = data + y * lock_rect.Pitch + x * format->bytes_per_pixel;
1676 for (i = 0; i < format->bytes_per_pixel; i++)
1679 for (c = 0; c < 4; c++)
1684 comp_value = value.w;
1687 comp_value = value.x;
1690 comp_value = value.y;
1693 comp_value = value.z;
1697 v = comp_value * ((1 << format->bits[c]) - 1) + 0.5f;
1699 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1701 mask = ((1 << format->bits[c]) - 1) << format->shift[c] >> i;
1702 byte = (v << format->shift[c] >> i) & mask;
1708 IDirect3DCubeTexture9_UnlockRect(texture, f, m);
1715 HRESULT WINAPI D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1718 DWORD m, i, x, y, z, c, v;
1719 D3DVOLUME_DESC desc;
1720 D3DLOCKED_BOX lock_box;
1722 D3DXVECTOR3 coord, size;
1723 const struct pixel_format_desc *format;
1728 if (texture == NULL || function == NULL)
1729 return D3DERR_INVALIDCALL;
1731 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1733 for (m = 0; m < miplevels; m++)
1735 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture, m, &desc)))
1736 return D3DERR_INVALIDCALL;
1738 format = get_format_info(desc.Format);
1739 if (format->type != FORMAT_ARGB)
1741 FIXME("Unsupported texture format %#x\n", desc.Format);
1742 return D3DERR_INVALIDCALL;
1745 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture, m, &lock_box, NULL, D3DLOCK_DISCARD)))
1746 return D3DERR_INVALIDCALL;
1748 size.x = 1.0f / desc.Width;
1749 size.y = 1.0f / desc.Height;
1750 size.z = 1.0f / desc.Depth;
1752 data = lock_box.pBits;
1754 for (z = 0; z < desc.Depth; z++)
1756 /* The callback function expects the coordinates of the center
1758 coord.z = (z + 0.5f) / desc.Depth;
1760 for (y = 0; y < desc.Height; y++)
1762 coord.y = (y + 0.5f) / desc.Height;
1764 for (x = 0; x < desc.Width; x++)
1766 coord.x = (x + 0.5f) / desc.Width;
1768 function(&value, &coord, &size, funcdata);
1770 pos = data + z * lock_box.SlicePitch + y * lock_box.RowPitch + x * format->bytes_per_pixel;
1772 for (i = 0; i < format->bytes_per_pixel; i++)
1775 for (c = 0; c < 4; c++)
1780 comp_value = value.w;
1783 comp_value = value.x;
1786 comp_value = value.y;
1789 comp_value = value.z;
1793 v = comp_value * ((1 << format->bits[c]) - 1) + 0.5f;
1795 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1797 mask = ((1 << format->bits[c]) - 1) << format->shift[c] >> i;
1798 byte = (v << format->shift[c] >> i) & mask;
1805 IDirect3DVolumeTexture9_UnlockBox(texture, m);
1811 HRESULT WINAPI D3DXSaveTextureToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1812 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1817 ID3DXBuffer *buffer;
1819 TRACE("(%s, %#x, %p, %p): relay\n",
1820 wine_dbgstr_a(dst_filename), file_format, src_texture, src_palette);
1822 if (!dst_filename) return D3DERR_INVALIDCALL;
1824 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1825 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1826 if (!filename) return E_OUTOFMEMORY;
1827 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1829 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1832 hr = write_buffer_to_file(filename, buffer);
1833 ID3DXBuffer_Release(buffer);
1836 HeapFree(GetProcessHeap(), 0, filename);
1840 HRESULT WINAPI D3DXSaveTextureToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1841 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1844 ID3DXBuffer *buffer;
1846 TRACE("(%s, %#x, %p, %p): relay\n",
1847 wine_dbgstr_w(dst_filename), file_format, src_texture, src_palette);
1849 if (!dst_filename) return D3DERR_INVALIDCALL;
1851 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1854 hr = write_buffer_to_file(dst_filename, buffer);
1855 ID3DXBuffer_Release(buffer);
1861 HRESULT WINAPI D3DXSaveTextureToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1862 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1865 D3DRESOURCETYPE type;
1866 IDirect3DSurface9 *surface;
1868 TRACE("(%p, %#x, %p, %p)\n",
1869 dst_buffer, file_format, src_texture, src_palette);
1871 if (!dst_buffer || !src_texture) return D3DERR_INVALIDCALL;
1873 if (file_format == D3DXIFF_DDS)
1875 FIXME("DDS file format isn't supported yet\n");
1879 type = IDirect3DBaseTexture9_GetType(src_texture);
1882 case D3DRTYPE_TEXTURE:
1883 case D3DRTYPE_CUBETEXTURE:
1884 hr = get_surface(type, src_texture, D3DCUBEMAP_FACE_POSITIVE_X, 0, &surface);
1886 case D3DRTYPE_VOLUMETEXTURE:
1887 FIXME("Volume textures aren't supported yet\n");
1890 return D3DERR_INVALIDCALL;
1895 hr = D3DXSaveSurfaceToFileInMemory(dst_buffer, file_format, surface, src_palette, NULL);
1896 IDirect3DSurface9_Release(surface);