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