executebuffer.c 29.1 KB
Newer Older
1
/* Direct3D ExecuteBuffer
2 3
 * Copyright (c) 1998-2004 Lionel ULMER
 * Copyright (c) 2002-2004 Christian Costa
4
 * Copyright (c) 2006      Stefan Dsinger
5
 *
6
 * This file contains the implementation of IDirect3DExecuteBuffer.
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 "winnls.h"
37
#include "winerror.h"
38
#include "wingdi.h"
39 40 41
#include "wine/exception.h"
#include "excpt.h"

42 43 44
#include "ddraw.h"
#include "d3d.h"

45 46
#include "ddraw_private.h"
#include "wine/debug.h"
47

48
WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
49

50 51 52 53 54 55 56
/*****************************************************************************
 * _dump_executedata
 * _dump_D3DEXECUTEBUFFERDESC
 *
 * Debug functions which write the executebuffer data to the console
 *
 *****************************************************************************/
57

58 59 60 61 62
static void _dump_executedata(LPD3DEXECUTEDATA lpData) {
    DPRINTF("dwSize : %ld\n", lpData->dwSize);
    DPRINTF("Vertex      Offset : %ld  Count  : %ld\n", lpData->dwVertexOffset, lpData->dwVertexCount);
    DPRINTF("Instruction Offset : %ld  Length : %ld\n", lpData->dwInstructionOffset, lpData->dwInstructionLength);
    DPRINTF("HVertex     Offset : %ld\n", lpData->dwHVertexOffset);
63 64
}

65
static void _dump_D3DEXECUTEBUFFERDESC(LPD3DEXECUTEBUFFERDESC lpDesc) {
66 67 68 69 70
    DPRINTF("dwSize       : %ld\n", lpDesc->dwSize);
    DPRINTF("dwFlags      : %lx\n", lpDesc->dwFlags);
    DPRINTF("dwCaps       : %lx\n", lpDesc->dwCaps);
    DPRINTF("dwBufferSize : %ld\n", lpDesc->dwBufferSize);
    DPRINTF("lpData       : %p\n", lpDesc->lpData);
71 72
}

73 74 75 76 77 78 79 80
/*****************************************************************************
 * IDirect3DExecuteBufferImpl_Execute
 *
 * The main functionality of the execute buffer
 * It transforms the vertices if necessary, and calls IDirect3DDevice7
 * for drawing the vertices. It is called from
 * IDirect3DDevice::Execute
 *
81
 * TODO: Perhaps some comments about the various opcodes wouldn't hurt
82 83 84 85 86 87 88 89 90 91 92 93 94
 *
 * Don't declare this static, as it's called from device.c,
 * IDirect3DDevice::Execute
 *
 * Params:
 *  Device: 3D Device associated to use for drawing
 *  Viewport: Viewport for this operation
 *
 *****************************************************************************/
void
IDirect3DExecuteBufferImpl_Execute(IDirect3DExecuteBufferImpl *This,
                                   IDirect3DDeviceImpl *lpDevice,
                                   IDirect3DViewportImpl *lpViewport)
