Moved signal handling to exception.c, generate exceptions on signals.
[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     ENTER_GL();
126     
127     /* All others state variables */
128     switch (dwRenderStateType) {
129
130     case D3DRENDERSTATE_TEXTUREHANDLE: {    /*  1 */
131       IDirect3DTexture2Impl* tex = (IDirect3DTexture2Impl*) dwRenderState;
132       
133       if (tex == NULL) {
134         glBindTexture(GL_TEXTURE_2D, 0);
135         glDisable(GL_TEXTURE_2D);
136       } else {
137         TRACE(ddraw, "setting OpenGL texture handle : %d\n", tex->tex_name);
138         glEnable(GL_TEXTURE_2D);
139         /* Default parameters */
140         glBindTexture(GL_TEXTURE_2D, tex->tex_name);
141         /* To prevent state change, we could test here what are the parameters
142            stored in the texture */
143         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, rs->mag);
144         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, rs->min);
145       }
146     } break;
147
148     case D3DRENDERSTATE_TEXTUREPERSPECTIVE: /* 4 */
149       if (dwRenderState)
150         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
151       else
152         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
153       break;
154       
155     case D3DRENDERSTATE_ZENABLE:          /*  7 */
156       if (dwRenderState)
157         glEnable(GL_DEPTH_TEST);
158       else
159         glDisable(GL_DEPTH_TEST);
160       break;
161       
162     case D3DRENDERSTATE_FILLMODE:           /*  8 */
163       switch ((D3DFILLMODE) dwRenderState) {
164       case D3DFILL_SOLID:
165         break;
166
167       default:
168         ERR(ddraw, "Unhandled fill mode !\n");
169       }
170       break;
171
172     case D3DRENDERSTATE_SHADEMODE:          /*  9 */
173       switch ((D3DSHADEMODE) dwRenderState) {
174       case D3DSHADE_FLAT:
175         glShadeModel(GL_FLAT);
176         break;
177
178       case D3DSHADE_GOURAUD:
179         glShadeModel(GL_SMOOTH);
180         break;
181
182       default:
183         ERR(ddraw, "Unhandled shade mode !\n");
184       }
185       break;
186       
187     case D3DRENDERSTATE_ZWRITEENABLE:     /* 14 */
188       if (dwRenderState)
189         glDepthMask(GL_TRUE);
190       else
191         glDepthMask(GL_FALSE);
192       break;
193       
194     case D3DRENDERSTATE_TEXTUREMAG:         /* 17 */
195       switch ((D3DTEXTUREFILTER) dwRenderState) {
196       case D3DFILTER_NEAREST:
197         rs->mag = GL_NEAREST;
198         break;
199         
200       case D3DFILTER_LINEAR:
201         rs->mag = GL_LINEAR;
202         break;
203         
204       default:
205         ERR(ddraw, "Unhandled texture mag !\n");
206       }
207       break;
208
209     case D3DRENDERSTATE_TEXTUREMIN:         /* 18 */
210       switch ((D3DTEXTUREFILTER) dwRenderState) {
211       case D3DFILTER_NEAREST:
212         rs->min = GL_NEAREST;
213         break;
214         
215       case D3DFILTER_LINEAR:
216         rs->mag = GL_LINEAR;
217         break;
218         
219       default:
220         ERR(ddraw, "Unhandled texture min !\n");
221       }
222       break;
223       
224     case D3DRENDERSTATE_SRCBLEND:           /* 19 */
225       switch ((D3DBLEND) dwRenderState) {
226       case D3DBLEND_SRCALPHA:
227         rs->src = GL_SRC_ALPHA;
228         break;
229
230       default:
231         ERR(ddraw, "Unhandled blend mode !\n");
232       }
233       
234       glBlendFunc(rs->src, rs->dst);
235       break;
236       
237     case D3DRENDERSTATE_DESTBLEND:          /* 20 */
238       switch ((D3DBLEND) dwRenderState) {
239       case D3DBLEND_INVSRCALPHA:
240         rs->dst = GL_ONE_MINUS_SRC_ALPHA;
241         break;
242         
243       default:
244         ERR(ddraw, "Unhandled blend mode !\n");
245       }
246       
247       glBlendFunc(rs->src, rs->dst);
248       break;
249
250     case D3DRENDERSTATE_TEXTUREMAPBLEND:    /* 21 */
251       switch ((D3DTEXTUREBLEND) dwRenderState) {
252       case D3DTBLEND_MODULATE:
253       case D3DTBLEND_MODULATEALPHA:
254         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
255         break;
256
257       default:
258         ERR(ddraw, "Unhandled texture environment !\n");
259       }
260       break;
261       
262     case D3DRENDERSTATE_CULLMODE:           /* 22 */
263       switch ((D3DCULL) dwRenderState) {
264       case D3DCULL_NONE:
265         glDisable(GL_CULL_FACE);
266         break;
267         
268       case D3DCULL_CW:
269         glEnable(GL_CULL_FACE);
270         glFrontFace(GL_CW);
271         break;
272         
273       case D3DCULL_CCW:
274         glEnable(GL_CULL_FACE);
275         glFrontFace(GL_CCW);
276         break;
277         
278       default:
279         ERR(ddraw, "Unhandled cull mode !\n");
280       }
281       break;
282       
283     case D3DRENDERSTATE_ZFUNC:            /* 23 */
284       switch ((D3DCMPFUNC) dwRenderState) {
285       case D3DCMP_NEVER:
286         glDepthFunc(GL_NEVER);
287         break;
288       case D3DCMP_LESS:
289         glDepthFunc(GL_LESS);
290         break;
291       case D3DCMP_EQUAL:
292         glDepthFunc(GL_EQUAL);
293         break;
294       case D3DCMP_LESSEQUAL:
295         glDepthFunc(GL_LEQUAL);
296         break;
297       case D3DCMP_GREATER:
298         glDepthFunc(GL_GREATER);
299         break;
300       case D3DCMP_NOTEQUAL:
301         glDepthFunc(GL_NOTEQUAL);
302         break;
303       case D3DCMP_GREATEREQUAL:
304         glDepthFunc(GL_GEQUAL);
305         break;
306       case D3DCMP_ALWAYS:
307         glDepthFunc(GL_ALWAYS);
308         break;
309
310       default:
311         ERR(ddraw, "Unexpected value\n");
312       }
313       break;
314       
315     case D3DRENDERSTATE_DITHERENABLE:     /* 26 */
316       if (dwRenderState)
317         glEnable(GL_DITHER);
318       else
319         glDisable(GL_DITHER);
320       break;
321       
322     case D3DRENDERSTATE_ALPHABLENDENABLE:   /* 27 */
323       if (dwRenderState)
324         glEnable(GL_BLEND);
325       else
326         glDisable(GL_BLEND);
327       break;
328
329     case D3DRENDERSTATE_COLORKEYENABLE:     /* 41 */
330       if (dwRenderState)
331         glEnable(GL_BLEND);
332       else
333         glDisable(GL_BLEND);
334       break;
335
336     case D3DRENDERSTATE_FLUSHBATCH:         /* 50 */
337       break;
338       
339     default:
340       ERR(ddraw, "Unhandled Render State\n");
341       break;
342     }
343
344     LEAVE_GL();
345   }
346 }
347
348
349 #endif /* HAVE_MESAGL */