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
27 #include "wine/debug.h"
29 #include "mesa_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
33 GLenum
convert_D3D_compare_to_GL(D3DCMPFUNC dwRenderState
)
35 switch (dwRenderState
) {
36 case D3DCMP_NEVER
: return GL_NEVER
;
37 case D3DCMP_LESS
: return GL_LESS
;
38 case D3DCMP_EQUAL
: return GL_EQUAL
;
39 case D3DCMP_LESSEQUAL
: return GL_LEQUAL
;
40 case D3DCMP_GREATER
: return GL_GREATER
;
41 case D3DCMP_NOTEQUAL
: return GL_NOTEQUAL
;
42 case D3DCMP_GREATEREQUAL
: return GL_GEQUAL
;
43 case D3DCMP_ALWAYS
: return GL_ALWAYS
;
44 default: ERR("Unexpected compare type %d !\n", dwRenderState
);
49 GLenum
convert_D3D_stencilop_to_GL(D3DSTENCILOP dwRenderState
)
51 switch (dwRenderState
) {
52 case D3DSTENCILOP_KEEP
: return GL_KEEP
;
53 case D3DSTENCILOP_ZERO
: return GL_ZERO
;
54 case D3DSTENCILOP_REPLACE
: return GL_REPLACE
;
55 case D3DSTENCILOP_INCRSAT
: return GL_INCR
;
56 case D3DSTENCILOP_DECRSAT
: return GL_DECR
;
57 case D3DSTENCILOP_INVERT
: return GL_INVERT
;
58 case D3DSTENCILOP_INCR
: WARN("D3DSTENCILOP_INCR not properly handled !\n"); return GL_INCR
;
59 case D3DSTENCILOP_DECR
: WARN("D3DSTENCILOP_DECR not properly handled !\n"); return GL_DECR
;
60 default: ERR("Unexpected compare type %d !\n", dwRenderState
);
65 GLenum
convert_D3D_blendop_to_GL(D3DBLEND dwRenderState
)
67 switch ((D3DBLEND
) dwRenderState
) {
68 case D3DBLEND_ZERO
: return GL_ZERO
;
69 case D3DBLEND_ONE
: return GL_ONE
;
70 case D3DBLEND_SRCALPHA
: return GL_SRC_ALPHA
;
71 case D3DBLEND_INVSRCALPHA
: return GL_ONE_MINUS_SRC_ALPHA
;
72 case D3DBLEND_DESTALPHA
: return GL_DST_ALPHA
;
73 case D3DBLEND_INVDESTALPHA
: return GL_ONE_MINUS_DST_ALPHA
;
74 case D3DBLEND_DESTCOLOR
: return GL_DST_COLOR
;
75 case D3DBLEND_INVDESTCOLOR
: return GL_ONE_MINUS_DST_COLOR
;
76 case D3DBLEND_SRCALPHASAT
: return GL_SRC_ALPHA_SATURATE
;
77 case D3DBLEND_SRCCOLOR
: return GL_SRC_COLOR
;
78 case D3DBLEND_INVSRCCOLOR
: return GL_ONE_MINUS_SRC_COLOR
;
79 default: ERR("Unhandled blend mode %d !\n", dwRenderState
); return GL_ZERO
;
83 void set_render_state(IDirect3DDeviceImpl
* This
,
84 D3DRENDERSTATETYPE dwRenderStateType
, STATEBLOCK
*lpStateBlock
)
86 DWORD dwRenderState
= lpStateBlock
->render_state
[dwRenderStateType
- 1];
87 IDirect3DDeviceGLImpl
*glThis
= (IDirect3DDeviceGLImpl
*) This
;
90 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType
), dwRenderState
);
92 /* First, all the stipple patterns */
93 if ((dwRenderStateType
>= D3DRENDERSTATE_STIPPLEPATTERN00
) &&
94 (dwRenderStateType
<= D3DRENDERSTATE_STIPPLEPATTERN31
)) {
95 ERR("Unhandled dwRenderStateType stipple %d!\n",dwRenderStateType
);
99 /* All others state variables */
100 switch (dwRenderStateType
) {
101 case D3DRENDERSTATE_TEXTUREHANDLE
: { /* 1 */
102 IDirectDrawSurfaceImpl
*tex
= (IDirectDrawSurfaceImpl
*) dwRenderState
;
104 IDirect3DDevice7_SetTexture(ICOM_INTERFACE(This
, IDirect3DDevice7
),
106 ICOM_INTERFACE(tex
, IDirectDrawSurface7
));
109 case D3DRENDERSTATE_TEXTUREADDRESSU
: /* 44 */
110 case D3DRENDERSTATE_TEXTUREADDRESSV
: /* 45 */
111 case D3DRENDERSTATE_TEXTUREADDRESS
: { /* 3 */
112 D3DTEXTURESTAGESTATETYPE d3dTexStageStateType
;
114 if (dwRenderStateType
== D3DRENDERSTATE_TEXTUREADDRESS
) d3dTexStageStateType
= D3DTSS_ADDRESS
;
115 else if (dwRenderStateType
== D3DRENDERSTATE_TEXTUREADDRESSU
) d3dTexStageStateType
= D3DTSS_ADDRESSU
;
116 else d3dTexStageStateType
= D3DTSS_ADDRESSV
;
118 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This
, IDirect3DDevice7
),
119 0, d3dTexStageStateType
,
123 case D3DRENDERSTATE_TEXTUREPERSPECTIVE
: /* 4 */
125 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_NICEST
);
127 glHint(GL_PERSPECTIVE_CORRECTION_HINT
, GL_FASTEST
);
130 case D3DRENDERSTATE_WRAPU
: /* 5 */
131 case D3DRENDERSTATE_WRAPV
: /* 6 */
132 case D3DRENDERSTATE_WRAP0
: /* 128 */
133 case D3DRENDERSTATE_WRAP1
: /* 129 */
134 case D3DRENDERSTATE_WRAP2
: /* 130 */
135 case D3DRENDERSTATE_WRAP3
: /* 131 */
136 case D3DRENDERSTATE_WRAP4
: /* 132 */
137 case D3DRENDERSTATE_WRAP5
: /* 133 */
138 case D3DRENDERSTATE_WRAP6
: /* 134 */
139 case D3DRENDERSTATE_WRAP7
: /* 135 */
141 ERR("Texture WRAP modes unsupported by OpenGL.. Expect graphical glitches !\n");
144 case D3DRENDERSTATE_ZENABLE
: /* 7 */
145 /* To investigate : in OpenGL, if we disable the depth test, the Z buffer will NOT be
146 updated either.. No idea about what happens in D3D.
148 Maybe replacing the Z function by ALWAYS would be a better idea. */
149 if (dwRenderState
== D3DZB_TRUE
)
150 glEnable(GL_DEPTH_TEST
);
151 else if (dwRenderState
== D3DZB_FALSE
)
152 glDisable(GL_DEPTH_TEST
);
154 glEnable(GL_DEPTH_TEST
);
155 WARN(" w-buffering not supported.\n");
159 case D3DRENDERSTATE_FILLMODE
: /* 8 */
160 switch ((D3DFILLMODE
) dwRenderState
) {
162 glPolygonMode(GL_FRONT_AND_BACK
,GL_POINT
);
164 case D3DFILL_WIREFRAME
:
165 glPolygonMode(GL_FRONT_AND_BACK
,GL_LINE
);
168 glPolygonMode(GL_FRONT_AND_BACK
,GL_FILL
);
171 ERR("Unhandled fill mode %ld !\n",dwRenderState
);
175 case D3DRENDERSTATE_SHADEMODE
: /* 9 */
176 switch ((D3DSHADEMODE
) dwRenderState
) {
178 glShadeModel(GL_FLAT
);
180 case D3DSHADE_GOURAUD
:
181 glShadeModel(GL_SMOOTH
);
184 ERR("Unhandled shade mode %ld !\n",dwRenderState
);
188 case D3DRENDERSTATE_ZWRITEENABLE
: /* 14 */
190 glDepthMask(GL_TRUE
);
192 glDepthMask(GL_FALSE
);
195 case D3DRENDERSTATE_ALPHATESTENABLE
: /* 15 */
197 glEnable(GL_ALPHA_TEST
);
199 glDisable(GL_ALPHA_TEST
);
202 case D3DRENDERSTATE_TEXTUREMAG
: { /* 17 */
203 DWORD tex_mag
= 0xFFFFFFFF;
205 switch ((D3DTEXTUREFILTER
) dwRenderState
) {
206 case D3DFILTER_NEAREST
:
207 tex_mag
= D3DTFG_POINT
;
209 case D3DFILTER_LINEAR
:
210 tex_mag
= D3DTFG_LINEAR
;
213 ERR("Unhandled texture mag %ld !\n",dwRenderState
);
216 if (tex_mag
!= 0xFFFFFFFF) {
217 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This
, IDirect3DDevice7
), 0, D3DTSS_MAGFILTER
, tex_mag
);
221 case D3DRENDERSTATE_TEXTUREMIN
: { /* 18 */
222 DWORD tex_min
= 0xFFFFFFFF;
224 switch ((D3DTEXTUREFILTER
) dwRenderState
) {
225 case D3DFILTER_NEAREST
:
226 tex_min
= D3DTFN_POINT
;
228 case D3DFILTER_LINEAR
:
229 tex_min
= D3DTFN_LINEAR
;
232 ERR("Unhandled texture min %ld !\n",dwRenderState
);
235 if (tex_min
!= 0xFFFFFFFF) {
236 IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This
, IDirect3DDevice7
), 0, D3DTSS_MINFILTER
, tex_min
);
240 case D3DRENDERSTATE_SRCBLEND
: /* 19 */
241 case D3DRENDERSTATE_DESTBLEND
: /* 20 */
242 glBlendFunc(convert_D3D_blendop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_SRCBLEND
- 1]),
243 convert_D3D_blendop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_DESTBLEND
- 1]));
246 case D3DRENDERSTATE_TEXTUREMAPBLEND
: /* 21 */
247 switch ((D3DTEXTUREBLEND
) dwRenderState
) {
248 case D3DTBLEND_DECAL
:
249 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_DECAL
);
251 case D3DTBLEND_MODULATE
:
252 case D3DTBLEND_MODULATEALPHA
:
253 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_MODULATE
);
256 ERR("Unhandled texture environment %ld !\n",dwRenderState
);
260 case D3DRENDERSTATE_CULLMODE
: /* 22 */
261 switch ((D3DCULL
) dwRenderState
) {
263 glDisable(GL_CULL_FACE
);
266 glEnable(GL_CULL_FACE
);
271 glEnable(GL_CULL_FACE
);
276 ERR("Unhandled cull mode %ld !\n",dwRenderState
);
280 case D3DRENDERSTATE_ZFUNC
: /* 23 */
281 glDepthFunc(convert_D3D_compare_to_GL(dwRenderState
));
284 case D3DRENDERSTATE_ALPHAREF
: /* 24 */
285 case D3DRENDERSTATE_ALPHAFUNC
: /* 25 */
286 glAlphaFunc(convert_D3D_compare_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_ALPHAFUNC
- 1]),
287 (lpStateBlock
->render_state
[D3DRENDERSTATE_ALPHAREF
- 1] & 0x000000FF) / 255.0);
290 case D3DRENDERSTATE_DITHERENABLE
: /* 26 */
294 glDisable(GL_DITHER
);
297 case D3DRENDERSTATE_ALPHABLENDENABLE
: /* 27 */
305 case D3DRENDERSTATE_FOGENABLE
: /* 28 */
306 /* Nothing to do here. Only the storage matters :-) */
309 case D3DRENDERSTATE_SPECULARENABLE
: /* 29 */
311 ERR(" Specular Lighting not supported yet.\n");
314 case D3DRENDERSTATE_SUBPIXEL
: /* 31 */
315 case D3DRENDERSTATE_SUBPIXELX
: /* 32 */
316 /* We do not support this anyway, so why protest :-) */
319 case D3DRENDERSTATE_STIPPLEDALPHA
: /* 33 */
321 ERR(" Stippled Alpha not supported yet.\n");
324 case D3DRENDERSTATE_FOGCOLOR
: { /* 34 */
326 color
[0] = ((dwRenderState
>> 16) & 0xFF)/255.0f
;
327 color
[1] = ((dwRenderState
>> 8) & 0xFF)/255.0f
;
328 color
[2] = ((dwRenderState
>> 0) & 0xFF)/255.0f
;
329 color
[3] = ((dwRenderState
>> 24) & 0xFF)/255.0f
;
330 glFogfv(GL_FOG_COLOR
,color
);
331 /* Note: glFogiv does not seem to work */
334 case D3DRENDERSTATE_FOGTABLEMODE
: /* 35 */
335 case D3DRENDERSTATE_FOGVERTEXMODE
: /* 140 */
336 case D3DRENDERSTATE_FOGSTART
: /* 36 */
337 case D3DRENDERSTATE_FOGEND
: /* 37 */
338 /* Nothing to do here. Only the storage matters :-) */
341 case D3DRENDERSTATE_FOGDENSITY
: /* 38 */
342 glFogi(GL_FOG_DENSITY
,*(float*)&dwRenderState
);
345 case D3DRENDERSTATE_COLORKEYENABLE
: /* 41 */
346 /* This needs to be fixed. */
353 case D3DRENDERSTATE_ZBIAS
: /* 47 */
354 /* This is a tad bit hacky.. But well, no idea how to do it better in OpenGL :-/ */
355 if (dwRenderState
== 0) {
356 glDisable(GL_POLYGON_OFFSET_FILL
);
357 glDisable(GL_POLYGON_OFFSET_LINE
);
358 glDisable(GL_POLYGON_OFFSET_POINT
);
360 glEnable(GL_POLYGON_OFFSET_FILL
);
361 glEnable(GL_POLYGON_OFFSET_LINE
);
362 glEnable(GL_POLYGON_OFFSET_POINT
);
363 glPolygonOffset(1.0, dwRenderState
* 1.0);
367 case D3DRENDERSTATE_FLUSHBATCH
: /* 50 */
370 case D3DRENDERSTATE_STENCILENABLE
: /* 52 */
372 glEnable(GL_STENCIL_TEST
);
374 glDisable(GL_STENCIL_TEST
);
377 case D3DRENDERSTATE_STENCILFAIL
: /* 53 */
378 case D3DRENDERSTATE_STENCILZFAIL
: /* 54 */
379 case D3DRENDERSTATE_STENCILPASS
: /* 55 */
380 glStencilOp(convert_D3D_stencilop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILFAIL
- 1]),
381 convert_D3D_stencilop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILZFAIL
- 1]),
382 convert_D3D_stencilop_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILPASS
- 1]));
385 case D3DRENDERSTATE_STENCILFUNC
: /* 56 */
386 case D3DRENDERSTATE_STENCILREF
: /* 57 */
387 case D3DRENDERSTATE_STENCILMASK
: /* 58 */
388 glStencilFunc(convert_D3D_compare_to_GL(lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILFUNC
- 1]),
389 lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILREF
- 1],
390 lpStateBlock
->render_state
[D3DRENDERSTATE_STENCILMASK
- 1]);
393 case D3DRENDERSTATE_STENCILWRITEMASK
: /* 59 */
394 glStencilMask(dwRenderState
);
397 case D3DRENDERSTATE_TEXTUREFACTOR
: /* 60 */
398 /* Only the storage matters... */
401 case D3DRENDERSTATE_CLIPPING
: /* 136 */
402 case D3DRENDERSTATE_CLIPPLANEENABLE
: { /* 152 */
406 if (dwRenderStateType
== D3DRENDERSTATE_CLIPPING
) {
407 mask
= ((dwRenderState
) ?
408 (This
->state_block
.render_state
[D3DRENDERSTATE_CLIPPLANEENABLE
- 1]) : (0x00000000));
410 mask
= dwRenderState
;
412 for (i
= 0, runner
= 0x00000001; i
< This
->max_clipping_planes
; i
++, runner
= (runner
<< 1)) {
415 glGetIntegerv(GL_CLIP_PLANE0
+ i
, &enabled
);
416 if (enabled
== GL_FALSE
) {
417 glEnable(GL_CLIP_PLANE0
+ i
);
418 /* Need to force a transform change so that this clipping plane parameters are sent
421 glThis
->transform_state
= GL_TRANSFORM_NONE
;
424 glDisable(GL_CLIP_PLANE0
+ i
);
430 case D3DRENDERSTATE_LIGHTING
: /* 137 */
432 glEnable(GL_LIGHTING
);
434 glDisable(GL_LIGHTING
);
437 case D3DRENDERSTATE_AMBIENT
: { /* 139 */
440 light
[0] = ((dwRenderState
>> 16) & 0xFF) / 255.0;
441 light
[1] = ((dwRenderState
>> 8) & 0xFF) / 255.0;
442 light
[2] = ((dwRenderState
>> 0) & 0xFF) / 255.0;
443 light
[3] = ((dwRenderState
>> 24) & 0xFF) / 255.0;
444 glLightModelfv(GL_LIGHT_MODEL_AMBIENT
, (float *) light
);
447 case D3DRENDERSTATE_COLORVERTEX
: /* 141 */
448 /* Nothing to do here.. Only storage matters */
451 case D3DRENDERSTATE_LOCALVIEWER
: /* 142 */
453 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
455 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_FALSE
);
458 case D3DRENDERSTATE_NORMALIZENORMALS
: /* 143 */
460 glEnable(GL_NORMALIZE
);
461 glEnable(GL_RESCALE_NORMAL
);
463 glDisable(GL_NORMALIZE
);
464 glDisable(GL_RESCALE_NORMAL
);
468 case D3DRENDERSTATE_DIFFUSEMATERIALSOURCE
: /* 145 */
469 case D3DRENDERSTATE_SPECULARMATERIALSOURCE
: /* 146 */
470 case D3DRENDERSTATE_AMBIENTMATERIALSOURCE
: /* 147 */
471 case D3DRENDERSTATE_EMISSIVEMATERIALSOURCE
: /* 148 */
472 /* Nothing to do here. Only the storage matters :-) */
476 ERR("Unhandled dwRenderStateType %s (%08x) value : %08lx !\n",
477 _get_renderstate(dwRenderStateType
), dwRenderStateType
, dwRenderState
);
483 void store_render_state(IDirect3DDeviceImpl
*This
,
484 D3DRENDERSTATETYPE dwRenderStateType
, DWORD dwRenderState
, STATEBLOCK
*lpStateBlock
)
486 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType
), dwRenderState
);
488 /* Some special cases first.. */
489 if (dwRenderStateType
== D3DRENDERSTATE_SRCBLEND
) {
490 if (dwRenderState
== D3DBLEND_BOTHSRCALPHA
) {
491 lpStateBlock
->render_state
[D3DRENDERSTATE_SRCBLEND
- 1] = D3DBLEND_SRCALPHA
;
492 lpStateBlock
->render_state
[D3DRENDERSTATE_DESTBLEND
- 1] = D3DBLEND_SRCALPHA
;
494 } else if (dwRenderState
== D3DBLEND_BOTHINVSRCALPHA
) {
495 lpStateBlock
->render_state
[D3DRENDERSTATE_SRCBLEND
- 1] = D3DBLEND_INVSRCALPHA
;
496 lpStateBlock
->render_state
[D3DRENDERSTATE_DESTBLEND
- 1] = D3DBLEND_INVSRCALPHA
;
499 } else if (dwRenderStateType
== D3DRENDERSTATE_TEXTUREADDRESS
) {
500 lpStateBlock
->render_state
[D3DRENDERSTATE_TEXTUREADDRESSU
- 1] = dwRenderState
;
501 lpStateBlock
->render_state
[D3DRENDERSTATE_TEXTUREADDRESSV
- 1] = dwRenderState
;
502 } else if (dwRenderStateType
== D3DRENDERSTATE_WRAPU
) {
504 lpStateBlock
->render_state
[D3DRENDERSTATE_WRAP0
] |= D3DWRAP_U
;
506 lpStateBlock
->render_state
[D3DRENDERSTATE_WRAP0
] &= ~D3DWRAP_U
;
507 } else if (dwRenderStateType
== D3DRENDERSTATE_WRAPV
) {
509 lpStateBlock
->render_state
[D3DRENDERSTATE_WRAP0
] |= D3DWRAP_V
;
511 lpStateBlock
->render_state
[D3DRENDERSTATE_WRAP0
] &= ~D3DWRAP_V
;
515 lpStateBlock
->render_state
[dwRenderStateType
- 1] = dwRenderState
;
518 void get_render_state(IDirect3DDeviceImpl
*This
,
519 D3DRENDERSTATETYPE dwRenderStateType
, LPDWORD lpdwRenderState
, STATEBLOCK
*lpStateBlock
)
521 *lpdwRenderState
= lpStateBlock
->render_state
[dwRenderStateType
- 1];
523 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType
), *lpdwRenderState
);
526 void apply_render_state(IDirect3DDeviceImpl
*This
, STATEBLOCK
*lpStateBlock
)
529 TRACE("(%p,%p)\n", This
, lpStateBlock
);
530 for(i
= 0; i
< HIGHEST_RENDER_STATE
; i
++)
531 if (lpStateBlock
->set_flags
.render_state
[i
])
532 set_render_state(This
, i
+ 1, lpStateBlock
);