Make 16-bit task properly inherit the current directory of their
[wine] / dlls / ddraw / opengl_utils.c
1 /* Direct3D Common functions
2  * Copyright (c) 1998 Lionel ULMER
3  *
4  * This file contains all MESA common code
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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "objbase.h"
30 #include "wingdi.h"
31 #include "ddraw.h"
32 #include "d3d.h"
33 #include "wine/debug.h"
34
35 #include "opengl_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
38
39 GLenum convert_D3D_compare_to_GL(D3DCMPFUNC dwRenderState)
40 {
41     switch (dwRenderState) {
42         case D3DCMP_NEVER: return GL_NEVER;
43         case D3DCMP_LESS: return GL_LESS;
44         case D3DCMP_EQUAL: return GL_EQUAL;
45         case D3DCMP_LESSEQUAL: return GL_LEQUAL;
46         case D3DCMP_GREATER: return GL_GREATER;
47         case D3DCMP_NOTEQUAL: return GL_NOTEQUAL;
48         case D3DCMP_GREATEREQUAL: return GL_GEQUAL;
49         case D3DCMP_ALWAYS: return GL_ALWAYS;
50         default: ERR("Unexpected compare type %d !\n", dwRenderState);
51     }
52     return GL_ALWAYS;
53 }
54
55 GLenum convert_D3D_stencilop_to_GL(D3DSTENCILOP dwRenderState)
56 {
57     switch (dwRenderState) {
58         case D3DSTENCILOP_KEEP: return GL_KEEP;
59         case D3DSTENCILOP_ZERO: return GL_ZERO;
60         case D3DSTENCILOP_REPLACE: return GL_REPLACE;
61         case D3DSTENCILOP_INCRSAT: return GL_INCR;
62         case D3DSTENCILOP_DECRSAT: return GL_DECR;
63         case D3DSTENCILOP_INVERT: return GL_INVERT;
64         case D3DSTENCILOP_INCR: WARN("D3DSTENCILOP_INCR not properly handled !\n"); return GL_INCR;
65         case D3DSTENCILOP_DECR: WARN("D3DSTENCILOP_DECR not properly handled !\n"); return GL_DECR;
66         default: ERR("Unexpected compare type %d !\n", dwRenderState);      
67     }
68     return GL_KEEP;
69 }
70
71 GLenum convert_D3D_blendop_to_GL(D3DBLEND dwRenderState)
72 {
73     switch ((D3DBLEND) dwRenderState) {
74         case D3DBLEND_ZERO: return GL_ZERO;
75         case D3DBLEND_ONE: return GL_ONE;
76         case D3DBLEND_SRCALPHA: return GL_SRC_ALPHA;
77         case D3DBLEND_INVSRCALPHA: return GL_ONE_MINUS_SRC_ALPHA;
78         case D3DBLEND_DESTALPHA: return GL_DST_ALPHA;
79         case D3DBLEND_INVDESTALPHA: return GL_ONE_MINUS_DST_ALPHA;
80         case D3DBLEND_DESTCOLOR: return GL_DST_COLOR;
81         case D3DBLEND_INVDESTCOLOR: return GL_ONE_MINUS_DST_COLOR;
82         case D3DBLEND_SRCALPHASAT: return GL_SRC_ALPHA_SATURATE;
83         case D3DBLEND_SRCCOLOR: return GL_SRC_COLOR;
84         case D3DBLEND_INVSRCCOLOR: return GL_ONE_MINUS_SRC_COLOR;
85         default: ERR("Unhandled blend mode %d !\n", dwRenderState); return GL_ZERO;
86     }
87 }
88
89 void set_render_state(IDirect3DDeviceImpl* This,
90                       D3DRENDERSTATETYPE dwRenderStateType, STATEBLOCK *lpStateBlock)
91 {
92     DWORD dwRenderState = lpStateBlock->render_state[dwRenderStateType - 1];
93     IDirect3DDeviceGLImpl *glThis = (IDirect3DDeviceGLImpl *) This;
94     
95     TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType), dwRenderState);
96
97     /* First, all the stipple patterns */
98     if ((dwRenderStateType >= D3DRENDERSTATE_STIPPLEPATTERN00) &&
99         (dwRenderStateType <= D3DRENDERSTATE_STIPPLEPATTERN31)) {
100         ERR("Unhandled dwRenderStateType stipple %d!\n",dwRenderStateType);
101     } else {
102         ENTER_GL();
103
104         /* All others state variables */
105         switch (dwRenderStateType) {
106             case D3DRENDERSTATE_TEXTUREHANDLE: {    /*  1 */
107                 IDirectDrawSurfaceImpl *tex = (IDirectDrawSurfaceImpl*) dwRenderState;
108                 
109                 IDirect3DDevice7_SetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
110                                             0, 
111                                             ICOM_INTERFACE(tex, IDirectDrawSurface7));
112             } break;
113
114             case D3DRENDERSTATE_ANTIALIAS:        /*  2 */
115                 if (dwRenderState)
116                     ERR("D3DRENDERSTATE_ANTIALIAS not supported yet !\n");
117                 break;
118               
119             case D3DRENDERSTATE_TEXTUREADDRESSU:  /* 44 */
120             case D3DRENDERSTATE_TEXTUREADDRESSV:  /* 45 */
121             case D3DRENDERSTATE_TEXTUREADDRESS: { /*  3 */
122                 D3DTEXTURESTAGESTATETYPE d3dTexStageStateType;
123
124                 if (dwRenderStateType == D3DRENDERSTATE_TEXTUREADDRESS) d3dTexStageStateType = D3DTSS_ADDRESS;
125                 else if (dwRenderStateType == D3DRENDERSTATE_TEXTUREADDRESSU) d3dTexStageStateType = D3DTSS_ADDRESSU;
126                 else d3dTexStageStateType = D3DTSS_ADDRESSV;
127
128                 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
129                                                       0, d3dTexStageStateType,
130                                                       dwRenderState);
131             } break;
132               
133             case D3DRENDERSTATE_TEXTUREPERSPECTIVE: /* 4 */
134                 if (dwRenderState)
135                     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
136                 else
137                     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
138                 break;
139
140             case D3DRENDERSTATE_WRAPU: /* 5 */
141             case D3DRENDERSTATE_WRAPV: /* 6 */
142             case D3DRENDERSTATE_WRAP0: /* 128 */
143             case D3DRENDERSTATE_WRAP1: /* 129 */
144             case D3DRENDERSTATE_WRAP2: /* 130 */
145             case D3DRENDERSTATE_WRAP3: /* 131 */
146             case D3DRENDERSTATE_WRAP4: /* 132 */
147             case D3DRENDERSTATE_WRAP5: /* 133 */
148             case D3DRENDERSTATE_WRAP6: /* 134 */
149             case D3DRENDERSTATE_WRAP7: /* 135 */
150                 if (dwRenderState)
151                     ERR("Texture WRAP modes unsupported by OpenGL.. Expect graphical glitches !\n");
152                 break;
153
154             case D3DRENDERSTATE_ZENABLE:          /*  7 */
155                 /* To investigate : in OpenGL, if we disable the depth test, the Z buffer will NOT be
156                    updated either.. No idea about what happens in D3D.
157                    
158                    Maybe replacing the Z function by ALWAYS would be a better idea. */
159                 if (dwRenderState == D3DZB_TRUE) {
160                     if (glThis->depth_test == FALSE) {
161                         glEnable(GL_DEPTH_TEST);
162                         glThis->depth_test = TRUE;
163                     }
164                 } else if (dwRenderState == D3DZB_FALSE) {
165                     if (glThis->depth_test) {
166                         glDisable(GL_DEPTH_TEST);
167                         glThis->depth_test = FALSE;
168                     }
169                 } else {
170                     if (glThis->depth_test == FALSE) {
171                         glEnable(GL_DEPTH_TEST);
172                         glThis->depth_test = TRUE;
173                     }
174                     WARN(" w-buffering not supported.\n");
175                 }
176                 break;
177
178             case D3DRENDERSTATE_FILLMODE:           /*  8 */
179                 switch ((D3DFILLMODE) dwRenderState) {
180                     case D3DFILL_POINT:
181                         glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);        
182                         break;
183                     case D3DFILL_WIREFRAME:
184                         glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); 
185                         break;
186                     case D3DFILL_SOLID:
187                         glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); 
188                         break;
189                     default:
190                         ERR("Unhandled fill mode %ld !\n",dwRenderState);
191                  }
192                  break;
193
194             case D3DRENDERSTATE_SHADEMODE:          /*  9 */
195                 switch ((D3DSHADEMODE) dwRenderState) {
196                     case D3DSHADE_FLAT:
197                         glShadeModel(GL_FLAT);
198                         break;
199                     case D3DSHADE_GOURAUD:
200                         glShadeModel(GL_SMOOTH);
201                         break;
202                     default:
203                         ERR("Unhandled shade mode %ld !\n",dwRenderState);
204                 }
205                 break;
206
207             case D3DRENDERSTATE_ZWRITEENABLE:     /* 14 */
208                 if ((dwRenderState != FALSE) && (glThis->depth_mask == FALSE))
209                     glDepthMask(GL_TRUE);
210                 else if ((dwRenderState == FALSE) && (glThis->depth_mask != FALSE))
211                     glDepthMask(GL_FALSE);
212                 glThis->depth_mask = dwRenderState;
213                 break;
214               
215             case D3DRENDERSTATE_ALPHATESTENABLE:  /* 15 */
216                 if ((dwRenderState != 0) && (glThis->alpha_test == FALSE))
217                     glEnable(GL_ALPHA_TEST);
218                 else if ((dwRenderState == 0) && (glThis->alpha_test != FALSE))
219                     glDisable(GL_ALPHA_TEST);
220                 glThis->alpha_test = dwRenderState;
221                 break;
222
223             case D3DRENDERSTATE_TEXTUREMAG: {     /* 17 */
224                 DWORD tex_mag = 0xFFFFFFFF;
225
226                 switch ((D3DTEXTUREFILTER) dwRenderState) {
227                     case D3DFILTER_NEAREST:
228                         tex_mag = D3DTFG_POINT;
229                         break;
230                     case D3DFILTER_LINEAR:
231                         tex_mag = D3DTFG_LINEAR;
232                         break;
233                     default:
234                         ERR("Unhandled texture mag %ld !\n",dwRenderState);
235                 }
236
237                 if (tex_mag != 0xFFFFFFFF) {
238                     IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7), 0, D3DTSS_MAGFILTER, tex_mag);
239                 }
240             } break;
241
242             case D3DRENDERSTATE_TEXTUREMIN: {       /* 18 */
243                 DWORD tex_min = 0xFFFFFFFF;
244
245                 switch ((D3DTEXTUREFILTER) dwRenderState) {
246                     case D3DFILTER_NEAREST:
247                         tex_min = D3DTFN_POINT;
248                         break;
249                     case D3DFILTER_LINEAR:
250                         tex_min = D3DTFN_LINEAR;
251                         break;
252                     default:
253                         ERR("Unhandled texture min %ld !\n",dwRenderState);
254                 }
255
256                 if (tex_min != 0xFFFFFFFF) {
257                     IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7), 0, D3DTSS_MINFILTER, tex_min);
258                 }
259             } break;
260
261             case D3DRENDERSTATE_SRCBLEND:           /* 19 */
262             case D3DRENDERSTATE_DESTBLEND:          /* 20 */
263                 glBlendFunc(convert_D3D_blendop_to_GL(lpStateBlock->render_state[D3DRENDERSTATE_SRCBLEND - 1]),
264                             convert_D3D_blendop_to_GL(lpStateBlock->render_state[D3DRENDERSTATE_DESTBLEND - 1]));
265                 break;
266
267             case D3DRENDERSTATE_TEXTUREMAPBLEND: {  /* 21 */
268                 IDirect3DDevice7 *d3ddev = ICOM_INTERFACE(This, IDirect3DDevice7);
269                 
270                 switch ((D3DTEXTUREBLEND) dwRenderState) {
271                     case D3DTBLEND_DECAL:
272                         if (glThis->current_tex_env != GL_REPLACE) {
273                             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
274                             glThis->current_tex_env = GL_REPLACE;
275                         }
276                         break;
277                     case D3DTBLEND_DECALALPHA:
278                         if (glThis->current_tex_env != GL_REPLACE) {
279                             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
280                             glThis->current_tex_env = GL_DECAL;
281                         }
282                         break;
283                     case D3DTBLEND_MODULATE:
284                         if (glThis->current_tex_env != GL_MODULATE) {
285                             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
286                             glThis->current_tex_env = GL_MODULATE;
287                         }
288                         break;
289                     case D3DTBLEND_MODULATEALPHA:
290                         IDirect3DDevice7_SetTextureStageState(d3ddev, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
291                         IDirect3DDevice7_SetTextureStageState(d3ddev, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
292                         IDirect3DDevice7_SetTextureStageState(d3ddev, 0, D3DTSS_COLORARG2, D3DTA_CURRENT);
293                         IDirect3DDevice7_SetTextureStageState(d3ddev, 0, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
294                         IDirect3DDevice7_SetTextureStageState(d3ddev, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
295                         IDirect3DDevice7_SetTextureStageState(d3ddev, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
296                         break;
297                     default:
298                         ERR("Unhandled texture environment %ld !\n",dwRenderState);
299                 }
300             } break;
301
302             case D3DRENDERSTATE_CULLMODE:           /* 22 */
303                 switch ((D3DCULL) dwRenderState) {
304                     case D3DCULL_NONE:
305                          if (glThis->cull_face != 0) {
306                              glDisable(GL_CULL_FACE);
307                              glThis->cull_face = 0;
308                          }
309                          break;
310                     case D3DCULL_CW:
311                          if (glThis->cull_face == 0) {
312                              glEnable(GL_CULL_FACE);
313                              glThis->cull_face = 1;
314                          }
315                          glFrontFace(GL_CCW);
316                          glCullFace(GL_BACK);
317                          break;
318                     case D3DCULL_CCW:
319                          if (glThis->cull_face == 0) {
320                              glEnable(GL_CULL_FACE);
321                              glThis->cull_face = 1;
322                          }
323                          glFrontFace(GL_CW);
324                          glCullFace(GL_BACK);
325                          break;
326                     default:
327                          ERR("Unhandled cull mode %ld !\n",dwRenderState);
328                 }
329                 break;
330
331             case D3DRENDERSTATE_ZFUNC:            /* 23 */
332                 glDepthFunc(convert_D3D_compare_to_GL(dwRenderState));
333                 break;
334               
335             case D3DRENDERSTATE_ALPHAREF:    /* 24 */
336             case D3DRENDERSTATE_ALPHAFUNC: { /* 25 */
337                     GLenum func = convert_D3D_compare_to_GL(lpStateBlock->render_state[D3DRENDERSTATE_ALPHAFUNC - 1]);
338                     GLclampf ref = (lpStateBlock->render_state[D3DRENDERSTATE_ALPHAREF - 1] & 0x000000FF) / 255.0;
339
340                     if ((func != glThis->current_alpha_test_func) || (ref != glThis->current_alpha_test_ref)) {
341                         glAlphaFunc(func, ref);
342                         glThis->current_alpha_test_func = func;
343                         glThis->current_alpha_test_ref = ref;
344                     }
345                 }
346                 break;
347
348             case D3DRENDERSTATE_DITHERENABLE:     /* 26 */
349                 if (dwRenderState)
350                     glEnable(GL_DITHER);
351                 else
352                     glDisable(GL_DITHER);
353                 break;
354
355             case D3DRENDERSTATE_ALPHABLENDENABLE:   /* 27 */
356                 if ((dwRenderState != 0) && (glThis->blending == 0)) {
357                     glEnable(GL_BLEND);
358                 } else if ((dwRenderState == 0) && (glThis->blending != 0)) {
359                     glDisable(GL_BLEND);
360                 }
361                 glThis->blending = dwRenderState;
362
363                 /* Hack for some old games ... */
364                 if (glThis->parent.version == 1) {
365                     lpStateBlock->render_state[D3DRENDERSTATE_COLORKEYENABLE - 1] = dwRenderState;
366                 }
367                 break;
368               
369             case D3DRENDERSTATE_FOGENABLE: /* 28 */
370                 /* Nothing to do here. Only the storage matters :-) */
371                 break;
372
373             case D3DRENDERSTATE_SPECULARENABLE: /* 29 */
374                 if (dwRenderState)
375                     ERR(" Specular Lighting not supported yet.\n");
376                 break;
377               
378             case D3DRENDERSTATE_SUBPIXEL:  /* 31 */
379             case D3DRENDERSTATE_SUBPIXELX: /* 32 */
380                 /* We do not support this anyway, so why protest :-) */
381                 break; 
382
383             case D3DRENDERSTATE_STIPPLEDALPHA: /* 33 */
384                 if (dwRenderState)
385                     ERR(" Stippled Alpha not supported yet.\n");
386                 break;
387
388             case D3DRENDERSTATE_FOGCOLOR: { /* 34 */
389                 GLfloat color[4];
390                 color[0] = ((dwRenderState >> 16) & 0xFF)/255.0f;
391                 color[1] = ((dwRenderState >>  8) & 0xFF)/255.0f;
392                 color[2] = ((dwRenderState >>  0) & 0xFF)/255.0f;
393                 color[3] = ((dwRenderState >> 24) & 0xFF)/255.0f;
394                 glFogfv(GL_FOG_COLOR,color);
395                 /* Note: glFogiv does not seem to work */
396             } break;
397
398             case D3DRENDERSTATE_FOGTABLEMODE:  /* 35 */
399             case D3DRENDERSTATE_FOGVERTEXMODE: /* 140 */
400             case D3DRENDERSTATE_FOGSTART:      /* 36 */
401             case D3DRENDERSTATE_FOGEND:        /* 37 */
402                 /* Nothing to do here. Only the storage matters :-) */
403                 break;
404
405             case D3DRENDERSTATE_FOGDENSITY:    /* 38 */
406                 glFogi(GL_FOG_DENSITY,*(float*)&dwRenderState);
407                 break;
408
409             case D3DRENDERSTATE_COLORKEYENABLE:     /* 41 */
410                 /* Nothing done here, only storage matters. */
411                 break;
412
413             case D3DRENDERSTATE_MIPMAPLODBIAS: /* 46 */
414                 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
415                                                       0, D3DTSS_MIPMAPLODBIAS,
416                                                       dwRenderState);
417                 break;
418               
419             case D3DRENDERSTATE_ZBIAS: /* 47 */
420                 /* This is a tad bit hacky.. But well, no idea how to do it better in OpenGL :-/ */
421                 if (dwRenderState == 0) {
422                     glDisable(GL_POLYGON_OFFSET_FILL);
423                     glDisable(GL_POLYGON_OFFSET_LINE);
424                     glDisable(GL_POLYGON_OFFSET_POINT);
425                 } else {
426                     glEnable(GL_POLYGON_OFFSET_FILL);
427                     glEnable(GL_POLYGON_OFFSET_LINE);
428                     glEnable(GL_POLYGON_OFFSET_POINT);
429                     glPolygonOffset(1.0, dwRenderState * 1.0);
430                 }
431                 break;
432               
433             case D3DRENDERSTATE_FLUSHBATCH:         /* 50 */
434                 break;
435
436             case D3DRENDERSTATE_STENCILENABLE:    /* 52 */
437                 if ((dwRenderState != 0) && (glThis->stencil_test == 0))
438                     glEnable(GL_STENCIL_TEST);
439                 else if ((dwRenderState == 0) && (glThis->stencil_test != 0))
440                     glDisable(GL_STENCIL_TEST);
441                 glThis->stencil_test = dwRenderState;
442                 break;
443             
444             case D3DRENDERSTATE_STENCILFAIL:      /* 53 */
445             case D3DRENDERSTATE_STENCILZFAIL:     /* 54 */
446             case D3DRENDERSTATE_STENCILPASS:      /* 55 */
447                 glStencilOp(convert_D3D_stencilop_to_GL(lpStateBlock->render_state[D3DRENDERSTATE_STENCILFAIL - 1]),
448                             convert_D3D_stencilop_to_GL(lpStateBlock->render_state[D3DRENDERSTATE_STENCILZFAIL - 1]),
449                             convert_D3D_stencilop_to_GL(lpStateBlock->render_state[D3DRENDERSTATE_STENCILPASS - 1]));
450                 break;
451
452             case D3DRENDERSTATE_STENCILFUNC:      /* 56 */
453             case D3DRENDERSTATE_STENCILREF:       /* 57 */
454             case D3DRENDERSTATE_STENCILMASK:      /* 58 */
455                 glStencilFunc(convert_D3D_compare_to_GL(lpStateBlock->render_state[D3DRENDERSTATE_STENCILFUNC - 1]),
456                               lpStateBlock->render_state[D3DRENDERSTATE_STENCILREF - 1],
457                               lpStateBlock->render_state[D3DRENDERSTATE_STENCILMASK - 1]);
458                 break;
459           
460             case D3DRENDERSTATE_STENCILWRITEMASK: /* 59 */
461                 glStencilMask(dwRenderState);
462                 break;
463
464             case D3DRENDERSTATE_TEXTUREFACTOR:      /* 60 */
465                 /* Only the storage matters... */
466                 break;
467
468             case D3DRENDERSTATE_CLIPPING:          /* 136 */
469             case D3DRENDERSTATE_CLIPPLANEENABLE: { /* 152 */
470                     GLint i;
471                     DWORD mask, runner;
472                     
473                     if (dwRenderStateType == D3DRENDERSTATE_CLIPPING) {
474                         mask = ((dwRenderState) ?
475                                 (This->state_block.render_state[D3DRENDERSTATE_CLIPPLANEENABLE - 1]) : (0x00000000));
476                     } else {
477                         mask = dwRenderState;
478                     }
479                     for (i = 0, runner = 0x00000001; i < This->max_clipping_planes; i++, runner = (runner << 1)) {
480                         if (mask & runner) {
481                             GLint enabled;
482                             glGetIntegerv(GL_CLIP_PLANE0 + i, &enabled);
483                             if (enabled == GL_FALSE) {
484                                 glEnable(GL_CLIP_PLANE0 + i);
485                                 /* Need to force a transform change so that this clipping plane parameters are sent
486                                  * properly to GL.
487                                  */
488                                 glThis->transform_state = GL_TRANSFORM_NONE;
489                             }
490                         } else {
491                             glDisable(GL_CLIP_PLANE0 + i);
492                         }
493                     }
494                 }
495                 break;
496
497             case D3DRENDERSTATE_LIGHTING:    /* 137 */
498                 /* Nothing to do, only storage matters... */
499                 break;
500                 
501             case D3DRENDERSTATE_AMBIENT: {            /* 139 */
502                 float light[4];
503
504                 light[0] = ((dwRenderState >> 16) & 0xFF) / 255.0;
505                 light[1] = ((dwRenderState >>  8) & 0xFF) / 255.0;
506                 light[2] = ((dwRenderState >>  0) & 0xFF) / 255.0;
507                 light[3] = ((dwRenderState >> 24) & 0xFF) / 255.0;
508                 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (float *) light);
509             } break;
510
511             case D3DRENDERSTATE_COLORVERTEX:          /* 141 */
512                   /* Nothing to do here.. Only storage matters */
513                   break;
514                   
515             case D3DRENDERSTATE_LOCALVIEWER:          /* 142 */
516                 if (dwRenderState)
517                     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
518                 else
519                     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
520                 break;
521
522             case D3DRENDERSTATE_NORMALIZENORMALS:     /* 143 */
523                 if (dwRenderState) {
524                     glEnable(GL_NORMALIZE);
525                     glEnable(GL_RESCALE_NORMAL);
526                 } else {
527                     glDisable(GL_NORMALIZE);
528                     glDisable(GL_RESCALE_NORMAL);
529                 }
530                 break;
531
532             case D3DRENDERSTATE_DIFFUSEMATERIALSOURCE:    /* 145 */
533             case D3DRENDERSTATE_SPECULARMATERIALSOURCE:   /* 146 */
534             case D3DRENDERSTATE_AMBIENTMATERIALSOURCE:    /* 147 */
535             case D3DRENDERSTATE_EMISSIVEMATERIALSOURCE:   /* 148 */
536                 /* Nothing to do here. Only the storage matters :-) */
537                 break;
538
539             default:
540                 ERR("Unhandled dwRenderStateType %s (%08x) value : %08lx !\n",
541                     _get_renderstate(dwRenderStateType), dwRenderStateType, dwRenderState);
542         }
543         LEAVE_GL();
544     }
545 }
546
547 void store_render_state(IDirect3DDeviceImpl *This,
548                         D3DRENDERSTATETYPE dwRenderStateType, DWORD dwRenderState, STATEBLOCK *lpStateBlock)
549 {
550     TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType), dwRenderState);
551     
552     /* Some special cases first.. */
553     if (dwRenderStateType == D3DRENDERSTATE_SRCBLEND) {
554         if (dwRenderState == D3DBLEND_BOTHSRCALPHA) {
555             lpStateBlock->render_state[D3DRENDERSTATE_SRCBLEND - 1] = D3DBLEND_SRCALPHA;
556             lpStateBlock->render_state[D3DRENDERSTATE_DESTBLEND - 1] = D3DBLEND_INVSRCALPHA;
557             return;
558         } else if (dwRenderState == D3DBLEND_BOTHINVSRCALPHA) {
559             lpStateBlock->render_state[D3DRENDERSTATE_SRCBLEND - 1] = D3DBLEND_INVSRCALPHA;
560             lpStateBlock->render_state[D3DRENDERSTATE_DESTBLEND - 1] = D3DBLEND_SRCALPHA;
561             return;
562         }
563     } else if (dwRenderStateType == D3DRENDERSTATE_TEXTUREADDRESS) {
564         lpStateBlock->render_state[D3DRENDERSTATE_TEXTUREADDRESSU - 1] = dwRenderState;
565         lpStateBlock->render_state[D3DRENDERSTATE_TEXTUREADDRESSV - 1] = dwRenderState;
566     } else if (dwRenderStateType == D3DRENDERSTATE_WRAPU) {
567         if (dwRenderState) 
568             lpStateBlock->render_state[D3DRENDERSTATE_WRAP0] |= D3DWRAP_U;
569         else
570             lpStateBlock->render_state[D3DRENDERSTATE_WRAP0] &= ~D3DWRAP_U;
571     } else if (dwRenderStateType == D3DRENDERSTATE_WRAPV) {
572         if (dwRenderState) 
573             lpStateBlock->render_state[D3DRENDERSTATE_WRAP0] |= D3DWRAP_V;
574         else
575             lpStateBlock->render_state[D3DRENDERSTATE_WRAP0] &= ~D3DWRAP_V;
576     }
577     
578     /* Default case */
579     lpStateBlock->render_state[dwRenderStateType - 1] = dwRenderState;
580 }
581
582 void get_render_state(IDirect3DDeviceImpl *This,
583                       D3DRENDERSTATETYPE dwRenderStateType, LPDWORD lpdwRenderState, STATEBLOCK *lpStateBlock)
584 {
585     *lpdwRenderState = lpStateBlock->render_state[dwRenderStateType - 1];
586     if (TRACE_ON(ddraw))
587         TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType), *lpdwRenderState);
588 }
589
590 void apply_render_state(IDirect3DDeviceImpl *This, STATEBLOCK *lpStateBlock)
591 {
592     DWORD i;
593     TRACE("(%p,%p)\n", This, lpStateBlock);
594     for(i = 0; i < HIGHEST_RENDER_STATE; i++)
595         if (lpStateBlock->set_flags.render_state[i])
596             set_render_state(This, i + 1, lpStateBlock);
597 }
598
599
600 /* Texture management code.
601
602     - upload_surface_to_tex_memory_init initialize the code and computes the GL formats 
603       according to the surface description.
604
605     - upload_surface_to_tex_memory does the real upload. If one buffer is split over
606       multiple textures, this can be called multiple times after the '_init' call. 'rect'
607       can be NULL if the whole buffer needs to be upload.
608
609     - upload_surface_to_tex_memory_release does the clean-up.
610
611    These functions are called in the following cases :
612     - texture management (ie to upload a D3D texture to GL when it changes).
613     - flush of the 'in-memory' frame buffer to the GL frame buffer using the texture
614       engine.
615     - use of the texture engine to simulate Blits to the 3D Device.
616 */
617 typedef enum {
618     NO_CONVERSION,
619     CONVERT_PALETTED,
620     CONVERT_CK_565,
621     CONVERT_CK_5551,
622     CONVERT_CK_4444,
623     CONVERT_CK_4444_ARGB,
624     CONVERT_CK_1555,
625     CONVERT_555,
626     CONVERT_CK_RGB24,
627     CONVERT_CK_8888,
628     CONVERT_CK_8888_ARGB,
629     CONVERT_RGB32_888
630 } CONVERT_TYPES;
631
632 /* Note : we suppose that all the code calling this is protected by the GL lock... Otherwise bad things
633    may happen :-) */
634 static GLenum current_format;
635 static GLenum current_pixel_format;
636 static CONVERT_TYPES convert_type;
637 static IDirectDrawSurfaceImpl *current_surface;
638 static GLuint current_level;
639 static DWORD current_tex_width;
640 static DWORD current_tex_height;
641 static GLuint current_alignement_constraints;
642 static int current_storage_width;
643
644 HRESULT upload_surface_to_tex_memory_init(IDirectDrawSurfaceImpl *surf_ptr, GLuint level, GLenum *current_internal_format,
645                                           BOOLEAN need_to_alloc, BOOLEAN need_alpha_ck, DWORD tex_width, DWORD tex_height)
646 {
647     const DDPIXELFORMAT * const src_pf = &(surf_ptr->surface_desc.u4.ddpfPixelFormat);
648     BOOL error = FALSE;
649     BOOL colorkey_active = need_alpha_ck && (surf_ptr->surface_desc.dwFlags & DDSD_CKSRCBLT);
650     GLenum internal_format = GL_LUMINANCE; /* A bogus value to be sure to have a nice Mesa warning :-) */
651     BYTE bpp = GET_BPP(surf_ptr->surface_desc);
652     BOOL sub_texture = TRUE;
653
654     current_surface = surf_ptr;
655     current_level = level;
656
657     if (src_pf->dwFlags & DDPF_FOURCC) {
658         GLenum retVal;
659         int size = surf_ptr->surface_desc.u1.dwLinearSize;
660         int width = surf_ptr->surface_desc.dwWidth;
661         int height = surf_ptr->surface_desc.dwHeight;
662         LPVOID buffer = surf_ptr->surface_desc.lpSurface;
663
664         switch (src_pf->dwFourCC) {
665             case MAKE_FOURCC('D','X','T','1'): retVal = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break;
666             case MAKE_FOURCC('D','X','T','3'): retVal = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
667             case MAKE_FOURCC('D','X','T','5'): retVal = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
668             default:
669                 FIXME("FourCC Not supported\n");
670                 return DD_OK;
671         }
672
673         if (GL_extensions.s3tc_compressed_texture) {
674             GL_extensions.glCompressedTexImage2D(GL_TEXTURE_2D, current_level, retVal, width, height, 0, size, buffer);
675         } else
676             ERR("Trying to upload S3TC texture whereas the device does not have support for it\n");
677
678         return DD_OK;
679     }
680
681     /* First, do some sanity checks ... */
682     if ((surf_ptr->surface_desc.u1.lPitch % bpp) != 0) {
683         FIXME("Warning : pitch is not a multiple of BPP - not supported yet !\n");
684     } else {
685         /* In that case, no need to have any alignement constraints... */
686         if (current_alignement_constraints != 1) {
687             glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
688             current_alignement_constraints = 1;
689         }
690     }
691
692     /* Note: we only check width here as you cannot have width non-zero while height is set to zero */
693     if (tex_width == 0) {
694         sub_texture = FALSE;
695         
696         tex_width = surf_ptr->surface_desc.dwWidth;
697         tex_height = surf_ptr->surface_desc.dwHeight;
698     }
699
700     current_tex_width = tex_width;
701     current_tex_height = tex_height;
702
703     if (src_pf->dwFlags & DDPF_PALETTEINDEXED8) {
704         /* ****************
705            Paletted Texture
706            **************** */
707         current_format = GL_RGBA;
708         internal_format = GL_RGBA;
709         current_pixel_format = GL_UNSIGNED_BYTE;
710         convert_type = CONVERT_PALETTED;
711     } else if (src_pf->dwFlags & DDPF_RGB) {
712         /* ************
713            RGB Textures
714            ************ */
715         if (src_pf->u1.dwRGBBitCount == 8) {
716             if ((src_pf->dwFlags & DDPF_ALPHAPIXELS) &&
717                 (src_pf->u5.dwRGBAlphaBitMask != 0x00)) {
718                 error = TRUE;
719             } else {
720                 if ((src_pf->u2.dwRBitMask == 0xE0) &&
721                     (src_pf->u3.dwGBitMask == 0x1C) &&
722                     (src_pf->u4.dwBBitMask == 0x03)) {
723                     /* **********************
724                        GL_UNSIGNED_BYTE_3_3_2
725                        ********************** */
726                     if (colorkey_active) {
727                         /* This texture format will never be used.. So do not care about color keying
728                            up until the point in time it will be needed :-) */
729                         FIXME(" ColorKeying not supported in the RGB 332 format !\n");
730                     }
731                     current_format = GL_RGB;
732                     internal_format = GL_RGB;
733                     current_pixel_format = GL_UNSIGNED_BYTE_3_3_2;
734                     convert_type = NO_CONVERSION;
735                 } else {
736                     error = TRUE;
737                 }
738             }
739         } else if (src_pf->u1.dwRGBBitCount == 16) {
740             if ((src_pf->dwFlags & DDPF_ALPHAPIXELS) &&
741                 (src_pf->u5.dwRGBAlphaBitMask != 0x0000)) {
742                 if ((src_pf->u2.dwRBitMask ==        0xF800) &&
743                     (src_pf->u3.dwGBitMask ==        0x07C0) &&
744                     (src_pf->u4.dwBBitMask ==        0x003E) &&
745                     (src_pf->u5.dwRGBAlphaBitMask == 0x0001)) {
746                     current_format = GL_RGBA;
747                     internal_format = GL_RGBA;
748                     current_pixel_format = GL_UNSIGNED_SHORT_5_5_5_1;
749                     if (colorkey_active) {
750                         convert_type = CONVERT_CK_5551;
751                     } else {
752                         convert_type = NO_CONVERSION;
753                     }
754                 } else if ((src_pf->u2.dwRBitMask ==        0xF000) &&
755                            (src_pf->u3.dwGBitMask ==        0x0F00) &&
756                            (src_pf->u4.dwBBitMask ==        0x00F0) &&
757                            (src_pf->u5.dwRGBAlphaBitMask == 0x000F)) {
758                     current_format = GL_RGBA;
759                     internal_format = GL_RGBA;
760                     current_pixel_format = GL_UNSIGNED_SHORT_4_4_4_4;
761                     if (colorkey_active) {
762                         convert_type = CONVERT_CK_4444;
763                     } else {
764                         convert_type = NO_CONVERSION;
765                     }
766                 } else if ((src_pf->u2.dwRBitMask ==        0x0F00) &&
767                            (src_pf->u3.dwGBitMask ==        0x00F0) &&
768                            (src_pf->u4.dwBBitMask ==        0x000F) &&
769                            (src_pf->u5.dwRGBAlphaBitMask == 0xF000)) {
770                     if (colorkey_active) {
771                         convert_type = CONVERT_CK_4444_ARGB;
772                         current_format = GL_RGBA;
773                         internal_format = GL_RGBA;
774                         current_pixel_format = GL_UNSIGNED_SHORT_4_4_4_4;
775                     } else {
776                         convert_type = NO_CONVERSION;
777                         current_format = GL_BGRA;
778                         internal_format = GL_RGBA;
779                         current_pixel_format = GL_UNSIGNED_SHORT_4_4_4_4_REV;
780                     }
781                 } else if ((src_pf->u2.dwRBitMask ==        0x7C00) &&
782                            (src_pf->u3.dwGBitMask ==        0x03E0) &&
783                            (src_pf->u4.dwBBitMask ==        0x001F) &&
784                            (src_pf->u5.dwRGBAlphaBitMask == 0x8000)) {
785                     if (colorkey_active) {
786                         convert_type = CONVERT_CK_1555;
787                         current_format = GL_RGBA;
788                         internal_format = GL_RGBA;
789                         current_pixel_format = GL_UNSIGNED_SHORT_5_5_5_1;
790                     } else {
791                         convert_type = NO_CONVERSION;
792                         current_format = GL_BGRA;
793                         internal_format = GL_RGBA;
794                         current_pixel_format = GL_UNSIGNED_SHORT_1_5_5_5_REV;
795                     }
796                 } else {
797                     error = TRUE;
798                 }
799             } else {
800                 if ((src_pf->u2.dwRBitMask == 0xF800) &&
801                     (src_pf->u3.dwGBitMask == 0x07E0) &&
802                     (src_pf->u4.dwBBitMask == 0x001F)) {
803                     if (colorkey_active) {
804                         convert_type = CONVERT_CK_565;
805                         current_format = GL_RGBA;
806                         internal_format = GL_RGBA;
807                         current_pixel_format = GL_UNSIGNED_SHORT_5_5_5_1;
808                     } else {
809                         convert_type = NO_CONVERSION;
810                         current_format = GL_RGB;
811                         internal_format = GL_RGB;
812                         current_pixel_format = GL_UNSIGNED_SHORT_5_6_5;
813                     }
814                 } else if ((src_pf->u2.dwRBitMask == 0x7C00) &&
815                            (src_pf->u3.dwGBitMask == 0x03E0) &&
816                            (src_pf->u4.dwBBitMask == 0x001F)) {
817                     convert_type = CONVERT_555;
818                     current_format = GL_RGBA;
819                     internal_format = GL_RGBA;
820                     current_pixel_format = GL_UNSIGNED_SHORT_5_5_5_1;
821                 } else {
822                     error = TRUE;
823                 }
824             }
825         } else if (src_pf->u1.dwRGBBitCount == 24) {
826             if ((src_pf->dwFlags & DDPF_ALPHAPIXELS) &&
827                 (src_pf->u5.dwRGBAlphaBitMask != 0x000000)) {
828                 error = TRUE;
829             } else {
830                 if ((src_pf->u2.dwRBitMask == 0xFF0000) &&
831                     (src_pf->u3.dwGBitMask == 0x00FF00) &&
832                     (src_pf->u4.dwBBitMask == 0x0000FF)) {
833                     if (colorkey_active) {
834                         convert_type = CONVERT_CK_RGB24;
835                         current_format = GL_RGBA;
836                         internal_format = GL_RGBA;
837                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8;
838                     } else {
839                         convert_type = NO_CONVERSION;
840                         current_format = GL_BGR;
841                         internal_format = GL_RGB;
842                         current_pixel_format = GL_UNSIGNED_BYTE;
843                     }
844                 } else {
845                     error = TRUE;
846                 }
847             }
848         } else if (src_pf->u1.dwRGBBitCount == 32) {
849             if ((src_pf->dwFlags & DDPF_ALPHAPIXELS) &&
850                 (src_pf->u5.dwRGBAlphaBitMask != 0x00000000)) {
851                 if ((src_pf->u2.dwRBitMask ==        0xFF000000) &&
852                     (src_pf->u3.dwGBitMask ==        0x00FF0000) &&
853                     (src_pf->u4.dwBBitMask ==        0x0000FF00) &&
854                     (src_pf->u5.dwRGBAlphaBitMask == 0x000000FF)) {
855                     if (colorkey_active) {
856                         convert_type = CONVERT_CK_8888;
857                     } else {
858                         convert_type = NO_CONVERSION;
859                     }
860                     current_format = GL_RGBA;
861                     internal_format = GL_RGBA;
862                     current_pixel_format = GL_UNSIGNED_INT_8_8_8_8;
863                 } else if ((src_pf->u2.dwRBitMask ==        0x00FF0000) &&
864                            (src_pf->u3.dwGBitMask ==        0x0000FF00) &&
865                            (src_pf->u4.dwBBitMask ==        0x000000FF) &&
866                            (src_pf->u5.dwRGBAlphaBitMask == 0xFF000000)) {
867                     if (colorkey_active) {
868                         convert_type = CONVERT_CK_8888_ARGB;
869                         current_format = GL_RGBA;
870                         internal_format = GL_RGBA;
871                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8;
872                     } else {
873                         convert_type = NO_CONVERSION;
874                         current_format = GL_BGRA;
875                         internal_format = GL_RGBA;
876                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8_REV;
877                     }
878                 } else {
879                     error = TRUE;
880                 }
881             } else {
882                 if ((src_pf->u2.dwRBitMask == 0x00FF0000) &&
883                     (src_pf->u3.dwGBitMask == 0x0000FF00) &&
884                     (src_pf->u4.dwBBitMask == 0x000000FF)) {
885                     if (need_alpha_ck) {
886                         convert_type = CONVERT_RGB32_888;
887                         current_format = GL_RGBA;
888                         internal_format = GL_RGBA;
889                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8;
890                     } else {
891                         convert_type = NO_CONVERSION;
892                         current_format = GL_BGRA;
893                         internal_format = GL_RGBA;
894                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8_REV;
895                     }
896                 } else {
897                     error = TRUE;
898                 }
899             }
900         } else {
901             error = TRUE;
902         }
903     } else {
904         error = TRUE;
905     } 
906
907     if (error) {
908         ERR("Unsupported pixel format for textures : \n");
909         if (ERR_ON(ddraw)) {
910             DDRAW_dump_pixelformat(src_pf);
911         }
912         return DDERR_INVALIDPIXELFORMAT;
913     } else {
914         if ((need_to_alloc) ||
915             (internal_format != *current_internal_format)) {
916             glTexImage2D(GL_TEXTURE_2D, level, internal_format,
917                          tex_width, tex_height, 0,
918                          current_format, current_pixel_format, NULL);
919             *current_internal_format = internal_format;
920         }
921     }
922
923     if (sub_texture && (convert_type == NO_CONVERSION)) {
924         current_storage_width = surf_ptr->surface_desc.u1.lPitch / bpp;
925     } else {
926         if (surf_ptr->surface_desc.u1.lPitch == (surf_ptr->surface_desc.dwWidth * bpp)) {
927             current_storage_width = 0;
928         } else {
929             current_storage_width = surf_ptr->surface_desc.u1.lPitch / bpp;
930         }       
931     }
932     glPixelStorei(GL_UNPACK_ROW_LENGTH, current_storage_width);
933
934     TRACE(" initialized texture upload for level %d with conversion %d.\n", current_level, convert_type);
935     
936     return DD_OK;
937 }
938
939 HRESULT upload_surface_to_tex_memory(RECT *rect, DWORD xoffset, DWORD yoffset, void **temp_buffer)
940 {
941     const DDSURFACEDESC * const src_d = (DDSURFACEDESC *)&(current_surface->surface_desc);
942     void *surf_buffer = NULL;
943     RECT lrect;
944     DWORD width, height;
945     BYTE bpp = GET_BPP(current_surface->surface_desc);
946     int line_increase;
947     
948     if (rect == NULL) {
949         lrect.top = 0;
950         lrect.left = 0;
951         lrect.bottom = current_tex_height;
952         lrect.right = current_tex_width;
953         rect = &lrect;
954     }
955
956     width = rect->right - rect->left;
957     height = rect->bottom - rect->top;
958
959     if (current_surface->surface_desc.u4.ddpfPixelFormat.dwFlags & DDPF_FOURCC) {
960         GLint retVal;
961         int size = current_surface->surface_desc.u1.dwLinearSize;
962         int width_ = current_surface->surface_desc.dwWidth;
963         int height_ = current_surface->surface_desc.dwHeight;
964         LPVOID buffer = current_surface->surface_desc.lpSurface;
965
966         switch (current_surface->surface_desc.u4.ddpfPixelFormat.dwFourCC) {
967             case MAKE_FOURCC('D','X','T','1'): retVal = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break;
968             case MAKE_FOURCC('D','X','T','3'): retVal = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
969             case MAKE_FOURCC('D','X','T','5'): retVal = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
970             default:
971                 FIXME("Not supported\n");
972                 return DD_OK;
973         }
974
975         if (GL_extensions.s3tc_compressed_texture) {
976             /* GL_extensions.glCompressedTexSubImage2D(GL_TEXTURE_2D, current_level, xoffset, yoffset, width, height, retVal, (unsigned char*)temp_buffer); */
977             GL_extensions.glCompressedTexImage2D(GL_TEXTURE_2D, current_level, retVal, width_, height_, 0, size, buffer);
978         } else
979             ERR("Trying to upload S3TC texture whereas the device does not have support for it\n");
980
981         return DD_OK;
982     }
983     
984     /* Used when converting stuff */
985     line_increase = src_d->u1.lPitch - (width * bpp);
986
987     switch (convert_type) {
988         case CONVERT_PALETTED: {
989             IDirectDrawPaletteImpl* pal = current_surface->palette;
990             BYTE table[256][4];
991             unsigned int i;
992             unsigned int x, y;
993             BYTE *src = (BYTE *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
994             
995             if (pal == NULL) {
996                 /* Upload a black texture. The real one will be uploaded on palette change */
997                 WARN("Palettized texture Loading with a NULL palette !\n");
998                 memset(table, 0, 256 * 4);
999             } else {
1000                 /* Get the surface's palette */
1001                 for (i = 0; i < 256; i++) {
1002                     table[i][0] = pal->palents[i].peRed;
1003                     table[i][1] = pal->palents[i].peGreen;
1004                     table[i][2] = pal->palents[i].peBlue;
1005                     if ((src_d->dwFlags & DDSD_CKSRCBLT) &&
1006                         (i >= src_d->ddckCKSrcBlt.dwColorSpaceLowValue) &&
1007                         (i <= src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1008                         /* We should maybe here put a more 'neutral' color than the standard bright purple
1009                            one often used by application to prevent the nice purple borders when bi-linear
1010                            filtering is on */
1011                         table[i][3] = 0x00;
1012                     else
1013                         table[i][3] = 0xFF;
1014                 }
1015             }
1016             
1017             if (*temp_buffer == NULL)
1018                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
1019                                          current_tex_width * current_tex_height * sizeof(DWORD));
1020             dst = (BYTE *) *temp_buffer;
1021
1022             for (y = 0; y < height; y++) {
1023                 for (x = 0; x < width; x++) {
1024                     BYTE color = *src++;
1025                     *dst++ = table[color][0];
1026                     *dst++ = table[color][1];
1027                     *dst++ = table[color][2];
1028                     *dst++ = table[color][3];
1029                 }
1030                 src += line_increase;
1031             }
1032         } break;
1033
1034         case CONVERT_CK_565: {
1035             /* Converting the 565 format in 5551 packed to emulate color-keying.
1036                
1037                Note : in all these conversion, it would be best to average the averaging
1038                       pixels to get the color of the pixel that will be color-keyed to
1039                       prevent 'color bleeding'. This will be done later on if ever it is
1040                       too visible.
1041                       
1042                Note2: when using color-keying + alpha, are the alpha bits part of the
1043                       color-space or not ?
1044             */
1045             unsigned int x, y;
1046             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1047             
1048             if (*temp_buffer == NULL)
1049                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1050                                          current_tex_width * current_tex_height * sizeof(WORD));
1051             dst = (WORD *) *temp_buffer;
1052             
1053             for (y = 0; y < height; y++) {
1054                 for (x = 0; x < width; x++) {
1055                     WORD color = *src++;
1056                     *dst = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1057                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1058                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1059                         *dst |= 0x0001;
1060                     dst++;
1061                 }
1062                 src = (WORD *) (((BYTE *) src) + line_increase);
1063             }
1064         } break;
1065         
1066         case CONVERT_CK_5551: {
1067             /* Change the alpha value of the color-keyed pixels to emulate color-keying. */
1068             unsigned int x, y;
1069             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1070             
1071             if (*temp_buffer == NULL)
1072                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1073                                          current_tex_width * current_tex_height * sizeof(WORD));
1074             dst = (WORD *) *temp_buffer;
1075             
1076             for (y = 0; y < height; y++) {
1077                 for (x = 0; x < width; x++) {
1078                     WORD color = *src++;
1079                     *dst = color & 0xFFFE;
1080                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1081                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1082                         *dst |= color & 0x0001;
1083                     dst++;
1084                 }
1085                 src = (WORD *) (((BYTE *) src) + line_increase);
1086             }
1087         } break;
1088         
1089         case CONVERT_CK_4444: {
1090             /* Change the alpha value of the color-keyed pixels to emulate color-keying. */
1091             unsigned int x, y;
1092             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1093             
1094             if (*temp_buffer == NULL)
1095                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1096                                          current_tex_width * current_tex_height * sizeof(WORD));
1097             dst = (WORD *) *temp_buffer;
1098             
1099             for (y = 0; y < height; y++) {
1100                 for (x = 0; x < width; x++) {
1101                     WORD color = *src++;
1102                     *dst = color & 0xFFF0;
1103                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1104                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1105                         *dst |= color & 0x000F;
1106                     dst++;
1107                 }
1108                 src = (WORD *) (((BYTE *) src) + line_increase);
1109             }
1110         } break;
1111         
1112         case CONVERT_CK_4444_ARGB: {
1113             /* Move the four Alpha bits... */
1114             unsigned int x, y;
1115             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1116             
1117             if (*temp_buffer == NULL)
1118                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1119                                          current_tex_width * current_tex_height * sizeof(WORD));
1120             dst = (WORD *) *temp_buffer;
1121             
1122             for (y = 0; y < height; y++) {
1123                 for (x = 0; x < width; x++) {
1124                     WORD color = *src++;
1125                     *dst = (color & 0x0FFF) << 4;
1126                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1127                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1128                         *dst |= (color & 0xF000) >> 12;
1129                     dst++;
1130                 }
1131                 src = (WORD *) (((BYTE *) src) + line_increase);
1132             }
1133         } break;
1134         
1135         case CONVERT_CK_1555: {
1136             unsigned int x, y;
1137             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1138             
1139             if (*temp_buffer == NULL)
1140                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1141                                          current_tex_width * current_tex_height * sizeof(WORD));
1142             dst = (WORD *) *temp_buffer;
1143             
1144             for (y = 0; y < height; y++) {
1145                 for (x = 0; x < width; x++) {
1146                     WORD color = *src++;
1147                     *dst = (color & 0x7FFF) << 1;
1148                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1149                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1150                         *dst |= (color & 0x8000) >> 15;
1151                     dst++;
1152                 }
1153                 src = (WORD *) (((BYTE *) src) + line_increase);
1154             }
1155         } break;
1156         
1157         case CONVERT_555: {
1158             /* Converting the 0555 format in 5551 packed */
1159             unsigned int x, y;
1160             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1161             
1162             if (*temp_buffer == NULL)
1163                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1164                                          current_tex_width * current_tex_height * sizeof(WORD));
1165             dst = (WORD *) *temp_buffer;
1166             
1167             if (src_d->dwFlags & DDSD_CKSRCBLT) {
1168                 for (y = 0; y < height; y++) {
1169                     for (x = 0; x < width; x++) {
1170                         WORD color = *src++;
1171                         *dst = (color & 0x7FFF) << 1;
1172                         if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1173                             (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1174                             *dst |= 0x0001;
1175                         dst++;
1176                     }
1177                     src = (WORD *) (((BYTE *) src) + line_increase);
1178                 }
1179             } else {
1180                 for (y = 0; y < height; y++) {
1181                     for (x = 0; x < width; x++) {
1182                         WORD color = *src++;
1183                         *dst++ = ((color & 0x7FFF) << 1) | 0x0001;
1184                     }
1185                     src = (WORD *) (((BYTE *) src) + line_increase);
1186                 }
1187             }
1188             
1189         } break;
1190         
1191         case CONVERT_CK_RGB24: {
1192             /* This is a pain :-) */
1193             unsigned int x, y;
1194             BYTE *src = (BYTE *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top));
1195             DWORD *dst;
1196
1197             if (*temp_buffer == NULL)
1198                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1199                                          current_tex_width * current_tex_height * sizeof(DWORD));
1200             dst = (DWORD *) *temp_buffer;
1201
1202             for (y = 0; y < height; y++) {
1203                 for (x = 0; x < width; x++) {
1204                     DWORD color = *((DWORD *) src) & 0x00FFFFFF;
1205                     src += 3;
1206                     *dst = color << 8;
1207                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1208                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1209                         *dst |= 0xFF;
1210                     dst++;
1211                 }
1212                 src += line_increase;
1213             }
1214         } break;
1215
1216         case CONVERT_CK_8888: {
1217             /* Just use the alpha component to handle color-keying... */
1218             unsigned int x, y;
1219             DWORD *src = (DWORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1220             
1221             if (*temp_buffer == NULL)
1222                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1223                                          current_tex_width * current_tex_height * sizeof(DWORD));           
1224             dst = (DWORD *) *temp_buffer;
1225             
1226             for (y = 0; y < height; y++) {
1227                 for (x = 0; x < width; x++) {
1228                     DWORD color = *src++;
1229                     *dst = color & 0xFFFFFF00;
1230                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1231                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1232                         *dst |= color & 0x000000FF;
1233                     dst++;
1234                 }
1235                 src = (DWORD *) (((BYTE *) src) + line_increase);
1236             }
1237         } break;
1238         
1239         case CONVERT_CK_8888_ARGB: {
1240             unsigned int x, y;
1241             DWORD *src = (DWORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1242             
1243             if (*temp_buffer == NULL)
1244                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1245                                          current_tex_width * current_tex_height * sizeof(DWORD));           
1246             dst = (DWORD *) *temp_buffer;
1247             
1248             for (y = 0; y < height; y++) {
1249                 for (x = 0; x < width; x++) {
1250                     DWORD color = *src++;
1251                     *dst = (color & 0x00FFFFFF) << 8;
1252                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1253                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1254                         *dst |= (color & 0xFF000000) >> 24;
1255                     dst++;
1256                 }
1257                 src = (DWORD *) (((BYTE *) src) + line_increase);
1258             }
1259         } break;
1260         
1261         case CONVERT_RGB32_888: {
1262             /* Just add an alpha component and handle color-keying... */
1263             unsigned int x, y;
1264             DWORD *src = (DWORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1265             
1266             if (*temp_buffer == NULL)
1267                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1268                                          current_tex_width * current_tex_height * sizeof(DWORD));           
1269             dst = (DWORD *) *temp_buffer;
1270             
1271             if (src_d->dwFlags & DDSD_CKSRCBLT) {
1272                 for (y = 0; y < height; y++) {
1273                     for (x = 0; x < width; x++) {
1274                         DWORD color = *src++;
1275                         *dst = color << 8;
1276                         if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1277                             (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1278                             *dst |= 0xFF;
1279                         dst++;
1280                     }
1281                     src = (DWORD *) (((BYTE *) src) + line_increase);
1282                 }
1283             } else {
1284                 for (y = 0; y < height; y++) {
1285                     for (x = 0; x < width; x++) {
1286                         *dst++ = (*src++ << 8) | 0xFF;
1287                     }
1288                     src = (DWORD *) (((BYTE *) src) + line_increase);
1289                 }
1290             }
1291         } break;
1292
1293         case NO_CONVERSION:
1294             /* Nothing to do here as the name suggests... Just set-up the buffer correctly */
1295             surf_buffer = (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top));
1296             break;
1297     }
1298
1299     if (convert_type != NO_CONVERSION) {
1300         /* When doing conversion, the storage is always of width 'width' as there will never
1301            be any Pitch issue... For now :-)
1302         */
1303         surf_buffer = *temp_buffer;
1304         if (width != current_storage_width) {
1305             glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
1306             current_storage_width = width;
1307         }
1308     }
1309     
1310     glTexSubImage2D(GL_TEXTURE_2D,
1311                     current_level,
1312                     xoffset, yoffset,
1313                     width, height,
1314                     current_format,
1315                     current_pixel_format,
1316                     surf_buffer);
1317
1318     return DD_OK;
1319 }
1320
1321 HRESULT upload_surface_to_tex_memory_release(void)
1322 {
1323     current_surface = NULL;
1324
1325     return DD_OK;
1326 }