The COM virtual tables must use the
[wine.git] / graphics / d3dtexture.c
blob47e95bbe38ac28ecaf7df9f425a9af6f50407391
1 /* Direct3D Texture
2 (c) 1998 Lionel ULMER
4 This files contains the implementation of interface Direct3DTexture2. */
7 #include <string.h>
8 #include "config.h"
9 #include "windef.h"
10 #include "winerror.h"
11 #include "wine/obj_base.h"
12 #include "heap.h"
13 #include "ddraw.h"
14 #include "d3d.h"
15 #include "debugtools.h"
17 #include "d3d_private.h"
19 DEFAULT_DEBUG_CHANNEL(ddraw)
21 #ifdef HAVE_MESAGL
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
25 texture memory) */
26 #undef TEXTURE_SNOOP
28 #ifdef TEXTURE_SNOOP
29 #define SNOOP_PALETTED() \
30 { \
31 FILE *f; \
32 char buf[32]; \
33 int x, y; \
35 sprintf(buf, "%ld.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); \
44 } \
45 } \
46 fclose(f); \
49 #define SNOOP_5650() \
50 { \
51 FILE *f; \
52 char buf[32]; \
53 int x, y; \
55 sprintf(buf, "%ld.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); \
64 } \
65 } \
66 fclose(f); \
69 #define SNOOP_5551() \
70 { \
71 FILE *f; \
72 char buf[32]; \
73 int x, y; \
75 sprintf(buf, "%ld.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); \
84 } \
85 } \
86 fclose(f); \
88 #else
89 #define SNOOP_PALETTED()
90 #define SNOOP_5650()
91 #define SNOOP_5551()
92 #endif
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));
105 tex->ref = 1;
106 ICOM_VTBL(tex) = &texture2_vtable;
107 tex->surface = surf;
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));
120 tex->ref = 1;
121 ICOM_VTBL(tex) = (ICOM_VTABLE(IDirect3DTexture2)*)&texture_vtable;
122 tex->surface = surf;
124 return (LPDIRECT3DTEXTURE)tex;
127 /*******************************************************************************
128 * IDirectSurface callback methods
130 HRESULT WINAPI SetColorKey_cb(IDirect3DTexture2Impl *texture, DWORD dwFlags, LPDDCOLORKEY ckey )
132 DDSURFACEDESC *tex_d;
133 int bpp;
134 GLuint current_texture;
136 TRACE("(%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.u.dwRGBBitCount / 8 /* RGB bits for each colors */ );
144 /* Now, save the current texture */
145 ENTER_GL();
146 glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
148 /* If the GetHandle was not done yet, it's an error */
149 if (texture->tex_name == 0) {
150 ERR("Unloaded texture !\n");
151 LEAVE_GL();
152 return DD_OK;
154 glBindTexture(GL_TEXTURE_2D, texture->tex_name);
156 if (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
157 FIXME("Todo Paletted\n");
158 } else if (tex_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
159 if (tex_d->ddpfPixelFormat.u.dwRGBBitCount == 8) {
160 FIXME("Todo 3_3_2_0\n");
161 } else if (tex_d->ddpfPixelFormat.u.dwRGBBitCount == 16) {
162 if (tex_d->ddpfPixelFormat.u4.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(),
165 HEAP_ZERO_MEMORY,
166 tex_d->dwWidth * tex_d->dwHeight * bpp);
167 unsigned short *src = (unsigned short *) tex_d->u1.lpSurface;
168 int x, y;
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,
185 GL_RGBA,
186 tex_d->dwWidth, tex_d->dwHeight,
188 GL_RGBA,
189 GL_UNSIGNED_SHORT_5_5_5_1,
190 dest);
192 /* Frees the temporary surface */
193 HeapFree(GetProcessHeap(),0,dest);
194 } else if (tex_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x00000001) {
195 FIXME("Todo 5_5_5_1\n");
196 } else if (tex_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x0000000F) {
197 FIXME("Todo 4_4_4_4\n");
198 } else {
199 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
201 } else if (tex_d->ddpfPixelFormat.u.dwRGBBitCount == 24) {
202 FIXME("Todo 8_8_8_0\n");
203 } else if (tex_d->ddpfPixelFormat.u.dwRGBBitCount == 32) {
204 FIXME("Todo 8_8_8_8\n");
205 } else {
206 ERR("Unhandled texture format (bad RGB count)\n");
208 } else {
209 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
211 LEAVE_GL();
213 return DD_OK;
216 /*******************************************************************************
217 * IDirect3DTexture2 methods
220 static HRESULT WINAPI IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface,
221 REFIID riid,
222 LPVOID* ppvObj)
224 ICOM_THIS(IDirect3DTexture2Impl,iface);
226 FIXME("(%p)->(%s,%p): stub\n", This, debugstr_guid(riid),ppvObj);
228 return S_OK;
233 static ULONG WINAPI IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface)
235 ICOM_THIS(IDirect3DTexture2Impl,iface);
236 TRACE("(%p)->()incrementing from %lu.\n", This, This->ref );
238 return ++(This->ref);
243 static ULONG WINAPI IDirect3DTexture2Impl_Release(LPDIRECT3DTEXTURE2 iface)
245 ICOM_THIS(IDirect3DTexture2Impl,iface);
246 FIXME("(%p)->() decrementing from %lu.\n", This, This->ref );
248 if (!--(This->ref)) {
249 /* Delete texture from OpenGL */
250 ENTER_GL();
251 glDeleteTextures(1, &(This->tex_name));
252 LEAVE_GL();
254 /* Release surface */
255 IDirectDrawSurface4_Release((IDirectDrawSurface4*)This->surface);
257 HeapFree(GetProcessHeap(),0,This);
258 return 0;
261 return This->ref;
264 /*** IDirect3DTexture methods ***/
265 static HRESULT WINAPI IDirect3DTextureImpl_GetHandle(LPDIRECT3DTEXTURE iface,
266 LPDIRECT3DDEVICE lpD3DDevice,
267 LPD3DTEXTUREHANDLE lpHandle)
269 ICOM_THIS(IDirect3DTexture2Impl,iface);
270 IDirect3DDeviceImpl* ilpD3DDevice=(IDirect3DDeviceImpl*)lpD3DDevice;
271 FIXME("(%p)->(%p,%p): stub\n", This, ilpD3DDevice, lpHandle);
273 *lpHandle = (D3DTEXTUREHANDLE) This;
275 /* Now, bind a new texture */
276 ENTER_GL();
277 ilpD3DDevice->set_context(ilpD3DDevice);
278 This->D3Ddevice = (void *) ilpD3DDevice;
279 if (This->tex_name == 0)
280 glGenTextures(1, &(This->tex_name));
281 LEAVE_GL();
283 TRACE("OpenGL texture handle is : %d\n", This->tex_name);
285 return D3D_OK;
288 static HRESULT WINAPI IDirect3DTextureImpl_Initialize(LPDIRECT3DTEXTURE iface,
289 LPDIRECT3DDEVICE lpD3DDevice,
290 LPDIRECTDRAWSURFACE lpSurface)
292 ICOM_THIS(IDirect3DTexture2Impl,iface);
293 TRACE("(%p)->(%p,%p)\n", This, lpD3DDevice, lpSurface);
295 return DDERR_ALREADYINITIALIZED;
298 static HRESULT WINAPI IDirect3DTextureImpl_Unload(LPDIRECT3DTEXTURE iface)
300 ICOM_THIS(IDirect3DTexture2Impl,iface);
301 FIXME("(%p)->(): stub\n", This);
303 return D3D_OK;
306 /*** IDirect3DTexture2 methods ***/
307 static HRESULT WINAPI IDirect3DTexture2Impl_GetHandle(LPDIRECT3DTEXTURE2 iface,
308 LPDIRECT3DDEVICE2 lpD3DDevice2,
309 LPD3DTEXTUREHANDLE lpHandle)
311 ICOM_THIS(IDirect3DTexture2Impl,iface);
312 IDirect3DDevice2Impl* ilpD3DDevice2=(IDirect3DDevice2Impl*)lpD3DDevice2;
313 TRACE("(%p)->(%p,%p)\n", This, ilpD3DDevice2, lpHandle);
315 /* For 32 bits OSes, handles = pointers */
316 *lpHandle = (D3DTEXTUREHANDLE) This;
318 /* Now, bind a new texture */
319 ENTER_GL();
320 ilpD3DDevice2->set_context(ilpD3DDevice2);
321 This->D3Ddevice = (void *) ilpD3DDevice2;
322 if (This->tex_name == 0)
323 glGenTextures(1, &(This->tex_name));
324 LEAVE_GL();
326 TRACE("OpenGL texture handle is : %d\n", This->tex_name);
328 return D3D_OK;
331 /* Common methods */
332 static HRESULT WINAPI IDirect3DTexture2Impl_PaletteChanged(LPDIRECT3DTEXTURE2 iface,
333 DWORD dwStart,
334 DWORD dwCount)
336 ICOM_THIS(IDirect3DTexture2Impl,iface);
337 FIXME("(%p)->(%8ld,%8ld): stub\n", This, dwStart, dwCount);
339 return D3D_OK;
342 /* NOTE : if you experience crashes in this function, you must have a buggy
343 version of Mesa. See the file d3dtexture.c for a cure */
344 static HRESULT WINAPI IDirect3DTexture2Impl_Load(LPDIRECT3DTEXTURE2 iface,
345 LPDIRECT3DTEXTURE2 lpD3DTexture2)
347 ICOM_THIS(IDirect3DTexture2Impl,iface);
348 IDirect3DTexture2Impl* ilpD3DTexture2=(IDirect3DTexture2Impl*)lpD3DTexture2;
349 DDSURFACEDESC *src_d, *dst_d;
350 TRACE("(%p)->(%p)\n", This, ilpD3DTexture2);
352 TRACE("Copied surface %p to surface %p\n", ilpD3DTexture2->surface, This->surface);
354 /* Suppress the ALLOCONLOAD flag */
355 This->surface->s.surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
357 /* Copy one surface on the other */
358 dst_d = &(This->surface->s.surface_desc);
359 src_d = &(ilpD3DTexture2->surface->s.surface_desc);
361 /* Install the callbacks to the destination surface */
362 This->surface->s.texture = This;
363 This->surface->s.SetColorKey_cb = SetColorKey_cb;
365 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
366 /* Should also check for same pixel format, lPitch, ... */
367 ERR("Error in surface sizes\n");
368 return D3DERR_TEXTURE_LOAD_FAILED;
369 } else {
370 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
371 /* I should put a macro for the calculus of bpp */
372 int bpp = (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
373 1 /* 8 bit of palette index */:
374 src_d->ddpfPixelFormat.u.dwRGBBitCount / 8 /* RGB bits for each colors */ );
375 GLuint current_texture;
377 /* Copy the main memry texture into the surface that corresponds to the OpenGL
378 texture object. */
379 memcpy(dst_d->u1.lpSurface, src_d->u1.lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
381 ENTER_GL();
383 /* Now, load the texture */
384 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
385 glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
387 /* If the GetHandle was not done, get the texture name here */
388 if (This->tex_name == 0)
389 glGenTextures(1, &(This->tex_name));
390 glBindTexture(GL_TEXTURE_2D, This->tex_name);
392 if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
393 /* ****************
394 Paletted Texture
395 **************** */
396 IDirectDrawPaletteImpl* pal = This->surface->s.palette;
397 BYTE table[256][4];
398 int i;
400 if (pal == NULL) {
401 ERR("Palettized texture Loading with a NULL palette !\n");
402 LEAVE_GL();
403 return D3DERR_TEXTURE_LOAD_FAILED;
406 /* Get the surface's palette */
407 for (i = 0; i < 256; i++) {
408 table[i][0] = pal->palents[i].peRed;
409 table[i][1] = pal->palents[i].peGreen;
410 table[i][2] = pal->palents[i].peBlue;
411 if ((This->surface->s.surface_desc.dwFlags & DDSD_CKSRCBLT) &&
412 (i >= This->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue) &&
413 (i <= This->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue))
414 table[i][3] = 0x00;
415 else
416 table[i][3] = 0xFF;
419 /* Texture snooping */
420 SNOOP_PALETTED();
422 #if defined(HAVE_GL_COLOR_TABLE) && defined(HAVE_GL_PALETTED_TEXTURE)
423 /* use Paletted Texture Extension */
424 glColorTableEXT(GL_TEXTURE_2D, /* target */
425 GL_RGBA, /* internal format */
426 256, /* table size */
427 GL_RGBA, /* table format */
428 GL_UNSIGNED_BYTE, /* table type */
429 table); /* the color table */
431 glTexImage2D(GL_TEXTURE_2D, /* target */
432 0, /* level */
433 GL_COLOR_INDEX8_EXT, /* internal format */
434 src_d->dwWidth, src_d->dwHeight, /* width, height */
435 0, /* border */
436 GL_COLOR_INDEX, /* texture format */
437 GL_UNSIGNED_BYTE, /* texture type */
438 src_d->u1.lpSurface); /* the texture */
439 #endif
440 } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
441 /* ************
442 RGB Textures
443 ************ */
444 if (src_d->ddpfPixelFormat.u.dwRGBBitCount == 8) {
445 /* **********************
446 GL_UNSIGNED_BYTE_3_3_2
447 ********************** */
448 glTexImage2D(GL_TEXTURE_2D,
450 GL_RGB,
451 src_d->dwWidth, src_d->dwHeight,
453 GL_RGB,
454 GL_UNSIGNED_BYTE_3_3_2,
455 src_d->u1.lpSurface);
456 } else if (src_d->ddpfPixelFormat.u.dwRGBBitCount == 16) {
457 if (src_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x00000000) {
459 /* Texture snooping */
460 SNOOP_5650();
462 glTexImage2D(GL_TEXTURE_2D,
464 GL_RGB,
465 src_d->dwWidth, src_d->dwHeight,
467 GL_RGB,
468 GL_UNSIGNED_SHORT_5_6_5,
469 src_d->u1.lpSurface);
470 } else if (src_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x00000001) {
471 /* Texture snooping */
472 SNOOP_5551();
474 glTexImage2D(GL_TEXTURE_2D,
476 GL_RGBA,
477 src_d->dwWidth, src_d->dwHeight,
479 GL_RGBA,
480 GL_UNSIGNED_SHORT_5_5_5_1,
481 src_d->u1.lpSurface);
482 } else if (src_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x0000000F) {
483 glTexImage2D(GL_TEXTURE_2D,
485 GL_RGBA,
486 src_d->dwWidth, src_d->dwHeight,
488 GL_RGBA,
489 GL_UNSIGNED_SHORT_4_4_4_4,
490 src_d->u1.lpSurface);
491 } else {
492 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
494 } else if (src_d->ddpfPixelFormat.u.dwRGBBitCount == 24) {
495 glTexImage2D(GL_TEXTURE_2D,
497 GL_RGB,
498 src_d->dwWidth, src_d->dwHeight,
500 GL_RGB,
501 GL_UNSIGNED_BYTE,
502 src_d->u1.lpSurface);
503 } else if (src_d->ddpfPixelFormat.u.dwRGBBitCount == 32) {
504 glTexImage2D(GL_TEXTURE_2D,
506 GL_RGBA,
507 src_d->dwWidth, src_d->dwHeight,
509 GL_RGBA,
510 GL_UNSIGNED_BYTE,
511 src_d->u1.lpSurface);
512 } else {
513 ERR("Unhandled texture format (bad RGB count)\n");
515 } else {
516 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
519 glBindTexture(GL_TEXTURE_2D, current_texture);
521 LEAVE_GL();
524 return D3D_OK;
528 /*******************************************************************************
529 * IDirect3DTexture2 VTable
531 static ICOM_VTABLE(IDirect3DTexture2) texture2_vtable =
533 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
534 /*** IUnknown methods ***/
535 IDirect3DTexture2Impl_QueryInterface,
536 IDirect3DTexture2Impl_AddRef,
537 IDirect3DTexture2Impl_Release,
538 /*** IDirect3DTexture methods ***/
539 IDirect3DTexture2Impl_GetHandle,
540 IDirect3DTexture2Impl_PaletteChanged,
541 IDirect3DTexture2Impl_Load
544 /*******************************************************************************
545 * IDirect3DTexture VTable
547 static ICOM_VTABLE(IDirect3DTexture) texture_vtable =
549 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
550 /*** IUnknown methods ***/
551 IDirect3DTexture2Impl_QueryInterface,
552 IDirect3DTexture2Impl_AddRef,
553 IDirect3DTexture2Impl_Release,
554 /*** IDirect3DTexture methods ***/
555 IDirect3DTextureImpl_Initialize,
556 IDirect3DTextureImpl_GetHandle,
557 IDirect3DTexture2Impl_PaletteChanged,
558 IDirect3DTexture2Impl_Load,
559 IDirect3DTextureImpl_Unload
562 #else /* HAVE_MESAGL */
564 /* These function should never be called if MesaGL is not present */
565 LPDIRECT3DTEXTURE2 d3dtexture2_create(IDirectDrawSurface4Impl* surf) {
566 ERR("Should not be called...\n");
567 return NULL;
570 LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurface4Impl* surf) {
571 ERR("Should not be called...\n");
572 return NULL;
575 #endif /* HAVE_MESAGL */