4 This files contains the implementation of interface Direct3DTexture2. */
11 #include "wine/obj_base.h"
17 #include "d3d_private.h"
19 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
29 #define SNOOP_PALETTED() \
35 sprintf(buf, "%d.pnm", This->tex_name); \
36 f = fopen(buf, "wb"); \
37 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
38 for (y = 0; y < src_d->dwHeight; y++) { \
39 for (x = 0; x < src_d->dwWidth; x++) { \
40 unsigned char c = ((unsigned char *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
41 fputc(table[c][0], f); \
42 fputc(table[c][1], f); \
43 fputc(table[c][2], f); \
49 #define SNOOP_5650() \
55 sprintf(buf, "%d.pnm", This->tex_name); \
56 f = fopen(buf, "wb"); \
57 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
58 for (y = 0; y < src_d->dwHeight; y++) { \
59 for (x = 0; x < src_d->dwWidth; x++) { \
60 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
61 fputc((c & 0xF800) >> 8, f); \
62 fputc((c & 0x07E0) >> 3, f); \
63 fputc((c & 0x001F) << 3, f); \
69 #define SNOOP_5551() \
75 sprintf(buf, "%d.pnm", This->tex_name); \
76 f = fopen(buf, "wb"); \
77 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
78 for (y = 0; y < src_d->dwHeight; y++) { \
79 for (x = 0; x < src_d->dwWidth; x++) { \
80 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
81 fputc((c & 0xF800) >> 8, f); \
82 fputc((c & 0x07C0) >> 3, f); \
83 fputc((c & 0x003E) << 2, f); \
89 #define SNOOP_PALETTED()
94 static ICOM_VTABLE(IDirect3DTexture2) texture2_vtable;
95 static ICOM_VTABLE(IDirect3DTexture) texture_vtable;
97 /*******************************************************************************
98 * Texture2 Creation functions
100 LPDIRECT3DTEXTURE2 d3dtexture2_create(IDirectDrawSurface4Impl* surf)
102 IDirect3DTexture2Impl* tex;
104 tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
106 tex->lpvtbl = &texture2_vtable;
109 return (LPDIRECT3DTEXTURE2)tex;
112 /*******************************************************************************
113 * Texture Creation functions
115 LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurface4Impl* surf)
117 IDirect3DTexture2Impl* tex;
119 tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
121 tex->lpvtbl = (ICOM_VTABLE(IDirect3DTexture2)*)&texture_vtable;
124 return (LPDIRECT3DTEXTURE)tex;
127 /*******************************************************************************
128 * IDirectSurface callback methods
130 HRESULT WINAPI SetColorKey_cb(IDirect3DTexture2Impl *texture, DWORD dwFlags, LPDDCOLORKEY ckey )
132 DDSURFACEDESC *tex_d;
134 GLuint current_texture;
136 TRACE(ddraw, "(%p) : colorkey callback\n", texture);
138 /* Get the texture description */
139 tex_d = &(texture->surface->s.surface_desc);
140 bpp = (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
141 1 /* 8 bit of palette index */:
142 tex_d->ddpfPixelFormat.x.dwRGBBitCount / 8 /* RGB bits for each colors */ );
144 /* Now, save the current texture */
146 glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
148 /* If the GetHandle was not done yet, it's an error */
149 if (texture->tex_name == 0) {
150 ERR(ddraw, "Unloaded texture !\n");
154 glBindTexture(GL_TEXTURE_2D, texture->tex_name);
156 if (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
157 FIXME(ddraw, "Todo Paletted\n");
158 } else if (tex_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
159 if (tex_d->ddpfPixelFormat.x.dwRGBBitCount == 8) {
160 FIXME(ddraw, "Todo 3_3_2_0\n");
161 } else if (tex_d->ddpfPixelFormat.x.dwRGBBitCount == 16) {
162 if (tex_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000000) {
163 /* Now transform the 5_6_5 into a 5_5_5_1 surface to support color keying */
164 unsigned short *dest = (unsigned short *) HeapAlloc(GetProcessHeap(),
166 tex_d->dwWidth * tex_d->dwHeight * bpp);
167 unsigned short *src = (unsigned short *) tex_d->y.lpSurface;
170 for (y = 0; y < tex_d->dwHeight; y++) {
171 for (x = 0; x < tex_d->dwWidth; x++) {
172 unsigned short cpixel = src[x + y * tex_d->dwWidth];
174 if ((dwFlags & DDCKEY_SRCBLT) &&
175 (cpixel >= ckey->dwColorSpaceLowValue) &&
176 (cpixel <= ckey->dwColorSpaceHighValue)) /* No alpha bit => this pixel is transparent */
177 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0000;
178 else /* Alpha bit is set => this pixel will be seen */
179 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0001;
183 glTexImage2D(GL_TEXTURE_2D,
186 tex_d->dwWidth, tex_d->dwHeight,
189 GL_UNSIGNED_SHORT_5_5_5_1,
192 /* Frees the temporary surface */
193 HeapFree(GetProcessHeap(),0,dest);
194 } else if (tex_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000001) {
195 FIXME(ddraw, "Todo 5_5_5_1\n");
196 } else if (tex_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x0000000F) {
197 FIXME(ddraw, "Todo 4_4_4_4\n");
199 ERR(ddraw, "Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
201 } else if (tex_d->ddpfPixelFormat.x.dwRGBBitCount == 24) {
202 FIXME(ddraw, "Todo 8_8_8_0\n");
203 } else if (tex_d->ddpfPixelFormat.x.dwRGBBitCount == 32) {
204 FIXME(ddraw, "Todo 8_8_8_8\n");
206 ERR(ddraw, "Unhandled texture format (bad RGB count)\n");
209 ERR(ddraw, "Unhandled texture format (neither RGB nor INDEX)\n");
216 /*******************************************************************************
217 * IDirect3DTexture2 methods
220 static HRESULT WINAPI IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface,
224 ICOM_THIS(IDirect3DTexture2Impl,iface);
227 WINE_StringFromCLSID((LPCLSID)riid,xrefiid);
228 FIXME(ddraw, "(%p)->(%s,%p): stub\n", This, xrefiid,ppvObj);
235 static ULONG WINAPI IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface)
237 ICOM_THIS(IDirect3DTexture2Impl,iface);
238 TRACE(ddraw, "(%p)->()incrementing from %lu.\n", This, This->ref );
240 return ++(This->ref);
245 static ULONG WINAPI IDirect3DTexture2Impl_Release(LPDIRECT3DTEXTURE2 iface)
247 ICOM_THIS(IDirect3DTexture2Impl,iface);
248 FIXME( ddraw, "(%p)->() decrementing from %lu.\n", This, This->ref );
250 if (!--(This->ref)) {
251 /* Delete texture from OpenGL */
253 glDeleteTextures(1, &(This->tex_name));
256 /* Release surface */
257 IDirectDrawSurface4_Release((IDirectDrawSurface4*)This->surface);
259 HeapFree(GetProcessHeap(),0,This);
266 /*** IDirect3DTexture methods ***/
267 static HRESULT WINAPI IDirect3DTextureImpl_GetHandle(LPDIRECT3DTEXTURE iface,
268 LPDIRECT3DDEVICE lpD3DDevice,
269 LPD3DTEXTUREHANDLE lpHandle)
271 ICOM_THIS(IDirect3DTexture2Impl,iface);
272 IDirect3DDeviceImpl* ilpD3DDevice=(IDirect3DDeviceImpl*)lpD3DDevice;
273 FIXME(ddraw, "(%p)->(%p,%p): stub\n", This, ilpD3DDevice, lpHandle);
275 *lpHandle = (D3DTEXTUREHANDLE) This;
277 /* Now, bind a new texture */
279 ilpD3DDevice->set_context(ilpD3DDevice);
280 This->D3Ddevice = (void *) ilpD3DDevice;
281 if (This->tex_name == 0)
282 glGenTextures(1, &(This->tex_name));
285 TRACE(ddraw, "OpenGL texture handle is : %d\n", This->tex_name);
290 static HRESULT WINAPI IDirect3DTextureImpl_Initialize(LPDIRECT3DTEXTURE iface,
291 LPDIRECT3DDEVICE lpD3DDevice,
292 LPDIRECTDRAWSURFACE lpSurface)
294 ICOM_THIS(IDirect3DTexture2Impl,iface);
295 TRACE(ddraw, "(%p)->(%p,%p)\n", This, lpD3DDevice, lpSurface);
297 return DDERR_ALREADYINITIALIZED;
300 static HRESULT WINAPI IDirect3DTextureImpl_Unload(LPDIRECT3DTEXTURE iface)
302 ICOM_THIS(IDirect3DTexture2Impl,iface);
303 FIXME(ddraw, "(%p)->(): stub\n", This);
308 /*** IDirect3DTexture2 methods ***/
309 static HRESULT WINAPI IDirect3DTexture2Impl_GetHandle(LPDIRECT3DTEXTURE2 iface,
310 LPDIRECT3DDEVICE2 lpD3DDevice2,
311 LPD3DTEXTUREHANDLE lpHandle)
313 ICOM_THIS(IDirect3DTexture2Impl,iface);
314 IDirect3DDevice2Impl* ilpD3DDevice2=(IDirect3DDevice2Impl*)lpD3DDevice2;
315 TRACE(ddraw, "(%p)->(%p,%p)\n", This, ilpD3DDevice2, lpHandle);
317 /* For 32 bits OSes, handles = pointers */
318 *lpHandle = (D3DTEXTUREHANDLE) This;
320 /* Now, bind a new texture */
322 ilpD3DDevice2->set_context(ilpD3DDevice2);
323 This->D3Ddevice = (void *) ilpD3DDevice2;
324 if (This->tex_name == 0)
325 glGenTextures(1, &(This->tex_name));
328 TRACE(ddraw, "OpenGL texture handle is : %d\n", This->tex_name);
334 static HRESULT WINAPI IDirect3DTexture2Impl_PaletteChanged(LPDIRECT3DTEXTURE2 iface,
338 ICOM_THIS(IDirect3DTexture2Impl,iface);
339 FIXME(ddraw, "(%p)->(%8ld,%8ld): stub\n", This, dwStart, dwCount);
344 /* NOTE : if you experience crashes in this function, you must have a buggy
345 version of Mesa. See the file d3dtexture.c for a cure */
346 static HRESULT WINAPI IDirect3DTexture2Impl_Load(LPDIRECT3DTEXTURE2 iface,
347 LPDIRECT3DTEXTURE2 lpD3DTexture2)
349 ICOM_THIS(IDirect3DTexture2Impl,iface);
350 IDirect3DTexture2Impl* ilpD3DTexture2=(IDirect3DTexture2Impl*)lpD3DTexture2;
351 DDSURFACEDESC *src_d, *dst_d;
352 TRACE(ddraw, "(%p)->(%p)\n", This, ilpD3DTexture2);
354 TRACE(ddraw, "Copied surface %p to surface %p\n", ilpD3DTexture2->surface, This->surface);
356 /* Suppress the ALLOCONLOAD flag */
357 This->surface->s.surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
359 /* Copy one surface on the other */
360 dst_d = &(This->surface->s.surface_desc);
361 src_d = &(ilpD3DTexture2->surface->s.surface_desc);
363 /* Install the callbacks to the destination surface */
364 This->surface->s.texture = This;
365 This->surface->s.SetColorKey_cb = SetColorKey_cb;
367 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
368 /* Should also check for same pixel format, lPitch, ... */
369 ERR(ddraw, "Error in surface sizes\n");
370 return D3DERR_TEXTURE_LOAD_FAILED;
372 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
373 /* I should put a macro for the calculus of bpp */
374 int bpp = (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
375 1 /* 8 bit of palette index */:
376 src_d->ddpfPixelFormat.x.dwRGBBitCount / 8 /* RGB bits for each colors */ );
377 GLuint current_texture;
379 /* Copy the main memry texture into the surface that corresponds to the OpenGL
381 memcpy(dst_d->y.lpSurface, src_d->y.lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
385 /* Now, load the texture */
386 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
387 glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture);
389 /* If the GetHandle was not done, get the texture name here */
390 if (This->tex_name == 0)
391 glGenTextures(1, &(This->tex_name));
392 glBindTexture(GL_TEXTURE_2D, This->tex_name);
394 if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
398 IDirectDrawPaletteImpl* pal = This->surface->s.palette;
403 ERR(ddraw, "Palettized texture Loading with a NULL palette !\n");
405 return D3DERR_TEXTURE_LOAD_FAILED;
408 /* Get the surface's palette */
409 for (i = 0; i < 256; i++) {
410 table[i][0] = pal->palents[i].peRed;
411 table[i][1] = pal->palents[i].peGreen;
412 table[i][2] = pal->palents[i].peBlue;
413 if ((This->surface->s.surface_desc.dwFlags & DDSD_CKSRCBLT) &&
414 (i >= This->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue) &&
415 (i <= This->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue))
421 /* Texture snooping */
424 /* Use Paletted Texture Extension */
425 glColorTableEXT(GL_TEXTURE_2D, /* target */
426 GL_RGBA, /* internal format */
427 256, /* table size */
428 GL_RGBA, /* table format */
429 GL_UNSIGNED_BYTE, /* table type */
430 table); /* the color table */
432 glTexImage2D(GL_TEXTURE_2D, /* target */
434 GL_COLOR_INDEX8_EXT, /* internal format */
435 src_d->dwWidth, src_d->dwHeight, /* width, height */
437 GL_COLOR_INDEX, /* texture format */
438 GL_UNSIGNED_BYTE, /* texture type */
439 src_d->y.lpSurface); /* the texture */
440 } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
444 if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 8) {
445 /* **********************
446 GL_UNSIGNED_BYTE_3_3_2
447 ********************** */
448 glTexImage2D(GL_TEXTURE_2D,
451 src_d->dwWidth, src_d->dwHeight,
454 GL_UNSIGNED_BYTE_3_3_2,
456 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 16) {
457 if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000000) {
459 /* Texture snooping */
462 glTexImage2D(GL_TEXTURE_2D,
465 src_d->dwWidth, src_d->dwHeight,
468 GL_UNSIGNED_SHORT_5_6_5,
470 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000001) {
471 /* Texture snooping */
474 glTexImage2D(GL_TEXTURE_2D,
477 src_d->dwWidth, src_d->dwHeight,
480 GL_UNSIGNED_SHORT_5_5_5_1,
482 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x0000000F) {
483 glTexImage2D(GL_TEXTURE_2D,
486 src_d->dwWidth, src_d->dwHeight,
489 GL_UNSIGNED_SHORT_4_4_4_4,
492 ERR(ddraw, "Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
494 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 24) {
495 glTexImage2D(GL_TEXTURE_2D,
498 src_d->dwWidth, src_d->dwHeight,
503 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 32) {
504 glTexImage2D(GL_TEXTURE_2D,
507 src_d->dwWidth, src_d->dwHeight,
513 ERR(ddraw, "Unhandled texture format (bad RGB count)\n");
516 ERR(ddraw, "Unhandled texture format (neither RGB nor INDEX)\n");
519 glBindTexture(GL_TEXTURE_2D, current_texture);
528 /*******************************************************************************
529 * IDirect3DTexture2 VTable
531 static ICOM_VTABLE(IDirect3DTexture2) texture2_vtable = {
532 /*** IUnknown methods ***/
533 IDirect3DTexture2Impl_QueryInterface,
534 IDirect3DTexture2Impl_AddRef,
535 IDirect3DTexture2Impl_Release,
536 /*** IDirect3DTexture methods ***/
537 IDirect3DTexture2Impl_GetHandle,
538 IDirect3DTexture2Impl_PaletteChanged,
539 IDirect3DTexture2Impl_Load
542 /*******************************************************************************
543 * IDirect3DTexture VTable
545 static ICOM_VTABLE(IDirect3DTexture) texture_vtable = {
546 /*** IUnknown methods ***/
547 IDirect3DTexture2Impl_QueryInterface,
548 IDirect3DTexture2Impl_AddRef,
549 IDirect3DTexture2Impl_Release,
550 /*** IDirect3DTexture methods ***/
551 IDirect3DTextureImpl_Initialize,
552 IDirect3DTextureImpl_GetHandle,
553 IDirect3DTexture2Impl_PaletteChanged,
554 IDirect3DTexture2Impl_Load,
555 IDirect3DTextureImpl_Unload
558 #else /* HAVE_MESAGL */
560 /* These function should never be called if MesaGL is not present */
561 LPDIRECT3DTEXTURE2 d3dtexture2_create(IDirectDrawSurface4Impl* surf) {
562 ERR(ddraw, "Should not be called...\n");
566 LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurface4Impl* surf) {
567 ERR(ddraw, "Should not be called...\n");
571 #endif /* HAVE_MESAGL */