- hack for one case of the ProcessVertices case
[wine] / dlls / ddraw / d3dcommon.c
1 /* Direct3D Common functions
2  * Copyright (c) 1998 Lionel ULMER
3  *
4  * This file contains all common miscellaneous code that spans
5  * different 'objects'
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "windef.h"
23 #include "objbase.h"
24 #include "ddraw.h"
25 #include "d3d.h"
26 #include "wine/debug.h"
27
28 #include "d3d_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
31
32 const char *_get_renderstate(D3DRENDERSTATETYPE type) {
33     static const char * const states[] = {
34         "ERR",
35         "D3DRENDERSTATE_TEXTUREHANDLE",
36         "D3DRENDERSTATE_ANTIALIAS",
37         "D3DRENDERSTATE_TEXTUREADDRESS",
38         "D3DRENDERSTATE_TEXTUREPERSPECTIVE",
39         "D3DRENDERSTATE_WRAPU",
40         "D3DRENDERSTATE_WRAPV",
41         "D3DRENDERSTATE_ZENABLE",
42         "D3DRENDERSTATE_FILLMODE",
43         "D3DRENDERSTATE_SHADEMODE",
44         "D3DRENDERSTATE_LINEPATTERN",
45         "D3DRENDERSTATE_MONOENABLE",
46         "D3DRENDERSTATE_ROP2",
47         "D3DRENDERSTATE_PLANEMASK",
48         "D3DRENDERSTATE_ZWRITEENABLE",
49         "D3DRENDERSTATE_ALPHATESTENABLE",
50         "D3DRENDERSTATE_LASTPIXEL",
51         "D3DRENDERSTATE_TEXTUREMAG",
52         "D3DRENDERSTATE_TEXTUREMIN",
53         "D3DRENDERSTATE_SRCBLEND",
54         "D3DRENDERSTATE_DESTBLEND",
55         "D3DRENDERSTATE_TEXTUREMAPBLEND",
56         "D3DRENDERSTATE_CULLMODE",
57         "D3DRENDERSTATE_ZFUNC",
58         "D3DRENDERSTATE_ALPHAREF",
59         "D3DRENDERSTATE_ALPHAFUNC",
60         "D3DRENDERSTATE_DITHERENABLE",
61         "D3DRENDERSTATE_ALPHABLENDENABLE",
62         "D3DRENDERSTATE_FOGENABLE",
63         "D3DRENDERSTATE_SPECULARENABLE",
64         "D3DRENDERSTATE_ZVISIBLE",
65         "D3DRENDERSTATE_SUBPIXEL",
66         "D3DRENDERSTATE_SUBPIXELX",
67         "D3DRENDERSTATE_STIPPLEDALPHA",
68         "D3DRENDERSTATE_FOGCOLOR",
69         "D3DRENDERSTATE_FOGTABLEMODE",
70         "D3DRENDERSTATE_FOGTABLESTART",
71         "D3DRENDERSTATE_FOGTABLEEND",
72         "D3DRENDERSTATE_FOGTABLEDENSITY",
73         "D3DRENDERSTATE_STIPPLEENABLE",
74         "D3DRENDERSTATE_EDGEANTIALIAS",
75         "D3DRENDERSTATE_COLORKEYENABLE",
76         "ERR",
77         "D3DRENDERSTATE_BORDERCOLOR",
78         "D3DRENDERSTATE_TEXTUREADDRESSU",
79         "D3DRENDERSTATE_TEXTUREADDRESSV",
80         "D3DRENDERSTATE_MIPMAPLODBIAS",
81         "D3DRENDERSTATE_ZBIAS",
82         "D3DRENDERSTATE_RANGEFOGENABLE",
83         "D3DRENDERSTATE_ANISOTROPY",
84         "D3DRENDERSTATE_FLUSHBATCH",
85         "D3DRENDERSTATE_TRANSLUCENTSORTINDEPENDENT",
86         "D3DRENDERSTATE_STENCILENABLE",
87         "D3DRENDERSTATE_STENCILFAIL",
88         "D3DRENDERSTATE_STENCILZFAIL",
89         "D3DRENDERSTATE_STENCILPASS",
90         "D3DRENDERSTATE_STENCILFUNC",
91         "D3DRENDERSTATE_STENCILREF",
92         "D3DRENDERSTATE_STENCILMASK",
93         "D3DRENDERSTATE_STENCILWRITEMASK",
94         "D3DRENDERSTATE_TEXTUREFACTOR",
95         "ERR",
96         "ERR",
97         "ERR",
98         "D3DRENDERSTATE_STIPPLEPATTERN00",
99         "D3DRENDERSTATE_STIPPLEPATTERN01",
100         "D3DRENDERSTATE_STIPPLEPATTERN02",
101         "D3DRENDERSTATE_STIPPLEPATTERN03",
102         "D3DRENDERSTATE_STIPPLEPATTERN04",
103         "D3DRENDERSTATE_STIPPLEPATTERN05",
104         "D3DRENDERSTATE_STIPPLEPATTERN06",
105         "D3DRENDERSTATE_STIPPLEPATTERN07",
106         "D3DRENDERSTATE_STIPPLEPATTERN08",
107         "D3DRENDERSTATE_STIPPLEPATTERN09",
108         "D3DRENDERSTATE_STIPPLEPATTERN10",
109         "D3DRENDERSTATE_STIPPLEPATTERN11",
110         "D3DRENDERSTATE_STIPPLEPATTERN12",
111         "D3DRENDERSTATE_STIPPLEPATTERN13",
112         "D3DRENDERSTATE_STIPPLEPATTERN14",
113         "D3DRENDERSTATE_STIPPLEPATTERN15",
114         "D3DRENDERSTATE_STIPPLEPATTERN16",
115         "D3DRENDERSTATE_STIPPLEPATTERN17",
116         "D3DRENDERSTATE_STIPPLEPATTERN18",
117         "D3DRENDERSTATE_STIPPLEPATTERN19",
118         "D3DRENDERSTATE_STIPPLEPATTERN20",
119         "D3DRENDERSTATE_STIPPLEPATTERN21",
120         "D3DRENDERSTATE_STIPPLEPATTERN22",
121         "D3DRENDERSTATE_STIPPLEPATTERN23",
122         "D3DRENDERSTATE_STIPPLEPATTERN24",
123         "D3DRENDERSTATE_STIPPLEPATTERN25",
124         "D3DRENDERSTATE_STIPPLEPATTERN26",
125         "D3DRENDERSTATE_STIPPLEPATTERN27",
126         "D3DRENDERSTATE_STIPPLEPATTERN28",
127         "D3DRENDERSTATE_STIPPLEPATTERN29",
128         "D3DRENDERSTATE_STIPPLEPATTERN30",
129         "D3DRENDERSTATE_STIPPLEPATTERN31"
130     };
131     static const char * const states_2[] = {
132         "D3DRENDERSTATE_WRAP0",
133         "D3DRENDERSTATE_WRAP1",
134         "D3DRENDERSTATE_WRAP2",
135         "D3DRENDERSTATE_WRAP3",
136         "D3DRENDERSTATE_WRAP4",
137         "D3DRENDERSTATE_WRAP5",
138         "D3DRENDERSTATE_WRAP6",
139         "D3DRENDERSTATE_WRAP7",
140         "D3DRENDERSTATE_CLIPPING",
141         "D3DRENDERSTATE_LIGHTING",
142         "D3DRENDERSTATE_EXTENTS",
143         "D3DRENDERSTATE_AMBIENT",
144         "D3DRENDERSTATE_FOGVERTEXMODE",
145         "D3DRENDERSTATE_COLORVERTEX",
146         "D3DRENDERSTATE_LOCALVIEWER",
147         "D3DRENDERSTATE_NORMALIZENORMALS",
148         "D3DRENDERSTATE_COLORKEYBLENDENABLE",
149         "D3DRENDERSTATE_DIFFUSEMATERIALSOURCE",
150         "D3DRENDERSTATE_SPECULARMATERIALSOURCE",
151         "D3DRENDERSTATE_AMBIENTMATERIALSOURCE",
152         "D3DRENDERSTATE_EMISSIVEMATERIALSOURCE",
153         "ERR",
154         "ERR",
155         "D3DRENDERSTATE_VERTEXBLEND",
156         "D3DRENDERSTATE_CLIPPLANEENABLE",
157     };
158     if (type >= D3DRENDERSTATE_WRAP0) {
159         type -= D3DRENDERSTATE_WRAP0;
160         if (type >= (sizeof(states_2) / sizeof(states_2[0]))) return "ERR";
161         return states_2[type];
162     }
163     if (type >= (sizeof(states) / sizeof(states[0]))) return "ERR";
164     return states[type];
165 }
166
167 void
168 dump_D3DCOLORVALUE(D3DCOLORVALUE *lpCol)
169 {
170     DPRINTF("%f %f %f %f", lpCol->u1.r, lpCol->u2.g, lpCol->u3.b, lpCol->u4.a);
171 }
172
173 void
174 dump_D3DVECTOR(D3DVECTOR *lpVec)
175 {
176     DPRINTF("%f %f %f", lpVec->u1.x, lpVec->u2.y, lpVec->u3.z);
177 }
178
179 void
180 dump_D3DMATERIAL7(LPD3DMATERIAL7 lpMat)
181 {
182     DPRINTF(" - diffuse  : "); dump_D3DCOLORVALUE(&(lpMat->u.diffuse)); DPRINTF("\n");
183     DPRINTF(" - ambient  : "); dump_D3DCOLORVALUE(&(lpMat->u1.ambient)); DPRINTF("\n");
184     DPRINTF(" - specular : "); dump_D3DCOLORVALUE(&(lpMat->u2.specular)); DPRINTF("\n");
185     DPRINTF(" - emissive : "); dump_D3DCOLORVALUE(&(lpMat->u3.emissive)); DPRINTF("\n");
186     DPRINTF(" - power    : %f\n", lpMat->u4.power);
187 }
188
189 void
190 dump_D3DLIGHT7(LPD3DLIGHT7 lpLight)
191 {
192     DPRINTF(" - light type : %s\n", (lpLight->dltType == D3DLIGHT_POINT ? "D3DLIGHT_POINT" : 
193                                      (lpLight->dltType == D3DLIGHT_SPOT ? "D3DLIGHT_SPOT" : 
194                                       (lpLight->dltType == D3DLIGHT_DIRECTIONAL ? "D3DLIGHT_DIRECTIONAL" : 
195                                        "UNSUPPORTED"))));
196     DPRINTF(" - diffuse       : "); dump_D3DCOLORVALUE(&(lpLight->dcvDiffuse)); DPRINTF("\n");
197     DPRINTF(" - specular      : "); dump_D3DCOLORVALUE(&(lpLight->dcvSpecular)); DPRINTF("\n");
198     DPRINTF(" - ambient       : "); dump_D3DCOLORVALUE(&(lpLight->dcvAmbient)); DPRINTF("\n");
199     DPRINTF(" - position      : "); dump_D3DVECTOR(&(lpLight->dvPosition)); DPRINTF("\n");
200     DPRINTF(" - direction     : "); dump_D3DVECTOR(&(lpLight->dvDirection)); DPRINTF("\n");
201     DPRINTF(" - dvRange       : %f\n", lpLight->dvRange);
202     DPRINTF(" - dvFalloff     : %f\n", lpLight->dvFalloff);
203     DPRINTF(" - dvAttenuation : %f %f %f\n", lpLight->dvAttenuation0, lpLight->dvAttenuation1, lpLight->dvAttenuation2);
204     DPRINTF(" - dvTheta       : %f\n", lpLight->dvTheta);
205     DPRINTF(" - dvPhi         : %f\n", lpLight->dvPhi);
206 }
207
208 void
209 dump_DPFLAGS(DWORD dwFlags)
210 {
211     static const flag_info flags[] =
212     {
213         FE(D3DDP_WAIT),
214         FE(D3DDP_OUTOFORDER),
215         FE(D3DDP_DONOTCLIP),
216         FE(D3DDP_DONOTUPDATEEXTENTS),
217         FE(D3DDP_DONOTLIGHT)
218     };
219
220     DDRAW_dump_flags(dwFlags, flags, sizeof(flags)/sizeof(flags[0]));
221 }
222
223 void
224 dump_D3DMATRIX(D3DMATRIX *mat)
225 {
226     DPRINTF("  %f %f %f %f\n", mat->_11, mat->_12, mat->_13, mat->_14);
227     DPRINTF("  %f %f %f %f\n", mat->_21, mat->_22, mat->_23, mat->_24);
228     DPRINTF("  %f %f %f %f\n", mat->_31, mat->_32, mat->_33, mat->_34);
229     DPRINTF("  %f %f %f %f\n", mat->_41, mat->_42, mat->_43, mat->_44);
230 }
231
232
233 DWORD get_flexible_vertex_size(DWORD d3dvtVertexType)
234 {
235     DWORD size = 0;
236     
237     if (d3dvtVertexType & D3DFVF_NORMAL) size += 3 * sizeof(D3DVALUE);
238     if (d3dvtVertexType & D3DFVF_DIFFUSE) size += sizeof(DWORD);
239     if (d3dvtVertexType & D3DFVF_SPECULAR) size += sizeof(DWORD);
240     if (d3dvtVertexType & D3DFVF_RESERVED1) size += sizeof(DWORD);
241     switch (d3dvtVertexType & D3DFVF_POSITION_MASK) {
242         case D3DFVF_XYZ: size += 3 * sizeof(D3DVALUE); break;
243         case D3DFVF_XYZRHW: size += 4 * sizeof(D3DVALUE); break;
244         default: TRACE(" matrix weighting not handled yet...\n");
245     }
246     size += 2 * sizeof(D3DVALUE) * ((d3dvtVertexType & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT);
247
248     return size;
249 }
250
251 void dump_flexible_vertex(DWORD d3dvtVertexType)
252 {
253     static const flag_info flags[] = {
254         FE(D3DFVF_NORMAL),
255         FE(D3DFVF_RESERVED1),
256         FE(D3DFVF_DIFFUSE),
257         FE(D3DFVF_SPECULAR)
258     };
259     int i;
260     
261     if (d3dvtVertexType & D3DFVF_RESERVED0) DPRINTF("D3DFVF_RESERVED0 ");
262     switch (d3dvtVertexType & D3DFVF_POSITION_MASK) {
263 #define GEN_CASE(a) case a: DPRINTF(#a " "); break
264         GEN_CASE(D3DFVF_XYZ);
265         GEN_CASE(D3DFVF_XYZRHW);
266         GEN_CASE(D3DFVF_XYZB1);
267         GEN_CASE(D3DFVF_XYZB2);
268         GEN_CASE(D3DFVF_XYZB3);
269         GEN_CASE(D3DFVF_XYZB4);
270         GEN_CASE(D3DFVF_XYZB5);
271     }
272     DDRAW_dump_flags_(d3dvtVertexType, flags, sizeof(flags)/sizeof(flags[0]), FALSE);
273     switch (d3dvtVertexType & D3DFVF_TEXCOUNT_MASK) {
274         GEN_CASE(D3DFVF_TEX0);
275         GEN_CASE(D3DFVF_TEX1);
276         GEN_CASE(D3DFVF_TEX2);
277         GEN_CASE(D3DFVF_TEX3);
278         GEN_CASE(D3DFVF_TEX4);
279         GEN_CASE(D3DFVF_TEX5);
280         GEN_CASE(D3DFVF_TEX6);
281         GEN_CASE(D3DFVF_TEX7);
282         GEN_CASE(D3DFVF_TEX8);
283     }
284 #undef GEN_CASE
285     for (i = 0; i < ((d3dvtVertexType & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT); i++) {
286         DPRINTF(" T%d-s%ld", i + 1, (((d3dvtVertexType >> (16 + (2 * i))) + 1) & 0x03) + 1);
287     }
288     DPRINTF("\n");
289 }
290
291 void
292 convert_FVF_to_strided_data(DWORD d3dvtVertexType, LPVOID lpvVertices, D3DDRAWPRIMITIVESTRIDEDDATA *strided)
293 {
294     int current_offset = 0;
295     int tex_index;
296     
297     if ((d3dvtVertexType & D3DFVF_POSITION_MASK) == D3DFVF_XYZ) {
298         strided->position.lpvData = lpvVertices;
299         current_offset += 3 * sizeof(D3DVALUE);
300     } else {
301         strided->position.lpvData  = lpvVertices;
302         current_offset += 4 * sizeof(D3DVALUE);
303     }
304     if (d3dvtVertexType & D3DFVF_RESERVED1) {
305         current_offset += sizeof(DWORD);
306     }
307     if (d3dvtVertexType & D3DFVF_NORMAL) { 
308         strided->normal.lpvData  = ((char *) lpvVertices) + current_offset;
309         current_offset += 3 * sizeof(D3DVALUE);
310     }
311     if (d3dvtVertexType & D3DFVF_DIFFUSE) { 
312         strided->diffuse.lpvData  = ((char *) lpvVertices) + current_offset;
313         current_offset += sizeof(DWORD);
314     }
315     if (d3dvtVertexType & D3DFVF_SPECULAR) {
316         strided->specular.lpvData  = ((char *) lpvVertices) + current_offset;
317         current_offset += sizeof(DWORD);
318     }
319     for (tex_index = 0; tex_index < ((d3dvtVertexType & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT); tex_index++) {
320         strided->textureCoords[tex_index].lpvData  = ((char *) lpvVertices) + current_offset;
321         current_offset += 2 * sizeof(D3DVALUE);
322     }
323     strided->position.dwStride = current_offset;
324     strided->normal.dwStride   = current_offset;
325     strided->diffuse.dwStride  = current_offset;
326     strided->specular.dwStride = current_offset;
327     for (tex_index = 0; tex_index < ((d3dvtVertexType & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT); tex_index++)
328         strided->textureCoords[tex_index].dwStride  = current_offset;   
329 }
330
331 void
332 dump_D3DVOP(DWORD dwVertexOp)
333 {
334     static const flag_info flags[] =
335     {
336         FE(D3DVOP_LIGHT),
337         FE(D3DVOP_CLIP),
338         FE(D3DVOP_EXTENTS),
339         FE(D3DVOP_TRANSFORM)
340     };
341     DDRAW_dump_flags(dwVertexOp, flags, sizeof(flags)/sizeof(flags[0]));
342 }
343
344 void
345 dump_D3DPV(DWORD dwFlags)
346 {
347     if (dwFlags == D3DPV_DONOTCOPYDATA) DPRINTF("D3DPV_DONOTCOPYDATA\n");
348     else if (dwFlags != 0) DPRINTF("Unknown !!!\n");
349     else DPRINTF("\n");
350 }