ddraw: Materials in the execute buffer are handles.
[wine] / dlls / ddraw / executebuffer.c
1 /* Direct3D ExecuteBuffer
2  * Copyright (c) 1998-2004 Lionel ULMER
3  * Copyright (c) 2002-2004 Christian Costa
4  * Copyright (c) 2006      Stefan Dösinger
5  *
6  * This file contains the implementation of IDirect3DExecuteBuffer.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #define COBJMACROS
32 #define NONAMELESSUNION
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "wine/exception.h"
39
40 #include "ddraw.h"
41 #include "d3d.h"
42
43 #include "ddraw_private.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
47
48 /*****************************************************************************
49  * _dump_executedata
50  * _dump_D3DEXECUTEBUFFERDESC
51  *
52  * Debug functions which write the executebuffer data to the console
53  *
54  *****************************************************************************/
55
56 static void _dump_executedata(const D3DEXECUTEDATA *lpData) {
57     DPRINTF("dwSize : %d\n", lpData->dwSize);
58     DPRINTF("Vertex      Offset : %d  Count  : %d\n", lpData->dwVertexOffset, lpData->dwVertexCount);
59     DPRINTF("Instruction Offset : %d  Length : %d\n", lpData->dwInstructionOffset, lpData->dwInstructionLength);
60     DPRINTF("HVertex     Offset : %d\n", lpData->dwHVertexOffset);
61 }
62
63 static void _dump_D3DEXECUTEBUFFERDESC(const D3DEXECUTEBUFFERDESC *lpDesc) {
64     DPRINTF("dwSize       : %d\n", lpDesc->dwSize);
65     DPRINTF("dwFlags      : %x\n", lpDesc->dwFlags);
66     DPRINTF("dwCaps       : %x\n", lpDesc->dwCaps);
67     DPRINTF("dwBufferSize : %d\n", lpDesc->dwBufferSize);
68     DPRINTF("lpData       : %p\n", lpDesc->lpData);
69 }
70
71 /*****************************************************************************
72  * IDirect3DExecuteBufferImpl_Execute
73  *
74  * The main functionality of the execute buffer
75  * It transforms the vertices if necessary, and calls IDirect3DDevice7
76  * for drawing the vertices. It is called from
77  * IDirect3DDevice::Execute
78  *
79  * TODO: Perhaps some comments about the various opcodes wouldn't hurt
80  *
81  * Don't declare this static, as it's called from device.c,
82  * IDirect3DDevice::Execute
83  *
84  * Params:
85  *  Device: 3D Device associated to use for drawing
86  *  Viewport: Viewport for this operation
87  *
88  *****************************************************************************/
89 void
90 IDirect3DExecuteBufferImpl_Execute(IDirect3DExecuteBufferImpl *This,
91                                    IDirect3DDeviceImpl *lpDevice,
92                                    IDirect3DViewportImpl *lpViewport)
93 {
94     /* DWORD bs = This->desc.dwBufferSize; */
95     DWORD vs = This->data.dwVertexOffset;
96     /* DWORD vc = This->data.dwVertexCount; */
97     DWORD is = This->data.dwInstructionOffset;
98     /* DWORD il = This->data.dwInstructionLength; */
99
100     char *instr = (char *)This->desc.lpData + is;
101
102     /* Should check if the viewport was added or not to the device */
103
104     /* Activate the viewport */
105     lpViewport->active_device = lpDevice;
106     lpViewport->activate(lpViewport);
107
108     TRACE("ExecuteData :\n");
109     if (TRACE_ON(d3d7))
110       _dump_executedata(&(This->data));
111
112     while (1) {
113         LPD3DINSTRUCTION current = (LPD3DINSTRUCTION) instr;
114         BYTE size;
115         WORD count;
116         
117         count = current->wCount;
118         size = current->bSize;
119         instr += sizeof(D3DINSTRUCTION);
120         
121         switch (current->bOpcode) {
122             case D3DOP_POINT: {
123                 WARN("POINT-s          (%d)\n", count);
124                 instr += count * size;
125             } break;
126
127             case D3DOP_LINE: {
128                 WARN("LINE-s           (%d)\n", count);
129                 instr += count * size;
130             } break;
131
132             case D3DOP_TRIANGLE: {
133                 int i;
134                 D3DTLVERTEX *tl_vx = (D3DTLVERTEX *) This->vertex_data;
135                 TRACE("TRIANGLE         (%d)\n", count);
136                 
137                 if (count*3>This->nb_indices) {
138                     This->nb_indices = count * 3;
139                     HeapFree(GetProcessHeap(),0,This->indices);
140                     This->indices = HeapAlloc(GetProcessHeap(),0,sizeof(WORD)*This->nb_indices);
141                 }
142                         
143                 for (i = 0; i < count; i++) {
144                     LPD3DTRIANGLE ci = (LPD3DTRIANGLE) instr;
145                     TRACE_(d3d7)("  v1: %d  v2: %d  v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3);
146                     TRACE_(d3d7)("  Flags : ");
147                     if (TRACE_ON(d3d7)) {
148                         /* Wireframe */
149                         if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
150                             TRACE_(d3d7)("EDGEENABLE1 ");
151                         if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2)
152                             TRACE_(d3d7)("EDGEENABLE2 ");
153                         if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
154                             TRACE_(d3d7)("EDGEENABLE3 ");
155                         /* Strips / Fans */
156                         if (ci->wFlags == D3DTRIFLAG_EVEN)
157                             TRACE_(d3d7)("EVEN ");
158                         if (ci->wFlags == D3DTRIFLAG_ODD)
159                             TRACE_(d3d7)("ODD ");
160                         if (ci->wFlags == D3DTRIFLAG_START)
161                             TRACE_(d3d7)("START ");
162                         if ((ci->wFlags > 0) && (ci->wFlags < 30))
163                             TRACE_(d3d7)("STARTFLAT(%d) ", ci->wFlags);
164                         TRACE_(d3d7)("\n");
165                     }
166                     This->indices[(i * 3)    ] = ci->u1.v1;
167                     This->indices[(i * 3) + 1] = ci->u2.v2;
168                     This->indices[(i * 3) + 2] = ci->u3.v3;
169                     instr += size;
170                 }
171                 /* IDirect3DDevices have color keying always enabled -
172                  * enable it before drawing. This overwrites any ALPHA*
173                  * render state
174                  */
175                 IWineD3DDevice_SetRenderState(lpDevice->wineD3DDevice,
176                                               WINED3DRS_COLORKEYENABLE,
177                                               1);
178                 IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(lpDevice,IDirect3DDevice7),
179                                                       D3DPT_TRIANGLELIST,D3DFVF_TLVERTEX,tl_vx,0,This->indices,count*3,0);
180             } break;
181
182             case D3DOP_MATRIXLOAD:
183                 WARN("MATRIXLOAD-s     (%d)\n", count);
184                 instr += count * size;
185                 break;
186
187             case D3DOP_MATRIXMULTIPLY: {
188                 int i;
189                 TRACE("MATRIXMULTIPLY   (%d)\n", count);
190                 
191                 for (i = 0; i < count; i++) {
192                     LPD3DMATRIXMULTIPLY ci = (LPD3DMATRIXMULTIPLY) instr;
193                     LPD3DMATRIX a, b, c;
194
195                     if(!ci->hDestMatrix || ci->hDestMatrix > lpDevice->numHandles ||
196                        !ci->hSrcMatrix1 || ci->hSrcMatrix1 > lpDevice->numHandles ||
197                        !ci->hSrcMatrix2 || ci->hSrcMatrix2 > lpDevice->numHandles) {
198                         ERR("Handles out of bounds\n");
199                     } else if (lpDevice->Handles[ci->hDestMatrix - 1].type != DDrawHandle_Matrix ||
200                                lpDevice->Handles[ci->hSrcMatrix1 - 1].type != DDrawHandle_Matrix ||
201                                lpDevice->Handles[ci->hSrcMatrix2 - 1].type != DDrawHandle_Matrix) {
202                         ERR("Handle types invalid\n");
203                     } else {
204                         a = (LPD3DMATRIX) lpDevice->Handles[ci->hDestMatrix - 1].ptr;
205                         b = (LPD3DMATRIX) lpDevice->Handles[ci->hSrcMatrix1 - 1].ptr;
206                         c = (LPD3DMATRIX) lpDevice->Handles[ci->hSrcMatrix2 - 1].ptr;
207                         TRACE("  Dest : %p  Src1 : %p  Src2 : %p\n",
208                             a, b, c);
209                         multiply_matrix(a,c,b);
210                     }
211
212                     instr += size;
213                 }
214             } break;
215
216             case D3DOP_STATETRANSFORM: {
217                 int i;
218                 TRACE("STATETRANSFORM   (%d)\n", count);
219                 
220                 for (i = 0; i < count; i++) {
221                     LPD3DSTATE ci = (LPD3DSTATE) instr;
222
223                     if(!ci->u2.dwArg[0]) {
224                         ERR("Setting a NULL matrix handle, what should I do?\n");
225                     } else if(ci->u2.dwArg[0] > lpDevice->numHandles) {
226                         ERR("Handle %d is out of bounds\n", ci->u2.dwArg[0]);
227                     } else if(lpDevice->Handles[ci->u2.dwArg[0] - 1].type != DDrawHandle_Matrix) {
228                         ERR("Handle %d is not a matrix handle\n", ci->u2.dwArg[0]);
229                     } else {
230                         IDirect3DDevice7_SetTransform(ICOM_INTERFACE(lpDevice, IDirect3DDevice7),
231                                                       ci->u1.drstRenderStateType, (LPD3DMATRIX) lpDevice->Handles[ci->u2.dwArg[0] - 1].ptr);
232                     }
233                     instr += size;
234                 }
235             } break;
236
237             case D3DOP_STATELIGHT: {
238                 int i;
239                 TRACE("STATELIGHT       (%d)\n", count);
240                 
241                 for (i = 0; i < count; i++) {
242                     LPD3DSTATE ci = (LPD3DSTATE) instr;
243
244                     TRACE("(%08x,%08x)\n",ci->u1.dlstLightStateType, ci->u2.dwArg[0]);
245
246                     if (!ci->u1.dlstLightStateType && (ci->u1.dlstLightStateType > D3DLIGHTSTATE_COLORVERTEX))
247                         ERR("Unexpected Light State Type\n");
248                     else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */) {
249             DWORD matHandle = ci->u2.dwArg[0];
250
251             if(!matHandle) {
252                 FIXME(" D3DLIGHTSTATE_MATERIAL called with NULL material !!!\n");
253             } else if(matHandle >= lpDevice->numHandles) {
254                 WARN("Material handle %d is invalid\n", matHandle);
255             } else if(lpDevice->Handles[matHandle - 1].type != DDrawHandle_Material) {
256                 WARN("Handle %d is not a material handle\n", matHandle);
257             } else {
258                 IDirect3DMaterialImpl *mat = (IDirect3DMaterialImpl *) lpDevice->Handles[matHandle - 1].ptr;
259                 mat->activate(mat);
260                     }
261             }
262                    else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */) {
263                         switch (ci->u2.dwArg[0]) {
264                             case D3DCOLOR_MONO:
265                                ERR("DDCOLOR_MONO should not happen!\n");
266                                break;
267                             case D3DCOLOR_RGB:
268                                /* We are already in this mode */
269                                break;
270                             default:
271                                ERR("Unknown color model!\n");
272                         }
273                     } else {
274                         D3DRENDERSTATETYPE rs = 0;
275                         switch (ci->u1.dlstLightStateType) {
276
277                             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
278                                 rs = D3DRENDERSTATE_AMBIENT;
279                                 break;          
280                             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
281                                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
282                                 break;
283                             case D3DLIGHTSTATE_FOGSTART:      /* 5 */
284                                 rs = D3DRENDERSTATE_FOGSTART;
285                                 break;
286                             case D3DLIGHTSTATE_FOGEND:        /* 6 */
287                                 rs = D3DRENDERSTATE_FOGEND;
288                                 break;
289                             case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
290                                 rs = D3DRENDERSTATE_FOGDENSITY;
291                                 break;
292                             case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
293                                 rs = D3DRENDERSTATE_COLORVERTEX;
294                                 break;
295                             default:
296                                 break;
297                         }
298                         
299                         IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(lpDevice, IDirect3DDevice7),
300                                                         rs,ci->u2.dwArg[0]);
301                    }
302
303                    instr += size;
304                 }
305             } break;
306
307             case D3DOP_STATERENDER: {
308                 int i;
309                 TRACE("STATERENDER      (%d)\n", count);
310
311                 for (i = 0; i < count; i++) {
312                     LPD3DSTATE ci = (LPD3DSTATE) instr;
313                     
314                     IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(lpDevice, IDirect3DDevice7),
315                                                     ci->u1.drstRenderStateType, ci->u2.dwArg[0]);
316
317                     instr += size;
318                 }
319             } break;
320
321             case D3DOP_PROCESSVERTICES:
322             {
323                 /* TODO: Share code with IDirect3DVertexBuffer::ProcessVertices and / or
324                  * IWineD3DDevice::ProcessVertices
325                  */
326                 int i;
327                 D3DMATRIX view_mat, world_mat, proj_mat;
328                 TRACE("PROCESSVERTICES  (%d)\n", count);
329
330                 /* Get the transform and world matrix */
331                 /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
332
333                 IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
334                                             D3DTRANSFORMSTATE_VIEW,
335                                             (WINED3DMATRIX*) &view_mat);
336
337                 IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
338                                             D3DTRANSFORMSTATE_PROJECTION,
339                                             (WINED3DMATRIX*) &proj_mat);
340
341                 IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
342                                             D3DTRANSFORMSTATE_WORLD,
343                                             (WINED3DMATRIX*) &world_mat);
344
345                 for (i = 0; i < count; i++) {
346                     LPD3DPROCESSVERTICES ci = (LPD3DPROCESSVERTICES) instr;
347
348                     TRACE("  Start : %d Dest : %d Count : %d\n",
349                           ci->wStart, ci->wDest, ci->dwCount);
350                     TRACE("  Flags : ");
351                     if (TRACE_ON(d3d7)) {
352                         if (ci->dwFlags & D3DPROCESSVERTICES_COPY)
353                             TRACE("COPY ");
354                         if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
355                             TRACE("NOCOLOR ");
356                         if (ci->dwFlags == D3DPROCESSVERTICES_OPMASK)
357                             TRACE("OPMASK ");
358                         if (ci->dwFlags & D3DPROCESSVERTICES_TRANSFORM)
359                             TRACE("TRANSFORM ");
360                         if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT)
361                             TRACE("TRANSFORMLIGHT ");
362                         if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
363                             TRACE("UPDATEEXTENTS ");
364                         TRACE("\n");
365                     }
366                     
367                     /* This is where doing Direct3D on top on OpenGL is quite difficult.
368                        This method transforms a set of vertices using the CURRENT state
369                        (lighting, projection, ...) but does not rasterize them.
370                        They will only be put on screen later (with the POINT / LINE and
371                        TRIANGLE op-codes). The problem is that you can have a triangle
372                        with each point having been transformed using another state...
373                        
374                        In this implementation, I will emulate only ONE thing : each
375                        vertex can have its own "WORLD" transformation (this is used in the
376                        TWIST.EXE demo of the 5.2 SDK). I suppose that all vertices of the
377                        execute buffer use the same state.
378                        
379                        If I find applications that change other states, I will try to do a
380                        more 'fine-tuned' state emulation (but I may become quite tricky if
381                        it changes a light position in the middle of a triangle).
382                        
383                        In this case, a 'direct' approach (i.e. without using OpenGL, but
384                        writing our own 3D rasterizer) would be easier. */
385                     
386                     /* The current method (with the hypothesis that only the WORLD matrix
387                        will change between two points) is like this :
388                        - I transform 'manually' all the vertices with the current WORLD
389                          matrix and store them in the vertex buffer
390                        - during the rasterization phase, the WORLD matrix will be set to
391                          the Identity matrix */
392                     
393                     /* Enough for the moment */
394                     if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) {
395                         unsigned int nb;
396                         D3DVERTEX  *src = ((LPD3DVERTEX)  ((char *)This->desc.lpData + vs)) + ci->wStart;
397                         D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
398                         D3DMATRIX *mat2 = &world_mat;
399                         D3DMATRIX mat;
400                         D3DVALUE nx,ny,nz;
401                         D3DVIEWPORT* Viewport = &lpViewport->viewports.vp1;
402                         
403                         if (TRACE_ON(d3d7)) {
404                             TRACE("  Projection Matrix : (%p)\n", &proj_mat);
405                             dump_D3DMATRIX(&proj_mat);
406                             TRACE("  View       Matrix : (%p)\n", &view_mat);
407                             dump_D3DMATRIX(&view_mat);
408                             TRACE("  World Matrix : (%p)\n", &world_mat);
409                             dump_D3DMATRIX(&world_mat);
410                         }
411
412                         multiply_matrix(&mat,&view_mat,&world_mat);
413                         multiply_matrix(&mat,&proj_mat,&mat);
414
415                         for (nb = 0; nb < ci->dwCount; nb++) {
416                             /* Normals transformation */
417                             nx = (src->u4.nx * mat2->_11) + (src->u5.ny * mat2->_21) + (src->u6.nz * mat2->_31);
418                             ny = (src->u4.nx * mat2->_12) + (src->u5.ny * mat2->_22) + (src->u6.nz * mat2->_32);
419                             nz = (src->u4.nx * mat2->_13) + (src->u5.ny * mat2->_23) + (src->u6.nz * mat2->_33);
420                             
421                             /* No lighting yet */
422                             dst->u5.color = 0xFFFFFFFF; /* Opaque white */
423                             dst->u6.specular = 0xFF000000; /* No specular and no fog factor */
424                             
425                             dst->u7.tu  = src->u7.tu;
426                             dst->u8.tv  = src->u8.tv;
427                             
428                             /* Now, the matrix multiplication */
429                             dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
430                             dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
431                             dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
432                             dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);
433
434                             dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dwWidth / 2
435                                        + Viewport->dwX + Viewport->dwWidth / 2;
436                             dst->u2.sy = dst->u2.sy / dst->u4.rhw * Viewport->dwHeight / 2
437                                        + Viewport->dwY + Viewport->dwHeight / 2;
438                             dst->u3.sz /= dst->u4.rhw;
439                             dst->u4.rhw = 1 / dst->u4.rhw;
440
441                             src++;
442                             dst++;
443                             
444                         }
445                     } else if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORM) {
446                         unsigned int nb;
447                         D3DLVERTEX *src  = ((LPD3DLVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
448                         D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
449                         D3DMATRIX mat;
450                         D3DVIEWPORT* Viewport = &lpViewport->viewports.vp1;
451                         
452                         if (TRACE_ON(d3d7)) {
453                             TRACE("  Projection Matrix : (%p)\n", &proj_mat);
454                             dump_D3DMATRIX(&proj_mat);
455                             TRACE("  View       Matrix : (%p)\n",&view_mat);
456                             dump_D3DMATRIX(&view_mat);
457                             TRACE("  World Matrix : (%p)\n", &mat);
458                             dump_D3DMATRIX(&mat);
459                         }
460
461                         multiply_matrix(&mat,&view_mat,&world_mat);
462                         multiply_matrix(&mat,&proj_mat,&mat);
463
464                         for (nb = 0; nb < ci->dwCount; nb++) {
465                             dst->u5.color = src->u4.color;
466                             dst->u6.specular = src->u5.specular;
467                             dst->u7.tu = src->u6.tu;
468                             dst->u8.tv = src->u7.tv;
469                             
470                             /* Now, the matrix multiplication */
471                             dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
472                             dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
473                             dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
474                             dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);
475
476                             dst->u1.sx /= dst->u4.rhw * Viewport->dvScaleX * Viewport->dwWidth / 2 + Viewport->dwX;
477                             dst->u2.sy /= dst->u4.rhw * Viewport->dvScaleY * Viewport->dwHeight / 2 + Viewport->dwY;
478                             dst->u3.sz /= dst->u4.rhw;
479                             dst->u4.rhw = 1 / dst->u4.rhw;
480
481                             src++;
482                             dst++;
483                         }
484                     } else if (ci->dwFlags == D3DPROCESSVERTICES_COPY) {
485                         D3DTLVERTEX *src = ((LPD3DTLVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
486                         D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
487                         
488                         memcpy(dst, src, ci->dwCount * sizeof(D3DTLVERTEX));
489                     } else {
490                         ERR("Unhandled vertex processing !\n");
491                     }
492
493                     instr += size;
494                 }
495             } break;
496
497             case D3DOP_TEXTURELOAD: {
498                 WARN("TEXTURELOAD-s    (%d)\n", count);
499
500                 instr += count * size;
501             } break;
502
503             case D3DOP_EXIT: {
504                 TRACE("EXIT             (%d)\n", count);
505                 /* We did this instruction */
506                 instr += size;
507                 /* Exit this loop */
508                 goto end_of_buffer;
509             } break;
510
511             case D3DOP_BRANCHFORWARD: {
512                 int i;
513                 TRACE("BRANCHFORWARD    (%d)\n", count);
514
515                 for (i = 0; i < count; i++) {
516                     LPD3DBRANCH ci = (LPD3DBRANCH) instr;
517
518                     if ((This->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue) {
519                         if (!ci->bNegate) {
520                             TRACE(" Branch to %d\n", ci->dwOffset);
521                             instr = (char*)current + ci->dwOffset;
522                             break;
523                         }
524                     } else {
525                         if (ci->bNegate) {
526                             TRACE(" Branch to %d\n", ci->dwOffset);
527                             instr = (char*)current + ci->dwOffset;
528                             break;
529                         }
530                     }
531
532                     instr += size;
533                 }
534             } break;
535
536             case D3DOP_SPAN: {
537                 WARN("SPAN-s           (%d)\n", count);
538
539                 instr += count * size;
540             } break;
541
542             case D3DOP_SETSTATUS: {
543                 int i;
544                 TRACE("SETSTATUS        (%d)\n", count);
545
546                 for (i = 0; i < count; i++) {
547                     LPD3DSTATUS ci = (LPD3DSTATUS) instr;
548                     
549                     This->data.dsStatus = *ci;
550
551                     instr += size;
552                 }
553             } break;
554
555             default:
556                 ERR("Unhandled OpCode %d !!!\n",current->bOpcode);
557                 /* Try to save ... */
558                 instr += count * size;
559                 break;
560         }
561     }
562
563 end_of_buffer:
564     ;
565 }
566
567 /*****************************************************************************
568  * IDirect3DExecuteBuffer::QueryInterface
569  *
570  * Well, a usual QueryInterface function. Don't know fur sure which
571  * interfaces it can Query.
572  *
573  * Params:
574  *  riid: The interface ID queried for
575  *  obj: Address to return the interface pointer at
576  *
577  * Returns:
578  *  D3D_OK in case of a success (S_OK? Think it's the same)
579  *  OLE_E_ENUM_NOMORE if the interface wasn't found.
580  *   (E_NOINTERFACE?? Don't know what I really need)
581  *
582  *****************************************************************************/
583 static HRESULT WINAPI
584 IDirect3DExecuteBufferImpl_QueryInterface(IDirect3DExecuteBuffer *iface,
585                                           REFIID riid,
586                                           void **obj)
587 {
588     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
589     TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_guid(riid), obj);
590
591     *obj = NULL;
592
593     if ( IsEqualGUID( &IID_IUnknown,  riid ) ) {
594         IDirect3DExecuteBuffer_AddRef(ICOM_INTERFACE(This, IDirect3DExecuteBuffer));
595         *obj = iface;
596         TRACE("  Creating IUnknown interface at %p.\n", *obj);
597         return S_OK;
598     }
599     if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
600         IDirect3DExecuteBuffer_AddRef(ICOM_INTERFACE(This, IDirect3DExecuteBuffer));
601         *obj = ICOM_INTERFACE(This, IDirect3DExecuteBuffer);
602         TRACE("  Creating IDirect3DExecuteBuffer interface %p\n", *obj);
603         return S_OK;
604     }
605     FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
606     return E_NOINTERFACE;
607 }
608
609
610 /*****************************************************************************
611  * IDirect3DExecuteBuffer::AddRef
612  *
613  * A normal AddRef method, nothing special
614  *
615  * Returns:
616  *  The new refcount
617  *
618  *****************************************************************************/
619 static ULONG WINAPI
620 IDirect3DExecuteBufferImpl_AddRef(IDirect3DExecuteBuffer *iface)
621 {
622     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
623     ULONG ref = InterlockedIncrement(&This->ref);
624
625     FIXME("(%p)->()incrementing from %u.\n", This, ref - 1);
626
627     return ref;
628 }
629
630 /*****************************************************************************
631  * IDirect3DExecuteBuffer::Release
632  *
633  * A normal Release method, nothing special
634  *
635  * Returns:
636  *  The new refcount
637  *
638  *****************************************************************************/
639 static ULONG WINAPI
640 IDirect3DExecuteBufferImpl_Release(IDirect3DExecuteBuffer *iface)
641 {
642     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
643     ULONG ref = InterlockedDecrement(&This->ref);
644
645     TRACE("(%p)->()decrementing from %u.\n", This, ref + 1);
646
647     if (!ref) {
648         if (This->need_free)
649             HeapFree(GetProcessHeap(),0,This->desc.lpData);
650         HeapFree(GetProcessHeap(),0,This->vertex_data);
651         HeapFree(GetProcessHeap(),0,This->indices);
652         HeapFree(GetProcessHeap(),0,This);
653         return 0;
654     }
655
656     return ref;
657 }
658
659 /*****************************************************************************
660  * IDirect3DExecuteBuffer::Initialize
661  *
662  * Initializes the Execute Buffer. This method exists for COM compliance
663  * Nothing to do here.
664  *
665  * Returns:
666  *  D3D_OK
667  *
668  *****************************************************************************/
669 static HRESULT WINAPI
670 IDirect3DExecuteBufferImpl_Initialize(IDirect3DExecuteBuffer *iface,
671                                         IDirect3DDevice *lpDirect3DDevice,
672                                         D3DEXECUTEBUFFERDESC *lpDesc)
673 {
674     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
675     TRACE("(%p)->(%p,%p) no-op....\n", This, lpDirect3DDevice, lpDesc);
676     return D3D_OK;
677 }
678
679 /*****************************************************************************
680  * IDirect3DExecuteBuffer::Lock
681  *
682  * Locks the buffer, so the app can write into it.
683  *
684  * Params:
685  *  Desc: Pointer to return the buffer description. This Description contains
686  *        a pointer to the buffer data.
687  *
688  * Returns:
689  *  This implementation always returns D3D_OK
690  *
691  *****************************************************************************/
692 static HRESULT WINAPI
693 IDirect3DExecuteBufferImpl_Lock(IDirect3DExecuteBuffer *iface,
694                                 D3DEXECUTEBUFFERDESC *lpDesc)
695 {
696     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
697     DWORD dwSize;
698     TRACE("(%p)->(%p)\n", This, lpDesc);
699
700     dwSize = lpDesc->dwSize;
701     memset(lpDesc, 0, dwSize);
702     memcpy(lpDesc, &This->desc, dwSize);
703     
704     if (TRACE_ON(d3d7)) {
705         TRACE("  Returning description :\n");
706         _dump_D3DEXECUTEBUFFERDESC(lpDesc);
707     }
708     return D3D_OK;
709 }
710
711 /*****************************************************************************
712  * IDirect3DExecuteBuffer::Unlock
713  *
714  * Unlocks the buffer. We don't have anything to do here
715  *
716  * Returns:
717  *  This implementation always returns D3D_OK
718  *
719  *****************************************************************************/
720 static HRESULT WINAPI
721 IDirect3DExecuteBufferImpl_Unlock(IDirect3DExecuteBuffer *iface)
722 {
723     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
724     TRACE("(%p)->() no-op...\n", This);
725     return D3D_OK;
726 }
727
728 /*****************************************************************************
729  * IDirect3DExecuteBuffer::SetExecuteData
730  *
731  * Sets the execute data. This data is used to describe the buffer's content
732  *
733  * Params:
734  *  Data: Pointer to a D3DEXECUTEDATA structure containing the data to
735  *  assign
736  *
737  * Returns:
738  *  D3D_OK on success
739  *  DDERR_OUTOFMEMORY if the vertex buffer allocation failed
740  *
741  *****************************************************************************/
742 static HRESULT WINAPI
743 IDirect3DExecuteBufferImpl_SetExecuteData(IDirect3DExecuteBuffer *iface,
744                                           D3DEXECUTEDATA *lpData)
745 {
746     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
747     DWORD nbvert;
748     TRACE("(%p)->(%p)\n", This, lpData);
749
750     memcpy(&This->data, lpData, lpData->dwSize);
751
752     /* Get the number of vertices in the execute buffer */
753     nbvert = This->data.dwVertexCount;
754     
755     /* Prepares the transformed vertex buffer */
756     HeapFree(GetProcessHeap(), 0, This->vertex_data);
757     This->vertex_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbvert * sizeof(D3DTLVERTEX));
758
759     if (TRACE_ON(d3d7)) {
760         _dump_executedata(lpData);
761     }
762
763     return D3D_OK;
764 }
765
766 /*****************************************************************************
767  * IDirect3DExecuteBuffer::GetExecuteData
768  *
769  * Returns the data in the execute buffer
770  *
771  * Params:
772  *  Data: Pointer to a D3DEXECUTEDATA structure used to return data
773  *
774  * Returns:
775  *  D3D_OK on success
776  *
777  *****************************************************************************/
778 static HRESULT WINAPI
779 IDirect3DExecuteBufferImpl_GetExecuteData(IDirect3DExecuteBuffer *iface,
780                                           D3DEXECUTEDATA *lpData)
781 {
782     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
783     DWORD dwSize;
784     TRACE("(%p)->(%p): stub!\n", This, lpData);
785
786     dwSize = lpData->dwSize;
787     memset(lpData, 0, dwSize);
788     memcpy(lpData, &This->data, dwSize);
789
790     if (TRACE_ON(d3d7)) {
791         TRACE("Returning data :\n");
792         _dump_executedata(lpData);
793     }
794
795     return DD_OK;
796 }
797
798 /*****************************************************************************
799  * IDirect3DExecuteBuffer::Validate
800  *
801  * DirectX 5 SDK: "The IDirect3DExecuteBuffer::Validate method is not
802  * currently implemented"
803  *
804  * Params:
805  *  ?
806  *
807  * Returns:
808  *  DDERR_UNSUPPORTED, because it's not implemented in Windows.
809  *
810  *****************************************************************************/
811 static HRESULT WINAPI
812 IDirect3DExecuteBufferImpl_Validate(IDirect3DExecuteBuffer *iface,
813                                     DWORD *Offset,
814                                     LPD3DVALIDATECALLBACK Func,
815                                     void *UserArg,
816                                     DWORD Reserved)
817 {
818     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
819     TRACE("(%p)->(%p,%p,%p,%08x): Unimplemented!\n", This, Offset, Func, UserArg, Reserved);
820     return DDERR_UNSUPPORTED; /* Unchecked */
821 }
822
823 /*****************************************************************************
824  * IDirect3DExecuteBuffer::Optimize
825  *
826  * DirectX5 SDK: "The IDirect3DExecuteBuffer::Optimize method is not
827  * currently supported"
828  *
829  * Params:
830  *  Dummy: Seems to be an unused dummy ;)
831  *
832  * Returns:
833  *  DDERR_UNSUPPORTED, because it's not implemented in Windows.
834  *
835  *****************************************************************************/
836 static HRESULT WINAPI
837 IDirect3DExecuteBufferImpl_Optimize(IDirect3DExecuteBuffer *iface,
838                                     DWORD Dummy)
839 {
840     ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
841     TRACE("(%p)->(%08x): Unimplemented\n", This, Dummy);
842     return DDERR_UNSUPPORTED; /* Unchecked */
843 }
844
845 const IDirect3DExecuteBufferVtbl IDirect3DExecuteBuffer_Vtbl =
846 {
847     IDirect3DExecuteBufferImpl_QueryInterface,
848     IDirect3DExecuteBufferImpl_AddRef,
849     IDirect3DExecuteBufferImpl_Release,
850     IDirect3DExecuteBufferImpl_Initialize,
851     IDirect3DExecuteBufferImpl_Lock,
852     IDirect3DExecuteBufferImpl_Unlock,
853     IDirect3DExecuteBufferImpl_SetExecuteData,
854     IDirect3DExecuteBufferImpl_GetExecuteData,
855     IDirect3DExecuteBufferImpl_Validate,
856     IDirect3DExecuteBufferImpl_Optimize,
857 };