95 96 97 98 99 100
{
    /* DWORD bs = This->desc.dwBufferSize; */
    DWORD vs = This->data.dwVertexOffset;
    /* DWORD vc = This->data.dwVertexCount; */
    DWORD is = This->data.dwInstructionOffset;
    /* DWORD il = This->data.dwInstructionLength; */
101 102

    char *instr = (char *)This->desc.lpData + is;
103 104 105 106

    /* Should check if the viewport was added or not to the device */

    /* Activate the viewport */
107 108
    lpViewport->active_device = lpDevice;
    lpViewport->activate(lpViewport);
109

110
    TRACE("ExecuteData :\n");
111
    if (TRACE_ON(d3d7))
112
      _dump_executedata(&(This->data));
113 114 115 116 117 118 119 120 121 122 123 124

    while (1) {
        LPD3DINSTRUCTION current = (LPD3DINSTRUCTION) instr;
	BYTE size;
	WORD count;
	
	count = current->wCount;
	size = current->bSize;
	instr += sizeof(D3DINSTRUCTION);
	
	switch (current->bOpcode) {
	    case D3DOP_POINT: {
125
	        WARN("POINT-s          (%d)\n", count);
126 127 128 129
		instr += count * size;
	    } break;

	    case D3DOP_LINE: {
130
	        WARN("LINE-s           (%d)\n", count);
131 132 133 134 135
		instr += count * size;
	    } break;

	    case D3DOP_TRIANGLE: {
	        int i;
136
		D3DTLVERTEX *tl_vx = (D3DTLVERTEX *) This->vertex_data;
137 138
		TRACE("TRIANGLE         (%d)\n", count);
		
139 140
		if (count*3>This->nb_indices) {
		    This->nb_indices = count * 3;
141
                    HeapFree(GetProcessHeap(),0,This->indices);
142
		    This->indices = HeapAlloc(GetProcessHeap(),0,sizeof(WORD)*This->nb_indices);
143
		}
144 145 146
			
		for (i = 0; i < count; i++) {
                    LPD3DTRIANGLE ci = (LPD3DTRIANGLE) instr;
147 148 149
		    TRACE_(d3d7)("  v1: %d  v2: %d  v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3);
		    TRACE_(d3d7)("  Flags : ");
		    if (TRACE_ON(d3d7)) {
150 151
			/* Wireframe */
			if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
152
	        	    TRACE_(d3d7)("EDGEENABLE1 ");
153
	    		if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2)
154
	        	    TRACE_(d3d7)("EDGEENABLE2 ");
155
	    		if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
156
	        	    TRACE_(d3d7)("EDGEENABLE3 ");
157 158
	    		/* Strips / Fans */
	    		if (ci->wFlags == D3DTRIFLAG_EVEN)
159
	        	    TRACE_(d3d7)("EVEN ");
160
	    		if (ci->wFlags == D3DTRIFLAG_ODD)
161
	        	    TRACE_(d3d7)("ODD ");
162
	    		if (ci->wFlags == D3DTRIFLAG_START)
163
	        	    TRACE_(d3d7)("START ");
164
	    		if ((ci->wFlags > 0) && (ci->wFlags < 30))
165 166
	       		    TRACE_(d3d7)("STARTFLAT(%d) ", ci->wFlags);
	    		TRACE_(d3d7)("\n");
167
        	    }
168 169 170
		    This->indices[(i * 3)    ] = ci->u1.v1;
		    This->indices[(i * 3) + 1] = ci->u2.v2;
		    This->indices[(i * 3) + 2] = ci->u3.v3;
171
                    instr += size;
172
		}
173 174 175 176 177 178 179
                /* IDirect3DDevices have color keying always enabled -
                 * enable it before drawing. This overwrites any ALPHA*
                 * render state
                 */
                IWineD3DDevice_SetRenderState(lpDevice->wineD3DDevice,
                                              WINED3DRS_COLORKEYENABLE,
                                              1);
180 181
                IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(lpDevice,IDirect3DDevice7),
				                      D3DPT_TRIANGLELIST,D3DFVF_TLVERTEX,tl_vx,0,This->indices,count*3,0);
182 183 184
	    } break;

	    case D3DOP_MATRIXLOAD:
185
	        WARN("MATRIXLOAD-s     (%d)\n", count);
186 187 188 189 190 191 192 193 194
	        instr += count * size;
	        break;

	    case D3DOP_MATRIXMULTIPLY: {
	        int i;
		TRACE("MATRIXMULTIPLY   (%d)\n", count);
		
		for (i = 0; i < count; i++) {
		    LPD3DMATRIXMULTIPLY ci = (LPD3DMATRIXMULTIPLY) instr;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
                    LPD3DMATRIX a, b, c;

                    if(!ci->hDestMatrix || ci->hDestMatrix > lpDevice->numHandles ||
                       !ci->hSrcMatrix1 || ci->hSrcMatrix1 > lpDevice->numHandles ||
                       !ci->hSrcMatrix2 || ci->hSrcMatrix2 > lpDevice->numHandles) {
                        ERR("Handles out of bounds\n");
                    } else if (lpDevice->Handles[ci->hDestMatrix - 1].type != DDrawHandle_Matrix ||
                               lpDevice->Handles[ci->hSrcMatrix1 - 1].type != DDrawHandle_Matrix ||
                               lpDevice->Handles[ci->hSrcMatrix2 - 1].type != DDrawHandle_Matrix) {
                        ERR("Handle types invalid\n");
                    } else {
                        a = (LPD3DMATRIX) lpDevice->Handles[ci->hDestMatrix - 1].ptr;
                        b = (LPD3DMATRIX) lpDevice->Handles[ci->hSrcMatrix1 - 1].ptr;
                        c = (LPD3DMATRIX) lpDevice->Handles[ci->hSrcMatrix2 - 1].ptr;
                        TRACE("  Dest : %p  Src1 : %p  Src2 : %p\n",
                            a, b, c);
                        multiply_matrix(a,c,b);
                    }
213

214
                    instr += size;
215 216 217 218 219 220 221 222 223 224
		}
	    } break;

	    case D3DOP_STATETRANSFORM: {
	        int i;
		TRACE("STATETRANSFORM   (%d)\n", count);
		
		for (i = 0; i < count; i++) {
		    LPD3DSTATE ci = (LPD3DSTATE) instr;

225 226 227 228 229 230 231 232 233 234
                    if(!ci->u2.dwArg[0]) {
                        ERR("Setting a NULL matrix handle, what should I do?\n");
                    } else if(ci->u2.dwArg[0] > lpDevice->numHandles) {
                        ERR("Handle %ld is out of bounds\n", ci->u2.dwArg[0]);
                    } else if(lpDevice->Handles[ci->u2.dwArg[0] - 1].type != DDrawHandle_Matrix) {
                        ERR("Handle %ld is not a matrix handle\n", ci->u2.dwArg[0]);
                    } else {
                        IDirect3DDevice7_SetTransform(ICOM_INTERFACE(lpDevice, IDirect3DDevice7),
                                                      ci->u1.drstRenderStateType, (LPD3DMATRIX) lpDevice->Handles[ci->u2.dwArg[0] - 1].ptr);
                    }
235 236 237 238 239 240 241 242 243 244
		    instr += size;
		}
	    } break;

	    case D3DOP_STATELIGHT: {
	        int i;
		TRACE("STATELIGHT       (%d)\n", count);
		
		for (i = 0; i < count; i++) {
		    LPD3DSTATE ci = (LPD3DSTATE) instr;
245 246 247 248 249 250 251 252 253 254 255

		    TRACE("(%08x,%08lx)\n",ci->u1.dlstLightStateType, ci->u2.dwArg[0]);

		    if (!ci->u1.dlstLightStateType && (ci->u1.dlstLightStateType > D3DLIGHTSTATE_COLORVERTEX))
			ERR("Unexpected Light State Type\n");
		    else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */) {
			IDirect3DMaterialImpl *mat = (IDirect3DMaterialImpl *) ci->u2.dwArg[0];

			if (mat != NULL) {
			    mat->activate(mat);
			} else {
256
			    FIXME(" D3DLIGHTSTATE_MATERIAL called with NULL material !!!\n");
257
			}
258
		    }
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		   else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */) {
			switch (ci->u2.dwArg[0]) {
			    case D3DCOLOR_MONO:
			       ERR("DDCOLOR_MONO should not happen!\n");
			       break;
			    case D3DCOLOR_RGB:
			       /* We are already in this mode */
			       break;
			    default:
		               ERR("Unknown color model!\n");
			}
		    } else {
		    	D3DRENDERSTATETYPE rs = 0;
		    	switch (ci->u1.dlstLightStateType) {

		    	    case D3DLIGHTSTATE_AMBIENT:       /* 2 */
				rs = D3DRENDERSTATE_AMBIENT;
				break;		
			    case D3DLIGHTSTATE_FOGMODE:       /* 4 */
				rs = D3DRENDERSTATE_FOGVERTEXMODE;
				break;
			    case D3DLIGHTSTATE_FOGSTART:      /* 5 */
				rs = D3DRENDERSTATE_FOGSTART;
				break;
			    case D3DLIGHTSTATE_FOGEND:        /* 6 */
				rs = D3DRENDERSTATE_FOGEND;
				break;
			    case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
				rs = D3DRENDERSTATE_FOGDENSITY;
				break;
			    case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
				rs = D3DRENDERSTATE_COLORVERTEX;
				break;
			    default:
				break;
			}
			
		        IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(lpDevice, IDirect3DDevice7),
	                                		rs,ci->u2.dwArg[0]);
		   }

300
		   instr += size;
301
		}
