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