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