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