[D3DX9_36]
[reactos.git] / reactos / dll / directx / wine / d3dx9_36 / texture.c
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
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "d3dx9_36_private.h"
23
24 /* Returns TRUE if num is a power of 2, FALSE if not, or if 0 */
25 static BOOL is_pow2(UINT num)
26 {
27 return !(num & (num - 1));
28 }
29
30 /* Returns the smallest power of 2 which is greater than or equal to num */
31 static UINT make_pow2(UINT num)
32 {
33 UINT result = 1;
34
35 /* In the unlikely event somebody passes a large value, make sure we don't enter an infinite loop */
36 if (num >= 0x80000000)
37 return 0x80000000;
38
39 while (result < num)
40 result <<= 1;
41
42 return result;
43 }
44
45 static HRESULT get_surface(D3DRESOURCETYPE type, struct IDirect3DBaseTexture9 *tex,
46 int face, UINT level, struct IDirect3DSurface9 **surf)
47 {
48 switch (type)
49 {
50 case D3DRTYPE_TEXTURE:
51 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9*) tex, level, surf);
52 case D3DRTYPE_CUBETEXTURE:
53 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9*) tex, face, level, surf);
54 default:
55 ERR("Unexpected texture type\n");
56 return E_NOTIMPL;
57 }
58 }
59
60 HRESULT WINAPI D3DXFilterTexture(IDirect3DBaseTexture9 *texture,
61 const PALETTEENTRY *palette,
62 UINT srclevel,
63 DWORD filter)
64 {
65 UINT level;
66 HRESULT hr;
67 D3DRESOURCETYPE type;
68
69 TRACE("(%p, %p, %u, %#x)\n", texture, palette, srclevel, filter);
70
71 if (!texture)
72 return D3DERR_INVALIDCALL;
73
74 if ((filter & 0xFFFF) > D3DX_FILTER_BOX && filter != D3DX_DEFAULT)
75 return D3DERR_INVALIDCALL;
76
77 if (srclevel == D3DX_DEFAULT)
78 srclevel = 0;
79 else if (srclevel >= IDirect3DBaseTexture9_GetLevelCount(texture))
80 return D3DERR_INVALIDCALL;
81
82 switch (type = IDirect3DBaseTexture9_GetType(texture))
83 {
84 case D3DRTYPE_TEXTURE:
85 case D3DRTYPE_CUBETEXTURE:
86 {
87 IDirect3DSurface9 *topsurf, *mipsurf;
88 D3DSURFACE_DESC desc;
89 int i, numfaces;
90
91 if (type == D3DRTYPE_TEXTURE)
92 {
93 numfaces = 1;
94 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9*) texture, srclevel, &desc);
95 }
96 else
97 {
98 numfaces = 6;
99 IDirect3DCubeTexture9_GetLevelDesc((IDirect3DTexture9*) texture, srclevel, &desc);
100 }
101
102 if (filter == D3DX_DEFAULT)
103 {
104 if (is_pow2(desc.Width) && is_pow2(desc.Height))
105 filter = D3DX_FILTER_BOX;
106 else
107 filter = D3DX_FILTER_BOX | D3DX_FILTER_DITHER;
108 }
109
110 for (i = 0; i < numfaces; i++)
111 {
112 level = srclevel + 1;
113 hr = get_surface(type, texture, i, srclevel, &topsurf);
114
115 if (FAILED(hr))
116 return D3DERR_INVALIDCALL;
117
118 while (get_surface(type, texture, i, level, &mipsurf) == D3D_OK)
119 {
120 hr = D3DXLoadSurfaceFromSurface(mipsurf, palette, NULL, topsurf, palette, NULL, filter, 0);
121 IDirect3DSurface9_Release(topsurf);
122 topsurf = mipsurf;
123
124 if (FAILED(hr))
125 break;
126
127 level++;
128 }
129
130 IDirect3DSurface9_Release(topsurf);
131 if (FAILED(hr))
132 return hr;
133 }
134
135 return D3D_OK;
136 }
137
138 case D3DRTYPE_VOLUMETEXTURE:
139 {
140 D3DVOLUME_DESC desc;
141 int level, level_count;
142 IDirect3DVolume9 *top_volume, *mip_volume;
143 IDirect3DVolumeTexture9 *volume_texture = (IDirect3DVolumeTexture9*) texture;
144
145 IDirect3DVolumeTexture9_GetLevelDesc(volume_texture, srclevel, &desc);
146
147 if (filter == D3DX_DEFAULT)
148 {
149 if (is_pow2(desc.Width) && is_pow2(desc.Height) && is_pow2(desc.Depth))
150 filter = D3DX_FILTER_BOX;
151 else
152 filter = D3DX_FILTER_BOX | D3DX_FILTER_DITHER;
153 }
154
155 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, srclevel, &top_volume);
156 if (FAILED(hr))
157 return hr;
158
159 level_count = IDirect3DVolumeTexture9_GetLevelCount(volume_texture);
160 for (level = srclevel + 1; level < level_count; level++)
161 {
162 IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, level, &mip_volume);
163 hr = D3DXLoadVolumeFromVolume(mip_volume, palette, NULL, top_volume, palette, NULL, filter, 0);
164 IDirect3DVolume9_Release(top_volume);
165 top_volume = mip_volume;
166
167 if (FAILED(hr))
168 break;
169 }
170
171 IDirect3DVolume9_Release(top_volume);
172 if (FAILED(hr))
173 return hr;
174
175 return D3D_OK;
176 }
177
178 default:
179 return D3DERR_INVALIDCALL;
180 }
181 }
182
183 HRESULT WINAPI D3DXCheckTextureRequirements(struct IDirect3DDevice9 *device, UINT *width, UINT *height,
184 UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
185 {
186 UINT w = (width && *width) ? *width : 1;
187 UINT h = (height && *height) ? *height : 1;
188 D3DCAPS9 caps;
189 D3DDEVICE_CREATION_PARAMETERS params;
190 IDirect3D9 *d3d = NULL;
191 D3DDISPLAYMODE mode;
192 HRESULT hr;
193 D3DFORMAT usedformat = D3DFMT_UNKNOWN;
194 const struct pixel_format_desc *fmt;
195
196 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device, width, height, miplevels, usage, format, pool);
197
198 if (!device)
199 return D3DERR_INVALIDCALL;
200
201 /* usage */
202 if (usage == D3DX_DEFAULT)
203 usage = 0;
204 if (usage & (D3DUSAGE_WRITEONLY | D3DUSAGE_DONOTCLIP | D3DUSAGE_POINTS | D3DUSAGE_RTPATCHES | D3DUSAGE_NPATCHES))
205 return D3DERR_INVALIDCALL;
206
207 /* pool */
208 if ((pool != D3DPOOL_DEFAULT) && (pool != D3DPOOL_MANAGED) && (pool != D3DPOOL_SYSTEMMEM) && (pool != D3DPOOL_SCRATCH))
209 return D3DERR_INVALIDCALL;
210
211 /* format */
212 if (format)
213 {
214 TRACE("Requested format %x\n", *format);
215 usedformat = *format;
216 }
217
218 hr = IDirect3DDevice9_GetDirect3D(device, &d3d);
219
220 if (FAILED(hr))
221 goto cleanup;
222
223 hr = IDirect3DDevice9_GetCreationParameters(device, &params);
224
225 if (FAILED(hr))
226 goto cleanup;
227
228 hr = IDirect3DDevice9_GetDisplayMode(device, 0, &mode);
229
230 if (FAILED(hr))
231 goto cleanup;
232
233 if ((usedformat == D3DFMT_UNKNOWN) || (usedformat == D3DX_DEFAULT))
234 usedformat = D3DFMT_A8R8G8B8;
235
236 fmt = get_format_info(usedformat);
237
238 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType, mode.Format,
239 usage, D3DRTYPE_TEXTURE, usedformat);
240 if (FAILED(hr))
241 {
242 BOOL allow_24bits;
243 int bestscore = INT_MIN, i = 0, j;
244 unsigned int channels;
245 const struct pixel_format_desc *curfmt, *bestfmt = NULL;
246
247 TRACE("Requested format not supported, looking for a fallback.\n");
248
249 if (!fmt)
250 {
251 FIXME("Pixel format %x not handled\n", usedformat);
252 goto cleanup;
253 }
254
255 allow_24bits = fmt->bytes_per_pixel == 3;
256 channels = (fmt->bits[0] ? 1 : 0) + (fmt->bits[1] ? 1 : 0)
257 + (fmt->bits[2] ? 1 : 0) + (fmt->bits[3] ? 1 : 0);
258 usedformat = D3DFMT_UNKNOWN;
259
260 while ((curfmt = get_format_info_idx(i)))
261 {
262 unsigned int curchannels = (curfmt->bits[0] ? 1 : 0) + (curfmt->bits[1] ? 1 : 0)
263 + (curfmt->bits[2] ? 1 : 0) + (curfmt->bits[3] ? 1 : 0);
264 int score;
265
266 i++;
267
268 if (curchannels < channels)
269 continue;
270 if (curfmt->bytes_per_pixel == 3 && !allow_24bits)
271 continue;
272
273 hr = IDirect3D9_CheckDeviceFormat(d3d, params.AdapterOrdinal, params.DeviceType,
274 mode.Format, usage, D3DRTYPE_TEXTURE, curfmt->format);
275 if (FAILED(hr))
276 continue;
277
278 /* This format can be used, let's evaluate it.
279 Weights chosen quite arbitrarily... */
280 score = 16 - 4 * (curchannels - channels);
281
282 for (j = 0; j < 4; j++)
283 {
284 int diff = curfmt->bits[j] - fmt->bits[j];
285 score += 16 - (diff < 0 ? -diff * 4 : diff);
286 }
287
288 if (score > bestscore)
289 {
290 bestscore = score;
291 usedformat = curfmt->format;
292 bestfmt = curfmt;
293 }
294 }
295 fmt = bestfmt;
296 hr = D3D_OK;
297 }
298
299 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
300 return D3DERR_INVALIDCALL;
301
302 if ((w == D3DX_DEFAULT) && (h == D3DX_DEFAULT))
303 w = h = 256;
304 else if (w == D3DX_DEFAULT)
305 w = (height ? h : 256);
306 else if (h == D3DX_DEFAULT)
307 h = (width ? w : 256);
308
309 if (fmt->block_width != 1 || fmt->block_height != 1)
310 {
311 if (w < fmt->block_width)
312 w = fmt->block_width;
313 if (h < fmt->block_height)
314 h = fmt->block_height;
315 }
316
317 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(w)))
318 w = make_pow2(w);
319
320 if (w > caps.MaxTextureWidth)
321 w = caps.MaxTextureWidth;
322
323 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && (!is_pow2(h)))
324 h = make_pow2(h);
325
326 if (h > caps.MaxTextureHeight)
327 h = caps.MaxTextureHeight;
328
329 if (caps.TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
330 {
331 if (w > h)
332 h = w;
333 else
334 w = h;
335 }
336
337 if (width)
338 *width = w;
339
340 if (height)
341 *height = h;
342
343 if (miplevels && (usage & D3DUSAGE_AUTOGENMIPMAP))
344 {
345 if (*miplevels > 1)
346 *miplevels = 0;
347 }
348 else if (miplevels)
349 {
350 UINT max_mipmaps = 1;
351
352 if (!width && !height)
353 max_mipmaps = 9; /* number of mipmaps in a 256x256 texture */
354 else
355 {
356 UINT max_dimen = max(w, h);
357
358 while (max_dimen > 1)
359 {
360 max_dimen >>= 1;
361 max_mipmaps++;
362 }
363 }
364
365 if (*miplevels == 0 || *miplevels > max_mipmaps)
366 *miplevels = max_mipmaps;
367 }
368
369 cleanup:
370
371 if (d3d)
372 IDirect3D9_Release(d3d);
373
374 if (FAILED(hr))
375 return hr;
376
377 if (usedformat == D3DFMT_UNKNOWN)
378 {
379 WARN("Couldn't find a suitable pixel format\n");
380 return D3DERR_NOTAVAILABLE;
381 }
382
383 TRACE("Format chosen: %x\n", usedformat);
384 if (format)
385 *format = usedformat;
386
387 return D3D_OK;
388 }
389
390 HRESULT WINAPI D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9 *device, UINT *size,
391 UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
392 {
393 D3DCAPS9 caps;
394 UINT s = (size && *size) ? *size : 256;
395 HRESULT hr;
396
397 TRACE("(%p, %p, %p, %u, %p, %u)\n", device, size, miplevels, usage, format, pool);
398
399 if (s == D3DX_DEFAULT)
400 s = 256;
401
402 if (!device || FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
403 return D3DERR_INVALIDCALL;
404
405 if (!(caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP))
406 return D3DERR_NOTAVAILABLE;
407
408 /* ensure width/height is power of 2 */
409 if ((caps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && (!is_pow2(s)))
410 s = make_pow2(s);
411
412 hr = D3DXCheckTextureRequirements(device, &s, &s, miplevels, usage, format, pool);
413
414 if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
415 {
416 if(miplevels)
417 *miplevels = 1;
418 }
419
420 if (size)
421 *size = s;
422
423 return hr;
424 }
425
426 HRESULT WINAPI D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9 *device, UINT *width, UINT *height,
427 UINT *depth, UINT *miplevels, DWORD usage, D3DFORMAT *format, D3DPOOL pool)
428 {
429 D3DCAPS9 caps;
430 UINT w = width ? *width : D3DX_DEFAULT;
431 UINT h = height ? *height : D3DX_DEFAULT;
432 UINT d = (depth && *depth) ? *depth : 1;
433 HRESULT hr;
434
435 TRACE("(%p, %p, %p, %p, %p, %u, %p, %u)\n", device, width, height, depth, miplevels,
436 usage, format, pool);
437
438 if (!device || FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
439 return D3DERR_INVALIDCALL;
440
441 if (!(caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP))
442 return D3DERR_NOTAVAILABLE;
443
444 hr = D3DXCheckTextureRequirements(device, &w, &h, NULL, usage, format, pool);
445 if (d == D3DX_DEFAULT)
446 d = 1;
447
448 /* ensure width/height is power of 2 */
449 if ((caps.TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2) &&
450 (!is_pow2(w) || !is_pow2(h) || !is_pow2(d)))
451 {
452 w = make_pow2(w);
453 h = make_pow2(h);
454 d = make_pow2(d);
455 }
456
457 if (w > caps.MaxVolumeExtent)
458 w = caps.MaxVolumeExtent;
459 if (h > caps.MaxVolumeExtent)
460 h = caps.MaxVolumeExtent;
461 if (d > caps.MaxVolumeExtent)
462 d = caps.MaxVolumeExtent;
463
464 if (miplevels)
465 {
466 if (!(caps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
467 *miplevels = 1;
468 else if ((usage & D3DUSAGE_AUTOGENMIPMAP))
469 {
470 if (*miplevels > 1)
471 *miplevels = 0;
472 }
473 else
474 {
475 UINT max_mipmaps = 1;
476 UINT max_dimen = max(max(w, h), d);
477
478 while (max_dimen > 1)
479 {
480 max_dimen >>= 1;
481 max_mipmaps++;
482 }
483
484 if (*miplevels == 0 || *miplevels > max_mipmaps)
485 *miplevels = max_mipmaps;
486 }
487 }
488
489 if (width)
490 *width = w;
491 if (height)
492 *height = h;
493 if (depth)
494 *depth = d;
495
496 return hr;
497 }
498
499 HRESULT WINAPI D3DXCreateTexture(struct IDirect3DDevice9 *device, UINT width, UINT height,
500 UINT miplevels, DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DTexture9 **texture)
501 {
502 HRESULT hr;
503
504 TRACE("device %p, width %u, height %u, miplevels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
505 device, width, height, miplevels, usage, format, pool, texture);
506
507 if (!device || !texture)
508 return D3DERR_INVALIDCALL;
509
510 if (FAILED(hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool)))
511 return hr;
512
513 return IDirect3DDevice9_CreateTexture(device, width, height, miplevels, usage, format, pool, texture, NULL);
514 }
515
516 HRESULT WINAPI D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9 *device, const void *srcdata,
517 UINT srcdatasize, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
518 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
519 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
520 {
521 IDirect3DTexture9 **texptr;
522 IDirect3DTexture9 *buftex;
523 IDirect3DSurface9 *surface;
524 BOOL file_width = FALSE, file_height = FALSE;
525 BOOL file_format = FALSE, file_miplevels = FALSE;
526 BOOL dynamic_texture;
527 D3DXIMAGE_INFO imginfo;
528 UINT loaded_miplevels, skip_levels;
529 D3DCAPS9 caps;
530 HRESULT hr;
531
532 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u,"
533 " usage %#x, format %#x, pool %#x, filter %#x, mipfilter %#x, colorkey %#x,"
534 " srcinfo %p, palette %p, texture %p.\n",
535 device, srcdata, srcdatasize, width, height, miplevels, usage, format, pool,
536 filter, mipfilter, colorkey, srcinfo, palette, texture);
537
538 /* check for invalid parameters */
539 if (!device || !texture || !srcdata || !srcdatasize)
540 return D3DERR_INVALIDCALL;
541
542 hr = D3DXGetImageInfoFromFileInMemory(srcdata, srcdatasize, &imginfo);
543 if (FAILED(hr))
544 {
545 FIXME("Unrecognized file format, returning failure.\n");
546 *texture = NULL;
547 return hr;
548 }
549
550 /* handle default values */
551 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
552 width = imginfo.Width;
553
554 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
555 height = imginfo.Height;
556
557 if (width == D3DX_DEFAULT)
558 width = make_pow2(imginfo.Width);
559
560 if (height == D3DX_DEFAULT)
561 height = make_pow2(imginfo.Height);
562
563 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
564 format = imginfo.Format;
565
566 if (width == D3DX_FROM_FILE)
567 {
568 file_width = TRUE;
569 width = imginfo.Width;
570 }
571
572 if (height == D3DX_FROM_FILE)
573 {
574 file_height = TRUE;
575 height = imginfo.Height;
576 }
577
578 if (format == D3DFMT_FROM_FILE)
579 {
580 file_format = TRUE;
581 format = imginfo.Format;
582 }
583
584 if (miplevels == D3DX_FROM_FILE)
585 {
586 file_miplevels = TRUE;
587 miplevels = imginfo.MipLevels;
588 }
589
590 skip_levels = mipfilter != D3DX_DEFAULT ? mipfilter >> D3DX_SKIP_DDS_MIP_LEVELS_SHIFT : 0;
591 if (skip_levels && imginfo.MipLevels > skip_levels)
592 {
593 TRACE("Skipping the first %u (of %u) levels of a DDS mipmapped texture.\n",
594 skip_levels, imginfo.MipLevels);
595 TRACE("Texture level 0 dimensions are %ux%u.\n", imginfo.Width, imginfo.Height);
596 width >>= skip_levels;
597 height >>= skip_levels;
598 miplevels -= skip_levels;
599 }
600 else
601 {
602 skip_levels = 0;
603 }
604
605 /* fix texture creation parameters */
606 hr = D3DXCheckTextureRequirements(device, &width, &height, &miplevels, usage, &format, pool);
607 if (FAILED(hr))
608 {
609 FIXME("Couldn't find suitable texture parameters.\n");
610 *texture = NULL;
611 return hr;
612 }
613
614 if (imginfo.MipLevels < miplevels && (D3DFMT_DXT1 <= imginfo.Format && imginfo.Format <= D3DFMT_DXT5))
615 {
616 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet\n");
617 miplevels = imginfo.MipLevels;
618 }
619 if (imginfo.ResourceType == D3DRTYPE_VOLUMETEXTURE
620 && D3DFMT_DXT1 <= imginfo.Format && imginfo.Format <= D3DFMT_DXT5 && miplevels > 1)
621 {
622 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet.\n");
623 miplevels = 1;
624 }
625
626 if (((file_width) && (width != imginfo.Width)) ||
627 ((file_height) && (height != imginfo.Height)) ||
628 ((file_format) && (format != imginfo.Format)) ||
629 ((file_miplevels) && (miplevels != imginfo.MipLevels)))
630 {
631 return D3DERR_NOTAVAILABLE;
632 }
633
634 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device, &caps)))
635 return D3DERR_INVALIDCALL;
636
637 /* Create the to-be-filled texture */
638 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
639 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
640 {
641 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
642 texptr = &buftex;
643 }
644 else
645 {
646 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
647 texptr = texture;
648 }
649
650 if (FAILED(hr))
651 {
652 FIXME("Texture creation failed.\n");
653 *texture = NULL;
654 return hr;
655 }
656
657 TRACE("Texture created correctly. Now loading the texture data into it.\n");
658 if (imginfo.ImageFileFormat != D3DXIFF_DDS)
659 {
660 IDirect3DTexture9_GetSurfaceLevel(*texptr, 0, &surface);
661 hr = D3DXLoadSurfaceFromFileInMemory(surface, palette, NULL, srcdata, srcdatasize, NULL, filter, colorkey, NULL);
662 IDirect3DSurface9_Release(surface);
663 loaded_miplevels = min(IDirect3DTexture9_GetLevelCount(*texptr), imginfo.MipLevels);
664 }
665 else
666 {
667 hr = load_texture_from_dds(*texptr, srcdata, palette, filter, colorkey, &imginfo, skip_levels,
668 &loaded_miplevels);
669 }
670
671 if (FAILED(hr))
672 {
673 FIXME("Texture loading failed.\n");
674 IDirect3DTexture9_Release(*texptr);
675 *texture = NULL;
676 return hr;
677 }
678
679 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) *texptr, palette, loaded_miplevels - 1, mipfilter);
680 if (FAILED(hr))
681 {
682 FIXME("Texture filtering failed.\n");
683 IDirect3DTexture9_Release(*texptr);
684 *texture = NULL;
685 return hr;
686 }
687
688 /* Move the data to the actual texture if necessary */
689 if (texptr == &buftex)
690 {
691 hr = D3DXCreateTexture(device, width, height, miplevels, usage, format, pool, texture);
692
693 if (FAILED(hr))
694 {
695 IDirect3DTexture9_Release(buftex);
696 *texture = NULL;
697 return hr;
698 }
699
700 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9*)buftex, (IDirect3DBaseTexture9*)(*texture));
701 IDirect3DTexture9_Release(buftex);
702 }
703
704 if (srcinfo)
705 *srcinfo = imginfo;
706
707 return D3D_OK;
708 }
709
710 HRESULT WINAPI D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9 *device,
711 const void *srcdata, UINT srcdatasize, struct IDirect3DTexture9 **texture)
712 {
713 TRACE("(%p, %p, %d, %p)\n", device, srcdata, srcdatasize, texture);
714
715 return D3DXCreateTextureFromFileInMemoryEx(device, srcdata, srcdatasize, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
716 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
717 }
718
719 HRESULT WINAPI D3DXCreateTextureFromFileExW(struct IDirect3DDevice9 *device, const WCHAR *srcfile,
720 UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
721 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
722 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
723 {
724 void *buffer;
725 HRESULT hr;
726 DWORD size;
727
728 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
729 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
730 device, debugstr_w(srcfile), width, height, miplevels, usage, format,
731 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
732
733 if (!srcfile)
734 return D3DERR_INVALIDCALL;
735
736 hr = map_view_of_file(srcfile, &buffer, &size);
737 if (FAILED(hr))
738 return D3DXERR_INVALIDDATA;
739
740 hr = D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels, usage, format, pool,
741 filter, mipfilter, colorkey, srcinfo, palette, texture);
742
743 UnmapViewOfFile(buffer);
744
745 return hr;
746 }
747
748 HRESULT WINAPI D3DXCreateTextureFromFileExA(struct IDirect3DDevice9 *device, const char *srcfile,
749 UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
750 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
751 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
752 {
753 WCHAR *widename;
754 HRESULT hr;
755 DWORD len;
756
757 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
758 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
759 device, debugstr_a(srcfile), width, height, miplevels, usage, format,
760 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
761
762 if (!device || !srcfile || !texture)
763 return D3DERR_INVALIDCALL;
764
765 len = MultiByteToWideChar(CP_ACP, 0, srcfile, -1, NULL, 0);
766 widename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*widename));
767 MultiByteToWideChar(CP_ACP, 0, srcfile, -1, widename, len);
768
769 hr = D3DXCreateTextureFromFileExW(device, widename, width, height, miplevels,
770 usage, format, pool, filter, mipfilter,
771 colorkey, srcinfo, palette, texture);
772
773 HeapFree(GetProcessHeap(), 0, widename);
774 return hr;
775 }
776
777 HRESULT WINAPI D3DXCreateTextureFromFileA(struct IDirect3DDevice9 *device,
778 const char *srcfile, struct IDirect3DTexture9 **texture)
779 {
780 TRACE("(%p, %s, %p)\n", device, debugstr_a(srcfile), texture);
781
782 return D3DXCreateTextureFromFileExA(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
783 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
784 }
785
786 HRESULT WINAPI D3DXCreateTextureFromFileW(struct IDirect3DDevice9 *device,
787 const WCHAR *srcfile, struct IDirect3DTexture9 **texture)
788 {
789 TRACE("(%p, %s, %p)\n", device, debugstr_w(srcfile), texture);
790
791 return D3DXCreateTextureFromFileExW(device, srcfile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
792 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
793 }
794
795
796 HRESULT WINAPI D3DXCreateTextureFromResourceA(struct IDirect3DDevice9 *device,
797 HMODULE srcmodule, const char *resource, struct IDirect3DTexture9 **texture)
798 {
799 TRACE("(%p, %s): relay\n", srcmodule, debugstr_a(resource));
800
801 return D3DXCreateTextureFromResourceExA(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
802 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
803 }
804
805 HRESULT WINAPI D3DXCreateTextureFromResourceW(struct IDirect3DDevice9 *device,
806 HMODULE srcmodule, const WCHAR *resource, struct IDirect3DTexture9 **texture)
807 {
808 TRACE("(%p, %s): relay\n", srcmodule, debugstr_w(resource));
809
810 return D3DXCreateTextureFromResourceExW(device, srcmodule, resource, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN,
811 D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
812 }
813
814 HRESULT WINAPI D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9 *device, HMODULE srcmodule,
815 const char *resource, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
816 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
817 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
818 {
819 HRSRC resinfo;
820 void *buffer;
821 DWORD size;
822
823 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
824 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
825 device, srcmodule, debugstr_a(resource), width, height, miplevels, usage, format,
826 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
827
828 if (!device || !texture)
829 return D3DERR_INVALIDCALL;
830
831 if (!(resinfo = FindResourceA(srcmodule, resource, (const char *)RT_RCDATA))
832 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
833 && !(resinfo = FindResourceA(srcmodule, resource, (const char *)RT_BITMAP)))
834 return D3DXERR_INVALIDDATA;
835
836 if (FAILED(load_resource_into_memory(srcmodule, resinfo, &buffer, &size)))
837 return D3DXERR_INVALIDDATA;
838
839 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels,
840 usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
841 }
842
843 HRESULT WINAPI D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9 *device, HMODULE srcmodule,
844 const WCHAR *resource, UINT width, UINT height, UINT miplevels, DWORD usage, D3DFORMAT format,
845 D3DPOOL pool, DWORD filter, DWORD mipfilter, D3DCOLOR colorkey, D3DXIMAGE_INFO *srcinfo,
846 PALETTEENTRY *palette, struct IDirect3DTexture9 **texture)
847 {
848 HRSRC resinfo;
849 void *buffer;
850 DWORD size;
851
852 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
853 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
854 device, srcmodule, debugstr_w(resource), width, height, miplevels, usage, format,
855 pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
856
857 if (!device || !texture)
858 return D3DERR_INVALIDCALL;
859
860 if (!(resinfo = FindResourceW(srcmodule, resource, (const WCHAR *)RT_RCDATA))
861 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
862 && !(resinfo = FindResourceW(srcmodule, resource, (const WCHAR *)RT_BITMAP)))
863 return D3DXERR_INVALIDDATA;
864
865 if (FAILED(load_resource_into_memory(srcmodule, resinfo, &buffer, &size)))
866 return D3DXERR_INVALIDDATA;
867
868 return D3DXCreateTextureFromFileInMemoryEx(device, buffer, size, width, height, miplevels,
869 usage, format, pool, filter, mipfilter, colorkey, srcinfo, palette, texture);
870 }
871
872 HRESULT WINAPI D3DXCreateCubeTexture(struct IDirect3DDevice9 *device, UINT size, UINT miplevels,
873 DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DCubeTexture9 **texture)
874 {
875 HRESULT hr;
876
877 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device, size, miplevels, usage, format,
878 pool, texture);
879
880 if (!device || !texture)
881 return D3DERR_INVALIDCALL;
882
883 hr = D3DXCheckCubeTextureRequirements(device, &size, &miplevels, usage, &format, pool);
884
885 if (FAILED(hr))
886 {
887 TRACE("D3DXCheckCubeTextureRequirements failed\n");
888 return hr;
889 }
890
891 return IDirect3DDevice9_CreateCubeTexture(device, size, miplevels, usage, format, pool, texture, NULL);
892 }
893
894 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9 *device,
895 const void *data, UINT datasize, struct IDirect3DCubeTexture9 **texture)
896 {
897 TRACE("(%p, %p, %u, %p)\n", device, data, datasize, texture);
898
899 return D3DXCreateCubeTextureFromFileInMemoryEx(device, data, datasize, D3DX_DEFAULT, D3DX_DEFAULT,
900 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, texture);
901 }
902
903 HRESULT WINAPI D3DXCreateVolumeTexture(struct IDirect3DDevice9 *device, UINT width, UINT height, UINT depth,
904 UINT miplevels, DWORD usage, D3DFORMAT format, D3DPOOL pool, struct IDirect3DVolumeTexture9 **texture)
905 {
906 HRESULT hr;
907
908 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device, width, height, depth,
909 miplevels, usage, format, pool, texture);
910
911 if (!device || !texture)
912 return D3DERR_INVALIDCALL;
913
914 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth,
915 &miplevels, usage, &format, pool);
916
917 if (FAILED(hr))
918 {
919 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
920 return hr;
921 }
922
923 return IDirect3DDevice9_CreateVolumeTexture(device, width, height, depth, miplevels,
924 usage, format, pool, texture, NULL);
925 }
926
927 HRESULT WINAPI D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9 *device,
928 const char *filename,
929 IDirect3DVolumeTexture9 **volume_texture)
930 {
931 int len;
932 HRESULT hr;
933 void *data;
934 DWORD data_size;
935 WCHAR *filenameW;
936
937 TRACE("(%p, %s, %p): relay\n",
938 device, debugstr_a(filename), volume_texture);
939
940 if (!filename) return D3DERR_INVALIDCALL;
941
942 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
943 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
944 if (!filenameW) return E_OUTOFMEMORY;
945 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
946
947 hr = map_view_of_file(filenameW, &data, &data_size);
948 HeapFree(GetProcessHeap(), 0, filenameW);
949 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
950
951 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
952 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
953
954 UnmapViewOfFile(data);
955 return hr;
956 }
957
958 HRESULT WINAPI D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9 *device,
959 const WCHAR *filename,
960 IDirect3DVolumeTexture9 **volume_texture)
961 {
962 HRESULT hr;
963 void *data;
964 DWORD data_size;
965
966 TRACE("(%p, %s, %p): relay\n",
967 device, debugstr_w(filename), volume_texture);
968
969 if (!filename) return D3DERR_INVALIDCALL;
970
971 hr = map_view_of_file(filename, &data, &data_size);
972 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
973
974 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
975 D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, volume_texture);
976
977 UnmapViewOfFile(data);
978 return hr;
979 }
980
981 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9 *device,
982 const char *filename,
983 UINT width,
984 UINT height,
985 UINT depth,
986 UINT mip_levels,
987 DWORD usage,
988 D3DFORMAT format,
989 D3DPOOL pool,
990 DWORD filter,
991 DWORD mip_filter,
992 D3DCOLOR color_key,
993 D3DXIMAGE_INFO *src_info,
994 PALETTEENTRY *palette,
995 IDirect3DVolumeTexture9 **volume_texture)
996 {
997 int len;
998 HRESULT hr;
999 WCHAR *filenameW;
1000 void *data;
1001 DWORD data_size;
1002
1003 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1004 device, debugstr_a(filename), width, height, depth, mip_levels,
1005 usage, format, pool, filter, mip_filter, color_key, src_info,
1006 palette, volume_texture);
1007
1008 if (!filename) return D3DERR_INVALIDCALL;
1009
1010 len = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
1011 filenameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1012 if (!filenameW) return E_OUTOFMEMORY;
1013 MultiByteToWideChar(CP_ACP, 0, filename, -1, filenameW, len);
1014
1015 hr = map_view_of_file(filenameW, &data, &data_size);
1016 HeapFree(GetProcessHeap(), 0, filenameW);
1017 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1018
1019 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1020 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1021 volume_texture);
1022
1023 UnmapViewOfFile(data);
1024 return hr;
1025 }
1026
1027 HRESULT WINAPI D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9 *device,
1028 const WCHAR *filename,
1029 UINT width,
1030 UINT height,
1031 UINT depth,
1032 UINT mip_levels,
1033 DWORD usage,
1034 D3DFORMAT format,
1035 D3DPOOL pool,
1036 DWORD filter,
1037 DWORD mip_filter,
1038 D3DCOLOR color_key,
1039 D3DXIMAGE_INFO *src_info,
1040 PALETTEENTRY *palette,
1041 IDirect3DVolumeTexture9 **volume_texture)
1042 {
1043 HRESULT hr;
1044 void *data;
1045 DWORD data_size;
1046
1047 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1048 device, debugstr_w(filename), width, height, depth, mip_levels,
1049 usage, format, pool, filter, mip_filter, color_key, src_info,
1050 palette, volume_texture);
1051
1052 if (!filename) return D3DERR_INVALIDCALL;
1053
1054 hr = map_view_of_file(filename, &data, &data_size);
1055 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1056
1057 hr = D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, width, height, depth,
1058 mip_levels, usage, format, pool, filter, mip_filter, color_key, src_info, palette,
1059 volume_texture);
1060
1061 UnmapViewOfFile(data);
1062 return hr;
1063 }
1064
1065 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9 *device,
1066 const void *data,
1067 UINT data_size,
1068 IDirect3DVolumeTexture9 **volume_texture)
1069 {
1070 TRACE("(%p, %p, %u, %p): relay\n", device, data, data_size, volume_texture);
1071
1072 return D3DXCreateVolumeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1073 D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT,
1074 0, NULL, NULL, volume_texture);
1075 }
1076
1077 HRESULT WINAPI D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
1078 const void *data,
1079 UINT data_size,
1080 UINT width,
1081 UINT height,
1082 UINT depth,
1083 UINT mip_levels,
1084 DWORD usage,
1085 D3DFORMAT format,
1086 D3DPOOL pool,
1087 DWORD filter,
1088 DWORD mip_filter,
1089 D3DCOLOR color_key,
1090 D3DXIMAGE_INFO *info,
1091 PALETTEENTRY *palette,
1092 IDirect3DVolumeTexture9 **volume_texture)
1093 {
1094 HRESULT hr;
1095 D3DCAPS9 caps;
1096 D3DXIMAGE_INFO image_info;
1097 BOOL dynamic_texture;
1098 BOOL file_width = FALSE;
1099 BOOL file_height = FALSE;
1100 BOOL file_depth = FALSE;
1101 BOOL file_format = FALSE;
1102 BOOL file_mip_levels = FALSE;
1103 IDirect3DVolumeTexture9 *tex, *buftex;
1104
1105 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1106 device, data, data_size, width, height, depth, mip_levels, usage, format, pool,
1107 filter, mip_filter, color_key, info, palette, volume_texture);
1108
1109 if (!device || !data || !data_size || !volume_texture)
1110 return D3DERR_INVALIDCALL;
1111
1112 hr = D3DXGetImageInfoFromFileInMemory(data, data_size, &image_info);
1113 if (FAILED(hr)) return hr;
1114
1115 if (image_info.ImageFileFormat != D3DXIFF_DDS)
1116 return D3DXERR_INVALIDDATA;
1117
1118 if (width == 0 || width == D3DX_DEFAULT_NONPOW2)
1119 width = image_info.Width;
1120 if (width == D3DX_DEFAULT)
1121 width = make_pow2(image_info.Width);
1122
1123 if (height == 0 || height == D3DX_DEFAULT_NONPOW2)
1124 height = image_info.Height;
1125 if (height == D3DX_DEFAULT)
1126 height = make_pow2(image_info.Height);
1127
1128 if (depth == 0 || depth == D3DX_DEFAULT_NONPOW2)
1129 depth = image_info.Depth;
1130 if (depth == D3DX_DEFAULT)
1131 depth = make_pow2(image_info.Depth);
1132
1133 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1134 format = image_info.Format;
1135
1136 if (width == D3DX_FROM_FILE)
1137 {
1138 file_width = TRUE;
1139 width = image_info.Width;
1140 }
1141
1142 if (height == D3DX_FROM_FILE)
1143 {
1144 file_height = TRUE;
1145 height = image_info.Height;
1146 }
1147
1148 if (depth == D3DX_FROM_FILE)
1149 {
1150 file_depth = TRUE;
1151 depth = image_info.Depth;
1152 }
1153
1154 if (format == D3DFMT_FROM_FILE)
1155 {
1156 file_format = TRUE;
1157 format = image_info.Format;
1158 }
1159
1160 if (mip_levels == D3DX_FROM_FILE)
1161 {
1162 file_mip_levels = TRUE;
1163 mip_levels = image_info.MipLevels;
1164 }
1165
1166 hr = D3DXCheckVolumeTextureRequirements(device, &width, &height, &depth, &mip_levels, usage, &format, pool);
1167 if (FAILED(hr)) return hr;
1168
1169 if ((file_width && width != image_info.Width)
1170 || (file_height && height != image_info.Height)
1171 || (file_depth && depth != image_info.Depth)
1172 || (file_format && format != image_info.Format)
1173 || (file_mip_levels && mip_levels != image_info.MipLevels))
1174 return D3DERR_NOTAVAILABLE;
1175
1176 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1177 if (FAILED(hr))
1178 return D3DERR_INVALIDCALL;
1179
1180 if (mip_levels > image_info.MipLevels)
1181 {
1182 FIXME("Generation of mipmaps for volume textures is not implemented yet\n");
1183 mip_levels = image_info.MipLevels;
1184 }
1185
1186 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1187 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1188 {
1189 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
1190 tex = buftex;
1191 }
1192 else
1193 {
1194 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1195 buftex = NULL;
1196 }
1197
1198 if (FAILED(hr)) return hr;
1199
1200 hr = load_volume_texture_from_dds(tex, data, palette, filter, color_key, &image_info);
1201 if (FAILED(hr))
1202 {
1203 IDirect3DVolumeTexture9_Release(tex);
1204 return hr;
1205 }
1206
1207 if (buftex)
1208 {
1209 hr = D3DXCreateVolumeTexture(device, width, height, depth, mip_levels, usage, format, pool, &tex);
1210 if (FAILED(hr))
1211 {
1212 IDirect3DVolumeTexture9_Release(buftex);
1213 return hr;
1214 }
1215
1216 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1217 IDirect3DVolumeTexture9_Release(buftex);
1218 }
1219
1220 if (info)
1221 *info = image_info;
1222
1223 *volume_texture = tex;
1224 return D3D_OK;
1225 }
1226
1227 static inline void fill_texture(const struct pixel_format_desc *format, BYTE *pos, const D3DXVECTOR4 *value)
1228 {
1229 DWORD c;
1230
1231 for (c = 0; c < format->bytes_per_pixel; c++)
1232 pos[c] = 0;
1233
1234 for (c = 0; c < 4; c++)
1235 {
1236 float comp_value;
1237 DWORD i, v = 0, mask32 = format->bits[c] == 32 ? ~0U : ((1 << format->bits[c]) - 1);
1238
1239 switch (c)
1240 {
1241 case 0: /* Alpha */
1242 comp_value = value->w;
1243 break;
1244 case 1: /* Red */
1245 comp_value = value->x;
1246 break;
1247 case 2: /* Green */
1248 comp_value = value->y;
1249 break;
1250 case 3: /* Blue */
1251 comp_value = value->z;
1252 break;
1253 }
1254
1255 if (format->type == FORMAT_ARGBF16)
1256 v = float_32_to_16(comp_value);
1257 else if (format->type == FORMAT_ARGBF)
1258 v = *(DWORD *)&comp_value;
1259 else if (format->type == FORMAT_ARGB)
1260 v = comp_value * ((1 << format->bits[c]) - 1) + 0.5f;
1261 else
1262 FIXME("Unhandled format type %#x\n", format->type);
1263
1264 for (i = 0; i < format->bits[c] + format->shift[c]; i += 8)
1265 {
1266 BYTE byte, mask;
1267
1268 if (format->shift[c] > i)
1269 {
1270 mask = mask32 << (format->shift[c] - i);
1271 byte = (v << (format->shift[c] - i)) & mask;
1272 }
1273 else
1274 {
1275 mask = mask32 >> (i - format->shift[c]);
1276 byte = (v >> (i - format->shift[c])) & mask;
1277 }
1278 pos[i / 8] |= byte;
1279 }
1280 }
1281 }
1282
1283 HRESULT WINAPI D3DXFillTexture(struct IDirect3DTexture9 *texture, LPD3DXFILL2D function, void *funcdata)
1284 {
1285 DWORD miplevels;
1286 DWORD m, x, y;
1287 D3DSURFACE_DESC desc;
1288 D3DLOCKED_RECT lock_rect;
1289 D3DXVECTOR4 value;
1290 D3DXVECTOR2 coord, size;
1291 const struct pixel_format_desc *format;
1292 BYTE *data;
1293
1294 if (texture == NULL || function == NULL)
1295 return D3DERR_INVALIDCALL;
1296
1297 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1298
1299 for (m = 0; m < miplevels; m++)
1300 {
1301 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture, m, &desc)))
1302 return D3DERR_INVALIDCALL;
1303
1304 format = get_format_info(desc.Format);
1305 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1306 {
1307 FIXME("Unsupported texture format %#x\n", desc.Format);
1308 return D3DERR_INVALIDCALL;
1309 }
1310
1311 if (FAILED(IDirect3DTexture9_LockRect(texture, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1312 return D3DERR_INVALIDCALL;
1313
1314 size.x = 1.0f / desc.Width;
1315 size.y = 1.0f / desc.Height;
1316
1317 data = lock_rect.pBits;
1318
1319 for (y = 0; y < desc.Height; y++)
1320 {
1321 /* The callback function expects the coordinates of the center
1322 of the texel */
1323 coord.y = (y + 0.5f) / desc.Height;
1324
1325 for (x = 0; x < desc.Width; x++)
1326 {
1327 coord.x = (x + 0.5f) / desc.Width;
1328
1329 function(&value, &coord, &size, funcdata);
1330
1331 fill_texture(format, data + y * lock_rect.Pitch + x * format->bytes_per_pixel, &value);
1332 }
1333 }
1334 IDirect3DTexture9_UnlockRect(texture, m);
1335 }
1336
1337 return D3D_OK;
1338 }
1339
1340 HRESULT WINAPI D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9 *device,
1341 const void *src_data,
1342 UINT src_data_size,
1343 UINT size,
1344 UINT mip_levels,
1345 DWORD usage,
1346 D3DFORMAT format,
1347 D3DPOOL pool,
1348 DWORD filter,
1349 DWORD mip_filter,
1350 D3DCOLOR color_key,
1351 D3DXIMAGE_INFO *src_info,
1352 PALETTEENTRY *palette,
1353 IDirect3DCubeTexture9 **cube_texture)
1354 {
1355 HRESULT hr;
1356 D3DCAPS9 caps;
1357 UINT loaded_miplevels;
1358 D3DXIMAGE_INFO img_info;
1359 BOOL dynamic_texture;
1360 BOOL file_size = FALSE;
1361 BOOL file_format = FALSE;
1362 BOOL file_mip_levels = FALSE;
1363 IDirect3DCubeTexture9 *tex, *buftex;
1364
1365 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device,
1366 src_data, src_data_size, size, mip_levels, usage, format, pool, filter, mip_filter,
1367 color_key, src_info, palette, cube_texture);
1368
1369 if (!device || !cube_texture || !src_data || !src_data_size)
1370 return D3DERR_INVALIDCALL;
1371
1372 hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &img_info);
1373 if (FAILED(hr))
1374 return hr;
1375
1376 if (img_info.ImageFileFormat != D3DXIFF_DDS)
1377 return D3DXERR_INVALIDDATA;
1378
1379 if (img_info.Width != img_info.Height)
1380 return D3DXERR_INVALIDDATA;
1381
1382 if (size == 0 || size == D3DX_DEFAULT_NONPOW2)
1383 size = img_info.Width;
1384 if (size == D3DX_DEFAULT)
1385 size = make_pow2(img_info.Width);
1386
1387 if (format == D3DFMT_UNKNOWN || format == D3DX_DEFAULT)
1388 format = img_info.Format;
1389
1390 if (size == D3DX_FROM_FILE)
1391 {
1392 file_size = TRUE;
1393 size = img_info.Width;
1394 }
1395
1396 if (format == D3DFMT_FROM_FILE)
1397 {
1398 file_format = TRUE;
1399 format = img_info.Format;
1400 }
1401
1402 if (mip_levels == D3DX_FROM_FILE)
1403 {
1404 file_mip_levels = TRUE;
1405 mip_levels = img_info.MipLevels;
1406 }
1407
1408 hr = D3DXCheckCubeTextureRequirements(device, &size, &mip_levels, usage, &format, pool);
1409 if (FAILED(hr))
1410 return hr;
1411
1412 if ((file_size && size != img_info.Width)
1413 || (file_format && format != img_info.Format)
1414 || (file_mip_levels && mip_levels != img_info.MipLevels))
1415 return D3DERR_NOTAVAILABLE;
1416
1417 hr = IDirect3DDevice9_GetDeviceCaps(device, &caps);
1418 if (FAILED(hr))
1419 return D3DERR_INVALIDCALL;
1420
1421 if (mip_levels > img_info.MipLevels && (D3DFMT_DXT1 <= img_info.Format && img_info.Format <= D3DFMT_DXT5))
1422 {
1423 FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
1424 mip_levels = img_info.MipLevels;
1425 }
1426
1427 dynamic_texture = (caps.Caps2 & D3DCAPS2_DYNAMICTEXTURES) && (usage & D3DUSAGE_DYNAMIC);
1428 if (pool == D3DPOOL_DEFAULT && !dynamic_texture)
1429 {
1430 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, D3DPOOL_SYSTEMMEM, &buftex);
1431 tex = buftex;
1432 }
1433 else
1434 {
1435 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1436 buftex = NULL;
1437 }
1438 if (FAILED(hr))
1439 return hr;
1440
1441 hr = load_cube_texture_from_dds(tex, src_data, palette, filter, color_key, &img_info);
1442 if (FAILED(hr))
1443 {
1444 IDirect3DCubeTexture9_Release(tex);
1445 return hr;
1446 }
1447
1448 loaded_miplevels = min(IDirect3DCubeTexture9_GetLevelCount(tex), img_info.MipLevels);
1449 hr = D3DXFilterTexture((IDirect3DBaseTexture9*) tex, palette, loaded_miplevels - 1, mip_filter);
1450 if (FAILED(hr))
1451 {
1452 IDirect3DCubeTexture9_Release(tex);
1453 return hr;
1454 }
1455
1456 if (buftex)
1457 {
1458 hr = D3DXCreateCubeTexture(device, size, mip_levels, usage, format, pool, &tex);
1459 if (FAILED(hr))
1460 {
1461 IDirect3DCubeTexture9_Release(buftex);
1462 return hr;
1463 }
1464
1465 IDirect3DDevice9_UpdateTexture(device, (IDirect3DBaseTexture9 *)buftex, (IDirect3DBaseTexture9 *)tex);
1466 IDirect3DCubeTexture9_Release(buftex);
1467 }
1468
1469 if (src_info)
1470 *src_info = img_info;
1471
1472 *cube_texture = tex;
1473 return D3D_OK;
1474 }
1475
1476
1477 HRESULT WINAPI D3DXCreateCubeTextureFromFileA(IDirect3DDevice9 *device,
1478 const char *src_filename,
1479 IDirect3DCubeTexture9 **cube_texture)
1480 {
1481 int len;
1482 HRESULT hr;
1483 WCHAR *filename;
1484 void *data;
1485 DWORD data_size;
1486
1487 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_a(src_filename), cube_texture);
1488
1489 if (!src_filename) return D3DERR_INVALIDCALL;
1490
1491 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1492 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1493 if (!filename) return E_OUTOFMEMORY;
1494 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1495
1496 hr = map_view_of_file(filename, &data, &data_size);
1497 if (FAILED(hr))
1498 {
1499 HeapFree(GetProcessHeap(), 0, filename);
1500 return D3DXERR_INVALIDDATA;
1501 }
1502
1503 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1504 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1505
1506 UnmapViewOfFile(data);
1507 HeapFree(GetProcessHeap(), 0, filename);
1508 return hr;
1509 }
1510
1511 HRESULT WINAPI D3DXCreateCubeTextureFromFileW(IDirect3DDevice9 *device,
1512 const WCHAR *src_filename,
1513 IDirect3DCubeTexture9 **cube_texture)
1514 {
1515 HRESULT hr;
1516 void *data;
1517 DWORD data_size;
1518
1519 TRACE("(%p, %s, %p): relay\n", device, wine_dbgstr_w(src_filename), cube_texture);
1520
1521 hr = map_view_of_file(src_filename, &data, &data_size);
1522 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1523
1524 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, D3DX_DEFAULT, D3DX_DEFAULT,
1525 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, cube_texture);
1526
1527 UnmapViewOfFile(data);
1528 return hr;
1529 }
1530
1531 HRESULT WINAPI D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9 *device,
1532 const char *src_filename,
1533 UINT size,
1534 UINT mip_levels,
1535 DWORD usage,
1536 D3DFORMAT format,
1537 D3DPOOL pool,
1538 DWORD filter,
1539 DWORD mip_filter,
1540 D3DCOLOR color_key,
1541 D3DXIMAGE_INFO *image_info,
1542 PALETTEENTRY *palette,
1543 IDirect3DCubeTexture9 **cube_texture)
1544 {
1545 int len;
1546 HRESULT hr;
1547 WCHAR *filename;
1548 void *data;
1549 DWORD data_size;
1550
1551 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1552 device, wine_dbgstr_a(src_filename), size, mip_levels, usage, format,
1553 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1554
1555 if (!src_filename) return D3DERR_INVALIDCALL;
1556
1557 len = MultiByteToWideChar(CP_ACP, 0, src_filename, -1, NULL, 0);
1558 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1559 if (!filename) return E_OUTOFMEMORY;
1560 MultiByteToWideChar(CP_ACP, 0, src_filename, -1, filename, len);
1561
1562 hr = map_view_of_file(filename, &data, &data_size);
1563 if (FAILED(hr))
1564 {
1565 HeapFree(GetProcessHeap(), 0, filename);
1566 return D3DXERR_INVALIDDATA;
1567 }
1568
1569 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1570 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1571
1572 UnmapViewOfFile(data);
1573 HeapFree(GetProcessHeap(), 0, filename);
1574 return hr;
1575 }
1576
1577 HRESULT WINAPI D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9 *device,
1578 const WCHAR *src_filename,
1579 UINT size,
1580 UINT mip_levels,
1581 DWORD usage,
1582 D3DFORMAT format,
1583 D3DPOOL pool,
1584 DWORD filter,
1585 DWORD mip_filter,
1586 D3DCOLOR color_key,
1587 D3DXIMAGE_INFO *image_info,
1588 PALETTEENTRY *palette,
1589 IDirect3DCubeTexture9 **cube_texture)
1590 {
1591 HRESULT hr;
1592 void *data;
1593 DWORD data_size;
1594
1595 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1596 device, wine_dbgstr_w(src_filename), size, mip_levels, usage, format,
1597 pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1598
1599 hr = map_view_of_file(src_filename, &data, &data_size);
1600 if (FAILED(hr)) return D3DXERR_INVALIDDATA;
1601
1602 hr = D3DXCreateCubeTextureFromFileInMemoryEx(device, data, data_size, size, mip_levels,
1603 usage, format, pool, filter, mip_filter, color_key, image_info, palette, cube_texture);
1604
1605 UnmapViewOfFile(data);
1606 return hr;
1607 }
1608
1609 enum cube_coord
1610 {
1611 XCOORD = 0,
1612 XCOORDINV = 1,
1613 YCOORD = 2,
1614 YCOORDINV = 3,
1615 ZERO = 4,
1616 ONE = 5
1617 };
1618
1619 static float get_cube_coord(enum cube_coord coord, unsigned int x, unsigned int y, unsigned int size)
1620 {
1621 switch (coord)
1622 {
1623 case XCOORD:
1624 return x + 0.5f;
1625 case XCOORDINV:
1626 return size - x - 0.5f;
1627 case YCOORD:
1628 return y + 0.5f;
1629 case YCOORDINV:
1630 return size - y - 0.5f;
1631 case ZERO:
1632 return 0.0f;
1633 case ONE:
1634 return size;
1635 default:
1636 ERR("Unexpected coordinate value\n");
1637 return 0.0f;
1638 }
1639 }
1640
1641 HRESULT WINAPI D3DXFillCubeTexture(struct IDirect3DCubeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1642 {
1643 DWORD miplevels;
1644 DWORD m, x, y, f;
1645 D3DSURFACE_DESC desc;
1646 D3DLOCKED_RECT lock_rect;
1647 D3DXVECTOR4 value;
1648 D3DXVECTOR3 coord, size;
1649 const struct pixel_format_desc *format;
1650 BYTE *data;
1651 static const enum cube_coord coordmap[6][3] =
1652 {
1653 {ONE, YCOORDINV, XCOORDINV},
1654 {ZERO, YCOORDINV, XCOORD},
1655 {XCOORD, ONE, YCOORD},
1656 {XCOORD, ZERO, YCOORDINV},
1657 {XCOORD, YCOORDINV, ONE},
1658 {XCOORDINV, YCOORDINV, ZERO}
1659 };
1660
1661 if (texture == NULL || function == NULL)
1662 return D3DERR_INVALIDCALL;
1663
1664 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1665
1666 for (m = 0; m < miplevels; m++)
1667 {
1668 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture, m, &desc)))
1669 return D3DERR_INVALIDCALL;
1670
1671 format = get_format_info(desc.Format);
1672 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1673 {
1674 FIXME("Unsupported texture format %#x\n", desc.Format);
1675 return D3DERR_INVALIDCALL;
1676 }
1677
1678 for (f = 0; f < 6; f++)
1679 {
1680 if (FAILED(IDirect3DCubeTexture9_LockRect(texture, f, m, &lock_rect, NULL, D3DLOCK_DISCARD)))
1681 return D3DERR_INVALIDCALL;
1682
1683 size.x = (f == 0) || (f == 1) ? 0.0f : 2.0f / desc.Width;
1684 size.y = (f == 2) || (f == 3) ? 0.0f : 2.0f / desc.Width;
1685 size.z = (f == 4) || (f == 5) ? 0.0f : 2.0f / desc.Width;
1686
1687 data = lock_rect.pBits;
1688
1689 for (y = 0; y < desc.Height; y++)
1690 {
1691 for (x = 0; x < desc.Width; x++)
1692 {
1693 coord.x = get_cube_coord(coordmap[f][0], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1694 coord.y = get_cube_coord(coordmap[f][1], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1695 coord.z = get_cube_coord(coordmap[f][2], x, y, desc.Width) / desc.Width * 2.0f - 1.0f;
1696
1697 function(&value, &coord, &size, funcdata);
1698
1699 fill_texture(format, data + y * lock_rect.Pitch + x * format->bytes_per_pixel, &value);
1700 }
1701 }
1702 IDirect3DCubeTexture9_UnlockRect(texture, f, m);
1703 }
1704 }
1705
1706 return D3D_OK;
1707 }
1708
1709 HRESULT WINAPI D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9 *texture, LPD3DXFILL3D function, void *funcdata)
1710 {
1711 DWORD miplevels;
1712 DWORD m, x, y, z;
1713 D3DVOLUME_DESC desc;
1714 D3DLOCKED_BOX lock_box;
1715 D3DXVECTOR4 value;
1716 D3DXVECTOR3 coord, size;
1717 const struct pixel_format_desc *format;
1718 BYTE *data;
1719
1720 if (texture == NULL || function == NULL)
1721 return D3DERR_INVALIDCALL;
1722
1723 miplevels = IDirect3DBaseTexture9_GetLevelCount(texture);
1724
1725 for (m = 0; m < miplevels; m++)
1726 {
1727 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture, m, &desc)))
1728 return D3DERR_INVALIDCALL;
1729
1730 format = get_format_info(desc.Format);
1731 if (format->type != FORMAT_ARGB && format->type != FORMAT_ARGBF16 && format->type != FORMAT_ARGBF)
1732 {
1733 FIXME("Unsupported texture format %#x\n", desc.Format);
1734 return D3DERR_INVALIDCALL;
1735 }
1736
1737 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture, m, &lock_box, NULL, D3DLOCK_DISCARD)))
1738 return D3DERR_INVALIDCALL;
1739
1740 size.x = 1.0f / desc.Width;
1741 size.y = 1.0f / desc.Height;
1742 size.z = 1.0f / desc.Depth;
1743
1744 data = lock_box.pBits;
1745
1746 for (z = 0; z < desc.Depth; z++)
1747 {
1748 /* The callback function expects the coordinates of the center
1749 of the texel */
1750 coord.z = (z + 0.5f) / desc.Depth;
1751
1752 for (y = 0; y < desc.Height; y++)
1753 {
1754 coord.y = (y + 0.5f) / desc.Height;
1755
1756 for (x = 0; x < desc.Width; x++)
1757 {
1758 coord.x = (x + 0.5f) / desc.Width;
1759
1760 function(&value, &coord, &size, funcdata);
1761
1762 fill_texture(format, data + z * lock_box.SlicePitch + y * lock_box.RowPitch
1763 + x * format->bytes_per_pixel, &value);
1764 }
1765 }
1766 }
1767 IDirect3DVolumeTexture9_UnlockBox(texture, m);
1768 }
1769
1770 return D3D_OK;
1771 }
1772
1773 HRESULT WINAPI D3DXSaveTextureToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1774 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1775 {
1776 int len;
1777 WCHAR *filename;
1778 HRESULT hr;
1779 ID3DXBuffer *buffer;
1780
1781 TRACE("(%s, %#x, %p, %p): relay\n",
1782 wine_dbgstr_a(dst_filename), file_format, src_texture, src_palette);
1783
1784 if (!dst_filename) return D3DERR_INVALIDCALL;
1785
1786 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1787 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1788 if (!filename) return E_OUTOFMEMORY;
1789 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1790
1791 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1792 if (SUCCEEDED(hr))
1793 {
1794 hr = write_buffer_to_file(filename, buffer);
1795 ID3DXBuffer_Release(buffer);
1796 }
1797
1798 HeapFree(GetProcessHeap(), 0, filename);
1799 return hr;
1800 }
1801
1802 HRESULT WINAPI D3DXSaveTextureToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1803 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1804 {
1805 HRESULT hr;
1806 ID3DXBuffer *buffer;
1807
1808 TRACE("(%s, %#x, %p, %p): relay\n",
1809 wine_dbgstr_w(dst_filename), file_format, src_texture, src_palette);
1810
1811 if (!dst_filename) return D3DERR_INVALIDCALL;
1812
1813 hr = D3DXSaveTextureToFileInMemory(&buffer, file_format, src_texture, src_palette);
1814 if (SUCCEEDED(hr))
1815 {
1816 hr = write_buffer_to_file(dst_filename, buffer);
1817 ID3DXBuffer_Release(buffer);
1818 }
1819
1820 return hr;
1821 }
1822
1823 HRESULT WINAPI D3DXSaveTextureToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1824 IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
1825 {
1826 HRESULT hr;
1827 D3DRESOURCETYPE type;
1828 IDirect3DSurface9 *surface;
1829
1830 TRACE("(%p, %#x, %p, %p)\n",
1831 dst_buffer, file_format, src_texture, src_palette);
1832
1833 if (!dst_buffer || !src_texture) return D3DERR_INVALIDCALL;
1834
1835 if (file_format == D3DXIFF_DDS)
1836 {
1837 FIXME("DDS file format isn't supported yet\n");
1838 return E_NOTIMPL;
1839 }
1840
1841 type = IDirect3DBaseTexture9_GetType(src_texture);
1842 switch (type)
1843 {
1844 case D3DRTYPE_TEXTURE:
1845 case D3DRTYPE_CUBETEXTURE:
1846 hr = get_surface(type, src_texture, D3DCUBEMAP_FACE_POSITIVE_X, 0, &surface);
1847 break;
1848 case D3DRTYPE_VOLUMETEXTURE:
1849 FIXME("Volume textures aren't supported yet\n");
1850 return E_NOTIMPL;
1851 default:
1852 return D3DERR_INVALIDCALL;
1853 }
1854
1855 if (SUCCEEDED(hr))
1856 {
1857 hr = D3DXSaveSurfaceToFileInMemory(dst_buffer, file_format, surface, src_palette, NULL);
1858 IDirect3DSurface9_Release(surface);
1859 }
1860
1861 return hr;
1862 }