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