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
23 #include "d3dx9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
27 /* Returns TRUE if num is a power of 2, FALSE if not, or if 0 */
28 static BOOL
is_pow2(UINT num
)
30 return !(num
& (num
- 1));
33 static HRESULT
get_surface(D3DRESOURCETYPE type
, struct IDirect3DBaseTexture9
*tex
,
34 int face
, UINT level
, struct IDirect3DSurface9
**surf
)
38 case D3DRTYPE_TEXTURE
:
39 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9
*) tex
, level
, surf
);
40 case D3DRTYPE_CUBETEXTURE
:
41 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9
*) tex
, face
, level
, surf
);
43 ERR("Unexpected texture type\n");
48 HRESULT WINAPI
D3DXFilterTexture(IDirect3DBaseTexture9
*texture
,
49 const PALETTEENTRY
*palette
,
57 TRACE("texture %p, palette %p, srclevel %u, filter %#lx.\n", texture
, palette
, srclevel
, filter
);
60 return D3DERR_INVALIDCALL
;
62 if ((filter
& 0xFFFF) > D3DX_FILTER_BOX
&& filter
!= D3DX_DEFAULT
)
63 return D3DERR_INVALIDCALL
;
65 if (srclevel
== D3DX_DEFAULT
)
67 else if (srclevel
>= IDirect3DBaseTexture9_GetLevelCount(texture
))
68 return D3DERR_INVALIDCALL
;
70 switch (type
= IDirect3DBaseTexture9_GetType(texture
))
72 case D3DRTYPE_TEXTURE
:
73 case D3DRTYPE_CUBETEXTURE
:
75 IDirect3DSurface9
*topsurf
, *mipsurf
;
79 if (type
== D3DRTYPE_TEXTURE
)
82 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
87 IDirect3DCubeTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
90 if (filter
== D3DX_DEFAULT
)
92 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
))
93 filter
= D3DX_FILTER_BOX
;
95 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
98 for (i
= 0; i
< numfaces
; i
++)
100 level
= srclevel
+ 1;
101 hr
= get_surface(type
, texture
, i
, srclevel
, &topsurf
);
104 return D3DERR_INVALIDCALL
;
106 while (get_surface(type
, texture
, i
, level
, &mipsurf
) == D3D_OK
)
108 hr
= D3DXLoadSurfaceFromSurface(mipsurf
, palette
, NULL
, topsurf
, palette
, NULL
, filter
, 0);
109 IDirect3DSurface9_Release(topsurf
);
118 IDirect3DSurface9_Release(topsurf
);
126 case D3DRTYPE_VOLUMETEXTURE
:
129 int level
, level_count
;
130 IDirect3DVolume9
*top_volume
, *mip_volume
;
131 IDirect3DVolumeTexture9
*volume_texture
= (IDirect3DVolumeTexture9
*) texture
;
133 IDirect3DVolumeTexture9_GetLevelDesc(volume_texture
, srclevel
, &desc
);
135 if (filter
== D3DX_DEFAULT
)
137 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
) && is_pow2(desc
.Depth
))
138 filter
= D3DX_FILTER_BOX
;
140 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
143 hr
= IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, srclevel
, &top_volume
);
147 level_count
= IDirect3DVolumeTexture9_GetLevelCount(volume_texture
);
148 for (level
= srclevel
+ 1; level
< level_count
; level
++)
150 IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, level
, &mip_volume
);
151 hr
= D3DXLoadVolumeFromVolume(mip_volume
, palette
, NULL
, top_volume
, palette
, NULL
, filter
, 0);
152 IDirect3DVolume9_Release(top_volume
);
153 top_volume
= mip_volume
;
159 IDirect3DVolume9_Release(top_volume
);
167 return D3DERR_INVALIDCALL
;
171 static D3DFORMAT
get_replacement_format(D3DFORMAT format
)
176 D3DFORMAT replacement_format
;
180 {D3DFMT_L8
, D3DFMT_X8R8G8B8
},
181 {D3DFMT_A8L8
, D3DFMT_A8R8G8B8
},
182 {D3DFMT_A4L4
, D3DFMT_A4R4G4B4
},
183 {D3DFMT_L16
, D3DFMT_A16B16G16R16
},
184 {D3DFMT_DXT1
, D3DFMT_A8R8G8B8
},
185 {D3DFMT_DXT2
, D3DFMT_A8R8G8B8
},
186 {D3DFMT_DXT3
, D3DFMT_A8R8G8B8
},
187 {D3DFMT_DXT4
, D3DFMT_A8R8G8B8
},
188 {D3DFMT_DXT5
, D3DFMT_A8R8G8B8
},
192 for (i
= 0; i
< ARRAY_SIZE(replacements
); ++i
)
193 if (format
== replacements
[i
].format
)
194 return replacements
[i
].replacement_format
;
198 static HRESULT
check_texture_requirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
199 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
, D3DRESOURCETYPE resource_type
)
201 UINT w
= (width
&& *width
) ? *width
: 1;
202 UINT h
= (height
&& *height
) ? *height
: 1;
204 D3DDEVICE_CREATION_PARAMETERS params
;
205 IDirect3D9
*d3d
= NULL
;
208 D3DFORMAT usedformat
= D3DFMT_UNKNOWN
;
209 const struct pixel_format_desc
*fmt
;
212 return D3DERR_INVALIDCALL
;
215 if (usage
== D3DX_DEFAULT
)
217 if (usage
& (D3DUSAGE_WRITEONLY
| D3DUSAGE_DONOTCLIP
| D3DUSAGE_POINTS
| D3DUSAGE_RTPATCHES
| D3DUSAGE_NPATCHES
))
218 return D3DERR_INVALIDCALL
;
221 if ((pool
!= D3DPOOL_DEFAULT
) && (pool
!= D3DPOOL_MANAGED
) && (pool
!= D3DPOOL_SYSTEMMEM
) && (pool
!= D3DPOOL_SCRATCH
))
222 return D3DERR_INVALIDCALL
;
227 TRACE("Requested format %x\n", *format
);
228 usedformat
= *format
;
231 hr
= IDirect3DDevice9_GetDirect3D(device
, &d3d
);
236 hr
= IDirect3DDevice9_GetCreationParameters(device
, ¶ms
);
241 hr
= IDirect3DDevice9_GetDisplayMode(device
, 0, &mode
);
246 if ((usedformat
== D3DFMT_UNKNOWN
) || (usedformat
== D3DX_DEFAULT
))
247 usedformat
= D3DFMT_A8R8G8B8
;
249 fmt
= get_format_info(usedformat
);
251 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
, mode
.Format
,
252 usage
, resource_type
, usedformat
);
256 int bestscore
= INT_MIN
, i
= 0, j
;
257 unsigned int channels
;
258 const struct pixel_format_desc
*curfmt
, *bestfmt
= NULL
;
260 TRACE("Requested format is not supported, looking for a fallback.\n");
264 FIXME("Pixel format %x not handled\n", usedformat
);
267 fmt
= get_format_info(get_replacement_format(usedformat
));
269 allow_24bits
= fmt
->bytes_per_pixel
== 3;
270 channels
= !!fmt
->bits
[0] + !!fmt
->bits
[1] + !!fmt
->bits
[2] + !!fmt
->bits
[3];
271 usedformat
= D3DFMT_UNKNOWN
;
273 while ((curfmt
= get_format_info_idx(i
)))
275 unsigned int curchannels
= !!curfmt
->bits
[0] + !!curfmt
->bits
[1]
276 + !!curfmt
->bits
[2] + !!curfmt
->bits
[3];
281 if (curchannels
< channels
)
283 if (curfmt
->bytes_per_pixel
== 3 && !allow_24bits
)
286 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
,
287 mode
.Format
, usage
, resource_type
, curfmt
->format
);
291 /* This format can be used, let's evaluate it.
292 Weights chosen quite arbitrarily... */
293 score
= 512 * (curfmt
->type
== fmt
->type
);
294 score
-= 32 * (curchannels
- channels
);
296 for (j
= 0; j
< 4; j
++)
298 int diff
= curfmt
->bits
[j
] - fmt
->bits
[j
];
299 score
-= (diff
< 0 ? -diff
* 8 : diff
) * (j
== 0 ? 1 : 2);
302 if (score
> bestscore
)
305 usedformat
= curfmt
->format
;
311 hr
= D3DERR_NOTAVAILABLE
;
318 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
319 return D3DERR_INVALIDCALL
;
321 if ((w
== D3DX_DEFAULT
) && (h
== D3DX_DEFAULT
))
323 else if (w
== D3DX_DEFAULT
)
324 w
= (height
? h
: 256);
325 else if (h
== D3DX_DEFAULT
)
326 h
= (width
? w
: 256);
328 assert(!(fmt
->block_width
& (fmt
->block_width
- 1)));
329 assert(!(fmt
->block_height
& (fmt
->block_height
- 1)));
330 if (w
& (fmt
->block_width
- 1))
331 w
= (w
+ fmt
->block_width
) & ~(fmt
->block_width
- 1);
332 if (h
& (fmt
->block_height
- 1))
333 h
= (h
+ fmt
->block_height
) & ~(fmt
->block_height
- 1);
335 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(w
)))
338 if (w
> caps
.MaxTextureWidth
)
339 w
= caps
.MaxTextureWidth
;
341 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(h
)))
344 if (h
> caps
.MaxTextureHeight
)
345 h
= caps
.MaxTextureHeight
;
347 if (caps
.TextureCaps
& D3DPTEXTURECAPS_SQUAREONLY
)
361 if (miplevels
&& (usage
& D3DUSAGE_AUTOGENMIPMAP
))
368 UINT max_mipmaps
= 1;
370 if (!width
&& !height
)
371 max_mipmaps
= 9; /* number of mipmaps in a 256x256 texture */
374 UINT max_dimen
= max(w
, h
);
376 while (max_dimen
> 1)
383 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
384 *miplevels
= max_mipmaps
;
390 IDirect3D9_Release(d3d
);
395 if (usedformat
== D3DFMT_UNKNOWN
)
397 WARN("Couldn't find a suitable pixel format\n");
398 return D3DERR_NOTAVAILABLE
;
401 TRACE("Format chosen: %x\n", usedformat
);
403 *format
= usedformat
;
408 HRESULT WINAPI
D3DXCheckTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
409 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
411 TRACE("device %p, width %p, height %p, miplevels %p, usage %#lx, format %p, pool %#x.\n",
412 device
, width
, height
, miplevels
, usage
, format
, pool
);
414 return check_texture_requirements(device
, width
, height
, miplevels
, usage
, format
, pool
, D3DRTYPE_TEXTURE
);
417 HRESULT WINAPI
D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*size
,
418 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
421 UINT s
= (size
&& *size
) ? *size
: 256;
424 TRACE("device %p, size %p, miplevels %p, usage %#lx, format %p, pool %#x.\n",
425 device
, size
, miplevels
, usage
, format
, pool
);
427 if (s
== D3DX_DEFAULT
)
430 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
431 return D3DERR_INVALIDCALL
;
433 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP
))
434 return D3DERR_NOTAVAILABLE
;
436 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP_POW2
) && (!is_pow2(s
)))
439 hr
= check_texture_requirements(device
, &s
, &s
, miplevels
, usage
, format
, pool
, D3DRTYPE_CUBETEXTURE
);
441 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPCUBEMAP
))
453 HRESULT WINAPI
D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
454 UINT
*depth
, UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
457 UINT w
= width
? *width
: D3DX_DEFAULT
;
458 UINT h
= height
? *height
: D3DX_DEFAULT
;
459 UINT d
= (depth
&& *depth
) ? *depth
: 1;
462 TRACE("device %p, width %p, height %p, depth %p, miplevels %p, usage %#lx, format %p, pool %#x.\n",
463 device
, width
, height
, depth
, miplevels
, usage
, format
, pool
);
465 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
466 return D3DERR_INVALIDCALL
;
468 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP
))
469 return D3DERR_NOTAVAILABLE
;
471 hr
= check_texture_requirements(device
, &w
, &h
, NULL
, usage
, format
, pool
, D3DRTYPE_VOLUMETEXTURE
);
472 if (d
== D3DX_DEFAULT
)
475 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP_POW2
) &&
476 (!is_pow2(w
) || !is_pow2(h
) || !is_pow2(d
)))
483 if (w
> caps
.MaxVolumeExtent
)
484 w
= caps
.MaxVolumeExtent
;
485 if (h
> caps
.MaxVolumeExtent
)
486 h
= caps
.MaxVolumeExtent
;
487 if (d
> caps
.MaxVolumeExtent
)
488 d
= caps
.MaxVolumeExtent
;
492 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPVOLUMEMAP
))
494 else if ((usage
& D3DUSAGE_AUTOGENMIPMAP
))
501 UINT max_mipmaps
= 1;
502 UINT max_dimen
= max(max(w
, h
), d
);
504 while (max_dimen
> 1)
510 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
511 *miplevels
= max_mipmaps
;
525 HRESULT WINAPI
D3DXCreateTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
,
526 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DTexture9
**texture
)
530 TRACE("device %p, width %u, height %u, miplevels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
531 device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
533 if (!device
|| !texture
)
534 return D3DERR_INVALIDCALL
;
536 if (FAILED(hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
)))
539 return IDirect3DDevice9_CreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
, NULL
);
542 static D3DFORMAT
get_alpha_replacement_format(D3DFORMAT format
)
546 D3DFORMAT orig_format
;
547 D3DFORMAT replacement_format
;
549 replacement_formats
[] =
551 {D3DFMT_X8R8G8B8
, D3DFMT_A8R8G8B8
},
552 {D3DFMT_X1R5G5B5
, D3DFMT_A1R5G5B5
},
553 {D3DFMT_X4R4G4B4
, D3DFMT_A4R4G4B4
},
554 {D3DFMT_X8B8G8R8
, D3DFMT_A8B8G8R8
},
555 {D3DFMT_L8
, D3DFMT_A8L8
},
559 for (i
= 0; i
< ARRAY_SIZE(replacement_formats
); ++i
)
560 if (replacement_formats
[i
].orig_format
== format
)
561 return replacement_formats
[i
].replacement_format
;
565 HRESULT WINAPI
D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9
*device
, const void *srcdata
,
566 UINT srcdatasize
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
567 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
568 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
570 BOOL dynamic_texture
, format_specified
= FALSE
;
571 unsigned int loaded_miplevels
, skip_levels
;
572 IDirect3DSurface9
*surface
;
573 IDirect3DTexture9
**texptr
;
574 IDirect3DTexture9
*buftex
;
575 D3DXIMAGE_INFO imginfo
;
579 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u, "
580 "usage %#lx, format %#x, pool %#x, filter %#lx, mipfilter %#lx, colorkey %#lx, "
581 "srcinfo %p, palette %p, texture %p.\n",
582 device
, srcdata
, srcdatasize
, width
, height
, miplevels
, usage
, format
, pool
,
583 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
585 /* check for invalid parameters */
586 if (!device
|| !texture
|| !srcdata
|| !srcdatasize
)
587 return D3DERR_INVALIDCALL
;
589 hr
= D3DXGetImageInfoFromFileInMemory(srcdata
, srcdatasize
, &imginfo
);
592 FIXME("Unrecognized file format, returning failure.\n");
597 /* handle default values */
598 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
599 width
= imginfo
.Width
;
601 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
602 height
= imginfo
.Height
;
604 if (width
== D3DX_DEFAULT
)
605 width
= make_pow2(imginfo
.Width
);
607 if (height
== D3DX_DEFAULT
)
608 height
= make_pow2(imginfo
.Height
);
610 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
611 format
= imginfo
.Format
;
613 format_specified
= TRUE
;
615 if (width
== D3DX_FROM_FILE
)
617 width
= imginfo
.Width
;
620 if (height
== D3DX_FROM_FILE
)
622 height
= imginfo
.Height
;
625 if (format
== D3DFMT_FROM_FILE
)
627 format
= imginfo
.Format
;
630 if (miplevels
== D3DX_FROM_FILE
)
632 miplevels
= imginfo
.MipLevels
;
635 skip_levels
= mipfilter
!= D3DX_DEFAULT
? mipfilter
>> D3DX_SKIP_DDS_MIP_LEVELS_SHIFT
: 0;
636 if (skip_levels
&& imginfo
.MipLevels
> skip_levels
)
638 TRACE("Skipping the first %u (of %u) levels of a DDS mipmapped texture.\n",
639 skip_levels
, imginfo
.MipLevels
);
640 TRACE("Texture level 0 dimensions are %ux%u.\n", imginfo
.Width
, imginfo
.Height
);
641 width
>>= skip_levels
;
642 height
>>= skip_levels
;
643 miplevels
-= skip_levels
;
650 /* Fix up texture creation parameters. */
651 hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
);
654 FIXME("Couldn't find suitable texture parameters.\n");
659 if (colorkey
&& !format_specified
)
660 format
= get_alpha_replacement_format(format
);
662 if (imginfo
.ResourceType
== D3DRTYPE_VOLUMETEXTURE
663 && D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
&& miplevels
> 1)
665 FIXME("Generation of mipmaps for compressed volume textures is not implemented yet.\n");
669 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
670 return D3DERR_INVALIDCALL
;
672 /* Create the to-be-filled texture */
673 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
674 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
676 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, 0, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
681 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
687 FIXME("Texture creation failed.\n");
692 TRACE("Texture created correctly. Now loading the texture data into it.\n");
693 if (imginfo
.ImageFileFormat
!= D3DXIFF_DDS
)
695 IDirect3DTexture9_GetSurfaceLevel(*texptr
, 0, &surface
);
696 hr
= D3DXLoadSurfaceFromFileInMemory(surface
, palette
, NULL
, srcdata
, srcdatasize
, NULL
, filter
, colorkey
, NULL
);
697 IDirect3DSurface9_Release(surface
);
698 loaded_miplevels
= min(IDirect3DTexture9_GetLevelCount(*texptr
), imginfo
.MipLevels
);
702 hr
= load_texture_from_dds(*texptr
, srcdata
, palette
, filter
, colorkey
, &imginfo
, skip_levels
,
708 FIXME("Texture loading failed.\n");
709 IDirect3DTexture9_Release(*texptr
);
714 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) *texptr
, palette
, loaded_miplevels
- 1, mipfilter
);
717 FIXME("Texture filtering failed.\n");
718 IDirect3DTexture9_Release(*texptr
);
723 /* Move the data to the actual texture if necessary */
724 if (texptr
== &buftex
)
726 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
730 IDirect3DTexture9_Release(buftex
);
735 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)(*texture
));
736 IDirect3DTexture9_Release(buftex
);
745 HRESULT WINAPI
D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
746 const void *srcdata
, UINT srcdatasize
, struct IDirect3DTexture9
**texture
)
748 TRACE("device %p, srcdata %p, srcdatasize %u, texture %p.\n", device
, srcdata
, srcdatasize
, texture
);
750 return D3DXCreateTextureFromFileInMemoryEx(device
, srcdata
, srcdatasize
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
751 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
754 HRESULT WINAPI
D3DXCreateTextureFromFileExW(struct IDirect3DDevice9
*device
, const WCHAR
*srcfile
,
755 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
756 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
757 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
763 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#lx, format %#x, "
764 "pool %#x, filter %#lx, mipfilter %#lx, colorkey 0x%08lx, srcinfo %p, palette %p, texture %p.\n",
765 device
, debugstr_w(srcfile
), width
, height
, miplevels
, usage
, format
,
766 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
769 return D3DERR_INVALIDCALL
;
771 hr
= map_view_of_file(srcfile
, &buffer
, &size
);
774 WARN("Failed to open file.\n");
775 return D3DXERR_INVALIDDATA
;
778 hr
= D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
, usage
, format
, pool
,
779 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
781 UnmapViewOfFile(buffer
);
786 HRESULT WINAPI
D3DXCreateTextureFromFileExA(struct IDirect3DDevice9
*device
, const char *srcfile
,
787 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
788 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
789 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
795 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#lx, format %#x, "
796 "pool %#x, filter %#lx, mipfilter %#lx, colorkey 0x%08lx, srcinfo %p, palette %p, texture %p.\n",
797 device
, debugstr_a(srcfile
), width
, height
, miplevels
, usage
, format
,
798 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
800 if (!device
|| !srcfile
|| !texture
)
801 return D3DERR_INVALIDCALL
;
803 len
= MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, NULL
, 0);
804 widename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*widename
));
805 MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, widename
, len
);
807 hr
= D3DXCreateTextureFromFileExW(device
, widename
, width
, height
, miplevels
,
808 usage
, format
, pool
, filter
, mipfilter
,
809 colorkey
, srcinfo
, palette
, texture
);
811 HeapFree(GetProcessHeap(), 0, widename
);
815 HRESULT WINAPI
D3DXCreateTextureFromFileA(struct IDirect3DDevice9
*device
,
816 const char *srcfile
, struct IDirect3DTexture9
**texture
)
818 TRACE("device %p, srcfile %s, texture %p.\n", device
, debugstr_a(srcfile
), texture
);
820 return D3DXCreateTextureFromFileExA(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
821 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
824 HRESULT WINAPI
D3DXCreateTextureFromFileW(struct IDirect3DDevice9
*device
,
825 const WCHAR
*srcfile
, struct IDirect3DTexture9
**texture
)
827 TRACE("device %p, srcfile %s, texture %p.\n", device
, debugstr_w(srcfile
), texture
);
829 return D3DXCreateTextureFromFileExW(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
830 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
834 HRESULT WINAPI
D3DXCreateTextureFromResourceA(struct IDirect3DDevice9
*device
,
835 HMODULE srcmodule
, const char *resource
, struct IDirect3DTexture9
**texture
)
837 TRACE("device %p, srcmodule %p, resource %s, texture %p.\n",
838 device
, srcmodule
, debugstr_a(resource
), texture
);
840 return D3DXCreateTextureFromResourceExA(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
,
841 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
,
845 HRESULT WINAPI
D3DXCreateTextureFromResourceW(struct IDirect3DDevice9
*device
,
846 HMODULE srcmodule
, const WCHAR
*resource
, struct IDirect3DTexture9
**texture
)
848 TRACE("device %p, srcmodule %p, resource %s, texture %p.\n",
849 device
, srcmodule
, debugstr_w(resource
), texture
);
851 return D3DXCreateTextureFromResourceExW(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
,
852 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
,
856 HRESULT WINAPI
D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
857 const char *resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
858 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
859 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
865 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#lx, format %#x, "
866 "pool %#x, filter %#lx, mipfilter %#lx, colorkey 0x%08lx, srcinfo %p, palette %p, texture %p.\n",
867 device
, srcmodule
, debugstr_a(resource
), width
, height
, miplevels
, usage
, format
,
868 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
870 if (!device
|| !texture
)
871 return D3DERR_INVALIDCALL
;
873 if (!(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_RCDATA
))
874 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
875 && !(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_BITMAP
)))
876 return D3DXERR_INVALIDDATA
;
878 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
879 return D3DXERR_INVALIDDATA
;
881 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
882 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
885 HRESULT WINAPI
D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
886 const WCHAR
*resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
887 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
888 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
894 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#lx, format %#x, "
895 "pool %#x, filter %#lx, mipfilter %#lx, colorkey 0x%08lx, srcinfo %p, palette %p, texture %p.\n",
896 device
, srcmodule
, debugstr_w(resource
), width
, height
, miplevels
, usage
, format
,
897 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
899 if (!device
|| !texture
)
900 return D3DERR_INVALIDCALL
;
902 if (!(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_RCDATA
))
903 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
904 && !(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_BITMAP
)))
905 return D3DXERR_INVALIDDATA
;
907 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
908 return D3DXERR_INVALIDDATA
;
910 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
911 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
914 HRESULT WINAPI
D3DXCreateCubeTexture(struct IDirect3DDevice9
*device
, UINT size
, UINT miplevels
,
915 DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DCubeTexture9
**texture
)
919 TRACE("device %p, size %u, miplevels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
920 device
, size
, miplevels
, usage
, format
, pool
, texture
);
922 if (!device
|| !texture
)
923 return D3DERR_INVALIDCALL
;
925 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &miplevels
, usage
, &format
, pool
);
929 TRACE("D3DXCheckCubeTextureRequirements failed\n");
933 return IDirect3DDevice9_CreateCubeTexture(device
, size
, miplevels
, usage
, format
, pool
, texture
, NULL
);
936 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
937 const void *data
, UINT datasize
, struct IDirect3DCubeTexture9
**texture
)
939 TRACE("device %p, data %p, datasize %u, texture %p.\n", device
, data
, datasize
, texture
);
941 return D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, datasize
, D3DX_DEFAULT
, D3DX_DEFAULT
,
942 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
945 HRESULT WINAPI
D3DXCreateVolumeTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
, UINT depth
,
946 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DVolumeTexture9
**texture
)
950 TRACE("device %p, width %u, height %u, depth %u, miplevels %u, usage %#lx, format %#x, pool %#x, texture %p.\n",
951 device
, width
, height
, depth
, miplevels
, usage
, format
, pool
, texture
);
953 if (!device
|| !texture
)
954 return D3DERR_INVALIDCALL
;
956 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
,
957 &miplevels
, usage
, &format
, pool
);
961 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
965 return IDirect3DDevice9_CreateVolumeTexture(device
, width
, height
, depth
, miplevels
,
966 usage
, format
, pool
, texture
, NULL
);
969 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9
*device
,
970 const char *filename
,
971 IDirect3DVolumeTexture9
**volume_texture
)
979 TRACE("device %p, filename %s, volume_texture %p.\n", device
, debugstr_a(filename
), volume_texture
);
981 if (!filename
) return D3DERR_INVALIDCALL
;
983 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
984 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
985 if (!filenameW
) return E_OUTOFMEMORY
;
986 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
988 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
989 HeapFree(GetProcessHeap(), 0, filenameW
);
990 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
992 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
993 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
995 UnmapViewOfFile(data
);
999 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9
*device
,
1000 const WCHAR
*filename
,
1001 IDirect3DVolumeTexture9
**volume_texture
)
1007 TRACE("device %p, filename %s, volume_texture %p.\n", device
, debugstr_w(filename
), volume_texture
);
1009 if (!filename
) return D3DERR_INVALIDCALL
;
1011 hr
= map_view_of_file(filename
, &data
, &data_size
);
1012 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1014 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1015 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
1017 UnmapViewOfFile(data
);
1021 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9
*device
,
1022 const char *filename
,
1033 D3DXIMAGE_INFO
*src_info
,
1034 PALETTEENTRY
*palette
,
1035 IDirect3DVolumeTexture9
**volume_texture
)
1043 TRACE("device %p, filename %s, width %u, height %u, depth %u, mip_levels %u, usage %#lx, "
1044 "format %#x, pool %#x, filter %#lx, mip_filter %#lx, color_key 0x%08lx, src_info %p, "
1045 "palette %p, volume_texture %p.\n",
1046 device
, debugstr_a(filename
), width
, height
, depth
, mip_levels
, usage
,
1047 format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
, volume_texture
);
1049 if (!filename
) return D3DERR_INVALIDCALL
;
1051 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
1052 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1053 if (!filenameW
) return E_OUTOFMEMORY
;
1054 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
1056 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
1057 HeapFree(GetProcessHeap(), 0, filenameW
);
1058 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1060 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1061 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1064 UnmapViewOfFile(data
);
1068 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9
*device
,
1069 const WCHAR
*filename
,
1080 D3DXIMAGE_INFO
*src_info
,
1081 PALETTEENTRY
*palette
,
1082 IDirect3DVolumeTexture9
**volume_texture
)
1088 TRACE("device %p, filename %s, width %u, height %u, depth %u, mip_levels %u, usage %#lx, "
1089 "format %#x, pool %#x, filter %#lx, mip_filter %#lx, color_key 0x%08lx, src_info %p, "
1090 "palette %p, volume_texture %p.\n",
1091 device
, debugstr_w(filename
), width
, height
, depth
, mip_levels
, usage
,
1092 format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
, volume_texture
);
1094 if (!filename
) return D3DERR_INVALIDCALL
;
1096 hr
= map_view_of_file(filename
, &data
, &data_size
);
1097 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1099 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1100 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1103 UnmapViewOfFile(data
);
1107 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9
*device
,
1110 IDirect3DVolumeTexture9
**volume_texture
)
1112 TRACE("device %p, data %p, data_size %u, volume_texture %p.\n", device
, data
, data_size
, volume_texture
);
1114 return D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1115 D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1116 0, NULL
, NULL
, volume_texture
);
1119 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
, const void *data
,
1120 UINT data_size
, UINT width
, UINT height
, UINT depth
, UINT mip_levels
, DWORD usage
,
1121 D3DFORMAT format
, D3DPOOL pool
, DWORD filter
, DWORD mip_filter
, D3DCOLOR color_key
,
1122 D3DXIMAGE_INFO
*info
, PALETTEENTRY
*palette
, IDirect3DVolumeTexture9
**volume_texture
)
1126 D3DXIMAGE_INFO image_info
;
1127 BOOL dynamic_texture
;
1128 BOOL file_width
= FALSE
;
1129 BOOL file_height
= FALSE
;
1130 BOOL file_depth
= FALSE
;
1131 BOOL file_format
= FALSE
;
1132 BOOL file_mip_levels
= FALSE
;
1133 IDirect3DVolumeTexture9
*tex
, *buftex
;
1135 TRACE("device %p, data %p, data_size %u, width %u, height %u, depth %u, mip_levels %u, "
1136 "usage %#lx, format %#x, pool %#x, filter %#lx, mip_filter %#lx, color_key 0x%08lx, "
1137 "info %p, palette %p, volume_texture %p.\n",
1138 device
, data
, data_size
, width
, height
, depth
, mip_levels
, usage
, format
, pool
,
1139 filter
, mip_filter
, color_key
, info
, palette
, volume_texture
);
1141 if (!device
|| !data
|| !data_size
|| !volume_texture
)
1142 return D3DERR_INVALIDCALL
;
1144 hr
= D3DXGetImageInfoFromFileInMemory(data
, data_size
, &image_info
);
1145 if (FAILED(hr
)) return hr
;
1147 if (image_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1148 return D3DXERR_INVALIDDATA
;
1150 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
1151 width
= image_info
.Width
;
1152 if (width
== D3DX_DEFAULT
)
1153 width
= make_pow2(image_info
.Width
);
1155 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
1156 height
= image_info
.Height
;
1157 if (height
== D3DX_DEFAULT
)
1158 height
= make_pow2(image_info
.Height
);
1160 if (depth
== 0 || depth
== D3DX_DEFAULT_NONPOW2
)
1161 depth
= image_info
.Depth
;
1162 if (depth
== D3DX_DEFAULT
)
1163 depth
= make_pow2(image_info
.Depth
);
1165 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1166 format
= image_info
.Format
;
1168 if (width
== D3DX_FROM_FILE
)
1171 width
= image_info
.Width
;
1174 if (height
== D3DX_FROM_FILE
)
1177 height
= image_info
.Height
;
1180 if (depth
== D3DX_FROM_FILE
)
1183 depth
= image_info
.Depth
;
1186 if (format
== D3DFMT_FROM_FILE
)
1189 format
= image_info
.Format
;
1192 if (mip_levels
== D3DX_FROM_FILE
)
1194 file_mip_levels
= TRUE
;
1195 mip_levels
= image_info
.MipLevels
;
1198 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
, &mip_levels
, usage
, &format
, pool
);
1199 if (FAILED(hr
)) return hr
;
1201 if ((file_width
&& width
!= image_info
.Width
)
1202 || (file_height
&& height
!= image_info
.Height
)
1203 || (file_depth
&& depth
!= image_info
.Depth
)
1204 || (file_format
&& format
!= image_info
.Format
)
1205 || (file_mip_levels
&& mip_levels
!= image_info
.MipLevels
))
1206 return D3DERR_NOTAVAILABLE
;
1208 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1210 return D3DERR_INVALIDCALL
;
1212 if (mip_levels
> image_info
.MipLevels
)
1214 FIXME("Generation of mipmaps for volume textures is not implemented yet.\n");
1215 mip_levels
= image_info
.MipLevels
;
1218 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1219 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1221 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, 0, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1226 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1229 if (FAILED(hr
)) return hr
;
1231 hr
= load_volume_texture_from_dds(tex
, data
, palette
, filter
, color_key
, &image_info
);
1234 IDirect3DVolumeTexture9_Release(tex
);
1240 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1243 IDirect3DVolumeTexture9_Release(buftex
);
1247 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1248 IDirect3DVolumeTexture9_Release(buftex
);
1254 *volume_texture
= tex
;
1258 static inline void fill_texture(const struct pixel_format_desc
*format
, BYTE
*pos
, const D3DXVECTOR4
*value
)
1262 for (c
= 0; c
< format
->bytes_per_pixel
; c
++)
1265 for (c
= 0; c
< 4; c
++)
1268 DWORD i
, v
= 0, mask32
= format
->bits
[c
] == 32 ? ~0U : ((1 << format
->bits
[c
]) - 1);
1273 comp_value
= value
->w
;
1276 comp_value
= value
->x
;
1279 comp_value
= value
->y
;
1282 comp_value
= value
->z
;
1286 if (format
->type
== FORMAT_ARGBF16
)
1287 v
= float_32_to_16(comp_value
);
1288 else if (format
->type
== FORMAT_ARGBF
)
1289 v
= *(DWORD
*)&comp_value
;
1290 else if (format
->type
== FORMAT_ARGB
)
1291 v
= max(comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
, 0);
1293 FIXME("Unhandled format type %#x\n", format
->type
);
1295 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1299 if (format
->shift
[c
] > i
)
1301 mask
= mask32
<< (format
->shift
[c
] - i
);
1302 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1306 mask
= mask32
>> (i
- format
->shift
[c
]);
1307 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1314 HRESULT WINAPI
D3DXFillTexture(struct IDirect3DTexture9
*texture
, LPD3DXFILL2D function
, void *funcdata
)
1316 IDirect3DSurface9
*surface
, *temp_surface
;
1319 D3DSURFACE_DESC desc
;
1320 D3DLOCKED_RECT lock_rect
;
1322 D3DXVECTOR2 coord
, size
;
1323 const struct pixel_format_desc
*format
;
1327 TRACE("texture %p, function %p, funcdata %p.\n", texture
, function
, funcdata
);
1329 if (!texture
|| !function
)
1330 return D3DERR_INVALIDCALL
;
1332 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1334 for (m
= 0; m
< miplevels
; m
++)
1336 if (FAILED(hr
= IDirect3DTexture9_GetLevelDesc(texture
, m
, &desc
)))
1339 format
= get_format_info(desc
.Format
);
1340 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1342 FIXME("Unsupported texture format %#x.\n", desc
.Format
);
1343 return D3DERR_INVALIDCALL
;
1346 if (FAILED(hr
= IDirect3DTexture9_GetSurfaceLevel(texture
, m
, &surface
)))
1348 if (FAILED(hr
= lock_surface(surface
, NULL
, &lock_rect
, &temp_surface
, TRUE
)))
1350 IDirect3DSurface9_Release(surface
);
1354 size
.x
= 1.0f
/ desc
.Width
;
1355 size
.y
= 1.0f
/ desc
.Height
;
1357 data
= lock_rect
.pBits
;
1359 for (y
= 0; y
< desc
.Height
; y
++)
1361 /* The callback function expects the coordinates of the center
1363 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1365 for (x
= 0; x
< desc
.Width
; x
++)
1367 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1369 function(&value
, &coord
, &size
, funcdata
);
1371 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1374 if (FAILED(hr
= unlock_surface(surface
, NULL
, temp_surface
, TRUE
)))
1376 IDirect3DSurface9_Release(surface
);
1379 IDirect3DSurface9_Release(surface
);
1385 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
, const void *src_data
,
1386 UINT src_data_size
, UINT size
, UINT mip_levels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
,
1387 DWORD filter
, DWORD mip_filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
,
1388 PALETTEENTRY
*palette
, IDirect3DCubeTexture9
**cube_texture
)
1392 UINT loaded_miplevels
;
1393 D3DXIMAGE_INFO img_info
;
1394 BOOL dynamic_texture
;
1395 BOOL file_size
= FALSE
;
1396 BOOL file_format
= FALSE
;
1397 BOOL file_mip_levels
= FALSE
;
1398 IDirect3DCubeTexture9
*tex
, *buftex
;
1400 TRACE("device %p, src_data %p, src_data_size %u, size %u, mip_levels %u, usage %#lx, "
1401 "format %#x, pool %#x, filter %#lx, mip_filter %#lx, color_key 0x%08lx, src_info %p, "
1402 "palette %p, cube_texture %p.\n",
1403 device
, src_data
, src_data_size
, size
, mip_levels
, usage
, format
, pool
, filter
,
1404 mip_filter
, color_key
, src_info
, palette
, cube_texture
);
1406 if (!device
|| !cube_texture
|| !src_data
|| !src_data_size
)
1407 return D3DERR_INVALIDCALL
;
1409 hr
= D3DXGetImageInfoFromFileInMemory(src_data
, src_data_size
, &img_info
);
1413 if (img_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1414 return D3DXERR_INVALIDDATA
;
1416 if (img_info
.Width
!= img_info
.Height
)
1417 return D3DXERR_INVALIDDATA
;
1419 if (size
== 0 || size
== D3DX_DEFAULT_NONPOW2
)
1420 size
= img_info
.Width
;
1421 if (size
== D3DX_DEFAULT
)
1422 size
= make_pow2(img_info
.Width
);
1424 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1425 format
= img_info
.Format
;
1427 if (size
== D3DX_FROM_FILE
)
1430 size
= img_info
.Width
;
1433 if (format
== D3DFMT_FROM_FILE
)
1436 format
= img_info
.Format
;
1439 if (mip_levels
== D3DX_FROM_FILE
)
1441 file_mip_levels
= TRUE
;
1442 mip_levels
= img_info
.MipLevels
;
1445 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &mip_levels
, usage
, &format
, pool
);
1449 if ((file_size
&& size
!= img_info
.Width
)
1450 || (file_format
&& format
!= img_info
.Format
)
1451 || (file_mip_levels
&& mip_levels
!= img_info
.MipLevels
))
1452 return D3DERR_NOTAVAILABLE
;
1454 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1456 return D3DERR_INVALIDCALL
;
1458 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1459 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1461 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, 0, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1466 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1472 hr
= load_cube_texture_from_dds(tex
, src_data
, palette
, filter
, color_key
, &img_info
);
1475 IDirect3DCubeTexture9_Release(tex
);
1479 loaded_miplevels
= min(IDirect3DCubeTexture9_GetLevelCount(tex
), img_info
.MipLevels
);
1480 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) tex
, palette
, loaded_miplevels
- 1, mip_filter
);
1483 IDirect3DCubeTexture9_Release(tex
);
1489 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1492 IDirect3DCubeTexture9_Release(buftex
);
1496 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1497 IDirect3DCubeTexture9_Release(buftex
);
1501 *src_info
= img_info
;
1503 *cube_texture
= tex
;
1508 HRESULT WINAPI
D3DXCreateCubeTextureFromFileA(IDirect3DDevice9
*device
,
1509 const char *src_filename
,
1510 IDirect3DCubeTexture9
**cube_texture
)
1518 TRACE("device %p, src_filename %s, cube_texture %p.\n", device
, wine_dbgstr_a(src_filename
), cube_texture
);
1520 if (!src_filename
) return D3DERR_INVALIDCALL
;
1522 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1523 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1524 if (!filename
) return E_OUTOFMEMORY
;
1525 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1527 hr
= map_view_of_file(filename
, &data
, &data_size
);
1530 HeapFree(GetProcessHeap(), 0, filename
);
1531 return D3DXERR_INVALIDDATA
;
1534 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1535 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1537 UnmapViewOfFile(data
);
1538 HeapFree(GetProcessHeap(), 0, filename
);
1542 HRESULT WINAPI
D3DXCreateCubeTextureFromFileW(IDirect3DDevice9
*device
,
1543 const WCHAR
*src_filename
,
1544 IDirect3DCubeTexture9
**cube_texture
)
1550 TRACE("device %p, src_filename %s, cube_texture %p.\n", device
, wine_dbgstr_w(src_filename
), cube_texture
);
1552 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1553 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1555 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1556 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1558 UnmapViewOfFile(data
);
1562 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9
*device
, const char *src_filename
,
1563 UINT size
, UINT mip_levels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, DWORD filter
,
1564 DWORD mip_filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*image_info
, PALETTEENTRY
*palette
,
1565 IDirect3DCubeTexture9
**cube_texture
)
1573 TRACE("device %p, src_filename %s, size %u, mip_levels %u, usage %#lx, format %#x, pool %#x, "
1574 "filter %#lx, mip_filter %#lx, color_key 0x%08lx, image_info %p, palette %p, cube_texture %p.\n",
1575 device
, wine_dbgstr_a(src_filename
), size
, mip_levels
, usage
, format
, pool
,
1576 filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1578 if (!src_filename
) return D3DERR_INVALIDCALL
;
1580 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1581 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1582 if (!filename
) return E_OUTOFMEMORY
;
1583 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1585 hr
= map_view_of_file(filename
, &data
, &data_size
);
1588 HeapFree(GetProcessHeap(), 0, filename
);
1589 return D3DXERR_INVALIDDATA
;
1592 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1593 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1595 UnmapViewOfFile(data
);
1596 HeapFree(GetProcessHeap(), 0, filename
);
1600 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9
*device
, const WCHAR
*src_filename
,
1601 UINT size
, UINT mip_levels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, DWORD filter
,
1602 DWORD mip_filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*image_info
, PALETTEENTRY
*palette
,
1603 IDirect3DCubeTexture9
**cube_texture
)
1609 TRACE("device %p, src_filename %s, size %u, mip_levels %u, usage %#lx, format %#x, pool %#x, "
1610 "filter %#lx, mip_filter %#lx, color_key 0x%08lx, image_info %p, palette %p, cube_texture %p.\n",
1611 device
, wine_dbgstr_w(src_filename
), size
, mip_levels
, usage
, format
,
1612 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1614 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1615 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1617 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1618 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1620 UnmapViewOfFile(data
);
1634 static float get_cube_coord(enum cube_coord coord
, unsigned int x
, unsigned int y
, unsigned int size
)
1641 return size
- x
- 0.5f
;
1645 return size
- y
- 0.5f
;
1651 ERR("Unexpected coordinate value\n");
1656 HRESULT WINAPI
D3DXFillCubeTexture(struct IDirect3DCubeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1660 D3DSURFACE_DESC desc
;
1661 D3DLOCKED_RECT lock_rect
;
1663 D3DXVECTOR3 coord
, size
;
1664 const struct pixel_format_desc
*format
;
1666 static const enum cube_coord coordmap
[6][3] =
1668 {ONE
, YCOORDINV
, XCOORDINV
},
1669 {ZERO
, YCOORDINV
, XCOORD
},
1670 {XCOORD
, ONE
, YCOORD
},
1671 {XCOORD
, ZERO
, YCOORDINV
},
1672 {XCOORD
, YCOORDINV
, ONE
},
1673 {XCOORDINV
, YCOORDINV
, ZERO
}
1676 TRACE("texture %p, function %p, funcdata %p.\n", texture
, function
, funcdata
);
1678 if (texture
== NULL
|| function
== NULL
)
1679 return D3DERR_INVALIDCALL
;
1681 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1683 for (m
= 0; m
< miplevels
; m
++)
1685 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1686 return D3DERR_INVALIDCALL
;
1688 format
= get_format_info(desc
.Format
);
1689 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1691 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1692 return D3DERR_INVALIDCALL
;
1695 for (f
= 0; f
< 6; f
++)
1697 if (FAILED(IDirect3DCubeTexture9_LockRect(texture
, f
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1698 return D3DERR_INVALIDCALL
;
1700 size
.x
= (f
== 0) || (f
== 1) ? 0.0f
: 2.0f
/ desc
.Width
;
1701 size
.y
= (f
== 2) || (f
== 3) ? 0.0f
: 2.0f
/ desc
.Width
;
1702 size
.z
= (f
== 4) || (f
== 5) ? 0.0f
: 2.0f
/ desc
.Width
;
1704 data
= lock_rect
.pBits
;
1706 for (y
= 0; y
< desc
.Height
; y
++)
1708 for (x
= 0; x
< desc
.Width
; x
++)
1710 coord
.x
= get_cube_coord(coordmap
[f
][0], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1711 coord
.y
= get_cube_coord(coordmap
[f
][1], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1712 coord
.z
= get_cube_coord(coordmap
[f
][2], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1714 function(&value
, &coord
, &size
, funcdata
);
1716 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1719 IDirect3DCubeTexture9_UnlockRect(texture
, f
, m
);
1726 HRESULT WINAPI
D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1730 D3DVOLUME_DESC desc
;
1731 D3DLOCKED_BOX lock_box
;
1733 D3DXVECTOR3 coord
, size
;
1734 const struct pixel_format_desc
*format
;
1737 TRACE("texture %p, function %p, funcdata %p.\n", texture
, function
, funcdata
);
1739 if (texture
== NULL
|| function
== NULL
)
1740 return D3DERR_INVALIDCALL
;
1742 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1744 for (m
= 0; m
< miplevels
; m
++)
1746 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1747 return D3DERR_INVALIDCALL
;
1749 format
= get_format_info(desc
.Format
);
1750 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1752 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1753 return D3DERR_INVALIDCALL
;
1756 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture
, m
, &lock_box
, NULL
, D3DLOCK_DISCARD
)))
1757 return D3DERR_INVALIDCALL
;
1759 size
.x
= 1.0f
/ desc
.Width
;
1760 size
.y
= 1.0f
/ desc
.Height
;
1761 size
.z
= 1.0f
/ desc
.Depth
;
1763 data
= lock_box
.pBits
;
1765 for (z
= 0; z
< desc
.Depth
; z
++)
1767 /* The callback function expects the coordinates of the center
1769 coord
.z
= (z
+ 0.5f
) / desc
.Depth
;
1771 for (y
= 0; y
< desc
.Height
; y
++)
1773 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1775 for (x
= 0; x
< desc
.Width
; x
++)
1777 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1779 function(&value
, &coord
, &size
, funcdata
);
1781 fill_texture(format
, data
+ z
* lock_box
.SlicePitch
+ y
* lock_box
.RowPitch
1782 + x
* format
->bytes_per_pixel
, &value
);
1786 IDirect3DVolumeTexture9_UnlockBox(texture
, m
);
1792 HRESULT WINAPI
D3DXFillVolumeTextureTX(struct IDirect3DVolumeTexture9
*texture
, ID3DXTextureShader
*texture_shader
)
1794 FIXME("texture %p, texture_shader %p stub.\n", texture
, texture_shader
);
1798 HRESULT WINAPI
D3DXSaveTextureToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1799 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1804 ID3DXBuffer
*buffer
;
1806 TRACE("dst_filename %s, file_format %u, src_texture %p, src_palette %p.\n",
1807 wine_dbgstr_a(dst_filename
), file_format
, src_texture
, src_palette
);
1809 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1811 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1812 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1813 if (!filename
) return E_OUTOFMEMORY
;
1814 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1816 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1819 hr
= write_buffer_to_file(filename
, buffer
);
1820 ID3DXBuffer_Release(buffer
);
1823 HeapFree(GetProcessHeap(), 0, filename
);
1827 HRESULT WINAPI
D3DXSaveTextureToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1828 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1831 ID3DXBuffer
*buffer
;
1833 TRACE("dst_filename %s, file_format %u, src_texture %p, src_palette %p.\n",
1834 wine_dbgstr_w(dst_filename
), file_format
, src_texture
, src_palette
);
1836 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1838 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1841 hr
= write_buffer_to_file(dst_filename
, buffer
);
1842 ID3DXBuffer_Release(buffer
);
1848 HRESULT WINAPI
D3DXSaveTextureToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1849 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1852 D3DRESOURCETYPE type
;
1853 IDirect3DSurface9
*surface
;
1855 TRACE("dst_buffer %p, file_format %u, src_texture %p, src_palette %p.\n",
1856 dst_buffer
, file_format
, src_texture
, src_palette
);
1858 if (!dst_buffer
|| !src_texture
) return D3DERR_INVALIDCALL
;
1860 if (file_format
== D3DXIFF_DDS
)
1862 FIXME("DDS file format isn't supported yet\n");
1866 type
= IDirect3DBaseTexture9_GetType(src_texture
);
1869 case D3DRTYPE_TEXTURE
:
1870 case D3DRTYPE_CUBETEXTURE
:
1871 hr
= get_surface(type
, src_texture
, D3DCUBEMAP_FACE_POSITIVE_X
, 0, &surface
);
1873 case D3DRTYPE_VOLUMETEXTURE
:
1874 FIXME("Volume textures aren't supported yet\n");
1877 return D3DERR_INVALIDCALL
;
1882 hr
= D3DXSaveSurfaceToFileInMemory(dst_buffer
, file_format
, surface
, src_palette
, NULL
);
1883 IDirect3DSurface9_Release(surface
);
1889 HRESULT WINAPI
D3DXComputeNormalMap(IDirect3DTexture9
*texture
, IDirect3DTexture9
*src_texture
,
1890 const PALETTEENTRY
*src_palette
, DWORD flags
, DWORD channel
, float amplitude
)
1892 FIXME("texture %p, src_texture %p, src_palette %p, flags %#lx, channel %lu, amplitude %.8e stub.\n",
1893 texture
, src_texture
, src_palette
, flags
, channel
, amplitude
);