302 303 304 305 306 307 308 309 310
	    } break;

	    case D3DOP_STATERENDER: {
	        int i;
		TRACE("STATERENDER      (%d)\n", count);

		for (i = 0; i < count; i++) {
		    LPD3DSTATE ci = (LPD3DSTATE) instr;
		    
311 312
		    IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(lpDevice, IDirect3DDevice7),
						    ci->u1.drstRenderStateType, ci->u2.dwArg[0]);
313 314 315 316 317

		    instr += size;
		}
	    } break;

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
            case D3DOP_PROCESSVERTICES:
            {
                /* TODO: Share code with IDirect3DVertexBuffer::ProcessVertices and / or
                 * IWineD3DDevice::ProcessVertices
                 */
                int i;
                D3DMATRIX view_mat, world_mat, proj_mat;
                TRACE("PROCESSVERTICES  (%d)\n", count);

                /* Get the transform and world matrix */
                IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
                                            D3DTRANSFORMSTATE_VIEW,
                                            &view_mat);

                IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
                                            D3DTRANSFORMSTATE_PROJECTION,
                                            &proj_mat);

                IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
                                            D3DTRANSFORMSTATE_WORLD,
                                            &world_mat);
339 340 341 342 343 344 345

		for (i = 0; i < count; i++) {
		    LPD3DPROCESSVERTICES ci = (LPD3DPROCESSVERTICES) instr;

		    TRACE("  Start : %d Dest : %d Count : %ld\n",
			  ci->wStart, ci->wDest, ci->dwCount);
		    TRACE("  Flags : ");
346
		    if (TRACE_ON(d3d7)) {
347
		        if (ci->dwFlags & D3DPROCESSVERTICES_COPY)
348
			    TRACE("COPY ");
349
			if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
350
			    TRACE("NOCOLOR ");
351
			if (ci->dwFlags == D3DPROCESSVERTICES_OPMASK)
352
			    TRACE("OPMASK ");
353
			if (ci->dwFlags & D3DPROCESSVERTICES_TRANSFORM)
354
			    TRACE("TRANSFORM ");
355
			if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT)
356
			    TRACE("TRANSFORMLIGHT ");
357
			if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
358 359
			    TRACE("UPDATEEXTENTS ");
			TRACE("\n");
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
		    }
		    
		    /* This is where doing Direct3D on top on OpenGL is quite difficult.
		       This method transforms a set of vertices using the CURRENT state
		       (lighting, projection, ...) but does not rasterize them.
		       They will only be put on screen later (with the POINT / LINE and
		       TRIANGLE op-codes). The problem is that you can have a triangle
		       with each point having been transformed using another state...
		       
		       In this implementation, I will emulate only ONE thing : each
		       vertex can have its own "WORLD" transformation (this is used in the
		       TWIST.EXE demo of the 5.2 SDK). I suppose that all vertices of the
		       execute buffer use the same state.
		       
		       If I find applications that change other states, I will try to do a
		       more 'fine-tuned' state emulation (but I may become quite tricky if
		       it changes a light position in the middle of a triangle).
		       
		       In this case, a 'direct' approach (i.e. without using OpenGL, but
		       writing our own 3D rasterizer) would be easier. */
		    
		    /* The current method (with the hypothesis that only the WORLD matrix
		       will change between two points) is like this :
		       - I transform 'manually' all the vertices with the current WORLD
		         matrix and store them in the vertex buffer
		       - during the rasterization phase, the WORLD matrix will be set to
		         the Identity matrix */
		    
		    /* Enough for the moment */
		    if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) {
390
		        unsigned int nb;
391
			D3DVERTEX  *src = ((LPD3DVERTEX)  ((char *)This->desc.lpData + vs)) + ci->wStart;
392
			D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
393
			D3DMATRIX *mat2 = &world_mat;
394 395 396
			D3DMATRIX mat;
			D3DVALUE nx,ny,nz;
			D3DVIEWPORT* Viewport = &lpViewport->viewports.vp1;
397
			
398 399 400 401 402 403 404
			if (TRACE_ON(d3d7)) {
			    TRACE("  Projection Matrix : (%p)\n", &proj_mat);
			    dump_D3DMATRIX(&proj_mat);
			    TRACE("  View       Matrix : (%p)\n", &view_mat);
			    dump_D3DMATRIX(&view_mat);
			    TRACE("  World Matrix : (%p)\n", &world_mat);
			    dump_D3DMATRIX(&world_mat);
405 406
			}

407 408
                        multiply_matrix(&mat,&view_mat,&world_mat);
                        multiply_matrix(&mat,&proj_mat,&mat);
409 410

			for (nb = 0; nb < ci->dwCount; nb++) {
411 412 413 414
			    /* Normals transformation */
			    nx = (src->u4.nx * mat2->_11) + (src->u5.ny * mat2->_21) + (src->u6.nz * mat2->_31);
			    ny = (src->u4.nx * mat2->_12) + (src->u5.ny * mat2->_22) + (src->u6.nz * mat2->_32);
			    nz = (src->u4.nx * mat2->_13) + (src->u5.ny * mat2->_23) + (src->u6.nz * mat2->_33);
415
			    
416 417 418
			    /* No lighting yet */
			    dst->u5.color = 0xFFFFFFFF; /* Opaque white */
			    dst->u6.specular = 0xFF000000; /* No specular and no fog factor */
419
			    
420 421
			    dst->u7.tu  = src->u7.tu;
			    dst->u8.tv  = src->u8.tv;
422
			    
423 424 425 426 427 428 429 430 431 432 433 434 435
			    /* Now, the matrix multiplication */
			    dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
			    dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
			    dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
			    dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);

			    dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dwWidth / 2
				       + Viewport->dwX + Viewport->dwWidth / 2;
			    dst->u2.sy = dst->u2.sy / dst->u4.rhw * Viewport->dwHeight / 2
				       + Viewport->dwY + Viewport->dwHeight / 2;
			    dst->u3.sz /= dst->u4.rhw;
			    dst->u4.rhw = 1 / dst->u4.rhw;

436 437
			    src++;
			    dst++;
438
			    
439 440
			}
		    } else if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORM) {
441
		        unsigned int nb;
442
			D3DLVERTEX *src  = ((LPD3DLVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
443 444 445
			D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
			D3DMATRIX mat;
			D3DVIEWPORT* Viewport = &lpViewport->viewports.vp1;
446
			
447 448 449 450 451
			if (TRACE_ON(d3d7)) {
			    TRACE("  Projection Matrix : (%p)\n", &proj_mat);
			    dump_D3DMATRIX(&proj_mat);
			    TRACE("  View       Matrix : (%p)\n",&view_mat);
			    dump_D3DMATRIX(&view_mat);
452 453 454 455
			    TRACE("  World Matrix : (%p)\n", &mat);
			    dump_D3DMATRIX(&mat);
			}

456 457
			multiply_matrix(&mat,&view_mat,&world_mat);
			multiply_matrix(&mat,&proj_mat,&mat);
458

459
			for (nb = 0; nb < ci->dwCount; nb++) {
460 461 462 463
			    dst->u5.color = src->u4.color;
			    dst->u6.specular = src->u5.specular;
			    dst->u7.tu = src->u6.tu;
			    dst->u8.tv = src->u7.tv;
464 465
			    
			    /* Now, the matrix multiplication */
466 467 468 469 470 471 472 473 474 475
			    dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
			    dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
			    dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
			    dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);

			    dst->u1.sx /= dst->u4.rhw * Viewport->dvScaleX * Viewport->dwWidth / 2 + Viewport->dwX;
			    dst->u2.sy /= dst->u4.rhw * Viewport->dvScaleY * Viewport->dwHeight / 2 + Viewport->dwY;
			    dst->u3.sz /= dst->u4.rhw;
			    dst->u4.rhw = 1 / dst->u4.rhw;

476 477 478 479
			    src++;
			    dst++;
			}
		    } else if (ci->dwFlags == D3DPROCESSVERTICES_COPY) {
480
		        D3DTLVERTEX *src = ((LPD3DTLVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
481
			D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
482 483 484 485 486 487 488 489 490 491 492
			
			memcpy(dst, src, ci->dwCount * sizeof(D3DTLVERTEX));
		    } else {
		        ERR("Unhandled vertex processing !\n");
		    }

		    instr += size;
		}
	    } break;

	    case D3DOP_TEXTURELOAD: {
493
	        WARN("TEXTURELOAD-s    (%d)\n", count);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

		instr += count * size;
	    } break;

	    case D3DOP_EXIT: {
	        TRACE("EXIT             (%d)\n", count);
		/* We did this instruction */
		instr += size;
		/* Exit this loop */
		goto end_of_buffer;
	    } break;

	    case D3DOP_BRANCHFORWARD: {
	        int i;
		TRACE("BRANCHFORWARD    (%d)\n", count);

		for (i = 0; i < count; i++) {
		    LPD3DBRANCH ci = (LPD3DBRANCH) instr;

513
		    if ((This->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue) {
514
		        if (!ci->bNegate) {
515 516 517
			    TRACE(" Branch to %ld\n", ci->dwOffset);
			    instr = (char*)current + ci->dwOffset;
			    break;
518 519 520
			}
		    } else {
		        if (ci->bNegate) {
521 522 523
			    TRACE(" Branch to %ld\n", ci->dwOffset);
			    instr = (char*)current + ci->dwOffset;
			    break;
524 525 526 527 528 529 530 531
			}
		    }

		    instr += size;
		}
	    } break;

	    case D3DOP_SPAN: {
532
	        WARN("SPAN-s           (%d)\n", count);
533 534 535 536 537 538 539 540 541 542 543

		instr += count * size;
	    } break;

	    case D3DOP_SETSTATUS: {
	        int i;
		TRACE("SETSTATUS        (%d)\n", count);

		for (i = 0; i < count; i++) {
		    LPD3DSTATUS ci = (LPD3DSTATUS) instr;
		    
544
		    This->data.dsStatus = *ci;
545 546 547 548 549 550

		    instr += size;
		}
	    } break;

	    default:
551
	        ERR("Unhandled OpCode %d !!!\n",current->bOpcode);
552 553 554
	        /* Try to save ... */
	        instr += count * size;
	        break;
555 556 557
	}
    }

