push 4e98c31ec75caed2ea3040ac2e710b58ba1ca0e1
[wine/hacks.git] / dlls / wined3d / drawprim.c
blobc4154a76f2d67015a05e63ed102c7a67fdac842f
1 /*
2 * WINED3D draw functions
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
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.
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.
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
26 #include "config.h"
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
30 #define GLINFO_LOCATION This->adapter->gl_info
32 #include <stdio.h>
33 #include <math.h>
35 /* Issues the glBegin call for gl given the primitive type and count */
36 static DWORD primitiveToGl(WINED3DPRIMITIVETYPE PrimitiveType,
37 DWORD NumPrimitives,
38 GLenum *primType)
40 DWORD NumVertexes = NumPrimitives;
42 switch (PrimitiveType) {
43 case WINED3DPT_POINTLIST:
44 TRACE("POINTS\n");
45 *primType = GL_POINTS;
46 NumVertexes = NumPrimitives;
47 break;
49 case WINED3DPT_LINELIST:
50 TRACE("LINES\n");
51 *primType = GL_LINES;
52 NumVertexes = NumPrimitives * 2;
53 break;
55 case WINED3DPT_LINESTRIP:
56 TRACE("LINE_STRIP\n");
57 *primType = GL_LINE_STRIP;
58 NumVertexes = NumPrimitives + 1;
59 break;
61 case WINED3DPT_TRIANGLELIST:
62 TRACE("TRIANGLES\n");
63 *primType = GL_TRIANGLES;
64 NumVertexes = NumPrimitives * 3;
65 break;
67 case WINED3DPT_TRIANGLESTRIP:
68 TRACE("TRIANGLE_STRIP\n");
69 *primType = GL_TRIANGLE_STRIP;
70 NumVertexes = NumPrimitives + 2;
71 break;
73 case WINED3DPT_TRIANGLEFAN:
74 TRACE("TRIANGLE_FAN\n");
75 *primType = GL_TRIANGLE_FAN;
76 NumVertexes = NumPrimitives + 2;
77 break;
79 default:
80 FIXME("Unhandled primitive\n");
81 *primType = GL_POINTS;
82 break;
84 return NumVertexes;
87 static BOOL fixed_get_input(
88 BYTE usage, BYTE usage_idx,
89 unsigned int* regnum) {
91 *regnum = -1;
93 /* Those positions must have the order in the
94 * named part of the strided data */
96 if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 0)
97 *regnum = 0;
98 else if (usage == WINED3DDECLUSAGE_BLENDWEIGHT && usage_idx == 0)
99 *regnum = 1;
100 else if (usage == WINED3DDECLUSAGE_BLENDINDICES && usage_idx == 0)
101 *regnum = 2;
102 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 0)
103 *regnum = 3;
104 else if (usage == WINED3DDECLUSAGE_PSIZE && usage_idx == 0)
105 *regnum = 4;
106 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 0)
107 *regnum = 5;
108 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 1)
109 *regnum = 6;
110 else if (usage == WINED3DDECLUSAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
111 *regnum = 7 + usage_idx;
112 else if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 1)
113 *regnum = 7 + WINED3DDP_MAXTEXCOORD;
114 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 1)
115 *regnum = 8 + WINED3DDP_MAXTEXCOORD;
116 else if (usage == WINED3DDECLUSAGE_TANGENT && usage_idx == 0)
117 *regnum = 9 + WINED3DDP_MAXTEXCOORD;
118 else if (usage == WINED3DDECLUSAGE_BINORMAL && usage_idx == 0)
119 *regnum = 10 + WINED3DDP_MAXTEXCOORD;
120 else if (usage == WINED3DDECLUSAGE_TESSFACTOR && usage_idx == 0)
121 *regnum = 11 + WINED3DDP_MAXTEXCOORD;
122 else if (usage == WINED3DDECLUSAGE_FOG && usage_idx == 0)
123 *regnum = 12 + WINED3DDP_MAXTEXCOORD;
124 else if (usage == WINED3DDECLUSAGE_DEPTH && usage_idx == 0)
125 *regnum = 13 + WINED3DDP_MAXTEXCOORD;
126 else if (usage == WINED3DDECLUSAGE_SAMPLE && usage_idx == 0)
127 *regnum = 14 + WINED3DDP_MAXTEXCOORD;
129 if (*regnum == -1) {
130 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
131 debug_d3ddeclusage(usage), usage_idx);
132 return FALSE;
134 return TRUE;
137 void primitiveDeclarationConvertToStridedData(
138 IWineD3DDevice *iface,
139 BOOL useVertexShaderFunction,
140 WineDirect3DVertexStridedData *strided,
141 BOOL *fixup) {
143 /* We need to deal with frequency data!*/
145 BYTE *data = NULL;
146 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
147 IWineD3DVertexDeclarationImpl* vertexDeclaration = (IWineD3DVertexDeclarationImpl *)This->stateBlock->vertexDecl;
148 int i;
149 WINED3DVERTEXELEMENT *element;
150 DWORD stride;
151 DWORD numPreloadStreams = This->stateBlock->streamIsUP ? 0 : vertexDeclaration->num_streams;
152 DWORD *streams = vertexDeclaration->streams;
154 /* Check for transformed vertices, disable vertex shader if present */
155 strided->u.s.position_transformed = vertexDeclaration->position_transformed;
156 if(vertexDeclaration->position_transformed) {
157 useVertexShaderFunction = FALSE;
160 /* Translate the declaration into strided data */
161 for (i = 0 ; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
162 GLint streamVBO = 0;
163 BOOL stride_used;
164 unsigned int idx;
166 element = vertexDeclaration->pDeclarationWine + i;
167 TRACE("%p Element %p (%d of %d)\n", vertexDeclaration->pDeclarationWine,
168 element, i + 1, vertexDeclaration->declarationWNumElements - 1);
170 if (This->stateBlock->streamSource[element->Stream] == NULL)
171 continue;
173 stride = This->stateBlock->streamStride[element->Stream];
174 if (This->stateBlock->streamIsUP) {
175 TRACE("Stream is up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
176 streamVBO = 0;
177 data = (BYTE *)This->stateBlock->streamSource[element->Stream];
178 } else {
179 TRACE("Stream isn't up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
180 data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[element->Stream], 0, &streamVBO);
182 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
183 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
184 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
185 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
186 * not, drawStridedSlow is needed, including a vertex buffer path.
188 if(This->stateBlock->loadBaseVertexIndex < 0) {
189 WARN("loadBaseVertexIndex is < 0 (%d), not using vbos\n", This->stateBlock->loadBaseVertexIndex);
190 streamVBO = 0;
191 data = ((IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[element->Stream])->resource.allocatedMemory;
192 if((UINT_PTR)data < -This->stateBlock->loadBaseVertexIndex * stride) {
193 FIXME("System memory vertex data load offset is negative!\n");
197 if(fixup) {
198 if( streamVBO != 0) *fixup = TRUE;
199 else if(*fixup && !useVertexShaderFunction &&
200 (element->Usage == WINED3DDECLUSAGE_COLOR ||
201 element->Usage == WINED3DDECLUSAGE_POSITIONT)) {
202 static BOOL warned = FALSE;
203 if(!warned) {
204 /* This may be bad with the fixed function pipeline */
205 FIXME("Missing vbo streams with unfixed colors or transformed position, expect problems\n");
206 warned = TRUE;
211 data += element->Offset;
213 TRACE("Offset %d Stream %d UsageIndex %d\n", element->Offset, element->Stream, element->UsageIndex);
215 if (useVertexShaderFunction)
216 stride_used = vshader_get_input(This->stateBlock->vertexShader,
217 element->Usage, element->UsageIndex, &idx);
218 else
219 stride_used = fixed_get_input(element->Usage, element->UsageIndex, &idx);
221 if (stride_used) {
222 TRACE("Load %s array %u [usage=%s, usage_idx=%u, "
223 "stream=%u, offset=%u, stride=%u, type=%s, VBO=%u]\n",
224 useVertexShaderFunction? "shader": "fixed function", idx,
225 debug_d3ddeclusage(element->Usage), element->UsageIndex,
226 element->Stream, element->Offset, stride, debug_d3ddecltype(element->Type), streamVBO);
228 if (!useVertexShaderFunction && !vertexDeclaration->ffp_valid[i]) {
229 WARN("Skipping unsupported fixed function element of type %s and usage %s\n",
230 debug_d3ddecltype(element->Type), debug_d3ddeclusage(element->Usage));
231 } else {
232 strided->u.input[idx].lpData = data;
233 strided->u.input[idx].dwType = element->Type;
234 strided->u.input[idx].dwStride = stride;
235 strided->u.input[idx].VBO = streamVBO;
236 strided->u.input[idx].streamNo = element->Stream;
240 /* Now call PreLoad on all the vertex buffers. In the very rare case
241 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
242 * The vertex buffer can now use the strided structure in the device instead of finding its
243 * own again.
245 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
246 * once in there.
248 for(i=0; i < numPreloadStreams; i++) {
249 IWineD3DVertexBuffer *vb = This->stateBlock->streamSource[streams[i]];
250 if(vb) {
251 IWineD3DVertexBuffer_PreLoad(vb);
256 static void drawStridedFast(IWineD3DDevice *iface,UINT numberOfVertices, GLenum glPrimitiveType,
257 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
258 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
260 if (idxSize != 0 /* This crashes sometimes!*/) {
261 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
262 idxData = idxData == (void *)-1 ? NULL : idxData;
263 #if 1
264 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
265 (const char *)idxData+(idxSize * startIdx));
266 checkGLcall("glDrawElements");
267 #else /* using drawRangeElements may be faster */
269 glDrawRangeElements(glPrimitiveType, minIndex, minIndex + numberOfVertices - 1, numberOfVertices,
270 idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
271 (const char *)idxData+(idxSize * startIdx));
272 checkGLcall("glDrawRangeElements");
273 #endif
275 } else {
276 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, glPrimitiveType, startVertex, numberOfVertices);
277 glDrawArrays(glPrimitiveType, startVertex, numberOfVertices);
278 checkGLcall("glDrawArrays");
281 return;
285 * Actually draw using the supplied information.
286 * Slower GL version which extracts info about each vertex in turn
289 static void drawStridedSlow(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd,
290 UINT NumVertexes, GLenum glPrimType,
291 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
293 unsigned int textureNo = 0;
294 const WORD *pIdxBufS = NULL;
295 const DWORD *pIdxBufL = NULL;
296 LONG vx_index;
297 DWORD diffuseColor = 0xFFFFFFFF; /* Diffuse Color */
298 DWORD specularColor = 0; /* Specular Color */
299 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
300 UINT *streamOffset = This->stateBlock->streamOffset;
301 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
302 BOOL pixelShader = use_ps(This);
304 BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
305 BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
307 TRACE("Using slow vertex array code\n");
309 /* Variable Initialization */
310 if (idxSize != 0) {
311 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
312 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
313 * idxData will be != NULL
315 if(idxData == NULL) {
316 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
319 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
320 else pIdxBufL = (const DWORD *) idxData;
323 /* Adding the stream offset once is cheaper than doing it every iteration. Do not modify the strided data, it is a pointer
324 * to the strided Data in the device and might be needed intact on the next draw
326 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
327 if(sd->u.s.texCoords[textureNo].lpData) {
328 texCoords[textureNo] = sd->u.s.texCoords[textureNo].lpData + streamOffset[sd->u.s.texCoords[textureNo].streamNo];
329 } else {
330 texCoords[textureNo] = NULL;
333 if(sd->u.s.diffuse.lpData) {
334 diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
336 if(sd->u.s.specular.lpData) {
337 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
339 if(sd->u.s.normal.lpData) {
340 normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
342 if(sd->u.s.position.lpData) {
343 position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
346 /* The texture coordinate types are not so easy to map into a common function signature - we're
347 * not using the vector functions here
349 if(FIXME_ON(d3d_draw)) {
350 for (textureNo = 0; textureNo < GL_LIMITS(textures); ++textureNo) {
351 DWORD type = sd->u.s.texCoords[textureNo].dwType;
352 if (sd->u.s.texCoords[textureNo].lpData &&
353 type != WINED3DDECLTYPE_FLOAT1 &&
354 type != WINED3DDECLTYPE_FLOAT2 &&
355 type != WINED3DDECLTYPE_FLOAT3 &&
356 type != WINED3DDECLTYPE_FLOAT4) {
357 FIXME("Implement fixed function texture coordinates from %s\n", debug_d3ddecltype(type));
360 if(specular && This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
361 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position_transformed )&&
362 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
363 if(GL_SUPPORT(EXT_FOG_COORD) && sd->u.s.specular.dwType != WINED3DDECLTYPE_D3DCOLOR) {
364 FIXME("Implement fog coordinates from %s\n", debug_d3ddecltype(sd->u.s.specular.dwType));
367 if(This->activeContext->num_untracked_materials && sd->u.s.diffuse.dwType != WINED3DDECLTYPE_D3DCOLOR) {
368 FIXME("Implement diffuse color tracking from %s\n", debug_d3ddecltype(sd->u.s.diffuse.dwType));
372 /* Start drawing in GL */
373 VTRACE(("glBegin(%x)\n", glPrimType));
374 glBegin(glPrimType);
376 /* Default settings for data that is not passed */
377 if (sd->u.s.normal.lpData == NULL) {
378 glNormal3f(0, 0, 0);
380 if(sd->u.s.diffuse.lpData == NULL) {
381 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
383 if(sd->u.s.specular.lpData == NULL) {
384 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
385 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
389 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
390 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
393 /* For each primitive */
394 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
396 /* Initialize diffuse color */
397 diffuseColor = 0xFFFFFFFF;
399 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
400 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
403 /* For indexed data, we need to go a few more strides in */
404 if (idxData != NULL) {
406 /* Indexed so work out the number of strides to skip */
407 if (idxSize == 2) {
408 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
409 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
410 } else {
411 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
412 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
416 /* Texture coords --------------------------- */
417 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
419 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0) {
420 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
421 continue ;
424 /* Query tex coords */
425 if (This->stateBlock->textures[textureNo] != NULL || pixelShader) {
427 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
428 int texture_idx = This->texUnitMap[textureNo];
429 float *ptrToCoords = NULL;
430 float s = 0.0, t = 0.0, r = 0.0, q = 0.0;
432 if (coordIdx > 7) {
433 VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo));
434 continue;
435 } else if (coordIdx < 0) {
436 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
437 continue;
440 ptrToCoords = (float *)(texCoords[coordIdx] + (SkipnStrides * sd->u.s.texCoords[coordIdx].dwStride));
441 if (texCoords[coordIdx] == NULL) {
442 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
443 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
444 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
445 } else {
446 glTexCoord4f(0, 0, 0, 1);
448 continue;
449 } else {
450 int coordsToUse = sd->u.s.texCoords[coordIdx].dwType + 1; /* 0 == WINED3DDECLTYPE_FLOAT1 etc */
452 if (texture_idx == -1) continue;
454 /* The coords to supply depend completely on the fvf / vertex shader */
455 switch (coordsToUse) {
456 case 4: q = ptrToCoords[3]; /* drop through */
457 case 3: r = ptrToCoords[2]; /* drop through */
458 case 2: t = ptrToCoords[1]; /* drop through */
459 case 1: s = ptrToCoords[0];
462 switch (coordsToUse) { /* Supply the provided texture coords */
463 case WINED3DTTFF_COUNT1:
464 VTRACE(("tex:%d, s=%f\n", textureNo, s));
465 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
466 GL_EXTCALL(glMultiTexCoord1fARB(GL_TEXTURE0_ARB + texture_idx, s));
467 } else {
468 glTexCoord1f(s);
470 break;
471 case WINED3DTTFF_COUNT2:
472 VTRACE(("tex:%d, s=%f, t=%f\n", textureNo, s, t));
473 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
474 GL_EXTCALL(glMultiTexCoord2fARB(GL_TEXTURE0_ARB + texture_idx, s, t));
475 } else {
476 glTexCoord2f(s, t);
478 break;
479 case WINED3DTTFF_COUNT3:
480 VTRACE(("tex:%d, s=%f, t=%f, r=%f\n", textureNo, s, t, r));
481 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
482 GL_EXTCALL(glMultiTexCoord3fARB(GL_TEXTURE0_ARB + texture_idx, s, t, r));
483 } else {
484 glTexCoord3f(s, t, r);
486 break;
487 case WINED3DTTFF_COUNT4:
488 VTRACE(("tex:%d, s=%f, t=%f, r=%f, q=%f\n", textureNo, s, t, r, q));
489 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
490 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, s, t, r, q));
491 } else {
492 glTexCoord4f(s, t, r, q);
494 break;
495 default:
496 FIXME("Should not get here as coordsToUse is two bits only (%x)!\n", coordsToUse);
500 } /* End of textures */
502 /* Diffuse -------------------------------- */
503 if (diffuse) {
504 DWORD *ptrToCoords = (DWORD *)(diffuse + (SkipnStrides * sd->u.s.diffuse.dwStride));
506 diffuse_funcs[sd->u.s.diffuse.dwType]((void *) ptrToCoords);
507 if(This->activeContext->num_untracked_materials) {
508 unsigned char i;
509 float color[4];
511 diffuseColor = ptrToCoords[0];
512 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
513 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
514 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
515 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
517 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
518 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
523 /* Specular ------------------------------- */
524 if (specular) {
525 DWORD *ptrToCoords = (DWORD *)(specular + (SkipnStrides * sd->u.s.specular.dwStride));
527 /* special case where the fog density is stored in the specular alpha channel */
528 if(This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
529 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4 )&&
530 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
531 if(GL_SUPPORT(EXT_FOG_COORD)) {
532 specularColor = ptrToCoords[0];
533 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
534 } else {
535 static BOOL warned = FALSE;
536 if(!warned) {
537 /* TODO: Use the fog table code from old ddraw */
538 FIXME("Implement fog for transformed vertices in software\n");
539 warned = TRUE;
544 specular_funcs[sd->u.s.specular.dwType]((void *) ptrToCoords);
547 /* Normal -------------------------------- */
548 if (normal != NULL) {
549 float *ptrToCoords = (float *)(normal + (SkipnStrides * sd->u.s.normal.dwStride));
550 normal_funcs[sd->u.s.normal.dwType](ptrToCoords);
553 /* Position -------------------------------- */
554 if (position) {
555 float *ptrToCoords = (float *)(position + (SkipnStrides * sd->u.s.position.dwStride));
556 position_funcs[sd->u.s.position.dwType](ptrToCoords);
559 /* For non indexed mode, step onto next parts */
560 if (idxData == NULL) {
561 ++SkipnStrides;
565 glEnd();
566 checkGLcall("glEnd and previous calls");
569 static inline void send_attribute(IWineD3DDeviceImpl *This, const DWORD type, const UINT index, const void *ptr) {
570 switch(type) {
571 case WINED3DDECLTYPE_FLOAT1:
572 GL_EXTCALL(glVertexAttrib1fvARB(index, (float *) ptr));
573 break;
574 case WINED3DDECLTYPE_FLOAT2:
575 GL_EXTCALL(glVertexAttrib2fvARB(index, (float *) ptr));
576 break;
577 case WINED3DDECLTYPE_FLOAT3:
578 GL_EXTCALL(glVertexAttrib3fvARB(index, (float *) ptr));
579 break;
580 case WINED3DDECLTYPE_FLOAT4:
581 GL_EXTCALL(glVertexAttrib4fvARB(index, (float *) ptr));
582 break;
584 case WINED3DDECLTYPE_UBYTE4:
585 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
586 break;
587 case WINED3DDECLTYPE_UBYTE4N:
588 case WINED3DDECLTYPE_D3DCOLOR:
589 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
590 break;
592 case WINED3DDECLTYPE_SHORT2:
593 GL_EXTCALL(glVertexAttrib4svARB(index, (GLshort *) ptr));
594 break;
595 case WINED3DDECLTYPE_SHORT4:
596 GL_EXTCALL(glVertexAttrib4svARB(index, (GLshort *) ptr));
597 break;
599 case WINED3DDECLTYPE_SHORT2N:
601 GLshort s[4] = {((short *) ptr)[0], ((short *) ptr)[1], 0, 1};
602 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
603 break;
605 case WINED3DDECLTYPE_USHORT2N:
607 GLushort s[4] = {((unsigned short *) ptr)[0], ((unsigned short *) ptr)[1], 0, 1};
608 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
609 break;
611 case WINED3DDECLTYPE_SHORT4N:
612 GL_EXTCALL(glVertexAttrib4NsvARB(index, (GLshort *) ptr));
613 break;
614 case WINED3DDECLTYPE_USHORT4N:
615 GL_EXTCALL(glVertexAttrib4NusvARB(index, (GLushort *) ptr));
616 break;
618 case WINED3DDECLTYPE_UDEC3:
619 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
620 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
621 break;
622 case WINED3DDECLTYPE_DEC3N:
623 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
624 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
625 break;
627 case WINED3DDECLTYPE_FLOAT16_2:
628 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
629 * byte float according to the IEEE standard
631 if (GL_SUPPORT(NV_HALF_FLOAT)) {
632 GL_EXTCALL(glVertexAttrib2hvNV(index, (GLhalfNV *)ptr));
633 } else {
634 float x = float_16_to_32(((unsigned short *) ptr) + 0);
635 float y = float_16_to_32(((unsigned short *) ptr) + 1);
636 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
638 break;
639 case WINED3DDECLTYPE_FLOAT16_4:
640 if (GL_SUPPORT(NV_HALF_FLOAT)) {
641 GL_EXTCALL(glVertexAttrib4hvNV(index, (GLhalfNV *)ptr));
642 } else {
643 float x = float_16_to_32(((unsigned short *) ptr) + 0);
644 float y = float_16_to_32(((unsigned short *) ptr) + 1);
645 float z = float_16_to_32(((unsigned short *) ptr) + 2);
646 float w = float_16_to_32(((unsigned short *) ptr) + 3);
647 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
649 break;
651 case WINED3DDECLTYPE_UNUSED:
652 default:
653 ERR("Unexpected attribute declaration: %d\n", type);
654 break;
658 static void drawStridedSlowVs(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
659 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx,
660 ULONG startVertex) {
662 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
663 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
664 const WORD *pIdxBufS = NULL;
665 const DWORD *pIdxBufL = NULL;
666 LONG vx_index;
667 int i;
668 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
669 BYTE *ptr;
671 if (idxSize != 0) {
672 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
673 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
674 * idxData will be != NULL
676 if(idxData == NULL) {
677 idxData = ((IWineD3DIndexBufferImpl *) stateblock->pIndexData)->resource.allocatedMemory;
680 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
681 else pIdxBufL = (const DWORD *) idxData;
684 /* Start drawing in GL */
685 VTRACE(("glBegin(%x)\n", glPrimitiveType));
686 glBegin(glPrimitiveType);
688 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
689 if (idxData != NULL) {
691 /* Indexed so work out the number of strides to skip */
692 if (idxSize == 2) {
693 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
694 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
695 } else {
696 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
697 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
701 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
702 if(!sd->u.input[i].lpData) continue;
704 ptr = sd->u.input[i].lpData +
705 sd->u.input[i].dwStride * SkipnStrides +
706 stateblock->streamOffset[sd->u.input[i].streamNo];
708 send_attribute(This, sd->u.input[i].dwType, i, ptr);
710 SkipnStrides++;
713 glEnd();
716 void depth_blt(IWineD3DDevice *iface, GLuint texture, GLsizei w, GLsizei h) {
717 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
718 GLint old_binding = 0;
720 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
722 glDisable(GL_CULL_FACE);
723 glEnable(GL_BLEND);
724 glDisable(GL_ALPHA_TEST);
725 glDisable(GL_SCISSOR_TEST);
726 glDisable(GL_STENCIL_TEST);
727 glEnable(GL_DEPTH_TEST);
728 glDepthFunc(GL_ALWAYS);
729 glDepthMask(GL_TRUE);
730 glBlendFunc(GL_ZERO, GL_ONE);
731 glViewport(0, 0, w, h);
733 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
734 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
735 glBindTexture(GL_TEXTURE_2D, texture);
736 glEnable(GL_TEXTURE_2D);
738 This->shader_backend->shader_select_depth_blt(iface);
740 glBegin(GL_TRIANGLE_STRIP);
741 glVertex2f(-1.0f, -1.0f);
742 glVertex2f(1.0f, -1.0f);
743 glVertex2f(-1.0f, 1.0f);
744 glVertex2f(1.0f, 1.0f);
745 glEnd();
747 glBindTexture(GL_TEXTURE_2D, old_binding);
749 glPopAttrib();
751 This->shader_backend->shader_deselect_depth_blt(iface);
754 static inline void drawStridedInstanced(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
755 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
756 ULONG startIdx, ULONG startVertex) {
757 UINT numInstances = 0;
758 int numInstancedAttribs = 0, i, j;
759 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
760 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
761 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
763 if (idxSize == 0) {
764 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
765 * We don't support this for now
767 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
768 * But the StreamSourceFreq value has a different meaning in that situation.
770 FIXME("Non-indexed instanced drawing is not supported\n");
771 return;
774 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
775 idxData = idxData == (void *)-1 ? NULL : idxData;
777 /* First, figure out how many instances we have to draw */
778 for(i = 0; i < MAX_STREAMS; i++) {
779 /* Look at the streams and take the first one which matches */
780 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
781 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
782 if(stateblock->streamFreq[i] == 0){
783 numInstances = 1;
784 } else {
785 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
787 break; /* break, because only the first suitable value is interesting */
791 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
792 if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
793 instancedData[numInstancedAttribs] = i;
794 numInstancedAttribs++;
798 /* now draw numInstances instances :-) */
799 for(i = 0; i < numInstances; i++) {
800 /* Specify the instanced attributes using immediate mode calls */
801 for(j = 0; j < numInstancedAttribs; j++) {
802 BYTE *ptr = sd->u.input[instancedData[j]].lpData +
803 sd->u.input[instancedData[j]].dwStride * i +
804 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
805 if(sd->u.input[instancedData[j]].VBO) {
806 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
807 ptr += (long) vb->resource.allocatedMemory;
810 send_attribute(This, sd->u.input[instancedData[j]].dwType, instancedData[j], ptr);
813 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
814 (const char *)idxData+(idxSize * startIdx));
815 checkGLcall("glDrawElements");
819 static inline void remove_vbos(IWineD3DDeviceImpl *This, WineDirect3DVertexStridedData *s) {
820 unsigned char i;
821 IWineD3DVertexBufferImpl *vb;
823 if(s->u.s.position.VBO) {
824 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position.streamNo];
825 s->u.s.position.VBO = 0;
826 s->u.s.position.lpData = (BYTE *) ((unsigned long) s->u.s.position.lpData + (unsigned long) vb->resource.allocatedMemory);
828 if(s->u.s.blendWeights.VBO) {
829 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendWeights.streamNo];
830 s->u.s.blendWeights.VBO = 0;
831 s->u.s.blendWeights.lpData = (BYTE *) ((unsigned long) s->u.s.blendWeights.lpData + (unsigned long) vb->resource.allocatedMemory);
833 if(s->u.s.blendMatrixIndices.VBO) {
834 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendMatrixIndices.streamNo];
835 s->u.s.blendMatrixIndices.VBO = 0;
836 s->u.s.blendMatrixIndices.lpData = (BYTE *) ((unsigned long) s->u.s.blendMatrixIndices.lpData + (unsigned long) vb->resource.allocatedMemory);
838 if(s->u.s.normal.VBO) {
839 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal.streamNo];
840 s->u.s.normal.VBO = 0;
841 s->u.s.normal.lpData = (BYTE *) ((unsigned long) s->u.s.normal.lpData + (unsigned long) vb->resource.allocatedMemory);
843 if(s->u.s.pSize.VBO) {
844 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.pSize.streamNo];
845 s->u.s.pSize.VBO = 0;
846 s->u.s.pSize.lpData = (BYTE *) ((unsigned long) s->u.s.pSize.lpData + (unsigned long) vb->resource.allocatedMemory);
848 if(s->u.s.diffuse.VBO) {
849 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.diffuse.streamNo];
850 s->u.s.diffuse.VBO = 0;
851 s->u.s.diffuse.lpData = (BYTE *) ((unsigned long) s->u.s.diffuse.lpData + (unsigned long) vb->resource.allocatedMemory);
853 if(s->u.s.specular.VBO) {
854 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.specular.streamNo];
855 s->u.s.specular.VBO = 0;
856 s->u.s.specular.lpData = (BYTE *) ((unsigned long) s->u.s.specular.lpData + (unsigned long) vb->resource.allocatedMemory);
858 for(i = 0; i < WINED3DDP_MAXTEXCOORD; i++) {
859 if(s->u.s.texCoords[i].VBO) {
860 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.texCoords[i].streamNo];
861 s->u.s.texCoords[i].VBO = 0;
862 s->u.s.texCoords[i].lpData = (BYTE *) ((unsigned long) s->u.s.texCoords[i].lpData + (unsigned long) vb->resource.allocatedMemory);
865 if(s->u.s.position2.VBO) {
866 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position2.streamNo];
867 s->u.s.position2.VBO = 0;
868 s->u.s.position2.lpData = (BYTE *) ((unsigned long) s->u.s.position2.lpData + (unsigned long) vb->resource.allocatedMemory);
870 if(s->u.s.normal2.VBO) {
871 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal2.streamNo];
872 s->u.s.normal2.VBO = 0;
873 s->u.s.normal2.lpData = (BYTE *) ((unsigned long) s->u.s.normal2.lpData + (unsigned long) vb->resource.allocatedMemory);
875 if(s->u.s.tangent.VBO) {
876 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tangent.streamNo];
877 s->u.s.tangent.VBO = 0;
878 s->u.s.tangent.lpData = (BYTE *) ((unsigned long) s->u.s.tangent.lpData + (unsigned long) vb->resource.allocatedMemory);
880 if(s->u.s.binormal.VBO) {
881 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.binormal.streamNo];
882 s->u.s.binormal.VBO = 0;
883 s->u.s.binormal.lpData = (BYTE *) ((unsigned long) s->u.s.binormal.lpData + (unsigned long) vb->resource.allocatedMemory);
885 if(s->u.s.tessFactor.VBO) {
886 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tessFactor.streamNo];
887 s->u.s.tessFactor.VBO = 0;
888 s->u.s.tessFactor.lpData = (BYTE *) ((unsigned long) s->u.s.tessFactor.lpData + (unsigned long) vb->resource.allocatedMemory);
890 if(s->u.s.fog.VBO) {
891 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.fog.streamNo];
892 s->u.s.fog.VBO = 0;
893 s->u.s.fog.lpData = (BYTE *) ((unsigned long) s->u.s.fog.lpData + (unsigned long) vb->resource.allocatedMemory);
895 if(s->u.s.depth.VBO) {
896 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.depth.streamNo];
897 s->u.s.depth.VBO = 0;
898 s->u.s.depth.lpData = (BYTE *) ((unsigned long) s->u.s.depth.lpData + (unsigned long) vb->resource.allocatedMemory);
900 if(s->u.s.sample.VBO) {
901 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.sample.streamNo];
902 s->u.s.sample.VBO = 0;
903 s->u.s.sample.lpData = (BYTE *) ((unsigned long) s->u.s.sample.lpData + (unsigned long) vb->resource.allocatedMemory);
907 /* Routine common to the draw primitive and draw indexed primitive routines */
908 void drawPrimitive(IWineD3DDevice *iface,
909 int PrimitiveType,
910 long NumPrimitives,
911 /* for Indexed: */
912 long StartVertexIndex,
913 UINT numberOfVertices,
914 long StartIdx,
915 short idxSize,
916 const void *idxData,
917 int minIndex) {
919 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
920 IWineD3DSurfaceImpl *target;
921 int i;
923 if (NumPrimitives == 0) return;
925 /* Invalidate the back buffer memory so LockRect will read it the next time */
926 for(i = 0; i < GL_LIMITS(buffers); i++) {
927 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
928 if (target) {
929 IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
930 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
934 /* Signals other modules that a drawing is in progress and the stateblock finalized */
935 This->isInDraw = TRUE;
937 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
939 if (This->stencilBufferTarget) {
940 /* Note that this depends on the ActivateContext call above to set
941 * This->render_offscreen properly */
942 DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
943 surface_load_ds_location(This->stencilBufferTarget, location);
944 surface_modify_ds_location(This->stencilBufferTarget, location);
947 /* Ok, we will be updating the screen from here onwards so grab the lock */
948 ENTER_GL();
950 GLenum glPrimType;
951 BOOL emulation = FALSE;
952 WineDirect3DVertexStridedData *strided = &This->strided_streams;
953 WineDirect3DVertexStridedData stridedlcl;
954 /* Ok, Work out which primitive is requested and how many vertexes that
955 will be */
956 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
957 if (numberOfVertices == 0 )
958 numberOfVertices = calculatedNumberOfindices;
960 if(!use_vs(This)) {
961 if(!This->strided_streams.u.s.position_transformed && This->activeContext->num_untracked_materials &&
962 This->stateBlock->renderState[WINED3DRS_LIGHTING]) {
963 static BOOL first = TRUE;
964 if(first) {
965 FIXME("Using software emulation because not all material properties could be tracked\n");
966 first = FALSE;
967 } else {
968 TRACE("Using software emulation because not all material properties could be tracked\n");
970 emulation = TRUE;
972 else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
973 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
974 * to a float in the vertex buffer
976 static BOOL first = TRUE;
977 if(first) {
978 FIXME("Using software emulation because manual fog coordinates are provided\n");
979 first = FALSE;
980 } else {
981 TRACE("Using software emulation because manual fog coordinates are provided\n");
983 emulation = TRUE;
986 if(emulation) {
987 strided = &stridedlcl;
988 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
989 remove_vbos(This, &stridedlcl);
993 if (This->useDrawStridedSlow || emulation) {
994 /* Immediate mode drawing */
995 if(use_vs(This)) {
996 static BOOL first = TRUE;
997 if(first) {
998 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
999 first = FALSE;
1000 } else {
1001 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
1003 drawStridedSlowVs(iface, strided, calculatedNumberOfindices, glPrimType,
1004 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1005 } else {
1006 drawStridedSlow(iface, strided, calculatedNumberOfindices,
1007 glPrimType, idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1009 } else if(This->instancedDraw) {
1010 /* Instancing emulation with mixing immediate mode and arrays */
1011 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
1012 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1013 } else {
1014 drawStridedFast(iface, calculatedNumberOfindices, glPrimType,
1015 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1019 /* Finished updating the screen, restore lock */
1020 LEAVE_GL();
1021 TRACE("Done all gl drawing\n");
1023 /* Diagnostics */
1024 #ifdef SHOW_FRAME_MAKEUP
1026 static long int primCounter = 0;
1027 /* NOTE: set primCounter to the value reported by drawprim
1028 before you want to to write frame makeup to /tmp */
1029 if (primCounter >= 0) {
1030 WINED3DLOCKED_RECT r;
1031 char buffer[80];
1032 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
1033 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
1034 TRACE("Saving screenshot %s\n", buffer);
1035 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
1036 IWineD3DSurface_UnlockRect(This->render_targets[0]);
1038 #ifdef SHOW_TEXTURE_MAKEUP
1040 IWineD3DSurface *pSur;
1041 int textureNo;
1042 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
1043 if (This->stateBlock->textures[textureNo] != NULL) {
1044 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
1045 TRACE("Saving texture %s\n", buffer);
1046 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
1047 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
1048 IWineD3DSurface_SaveSnapshot(pSur, buffer);
1049 IWineD3DSurface_Release(pSur);
1050 } else {
1051 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
1056 #endif
1058 TRACE("drawprim #%ld\n", primCounter);
1059 ++primCounter;
1061 #endif
1063 /* Control goes back to the device, stateblock values may change again */
1064 This->isInDraw = FALSE;
1067 static void normalize_normal(float *n) {
1068 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
1069 if(length == 0.0) return;
1070 length = sqrt(length);
1071 n[0] = n[0] / length;
1072 n[1] = n[1] / length;
1073 n[2] = n[2] / length;
1076 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
1078 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
1079 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
1080 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
1081 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
1082 * in drawprim.
1084 * To read back, the opengl feedback mode is used. This creates a problem because we want
1085 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
1086 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
1087 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
1088 * them to [-1.0;+1.0] and set the viewport up to scale them back.
1090 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
1091 * resulting colors back to the normals.
1093 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
1094 * does not restore it because normally a draw follows immediately afterwards. The caller is
1095 * responsible of taking care that either the gl states are restored, or the context activated
1096 * for drawing to reset the lastWasBlit flag.
1098 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
1099 struct WineD3DRectPatch *patch) {
1100 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
1101 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
1102 WineDirect3DVertexStridedData strided;
1103 BYTE *data;
1104 WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
1105 DWORD vtxStride;
1106 GLenum feedback_type;
1107 GLfloat *feedbuffer;
1109 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1110 * Beware of vbos
1112 memset(&strided, 0, sizeof(strided));
1113 primitiveDeclarationConvertToStridedData((IWineD3DDevice *) This, FALSE, &strided, NULL);
1114 if(strided.u.s.position.VBO) {
1115 IWineD3DVertexBufferImpl *vb;
1116 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[strided.u.s.position.streamNo];
1117 strided.u.s.position.lpData = (BYTE *) ((unsigned long) strided.u.s.position.lpData +
1118 (unsigned long) vb->resource.allocatedMemory);
1120 vtxStride = strided.u.s.position.dwStride;
1121 data = strided.u.s.position.lpData +
1122 vtxStride * info->Stride * info->StartVertexOffsetHeight +
1123 vtxStride * info->StartVertexOffsetWidth;
1125 /* Not entirely sure about what happens with transformed vertices */
1126 if(strided.u.s.position_transformed) {
1127 FIXME("Transformed position in rectpatch generation\n");
1129 if(vtxStride % sizeof(GLfloat)) {
1130 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1131 * I don't see how the stride could not be a multiple of 4, but make sure
1132 * to check it
1134 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1136 if(info->Basis != WINED3DBASIS_BEZIER) {
1137 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
1139 if(info->Degree != WINED3DDEGREE_CUBIC) {
1140 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
1143 /* First, get the boundary cube of the input data */
1144 for(j = 0; j < info->Height; j++) {
1145 for(i = 0; i < info->Width; i++) {
1146 float *v = (float *) (data + vtxStride * i + vtxStride * info->Stride * j);
1147 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
1148 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
1149 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
1150 if(v[2] < neg_z) neg_z = v[2];
1154 /* This needs some improvements in the vertex decl code */
1155 FIXME("Cannot find data to generate. Only generating position and normals\n");
1156 patch->has_normals = TRUE;
1157 patch->has_texcoords = FALSE;
1159 /* Simply activate the context for blitting. This disables all the things we don't want and
1160 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
1161 * patch (as opposed to normal draws) will most likely need different changes anyway
1163 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
1164 ENTER_GL();
1166 glMatrixMode(GL_PROJECTION);
1167 checkGLcall("glMatrixMode(GL_PROJECTION)");
1168 glLoadIdentity();
1169 checkGLcall("glLoadIndentity()");
1170 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
1171 glTranslatef(0, 0, 0.5);
1172 checkGLcall("glScalef");
1173 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
1174 checkGLcall("glViewport");
1176 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1177 * our feedback buffer parser
1179 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1180 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1181 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
1182 if(patch->has_normals) {
1183 float black[4] = {0, 0, 0, 0};
1184 float red[4] = {1, 0, 0, 0};
1185 float green[4] = {0, 1, 0, 0};
1186 float blue[4] = {0, 0, 1, 0};
1187 float white[4] = {1, 1, 1, 1};
1188 glEnable(GL_LIGHTING);
1189 checkGLcall("glEnable(GL_LIGHTING)");
1190 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
1191 checkGLcall("glLightModel for MODEL_AMBIENT");
1192 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
1194 for(i = 3; i < GL_LIMITS(lights); i++) {
1195 glDisable(GL_LIGHT0 + i);
1196 checkGLcall("glDisable(GL_LIGHT0 + i)");
1197 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
1200 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
1201 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
1202 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
1203 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
1204 glLightfv(GL_LIGHT0, GL_POSITION, red);
1205 glEnable(GL_LIGHT0);
1206 checkGLcall("Setting up light 1\n");
1207 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
1208 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
1209 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
1210 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
1211 glLightfv(GL_LIGHT1, GL_POSITION, green);
1212 glEnable(GL_LIGHT1);
1213 checkGLcall("Setting up light 2\n");
1214 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
1215 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
1216 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
1217 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
1218 glLightfv(GL_LIGHT2, GL_POSITION, blue);
1219 glEnable(GL_LIGHT2);
1220 checkGLcall("Setting up light 3\n");
1222 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
1223 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
1224 glDisable(GL_COLOR_MATERIAL);
1225 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
1226 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
1227 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
1228 checkGLcall("Setting up materials\n");
1231 /* Enable the needed maps.
1232 * GL_MAP2_VERTEX_3 is needed for positional data.
1233 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1234 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1236 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
1237 out_vertex_size = 3 /* position */;
1238 d3d_out_vertex_size = 3;
1239 glEnable(GL_MAP2_VERTEX_3);
1240 if(patch->has_normals && patch->has_texcoords) {
1241 FIXME("Texcoords not handled yet\n");
1242 feedback_type = GL_3D_COLOR_TEXTURE;
1243 out_vertex_size += 8;
1244 d3d_out_vertex_size += 7;
1245 glEnable(GL_AUTO_NORMAL);
1246 glEnable(GL_MAP2_TEXTURE_COORD_4);
1247 } else if(patch->has_texcoords) {
1248 FIXME("Texcoords not handled yet\n");
1249 feedback_type = GL_3D_COLOR_TEXTURE;
1250 out_vertex_size += 7;
1251 d3d_out_vertex_size += 4;
1252 glEnable(GL_MAP2_TEXTURE_COORD_4);
1253 } else if(patch->has_normals) {
1254 feedback_type = GL_3D_COLOR;
1255 out_vertex_size += 4;
1256 d3d_out_vertex_size += 3;
1257 glEnable(GL_AUTO_NORMAL);
1258 } else {
1259 feedback_type = GL_3D;
1261 checkGLcall("glEnable vertex attrib generation");
1263 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
1264 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
1265 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
1267 glMap2f(GL_MAP2_VERTEX_3,
1268 0, 1, vtxStride / sizeof(float), info->Width,
1269 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1270 (float *) data);
1271 checkGLcall("glMap2f");
1272 if(patch->has_texcoords) {
1273 glMap2f(GL_MAP2_TEXTURE_COORD_4,
1274 0, 1, vtxStride / sizeof(float), info->Width,
1275 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1276 (float *) data);
1277 checkGLcall("glMap2f");
1279 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
1280 checkGLcall("glMapGrid2f");
1282 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
1283 checkGLcall("glFeedbackBuffer");
1284 glRenderMode(GL_FEEDBACK);
1286 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1287 checkGLcall("glEvalMesh2\n");
1289 i = glRenderMode(GL_RENDER);
1290 if(i == -1) {
1291 LEAVE_GL();
1292 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
1293 Sleep(10000);
1294 HeapFree(GetProcessHeap(), 0, feedbuffer);
1295 return WINED3DERR_DRIVERINTERNALERROR;
1296 } else if(i != buffer_size) {
1297 LEAVE_GL();
1298 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1299 Sleep(10000);
1300 HeapFree(GetProcessHeap(), 0, feedbuffer);
1301 return WINED3DERR_DRIVERINTERNALERROR;
1302 } else {
1303 TRACE("Got %d elements as expected\n", i);
1306 HeapFree(GetProcessHeap(), 0, patch->mem);
1307 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1308 i = 0;
1309 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1310 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1311 ERR("Unexpected token: %f\n", feedbuffer[j]);
1312 continue;
1314 if(feedbuffer[j + 1] != 3) {
1315 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1316 continue;
1318 /* Somehow there are different ideas about back / front facing, so fix up the
1319 * vertex order
1321 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1322 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1323 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
1324 if(patch->has_normals) {
1325 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1326 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1327 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1329 i += d3d_out_vertex_size;
1331 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1332 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1333 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
1334 if(patch->has_normals) {
1335 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1336 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1337 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1339 i += d3d_out_vertex_size;
1341 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1342 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1343 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
1344 if(patch->has_normals) {
1345 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1346 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1347 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1349 i += d3d_out_vertex_size;
1352 if(patch->has_normals) {
1353 /* Now do the same with reverse light directions */
1354 float x[4] = {-1, 0, 0, 0};
1355 float y[4] = { 0, -1, 0, 0};
1356 float z[4] = { 0, 0, -1, 0};
1357 glLightfv(GL_LIGHT0, GL_POSITION, x);
1358 glLightfv(GL_LIGHT1, GL_POSITION, y);
1359 glLightfv(GL_LIGHT2, GL_POSITION, z);
1360 checkGLcall("Setting up reverse light directions\n");
1362 glRenderMode(GL_FEEDBACK);
1363 checkGLcall("glRenderMode(GL_FEEDBACK)");
1364 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1365 checkGLcall("glEvalMesh2\n");
1366 i = glRenderMode(GL_RENDER);
1367 checkGLcall("glRenderMode(GL_RENDER)");
1369 i = 0;
1370 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1371 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1372 ERR("Unexpected token: %f\n", feedbuffer[j]);
1373 continue;
1375 if(feedbuffer[j + 1] != 3) {
1376 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1377 continue;
1379 if(patch->mem[i + 3] == 0.0)
1380 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1381 if(patch->mem[i + 4] == 0.0)
1382 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1383 if(patch->mem[i + 5] == 0.0)
1384 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1385 normalize_normal(patch->mem + i + 3);
1386 i += d3d_out_vertex_size;
1388 if(patch->mem[i + 3] == 0.0)
1389 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1390 if(patch->mem[i + 4] == 0.0)
1391 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1392 if(patch->mem[i + 5] == 0.0)
1393 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1394 normalize_normal(patch->mem + i + 3);
1395 i += d3d_out_vertex_size;
1397 if(patch->mem[i + 3] == 0.0)
1398 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1399 if(patch->mem[i + 4] == 0.0)
1400 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1401 if(patch->mem[i + 5] == 0.0)
1402 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1403 normalize_normal(patch->mem + i + 3);
1404 i += d3d_out_vertex_size;
1408 glDisable(GL_MAP2_VERTEX_3);
1409 glDisable(GL_AUTO_NORMAL);
1410 glDisable(GL_MAP2_NORMAL);
1411 glDisable(GL_MAP2_TEXTURE_COORD_4);
1412 checkGLcall("glDisable vertex attrib generation");
1413 LEAVE_GL();
1415 HeapFree(GetProcessHeap(), 0, feedbuffer);
1417 vtxStride = 3 * sizeof(float);
1418 if(patch->has_normals) {
1419 vtxStride += 3 * sizeof(float);
1421 if(patch->has_texcoords) {
1422 vtxStride += 4 * sizeof(float);
1424 memset(&patch->strided, 0, sizeof(&patch->strided));
1425 patch->strided.u.s.position.lpData = (BYTE *) patch->mem;
1426 patch->strided.u.s.position.dwStride = vtxStride;
1427 patch->strided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
1428 patch->strided.u.s.position.streamNo = 255;
1430 if(patch->has_normals) {
1431 patch->strided.u.s.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1432 patch->strided.u.s.normal.dwStride = vtxStride;
1433 patch->strided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
1434 patch->strided.u.s.normal.streamNo = 255;
1436 if(patch->has_texcoords) {
1437 patch->strided.u.s.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1438 if(patch->has_normals) {
1439 patch->strided.u.s.texCoords[0].lpData += 3 * sizeof(float);
1441 patch->strided.u.s.texCoords[0].dwStride = vtxStride;
1442 patch->strided.u.s.texCoords[0].dwType = WINED3DDECLTYPE_FLOAT4;
1443 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1444 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1445 * application.
1447 patch->strided.u.s.texCoords[0].streamNo = MAX_STREAMS;
1450 return WINED3D_OK;