wined3d: Trace the declaration element type in primitiveDeclarationConvertToStridedDa...
[wine.git] / dlls / wined3d / drawprim.c
blob721d9131f94b60b40da7abea931cde4cba0cf020
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 Henri Verbeet
9 * Copyright 2007 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>
34 #if 0 /* TODO */
35 extern IWineD3DVertexShaderImpl* VertexShaders[64];
36 extern IWineD3DVertexDeclarationImpl* VertexShaderDeclarations[64];
37 extern IWineD3DPixelShaderImpl* PixelShaders[64];
39 #undef GL_VERSION_1_4 /* To be fixed, caused by mesa headers */
40 #endif
42 /* Issues the glBegin call for gl given the primitive type and count */
43 static DWORD primitiveToGl(WINED3DPRIMITIVETYPE PrimitiveType,
44 DWORD NumPrimitives,
45 GLenum *primType)
47 DWORD NumVertexes = NumPrimitives;
49 switch (PrimitiveType) {
50 case WINED3DPT_POINTLIST:
51 TRACE("POINTS\n");
52 *primType = GL_POINTS;
53 NumVertexes = NumPrimitives;
54 break;
56 case WINED3DPT_LINELIST:
57 TRACE("LINES\n");
58 *primType = GL_LINES;
59 NumVertexes = NumPrimitives * 2;
60 break;
62 case WINED3DPT_LINESTRIP:
63 TRACE("LINE_STRIP\n");
64 *primType = GL_LINE_STRIP;
65 NumVertexes = NumPrimitives + 1;
66 break;
68 case WINED3DPT_TRIANGLELIST:
69 TRACE("TRIANGLES\n");
70 *primType = GL_TRIANGLES;
71 NumVertexes = NumPrimitives * 3;
72 break;
74 case WINED3DPT_TRIANGLESTRIP:
75 TRACE("TRIANGLE_STRIP\n");
76 *primType = GL_TRIANGLE_STRIP;
77 NumVertexes = NumPrimitives + 2;
78 break;
80 case WINED3DPT_TRIANGLEFAN:
81 TRACE("TRIANGLE_FAN\n");
82 *primType = GL_TRIANGLE_FAN;
83 NumVertexes = NumPrimitives + 2;
84 break;
86 default:
87 FIXME("Unhandled primitive\n");
88 *primType = GL_POINTS;
89 break;
91 return NumVertexes;
94 static BOOL fixed_get_input(
95 BYTE usage, BYTE usage_idx,
96 unsigned int* regnum) {
98 *regnum = -1;
100 /* Those positions must have the order in the
101 * named part of the strided data */
103 if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 0)
104 *regnum = 0;
105 else if (usage == WINED3DDECLUSAGE_BLENDWEIGHT && usage_idx == 0)
106 *regnum = 1;
107 else if (usage == WINED3DDECLUSAGE_BLENDINDICES && usage_idx == 0)
108 *regnum = 2;
109 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 0)
110 *regnum = 3;
111 else if (usage == WINED3DDECLUSAGE_PSIZE && usage_idx == 0)
112 *regnum = 4;
113 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 0)
114 *regnum = 5;
115 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 1)
116 *regnum = 6;
117 else if (usage == WINED3DDECLUSAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
118 *regnum = 7 + usage_idx;
119 else if ((usage == WINED3DDECLUSAGE_POSITION || usage == WINED3DDECLUSAGE_POSITIONT) && usage_idx == 1)
120 *regnum = 7 + WINED3DDP_MAXTEXCOORD;
121 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 1)
122 *regnum = 8 + WINED3DDP_MAXTEXCOORD;
123 else if (usage == WINED3DDECLUSAGE_TANGENT && usage_idx == 0)
124 *regnum = 9 + WINED3DDP_MAXTEXCOORD;
125 else if (usage == WINED3DDECLUSAGE_BINORMAL && usage_idx == 0)
126 *regnum = 10 + WINED3DDP_MAXTEXCOORD;
127 else if (usage == WINED3DDECLUSAGE_TESSFACTOR && usage_idx == 0)
128 *regnum = 11 + WINED3DDP_MAXTEXCOORD;
129 else if (usage == WINED3DDECLUSAGE_FOG && usage_idx == 0)
130 *regnum = 12 + WINED3DDP_MAXTEXCOORD;
131 else if (usage == WINED3DDECLUSAGE_DEPTH && usage_idx == 0)
132 *regnum = 13 + WINED3DDP_MAXTEXCOORD;
133 else if (usage == WINED3DDECLUSAGE_SAMPLE && usage_idx == 0)
134 *regnum = 14 + WINED3DDP_MAXTEXCOORD;
136 if (*regnum < 0) {
137 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
138 debug_d3ddeclusage(usage), usage_idx);
139 return FALSE;
141 return TRUE;
144 void primitiveDeclarationConvertToStridedData(
145 IWineD3DDevice *iface,
146 BOOL useVertexShaderFunction,
147 WineDirect3DVertexStridedData *strided,
148 BOOL *fixup) {
150 /* We need to deal with frequency data!*/
152 BYTE *data = NULL;
153 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
154 IWineD3DVertexDeclarationImpl* vertexDeclaration = (IWineD3DVertexDeclarationImpl *)This->stateBlock->vertexDecl;
155 int i;
156 WINED3DVERTEXELEMENT *element;
157 DWORD stride;
158 int reg;
159 char isPreLoaded[MAX_STREAMS];
160 DWORD preLoadStreams[MAX_STREAMS], numPreloadStreams = 0;
162 memset(isPreLoaded, 0, sizeof(isPreLoaded));
164 /* Check for transformed vertices, disable vertex shader if present */
165 strided->u.s.position_transformed = FALSE;
166 for (i = 0; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
167 element = vertexDeclaration->pDeclarationWine + i;
169 if (element->Usage == WINED3DDECLUSAGE_POSITIONT) {
170 strided->u.s.position_transformed = TRUE;
171 useVertexShaderFunction = FALSE;
175 /* Translate the declaration into strided data */
176 for (i = 0 ; i < vertexDeclaration->declarationWNumElements - 1; ++i) {
177 GLint streamVBO = 0;
178 BOOL stride_used;
179 unsigned int idx;
181 element = vertexDeclaration->pDeclarationWine + i;
182 TRACE("%p Element %p (%d of %d)\n", vertexDeclaration->pDeclarationWine,
183 element, i + 1, vertexDeclaration->declarationWNumElements - 1);
185 if (This->stateBlock->streamSource[element->Stream] == NULL)
186 continue;
188 if (This->stateBlock->streamIsUP) {
189 TRACE("Stream is up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
190 streamVBO = 0;
191 data = (BYTE *)This->stateBlock->streamSource[element->Stream];
192 } else {
193 TRACE("Stream isn't up %d, %p\n", element->Stream, This->stateBlock->streamSource[element->Stream]);
194 if(!isPreLoaded[element->Stream]) {
195 preLoadStreams[numPreloadStreams] = element->Stream;
196 numPreloadStreams++;
197 isPreLoaded[element->Stream] = 1;
199 data = IWineD3DVertexBufferImpl_GetMemory(This->stateBlock->streamSource[element->Stream], 0, &streamVBO);
200 if(fixup) {
201 if( streamVBO != 0) *fixup = TRUE;
202 else if(*fixup && !useVertexShaderFunction &&
203 (element->Usage == WINED3DDECLUSAGE_COLOR ||
204 element->Usage == WINED3DDECLUSAGE_POSITIONT)) {
205 /* This may be bad with the fixed function pipeline */
206 FIXME("Missing vbo streams with unfixed colors or transformed position, expect problems\n");
210 stride = This->stateBlock->streamStride[element->Stream];
211 data += element->Offset;
212 reg = element->Reg;
214 TRACE("Offset %d Stream %d UsageIndex %d\n", element->Offset, element->Stream, element->UsageIndex);
216 if (useVertexShaderFunction)
217 stride_used = vshader_get_input(This->stateBlock->vertexShader,
218 element->Usage, element->UsageIndex, &idx);
219 else
220 stride_used = fixed_get_input(element->Usage, element->UsageIndex, &idx);
222 if (stride_used) {
223 TRACE("Loaded %s array %u [usage=%s, usage_idx=%u, "
224 "stream=%u, offset=%u, stride=%u, type=%s, VBO=%u]\n",
225 useVertexShaderFunction? "shader": "fixed function", idx,
226 debug_d3ddeclusage(element->Usage), element->UsageIndex,
227 element->Stream, element->Offset, stride, debug_d3ddecltype(element->Type), streamVBO);
229 strided->u.input[idx].lpData = data;
230 strided->u.input[idx].dwType = element->Type;
231 strided->u.input[idx].dwStride = stride;
232 strided->u.input[idx].VBO = streamVBO;
233 strided->u.input[idx].streamNo = element->Stream;
236 /* Now call PreLoad on all the vertex buffers. In the very rare case
237 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
238 * The vertex buffer can now use the strided structure in the device instead of finding its
239 * own again.
241 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
242 * once in there.
244 for(i=0; i < numPreloadStreams; i++) {
245 IWineD3DVertexBuffer_PreLoad(This->stateBlock->streamSource[preLoadStreams[i]]);
249 static void drawStridedFast(IWineD3DDevice *iface,UINT numberOfVertices, GLenum glPrimitiveType,
250 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
251 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
253 if (idxSize != 0 /* This crashes sometimes!*/) {
254 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
255 idxData = idxData == (void *)-1 ? NULL : idxData;
256 #if 1
257 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
258 (const char *)idxData+(idxSize * startIdx));
259 #else /* using drawRangeElements may be faster */
261 glDrawRangeElements(glPrimitiveType, minIndex, minIndex + numberOfVertices - 1, numberOfVertices,
262 idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
263 (const char *)idxData+(idxSize * startIdx));
264 #endif
265 checkGLcall("glDrawRangeElements");
267 } else {
269 /* Note first is now zero as we shuffled along earlier */
270 TRACE("(%p) : glDrawArrays(%x, 0, %d)\n", This, glPrimitiveType, numberOfVertices);
271 glDrawArrays(glPrimitiveType, startVertex, numberOfVertices);
272 checkGLcall("glDrawArrays");
276 return;
280 * Actually draw using the supplied information.
281 * Slower GL version which extracts info about each vertex in turn
284 static void drawStridedSlow(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd,
285 UINT NumVertexes, GLenum glPrimType,
286 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
288 unsigned int textureNo = 0;
289 const WORD *pIdxBufS = NULL;
290 const DWORD *pIdxBufL = NULL;
291 LONG vx_index;
292 float x = 0.0f, y = 0.0f, z = 0.0f; /* x,y,z coordinates */
293 float rhw = 0.0f; /* rhw */
294 DWORD diffuseColor = 0xFFFFFFFF; /* Diffuse Color */
295 DWORD specularColor = 0; /* Specular Color */
296 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
297 UINT *streamOffset = This->stateBlock->streamOffset;
298 DWORD SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
300 BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
301 BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
303 TRACE("Using slow vertex array code\n");
305 /* Variable Initialization */
306 if (idxSize != 0) {
307 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
308 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
309 * idxData will be != NULL
311 if(idxData == NULL) {
312 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
315 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
316 else pIdxBufL = (const DWORD *) idxData;
319 /* Adding the stream offset once is cheaper than doing it every iteration. Do not modify the strided data, it is a pointer
320 * to the strided Data in the device and might be needed intact on the next draw
322 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
323 if(sd->u.s.texCoords[textureNo].lpData) {
324 texCoords[textureNo] = sd->u.s.texCoords[textureNo].lpData + streamOffset[sd->u.s.texCoords[textureNo].streamNo];
325 } else {
326 texCoords[textureNo] = NULL;
329 if(sd->u.s.diffuse.lpData) {
330 diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
332 if(sd->u.s.specular.lpData) {
333 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
335 if(sd->u.s.normal.lpData) {
336 normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
338 if(sd->u.s.position.lpData) {
339 position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
342 /* Start drawing in GL */
343 VTRACE(("glBegin(%x)\n", glPrimType));
344 glBegin(glPrimType);
346 /* Default settings for data that is not passed */
347 if (sd->u.s.normal.lpData == NULL) {
348 glNormal3f(0, 0, 1);
350 if(sd->u.s.diffuse.lpData == NULL) {
351 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
353 if(sd->u.s.specular.lpData == NULL) {
354 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
355 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
359 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
360 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
363 /* For each primitive */
364 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
366 /* Initialize diffuse color */
367 diffuseColor = 0xFFFFFFFF;
369 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
370 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
373 /* For indexed data, we need to go a few more strides in */
374 if (idxData != NULL) {
376 /* Indexed so work out the number of strides to skip */
377 if (idxSize == 2) {
378 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
379 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
380 } else {
381 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
382 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
386 /* Texture coords --------------------------- */
387 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
389 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0) {
390 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
391 continue ;
394 /* Query tex coords */
395 if (This->stateBlock->textures[textureNo] != NULL) {
397 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
398 float *ptrToCoords = NULL;
399 float s = 0.0, t = 0.0, r = 0.0, q = 0.0;
401 if (coordIdx > 7) {
402 VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo));
403 continue;
404 } else if (coordIdx < 0) {
405 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
406 continue;
409 ptrToCoords = (float *)(texCoords[coordIdx] + (SkipnStrides * sd->u.s.texCoords[coordIdx].dwStride));
410 if (texCoords[coordIdx] == NULL) {
411 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
412 continue;
413 } else {
414 int texture_idx = This->texUnitMap[textureNo];
415 int coordsToUse = sd->u.s.texCoords[coordIdx].dwType + 1; /* 0 == WINED3DDECLTYPE_FLOAT1 etc */
417 if (texture_idx == -1) continue;
419 /* The coords to supply depend completely on the fvf / vertex shader */
420 switch (coordsToUse) {
421 case 4: q = ptrToCoords[3]; /* drop through */
422 case 3: r = ptrToCoords[2]; /* drop through */
423 case 2: t = ptrToCoords[1]; /* drop through */
424 case 1: s = ptrToCoords[0];
427 /* Projected is more 'fun' - Move the last coord to the 'q'
428 parameter (see comments under WINED3DTSS_TEXTURETRANSFORMFLAGS */
429 if ((This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] != WINED3DTTFF_DISABLE) &&
430 (This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED)) {
432 if (This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
433 switch (coordsToUse) {
434 case 0: /* Drop Through */
435 case 1:
436 FIXME("WINED3DTTFF_PROJECTED but only zero or one coordinate?\n");
437 break;
438 case 2:
439 q = t;
440 t = 0.0;
441 coordsToUse = 4;
442 break;
443 case 3:
444 q = r;
445 r = 0.0;
446 coordsToUse = 4;
447 break;
448 case 4: /* Nop here */
449 break;
450 default:
451 FIXME("Unexpected WINED3DTSS_TEXTURETRANSFORMFLAGS value of %d\n",
452 This->stateBlock->textureState[textureNo][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED);
457 switch (coordsToUse) { /* Supply the provided texture coords */
458 case WINED3DTTFF_COUNT1:
459 VTRACE(("tex:%d, s=%f\n", textureNo, s));
460 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
461 GL_EXTCALL(glMultiTexCoord1fARB(texture_idx, s));
462 } else {
463 glTexCoord1f(s);
465 break;
466 case WINED3DTTFF_COUNT2:
467 VTRACE(("tex:%d, s=%f, t=%f\n", textureNo, s, t));
468 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
469 GL_EXTCALL(glMultiTexCoord2fARB(texture_idx, s, t));
470 } else {
471 glTexCoord2f(s, t);
473 break;
474 case WINED3DTTFF_COUNT3:
475 VTRACE(("tex:%d, s=%f, t=%f, r=%f\n", textureNo, s, t, r));
476 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
477 GL_EXTCALL(glMultiTexCoord3fARB(texture_idx, s, t, r));
478 } else {
479 glTexCoord3f(s, t, r);
481 break;
482 case WINED3DTTFF_COUNT4:
483 VTRACE(("tex:%d, s=%f, t=%f, r=%f, q=%f\n", textureNo, s, t, r, q));
484 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
485 GL_EXTCALL(glMultiTexCoord4fARB(texture_idx, s, t, r, q));
486 } else {
487 glTexCoord4f(s, t, r, q);
489 break;
490 default:
491 FIXME("Should not get here as coordsToUse is two bits only (%x)!\n", coordsToUse);
495 } /* End of textures */
497 /* Diffuse -------------------------------- */
498 if (diffuse) {
499 DWORD *ptrToCoords = (DWORD *)(diffuse + (SkipnStrides * sd->u.s.diffuse.dwStride));
500 diffuseColor = ptrToCoords[0];
501 VTRACE(("diffuseColor=%lx\n", diffuseColor));
503 glColor4ub(D3DCOLOR_B_R(diffuseColor),
504 D3DCOLOR_B_G(diffuseColor),
505 D3DCOLOR_B_B(diffuseColor),
506 D3DCOLOR_B_A(diffuseColor));
507 VTRACE(("glColor4ub: r,g,b,a=%lu,%lu,%lu,%lu\n",
508 D3DCOLOR_B_R(diffuseColor),
509 D3DCOLOR_B_G(diffuseColor),
510 D3DCOLOR_B_B(diffuseColor),
511 D3DCOLOR_B_A(diffuseColor)));
513 if(This->activeContext->num_untracked_materials) {
514 unsigned char i;
515 float color[4];
516 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
517 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
518 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
519 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
521 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
522 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
527 /* Specular ------------------------------- */
528 if (specular) {
529 DWORD *ptrToCoords = (DWORD *)(specular + (SkipnStrides * sd->u.s.specular.dwStride));
530 specularColor = ptrToCoords[0];
531 VTRACE(("specularColor=%lx\n", specularColor));
533 /* special case where the fog density is stored in the specular alpha channel */
534 if(This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
535 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4 )&&
536 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
537 if(GL_SUPPORT(EXT_FOG_COORD)) {
538 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
539 } else {
540 static BOOL warned = FALSE;
541 if(!warned) {
542 /* TODO: Use the fog table code from old ddraw */
543 FIXME("Implement fog for transformed vertices in software\n");
544 warned = TRUE;
549 VTRACE(("glSecondaryColor4ub: r,g,b=%lu,%lu,%lu\n",
550 D3DCOLOR_B_R(specularColor),
551 D3DCOLOR_B_G(specularColor),
552 D3DCOLOR_B_B(specularColor)));
553 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
554 GL_EXTCALL(glSecondaryColor3ubEXT)(
555 D3DCOLOR_B_R(specularColor),
556 D3DCOLOR_B_G(specularColor),
557 D3DCOLOR_B_B(specularColor));
558 } else {
559 /* Do not worry if specular colour missing and disable request */
560 VTRACE(("Specular color extensions not supplied\n"));
564 /* Normal -------------------------------- */
565 if (normal != NULL) {
566 float *ptrToCoords = (float *)(normal + (SkipnStrides * sd->u.s.normal.dwStride));
568 VTRACE(("glNormal:nx,ny,nz=%f,%f,%f\n", ptrToCoords[0], ptrToCoords[1], ptrToCoords[2]));
569 glNormal3f(ptrToCoords[0], ptrToCoords[1], ptrToCoords[2]);
572 /* Position -------------------------------- */
573 if (position) {
574 float *ptrToCoords = (float *)(position + (SkipnStrides * sd->u.s.position.dwStride));
575 x = ptrToCoords[0];
576 y = ptrToCoords[1];
577 z = ptrToCoords[2];
578 rhw = 1.0;
579 VTRACE(("x,y,z=%f,%f,%f\n", x,y,z));
581 /* RHW follows, only if transformed, ie 4 floats were provided */
582 if (sd->u.s.position_transformed) {
583 rhw = ptrToCoords[3];
584 VTRACE(("rhw=%f\n", rhw));
587 if (1.0f == rhw || ((rhw < eps) && (rhw > -eps))) {
588 VTRACE(("Vertex: glVertex:x,y,z=%f,%f,%f\n", x,y,z));
589 glVertex3f(x, y, z);
590 } else {
591 GLfloat w = 1.0 / rhw;
592 VTRACE(("Vertex: glVertex:x,y,z=%f,%f,%f / rhw=%f\n", x,y,z,rhw));
593 glVertex4f(x*w, y*w, z*w, w);
597 /* For non indexed mode, step onto next parts */
598 if (idxData == NULL) {
599 ++SkipnStrides;
603 glEnd();
604 checkGLcall("glEnd and previous calls");
607 static void depth_blt(IWineD3DDevice *iface, GLuint texture) {
608 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
609 GLint old_binding = 0;
611 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
613 glDisable(GL_CULL_FACE);
614 glEnable(GL_BLEND);
615 glDisable(GL_ALPHA_TEST);
616 glDisable(GL_SCISSOR_TEST);
617 glDisable(GL_STENCIL_TEST);
618 glEnable(GL_DEPTH_TEST);
619 glDepthFunc(GL_ALWAYS);
620 glBlendFunc(GL_ZERO, GL_ONE);
622 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
623 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
624 glBindTexture(GL_TEXTURE_2D, texture);
625 glEnable(GL_TEXTURE_2D);
627 This->shader_backend->shader_select_depth_blt(iface);
629 glBegin(GL_TRIANGLE_STRIP);
630 glVertex2f(-1.0f, -1.0f);
631 glVertex2f(1.0f, -1.0f);
632 glVertex2f(-1.0f, 1.0f);
633 glVertex2f(1.0f, 1.0f);
634 glEnd();
636 glBindTexture(GL_TEXTURE_2D, old_binding);
638 glPopAttrib();
640 /* Reselect the old shaders. There doesn't seem to be any glPushAttrib bit for arb shaders,
641 * and this seems easier and more efficient than providing the shader backend with a private
642 * storage to read and restore the old shader settings
644 This->shader_backend->shader_select(iface, use_ps(This), use_vs(This));
647 static void depth_copy(IWineD3DDevice *iface) {
648 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
649 IWineD3DSurfaceImpl *depth_stencil = (IWineD3DSurfaceImpl *)This->depthStencilBuffer;
651 /* Only copy the depth buffer if there is one. */
652 if (!depth_stencil) return;
654 /* TODO: Make this work for modes other than FBO */
655 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
657 if (depth_stencil->current_renderbuffer) {
658 FIXME("Not supported with fixed up depth stencil\n");
659 return;
662 if (This->render_offscreen) {
663 static GLuint tmp_texture = 0;
664 GLint old_binding = 0;
666 TRACE("Copying onscreen depth buffer to offscreen surface\n");
668 if (!tmp_texture) {
669 glGenTextures(1, &tmp_texture);
672 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
673 * directly on the FBO texture. That's because we need to flip. */
674 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
675 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
676 glBindTexture(GL_TEXTURE_2D, tmp_texture);
677 glCopyTexImage2D(depth_stencil->glDescription.target,
678 depth_stencil->glDescription.level,
679 depth_stencil->glDescription.glFormatInternal,
682 depth_stencil->currentDesc.Width,
683 depth_stencil->currentDesc.Height,
685 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
686 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
687 glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
688 glBindTexture(GL_TEXTURE_2D, old_binding);
690 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, This->fbo));
691 checkGLcall("glBindFramebuffer()");
692 depth_blt(iface, tmp_texture);
693 checkGLcall("depth_blt");
694 } else {
695 TRACE("Copying offscreen surface to onscreen depth buffer\n");
697 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
698 checkGLcall("glBindFramebuffer()");
699 depth_blt(iface, depth_stencil->glDescription.textureName);
700 checkGLcall("depth_blt");
704 static inline void drawStridedInstanced(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
705 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
706 ULONG startIdx, ULONG startVertex) {
707 UINT numInstances = 0;
708 int numInstancedAttribs = 0, i, j;
709 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
710 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
711 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
713 if (idxSize == 0) {
714 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
715 * We don't support this for now
717 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
718 * But the StreamSourceFreq value has a different meaning in that situation.
720 FIXME("Non-indexed instanced drawing is not supported\n");
721 return;
724 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
725 idxData = idxData == (void *)-1 ? NULL : idxData;
727 /* First, figure out how many instances we have to draw */
728 for(i = 0; i < MAX_STREAMS; i++) {
729 /* Look at all non-instanced streams */
730 if(!(stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) &&
731 stateblock->streamSource[i]) {
732 int inst = stateblock->streamFreq[i];
734 if(numInstances && inst != numInstances) {
735 ERR("Two streams specify a different number of instances. Got %d, new is %d\n", numInstances, inst);
737 numInstances = inst;
741 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
742 if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
743 instancedData[numInstancedAttribs] = i;
744 numInstancedAttribs++;
748 /* now draw numInstances instances :-) */
749 for(i = 0; i < numInstances; i++) {
750 /* Specify the instanced attributes using immediate mode calls */
751 for(j = 0; j < numInstancedAttribs; j++) {
752 BYTE *ptr = sd->u.input[instancedData[j]].lpData +
753 sd->u.input[instancedData[j]].dwStride * i +
754 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
755 if(sd->u.input[instancedData[j]].VBO) {
756 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
757 ptr += (long) vb->resource.allocatedMemory;
760 switch(sd->u.input[instancedData[j]].dwType) {
761 case WINED3DDECLTYPE_FLOAT1:
762 GL_EXTCALL(glVertexAttrib1fvARB(instancedData[j], (float *) ptr));
763 break;
764 case WINED3DDECLTYPE_FLOAT2:
765 GL_EXTCALL(glVertexAttrib2fvARB(instancedData[j], (float *) ptr));
766 break;
767 case WINED3DDECLTYPE_FLOAT3:
768 GL_EXTCALL(glVertexAttrib3fvARB(instancedData[j], (float *) ptr));
769 break;
770 case WINED3DDECLTYPE_FLOAT4:
771 GL_EXTCALL(glVertexAttrib4fvARB(instancedData[j], (float *) ptr));
772 break;
774 case WINED3DDECLTYPE_UBYTE4:
775 GL_EXTCALL(glVertexAttrib4NubvARB(instancedData[j], ptr));
776 break;
777 case WINED3DDECLTYPE_UBYTE4N:
778 case WINED3DDECLTYPE_D3DCOLOR:
779 GL_EXTCALL(glVertexAttrib4NubvARB(instancedData[j], ptr));
780 break;
782 case WINED3DDECLTYPE_SHORT2:
783 GL_EXTCALL(glVertexAttrib4svARB(instancedData[j], (GLshort *) ptr));
784 break;
785 case WINED3DDECLTYPE_SHORT4:
786 GL_EXTCALL(glVertexAttrib4svARB(instancedData[j], (GLshort *) ptr));
787 break;
789 case WINED3DDECLTYPE_SHORT2N:
791 GLshort s[4] = {((short *) ptr)[0], ((short *) ptr)[1], 0, 1};
792 GL_EXTCALL(glVertexAttrib4NsvARB(instancedData[j], s));
793 break;
795 case WINED3DDECLTYPE_USHORT2N:
797 GLushort s[4] = {((unsigned short *) ptr)[0], ((unsigned short *) ptr)[1], 0, 1};
798 GL_EXTCALL(glVertexAttrib4NusvARB(instancedData[j], s));
799 break;
801 case WINED3DDECLTYPE_SHORT4N:
802 GL_EXTCALL(glVertexAttrib4NsvARB(instancedData[j], (GLshort *) ptr));
803 break;
804 case WINED3DDECLTYPE_USHORT4N:
805 GL_EXTCALL(glVertexAttrib4NusvARB(instancedData[j], (GLushort *) ptr));
806 break;
808 case WINED3DDECLTYPE_UDEC3:
809 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
810 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
811 break;
812 case WINED3DDECLTYPE_DEC3N:
813 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
814 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
815 break;
817 case WINED3DDECLTYPE_FLOAT16_2:
818 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
819 * byte float according to the IEEE standard
821 FIXME("Unsupported WINED3DDECLTYPE_FLOAT16_2\n");
822 break;
823 case WINED3DDECLTYPE_FLOAT16_4:
824 FIXME("Unsupported WINED3DDECLTYPE_FLOAT16_4\n");
825 break;
827 case WINED3DDECLTYPE_UNUSED:
828 default:
829 ERR("Unexpected declaration in instanced attributes\n");
830 break;
834 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
835 (const char *)idxData+(idxSize * startIdx));
836 checkGLcall("glDrawElements");
840 struct coords {
841 int x, y, z;
844 void blt_to_drawable(IWineD3DDeviceImpl *This, IWineD3DSurfaceImpl *surface) {
845 struct coords coords[4];
846 int low_coord;
848 /* TODO: This could be supported for lazy unlocking */
849 if(!(surface->Flags & SFLAG_INTEXTURE)) {
850 /* It is ok at init to be nowhere */
851 if(!(surface->Flags & SFLAG_INSYSMEM)) {
852 ERR("Blitting surfaces from sysmem not supported yet\n");
854 return;
857 ENTER_GL();
858 ActivateContext(This, This->render_targets[0], CTXUSAGE_BLIT);
860 if(surface->glDescription.target == GL_TEXTURE_2D) {
861 glBindTexture(GL_TEXTURE_2D, surface->glDescription.textureName);
862 checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
864 coords[0].x = 0; coords[0].y = 0; coords[0].z = 0;
865 coords[1].x = 0; coords[1].y = 1; coords[1].z = 0;
866 coords[2].x = 1; coords[2].y = 1; coords[2].z = 0;
867 coords[3].x = 1; coords[3].y = 0; coords[3].z = 0;
869 low_coord = 0;
870 } else {
871 /* Must be a cube map */
872 glDisable(GL_TEXTURE_2D);
873 checkGLcall("glDisable(GL_TEXTURE_2D)");
874 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
875 checkGLcall("glEnable(surface->glDescription.target)");
876 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, surface->glDescription.textureName);
877 checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
879 switch(surface->glDescription.target) {
880 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
881 coords[0].x = 1; coords[0].y = -1; coords[0].z = 1;
882 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
883 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
884 coords[3].x = 1; coords[3].y = -1; coords[3].z = -1;
885 break;
887 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
888 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
889 coords[1].x = -1; coords[1].y = 1; coords[1].z = 1;
890 coords[2].x = -1; coords[2].y = 1; coords[2].z = -1;
891 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
892 break;
894 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
895 coords[0].x = -1; coords[0].y = 1; coords[0].z = 1;
896 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
897 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
898 coords[3].x = -1; coords[3].y = 1; coords[3].z = -1;
899 break;
901 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
902 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
903 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
904 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
905 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
906 break;
908 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
909 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
910 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
911 coords[2].x = 1; coords[2].y = -1; coords[2].z = 1;
912 coords[3].x = -1; coords[3].y = -1; coords[3].z = 1;
913 break;
915 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
916 coords[0].x = -1; coords[0].y = -1; coords[0].z = -1;
917 coords[1].x = 1; coords[1].y = -1; coords[1].z = -1;
918 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
919 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
921 default:
922 ERR("Unexpected texture target\n");
923 LEAVE_GL();
924 return;
927 low_coord = -1;
930 if(This->render_offscreen) {
931 coords[0].y = coords[0].y == 1 ? low_coord : 1;
932 coords[1].y = coords[1].y == 1 ? low_coord : 1;
933 coords[2].y = coords[2].y == 1 ? low_coord : 1;
934 coords[3].y = coords[3].y == 1 ? low_coord : 1;
937 glBegin(GL_QUADS);
938 glTexCoord3iv((GLint *) &coords[0]);
939 glVertex2i(0, 0);
941 glTexCoord3iv((GLint *) &coords[1]);
942 glVertex2i(0, surface->pow2Height);
944 glTexCoord3iv((GLint *) &coords[2]);
945 glVertex2i(surface->pow2Width, surface->pow2Height);
947 glTexCoord3iv((GLint *) &coords[3]);
948 glVertex2i(surface->pow2Width, 0);
949 glEnd();
950 checkGLcall("glEnd");
952 if(surface->glDescription.target != GL_TEXTURE_2D) {
953 glEnable(GL_TEXTURE_2D);
954 checkGLcall("glEnable(GL_TEXTURE_2D)");
955 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
956 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
958 LEAVE_GL();
961 /* Routine common to the draw primitive and draw indexed primitive routines */
962 void drawPrimitive(IWineD3DDevice *iface,
963 int PrimitiveType,
964 long NumPrimitives,
965 /* for Indexed: */
966 long StartVertexIndex,
967 UINT numberOfVertices,
968 long StartIdx,
969 short idxSize,
970 const void *idxData,
971 int minIndex) {
973 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
974 IWineD3DSwapChain *swapchain;
975 IWineD3DBaseTexture *texture = NULL;
976 IWineD3DSurfaceImpl *target;
977 int i;
979 /* Signals other modules that a drawing is in progress and the stateblock finalized */
980 This->isInDraw = TRUE;
982 /* Invalidate the back buffer memory so LockRect will read it the next time */
983 for(i = 0; i < GL_LIMITS(buffers); i++) {
984 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
986 /* TODO: Only do all that if we're going to change anything
987 * Texture container dirtification does not work quite right yet
989 if(target /*&& target->Flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)*/) {
990 swapchain = NULL;
991 texture = NULL;
993 if(i == 0) {
994 IWineD3DSurface_GetContainer((IWineD3DSurface *) target, &IID_IWineD3DSwapChain, (void **)&swapchain);
996 /* Need the surface in the drawable! */
997 if(!(target->Flags & SFLAG_INDRAWABLE) && (swapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
998 blt_to_drawable(This, target);
1001 if(swapchain) {
1002 /* Onscreen target. Invalidate system memory copy and texture copy */
1003 target->Flags &= ~(SFLAG_INSYSMEM | SFLAG_INTEXTURE);
1004 target->Flags |= SFLAG_INDRAWABLE;
1005 IWineD3DSwapChain_Release(swapchain);
1006 } else if(wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
1007 /* Non-FBO target: Invalidate system copy, texture copy and dirtify the container */
1008 IWineD3DSurface_GetContainer((IWineD3DSurface *) target, &IID_IWineD3DBaseTexture, (void **)&texture);
1010 if(texture) {
1011 IWineD3DBaseTexture_SetDirty(texture, TRUE);
1012 IWineD3DTexture_Release(texture);
1015 target->Flags &= ~(SFLAG_INSYSMEM | SFLAG_INTEXTURE);
1016 target->Flags |= SFLAG_INDRAWABLE;
1017 } else {
1018 /* FBO offscreen target. Invalidate system memory copy */
1019 target->Flags &= ~SFLAG_INSYSMEM;
1021 } else {
1022 /* Must be an fbo render target */
1023 target->Flags &= ~SFLAG_INSYSMEM;
1024 target->Flags |= SFLAG_INTEXTURE;
1029 /* Ok, we will be updating the screen from here onwards so grab the lock */
1030 ENTER_GL();
1032 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
1033 apply_fbo_state(iface);
1036 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
1038 if (This->depth_copy_state == WINED3D_DCS_COPY) {
1039 depth_copy(iface);
1041 This->depth_copy_state = WINED3D_DCS_INITIAL;
1044 GLenum glPrimType;
1045 BOOL emulation = FALSE;
1046 WineDirect3DVertexStridedData *strided = &This->strided_streams;
1047 WineDirect3DVertexStridedData stridedlcl;
1048 /* Ok, Work out which primitive is requested and how many vertexes that
1049 will be */
1050 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
1051 if (numberOfVertices == 0 )
1052 numberOfVertices = calculatedNumberOfindices;
1054 if(!This->strided_streams.u.s.position_transformed && !use_vs(This)) {
1055 if(This->activeContext->num_untracked_materials &&
1056 This->stateBlock->renderState[WINED3DRS_LIGHTING]) {
1057 IWineD3DVertexBufferImpl *vb;
1059 FIXME("Using software emulation because not all material properties could be tracked\n");
1060 emulation = TRUE;
1062 strided = &stridedlcl;
1063 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
1065 #define FIXVBO(type) \
1066 if(stridedlcl.u.s.type.VBO) { \
1067 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[stridedlcl.u.s.type.streamNo]; \
1068 stridedlcl.u.s.type.VBO = 0; \
1069 stridedlcl.u.s.type.lpData = (BYTE *) ((unsigned long) stridedlcl.u.s.type.lpData + (unsigned long) vb->resource.allocatedMemory); \
1071 FIXVBO(position);
1072 FIXVBO(blendWeights);
1073 FIXVBO(blendMatrixIndices);
1074 FIXVBO(normal);
1075 FIXVBO(pSize);
1076 FIXVBO(diffuse);
1077 FIXVBO(specular);
1078 for(i = 0; i < WINED3DDP_MAXTEXCOORD; i++) FIXVBO(texCoords[i]);
1079 FIXVBO(position2);
1080 FIXVBO(normal2);
1081 FIXVBO(tangent);
1082 FIXVBO(binormal);
1083 FIXVBO(tessFactor);
1084 FIXVBO(fog);
1085 FIXVBO(depth);
1086 FIXVBO(sample);
1087 #undef FIXVBO
1091 if (This->useDrawStridedSlow || emulation) {
1092 /* Immediate mode drawing */
1093 drawStridedSlow(iface, strided, calculatedNumberOfindices,
1094 glPrimType, idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1095 } else if(This->instancedDraw) {
1096 /* Instancing emulation with mixing immediate mode and arrays */
1097 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
1098 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1099 } else {
1100 /* Simple array draw call */
1101 drawStridedFast(iface, calculatedNumberOfindices, glPrimType,
1102 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1106 /* Finshed updating the screen, restore lock */
1107 LEAVE_GL();
1108 TRACE("Done all gl drawing\n");
1110 /* Diagnostics */
1111 #ifdef SHOW_FRAME_MAKEUP
1113 static long int primCounter = 0;
1114 /* NOTE: set primCounter to the value reported by drawprim
1115 before you want to to write frame makeup to /tmp */
1116 if (primCounter >= 0) {
1117 WINED3DLOCKED_RECT r;
1118 char buffer[80];
1119 IWineD3DSurface_LockRect(This->renderTarget, &r, NULL, WINED3DLOCK_READONLY);
1120 sprintf(buffer, "/tmp/backbuffer_%d.tga", primCounter);
1121 TRACE("Saving screenshot %s\n", buffer);
1122 IWineD3DSurface_SaveSnapshot(This->renderTarget, buffer);
1123 IWineD3DSurface_UnlockRect(This->renderTarget);
1125 #ifdef SHOW_TEXTURE_MAKEUP
1127 IWineD3DSurface *pSur;
1128 int textureNo;
1129 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
1130 if (This->stateBlock->textures[textureNo] != NULL) {
1131 sprintf(buffer, "/tmp/texture_%p_%d_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
1132 TRACE("Saving texture %s\n", buffer);
1133 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
1134 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
1135 IWineD3DSurface_SaveSnapshot(pSur, buffer);
1136 IWineD3DSurface_Release(pSur);
1137 } else {
1138 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
1143 #endif
1145 TRACE("drawprim #%d\n", primCounter);
1146 ++primCounter;
1148 #endif
1150 /* Control goes back to the device, stateblock values may change again */
1151 This->isInDraw = FALSE;
1154 static void normalize_normal(float *n) {
1155 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
1156 if(length == 0.0) return;
1157 length = sqrt(length);
1158 n[0] = n[0] / length;
1159 n[1] = n[1] / length;
1160 n[2] = n[2] / length;
1163 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
1165 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
1166 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
1167 * to chache the patches in a vertex buffer. But more importantly, gl can't bind generated
1168 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
1169 * in drawprim.
1171 * To read back, the opengl feedback mode is used. This creates a proplem because we want
1172 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
1173 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
1174 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
1175 * them to [-1.0;+1.0] and set the viewport up to scale them back.
1177 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
1178 * resulting colors back to the normals.
1180 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
1181 * does not restore it because normally a draw follows immediately afterwards. The caller is
1182 * responsible of taking care that either the gl states are restored, or the context activated
1183 * for drawing to reset the lastWasBlit flag.
1185 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
1186 struct WineD3DRectPatch *patch) {
1187 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
1188 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
1189 WineDirect3DVertexStridedData strided;
1190 BYTE *data;
1191 WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
1192 DWORD vtxStride;
1193 GLenum feedback_type;
1194 GLfloat *feedbuffer;
1196 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1197 * Beware of vbos
1199 memset(&strided, 0, sizeof(strided));
1200 primitiveDeclarationConvertToStridedData((IWineD3DDevice *) This, FALSE, &strided, NULL);
1201 if(strided.u.s.position.VBO) {
1202 IWineD3DVertexBufferImpl *vb;
1203 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[strided.u.s.position.streamNo];
1204 strided.u.s.position.lpData = (BYTE *) ((unsigned long) strided.u.s.position.lpData +
1205 (unsigned long) vb->resource.allocatedMemory);
1207 vtxStride = strided.u.s.position.dwStride;
1208 data = strided.u.s.position.lpData +
1209 vtxStride * info->Stride * info->StartVertexOffsetHeight +
1210 vtxStride * info->StartVertexOffsetWidth;
1212 /* Not entirely sure about what happens with transformed vertices */
1213 if(strided.u.s.position_transformed) {
1214 FIXME("Transformed position in rectpatch generation\n");
1216 if(vtxStride % sizeof(GLfloat)) {
1217 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1218 * I don't see how the stride could not be a multiple of 4, but make sure
1219 * to check it
1221 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1223 if(info->Basis != WINED3DBASIS_BEZIER) {
1224 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
1226 if(info->Degree != WINED3DDEGREE_CUBIC) {
1227 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
1230 /* First, get the boundary cube of the input data */
1231 for(j = 0; j < info->Height; j++) {
1232 for(i = 0; i < info->Width; i++) {
1233 float *v = (float *) (data + vtxStride * i + vtxStride * info->Stride * j);
1234 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
1235 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
1236 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
1237 if(v[2] < neg_z) neg_z = v[2];
1241 /* This needs some improvements in the vertex decl code */
1242 FIXME("Cannot find data to generate. Only generating position and normals\n");
1243 patch->has_normals = TRUE;
1244 patch->has_texcoords = FALSE;
1246 ENTER_GL();
1247 /* Simply activate the context for blitting. This disables all the things we don't want and
1248 * takes care for dirtifying. Dirtifying is prefered over pushing / popping, since drawing the
1249 * patch(as opposed to normal draws) will most likely need different changes anyway
1251 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
1253 glMatrixMode(GL_PROJECTION);
1254 checkGLcall("glMatrixMode(GL_PROJECTION)");
1255 glLoadIdentity();
1256 checkGLcall("glLoadIndentity()");
1257 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
1258 glTranslatef(0, 0, 0.5);
1259 checkGLcall("glScalef");
1260 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
1261 checkGLcall("glViewport");
1263 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1264 * our feedback buffer parser
1266 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1267 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1268 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
1269 if(patch->has_normals) {
1270 float black[4] = {0, 0, 0, 0};
1271 float red[4] = {1, 0, 0, 0};
1272 float green[4] = {0, 1, 0, 0};
1273 float blue[4] = {0, 0, 1, 0};
1274 float white[4] = {1, 1, 1, 1};
1275 glEnable(GL_LIGHTING);
1276 checkGLcall("glEnable(GL_LIGHTING)");
1277 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
1278 checkGLcall("glLightModel for MODEL_AMBIENT");
1279 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
1281 for(i = 3; i < GL_LIMITS(lights); i++) {
1282 glDisable(GL_LIGHT0 + i);
1283 checkGLcall("glDisable(GL_LIGHT0 + i)");
1284 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
1287 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
1288 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
1289 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
1290 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
1291 glLightfv(GL_LIGHT0, GL_POSITION, red);
1292 glEnable(GL_LIGHT0);
1293 checkGLcall("Setting up light 1\n");
1294 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
1295 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
1296 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
1297 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
1298 glLightfv(GL_LIGHT1, GL_POSITION, green);
1299 glEnable(GL_LIGHT1);
1300 checkGLcall("Setting up light 2\n");
1301 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
1302 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
1303 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
1304 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
1305 glLightfv(GL_LIGHT2, GL_POSITION, blue);
1306 glEnable(GL_LIGHT2);
1307 checkGLcall("Setting up light 3\n");
1309 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
1310 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
1311 glDisable(GL_COLOR_MATERIAL);
1312 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
1313 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
1314 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
1315 checkGLcall("Setting up materials\n");
1318 /* Enable the needed maps.
1319 * GL_MAP2_VERTEX_3 is needed for positional data.
1320 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1321 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1323 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
1324 out_vertex_size = 3 /* position */;
1325 d3d_out_vertex_size = 3;
1326 glEnable(GL_MAP2_VERTEX_3);
1327 if(patch->has_normals && patch->has_texcoords) {
1328 FIXME("Texcoords not handled yet\n");
1329 feedback_type = GL_3D_COLOR_TEXTURE;
1330 out_vertex_size += 8;
1331 d3d_out_vertex_size += 7;
1332 glEnable(GL_AUTO_NORMAL);
1333 glEnable(GL_MAP2_TEXTURE_COORD_4);
1334 } else if(patch->has_texcoords) {
1335 FIXME("Texcoords not handled yet\n");
1336 feedback_type = GL_3D_COLOR_TEXTURE;
1337 out_vertex_size += 7;
1338 d3d_out_vertex_size += 4;
1339 glEnable(GL_MAP2_TEXTURE_COORD_4);
1340 } else if(patch->has_normals) {
1341 feedback_type = GL_3D_COLOR;
1342 out_vertex_size += 4;
1343 d3d_out_vertex_size += 3;
1344 glEnable(GL_AUTO_NORMAL);
1345 } else {
1346 feedback_type = GL_3D;
1348 checkGLcall("glEnable vertex attrib generation");
1350 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
1351 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
1352 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
1354 glMap2f(GL_MAP2_VERTEX_3,
1355 0, 1, vtxStride / sizeof(float), info->Width,
1356 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1357 (float *) data);
1358 checkGLcall("glMap2f");
1359 if(patch->has_texcoords) {
1360 glMap2f(GL_MAP2_TEXTURE_COORD_4,
1361 0, 1, vtxStride / sizeof(float), info->Width,
1362 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1363 (float *) data);
1364 checkGLcall("glMap2f");
1366 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
1367 checkGLcall("glMapGrid2f");
1369 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
1370 checkGLcall("glFeedbackBuffer");
1371 glRenderMode(GL_FEEDBACK);
1373 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1374 checkGLcall("glEvalMesh2\n");
1376 i = glRenderMode(GL_RENDER);
1377 if(i == -1) {
1378 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
1379 Sleep(10000);
1380 HeapFree(GetProcessHeap(), 0, feedbuffer);
1381 return WINED3DERR_DRIVERINTERNALERROR;
1382 } else if(i != buffer_size) {
1383 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1384 Sleep(10000);
1385 HeapFree(GetProcessHeap(), 0, feedbuffer);
1386 return WINED3DERR_DRIVERINTERNALERROR;
1387 } else {
1388 TRACE("Got %d elements as expected\n", i);
1391 HeapFree(GetProcessHeap(), 0, patch->mem);
1392 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1393 i = 0;
1394 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1395 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1396 ERR("Unexpected token: %f\n", feedbuffer[j]);
1397 continue;
1399 if(feedbuffer[j + 1] != 3) {
1400 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1401 continue;
1403 /* Somehow there are different ideas about back / front facing, so fix up the
1404 * vertex order
1406 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1407 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1408 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
1409 if(patch->has_normals) {
1410 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1411 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1412 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1414 i += d3d_out_vertex_size;
1416 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1417 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1418 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
1419 if(patch->has_normals) {
1420 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1421 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1422 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1424 i += d3d_out_vertex_size;
1426 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1427 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1428 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
1429 if(patch->has_normals) {
1430 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1431 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1432 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1434 i += d3d_out_vertex_size;
1437 if(patch->has_normals) {
1438 /* Now do the same with reverse light directions */
1439 float x[4] = {-1, 0, 0, 0};
1440 float y[4] = { 0, -1, 0, 0};
1441 float z[4] = { 0, 0, -1, 0};
1442 glLightfv(GL_LIGHT0, GL_POSITION, x);
1443 glLightfv(GL_LIGHT1, GL_POSITION, y);
1444 glLightfv(GL_LIGHT2, GL_POSITION, z);
1445 checkGLcall("Setting up reverse light directions\n");
1447 glRenderMode(GL_FEEDBACK);
1448 checkGLcall("glRenderMode(GL_FEEDBACK)");
1449 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1450 checkGLcall("glEvalMesh2\n");
1451 i = glRenderMode(GL_RENDER);
1452 checkGLcall("glRenderMode(GL_RENDER)");
1454 i = 0;
1455 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1456 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1457 ERR("Unexpected token: %f\n", feedbuffer[j]);
1458 continue;
1460 if(feedbuffer[j + 1] != 3) {
1461 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1462 continue;
1464 if(patch->mem[i + 3] == 0.0)
1465 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1466 if(patch->mem[i + 4] == 0.0)
1467 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1468 if(patch->mem[i + 5] == 0.0)
1469 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1470 normalize_normal(patch->mem + i + 3);
1471 i += d3d_out_vertex_size;
1473 if(patch->mem[i + 3] == 0.0)
1474 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1475 if(patch->mem[i + 4] == 0.0)
1476 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1477 if(patch->mem[i + 5] == 0.0)
1478 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1479 normalize_normal(patch->mem + i + 3);
1480 i += d3d_out_vertex_size;
1482 if(patch->mem[i + 3] == 0.0)
1483 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1484 if(patch->mem[i + 4] == 0.0)
1485 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1486 if(patch->mem[i + 5] == 0.0)
1487 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1488 normalize_normal(patch->mem + i + 3);
1489 i += d3d_out_vertex_size;
1493 glDisable(GL_MAP2_VERTEX_3);
1494 glDisable(GL_AUTO_NORMAL);
1495 glDisable(GL_MAP2_NORMAL);
1496 glDisable(GL_MAP2_TEXTURE_COORD_4);
1497 checkGLcall("glDisable vertex attrib generation");
1498 LEAVE_GL();
1500 HeapFree(GetProcessHeap(), 0, feedbuffer);
1502 vtxStride = 3 * sizeof(float);
1503 if(patch->has_normals) {
1504 vtxStride += 3 * sizeof(float);
1506 if(patch->has_texcoords) {
1507 vtxStride += 4 * sizeof(float);
1509 memset(&patch->strided, 0, sizeof(&patch->strided));
1510 patch->strided.u.s.position.lpData = (BYTE *) patch->mem;
1511 patch->strided.u.s.position.dwStride = vtxStride;
1512 patch->strided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
1513 patch->strided.u.s.position.streamNo = 255;
1515 if(patch->has_normals) {
1516 patch->strided.u.s.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1517 patch->strided.u.s.normal.dwStride = vtxStride;
1518 patch->strided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
1519 patch->strided.u.s.normal.streamNo = 255;
1521 if(patch->has_texcoords) {
1522 patch->strided.u.s.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1523 if(patch->has_normals) {
1524 patch->strided.u.s.texCoords[0].lpData += 3 * sizeof(float);
1526 patch->strided.u.s.texCoords[0].dwStride = vtxStride;
1527 patch->strided.u.s.texCoords[0].dwType = WINED3DDECLTYPE_FLOAT4;
1528 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1529 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1530 * application.
1532 patch->strided.u.s.texCoords[0].streamNo = MAX_STREAMS;
1535 return WINED3D_OK;