wbemdisp: Added WinMGMTS object stub implementation.
[wine/wine-gecko.git] / dlls / wined3d / drawprim.c
blob8814ed90378afd84dea2e2ee12e74ddf75c8beca
1 /*
2 * WINED3D draw functions
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
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.
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.
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
27 #include "config.h"
28 #include "wine/port.h"
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_draw);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
35 #include <stdio.h>
36 #include <math.h>
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)
42 if (idx_size)
44 GLenum idxtype = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
45 if (instance_count)
47 if (!gl_info->supported[ARB_DRAW_INSTANCED])
49 FIXME("Instanced drawing not supported.\n");
51 else
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");
60 else if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
62 GL_EXTCALL(glDrawElementsBaseVertex(primitive_type, count, idxtype,
63 (const char *)idx_data + (idx_size * start_idx), base_vertex_index));
64 checkGLcall("glDrawElementsBaseVertex");
66 else
68 gl_info->gl_ops.gl.p_glDrawElements(primitive_type, count,
69 idxtype, (const char *)idx_data + (idx_size * start_idx));
70 checkGLcall("glDrawElements");
73 else
75 gl_info->gl_ops.gl.p_glDrawArrays(primitive_type, start_idx, count);
76 checkGLcall("glDrawArrays");
81 * Actually draw using the supplied information.
82 * Slower GL version which extracts info about each vertex in turn
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)
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->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 const struct wined3d_ffp_attrib_ops *ops = &d3d_info->ffp_attrib_ops;
103 UINT texture_stages = d3d_info->limits.ffp_blend_stages;
104 const struct wined3d_stream_info_element *element;
105 UINT num_untracked_materials;
106 DWORD tex_mask = 0;
108 TRACE_(d3d_perf)("Using slow vertex array code\n");
110 /* Variable Initialization */
111 if (idxSize)
113 /* Immediate mode drawing can't make use of indices in a vbo - get the
114 * data from the index buffer. If the index buffer has no vbo (not
115 * supported or other reason), or with user pointer drawing idxData
116 * will be non-NULL. */
117 if (!idxData)
118 idxData = buffer_get_sysmem(state->index_buffer, gl_info);
120 if (idxSize == 2) pIdxBufS = idxData;
121 else pIdxBufL = idxData;
122 } else if (idxData) {
123 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
124 return;
127 /* Start drawing in GL */
128 gl_info->gl_ops.gl.p_glBegin(glPrimType);
130 if (si->use_map & (1 << WINED3D_FFP_POSITION))
132 element = &si->elements[WINED3D_FFP_POSITION];
133 position = element->data.addr;
136 if (si->use_map & (1 << WINED3D_FFP_NORMAL))
138 element = &si->elements[WINED3D_FFP_NORMAL];
139 normal = element->data.addr;
141 else
143 gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
146 num_untracked_materials = context->num_untracked_materials;
147 if (si->use_map & (1 << WINED3D_FFP_DIFFUSE))
149 element = &si->elements[WINED3D_FFP_DIFFUSE];
150 diffuse = element->data.addr;
152 if (num_untracked_materials && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
153 FIXME("Implement diffuse color tracking from %s\n", debug_d3dformat(element->format->id));
155 else
157 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
160 if (si->use_map & (1 << WINED3D_FFP_SPECULAR))
162 element = &si->elements[WINED3D_FFP_SPECULAR];
163 specular = element->data.addr;
165 /* special case where the fog density is stored in the specular alpha channel */
166 if (state->render_states[WINED3D_RS_FOGENABLE]
167 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
168 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
169 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
171 if (gl_info->supported[EXT_FOG_COORD])
173 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM) specular_fog = TRUE;
174 else FIXME("Implement fog coordinates from %s\n", debug_d3dformat(element->format->id));
176 else
178 static BOOL warned;
180 if (!warned)
182 /* TODO: Use the fog table code from old ddraw */
183 FIXME("Implement fog for transformed vertices in software\n");
184 warned = TRUE;
189 else if (gl_info->supported[EXT_SECONDARY_COLOR])
191 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
194 for (textureNo = 0; textureNo < texture_stages; ++textureNo)
196 int coordIdx = state->texture_states[textureNo][WINED3D_TSS_TEXCOORD_INDEX];
197 DWORD texture_idx = device->texUnitMap[textureNo];
199 if (!gl_info->supported[ARB_MULTITEXTURE] && textureNo > 0)
201 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
202 continue;
205 if (!pixelShader && !state->textures[textureNo]) continue;
207 if (texture_idx == WINED3D_UNMAPPED_STAGE) continue;
209 if (coordIdx > 7)
211 TRACE("tex: %d - Skip tex coords, as being system generated\n", textureNo);
212 continue;
214 else if (coordIdx < 0)
216 FIXME("tex: %d - Coord index %d is less than zero, expect a crash.\n", textureNo, coordIdx);
217 continue;
220 if (si->use_map & (1 << (WINED3D_FFP_TEXCOORD0 + coordIdx)))
222 element = &si->elements[WINED3D_FFP_TEXCOORD0 + coordIdx];
223 texCoords[coordIdx] = element->data.addr;
224 tex_mask |= (1 << textureNo);
226 else
228 TRACE("tex: %d - Skipping tex coords, as no data supplied\n", textureNo);
229 if (gl_info->supported[ARB_MULTITEXTURE])
230 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
231 else
232 gl_info->gl_ops.gl.p_glTexCoord4f(0, 0, 0, 1);
236 /* We shouldn't start this function if any VBO is involved. Should I put a safety check here?
237 * Guess it's not necessary(we crash then anyway) and would only eat CPU time
240 /* For each primitive */
241 for (vx_index = 0; vx_index < NumVertexes; ++vx_index) {
242 UINT texture, tmp_tex_mask;
243 /* Blending data and Point sizes are not supported by this function. They are not supported by the fixed
244 * function pipeline at all. A Fixme for them is printed after decoding the vertex declaration
247 /* For indexed data, we need to go a few more strides in */
248 if (idxData)
250 /* Indexed so work out the number of strides to skip */
251 if (idxSize == 2)
252 SkipnStrides = pIdxBufS[startIdx + vx_index] + state->base_vertex_index;
253 else
254 SkipnStrides = pIdxBufL[startIdx + vx_index] + state->base_vertex_index;
257 tmp_tex_mask = tex_mask;
258 for (texture = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture)
260 int coord_idx;
261 const void *ptr;
262 DWORD texture_idx;
264 if (!(tmp_tex_mask & 1)) continue;
266 coord_idx = state->texture_states[texture][WINED3D_TSS_TEXCOORD_INDEX];
267 ptr = texCoords[coord_idx] + (SkipnStrides * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
269 texture_idx = device->texUnitMap[texture];
270 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
271 GL_TEXTURE0_ARB + texture_idx, ptr);
274 /* Diffuse -------------------------------- */
275 if (diffuse)
277 const void *ptrToCoords = diffuse + SkipnStrides * si->elements[WINED3D_FFP_DIFFUSE].stride;
278 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptrToCoords);
280 if (num_untracked_materials)
282 DWORD diffuseColor = ((const DWORD *)ptrToCoords)[0];
283 unsigned char i;
284 float color[4];
286 color[0] = D3DCOLOR_B_R(diffuseColor) / 255.0f;
287 color[1] = D3DCOLOR_B_G(diffuseColor) / 255.0f;
288 color[2] = D3DCOLOR_B_B(diffuseColor) / 255.0f;
289 color[3] = D3DCOLOR_B_A(diffuseColor) / 255.0f;
291 for (i = 0; i < num_untracked_materials; ++i)
293 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], color);
298 /* Specular ------------------------------- */
299 if (specular)
301 const void *ptrToCoords = specular + SkipnStrides * si->elements[WINED3D_FFP_SPECULAR].stride;
302 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptrToCoords);
304 if (specular_fog)
306 DWORD specularColor = *(const DWORD *)ptrToCoords;
307 GL_EXTCALL(glFogCoordfEXT((float) (specularColor >> 24)));
311 /* Normal -------------------------------- */
312 if (normal)
314 const void *ptrToCoords = normal + SkipnStrides * si->elements[WINED3D_FFP_NORMAL].stride;
315 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptrToCoords);
318 /* Position -------------------------------- */
319 if (position)
321 const void *ptrToCoords = position + SkipnStrides * si->elements[WINED3D_FFP_POSITION].stride;
322 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptrToCoords);
325 /* For non indexed mode, step onto next parts */
326 if (!idxData) ++SkipnStrides;
329 gl_info->gl_ops.gl.p_glEnd();
330 checkGLcall("glEnd and previous calls");
333 /* Context activation is done by the caller. */
334 static inline void send_attribute(const struct wined3d_gl_info *gl_info,
335 enum wined3d_format_id format, const UINT index, const void *ptr)
337 switch(format)
339 case WINED3DFMT_R32_FLOAT:
340 GL_EXTCALL(glVertexAttrib1fvARB(index, ptr));
341 break;
342 case WINED3DFMT_R32G32_FLOAT:
343 GL_EXTCALL(glVertexAttrib2fvARB(index, ptr));
344 break;
345 case WINED3DFMT_R32G32B32_FLOAT:
346 GL_EXTCALL(glVertexAttrib3fvARB(index, ptr));
347 break;
348 case WINED3DFMT_R32G32B32A32_FLOAT:
349 GL_EXTCALL(glVertexAttrib4fvARB(index, ptr));
350 break;
352 case WINED3DFMT_R8G8B8A8_UINT:
353 GL_EXTCALL(glVertexAttrib4ubvARB(index, ptr));
354 break;
355 case WINED3DFMT_B8G8R8A8_UNORM:
356 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
358 const DWORD *src = ptr;
359 DWORD c = *src & 0xff00ff00;
360 c |= (*src & 0xff0000) >> 16;
361 c |= (*src & 0xff) << 16;
362 GL_EXTCALL(glVertexAttrib4NubvARB(index, (GLubyte *)&c));
363 break;
365 /* else fallthrough */
366 case WINED3DFMT_R8G8B8A8_UNORM:
367 GL_EXTCALL(glVertexAttrib4NubvARB(index, ptr));
368 break;
370 case WINED3DFMT_R16G16_SINT:
371 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
372 break;
373 case WINED3DFMT_R16G16B16A16_SINT:
374 GL_EXTCALL(glVertexAttrib4svARB(index, ptr));
375 break;
377 case WINED3DFMT_R16G16_SNORM:
379 GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
380 GL_EXTCALL(glVertexAttrib4NsvARB(index, s));
381 break;
383 case WINED3DFMT_R16G16_UNORM:
385 GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
386 GL_EXTCALL(glVertexAttrib4NusvARB(index, s));
387 break;
389 case WINED3DFMT_R16G16B16A16_SNORM:
390 GL_EXTCALL(glVertexAttrib4NsvARB(index, ptr));
391 break;
392 case WINED3DFMT_R16G16B16A16_UNORM:
393 GL_EXTCALL(glVertexAttrib4NusvARB(index, ptr));
394 break;
396 case WINED3DFMT_R10G10B10A2_UINT:
397 FIXME("Unsure about WINED3DDECLTYPE_UDEC3\n");
398 /*glVertexAttrib3usvARB(instancedData[j], (GLushort *) ptr); Does not exist */
399 break;
400 case WINED3DFMT_R10G10B10A2_SNORM:
401 FIXME("Unsure about WINED3DDECLTYPE_DEC3N\n");
402 /*glVertexAttrib3NusvARB(instancedData[j], (GLushort *) ptr); Does not exist */
403 break;
405 case WINED3DFMT_R16G16_FLOAT:
406 /* Are those 16 bit floats. C doesn't have a 16 bit float type. I could read the single bits and calculate a 4
407 * byte float according to the IEEE standard
409 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
411 /* Not supported by GL_ARB_half_float_vertex */
412 GL_EXTCALL(glVertexAttrib2hvNV(index, ptr));
414 else
416 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
417 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
418 GL_EXTCALL(glVertexAttrib2fARB(index, x, y));
420 break;
421 case WINED3DFMT_R16G16B16A16_FLOAT:
422 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
424 /* Not supported by GL_ARB_half_float_vertex */
425 GL_EXTCALL(glVertexAttrib4hvNV(index, ptr));
427 else
429 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
430 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
431 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
432 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
433 GL_EXTCALL(glVertexAttrib4fARB(index, x, y, z, w));
435 break;
437 default:
438 ERR("Unexpected attribute format: %s\n", debug_d3dformat(format));
439 break;
443 /* Context activation is done by the caller. */
444 static void drawStridedSlowVs(const struct wined3d_gl_info *gl_info, const struct wined3d_state *state,
445 const struct wined3d_stream_info *si, UINT numberOfVertices, GLenum glPrimitiveType,
446 const void *idxData, UINT idxSize, UINT startIdx)
448 LONG SkipnStrides = startIdx + state->load_base_vertex_index;
449 const DWORD *pIdxBufL = NULL;
450 const WORD *pIdxBufS = NULL;
451 UINT vx_index;
452 int i;
453 const BYTE *ptr;
455 if (idxSize)
457 /* Immediate mode drawing can't make use of indices in a vbo - get the
458 * data from the index buffer. If the index buffer has no vbo (not
459 * supported or other reason), or with user pointer drawing idxData
460 * will be non-NULL. */
461 if (!idxData)
462 idxData = buffer_get_sysmem(state->index_buffer, gl_info);
464 if (idxSize == 2) pIdxBufS = idxData;
465 else pIdxBufL = idxData;
466 } else if (idxData) {
467 ERR("non-NULL idxData with 0 idxSize, this should never happen\n");
468 return;
471 /* Start drawing in GL */
472 gl_info->gl_ops.gl.p_glBegin(glPrimitiveType);
474 for (vx_index = 0; vx_index < numberOfVertices; ++vx_index)
476 if (idxData)
478 /* Indexed so work out the number of strides to skip */
479 if (idxSize == 2)
480 SkipnStrides = pIdxBufS[startIdx + vx_index] + state->load_base_vertex_index;
481 else
482 SkipnStrides = pIdxBufL[startIdx + vx_index] + state->load_base_vertex_index;
485 for (i = MAX_ATTRIBS - 1; i >= 0; i--)
487 if (!(si->use_map & (1 << i))) continue;
489 ptr = si->elements[i].data.addr + si->elements[i].stride * SkipnStrides;
491 send_attribute(gl_info, si->elements[i].format->id, i, ptr);
493 SkipnStrides++;
496 gl_info->gl_ops.gl.p_glEnd();
499 /* Context activation is done by the caller. */
500 static void drawStridedInstanced(const struct wined3d_gl_info *gl_info, const struct wined3d_state *state,
501 const struct wined3d_stream_info *si, UINT numberOfVertices, GLenum glPrimitiveType,
502 const void *idxData, UINT idxSize, UINT startIdx, UINT base_vertex_index, UINT instance_count)
504 int numInstancedAttribs = 0, j;
505 UINT instancedData[sizeof(si->elements) / sizeof(*si->elements) /* 16 */];
506 GLenum idxtype = idxSize == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
507 UINT i;
509 if (!idxSize)
511 /* This is a nasty thing. MSDN says no hardware supports that and apps have to use software vertex processing.
512 * We don't support this for now
514 * Shouldn't be too hard to support with opengl, in theory just call glDrawArrays instead of drawElements.
515 * But the StreamSourceFreq value has a different meaning in that situation.
517 FIXME("Non-indexed instanced drawing is not supported\n");
518 return;
521 for (i = 0; i < sizeof(si->elements) / sizeof(*si->elements); ++i)
523 if (!(si->use_map & (1 << i))) continue;
525 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
527 instancedData[numInstancedAttribs] = i;
528 numInstancedAttribs++;
532 for (i = 0; i < instance_count; ++i)
534 /* Specify the instanced attributes using immediate mode calls */
535 for(j = 0; j < numInstancedAttribs; j++) {
536 const BYTE *ptr = si->elements[instancedData[j]].data.addr
537 + si->elements[instancedData[j]].stride * i;
538 if (si->elements[instancedData[j]].data.buffer_object)
540 struct wined3d_buffer *vb = state->streams[si->elements[instancedData[j]].stream_idx].buffer;
541 ptr += (ULONG_PTR)buffer_get_sysmem(vb, gl_info);
544 send_attribute(gl_info, si->elements[instancedData[j]].format->id, instancedData[j], ptr);
547 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
549 GL_EXTCALL(glDrawElementsBaseVertex(glPrimitiveType, numberOfVertices, idxtype,
550 (const char *)idxData+(idxSize * startIdx), base_vertex_index));
551 checkGLcall("glDrawElementsBaseVertex");
553 else
555 gl_info->gl_ops.gl.p_glDrawElements(glPrimitiveType, numberOfVertices, idxtype,
556 (const char *)idxData + (idxSize * startIdx));
557 checkGLcall("glDrawElements");
562 static void remove_vbos(const struct wined3d_gl_info *gl_info,
563 const struct wined3d_state *state, struct wined3d_stream_info *s)
565 unsigned int i;
567 for (i = 0; i < (sizeof(s->elements) / sizeof(*s->elements)); ++i)
569 struct wined3d_stream_info_element *e;
571 if (!(s->use_map & (1 << i))) continue;
573 e = &s->elements[i];
574 if (e->data.buffer_object)
576 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
577 e->data.buffer_object = 0;
578 e->data.addr = (BYTE *)((ULONG_PTR)e->data.addr + (ULONG_PTR)buffer_get_sysmem(vb, gl_info));
583 /* Routine common to the draw primitive and draw indexed primitive routines */
584 void draw_primitive(struct wined3d_device *device, UINT start_idx, UINT index_count,
585 UINT start_instance, UINT instance_count, BOOL indexed)
587 const struct wined3d_state *state = &device->state;
588 const struct wined3d_stream_info *stream_info;
589 struct wined3d_event_query *ib_query = NULL;
590 struct wined3d_stream_info si_emulated;
591 const struct wined3d_gl_info *gl_info;
592 struct wined3d_context *context;
593 BOOL emulation = FALSE;
594 const void *idx_data = NULL;
595 UINT idx_size = 0;
596 unsigned int i;
598 if (!index_count) return;
600 if (state->render_states[WINED3D_RS_COLORWRITEENABLE])
602 /* Invalidate the back buffer memory so LockRect will read it the next time */
603 for (i = 0; i < device->adapter->gl_info.limits.buffers; ++i)
605 struct wined3d_surface *target = device->fb.render_targets[i];
606 if (target)
608 surface_load_location(target, target->draw_binding, NULL);
609 surface_modify_location(target, target->draw_binding, TRUE);
614 /* Signals other modules that a drawing is in progress and the stateblock finalized */
615 device->isInDraw = TRUE;
617 context = context_acquire(device, device->fb.render_targets[0]);
618 if (!context->valid)
620 context_release(context);
621 WARN("Invalid context, skipping draw.\n");
622 return;
624 gl_info = context->gl_info;
626 if (device->fb.depth_stencil)
628 /* Note that this depends on the context_acquire() call above to set
629 * context->render_offscreen properly. We don't currently take the
630 * Z-compare function into account, but we could skip loading the
631 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
632 * that we never copy the stencil data.*/
633 DWORD location = context->render_offscreen ? device->fb.depth_stencil->draw_binding : SFLAG_INDRAWABLE;
634 if (state->render_states[WINED3D_RS_ZWRITEENABLE] || state->render_states[WINED3D_RS_ZENABLE])
636 struct wined3d_surface *ds = device->fb.depth_stencil;
637 RECT current_rect, draw_rect, r;
639 if (!context->render_offscreen && ds != device->onscreen_depth_stencil)
640 device_switch_onscreen_ds(device, context, ds);
642 if (ds->flags & location)
643 SetRect(&current_rect, 0, 0, ds->ds_current_size.cx, ds->ds_current_size.cy);
644 else
645 SetRectEmpty(&current_rect);
647 wined3d_get_draw_rect(state, &draw_rect);
649 IntersectRect(&r, &draw_rect, &current_rect);
650 if (!EqualRect(&r, &draw_rect))
651 surface_load_ds_location(ds, context, location);
655 if (!context_apply_draw_state(context, device))
657 context_release(context);
658 WARN("Unable to apply draw state, skipping draw.\n");
659 return;
662 if (device->fb.depth_stencil && state->render_states[WINED3D_RS_ZWRITEENABLE])
664 struct wined3d_surface *ds = device->fb.depth_stencil;
665 DWORD location = context->render_offscreen ? ds->draw_binding : SFLAG_INDRAWABLE;
667 surface_modify_ds_location(ds, location, ds->ds_current_size.cx, ds->ds_current_size.cy);
670 if ((!gl_info->supported[WINED3D_GL_VERSION_2_0]
671 || !gl_info->supported[NV_POINT_SPRITE])
672 && context->render_offscreen
673 && state->render_states[WINED3D_RS_POINTSPRITEENABLE]
674 && state->gl_primitive_type == GL_POINTS)
676 FIXME("Point sprite coordinate origin switching not supported.\n");
679 stream_info = &device->stream_info;
680 if (device->instance_count)
681 instance_count = device->instance_count;
683 if (indexed)
685 struct wined3d_buffer *index_buffer = state->index_buffer;
686 if (!index_buffer->buffer_object || !stream_info->all_vbo)
687 idx_data = index_buffer->resource.allocatedMemory;
688 else
690 ib_query = index_buffer->query;
691 idx_data = NULL;
694 if (state->index_format == WINED3DFMT_R16_UINT)
695 idx_size = 2;
696 else
697 idx_size = 4;
700 if (!use_vs(state))
702 if (!stream_info->position_transformed && context->num_untracked_materials
703 && state->render_states[WINED3D_RS_LIGHTING])
705 static BOOL warned;
707 if (!warned++)
708 FIXME("Using software emulation because not all material properties could be tracked.\n");
709 else
710 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
711 emulation = TRUE;
713 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
715 static BOOL warned;
717 /* Either write a pipeline replacement shader or convert the
718 * specular alpha from unsigned byte to a float in the vertex
719 * buffer. */
720 if (!warned++)
721 FIXME("Using software emulation because manual fog coordinates are provided.\n");
722 else
723 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
724 emulation = TRUE;
727 if (emulation)
729 si_emulated = device->stream_info;
730 remove_vbos(gl_info, state, &si_emulated);
731 stream_info = &si_emulated;
735 if (device->useDrawStridedSlow || emulation)
737 /* Immediate mode drawing. */
738 if (use_vs(state))
740 static BOOL warned;
742 if (!warned++)
743 FIXME("Using immediate mode with vertex shaders for half float emulation.\n");
744 else
745 WARN_(d3d_perf)("Using immediate mode with vertex shaders for half float emulation.\n");
747 drawStridedSlowVs(gl_info, state, stream_info, index_count,
748 state->gl_primitive_type, idx_data, idx_size, start_idx);
750 else
752 drawStridedSlow(device, context, stream_info, index_count,
753 state->gl_primitive_type, idx_data, idx_size, start_idx);
756 else if (!gl_info->supported[ARB_INSTANCED_ARRAYS] && instance_count)
758 /* Instancing emulation by mixing immediate mode and arrays. */
759 drawStridedInstanced(gl_info, state, stream_info, index_count, state->gl_primitive_type,
760 idx_data, idx_size, start_idx, state->base_vertex_index, instance_count);
762 else
764 drawStridedFast(gl_info, state->gl_primitive_type, index_count, idx_size, idx_data,
765 start_idx, state->base_vertex_index, start_instance, instance_count);
768 if (ib_query)
769 wined3d_event_query_issue(ib_query, device);
770 for (i = 0; i < device->num_buffer_queries; ++i)
772 wined3d_event_query_issue(device->buffer_queries[i], device);
775 if (wined3d_settings.strict_draw_ordering)
776 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
778 context_release(context);
780 TRACE("Done all gl drawing\n");
782 /* Control goes back to the device, stateblock values may change again */
783 device->isInDraw = FALSE;