2 * Copyright 2009 Tony Wasserka
3 * Copyright 2010 Christian Costa
4 * Copyright 2010 Owen Rudge for CodeWeavers
5 * Copyright 2010 Matteo Bruni for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/unicode.h"
23 #include "wine/debug.h"
24 #include "d3dx9_36_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
28 /* Returns TRUE if num is a power of 2, FALSE if not, or if 0 */
29 static BOOL
is_pow2(UINT num
)
31 return !(num
& (num
- 1));
34 /* Returns the smallest power of 2 which is greater than or equal to num */
35 static UINT
make_pow2(UINT num
)
39 /* In the unlikely event somebody passes a large value, make sure we don't enter an infinite loop */
40 if (num
>= 0x80000000)
49 static HRESULT
get_surface(D3DRESOURCETYPE type
, struct IDirect3DBaseTexture9
*tex
,
50 int face
, UINT level
, struct IDirect3DSurface9
**surf
)
54 case D3DRTYPE_TEXTURE
:
55 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9
*) tex
, level
, surf
);
56 case D3DRTYPE_CUBETEXTURE
:
57 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9
*) tex
, face
, level
, surf
);
59 ERR("Unexpected texture type\n");
64 HRESULT WINAPI
D3DXFilterTexture(IDirect3DBaseTexture9
*texture
,
65 const PALETTEENTRY
*palette
,
73 TRACE("(%p, %p, %u, %#x)\n", texture
, palette
, srclevel
, filter
);
76 return D3DERR_INVALIDCALL
;
78 if ((filter
& 0xFFFF) > D3DX_FILTER_BOX
&& filter
!= D3DX_DEFAULT
)
79 return D3DERR_INVALIDCALL
;
81 if (srclevel
== D3DX_DEFAULT
)
83 else if (srclevel
>= IDirect3DBaseTexture9_GetLevelCount(texture
))
84 return D3DERR_INVALIDCALL
;
86 switch (type
= IDirect3DBaseTexture9_GetType(texture
))
88 case D3DRTYPE_TEXTURE
:
89 case D3DRTYPE_CUBETEXTURE
:
91 IDirect3DSurface9
*topsurf
, *mipsurf
;
95 if (type
== D3DRTYPE_TEXTURE
)
98 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
103 IDirect3DCubeTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
106 if (filter
== D3DX_DEFAULT
)
108 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
))
109 filter
= D3DX_FILTER_BOX
;
111 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
114 for (i
= 0; i
< numfaces
; i
++)
116 level
= srclevel
+ 1;
117 hr
= get_surface(type
, texture
, i
, srclevel
, &topsurf
);
120 return D3DERR_INVALIDCALL
;
122 while (get_surface(type
, texture
, i
, level
, &mipsurf
) == D3D_OK
)
124 hr
= D3DXLoadSurfaceFromSurface(mipsurf
, palette
, NULL
, topsurf
, palette
, NULL
, filter
, 0);
125 IDirect3DSurface9_Release(topsurf
);
134 IDirect3DSurface9_Release(topsurf
);
142 case D3DRTYPE_VOLUMETEXTURE
:
145 int level
, level_count
;
146 IDirect3DVolume9
*top_volume
, *mip_volume
;
147 IDirect3DVolumeTexture9
*volume_texture
= (IDirect3DVolumeTexture9
*) texture
;
149 IDirect3DVolumeTexture9_GetLevelDesc(volume_texture
, srclevel
, &desc
);
151 if (filter
== D3DX_DEFAULT
)
153 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
) && is_pow2(desc
.Depth
))
154 filter
= D3DX_FILTER_BOX
;
156 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
159 hr
= IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, srclevel
, &top_volume
);
163 level_count
= IDirect3DVolumeTexture9_GetLevelCount(volume_texture
);
164 for (level
= srclevel
+ 1; level
< level_count
; level
++)
166 IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, level
, &mip_volume
);
167 hr
= D3DXLoadVolumeFromVolume(mip_volume
, palette
, NULL
, top_volume
, palette
, NULL
, filter
, 0);
168 IDirect3DVolume9_Release(top_volume
);
169 top_volume
= mip_volume
;
175 IDirect3DVolume9_Release(top_volume
);
183 return D3DERR_INVALIDCALL
;
187 HRESULT WINAPI
D3DXCheckTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
188 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
190 UINT w
= (width
&& *width
) ? *width
: 1;
191 UINT h
= (height
&& *height
) ? *height
: 1;
193 D3DDEVICE_CREATION_PARAMETERS params
;
194 IDirect3D9
*d3d
= NULL
;
197 D3DFORMAT usedformat
= D3DFMT_UNKNOWN
;
198 const struct pixel_format_desc
*fmt
;
200 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, miplevels
, usage
, format
, pool
);
203 return D3DERR_INVALIDCALL
;
206 if (usage
== D3DX_DEFAULT
)
208 if (usage
& (D3DUSAGE_WRITEONLY
| D3DUSAGE_DONOTCLIP
| D3DUSAGE_POINTS
| D3DUSAGE_RTPATCHES
| D3DUSAGE_NPATCHES
))
209 return D3DERR_INVALIDCALL
;
212 if ((pool
!= D3DPOOL_DEFAULT
) && (pool
!= D3DPOOL_MANAGED
) && (pool
!= D3DPOOL_SYSTEMMEM
) && (pool
!= D3DPOOL_SCRATCH
))
213 return D3DERR_INVALIDCALL
;
218 TRACE("Requested format %x\n", *format
);
219 usedformat
= *format
;
222 hr
= IDirect3DDevice9_GetDirect3D(device
, &d3d
);
227 hr
= IDirect3DDevice9_GetCreationParameters(device
, ¶ms
);
232 hr
= IDirect3DDevice9_GetDisplayMode(device
, 0, &mode
);
237 if ((usedformat
== D3DFMT_UNKNOWN
) || (usedformat
== D3DX_DEFAULT
))
238 usedformat
= D3DFMT_A8R8G8B8
;
240 fmt
= get_format_info(usedformat
);
242 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
, mode
.Format
,
243 usage
, D3DRTYPE_TEXTURE
, usedformat
);
247 int bestscore
= INT_MIN
, i
= 0, j
;
248 unsigned int channels
;
249 const struct pixel_format_desc
*curfmt
, *bestfmt
= NULL
;
251 TRACE("Requested format not supported, looking for a fallback.\n");
255 FIXME("Pixel format %x not handled\n", usedformat
);
259 allow_24bits
= fmt
->bytes_per_pixel
== 3;
260 channels
= (fmt
->bits
[0] ? 1 : 0) + (fmt
->bits
[1] ? 1 : 0)
261 + (fmt
->bits
[2] ? 1 : 0) + (fmt
->bits
[3] ? 1 : 0);
262 usedformat
= D3DFMT_UNKNOWN
;
264 while ((curfmt
= get_format_info_idx(i
)))
266 unsigned int curchannels
= (curfmt
->bits
[0] ? 1 : 0) + (curfmt
->bits
[1] ? 1 : 0)
267 + (curfmt
->bits
[2] ? 1 : 0) + (curfmt
->bits
[3] ? 1 : 0);
272 if (curchannels
< channels
)
274 if (curfmt
->bytes_per_pixel
== 3 && !allow_24bits
)
277 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
,
278 mode
.Format
, usage
, D3DRTYPE_TEXTURE
, curfmt
->format
);
282 /* This format can be used, let's evaluate it.
283 Weights chosen quite arbitrarily... */
284 score
= 16 - 4 * (curchannels
- channels
);
286 for (j
= 0; j
< 4; j
++)
288 int diff
= curfmt
->bits
[j
] - fmt
->bits
[j
];
289 score
+= 16 - (diff
< 0 ? -diff
* 4 : diff
);
292 if (score
> bestscore
)
295 usedformat
= curfmt
->format
;
303 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
304 return D3DERR_INVALIDCALL
;
306 if ((w
== D3DX_DEFAULT
) && (h
== D3DX_DEFAULT
))
308 else if (w
== D3DX_DEFAULT
)
309 w
= (height
? h
: 256);
310 else if (h
== D3DX_DEFAULT
)
311 h
= (width
? w
: 256);
313 if (fmt
->block_width
!= 1 || fmt
->block_height
!= 1)
315 if (w
< fmt
->block_width
)
316 w
= fmt
->block_width
;
317 if (h
< fmt
->block_height
)
318 h
= fmt
->block_height
;
321 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(w
)))
324 if (w
> caps
.MaxTextureWidth
)
325 w
= caps
.MaxTextureWidth
;
327 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(h
)))
330 if (h
> caps
.MaxTextureHeight
)
331 h
= caps
.MaxTextureHeight
;
333 if (caps
.TextureCaps
& D3DPTEXTURECAPS_SQUAREONLY
)
347 if (miplevels
&& (usage
& D3DUSAGE_AUTOGENMIPMAP
))
354 UINT max_mipmaps
= 1;
356 if (!width
&& !height
)
357 max_mipmaps
= 9; /* number of mipmaps in a 256x256 texture */
360 UINT max_dimen
= max(w
, h
);
362 while (max_dimen
> 1)
369 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
370 *miplevels
= max_mipmaps
;
376 IDirect3D9_Release(d3d
);
381 if (usedformat
== D3DFMT_UNKNOWN
)
383 WARN("Couldn't find a suitable pixel format\n");
384 return D3DERR_NOTAVAILABLE
;
387 TRACE("Format chosen: %x\n", usedformat
);
389 *format
= usedformat
;
394 HRESULT WINAPI
D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*size
,
395 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
398 UINT s
= (size
&& *size
) ? *size
: 256;
401 TRACE("(%p, %p, %p, %u, %p, %u)\n", device
, size
, miplevels
, usage
, format
, pool
);
403 if (s
== D3DX_DEFAULT
)
406 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
407 return D3DERR_INVALIDCALL
;
409 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP
))
410 return D3DERR_NOTAVAILABLE
;
412 /* ensure width/height is power of 2 */
413 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP_POW2
) && (!is_pow2(s
)))
416 hr
= D3DXCheckTextureRequirements(device
, &s
, &s
, miplevels
, usage
, format
, pool
);
418 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPCUBEMAP
))
430 HRESULT WINAPI
D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
431 UINT
*depth
, UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
434 UINT w
= width
? *width
: D3DX_DEFAULT
;
435 UINT h
= height
? *height
: D3DX_DEFAULT
;
436 UINT d
= (depth
&& *depth
) ? *depth
: 1;
439 TRACE("(%p, %p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, depth
, miplevels
,
440 usage
, format
, pool
);
442 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
443 return D3DERR_INVALIDCALL
;
445 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP
))
446 return D3DERR_NOTAVAILABLE
;
448 hr
= D3DXCheckTextureRequirements(device
, &w
, &h
, NULL
, usage
, format
, pool
);
449 if (d
== D3DX_DEFAULT
)
452 /* ensure width/height is power of 2 */
453 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP_POW2
) &&
454 (!is_pow2(w
) || !is_pow2(h
) || !is_pow2(d
)))
461 if (w
> caps
.MaxVolumeExtent
)
462 w
= caps
.MaxVolumeExtent
;
463 if (h
> caps
.MaxVolumeExtent
)
464 h
= caps
.MaxVolumeExtent
;
465 if (d
> caps
.MaxVolumeExtent
)
466 d
= caps
.MaxVolumeExtent
;
470 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPVOLUMEMAP
))
472 else if ((usage
& D3DUSAGE_AUTOGENMIPMAP
))
479 UINT max_mipmaps
= 1;
480 UINT max_dimen
= max(max(w
, h
), d
);
482 while (max_dimen
> 1)
488 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
489 *miplevels
= max_mipmaps
;
503 HRESULT WINAPI
D3DXCreateTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
,
504 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DTexture9
**texture
)
508 TRACE("device %p, width %u, height %u, miplevels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
509 device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
511 if (!device
|| !texture
)
512 return D3DERR_INVALIDCALL
;
514 if (FAILED(hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
)))
517 return IDirect3DDevice9_CreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
, NULL
);
520 HRESULT WINAPI
D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9
*device
, const void *srcdata
,
521 UINT srcdatasize
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
522 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
523 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
525 IDirect3DTexture9
**texptr
;
526 IDirect3DTexture9
*buftex
;
527 IDirect3DSurface9
*surface
;
528 BOOL file_width
= FALSE
, file_height
= FALSE
;
529 BOOL file_format
= FALSE
, file_miplevels
= FALSE
;
530 BOOL dynamic_texture
;
531 D3DXIMAGE_INFO imginfo
;
532 UINT loaded_miplevels
, skip_levels
;
536 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u,"
537 " usage %#x, format %#x, pool %#x, filter %#x, mipfilter %#x, colorkey %#x,"
538 " srcinfo %p, palette %p, texture %p.\n",
539 device
, srcdata
, srcdatasize
, width
, height
, miplevels
, usage
, format
, pool
,
540 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
542 /* check for invalid parameters */
543 if (!device
|| !texture
|| !srcdata
|| !srcdatasize
)
544 return D3DERR_INVALIDCALL
;
546 hr
= D3DXGetImageInfoFromFileInMemory(srcdata
, srcdatasize
, &imginfo
);
549 FIXME("Unrecognized file format, returning failure.\n");
554 /* handle default values */
555 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
556 width
= imginfo
.Width
;
558 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
559 height
= imginfo
.Height
;
561 if (width
== D3DX_DEFAULT
)
562 width
= make_pow2(imginfo
.Width
);
564 if (height
== D3DX_DEFAULT
)
565 height
= make_pow2(imginfo
.Height
);
567 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
568 format
= imginfo
.Format
;
570 if (width
== D3DX_FROM_FILE
)
573 width
= imginfo
.Width
;
576 if (height
== D3DX_FROM_FILE
)
579 height
= imginfo
.Height
;
582 if (format
== D3DFMT_FROM_FILE
)
585 format
= imginfo
.Format
;
588 if (miplevels
== D3DX_FROM_FILE
)
590 file_miplevels
= TRUE
;
591 miplevels
= imginfo
.MipLevels
;
594 skip_levels
= mipfilter
!= D3DX_DEFAULT
? mipfilter
>> D3DX_SKIP_DDS_MIP_LEVELS_SHIFT
: 0;
595 if (skip_levels
&& imginfo
.MipLevels
> skip_levels
)
597 TRACE("Skipping the first %u (of %u) levels of a DDS mipmapped texture.\n",
598 skip_levels
, imginfo
.MipLevels
);
599 TRACE("Texture level 0 dimensions are %ux%u.\n", imginfo
.Width
, imginfo
.Height
);
600 width
>>= skip_levels
;
601 height
>>= skip_levels
;
602 miplevels
-= skip_levels
;
609 /* fix texture creation parameters */
610 hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
);
613 FIXME("Couldn't find suitable texture parameters.\n");
618 if (imginfo
.MipLevels
< miplevels
&& (D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
))
620 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet\n");
621 miplevels
= imginfo
.MipLevels
;
623 if (imginfo
.ResourceType
== D3DRTYPE_VOLUMETEXTURE
624 && D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
&& miplevels
> 1)
626 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet.\n");
630 if (((file_width
) && (width
!= imginfo
.Width
)) ||
631 ((file_height
) && (height
!= imginfo
.Height
)) ||
632 ((file_format
) && (format
!= imginfo
.Format
)) ||
633 ((file_miplevels
) && (miplevels
!= imginfo
.MipLevels
)))
635 return D3DERR_NOTAVAILABLE
;
638 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
639 return D3DERR_INVALIDCALL
;
641 /* Create the to-be-filled texture */
642 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
643 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
645 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
650 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
656 FIXME("Texture creation failed.\n");
661 TRACE("Texture created correctly. Now loading the texture data into it.\n");
662 if (imginfo
.ImageFileFormat
!= D3DXIFF_DDS
)
664 IDirect3DTexture9_GetSurfaceLevel(*texptr
, 0, &surface
);
665 hr
= D3DXLoadSurfaceFromFileInMemory(surface
, palette
, NULL
, srcdata
, srcdatasize
, NULL
, filter
, colorkey
, NULL
);
666 IDirect3DSurface9_Release(surface
);
667 loaded_miplevels
= min(IDirect3DTexture9_GetLevelCount(*texptr
), imginfo
.MipLevels
);
671 hr
= load_texture_from_dds(*texptr
, srcdata
, palette
, filter
, colorkey
, &imginfo
, skip_levels
,
677 FIXME("Texture loading failed.\n");
678 IDirect3DTexture9_Release(*texptr
);
683 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) *texptr
, palette
, loaded_miplevels
- 1, mipfilter
);
686 FIXME("Texture filtering failed.\n");
687 IDirect3DTexture9_Release(*texptr
);
692 /* Move the data to the actual texture if necessary */
693 if (texptr
== &buftex
)
695 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
699 IDirect3DTexture9_Release(buftex
);
704 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)(*texture
));
705 IDirect3DTexture9_Release(buftex
);
714 HRESULT WINAPI
D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
715 const void *srcdata
, UINT srcdatasize
, struct IDirect3DTexture9
**texture
)
717 TRACE("(%p, %p, %d, %p)\n", device
, srcdata
, srcdatasize
, texture
);
719 return D3DXCreateTextureFromFileInMemoryEx(device
, srcdata
, srcdatasize
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
720 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
723 HRESULT WINAPI
D3DXCreateTextureFromFileExW(struct IDirect3DDevice9
*device
, const WCHAR
*srcfile
,
724 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
725 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
726 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
732 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
733 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
734 device
, debugstr_w(srcfile
), width
, height
, miplevels
, usage
, format
,
735 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
738 return D3DERR_INVALIDCALL
;
740 hr
= map_view_of_file(srcfile
, &buffer
, &size
);
742 return D3DXERR_INVALIDDATA
;
744 hr
= D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
, usage
, format
, pool
,
745 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
747 UnmapViewOfFile(buffer
);
752 HRESULT WINAPI
D3DXCreateTextureFromFileExA(struct IDirect3DDevice9
*device
, const char *srcfile
,
753 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
754 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
755 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
761 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
762 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
763 device
, debugstr_a(srcfile
), width
, height
, miplevels
, usage
, format
,
764 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
766 if (!device
|| !srcfile
|| !texture
)
767 return D3DERR_INVALIDCALL
;
769 len
= MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, NULL
, 0);
770 widename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*widename
));
771 MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, widename
, len
);
773 hr
= D3DXCreateTextureFromFileExW(device
, widename
, width
, height
, miplevels
,
774 usage
, format
, pool
, filter
, mipfilter
,
775 colorkey
, srcinfo
, palette
, texture
);
777 HeapFree(GetProcessHeap(), 0, widename
);
781 HRESULT WINAPI
D3DXCreateTextureFromFileA(struct IDirect3DDevice9
*device
,
782 const char *srcfile
, struct IDirect3DTexture9
**texture
)
784 TRACE("(%p, %s, %p)\n", device
, debugstr_a(srcfile
), texture
);
786 return D3DXCreateTextureFromFileExA(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
787 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
790 HRESULT WINAPI
D3DXCreateTextureFromFileW(struct IDirect3DDevice9
*device
,
791 const WCHAR
*srcfile
, struct IDirect3DTexture9
**texture
)
793 TRACE("(%p, %s, %p)\n", device
, debugstr_w(srcfile
), texture
);
795 return D3DXCreateTextureFromFileExW(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
796 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
800 HRESULT WINAPI
D3DXCreateTextureFromResourceA(struct IDirect3DDevice9
*device
,
801 HMODULE srcmodule
, const char *resource
, struct IDirect3DTexture9
**texture
)
803 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_a(resource
));
805 return D3DXCreateTextureFromResourceExA(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
806 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
809 HRESULT WINAPI
D3DXCreateTextureFromResourceW(struct IDirect3DDevice9
*device
,
810 HMODULE srcmodule
, const WCHAR
*resource
, struct IDirect3DTexture9
**texture
)
812 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_w(resource
));
814 return D3DXCreateTextureFromResourceExW(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
815 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
818 HRESULT WINAPI
D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
819 const char *resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
820 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
821 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
827 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
828 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
829 device
, srcmodule
, debugstr_a(resource
), width
, height
, miplevels
, usage
, format
,
830 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
832 if (!device
|| !texture
)
833 return D3DERR_INVALIDCALL
;
835 if (!(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_RCDATA
))
836 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
837 && !(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_BITMAP
)))
838 return D3DXERR_INVALIDDATA
;
840 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
841 return D3DXERR_INVALIDDATA
;
843 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
844 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
847 HRESULT WINAPI
D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
848 const WCHAR
*resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
849 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
850 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
856 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
857 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
858 device
, srcmodule
, debugstr_w(resource
), width
, height
, miplevels
, usage
, format
,
859 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
861 if (!device
|| !texture
)
862 return D3DERR_INVALIDCALL
;
864 if (!(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_RCDATA
))
865 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
866 && !(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_BITMAP
)))
867 return D3DXERR_INVALIDDATA
;
869 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
870 return D3DXERR_INVALIDDATA
;
872 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
873 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
876 HRESULT WINAPI
D3DXCreateCubeTexture(struct IDirect3DDevice9
*device
, UINT size
, UINT miplevels
,
877 DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DCubeTexture9
**texture
)
881 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device
, size
, miplevels
, usage
, format
,
884 if (!device
|| !texture
)
885 return D3DERR_INVALIDCALL
;
887 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &miplevels
, usage
, &format
, pool
);
891 TRACE("D3DXCheckCubeTextureRequirements failed\n");
895 return IDirect3DDevice9_CreateCubeTexture(device
, size
, miplevels
, usage
, format
, pool
, texture
, NULL
);
898 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
899 const void *data
, UINT datasize
, struct IDirect3DCubeTexture9
**texture
)
901 TRACE("(%p, %p, %u, %p)\n", device
, data
, datasize
, texture
);
903 return D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, datasize
, D3DX_DEFAULT
, D3DX_DEFAULT
,
904 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
907 HRESULT WINAPI
D3DXCreateVolumeTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
, UINT depth
,
908 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DVolumeTexture9
**texture
)
912 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device
, width
, height
, depth
,
913 miplevels
, usage
, format
, pool
, texture
);
915 if (!device
|| !texture
)
916 return D3DERR_INVALIDCALL
;
918 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
,
919 &miplevels
, usage
, &format
, pool
);
923 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
927 return IDirect3DDevice9_CreateVolumeTexture(device
, width
, height
, depth
, miplevels
,
928 usage
, format
, pool
, texture
, NULL
);
931 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9
*device
,
932 const char *filename
,
933 IDirect3DVolumeTexture9
**volume_texture
)
941 TRACE("(%p, %s, %p): relay\n",
942 device
, debugstr_a(filename
), volume_texture
);
944 if (!filename
) return D3DERR_INVALIDCALL
;
946 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
947 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
948 if (!filenameW
) return E_OUTOFMEMORY
;
949 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
951 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
952 HeapFree(GetProcessHeap(), 0, filenameW
);
953 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
955 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
956 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
958 UnmapViewOfFile(data
);
962 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9
*device
,
963 const WCHAR
*filename
,
964 IDirect3DVolumeTexture9
**volume_texture
)
970 TRACE("(%p, %s, %p): relay\n",
971 device
, debugstr_w(filename
), volume_texture
);
973 if (!filename
) return D3DERR_INVALIDCALL
;
975 hr
= map_view_of_file(filename
, &data
, &data_size
);
976 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
978 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
979 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
981 UnmapViewOfFile(data
);
985 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9
*device
,
986 const char *filename
,
997 D3DXIMAGE_INFO
*src_info
,
998 PALETTEENTRY
*palette
,
999 IDirect3DVolumeTexture9
**volume_texture
)
1007 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1008 device
, debugstr_a(filename
), width
, height
, depth
, mip_levels
,
1009 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1010 palette
, volume_texture
);
1012 if (!filename
) return D3DERR_INVALIDCALL
;
1014 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
1015 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1016 if (!filenameW
) return E_OUTOFMEMORY
;
1017 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
1019 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
1020 HeapFree(GetProcessHeap(), 0, filenameW
);
1021 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1023 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1024 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1027 UnmapViewOfFile(data
);
1031 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9
*device
,
1032 const WCHAR
*filename
,
1043 D3DXIMAGE_INFO
*src_info
,
1044 PALETTEENTRY
*palette
,
1045 IDirect3DVolumeTexture9
**volume_texture
)
1051 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1052 device
, debugstr_w(filename
), width
, height
, depth
, mip_levels
,
1053 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1054 palette
, volume_texture
);
1056 if (!filename
) return D3DERR_INVALIDCALL
;
1058 hr
= map_view_of_file(filename
, &data
, &data_size
);
1059 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1061 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1062 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1065 UnmapViewOfFile(data
);
1069 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9
*device
,
1072 IDirect3DVolumeTexture9
**volume_texture
)
1074 TRACE("(%p, %p, %u, %p): relay\n", device
, data
, data_size
, volume_texture
);
1076 return D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1077 D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1078 0, NULL
, NULL
, volume_texture
);
1081 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1094 D3DXIMAGE_INFO
*info
,
1095 PALETTEENTRY
*palette
,
1096 IDirect3DVolumeTexture9
**volume_texture
)
1100 D3DXIMAGE_INFO image_info
;
1101 BOOL dynamic_texture
;
1102 BOOL file_width
= FALSE
;
1103 BOOL file_height
= FALSE
;
1104 BOOL file_depth
= FALSE
;
1105 BOOL file_format
= FALSE
;
1106 BOOL file_mip_levels
= FALSE
;
1107 IDirect3DVolumeTexture9
*tex
, *buftex
;
1109 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1110 device
, data
, data_size
, width
, height
, depth
, mip_levels
, usage
, format
, pool
,
1111 filter
, mip_filter
, color_key
, info
, palette
, volume_texture
);
1113 if (!device
|| !data
|| !data_size
|| !volume_texture
)
1114 return D3DERR_INVALIDCALL
;
1116 hr
= D3DXGetImageInfoFromFileInMemory(data
, data_size
, &image_info
);
1117 if (FAILED(hr
)) return hr
;
1119 if (image_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1120 return D3DXERR_INVALIDDATA
;
1122 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
1123 width
= image_info
.Width
;
1124 if (width
== D3DX_DEFAULT
)
1125 width
= make_pow2(image_info
.Width
);
1127 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
1128 height
= image_info
.Height
;
1129 if (height
== D3DX_DEFAULT
)
1130 height
= make_pow2(image_info
.Height
);
1132 if (depth
== 0 || depth
== D3DX_DEFAULT_NONPOW2
)
1133 depth
= image_info
.Depth
;
1134 if (depth
== D3DX_DEFAULT
)
1135 depth
= make_pow2(image_info
.Depth
);
1137 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1138 format
= image_info
.Format
;
1140 if (width
== D3DX_FROM_FILE
)
1143 width
= image_info
.Width
;
1146 if (height
== D3DX_FROM_FILE
)
1149 height
= image_info
.Height
;
1152 if (depth
== D3DX_FROM_FILE
)
1155 depth
= image_info
.Depth
;
1158 if (format
== D3DFMT_FROM_FILE
)
1161 format
= image_info
.Format
;
1164 if (mip_levels
== D3DX_FROM_FILE
)
1166 file_mip_levels
= TRUE
;
1167 mip_levels
= image_info
.MipLevels
;
1170 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
, &mip_levels
, usage
, &format
, pool
);
1171 if (FAILED(hr
)) return hr
;
1173 if ((file_width
&& width
!= image_info
.Width
)
1174 || (file_height
&& height
!= image_info
.Height
)
1175 || (file_depth
&& depth
!= image_info
.Depth
)
1176 || (file_format
&& format
!= image_info
.Format
)
1177 || (file_mip_levels
&& mip_levels
!= image_info
.MipLevels
))
1178 return D3DERR_NOTAVAILABLE
;
1180 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1182 return D3DERR_INVALIDCALL
;
1184 if (mip_levels
> image_info
.MipLevels
)
1186 FIXME("Generation of mipmaps for volume textures is not implemented yet\n");
1187 mip_levels
= image_info
.MipLevels
;
1190 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1191 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1193 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1198 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1202 if (FAILED(hr
)) return hr
;
1204 hr
= load_volume_texture_from_dds(tex
, data
, palette
, filter
, color_key
, &image_info
);
1207 IDirect3DVolumeTexture9_Release(tex
);
1213 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1216 IDirect3DVolumeTexture9_Release(buftex
);
1220 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1221 IDirect3DVolumeTexture9_Release(buftex
);
1227 *volume_texture
= tex
;
1231 static inline void fill_texture(const struct pixel_format_desc
*format
, BYTE
*pos
, const D3DXVECTOR4
*value
)
1235 for (c
= 0; c
< format
->bytes_per_pixel
; c
++)
1238 for (c
= 0; c
< 4; c
++)
1241 DWORD i
, v
= 0, mask32
= format
->bits
[c
] == 32 ? ~0U : ((1 << format
->bits
[c
]) - 1);
1246 comp_value
= value
->w
;
1249 comp_value
= value
->x
;
1252 comp_value
= value
->y
;
1255 comp_value
= value
->z
;
1259 if (format
->type
== FORMAT_ARGBF16
)
1260 v
= float_32_to_16(comp_value
);
1261 else if (format
->type
== FORMAT_ARGBF
)
1262 v
= *(DWORD
*)&comp_value
;
1263 else if (format
->type
== FORMAT_ARGB
)
1264 v
= comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
;
1266 FIXME("Unhandled format type %#x\n", format
->type
);
1268 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1272 if (format
->shift
[c
] > i
)
1274 mask
= mask32
<< (format
->shift
[c
] - i
);
1275 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1279 mask
= mask32
>> (i
- format
->shift
[c
]);
1280 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1287 HRESULT WINAPI
D3DXFillTexture(struct IDirect3DTexture9
*texture
, LPD3DXFILL2D function
, void *funcdata
)
1291 D3DSURFACE_DESC desc
;
1292 D3DLOCKED_RECT lock_rect
;
1294 D3DXVECTOR2 coord
, size
;
1295 const struct pixel_format_desc
*format
;
1298 if (texture
== NULL
|| function
== NULL
)
1299 return D3DERR_INVALIDCALL
;
1301 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1303 for (m
= 0; m
< miplevels
; m
++)
1305 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture
, m
, &desc
)))
1306 return D3DERR_INVALIDCALL
;
1308 format
= get_format_info(desc
.Format
);
1309 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1311 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1312 return D3DERR_INVALIDCALL
;
1315 if (FAILED(IDirect3DTexture9_LockRect(texture
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1316 return D3DERR_INVALIDCALL
;
1318 size
.x
= 1.0f
/ desc
.Width
;
1319 size
.y
= 1.0f
/ desc
.Height
;
1321 data
= lock_rect
.pBits
;
1323 for (y
= 0; y
< desc
.Height
; y
++)
1325 /* The callback function expects the coordinates of the center
1327 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1329 for (x
= 0; x
< desc
.Width
; x
++)
1331 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1333 function(&value
, &coord
, &size
, funcdata
);
1335 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1338 IDirect3DTexture9_UnlockRect(texture
, m
);
1344 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1345 const void *src_data
,
1355 D3DXIMAGE_INFO
*src_info
,
1356 PALETTEENTRY
*palette
,
1357 IDirect3DCubeTexture9
**cube_texture
)
1361 UINT loaded_miplevels
;
1362 D3DXIMAGE_INFO img_info
;
1363 BOOL dynamic_texture
;
1364 BOOL file_size
= FALSE
;
1365 BOOL file_format
= FALSE
;
1366 BOOL file_mip_levels
= FALSE
;
1367 IDirect3DCubeTexture9
*tex
, *buftex
;
1369 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device
,
1370 src_data
, src_data_size
, size
, mip_levels
, usage
, format
, pool
, filter
, mip_filter
,
1371 color_key
, src_info
, palette
, cube_texture
);
1373 if (!device
|| !cube_texture
|| !src_data
|| !src_data_size
)
1374 return D3DERR_INVALIDCALL
;
1376 hr
= D3DXGetImageInfoFromFileInMemory(src_data
, src_data_size
, &img_info
);
1380 if (img_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1381 return D3DXERR_INVALIDDATA
;
1383 if (img_info
.Width
!= img_info
.Height
)
1384 return D3DXERR_INVALIDDATA
;
1386 if (size
== 0 || size
== D3DX_DEFAULT_NONPOW2
)
1387 size
= img_info
.Width
;
1388 if (size
== D3DX_DEFAULT
)
1389 size
= make_pow2(img_info
.Width
);
1391 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1392 format
= img_info
.Format
;
1394 if (size
== D3DX_FROM_FILE
)
1397 size
= img_info
.Width
;
1400 if (format
== D3DFMT_FROM_FILE
)
1403 format
= img_info
.Format
;
1406 if (mip_levels
== D3DX_FROM_FILE
)
1408 file_mip_levels
= TRUE
;
1409 mip_levels
= img_info
.MipLevels
;
1412 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &mip_levels
, usage
, &format
, pool
);
1416 if ((file_size
&& size
!= img_info
.Width
)
1417 || (file_format
&& format
!= img_info
.Format
)
1418 || (file_mip_levels
&& mip_levels
!= img_info
.MipLevels
))
1419 return D3DERR_NOTAVAILABLE
;
1421 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1423 return D3DERR_INVALIDCALL
;
1425 if (mip_levels
> img_info
.MipLevels
&& (D3DFMT_DXT1
<= img_info
.Format
&& img_info
.Format
<= D3DFMT_DXT5
))
1427 FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
1428 mip_levels
= img_info
.MipLevels
;
1431 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1432 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1434 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1439 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1445 hr
= load_cube_texture_from_dds(tex
, src_data
, palette
, filter
, color_key
, &img_info
);
1448 IDirect3DCubeTexture9_Release(tex
);
1452 loaded_miplevels
= min(IDirect3DCubeTexture9_GetLevelCount(tex
), img_info
.MipLevels
);
1453 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) tex
, palette
, loaded_miplevels
- 1, mip_filter
);
1456 IDirect3DCubeTexture9_Release(tex
);
1462 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1465 IDirect3DCubeTexture9_Release(buftex
);
1469 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1470 IDirect3DCubeTexture9_Release(buftex
);
1474 *src_info
= img_info
;
1476 *cube_texture
= tex
;
1481 HRESULT WINAPI
D3DXCreateCubeTextureFromFileA(IDirect3DDevice9
*device
,
1482 const char *src_filename
,
1483 IDirect3DCubeTexture9
**cube_texture
)
1491 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_a(src_filename
), cube_texture
);
1493 if (!src_filename
) return D3DERR_INVALIDCALL
;
1495 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1496 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1497 if (!filename
) return E_OUTOFMEMORY
;
1498 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1500 hr
= map_view_of_file(filename
, &data
, &data_size
);
1503 HeapFree(GetProcessHeap(), 0, filename
);
1504 return D3DXERR_INVALIDDATA
;
1507 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1508 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1510 UnmapViewOfFile(data
);
1511 HeapFree(GetProcessHeap(), 0, filename
);
1515 HRESULT WINAPI
D3DXCreateCubeTextureFromFileW(IDirect3DDevice9
*device
,
1516 const WCHAR
*src_filename
,
1517 IDirect3DCubeTexture9
**cube_texture
)
1523 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_w(src_filename
), cube_texture
);
1525 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1526 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1528 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1529 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1531 UnmapViewOfFile(data
);
1535 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9
*device
,
1536 const char *src_filename
,
1545 D3DXIMAGE_INFO
*image_info
,
1546 PALETTEENTRY
*palette
,
1547 IDirect3DCubeTexture9
**cube_texture
)
1555 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1556 device
, wine_dbgstr_a(src_filename
), size
, mip_levels
, usage
, format
,
1557 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1559 if (!src_filename
) return D3DERR_INVALIDCALL
;
1561 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1562 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1563 if (!filename
) return E_OUTOFMEMORY
;
1564 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1566 hr
= map_view_of_file(filename
, &data
, &data_size
);
1569 HeapFree(GetProcessHeap(), 0, filename
);
1570 return D3DXERR_INVALIDDATA
;
1573 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1574 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1576 UnmapViewOfFile(data
);
1577 HeapFree(GetProcessHeap(), 0, filename
);
1581 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9
*device
,
1582 const WCHAR
*src_filename
,
1591 D3DXIMAGE_INFO
*image_info
,
1592 PALETTEENTRY
*palette
,
1593 IDirect3DCubeTexture9
**cube_texture
)
1599 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1600 device
, wine_dbgstr_w(src_filename
), size
, mip_levels
, usage
, format
,
1601 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1603 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1604 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1606 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1607 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1609 UnmapViewOfFile(data
);
1623 static float get_cube_coord(enum cube_coord coord
, unsigned int x
, unsigned int y
, unsigned int size
)
1630 return size
- x
- 0.5f
;
1634 return size
- y
- 0.5f
;
1640 ERR("Unexpected coordinate value\n");
1645 HRESULT WINAPI
D3DXFillCubeTexture(struct IDirect3DCubeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1649 D3DSURFACE_DESC desc
;
1650 D3DLOCKED_RECT lock_rect
;
1652 D3DXVECTOR3 coord
, size
;
1653 const struct pixel_format_desc
*format
;
1655 static const enum cube_coord coordmap
[6][3] =
1657 {ONE
, YCOORDINV
, XCOORDINV
},
1658 {ZERO
, YCOORDINV
, XCOORD
},
1659 {XCOORD
, ONE
, YCOORD
},
1660 {XCOORD
, ZERO
, YCOORDINV
},
1661 {XCOORD
, YCOORDINV
, ONE
},
1662 {XCOORDINV
, YCOORDINV
, ZERO
}
1665 if (texture
== NULL
|| function
== NULL
)
1666 return D3DERR_INVALIDCALL
;
1668 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1670 for (m
= 0; m
< miplevels
; m
++)
1672 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1673 return D3DERR_INVALIDCALL
;
1675 format
= get_format_info(desc
.Format
);
1676 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1678 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1679 return D3DERR_INVALIDCALL
;
1682 for (f
= 0; f
< 6; f
++)
1684 if (FAILED(IDirect3DCubeTexture9_LockRect(texture
, f
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1685 return D3DERR_INVALIDCALL
;
1687 size
.x
= (f
== 0) || (f
== 1) ? 0.0f
: 2.0f
/ desc
.Width
;
1688 size
.y
= (f
== 2) || (f
== 3) ? 0.0f
: 2.0f
/ desc
.Width
;
1689 size
.z
= (f
== 4) || (f
== 5) ? 0.0f
: 2.0f
/ desc
.Width
;
1691 data
= lock_rect
.pBits
;
1693 for (y
= 0; y
< desc
.Height
; y
++)
1695 for (x
= 0; x
< desc
.Width
; x
++)
1697 coord
.x
= get_cube_coord(coordmap
[f
][0], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1698 coord
.y
= get_cube_coord(coordmap
[f
][1], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1699 coord
.z
= get_cube_coord(coordmap
[f
][2], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1701 function(&value
, &coord
, &size
, funcdata
);
1703 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1706 IDirect3DCubeTexture9_UnlockRect(texture
, f
, m
);
1713 HRESULT WINAPI
D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1717 D3DVOLUME_DESC desc
;
1718 D3DLOCKED_BOX lock_box
;
1720 D3DXVECTOR3 coord
, size
;
1721 const struct pixel_format_desc
*format
;
1724 if (texture
== NULL
|| function
== NULL
)
1725 return D3DERR_INVALIDCALL
;
1727 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1729 for (m
= 0; m
< miplevels
; m
++)
1731 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1732 return D3DERR_INVALIDCALL
;
1734 format
= get_format_info(desc
.Format
);
1735 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1737 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1738 return D3DERR_INVALIDCALL
;
1741 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture
, m
, &lock_box
, NULL
, D3DLOCK_DISCARD
)))
1742 return D3DERR_INVALIDCALL
;
1744 size
.x
= 1.0f
/ desc
.Width
;
1745 size
.y
= 1.0f
/ desc
.Height
;
1746 size
.z
= 1.0f
/ desc
.Depth
;
1748 data
= lock_box
.pBits
;
1750 for (z
= 0; z
< desc
.Depth
; z
++)
1752 /* The callback function expects the coordinates of the center
1754 coord
.z
= (z
+ 0.5f
) / desc
.Depth
;
1756 for (y
= 0; y
< desc
.Height
; y
++)
1758 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1760 for (x
= 0; x
< desc
.Width
; x
++)
1762 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1764 function(&value
, &coord
, &size
, funcdata
);
1766 fill_texture(format
, data
+ z
* lock_box
.SlicePitch
+ y
* lock_box
.RowPitch
1767 + x
* format
->bytes_per_pixel
, &value
);
1771 IDirect3DVolumeTexture9_UnlockBox(texture
, m
);
1777 HRESULT WINAPI
D3DXSaveTextureToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1778 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1783 ID3DXBuffer
*buffer
;
1785 TRACE("(%s, %#x, %p, %p): relay\n",
1786 wine_dbgstr_a(dst_filename
), file_format
, src_texture
, src_palette
);
1788 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1790 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1791 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1792 if (!filename
) return E_OUTOFMEMORY
;
1793 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1795 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1798 hr
= write_buffer_to_file(filename
, buffer
);
1799 ID3DXBuffer_Release(buffer
);
1802 HeapFree(GetProcessHeap(), 0, filename
);
1806 HRESULT WINAPI
D3DXSaveTextureToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1807 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1810 ID3DXBuffer
*buffer
;
1812 TRACE("(%s, %#x, %p, %p): relay\n",
1813 wine_dbgstr_w(dst_filename
), file_format
, src_texture
, src_palette
);
1815 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1817 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1820 hr
= write_buffer_to_file(dst_filename
, buffer
);
1821 ID3DXBuffer_Release(buffer
);
1827 HRESULT WINAPI
D3DXSaveTextureToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1828 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1831 D3DRESOURCETYPE type
;
1832 IDirect3DSurface9
*surface
;
1834 TRACE("(%p, %#x, %p, %p)\n",
1835 dst_buffer
, file_format
, src_texture
, src_palette
);
1837 if (!dst_buffer
|| !src_texture
) return D3DERR_INVALIDCALL
;
1839 if (file_format
== D3DXIFF_DDS
)
1841 FIXME("DDS file format isn't supported yet\n");
1845 type
= IDirect3DBaseTexture9_GetType(src_texture
);
1848 case D3DRTYPE_TEXTURE
:
1849 case D3DRTYPE_CUBETEXTURE
:
1850 hr
= get_surface(type
, src_texture
, D3DCUBEMAP_FACE_POSITIVE_X
, 0, &surface
);
1852 case D3DRTYPE_VOLUMETEXTURE
:
1853 FIXME("Volume textures aren't supported yet\n");
1856 return D3DERR_INVALIDCALL
;
1861 hr
= D3DXSaveSurfaceToFileInMemory(dst_buffer
, file_format
, surface
, src_palette
, NULL
);
1862 IDirect3DSurface9_Release(surface
);