Replaced references to interfaces.h by wine/obj_xxx.h headers instead
[wine] / graphics / d3dtexture.c
1 /* Direct3D Texture
2    (c) 1998 Lionel ULMER
3    
4    This files contains the implementation of interface Direct3DTexture2. */
5
6
7 #include "config.h"
8 #include "windows.h"
9 #include "wintypes.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"
16
17 #include "d3d_private.h"
18
19 #ifdef HAVE_MESAGL
20
21 /* Define this if you want to save to a file all the textures used by a game
22    (can be funny to see how they managed to cram all the pictures in
23    texture memory) */
24 #undef TEXTURE_SNOOP
25
26 static IDirect3DTexture2_VTable texture2_vtable;
27 static IDirect3DTexture_VTable texture_vtable;
28
29 /*******************************************************************************
30  *                              Texture2 Creation functions
31  */
32 LPDIRECT3DTEXTURE2 d3dtexture2_create(LPDIRECTDRAWSURFACE4 surf)
33 {
34   LPDIRECT3DTEXTURE2 mat;
35   
36   mat = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2));
37   mat->ref = 1;
38   mat->lpvtbl = &texture2_vtable;
39   mat->surface = surf;
40   
41   return mat;
42 }
43
44 /*******************************************************************************
45  *                              Texture Creation functions
46  */
47 LPDIRECT3DTEXTURE d3dtexture_create(LPDIRECTDRAWSURFACE4 surf)
48 {
49   LPDIRECT3DTEXTURE mat;
50   
51   mat = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture));
52   mat->ref = 1;
53   mat->lpvtbl = (IDirect3DTexture2_VTable*) &texture_vtable;
54   mat->surface = surf;
55   
56   return mat;
57 }
58
59
60 /*******************************************************************************
61  *                              IDirect3DTexture2 methods
62  */
63
64 static HRESULT WINAPI IDirect3DTexture2_QueryInterface(LPDIRECT3DTEXTURE2 this,
65                                                         REFIID riid,
66                                                         LPVOID* ppvObj)
67 {
68   char xrefiid[50];
69   
70   WINE_StringFromCLSID((LPCLSID)riid,xrefiid);
71   FIXME(ddraw, "(%p)->(%s,%p): stub\n", this, xrefiid,ppvObj);
72   
73   return S_OK;
74 }
75
76
77
78 static ULONG WINAPI IDirect3DTexture2_AddRef(LPDIRECT3DTEXTURE2 this)
79 {
80   TRACE(ddraw, "(%p)->()incrementing from %lu.\n", this, this->ref );
81   
82   return ++(this->ref);
83 }
84
85
86
87 static ULONG WINAPI IDirect3DTexture2_Release(LPDIRECT3DTEXTURE2 this)
88 {
89   FIXME( ddraw, "(%p)->() decrementing from %lu.\n", this, this->ref );
90   
91   if (!--(this->ref)) {
92     /* Delete texture from OpenGL */
93     glDeleteTextures(1, &(this->tex_name));
94     
95     /* Release surface */
96     this->surface->lpvtbl->fnRelease(this->surface);
97     
98     HeapFree(GetProcessHeap(),0,this);
99     return 0;
100   }
101   
102   return this->ref;
103 }
104
105 /*** IDirect3DTexture methods ***/
106 static HRESULT WINAPI IDirect3DTexture_GetHandle(LPDIRECT3DTEXTURE this,
107                                                  LPDIRECT3DDEVICE lpD3DDevice,
108                                                  LPD3DTEXTUREHANDLE lpHandle)
109 {
110   FIXME(ddraw, "(%p)->(%p,%p): stub\n", this, lpD3DDevice, lpHandle);
111
112   *lpHandle = (DWORD) this;
113   
114   /* Now, bind a new texture */
115   lpD3DDevice->set_context(lpD3DDevice);
116   this->D3Ddevice = (void *) lpD3DDevice;
117   if (this->tex_name == 0)
118     glGenTextures(1, &(this->tex_name));
119
120   TRACE(ddraw, "OpenGL texture handle is : %d\n", this->tex_name);
121   
122   return D3D_OK;
123 }
124
125 static HRESULT WINAPI IDirect3DTexture_Initialize(LPDIRECT3DTEXTURE this,
126                                                   LPDIRECT3DDEVICE lpD3DDevice,
127                                                   LPDIRECTDRAWSURFACE lpSurface)
128 {
129   TRACE(ddraw, "(%p)->(%p,%p)\n", this, lpD3DDevice, lpSurface);
130
131   return DDERR_ALREADYINITIALIZED;
132 }
133
134 static HRESULT WINAPI IDirect3DTexture_Unload(LPDIRECT3DTEXTURE this)
135 {
136   FIXME(ddraw, "(%p)->(): stub\n", this);
137
138   return D3D_OK;
139 }
140
141 /*** IDirect3DTexture2 methods ***/
142 static HRESULT WINAPI IDirect3DTexture2_GetHandle(LPDIRECT3DTEXTURE2 this,
143                                                   LPDIRECT3DDEVICE2 lpD3DDevice2,
144                                                   LPD3DTEXTUREHANDLE lpHandle)
145 {
146   TRACE(ddraw, "(%p)->(%p,%p)\n", this, lpD3DDevice2, lpHandle);
147
148   /* For 32 bits OSes, handles = pointers */
149   *lpHandle = (DWORD) this;
150   
151   /* Now, bind a new texture */
152   lpD3DDevice2->set_context(lpD3DDevice2);
153   this->D3Ddevice = (void *) lpD3DDevice2;
154   if (this->tex_name == 0)
155   glGenTextures(1, &(this->tex_name));
156
157   TRACE(ddraw, "OpenGL texture handle is : %d\n", this->tex_name);
158   
159   return D3D_OK;
160 }
161
162 /* Common methods */
163 static HRESULT WINAPI IDirect3DTexture2_PaletteChanged(LPDIRECT3DTEXTURE2 this,
164                                                        DWORD dwStart,
165                                                        DWORD dwCount)
166 {
167   FIXME(ddraw, "(%p)->(%8ld,%8ld): stub\n", this, dwStart, dwCount);
168
169   return D3D_OK;
170 }
171
172 /* NOTE : if you experience crashes in this function, you must have a buggy
173           version of Mesa. See the file d3dtexture.c for a cure */
174 static HRESULT WINAPI IDirect3DTexture2_Load(LPDIRECT3DTEXTURE2 this,
175                                              LPDIRECT3DTEXTURE2 lpD3DTexture2)
176 {
177   DDSURFACEDESC *src_d, *dst_d;
178   TRACE(ddraw, "(%p)->(%p)\n", this, lpD3DTexture2);
179
180   TRACE(ddraw, "Copied to surface %p, surface %p\n", this->surface, lpD3DTexture2->surface);
181
182   /* Suppress the ALLOCONLOAD flag */
183   this->surface->s.surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
184
185   /* Copy one surface on the other */
186   dst_d = &(this->surface->s.surface_desc);
187   src_d = &(lpD3DTexture2->surface->s.surface_desc);
188
189   if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
190     /* Should also check for same pixel format, lPitch, ... */
191     ERR(ddraw, "Error in surface sizes\n");
192     return D3DERR_TEXTURE_LOAD_FAILED;
193   } else {
194     /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) this->D3Ddevice; */
195     /* I should put a macro for the calculus of bpp */
196     int bpp = (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
197                1 /* 8 bit of palette index */:
198                src_d->ddpfPixelFormat.x.dwRGBBitCount / 8 /* RGB bits for each colors */ );
199     GLuint current_texture;
200
201     /* Not sure if this is usefull ! */
202     memcpy(dst_d->y.lpSurface, src_d->y.lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
203
204     /* Now, load the texture */
205     /* d3dd->set_context(d3dd); We need to set the context somehow.... */
206     glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
207
208     /* If the GetHandle was not done, get the texture name here */
209     if (this->tex_name == 0)
210       glGenTextures(1, &(this->tex_name));
211     glBindTexture(GL_TEXTURE_2D, this->tex_name);
212
213     if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
214       /* ****************
215          Paletted Texture
216          **************** */
217       LPDIRECTDRAWPALETTE pal = this->surface->s.palette;
218       BYTE table[256][4];
219       int i;
220       
221       if (pal == NULL) {
222         ERR(ddraw, "Palettized texture Loading with a NULL palette !\n");
223         return D3DERR_TEXTURE_LOAD_FAILED;
224       }
225
226       /* Get the surface's palette */
227       for (i = 0; i < 256; i++) {
228         table[i][0] = pal->palents[i].peRed;
229         table[i][1] = pal->palents[i].peGreen;
230         table[i][2] = pal->palents[i].peBlue;
231         if ((this->surface->s.surface_desc.dwFlags & DDSD_CKSRCBLT) &&
232             (i >= this->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue) &&
233             (i <= this->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue))
234           table[i][3] = 0x00;
235         else
236         table[i][3] = 0xFF;
237       }
238       
239 #ifdef TEXTURE_SNOOP
240       {
241         FILE *f;
242         char buf[32];
243         int x, y;
244         
245         sprintf(buf, "%d.pnm", this->tex_name);
246         f = fopen(buf, "wb");
247         fprintf(f, "P6\n%d %d\n255\n", src_d->dwWidth, src_d->dwHeight);
248         for (y = 0; y < src_d->dwHeight; y++) {
249           for (x = 0; x < src_d->dwWidth; x++) {
250             unsigned char c = ((unsigned char *) src_d->y.lpSurface)[y * src_d->dwWidth + x];
251             fputc(table[c][0], f);
252             fputc(table[c][1], f);
253             fputc(table[c][2], f);
254           }
255         }
256         fclose(f);
257       }
258 #endif 
259       /* Use Paletted Texture Extension */
260       glColorTableEXT(GL_TEXTURE_2D,    /* target */
261                       GL_RGBA,          /* internal format */
262                       256,              /* table size */
263                       GL_RGBA,          /* table format */
264                       GL_UNSIGNED_BYTE, /* table type */
265                       table);           /* the color table */
266
267       glTexImage2D(GL_TEXTURE_2D,       /* target */
268                    0,                   /* level */
269                    GL_COLOR_INDEX8_EXT, /* internal format */
270                    src_d->dwWidth, src_d->dwHeight, /* width, height */
271                    0,                   /* border */
272                    GL_COLOR_INDEX,      /* texture format */
273                    GL_UNSIGNED_BYTE,    /* texture type */
274                    src_d->y.lpSurface); /* the texture */
275     } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
276       /* ************
277          RGB Textures
278          ************ */
279       if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 8) {
280         /* **********************
281            GL_UNSIGNED_BYTE_3_3_2 
282            ********************** */
283         glTexImage2D(GL_TEXTURE_2D,
284                      0,
285                      GL_RGB,
286                      src_d->dwWidth, src_d->dwHeight,
287                      0,
288                      GL_RGB,
289                      GL_UNSIGNED_BYTE_3_3_2,
290                      src_d->y.lpSurface);
291       } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 16) {
292         if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000000) {
293 #ifdef TEXTURE_SNOOP
294           {
295             FILE *f;
296             char buf[32];
297             int x, y;
298             
299             sprintf(buf, "%d.pnm", this->tex_name);
300             f = fopen(buf, "wb");
301             fprintf(f, "P6\n%d %d\n255\n", src_d->dwWidth, src_d->dwHeight);
302             for (y = 0; y < src_d->dwHeight; y++) {
303               for (x = 0; x < src_d->dwWidth; x++) {
304                 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x];
305                 fputc((c & 0xF800) >> 8, f);
306                 fputc((c & 0x07E0) >> 3, f);
307                 fputc((c & 0x001F) << 3, f);
308               }
309             }
310             fclose(f);
311           }
312 #endif
313           glTexImage2D(GL_TEXTURE_2D,
314                        0,
315                        GL_RGB,
316                        src_d->dwWidth, src_d->dwHeight,
317                        0,
318                        GL_RGB,
319                        GL_UNSIGNED_SHORT_5_6_5,
320                        src_d->y.lpSurface);
321         } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000001) {
322 #ifdef TEXTURE_SNOOP
323           {
324             FILE *f;
325             char buf[32];
326             int x, y;
327             
328             sprintf(buf, "%d.pnm", this->tex_name);
329             f = fopen(buf, "wb");
330             fprintf(f, "P6\n%d %d\n255\n", src_d->dwWidth, src_d->dwHeight);
331             for (y = 0; y < src_d->dwHeight; y++) {
332               for (x = 0; x < src_d->dwWidth; x++) {
333                 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x];
334                 fputc((c & 0xF800) >> 8, f);
335                 fputc((c & 0x07C0) >> 3, f);
336                 fputc((c & 0x003E) << 2, f);
337               }
338             }
339             fclose(f);
340           }
341 #endif
342           
343           glTexImage2D(GL_TEXTURE_2D,
344                        0,
345                        GL_RGBA,
346                        src_d->dwWidth, src_d->dwHeight,
347                        0,
348                        GL_RGBA,
349                        GL_UNSIGNED_SHORT_5_5_5_1,
350                        src_d->y.lpSurface);
351         } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x0000000F) {
352           glTexImage2D(GL_TEXTURE_2D,
353                        0,
354                        GL_RGBA,
355                        src_d->dwWidth, src_d->dwHeight,
356                        0,
357                        GL_RGBA,
358                        GL_UNSIGNED_SHORT_4_4_4_4,
359                        src_d->y.lpSurface);
360         } else {
361           ERR(ddraw, "Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
362         }
363       } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 24) {
364         glTexImage2D(GL_TEXTURE_2D,
365                      0,
366                      GL_RGB,
367                      src_d->dwWidth, src_d->dwHeight,
368                      0,
369                      GL_RGB,
370                      GL_UNSIGNED_BYTE,
371                      src_d->y.lpSurface);
372       } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 32) {
373         glTexImage2D(GL_TEXTURE_2D,
374                      0,
375                      GL_RGBA,
376                      src_d->dwWidth, src_d->dwHeight,
377                      0,
378                      GL_RGBA,
379                      GL_UNSIGNED_BYTE,
380                      src_d->y.lpSurface);
381       } else {
382         ERR(ddraw, "Unhandled texture format (bad RGB count)\n");
383       }
384     } else {
385       ERR(ddraw, "Unhandled texture format (neither RGB nor INDEX)\n");
386     }
387
388     glBindTexture(GL_TEXTURE_2D, current_texture);
389   }
390   
391   return D3D_OK;
392 }
393
394
395 /*******************************************************************************
396  *                              IDirect3DTexture2 VTable
397  */
398 static IDirect3DTexture2_VTable texture2_vtable = {
399   /*** IUnknown methods ***/
400   IDirect3DTexture2_QueryInterface,
401   IDirect3DTexture2_AddRef,
402   IDirect3DTexture2_Release,
403   /*** IDirect3DTexture methods ***/
404   IDirect3DTexture2_GetHandle,
405   IDirect3DTexture2_PaletteChanged,
406   IDirect3DTexture2_Load
407 };
408
409 /*******************************************************************************
410  *                              IDirect3DTexture VTable
411  */
412 static IDirect3DTexture_VTable texture_vtable = {
413   /*** IUnknown methods ***/
414   IDirect3DTexture2_QueryInterface,
415   IDirect3DTexture2_AddRef,
416   IDirect3DTexture2_Release,
417   /*** IDirect3DTexture methods ***/
418   IDirect3DTexture_Initialize,
419   IDirect3DTexture_GetHandle,
420   IDirect3DTexture2_PaletteChanged,
421   IDirect3DTexture2_Load,
422   IDirect3DTexture_Unload
423 };
424
425 #else /* HAVE_MESAGL */
426
427 /* These function should never be called if MesaGL is not present */
428 LPDIRECT3DTEXTURE2 d3dtexture2_create(LPDIRECTDRAWSURFACE4 surf) {
429   ERR(ddraw, "Should not be called...\n");
430   return NULL;
431 }
432
433 LPDIRECT3DTEXTURE d3dtexture_create(LPDIRECTDRAWSURFACE4 surf) {
434   ERR(ddraw, "Should not be called...\n");
435   return NULL;
436 }
437
438 #endif /* HAVE_MESAGL */