VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/drawprim.c@ 16684

Last change on this file since 16684 was 16477, checked in by vboxsync, 16 years ago

LGPL disclaimer by filemuncher

  • Property svn:eol-style set to native
File size: 59.2 KB
Line 
1/*
2 * WINED3D draw functions
3 *
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006, 2008 Henri Verbeet
9 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26/*
27 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
28 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
29 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
30 * a choice of LGPL license versions is made available with the language indicating
31 * that LGPLv2 or any later version may be used, or where a choice of which version
32 * of the LGPL is applied is otherwise unspecified.
33 */
34
35#include "config.h"
36#include "wined3d_private.h"
37
38WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
39#define GLINFO_LOCATION This->adapter->gl_info
40
41#include <stdio.h>
42#include <math.h>
43
44/* Issues the glBegin call for gl given the primitive type and count */
45static DWORD primitiveToGl(WINED3DPRIMITIVETYPE PrimitiveType,
46 DWORD NumPrimitives,
47 GLenum *primType)
48{
49 DWORD NumVertexes = NumPrimitives;
50
51 switch (PrimitiveType) {
52 case WINED3DPT_POINTLIST:
53 TRACE("POINTS\n");
54 *primType = GL_POINTS;
55 NumVertexes = NumPrimitives;
56 break;
57
58 case WINED3DPT_LINELIST:
59 TRACE("LINES\n");
60 *primType = GL_LINES;
61 NumVertexes = NumPrimitives * 2;
62 break;
63
64 case WINED3DPT_LINESTRIP:
65 TRACE("LINE_STRIP\n");
66 *primType = GL_LINE_STRIP;
67 NumVertexes = NumPrimitives + 1;
68 break;
69
70 case WINED3DPT_TRIANGLELIST:
71 TRACE("TRIANGLES\n");
72 *primType = GL_TRIANGLES;
73 NumVertexes = NumPrimitives * 3;
74 break;
75
76 case WINED3DPT_TRIANGLESTRIP:
77 TRACE("TRIANGLE_STRIP\n");
78 *primType = GL_TRIANGLE_STRIP;
79 NumVertexes = NumPrimitives + 2;
80 break;
81
82 case WINED3DPT_TRIANGLEFAN:
83 TRACE("TRIANGLE_FAN\n");
84 *primType = GL_TRIANGLE_FAN;
85 NumVertexes = NumPrimitives + 2;
86 break;
87
88 default:
89 FIXME("Unhandled primitive\n");
90 *primType = GL_POINTS;
91 break;
92 }
93 return NumVertexes;
94}
95
96static BOOL fixed_get_input(
97 BYTE usage, BYTE usage_idx,
98 unsigned int* regnum) {
99
100 *regnum = -1;
101
102 /* Those positions must have the order in the
103 * named part of the strided data */
104
105 if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 0)
106 *regnum = 0;
107 else if (usage == WINED3DDECLUSAGE_BLENDWEIGHT && usage_idx == 0)
108 *regnum = 1;
109 else if (usage == WINED3DDECLUSAGE_BLENDINDICES && usage_idx == 0)
110 *regnum = 2;
111 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 0)
112 *regnum = 3;
113 else if (usage == WINED3DDECLUSAGE_PSIZE && usage_idx == 0)
114 *regnum = 4;
115 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 0)
116 *regnum = 5;
117 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 1)
118 *regnum = 6;
119 else if (usage == WINED3DDECLUSAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
120 *regnum = 7 + usage_idx;
121 else if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 1)
122 *regnum = 7 + WINED3DDP_MAXTEXCOORD;
123 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 1)
124 *regnum = 8 + WINED3DDP_MAXTEXCOORD;
125 else if (usage == WINED3DDECLUSAGE_TANGENT && usage_idx == 0)
126 *regnum = 9 + WINED3DDP_MAXTEXCOORD;
127 else if (usage == WINED3DDECLUSAGE_BINORMAL && usage_idx == 0)
128 *regnum = 10 + WINED3DDP_MAXTEXCOORD;
129 else if (usage == WINED3DDECLUSAGE_TESSFACTOR && usage_idx == 0)
130 *regnum = 11 + WINED3DDP_MAXTEXCOORD;
131 else if (usage == WINED3DDECLUSAGE_FOG && usage_idx == 0)
132 *regnum = 12 + WINED3DDP_MAXTEXCOORD;
133 else if (usage == WINED3DDECLUSAGE_DEPTH && usage_idx == 0)
134 *regnum = 13 + WINED3DDP_MAXTEXCOORD;
135 else if (usage == WINED3DDECLUSAGE_SAMPLE && usage_idx == 0)
136 *regnum = 14 + WINED3DDP_MAXTEXCOORD;
137
138 if (*regnum == -1) {
139 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
140 debug_d3ddeclusage(usage), usage_idx);
141 return FALSE;
142 }
143 return TRUE;
144}
145
146void primitiveDeclarationConvertToStridedData(
147 IWineD3DDevice *iface,
148 BOOL useVertexShaderFunction,
149 WineDirect3DVertexStridedData *strided,
150 BOOL *fixup) {
151
152 /* We need to deal with frequency data!*/
153
154 const BYTE *data = NULL;
155 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
156 IWineD3DVertexDeclarationImpl* vertexDeclaration = (IWineD3DVertexDeclarationImpl *)This->stateBlock->vertexDecl;
157 unsigned int i;
158 const WINED3DVERTEXELEMENT *element;
159 DWORD stride;
160 DWORD numPreloadStreams = This->stateBlock->streamIsUP ? 0 : vertexDeclaration->num_streams;
161 const DWORD *streams = vertexDeclaration->streams;
162
163 /* Check for transformed vertices, disable vertex shader if present */
164 strided->u.s.position_transformed = vertexDeclaration->position_transformed;
165 if(vertexDeclaration->position_transformed) {
166 useVertexShaderFunction = FALSE;
167 }
168
169 /* Translate the declaration into strided data */
170 for (i = 0 ; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
171 GLint streamVBO = 0;
172 BOOL stride_used;
173 unsigned int idx;
174
175 element = vertexDeclaration->pDeclarationWine + i;
176 TRACE("%p Element %p (%u of %u)\n", vertexDeclaration->pDeclarationWine,
177 element, i + 1, vertexDeclaration->declarationWNumElements - 1);
178
179 if (This->stateBlock->streamSource[element->Stream] == NULL)
180 continue;
181
182 stride = This->stateBlock->streamStride[element->Stream];
183 if (This->stateBlock->streamIsUP) {
184 TRACE("Stream is up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
185 streamVBO = 0;
186 data = (BYTE *)This->stateBlock->streamSource[element->Stream];
187 } else {
188 TRACE("Stream isn't up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
189 data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[element->Stream], 0, &streamVBO);
190
191 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
192 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
193 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
194 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
195 * not, drawStridedSlow is needed, including a vertex buffer path.
196 */
197 if(This->stateBlock->loadBaseVertexIndex < 0) {
198 WARN("loadBaseVertexIndex is < 0 (%d), not using vbos\n", This->stateBlock->loadBaseVertexIndex);
199 streamVBO = 0;
200 data = ((IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[element->Stream])->resource.allocatedMemory;
201 if((UINT_PTR)data < -This->stateBlock->loadBaseVertexIndex * stride) {
202 FIXME("System memory vertex data load offset is negative!\n");
203 }
204 }
205
206 if(fixup) {
207 if( streamVBO != 0) *fixup = TRUE;
208 else if(*fixup && !useVertexShaderFunction &&
209 (element->Usage == WINED3DDECLUSAGE_COLOR ||
210 element->Usage == WINED3DDECLUSAGE_POSITIONT)) {
211 static BOOL warned = FALSE;
212 if(!warned) {
213 /* This may be bad with the fixed function pipeline */
214 FIXME("Missing vbo streams with unfixed colors or transformed position, expect problems\n");
215 warned = TRUE;
216 }
217 }
218 }
219 }
220 data += element->Offset;
221
222 TRACE("Offset %d Stream %d UsageIndex %d\n", element->Offset, element->Stream, element->UsageIndex);
223
224 if (useVertexShaderFunction)
225 stride_used = vshader_get_input(This->stateBlock->vertexShader,
226 element->Usage, element->UsageIndex, &idx);
227 else
228 stride_used = fixed_get_input(element->Usage, element->UsageIndex, &idx);
229
230 if (stride_used) {
231 TRACE("Load %s array %u [usage=%s, usage_idx=%u, "
232 "stream=%u, offset=%u, stride=%u, type=%s, VBO=%u]\n",
233 useVertexShaderFunction? "shader": "fixed function", idx,
234 debug_d3ddeclusage(element->Usage), element->UsageIndex,
235 element->Stream, element->Offset, stride, debug_d3ddecltype(element->Type), streamVBO);
236
237 if (!useVertexShaderFunction && !vertexDeclaration->ffp_valid[i]) {
238 WARN("Skipping unsupported fixed function element of type %s and usage %s\n",
239 debug_d3ddecltype(element->Type), debug_d3ddeclusage(element->Usage));
240 } else {
241 strided->u.input[idx].lpData = data;
242 strided->u.input[idx].dwType = element->Type;
243 strided->u.input[idx].dwStride = stride;
244 strided->u.input[idx].VBO = streamVBO;
245 strided->u.input[idx].streamNo = element->Stream;
246 }
247 }
248 }
249 /* Now call PreLoad on all the vertex buffers. In the very rare case
250 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
251 * The vertex buffer can now use the strided structure in the device instead of finding its
252 * own again.
253 *
254 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
255 * once in there.
256 */
257 for(i=0; i < numPreloadStreams; i++) {
258 IWineD3DVertexBuffer *vb = This->stateBlock->streamSource[streams[i]];
259 if(vb) {
260 IWineD3DVertexBuffer_PreLoad(vb);
261 }
262 }
263}
264
265static void drawStridedFast(IWineD3DDevice *iface,UINT numberOfVertices, GLenum glPrimitiveType,
266 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
267 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
268
269 if (idxSize != 0 /* This crashes sometimes!*/) {
270 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
271 idxData = idxData == (void *)-1 ? NULL : idxData;
272#if 1
273 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
274 (const char *)idxData+(idxSize * startIdx));
275 checkGLcall("glDrawElements");
276#else /* using drawRangeElements may be faster */
277
278 glDrawRangeElements(glPrimitiveType, minIndex, minIndex + numberOfVertices - 1, numberOfVertices,
279 idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
280 (const char *)idxData+(idxSize * startIdx));
281 checkGLcall("glDrawRangeElements");
282#endif
283
284 } else {
285 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, glPrimitiveType, startVertex, numberOfVertices);
286 glDrawArrays(glPrimitiveType, startVertex, numberOfVertices);
287 checkGLcall("glDrawArrays");
288 }
289
290 return;
291}
292
293/*
294 * Actually draw using the supplied information.
295 * Slower GL version which extracts info about each vertex in turn
296 */
297
298static void drawStridedSlow(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd, UINT NumVertexes,
299 GLenum glPrimType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex)
300{
301 unsigned int textureNo = 0;
302 const WORD *pIdxBufS = NULL;
303 const DWORD *pIdxBufL = NULL;
304 ULONG vx_index;
305 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
306 const UINT *streamOffset = This->stateBlock->streamOffset;
307 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
308 BOOL pixelShader = use_ps(This);
309 BOOL specular_fog = FALSE;
310 UINT texture_stages = GL_LIMITS(texture_stages);
311 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
312 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
313 DWORD tex_mask = 0;
314
315 TRACE("Using slow vertex array code\n");
316
317 /* Variable Initialization */
318 if (idxSize != 0) {
319 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
320 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
321 * idxData will be != NULL
322 */
323 if(idxData == NULL) {
324 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
325 }
326
327 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
328 else pIdxBufL = (const DWORD *) idxData;
329 } else if (idxData) {
330 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
331 return;
332 }
333
334 /* Start drawing in GL */
335 VTRACE(("glBegin(%x)\n", glPrimType));
336 glBegin(glPrimType);
337
338 if (sd->u.s.position.lpData) position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
339
340 if (sd->u.s.normal.lpData) normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
341 else glNormal3f(0, 0, 0);
342
343 if (sd->u.s.diffuse.lpData) diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
344 else glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
345 if (This->activeContext->num_untracked_materials && sd->u.s.diffuse.dwType != WINED3DDECLTYPE_D3DCOLOR)
346 FIXME("Implement diffuse color tracking from %s\n", debug_d3ddecltype(sd->u.s.diffuse.dwType));
347
348 if (sd->u.s.specular.lpData)
349 {
350 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
351
352 /* special case where the fog density is stored in the specular alpha channel */
353 if (This->stateBlock->renderState[WINED3DRS_FOGENABLE]
354 && (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
355 || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4)
356 && This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
357 {
358 if (GL_SUPPORT(EXT_FOG_COORD))
359 {
360 if (sd->u.s.specular.dwType == WINED3DDECLTYPE_D3DCOLOR) specular_fog = TRUE;
361 else FIXME("Implement fog coordinates from %s\n", debug_d3ddecltype(sd->u.s.specular.dwType));
362 }
363 else
364 {
365 static BOOL warned;
366
367 if (!warned)
368 {
369 /* TODO: Use the fog table code from old ddraw */
370 FIXME("Implement fog for transformed vertices in software\n");
371 warned = TRUE;
372 }
373 }
374 }
375 }
376 else if (GL_SUPPORT(EXT_SECONDARY_COLOR))
377 {
378 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
379 }
380
381 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
382 {
383 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
384 int texture_idx = This->texUnitMap[textureNo];
385
386 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0)
387 {
388 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
389 continue;
390 }
391
392 if (!pixelShader && !This->stateBlock->textures[textureNo]) continue;
393
394 if (texture_idx == -1) continue;
395
396 if (coordIdx > 7)
397 {
398 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
399 continue;
400 }
401 else if (coordIdx < 0)
402 {
403 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
404 continue;
405 }
406
407 if(sd->u.s.texCoords[textureNo].lpData)
408 {
409 texCoords[textureNo] =
410 sd->u.s.texCoords[textureNo].lpData + streamOffset[sd->u.s.texCoords[textureNo].streamNo];
411 tex_mask |= (1 << textureNo);
412 }
413 else
414 {
415 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
416 if (GL_SUPPORT(ARB_MULTITEXTURE))
417 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
418 else
419 glTexCoord4f(0, 0, 0, 1);
420 }
421 }
422
423 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
424 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
425 */
426
427 /* For each primitive */
428 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
429 UINT texture, tmp_tex_mask;
430 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
431 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
432 */
433
434 /* For indexed data, we need to go a few more strides in */
435 if (idxData != NULL) {
436
437 /* Indexed so work out the number of strides to skip */
438 if (idxSize == 2) {
439 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufS[startIdx+vx_index]));
440 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
441 } else {
442 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufL[startIdx+vx_index]));
443 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
444 }
445 }
446
447 tmp_tex_mask = tex_mask;
448 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
449 {
450 int coord_idx;
451 const void *ptr;
452
453 if (!(tmp_tex_mask & 1)) continue;
454
455 coord_idx = This->stateBlock->textureState[texture][WINED3DTSS_TEXCOORDINDEX];
456 ptr = texCoords[coord_idx] + (SkipnStrides * sd->u.s.texCoords[coord_idx].dwStride);
457
458 if (GL_SUPPORT(ARB_MULTITEXTURE))
459 {
460 int texture_idx = This->texUnitMap[texture];
461 multi_texcoord_funcs[sd->u.s.texCoords[coord_idx].dwType](GL_TEXTURE0_ARB + texture_idx, ptr);
462 }
463 else
464 {
465 texcoord_funcs[sd->u.s.texCoords[coord_idx].dwType](ptr);
466 }
467 }
468
469 /* Diffuse -------------------------------- */
470 if (diffuse) {
471 const void *ptrToCoords = diffuse + SkipnStrides * sd->u.s.diffuse.dwStride;
472
473 diffuse_funcs[sd->u.s.diffuse.dwType](ptrToCoords);
474 if(This->activeContext->num_untracked_materials) {
475 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
476 unsigned char i;
477 float color[4];
478
479 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
480 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
481 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
482 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
483
484 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
485 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
486 }
487 }
488 }
489
490 /* Specular ------------------------------- */
491 if (specular) {
492 const void *ptrToCoords = specular + SkipnStrides * sd->u.s.specular.dwStride;
493
494 specular_funcs[sd->u.s.specular.dwType](ptrToCoords);
495
496 if (specular_fog)
497 {
498 DWORD specularColor = *(const DWORD *)ptrToCoords;
499 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
500 }
501 }
502
503 /* Normal -------------------------------- */
504 if (normal != NULL) {
505 const void *ptrToCoords = normal + SkipnStrides * sd->u.s.normal.dwStride;
506 normal_funcs[sd->u.s.normal.dwType](ptrToCoords);
507 }
508
509 /* Position -------------------------------- */
510 if (position) {
511 const void *ptrToCoords = position + SkipnStrides * sd->u.s.position.dwStride;
512 position_funcs[sd->u.s.position.dwType](ptrToCoords);
513 }
514
515 /* For non indexed mode, step onto next parts */
516 if (idxData == NULL) {
517 ++SkipnStrides;
518 }
519 }
520
521 glEnd();
522 checkGLcall("glEnd and previous calls");
523}
524
525static inline void send_attribute(IWineD3DDeviceImpl *This, const DWORD type, const UINT index, const void *ptr) {
526 switch(type) {
527 case WINED3DDECLTYPE_FLOAT1:
528 GL_EXTCALL(glVertexAttrib1fvARB(index, (const float *)ptr));
529 break;
530 case WINED3DDECLTYPE_FLOAT2:
531 GL_EXTCALL(glVertexAttrib2fvARB(index, (const float *)ptr));
532 break;
533 case WINED3DDECLTYPE_FLOAT3:
534 GL_EXTCALL(glVertexAttrib3fvARB(index, (const float *)ptr));
535 break;
536 case WINED3DDECLTYPE_FLOAT4:
537 GL_EXTCALL(glVertexAttrib4fvARB(index, (const float *)ptr));
538 break;
539
540 case WINED3DDECLTYPE_UBYTE4:
541 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
542 break;
543 case WINED3DDECLTYPE_UBYTE4N:
544 case WINED3DDECLTYPE_D3DCOLOR:
545 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
546 break;
547
548 case WINED3DDECLTYPE_SHORT2:
549 GL_EXTCALL(glVertexAttrib4svARB(index, (const GLshort *)ptr));
550 break;
551 case WINED3DDECLTYPE_SHORT4:
552 GL_EXTCALL(glVertexAttrib4svARB(index, (const GLshort *)ptr));
553 break;
554
555 case WINED3DDECLTYPE_SHORT2N:
556 {
557 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
558 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
559 break;
560 }
561 case WINED3DDECLTYPE_USHORT2N:
562 {
563 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
564 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
565 break;
566 }
567 case WINED3DDECLTYPE_SHORT4N:
568 GL_EXTCALL(glVertexAttrib4NsvARB(index, (const GLshort *)ptr));
569 break;
570 case WINED3DDECLTYPE_USHORT4N:
571 GL_EXTCALL(glVertexAttrib4NusvARB(index, (const GLushort *)ptr));
572 break;
573
574 case WINED3DDECLTYPE_UDEC3:
575 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
576 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
577 break;
578 case WINED3DDECLTYPE_DEC3N:
579 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
580 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
581 break;
582
583 case WINED3DDECLTYPE_FLOAT16_2:
584 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
585 * byte float according to the IEEE standard
586 */
587 if (GL_SUPPORT(NV_HALF_FLOAT)) {
588 GL_EXTCALL(glVertexAttrib2hvNV(index, (const GLhalfNV *)ptr));
589 } else {
590 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
591 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
592 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
593 }
594 break;
595 case WINED3DDECLTYPE_FLOAT16_4:
596 if (GL_SUPPORT(NV_HALF_FLOAT)) {
597 GL_EXTCALL(glVertexAttrib4hvNV(index, (const GLhalfNV *)ptr));
598 } else {
599 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
600 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
601 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
602 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
603 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
604 }
605 break;
606
607 case WINED3DDECLTYPE_UNUSED:
608 default:
609 ERR("Unexpected attribute declaration: %d\n", type);
610 break;
611 }
612}
613
614static void drawStridedSlowVs(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
615 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex)
616{
617 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
618 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
619 const WORD *pIdxBufS = NULL;
620 const DWORD *pIdxBufL = NULL;
621 ULONG vx_index;
622 int i;
623 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
624 const BYTE *ptr;
625
626 if (idxSize != 0) {
627 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
628 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
629 * idxData will be != NULL
630 */
631 if(idxData == NULL) {
632 idxData = ((IWineD3DIndexBufferImpl *) stateblock->pIndexData)->resource.allocatedMemory;
633 }
634
635 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
636 else pIdxBufL = (const DWORD *) idxData;
637 } else if (idxData) {
638 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
639 return;
640 }
641
642 /* Start drawing in GL */
643 VTRACE(("glBegin(%x)\n", glPrimitiveType));
644 glBegin(glPrimitiveType);
645
646 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
647 if (idxData != NULL) {
648
649 /* Indexed so work out the number of strides to skip */
650 if (idxSize == 2) {
651 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
652 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
653 } else {
654 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
655 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
656 }
657 }
658
659 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
660 if(!sd->u.input[i].lpData) continue;
661
662 ptr = sd->u.input[i].lpData +
663 sd->u.input[i].dwStride * SkipnStrides +
664 stateblock->streamOffset[sd->u.input[i].streamNo];
665
666 send_attribute(This, sd->u.input[i].dwType, i, ptr);
667 }
668 SkipnStrides++;
669 }
670
671 glEnd();
672}
673
674static inline void drawStridedInstanced(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd,
675 UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
676 ULONG startIdx, ULONG startVertex)
677{
678 UINT numInstances = 0, i;
679 int numInstancedAttribs = 0, j;
680 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
681 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
682 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
683
684 if (idxSize == 0) {
685 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
686 * We don't support this for now
687 *
688 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
689 * But the StreamSourceFreq value has a different meaning in that situation.
690 */
691 FIXME("Non-indexed instanced drawing is not supported\n");
692 return;
693 }
694
695 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
696 idxData = idxData == (void *)-1 ? NULL : idxData;
697
698 /* First, figure out how many instances we have to draw */
699 for(i = 0; i < MAX_STREAMS; i++) {
700 /* Look at the streams and take the first one which matches */
701 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
702 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
703 if(stateblock->streamFreq[i] == 0){
704 numInstances = 1;
705 } else {
706 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
707 }
708 break; /* break, because only the first suitable value is interesting */
709 }
710 }
711
712 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
713 if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
714 instancedData[numInstancedAttribs] = i;
715 numInstancedAttribs++;
716 }
717 }
718
719 /* now draw numInstances instances :-) */
720 for(i = 0; i < numInstances; i++) {
721 /* Specify the instanced attributes using immediate mode calls */
722 for(j = 0; j < numInstancedAttribs; j++) {
723 const BYTE *ptr = sd->u.input[instancedData[j]].lpData +
724 sd->u.input[instancedData[j]].dwStride * i +
725 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
726 if(sd->u.input[instancedData[j]].VBO) {
727 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
728 ptr += (long) vb->resource.allocatedMemory;
729 }
730
731 send_attribute(This, sd->u.input[instancedData[j]].dwType, instancedData[j], ptr);
732 }
733
734 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
735 (const char *)idxData+(idxSize * startIdx));
736 checkGLcall("glDrawElements");
737 }
738}
739
740static inline void remove_vbos(IWineD3DDeviceImpl *This, WineDirect3DVertexStridedData *s) {
741 unsigned char i;
742 IWineD3DVertexBufferImpl *vb;
743
744 if(s->u.s.position.VBO) {
745 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position.streamNo];
746 s->u.s.position.VBO = 0;
747 s->u.s.position.lpData = (BYTE *) ((unsigned long) s->u.s.position.lpData + (unsigned long) vb->resource.allocatedMemory);
748 }
749 if(s->u.s.blendWeights.VBO) {
750 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendWeights.streamNo];
751 s->u.s.blendWeights.VBO = 0;
752 s->u.s.blendWeights.lpData = (BYTE *) ((unsigned long) s->u.s.blendWeights.lpData + (unsigned long) vb->resource.allocatedMemory);
753 }
754 if(s->u.s.blendMatrixIndices.VBO) {
755 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendMatrixIndices.streamNo];
756 s->u.s.blendMatrixIndices.VBO = 0;
757 s->u.s.blendMatrixIndices.lpData = (BYTE *) ((unsigned long) s->u.s.blendMatrixIndices.lpData + (unsigned long) vb->resource.allocatedMemory);
758 }
759 if(s->u.s.normal.VBO) {
760 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal.streamNo];
761 s->u.s.normal.VBO = 0;
762 s->u.s.normal.lpData = (BYTE *) ((unsigned long) s->u.s.normal.lpData + (unsigned long) vb->resource.allocatedMemory);
763 }
764 if(s->u.s.pSize.VBO) {
765 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.pSize.streamNo];
766 s->u.s.pSize.VBO = 0;
767 s->u.s.pSize.lpData = (BYTE *) ((unsigned long) s->u.s.pSize.lpData + (unsigned long) vb->resource.allocatedMemory);
768 }
769 if(s->u.s.diffuse.VBO) {
770 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.diffuse.streamNo];
771 s->u.s.diffuse.VBO = 0;
772 s->u.s.diffuse.lpData = (BYTE *) ((unsigned long) s->u.s.diffuse.lpData + (unsigned long) vb->resource.allocatedMemory);
773 }
774 if(s->u.s.specular.VBO) {
775 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.specular.streamNo];
776 s->u.s.specular.VBO = 0;
777 s->u.s.specular.lpData = (BYTE *) ((unsigned long) s->u.s.specular.lpData + (unsigned long) vb->resource.allocatedMemory);
778 }
779 for(i = 0; i < WINED3DDP_MAXTEXCOORD; i++) {
780 if(s->u.s.texCoords[i].VBO) {
781 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.texCoords[i].streamNo];
782 s->u.s.texCoords[i].VBO = 0;
783 s->u.s.texCoords[i].lpData = (BYTE *) ((unsigned long) s->u.s.texCoords[i].lpData + (unsigned long) vb->resource.allocatedMemory);
784 }
785 }
786 if(s->u.s.position2.VBO) {
787 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position2.streamNo];
788 s->u.s.position2.VBO = 0;
789 s->u.s.position2.lpData = (BYTE *) ((unsigned long) s->u.s.position2.lpData + (unsigned long) vb->resource.allocatedMemory);
790 }
791 if(s->u.s.normal2.VBO) {
792 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal2.streamNo];
793 s->u.s.normal2.VBO = 0;
794 s->u.s.normal2.lpData = (BYTE *) ((unsigned long) s->u.s.normal2.lpData + (unsigned long) vb->resource.allocatedMemory);
795 }
796 if(s->u.s.tangent.VBO) {
797 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tangent.streamNo];
798 s->u.s.tangent.VBO = 0;
799 s->u.s.tangent.lpData = (BYTE *) ((unsigned long) s->u.s.tangent.lpData + (unsigned long) vb->resource.allocatedMemory);
800 }
801 if(s->u.s.binormal.VBO) {
802 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.binormal.streamNo];
803 s->u.s.binormal.VBO = 0;
804 s->u.s.binormal.lpData = (BYTE *) ((unsigned long) s->u.s.binormal.lpData + (unsigned long) vb->resource.allocatedMemory);
805 }
806 if(s->u.s.tessFactor.VBO) {
807 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tessFactor.streamNo];
808 s->u.s.tessFactor.VBO = 0;
809 s->u.s.tessFactor.lpData = (BYTE *) ((unsigned long) s->u.s.tessFactor.lpData + (unsigned long) vb->resource.allocatedMemory);
810 }
811 if(s->u.s.fog.VBO) {
812 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.fog.streamNo];
813 s->u.s.fog.VBO = 0;
814 s->u.s.fog.lpData = (BYTE *) ((unsigned long) s->u.s.fog.lpData + (unsigned long) vb->resource.allocatedMemory);
815 }
816 if(s->u.s.depth.VBO) {
817 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.depth.streamNo];
818 s->u.s.depth.VBO = 0;
819 s->u.s.depth.lpData = (BYTE *) ((unsigned long) s->u.s.depth.lpData + (unsigned long) vb->resource.allocatedMemory);
820 }
821 if(s->u.s.sample.VBO) {
822 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.sample.streamNo];
823 s->u.s.sample.VBO = 0;
824 s->u.s.sample.lpData = (BYTE *) ((unsigned long) s->u.s.sample.lpData + (unsigned long) vb->resource.allocatedMemory);
825 }
826}
827
828/* Routine common to the draw primitive and draw indexed primitive routines */
829void drawPrimitive(IWineD3DDevice *iface,
830 int PrimitiveType,
831 long NumPrimitives,
832 /* for Indexed: */
833 long StartVertexIndex,
834 UINT numberOfVertices,
835 long StartIdx,
836 short idxSize,
837 const void *idxData,
838 int minIndex) {
839
840 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
841 IWineD3DSurfaceImpl *target;
842 unsigned int i;
843
844 if (NumPrimitives == 0) return;
845
846 /* Invalidate the back buffer memory so LockRect will read it the next time */
847 for(i = 0; i < GL_LIMITS(buffers); i++) {
848 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
849 if (target) {
850 IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
851 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
852 }
853 }
854
855 /* Signals other modules that a drawing is in progress and the stateblock finalized */
856 This->isInDraw = TRUE;
857
858 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
859
860 if (This->stencilBufferTarget) {
861 /* Note that this depends on the ActivateContext call above to set
862 * This->render_offscreen properly */
863 DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
864 surface_load_ds_location(This->stencilBufferTarget, location);
865 surface_modify_ds_location(This->stencilBufferTarget, location);
866 }
867
868 /* Ok, we will be updating the screen from here onwards so grab the lock */
869 ENTER_GL();
870 {
871 GLenum glPrimType;
872 BOOL emulation = FALSE;
873 const WineDirect3DVertexStridedData *strided = &This->strided_streams;
874 WineDirect3DVertexStridedData stridedlcl;
875 /* Ok, Work out which primitive is requested and how many vertexes that
876 will be */
877 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
878 if (numberOfVertices == 0 )
879 numberOfVertices = calculatedNumberOfindices;
880
881 if(!use_vs(This)) {
882 if(!This->strided_streams.u.s.position_transformed && This->activeContext->num_untracked_materials &&
883 This->stateBlock->renderState[WINED3DRS_LIGHTING]) {
884 static BOOL warned;
885 if (!warned) {
886 FIXME("Using software emulation because not all material properties could be tracked\n");
887 warned = TRUE;
888 } else {
889 TRACE("Using software emulation because not all material properties could be tracked\n");
890 }
891 emulation = TRUE;
892 }
893 else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
894 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
895 * to a float in the vertex buffer
896 */
897 static BOOL warned;
898 if (!warned) {
899 FIXME("Using software emulation because manual fog coordinates are provided\n");
900 warned = TRUE;
901 } else {
902 TRACE("Using software emulation because manual fog coordinates are provided\n");
903 }
904 emulation = TRUE;
905 }
906
907 if(emulation) {
908 strided = &stridedlcl;
909 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
910 remove_vbos(This, &stridedlcl);
911 }
912 }
913
914 if (This->useDrawStridedSlow || emulation) {
915 /* Immediate mode drawing */
916 if(use_vs(This)) {
917 static BOOL warned;
918 if (!warned) {
919 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
920 warned = TRUE;
921 } else {
922 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
923 }
924 drawStridedSlowVs(iface, strided, calculatedNumberOfindices, glPrimType,
925 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
926 } else {
927 drawStridedSlow(iface, strided, calculatedNumberOfindices,
928 glPrimType, idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
929 }
930 } else if(This->instancedDraw) {
931 /* Instancing emulation with mixing immediate mode and arrays */
932 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
933 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
934 } else {
935 drawStridedFast(iface, calculatedNumberOfindices, glPrimType,
936 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
937 }
938 }
939
940 /* Finished updating the screen, restore lock */
941 LEAVE_GL();
942 TRACE("Done all gl drawing\n");
943
944 /* Diagnostics */
945#ifdef SHOW_FRAME_MAKEUP
946 {
947 static long int primCounter = 0;
948 /* NOTE: set primCounter to the value reported by drawprim
949 before you want to to write frame makeup to /tmp */
950 if (primCounter >= 0) {
951 WINED3DLOCKED_RECT r;
952 char buffer[80];
953 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
954 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
955 TRACE("Saving screenshot %s\n", buffer);
956 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
957 IWineD3DSurface_UnlockRect(This->render_targets[0]);
958
959#ifdef SHOW_TEXTURE_MAKEUP
960 {
961 IWineD3DSurface *pSur;
962 int textureNo;
963 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
964 if (This->stateBlock->textures[textureNo] != NULL) {
965 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
966 TRACE("Saving texture %s\n", buffer);
967 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
968 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
969 IWineD3DSurface_SaveSnapshot(pSur, buffer);
970 IWineD3DSurface_Release(pSur);
971 } else {
972 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
973 }
974 }
975 }
976 }
977#endif
978 }
979 TRACE("drawprim #%ld\n", primCounter);
980 ++primCounter;
981 }
982#endif
983
984 /* Control goes back to the device, stateblock values may change again */
985 This->isInDraw = FALSE;
986}
987
988static void normalize_normal(float *n) {
989 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
990 if(length == 0.0) return;
991 length = sqrt(length);
992 n[0] = n[0] / length;
993 n[1] = n[1] / length;
994 n[2] = n[2] / length;
995}
996
997/* Tesselates a high order rectangular patch into single triangles using gl evaluators
998 *
999 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
1000 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
1001 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
1002 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
1003 * in drawprim.
1004 *
1005 * To read back, the opengl feedback mode is used. This creates a problem because we want
1006 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
1007 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
1008 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
1009 * them to [-1.0;+1.0] and set the viewport up to scale them back.
1010 *
1011 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
1012 * resulting colors back to the normals.
1013 *
1014 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
1015 * does not restore it because normally a draw follows immediately afterwards. The caller is
1016 * responsible of taking care that either the gl states are restored, or the context activated
1017 * for drawing to reset the lastWasBlit flag.
1018 */
1019HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
1020 struct WineD3DRectPatch *patch) {
1021 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
1022 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
1023 WineDirect3DVertexStridedData strided;
1024 const BYTE *data;
1025 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
1026 DWORD vtxStride;
1027 GLenum feedback_type;
1028 GLfloat *feedbuffer;
1029
1030 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1031 * Beware of vbos
1032 */
1033 memset(&strided, 0, sizeof(strided));
1034 primitiveDeclarationConvertToStridedData((IWineD3DDevice *) This, FALSE, &strided, NULL);
1035 if(strided.u.s.position.VBO) {
1036 IWineD3DVertexBufferImpl *vb;
1037 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[strided.u.s.position.streamNo];
1038 strided.u.s.position.lpData = (BYTE *) ((unsigned long) strided.u.s.position.lpData +
1039 (unsigned long) vb->resource.allocatedMemory);
1040 }
1041 vtxStride = strided.u.s.position.dwStride;
1042 data = strided.u.s.position.lpData +
1043 vtxStride * info->Stride * info->StartVertexOffsetHeight +
1044 vtxStride * info->StartVertexOffsetWidth;
1045
1046 /* Not entirely sure about what happens with transformed vertices */
1047 if(strided.u.s.position_transformed) {
1048 FIXME("Transformed position in rectpatch generation\n");
1049 }
1050 if(vtxStride % sizeof(GLfloat)) {
1051 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1052 * I don't see how the stride could not be a multiple of 4, but make sure
1053 * to check it
1054 */
1055 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1056 }
1057 if(info->Basis != WINED3DBASIS_BEZIER) {
1058 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
1059 }
1060 if(info->Degree != WINED3DDEGREE_CUBIC) {
1061 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
1062 }
1063
1064 /* First, get the boundary cube of the input data */
1065 for(j = 0; j < info->Height; j++) {
1066 for(i = 0; i < info->Width; i++) {
1067 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
1068 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
1069 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
1070 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
1071 if(v[2] < neg_z) neg_z = v[2];
1072 }
1073 }
1074
1075 /* This needs some improvements in the vertex decl code */
1076 FIXME("Cannot find data to generate. Only generating position and normals\n");
1077 patch->has_normals = TRUE;
1078 patch->has_texcoords = FALSE;
1079
1080 /* Simply activate the context for blitting. This disables all the things we don't want and
1081 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
1082 * patch (as opposed to normal draws) will most likely need different changes anyway
1083 */
1084 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
1085 ENTER_GL();
1086
1087 glMatrixMode(GL_PROJECTION);
1088 checkGLcall("glMatrixMode(GL_PROJECTION)");
1089 glLoadIdentity();
1090 checkGLcall("glLoadIndentity()");
1091 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
1092 glTranslatef(0, 0, 0.5);
1093 checkGLcall("glScalef");
1094 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
1095 checkGLcall("glViewport");
1096
1097 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1098 * our feedback buffer parser
1099 */
1100 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1101 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1102 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
1103 if(patch->has_normals) {
1104 const GLfloat black[4] = {0, 0, 0, 0};
1105 const GLfloat red[4] = {1, 0, 0, 0};
1106 const GLfloat green[4] = {0, 1, 0, 0};
1107 const GLfloat blue[4] = {0, 0, 1, 0};
1108 const GLfloat white[4] = {1, 1, 1, 1};
1109 glEnable(GL_LIGHTING);
1110 checkGLcall("glEnable(GL_LIGHTING)");
1111 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
1112 checkGLcall("glLightModel for MODEL_AMBIENT");
1113 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
1114
1115 for(i = 3; i < GL_LIMITS(lights); i++) {
1116 glDisable(GL_LIGHT0 + i);
1117 checkGLcall("glDisable(GL_LIGHT0 + i)");
1118 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
1119 }
1120
1121 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
1122 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
1123 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
1124 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
1125 glLightfv(GL_LIGHT0, GL_POSITION, red);
1126 glEnable(GL_LIGHT0);
1127 checkGLcall("Setting up light 1\n");
1128 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
1129 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
1130 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
1131 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
1132 glLightfv(GL_LIGHT1, GL_POSITION, green);
1133 glEnable(GL_LIGHT1);
1134 checkGLcall("Setting up light 2\n");
1135 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
1136 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
1137 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
1138 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
1139 glLightfv(GL_LIGHT2, GL_POSITION, blue);
1140 glEnable(GL_LIGHT2);
1141 checkGLcall("Setting up light 3\n");
1142
1143 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
1144 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
1145 glDisable(GL_COLOR_MATERIAL);
1146 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
1147 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
1148 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
1149 checkGLcall("Setting up materials\n");
1150 }
1151
1152 /* Enable the needed maps.
1153 * GL_MAP2_VERTEX_3 is needed for positional data.
1154 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1155 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1156 */
1157 num_quads = ceil(patch->numSegs[0]) * ceil(patch->numSegs[1]);
1158 out_vertex_size = 3 /* position */;
1159 d3d_out_vertex_size = 3;
1160 glEnable(GL_MAP2_VERTEX_3);
1161 if(patch->has_normals && patch->has_texcoords) {
1162 FIXME("Texcoords not handled yet\n");
1163 feedback_type = GL_3D_COLOR_TEXTURE;
1164 out_vertex_size += 8;
1165 d3d_out_vertex_size += 7;
1166 glEnable(GL_AUTO_NORMAL);
1167 glEnable(GL_MAP2_TEXTURE_COORD_4);
1168 } else if(patch->has_texcoords) {
1169 FIXME("Texcoords not handled yet\n");
1170 feedback_type = GL_3D_COLOR_TEXTURE;
1171 out_vertex_size += 7;
1172 d3d_out_vertex_size += 4;
1173 glEnable(GL_MAP2_TEXTURE_COORD_4);
1174 } else if(patch->has_normals) {
1175 feedback_type = GL_3D_COLOR;
1176 out_vertex_size += 4;
1177 d3d_out_vertex_size += 3;
1178 glEnable(GL_AUTO_NORMAL);
1179 } else {
1180 feedback_type = GL_3D;
1181 }
1182 checkGLcall("glEnable vertex attrib generation");
1183
1184 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
1185 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
1186 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
1187
1188 glMap2f(GL_MAP2_VERTEX_3,
1189 0, 1, vtxStride / sizeof(float), info->Width,
1190 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1191 (const GLfloat *)data);
1192 checkGLcall("glMap2f");
1193 if(patch->has_texcoords) {
1194 glMap2f(GL_MAP2_TEXTURE_COORD_4,
1195 0, 1, vtxStride / sizeof(float), info->Width,
1196 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1197 (const GLfloat *)data);
1198 checkGLcall("glMap2f");
1199 }
1200 glMapGrid2f(ceil(patch->numSegs[0]), 0.0, 1.0, ceil(patch->numSegs[1]), 0.0, 1.0);
1201 checkGLcall("glMapGrid2f");
1202
1203 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
1204 checkGLcall("glFeedbackBuffer");
1205 glRenderMode(GL_FEEDBACK);
1206
1207 glEvalMesh2(GL_FILL, 0, ceil(patch->numSegs[0]), 0, ceil(patch->numSegs[1]));
1208 checkGLcall("glEvalMesh2\n");
1209
1210 i = glRenderMode(GL_RENDER);
1211 if(i == -1) {
1212 LEAVE_GL();
1213 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
1214 Sleep(10000);
1215 HeapFree(GetProcessHeap(), 0, feedbuffer);
1216 return WINED3DERR_DRIVERINTERNALERROR;
1217 } else if(i != buffer_size) {
1218 LEAVE_GL();
1219 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1220 Sleep(10000);
1221 HeapFree(GetProcessHeap(), 0, feedbuffer);
1222 return WINED3DERR_DRIVERINTERNALERROR;
1223 } else {
1224 TRACE("Got %d elements as expected\n", i);
1225 }
1226
1227 HeapFree(GetProcessHeap(), 0, patch->mem);
1228 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1229 i = 0;
1230 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1231 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1232 ERR("Unexpected token: %f\n", feedbuffer[j]);
1233 continue;
1234 }
1235 if(feedbuffer[j + 1] != 3) {
1236 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1237 continue;
1238 }
1239 /* Somehow there are different ideas about back / front facing, so fix up the
1240 * vertex order
1241 */
1242 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1243 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1244 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
1245 if(patch->has_normals) {
1246 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1247 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1248 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1249 }
1250 i += d3d_out_vertex_size;
1251
1252 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1253 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1254 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
1255 if(patch->has_normals) {
1256 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1257 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1258 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1259 }
1260 i += d3d_out_vertex_size;
1261
1262 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1263 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1264 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
1265 if(patch->has_normals) {
1266 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1267 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1268 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1269 }
1270 i += d3d_out_vertex_size;
1271 }
1272
1273 if(patch->has_normals) {
1274 /* Now do the same with reverse light directions */
1275 const GLfloat x[4] = {-1, 0, 0, 0};
1276 const GLfloat y[4] = { 0, -1, 0, 0};
1277 const GLfloat z[4] = { 0, 0, -1, 0};
1278 glLightfv(GL_LIGHT0, GL_POSITION, x);
1279 glLightfv(GL_LIGHT1, GL_POSITION, y);
1280 glLightfv(GL_LIGHT2, GL_POSITION, z);
1281 checkGLcall("Setting up reverse light directions\n");
1282
1283 glRenderMode(GL_FEEDBACK);
1284 checkGLcall("glRenderMode(GL_FEEDBACK)");
1285 glEvalMesh2(GL_FILL, 0, ceil(patch->numSegs[0]), 0, ceil(patch->numSegs[1]));
1286 checkGLcall("glEvalMesh2\n");
1287 i = glRenderMode(GL_RENDER);
1288 checkGLcall("glRenderMode(GL_RENDER)");
1289
1290 i = 0;
1291 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1292 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1293 ERR("Unexpected token: %f\n", feedbuffer[j]);
1294 continue;
1295 }
1296 if(feedbuffer[j + 1] != 3) {
1297 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1298 continue;
1299 }
1300 if(patch->mem[i + 3] == 0.0)
1301 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1302 if(patch->mem[i + 4] == 0.0)
1303 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1304 if(patch->mem[i + 5] == 0.0)
1305 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1306 normalize_normal(patch->mem + i + 3);
1307 i += d3d_out_vertex_size;
1308
1309 if(patch->mem[i + 3] == 0.0)
1310 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1311 if(patch->mem[i + 4] == 0.0)
1312 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1313 if(patch->mem[i + 5] == 0.0)
1314 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1315 normalize_normal(patch->mem + i + 3);
1316 i += d3d_out_vertex_size;
1317
1318 if(patch->mem[i + 3] == 0.0)
1319 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1320 if(patch->mem[i + 4] == 0.0)
1321 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1322 if(patch->mem[i + 5] == 0.0)
1323 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1324 normalize_normal(patch->mem + i + 3);
1325 i += d3d_out_vertex_size;
1326 }
1327 }
1328
1329 glDisable(GL_MAP2_VERTEX_3);
1330 glDisable(GL_AUTO_NORMAL);
1331 glDisable(GL_MAP2_NORMAL);
1332 glDisable(GL_MAP2_TEXTURE_COORD_4);
1333 checkGLcall("glDisable vertex attrib generation");
1334 LEAVE_GL();
1335
1336 HeapFree(GetProcessHeap(), 0, feedbuffer);
1337
1338 vtxStride = 3 * sizeof(float);
1339 if(patch->has_normals) {
1340 vtxStride += 3 * sizeof(float);
1341 }
1342 if(patch->has_texcoords) {
1343 vtxStride += 4 * sizeof(float);
1344 }
1345 memset(&patch->strided, 0, sizeof(&patch->strided));
1346 patch->strided.u.s.position.lpData = (BYTE *) patch->mem;
1347 patch->strided.u.s.position.dwStride = vtxStride;
1348 patch->strided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
1349 patch->strided.u.s.position.streamNo = 255;
1350
1351 if(patch->has_normals) {
1352 patch->strided.u.s.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1353 patch->strided.u.s.normal.dwStride = vtxStride;
1354 patch->strided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
1355 patch->strided.u.s.normal.streamNo = 255;
1356 }
1357 if(patch->has_texcoords) {
1358 patch->strided.u.s.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1359 if(patch->has_normals) {
1360 patch->strided.u.s.texCoords[0].lpData += 3 * sizeof(float);
1361 }
1362 patch->strided.u.s.texCoords[0].dwStride = vtxStride;
1363 patch->strided.u.s.texCoords[0].dwType = WINED3DDECLTYPE_FLOAT4;
1364 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1365 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1366 * application.
1367 */
1368 patch->strided.u.s.texCoords[0].streamNo = MAX_STREAMS;
1369 }
1370
1371 return WINED3D_OK;
1372}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette