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