2 * Copyright 2009 Tony Wasserka
3 * Copyright 2010 Christian Costa
4 * Copyright 2010 Owen Rudge for CodeWeavers
5 * Copyright 2010 Matteo Bruni for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/unicode.h"
23 #include "wine/debug.h"
24 #include "d3dx9_36_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
28 /* Returns TRUE if num is a power of 2, FALSE if not, or if 0 */
29 static BOOL
is_pow2(UINT num
)
31 return !(num
& (num
- 1));
34 /* Returns the smallest power of 2 which is greater than or equal to num */
35 static UINT
make_pow2(UINT num
)
39 /* In the unlikely event somebody passes a large value, make sure we don't enter an infinite loop */
40 if (num
>= 0x80000000)
49 static HRESULT
get_surface(D3DRESOURCETYPE type
, struct IDirect3DBaseTexture9
*tex
,
50 int face
, UINT level
, struct IDirect3DSurface9
**surf
)
54 case D3DRTYPE_TEXTURE
:
55 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9
*) tex
, level
, surf
);
56 case D3DRTYPE_CUBETEXTURE
:
57 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9
*) tex
, face
, level
, surf
);
59 ERR("Unexpected texture type\n");
64 HRESULT WINAPI
D3DXFilterTexture(IDirect3DBaseTexture9
*texture
,
65 const PALETTEENTRY
*palette
,
73 TRACE("(%p, %p, %u, %#x)\n", texture
, palette
, srclevel
, filter
);
76 return D3DERR_INVALIDCALL
;
78 if ((filter
& 0xFFFF) > D3DX_FILTER_BOX
&& filter
!= D3DX_DEFAULT
)
79 return D3DERR_INVALIDCALL
;
81 if (srclevel
== D3DX_DEFAULT
)
83 else if (srclevel
>= IDirect3DBaseTexture9_GetLevelCount(texture
))
84 return D3DERR_INVALIDCALL
;
86 switch (type
= IDirect3DBaseTexture9_GetType(texture
))
88 case D3DRTYPE_TEXTURE
:
89 case D3DRTYPE_CUBETEXTURE
:
91 IDirect3DSurface9
*topsurf
, *mipsurf
;
95 if (type
== D3DRTYPE_TEXTURE
)
98 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
103 IDirect3DCubeTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
106 if (filter
== D3DX_DEFAULT
)
108 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
))
109 filter
= D3DX_FILTER_BOX
;
111 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
114 for (i
= 0; i
< numfaces
; i
++)
116 level
= srclevel
+ 1;
117 hr
= get_surface(type
, texture
, i
, srclevel
, &topsurf
);
120 return D3DERR_INVALIDCALL
;
122 while (get_surface(type
, texture
, i
, level
, &mipsurf
) == D3D_OK
)
124 hr
= D3DXLoadSurfaceFromSurface(mipsurf
, palette
, NULL
, topsurf
, palette
, NULL
, filter
, 0);
125 IDirect3DSurface9_Release(topsurf
);
134 IDirect3DSurface9_Release(topsurf
);
142 case D3DRTYPE_VOLUMETEXTURE
:
145 int level
, level_count
;
146 IDirect3DVolume9
*top_volume
, *mip_volume
;
147 IDirect3DVolumeTexture9
*volume_texture
= (IDirect3DVolumeTexture9
*) texture
;
149 IDirect3DVolumeTexture9_GetLevelDesc(volume_texture
, srclevel
, &desc
);
151 if (filter
== D3DX_DEFAULT
)
153 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
) && is_pow2(desc
.Depth
))
154 filter
= D3DX_FILTER_BOX
;
156 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
159 hr
= IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, srclevel
, &top_volume
);
163 level_count
= IDirect3DVolumeTexture9_GetLevelCount(volume_texture
);
164 for (level
= srclevel
+ 1; level
< level_count
; level
++)
166 IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, level
, &mip_volume
);
167 hr
= D3DXLoadVolumeFromVolume(mip_volume
, palette
, NULL
, top_volume
, palette
, NULL
, filter
, 0);
168 IDirect3DVolume9_Release(top_volume
);
169 top_volume
= mip_volume
;
175 IDirect3DVolume9_Release(top_volume
);
183 return D3DERR_INVALIDCALL
;
187 static D3DFORMAT
get_luminance_replacement_format(D3DFORMAT format
)
191 D3DFORMAT luminance_format
;
192 D3DFORMAT replacement_format
;
193 } luminance_replacements
[] =
195 {D3DFMT_L8
, D3DFMT_X8R8G8B8
},
196 {D3DFMT_A8L8
, D3DFMT_A8R8G8B8
},
197 {D3DFMT_A4L4
, D3DFMT_A4R4G4B4
},
198 {D3DFMT_L16
, D3DFMT_A16B16G16R16
}
202 for (i
= 0; i
< sizeof(luminance_replacements
) / sizeof(luminance_replacements
[0]); ++i
)
203 if (format
== luminance_replacements
[i
].luminance_format
)
204 return luminance_replacements
[i
].replacement_format
;
208 HRESULT WINAPI
D3DXCheckTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
209 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
211 UINT w
= (width
&& *width
) ? *width
: 1;
212 UINT h
= (height
&& *height
) ? *height
: 1;
214 D3DDEVICE_CREATION_PARAMETERS params
;
215 IDirect3D9
*d3d
= NULL
;
218 D3DFORMAT usedformat
= D3DFMT_UNKNOWN
;
219 const struct pixel_format_desc
*fmt
;
221 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, miplevels
, usage
, format
, pool
);
224 return D3DERR_INVALIDCALL
;
227 if (usage
== D3DX_DEFAULT
)
229 if (usage
& (D3DUSAGE_WRITEONLY
| D3DUSAGE_DONOTCLIP
| D3DUSAGE_POINTS
| D3DUSAGE_RTPATCHES
| D3DUSAGE_NPATCHES
))
230 return D3DERR_INVALIDCALL
;
233 if ((pool
!= D3DPOOL_DEFAULT
) && (pool
!= D3DPOOL_MANAGED
) && (pool
!= D3DPOOL_SYSTEMMEM
) && (pool
!= D3DPOOL_SCRATCH
))
234 return D3DERR_INVALIDCALL
;
239 TRACE("Requested format %x\n", *format
);
240 usedformat
= *format
;
243 hr
= IDirect3DDevice9_GetDirect3D(device
, &d3d
);
248 hr
= IDirect3DDevice9_GetCreationParameters(device
, ¶ms
);
253 hr
= IDirect3DDevice9_GetDisplayMode(device
, 0, &mode
);
258 if ((usedformat
== D3DFMT_UNKNOWN
) || (usedformat
== D3DX_DEFAULT
))
259 usedformat
= D3DFMT_A8R8G8B8
;
261 fmt
= get_format_info(usedformat
);
263 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
, mode
.Format
,
264 usage
, D3DRTYPE_TEXTURE
, usedformat
);
268 int bestscore
= INT_MIN
, i
= 0, j
;
269 unsigned int channels
;
270 const struct pixel_format_desc
*curfmt
, *bestfmt
= NULL
;
272 TRACE("Requested format not supported, looking for a fallback.\n");
276 FIXME("Pixel format %x not handled\n", usedformat
);
279 fmt
= get_format_info(get_luminance_replacement_format(usedformat
));
281 allow_24bits
= fmt
->bytes_per_pixel
== 3;
282 channels
= !!fmt
->bits
[0] + !!fmt
->bits
[1] + !!fmt
->bits
[2] + !!fmt
->bits
[3];
283 usedformat
= D3DFMT_UNKNOWN
;
285 while ((curfmt
= get_format_info_idx(i
)))
287 unsigned int curchannels
= !!curfmt
->bits
[0] + !!curfmt
->bits
[1]
288 + !!curfmt
->bits
[2] + !!curfmt
->bits
[3];
293 if (curchannels
< channels
)
295 if (curfmt
->bytes_per_pixel
== 3 && !allow_24bits
)
298 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
,
299 mode
.Format
, usage
, D3DRTYPE_TEXTURE
, curfmt
->format
);
303 /* This format can be used, let's evaluate it.
304 Weights chosen quite arbitrarily... */
305 score
= 512 * (curfmt
->type
== fmt
->type
);
306 score
-= 32 * (curchannels
- channels
);
308 for (j
= 0; j
< 4; j
++)
310 int diff
= curfmt
->bits
[j
] - fmt
->bits
[j
];
311 score
-= (diff
< 0 ? -diff
* 8 : diff
) * (j
== 0 ? 1 : 2);
314 if (score
> bestscore
)
317 usedformat
= curfmt
->format
;
325 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
326 return D3DERR_INVALIDCALL
;
328 if ((w
== D3DX_DEFAULT
) && (h
== D3DX_DEFAULT
))
330 else if (w
== D3DX_DEFAULT
)
331 w
= (height
? h
: 256);
332 else if (h
== D3DX_DEFAULT
)
333 h
= (width
? w
: 256);
335 if (fmt
->block_width
!= 1 || fmt
->block_height
!= 1)
337 if (w
< fmt
->block_width
)
338 w
= fmt
->block_width
;
339 if (h
< fmt
->block_height
)
340 h
= fmt
->block_height
;
343 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(w
)))
346 if (w
> caps
.MaxTextureWidth
)
347 w
= caps
.MaxTextureWidth
;
349 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(h
)))
352 if (h
> caps
.MaxTextureHeight
)
353 h
= caps
.MaxTextureHeight
;
355 if (caps
.TextureCaps
& D3DPTEXTURECAPS_SQUAREONLY
)
369 if (miplevels
&& (usage
& D3DUSAGE_AUTOGENMIPMAP
))
376 UINT max_mipmaps
= 1;
378 if (!width
&& !height
)
379 max_mipmaps
= 9; /* number of mipmaps in a 256x256 texture */
382 UINT max_dimen
= max(w
, h
);
384 while (max_dimen
> 1)
391 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
392 *miplevels
= max_mipmaps
;
398 IDirect3D9_Release(d3d
);
403 if (usedformat
== D3DFMT_UNKNOWN
)
405 WARN("Couldn't find a suitable pixel format\n");
406 return D3DERR_NOTAVAILABLE
;
409 TRACE("Format chosen: %x\n", usedformat
);
411 *format
= usedformat
;
416 HRESULT WINAPI
D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*size
,
417 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
420 UINT s
= (size
&& *size
) ? *size
: 256;
423 TRACE("(%p, %p, %p, %u, %p, %u)\n", device
, size
, miplevels
, usage
, format
, pool
);
425 if (s
== D3DX_DEFAULT
)
428 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
429 return D3DERR_INVALIDCALL
;
431 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP
))
432 return D3DERR_NOTAVAILABLE
;
434 /* ensure width/height is power of 2 */
435 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP_POW2
) && (!is_pow2(s
)))
438 hr
= D3DXCheckTextureRequirements(device
, &s
, &s
, miplevels
, usage
, format
, pool
);
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
= D3DXCheckTextureRequirements(device
, &w
, &h
, NULL
, usage
, format
, pool
);
471 if (d
== D3DX_DEFAULT
)
474 /* ensure width/height is power of 2 */
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 %#x, 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
< sizeof(replacement_formats
) / sizeof(replacement_formats
[0]); ++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 IDirect3DTexture9
**texptr
;
571 IDirect3DTexture9
*buftex
;
572 IDirect3DSurface9
*surface
;
573 BOOL dynamic_texture
, format_specified
= FALSE
;
574 D3DXIMAGE_INFO imginfo
;
575 UINT loaded_miplevels
, skip_levels
;
579 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u,"
580 " usage %#x, format %#x, pool %#x, filter %#x, mipfilter %#x, colorkey %#x,"
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 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
.MipLevels
< miplevels
&& (D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
))
664 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet\n");
665 miplevels
= imginfo
.MipLevels
;
667 if (imginfo
.ResourceType
== D3DRTYPE_VOLUMETEXTURE
668 && D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
&& miplevels
> 1)
670 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet.\n");
674 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
675 return D3DERR_INVALIDCALL
;
677 /* Create the to-be-filled texture */
678 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
679 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
681 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
686 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
692 FIXME("Texture creation failed.\n");
697 TRACE("Texture created correctly. Now loading the texture data into it.\n");
698 if (imginfo
.ImageFileFormat
!= D3DXIFF_DDS
)
700 IDirect3DTexture9_GetSurfaceLevel(*texptr
, 0, &surface
);
701 hr
= D3DXLoadSurfaceFromFileInMemory(surface
, palette
, NULL
, srcdata
, srcdatasize
, NULL
, filter
, colorkey
, NULL
);
702 IDirect3DSurface9_Release(surface
);
703 loaded_miplevels
= min(IDirect3DTexture9_GetLevelCount(*texptr
), imginfo
.MipLevels
);
707 hr
= load_texture_from_dds(*texptr
, srcdata
, palette
, filter
, colorkey
, &imginfo
, skip_levels
,
713 FIXME("Texture loading failed.\n");
714 IDirect3DTexture9_Release(*texptr
);
719 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) *texptr
, palette
, loaded_miplevels
- 1, mipfilter
);
722 FIXME("Texture filtering failed.\n");
723 IDirect3DTexture9_Release(*texptr
);
728 /* Move the data to the actual texture if necessary */
729 if (texptr
== &buftex
)
731 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
735 IDirect3DTexture9_Release(buftex
);
740 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)(*texture
));
741 IDirect3DTexture9_Release(buftex
);
750 HRESULT WINAPI
D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
751 const void *srcdata
, UINT srcdatasize
, struct IDirect3DTexture9
**texture
)
753 TRACE("(%p, %p, %d, %p)\n", device
, srcdata
, srcdatasize
, texture
);
755 return D3DXCreateTextureFromFileInMemoryEx(device
, srcdata
, srcdatasize
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
756 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
759 HRESULT WINAPI
D3DXCreateTextureFromFileExW(struct IDirect3DDevice9
*device
, const WCHAR
*srcfile
,
760 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
761 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
762 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
768 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
769 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
770 device
, debugstr_w(srcfile
), width
, height
, miplevels
, usage
, format
,
771 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
774 return D3DERR_INVALIDCALL
;
776 hr
= map_view_of_file(srcfile
, &buffer
, &size
);
778 return D3DXERR_INVALIDDATA
;
780 hr
= D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
, usage
, format
, pool
,
781 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
783 UnmapViewOfFile(buffer
);
788 HRESULT WINAPI
D3DXCreateTextureFromFileExA(struct IDirect3DDevice9
*device
, const char *srcfile
,
789 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
790 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
791 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
797 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
798 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
799 device
, debugstr_a(srcfile
), width
, height
, miplevels
, usage
, format
,
800 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
802 if (!device
|| !srcfile
|| !texture
)
803 return D3DERR_INVALIDCALL
;
805 len
= MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, NULL
, 0);
806 widename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*widename
));
807 MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, widename
, len
);
809 hr
= D3DXCreateTextureFromFileExW(device
, widename
, width
, height
, miplevels
,
810 usage
, format
, pool
, filter
, mipfilter
,
811 colorkey
, srcinfo
, palette
, texture
);
813 HeapFree(GetProcessHeap(), 0, widename
);
817 HRESULT WINAPI
D3DXCreateTextureFromFileA(struct IDirect3DDevice9
*device
,
818 const char *srcfile
, struct IDirect3DTexture9
**texture
)
820 TRACE("(%p, %s, %p)\n", device
, debugstr_a(srcfile
), texture
);
822 return D3DXCreateTextureFromFileExA(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
823 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
826 HRESULT WINAPI
D3DXCreateTextureFromFileW(struct IDirect3DDevice9
*device
,
827 const WCHAR
*srcfile
, struct IDirect3DTexture9
**texture
)
829 TRACE("(%p, %s, %p)\n", device
, debugstr_w(srcfile
), texture
);
831 return D3DXCreateTextureFromFileExW(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
832 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
836 HRESULT WINAPI
D3DXCreateTextureFromResourceA(struct IDirect3DDevice9
*device
,
837 HMODULE srcmodule
, const char *resource
, struct IDirect3DTexture9
**texture
)
839 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_a(resource
));
841 return D3DXCreateTextureFromResourceExA(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
842 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
845 HRESULT WINAPI
D3DXCreateTextureFromResourceW(struct IDirect3DDevice9
*device
,
846 HMODULE srcmodule
, const WCHAR
*resource
, struct IDirect3DTexture9
**texture
)
848 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_w(resource
));
850 return D3DXCreateTextureFromResourceExW(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
851 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
854 HRESULT WINAPI
D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
855 const char *resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
856 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
857 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
863 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
864 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
865 device
, srcmodule
, debugstr_a(resource
), width
, height
, miplevels
, usage
, format
,
866 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
868 if (!device
|| !texture
)
869 return D3DERR_INVALIDCALL
;
871 if (!(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_RCDATA
))
872 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
873 && !(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_BITMAP
)))
874 return D3DXERR_INVALIDDATA
;
876 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
877 return D3DXERR_INVALIDDATA
;
879 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
880 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
883 HRESULT WINAPI
D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
884 const WCHAR
*resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
885 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
886 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
892 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
893 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
894 device
, srcmodule
, debugstr_w(resource
), width
, height
, miplevels
, usage
, format
,
895 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
897 if (!device
|| !texture
)
898 return D3DERR_INVALIDCALL
;
900 if (!(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_RCDATA
))
901 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
902 && !(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_BITMAP
)))
903 return D3DXERR_INVALIDDATA
;
905 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
906 return D3DXERR_INVALIDDATA
;
908 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
909 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
912 HRESULT WINAPI
D3DXCreateCubeTexture(struct IDirect3DDevice9
*device
, UINT size
, UINT miplevels
,
913 DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DCubeTexture9
**texture
)
917 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device
, size
, miplevels
, usage
, format
,
920 if (!device
|| !texture
)
921 return D3DERR_INVALIDCALL
;
923 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &miplevels
, usage
, &format
, pool
);
927 TRACE("D3DXCheckCubeTextureRequirements failed\n");
931 return IDirect3DDevice9_CreateCubeTexture(device
, size
, miplevels
, usage
, format
, pool
, texture
, NULL
);
934 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
935 const void *data
, UINT datasize
, struct IDirect3DCubeTexture9
**texture
)
937 TRACE("(%p, %p, %u, %p)\n", device
, data
, datasize
, texture
);
939 return D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, datasize
, D3DX_DEFAULT
, D3DX_DEFAULT
,
940 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
943 HRESULT WINAPI
D3DXCreateVolumeTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
, UINT depth
,
944 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DVolumeTexture9
**texture
)
948 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device
, width
, height
, depth
,
949 miplevels
, usage
, format
, pool
, texture
);
951 if (!device
|| !texture
)
952 return D3DERR_INVALIDCALL
;
954 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
,
955 &miplevels
, usage
, &format
, pool
);
959 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
963 return IDirect3DDevice9_CreateVolumeTexture(device
, width
, height
, depth
, miplevels
,
964 usage
, format
, pool
, texture
, NULL
);
967 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9
*device
,
968 const char *filename
,
969 IDirect3DVolumeTexture9
**volume_texture
)
977 TRACE("(%p, %s, %p): relay\n",
978 device
, debugstr_a(filename
), volume_texture
);
980 if (!filename
) return D3DERR_INVALIDCALL
;
982 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
983 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
984 if (!filenameW
) return E_OUTOFMEMORY
;
985 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
987 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
988 HeapFree(GetProcessHeap(), 0, filenameW
);
989 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
991 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
992 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
994 UnmapViewOfFile(data
);
998 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9
*device
,
999 const WCHAR
*filename
,
1000 IDirect3DVolumeTexture9
**volume_texture
)
1006 TRACE("(%p, %s, %p): relay\n",
1007 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("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1044 device
, debugstr_a(filename
), width
, height
, depth
, mip_levels
,
1045 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1046 palette
, volume_texture
);
1048 if (!filename
) return D3DERR_INVALIDCALL
;
1050 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
1051 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1052 if (!filenameW
) return E_OUTOFMEMORY
;
1053 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
1055 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
1056 HeapFree(GetProcessHeap(), 0, filenameW
);
1057 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1059 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1060 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1063 UnmapViewOfFile(data
);
1067 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9
*device
,
1068 const WCHAR
*filename
,
1079 D3DXIMAGE_INFO
*src_info
,
1080 PALETTEENTRY
*palette
,
1081 IDirect3DVolumeTexture9
**volume_texture
)
1087 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1088 device
, debugstr_w(filename
), width
, height
, depth
, mip_levels
,
1089 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1090 palette
, volume_texture
);
1092 if (!filename
) return D3DERR_INVALIDCALL
;
1094 hr
= map_view_of_file(filename
, &data
, &data_size
);
1095 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1097 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1098 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1101 UnmapViewOfFile(data
);
1105 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9
*device
,
1108 IDirect3DVolumeTexture9
**volume_texture
)
1110 TRACE("(%p, %p, %u, %p): relay\n", device
, data
, data_size
, volume_texture
);
1112 return D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1113 D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1114 0, NULL
, NULL
, volume_texture
);
1117 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1130 D3DXIMAGE_INFO
*info
,
1131 PALETTEENTRY
*palette
,
1132 IDirect3DVolumeTexture9
**volume_texture
)
1136 D3DXIMAGE_INFO image_info
;
1137 BOOL dynamic_texture
;
1138 BOOL file_width
= FALSE
;
1139 BOOL file_height
= FALSE
;
1140 BOOL file_depth
= FALSE
;
1141 BOOL file_format
= FALSE
;
1142 BOOL file_mip_levels
= FALSE
;
1143 IDirect3DVolumeTexture9
*tex
, *buftex
;
1145 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1146 device
, data
, data_size
, width
, height
, depth
, mip_levels
, usage
, format
, pool
,
1147 filter
, mip_filter
, color_key
, info
, palette
, volume_texture
);
1149 if (!device
|| !data
|| !data_size
|| !volume_texture
)
1150 return D3DERR_INVALIDCALL
;
1152 hr
= D3DXGetImageInfoFromFileInMemory(data
, data_size
, &image_info
);
1153 if (FAILED(hr
)) return hr
;
1155 if (image_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1156 return D3DXERR_INVALIDDATA
;
1158 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
1159 width
= image_info
.Width
;
1160 if (width
== D3DX_DEFAULT
)
1161 width
= make_pow2(image_info
.Width
);
1163 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
1164 height
= image_info
.Height
;
1165 if (height
== D3DX_DEFAULT
)
1166 height
= make_pow2(image_info
.Height
);
1168 if (depth
== 0 || depth
== D3DX_DEFAULT_NONPOW2
)
1169 depth
= image_info
.Depth
;
1170 if (depth
== D3DX_DEFAULT
)
1171 depth
= make_pow2(image_info
.Depth
);
1173 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1174 format
= image_info
.Format
;
1176 if (width
== D3DX_FROM_FILE
)
1179 width
= image_info
.Width
;
1182 if (height
== D3DX_FROM_FILE
)
1185 height
= image_info
.Height
;
1188 if (depth
== D3DX_FROM_FILE
)
1191 depth
= image_info
.Depth
;
1194 if (format
== D3DFMT_FROM_FILE
)
1197 format
= image_info
.Format
;
1200 if (mip_levels
== D3DX_FROM_FILE
)
1202 file_mip_levels
= TRUE
;
1203 mip_levels
= image_info
.MipLevels
;
1206 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
, &mip_levels
, usage
, &format
, pool
);
1207 if (FAILED(hr
)) return hr
;
1209 if ((file_width
&& width
!= image_info
.Width
)
1210 || (file_height
&& height
!= image_info
.Height
)
1211 || (file_depth
&& depth
!= image_info
.Depth
)
1212 || (file_format
&& format
!= image_info
.Format
)
1213 || (file_mip_levels
&& mip_levels
!= image_info
.MipLevels
))
1214 return D3DERR_NOTAVAILABLE
;
1216 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1218 return D3DERR_INVALIDCALL
;
1220 if (mip_levels
> image_info
.MipLevels
)
1222 FIXME("Generation of mipmaps for volume textures is not implemented yet\n");
1223 mip_levels
= image_info
.MipLevels
;
1226 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1227 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1229 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1234 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1238 if (FAILED(hr
)) return hr
;
1240 hr
= load_volume_texture_from_dds(tex
, data
, palette
, filter
, color_key
, &image_info
);
1243 IDirect3DVolumeTexture9_Release(tex
);
1249 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1252 IDirect3DVolumeTexture9_Release(buftex
);
1256 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1257 IDirect3DVolumeTexture9_Release(buftex
);
1263 *volume_texture
= tex
;
1267 static inline void fill_texture(const struct pixel_format_desc
*format
, BYTE
*pos
, const D3DXVECTOR4
*value
)
1271 for (c
= 0; c
< format
->bytes_per_pixel
; c
++)
1274 for (c
= 0; c
< 4; c
++)
1277 DWORD i
, v
= 0, mask32
= format
->bits
[c
] == 32 ? ~0U : ((1 << format
->bits
[c
]) - 1);
1282 comp_value
= value
->w
;
1285 comp_value
= value
->x
;
1288 comp_value
= value
->y
;
1291 comp_value
= value
->z
;
1295 if (format
->type
== FORMAT_ARGBF16
)
1296 v
= float_32_to_16(comp_value
);
1297 else if (format
->type
== FORMAT_ARGBF
)
1298 v
= *(DWORD
*)&comp_value
;
1299 else if (format
->type
== FORMAT_ARGB
)
1300 v
= comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
;
1302 FIXME("Unhandled format type %#x\n", format
->type
);
1304 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1308 if (format
->shift
[c
] > i
)
1310 mask
= mask32
<< (format
->shift
[c
] - i
);
1311 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1315 mask
= mask32
>> (i
- format
->shift
[c
]);
1316 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1323 HRESULT WINAPI
D3DXFillTexture(struct IDirect3DTexture9
*texture
, LPD3DXFILL2D function
, void *funcdata
)
1327 D3DSURFACE_DESC desc
;
1328 D3DLOCKED_RECT lock_rect
;
1330 D3DXVECTOR2 coord
, size
;
1331 const struct pixel_format_desc
*format
;
1334 if (texture
== NULL
|| function
== NULL
)
1335 return D3DERR_INVALIDCALL
;
1337 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1339 for (m
= 0; m
< miplevels
; m
++)
1341 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture
, m
, &desc
)))
1342 return D3DERR_INVALIDCALL
;
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(IDirect3DTexture9_LockRect(texture
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1352 return D3DERR_INVALIDCALL
;
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 IDirect3DTexture9_UnlockRect(texture
, m
);
1380 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1381 const void *src_data
,
1391 D3DXIMAGE_INFO
*src_info
,
1392 PALETTEENTRY
*palette
,
1393 IDirect3DCubeTexture9
**cube_texture
)
1397 UINT loaded_miplevels
;
1398 D3DXIMAGE_INFO img_info
;
1399 BOOL dynamic_texture
;
1400 BOOL file_size
= FALSE
;
1401 BOOL file_format
= FALSE
;
1402 BOOL file_mip_levels
= FALSE
;
1403 IDirect3DCubeTexture9
*tex
, *buftex
;
1405 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device
,
1406 src_data
, src_data_size
, size
, mip_levels
, usage
, format
, pool
, filter
, mip_filter
,
1407 color_key
, src_info
, palette
, cube_texture
);
1409 if (!device
|| !cube_texture
|| !src_data
|| !src_data_size
)
1410 return D3DERR_INVALIDCALL
;
1412 hr
= D3DXGetImageInfoFromFileInMemory(src_data
, src_data_size
, &img_info
);
1416 if (img_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1417 return D3DXERR_INVALIDDATA
;
1419 if (img_info
.Width
!= img_info
.Height
)
1420 return D3DXERR_INVALIDDATA
;
1422 if (size
== 0 || size
== D3DX_DEFAULT_NONPOW2
)
1423 size
= img_info
.Width
;
1424 if (size
== D3DX_DEFAULT
)
1425 size
= make_pow2(img_info
.Width
);
1427 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1428 format
= img_info
.Format
;
1430 if (size
== D3DX_FROM_FILE
)
1433 size
= img_info
.Width
;
1436 if (format
== D3DFMT_FROM_FILE
)
1439 format
= img_info
.Format
;
1442 if (mip_levels
== D3DX_FROM_FILE
)
1444 file_mip_levels
= TRUE
;
1445 mip_levels
= img_info
.MipLevels
;
1448 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &mip_levels
, usage
, &format
, pool
);
1452 if ((file_size
&& size
!= img_info
.Width
)
1453 || (file_format
&& format
!= img_info
.Format
)
1454 || (file_mip_levels
&& mip_levels
!= img_info
.MipLevels
))
1455 return D3DERR_NOTAVAILABLE
;
1457 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1459 return D3DERR_INVALIDCALL
;
1461 if (mip_levels
> img_info
.MipLevels
&& (D3DFMT_DXT1
<= img_info
.Format
&& img_info
.Format
<= D3DFMT_DXT5
))
1463 FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
1464 mip_levels
= img_info
.MipLevels
;
1467 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1468 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1470 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1475 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1481 hr
= load_cube_texture_from_dds(tex
, src_data
, palette
, filter
, color_key
, &img_info
);
1484 IDirect3DCubeTexture9_Release(tex
);
1488 loaded_miplevels
= min(IDirect3DCubeTexture9_GetLevelCount(tex
), img_info
.MipLevels
);
1489 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) tex
, palette
, loaded_miplevels
- 1, mip_filter
);
1492 IDirect3DCubeTexture9_Release(tex
);
1498 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1501 IDirect3DCubeTexture9_Release(buftex
);
1505 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1506 IDirect3DCubeTexture9_Release(buftex
);
1510 *src_info
= img_info
;
1512 *cube_texture
= tex
;
1517 HRESULT WINAPI
D3DXCreateCubeTextureFromFileA(IDirect3DDevice9
*device
,
1518 const char *src_filename
,
1519 IDirect3DCubeTexture9
**cube_texture
)
1527 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_a(src_filename
), cube_texture
);
1529 if (!src_filename
) return D3DERR_INVALIDCALL
;
1531 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1532 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1533 if (!filename
) return E_OUTOFMEMORY
;
1534 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1536 hr
= map_view_of_file(filename
, &data
, &data_size
);
1539 HeapFree(GetProcessHeap(), 0, filename
);
1540 return D3DXERR_INVALIDDATA
;
1543 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1544 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1546 UnmapViewOfFile(data
);
1547 HeapFree(GetProcessHeap(), 0, filename
);
1551 HRESULT WINAPI
D3DXCreateCubeTextureFromFileW(IDirect3DDevice9
*device
,
1552 const WCHAR
*src_filename
,
1553 IDirect3DCubeTexture9
**cube_texture
)
1559 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_w(src_filename
), cube_texture
);
1561 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1562 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1564 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1565 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1567 UnmapViewOfFile(data
);
1571 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9
*device
,
1572 const char *src_filename
,
1581 D3DXIMAGE_INFO
*image_info
,
1582 PALETTEENTRY
*palette
,
1583 IDirect3DCubeTexture9
**cube_texture
)
1591 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1592 device
, wine_dbgstr_a(src_filename
), size
, mip_levels
, usage
, format
,
1593 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1595 if (!src_filename
) return D3DERR_INVALIDCALL
;
1597 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1598 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1599 if (!filename
) return E_OUTOFMEMORY
;
1600 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1602 hr
= map_view_of_file(filename
, &data
, &data_size
);
1605 HeapFree(GetProcessHeap(), 0, filename
);
1606 return D3DXERR_INVALIDDATA
;
1609 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1610 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1612 UnmapViewOfFile(data
);
1613 HeapFree(GetProcessHeap(), 0, filename
);
1617 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9
*device
,
1618 const WCHAR
*src_filename
,
1627 D3DXIMAGE_INFO
*image_info
,
1628 PALETTEENTRY
*palette
,
1629 IDirect3DCubeTexture9
**cube_texture
)
1635 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1636 device
, wine_dbgstr_w(src_filename
), size
, mip_levels
, usage
, format
,
1637 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1639 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1640 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1642 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1643 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1645 UnmapViewOfFile(data
);
1659 static float get_cube_coord(enum cube_coord coord
, unsigned int x
, unsigned int y
, unsigned int size
)
1666 return size
- x
- 0.5f
;
1670 return size
- y
- 0.5f
;
1676 ERR("Unexpected coordinate value\n");
1681 HRESULT WINAPI
D3DXFillCubeTexture(struct IDirect3DCubeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1685 D3DSURFACE_DESC desc
;
1686 D3DLOCKED_RECT lock_rect
;
1688 D3DXVECTOR3 coord
, size
;
1689 const struct pixel_format_desc
*format
;
1691 static const enum cube_coord coordmap
[6][3] =
1693 {ONE
, YCOORDINV
, XCOORDINV
},
1694 {ZERO
, YCOORDINV
, XCOORD
},
1695 {XCOORD
, ONE
, YCOORD
},
1696 {XCOORD
, ZERO
, YCOORDINV
},
1697 {XCOORD
, YCOORDINV
, ONE
},
1698 {XCOORDINV
, YCOORDINV
, ZERO
}
1701 if (texture
== NULL
|| function
== NULL
)
1702 return D3DERR_INVALIDCALL
;
1704 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1706 for (m
= 0; m
< miplevels
; m
++)
1708 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1709 return D3DERR_INVALIDCALL
;
1711 format
= get_format_info(desc
.Format
);
1712 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1714 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1715 return D3DERR_INVALIDCALL
;
1718 for (f
= 0; f
< 6; f
++)
1720 if (FAILED(IDirect3DCubeTexture9_LockRect(texture
, f
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1721 return D3DERR_INVALIDCALL
;
1723 size
.x
= (f
== 0) || (f
== 1) ? 0.0f
: 2.0f
/ desc
.Width
;
1724 size
.y
= (f
== 2) || (f
== 3) ? 0.0f
: 2.0f
/ desc
.Width
;
1725 size
.z
= (f
== 4) || (f
== 5) ? 0.0f
: 2.0f
/ desc
.Width
;
1727 data
= lock_rect
.pBits
;
1729 for (y
= 0; y
< desc
.Height
; y
++)
1731 for (x
= 0; x
< desc
.Width
; x
++)
1733 coord
.x
= get_cube_coord(coordmap
[f
][0], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1734 coord
.y
= get_cube_coord(coordmap
[f
][1], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1735 coord
.z
= get_cube_coord(coordmap
[f
][2], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1737 function(&value
, &coord
, &size
, funcdata
);
1739 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1742 IDirect3DCubeTexture9_UnlockRect(texture
, f
, m
);
1749 HRESULT WINAPI
D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1753 D3DVOLUME_DESC desc
;
1754 D3DLOCKED_BOX lock_box
;
1756 D3DXVECTOR3 coord
, size
;
1757 const struct pixel_format_desc
*format
;
1760 if (texture
== NULL
|| function
== NULL
)
1761 return D3DERR_INVALIDCALL
;
1763 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1765 for (m
= 0; m
< miplevels
; m
++)
1767 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1768 return D3DERR_INVALIDCALL
;
1770 format
= get_format_info(desc
.Format
);
1771 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1773 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1774 return D3DERR_INVALIDCALL
;
1777 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture
, m
, &lock_box
, NULL
, D3DLOCK_DISCARD
)))
1778 return D3DERR_INVALIDCALL
;
1780 size
.x
= 1.0f
/ desc
.Width
;
1781 size
.y
= 1.0f
/ desc
.Height
;
1782 size
.z
= 1.0f
/ desc
.Depth
;
1784 data
= lock_box
.pBits
;
1786 for (z
= 0; z
< desc
.Depth
; z
++)
1788 /* The callback function expects the coordinates of the center
1790 coord
.z
= (z
+ 0.5f
) / desc
.Depth
;
1792 for (y
= 0; y
< desc
.Height
; y
++)
1794 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1796 for (x
= 0; x
< desc
.Width
; x
++)
1798 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1800 function(&value
, &coord
, &size
, funcdata
);
1802 fill_texture(format
, data
+ z
* lock_box
.SlicePitch
+ y
* lock_box
.RowPitch
1803 + x
* format
->bytes_per_pixel
, &value
);
1807 IDirect3DVolumeTexture9_UnlockBox(texture
, m
);
1813 HRESULT WINAPI
D3DXSaveTextureToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1814 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1819 ID3DXBuffer
*buffer
;
1821 TRACE("(%s, %#x, %p, %p): relay\n",
1822 wine_dbgstr_a(dst_filename
), file_format
, src_texture
, src_palette
);
1824 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1826 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1827 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1828 if (!filename
) return E_OUTOFMEMORY
;
1829 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1831 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1834 hr
= write_buffer_to_file(filename
, buffer
);
1835 ID3DXBuffer_Release(buffer
);
1838 HeapFree(GetProcessHeap(), 0, filename
);
1842 HRESULT WINAPI
D3DXSaveTextureToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1843 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1846 ID3DXBuffer
*buffer
;
1848 TRACE("(%s, %#x, %p, %p): relay\n",
1849 wine_dbgstr_w(dst_filename
), file_format
, src_texture
, src_palette
);
1851 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1853 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1856 hr
= write_buffer_to_file(dst_filename
, buffer
);
1857 ID3DXBuffer_Release(buffer
);
1863 HRESULT WINAPI
D3DXSaveTextureToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1864 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1867 D3DRESOURCETYPE type
;
1868 IDirect3DSurface9
*surface
;
1870 TRACE("(%p, %#x, %p, %p)\n",
1871 dst_buffer
, file_format
, src_texture
, src_palette
);
1873 if (!dst_buffer
|| !src_texture
) return D3DERR_INVALIDCALL
;
1875 if (file_format
== D3DXIFF_DDS
)
1877 FIXME("DDS file format isn't supported yet\n");
1881 type
= IDirect3DBaseTexture9_GetType(src_texture
);
1884 case D3DRTYPE_TEXTURE
:
1885 case D3DRTYPE_CUBETEXTURE
:
1886 hr
= get_surface(type
, src_texture
, D3DCUBEMAP_FACE_POSITIVE_X
, 0, &surface
);
1888 case D3DRTYPE_VOLUMETEXTURE
:
1889 FIXME("Volume textures aren't supported yet\n");
1892 return D3DERR_INVALIDCALL
;
1897 hr
= D3DXSaveSurfaceToFileInMemory(dst_buffer
, file_format
, surface
, src_palette
, NULL
);
1898 IDirect3DSurface9_Release(surface
);