2 * Copyright (c) 1998 Lionel ULMER
4 * This file contains the implementation of interface Direct3DTexture2.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/obj_base.h"
30 #include "wine/debug.h"
32 #include "mesa_private.h"
34 #define D3DDPRIVATE(x) mesa_d3dd_private*odev=(mesa_d3dd_private*)(x)->private
35 #define D3DTPRIVATE(x) mesa_d3dt_private*dtpriv=(mesa_d3dt_private*)(x)->private
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 /* Define this if you want to save to a file all the textures used by a game
40 (can be funny to see how they managed to cram all the pictures in
47 #define SNOOP_PALETTED() \
53 sprintf(buf, "%ld.pnm", dtpriv->tex_name); \
54 f = fopen(buf, "wb"); \
55 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
56 for (y = 0; y < src_d->dwHeight; y++) { \
57 for (x = 0; x < src_d->dwWidth; x++) { \
58 unsigned char c = ((unsigned char *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
59 fputc(table[c][0], f); \
60 fputc(table[c][1], f); \
61 fputc(table[c][2], f); \
67 #define SNOOP_5650() \
73 sprintf(buf, "%ld.pnm", dtpriv->tex_name); \
74 f = fopen(buf, "wb"); \
75 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
76 for (y = 0; y < src_d->dwHeight; y++) { \
77 for (x = 0; x < src_d->dwWidth; x++) { \
78 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
79 fputc((c & 0xF800) >> 8, f); \
80 fputc((c & 0x07E0) >> 3, f); \
81 fputc((c & 0x001F) << 3, f); \
87 #define SNOOP_5551() \
93 sprintf(buf, "%ld.pnm", dtpriv->tex_name); \
94 f = fopen(buf, "wb"); \
95 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
96 for (y = 0; y < src_d->dwHeight; y++) { \
97 for (x = 0; x < src_d->dwWidth; x++) { \
98 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
99 fputc((c & 0xF800) >> 8, f); \
100 fputc((c & 0x07C0) >> 3, f); \
101 fputc((c & 0x003E) << 2, f); \
107 #define SNOOP_PALETTED()
112 extern ICOM_VTABLE(IDirect3DTexture2) mesa_texture2_vtable;
113 extern ICOM_VTABLE(IDirect3DTexture) mesa_texture_vtable;
115 /*******************************************************************************
116 * Texture2 Creation functions
118 LPDIRECT3DTEXTURE2 d3dtexture2_create(IDirectDrawSurfaceImpl* surf)
120 IDirect3DTexture2Impl* tex;
122 tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
124 ICOM_VTBL(tex) = &mesa_texture2_vtable;
127 tex->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(mesa_d3dt_private));
129 return (LPDIRECT3DTEXTURE2)tex;
132 /*******************************************************************************
133 * Texture Creation functions
135 LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurfaceImpl* surf)
137 IDirect3DTexture2Impl* tex;
139 tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
141 ICOM_VTBL(tex) = (ICOM_VTABLE(IDirect3DTexture2)*)&mesa_texture_vtable;
144 tex->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(mesa_d3dt_private));
146 return (LPDIRECT3DTEXTURE)tex;
149 /*******************************************************************************
150 * IDirectSurface callback methods
152 HRESULT WINAPI SetColorKey_cb(IDirect3DTexture2Impl *texture, DWORD dwFlags, LPDDCOLORKEY ckey )
154 DDSURFACEDESC *tex_d;
155 D3DTPRIVATE(texture);
157 GLuint current_texture;
159 TRACE("(%p) : colorkey callback\n", texture);
161 /* Get the texture description */
162 tex_d = (DDSURFACEDESC *)&(texture->surface->surface_desc);
163 bpp = (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
164 1 /* 8 bit of palette index */:
165 tex_d->ddpfPixelFormat.u1.dwRGBBitCount / 8 /* RGB bits for each colors */ );
167 /* Now, save the current texture */
169 glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
171 /* If the GetHandle was not done yet, it's an error */
172 if (dtpriv->tex_name == 0) {
173 ERR("Unloaded texture !\n");
177 glBindTexture(GL_TEXTURE_2D, dtpriv->tex_name);
179 if (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
180 FIXME("Todo Paletted\n");
181 } else if (tex_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
182 if (tex_d->ddpfPixelFormat.u1.dwRGBBitCount == 8) {
183 FIXME("Todo 3_3_2_0\n");
184 } else if (tex_d->ddpfPixelFormat.u1.dwRGBBitCount == 16) {
185 if (tex_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000000) {
186 /* Now transform the 5_6_5 into a 5_5_5_1 surface to support color keying */
187 unsigned short *dest = (unsigned short *) HeapAlloc(GetProcessHeap(),
189 tex_d->dwWidth * tex_d->dwHeight * bpp);
190 unsigned short *src = (unsigned short *) tex_d->lpSurface;
193 for (y = 0; y < tex_d->dwHeight; y++) {
194 for (x = 0; x < tex_d->dwWidth; x++) {
195 unsigned short cpixel = src[x + y * tex_d->dwWidth];
197 if ((dwFlags & DDCKEY_SRCBLT) &&
198 (cpixel >= ckey->dwColorSpaceLowValue) &&
199 (cpixel <= ckey->dwColorSpaceHighValue)) /* No alpha bit => this pixel is transparent */
200 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0000;
201 else /* Alpha bit is set => this pixel will be seen */
202 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0001;
206 glTexImage2D(GL_TEXTURE_2D,
209 tex_d->dwWidth, tex_d->dwHeight,
212 GL_UNSIGNED_SHORT_5_5_5_1,
215 /* Frees the temporary surface */
216 HeapFree(GetProcessHeap(),0,dest);
217 } else if (tex_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000001) {
218 FIXME("Todo 5_5_5_1\n");
219 } else if (tex_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x0000000F) {
220 FIXME("Todo 4_4_4_4\n");
222 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
224 } else if (tex_d->ddpfPixelFormat.u1.dwRGBBitCount == 24) {
225 FIXME("Todo 8_8_8_0\n");
226 } else if (tex_d->ddpfPixelFormat.u1.dwRGBBitCount == 32) {
227 FIXME("Todo 8_8_8_8\n");
229 ERR("Unhandled texture format (bad RGB count)\n");
232 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
239 /*******************************************************************************
240 * IDirect3DTexture2 methods
243 HRESULT WINAPI IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface,
247 ICOM_THIS(IDirect3DTexture2Impl,iface);
249 FIXME("(%p)->(%s,%p): stub\n", This, debugstr_guid(riid),ppvObj);
256 ULONG WINAPI IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface)
258 ICOM_THIS(IDirect3DTexture2Impl,iface);
259 TRACE("(%p)->()incrementing from %lu.\n", This, This->ref );
261 return ++(This->ref);
266 ULONG WINAPI IDirect3DTexture2Impl_Release(LPDIRECT3DTEXTURE2 iface)
268 ICOM_THIS(IDirect3DTexture2Impl,iface);
270 FIXME("(%p)->() decrementing from %lu.\n", This, This->ref );
272 if (!--(This->ref)) {
273 /* Delete texture from OpenGL */
275 glDeleteTextures(1, &(dtpriv->tex_name));
278 /* Release surface */
279 IDirectDrawSurface4_Release((IDirectDrawSurface4*)This->surface);
281 HeapFree(GetProcessHeap(),0,This);
288 /*** IDirect3DTexture methods ***/
289 HRESULT WINAPI IDirect3DTextureImpl_GetHandle(LPDIRECT3DTEXTURE iface,
290 LPDIRECT3DDEVICE lpD3DDevice,
291 LPD3DTEXTUREHANDLE lpHandle)
293 ICOM_THIS(IDirect3DTexture2Impl,iface);
295 IDirect3DDeviceImpl* ilpD3DDevice=(IDirect3DDeviceImpl*)lpD3DDevice;
296 FIXME("(%p)->(%p,%p): stub\n", This, ilpD3DDevice, lpHandle);
298 *lpHandle = (D3DTEXTUREHANDLE) This;
300 /* Now, bind a new texture */
302 ilpD3DDevice->set_context(ilpD3DDevice);
303 This->D3Ddevice = (void *) ilpD3DDevice;
304 if (dtpriv->tex_name == 0)
305 glGenTextures(1, &(dtpriv->tex_name));
308 TRACE("OpenGL texture handle is : %d\n", dtpriv->tex_name);
313 HRESULT WINAPI IDirect3DTextureImpl_Initialize(LPDIRECT3DTEXTURE iface,
314 LPDIRECT3DDEVICE lpD3DDevice,
315 LPDIRECTDRAWSURFACE lpSurface)
317 ICOM_THIS(IDirect3DTexture2Impl,iface);
318 TRACE("(%p)->(%p,%p)\n", This, lpD3DDevice, lpSurface);
320 return DDERR_ALREADYINITIALIZED;
323 HRESULT WINAPI IDirect3DTextureImpl_Unload(LPDIRECT3DTEXTURE iface)
325 ICOM_THIS(IDirect3DTexture2Impl,iface);
326 FIXME("(%p)->(): stub\n", This);
331 /*** IDirect3DTexture2 methods ***/
332 HRESULT WINAPI IDirect3DTexture2Impl_GetHandle(LPDIRECT3DTEXTURE2 iface,
333 LPDIRECT3DDEVICE2 lpD3DDevice2,
334 LPD3DTEXTUREHANDLE lpHandle)
336 ICOM_THIS(IDirect3DTexture2Impl,iface);
338 IDirect3DDevice2Impl* ilpD3DDevice2=(IDirect3DDevice2Impl*)lpD3DDevice2;
339 TRACE("(%p)->(%p,%p)\n", This, ilpD3DDevice2, lpHandle);
341 /* For 32 bits OSes, handles = pointers */
342 *lpHandle = (D3DTEXTUREHANDLE) This;
344 /* Now, bind a new texture */
346 ilpD3DDevice2->set_context(ilpD3DDevice2);
347 This->D3Ddevice = (void *) ilpD3DDevice2;
348 if (dtpriv->tex_name == 0)
349 glGenTextures(1, &(dtpriv->tex_name));
352 TRACE("OpenGL texture handle is : %d\n", dtpriv->tex_name);
358 HRESULT WINAPI IDirect3DTexture2Impl_PaletteChanged(
359 LPDIRECT3DTEXTURE2 iface, DWORD dwStart, DWORD dwCount
361 ICOM_THIS(IDirect3DTexture2Impl,iface);
362 FIXME("(%p)->(%8ld,%8ld): stub\n", This, dwStart, dwCount);
367 /* NOTE : if you experience crashes in this function, you must have a buggy
368 version of Mesa. See the file d3dtexture.c for a cure */
369 HRESULT WINAPI IDirect3DTexture2Impl_Load(
370 LPDIRECT3DTEXTURE2 iface, LPDIRECT3DTEXTURE2 lpD3DTexture2
372 ICOM_THIS(IDirect3DTexture2Impl,iface);
374 IDirect3DTexture2Impl* ilpD3DTexture2=(IDirect3DTexture2Impl*)lpD3DTexture2;
375 DDSURFACEDESC *src_d, *dst_d;
376 static void (*ptr_ColorTableEXT) (GLenum target, GLenum internalformat,
377 GLsizei width, GLenum format, GLenum type, const GLvoid *table) = NULL;
379 static BOOL color_table_queried = FALSE;
382 TRACE("(%p)->(%p)\n", This, ilpD3DTexture2);
383 TRACE("Copied surface %p to surface %p\n", ilpD3DTexture2->surface, This->surface);
385 /* Suppress the ALLOCONLOAD flag */
386 This->surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
388 /* Copy one surface on the other */
389 dst_d = (DDSURFACEDESC *)&(This->surface->surface_desc);
390 src_d = (DDSURFACEDESC *)&(ilpD3DTexture2->surface->surface_desc);
392 /* Install the callbacks to the destination surface */
393 This->surface->texture = This;
394 This->surface->SetColorKey_cb = SetColorKey_cb;
396 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
397 /* Should also check for same pixel format, lPitch, ... */
398 ERR("Error in surface sizes\n");
399 return D3DERR_TEXTURE_LOAD_FAILED;
401 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
402 /* I should put a macro for the calculus of bpp */
403 int bpp = (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
404 1 /* 8 bit of palette index */:
405 src_d->ddpfPixelFormat.u1.dwRGBBitCount / 8 /* RGB bits for each colors */ );
406 GLuint current_texture;
408 /* Copy the main memry texture into the surface that corresponds to the OpenGL
410 memcpy(dst_d->lpSurface, src_d->lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
414 /* Now, load the texture */
415 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
416 glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
418 /* If the GetHandle was not done, get the texture name here */
419 if (dtpriv->tex_name == 0)
420 glGenTextures(1, &(dtpriv->tex_name));
421 glBindTexture(GL_TEXTURE_2D, dtpriv->tex_name);
423 if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
427 IDirectDrawPaletteImpl* pal = This->surface->palette;
432 if (color_table_queried == FALSE) {
434 ((Mesa_DeviceCapabilities *) ((x11_dd_private *) This->surface->s.ddraw->d->private)->device_capabilities)->ptr_ColorTableEXT;
439 ERR("Palettized texture Loading with a NULL palette !\n");
441 return D3DERR_TEXTURE_LOAD_FAILED;
444 /* Get the surface's palette */
445 for (i = 0; i < 256; i++) {
446 table[i][0] = pal->palents[i].peRed;
447 table[i][1] = pal->palents[i].peGreen;
448 table[i][2] = pal->palents[i].peBlue;
449 if ((This->surface->surface_desc.dwFlags & DDSD_CKSRCBLT) &&
450 (i >= This->surface->surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue) &&
451 (i <= This->surface->surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue))
457 /* Texture snooping */
460 if (ptr_ColorTableEXT != NULL) {
461 /* use Paletted Texture Extension */
462 ptr_ColorTableEXT(GL_TEXTURE_2D, /* target */
463 GL_RGBA, /* internal format */
464 256, /* table size */
465 GL_RGBA, /* table format */
466 GL_UNSIGNED_BYTE, /* table type */
467 table); /* the color table */
469 glTexImage2D(GL_TEXTURE_2D, /* target */
471 GL_COLOR_INDEX8_EXT, /* internal format */
472 src_d->dwWidth, src_d->dwHeight, /* width, height */
474 GL_COLOR_INDEX, /* texture format */
475 GL_UNSIGNED_BYTE, /* texture type */
476 src_d->lpSurface); /* the texture */
478 DWORD *surface = (DWORD *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, src_d->dwWidth * src_d->dwHeight * sizeof(DWORD));
480 BYTE *src = (BYTE *) src_d->lpSurface, *dst = (BYTE *) surface;
482 for (i = 0; i < src_d->dwHeight * src_d->dwWidth; i++) {
484 *dst++ = table[color][0];
485 *dst++ = table[color][1];
486 *dst++ = table[color][2];
487 *dst++ = table[color][3];
490 glTexImage2D(GL_TEXTURE_2D,
493 src_d->dwWidth, src_d->dwHeight,
499 HeapFree(GetProcessHeap(), 0, surface);
501 } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
505 if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 8) {
506 /* **********************
507 GL_UNSIGNED_BYTE_3_3_2
508 ********************** */
509 glTexImage2D(GL_TEXTURE_2D,
512 src_d->dwWidth, src_d->dwHeight,
515 GL_UNSIGNED_BYTE_3_3_2,
517 } else if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 16) {
518 if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000000) {
520 /* Texture snooping */
523 glTexImage2D(GL_TEXTURE_2D,
526 src_d->dwWidth, src_d->dwHeight,
529 GL_UNSIGNED_SHORT_5_6_5,
531 } else if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000001) {
532 /* Texture snooping */
535 glTexImage2D(GL_TEXTURE_2D,
538 src_d->dwWidth, src_d->dwHeight,
541 GL_UNSIGNED_SHORT_5_5_5_1,
543 } else if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x0000000F) {
544 glTexImage2D(GL_TEXTURE_2D,
547 src_d->dwWidth, src_d->dwHeight,
550 GL_UNSIGNED_SHORT_4_4_4_4,
552 } else if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00008000) {
553 /* Converting the 1555 format in 5551 packed */
554 WORD *surface = (WORD *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, src_d->dwWidth * src_d->dwHeight * sizeof(WORD));
556 WORD *src = (WORD *) src_d->lpSurface, *dst = surface;
558 for (i = 0; i < src_d->dwHeight * src_d->dwWidth; i++) {
559 *dst++ = (((*src & 0x8000) >> 15) |
560 ((*src & 0x7FFF) << 1));
564 glTexImage2D(GL_TEXTURE_2D,
567 src_d->dwWidth, src_d->dwHeight,
570 GL_UNSIGNED_SHORT_5_5_5_1,
573 HeapFree(GetProcessHeap(), 0, surface);
575 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
577 } else if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 24) {
578 glTexImage2D(GL_TEXTURE_2D,
581 src_d->dwWidth, src_d->dwHeight,
586 } else if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 32) {
587 glTexImage2D(GL_TEXTURE_2D,
590 src_d->dwWidth, src_d->dwHeight,
596 ERR("Unhandled texture format (bad RGB count)\n");
599 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
602 glBindTexture(GL_TEXTURE_2D, current_texture);
611 /*******************************************************************************
612 * IDirect3DTexture2 VTable
614 ICOM_VTABLE(IDirect3DTexture2) mesa_texture2_vtable =
616 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
617 /*** IUnknown methods ***/
618 IDirect3DTexture2Impl_QueryInterface,
619 IDirect3DTexture2Impl_AddRef,
620 IDirect3DTexture2Impl_Release,
621 /*** IDirect3DTexture methods ***/
622 IDirect3DTexture2Impl_GetHandle,
623 IDirect3DTexture2Impl_PaletteChanged,
624 IDirect3DTexture2Impl_Load
627 /*******************************************************************************
628 * IDirect3DTexture VTable
630 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
631 # define XCAST(fun) (typeof(mesa_texture_vtable.fun))
633 # define XCAST(fun) (void*)
636 ICOM_VTABLE(IDirect3DTexture) mesa_texture_vtable =
638 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
639 /*** IUnknown methods ***/
640 XCAST(QueryInterface)IDirect3DTexture2Impl_QueryInterface,
641 XCAST(AddRef)IDirect3DTexture2Impl_AddRef,
642 XCAST(Release)IDirect3DTexture2Impl_Release,
643 /*** IDirect3DTexture methods ***/
644 IDirect3DTextureImpl_Initialize,
645 IDirect3DTextureImpl_GetHandle,
646 XCAST(PaletteChanged)IDirect3DTexture2Impl_PaletteChanged,
647 XCAST(Load)IDirect3DTexture2Impl_Load,
648 IDirect3DTextureImpl_Unload
651 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)