dmime: Remove DECLSPEC_HIDDEN usage.
[wine.git] / dlls / d3dx9_36 / texture.c
blob2ee79b51f795a1f7d888c8a267aa3ee570ef1030
1 /*
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 <assert.h>
23 #include "d3dx9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
27 /* Returns TRUE if num is a power of 2, FALSE if not, or if 0 */
28 static BOOL is_pow2(UINT num)
30 return !(num & (num - 1));
33 static HRESULT get_surface(D3DRESOURCETYPE type, struct IDirect3DBaseTexture9 *tex,
34 int face, UINT level, struct IDirect3DSurface9 **surf)
36 switch (type)
38 case D3DRTYPE_TEXTURE:
39 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9*) tex, level, surf);
40 case D3DRTYPE_CUBETEXTURE:
41 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9*) tex, face, level, surf);
42 default:
43 ERR("Unexpected texture type\n");
44 return E_NOTIMPL;
48 HRESULT WINAPI D3DXFilterTexture(IDirect3DBaseTexture9 *texture,
49 const PALETTEENTRY *palette,
50 UINT srclevel,
51 DWORD filter)
53 UINT level;
54 HRESULT hr;
55 D3DRESOURCETYPE type;
57 TRACE("texture %p, palette %p, srclevel %u, filter %#lx.\n", texture, palette, srclevel, filter);
59 if (!texture)
60 return D3DERR_INVALIDCALL;
62 if ((filter & 0xFFFF) > D3DX_FILTER_BOX && filter != D3DX_DEFAULT)
63 return D3DERR_INVALIDCALL;
65 if (srclevel == D3DX_DEFAULT)
66 srclevel = 0;
67 else if (srclevel >= IDirect3DBaseTexture9_GetLevelCount(texture))
68 return D3DERR_INVALIDCALL;
70 switch (type = IDirect3DBaseTexture9_GetType(texture))
72 case D3DRTYPE_TEXTURE:
73 case D3DRTYPE_CUBETEXTURE:
75 IDirect3DSurface9 *topsurf, *mipsurf;
76 D3DSURFACE_DESC desc;
77 int i, numfaces;
79 if (type == D3DRTYPE_TEXTURE)
81 numfaces = 1;
82 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9*) texture, srclevel, &desc);
84 else
86 numfaces = 6;
87 IDirect3DCubeTexture9_GetLevelDesc((IDirect3DTexture9*) texture, srclevel, &desc);
90 if (filter == D3DX_DEFAULT)
92 if (is_pow2(desc.Width) && is_pow2(desc.Height))
93 filter = D3DX_FILTER_BOX;
94 else
95 filter = D3DX_FILTER_BOX | D3DX_FILTER_DITHER;
98 for (i = 0; i < numfaces; i++)
100 level = srclevel + 1;
101 hr = get_surface(type, texture, i, srclevel, &topsurf);
103 if (FAILED(hr))
104 return D3DERR_INVALIDCALL;
106 while (get_surface(type, texture, i, level, &mipsurf) == D3D_OK)
108 hr = D3DXLoadSurfaceFromSurface(mipsurf, palette, NULL, topsurf, palette, NULL, filter, 0);
109 IDirect3DSurface9_Release(topsurf);
110 topsurf = mipsurf;
112 if (FAILED(hr))
113 break;
115 level++;
118 IDirect3DSurface9_Release(topsurf);
119 if (FAILED(hr))
120 return hr;
123 return D3D_OK;
126 case D3DRTYPE_VOLUMETEXTURE:
128 D3DVOLUME_DESC desc;
129 int level, level_count;
130 IDirect3DVolume9 *top_volume, *mip_volume;
131 IDirect3DVolumeTexture9 *volume_texture = (IDirect3DVolumeTexture9*) texture;
133 IDirect3DVolumeTexture9_GetLevelDesc(volume_texture, srclevel, &desc);
135 if (filter == D3DX_DEFAULT)
137 if (is_pow2(desc.Width) && is_pow2(desc.Height) && is_pow2(desc.Depth))
138 filter = D3DX_FILTER_BOX;
139 else
140 filter = D3DX_FILTER_BOX | D3DX_FILTER_DITHER;
143 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, srclevel, &top_volume);
144 if (FAILED(hr))
145 return hr;
147 level_count = IDirect3DVolumeTexture9_GetLevelCount(volume_texture);
148 for (level = srclevel + 1; level < level_count; level++)
150 IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, level, &mip_volume);
151 hr = D3DXLoadVolumeFromVolume(mip_volume, palette, NULL, top_volume, palette, NULL, filter, 0);
152 IDirect3DVolume9_Release(top_volume);
153 top_volume = mip_volume;
155 if (FAILED(hr))
156 break;
159 IDirect3DVolume9_Release(top_volume);
160 if (FAILED(hr))
161 return hr;
163 return D3D_OK;
166 default:
167 return D3DERR_INVALIDCALL;
171 static D3DFORMAT get_replacement_format(D3DFORMAT format)
173 static const struct
175 D3DFORMAT format;
176 D3DFORMAT replacement_format;
178 replacements[] =
180 {D3DFMT_L8, D3DFMT_X8R8G8B8},
181 {D3DFMT_A8L8, D3DFMT_A8R8G8B8},
182 {D3DFMT_A4L4, D3DFMT_A4R4G4B4},
183 {D3DFMT_L16, D3DFMT_A16B16G16R16},
184 {D3DFMT_DXT1, D3DFMT_A8R8G8B8},
185 {D3DFMT_DXT2, D3DFMT_A8R8G8B8},
186 {D3DFMT_DXT3, D3DFMT_A8R8G8B8},
187 {D3DFMT_DXT4, D3DFMT_A8R8G8B8},
188 {D3DFMT_DXT5, D3DFMT_A8R8G8B8},
190 unsigned int i;
192 for (i = 0; i < ARRAY_SIZE(replacements); ++i)
193 if (format == replacements[i].format)
194 return replacements[i].replacement_format;
195 return format;
198 static HRESULT check_texture_requirements(struct IDirect3DDevice9 *device, UINT *width, UINT *height,
199 UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool, D3DRESOURCETYPE resource_type)
201 UINT w = (width && *width) ? *width : 1;
202 UINT h = (height && *height) ? *height : 1;
203 D3DCAPS9 caps;
204 D3DDEVICE_CREATION_PARAMETERS params;
205 IDirect3D9 *d3d = NULL;
206 D3DDISPLAYMODE mode;
207 HRESULT hr;
208 D3DFORMAT usedformat = D3DFMT_UNKNOWN;
209 const struct pixel_format_desc *fmt;
211 if (!device)
212 return D3DERR_INVALIDCALL;
214 /* usage */
215 if (usage == D3DX_DEFAULT)
216 usage = 0;
217 if (usage & (D3DUSAGE_WRITEONLY | D3DUSAGE_DONOTCLIP | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES | D3DUSAGE_NPATCHES))
218 return D3DERR_INVALIDCALL;
220 /* pool */
221 if ((pool != D3DPOOL_DEFAULT) && (pool != D3DPOOL_MANAGED) && (pool != D3DPOOL_SYSTEMMEM) && (pool != D3DPOOL_SCRATCH))
222 return D3DERR_INVALIDCALL;
224 /* format */
225 if (format)
227 TRACE("Requested format %x\n", *format);
228 usedformat = *format;
231 hr = IDirect3DDevice9_GetDirect3D(device, &d3d);
233 if (FAILED(hr))
234 goto cleanup;
236 hr = IDirect3DDevice9_GetCreationParameters(device, &params);
238 if (FAILED(hr))
239 goto cleanup;
241 hr = IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
243 if (FAILED(hr))
244 goto cleanup;
246 if ((usedformat == D3DFMT_UNKNOWN) || (usedformat == D3DX_DEFAULT))
247 usedformat = D3DFMT_A8R8G8B8;
249 fmt = get_format_info(usedformat);
251 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType, mode.Format,
252 usage, resource_type, usedformat);
253 if (FAILED(hr))
255 BOOL allow_24bits;
256 int bestscore = INT_MIN, i = 0, j;
257 unsigned int channels;
258 const struct pixel_format_desc *curfmt, *bestfmt = NULL;
260 TRACE("Requested format is not supported, looking for a fallback.\n");
262 if (!fmt)
264 FIXME("Pixel format %x not handled\n", usedformat);
265 goto cleanup;
267 fmt = get_format_info(get_replacement_format(usedformat));
269 allow_24bits = fmt->bytes_per_pixel == 3;
270 channels = !!fmt->bits[0] + !!fmt->bits[1] + !!fmt->bits[2] + !!fmt->bits[3];
271 usedformat = D3DFMT_UNKNOWN;
273 while ((curfmt = get_format_info_idx(i)))
275 unsigned int curchannels = !!curfmt->bits[0] + !!curfmt->bits[1]
276 + !!curfmt->bits[2] + !!curfmt->bits[3];
277 int score;
279 i++;
281 if (curchannels < channels)
282 continue;
283 if (curfmt->bytes_per_pixel == 3 && !allow_24bits)
284 continue;
286 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
287 mode.Format, usage, resource_type, curfmt->format);
288 if (FAILED(hr))
289 continue;
291 /* This format can be used, let's evaluate it.
292 Weights chosen quite arbitrarily... */
293 score = 512 * (curfmt->type == fmt->type);
294 score -= 32 * (curchannels - channels);
296 for (j = 0; j < 4; j++)
298 int diff = curfmt->bits[j] - fmt->bits[j];
299 score -= (diff < 0 ? -diff * 8 : diff) * (j == 0 ? 1 : 2);
302 if (score > bestscore)
304 bestscore = score;
305 usedformat = curfmt->format;
306 bestfmt = curfmt;
309 if (!bestfmt)
311 hr = D3DERR_NOTAVAILABLE;
312 goto cleanup;
314 fmt = bestfmt;
315 hr = D3D_OK;
318 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
319 return D3DERR_INVALIDCALL;
321 if ((w == D3DX_DEFAULT) && (h == D3DX_DEFAULT))
322 w = h = 256;
323 else if (w == D3DX_DEFAULT)
324 w = (height ? h : 256);
325 else if (h == D3DX_DEFAULT)
326 h = (width ? w : 256);
328 assert(!(fmt->block_width & (fmt->block_width - 1)));
329 assert(!(fmt->block_height & (fmt->block_height - 1)));
330 if (w & (fmt->block_width - 1))
331 w = (w + fmt->block_width) & ~(fmt->block_width - 1);
332 if (h & (fmt->block_height - 1))
333 h = (h + fmt->block_height) & ~(fmt->block_height - 1);
335 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(w)))
336 w = make_pow2(w);
338 if (w > caps.MaxTextureWidth)
339 w = caps.MaxTextureWidth;
341 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(h)))
342 h = make_pow2(h);
344 if (h > caps.MaxTextureHeight)
345 h = caps.MaxTextureHeight;
347 if (caps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
349 if (w > h)
350 h = w;
351 else
352 w = h;
355 if (width)
356 *width = w;
358 if (height)
359 *height = h;
361 if (miplevels && (usage & D3DUSAGE_AUTOGENMIPMAP))
363 if (*miplevels > 1)
364 *miplevels = 0;
366 else if (miplevels)
368 UINT max_mipmaps = 1;
370 if (!width && !height)
371 max_mipmaps = 9; /* number of mipmaps in a 256x256 texture */
372 else
374 UINT max_dimen = max(w, h);
376 while (max_dimen > 1)
378 max_dimen >>= 1;
379 max_mipmaps++;
383 if (*miplevels == 0 || *miplevels > max_mipmaps)
384 *miplevels = max_mipmaps;
387 cleanup:
389 if (d3d)
390 IDirect3D9_Release(d3d);
392 if (FAILED(hr))
393 return hr;
395 if (usedformat == D3DFMT_UNKNOWN)
397 WARN("Couldn't find a suitable pixel format\n");
398 return D3DERR_NOTAVAILABLE;
401 TRACE("Format chosen: %x\n", usedformat);
402 if (format)
403 *format = usedformat;
405 return D3D_OK;
408 HRESULT WINAPI D3DXCheckTextureRequirements(struct IDirect3DDevice9 *device, UINT *width, UINT *height,
409 UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
411 TRACE("device %p, width %p, height %p, miplevels %p, usage %#lx, format %p, pool %#x.\n",
412 device, width, height, miplevels, usage, format, pool);
414 return check_texture_requirements(device, width, height, miplevels, usage, format, pool, D3DRTYPE_TEXTURE);
417 HRESULT WINAPI D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9 *device, UINT *size,
418 UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
420 D3DCAPS9 caps;
421 UINT s = (size && *size) ? *size : 256;
422 HRESULT hr;
424 TRACE("device %p, size %p, miplevels %p, usage %#lx, format %p, pool %#x.\n",
425 device, size, miplevels, usage, format, pool);
427 if (s == D3DX_DEFAULT)
428 s = 256;
430 if (!device || FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
431 return D3DERR_INVALIDCALL;
433 if (!(caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP))
434 return D3DERR_NOTAVAILABLE;
436 if ((caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && (!is_pow2(s)))
437 s = make_pow2(s);
439 hr = check_texture_requirements(device, &s, &s, miplevels, usage, format, pool, D3DRTYPE_CUBETEXTURE);
441 if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
443 if(miplevels)
444 *miplevels = 1;
447 if (size)
448 *size = s;
450 return hr;
453 HRESULT WINAPI D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9 *device, UINT *width, UINT *height,
454 UINT *depth, UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
456 D3DCAPS9 caps;
457 UINT w = width ? *width : D3DX_DEFAULT;
458 UINT h = height ? *height : D3DX_DEFAULT;
459 UINT d = (depth && *depth) ? *depth : 1;
460 HRESULT hr;
462 TRACE("device %p, width %p, height %p, depth %p, miplevels %p, usage %#lx, format %p, pool %#x.\n",
463 device, width, height, depth, miplevels, usage, format, pool);
465 if (!device || FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
466 return D3DERR_INVALIDCALL;
468 if (!(caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP))
469 return D3DERR_NOTAVAILABLE;
471 hr = check_texture_requirements(device, &w, &h, NULL, usage, format, pool, D3DRTYPE_VOLUMETEXTURE);
472 if (d == D3DX_DEFAULT)
473 d = 1;
475 if ((caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2) &&
476 (!is_pow2(w) || !is_pow2(h) || !is_pow2(d)))
478 w = make_pow2(w);
479 h = make_pow2(h);
480 d = make_pow2(d);
483 if (w > caps.MaxVolumeExtent)
484 w = caps.MaxVolumeExtent;
485 if (h > caps.MaxVolumeExtent)
486 h = caps.MaxVolumeExtent;
487 if (d > caps.MaxVolumeExtent)
488 d = caps.MaxVolumeExtent;
490 if (miplevels)
492 if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
493 *miplevels = 1;
494 else if ((usage & D3DUSAGE_AUTOGENMIPMAP))
496 if (*miplevels > 1)
497 *miplevels = 0;
499 else
501 UINT max_mipmaps = 1;
502 UINT max_dimen = max(max(w, h), d);
504 while (max_dimen > 1)
506 max_dimen >>= 1;
507 max_mipmaps++;
510 if (*miplevels == 0 || *miplevels > max_mipmaps)
511 *miplevels = max_mipmaps;
515 if (width)
516 *width = w;
517 if (height)
518 *height = h;
519 if (depth)
520 *depth = d;
522 return hr;
525 HRESULT WINAPI D3DXCreateTexture(struct IDirect3DDevice9 *device, UINT width, UINT height,
526 UINT miplevels, DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DTexture9 **texture)
528 HRESULT hr;
530 TRACE("device %p, width %u, height %u, miplevels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
531 device, width, height, miplevels, usage, format, pool, texture);
533 if (!device || !texture)
534 return D3DERR_INVALIDCALL;
536 if (FAILED(hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool)))
537 return hr;
539 return IDirect3DDevice9_CreateTexture(device, width, height, miplevels, usage, format, pool, texture, NULL);
542 static D3DFORMAT get_alpha_replacement_format(D3DFORMAT format)
544 static const struct
546 D3DFORMAT orig_format;
547 D3DFORMAT replacement_format;
549 replacement_formats[] =
551 {D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8},
552 {D3DFMT_X1R5G5B5, D3DFMT_A1R5G5B5},
553 {D3DFMT_X4R4G4B4, D3DFMT_A4R4G4B4},
554 {D3DFMT_X8B8G8R8, D3DFMT_A8B8G8R8},
555 {D3DFMT_L8, D3DFMT_A8L8},
557 unsigned int i;
559 for (i = 0; i < ARRAY_SIZE(replacement_formats); ++i)
560 if (replacement_formats[i].orig_format == format)
561 return replacement_formats[i].replacement_format;
562 return format;
565 HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9 *device, const void *srcdata,
566 UINT srcdatasize, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
567 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
568 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
570 BOOL dynamic_texture, format_specified = FALSE;
571 unsigned int loaded_miplevels, skip_levels;
572 IDirect3DSurface9 *surface;
573 IDirect3DTexture9 **texptr;
574 IDirect3DTexture9 *buftex;
575 D3DXIMAGE_INFO imginfo;
576 D3DCAPS9 caps;
577 HRESULT hr;
579 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u, "
580 "usage %#lx, format %#x, pool %#x, filter %#lx, mipfilter %#lx, colorkey %#lx, "
581 "srcinfo %p, palette %p, texture %p.\n",
582 device, srcdata, srcdatasize, width, height, miplevels, usage, format, pool,
583 filter, mipfilter, colorkey, srcinfo, palette, texture);
585 /* check for invalid parameters */
586 if (!device || !texture || !srcdata || !srcdatasize)
587 return D3DERR_INVALIDCALL;
589 hr = D3DXGetImageInfoFromFileInMemory(srcdata, srcdatasize, &imginfo);
590 if (FAILED(hr))
592 FIXME("Unrecognized file format, returning failure.\n");
593 *texture = NULL;
594 return hr;
597 /* handle default values */
598 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
599 width = imginfo.Width;
601 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
602 height = imginfo.Height;
604 if (width == D3DX_DEFAULT)
605 width = make_pow2(imginfo.Width);
607 if (height == D3DX_DEFAULT)
608 height = make_pow2(imginfo.Height);
610 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
611 format = imginfo.Format;
612 else
613 format_specified = TRUE;
615 if (width == D3DX_FROM_FILE)
617 width = imginfo.Width;
620 if (height == D3DX_FROM_FILE)
622 height = imginfo.Height;
625 if (format == D3DFMT_FROM_FILE)
627 format = imginfo.Format;
630 if (miplevels == D3DX_FROM_FILE)
632 miplevels = imginfo.MipLevels;
635 skip_levels = mipfilter != D3DX_DEFAULT ? mipfilter >> D3DX_SKIP_DDS_MIP_LEVELS_SHIFT : 0;
636 if (skip_levels && imginfo.MipLevels > skip_levels)
638 TRACE("Skipping the first %u (of %u) levels of a DDS mipmapped texture.\n",
639 skip_levels, imginfo.MipLevels);
640 TRACE("Texture level 0 dimensions are %ux%u.\n", imginfo.Width, imginfo.Height);
641 width >>= skip_levels;
642 height >>= skip_levels;
643 miplevels -= skip_levels;
645 else
647 skip_levels = 0;
650 /* Fix up texture creation parameters. */
651 hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool);
652 if (FAILED(hr))
654 FIXME("Couldn't find suitable texture parameters.\n");
655 *texture = NULL;
656 return hr;
659 if (colorkey && !format_specified)
660 format = get_alpha_replacement_format(format);
662 if (imginfo.ResourceType == D3DRTYPE_VOLUMETEXTURE
663 && D3DFMT_DXT1 <= imginfo.Format && imginfo.Format <= D3DFMT_DXT5 && miplevels > 1)
665 FIXME("Generation of mipmaps for compressed volume textures is not implemented yet.\n");
666 miplevels = 1;
669 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
670 return D3DERR_INVALIDCALL;
672 /* Create the to-be-filled texture */
673 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
674 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
676 hr = D3DXCreateTexture(device, width, height, miplevels, 0, format, D3DPOOL_SYSTEMMEM, &buftex);
677 texptr = &buftex;
679 else
681 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
682 texptr = texture;
685 if (FAILED(hr))
687 FIXME("Texture creation failed.\n");
688 *texture = NULL;
689 return hr;
692 TRACE("Texture created correctly. Now loading the texture data into it.\n");
693 if (imginfo.ImageFileFormat != D3DXIFF_DDS)
695 IDirect3DTexture9_GetSurfaceLevel(*texptr, 0, &surface);
696 hr = D3DXLoadSurfaceFromFileInMemory(surface, palette, NULL, srcdata, srcdatasize, NULL, filter, colorkey, NULL);
697 IDirect3DSurface9_Release(surface);
698 loaded_miplevels = min(IDirect3DTexture9_GetLevelCount(*texptr), imginfo.MipLevels);
700 else
702 hr = load_texture_from_dds(*texptr, srcdata, palette, filter, colorkey, &imginfo, skip_levels,
703 &loaded_miplevels);
706 if (FAILED(hr))
708 FIXME("Texture loading failed.\n");
709 IDirect3DTexture9_Release(*texptr);
710 *texture = NULL;
711 return hr;
714 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) *texptr, palette, loaded_miplevels - 1, mipfilter);
715 if (FAILED(hr))
717 FIXME("Texture filtering failed.\n");
718 IDirect3DTexture9_Release(*texptr);
719 *texture = NULL;
720 return hr;
723 /* Move the data to the actual texture if necessary */
724 if (texptr == &buftex)
726 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
728 if (FAILED(hr))
730 IDirect3DTexture9_Release(buftex);
731 *texture = NULL;
732 return hr;
735 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9*)buftex, (IDirect3DBaseTexture9*)(*texture));
736 IDirect3DTexture9_Release(buftex);
739 if (srcinfo)
740 *srcinfo = imginfo;
742 return D3D_OK;
745 HRESULT WINAPI D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9 *device,
746 const void *srcdata, UINT srcdatasize, struct IDirect3DTexture9 **texture)
748 TRACE("device %p, srcdata %p, srcdatasize %u, texture %p.\n", device, srcdata, srcdatasize, texture);
750 return D3DXCreateTextureFromFileInMemoryEx(device, srcdata, srcdatasize, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
751 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
754 HRESULT WINAPI D3DXCreateTextureFromFileExW(struct IDirect3DDevice9 *device, const WCHAR *srcfile,
755 UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
756 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
757 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
759 void *buffer;
760 HRESULT hr;
761 DWORD size;
763 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#lx, format %#x, "
764 "pool %#x, filter %#lx, mipfilter %#lx, colorkey 0x%08lx, srcinfo %p, palette %p, texture %p.\n",
765 device, debugstr_w(srcfile), width, height, miplevels, usage, format,
766 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
768 if (!srcfile)
769 return D3DERR_INVALIDCALL;
771 hr = map_view_of_file(srcfile, &buffer, &size);
772 if (FAILED(hr))
774 WARN("Failed to open file.\n");
775 return D3DXERR_INVALIDDATA;
778 hr = D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels, usage, format, pool,
779 filter, mipfilter, colorkey, srcinfo, palette, texture);
781 UnmapViewOfFile(buffer);
783 return hr;
786 HRESULT WINAPI D3DXCreateTextureFromFileExA(struct IDirect3DDevice9 *device, const char *srcfile,
787 UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
788 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
789 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
791 WCHAR *widename;
792 HRESULT hr;
793 DWORD len;
795 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#lx, format %#x, "
796 "pool %#x, filter %#lx, mipfilter %#lx, colorkey 0x%08lx, srcinfo %p, palette %p, texture %p.\n",
797 device, debugstr_a(srcfile), width, height, miplevels, usage, format,
798 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
800 if (!device || !srcfile || !texture)
801 return D3DERR_INVALIDCALL;
803 len = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
804 widename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*widename));
805 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, widename, len);
807 hr = D3DXCreateTextureFromFileExW(device, widename, width, height, miplevels,
808 usage, format, pool, filter, mipfilter,
809 colorkey, srcinfo, palette, texture);
811 HeapFree(GetProcessHeap(), 0, widename);
812 return hr;
815 HRESULT WINAPI D3DXCreateTextureFromFileA(struct IDirect3DDevice9 *device,
816 const char *srcfile, struct IDirect3DTexture9 **texture)
818 TRACE("device %p, srcfile %s, texture %p.\n", device, debugstr_a(srcfile), texture);
820 return D3DXCreateTextureFromFileExA(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
821 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
824 HRESULT WINAPI D3DXCreateTextureFromFileW(struct IDirect3DDevice9 *device,
825 const WCHAR *srcfile, struct IDirect3DTexture9 **texture)
827 TRACE("device %p, srcfile %s, texture %p.\n", device, debugstr_w(srcfile), texture);
829 return D3DXCreateTextureFromFileExW(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
830 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
834 HRESULT WINAPI D3DXCreateTextureFromResourceA(struct IDirect3DDevice9 *device,
835 HMODULE srcmodule, const char *resource, struct IDirect3DTexture9 **texture)
837 TRACE("device %p, srcmodule %p, resource %s, texture %p.\n",
838 device, srcmodule, debugstr_a(resource), texture);
840 return D3DXCreateTextureFromResourceExA(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT,
841 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL,
842 NULL, texture);
845 HRESULT WINAPI D3DXCreateTextureFromResourceW(struct IDirect3DDevice9 *device,
846 HMODULE srcmodule, const WCHAR *resource, struct IDirect3DTexture9 **texture)
848 TRACE("device %p, srcmodule %p, resource %s, texture %p.\n",
849 device, srcmodule, debugstr_w(resource), texture);
851 return D3DXCreateTextureFromResourceExW(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT,
852 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL,
853 NULL, texture);
856 HRESULT WINAPI D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9 *device, HMODULE srcmodule,
857 const char *resource, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
858 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
859 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
861 HRSRC resinfo;
862 void *buffer;
863 DWORD size;
865 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#lx, format %#x, "
866 "pool %#x, filter %#lx, mipfilter %#lx, colorkey 0x%08lx, srcinfo %p, palette %p, texture %p.\n",
867 device, srcmodule, debugstr_a(resource), width, height, miplevels, usage, format,
868 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
870 if (!device || !texture)
871 return D3DERR_INVALIDCALL;
873 if (!(resinfo = FindResourceA(srcmodule, resource, (const char *)RT_RCDATA))
874 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
875 && !(resinfo = FindResourceA(srcmodule, resource, (const char *)RT_BITMAP)))
876 return D3DXERR_INVALIDDATA;
878 if (FAILED(load_resource_into_memory(srcmodule, resinfo, &buffer, &size)))
879 return D3DXERR_INVALIDDATA;
881 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels,
882 usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
885 HRESULT WINAPI D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9 *device, HMODULE srcmodule,
886 const WCHAR *resource, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
887 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
888 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
890 HRSRC resinfo;
891 void *buffer;
892 DWORD size;
894 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#lx, format %#x, "
895 "pool %#x, filter %#lx, mipfilter %#lx, colorkey 0x%08lx, srcinfo %p, palette %p, texture %p.\n",
896 device, srcmodule, debugstr_w(resource), width, height, miplevels, usage, format,
897 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
899 if (!device || !texture)
900 return D3DERR_INVALIDCALL;
902 if (!(resinfo = FindResourceW(srcmodule, resource, (const WCHAR *)RT_RCDATA))
903 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
904 && !(resinfo = FindResourceW(srcmodule, resource, (const WCHAR *)RT_BITMAP)))
905 return D3DXERR_INVALIDDATA;
907 if (FAILED(load_resource_into_memory(srcmodule, resinfo, &buffer, &size)))
908 return D3DXERR_INVALIDDATA;
910 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels,
911 usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
914 HRESULT WINAPI D3DXCreateCubeTexture(struct IDirect3DDevice9 *device, UINT size, UINT miplevels,
915 DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DCubeTexture9 **texture)
917 HRESULT hr;
919 TRACE("device %p, size %u, miplevels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
920 device, size, miplevels, usage, format, pool, texture);
922 if (!device || !texture)
923 return D3DERR_INVALIDCALL;
925 hr = D3DXCheckCubeTextureRequirements(device, &size, &miplevels, usage, &format, pool);
927 if (FAILED(hr))
929 TRACE("D3DXCheckCubeTextureRequirements failed\n");
930 return hr;
933 return IDirect3DDevice9_CreateCubeTexture(device, size, miplevels, usage, format, pool, texture, NULL);
936 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9 *device,
937 const void *data, UINT datasize, struct IDirect3DCubeTexture9 **texture)
939 TRACE("device %p, data %p, datasize %u, texture %p.\n", device, data, datasize, texture);
941 return D3DXCreateCubeTextureFromFileInMemoryEx(device, data, datasize, D3DX_DEFAULT, D3DX_DEFAULT,
942 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
945 HRESULT WINAPI D3DXCreateVolumeTexture(struct IDirect3DDevice9 *device, UINT width, UINT height, UINT depth,
946 UINT miplevels, DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DVolumeTexture9 **texture)
948 HRESULT hr;
950 TRACE("device %p, width %u, height %u, depth %u, miplevels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
951 device, width, height, depth, miplevels, usage, format, pool, texture);
953 if (!device || !texture)
954 return D3DERR_INVALIDCALL;
956 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth,
957 &miplevels, usage, &format, pool);
959 if (FAILED(hr))
961 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
962 return hr;
965 return IDirect3DDevice9_CreateVolumeTexture(device, width, height, depth, miplevels,
966 usage, format, pool, texture, NULL);
969 HRESULT WINAPI D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9 *device,
970 const char *filename,
971 IDirect3DVolumeTexture9 **volume_texture)
973 int len;
974 HRESULT hr;
975 void *data;
976 DWORD data_size;
977 WCHAR *filenameW;
979 TRACE("device %p, filename %s, volume_texture %p.\n", device, debugstr_a(filename), volume_texture);
981 if (!filename) return D3DERR_INVALIDCALL;
983 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
984 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
985 if (!filenameW) return E_OUTOFMEMORY;
986 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
988 hr = map_view_of_file(filenameW, &data, &data_size);
989 HeapFree(GetProcessHeap(), 0, filenameW);
990 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
992 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
993 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
995 UnmapViewOfFile(data);
996 return hr;
999 HRESULT WINAPI D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9 *device,
1000 const WCHAR *filename,
1001 IDirect3DVolumeTexture9 **volume_texture)
1003 HRESULT hr;
1004 void *data;
1005 DWORD data_size;
1007 TRACE("device %p, filename %s, volume_texture %p.\n", device, debugstr_w(filename), volume_texture);
1009 if (!filename) return D3DERR_INVALIDCALL;
1011 hr = map_view_of_file(filename, &data, &data_size);
1012 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1014 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
1015 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
1017 UnmapViewOfFile(data);
1018 return hr;
1021 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9 *device,
1022 const char *filename,
1023 UINT width,
1024 UINT height,
1025 UINT depth,
1026 UINT mip_levels,
1027 DWORD usage,
1028 D3DFORMAT format,
1029 D3DPOOL pool,
1030 DWORD filter,
1031 DWORD mip_filter,
1032 D3DCOLOR color_key,
1033 D3DXIMAGE_INFO *src_info,
1034 PALETTEENTRY *palette,
1035 IDirect3DVolumeTexture9 **volume_texture)
1037 int len;
1038 HRESULT hr;
1039 WCHAR *filenameW;
1040 void *data;
1041 DWORD data_size;
1043 TRACE("device %p, filename %s, width %u, height %u, depth %u, mip_levels %u, usage %#lx, "
1044 "format %#x, pool %#x, filter %#lx, mip_filter %#lx, color_key 0x%08lx, src_info %p, "
1045 "palette %p, volume_texture %p.\n",
1046 device, debugstr_a(filename), width, height, depth, mip_levels, usage,
1047 format, pool, filter, mip_filter, color_key, src_info, palette, volume_texture);
1049 if (!filename) return D3DERR_INVALIDCALL;
1051 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
1052 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1053 if (!filenameW) return E_OUTOFMEMORY;
1054 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
1056 hr = map_view_of_file(filenameW, &data, &data_size);
1057 HeapFree(GetProcessHeap(), 0, filenameW);
1058 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1060 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1061 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1062 volume_texture);
1064 UnmapViewOfFile(data);
1065 return hr;
1068 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9 *device,
1069 const WCHAR *filename,
1070 UINT width,
1071 UINT height,
1072 UINT depth,
1073 UINT mip_levels,
1074 DWORD usage,
1075 D3DFORMAT format,
1076 D3DPOOL pool,
1077 DWORD filter,
1078 DWORD mip_filter,
1079 D3DCOLOR color_key,
1080 D3DXIMAGE_INFO *src_info,
1081 PALETTEENTRY *palette,
1082 IDirect3DVolumeTexture9 **volume_texture)
1084 HRESULT hr;
1085 void *data;
1086 DWORD data_size;
1088 TRACE("device %p, filename %s, width %u, height %u, depth %u, mip_levels %u, usage %#lx, "
1089 "format %#x, pool %#x, filter %#lx, mip_filter %#lx, color_key 0x%08lx, src_info %p, "
1090 "palette %p, volume_texture %p.\n",
1091 device, debugstr_w(filename), width, height, depth, mip_levels, usage,
1092 format, pool, filter, mip_filter, color_key, src_info, palette, volume_texture);
1094 if (!filename) return D3DERR_INVALIDCALL;
1096 hr = map_view_of_file(filename, &data, &data_size);
1097 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1099 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1100 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1101 volume_texture);
1103 UnmapViewOfFile(data);
1104 return hr;
1107 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9 *device,
1108 const void *data,
1109 UINT data_size,
1110 IDirect3DVolumeTexture9 **volume_texture)
1112 TRACE("device %p, data %p, data_size %u, volume_texture %p.\n", device, data, data_size, volume_texture);
1114 return D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1115 D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
1116 0, NULL, NULL, volume_texture);
1119 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9 *device, const void *data,
1120 UINT data_size, UINT width, UINT height, UINT depth, UINT mip_levels, DWORD usage,
1121 D3DFORMAT format, D3DPOOL pool, DWORD filter, DWORD mip_filter, D3DCOLOR color_key,
1122 D3DXIMAGE_INFO *info, PALETTEENTRY *palette, IDirect3DVolumeTexture9 **volume_texture)
1124 HRESULT hr;
1125 D3DCAPS9 caps;
1126 D3DXIMAGE_INFO image_info;
1127 BOOL dynamic_texture;
1128 BOOL file_width = FALSE;
1129 BOOL file_height = FALSE;
1130 BOOL file_depth = FALSE;
1131 BOOL file_format = FALSE;
1132 BOOL file_mip_levels = FALSE;
1133 IDirect3DVolumeTexture9 *tex, *buftex;
1135 TRACE("device %p, data %p, data_size %u, width %u, height %u, depth %u, mip_levels %u, "
1136 "usage %#lx, format %#x, pool %#x, filter %#lx, mip_filter %#lx, color_key 0x%08lx, "
1137 "info %p, palette %p, volume_texture %p.\n",
1138 device, data, data_size, width, height, depth, mip_levels, usage, format, pool,
1139 filter, mip_filter, color_key, info, palette, volume_texture);
1141 if (!device || !data || !data_size || !volume_texture)
1142 return D3DERR_INVALIDCALL;
1144 hr = D3DXGetImageInfoFromFileInMemory(data, data_size, &image_info);
1145 if (FAILED(hr)) return hr;
1147 if (image_info.ImageFileFormat != D3DXIFF_DDS)
1148 return D3DXERR_INVALIDDATA;
1150 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
1151 width = image_info.Width;
1152 if (width == D3DX_DEFAULT)
1153 width = make_pow2(image_info.Width);
1155 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
1156 height = image_info.Height;
1157 if (height == D3DX_DEFAULT)
1158 height = make_pow2(image_info.Height);
1160 if (depth == 0 || depth == D3DX_DEFAULT_NONPOW2)
1161 depth = image_info.Depth;
1162 if (depth == D3DX_DEFAULT)
1163 depth = make_pow2(image_info.Depth);
1165 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1166 format = image_info.Format;
1168 if (width == D3DX_FROM_FILE)
1170 file_width = TRUE;
1171 width = image_info.Width;
1174 if (height == D3DX_FROM_FILE)
1176 file_height = TRUE;
1177 height = image_info.Height;
1180 if (depth == D3DX_FROM_FILE)
1182 file_depth = TRUE;
1183 depth = image_info.Depth;
1186 if (format == D3DFMT_FROM_FILE)
1188 file_format = TRUE;
1189 format = image_info.Format;
1192 if (mip_levels == D3DX_FROM_FILE)
1194 file_mip_levels = TRUE;
1195 mip_levels = image_info.MipLevels;
1198 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, &mip_levels, usage, &format, pool);
1199 if (FAILED(hr)) return hr;
1201 if ((file_width && width != image_info.Width)
1202 || (file_height && height != image_info.Height)
1203 || (file_depth && depth != image_info.Depth)
1204 || (file_format && format != image_info.Format)
1205 || (file_mip_levels && mip_levels != image_info.MipLevels))
1206 return D3DERR_NOTAVAILABLE;
1208 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1209 if (FAILED(hr))
1210 return D3DERR_INVALIDCALL;
1212 if (mip_levels > image_info.MipLevels)
1214 FIXME("Generation of mipmaps for volume textures is not implemented yet.\n");
1215 mip_levels = image_info.MipLevels;
1218 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1219 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1221 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, 0, format, D3DPOOL_SYSTEMMEM, &buftex);
1222 tex = buftex;
1224 else
1226 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1227 buftex = NULL;
1229 if (FAILED(hr)) return hr;
1231 hr = load_volume_texture_from_dds(tex, data, palette, filter, color_key, &image_info);
1232 if (FAILED(hr))
1234 IDirect3DVolumeTexture9_Release(tex);
1235 return hr;
1238 if (buftex)
1240 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1241 if (FAILED(hr))
1243 IDirect3DVolumeTexture9_Release(buftex);
1244 return hr;
1247 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1248 IDirect3DVolumeTexture9_Release(buftex);
1251 if (info)
1252 *info = image_info;
1254 *volume_texture = tex;
1255 return D3D_OK;
1258 static inline void fill_texture(const struct pixel_format_desc *format, BYTE *pos, const D3DXVECTOR4 *value)
1260 DWORD c;
1262 for (c = 0; c < format->bytes_per_pixel; c++)
1263 pos[c] = 0;
1265 for (c = 0; c < 4; c++)
1267 float comp_value;
1268 DWORD i, v = 0, mask32 = format->bits[c] == 32 ? ~0U : ((1 << format->bits[c]) - 1);
1270 switch (c)
1272 case 0: /* Alpha */
1273 comp_value = value->w;
1274 break;
1275 case 1: /* Red */
1276 comp_value = value->x;
1277 break;
1278 case 2: /* Green */
1279 comp_value = value->y;
1280 break;
1281 case 3: /* Blue */
1282 comp_value = value->z;
1283 break;
1286 if (format->type == FORMAT_ARGBF16)
1287 v = float_32_to_16(comp_value);
1288 else if (format->type == FORMAT_ARGBF)
1289 v = *(DWORD *)&comp_value;
1290 else if (format->type == FORMAT_ARGB)
1291 v = max(comp_value * ((1 << format->bits[c]) - 1) + 0.5f, 0);
1292 else
1293 FIXME("Unhandled format type %#x\n", format->type);
1295 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1297 BYTE byte, mask;
1299 if (format->shift[c] > i)
1301 mask = mask32 << (format->shift[c] - i);
1302 byte = (v << (format->shift[c] - i)) & mask;
1304 else
1306 mask = mask32 >> (i - format->shift[c]);
1307 byte = (v >> (i - format->shift[c])) & mask;
1309 pos[i / 8] |= byte;
1314 HRESULT WINAPI D3DXFillTexture(struct IDirect3DTexture9 *texture, LPD3DXFILL2D function, void *funcdata)
1316 IDirect3DSurface9 *surface, *temp_surface;
1317 DWORD miplevels;
1318 DWORD m, x, y;
1319 D3DSURFACE_DESC desc;
1320 D3DLOCKED_RECT lock_rect;
1321 D3DXVECTOR4 value;
1322 D3DXVECTOR2 coord, size;
1323 const struct pixel_format_desc *format;
1324 BYTE *data;
1325 HRESULT hr;
1327 TRACE("texture %p, function %p, funcdata %p.\n", texture, function, funcdata);
1329 if (!texture || !function)
1330 return D3DERR_INVALIDCALL;
1332 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1334 for (m = 0; m < miplevels; m++)
1336 if (FAILED(hr = IDirect3DTexture9_GetLevelDesc(texture, m, &desc)))
1337 return hr;
1339 format = get_format_info(desc.Format);
1340 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1342 FIXME("Unsupported texture format %#x.\n", desc.Format);
1343 return D3DERR_INVALIDCALL;
1346 if (FAILED(hr = IDirect3DTexture9_GetSurfaceLevel(texture, m, &surface)))
1347 return hr;
1348 if (FAILED(hr = lock_surface(surface, NULL, &lock_rect, &temp_surface, TRUE)))
1350 IDirect3DSurface9_Release(surface);
1351 return hr;
1354 size.x = 1.0f / desc.Width;
1355 size.y = 1.0f / desc.Height;
1357 data = lock_rect.pBits;
1359 for (y = 0; y < desc.Height; y++)
1361 /* The callback function expects the coordinates of the center
1362 of the texel */
1363 coord.y = (y + 0.5f) / desc.Height;
1365 for (x = 0; x < desc.Width; x++)
1367 coord.x = (x + 0.5f) / desc.Width;
1369 function(&value, &coord, &size, funcdata);
1371 fill_texture(format, data + y * lock_rect.Pitch + x * format->bytes_per_pixel, &value);
1374 if (FAILED(hr = unlock_surface(surface, NULL, temp_surface, TRUE)))
1376 IDirect3DSurface9_Release(surface);
1377 return hr;
1379 IDirect3DSurface9_Release(surface);
1382 return D3D_OK;
1385 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9 *device, const void *src_data,
1386 UINT src_data_size, UINT size, UINT mip_levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
1387 DWORD filter, DWORD mip_filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info,
1388 PALETTEENTRY *palette, IDirect3DCubeTexture9 **cube_texture)
1390 HRESULT hr;
1391 D3DCAPS9 caps;
1392 UINT loaded_miplevels;
1393 D3DXIMAGE_INFO img_info;
1394 BOOL dynamic_texture;
1395 BOOL file_size = FALSE;
1396 BOOL file_format = FALSE;
1397 BOOL file_mip_levels = FALSE;
1398 IDirect3DCubeTexture9 *tex, *buftex;
1400 TRACE("device %p, src_data %p, src_data_size %u, size %u, mip_levels %u, usage %#lx, "
1401 "format %#x, pool %#x, filter %#lx, mip_filter %#lx, color_key 0x%08lx, src_info %p, "
1402 "palette %p, cube_texture %p.\n",
1403 device, src_data, src_data_size, size, mip_levels, usage, format, pool, filter,
1404 mip_filter, color_key, src_info, palette, cube_texture);
1406 if (!device || !cube_texture || !src_data || !src_data_size)
1407 return D3DERR_INVALIDCALL;
1409 hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &img_info);
1410 if (FAILED(hr))
1411 return hr;
1413 if (img_info.ImageFileFormat != D3DXIFF_DDS)
1414 return D3DXERR_INVALIDDATA;
1416 if (img_info.Width != img_info.Height)
1417 return D3DXERR_INVALIDDATA;
1419 if (size == 0 || size == D3DX_DEFAULT_NONPOW2)
1420 size = img_info.Width;
1421 if (size == D3DX_DEFAULT)
1422 size = make_pow2(img_info.Width);
1424 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1425 format = img_info.Format;
1427 if (size == D3DX_FROM_FILE)
1429 file_size = TRUE;
1430 size = img_info.Width;
1433 if (format == D3DFMT_FROM_FILE)
1435 file_format = TRUE;
1436 format = img_info.Format;
1439 if (mip_levels == D3DX_FROM_FILE)
1441 file_mip_levels = TRUE;
1442 mip_levels = img_info.MipLevels;
1445 hr = D3DXCheckCubeTextureRequirements(device, &size, &mip_levels, usage, &format, pool);
1446 if (FAILED(hr))
1447 return hr;
1449 if ((file_size && size != img_info.Width)
1450 || (file_format && format != img_info.Format)
1451 || (file_mip_levels && mip_levels != img_info.MipLevels))
1452 return D3DERR_NOTAVAILABLE;
1454 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1455 if (FAILED(hr))
1456 return D3DERR_INVALIDCALL;
1458 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1459 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1461 hr = D3DXCreateCubeTexture(device, size, mip_levels, 0, format, D3DPOOL_SYSTEMMEM, &buftex);
1462 tex = buftex;
1464 else
1466 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1467 buftex = NULL;
1469 if (FAILED(hr))
1470 return hr;
1472 hr = load_cube_texture_from_dds(tex, src_data, palette, filter, color_key, &img_info);
1473 if (FAILED(hr))
1475 IDirect3DCubeTexture9_Release(tex);
1476 return hr;
1479 loaded_miplevels = min(IDirect3DCubeTexture9_GetLevelCount(tex), img_info.MipLevels);
1480 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, palette, loaded_miplevels - 1, mip_filter);
1481 if (FAILED(hr))
1483 IDirect3DCubeTexture9_Release(tex);
1484 return hr;
1487 if (buftex)
1489 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1490 if (FAILED(hr))
1492 IDirect3DCubeTexture9_Release(buftex);
1493 return hr;
1496 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1497 IDirect3DCubeTexture9_Release(buftex);
1500 if (src_info)
1501 *src_info = img_info;
1503 *cube_texture = tex;
1504 return D3D_OK;
1508 HRESULT WINAPI D3DXCreateCubeTextureFromFileA(IDirect3DDevice9 *device,
1509 const char *src_filename,
1510 IDirect3DCubeTexture9 **cube_texture)
1512 int len;
1513 HRESULT hr;
1514 WCHAR *filename;
1515 void *data;
1516 DWORD data_size;
1518 TRACE("device %p, src_filename %s, cube_texture %p.\n", device, wine_dbgstr_a(src_filename), cube_texture);
1520 if (!src_filename) return D3DERR_INVALIDCALL;
1522 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1523 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1524 if (!filename) return E_OUTOFMEMORY;
1525 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1527 hr = map_view_of_file(filename, &data, &data_size);
1528 if (FAILED(hr))
1530 HeapFree(GetProcessHeap(), 0, filename);
1531 return D3DXERR_INVALIDDATA;
1534 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1535 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1537 UnmapViewOfFile(data);
1538 HeapFree(GetProcessHeap(), 0, filename);
1539 return hr;
1542 HRESULT WINAPI D3DXCreateCubeTextureFromFileW(IDirect3DDevice9 *device,
1543 const WCHAR *src_filename,
1544 IDirect3DCubeTexture9 **cube_texture)
1546 HRESULT hr;
1547 void *data;
1548 DWORD data_size;
1550 TRACE("device %p, src_filename %s, cube_texture %p.\n", device, wine_dbgstr_w(src_filename), cube_texture);
1552 hr = map_view_of_file(src_filename, &data, &data_size);
1553 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1555 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1556 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1558 UnmapViewOfFile(data);
1559 return hr;
1562 HRESULT WINAPI D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9 *device, const char *src_filename,
1563 UINT size, UINT mip_levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, DWORD filter,
1564 DWORD mip_filter, D3DCOLOR color_key, D3DXIMAGE_INFO *image_info, PALETTEENTRY *palette,
1565 IDirect3DCubeTexture9 **cube_texture)
1567 int len;
1568 HRESULT hr;
1569 WCHAR *filename;
1570 void *data;
1571 DWORD data_size;
1573 TRACE("device %p, src_filename %s, size %u, mip_levels %u, usage %#lx, format %#x, pool %#x, "
1574 "filter %#lx, mip_filter %#lx, color_key 0x%08lx, image_info %p, palette %p, cube_texture %p.\n",
1575 device, wine_dbgstr_a(src_filename), size, mip_levels, usage, format, pool,
1576 filter, mip_filter, color_key, image_info, palette, cube_texture);
1578 if (!src_filename) return D3DERR_INVALIDCALL;
1580 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1581 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1582 if (!filename) return E_OUTOFMEMORY;
1583 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1585 hr = map_view_of_file(filename, &data, &data_size);
1586 if (FAILED(hr))
1588 HeapFree(GetProcessHeap(), 0, filename);
1589 return D3DXERR_INVALIDDATA;
1592 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1593 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1595 UnmapViewOfFile(data);
1596 HeapFree(GetProcessHeap(), 0, filename);
1597 return hr;
1600 HRESULT WINAPI D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9 *device, const WCHAR *src_filename,
1601 UINT size, UINT mip_levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, DWORD filter,
1602 DWORD mip_filter, D3DCOLOR color_key, D3DXIMAGE_INFO *image_info, PALETTEENTRY *palette,
1603 IDirect3DCubeTexture9 **cube_texture)
1605 HRESULT hr;
1606 void *data;
1607 DWORD data_size;
1609 TRACE("device %p, src_filename %s, size %u, mip_levels %u, usage %#lx, format %#x, pool %#x, "
1610 "filter %#lx, mip_filter %#lx, color_key 0x%08lx, image_info %p, palette %p, cube_texture %p.\n",
1611 device, wine_dbgstr_w(src_filename), size, mip_levels, usage, format,
1612 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1614 hr = map_view_of_file(src_filename, &data, &data_size);
1615 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1617 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1618 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1620 UnmapViewOfFile(data);
1621 return hr;
1624 enum cube_coord
1626 XCOORD = 0,
1627 XCOORDINV = 1,
1628 YCOORD = 2,
1629 YCOORDINV = 3,
1630 ZERO = 4,
1631 ONE = 5
1634 static float get_cube_coord(enum cube_coord coord, unsigned int x, unsigned int y, unsigned int size)
1636 switch (coord)
1638 case XCOORD:
1639 return x + 0.5f;
1640 case XCOORDINV:
1641 return size - x - 0.5f;
1642 case YCOORD:
1643 return y + 0.5f;
1644 case YCOORDINV:
1645 return size - y - 0.5f;
1646 case ZERO:
1647 return 0.0f;
1648 case ONE:
1649 return size;
1650 default:
1651 ERR("Unexpected coordinate value\n");
1652 return 0.0f;
1656 HRESULT WINAPI D3DXFillCubeTexture(struct IDirect3DCubeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1658 DWORD miplevels;
1659 DWORD m, x, y, f;
1660 D3DSURFACE_DESC desc;
1661 D3DLOCKED_RECT lock_rect;
1662 D3DXVECTOR4 value;
1663 D3DXVECTOR3 coord, size;
1664 const struct pixel_format_desc *format;
1665 BYTE *data;
1666 static const enum cube_coord coordmap[6][3] =
1668 {ONE, YCOORDINV, XCOORDINV},
1669 {ZERO, YCOORDINV, XCOORD},
1670 {XCOORD, ONE, YCOORD},
1671 {XCOORD, ZERO, YCOORDINV},
1672 {XCOORD, YCOORDINV, ONE},
1673 {XCOORDINV, YCOORDINV, ZERO}
1676 TRACE("texture %p, function %p, funcdata %p.\n", texture, function, funcdata);
1678 if (texture == NULL || function == NULL)
1679 return D3DERR_INVALIDCALL;
1681 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1683 for (m = 0; m < miplevels; m++)
1685 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture, m, &desc)))
1686 return D3DERR_INVALIDCALL;
1688 format = get_format_info(desc.Format);
1689 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1691 FIXME("Unsupported texture format %#x\n", desc.Format);
1692 return D3DERR_INVALIDCALL;
1695 for (f = 0; f < 6; f++)
1697 if (FAILED(IDirect3DCubeTexture9_LockRect(texture, f, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1698 return D3DERR_INVALIDCALL;
1700 size.x = (f == 0) || (f == 1) ? 0.0f : 2.0f / desc.Width;
1701 size.y = (f == 2) || (f == 3) ? 0.0f : 2.0f / desc.Width;
1702 size.z = (f == 4) || (f == 5) ? 0.0f : 2.0f / desc.Width;
1704 data = lock_rect.pBits;
1706 for (y = 0; y < desc.Height; y++)
1708 for (x = 0; x < desc.Width; x++)
1710 coord.x = get_cube_coord(coordmap[f][0], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1711 coord.y = get_cube_coord(coordmap[f][1], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1712 coord.z = get_cube_coord(coordmap[f][2], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1714 function(&value, &coord, &size, funcdata);
1716 fill_texture(format, data + y * lock_rect.Pitch + x * format->bytes_per_pixel, &value);
1719 IDirect3DCubeTexture9_UnlockRect(texture, f, m);
1723 return D3D_OK;
1726 HRESULT WINAPI D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1728 DWORD miplevels;
1729 DWORD m, x, y, z;
1730 D3DVOLUME_DESC desc;
1731 D3DLOCKED_BOX lock_box;
1732 D3DXVECTOR4 value;
1733 D3DXVECTOR3 coord, size;
1734 const struct pixel_format_desc *format;
1735 BYTE *data;
1737 TRACE("texture %p, function %p, funcdata %p.\n", texture, function, funcdata);
1739 if (texture == NULL || function == NULL)
1740 return D3DERR_INVALIDCALL;
1742 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1744 for (m = 0; m < miplevels; m++)
1746 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture, m, &desc)))
1747 return D3DERR_INVALIDCALL;
1749 format = get_format_info(desc.Format);
1750 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1752 FIXME("Unsupported texture format %#x\n", desc.Format);
1753 return D3DERR_INVALIDCALL;
1756 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture, m, &lock_box, NULL, D3DLOCK_DISCARD)))
1757 return D3DERR_INVALIDCALL;
1759 size.x = 1.0f / desc.Width;
1760 size.y = 1.0f / desc.Height;
1761 size.z = 1.0f / desc.Depth;
1763 data = lock_box.pBits;
1765 for (z = 0; z < desc.Depth; z++)
1767 /* The callback function expects the coordinates of the center
1768 of the texel */
1769 coord.z = (z + 0.5f) / desc.Depth;
1771 for (y = 0; y < desc.Height; y++)
1773 coord.y = (y + 0.5f) / desc.Height;
1775 for (x = 0; x < desc.Width; x++)
1777 coord.x = (x + 0.5f) / desc.Width;
1779 function(&value, &coord, &size, funcdata);
1781 fill_texture(format, data + z * lock_box.SlicePitch + y * lock_box.RowPitch
1782 + x * format->bytes_per_pixel, &value);
1786 IDirect3DVolumeTexture9_UnlockBox(texture, m);
1789 return D3D_OK;
1792 HRESULT WINAPI D3DXFillVolumeTextureTX(struct IDirect3DVolumeTexture9 *texture, ID3DXTextureShader *texture_shader)
1794 FIXME("texture %p, texture_shader %p stub.\n", texture, texture_shader);
1795 return E_NOTIMPL;
1798 HRESULT WINAPI D3DXSaveTextureToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1799 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1801 int len;
1802 WCHAR *filename;
1803 HRESULT hr;
1804 ID3DXBuffer *buffer;
1806 TRACE("dst_filename %s, file_format %u, src_texture %p, src_palette %p.\n",
1807 wine_dbgstr_a(dst_filename), file_format, src_texture, src_palette);
1809 if (!dst_filename) return D3DERR_INVALIDCALL;
1811 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1812 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1813 if (!filename) return E_OUTOFMEMORY;
1814 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1816 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1817 if (SUCCEEDED(hr))
1819 hr = write_buffer_to_file(filename, buffer);
1820 ID3DXBuffer_Release(buffer);
1823 HeapFree(GetProcessHeap(), 0, filename);
1824 return hr;
1827 HRESULT WINAPI D3DXSaveTextureToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1828 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1830 HRESULT hr;
1831 ID3DXBuffer *buffer;
1833 TRACE("dst_filename %s, file_format %u, src_texture %p, src_palette %p.\n",
1834 wine_dbgstr_w(dst_filename), file_format, src_texture, src_palette);
1836 if (!dst_filename) return D3DERR_INVALIDCALL;
1838 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1839 if (SUCCEEDED(hr))
1841 hr = write_buffer_to_file(dst_filename, buffer);
1842 ID3DXBuffer_Release(buffer);
1845 return hr;
1848 HRESULT WINAPI D3DXSaveTextureToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1849 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1851 HRESULT hr;
1852 D3DRESOURCETYPE type;
1853 IDirect3DSurface9 *surface;
1855 TRACE("dst_buffer %p, file_format %u, src_texture %p, src_palette %p.\n",
1856 dst_buffer, file_format, src_texture, src_palette);
1858 if (!dst_buffer || !src_texture) return D3DERR_INVALIDCALL;
1860 if (file_format == D3DXIFF_DDS)
1862 FIXME("DDS file format isn't supported yet\n");
1863 return E_NOTIMPL;
1866 type = IDirect3DBaseTexture9_GetType(src_texture);
1867 switch (type)
1869 case D3DRTYPE_TEXTURE:
1870 case D3DRTYPE_CUBETEXTURE:
1871 hr = get_surface(type, src_texture, D3DCUBEMAP_FACE_POSITIVE_X, 0, &surface);
1872 break;
1873 case D3DRTYPE_VOLUMETEXTURE:
1874 FIXME("Volume textures aren't supported yet\n");
1875 return E_NOTIMPL;
1876 default:
1877 return D3DERR_INVALIDCALL;
1880 if (SUCCEEDED(hr))
1882 hr = D3DXSaveSurfaceToFileInMemory(dst_buffer, file_format, surface, src_palette, NULL);
1883 IDirect3DSurface9_Release(surface);
1886 return hr;
1889 HRESULT WINAPI D3DXComputeNormalMap(IDirect3DTexture9 *texture, IDirect3DTexture9 *src_texture,
1890 const PALETTEENTRY *src_palette, DWORD flags, DWORD channel, float amplitude)
1892 FIXME("texture %p, src_texture %p, src_palette %p, flags %#lx, channel %lu, amplitude %.8e stub.\n",
1893 texture, src_texture, src_palette, flags, channel, amplitude);
1895 return D3D_OK;