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);
50 void set_render_state(D3DRENDERSTATETYPE dwRenderStateType,
51 DWORD dwRenderState, RenderState *rs)
54 TRACE("%s = %08lx\n", _get_renderstate(dwRenderStateType), dwRenderState);
56 /* First, all the stipple patterns */
57 if ((dwRenderStateType >= D3DRENDERSTATE_STIPPLEPATTERN00) &&
58 (dwRenderStateType <= D3DRENDERSTATE_STIPPLEPATTERN31)) {
59 ERR("Unhandled dwRenderStateType stipple %d!\n",dwRenderStateType);
63 /* All others state variables */
64 switch (dwRenderStateType) {
65 case D3DRENDERSTATE_TEXTUREHANDLE: { /* 1 */
66 IDirectDrawSurfaceImpl *tex = (IDirectDrawSurfaceImpl*) dwRenderState;
69 glBindTexture(GL_TEXTURE_2D, 0);
70 glDisable(GL_TEXTURE_2D);
71 TRACE("disabling texturing\n");
73 IDirect3DTextureGLImpl *gl_tex = (IDirect3DTextureGLImpl *) tex->tex_private;
75 glEnable(GL_TEXTURE_2D);
76 /* Default parameters */
77 glBindTexture(GL_TEXTURE_2D, gl_tex->tex_name);
78 /* To prevent state change, we could test here what are the parameters
79 stored in the texture */
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, rs->mag);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, rs->min);
82 TRACE("setting OpenGL texture handle : %d\n", gl_tex->tex_name);
87 case D3DRENDERSTATE_TEXTUREADDRESSU: /* 44 */
88 case D3DRENDERSTATE_TEXTUREADDRESSV: /* 45 */
89 case D3DRENDERSTATE_TEXTUREADDRESS: { /* 3 */
90 GLenum arg = GL_REPEAT; /* Default value */
91 switch ((D3DTEXTUREADDRESS) dwRenderState) {
92 case D3DTADDRESS_WRAP: arg = GL_REPEAT; break;
93 case D3DTADDRESS_CLAMP: arg = GL_CLAMP; break;
94 case D3DTADDRESS_BORDER: arg = GL_CLAMP_TO_EDGE; break;
95 default: ERR("Unhandled TEXTUREADDRESS mode %ld !\n", dwRenderState);
97 if ((dwRenderStateType == D3DRENDERSTATE_TEXTUREADDRESSU) ||
98 (dwRenderStateType == D3DRENDERSTATE_TEXTUREADDRESS))
99 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, arg);
100 if ((dwRenderStateType == D3DRENDERSTATE_TEXTUREADDRESSV) ||
101 (dwRenderStateType == D3DRENDERSTATE_TEXTUREADDRESS))
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, arg);
105 case D3DRENDERSTATE_TEXTUREPERSPECTIVE: /* 4 */
107 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
109 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
112 case D3DRENDERSTATE_WRAPU: /* 5 */
114 ERR("WRAPU mode unsupported by OpenGL.. Expect graphical glitches !\n");
117 case D3DRENDERSTATE_WRAPV: /* 6 */
119 ERR("WRAPV mode unsupported by OpenGL.. Expect graphical glitches !\n");
122 case D3DRENDERSTATE_ZENABLE: /* 7 */
124 glEnable(GL_DEPTH_TEST);
126 glDisable(GL_DEPTH_TEST);
129 case D3DRENDERSTATE_FILLMODE: /* 8 */
130 switch ((D3DFILLMODE) dwRenderState) {
132 glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);
134 case D3DFILL_WIREFRAME:
135 glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
138 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
141 ERR("Unhandled fill mode %ld !\n",dwRenderState);
145 case D3DRENDERSTATE_SHADEMODE: /* 9 */
146 switch ((D3DSHADEMODE) dwRenderState) {
148 glShadeModel(GL_FLAT);
150 case D3DSHADE_GOURAUD:
151 glShadeModel(GL_SMOOTH);
154 ERR("Unhandled shade mode %ld !\n",dwRenderState);
158 case D3DRENDERSTATE_ZWRITEENABLE: /* 14 */
160 glDepthMask(GL_TRUE);
162 glDepthMask(GL_FALSE);
165 case D3DRENDERSTATE_ALPHATESTENABLE: /* 15 */
167 glEnable(GL_ALPHA_TEST);
169 glDisable(GL_ALPHA_TEST);
172 case D3DRENDERSTATE_TEXTUREMAG: /* 17 */
173 switch ((D3DTEXTUREFILTER) dwRenderState) {
174 case D3DFILTER_NEAREST:
175 rs->mag = GL_NEAREST;
177 case D3DFILTER_LINEAR:
181 ERR("Unhandled texture mag %ld !\n",dwRenderState);
185 case D3DRENDERSTATE_TEXTUREMIN: /* 18 */
186 switch ((D3DTEXTUREFILTER) dwRenderState) {
187 case D3DFILTER_NEAREST:
188 rs->min = GL_NEAREST;
190 case D3DFILTER_LINEAR:
194 ERR("Unhandled texture min %ld !\n",dwRenderState);
198 case D3DRENDERSTATE_SRCBLEND: /* 19 */
199 switch ((D3DBLEND) dwRenderState) {
203 case D3DBLEND_SRCALPHA:
204 rs->src = GL_SRC_ALPHA;
207 ERR("Unhandled blend mode %ld !\n",dwRenderState);
209 glBlendFunc(rs->src, rs->dst);
212 case D3DRENDERSTATE_DESTBLEND: /* 20 */
213 switch ((D3DBLEND) dwRenderState) {
217 case D3DBLEND_INVSRCALPHA:
218 rs->dst = GL_ONE_MINUS_SRC_ALPHA;
221 ERR("Unhandled blend mode %ld !\n",dwRenderState);
223 glBlendFunc(rs->src, rs->dst);
226 case D3DRENDERSTATE_TEXTUREMAPBLEND: /* 21 */
227 switch ((D3DTEXTUREBLEND) dwRenderState) {
228 case D3DTBLEND_MODULATE:
229 case D3DTBLEND_MODULATEALPHA:
230 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
233 ERR("Unhandled texture environment %ld !\n",dwRenderState);
237 case D3DRENDERSTATE_CULLMODE: /* 22 */
238 switch ((D3DCULL) dwRenderState) {
240 glDisable(GL_CULL_FACE);
243 glEnable(GL_CULL_FACE);
247 glEnable(GL_CULL_FACE);
251 ERR("Unhandled cull mode %ld !\n",dwRenderState);
255 case D3DRENDERSTATE_ZFUNC: /* 23 */
256 glDepthFunc(convert_D3D_compare_to_GL(dwRenderState));
259 case D3DRENDERSTATE_ALPHAREF: /* 24 */
260 rs->alpha_ref = dwRenderState / 255.0;
261 glAlphaFunc(rs->alpha_func, rs->alpha_ref);
264 case D3DRENDERSTATE_ALPHAFUNC: /* 25 */
265 rs->alpha_func = convert_D3D_compare_to_GL(dwRenderState);
266 glAlphaFunc(rs->alpha_func, rs->alpha_ref);
269 case D3DRENDERSTATE_DITHERENABLE: /* 26 */
273 glDisable(GL_DITHER);
276 case D3DRENDERSTATE_ALPHABLENDENABLE: /* 27 */
283 case D3DRENDERSTATE_FOGENABLE: /* 28 */
290 case D3DRENDERSTATE_SPECULARENABLE: /* 29 */
292 ERR(" Specular Lighting not supported yet.\n");
295 case D3DRENDERSTATE_SUBPIXEL: /* 31 */
296 case D3DRENDERSTATE_SUBPIXELX: /* 32 */
297 /* We do not support this anyway, so why protest :-) */
300 case D3DRENDERSTATE_FOGCOLOR: { /* 34 */
302 color[0] = (dwRenderState >> 16) & 0xFF;
303 color[1] = (dwRenderState >> 8) & 0xFF;
304 color[2] = (dwRenderState >> 0) & 0xFF;
305 color[3] = (dwRenderState >> 24) & 0xFF;
306 glFogiv(GL_FOG_COLOR, color);
310 case D3DRENDERSTATE_COLORKEYENABLE: /* 41 */
317 case D3DRENDERSTATE_ZBIAS: /* 47 */
318 /* This is a tad bit hacky.. But well, no idea how to do it better in OpenGL :-/ */
319 if (dwRenderState == 0) {
320 glDisable(GL_POLYGON_OFFSET_FILL);
321 glDisable(GL_POLYGON_OFFSET_LINE);
322 glDisable(GL_POLYGON_OFFSET_POINT);
324 glEnable(GL_POLYGON_OFFSET_FILL);
325 glEnable(GL_POLYGON_OFFSET_LINE);
326 glEnable(GL_POLYGON_OFFSET_POINT);
327 glPolygonOffset(1.0, dwRenderState * 1.0);
331 case D3DRENDERSTATE_FLUSHBATCH: /* 50 */
335 ERR("Unhandled dwRenderStateType %s !\n", _get_renderstate(dwRenderStateType));