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