2 * Copyright 2009 Tony Wasserka
3 * Copyright 2010 Christian Costa
4 * Copyright 2010 Owen Rudge for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/unicode.h"
22 #include "wine/debug.h"
23 #include "d3dx9_36_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
27 /* Returns TRUE if num is a power of 2, FALSE if not, or if 0 */
28 static BOOL is_pow2(UINT num)
30 return !(num & (num - 1));
33 /* Returns the smallest power of 2 which is greater than or equal to num */
34 static UINT make_pow2(UINT num)
38 /* In the unlikely event somebody passes a large value, make sure we don't enter an infinite loop */
39 if (num >= 0x80000000)
48 HRESULT WINAPI D3DXFilterTexture(LPDIRECT3DBASETEXTURE9 texture,
49 CONST PALETTEENTRY *palette,
53 UINT level = srclevel + 1;
56 TRACE("(%p, %p, %d, %d)\n", texture, palette, srclevel, filter);
59 return D3DERR_INVALIDCALL;
61 if ((filter & 0xFFFF) > D3DX_FILTER_BOX && filter != D3DX_DEFAULT)
62 return D3DERR_INVALIDCALL;
64 if (srclevel >= IDirect3DBaseTexture9_GetLevelCount(texture))
65 return D3DERR_INVALIDCALL;
67 switch (IDirect3DBaseTexture9_GetType(texture))
69 case D3DRTYPE_TEXTURE:
71 IDirect3DSurface9 *topsurf, *mipsurf;
74 if (filter == D3DX_DEFAULT)
76 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9*) texture, srclevel, &desc);
78 if (is_pow2(desc.Width) && is_pow2(desc.Height))
79 filter = D3DX_FILTER_BOX;
81 filter = D3DX_FILTER_BOX | D3DX_FILTER_DITHER;
84 hr = IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9*) texture, srclevel, &topsurf);
87 return D3DERR_INVALIDCALL;
89 while (IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9*) texture, level, &mipsurf) == D3D_OK)
91 hr = D3DXLoadSurfaceFromSurface(mipsurf, palette, NULL, topsurf, palette, NULL, filter, 0);
92 IDirect3DSurface9_Release(mipsurf);
100 IDirect3DSurface9_Release(topsurf);
106 FIXME("Implement volume and cube texture filtering\n");
111 HRESULT WINAPI D3DXCheckTextureRequirements(LPDIRECT3DDEVICE9 device,
119 UINT w = (width && *width) ? *width : 1;
120 UINT h = (height && *height) ? *height : 1;
122 D3DDEVICE_CREATION_PARAMETERS params;
123 IDirect3D9 *d3d = NULL;
126 D3DFORMAT usedformat = D3DFMT_UNKNOWN;
128 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device, width, height, miplevels, usage, format, pool);
131 return D3DERR_INVALIDCALL;
134 if (usage == D3DX_DEFAULT)
136 if (usage & (D3DUSAGE_WRITEONLY | D3DUSAGE_DONOTCLIP | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES | D3DUSAGE_NPATCHES))
137 return D3DERR_INVALIDCALL;
140 if ((pool != D3DPOOL_DEFAULT) && (pool != D3DPOOL_MANAGED) && (pool != D3DPOOL_SYSTEMMEM) && (pool != D3DPOOL_SCRATCH))
141 return D3DERR_INVALIDCALL;
143 /* width and height */
144 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
145 return D3DERR_INVALIDCALL;
147 /* 256 x 256 default width/height */
148 if ((w == D3DX_DEFAULT) && (h == D3DX_DEFAULT))
150 else if (w == D3DX_DEFAULT)
151 w = (height ? h : 256);
152 else if (h == D3DX_DEFAULT)
153 h = (width ? w : 256);
155 /* ensure width/height is power of 2 */
156 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(w)))
159 if (w > caps.MaxTextureWidth)
160 w = caps.MaxTextureWidth;
162 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(h)))
165 if (h > caps.MaxTextureHeight)
166 h = caps.MaxTextureHeight;
168 /* texture must be square? */
169 if (caps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
186 UINT max_mipmaps = 1;
188 if (!width && !height)
189 max_mipmaps = 9; /* number of mipmaps in a 256x256 texture */
192 UINT max_dimen = max(w, h);
194 while (max_dimen > 1)
201 if (*miplevels == 0 || *miplevels > max_mipmaps)
202 *miplevels = max_mipmaps;
208 TRACE("Requested format %x\n", *format);
209 usedformat = *format;
212 hr = IDirect3DDevice9_GetDirect3D(device, &d3d);
217 hr = IDirect3DDevice9_GetCreationParameters(device, ¶ms);
222 hr = IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
227 if ((usedformat == D3DFMT_UNKNOWN) || (usedformat == D3DX_DEFAULT))
228 usedformat = D3DFMT_A8R8G8B8;
230 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType, mode.Format,
231 usage, D3DRTYPE_TEXTURE, usedformat);
235 /* Heuristic to choose the fallback format */
236 const PixelFormatDesc *fmt = get_format_info(usedformat);
238 int bestscore = INT_MIN, i = 0, j;
239 unsigned int channels;
240 const PixelFormatDesc *curfmt;
244 FIXME("Pixel format %x not handled\n", usedformat);
248 allow_24bits = fmt->bytes_per_pixel == 3;
249 channels = (fmt->bits[0] ? 1 : 0) + (fmt->bits[1] ? 1 : 0)
250 + (fmt->bits[2] ? 1 : 0) + (fmt->bits[3] ? 1 : 0);
251 usedformat = D3DFMT_UNKNOWN;
253 while ((curfmt = get_format_info_idx(i)))
255 unsigned int curchannels = (curfmt->bits[0] ? 1 : 0) + (curfmt->bits[1] ? 1 : 0)
256 + (curfmt->bits[2] ? 1 : 0) + (curfmt->bits[3] ? 1 : 0);
261 if (curchannels < channels)
263 if (curfmt->bytes_per_pixel == 3 && !allow_24bits)
266 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
267 mode.Format, usage, D3DRTYPE_TEXTURE, curfmt->format);
271 /* This format can be used, let's evaluate it.
272 Weights chosen quite arbitrarily... */
273 score = 16 - 4 * (curchannels - channels);
275 for (j = 0; j < 4; j++)
277 int diff = curfmt->bits[j] - fmt->bits[j];
278 score += 16 - (diff < 0 ? -diff * 4 : diff);
281 if (score > bestscore)
284 usedformat = curfmt->format;
293 IDirect3D9_Release(d3d);
298 if (usedformat == D3DFMT_UNKNOWN)
300 WARN("Couldn't find a suitable pixel format\n");
301 return D3DERR_NOTAVAILABLE;
304 TRACE("Format chosen: %x\n", usedformat);
306 *format = usedformat;
311 HRESULT WINAPI D3DXCreateTexture(LPDIRECT3DDEVICE9 pDevice,
318 LPDIRECT3DTEXTURE9 *ppTexture)
322 TRACE("(%p, %u, %u, %u, %x, %x, %x, %p)\n", pDevice, width, height, miplevels, usage, format,
325 if (!pDevice || !ppTexture)
326 return D3DERR_INVALIDCALL;
328 hr = D3DXCheckTextureRequirements(pDevice, &width, &height, &miplevels, usage, &format, pool);
333 return IDirect3DDevice9_CreateTexture(pDevice, width, height, miplevels, usage, format, pool, ppTexture, NULL);
336 HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(LPDIRECT3DDEVICE9 device,
348 D3DXIMAGE_INFO* srcinfo,
349 PALETTEENTRY* palette,
350 LPDIRECT3DTEXTURE9* texture)
352 IDirect3DTexture9 **texptr;
353 IDirect3DTexture9 *buftex;
354 IDirect3DSurface9 *surface;
355 BOOL file_width = FALSE, file_height = FALSE;
356 BOOL file_format = FALSE, file_miplevels = FALSE;
357 D3DXIMAGE_INFO imginfo;
361 TRACE("(%p, %p, %u, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p)\n", device, srcdata, srcdatasize, width,
362 height, miplevels, usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
364 /* check for invalid parameters */
365 if (!device || !texture || !srcdata || !srcdatasize)
366 return D3DERR_INVALIDCALL;
368 hr = D3DXGetImageInfoFromFileInMemory(srcdata, srcdatasize, &imginfo);
376 /* handle default values */
377 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
378 width = imginfo.Width;
380 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
381 height = imginfo.Height;
383 if (width == D3DX_DEFAULT)
384 width = make_pow2(imginfo.Width);
386 if (height == D3DX_DEFAULT)
387 height = make_pow2(imginfo.Height);
389 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
390 format = imginfo.Format;
392 if (width == D3DX_FROM_FILE)
395 width = imginfo.Width;
398 if (height == D3DX_FROM_FILE)
401 height = imginfo.Height;
404 if (format == D3DFMT_FROM_FILE)
407 format = imginfo.Format;
410 if (miplevels == D3DX_FROM_FILE)
412 file_miplevels = TRUE;
413 miplevels = imginfo.MipLevels;
416 /* fix texture creation parameters */
417 hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool);
425 if (((file_width) && (width != imginfo.Width)) ||
426 ((file_height) && (height != imginfo.Height)) ||
427 ((file_format) && (format != imginfo.Format)) ||
428 ((file_miplevels) && (miplevels != imginfo.MipLevels)))
430 return D3DERR_NOTAVAILABLE;
433 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
434 return D3DERR_INVALIDCALL;
436 /* Create the to-be-filled texture */
437 if ((caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (pool != D3DPOOL_DEFAULT) && (usage != D3DUSAGE_DYNAMIC))
439 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
444 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
455 IDirect3DTexture9_GetSurfaceLevel(*texptr, 0, &surface);
456 hr = D3DXLoadSurfaceFromFileInMemory(surface, palette, NULL, srcdata, srcdatasize, NULL, filter, colorkey, NULL);
457 IDirect3DSurface9_Release(surface);
461 IDirect3DTexture9_Release(*texptr);
466 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) *texptr, palette, 0, mipfilter);
470 IDirect3DTexture9_Release(*texptr);
475 /* Move the data to the actual texture if necessary */
476 if (texptr == &buftex)
478 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
482 IDirect3DTexture9_Release(buftex);
487 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9*)buftex, (IDirect3DBaseTexture9*)(*texture));
488 IDirect3DTexture9_Release(buftex);
497 HRESULT WINAPI D3DXCreateTextureFromFileInMemory(LPDIRECT3DDEVICE9 device,
500 LPDIRECT3DTEXTURE9 *texture)
502 TRACE("(%p, %p, %d, %p)\n", device, srcdata, srcdatasize, texture);
504 return D3DXCreateTextureFromFileInMemoryEx(device, srcdata, srcdatasize, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
505 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
508 HRESULT WINAPI D3DXCreateTextureFromFileExW(LPDIRECT3DDEVICE9 device,
519 D3DXIMAGE_INFO *srcinfo,
520 PALETTEENTRY *palette,
521 LPDIRECT3DTEXTURE9 *texture)
527 TRACE("(%p, %p, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): relay\n", device, debugstr_w(srcfile), width,
528 height, miplevels, usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
531 return D3DERR_INVALIDCALL;
533 hr = map_view_of_file(srcfile, &buffer, &size);
535 return D3DXERR_INVALIDDATA;
537 hr = D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels, usage, format, pool,
538 filter, mipfilter, colorkey, srcinfo, palette, texture);
540 UnmapViewOfFile(buffer);
545 HRESULT WINAPI D3DXCreateTextureFromFileExA(LPDIRECT3DDEVICE9 device,
556 D3DXIMAGE_INFO *srcinfo,
557 PALETTEENTRY *palette,
558 LPDIRECT3DTEXTURE9 *texture)
564 TRACE("(%p, %p, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): relay\n", device, debugstr_a(srcfile), width,
565 height, miplevels, usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
567 if (!device || !srcfile || !texture)
568 return D3DERR_INVALIDCALL;
570 len = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
571 widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR));
572 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, widename, len);
574 hr = D3DXCreateTextureFromFileExW(device, widename, width, height, miplevels,
575 usage, format, pool, filter, mipfilter,
576 colorkey, srcinfo, palette, texture);
578 HeapFree(GetProcessHeap(), 0, widename);
582 HRESULT WINAPI D3DXCreateTextureFromFileA(LPDIRECT3DDEVICE9 device,
584 LPDIRECT3DTEXTURE9 *texture)
586 TRACE("(%p, %s, %p)\n", device, debugstr_a(srcfile), texture);
588 return D3DXCreateTextureFromFileExA(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
589 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
592 HRESULT WINAPI D3DXCreateTextureFromFileW(LPDIRECT3DDEVICE9 device,
594 LPDIRECT3DTEXTURE9 *texture)
596 TRACE("(%p, %s, %p)\n", device, debugstr_w(srcfile), texture);
598 return D3DXCreateTextureFromFileExW(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
599 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
603 HRESULT WINAPI D3DXCreateTextureFromResourceA(LPDIRECT3DDEVICE9 device,
606 LPDIRECT3DTEXTURE9 *texture)
608 TRACE("(%p, %s): relay\n", srcmodule, debugstr_a(resource));
610 return D3DXCreateTextureFromResourceExA(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
611 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
614 HRESULT WINAPI D3DXCreateTextureFromResourceW(LPDIRECT3DDEVICE9 device,
617 LPDIRECT3DTEXTURE9 *texture)
619 TRACE("(%p, %s): relay\n", srcmodule, debugstr_w(resource));
621 return D3DXCreateTextureFromResourceExW(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
622 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
625 HRESULT WINAPI D3DXCreateTextureFromResourceExA(LPDIRECT3DDEVICE9 device,
637 D3DXIMAGE_INFO *srcinfo,
638 PALETTEENTRY *palette,
639 LPDIRECT3DTEXTURE9 *texture)
643 TRACE("(%p, %s): relay\n", srcmodule, debugstr_a(resource));
645 if (!device || !texture)
646 return D3DERR_INVALIDCALL;
648 resinfo = FindResourceA(srcmodule, resource, (LPCSTR) RT_RCDATA);
656 hr = load_resource_into_memory(srcmodule, resinfo, &buffer, &size);
659 return D3DXERR_INVALIDDATA;
661 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width,
662 height, miplevels, usage, format,
663 pool, filter, mipfilter, colorkey,
664 srcinfo, palette, texture);
667 /* Try loading the resource as bitmap data */
668 resinfo = FindResourceA(srcmodule, resource, (LPCSTR) RT_BITMAP);
672 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
676 return D3DXERR_INVALIDDATA;
679 HRESULT WINAPI D3DXCreateTextureFromResourceExW(LPDIRECT3DDEVICE9 device,
691 D3DXIMAGE_INFO *srcinfo,
692 PALETTEENTRY *palette,
693 LPDIRECT3DTEXTURE9 *texture)
697 TRACE("(%p, %s): relay\n", srcmodule, debugstr_w(resource));
699 if (!device || !texture)
700 return D3DERR_INVALIDCALL;
702 resinfo = FindResourceW(srcmodule, resource, (LPCWSTR) RT_RCDATA);
710 hr = load_resource_into_memory(srcmodule, resinfo, &buffer, &size);
713 return D3DXERR_INVALIDDATA;
715 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width,
716 height, miplevels, usage, format,
717 pool, filter, mipfilter, colorkey,
718 srcinfo, palette, texture);
721 /* Try loading the resource as bitmap data */
722 resinfo = FindResourceW(srcmodule, resource, (LPCWSTR) RT_BITMAP);
726 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
730 return D3DXERR_INVALIDDATA;