wined3d: Adding AMD HD5670, HD5570, HD5550, HD5450 detection.
[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     if (!context_apply_draw_state(context, device))
595     {
596         context_release(context);
597         WARN("Unable to apply draw state, skipping draw.\n");
598         return;
599     }
600
601     if (device->depth_stencil)
602     {
603         /* Note that this depends on the context_acquire() call above to set
604          * context->render_offscreen properly. We don't currently take the
605          * Z-compare function into account, but we could skip loading the
606          * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
607          * that we never copy the stencil data.*/
608         DWORD location = context->render_offscreen ? SFLAG_DS_OFFSCREEN : SFLAG_DS_ONSCREEN;
609         if (state->render_states[WINED3DRS_ZWRITEENABLE] || state->render_states[WINED3DRS_ZENABLE])
610         {
611             IWineD3DSurfaceImpl *ds = device->depth_stencil;
612             RECT current_rect, draw_rect, r;
613
614             if (location == SFLAG_DS_ONSCREEN && ds != device->onscreen_depth_stencil)
615                 device_switch_onscreen_ds(device, context, ds);
616
617             if (ds->flags & location)
618                 SetRect(&current_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
619             else
620                 SetRectEmpty(&current_rect);
621
622             device_get_draw_rect(device, &draw_rect);
623
624             IntersectRect(&r, &draw_rect, &current_rect);
625             if (!EqualRect(&r, &draw_rect))
626                 surface_load_ds_location(ds, context, location);
627
628             if (state->render_states[WINED3DRS_ZWRITEENABLE])
629             {
630                 surface_modify_ds_location(ds, location, ds->ds_current_size.cx, ds->ds_current_size.cy);
631                 surface_modify_location(ds, SFLAG_INDRAWABLE, TRUE);
632             }
633         }
634     }
635
636     if ((!context->gl_info->supported[WINED3D_GL_VERSION_2_0]
637             || (!glPointParameteri && !context->gl_info->supported[NV_POINT_SPRITE]))
638             && context->render_offscreen
639             && state->render_states[WINED3DRS_POINTSPRITEENABLE]
640             && state->gl_primitive_type == GL_POINTS)
641     {
642         FIXME("Point sprite coordinate origin switching not supported.\n");
643     }
644
645     /* Ok, we will be updating the screen from here onwards so grab the lock */
646     ENTER_GL();
647     {
648         GLenum glPrimType = state->gl_primitive_type;
649         BOOL emulation = FALSE;
650         const struct wined3d_stream_info *stream_info = &device->strided_streams;
651         struct wined3d_stream_info stridedlcl;
652
653         if (!use_vs(state))
654         {
655             if (!stream_info->position_transformed && context->num_untracked_materials
656                     && state->render_states[WINED3DRS_LIGHTING])
657             {
658                 static BOOL warned;
659                 if (!warned) {
660                     FIXME("Using software emulation because not all material properties could be tracked\n");
661                     warned = TRUE;
662                 } else {
663                     TRACE("Using software emulation because not all material properties could be tracked\n");
664                 }
665                 emulation = TRUE;
666             }
667             else if (context->fog_coord && state->render_states[WINED3DRS_FOGENABLE])
668             {
669                 /* Either write a pipeline replacement shader or convert the specular alpha from unsigned byte
670                  * to a float in the vertex buffer
671                  */
672                 static BOOL warned;
673                 if (!warned) {
674                     FIXME("Using software emulation because manual fog coordinates are provided\n");
675                     warned = TRUE;
676                 } else {
677                     TRACE("Using software emulation because manual fog coordinates are provided\n");
678                 }
679                 emulation = TRUE;
680             }
681
682             if(emulation) {
683                 stream_info = &stridedlcl;
684                 memcpy(&stridedlcl, &device->strided_streams, sizeof(stridedlcl));
685                 remove_vbos(context->gl_info, state, &stridedlcl);
686             }
687         }
688
689         if (device->useDrawStridedSlow || emulation)
690         {
691             /* Immediate mode drawing */
692             if (use_vs(state))
693             {
694                 static BOOL warned;
695                 if (!warned) {
696                     FIXME("Using immediate mode with vertex shaders for half float emulation\n");
697                     warned = TRUE;
698                 } else {
699                     TRACE("Using immediate mode with vertex shaders for half float emulation\n");
700                 }
701                 drawStridedSlowVs(context->gl_info, state, stream_info,
702                         index_count, glPrimType, idxData, idxSize, StartIdx);
703             }
704             else
705             {
706                 drawStridedSlow(device, context, stream_info, index_count,
707                         glPrimType, idxData, idxSize, StartIdx);
708             }
709         }
710         else if (device->instancedDraw)
711         {
712             /* Instancing emulation with mixing immediate mode and arrays */
713             drawStridedInstanced(context->gl_info, state, stream_info,
714                     index_count, glPrimType, idxData, idxSize, StartIdx);
715         }
716         else
717         {
718             drawStridedFast(glPrimType, index_count, idxSize, idxData, StartIdx);
719         }
720     }
721
722     /* Finished updating the screen, restore lock */
723     LEAVE_GL();
724
725     for(i = 0; i < device->num_buffer_queries; ++i)
726     {
727         wined3d_event_query_issue(device->buffer_queries[i], device);
728     }
729
730     if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
731
732     context_release(context);
733
734     TRACE("Done all gl drawing\n");
735
736     /* Control goes back to the device, stateblock values may change again */
737     device->isInDraw = FALSE;
738 }
739
740 static void normalize_normal(float *n) {
741     float length = n[0] * n[0] + n[1] * n[1] + n[2] * n[2];
742     if (length == 0.0f) return;
743     length = sqrtf(length);
744     n[0] = n[0] / length;
745     n[1] = n[1] / length;
746     n[2] = n[2] / length;
747 }
748
749 /* Tesselates a high order rectangular patch into single triangles using gl evaluators
750  *
751  * The problem is that OpenGL does not offer a direct way to return the tesselated primitives,
752  * and they can't be sent off for rendering directly either. Tesselating is slow, so we want
753  * to cache the patches in a vertex buffer. But more importantly, gl can't bind generated
754  * attributes to numbered shader attributes, so we have to store them and rebind them as needed
755  * in drawprim.
756  *
757  * To read back, the opengl feedback mode is used. This creates a problem because we want
758  * untransformed, unlit vertices, but feedback runs everything through transform and lighting.
759  * Thus disable lighting and set identity matrices to get unmodified colors and positions.
760  * To overcome clipping find the biggest x, y and z values of the vertices in the patch and scale
761  * them to [-1.0;+1.0] and set the viewport up to scale them back.
762  *
763  * Normals are more tricky: Draw white vertices with 3 directional lights, and calculate the
764  * resulting colors back to the normals.
765  *
766  * NOTE: This function activates a context for blitting, modifies matrices & viewport, but
767  * does not restore it because normally a draw follows immediately afterwards. The caller is
768  * responsible of taking care that either the gl states are restored, or the context activated
769  * for drawing to reset the lastWasBlit flag.
770  */
771 HRESULT tesselate_rectpatch(IWineD3DDeviceImpl *This,
772                             struct WineD3DRectPatch *patch) {
773     unsigned int i, j, num_quads, out_vertex_size, buffer_size, d3d_out_vertex_size;
774     float max_x = 0.0f, max_y = 0.0f, max_z = 0.0f, neg_z = 0.0f;
775     struct wined3d_stream_info stream_info;
776     struct wined3d_stream_info_element *e;
777     struct wined3d_context *context;
778     const BYTE *data;
779     const WINED3DRECTPATCH_INFO *info = &patch->RectPatchInfo;
780     DWORD vtxStride;
781     GLenum feedback_type;
782     GLfloat *feedbuffer;
783
784     /* Simply activate the context for blitting. This disables all the things we don't want and
785      * takes care of dirtifying. Dirtifying is preferred over pushing / popping, since drawing the
786      * patch (as opposed to normal draws) will most likely need different changes anyway. */
787     context = context_acquire(This, NULL);
788     context_apply_blit_state(context, This);
789
790     /* First, locate the position data. This is provided in a vertex buffer in the stateblock.
791      * Beware of vbos
792      */
793     device_stream_info_from_declaration(This, FALSE, &stream_info, NULL);
794
795     e = &stream_info.elements[WINED3D_FFP_POSITION];
796     if (e->buffer_object)
797     {
798         struct wined3d_buffer *vb = This->stateBlock->state.streams[e->stream_idx].buffer;
799         e->data = (BYTE *)((ULONG_PTR)e->data + (ULONG_PTR)buffer_get_sysmem(vb, context->gl_info));
800     }
801     vtxStride = e->stride;
802     data = e->data +
803            vtxStride * info->Stride * info->StartVertexOffsetHeight +
804            vtxStride * info->StartVertexOffsetWidth;
805
806     /* Not entirely sure about what happens with transformed vertices */
807     if (stream_info.position_transformed) FIXME("Transformed position in rectpatch generation\n");
808
809     if(vtxStride % sizeof(GLfloat)) {
810         /* glMap2f reads vertex sizes in GLfloats, the d3d stride is in bytes.
811          * I don't see how the stride could not be a multiple of 4, but make sure
812          * to check it
813          */
814         ERR("Vertex stride is not a multiple of sizeof(GLfloat)\n");
815     }
816     if(info->Basis != WINED3DBASIS_BEZIER) {
817         FIXME("Basis is %s, how to handle this?\n", debug_d3dbasis(info->Basis));
818     }
819     if(info->Degree != WINED3DDEGREE_CUBIC) {
820         FIXME("Degree is %s, how to handle this?\n", debug_d3ddegree(info->Degree));
821     }
822
823     /* First, get the boundary cube of the input data */
824     for(j = 0; j < info->Height; j++) {
825         for(i = 0; i < info->Width; i++) {
826             const float *v = (const float *)(data + vtxStride * i + vtxStride * info->Stride * j);
827             if(fabs(v[0]) > max_x) max_x = fabsf(v[0]);
828             if(fabs(v[1]) > max_y) max_y = fabsf(v[1]);
829             if(fabs(v[2]) > max_z) max_z = fabsf(v[2]);
830             if(v[2] < neg_z) neg_z = v[2];
831         }
832     }
833
834     /* This needs some improvements in the vertex decl code */
835     FIXME("Cannot find data to generate. Only generating position and normals\n");
836     patch->has_normals = TRUE;
837     patch->has_texcoords = FALSE;
838
839     ENTER_GL();
840
841     glMatrixMode(GL_PROJECTION);
842     checkGLcall("glMatrixMode(GL_PROJECTION)");
843     glLoadIdentity();
844     checkGLcall("glLoadIdentity()");
845     glScalef(1.0f / (max_x), 1.0f / (max_y), max_z == 0.0f ? 1.0f : 1.0f / (2.0f * max_z));
846     glTranslatef(0.0f, 0.0f, 0.5f);
847     checkGLcall("glScalef");
848     glViewport(-max_x, -max_y, 2 * (max_x), 2 * (max_y));
849     checkGLcall("glViewport");
850
851     /* Some states to take care of. If we're in wireframe opengl will produce lines, and confuse
852      * our feedback buffer parser
853      */
854     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
855     checkGLcall("glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)");
856     IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_FILLMODE));
857     if(patch->has_normals) {
858         static const GLfloat black[] = {0.0f, 0.0f, 0.0f, 0.0f};
859         static const GLfloat red[]   = {1.0f, 0.0f, 0.0f, 0.0f};
860         static const GLfloat green[] = {0.0f, 1.0f, 0.0f, 0.0f};
861         static const GLfloat blue[]  = {0.0f, 0.0f, 1.0f, 0.0f};
862         static const GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
863         glEnable(GL_LIGHTING);
864         checkGLcall("glEnable(GL_LIGHTING)");
865         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, black);
866         checkGLcall("glLightModel for MODEL_AMBIENT");
867         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_AMBIENT));
868
869         for (i = 3; i < context->gl_info->limits.lights; ++i)
870         {
871             glDisable(GL_LIGHT0 + i);
872             checkGLcall("glDisable(GL_LIGHT0 + i)");
873             IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(i));
874         }
875
876         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(0));
877         glLightfv(GL_LIGHT0, GL_DIFFUSE, red);
878         glLightfv(GL_LIGHT0, GL_SPECULAR, black);
879         glLightfv(GL_LIGHT0, GL_AMBIENT, black);
880         glLightfv(GL_LIGHT0, GL_POSITION, red);
881         glEnable(GL_LIGHT0);
882         checkGLcall("Setting up light 1");
883         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(1));
884         glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
885         glLightfv(GL_LIGHT1, GL_SPECULAR, black);
886         glLightfv(GL_LIGHT1, GL_AMBIENT, black);
887         glLightfv(GL_LIGHT1, GL_POSITION, green);
888         glEnable(GL_LIGHT1);
889         checkGLcall("Setting up light 2");
890         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_ACTIVELIGHT(2));
891         glLightfv(GL_LIGHT2, GL_DIFFUSE, blue);
892         glLightfv(GL_LIGHT2, GL_SPECULAR, black);
893         glLightfv(GL_LIGHT2, GL_AMBIENT, black);
894         glLightfv(GL_LIGHT2, GL_POSITION, blue);
895         glEnable(GL_LIGHT2);
896         checkGLcall("Setting up light 3");
897
898         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_MATERIAL);
899         IWineD3DDeviceImpl_MarkStateDirty(This, STATE_RENDER(WINED3DRS_COLORVERTEX));
900         glDisable(GL_COLOR_MATERIAL);
901         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
902         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, black);
903         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, white);
904         checkGLcall("Setting up materials");
905     }
906
907     /* Enable the needed maps.
908      * GL_MAP2_VERTEX_3 is needed for positional data.
909      * GL_AUTO_NORMAL to generate normals from the position. Do not use GL_MAP2_NORMAL.
910      * GL_MAP2_TEXTURE_COORD_4 for texture coords
911      */
912     num_quads = ceilf(patch->numSegs[0]) * ceilf(patch->numSegs[1]);
913     out_vertex_size = 3 /* position */;
914     d3d_out_vertex_size = 3;
915     glEnable(GL_MAP2_VERTEX_3);
916     if(patch->has_normals && patch->has_texcoords) {
917         FIXME("Texcoords not handled yet\n");
918         feedback_type = GL_3D_COLOR_TEXTURE;
919         out_vertex_size += 8;
920         d3d_out_vertex_size += 7;
921         glEnable(GL_AUTO_NORMAL);
922         glEnable(GL_MAP2_TEXTURE_COORD_4);
923     } else if(patch->has_texcoords) {
924         FIXME("Texcoords not handled yet\n");
925         feedback_type = GL_3D_COLOR_TEXTURE;
926         out_vertex_size += 7;
927         d3d_out_vertex_size += 4;
928         glEnable(GL_MAP2_TEXTURE_COORD_4);
929     } else if(patch->has_normals) {
930         feedback_type = GL_3D_COLOR;
931         out_vertex_size += 4;
932         d3d_out_vertex_size += 3;
933         glEnable(GL_AUTO_NORMAL);
934     } else {
935         feedback_type = GL_3D;
936     }
937     checkGLcall("glEnable vertex attrib generation");
938
939     buffer_size = num_quads * out_vertex_size * 2 /* triangle list */ * 3 /* verts per tri */
940                    + 4 * num_quads /* 2 triangle markers per quad + num verts in tri */;
941     feedbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buffer_size * sizeof(float) * 8);
942
943     glMap2f(GL_MAP2_VERTEX_3,
944             0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
945             0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
946             (const GLfloat *)data);
947     checkGLcall("glMap2f");
948     if(patch->has_texcoords) {
949         glMap2f(GL_MAP2_TEXTURE_COORD_4,
950                 0.0f, 1.0f, vtxStride / sizeof(float), info->Width,
951                 0.0f, 1.0f, info->Stride * vtxStride / sizeof(float), info->Height,
952                 (const GLfloat *)data);
953         checkGLcall("glMap2f");
954     }
955     glMapGrid2f(ceilf(patch->numSegs[0]), 0.0f, 1.0f, ceilf(patch->numSegs[1]), 0.0f, 1.0f);
956     checkGLcall("glMapGrid2f");
957
958     glFeedbackBuffer(buffer_size * 2, feedback_type, feedbuffer);
959     checkGLcall("glFeedbackBuffer");
960     glRenderMode(GL_FEEDBACK);
961
962     glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
963     checkGLcall("glEvalMesh2");
964
965     i = glRenderMode(GL_RENDER);
966     if(i == -1) {
967         LEAVE_GL();
968         ERR("Feedback failed. Expected %d elements back\n", buffer_size);
969         HeapFree(GetProcessHeap(), 0, feedbuffer);
970         context_release(context);
971         return WINED3DERR_DRIVERINTERNALERROR;
972     } else if(i != buffer_size) {
973         LEAVE_GL();
974         ERR("Unexpected amount of elements returned. Expected %d, got %d\n", buffer_size, i);
975         HeapFree(GetProcessHeap(), 0, feedbuffer);
976         context_release(context);
977         return WINED3DERR_DRIVERINTERNALERROR;
978     } else {
979         TRACE("Got %d elements as expected\n", i);
980     }
981
982     HeapFree(GetProcessHeap(), 0, patch->mem);
983     patch->mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, num_quads * 6 * d3d_out_vertex_size * sizeof(float) * 8);
984     i = 0;
985     for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
986         if(feedbuffer[j] != GL_POLYGON_TOKEN) {
987             ERR("Unexpected token: %f\n", feedbuffer[j]);
988             continue;
989         }
990         if(feedbuffer[j + 1] != 3) {
991             ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
992             continue;
993         }
994         /* Somehow there are different ideas about back / front facing, so fix up the
995          * vertex order
996          */
997         patch->mem[i + 0] =  feedbuffer[j + out_vertex_size * 2 + 2]; /* x, triangle 2 */
998         patch->mem[i + 1] =  feedbuffer[j + out_vertex_size * 2 + 3]; /* y, triangle 2 */
999         patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 2 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 3 */
1000         if(patch->has_normals) {
1001             patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 2 + 5];
1002             patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 2 + 6];
1003             patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 2 + 7];
1004         }
1005         i += d3d_out_vertex_size;
1006
1007         patch->mem[i + 0] =  feedbuffer[j + out_vertex_size * 1 + 2]; /* x, triangle 2 */
1008         patch->mem[i + 1] =  feedbuffer[j + out_vertex_size * 1 + 3]; /* y, triangle 2 */
1009         patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 1 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 2 */
1010         if(patch->has_normals) {
1011             patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 1 + 5];
1012             patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 1 + 6];
1013             patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 1 + 7];
1014         }
1015         i += d3d_out_vertex_size;
1016
1017         patch->mem[i + 0] =  feedbuffer[j + out_vertex_size * 0 + 2]; /* x, triangle 1 */
1018         patch->mem[i + 1] =  feedbuffer[j + out_vertex_size * 0 + 3]; /* y, triangle 1 */
1019         patch->mem[i + 2] = (feedbuffer[j + out_vertex_size * 0 + 4] - 0.5f) * 4.0f * max_z; /* z, triangle 1 */
1020         if(patch->has_normals) {
1021             patch->mem[i + 3] = feedbuffer[j + out_vertex_size * 0 + 5];
1022             patch->mem[i + 4] = feedbuffer[j + out_vertex_size * 0 + 6];
1023             patch->mem[i + 5] = feedbuffer[j + out_vertex_size * 0 + 7];
1024         }
1025         i += d3d_out_vertex_size;
1026     }
1027
1028     if(patch->has_normals) {
1029         /* Now do the same with reverse light directions */
1030         static const GLfloat x[] = {-1.0f,  0.0f,  0.0f, 0.0f};
1031         static const GLfloat y[] = { 0.0f, -1.0f,  0.0f, 0.0f};
1032         static const GLfloat z[] = { 0.0f,  0.0f, -1.0f, 0.0f};
1033         glLightfv(GL_LIGHT0, GL_POSITION, x);
1034         glLightfv(GL_LIGHT1, GL_POSITION, y);
1035         glLightfv(GL_LIGHT2, GL_POSITION, z);
1036         checkGLcall("Setting up reverse light directions");
1037
1038         glRenderMode(GL_FEEDBACK);
1039         checkGLcall("glRenderMode(GL_FEEDBACK)");
1040         glEvalMesh2(GL_FILL, 0, ceilf(patch->numSegs[0]), 0, ceilf(patch->numSegs[1]));
1041         checkGLcall("glEvalMesh2");
1042         i = glRenderMode(GL_RENDER);
1043         checkGLcall("glRenderMode(GL_RENDER)");
1044
1045         i = 0;
1046         for(j = 0; j < buffer_size; j += (3 /* num verts */ * out_vertex_size + 2 /* tri marker */)) {
1047             if(feedbuffer[j] != GL_POLYGON_TOKEN) {
1048                 ERR("Unexpected token: %f\n", feedbuffer[j]);
1049                 continue;
1050             }
1051             if(feedbuffer[j + 1] != 3) {
1052                 ERR("Unexpected polygon: %f corners\n", feedbuffer[j + 1]);
1053                 continue;
1054             }
1055             if(patch->mem[i + 3] == 0.0f)
1056                 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 2 + 5];
1057             if(patch->mem[i + 4] == 0.0f)
1058                 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 2 + 6];
1059             if(patch->mem[i + 5] == 0.0f)
1060                 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 2 + 7];
1061             normalize_normal(patch->mem + i + 3);
1062             i += d3d_out_vertex_size;
1063
1064             if(patch->mem[i + 3] == 0.0f)
1065                 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 1 + 5];
1066             if(patch->mem[i + 4] == 0.0f)
1067                 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 1 + 6];
1068             if(patch->mem[i + 5] == 0.0f)
1069                 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 1 + 7];
1070             normalize_normal(patch->mem + i + 3);
1071             i += d3d_out_vertex_size;
1072
1073             if(patch->mem[i + 3] == 0.0f)
1074                 patch->mem[i + 3] = -feedbuffer[j + out_vertex_size * 0 + 5];
1075             if(patch->mem[i + 4] == 0.0f)
1076                 patch->mem[i + 4] = -feedbuffer[j + out_vertex_size * 0 + 6];
1077             if(patch->mem[i + 5] == 0.0f)
1078                 patch->mem[i + 5] = -feedbuffer[j + out_vertex_size * 0 + 7];
1079             normalize_normal(patch->mem + i + 3);
1080             i += d3d_out_vertex_size;
1081         }
1082     }
1083
1084     glDisable(GL_MAP2_VERTEX_3);
1085     glDisable(GL_AUTO_NORMAL);
1086     glDisable(GL_MAP2_NORMAL);
1087     glDisable(GL_MAP2_TEXTURE_COORD_4);
1088     checkGLcall("glDisable vertex attrib generation");
1089     LEAVE_GL();
1090
1091     context_release(context);
1092
1093     HeapFree(GetProcessHeap(), 0, feedbuffer);
1094
1095     vtxStride = 3 * sizeof(float);
1096     if(patch->has_normals) {
1097         vtxStride += 3 * sizeof(float);
1098     }
1099     if(patch->has_texcoords) {
1100         vtxStride += 4 * sizeof(float);
1101     }
1102     memset(&patch->strided, 0, sizeof(patch->strided));
1103     patch->strided.position.format = WINED3DFMT_R32G32B32_FLOAT;
1104     patch->strided.position.lpData = (BYTE *) patch->mem;
1105     patch->strided.position.dwStride = vtxStride;
1106
1107     if(patch->has_normals) {
1108         patch->strided.normal.format = WINED3DFMT_R32G32B32_FLOAT;
1109         patch->strided.normal.lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1110         patch->strided.normal.dwStride = vtxStride;
1111     }
1112     if(patch->has_texcoords) {
1113         patch->strided.texCoords[0].format = WINED3DFMT_R32G32B32A32_FLOAT;
1114         patch->strided.texCoords[0].lpData = (BYTE *) patch->mem + 3 * sizeof(float) /* pos */;
1115         if(patch->has_normals) {
1116             patch->strided.texCoords[0].lpData += 3 * sizeof(float);
1117         }
1118         patch->strided.texCoords[0].dwStride = vtxStride;
1119     }
1120
1121     return WINED3D_OK;
1122 }