Bugfix for EnumPrinters level 2.
[wine] / graphics / d3dexecutebuffer.c
1 /* Direct3D ExecuteBuffer
2    (c) 1998 Lionel ULMER
3    
4    This files contains the implementation of Direct3DExecuteBuffer. */
5
6
7 #include <string.h>
8 #include "config.h"
9 #include "windef.h"
10 #include "winerror.h"
11 #include "wine/obj_base.h"
12 #include "heap.h"
13 #include "ddraw.h"
14 #include "d3d.h"
15 #include "debug.h"
16
17 #include "d3d_private.h"
18
19 DEFAULT_DEBUG_CHANNEL(ddraw)
20
21 #ifdef HAVE_MESAGL
22
23 /* Structure to store the 'semi transformed' vertices */
24 typedef struct {
25   D3DVALUE x;
26   D3DVALUE y;
27   D3DVALUE z;
28   D3DVALUE w;
29
30   D3DVALUE nx;
31   D3DVALUE ny;
32   D3DVALUE nz;
33
34   D3DVALUE u;
35   D3DVALUE v;
36 } OGL_Vertex;
37
38 typedef struct {
39   D3DVALUE x;
40   D3DVALUE y;
41   D3DVALUE z;
42   D3DVALUE w;
43
44   D3DCOLOR c;
45   D3DCOLOR sc;
46
47   D3DVALUE u;
48   D3DVALUE v;
49 } OGL_LVertex;
50
51 static ICOM_VTABLE(IDirect3DExecuteBuffer) executebuffer_vtable;
52
53 /*******************************************************************************
54  *                              ExecuteBuffer static functions
55  */
56 void _dump_d3dstatus(LPD3DSTATUS lpStatus) {
57   
58 }
59
60 void _dump_executedata(LPD3DEXECUTEDATA lpData) {
61   DUMP("dwSize : %ld\n", lpData->dwSize);
62   DUMP("Vertex      Offset : %ld  Count  : %ld\n", lpData->dwVertexOffset, lpData->dwVertexCount);
63   DUMP("Instruction Offset : %ld  Length : %ld\n", lpData->dwInstructionOffset, lpData->dwInstructionLength);
64   DUMP("HVertex     Offset : %ld\n", lpData->dwHVertexOffset);
65   _dump_d3dstatus(&(lpData->dsStatus));
66 }
67
68 #define DO_VERTEX(index)                        \
69 {                                                                               \
70   glTexCoord2f(vx[index].u,                                                     \
71                vx[index].v);                                                    \
72   glNormal3f(vx[index].nx,                                                      \
73              vx[index].ny,                                                      \
74              vx[index].nz);                                                     \
75   glVertex4f(vx[index].x,                                                       \
76              vx[index].y,                                                       \
77              vx[index].z,                                                       \
78              vx[index].w);                                                      \
79                                                                                 \
80   TRACE(ddraw, "   V: %f %f %f %f (%f %f %f) (%f %f)\n",                        \
81         vx[index].x, vx[index].y, vx[index].z, vx[index].w,                     \
82         vx[index].nx, vx[index].ny, vx[index].nz,                               \
83         vx[index].u, vx[index].v);                                              \
84 }
85
86 #define DO_LVERTEX(index)                                                       \
87 {                                                                               \
88   DWORD col = l_vx[index].c;                                                    \
89                                                                                 \
90   glColor3f(((col >> 16) & 0xFF) / 255.0,                                       \
91             ((col >>  8) & 0xFF) / 255.0,                                       \
92             ((col >>  0) & 0xFF) / 255.0);                                      \
93   glTexCoord2f(l_vx[index].u,                                                   \
94                l_vx[index].v);                                                  \
95   glVertex4f(l_vx[index].x,                                                     \
96              l_vx[index].y,                                                     \
97              l_vx[index].z,                                                     \
98              l_vx[index].w);                                                    \
99                                                                                 \
100   TRACE(ddraw, "  LV: %f %f %f %f (%02lx %02lx %02lx) (%f %f)\n",               \
101         l_vx[index].x, l_vx[index].y, l_vx[index].z, l_vx[index].w,             \
102         ((col >> 16) & 0xFF), ((col >>  8) & 0xFF), ((col >>  0) & 0xFF),       \
103         l_vx[index].u, l_vx[index].v);                                          \
104 }
105
106 #define DO_TLVERTEX(index)                                                      \
107 {                                                                               \
108   D3DTLVERTEX *vx = &(tl_vx[index]);                                            \
109   DWORD col = vx->c.color;                                                      \
110                                                                                 \
111   glColor3f(((col >> 16) & 0xFF) / 255.0,                                       \
112             ((col >>  8) & 0xFF) / 255.0,                                       \
113             ((col >>  0) & 0xFF) / 255.0);                                      \
114   glTexCoord2f(vx->u.tu, vx->v.tv);                                             \
115   if (vx->r.rhw < 0.01)                                                         \
116     glVertex3f(vx->x.sx,                                                        \
117                vx->y.sy,                                                        \
118                vx->z.sz);                                                       \
119   else                                                                          \
120     glVertex4f(vx->x.sx / vx->r.rhw,                                            \
121                vx->y.sy / vx->r.rhw,                                            \
122                vx->z.sz / vx->r.rhw,                                            \
123                1.0 / vx->r.rhw);                                                \
124   TRACE(ddraw, " TLV: %f %f %f (%02lx %02lx %02lx) (%f %f) (%f)\n",             \
125         vx->x.sx, vx->y.sy, vx->z.sz,                                           \
126         ((col >> 16) & 0xFF), ((col >>  8) & 0xFF), ((col >>  0) & 0xFF),       \
127         vx->u.tu, vx->v.tv, vx->r.rhw);                                         \
128 }
129
130 #define TRIANGLE_LOOP(macro)                            \
131 {                                                       \
132   glBegin(GL_TRIANGLES); {                              \
133     for (i = 0; i < count; i++) {                       \
134       LPD3DTRIANGLE ci = (LPD3DTRIANGLE) instr;         \
135                                                         \
136       TRACE(ddraw, "  v1: %d  v2: %d  v3: %d\n",        \
137             ci->v1.v1, ci->v2.v2, ci->v3.v3);           \
138       TRACE(ddraw, "  Flags : ");                       \
139       if (TRACE_ON(ddraw)) {                            \
140         /* Wireframe */                                 \
141         if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)        \
142           DUMP("EDGEENABLE1 ");                         \
143         if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2)        \
144           DUMP("EDGEENABLE2 ");                         \
145         if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)        \
146           DUMP("EDGEENABLE3 ");                         \
147                                                         \
148         /* Strips / Fans */                             \
149         if (ci->wFlags == D3DTRIFLAG_EVEN)              \
150           DUMP("EVEN ");                                \
151         if (ci->wFlags == D3DTRIFLAG_ODD)               \
152           DUMP("ODD ");                                 \
153         if (ci->wFlags == D3DTRIFLAG_START)             \
154           DUMP("START ");                               \
155         if ((ci->wFlags > 0) && (ci->wFlags < 30))      \
156           DUMP("STARTFLAT(%d) ", ci->wFlags);           \
157         DUMP("\n");                                     \
158       }                                                 \
159                                                         \
160       /* Draw the triangle */                           \
161       macro(ci->v1.v1);                                 \
162       macro(ci->v2.v2);                                 \
163       macro(ci->v3.v3);                                 \
164                                                         \
165       instr += size;                                    \
166     }                                                   \
167   } glEnd();                                            \
168 }
169
170
171 static void execute(LPDIRECT3DEXECUTEBUFFER lpBuff,
172                     LPDIRECT3DDEVICE dev,
173                     LPDIRECT3DVIEWPORT vp) {
174   IDirect3DExecuteBufferImpl* ilpBuff=(IDirect3DExecuteBufferImpl*)lpBuff;
175   IDirect3DViewport2Impl* ivp=(IDirect3DViewport2Impl*)vp;
176   /* DWORD bs = ilpBuff->desc.dwBufferSize; */
177   DWORD vs = ilpBuff->data.dwVertexOffset;
178   /* DWORD vc = ilpBuff->data.dwVertexCount; */
179   DWORD is = ilpBuff->data.dwInstructionOffset;
180   /* DWORD il = ilpBuff->data.dwInstructionLength; */
181   
182   void *instr = ilpBuff->desc.lpData + is;
183   OpenGL_IDirect3DDevice *odev = (OpenGL_IDirect3DDevice *) dev;
184   
185   TRACE(ddraw, "ExecuteData : \n");
186   if (TRACE_ON(ddraw))
187   _dump_executedata(&(ilpBuff->data));
188   
189   while (1) {
190     LPD3DINSTRUCTION current = (LPD3DINSTRUCTION) instr;
191     BYTE size;
192     WORD count;
193
194     count = current->wCount;
195     size = current->bSize;
196     instr += sizeof(D3DINSTRUCTION);
197     
198     switch (current->bOpcode) {
199     case D3DOP_POINT: {
200       TRACE(ddraw, "POINT-s          (%d)\n", count);
201
202       instr += count * size;
203     } break;
204       
205     case D3DOP_LINE: {
206       TRACE(ddraw, "LINE-s           (%d)\n", count);
207
208       instr += count * size;
209     } break;
210       
211     case D3DOP_TRIANGLE: {
212       int i;
213       float z_inv_matrix[16] = {
214         1.0, 0.0,  0.0, 0.0,
215         0.0, 1.0,  0.0, 0.0,
216         0.0, 0.0, -1.0, 0.0,
217         0.0, 0.0,  1.0, 1.0
218       };
219       
220       OGL_Vertex  *vx    = (OGL_Vertex  *) ilpBuff->vertex_data;
221       OGL_LVertex *l_vx  = (OGL_LVertex *) ilpBuff->vertex_data;
222       D3DTLVERTEX *tl_vx = (D3DTLVERTEX *) ilpBuff->vertex_data;
223       
224       TRACE(ddraw, "TRIANGLE         (%d)\n", count);
225
226       switch (ilpBuff->vertex_type) {
227       case D3DVT_VERTEX:
228         /* This time, there is lighting */
229         glEnable(GL_LIGHTING);
230         
231       /* Use given matrixes */
232       glMatrixMode(GL_MODELVIEW);
233       glLoadIdentity(); /* The model transformation was done during the
234                            transformation phase */
235       glMatrixMode(GL_PROJECTION);
236         TRACE(ddraw, "  Projection Matrix : (%p)\n", odev->proj_mat);
237         dump_mat(odev->proj_mat);
238         TRACE(ddraw, "  View       Matrix : (%p)\n", odev->view_mat);
239         dump_mat(odev->view_mat);
240
241         glLoadMatrixf((float *) z_inv_matrix);
242         glMultMatrixf((float *) odev->proj_mat);
243       glMultMatrixf((float *) odev->view_mat);
244         break;
245
246       case D3DVT_LVERTEX:
247         /* No lighting */
248         glDisable(GL_LIGHTING);
249
250         /* Use given matrixes */
251         glMatrixMode(GL_MODELVIEW);
252         glLoadIdentity(); /* The model transformation was done during the
253                              transformation phase */
254         glMatrixMode(GL_PROJECTION);
255
256         TRACE(ddraw, "  Projection Matrix : (%p)\n", odev->proj_mat);
257         dump_mat(odev->proj_mat);
258         TRACE(ddraw, "  View       Matrix : (%p)\n", odev->view_mat);
259         dump_mat(odev->view_mat);
260
261         glLoadMatrixf((float *) z_inv_matrix);
262         glMultMatrixf((float *) odev->proj_mat);
263         glMultMatrixf((float *) odev->view_mat);
264         break;
265
266       case D3DVT_TLVERTEX: {
267         GLdouble height, width, minZ, maxZ;
268         
269         /* First, disable lighting */
270         glDisable(GL_LIGHTING);
271         
272         /* Then do not put any transformation matrixes */
273         glMatrixMode(GL_MODELVIEW);
274         glLoadIdentity();
275         glMatrixMode(GL_PROJECTION);
276         glLoadIdentity();
277         
278         if (ivp == NULL) {
279           ERR(ddraw, "No current viewport !\n");
280           /* Using standard values */
281           height = 640.0;
282           width = 480.0;
283           minZ = -10.0;
284           maxZ = 10.0;
285         } else {
286           height = (GLdouble) ivp->viewport.vp1.dwHeight;
287           width  = (GLdouble) ivp->viewport.vp1.dwWidth;
288           minZ   = (GLdouble) ivp->viewport.vp1.dvMinZ;
289           maxZ   = (GLdouble) ivp->viewport.vp1.dvMaxZ;
290
291           if (minZ == maxZ) {
292             /* I do not know why, but many Dx 3.0 games have minZ = maxZ = 0.0 */
293             minZ = 0.0;
294             maxZ = 1.0;
295         }
296         }
297
298         glOrtho(0.0, width, height, 0.0, -minZ, -maxZ);
299       } break;
300         
301       default:
302         ERR(ddraw, "Unhandled vertex type !\n");
303         break;
304       }
305
306       switch (ilpBuff->vertex_type) {
307       case D3DVT_VERTEX:
308         TRIANGLE_LOOP(DO_VERTEX);
309         break;
310         
311       case D3DVT_LVERTEX:
312         TRIANGLE_LOOP(DO_LVERTEX);
313         break;
314         
315       case D3DVT_TLVERTEX:
316         TRIANGLE_LOOP(DO_TLVERTEX);
317         break;
318         
319       default:
320         ERR(ddraw, "Unhandled vertex type !\n");
321       }
322       
323     } break;
324     
325     case D3DOP_MATRIXLOAD: {
326       TRACE(ddraw, "MATRIXLOAD-s     (%d)\n", count);
327
328       instr += count * size;
329     } break;
330       
331     case D3DOP_MATRIXMULTIPLY: {
332       int i;
333       TRACE(ddraw, "MATRIXMULTIPLY   (%d)\n", count);
334
335       for (i = 0; i < count; i++) {
336         LPD3DMATRIXMULTIPLY ci = (LPD3DMATRIXMULTIPLY) instr;
337         LPD3DMATRIX a = (LPD3DMATRIX) ci->hDestMatrix;
338         LPD3DMATRIX b = (LPD3DMATRIX) ci->hSrcMatrix1;
339         LPD3DMATRIX c = (LPD3DMATRIX) ci->hSrcMatrix2;
340         
341         TRACE(ddraw, "  Dest : %08lx  Src1 : %08lx  Src2 : %08lx\n",
342               ci->hDestMatrix, ci->hSrcMatrix1, ci->hSrcMatrix2);
343
344         /* Do the multiplication..
345            As I am VERY lazy, I let OpenGL do the multiplication for me */
346         glMatrixMode(GL_PROJECTION);
347         /* Save the current matrix */
348         glPushMatrix();
349         /* Load Matrix one and do the multiplication */
350         glLoadMatrixf((float *) c);
351         glMultMatrixf((float *) b);
352         glGetFloatv(GL_PROJECTION_MATRIX, (float *) a);
353         /* Restore the current matrix */
354         glPopMatrix();
355         
356         instr += size;
357       }
358     } break;
359       
360     case D3DOP_STATETRANSFORM: {
361       int i;
362       TRACE(ddraw, "STATETRANSFORM   (%d)\n", count);
363
364       for (i = 0; i < count; i++) {
365         LPD3DSTATE ci = (LPD3DSTATE) instr;
366
367         /* Handle the state transform */
368         switch (ci->t.dtstTransformStateType) {
369         case D3DTRANSFORMSTATE_WORLD: {
370           TRACE(ddraw, "  WORLD (%p)\n", (D3DMATRIX*) ci->v.dwArg[0]);
371           odev->world_mat = (D3DMATRIX*) ci->v.dwArg[0];
372         } break;
373
374         case D3DTRANSFORMSTATE_VIEW: {
375           TRACE(ddraw, "  VIEW (%p)\n", (D3DMATRIX*) ci->v.dwArg[0]);
376           odev->view_mat = (D3DMATRIX*) ci->v.dwArg[0];
377         } break;
378
379         case D3DTRANSFORMSTATE_PROJECTION: {
380           TRACE(ddraw, "  PROJECTION (%p)\n", (D3DMATRIX*) ci->v.dwArg[0]);
381           odev->proj_mat = (D3DMATRIX*) ci->v.dwArg[0];
382         } break;
383
384         default:
385           ERR(ddraw, "  Unhandled state transformation !! (%d)\n", (int) ci->t.dtstTransformStateType);
386           break;
387           
388         }
389         
390         instr += size;
391       }
392     } break;
393       
394     case D3DOP_STATELIGHT: {
395       int i;
396       TRACE(ddraw, "STATELIGHT       (%d)\n", count);
397
398       for (i = 0; i < count; i++) {
399         LPD3DSTATE ci = (LPD3DSTATE) instr;
400         
401         /* Handle the state transform */
402         switch (ci->t.dlstLightStateType) {
403         case D3DLIGHTSTATE_MATERIAL: {
404           IDirect3DMaterial2Impl* mat = (IDirect3DMaterial2Impl*) ci->v.dwArg[0];
405           TRACE(ddraw, "  MATERIAL\n");
406           
407           if (mat != NULL) {
408             mat->activate(mat);
409           } else {
410             TRACE(ddraw, "    bad Material Handle\n");
411           }
412         } break ;
413           
414         case D3DLIGHTSTATE_AMBIENT: {
415           float light[4];
416           DWORD dwLightState = ci->v.dwArg[0];
417           TRACE(ddraw, "  AMBIENT\n");
418           
419           light[0] = ((dwLightState >> 16) & 0xFF) / 255.0;
420           light[1] = ((dwLightState >>  8) & 0xFF) / 255.0;
421           light[2] = ((dwLightState >>  0) & 0xFF) / 255.0;
422           light[3] = 1.0;
423           glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (float *) light);
424
425           TRACE(ddraw, "    R:%02lx G:%02lx B:%02lx A:%02lx\n",
426                 ((dwLightState >> 16) & 0xFF),
427                 ((dwLightState >>  8) & 0xFF),
428                 ((dwLightState >>  0) & 0xFF),
429                 ((dwLightState >> 24) & 0xFF));
430         } break ;
431           
432         case D3DLIGHTSTATE_COLORMODEL: {
433           TRACE(ddraw, "  COLORMODEL\n");
434         } break ;
435           
436         case D3DLIGHTSTATE_FOGMODE: {
437           TRACE(ddraw, "  FOGMODE\n");
438         } break ;
439           
440         case D3DLIGHTSTATE_FOGSTART: {
441           TRACE(ddraw, "  FOGSTART\n");
442         } break ;
443           
444         case D3DLIGHTSTATE_FOGEND: {
445           TRACE(ddraw, "  FOGEND\n");
446         } break ;
447           
448         case D3DLIGHTSTATE_FOGDENSITY: {
449           TRACE(ddraw, "  FOGDENSITY\n");
450         } break ;
451           
452         default:
453           ERR(ddraw, "  Unhandled light state !! (%d)\n", (int) ci->t.dlstLightStateType);
454           break;
455         }
456         instr += size;
457       }
458     } break;
459       
460     case D3DOP_STATERENDER: {
461       int i;
462       TRACE(ddraw, "STATERENDER      (%d)\n", count);
463       
464       for (i = 0; i < count; i++) {
465         LPD3DSTATE ci = (LPD3DSTATE) instr;
466         
467         /* Handle the state transform */
468         set_render_state(ci->t.drstRenderStateType, ci->v.dwArg[0], &(odev->rs));
469
470         instr += size;
471       }
472     } break;
473       
474     case D3DOP_PROCESSVERTICES: {
475       int i;
476       TRACE(ddraw, "PROCESSVERTICES  (%d)\n", count);
477       
478       for (i = 0; i < count; i++) {
479         LPD3DPROCESSVERTICES ci = (LPD3DPROCESSVERTICES) instr;
480         
481         TRACE(ddraw, "  Start : %d Dest : %d Count : %ld\n",
482               ci->wStart, ci->wDest, ci->dwCount);
483         TRACE(ddraw, "  Flags : ");
484         if (TRACE_ON(ddraw)) {
485           if (ci->dwFlags & D3DPROCESSVERTICES_COPY)
486             DUMP("COPY ");
487           if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
488             DUMP("NOCOLOR ");
489           if (ci->dwFlags == D3DPROCESSVERTICES_OPMASK)
490             DUMP("OPMASK ");
491           if (ci->dwFlags & D3DPROCESSVERTICES_TRANSFORM)
492             DUMP("TRANSFORM ");
493           if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT)
494             DUMP("TRANSFORMLIGHT ");
495           if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
496             DUMP("UPDATEEXTENTS ");
497           DUMP("\n");
498         }
499
500         /* This is where doing Direct3D on top on OpenGL is quite difficult.
501            This method transforms a set of vertices using the CURRENT state
502            (lighting, projection, ...) but does not rasterize them.
503            They will oinly be put on screen later (with the POINT / LINE and
504            TRIANGLE op-codes). The problem is that you can have a triangle
505            with each point having been transformed using another state...
506
507            In this implementation, I will emulate only ONE thing : each
508            vertex can have its own "WORLD" transformation (this is used in the
509            TWIST.EXE demo of the 5.2 SDK). I suppose that all vertices of the 
510            execute buffer use the same state.
511
512            If I find applications that change other states, I will try to do a
513            more 'fine-tuned' state emulation (but I may become quite tricky if 
514            it changes a light position in the middle of a triangle).
515            
516            In this case, a 'direct' approach (i.e. without using OpenGL, but
517            writing our own 3D rasterizer) would be easier. */
518
519         /* The current method (with the hypothesis that only the WORLD matrix
520            will change between two points) is like this :
521             - I transform 'manually' all the vertices with the current WORLD
522               matrix and store them in the vertex buffer
523             - during the rasterization phase, the WORLD matrix will be set to
524               the Identity matrix */
525
526         /* Enough for the moment */
527         if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) {
528           int nb;
529           D3DVERTEX  *src = ((LPD3DVERTEX)  (ilpBuff->desc.lpData + vs)) + ci->wStart;
530           OGL_Vertex *dst = ((OGL_Vertex *) (ilpBuff->vertex_data)) + ci->wDest;
531           D3DMATRIX *mat = odev->world_mat;
532
533           TRACE(ddraw, "  World Matrix : (%p)\n", mat);
534           dump_mat(mat);
535
536           ilpBuff->vertex_type = D3DVT_VERTEX;
537           
538           for (nb = 0; nb < ci->dwCount; nb++) {
539             /* For the moment, no normal transformation... */
540             dst->nx = src->nx.nx;
541             dst->ny = src->ny.ny;
542             dst->nz = src->nz.nz;
543             
544             dst->u  = src->u.tu;
545             dst->v  = src->v.tv;
546
547             /* Now, the matrix multiplication */
548             dst->x = (src->x.x * mat->_11) + (src->y.y * mat->_21) + (src->z.z * mat->_31) + (1.0 * mat->_41);
549             dst->y = (src->x.x * mat->_12) + (src->y.y * mat->_22) + (src->z.z * mat->_32) + (1.0 * mat->_42);
550             dst->z = (src->x.x * mat->_13) + (src->y.y * mat->_23) + (src->z.z * mat->_33) + (1.0 * mat->_43);
551             dst->w = (src->x.x * mat->_14) + (src->y.y * mat->_24) + (src->z.z * mat->_34) + (1.0 * mat->_44);
552             
553             src++;
554             dst++;
555           }
556         } else if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORM) {
557           int nb;
558           D3DLVERTEX *src  = ((LPD3DLVERTEX) (ilpBuff->desc.lpData + vs)) + ci->wStart;
559           OGL_LVertex *dst = ((OGL_LVertex *) (ilpBuff->vertex_data)) + ci->wDest;
560           D3DMATRIX *mat = odev->world_mat;
561
562           TRACE(ddraw, "  World Matrix : (%p)\n", mat);
563           dump_mat(mat);
564
565           ilpBuff->vertex_type = D3DVT_LVERTEX;
566           
567           for (nb = 0; nb < ci->dwCount; nb++) {
568             dst->c  = src->c.color;
569             dst->sc = src->s.specular;
570             dst->u  = src->u.tu;
571             dst->v  = src->v.tv;
572
573             /* Now, the matrix multiplication */
574             dst->x = (src->x.x * mat->_11) + (src->y.y * mat->_21) + (src->z.z * mat->_31) + (1.0 * mat->_41);
575             dst->y = (src->x.x * mat->_12) + (src->y.y * mat->_22) + (src->z.z * mat->_32) + (1.0 * mat->_42);
576             dst->z = (src->x.x * mat->_13) + (src->y.y * mat->_23) + (src->z.z * mat->_33) + (1.0 * mat->_43);
577             dst->w = (src->x.x * mat->_14) + (src->y.y * mat->_24) + (src->z.z * mat->_34) + (1.0 * mat->_44);
578             
579             src++;
580             dst++;
581           }
582         } else if (ci->dwFlags == D3DPROCESSVERTICES_COPY) {
583           D3DTLVERTEX *src = ((LPD3DTLVERTEX) (ilpBuff->desc.lpData + vs)) + ci->wStart;
584           D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (ilpBuff->vertex_data)) + ci->wDest;
585
586           ilpBuff->vertex_type = D3DVT_TLVERTEX;
587           
588           memcpy(dst, src, ci->dwCount * sizeof(D3DTLVERTEX));
589         } else {
590           ERR(ddraw, "Unhandled vertex processing !\n");
591         }
592         
593         instr += size;
594       }
595     } break;
596       
597     case D3DOP_TEXTURELOAD: {
598       TRACE(ddraw, "TEXTURELOAD-s    (%d)\n", count);
599
600       instr += count * size;
601     } break;
602       
603     case D3DOP_EXIT: {
604       TRACE(ddraw, "EXIT             (%d)\n", count);
605       /* We did this instruction */
606       instr += size;
607       /* Exit this loop */
608       goto end_of_buffer;
609     } break;
610       
611     case D3DOP_BRANCHFORWARD: {
612       int i;
613       TRACE(ddraw, "BRANCHFORWARD    (%d)\n", count);
614
615       for (i = 0; i < count; i++) {
616         LPD3DBRANCH ci = (LPD3DBRANCH) instr;
617         
618         if ((ilpBuff->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue) {
619           if (!ci->bNegate) {
620             TRACE(ddraw," Should branch to %ld\n", ci->dwOffset);
621           }
622         } else {
623           if (ci->bNegate) {
624             TRACE(ddraw," Should branch to %ld\n", ci->dwOffset);
625           }
626         }
627
628         instr += size;
629       }
630     } break;
631       
632     case D3DOP_SPAN: {
633       TRACE(ddraw, "SPAN-s           (%d)\n", count);
634
635       instr += count * size;
636     } break;
637       
638     case D3DOP_SETSTATUS: {
639       int i;
640       TRACE(ddraw, "SETSTATUS        (%d)\n", count);
641
642       for (i = 0; i < count; i++) {
643         LPD3DSTATUS ci = (LPD3DSTATUS) instr;
644         
645         ilpBuff->data.dsStatus = *ci;
646         
647         instr += size;
648       }
649     } break;
650
651     default:
652       ERR(ddraw, "Unhandled OpCode !!!\n");
653       /* Try to save ... */
654       instr += count * size;
655       break;
656     }
657   }
658
659  end_of_buffer:
660 }
661
662 /*******************************************************************************
663  *                              ExecuteBuffer Creation functions
664  */
665 LPDIRECT3DEXECUTEBUFFER d3dexecutebuffer_create(IDirect3DDeviceImpl* d3ddev, LPD3DEXECUTEBUFFERDESC lpDesc)
666 {
667   IDirect3DExecuteBufferImpl* eb;
668   
669   eb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DExecuteBufferImpl));
670   eb->ref = 1;
671   eb->lpvtbl = &executebuffer_vtable;
672   eb->d3ddev = d3ddev;
673
674   /* Initializes memory */
675   eb->desc = *lpDesc;
676
677   /* No buffer given */
678   if (!(eb->desc.dwFlags & D3DDEB_LPDATA))
679     eb->desc.lpData = NULL;
680
681   /* No buffer size given */
682   if (!(lpDesc->dwFlags & D3DDEB_BUFSIZE))
683     eb->desc.dwBufferSize = 0;
684
685   /* Create buffer if asked */
686   if ((eb->desc.lpData == NULL) && (eb->desc.dwBufferSize > 0)) {
687     eb->need_free = TRUE;
688     eb->desc.lpData = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,eb->desc.dwBufferSize);
689   } else {
690     eb->need_free = FALSE;
691   }
692     
693   /* No vertices for the moment */
694   eb->vertex_data = NULL;
695
696   eb->desc.dwFlags |= D3DDEB_LPDATA;
697
698   eb->execute = execute;
699   
700   return (LPDIRECT3DEXECUTEBUFFER)eb;
701 }
702
703 /*******************************************************************************
704  *                              IDirect3ExecuteBuffer methods
705  */
706
707 static HRESULT WINAPI IDirect3DExecuteBufferImpl_QueryInterface(LPDIRECT3DEXECUTEBUFFER iface,
708                                                             REFIID riid,
709                                                             LPVOID* ppvObj)
710 {
711   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
712   char xrefiid[50];
713   
714   WINE_StringFromCLSID((LPCLSID)riid,xrefiid);
715   FIXME(ddraw, "(%p)->(%s,%p): stub\n", This, xrefiid,ppvObj);
716   
717   return S_OK;
718 }
719
720
721
722 static ULONG WINAPI IDirect3DExecuteBufferImpl_AddRef(LPDIRECT3DEXECUTEBUFFER iface)
723 {
724   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
725   TRACE(ddraw, "(%p)->()incrementing from %lu.\n", This, This->ref );
726   
727   return ++(This->ref);
728 }
729
730
731
732 static ULONG WINAPI IDirect3DExecuteBufferImpl_Release(LPDIRECT3DEXECUTEBUFFER iface)
733 {
734   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
735   FIXME( ddraw, "(%p)->() decrementing from %lu.\n", This, This->ref );
736   
737   if (!--(This->ref)) {
738     if ((This->desc.lpData != NULL) && This->need_free)
739       HeapFree(GetProcessHeap(),0,This->desc.lpData);
740
741     if (This->vertex_data != NULL)
742       HeapFree(GetProcessHeap(),0,This->vertex_data);
743
744     HeapFree(GetProcessHeap(),0,This);
745     return 0;
746   }
747   
748   return This->ref;
749 }
750
751 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Initialize(LPDIRECT3DEXECUTEBUFFER iface,
752                                                         LPDIRECT3DDEVICE lpDirect3DDevice,
753                                                         LPD3DEXECUTEBUFFERDESC lpDesc)
754 {
755   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
756   FIXME(ddraw, "(%p)->(%p,%p): stub\n", This, lpDirect3DDevice, lpDesc);
757   
758   return DD_OK;
759 }
760
761 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Lock(LPDIRECT3DEXECUTEBUFFER iface,
762                                                   LPD3DEXECUTEBUFFERDESC lpDesc)
763 {
764   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
765   TRACE(ddraw, "(%p)->(%p)\n", This, lpDesc);
766
767   /* Copies the buffer description */
768   *lpDesc = This->desc;
769   
770   return DD_OK;
771 }
772
773 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Unlock(LPDIRECT3DEXECUTEBUFFER iface)
774 {
775   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
776   TRACE(ddraw, "(%p)->()\n", This);
777
778   return DD_OK;
779 }
780
781 static HRESULT WINAPI IDirect3DExecuteBufferImpl_SetExecuteData(LPDIRECT3DEXECUTEBUFFER iface,
782                                                             LPD3DEXECUTEDATA lpData)
783 {
784   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
785   DWORD nbvert;
786
787   TRACE(ddraw, "(%p)->(%p)\n", This, lpData);
788
789   This->data = *lpData;
790
791   /* Get the number of vertices in the execute buffer */
792   nbvert = This->data.dwVertexCount;
793     
794   /* Prepares the transformed vertex buffer */
795   if (This->vertex_data != NULL)
796     HeapFree(GetProcessHeap(), 0, This->vertex_data);
797   This->vertex_data = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,nbvert * sizeof(OGL_Vertex));
798
799
800   if (TRACE_ON(ddraw)) {
801     _dump_executedata(lpData);
802   }
803   
804   return DD_OK;
805 }
806
807 static HRESULT WINAPI IDirect3DExecuteBufferImpl_GetExecuteData(LPDIRECT3DEXECUTEBUFFER iface,
808                                                             LPD3DEXECUTEDATA lpData)
809 {
810   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
811   TRACE(ddraw, "(%p)->(%p): stub\n", This, lpData);
812
813   *lpData = This->data;
814   
815   return DD_OK;
816 }
817
818 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Validate(LPDIRECT3DEXECUTEBUFFER iface,
819                                                       LPDWORD lpdwOffset,
820                                                       LPD3DVALIDATECALLBACK lpFunc,
821                                                       LPVOID lpUserArg,
822                                                       DWORD dwReserved)
823 {
824   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
825   TRACE(ddraw, "(%p)->(%p,%p,%p,%lu)\n", This, lpdwOffset, lpFunc, lpUserArg, dwReserved);
826
827   return DD_OK;
828 }
829
830 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Optimize(LPDIRECT3DEXECUTEBUFFER iface,
831                                                       DWORD dwReserved)
832 {
833   ICOM_THIS(IDirect3DExecuteBufferImpl,iface);
834   TRACE(ddraw, "(%p)->(%lu)\n", This, dwReserved);
835
836   return DD_OK;
837 }
838
839
840 /*******************************************************************************
841  *                              IDirect3DLight VTable
842  */
843 static ICOM_VTABLE(IDirect3DExecuteBuffer) executebuffer_vtable = {
844   /*** IUnknown methods ***/
845   IDirect3DExecuteBufferImpl_QueryInterface,
846   IDirect3DExecuteBufferImpl_AddRef,
847   IDirect3DExecuteBufferImpl_Release,
848   /*** IDirect3DExecuteBuffer methods ***/
849   IDirect3DExecuteBufferImpl_Initialize,
850   IDirect3DExecuteBufferImpl_Lock,
851   IDirect3DExecuteBufferImpl_Unlock,
852   IDirect3DExecuteBufferImpl_SetExecuteData,
853   IDirect3DExecuteBufferImpl_GetExecuteData,
854   IDirect3DExecuteBufferImpl_Validate,
855   IDirect3DExecuteBufferImpl_Optimize
856 };
857
858 #endif /* HAVE_MESAGL */