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("(%p, %p, %u, %#x)\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 %u, format %p, pool %u.\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("(%p, %p, %p, %u, %p, %u)\n", device
, size
, miplevels
, usage
, format
, pool
);
426 if (s
== D3DX_DEFAULT
)
429 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
430 return D3DERR_INVALIDCALL
;
432 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP
))
433 return D3DERR_NOTAVAILABLE
;
435 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP_POW2
) && (!is_pow2(s
)))
438 hr
= check_texture_requirements(device
, &s
, &s
, miplevels
, usage
, format
, pool
, D3DRTYPE_CUBETEXTURE
);
440 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPCUBEMAP
))
452 HRESULT WINAPI
D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
453 UINT
*depth
, UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
456 UINT w
= width
? *width
: D3DX_DEFAULT
;
457 UINT h
= height
? *height
: D3DX_DEFAULT
;
458 UINT d
= (depth
&& *depth
) ? *depth
: 1;
461 TRACE("(%p, %p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, depth
, miplevels
,
462 usage
, format
, pool
);
464 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
465 return D3DERR_INVALIDCALL
;
467 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP
))
468 return D3DERR_NOTAVAILABLE
;
470 hr
= check_texture_requirements(device
, &w
, &h
, NULL
, usage
, format
, pool
, D3DRTYPE_VOLUMETEXTURE
);
471 if (d
== D3DX_DEFAULT
)
474 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP_POW2
) &&
475 (!is_pow2(w
) || !is_pow2(h
) || !is_pow2(d
)))
482 if (w
> caps
.MaxVolumeExtent
)
483 w
= caps
.MaxVolumeExtent
;
484 if (h
> caps
.MaxVolumeExtent
)
485 h
= caps
.MaxVolumeExtent
;
486 if (d
> caps
.MaxVolumeExtent
)
487 d
= caps
.MaxVolumeExtent
;
491 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPVOLUMEMAP
))
493 else if ((usage
& D3DUSAGE_AUTOGENMIPMAP
))
500 UINT max_mipmaps
= 1;
501 UINT max_dimen
= max(max(w
, h
), d
);
503 while (max_dimen
> 1)
509 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
510 *miplevels
= max_mipmaps
;
524 HRESULT WINAPI
D3DXCreateTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
,
525 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DTexture9
**texture
)
529 TRACE("device %p, width %u, height %u, miplevels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
530 device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
532 if (!device
|| !texture
)
533 return D3DERR_INVALIDCALL
;
535 if (FAILED(hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
)))
538 return IDirect3DDevice9_CreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
, NULL
);
541 static D3DFORMAT
get_alpha_replacement_format(D3DFORMAT format
)
545 D3DFORMAT orig_format
;
546 D3DFORMAT replacement_format
;
548 replacement_formats
[] =
550 {D3DFMT_X8R8G8B8
, D3DFMT_A8R8G8B8
},
551 {D3DFMT_X1R5G5B5
, D3DFMT_A1R5G5B5
},
552 {D3DFMT_X4R4G4B4
, D3DFMT_A4R4G4B4
},
553 {D3DFMT_X8B8G8R8
, D3DFMT_A8B8G8R8
},
554 {D3DFMT_L8
, D3DFMT_A8L8
},
558 for (i
= 0; i
< ARRAY_SIZE(replacement_formats
); ++i
)
559 if (replacement_formats
[i
].orig_format
== format
)
560 return replacement_formats
[i
].replacement_format
;
564 HRESULT WINAPI
D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9
*device
, const void *srcdata
,
565 UINT srcdatasize
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
566 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
567 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
569 BOOL dynamic_texture
, format_specified
= FALSE
;
570 unsigned int loaded_miplevels
, skip_levels
;
571 IDirect3DSurface9
*surface
;
572 IDirect3DTexture9
**texptr
;
573 IDirect3DTexture9
*buftex
;
574 D3DXIMAGE_INFO imginfo
;
578 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u,"
579 " usage %#x, format %#x, pool %#x, filter %#x, mipfilter %#x, colorkey %#x,"
580 " srcinfo %p, palette %p, texture %p.\n",
581 device
, srcdata
, srcdatasize
, width
, height
, miplevels
, usage
, format
, pool
,
582 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
584 /* check for invalid parameters */
585 if (!device
|| !texture
|| !srcdata
|| !srcdatasize
)
586 return D3DERR_INVALIDCALL
;
588 hr
= D3DXGetImageInfoFromFileInMemory(srcdata
, srcdatasize
, &imginfo
);
591 FIXME("Unrecognized file format, returning failure.\n");
596 /* handle default values */
597 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
598 width
= imginfo
.Width
;
600 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
601 height
= imginfo
.Height
;
603 if (width
== D3DX_DEFAULT
)
604 width
= make_pow2(imginfo
.Width
);
606 if (height
== D3DX_DEFAULT
)
607 height
= make_pow2(imginfo
.Height
);
609 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
610 format
= imginfo
.Format
;
612 format_specified
= TRUE
;
614 if (width
== D3DX_FROM_FILE
)
616 width
= imginfo
.Width
;
619 if (height
== D3DX_FROM_FILE
)
621 height
= imginfo
.Height
;
624 if (format
== D3DFMT_FROM_FILE
)
626 format
= imginfo
.Format
;
629 if (miplevels
== D3DX_FROM_FILE
)
631 miplevels
= imginfo
.MipLevels
;
634 skip_levels
= mipfilter
!= D3DX_DEFAULT
? mipfilter
>> D3DX_SKIP_DDS_MIP_LEVELS_SHIFT
: 0;
635 if (skip_levels
&& imginfo
.MipLevels
> skip_levels
)
637 TRACE("Skipping the first %u (of %u) levels of a DDS mipmapped texture.\n",
638 skip_levels
, imginfo
.MipLevels
);
639 TRACE("Texture level 0 dimensions are %ux%u.\n", imginfo
.Width
, imginfo
.Height
);
640 width
>>= skip_levels
;
641 height
>>= skip_levels
;
642 miplevels
-= skip_levels
;
649 /* Fix up texture creation parameters. */
650 hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
);
653 FIXME("Couldn't find suitable texture parameters.\n");
658 if (colorkey
&& !format_specified
)
659 format
= get_alpha_replacement_format(format
);
661 if (imginfo
.ResourceType
== D3DRTYPE_VOLUMETEXTURE
662 && D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
&& miplevels
> 1)
664 FIXME("Generation of mipmaps for compressed volume textures is not implemented yet.\n");
668 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
669 return D3DERR_INVALIDCALL
;
671 /* Create the to-be-filled texture */
672 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
673 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
675 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, 0, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
680 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
686 FIXME("Texture creation failed.\n");
691 TRACE("Texture created correctly. Now loading the texture data into it.\n");
692 if (imginfo
.ImageFileFormat
!= D3DXIFF_DDS
)
694 IDirect3DTexture9_GetSurfaceLevel(*texptr
, 0, &surface
);
695 hr
= D3DXLoadSurfaceFromFileInMemory(surface
, palette
, NULL
, srcdata
, srcdatasize
, NULL
, filter
, colorkey
, NULL
);
696 IDirect3DSurface9_Release(surface
);
697 loaded_miplevels
= min(IDirect3DTexture9_GetLevelCount(*texptr
), imginfo
.MipLevels
);
701 hr
= load_texture_from_dds(*texptr
, srcdata
, palette
, filter
, colorkey
, &imginfo
, skip_levels
,
707 FIXME("Texture loading failed.\n");
708 IDirect3DTexture9_Release(*texptr
);
713 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) *texptr
, palette
, loaded_miplevels
- 1, mipfilter
);
716 FIXME("Texture filtering failed.\n");
717 IDirect3DTexture9_Release(*texptr
);
722 /* Move the data to the actual texture if necessary */
723 if (texptr
== &buftex
)
725 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
729 IDirect3DTexture9_Release(buftex
);
734 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)(*texture
));
735 IDirect3DTexture9_Release(buftex
);
744 HRESULT WINAPI
D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
745 const void *srcdata
, UINT srcdatasize
, struct IDirect3DTexture9
**texture
)
747 TRACE("(%p, %p, %d, %p)\n", device
, srcdata
, srcdatasize
, texture
);
749 return D3DXCreateTextureFromFileInMemoryEx(device
, srcdata
, srcdatasize
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
750 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
753 HRESULT WINAPI
D3DXCreateTextureFromFileExW(struct IDirect3DDevice9
*device
, const WCHAR
*srcfile
,
754 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
755 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
756 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
762 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
763 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
764 device
, debugstr_w(srcfile
), width
, height
, miplevels
, usage
, format
,
765 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
768 return D3DERR_INVALIDCALL
;
770 hr
= map_view_of_file(srcfile
, &buffer
, &size
);
773 WARN("Failed to open file.\n");
774 return D3DXERR_INVALIDDATA
;
777 hr
= D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
, usage
, format
, pool
,
778 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
780 UnmapViewOfFile(buffer
);
785 HRESULT WINAPI
D3DXCreateTextureFromFileExA(struct IDirect3DDevice9
*device
, const char *srcfile
,
786 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
787 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
788 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
794 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
795 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
796 device
, debugstr_a(srcfile
), width
, height
, miplevels
, usage
, format
,
797 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
799 if (!device
|| !srcfile
|| !texture
)
800 return D3DERR_INVALIDCALL
;
802 len
= MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, NULL
, 0);
803 widename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*widename
));
804 MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, widename
, len
);
806 hr
= D3DXCreateTextureFromFileExW(device
, widename
, width
, height
, miplevels
,
807 usage
, format
, pool
, filter
, mipfilter
,
808 colorkey
, srcinfo
, palette
, texture
);
810 HeapFree(GetProcessHeap(), 0, widename
);
814 HRESULT WINAPI
D3DXCreateTextureFromFileA(struct IDirect3DDevice9
*device
,
815 const char *srcfile
, struct IDirect3DTexture9
**texture
)
817 TRACE("(%p, %s, %p)\n", device
, debugstr_a(srcfile
), texture
);
819 return D3DXCreateTextureFromFileExA(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
820 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
823 HRESULT WINAPI
D3DXCreateTextureFromFileW(struct IDirect3DDevice9
*device
,
824 const WCHAR
*srcfile
, struct IDirect3DTexture9
**texture
)
826 TRACE("(%p, %s, %p)\n", device
, debugstr_w(srcfile
), texture
);
828 return D3DXCreateTextureFromFileExW(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
829 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
833 HRESULT WINAPI
D3DXCreateTextureFromResourceA(struct IDirect3DDevice9
*device
,
834 HMODULE srcmodule
, const char *resource
, struct IDirect3DTexture9
**texture
)
836 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_a(resource
));
838 return D3DXCreateTextureFromResourceExA(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
839 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
842 HRESULT WINAPI
D3DXCreateTextureFromResourceW(struct IDirect3DDevice9
*device
,
843 HMODULE srcmodule
, const WCHAR
*resource
, struct IDirect3DTexture9
**texture
)
845 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_w(resource
));
847 return D3DXCreateTextureFromResourceExW(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
848 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
851 HRESULT WINAPI
D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
852 const char *resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
853 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
854 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
860 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
861 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
862 device
, srcmodule
, debugstr_a(resource
), width
, height
, miplevels
, usage
, format
,
863 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
865 if (!device
|| !texture
)
866 return D3DERR_INVALIDCALL
;
868 if (!(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_RCDATA
))
869 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
870 && !(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_BITMAP
)))
871 return D3DXERR_INVALIDDATA
;
873 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
874 return D3DXERR_INVALIDDATA
;
876 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
877 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
880 HRESULT WINAPI
D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
881 const WCHAR
*resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
882 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
883 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
889 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
890 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
891 device
, srcmodule
, debugstr_w(resource
), width
, height
, miplevels
, usage
, format
,
892 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
894 if (!device
|| !texture
)
895 return D3DERR_INVALIDCALL
;
897 if (!(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_RCDATA
))
898 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
899 && !(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_BITMAP
)))
900 return D3DXERR_INVALIDDATA
;
902 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
903 return D3DXERR_INVALIDDATA
;
905 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
906 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
909 HRESULT WINAPI
D3DXCreateCubeTexture(struct IDirect3DDevice9
*device
, UINT size
, UINT miplevels
,
910 DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DCubeTexture9
**texture
)
914 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device
, size
, miplevels
, usage
, format
,
917 if (!device
|| !texture
)
918 return D3DERR_INVALIDCALL
;
920 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &miplevels
, usage
, &format
, pool
);
924 TRACE("D3DXCheckCubeTextureRequirements failed\n");
928 return IDirect3DDevice9_CreateCubeTexture(device
, size
, miplevels
, usage
, format
, pool
, texture
, NULL
);
931 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
932 const void *data
, UINT datasize
, struct IDirect3DCubeTexture9
**texture
)
934 TRACE("(%p, %p, %u, %p)\n", device
, data
, datasize
, texture
);
936 return D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, datasize
, D3DX_DEFAULT
, D3DX_DEFAULT
,
937 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
940 HRESULT WINAPI
D3DXCreateVolumeTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
, UINT depth
,
941 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DVolumeTexture9
**texture
)
945 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device
, width
, height
, depth
,
946 miplevels
, usage
, format
, pool
, texture
);
948 if (!device
|| !texture
)
949 return D3DERR_INVALIDCALL
;
951 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
,
952 &miplevels
, usage
, &format
, pool
);
956 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
960 return IDirect3DDevice9_CreateVolumeTexture(device
, width
, height
, depth
, miplevels
,
961 usage
, format
, pool
, texture
, NULL
);
964 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9
*device
,
965 const char *filename
,
966 IDirect3DVolumeTexture9
**volume_texture
)
974 TRACE("(%p, %s, %p): relay\n",
975 device
, debugstr_a(filename
), volume_texture
);
977 if (!filename
) return D3DERR_INVALIDCALL
;
979 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
980 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
981 if (!filenameW
) return E_OUTOFMEMORY
;
982 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
984 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
985 HeapFree(GetProcessHeap(), 0, filenameW
);
986 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
988 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
989 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
991 UnmapViewOfFile(data
);
995 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9
*device
,
996 const WCHAR
*filename
,
997 IDirect3DVolumeTexture9
**volume_texture
)
1003 TRACE("(%p, %s, %p): relay\n",
1004 device
, debugstr_w(filename
), volume_texture
);
1006 if (!filename
) return D3DERR_INVALIDCALL
;
1008 hr
= map_view_of_file(filename
, &data
, &data_size
);
1009 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1011 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1012 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
1014 UnmapViewOfFile(data
);
1018 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9
*device
,
1019 const char *filename
,
1030 D3DXIMAGE_INFO
*src_info
,
1031 PALETTEENTRY
*palette
,
1032 IDirect3DVolumeTexture9
**volume_texture
)
1040 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1041 device
, debugstr_a(filename
), width
, height
, depth
, mip_levels
,
1042 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1043 palette
, volume_texture
);
1045 if (!filename
) return D3DERR_INVALIDCALL
;
1047 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
1048 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1049 if (!filenameW
) return E_OUTOFMEMORY
;
1050 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
1052 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
1053 HeapFree(GetProcessHeap(), 0, filenameW
);
1054 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1056 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1057 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1060 UnmapViewOfFile(data
);
1064 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9
*device
,
1065 const WCHAR
*filename
,
1076 D3DXIMAGE_INFO
*src_info
,
1077 PALETTEENTRY
*palette
,
1078 IDirect3DVolumeTexture9
**volume_texture
)
1084 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1085 device
, debugstr_w(filename
), width
, height
, depth
, mip_levels
,
1086 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1087 palette
, volume_texture
);
1089 if (!filename
) return D3DERR_INVALIDCALL
;
1091 hr
= map_view_of_file(filename
, &data
, &data_size
);
1092 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1094 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1095 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1098 UnmapViewOfFile(data
);
1102 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9
*device
,
1105 IDirect3DVolumeTexture9
**volume_texture
)
1107 TRACE("(%p, %p, %u, %p): relay\n", device
, data
, data_size
, volume_texture
);
1109 return D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1110 D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1111 0, NULL
, NULL
, volume_texture
);
1114 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1127 D3DXIMAGE_INFO
*info
,
1128 PALETTEENTRY
*palette
,
1129 IDirect3DVolumeTexture9
**volume_texture
)
1133 D3DXIMAGE_INFO image_info
;
1134 BOOL dynamic_texture
;
1135 BOOL file_width
= FALSE
;
1136 BOOL file_height
= FALSE
;
1137 BOOL file_depth
= FALSE
;
1138 BOOL file_format
= FALSE
;
1139 BOOL file_mip_levels
= FALSE
;
1140 IDirect3DVolumeTexture9
*tex
, *buftex
;
1142 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1143 device
, data
, data_size
, width
, height
, depth
, mip_levels
, usage
, format
, pool
,
1144 filter
, mip_filter
, color_key
, info
, palette
, volume_texture
);
1146 if (!device
|| !data
|| !data_size
|| !volume_texture
)
1147 return D3DERR_INVALIDCALL
;
1149 hr
= D3DXGetImageInfoFromFileInMemory(data
, data_size
, &image_info
);
1150 if (FAILED(hr
)) return hr
;
1152 if (image_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1153 return D3DXERR_INVALIDDATA
;
1155 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
1156 width
= image_info
.Width
;
1157 if (width
== D3DX_DEFAULT
)
1158 width
= make_pow2(image_info
.Width
);
1160 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
1161 height
= image_info
.Height
;
1162 if (height
== D3DX_DEFAULT
)
1163 height
= make_pow2(image_info
.Height
);
1165 if (depth
== 0 || depth
== D3DX_DEFAULT_NONPOW2
)
1166 depth
= image_info
.Depth
;
1167 if (depth
== D3DX_DEFAULT
)
1168 depth
= make_pow2(image_info
.Depth
);
1170 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1171 format
= image_info
.Format
;
1173 if (width
== D3DX_FROM_FILE
)
1176 width
= image_info
.Width
;
1179 if (height
== D3DX_FROM_FILE
)
1182 height
= image_info
.Height
;
1185 if (depth
== D3DX_FROM_FILE
)
1188 depth
= image_info
.Depth
;
1191 if (format
== D3DFMT_FROM_FILE
)
1194 format
= image_info
.Format
;
1197 if (mip_levels
== D3DX_FROM_FILE
)
1199 file_mip_levels
= TRUE
;
1200 mip_levels
= image_info
.MipLevels
;
1203 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
, &mip_levels
, usage
, &format
, pool
);
1204 if (FAILED(hr
)) return hr
;
1206 if ((file_width
&& width
!= image_info
.Width
)
1207 || (file_height
&& height
!= image_info
.Height
)
1208 || (file_depth
&& depth
!= image_info
.Depth
)
1209 || (file_format
&& format
!= image_info
.Format
)
1210 || (file_mip_levels
&& mip_levels
!= image_info
.MipLevels
))
1211 return D3DERR_NOTAVAILABLE
;
1213 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1215 return D3DERR_INVALIDCALL
;
1217 if (mip_levels
> image_info
.MipLevels
)
1219 FIXME("Generation of mipmaps for volume textures is not implemented yet.\n");
1220 mip_levels
= image_info
.MipLevels
;
1223 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1224 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1226 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, 0, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1231 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1234 if (FAILED(hr
)) return hr
;
1236 hr
= load_volume_texture_from_dds(tex
, data
, palette
, filter
, color_key
, &image_info
);
1239 IDirect3DVolumeTexture9_Release(tex
);
1245 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1248 IDirect3DVolumeTexture9_Release(buftex
);
1252 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1253 IDirect3DVolumeTexture9_Release(buftex
);
1259 *volume_texture
= tex
;
1263 static inline void fill_texture(const struct pixel_format_desc
*format
, BYTE
*pos
, const D3DXVECTOR4
*value
)
1267 for (c
= 0; c
< format
->bytes_per_pixel
; c
++)
1270 for (c
= 0; c
< 4; c
++)
1273 DWORD i
, v
= 0, mask32
= format
->bits
[c
] == 32 ? ~0U : ((1 << format
->bits
[c
]) - 1);
1278 comp_value
= value
->w
;
1281 comp_value
= value
->x
;
1284 comp_value
= value
->y
;
1287 comp_value
= value
->z
;
1291 if (format
->type
== FORMAT_ARGBF16
)
1292 v
= float_32_to_16(comp_value
);
1293 else if (format
->type
== FORMAT_ARGBF
)
1294 v
= *(DWORD
*)&comp_value
;
1295 else if (format
->type
== FORMAT_ARGB
)
1296 v
= comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
;
1298 FIXME("Unhandled format type %#x\n", format
->type
);
1300 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1304 if (format
->shift
[c
] > i
)
1306 mask
= mask32
<< (format
->shift
[c
] - i
);
1307 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1311 mask
= mask32
>> (i
- format
->shift
[c
]);
1312 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1319 HRESULT WINAPI
D3DXFillTexture(struct IDirect3DTexture9
*texture
, LPD3DXFILL2D function
, void *funcdata
)
1321 IDirect3DSurface9
*surface
, *temp_surface
;
1324 D3DSURFACE_DESC desc
;
1325 D3DLOCKED_RECT lock_rect
;
1327 D3DXVECTOR2 coord
, size
;
1328 const struct pixel_format_desc
*format
;
1332 TRACE("texture %p, function %p, funcdata %p.\n", texture
, function
, funcdata
);
1334 if (!texture
|| !function
)
1335 return D3DERR_INVALIDCALL
;
1337 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1339 for (m
= 0; m
< miplevels
; m
++)
1341 if (FAILED(hr
= IDirect3DTexture9_GetLevelDesc(texture
, m
, &desc
)))
1344 format
= get_format_info(desc
.Format
);
1345 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1347 FIXME("Unsupported texture format %#x.\n", desc
.Format
);
1348 return D3DERR_INVALIDCALL
;
1351 if (FAILED(hr
= IDirect3DTexture9_GetSurfaceLevel(texture
, m
, &surface
)))
1353 if (FAILED(hr
= lock_surface(surface
, NULL
, &lock_rect
, &temp_surface
, TRUE
)))
1355 IDirect3DSurface9_Release(surface
);
1359 size
.x
= 1.0f
/ desc
.Width
;
1360 size
.y
= 1.0f
/ desc
.Height
;
1362 data
= lock_rect
.pBits
;
1364 for (y
= 0; y
< desc
.Height
; y
++)
1366 /* The callback function expects the coordinates of the center
1368 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1370 for (x
= 0; x
< desc
.Width
; x
++)
1372 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1374 function(&value
, &coord
, &size
, funcdata
);
1376 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1379 if (FAILED(hr
= unlock_surface(surface
, NULL
, temp_surface
, TRUE
)))
1381 IDirect3DSurface9_Release(surface
);
1384 IDirect3DSurface9_Release(surface
);
1390 HRESULT WINAPI
D3DXFillTextureTX(struct IDirect3DTexture9
*texture
, ID3DXTextureShader
*texture_shader
)
1392 FIXME("texture %p, texture_shader %p stub.\n", texture
, texture_shader
);
1396 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1397 const void *src_data
,
1407 D3DXIMAGE_INFO
*src_info
,
1408 PALETTEENTRY
*palette
,
1409 IDirect3DCubeTexture9
**cube_texture
)
1413 UINT loaded_miplevels
;
1414 D3DXIMAGE_INFO img_info
;
1415 BOOL dynamic_texture
;
1416 BOOL file_size
= FALSE
;
1417 BOOL file_format
= FALSE
;
1418 BOOL file_mip_levels
= FALSE
;
1419 IDirect3DCubeTexture9
*tex
, *buftex
;
1421 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device
,
1422 src_data
, src_data_size
, size
, mip_levels
, usage
, format
, pool
, filter
, mip_filter
,
1423 color_key
, src_info
, palette
, cube_texture
);
1425 if (!device
|| !cube_texture
|| !src_data
|| !src_data_size
)
1426 return D3DERR_INVALIDCALL
;
1428 hr
= D3DXGetImageInfoFromFileInMemory(src_data
, src_data_size
, &img_info
);
1432 if (img_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1433 return D3DXERR_INVALIDDATA
;
1435 if (img_info
.Width
!= img_info
.Height
)
1436 return D3DXERR_INVALIDDATA
;
1438 if (size
== 0 || size
== D3DX_DEFAULT_NONPOW2
)
1439 size
= img_info
.Width
;
1440 if (size
== D3DX_DEFAULT
)
1441 size
= make_pow2(img_info
.Width
);
1443 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1444 format
= img_info
.Format
;
1446 if (size
== D3DX_FROM_FILE
)
1449 size
= img_info
.Width
;
1452 if (format
== D3DFMT_FROM_FILE
)
1455 format
= img_info
.Format
;
1458 if (mip_levels
== D3DX_FROM_FILE
)
1460 file_mip_levels
= TRUE
;
1461 mip_levels
= img_info
.MipLevels
;
1464 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &mip_levels
, usage
, &format
, pool
);
1468 if ((file_size
&& size
!= img_info
.Width
)
1469 || (file_format
&& format
!= img_info
.Format
)
1470 || (file_mip_levels
&& mip_levels
!= img_info
.MipLevels
))
1471 return D3DERR_NOTAVAILABLE
;
1473 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1475 return D3DERR_INVALIDCALL
;
1477 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1478 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1480 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, 0, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1485 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1491 hr
= load_cube_texture_from_dds(tex
, src_data
, palette
, filter
, color_key
, &img_info
);
1494 IDirect3DCubeTexture9_Release(tex
);
1498 loaded_miplevels
= min(IDirect3DCubeTexture9_GetLevelCount(tex
), img_info
.MipLevels
);
1499 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) tex
, palette
, loaded_miplevels
- 1, mip_filter
);
1502 IDirect3DCubeTexture9_Release(tex
);
1508 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1511 IDirect3DCubeTexture9_Release(buftex
);
1515 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1516 IDirect3DCubeTexture9_Release(buftex
);
1520 *src_info
= img_info
;
1522 *cube_texture
= tex
;
1527 HRESULT WINAPI
D3DXCreateCubeTextureFromFileA(IDirect3DDevice9
*device
,
1528 const char *src_filename
,
1529 IDirect3DCubeTexture9
**cube_texture
)
1537 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_a(src_filename
), cube_texture
);
1539 if (!src_filename
) return D3DERR_INVALIDCALL
;
1541 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1542 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1543 if (!filename
) return E_OUTOFMEMORY
;
1544 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1546 hr
= map_view_of_file(filename
, &data
, &data_size
);
1549 HeapFree(GetProcessHeap(), 0, filename
);
1550 return D3DXERR_INVALIDDATA
;
1553 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1554 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1556 UnmapViewOfFile(data
);
1557 HeapFree(GetProcessHeap(), 0, filename
);
1561 HRESULT WINAPI
D3DXCreateCubeTextureFromFileW(IDirect3DDevice9
*device
,
1562 const WCHAR
*src_filename
,
1563 IDirect3DCubeTexture9
**cube_texture
)
1569 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_w(src_filename
), cube_texture
);
1571 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1572 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1574 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1575 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1577 UnmapViewOfFile(data
);
1581 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9
*device
,
1582 const char *src_filename
,
1591 D3DXIMAGE_INFO
*image_info
,
1592 PALETTEENTRY
*palette
,
1593 IDirect3DCubeTexture9
**cube_texture
)
1601 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1602 device
, wine_dbgstr_a(src_filename
), size
, mip_levels
, usage
, format
,
1603 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1605 if (!src_filename
) return D3DERR_INVALIDCALL
;
1607 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1608 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1609 if (!filename
) return E_OUTOFMEMORY
;
1610 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1612 hr
= map_view_of_file(filename
, &data
, &data_size
);
1615 HeapFree(GetProcessHeap(), 0, filename
);
1616 return D3DXERR_INVALIDDATA
;
1619 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1620 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1622 UnmapViewOfFile(data
);
1623 HeapFree(GetProcessHeap(), 0, filename
);
1627 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9
*device
,
1628 const WCHAR
*src_filename
,
1637 D3DXIMAGE_INFO
*image_info
,
1638 PALETTEENTRY
*palette
,
1639 IDirect3DCubeTexture9
**cube_texture
)
1645 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1646 device
, wine_dbgstr_w(src_filename
), size
, mip_levels
, usage
, format
,
1647 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1649 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1650 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1652 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1653 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1655 UnmapViewOfFile(data
);
1669 static float get_cube_coord(enum cube_coord coord
, unsigned int x
, unsigned int y
, unsigned int size
)
1676 return size
- x
- 0.5f
;
1680 return size
- y
- 0.5f
;
1686 ERR("Unexpected coordinate value\n");
1691 HRESULT WINAPI
D3DXFillCubeTexture(struct IDirect3DCubeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1695 D3DSURFACE_DESC desc
;
1696 D3DLOCKED_RECT lock_rect
;
1698 D3DXVECTOR3 coord
, size
;
1699 const struct pixel_format_desc
*format
;
1701 static const enum cube_coord coordmap
[6][3] =
1703 {ONE
, YCOORDINV
, XCOORDINV
},
1704 {ZERO
, YCOORDINV
, XCOORD
},
1705 {XCOORD
, ONE
, YCOORD
},
1706 {XCOORD
, ZERO
, YCOORDINV
},
1707 {XCOORD
, YCOORDINV
, ONE
},
1708 {XCOORDINV
, YCOORDINV
, ZERO
}
1711 if (texture
== NULL
|| function
== NULL
)
1712 return D3DERR_INVALIDCALL
;
1714 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1716 for (m
= 0; m
< miplevels
; m
++)
1718 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1719 return D3DERR_INVALIDCALL
;
1721 format
= get_format_info(desc
.Format
);
1722 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1724 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1725 return D3DERR_INVALIDCALL
;
1728 for (f
= 0; f
< 6; f
++)
1730 if (FAILED(IDirect3DCubeTexture9_LockRect(texture
, f
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1731 return D3DERR_INVALIDCALL
;
1733 size
.x
= (f
== 0) || (f
== 1) ? 0.0f
: 2.0f
/ desc
.Width
;
1734 size
.y
= (f
== 2) || (f
== 3) ? 0.0f
: 2.0f
/ desc
.Width
;
1735 size
.z
= (f
== 4) || (f
== 5) ? 0.0f
: 2.0f
/ desc
.Width
;
1737 data
= lock_rect
.pBits
;
1739 for (y
= 0; y
< desc
.Height
; y
++)
1741 for (x
= 0; x
< desc
.Width
; x
++)
1743 coord
.x
= get_cube_coord(coordmap
[f
][0], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1744 coord
.y
= get_cube_coord(coordmap
[f
][1], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1745 coord
.z
= get_cube_coord(coordmap
[f
][2], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1747 function(&value
, &coord
, &size
, funcdata
);
1749 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1752 IDirect3DCubeTexture9_UnlockRect(texture
, f
, m
);
1759 HRESULT WINAPI
D3DXFillCubeTextureTX(struct IDirect3DCubeTexture9
*texture
, ID3DXTextureShader
*texture_shader
)
1761 FIXME("texture %p, texture_shader %p stub.\n", texture
, texture_shader
);
1765 HRESULT WINAPI
D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1769 D3DVOLUME_DESC desc
;
1770 D3DLOCKED_BOX lock_box
;
1772 D3DXVECTOR3 coord
, size
;
1773 const struct pixel_format_desc
*format
;
1776 if (texture
== NULL
|| function
== NULL
)
1777 return D3DERR_INVALIDCALL
;
1779 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1781 for (m
= 0; m
< miplevels
; m
++)
1783 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1784 return D3DERR_INVALIDCALL
;
1786 format
= get_format_info(desc
.Format
);
1787 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1789 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1790 return D3DERR_INVALIDCALL
;
1793 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture
, m
, &lock_box
, NULL
, D3DLOCK_DISCARD
)))
1794 return D3DERR_INVALIDCALL
;
1796 size
.x
= 1.0f
/ desc
.Width
;
1797 size
.y
= 1.0f
/ desc
.Height
;
1798 size
.z
= 1.0f
/ desc
.Depth
;
1800 data
= lock_box
.pBits
;
1802 for (z
= 0; z
< desc
.Depth
; z
++)
1804 /* The callback function expects the coordinates of the center
1806 coord
.z
= (z
+ 0.5f
) / desc
.Depth
;
1808 for (y
= 0; y
< desc
.Height
; y
++)
1810 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1812 for (x
= 0; x
< desc
.Width
; x
++)
1814 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1816 function(&value
, &coord
, &size
, funcdata
);
1818 fill_texture(format
, data
+ z
* lock_box
.SlicePitch
+ y
* lock_box
.RowPitch
1819 + x
* format
->bytes_per_pixel
, &value
);
1823 IDirect3DVolumeTexture9_UnlockBox(texture
, m
);
1829 HRESULT WINAPI
D3DXFillVolumeTextureTX(struct IDirect3DVolumeTexture9
*texture
, ID3DXTextureShader
*texture_shader
)
1831 FIXME("texture %p, texture_shader %p stub.\n", texture
, texture_shader
);
1835 HRESULT WINAPI
D3DXSaveTextureToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1836 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1841 ID3DXBuffer
*buffer
;
1843 TRACE("(%s, %#x, %p, %p): relay\n",
1844 wine_dbgstr_a(dst_filename
), file_format
, src_texture
, src_palette
);
1846 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1848 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1849 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1850 if (!filename
) return E_OUTOFMEMORY
;
1851 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1853 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1856 hr
= write_buffer_to_file(filename
, buffer
);
1857 ID3DXBuffer_Release(buffer
);
1860 HeapFree(GetProcessHeap(), 0, filename
);
1864 HRESULT WINAPI
D3DXSaveTextureToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1865 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1868 ID3DXBuffer
*buffer
;
1870 TRACE("(%s, %#x, %p, %p): relay\n",
1871 wine_dbgstr_w(dst_filename
), file_format
, src_texture
, src_palette
);
1873 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1875 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1878 hr
= write_buffer_to_file(dst_filename
, buffer
);
1879 ID3DXBuffer_Release(buffer
);
1885 HRESULT WINAPI
D3DXSaveTextureToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1886 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1889 D3DRESOURCETYPE type
;
1890 IDirect3DSurface9
*surface
;
1892 TRACE("(%p, %#x, %p, %p)\n",
1893 dst_buffer
, file_format
, src_texture
, src_palette
);
1895 if (!dst_buffer
|| !src_texture
) return D3DERR_INVALIDCALL
;
1897 if (file_format
== D3DXIFF_DDS
)
1899 FIXME("DDS file format isn't supported yet\n");
1903 type
= IDirect3DBaseTexture9_GetType(src_texture
);
1906 case D3DRTYPE_TEXTURE
:
1907 case D3DRTYPE_CUBETEXTURE
:
1908 hr
= get_surface(type
, src_texture
, D3DCUBEMAP_FACE_POSITIVE_X
, 0, &surface
);
1910 case D3DRTYPE_VOLUMETEXTURE
:
1911 FIXME("Volume textures aren't supported yet\n");
1914 return D3DERR_INVALIDCALL
;
1919 hr
= D3DXSaveSurfaceToFileInMemory(dst_buffer
, file_format
, surface
, src_palette
, NULL
);
1920 IDirect3DSurface9_Release(surface
);
1926 HRESULT WINAPI
D3DXComputeNormalMap(IDirect3DTexture9
*texture
, IDirect3DTexture9
*src_texture
,
1927 const PALETTEENTRY
*src_palette
, DWORD flags
, DWORD channel
, float amplitude
)
1929 FIXME("texture %p, src_texture %p, src_palette %p, flags %#x, channel %u, amplitude %.8e stub.\n",
1930 texture
, src_texture
, src_palette
, flags
, channel
, amplitude
);