po: Update the English (neutral / Great Britain) translation.
[wine/multimedia.git] / dlls / ddraw / executebuffer.c
blob74a4984e6241eaa0be9cebb6aa8274923047ff12
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 HRESULT d3d_execute_buffer_execute(IDirect3DExecuteBufferImpl *This,
72 IDirect3DDeviceImpl *lpDevice, IDirect3DViewportImpl *lpViewport)
74 /* DWORD bs = This->desc.dwBufferSize; */
75 DWORD vs = This->data.dwVertexOffset;
76 /* DWORD vc = This->data.dwVertexCount; */
77 DWORD is = This->data.dwInstructionOffset;
78 /* DWORD il = This->data.dwInstructionLength; */
80 char *instr = (char *)This->desc.lpData + is;
82 if (lpViewport->active_device != lpDevice)
84 WARN("Viewport %p active device is %p.\n",
85 lpViewport, lpViewport->active_device);
86 return DDERR_INVALIDPARAMS;
89 /* Activate the viewport */
90 viewport_activate(lpViewport, FALSE);
92 TRACE("ExecuteData :\n");
93 if (TRACE_ON(ddraw))
94 _dump_executedata(&(This->data));
96 while (1) {
97 LPD3DINSTRUCTION current = (LPD3DINSTRUCTION) instr;
98 BYTE size;
99 WORD count;
101 count = current->wCount;
102 size = current->bSize;
103 instr += sizeof(D3DINSTRUCTION);
105 switch (current->bOpcode) {
106 case D3DOP_POINT: {
107 WARN("POINT-s (%d)\n", count);
108 instr += count * size;
109 } break;
111 case D3DOP_LINE: {
112 WARN("LINE-s (%d)\n", count);
113 instr += count * size;
114 } break;
116 case D3DOP_TRIANGLE: {
117 int i;
118 D3DTLVERTEX *tl_vx = This->vertex_data;
119 TRACE("TRIANGLE (%d)\n", count);
121 if (count*3>This->nb_indices) {
122 This->nb_indices = count * 3;
123 HeapFree(GetProcessHeap(),0,This->indices);
124 This->indices = HeapAlloc(GetProcessHeap(),0,sizeof(WORD)*This->nb_indices);
127 for (i = 0; i < count; i++) {
128 LPD3DTRIANGLE ci = (LPD3DTRIANGLE) instr;
129 TRACE(" v1: %d v2: %d v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3);
130 TRACE(" Flags : ");
131 if (TRACE_ON(ddraw))
133 /* Wireframe */
134 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
135 TRACE("EDGEENABLE1 ");
136 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2)
137 TRACE("EDGEENABLE2 ");
138 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
139 TRACE("EDGEENABLE3 ");
140 /* Strips / Fans */
141 if (ci->wFlags == D3DTRIFLAG_EVEN)
142 TRACE("EVEN ");
143 if (ci->wFlags == D3DTRIFLAG_ODD)
144 TRACE("ODD ");
145 if (ci->wFlags == D3DTRIFLAG_START)
146 TRACE("START ");
147 if ((ci->wFlags > 0) && (ci->wFlags < 30))
148 TRACE("STARTFLAT(%u) ", ci->wFlags);
149 TRACE("\n");
151 This->indices[(i * 3) ] = ci->u1.v1;
152 This->indices[(i * 3) + 1] = ci->u2.v2;
153 This->indices[(i * 3) + 2] = ci->u3.v3;
154 instr += size;
156 /* IDirect3DDevices have color keying always enabled -
157 * enable it before drawing. This overwrites any ALPHA*
158 * render state. */
159 wined3d_device_set_render_state(lpDevice->wined3d_device, WINED3D_RS_COLORKEYENABLE, 1);
160 IDirect3DDevice7_DrawIndexedPrimitive(&lpDevice->IDirect3DDevice7_iface,
161 D3DPT_TRIANGLELIST, D3DFVF_TLVERTEX, tl_vx, 0, This->indices, count * 3, 0);
162 } break;
164 case D3DOP_MATRIXLOAD:
165 WARN("MATRIXLOAD-s (%d)\n", count);
166 instr += count * size;
167 break;
169 case D3DOP_MATRIXMULTIPLY: {
170 int i;
171 TRACE("MATRIXMULTIPLY (%d)\n", count);
173 for (i = 0; i < count; ++i)
175 D3DMATRIXMULTIPLY *ci = (D3DMATRIXMULTIPLY *)instr;
176 D3DMATRIX *a, *b, *c;
178 a = ddraw_get_object(&lpDevice->handle_table, ci->hDestMatrix - 1, DDRAW_HANDLE_MATRIX);
179 b = ddraw_get_object(&lpDevice->handle_table, ci->hSrcMatrix1 - 1, DDRAW_HANDLE_MATRIX);
180 c = ddraw_get_object(&lpDevice->handle_table, ci->hSrcMatrix2 - 1, DDRAW_HANDLE_MATRIX);
182 if (!a || !b || !c)
184 ERR("Invalid matrix handle (a %#x -> %p, b %#x -> %p, c %#x -> %p).\n",
185 ci->hDestMatrix, a, ci->hSrcMatrix1, b, ci->hSrcMatrix2, c);
187 else
189 TRACE("dst %p, src1 %p, src2 %p.\n", a, b, c);
190 multiply_matrix(a, c, b);
193 instr += size;
195 } break;
197 case D3DOP_STATETRANSFORM: {
198 int i;
199 TRACE("STATETRANSFORM (%d)\n", count);
201 for (i = 0; i < count; ++i)
203 D3DSTATE *ci = (D3DSTATE *)instr;
204 D3DMATRIX *m;
206 m = ddraw_get_object(&lpDevice->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATRIX);
207 if (!m)
209 ERR("Invalid matrix handle %#x.\n", ci->u2.dwArg[0]);
211 else
213 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_WORLD)
214 lpDevice->world = ci->u2.dwArg[0];
215 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_VIEW)
216 lpDevice->view = ci->u2.dwArg[0];
217 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_PROJECTION)
218 lpDevice->proj = ci->u2.dwArg[0];
219 IDirect3DDevice7_SetTransform(&lpDevice->IDirect3DDevice7_iface,
220 ci->u1.dtstTransformStateType, m);
223 instr += size;
225 } break;
227 case D3DOP_STATELIGHT: {
228 int i;
229 TRACE("STATELIGHT (%d)\n", count);
231 for (i = 0; i < count; i++) {
232 LPD3DSTATE ci = (LPD3DSTATE) instr;
234 TRACE("(%08x,%08x)\n", ci->u1.dlstLightStateType, ci->u2.dwArg[0]);
236 if (!ci->u1.dlstLightStateType || (ci->u1.dlstLightStateType > D3DLIGHTSTATE_COLORVERTEX))
237 ERR("Unexpected Light State Type %d\n", ci->u1.dlstLightStateType);
238 else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
240 IDirect3DMaterialImpl *m;
242 m = ddraw_get_object(&lpDevice->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATERIAL);
243 if (!m)
244 ERR("Invalid material handle %#x.\n", ci->u2.dwArg[0]);
245 else
246 material_activate(m);
248 else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
250 switch (ci->u2.dwArg[0]) {
251 case D3DCOLOR_MONO:
252 ERR("DDCOLOR_MONO should not happen!\n");
253 break;
254 case D3DCOLOR_RGB:
255 /* We are already in this mode */
256 break;
257 default:
258 ERR("Unknown color model!\n");
260 } else {
261 D3DRENDERSTATETYPE rs = 0;
262 switch (ci->u1.dlstLightStateType) {
264 case D3DLIGHTSTATE_AMBIENT: /* 2 */
265 rs = D3DRENDERSTATE_AMBIENT;
266 break;
267 case D3DLIGHTSTATE_FOGMODE: /* 4 */
268 rs = D3DRENDERSTATE_FOGVERTEXMODE;
269 break;
270 case D3DLIGHTSTATE_FOGSTART: /* 5 */
271 rs = D3DRENDERSTATE_FOGSTART;
272 break;
273 case D3DLIGHTSTATE_FOGEND: /* 6 */
274 rs = D3DRENDERSTATE_FOGEND;
275 break;
276 case D3DLIGHTSTATE_FOGDENSITY: /* 7 */
277 rs = D3DRENDERSTATE_FOGDENSITY;
278 break;
279 case D3DLIGHTSTATE_COLORVERTEX: /* 8 */
280 rs = D3DRENDERSTATE_COLORVERTEX;
281 break;
282 default:
283 break;
286 IDirect3DDevice7_SetRenderState(&lpDevice->IDirect3DDevice7_iface, rs, ci->u2.dwArg[0]);
289 instr += size;
291 } break;
293 case D3DOP_STATERENDER: {
294 int i;
295 IDirect3DDevice2 *d3d_device2 = &lpDevice->IDirect3DDevice2_iface;
296 TRACE("STATERENDER (%d)\n", count);
298 for (i = 0; i < count; i++) {
299 LPD3DSTATE ci = (LPD3DSTATE) instr;
301 IDirect3DDevice2_SetRenderState(d3d_device2, ci->u1.drstRenderStateType, ci->u2.dwArg[0]);
303 instr += size;
305 } break;
307 case D3DOP_PROCESSVERTICES:
309 /* TODO: Share code with IDirect3DVertexBuffer::ProcessVertices and / or
310 * IWineD3DDevice::ProcessVertices
312 int i;
313 D3DMATRIX view_mat, world_mat, proj_mat;
314 TRACE("PROCESSVERTICES (%d)\n", count);
316 /* Get the transform and world matrix */
317 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
318 wined3d_device_get_transform(lpDevice->wined3d_device,
319 D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat);
320 wined3d_device_get_transform(lpDevice->wined3d_device,
321 D3DTRANSFORMSTATE_PROJECTION, (struct wined3d_matrix *)&proj_mat);
322 wined3d_device_get_transform(lpDevice->wined3d_device,
323 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat);
325 for (i = 0; i < count; i++) {
326 LPD3DPROCESSVERTICES ci = (LPD3DPROCESSVERTICES) instr;
328 TRACE(" Start : %d Dest : %d Count : %d\n",
329 ci->wStart, ci->wDest, ci->dwCount);
330 TRACE(" Flags : ");
331 if (TRACE_ON(ddraw))
333 if (ci->dwFlags & D3DPROCESSVERTICES_COPY)
334 TRACE("COPY ");
335 if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
336 TRACE("NOCOLOR ");
337 if (ci->dwFlags == D3DPROCESSVERTICES_OPMASK)
338 TRACE("OPMASK ");
339 if (ci->dwFlags & D3DPROCESSVERTICES_TRANSFORM)
340 TRACE("TRANSFORM ");
341 if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT)
342 TRACE("TRANSFORMLIGHT ");
343 if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
344 TRACE("UPDATEEXTENTS ");
345 TRACE("\n");
348 /* This is where doing Direct3D on top on OpenGL is quite difficult.
349 This method transforms a set of vertices using the CURRENT state
350 (lighting, projection, ...) but does not rasterize them.
351 They will only be put on screen later (with the POINT / LINE and
352 TRIANGLE op-codes). The problem is that you can have a triangle
353 with each point having been transformed using another state...
355 In this implementation, I will emulate only ONE thing : each
356 vertex can have its own "WORLD" transformation (this is used in the
357 TWIST.EXE demo of the 5.2 SDK). I suppose that all vertices of the
358 execute buffer use the same state.
360 If I find applications that change other states, I will try to do a
361 more 'fine-tuned' state emulation (but I may become quite tricky if
362 it changes a light position in the middle of a triangle).
364 In this case, a 'direct' approach (i.e. without using OpenGL, but
365 writing our own 3D rasterizer) would be easier. */
367 /* The current method (with the hypothesis that only the WORLD matrix
368 will change between two points) is like this :
369 - I transform 'manually' all the vertices with the current WORLD
370 matrix and store them in the vertex buffer
371 - during the rasterization phase, the WORLD matrix will be set to
372 the Identity matrix */
374 /* Enough for the moment */
375 if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) {
376 unsigned int nb;
377 D3DVERTEX *src = ((LPD3DVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
378 D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
379 D3DMATRIX mat;
380 D3DVIEWPORT* Viewport = &lpViewport->viewports.vp1;
382 if (TRACE_ON(ddraw))
384 TRACE(" Projection Matrix : (%p)\n", &proj_mat);
385 dump_D3DMATRIX(&proj_mat);
386 TRACE(" View Matrix : (%p)\n", &view_mat);
387 dump_D3DMATRIX(&view_mat);
388 TRACE(" World Matrix : (%p)\n", &world_mat);
389 dump_D3DMATRIX(&world_mat);
392 multiply_matrix(&mat,&view_mat,&world_mat);
393 multiply_matrix(&mat,&proj_mat,&mat);
395 for (nb = 0; nb < ci->dwCount; nb++) {
396 /* No lighting yet */
397 dst->u5.color = 0xFFFFFFFF; /* Opaque white */
398 dst->u6.specular = 0xFF000000; /* No specular and no fog factor */
400 dst->u7.tu = src->u7.tu;
401 dst->u8.tv = src->u8.tv;
403 /* Now, the matrix multiplication */
404 dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
405 dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
406 dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
407 dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);
409 dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX
410 + Viewport->dwX + Viewport->dwWidth / 2;
411 dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
412 + Viewport->dwY + Viewport->dwHeight / 2;
413 dst->u3.sz /= dst->u4.rhw;
414 dst->u4.rhw = 1 / dst->u4.rhw;
416 src++;
417 dst++;
420 } else if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORM) {
421 unsigned int nb;
422 D3DLVERTEX *src = ((LPD3DLVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
423 D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
424 D3DMATRIX mat;
425 D3DVIEWPORT* Viewport = &lpViewport->viewports.vp1;
427 if (TRACE_ON(ddraw))
429 TRACE(" Projection Matrix : (%p)\n", &proj_mat);
430 dump_D3DMATRIX(&proj_mat);
431 TRACE(" View Matrix : (%p)\n",&view_mat);
432 dump_D3DMATRIX(&view_mat);
433 TRACE(" World Matrix : (%p)\n", &world_mat);
434 dump_D3DMATRIX(&world_mat);
437 multiply_matrix(&mat,&view_mat,&world_mat);
438 multiply_matrix(&mat,&proj_mat,&mat);
440 for (nb = 0; nb < ci->dwCount; nb++) {
441 dst->u5.color = src->u4.color;
442 dst->u6.specular = src->u5.specular;
443 dst->u7.tu = src->u6.tu;
444 dst->u8.tv = src->u7.tv;
446 /* Now, the matrix multiplication */
447 dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
448 dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
449 dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
450 dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);
452 dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX
453 + Viewport->dwX + Viewport->dwWidth / 2;
454 dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
455 + Viewport->dwY + Viewport->dwHeight / 2;
457 dst->u3.sz /= dst->u4.rhw;
458 dst->u4.rhw = 1 / dst->u4.rhw;
460 src++;
461 dst++;
463 } else if (ci->dwFlags == D3DPROCESSVERTICES_COPY) {
464 D3DTLVERTEX *src = ((LPD3DTLVERTEX) ((char *)This->desc.lpData + vs)) + ci->wStart;
465 D3DTLVERTEX *dst = ((LPD3DTLVERTEX) (This->vertex_data)) + ci->wDest;
467 memcpy(dst, src, ci->dwCount * sizeof(D3DTLVERTEX));
468 } else {
469 ERR("Unhandled vertex processing flag %#x.\n", ci->dwFlags);
472 instr += size;
474 } break;
476 case D3DOP_TEXTURELOAD: {
477 WARN("TEXTURELOAD-s (%d)\n", count);
479 instr += count * size;
480 } break;
482 case D3DOP_EXIT: {
483 TRACE("EXIT (%d)\n", count);
484 /* We did this instruction */
485 instr += size;
486 /* Exit this loop */
487 goto end_of_buffer;
488 } break;
490 case D3DOP_BRANCHFORWARD: {
491 int i;
492 TRACE("BRANCHFORWARD (%d)\n", count);
494 for (i = 0; i < count; i++) {
495 LPD3DBRANCH ci = (LPD3DBRANCH) instr;
497 if ((This->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue) {
498 if (!ci->bNegate) {
499 TRACE(" Branch to %d\n", ci->dwOffset);
500 if (ci->dwOffset) {
501 instr = (char*)current + ci->dwOffset;
502 break;
505 } else {
506 if (ci->bNegate) {
507 TRACE(" Branch to %d\n", ci->dwOffset);
508 if (ci->dwOffset) {
509 instr = (char*)current + ci->dwOffset;
510 break;
515 instr += size;
517 } break;
519 case D3DOP_SPAN: {
520 WARN("SPAN-s (%d)\n", count);
522 instr += count * size;
523 } break;
525 case D3DOP_SETSTATUS: {
526 int i;
527 TRACE("SETSTATUS (%d)\n", count);
529 for (i = 0; i < count; i++) {
530 LPD3DSTATUS ci = (LPD3DSTATUS) instr;
532 This->data.dsStatus = *ci;
534 instr += size;
536 } break;
538 default:
539 ERR("Unhandled OpCode %d !!!\n",current->bOpcode);
540 /* Try to save ... */
541 instr += count * size;
542 break;
546 end_of_buffer:
547 return D3D_OK;
550 static inline IDirect3DExecuteBufferImpl *impl_from_IDirect3DExecuteBuffer(IDirect3DExecuteBuffer *iface)
552 return CONTAINING_RECORD(iface, IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer_iface);
555 /*****************************************************************************
556 * IDirect3DExecuteBuffer::QueryInterface
558 * Well, a usual QueryInterface function. Don't know fur sure which
559 * interfaces it can Query.
561 * Params:
562 * riid: The interface ID queried for
563 * obj: Address to return the interface pointer at
565 * Returns:
566 * D3D_OK in case of a success (S_OK? Think it's the same)
567 * OLE_E_ENUM_NOMORE if the interface wasn't found.
568 * (E_NOINTERFACE?? Don't know what I really need)
570 *****************************************************************************/
571 static HRESULT WINAPI
572 IDirect3DExecuteBufferImpl_QueryInterface(IDirect3DExecuteBuffer *iface,
573 REFIID riid,
574 void **obj)
576 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
578 *obj = NULL;
580 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
581 IDirect3DExecuteBuffer_AddRef(iface);
582 *obj = iface;
583 TRACE(" Creating IUnknown interface at %p.\n", *obj);
584 return S_OK;
586 if ( IsEqualGUID( &IID_IDirect3DExecuteBuffer, riid ) ) {
587 IDirect3DExecuteBuffer_AddRef(iface);
588 *obj = iface;
589 TRACE(" Creating IDirect3DExecuteBuffer interface %p\n", *obj);
590 return S_OK;
592 FIXME("(%p): interface for IID %s NOT found!\n", iface, debugstr_guid(riid));
593 return E_NOINTERFACE;
597 /*****************************************************************************
598 * IDirect3DExecuteBuffer::AddRef
600 * A normal AddRef method, nothing special
602 * Returns:
603 * The new refcount
605 *****************************************************************************/
606 static ULONG WINAPI IDirect3DExecuteBufferImpl_AddRef(IDirect3DExecuteBuffer *iface)
608 IDirect3DExecuteBufferImpl *This = impl_from_IDirect3DExecuteBuffer(iface);
609 ULONG ref = InterlockedIncrement(&This->ref);
611 TRACE("%p increasing refcount to %u.\n", This, ref);
613 return ref;
616 /*****************************************************************************
617 * IDirect3DExecuteBuffer::Release
619 * A normal Release method, nothing special
621 * Returns:
622 * The new refcount
624 *****************************************************************************/
625 static ULONG WINAPI IDirect3DExecuteBufferImpl_Release(IDirect3DExecuteBuffer *iface)
627 IDirect3DExecuteBufferImpl *This = impl_from_IDirect3DExecuteBuffer(iface);
628 ULONG ref = InterlockedDecrement(&This->ref);
630 TRACE("%p decreasing refcount to %u.\n", This, ref);
632 if (!ref) {
633 if (This->need_free)
634 HeapFree(GetProcessHeap(),0,This->desc.lpData);
635 HeapFree(GetProcessHeap(),0,This->vertex_data);
636 HeapFree(GetProcessHeap(),0,This->indices);
637 HeapFree(GetProcessHeap(),0,This);
638 return 0;
641 return ref;
644 /*****************************************************************************
645 * IDirect3DExecuteBuffer::Initialize
647 * Initializes the Execute Buffer. This method exists for COM compliance
648 * Nothing to do here.
650 * Returns:
651 * D3D_OK
653 *****************************************************************************/
654 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Initialize(IDirect3DExecuteBuffer *iface,
655 IDirect3DDevice *device, D3DEXECUTEBUFFERDESC *desc)
657 TRACE("iface %p, device %p, desc %p.\n", iface, device, desc);
659 return D3D_OK;
662 /*****************************************************************************
663 * IDirect3DExecuteBuffer::Lock
665 * Locks the buffer, so the app can write into it.
667 * Params:
668 * Desc: Pointer to return the buffer description. This Description contains
669 * a pointer to the buffer data.
671 * Returns:
672 * This implementation always returns D3D_OK
674 *****************************************************************************/
675 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Lock(IDirect3DExecuteBuffer *iface,
676 D3DEXECUTEBUFFERDESC *lpDesc)
678 IDirect3DExecuteBufferImpl *This = impl_from_IDirect3DExecuteBuffer(iface);
679 DWORD dwSize;
681 TRACE("iface %p, desc %p.\n", iface, lpDesc);
683 dwSize = lpDesc->dwSize;
684 memcpy(lpDesc, &This->desc, dwSize);
686 if (TRACE_ON(ddraw))
688 TRACE(" Returning description :\n");
689 _dump_D3DEXECUTEBUFFERDESC(lpDesc);
691 return D3D_OK;
694 /*****************************************************************************
695 * IDirect3DExecuteBuffer::Unlock
697 * Unlocks the buffer. We don't have anything to do here
699 * Returns:
700 * This implementation always returns D3D_OK
702 *****************************************************************************/
703 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Unlock(IDirect3DExecuteBuffer *iface)
705 TRACE("iface %p.\n", iface);
707 return D3D_OK;
710 /*****************************************************************************
711 * IDirect3DExecuteBuffer::SetExecuteData
713 * Sets the execute data. This data is used to describe the buffer's content
715 * Params:
716 * Data: Pointer to a D3DEXECUTEDATA structure containing the data to
717 * assign
719 * Returns:
720 * D3D_OK on success
721 * DDERR_OUTOFMEMORY if the vertex buffer allocation failed
723 *****************************************************************************/
724 static HRESULT WINAPI IDirect3DExecuteBufferImpl_SetExecuteData(IDirect3DExecuteBuffer *iface,
725 D3DEXECUTEDATA *lpData)
727 IDirect3DExecuteBufferImpl *This = impl_from_IDirect3DExecuteBuffer(iface);
728 DWORD nbvert;
730 TRACE("iface %p, data %p.\n", iface, lpData);
732 memcpy(&This->data, lpData, lpData->dwSize);
734 /* Get the number of vertices in the execute buffer */
735 nbvert = This->data.dwVertexCount;
737 /* Prepares the transformed vertex buffer */
738 HeapFree(GetProcessHeap(), 0, This->vertex_data);
739 This->vertex_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbvert * sizeof(D3DTLVERTEX));
741 if (TRACE_ON(ddraw))
742 _dump_executedata(lpData);
744 return D3D_OK;
747 /*****************************************************************************
748 * IDirect3DExecuteBuffer::GetExecuteData
750 * Returns the data in the execute buffer
752 * Params:
753 * Data: Pointer to a D3DEXECUTEDATA structure used to return data
755 * Returns:
756 * D3D_OK on success
758 *****************************************************************************/
759 static HRESULT WINAPI IDirect3DExecuteBufferImpl_GetExecuteData(IDirect3DExecuteBuffer *iface,
760 D3DEXECUTEDATA *lpData)
762 IDirect3DExecuteBufferImpl *This = impl_from_IDirect3DExecuteBuffer(iface);
763 DWORD dwSize;
765 TRACE("iface %p, data %p.\n", iface, lpData);
767 dwSize = lpData->dwSize;
768 memcpy(lpData, &This->data, dwSize);
770 if (TRACE_ON(ddraw))
772 TRACE("Returning data :\n");
773 _dump_executedata(lpData);
776 return DD_OK;
779 /*****************************************************************************
780 * IDirect3DExecuteBuffer::Validate
782 * DirectX 5 SDK: "The IDirect3DExecuteBuffer::Validate method is not
783 * currently implemented"
785 * Params:
788 * Returns:
789 * DDERR_UNSUPPORTED, because it's not implemented in Windows.
791 *****************************************************************************/
792 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Validate(IDirect3DExecuteBuffer *iface,
793 DWORD *offset, LPD3DVALIDATECALLBACK callback, void *context, DWORD reserved)
795 TRACE("iface %p, offset %p, callback %p, context %p, reserved %#x.\n",
796 iface, offset, callback, context, reserved);
798 WARN("Not implemented.\n");
800 return DDERR_UNSUPPORTED; /* Unchecked */
803 /*****************************************************************************
804 * IDirect3DExecuteBuffer::Optimize
806 * DirectX5 SDK: "The IDirect3DExecuteBuffer::Optimize method is not
807 * currently supported"
809 * Params:
810 * Dummy: Seems to be an unused dummy ;)
812 * Returns:
813 * DDERR_UNSUPPORTED, because it's not implemented in Windows.
815 *****************************************************************************/
816 static HRESULT WINAPI IDirect3DExecuteBufferImpl_Optimize(IDirect3DExecuteBuffer *iface, DWORD reserved)
818 TRACE("iface %p, reserved %#x.\n", iface, reserved);
820 WARN("Not implemented.\n");
822 return DDERR_UNSUPPORTED; /* Unchecked */
825 static const struct IDirect3DExecuteBufferVtbl d3d_execute_buffer_vtbl =
827 IDirect3DExecuteBufferImpl_QueryInterface,
828 IDirect3DExecuteBufferImpl_AddRef,
829 IDirect3DExecuteBufferImpl_Release,
830 IDirect3DExecuteBufferImpl_Initialize,
831 IDirect3DExecuteBufferImpl_Lock,
832 IDirect3DExecuteBufferImpl_Unlock,
833 IDirect3DExecuteBufferImpl_SetExecuteData,
834 IDirect3DExecuteBufferImpl_GetExecuteData,
835 IDirect3DExecuteBufferImpl_Validate,
836 IDirect3DExecuteBufferImpl_Optimize,
839 HRESULT d3d_execute_buffer_init(IDirect3DExecuteBufferImpl *execute_buffer,
840 IDirect3DDeviceImpl *device, D3DEXECUTEBUFFERDESC *desc)
842 execute_buffer->IDirect3DExecuteBuffer_iface.lpVtbl = &d3d_execute_buffer_vtbl;
843 execute_buffer->ref = 1;
844 execute_buffer->d3ddev = device;
846 /* Initializes memory */
847 memcpy(&execute_buffer->desc, desc, desc->dwSize);
849 /* No buffer given */
850 if (!(execute_buffer->desc.dwFlags & D3DDEB_LPDATA))
851 execute_buffer->desc.lpData = NULL;
853 /* No buffer size given */
854 if (!(execute_buffer->desc.dwFlags & D3DDEB_BUFSIZE))
855 execute_buffer->desc.dwBufferSize = 0;
857 /* Create buffer if asked */
858 if (!execute_buffer->desc.lpData && execute_buffer->desc.dwBufferSize)
860 execute_buffer->need_free = TRUE;
861 execute_buffer->desc.lpData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, execute_buffer->desc.dwBufferSize);
862 if (!execute_buffer->desc.lpData)
864 ERR("Failed to allocate execute buffer data.\n");
865 return DDERR_OUTOFMEMORY;
869 execute_buffer->desc.dwFlags |= D3DDEB_LPDATA;
871 return D3D_OK;
874 IDirect3DExecuteBufferImpl *unsafe_impl_from_IDirect3DExecuteBuffer(IDirect3DExecuteBuffer *iface)
876 if (!iface)
877 return NULL;
878 assert(iface->lpVtbl == &d3d_execute_buffer_vtbl);
880 return impl_from_IDirect3DExecuteBuffer(iface);