Do not kill the animation thread with TerminateThread, let it finish
[wine] / dlls / ddraw / d3dtexture.c
1 /* Direct3D Texture
2  * Copyright (c) 1998 Lionel ULMER
3  *
4  * This file contains the implementation of interface Direct3DTexture2.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <string.h>
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "objbase.h"
32 #include "wingdi.h"
33 #include "ddraw.h"
34 #include "d3d.h"
35 #include "wine/debug.h"
36
37 #include "mesa_private.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
40 WINE_DECLARE_DEBUG_CHANNEL(ddraw_tex);
41
42 #include <stdio.h>
43
44 static void 
45 snoop_texture(IDirectDrawSurfaceImpl *This) {
46     IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
47     char buf[128];
48     FILE *f;
49
50     TRACE_(ddraw_tex)("Dumping surface id (%5d) level (%2d) : \n", glThis->tex_name, This->mipmap_level);
51     DDRAW_dump_surface_desc(&(This->surface_desc));
52     
53     sprintf(buf, "tex_%05d_%02d.pnm", glThis->tex_name, This->mipmap_level);
54     f = fopen(buf, "wb");
55     DDRAW_dump_surface_to_disk(This, f);
56 }
57
58 static IDirectDrawSurfaceImpl *
59 get_sub_mimaplevel(IDirectDrawSurfaceImpl *tex_ptr)
60 {
61     /* Now go down the mipmap chain to the next surface */
62     static const DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
63     LPDIRECTDRAWSURFACE7 next_level;
64     IDirectDrawSurfaceImpl *surf_ptr;
65     HRESULT hr;
66     
67     hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(tex_ptr, IDirectDrawSurface7),
68                                                 (DDSCAPS2 *) &mipmap_caps, &next_level);
69     if (FAILED(hr)) return NULL;
70     
71     surf_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, next_level);
72     IDirectDrawSurface7_Release(next_level);
73     
74     return surf_ptr;
75 }
76
77 /*******************************************************************************
78  *                         IDirectSurface callback methods
79  */
80
81 HRESULT
82 gltex_download_texture(IDirectDrawSurfaceImpl *surf_ptr) {
83     IDirect3DTextureGLImpl *gl_surf_ptr = (IDirect3DTextureGLImpl *) surf_ptr->tex_private;
84
85     FIXME("This is not supported yet... Expect some graphical glitches !!!\n");
86
87     /* GL and memory are in sync again ... 
88        No need to change the 'global' flag as it only handles the 'MEMORY_DIRTY' case.
89     */
90     gl_surf_ptr->dirty_flag = SURFACE_MEMORY;
91     
92     return DD_OK;
93 }
94
95 static GLenum
96 convert_min_filter_to_GL(D3DTEXTUREMINFILTER dwMinState, D3DTEXTUREMIPFILTER dwMipState)
97 {
98     GLenum gl_state;
99
100     if (dwMipState == D3DTFP_NONE) {
101         switch (dwMinState) {
102             case D3DTFN_POINT:  gl_state = GL_NEAREST; break;
103             case D3DTFN_LINEAR: gl_state = GL_LINEAR;  break;
104             default:            gl_state = GL_LINEAR;  break;
105         }
106     } else if (dwMipState == D3DTFP_POINT) {
107         switch (dwMinState) {
108             case D3DTFN_POINT:  gl_state = GL_NEAREST_MIPMAP_NEAREST; break;
109             case D3DTFN_LINEAR: gl_state = GL_LINEAR_MIPMAP_NEAREST;  break;
110             default:            gl_state = GL_LINEAR_MIPMAP_NEAREST;  break;
111         }
112     } else {
113         switch (dwMinState) {
114             case D3DTFN_POINT:  gl_state = GL_NEAREST_MIPMAP_LINEAR; break;
115             case D3DTFN_LINEAR: gl_state = GL_LINEAR_MIPMAP_LINEAR;  break;
116             default:            gl_state = GL_LINEAR_MIPMAP_LINEAR;  break;
117         }
118     }
119     return gl_state;
120 }
121
122 static GLenum
123 convert_mag_filter_to_GL(D3DTEXTUREMAGFILTER dwState)
124 {
125     GLenum gl_state;
126
127     switch (dwState) {
128         case D3DTFG_POINT:
129             gl_state = GL_NEAREST;
130             break;
131         case D3DTFG_LINEAR:
132             gl_state = GL_LINEAR;
133             break;
134         default:
135             gl_state = GL_LINEAR;
136             break;
137     }
138     return gl_state;
139 }
140
141 static GLenum
142 convert_tex_address_to_GL(D3DTEXTUREADDRESS dwState)
143 {
144     GLenum gl_state;
145     switch (dwState) {
146         case D3DTADDRESS_WRAP:   gl_state = GL_REPEAT; break;
147         case D3DTADDRESS_CLAMP:  gl_state = GL_CLAMP; break;
148         case D3DTADDRESS_BORDER: gl_state = GL_CLAMP_TO_EDGE; break;
149         case D3DTADDRESS_MIRROR:
150             if (GL_extensions.mirrored_repeat == TRUE) {
151                 gl_state = GL_MIRRORED_REPEAT_WINE;
152             } else {
153                 gl_state = GL_REPEAT;
154                 /* This is a TRACE instead of a FIXME as the FIXME was already printed when the game
155                    actually set D3DTADDRESS_MIRROR.
156                 */
157                 TRACE(" setting GL_REPEAT instead of GL_MIRRORED_REPEAT.\n");
158             }
159         break;
160         default:                 gl_state = GL_REPEAT; break;
161     }
162     return gl_state;
163 }
164
165 HRESULT
166 gltex_upload_texture(IDirectDrawSurfaceImpl *surf_ptr, IDirect3DDeviceImpl *d3ddev, DWORD stage) {
167     IDirect3DTextureGLImpl *gl_surf_ptr = (IDirect3DTextureGLImpl *) surf_ptr->tex_private;
168     IDirect3DDeviceGLImpl *gl_d3ddev = (IDirect3DDeviceGLImpl *) d3ddev;
169     BOOLEAN changed = FALSE;
170     GLenum unit = GL_TEXTURE0_WINE + stage;
171     
172     if (surf_ptr->mipmap_level != 0) {
173         WARN(" application activating a sub-level of the mipmapping chain (level %d) !\n", surf_ptr->mipmap_level);
174     }
175
176     /* Now check if the texture parameters for this texture are still in-line with what D3D expect
177        us to do..
178
179        NOTE: there is no check for the situation where the same texture is bound to multiple stage
180              but with different parameters per stage.
181     */
182     if ((gl_surf_ptr->tex_parameters == NULL) ||
183         (gl_surf_ptr->tex_parameters[D3DTSS_MAXMIPLEVEL - D3DTSS_ADDRESSU] != 
184          d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAXMIPLEVEL - 1])) {
185         DWORD max_mip_level;
186         
187         if ((surf_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) == 0) {
188             max_mip_level = 0;
189         } else {
190             max_mip_level = surf_ptr->surface_desc.u2.dwMipMapCount - 1;
191             if (d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAXMIPLEVEL - 1] != 0) {
192                 if (max_mip_level >= d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAXMIPLEVEL - 1]) {
193                     max_mip_level = d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAXMIPLEVEL - 1] - 1;
194                 }
195             }
196         }
197         if (unit != gl_d3ddev->current_active_tex_unit) {
198             GL_extensions.glActiveTexture(unit);
199             gl_d3ddev->current_active_tex_unit = unit;
200         }
201         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, max_mip_level);
202         changed = TRUE;
203     }
204     
205     if ((gl_surf_ptr->tex_parameters == NULL) ||
206         (gl_surf_ptr->tex_parameters[D3DTSS_MAGFILTER - D3DTSS_ADDRESSU] != 
207          d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAGFILTER - 1])) {
208         if (unit != gl_d3ddev->current_active_tex_unit) {
209             GL_extensions.glActiveTexture(unit);
210             gl_d3ddev->current_active_tex_unit = unit;
211         }
212         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
213                         convert_mag_filter_to_GL(d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAGFILTER - 1]));
214         changed = TRUE;
215     }
216     if ((gl_surf_ptr->tex_parameters == NULL) ||
217         (gl_surf_ptr->tex_parameters[D3DTSS_MINFILTER - D3DTSS_ADDRESSU] != 
218          d3ddev->state_block.texture_stage_state[stage][D3DTSS_MINFILTER - 1]) ||
219         (gl_surf_ptr->tex_parameters[D3DTSS_MIPFILTER - D3DTSS_ADDRESSU] != 
220          d3ddev->state_block.texture_stage_state[stage][D3DTSS_MIPFILTER - 1])) {
221         if (unit != gl_d3ddev->current_active_tex_unit) {
222             GL_extensions.glActiveTexture(unit);
223             gl_d3ddev->current_active_tex_unit = unit;
224         }
225         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
226                         convert_min_filter_to_GL(d3ddev->state_block.texture_stage_state[stage][D3DTSS_MINFILTER - 1],
227                                                  d3ddev->state_block.texture_stage_state[stage][D3DTSS_MIPFILTER - 1]));
228         changed = TRUE;
229     }
230     if ((gl_surf_ptr->tex_parameters == NULL) ||
231         (gl_surf_ptr->tex_parameters[D3DTSS_ADDRESSU - D3DTSS_ADDRESSU] != 
232          d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSU - 1])) {
233         if (unit != gl_d3ddev->current_active_tex_unit) {
234             GL_extensions.glActiveTexture(unit);
235             gl_d3ddev->current_active_tex_unit = unit;
236         }
237         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
238                         convert_tex_address_to_GL(d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSU - 1]));
239         changed = TRUE;
240     }
241     if ((gl_surf_ptr->tex_parameters == NULL) ||
242         (gl_surf_ptr->tex_parameters[D3DTSS_ADDRESSV - D3DTSS_ADDRESSU] != 
243          d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSV - 1])) {
244         if (unit != gl_d3ddev->current_active_tex_unit) {
245             GL_extensions.glActiveTexture(unit);
246             gl_d3ddev->current_active_tex_unit = unit;
247         }
248         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
249                         convert_tex_address_to_GL(d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSV - 1]));        
250         changed = TRUE;
251     }
252     if ((gl_surf_ptr->tex_parameters == NULL) ||
253         (gl_surf_ptr->tex_parameters[D3DTSS_BORDERCOLOR - D3DTSS_ADDRESSU] != 
254          d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1])) {
255         GLfloat color[4];
256         
257         color[0] = ((d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1] >> 16) & 0xFF) / 255.0;
258         color[1] = ((d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1] >>  8) & 0xFF) / 255.0;
259         color[2] = ((d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1] >>  0) & 0xFF) / 255.0;
260         color[3] = ((d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1] >> 24) & 0xFF) / 255.0;
261         if (unit != gl_d3ddev->current_active_tex_unit) {
262             GL_extensions.glActiveTexture(unit);
263             gl_d3ddev->current_active_tex_unit = unit;
264         }
265         glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color);
266         changed = TRUE;
267     }
268
269     if (changed == TRUE) {
270         if (gl_surf_ptr->tex_parameters == NULL) {
271             gl_surf_ptr->tex_parameters = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
272                                                     sizeof(DWORD) * (D3DTSS_MAXMIPLEVEL + 1 - D3DTSS_ADDRESSU));
273         }
274         memcpy(gl_surf_ptr->tex_parameters, &(d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSU - 1]),
275                sizeof(DWORD) * (D3DTSS_MAXMIPLEVEL + 1 - D3DTSS_ADDRESSU));
276     }
277
278     if (*(gl_surf_ptr->global_dirty_flag) != SURFACE_MEMORY_DIRTY) {
279         TRACE(" nothing to do - memory copy and GL state in synch for all texture levels.\n");
280         return DD_OK;
281     }
282     
283     while (surf_ptr != NULL) {
284         IDirect3DTextureGLImpl *gl_surf_ptr = (IDirect3DTextureGLImpl *) surf_ptr->tex_private;
285         
286         if (gl_surf_ptr->dirty_flag != SURFACE_MEMORY_DIRTY) {
287             TRACE("   - level %d already uploaded.\n", surf_ptr->mipmap_level);
288         } else {
289             TRACE("   - uploading texture level %d (initial done = %d).\n",
290                   surf_ptr->mipmap_level, gl_surf_ptr->initial_upload_done);
291
292             /* Texture snooping for the curious :-) */
293             if (TRACE_ON(ddraw_tex)) {
294                 snoop_texture(surf_ptr);
295             }
296
297             if (unit != gl_d3ddev->current_active_tex_unit) {
298                 GL_extensions.glActiveTexture(unit);
299                 gl_d3ddev->current_active_tex_unit = unit;
300             }
301             
302             if (upload_surface_to_tex_memory_init(surf_ptr, surf_ptr->mipmap_level, &(gl_surf_ptr->current_internal_format),
303                                                   gl_surf_ptr->initial_upload_done == FALSE, TRUE, 0, 0) == DD_OK) {
304                 upload_surface_to_tex_memory(NULL, 0, 0, &(gl_surf_ptr->surface_ptr));
305                 upload_surface_to_tex_memory_release();
306                 gl_surf_ptr->dirty_flag = SURFACE_MEMORY;
307                 gl_surf_ptr->initial_upload_done = TRUE;
308             } else {
309                 ERR("Problem for upload of texture %d (level = %d / initial done = %d).\n",
310                     gl_surf_ptr->tex_name, surf_ptr->mipmap_level, gl_surf_ptr->initial_upload_done);
311             }
312         }
313         
314         if (surf_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) {
315             surf_ptr = get_sub_mimaplevel(surf_ptr);
316         } else {
317             surf_ptr = NULL;
318         }
319     }
320     
321     *(gl_surf_ptr->global_dirty_flag) = SURFACE_MEMORY;
322     
323     return DD_OK;
324 }
325
326 HRESULT WINAPI
327 Main_IDirect3DTextureImpl_1_Initialize(LPDIRECT3DTEXTURE iface,
328                                        LPDIRECT3DDEVICE lpDirect3DDevice,
329                                        LPDIRECTDRAWSURFACE lpDDSurface)
330 {
331     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
332     FIXME("(%p/%p)->(%p,%p) no-op...\n", This, iface, lpDirect3DDevice, lpDDSurface);
333     return DD_OK;
334 }
335
336 static HRESULT
337 gltex_setcolorkey_cb(IDirectDrawSurfaceImpl *This, DWORD dwFlags, LPDDCOLORKEY ckey )
338 {
339     IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
340
341     if (glThis->dirty_flag == SURFACE_GL) {
342         GLuint cur_tex;
343
344         TRACE(" flushing GL texture back to memory.\n");
345         
346         ENTER_GL();
347         glGetIntegerv(GL_TEXTURE_BINDING_2D, &cur_tex);
348         glBindTexture(GL_TEXTURE_2D, glThis->tex_name);
349         gltex_download_texture(This);
350         glBindTexture(GL_TEXTURE_2D, cur_tex);
351         LEAVE_GL();
352     }
353
354     glThis->dirty_flag = SURFACE_MEMORY_DIRTY;
355     *(glThis->global_dirty_flag) = SURFACE_MEMORY_DIRTY;
356     /* TODO: check color-keying on mipmapped surfaces... */
357     
358     return DD_OK;
359 }
360
361 HRESULT
362 gltex_blt(IDirectDrawSurfaceImpl *This, LPRECT rdst,
363           LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
364           DWORD dwFlags, LPDDBLTFX lpbltfx)
365 {
366     if (src != NULL) {
367         IDirectDrawSurfaceImpl *src_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, src);
368         if (src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE) {
369             FIXME("Blt from framebuffer to texture - unsupported now, please report !\n");
370         }
371     }
372     return DDERR_INVALIDPARAMS;
373 }
374
375 HRESULT
376 gltex_bltfast(IDirectDrawSurfaceImpl *surf_ptr, DWORD dstx,
377               DWORD dsty, LPDIRECTDRAWSURFACE7 src,
378               LPRECT rsrc, DWORD trans)
379 {
380     if (src != NULL) {
381         IDirectDrawSurfaceImpl *src_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, src);
382         
383         if ((src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE) &&
384             ((trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) == 0)) {
385             /* This is a blt without color keying... We can use the direct copy. */
386             RECT rsrc2;
387             DWORD width, height;
388             GLuint cur_tex;
389             IDirect3DTextureGLImpl *gl_surf_ptr = surf_ptr->tex_private;
390             int y;
391             
392             if (rsrc == NULL) {
393                 WARN("rsrc is NULL\n");
394                 rsrc = &rsrc2;
395                 
396                 rsrc->left = 0;
397                 rsrc->top = 0;
398                 rsrc->right = src_ptr->surface_desc.dwWidth;
399                 rsrc->bottom = src_ptr->surface_desc.dwHeight;
400             }
401             
402             width = rsrc->right - rsrc->left;
403             height = rsrc->bottom - rsrc->top;
404             
405             if (((dstx + width)  > surf_ptr->surface_desc.dwWidth) ||
406                 ((dsty + height) > surf_ptr->surface_desc.dwHeight)) {
407                 FIXME("Does not handle clipping yet in FB => Texture blits !\n");
408                 return DDERR_INVALIDPARAMS;
409             }
410
411             if ((width == 0) || (height == 0)) {
412                 return DD_OK;
413             }
414             
415             TRACE(" direct frame buffer => texture BltFast override.\n");
416             
417             ENTER_GL();
418             
419             glGetIntegerv(GL_TEXTURE_BINDING_2D, &cur_tex);
420             glBindTexture(GL_TEXTURE_2D, gl_surf_ptr->tex_name);
421             
422             if ((gl_surf_ptr->dirty_flag == SURFACE_MEMORY_DIRTY) &&
423                 !((dstx == 0) && (dsty == 0) &&
424                   (width == surf_ptr->surface_desc.dwWidth) && (height == surf_ptr->surface_desc.dwHeight))) {
425                 /* If not 'full size' and the surface is dirty, first flush it to GL before doing the copy. */
426                 if (upload_surface_to_tex_memory_init(surf_ptr, surf_ptr->mipmap_level, &(gl_surf_ptr->current_internal_format),
427                                                       gl_surf_ptr->initial_upload_done == FALSE, TRUE, 0, 0) == DD_OK) {
428                     upload_surface_to_tex_memory(NULL, 0, 0, &(gl_surf_ptr->surface_ptr));
429                     upload_surface_to_tex_memory_release();
430                     gl_surf_ptr->dirty_flag = SURFACE_MEMORY;
431                     gl_surf_ptr->initial_upload_done = TRUE;
432                 } else {
433                     glBindTexture(GL_TEXTURE_2D, cur_tex);
434                     LEAVE_GL();
435                     ERR("Error at texture upload !\n");
436                     return DDERR_INVALIDPARAMS;
437                 }
438             }
439
440             /* This is a hack and would need some clean-up :-) */
441             if (gl_surf_ptr->initial_upload_done == FALSE) {
442                 if (upload_surface_to_tex_memory_init(surf_ptr, surf_ptr->mipmap_level, &(gl_surf_ptr->current_internal_format),
443                                                       TRUE, TRUE, 0, 0) == DD_OK) {
444                     upload_surface_to_tex_memory(NULL, 0, 0, &(gl_surf_ptr->surface_ptr));
445                     upload_surface_to_tex_memory_release();
446                     gl_surf_ptr->dirty_flag = SURFACE_MEMORY;
447                     gl_surf_ptr->initial_upload_done = TRUE;
448                 } else {
449                     glBindTexture(GL_TEXTURE_2D, cur_tex);
450                     LEAVE_GL();
451                     ERR("Error at texture upload (initial case) !\n");
452                     return DDERR_INVALIDPARAMS;
453                 }
454             }
455             
456             if ((src_ptr->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER|DDSCAPS_PRIMARYSURFACE)) != 0)
457                 glReadBuffer(GL_FRONT);
458             else if ((src_ptr->surface_desc.ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER)) == (DDSCAPS_BACKBUFFER))
459                 glReadBuffer(GL_BACK);
460             else {
461                 ERR("Wrong surface type for locking !\n");
462                 glBindTexture(GL_TEXTURE_2D, cur_tex);
463                 LEAVE_GL();
464                 return DDERR_INVALIDPARAMS;
465             }
466             
467             for (y = (src_ptr->surface_desc.dwHeight - rsrc->top - 1);
468                  y >= (src_ptr->surface_desc.dwHeight - (rsrc->top + height));
469                  y--) {
470                 glCopyTexSubImage2D(GL_TEXTURE_2D, surf_ptr->mipmap_level,
471                                     dstx, dsty,
472                                     rsrc->left, y,
473                                     width, 1);
474                 dsty++;
475             }
476             
477             glBindTexture(GL_TEXTURE_2D, cur_tex);
478             LEAVE_GL();
479             
480             /* The SURFACE_GL case is not handled by the 'global' dirty flag */
481             gl_surf_ptr->dirty_flag = SURFACE_GL;
482             
483             return DD_OK;
484         }
485     }
486     return DDERR_INVALIDPARAMS;
487 }
488
489 HRESULT WINAPI
490 Main_IDirect3DTextureImpl_2_1T_PaletteChanged(LPDIRECT3DTEXTURE2 iface,
491                                               DWORD dwStart,
492                                               DWORD dwCount)
493 {
494     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
495     FIXME("(%p/%p)->(%08lx,%08lx): stub!\n", This, iface, dwStart, dwCount);
496     return DD_OK;
497 }
498
499 HRESULT WINAPI
500 Main_IDirect3DTextureImpl_1_Unload(LPDIRECT3DTEXTURE iface)
501 {
502     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
503     FIXME("(%p/%p)->(): stub!\n", This, iface);
504     return DD_OK;
505 }
506
507 HRESULT WINAPI
508 Main_IDirect3DTextureImpl_2_1T_GetHandle(LPDIRECT3DTEXTURE2 iface,
509                                          LPDIRECT3DDEVICE2 lpDirect3DDevice2,
510                                          LPD3DTEXTUREHANDLE lpHandle)
511 {
512     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
513     IDirect3DDeviceImpl *lpDeviceImpl = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice2, lpDirect3DDevice2);
514     
515     TRACE("(%p/%p)->(%p,%p)\n", This, iface, lpDirect3DDevice2, lpHandle);
516
517     /* The handle is simply the pointer to the implementation structure */
518     *lpHandle = (D3DTEXTUREHANDLE) This;
519
520     TRACE(" returning handle %08lx.\n", *lpHandle);
521     
522     /* Now set the device for this texture */
523     This->d3ddevice = lpDeviceImpl;
524
525     return D3D_OK;
526 }
527
528 HRESULT WINAPI
529 Main_IDirect3DTextureImpl_2_1T_Load(LPDIRECT3DTEXTURE2 iface,
530                                     LPDIRECT3DTEXTURE2 lpD3DTexture2)
531 {
532     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
533     FIXME("(%p/%p)->(%p): stub!\n", This, iface, lpD3DTexture2);
534     return DD_OK;
535 }
536
537 static void gltex_set_palette(IDirectDrawSurfaceImpl* This, IDirectDrawPaletteImpl* pal)
538 {
539     IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
540
541     if (glThis->dirty_flag == SURFACE_GL) {
542         GLuint cur_tex;
543         
544         TRACE(" flushing GL texture back to memory.\n");
545         
546         ENTER_GL();
547         glGetIntegerv(GL_TEXTURE_BINDING_2D, &cur_tex);
548         glBindTexture(GL_TEXTURE_2D, glThis->tex_name);
549         gltex_download_texture(This);
550         glBindTexture(GL_TEXTURE_2D, cur_tex);
551         LEAVE_GL();
552     }
553     
554     /* First call the previous set_palette function */
555     glThis->set_palette(This, pal);
556     
557     /* And set the dirty flag */
558     glThis->dirty_flag = SURFACE_MEMORY_DIRTY;
559     *(glThis->global_dirty_flag) = SURFACE_MEMORY_DIRTY;
560     
561     /* TODO: check palette on mipmapped surfaces...
562        TODO: do we need to re-upload in case of usage of the paletted texture extension ? */
563 }
564
565 static void
566 gltex_final_release(IDirectDrawSurfaceImpl *This)
567 {
568     IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
569     DWORD mem_used;
570     int i;
571
572     TRACE(" deleting texture with GL id %d.\n", glThis->tex_name);
573
574     /* And delete texture handle */
575     ENTER_GL();
576     if (glThis->tex_name != 0)
577         glDeleteTextures(1, &(glThis->tex_name));
578     LEAVE_GL(); 
579
580     if (glThis->surface_ptr != NULL)
581         HeapFree(GetProcessHeap(), 0, glThis->surface_ptr);
582
583     /* And if this texture was the current one, remove it at the device level */
584     if (This->d3ddevice != NULL)
585         for (i = 0; i < MAX_TEXTURES; i++)
586             if (This->d3ddevice->current_texture[i] == This)
587                 This->d3ddevice->current_texture[i] = NULL;
588
589     /* All this should be part of main surface management not just a hack for texture.. */
590     if (glThis->loaded) {
591         mem_used = This->surface_desc.dwHeight *
592                    This->surface_desc.u1.lPitch;
593         This->ddraw_owner->free_memory(This->ddraw_owner, mem_used);
594     }
595
596     glThis->final_release(This);
597 }
598
599 static void
600 gltex_lock_update(IDirectDrawSurfaceImpl* This, LPCRECT pRect, DWORD dwFlags)
601 {
602     IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
603     
604     glThis->lock_update(This, pRect, dwFlags);
605 }
606
607 static void
608 gltex_unlock_update(IDirectDrawSurfaceImpl* This, LPCRECT pRect)
609 {
610     IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
611
612     glThis->unlock_update(This, pRect);
613     
614     /* Set the dirty flag according to the lock type */
615     if ((This->lastlocktype & DDLOCK_READONLY) == 0) {
616         glThis->dirty_flag = SURFACE_MEMORY_DIRTY;
617         *(glThis->global_dirty_flag) = SURFACE_MEMORY_DIRTY;
618     }
619 }
620
621 HRESULT WINAPI
622 GL_IDirect3DTextureImpl_2_1T_Load(LPDIRECT3DTEXTURE2 iface,
623                                   LPDIRECT3DTEXTURE2 lpD3DTexture2)
624 {
625     ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
626     IDirectDrawSurfaceImpl *src_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, lpD3DTexture2);
627     IDirectDrawSurfaceImpl *dst_ptr = This;
628     HRESULT ret_value = D3D_OK;
629    
630     TRACE("(%p/%p)->(%p)\n", This, iface, lpD3DTexture2);
631
632     if (((src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) != (dst_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)) ||
633         (src_ptr->surface_desc.u2.dwMipMapCount != dst_ptr->surface_desc.u2.dwMipMapCount)) {
634         ERR("Trying to load surfaces with different mip-map counts !\n");
635     }
636
637     /* Now loop through all mipmap levels and load all of them... */
638     while (1) {
639         IDirect3DTextureGLImpl *gl_dst_ptr = (IDirect3DTextureGLImpl *) dst_ptr->tex_private;
640         DDSURFACEDESC *src_d, *dst_d;
641         
642         if (gl_dst_ptr != NULL) {
643             if (gl_dst_ptr->loaded == FALSE) {
644                 /* Only check memory for not already loaded texture... */
645                 DWORD mem_used = dst_ptr->surface_desc.dwHeight * dst_ptr->surface_desc.u1.lPitch;
646                 if (This->ddraw_owner->allocate_memory(This->ddraw_owner, mem_used) < 0) {
647                     TRACE(" out of virtual memory... Warning application.\n");
648                     return D3DERR_TEXTURE_LOAD_FAILED;
649                 }
650             }
651             gl_dst_ptr->loaded = TRUE;
652         }
653     
654         TRACE(" copying surface %p to surface %p (mipmap level %d)\n", src_ptr, dst_ptr, src_ptr->mipmap_level);
655
656         if ( dst_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD )
657             /* If the surface is not allocated and its location is not yet specified,
658                force it to video memory */ 
659             if ( !(dst_ptr->surface_desc.ddsCaps.dwCaps & (DDSCAPS_SYSTEMMEMORY|DDSCAPS_VIDEOMEMORY)) )
660                 dst_ptr->surface_desc.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
661
662         /* Suppress the ALLOCONLOAD flag */
663         dst_ptr->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
664     
665         /* After seeing some logs, not sure at all about this... */
666         if (dst_ptr->palette == NULL) {
667             dst_ptr->palette = src_ptr->palette;
668             if (src_ptr->palette != NULL) IDirectDrawPalette_AddRef(ICOM_INTERFACE(src_ptr->palette, IDirectDrawPalette));
669         } else {
670             if (src_ptr->palette != NULL) {
671                 PALETTEENTRY palent[256];
672                 IDirectDrawPalette_GetEntries(ICOM_INTERFACE(src_ptr->palette, IDirectDrawPalette),
673                                               0, 0, 256, palent);
674                 IDirectDrawPalette_SetEntries(ICOM_INTERFACE(dst_ptr->palette, IDirectDrawPalette),
675                                               0, 0, 256, palent);
676             }
677         }
678         
679         /* Copy one surface on the other */
680         dst_d = (DDSURFACEDESC *)&(dst_ptr->surface_desc);
681         src_d = (DDSURFACEDESC *)&(src_ptr->surface_desc);
682
683         if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
684             /* Should also check for same pixel format, u1.lPitch, ... */
685             ERR("Error in surface sizes\n");
686             return D3DERR_TEXTURE_LOAD_FAILED;
687         } else {
688             /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
689             /* I should put a macro for the calculus of bpp */
690           
691             /* Copy also the ColorKeying stuff */
692             if (src_d->dwFlags & DDSD_CKSRCBLT) {
693                 dst_d->dwFlags |= DDSD_CKSRCBLT;
694                 dst_d->ddckCKSrcBlt.dwColorSpaceLowValue = src_d->ddckCKSrcBlt.dwColorSpaceLowValue;
695                 dst_d->ddckCKSrcBlt.dwColorSpaceHighValue = src_d->ddckCKSrcBlt.dwColorSpaceHighValue;
696             }
697
698             /* Copy the main memory texture into the surface that corresponds to the OpenGL
699                texture object. */
700             memcpy(dst_d->lpSurface, src_d->lpSurface, src_d->u1.lPitch * src_d->dwHeight);
701
702             if (gl_dst_ptr != NULL) {
703                 /* If the GetHandle was not done, it is an error... */
704                 if (gl_dst_ptr->tex_name == 0) ERR("Unbound GL texture !!!\n");
705
706                 /* Set this texture as dirty */
707                 gl_dst_ptr->dirty_flag = SURFACE_MEMORY_DIRTY;
708                 *(gl_dst_ptr->global_dirty_flag) = SURFACE_MEMORY_DIRTY;
709             }
710         }
711
712         if (src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) {
713             src_ptr = get_sub_mimaplevel(src_ptr);
714         } else {
715             src_ptr = NULL;
716         }
717         if (dst_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) {
718             dst_ptr = get_sub_mimaplevel(dst_ptr);
719         } else {
720             dst_ptr = NULL;
721         }
722
723         if ((src_ptr == NULL) || (dst_ptr == NULL)) {
724             if (src_ptr != dst_ptr) {
725                 ERR(" Loading surface with different mipmap structure !!!\n");
726             }
727             break;
728         }
729     }
730
731     return ret_value;
732 }
733
734 HRESULT WINAPI
735 Thunk_IDirect3DTextureImpl_2_QueryInterface(LPDIRECT3DTEXTURE2 iface,
736                                             REFIID riid,
737                                             LPVOID* obp)
738 {
739     TRACE("(%p)->(%s,%p) thunking to IDirectDrawSurface7 interface.\n", iface, debugstr_guid(riid), obp);
740     return IDirectDrawSurface7_QueryInterface(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture2, IDirectDrawSurface7, iface),
741                                               riid,
742                                               obp);
743 }
744
745 ULONG WINAPI
746 Thunk_IDirect3DTextureImpl_2_AddRef(LPDIRECT3DTEXTURE2 iface)
747 {
748     TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", iface);
749     return IDirectDrawSurface7_AddRef(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture2, IDirectDrawSurface7, iface));
750 }
751
752 ULONG WINAPI
753 Thunk_IDirect3DTextureImpl_2_Release(LPDIRECT3DTEXTURE2 iface)
754 {
755     TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", iface);
756     return IDirectDrawSurface7_Release(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture2, IDirectDrawSurface7, iface));
757 }
758
759 HRESULT WINAPI
760 Thunk_IDirect3DTextureImpl_1_QueryInterface(LPDIRECT3DTEXTURE iface,
761                                             REFIID riid,
762                                             LPVOID* obp)
763 {
764     TRACE("(%p)->(%s,%p) thunking to IDirectDrawSurface7 interface.\n", iface, debugstr_guid(riid), obp);
765     return IDirectDrawSurface7_QueryInterface(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirectDrawSurface7, iface),
766                                               riid,
767                                               obp);
768 }
769
770 ULONG WINAPI
771 Thunk_IDirect3DTextureImpl_1_AddRef(LPDIRECT3DTEXTURE iface)
772 {
773     TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", iface);
774     return IDirectDrawSurface7_AddRef(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirectDrawSurface7, iface));
775 }
776
777 ULONG WINAPI
778 Thunk_IDirect3DTextureImpl_1_Release(LPDIRECT3DTEXTURE iface)
779 {
780     TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", iface);
781     return IDirectDrawSurface7_Release(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirectDrawSurface7, iface));
782 }
783
784 HRESULT WINAPI
785 Thunk_IDirect3DTextureImpl_1_PaletteChanged(LPDIRECT3DTEXTURE iface,
786                                             DWORD dwStart,
787                                             DWORD dwCount)
788 {
789     TRACE("(%p)->(%08lx,%08lx) thunking to IDirect3DTexture2 interface.\n", iface, dwStart, dwCount);
790     return IDirect3DTexture2_PaletteChanged(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, iface),
791                                             dwStart,
792                                             dwCount);
793 }
794
795 HRESULT WINAPI
796 Thunk_IDirect3DTextureImpl_1_GetHandle(LPDIRECT3DTEXTURE iface,
797                                        LPDIRECT3DDEVICE lpDirect3DDevice,
798                                        LPD3DTEXTUREHANDLE lpHandle)
799 {
800     TRACE("(%p)->(%p,%p) thunking to IDirect3DTexture2 interface.\n", iface, lpDirect3DDevice, lpHandle);
801     return IDirect3DTexture2_GetHandle(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, iface),
802                                        COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice2, lpDirect3DDevice),
803                                        lpHandle);
804 }
805
806 HRESULT WINAPI
807 Thunk_IDirect3DTextureImpl_1_Load(LPDIRECT3DTEXTURE iface,
808                                   LPDIRECT3DTEXTURE lpD3DTexture)
809 {
810     TRACE("(%p)->(%p) thunking to IDirect3DTexture2 interface.\n", iface, lpD3DTexture);
811     return IDirect3DTexture2_Load(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, iface),
812                                   COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, lpD3DTexture));
813 }
814
815 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
816 # define XCAST(fun)     (typeof(VTABLE_IDirect3DTexture2.fun))
817 #else
818 # define XCAST(fun)     (void*)
819 #endif
820
821 ICOM_VTABLE(IDirect3DTexture2) VTABLE_IDirect3DTexture2 =
822 {
823     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
824     XCAST(QueryInterface) Thunk_IDirect3DTextureImpl_2_QueryInterface,
825     XCAST(AddRef) Thunk_IDirect3DTextureImpl_2_AddRef,
826     XCAST(Release) Thunk_IDirect3DTextureImpl_2_Release,
827     XCAST(GetHandle) Main_IDirect3DTextureImpl_2_1T_GetHandle,
828     XCAST(PaletteChanged) Main_IDirect3DTextureImpl_2_1T_PaletteChanged,
829     XCAST(Load) GL_IDirect3DTextureImpl_2_1T_Load,
830 };
831
832 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
833 #undef XCAST
834 #endif
835
836
837 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
838 # define XCAST(fun)     (typeof(VTABLE_IDirect3DTexture.fun))
839 #else
840 # define XCAST(fun)     (void*)
841 #endif
842
843 ICOM_VTABLE(IDirect3DTexture) VTABLE_IDirect3DTexture =
844 {
845     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
846     XCAST(QueryInterface) Thunk_IDirect3DTextureImpl_1_QueryInterface,
847     XCAST(AddRef) Thunk_IDirect3DTextureImpl_1_AddRef,
848     XCAST(Release) Thunk_IDirect3DTextureImpl_1_Release,
849     XCAST(Initialize) Main_IDirect3DTextureImpl_1_Initialize,
850     XCAST(GetHandle) Thunk_IDirect3DTextureImpl_1_GetHandle,
851     XCAST(PaletteChanged) Thunk_IDirect3DTextureImpl_1_PaletteChanged,
852     XCAST(Load) Thunk_IDirect3DTextureImpl_1_Load,
853     XCAST(Unload) Main_IDirect3DTextureImpl_1_Unload,
854 };
855
856 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
857 #undef XCAST
858 #endif
859
860 HRESULT d3dtexture_create(IDirectDrawImpl *d3d, IDirectDrawSurfaceImpl *surf, BOOLEAN at_creation, 
861                           IDirectDrawSurfaceImpl *main)
862 {
863     /* First, initialize the texture vtables... */
864     ICOM_INIT_INTERFACE(surf, IDirect3DTexture,  VTABLE_IDirect3DTexture);
865     ICOM_INIT_INTERFACE(surf, IDirect3DTexture2, VTABLE_IDirect3DTexture2);
866         
867     /* Only create all the private stuff if we actually have an OpenGL context.. */
868     if (d3d->current_device != NULL) {
869         IDirect3DTextureGLImpl *private;
870
871         private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTextureGLImpl));
872         if (private == NULL) return DDERR_OUTOFMEMORY;
873
874         private->final_release = surf->final_release;
875         private->lock_update = surf->lock_update;
876         private->unlock_update = surf->unlock_update;
877         private->set_palette = surf->set_palette;
878         
879         /* If at creation, we can optimize stuff and wait the first 'unlock' to upload a valid stuff to OpenGL.
880            Otherwise, it will be uploaded here (and may be invalid). */
881         surf->final_release = gltex_final_release;
882         surf->lock_update = gltex_lock_update;
883         surf->unlock_update = gltex_unlock_update;
884         surf->tex_private = private;
885         surf->aux_setcolorkey_cb = gltex_setcolorkey_cb;
886         surf->set_palette = gltex_set_palette;
887
888         /* We are the only one to use the aux_blt and aux_bltfast overides, so no need
889            to save those... */
890         surf->aux_blt = gltex_blt;
891         surf->aux_bltfast = gltex_bltfast;
892         
893         TRACE(" GL texture created for surface %p (private data at %p)\n", surf, private);
894         
895         ENTER_GL();
896         if (surf->mipmap_level == 0) {
897             glGenTextures(1, &(private->tex_name));
898             if (private->tex_name == 0) ERR("Error at creation of OpenGL texture ID !\n");
899             TRACE(" GL texture id is : %d.\n", private->tex_name);
900             private->__global_dirty_flag = (at_creation == FALSE ? SURFACE_MEMORY_DIRTY : SURFACE_MEMORY);
901             private->global_dirty_flag = &(private->__global_dirty_flag);
902         } else {
903             private->tex_name = ((IDirect3DTextureGLImpl *) (main->tex_private))->tex_name;
904             TRACE(" GL texture id reusing id %d from surface %p (private at %p)).\n", private->tex_name, main, main->tex_private);
905             private->global_dirty_flag = &(((IDirect3DTextureGLImpl *) (main->tex_private))->__global_dirty_flag);
906         }
907         LEAVE_GL();
908
909         /* And set the dirty flag accordingly */
910         private->dirty_flag = (at_creation == FALSE ? SURFACE_MEMORY_DIRTY : SURFACE_MEMORY);
911         private->initial_upload_done = FALSE;
912     }
913
914     return D3D_OK;
915 }