gdiplus: Avoid a crash in GdipDrawImagePointRect.
[wine/multimedia.git] / dlls / ddraw / executebuffer.c
blob2925cfa1ca4834a9813fd3cedcbafac1da4108c6
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 HRESULT d3d_execute_buffer_execute(struct d3d_execute_buffer *buffer,
54 struct d3d_device *device, struct d3d_viewport *viewport)
56 DWORD vs = buffer->data.dwVertexOffset;
57 DWORD is = buffer->data.dwInstructionOffset;
58 char *instr = (char *)buffer->desc.lpData + is;
60 if (viewport->active_device != device)
62 WARN("Viewport %p active device is %p.\n",
63 viewport, viewport->active_device);
64 return DDERR_INVALIDPARAMS;
67 /* Activate the viewport */
68 viewport_activate(viewport, FALSE);
70 TRACE("ExecuteData :\n");
71 if (TRACE_ON(ddraw))
72 _dump_executedata(&(buffer->data));
74 for (;;)
76 D3DINSTRUCTION *current = (D3DINSTRUCTION *)instr;
77 BYTE size;
78 WORD count;
80 count = current->wCount;
81 size = current->bSize;
82 instr += sizeof(D3DINSTRUCTION);
84 switch (current->bOpcode) {
85 case D3DOP_POINT: {
86 WARN("POINT-s (%d)\n", count);
87 instr += count * size;
88 } break;
90 case D3DOP_LINE: {
91 WARN("LINE-s (%d)\n", count);
92 instr += count * size;
93 } break;
95 case D3DOP_TRIANGLE: {
96 DWORD i;
97 D3DTLVERTEX *tl_vx = buffer->vertex_data;
98 TRACE("TRIANGLE (%d)\n", count);
100 if (buffer->nb_indices < count * 3)
102 buffer->nb_indices = count * 3;
103 HeapFree(GetProcessHeap(), 0, buffer->indices);
104 buffer->indices = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->indices) * buffer->nb_indices);
107 for (i = 0; i < count; ++i)
109 D3DTRIANGLE *ci = (D3DTRIANGLE *)instr;
110 TRACE(" v1: %d v2: %d v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3);
111 TRACE(" Flags : ");
112 if (TRACE_ON(ddraw))
114 /* Wireframe */
115 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
116 TRACE("EDGEENABLE1 ");
117 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2)
118 TRACE("EDGEENABLE2 ");
119 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
120 TRACE("EDGEENABLE3 ");
121 /* Strips / Fans */
122 if (ci->wFlags == D3DTRIFLAG_EVEN)
123 TRACE("EVEN ");
124 if (ci->wFlags == D3DTRIFLAG_ODD)
125 TRACE("ODD ");
126 if (ci->wFlags == D3DTRIFLAG_START)
127 TRACE("START ");
128 if ((ci->wFlags > 0) && (ci->wFlags < 30))
129 TRACE("STARTFLAT(%u) ", ci->wFlags);
130 TRACE("\n");
132 buffer->indices[(i * 3) ] = ci->u1.v1;
133 buffer->indices[(i * 3) + 1] = ci->u2.v2;
134 buffer->indices[(i * 3) + 2] = ci->u3.v3;
135 instr += size;
137 IDirect3DDevice7_DrawIndexedPrimitive(&device->IDirect3DDevice7_iface,
138 D3DPT_TRIANGLELIST, D3DFVF_TLVERTEX, tl_vx, buffer->nb_vertices,
139 buffer->indices, count * 3, 0);
140 } break;
142 case D3DOP_MATRIXLOAD:
143 WARN("MATRIXLOAD-s (%d)\n", count);
144 instr += count * size;
145 break;
147 case D3DOP_MATRIXMULTIPLY: {
148 DWORD i;
149 TRACE("MATRIXMULTIPLY (%d)\n", count);
151 for (i = 0; i < count; ++i)
153 D3DMATRIXMULTIPLY *ci = (D3DMATRIXMULTIPLY *)instr;
154 D3DMATRIX *a, *b, *c;
156 a = ddraw_get_object(&device->handle_table, ci->hDestMatrix - 1, DDRAW_HANDLE_MATRIX);
157 b = ddraw_get_object(&device->handle_table, ci->hSrcMatrix1 - 1, DDRAW_HANDLE_MATRIX);
158 c = ddraw_get_object(&device->handle_table, ci->hSrcMatrix2 - 1, DDRAW_HANDLE_MATRIX);
160 if (!a || !b || !c)
162 ERR("Invalid matrix handle (a %#x -> %p, b %#x -> %p, c %#x -> %p).\n",
163 ci->hDestMatrix, a, ci->hSrcMatrix1, b, ci->hSrcMatrix2, c);
165 else
167 TRACE("dst %p, src1 %p, src2 %p.\n", a, b, c);
168 multiply_matrix(a, c, b);
171 instr += size;
173 } break;
175 case D3DOP_STATETRANSFORM: {
176 DWORD i;
177 TRACE("STATETRANSFORM (%d)\n", count);
179 for (i = 0; i < count; ++i)
181 D3DSTATE *ci = (D3DSTATE *)instr;
182 D3DMATRIX *m;
184 m = ddraw_get_object(&device->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATRIX);
185 if (!m)
187 ERR("Invalid matrix handle %#x.\n", ci->u2.dwArg[0]);
189 else
191 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_WORLD)
192 device->world = ci->u2.dwArg[0];
193 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_VIEW)
194 device->view = ci->u2.dwArg[0];
195 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_PROJECTION)
196 device->proj = ci->u2.dwArg[0];
197 IDirect3DDevice7_SetTransform(&device->IDirect3DDevice7_iface,
198 ci->u1.dtstTransformStateType, m);
201 instr += size;
203 } break;
205 case D3DOP_STATELIGHT: {
206 DWORD i;
207 TRACE("STATELIGHT (%d)\n", count);
209 for (i = 0; i < count; ++i)
211 D3DSTATE *ci = (D3DSTATE *)instr;
213 TRACE("(%08x,%08x)\n", ci->u1.dlstLightStateType, ci->u2.dwArg[0]);
215 if (!ci->u1.dlstLightStateType || (ci->u1.dlstLightStateType > D3DLIGHTSTATE_COLORVERTEX))
216 ERR("Unexpected Light State Type %d\n", ci->u1.dlstLightStateType);
217 else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
219 struct d3d_material *m;
221 m = ddraw_get_object(&device->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATERIAL);
222 if (!m)
223 ERR("Invalid material handle %#x.\n", ci->u2.dwArg[0]);
224 else
225 material_activate(m);
227 else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
229 switch (ci->u2.dwArg[0]) {
230 case D3DCOLOR_MONO:
231 ERR("DDCOLOR_MONO should not happen!\n");
232 break;
233 case D3DCOLOR_RGB:
234 /* We are already in this mode */
235 break;
236 default:
237 ERR("Unknown color model!\n");
239 } else {
240 D3DRENDERSTATETYPE rs = 0;
241 switch (ci->u1.dlstLightStateType) {
243 case D3DLIGHTSTATE_AMBIENT: /* 2 */
244 rs = D3DRENDERSTATE_AMBIENT;
245 break;
246 case D3DLIGHTSTATE_FOGMODE: /* 4 */
247 rs = D3DRENDERSTATE_FOGVERTEXMODE;
248 break;
249 case D3DLIGHTSTATE_FOGSTART: /* 5 */
250 rs = D3DRENDERSTATE_FOGSTART;
251 break;
252 case D3DLIGHTSTATE_FOGEND: /* 6 */
253 rs = D3DRENDERSTATE_FOGEND;
254 break;
255 case D3DLIGHTSTATE_FOGDENSITY: /* 7 */
256 rs = D3DRENDERSTATE_FOGDENSITY;
257 break;
258 case D3DLIGHTSTATE_COLORVERTEX: /* 8 */
259 rs = D3DRENDERSTATE_COLORVERTEX;
260 break;
261 default:
262 break;
265 IDirect3DDevice7_SetRenderState(&device->IDirect3DDevice7_iface, rs, ci->u2.dwArg[0]);
268 instr += size;
270 } break;
272 case D3DOP_STATERENDER: {
273 DWORD i;
274 IDirect3DDevice2 *d3d_device2 = &device->IDirect3DDevice2_iface;
275 TRACE("STATERENDER (%d)\n", count);
277 for (i = 0; i < count; ++i)
279 D3DSTATE *ci = (D3DSTATE *)instr;
281 IDirect3DDevice2_SetRenderState(d3d_device2, ci->u1.drstRenderStateType, ci->u2.dwArg[0]);
283 instr += size;
285 } break;
287 case D3DOP_PROCESSVERTICES:
289 /* TODO: Share code with IDirect3DVertexBuffer::ProcessVertices and / or
290 * IWineD3DDevice::ProcessVertices
292 DWORD i;
293 D3DMATRIX view_mat, world_mat, proj_mat;
294 TRACE("PROCESSVERTICES (%d)\n", count);
296 /* Get the transform and world matrix */
297 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
298 wined3d_device_get_transform(device->wined3d_device,
299 D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat);
300 wined3d_device_get_transform(device->wined3d_device,
301 D3DTRANSFORMSTATE_PROJECTION, (struct wined3d_matrix *)&proj_mat);
302 wined3d_device_get_transform(device->wined3d_device,
303 WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat);
305 for (i = 0; i < count; ++i)
307 D3DPROCESSVERTICES *ci = (D3DPROCESSVERTICES *)instr;
309 TRACE(" Start : %d Dest : %d Count : %d\n",
310 ci->wStart, ci->wDest, ci->dwCount);
311 TRACE(" Flags : ");
312 if (TRACE_ON(ddraw))
314 if (ci->dwFlags & D3DPROCESSVERTICES_COPY)
315 TRACE("COPY ");
316 if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
317 TRACE("NOCOLOR ");
318 if (ci->dwFlags == D3DPROCESSVERTICES_OPMASK)
319 TRACE("OPMASK ");
320 if (ci->dwFlags & D3DPROCESSVERTICES_TRANSFORM)
321 TRACE("TRANSFORM ");
322 if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT)
323 TRACE("TRANSFORMLIGHT ");
324 if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
325 TRACE("UPDATEEXTENTS ");
326 TRACE("\n");
329 /* This is where doing Direct3D on top on OpenGL is quite difficult.
330 This method transforms a set of vertices using the CURRENT state
331 (lighting, projection, ...) but does not rasterize them.
332 They will only be put on screen later (with the POINT / LINE and
333 TRIANGLE op-codes). The problem is that you can have a triangle
334 with each point having been transformed using another state...
336 In this implementation, I will emulate only ONE thing : each
337 vertex can have its own "WORLD" transformation (this is used in the
338 TWIST.EXE demo of the 5.2 SDK). I suppose that all vertices of the
339 execute buffer use the same state.
341 If I find applications that change other states, I will try to do a
342 more 'fine-tuned' state emulation (but I may become quite tricky if
343 it changes a light position in the middle of a triangle).
345 In this case, a 'direct' approach (i.e. without using OpenGL, but
346 writing our own 3D rasterizer) would be easier. */
348 /* The current method (with the hypothesis that only the WORLD matrix
349 will change between two points) is like this :
350 - I transform 'manually' all the vertices with the current WORLD
351 matrix and store them in the vertex buffer
352 - during the rasterization phase, the WORLD matrix will be set to
353 the Identity matrix */
355 /* Enough for the moment */
356 if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) {
357 unsigned int nb;
358 D3DVERTEX *src = ((D3DVERTEX *)((char *)buffer->desc.lpData + vs)) + ci->wStart;
359 D3DTLVERTEX *dst = ((D3DTLVERTEX *)buffer->vertex_data) + ci->wDest;
360 D3DVIEWPORT *Viewport = &viewport->viewports.vp1;
361 D3DMATRIX mat;
363 if (TRACE_ON(ddraw))
365 TRACE(" Projection Matrix : (%p)\n", &proj_mat);
366 dump_D3DMATRIX(&proj_mat);
367 TRACE(" View Matrix : (%p)\n", &view_mat);
368 dump_D3DMATRIX(&view_mat);
369 TRACE(" World Matrix : (%p)\n", &world_mat);
370 dump_D3DMATRIX(&world_mat);
373 multiply_matrix(&mat,&view_mat,&world_mat);
374 multiply_matrix(&mat,&proj_mat,&mat);
376 for (nb = 0; nb < ci->dwCount; nb++) {
377 /* No lighting yet */
378 dst->u5.color = 0xFFFFFFFF; /* Opaque white */
379 dst->u6.specular = 0xFF000000; /* No specular and no fog factor */
381 dst->u7.tu = src->u7.tu;
382 dst->u8.tv = src->u8.tv;
384 /* Now, the matrix multiplication */
385 dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
386 dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
387 dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
388 dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);
390 dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX
391 + Viewport->dwX + Viewport->dwWidth / 2;
392 dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
393 + Viewport->dwY + Viewport->dwHeight / 2;
394 dst->u3.sz /= dst->u4.rhw;
395 dst->u4.rhw = 1 / dst->u4.rhw;
397 src++;
398 dst++;
401 } else if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORM) {
402 unsigned int nb;
403 D3DLVERTEX *src = ((D3DLVERTEX *)((char *)buffer->desc.lpData + vs)) + ci->wStart;
404 D3DTLVERTEX *dst = ((D3DTLVERTEX *)buffer->vertex_data) + ci->wDest;
405 D3DVIEWPORT *Viewport = &viewport->viewports.vp1;
406 D3DMATRIX mat;
408 if (TRACE_ON(ddraw))
410 TRACE(" Projection Matrix : (%p)\n", &proj_mat);
411 dump_D3DMATRIX(&proj_mat);
412 TRACE(" View Matrix : (%p)\n",&view_mat);
413 dump_D3DMATRIX(&view_mat);
414 TRACE(" World Matrix : (%p)\n", &world_mat);
415 dump_D3DMATRIX(&world_mat);
418 multiply_matrix(&mat,&view_mat,&world_mat);
419 multiply_matrix(&mat,&proj_mat,&mat);
421 for (nb = 0; nb < ci->dwCount; nb++) {
422 dst->u5.color = src->u4.color;
423 dst->u6.specular = src->u5.specular;
424 dst->u7.tu = src->u6.tu;
425 dst->u8.tv = src->u7.tv;
427 /* Now, the matrix multiplication */
428 dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + (1.0 * mat._41);
429 dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + (1.0 * mat._42);
430 dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + (1.0 * mat._43);
431 dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + (1.0 * mat._44);
433 dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX
434 + Viewport->dwX + Viewport->dwWidth / 2;
435 dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
436 + Viewport->dwY + Viewport->dwHeight / 2;
438 dst->u3.sz /= dst->u4.rhw;
439 dst->u4.rhw = 1 / dst->u4.rhw;
441 src++;
442 dst++;
445 else if (ci->dwFlags == D3DPROCESSVERTICES_COPY)
447 D3DTLVERTEX *src = ((D3DTLVERTEX *)((char *)buffer->desc.lpData + vs)) + ci->wStart;
448 D3DTLVERTEX *dst = ((D3DTLVERTEX *)buffer->vertex_data) + ci->wDest;
450 memcpy(dst, src, ci->dwCount * sizeof(D3DTLVERTEX));
451 } else {
452 ERR("Unhandled vertex processing flag %#x.\n", ci->dwFlags);
455 instr += size;
457 } break;
459 case D3DOP_TEXTURELOAD: {
460 WARN("TEXTURELOAD-s (%d)\n", count);
462 instr += count * size;
463 } break;
465 case D3DOP_EXIT: {
466 TRACE("EXIT (%d)\n", count);
467 /* We did this instruction */
468 instr += size;
469 /* Exit this loop */
470 goto end_of_buffer;
471 } break;
473 case D3DOP_BRANCHFORWARD: {
474 DWORD i;
475 TRACE("BRANCHFORWARD (%d)\n", count);
477 for (i = 0; i < count; ++i)
479 D3DBRANCH *ci = (D3DBRANCH *)instr;
481 if ((buffer->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue)
483 if (!ci->bNegate)
485 TRACE(" Branch to %d\n", ci->dwOffset);
486 if (ci->dwOffset) {
487 instr = (char*)current + ci->dwOffset;
488 break;
491 } else {
492 if (ci->bNegate) {
493 TRACE(" Branch to %d\n", ci->dwOffset);
494 if (ci->dwOffset) {
495 instr = (char*)current + ci->dwOffset;
496 break;
501 instr += size;
503 } break;
505 case D3DOP_SPAN: {
506 WARN("SPAN-s (%d)\n", count);
508 instr += count * size;
509 } break;
511 case D3DOP_SETSTATUS: {
512 DWORD i;
513 TRACE("SETSTATUS (%d)\n", count);
515 for (i = 0; i < count; ++i)
517 buffer->data.dsStatus = *(D3DSTATUS *)instr;
518 instr += size;
520 } break;
522 default:
523 ERR("Unhandled OpCode %d !!!\n",current->bOpcode);
524 /* Try to save ... */
525 instr += count * size;
526 break;
530 end_of_buffer:
531 return D3D_OK;
534 static inline struct d3d_execute_buffer *impl_from_IDirect3DExecuteBuffer(IDirect3DExecuteBuffer *iface)
536 return CONTAINING_RECORD(iface, struct d3d_execute_buffer, IDirect3DExecuteBuffer_iface);
539 /*****************************************************************************
540 * IDirect3DExecuteBuffer::QueryInterface
542 * Well, a usual QueryInterface function. Don't know fur sure which
543 * interfaces it can Query.
545 * Params:
546 * riid: The interface ID queried for
547 * obj: Address to return the interface pointer at
549 * Returns:
550 * D3D_OK in case of a success (S_OK? Think it's the same)
551 * OLE_E_ENUM_NOMORE if the interface wasn't found.
552 * (E_NOINTERFACE?? Don't know what I really need)
554 *****************************************************************************/
555 static HRESULT WINAPI d3d_execute_buffer_QueryInterface(IDirect3DExecuteBuffer *iface, REFIID riid, void **obj)
557 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
559 *obj = NULL;
561 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
562 IDirect3DExecuteBuffer_AddRef(iface);
563 *obj = iface;
564 TRACE(" Creating IUnknown interface at %p.\n", *obj);
565 return S_OK;
567 if ( IsEqualGUID( &IID_IDirect3DExecuteBuffer, riid ) ) {
568 IDirect3DExecuteBuffer_AddRef(iface);
569 *obj = iface;
570 TRACE(" Creating IDirect3DExecuteBuffer interface %p\n", *obj);
571 return S_OK;
573 FIXME("(%p): interface for IID %s NOT found!\n", iface, debugstr_guid(riid));
574 return E_NOINTERFACE;
578 /*****************************************************************************
579 * IDirect3DExecuteBuffer::AddRef
581 * A normal AddRef method, nothing special
583 * Returns:
584 * The new refcount
586 *****************************************************************************/
587 static ULONG WINAPI d3d_execute_buffer_AddRef(IDirect3DExecuteBuffer *iface)
589 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
590 ULONG ref = InterlockedIncrement(&buffer->ref);
592 TRACE("%p increasing refcount to %u.\n", buffer, ref);
594 return ref;
597 /*****************************************************************************
598 * IDirect3DExecuteBuffer::Release
600 * A normal Release method, nothing special
602 * Returns:
603 * The new refcount
605 *****************************************************************************/
606 static ULONG WINAPI d3d_execute_buffer_Release(IDirect3DExecuteBuffer *iface)
608 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
609 ULONG ref = InterlockedDecrement(&buffer->ref);
611 TRACE("%p decreasing refcount to %u.\n", buffer, ref);
613 if (!ref)
615 if (buffer->need_free)
616 HeapFree(GetProcessHeap(), 0, buffer->desc.lpData);
617 HeapFree(GetProcessHeap(), 0, buffer->vertex_data);
618 HeapFree(GetProcessHeap(), 0, buffer->indices);
619 HeapFree(GetProcessHeap(), 0, buffer);
622 return ref;
625 /*****************************************************************************
626 * IDirect3DExecuteBuffer::Initialize
628 * Initializes the Execute Buffer. This method exists for COM compliance
629 * Nothing to do here.
631 * Returns:
632 * D3D_OK
634 *****************************************************************************/
635 static HRESULT WINAPI d3d_execute_buffer_Initialize(IDirect3DExecuteBuffer *iface,
636 IDirect3DDevice *device, D3DEXECUTEBUFFERDESC *desc)
638 TRACE("iface %p, device %p, desc %p.\n", iface, device, desc);
640 return D3D_OK;
643 /*****************************************************************************
644 * IDirect3DExecuteBuffer::Lock
646 * Locks the buffer, so the app can write into it.
648 * Params:
649 * Desc: Pointer to return the buffer description. This Description contains
650 * a pointer to the buffer data.
652 * Returns:
653 * This implementation always returns D3D_OK
655 *****************************************************************************/
656 static HRESULT WINAPI d3d_execute_buffer_Lock(IDirect3DExecuteBuffer *iface, D3DEXECUTEBUFFERDESC *desc)
658 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
659 DWORD dwSize;
661 TRACE("iface %p, desc %p.\n", iface, desc);
663 dwSize = desc->dwSize;
664 memcpy(desc, &buffer->desc, dwSize);
666 if (TRACE_ON(ddraw))
668 TRACE(" Returning description :\n");
669 _dump_D3DEXECUTEBUFFERDESC(desc);
671 return D3D_OK;
674 /*****************************************************************************
675 * IDirect3DExecuteBuffer::Unlock
677 * Unlocks the buffer. We don't have anything to do here
679 * Returns:
680 * This implementation always returns D3D_OK
682 *****************************************************************************/
683 static HRESULT WINAPI d3d_execute_buffer_Unlock(IDirect3DExecuteBuffer *iface)
685 TRACE("iface %p.\n", iface);
687 return D3D_OK;
690 /*****************************************************************************
691 * IDirect3DExecuteBuffer::SetExecuteData
693 * Sets the execute data. This data is used to describe the buffer's content
695 * Params:
696 * Data: Pointer to a D3DEXECUTEDATA structure containing the data to
697 * assign
699 * Returns:
700 * D3D_OK on success
701 * DDERR_OUTOFMEMORY if the vertex buffer allocation failed
703 *****************************************************************************/
704 static HRESULT WINAPI d3d_execute_buffer_SetExecuteData(IDirect3DExecuteBuffer *iface, D3DEXECUTEDATA *data)
706 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
707 DWORD nbvert;
709 TRACE("iface %p, data %p.\n", iface, data);
711 memcpy(&buffer->data, data, data->dwSize);
713 /* Get the number of vertices in the execute buffer */
714 nbvert = buffer->data.dwVertexCount;
716 /* Prepares the transformed vertex buffer */
717 HeapFree(GetProcessHeap(), 0, buffer->vertex_data);
718 buffer->vertex_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbvert * sizeof(D3DTLVERTEX));
719 buffer->nb_vertices = nbvert;
721 if (TRACE_ON(ddraw))
722 _dump_executedata(data);
724 return D3D_OK;
727 /*****************************************************************************
728 * IDirect3DExecuteBuffer::GetExecuteData
730 * Returns the data in the execute buffer
732 * Params:
733 * Data: Pointer to a D3DEXECUTEDATA structure used to return data
735 * Returns:
736 * D3D_OK on success
738 *****************************************************************************/
739 static HRESULT WINAPI d3d_execute_buffer_GetExecuteData(IDirect3DExecuteBuffer *iface, D3DEXECUTEDATA *data)
741 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
742 DWORD dwSize;
744 TRACE("iface %p, data %p.\n", iface, data);
746 dwSize = data->dwSize;
747 memcpy(data, &buffer->data, dwSize);
749 if (TRACE_ON(ddraw))
751 TRACE("Returning data :\n");
752 _dump_executedata(data);
755 return DD_OK;
758 /*****************************************************************************
759 * IDirect3DExecuteBuffer::Validate
761 * DirectX 5 SDK: "The IDirect3DExecuteBuffer::Validate method is not
762 * currently implemented"
764 * Params:
767 * Returns:
768 * DDERR_UNSUPPORTED, because it's not implemented in Windows.
770 *****************************************************************************/
771 static HRESULT WINAPI d3d_execute_buffer_Validate(IDirect3DExecuteBuffer *iface,
772 DWORD *offset, LPD3DVALIDATECALLBACK callback, void *context, DWORD reserved)
774 TRACE("iface %p, offset %p, callback %p, context %p, reserved %#x.\n",
775 iface, offset, callback, context, reserved);
777 WARN("Not implemented.\n");
779 return DDERR_UNSUPPORTED; /* Unchecked */
782 /*****************************************************************************
783 * IDirect3DExecuteBuffer::Optimize
785 * DirectX5 SDK: "The IDirect3DExecuteBuffer::Optimize method is not
786 * currently supported"
788 * Params:
789 * Dummy: Seems to be an unused dummy ;)
791 * Returns:
792 * DDERR_UNSUPPORTED, because it's not implemented in Windows.
794 *****************************************************************************/
795 static HRESULT WINAPI d3d_execute_buffer_Optimize(IDirect3DExecuteBuffer *iface, DWORD reserved)
797 TRACE("iface %p, reserved %#x.\n", iface, reserved);
799 WARN("Not implemented.\n");
801 return DDERR_UNSUPPORTED; /* Unchecked */
804 static const struct IDirect3DExecuteBufferVtbl d3d_execute_buffer_vtbl =
806 d3d_execute_buffer_QueryInterface,
807 d3d_execute_buffer_AddRef,
808 d3d_execute_buffer_Release,
809 d3d_execute_buffer_Initialize,
810 d3d_execute_buffer_Lock,
811 d3d_execute_buffer_Unlock,
812 d3d_execute_buffer_SetExecuteData,
813 d3d_execute_buffer_GetExecuteData,
814 d3d_execute_buffer_Validate,
815 d3d_execute_buffer_Optimize,
818 HRESULT d3d_execute_buffer_init(struct d3d_execute_buffer *execute_buffer,
819 struct d3d_device *device, D3DEXECUTEBUFFERDESC *desc)
821 execute_buffer->IDirect3DExecuteBuffer_iface.lpVtbl = &d3d_execute_buffer_vtbl;
822 execute_buffer->ref = 1;
823 execute_buffer->d3ddev = device;
825 /* Initializes memory */
826 memcpy(&execute_buffer->desc, desc, desc->dwSize);
828 /* No buffer given */
829 if (!(execute_buffer->desc.dwFlags & D3DDEB_LPDATA))
830 execute_buffer->desc.lpData = NULL;
832 /* No buffer size given */
833 if (!(execute_buffer->desc.dwFlags & D3DDEB_BUFSIZE))
834 execute_buffer->desc.dwBufferSize = 0;
836 /* Create buffer if asked */
837 if (!execute_buffer->desc.lpData && execute_buffer->desc.dwBufferSize)
839 execute_buffer->need_free = TRUE;
840 execute_buffer->desc.lpData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, execute_buffer->desc.dwBufferSize);
841 if (!execute_buffer->desc.lpData)
843 ERR("Failed to allocate execute buffer data.\n");
844 return DDERR_OUTOFMEMORY;
848 execute_buffer->desc.dwFlags |= D3DDEB_LPDATA;
850 return D3D_OK;
853 struct d3d_execute_buffer *unsafe_impl_from_IDirect3DExecuteBuffer(IDirect3DExecuteBuffer *iface)
855 if (!iface)
856 return NULL;
857 assert(iface->lpVtbl == &d3d_execute_buffer_vtbl);
859 return impl_from_IDirect3DExecuteBuffer(iface);