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