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/debug.h"
22 #include "d3dx9_36_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
26 /* Returns TRUE if num is a power of 2, FALSE if not, or if 0 */
27 BOOL is_pow2(UINT num)
29 return !(num & (num - 1));
32 /* Returns the smallest power of 2 which is greater than or equal to num */
33 UINT make_pow2(UINT num)
37 /* In the unlikely event somebody passes a large value, make sure we don't enter an infinite loop */
38 if (num >= 0x80000000)
47 HRESULT WINAPI D3DXCheckTextureRequirements(LPDIRECT3DDEVICE9 device,
55 UINT w = (width && *width) ? *width : 1;
56 UINT h = (height && *height) ? *height : 1;
59 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device, width, height, miplevels, usage, format, pool);
62 return D3DERR_INVALIDCALL;
65 if ((usage != D3DX_DEFAULT) &&
66 (usage & (D3DUSAGE_WRITEONLY | D3DUSAGE_DONOTCLIP | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES | D3DUSAGE_NPATCHES)))
67 return D3DERR_INVALIDCALL;
70 if ((pool != D3DPOOL_DEFAULT) && (pool != D3DPOOL_MANAGED) && (pool != D3DPOOL_SYSTEMMEM) && (pool != D3DPOOL_SCRATCH))
71 return D3DERR_INVALIDCALL;
73 /* width and height */
74 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
75 return D3DERR_INVALIDCALL;
77 /* 256 x 256 default width/height */
78 if ((w == D3DX_DEFAULT) && (h == D3DX_DEFAULT))
80 else if (w == D3DX_DEFAULT)
81 w = (height ? h : 256);
82 else if (h == D3DX_DEFAULT)
83 h = (width ? w : 256);
85 /* ensure width/height is power of 2 */
86 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(w)))
89 if (w > caps.MaxTextureWidth)
90 w = caps.MaxTextureWidth;
92 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(h)))
95 if (h > caps.MaxTextureHeight)
96 h = caps.MaxTextureHeight;
98 /* texture must be square? */
99 if (caps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
116 UINT max_mipmaps = 1;
118 if (!width && !height)
119 max_mipmaps = 9; /* number of mipmaps in a 256x256 texture */
122 UINT max_dimen = max(w, h);
124 while (max_dimen > 1)
131 if (*miplevels == 0 || *miplevels > max_mipmaps)
132 *miplevels = max_mipmaps;
138 D3DDEVICE_CREATION_PARAMETERS params;
139 IDirect3D9 *d3d = NULL;
143 hr = IDirect3DDevice9_GetDirect3D(device, &d3d);
148 hr = IDirect3DDevice9_GetCreationParameters(device, ¶ms);
153 hr = IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
158 if ((*format == D3DFMT_UNKNOWN) || (*format == D3DX_DEFAULT))
159 *format = D3DFMT_A8R8G8B8;
161 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType, mode.Format, usage,
162 D3DRTYPE_TEXTURE, *format);
165 FIXME("Pixel format adjustment not implemented yet\n");
170 IDirect3D9_Release(d3d);
173 return D3DERR_INVALIDCALL;
179 HRESULT WINAPI D3DXCreateTexture(LPDIRECT3DDEVICE9 pDevice,
186 LPDIRECT3DTEXTURE9 *ppTexture)
188 FIXME("(%p, %u, %u, %u, %x, %x, %x, %p): semi-stub\n", pDevice, width, height, miplevels, usage, format,
191 return IDirect3DDevice9_CreateTexture(pDevice, width, height, miplevels, usage, format, pool, ppTexture, NULL);
194 HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(LPDIRECT3DDEVICE9 device,
206 D3DXIMAGE_INFO* srcinfo,
207 PALETTEENTRY* palette,
208 LPDIRECT3DTEXTURE9* texture)
210 FIXME("(%p, %p, %u, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): stub\n", device, srcdata, srcdatasize, width,
211 height, miplevels, usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
216 HRESULT WINAPI D3DXCreateTextureFromFileExW(LPDIRECT3DDEVICE9 device,
227 D3DXIMAGE_INFO *srcinfo,
228 PALETTEENTRY *palette,
229 LPDIRECT3DTEXTURE9 *texture)
235 TRACE("(%p, %p, %u, %u, %u, %x, %x, %x, %u, %u, %x, %p, %p, %p): relay\n", device, debugstr_w(srcfile), width,
236 height, miplevels, usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
239 return D3DERR_INVALIDCALL;
241 hr = map_view_of_file(srcfile, &buffer, &size);
243 return D3DXERR_INVALIDDATA;
245 hr = D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels, usage, format, pool,
246 filter, mipfilter, colorkey, srcinfo, palette, texture);
248 UnmapViewOfFile(buffer);
253 HRESULT WINAPI D3DXCreateTextureFromFileA(LPDIRECT3DDEVICE9 device,
255 LPDIRECT3DTEXTURE9 *texture)
257 FIXME("(%p, %s, %p): stub\n", device, debugstr_a(srcfile), texture);