4 This files contains the implementation of interface Direct3DTexture2. */
11 #include "wine/obj_base.h"
15 #include "debugtools.h"
17 #include "mesa_private.h"
19 #define D3DDPRIVATE(x) mesa_d3dd_private*odev=(mesa_d3dd_private*)(x)->private
20 #define D3DTPRIVATE(x) mesa_d3dt_private*dtpriv=(mesa_d3dt_private*)(x)->private
22 DEFAULT_DEBUG_CHANNEL(ddraw
);
24 /* Define this if you want to save to a file all the textures used by a game
25 (can be funny to see how they managed to cram all the pictures in
32 #define SNOOP_PALETTED() \
38 sprintf(buf, "%ld.pnm", dtpriv->tex_name); \
39 f = fopen(buf, "wb"); \
40 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
41 for (y = 0; y < src_d->dwHeight; y++) { \
42 for (x = 0; x < src_d->dwWidth; x++) { \
43 unsigned char c = ((unsigned char *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
44 fputc(table[c][0], f); \
45 fputc(table[c][1], f); \
46 fputc(table[c][2], f); \
52 #define SNOOP_5650() \
58 sprintf(buf, "%ld.pnm", dtpriv->tex_name); \
59 f = fopen(buf, "wb"); \
60 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
61 for (y = 0; y < src_d->dwHeight; y++) { \
62 for (x = 0; x < src_d->dwWidth; x++) { \
63 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
64 fputc((c & 0xF800) >> 8, f); \
65 fputc((c & 0x07E0) >> 3, f); \
66 fputc((c & 0x001F) << 3, f); \
72 #define SNOOP_5551() \
78 sprintf(buf, "%ld.pnm", dtpriv->tex_name); \
79 f = fopen(buf, "wb"); \
80 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
81 for (y = 0; y < src_d->dwHeight; y++) { \
82 for (x = 0; x < src_d->dwWidth; x++) { \
83 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
84 fputc((c & 0xF800) >> 8, f); \
85 fputc((c & 0x07C0) >> 3, f); \
86 fputc((c & 0x003E) << 2, f); \
92 #define SNOOP_PALETTED()
97 extern ICOM_VTABLE(IDirect3DTexture2
) mesa_texture2_vtable
;
98 extern ICOM_VTABLE(IDirect3DTexture
) mesa_texture_vtable
;
100 /*******************************************************************************
101 * Texture2 Creation functions
103 LPDIRECT3DTEXTURE2
d3dtexture2_create(IDirectDrawSurfaceImpl
* surf
)
105 IDirect3DTexture2Impl
* tex
;
107 tex
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDirect3DTexture2Impl
));
109 ICOM_VTBL(tex
) = &mesa_texture2_vtable
;
112 tex
->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(mesa_d3dt_private
));
114 return (LPDIRECT3DTEXTURE2
)tex
;
117 /*******************************************************************************
118 * Texture Creation functions
120 LPDIRECT3DTEXTURE
d3dtexture_create(IDirectDrawSurfaceImpl
* surf
)
122 IDirect3DTexture2Impl
* tex
;
124 tex
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDirect3DTexture2Impl
));
126 ICOM_VTBL(tex
) = (ICOM_VTABLE(IDirect3DTexture2
)*)&mesa_texture_vtable
;
129 tex
->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(mesa_d3dt_private
));
131 return (LPDIRECT3DTEXTURE
)tex
;
134 /*******************************************************************************
135 * IDirectSurface callback methods
137 HRESULT WINAPI
SetColorKey_cb(IDirect3DTexture2Impl
*texture
, DWORD dwFlags
, LPDDCOLORKEY ckey
)
139 DDSURFACEDESC
*tex_d
;
140 D3DTPRIVATE(texture
);
142 GLuint current_texture
;
144 TRACE("(%p) : colorkey callback\n", texture
);
146 /* Get the texture description */
147 tex_d
= (DDSURFACEDESC
*)&(texture
->surface
->surface_desc
);
148 bpp
= (tex_d
->ddpfPixelFormat
.dwFlags
& DDPF_PALETTEINDEXED8
?
149 1 /* 8 bit of palette index */:
150 tex_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
/ 8 /* RGB bits for each colors */ );
152 /* Now, save the current texture */
154 glGetIntegerv(GL_TEXTURE_BINDING_2D
, ¤t_texture
);
156 /* If the GetHandle was not done yet, it's an error */
157 if (dtpriv
->tex_name
== 0) {
158 ERR("Unloaded texture !\n");
162 glBindTexture(GL_TEXTURE_2D
, dtpriv
->tex_name
);
164 if (tex_d
->ddpfPixelFormat
.dwFlags
& DDPF_PALETTEINDEXED8
) {
165 FIXME("Todo Paletted\n");
166 } else if (tex_d
->ddpfPixelFormat
.dwFlags
& DDPF_RGB
) {
167 if (tex_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
== 8) {
168 FIXME("Todo 3_3_2_0\n");
169 } else if (tex_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
== 16) {
170 if (tex_d
->ddpfPixelFormat
.u5
.dwRGBAlphaBitMask
== 0x00000000) {
171 /* Now transform the 5_6_5 into a 5_5_5_1 surface to support color keying */
172 unsigned short *dest
= (unsigned short *) HeapAlloc(GetProcessHeap(),
174 tex_d
->dwWidth
* tex_d
->dwHeight
* bpp
);
175 unsigned short *src
= (unsigned short *) tex_d
->lpSurface
;
178 for (y
= 0; y
< tex_d
->dwHeight
; y
++) {
179 for (x
= 0; x
< tex_d
->dwWidth
; x
++) {
180 unsigned short cpixel
= src
[x
+ y
* tex_d
->dwWidth
];
182 if ((dwFlags
& DDCKEY_SRCBLT
) &&
183 (cpixel
>= ckey
->dwColorSpaceLowValue
) &&
184 (cpixel
<= ckey
->dwColorSpaceHighValue
)) /* No alpha bit => this pixel is transparent */
185 dest
[x
+ y
* tex_d
->dwWidth
] = (cpixel
& ~0x003F) | ((cpixel
& 0x001F) << 1) | 0x0000;
186 else /* Alpha bit is set => this pixel will be seen */
187 dest
[x
+ y
* tex_d
->dwWidth
] = (cpixel
& ~0x003F) | ((cpixel
& 0x001F) << 1) | 0x0001;
191 glTexImage2D(GL_TEXTURE_2D
,
194 tex_d
->dwWidth
, tex_d
->dwHeight
,
197 GL_UNSIGNED_SHORT_5_5_5_1
,
200 /* Frees the temporary surface */
201 HeapFree(GetProcessHeap(),0,dest
);
202 } else if (tex_d
->ddpfPixelFormat
.u5
.dwRGBAlphaBitMask
== 0x00000001) {
203 FIXME("Todo 5_5_5_1\n");
204 } else if (tex_d
->ddpfPixelFormat
.u5
.dwRGBAlphaBitMask
== 0x0000000F) {
205 FIXME("Todo 4_4_4_4\n");
207 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
209 } else if (tex_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
== 24) {
210 FIXME("Todo 8_8_8_0\n");
211 } else if (tex_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
== 32) {
212 FIXME("Todo 8_8_8_8\n");
214 ERR("Unhandled texture format (bad RGB count)\n");
217 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
224 /*******************************************************************************
225 * IDirect3DTexture2 methods
228 HRESULT WINAPI
IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface
,
232 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
234 FIXME("(%p)->(%s,%p): stub\n", This
, debugstr_guid(riid
),ppvObj
);
241 ULONG WINAPI
IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface
)
243 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
244 TRACE("(%p)->()incrementing from %lu.\n", This
, This
->ref
);
246 return ++(This
->ref
);
251 ULONG WINAPI
IDirect3DTexture2Impl_Release(LPDIRECT3DTEXTURE2 iface
)
253 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
255 FIXME("(%p)->() decrementing from %lu.\n", This
, This
->ref
);
257 if (!--(This
->ref
)) {
258 /* Delete texture from OpenGL */
260 glDeleteTextures(1, &(dtpriv
->tex_name
));
263 /* Release surface */
264 IDirectDrawSurface4_Release((IDirectDrawSurface4
*)This
->surface
);
266 HeapFree(GetProcessHeap(),0,This
);
273 /*** IDirect3DTexture methods ***/
274 HRESULT WINAPI
IDirect3DTextureImpl_GetHandle(LPDIRECT3DTEXTURE iface
,
275 LPDIRECT3DDEVICE lpD3DDevice
,
276 LPD3DTEXTUREHANDLE lpHandle
)
278 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
280 IDirect3DDeviceImpl
* ilpD3DDevice
=(IDirect3DDeviceImpl
*)lpD3DDevice
;
281 FIXME("(%p)->(%p,%p): stub\n", This
, ilpD3DDevice
, lpHandle
);
283 *lpHandle
= (D3DTEXTUREHANDLE
) This
;
285 /* Now, bind a new texture */
287 ilpD3DDevice
->set_context(ilpD3DDevice
);
288 This
->D3Ddevice
= (void *) ilpD3DDevice
;
289 if (dtpriv
->tex_name
== 0)
290 glGenTextures(1, &(dtpriv
->tex_name
));
293 TRACE("OpenGL texture handle is : %d\n", dtpriv
->tex_name
);
298 HRESULT WINAPI
IDirect3DTextureImpl_Initialize(LPDIRECT3DTEXTURE iface
,
299 LPDIRECT3DDEVICE lpD3DDevice
,
300 LPDIRECTDRAWSURFACE lpSurface
)
302 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
303 TRACE("(%p)->(%p,%p)\n", This
, lpD3DDevice
, lpSurface
);
305 return DDERR_ALREADYINITIALIZED
;
308 HRESULT WINAPI
IDirect3DTextureImpl_Unload(LPDIRECT3DTEXTURE iface
)
310 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
311 FIXME("(%p)->(): stub\n", This
);
316 /*** IDirect3DTexture2 methods ***/
317 HRESULT WINAPI
IDirect3DTexture2Impl_GetHandle(LPDIRECT3DTEXTURE2 iface
,
318 LPDIRECT3DDEVICE2 lpD3DDevice2
,
319 LPD3DTEXTUREHANDLE lpHandle
)
321 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
323 IDirect3DDevice2Impl
* ilpD3DDevice2
=(IDirect3DDevice2Impl
*)lpD3DDevice2
;
324 TRACE("(%p)->(%p,%p)\n", This
, ilpD3DDevice2
, lpHandle
);
326 /* For 32 bits OSes, handles = pointers */
327 *lpHandle
= (D3DTEXTUREHANDLE
) This
;
329 /* Now, bind a new texture */
331 ilpD3DDevice2
->set_context(ilpD3DDevice2
);
332 This
->D3Ddevice
= (void *) ilpD3DDevice2
;
333 if (dtpriv
->tex_name
== 0)
334 glGenTextures(1, &(dtpriv
->tex_name
));
337 TRACE("OpenGL texture handle is : %d\n", dtpriv
->tex_name
);
343 HRESULT WINAPI
IDirect3DTexture2Impl_PaletteChanged(
344 LPDIRECT3DTEXTURE2 iface
, DWORD dwStart
, DWORD dwCount
346 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
347 FIXME("(%p)->(%8ld,%8ld): stub\n", This
, dwStart
, dwCount
);
352 /* NOTE : if you experience crashes in this function, you must have a buggy
353 version of Mesa. See the file d3dtexture.c for a cure */
354 HRESULT WINAPI
IDirect3DTexture2Impl_Load(
355 LPDIRECT3DTEXTURE2 iface
, LPDIRECT3DTEXTURE2 lpD3DTexture2
357 ICOM_THIS(IDirect3DTexture2Impl
,iface
);
359 IDirect3DTexture2Impl
* ilpD3DTexture2
=(IDirect3DTexture2Impl
*)lpD3DTexture2
;
360 DDSURFACEDESC
*src_d
, *dst_d
;
361 static void (*ptr_ColorTableEXT
) (GLenum target
, GLenum internalformat
,
362 GLsizei width
, GLenum format
, GLenum type
, const GLvoid
*table
) = NULL
;
364 static BOOL color_table_queried
= FALSE
;
367 TRACE("(%p)->(%p)\n", This
, ilpD3DTexture2
);
368 TRACE("Copied surface %p to surface %p\n", ilpD3DTexture2
->surface
, This
->surface
);
370 /* Suppress the ALLOCONLOAD flag */
371 This
->surface
->surface_desc
.ddsCaps
.dwCaps
&= ~DDSCAPS_ALLOCONLOAD
;
373 /* Copy one surface on the other */
374 dst_d
= (DDSURFACEDESC
*)&(This
->surface
->surface_desc
);
375 src_d
= (DDSURFACEDESC
*)&(ilpD3DTexture2
->surface
->surface_desc
);
377 /* Install the callbacks to the destination surface */
378 This
->surface
->texture
= This
;
379 This
->surface
->SetColorKey_cb
= SetColorKey_cb
;
381 if ((src_d
->dwWidth
!= dst_d
->dwWidth
) || (src_d
->dwHeight
!= dst_d
->dwHeight
)) {
382 /* Should also check for same pixel format, lPitch, ... */
383 ERR("Error in surface sizes\n");
384 return D3DERR_TEXTURE_LOAD_FAILED
;
386 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
387 /* I should put a macro for the calculus of bpp */
388 int bpp
= (src_d
->ddpfPixelFormat
.dwFlags
& DDPF_PALETTEINDEXED8
?
389 1 /* 8 bit of palette index */:
390 src_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
/ 8 /* RGB bits for each colors */ );
391 GLuint current_texture
;
393 /* Copy the main memry texture into the surface that corresponds to the OpenGL
395 memcpy(dst_d
->lpSurface
, src_d
->lpSurface
, src_d
->dwWidth
* src_d
->dwHeight
* bpp
);
399 /* Now, load the texture */
400 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
401 glGetIntegerv(GL_TEXTURE_BINDING_2D
, ¤t_texture
);
403 /* If the GetHandle was not done, get the texture name here */
404 if (dtpriv
->tex_name
== 0)
405 glGenTextures(1, &(dtpriv
->tex_name
));
406 glBindTexture(GL_TEXTURE_2D
, dtpriv
->tex_name
);
408 if (src_d
->ddpfPixelFormat
.dwFlags
& DDPF_PALETTEINDEXED8
) {
412 IDirectDrawPaletteImpl
* pal
= This
->surface
->palette
;
417 if (color_table_queried
== FALSE
) {
419 ((Mesa_DeviceCapabilities
*) ((x11_dd_private
*) This
->surface
->s
.ddraw
->d
->private)->device_capabilities
)->ptr_ColorTableEXT
;
424 ERR("Palettized texture Loading with a NULL palette !\n");
426 return D3DERR_TEXTURE_LOAD_FAILED
;
429 /* Get the surface's palette */
430 for (i
= 0; i
< 256; i
++) {
431 table
[i
][0] = pal
->palents
[i
].peRed
;
432 table
[i
][1] = pal
->palents
[i
].peGreen
;
433 table
[i
][2] = pal
->palents
[i
].peBlue
;
434 if ((This
->surface
->surface_desc
.dwFlags
& DDSD_CKSRCBLT
) &&
435 (i
>= This
->surface
->surface_desc
.ddckCKSrcBlt
.dwColorSpaceLowValue
) &&
436 (i
<= This
->surface
->surface_desc
.ddckCKSrcBlt
.dwColorSpaceHighValue
))
442 /* Texture snooping */
445 if (ptr_ColorTableEXT
!= NULL
) {
446 /* use Paletted Texture Extension */
447 ptr_ColorTableEXT(GL_TEXTURE_2D
, /* target */
448 GL_RGBA
, /* internal format */
449 256, /* table size */
450 GL_RGBA
, /* table format */
451 GL_UNSIGNED_BYTE
, /* table type */
452 table
); /* the color table */
454 glTexImage2D(GL_TEXTURE_2D
, /* target */
456 GL_COLOR_INDEX8_EXT
, /* internal format */
457 src_d
->dwWidth
, src_d
->dwHeight
, /* width, height */
459 GL_COLOR_INDEX
, /* texture format */
460 GL_UNSIGNED_BYTE
, /* texture type */
461 src_d
->lpSurface
); /* the texture */
463 DWORD
*surface
= (DWORD
*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, src_d
->dwWidth
* src_d
->dwHeight
* sizeof(DWORD
));
465 BYTE
*src
= (BYTE
*) src_d
->lpSurface
, *dst
= (BYTE
*) surface
;
467 for (i
= 0; i
< src_d
->dwHeight
* src_d
->dwWidth
; i
++) {
469 *dst
++ = table
[color
][0];
470 *dst
++ = table
[color
][1];
471 *dst
++ = table
[color
][2];
472 *dst
++ = table
[color
][3];
475 glTexImage2D(GL_TEXTURE_2D
,
478 src_d
->dwWidth
, src_d
->dwHeight
,
484 HeapFree(GetProcessHeap(), 0, surface
);
486 } else if (src_d
->ddpfPixelFormat
.dwFlags
& DDPF_RGB
) {
490 if (src_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
== 8) {
491 /* **********************
492 GL_UNSIGNED_BYTE_3_3_2
493 ********************** */
494 glTexImage2D(GL_TEXTURE_2D
,
497 src_d
->dwWidth
, src_d
->dwHeight
,
500 GL_UNSIGNED_BYTE_3_3_2
,
502 } else if (src_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
== 16) {
503 if (src_d
->ddpfPixelFormat
.u5
.dwRGBAlphaBitMask
== 0x00000000) {
505 /* Texture snooping */
508 glTexImage2D(GL_TEXTURE_2D
,
511 src_d
->dwWidth
, src_d
->dwHeight
,
514 GL_UNSIGNED_SHORT_5_6_5
,
516 } else if (src_d
->ddpfPixelFormat
.u5
.dwRGBAlphaBitMask
== 0x00000001) {
517 /* Texture snooping */
520 glTexImage2D(GL_TEXTURE_2D
,
523 src_d
->dwWidth
, src_d
->dwHeight
,
526 GL_UNSIGNED_SHORT_5_5_5_1
,
528 } else if (src_d
->ddpfPixelFormat
.u5
.dwRGBAlphaBitMask
== 0x0000000F) {
529 glTexImage2D(GL_TEXTURE_2D
,
532 src_d
->dwWidth
, src_d
->dwHeight
,
535 GL_UNSIGNED_SHORT_4_4_4_4
,
537 } else if (src_d
->ddpfPixelFormat
.u5
.dwRGBAlphaBitMask
== 0x00008000) {
538 /* Converting the 1555 format in 5551 packed */
539 WORD
*surface
= (WORD
*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, src_d
->dwWidth
* src_d
->dwHeight
* sizeof(WORD
));
541 WORD
*src
= (WORD
*) src_d
->lpSurface
, *dst
= surface
;
543 for (i
= 0; i
< src_d
->dwHeight
* src_d
->dwWidth
; i
++) {
544 *dst
++ = (((*src
& 0x8000) >> 15) |
545 ((*src
& 0x7FFF) << 1));
549 glTexImage2D(GL_TEXTURE_2D
,
552 src_d
->dwWidth
, src_d
->dwHeight
,
555 GL_UNSIGNED_SHORT_5_5_5_1
,
558 HeapFree(GetProcessHeap(), 0, surface
);
560 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
562 } else if (src_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
== 24) {
563 glTexImage2D(GL_TEXTURE_2D
,
566 src_d
->dwWidth
, src_d
->dwHeight
,
571 } else if (src_d
->ddpfPixelFormat
.u1
.dwRGBBitCount
== 32) {
572 glTexImage2D(GL_TEXTURE_2D
,
575 src_d
->dwWidth
, src_d
->dwHeight
,
581 ERR("Unhandled texture format (bad RGB count)\n");
584 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
587 glBindTexture(GL_TEXTURE_2D
, current_texture
);
596 /*******************************************************************************
597 * IDirect3DTexture2 VTable
599 ICOM_VTABLE(IDirect3DTexture2
) mesa_texture2_vtable
=
601 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
602 /*** IUnknown methods ***/
603 IDirect3DTexture2Impl_QueryInterface
,
604 IDirect3DTexture2Impl_AddRef
,
605 IDirect3DTexture2Impl_Release
,
606 /*** IDirect3DTexture methods ***/
607 IDirect3DTexture2Impl_GetHandle
,
608 IDirect3DTexture2Impl_PaletteChanged
,
609 IDirect3DTexture2Impl_Load
612 /*******************************************************************************
613 * IDirect3DTexture VTable
615 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
616 # define XCAST(fun) (typeof(mesa_texture_vtable.fun))
618 # define XCAST(fun) (void*)
621 ICOM_VTABLE(IDirect3DTexture
) mesa_texture_vtable
=
623 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
624 /*** IUnknown methods ***/
625 XCAST(QueryInterface
)IDirect3DTexture2Impl_QueryInterface
,
626 XCAST(AddRef
)IDirect3DTexture2Impl_AddRef
,
627 XCAST(Release
)IDirect3DTexture2Impl_Release
,
628 /*** IDirect3DTexture methods ***/
629 IDirect3DTextureImpl_Initialize
,
630 IDirect3DTextureImpl_GetHandle
,
631 XCAST(PaletteChanged
)IDirect3DTexture2Impl_PaletteChanged
,
632 XCAST(Load
)IDirect3DTexture2Impl_Load
,
633 IDirect3DTextureImpl_Unload
636 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)