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