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