Implemented stubs for SHRegEnumUSKey{A|W} and return end-of-list error
[wine] / dlls / ddraw / d3dtexture.c
1 /* Direct3D Texture
2    (c) 1998 Lionel ULMER
3    
4    This files contains the implementation of interface Direct3DTexture2. */
5
6
7 #include <string.h>
8 #include "config.h"
9 #include "windef.h"
10 #include "winerror.h"
11 #include "wine/obj_base.h"
12 #include "ddraw.h"
13 #include "d3d.h"
14 #include "debugtools.h"
15
16 #include "mesa_private.h"
17
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
20
21 DEFAULT_DEBUG_CHANNEL(ddraw);
22
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
27
28 #ifdef TEXTURE_SNOOP
29 #include <stdio.h>
30      
31 #define SNOOP_PALETTED()                                                                        \
32       {                                                                                         \
33         FILE *f;                                                                                \
34         char buf[32];                                                                           \
35         int x, y;                                                                               \
36                                                                                                 \
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);                                                              \
46           }                                                                                     \
47         }                                                                                       \
48         fclose(f);                                                                              \
49       }
50
51 #define SNOOP_5650()                                                                                    \
52           {                                                                                             \
53             FILE *f;                                                                                    \
54             char buf[32];                                                                               \
55             int x, y;                                                                                   \
56                                                                                                         \
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);                                                            \
66               }                                                                                         \
67             }                                                                                           \
68             fclose(f);                                                                                  \
69           }
70
71 #define SNOOP_5551()                                                                                    \
72           {                                                                                             \
73             FILE *f;                                                                                    \
74             char buf[32];                                                                               \
75             int x, y;                                                                                   \
76                                                                                                         \
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);                                                            \
86               }                                                                                         \
87             }                                                                                           \
88             fclose(f);                                                                                  \
89           }
90 #else
91 #define SNOOP_PALETTED()
92 #define SNOOP_5650()
93 #define SNOOP_5551()
94 #endif
95
96 extern ICOM_VTABLE(IDirect3DTexture2) mesa_texture2_vtable;
97 extern ICOM_VTABLE(IDirect3DTexture) mesa_texture_vtable;
98
99 /*******************************************************************************
100  *                              Texture2 Creation functions
101  */
102 LPDIRECT3DTEXTURE2 d3dtexture2_create(IDirectDrawSurfaceImpl* surf)
103 {
104   IDirect3DTexture2Impl* tex;
105   
106   tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
107   tex->ref = 1;
108   ICOM_VTBL(tex) = &mesa_texture2_vtable;
109   tex->surface = surf;
110
111   tex->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(mesa_d3dt_private));
112   
113   return (LPDIRECT3DTEXTURE2)tex;
114 }
115
116 /*******************************************************************************
117  *                              Texture Creation functions
118  */
119 LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurfaceImpl* surf)
120 {
121   IDirect3DTexture2Impl* tex;
122   
123   tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
124   tex->ref = 1;
125   ICOM_VTBL(tex) = (ICOM_VTABLE(IDirect3DTexture2)*)&mesa_texture_vtable;
126   tex->surface = surf;
127
128   tex->private = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(mesa_d3dt_private));
129   
130   return (LPDIRECT3DTEXTURE)tex;
131 }
132
133 /*******************************************************************************
134  *                         IDirectSurface callback methods
135  */
136 HRESULT WINAPI  SetColorKey_cb(IDirect3DTexture2Impl *texture, DWORD dwFlags, LPDDCOLORKEY ckey )
137 {
138   DDSURFACEDESC *tex_d;
139   D3DTPRIVATE(texture);
140   int bpp;
141   GLuint current_texture;
142   
143   TRACE("(%p) : colorkey callback\n", texture);
144
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 */ );
150   
151   /* Now, save the current texture */
152   ENTER_GL();
153   glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
154
155   /* If the GetHandle was not done yet, it's an error */
156   if (dtpriv->tex_name == 0) {
157     ERR("Unloaded texture !\n");
158     LEAVE_GL();
159     return DD_OK;
160   }
161   glBindTexture(GL_TEXTURE_2D, dtpriv->tex_name);
162
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(),
172                                                             HEAP_ZERO_MEMORY,
173                                                             tex_d->dwWidth * tex_d->dwHeight * bpp);
174         unsigned short *src = (unsigned short *) tex_d->lpSurface;
175         int x, y;
176
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];
180             
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; 
187           }
188         }
189
190         glTexImage2D(GL_TEXTURE_2D,
191                      0,
192                      GL_RGBA,
193                      tex_d->dwWidth, tex_d->dwHeight,
194                      0,
195                      GL_RGBA,
196                      GL_UNSIGNED_SHORT_5_5_5_1,
197                      dest);
198
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");
205       } else {
206         ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
207       }
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");
212     } else {
213       ERR("Unhandled texture format (bad RGB count)\n");
214     }
215   } else {
216     ERR("Unhandled texture format (neither RGB nor INDEX)\n");
217   }
218   LEAVE_GL();
219
220   return DD_OK;
221 }
222
223 /*******************************************************************************
224  *                              IDirect3DTexture2 methods
225  */
226
227 HRESULT WINAPI IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface,
228                                                         REFIID riid,
229                                                         LPVOID* ppvObj)
230 {
231   ICOM_THIS(IDirect3DTexture2Impl,iface);
232   
233   FIXME("(%p)->(%s,%p): stub\n", This, debugstr_guid(riid),ppvObj);
234   
235   return S_OK;
236 }
237
238
239
240 ULONG WINAPI IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface)
241 {
242   ICOM_THIS(IDirect3DTexture2Impl,iface);
243   TRACE("(%p)->()incrementing from %lu.\n", This, This->ref );
244   
245   return ++(This->ref);
246 }
247
248
249
250 ULONG WINAPI IDirect3DTexture2Impl_Release(LPDIRECT3DTEXTURE2 iface)
251 {
252   ICOM_THIS(IDirect3DTexture2Impl,iface);
253   D3DTPRIVATE(This);
254   FIXME("(%p)->() decrementing from %lu.\n", This, This->ref );
255   
256   if (!--(This->ref)) {
257     /* Delete texture from OpenGL */
258     ENTER_GL();
259     glDeleteTextures(1, &(dtpriv->tex_name));
260     LEAVE_GL();
261     
262     /* Release surface */
263     IDirectDrawSurface4_Release((IDirectDrawSurface4*)This->surface);
264     
265     HeapFree(GetProcessHeap(),0,This);
266     return 0;
267   }
268   
269   return This->ref;
270 }
271
272 /*** IDirect3DTexture methods ***/
273 HRESULT WINAPI IDirect3DTextureImpl_GetHandle(LPDIRECT3DTEXTURE iface,
274                                                  LPDIRECT3DDEVICE lpD3DDevice,
275                                                  LPD3DTEXTUREHANDLE lpHandle)
276 {
277     ICOM_THIS(IDirect3DTexture2Impl,iface);
278     D3DTPRIVATE(This);
279     IDirect3DDeviceImpl* ilpD3DDevice=(IDirect3DDeviceImpl*)lpD3DDevice;
280     FIXME("(%p)->(%p,%p): stub\n", This, ilpD3DDevice, lpHandle);
281
282     *lpHandle = (D3DTEXTUREHANDLE) This;
283
284     /* Now, bind a new texture */
285     ENTER_GL();
286     ilpD3DDevice->set_context(ilpD3DDevice);
287     This->D3Ddevice = (void *) ilpD3DDevice;
288     if (dtpriv->tex_name == 0)
289         glGenTextures(1, &(dtpriv->tex_name));
290     LEAVE_GL();
291
292     TRACE("OpenGL texture handle is : %d\n", dtpriv->tex_name);
293
294     return D3D_OK;
295 }
296
297 HRESULT WINAPI IDirect3DTextureImpl_Initialize(LPDIRECT3DTEXTURE iface,
298                                                   LPDIRECT3DDEVICE lpD3DDevice,
299                                                   LPDIRECTDRAWSURFACE lpSurface)
300 {
301   ICOM_THIS(IDirect3DTexture2Impl,iface);
302   TRACE("(%p)->(%p,%p)\n", This, lpD3DDevice, lpSurface);
303
304   return DDERR_ALREADYINITIALIZED;
305 }
306
307 HRESULT WINAPI IDirect3DTextureImpl_Unload(LPDIRECT3DTEXTURE iface)
308 {
309   ICOM_THIS(IDirect3DTexture2Impl,iface);
310   FIXME("(%p)->(): stub\n", This);
311
312   return D3D_OK;
313 }
314
315 /*** IDirect3DTexture2 methods ***/
316 HRESULT WINAPI IDirect3DTexture2Impl_GetHandle(LPDIRECT3DTEXTURE2 iface,
317                                                   LPDIRECT3DDEVICE2 lpD3DDevice2,
318                                                   LPD3DTEXTUREHANDLE lpHandle)
319 {
320     ICOM_THIS(IDirect3DTexture2Impl,iface);
321     D3DTPRIVATE(This);
322     IDirect3DDevice2Impl* ilpD3DDevice2=(IDirect3DDevice2Impl*)lpD3DDevice2;
323     TRACE("(%p)->(%p,%p)\n", This, ilpD3DDevice2, lpHandle);
324
325     /* For 32 bits OSes, handles = pointers */
326     *lpHandle = (D3DTEXTUREHANDLE) This;
327
328     /* Now, bind a new texture */
329     ENTER_GL();
330     ilpD3DDevice2->set_context(ilpD3DDevice2);
331     This->D3Ddevice = (void *) ilpD3DDevice2;
332     if (dtpriv->tex_name == 0)
333         glGenTextures(1, &(dtpriv->tex_name));
334     LEAVE_GL();
335
336     TRACE("OpenGL texture handle is : %d\n", dtpriv->tex_name);
337
338     return D3D_OK;
339 }
340
341 /* Common methods */
342 HRESULT WINAPI IDirect3DTexture2Impl_PaletteChanged(
343     LPDIRECT3DTEXTURE2 iface, DWORD dwStart, DWORD dwCount
344 ) {
345   ICOM_THIS(IDirect3DTexture2Impl,iface);
346   FIXME("(%p)->(%8ld,%8ld): stub\n", This, dwStart, dwCount);
347
348   return D3D_OK;
349 }
350
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
355 ) {
356   ICOM_THIS(IDirect3DTexture2Impl,iface);
357   D3DTPRIVATE(This);
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;
362 #if 0
363   static BOOL color_table_queried = FALSE;
364 #endif
365
366   TRACE("(%p)->(%p)\n", This, ilpD3DTexture2);
367   TRACE("Copied surface %p to surface %p\n", ilpD3DTexture2->surface, This->surface);
368
369   /* Suppress the ALLOCONLOAD flag */
370   This->surface->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
371
372   /* Copy one surface on the other */
373   dst_d = (DDSURFACEDESC *)&(This->surface->surface_desc);
374   src_d = (DDSURFACEDESC *)&(ilpD3DTexture2->surface->surface_desc);
375
376   /* Install the callbacks to the destination surface */
377   This->surface->texture = This;
378   This->surface->SetColorKey_cb = SetColorKey_cb;
379   
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;
384   } else {
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;
391
392     /* Copy the main memry texture into the surface that corresponds to the OpenGL
393        texture object. */
394     memcpy(dst_d->lpSurface, src_d->lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
395
396     ENTER_GL();
397     
398     /* Now, load the texture */
399     /* d3dd->set_context(d3dd); We need to set the context somehow.... */
400     glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
401
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);
406
407     if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
408       /* ****************
409          Paletted Texture
410          **************** */
411       IDirectDrawPaletteImpl* pal = This->surface->palette;
412       BYTE table[256][4];
413       int i;
414
415 #if 0
416       if (color_table_queried == FALSE) {
417         ptr_ColorTableEXT =
418           ((Mesa_DeviceCapabilities *) ((x11_dd_private *) This->surface->s.ddraw->d->private)->device_capabilities)->ptr_ColorTableEXT;
419       }
420 #endif
421       
422       if (pal == NULL) {
423         ERR("Palettized texture Loading with a NULL palette !\n");
424         LEAVE_GL();
425         return D3DERR_TEXTURE_LOAD_FAILED;
426       }
427
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))
436           table[i][3] = 0x00;
437         else
438         table[i][3] = 0xFF;
439       }
440       
441       /* Texture snooping */
442       SNOOP_PALETTED();
443
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 */
452         
453         glTexImage2D(GL_TEXTURE_2D,       /* target */
454                      0,                   /* level */
455                      GL_COLOR_INDEX8_EXT, /* internal format */
456                      src_d->dwWidth, src_d->dwHeight, /* width, height */
457                      0,                   /* border */
458                      GL_COLOR_INDEX,      /* texture format */
459                      GL_UNSIGNED_BYTE,    /* texture type */
460                      src_d->lpSurface); /* the texture */
461       } else {
462         DWORD *surface = (DWORD *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, src_d->dwWidth * src_d->dwHeight * sizeof(DWORD));
463         DWORD i;
464         BYTE *src = (BYTE *) src_d->lpSurface, *dst = (BYTE *) surface;
465         
466         for (i = 0; i < src_d->dwHeight * src_d->dwWidth; i++) {
467           BYTE color = *src++;
468           *dst++ = table[color][0];
469           *dst++ = table[color][1];
470           *dst++ = table[color][2];
471           *dst++ = table[color][3];
472         }
473         
474         glTexImage2D(GL_TEXTURE_2D,
475                      0,
476                      GL_RGBA,
477                      src_d->dwWidth, src_d->dwHeight,
478                      0,
479                      GL_RGBA,
480                      GL_UNSIGNED_BYTE,
481                      surface);
482         
483         HeapFree(GetProcessHeap(), 0, surface);
484       }
485     } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
486       /* ************
487          RGB Textures
488          ************ */
489       if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 8) {
490         /* **********************
491            GL_UNSIGNED_BYTE_3_3_2 
492            ********************** */
493         glTexImage2D(GL_TEXTURE_2D,
494                      0,
495                      GL_RGB,
496                      src_d->dwWidth, src_d->dwHeight,
497                      0,
498                      GL_RGB,
499                      GL_UNSIGNED_BYTE_3_3_2,
500                      src_d->lpSurface);
501       } else if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 16) {
502         if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000000) {
503             
504           /* Texture snooping */
505           SNOOP_5650();
506           
507           glTexImage2D(GL_TEXTURE_2D,
508                        0,
509                        GL_RGB,
510                        src_d->dwWidth, src_d->dwHeight,
511                        0,
512                        GL_RGB,
513                        GL_UNSIGNED_SHORT_5_6_5,
514                        src_d->lpSurface);
515         } else if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x00000001) {
516           /* Texture snooping */
517           SNOOP_5551();
518           
519           glTexImage2D(GL_TEXTURE_2D,
520                        0,
521                        GL_RGBA,
522                        src_d->dwWidth, src_d->dwHeight,
523                        0,
524                        GL_RGBA,
525                        GL_UNSIGNED_SHORT_5_5_5_1,
526                        src_d->lpSurface);
527         } else if (src_d->ddpfPixelFormat.u5.dwRGBAlphaBitMask == 0x0000000F) {
528           glTexImage2D(GL_TEXTURE_2D,
529                        0,
530                        GL_RGBA,
531                        src_d->dwWidth, src_d->dwHeight,
532                        0,
533                        GL_RGBA,
534                        GL_UNSIGNED_SHORT_4_4_4_4,
535                        src_d->lpSurface);
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));
539           DWORD i;
540           WORD *src = (WORD *) src_d->lpSurface, *dst = surface;
541
542           for (i = 0; i < src_d->dwHeight * src_d->dwWidth; i++) {
543             *dst++ = (((*src & 0x8000) >> 15) |
544                       ((*src & 0x7FFF) <<  1));
545             src++;
546           }
547           
548           glTexImage2D(GL_TEXTURE_2D,
549                        0,
550                        GL_RGBA,
551                        src_d->dwWidth, src_d->dwHeight,
552                        0,
553                        GL_RGBA,
554                        GL_UNSIGNED_SHORT_5_5_5_1,
555                        surface);
556           
557           HeapFree(GetProcessHeap(), 0, surface);         
558         } else {
559           ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
560         }
561       } else if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 24) {
562         glTexImage2D(GL_TEXTURE_2D,
563                      0,
564                      GL_RGB,
565                      src_d->dwWidth, src_d->dwHeight,
566                      0,
567                      GL_RGB,
568                      GL_UNSIGNED_BYTE,
569                      src_d->lpSurface);
570       } else if (src_d->ddpfPixelFormat.u1.dwRGBBitCount == 32) {
571         glTexImage2D(GL_TEXTURE_2D,
572                      0,
573                      GL_RGBA,
574                      src_d->dwWidth, src_d->dwHeight,
575                      0,
576                      GL_RGBA,
577                      GL_UNSIGNED_BYTE,
578                      src_d->lpSurface);
579       } else {
580         ERR("Unhandled texture format (bad RGB count)\n");
581       }
582     } else {
583       ERR("Unhandled texture format (neither RGB nor INDEX)\n");
584     }
585
586     glBindTexture(GL_TEXTURE_2D, current_texture);
587
588     LEAVE_GL();
589   }
590   
591   return D3D_OK;
592 }
593
594
595 /*******************************************************************************
596  *                              IDirect3DTexture2 VTable
597  */
598 ICOM_VTABLE(IDirect3DTexture2) mesa_texture2_vtable = 
599 {
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
609 };
610
611 /*******************************************************************************
612  *                              IDirect3DTexture VTable
613  */
614 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
615 # define XCAST(fun)     (typeof(mesa_texture_vtable.fun))
616 #else
617 # define XCAST(fun)     (void*)
618 #endif
619
620 ICOM_VTABLE(IDirect3DTexture) mesa_texture_vtable = 
621 {
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
633 };
634
635 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
636 #undef XCAST
637 #endif