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