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