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