ddraw: Use a less offensive handle table implementation for surfaces.
[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     TRACE("dwSize : %d\n", lpData->dwSize);
58     TRACE("Vertex      Offset : %d  Count  : %d\n", lpData->dwVertexOffset, lpData->dwVertexCount);
59     TRACE("Instruction Offset : %d  Length : %d\n", lpData->dwInstructionOffset, lpData->dwInstructionLength);
60     TRACE("HVertex     Offset : %d\n", lpData->dwHVertexOffset);
61 }
62
63 static void _dump_D3DEXECUTEBUFFERDESC(const D3DEXECUTEBUFFERDESC *lpDesc) {
64     TRACE("dwSize       : %d\n", lpDesc->dwSize);
65     TRACE("dwFlags      : %x\n", lpDesc->dwFlags);
66     TRACE("dwCaps       : %x\n", lpDesc->dwCaps);
67     TRACE("dwBufferSize : %d\n", lpDesc->dwBufferSize);
68     TRACE("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, FALSE);
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 = 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((IDirect3DDevice7 *)lpDevice,
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                 {
193                     D3DMATRIXMULTIPLY *ci = (D3DMATRIXMULTIPLY *)instr;
194                     D3DMATRIX *a, *b, *c;
195
196                     a = ddraw_get_object(&lpDevice->handle_table, ci->hDestMatrix - 1, DDRAW_HANDLE_MATRIX);
197                     b = ddraw_get_object(&lpDevice->handle_table, ci->hSrcMatrix1 - 1, DDRAW_HANDLE_MATRIX);
198                     c = ddraw_get_object(&lpDevice->handle_table, ci->hSrcMatrix2 - 1, DDRAW_HANDLE_MATRIX);
199
200                     if (!a || !b || !c)
201                     {
202                         ERR("Invalid matrix handle (a %#x -> %p, b %#x -> %p, c %#x -> %p).\n",
203                                 ci->hDestMatrix, a, ci->hSrcMatrix1, b, ci->hSrcMatrix2, c);
204                     }
205                     else
206                     {
207                         TRACE("dst %p, src1 %p, src2 %p.\n", a, b, c);
208                         multiply_matrix(a, c, b);
209                     }
210
211                     instr += size;
212                 }
213             } break;
214
215             case D3DOP_STATETRANSFORM: {
216                 int i;
217                 TRACE("STATETRANSFORM   (%d)\n", count);
218                 
219                 for (i = 0; i < count; ++i)
220                 {
221                     D3DSTATE *ci = (D3DSTATE *)instr;
222                     D3DMATRIX *m;
223
224                     m = ddraw_get_object(&lpDevice->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATRIX);
225                     if (!m)
226                     {
227                         ERR("Invalid matrix handle %#x.\n", ci->u2.dwArg[0]);
228                     }
229                     else
230                     {
231                         if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_WORLD)
232                             lpDevice->world = ci->u2.dwArg[0];
233                         if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_VIEW)
234                             lpDevice->view = ci->u2.dwArg[0];
235                         if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_PROJECTION)
236                             lpDevice->proj = ci->u2.dwArg[0];
237                         IDirect3DDevice7_SetTransform((IDirect3DDevice7 *)lpDevice,
238                                 ci->u1.dtstTransformStateType, m);
239                     }
240
241                     instr += size;
242                 }
243             } break;
244
245             case D3DOP_STATELIGHT: {
246                 int i;
247                 TRACE("STATELIGHT       (%d)\n", count);
248
249                 for (i = 0; i < count; i++) {
250                     LPD3DSTATE ci = (LPD3DSTATE) instr;
251
252                     TRACE("(%08x,%08x)\n", ci->u1.dlstLightStateType, ci->u2.dwArg[0]);
253
254                     if (!ci->u1.dlstLightStateType || (ci->u1.dlstLightStateType > D3DLIGHTSTATE_COLORVERTEX))
255                         ERR("Unexpected Light State Type %d\n", ci->u1.dlstLightStateType);
256                     else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
257                     {
258                         IDirect3DMaterialImpl *m;
259
260                         m = ddraw_get_object(&lpDevice->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATERIAL);
261                         if (!m)
262                             ERR("Invalid material handle %#x.\n", ci->u2.dwArg[0]);
263                         else
264                             m->activate(m);
265                     }
266                     else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
267                     {
268                         switch (ci->u2.dwArg[0]) {
269                             case D3DCOLOR_MONO:
270                                 ERR("DDCOLOR_MONO should not happen!\n");
271                                 break;
272                             case D3DCOLOR_RGB:
273                                 /* We are already in this mode */
274                                 break;
275                             default:
276                                 ERR("Unknown color model!\n");
277                         }
278                     } else {
279                         D3DRENDERSTATETYPE rs = 0;
280                         switch (ci->u1.dlstLightStateType) {
281
282                             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
283                                 rs = D3DRENDERSTATE_AMBIENT;
284                                 break;
285                             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
286                                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
287                                 break;
288                             case D3DLIGHTSTATE_FOGSTART:      /* 5 */
289                                 rs = D3DRENDERSTATE_FOGSTART;
290                                 break;
291                             case D3DLIGHTSTATE_FOGEND:        /* 6 */
292                                 rs = D3DRENDERSTATE_FOGEND;
293                                 break;
294                             case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
295                                 rs = D3DRENDERSTATE_FOGDENSITY;
296                                 break;
297                             case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
298                                 rs = D3DRENDERSTATE_COLORVERTEX;
299                                 break;
300                             default:
301                                 break;
302                         }
303
304                         IDirect3DDevice7_SetRenderState((IDirect3DDevice7 *)lpDevice, rs, ci->u2.dwArg[0]);
305                     }
306
307                     instr += size;
308                 }
309             } break;
310
311             case D3DOP_STATERENDER: {
312                 int i;
313                 IDirect3DDevice2 *d3d_device2 = (IDirect3DDevice2 *)&lpDevice->IDirect3DDevice2_vtbl;
314                 TRACE("STATERENDER      (%d)\n", count);
315
316                 for (i = 0; i < count; i++) {
317                     LPD3DSTATE ci = (LPD3DSTATE) instr;
318
319                     IDirect3DDevice2_SetRenderState(d3d_device2, ci->u1.drstRenderStateType, ci->u2.dwArg[0]);
320
321                     instr += size;
322                 }
323             } break;
324
325             case D3DOP_PROCESSVERTICES:
326             {
327                 /* TODO: Share code with IDirect3DVertexBuffer::ProcessVertices and / or
328                  * IWineD3DDevice::ProcessVertices
329                  */
330                 int i;
331                 D3DMATRIX view_mat, world_mat, proj_mat;
332                 TRACE("PROCESSVERTICES  (%d)\n", count);
333
334                 /* Get the transform and world matrix */
335                 /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
336
337                 IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
338                                             D3DTRANSFORMSTATE_VIEW,
339                                             (WINED3DMATRIX*) &view_mat);
340
341                 IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
342                                             D3DTRANSFORMSTATE_PROJECTION,
343                                             (WINED3DMATRIX*) &proj_mat);
344
345                 IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
346                                             WINED3DTS_WORLDMATRIX(0),
347                                             (WINED3DMATRIX*) &world_mat);
348
349                 for (i = 0; i < count; i++) {
350                     LPD3DPROCESSVERTICES ci = (LPD3DPROCESSVERTICES) instr;
351
352                     TRACE("  Start : %d Dest : %d Count : %d\n",
353                           ci->wStart, ci->wDest, ci->dwCount);
354                     TRACE("  Flags : ");
355                     if (TRACE_ON(d3d7)) {
356                         if (ci->dwFlags & D3DPROCESSVERTICES_COPY)
357                             TRACE("COPY ");
358                         if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
359                             TRACE("NOCOLOR ");
360                         if (ci->dwFlags == D3DPROCESSVERTICES_OPMASK)
361                             TRACE("OPMASK ");
362                         if (ci->dwFlags & D3DPROCESSVERTICES_TRANSFORM)
363                             TRACE("TRANSFORM ");
364                         if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT)
365                             TRACE("TRANSFORMLIGHT ");
366                         if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
367                             TRACE("UPDATEEXTENTS ");
368                         TRACE("\n");
369                     }
370
371                     /* This is where doing Direct3D on top on OpenGL is quite difficult.
372                        This method transforms a set of vertices using the CURRENT state
373                        (lighting, projection, ...) but does not rasterize them.
374                        They will only be put on screen later (with the POINT / LINE and
375                        TRIANGLE op-codes). The problem is that you can have a triangle
376                        with each point having been transformed using another state...
377
378                        In this implementation, I will emulate only ONE thing : each
379                        vertex can have its own "WORLD" transformation (this is used in the
380                        TWIST.EXE demo of the 5.2 SDK). I suppose that all vertices of the
381                        execute buffer use the same state.
382
383                        If I find applications that change other states, I will try to do a
384                        more 'fine-tuned' state emulation (but I may become quite tricky if
385                        it changes a light position in the middle of a triangle).
386
387                        In this case, a 'direct' approach (i.e. without using OpenGL, but
388                        writing our own 3D rasterizer) would be easier. */
389
390                     /* The current method (with the hypothesis that only the WORLD matrix
391                        will change between two points) is like this :
392                        - I transform 'manually' all the vertices with the current WORLD
393                          matrix and store them in the vertex buffer
394                        - during the rasterization phase, the WORLD matrix will be set to
395                          the Identity matrix */
396
397                     /* Enough for the moment */
398                     if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) {
399                         unsigned int nb;
400                         D3DVERTEX  *src = ((LPD3DVERTEX)  ((char *)This->desc.lpData + vs)) + ci->wStart;
401                         D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
402                         D3DMATRIX mat;
403                         D3DVIEWPORT* Viewport = &lpViewport->viewports.vp1;
404                         
405                         if (TRACE_ON(d3d7)) {
406                             TRACE("  Projection Matrix : (%p)\n", &proj_mat);
407                             dump_D3DMATRIX(&proj_mat);
408                             TRACE("  View       Matrix : (%p)\n", &view_mat);
409                             dump_D3DMATRIX(&view_mat);
410                             TRACE("  World Matrix : (%p)\n", &world_mat);
411                             dump_D3DMATRIX(&world_mat);
412                         }
413
414                         multiply_matrix(&mat,&view_mat,&world_mat);
415                         multiply_matrix(&mat,&proj_mat,&mat);
416
417                         for (nb = 0; nb < ci->dwCount; nb++) {
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->dvScaleX
432                                        + Viewport->dwX + Viewport->dwWidth / 2;
433                             dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
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", &world_mat);
455                             dump_D3DMATRIX(&world_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->u1.sx / dst->u4.rhw * Viewport->dvScaleX
474                                        + Viewport->dwX + Viewport->dwWidth / 2;
475                             dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
476                                        + Viewport->dwY + Viewport->dwHeight / 2;
477
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 flag %#x.\n", ci->dwFlags);
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                             if (ci->dwOffset) {
522                                 instr = (char*)current + ci->dwOffset;
523                                 break;
524                             }
525                         }
526                     } else {
527                         if (ci->bNegate) {
528                             TRACE(" Branch to %d\n", ci->dwOffset);
529                             if (ci->dwOffset) {
530                                 instr = (char*)current + ci->dwOffset;
531                                 break;
532                             }
533                         }
534                     }
535
536                     instr += size;
537                 }
538             } break;
539
540             case D3DOP_SPAN: {
541                 WARN("SPAN-s           (%d)\n", count);
542
543                 instr += count * size;
544             } break;
545
546             case D3DOP_SETSTATUS: {
547                 int i;
548                 TRACE("SETSTATUS        (%d)\n", count);
549
550                 for (i = 0; i < count; i++) {
551                     LPD3DSTATUS ci = (LPD3DSTATUS) instr;
552
553                     This->data.dsStatus = *ci;
554
555                     instr += size;
556                 }
557             } break;
558
559             default:
560                 ERR("Unhandled OpCode %d !!!\n",current->bOpcode);
561                 /* Try to save ... */
562                 instr += count * size;
563                 break;
564         }
565     }
566
567 end_of_buffer:
568     ;
569 }
570
571 /*****************************************************************************
572  * IDirect3DExecuteBuffer::QueryInterface
573  *
574  * Well, a usual QueryInterface function. Don't know fur sure which
575  * interfaces it can Query.
576  *
577  * Params:
578  *  riid: The interface ID queried for
579  *  obj: Address to return the interface pointer at
580  *
581  * Returns:
582  *  D3D_OK in case of a success (S_OK? Think it's the same)
583  *  OLE_E_ENUM_NOMORE if the interface wasn't found.
584  *   (E_NOINTERFACE?? Don't know what I really need)
585  *
586  *****************************************************************************/
587 static HRESULT WINAPI
588 IDirect3DExecuteBufferImpl_QueryInterface(IDirect3DExecuteBuffer *iface,
589                                           REFIID riid,
590                                           void **obj)
591 {
592     TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), obj);
593
594     *obj = NULL;
595
596     if ( IsEqualGUID( &IID_IUnknown,  riid ) ) {
597         IDirect3DExecuteBuffer_AddRef(iface);
598         *obj = iface;
599         TRACE("  Creating IUnknown interface at %p.\n", *obj);
600         return S_OK;
601     }
602     if ( IsEqualGUID( &IID_IDirect3DExecuteBuffer, riid ) ) {
603         IDirect3DExecuteBuffer_AddRef(iface);
604         *obj = iface;
605         TRACE("  Creating IDirect3DExecuteBuffer interface %p\n", *obj);
606         return S_OK;
607     }
608     FIXME("(%p): interface for IID %s NOT found!\n", iface, debugstr_guid(riid));
609     return E_NOINTERFACE;
610 }
611
612
613 /*****************************************************************************
614  * IDirect3DExecuteBuffer::AddRef
615  *
616  * A normal AddRef method, nothing special
617  *
618  * Returns:
619  *  The new refcount
620  *
621  *****************************************************************************/
622 static ULONG WINAPI
623 IDirect3DExecuteBufferImpl_AddRef(IDirect3DExecuteBuffer *iface)
624 {
625     IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
626     ULONG ref = InterlockedIncrement(&This->ref);
627
628     FIXME("(%p)->()incrementing from %u.\n", This, ref - 1);
629
630     return ref;
631 }
632
633 /*****************************************************************************
634  * IDirect3DExecuteBuffer::Release
635  *
636  * A normal Release method, nothing special
637  *
638  * Returns:
639  *  The new refcount
640  *
641  *****************************************************************************/
642 static ULONG WINAPI
643 IDirect3DExecuteBufferImpl_Release(IDirect3DExecuteBuffer *iface)
644 {
645     IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
646     ULONG ref = InterlockedDecrement(&This->ref);
647
648     TRACE("(%p)->()decrementing from %u.\n", This, ref + 1);
649
650     if (!ref) {
651         if (This->need_free)
652             HeapFree(GetProcessHeap(),0,This->desc.lpData);
653         HeapFree(GetProcessHeap(),0,This->vertex_data);
654         HeapFree(GetProcessHeap(),0,This->indices);
655         HeapFree(GetProcessHeap(),0,This);
656         return 0;
657     }
658
659     return ref;
660 }
661
662 /*****************************************************************************
663  * IDirect3DExecuteBuffer::Initialize
664  *
665  * Initializes the Execute Buffer. This method exists for COM compliance
666  * Nothing to do here.
667  *
668  * Returns:
669  *  D3D_OK
670  *
671  *****************************************************************************/
672 static HRESULT WINAPI
673 IDirect3DExecuteBufferImpl_Initialize(IDirect3DExecuteBuffer *iface,
674                                         IDirect3DDevice *lpDirect3DDevice,
675                                         D3DEXECUTEBUFFERDESC *lpDesc)
676 {
677     IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
678     TRACE("(%p)->(%p,%p) no-op....\n", This, lpDirect3DDevice, lpDesc);
679     return D3D_OK;
680 }
681
682 /*****************************************************************************
683  * IDirect3DExecuteBuffer::Lock
684  *
685  * Locks the buffer, so the app can write into it.
686  *
687  * Params:
688  *  Desc: Pointer to return the buffer description. This Description contains
689  *        a pointer to the buffer data.
690  *
691  * Returns:
692  *  This implementation always returns D3D_OK
693  *
694  *****************************************************************************/
695 static HRESULT WINAPI
696 IDirect3DExecuteBufferImpl_Lock(IDirect3DExecuteBuffer *iface,
697                                 D3DEXECUTEBUFFERDESC *lpDesc)
698 {
699     IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
700     DWORD dwSize;
701     TRACE("(%p)->(%p)\n", This, lpDesc);
702
703     dwSize = lpDesc->dwSize;
704     memcpy(lpDesc, &This->desc, dwSize);
705
706     if (TRACE_ON(d3d7)) {
707         TRACE("  Returning description :\n");
708         _dump_D3DEXECUTEBUFFERDESC(lpDesc);
709     }
710     return D3D_OK;
711 }
712
713 /*****************************************************************************
714  * IDirect3DExecuteBuffer::Unlock
715  *
716  * Unlocks the buffer. We don't have anything to do here
717  *
718  * Returns:
719  *  This implementation always returns D3D_OK
720  *
721  *****************************************************************************/
722 static HRESULT WINAPI
723 IDirect3DExecuteBufferImpl_Unlock(IDirect3DExecuteBuffer *iface)
724 {
725     IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
726     TRACE("(%p)->() no-op...\n", This);
727     return D3D_OK;
728 }
729
730 /*****************************************************************************
731  * IDirect3DExecuteBuffer::SetExecuteData
732  *
733  * Sets the execute data. This data is used to describe the buffer's content
734  *
735  * Params:
736  *  Data: Pointer to a D3DEXECUTEDATA structure containing the data to
737  *  assign
738  *
739  * Returns:
740  *  D3D_OK on success
741  *  DDERR_OUTOFMEMORY if the vertex buffer allocation failed
742  *
743  *****************************************************************************/
744 static HRESULT WINAPI
745 IDirect3DExecuteBufferImpl_SetExecuteData(IDirect3DExecuteBuffer *iface,
746                                           D3DEXECUTEDATA *lpData)
747 {
748     IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
749     DWORD nbvert;
750     TRACE("(%p)->(%p)\n", This, lpData);
751
752     memcpy(&This->data, lpData, lpData->dwSize);
753
754     /* Get the number of vertices in the execute buffer */
755     nbvert = This->data.dwVertexCount;
756
757     /* Prepares the transformed vertex buffer */
758     HeapFree(GetProcessHeap(), 0, This->vertex_data);
759     This->vertex_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbvert * sizeof(D3DTLVERTEX));
760
761     if (TRACE_ON(d3d7)) {
762         _dump_executedata(lpData);
763     }
764
765     return D3D_OK;
766 }
767
768 /*****************************************************************************
769  * IDirect3DExecuteBuffer::GetExecuteData
770  *
771  * Returns the data in the execute buffer
772  *
773  * Params:
774  *  Data: Pointer to a D3DEXECUTEDATA structure used to return data
775  *
776  * Returns:
777  *  D3D_OK on success
778  *
779  *****************************************************************************/
780 static HRESULT WINAPI
781 IDirect3DExecuteBufferImpl_GetExecuteData(IDirect3DExecuteBuffer *iface,
782                                           D3DEXECUTEDATA *lpData)
783 {
784     IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
785     DWORD dwSize;
786     TRACE("(%p)->(%p): stub!\n", This, lpData);
787
788     dwSize = lpData->dwSize;
789     memcpy(lpData, &This->data, dwSize);
790
791     if (TRACE_ON(d3d7)) {
792         TRACE("Returning data :\n");
793         _dump_executedata(lpData);
794     }
795
796     return DD_OK;
797 }
798
799 /*****************************************************************************
800  * IDirect3DExecuteBuffer::Validate
801  *
802  * DirectX 5 SDK: "The IDirect3DExecuteBuffer::Validate method is not
803  * currently implemented"
804  *
805  * Params:
806  *  ?
807  *
808  * Returns:
809  *  DDERR_UNSUPPORTED, because it's not implemented in Windows.
810  *
811  *****************************************************************************/
812 static HRESULT WINAPI
813 IDirect3DExecuteBufferImpl_Validate(IDirect3DExecuteBuffer *iface,
814                                     DWORD *Offset,
815                                     LPD3DVALIDATECALLBACK Func,
816                                     void *UserArg,
817                                     DWORD Reserved)
818 {
819     IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
820     TRACE("(%p)->(%p,%p,%p,%08x): Unimplemented!\n", This, Offset, Func, UserArg, Reserved);
821     return DDERR_UNSUPPORTED; /* Unchecked */
822 }
823
824 /*****************************************************************************
825  * IDirect3DExecuteBuffer::Optimize
826  *
827  * DirectX5 SDK: "The IDirect3DExecuteBuffer::Optimize method is not
828  * currently supported"
829  *
830  * Params:
831  *  Dummy: Seems to be an unused dummy ;)
832  *
833  * Returns:
834  *  DDERR_UNSUPPORTED, because it's not implemented in Windows.
835  *
836  *****************************************************************************/
837 static HRESULT WINAPI
838 IDirect3DExecuteBufferImpl_Optimize(IDirect3DExecuteBuffer *iface,
839                                     DWORD Dummy)
840 {
841     IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
842     TRACE("(%p)->(%08x): Unimplemented\n", This, Dummy);
843     return DDERR_UNSUPPORTED; /* Unchecked */
844 }
845
846 const IDirect3DExecuteBufferVtbl IDirect3DExecuteBuffer_Vtbl =
847 {
848     IDirect3DExecuteBufferImpl_QueryInterface,
849     IDirect3DExecuteBufferImpl_AddRef,
850     IDirect3DExecuteBufferImpl_Release,
851     IDirect3DExecuteBufferImpl_Initialize,
852     IDirect3DExecuteBufferImpl_Lock,
853     IDirect3DExecuteBufferImpl_Unlock,
854     IDirect3DExecuteBufferImpl_SetExecuteData,
855     IDirect3DExecuteBufferImpl_GetExecuteData,
856     IDirect3DExecuteBufferImpl_Validate,
857     IDirect3DExecuteBufferImpl_Optimize,
858 };