push 8edcbf8579c1d48dd3fb4c679acf4b3012a9efac
[wine/hacks.git] / dlls / wined3d / drawprim.c
blob37cbe1975c6beb10082b55492f16d5bd71e74189
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("Loaded %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 strided->u.input[idx].lpData = data;
229 strided->u.input[idx].dwType = element->Type;
230 strided->u.input[idx].dwStride = stride;
231 strided->u.input[idx].VBO = streamVBO;
232 strided->u.input[idx].streamNo = element->Stream;
235 /* Now call PreLoad on all the vertex buffers. In the very rare case
236 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
237 * The vertex buffer can now use the strided structure in the device instead of finding its
238 * own again.
240 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
241 * once in there.
243 for(i=0; i < numPreloadStreams; i++) {
244 IWineD3DVertexBuffer *vb = This->stateBlock->streamSource[streams[i]];
245 if(vb) {
246 IWineD3DVertexBuffer_PreLoad(vb);
251 static void drawStridedFast(IWineD3DDevice *iface,UINT numberOfVertices, GLenum glPrimitiveType,
252 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
253 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
255 if (idxSize != 0 /* This crashes sometimes!*/) {
256 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
257 idxData = idxData == (void *)-1 ? NULL : idxData;
258 #if 1
259 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
260 (const char *)idxData+(idxSize * startIdx));
261 checkGLcall("glDrawElements");
262 #else /* using drawRangeElements may be faster */
264 glDrawRangeElements(glPrimitiveType, minIndex, minIndex + numberOfVertices - 1, numberOfVertices,
265 idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
266 (const char *)idxData+(idxSize * startIdx));
267 checkGLcall("glDrawRangeElements");
268 #endif
270 } else {
271 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, glPrimitiveType, startVertex, numberOfVertices);
272 glDrawArrays(glPrimitiveType, startVertex, numberOfVertices);
273 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 DWORD diffuseColor = 0xFFFFFFFF; /* Diffuse Color */
293 DWORD specularColor = 0; /* Specular Color */
294 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
295 UINT *streamOffset = This->stateBlock->streamOffset;
296 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
297 BOOL pixelShader = use_ps(This);
299 BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
300 BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
302 TRACE("Using slow vertex array code\n");
304 /* Variable Initialization */
305 if (idxSize != 0) {
306 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
307 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
308 * idxData will be != NULL
310 if(idxData == NULL) {
311 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
314 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
315 else pIdxBufL = (const DWORD *) idxData;
318 /* Adding the stream offset once is cheaper than doing it every iteration. Do not modify the strided data, it is a pointer
319 * to the strided Data in the device and might be needed intact on the next draw
321 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
322 if(sd->u.s.texCoords[textureNo].lpData) {
323 texCoords[textureNo] = sd->u.s.texCoords[textureNo].lpData + streamOffset[sd->u.s.texCoords[textureNo].streamNo];
324 } else {
325 texCoords[textureNo] = NULL;
328 if(sd->u.s.diffuse.lpData) {
329 diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
331 if(sd->u.s.specular.lpData) {
332 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
334 if(sd->u.s.normal.lpData) {
335 normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
337 if(sd->u.s.position.lpData) {
338 position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
341 /* The texture coordinate types are not so easy to map into a common function signature - we're
342 * not using the vector functions here
344 if(FIXME_ON(d3d_draw)) {
345 for (textureNo = 0; textureNo < GL_LIMITS(textures); ++textureNo) {
346 DWORD type = sd->u.s.texCoords[textureNo].dwType;
347 if (sd->u.s.texCoords[textureNo].lpData &&
348 type != WINED3DDECLTYPE_FLOAT1 &&
349 type != WINED3DDECLTYPE_FLOAT2 &&
350 type != WINED3DDECLTYPE_FLOAT3 &&
351 type != WINED3DDECLTYPE_FLOAT4) {
352 FIXME("Implement fixed function texture coordinates from %s\n", debug_d3ddecltype(type));
355 if(specular && This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
356 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position_transformed )&&
357 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
358 if(GL_SUPPORT(EXT_FOG_COORD) && sd->u.s.specular.dwType != WINED3DDECLTYPE_D3DCOLOR) {
359 FIXME("Implement fog coordinates from %s\n", debug_d3ddecltype(sd->u.s.specular.dwType));
362 if(This->activeContext->num_untracked_materials && sd->u.s.diffuse.dwType != WINED3DDECLTYPE_D3DCOLOR) {
363 FIXME("Implement diffuse color tracking from %s\n", debug_d3ddecltype(sd->u.s.diffuse.dwType));
367 /* Start drawing in GL */
368 VTRACE(("glBegin(%x)\n", glPrimType));
369 glBegin(glPrimType);
371 /* Default settings for data that is not passed */
372 if (sd->u.s.normal.lpData == NULL) {
373 glNormal3f(0, 0, 0);
375 if(sd->u.s.diffuse.lpData == NULL) {
376 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
378 if(sd->u.s.specular.lpData == NULL) {
379 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
380 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
384 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
385 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
388 /* For each primitive */
389 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
391 /* Initialize diffuse color */
392 diffuseColor = 0xFFFFFFFF;
394 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
395 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
398 /* For indexed data, we need to go a few more strides in */
399 if (idxData != NULL) {
401 /* Indexed so work out the number of strides to skip */
402 if (idxSize == 2) {
403 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
404 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
405 } else {
406 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
407 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
411 /* Texture coords --------------------------- */
412 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
414 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0) {
415 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
416 continue ;
419 /* Query tex coords */
420 if (This->stateBlock->textures[textureNo] != NULL || pixelShader) {
422 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
423 int texture_idx = This->texUnitMap[textureNo];
424 float *ptrToCoords = NULL;
425 float s = 0.0, t = 0.0, r = 0.0, q = 0.0;
427 if (coordIdx > 7) {
428 VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo));
429 continue;
430 } else if (coordIdx < 0) {
431 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
432 continue;
435 ptrToCoords = (float *)(texCoords[coordIdx] + (SkipnStrides * sd->u.s.texCoords[coordIdx].dwStride));
436 if (texCoords[coordIdx] == NULL) {
437 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
438 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
439 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
440 } else {
441 glTexCoord4f(0, 0, 0, 1);
443 continue;
444 } else {
445 int coordsToUse = sd->u.s.texCoords[coordIdx].dwType + 1; /* 0 == WINED3DDECLTYPE_FLOAT1 etc */
447 if (texture_idx == -1) continue;
449 /* The coords to supply depend completely on the fvf / vertex shader */
450 switch (coordsToUse) {
451 case 4: q = ptrToCoords[3]; /* drop through */
452 case 3: r = ptrToCoords[2]; /* drop through */
453 case 2: t = ptrToCoords[1]; /* drop through */
454 case 1: s = ptrToCoords[0];
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(GL_TEXTURE0_ARB + 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(GL_TEXTURE0_ARB + 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(GL_TEXTURE0_ARB + 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(GL_TEXTURE0_ARB + 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));
501 diffuse_funcs[sd->u.s.diffuse.dwType]((void *) ptrToCoords);
502 if(This->activeContext->num_untracked_materials) {
503 unsigned char i;
504 float color[4];
506 diffuseColor = ptrToCoords[0];
507 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
508 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
509 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
510 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
512 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
513 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
518 /* Specular ------------------------------- */
519 if (specular) {
520 DWORD *ptrToCoords = (DWORD *)(specular + (SkipnStrides * sd->u.s.specular.dwStride));
522 /* special case where the fog density is stored in the specular alpha channel */
523 if(This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
524 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4 )&&
525 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
526 if(GL_SUPPORT(EXT_FOG_COORD)) {
527 specularColor = ptrToCoords[0];
528 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
529 } else {
530 static BOOL warned = FALSE;
531 if(!warned) {
532 /* TODO: Use the fog table code from old ddraw */
533 FIXME("Implement fog for transformed vertices in software\n");
534 warned = TRUE;
539 specular_funcs[sd->u.s.specular.dwType]((void *) ptrToCoords);
542 /* Normal -------------------------------- */
543 if (normal != NULL) {
544 float *ptrToCoords = (float *)(normal + (SkipnStrides * sd->u.s.normal.dwStride));
545 normal_funcs[sd->u.s.normal.dwType](ptrToCoords);
548 /* Position -------------------------------- */
549 if (position) {
550 float *ptrToCoords = (float *)(position + (SkipnStrides * sd->u.s.position.dwStride));
551 position_funcs[sd->u.s.position.dwType](ptrToCoords);
554 /* For non indexed mode, step onto next parts */
555 if (idxData == NULL) {
556 ++SkipnStrides;
560 glEnd();
561 checkGLcall("glEnd and previous calls");
564 static inline void send_attribute(IWineD3DDeviceImpl *This, const DWORD type, const UINT index, const void *ptr) {
565 switch(type) {
566 case WINED3DDECLTYPE_FLOAT1:
567 GL_EXTCALL(glVertexAttrib1fvARB(index, (float *) ptr));
568 break;
569 case WINED3DDECLTYPE_FLOAT2:
570 GL_EXTCALL(glVertexAttrib2fvARB(index, (float *) ptr));
571 break;
572 case WINED3DDECLTYPE_FLOAT3:
573 GL_EXTCALL(glVertexAttrib3fvARB(index, (float *) ptr));
574 break;
575 case WINED3DDECLTYPE_FLOAT4:
576 GL_EXTCALL(glVertexAttrib4fvARB(index, (float *) ptr));
577 break;
579 case WINED3DDECLTYPE_UBYTE4:
580 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
581 break;
582 case WINED3DDECLTYPE_UBYTE4N:
583 case WINED3DDECLTYPE_D3DCOLOR:
584 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
585 break;
587 case WINED3DDECLTYPE_SHORT2:
588 GL_EXTCALL(glVertexAttrib4svARB(index, (GLshort *) ptr));
589 break;
590 case WINED3DDECLTYPE_SHORT4:
591 GL_EXTCALL(glVertexAttrib4svARB(index, (GLshort *) ptr));
592 break;
594 case WINED3DDECLTYPE_SHORT2N:
596 GLshort s[4] = {((short *) ptr)[0], ((short *) ptr)[1], 0, 1};
597 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
598 break;
600 case WINED3DDECLTYPE_USHORT2N:
602 GLushort s[4] = {((unsigned short *) ptr)[0], ((unsigned short *) ptr)[1], 0, 1};
603 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
604 break;
606 case WINED3DDECLTYPE_SHORT4N:
607 GL_EXTCALL(glVertexAttrib4NsvARB(index, (GLshort *) ptr));
608 break;
609 case WINED3DDECLTYPE_USHORT4N:
610 GL_EXTCALL(glVertexAttrib4NusvARB(index, (GLushort *) ptr));
611 break;
613 case WINED3DDECLTYPE_UDEC3:
614 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
615 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
616 break;
617 case WINED3DDECLTYPE_DEC3N:
618 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
619 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
620 break;
622 case WINED3DDECLTYPE_FLOAT16_2:
623 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
624 * byte float according to the IEEE standard
626 if (GL_SUPPORT(NV_HALF_FLOAT)) {
627 GL_EXTCALL(glVertexAttrib2hvNV(index, (GLhalfNV *)ptr));
628 } else {
629 float x = float_16_to_32(((unsigned short *) ptr) + 0);
630 float y = float_16_to_32(((unsigned short *) ptr) + 1);
631 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
633 break;
634 case WINED3DDECLTYPE_FLOAT16_4:
635 if (GL_SUPPORT(NV_HALF_FLOAT)) {
636 GL_EXTCALL(glVertexAttrib4hvNV(index, (GLhalfNV *)ptr));
637 } else {
638 float x = float_16_to_32(((unsigned short *) ptr) + 0);
639 float y = float_16_to_32(((unsigned short *) ptr) + 1);
640 float z = float_16_to_32(((unsigned short *) ptr) + 2);
641 float w = float_16_to_32(((unsigned short *) ptr) + 3);
642 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
644 break;
646 case WINED3DDECLTYPE_UNUSED:
647 default:
648 ERR("Unexpected attribute declaration: %d\n", type);
649 break;
653 static void drawStridedSlowVs(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
654 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx,
655 ULONG startVertex) {
657 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
658 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
659 const WORD *pIdxBufS = NULL;
660 const DWORD *pIdxBufL = NULL;
661 LONG vx_index;
662 int i;
663 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
664 BYTE *ptr;
666 if (idxSize != 0) {
667 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
668 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
669 * idxData will be != NULL
671 if(idxData == NULL) {
672 idxData = ((IWineD3DIndexBufferImpl *) stateblock->pIndexData)->resource.allocatedMemory;
675 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
676 else pIdxBufL = (const DWORD *) idxData;
679 /* Start drawing in GL */
680 VTRACE(("glBegin(%x)\n", glPrimitiveType));
681 glBegin(glPrimitiveType);
683 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
684 if (idxData != NULL) {
686 /* Indexed so work out the number of strides to skip */
687 if (idxSize == 2) {
688 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
689 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
690 } else {
691 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
692 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
696 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
697 if(!sd->u.input[i].lpData) continue;
699 ptr = sd->u.input[i].lpData +
700 sd->u.input[i].dwStride * SkipnStrides +
701 stateblock->streamOffset[sd->u.input[i].streamNo];
703 send_attribute(This, sd->u.input[i].dwType, i, ptr);
705 SkipnStrides++;
708 glEnd();
711 void depth_blt(IWineD3DDevice *iface, GLuint texture, GLsizei w, GLsizei h) {
712 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
713 GLint old_binding = 0;
715 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
717 glDisable(GL_CULL_FACE);
718 glEnable(GL_BLEND);
719 glDisable(GL_ALPHA_TEST);
720 glDisable(GL_SCISSOR_TEST);
721 glDisable(GL_STENCIL_TEST);
722 glEnable(GL_DEPTH_TEST);
723 glDepthFunc(GL_ALWAYS);
724 glDepthMask(GL_TRUE);
725 glBlendFunc(GL_ZERO, GL_ONE);
726 glViewport(0, 0, w, h);
728 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
729 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
730 glBindTexture(GL_TEXTURE_2D, texture);
731 glEnable(GL_TEXTURE_2D);
733 This->shader_backend->shader_select_depth_blt(iface);
735 glBegin(GL_TRIANGLE_STRIP);
736 glVertex2f(-1.0f, -1.0f);
737 glVertex2f(1.0f, -1.0f);
738 glVertex2f(-1.0f, 1.0f);
739 glVertex2f(1.0f, 1.0f);
740 glEnd();
742 glBindTexture(GL_TEXTURE_2D, old_binding);
744 glPopAttrib();
746 This->shader_backend->shader_deselect_depth_blt(iface);
749 static inline void drawStridedInstanced(IWineD3DDevice *iface, WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
750 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
751 ULONG startIdx, ULONG startVertex) {
752 UINT numInstances = 0;
753 int numInstancedAttribs = 0, i, j;
754 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
755 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
756 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
758 if (idxSize == 0) {
759 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
760 * We don't support this for now
762 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
763 * But the StreamSourceFreq value has a different meaning in that situation.
765 FIXME("Non-indexed instanced drawing is not supported\n");
766 return;
769 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
770 idxData = idxData == (void *)-1 ? NULL : idxData;
772 /* First, figure out how many instances we have to draw */
773 for(i = 0; i < MAX_STREAMS; i++) {
774 /* Look at the streams and take the first one which matches */
775 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
776 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
777 if(stateblock->streamFreq[i] == 0){
778 numInstances = 1;
779 } else {
780 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
782 break; /* break, because only the first suitable value is interesting */
786 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
787 if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
788 instancedData[numInstancedAttribs] = i;
789 numInstancedAttribs++;
793 /* now draw numInstances instances :-) */
794 for(i = 0; i < numInstances; i++) {
795 /* Specify the instanced attributes using immediate mode calls */
796 for(j = 0; j < numInstancedAttribs; j++) {
797 BYTE *ptr = sd->u.input[instancedData[j]].lpData +
798 sd->u.input[instancedData[j]].dwStride * i +
799 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
800 if(sd->u.input[instancedData[j]].VBO) {
801 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
802 ptr += (long) vb->resource.allocatedMemory;
805 send_attribute(This, sd->u.input[instancedData[j]].dwType, instancedData[j], ptr);
808 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
809 (const char *)idxData+(idxSize * startIdx));
810 checkGLcall("glDrawElements");
814 static inline void remove_vbos(IWineD3DDeviceImpl *This, WineDirect3DVertexStridedData *s) {
815 unsigned char i;
816 IWineD3DVertexBufferImpl *vb;
818 if(s->u.s.position.VBO) {
819 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position.streamNo];
820 s->u.s.position.VBO = 0;
821 s->u.s.position.lpData = (BYTE *) ((unsigned long) s->u.s.position.lpData + (unsigned long) vb->resource.allocatedMemory);
823 if(s->u.s.blendWeights.VBO) {
824 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendWeights.streamNo];
825 s->u.s.blendWeights.VBO = 0;
826 s->u.s.blendWeights.lpData = (BYTE *) ((unsigned long) s->u.s.blendWeights.lpData + (unsigned long) vb->resource.allocatedMemory);
828 if(s->u.s.blendMatrixIndices.VBO) {
829 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendMatrixIndices.streamNo];
830 s->u.s.blendMatrixIndices.VBO = 0;
831 s->u.s.blendMatrixIndices.lpData = (BYTE *) ((unsigned long) s->u.s.blendMatrixIndices.lpData + (unsigned long) vb->resource.allocatedMemory);
833 if(s->u.s.normal.VBO) {
834 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal.streamNo];
835 s->u.s.normal.VBO = 0;
836 s->u.s.normal.lpData = (BYTE *) ((unsigned long) s->u.s.normal.lpData + (unsigned long) vb->resource.allocatedMemory);
838 if(s->u.s.pSize.VBO) {
839 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.pSize.streamNo];
840 s->u.s.pSize.VBO = 0;
841 s->u.s.pSize.lpData = (BYTE *) ((unsigned long) s->u.s.pSize.lpData + (unsigned long) vb->resource.allocatedMemory);
843 if(s->u.s.diffuse.VBO) {
844 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.diffuse.streamNo];
845 s->u.s.diffuse.VBO = 0;
846 s->u.s.diffuse.lpData = (BYTE *) ((unsigned long) s->u.s.diffuse.lpData + (unsigned long) vb->resource.allocatedMemory);
848 if(s->u.s.specular.VBO) {
849 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.specular.streamNo];
850 s->u.s.specular.VBO = 0;
851 s->u.s.specular.lpData = (BYTE *) ((unsigned long) s->u.s.specular.lpData + (unsigned long) vb->resource.allocatedMemory);
853 for(i = 0; i < WINED3DDP_MAXTEXCOORD; i++) {
854 if(s->u.s.texCoords[i].VBO) {
855 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.texCoords[i].streamNo];
856 s->u.s.texCoords[i].VBO = 0;
857 s->u.s.texCoords[i].lpData = (BYTE *) ((unsigned long) s->u.s.texCoords[i].lpData + (unsigned long) vb->resource.allocatedMemory);
860 if(s->u.s.position2.VBO) {
861 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position2.streamNo];
862 s->u.s.position2.VBO = 0;
863 s->u.s.position2.lpData = (BYTE *) ((unsigned long) s->u.s.position2.lpData + (unsigned long) vb->resource.allocatedMemory);
865 if(s->u.s.normal2.VBO) {
866 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal2.streamNo];
867 s->u.s.normal2.VBO = 0;
868 s->u.s.normal2.lpData = (BYTE *) ((unsigned long) s->u.s.normal2.lpData + (unsigned long) vb->resource.allocatedMemory);
870 if(s->u.s.tangent.VBO) {
871 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tangent.streamNo];
872 s->u.s.tangent.VBO = 0;
873 s->u.s.tangent.lpData = (BYTE *) ((unsigned long) s->u.s.tangent.lpData + (unsigned long) vb->resource.allocatedMemory);
875 if(s->u.s.binormal.VBO) {
876 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.binormal.streamNo];
877 s->u.s.binormal.VBO = 0;
878 s->u.s.binormal.lpData = (BYTE *) ((unsigned long) s->u.s.binormal.lpData + (unsigned long) vb->resource.allocatedMemory);
880 if(s->u.s.tessFactor.VBO) {
881 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tessFactor.streamNo];
882 s->u.s.tessFactor.VBO = 0;
883 s->u.s.tessFactor.lpData = (BYTE *) ((unsigned long) s->u.s.tessFactor.lpData + (unsigned long) vb->resource.allocatedMemory);
885 if(s->u.s.fog.VBO) {
886 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.fog.streamNo];
887 s->u.s.fog.VBO = 0;
888 s->u.s.fog.lpData = (BYTE *) ((unsigned long) s->u.s.fog.lpData + (unsigned long) vb->resource.allocatedMemory);
890 if(s->u.s.depth.VBO) {
891 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.depth.streamNo];
892 s->u.s.depth.VBO = 0;
893 s->u.s.depth.lpData = (BYTE *) ((unsigned long) s->u.s.depth.lpData + (unsigned long) vb->resource.allocatedMemory);
895 if(s->u.s.sample.VBO) {
896 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.sample.streamNo];
897 s->u.s.sample.VBO = 0;
898 s->u.s.sample.lpData = (BYTE *) ((unsigned long) s->u.s.sample.lpData + (unsigned long) vb->resource.allocatedMemory);
902 /* Routine common to the draw primitive and draw indexed primitive routines */
903 void drawPrimitive(IWineD3DDevice *iface,
904 int PrimitiveType,
905 long NumPrimitives,
906 /* for Indexed: */
907 long StartVertexIndex,
908 UINT numberOfVertices,
909 long StartIdx,
910 short idxSize,
911 const void *idxData,
912 int minIndex) {
914 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
915 IWineD3DSurfaceImpl *target;
916 int i;
918 if (NumPrimitives == 0) return;
920 /* Invalidate the back buffer memory so LockRect will read it the next time */
921 for(i = 0; i < GL_LIMITS(buffers); i++) {
922 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
923 if (target) {
924 IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
925 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
929 /* Signals other modules that a drawing is in progress and the stateblock finalized */
930 This->isInDraw = TRUE;
932 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
934 if (This->stencilBufferTarget) {
935 /* Note that this depends on the ActivateContext call above to set
936 * This->render_offscreen properly */
937 DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
938 surface_load_ds_location(This->stencilBufferTarget, location);
939 surface_modify_ds_location(This->stencilBufferTarget, location);
942 /* Ok, we will be updating the screen from here onwards so grab the lock */
943 ENTER_GL();
945 GLenum glPrimType;
946 BOOL emulation = FALSE;
947 WineDirect3DVertexStridedData *strided = &This->strided_streams;
948 WineDirect3DVertexStridedData stridedlcl;
949 /* Ok, Work out which primitive is requested and how many vertexes that
950 will be */
951 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
952 if (numberOfVertices == 0 )
953 numberOfVertices = calculatedNumberOfindices;
955 if(!use_vs(This)) {
956 if(!This->strided_streams.u.s.position_transformed && This->activeContext->num_untracked_materials &&
957 This->stateBlock->renderState[WINED3DRS_LIGHTING]) {
958 static BOOL first = TRUE;
959 if(first) {
960 FIXME("Using software emulation because not all material properties could be tracked\n");
961 first = FALSE;
962 } else {
963 TRACE("Using software emulation because not all material properties could be tracked\n");
965 emulation = TRUE;
967 else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
968 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
969 * to a float in the vertex buffer
971 static BOOL first = TRUE;
972 if(first) {
973 FIXME("Using software emulation because manual fog coordinates are provided\n");
974 first = FALSE;
975 } else {
976 TRACE("Using software emulation because manual fog coordinates are provided\n");
978 emulation = TRUE;
981 if(emulation) {
982 strided = &stridedlcl;
983 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
984 remove_vbos(This, &stridedlcl);
988 if (This->useDrawStridedSlow || emulation) {
989 /* Immediate mode drawing */
990 if(use_vs(This)) {
991 static BOOL first = TRUE;
992 if(first) {
993 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
994 first = FALSE;
995 } else {
996 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
998 drawStridedSlowVs(iface, strided, calculatedNumberOfindices, glPrimType,
999 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1000 } else {
1001 drawStridedSlow(iface, strided, calculatedNumberOfindices,
1002 glPrimType, idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1004 } else if(This->instancedDraw) {
1005 /* Instancing emulation with mixing immediate mode and arrays */
1006 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
1007 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1008 } else {
1009 drawStridedFast(iface, calculatedNumberOfindices, glPrimType,
1010 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
1014 /* Finished updating the screen, restore lock */
1015 LEAVE_GL();
1016 TRACE("Done all gl drawing\n");
1018 /* Diagnostics */
1019 #ifdef SHOW_FRAME_MAKEUP
1021 static long int primCounter = 0;
1022 /* NOTE: set primCounter to the value reported by drawprim
1023 before you want to to write frame makeup to /tmp */
1024 if (primCounter >= 0) {
1025 WINED3DLOCKED_RECT r;
1026 char buffer[80];
1027 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
1028 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
1029 TRACE("Saving screenshot %s\n", buffer);
1030 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
1031 IWineD3DSurface_UnlockRect(This->render_targets[0]);
1033 #ifdef SHOW_TEXTURE_MAKEUP
1035 IWineD3DSurface *pSur;
1036 int textureNo;
1037 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
1038 if (This->stateBlock->textures[textureNo] != NULL) {
1039 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
1040 TRACE("Saving texture %s\n", buffer);
1041 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
1042 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
1043 IWineD3DSurface_SaveSnapshot(pSur, buffer);
1044 IWineD3DSurface_Release(pSur);
1045 } else {
1046 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
1051 #endif
1053 TRACE("drawprim #%ld\n", primCounter);
1054 ++primCounter;
1056 #endif
1058 /* Control goes back to the device, stateblock values may change again */
1059 This->isInDraw = FALSE;
1062 static void normalize_normal(float *n) {
1063 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
1064 if(length == 0.0) return;
1065 length = sqrt(length);
1066 n[0] = n[0] / length;
1067 n[1] = n[1] / length;
1068 n[2] = n[2] / length;
1071 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
1073 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
1074 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
1075 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
1076 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
1077 * in drawprim.
1079 * To read back, the opengl feedback mode is used. This creates a problem because we want
1080 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
1081 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
1082 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
1083 * them to [-1.0;+1.0] and set the viewport up to scale them back.
1085 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
1086 * resulting colors back to the normals.
1088 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
1089 * does not restore it because normally a draw follows immediately afterwards. The caller is
1090 * responsible of taking care that either the gl states are restored, or the context activated
1091 * for drawing to reset the lastWasBlit flag.
1093 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
1094 struct WineD3DRectPatch *patch) {
1095 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
1096 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
1097 WineDirect3DVertexStridedData strided;
1098 BYTE *data;
1099 WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
1100 DWORD vtxStride;
1101 GLenum feedback_type;
1102 GLfloat *feedbuffer;
1104 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1105 * Beware of vbos
1107 memset(&strided, 0, sizeof(strided));
1108 primitiveDeclarationConvertToStridedData((IWineD3DDevice *) This, FALSE, &strided, NULL);
1109 if(strided.u.s.position.VBO) {
1110 IWineD3DVertexBufferImpl *vb;
1111 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[strided.u.s.position.streamNo];
1112 strided.u.s.position.lpData = (BYTE *) ((unsigned long) strided.u.s.position.lpData +
1113 (unsigned long) vb->resource.allocatedMemory);
1115 vtxStride = strided.u.s.position.dwStride;
1116 data = strided.u.s.position.lpData +
1117 vtxStride * info->Stride * info->StartVertexOffsetHeight +
1118 vtxStride * info->StartVertexOffsetWidth;
1120 /* Not entirely sure about what happens with transformed vertices */
1121 if(strided.u.s.position_transformed) {
1122 FIXME("Transformed position in rectpatch generation\n");
1124 if(vtxStride % sizeof(GLfloat)) {
1125 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1126 * I don't see how the stride could not be a multiple of 4, but make sure
1127 * to check it
1129 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1131 if(info->Basis != WINED3DBASIS_BEZIER) {
1132 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
1134 if(info->Degree != WINED3DDEGREE_CUBIC) {
1135 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
1138 /* First, get the boundary cube of the input data */
1139 for(j = 0; j < info->Height; j++) {
1140 for(i = 0; i < info->Width; i++) {
1141 float *v = (float *) (data + vtxStride * i + vtxStride * info->Stride * j);
1142 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
1143 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
1144 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
1145 if(v[2] < neg_z) neg_z = v[2];
1149 /* This needs some improvements in the vertex decl code */
1150 FIXME("Cannot find data to generate. Only generating position and normals\n");
1151 patch->has_normals = TRUE;
1152 patch->has_texcoords = FALSE;
1154 /* Simply activate the context for blitting. This disables all the things we don't want and
1155 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
1156 * patch (as opposed to normal draws) will most likely need different changes anyway
1158 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
1159 ENTER_GL();
1161 glMatrixMode(GL_PROJECTION);
1162 checkGLcall("glMatrixMode(GL_PROJECTION)");
1163 glLoadIdentity();
1164 checkGLcall("glLoadIndentity()");
1165 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
1166 glTranslatef(0, 0, 0.5);
1167 checkGLcall("glScalef");
1168 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
1169 checkGLcall("glViewport");
1171 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1172 * our feedback buffer parser
1174 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1175 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1176 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
1177 if(patch->has_normals) {
1178 float black[4] = {0, 0, 0, 0};
1179 float red[4] = {1, 0, 0, 0};
1180 float green[4] = {0, 1, 0, 0};
1181 float blue[4] = {0, 0, 1, 0};
1182 float white[4] = {1, 1, 1, 1};
1183 glEnable(GL_LIGHTING);
1184 checkGLcall("glEnable(GL_LIGHTING)");
1185 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
1186 checkGLcall("glLightModel for MODEL_AMBIENT");
1187 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
1189 for(i = 3; i < GL_LIMITS(lights); i++) {
1190 glDisable(GL_LIGHT0 + i);
1191 checkGLcall("glDisable(GL_LIGHT0 + i)");
1192 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
1195 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
1196 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
1197 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
1198 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
1199 glLightfv(GL_LIGHT0, GL_POSITION, red);
1200 glEnable(GL_LIGHT0);
1201 checkGLcall("Setting up light 1\n");
1202 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
1203 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
1204 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
1205 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
1206 glLightfv(GL_LIGHT1, GL_POSITION, green);
1207 glEnable(GL_LIGHT1);
1208 checkGLcall("Setting up light 2\n");
1209 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
1210 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
1211 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
1212 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
1213 glLightfv(GL_LIGHT2, GL_POSITION, blue);
1214 glEnable(GL_LIGHT2);
1215 checkGLcall("Setting up light 3\n");
1217 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
1218 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
1219 glDisable(GL_COLOR_MATERIAL);
1220 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
1221 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
1222 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
1223 checkGLcall("Setting up materials\n");
1226 /* Enable the needed maps.
1227 * GL_MAP2_VERTEX_3 is needed for positional data.
1228 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1229 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1231 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
1232 out_vertex_size = 3 /* position */;
1233 d3d_out_vertex_size = 3;
1234 glEnable(GL_MAP2_VERTEX_3);
1235 if(patch->has_normals && patch->has_texcoords) {
1236 FIXME("Texcoords not handled yet\n");
1237 feedback_type = GL_3D_COLOR_TEXTURE;
1238 out_vertex_size += 8;
1239 d3d_out_vertex_size += 7;
1240 glEnable(GL_AUTO_NORMAL);
1241 glEnable(GL_MAP2_TEXTURE_COORD_4);
1242 } else if(patch->has_texcoords) {
1243 FIXME("Texcoords not handled yet\n");
1244 feedback_type = GL_3D_COLOR_TEXTURE;
1245 out_vertex_size += 7;
1246 d3d_out_vertex_size += 4;
1247 glEnable(GL_MAP2_TEXTURE_COORD_4);
1248 } else if(patch->has_normals) {
1249 feedback_type = GL_3D_COLOR;
1250 out_vertex_size += 4;
1251 d3d_out_vertex_size += 3;
1252 glEnable(GL_AUTO_NORMAL);
1253 } else {
1254 feedback_type = GL_3D;
1256 checkGLcall("glEnable vertex attrib generation");
1258 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
1259 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
1260 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
1262 glMap2f(GL_MAP2_VERTEX_3,
1263 0, 1, vtxStride / sizeof(float), info->Width,
1264 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1265 (float *) data);
1266 checkGLcall("glMap2f");
1267 if(patch->has_texcoords) {
1268 glMap2f(GL_MAP2_TEXTURE_COORD_4,
1269 0, 1, vtxStride / sizeof(float), info->Width,
1270 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1271 (float *) data);
1272 checkGLcall("glMap2f");
1274 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
1275 checkGLcall("glMapGrid2f");
1277 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
1278 checkGLcall("glFeedbackBuffer");
1279 glRenderMode(GL_FEEDBACK);
1281 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1282 checkGLcall("glEvalMesh2\n");
1284 i = glRenderMode(GL_RENDER);
1285 if(i == -1) {
1286 LEAVE_GL();
1287 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
1288 Sleep(10000);
1289 HeapFree(GetProcessHeap(), 0, feedbuffer);
1290 return WINED3DERR_DRIVERINTERNALERROR;
1291 } else if(i != buffer_size) {
1292 LEAVE_GL();
1293 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1294 Sleep(10000);
1295 HeapFree(GetProcessHeap(), 0, feedbuffer);
1296 return WINED3DERR_DRIVERINTERNALERROR;
1297 } else {
1298 TRACE("Got %d elements as expected\n", i);
1301 HeapFree(GetProcessHeap(), 0, patch->mem);
1302 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1303 i = 0;
1304 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1305 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1306 ERR("Unexpected token: %f\n", feedbuffer[j]);
1307 continue;
1309 if(feedbuffer[j + 1] != 3) {
1310 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1311 continue;
1313 /* Somehow there are different ideas about back / front facing, so fix up the
1314 * vertex order
1316 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1317 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1318 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
1319 if(patch->has_normals) {
1320 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1321 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1322 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1324 i += d3d_out_vertex_size;
1326 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1327 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1328 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
1329 if(patch->has_normals) {
1330 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1331 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1332 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1334 i += d3d_out_vertex_size;
1336 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1337 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1338 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
1339 if(patch->has_normals) {
1340 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1341 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1342 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1344 i += d3d_out_vertex_size;
1347 if(patch->has_normals) {
1348 /* Now do the same with reverse light directions */
1349 float x[4] = {-1, 0, 0, 0};
1350 float y[4] = { 0, -1, 0, 0};
1351 float z[4] = { 0, 0, -1, 0};
1352 glLightfv(GL_LIGHT0, GL_POSITION, x);
1353 glLightfv(GL_LIGHT1, GL_POSITION, y);
1354 glLightfv(GL_LIGHT2, GL_POSITION, z);
1355 checkGLcall("Setting up reverse light directions\n");
1357 glRenderMode(GL_FEEDBACK);
1358 checkGLcall("glRenderMode(GL_FEEDBACK)");
1359 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1360 checkGLcall("glEvalMesh2\n");
1361 i = glRenderMode(GL_RENDER);
1362 checkGLcall("glRenderMode(GL_RENDER)");
1364 i = 0;
1365 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1366 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1367 ERR("Unexpected token: %f\n", feedbuffer[j]);
1368 continue;
1370 if(feedbuffer[j + 1] != 3) {
1371 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1372 continue;
1374 if(patch->mem[i + 3] == 0.0)
1375 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1376 if(patch->mem[i + 4] == 0.0)
1377 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1378 if(patch->mem[i + 5] == 0.0)
1379 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1380 normalize_normal(patch->mem + i + 3);
1381 i += d3d_out_vertex_size;
1383 if(patch->mem[i + 3] == 0.0)
1384 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1385 if(patch->mem[i + 4] == 0.0)
1386 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1387 if(patch->mem[i + 5] == 0.0)
1388 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1389 normalize_normal(patch->mem + i + 3);
1390 i += d3d_out_vertex_size;
1392 if(patch->mem[i + 3] == 0.0)
1393 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1394 if(patch->mem[i + 4] == 0.0)
1395 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1396 if(patch->mem[i + 5] == 0.0)
1397 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1398 normalize_normal(patch->mem + i + 3);
1399 i += d3d_out_vertex_size;
1403 glDisable(GL_MAP2_VERTEX_3);
1404 glDisable(GL_AUTO_NORMAL);
1405 glDisable(GL_MAP2_NORMAL);
1406 glDisable(GL_MAP2_TEXTURE_COORD_4);
1407 checkGLcall("glDisable vertex attrib generation");
1408 LEAVE_GL();
1410 HeapFree(GetProcessHeap(), 0, feedbuffer);
1412 vtxStride = 3 * sizeof(float);
1413 if(patch->has_normals) {
1414 vtxStride += 3 * sizeof(float);
1416 if(patch->has_texcoords) {
1417 vtxStride += 4 * sizeof(float);
1419 memset(&patch->strided, 0, sizeof(&patch->strided));
1420 patch->strided.u.s.position.lpData = (BYTE *) patch->mem;
1421 patch->strided.u.s.position.dwStride = vtxStride;
1422 patch->strided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
1423 patch->strided.u.s.position.streamNo = 255;
1425 if(patch->has_normals) {
1426 patch->strided.u.s.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1427 patch->strided.u.s.normal.dwStride = vtxStride;
1428 patch->strided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
1429 patch->strided.u.s.normal.streamNo = 255;
1431 if(patch->has_texcoords) {
1432 patch->strided.u.s.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1433 if(patch->has_normals) {
1434 patch->strided.u.s.texCoords[0].lpData += 3 * sizeof(float);
1436 patch->strided.u.s.texCoords[0].dwStride = vtxStride;
1437 patch->strided.u.s.texCoords[0].dwType = WINED3DDECLTYPE_FLOAT4;
1438 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1439 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1440 * application.
1442 patch->strided.u.s.texCoords[0].streamNo = MAX_STREAMS;
1445 return WINED3D_OK;