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