wined3d: Rename the device "strided_streams" field to "stream_info".
[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 "wine/port.h"
29
30 #include "wined3d_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
34
35 #include <stdio.h>
36 #include <math.h>
37
38 /* Context activation is done by the caller. */
39 static void drawStridedFast(const struct wined3d_gl_info *gl_info, GLenum primitive_type, UINT count, UINT idx_size,
40         const void *idx_data, UINT start_idx, INT base_vertex_index, UINT start_instance, UINT instance_count)
41 {
42     if (idx_size)
43     {
44         GLenum idxtype = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
45         if (instance_count)
46         {
47             if (!gl_info->supported[ARB_DRAW_INSTANCED])
48             {
49                 FIXME("Instanced drawing not supported.\n");
50             }
51             else
52             {
53                 if (start_instance)
54                     FIXME("Start instance (%u) not supported.\n", start_instance);
55                 GL_EXTCALL(glDrawElementsInstancedBaseVertex(primitive_type, count, idxtype,
56                         (const char *)idx_data + (idx_size * start_idx), instance_count, base_vertex_index));
57                 checkGLcall("glDrawElementsInstancedBaseVertex");
58             }
59         }
60         else if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
61         {
62             GL_EXTCALL(glDrawElementsBaseVertex(primitive_type, count, idxtype,
63                     (const char *)idx_data + (idx_size * start_idx), base_vertex_index));
64             checkGLcall("glDrawElementsBaseVertex");
65         }
66         else
67         {
68             gl_info->gl_ops.gl.p_glDrawElements(primitive_type, count,
69                     idxtype, (const char *)idx_data + (idx_size * start_idx));
70             checkGLcall("glDrawElements");
71         }
72     }
73     else
74     {
75         gl_info->gl_ops.gl.p_glDrawArrays(primitive_type, start_idx, count);
76         checkGLcall("glDrawArrays");
77     }
78 }
79
80 /*
81  * Actually draw using the supplied information.
82  * Slower GL version which extracts info about each vertex in turn
83  */
84
85 /* Context activation is done by the caller. */
86 static void drawStridedSlow(const struct wined3d_device *device, const struct wined3d_context *context,
87         const struct wined3d_stream_info *si, UINT NumVertexes, GLenum glPrimType,
88         const void *idxData, UINT idxSize, UINT startIdx)
89 {
90     unsigned int               textureNo    = 0;
91     const WORD                *pIdxBufS     = NULL;
92     const DWORD               *pIdxBufL     = NULL;
93     UINT vx_index;
94     const struct wined3d_state *state = &device->stateBlock->state;
95     LONG SkipnStrides = startIdx;
96     BOOL pixelShader = use_ps(state);
97     BOOL specular_fog = FALSE;
98     const BYTE *texCoords[WINED3DDP_MAXTEXCOORD];
99     const BYTE *diffuse = NULL, *specular = NULL, *normal = NULL, *position = NULL;
100     const struct wined3d_gl_info *gl_info = context->gl_info;
101     UINT texture_stages = gl_info->limits.texture_stages;
102     const struct wined3d_stream_info_element *element;
103     UINT num_untracked_materials;
104     DWORD tex_mask = 0;
105
106     TRACE_(d3d_perf)("Using slow vertex array code\n");
107
108     /* Variable Initialization */
109     if (idxSize)
110     {
111         /* Immediate mode drawing can't make use of indices in a vbo - get the
112          * data from the index buffer. If the index buffer has no vbo (not
113          * supported or other reason), or with user pointer drawing idxData
114          * will be non-NULL. */
115         if (!idxData)
116             idxData = buffer_get_sysmem(state->index_buffer, gl_info);
117
118         if (idxSize == 2) pIdxBufS = idxData;
119         else pIdxBufL = idxData;
120     } else if (idxData) {
121         ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
122         return;
123     }
124
125     /* Start drawing in GL */
126     gl_info->gl_ops.gl.p_glBegin(glPrimType);
127
128     if (si->use_map & (1 << WINED3D_FFP_POSITION))
129     {
130         element = &si->elements[WINED3D_FFP_POSITION];
131         position = element->data.addr;
132     }
133
134     if (si->use_map & (1 << WINED3D_FFP_NORMAL))
135     {
136         element = &si->elements[WINED3D_FFP_NORMAL];
137         normal = element->data.addr;
138     }
139     else
140     {
141         gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
142     }
143
144     num_untracked_materials = context->num_untracked_materials;
145     if (si->use_map & (1 << WINED3D_FFP_DIFFUSE))
146     {
147         element = &si->elements[WINED3D_FFP_DIFFUSE];
148         diffuse = element->data.addr;
149
150         if (num_untracked_materials && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
151             FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format->id));
152     }
153     else
154     {
155         gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
156     }
157
158     if (si->use_map & (1 << WINED3D_FFP_SPECULAR))
159     {
160         element = &si->elements[WINED3D_FFP_SPECULAR];
161         specular = element->data.addr;
162
163         /* special case where the fog density is stored in the specular alpha channel */
164         if (state->render_states[WINED3D_RS_FOGENABLE]
165                 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
166                     || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
167                 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
168         {
169             if (gl_info->supported[EXT_FOG_COORD])
170             {
171                 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM) specular_fog = TRUE;
172                 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format->id));
173             }
174             else
175             {
176                 static BOOL warned;
177
178                 if (!warned)
179                 {
180                     /* TODO: Use the fog table code from old ddraw */
181                     FIXME("Implement fog for transformed vertices in software\n");
182                     warned = TRUE;
183                 }
184             }
185         }
186     }
187     else if (gl_info->supported[EXT_SECONDARY_COLOR])
188     {
189         GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
190     }
191
192     for (textureNo = 0; textureNo < texture_stages; ++textureNo)
193     {
194         int coordIdx = state->texture_states[textureNo][WINED3D_TSS_TEXCOORD_INDEX];
195         DWORD texture_idx = device->texUnitMap[textureNo];
196
197         if (!gl_info->supported[ARB_MULTITEXTURE] && textureNo > 0)
198         {
199             FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
200             continue;
201         }
202
203         if (!pixelShader && !state->textures[textureNo]) continue;
204
205         if (texture_idx == WINED3D_UNMAPPED_STAGE) continue;
206
207         if (coordIdx > 7)
208         {
209             TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
210             continue;
211         }
212         else if (coordIdx < 0)
213         {
214             FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
215             continue;
216         }
217
218         if (si->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + coordIdx)))
219         {
220             element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
221             texCoords[coordIdx] = element->data.addr;
222             tex_mask |= (1 << textureNo);
223         }
224         else
225         {
226             TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
227             if (gl_info->supported[ARB_MULTITEXTURE])
228                 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
229             else
230                 gl_info->gl_ops.gl.p_glTexCoord4f(0, 0, 0, 1);
231         }
232     }
233
234     /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
235      * Guess it's not necessary(we crash then anyway) and would only eat CPU time
236      */
237
238     /* For each primitive */
239     for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
240         UINT texture, tmp_tex_mask;
241         /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
242          * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
243          */
244
245         /* For indexed data, we need to go a few more strides in */
246         if (idxData)
247         {
248             /* Indexed so work out the number of strides to skip */
249             if (idxSize == 2)
250                 SkipnStrides = pIdxBufS[startIdx + vx_index] + state->base_vertex_index;
251             else
252                 SkipnStrides = pIdxBufL[startIdx + vx_index] + state->base_vertex_index;
253         }
254
255         tmp_tex_mask = tex_mask;
256         for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
257         {
258             int coord_idx;
259             const void *ptr;
260             DWORD texture_idx;
261
262             if (!(tmp_tex_mask & 1)) continue;
263
264             coord_idx = state->texture_states[texture][WINED3D_TSS_TEXCOORD_INDEX];
265             ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
266
267             texture_idx = device->texUnitMap[texture];
268             multi_texcoord_funcs[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
269                     GL_TEXTURE0_ARB + texture_idx, ptr);
270         }
271
272         /* Diffuse -------------------------------- */
273         if (diffuse) {
274             const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
275
276             diffuse_funcs[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptrToCoords);
277             if (num_untracked_materials)
278             {
279                 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
280                 unsigned char i;
281                 float color[4];
282
283                 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0f;
284                 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0f;
285                 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0f;
286                 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0f;
287
288                 for (i = 0; i < num_untracked_materials; ++i)
289                 {
290                     gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], color);
291                 }
292             }
293         }
294
295         /* Specular ------------------------------- */
296         if (specular) {
297             const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
298
299             specular_funcs[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptrToCoords);
300
301             if (specular_fog)
302             {
303                 DWORD specularColor = *(const DWORD *)ptrToCoords;
304                 GL_EXTCALL(glFogCoordfEXT((float) (specularColor >> 24)));
305             }
306         }
307
308         /* Normal -------------------------------- */
309         if (normal)
310         {
311             const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
312             normal_funcs[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptrToCoords);
313         }
314
315         /* Position -------------------------------- */
316         if (position) {
317             const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
318             position_funcs[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptrToCoords);
319         }
320
321         /* For non indexed mode, step onto next parts */
322         if (!idxData) ++SkipnStrides;
323     }
324
325     gl_info->gl_ops.gl.p_glEnd();
326     checkGLcall("glEnd and previous calls");
327 }
328
329 /* Context activation is done by the caller. */
330 static inline void send_attribute(const struct wined3d_gl_info *gl_info,
331         enum wined3d_format_id format, const UINT index, const void *ptr)
332 {
333     switch(format)
334     {
335         case WINED3DFMT_R32_FLOAT:
336             GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
337             break;
338         case WINED3DFMT_R32G32_FLOAT:
339             GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
340             break;
341         case WINED3DFMT_R32G32B32_FLOAT:
342             GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
343             break;
344         case WINED3DFMT_R32G32B32A32_FLOAT:
345             GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
346             break;
347
348         case WINED3DFMT_R8G8B8A8_UINT:
349             GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
350             break;
351         case WINED3DFMT_B8G8R8A8_UNORM:
352             if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
353             {
354                 const DWORD *src = ptr;
355                 DWORD c = *src & 0xff00ff00;
356                 c |= (*src & 0xff0000) >> 16;
357                 c |= (*src & 0xff) << 16;
358                 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
359                 break;
360             }
361             /* else fallthrough */
362         case WINED3DFMT_R8G8B8A8_UNORM:
363             GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
364             break;
365
366         case WINED3DFMT_R16G16_SINT:
367             GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
368             break;
369         case WINED3DFMT_R16G16B16A16_SINT:
370             GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
371             break;
372
373         case WINED3DFMT_R16G16_SNORM:
374         {
375             GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
376             GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
377             break;
378         }
379         case WINED3DFMT_R16G16_UNORM:
380         {
381             GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
382             GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
383             break;
384         }
385         case WINED3DFMT_R16G16B16A16_SNORM:
386             GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
387             break;
388         case WINED3DFMT_R16G16B16A16_UNORM:
389             GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
390             break;
391
392         case WINED3DFMT_R10G10B10A2_UINT:
393             FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
394             /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
395             break;
396         case WINED3DFMT_R10G10B10A2_SNORM:
397             FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
398             /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
399             break;
400
401         case WINED3DFMT_R16G16_FLOAT:
402             /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
403              * byte float according to the IEEE standard
404              */
405             if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
406             {
407                 /* Not supported by GL_ARB_half_float_vertex */
408                 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
409             }
410             else
411             {
412                 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
413                 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
414                 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
415             }
416             break;
417         case WINED3DFMT_R16G16B16A16_FLOAT:
418             if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
419             {
420                 /* Not supported by GL_ARB_half_float_vertex */
421                 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
422             }
423             else
424             {
425                 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
426                 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
427                 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
428                 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
429                 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
430             }
431             break;
432
433         default:
434             ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
435             break;
436     }
437 }
438
439 /* Context activation is done by the caller. */
440 static void drawStridedSlowVs(const struct wined3d_gl_info *gl_info, const struct wined3d_state *state,
441         const struct wined3d_stream_info *si, UINT numberOfVertices, GLenum glPrimitiveType,
442         const void *idxData, UINT idxSize, UINT startIdx)
443 {
444     LONG SkipnStrides = startIdx + state->load_base_vertex_index;
445     const DWORD *pIdxBufL = NULL;
446     const WORD *pIdxBufS = NULL;
447     UINT vx_index;
448     int i;
449     const BYTE *ptr;
450
451     if (idxSize)
452     {
453         /* Immediate mode drawing can't make use of indices in a vbo - get the
454          * data from the index buffer. If the index buffer has no vbo (not
455          * supported or other reason), or with user pointer drawing idxData
456          * will be non-NULL. */
457         if (!idxData)
458             idxData = buffer_get_sysmem(state->index_buffer, gl_info);
459
460         if (idxSize == 2) pIdxBufS = idxData;
461         else pIdxBufL = idxData;
462     } else if (idxData) {
463         ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
464         return;
465     }
466
467     /* Start drawing in GL */
468     gl_info->gl_ops.gl.p_glBegin(glPrimitiveType);
469
470     for (vx_index = 0; vx_index < numberOfVertices; ++vx_index)
471     {
472         if (idxData)
473         {
474             /* Indexed so work out the number of strides to skip */
475             if (idxSize == 2)
476                 SkipnStrides = pIdxBufS[startIdx + vx_index] + state->load_base_vertex_index;
477             else
478                 SkipnStrides = pIdxBufL[startIdx + vx_index] + state->load_base_vertex_index;
479         }
480
481         for (i = MAX_ATTRIBS - 1; i >= 0; i--)
482         {
483             if (!(si->use_map & (1 << i))) continue;
484
485             ptr = si->elements[i].data.addr + si->elements[i].stride * SkipnStrides;
486
487             send_attribute(gl_info, si->elements[i].format->id, i, ptr);
488         }
489         SkipnStrides++;
490     }
491
492     gl_info->gl_ops.gl.p_glEnd();
493 }
494
495 /* Context activation is done by the caller. */
496 static void drawStridedInstanced(const struct wined3d_gl_info *gl_info, const struct wined3d_state *state,
497         const struct wined3d_stream_info *si, UINT numberOfVertices, GLenum glPrimitiveType,
498         const void *idxData, UINT idxSize, UINT startIdx, UINT base_vertex_index, UINT instance_count)
499 {
500     int numInstancedAttribs = 0, j;
501     UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
502     GLenum idxtype = idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
503     UINT i;
504
505     if (!idxSize)
506     {
507         /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
508          * We don't support this for now
509          *
510          * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
511          * But the StreamSourceFreq value has a different meaning in that situation.
512          */
513         FIXME("Non-indexed instanced drawing is not supported\n");
514         return;
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     for (i = 0; i < instance_count; ++i)
529     {
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.addr
533                     + si->elements[instancedData[j]].stride * i;
534             if (si->elements[instancedData[j]].data.buffer_object)
535             {
536                 struct wined3d_buffer *vb = state->streams[si->elements[instancedData[j]].stream_idx].buffer;
537                 ptr += (ULONG_PTR)buffer_get_sysmem(vb, gl_info);
538             }
539
540             send_attribute(gl_info, si->elements[instancedData[j]].format->id, instancedData[j], ptr);
541         }
542
543         if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
544         {
545             GL_EXTCALL(glDrawElementsBaseVertex(glPrimitiveType, numberOfVertices, idxtype,
546                         (const char *)idxData+(idxSize * startIdx), base_vertex_index));
547             checkGLcall("glDrawElementsBaseVertex");
548         }
549         else
550         {
551             gl_info->gl_ops.gl.p_glDrawElements(glPrimitiveType, numberOfVertices, idxtype,
552                         (const char *)idxData + (idxSize * startIdx));
553             checkGLcall("glDrawElements");
554         }
555     }
556 }
557
558 static void remove_vbos(const struct wined3d_gl_info *gl_info,
559         const struct wined3d_state *state, struct wined3d_stream_info *s)
560 {
561     unsigned int i;
562
563     for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
564     {
565         struct wined3d_stream_info_element *e;
566
567         if (!(s->use_map & (1 << i))) continue;
568
569         e = &s->elements[i];
570         if (e->data.buffer_object)
571         {
572             struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
573             e->data.buffer_object = 0;
574             e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
575         }
576     }
577 }
578
579 /* Routine common to the draw primitive and draw indexed primitive routines */
580 void draw_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count,
581         UINT start_instance, UINT instance_count, BOOL indexed)
582 {
583     const struct wined3d_state *state = &device->stateBlock->state;
584     const struct wined3d_stream_info *stream_info;
585     struct wined3d_event_query *ib_query = NULL;
586     struct wined3d_stream_info si_emulated;
587     const struct wined3d_gl_info *gl_info;
588     struct wined3d_context *context;
589     BOOL emulation = FALSE;
590     const void *idx_data = NULL;
591     UINT idx_size = 0;
592     unsigned int i;
593
594     if (!index_count) return;
595
596     if (state->render_states[WINED3D_RS_COLORWRITEENABLE])
597     {
598         /* Invalidate the back buffer memory so LockRect will read it the next time */
599         for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
600         {
601             struct wined3d_surface *target = device->fb.render_targets[i];
602             if (target)
603             {
604                 surface_load_location(target, target->draw_binding, NULL);
605                 surface_modify_location(target, target->draw_binding, TRUE);
606             }
607         }
608     }
609
610     /* Signals other modules that a drawing is in progress and the stateblock finalized */
611     device->isInDraw = TRUE;
612
613     context = context_acquire(device, device->fb.render_targets[0]);
614     if (!context->valid)
615     {
616         context_release(context);
617         WARN("Invalid context, skipping draw.\n");
618         return;
619     }
620     gl_info = context->gl_info;
621
622     if (device->fb.depth_stencil)
623     {
624         /* Note that this depends on the context_acquire() call above to set
625          * context->render_offscreen properly. We don't currently take the
626          * Z-compare function into account, but we could skip loading the
627          * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
628          * that we never copy the stencil data.*/
629         DWORD location = context->render_offscreen ? device->fb.depth_stencil->draw_binding : SFLAG_INDRAWABLE;
630         if (state->render_states[WINED3D_RS_ZWRITEENABLE] || state->render_states[WINED3D_RS_ZENABLE])
631         {
632             struct wined3d_surface *ds = device->fb.depth_stencil;
633             RECT current_rect, draw_rect, r;
634
635             if (!context->render_offscreen && ds != device->onscreen_depth_stencil)
636                 device_switch_onscreen_ds(device, context, ds);
637
638             if (ds->flags & location)
639                 SetRect(&current_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
640             else
641                 SetRectEmpty(&current_rect);
642
643             wined3d_get_draw_rect(state, &draw_rect);
644
645             IntersectRect(&r, &draw_rect, &current_rect);
646             if (!EqualRect(&r, &draw_rect))
647                 surface_load_ds_location(ds, context, location);
648         }
649     }
650
651     if (!context_apply_draw_state(context, device))
652     {
653         context_release(context);
654         WARN("Unable to apply draw state, skipping draw.\n");
655         return;
656     }
657
658     if (device->fb.depth_stencil && state->render_states[WINED3D_RS_ZWRITEENABLE])
659     {
660         struct wined3d_surface *ds = device->fb.depth_stencil;
661         DWORD location = context->render_offscreen ? ds->draw_binding : SFLAG_INDRAWABLE;
662
663         surface_modify_ds_location(ds, location, ds->ds_current_size.cx, ds->ds_current_size.cy);
664     }
665
666     if ((!gl_info->supported[WINED3D_GL_VERSION_2_0]
667             || !gl_info->supported[NV_POINT_SPRITE])
668             && context->render_offscreen
669             && state->render_states[WINED3D_RS_POINTSPRITEENABLE]
670             && state->gl_primitive_type == GL_POINTS)
671     {
672         FIXME("Point sprite coordinate origin switching not supported.\n");
673     }
674
675     stream_info = &device->stream_info;
676     if (device->instance_count)
677         instance_count = device->instance_count;
678
679     if (indexed)
680     {
681         struct wined3d_buffer *index_buffer = state->index_buffer;
682         if (!index_buffer->buffer_object || !stream_info->all_vbo)
683             idx_data = index_buffer->resource.allocatedMemory;
684         else
685         {
686             ib_query = index_buffer->query;
687             idx_data = NULL;
688         }
689
690         if (state->index_format == WINED3DFMT_R16_UINT)
691             idx_size = 2;
692         else
693             idx_size = 4;
694     }
695
696     if (!use_vs(state))
697     {
698         if (!stream_info->position_transformed && context->num_untracked_materials
699                 && state->render_states[WINED3D_RS_LIGHTING])
700         {
701             static BOOL warned;
702
703             if (!warned++)
704                 FIXME("Using software emulation because not all material properties could be tracked.\n");
705             else
706                 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
707             emulation = TRUE;
708         }
709         else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
710         {
711             static BOOL warned;
712
713             /* Either write a pipeline replacement shader or convert the
714              * specular alpha from unsigned byte to a float in the vertex
715              * buffer. */
716             if (!warned++)
717                 FIXME("Using software emulation because manual fog coordinates are provided.\n");
718             else
719                 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
720             emulation = TRUE;
721         }
722
723         if (emulation)
724         {
725             si_emulated = device->stream_info;
726             remove_vbos(gl_info, state, &si_emulated);
727             stream_info = &si_emulated;
728         }
729     }
730
731     if (device->useDrawStridedSlow || emulation)
732     {
733         /* Immediate mode drawing. */
734         if (use_vs(state))
735         {
736             static BOOL warned;
737
738             if (!warned++)
739                 FIXME("Using immediate mode with vertex shaders for half float emulation.\n");
740             else
741                 WARN_(d3d_perf)("Using immediate mode with vertex shaders for half float emulation.\n");
742
743             drawStridedSlowVs(gl_info, state, stream_info, index_count,
744                     state->gl_primitive_type, idx_data, idx_size, start_idx);
745         }
746         else
747         {
748             drawStridedSlow(device, context, stream_info, index_count,
749                     state->gl_primitive_type, idx_data, idx_size, start_idx);
750         }
751     }
752     else if (!gl_info->supported[ARB_INSTANCED_ARRAYS] && instance_count)
753     {
754         /* Instancing emulation by mixing immediate mode and arrays. */
755         drawStridedInstanced(gl_info, state, stream_info, index_count, state->gl_primitive_type,
756                 idx_data, idx_size, start_idx, state->base_vertex_index, instance_count);
757     }
758     else
759     {
760         drawStridedFast(gl_info, state->gl_primitive_type, index_count, idx_size, idx_data,
761                 start_idx, state->base_vertex_index, start_instance, instance_count);
762     }
763
764     if (ib_query)
765         wined3d_event_query_issue(ib_query, device);
766     for (i = 0; i < device->num_buffer_queries; ++i)
767     {
768         wined3d_event_query_issue(device->buffer_queries[i], device);
769     }
770
771     if (wined3d_settings.strict_draw_ordering)
772         gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
773
774     context_release(context);
775
776     TRACE("Done all gl drawing\n");
777
778     /* Control goes back to the device, stateblock values may change again */
779     device->isInDraw = FALSE;
780 }