xref: /reactos/dll/directx/wine/ddraw/executebuffer.c (revision b819608e)
1 /* Direct3D ExecuteBuffer
2  * Copyright (c) 1998-2004 Lionel ULMER
3  * Copyright (c) 2002-2004 Christian Costa
4  * Copyright (c) 2006      Stefan Dösinger
5  *
6  * This file contains the implementation of IDirect3DExecuteBuffer.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 #include "ddraw_private.h"
24 
25 /*****************************************************************************
26  * _dump_executedata
27  * _dump_D3DEXECUTEBUFFERDESC
28  *
29  * Debug functions which write the executebuffer data to the console
30  *
31  *****************************************************************************/
32 
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);
38 }
39 
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);
46 }
47 
48 HRESULT d3d_execute_buffer_execute(struct d3d_execute_buffer *buffer,
49         struct d3d_device *device, struct d3d_viewport *viewport)
50 {
51     DWORD vs = buffer->data.dwVertexOffset;
52     DWORD is = buffer->data.dwInstructionOffset;
53     char *instr = (char *)buffer->desc.lpData + is;
54 
55     if (viewport->active_device != device)
56     {
57         WARN("Viewport %p active device is %p.\n",
58                 viewport, viewport->active_device);
59         return DDERR_INVALIDPARAMS;
60     }
61 
62     /* Activate the viewport */
63     viewport_activate(viewport, FALSE);
64 
65     TRACE("ExecuteData :\n");
66     if (TRACE_ON(ddraw))
67         _dump_executedata(&(buffer->data));
68 
69     for (;;)
70     {
71         D3DINSTRUCTION *current = (D3DINSTRUCTION *)instr;
72 	BYTE size;
73 	WORD count;
74 
75 	count = current->wCount;
76 	size = current->bSize;
77 	instr += sizeof(D3DINSTRUCTION);
78 
79 	switch (current->bOpcode) {
80 	    case D3DOP_POINT: {
81 	        WARN("POINT-s          (%d)\n", count);
82 		instr += count * size;
83 	    } break;
84 
85 	    case D3DOP_LINE: {
86 	        WARN("LINE-s           (%d)\n", count);
87 		instr += count * size;
88 	    } break;
89 
90 	    case D3DOP_TRIANGLE: {
91                 DWORD i;
92                 D3DTLVERTEX *tl_vx = buffer->vertex_data;
93 		TRACE("TRIANGLE         (%d)\n", count);
94 
95                 if (buffer->nb_indices < count * 3)
96                 {
97                     buffer->nb_indices = count * 3;
98                     HeapFree(GetProcessHeap(), 0, buffer->indices);
99                     buffer->indices = HeapAlloc(GetProcessHeap(), 0, sizeof(*buffer->indices) * buffer->nb_indices);
100                 }
101 
102                 for (i = 0; i < count; ++i)
103                 {
104                     D3DTRIANGLE *ci = (D3DTRIANGLE *)instr;
105 		    TRACE("  v1: %d  v2: %d  v3: %d\n",ci->u1.v1, ci->u2.v2, ci->u3.v3);
106 		    TRACE("  Flags : ");
107                     if (TRACE_ON(ddraw))
108                     {
109                         /* Wireframe */
110                         if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
111                             TRACE("EDGEENABLE1 ");
112                         if (ci->wFlags & D3DTRIFLAG_EDGEENABLE2)
113                             TRACE("EDGEENABLE2 ");
114                         if (ci->wFlags & D3DTRIFLAG_EDGEENABLE1)
115                             TRACE("EDGEENABLE3 ");
116                         /* Strips / Fans */
117                         if (ci->wFlags == D3DTRIFLAG_EVEN)
118                             TRACE("EVEN ");
119                         if (ci->wFlags == D3DTRIFLAG_ODD)
120                             TRACE("ODD ");
121                         if (ci->wFlags == D3DTRIFLAG_START)
122                             TRACE("START ");
123                         if ((ci->wFlags > 0) && (ci->wFlags < 30))
124                             TRACE("STARTFLAT(%u) ", ci->wFlags);
125                         TRACE("\n");
126                     }
127                     buffer->indices[(i * 3)    ] = ci->u1.v1;
128                     buffer->indices[(i * 3) + 1] = ci->u2.v2;
129                     buffer->indices[(i * 3) + 2] = ci->u3.v3;
130                     instr += size;
131                 }
132                 IDirect3DDevice7_DrawIndexedPrimitive(&device->IDirect3DDevice7_iface,
133                         D3DPT_TRIANGLELIST, D3DFVF_TLVERTEX, tl_vx, buffer->nb_vertices,
134                         buffer->indices, count * 3, 0);
135 	    } break;
136 
137 	    case D3DOP_MATRIXLOAD:
138 	        WARN("MATRIXLOAD-s     (%d)\n", count);
139 	        instr += count * size;
140 	        break;
141 
142 	    case D3DOP_MATRIXMULTIPLY: {
143                 DWORD  i;
144 		TRACE("MATRIXMULTIPLY   (%d)\n", count);
145 
146                 for (i = 0; i < count; ++i)
147                 {
148                     D3DMATRIXMULTIPLY *ci = (D3DMATRIXMULTIPLY *)instr;
149                     D3DMATRIX *a, *b, *c;
150 
151                     a = ddraw_get_object(&device->handle_table, ci->hDestMatrix - 1, DDRAW_HANDLE_MATRIX);
152                     b = ddraw_get_object(&device->handle_table, ci->hSrcMatrix1 - 1, DDRAW_HANDLE_MATRIX);
153                     c = ddraw_get_object(&device->handle_table, ci->hSrcMatrix2 - 1, DDRAW_HANDLE_MATRIX);
154 
155                     if (!a || !b || !c)
156                     {
157                         ERR("Invalid matrix handle (a %#x -> %p, b %#x -> %p, c %#x -> %p).\n",
158                                 ci->hDestMatrix, a, ci->hSrcMatrix1, b, ci->hSrcMatrix2, c);
159                     }
160                     else
161                     {
162                         TRACE("dst %p, src1 %p, src2 %p.\n", a, b, c);
163                         multiply_matrix(a, c, b);
164                     }
165 
166                     instr += size;
167 		}
168 	    } break;
169 
170 	    case D3DOP_STATETRANSFORM: {
171                 DWORD i;
172 		TRACE("STATETRANSFORM   (%d)\n", count);
173 
174                 for (i = 0; i < count; ++i)
175                 {
176                     D3DSTATE *ci = (D3DSTATE *)instr;
177                     D3DMATRIX *m;
178 
179                     m = ddraw_get_object(&device->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATRIX);
180                     if (!m)
181                     {
182                         ERR("Invalid matrix handle %#x.\n", ci->u2.dwArg[0]);
183                     }
184                     else
185                     {
186                         if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_WORLD)
187                             device->world = ci->u2.dwArg[0];
188                         if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_VIEW)
189                             device->view = ci->u2.dwArg[0];
190                         if (ci->u1.dtstTransformStateType == D3DTRANSFORMSTATE_PROJECTION)
191                             device->proj = ci->u2.dwArg[0];
192                         IDirect3DDevice7_SetTransform(&device->IDirect3DDevice7_iface,
193                                 ci->u1.dtstTransformStateType, m);
194                     }
195 
196                     instr += size;
197                 }
198 	    } break;
199 
200 	    case D3DOP_STATELIGHT: {
201                 DWORD i;
202 		TRACE("STATELIGHT       (%d)\n", count);
203 
204                 for (i = 0; i < count; ++i)
205                 {
206                     D3DSTATE *ci = (D3DSTATE *)instr;
207 
208 		    TRACE("(%08x,%08x)\n", ci->u1.dlstLightStateType, ci->u2.dwArg[0]);
209 
210 		    if (!ci->u1.dlstLightStateType || (ci->u1.dlstLightStateType > D3DLIGHTSTATE_COLORVERTEX))
211 			ERR("Unexpected Light State Type %d\n", ci->u1.dlstLightStateType);
212                     else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
213                     {
214                         struct d3d_material *m;
215 
216                         m = ddraw_get_object(&device->handle_table, ci->u2.dwArg[0] - 1, DDRAW_HANDLE_MATERIAL);
217                         if (!m)
218                             ERR("Invalid material handle %#x.\n", ci->u2.dwArg[0]);
219                         else
220                             material_activate(m);
221                     }
222                     else if (ci->u1.dlstLightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
223                     {
224 			switch (ci->u2.dwArg[0]) {
225 			    case D3DCOLOR_MONO:
226 				ERR("DDCOLOR_MONO should not happen!\n");
227 				break;
228 			    case D3DCOLOR_RGB:
229 				/* We are already in this mode */
230 				break;
231 			    default:
232 				ERR("Unknown color model!\n");
233 			}
234 		    } else {
235 			D3DRENDERSTATETYPE rs = 0;
236 			switch (ci->u1.dlstLightStateType) {
237 
238 			    case D3DLIGHTSTATE_AMBIENT:       /* 2 */
239 				rs = D3DRENDERSTATE_AMBIENT;
240 				break;
241 			    case D3DLIGHTSTATE_FOGMODE:       /* 4 */
242 				rs = D3DRENDERSTATE_FOGVERTEXMODE;
243 				break;
244 			    case D3DLIGHTSTATE_FOGSTART:      /* 5 */
245 				rs = D3DRENDERSTATE_FOGSTART;
246 				break;
247 			    case D3DLIGHTSTATE_FOGEND:        /* 6 */
248 				rs = D3DRENDERSTATE_FOGEND;
249 				break;
250 			    case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
251 				rs = D3DRENDERSTATE_FOGDENSITY;
252 				break;
253 			    case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
254 				rs = D3DRENDERSTATE_COLORVERTEX;
255 				break;
256 			    default:
257 				break;
258 			}
259 
260                         IDirect3DDevice7_SetRenderState(&device->IDirect3DDevice7_iface, rs, ci->u2.dwArg[0]);
261 		    }
262 
263 		    instr += size;
264 		}
265 	    } break;
266 
267 	    case D3DOP_STATERENDER: {
268                 DWORD i;
269                 IDirect3DDevice2 *d3d_device2 = &device->IDirect3DDevice2_iface;
270 		TRACE("STATERENDER      (%d)\n", count);
271 
272                 for (i = 0; i < count; ++i)
273                 {
274                     D3DSTATE *ci = (D3DSTATE *)instr;
275 
276                     IDirect3DDevice2_SetRenderState(d3d_device2, ci->u1.drstRenderStateType, ci->u2.dwArg[0]);
277 
278 		    instr += size;
279 		}
280 	    } break;
281 
282             case D3DOP_PROCESSVERTICES:
283             {
284                 /* TODO: Share code with IDirect3DVertexBuffer::ProcessVertices and / or
285                  * IWineD3DDevice::ProcessVertices
286                  */
287                 DWORD i;
288                 D3DMATRIX view_mat, world_mat, proj_mat;
289                 TRACE("PROCESSVERTICES  (%d)\n", count);
290 
291                 /* Get the transform and world matrix */
292                 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
293                 wined3d_device_get_transform(device->wined3d_device,
294                         D3DTRANSFORMSTATE_VIEW, (struct wined3d_matrix *)&view_mat);
295                 wined3d_device_get_transform(device->wined3d_device,
296                         D3DTRANSFORMSTATE_PROJECTION, (struct wined3d_matrix *)&proj_mat);
297                 wined3d_device_get_transform(device->wined3d_device,
298                         WINED3D_TS_WORLD_MATRIX(0), (struct wined3d_matrix *)&world_mat);
299 
300                 for (i = 0; i < count; ++i)
301                 {
302                     D3DPROCESSVERTICES *ci = (D3DPROCESSVERTICES *)instr;
303 
304                     TRACE("  Start : %d Dest : %d Count : %d\n",
305 			  ci->wStart, ci->wDest, ci->dwCount);
306 		    TRACE("  Flags : ");
307                     if (TRACE_ON(ddraw))
308                     {
309 		        if (ci->dwFlags & D3DPROCESSVERTICES_COPY)
310 			    TRACE("COPY ");
311 			if (ci->dwFlags & D3DPROCESSVERTICES_NOCOLOR)
312 			    TRACE("NOCOLOR ");
313 			if (ci->dwFlags == D3DPROCESSVERTICES_OPMASK)
314 			    TRACE("OPMASK ");
315 			if (ci->dwFlags & D3DPROCESSVERTICES_TRANSFORM)
316 			    TRACE("TRANSFORM ");
317 			if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT)
318 			    TRACE("TRANSFORMLIGHT ");
319 			if (ci->dwFlags & D3DPROCESSVERTICES_UPDATEEXTENTS)
320 			    TRACE("UPDATEEXTENTS ");
321 			TRACE("\n");
322 		    }
323 
324 		    /* This is where doing Direct3D on top on OpenGL is quite difficult.
325 		       This method transforms a set of vertices using the CURRENT state
326 		       (lighting, projection, ...) but does not rasterize them.
327 		       They will only be put on screen later (with the POINT / LINE and
328 		       TRIANGLE op-codes). The problem is that you can have a triangle
329 		       with each point having been transformed using another state...
330 
331 		       In this implementation, I will emulate only ONE thing : each
332 		       vertex can have its own "WORLD" transformation (this is used in the
333 		       TWIST.EXE demo of the 5.2 SDK). I suppose that all vertices of the
334 		       execute buffer use the same state.
335 
336 		       If I find applications that change other states, I will try to do a
337 		       more 'fine-tuned' state emulation (but I may become quite tricky if
338 		       it changes a light position in the middle of a triangle).
339 
340 		       In this case, a 'direct' approach (i.e. without using OpenGL, but
341 		       writing our own 3D rasterizer) would be easier. */
342 
343 		    /* The current method (with the hypothesis that only the WORLD matrix
344 		       will change between two points) is like this :
345 		       - I transform 'manually' all the vertices with the current WORLD
346 		         matrix and store them in the vertex buffer
347 		       - during the rasterization phase, the WORLD matrix will be set to
348 		         the Identity matrix */
349 
350 		    /* Enough for the moment */
351 		    if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORMLIGHT) {
352 		        unsigned int nb;
353                         D3DVERTEX *src = ((D3DVERTEX *)((char *)buffer->desc.lpData + vs)) + ci->wStart;
354                         D3DTLVERTEX *dst = ((D3DTLVERTEX *)buffer->vertex_data) + ci->wDest;
355                         D3DVIEWPORT *Viewport = &viewport->viewports.vp1;
356 			D3DMATRIX mat;
357 
358                         if (TRACE_ON(ddraw))
359                         {
360 			    TRACE("  Projection Matrix : (%p)\n", &proj_mat);
361 			    dump_D3DMATRIX(&proj_mat);
362 			    TRACE("  View       Matrix : (%p)\n", &view_mat);
363 			    dump_D3DMATRIX(&view_mat);
364 			    TRACE("  World Matrix : (%p)\n", &world_mat);
365 			    dump_D3DMATRIX(&world_mat);
366 			}
367 
368                         multiply_matrix(&mat,&view_mat,&world_mat);
369                         multiply_matrix(&mat,&proj_mat,&mat);
370 
371 			for (nb = 0; nb < ci->dwCount; nb++) {
372 			    /* No lighting yet */
373 			    dst->u5.color = 0xFFFFFFFF; /* Opaque white */
374 			    dst->u6.specular = 0xFF000000; /* No specular and no fog factor */
375 
376 			    dst->u7.tu  = src->u7.tu;
377 			    dst->u8.tv  = src->u8.tv;
378 
379                             dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + mat._41;
380                             dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + mat._42;
381                             dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + mat._43;
382                             dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + mat._44;
383 
384 			    dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX
385 				       + Viewport->dwX + Viewport->dwWidth / 2;
386 			    dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
387 				       + Viewport->dwY + Viewport->dwHeight / 2;
388 			    dst->u3.sz /= dst->u4.rhw;
389 			    dst->u4.rhw = 1 / dst->u4.rhw;
390 
391 			    src++;
392 			    dst++;
393 
394 			}
395 		    } else if (ci->dwFlags == D3DPROCESSVERTICES_TRANSFORM) {
396 		        unsigned int nb;
397                         D3DLVERTEX *src = ((D3DLVERTEX *)((char *)buffer->desc.lpData + vs)) + ci->wStart;
398                         D3DTLVERTEX *dst = ((D3DTLVERTEX *)buffer->vertex_data) + ci->wDest;
399                         D3DVIEWPORT *Viewport = &viewport->viewports.vp1;
400 			D3DMATRIX mat;
401 
402                         if (TRACE_ON(ddraw))
403                         {
404 			    TRACE("  Projection Matrix : (%p)\n", &proj_mat);
405 			    dump_D3DMATRIX(&proj_mat);
406 			    TRACE("  View       Matrix : (%p)\n",&view_mat);
407 			    dump_D3DMATRIX(&view_mat);
408 			    TRACE("  World Matrix : (%p)\n", &world_mat);
409 			    dump_D3DMATRIX(&world_mat);
410 			}
411 
412 			multiply_matrix(&mat,&view_mat,&world_mat);
413 			multiply_matrix(&mat,&proj_mat,&mat);
414 
415 			for (nb = 0; nb < ci->dwCount; nb++) {
416 			    dst->u5.color = src->u4.color;
417 			    dst->u6.specular = src->u5.specular;
418 			    dst->u7.tu = src->u6.tu;
419 			    dst->u8.tv = src->u7.tv;
420 
421                             dst->u1.sx = (src->u1.x * mat._11) + (src->u2.y * mat._21) + (src->u3.z * mat._31) + mat._41;
422                             dst->u2.sy = (src->u1.x * mat._12) + (src->u2.y * mat._22) + (src->u3.z * mat._32) + mat._42;
423                             dst->u3.sz = (src->u1.x * mat._13) + (src->u2.y * mat._23) + (src->u3.z * mat._33) + mat._43;
424                             dst->u4.rhw = (src->u1.x * mat._14) + (src->u2.y * mat._24) + (src->u3.z * mat._34) + mat._44;
425 
426 			    dst->u1.sx = dst->u1.sx / dst->u4.rhw * Viewport->dvScaleX
427 				       + Viewport->dwX + Viewport->dwWidth / 2;
428 			    dst->u2.sy = (-dst->u2.sy) / dst->u4.rhw * Viewport->dvScaleY
429 				       + Viewport->dwY + Viewport->dwHeight / 2;
430 
431 			    dst->u3.sz /= dst->u4.rhw;
432 			    dst->u4.rhw = 1 / dst->u4.rhw;
433 
434 			    src++;
435 			    dst++;
436 			}
437                     }
438                     else if (ci->dwFlags == D3DPROCESSVERTICES_COPY)
439                     {
440                         D3DTLVERTEX *src = ((D3DTLVERTEX *)((char *)buffer->desc.lpData + vs)) + ci->wStart;
441                         D3DTLVERTEX *dst = ((D3DTLVERTEX *)buffer->vertex_data) + ci->wDest;
442 
443 			memcpy(dst, src, ci->dwCount * sizeof(D3DTLVERTEX));
444 		    } else {
445 		        ERR("Unhandled vertex processing flag %#x.\n", ci->dwFlags);
446 		    }
447 
448 		    instr += size;
449 		}
450 	    } break;
451 
452 	    case D3DOP_TEXTURELOAD: {
453 	        WARN("TEXTURELOAD-s    (%d)\n", count);
454 
455 		instr += count * size;
456 	    } break;
457 
458 	    case D3DOP_EXIT: {
459 	        TRACE("EXIT             (%d)\n", count);
460 		/* We did this instruction */
461 		instr += size;
462 		/* Exit this loop */
463 		goto end_of_buffer;
464 	    } break;
465 
466 	    case D3DOP_BRANCHFORWARD: {
467                 DWORD i;
468 		TRACE("BRANCHFORWARD    (%d)\n", count);
469 
470                 for (i = 0; i < count; ++i)
471                 {
472                     D3DBRANCH *ci = (D3DBRANCH *)instr;
473 
474                     if ((buffer->data.dsStatus.dwStatus & ci->dwMask) == ci->dwValue)
475                     {
476                         if (!ci->bNegate)
477                         {
478                             TRACE(" Branch to %d\n", ci->dwOffset);
479                             if (ci->dwOffset) {
480                                 instr = (char*)current + ci->dwOffset;
481                                 break;
482                             }
483 			}
484 		    } else {
485 		        if (ci->bNegate) {
486                             TRACE(" Branch to %d\n", ci->dwOffset);
487                             if (ci->dwOffset) {
488                                 instr = (char*)current + ci->dwOffset;
489                                 break;
490                             }
491 			}
492 		    }
493 
494 		    instr += size;
495 		}
496 	    } break;
497 
498 	    case D3DOP_SPAN: {
499 	        WARN("SPAN-s           (%d)\n", count);
500 
501 		instr += count * size;
502 	    } break;
503 
504 	    case D3DOP_SETSTATUS: {
505                 DWORD i;
506 		TRACE("SETSTATUS        (%d)\n", count);
507 
508                 for (i = 0; i < count; ++i)
509                 {
510                     buffer->data.dsStatus = *(D3DSTATUS *)instr;
511                     instr += size;
512                 }
513 	    } break;
514 
515 	    default:
516 	        ERR("Unhandled OpCode %d !!!\n",current->bOpcode);
517 	        /* Try to save ... */
518 	        instr += count * size;
519 	        break;
520 	}
521     }
522 
523 end_of_buffer:
524     return D3D_OK;
525 }
526 
527 static inline struct d3d_execute_buffer *impl_from_IDirect3DExecuteBuffer(IDirect3DExecuteBuffer *iface)
528 {
529     return CONTAINING_RECORD(iface, struct d3d_execute_buffer, IDirect3DExecuteBuffer_iface);
530 }
531 
532 /*****************************************************************************
533  * IDirect3DExecuteBuffer::QueryInterface
534  *
535  * Well, a usual QueryInterface function. Don't know fur sure which
536  * interfaces it can Query.
537  *
538  * Params:
539  *  riid: The interface ID queried for
540  *  obj: Address to return the interface pointer at
541  *
542  * Returns:
543  *  D3D_OK in case of a success (S_OK? Think it's the same)
544  *  OLE_E_ENUM_NOMORE if the interface wasn't found.
545  *   (E_NOINTERFACE?? Don't know what I really need)
546  *
547  *****************************************************************************/
548 static HRESULT WINAPI d3d_execute_buffer_QueryInterface(IDirect3DExecuteBuffer *iface, REFIID riid, void **obj)
549 {
550     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obj);
551 
552     *obj = NULL;
553 
554     if ( IsEqualGUID( &IID_IUnknown,  riid ) ) {
555         IDirect3DExecuteBuffer_AddRef(iface);
556 	*obj = iface;
557 	TRACE("  Creating IUnknown interface at %p.\n", *obj);
558 	return S_OK;
559     }
560     if ( IsEqualGUID( &IID_IDirect3DExecuteBuffer, riid ) ) {
561         IDirect3DExecuteBuffer_AddRef(iface);
562         *obj = iface;
563 	TRACE("  Creating IDirect3DExecuteBuffer interface %p\n", *obj);
564 	return S_OK;
565     }
566     FIXME("(%p): interface for IID %s NOT found!\n", iface, debugstr_guid(riid));
567     return E_NOINTERFACE;
568 }
569 
570 
571 /*****************************************************************************
572  * IDirect3DExecuteBuffer::AddRef
573  *
574  * A normal AddRef method, nothing special
575  *
576  * Returns:
577  *  The new refcount
578  *
579  *****************************************************************************/
580 static ULONG WINAPI d3d_execute_buffer_AddRef(IDirect3DExecuteBuffer *iface)
581 {
582     struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
583     ULONG ref = InterlockedIncrement(&buffer->ref);
584 
585     TRACE("%p increasing refcount to %u.\n", buffer, ref);
586 
587     return ref;
588 }
589 
590 /*****************************************************************************
591  * IDirect3DExecuteBuffer::Release
592  *
593  * A normal Release method, nothing special
594  *
595  * Returns:
596  *  The new refcount
597  *
598  *****************************************************************************/
599 static ULONG WINAPI d3d_execute_buffer_Release(IDirect3DExecuteBuffer *iface)
600 {
601     struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
602     ULONG ref = InterlockedDecrement(&buffer->ref);
603 
604     TRACE("%p decreasing refcount to %u.\n", buffer, ref);
605 
606     if (!ref)
607     {
608         if (buffer->need_free)
609             HeapFree(GetProcessHeap(), 0, buffer->desc.lpData);
610         HeapFree(GetProcessHeap(), 0, buffer->vertex_data);
611         HeapFree(GetProcessHeap(), 0, buffer->indices);
612         HeapFree(GetProcessHeap(), 0, buffer);
613     }
614 
615     return ref;
616 }
617 
618 /*****************************************************************************
619  * IDirect3DExecuteBuffer::Initialize
620  *
621  * Initializes the Execute Buffer. This method exists for COM compliance
622  * Nothing to do here.
623  *
624  * Returns:
625  *  D3D_OK
626  *
627  *****************************************************************************/
628 static HRESULT WINAPI d3d_execute_buffer_Initialize(IDirect3DExecuteBuffer *iface,
629         IDirect3DDevice *device, D3DEXECUTEBUFFERDESC *desc)
630 {
631     TRACE("iface %p, device %p, desc %p.\n", iface, device, desc);
632 
633     return D3D_OK;
634 }
635 
636 /*****************************************************************************
637  * IDirect3DExecuteBuffer::Lock
638  *
639  * Locks the buffer, so the app can write into it.
640  *
641  * Params:
642  *  Desc: Pointer to return the buffer description. This Description contains
643  *        a pointer to the buffer data.
644  *
645  * Returns:
646  *  This implementation always returns D3D_OK
647  *
648  *****************************************************************************/
649 static HRESULT WINAPI d3d_execute_buffer_Lock(IDirect3DExecuteBuffer *iface, D3DEXECUTEBUFFERDESC *desc)
650 {
651     struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
652     DWORD dwSize;
653 
654     TRACE("iface %p, desc %p.\n", iface, desc);
655 
656     dwSize = desc->dwSize;
657     memcpy(desc, &buffer->desc, dwSize);
658 
659     if (TRACE_ON(ddraw))
660     {
661         TRACE("  Returning description :\n");
662         _dump_D3DEXECUTEBUFFERDESC(desc);
663     }
664     return D3D_OK;
665 }
666 
667 /*****************************************************************************
668  * IDirect3DExecuteBuffer::Unlock
669  *
670  * Unlocks the buffer. We don't have anything to do here
671  *
672  * Returns:
673  *  This implementation always returns D3D_OK
674  *
675  *****************************************************************************/
676 static HRESULT WINAPI d3d_execute_buffer_Unlock(IDirect3DExecuteBuffer *iface)
677 {
678     TRACE("iface %p.\n", iface);
679 
680     return D3D_OK;
681 }
682 
683 /*****************************************************************************
684  * IDirect3DExecuteBuffer::SetExecuteData
685  *
686  * Sets the execute data. This data is used to describe the buffer's content
687  *
688  * Params:
689  *  Data: Pointer to a D3DEXECUTEDATA structure containing the data to
690  *  assign
691  *
692  * Returns:
693  *  D3D_OK on success
694  *  DDERR_OUTOFMEMORY if the vertex buffer allocation failed
695  *
696  *****************************************************************************/
697 static HRESULT WINAPI d3d_execute_buffer_SetExecuteData(IDirect3DExecuteBuffer *iface, D3DEXECUTEDATA *data)
698 {
699     struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
700     DWORD nbvert;
701 
702     TRACE("iface %p, data %p.\n", iface, data);
703 
704     memcpy(&buffer->data, data, data->dwSize);
705 
706     /* Get the number of vertices in the execute buffer */
707     nbvert = buffer->data.dwVertexCount;
708 
709     /* Prepares the transformed vertex buffer */
710     HeapFree(GetProcessHeap(), 0, buffer->vertex_data);
711     buffer->vertex_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nbvert * sizeof(D3DTLVERTEX));
712     buffer->nb_vertices = nbvert;
713 
714     if (TRACE_ON(ddraw))
715         _dump_executedata(data);
716 
717     return D3D_OK;
718 }
719 
720 /*****************************************************************************
721  * IDirect3DExecuteBuffer::GetExecuteData
722  *
723  * Returns the data in the execute buffer
724  *
725  * Params:
726  *  Data: Pointer to a D3DEXECUTEDATA structure used to return data
727  *
728  * Returns:
729  *  D3D_OK on success
730  *
731  *****************************************************************************/
732 static HRESULT WINAPI d3d_execute_buffer_GetExecuteData(IDirect3DExecuteBuffer *iface, D3DEXECUTEDATA *data)
733 {
734     struct d3d_execute_buffer *buffer = impl_from_IDirect3DExecuteBuffer(iface);
735     DWORD dwSize;
736 
737     TRACE("iface %p, data %p.\n", iface, data);
738 
739     dwSize = data->dwSize;
740     memcpy(data, &buffer->data, dwSize);
741 
742     if (TRACE_ON(ddraw))
743     {
744         TRACE("Returning data :\n");
745         _dump_executedata(data);
746     }
747 
748     return DD_OK;
749 }
750 
751 /*****************************************************************************
752  * IDirect3DExecuteBuffer::Validate
753  *
754  * DirectX 5 SDK: "The IDirect3DExecuteBuffer::Validate method is not
755  * currently implemented"
756  *
757  * Params:
758  *  ?
759  *
760  * Returns:
761  *  DDERR_UNSUPPORTED, because it's not implemented in Windows.
762  *
763  *****************************************************************************/
764 static HRESULT WINAPI d3d_execute_buffer_Validate(IDirect3DExecuteBuffer *iface,
765         DWORD *offset, LPD3DVALIDATECALLBACK callback, void *context, DWORD reserved)
766 {
767     TRACE("iface %p, offset %p, callback %p, context %p, reserved %#x.\n",
768             iface, offset, callback, context, reserved);
769 
770     WARN("Not implemented.\n");
771 
772     return DDERR_UNSUPPORTED; /* Unchecked */
773 }
774 
775 /*****************************************************************************
776  * IDirect3DExecuteBuffer::Optimize
777  *
778  * DirectX5 SDK: "The IDirect3DExecuteBuffer::Optimize method is not
779  * currently supported"
780  *
781  * Params:
782  *  Dummy: Seems to be an unused dummy ;)
783  *
784  * Returns:
785  *  DDERR_UNSUPPORTED, because it's not implemented in Windows.
786  *
787  *****************************************************************************/
788 static HRESULT WINAPI d3d_execute_buffer_Optimize(IDirect3DExecuteBuffer *iface, DWORD reserved)
789 {
790     TRACE("iface %p, reserved %#x.\n", iface, reserved);
791 
792     WARN("Not implemented.\n");
793 
794     return DDERR_UNSUPPORTED; /* Unchecked */
795 }
796 
797 static const struct IDirect3DExecuteBufferVtbl d3d_execute_buffer_vtbl =
798 {
799     d3d_execute_buffer_QueryInterface,
800     d3d_execute_buffer_AddRef,
801     d3d_execute_buffer_Release,
802     d3d_execute_buffer_Initialize,
803     d3d_execute_buffer_Lock,
804     d3d_execute_buffer_Unlock,
805     d3d_execute_buffer_SetExecuteData,
806     d3d_execute_buffer_GetExecuteData,
807     d3d_execute_buffer_Validate,
808     d3d_execute_buffer_Optimize,
809 };
810 
811 HRESULT d3d_execute_buffer_init(struct d3d_execute_buffer *execute_buffer,
812         struct d3d_device *device, D3DEXECUTEBUFFERDESC *desc)
813 {
814     execute_buffer->IDirect3DExecuteBuffer_iface.lpVtbl = &d3d_execute_buffer_vtbl;
815     execute_buffer->ref = 1;
816     execute_buffer->d3ddev = device;
817 
818     /* Initializes memory */
819     memcpy(&execute_buffer->desc, desc, desc->dwSize);
820 
821     /* No buffer given */
822     if (!(execute_buffer->desc.dwFlags & D3DDEB_LPDATA))
823         execute_buffer->desc.lpData = NULL;
824 
825     /* No buffer size given */
826     if (!(execute_buffer->desc.dwFlags & D3DDEB_BUFSIZE))
827         execute_buffer->desc.dwBufferSize = 0;
828 
829     /* Create buffer if asked */
830     if (!execute_buffer->desc.lpData && execute_buffer->desc.dwBufferSize)
831     {
832         execute_buffer->need_free = TRUE;
833         execute_buffer->desc.lpData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, execute_buffer->desc.dwBufferSize);
834         if (!execute_buffer->desc.lpData)
835         {
836             ERR("Failed to allocate execute buffer data.\n");
837             return DDERR_OUTOFMEMORY;
838         }
839     }
840 
841     execute_buffer->desc.dwFlags |= D3DDEB_LPDATA;
842 
843     return D3D_OK;
844 }
845 
846 struct d3d_execute_buffer *unsafe_impl_from_IDirect3DExecuteBuffer(IDirect3DExecuteBuffer *iface)
847 {
848     if (!iface)
849         return NULL;
850     assert(iface->lpVtbl == &d3d_execute_buffer_vtbl);
851 
852     return impl_from_IDirect3DExecuteBuffer(iface);
853 }
854