Fix subclassing to support nested messages.
[wine] / dlls / ddraw / mesa.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 "mesa_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 == TRUE) {
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->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_SRCALPHA;
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_INVSRCALPHA;
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     /* First, do some sanity checks ... */
658     if ((surf_ptr->surface_desc.u1.lPitch % bpp) != 0) {
659         FIXME("Warning : pitch is not a multiple of BPP - not supported yet !\n");
660     } else {
661         /* In that case, no need to have any alignement constraints... */
662         if (current_alignement_constraints != 1) {
663             glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
664             current_alignement_constraints = 1;
665         }
666     }
667
668     /* Note: we only check width here as you cannot have width non-zero while height is set to zero */
669     if (tex_width == 0) {
670         sub_texture = FALSE;
671         
672         tex_width = surf_ptr->surface_desc.dwWidth;
673         tex_height = surf_ptr->surface_desc.dwHeight;
674     }
675
676     current_tex_width = tex_width;
677     current_tex_height = tex_height;
678
679     if (src_pf->dwFlags & DDPF_PALETTEINDEXED8) {
680         /* ****************
681            Paletted Texture
682            **************** */
683         current_format = GL_RGBA;
684         internal_format = GL_RGBA;
685         current_pixel_format = GL_UNSIGNED_BYTE;
686         convert_type = CONVERT_PALETTED;
687     } else if (src_pf->dwFlags & DDPF_RGB) {
688         /* ************
689            RGB Textures
690            ************ */
691         if (src_pf->u1.dwRGBBitCount == 8) {
692             if ((src_pf->dwFlags & DDPF_ALPHAPIXELS) &&
693                 (src_pf->u5.dwRGBAlphaBitMask != 0x00)) {
694                 error = TRUE;
695             } else {
696                 if ((src_pf->u2.dwRBitMask == 0xE0) &&
697                     (src_pf->u3.dwGBitMask == 0x1C) &&
698                     (src_pf->u4.dwBBitMask == 0x03)) {
699                     /* **********************
700                        GL_UNSIGNED_BYTE_3_3_2
701                        ********************** */
702                     if (colorkey_active) {
703                         /* This texture format will never be used.. So do not care about color keying
704                            up until the point in time it will be needed :-) */
705                         FIXME(" ColorKeying not supported in the RGB 332 format !\n");
706                     }
707                     current_format = GL_RGB;
708                     internal_format = GL_RGB;
709                     current_pixel_format = GL_UNSIGNED_BYTE_3_3_2;
710                     convert_type = NO_CONVERSION;
711                 } else {
712                     error = TRUE;
713                 }
714             }
715         } else if (src_pf->u1.dwRGBBitCount == 16) {
716             if ((src_pf->dwFlags & DDPF_ALPHAPIXELS) &&
717                 (src_pf->u5.dwRGBAlphaBitMask != 0x0000)) {
718                 if ((src_pf->u2.dwRBitMask ==        0xF800) &&
719                     (src_pf->u3.dwGBitMask ==        0x07C0) &&
720                     (src_pf->u4.dwBBitMask ==        0x003E) &&
721                     (src_pf->u5.dwRGBAlphaBitMask == 0x0001)) {
722                     current_format = GL_RGBA;
723                     internal_format = GL_RGBA;
724                     current_pixel_format = GL_UNSIGNED_SHORT_5_5_5_1;
725                     if (colorkey_active) {
726                         convert_type = CONVERT_CK_5551;
727                     } else {
728                         convert_type = NO_CONVERSION;
729                     }
730                 } else if ((src_pf->u2.dwRBitMask ==        0xF000) &&
731                            (src_pf->u3.dwGBitMask ==        0x0F00) &&
732                            (src_pf->u4.dwBBitMask ==        0x00F0) &&
733                            (src_pf->u5.dwRGBAlphaBitMask == 0x000F)) {
734                     current_format = GL_RGBA;
735                     internal_format = GL_RGBA;
736                     current_pixel_format = GL_UNSIGNED_SHORT_4_4_4_4;
737                     if (colorkey_active) {
738                         convert_type = CONVERT_CK_4444;
739                     } else {
740                         convert_type = NO_CONVERSION;
741                     }
742                 } else if ((src_pf->u2.dwRBitMask ==        0x0F00) &&
743                            (src_pf->u3.dwGBitMask ==        0x00F0) &&
744                            (src_pf->u4.dwBBitMask ==        0x000F) &&
745                            (src_pf->u5.dwRGBAlphaBitMask == 0xF000)) {
746                     if (colorkey_active) {
747                         convert_type = CONVERT_CK_4444_ARGB;
748                         current_format = GL_RGBA;
749                         internal_format = GL_RGBA;
750                         current_pixel_format = GL_UNSIGNED_SHORT_4_4_4_4;
751                     } else {
752                         convert_type = NO_CONVERSION;
753                         current_format = GL_BGRA;
754                         internal_format = GL_RGBA;
755                         current_pixel_format = GL_UNSIGNED_SHORT_4_4_4_4_REV;
756                     }
757                 } else if ((src_pf->u2.dwRBitMask ==        0x7C00) &&
758                            (src_pf->u3.dwGBitMask ==        0x03E0) &&
759                            (src_pf->u4.dwBBitMask ==        0x001F) &&
760                            (src_pf->u5.dwRGBAlphaBitMask == 0x8000)) {
761                     if (colorkey_active) {
762                         convert_type = CONVERT_CK_1555;
763                         current_format = GL_RGBA;
764                         internal_format = GL_RGBA;
765                         current_pixel_format = GL_UNSIGNED_SHORT_5_5_5_1;
766                     } else {
767                         convert_type = NO_CONVERSION;
768                         current_format = GL_BGRA;
769                         internal_format = GL_RGBA;
770                         current_pixel_format = GL_UNSIGNED_SHORT_1_5_5_5_REV;
771                     }
772                 } else {
773                     error = TRUE;
774                 }
775             } else {
776                 if ((src_pf->u2.dwRBitMask == 0xF800) &&
777                     (src_pf->u3.dwGBitMask == 0x07E0) &&
778                     (src_pf->u4.dwBBitMask == 0x001F)) {
779                     if (colorkey_active) {
780                         convert_type = CONVERT_CK_565;
781                         current_format = GL_RGBA;
782                         internal_format = GL_RGBA;
783                         current_pixel_format = GL_UNSIGNED_SHORT_5_5_5_1;
784                     } else {
785                         convert_type = NO_CONVERSION;
786                         current_format = GL_RGB;
787                         internal_format = GL_RGB;
788                         current_pixel_format = GL_UNSIGNED_SHORT_5_6_5;
789                     }
790                 } else if ((src_pf->u2.dwRBitMask == 0x7C00) &&
791                            (src_pf->u3.dwGBitMask == 0x03E0) &&
792                            (src_pf->u4.dwBBitMask == 0x001F)) {
793                     convert_type = CONVERT_555;
794                     current_format = GL_RGBA;
795                     internal_format = GL_RGBA;
796                     current_pixel_format = GL_UNSIGNED_SHORT_5_5_5_1;
797                 } else {
798                     error = TRUE;
799                 }
800             }
801         } else if (src_pf->u1.dwRGBBitCount == 24) {
802             if ((src_pf->dwFlags & DDPF_ALPHAPIXELS) &&
803                 (src_pf->u5.dwRGBAlphaBitMask != 0x000000)) {
804                 error = TRUE;
805             } else {
806                 if ((src_pf->u2.dwRBitMask == 0xFF0000) &&
807                     (src_pf->u3.dwGBitMask == 0x00FF00) &&
808                     (src_pf->u4.dwBBitMask == 0x0000FF)) {
809                     if (colorkey_active) {
810                         convert_type = CONVERT_CK_RGB24;
811                         current_format = GL_RGBA;
812                         internal_format = GL_RGBA;
813                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8;
814                     } else {
815                         convert_type = NO_CONVERSION;
816                         current_format = GL_BGR;
817                         internal_format = GL_RGB;
818                         current_pixel_format = GL_UNSIGNED_BYTE;
819                     }
820                 } else {
821                     error = TRUE;
822                 }
823             }
824         } else if (src_pf->u1.dwRGBBitCount == 32) {
825             if ((src_pf->dwFlags & DDPF_ALPHAPIXELS) &&
826                 (src_pf->u5.dwRGBAlphaBitMask != 0x00000000)) {
827                 if ((src_pf->u2.dwRBitMask ==        0xFF000000) &&
828                     (src_pf->u3.dwGBitMask ==        0x00FF0000) &&
829                     (src_pf->u4.dwBBitMask ==        0x0000FF00) &&
830                     (src_pf->u5.dwRGBAlphaBitMask == 0x000000FF)) {
831                     if (colorkey_active) {
832                         convert_type = CONVERT_CK_8888;
833                     } else {
834                         convert_type = NO_CONVERSION;
835                     }
836                     current_format = GL_RGBA;
837                     internal_format = GL_RGBA;
838                     current_pixel_format = GL_UNSIGNED_INT_8_8_8_8;
839                 } else if ((src_pf->u2.dwRBitMask ==        0x00FF0000) &&
840                            (src_pf->u3.dwGBitMask ==        0x0000FF00) &&
841                            (src_pf->u4.dwBBitMask ==        0x000000FF) &&
842                            (src_pf->u5.dwRGBAlphaBitMask == 0xFF000000)) {
843                     if (colorkey_active) {
844                         convert_type = CONVERT_CK_8888_ARGB;
845                         current_format = GL_RGBA;
846                         internal_format = GL_RGBA;
847                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8;
848                     } else {
849                         convert_type = NO_CONVERSION;
850                         current_format = GL_BGRA;
851                         internal_format = GL_RGBA;
852                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8_REV;
853                     }
854                 } else {
855                     error = TRUE;
856                 }
857             } else {
858                 if ((src_pf->u2.dwRBitMask == 0x00FF0000) &&
859                     (src_pf->u3.dwGBitMask == 0x0000FF00) &&
860                     (src_pf->u4.dwBBitMask == 0x000000FF)) {
861                     if (need_alpha_ck == TRUE) {
862                         convert_type = CONVERT_RGB32_888;
863                         current_format = GL_RGBA;
864                         internal_format = GL_RGBA;
865                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8;
866                     } else {
867                         convert_type = NO_CONVERSION;
868                         current_format = GL_BGRA;
869                         internal_format = GL_RGBA;
870                         current_pixel_format = GL_UNSIGNED_INT_8_8_8_8_REV;
871                     }
872                 } else {
873                     error = TRUE;
874                 }
875             }
876         } else {
877             error = TRUE;
878         }
879     } else {
880         error = TRUE;
881     } 
882
883     if (error == TRUE) {
884         ERR("Unsupported pixel format for textures : \n");
885         if (ERR_ON(ddraw)) {
886             DDRAW_dump_pixelformat(src_pf);
887         }
888         return DDERR_INVALIDPIXELFORMAT;
889     } else {
890         if ((need_to_alloc) ||
891             (internal_format != *current_internal_format)) {
892             glTexImage2D(GL_TEXTURE_2D, level, internal_format,
893                          tex_width, tex_height, 0,
894                          current_format, current_pixel_format, NULL);
895             *current_internal_format = internal_format;
896         }
897     }
898
899     if ((sub_texture == TRUE) && (convert_type == NO_CONVERSION)) {
900         current_storage_width = surf_ptr->surface_desc.u1.lPitch / bpp;
901     } else {
902         if (surf_ptr->surface_desc.u1.lPitch == (surf_ptr->surface_desc.dwWidth * bpp)) {
903             current_storage_width = 0;
904         } else {
905             current_storage_width = surf_ptr->surface_desc.u1.lPitch / bpp;
906         }       
907     }
908     glPixelStorei(GL_UNPACK_ROW_LENGTH, current_storage_width);
909
910     TRACE(" initialized texture upload for level %d with conversion %d.\n", current_level, convert_type);
911     
912     return DD_OK;
913 }
914
915 HRESULT upload_surface_to_tex_memory(RECT *rect, DWORD xoffset, DWORD yoffset, void **temp_buffer)
916 {
917     const DDSURFACEDESC * const src_d = (DDSURFACEDESC *)&(current_surface->surface_desc);
918     void *surf_buffer = NULL;
919     RECT lrect;
920     DWORD width, height;
921     BYTE bpp = GET_BPP(current_surface->surface_desc);
922     int line_increase;
923     
924     if (rect == NULL) {
925         lrect.top = 0;
926         lrect.left = 0;
927         lrect.bottom = current_tex_height;
928         lrect.right = current_tex_width;
929         rect = &lrect;
930     }
931
932     width = rect->right - rect->left;
933     height = rect->bottom - rect->top;
934
935     /* Used when converting stuff */
936     line_increase = src_d->u1.lPitch - (width * bpp);
937
938     switch (convert_type) {
939         case CONVERT_PALETTED: {
940             IDirectDrawPaletteImpl* pal = current_surface->palette;
941             BYTE table[256][4];
942             int i;
943             int x, y;
944             BYTE *src = (BYTE *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
945             
946             if (pal == NULL) {
947                 /* Upload a black texture. The real one will be uploaded on palette change */
948                 WARN("Palettized texture Loading with a NULL palette !\n");
949                 memset(table, 0, 256 * 4);
950             } else {
951                 /* Get the surface's palette */
952                 for (i = 0; i < 256; i++) {
953                     table[i][0] = pal->palents[i].peRed;
954                     table[i][1] = pal->palents[i].peGreen;
955                     table[i][2] = pal->palents[i].peBlue;
956                     if ((src_d->dwFlags & DDSD_CKSRCBLT) &&
957                         (i >= src_d->ddckCKSrcBlt.dwColorSpaceLowValue) &&
958                         (i <= src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
959                         /* We should maybe here put a more 'neutral' color than the standard bright purple
960                            one often used by application to prevent the nice purple borders when bi-linear
961                            filtering is on */
962                         table[i][3] = 0x00;
963                     else
964                         table[i][3] = 0xFF;
965                 }
966             }
967             
968             if (*temp_buffer == NULL)
969                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
970                                          current_tex_width * current_tex_height * sizeof(DWORD));
971             dst = (BYTE *) *temp_buffer;
972
973             for (y = 0; y < height; y++) {
974                 for (x = 0; x < width; x++) {
975                     BYTE color = *src++;
976                     *dst++ = table[color][0];
977                     *dst++ = table[color][1];
978                     *dst++ = table[color][2];
979                     *dst++ = table[color][3];
980                 }
981                 src += line_increase;
982             }
983         } break;
984
985         case CONVERT_CK_565: {
986             /* Converting the 565 format in 5551 packed to emulate color-keying.
987                
988                Note : in all these conversion, it would be best to average the averaging
989                       pixels to get the color of the pixel that will be color-keyed to
990                       prevent 'color bleeding'. This will be done later on if ever it is
991                       too visible.
992                       
993                Note2: when using color-keying + alpha, are the alpha bits part of the
994                       color-space or not ?
995             */
996             int x, y;
997             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
998             
999             if (*temp_buffer == NULL)
1000                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1001                                          current_tex_width * current_tex_height * sizeof(WORD));
1002             dst = (WORD *) *temp_buffer;
1003             
1004             for (y = 0; y < height; y++) {
1005                 for (x = 0; x < width; x++) {
1006                     WORD color = *src++;
1007                     *dst = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1008                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1009                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1010                         *dst |= 0x0001;
1011                     dst++;
1012                 }
1013                 src = (WORD *) (((BYTE *) src) + line_increase);
1014             }
1015         } break;
1016         
1017         case CONVERT_CK_5551: {
1018             /* Change the alpha value of the color-keyed pixels to emulate color-keying. */
1019             int x, y;
1020             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1021             
1022             if (*temp_buffer == NULL)
1023                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1024                                          current_tex_width * current_tex_height * sizeof(WORD));
1025             dst = (WORD *) *temp_buffer;
1026             
1027             for (y = 0; y < height; y++) {
1028                 for (x = 0; x < width; x++) {
1029                     WORD color = *src++;
1030                     *dst = color & 0xFFFE;
1031                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1032                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1033                         *dst |= color & 0x0001;
1034                     dst++;
1035                 }
1036                 src = (WORD *) (((BYTE *) src) + line_increase);
1037             }
1038         } break;
1039         
1040         case CONVERT_CK_4444: {
1041             /* Change the alpha value of the color-keyed pixels to emulate color-keying. */
1042             int x, y;
1043             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1044             
1045             if (*temp_buffer == NULL)
1046                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1047                                          current_tex_width * current_tex_height * sizeof(WORD));
1048             dst = (WORD *) *temp_buffer;
1049             
1050             for (y = 0; y < height; y++) {
1051                 for (x = 0; x < width; x++) {
1052                     WORD color = *src++;
1053                     *dst = color & 0xFFF0;
1054                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1055                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1056                         *dst |= color & 0x000F;
1057                     dst++;
1058                 }
1059                 src = (WORD *) (((BYTE *) src) + line_increase);
1060             }
1061         } break;
1062         
1063         case CONVERT_CK_4444_ARGB: {
1064             /* Move the four Alpha bits... */
1065             int x, y;
1066             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1067             
1068             if (*temp_buffer == NULL)
1069                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1070                                          current_tex_width * current_tex_height * sizeof(WORD));
1071             dst = (WORD *) *temp_buffer;
1072             
1073             for (y = 0; y < height; y++) {
1074                 for (x = 0; x < width; x++) {
1075                     WORD color = *src++;
1076                     *dst = (color & 0x0FFF) << 4;
1077                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1078                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1079                         *dst |= (color & 0xF000) >> 12;
1080                     dst++;
1081                 }
1082                 src = (WORD *) (((BYTE *) src) + line_increase);
1083             }
1084         } break;
1085         
1086         case CONVERT_CK_1555: {
1087             int x, y;
1088             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1089             
1090             if (*temp_buffer == NULL)
1091                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1092                                          current_tex_width * current_tex_height * sizeof(WORD));
1093             dst = (WORD *) *temp_buffer;
1094             
1095             for (y = 0; y < height; y++) {
1096                 for (x = 0; x < width; x++) {
1097                     WORD color = *src++;
1098                     *dst = (color & 0x7FFF) << 1;
1099                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1100                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1101                         *dst |= (color & 0x8000) >> 15;
1102                     dst++;
1103                 }
1104                 src = (WORD *) (((BYTE *) src) + line_increase);
1105             }
1106         } break;
1107         
1108         case CONVERT_555: {
1109             /* Converting the 0555 format in 5551 packed */
1110             int x, y;
1111             WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1112             
1113             if (*temp_buffer == NULL)
1114                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1115                                          current_tex_width * current_tex_height * sizeof(WORD));
1116             dst = (WORD *) *temp_buffer;
1117             
1118             if (src_d->dwFlags & DDSD_CKSRCBLT) {
1119                 for (y = 0; y < height; y++) {
1120                     for (x = 0; x < width; x++) {
1121                         WORD color = *src++;
1122                         *dst = (color & 0x7FFF) << 1;
1123                         if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1124                             (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1125                             *dst |= 0x0001;
1126                         dst++;
1127                     }
1128                     src = (WORD *) (((BYTE *) src) + line_increase);
1129                 }
1130             } else {
1131                 for (y = 0; y < height; y++) {
1132                     for (x = 0; x < width; x++) {
1133                         WORD color = *src++;
1134                         *dst++ = ((color & 0x7FFF) << 1) | 0x0001;
1135                     }
1136                     src = (WORD *) (((BYTE *) src) + line_increase);
1137                 }
1138             }
1139             
1140         } break;
1141         
1142         case CONVERT_CK_RGB24: {
1143             /* This is a pain :-) */
1144             int x, y;
1145             BYTE *src = (BYTE *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top));
1146             DWORD *dst;
1147             
1148             if (*temp_buffer == NULL)
1149                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1150                                          current_tex_width * current_tex_height * sizeof(DWORD));
1151             dst = (DWORD *) *temp_buffer;
1152
1153             for (y = 0; y < height; y++) {
1154                 for (x = 0; x < width; x++) {
1155                     DWORD color = *((DWORD *) src) & 0x00FFFFFF;
1156                     src += 3;
1157                     *dst = *src++ << 8;
1158                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1159                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1160                         *dst |= 0xFF;
1161                     dst++;
1162                 }
1163                 src += line_increase;
1164             }
1165         } break;
1166
1167         case CONVERT_CK_8888: {
1168             /* Just use the alpha component to handle color-keying... */
1169             int x, y;
1170             DWORD *src = (DWORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1171             
1172             if (*temp_buffer == NULL)
1173                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1174                                          current_tex_width * current_tex_height * sizeof(DWORD));           
1175             dst = (DWORD *) *temp_buffer;
1176             
1177             for (y = 0; y < height; y++) {
1178                 for (x = 0; x < width; x++) {
1179                     DWORD color = *src++;
1180                     *dst = color & 0xFFFFFF00;
1181                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1182                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1183                         *dst |= color & 0x000000FF;
1184                     dst++;
1185                 }
1186                 src = (DWORD *) (((BYTE *) src) + line_increase);
1187             }
1188         } break;
1189         
1190         case CONVERT_CK_8888_ARGB: {
1191             int x, y;
1192             DWORD *src = (DWORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1193             
1194             if (*temp_buffer == NULL)
1195                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1196                                          current_tex_width * current_tex_height * sizeof(DWORD));           
1197             dst = (DWORD *) *temp_buffer;
1198             
1199             for (y = 0; y < height; y++) {
1200                 for (x = 0; x < width; x++) {
1201                     DWORD color = *src++;
1202                     *dst = (color & 0x00FFFFFF) << 8;
1203                     if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1204                         (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1205                         *dst |= (color & 0xFF000000) >> 24;
1206                     dst++;
1207                 }
1208                 src = (DWORD *) (((BYTE *) src) + line_increase);
1209             }
1210         } break;
1211         
1212         case CONVERT_RGB32_888: {
1213             /* Just add an alpha component and handle color-keying... */
1214             int x, y;
1215             DWORD *src = (DWORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
1216             
1217             if (*temp_buffer == NULL)
1218                 *temp_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1219                                          current_tex_width * current_tex_height * sizeof(DWORD));           
1220             dst = (DWORD *) *temp_buffer;
1221             
1222             if (src_d->dwFlags & DDSD_CKSRCBLT) {
1223                 for (y = 0; y < height; y++) {
1224                     for (x = 0; x < width; x++) {
1225                         DWORD color = *src++;
1226                         *dst = color << 8;
1227                         if ((color < src_d->ddckCKSrcBlt.dwColorSpaceLowValue) ||
1228                             (color > src_d->ddckCKSrcBlt.dwColorSpaceHighValue))
1229                             *dst |= 0xFF;
1230                         dst++;
1231                     }
1232                     src = (DWORD *) (((BYTE *) src) + line_increase);
1233                 }
1234             } else {
1235                 for (y = 0; y < height; y++) {
1236                     for (x = 0; x < width; x++) {
1237                         *dst++ = (*src++ << 8) | 0xFF;
1238                     }
1239                     src = (DWORD *) (((BYTE *) src) + line_increase);
1240                 }
1241             }
1242         } break;
1243
1244         case NO_CONVERSION:
1245             /* Nothing to do here as the name suggests... Just set-up the buffer correctly */
1246             surf_buffer = (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top));
1247             break;
1248     }
1249
1250     if (convert_type != NO_CONVERSION) {
1251         /* When doing conversion, the storage is always of width 'width' as there will never
1252            be any Pitch issue... For now :-)
1253         */
1254         surf_buffer = *temp_buffer;
1255         if (width != current_storage_width) {
1256             glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
1257             current_storage_width = width;
1258         }
1259     }
1260     
1261     glTexSubImage2D(GL_TEXTURE_2D,
1262                     current_level,
1263                     xoffset, yoffset,
1264                     width, height,
1265                     current_format,
1266                     current_pixel_format,
1267                     surf_buffer);
1268
1269     return DD_OK;
1270 }
1271
1272 HRESULT upload_surface_to_tex_memory_release(void)
1273 {
1274     current_surface = NULL;
1275
1276     return DD_OK;
1277 }