Added support for forwarded ordinals in built-in dlls.
[wine/multimedia.git] / graphics / d3dtexture.c
blobb961b5925c6d5820c49db260442ecc71a0185da4
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 "debug.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, "%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); \
44 } \
45 } \
46 fclose(f); \
49 #define SNOOP_5650() \
50 { \
51 FILE *f; \
52 char buf[32]; \
53 int x, y; \
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); \
64 } \
65 } \
66 fclose(f); \
69 #define SNOOP_5551() \
70 { \
71 FILE *f; \
72 char buf[32]; \
73 int x, y; \
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); \
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 tex->lpvtbl = &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 tex->lpvtbl = (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(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 */
145 glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
147 /* If the GetHandle was not done yet, it's an error */
148 if (texture->tex_name == 0) {
149 ERR(ddraw, "Unloaded texture !\n");
150 return DD_OK;
152 glBindTexture(GL_TEXTURE_2D, texture->tex_name);
154 if (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
155 FIXME(ddraw, "Todo Paletted\n");
156 } else if (tex_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
157 if (tex_d->ddpfPixelFormat.x.dwRGBBitCount == 8) {
158 FIXME(ddraw, "Todo 3_3_2_0\n");
159 } else if (tex_d->ddpfPixelFormat.x.dwRGBBitCount == 16) {
160 if (tex_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000000) {
161 /* Now transform the 5_6_5 into a 5_5_5_1 surface to support color keying */
162 unsigned short *dest = (unsigned short *) HeapAlloc(GetProcessHeap(),
163 HEAP_ZERO_MEMORY,
164 tex_d->dwWidth * tex_d->dwHeight * bpp);
165 unsigned short *src = (unsigned short *) tex_d->y.lpSurface;
166 int x, y;
168 for (y = 0; y < tex_d->dwHeight; y++) {
169 for (x = 0; x < tex_d->dwWidth; x++) {
170 unsigned short cpixel = src[x + y * tex_d->dwWidth];
172 if ((dwFlags & DDCKEY_SRCBLT) &&
173 (cpixel >= ckey->dwColorSpaceLowValue) &&
174 (cpixel <= ckey->dwColorSpaceHighValue)) /* No alpha bit => this pixel is transparent */
175 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0000;
176 else /* Alpha bit is set => this pixel will be seen */
177 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0001;
181 glTexImage2D(GL_TEXTURE_2D,
183 GL_RGBA,
184 tex_d->dwWidth, tex_d->dwHeight,
186 GL_RGBA,
187 GL_UNSIGNED_SHORT_5_5_5_1,
188 dest);
190 /* Frees the temporary surface */
191 HeapFree(GetProcessHeap(),0,dest);
192 } else if (tex_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000001) {
193 FIXME(ddraw, "Todo 5_5_5_1\n");
194 } else if (tex_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x0000000F) {
195 FIXME(ddraw, "Todo 4_4_4_4\n");
196 } else {
197 ERR(ddraw, "Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
199 } else if (tex_d->ddpfPixelFormat.x.dwRGBBitCount == 24) {
200 FIXME(ddraw, "Todo 8_8_8_0\n");
201 } else if (tex_d->ddpfPixelFormat.x.dwRGBBitCount == 32) {
202 FIXME(ddraw, "Todo 8_8_8_8\n");
203 } else {
204 ERR(ddraw, "Unhandled texture format (bad RGB count)\n");
206 } else {
207 ERR(ddraw, "Unhandled texture format (neither RGB nor INDEX)\n");
210 return DD_OK;
213 /*******************************************************************************
214 * IDirect3DTexture2 methods
217 static HRESULT WINAPI IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface,
218 REFIID riid,
219 LPVOID* ppvObj)
221 ICOM_THIS(IDirect3DTexture2Impl,iface);
222 char xrefiid[50];
224 WINE_StringFromCLSID((LPCLSID)riid,xrefiid);
225 FIXME(ddraw, "(%p)->(%s,%p): stub\n", This, xrefiid,ppvObj);
227 return S_OK;
232 static ULONG WINAPI IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface)
234 ICOM_THIS(IDirect3DTexture2Impl,iface);
235 TRACE(ddraw, "(%p)->()incrementing from %lu.\n", This, This->ref );
237 return ++(This->ref);
242 static ULONG WINAPI IDirect3DTexture2Impl_Release(LPDIRECT3DTEXTURE2 iface)
244 ICOM_THIS(IDirect3DTexture2Impl,iface);
245 FIXME( ddraw, "(%p)->() decrementing from %lu.\n", This, This->ref );
247 if (!--(This->ref)) {
248 /* Delete texture from OpenGL */
249 glDeleteTextures(1, &(This->tex_name));
251 /* Release surface */
252 IDirectDrawSurface4_Release((IDirectDrawSurface4*)This->surface);
254 HeapFree(GetProcessHeap(),0,This);
255 return 0;
258 return This->ref;
261 /*** IDirect3DTexture methods ***/
262 static HRESULT WINAPI IDirect3DTextureImpl_GetHandle(LPDIRECT3DTEXTURE iface,
263 LPDIRECT3DDEVICE lpD3DDevice,
264 LPD3DTEXTUREHANDLE lpHandle)
266 ICOM_THIS(IDirect3DTexture2Impl,iface);
267 IDirect3DDeviceImpl* ilpD3DDevice=(IDirect3DDeviceImpl*)lpD3DDevice;
268 FIXME(ddraw, "(%p)->(%p,%p): stub\n", This, ilpD3DDevice, lpHandle);
270 *lpHandle = (D3DTEXTUREHANDLE) This;
272 /* Now, bind a new texture */
273 ilpD3DDevice->set_context(ilpD3DDevice);
274 This->D3Ddevice = (void *) ilpD3DDevice;
275 if (This->tex_name == 0)
276 glGenTextures(1, &(This->tex_name));
278 TRACE(ddraw, "OpenGL texture handle is : %d\n", This->tex_name);
280 return D3D_OK;
283 static HRESULT WINAPI IDirect3DTextureImpl_Initialize(LPDIRECT3DTEXTURE iface,
284 LPDIRECT3DDEVICE lpD3DDevice,
285 LPDIRECTDRAWSURFACE lpSurface)
287 ICOM_THIS(IDirect3DTexture2Impl,iface);
288 TRACE(ddraw, "(%p)->(%p,%p)\n", This, lpD3DDevice, lpSurface);
290 return DDERR_ALREADYINITIALIZED;
293 static HRESULT WINAPI IDirect3DTextureImpl_Unload(LPDIRECT3DTEXTURE iface)
295 ICOM_THIS(IDirect3DTexture2Impl,iface);
296 FIXME(ddraw, "(%p)->(): stub\n", This);
298 return D3D_OK;
301 /*** IDirect3DTexture2 methods ***/
302 static HRESULT WINAPI IDirect3DTexture2Impl_GetHandle(LPDIRECT3DTEXTURE2 iface,
303 LPDIRECT3DDEVICE2 lpD3DDevice2,
304 LPD3DTEXTUREHANDLE lpHandle)
306 ICOM_THIS(IDirect3DTexture2Impl,iface);
307 IDirect3DDevice2Impl* ilpD3DDevice2=(IDirect3DDevice2Impl*)lpD3DDevice2;
308 TRACE(ddraw, "(%p)->(%p,%p)\n", This, ilpD3DDevice2, lpHandle);
310 /* For 32 bits OSes, handles = pointers */
311 *lpHandle = (D3DTEXTUREHANDLE) This;
313 /* Now, bind a new texture */
314 ilpD3DDevice2->set_context(ilpD3DDevice2);
315 This->D3Ddevice = (void *) ilpD3DDevice2;
316 if (This->tex_name == 0)
317 glGenTextures(1, &(This->tex_name));
319 TRACE(ddraw, "OpenGL texture handle is : %d\n", This->tex_name);
321 return D3D_OK;
324 /* Common methods */
325 static HRESULT WINAPI IDirect3DTexture2Impl_PaletteChanged(LPDIRECT3DTEXTURE2 iface,
326 DWORD dwStart,
327 DWORD dwCount)
329 ICOM_THIS(IDirect3DTexture2Impl,iface);
330 FIXME(ddraw, "(%p)->(%8ld,%8ld): stub\n", This, dwStart, dwCount);
332 return D3D_OK;
335 /* NOTE : if you experience crashes in this function, you must have a buggy
336 version of Mesa. See the file d3dtexture.c for a cure */
337 static HRESULT WINAPI IDirect3DTexture2Impl_Load(LPDIRECT3DTEXTURE2 iface,
338 LPDIRECT3DTEXTURE2 lpD3DTexture2)
340 ICOM_THIS(IDirect3DTexture2Impl,iface);
341 IDirect3DTexture2Impl* ilpD3DTexture2=(IDirect3DTexture2Impl*)lpD3DTexture2;
342 DDSURFACEDESC *src_d, *dst_d;
343 TRACE(ddraw, "(%p)->(%p)\n", This, ilpD3DTexture2);
345 TRACE(ddraw, "Copied surface %p to surface %p\n", ilpD3DTexture2->surface, This->surface);
347 /* Suppress the ALLOCONLOAD flag */
348 This->surface->s.surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
350 /* Copy one surface on the other */
351 dst_d = &(This->surface->s.surface_desc);
352 src_d = &(ilpD3DTexture2->surface->s.surface_desc);
354 /* Install the callbacks to the destination surface */
355 This->surface->s.texture = This;
356 This->surface->s.SetColorKey_cb = SetColorKey_cb;
358 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
359 /* Should also check for same pixel format, lPitch, ... */
360 ERR(ddraw, "Error in surface sizes\n");
361 return D3DERR_TEXTURE_LOAD_FAILED;
362 } else {
363 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
364 /* I should put a macro for the calculus of bpp */
365 int bpp = (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
366 1 /* 8 bit of palette index */:
367 src_d->ddpfPixelFormat.x.dwRGBBitCount / 8 /* RGB bits for each colors */ );
368 GLuint current_texture;
370 /* Copy the main memry texture into the surface that corresponds to the OpenGL
371 texture object. */
372 memcpy(dst_d->y.lpSurface, src_d->y.lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
374 /* Now, load the texture */
375 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
376 glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
378 /* If the GetHandle was not done, get the texture name here */
379 if (This->tex_name == 0)
380 glGenTextures(1, &(This->tex_name));
381 glBindTexture(GL_TEXTURE_2D, This->tex_name);
383 if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
384 /* ****************
385 Paletted Texture
386 **************** */
387 IDirectDrawPaletteImpl* pal = This->surface->s.palette;
388 BYTE table[256][4];
389 int i;
391 if (pal == NULL) {
392 ERR(ddraw, "Palettized texture Loading with a NULL palette !\n");
393 return D3DERR_TEXTURE_LOAD_FAILED;
396 /* Get the surface's palette */
397 for (i = 0; i < 256; i++) {
398 table[i][0] = pal->palents[i].peRed;
399 table[i][1] = pal->palents[i].peGreen;
400 table[i][2] = pal->palents[i].peBlue;
401 if ((This->surface->s.surface_desc.dwFlags & DDSD_CKSRCBLT) &&
402 (i >= This->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue) &&
403 (i <= This->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue))
404 table[i][3] = 0x00;
405 else
406 table[i][3] = 0xFF;
409 /* Texture snooping */
410 SNOOP_PALETTED();
412 /* Use Paletted Texture Extension */
413 glColorTableEXT(GL_TEXTURE_2D, /* target */
414 GL_RGBA, /* internal format */
415 256, /* table size */
416 GL_RGBA, /* table format */
417 GL_UNSIGNED_BYTE, /* table type */
418 table); /* the color table */
420 glTexImage2D(GL_TEXTURE_2D, /* target */
421 0, /* level */
422 GL_COLOR_INDEX8_EXT, /* internal format */
423 src_d->dwWidth, src_d->dwHeight, /* width, height */
424 0, /* border */
425 GL_COLOR_INDEX, /* texture format */
426 GL_UNSIGNED_BYTE, /* texture type */
427 src_d->y.lpSurface); /* the texture */
428 } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
429 /* ************
430 RGB Textures
431 ************ */
432 if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 8) {
433 /* **********************
434 GL_UNSIGNED_BYTE_3_3_2
435 ********************** */
436 glTexImage2D(GL_TEXTURE_2D,
438 GL_RGB,
439 src_d->dwWidth, src_d->dwHeight,
441 GL_RGB,
442 GL_UNSIGNED_BYTE_3_3_2,
443 src_d->y.lpSurface);
444 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 16) {
445 if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000000) {
447 /* Texture snooping */
448 SNOOP_5650();
450 glTexImage2D(GL_TEXTURE_2D,
452 GL_RGB,
453 src_d->dwWidth, src_d->dwHeight,
455 GL_RGB,
456 GL_UNSIGNED_SHORT_5_6_5,
457 src_d->y.lpSurface);
458 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000001) {
459 /* Texture snooping */
460 SNOOP_5551();
462 glTexImage2D(GL_TEXTURE_2D,
464 GL_RGBA,
465 src_d->dwWidth, src_d->dwHeight,
467 GL_RGBA,
468 GL_UNSIGNED_SHORT_5_5_5_1,
469 src_d->y.lpSurface);
470 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x0000000F) {
471 glTexImage2D(GL_TEXTURE_2D,
473 GL_RGBA,
474 src_d->dwWidth, src_d->dwHeight,
476 GL_RGBA,
477 GL_UNSIGNED_SHORT_4_4_4_4,
478 src_d->y.lpSurface);
479 } else {
480 ERR(ddraw, "Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
482 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 24) {
483 glTexImage2D(GL_TEXTURE_2D,
485 GL_RGB,
486 src_d->dwWidth, src_d->dwHeight,
488 GL_RGB,
489 GL_UNSIGNED_BYTE,
490 src_d->y.lpSurface);
491 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 32) {
492 glTexImage2D(GL_TEXTURE_2D,
494 GL_RGBA,
495 src_d->dwWidth, src_d->dwHeight,
497 GL_RGBA,
498 GL_UNSIGNED_BYTE,
499 src_d->y.lpSurface);
500 } else {
501 ERR(ddraw, "Unhandled texture format (bad RGB count)\n");
503 } else {
504 ERR(ddraw, "Unhandled texture format (neither RGB nor INDEX)\n");
507 glBindTexture(GL_TEXTURE_2D, current_texture);
510 return D3D_OK;
514 /*******************************************************************************
515 * IDirect3DTexture2 VTable
517 static ICOM_VTABLE(IDirect3DTexture2) texture2_vtable = {
518 /*** IUnknown methods ***/
519 IDirect3DTexture2Impl_QueryInterface,
520 IDirect3DTexture2Impl_AddRef,
521 IDirect3DTexture2Impl_Release,
522 /*** IDirect3DTexture methods ***/
523 IDirect3DTexture2Impl_GetHandle,
524 IDirect3DTexture2Impl_PaletteChanged,
525 IDirect3DTexture2Impl_Load
528 /*******************************************************************************
529 * IDirect3DTexture VTable
531 static ICOM_VTABLE(IDirect3DTexture) texture_vtable = {
532 /*** IUnknown methods ***/
533 IDirect3DTexture2Impl_QueryInterface,
534 IDirect3DTexture2Impl_AddRef,
535 IDirect3DTexture2Impl_Release,
536 /*** IDirect3DTexture methods ***/
537 IDirect3DTextureImpl_Initialize,
538 IDirect3DTextureImpl_GetHandle,
539 IDirect3DTexture2Impl_PaletteChanged,
540 IDirect3DTexture2Impl_Load,
541 IDirect3DTextureImpl_Unload
544 #else /* HAVE_MESAGL */
546 /* These function should never be called if MesaGL is not present */
547 LPDIRECT3DTEXTURE2 d3dtexture2_create(IDirectDrawSurface4Impl* surf) {
548 ERR(ddraw, "Should not be called...\n");
549 return NULL;
552 LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurface4Impl* surf) {
553 ERR(ddraw, "Should not be called...\n");
554 return NULL;
557 #endif /* HAVE_MESAGL */