2 * Copyright 2009 Tony Wasserka
3 * Copyright 2010 Christian Costa
4 * Copyright 2010 Owen Rudge for CodeWeavers
5 * Copyright 2010 Matteo Bruni for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
25 #include "d3dx9_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
29 /* Returns TRUE if num is a power of 2, FALSE if not, or if 0 */
30 static BOOL
is_pow2(UINT num
)
32 return !(num
& (num
- 1));
35 /* Returns the smallest power of 2 which is greater than or equal to num */
36 static UINT
make_pow2(UINT num
)
40 /* In the unlikely event somebody passes a large value, make sure we don't enter an infinite loop */
41 if (num
>= 0x80000000)
50 static HRESULT
get_surface(D3DRESOURCETYPE type
, struct IDirect3DBaseTexture9
*tex
,
51 int face
, UINT level
, struct IDirect3DSurface9
**surf
)
55 case D3DRTYPE_TEXTURE
:
56 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9
*) tex
, level
, surf
);
57 case D3DRTYPE_CUBETEXTURE
:
58 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9
*) tex
, face
, level
, surf
);
60 ERR("Unexpected texture type\n");
65 HRESULT WINAPI
D3DXFilterTexture(IDirect3DBaseTexture9
*texture
,
66 const PALETTEENTRY
*palette
,
74 TRACE("(%p, %p, %u, %#x)\n", texture
, palette
, srclevel
, filter
);
77 return D3DERR_INVALIDCALL
;
79 if ((filter
& 0xFFFF) > D3DX_FILTER_BOX
&& filter
!= D3DX_DEFAULT
)
80 return D3DERR_INVALIDCALL
;
82 if (srclevel
== D3DX_DEFAULT
)
84 else if (srclevel
>= IDirect3DBaseTexture9_GetLevelCount(texture
))
85 return D3DERR_INVALIDCALL
;
87 switch (type
= IDirect3DBaseTexture9_GetType(texture
))
89 case D3DRTYPE_TEXTURE
:
90 case D3DRTYPE_CUBETEXTURE
:
92 IDirect3DSurface9
*topsurf
, *mipsurf
;
96 if (type
== D3DRTYPE_TEXTURE
)
99 IDirect3DTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
104 IDirect3DCubeTexture9_GetLevelDesc((IDirect3DTexture9
*) texture
, srclevel
, &desc
);
107 if (filter
== D3DX_DEFAULT
)
109 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
))
110 filter
= D3DX_FILTER_BOX
;
112 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
115 for (i
= 0; i
< numfaces
; i
++)
117 level
= srclevel
+ 1;
118 hr
= get_surface(type
, texture
, i
, srclevel
, &topsurf
);
121 return D3DERR_INVALIDCALL
;
123 while (get_surface(type
, texture
, i
, level
, &mipsurf
) == D3D_OK
)
125 hr
= D3DXLoadSurfaceFromSurface(mipsurf
, palette
, NULL
, topsurf
, palette
, NULL
, filter
, 0);
126 IDirect3DSurface9_Release(topsurf
);
135 IDirect3DSurface9_Release(topsurf
);
143 case D3DRTYPE_VOLUMETEXTURE
:
146 int level
, level_count
;
147 IDirect3DVolume9
*top_volume
, *mip_volume
;
148 IDirect3DVolumeTexture9
*volume_texture
= (IDirect3DVolumeTexture9
*) texture
;
150 IDirect3DVolumeTexture9_GetLevelDesc(volume_texture
, srclevel
, &desc
);
152 if (filter
== D3DX_DEFAULT
)
154 if (is_pow2(desc
.Width
) && is_pow2(desc
.Height
) && is_pow2(desc
.Depth
))
155 filter
= D3DX_FILTER_BOX
;
157 filter
= D3DX_FILTER_BOX
| D3DX_FILTER_DITHER
;
160 hr
= IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, srclevel
, &top_volume
);
164 level_count
= IDirect3DVolumeTexture9_GetLevelCount(volume_texture
);
165 for (level
= srclevel
+ 1; level
< level_count
; level
++)
167 IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, level
, &mip_volume
);
168 hr
= D3DXLoadVolumeFromVolume(mip_volume
, palette
, NULL
, top_volume
, palette
, NULL
, filter
, 0);
169 IDirect3DVolume9_Release(top_volume
);
170 top_volume
= mip_volume
;
176 IDirect3DVolume9_Release(top_volume
);
184 return D3DERR_INVALIDCALL
;
188 static D3DFORMAT
get_luminance_replacement_format(D3DFORMAT format
)
192 D3DFORMAT luminance_format
;
193 D3DFORMAT replacement_format
;
194 } luminance_replacements
[] =
196 {D3DFMT_L8
, D3DFMT_X8R8G8B8
},
197 {D3DFMT_A8L8
, D3DFMT_A8R8G8B8
},
198 {D3DFMT_A4L4
, D3DFMT_A4R4G4B4
},
199 {D3DFMT_L16
, D3DFMT_A16B16G16R16
}
203 for (i
= 0; i
< sizeof(luminance_replacements
) / sizeof(luminance_replacements
[0]); ++i
)
204 if (format
== luminance_replacements
[i
].luminance_format
)
205 return luminance_replacements
[i
].replacement_format
;
209 HRESULT WINAPI
D3DXCheckTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
210 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
212 UINT w
= (width
&& *width
) ? *width
: 1;
213 UINT h
= (height
&& *height
) ? *height
: 1;
215 D3DDEVICE_CREATION_PARAMETERS params
;
216 IDirect3D9
*d3d
= NULL
;
219 D3DFORMAT usedformat
= D3DFMT_UNKNOWN
;
220 const struct pixel_format_desc
*fmt
;
222 TRACE("(%p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, miplevels
, usage
, format
, pool
);
225 return D3DERR_INVALIDCALL
;
228 if (usage
== D3DX_DEFAULT
)
230 if (usage
& (D3DUSAGE_WRITEONLY
| D3DUSAGE_DONOTCLIP
| D3DUSAGE_POINTS
| D3DUSAGE_RTPATCHES
| D3DUSAGE_NPATCHES
))
231 return D3DERR_INVALIDCALL
;
234 if ((pool
!= D3DPOOL_DEFAULT
) && (pool
!= D3DPOOL_MANAGED
) && (pool
!= D3DPOOL_SYSTEMMEM
) && (pool
!= D3DPOOL_SCRATCH
))
235 return D3DERR_INVALIDCALL
;
240 TRACE("Requested format %x\n", *format
);
241 usedformat
= *format
;
244 hr
= IDirect3DDevice9_GetDirect3D(device
, &d3d
);
249 hr
= IDirect3DDevice9_GetCreationParameters(device
, ¶ms
);
254 hr
= IDirect3DDevice9_GetDisplayMode(device
, 0, &mode
);
259 if ((usedformat
== D3DFMT_UNKNOWN
) || (usedformat
== D3DX_DEFAULT
))
260 usedformat
= D3DFMT_A8R8G8B8
;
262 fmt
= get_format_info(usedformat
);
264 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
, mode
.Format
,
265 usage
, D3DRTYPE_TEXTURE
, usedformat
);
269 int bestscore
= INT_MIN
, i
= 0, j
;
270 unsigned int channels
;
271 const struct pixel_format_desc
*curfmt
, *bestfmt
= NULL
;
273 TRACE("Requested format is not supported, looking for a fallback.\n");
277 FIXME("Pixel format %x not handled\n", usedformat
);
280 fmt
= get_format_info(get_luminance_replacement_format(usedformat
));
282 allow_24bits
= fmt
->bytes_per_pixel
== 3;
283 channels
= !!fmt
->bits
[0] + !!fmt
->bits
[1] + !!fmt
->bits
[2] + !!fmt
->bits
[3];
284 usedformat
= D3DFMT_UNKNOWN
;
286 while ((curfmt
= get_format_info_idx(i
)))
288 unsigned int curchannels
= !!curfmt
->bits
[0] + !!curfmt
->bits
[1]
289 + !!curfmt
->bits
[2] + !!curfmt
->bits
[3];
294 if (curchannels
< channels
)
296 if (curfmt
->bytes_per_pixel
== 3 && !allow_24bits
)
299 hr
= IDirect3D9_CheckDeviceFormat(d3d
, params
.AdapterOrdinal
, params
.DeviceType
,
300 mode
.Format
, usage
, D3DRTYPE_TEXTURE
, curfmt
->format
);
304 /* This format can be used, let's evaluate it.
305 Weights chosen quite arbitrarily... */
306 score
= 512 * (curfmt
->type
== fmt
->type
);
307 score
-= 32 * (curchannels
- channels
);
309 for (j
= 0; j
< 4; j
++)
311 int diff
= curfmt
->bits
[j
] - fmt
->bits
[j
];
312 score
-= (diff
< 0 ? -diff
* 8 : diff
) * (j
== 0 ? 1 : 2);
315 if (score
> bestscore
)
318 usedformat
= curfmt
->format
;
326 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
327 return D3DERR_INVALIDCALL
;
329 if ((w
== D3DX_DEFAULT
) && (h
== D3DX_DEFAULT
))
331 else if (w
== D3DX_DEFAULT
)
332 w
= (height
? h
: 256);
333 else if (h
== D3DX_DEFAULT
)
334 h
= (width
? w
: 256);
336 if (fmt
->block_width
!= 1 || fmt
->block_height
!= 1)
338 if (w
< fmt
->block_width
)
339 w
= fmt
->block_width
;
340 if (h
< fmt
->block_height
)
341 h
= fmt
->block_height
;
344 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(w
)))
347 if (w
> caps
.MaxTextureWidth
)
348 w
= caps
.MaxTextureWidth
;
350 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_POW2
) && (!is_pow2(h
)))
353 if (h
> caps
.MaxTextureHeight
)
354 h
= caps
.MaxTextureHeight
;
356 if (caps
.TextureCaps
& D3DPTEXTURECAPS_SQUAREONLY
)
370 if (miplevels
&& (usage
& D3DUSAGE_AUTOGENMIPMAP
))
377 UINT max_mipmaps
= 1;
379 if (!width
&& !height
)
380 max_mipmaps
= 9; /* number of mipmaps in a 256x256 texture */
383 UINT max_dimen
= max(w
, h
);
385 while (max_dimen
> 1)
392 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
393 *miplevels
= max_mipmaps
;
399 IDirect3D9_Release(d3d
);
404 if (usedformat
== D3DFMT_UNKNOWN
)
406 WARN("Couldn't find a suitable pixel format\n");
407 return D3DERR_NOTAVAILABLE
;
410 TRACE("Format chosen: %x\n", usedformat
);
412 *format
= usedformat
;
417 HRESULT WINAPI
D3DXCheckCubeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*size
,
418 UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
421 UINT s
= (size
&& *size
) ? *size
: 256;
424 TRACE("(%p, %p, %p, %u, %p, %u)\n", device
, size
, miplevels
, usage
, format
, pool
);
426 if (s
== D3DX_DEFAULT
)
429 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
430 return D3DERR_INVALIDCALL
;
432 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP
))
433 return D3DERR_NOTAVAILABLE
;
435 /* ensure width/height is power of 2 */
436 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_CUBEMAP_POW2
) && (!is_pow2(s
)))
439 hr
= D3DXCheckTextureRequirements(device
, &s
, &s
, miplevels
, usage
, format
, pool
);
441 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPCUBEMAP
))
453 HRESULT WINAPI
D3DXCheckVolumeTextureRequirements(struct IDirect3DDevice9
*device
, UINT
*width
, UINT
*height
,
454 UINT
*depth
, UINT
*miplevels
, DWORD usage
, D3DFORMAT
*format
, D3DPOOL pool
)
457 UINT w
= width
? *width
: D3DX_DEFAULT
;
458 UINT h
= height
? *height
: D3DX_DEFAULT
;
459 UINT d
= (depth
&& *depth
) ? *depth
: 1;
462 TRACE("(%p, %p, %p, %p, %p, %u, %p, %u)\n", device
, width
, height
, depth
, miplevels
,
463 usage
, format
, pool
);
465 if (!device
|| FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
466 return D3DERR_INVALIDCALL
;
468 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP
))
469 return D3DERR_NOTAVAILABLE
;
471 hr
= D3DXCheckTextureRequirements(device
, &w
, &h
, NULL
, usage
, format
, pool
);
472 if (d
== D3DX_DEFAULT
)
475 /* ensure width/height is power of 2 */
476 if ((caps
.TextureCaps
& D3DPTEXTURECAPS_VOLUMEMAP_POW2
) &&
477 (!is_pow2(w
) || !is_pow2(h
) || !is_pow2(d
)))
484 if (w
> caps
.MaxVolumeExtent
)
485 w
= caps
.MaxVolumeExtent
;
486 if (h
> caps
.MaxVolumeExtent
)
487 h
= caps
.MaxVolumeExtent
;
488 if (d
> caps
.MaxVolumeExtent
)
489 d
= caps
.MaxVolumeExtent
;
493 if (!(caps
.TextureCaps
& D3DPTEXTURECAPS_MIPVOLUMEMAP
))
495 else if ((usage
& D3DUSAGE_AUTOGENMIPMAP
))
502 UINT max_mipmaps
= 1;
503 UINT max_dimen
= max(max(w
, h
), d
);
505 while (max_dimen
> 1)
511 if (*miplevels
== 0 || *miplevels
> max_mipmaps
)
512 *miplevels
= max_mipmaps
;
526 HRESULT WINAPI
D3DXCreateTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
,
527 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DTexture9
**texture
)
531 TRACE("device %p, width %u, height %u, miplevels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
532 device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
534 if (!device
|| !texture
)
535 return D3DERR_INVALIDCALL
;
537 if (FAILED(hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
)))
540 return IDirect3DDevice9_CreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
, NULL
);
543 static D3DFORMAT
get_alpha_replacement_format(D3DFORMAT format
)
547 D3DFORMAT orig_format
;
548 D3DFORMAT replacement_format
;
550 replacement_formats
[] =
552 {D3DFMT_X8R8G8B8
, D3DFMT_A8R8G8B8
},
553 {D3DFMT_X1R5G5B5
, D3DFMT_A1R5G5B5
},
554 {D3DFMT_X4R4G4B4
, D3DFMT_A4R4G4B4
},
555 {D3DFMT_X8B8G8R8
, D3DFMT_A8B8G8R8
},
556 {D3DFMT_L8
, D3DFMT_A8L8
},
560 for (i
= 0; i
< sizeof(replacement_formats
) / sizeof(replacement_formats
[0]); ++i
)
561 if (replacement_formats
[i
].orig_format
== format
)
562 return replacement_formats
[i
].replacement_format
;
566 HRESULT WINAPI
D3DXCreateTextureFromFileInMemoryEx(struct IDirect3DDevice9
*device
, const void *srcdata
,
567 UINT srcdatasize
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
568 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
569 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
571 IDirect3DTexture9
**texptr
;
572 IDirect3DTexture9
*buftex
;
573 IDirect3DSurface9
*surface
;
574 BOOL dynamic_texture
, format_specified
= FALSE
;
575 D3DXIMAGE_INFO imginfo
;
576 UINT loaded_miplevels
, skip_levels
;
580 TRACE("device %p, srcdata %p, srcdatasize %u, width %u, height %u, miplevels %u,"
581 " usage %#x, format %#x, pool %#x, filter %#x, mipfilter %#x, colorkey %#x,"
582 " srcinfo %p, palette %p, texture %p.\n",
583 device
, srcdata
, srcdatasize
, width
, height
, miplevels
, usage
, format
, pool
,
584 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
586 /* check for invalid parameters */
587 if (!device
|| !texture
|| !srcdata
|| !srcdatasize
)
588 return D3DERR_INVALIDCALL
;
590 hr
= D3DXGetImageInfoFromFileInMemory(srcdata
, srcdatasize
, &imginfo
);
593 FIXME("Unrecognized file format, returning failure.\n");
598 /* handle default values */
599 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
600 width
= imginfo
.Width
;
602 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
603 height
= imginfo
.Height
;
605 if (width
== D3DX_DEFAULT
)
606 width
= make_pow2(imginfo
.Width
);
608 if (height
== D3DX_DEFAULT
)
609 height
= make_pow2(imginfo
.Height
);
611 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
612 format
= imginfo
.Format
;
614 format_specified
= TRUE
;
616 if (width
== D3DX_FROM_FILE
)
618 width
= imginfo
.Width
;
621 if (height
== D3DX_FROM_FILE
)
623 height
= imginfo
.Height
;
626 if (format
== D3DFMT_FROM_FILE
)
628 format
= imginfo
.Format
;
631 if (miplevels
== D3DX_FROM_FILE
)
633 miplevels
= imginfo
.MipLevels
;
636 skip_levels
= mipfilter
!= D3DX_DEFAULT
? mipfilter
>> D3DX_SKIP_DDS_MIP_LEVELS_SHIFT
: 0;
637 if (skip_levels
&& imginfo
.MipLevels
> skip_levels
)
639 TRACE("Skipping the first %u (of %u) levels of a DDS mipmapped texture.\n",
640 skip_levels
, imginfo
.MipLevels
);
641 TRACE("Texture level 0 dimensions are %ux%u.\n", imginfo
.Width
, imginfo
.Height
);
642 width
>>= skip_levels
;
643 height
>>= skip_levels
;
644 miplevels
-= skip_levels
;
651 /* fix texture creation parameters */
652 hr
= D3DXCheckTextureRequirements(device
, &width
, &height
, &miplevels
, usage
, &format
, pool
);
655 FIXME("Couldn't find suitable texture parameters.\n");
660 if (colorkey
&& !format_specified
)
661 format
= get_alpha_replacement_format(format
);
663 if (imginfo
.MipLevels
< miplevels
&& (D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
))
665 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet.\n");
666 miplevels
= imginfo
.MipLevels
;
668 if (imginfo
.ResourceType
== D3DRTYPE_VOLUMETEXTURE
669 && D3DFMT_DXT1
<= imginfo
.Format
&& imginfo
.Format
<= D3DFMT_DXT5
&& miplevels
> 1)
671 FIXME("Generation of mipmaps for compressed pixel formats is not implemented yet.\n");
675 if (FAILED(IDirect3DDevice9_GetDeviceCaps(device
, &caps
)))
676 return D3DERR_INVALIDCALL
;
678 /* Create the to-be-filled texture */
679 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
680 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
682 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, 0, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
687 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
693 FIXME("Texture creation failed.\n");
698 TRACE("Texture created correctly. Now loading the texture data into it.\n");
699 if (imginfo
.ImageFileFormat
!= D3DXIFF_DDS
)
701 IDirect3DTexture9_GetSurfaceLevel(*texptr
, 0, &surface
);
702 hr
= D3DXLoadSurfaceFromFileInMemory(surface
, palette
, NULL
, srcdata
, srcdatasize
, NULL
, filter
, colorkey
, NULL
);
703 IDirect3DSurface9_Release(surface
);
704 loaded_miplevels
= min(IDirect3DTexture9_GetLevelCount(*texptr
), imginfo
.MipLevels
);
708 hr
= load_texture_from_dds(*texptr
, srcdata
, palette
, filter
, colorkey
, &imginfo
, skip_levels
,
714 FIXME("Texture loading failed.\n");
715 IDirect3DTexture9_Release(*texptr
);
720 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) *texptr
, palette
, loaded_miplevels
- 1, mipfilter
);
723 FIXME("Texture filtering failed.\n");
724 IDirect3DTexture9_Release(*texptr
);
729 /* Move the data to the actual texture if necessary */
730 if (texptr
== &buftex
)
732 hr
= D3DXCreateTexture(device
, width
, height
, miplevels
, usage
, format
, pool
, texture
);
736 IDirect3DTexture9_Release(buftex
);
741 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)(*texture
));
742 IDirect3DTexture9_Release(buftex
);
751 HRESULT WINAPI
D3DXCreateTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
752 const void *srcdata
, UINT srcdatasize
, struct IDirect3DTexture9
**texture
)
754 TRACE("(%p, %p, %d, %p)\n", device
, srcdata
, srcdatasize
, texture
);
756 return D3DXCreateTextureFromFileInMemoryEx(device
, srcdata
, srcdatasize
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
757 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
760 HRESULT WINAPI
D3DXCreateTextureFromFileExW(struct IDirect3DDevice9
*device
, const WCHAR
*srcfile
,
761 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
762 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
763 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
769 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
770 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
771 device
, debugstr_w(srcfile
), width
, height
, miplevels
, usage
, format
,
772 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
775 return D3DERR_INVALIDCALL
;
777 hr
= map_view_of_file(srcfile
, &buffer
, &size
);
779 return D3DXERR_INVALIDDATA
;
781 hr
= D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
, usage
, format
, pool
,
782 filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
784 UnmapViewOfFile(buffer
);
789 HRESULT WINAPI
D3DXCreateTextureFromFileExA(struct IDirect3DDevice9
*device
, const char *srcfile
,
790 UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
791 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
792 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
798 TRACE("device %p, srcfile %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
799 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
800 device
, debugstr_a(srcfile
), width
, height
, miplevels
, usage
, format
,
801 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
803 if (!device
|| !srcfile
|| !texture
)
804 return D3DERR_INVALIDCALL
;
806 len
= MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, NULL
, 0);
807 widename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(*widename
));
808 MultiByteToWideChar(CP_ACP
, 0, srcfile
, -1, widename
, len
);
810 hr
= D3DXCreateTextureFromFileExW(device
, widename
, width
, height
, miplevels
,
811 usage
, format
, pool
, filter
, mipfilter
,
812 colorkey
, srcinfo
, palette
, texture
);
814 HeapFree(GetProcessHeap(), 0, widename
);
818 HRESULT WINAPI
D3DXCreateTextureFromFileA(struct IDirect3DDevice9
*device
,
819 const char *srcfile
, struct IDirect3DTexture9
**texture
)
821 TRACE("(%p, %s, %p)\n", device
, debugstr_a(srcfile
), texture
);
823 return D3DXCreateTextureFromFileExA(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
824 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
827 HRESULT WINAPI
D3DXCreateTextureFromFileW(struct IDirect3DDevice9
*device
,
828 const WCHAR
*srcfile
, struct IDirect3DTexture9
**texture
)
830 TRACE("(%p, %s, %p)\n", device
, debugstr_w(srcfile
), texture
);
832 return D3DXCreateTextureFromFileExW(device
, srcfile
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
833 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
837 HRESULT WINAPI
D3DXCreateTextureFromResourceA(struct IDirect3DDevice9
*device
,
838 HMODULE srcmodule
, const char *resource
, struct IDirect3DTexture9
**texture
)
840 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_a(resource
));
842 return D3DXCreateTextureFromResourceExA(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
843 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
846 HRESULT WINAPI
D3DXCreateTextureFromResourceW(struct IDirect3DDevice9
*device
,
847 HMODULE srcmodule
, const WCHAR
*resource
, struct IDirect3DTexture9
**texture
)
849 TRACE("(%p, %s): relay\n", srcmodule
, debugstr_w(resource
));
851 return D3DXCreateTextureFromResourceExW(device
, srcmodule
, resource
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
,
852 D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
855 HRESULT WINAPI
D3DXCreateTextureFromResourceExA(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
856 const char *resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
857 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
858 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
864 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
865 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
866 device
, srcmodule
, debugstr_a(resource
), width
, height
, miplevels
, usage
, format
,
867 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
869 if (!device
|| !texture
)
870 return D3DERR_INVALIDCALL
;
872 if (!(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_RCDATA
))
873 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
874 && !(resinfo
= FindResourceA(srcmodule
, resource
, (const char *)RT_BITMAP
)))
875 return D3DXERR_INVALIDDATA
;
877 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
878 return D3DXERR_INVALIDDATA
;
880 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
881 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
884 HRESULT WINAPI
D3DXCreateTextureFromResourceExW(struct IDirect3DDevice9
*device
, HMODULE srcmodule
,
885 const WCHAR
*resource
, UINT width
, UINT height
, UINT miplevels
, DWORD usage
, D3DFORMAT format
,
886 D3DPOOL pool
, DWORD filter
, DWORD mipfilter
, D3DCOLOR colorkey
, D3DXIMAGE_INFO
*srcinfo
,
887 PALETTEENTRY
*palette
, struct IDirect3DTexture9
**texture
)
893 TRACE("device %p, srcmodule %p, resource %s, width %u, height %u, miplevels %u, usage %#x, format %#x, "
894 "pool %#x, filter %#x, mipfilter %#x, colorkey 0x%08x, srcinfo %p, palette %p, texture %p.\n",
895 device
, srcmodule
, debugstr_w(resource
), width
, height
, miplevels
, usage
, format
,
896 pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
898 if (!device
|| !texture
)
899 return D3DERR_INVALIDCALL
;
901 if (!(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_RCDATA
))
902 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
903 && !(resinfo
= FindResourceW(srcmodule
, resource
, (const WCHAR
*)RT_BITMAP
)))
904 return D3DXERR_INVALIDDATA
;
906 if (FAILED(load_resource_into_memory(srcmodule
, resinfo
, &buffer
, &size
)))
907 return D3DXERR_INVALIDDATA
;
909 return D3DXCreateTextureFromFileInMemoryEx(device
, buffer
, size
, width
, height
, miplevels
,
910 usage
, format
, pool
, filter
, mipfilter
, colorkey
, srcinfo
, palette
, texture
);
913 HRESULT WINAPI
D3DXCreateCubeTexture(struct IDirect3DDevice9
*device
, UINT size
, UINT miplevels
,
914 DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DCubeTexture9
**texture
)
918 TRACE("(%p, %u, %u, %#x, %#x, %#x, %p)\n", device
, size
, miplevels
, usage
, format
,
921 if (!device
|| !texture
)
922 return D3DERR_INVALIDCALL
;
924 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &miplevels
, usage
, &format
, pool
);
928 TRACE("D3DXCheckCubeTextureRequirements failed\n");
932 return IDirect3DDevice9_CreateCubeTexture(device
, size
, miplevels
, usage
, format
, pool
, texture
, NULL
);
935 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemory(struct IDirect3DDevice9
*device
,
936 const void *data
, UINT datasize
, struct IDirect3DCubeTexture9
**texture
)
938 TRACE("(%p, %p, %u, %p)\n", device
, data
, datasize
, texture
);
940 return D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, datasize
, D3DX_DEFAULT
, D3DX_DEFAULT
,
941 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, texture
);
944 HRESULT WINAPI
D3DXCreateVolumeTexture(struct IDirect3DDevice9
*device
, UINT width
, UINT height
, UINT depth
,
945 UINT miplevels
, DWORD usage
, D3DFORMAT format
, D3DPOOL pool
, struct IDirect3DVolumeTexture9
**texture
)
949 TRACE("(%p, %u, %u, %u, %u, %#x, %#x, %#x, %p)\n", device
, width
, height
, depth
,
950 miplevels
, usage
, format
, pool
, texture
);
952 if (!device
|| !texture
)
953 return D3DERR_INVALIDCALL
;
955 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
,
956 &miplevels
, usage
, &format
, pool
);
960 TRACE("D3DXCheckVolumeTextureRequirements failed\n");
964 return IDirect3DDevice9_CreateVolumeTexture(device
, width
, height
, depth
, miplevels
,
965 usage
, format
, pool
, texture
, NULL
);
968 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileA(IDirect3DDevice9
*device
,
969 const char *filename
,
970 IDirect3DVolumeTexture9
**volume_texture
)
978 TRACE("(%p, %s, %p): relay\n",
979 device
, debugstr_a(filename
), volume_texture
);
981 if (!filename
) return D3DERR_INVALIDCALL
;
983 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
984 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
985 if (!filenameW
) return E_OUTOFMEMORY
;
986 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
988 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
989 HeapFree(GetProcessHeap(), 0, filenameW
);
990 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
992 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
993 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
995 UnmapViewOfFile(data
);
999 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileW(IDirect3DDevice9
*device
,
1000 const WCHAR
*filename
,
1001 IDirect3DVolumeTexture9
**volume_texture
)
1007 TRACE("(%p, %s, %p): relay\n",
1008 device
, debugstr_w(filename
), volume_texture
);
1010 if (!filename
) return D3DERR_INVALIDCALL
;
1012 hr
= map_view_of_file(filename
, &data
, &data_size
);
1013 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1015 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1016 D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, volume_texture
);
1018 UnmapViewOfFile(data
);
1022 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExA(IDirect3DDevice9
*device
,
1023 const char *filename
,
1034 D3DXIMAGE_INFO
*src_info
,
1035 PALETTEENTRY
*palette
,
1036 IDirect3DVolumeTexture9
**volume_texture
)
1044 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1045 device
, debugstr_a(filename
), width
, height
, depth
, mip_levels
,
1046 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1047 palette
, volume_texture
);
1049 if (!filename
) return D3DERR_INVALIDCALL
;
1051 len
= MultiByteToWideChar(CP_ACP
, 0, filename
, -1, NULL
, 0);
1052 filenameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1053 if (!filenameW
) return E_OUTOFMEMORY
;
1054 MultiByteToWideChar(CP_ACP
, 0, filename
, -1, filenameW
, len
);
1056 hr
= map_view_of_file(filenameW
, &data
, &data_size
);
1057 HeapFree(GetProcessHeap(), 0, filenameW
);
1058 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1060 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1061 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1064 UnmapViewOfFile(data
);
1068 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileExW(IDirect3DDevice9
*device
,
1069 const WCHAR
*filename
,
1080 D3DXIMAGE_INFO
*src_info
,
1081 PALETTEENTRY
*palette
,
1082 IDirect3DVolumeTexture9
**volume_texture
)
1088 TRACE("(%p, %s, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1089 device
, debugstr_w(filename
), width
, height
, depth
, mip_levels
,
1090 usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
,
1091 palette
, volume_texture
);
1093 if (!filename
) return D3DERR_INVALIDCALL
;
1095 hr
= map_view_of_file(filename
, &data
, &data_size
);
1096 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1098 hr
= D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, width
, height
, depth
,
1099 mip_levels
, usage
, format
, pool
, filter
, mip_filter
, color_key
, src_info
, palette
,
1102 UnmapViewOfFile(data
);
1106 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemory(IDirect3DDevice9
*device
,
1109 IDirect3DVolumeTexture9
**volume_texture
)
1111 TRACE("(%p, %p, %u, %p): relay\n", device
, data
, data_size
, volume_texture
);
1113 return D3DXCreateVolumeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1114 D3DX_DEFAULT
, D3DX_DEFAULT
, 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1115 0, NULL
, NULL
, volume_texture
);
1118 HRESULT WINAPI
D3DXCreateVolumeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1131 D3DXIMAGE_INFO
*info
,
1132 PALETTEENTRY
*palette
,
1133 IDirect3DVolumeTexture9
**volume_texture
)
1137 D3DXIMAGE_INFO image_info
;
1138 BOOL dynamic_texture
;
1139 BOOL file_width
= FALSE
;
1140 BOOL file_height
= FALSE
;
1141 BOOL file_depth
= FALSE
;
1142 BOOL file_format
= FALSE
;
1143 BOOL file_mip_levels
= FALSE
;
1144 IDirect3DVolumeTexture9
*tex
, *buftex
;
1146 TRACE("(%p, %p, %u, %u, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n",
1147 device
, data
, data_size
, width
, height
, depth
, mip_levels
, usage
, format
, pool
,
1148 filter
, mip_filter
, color_key
, info
, palette
, volume_texture
);
1150 if (!device
|| !data
|| !data_size
|| !volume_texture
)
1151 return D3DERR_INVALIDCALL
;
1153 hr
= D3DXGetImageInfoFromFileInMemory(data
, data_size
, &image_info
);
1154 if (FAILED(hr
)) return hr
;
1156 if (image_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1157 return D3DXERR_INVALIDDATA
;
1159 if (width
== 0 || width
== D3DX_DEFAULT_NONPOW2
)
1160 width
= image_info
.Width
;
1161 if (width
== D3DX_DEFAULT
)
1162 width
= make_pow2(image_info
.Width
);
1164 if (height
== 0 || height
== D3DX_DEFAULT_NONPOW2
)
1165 height
= image_info
.Height
;
1166 if (height
== D3DX_DEFAULT
)
1167 height
= make_pow2(image_info
.Height
);
1169 if (depth
== 0 || depth
== D3DX_DEFAULT_NONPOW2
)
1170 depth
= image_info
.Depth
;
1171 if (depth
== D3DX_DEFAULT
)
1172 depth
= make_pow2(image_info
.Depth
);
1174 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1175 format
= image_info
.Format
;
1177 if (width
== D3DX_FROM_FILE
)
1180 width
= image_info
.Width
;
1183 if (height
== D3DX_FROM_FILE
)
1186 height
= image_info
.Height
;
1189 if (depth
== D3DX_FROM_FILE
)
1192 depth
= image_info
.Depth
;
1195 if (format
== D3DFMT_FROM_FILE
)
1198 format
= image_info
.Format
;
1201 if (mip_levels
== D3DX_FROM_FILE
)
1203 file_mip_levels
= TRUE
;
1204 mip_levels
= image_info
.MipLevels
;
1207 hr
= D3DXCheckVolumeTextureRequirements(device
, &width
, &height
, &depth
, &mip_levels
, usage
, &format
, pool
);
1208 if (FAILED(hr
)) return hr
;
1210 if ((file_width
&& width
!= image_info
.Width
)
1211 || (file_height
&& height
!= image_info
.Height
)
1212 || (file_depth
&& depth
!= image_info
.Depth
)
1213 || (file_format
&& format
!= image_info
.Format
)
1214 || (file_mip_levels
&& mip_levels
!= image_info
.MipLevels
))
1215 return D3DERR_NOTAVAILABLE
;
1217 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1219 return D3DERR_INVALIDCALL
;
1221 if (mip_levels
> image_info
.MipLevels
)
1223 FIXME("Generation of mipmaps for volume textures is not implemented yet.\n");
1224 mip_levels
= image_info
.MipLevels
;
1227 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1228 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1230 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1235 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1239 if (FAILED(hr
)) return hr
;
1241 hr
= load_volume_texture_from_dds(tex
, data
, palette
, filter
, color_key
, &image_info
);
1244 IDirect3DVolumeTexture9_Release(tex
);
1250 hr
= D3DXCreateVolumeTexture(device
, width
, height
, depth
, mip_levels
, usage
, format
, pool
, &tex
);
1253 IDirect3DVolumeTexture9_Release(buftex
);
1257 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1258 IDirect3DVolumeTexture9_Release(buftex
);
1264 *volume_texture
= tex
;
1268 static inline void fill_texture(const struct pixel_format_desc
*format
, BYTE
*pos
, const D3DXVECTOR4
*value
)
1272 for (c
= 0; c
< format
->bytes_per_pixel
; c
++)
1275 for (c
= 0; c
< 4; c
++)
1278 DWORD i
, v
= 0, mask32
= format
->bits
[c
] == 32 ? ~0U : ((1 << format
->bits
[c
]) - 1);
1283 comp_value
= value
->w
;
1286 comp_value
= value
->x
;
1289 comp_value
= value
->y
;
1292 comp_value
= value
->z
;
1296 if (format
->type
== FORMAT_ARGBF16
)
1297 v
= float_32_to_16(comp_value
);
1298 else if (format
->type
== FORMAT_ARGBF
)
1299 v
= *(DWORD
*)&comp_value
;
1300 else if (format
->type
== FORMAT_ARGB
)
1301 v
= comp_value
* ((1 << format
->bits
[c
]) - 1) + 0.5f
;
1303 FIXME("Unhandled format type %#x\n", format
->type
);
1305 for (i
= 0; i
< format
->bits
[c
] + format
->shift
[c
]; i
+= 8)
1309 if (format
->shift
[c
] > i
)
1311 mask
= mask32
<< (format
->shift
[c
] - i
);
1312 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1316 mask
= mask32
>> (i
- format
->shift
[c
]);
1317 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1324 HRESULT WINAPI
D3DXFillTexture(struct IDirect3DTexture9
*texture
, LPD3DXFILL2D function
, void *funcdata
)
1328 D3DSURFACE_DESC desc
;
1329 D3DLOCKED_RECT lock_rect
;
1331 D3DXVECTOR2 coord
, size
;
1332 const struct pixel_format_desc
*format
;
1335 if (texture
== NULL
|| function
== NULL
)
1336 return D3DERR_INVALIDCALL
;
1338 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1340 for (m
= 0; m
< miplevels
; m
++)
1342 if (FAILED(IDirect3DTexture9_GetLevelDesc(texture
, m
, &desc
)))
1343 return D3DERR_INVALIDCALL
;
1345 format
= get_format_info(desc
.Format
);
1346 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1348 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1349 return D3DERR_INVALIDCALL
;
1352 if (FAILED(IDirect3DTexture9_LockRect(texture
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1353 return D3DERR_INVALIDCALL
;
1355 size
.x
= 1.0f
/ desc
.Width
;
1356 size
.y
= 1.0f
/ desc
.Height
;
1358 data
= lock_rect
.pBits
;
1360 for (y
= 0; y
< desc
.Height
; y
++)
1362 /* The callback function expects the coordinates of the center
1364 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1366 for (x
= 0; x
< desc
.Width
; x
++)
1368 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1370 function(&value
, &coord
, &size
, funcdata
);
1372 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1375 IDirect3DTexture9_UnlockRect(texture
, m
);
1381 HRESULT WINAPI
D3DXFillTextureTX(struct IDirect3DTexture9
*texture
, ID3DXTextureShader
*texture_shader
)
1383 FIXME("texture %p, texture_shader %p stub.\n", texture
, texture_shader
);
1387 HRESULT WINAPI
D3DXCreateCubeTextureFromFileInMemoryEx(IDirect3DDevice9
*device
,
1388 const void *src_data
,
1398 D3DXIMAGE_INFO
*src_info
,
1399 PALETTEENTRY
*palette
,
1400 IDirect3DCubeTexture9
**cube_texture
)
1404 UINT loaded_miplevels
;
1405 D3DXIMAGE_INFO img_info
;
1406 BOOL dynamic_texture
;
1407 BOOL file_size
= FALSE
;
1408 BOOL file_format
= FALSE
;
1409 BOOL file_mip_levels
= FALSE
;
1410 IDirect3DCubeTexture9
*tex
, *buftex
;
1412 TRACE("(%p, %p, %u, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p)\n", device
,
1413 src_data
, src_data_size
, size
, mip_levels
, usage
, format
, pool
, filter
, mip_filter
,
1414 color_key
, src_info
, palette
, cube_texture
);
1416 if (!device
|| !cube_texture
|| !src_data
|| !src_data_size
)
1417 return D3DERR_INVALIDCALL
;
1419 hr
= D3DXGetImageInfoFromFileInMemory(src_data
, src_data_size
, &img_info
);
1423 if (img_info
.ImageFileFormat
!= D3DXIFF_DDS
)
1424 return D3DXERR_INVALIDDATA
;
1426 if (img_info
.Width
!= img_info
.Height
)
1427 return D3DXERR_INVALIDDATA
;
1429 if (size
== 0 || size
== D3DX_DEFAULT_NONPOW2
)
1430 size
= img_info
.Width
;
1431 if (size
== D3DX_DEFAULT
)
1432 size
= make_pow2(img_info
.Width
);
1434 if (format
== D3DFMT_UNKNOWN
|| format
== D3DX_DEFAULT
)
1435 format
= img_info
.Format
;
1437 if (size
== D3DX_FROM_FILE
)
1440 size
= img_info
.Width
;
1443 if (format
== D3DFMT_FROM_FILE
)
1446 format
= img_info
.Format
;
1449 if (mip_levels
== D3DX_FROM_FILE
)
1451 file_mip_levels
= TRUE
;
1452 mip_levels
= img_info
.MipLevels
;
1455 hr
= D3DXCheckCubeTextureRequirements(device
, &size
, &mip_levels
, usage
, &format
, pool
);
1459 if ((file_size
&& size
!= img_info
.Width
)
1460 || (file_format
&& format
!= img_info
.Format
)
1461 || (file_mip_levels
&& mip_levels
!= img_info
.MipLevels
))
1462 return D3DERR_NOTAVAILABLE
;
1464 hr
= IDirect3DDevice9_GetDeviceCaps(device
, &caps
);
1466 return D3DERR_INVALIDCALL
;
1468 if (mip_levels
> img_info
.MipLevels
&& (D3DFMT_DXT1
<= img_info
.Format
&& img_info
.Format
<= D3DFMT_DXT5
))
1470 FIXME("Generation of mipmaps for compressed pixel formats is not supported yet.\n");
1471 mip_levels
= img_info
.MipLevels
;
1474 dynamic_texture
= (caps
.Caps2
& D3DCAPS2_DYNAMICTEXTURES
) && (usage
& D3DUSAGE_DYNAMIC
);
1475 if (pool
== D3DPOOL_DEFAULT
&& !dynamic_texture
)
1477 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, D3DPOOL_SYSTEMMEM
, &buftex
);
1482 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1488 hr
= load_cube_texture_from_dds(tex
, src_data
, palette
, filter
, color_key
, &img_info
);
1491 IDirect3DCubeTexture9_Release(tex
);
1495 loaded_miplevels
= min(IDirect3DCubeTexture9_GetLevelCount(tex
), img_info
.MipLevels
);
1496 hr
= D3DXFilterTexture((IDirect3DBaseTexture9
*) tex
, palette
, loaded_miplevels
- 1, mip_filter
);
1499 IDirect3DCubeTexture9_Release(tex
);
1505 hr
= D3DXCreateCubeTexture(device
, size
, mip_levels
, usage
, format
, pool
, &tex
);
1508 IDirect3DCubeTexture9_Release(buftex
);
1512 IDirect3DDevice9_UpdateTexture(device
, (IDirect3DBaseTexture9
*)buftex
, (IDirect3DBaseTexture9
*)tex
);
1513 IDirect3DCubeTexture9_Release(buftex
);
1517 *src_info
= img_info
;
1519 *cube_texture
= tex
;
1524 HRESULT WINAPI
D3DXCreateCubeTextureFromFileA(IDirect3DDevice9
*device
,
1525 const char *src_filename
,
1526 IDirect3DCubeTexture9
**cube_texture
)
1534 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_a(src_filename
), cube_texture
);
1536 if (!src_filename
) return D3DERR_INVALIDCALL
;
1538 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1539 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1540 if (!filename
) return E_OUTOFMEMORY
;
1541 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1543 hr
= map_view_of_file(filename
, &data
, &data_size
);
1546 HeapFree(GetProcessHeap(), 0, filename
);
1547 return D3DXERR_INVALIDDATA
;
1550 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1551 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1553 UnmapViewOfFile(data
);
1554 HeapFree(GetProcessHeap(), 0, filename
);
1558 HRESULT WINAPI
D3DXCreateCubeTextureFromFileW(IDirect3DDevice9
*device
,
1559 const WCHAR
*src_filename
,
1560 IDirect3DCubeTexture9
**cube_texture
)
1566 TRACE("(%p, %s, %p): relay\n", device
, wine_dbgstr_w(src_filename
), cube_texture
);
1568 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1569 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1571 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, D3DX_DEFAULT
, D3DX_DEFAULT
,
1572 0, D3DFMT_UNKNOWN
, D3DPOOL_MANAGED
, D3DX_DEFAULT
, D3DX_DEFAULT
, 0, NULL
, NULL
, cube_texture
);
1574 UnmapViewOfFile(data
);
1578 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExA(IDirect3DDevice9
*device
,
1579 const char *src_filename
,
1588 D3DXIMAGE_INFO
*image_info
,
1589 PALETTEENTRY
*palette
,
1590 IDirect3DCubeTexture9
**cube_texture
)
1598 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1599 device
, wine_dbgstr_a(src_filename
), size
, mip_levels
, usage
, format
,
1600 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1602 if (!src_filename
) return D3DERR_INVALIDCALL
;
1604 len
= MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, NULL
, 0);
1605 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1606 if (!filename
) return E_OUTOFMEMORY
;
1607 MultiByteToWideChar(CP_ACP
, 0, src_filename
, -1, filename
, len
);
1609 hr
= map_view_of_file(filename
, &data
, &data_size
);
1612 HeapFree(GetProcessHeap(), 0, filename
);
1613 return D3DXERR_INVALIDDATA
;
1616 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1617 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1619 UnmapViewOfFile(data
);
1620 HeapFree(GetProcessHeap(), 0, filename
);
1624 HRESULT WINAPI
D3DXCreateCubeTextureFromFileExW(IDirect3DDevice9
*device
,
1625 const WCHAR
*src_filename
,
1634 D3DXIMAGE_INFO
*image_info
,
1635 PALETTEENTRY
*palette
,
1636 IDirect3DCubeTexture9
**cube_texture
)
1642 TRACE("(%p, %s, %u, %u, %#x, %#x, %#x, %#x, %#x, %#x, %p, %p, %p): relay\n",
1643 device
, wine_dbgstr_w(src_filename
), size
, mip_levels
, usage
, format
,
1644 pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1646 hr
= map_view_of_file(src_filename
, &data
, &data_size
);
1647 if (FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1649 hr
= D3DXCreateCubeTextureFromFileInMemoryEx(device
, data
, data_size
, size
, mip_levels
,
1650 usage
, format
, pool
, filter
, mip_filter
, color_key
, image_info
, palette
, cube_texture
);
1652 UnmapViewOfFile(data
);
1666 static float get_cube_coord(enum cube_coord coord
, unsigned int x
, unsigned int y
, unsigned int size
)
1673 return size
- x
- 0.5f
;
1677 return size
- y
- 0.5f
;
1683 ERR("Unexpected coordinate value\n");
1688 HRESULT WINAPI
D3DXFillCubeTexture(struct IDirect3DCubeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1692 D3DSURFACE_DESC desc
;
1693 D3DLOCKED_RECT lock_rect
;
1695 D3DXVECTOR3 coord
, size
;
1696 const struct pixel_format_desc
*format
;
1698 static const enum cube_coord coordmap
[6][3] =
1700 {ONE
, YCOORDINV
, XCOORDINV
},
1701 {ZERO
, YCOORDINV
, XCOORD
},
1702 {XCOORD
, ONE
, YCOORD
},
1703 {XCOORD
, ZERO
, YCOORDINV
},
1704 {XCOORD
, YCOORDINV
, ONE
},
1705 {XCOORDINV
, YCOORDINV
, ZERO
}
1708 if (texture
== NULL
|| function
== NULL
)
1709 return D3DERR_INVALIDCALL
;
1711 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1713 for (m
= 0; m
< miplevels
; m
++)
1715 if (FAILED(IDirect3DCubeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1716 return D3DERR_INVALIDCALL
;
1718 format
= get_format_info(desc
.Format
);
1719 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1721 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1722 return D3DERR_INVALIDCALL
;
1725 for (f
= 0; f
< 6; f
++)
1727 if (FAILED(IDirect3DCubeTexture9_LockRect(texture
, f
, m
, &lock_rect
, NULL
, D3DLOCK_DISCARD
)))
1728 return D3DERR_INVALIDCALL
;
1730 size
.x
= (f
== 0) || (f
== 1) ? 0.0f
: 2.0f
/ desc
.Width
;
1731 size
.y
= (f
== 2) || (f
== 3) ? 0.0f
: 2.0f
/ desc
.Width
;
1732 size
.z
= (f
== 4) || (f
== 5) ? 0.0f
: 2.0f
/ desc
.Width
;
1734 data
= lock_rect
.pBits
;
1736 for (y
= 0; y
< desc
.Height
; y
++)
1738 for (x
= 0; x
< desc
.Width
; x
++)
1740 coord
.x
= get_cube_coord(coordmap
[f
][0], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1741 coord
.y
= get_cube_coord(coordmap
[f
][1], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1742 coord
.z
= get_cube_coord(coordmap
[f
][2], x
, y
, desc
.Width
) / desc
.Width
* 2.0f
- 1.0f
;
1744 function(&value
, &coord
, &size
, funcdata
);
1746 fill_texture(format
, data
+ y
* lock_rect
.Pitch
+ x
* format
->bytes_per_pixel
, &value
);
1749 IDirect3DCubeTexture9_UnlockRect(texture
, f
, m
);
1756 HRESULT WINAPI
D3DXFillCubeTextureTX(struct IDirect3DCubeTexture9
*texture
, ID3DXTextureShader
*texture_shader
)
1758 FIXME("texture %p, texture_shader %p stub.\n", texture
, texture_shader
);
1762 HRESULT WINAPI
D3DXFillVolumeTexture(struct IDirect3DVolumeTexture9
*texture
, LPD3DXFILL3D function
, void *funcdata
)
1766 D3DVOLUME_DESC desc
;
1767 D3DLOCKED_BOX lock_box
;
1769 D3DXVECTOR3 coord
, size
;
1770 const struct pixel_format_desc
*format
;
1773 if (texture
== NULL
|| function
== NULL
)
1774 return D3DERR_INVALIDCALL
;
1776 miplevels
= IDirect3DBaseTexture9_GetLevelCount(texture
);
1778 for (m
= 0; m
< miplevels
; m
++)
1780 if (FAILED(IDirect3DVolumeTexture9_GetLevelDesc(texture
, m
, &desc
)))
1781 return D3DERR_INVALIDCALL
;
1783 format
= get_format_info(desc
.Format
);
1784 if (format
->type
!= FORMAT_ARGB
&& format
->type
!= FORMAT_ARGBF16
&& format
->type
!= FORMAT_ARGBF
)
1786 FIXME("Unsupported texture format %#x\n", desc
.Format
);
1787 return D3DERR_INVALIDCALL
;
1790 if (FAILED(IDirect3DVolumeTexture9_LockBox(texture
, m
, &lock_box
, NULL
, D3DLOCK_DISCARD
)))
1791 return D3DERR_INVALIDCALL
;
1793 size
.x
= 1.0f
/ desc
.Width
;
1794 size
.y
= 1.0f
/ desc
.Height
;
1795 size
.z
= 1.0f
/ desc
.Depth
;
1797 data
= lock_box
.pBits
;
1799 for (z
= 0; z
< desc
.Depth
; z
++)
1801 /* The callback function expects the coordinates of the center
1803 coord
.z
= (z
+ 0.5f
) / desc
.Depth
;
1805 for (y
= 0; y
< desc
.Height
; y
++)
1807 coord
.y
= (y
+ 0.5f
) / desc
.Height
;
1809 for (x
= 0; x
< desc
.Width
; x
++)
1811 coord
.x
= (x
+ 0.5f
) / desc
.Width
;
1813 function(&value
, &coord
, &size
, funcdata
);
1815 fill_texture(format
, data
+ z
* lock_box
.SlicePitch
+ y
* lock_box
.RowPitch
1816 + x
* format
->bytes_per_pixel
, &value
);
1820 IDirect3DVolumeTexture9_UnlockBox(texture
, m
);
1826 HRESULT WINAPI
D3DXFillVolumeTextureTX(struct IDirect3DVolumeTexture9
*texture
, ID3DXTextureShader
*texture_shader
)
1828 FIXME("texture %p, texture_shader %p stub.\n", texture
, texture_shader
);
1832 HRESULT WINAPI
D3DXSaveTextureToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1833 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1838 ID3DXBuffer
*buffer
;
1840 TRACE("(%s, %#x, %p, %p): relay\n",
1841 wine_dbgstr_a(dst_filename
), file_format
, src_texture
, src_palette
);
1843 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1845 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1846 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1847 if (!filename
) return E_OUTOFMEMORY
;
1848 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1850 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1853 hr
= write_buffer_to_file(filename
, buffer
);
1854 ID3DXBuffer_Release(buffer
);
1857 HeapFree(GetProcessHeap(), 0, filename
);
1861 HRESULT WINAPI
D3DXSaveTextureToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1862 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1865 ID3DXBuffer
*buffer
;
1867 TRACE("(%s, %#x, %p, %p): relay\n",
1868 wine_dbgstr_w(dst_filename
), file_format
, src_texture
, src_palette
);
1870 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1872 hr
= D3DXSaveTextureToFileInMemory(&buffer
, file_format
, src_texture
, src_palette
);
1875 hr
= write_buffer_to_file(dst_filename
, buffer
);
1876 ID3DXBuffer_Release(buffer
);
1882 HRESULT WINAPI
D3DXSaveTextureToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1883 IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
1886 D3DRESOURCETYPE type
;
1887 IDirect3DSurface9
*surface
;
1889 TRACE("(%p, %#x, %p, %p)\n",
1890 dst_buffer
, file_format
, src_texture
, src_palette
);
1892 if (!dst_buffer
|| !src_texture
) return D3DERR_INVALIDCALL
;
1894 if (file_format
== D3DXIFF_DDS
)
1896 FIXME("DDS file format isn't supported yet\n");
1900 type
= IDirect3DBaseTexture9_GetType(src_texture
);
1903 case D3DRTYPE_TEXTURE
:
1904 case D3DRTYPE_CUBETEXTURE
:
1905 hr
= get_surface(type
, src_texture
, D3DCUBEMAP_FACE_POSITIVE_X
, 0, &surface
);
1907 case D3DRTYPE_VOLUMETEXTURE
:
1908 FIXME("Volume textures aren't supported yet\n");
1911 return D3DERR_INVALIDCALL
;
1916 hr
= D3DXSaveSurfaceToFileInMemory(dst_buffer
, file_format
, surface
, src_palette
, NULL
);
1917 IDirect3DSurface9_Release(surface
);