558
end_of_buffer:
559
    ;
560 561
}

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
/*****************************************************************************
 * IDirect3DExecuteBuffer::QueryInterface
 *
 * Well, a usual QueryInterface function. Don't know fur sure which
 * interfaces it can Query.
 *
 * Params:
 *  riid: The interface ID queried for
 *  obj: Address to return the interface pointer at
 *
 * Returns:
 *  D3D_OK in case of a success (S_OK? Think it's the same)
 *  OLE_E_ENUM_NOMORE if the interface wasn't found.
 *   (E_NOINTERFACE?? Don't know what I really need)
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DExecuteBufferImpl_QueryInterface(IDirect3DExecuteBuffer *iface,
                                          REFIID riid,
                                          void **obj)
582
{
583
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
584
    TRACE("(%p/%p)->(%s,%p)\n", This, iface, debugstr_guid(riid), obj);
585

586
    *obj = NULL;
587

588 589
    if ( IsEqualGUID( &IID_IUnknown,  riid ) ) {
        IDirect3DExecuteBuffer_AddRef(ICOM_INTERFACE(This, IDirect3DExecuteBuffer));
590 591
	*obj = iface;
	TRACE("  Creating IUnknown interface at %p.\n", *obj);
592 593 594 595
	return S_OK;
    }
    if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
        IDirect3DExecuteBuffer_AddRef(ICOM_INTERFACE(This, IDirect3DExecuteBuffer));
596 597
        *obj = ICOM_INTERFACE(This, IDirect3DExecuteBuffer);
	TRACE("  Creating IDirect3DExecuteBuffer interface %p\n", *obj);
598 599 600
	return S_OK;
    }
    FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
601
    return E_NOINTERFACE;
602 603
}

604 605 606 607 608 609 610 611 612 613 614 615

/*****************************************************************************
 * IDirect3DExecuteBuffer::AddRef
 *
 * A normal AddRef method, nothing special
 *
 * Returns:
 *  The new refcount
 *
 *****************************************************************************/
