d2d1: Add the ID2D1Image interface.
[wine/wine-gecko.git] / dlls / d3dx9_36 / texture.c
blob32415a25a78671a96e9496313446bf23e3ced8e0
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 HRESULT WINAPI D3DXCheckTextureRequirements(struct IDirect3DDevice9 *device, UINT *width, UINT *height,
188 UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
190 UINT w = (width && *width) ? *width : 1;
191 UINT h = (height && *height) ? *height : 1;
192 D3DCAPS9 caps;
193 D3DDEVICE_CREATION_PARAMETERS params;
194 IDirect3D9 *d3d = NULL;
195 D3DDISPLAYMODE mode;
196 HRESULT hr;
197 D3DFORMAT usedformat = D3DFMT_UNKNOWN;
198 const struct pixel_format_desc *fmt;
200 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device, width, height, miplevels, usage, format, pool);
202 if (!device)
203 return D3DERR_INVALIDCALL;
205 /* usage */
206 if (usage == D3DX_DEFAULT)
207 usage = 0;
208 if (usage & (D3DUSAGE_WRITEONLY | D3DUSAGE_DONOTCLIP | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES | D3DUSAGE_NPATCHES))
209 return D3DERR_INVALIDCALL;
211 /* pool */
212 if ((pool != D3DPOOL_DEFAULT) && (pool != D3DPOOL_MANAGED) && (pool != D3DPOOL_SYSTEMMEM) && (pool != D3DPOOL_SCRATCH))
213 return D3DERR_INVALIDCALL;
215 /* format */
216 if (format)
218 TRACE("Requested format %x\n", *format);
219 usedformat = *format;
222 hr = IDirect3DDevice9_GetDirect3D(device, &d3d);
224 if (FAILED(hr))
225 goto cleanup;
227 hr = IDirect3DDevice9_GetCreationParameters(device, &params);
229 if (FAILED(hr))
230 goto cleanup;
232 hr = IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
234 if (FAILED(hr))
235 goto cleanup;
237 if ((usedformat == D3DFMT_UNKNOWN) || (usedformat == D3DX_DEFAULT))
238 usedformat = D3DFMT_A8R8G8B8;
240 fmt = get_format_info(usedformat);
242 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType, mode.Format,
243 usage, D3DRTYPE_TEXTURE, usedformat);
244 if (FAILED(hr))
246 BOOL allow_24bits;
247 int bestscore = INT_MIN, i = 0, j;
248 unsigned int channels;
249 const struct pixel_format_desc *curfmt, *bestfmt = NULL;
251 TRACE("Requested format not supported, looking for a fallback.\n");
253 if (!fmt)
255 FIXME("Pixel format %x not handled\n", usedformat);
256 goto cleanup;
259 allow_24bits = fmt->bytes_per_pixel == 3;
260 channels = (fmt->bits[0] ? 1 : 0) + (fmt->bits[1] ? 1 : 0)
261 + (fmt->bits[2] ? 1 : 0) + (fmt->bits[3] ? 1 : 0);
262 usedformat = D3DFMT_UNKNOWN;
264 while ((curfmt = get_format_info_idx(i)))
266 unsigned int curchannels = (curfmt->bits[0] ? 1 : 0) + (curfmt->bits[1] ? 1 : 0)
267 + (curfmt->bits[2] ? 1 : 0) + (curfmt->bits[3] ? 1 : 0);
268 int score;
270 i++;
272 if (curchannels < channels)
273 continue;
274 if (curfmt->bytes_per_pixel == 3 && !allow_24bits)
275 continue;
277 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
278 mode.Format, usage, D3DRTYPE_TEXTURE, curfmt->format);
279 if (FAILED(hr))
280 continue;
282 /* This format can be used, let's evaluate it.
283 Weights chosen quite arbitrarily... */
284 score = 16 - 4 * (curchannels - channels);
286 for (j = 0; j < 4; j++)
288 int diff = curfmt->bits[j] - fmt->bits[j];
289 score += 16 - (diff < 0 ? -diff * 4 : diff);
292 if (score > bestscore)
294 bestscore = score;
295 usedformat = curfmt->format;
296 bestfmt = curfmt;
299 fmt = bestfmt;
300 hr = D3D_OK;
303 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
304 return D3DERR_INVALIDCALL;
306 if ((w == D3DX_DEFAULT) && (h == D3DX_DEFAULT))
307 w = h = 256;
308 else if (w == D3DX_DEFAULT)
309 w = (height ? h : 256);
310 else if (h == D3DX_DEFAULT)
311 h = (width ? w : 256);
313 if (fmt->block_width != 1 || fmt->block_height != 1)
315 if (w < fmt->block_width)
316 w = fmt->block_width;
317 if (h < fmt->block_height)
318 h = fmt->block_height;
321 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(w)))
322 w = make_pow2(w);
324 if (w > caps.MaxTextureWidth)
325 w = caps.MaxTextureWidth;
327 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(h)))
328 h = make_pow2(h);
330 if (h > caps.MaxTextureHeight)
331 h = caps.MaxTextureHeight;
333 if (caps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
335 if (w > h)
336 h = w;
337 else
338 w = h;
341 if (width)
342 *width = w;
344 if (height)
345 *height = h;
347 if (miplevels && (usage & D3DUSAGE_AUTOGENMIPMAP))
349 if (*miplevels > 1)
350 *miplevels = 0;
352 else if (miplevels)
354 UINT max_mipmaps = 1;
356 if (!width && !height)
357 max_mipmaps = 9; /* number of mipmaps in a 256x256 texture */
358 else
360 UINT max_dimen = max(w, h);
362 while (max_dimen > 1)
364 max_dimen >>= 1;
365 max_mipmaps++;
369 if (*miplevels == 0 || *miplevels > max_mipmaps)
370 *miplevels = max_mipmaps;
373 cleanup:
375 if (d3d)
376 IDirect3D9_Release(d3d);
378 if (FAILED(hr))
379 return hr;
381 if (usedformat == D3DFMT_UNKNOWN)
383 WARN("Couldn't find a suitable pixel format\n");
384 return D3DERR_NOTAVAILABLE;
387 TRACE("Format chosen: %x\n", usedformat);
388 if (format)
389 *format = usedformat;
391 return D3D_OK;
394 HRESULT WINAPI D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9 *device, UINT *size,
395 UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
397 D3DCAPS9 caps;
398 UINT s = (size && *size) ? *size : 256;
399 HRESULT hr;
401 TRACE("(%p, %p, %p, %u, %p, %u)\n", device, size, miplevels, usage, format, pool);
403 if (s == D3DX_DEFAULT)
404 s = 256;
406 if (!device || FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
407 return D3DERR_INVALIDCALL;
409 if (!(caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP))
410 return D3DERR_NOTAVAILABLE;
412 /* ensure width/height is power of 2 */
413 if ((caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && (!is_pow2(s)))
414 s = make_pow2(s);
416 hr = D3DXCheckTextureRequirements(device, &s, &s, miplevels, usage, format, pool);
418 if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
420 if(miplevels)
421 *miplevels = 1;
424 if (size)
425 *size = s;
427 return hr;
430 HRESULT WINAPI D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9 *device, UINT *width, UINT *height,
431 UINT *depth, UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
433 D3DCAPS9 caps;
434 UINT w = width ? *width : D3DX_DEFAULT;
435 UINT h = height ? *height : D3DX_DEFAULT;
436 UINT d = (depth && *depth) ? *depth : 1;
437 HRESULT hr;
439 TRACE("(%p, %p, %p, %p, %p, %u, %p, %u)\n", device, width, height, depth, miplevels,
440 usage, format, pool);
442 if (!device || FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
443 return D3DERR_INVALIDCALL;
445 if (!(caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP))
446 return D3DERR_NOTAVAILABLE;
448 hr = D3DXCheckTextureRequirements(device, &w, &h, NULL, usage, format, pool);
449 if (d == D3DX_DEFAULT)
450 d = 1;
452 /* ensure width/height is power of 2 */
453 if ((caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2) &&
454 (!is_pow2(w) || !is_pow2(h) || !is_pow2(d)))
456 w = make_pow2(w);
457 h = make_pow2(h);
458 d = make_pow2(d);
461 if (w > caps.MaxVolumeExtent)
462 w = caps.MaxVolumeExtent;
463 if (h > caps.MaxVolumeExtent)
464 h = caps.MaxVolumeExtent;
465 if (d > caps.MaxVolumeExtent)
466 d = caps.MaxVolumeExtent;
468 if (miplevels)
470 if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
471 *miplevels = 1;
472 else if ((usage & D3DUSAGE_AUTOGENMIPMAP))
474 if (*miplevels > 1)
475 *miplevels = 0;
477 else
479 UINT max_mipmaps = 1;
480 UINT max_dimen = max(max(w, h), d);
482 while (max_dimen > 1)
484 max_dimen >>= 1;
485 max_mipmaps++;
488 if (*miplevels == 0 || *miplevels > max_mipmaps)
489 *miplevels = max_mipmaps;
493 if (width)
494 *width = w;
495 if (height)
496 *height = h;
497 if (depth)
498 *depth = d;
500 return hr;
503 HRESULT WINAPI D3DXCreateTexture(struct IDirect3DDevice9 *device, UINT width, UINT height,
504 UINT miplevels, DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DTexture9 **texture)
506 HRESULT hr;
508 TRACE("device %p, width %u, height %u, miplevels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
509 device, width, height, miplevels, usage, format, pool, texture);
511 if (!device || !texture)
512 return D3DERR_INVALIDCALL;
514 if (FAILED(hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool)))
515 return hr;
517 return IDirect3DDevice9_CreateTexture(device, width, height, miplevels, usage, format, pool, texture, NULL);
520 HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9 *device, const void *srcdata,
521 UINT srcdatasize, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
522 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
523 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
525 IDirect3DTexture9 **texptr;
526 IDirect3DTexture9 *buftex;
527 IDirect3DSurface9 *surface;
528 BOOL file_width = FALSE, file_height = FALSE;
529 BOOL file_format = FALSE, file_miplevels = FALSE;
530 BOOL dynamic_texture;
531 D3DXIMAGE_INFO imginfo;
532 UINT loaded_miplevels, skip_levels;
533 D3DCAPS9 caps;
534 HRESULT hr;
536 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u,"
537 " usage %#x, format %#x, pool %#x, filter %#x, mipfilter %#x, colorkey %#x,"
538 " srcinfo %p, palette %p, texture %p.\n",
539 device, srcdata, srcdatasize, width, height, miplevels, usage, format, pool,
540 filter, mipfilter, colorkey, srcinfo, palette, texture);
542 /* check for invalid parameters */
543 if (!device || !texture || !srcdata || !srcdatasize)
544 return D3DERR_INVALIDCALL;
546 hr = D3DXGetImageInfoFromFileInMemory(srcdata, srcdatasize, &imginfo);
547 if (FAILED(hr))
549 FIXME("Unrecognized file format, returning failure.\n");
550 *texture = NULL;
551 return hr;
554 /* handle default values */
555 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
556 width = imginfo.Width;
558 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
559 height = imginfo.Height;
561 if (width == D3DX_DEFAULT)
562 width = make_pow2(imginfo.Width);
564 if (height == D3DX_DEFAULT)
565 height = make_pow2(imginfo.Height);
567 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
568 format = imginfo.Format;
570 if (width == D3DX_FROM_FILE)
572 file_width = TRUE;
573 width = imginfo.Width;
576 if (height == D3DX_FROM_FILE)
578 file_height = TRUE;
579 height = imginfo.Height;
582 if (format == D3DFMT_FROM_FILE)
584 file_format = TRUE;
585 format = imginfo.Format;
588 if (miplevels == D3DX_FROM_FILE)
590 file_miplevels = TRUE;
591 miplevels = imginfo.MipLevels;
594 skip_levels = mipfilter != D3DX_DEFAULT ? mipfilter >> D3DX_SKIP_DDS_MIP_LEVELS_SHIFT : 0;
595 if (skip_levels && imginfo.MipLevels > skip_levels)
597 TRACE("Skipping the first %u (of %u) levels of a DDS mipmapped texture.\n",
598 skip_levels, imginfo.MipLevels);
599 TRACE("Texture level 0 dimensions are %ux%u.\n", imginfo.Width, imginfo.Height);
600 width >>= skip_levels;
601 height >>= skip_levels;
602 miplevels -= skip_levels;
604 else
606 skip_levels = 0;
609 /* fix texture creation parameters */
610 hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool);
611 if (FAILED(hr))
613 FIXME("Couldn't find suitable texture parameters.\n");
614 *texture = NULL;
615 return hr;
618 if (imginfo.MipLevels < miplevels && (D3DFMT_DXT1 <= imginfo.Format && imginfo.Format <= D3DFMT_DXT5))
620 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet\n");
621 miplevels = imginfo.MipLevels;
623 if (imginfo.ResourceType == D3DRTYPE_VOLUMETEXTURE
624 && D3DFMT_DXT1 <= imginfo.Format && imginfo.Format <= D3DFMT_DXT5 && miplevels > 1)
626 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet.\n");
627 miplevels = 1;
630 if (((file_width) && (width != imginfo.Width)) ||
631 ((file_height) && (height != imginfo.Height)) ||
632 ((file_format) && (format != imginfo.Format)) ||
633 ((file_miplevels) && (miplevels != imginfo.MipLevels)))
635 return D3DERR_NOTAVAILABLE;
638 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
639 return D3DERR_INVALIDCALL;
641 /* Create the to-be-filled texture */
642 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
643 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
645 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
646 texptr = &buftex;
648 else
650 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
651 texptr = texture;
654 if (FAILED(hr))
656 FIXME("Texture creation failed.\n");
657 *texture = NULL;
658 return hr;
661 TRACE("Texture created correctly. Now loading the texture data into it.\n");
662 if (imginfo.ImageFileFormat != D3DXIFF_DDS)
664 IDirect3DTexture9_GetSurfaceLevel(*texptr, 0, &surface);
665 hr = D3DXLoadSurfaceFromFileInMemory(surface, palette, NULL, srcdata, srcdatasize, NULL, filter, colorkey, NULL);
666 IDirect3DSurface9_Release(surface);
667 loaded_miplevels = min(IDirect3DTexture9_GetLevelCount(*texptr), imginfo.MipLevels);
669 else
671 hr = load_texture_from_dds(*texptr, srcdata, palette, filter, colorkey, &imginfo, skip_levels,
672 &loaded_miplevels);
675 if (FAILED(hr))
677 FIXME("Texture loading failed.\n");
678 IDirect3DTexture9_Release(*texptr);
679 *texture = NULL;
680 return hr;
683 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) *texptr, palette, loaded_miplevels - 1, mipfilter);
684 if (FAILED(hr))
686 FIXME("Texture filtering failed.\n");
687 IDirect3DTexture9_Release(*texptr);
688 *texture = NULL;
689 return hr;
692 /* Move the data to the actual texture if necessary */
693 if (texptr == &buftex)
695 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
697 if (FAILED(hr))
699 IDirect3DTexture9_Release(buftex);
700 *texture = NULL;
701 return hr;
704 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9*)buftex, (IDirect3DBaseTexture9*)(*texture));
705 IDirect3DTexture9_Release(buftex);
708 if (srcinfo)
709 *srcinfo = imginfo;
711 return D3D_OK;
714 HRESULT WINAPI D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9 *device,
715 const void *srcdata, UINT srcdatasize, struct IDirect3DTexture9 **texture)
717 TRACE("(%p, %p, %d, %p)\n", device, srcdata, srcdatasize, texture);
719 return D3DXCreateTextureFromFileInMemoryEx(device, srcdata, srcdatasize, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
720 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
723 HRESULT WINAPI D3DXCreateTextureFromFileExW(struct IDirect3DDevice9 *device, const WCHAR *srcfile,
724 UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
725 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
726 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
728 void *buffer;
729 HRESULT hr;
730 DWORD size;
732 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
733 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
734 device, debugstr_w(srcfile), width, height, miplevels, usage, format,
735 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
737 if (!srcfile)
738 return D3DERR_INVALIDCALL;
740 hr = map_view_of_file(srcfile, &buffer, &size);
741 if (FAILED(hr))
742 return D3DXERR_INVALIDDATA;
744 hr = D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels, usage, format, pool,
745 filter, mipfilter, colorkey, srcinfo, palette, texture);
747 UnmapViewOfFile(buffer);
749 return hr;
752 HRESULT WINAPI D3DXCreateTextureFromFileExA(struct IDirect3DDevice9 *device, const char *srcfile,
753 UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
754 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
755 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
757 WCHAR *widename;
758 HRESULT hr;
759 DWORD len;
761 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
762 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
763 device, debugstr_a(srcfile), width, height, miplevels, usage, format,
764 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
766 if (!device || !srcfile || !texture)
767 return D3DERR_INVALIDCALL;
769 len = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
770 widename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*widename));
771 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, widename, len);
773 hr = D3DXCreateTextureFromFileExW(device, widename, width, height, miplevels,
774 usage, format, pool, filter, mipfilter,
775 colorkey, srcinfo, palette, texture);
777 HeapFree(GetProcessHeap(), 0, widename);
778 return hr;
781 HRESULT WINAPI D3DXCreateTextureFromFileA(struct IDirect3DDevice9 *device,
782 const char *srcfile, struct IDirect3DTexture9 **texture)
784 TRACE("(%p, %s, %p)\n", device, debugstr_a(srcfile), texture);
786 return D3DXCreateTextureFromFileExA(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
787 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
790 HRESULT WINAPI D3DXCreateTextureFromFileW(struct IDirect3DDevice9 *device,
791 const WCHAR *srcfile, struct IDirect3DTexture9 **texture)
793 TRACE("(%p, %s, %p)\n", device, debugstr_w(srcfile), texture);
795 return D3DXCreateTextureFromFileExW(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
796 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
800 HRESULT WINAPI D3DXCreateTextureFromResourceA(struct IDirect3DDevice9 *device,
801 HMODULE srcmodule, const char *resource, struct IDirect3DTexture9 **texture)
803 TRACE("(%p, %s): relay\n", srcmodule, debugstr_a(resource));
805 return D3DXCreateTextureFromResourceExA(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
806 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
809 HRESULT WINAPI D3DXCreateTextureFromResourceW(struct IDirect3DDevice9 *device,
810 HMODULE srcmodule, const WCHAR *resource, struct IDirect3DTexture9 **texture)
812 TRACE("(%p, %s): relay\n", srcmodule, debugstr_w(resource));
814 return D3DXCreateTextureFromResourceExW(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
815 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
818 HRESULT WINAPI D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9 *device, HMODULE srcmodule,
819 const char *resource, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
820 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
821 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
823 HRSRC resinfo;
824 void *buffer;
825 DWORD size;
827 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
828 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
829 device, srcmodule, debugstr_a(resource), width, height, miplevels, usage, format,
830 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
832 if (!device || !texture)
833 return D3DERR_INVALIDCALL;
835 if (!(resinfo = FindResourceA(srcmodule, resource, (const char *)RT_RCDATA))
836 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
837 && !(resinfo = FindResourceA(srcmodule, resource, (const char *)RT_BITMAP)))
838 return D3DXERR_INVALIDDATA;
840 if (FAILED(load_resource_into_memory(srcmodule, resinfo, &buffer, &size)))
841 return D3DXERR_INVALIDDATA;
843 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels,
844 usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
847 HRESULT WINAPI D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9 *device, HMODULE srcmodule,
848 const WCHAR *resource, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
849 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
850 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
852 HRSRC resinfo;
853 void *buffer;
854 DWORD size;
856 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
857 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
858 device, srcmodule, debugstr_w(resource), width, height, miplevels, usage, format,
859 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
861 if (!device || !texture)
862 return D3DERR_INVALIDCALL;
864 if (!(resinfo = FindResourceW(srcmodule, resource, (const WCHAR *)RT_RCDATA))
865 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
866 && !(resinfo = FindResourceW(srcmodule, resource, (const WCHAR *)RT_BITMAP)))
867 return D3DXERR_INVALIDDATA;
869 if (FAILED(load_resource_into_memory(srcmodule, resinfo, &buffer, &size)))
870 return D3DXERR_INVALIDDATA;
872 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels,
873 usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
876 HRESULT WINAPI D3DXCreateCubeTexture(struct IDirect3DDevice9 *device, UINT size, UINT miplevels,
877 DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DCubeTexture9 **texture)
879 HRESULT hr;
881 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device, size, miplevels, usage, format,
882 pool, texture);
884 if (!device || !texture)
885 return D3DERR_INVALIDCALL;
887 hr = D3DXCheckCubeTextureRequirements(device, &size, &miplevels, usage, &format, pool);
889 if (FAILED(hr))
891 TRACE("D3DXCheckCubeTextureRequirements failed\n");
892 return hr;
895 return IDirect3DDevice9_CreateCubeTexture(device, size, miplevels, usage, format, pool, texture, NULL);
898 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9 *device,
899 const void *data, UINT datasize, struct IDirect3DCubeTexture9 **texture)
901 TRACE("(%p, %p, %u, %p)\n", device, data, datasize, texture);
903 return D3DXCreateCubeTextureFromFileInMemoryEx(device, data, datasize, D3DX_DEFAULT, D3DX_DEFAULT,
904 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
907 HRESULT WINAPI D3DXCreateVolumeTexture(struct IDirect3DDevice9 *device, UINT width, UINT height, UINT depth,
908 UINT miplevels, DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DVolumeTexture9 **texture)
910 HRESULT hr;
912 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device, width, height, depth,
913 miplevels, usage, format, pool, texture);
915 if (!device || !texture)
916 return D3DERR_INVALIDCALL;
918 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth,
919 &miplevels, usage, &format, pool);
921 if (FAILED(hr))
923 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
924 return hr;
927 return IDirect3DDevice9_CreateVolumeTexture(device, width, height, depth, miplevels,
928 usage, format, pool, texture, NULL);
931 HRESULT WINAPI D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9 *device,
932 const char *filename,
933 IDirect3DVolumeTexture9 **volume_texture)
935 int len;
936 HRESULT hr;
937 void *data;
938 DWORD data_size;
939 WCHAR *filenameW;
941 TRACE("(%p, %s, %p): relay\n",
942 device, debugstr_a(filename), volume_texture);
944 if (!filename) return D3DERR_INVALIDCALL;
946 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
947 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
948 if (!filenameW) return E_OUTOFMEMORY;
949 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
951 hr = map_view_of_file(filenameW, &data, &data_size);
952 HeapFree(GetProcessHeap(), 0, filenameW);
953 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
955 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
956 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
958 UnmapViewOfFile(data);
959 return hr;
962 HRESULT WINAPI D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9 *device,
963 const WCHAR *filename,
964 IDirect3DVolumeTexture9 **volume_texture)
966 HRESULT hr;
967 void *data;
968 DWORD data_size;
970 TRACE("(%p, %s, %p): relay\n",
971 device, debugstr_w(filename), volume_texture);
973 if (!filename) return D3DERR_INVALIDCALL;
975 hr = map_view_of_file(filename, &data, &data_size);
976 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
978 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
979 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
981 UnmapViewOfFile(data);
982 return hr;
985 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9 *device,
986 const char *filename,
987 UINT width,
988 UINT height,
989 UINT depth,
990 UINT mip_levels,
991 DWORD usage,
992 D3DFORMAT format,
993 D3DPOOL pool,
994 DWORD filter,
995 DWORD mip_filter,
996 D3DCOLOR color_key,
997 D3DXIMAGE_INFO *src_info,
998 PALETTEENTRY *palette,
999 IDirect3DVolumeTexture9 **volume_texture)
1001 int len;
1002 HRESULT hr;
1003 WCHAR *filenameW;
1004 void *data;
1005 DWORD data_size;
1007 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1008 device, debugstr_a(filename), width, height, depth, mip_levels,
1009 usage, format, pool, filter, mip_filter, color_key, src_info,
1010 palette, volume_texture);
1012 if (!filename) return D3DERR_INVALIDCALL;
1014 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
1015 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1016 if (!filenameW) return E_OUTOFMEMORY;
1017 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
1019 hr = map_view_of_file(filenameW, &data, &data_size);
1020 HeapFree(GetProcessHeap(), 0, filenameW);
1021 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1023 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1024 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1025 volume_texture);
1027 UnmapViewOfFile(data);
1028 return hr;
1031 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9 *device,
1032 const WCHAR *filename,
1033 UINT width,
1034 UINT height,
1035 UINT depth,
1036 UINT mip_levels,
1037 DWORD usage,
1038 D3DFORMAT format,
1039 D3DPOOL pool,
1040 DWORD filter,
1041 DWORD mip_filter,
1042 D3DCOLOR color_key,
1043 D3DXIMAGE_INFO *src_info,
1044 PALETTEENTRY *palette,
1045 IDirect3DVolumeTexture9 **volume_texture)
1047 HRESULT hr;
1048 void *data;
1049 DWORD data_size;
1051 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1052 device, debugstr_w(filename), width, height, depth, mip_levels,
1053 usage, format, pool, filter, mip_filter, color_key, src_info,
1054 palette, volume_texture);
1056 if (!filename) return D3DERR_INVALIDCALL;
1058 hr = map_view_of_file(filename, &data, &data_size);
1059 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1061 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1062 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1063 volume_texture);
1065 UnmapViewOfFile(data);
1066 return hr;
1069 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9 *device,
1070 const void *data,
1071 UINT data_size,
1072 IDirect3DVolumeTexture9 **volume_texture)
1074 TRACE("(%p, %p, %u, %p): relay\n", device, data, data_size, volume_texture);
1076 return D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1077 D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
1078 0, NULL, NULL, volume_texture);
1081 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
1082 const void *data,
1083 UINT data_size,
1084 UINT width,
1085 UINT height,
1086 UINT depth,
1087 UINT mip_levels,
1088 DWORD usage,
1089 D3DFORMAT format,
1090 D3DPOOL pool,
1091 DWORD filter,
1092 DWORD mip_filter,
1093 D3DCOLOR color_key,
1094 D3DXIMAGE_INFO *info,
1095 PALETTEENTRY *palette,
1096 IDirect3DVolumeTexture9 **volume_texture)
1098 HRESULT hr;
1099 D3DCAPS9 caps;
1100 D3DXIMAGE_INFO image_info;
1101 BOOL dynamic_texture;
1102 BOOL file_width = FALSE;
1103 BOOL file_height = FALSE;
1104 BOOL file_depth = FALSE;
1105 BOOL file_format = FALSE;
1106 BOOL file_mip_levels = FALSE;
1107 IDirect3DVolumeTexture9 *tex, *buftex;
1109 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1110 device, data, data_size, width, height, depth, mip_levels, usage, format, pool,
1111 filter, mip_filter, color_key, info, palette, volume_texture);
1113 if (!device || !data || !data_size || !volume_texture)
1114 return D3DERR_INVALIDCALL;
1116 hr = D3DXGetImageInfoFromFileInMemory(data, data_size, &image_info);
1117 if (FAILED(hr)) return hr;
1119 if (image_info.ImageFileFormat != D3DXIFF_DDS)
1120 return D3DXERR_INVALIDDATA;
1122 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
1123 width = image_info.Width;
1124 if (width == D3DX_DEFAULT)
1125 width = make_pow2(image_info.Width);
1127 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
1128 height = image_info.Height;
1129 if (height == D3DX_DEFAULT)
1130 height = make_pow2(image_info.Height);
1132 if (depth == 0 || depth == D3DX_DEFAULT_NONPOW2)
1133 depth = image_info.Depth;
1134 if (depth == D3DX_DEFAULT)
1135 depth = make_pow2(image_info.Depth);
1137 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1138 format = image_info.Format;
1140 if (width == D3DX_FROM_FILE)
1142 file_width = TRUE;
1143 width = image_info.Width;
1146 if (height == D3DX_FROM_FILE)
1148 file_height = TRUE;
1149 height = image_info.Height;
1152 if (depth == D3DX_FROM_FILE)
1154 file_depth = TRUE;
1155 depth = image_info.Depth;
1158 if (format == D3DFMT_FROM_FILE)
1160 file_format = TRUE;
1161 format = image_info.Format;
1164 if (mip_levels == D3DX_FROM_FILE)
1166 file_mip_levels = TRUE;
1167 mip_levels = image_info.MipLevels;
1170 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, &mip_levels, usage, &format, pool);
1171 if (FAILED(hr)) return hr;
1173 if ((file_width && width != image_info.Width)
1174 || (file_height && height != image_info.Height)
1175 || (file_depth && depth != image_info.Depth)
1176 || (file_format && format != image_info.Format)
1177 || (file_mip_levels && mip_levels != image_info.MipLevels))
1178 return D3DERR_NOTAVAILABLE;
1180 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1181 if (FAILED(hr))
1182 return D3DERR_INVALIDCALL;
1184 if (mip_levels > image_info.MipLevels)
1186 FIXME("Generation of mipmaps for volume textures is not implemented yet\n");
1187 mip_levels = image_info.MipLevels;
1190 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1191 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1193 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
1194 tex = buftex;
1196 else
1198 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1199 buftex = NULL;
1202 if (FAILED(hr)) return hr;
1204 hr = load_volume_texture_from_dds(tex, data, palette, filter, color_key, &image_info);
1205 if (FAILED(hr))
1207 IDirect3DVolumeTexture9_Release(tex);
1208 return hr;
1211 if (buftex)
1213 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1214 if (FAILED(hr))
1216 IDirect3DVolumeTexture9_Release(buftex);
1217 return hr;
1220 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1221 IDirect3DVolumeTexture9_Release(buftex);
1224 if (info)
1225 *info = image_info;
1227 *volume_texture = tex;
1228 return D3D_OK;
1231 static inline void fill_texture(const struct pixel_format_desc *format, BYTE *pos, const D3DXVECTOR4 *value)
1233 DWORD c;
1235 for (c = 0; c < format->bytes_per_pixel; c++)
1236 pos[c] = 0;
1238 for (c = 0; c < 4; c++)
1240 float comp_value;
1241 DWORD i, v = 0, mask32 = format->bits[c] == 32 ? ~0U : ((1 << format->bits[c]) - 1);
1243 switch (c)
1245 case 0: /* Alpha */
1246 comp_value = value->w;
1247 break;
1248 case 1: /* Red */
1249 comp_value = value->x;
1250 break;
1251 case 2: /* Green */
1252 comp_value = value->y;
1253 break;
1254 case 3: /* Blue */
1255 comp_value = value->z;
1256 break;
1259 if (format->type == FORMAT_ARGBF16)
1260 v = float_32_to_16(comp_value);
1261 else if (format->type == FORMAT_ARGBF)
1262 v = *(DWORD *)&comp_value;
1263 else if (format->type == FORMAT_ARGB)
1264 v = comp_value * ((1 << format->bits[c]) - 1) + 0.5f;
1265 else
1266 FIXME("Unhandled format type %#x\n", format->type);
1268 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1270 BYTE byte, mask;
1272 if (format->shift[c] > i)
1274 mask = mask32 << (format->shift[c] - i);
1275 byte = (v << (format->shift[c] - i)) & mask;
1277 else
1279 mask = mask32 >> (i - format->shift[c]);
1280 byte = (v >> (i - format->shift[c])) & mask;
1282 pos[i / 8] |= byte;
1287 HRESULT WINAPI D3DXFillTexture(struct IDirect3DTexture9 *texture, LPD3DXFILL2D function, void *funcdata)
1289 DWORD miplevels;
1290 DWORD m, x, y;
1291 D3DSURFACE_DESC desc;
1292 D3DLOCKED_RECT lock_rect;
1293 D3DXVECTOR4 value;
1294 D3DXVECTOR2 coord, size;
1295 const struct pixel_format_desc *format;
1296 BYTE *data;
1298 if (texture == NULL || function == NULL)
1299 return D3DERR_INVALIDCALL;
1301 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1303 for (m = 0; m < miplevels; m++)
1305 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture, m, &desc)))
1306 return D3DERR_INVALIDCALL;
1308 format = get_format_info(desc.Format);
1309 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1311 FIXME("Unsupported texture format %#x\n", desc.Format);
1312 return D3DERR_INVALIDCALL;
1315 if (FAILED(IDirect3DTexture9_LockRect(texture, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1316 return D3DERR_INVALIDCALL;
1318 size.x = 1.0f / desc.Width;
1319 size.y = 1.0f / desc.Height;
1321 data = lock_rect.pBits;
1323 for (y = 0; y < desc.Height; y++)
1325 /* The callback function expects the coordinates of the center
1326 of the texel */
1327 coord.y = (y + 0.5f) / desc.Height;
1329 for (x = 0; x < desc.Width; x++)
1331 coord.x = (x + 0.5f) / desc.Width;
1333 function(&value, &coord, &size, funcdata);
1335 fill_texture(format, data + y * lock_rect.Pitch + x * format->bytes_per_pixel, &value);
1338 IDirect3DTexture9_UnlockRect(texture, m);
1341 return D3D_OK;
1344 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
1345 const void *src_data,
1346 UINT src_data_size,
1347 UINT size,
1348 UINT mip_levels,
1349 DWORD usage,
1350 D3DFORMAT format,
1351 D3DPOOL pool,
1352 DWORD filter,
1353 DWORD mip_filter,
1354 D3DCOLOR color_key,
1355 D3DXIMAGE_INFO *src_info,
1356 PALETTEENTRY *palette,
1357 IDirect3DCubeTexture9 **cube_texture)
1359 HRESULT hr;
1360 D3DCAPS9 caps;
1361 UINT loaded_miplevels;
1362 D3DXIMAGE_INFO img_info;
1363 BOOL dynamic_texture;
1364 BOOL file_size = FALSE;
1365 BOOL file_format = FALSE;
1366 BOOL file_mip_levels = FALSE;
1367 IDirect3DCubeTexture9 *tex, *buftex;
1369 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device,
1370 src_data, src_data_size, size, mip_levels, usage, format, pool, filter, mip_filter,
1371 color_key, src_info, palette, cube_texture);
1373 if (!device || !cube_texture || !src_data || !src_data_size)
1374 return D3DERR_INVALIDCALL;
1376 hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &img_info);
1377 if (FAILED(hr))
1378 return hr;
1380 if (img_info.ImageFileFormat != D3DXIFF_DDS)
1381 return D3DXERR_INVALIDDATA;
1383 if (img_info.Width != img_info.Height)
1384 return D3DXERR_INVALIDDATA;
1386 if (size == 0 || size == D3DX_DEFAULT_NONPOW2)
1387 size = img_info.Width;
1388 if (size == D3DX_DEFAULT)
1389 size = make_pow2(img_info.Width);
1391 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1392 format = img_info.Format;
1394 if (size == D3DX_FROM_FILE)
1396 file_size = TRUE;
1397 size = img_info.Width;
1400 if (format == D3DFMT_FROM_FILE)
1402 file_format = TRUE;
1403 format = img_info.Format;
1406 if (mip_levels == D3DX_FROM_FILE)
1408 file_mip_levels = TRUE;
1409 mip_levels = img_info.MipLevels;
1412 hr = D3DXCheckCubeTextureRequirements(device, &size, &mip_levels, usage, &format, pool);
1413 if (FAILED(hr))
1414 return hr;
1416 if ((file_size && size != img_info.Width)
1417 || (file_format && format != img_info.Format)
1418 || (file_mip_levels && mip_levels != img_info.MipLevels))
1419 return D3DERR_NOTAVAILABLE;
1421 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1422 if (FAILED(hr))
1423 return D3DERR_INVALIDCALL;
1425 if (mip_levels > img_info.MipLevels && (D3DFMT_DXT1 <= img_info.Format && img_info.Format <= D3DFMT_DXT5))
1427 FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
1428 mip_levels = img_info.MipLevels;
1431 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1432 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1434 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
1435 tex = buftex;
1437 else
1439 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1440 buftex = NULL;
1442 if (FAILED(hr))
1443 return hr;
1445 hr = load_cube_texture_from_dds(tex, src_data, palette, filter, color_key, &img_info);
1446 if (FAILED(hr))
1448 IDirect3DCubeTexture9_Release(tex);
1449 return hr;
1452 loaded_miplevels = min(IDirect3DCubeTexture9_GetLevelCount(tex), img_info.MipLevels);
1453 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, palette, loaded_miplevels - 1, mip_filter);
1454 if (FAILED(hr))
1456 IDirect3DCubeTexture9_Release(tex);
1457 return hr;
1460 if (buftex)
1462 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1463 if (FAILED(hr))
1465 IDirect3DCubeTexture9_Release(buftex);
1466 return hr;
1469 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1470 IDirect3DCubeTexture9_Release(buftex);
1473 if (src_info)
1474 *src_info = img_info;
1476 *cube_texture = tex;
1477 return D3D_OK;
1481 HRESULT WINAPI D3DXCreateCubeTextureFromFileA(IDirect3DDevice9 *device,
1482 const char *src_filename,
1483 IDirect3DCubeTexture9 **cube_texture)
1485 int len;
1486 HRESULT hr;
1487 WCHAR *filename;
1488 void *data;
1489 DWORD data_size;
1491 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_a(src_filename), cube_texture);
1493 if (!src_filename) return D3DERR_INVALIDCALL;
1495 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1496 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1497 if (!filename) return E_OUTOFMEMORY;
1498 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1500 hr = map_view_of_file(filename, &data, &data_size);
1501 if (FAILED(hr))
1503 HeapFree(GetProcessHeap(), 0, filename);
1504 return D3DXERR_INVALIDDATA;
1507 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1508 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1510 UnmapViewOfFile(data);
1511 HeapFree(GetProcessHeap(), 0, filename);
1512 return hr;
1515 HRESULT WINAPI D3DXCreateCubeTextureFromFileW(IDirect3DDevice9 *device,
1516 const WCHAR *src_filename,
1517 IDirect3DCubeTexture9 **cube_texture)
1519 HRESULT hr;
1520 void *data;
1521 DWORD data_size;
1523 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_w(src_filename), cube_texture);
1525 hr = map_view_of_file(src_filename, &data, &data_size);
1526 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1528 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1529 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1531 UnmapViewOfFile(data);
1532 return hr;
1535 HRESULT WINAPI D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9 *device,
1536 const char *src_filename,
1537 UINT size,
1538 UINT mip_levels,
1539 DWORD usage,
1540 D3DFORMAT format,
1541 D3DPOOL pool,
1542 DWORD filter,
1543 DWORD mip_filter,
1544 D3DCOLOR color_key,
1545 D3DXIMAGE_INFO *image_info,
1546 PALETTEENTRY *palette,
1547 IDirect3DCubeTexture9 **cube_texture)
1549 int len;
1550 HRESULT hr;
1551 WCHAR *filename;
1552 void *data;
1553 DWORD data_size;
1555 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1556 device, wine_dbgstr_a(src_filename), size, mip_levels, usage, format,
1557 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1559 if (!src_filename) return D3DERR_INVALIDCALL;
1561 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1562 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1563 if (!filename) return E_OUTOFMEMORY;
1564 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1566 hr = map_view_of_file(filename, &data, &data_size);
1567 if (FAILED(hr))
1569 HeapFree(GetProcessHeap(), 0, filename);
1570 return D3DXERR_INVALIDDATA;
1573 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1574 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1576 UnmapViewOfFile(data);
1577 HeapFree(GetProcessHeap(), 0, filename);
1578 return hr;
1581 HRESULT WINAPI D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9 *device,
1582 const WCHAR *src_filename,
1583 UINT size,
1584 UINT mip_levels,
1585 DWORD usage,
1586 D3DFORMAT format,
1587 D3DPOOL pool,
1588 DWORD filter,
1589 DWORD mip_filter,
1590 D3DCOLOR color_key,
1591 D3DXIMAGE_INFO *image_info,
1592 PALETTEENTRY *palette,
1593 IDirect3DCubeTexture9 **cube_texture)
1595 HRESULT hr;
1596 void *data;
1597 DWORD data_size;
1599 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1600 device, wine_dbgstr_w(src_filename), size, mip_levels, usage, format,
1601 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1603 hr = map_view_of_file(src_filename, &data, &data_size);
1604 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1606 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1607 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1609 UnmapViewOfFile(data);
1610 return hr;
1613 enum cube_coord
1615 XCOORD = 0,
1616 XCOORDINV = 1,
1617 YCOORD = 2,
1618 YCOORDINV = 3,
1619 ZERO = 4,
1620 ONE = 5
1623 static float get_cube_coord(enum cube_coord coord, unsigned int x, unsigned int y, unsigned int size)
1625 switch (coord)
1627 case XCOORD:
1628 return x + 0.5f;
1629 case XCOORDINV:
1630 return size - x - 0.5f;
1631 case YCOORD:
1632 return y + 0.5f;
1633 case YCOORDINV:
1634 return size - y - 0.5f;
1635 case ZERO:
1636 return 0.0f;
1637 case ONE:
1638 return size;
1639 default:
1640 ERR("Unexpected coordinate value\n");
1641 return 0.0f;
1645 HRESULT WINAPI D3DXFillCubeTexture(struct IDirect3DCubeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1647 DWORD miplevels;
1648 DWORD m, x, y, f;
1649 D3DSURFACE_DESC desc;
1650 D3DLOCKED_RECT lock_rect;
1651 D3DXVECTOR4 value;
1652 D3DXVECTOR3 coord, size;
1653 const struct pixel_format_desc *format;
1654 BYTE *data;
1655 static const enum cube_coord coordmap[6][3] =
1657 {ONE, YCOORDINV, XCOORDINV},
1658 {ZERO, YCOORDINV, XCOORD},
1659 {XCOORD, ONE, YCOORD},
1660 {XCOORD, ZERO, YCOORDINV},
1661 {XCOORD, YCOORDINV, ONE},
1662 {XCOORDINV, YCOORDINV, ZERO}
1665 if (texture == NULL || function == NULL)
1666 return D3DERR_INVALIDCALL;
1668 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1670 for (m = 0; m < miplevels; m++)
1672 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture, m, &desc)))
1673 return D3DERR_INVALIDCALL;
1675 format = get_format_info(desc.Format);
1676 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1678 FIXME("Unsupported texture format %#x\n", desc.Format);
1679 return D3DERR_INVALIDCALL;
1682 for (f = 0; f < 6; f++)
1684 if (FAILED(IDirect3DCubeTexture9_LockRect(texture, f, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1685 return D3DERR_INVALIDCALL;
1687 size.x = (f == 0) || (f == 1) ? 0.0f : 2.0f / desc.Width;
1688 size.y = (f == 2) || (f == 3) ? 0.0f : 2.0f / desc.Width;
1689 size.z = (f == 4) || (f == 5) ? 0.0f : 2.0f / desc.Width;
1691 data = lock_rect.pBits;
1693 for (y = 0; y < desc.Height; y++)
1695 for (x = 0; x < desc.Width; x++)
1697 coord.x = get_cube_coord(coordmap[f][0], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1698 coord.y = get_cube_coord(coordmap[f][1], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1699 coord.z = get_cube_coord(coordmap[f][2], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1701 function(&value, &coord, &size, funcdata);
1703 fill_texture(format, data + y * lock_rect.Pitch + x * format->bytes_per_pixel, &value);
1706 IDirect3DCubeTexture9_UnlockRect(texture, f, m);
1710 return D3D_OK;
1713 HRESULT WINAPI D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1715 DWORD miplevels;
1716 DWORD m, x, y, z;
1717 D3DVOLUME_DESC desc;
1718 D3DLOCKED_BOX lock_box;
1719 D3DXVECTOR4 value;
1720 D3DXVECTOR3 coord, size;
1721 const struct pixel_format_desc *format;
1722 BYTE *data;
1724 if (texture == NULL || function == NULL)
1725 return D3DERR_INVALIDCALL;
1727 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1729 for (m = 0; m < miplevels; m++)
1731 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture, m, &desc)))
1732 return D3DERR_INVALIDCALL;
1734 format = get_format_info(desc.Format);
1735 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1737 FIXME("Unsupported texture format %#x\n", desc.Format);
1738 return D3DERR_INVALIDCALL;
1741 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture, m, &lock_box, NULL, D3DLOCK_DISCARD)))
1742 return D3DERR_INVALIDCALL;
1744 size.x = 1.0f / desc.Width;
1745 size.y = 1.0f / desc.Height;
1746 size.z = 1.0f / desc.Depth;
1748 data = lock_box.pBits;
1750 for (z = 0; z < desc.Depth; z++)
1752 /* The callback function expects the coordinates of the center
1753 of the texel */
1754 coord.z = (z + 0.5f) / desc.Depth;
1756 for (y = 0; y < desc.Height; y++)
1758 coord.y = (y + 0.5f) / desc.Height;
1760 for (x = 0; x < desc.Width; x++)
1762 coord.x = (x + 0.5f) / desc.Width;
1764 function(&value, &coord, &size, funcdata);
1766 fill_texture(format, data + z * lock_box.SlicePitch + y * lock_box.RowPitch
1767 + x * format->bytes_per_pixel, &value);
1771 IDirect3DVolumeTexture9_UnlockBox(texture, m);
1774 return D3D_OK;
1777 HRESULT WINAPI D3DXSaveTextureToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1778 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1780 int len;
1781 WCHAR *filename;
1782 HRESULT hr;
1783 ID3DXBuffer *buffer;
1785 TRACE("(%s, %#x, %p, %p): relay\n",
1786 wine_dbgstr_a(dst_filename), file_format, src_texture, src_palette);
1788 if (!dst_filename) return D3DERR_INVALIDCALL;
1790 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1791 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1792 if (!filename) return E_OUTOFMEMORY;
1793 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1795 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1796 if (SUCCEEDED(hr))
1798 hr = write_buffer_to_file(filename, buffer);
1799 ID3DXBuffer_Release(buffer);
1802 HeapFree(GetProcessHeap(), 0, filename);
1803 return hr;
1806 HRESULT WINAPI D3DXSaveTextureToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1807 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1809 HRESULT hr;
1810 ID3DXBuffer *buffer;
1812 TRACE("(%s, %#x, %p, %p): relay\n",
1813 wine_dbgstr_w(dst_filename), file_format, src_texture, src_palette);
1815 if (!dst_filename) return D3DERR_INVALIDCALL;
1817 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1818 if (SUCCEEDED(hr))
1820 hr = write_buffer_to_file(dst_filename, buffer);
1821 ID3DXBuffer_Release(buffer);
1824 return hr;
1827 HRESULT WINAPI D3DXSaveTextureToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1828 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1830 HRESULT hr;
1831 D3DRESOURCETYPE type;
1832 IDirect3DSurface9 *surface;
1834 TRACE("(%p, %#x, %p, %p)\n",
1835 dst_buffer, file_format, src_texture, src_palette);
1837 if (!dst_buffer || !src_texture) return D3DERR_INVALIDCALL;
1839 if (file_format == D3DXIFF_DDS)
1841 FIXME("DDS file format isn't supported yet\n");
1842 return E_NOTIMPL;
1845 type = IDirect3DBaseTexture9_GetType(src_texture);
1846 switch (type)
1848 case D3DRTYPE_TEXTURE:
1849 case D3DRTYPE_CUBETEXTURE:
1850 hr = get_surface(type, src_texture, D3DCUBEMAP_FACE_POSITIVE_X, 0, &surface);
1851 break;
1852 case D3DRTYPE_VOLUMETEXTURE:
1853 FIXME("Volume textures aren't supported yet\n");
1854 return E_NOTIMPL;
1855 default:
1856 return D3DERR_INVALIDCALL;
1859 if (SUCCEEDED(hr))
1861 hr = D3DXSaveSurfaceToFileInMemory(dst_buffer, file_format, surface, src_palette, NULL);
1862 IDirect3DSurface9_Release(surface);
1865 return hr;