wined3d: Add NV asm extension support to the ARB backend.
[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 /* GL locking is done by the caller */
36 static void drawStridedFast(IWineD3DDevice *iface, GLenum primitive_type,
37         UINT min_vertex_idx, UINT max_vertex_idx, UINT count, UINT idx_size,
38         const void *idx_data, UINT start_idx)
39 {
40     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
41
42     if (idx_size)
43     {
44         TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, primitive_type, count, min_vertex_idx);
45
46 #if 1
47         glDrawElements(primitive_type, count,
48                 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
49                 (const char *)idx_data + (idx_size * start_idx));
50         checkGLcall("glDrawElements");
51 #else
52         glDrawRangeElements(primitive_type, min_vertex_idx, max_vertex_idx, count,
53                 idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
54                 (const char *)idx_data + (idx_size * start_idx));
55         checkGLcall("glDrawRangeElements");
56 #endif
57     }
58     else
59     {
60         TRACE("(%p) : glDrawArrays(%#x, %d, %d)\n", This, primitive_type, start_idx, count);
61
62         glDrawArrays(primitive_type, start_idx, count);
63         checkGLcall("glDrawArrays");
64     }
65 }
66
67 /*
68  * Actually draw using the supplied information.
69  * Slower GL version which extracts info about each vertex in turn
70  */
71
72 /* GL locking is done by the caller */
73 static void drawStridedSlow(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT NumVertexes,
74         GLenum glPrimType, const void *idxData, UINT idxSize, UINT minIndex, UINT startIdx)
75 {
76     unsigned int               textureNo    = 0;
77     const WORD                *pIdxBufS     = NULL;
78     const DWORD               *pIdxBufL     = NULL;
79     UINT vx_index;
80     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
81     const UINT *streamOffset = This->stateBlock->streamOffset;
82     long                      SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
83     BOOL                      pixelShader = use_ps(This->stateBlock);
84     BOOL specular_fog = FALSE;
85     UINT texture_stages = GL_LIMITS(texture_stages);
86     const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
87     const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
88     const struct wined3d_stream_info_element *element;
89     DWORD tex_mask = 0;
90
91     TRACE("Using slow vertex array code\n");
92
93     /* Variable Initialization */
94     if (idxSize != 0) {
95         /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
96          * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
97          * idxData will be != NULL
98          */
99         if(idxData == NULL) {
100             idxData = buffer_get_sysmem((struct wined3d_buffer *) This->stateBlock->pIndexData);
101         }
102
103         if (idxSize == 2) pIdxBufS = idxData;
104         else pIdxBufL = idxData;
105     } else if (idxData) {
106         ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
107         return;
108     }
109
110     /* Start drawing in GL */
111     VTRACE(("glBegin(%x)\n", glPrimType));
112     glBegin(glPrimType);
113
114     element = &si->elements[WINED3D_FFP_POSITION];
115     if (element->data) position = element->data + streamOffset[element->stream_idx];
116
117     element = &si->elements[WINED3D_FFP_NORMAL];
118     if (element->data) normal = element->data + streamOffset[element->stream_idx];
119     else glNormal3f(0, 0, 0);
120
121     element = &si->elements[WINED3D_FFP_DIFFUSE];
122     if (element->data) diffuse = element->data + streamOffset[element->stream_idx];
123     else glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
124     if (This->activeContext->num_untracked_materials && element->format_desc->format != WINED3DFMT_A8R8G8B8)
125         FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format_desc->format));
126
127     element = &si->elements[WINED3D_FFP_SPECULAR];
128     if (element->data)
129     {
130         specular = element->data + streamOffset[element->stream_idx];
131
132         /* special case where the fog density is stored in the specular alpha channel */
133         if (This->stateBlock->renderState[WINED3DRS_FOGENABLE]
134                 && (This->stateBlock->renderState[WINED3DRS_FOGVERTEXMODE] == WINED3DFOG_NONE
135                     || si->elements[WINED3D_FFP_POSITION].format_desc->format == WINED3DFMT_R32G32B32A32_FLOAT)
136                 && This->stateBlock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE)
137         {
138             if (GL_SUPPORT(EXT_FOG_COORD))
139             {
140                 if (element->format_desc->format == WINED3DFMT_A8R8G8B8) specular_fog = TRUE;
141                 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format_desc->format));
142             }
143             else
144             {
145                 static BOOL warned;
146
147                 if (!warned)
148                 {
149                     /* TODO: Use the fog table code from old ddraw */
150                     FIXME("Implement fog for transformed vertices in software\n");
151                     warned = TRUE;
152                 }
153             }
154         }
155     }
156     else if (GL_SUPPORT(EXT_SECONDARY_COLOR))
157     {
158         GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
159     }
160
161     for (textureNo = 0; textureNo < texture_stages; ++textureNo)
162     {
163         int coordIdx = This->stateBlock->textureState[textureNo][WINED3DTSS_TEXCOORDINDEX];
164         int texture_idx = This->texUnitMap[textureNo];
165
166         if (!GL_SUPPORT(ARB_MULTITEXTURE) && textureNo > 0)
167         {
168             FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
169             continue;
170         }
171
172         if (!pixelShader && !This->stateBlock->textures[textureNo]) continue;
173
174         if (texture_idx == -1) continue;
175
176         if (coordIdx > 7)
177         {
178             TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
179             continue;
180         }
181         else if (coordIdx < 0)
182         {
183             FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
184             continue;
185         }
186
187         element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
188         if (element->data)
189         {
190             texCoords[coordIdx] = element->data + streamOffset[element->stream_idx];
191             tex_mask |= (1 << textureNo);
192         }
193         else
194         {
195             TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
196             if (GL_SUPPORT(ARB_MULTITEXTURE))
197                 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
198             else
199                 glTexCoord4f(0, 0, 0, 1);
200         }
201     }
202
203     /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
204      * Guess it's not necessary(we crash then anyway) and would only eat CPU time
205      */
206
207     /* For each primitive */
208     for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
209         UINT texture, tmp_tex_mask;
210         /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
211          * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
212          */
213
214         /* For indexed data, we need to go a few more strides in */
215         if (idxData != NULL) {
216
217             /* Indexed so work out the number of strides to skip */
218             if (idxSize == 2) {
219                 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufS[startIdx+vx_index]));
220                 SkipnStrides = pIdxBufS[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
221             } else {
222                 VTRACE(("Idx for vertex %u = %u\n", vx_index, pIdxBufL[startIdx+vx_index]));
223                 SkipnStrides = pIdxBufL[startIdx + vx_index] + This->stateBlock->loadBaseVertexIndex;
224             }
225         }
226
227         tmp_tex_mask = tex_mask;
228         for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
229         {
230             int coord_idx;
231             const void *ptr;
232             int texture_idx;
233
234             if (!(tmp_tex_mask & 1)) continue;
235
236             coord_idx = This->stateBlock->textureState[texture][WINED3DTSS_TEXCOORDINDEX];
237             ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
238
239             texture_idx = This->texUnitMap[texture];
240             multi_texcoord_funcs[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format_desc->emit_idx](
241                     GL_TEXTURE0_ARB + texture_idx, ptr);
242         }
243
244         /* Diffuse -------------------------------- */
245         if (diffuse) {
246             const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
247
248             diffuse_funcs[si->elements[WINED3D_FFP_DIFFUSE].format_desc->emit_idx](ptrToCoords);
249             if(This->activeContext->num_untracked_materials) {
250                 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
251                 unsigned char i;
252                 float color[4];
253
254                 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0;
255                 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0;
256                 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0;
257                 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0;
258
259                 for(i = 0; i < This->activeContext->num_untracked_materials; i++) {
260                     glMaterialfv(GL_FRONT_AND_BACK, This->activeContext->untracked_materials[i], color);
261                 }
262             }
263         }
264
265         /* Specular ------------------------------- */
266         if (specular) {
267             const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
268
269             specular_funcs[si->elements[WINED3D_FFP_SPECULAR].format_desc->emit_idx](ptrToCoords);
270
271             if (specular_fog)
272             {
273                 DWORD specularColor = *(const DWORD *)ptrToCoords;
274                 GL_EXTCALL(glFogCoordfEXT(specularColor >> 24));
275             }
276         }
277
278         /* Normal -------------------------------- */
279         if (normal != NULL) {
280             const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
281             normal_funcs[si->elements[WINED3D_FFP_NORMAL].format_desc->emit_idx](ptrToCoords);
282         }
283
284         /* Position -------------------------------- */
285         if (position) {
286             const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
287             position_funcs[si->elements[WINED3D_FFP_POSITION].format_desc->emit_idx](ptrToCoords);
288         }
289
290         /* For non indexed mode, step onto next parts */
291         if (idxData == NULL) {
292             ++SkipnStrides;
293         }
294     }
295
296     glEnd();
297     checkGLcall("glEnd and previous calls");
298 }
299
300 /* GL locking is done by the caller */
301 static inline void send_attribute(IWineD3DDeviceImpl *This, WINED3DFORMAT format, const UINT index, const void *ptr)
302 {
303     switch(format)
304     {
305         case WINED3DFMT_R32_FLOAT:
306             GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
307             break;
308         case WINED3DFMT_R32G32_FLOAT:
309             GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
310             break;
311         case WINED3DFMT_R32G32B32_FLOAT:
312             GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
313             break;
314         case WINED3DFMT_R32G32B32A32_FLOAT:
315             GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
316             break;
317
318         case WINED3DFMT_R8G8B8A8_UINT:
319             GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
320             break;
321         case WINED3DFMT_A8R8G8B8:
322             if (GL_SUPPORT(EXT_VERTEX_ARRAY_BGRA))
323             {
324                 const DWORD *src = ptr;
325                 DWORD c = *src & 0xff00ff00;
326                 c |= (*src & 0xff0000) >> 16;
327                 c |= (*src & 0xff) << 16;
328                 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
329                 break;
330             }
331             /* else fallthrough */
332         case WINED3DFMT_R8G8B8A8_UNORM:
333             GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
334             break;
335
336         case WINED3DFMT_R16G16_SINT:
337             GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
338             break;
339         case WINED3DFMT_R16G16B16A16_SINT:
340             GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
341             break;
342
343         case WINED3DFMT_R16G16_SNORM:
344         {
345             GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
346             GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
347             break;
348         }
349         case WINED3DFMT_R16G16_UNORM:
350         {
351             GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
352             GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
353             break;
354         }
355         case WINED3DFMT_R16G16B16A16_SNORM:
356             GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
357             break;
358         case WINED3DFMT_R16G16B16A16_UNORM:
359             GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
360             break;
361
362         case WINED3DFMT_R10G10B10A2_UINT:
363             FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
364             /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
365             break;
366         case WINED3DFMT_R10G10B10A2_SNORM:
367             FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
368             /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
369             break;
370
371         case WINED3DFMT_R16G16_FLOAT:
372             /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
373              * byte float according to the IEEE standard
374              */
375             if (GL_SUPPORT(NV_HALF_FLOAT)) {
376                 /* Not supported by GL_ARB_half_float_vertex */
377                 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
378             } else {
379                 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
380                 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
381                 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
382             }
383             break;
384         case WINED3DFMT_R16G16B16A16_FLOAT:
385             if (GL_SUPPORT(NV_HALF_FLOAT)) {
386                 /* Not supported by GL_ARB_half_float_vertex */
387                 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
388             } else {
389                 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
390                 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
391                 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
392                 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
393                 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
394             }
395             break;
396
397         default:
398             ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
399             break;
400     }
401 }
402
403 /* GL locking is done by the caller */
404 static void drawStridedSlowVs(IWineD3DDevice *iface, const struct wined3d_stream_info *si, UINT numberOfVertices,
405         GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT minIndex, UINT startIdx)
406 {
407     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
408     long                      SkipnStrides = startIdx + This->stateBlock->loadBaseVertexIndex;
409     const WORD                *pIdxBufS     = NULL;
410     const DWORD               *pIdxBufL     = NULL;
411     UINT vx_index;
412     int i;
413     IWineD3DStateBlockImpl *stateblock = This->stateBlock;
414     const BYTE *ptr;
415
416     if (idxSize != 0) {
417         /* Immediate mode drawing can't make use of indices in a vbo - get the data from the index buffer.
418          * If the index buffer has no vbo(not supported or other reason), or with user pointer drawing
419          * idxData will be != NULL
420          */
421         if(idxData == NULL) {
422             idxData = buffer_get_sysmem((struct wined3d_buffer *) This->stateBlock->pIndexData);
423         }
424
425         if (idxSize == 2) pIdxBufS = idxData;
426         else pIdxBufL = idxData;
427     } else if (idxData) {
428         ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
429         return;
430     }
431
432     /* Start drawing in GL */
433     VTRACE(("glBegin(%x)\n", glPrimitiveType));
434     glBegin(glPrimitiveType);
435
436     for (vx_index = 0; vx_index < numberOfVertices; ++vx_index) {
437         if (idxData != NULL) {
438
439             /* Indexed so work out the number of strides to skip */
440             if (idxSize == 2) {
441                 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufS[startIdx+vx_index]));
442                 SkipnStrides = pIdxBufS[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
443             } else {
444                 VTRACE(("Idx for vertex %d = %d\n", vx_index, pIdxBufL[startIdx+vx_index]));
445                 SkipnStrides = pIdxBufL[startIdx + vx_index] + stateblock->loadBaseVertexIndex;
446             }
447         }
448
449         for(i = MAX_ATTRIBS - 1; i >= 0; i--) {
450             if(!si->elements[i].data) continue;
451
452             ptr = si->elements[i].data +
453                   si->elements[i].stride * SkipnStrides +
454                   stateblock->streamOffset[si->elements[i].stream_idx];
455
456             send_attribute(This, si->elements[i].format_desc->format, i, ptr);
457         }
458         SkipnStrides++;
459     }
460
461     glEnd();
462 }
463
464 /* GL locking is done by the caller */
465 static inline void drawStridedInstanced(IWineD3DDevice *iface, const struct wined3d_stream_info *si,
466         UINT numberOfVertices, GLenum glPrimitiveType, const void *idxData, UINT idxSize, UINT minIndex,
467         UINT startIdx)
468 {
469     UINT numInstances = 0, i;
470     int numInstancedAttribs = 0, j;
471     UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
472     IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *) iface;
473     IWineD3DStateBlockImpl *stateblock = This->stateBlock;
474
475     if (idxSize == 0) {
476         /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
477          * We don't support this for now
478          *
479          * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
480          * But the StreamSourceFreq value has a different meaning in that situation.
481          */
482         FIXME("Non-indexed instanced drawing is not supported\n");
483         return;
484     }
485
486     TRACE("(%p) : glElements(%x, %d, %d, ...)\n", This, glPrimitiveType, numberOfVertices, minIndex);
487
488     /* First, figure out how many instances we have to draw */
489     for(i = 0; i < MAX_STREAMS; i++) {
490         /* Look at the streams and take the first one which matches */
491         if(((stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INSTANCEDATA) || (stateblock->streamFlags[i] & WINED3DSTREAMSOURCE_INDEXEDDATA)) && stateblock->streamSource[i]) {
492             /* D3D9 could set streamFreq 0 with (INSTANCEDATA or INDEXEDDATA) and then it is handled as 1. See d3d9/tests/visual.c-> stream_test() */
493             if(stateblock->streamFreq[i] == 0){
494                 numInstances = 1;
495             } else {
496                 numInstances = stateblock->streamFreq[i]; /* use the specified number of instances from the first matched stream. See d3d9/tests/visual.c-> stream_test() */
497             }
498             break; /* break, because only the first suitable value is interesting */
499         }
500     }
501
502     for (i = 0; i < sizeof(si->elements) / sizeof(*si->elements); ++i)
503     {
504         if (stateblock->streamFlags[si->elements[i].stream_idx] & WINED3DSTREAMSOURCE_INSTANCEDATA)
505         {
506             instancedData[numInstancedAttribs] = i;
507             numInstancedAttribs++;
508         }
509     }
510
511     /* now draw numInstances instances :-) */
512     for(i = 0; i < numInstances; i++) {
513         /* Specify the instanced attributes using immediate mode calls */
514         for(j = 0; j < numInstancedAttribs; j++) {
515             const BYTE *ptr = si->elements[instancedData[j]].data +
516                         si->elements[instancedData[j]].stride * i +
517                         stateblock->streamOffset[si->elements[instancedData[j]].stream_idx];
518             if (si->elements[instancedData[j]].buffer_object)
519             {
520                 struct wined3d_buffer *vb =
521                         (struct wined3d_buffer *)stateblock->streamSource[si->elements[instancedData[j]].stream_idx];
522                 ptr += (long) buffer_get_sysmem(vb);
523             }
524
525             send_attribute(This, si->elements[instancedData[j]].format_desc->format, instancedData[j], ptr);
526         }
527
528         glDrawElements(glPrimitiveType, numberOfVertices, idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
529                     (const char *)idxData+(idxSize * startIdx));
530         checkGLcall("glDrawElements");
531     }
532 }
533
534 static inline void remove_vbos(IWineD3DDeviceImpl *This, struct wined3d_stream_info *s)
535 {
536     unsigned int i;
537
538     for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
539     {
540         struct wined3d_stream_info_element *e = &s->elements[i];
541         if (e->buffer_object)
542         {
543             struct wined3d_buffer *vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
544             e->buffer_object = 0;
545             e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
546         }
547     }
548 }
549
550 /* Routine common to the draw primitive and draw indexed primitive routines */
551 void drawPrimitive(IWineD3DDevice *iface, UINT index_count, UINT numberOfVertices,
552         UINT StartIdx, UINT idxSize, const void *idxData, UINT minIndex)
553 {
554
555     IWineD3DDeviceImpl           *This = (IWineD3DDeviceImpl *)iface;
556     IWineD3DSurfaceImpl          *target;
557     unsigned int i;
558
559     if (!index_count) return;
560
561     /* Invalidate the back buffer memory so LockRect will read it the next time */
562     for(i = 0; i < GL_LIMITS(buffers); i++) {
563         target = (IWineD3DSurfaceImpl *) This->render_targets[i];
564         if (target) {
565             IWineD3DSurface_LoadLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, NULL);
566             IWineD3DSurface_ModifyLocation((IWineD3DSurface *) target, SFLAG_INDRAWABLE, TRUE);
567         }
568     }
569
570     /* Signals other modules that a drawing is in progress and the stateblock finalized */
571     This->isInDraw = TRUE;
572
573     ActivateContext(This, This->render_targets[0], CTXUSAGE_DRAWPRIM);
574
575     if (This->stencilBufferTarget) {
576         /* Note that this depends on the ActivateContext call above to set
577          * This->render_offscreen properly */
578         DWORD location = This->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
579         surface_load_ds_location(This->stencilBufferTarget, location);
580         surface_modify_ds_location(This->stencilBufferTarget, location);
581     }
582
583     /* Ok, we will be updating the screen from here onwards so grab the lock */
584     ENTER_GL();
585     {
586         GLenum glPrimType = This->stateBlock->gl_primitive_type;
587         BOOL emulation = FALSE;
588         const struct wined3d_stream_info *stream_info = &This->strided_streams;
589         struct wined3d_stream_info stridedlcl;
590
591         if (!numberOfVertices) numberOfVertices = index_count;
592
593         if (!use_vs(This->stateBlock))
594         {
595             if (!This->strided_streams.position_transformed && This->activeContext->num_untracked_materials
596                     && This->stateBlock->renderState[WINED3DRS_LIGHTING])
597             {
598                 static BOOL warned;
599                 if (!warned) {
600                     FIXME("Using software emulation because not all material properties could be tracked\n");
601                     warned = TRUE;
602                 } else {
603                     TRACE("Using software emulation because not all material properties could be tracked\n");
604                 }
605                 emulation = TRUE;
606             }
607             else if(This->activeContext->fog_coord && This->stateBlock->renderState[WINED3DRS_FOGENABLE]) {
608                 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
609                  * to a float in the vertex buffer
610                  */
611                 static BOOL warned;
612                 if (!warned) {
613                     FIXME("Using software emulation because manual fog coordinates are provided\n");
614                     warned = TRUE;
615                 } else {
616                     TRACE("Using software emulation because manual fog coordinates are provided\n");
617                 }
618                 emulation = TRUE;
619             }
620
621             if(emulation) {
622                 stream_info = &stridedlcl;
623                 memcpy(&stridedlcl, &This->strided_streams, sizeof(stridedlcl));
624                 remove_vbos(This, &stridedlcl);
625             }
626         }
627
628         if (This->useDrawStridedSlow || emulation) {
629             /* Immediate mode drawing */
630             if (use_vs(This->stateBlock))
631             {
632                 static BOOL warned;
633                 if (!warned) {
634                     FIXME("Using immediate mode with vertex shaders for half float emulation\n");
635                     warned = TRUE;
636                 } else {
637                     TRACE("Using immediate mode with vertex shaders for half float emulation\n");
638                 }
639                 drawStridedSlowVs(iface, stream_info, index_count, glPrimType, idxData, idxSize, minIndex, StartIdx);
640             } else {
641                 drawStridedSlow(iface, stream_info, index_count, glPrimType, idxData, idxSize, minIndex, StartIdx);
642             }
643         } else if(This->instancedDraw) {
644             /* Instancing emulation with mixing immediate mode and arrays */
645             drawStridedInstanced(iface, &This->strided_streams, index_count,
646                     glPrimType, idxData, idxSize, minIndex, StartIdx);
647         } else {
648             drawStridedFast(iface, glPrimType, minIndex, minIndex + numberOfVertices - 1,
649                     index_count, idxSize, idxData, StartIdx);
650         }
651     }
652
653     /* Finished updating the screen, restore lock */
654     LEAVE_GL();
655     TRACE("Done all gl drawing\n");
656
657     /* Diagnostics */
658 #ifdef SHOW_FRAME_MAKEUP
659     {
660         static long int primCounter = 0;
661         /* NOTE: set primCounter to the value reported by drawprim 
662            before you want to to write frame makeup to /tmp */
663         if (primCounter >= 0) {
664             WINED3DLOCKED_RECT r;
665             char buffer[80];
666             IWineD3DSurface_LockRect(This->render_targets[0], &r, NULL, WINED3DLOCK_READONLY);
667             sprintf(buffer, "/tmp/backbuffer_%ld.tga", primCounter);
668             TRACE("Saving screenshot %s\n", buffer);
669             IWineD3DSurface_SaveSnapshot(This->render_targets[0], buffer);
670             IWineD3DSurface_UnlockRect(This->render_targets[0]);
671
672 #ifdef SHOW_TEXTURE_MAKEUP
673            {
674             IWineD3DSurface *pSur;
675             int textureNo;
676             for (textureNo = 0; textureNo < MAX_COMBINED_SAMPLERS; ++textureNo) {
677                 if (This->stateBlock->textures[textureNo] != NULL) {
678                     sprintf(buffer, "/tmp/texture_%p_%ld_%d.tga", This->stateBlock->textures[textureNo], primCounter, textureNo);
679                     TRACE("Saving texture %s\n", buffer);
680                     if (IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]) == WINED3DRTYPE_TEXTURE) {
681                             IWineD3DTexture_GetSurfaceLevel(This->stateBlock->textures[textureNo], 0, &pSur);
682                             IWineD3DSurface_SaveSnapshot(pSur, buffer);
683                             IWineD3DSurface_Release(pSur);
684                     } else  {
685                         FIXME("base Texture isn't of type texture %d\n", IWineD3DBaseTexture_GetType(This->stateBlock->textures[textureNo]));
686                     }
687                 }
688             }
689            }
690 #endif
691         }
692         TRACE("drawprim #%ld\n", primCounter);
693         ++primCounter;
694     }
695 #endif
696
697     /* Control goes back to the device, stateblock values may change again */
698     This->isInDraw = FALSE;
699 }
700
701 static void normalize_normal(float *n) {
702     float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
703     if(length == 0.0) return;
704     length = sqrt(length);
705     n[0] = n[0] / length;
706     n[1] = n[1] / length;
707     n[2] = n[2] / length;
708 }
709
710 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
711  *
712  * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
713  * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
714  * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
715  * attributes to numbered shader attributes, so we have to store them and rebind them as needed
716  * in drawprim.
717  *
718  * To read back, the opengl feedback mode is used. This creates a problem because we want
719  * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
720  * Thus disable lighting and set identity matrices to get unmodified colors and positions.
721  * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
722  * them to [-1.0;+1.0] and set the viewport up to scale them back.
723  *
724  * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
725  * resulting colors back to the normals.
726  *
727  * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
728  * does not restore it because normally a draw follows immediately afterwards. The caller is
729  * responsible of taking care that either the gl states are restored, or the context activated
730  * for drawing to reset the lastWasBlit flag.
731  */
732 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
733                             struct WineD3DRectPatch *patch) {
734     unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
735     float max_x = 0.0, max_y = 0.0, max_z = 0.0, neg_z = 0.0;
736     struct wined3d_stream_info stream_info;
737     struct wined3d_stream_info_element *e;
738     const BYTE *data;
739     const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
740     DWORD vtxStride;
741     GLenum feedback_type;
742     GLfloat *feedbuffer;
743
744     /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
745      * Beware of vbos
746      */
747     device_stream_info_from_declaration(This, FALSE, &stream_info, NULL);
748
749     e = &stream_info.elements[WINED3D_FFP_POSITION];
750     if (e->buffer_object)
751     {
752         struct wined3d_buffer *vb;
753         vb = (struct wined3d_buffer *)This->stateBlock->streamSource[e->stream_idx];
754         e->data = (BYTE *)((unsigned long)e->data + (unsigned long)buffer_get_sysmem(vb));
755     }
756     vtxStride = e->stride;
757     data = e->data +
758            vtxStride * info->Stride * info->StartVertexOffsetHeight +
759            vtxStride * info->StartVertexOffsetWidth;
760
761     /* Not entirely sure about what happens with transformed vertices */
762     if (stream_info.position_transformed) FIXME("Transformed position in rectpatch generation\n");
763
764     if(vtxStride % sizeof(GLfloat)) {
765         /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
766          * I don't see how the stride could not be a multiple of 4, but make sure
767          * to check it
768          */
769         ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
770     }
771     if(info->Basis != WINED3DBASIS_BEZIER) {
772         FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
773     }
774     if(info->Degree != WINED3DDEGREE_CUBIC) {
775         FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
776     }
777
778     /* First, get the boundary cube of the input data */
779     for(j = 0; j < info->Height; j++) {
780         for(i = 0; i < info->Width; i++) {
781             const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
782             if(fabs(v[0]) > max_x) max_x = fabs(v[0]);
783             if(fabs(v[1]) > max_y) max_y = fabs(v[1]);
784             if(fabs(v[2]) > max_z) max_z = fabs(v[2]);
785             if(v[2] < neg_z) neg_z = v[2];
786         }
787     }
788
789     /* This needs some improvements in the vertex decl code */
790     FIXME("Cannot find data to generate. Only generating position and normals\n");
791     patch->has_normals = TRUE;
792     patch->has_texcoords = FALSE;
793
794     /* Simply activate the context for blitting. This disables all the things we don't want and
795      * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
796      * patch (as opposed to normal draws) will most likely need different changes anyway
797      */
798     ActivateContext(This, This->lastActiveRenderTarget, CTXUSAGE_BLIT);
799     ENTER_GL();
800
801     glMatrixMode(GL_PROJECTION);
802     checkGLcall("glMatrixMode(GL_PROJECTION)");
803     glLoadIdentity();
804     checkGLcall("glLoadIndentity()");
805     glScalef(1 / (max_x) , 1 / (max_y), max_z == 0 ? 1 : 1 / ( 2 * max_z));
806     glTranslatef(0, 0, 0.5);
807     checkGLcall("glScalef");
808     glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
809     checkGLcall("glViewport");
810
811     /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
812      * our feedback buffer parser
813      */
814     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
815     checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
816     IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
817     if(patch->has_normals) {
818         static const GLfloat black[] = {0, 0, 0, 0};
819         static const GLfloat red[]   = {1, 0, 0, 0};
820         static const GLfloat green[] = {0, 1, 0, 0};
821         static const GLfloat blue[]  = {0, 0, 1, 0};
822         static const GLfloat white[] = {1, 1, 1, 1};
823         glEnable(GL_LIGHTING);
824         checkGLcall("glEnable(GL_LIGHTING)");
825         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
826         checkGLcall("glLightModel for MODEL_AMBIENT");
827         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
828
829         for(i = 3; i < GL_LIMITS(lights); i++) {
830             glDisable(GL_LIGHT0 + i);
831             checkGLcall("glDisable(GL_LIGHT0 + i)");
832             IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
833         }
834
835         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
836         glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
837         glLightfv(GL_LIGHT0, GL_SPECULAR, black);
838         glLightfv(GL_LIGHT0, GL_AMBIENT, black);
839         glLightfv(GL_LIGHT0, GL_POSITION, red);
840         glEnable(GL_LIGHT0);
841         checkGLcall("Setting up light 1\n");
842         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
843         glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
844         glLightfv(GL_LIGHT1, GL_SPECULAR, black);
845         glLightfv(GL_LIGHT1, GL_AMBIENT, black);
846         glLightfv(GL_LIGHT1, GL_POSITION, green);
847         glEnable(GL_LIGHT1);
848         checkGLcall("Setting up light 2\n");
849         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
850         glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
851         glLightfv(GL_LIGHT2, GL_SPECULAR, black);
852         glLightfv(GL_LIGHT2, GL_AMBIENT, black);
853         glLightfv(GL_LIGHT2, GL_POSITION, blue);
854         glEnable(GL_LIGHT2);
855         checkGLcall("Setting up light 3\n");
856
857         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
858         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
859         glDisable(GL_COLOR_MATERIAL);
860         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
861         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
862         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
863         checkGLcall("Setting up materials\n");
864     }
865
866     /* Enable the needed maps.
867      * GL_MAP2_VERTEX_3 is needed for positional data.
868      * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
869      * GL_MAP2_TEXTURE_COORD_4 for texture coords
870      */
871     num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
872     out_vertex_size = 3 /* position */;
873     d3d_out_vertex_size = 3;
874     glEnable(GL_MAP2_VERTEX_3);
875     if(patch->has_normals && patch->has_texcoords) {
876         FIXME("Texcoords not handled yet\n");
877         feedback_type = GL_3D_COLOR_TEXTURE;
878         out_vertex_size += 8;
879         d3d_out_vertex_size += 7;
880         glEnable(GL_AUTO_NORMAL);
881         glEnable(GL_MAP2_TEXTURE_COORD_4);
882     } else if(patch->has_texcoords) {
883         FIXME("Texcoords not handled yet\n");
884         feedback_type = GL_3D_COLOR_TEXTURE;
885         out_vertex_size += 7;
886         d3d_out_vertex_size += 4;
887         glEnable(GL_MAP2_TEXTURE_COORD_4);
888     } else if(patch->has_normals) {
889         feedback_type = GL_3D_COLOR;
890         out_vertex_size += 4;
891         d3d_out_vertex_size += 3;
892         glEnable(GL_AUTO_NORMAL);
893     } else {
894         feedback_type = GL_3D;
895     }
896     checkGLcall("glEnable vertex attrib generation");
897
898     buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
899                    + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
900     feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
901
902     glMap2f(GL_MAP2_VERTEX_3,
903             0, 1, vtxStride / sizeof(float), info->Width,
904             0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
905             (const GLfloat *)data);
906     checkGLcall("glMap2f");
907     if(patch->has_texcoords) {
908         glMap2f(GL_MAP2_TEXTURE_COORD_4,
909                 0, 1, vtxStride / sizeof(float), info->Width,
910                 0, 1, info->Stride * vtxStride / sizeof(float), info->Height,
911                 (const GLfloat *)data);
912         checkGLcall("glMap2f");
913     }
914     glMapGrid2f(ceilf(patch->numSegs[0]), 0.0, 1.0, ceilf(patch->numSegs[1]), 0.0, 1.0);
915     checkGLcall("glMapGrid2f");
916
917     glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
918     checkGLcall("glFeedbackBuffer");
919     glRenderMode(GL_FEEDBACK);
920
921     glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
922     checkGLcall("glEvalMesh2\n");
923
924     i = glRenderMode(GL_RENDER);
925     if(i == -1) {
926         LEAVE_GL();
927         ERR("Feedback failed. Expected %d elements back\n", buffer_size);
928         Sleep(10000);
929         HeapFree(GetProcessHeap(), 0, feedbuffer);
930         return WINED3DERR_DRIVERINTERNALERROR;
931     } else if(i != buffer_size) {
932         LEAVE_GL();
933         ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
934         Sleep(10000);
935         HeapFree(GetProcessHeap(), 0, feedbuffer);
936         return WINED3DERR_DRIVERINTERNALERROR;
937     } else {
938         TRACE("Got %d elements as expected\n", i);
939     }
940
941     HeapFree(GetProcessHeap(), 0, patch->mem);
942     patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
943     i = 0;
944     for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
945         if(feedbuffer[j] != GL_POLYGON_TOKEN) {
946             ERR("Unexpected token: %f\n", feedbuffer[j]);
947             continue;
948         }
949         if(feedbuffer[j + 1] != 3) {
950             ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
951             continue;
952         }
953         /* Somehow there are different ideas about back / front facing, so fix up the
954          * vertex order
955          */
956         patch->mem[i + 0] =  feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
957         patch->mem[i + 1] =  feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
958         patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5) * 4 * max_z; /* z, triangle 3 */
959         if(patch->has_normals) {
960             patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
961             patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
962             patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
963         }
964         i += d3d_out_vertex_size;
965
966         patch->mem[i + 0] =  feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
967         patch->mem[i + 1] =  feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
968         patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5) * 4 * max_z; /* z, triangle 2 */
969         if(patch->has_normals) {
970             patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
971             patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
972             patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
973         }
974         i += d3d_out_vertex_size;
975
976         patch->mem[i + 0] =  feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
977         patch->mem[i + 1] =  feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
978         patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5) * 4 * max_z; /* z, triangle 1 */
979         if(patch->has_normals) {
980             patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
981             patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
982             patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
983         }
984         i += d3d_out_vertex_size;
985     }
986
987     if(patch->has_normals) {
988         /* Now do the same with reverse light directions */
989         static const GLfloat x[] = {-1,  0,  0, 0};
990         static const GLfloat y[] = { 0, -1,  0, 0};
991         static const GLfloat z[] = { 0,  0, -1, 0};
992         glLightfv(GL_LIGHT0, GL_POSITION, x);
993         glLightfv(GL_LIGHT1, GL_POSITION, y);
994         glLightfv(GL_LIGHT2, GL_POSITION, z);
995         checkGLcall("Setting up reverse light directions\n");
996
997         glRenderMode(GL_FEEDBACK);
998         checkGLcall("glRenderMode(GL_FEEDBACK)");
999         glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1000         checkGLcall("glEvalMesh2\n");
1001         i = glRenderMode(GL_RENDER);
1002         checkGLcall("glRenderMode(GL_RENDER)");
1003
1004         i = 0;
1005         for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1006             if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1007                 ERR("Unexpected token: %f\n", feedbuffer[j]);
1008                 continue;
1009             }
1010             if(feedbuffer[j + 1] != 3) {
1011                 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1012                 continue;
1013             }
1014             if(patch->mem[i + 3] == 0.0)
1015                 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1016             if(patch->mem[i + 4] == 0.0)
1017                 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1018             if(patch->mem[i + 5] == 0.0)
1019                 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1020             normalize_normal(patch->mem + i + 3);
1021             i += d3d_out_vertex_size;
1022
1023             if(patch->mem[i + 3] == 0.0)
1024                 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1025             if(patch->mem[i + 4] == 0.0)
1026                 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1027             if(patch->mem[i + 5] == 0.0)
1028                 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1029             normalize_normal(patch->mem + i + 3);
1030             i += d3d_out_vertex_size;
1031
1032             if(patch->mem[i + 3] == 0.0)
1033                 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1034             if(patch->mem[i + 4] == 0.0)
1035                 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1036             if(patch->mem[i + 5] == 0.0)
1037                 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1038             normalize_normal(patch->mem + i + 3);
1039             i += d3d_out_vertex_size;
1040         }
1041     }
1042
1043     glDisable(GL_MAP2_VERTEX_3);
1044     glDisable(GL_AUTO_NORMAL);
1045     glDisable(GL_MAP2_NORMAL);
1046     glDisable(GL_MAP2_TEXTURE_COORD_4);
1047     checkGLcall("glDisable vertex attrib generation");
1048     LEAVE_GL();
1049
1050     HeapFree(GetProcessHeap(), 0, feedbuffer);
1051
1052     vtxStride = 3 * sizeof(float);
1053     if(patch->has_normals) {
1054         vtxStride += 3 * sizeof(float);
1055     }
1056     if(patch->has_texcoords) {
1057         vtxStride += 4 * sizeof(float);
1058     }
1059     memset(&patch->strided, 0, sizeof(&patch->strided));
1060     patch->strided.position.format = WINED3DFMT_R32G32B32_FLOAT;
1061     patch->strided.position.lpData = (BYTE *) patch->mem;
1062     patch->strided.position.dwStride = vtxStride;
1063
1064     if(patch->has_normals) {
1065         patch->strided.normal.format = WINED3DFMT_R32G32B32_FLOAT;
1066         patch->strided.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1067         patch->strided.normal.dwStride = vtxStride;
1068     }
1069     if(patch->has_texcoords) {
1070         patch->strided.texCoords[0].format = WINED3DFMT_R32G32B32A32_FLOAT;
1071         patch->strided.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1072         if(patch->has_normals) {
1073             patch->strided.texCoords[0].lpData += 3 * sizeof(float);
1074         }
1075         patch->strided.texCoords[0].dwStride = vtxStride;
1076     }
1077
1078     return WINED3D_OK;
1079 }