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