static ULONG WINAPI
IDirect3DExecuteBufferImpl_AddRef(IDirect3DExecuteBuffer *iface)
616
{
617
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
618 619
    ULONG ref = InterlockedIncrement(&This->ref);

620
    FIXME("(%p)->()incrementing from %lu.\n", This, ref - 1);
621 622

    return ref;
623 624
}

625 626 627 628 629 630 631 632 633 634 635
/*****************************************************************************
 * IDirect3DExecuteBuffer::Release
 *
 * A normal Release method, nothing special
 *
 * Returns:
 *  The new refcount
 *
 *****************************************************************************/
static ULONG WINAPI
IDirect3DExecuteBufferImpl_Release(IDirect3DExecuteBuffer *iface)
636 637
{
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
638 639
    ULONG ref = InterlockedDecrement(&This->ref);

640
    TRACE("(%p)->()decrementing from %lu.\n", This, ref + 1);
641 642

    if (!ref) {
643
        if (This->need_free)
644
	    HeapFree(GetProcessHeap(),0,This->desc.lpData);
645 646
        HeapFree(GetProcessHeap(),0,This->vertex_data);
        HeapFree(GetProcessHeap(),0,This->indices);
647 648 649
	HeapFree(GetProcessHeap(),0,This);
	return 0;
    }
650

651
    return ref;
652
}
653

