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