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
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
30 #define GLINFO_LOCATION This->adapter->gl_info
35 /* Issues the glBegin call for gl given the primitive type and count */
36 static DWORD primitiveToGl(WINED3DPRIMITIVETYPE PrimitiveType,
40 DWORD NumVertexes = NumPrimitives;
42 switch (PrimitiveType) {
43 case WINED3DPT_POINTLIST:
45 *primType = GL_POINTS;
46 NumVertexes = NumPrimitives;
49 case WINED3DPT_LINELIST:
52 NumVertexes = NumPrimitives * 2;
55 case WINED3DPT_LINESTRIP:
56 TRACE("LINE_STRIP\n");
57 *primType = GL_LINE_STRIP;
58 NumVertexes = NumPrimitives + 1;
61 case WINED3DPT_TRIANGLELIST:
63 *primType = GL_TRIANGLES;
64 NumVertexes = NumPrimitives * 3;
67 case WINED3DPT_TRIANGLESTRIP:
68 TRACE("TRIANGLE_STRIP\n");
69 *primType = GL_TRIANGLE_STRIP;
70 NumVertexes = NumPrimitives + 2;
73 case WINED3DPT_TRIANGLEFAN:
74 TRACE("TRIANGLE_FAN\n");
75 *primType = GL_TRIANGLE_FAN;
76 NumVertexes = NumPrimitives + 2;
80 FIXME("Unhandled primitive\n");
81 *primType = GL_POINTS;
87 static BOOL fixed_get_input(
88 BYTE usage, BYTE usage_idx,
89 unsigned int* regnum) {
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)
98 else if (usage == WINED3DDECLUSAGE_BLENDWEIGHT && usage_idx == 0)
100 else if (usage == WINED3DDECLUSAGE_BLENDINDICES && usage_idx == 0)
102 else if (usage == WINED3DDECLUSAGE_NORMAL && usage_idx == 0)
104 else if (usage == WINED3DDECLUSAGE_PSIZE && usage_idx == 0)
106 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 0)
108 else if (usage == WINED3DDECLUSAGE_COLOR && usage_idx == 1)
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;
130 FIXME("Unsupported input stream [usage=%s, usage_idx=%u]\n",
131 debug_d3ddeclusage(usage), usage_idx);
137 void primitiveDeclarationConvertToStridedData(
138 IWineD3DDevice *iface,
139 BOOL useVertexShaderFunction,
140 WineDirect3DVertexStridedData *strided,
143 /* We need to deal with frequency data!*/
145 const BYTE *data = NULL;
146 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
147 IWineD3DVertexDeclarationImpl* vertexDeclaration = (IWineD3DVertexDeclarationImpl *)This->stateBlock->vertexDecl;
149 const WINED3DVERTEXELEMENT *element;
151 DWORD numPreloadStreams = This->stateBlock->streamIsUP ? 0 : vertexDeclaration->num_streams;
152 const 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) {
166 element = vertexDeclaration->pDeclarationWine + i;
167 TRACE("%p Element %p (%u of %u)\n", vertexDeclaration->pDeclarationWine,
168 element, i + 1, vertexDeclaration->declarationWNumElements - 1);
170 if (This->stateBlock->streamSource[element->Stream] == NULL)
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]);
177 data = (BYTE *)This->stateBlock->streamSource[element->Stream];
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);
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");
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;
204 /* This may be bad with the fixed function pipeline */
205 FIXME("Missing vbo streams with unfixed colors or transformed position, expect problems\n");
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);
219 stride_used = fixed_get_input(element->Usage, element->UsageIndex, &idx);
222 TRACE("Load %s array %u [usage=%s, usage_idx=%u, "
223 "stream=%u, offset=%u, stride=%u, type=%s, VBO=%u]\n",
224 useVertexShaderFunction? "shader": "fixed function", idx,
225 debug_d3ddeclusage(element->Usage), element->UsageIndex,
226 element->Stream, element->Offset, stride, debug_d3ddecltype(element->Type), streamVBO);
228 if (!useVertexShaderFunction && !vertexDeclaration->ffp_valid[i]) {
229 WARN("Skipping unsupported fixed function element of type %s and usage %s\n",
230 debug_d3ddecltype(element->Type), debug_d3ddeclusage(element->Usage));
232 strided->u.input[idx].lpData = data;
233 strided->u.input[idx].dwType = element->Type;
234 strided->u.input[idx].dwStride = stride;
235 strided->u.input[idx].VBO = streamVBO;
236 strided->u.input[idx].streamNo = element->Stream;
240 /* Now call PreLoad on all the vertex buffers. In the very rare case
241 * that the buffers stopps converting PreLoad will dirtify the VDECL again.
242 * The vertex buffer can now use the strided structure in the device instead of finding its
245 * NULL streams won't be recorded in the array, UP streams won't be either. A stream is only
248 for(i=0; i < numPreloadStreams; i++) {
249 IWineD3DVertexBuffer *vb = This->stateBlock->streamSource[streams[i]];
251 IWineD3DVertexBuffer_PreLoad(vb);
256 static void drawStridedFast(IWineD3DDevice *iface,UINT numberOfVertices, GLenum glPrimitiveType,
257 const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex) {
258 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
260 if (idxSize != 0 /* This crashes sometimes!*/) {
261 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
262 idxData = idxData == (void *)-1 ? NULL : idxData;
264 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
265 (const char *)idxData+(idxSize * startIdx));
266 checkGLcall("glDrawElements");
267 #else /* using drawRangeElements may be faster */
269 glDrawRangeElements(glPrimitiveType, minIndex, minIndex + numberOfVertices - 1, numberOfVertices,
270 idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
271 (const char *)idxData+(idxSize * startIdx));
272 checkGLcall("glDrawRangeElements");
276 TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, glPrimitiveType, startVertex, numberOfVertices);
277 glDrawArrays(glPrimitiveType, startVertex, numberOfVertices);
278 checkGLcall("glDrawArrays");
285 * Actually draw using the supplied information.
286 * Slower GL version which extracts info about each vertex in turn
289 static void drawStridedSlow(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd, UINT NumVertexes,
290 GLenum glPrimType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex)
292 unsigned int textureNo = 0;
293 const WORD *pIdxBufS = NULL;
294 const DWORD *pIdxBufL = NULL;
296 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
297 const UINT *streamOffset = This->stateBlock->streamOffset;
298 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
299 BOOL pixelShader = use_ps(This);
301 const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
302 const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
304 TRACE("Using slow vertex array code\n");
306 /* Variable Initialization */
308 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
309 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
310 * idxData will be != NULL
312 if(idxData == NULL) {
313 idxData = ((IWineD3DIndexBufferImpl *) This->stateBlock->pIndexData)->resource.allocatedMemory;
316 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
317 else pIdxBufL = (const DWORD *) idxData;
318 } else if (idxData) {
319 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
323 /* Adding the stream offset once is cheaper than doing it every iteration. Do not modify the strided data, it is a pointer
324 * to the strided Data in the device and might be needed intact on the next draw
326 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
327 if(sd->u.s.texCoords[textureNo].lpData) {
328 texCoords[textureNo] = sd->u.s.texCoords[textureNo].lpData + streamOffset[sd->u.s.texCoords[textureNo].streamNo];
330 texCoords[textureNo] = NULL;
333 if(sd->u.s.diffuse.lpData) {
334 diffuse = sd->u.s.diffuse.lpData + streamOffset[sd->u.s.diffuse.streamNo];
336 if(sd->u.s.specular.lpData) {
337 specular = sd->u.s.specular.lpData + streamOffset[sd->u.s.specular.streamNo];
339 if(sd->u.s.normal.lpData) {
340 normal = sd->u.s.normal.lpData + streamOffset[sd->u.s.normal.streamNo];
342 if(sd->u.s.position.lpData) {
343 position = sd->u.s.position.lpData + streamOffset[sd->u.s.position.streamNo];
346 if(FIXME_ON(d3d_draw)) {
347 if(specular && This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
348 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position_transformed )&&
349 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
350 if(GL_SUPPORT(EXT_FOG_COORD) && sd->u.s.specular.dwType != WINED3DDECLTYPE_D3DCOLOR) {
351 FIXME("Implement fog coordinates from %s\n", debug_d3ddecltype(sd->u.s.specular.dwType));
354 if(This->activeContext->num_untracked_materials && sd->u.s.diffuse.dwType != WINED3DDECLTYPE_D3DCOLOR) {
355 FIXME("Implement diffuse color tracking from %s\n", debug_d3ddecltype(sd->u.s.diffuse.dwType));
359 /* Start drawing in GL */
360 VTRACE(("glBegin(%x)\n", glPrimType));
363 /* Default settings for data that is not passed */
364 if (sd->u.s.normal.lpData == NULL) {
367 if(sd->u.s.diffuse.lpData == NULL) {
368 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
370 if(sd->u.s.specular.lpData == NULL) {
371 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
372 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
376 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
377 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
380 /* For each primitive */
381 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
382 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
383 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
386 /* For indexed data, we need to go a few more strides in */
387 if (idxData != NULL) {
389 /* Indexed so work out the number of strides to skip */
391 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufS[startIdx+vx_index]));
392 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
394 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufL[startIdx+vx_index]));
395 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
399 /* Texture coords --------------------------- */
400 for (textureNo = 0; textureNo < GL_LIMITS(texture_stages); ++textureNo) {
402 if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0) {
403 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
407 /* Query tex coords */
408 if (This->stateBlock->textures[textureNo] != NULL || pixelShader) {
409 int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
410 int texture_idx = This->texUnitMap[textureNo];
411 const void *ptrToCoords;
414 VTRACE(("tex: %d - Skip tex coords, as being system generated\n", textureNo));
416 } else if (coordIdx < 0) {
417 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
421 if (texture_idx == -1) continue;
423 if (texCoords[coordIdx] == NULL) {
424 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
425 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
426 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
428 glTexCoord4f(0, 0, 0, 1);
433 ptrToCoords = texCoords[coordIdx] + (SkipnStrides * sd->u.s.texCoords[coordIdx].dwStride);
434 if (GL_SUPPORT(ARB_MULTITEXTURE))
435 multi_texcoord_funcs[sd->u.s.texCoords[coordIdx].dwType](GL_TEXTURE0_ARB + texture_idx, ptrToCoords);
437 texcoord_funcs[sd->u.s.texCoords[coordIdx].dwType](ptrToCoords);
439 } /* End of textures */
441 /* Diffuse -------------------------------- */
443 const void *ptrToCoords = diffuse + SkipnStrides * sd->u.s.diffuse.dwStride;
445 diffuse_funcs[sd->u.s.diffuse.dwType](ptrToCoords);
446 if(This->activeContext->num_untracked_materials) {
447 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
451 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
452 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
453 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
454 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
456 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
457 glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
462 /* Specular ------------------------------- */
464 const void *ptrToCoords = specular + SkipnStrides * sd->u.s.specular.dwStride;
466 /* special case where the fog density is stored in the specular alpha channel */
467 if(This->stateBlock->renderState[WINED3DRS_FOGENABLE] &&
468 (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE || sd->u.s.position.dwType == WINED3DDECLTYPE_FLOAT4 )&&
469 This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE) {
470 if(GL_SUPPORT(EXT_FOG_COORD)) {
471 DWORD specularColor = ((const DWORD *)ptrToCoords)[0];
472 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
474 static BOOL warned = FALSE;
476 /* TODO: Use the fog table code from old ddraw */
477 FIXME("Implement fog for transformed vertices in software\n");
483 specular_funcs[sd->u.s.specular.dwType](ptrToCoords);
486 /* Normal -------------------------------- */
487 if (normal != NULL) {
488 const void *ptrToCoords = normal + SkipnStrides * sd->u.s.normal.dwStride;
489 normal_funcs[sd->u.s.normal.dwType](ptrToCoords);
492 /* Position -------------------------------- */
494 const void *ptrToCoords = position + SkipnStrides * sd->u.s.position.dwStride;
495 position_funcs[sd->u.s.position.dwType](ptrToCoords);
498 /* For non indexed mode, step onto next parts */
499 if (idxData == NULL) {
505 checkGLcall("glEnd and previous calls");
508 static inline void send_attribute(IWineD3DDeviceImpl *This, const DWORD type, const UINT index, const void *ptr) {
510 case WINED3DDECLTYPE_FLOAT1:
511 GL_EXTCALL(glVertexAttrib1fvARB(index, (const float *)ptr));
513 case WINED3DDECLTYPE_FLOAT2:
514 GL_EXTCALL(glVertexAttrib2fvARB(index, (const float *)ptr));
516 case WINED3DDECLTYPE_FLOAT3:
517 GL_EXTCALL(glVertexAttrib3fvARB(index, (const float *)ptr));
519 case WINED3DDECLTYPE_FLOAT4:
520 GL_EXTCALL(glVertexAttrib4fvARB(index, (const float *)ptr));
523 case WINED3DDECLTYPE_UBYTE4:
524 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
526 case WINED3DDECLTYPE_UBYTE4N:
527 case WINED3DDECLTYPE_D3DCOLOR:
528 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
531 case WINED3DDECLTYPE_SHORT2:
532 GL_EXTCALL(glVertexAttrib4svARB(index, (const GLshort *)ptr));
534 case WINED3DDECLTYPE_SHORT4:
535 GL_EXTCALL(glVertexAttrib4svARB(index, (const GLshort *)ptr));
538 case WINED3DDECLTYPE_SHORT2N:
540 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
541 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
544 case WINED3DDECLTYPE_USHORT2N:
546 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
547 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
550 case WINED3DDECLTYPE_SHORT4N:
551 GL_EXTCALL(glVertexAttrib4NsvARB(index, (const GLshort *)ptr));
553 case WINED3DDECLTYPE_USHORT4N:
554 GL_EXTCALL(glVertexAttrib4NusvARB(index, (const GLushort *)ptr));
557 case WINED3DDECLTYPE_UDEC3:
558 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
559 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
561 case WINED3DDECLTYPE_DEC3N:
562 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
563 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
566 case WINED3DDECLTYPE_FLOAT16_2:
567 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
568 * byte float according to the IEEE standard
570 if (GL_SUPPORT(NV_HALF_FLOAT)) {
571 GL_EXTCALL(glVertexAttrib2hvNV(index, (const GLhalfNV *)ptr));
573 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
574 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
575 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
578 case WINED3DDECLTYPE_FLOAT16_4:
579 if (GL_SUPPORT(NV_HALF_FLOAT)) {
580 GL_EXTCALL(glVertexAttrib4hvNV(index, (const GLhalfNV *)ptr));
582 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
583 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
584 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
585 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
586 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
590 case WINED3DDECLTYPE_UNUSED:
592 ERR("Unexpected attribute declaration: %d\n", type);
597 static void drawStridedSlowVs(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd, UINT numberOfVertices,
598 GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex, ULONG startIdx, ULONG startVertex)
600 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
601 long SkipnStrides = startVertex + This->stateBlock->loadBaseVertexIndex;
602 const WORD *pIdxBufS = NULL;
603 const DWORD *pIdxBufL = NULL;
606 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
610 /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
611 * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
612 * idxData will be != NULL
614 if(idxData == NULL) {
615 idxData = ((IWineD3DIndexBufferImpl *) stateblock->pIndexData)->resource.allocatedMemory;
618 if (idxSize == 2) pIdxBufS = (const WORD *) idxData;
619 else pIdxBufL = (const DWORD *) idxData;
620 } else if (idxData) {
621 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
625 /* Start drawing in GL */
626 VTRACE(("glBegin(%x)\n", glPrimitiveType));
627 glBegin(glPrimitiveType);
629 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
630 if (idxData != NULL) {
632 /* Indexed so work out the number of strides to skip */
634 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
635 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
637 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
638 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
642 for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
643 if(!sd->u.input[i].lpData) continue;
645 ptr = sd->u.input[i].lpData +
646 sd->u.input[i].dwStride * SkipnStrides +
647 stateblock->streamOffset[sd->u.input[i].streamNo];
649 send_attribute(This, sd->u.input[i].dwType, i, ptr);
657 static inline void drawStridedInstanced(IWineD3DDevice *iface, const WineDirect3DVertexStridedData *sd,
658 UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, short idxSize, ULONG minIndex,
659 ULONG startIdx, ULONG startVertex)
661 UINT numInstances = 0, i;
662 int numInstancedAttribs = 0, j;
663 UINT instancedData[sizeof(sd->u.input) / sizeof(sd->u.input[0]) /* 16 */];
664 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
665 IWineD3DStateBlockImpl *stateblock = This->stateBlock;
668 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
669 * We don't support this for now
671 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
672 * But the StreamSourceFreq value has a different meaning in that situation.
674 FIXME("Non-indexed instanced drawing is not supported\n");
678 TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
679 idxData = idxData == (void *)-1 ? NULL : idxData;
681 /* First, figure out how many instances we have to draw */
682 for(i = 0; i < MAX_STREAMS; i++) {
683 /* Look at the streams and take the first one which matches */
684 if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
685 /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
686 if(stateblock->streamFreq[i] == 0){
689 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
691 break; /* break, because only the first suitable value is interesting */
695 for(i = 0; i < sizeof(sd->u.input) / sizeof(sd->u.input[0]); i++) {
696 if(stateblock->streamFlags[sd->u.input[i].streamNo] & WINED3DSTREAMSOURCE_INSTANCEDATA) {
697 instancedData[numInstancedAttribs] = i;
698 numInstancedAttribs++;
702 /* now draw numInstances instances :-) */
703 for(i = 0; i < numInstances; i++) {
704 /* Specify the instanced attributes using immediate mode calls */
705 for(j = 0; j < numInstancedAttribs; j++) {
706 const BYTE *ptr = sd->u.input[instancedData[j]].lpData +
707 sd->u.input[instancedData[j]].dwStride * i +
708 stateblock->streamOffset[sd->u.input[instancedData[j]].streamNo];
709 if(sd->u.input[instancedData[j]].VBO) {
710 IWineD3DVertexBufferImpl *vb = (IWineD3DVertexBufferImpl *) stateblock->streamSource[sd->u.input[instancedData[j]].streamNo];
711 ptr += (long) vb->resource.allocatedMemory;
714 send_attribute(This, sd->u.input[instancedData[j]].dwType, instancedData[j], ptr);
717 glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
718 (const char *)idxData+(idxSize * startIdx));
719 checkGLcall("glDrawElements");
723 static inline void remove_vbos(IWineD3DDeviceImpl *This, WineDirect3DVertexStridedData *s) {
725 IWineD3DVertexBufferImpl *vb;
727 if(s->u.s.position.VBO) {
728 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position.streamNo];
729 s->u.s.position.VBO = 0;
730 s->u.s.position.lpData = (BYTE *) ((unsigned long) s->u.s.position.lpData + (unsigned long) vb->resource.allocatedMemory);
732 if(s->u.s.blendWeights.VBO) {
733 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendWeights.streamNo];
734 s->u.s.blendWeights.VBO = 0;
735 s->u.s.blendWeights.lpData = (BYTE *) ((unsigned long) s->u.s.blendWeights.lpData + (unsigned long) vb->resource.allocatedMemory);
737 if(s->u.s.blendMatrixIndices.VBO) {
738 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.blendMatrixIndices.streamNo];
739 s->u.s.blendMatrixIndices.VBO = 0;
740 s->u.s.blendMatrixIndices.lpData = (BYTE *) ((unsigned long) s->u.s.blendMatrixIndices.lpData + (unsigned long) vb->resource.allocatedMemory);
742 if(s->u.s.normal.VBO) {
743 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal.streamNo];
744 s->u.s.normal.VBO = 0;
745 s->u.s.normal.lpData = (BYTE *) ((unsigned long) s->u.s.normal.lpData + (unsigned long) vb->resource.allocatedMemory);
747 if(s->u.s.pSize.VBO) {
748 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.pSize.streamNo];
749 s->u.s.pSize.VBO = 0;
750 s->u.s.pSize.lpData = (BYTE *) ((unsigned long) s->u.s.pSize.lpData + (unsigned long) vb->resource.allocatedMemory);
752 if(s->u.s.diffuse.VBO) {
753 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.diffuse.streamNo];
754 s->u.s.diffuse.VBO = 0;
755 s->u.s.diffuse.lpData = (BYTE *) ((unsigned long) s->u.s.diffuse.lpData + (unsigned long) vb->resource.allocatedMemory);
757 if(s->u.s.specular.VBO) {
758 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.specular.streamNo];
759 s->u.s.specular.VBO = 0;
760 s->u.s.specular.lpData = (BYTE *) ((unsigned long) s->u.s.specular.lpData + (unsigned long) vb->resource.allocatedMemory);
762 for(i = 0; i < WINED3DDP_MAXTEXCOORD; i++) {
763 if(s->u.s.texCoords[i].VBO) {
764 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.texCoords[i].streamNo];
765 s->u.s.texCoords[i].VBO = 0;
766 s->u.s.texCoords[i].lpData = (BYTE *) ((unsigned long) s->u.s.texCoords[i].lpData + (unsigned long) vb->resource.allocatedMemory);
769 if(s->u.s.position2.VBO) {
770 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.position2.streamNo];
771 s->u.s.position2.VBO = 0;
772 s->u.s.position2.lpData = (BYTE *) ((unsigned long) s->u.s.position2.lpData + (unsigned long) vb->resource.allocatedMemory);
774 if(s->u.s.normal2.VBO) {
775 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.normal2.streamNo];
776 s->u.s.normal2.VBO = 0;
777 s->u.s.normal2.lpData = (BYTE *) ((unsigned long) s->u.s.normal2.lpData + (unsigned long) vb->resource.allocatedMemory);
779 if(s->u.s.tangent.VBO) {
780 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tangent.streamNo];
781 s->u.s.tangent.VBO = 0;
782 s->u.s.tangent.lpData = (BYTE *) ((unsigned long) s->u.s.tangent.lpData + (unsigned long) vb->resource.allocatedMemory);
784 if(s->u.s.binormal.VBO) {
785 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.binormal.streamNo];
786 s->u.s.binormal.VBO = 0;
787 s->u.s.binormal.lpData = (BYTE *) ((unsigned long) s->u.s.binormal.lpData + (unsigned long) vb->resource.allocatedMemory);
789 if(s->u.s.tessFactor.VBO) {
790 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.tessFactor.streamNo];
791 s->u.s.tessFactor.VBO = 0;
792 s->u.s.tessFactor.lpData = (BYTE *) ((unsigned long) s->u.s.tessFactor.lpData + (unsigned long) vb->resource.allocatedMemory);
795 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.fog.streamNo];
797 s->u.s.fog.lpData = (BYTE *) ((unsigned long) s->u.s.fog.lpData + (unsigned long) vb->resource.allocatedMemory);
799 if(s->u.s.depth.VBO) {
800 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.depth.streamNo];
801 s->u.s.depth.VBO = 0;
802 s->u.s.depth.lpData = (BYTE *) ((unsigned long) s->u.s.depth.lpData + (unsigned long) vb->resource.allocatedMemory);
804 if(s->u.s.sample.VBO) {
805 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[s->u.s.sample.streamNo];
806 s->u.s.sample.VBO = 0;
807 s->u.s.sample.lpData = (BYTE *) ((unsigned long) s->u.s.sample.lpData + (unsigned long) vb->resource.allocatedMemory);
811 /* Routine common to the draw primitive and draw indexed primitive routines */
812 void drawPrimitive(IWineD3DDevice *iface,
816 long StartVertexIndex,
817 UINT numberOfVertices,
823 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
824 IWineD3DSurfaceImpl *target;
827 if (NumPrimitives == 0) return;
829 /* Invalidate the back buffer memory so LockRect will read it the next time */
830 for(i = 0; i < GL_LIMITS(buffers); i++) {
831 target = (IWineD3DSurfaceImpl *) This->render_targets[i];
833 IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
834 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
838 /* Signals other modules that a drawing is in progress and the stateblock finalized */
839 This->isInDraw = TRUE;
841 ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
843 if (This->stencilBufferTarget) {
844 /* Note that this depends on the ActivateContext call above to set
845 * This->render_offscreen properly */
846 DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
847 surface_load_ds_location(This->stencilBufferTarget, location);
848 surface_modify_ds_location(This->stencilBufferTarget, location);
851 /* Ok, we will be updating the screen from here onwards so grab the lock */
855 BOOL emulation = FALSE;
856 const WineDirect3DVertexStridedData *strided = &This->strided_streams;
857 WineDirect3DVertexStridedData stridedlcl;
858 /* Ok, Work out which primitive is requested and how many vertexes that
860 UINT calculatedNumberOfindices = primitiveToGl(PrimitiveType, NumPrimitives, &glPrimType);
861 if (numberOfVertices == 0 )
862 numberOfVertices = calculatedNumberOfindices;
865 if(!This->strided_streams.u.s.position_transformed && This->activeContext->num_untracked_materials &&
866 This->stateBlock->renderState[WINED3DRS_LIGHTING]) {
869 FIXME("Using software emulation because not all material properties could be tracked\n");
872 TRACE("Using software emulation because not all material properties could be tracked\n");
876 else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
877 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
878 * to a float in the vertex buffer
882 FIXME("Using software emulation because manual fog coordinates are provided\n");
885 TRACE("Using software emulation because manual fog coordinates are provided\n");
891 strided = &stridedlcl;
892 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
893 remove_vbos(This, &stridedlcl);
897 if (This->useDrawStridedSlow || emulation) {
898 /* Immediate mode drawing */
902 FIXME("Using immediate mode with vertex shaders for half float emulation\n");
905 TRACE("Using immediate mode with vertex shaders for half float emulation\n");
907 drawStridedSlowVs(iface, strided, calculatedNumberOfindices, glPrimType,
908 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
910 drawStridedSlow(iface, strided, calculatedNumberOfindices,
911 glPrimType, idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
913 } else if(This->instancedDraw) {
914 /* Instancing emulation with mixing immediate mode and arrays */
915 drawStridedInstanced(iface, &This->strided_streams, calculatedNumberOfindices, glPrimType,
916 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
918 drawStridedFast(iface, calculatedNumberOfindices, glPrimType,
919 idxData, idxSize, minIndex, StartIdx, StartVertexIndex);
923 /* Finished updating the screen, restore lock */
925 TRACE("Done all gl drawing\n");
928 #ifdef SHOW_FRAME_MAKEUP
930 static long int primCounter = 0;
931 /* NOTE: set primCounter to the value reported by drawprim
932 before you want to to write frame makeup to /tmp */
933 if (primCounter >= 0) {
934 WINED3DLOCKED_RECT r;
936 IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
937 sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
938 TRACE("Saving screenshot %s\n", buffer);
939 IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
940 IWineD3DSurface_UnlockRect(This->render_targets[0]);
942 #ifdef SHOW_TEXTURE_MAKEUP
944 IWineD3DSurface *pSur;
946 for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
947 if (This->stateBlock->textures[textureNo] != NULL) {
948 sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
949 TRACE("Saving texture %s\n", buffer);
950 if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
951 IWineD3DTexture_GetSurfaceLevel((IWineD3DTexture *)This->stateBlock->textures[textureNo], 0, &pSur);
952 IWineD3DSurface_SaveSnapshot(pSur, buffer);
953 IWineD3DSurface_Release(pSur);
955 FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
962 TRACE("drawprim #%ld\n", primCounter);
967 /* Control goes back to the device, stateblock values may change again */
968 This->isInDraw = FALSE;
971 static void normalize_normal(float *n) {
972 float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
973 if(length == 0.0) return;
974 length = sqrt(length);
975 n[0] = n[0] / length;
976 n[1] = n[1] / length;
977 n[2] = n[2] / length;
980 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
982 * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
983 * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
984 * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
985 * attributes to numbered shader attributes, so we have to store them and rebind them as needed
988 * To read back, the opengl feedback mode is used. This creates a problem because we want
989 * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
990 * Thus disable lighting and set identity matrices to get unmodified colors and positions.
991 * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
992 * them to [-1.0;+1.0] and set the viewport up to scale them back.
994 * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
995 * resulting colors back to the normals.
997 * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
998 * does not restore it because normally a draw follows immediately afterwards. The caller is
999 * responsible of taking care that either the gl states are restored, or the context activated
1000 * for drawing to reset the lastWasBlit flag.
1002 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
1003 struct WineD3DRectPatch *patch) {
1004 unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
1005 float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
1006 WineDirect3DVertexStridedData strided;
1008 const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
1010 GLenum feedback_type;
1011 GLfloat *feedbuffer;
1013 /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
1016 memset(&strided, 0, sizeof(strided));
1017 primitiveDeclarationConvertToStridedData((IWineD3DDevice *) This, FALSE, &strided, NULL);
1018 if(strided.u.s.position.VBO) {
1019 IWineD3DVertexBufferImpl *vb;
1020 vb = (IWineD3DVertexBufferImpl *) This->stateBlock->streamSource[strided.u.s.position.streamNo];
1021 strided.u.s.position.lpData = (BYTE *) ((unsigned long) strided.u.s.position.lpData +
1022 (unsigned long) vb->resource.allocatedMemory);
1024 vtxStride = strided.u.s.position.dwStride;
1025 data = strided.u.s.position.lpData +
1026 vtxStride * info->Stride * info->StartVertexOffsetHeight +
1027 vtxStride * info->StartVertexOffsetWidth;
1029 /* Not entirely sure about what happens with transformed vertices */
1030 if(strided.u.s.position_transformed) {
1031 FIXME("Transformed position in rectpatch generation\n");
1033 if(vtxStride % sizeof(GLfloat)) {
1034 /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
1035 * I don't see how the stride could not be a multiple of 4, but make sure
1038 ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
1040 if(info->Basis != WINED3DBASIS_BEZIER) {
1041 FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
1043 if(info->Degree != WINED3DDEGREE_CUBIC) {
1044 FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
1047 /* First, get the boundary cube of the input data */
1048 for(j = 0; j < info->Height; j++) {
1049 for(i = 0; i < info->Width; i++) {
1050 const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
1051 if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
1052 if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
1053 if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
1054 if(v[2] < neg_z) neg_z = v[2];
1058 /* This needs some improvements in the vertex decl code */
1059 FIXME("Cannot find data to generate. Only generating position and normals\n");
1060 patch->has_normals = TRUE;
1061 patch->has_texcoords = FALSE;
1063 /* Simply activate the context for blitting. This disables all the things we don't want and
1064 * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
1065 * patch (as opposed to normal draws) will most likely need different changes anyway
1067 ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
1070 glMatrixMode(GL_PROJECTION);
1071 checkGLcall("glMatrixMode(GL_PROJECTION)");
1073 checkGLcall("glLoadIndentity()");
1074 glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
1075 glTranslatef(0, 0, 0.5);
1076 checkGLcall("glScalef");
1077 glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
1078 checkGLcall("glViewport");
1080 /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
1081 * our feedback buffer parser
1083 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
1084 checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
1085 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
1086 if(patch->has_normals) {
1087 const GLfloat black[4] = {0, 0, 0, 0};
1088 const GLfloat red[4] = {1, 0, 0, 0};
1089 const GLfloat green[4] = {0, 1, 0, 0};
1090 const GLfloat blue[4] = {0, 0, 1, 0};
1091 const GLfloat white[4] = {1, 1, 1, 1};
1092 glEnable(GL_LIGHTING);
1093 checkGLcall("glEnable(GL_LIGHTING)");
1094 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
1095 checkGLcall("glLightModel for MODEL_AMBIENT");
1096 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
1098 for(i = 3; i < GL_LIMITS(lights); i++) {
1099 glDisable(GL_LIGHT0 + i);
1100 checkGLcall("glDisable(GL_LIGHT0 + i)");
1101 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
1104 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
1105 glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
1106 glLightfv(GL_LIGHT0, GL_SPECULAR, black);
1107 glLightfv(GL_LIGHT0, GL_AMBIENT, black);
1108 glLightfv(GL_LIGHT0, GL_POSITION, red);
1109 glEnable(GL_LIGHT0);
1110 checkGLcall("Setting up light 1\n");
1111 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
1112 glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
1113 glLightfv(GL_LIGHT1, GL_SPECULAR, black);
1114 glLightfv(GL_LIGHT1, GL_AMBIENT, black);
1115 glLightfv(GL_LIGHT1, GL_POSITION, green);
1116 glEnable(GL_LIGHT1);
1117 checkGLcall("Setting up light 2\n");
1118 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
1119 glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
1120 glLightfv(GL_LIGHT2, GL_SPECULAR, black);
1121 glLightfv(GL_LIGHT2, GL_AMBIENT, black);
1122 glLightfv(GL_LIGHT2, GL_POSITION, blue);
1123 glEnable(GL_LIGHT2);
1124 checkGLcall("Setting up light 3\n");
1126 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
1127 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
1128 glDisable(GL_COLOR_MATERIAL);
1129 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
1130 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
1131 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
1132 checkGLcall("Setting up materials\n");
1135 /* Enable the needed maps.
1136 * GL_MAP2_VERTEX_3 is needed for positional data.
1137 * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
1138 * GL_MAP2_TEXTURE_COORD_4 for texture coords
1140 num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
1141 out_vertex_size = 3 /* position */;
1142 d3d_out_vertex_size = 3;
1143 glEnable(GL_MAP2_VERTEX_3);
1144 if(patch->has_normals && patch->has_texcoords) {
1145 FIXME("Texcoords not handled yet\n");
1146 feedback_type = GL_3D_COLOR_TEXTURE;
1147 out_vertex_size += 8;
1148 d3d_out_vertex_size += 7;
1149 glEnable(GL_AUTO_NORMAL);
1150 glEnable(GL_MAP2_TEXTURE_COORD_4);
1151 } else if(patch->has_texcoords) {
1152 FIXME("Texcoords not handled yet\n");
1153 feedback_type = GL_3D_COLOR_TEXTURE;
1154 out_vertex_size += 7;
1155 d3d_out_vertex_size += 4;
1156 glEnable(GL_MAP2_TEXTURE_COORD_4);
1157 } else if(patch->has_normals) {
1158 feedback_type = GL_3D_COLOR;
1159 out_vertex_size += 4;
1160 d3d_out_vertex_size += 3;
1161 glEnable(GL_AUTO_NORMAL);
1163 feedback_type = GL_3D;
1165 checkGLcall("glEnable vertex attrib generation");
1167 buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
1168 + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
1169 feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
1171 glMap2f(GL_MAP2_VERTEX_3,
1172 0, 1, vtxStride / sizeof(float), info->Width,
1173 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1174 (const GLfloat *)data);
1175 checkGLcall("glMap2f");
1176 if(patch->has_texcoords) {
1177 glMap2f(GL_MAP2_TEXTURE_COORD_4,
1178 0, 1, vtxStride / sizeof(float), info->Width,
1179 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
1180 (const GLfloat *)data);
1181 checkGLcall("glMap2f");
1183 glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
1184 checkGLcall("glMapGrid2f");
1186 glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
1187 checkGLcall("glFeedbackBuffer");
1188 glRenderMode(GL_FEEDBACK);
1190 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1191 checkGLcall("glEvalMesh2\n");
1193 i = glRenderMode(GL_RENDER);
1196 ERR("Feedback failed. Expected %d elements back\n", buffer_size);
1198 HeapFree(GetProcessHeap(), 0, feedbuffer);
1199 return WINED3DERR_DRIVERINTERNALERROR;
1200 } else if(i != buffer_size) {
1202 ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
1204 HeapFree(GetProcessHeap(), 0, feedbuffer);
1205 return WINED3DERR_DRIVERINTERNALERROR;
1207 TRACE("Got %d elements as expected\n", i);
1210 HeapFree(GetProcessHeap(), 0, patch->mem);
1211 patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
1213 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1214 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1215 ERR("Unexpected token: %f\n", feedbuffer[j]);
1218 if(feedbuffer[j + 1] != 3) {
1219 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1222 /* Somehow there are different ideas about back / front facing, so fix up the
1225 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
1226 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
1227 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
1228 if(patch->has_normals) {
1229 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1230 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1231 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1233 i += d3d_out_vertex_size;
1235 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1236 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1237 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
1238 if(patch->has_normals) {
1239 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1240 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1241 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1243 i += d3d_out_vertex_size;
1245 patch->mem[i + 0] = feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1246 patch->mem[i + 1] = feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1247 patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
1248 if(patch->has_normals) {
1249 patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1250 patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1251 patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1253 i += d3d_out_vertex_size;
1256 if(patch->has_normals) {
1257 /* Now do the same with reverse light directions */
1258 const GLfloat x[4] = {-1, 0, 0, 0};
1259 const GLfloat y[4] = { 0, -1, 0, 0};
1260 const GLfloat z[4] = { 0, 0, -1, 0};
1261 glLightfv(GL_LIGHT0, GL_POSITION, x);
1262 glLightfv(GL_LIGHT1, GL_POSITION, y);
1263 glLightfv(GL_LIGHT2, GL_POSITION, z);
1264 checkGLcall("Setting up reverse light directions\n");
1266 glRenderMode(GL_FEEDBACK);
1267 checkGLcall("glRenderMode(GL_FEEDBACK)");
1268 glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1269 checkGLcall("glEvalMesh2\n");
1270 i = glRenderMode(GL_RENDER);
1271 checkGLcall("glRenderMode(GL_RENDER)");
1274 for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1275 if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1276 ERR("Unexpected token: %f\n", feedbuffer[j]);
1279 if(feedbuffer[j + 1] != 3) {
1280 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1283 if(patch->mem[i + 3] == 0.0)
1284 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1285 if(patch->mem[i + 4] == 0.0)
1286 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1287 if(patch->mem[i + 5] == 0.0)
1288 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1289 normalize_normal(patch->mem + i + 3);
1290 i += d3d_out_vertex_size;
1292 if(patch->mem[i + 3] == 0.0)
1293 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1294 if(patch->mem[i + 4] == 0.0)
1295 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1296 if(patch->mem[i + 5] == 0.0)
1297 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1298 normalize_normal(patch->mem + i + 3);
1299 i += d3d_out_vertex_size;
1301 if(patch->mem[i + 3] == 0.0)
1302 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1303 if(patch->mem[i + 4] == 0.0)
1304 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1305 if(patch->mem[i + 5] == 0.0)
1306 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1307 normalize_normal(patch->mem + i + 3);
1308 i += d3d_out_vertex_size;
1312 glDisable(GL_MAP2_VERTEX_3);
1313 glDisable(GL_AUTO_NORMAL);
1314 glDisable(GL_MAP2_NORMAL);
1315 glDisable(GL_MAP2_TEXTURE_COORD_4);
1316 checkGLcall("glDisable vertex attrib generation");
1319 HeapFree(GetProcessHeap(), 0, feedbuffer);
1321 vtxStride = 3 * sizeof(float);
1322 if(patch->has_normals) {
1323 vtxStride += 3 * sizeof(float);
1325 if(patch->has_texcoords) {
1326 vtxStride += 4 * sizeof(float);
1328 memset(&patch->strided, 0, sizeof(&patch->strided));
1329 patch->strided.u.s.position.lpData = (BYTE *) patch->mem;
1330 patch->strided.u.s.position.dwStride = vtxStride;
1331 patch->strided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
1332 patch->strided.u.s.position.streamNo = 255;
1334 if(patch->has_normals) {
1335 patch->strided.u.s.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1336 patch->strided.u.s.normal.dwStride = vtxStride;
1337 patch->strided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
1338 patch->strided.u.s.normal.streamNo = 255;
1340 if(patch->has_texcoords) {
1341 patch->strided.u.s.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1342 if(patch->has_normals) {
1343 patch->strided.u.s.texCoords[0].lpData += 3 * sizeof(float);
1345 patch->strided.u.s.texCoords[0].dwStride = vtxStride;
1346 patch->strided.u.s.texCoords[0].dwType = WINED3DDECLTYPE_FLOAT4;
1347 /* MAX_STREAMS index points to an unused element in stateblock->streamOffsets which
1348 * always remains set to 0. Windows uses stream 255 here, but this is not visible to the
1351 patch->strided.u.s.texCoords[0].streamNo = MAX_STREAMS;