654 655 656 657 658 659 660 661 662 663 664 665 666 667
/*****************************************************************************
 * IDirect3DExecuteBuffer::Initialize
 *
 * Initializes the Execute Buffer. This method exists for COM compliance
 * Nothing to do here.
 *
 * Returns:
 *  D3D_OK
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DExecuteBufferImpl_Initialize(IDirect3DExecuteBuffer *iface,
                                        IDirect3DDevice *lpDirect3DDevice,
                                        D3DEXECUTEBUFFERDESC *lpDesc)
668
{
669
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
670 671
    TRACE("(%p)->(%p,%p) no-op....\n", This, lpDirect3DDevice, lpDesc);
    return D3D_OK;
672 673
}

674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
/*****************************************************************************
 * IDirect3DExecuteBuffer::Lock
 *
 * Locks the buffer, so the app can write into it.
 *
 * Params:
 *  Desc: Pointer to return the buffer description. This Description contains
 *        a pointer to the buffer data.
 *
 * Returns:
 *  This implementation always returns D3D_OK
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DExecuteBufferImpl_Lock(IDirect3DExecuteBuffer *iface,
                                D3DEXECUTEBUFFERDESC *lpDesc)
690 691 692
{
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
    DWORD dwSize;
693
    TRACE("(%p)->(%p)\n", This, lpDesc);
694 695 696 697 698

    dwSize = lpDesc->dwSize;
    memset(lpDesc, 0, dwSize);
    memcpy(lpDesc, &This->desc, dwSize);
    
699
    if (TRACE_ON(d3d7)) {
700
        TRACE("  Returning description :\n");
701 702
	_dump_D3DEXECUTEBUFFERDESC(lpDesc);
    }
703
    return D3D_OK;
704
}
705

706 707 708 709 710 711 712 713 714 715 716
/*****************************************************************************
 * IDirect3DExecuteBuffer::Unlock
 *
 * Unlocks the buffer. We don't have anything to do here
 *
 * Returns:
 *  This implementation always returns D3D_OK
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DExecuteBufferImpl_Unlock(IDirect3DExecuteBuffer *iface)
717 718
{
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
719 720
    TRACE("(%p)->() no-op...\n", This);
    return D3D_OK;
721
}
722

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
/*****************************************************************************
 * IDirect3DExecuteBuffer::SetExecuteData
 *
 * Sets the execute data. This data is used to describe the buffer's content
 *
 * Params:
 *  Data: Pointer to a D3DEXECUTEDATA structure containing the data to
 *  assign
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_OUTOFMEMORY if the vertex buffer allocation failed
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DExecuteBufferImpl_SetExecuteData(IDirect3DExecuteBuffer *iface,
                                          D3DEXECUTEDATA *lpData)
740
{
741 742
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
    DWORD nbvert;
743
    TRACE("(%p)->(%p)\n", This, lpData);
744

745
    memcpy(&This->data, lpData, lpData->dwSize);
746

747 748 749 750
    /* Get the number of vertices in the execute buffer */
    nbvert = This->data.dwVertexCount;
    
    /* Prepares the transformed vertex buffer */
