4 This files contains the implementation of interface Direct3DTexture2. */
11 #include "wine/obj_base.h"
14 #include "debugtools.h"
16 #include "mesa_private.h"
18 #define D3DDPRIVATE(x) mesa_d3dd_private*odev=(mesa_d3dd_private*)(x)->private
19 #define D3DTPRIVATE(x) mesa_d3dt_private*dtpriv=(mesa_d3dt_private*)(x)->private
21 DEFAULT_DEBUG_CHANNEL(ddraw);
23 /* Define this if you want to save to a file all the textures used by a game
24 (can be funny to see how they managed to cram all the pictures in
31 #define SNOOP_PALETTED() \
37 sprintf(buf, "%ld.pnm", dtpriv->tex_name); \
38 f = fopen(buf, "wb"); \
39 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
40 for (y = 0; y < src_d->dwHeight; y++) { \
41 for (x = 0; x < src_d->dwWidth; x++) { \
42 unsigned char c = ((unsigned char *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
43 fputc(table[c][0], f); \
44 fputc(table[c][1], f); \
45 fputc(table[c][2], f); \
51 #define SNOOP_5650() \
57 sprintf(buf, "%ld.pnm", dtpriv->tex_name); \
58 f = fopen(buf, "wb"); \
59 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
60 for (y = 0; y < src_d->dwHeight; y++) { \
61 for (x = 0; x < src_d->dwWidth; x++) { \
62 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
63 fputc((c & 0xF800) >> 8, f); \
64 fputc((c & 0x07E0) >> 3, f); \
65 fputc((c & 0x001F) << 3, f); \
71 #define SNOOP_5551() \
77 sprintf(buf, "%ld.pnm", dtpriv->tex_name); \
78 f = fopen(buf, "wb"); \
79 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
80 for (y = 0; y < src_d->dwHeight; y++) { \
81 for (x = 0; x < src_d->dwWidth; x++) { \
82 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
83 fputc((c & 0xF800) >> 8, f); \
84 fputc((c & 0x07C0) >> 3, f); \
85 fputc((c & 0x003E) << 2, f); \
91 #define SNOOP_PALETTED()
96 extern ICOM_VTABLE(IDirect3DTexture2) mesa_texture2_vtable;
97 extern ICOM_VTABLE(IDirect3DTexture) mesa_texture_vtable;
99 /*******************************************************************************
100 * Texture2 Creation functions
102 LPDIRECT3DTEXTURE2 d3dtexture2_create(IDirectDrawSurfaceImpl* surf)
104 IDirect3DTexture2Impl* tex;
106 tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
108 ICOM_VTBL(tex) = &mesa_texture2_vtable;
111 tex->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(mesa_d3dt_private));
113 return (LPDIRECT3DTEXTURE2)tex;
116 /*******************************************************************************
117 * Texture Creation functions
119 LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurfaceImpl* surf)
121 IDirect3DTexture2Impl* tex;
123 tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
125 ICOM_VTBL(tex) = (ICOM_VTABLE(IDirect3DTexture2)*)&mesa_texture_vtable;
128 tex->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(mesa_d3dt_private));
130 return (LPDIRECT3DTEXTURE)tex;
133 /*******************************************************************************
134 * IDirectSurface callback methods
136 HRESULT WINAPI SetColorKey_cb(IDirect3DTexture2Impl *texture, DWORD dwFlags, LPDDCOLORKEY ckey )
138 DDSURFACEDESC *tex_d;
139 D3DTPRIVATE(texture);
141 GLuint current_texture;
143 TRACE("(%p) : colorkey callback\n", texture);
145 /* Get the texture description */
146 tex_d = (DDSURFACEDESC *)&(texture->surface->surface_desc);
147 bpp = (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
148 1 /* 8 bit of palette index */:
149 tex_d->ddpfPixelFormat.u1.dwRGBBitCount / 8 /* RGB bits for each colors */ );
151 /* Now, save the current texture */
153 glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
155 /* If the GetHandle was not done yet, it's an error */
156 if (dtpriv->tex_name == 0) {
157 ERR("Unloaded texture !\n");
161 glBindTexture(GL_TEXTURE_2D, dtpriv->tex_name);
163 if (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
164 FIXME("Todo Paletted\n");
165 } else if (tex_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
166 if (tex_d->ddpfPixelFormat.u1.dwRGBBitCount == 8) {
167 FIXME("Todo 3_3_2_0\n");
168 } else if (tex_d->ddpfPixelFormat.u1.dwRGBBitCount == 16) {
169 if (tex_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000000) {
170 /* Now transform the 5_6_5 into a 5_5_5_1 surface to support color keying */
171 unsigned short *dest = (unsigned short *) HeapAlloc(GetProcessHeap(),
173 tex_d->dwWidth * tex_d->dwHeight * bpp);
174 unsigned short *src = (unsigned short *) tex_d->lpSurface;
177 for (y = 0; y < tex_d->dwHeight; y++) {
178 for (x = 0; x < tex_d->dwWidth; x++) {
179 unsigned short cpixel = src[x + y * tex_d->dwWidth];
181 if ((dwFlags & DDCKEY_SRCBLT) &&
182 (cpixel >= ckey->dwColorSpaceLowValue) &&
183 (cpixel <= ckey->dwColorSpaceHighValue)) /* No alpha bit => this pixel is transparent */
184 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0000;
185 else /* Alpha bit is set => this pixel will be seen */
186 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0001;
190 glTexImage2D(GL_TEXTURE_2D,
193 tex_d->dwWidth, tex_d->dwHeight,
196 GL_UNSIGNED_SHORT_5_5_5_1,
199 /* Frees the temporary surface */
200 HeapFree(GetProcessHeap(),0,dest);
201 } else if (tex_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000001) {
202 FIXME("Todo 5_5_5_1\n");
203 } else if (tex_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x0000000F) {
204 FIXME("Todo 4_4_4_4\n");
206 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
208 } else if (tex_d->ddpfPixelFormat.u1.dwRGBBitCount == 24) {
209 FIXME("Todo 8_8_8_0\n");
210 } else if (tex_d->ddpfPixelFormat.u1.dwRGBBitCount == 32) {
211 FIXME("Todo 8_8_8_8\n");
213 ERR("Unhandled texture format (bad RGB count)\n");
216 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
223 /*******************************************************************************
224 * IDirect3DTexture2 methods
227 HRESULT WINAPI IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface,
231 ICOM_THIS(IDirect3DTexture2Impl,iface);
233 FIXME("(%p)->(%s,%p): stub\n", This, debugstr_guid(riid),ppvObj);
240 ULONG WINAPI IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface)
242 ICOM_THIS(IDirect3DTexture2Impl,iface);
243 TRACE("(%p)->()incrementing from %lu.\n", This, This->ref );
245 return ++(This->ref);
250 ULONG WINAPI IDirect3DTexture2Impl_Release(LPDIRECT3DTEXTURE2 iface)
252 ICOM_THIS(IDirect3DTexture2Impl,iface);
254 FIXME("(%p)->() decrementing from %lu.\n", This, This->ref );
256 if (!--(This->ref)) {
257 /* Delete texture from OpenGL */
259 glDeleteTextures(1, &(dtpriv->tex_name));
262 /* Release surface */
263 IDirectDrawSurface4_Release((IDirectDrawSurface4*)This->surface);
265 HeapFree(GetProcessHeap(),0,This);
272 /*** IDirect3DTexture methods ***/
273 HRESULT WINAPI IDirect3DTextureImpl_GetHandle(LPDIRECT3DTEXTURE iface,
274 LPDIRECT3DDEVICE lpD3DDevice,
275 LPD3DTEXTUREHANDLE lpHandle)
277 ICOM_THIS(IDirect3DTexture2Impl,iface);
279 IDirect3DDeviceImpl* ilpD3DDevice=(IDirect3DDeviceImpl*)lpD3DDevice;
280 FIXME("(%p)->(%p,%p): stub\n", This, ilpD3DDevice, lpHandle);
282 *lpHandle = (D3DTEXTUREHANDLE) This;
284 /* Now, bind a new texture */
286 ilpD3DDevice->set_context(ilpD3DDevice);
287 This->D3Ddevice = (void *) ilpD3DDevice;
288 if (dtpriv->tex_name == 0)
289 glGenTextures(1, &(dtpriv->tex_name));
292 TRACE("OpenGL texture handle is : %d\n", dtpriv->tex_name);
297 HRESULT WINAPI IDirect3DTextureImpl_Initialize(LPDIRECT3DTEXTURE iface,
298 LPDIRECT3DDEVICE lpD3DDevice,
299 LPDIRECTDRAWSURFACE lpSurface)
301 ICOM_THIS(IDirect3DTexture2Impl,iface);
302 TRACE("(%p)->(%p,%p)\n", This, lpD3DDevice, lpSurface);
304 return DDERR_ALREADYINITIALIZED;
307 HRESULT WINAPI IDirect3DTextureImpl_Unload(LPDIRECT3DTEXTURE iface)
309 ICOM_THIS(IDirect3DTexture2Impl,iface);
310 FIXME("(%p)->(): stub\n", This);
315 /*** IDirect3DTexture2 methods ***/
316 HRESULT WINAPI IDirect3DTexture2Impl_GetHandle(LPDIRECT3DTEXTURE2 iface,
317 LPDIRECT3DDEVICE2 lpD3DDevice2,
318 LPD3DTEXTUREHANDLE lpHandle)
320 ICOM_THIS(IDirect3DTexture2Impl,iface);
322 IDirect3DDevice2Impl* ilpD3DDevice2=(IDirect3DDevice2Impl*)lpD3DDevice2;
323 TRACE("(%p)->(%p,%p)\n", This, ilpD3DDevice2, lpHandle);
325 /* For 32 bits OSes, handles = pointers */
326 *lpHandle = (D3DTEXTUREHANDLE) This;
328 /* Now, bind a new texture */
330 ilpD3DDevice2->set_context(ilpD3DDevice2);
331 This->D3Ddevice = (void *) ilpD3DDevice2;
332 if (dtpriv->tex_name == 0)
333 glGenTextures(1, &(dtpriv->tex_name));
336 TRACE("OpenGL texture handle is : %d\n", dtpriv->tex_name);
342 HRESULT WINAPI IDirect3DTexture2Impl_PaletteChanged(
343 LPDIRECT3DTEXTURE2 iface, DWORD dwStart, DWORD dwCount
345 ICOM_THIS(IDirect3DTexture2Impl,iface);
346 FIXME("(%p)->(%8ld,%8ld): stub\n", This, dwStart, dwCount);
351 /* NOTE : if you experience crashes in this function, you must have a buggy
352 version of Mesa. See the file d3dtexture.c for a cure */
353 HRESULT WINAPI IDirect3DTexture2Impl_Load(
354 LPDIRECT3DTEXTURE2 iface, LPDIRECT3DTEXTURE2 lpD3DTexture2
356 ICOM_THIS(IDirect3DTexture2Impl,iface);
358 IDirect3DTexture2Impl* ilpD3DTexture2=(IDirect3DTexture2Impl*)lpD3DTexture2;
359 DDSURFACEDESC *src_d, *dst_d;
360 static void (*ptr_ColorTableEXT) (GLenum target, GLenum internalformat,
361 GLsizei width, GLenum format, GLenum type, const GLvoid *table) = NULL;
363 static BOOL color_table_queried = FALSE;
366 TRACE("(%p)->(%p)\n", This, ilpD3DTexture2);
367 TRACE("Copied surface %p to surface %p\n", ilpD3DTexture2->surface, This->surface);
369 /* Suppress the ALLOCONLOAD flag */
370 This->surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
372 /* Copy one surface on the other */
373 dst_d = (DDSURFACEDESC *)&(This->surface->surface_desc);
374 src_d = (DDSURFACEDESC *)&(ilpD3DTexture2->surface->surface_desc);
376 /* Install the callbacks to the destination surface */
377 This->surface->texture = This;
378 This->surface->SetColorKey_cb = SetColorKey_cb;
380 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
381 /* Should also check for same pixel format, lPitch, ... */
382 ERR("Error in surface sizes\n");
383 return D3DERR_TEXTURE_LOAD_FAILED;
385 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
386 /* I should put a macro for the calculus of bpp */
387 int bpp = (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
388 1 /* 8 bit of palette index */:
389 src_d->ddpfPixelFormat.u1.dwRGBBitCount / 8 /* RGB bits for each colors */ );
390 GLuint current_texture;
392 /* Copy the main memry texture into the surface that corresponds to the OpenGL
394 memcpy(dst_d->lpSurface, src_d->lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
398 /* Now, load the texture */
399 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
400 glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
402 /* If the GetHandle was not done, get the texture name here */
403 if (dtpriv->tex_name == 0)
404 glGenTextures(1, &(dtpriv->tex_name));
405 glBindTexture(GL_TEXTURE_2D, dtpriv->tex_name);
407 if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
411 IDirectDrawPaletteImpl* pal = This->surface->palette;
416 if (color_table_queried == FALSE) {
418 ((Mesa_DeviceCapabilities *) ((x11_dd_private *) This->surface->s.ddraw->d->private)->device_capabilities)->ptr_ColorTableEXT;
423 ERR("Palettized texture Loading with a NULL palette !\n");
425 return D3DERR_TEXTURE_LOAD_FAILED;
428 /* Get the surface's palette */
429 for (i = 0; i < 256; i++) {
430 table[i][0] = pal->palents[i].peRed;
431 table[i][1] = pal->palents[i].peGreen;
432 table[i][2] = pal->palents[i].peBlue;
433 if ((This->surface->surface_desc.dwFlags & DDSD_CKSRCBLT) &&
434 (i >= This->surface->surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue) &&
435 (i <= This->surface->surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue))
441 /* Texture snooping */
444 if (ptr_ColorTableEXT != NULL) {
445 /* use Paletted Texture Extension */
446 ptr_ColorTableEXT(GL_TEXTURE_2D, /* target */
447 GL_RGBA, /* internal format */
448 256, /* table size */
449 GL_RGBA, /* table format */
450 GL_UNSIGNED_BYTE, /* table type */
451 table); /* the color table */
453 glTexImage2D(GL_TEXTURE_2D, /* target */
455 GL_COLOR_INDEX8_EXT, /* internal format */
456 src_d->dwWidth, src_d->dwHeight, /* width, height */
458 GL_COLOR_INDEX, /* texture format */
459 GL_UNSIGNED_BYTE, /* texture type */
460 src_d->lpSurface); /* the texture */
462 DWORD *surface = (DWORD *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, src_d->dwWidth * src_d->dwHeight * sizeof(DWORD));
464 BYTE *src = (BYTE *) src_d->lpSurface, *dst = (BYTE *) surface;
466 for (i = 0; i < src_d->dwHeight * src_d->dwWidth; i++) {
468 *dst++ = table[color][0];
469 *dst++ = table[color][1];
470 *dst++ = table[color][2];
471 *dst++ = table[color][3];
474 glTexImage2D(GL_TEXTURE_2D,
477 src_d->dwWidth, src_d->dwHeight,
483 HeapFree(GetProcessHeap(), 0, surface);
485 } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
489 if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 8) {
490 /* **********************
491 GL_UNSIGNED_BYTE_3_3_2
492 ********************** */
493 glTexImage2D(GL_TEXTURE_2D,
496 src_d->dwWidth, src_d->dwHeight,
499 GL_UNSIGNED_BYTE_3_3_2,
501 } else if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 16) {
502 if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000000) {
504 /* Texture snooping */
507 glTexImage2D(GL_TEXTURE_2D,
510 src_d->dwWidth, src_d->dwHeight,
513 GL_UNSIGNED_SHORT_5_6_5,
515 } else if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000001) {
516 /* Texture snooping */
519 glTexImage2D(GL_TEXTURE_2D,
522 src_d->dwWidth, src_d->dwHeight,
525 GL_UNSIGNED_SHORT_5_5_5_1,
527 } else if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x0000000F) {
528 glTexImage2D(GL_TEXTURE_2D,
531 src_d->dwWidth, src_d->dwHeight,
534 GL_UNSIGNED_SHORT_4_4_4_4,
536 } else if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00008000) {
537 /* Converting the 1555 format in 5551 packed */
538 WORD *surface = (WORD *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, src_d->dwWidth * src_d->dwHeight * sizeof(WORD));
540 WORD *src = (WORD *) src_d->lpSurface, *dst = surface;
542 for (i = 0; i < src_d->dwHeight * src_d->dwWidth; i++) {
543 *dst++ = (((*src & 0x8000) >> 15) |
544 ((*src & 0x7FFF) << 1));
548 glTexImage2D(GL_TEXTURE_2D,
551 src_d->dwWidth, src_d->dwHeight,
554 GL_UNSIGNED_SHORT_5_5_5_1,
557 HeapFree(GetProcessHeap(), 0, surface);
559 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
561 } else if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 24) {
562 glTexImage2D(GL_TEXTURE_2D,
565 src_d->dwWidth, src_d->dwHeight,
570 } else if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 32) {
571 glTexImage2D(GL_TEXTURE_2D,
574 src_d->dwWidth, src_d->dwHeight,
580 ERR("Unhandled texture format (bad RGB count)\n");
583 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
586 glBindTexture(GL_TEXTURE_2D, current_texture);
595 /*******************************************************************************
596 * IDirect3DTexture2 VTable
598 ICOM_VTABLE(IDirect3DTexture2) mesa_texture2_vtable =
600 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
601 /*** IUnknown methods ***/
602 IDirect3DTexture2Impl_QueryInterface,
603 IDirect3DTexture2Impl_AddRef,
604 IDirect3DTexture2Impl_Release,
605 /*** IDirect3DTexture methods ***/
606 IDirect3DTexture2Impl_GetHandle,
607 IDirect3DTexture2Impl_PaletteChanged,
608 IDirect3DTexture2Impl_Load
611 /*******************************************************************************
612 * IDirect3DTexture VTable
614 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
615 # define XCAST(fun) (typeof(mesa_texture_vtable.fun))
617 # define XCAST(fun) (void*)
620 ICOM_VTABLE(IDirect3DTexture) mesa_texture_vtable =
622 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
623 /*** IUnknown methods ***/
624 XCAST(QueryInterface)IDirect3DTexture2Impl_QueryInterface,
625 XCAST(AddRef)IDirect3DTexture2Impl_AddRef,
626 XCAST(Release)IDirect3DTexture2Impl_Release,
627 /*** IDirect3DTexture methods ***/
628 IDirect3DTextureImpl_Initialize,
629 IDirect3DTextureImpl_GetHandle,
630 XCAST(PaletteChanged)IDirect3DTexture2Impl_PaletteChanged,
631 XCAST(Load)IDirect3DTexture2Impl_Load,
632 IDirect3DTextureImpl_Unload
635 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)