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