751
    HeapFree(GetProcessHeap(), 0, This->vertex_data);
752
    This->vertex_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbvert * sizeof(D3DTLVERTEX));
753

754
    if (TRACE_ON(d3d7)) {
755 756
        _dump_executedata(lpData);
    }
757

758
    return D3D_OK;
759 760
}

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
/*****************************************************************************
 * IDirect3DExecuteBuffer::GetExecuteData
 *
 * Returns the data in the execute buffer
 *
 * Params:
 *  Data: Pointer to a D3DEXECUTEDATA structure used to return data
 *
 * Returns:
 *  D3D_OK on success
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DExecuteBufferImpl_GetExecuteData(IDirect3DExecuteBuffer *iface,
                                          D3DEXECUTEDATA *lpData)
776
{
777 778
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
    DWORD dwSize;
779
    TRACE("(%p)->(%p): stub!\n", This, lpData);
780

781 782 783
    dwSize = lpData->dwSize;
    memset(lpData, 0, dwSize);
    memcpy(lpData, &This->data, dwSize);
784

785
    if (TRACE_ON(d3d7)) {
786
        TRACE("Returning data :\n");
787 788
	_dump_executedata(lpData);
    }
789

790
    return DD_OK;
791 792
}

793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
/*****************************************************************************
 * IDirect3DExecuteBuffer::Validate
 *
 * DirectX 5 SDK: "The IDirect3DExecuteBuffer::Validate method is not
 * currently implemented"
 *
 * Params:
 *  ?
 *
 * Returns:
 *  DDERR_UNSUPPORTED, because it's not implemented in Windows.
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DExecuteBufferImpl_Validate(IDirect3DExecuteBuffer *iface,
                                    DWORD *Offset,
                                    LPD3DVALIDATECALLBACK Func,
                                    void *UserArg,
                                    DWORD Reserved)
812
{
813
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
814 815
    TRACE("(%p)->(%p,%p,%p,%08lx): Unimplemented!\n", This, Offset, Func, UserArg, Reserved);
    return DDERR_UNSUPPORTED; /* Unchecked */
