New debug scheme with explicit debug channels declaration.
[wine] / graphics / d3dcommon.c
1 /* Direct3D Common functions
2    (c) 1998 Lionel ULMER
3    
4    This file contains all common miscellaneous code that spans
5    different 'objects' */
6
7 #include "windef.h"
8 #include "wine/obj_base.h"
9 #include "ddraw.h"
10 #include "d3d.h"
11 #include "debug.h"
12
13 #include "d3d_private.h"
14
15 DEFAULT_DEBUG_CHANNEL(ddraw)
16
17 #ifdef HAVE_MESAGL
18
19 static void _dump_renderstate(D3DRENDERSTATETYPE type,
20                               DWORD value) {
21   char *states[] = {
22     NULL,
23     "D3DRENDERSTATE_TEXTUREHANDLE",
24     "D3DRENDERSTATE_ANTIALIAS",
25     "D3DRENDERSTATE_TEXTUREADDRESS",
26     "D3DRENDERSTATE_TEXTUREPERSPECTIVE",
27     "D3DRENDERSTATE_WRAPU",
28     "D3DRENDERSTATE_WRAPV",
29     "D3DRENDERSTATE_ZENABLE",
30     "D3DRENDERSTATE_FILLMODE",
31     "D3DRENDERSTATE_SHADEMODE",
32     "D3DRENDERSTATE_LINEPATTERN",
33     "D3DRENDERSTATE_MONOENABLE",
34     "D3DRENDERSTATE_ROP2",
35     "D3DRENDERSTATE_PLANEMASK",
36     "D3DRENDERSTATE_ZWRITEENABLE",
37     "D3DRENDERSTATE_ALPHATESTENABLE",
38     "D3DRENDERSTATE_LASTPIXEL",
39     "D3DRENDERSTATE_TEXTUREMAG",
40     "D3DRENDERSTATE_TEXTUREMIN",
41     "D3DRENDERSTATE_SRCBLEND",
42     "D3DRENDERSTATE_DESTBLEND",
43     "D3DRENDERSTATE_TEXTUREMAPBLEND",
44     "D3DRENDERSTATE_CULLMODE",
45     "D3DRENDERSTATE_ZFUNC",
46     "D3DRENDERSTATE_ALPHAREF",
47     "D3DRENDERSTATE_ALPHAFUNC",
48     "D3DRENDERSTATE_DITHERENABLE",
49     "D3DRENDERSTATE_ALPHABLENDENABLE",
50     "D3DRENDERSTATE_FOGENABLE",
51     "D3DRENDERSTATE_SPECULARENABLE",
52     "D3DRENDERSTATE_ZVISIBLE",
53     "D3DRENDERSTATE_SUBPIXEL",
54     "D3DRENDERSTATE_SUBPIXELX",
55     "D3DRENDERSTATE_STIPPLEDALPHA",
56     "D3DRENDERSTATE_FOGCOLOR",
57     "D3DRENDERSTATE_FOGTABLEMODE",
58     "D3DRENDERSTATE_FOGTABLESTART",
59     "D3DRENDERSTATE_FOGTABLEEND",
60     "D3DRENDERSTATE_FOGTABLEDENSITY",
61     "D3DRENDERSTATE_STIPPLEENABLE",
62     "D3DRENDERSTATE_EDGEANTIALIAS",
63     "D3DRENDERSTATE_COLORKEYENABLE",
64     "ERR",
65     "D3DRENDERSTATE_BORDERCOLOR",
66     "D3DRENDERSTATE_TEXTUREADDRESSU",
67     "D3DRENDERSTATE_TEXTUREADDRESSV",
68     "D3DRENDERSTATE_MIPMAPLODBIAS",
69     "D3DRENDERSTATE_ZBIAS",
70     "D3DRENDERSTATE_RANGEFOGENABLE",
71     "D3DRENDERSTATE_ANISOTROPY",
72     "D3DRENDERSTATE_FLUSHBATCH",
73     "ERR", "ERR", "ERR", "ERR", "ERR", "ERR", "ERR",
74     "ERR", "ERR", "ERR", "ERR", "ERR", "ERR",
75     "D3DRENDERSTATE_STIPPLEPATTERN00",
76     "D3DRENDERSTATE_STIPPLEPATTERN01",
77     "D3DRENDERSTATE_STIPPLEPATTERN02",
78     "D3DRENDERSTATE_STIPPLEPATTERN03",
79     "D3DRENDERSTATE_STIPPLEPATTERN04",
80     "D3DRENDERSTATE_STIPPLEPATTERN05",
81     "D3DRENDERSTATE_STIPPLEPATTERN06",
82     "D3DRENDERSTATE_STIPPLEPATTERN07",
83     "D3DRENDERSTATE_STIPPLEPATTERN08",
84     "D3DRENDERSTATE_STIPPLEPATTERN09",
85     "D3DRENDERSTATE_STIPPLEPATTERN10",
86     "D3DRENDERSTATE_STIPPLEPATTERN11",
87     "D3DRENDERSTATE_STIPPLEPATTERN12",
88     "D3DRENDERSTATE_STIPPLEPATTERN13",
89     "D3DRENDERSTATE_STIPPLEPATTERN14",
90     "D3DRENDERSTATE_STIPPLEPATTERN15",
91     "D3DRENDERSTATE_STIPPLEPATTERN16",
92     "D3DRENDERSTATE_STIPPLEPATTERN17",
93     "D3DRENDERSTATE_STIPPLEPATTERN18",
94     "D3DRENDERSTATE_STIPPLEPATTERN19",
95     "D3DRENDERSTATE_STIPPLEPATTERN20",
96     "D3DRENDERSTATE_STIPPLEPATTERN21",
97     "D3DRENDERSTATE_STIPPLEPATTERN22",
98     "D3DRENDERSTATE_STIPPLEPATTERN23",
99     "D3DRENDERSTATE_STIPPLEPATTERN24",
100     "D3DRENDERSTATE_STIPPLEPATTERN25",
101     "D3DRENDERSTATE_STIPPLEPATTERN26",
102     "D3DRENDERSTATE_STIPPLEPATTERN27",
103     "D3DRENDERSTATE_STIPPLEPATTERN28",
104     "D3DRENDERSTATE_STIPPLEPATTERN29",
105     "D3DRENDERSTATE_STIPPLEPATTERN30",
106     "D3DRENDERSTATE_STIPPLEPATTERN31"
107   };
108
109   DUMP(" %s = 0x%08lx\n", states[type], value);
110 }
111
112
113 void set_render_state(D3DRENDERSTATETYPE dwRenderStateType,
114                       DWORD dwRenderState, RenderState *rs)
115 {
116
117   if (TRACE_ON(ddraw))
118     _dump_renderstate(dwRenderStateType, dwRenderState);
119
120   /* First, all the stipple patterns */
121   if ((dwRenderStateType >= D3DRENDERSTATE_STIPPLEPATTERN00) && 
122       (dwRenderStateType <= D3DRENDERSTATE_STIPPLEPATTERN31)) {
123     ERR(ddraw, "Unhandled stipple !\n");
124   } else {
125     /* All others state variables */
126     switch (dwRenderStateType) {
127
128     case D3DRENDERSTATE_TEXTUREHANDLE: {    /*  1 */
129       IDirect3DTexture2Impl* tex = (IDirect3DTexture2Impl*) dwRenderState;
130       
131       if (tex == NULL) {
132         glBindTexture(GL_TEXTURE_2D, 0);
133         glDisable(GL_TEXTURE_2D);
134       } else {
135         TRACE(ddraw, "setting OpenGL texture handle : %d\n", tex->tex_name);
136         glEnable(GL_TEXTURE_2D);
137         /* Default parameters */
138         glBindTexture(GL_TEXTURE_2D, tex->tex_name);
139         /* To prevent state change, we could test here what are the parameters
140            stored in the texture */
141         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, rs->mag);
142         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, rs->min);
143       }
144     } break;
145
146     case D3DRENDERSTATE_TEXTUREPERSPECTIVE: /* 4 */
147       if (dwRenderState)
148         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
149       else
150         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
151       break;
152       
153     case D3DRENDERSTATE_ZENABLE:          /*  7 */
154       if (dwRenderState)
155         glEnable(GL_DEPTH_TEST);
156       else
157         glDisable(GL_DEPTH_TEST);
158       break;
159       
160     case D3DRENDERSTATE_FILLMODE:           /*  8 */
161       switch ((D3DFILLMODE) dwRenderState) {
162       case D3DFILL_SOLID:
163         break;
164
165       default:
166         ERR(ddraw, "Unhandled fill mode !\n");
167       }
168       break;
169
170     case D3DRENDERSTATE_SHADEMODE:          /*  9 */
171       switch ((D3DSHADEMODE) dwRenderState) {
172       case D3DSHADE_FLAT:
173         glShadeModel(GL_FLAT);
174         break;
175
176       case D3DSHADE_GOURAUD:
177         glShadeModel(GL_SMOOTH);
178         break;
179
180       default:
181         ERR(ddraw, "Unhandled shade mode !\n");
182       }
183       break;
184       
185     case D3DRENDERSTATE_ZWRITEENABLE:     /* 14 */
186       if (dwRenderState)
187         glDepthMask(GL_TRUE);
188       else
189         glDepthMask(GL_FALSE);
190       break;
191       
192     case D3DRENDERSTATE_TEXTUREMAG:         /* 17 */
193       switch ((D3DTEXTUREFILTER) dwRenderState) {
194       case D3DFILTER_NEAREST:
195         rs->mag = GL_NEAREST;
196         break;
197         
198       case D3DFILTER_LINEAR:
199         rs->mag = GL_LINEAR;
200         break;
201         
202       default:
203         ERR(ddraw, "Unhandled texture mag !\n");
204       }
205       break;
206
207     case D3DRENDERSTATE_TEXTUREMIN:         /* 18 */
208       switch ((D3DTEXTUREFILTER) dwRenderState) {
209       case D3DFILTER_NEAREST:
210         rs->min = GL_NEAREST;
211         break;
212         
213       case D3DFILTER_LINEAR:
214         rs->mag = GL_LINEAR;
215         break;
216         
217       default:
218         ERR(ddraw, "Unhandled texture min !\n");
219       }
220       break;
221       
222     case D3DRENDERSTATE_SRCBLEND:           /* 19 */
223       switch ((D3DBLEND) dwRenderState) {
224       case D3DBLEND_SRCALPHA:
225         rs->src = GL_SRC_ALPHA;
226         break;
227
228       default:
229         ERR(ddraw, "Unhandled blend mode !\n");
230       }
231       
232       glBlendFunc(rs->src, rs->dst);
233       break;
234       
235     case D3DRENDERSTATE_DESTBLEND:          /* 20 */
236       switch ((D3DBLEND) dwRenderState) {
237       case D3DBLEND_INVSRCALPHA:
238         rs->dst = GL_ONE_MINUS_SRC_ALPHA;
239         break;
240         
241       default:
242         ERR(ddraw, "Unhandled blend mode !\n");
243       }
244       
245       glBlendFunc(rs->src, rs->dst);
246       break;
247
248     case D3DRENDERSTATE_TEXTUREMAPBLEND:    /* 21 */
249       switch ((D3DTEXTUREBLEND) dwRenderState) {
250       case D3DTBLEND_MODULATE:
251       case D3DTBLEND_MODULATEALPHA:
252         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
253         break;
254
255       default:
256         ERR(ddraw, "Unhandled texture environment !\n");
257       }
258       break;
259       
260     case D3DRENDERSTATE_CULLMODE:           /* 22 */
261       switch ((D3DCULL) dwRenderState) {
262       case D3DCULL_NONE:
263         glDisable(GL_CULL_FACE);
264         break;
265         
266       case D3DCULL_CW:
267         glEnable(GL_CULL_FACE);
268         glFrontFace(GL_CW);
269         break;
270         
271       case D3DCULL_CCW:
272         glEnable(GL_CULL_FACE);
273         glFrontFace(GL_CCW);
274         break;
275         
276       default:
277         ERR(ddraw, "Unhandled cull mode !\n");
278       }
279       break;
280       
281     case D3DRENDERSTATE_ZFUNC:            /* 23 */
282       switch ((D3DCMPFUNC) dwRenderState) {
283       case D3DCMP_NEVER:
284         glDepthFunc(GL_NEVER);
285         break;
286       case D3DCMP_LESS:
287         glDepthFunc(GL_LESS);
288         break;
289       case D3DCMP_EQUAL:
290         glDepthFunc(GL_EQUAL);
291         break;
292       case D3DCMP_LESSEQUAL:
293         glDepthFunc(GL_LEQUAL);
294         break;
295       case D3DCMP_GREATER:
296         glDepthFunc(GL_GREATER);
297         break;
298       case D3DCMP_NOTEQUAL:
299         glDepthFunc(GL_NOTEQUAL);
300         break;
301       case D3DCMP_GREATEREQUAL:
302         glDepthFunc(GL_GEQUAL);
303         break;
304       case D3DCMP_ALWAYS:
305         glDepthFunc(GL_ALWAYS);
306         break;
307
308       default:
309         ERR(ddraw, "Unexpected value\n");
310       }
311       break;
312       
313     case D3DRENDERSTATE_DITHERENABLE:     /* 26 */
314       if (dwRenderState)
315         glEnable(GL_DITHER);
316       else
317         glDisable(GL_DITHER);
318       break;
319       
320     case D3DRENDERSTATE_ALPHABLENDENABLE:   /* 27 */
321       if (dwRenderState)
322         glEnable(GL_BLEND);
323       else
324         glDisable(GL_BLEND);
325       break;
326
327     case D3DRENDERSTATE_COLORKEYENABLE:     /* 41 */
328       if (dwRenderState)
329         glEnable(GL_BLEND);
330       else
331         glDisable(GL_BLEND);
332       break;
333
334     case D3DRENDERSTATE_FLUSHBATCH:         /* 50 */
335       break;
336       
337     default:
338       ERR(ddraw, "Unhandled Render State\n");
339       break;
340     }
341   }
342 }
343
344
345 #endif /* HAVE_MESAGL */