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