mscoree: Implement CorBindToCurrentRuntime.
[wine/multimedia.git] / dlls / d3dx9_36 / texture.c
blob7ebe2645bde1e63422d533877d328f9d89b9b56c
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 "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)
37 UINT result = 1;
39 /* In the unlikely event somebody passes a large value, make sure we don't enter an infinite loop */
40 if (num >= 0x80000000)
41 return 0x80000000;
43 while (result < num)
44 result <<= 1;
46 return result;
49 static HRESULT get_surface(D3DRESOURCETYPE type, struct IDirect3DBaseTexture9 *tex,
50 int face, UINT level, struct IDirect3DSurface9 **surf)
52 switch (type)
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);
58 default:
59 ERR("Unexpected texture type\n");
60 return E_NOTIMPL;
64 HRESULT WINAPI D3DXFilterTexture(IDirect3DBaseTexture9 *texture,
65 const PALETTEENTRY *palette,
66 UINT srclevel,
67 DWORD filter)
69 UINT level;
70 HRESULT hr;
71 D3DRESOURCETYPE type;
73 TRACE("(%p, %p, %u, %#x)\n", texture, palette, srclevel, filter);
75 if (!texture)
76 return D3DERR_INVALIDCALL;
78 if ((filter & 0xFFFF) > D3DX_FILTER_BOX && filter != D3DX_DEFAULT)
79 return D3DERR_INVALIDCALL;
81 if (srclevel == D3DX_DEFAULT)
82 srclevel = 0;
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;
92 D3DSURFACE_DESC desc;
93 int i, numfaces;
95 if (type == D3DRTYPE_TEXTURE)
97 numfaces = 1;
98 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9*) texture, srclevel, &desc);
100 else
102 numfaces = 6;
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;
110 else
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);
119 if (FAILED(hr))
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);
126 topsurf = mipsurf;
128 if (FAILED(hr))
129 break;
131 level++;
134 IDirect3DSurface9_Release(topsurf);
135 if (FAILED(hr))
136 return hr;
139 return D3D_OK;
142 case D3DRTYPE_VOLUMETEXTURE:
144 D3DVOLUME_DESC desc;
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;
155 else
156 filter = D3DX_FILTER_BOX | D3DX_FILTER_DITHER;
159 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, srclevel, &top_volume);
160 if (FAILED(hr))
161 return hr;
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;
171 if (FAILED(hr))
172 break;
175 IDirect3DVolume9_Release(top_volume);
176 if (FAILED(hr))
177 return hr;
179 return D3D_OK;
182 default:
183 return D3DERR_INVALIDCALL;
187 static D3DFORMAT get_luminance_replacement_format(D3DFORMAT format)
189 static const struct
191 D3DFORMAT luminance_format;
192 D3DFORMAT replacement_format;
193 } luminance_replacements[] =
195 {D3DFMT_L8, D3DFMT_X8R8G8B8},
196 {D3DFMT_A8L8, D3DFMT_A8R8G8B8},
197 {D3DFMT_A4L4, D3DFMT_A4R4G4B4},
198 {D3DFMT_L16, D3DFMT_A16B16G16R16}
200 unsigned int i;
202 for (i = 0; i < sizeof(luminance_replacements) / sizeof(luminance_replacements[0]); ++i)
203 if (format == luminance_replacements[i].luminance_format)
204 return luminance_replacements[i].replacement_format;
205 return format;
208 HRESULT WINAPI D3DXCheckTextureRequirements(struct IDirect3DDevice9 *device, UINT *width, UINT *height,
209 UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
211 UINT w = (width && *width) ? *width : 1;
212 UINT h = (height && *height) ? *height : 1;
213 D3DCAPS9 caps;
214 D3DDEVICE_CREATION_PARAMETERS params;
215 IDirect3D9 *d3d = NULL;
216 D3DDISPLAYMODE mode;
217 HRESULT hr;
218 D3DFORMAT usedformat = D3DFMT_UNKNOWN;
219 const struct pixel_format_desc *fmt;
221 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device, width, height, miplevels, usage, format, pool);
223 if (!device)
224 return D3DERR_INVALIDCALL;
226 /* usage */
227 if (usage == D3DX_DEFAULT)
228 usage = 0;
229 if (usage & (D3DUSAGE_WRITEONLY | D3DUSAGE_DONOTCLIP | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES | D3DUSAGE_NPATCHES))
230 return D3DERR_INVALIDCALL;
232 /* pool */
233 if ((pool != D3DPOOL_DEFAULT) && (pool != D3DPOOL_MANAGED) && (pool != D3DPOOL_SYSTEMMEM) && (pool != D3DPOOL_SCRATCH))
234 return D3DERR_INVALIDCALL;
236 /* format */
237 if (format)
239 TRACE("Requested format %x\n", *format);
240 usedformat = *format;
243 hr = IDirect3DDevice9_GetDirect3D(device, &d3d);
245 if (FAILED(hr))
246 goto cleanup;
248 hr = IDirect3DDevice9_GetCreationParameters(device, &params);
250 if (FAILED(hr))
251 goto cleanup;
253 hr = IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
255 if (FAILED(hr))
256 goto cleanup;
258 if ((usedformat == D3DFMT_UNKNOWN) || (usedformat == D3DX_DEFAULT))
259 usedformat = D3DFMT_A8R8G8B8;
261 fmt = get_format_info(usedformat);
263 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType, mode.Format,
264 usage, D3DRTYPE_TEXTURE, usedformat);
265 if (FAILED(hr))
267 BOOL allow_24bits;
268 int bestscore = INT_MIN, i = 0, j;
269 unsigned int channels;
270 const struct pixel_format_desc *curfmt, *bestfmt = NULL;
272 TRACE("Requested format not supported, looking for a fallback.\n");
274 if (!fmt)
276 FIXME("Pixel format %x not handled\n", usedformat);
277 goto cleanup;
279 fmt = get_format_info(get_luminance_replacement_format(usedformat));
281 allow_24bits = fmt->bytes_per_pixel == 3;
282 channels = !!fmt->bits[0] + !!fmt->bits[1] + !!fmt->bits[2] + !!fmt->bits[3];
283 usedformat = D3DFMT_UNKNOWN;
285 while ((curfmt = get_format_info_idx(i)))
287 unsigned int curchannels = !!curfmt->bits[0] + !!curfmt->bits[1]
288 + !!curfmt->bits[2] + !!curfmt->bits[3];
289 int score;
291 i++;
293 if (curchannels < channels)
294 continue;
295 if (curfmt->bytes_per_pixel == 3 && !allow_24bits)
296 continue;
298 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
299 mode.Format, usage, D3DRTYPE_TEXTURE, curfmt->format);
300 if (FAILED(hr))
301 continue;
303 /* This format can be used, let's evaluate it.
304 Weights chosen quite arbitrarily... */
305 score = 16 - 4 * (curchannels - channels);
307 for (j = 0; j < 4; j++)
309 int diff = curfmt->bits[j] - fmt->bits[j];
310 score += 16 - (diff < 0 ? -diff * 4 : diff);
313 if (score > bestscore)
315 bestscore = score;
316 usedformat = curfmt->format;
317 bestfmt = curfmt;
320 fmt = bestfmt;
321 hr = D3D_OK;
324 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
325 return D3DERR_INVALIDCALL;
327 if ((w == D3DX_DEFAULT) && (h == D3DX_DEFAULT))
328 w = h = 256;
329 else if (w == D3DX_DEFAULT)
330 w = (height ? h : 256);
331 else if (h == D3DX_DEFAULT)
332 h = (width ? w : 256);
334 if (fmt->block_width != 1 || fmt->block_height != 1)
336 if (w < fmt->block_width)
337 w = fmt->block_width;
338 if (h < fmt->block_height)
339 h = fmt->block_height;
342 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(w)))
343 w = make_pow2(w);
345 if (w > caps.MaxTextureWidth)
346 w = caps.MaxTextureWidth;
348 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(h)))
349 h = make_pow2(h);
351 if (h > caps.MaxTextureHeight)
352 h = caps.MaxTextureHeight;
354 if (caps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
356 if (w > h)
357 h = w;
358 else
359 w = h;
362 if (width)
363 *width = w;
365 if (height)
366 *height = h;
368 if (miplevels && (usage & D3DUSAGE_AUTOGENMIPMAP))
370 if (*miplevels > 1)
371 *miplevels = 0;
373 else if (miplevels)
375 UINT max_mipmaps = 1;
377 if (!width && !height)
378 max_mipmaps = 9; /* number of mipmaps in a 256x256 texture */
379 else
381 UINT max_dimen = max(w, h);
383 while (max_dimen > 1)
385 max_dimen >>= 1;
386 max_mipmaps++;
390 if (*miplevels == 0 || *miplevels > max_mipmaps)
391 *miplevels = max_mipmaps;
394 cleanup:
396 if (d3d)
397 IDirect3D9_Release(d3d);
399 if (FAILED(hr))
400 return hr;
402 if (usedformat == D3DFMT_UNKNOWN)
404 WARN("Couldn't find a suitable pixel format\n");
405 return D3DERR_NOTAVAILABLE;
408 TRACE("Format chosen: %x\n", usedformat);
409 if (format)
410 *format = usedformat;
412 return D3D_OK;
415 HRESULT WINAPI D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9 *device, UINT *size,
416 UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
418 D3DCAPS9 caps;
419 UINT s = (size && *size) ? *size : 256;
420 HRESULT hr;
422 TRACE("(%p, %p, %p, %u, %p, %u)\n", device, size, miplevels, usage, format, pool);
424 if (s == D3DX_DEFAULT)
425 s = 256;
427 if (!device || FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
428 return D3DERR_INVALIDCALL;
430 if (!(caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP))
431 return D3DERR_NOTAVAILABLE;
433 /* ensure width/height is power of 2 */
434 if ((caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && (!is_pow2(s)))
435 s = make_pow2(s);
437 hr = D3DXCheckTextureRequirements(device, &s, &s, miplevels, usage, format, pool);
439 if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
441 if(miplevels)
442 *miplevels = 1;
445 if (size)
446 *size = s;
448 return hr;
451 HRESULT WINAPI D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9 *device, UINT *width, UINT *height,
452 UINT *depth, UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
454 D3DCAPS9 caps;
455 UINT w = width ? *width : D3DX_DEFAULT;
456 UINT h = height ? *height : D3DX_DEFAULT;
457 UINT d = (depth && *depth) ? *depth : 1;
458 HRESULT hr;
460 TRACE("(%p, %p, %p, %p, %p, %u, %p, %u)\n", device, width, height, depth, miplevels,
461 usage, format, pool);
463 if (!device || FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
464 return D3DERR_INVALIDCALL;
466 if (!(caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP))
467 return D3DERR_NOTAVAILABLE;
469 hr = D3DXCheckTextureRequirements(device, &w, &h, NULL, usage, format, pool);
470 if (d == D3DX_DEFAULT)
471 d = 1;
473 /* ensure width/height is power of 2 */
474 if ((caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2) &&
475 (!is_pow2(w) || !is_pow2(h) || !is_pow2(d)))
477 w = make_pow2(w);
478 h = make_pow2(h);
479 d = make_pow2(d);
482 if (w > caps.MaxVolumeExtent)
483 w = caps.MaxVolumeExtent;
484 if (h > caps.MaxVolumeExtent)
485 h = caps.MaxVolumeExtent;
486 if (d > caps.MaxVolumeExtent)
487 d = caps.MaxVolumeExtent;
489 if (miplevels)
491 if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
492 *miplevels = 1;
493 else if ((usage & D3DUSAGE_AUTOGENMIPMAP))
495 if (*miplevels > 1)
496 *miplevels = 0;
498 else
500 UINT max_mipmaps = 1;
501 UINT max_dimen = max(max(w, h), d);
503 while (max_dimen > 1)
505 max_dimen >>= 1;
506 max_mipmaps++;
509 if (*miplevels == 0 || *miplevels > max_mipmaps)
510 *miplevels = max_mipmaps;
514 if (width)
515 *width = w;
516 if (height)
517 *height = h;
518 if (depth)
519 *depth = d;
521 return hr;
524 HRESULT WINAPI D3DXCreateTexture(struct IDirect3DDevice9 *device, UINT width, UINT height,
525 UINT miplevels, DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DTexture9 **texture)
527 HRESULT hr;
529 TRACE("device %p, width %u, height %u, miplevels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
530 device, width, height, miplevels, usage, format, pool, texture);
532 if (!device || !texture)
533 return D3DERR_INVALIDCALL;
535 if (FAILED(hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool)))
536 return hr;
538 return IDirect3DDevice9_CreateTexture(device, width, height, miplevels, usage, format, pool, texture, NULL);
541 HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9 *device, const void *srcdata,
542 UINT srcdatasize, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
543 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
544 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
546 IDirect3DTexture9 **texptr;
547 IDirect3DTexture9 *buftex;
548 IDirect3DSurface9 *surface;
549 BOOL dynamic_texture;
550 D3DXIMAGE_INFO imginfo;
551 UINT loaded_miplevels, skip_levels;
552 D3DCAPS9 caps;
553 HRESULT hr;
555 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u,"
556 " usage %#x, format %#x, pool %#x, filter %#x, mipfilter %#x, colorkey %#x,"
557 " srcinfo %p, palette %p, texture %p.\n",
558 device, srcdata, srcdatasize, width, height, miplevels, usage, format, pool,
559 filter, mipfilter, colorkey, srcinfo, palette, texture);
561 /* check for invalid parameters */
562 if (!device || !texture || !srcdata || !srcdatasize)
563 return D3DERR_INVALIDCALL;
565 hr = D3DXGetImageInfoFromFileInMemory(srcdata, srcdatasize, &imginfo);
566 if (FAILED(hr))
568 FIXME("Unrecognized file format, returning failure.\n");
569 *texture = NULL;
570 return hr;
573 /* handle default values */
574 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
575 width = imginfo.Width;
577 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
578 height = imginfo.Height;
580 if (width == D3DX_DEFAULT)
581 width = make_pow2(imginfo.Width);
583 if (height == D3DX_DEFAULT)
584 height = make_pow2(imginfo.Height);
586 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
587 format = imginfo.Format;
589 if (width == D3DX_FROM_FILE)
591 width = imginfo.Width;
594 if (height == D3DX_FROM_FILE)
596 height = imginfo.Height;
599 if (format == D3DFMT_FROM_FILE)
601 format = imginfo.Format;
604 if (miplevels == D3DX_FROM_FILE)
606 miplevels = imginfo.MipLevels;
609 skip_levels = mipfilter != D3DX_DEFAULT ? mipfilter >> D3DX_SKIP_DDS_MIP_LEVELS_SHIFT : 0;
610 if (skip_levels && imginfo.MipLevels > skip_levels)
612 TRACE("Skipping the first %u (of %u) levels of a DDS mipmapped texture.\n",
613 skip_levels, imginfo.MipLevels);
614 TRACE("Texture level 0 dimensions are %ux%u.\n", imginfo.Width, imginfo.Height);
615 width >>= skip_levels;
616 height >>= skip_levels;
617 miplevels -= skip_levels;
619 else
621 skip_levels = 0;
624 /* fix texture creation parameters */
625 hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool);
626 if (FAILED(hr))
628 FIXME("Couldn't find suitable texture parameters.\n");
629 *texture = NULL;
630 return hr;
633 if (imginfo.MipLevels < miplevels && (D3DFMT_DXT1 <= imginfo.Format && imginfo.Format <= D3DFMT_DXT5))
635 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet\n");
636 miplevels = imginfo.MipLevels;
638 if (imginfo.ResourceType == D3DRTYPE_VOLUMETEXTURE
639 && D3DFMT_DXT1 <= imginfo.Format && imginfo.Format <= D3DFMT_DXT5 && miplevels > 1)
641 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet.\n");
642 miplevels = 1;
645 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
646 return D3DERR_INVALIDCALL;
648 /* Create the to-be-filled texture */
649 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
650 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
652 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
653 texptr = &buftex;
655 else
657 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
658 texptr = texture;
661 if (FAILED(hr))
663 FIXME("Texture creation failed.\n");
664 *texture = NULL;
665 return hr;
668 TRACE("Texture created correctly. Now loading the texture data into it.\n");
669 if (imginfo.ImageFileFormat != D3DXIFF_DDS)
671 IDirect3DTexture9_GetSurfaceLevel(*texptr, 0, &surface);
672 hr = D3DXLoadSurfaceFromFileInMemory(surface, palette, NULL, srcdata, srcdatasize, NULL, filter, colorkey, NULL);
673 IDirect3DSurface9_Release(surface);
674 loaded_miplevels = min(IDirect3DTexture9_GetLevelCount(*texptr), imginfo.MipLevels);
676 else
678 hr = load_texture_from_dds(*texptr, srcdata, palette, filter, colorkey, &imginfo, skip_levels,
679 &loaded_miplevels);
682 if (FAILED(hr))
684 FIXME("Texture loading failed.\n");
685 IDirect3DTexture9_Release(*texptr);
686 *texture = NULL;
687 return hr;
690 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) *texptr, palette, loaded_miplevels - 1, mipfilter);
691 if (FAILED(hr))
693 FIXME("Texture filtering failed.\n");
694 IDirect3DTexture9_Release(*texptr);
695 *texture = NULL;
696 return hr;
699 /* Move the data to the actual texture if necessary */
700 if (texptr == &buftex)
702 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
704 if (FAILED(hr))
706 IDirect3DTexture9_Release(buftex);
707 *texture = NULL;
708 return hr;
711 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9*)buftex, (IDirect3DBaseTexture9*)(*texture));
712 IDirect3DTexture9_Release(buftex);
715 if (srcinfo)
716 *srcinfo = imginfo;
718 return D3D_OK;
721 HRESULT WINAPI D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9 *device,
722 const void *srcdata, UINT srcdatasize, struct IDirect3DTexture9 **texture)
724 TRACE("(%p, %p, %d, %p)\n", device, srcdata, srcdatasize, texture);
726 return D3DXCreateTextureFromFileInMemoryEx(device, srcdata, srcdatasize, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
727 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
730 HRESULT WINAPI D3DXCreateTextureFromFileExW(struct IDirect3DDevice9 *device, const WCHAR *srcfile,
731 UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
732 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
733 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
735 void *buffer;
736 HRESULT hr;
737 DWORD size;
739 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
740 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
741 device, debugstr_w(srcfile), width, height, miplevels, usage, format,
742 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
744 if (!srcfile)
745 return D3DERR_INVALIDCALL;
747 hr = map_view_of_file(srcfile, &buffer, &size);
748 if (FAILED(hr))
749 return D3DXERR_INVALIDDATA;
751 hr = D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels, usage, format, pool,
752 filter, mipfilter, colorkey, srcinfo, palette, texture);
754 UnmapViewOfFile(buffer);
756 return hr;
759 HRESULT WINAPI D3DXCreateTextureFromFileExA(struct IDirect3DDevice9 *device, const char *srcfile,
760 UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
761 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
762 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
764 WCHAR *widename;
765 HRESULT hr;
766 DWORD len;
768 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
769 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
770 device, debugstr_a(srcfile), width, height, miplevels, usage, format,
771 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
773 if (!device || !srcfile || !texture)
774 return D3DERR_INVALIDCALL;
776 len = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
777 widename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*widename));
778 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, widename, len);
780 hr = D3DXCreateTextureFromFileExW(device, widename, width, height, miplevels,
781 usage, format, pool, filter, mipfilter,
782 colorkey, srcinfo, palette, texture);
784 HeapFree(GetProcessHeap(), 0, widename);
785 return hr;
788 HRESULT WINAPI D3DXCreateTextureFromFileA(struct IDirect3DDevice9 *device,
789 const char *srcfile, struct IDirect3DTexture9 **texture)
791 TRACE("(%p, %s, %p)\n", device, debugstr_a(srcfile), texture);
793 return D3DXCreateTextureFromFileExA(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
794 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
797 HRESULT WINAPI D3DXCreateTextureFromFileW(struct IDirect3DDevice9 *device,
798 const WCHAR *srcfile, struct IDirect3DTexture9 **texture)
800 TRACE("(%p, %s, %p)\n", device, debugstr_w(srcfile), texture);
802 return D3DXCreateTextureFromFileExW(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
803 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
807 HRESULT WINAPI D3DXCreateTextureFromResourceA(struct IDirect3DDevice9 *device,
808 HMODULE srcmodule, const char *resource, struct IDirect3DTexture9 **texture)
810 TRACE("(%p, %s): relay\n", srcmodule, debugstr_a(resource));
812 return D3DXCreateTextureFromResourceExA(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
813 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
816 HRESULT WINAPI D3DXCreateTextureFromResourceW(struct IDirect3DDevice9 *device,
817 HMODULE srcmodule, const WCHAR *resource, struct IDirect3DTexture9 **texture)
819 TRACE("(%p, %s): relay\n", srcmodule, debugstr_w(resource));
821 return D3DXCreateTextureFromResourceExW(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
822 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
825 HRESULT WINAPI D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9 *device, HMODULE srcmodule,
826 const char *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)
830 HRSRC resinfo;
831 void *buffer;
832 DWORD size;
834 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
835 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
836 device, srcmodule, debugstr_a(resource), width, height, miplevels, usage, format,
837 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
839 if (!device || !texture)
840 return D3DERR_INVALIDCALL;
842 if (!(resinfo = FindResourceA(srcmodule, resource, (const char *)RT_RCDATA))
843 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
844 && !(resinfo = FindResourceA(srcmodule, resource, (const char *)RT_BITMAP)))
845 return D3DXERR_INVALIDDATA;
847 if (FAILED(load_resource_into_memory(srcmodule, resinfo, &buffer, &size)))
848 return D3DXERR_INVALIDDATA;
850 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels,
851 usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
854 HRESULT WINAPI D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9 *device, HMODULE srcmodule,
855 const WCHAR *resource, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
856 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
857 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
859 HRSRC resinfo;
860 void *buffer;
861 DWORD size;
863 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
864 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
865 device, srcmodule, debugstr_w(resource), width, height, miplevels, usage, format,
866 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
868 if (!device || !texture)
869 return D3DERR_INVALIDCALL;
871 if (!(resinfo = FindResourceW(srcmodule, resource, (const WCHAR *)RT_RCDATA))
872 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
873 && !(resinfo = FindResourceW(srcmodule, resource, (const WCHAR *)RT_BITMAP)))
874 return D3DXERR_INVALIDDATA;
876 if (FAILED(load_resource_into_memory(srcmodule, resinfo, &buffer, &size)))
877 return D3DXERR_INVALIDDATA;
879 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels,
880 usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
883 HRESULT WINAPI D3DXCreateCubeTexture(struct IDirect3DDevice9 *device, UINT size, UINT miplevels,
884 DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DCubeTexture9 **texture)
886 HRESULT hr;
888 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device, size, miplevels, usage, format,
889 pool, texture);
891 if (!device || !texture)
892 return D3DERR_INVALIDCALL;
894 hr = D3DXCheckCubeTextureRequirements(device, &size, &miplevels, usage, &format, pool);
896 if (FAILED(hr))
898 TRACE("D3DXCheckCubeTextureRequirements failed\n");
899 return hr;
902 return IDirect3DDevice9_CreateCubeTexture(device, size, miplevels, usage, format, pool, texture, NULL);
905 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9 *device,
906 const void *data, UINT datasize, struct IDirect3DCubeTexture9 **texture)
908 TRACE("(%p, %p, %u, %p)\n", device, data, datasize, texture);
910 return D3DXCreateCubeTextureFromFileInMemoryEx(device, data, datasize, D3DX_DEFAULT, D3DX_DEFAULT,
911 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
914 HRESULT WINAPI D3DXCreateVolumeTexture(struct IDirect3DDevice9 *device, UINT width, UINT height, UINT depth,
915 UINT miplevels, DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DVolumeTexture9 **texture)
917 HRESULT hr;
919 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device, width, height, depth,
920 miplevels, usage, format, pool, texture);
922 if (!device || !texture)
923 return D3DERR_INVALIDCALL;
925 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth,
926 &miplevels, usage, &format, pool);
928 if (FAILED(hr))
930 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
931 return hr;
934 return IDirect3DDevice9_CreateVolumeTexture(device, width, height, depth, miplevels,
935 usage, format, pool, texture, NULL);
938 HRESULT WINAPI D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9 *device,
939 const char *filename,
940 IDirect3DVolumeTexture9 **volume_texture)
942 int len;
943 HRESULT hr;
944 void *data;
945 DWORD data_size;
946 WCHAR *filenameW;
948 TRACE("(%p, %s, %p): relay\n",
949 device, debugstr_a(filename), volume_texture);
951 if (!filename) return D3DERR_INVALIDCALL;
953 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
954 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
955 if (!filenameW) return E_OUTOFMEMORY;
956 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
958 hr = map_view_of_file(filenameW, &data, &data_size);
959 HeapFree(GetProcessHeap(), 0, filenameW);
960 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
962 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
963 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
965 UnmapViewOfFile(data);
966 return hr;
969 HRESULT WINAPI D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9 *device,
970 const WCHAR *filename,
971 IDirect3DVolumeTexture9 **volume_texture)
973 HRESULT hr;
974 void *data;
975 DWORD data_size;
977 TRACE("(%p, %s, %p): relay\n",
978 device, debugstr_w(filename), volume_texture);
980 if (!filename) return D3DERR_INVALIDCALL;
982 hr = map_view_of_file(filename, &data, &data_size);
983 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
985 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
986 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
988 UnmapViewOfFile(data);
989 return hr;
992 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9 *device,
993 const char *filename,
994 UINT width,
995 UINT height,
996 UINT depth,
997 UINT mip_levels,
998 DWORD usage,
999 D3DFORMAT format,
1000 D3DPOOL pool,
1001 DWORD filter,
1002 DWORD mip_filter,
1003 D3DCOLOR color_key,
1004 D3DXIMAGE_INFO *src_info,
1005 PALETTEENTRY *palette,
1006 IDirect3DVolumeTexture9 **volume_texture)
1008 int len;
1009 HRESULT hr;
1010 WCHAR *filenameW;
1011 void *data;
1012 DWORD data_size;
1014 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1015 device, debugstr_a(filename), width, height, depth, mip_levels,
1016 usage, format, pool, filter, mip_filter, color_key, src_info,
1017 palette, volume_texture);
1019 if (!filename) return D3DERR_INVALIDCALL;
1021 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
1022 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1023 if (!filenameW) return E_OUTOFMEMORY;
1024 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
1026 hr = map_view_of_file(filenameW, &data, &data_size);
1027 HeapFree(GetProcessHeap(), 0, filenameW);
1028 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1030 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1031 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1032 volume_texture);
1034 UnmapViewOfFile(data);
1035 return hr;
1038 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9 *device,
1039 const WCHAR *filename,
1040 UINT width,
1041 UINT height,
1042 UINT depth,
1043 UINT mip_levels,
1044 DWORD usage,
1045 D3DFORMAT format,
1046 D3DPOOL pool,
1047 DWORD filter,
1048 DWORD mip_filter,
1049 D3DCOLOR color_key,
1050 D3DXIMAGE_INFO *src_info,
1051 PALETTEENTRY *palette,
1052 IDirect3DVolumeTexture9 **volume_texture)
1054 HRESULT hr;
1055 void *data;
1056 DWORD data_size;
1058 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1059 device, debugstr_w(filename), width, height, depth, mip_levels,
1060 usage, format, pool, filter, mip_filter, color_key, src_info,
1061 palette, volume_texture);
1063 if (!filename) return D3DERR_INVALIDCALL;
1065 hr = map_view_of_file(filename, &data, &data_size);
1066 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1068 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1069 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1070 volume_texture);
1072 UnmapViewOfFile(data);
1073 return hr;
1076 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9 *device,
1077 const void *data,
1078 UINT data_size,
1079 IDirect3DVolumeTexture9 **volume_texture)
1081 TRACE("(%p, %p, %u, %p): relay\n", device, data, data_size, volume_texture);
1083 return D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1084 D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
1085 0, NULL, NULL, volume_texture);
1088 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
1089 const void *data,
1090 UINT data_size,
1091 UINT width,
1092 UINT height,
1093 UINT depth,
1094 UINT mip_levels,
1095 DWORD usage,
1096 D3DFORMAT format,
1097 D3DPOOL pool,
1098 DWORD filter,
1099 DWORD mip_filter,
1100 D3DCOLOR color_key,
1101 D3DXIMAGE_INFO *info,
1102 PALETTEENTRY *palette,
1103 IDirect3DVolumeTexture9 **volume_texture)
1105 HRESULT hr;
1106 D3DCAPS9 caps;
1107 D3DXIMAGE_INFO image_info;
1108 BOOL dynamic_texture;
1109 BOOL file_width = FALSE;
1110 BOOL file_height = FALSE;
1111 BOOL file_depth = FALSE;
1112 BOOL file_format = FALSE;
1113 BOOL file_mip_levels = FALSE;
1114 IDirect3DVolumeTexture9 *tex, *buftex;
1116 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1117 device, data, data_size, width, height, depth, mip_levels, usage, format, pool,
1118 filter, mip_filter, color_key, info, palette, volume_texture);
1120 if (!device || !data || !data_size || !volume_texture)
1121 return D3DERR_INVALIDCALL;
1123 hr = D3DXGetImageInfoFromFileInMemory(data, data_size, &image_info);
1124 if (FAILED(hr)) return hr;
1126 if (image_info.ImageFileFormat != D3DXIFF_DDS)
1127 return D3DXERR_INVALIDDATA;
1129 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
1130 width = image_info.Width;
1131 if (width == D3DX_DEFAULT)
1132 width = make_pow2(image_info.Width);
1134 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
1135 height = image_info.Height;
1136 if (height == D3DX_DEFAULT)
1137 height = make_pow2(image_info.Height);
1139 if (depth == 0 || depth == D3DX_DEFAULT_NONPOW2)
1140 depth = image_info.Depth;
1141 if (depth == D3DX_DEFAULT)
1142 depth = make_pow2(image_info.Depth);
1144 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1145 format = image_info.Format;
1147 if (width == D3DX_FROM_FILE)
1149 file_width = TRUE;
1150 width = image_info.Width;
1153 if (height == D3DX_FROM_FILE)
1155 file_height = TRUE;
1156 height = image_info.Height;
1159 if (depth == D3DX_FROM_FILE)
1161 file_depth = TRUE;
1162 depth = image_info.Depth;
1165 if (format == D3DFMT_FROM_FILE)
1167 file_format = TRUE;
1168 format = image_info.Format;
1171 if (mip_levels == D3DX_FROM_FILE)
1173 file_mip_levels = TRUE;
1174 mip_levels = image_info.MipLevels;
1177 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, &mip_levels, usage, &format, pool);
1178 if (FAILED(hr)) return hr;
1180 if ((file_width && width != image_info.Width)
1181 || (file_height && height != image_info.Height)
1182 || (file_depth && depth != image_info.Depth)
1183 || (file_format && format != image_info.Format)
1184 || (file_mip_levels && mip_levels != image_info.MipLevels))
1185 return D3DERR_NOTAVAILABLE;
1187 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1188 if (FAILED(hr))
1189 return D3DERR_INVALIDCALL;
1191 if (mip_levels > image_info.MipLevels)
1193 FIXME("Generation of mipmaps for volume textures is not implemented yet\n");
1194 mip_levels = image_info.MipLevels;
1197 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1198 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1200 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
1201 tex = buftex;
1203 else
1205 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1206 buftex = NULL;
1209 if (FAILED(hr)) return hr;
1211 hr = load_volume_texture_from_dds(tex, data, palette, filter, color_key, &image_info);
1212 if (FAILED(hr))
1214 IDirect3DVolumeTexture9_Release(tex);
1215 return hr;
1218 if (buftex)
1220 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1221 if (FAILED(hr))
1223 IDirect3DVolumeTexture9_Release(buftex);
1224 return hr;
1227 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1228 IDirect3DVolumeTexture9_Release(buftex);
1231 if (info)
1232 *info = image_info;
1234 *volume_texture = tex;
1235 return D3D_OK;
1238 static inline void fill_texture(const struct pixel_format_desc *format, BYTE *pos, const D3DXVECTOR4 *value)
1240 DWORD c;
1242 for (c = 0; c < format->bytes_per_pixel; c++)
1243 pos[c] = 0;
1245 for (c = 0; c < 4; c++)
1247 float comp_value;
1248 DWORD i, v = 0, mask32 = format->bits[c] == 32 ? ~0U : ((1 << format->bits[c]) - 1);
1250 switch (c)
1252 case 0: /* Alpha */
1253 comp_value = value->w;
1254 break;
1255 case 1: /* Red */
1256 comp_value = value->x;
1257 break;
1258 case 2: /* Green */
1259 comp_value = value->y;
1260 break;
1261 case 3: /* Blue */
1262 comp_value = value->z;
1263 break;
1266 if (format->type == FORMAT_ARGBF16)
1267 v = float_32_to_16(comp_value);
1268 else if (format->type == FORMAT_ARGBF)
1269 v = *(DWORD *)&comp_value;
1270 else if (format->type == FORMAT_ARGB)
1271 v = comp_value * ((1 << format->bits[c]) - 1) + 0.5f;
1272 else
1273 FIXME("Unhandled format type %#x\n", format->type);
1275 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1277 BYTE byte, mask;
1279 if (format->shift[c] > i)
1281 mask = mask32 << (format->shift[c] - i);
1282 byte = (v << (format->shift[c] - i)) & mask;
1284 else
1286 mask = mask32 >> (i - format->shift[c]);
1287 byte = (v >> (i - format->shift[c])) & mask;
1289 pos[i / 8] |= byte;
1294 HRESULT WINAPI D3DXFillTexture(struct IDirect3DTexture9 *texture, LPD3DXFILL2D function, void *funcdata)
1296 DWORD miplevels;
1297 DWORD m, x, y;
1298 D3DSURFACE_DESC desc;
1299 D3DLOCKED_RECT lock_rect;
1300 D3DXVECTOR4 value;
1301 D3DXVECTOR2 coord, size;
1302 const struct pixel_format_desc *format;
1303 BYTE *data;
1305 if (texture == NULL || function == NULL)
1306 return D3DERR_INVALIDCALL;
1308 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1310 for (m = 0; m < miplevels; m++)
1312 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture, m, &desc)))
1313 return D3DERR_INVALIDCALL;
1315 format = get_format_info(desc.Format);
1316 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1318 FIXME("Unsupported texture format %#x\n", desc.Format);
1319 return D3DERR_INVALIDCALL;
1322 if (FAILED(IDirect3DTexture9_LockRect(texture, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1323 return D3DERR_INVALIDCALL;
1325 size.x = 1.0f / desc.Width;
1326 size.y = 1.0f / desc.Height;
1328 data = lock_rect.pBits;
1330 for (y = 0; y < desc.Height; y++)
1332 /* The callback function expects the coordinates of the center
1333 of the texel */
1334 coord.y = (y + 0.5f) / desc.Height;
1336 for (x = 0; x < desc.Width; x++)
1338 coord.x = (x + 0.5f) / desc.Width;
1340 function(&value, &coord, &size, funcdata);
1342 fill_texture(format, data + y * lock_rect.Pitch + x * format->bytes_per_pixel, &value);
1345 IDirect3DTexture9_UnlockRect(texture, m);
1348 return D3D_OK;
1351 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
1352 const void *src_data,
1353 UINT src_data_size,
1354 UINT size,
1355 UINT mip_levels,
1356 DWORD usage,
1357 D3DFORMAT format,
1358 D3DPOOL pool,
1359 DWORD filter,
1360 DWORD mip_filter,
1361 D3DCOLOR color_key,
1362 D3DXIMAGE_INFO *src_info,
1363 PALETTEENTRY *palette,
1364 IDirect3DCubeTexture9 **cube_texture)
1366 HRESULT hr;
1367 D3DCAPS9 caps;
1368 UINT loaded_miplevels;
1369 D3DXIMAGE_INFO img_info;
1370 BOOL dynamic_texture;
1371 BOOL file_size = FALSE;
1372 BOOL file_format = FALSE;
1373 BOOL file_mip_levels = FALSE;
1374 IDirect3DCubeTexture9 *tex, *buftex;
1376 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device,
1377 src_data, src_data_size, size, mip_levels, usage, format, pool, filter, mip_filter,
1378 color_key, src_info, palette, cube_texture);
1380 if (!device || !cube_texture || !src_data || !src_data_size)
1381 return D3DERR_INVALIDCALL;
1383 hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &img_info);
1384 if (FAILED(hr))
1385 return hr;
1387 if (img_info.ImageFileFormat != D3DXIFF_DDS)
1388 return D3DXERR_INVALIDDATA;
1390 if (img_info.Width != img_info.Height)
1391 return D3DXERR_INVALIDDATA;
1393 if (size == 0 || size == D3DX_DEFAULT_NONPOW2)
1394 size = img_info.Width;
1395 if (size == D3DX_DEFAULT)
1396 size = make_pow2(img_info.Width);
1398 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1399 format = img_info.Format;
1401 if (size == D3DX_FROM_FILE)
1403 file_size = TRUE;
1404 size = img_info.Width;
1407 if (format == D3DFMT_FROM_FILE)
1409 file_format = TRUE;
1410 format = img_info.Format;
1413 if (mip_levels == D3DX_FROM_FILE)
1415 file_mip_levels = TRUE;
1416 mip_levels = img_info.MipLevels;
1419 hr = D3DXCheckCubeTextureRequirements(device, &size, &mip_levels, usage, &format, pool);
1420 if (FAILED(hr))
1421 return hr;
1423 if ((file_size && size != img_info.Width)
1424 || (file_format && format != img_info.Format)
1425 || (file_mip_levels && mip_levels != img_info.MipLevels))
1426 return D3DERR_NOTAVAILABLE;
1428 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1429 if (FAILED(hr))
1430 return D3DERR_INVALIDCALL;
1432 if (mip_levels > img_info.MipLevels && (D3DFMT_DXT1 <= img_info.Format && img_info.Format <= D3DFMT_DXT5))
1434 FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
1435 mip_levels = img_info.MipLevels;
1438 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1439 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1441 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
1442 tex = buftex;
1444 else
1446 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1447 buftex = NULL;
1449 if (FAILED(hr))
1450 return hr;
1452 hr = load_cube_texture_from_dds(tex, src_data, palette, filter, color_key, &img_info);
1453 if (FAILED(hr))
1455 IDirect3DCubeTexture9_Release(tex);
1456 return hr;
1459 loaded_miplevels = min(IDirect3DCubeTexture9_GetLevelCount(tex), img_info.MipLevels);
1460 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, palette, loaded_miplevels - 1, mip_filter);
1461 if (FAILED(hr))
1463 IDirect3DCubeTexture9_Release(tex);
1464 return hr;
1467 if (buftex)
1469 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1470 if (FAILED(hr))
1472 IDirect3DCubeTexture9_Release(buftex);
1473 return hr;
1476 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1477 IDirect3DCubeTexture9_Release(buftex);
1480 if (src_info)
1481 *src_info = img_info;
1483 *cube_texture = tex;
1484 return D3D_OK;
1488 HRESULT WINAPI D3DXCreateCubeTextureFromFileA(IDirect3DDevice9 *device,
1489 const char *src_filename,
1490 IDirect3DCubeTexture9 **cube_texture)
1492 int len;
1493 HRESULT hr;
1494 WCHAR *filename;
1495 void *data;
1496 DWORD data_size;
1498 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_a(src_filename), cube_texture);
1500 if (!src_filename) return D3DERR_INVALIDCALL;
1502 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1503 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1504 if (!filename) return E_OUTOFMEMORY;
1505 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1507 hr = map_view_of_file(filename, &data, &data_size);
1508 if (FAILED(hr))
1510 HeapFree(GetProcessHeap(), 0, filename);
1511 return D3DXERR_INVALIDDATA;
1514 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1515 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1517 UnmapViewOfFile(data);
1518 HeapFree(GetProcessHeap(), 0, filename);
1519 return hr;
1522 HRESULT WINAPI D3DXCreateCubeTextureFromFileW(IDirect3DDevice9 *device,
1523 const WCHAR *src_filename,
1524 IDirect3DCubeTexture9 **cube_texture)
1526 HRESULT hr;
1527 void *data;
1528 DWORD data_size;
1530 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_w(src_filename), cube_texture);
1532 hr = map_view_of_file(src_filename, &data, &data_size);
1533 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1535 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1536 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1538 UnmapViewOfFile(data);
1539 return hr;
1542 HRESULT WINAPI D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9 *device,
1543 const char *src_filename,
1544 UINT size,
1545 UINT mip_levels,
1546 DWORD usage,
1547 D3DFORMAT format,
1548 D3DPOOL pool,
1549 DWORD filter,
1550 DWORD mip_filter,
1551 D3DCOLOR color_key,
1552 D3DXIMAGE_INFO *image_info,
1553 PALETTEENTRY *palette,
1554 IDirect3DCubeTexture9 **cube_texture)
1556 int len;
1557 HRESULT hr;
1558 WCHAR *filename;
1559 void *data;
1560 DWORD data_size;
1562 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1563 device, wine_dbgstr_a(src_filename), size, mip_levels, usage, format,
1564 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1566 if (!src_filename) return D3DERR_INVALIDCALL;
1568 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1569 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1570 if (!filename) return E_OUTOFMEMORY;
1571 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1573 hr = map_view_of_file(filename, &data, &data_size);
1574 if (FAILED(hr))
1576 HeapFree(GetProcessHeap(), 0, filename);
1577 return D3DXERR_INVALIDDATA;
1580 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1581 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1583 UnmapViewOfFile(data);
1584 HeapFree(GetProcessHeap(), 0, filename);
1585 return hr;
1588 HRESULT WINAPI D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9 *device,
1589 const WCHAR *src_filename,
1590 UINT size,
1591 UINT mip_levels,
1592 DWORD usage,
1593 D3DFORMAT format,
1594 D3DPOOL pool,
1595 DWORD filter,
1596 DWORD mip_filter,
1597 D3DCOLOR color_key,
1598 D3DXIMAGE_INFO *image_info,
1599 PALETTEENTRY *palette,
1600 IDirect3DCubeTexture9 **cube_texture)
1602 HRESULT hr;
1603 void *data;
1604 DWORD data_size;
1606 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1607 device, wine_dbgstr_w(src_filename), size, mip_levels, usage, format,
1608 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1610 hr = map_view_of_file(src_filename, &data, &data_size);
1611 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1613 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1614 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1616 UnmapViewOfFile(data);
1617 return hr;
1620 enum cube_coord
1622 XCOORD = 0,
1623 XCOORDINV = 1,
1624 YCOORD = 2,
1625 YCOORDINV = 3,
1626 ZERO = 4,
1627 ONE = 5
1630 static float get_cube_coord(enum cube_coord coord, unsigned int x, unsigned int y, unsigned int size)
1632 switch (coord)
1634 case XCOORD:
1635 return x + 0.5f;
1636 case XCOORDINV:
1637 return size - x - 0.5f;
1638 case YCOORD:
1639 return y + 0.5f;
1640 case YCOORDINV:
1641 return size - y - 0.5f;
1642 case ZERO:
1643 return 0.0f;
1644 case ONE:
1645 return size;
1646 default:
1647 ERR("Unexpected coordinate value\n");
1648 return 0.0f;
1652 HRESULT WINAPI D3DXFillCubeTexture(struct IDirect3DCubeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1654 DWORD miplevels;
1655 DWORD m, x, y, f;
1656 D3DSURFACE_DESC desc;
1657 D3DLOCKED_RECT lock_rect;
1658 D3DXVECTOR4 value;
1659 D3DXVECTOR3 coord, size;
1660 const struct pixel_format_desc *format;
1661 BYTE *data;
1662 static const enum cube_coord coordmap[6][3] =
1664 {ONE, YCOORDINV, XCOORDINV},
1665 {ZERO, YCOORDINV, XCOORD},
1666 {XCOORD, ONE, YCOORD},
1667 {XCOORD, ZERO, YCOORDINV},
1668 {XCOORD, YCOORDINV, ONE},
1669 {XCOORDINV, YCOORDINV, ZERO}
1672 if (texture == NULL || function == NULL)
1673 return D3DERR_INVALIDCALL;
1675 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1677 for (m = 0; m < miplevels; m++)
1679 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture, m, &desc)))
1680 return D3DERR_INVALIDCALL;
1682 format = get_format_info(desc.Format);
1683 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1685 FIXME("Unsupported texture format %#x\n", desc.Format);
1686 return D3DERR_INVALIDCALL;
1689 for (f = 0; f < 6; f++)
1691 if (FAILED(IDirect3DCubeTexture9_LockRect(texture, f, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1692 return D3DERR_INVALIDCALL;
1694 size.x = (f == 0) || (f == 1) ? 0.0f : 2.0f / desc.Width;
1695 size.y = (f == 2) || (f == 3) ? 0.0f : 2.0f / desc.Width;
1696 size.z = (f == 4) || (f == 5) ? 0.0f : 2.0f / desc.Width;
1698 data = lock_rect.pBits;
1700 for (y = 0; y < desc.Height; y++)
1702 for (x = 0; x < desc.Width; x++)
1704 coord.x = get_cube_coord(coordmap[f][0], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1705 coord.y = get_cube_coord(coordmap[f][1], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1706 coord.z = get_cube_coord(coordmap[f][2], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1708 function(&value, &coord, &size, funcdata);
1710 fill_texture(format, data + y * lock_rect.Pitch + x * format->bytes_per_pixel, &value);
1713 IDirect3DCubeTexture9_UnlockRect(texture, f, m);
1717 return D3D_OK;
1720 HRESULT WINAPI D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1722 DWORD miplevels;
1723 DWORD m, x, y, z;
1724 D3DVOLUME_DESC desc;
1725 D3DLOCKED_BOX lock_box;
1726 D3DXVECTOR4 value;
1727 D3DXVECTOR3 coord, size;
1728 const struct pixel_format_desc *format;
1729 BYTE *data;
1731 if (texture == NULL || function == NULL)
1732 return D3DERR_INVALIDCALL;
1734 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1736 for (m = 0; m < miplevels; m++)
1738 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture, m, &desc)))
1739 return D3DERR_INVALIDCALL;
1741 format = get_format_info(desc.Format);
1742 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1744 FIXME("Unsupported texture format %#x\n", desc.Format);
1745 return D3DERR_INVALIDCALL;
1748 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture, m, &lock_box, NULL, D3DLOCK_DISCARD)))
1749 return D3DERR_INVALIDCALL;
1751 size.x = 1.0f / desc.Width;
1752 size.y = 1.0f / desc.Height;
1753 size.z = 1.0f / desc.Depth;
1755 data = lock_box.pBits;
1757 for (z = 0; z < desc.Depth; z++)
1759 /* The callback function expects the coordinates of the center
1760 of the texel */
1761 coord.z = (z + 0.5f) / desc.Depth;
1763 for (y = 0; y < desc.Height; y++)
1765 coord.y = (y + 0.5f) / desc.Height;
1767 for (x = 0; x < desc.Width; x++)
1769 coord.x = (x + 0.5f) / desc.Width;
1771 function(&value, &coord, &size, funcdata);
1773 fill_texture(format, data + z * lock_box.SlicePitch + y * lock_box.RowPitch
1774 + x * format->bytes_per_pixel, &value);
1778 IDirect3DVolumeTexture9_UnlockBox(texture, m);
1781 return D3D_OK;
1784 HRESULT WINAPI D3DXSaveTextureToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1785 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1787 int len;
1788 WCHAR *filename;
1789 HRESULT hr;
1790 ID3DXBuffer *buffer;
1792 TRACE("(%s, %#x, %p, %p): relay\n",
1793 wine_dbgstr_a(dst_filename), file_format, src_texture, src_palette);
1795 if (!dst_filename) return D3DERR_INVALIDCALL;
1797 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1798 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1799 if (!filename) return E_OUTOFMEMORY;
1800 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1802 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1803 if (SUCCEEDED(hr))
1805 hr = write_buffer_to_file(filename, buffer);
1806 ID3DXBuffer_Release(buffer);
1809 HeapFree(GetProcessHeap(), 0, filename);
1810 return hr;
1813 HRESULT WINAPI D3DXSaveTextureToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1814 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1816 HRESULT hr;
1817 ID3DXBuffer *buffer;
1819 TRACE("(%s, %#x, %p, %p): relay\n",
1820 wine_dbgstr_w(dst_filename), file_format, src_texture, src_palette);
1822 if (!dst_filename) return D3DERR_INVALIDCALL;
1824 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1825 if (SUCCEEDED(hr))
1827 hr = write_buffer_to_file(dst_filename, buffer);
1828 ID3DXBuffer_Release(buffer);
1831 return hr;
1834 HRESULT WINAPI D3DXSaveTextureToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1835 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1837 HRESULT hr;
1838 D3DRESOURCETYPE type;
1839 IDirect3DSurface9 *surface;
1841 TRACE("(%p, %#x, %p, %p)\n",
1842 dst_buffer, file_format, src_texture, src_palette);
1844 if (!dst_buffer || !src_texture) return D3DERR_INVALIDCALL;
1846 if (file_format == D3DXIFF_DDS)
1848 FIXME("DDS file format isn't supported yet\n");
1849 return E_NOTIMPL;
1852 type = IDirect3DBaseTexture9_GetType(src_texture);
1853 switch (type)
1855 case D3DRTYPE_TEXTURE:
1856 case D3DRTYPE_CUBETEXTURE:
1857 hr = get_surface(type, src_texture, D3DCUBEMAP_FACE_POSITIVE_X, 0, &surface);
1858 break;
1859 case D3DRTYPE_VOLUMETEXTURE:
1860 FIXME("Volume textures aren't supported yet\n");
1861 return E_NOTIMPL;
1862 default:
1863 return D3DERR_INVALIDCALL;
1866 if (SUCCEEDED(hr))
1868 hr = D3DXSaveSurfaceToFileInMemory(dst_buffer, file_format, surface, src_palette, NULL);
1869 IDirect3DSurface9_Release(surface);
1872 return hr;