816 817
}

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
/*****************************************************************************
 * IDirect3DExecuteBuffer::Optimize
 *
 * DirectX5 SDK: "The IDirect3DExecuteBuffer::Optimize method is not
 * currently supported"
 *
 * Params:
 *  Dummy: Seems to be an unused dummy ;)
 *
 * Returns:
 *  DDERR_UNSUPPORTED, because it's not implemented in Windows.
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DExecuteBufferImpl_Optimize(IDirect3DExecuteBuffer *iface,
                                    DWORD Dummy)
834
{
835
    ICOM_THIS_FROM(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, iface);
836 837
    TRACE("(%p)->(%08lx): Unimplemented\n", This, Dummy);
    return DDERR_UNSUPPORTED; /* Unchecked */
838
}
839

840
const IDirect3DExecuteBufferVtbl IDirect3DExecuteBuffer_Vtbl =
841
{
842 843 844 845 846 847 848 849 850 851
    IDirect3DExecuteBufferImpl_QueryInterface,
    IDirect3DExecuteBufferImpl_AddRef,
    IDirect3DExecuteBufferImpl_Release,
    IDirect3DExecuteBufferImpl_Initialize,
    IDirect3DExecuteBufferImpl_Lock,
    IDirect3DExecuteBufferImpl_Unlock,
    IDirect3DExecuteBufferImpl_SetExecuteData,
    IDirect3DExecuteBufferImpl_GetExecuteData,
    IDirect3DExecuteBufferImpl_Validate,
    IDirect3DExecuteBufferImpl_Optimize,
852
};