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
= 16 - 4 * (curchannels
- channels
);
307 for (j
= 0; j
< 4; j
++)
309 int diff
= curfmt
->bits
[j
] - fmt
->bits
[j
];
310 score
+= 16 - (diff
< 0 ? -diff
* 4 : diff
);
313 if (score
> bestscore
)
316 usedformat
= curfmt
->format
;
324 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
325 return D3DERR_INVALIDCALL
;
327 if ((w
== D3DX_DEFAULT
) && (h
== D3DX_DEFAULT
))
329 else if (w
== D3DX_DEFAULT
)
330 w
= (height
? h
: 256);
331 else if (h
== D3DX_DEFAULT
)
332 h
= (width
? w
: 256);
334 if (fmt
->block_width
!= 1 || fmt
->block_height
!= 1)
336 if (w
< fmt
->block_width
)
337 w
= fmt
->block_width
;
338 if (h
< fmt
->block_height
)
339 h
= fmt
->block_height
;
342 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(w
)))
345 if (w
> caps
.MaxTextureWidth
)
346 w
= caps
.MaxTextureWidth
;
348 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(h
)))
351 if (h
> caps
.MaxTextureHeight
)
352 h
= caps
.MaxTextureHeight
;
354 if (caps
.TextureCaps
& D3DPTEXTURECAPS_SQUAREONLY
)
368 if (miplevels
&& (usage
& D3DUSAGE_AUTOGENMIPMAP
))
375 UINT max_mipmaps
= 1;
377 if (!width
&& !height
)
378 max_mipmaps
= 9; /* number of mipmaps in a 256x256 texture */
381 UINT max_dimen
= max(w
, h
);
383 while (max_dimen
> 1)
390 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
391 *miplevels
= max_mipmaps
;
397 IDirect3D9_Release(d3d
);
402 if (usedformat
== D3DFMT_UNKNOWN
)
404 WARN("Couldn't find a suitable pixel format\n");
405 return D3DERR_NOTAVAILABLE
;
408 TRACE("Format chosen: %x\n", usedformat
);
410 *format
= usedformat
;
415 HRESULT WINAPI
D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*size
,
416 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
419 UINT s
= (size
&& *size
) ? *size
: 256;
422 TRACE("(%p, %p, %p, %u, %p, %u)\n", device
, size
, miplevels
, usage
, format
, pool
);
424 if (s
== D3DX_DEFAULT
)
427 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
428 return D3DERR_INVALIDCALL
;
430 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP
))
431 return D3DERR_NOTAVAILABLE
;
433 /* ensure width/height is power of 2 */
434 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP_POW2
) && (!is_pow2(s
)))
437 hr
= D3DXCheckTextureRequirements(device
, &s
, &s
, miplevels
, usage
, format
, pool
);
439 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPCUBEMAP
))
451 HRESULT WINAPI
D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
452 UINT
*depth
, UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
455 UINT w
= width
? *width
: D3DX_DEFAULT
;
456 UINT h
= height
? *height
: D3DX_DEFAULT
;
457 UINT d
= (depth
&& *depth
) ? *depth
: 1;
460 TRACE("(%p, %p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, depth
, miplevels
,
461 usage
, format
, pool
);
463 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
464 return D3DERR_INVALIDCALL
;
466 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP
))
467 return D3DERR_NOTAVAILABLE
;
469 hr
= D3DXCheckTextureRequirements(device
, &w
, &h
, NULL
, usage
, format
, pool
);
470 if (d
== D3DX_DEFAULT
)
473 /* ensure width/height is power of 2 */
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 HRESULT WINAPI
D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9
*device
, const void *srcdata
,
542 UINT srcdatasize
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
543 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
544 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
546 IDirect3DTexture9
**texptr
;
547 IDirect3DTexture9
*buftex
;
548 IDirect3DSurface9
*surface
;
549 BOOL dynamic_texture
;
550 D3DXIMAGE_INFO imginfo
;
551 UINT loaded_miplevels
, skip_levels
;
555 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u,"
556 " usage %#x, format %#x, pool %#x, filter %#x, mipfilter %#x, colorkey %#x,"
557 " srcinfo %p, palette %p, texture %p.\n",
558 device
, srcdata
, srcdatasize
, width
, height
, miplevels
, usage
, format
, pool
,
559 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
561 /* check for invalid parameters */
562 if (!device
|| !texture
|| !srcdata
|| !srcdatasize
)
563 return D3DERR_INVALIDCALL
;
565 hr
= D3DXGetImageInfoFromFileInMemory(srcdata
, srcdatasize
, &imginfo
);
568 FIXME("Unrecognized file format, returning failure.\n");
573 /* handle default values */
574 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
575 width
= imginfo
.Width
;
577 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
578 height
= imginfo
.Height
;
580 if (width
== D3DX_DEFAULT
)
581 width
= make_pow2(imginfo
.Width
);
583 if (height
== D3DX_DEFAULT
)
584 height
= make_pow2(imginfo
.Height
);
586 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
587 format
= imginfo
.Format
;
589 if (width
== D3DX_FROM_FILE
)
591 width
= imginfo
.Width
;
594 if (height
== D3DX_FROM_FILE
)
596 height
= imginfo
.Height
;
599 if (format
== D3DFMT_FROM_FILE
)
601 format
= imginfo
.Format
;
604 if (miplevels
== D3DX_FROM_FILE
)
606 miplevels
= imginfo
.MipLevels
;
609 skip_levels
= mipfilter
!= D3DX_DEFAULT
? mipfilter
>> D3DX_SKIP_DDS_MIP_LEVELS_SHIFT
: 0;
610 if (skip_levels
&& imginfo
.MipLevels
> skip_levels
)
612 TRACE("Skipping the first %u (of %u) levels of a DDS mipmapped texture.\n",
613 skip_levels
, imginfo
.MipLevels
);
614 TRACE("Texture level 0 dimensions are %ux%u.\n", imginfo
.Width
, imginfo
.Height
);
615 width
>>= skip_levels
;
616 height
>>= skip_levels
;
617 miplevels
-= skip_levels
;
624 /* fix texture creation parameters */
625 hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
);
628 FIXME("Couldn't find suitable texture parameters.\n");
633 if (imginfo
.MipLevels
< miplevels
&& (D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
))
635 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet\n");
636 miplevels
= imginfo
.MipLevels
;
638 if (imginfo
.ResourceType
== D3DRTYPE_VOLUMETEXTURE
639 && D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
&& miplevels
> 1)
641 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet.\n");
645 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
646 return D3DERR_INVALIDCALL
;
648 /* Create the to-be-filled texture */
649 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
650 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
652 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
657 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
663 FIXME("Texture creation failed.\n");
668 TRACE("Texture created correctly. Now loading the texture data into it.\n");
669 if (imginfo
.ImageFileFormat
!= D3DXIFF_DDS
)
671 IDirect3DTexture9_GetSurfaceLevel(*texptr
, 0, &surface
);
672 hr
= D3DXLoadSurfaceFromFileInMemory(surface
, palette
, NULL
, srcdata
, srcdatasize
, NULL
, filter
, colorkey
, NULL
);
673 IDirect3DSurface9_Release(surface
);
674 loaded_miplevels
= min(IDirect3DTexture9_GetLevelCount(*texptr
), imginfo
.MipLevels
);
678 hr
= load_texture_from_dds(*texptr
, srcdata
, palette
, filter
, colorkey
, &imginfo
, skip_levels
,
684 FIXME("Texture loading failed.\n");
685 IDirect3DTexture9_Release(*texptr
);
690 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) *texptr
, palette
, loaded_miplevels
- 1, mipfilter
);
693 FIXME("Texture filtering failed.\n");
694 IDirect3DTexture9_Release(*texptr
);
699 /* Move the data to the actual texture if necessary */
700 if (texptr
== &buftex
)
702 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
706 IDirect3DTexture9_Release(buftex
);
711 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)(*texture
));
712 IDirect3DTexture9_Release(buftex
);
721 HRESULT WINAPI
D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
722 const void *srcdata
, UINT srcdatasize
, struct IDirect3DTexture9
**texture
)
724 TRACE("(%p, %p, %d, %p)\n", device
, srcdata
, srcdatasize
, texture
);
726 return D3DXCreateTextureFromFileInMemoryEx(device
, srcdata
, srcdatasize
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
727 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
730 HRESULT WINAPI
D3DXCreateTextureFromFileExW(struct IDirect3DDevice9
*device
, const WCHAR
*srcfile
,
731 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
732 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
733 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
739 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
740 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
741 device
, debugstr_w(srcfile
), width
, height
, miplevels
, usage
, format
,
742 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
745 return D3DERR_INVALIDCALL
;
747 hr
= map_view_of_file(srcfile
, &buffer
, &size
);
749 return D3DXERR_INVALIDDATA
;
751 hr
= D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
, usage
, format
, pool
,
752 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
754 UnmapViewOfFile(buffer
);
759 HRESULT WINAPI
D3DXCreateTextureFromFileExA(struct IDirect3DDevice9
*device
, const char *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_a(srcfile
), width
, height
, miplevels
, usage
, format
,
771 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
773 if (!device
|| !srcfile
|| !texture
)
774 return D3DERR_INVALIDCALL
;
776 len
= MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, NULL
, 0);
777 widename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*widename
));
778 MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, widename
, len
);
780 hr
= D3DXCreateTextureFromFileExW(device
, widename
, width
, height
, miplevels
,
781 usage
, format
, pool
, filter
, mipfilter
,
782 colorkey
, srcinfo
, palette
, texture
);
784 HeapFree(GetProcessHeap(), 0, widename
);
788 HRESULT WINAPI
D3DXCreateTextureFromFileA(struct IDirect3DDevice9
*device
,
789 const char *srcfile
, struct IDirect3DTexture9
**texture
)
791 TRACE("(%p, %s, %p)\n", device
, debugstr_a(srcfile
), texture
);
793 return D3DXCreateTextureFromFileExA(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
794 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
797 HRESULT WINAPI
D3DXCreateTextureFromFileW(struct IDirect3DDevice9
*device
,
798 const WCHAR
*srcfile
, struct IDirect3DTexture9
**texture
)
800 TRACE("(%p, %s, %p)\n", device
, debugstr_w(srcfile
), texture
);
802 return D3DXCreateTextureFromFileExW(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
803 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
807 HRESULT WINAPI
D3DXCreateTextureFromResourceA(struct IDirect3DDevice9
*device
,
808 HMODULE srcmodule
, const char *resource
, struct IDirect3DTexture9
**texture
)
810 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_a(resource
));
812 return D3DXCreateTextureFromResourceExA(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
813 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
816 HRESULT WINAPI
D3DXCreateTextureFromResourceW(struct IDirect3DDevice9
*device
,
817 HMODULE srcmodule
, const WCHAR
*resource
, struct IDirect3DTexture9
**texture
)
819 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_w(resource
));
821 return D3DXCreateTextureFromResourceExW(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
822 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
825 HRESULT WINAPI
D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
826 const char *resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
827 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
828 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
834 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
835 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
836 device
, srcmodule
, debugstr_a(resource
), width
, height
, miplevels
, usage
, format
,
837 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
839 if (!device
|| !texture
)
840 return D3DERR_INVALIDCALL
;
842 if (!(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_RCDATA
))
843 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
844 && !(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_BITMAP
)))
845 return D3DXERR_INVALIDDATA
;
847 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
848 return D3DXERR_INVALIDDATA
;
850 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
851 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
854 HRESULT WINAPI
D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
855 const WCHAR
*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_w(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
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_RCDATA
))
872 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
873 && !(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)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
D3DXCreateCubeTexture(struct IDirect3DDevice9
*device
, UINT size
, UINT miplevels
,
884 DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DCubeTexture9
**texture
)
888 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device
, size
, miplevels
, usage
, format
,
891 if (!device
|| !texture
)
892 return D3DERR_INVALIDCALL
;
894 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &miplevels
, usage
, &format
, pool
);
898 TRACE("D3DXCheckCubeTextureRequirements failed\n");
902 return IDirect3DDevice9_CreateCubeTexture(device
, size
, miplevels
, usage
, format
, pool
, texture
, NULL
);
905 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
906 const void *data
, UINT datasize
, struct IDirect3DCubeTexture9
**texture
)
908 TRACE("(%p, %p, %u, %p)\n", device
, data
, datasize
, texture
);
910 return D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, datasize
, D3DX_DEFAULT
, D3DX_DEFAULT
,
911 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
914 HRESULT WINAPI
D3DXCreateVolumeTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
, UINT depth
,
915 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DVolumeTexture9
**texture
)
919 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device
, width
, height
, depth
,
920 miplevels
, usage
, format
, pool
, texture
);
922 if (!device
|| !texture
)
923 return D3DERR_INVALIDCALL
;
925 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
,
926 &miplevels
, usage
, &format
, pool
);
930 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
934 return IDirect3DDevice9_CreateVolumeTexture(device
, width
, height
, depth
, miplevels
,
935 usage
, format
, pool
, texture
, NULL
);
938 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9
*device
,
939 const char *filename
,
940 IDirect3DVolumeTexture9
**volume_texture
)
948 TRACE("(%p, %s, %p): relay\n",
949 device
, debugstr_a(filename
), volume_texture
);
951 if (!filename
) return D3DERR_INVALIDCALL
;
953 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
954 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
955 if (!filenameW
) return E_OUTOFMEMORY
;
956 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
958 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
959 HeapFree(GetProcessHeap(), 0, filenameW
);
960 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
962 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
963 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
965 UnmapViewOfFile(data
);
969 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9
*device
,
970 const WCHAR
*filename
,
971 IDirect3DVolumeTexture9
**volume_texture
)
977 TRACE("(%p, %s, %p): relay\n",
978 device
, debugstr_w(filename
), volume_texture
);
980 if (!filename
) return D3DERR_INVALIDCALL
;
982 hr
= map_view_of_file(filename
, &data
, &data_size
);
983 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
985 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
986 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
988 UnmapViewOfFile(data
);
992 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9
*device
,
993 const char *filename
,
1004 D3DXIMAGE_INFO
*src_info
,
1005 PALETTEENTRY
*palette
,
1006 IDirect3DVolumeTexture9
**volume_texture
)
1014 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1015 device
, debugstr_a(filename
), width
, height
, depth
, mip_levels
,
1016 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1017 palette
, volume_texture
);
1019 if (!filename
) return D3DERR_INVALIDCALL
;
1021 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
1022 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1023 if (!filenameW
) return E_OUTOFMEMORY
;
1024 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
1026 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
1027 HeapFree(GetProcessHeap(), 0, filenameW
);
1028 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1030 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1031 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1034 UnmapViewOfFile(data
);
1038 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9
*device
,
1039 const WCHAR
*filename
,
1050 D3DXIMAGE_INFO
*src_info
,
1051 PALETTEENTRY
*palette
,
1052 IDirect3DVolumeTexture9
**volume_texture
)
1058 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1059 device
, debugstr_w(filename
), width
, height
, depth
, mip_levels
,
1060 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1061 palette
, volume_texture
);
1063 if (!filename
) return D3DERR_INVALIDCALL
;
1065 hr
= map_view_of_file(filename
, &data
, &data_size
);
1066 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1068 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1069 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1072 UnmapViewOfFile(data
);
1076 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9
*device
,
1079 IDirect3DVolumeTexture9
**volume_texture
)
1081 TRACE("(%p, %p, %u, %p): relay\n", device
, data
, data_size
, volume_texture
);
1083 return D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1084 D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1085 0, NULL
, NULL
, volume_texture
);
1088 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1101 D3DXIMAGE_INFO
*info
,
1102 PALETTEENTRY
*palette
,
1103 IDirect3DVolumeTexture9
**volume_texture
)
1107 D3DXIMAGE_INFO image_info
;
1108 BOOL dynamic_texture
;
1109 BOOL file_width
= FALSE
;
1110 BOOL file_height
= FALSE
;
1111 BOOL file_depth
= FALSE
;
1112 BOOL file_format
= FALSE
;
1113 BOOL file_mip_levels
= FALSE
;
1114 IDirect3DVolumeTexture9
*tex
, *buftex
;
1116 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1117 device
, data
, data_size
, width
, height
, depth
, mip_levels
, usage
, format
, pool
,
1118 filter
, mip_filter
, color_key
, info
, palette
, volume_texture
);
1120 if (!device
|| !data
|| !data_size
|| !volume_texture
)
1121 return D3DERR_INVALIDCALL
;
1123 hr
= D3DXGetImageInfoFromFileInMemory(data
, data_size
, &image_info
);
1124 if (FAILED(hr
)) return hr
;
1126 if (image_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1127 return D3DXERR_INVALIDDATA
;
1129 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
1130 width
= image_info
.Width
;
1131 if (width
== D3DX_DEFAULT
)
1132 width
= make_pow2(image_info
.Width
);
1134 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
1135 height
= image_info
.Height
;
1136 if (height
== D3DX_DEFAULT
)
1137 height
= make_pow2(image_info
.Height
);
1139 if (depth
== 0 || depth
== D3DX_DEFAULT_NONPOW2
)
1140 depth
= image_info
.Depth
;
1141 if (depth
== D3DX_DEFAULT
)
1142 depth
= make_pow2(image_info
.Depth
);
1144 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1145 format
= image_info
.Format
;
1147 if (width
== D3DX_FROM_FILE
)
1150 width
= image_info
.Width
;
1153 if (height
== D3DX_FROM_FILE
)
1156 height
= image_info
.Height
;
1159 if (depth
== D3DX_FROM_FILE
)
1162 depth
= image_info
.Depth
;
1165 if (format
== D3DFMT_FROM_FILE
)
1168 format
= image_info
.Format
;
1171 if (mip_levels
== D3DX_FROM_FILE
)
1173 file_mip_levels
= TRUE
;
1174 mip_levels
= image_info
.MipLevels
;
1177 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
, &mip_levels
, usage
, &format
, pool
);
1178 if (FAILED(hr
)) return hr
;
1180 if ((file_width
&& width
!= image_info
.Width
)
1181 || (file_height
&& height
!= image_info
.Height
)
1182 || (file_depth
&& depth
!= image_info
.Depth
)
1183 || (file_format
&& format
!= image_info
.Format
)
1184 || (file_mip_levels
&& mip_levels
!= image_info
.MipLevels
))
1185 return D3DERR_NOTAVAILABLE
;
1187 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1189 return D3DERR_INVALIDCALL
;
1191 if (mip_levels
> image_info
.MipLevels
)
1193 FIXME("Generation of mipmaps for volume textures is not implemented yet\n");
1194 mip_levels
= image_info
.MipLevels
;
1197 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1198 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1200 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1205 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1209 if (FAILED(hr
)) return hr
;
1211 hr
= load_volume_texture_from_dds(tex
, data
, palette
, filter
, color_key
, &image_info
);
1214 IDirect3DVolumeTexture9_Release(tex
);
1220 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1223 IDirect3DVolumeTexture9_Release(buftex
);
1227 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1228 IDirect3DVolumeTexture9_Release(buftex
);
1234 *volume_texture
= tex
;
1238 static inline void fill_texture(const struct pixel_format_desc
*format
, BYTE
*pos
, const D3DXVECTOR4
*value
)
1242 for (c
= 0; c
< format
->bytes_per_pixel
; c
++)
1245 for (c
= 0; c
< 4; c
++)
1248 DWORD i
, v
= 0, mask32
= format
->bits
[c
] == 32 ? ~0U : ((1 << format
->bits
[c
]) - 1);
1253 comp_value
= value
->w
;
1256 comp_value
= value
->x
;
1259 comp_value
= value
->y
;
1262 comp_value
= value
->z
;
1266 if (format
->type
== FORMAT_ARGBF16
)
1267 v
= float_32_to_16(comp_value
);
1268 else if (format
->type
== FORMAT_ARGBF
)
1269 v
= *(DWORD
*)&comp_value
;
1270 else if (format
->type
== FORMAT_ARGB
)
1271 v
= comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
;
1273 FIXME("Unhandled format type %#x\n", format
->type
);
1275 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1279 if (format
->shift
[c
] > i
)
1281 mask
= mask32
<< (format
->shift
[c
] - i
);
1282 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1286 mask
= mask32
>> (i
- format
->shift
[c
]);
1287 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1294 HRESULT WINAPI
D3DXFillTexture(struct IDirect3DTexture9
*texture
, LPD3DXFILL2D function
, void *funcdata
)
1298 D3DSURFACE_DESC desc
;
1299 D3DLOCKED_RECT lock_rect
;
1301 D3DXVECTOR2 coord
, size
;
1302 const struct pixel_format_desc
*format
;
1305 if (texture
== NULL
|| function
== NULL
)
1306 return D3DERR_INVALIDCALL
;
1308 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1310 for (m
= 0; m
< miplevels
; m
++)
1312 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture
, m
, &desc
)))
1313 return D3DERR_INVALIDCALL
;
1315 format
= get_format_info(desc
.Format
);
1316 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1318 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1319 return D3DERR_INVALIDCALL
;
1322 if (FAILED(IDirect3DTexture9_LockRect(texture
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1323 return D3DERR_INVALIDCALL
;
1325 size
.x
= 1.0f
/ desc
.Width
;
1326 size
.y
= 1.0f
/ desc
.Height
;
1328 data
= lock_rect
.pBits
;
1330 for (y
= 0; y
< desc
.Height
; y
++)
1332 /* The callback function expects the coordinates of the center
1334 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1336 for (x
= 0; x
< desc
.Width
; x
++)
1338 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1340 function(&value
, &coord
, &size
, funcdata
);
1342 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1345 IDirect3DTexture9_UnlockRect(texture
, m
);
1351 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1352 const void *src_data
,
1362 D3DXIMAGE_INFO
*src_info
,
1363 PALETTEENTRY
*palette
,
1364 IDirect3DCubeTexture9
**cube_texture
)
1368 UINT loaded_miplevels
;
1369 D3DXIMAGE_INFO img_info
;
1370 BOOL dynamic_texture
;
1371 BOOL file_size
= FALSE
;
1372 BOOL file_format
= FALSE
;
1373 BOOL file_mip_levels
= FALSE
;
1374 IDirect3DCubeTexture9
*tex
, *buftex
;
1376 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device
,
1377 src_data
, src_data_size
, size
, mip_levels
, usage
, format
, pool
, filter
, mip_filter
,
1378 color_key
, src_info
, palette
, cube_texture
);
1380 if (!device
|| !cube_texture
|| !src_data
|| !src_data_size
)
1381 return D3DERR_INVALIDCALL
;
1383 hr
= D3DXGetImageInfoFromFileInMemory(src_data
, src_data_size
, &img_info
);
1387 if (img_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1388 return D3DXERR_INVALIDDATA
;
1390 if (img_info
.Width
!= img_info
.Height
)
1391 return D3DXERR_INVALIDDATA
;
1393 if (size
== 0 || size
== D3DX_DEFAULT_NONPOW2
)
1394 size
= img_info
.Width
;
1395 if (size
== D3DX_DEFAULT
)
1396 size
= make_pow2(img_info
.Width
);
1398 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1399 format
= img_info
.Format
;
1401 if (size
== D3DX_FROM_FILE
)
1404 size
= img_info
.Width
;
1407 if (format
== D3DFMT_FROM_FILE
)
1410 format
= img_info
.Format
;
1413 if (mip_levels
== D3DX_FROM_FILE
)
1415 file_mip_levels
= TRUE
;
1416 mip_levels
= img_info
.MipLevels
;
1419 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &mip_levels
, usage
, &format
, pool
);
1423 if ((file_size
&& size
!= img_info
.Width
)
1424 || (file_format
&& format
!= img_info
.Format
)
1425 || (file_mip_levels
&& mip_levels
!= img_info
.MipLevels
))
1426 return D3DERR_NOTAVAILABLE
;
1428 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1430 return D3DERR_INVALIDCALL
;
1432 if (mip_levels
> img_info
.MipLevels
&& (D3DFMT_DXT1
<= img_info
.Format
&& img_info
.Format
<= D3DFMT_DXT5
))
1434 FIXME("Generation of mipmaps for compressed pixel formats not supported yet\n");
1435 mip_levels
= img_info
.MipLevels
;
1438 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1439 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1441 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1446 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1452 hr
= load_cube_texture_from_dds(tex
, src_data
, palette
, filter
, color_key
, &img_info
);
1455 IDirect3DCubeTexture9_Release(tex
);
1459 loaded_miplevels
= min(IDirect3DCubeTexture9_GetLevelCount(tex
), img_info
.MipLevels
);
1460 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) tex
, palette
, loaded_miplevels
- 1, mip_filter
);
1463 IDirect3DCubeTexture9_Release(tex
);
1469 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1472 IDirect3DCubeTexture9_Release(buftex
);
1476 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1477 IDirect3DCubeTexture9_Release(buftex
);
1481 *src_info
= img_info
;
1483 *cube_texture
= tex
;
1488 HRESULT WINAPI
D3DXCreateCubeTextureFromFileA(IDirect3DDevice9
*device
,
1489 const char *src_filename
,
1490 IDirect3DCubeTexture9
**cube_texture
)
1498 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_a(src_filename
), cube_texture
);
1500 if (!src_filename
) return D3DERR_INVALIDCALL
;
1502 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1503 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1504 if (!filename
) return E_OUTOFMEMORY
;
1505 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1507 hr
= map_view_of_file(filename
, &data
, &data_size
);
1510 HeapFree(GetProcessHeap(), 0, filename
);
1511 return D3DXERR_INVALIDDATA
;
1514 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1515 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1517 UnmapViewOfFile(data
);
1518 HeapFree(GetProcessHeap(), 0, filename
);
1522 HRESULT WINAPI
D3DXCreateCubeTextureFromFileW(IDirect3DDevice9
*device
,
1523 const WCHAR
*src_filename
,
1524 IDirect3DCubeTexture9
**cube_texture
)
1530 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_w(src_filename
), cube_texture
);
1532 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1533 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1535 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1536 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1538 UnmapViewOfFile(data
);
1542 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9
*device
,
1543 const char *src_filename
,
1552 D3DXIMAGE_INFO
*image_info
,
1553 PALETTEENTRY
*palette
,
1554 IDirect3DCubeTexture9
**cube_texture
)
1562 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1563 device
, wine_dbgstr_a(src_filename
), size
, mip_levels
, usage
, format
,
1564 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1566 if (!src_filename
) return D3DERR_INVALIDCALL
;
1568 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1569 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1570 if (!filename
) return E_OUTOFMEMORY
;
1571 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1573 hr
= map_view_of_file(filename
, &data
, &data_size
);
1576 HeapFree(GetProcessHeap(), 0, filename
);
1577 return D3DXERR_INVALIDDATA
;
1580 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1581 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1583 UnmapViewOfFile(data
);
1584 HeapFree(GetProcessHeap(), 0, filename
);
1588 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9
*device
,
1589 const WCHAR
*src_filename
,
1598 D3DXIMAGE_INFO
*image_info
,
1599 PALETTEENTRY
*palette
,
1600 IDirect3DCubeTexture9
**cube_texture
)
1606 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1607 device
, wine_dbgstr_w(src_filename
), size
, mip_levels
, usage
, format
,
1608 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1610 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1611 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1613 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1614 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1616 UnmapViewOfFile(data
);
1630 static float get_cube_coord(enum cube_coord coord
, unsigned int x
, unsigned int y
, unsigned int size
)
1637 return size
- x
- 0.5f
;
1641 return size
- y
- 0.5f
;
1647 ERR("Unexpected coordinate value\n");
1652 HRESULT WINAPI
D3DXFillCubeTexture(struct IDirect3DCubeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1656 D3DSURFACE_DESC desc
;
1657 D3DLOCKED_RECT lock_rect
;
1659 D3DXVECTOR3 coord
, size
;
1660 const struct pixel_format_desc
*format
;
1662 static const enum cube_coord coordmap
[6][3] =
1664 {ONE
, YCOORDINV
, XCOORDINV
},
1665 {ZERO
, YCOORDINV
, XCOORD
},
1666 {XCOORD
, ONE
, YCOORD
},
1667 {XCOORD
, ZERO
, YCOORDINV
},
1668 {XCOORD
, YCOORDINV
, ONE
},
1669 {XCOORDINV
, YCOORDINV
, ZERO
}
1672 if (texture
== NULL
|| function
== NULL
)
1673 return D3DERR_INVALIDCALL
;
1675 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1677 for (m
= 0; m
< miplevels
; m
++)
1679 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1680 return D3DERR_INVALIDCALL
;
1682 format
= get_format_info(desc
.Format
);
1683 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1685 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1686 return D3DERR_INVALIDCALL
;
1689 for (f
= 0; f
< 6; f
++)
1691 if (FAILED(IDirect3DCubeTexture9_LockRect(texture
, f
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1692 return D3DERR_INVALIDCALL
;
1694 size
.x
= (f
== 0) || (f
== 1) ? 0.0f
: 2.0f
/ desc
.Width
;
1695 size
.y
= (f
== 2) || (f
== 3) ? 0.0f
: 2.0f
/ desc
.Width
;
1696 size
.z
= (f
== 4) || (f
== 5) ? 0.0f
: 2.0f
/ desc
.Width
;
1698 data
= lock_rect
.pBits
;
1700 for (y
= 0; y
< desc
.Height
; y
++)
1702 for (x
= 0; x
< desc
.Width
; x
++)
1704 coord
.x
= get_cube_coord(coordmap
[f
][0], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1705 coord
.y
= get_cube_coord(coordmap
[f
][1], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1706 coord
.z
= get_cube_coord(coordmap
[f
][2], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1708 function(&value
, &coord
, &size
, funcdata
);
1710 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1713 IDirect3DCubeTexture9_UnlockRect(texture
, f
, m
);
1720 HRESULT WINAPI
D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1724 D3DVOLUME_DESC desc
;
1725 D3DLOCKED_BOX lock_box
;
1727 D3DXVECTOR3 coord
, size
;
1728 const struct pixel_format_desc
*format
;
1731 if (texture
== NULL
|| function
== NULL
)
1732 return D3DERR_INVALIDCALL
;
1734 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1736 for (m
= 0; m
< miplevels
; m
++)
1738 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1739 return D3DERR_INVALIDCALL
;
1741 format
= get_format_info(desc
.Format
);
1742 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1744 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1745 return D3DERR_INVALIDCALL
;
1748 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture
, m
, &lock_box
, NULL
, D3DLOCK_DISCARD
)))
1749 return D3DERR_INVALIDCALL
;
1751 size
.x
= 1.0f
/ desc
.Width
;
1752 size
.y
= 1.0f
/ desc
.Height
;
1753 size
.z
= 1.0f
/ desc
.Depth
;
1755 data
= lock_box
.pBits
;
1757 for (z
= 0; z
< desc
.Depth
; z
++)
1759 /* The callback function expects the coordinates of the center
1761 coord
.z
= (z
+ 0.5f
) / desc
.Depth
;
1763 for (y
= 0; y
< desc
.Height
; y
++)
1765 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1767 for (x
= 0; x
< desc
.Width
; x
++)
1769 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1771 function(&value
, &coord
, &size
, funcdata
);
1773 fill_texture(format
, data
+ z
* lock_box
.SlicePitch
+ y
* lock_box
.RowPitch
1774 + x
* format
->bytes_per_pixel
, &value
);
1778 IDirect3DVolumeTexture9_UnlockBox(texture
, m
);
1784 HRESULT WINAPI
D3DXSaveTextureToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1785 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1790 ID3DXBuffer
*buffer
;
1792 TRACE("(%s, %#x, %p, %p): relay\n",
1793 wine_dbgstr_a(dst_filename
), file_format
, src_texture
, src_palette
);
1795 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1797 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1798 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1799 if (!filename
) return E_OUTOFMEMORY
;
1800 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1802 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1805 hr
= write_buffer_to_file(filename
, buffer
);
1806 ID3DXBuffer_Release(buffer
);
1809 HeapFree(GetProcessHeap(), 0, filename
);
1813 HRESULT WINAPI
D3DXSaveTextureToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1814 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1817 ID3DXBuffer
*buffer
;
1819 TRACE("(%s, %#x, %p, %p): relay\n",
1820 wine_dbgstr_w(dst_filename
), file_format
, src_texture
, src_palette
);
1822 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1824 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1827 hr
= write_buffer_to_file(dst_filename
, buffer
);
1828 ID3DXBuffer_Release(buffer
);
1834 HRESULT WINAPI
D3DXSaveTextureToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1835 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1838 D3DRESOURCETYPE type
;
1839 IDirect3DSurface9
*surface
;
1841 TRACE("(%p, %#x, %p, %p)\n",
1842 dst_buffer
, file_format
, src_texture
, src_palette
);
1844 if (!dst_buffer
|| !src_texture
) return D3DERR_INVALIDCALL
;
1846 if (file_format
== D3DXIFF_DDS
)
1848 FIXME("DDS file format isn't supported yet\n");
1852 type
= IDirect3DBaseTexture9_GetType(src_texture
);
1855 case D3DRTYPE_TEXTURE
:
1856 case D3DRTYPE_CUBETEXTURE
:
1857 hr
= get_surface(type
, src_texture
, D3DCUBEMAP_FACE_POSITIVE_X
, 0, &surface
);
1859 case D3DRTYPE_VOLUMETEXTURE
:
1860 FIXME("Volume textures aren't supported yet\n");
1863 return D3DERR_INVALIDCALL
;
1868 hr
= D3DXSaveSurfaceToFileInMemory(dst_buffer
, file_format
, surface
, src_palette
, NULL
);
1869 IDirect3DSurface9_Release(surface
);