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