ifdef guard TCHAR declaration like PSDK does.
[wine.git] / dlls / ddraw / mesa.c
blob8bb19346d0d107d630a653ffd503fe5d243d6680
1 /* Direct3D Common functions
2 * Copyright (c) 1998 Lionel ULMER
4 * This file contains all MESA common code
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.
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.
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
21 #include "config.h"
23 #include <stdarg.h>
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"
35 #include "mesa_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
39 GLenum convert_D3D_compare_to_GL(D3DCMPFUNC dwRenderState)
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);
52 return GL_ALWAYS;
55 GLenum convert_D3D_stencilop_to_GL(D3DSTENCILOP dwRenderState)
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);
68 return GL_KEEP;
71 GLenum convert_D3D_blendop_to_GL(D3DBLEND dwRenderState)
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;
89 void set_render_state(IDirect3DDeviceImpl* This,
90 D3DRENDERSTATETYPE dwRenderStateType, STATEBLOCK *lpStateBlock)
92 DWORD dwRenderState = lpStateBlock->render_state[dwRenderStateType - 1];
93 IDirect3DDeviceGLImpl *glThis = (IDirect3DDeviceGLImpl *) This;
95 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType), dwRenderState);
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();
104 /* All others state variables */
105 switch (dwRenderStateType) {
106 case D3DRENDERSTATE_TEXTUREHANDLE: { /* 1 */
107 IDirectDrawSurfaceImpl *tex = (IDirectDrawSurfaceImpl*) dwRenderState;
109 IDirect3DDevice7_SetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
111 ICOM_INTERFACE(tex, IDirectDrawSurface7));
112 } break;
114 case D3DRENDERSTATE_ANTIALIAS: /* 2 */
115 if (dwRenderState)
116 ERR("D3DRENDERSTATE_ANTIALIAS not supported yet !\n");
117 break;
119 case D3DRENDERSTATE_TEXTUREADDRESSU: /* 44 */
120 case D3DRENDERSTATE_TEXTUREADDRESSV: /* 45 */
121 case D3DRENDERSTATE_TEXTUREADDRESS: { /* 3 */
122 D3DTEXTURESTAGESTATETYPE d3dTexStageStateType;
124 if (dwRenderStateType == D3DRENDERSTATE_TEXTUREADDRESS) d3dTexStageStateType = D3DTSS_ADDRESS;
125 else if (dwRenderStateType == D3DRENDERSTATE_TEXTUREADDRESSU) d3dTexStageStateType = D3DTSS_ADDRESSU;
126 else d3dTexStageStateType = D3DTSS_ADDRESSV;
128 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
129 0, d3dTexStageStateType,
130 dwRenderState);
131 } break;
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;
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;
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.
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;
164 } else if (dwRenderState == D3DZB_FALSE) {
165 if (glThis->depth_test == TRUE) {
166 glDisable(GL_DEPTH_TEST);
167 glThis->depth_test = FALSE;
169 } else {
170 if (glThis->depth_test == FALSE) {
171 glEnable(GL_DEPTH_TEST);
172 glThis->depth_test = TRUE;
174 WARN(" w-buffering not supported.\n");
176 break;
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);
192 break;
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);
205 break;
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;
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;
223 case D3DRENDERSTATE_TEXTUREMAG: { /* 17 */
224 DWORD tex_mag = 0xFFFFFFFF;
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);
237 if (tex_mag != 0xFFFFFFFF) {
238 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7), 0, D3DTSS_MAGFILTER, tex_mag);
240 } break;
242 case D3DRENDERSTATE_TEXTUREMIN: { /* 18 */
243 DWORD tex_min = 0xFFFFFFFF;
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);
256 if (tex_min != 0xFFFFFFFF) {
257 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7), 0, D3DTSS_MINFILTER, tex_min);
259 } break;
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;
267 case D3DRENDERSTATE_TEXTUREMAPBLEND: { /* 21 */
268 IDirect3DDevice7 *d3ddev = ICOM_INTERFACE(This, IDirect3DDevice7);
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;
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;
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;
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);
300 } break;
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;
309 break;
310 case D3DCULL_CW:
311 if (glThis->cull_face == 0) {
312 glEnable(GL_CULL_FACE);
313 glThis->cull_face = 1;
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;
323 glFrontFace(GL_CW);
324 glCullFace(GL_BACK);
325 break;
326 default:
327 ERR("Unhandled cull mode %ld !\n",dwRenderState);
329 break;
331 case D3DRENDERSTATE_ZFUNC: /* 23 */
332 glDepthFunc(convert_D3D_compare_to_GL(dwRenderState));
333 break;
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;
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;
346 break;
348 case D3DRENDERSTATE_DITHERENABLE: /* 26 */
349 if (dwRenderState)
350 glEnable(GL_DITHER);
351 else
352 glDisable(GL_DITHER);
353 break;
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);
361 glThis->blending = dwRenderState;
363 /* Hack for some old games ... */
364 if (glThis->version == 1) {
365 lpStateBlock->render_state[D3DRENDERSTATE_COLORKEYENABLE - 1] = dwRenderState;
367 break;
369 case D3DRENDERSTATE_FOGENABLE: /* 28 */
370 /* Nothing to do here. Only the storage matters :-) */
371 break;
373 case D3DRENDERSTATE_SPECULARENABLE: /* 29 */
374 if (dwRenderState)
375 ERR(" Specular Lighting not supported yet.\n");
376 break;
378 case D3DRENDERSTATE_SUBPIXEL: /* 31 */
379 case D3DRENDERSTATE_SUBPIXELX: /* 32 */
380 /* We do not support this anyway, so why protest :-) */
381 break;
383 case D3DRENDERSTATE_STIPPLEDALPHA: /* 33 */
384 if (dwRenderState)
385 ERR(" Stippled Alpha not supported yet.\n");
386 break;
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;
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;
405 case D3DRENDERSTATE_FOGDENSITY: /* 38 */
406 glFogi(GL_FOG_DENSITY,*(float*)&dwRenderState);
407 break;
409 case D3DRENDERSTATE_COLORKEYENABLE: /* 41 */
410 /* Nothing done here, only storage matters. */
411 break;
413 case D3DRENDERSTATE_MIPMAPLODBIAS: /* 46 */
414 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
415 0, D3DTSS_MIPMAPLODBIAS,
416 dwRenderState);
417 break;
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);
431 break;
433 case D3DRENDERSTATE_FLUSHBATCH: /* 50 */
434 break;
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;
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;
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;
460 case D3DRENDERSTATE_STENCILWRITEMASK: /* 59 */
461 glStencilMask(dwRenderState);
462 break;
464 case D3DRENDERSTATE_TEXTUREFACTOR: /* 60 */
465 /* Only the storage matters... */
466 break;
468 case D3DRENDERSTATE_CLIPPING: /* 136 */
469 case D3DRENDERSTATE_CLIPPLANEENABLE: { /* 152 */
470 GLint i;
471 DWORD mask, runner;
473 if (dwRenderStateType == D3DRENDERSTATE_CLIPPING) {
474 mask = ((dwRenderState) ?
475 (This->state_block.render_state[D3DRENDERSTATE_CLIPPLANEENABLE - 1]) : (0x00000000));
476 } else {
477 mask = dwRenderState;
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.
488 glThis->transform_state = GL_TRANSFORM_NONE;
490 } else {
491 glDisable(GL_CLIP_PLANE0 + i);
495 break;
497 case D3DRENDERSTATE_LIGHTING: /* 137 */
498 /* Nothing to do, only storage matters... */
499 break;
501 case D3DRENDERSTATE_AMBIENT: { /* 139 */
502 float light[4];
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;
511 case D3DRENDERSTATE_COLORVERTEX: /* 141 */
512 /* Nothing to do here.. Only storage matters */
513 break;
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;
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);
530 break;
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;
539 default:
540 ERR("Unhandled dwRenderStateType %s (%08x) value : %08lx !\n",
541 _get_renderstate(dwRenderStateType), dwRenderStateType, dwRenderState);
543 LEAVE_GL();
547 void store_render_state(IDirect3DDeviceImpl *This,
548 D3DRENDERSTATETYPE dwRenderStateType, DWORD dwRenderState, STATEBLOCK *lpStateBlock)
550 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType), dwRenderState);
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;
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;
578 /* Default case */
579 lpStateBlock->render_state[dwRenderStateType - 1] = dwRenderState;
582 void get_render_state(IDirect3DDeviceImpl *This,
583 D3DRENDERSTATETYPE dwRenderStateType, LPDWORD lpdwRenderState, STATEBLOCK *lpStateBlock)
585 *lpdwRenderState = lpStateBlock->render_state[dwRenderStateType - 1];
586 if (TRACE_ON(ddraw))
587 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType), *lpdwRenderState);
590 void apply_render_state(IDirect3DDeviceImpl *This, STATEBLOCK *lpStateBlock)
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);
600 /* Texture management code.
602 - upload_surface_to_tex_memory_init initialize the code and computes the GL formats
603 according to the surface description.
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.
609 - upload_surface_to_tex_memory_release does the clean-up.
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.
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;
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;
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)
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;
654 current_surface = surf_ptr;
655 current_level = level;
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;
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;
672 tex_width = surf_ptr->surface_desc.dwWidth;
673 tex_height = surf_ptr->surface_desc.dwHeight;
676 current_tex_width = tex_width;
677 current_tex_height = tex_height;
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");
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;
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;
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;
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;
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;
772 } else {
773 error = TRUE;
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;
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;
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;
820 } else {
821 error = TRUE;
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;
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;
854 } else {
855 error = TRUE;
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;
872 } else {
873 error = TRUE;
876 } else {
877 error = TRUE;
879 } else {
880 error = TRUE;
883 if (error == TRUE) {
884 ERR("Unsupported pixel format for textures : \n");
885 if (ERR_ON(ddraw)) {
886 DDRAW_dump_pixelformat(src_pf);
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;
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;
908 glPixelStorei(GL_UNPACK_ROW_LENGTH, current_storage_width);
910 TRACE(" initialized texture upload for level %d with conversion %d.\n", current_level, convert_type);
912 return DD_OK;
915 HRESULT upload_surface_to_tex_memory(RECT *rect, DWORD xoffset, DWORD yoffset, void **temp_buffer)
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;
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;
932 width = rect->right - rect->left;
933 height = rect->bottom - rect->top;
935 /* Used when converting stuff */
936 line_increase = src_d->u1.lPitch - (width * bpp);
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;
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;
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;
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];
981 src += line_increase;
983 } break;
985 case CONVERT_CK_565: {
986 /* Converting the 565 format in 5551 packed to emulate color-keying.
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.
993 Note2: when using color-keying + alpha, are the alpha bits part of the
994 color-space or not ?
996 int x, y;
997 WORD *src = (WORD *) (((BYTE *) src_d->lpSurface) + (bpp * rect->left) + (src_d->u1.lPitch * rect->top)), *dst;
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;
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++;
1013 src = (WORD *) (((BYTE *) src) + line_increase);
1015 } break;
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;
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;
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++;
1036 src = (WORD *) (((BYTE *) src) + line_increase);
1038 } break;
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;
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;
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++;
1059 src = (WORD *) (((BYTE *) src) + line_increase);
1061 } break;
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;
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;
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++;
1082 src = (WORD *) (((BYTE *) src) + line_increase);
1084 } break;
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;
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;
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++;
1104 src = (WORD *) (((BYTE *) src) + line_increase);
1106 } break;
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;
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;
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++;
1128 src = (WORD *) (((BYTE *) src) + line_increase);
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;
1136 src = (WORD *) (((BYTE *) src) + line_increase);
1140 } break;
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;
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;
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++;
1163 src += line_increase;
1165 } break;
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;
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;
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++;
1186 src = (DWORD *) (((BYTE *) src) + line_increase);
1188 } break;
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;
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;
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++;
1208 src = (DWORD *) (((BYTE *) src) + line_increase);
1210 } break;
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;
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;
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++;
1232 src = (DWORD *) (((BYTE *) src) + line_increase);
1234 } else {
1235 for (y = 0; y < height; y++) {
1236 for (x = 0; x < width; x++) {
1237 *dst++ = (*src++ << 8) | 0xFF;
1239 src = (DWORD *) (((BYTE *) src) + line_increase);
1242 } break;
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;
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 :-)
1254 surf_buffer = *temp_buffer;
1255 if (width != current_storage_width) {
1256 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
1257 current_storage_width = width;
1261 glTexSubImage2D(GL_TEXTURE_2D,
1262 current_level,
1263 xoffset, yoffset,
1264 width, height,
1265 current_format,
1266 current_pixel_format,
1267 surf_buffer);
1269 return DD_OK;
1272 HRESULT upload_surface_to_tex_memory_release(void)
1274 current_surface = NULL;
1276 return DD_OK;