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