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
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
29 #include "wine/debug.h"
31 #include "mesa_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
35 GLenum
convert_D3D_compare_to_GL(D3DCMPFUNC dwRenderState
)
37 switch (dwRenderState
) {
38 case D3DCMP_NEVER
: return GL_NEVER
;
39 case D3DCMP_LESS
: return GL_LESS
;
40 case D3DCMP_EQUAL
: return GL_EQUAL
;
41 case D3DCMP_LESSEQUAL
: return GL_LEQUAL
;
42 case D3DCMP_GREATER
: return GL_GREATER
;
43 case D3DCMP_NOTEQUAL
: return GL_NOTEQUAL
;
44 case D3DCMP_GREATEREQUAL
: return GL_GEQUAL
;
45 case D3DCMP_ALWAYS
: return GL_ALWAYS
;
46 default: ERR("Unexpected compare type %d !\n", dwRenderState
);
51 GLenum
convert_D3D_stencilop_to_GL(D3DSTENCILOP dwRenderState
)
53 switch (dwRenderState
) {
54 case D3DSTENCILOP_KEEP
: return GL_KEEP
;
55 case D3DSTENCILOP_ZERO
: return GL_ZERO
;
56 case D3DSTENCILOP_REPLACE
: return GL_REPLACE
;
57 case D3DSTENCILOP_INCRSAT
: return GL_INCR
;
58 case D3DSTENCILOP_DECRSAT
: return GL_DECR
;
59 case D3DSTENCILOP_INVERT
: return GL_INVERT
;
60 case D3DSTENCILOP_INCR
: WARN("D3DSTENCILOP_INCR not properly handled !\n"); return GL_INCR
;
61 case D3DSTENCILOP_DECR
: WARN("D3DSTENCILOP_DECR not properly handled !\n"); return GL_DECR
;
62 default: ERR("Unexpected compare type %d !\n", dwRenderState
);
67 GLenum
convert_D3D_blendop_to_GL(D3DBLEND dwRenderState
)
69 switch ((D3DBLEND
) dwRenderState
) {
70 case D3DBLEND_ZERO
: return GL_ZERO
;
71 case D3DBLEND_ONE
: return GL_ONE
;
72 case D3DBLEND_SRCALPHA
: return GL_SRC_ALPHA
;
73 case D3DBLEND_INVSRCALPHA
: return GL_ONE_MINUS_SRC_ALPHA
;
74 case D3DBLEND_DESTALPHA
: return GL_DST_ALPHA
;
75 case D3DBLEND_INVDESTALPHA
: return GL_ONE_MINUS_DST_ALPHA
;
76 case D3DBLEND_DESTCOLOR
: return GL_DST_COLOR
;
77 case D3DBLEND_INVDESTCOLOR
: return GL_ONE_MINUS_DST_COLOR
;
78 case D3DBLEND_SRCALPHASAT
: return GL_SRC_ALPHA_SATURATE
;
79 case D3DBLEND_SRCCOLOR
: return GL_SRC_COLOR
;
80 case D3DBLEND_INVSRCCOLOR
: return GL_ONE_MINUS_SRC_COLOR
;
81 default: ERR("Unhandled blend mode %d !\n", dwRenderState
); return GL_ZERO
;
85 void set_render_state(IDirect3DDeviceImpl
* This
,
86 D3DRENDERSTATETYPE dwRenderStateType
, STATEBLOCK
*lpStateBlock
)
88 DWORD dwRenderState
= lpStateBlock
->render_state
[dwRenderStateType
- 1];
89 IDirect3DDeviceGLImpl
*glThis
= (IDirect3DDeviceGLImpl
*) This
;
91 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType
), dwRenderState
);
93 /* First, all the stipple patterns */
94 if ((dwRenderStateType
>= D3DRENDERSTATE_STIPPLEPATTERN00
) &&
95 (dwRenderStateType
<= D3DRENDERSTATE_STIPPLEPATTERN31
)) {
96 ERR("Unhandled dwRenderStateType stipple %d!\n",dwRenderStateType
);
100 /* All others state variables */
101 switch (dwRenderStateType
) {
102 case D3DRENDERSTATE_TEXTUREHANDLE
: { /* 1 */
103 IDirectDrawSurfaceImpl
*tex
= (IDirectDrawSurfaceImpl
*) dwRenderState
;
105 IDirect3DDevice7_SetTexture(ICOM_INTERFACE(This
, IDirect3DDevice7
),
107 ICOM_INTERFACE(tex
, IDirectDrawSurface7
));
110 case D3DRENDERSTATE_ANTIALIAS
: /* 2 */
112 ERR("D3DRENDERSTATE_ANTIALIAS not supported yet !\n");
115 case D3DRENDERSTATE_TEXTUREADDRESSU
: /* 44 */
116 case D3DRENDERSTATE_TEXTUREADDRESSV
: /* 45 */
117 case D3DRENDERSTATE_TEXTUREADDRESS
: { /* 3 */
118 D3DTEXTURESTAGESTATETYPE d3dTexStageStateType
;
120 if (dwRenderStateType
== D3DRENDERSTATE_TEXTUREADDRESS
) d3dTexStageStateType
= D3DTSS_ADDRESS
;
121 else if (dwRenderStateType
== D3DRENDERSTATE_TEXTUREADDRESSU
) d3dTexStageStateType
= D3DTSS_ADDRESSU
;
122 else d3dTexStageStateType
= D3DTSS_ADDRESSV
;
124 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This
, IDirect3DDevice7
),
125 0, d3dTexStageStateType
,
129 case D3DRENDERSTATE_TEXTUREPERSPECTIVE
: /* 4 */
131 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
);
133 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
136 case D3DRENDERSTATE_WRAPU
: /* 5 */
137 case D3DRENDERSTATE_WRAPV
: /* 6 */
138 case D3DRENDERSTATE_WRAP0
: /* 128 */
139 case D3DRENDERSTATE_WRAP1
: /* 129 */
140 case D3DRENDERSTATE_WRAP2
: /* 130 */
141 case D3DRENDERSTATE_WRAP3
: /* 131 */
142 case D3DRENDERSTATE_WRAP4
: /* 132 */
143 case D3DRENDERSTATE_WRAP5
: /* 133 */
144 case D3DRENDERSTATE_WRAP6
: /* 134 */
145 case D3DRENDERSTATE_WRAP7
: /* 135 */
147 ERR("Texture WRAP modes unsupported by OpenGL.. Expect graphical glitches !\n");
150 case D3DRENDERSTATE_ZENABLE
: /* 7 */
151 /* To investigate : in OpenGL, if we disable the depth test, the Z buffer will NOT be
152 updated either.. No idea about what happens in D3D.
154 Maybe replacing the Z function by ALWAYS would be a better idea. */
155 if (dwRenderState
== D3DZB_TRUE
) {
156 if (glThis
->depth_test
== FALSE
) {
157 glEnable(GL_DEPTH_TEST
);
158 glThis
->depth_test
= TRUE
;
160 } else if (dwRenderState
== D3DZB_FALSE
) {
161 if (glThis
->depth_test
== TRUE
) {
162 glDisable(GL_DEPTH_TEST
);
163 glThis
->depth_test
= FALSE
;
166 if (glThis
->depth_test
== FALSE
) {
167 glEnable(GL_DEPTH_TEST
);
168 glThis
->depth_test
= TRUE
;
170 WARN(" w-buffering not supported.\n");
174 case D3DRENDERSTATE_FILLMODE
: /* 8 */
175 switch ((D3DFILLMODE
) dwRenderState
) {
177 glPolygonMode(GL_FRONT_AND_BACK
,GL_POINT
);
179 case D3DFILL_WIREFRAME
:
180 glPolygonMode(GL_FRONT_AND_BACK
,GL_LINE
);
183 glPolygonMode(GL_FRONT_AND_BACK
,GL_FILL
);
186 ERR("Unhandled fill mode %ld !\n",dwRenderState
);
190 case D3DRENDERSTATE_SHADEMODE
: /* 9 */
191 switch ((D3DSHADEMODE
) dwRenderState
) {
193 glShadeModel(GL_FLAT
);
195 case D3DSHADE_GOURAUD
:
196 glShadeModel(GL_SMOOTH
);
199 ERR("Unhandled shade mode %ld !\n",dwRenderState
);
203 case D3DRENDERSTATE_ZWRITEENABLE
: /* 14 */
204 if ((dwRenderState
!= FALSE
) && (glThis
->depth_mask
== FALSE
))
205 glDepthMask(GL_TRUE
);
206 else if ((dwRenderState
== FALSE
) && (glThis
->depth_mask
!= FALSE
))
207 glDepthMask(GL_FALSE
);
208 glThis
->depth_mask
= dwRenderState
;
211 case D3DRENDERSTATE_ALPHATESTENABLE
: /* 15 */
212 if ((dwRenderState
!= 0) && (glThis
->alpha_test
== FALSE
))
213 glEnable(GL_ALPHA_TEST
);
214 else if ((dwRenderState
== 0) && (glThis
->alpha_test
!= FALSE
))
215 glDisable(GL_ALPHA_TEST
);
216 glThis
->alpha_test
= dwRenderState
;
219 case D3DRENDERSTATE_TEXTUREMAG
: { /* 17 */
220 DWORD tex_mag
= 0xFFFFFFFF;
222 switch ((D3DTEXTUREFILTER
) dwRenderState
) {
223 case D3DFILTER_NEAREST
:
224 tex_mag
= D3DTFG_POINT
;
226 case D3DFILTER_LINEAR
:
227 tex_mag
= D3DTFG_LINEAR
;
230 ERR("Unhandled texture mag %ld !\n",dwRenderState
);
233 if (tex_mag
!= 0xFFFFFFFF) {
234 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This
, IDirect3DDevice7
), 0, D3DTSS_MAGFILTER
, tex_mag
);
238 case D3DRENDERSTATE_TEXTUREMIN
: { /* 18 */
239 DWORD tex_min
= 0xFFFFFFFF;
241 switch ((D3DTEXTUREFILTER
) dwRenderState
) {
242 case D3DFILTER_NEAREST
:
243 tex_min
= D3DTFN_POINT
;
245 case D3DFILTER_LINEAR
:
246 tex_min
= D3DTFN_LINEAR
;
249 ERR("Unhandled texture min %ld !\n",dwRenderState
);
252 if (tex_min
!= 0xFFFFFFFF) {
253 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This
, IDirect3DDevice7
), 0, D3DTSS_MINFILTER
, tex_min
);
257 case D3DRENDERSTATE_SRCBLEND
: /* 19 */
258 case D3DRENDERSTATE_DESTBLEND
: /* 20 */
259 glBlendFunc(convert_D3D_blendop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_SRCBLEND
- 1]),
260 convert_D3D_blendop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_DESTBLEND
- 1]));
263 case D3DRENDERSTATE_TEXTUREMAPBLEND
: { /* 21 */
264 IDirect3DDevice7
*d3ddev
= ICOM_INTERFACE(This
, IDirect3DDevice7
);
266 switch ((D3DTEXTUREBLEND
) dwRenderState
) {
267 case D3DTBLEND_DECAL
:
268 if (glThis
->current_tex_env
!= GL_REPLACE
) {
269 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
270 glThis
->current_tex_env
= GL_REPLACE
;
273 case D3DTBLEND_DECALALPHA
:
274 if (glThis
->current_tex_env
!= GL_REPLACE
) {
275 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
276 glThis
->current_tex_env
= GL_DECAL
;
279 case D3DTBLEND_MODULATE
:
280 if (glThis
->current_tex_env
!= GL_MODULATE
) {
281 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
282 glThis
->current_tex_env
= GL_MODULATE
;
285 case D3DTBLEND_MODULATEALPHA
:
286 IDirect3DDevice7_SetTextureStageState(d3ddev
, 0, D3DTSS_COLORARG1
, D3DTA_TEXTURE
);
287 IDirect3DDevice7_SetTextureStageState(d3ddev
, 0, D3DTSS_ALPHAARG1
, D3DTA_TEXTURE
);
288 IDirect3DDevice7_SetTextureStageState(d3ddev
, 0, D3DTSS_COLORARG2
, D3DTA_CURRENT
);
289 IDirect3DDevice7_SetTextureStageState(d3ddev
, 0, D3DTSS_ALPHAARG2
, D3DTA_CURRENT
);
290 IDirect3DDevice7_SetTextureStageState(d3ddev
, 0, D3DTSS_COLOROP
, D3DTOP_MODULATE
);
291 IDirect3DDevice7_SetTextureStageState(d3ddev
, 0, D3DTSS_ALPHAOP
, D3DTOP_MODULATE
);
294 ERR("Unhandled texture environment %ld !\n",dwRenderState
);
298 case D3DRENDERSTATE_CULLMODE
: /* 22 */
299 switch ((D3DCULL
) dwRenderState
) {
301 if (glThis
->cull_face
!= 0) {
302 glDisable(GL_CULL_FACE
);
303 glThis
->cull_face
= 0;
307 if (glThis
->cull_face
== 0) {
308 glEnable(GL_CULL_FACE
);
309 glThis
->cull_face
= 1;
315 if (glThis
->cull_face
== 0) {
316 glEnable(GL_CULL_FACE
);
317 glThis
->cull_face
= 1;
323 ERR("Unhandled cull mode %ld !\n",dwRenderState
);
327 case D3DRENDERSTATE_ZFUNC
: /* 23 */
328 glDepthFunc(convert_D3D_compare_to_GL(dwRenderState
));
331 case D3DRENDERSTATE_ALPHAREF
: /* 24 */
332 case D3DRENDERSTATE_ALPHAFUNC
: /* 25 */
333 glAlphaFunc(convert_D3D_compare_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_ALPHAFUNC
- 1]),
334 (lpStateBlock
->render_state
[D3DRENDERSTATE_ALPHAREF
- 1] & 0x000000FF) / 255.0);
337 case D3DRENDERSTATE_DITHERENABLE
: /* 26 */
341 glDisable(GL_DITHER
);
344 case D3DRENDERSTATE_ALPHABLENDENABLE
: /* 27 */
345 if ((dwRenderState
!= 0) && (glThis
->blending
== 0)) {
347 } else if ((dwRenderState
== 0) && (glThis
->blending
!= 0)) {
350 glThis
->blending
= dwRenderState
;
353 case D3DRENDERSTATE_FOGENABLE
: /* 28 */
354 /* Nothing to do here. Only the storage matters :-) */
357 case D3DRENDERSTATE_SPECULARENABLE
: /* 29 */
359 ERR(" Specular Lighting not supported yet.\n");
362 case D3DRENDERSTATE_SUBPIXEL
: /* 31 */
363 case D3DRENDERSTATE_SUBPIXELX
: /* 32 */
364 /* We do not support this anyway, so why protest :-) */
367 case D3DRENDERSTATE_STIPPLEDALPHA
: /* 33 */
369 ERR(" Stippled Alpha not supported yet.\n");
372 case D3DRENDERSTATE_FOGCOLOR
: { /* 34 */
374 color
[0] = ((dwRenderState
>> 16) & 0xFF)/255.0f
;
375 color
[1] = ((dwRenderState
>> 8) & 0xFF)/255.0f
;
376 color
[2] = ((dwRenderState
>> 0) & 0xFF)/255.0f
;
377 color
[3] = ((dwRenderState
>> 24) & 0xFF)/255.0f
;
378 glFogfv(GL_FOG_COLOR
,color
);
379 /* Note: glFogiv does not seem to work */
382 case D3DRENDERSTATE_FOGTABLEMODE
: /* 35 */
383 case D3DRENDERSTATE_FOGVERTEXMODE
: /* 140 */
384 case D3DRENDERSTATE_FOGSTART
: /* 36 */
385 case D3DRENDERSTATE_FOGEND
: /* 37 */
386 /* Nothing to do here. Only the storage matters :-) */
389 case D3DRENDERSTATE_FOGDENSITY
: /* 38 */
390 glFogi(GL_FOG_DENSITY
,*(float*)&dwRenderState
);
393 case D3DRENDERSTATE_COLORKEYENABLE
: /* 41 */
394 /* This needs to be fixed. */
395 if ((dwRenderState
!= 0) && (glThis
->blending
== 0)) {
397 } else if ((dwRenderState
== 0) && (glThis
->blending
!= 0)) {
400 glThis
->blending
= dwRenderState
;
403 case D3DRENDERSTATE_ZBIAS
: /* 47 */
404 /* This is a tad bit hacky.. But well, no idea how to do it better in OpenGL :-/ */
405 if (dwRenderState
== 0) {
406 glDisable(GL_POLYGON_OFFSET_FILL
);
407 glDisable(GL_POLYGON_OFFSET_LINE
);
408 glDisable(GL_POLYGON_OFFSET_POINT
);
410 glEnable(GL_POLYGON_OFFSET_FILL
);
411 glEnable(GL_POLYGON_OFFSET_LINE
);
412 glEnable(GL_POLYGON_OFFSET_POINT
);
413 glPolygonOffset(1.0, dwRenderState
* 1.0);
417 case D3DRENDERSTATE_FLUSHBATCH
: /* 50 */
420 case D3DRENDERSTATE_STENCILENABLE
: /* 52 */
421 if ((dwRenderState
!= 0) && (glThis
->stencil_test
== 0))
422 glEnable(GL_STENCIL_TEST
);
423 else if ((dwRenderState
== 0) && (glThis
->stencil_test
!= 0))
424 glDisable(GL_STENCIL_TEST
);
425 glThis
->stencil_test
= dwRenderState
;
428 case D3DRENDERSTATE_STENCILFAIL
: /* 53 */
429 case D3DRENDERSTATE_STENCILZFAIL
: /* 54 */
430 case D3DRENDERSTATE_STENCILPASS
: /* 55 */
431 glStencilOp(convert_D3D_stencilop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILFAIL
- 1]),
432 convert_D3D_stencilop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILZFAIL
- 1]),
433 convert_D3D_stencilop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILPASS
- 1]));
436 case D3DRENDERSTATE_STENCILFUNC
: /* 56 */
437 case D3DRENDERSTATE_STENCILREF
: /* 57 */
438 case D3DRENDERSTATE_STENCILMASK
: /* 58 */
439 glStencilFunc(convert_D3D_compare_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILFUNC
- 1]),
440 lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILREF
- 1],
441 lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILMASK
- 1]);
444 case D3DRENDERSTATE_STENCILWRITEMASK
: /* 59 */
445 glStencilMask(dwRenderState
);
448 case D3DRENDERSTATE_TEXTUREFACTOR
: /* 60 */
449 /* Only the storage matters... */
452 case D3DRENDERSTATE_CLIPPING
: /* 136 */
453 case D3DRENDERSTATE_CLIPPLANEENABLE
: { /* 152 */
457 if (dwRenderStateType
== D3DRENDERSTATE_CLIPPING
) {
458 mask
= ((dwRenderState
) ?
459 (This
->state_block
.render_state
[D3DRENDERSTATE_CLIPPLANEENABLE
- 1]) : (0x00000000));
461 mask
= dwRenderState
;
463 for (i
= 0, runner
= 0x00000001; i
< This
->max_clipping_planes
; i
++, runner
= (runner
<< 1)) {
466 glGetIntegerv(GL_CLIP_PLANE0
+ i
, &enabled
);
467 if (enabled
== GL_FALSE
) {
468 glEnable(GL_CLIP_PLANE0
+ i
);
469 /* Need to force a transform change so that this clipping plane parameters are sent
472 glThis
->transform_state
= GL_TRANSFORM_NONE
;
475 glDisable(GL_CLIP_PLANE0
+ i
);
481 case D3DRENDERSTATE_LIGHTING
: /* 137 */
482 /* Nothing to do, only storage matters... */
485 case D3DRENDERSTATE_AMBIENT
: { /* 139 */
488 light
[0] = ((dwRenderState
>> 16) & 0xFF) / 255.0;
489 light
[1] = ((dwRenderState
>> 8) & 0xFF) / 255.0;
490 light
[2] = ((dwRenderState
>> 0) & 0xFF) / 255.0;
491 light
[3] = ((dwRenderState
>> 24) & 0xFF) / 255.0;
492 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, (float *) light
);
495 case D3DRENDERSTATE_COLORVERTEX
: /* 141 */
496 /* Nothing to do here.. Only storage matters */
499 case D3DRENDERSTATE_LOCALVIEWER
: /* 142 */
501 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
503 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_FALSE
);
506 case D3DRENDERSTATE_NORMALIZENORMALS
: /* 143 */
508 glEnable(GL_NORMALIZE
);
509 glEnable(GL_RESCALE_NORMAL
);
511 glDisable(GL_NORMALIZE
);
512 glDisable(GL_RESCALE_NORMAL
);
516 case D3DRENDERSTATE_DIFFUSEMATERIALSOURCE
: /* 145 */
517 case D3DRENDERSTATE_SPECULARMATERIALSOURCE
: /* 146 */
518 case D3DRENDERSTATE_AMBIENTMATERIALSOURCE
: /* 147 */
519 case D3DRENDERSTATE_EMISSIVEMATERIALSOURCE
: /* 148 */
520 /* Nothing to do here. Only the storage matters :-) */
524 ERR("Unhandled dwRenderStateType %s (%08x) value : %08lx !\n",
525 _get_renderstate(dwRenderStateType
), dwRenderStateType
, dwRenderState
);
531 void store_render_state(IDirect3DDeviceImpl
*This
,
532 D3DRENDERSTATETYPE dwRenderStateType
, DWORD dwRenderState
, STATEBLOCK
*lpStateBlock
)
534 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType
), dwRenderState
);
536 /* Some special cases first.. */
537 if (dwRenderStateType
== D3DRENDERSTATE_SRCBLEND
) {
538 if (dwRenderState
== D3DBLEND_BOTHSRCALPHA
) {
539 lpStateBlock
->render_state
[D3DRENDERSTATE_SRCBLEND
- 1] = D3DBLEND_SRCALPHA
;
540 lpStateBlock
->render_state
[D3DRENDERSTATE_DESTBLEND
- 1] = D3DBLEND_SRCALPHA
;
542 } else if (dwRenderState
== D3DBLEND_BOTHINVSRCALPHA
) {
543 lpStateBlock
->render_state
[D3DRENDERSTATE_SRCBLEND
- 1] = D3DBLEND_INVSRCALPHA
;
544 lpStateBlock
->render_state
[D3DRENDERSTATE_DESTBLEND
- 1] = D3DBLEND_INVSRCALPHA
;
547 } else if (dwRenderStateType
== D3DRENDERSTATE_TEXTUREADDRESS
) {
548 lpStateBlock
->render_state
[D3DRENDERSTATE_TEXTUREADDRESSU
- 1] = dwRenderState
;
549 lpStateBlock
->render_state
[D3DRENDERSTATE_TEXTUREADDRESSV
- 1] = dwRenderState
;
550 } else if (dwRenderStateType
== D3DRENDERSTATE_WRAPU
) {
552 lpStateBlock
->render_state
[D3DRENDERSTATE_WRAP0
] |= D3DWRAP_U
;
554 lpStateBlock
->render_state
[D3DRENDERSTATE_WRAP0
] &= ~D3DWRAP_U
;
555 } else if (dwRenderStateType
== D3DRENDERSTATE_WRAPV
) {
557 lpStateBlock
->render_state
[D3DRENDERSTATE_WRAP0
] |= D3DWRAP_V
;
559 lpStateBlock
->render_state
[D3DRENDERSTATE_WRAP0
] &= ~D3DWRAP_V
;
563 lpStateBlock
->render_state
[dwRenderStateType
- 1] = dwRenderState
;
566 void get_render_state(IDirect3DDeviceImpl
*This
,
567 D3DRENDERSTATETYPE dwRenderStateType
, LPDWORD lpdwRenderState
, STATEBLOCK
*lpStateBlock
)
569 *lpdwRenderState
= lpStateBlock
->render_state
[dwRenderStateType
- 1];
571 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType
), *lpdwRenderState
);
574 void apply_render_state(IDirect3DDeviceImpl
*This
, STATEBLOCK
*lpStateBlock
)
577 TRACE("(%p,%p)\n", This
, lpStateBlock
);
578 for(i
= 0; i
< HIGHEST_RENDER_STATE
; i
++)
579 if (lpStateBlock
->set_flags
.render_state
[i
])
580 set_render_state(This
, i
+ 1, lpStateBlock
);
584 /* Texture management code.
586 - upload_surface_to_tex_memory_init initialize the code and computes the GL formats
587 according to the surface description.
589 - upload_surface_to_tex_memory does the real upload. If one buffer is split over
590 multiple textures, this can be called multiple times after the '_init' call. 'rect'
591 can be NULL if the whole buffer needs to be upload.
593 - upload_surface_to_tex_memory_release does the clean-up.
595 These functions are called in the following cases :
596 - texture management (ie to upload a D3D texture to GL when it changes).
597 - flush of the 'in-memory' frame buffer to the GL frame buffer using the texture
599 - use of the texture engine to simulate Blits to the 3D Device.
607 CONVERT_CK_4444_ARGB
,
612 CONVERT_CK_8888_ARGB
,
616 /* Note : we suppose that all the code calling this is protected by the GL lock... Otherwise bad things
618 static GLenum current_format
;
619 static GLenum current_pixel_format
;
620 static CONVERT_TYPES convert_type
;
621 static IDirectDrawSurfaceImpl
*current_surface
;
622 static GLuint current_level
;
623 static DWORD current_tex_width
;
624 static DWORD current_tex_height
;
625 static GLuint current_alignement_constraints
;
626 static int current_storage_width
;
628 HRESULT
upload_surface_to_tex_memory_init(IDirectDrawSurfaceImpl
*surf_ptr
, GLuint level
, GLenum
*current_internal_format
,
629 BOOLEAN need_to_alloc
, BOOLEAN need_alpha_ck
, DWORD tex_width
, DWORD tex_height
)
631 const DDPIXELFORMAT
* const src_pf
= &(surf_ptr
->surface_desc
.u4
.ddpfPixelFormat
);
633 BOOL colorkey_active
= need_alpha_ck
&& (surf_ptr
->surface_desc
.dwFlags
& DDSD_CKSRCBLT
);
634 GLenum internal_format
= GL_LUMINANCE
; /* A bogus value to be sure to have a nice Mesa warning :-) */
635 BYTE bpp
= GET_BPP(surf_ptr
->surface_desc
);
636 BOOL sub_texture
= TRUE
;
638 current_surface
= surf_ptr
;
639 current_level
= level
;
641 /* First, do some sanity checks ... */
642 if ((surf_ptr
->surface_desc
.u1
.lPitch
% bpp
) != 0) {
643 FIXME("Warning : pitch is not a multiple of BPP - not supported yet !\n");
645 /* In that case, no need to have any alignement constraints... */
646 if (current_alignement_constraints
!= 1) {
647 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
648 current_alignement_constraints
= 1;
652 /* Note: we only check width here as you cannot have width non-zero while height is set to zero */
653 if (tex_width
== 0) {
656 tex_width
= surf_ptr
->surface_desc
.dwWidth
;
657 tex_height
= surf_ptr
->surface_desc
.dwHeight
;
660 current_tex_width
= tex_width
;
661 current_tex_height
= tex_height
;
663 if (src_pf
->dwFlags
& DDPF_PALETTEINDEXED8
) {
667 current_format
= GL_RGBA
;
668 internal_format
= GL_RGBA
;
669 current_pixel_format
= GL_UNSIGNED_BYTE
;
670 convert_type
= CONVERT_PALETTED
;
671 } else if (src_pf
->dwFlags
& DDPF_RGB
) {
675 if (src_pf
->u1
.dwRGBBitCount
== 8) {
676 if ((src_pf
->dwFlags
& DDPF_ALPHAPIXELS
) &&
677 (src_pf
->u5
.dwRGBAlphaBitMask
!= 0x00)) {
680 if ((src_pf
->u2
.dwRBitMask
== 0xE0) &&
681 (src_pf
->u3
.dwGBitMask
== 0x1C) &&
682 (src_pf
->u4
.dwBBitMask
== 0x03)) {
683 /* **********************
684 GL_UNSIGNED_BYTE_3_3_2
685 ********************** */
686 if (colorkey_active
) {
687 /* This texture format will never be used.. So do not care about color keying
688 up until the point in time it will be needed :-) */
689 FIXME(" ColorKeying not supported in the RGB 332 format !");
691 current_format
= GL_RGB
;
692 internal_format
= GL_RGB
;
693 current_pixel_format
= GL_UNSIGNED_BYTE_3_3_2
;
694 convert_type
= NO_CONVERSION
;
699 } else if (src_pf
->u1
.dwRGBBitCount
== 16) {
700 if ((src_pf
->dwFlags
& DDPF_ALPHAPIXELS
) &&
701 (src_pf
->u5
.dwRGBAlphaBitMask
!= 0x0000)) {
702 if ((src_pf
->u2
.dwRBitMask
== 0xF800) &&
703 (src_pf
->u3
.dwGBitMask
== 0x07C0) &&
704 (src_pf
->u4
.dwBBitMask
== 0x003E) &&
705 (src_pf
->u5
.dwRGBAlphaBitMask
== 0x0001)) {
706 current_format
= GL_RGBA
;
707 internal_format
= GL_RGBA
;
708 current_pixel_format
= GL_UNSIGNED_SHORT_5_5_5_1
;
709 if (colorkey_active
) {
710 convert_type
= CONVERT_CK_5551
;
712 convert_type
= NO_CONVERSION
;
714 } else if ((src_pf
->u2
.dwRBitMask
== 0xF000) &&
715 (src_pf
->u3
.dwGBitMask
== 0x0F00) &&
716 (src_pf
->u4
.dwBBitMask
== 0x00F0) &&
717 (src_pf
->u5
.dwRGBAlphaBitMask
== 0x000F)) {
718 current_format
= GL_RGBA
;
719 internal_format
= GL_RGBA
;
720 current_pixel_format
= GL_UNSIGNED_SHORT_4_4_4_4
;
721 if (colorkey_active
) {
722 convert_type
= CONVERT_CK_4444
;
724 convert_type
= NO_CONVERSION
;
726 } else if ((src_pf
->u2
.dwRBitMask
== 0x0F00) &&
727 (src_pf
->u3
.dwGBitMask
== 0x00F0) &&
728 (src_pf
->u4
.dwBBitMask
== 0x000F) &&
729 (src_pf
->u5
.dwRGBAlphaBitMask
== 0xF000)) {
730 if (colorkey_active
) {
731 convert_type
= CONVERT_CK_4444_ARGB
;
732 current_format
= GL_RGBA
;
733 internal_format
= GL_RGBA
;
734 current_pixel_format
= GL_UNSIGNED_SHORT_4_4_4_4
;
736 convert_type
= NO_CONVERSION
;
737 current_format
= GL_BGRA
;
738 internal_format
= GL_RGBA
;
739 current_pixel_format
= GL_UNSIGNED_SHORT_4_4_4_4_REV
;
741 } else if ((src_pf
->u2
.dwRBitMask
== 0x7C00) &&
742 (src_pf
->u3
.dwGBitMask
== 0x03E0) &&
743 (src_pf
->u4
.dwBBitMask
== 0x001F) &&
744 (src_pf
->u5
.dwRGBAlphaBitMask
== 0x8000)) {
745 if (colorkey_active
) {
746 convert_type
= CONVERT_CK_1555
;
747 current_format
= GL_RGBA
;
748 internal_format
= GL_RGBA
;
749 current_pixel_format
= GL_UNSIGNED_SHORT_5_5_5_1
;
751 convert_type
= NO_CONVERSION
;
752 current_format
= GL_BGRA
;
753 internal_format
= GL_RGBA
;
754 current_pixel_format
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
760 if ((src_pf
->u2
.dwRBitMask
== 0xF800) &&
761 (src_pf
->u3
.dwGBitMask
== 0x07E0) &&
762 (src_pf
->u4
.dwBBitMask
== 0x001F)) {
763 if (colorkey_active
) {
764 convert_type
= CONVERT_CK_565
;
765 current_format
= GL_RGBA
;
766 internal_format
= GL_RGBA
;
767 current_pixel_format
= GL_UNSIGNED_SHORT_5_5_5_1
;
769 convert_type
= NO_CONVERSION
;
770 current_format
= GL_RGB
;
771 internal_format
= GL_RGB
;
772 current_pixel_format
= GL_UNSIGNED_SHORT_5_6_5
;
774 } else if ((src_pf
->u2
.dwRBitMask
== 0x7C00) &&
775 (src_pf
->u3
.dwGBitMask
== 0x03E0) &&
776 (src_pf
->u4
.dwBBitMask
== 0x001F)) {
777 convert_type
= CONVERT_555
;
778 current_format
= GL_RGBA
;
779 internal_format
= GL_RGBA
;
780 current_pixel_format
= GL_UNSIGNED_SHORT_5_5_5_1
;
785 } else if (src_pf
->u1
.dwRGBBitCount
== 24) {
786 if ((src_pf
->dwFlags
& DDPF_ALPHAPIXELS
) &&
787 (src_pf
->u5
.dwRGBAlphaBitMask
!= 0x000000)) {
790 if ((src_pf
->u2
.dwRBitMask
== 0xFF0000) &&
791 (src_pf
->u3
.dwGBitMask
== 0x00FF00) &&
792 (src_pf
->u4
.dwBBitMask
== 0x0000FF)) {
793 if (colorkey_active
) {
794 convert_type
= CONVERT_CK_RGB24
;
795 current_format
= GL_RGBA
;
796 internal_format
= GL_RGBA
;
797 current_pixel_format
= GL_UNSIGNED_INT_8_8_8_8
;
799 convert_type
= NO_CONVERSION
;
800 current_format
= GL_BGR
;
801 internal_format
= GL_RGB
;
802 current_pixel_format
= GL_UNSIGNED_BYTE
;
808 } else if (src_pf
->u1
.dwRGBBitCount
== 32) {
809 if ((src_pf
->dwFlags
& DDPF_ALPHAPIXELS
) &&
810 (src_pf
->u5
.dwRGBAlphaBitMask
!= 0x00000000)) {
811 if ((src_pf
->u2
.dwRBitMask
== 0xFF000000) &&
812 (src_pf
->u3
.dwGBitMask
== 0x00FF0000) &&
813 (src_pf
->u4
.dwBBitMask
== 0x0000FF00) &&
814 (src_pf
->u5
.dwRGBAlphaBitMask
== 0x000000FF)) {
815 if (colorkey_active
) {
816 convert_type
= CONVERT_CK_8888
;
818 convert_type
= NO_CONVERSION
;
820 current_format
= GL_RGBA
;
821 internal_format
= GL_RGBA
;
822 current_pixel_format
= GL_UNSIGNED_INT_8_8_8_8
;
823 } else if ((src_pf
->u2
.dwRBitMask
== 0x00FF0000) &&
824 (src_pf
->u3
.dwGBitMask
== 0x0000FF00) &&
825 (src_pf
->u4
.dwBBitMask
== 0x000000FF) &&
826 (src_pf
->u5
.dwRGBAlphaBitMask
== 0xFF000000)) {
827 if (colorkey_active
) {
828 convert_type
= CONVERT_CK_8888_ARGB
;
829 current_format
= GL_RGBA
;
830 internal_format
= GL_RGBA
;
831 current_pixel_format
= GL_UNSIGNED_INT_8_8_8_8
;
833 convert_type
= NO_CONVERSION
;
834 current_format
= GL_BGRA
;
835 internal_format
= GL_RGBA
;
836 current_pixel_format
= GL_UNSIGNED_INT_8_8_8_8_REV
;
842 if ((src_pf
->u2
.dwRBitMask
== 0x00FF0000) &&
843 (src_pf
->u3
.dwGBitMask
== 0x0000FF00) &&
844 (src_pf
->u4
.dwBBitMask
== 0x000000FF)) {
845 if (need_alpha_ck
== TRUE
) {
846 convert_type
= CONVERT_RGB32_888
;
847 current_format
= GL_RGBA
;
848 internal_format
= GL_RGBA
;
849 current_pixel_format
= GL_UNSIGNED_INT_8_8_8_8
;
851 convert_type
= NO_CONVERSION
;
852 current_format
= GL_BGRA
;
853 internal_format
= GL_RGBA
;
854 current_pixel_format
= GL_UNSIGNED_INT_8_8_8_8_REV
;
868 ERR("Unsupported pixel format for textures : \n");
870 DDRAW_dump_pixelformat(src_pf
);
872 return DDERR_INVALIDPIXELFORMAT
;
874 if ((need_to_alloc
) ||
875 (internal_format
!= *current_internal_format
)) {
876 glTexImage2D(GL_TEXTURE_2D
, level
, internal_format
,
877 tex_width
, tex_height
, 0,
878 current_format
, current_pixel_format
, NULL
);
879 *current_internal_format
= internal_format
;
883 if ((sub_texture
== TRUE
) && (convert_type
== NO_CONVERSION
)) {
884 current_storage_width
= surf_ptr
->surface_desc
.u1
.lPitch
/ bpp
;
886 if (surf_ptr
->surface_desc
.u1
.lPitch
== (surf_ptr
->surface_desc
.dwWidth
* bpp
)) {
887 current_storage_width
= 0;
889 current_storage_width
= surf_ptr
->surface_desc
.u1
.lPitch
/ bpp
;
892 glPixelStorei(GL_UNPACK_ROW_LENGTH
, current_storage_width
);
897 HRESULT
upload_surface_to_tex_memory(RECT
*rect
, DWORD xoffset
, DWORD yoffset
, void **temp_buffer
)
899 const DDSURFACEDESC
* const src_d
= (DDSURFACEDESC
*)&(current_surface
->surface_desc
);
900 void *surf_buffer
= NULL
;
903 BYTE bpp
= GET_BPP(current_surface
->surface_desc
);
909 lrect
.bottom
= current_tex_height
;
910 lrect
.right
= current_tex_width
;
914 width
= rect
->right
- rect
->left
;
915 height
= rect
->bottom
- rect
->top
;
917 /* Used when converting stuff */
918 line_increase
= src_d
->u1
.lPitch
- (width
* bpp
);
920 switch (convert_type
) {
921 case CONVERT_PALETTED
: {
922 IDirectDrawPaletteImpl
* pal
= current_surface
->palette
;
926 BYTE
*src
= (BYTE
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
929 /* Upload a black texture. The real one will be uploaded on palette change */
930 WARN("Palettized texture Loading with a NULL palette !\n");
931 memset(table
, 0, 256 * 4);
933 /* Get the surface's palette */
934 for (i
= 0; i
< 256; i
++) {
935 table
[i
][0] = pal
->palents
[i
].peRed
;
936 table
[i
][1] = pal
->palents
[i
].peGreen
;
937 table
[i
][2] = pal
->palents
[i
].peBlue
;
938 if ((src_d
->dwFlags
& DDSD_CKSRCBLT
) &&
939 (i
>= src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) &&
940 (i
<= src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
941 /* We should maybe here put a more 'neutral' color than the standard bright purple
942 one often used by application to prevent the nice purple borders when bi-linear
950 if (*temp_buffer
== NULL
)
951 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
952 current_tex_width
* current_tex_height
* sizeof(DWORD
));
953 dst
= (BYTE
*) *temp_buffer
;
955 for (y
= 0; y
< height
; y
++) {
956 for (x
= 0; x
< width
; x
++) {
958 *dst
++ = table
[color
][0];
959 *dst
++ = table
[color
][1];
960 *dst
++ = table
[color
][2];
961 *dst
++ = table
[color
][3];
963 src
+= line_increase
;
967 case CONVERT_CK_565
: {
968 /* Converting the 565 format in 5551 packed to emulate color-keying.
970 Note : in all these conversion, it would be best to average the averaging
971 pixels to get the color of the pixel that will be color-keyed to
972 prevent 'color bleeding'. This will be done later on if ever it is
975 Note2: when using color-keying + alpha, are the alpha bits part of the
979 WORD
*src
= (WORD
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
981 if (*temp_buffer
== NULL
)
982 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
983 current_tex_width
* current_tex_height
* sizeof(WORD
));
984 dst
= (WORD
*) *temp_buffer
;
986 for (y
= 0; y
< height
; y
++) {
987 for (x
= 0; x
< width
; x
++) {
989 *dst
= ((color
& 0xFFD0) | ((color
& 0x1F) << 1));
990 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
991 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
995 src
= (WORD
*) (((BYTE
*) src
) + line_increase
);
999 case CONVERT_CK_5551
: {
1000 /* Change the alpha value of the color-keyed pixels to emulate color-keying. */
1002 WORD
*src
= (WORD
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
1004 if (*temp_buffer
== NULL
)
1005 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1006 current_tex_width
* current_tex_height
* sizeof(WORD
));
1007 dst
= (WORD
*) *temp_buffer
;
1009 for (y
= 0; y
< height
; y
++) {
1010 for (x
= 0; x
< width
; x
++) {
1011 WORD color
= *src
++;
1012 *dst
= color
& 0xFFFE;
1013 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
1014 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
1015 *dst
|= color
& 0x0001;
1018 src
= (WORD
*) (((BYTE
*) src
) + line_increase
);
1022 case CONVERT_CK_4444
: {
1023 /* Change the alpha value of the color-keyed pixels to emulate color-keying. */
1025 WORD
*src
= (WORD
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
1027 if (*temp_buffer
== NULL
)
1028 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1029 current_tex_width
* current_tex_height
* sizeof(WORD
));
1030 dst
= (WORD
*) *temp_buffer
;
1032 for (y
= 0; y
< height
; y
++) {
1033 for (x
= 0; x
< width
; x
++) {
1034 WORD color
= *src
++;
1035 *dst
= color
& 0xFFF0;
1036 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
1037 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
1038 *dst
|= color
& 0x000F;
1041 src
= (WORD
*) (((BYTE
*) src
) + line_increase
);
1045 case CONVERT_CK_4444_ARGB
: {
1046 /* Move the four Alpha bits... */
1048 WORD
*src
= (WORD
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
1050 if (*temp_buffer
== NULL
)
1051 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1052 current_tex_width
* current_tex_height
* sizeof(WORD
));
1053 dst
= (WORD
*) *temp_buffer
;
1055 for (y
= 0; y
< height
; y
++) {
1056 for (x
= 0; x
< width
; x
++) {
1057 WORD color
= *src
++;
1058 *dst
= (color
& 0x0FFF) << 4;
1059 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
1060 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
1061 *dst
|= (color
& 0xF000) >> 12;
1064 src
= (WORD
*) (((BYTE
*) src
) + line_increase
);
1068 case CONVERT_CK_1555
: {
1070 WORD
*src
= (WORD
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
1072 if (*temp_buffer
== NULL
)
1073 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1074 current_tex_width
* current_tex_height
* sizeof(WORD
));
1075 dst
= (WORD
*) *temp_buffer
;
1077 for (y
= 0; y
< height
; y
++) {
1078 for (x
= 0; x
< width
; x
++) {
1079 WORD color
= *src
++;
1080 *dst
= (color
& 0x7FFF) << 1;
1081 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
1082 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
1083 *dst
|= (color
& 0x8000) >> 15;
1086 src
= (WORD
*) (((BYTE
*) src
) + line_increase
);
1091 /* Converting the 0555 format in 5551 packed */
1093 WORD
*src
= (WORD
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
1095 if (*temp_buffer
== NULL
)
1096 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1097 current_tex_width
* current_tex_height
* sizeof(WORD
));
1098 dst
= (WORD
*) *temp_buffer
;
1100 if (src_d
->dwFlags
& DDSD_CKSRCBLT
) {
1101 for (y
= 0; y
< height
; y
++) {
1102 for (x
= 0; x
< width
; x
++) {
1103 WORD color
= *src
++;
1104 *dst
= (color
& 0x7FFF) << 1;
1105 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
1106 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
1110 src
= (WORD
*) (((BYTE
*) src
) + line_increase
);
1113 for (y
= 0; y
< height
; y
++) {
1114 for (x
= 0; x
< width
; x
++) {
1115 WORD color
= *src
++;
1116 *dst
++ = ((color
& 0x7FFF) << 1) | 0x0001;
1118 src
= (WORD
*) (((BYTE
*) src
) + line_increase
);
1124 case CONVERT_CK_RGB24
: {
1125 /* This is a pain :-) */
1127 BYTE
*src
= (BYTE
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
));
1130 if (*temp_buffer
== NULL
)
1131 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1132 current_tex_width
* current_tex_height
* sizeof(DWORD
));
1133 dst
= (DWORD
*) *temp_buffer
;
1135 for (y
= 0; y
< height
; y
++) {
1136 for (x
= 0; x
< width
; x
++) {
1137 DWORD color
= *((DWORD
*) src
) & 0x00FFFFFF;
1140 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
1141 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
1145 src
+= line_increase
;
1149 case CONVERT_CK_8888
: {
1150 /* Just use the alpha component to handle color-keying... */
1152 DWORD
*src
= (DWORD
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
1154 if (*temp_buffer
== NULL
)
1155 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1156 current_tex_width
* current_tex_height
* sizeof(DWORD
));
1157 dst
= (DWORD
*) *temp_buffer
;
1159 for (y
= 0; y
< height
; y
++) {
1160 for (x
= 0; x
< width
; x
++) {
1161 DWORD color
= *src
++;
1162 *dst
= color
& 0xFFFFFF00;
1163 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
1164 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
1165 *dst
|= color
& 0x000000FF;
1168 src
= (DWORD
*) (((BYTE
*) src
) + line_increase
);
1172 case CONVERT_CK_8888_ARGB
: {
1174 DWORD
*src
= (DWORD
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
1176 if (*temp_buffer
== NULL
)
1177 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1178 current_tex_width
* current_tex_height
* sizeof(DWORD
));
1179 dst
= (DWORD
*) *temp_buffer
;
1181 for (y
= 0; y
< height
; y
++) {
1182 for (x
= 0; x
< width
; x
++) {
1183 DWORD color
= *src
++;
1184 *dst
= (color
& 0x00FFFFFF) << 8;
1185 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
1186 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
1187 *dst
|= (color
& 0xFF000000) >> 24;
1190 src
= (DWORD
*) (((BYTE
*) src
) + line_increase
);
1194 case CONVERT_RGB32_888
: {
1195 /* Just add an alpha component and handle color-keying... */
1197 DWORD
*src
= (DWORD
*) (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
)), *dst
;
1199 if (*temp_buffer
== NULL
)
1200 *temp_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1201 current_tex_width
* current_tex_height
* sizeof(DWORD
));
1202 dst
= (DWORD
*) *temp_buffer
;
1204 if (src_d
->dwFlags
& DDSD_CKSRCBLT
) {
1205 for (y
= 0; y
< height
; y
++) {
1206 for (x
= 0; x
< width
; x
++) {
1207 DWORD color
= *src
++;
1209 if ((color
< src_d
->ddckCKSrcBlt
.dwColorSpaceLowValue
) ||
1210 (color
> src_d
->ddckCKSrcBlt
.dwColorSpaceHighValue
))
1214 src
= (DWORD
*) (((BYTE
*) src
) + line_increase
);
1217 for (y
= 0; y
< height
; y
++) {
1218 for (x
= 0; x
< width
; x
++) {
1219 *dst
++ = (*src
++ << 8) | 0xFF;
1221 src
= (DWORD
*) (((BYTE
*) src
) + line_increase
);
1227 /* Nothing to do here as the name suggests... Just set-up the buffer correctly */
1228 surf_buffer
= (((BYTE
*) src_d
->lpSurface
) + (bpp
* rect
->left
) + (src_d
->u1
.lPitch
* rect
->top
));
1232 if (convert_type
!= NO_CONVERSION
) {
1233 /* When doing conversion, the storage is always of width 'width' as there will never
1234 be any Pitch issue... For now :-)
1236 surf_buffer
= *temp_buffer
;
1237 if (width
!= current_storage_width
) {
1238 glPixelStorei(GL_UNPACK_ROW_LENGTH
, width
);
1239 current_storage_width
= width
;
1243 glTexSubImage2D(GL_TEXTURE_2D
,
1248 current_pixel_format
,
1254 HRESULT
upload_surface_to_tex_memory_release(void)
1256 current_surface
= NULL
;