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