wined3d: Factor out draw_indirect() function.
[wine.git] / dlls / wined3d / drawprim.c
blob690057841038407891aeaddb4557052950d13eb0
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);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d);
36 /* Context activation is done by the caller. */
37 static void draw_primitive_arrays(struct wined3d_context *context, const struct wined3d_state *state,
38 const void *idx_data, unsigned int idx_size, int base_vertex_idx, unsigned int start_idx,
39 unsigned int count, unsigned int start_instance, unsigned int instance_count)
41 const struct wined3d_ffp_attrib_ops *ops = &context->d3d_info->ffp_attrib_ops;
42 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
43 const struct wined3d_stream_info *si = &context->stream_info;
44 unsigned int instanced_elements[ARRAY_SIZE(si->elements)];
45 const struct wined3d_gl_info *gl_info = context->gl_info;
46 unsigned int instanced_element_count = 0;
47 unsigned int i, j;
49 if (!instance_count)
51 if (!idx_size)
53 gl_info->gl_ops.gl.p_glDrawArrays(state->gl_primitive_type, start_idx, count);
54 checkGLcall("glDrawArrays");
55 return;
58 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
60 GL_EXTCALL(glDrawElementsBaseVertex(state->gl_primitive_type, count, idx_type,
61 (const char *)idx_data + (idx_size * start_idx), base_vertex_idx));
62 checkGLcall("glDrawElementsBaseVertex");
63 return;
66 gl_info->gl_ops.gl.p_glDrawElements(state->gl_primitive_type, count,
67 idx_type, (const char *)idx_data + (idx_size * start_idx));
68 checkGLcall("glDrawElements");
69 return;
72 if (start_instance && !(gl_info->supported[ARB_BASE_INSTANCE] && gl_info->supported[ARB_INSTANCED_ARRAYS]))
73 FIXME("Start instance (%u) not supported.\n", start_instance);
75 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
77 if (!idx_size)
79 if (gl_info->supported[ARB_BASE_INSTANCE])
81 GL_EXTCALL(glDrawArraysInstancedBaseInstance(state->gl_primitive_type, start_idx, count, instance_count, start_instance));
82 checkGLcall("glDrawArraysInstancedBaseInstance");
83 return;
86 GL_EXTCALL(glDrawArraysInstanced(state->gl_primitive_type, start_idx, count, instance_count));
87 checkGLcall("glDrawArraysInstanced");
88 return;
91 if (gl_info->supported[ARB_BASE_INSTANCE])
93 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(state->gl_primitive_type, count, idx_type,
94 (const char *)idx_data + (idx_size * start_idx), instance_count, base_vertex_idx, start_instance));
95 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
96 return;
98 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
100 GL_EXTCALL(glDrawElementsInstancedBaseVertex(state->gl_primitive_type, count, idx_type,
101 (const char *)idx_data + (idx_size * start_idx), instance_count, base_vertex_idx));
102 checkGLcall("glDrawElementsInstancedBaseVertex");
103 return;
106 GL_EXTCALL(glDrawElementsInstanced(state->gl_primitive_type, count, idx_type,
107 (const char *)idx_data + (idx_size * start_idx), instance_count));
108 checkGLcall("glDrawElementsInstanced");
109 return;
112 /* Instancing emulation by mixing immediate mode and arrays. */
114 /* This is a nasty thing. MSDN says no hardware supports this and
115 * applications have to use software vertex processing. We don't support
116 * this for now.
118 * Shouldn't be too hard to support with OpenGL, in theory just call
119 * glDrawArrays() instead of drawElements(). But the stream fequency value
120 * has a different meaning in that situation. */
121 if (!idx_size)
123 FIXME("Non-indexed instanced drawing is not supported\n");
124 return;
127 for (i = 0; i < ARRAY_SIZE(si->elements); ++i)
129 if (!(si->use_map & (1u << i)))
130 continue;
132 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
133 instanced_elements[instanced_element_count++] = i;
136 for (i = 0; i < instance_count; ++i)
138 /* Specify the instanced attributes using immediate mode calls. */
139 for (j = 0; j < instanced_element_count; ++j)
141 const struct wined3d_stream_info_element *element;
142 unsigned int element_idx;
143 const BYTE *ptr;
145 element_idx = instanced_elements[j];
146 element = &si->elements[element_idx];
147 ptr = element->data.addr + element->stride * i;
148 if (element->data.buffer_object)
149 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(state->streams[element->stream_idx].buffer, context);
150 ops->generic[element->format->emit_idx](element_idx, ptr);
153 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
155 GL_EXTCALL(glDrawElementsBaseVertex(state->gl_primitive_type, count, idx_type,
156 (const char *)idx_data + (idx_size * start_idx), base_vertex_idx));
157 checkGLcall("glDrawElementsBaseVertex");
159 else
161 gl_info->gl_ops.gl.p_glDrawElements(state->gl_primitive_type, count, idx_type,
162 (const char *)idx_data + (idx_size * start_idx));
163 checkGLcall("glDrawElements");
168 static unsigned int get_stride_idx(const void *idx_data, unsigned int idx_size,
169 unsigned int base_vertex_idx, unsigned int start_idx, unsigned int vertex_idx)
171 if (!idx_data)
172 return start_idx + vertex_idx;
173 if (idx_size == 2)
174 return ((const WORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
175 return ((const DWORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
178 /* Context activation is done by the caller. */
179 static void draw_primitive_immediate_mode(struct wined3d_context *context, const struct wined3d_state *state,
180 const struct wined3d_stream_info *si, const void *idx_data, unsigned int idx_size,
181 int base_vertex_idx, unsigned int start_idx, unsigned int vertex_count, unsigned int instance_count)
183 const BYTE *position = NULL, *normal = NULL, *diffuse = NULL, *specular = NULL;
184 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
185 unsigned int coord_idx, stride_idx, texture_idx, vertex_idx;
186 const struct wined3d_gl_info *gl_info = context->gl_info;
187 const struct wined3d_stream_info_element *element;
188 const BYTE *tex_coords[WINED3DDP_MAXTEXCOORD];
189 unsigned int texture_unit, texture_stages;
190 const struct wined3d_ffp_attrib_ops *ops;
191 unsigned int untracked_material_count;
192 unsigned int tex_mask = 0;
193 BOOL specular_fog = FALSE;
194 BOOL ps = use_ps(state);
195 const void *ptr;
197 static unsigned int once;
199 if (!once++)
200 FIXME_(d3d_perf)("Drawing using immediate mode.\n");
201 else
202 WARN_(d3d_perf)("Drawing using immediate mode.\n");
204 if (!idx_size && idx_data)
205 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
207 if (instance_count)
208 FIXME("Instancing not implemented.\n");
210 /* Immediate mode drawing can't make use of indices in a VBO - get the
211 * data from the index buffer. */
212 if (idx_size)
213 idx_data = wined3d_buffer_load_sysmem(state->index_buffer, context) + state->index_offset;
215 ops = &d3d_info->ffp_attrib_ops;
217 gl_info->gl_ops.gl.p_glBegin(state->gl_primitive_type);
219 if (use_vs(state) || d3d_info->ffp_generic_attributes)
221 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
223 unsigned int use_map = si->use_map;
224 unsigned int element_idx;
226 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
227 for (element_idx = MAX_ATTRIBS - 1; use_map; use_map &= ~(1u << element_idx), --element_idx)
229 if (!(use_map & 1u << element_idx))
230 continue;
232 ptr = si->elements[element_idx].data.addr + si->elements[element_idx].stride * stride_idx;
233 ops->generic[si->elements[element_idx].format->emit_idx](element_idx, ptr);
237 gl_info->gl_ops.gl.p_glEnd();
238 return;
241 if (si->use_map & (1u << WINED3D_FFP_POSITION))
242 position = si->elements[WINED3D_FFP_POSITION].data.addr;
244 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
245 normal = si->elements[WINED3D_FFP_NORMAL].data.addr;
246 else
247 gl_info->gl_ops.gl.p_glNormal3f(0.0f, 0.0f, 0.0f);
249 untracked_material_count = context->num_untracked_materials;
250 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
252 element = &si->elements[WINED3D_FFP_DIFFUSE];
253 diffuse = element->data.addr;
255 if (untracked_material_count && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
256 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element->format->id));
258 else
260 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
263 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
265 element = &si->elements[WINED3D_FFP_SPECULAR];
266 specular = element->data.addr;
268 /* Special case where the fog density is stored in the specular alpha channel. */
269 if (state->render_states[WINED3D_RS_FOGENABLE]
270 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
271 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
272 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
274 if (gl_info->supported[EXT_FOG_COORD])
276 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
277 specular_fog = TRUE;
278 else
279 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element->format->id));
281 else
283 static unsigned int once;
285 if (!once++)
286 FIXME("Implement fog for transformed vertices in software.\n");
290 else if (gl_info->supported[EXT_SECONDARY_COLOR])
292 GL_EXTCALL(glSecondaryColor3fEXT)(0.0f, 0.0f, 0.0f);
295 texture_stages = d3d_info->limits.ffp_blend_stages;
296 for (texture_idx = 0; texture_idx < texture_stages; ++texture_idx)
298 if (!gl_info->supported[ARB_MULTITEXTURE] && texture_idx > 0)
300 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
301 continue;
304 if (!ps && !state->textures[texture_idx])
305 continue;
307 texture_unit = context->tex_unit_map[texture_idx];
308 if (texture_unit == WINED3D_UNMAPPED_STAGE)
309 continue;
311 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
312 if (coord_idx > 7)
314 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx, texture_idx);
315 continue;
318 if (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))
320 tex_coords[coord_idx] = si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].data.addr;
321 tex_mask |= (1u << texture_idx);
323 else
325 TRACE("Setting default coordinates for texture %u.\n", texture_idx);
326 if (gl_info->supported[ARB_MULTITEXTURE])
327 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_unit, 0.0f, 0.0f, 0.0f, 1.0f));
328 else
329 gl_info->gl_ops.gl.p_glTexCoord4f(0.0f, 0.0f, 0.0f, 1.0f);
333 /* Blending data and point sizes are not supported by this function. They
334 * are not supported by the fixed function pipeline at all. A FIXME for
335 * them is printed after decoding the vertex declaration. */
336 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
338 unsigned int tmp_tex_mask;
340 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
342 if (normal)
344 ptr = normal + stride_idx * si->elements[WINED3D_FFP_NORMAL].stride;
345 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptr);
348 if (diffuse)
350 ptr = diffuse + stride_idx * si->elements[WINED3D_FFP_DIFFUSE].stride;
351 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptr);
353 if (untracked_material_count)
355 struct wined3d_color color;
356 unsigned int i;
358 wined3d_color_from_d3dcolor(&color, *(const DWORD *)ptr);
359 for (i = 0; i < untracked_material_count; ++i)
361 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], &color.r);
366 if (specular)
368 ptr = specular + stride_idx * si->elements[WINED3D_FFP_SPECULAR].stride;
369 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptr);
371 if (specular_fog)
372 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD *)ptr >> 24)));
375 tmp_tex_mask = tex_mask;
376 for (texture_idx = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture_idx)
378 if (!(tmp_tex_mask & 1))
379 continue;
381 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
382 ptr = tex_coords[coord_idx] + (stride_idx * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
383 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
384 GL_TEXTURE0_ARB + context->tex_unit_map[texture_idx], ptr);
387 if (position)
389 ptr = position + stride_idx * si->elements[WINED3D_FFP_POSITION].stride;
390 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptr);
394 gl_info->gl_ops.gl.p_glEnd();
395 checkGLcall("glEnd and previous calls");
398 static void draw_indirect(struct wined3d_context *context, const struct wined3d_state *state,
399 const struct wined3d_indirect_draw_parameters *parameters, unsigned int idx_size)
401 const struct wined3d_gl_info *gl_info = context->gl_info;
402 struct wined3d_buffer *buffer = parameters->buffer;
404 wined3d_buffer_load(buffer, context, state);
405 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer->buffer_object));
407 if (idx_size)
409 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
410 if (state->index_offset)
411 FIXME("Ignoring index offset %u.\n", state->index_offset);
412 GL_EXTCALL(glDrawElementsIndirect(state->gl_primitive_type, idx_type,
413 (void *)(GLintptr)parameters->offset));
415 else
417 GL_EXTCALL(glDrawArraysIndirect(state->gl_primitive_type,
418 (void *)(GLintptr)parameters->offset));
421 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0));
423 checkGLcall("draw indirect");
426 static void remove_vbos(struct wined3d_context *context,
427 const struct wined3d_state *state, struct wined3d_stream_info *s)
429 unsigned int i;
431 for (i = 0; i < ARRAY_SIZE(s->elements); ++i)
433 struct wined3d_stream_info_element *e;
435 if (!(s->use_map & (1u << i)))
436 continue;
438 e = &s->elements[i];
439 if (e->data.buffer_object)
441 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
442 e->data.buffer_object = 0;
443 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(vb, context);
448 static BOOL use_transform_feedback(const struct wined3d_state *state)
450 const struct wined3d_shader *shader;
451 if (!(shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
452 return FALSE;
453 return shader->u.gs.so_desc.element_count;
456 static void context_pause_transform_feedback(struct wined3d_context *context, BOOL force)
458 const struct wined3d_gl_info *gl_info = context->gl_info;
460 if (!context->transform_feedback_active || context->transform_feedback_paused)
461 return;
463 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK2])
465 GL_EXTCALL(glPauseTransformFeedback());
466 checkGLcall("glPauseTransformFeedback");
467 context->transform_feedback_paused = 1;
468 return;
471 WARN("Cannot pause transform feedback operations.\n");
473 if (force)
474 context_end_transform_feedback(context);
477 static GLenum gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
479 GLenum gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
480 switch (gl_primitive_type)
482 case GL_POINTS:
483 return GL_POINTS;
485 case GL_LINE_STRIP:
486 case GL_LINE_STRIP_ADJACENCY:
487 case GL_LINES_ADJACENCY:
488 case GL_LINES:
489 return GL_LINES;
491 case GL_TRIANGLE_FAN:
492 case GL_TRIANGLE_STRIP:
493 case GL_TRIANGLE_STRIP_ADJACENCY:
494 case GL_TRIANGLES_ADJACENCY:
495 case GL_TRIANGLES:
496 return GL_TRIANGLES;
498 default:
499 return gl_primitive_type;
503 /* Routine common to the draw primitive and draw indexed primitive routines */
504 void draw_primitive(struct wined3d_device *device, const struct wined3d_state *state,
505 const struct wined3d_draw_parameters *parameters)
507 BOOL emulation = FALSE, rasterizer_discard = FALSE;
508 const struct wined3d_fb_state *fb = state->fb;
509 const struct wined3d_stream_info *stream_info;
510 struct wined3d_rendertarget_view *dsv, *rtv;
511 struct wined3d_stream_info si_emulated;
512 struct wined3d_fence *ib_fence = NULL;
513 const struct wined3d_gl_info *gl_info;
514 struct wined3d_context *context;
515 unsigned int i, idx_size = 0;
516 const void *idx_data = NULL;
518 if (!parameters->indirect && !parameters->u.direct.index_count)
519 return;
521 if (!(rtv = fb->render_targets[0]))
522 rtv = fb->depth_stencil;
523 if (rtv)
524 context = context_acquire(device, wined3d_texture_from_resource(rtv->resource), rtv->sub_resource_idx);
525 else
526 context = context_acquire(device, NULL, 0);
527 if (!context->valid)
529 context_release(context);
530 WARN("Invalid context, skipping draw.\n");
531 return;
533 gl_info = context->gl_info;
535 if (!use_transform_feedback(state))
536 context_pause_transform_feedback(context, TRUE);
538 for (i = 0; i < gl_info->limits.buffers; ++i)
540 if (!(rtv = fb->render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
541 continue;
543 if (state->render_states[WINED3D_RS_COLORWRITEENABLE])
545 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
546 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
548 else
550 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
554 if ((dsv = fb->depth_stencil))
556 /* Note that this depends on the context_acquire() call above to set
557 * context->render_offscreen properly. We don't currently take the
558 * Z-compare function into account, but we could skip loading the
559 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
560 * that we never copy the stencil data.*/
561 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
563 if (state->render_states[WINED3D_RS_ZWRITEENABLE] || state->render_states[WINED3D_RS_ZENABLE])
564 wined3d_rendertarget_view_load_location(dsv, context, location);
565 else
566 wined3d_rendertarget_view_prepare_location(dsv, context, location);
569 if (!context_apply_draw_state(context, device, state))
571 context_release(context);
572 WARN("Unable to apply draw state, skipping draw.\n");
573 return;
576 if (dsv && state->render_states[WINED3D_RS_ZWRITEENABLE])
578 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
580 wined3d_rendertarget_view_validate_location(dsv, location);
581 wined3d_rendertarget_view_invalidate_location(dsv, ~location);
584 stream_info = &context->stream_info;
586 if (parameters->indexed)
588 struct wined3d_buffer *index_buffer = state->index_buffer;
589 if (!index_buffer->buffer_object || !stream_info->all_vbo)
591 idx_data = index_buffer->resource.heap_memory;
593 else
595 ib_fence = index_buffer->fence;
596 idx_data = NULL;
598 idx_data = (const BYTE *)idx_data + state->index_offset;
600 if (state->index_format == WINED3DFMT_R16_UINT)
601 idx_size = 2;
602 else
603 idx_size = 4;
606 if (!use_vs(state))
608 if (!stream_info->position_transformed && context->num_untracked_materials
609 && state->render_states[WINED3D_RS_LIGHTING])
611 static BOOL warned;
613 if (!warned++)
614 FIXME("Using software emulation because not all material properties could be tracked.\n");
615 else
616 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
617 emulation = TRUE;
619 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
621 static BOOL warned;
623 /* Either write a pipeline replacement shader or convert the
624 * specular alpha from unsigned byte to a float in the vertex
625 * buffer. */
626 if (!warned++)
627 FIXME("Using software emulation because manual fog coordinates are provided.\n");
628 else
629 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
630 emulation = TRUE;
633 if (emulation)
635 si_emulated = context->stream_info;
636 remove_vbos(context, state, &si_emulated);
637 stream_info = &si_emulated;
641 if (use_transform_feedback(state))
643 const struct wined3d_shader *shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
645 if (shader->u.gs.so_desc.rasterizer_stream_idx == WINED3D_NO_RASTERIZER_STREAM)
647 glEnable(GL_RASTERIZER_DISCARD);
648 checkGLcall("enable rasterizer discard");
649 rasterizer_discard = TRUE;
652 if (context->transform_feedback_paused)
654 GL_EXTCALL(glResumeTransformFeedback());
655 checkGLcall("glResumeTransformFeedback");
656 context->transform_feedback_paused = 0;
658 else if (!context->transform_feedback_active)
660 GLenum mode = gl_tfb_primitive_type_from_d3d(shader->u.gs.output_type);
661 GL_EXTCALL(glBeginTransformFeedback(mode));
662 checkGLcall("glBeginTransformFeedback");
663 context->transform_feedback_active = 1;
667 if (state->gl_primitive_type == GL_PATCHES)
669 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES, state->gl_patch_vertices));
670 checkGLcall("glPatchParameteri");
673 if (parameters->indirect)
675 if (!context->use_immediate_mode_draw && !emulation)
676 draw_indirect(context, state, &parameters->u.indirect, idx_size);
677 else
678 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
680 else
682 unsigned int instance_count = parameters->u.direct.instance_count;
683 if (context->instance_count)
684 instance_count = context->instance_count;
686 if (context->use_immediate_mode_draw || emulation)
687 draw_primitive_immediate_mode(context, state, stream_info, idx_data,
688 idx_size, parameters->u.direct.base_vertex_idx,
689 parameters->u.direct.start_idx, parameters->u.direct.index_count, instance_count);
690 else
691 draw_primitive_arrays(context, state, idx_data, idx_size, parameters->u.direct.base_vertex_idx,
692 parameters->u.direct.start_idx, parameters->u.direct.index_count,
693 parameters->u.direct.start_instance, instance_count);
696 if (context->uses_uavs)
698 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
699 checkGLcall("glMemoryBarrier");
702 context_pause_transform_feedback(context, FALSE);
704 if (rasterizer_discard)
706 glDisable(GL_RASTERIZER_DISCARD);
707 checkGLcall("disable rasterizer discard");
710 if (ib_fence)
711 wined3d_fence_issue(ib_fence, device);
712 for (i = 0; i < context->buffer_fence_count; ++i)
713 wined3d_fence_issue(context->buffer_fences[i], device);
715 if (wined3d_settings.strict_draw_ordering)
716 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
718 context_release(context);
720 TRACE("Done all gl drawing.\n");
723 void dispatch_compute(struct wined3d_device *device, const struct wined3d_state *state,
724 const struct wined3d_dispatch_parameters *parameters)
726 const struct wined3d_gl_info *gl_info;
727 struct wined3d_context *context;
729 context = context_acquire(device, NULL, 0);
730 if (!context->valid)
732 context_release(context);
733 WARN("Invalid context, skipping dispatch.\n");
734 return;
736 gl_info = context->gl_info;
738 if (!gl_info->supported[ARB_COMPUTE_SHADER])
740 context_release(context);
741 FIXME("OpenGL implementation does not support compute shaders.\n");
742 return;
745 context_apply_compute_state(context, device, state);
747 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
749 context_release(context);
750 WARN("No compute shader bound, skipping dispatch.\n");
751 return;
754 if (parameters->indirect)
756 const struct wined3d_indirect_dispatch_parameters *indirect = &parameters->u.indirect;
757 struct wined3d_buffer *buffer = indirect->buffer;
759 wined3d_buffer_load(buffer, context, state);
760 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, buffer->buffer_object));
761 GL_EXTCALL(glDispatchComputeIndirect((GLintptr)indirect->offset));
762 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0));
764 else
766 const struct wined3d_direct_dispatch_parameters *direct = &parameters->u.direct;
767 GL_EXTCALL(glDispatchCompute(direct->group_count_x, direct->group_count_y, direct->group_count_z));
769 checkGLcall("dispatch compute");
771 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
772 checkGLcall("glMemoryBarrier");
774 if (wined3d_settings.strict_draw_ordering)
775 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
777 context_release(context);