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