Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / ddraw / executebuffer.c
blobd4d2dd0b72ffd2195039d63c5309c622d05aa25a
1 /* Direct3D ExecuteBuffer
2 * Copyright (c) 1998-2004 Lionel ULMER
3 * Copyright (c) 2002-2004 Christian Costa
4 * Copyright (c) 2006 Stefan Dösinger
6 * This file contains the implementation of IDirect3DExecuteBuffer.
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include "ddraw_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
30 /*****************************************************************************
31 * _dump_executedata
32 * _dump_D3DEXECUTEBUFFERDESC
34 * Debug functions which write the executebuffer data to the console
36 *****************************************************************************/
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);
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);
53 /*****************************************************************************
54 * IDirect3DExecuteBufferImpl_Execute
56 * The main functionality of the execute buffer
57 * It transforms the vertices if necessary, and calls IDirect3DDevice7
58 * for drawing the vertices. It is called from
59 * IDirect3DDevice::Execute
61 * TODO: Perhaps some comments about the various opcodes wouldn't hurt
63 * Don't declare this static, as it's called from device.c,
64 * IDirect3DDevice::Execute
66 * Params:
67 * Device: 3D Device associated to use for drawing
68 * Viewport: Viewport for this operation
70 *****************************************************************************/
71 void
72 IDirect3DExecuteBufferImpl_Execute(IDirect3DExecuteBufferImpl *This,
73 IDirect3DDeviceImpl *lpDevice,
74 IDirect3DViewportImpl *lpViewport)
76 /* DWORD bs = This->desc.dwBufferSize; */
77 DWORD vs = This->data.dwVertexOffset;
78 /* DWORD vc = This->data.dwVertexCount; */
79 DWORD is = This->data.dwInstructionOffset;
80 /* DWORD il = This->data.dwInstructionLength; */
82 char *instr = (char *)This->desc.lpData + is;
84 /* Should check if the viewport was added or not to the device */
86 /* Activate the viewport */
87 lpViewport->active_device = lpDevice;
88 viewport_activate(lpViewport, FALSE);
90 TRACE("ExecuteData :\n");
91 if (TRACE_ON(ddraw))
92 _dump_executedata(&(This->data));
94 while (1) {
95 LPD3DINSTRUCTION current = (LPD3DINSTRUCTION) instr;
96 BYTE size;
97 WORD count;
99 count = current->wCount;
100 size = current->bSize;
101 instr += sizeof(D3DINSTRUCTION);
103 switch (current->bOpcode) {
104 case D3DOP_POINT: {
105 WARN("POINT-s (%d)\n", count);
106 instr += count * size;
107 } break;
109 case D3DOP_LINE: {
110 WARN("LINE-s (%d)\n", count);
111 instr += count * size;
112 } break;
114 case D3DOP_TRIANGLE: {
115 int i;
116 D3DTLVERTEX *tl_vx = This->vertex_data;
117 TRACE("TRIANGLE (%d)\n", count);
119 if (count*3>This->nb_indices) {
120 This->nb_indices = count * 3;
121 HeapFree(GetProcessHeap(),0,This->indices);
122 This->indices = HeapAlloc(GetProcessHeap(),0,sizeof(WORD)*This->nb_indices);
125 for (i = 0; i < count; i++) {
126 LPD3DTRIANGLE ci = (LPD3DTRIANGLE) instr;
127 TRACE(" v1: %d v2: %d v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3);
128 TRACE(" Flags : ");
129 if (TRACE_ON(ddraw))
131 /* Wireframe */
132 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
133 TRACE("EDGEENABLE1 ");
134 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2)
135 TRACE("EDGEENABLE2 ");
136 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
137 TRACE("EDGEENABLE3 ");
138 /* Strips / Fans */
139 if (ci->wFlags == D3DTRIFLAG_EVEN)
140 TRACE("EVEN ");
141 if (ci->wFlags == D3DTRIFLAG_ODD)
142 TRACE("ODD ");
143 if (ci->wFlags == D3DTRIFLAG_START)
144 TRACE("START ");
145 if ((ci->wFlags > 0) && (ci->wFlags < 30))
146 TRACE("STARTFLAT(%u) ", ci->wFlags);
147 TRACE("\n");
149 This->indices[(i * 3) ] = ci->u1.v1;
150 This->indices[(i * 3) + 1] = ci->u2.v2;
151 This->indices[(i * 3) + 2] = ci->u3.v3;
152 instr += size;
154 /* IDirect3DDevices have color keying always enabled -
155 * enable it before drawing. This overwrites any ALPHA*
156 * render state
158 IWineD3DDevice_SetRenderState(lpDevice->wineD3DDevice,
159 WINED3DRS_COLORKEYENABLE,
161 IDirect3DDevice7_DrawIndexedPrimitive((IDirect3DDevice7 *)lpDevice,
162 D3DPT_TRIANGLELIST, D3DFVF_TLVERTEX, tl_vx, 0, This->indices, count * 3, 0);
163 } break;
165 case D3DOP_MATRIXLOAD:
166 WARN("MATRIXLOAD-s (%d)\n", count);
167 instr += count * size;
168 break;
170 case D3DOP_MATRIXMULTIPLY: {
171 int i;
172 TRACE("MATRIXMULTIPLY (%d)\n", count);
174 for (i = 0; i < count; ++i)
176 D3DMATRIXMULTIPLY *ci = (D3DMATRIXMULTIPLY *)instr;
177 D3DMATRIX *a, *b, *c;
179 a = ddraw_get_object(&lpDevice->handle_table, ci->hDestMatrix - 1, DDRAW_HANDLE_MATRIX);
180 b = ddraw_get_object(&lpDevice->handle_table, ci->hSrcMatrix1 - 1, DDRAW_HANDLE_MATRIX);
181 c = ddraw_get_object(&lpDevice->handle_table, ci->hSrcMatrix2 - 1, DDRAW_HANDLE_MATRIX);
183 if (!a || !b || !c)
185 ERR("Invalid matrix handle (a %#x -> %p, b %#x -> %p, c %#x -> %p).\n",
186 ci->hDestMatrix, a, ci->hSrcMatrix1, b, ci->hSrcMatrix2, c);
188 else
190 TRACE("dst %p, src1 %p, src2 %p.\n", a, b, c);
191 multiply_matrix(a, c, b);
194 instr += size;
196 } break;
198 case D3DOP_STATETRANSFORM: {
199 int i;
200 TRACE("STATETRANSFORM (%d)\n", count);
202 for (i = 0; i < count; ++i)
204 D3DSTATE *ci = (D3DSTATE *)instr;
205 D3DMATRIX *m;
207 m = ddraw_get_object(&lpDevice->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATRIX);
208 if (!m)
210 ERR("Invalid matrix handle %#x.\n", ci->u2.dwArg[0]);
212 else
214 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_WORLD)
215 lpDevice->world = ci->u2.dwArg[0];
216 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_VIEW)
217 lpDevice->view = ci->u2.dwArg[0];
218 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_PROJECTION)
219 lpDevice->proj = ci->u2.dwArg[0];
220 IDirect3DDevice7_SetTransform((IDirect3DDevice7 *)lpDevice,
221 ci->u1.dtstTransformStateType, m);
224 instr += size;
226 } break;
228 case D3DOP_STATELIGHT: {
229 int i;
230 TRACE("STATELIGHT (%d)\n", count);
232 for (i = 0; i < count; i++) {
233 LPD3DSTATE ci = (LPD3DSTATE) instr;
235 TRACE("(%08x,%08x)\n", ci->u1.dlstLightStateType, ci->u2.dwArg[0]);
237 if (!ci->u1.dlstLightStateType || (ci->u1.dlstLightStateType > D3DLIGHTSTATE_COLORVERTEX))
238 ERR("Unexpected Light State Type %d\n", ci->u1.dlstLightStateType);
239 else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
241 IDirect3DMaterialImpl *m;
243 m = ddraw_get_object(&lpDevice->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATERIAL);
244 if (!m)
245 ERR("Invalid material handle %#x.\n", ci->u2.dwArg[0]);
246 else
247 material_activate(m);
249 else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
251 switch (ci->u2.dwArg[0]) {
252 case D3DCOLOR_MONO:
253 ERR("DDCOLOR_MONO should not happen!\n");
254 break;
255 case D3DCOLOR_RGB:
256 /* We are already in this mode */
257 break;
258 default:
259 ERR("Unknown color model!\n");
261 } else {
262 D3DRENDERSTATETYPE rs = 0;
263 switch (ci->u1.dlstLightStateType) {
265 case D3DLIGHTSTATE_AMBIENT: /* 2 */
266 rs = D3DRENDERSTATE_AMBIENT;
267 break;
268 case D3DLIGHTSTATE_FOGMODE: /* 4 */
269 rs = D3DRENDERSTATE_FOGVERTEXMODE;
270 break;
271 case D3DLIGHTSTATE_FOGSTART: /* 5 */
272 rs = D3DRENDERSTATE_FOGSTART;
273 break;
274 case D3DLIGHTSTATE_FOGEND: /* 6 */
275 rs = D3DRENDERSTATE_FOGEND;
276 break;
277 case D3DLIGHTSTATE_FOGDENSITY: /* 7 */
278 rs = D3DRENDERSTATE_FOGDENSITY;
279 break;
280 case D3DLIGHTSTATE_COLORVERTEX: /* 8 */
281 rs = D3DRENDERSTATE_COLORVERTEX;
282 break;
283 default:
284 break;
287 IDirect3DDevice7_SetRenderState((IDirect3DDevice7 *)lpDevice, rs, ci->u2.dwArg[0]);
290 instr += size;
292 } break;
294 case D3DOP_STATERENDER: {
295 int i;
296 IDirect3DDevice2 *d3d_device2 = (IDirect3DDevice2 *)&lpDevice->IDirect3DDevice2_vtbl;
297 TRACE("STATERENDER (%d)\n", count);
299 for (i = 0; i < count; i++) {
300 LPD3DSTATE ci = (LPD3DSTATE) instr;
302 IDirect3DDevice2_SetRenderState(d3d_device2, ci->u1.drstRenderStateType, ci->u2.dwArg[0]);
304 instr += size;
306 } break;
308 case D3DOP_PROCESSVERTICES:
310 /* TODO: Share code with IDirect3DVertexBuffer::ProcessVertices and / or
311 * IWineD3DDevice::ProcessVertices
313 int i;
314 D3DMATRIX view_mat, world_mat, proj_mat;
315 TRACE("PROCESSVERTICES (%d)\n", count);
317 /* Get the transform and world matrix */
318 /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
320 IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
321 D3DTRANSFORMSTATE_VIEW,
322 (WINED3DMATRIX*) &view_mat);
324 IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
325 D3DTRANSFORMSTATE_PROJECTION,
326 (WINED3DMATRIX*) &proj_mat);
328 IWineD3DDevice_GetTransform(lpDevice->wineD3DDevice,
329 WINED3DTS_WORLDMATRIX(0),
330 (WINED3DMATRIX*) &world_mat);
332 for (i = 0; i < count; i++) {
333 LPD3DPROCESSVERTICES ci = (LPD3DPROCESSVERTICES) instr;
335 TRACE(" Start : %d Dest : %d Count : %d\n",
336 ci->wStart, ci->wDest, ci->dwCount);
337 TRACE(" Flags : ");
338 if (TRACE_ON(ddraw))
340 if (ci->dwFlags & D3DPROCESSVERTICES_COPY)
341 TRACE("COPY ");
342 if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
343 TRACE("NOCOLOR ");
344 if (ci->dwFlags == D3DPROCESSVERTICES_OPMASK)
345 TRACE("OPMASK ");
346 if (ci->dwFlags & D3DPROCESSVERTICES_TRANSFORM)
347 TRACE("TRANSFORM ");
348 if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT)
349 TRACE("TRANSFORMLIGHT ");
350 if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
351 TRACE("UPDATEEXTENTS ");
352 TRACE("\n");
355 /* This is where doing Direct3D on top on OpenGL is quite difficult.
356 This method transforms a set of vertices using the CURRENT state
357 (lighting, projection, ...) but does not rasterize them.
358 They will only be put on screen later (with the POINT / LINE and
359 TRIANGLE op-codes). The problem is that you can have a triangle
360 with each point having been transformed using another state...
362 In this implementation, I will emulate only ONE thing : each
363 vertex can have its own "WORLD" transformation (this is used in the
364 TWIST.EXE demo of the 5.2 SDK). I suppose that all vertices of the
365 execute buffer use the same state.
367 If I find applications that change other states, I will try to do a
368 more 'fine-tuned' state emulation (but I may become quite tricky if
369 it changes a light position in the middle of a triangle).
371 In this case, a 'direct' approach (i.e. without using OpenGL, but
372 writing our own 3D rasterizer) would be easier. */
374 /* The current method (with the hypothesis that only the WORLD matrix
375 will change between two points) is like this :
376 - I transform 'manually' all the vertices with the current WORLD
377 matrix and store them in the vertex buffer
378 - during the rasterization phase, the WORLD matrix will be set to
379 the Identity matrix */
381 /* Enough for the moment */
382 if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) {
383 unsigned int nb;
384 D3DVERTEX *src = ((LPD3DVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
385 D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
386 D3DMATRIX mat;
387 D3DVIEWPORT* Viewport = &lpViewport->viewports.vp1;
389 if (TRACE_ON(ddraw))
391 TRACE(" Projection Matrix : (%p)\n", &proj_mat);
392 dump_D3DMATRIX(&proj_mat);
393 TRACE(" View Matrix : (%p)\n", &view_mat);
394 dump_D3DMATRIX(&view_mat);
395 TRACE(" World Matrix : (%p)\n", &world_mat);
396 dump_D3DMATRIX(&world_mat);
399 multiply_matrix(&mat,&view_mat,&world_mat);
400 multiply_matrix(&mat,&proj_mat,&mat);
402 for (nb = 0; nb < ci->dwCount; nb++) {
403 /* No lighting yet */
404 dst->u5.color = 0xFFFFFFFF; /* Opaque white */
405 dst->u6.specular = 0xFF000000; /* No specular and no fog factor */
407 dst->u7.tu = src->u7.tu;
408 dst->u8.tv = src->u8.tv;
410 /* Now, the matrix multiplication */
411 dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
412 dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
413 dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
414 dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);
416 dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX
417 + Viewport->dwX + Viewport->dwWidth / 2;
418 dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
419 + Viewport->dwY + Viewport->dwHeight / 2;
420 dst->u3.sz /= dst->u4.rhw;
421 dst->u4.rhw = 1 / dst->u4.rhw;
423 src++;
424 dst++;
427 } else if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORM) {
428 unsigned int nb;
429 D3DLVERTEX *src = ((LPD3DLVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
430 D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
431 D3DMATRIX mat;
432 D3DVIEWPORT* Viewport = &lpViewport->viewports.vp1;
434 if (TRACE_ON(ddraw))
436 TRACE(" Projection Matrix : (%p)\n", &proj_mat);
437 dump_D3DMATRIX(&proj_mat);
438 TRACE(" View Matrix : (%p)\n",&view_mat);
439 dump_D3DMATRIX(&view_mat);
440 TRACE(" World Matrix : (%p)\n", &world_mat);
441 dump_D3DMATRIX(&world_mat);
444 multiply_matrix(&mat,&view_mat,&world_mat);
445 multiply_matrix(&mat,&proj_mat,&mat);
447 for (nb = 0; nb < ci->dwCount; nb++) {
448 dst->u5.color = src->u4.color;
449 dst->u6.specular = src->u5.specular;
450 dst->u7.tu = src->u6.tu;
451 dst->u8.tv = src->u7.tv;
453 /* Now, the matrix multiplication */
454 dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
455 dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
456 dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
457 dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);
459 dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX
460 + Viewport->dwX + Viewport->dwWidth / 2;
461 dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
462 + Viewport->dwY + Viewport->dwHeight / 2;
464 dst->u3.sz /= dst->u4.rhw;
465 dst->u4.rhw = 1 / dst->u4.rhw;
467 src++;
468 dst++;
470 } else if (ci->dwFlags == D3DPROCESSVERTICES_COPY) {
471 D3DTLVERTEX *src = ((LPD3DTLVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
472 D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
474 memcpy(dst, src, ci->dwCount * sizeof(D3DTLVERTEX));
475 } else {
476 ERR("Unhandled vertex processing flag %#x.\n", ci->dwFlags);
479 instr += size;
481 } break;
483 case D3DOP_TEXTURELOAD: {
484 WARN("TEXTURELOAD-s (%d)\n", count);
486 instr += count * size;
487 } break;
489 case D3DOP_EXIT: {
490 TRACE("EXIT (%d)\n", count);
491 /* We did this instruction */
492 instr += size;
493 /* Exit this loop */
494 goto end_of_buffer;
495 } break;
497 case D3DOP_BRANCHFORWARD: {
498 int i;
499 TRACE("BRANCHFORWARD (%d)\n", count);
501 for (i = 0; i < count; i++) {
502 LPD3DBRANCH ci = (LPD3DBRANCH) instr;
504 if ((This->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue) {
505 if (!ci->bNegate) {
506 TRACE(" Branch to %d\n", ci->dwOffset);
507 if (ci->dwOffset) {
508 instr = (char*)current + ci->dwOffset;
509 break;
512 } else {
513 if (ci->bNegate) {
514 TRACE(" Branch to %d\n", ci->dwOffset);
515 if (ci->dwOffset) {
516 instr = (char*)current + ci->dwOffset;
517 break;
522 instr += size;
524 } break;
526 case D3DOP_SPAN: {
527 WARN("SPAN-s (%d)\n", count);
529 instr += count * size;
530 } break;
532 case D3DOP_SETSTATUS: {
533 int i;
534 TRACE("SETSTATUS (%d)\n", count);
536 for (i = 0; i < count; i++) {
537 LPD3DSTATUS ci = (LPD3DSTATUS) instr;
539 This->data.dsStatus = *ci;
541 instr += size;
543 } break;
545 default:
546 ERR("Unhandled OpCode %d !!!\n",current->bOpcode);
547 /* Try to save ... */
548 instr += count * size;
549 break;
553 end_of_buffer:
557 /*****************************************************************************
558 * IDirect3DExecuteBuffer::QueryInterface
560 * Well, a usual QueryInterface function. Don't know fur sure which
561 * interfaces it can Query.
563 * Params:
564 * riid: The interface ID queried for
565 * obj: Address to return the interface pointer at
567 * Returns:
568 * D3D_OK in case of a success (S_OK? Think it's the same)
569 * OLE_E_ENUM_NOMORE if the interface wasn't found.
570 * (E_NOINTERFACE?? Don't know what I really need)
572 *****************************************************************************/
573 static HRESULT WINAPI
574 IDirect3DExecuteBufferImpl_QueryInterface(IDirect3DExecuteBuffer *iface,
575 REFIID riid,
576 void **obj)
578 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
580 *obj = NULL;
582 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
583 IDirect3DExecuteBuffer_AddRef(iface);
584 *obj = iface;
585 TRACE(" Creating IUnknown interface at %p.\n", *obj);
586 return S_OK;
588 if ( IsEqualGUID( &IID_IDirect3DExecuteBuffer, riid ) ) {
589 IDirect3DExecuteBuffer_AddRef(iface);
590 *obj = iface;
591 TRACE(" Creating IDirect3DExecuteBuffer interface %p\n", *obj);
592 return S_OK;
594 FIXME("(%p): interface for IID %s NOT found!\n", iface, debugstr_guid(riid));
595 return E_NOINTERFACE;
599 /*****************************************************************************
600 * IDirect3DExecuteBuffer::AddRef
602 * A normal AddRef method, nothing special
604 * Returns:
605 * The new refcount
607 *****************************************************************************/
608 static ULONG WINAPI
609 IDirect3DExecuteBufferImpl_AddRef(IDirect3DExecuteBuffer *iface)
611 IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
612 ULONG ref = InterlockedIncrement(&This->ref);
614 TRACE("%p increasing refcount to %u.\n", This, ref);
616 return ref;
619 /*****************************************************************************
620 * IDirect3DExecuteBuffer::Release
622 * A normal Release method, nothing special
624 * Returns:
625 * The new refcount
627 *****************************************************************************/
628 static ULONG WINAPI
629 IDirect3DExecuteBufferImpl_Release(IDirect3DExecuteBuffer *iface)
631 IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
632 ULONG ref = InterlockedDecrement(&This->ref);
634 TRACE("%p decreasing refcount to %u.\n", This, ref);
636 if (!ref) {
637 if (This->need_free)
638 HeapFree(GetProcessHeap(),0,This->desc.lpData);
639 HeapFree(GetProcessHeap(),0,This->vertex_data);
640 HeapFree(GetProcessHeap(),0,This->indices);
641 HeapFree(GetProcessHeap(),0,This);
642 return 0;
645 return ref;
648 /*****************************************************************************
649 * IDirect3DExecuteBuffer::Initialize
651 * Initializes the Execute Buffer. This method exists for COM compliance
652 * Nothing to do here.
654 * Returns:
655 * D3D_OK
657 *****************************************************************************/
658 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Initialize(IDirect3DExecuteBuffer *iface,
659 IDirect3DDevice *device, D3DEXECUTEBUFFERDESC *desc)
661 TRACE("iface %p, device %p, desc %p.\n", iface, device, desc);
663 return D3D_OK;
666 /*****************************************************************************
667 * IDirect3DExecuteBuffer::Lock
669 * Locks the buffer, so the app can write into it.
671 * Params:
672 * Desc: Pointer to return the buffer description. This Description contains
673 * a pointer to the buffer data.
675 * Returns:
676 * This implementation always returns D3D_OK
678 *****************************************************************************/
679 static HRESULT WINAPI
680 IDirect3DExecuteBufferImpl_Lock(IDirect3DExecuteBuffer *iface,
681 D3DEXECUTEBUFFERDESC *lpDesc)
683 IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
684 DWORD dwSize;
686 TRACE("iface %p, desc %p.\n", iface, lpDesc);
688 dwSize = lpDesc->dwSize;
689 memcpy(lpDesc, &This->desc, dwSize);
691 if (TRACE_ON(ddraw))
693 TRACE(" Returning description :\n");
694 _dump_D3DEXECUTEBUFFERDESC(lpDesc);
696 return D3D_OK;
699 /*****************************************************************************
700 * IDirect3DExecuteBuffer::Unlock
702 * Unlocks the buffer. We don't have anything to do here
704 * Returns:
705 * This implementation always returns D3D_OK
707 *****************************************************************************/
708 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Unlock(IDirect3DExecuteBuffer *iface)
710 TRACE("iface %p.\n", iface);
712 return D3D_OK;
715 /*****************************************************************************
716 * IDirect3DExecuteBuffer::SetExecuteData
718 * Sets the execute data. This data is used to describe the buffer's content
720 * Params:
721 * Data: Pointer to a D3DEXECUTEDATA structure containing the data to
722 * assign
724 * Returns:
725 * D3D_OK on success
726 * DDERR_OUTOFMEMORY if the vertex buffer allocation failed
728 *****************************************************************************/
729 static HRESULT WINAPI
730 IDirect3DExecuteBufferImpl_SetExecuteData(IDirect3DExecuteBuffer *iface,
731 D3DEXECUTEDATA *lpData)
733 IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
734 DWORD nbvert;
736 TRACE("iface %p, data %p.\n", iface, lpData);
738 memcpy(&This->data, lpData, lpData->dwSize);
740 /* Get the number of vertices in the execute buffer */
741 nbvert = This->data.dwVertexCount;
743 /* Prepares the transformed vertex buffer */
744 HeapFree(GetProcessHeap(), 0, This->vertex_data);
745 This->vertex_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbvert * sizeof(D3DTLVERTEX));
747 if (TRACE_ON(ddraw))
748 _dump_executedata(lpData);
750 return D3D_OK;
753 /*****************************************************************************
754 * IDirect3DExecuteBuffer::GetExecuteData
756 * Returns the data in the execute buffer
758 * Params:
759 * Data: Pointer to a D3DEXECUTEDATA structure used to return data
761 * Returns:
762 * D3D_OK on success
764 *****************************************************************************/
765 static HRESULT WINAPI
766 IDirect3DExecuteBufferImpl_GetExecuteData(IDirect3DExecuteBuffer *iface,
767 D3DEXECUTEDATA *lpData)
769 IDirect3DExecuteBufferImpl *This = (IDirect3DExecuteBufferImpl *)iface;
770 DWORD dwSize;
772 TRACE("iface %p, data %p.\n", iface, lpData);
774 dwSize = lpData->dwSize;
775 memcpy(lpData, &This->data, dwSize);
777 if (TRACE_ON(ddraw))
779 TRACE("Returning data :\n");
780 _dump_executedata(lpData);
783 return DD_OK;
786 /*****************************************************************************
787 * IDirect3DExecuteBuffer::Validate
789 * DirectX 5 SDK: "The IDirect3DExecuteBuffer::Validate method is not
790 * currently implemented"
792 * Params:
795 * Returns:
796 * DDERR_UNSUPPORTED, because it's not implemented in Windows.
798 *****************************************************************************/
799 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Validate(IDirect3DExecuteBuffer *iface,
800 DWORD *offset, LPD3DVALIDATECALLBACK callback, void *context, DWORD reserved)
802 TRACE("iface %p, offset %p, callback %p, context %p, reserved %#x.\n",
803 iface, offset, callback, context, reserved);
805 WARN("Not implemented.\n");
807 return DDERR_UNSUPPORTED; /* Unchecked */
810 /*****************************************************************************
811 * IDirect3DExecuteBuffer::Optimize
813 * DirectX5 SDK: "The IDirect3DExecuteBuffer::Optimize method is not
814 * currently supported"
816 * Params:
817 * Dummy: Seems to be an unused dummy ;)
819 * Returns:
820 * DDERR_UNSUPPORTED, because it's not implemented in Windows.
822 *****************************************************************************/
823 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Optimize(IDirect3DExecuteBuffer *iface, DWORD reserved)
825 TRACE("iface %p, reserved %#x.\n", iface, reserved);
827 WARN("Not implemented.\n");
829 return DDERR_UNSUPPORTED; /* Unchecked */
832 static const struct IDirect3DExecuteBufferVtbl d3d_execute_buffer_vtbl =
834 IDirect3DExecuteBufferImpl_QueryInterface,
835 IDirect3DExecuteBufferImpl_AddRef,
836 IDirect3DExecuteBufferImpl_Release,
837 IDirect3DExecuteBufferImpl_Initialize,
838 IDirect3DExecuteBufferImpl_Lock,
839 IDirect3DExecuteBufferImpl_Unlock,
840 IDirect3DExecuteBufferImpl_SetExecuteData,
841 IDirect3DExecuteBufferImpl_GetExecuteData,
842 IDirect3DExecuteBufferImpl_Validate,
843 IDirect3DExecuteBufferImpl_Optimize,
846 HRESULT d3d_execute_buffer_init(IDirect3DExecuteBufferImpl *execute_buffer,
847 IDirect3DDeviceImpl *device, D3DEXECUTEBUFFERDESC *desc)
849 execute_buffer->lpVtbl = &d3d_execute_buffer_vtbl;
850 execute_buffer->ref = 1;
851 execute_buffer->d3ddev = device;
853 /* Initializes memory */
854 memcpy(&execute_buffer->desc, desc, desc->dwSize);
856 /* No buffer given */
857 if (!(execute_buffer->desc.dwFlags & D3DDEB_LPDATA))
858 execute_buffer->desc.lpData = NULL;
860 /* No buffer size given */
861 if (!(execute_buffer->desc.dwFlags & D3DDEB_BUFSIZE))
862 execute_buffer->desc.dwBufferSize = 0;
864 /* Create buffer if asked */
865 if (!execute_buffer->desc.lpData && execute_buffer->desc.dwBufferSize)
867 execute_buffer->need_free = TRUE;
868 execute_buffer->desc.lpData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, execute_buffer->desc.dwBufferSize);
869 if (!execute_buffer->desc.lpData)
871 ERR("Failed to allocate execute buffer data.\n");
872 return DDERR_OUTOFMEMORY;
876 execute_buffer->desc.dwFlags |= D3DDEB_LPDATA;
878 return D3D_OK;