mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / ddraw / executebuffer.c
blob7b477a79415c9ab4699ec07a4c6a0a628dcd3d2c
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 library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "ddraw_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
25 /*****************************************************************************
26 * _dump_executedata
27 * _dump_D3DEXECUTEBUFFERDESC
29 * Debug functions which write the executebuffer data to the console
31 *****************************************************************************/
33 static void _dump_executedata(const D3DEXECUTEDATA *lpData) {
34 TRACE("dwSize : %d\n", lpData->dwSize);
35 TRACE("Vertex Offset : %d Count : %d\n", lpData->dwVertexOffset, lpData->dwVertexCount);
36 TRACE("Instruction Offset : %d Length : %d\n", lpData->dwInstructionOffset, lpData->dwInstructionLength);
37 TRACE("HVertex Offset : %d\n", lpData->dwHVertexOffset);
40 static void _dump_D3DEXECUTEBUFFERDESC(const D3DEXECUTEBUFFERDESC *lpDesc) {
41 TRACE("dwSize : %d\n", lpDesc->dwSize);
42 TRACE("dwFlags : %x\n", lpDesc->dwFlags);
43 TRACE("dwCaps : %x\n", lpDesc->dwCaps);
44 TRACE("dwBufferSize : %d\n", lpDesc->dwBufferSize);
45 TRACE("lpData : %p\n", lpDesc->lpData);
48 HRESULT d3d_execute_buffer_execute(struct d3d_execute_buffer *buffer, struct d3d_device *device)
50 DWORD is = buffer->data.dwInstructionOffset;
51 char *instr = (char *)buffer->desc.lpData + is;
52 unsigned int i, primitive_size;
53 struct wined3d_map_desc map_desc;
54 struct wined3d_box box = {0};
55 HRESULT hr;
57 TRACE("ExecuteData :\n");
58 if (TRACE_ON(ddraw))
59 _dump_executedata(&(buffer->data));
61 for (;;)
63 D3DINSTRUCTION *current = (D3DINSTRUCTION *)instr;
64 BYTE size;
65 WORD count;
67 count = current->wCount;
68 size = current->bSize;
69 instr += sizeof(*current);
70 primitive_size = 0;
72 switch (current->bOpcode)
74 case D3DOP_POINT:
76 const D3DPOINT *p = (D3DPOINT *)instr;
77 wined3d_device_set_primitive_type(device->wined3d_device, WINED3D_PT_POINTLIST, 0);
78 wined3d_stateblock_set_stream_source(device->state, 0,
79 buffer->dst_vertex_buffer, 0, sizeof(D3DTLVERTEX));
80 wined3d_stateblock_set_vertex_declaration(device->state,
81 ddraw_find_decl(device->ddraw, D3DFVF_TLVERTEX));
83 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
84 d3d_device_sync_surfaces(device);
85 for (i = 0; i < count; ++i)
86 wined3d_device_context_draw(device->immediate_context, p[i].wFirst, p[i].wCount, 0, 0);
88 instr += sizeof(*p) * count;
89 break;
92 case D3DOP_LINE:
93 primitive_size = 2;
94 wined3d_device_set_primitive_type(device->wined3d_device, WINED3D_PT_LINELIST, 0);
95 /* Drop through. */
96 case D3DOP_TRIANGLE:
98 WORD *indices;
99 unsigned int index_pos = buffer->index_pos, index_count;
100 TRACE("TRIANGLE (%d)\n", count);
102 if (!count)
103 break;
105 if (!primitive_size)
107 wined3d_device_set_primitive_type(device->wined3d_device, WINED3D_PT_TRIANGLELIST, 0);
108 primitive_size = 3;
111 index_count = count * primitive_size;
112 if (buffer->index_size < index_count)
114 unsigned int new_size = max(buffer->index_size * 2, index_count);
115 struct wined3d_buffer *new_buffer;
116 struct wined3d_buffer_desc desc;
118 desc.byte_width = new_size * sizeof(*indices);
119 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_STATICDECL;
120 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
121 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
122 desc.misc_flags = 0;
123 desc.structure_byte_stride = 0;
125 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
126 NULL, NULL, &ddraw_null_wined3d_parent_ops, &new_buffer)))
127 return hr;
129 buffer->index_size = new_size;
130 if (buffer->index_buffer)
131 wined3d_buffer_decref(buffer->index_buffer);
132 buffer->index_buffer = new_buffer;
133 index_pos = 0;
135 else if (buffer->index_size - index_count < index_pos)
137 index_pos = 0;
140 box.left = index_pos * sizeof(*indices);
141 box.right = (index_pos + index_count) * sizeof(*indices);
142 if (FAILED(hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->index_buffer), 0, &map_desc,
143 &box, WINED3D_MAP_WRITE | (index_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD))))
144 return hr;
145 indices = map_desc.data;
147 for (i = 0; i < count; ++i)
149 D3DTRIANGLE *ci = (D3DTRIANGLE *)instr;
150 TRACE(" v1: %d v2: %d v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3);
151 TRACE(" Flags : ");
152 if (TRACE_ON(ddraw))
154 /* Wireframe */
155 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
156 TRACE("EDGEENABLE1 ");
157 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2)
158 TRACE("EDGEENABLE2 ");
159 if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
160 TRACE("EDGEENABLE3 ");
161 /* Strips / Fans */
162 if (ci->wFlags == D3DTRIFLAG_EVEN)
163 TRACE("EVEN ");
164 if (ci->wFlags == D3DTRIFLAG_ODD)
165 TRACE("ODD ");
166 if (ci->wFlags == D3DTRIFLAG_START)
167 TRACE("START ");
168 if ((ci->wFlags > 0) && (ci->wFlags < 30))
169 TRACE("STARTFLAT(%u) ", ci->wFlags);
170 TRACE("\n");
173 switch (primitive_size)
175 case 3:
176 indices[(i * primitive_size) + 2] = ci->u3.v3;
177 /* Drop through. */
178 case 2:
179 indices[(i * primitive_size) + 1] = ci->u2.v2;
180 indices[(i * primitive_size) ] = ci->u1.v1;
182 instr += size;
185 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->index_buffer), 0);
187 wined3d_stateblock_set_stream_source(device->state, 0,
188 buffer->dst_vertex_buffer, 0, sizeof(D3DTLVERTEX));
189 wined3d_stateblock_set_vertex_declaration(device->state,
190 ddraw_find_decl(device->ddraw, D3DFVF_TLVERTEX));
191 wined3d_stateblock_set_index_buffer(device->state, buffer->index_buffer, WINED3DFMT_R16_UINT);
192 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
193 d3d_device_sync_surfaces(device);
194 wined3d_device_context_draw_indexed(device->immediate_context, 0, index_pos, index_count, 0, 0);
196 buffer->index_pos = index_pos + index_count;
197 break;
200 case D3DOP_MATRIXLOAD:
201 WARN("MATRIXLOAD-s (%u)\n", count);
202 instr += count * size;
203 break;
205 case D3DOP_MATRIXMULTIPLY:
206 TRACE("MATRIXMULTIPLY (%d)\n", count);
207 for (i = 0; i < count; ++i)
209 D3DMATRIXMULTIPLY *ci = (D3DMATRIXMULTIPLY *)instr;
210 struct wined3d_matrix *a, *b, *c;
212 a = ddraw_get_object(&device->handle_table, ci->hDestMatrix - 1, DDRAW_HANDLE_MATRIX);
213 b = ddraw_get_object(&device->handle_table, ci->hSrcMatrix1 - 1, DDRAW_HANDLE_MATRIX);
214 c = ddraw_get_object(&device->handle_table, ci->hSrcMatrix2 - 1, DDRAW_HANDLE_MATRIX);
216 if (!a || !b || !c)
218 ERR("Invalid matrix handle (a %#x -> %p, b %#x -> %p, c %#x -> %p).\n",
219 ci->hDestMatrix, a, ci->hSrcMatrix1, b, ci->hSrcMatrix2, c);
221 else
223 TRACE("dst %p, src1 %p, src2 %p.\n", a, b, c);
224 multiply_matrix(a, c, b);
227 instr += size;
229 break;
231 case D3DOP_STATETRANSFORM:
232 TRACE("STATETRANSFORM (%d)\n", count);
233 for (i = 0; i < count; ++i)
235 D3DSTATE *ci = (D3DSTATE *)instr;
236 D3DMATRIX *m;
238 m = ddraw_get_object(&device->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATRIX);
239 if (!m)
241 ERR("Invalid matrix handle %#x.\n", ci->u2.dwArg[0]);
243 else
245 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_WORLD)
246 device->world = ci->u2.dwArg[0];
247 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_VIEW)
248 device->view = ci->u2.dwArg[0];
249 if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_PROJECTION)
250 device->proj = ci->u2.dwArg[0];
251 IDirect3DDevice3_SetTransform(&device->IDirect3DDevice3_iface,
252 ci->u1.dtstTransformStateType, m);
255 instr += size;
257 break;
259 case D3DOP_STATELIGHT:
260 TRACE("STATELIGHT (%d)\n", count);
261 for (i = 0; i < count; ++i)
263 D3DSTATE *ci = (D3DSTATE *)instr;
265 if (FAILED(IDirect3DDevice3_SetLightState(&device->IDirect3DDevice3_iface,
266 ci->u1.dlstLightStateType, ci->u2.dwArg[0])))
267 WARN("Failed to set light state.\n");
269 instr += size;
271 break;
273 case D3DOP_STATERENDER:
274 TRACE("STATERENDER (%d)\n", count);
275 for (i = 0; i < count; ++i)
277 D3DSTATE *ci = (D3DSTATE *)instr;
279 if (FAILED(IDirect3DDevice3_SetRenderState(&device->IDirect3DDevice3_iface,
280 ci->u1.drstRenderStateType, ci->u2.dwArg[0])))
281 WARN("Failed to set render state.\n");
283 instr += size;
285 break;
287 case D3DOP_PROCESSVERTICES:
288 TRACE("PROCESSVERTICES (%d)\n", count);
290 for (i = 0; i < count; ++i)
292 D3DPROCESSVERTICES *ci = (D3DPROCESSVERTICES *)instr;
293 DWORD op = ci->dwFlags & D3DPROCESSVERTICES_OPMASK;
295 TRACE(" start %u, dest %u, count %u, flags %#x.\n",
296 ci->wStart, ci->wDest, ci->dwCount, ci->dwFlags);
298 if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
299 FIXME("D3DPROCESSVERTICES_UPDATEEXTENTS not implemented.\n");
300 if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
301 FIXME("D3DPROCESSVERTICES_NOCOLOR not implemented.\n");
303 switch (op)
305 case D3DPROCESSVERTICES_TRANSFORMLIGHT:
306 case D3DPROCESSVERTICES_TRANSFORM:
307 wined3d_stateblock_set_stream_source(device->state, 0,
308 buffer->src_vertex_buffer, buffer->src_vertex_pos * sizeof(D3DVERTEX), sizeof(D3DVERTEX));
309 wined3d_stateblock_set_render_state(device->state, WINED3D_RS_LIGHTING,
310 op == D3DPROCESSVERTICES_TRANSFORMLIGHT && !!device->material);
311 wined3d_stateblock_set_vertex_declaration(device->state,
312 ddraw_find_decl(device->ddraw, op == D3DPROCESSVERTICES_TRANSFORMLIGHT
313 ? D3DFVF_VERTEX : D3DFVF_LVERTEX));
314 wined3d_device_apply_stateblock(device->wined3d_device, device->state);
315 d3d_device_sync_surfaces(device);
316 wined3d_device_process_vertices(device->wined3d_device, ci->wStart, ci->wDest,
317 ci->dwCount, buffer->dst_vertex_buffer, NULL, 0, D3DFVF_TLVERTEX);
318 break;
320 case D3DPROCESSVERTICES_COPY:
321 box.left = (buffer->src_vertex_pos + ci->wStart) * sizeof(D3DTLVERTEX);
322 box.right = box.left + ci->dwCount * sizeof(D3DTLVERTEX);
323 box.top = box.front = 0;
324 box.bottom = box.back = 1;
325 wined3d_device_context_copy_sub_resource_region(device->immediate_context,
326 wined3d_buffer_get_resource(buffer->dst_vertex_buffer), 0,
327 ci->wDest * sizeof(D3DTLVERTEX), 0, 0,
328 wined3d_buffer_get_resource(buffer->src_vertex_buffer), 0, &box, 0);
329 break;
331 default:
332 FIXME("Unhandled vertex processing op %#x.\n", op);
333 break;
336 instr += size;
338 break;
340 case D3DOP_TEXTURELOAD:
341 TRACE("TEXTURELOAD (%u)\n", count);
343 for (i = 0; i < count; ++i)
345 D3DTEXTURELOAD *ci = (D3DTEXTURELOAD *)instr;
346 struct ddraw_surface *dst, *src;
348 instr += size;
350 if (!(dst = ddraw_get_object(&device->handle_table,
351 ci->hDestTexture - 1, DDRAW_HANDLE_SURFACE)))
353 WARN("Invalid destination texture handle %#x.\n", ci->hDestTexture);
354 continue;
356 if (!(src = ddraw_get_object(&device->handle_table,
357 ci->hSrcTexture - 1, DDRAW_HANDLE_SURFACE)))
359 WARN("Invalid source texture handle %#x.\n", ci->hSrcTexture);
360 continue;
363 IDirect3DTexture2_Load(&dst->IDirect3DTexture2_iface, &src->IDirect3DTexture2_iface);
365 break;
367 case D3DOP_EXIT:
368 TRACE("EXIT (%u)\n", count);
369 instr += size;
370 goto end_of_buffer;
372 case D3DOP_BRANCHFORWARD:
373 TRACE("BRANCHFORWARD (%d)\n", count);
374 for (i = 0; i < count; ++i)
376 D3DBRANCH *ci = (D3DBRANCH *)instr;
378 if ((buffer->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue)
380 if (!ci->bNegate)
382 TRACE(" Branch to %d\n", ci->dwOffset);
383 if (ci->dwOffset) {
384 instr = (char*)current + ci->dwOffset;
385 break;
389 else
391 if (ci->bNegate)
393 TRACE(" Branch to %d\n", ci->dwOffset);
394 if (ci->dwOffset) {
395 instr = (char*)current + ci->dwOffset;
396 break;
401 instr += size;
403 break;
405 case D3DOP_SPAN:
406 WARN("SPAN-s (%u)\n", count);
407 instr += count * size;
408 break;
410 case D3DOP_SETSTATUS:
411 TRACE("SETSTATUS (%d)\n", count);
412 for (i = 0; i < count; ++i)
414 buffer->data.dsStatus = *(D3DSTATUS *)instr;
415 instr += size;
417 break;
419 default:
420 ERR("Unhandled OpCode %#x.\n",current->bOpcode);
421 instr += count * size;
422 break;
426 end_of_buffer:
427 return D3D_OK;
430 static inline struct d3d_execute_buffer *impl_from_IDirect3DExecuteBuffer(IDirect3DExecuteBuffer *iface)
432 return CONTAINING_RECORD(iface, struct d3d_execute_buffer, IDirect3DExecuteBuffer_iface);
435 static HRESULT WINAPI d3d_execute_buffer_QueryInterface(IDirect3DExecuteBuffer *iface, REFIID iid, void **out)
437 TRACE("iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
439 if (IsEqualGUID(&IID_IDirect3DExecuteBuffer, iid)
440 || IsEqualGUID(&IID_IUnknown, iid))
442 IDirect3DExecuteBuffer_AddRef(iface);
443 *out = iface;
444 return S_OK;
447 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
449 *out = NULL;
450 return E_NOINTERFACE;
453 /*****************************************************************************
454 * IDirect3DExecuteBuffer::AddRef
456 * A normal AddRef method, nothing special
458 * Returns:
459 * The new refcount
461 *****************************************************************************/
462 static ULONG WINAPI d3d_execute_buffer_AddRef(IDirect3DExecuteBuffer *iface)
464 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
465 ULONG ref = InterlockedIncrement(&buffer->ref);
467 TRACE("%p increasing refcount to %u.\n", buffer, ref);
469 return ref;
472 /*****************************************************************************
473 * IDirect3DExecuteBuffer::Release
475 * A normal Release method, nothing special
477 * Returns:
478 * The new refcount
480 *****************************************************************************/
481 static ULONG WINAPI d3d_execute_buffer_Release(IDirect3DExecuteBuffer *iface)
483 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
484 ULONG ref = InterlockedDecrement(&buffer->ref);
486 TRACE("%p decreasing refcount to %u.\n", buffer, ref);
488 if (!ref)
490 if (buffer->need_free)
491 heap_free(buffer->desc.lpData);
492 if (buffer->index_buffer)
493 wined3d_buffer_decref(buffer->index_buffer);
494 if (buffer->dst_vertex_buffer)
496 wined3d_buffer_decref(buffer->src_vertex_buffer);
497 wined3d_buffer_decref(buffer->dst_vertex_buffer);
499 heap_free(buffer);
502 return ref;
505 /*****************************************************************************
506 * IDirect3DExecuteBuffer::Initialize
508 * Initializes the Execute Buffer. This method exists for COM compliance
509 * Nothing to do here.
511 * Returns:
512 * D3D_OK
514 *****************************************************************************/
515 static HRESULT WINAPI d3d_execute_buffer_Initialize(IDirect3DExecuteBuffer *iface,
516 IDirect3DDevice *device, D3DEXECUTEBUFFERDESC *desc)
518 TRACE("iface %p, device %p, desc %p.\n", iface, device, desc);
520 return D3D_OK;
523 /*****************************************************************************
524 * IDirect3DExecuteBuffer::Lock
526 * Locks the buffer, so the app can write into it.
528 * Params:
529 * Desc: Pointer to return the buffer description. This Description contains
530 * a pointer to the buffer data.
532 * Returns:
533 * This implementation always returns D3D_OK
535 *****************************************************************************/
536 static HRESULT WINAPI d3d_execute_buffer_Lock(IDirect3DExecuteBuffer *iface, D3DEXECUTEBUFFERDESC *desc)
538 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
539 DWORD dwSize;
541 TRACE("iface %p, desc %p.\n", iface, desc);
543 dwSize = desc->dwSize;
544 memcpy(desc, &buffer->desc, dwSize);
546 if (TRACE_ON(ddraw))
548 TRACE(" Returning description :\n");
549 _dump_D3DEXECUTEBUFFERDESC(desc);
551 return D3D_OK;
554 /*****************************************************************************
555 * IDirect3DExecuteBuffer::Unlock
557 * Unlocks the buffer. We don't have anything to do here
559 * Returns:
560 * This implementation always returns D3D_OK
562 *****************************************************************************/
563 static HRESULT WINAPI d3d_execute_buffer_Unlock(IDirect3DExecuteBuffer *iface)
565 TRACE("iface %p.\n", iface);
567 return D3D_OK;
570 /*****************************************************************************
571 * IDirect3DExecuteBuffer::SetExecuteData
573 * Sets the execute data. This data is used to describe the buffer's content
575 * Params:
576 * Data: Pointer to a D3DEXECUTEDATA structure containing the data to
577 * assign
579 * Returns:
580 * D3D_OK on success
581 * DDERR_OUTOFMEMORY if the vertex buffer allocation failed
583 *****************************************************************************/
584 static HRESULT WINAPI d3d_execute_buffer_SetExecuteData(IDirect3DExecuteBuffer *iface, D3DEXECUTEDATA *data)
586 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
587 struct wined3d_map_desc map_desc;
588 struct wined3d_box box = {0};
589 HRESULT hr;
590 DWORD buf_size = buffer->desc.dwBufferSize, copy_size;
592 TRACE("iface %p, data %p.\n", iface, data);
594 if (data->dwSize != sizeof(*data))
596 WARN("data->dwSize is %u, returning DDERR_INVALIDPARAMS.\n", data->dwSize);
597 return DDERR_INVALIDPARAMS;
600 /* Skip past previous vertex data. */
601 buffer->src_vertex_pos += buffer->data.dwVertexCount;
603 if (buffer->vertex_size < data->dwVertexCount)
605 unsigned int new_size = max(data->dwVertexCount, buffer->vertex_size * 2);
606 struct wined3d_buffer *src_buffer, *dst_buffer;
607 struct wined3d_buffer_desc desc;
609 desc.byte_width = new_size * sizeof(D3DVERTEX);
610 desc.usage = 0;
611 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
612 desc.access = WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
613 desc.misc_flags = 0;
614 desc.structure_byte_stride = 0;
616 if (FAILED(hr = wined3d_buffer_create(buffer->d3ddev->wined3d_device, &desc,
617 NULL, NULL, &ddraw_null_wined3d_parent_ops, &src_buffer)))
618 return hr;
620 desc.byte_width = new_size * sizeof(D3DTLVERTEX);
621 desc.usage = WINED3DUSAGE_STATICDECL;
622 desc.access = WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_MAP_W;
624 if (FAILED(hr = wined3d_buffer_create(buffer->d3ddev->wined3d_device, &desc,
625 NULL, NULL, &ddraw_null_wined3d_parent_ops, &dst_buffer)))
627 wined3d_buffer_decref(src_buffer);
628 return hr;
631 if (buffer->dst_vertex_buffer)
633 wined3d_buffer_decref(buffer->src_vertex_buffer);
634 wined3d_buffer_decref(buffer->dst_vertex_buffer);
636 buffer->src_vertex_buffer = src_buffer;
637 buffer->dst_vertex_buffer = dst_buffer;
638 buffer->vertex_size = new_size;
639 buffer->src_vertex_pos = 0;
641 else if (buffer->vertex_size - data->dwVertexCount < buffer->src_vertex_pos)
643 buffer->src_vertex_pos = 0;
646 if (data->dwVertexCount && (!buf_size || data->dwVertexOffset < buf_size))
648 box.left = buffer->src_vertex_pos * sizeof(D3DVERTEX);
649 box.right = box.left + data->dwVertexCount * sizeof(D3DVERTEX);
650 if (FAILED(hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->src_vertex_buffer),
651 0, &map_desc, &box, WINED3D_MAP_WRITE)))
652 return hr;
654 copy_size = data->dwVertexCount * sizeof(D3DVERTEX);
655 if (buf_size)
656 copy_size = min(copy_size, buf_size - data->dwVertexOffset);
658 memcpy(map_desc.data, ((BYTE *)buffer->desc.lpData) + data->dwVertexOffset, copy_size);
660 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->src_vertex_buffer), 0);
663 memcpy(&buffer->data, data, data->dwSize);
665 if (TRACE_ON(ddraw))
666 _dump_executedata(data);
668 return D3D_OK;
671 /*****************************************************************************
672 * IDirect3DExecuteBuffer::GetExecuteData
674 * Returns the data in the execute buffer
676 * Params:
677 * Data: Pointer to a D3DEXECUTEDATA structure used to return data
679 * Returns:
680 * D3D_OK on success
682 *****************************************************************************/
683 static HRESULT WINAPI d3d_execute_buffer_GetExecuteData(IDirect3DExecuteBuffer *iface, D3DEXECUTEDATA *data)
685 struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
687 TRACE("iface %p, data %p.\n", iface, data);
689 /* Tests show that dwSize is ignored. */
690 memcpy(data, &buffer->data, sizeof(*data));
692 if (TRACE_ON(ddraw))
694 TRACE("Returning data :\n");
695 _dump_executedata(data);
698 return DD_OK;
701 /*****************************************************************************
702 * IDirect3DExecuteBuffer::Validate
704 * DirectX 5 SDK: "The IDirect3DExecuteBuffer::Validate method is not
705 * currently implemented"
707 * Params:
710 * Returns:
711 * DDERR_UNSUPPORTED, because it's not implemented in Windows.
713 *****************************************************************************/
714 static HRESULT WINAPI d3d_execute_buffer_Validate(IDirect3DExecuteBuffer *iface,
715 DWORD *offset, LPD3DVALIDATECALLBACK callback, void *context, DWORD reserved)
717 TRACE("iface %p, offset %p, callback %p, context %p, reserved %#x.\n",
718 iface, offset, callback, context, reserved);
720 WARN("Not implemented.\n");
722 return DDERR_UNSUPPORTED; /* Unchecked */
725 /*****************************************************************************
726 * IDirect3DExecuteBuffer::Optimize
728 * DirectX5 SDK: "The IDirect3DExecuteBuffer::Optimize method is not
729 * currently supported"
731 * Params:
732 * Dummy: Seems to be an unused dummy ;)
734 * Returns:
735 * DDERR_UNSUPPORTED, because it's not implemented in Windows.
737 *****************************************************************************/
738 static HRESULT WINAPI d3d_execute_buffer_Optimize(IDirect3DExecuteBuffer *iface, DWORD reserved)
740 TRACE("iface %p, reserved %#x.\n", iface, reserved);
742 WARN("Not implemented.\n");
744 return DDERR_UNSUPPORTED; /* Unchecked */
747 static const struct IDirect3DExecuteBufferVtbl d3d_execute_buffer_vtbl =
749 d3d_execute_buffer_QueryInterface,
750 d3d_execute_buffer_AddRef,
751 d3d_execute_buffer_Release,
752 d3d_execute_buffer_Initialize,
753 d3d_execute_buffer_Lock,
754 d3d_execute_buffer_Unlock,
755 d3d_execute_buffer_SetExecuteData,
756 d3d_execute_buffer_GetExecuteData,
757 d3d_execute_buffer_Validate,
758 d3d_execute_buffer_Optimize,
761 HRESULT d3d_execute_buffer_init(struct d3d_execute_buffer *execute_buffer,
762 struct d3d_device *device, D3DEXECUTEBUFFERDESC *desc)
764 execute_buffer->IDirect3DExecuteBuffer_iface.lpVtbl = &d3d_execute_buffer_vtbl;
765 execute_buffer->ref = 1;
766 execute_buffer->d3ddev = device;
768 /* Initializes memory */
769 memcpy(&execute_buffer->desc, desc, desc->dwSize);
771 /* No buffer given */
772 if (!(execute_buffer->desc.dwFlags & D3DDEB_LPDATA))
773 execute_buffer->desc.lpData = NULL;
775 /* No buffer size given */
776 if (!(execute_buffer->desc.dwFlags & D3DDEB_BUFSIZE))
777 execute_buffer->desc.dwBufferSize = 0;
779 /* Create buffer if asked */
780 if (!execute_buffer->desc.lpData && execute_buffer->desc.dwBufferSize)
782 execute_buffer->need_free = TRUE;
783 if (!(execute_buffer->desc.lpData = heap_alloc_zero(execute_buffer->desc.dwBufferSize)))
785 ERR("Failed to allocate execute buffer data.\n");
786 return DDERR_OUTOFMEMORY;
790 execute_buffer->desc.dwFlags |= D3DDEB_LPDATA;
792 return D3D_OK;
795 struct d3d_execute_buffer *unsafe_impl_from_IDirect3DExecuteBuffer(IDirect3DExecuteBuffer *iface)
797 if (!iface)
798 return NULL;
799 assert(iface->lpVtbl == &d3d_execute_buffer_vtbl);
801 return impl_from_IDirect3DExecuteBuffer(iface);