wined3d: Allow wined3d_texture_upload_data() to upload to WINED3D_LOCATION_TEXTURE_SRGB.
[wine.git] / dlls / wined3d / context.c
blob34102c76784d628c3c30a5681c7a2476377dea46
1 /*
2 * Context and render target management in wined3d
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-2011, 2013 Stefan Dösinger for CodeWeavers
10 * Copyright 2009-2011 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 <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous);
41 #define WINED3D_MAX_FBO_ENTRIES 64
42 #define WINED3D_ALL_LAYERS (~0u)
44 static DWORD wined3d_context_tls_idx;
46 /* FBO helper functions */
48 /* Context activation is done by the caller. */
49 static void context_bind_fbo(struct wined3d_context *context, GLenum target, GLuint fbo)
51 const struct wined3d_gl_info *gl_info = context->gl_info;
53 switch (target)
55 case GL_READ_FRAMEBUFFER:
56 if (context->fbo_read_binding == fbo) return;
57 context->fbo_read_binding = fbo;
58 break;
60 case GL_DRAW_FRAMEBUFFER:
61 if (context->fbo_draw_binding == fbo) return;
62 context->fbo_draw_binding = fbo;
63 break;
65 case GL_FRAMEBUFFER:
66 if (context->fbo_read_binding == fbo
67 && context->fbo_draw_binding == fbo) return;
68 context->fbo_read_binding = fbo;
69 context->fbo_draw_binding = fbo;
70 break;
72 default:
73 FIXME("Unhandled target %#x.\n", target);
74 break;
77 gl_info->fbo_ops.glBindFramebuffer(target, fbo);
78 checkGLcall("glBindFramebuffer()");
81 /* Context activation is done by the caller. */
82 static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info, GLenum target)
84 unsigned int i;
86 for (i = 0; i < gl_info->limits.buffers; ++i)
88 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
89 checkGLcall("glFramebufferTexture2D()");
91 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
92 checkGLcall("glFramebufferTexture2D()");
94 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
95 checkGLcall("glFramebufferTexture2D()");
98 /* Context activation is done by the caller. */
99 static void context_destroy_fbo(struct wined3d_context *context, GLuint fbo)
101 const struct wined3d_gl_info *gl_info = context->gl_info;
103 context_bind_fbo(context, GL_FRAMEBUFFER, fbo);
104 context_clean_fbo_attachments(gl_info, GL_FRAMEBUFFER);
105 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
107 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
108 checkGLcall("glDeleteFramebuffers()");
111 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info *gl_info,
112 GLenum fbo_target, DWORD flags, GLuint rb)
114 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
116 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
117 checkGLcall("glFramebufferRenderbuffer()");
120 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
122 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
123 checkGLcall("glFramebufferRenderbuffer()");
127 static void context_attach_gl_texture_fbo(struct wined3d_context *context,
128 GLenum fbo_target, GLenum attachment, const struct wined3d_fbo_resource *resource)
130 const struct wined3d_gl_info *gl_info = context->gl_info;
132 if (!resource)
134 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment, GL_TEXTURE_2D, 0, 0);
136 else if (resource->layer == WINED3D_ALL_LAYERS)
138 if (!gl_info->fbo_ops.glFramebufferTexture)
140 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
141 return;
144 gl_info->fbo_ops.glFramebufferTexture(fbo_target, attachment,
145 resource->object, resource->level);
147 else if (resource->target == GL_TEXTURE_2D_ARRAY || resource->target == GL_TEXTURE_3D)
149 if (!gl_info->fbo_ops.glFramebufferTextureLayer)
151 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
152 return;
155 gl_info->fbo_ops.glFramebufferTextureLayer(fbo_target, attachment,
156 resource->object, resource->level, resource->layer);
158 else
160 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment,
161 resource->target, resource->object, resource->level);
163 checkGLcall("attach texture to fbo");
166 /* Context activation is done by the caller. */
167 static void context_attach_depth_stencil_fbo(struct wined3d_context *context,
168 GLenum fbo_target, const struct wined3d_fbo_resource *resource, BOOL rb_namespace,
169 DWORD flags)
171 const struct wined3d_gl_info *gl_info = context->gl_info;
173 if (resource->object)
175 TRACE("Attach depth stencil %u.\n", resource->object);
177 if (rb_namespace)
179 context_attach_depth_stencil_rb(gl_info, fbo_target,
180 flags, resource->object);
182 else
184 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
185 context_attach_gl_texture_fbo(context, fbo_target, GL_DEPTH_ATTACHMENT, resource);
187 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
188 context_attach_gl_texture_fbo(context, fbo_target, GL_STENCIL_ATTACHMENT, resource);
191 if (!(flags & WINED3D_FBO_ENTRY_FLAG_DEPTH))
192 context_attach_gl_texture_fbo(context, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
194 if (!(flags & WINED3D_FBO_ENTRY_FLAG_STENCIL))
195 context_attach_gl_texture_fbo(context, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
197 else
199 TRACE("Attach depth stencil 0.\n");
201 context_attach_gl_texture_fbo(context, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
202 context_attach_gl_texture_fbo(context, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
206 /* Context activation is done by the caller. */
207 static void context_attach_surface_fbo(struct wined3d_context *context,
208 GLenum fbo_target, DWORD idx, const struct wined3d_fbo_resource *resource, BOOL rb_namespace)
210 const struct wined3d_gl_info *gl_info = context->gl_info;
212 TRACE("Attach GL object %u to %u.\n", resource->object, idx);
214 if (resource->object)
216 if (rb_namespace)
218 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
219 GL_RENDERBUFFER, resource->object);
220 checkGLcall("glFramebufferRenderbuffer()");
222 else
224 context_attach_gl_texture_fbo(context, fbo_target, GL_COLOR_ATTACHMENT0 + idx, resource);
227 else
229 context_attach_gl_texture_fbo(context, fbo_target, GL_COLOR_ATTACHMENT0 + idx, NULL);
233 static void context_dump_fbo_attachment(const struct wined3d_gl_info *gl_info, GLenum target,
234 GLenum attachment)
236 static const struct
238 GLenum target;
239 GLenum binding;
240 const char *str;
241 enum wined3d_gl_extension extension;
243 texture_type[] =
245 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, "2d", WINED3D_GL_EXT_NONE},
246 {GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_BINDING_RECTANGLE_ARB, "rectangle", ARB_TEXTURE_RECTANGLE},
247 {GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BINDING_2D_ARRAY, "2d-array" , EXT_TEXTURE_ARRAY},
248 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, "cube", ARB_TEXTURE_CUBE_MAP},
249 {GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_BINDING_2D_MULTISAMPLE, "2d-ms", ARB_TEXTURE_MULTISAMPLE},
250 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY, "2d-array-ms", ARB_TEXTURE_MULTISAMPLE},
253 GLint type, name, samples, width, height, old_texture, level, face, fmt, tex_target;
254 const char *tex_type_str;
255 unsigned int i;
257 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
258 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &name);
259 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
260 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &type);
262 if (type == GL_RENDERBUFFER)
264 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, name);
265 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
266 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
267 if (gl_info->limits.samples > 1)
268 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
269 else
270 samples = 1;
271 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &fmt);
272 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
273 debug_fboattachment(attachment), name, width, height, samples, fmt);
275 else if (type == GL_TEXTURE)
277 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
278 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL, &level);
279 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
280 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE, &face);
282 if (gl_info->gl_ops.ext.p_glGetTextureParameteriv)
284 GL_EXTCALL(glGetTextureParameteriv(name, GL_TEXTURE_TARGET, &tex_target));
286 for (i = 0; i < ARRAY_SIZE(texture_type); ++i)
288 if (texture_type[i].target == tex_target)
290 tex_type_str = texture_type[i].str;
291 break;
294 if (i == ARRAY_SIZE(texture_type))
295 tex_type_str = wine_dbg_sprintf("%#x", tex_target);
297 else if (face)
299 gl_info->gl_ops.gl.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &old_texture);
300 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, name);
302 tex_target = GL_TEXTURE_CUBE_MAP;
303 tex_type_str = "cube";
305 else
307 tex_type_str = NULL;
309 for (i = 0; i < ARRAY_SIZE(texture_type); ++i)
311 if (!gl_info->supported[texture_type[i].extension])
312 continue;
314 gl_info->gl_ops.gl.p_glGetIntegerv(texture_type[i].binding, &old_texture);
315 while (gl_info->gl_ops.gl.p_glGetError());
317 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, name);
318 if (!gl_info->gl_ops.gl.p_glGetError())
320 tex_target = texture_type[i].target;
321 tex_type_str = texture_type[i].str;
322 break;
324 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, old_texture);
327 if (!tex_type_str)
329 FIXME("Cannot find type of texture %d.\n", name);
330 return;
334 if (gl_info->gl_ops.ext.p_glGetTextureParameteriv)
336 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt));
337 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_WIDTH, &width));
338 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_HEIGHT, &height));
339 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_SAMPLES, &samples));
341 else
343 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
344 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_WIDTH, &width);
345 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_HEIGHT, &height);
346 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
347 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_SAMPLES, &samples);
348 else
349 samples = 1;
351 gl_info->gl_ops.gl.p_glBindTexture(tex_target, old_texture);
354 FIXME(" %s: %s texture %d, %dx%d, %d samples, format %#x.\n",
355 debug_fboattachment(attachment), tex_type_str, name, width, height, samples, fmt);
357 else if (type == GL_NONE)
359 FIXME(" %s: NONE.\n", debug_fboattachment(attachment));
361 else
363 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment), type);
366 checkGLcall("dump FBO attachment");
369 /* Context activation is done by the caller. */
370 void context_check_fbo_status(const struct wined3d_context *context, GLenum target)
372 const struct wined3d_gl_info *gl_info = context->gl_info;
373 GLenum status;
375 if (!FIXME_ON(d3d))
376 return;
378 status = gl_info->fbo_ops.glCheckFramebufferStatus(target);
379 if (status == GL_FRAMEBUFFER_COMPLETE)
381 TRACE("FBO complete.\n");
383 else
385 unsigned int i;
387 FIXME("FBO status %s (%#x).\n", debug_fbostatus(status), status);
389 if (!context->current_fbo)
391 ERR("FBO 0 is incomplete, driver bug?\n");
392 return;
395 context_dump_fbo_attachment(gl_info, target, GL_DEPTH_ATTACHMENT);
396 context_dump_fbo_attachment(gl_info, target, GL_STENCIL_ATTACHMENT);
398 for (i = 0; i < gl_info->limits.buffers; ++i)
399 context_dump_fbo_attachment(gl_info, target, GL_COLOR_ATTACHMENT0 + i);
403 static inline DWORD context_generate_rt_mask(GLenum buffer)
405 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
406 return buffer ? (1u << 31) | buffer : 0;
409 static inline DWORD context_generate_rt_mask_from_resource(struct wined3d_resource *resource)
411 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
413 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
414 return 0;
417 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource));
420 static inline void context_set_fbo_key_for_render_target(const struct wined3d_context *context,
421 struct wined3d_fbo_entry_key *key, unsigned int idx, const struct wined3d_rendertarget_info *render_target,
422 DWORD location)
424 unsigned int sub_resource_idx = render_target->sub_resource_idx;
425 struct wined3d_resource *resource = render_target->resource;
426 struct wined3d_texture *texture;
428 if (!resource || resource->format->id == WINED3DFMT_NULL || resource->type == WINED3D_RTYPE_BUFFER)
430 if (resource && resource->type == WINED3D_RTYPE_BUFFER)
431 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
432 key->objects[idx].object = 0;
433 key->objects[idx].target = 0;
434 key->objects[idx].level = key->objects[idx].layer = 0;
435 return;
438 if (render_target->gl_view.name)
440 key->objects[idx].object = render_target->gl_view.name;
441 key->objects[idx].target = render_target->gl_view.target;
442 key->objects[idx].level = 0;
443 key->objects[idx].layer = WINED3D_ALL_LAYERS;
444 return;
447 texture = wined3d_texture_from_resource(resource);
448 if (texture->current_renderbuffer)
450 key->objects[idx].object = texture->current_renderbuffer->id;
451 key->objects[idx].target = 0;
452 key->objects[idx].level = key->objects[idx].layer = 0;
453 key->rb_namespace |= 1 << idx;
454 return;
457 key->objects[idx].target = wined3d_texture_get_sub_resource_target(texture, sub_resource_idx);
458 key->objects[idx].level = sub_resource_idx % texture->level_count;
459 key->objects[idx].layer = sub_resource_idx / texture->level_count;
461 if (render_target->layer_count != 1)
462 key->objects[idx].layer = WINED3D_ALL_LAYERS;
464 switch (location)
466 case WINED3D_LOCATION_TEXTURE_RGB:
467 key->objects[idx].object = wined3d_texture_get_texture_name(texture, context, FALSE);
468 break;
470 case WINED3D_LOCATION_TEXTURE_SRGB:
471 key->objects[idx].object = wined3d_texture_get_texture_name(texture, context, TRUE);
472 break;
474 case WINED3D_LOCATION_RB_MULTISAMPLE:
475 key->objects[idx].object = texture->rb_multisample;
476 key->objects[idx].target = 0;
477 key->objects[idx].level = key->objects[idx].layer = 0;
478 key->rb_namespace |= 1 << idx;
479 break;
481 case WINED3D_LOCATION_RB_RESOLVED:
482 key->objects[idx].object = texture->rb_resolved;
483 key->objects[idx].target = 0;
484 key->objects[idx].level = key->objects[idx].layer = 0;
485 key->rb_namespace |= 1 << idx;
486 break;
490 static void context_generate_fbo_key(const struct wined3d_context *context,
491 struct wined3d_fbo_entry_key *key, const struct wined3d_rendertarget_info *render_targets,
492 const struct wined3d_rendertarget_info *depth_stencil, DWORD color_location, DWORD ds_location)
494 unsigned int buffers = context->gl_info->limits.buffers;
495 unsigned int i;
497 key->rb_namespace = 0;
498 context_set_fbo_key_for_render_target(context, key, 0, depth_stencil, ds_location);
500 for (i = 0; i < buffers; ++i)
501 context_set_fbo_key_for_render_target(context, key, i + 1, &render_targets[i], color_location);
503 memset(&key->objects[buffers + 1], 0, (ARRAY_SIZE(key->objects) - buffers - 1) * sizeof(*key->objects));
506 static struct fbo_entry *context_create_fbo_entry(const struct wined3d_context *context,
507 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
508 DWORD color_location, DWORD ds_location)
510 const struct wined3d_gl_info *gl_info = context->gl_info;
511 struct fbo_entry *entry;
513 entry = heap_alloc(sizeof(*entry));
514 context_generate_fbo_key(context, &entry->key, render_targets, depth_stencil, color_location, ds_location);
515 entry->flags = 0;
516 if (depth_stencil->resource)
518 if (depth_stencil->resource->format_flags & WINED3DFMT_FLAG_DEPTH)
519 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
520 if (depth_stencil->resource->format_flags & WINED3DFMT_FLAG_STENCIL)
521 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
523 entry->rt_mask = context_generate_rt_mask(GL_COLOR_ATTACHMENT0);
524 gl_info->fbo_ops.glGenFramebuffers(1, &entry->id);
525 checkGLcall("glGenFramebuffers()");
526 TRACE("Created FBO %u.\n", entry->id);
528 return entry;
531 /* Context activation is done by the caller. */
532 static void context_reuse_fbo_entry(struct wined3d_context *context, GLenum target,
533 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
534 DWORD color_location, DWORD ds_location, struct fbo_entry *entry)
536 const struct wined3d_gl_info *gl_info = context->gl_info;
538 context_bind_fbo(context, target, entry->id);
539 context_clean_fbo_attachments(gl_info, target);
541 context_generate_fbo_key(context, &entry->key, render_targets, depth_stencil, color_location, ds_location);
542 entry->flags = 0;
543 if (depth_stencil->resource)
545 if (depth_stencil->resource->format_flags & WINED3DFMT_FLAG_DEPTH)
546 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
547 if (depth_stencil->resource->format_flags & WINED3DFMT_FLAG_STENCIL)
548 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
552 /* Context activation is done by the caller. */
553 static void context_destroy_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
555 if (entry->id)
557 TRACE("Destroy FBO %u.\n", entry->id);
558 context_destroy_fbo(context, entry->id);
560 --context->fbo_entry_count;
561 list_remove(&entry->entry);
562 heap_free(entry);
565 /* Context activation is done by the caller. */
566 static struct fbo_entry *context_find_fbo_entry(struct wined3d_context *context, GLenum target,
567 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
568 DWORD color_location, DWORD ds_location)
570 static const struct wined3d_rendertarget_info ds_null = {{0}};
571 const struct wined3d_gl_info *gl_info = context->gl_info;
572 struct wined3d_texture *rt_texture, *ds_texture;
573 struct wined3d_fbo_entry_key fbo_key;
574 unsigned int i, ds_level, rt_level;
575 struct fbo_entry *entry;
577 if (depth_stencil->resource && depth_stencil->resource->type != WINED3D_RTYPE_BUFFER
578 && render_targets[0].resource && render_targets[0].resource->type != WINED3D_RTYPE_BUFFER)
580 rt_texture = wined3d_texture_from_resource(render_targets[0].resource);
581 rt_level = render_targets[0].sub_resource_idx % rt_texture->level_count;
582 ds_texture = wined3d_texture_from_resource(depth_stencil->resource);
583 ds_level = depth_stencil->sub_resource_idx % ds_texture->level_count;
585 if (wined3d_texture_get_level_width(ds_texture, ds_level)
586 < wined3d_texture_get_level_width(rt_texture, rt_level)
587 || wined3d_texture_get_level_height(ds_texture, ds_level)
588 < wined3d_texture_get_level_height(rt_texture, rt_level))
590 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
591 depth_stencil = &ds_null;
593 else if (ds_texture->resource.multisample_type != rt_texture->resource.multisample_type
594 || ds_texture->resource.multisample_quality != rt_texture->resource.multisample_quality)
596 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
597 rt_texture->resource.multisample_type, rt_texture->resource.multisample_quality,
598 ds_texture->resource.multisample_type, ds_texture->resource.multisample_quality);
599 depth_stencil = &ds_null;
601 else if (depth_stencil->resource->type == WINED3D_RTYPE_TEXTURE_2D)
603 wined3d_texture_set_compatible_renderbuffer(ds_texture, ds_level, &render_targets[0]);
607 context_generate_fbo_key(context, &fbo_key, render_targets, depth_stencil, color_location, ds_location);
609 if (TRACE_ON(d3d))
611 struct wined3d_resource *resource;
612 unsigned int width, height;
613 const char *resource_type;
615 TRACE("Dumping FBO attachments:\n");
616 for (i = 0; i < gl_info->limits.buffers; ++i)
618 if ((resource = render_targets[i].resource))
620 if (resource->type == WINED3D_RTYPE_BUFFER)
622 width = resource->size;
623 height = 1;
624 resource_type = "buffer";
626 else
628 rt_texture = wined3d_texture_from_resource(resource);
629 rt_level = render_targets[i].sub_resource_idx % rt_texture->level_count;
630 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
631 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
632 resource_type = "texture";
635 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
636 i, resource, render_targets[i].sub_resource_idx, debug_d3dformat(resource->format->id),
637 fbo_key.rb_namespace & (1 << (i + 1)) ? "renderbuffer" : resource_type,
638 fbo_key.objects[i + 1].object, width, height, resource->multisample_type);
641 if ((resource = depth_stencil->resource))
643 if (resource->type == WINED3D_RTYPE_BUFFER)
645 width = resource->size;
646 height = 1;
647 resource_type = "buffer";
649 else
651 ds_texture = wined3d_texture_from_resource(resource);
652 ds_level = depth_stencil->sub_resource_idx % ds_texture->level_count;
653 width = wined3d_texture_get_level_pow2_width(ds_texture, ds_level);
654 height = wined3d_texture_get_level_pow2_height(ds_texture, ds_level);
655 resource_type = "texture";
658 TRACE(" Depth attachment: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
659 resource, depth_stencil->sub_resource_idx, debug_d3dformat(resource->format->id),
660 fbo_key.rb_namespace & (1 << 0) ? "renderbuffer" : resource_type,
661 fbo_key.objects[0].object, width, height, resource->multisample_type);
665 LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
667 if (memcmp(&fbo_key, &entry->key, sizeof(fbo_key)))
668 continue;
670 list_remove(&entry->entry);
671 list_add_head(&context->fbo_list, &entry->entry);
672 return entry;
675 if (context->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
677 entry = context_create_fbo_entry(context, render_targets, depth_stencil, color_location, ds_location);
678 list_add_head(&context->fbo_list, &entry->entry);
679 ++context->fbo_entry_count;
681 else
683 entry = LIST_ENTRY(list_tail(&context->fbo_list), struct fbo_entry, entry);
684 context_reuse_fbo_entry(context, target, render_targets, depth_stencil, color_location, ds_location, entry);
685 list_remove(&entry->entry);
686 list_add_head(&context->fbo_list, &entry->entry);
689 return entry;
692 /* Context activation is done by the caller. */
693 static void context_apply_fbo_entry(struct wined3d_context *context, GLenum target, struct fbo_entry *entry)
695 const struct wined3d_gl_info *gl_info = context->gl_info;
696 GLuint read_binding, draw_binding;
697 unsigned int i;
699 if (entry->flags & WINED3D_FBO_ENTRY_FLAG_ATTACHED)
701 context_bind_fbo(context, target, entry->id);
702 return;
705 read_binding = context->fbo_read_binding;
706 draw_binding = context->fbo_draw_binding;
707 context_bind_fbo(context, GL_FRAMEBUFFER, entry->id);
709 if (gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
711 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER,
712 GL_FRAMEBUFFER_DEFAULT_WIDTH, gl_info->limits.framebuffer_width));
713 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER,
714 GL_FRAMEBUFFER_DEFAULT_HEIGHT, gl_info->limits.framebuffer_height));
715 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS, 1));
716 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES, 1));
719 /* Apply render targets */
720 for (i = 0; i < gl_info->limits.buffers; ++i)
722 context_attach_surface_fbo(context, target, i, &entry->key.objects[i + 1],
723 entry->key.rb_namespace & (1 << (i + 1)));
726 context_attach_depth_stencil_fbo(context, target, &entry->key.objects[0],
727 entry->key.rb_namespace & 0x1, entry->flags);
729 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
730 * GL contexts requirements. */
731 gl_info->gl_ops.gl.p_glReadBuffer(GL_NONE);
732 context_set_draw_buffer(context, GL_NONE);
733 if (target != GL_FRAMEBUFFER)
735 if (target == GL_READ_FRAMEBUFFER)
736 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, draw_binding);
737 else
738 context_bind_fbo(context, GL_READ_FRAMEBUFFER, read_binding);
741 entry->flags |= WINED3D_FBO_ENTRY_FLAG_ATTACHED;
744 /* Context activation is done by the caller. */
745 static void context_apply_fbo_state(struct wined3d_context *context, GLenum target,
746 const struct wined3d_rendertarget_info *render_targets,
747 const struct wined3d_rendertarget_info *depth_stencil, DWORD color_location, DWORD ds_location)
749 struct fbo_entry *entry, *entry2;
751 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
753 context_destroy_fbo_entry(context, entry);
756 if (context->rebind_fbo)
758 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
759 context->rebind_fbo = FALSE;
762 if (color_location == WINED3D_LOCATION_DRAWABLE)
764 context->current_fbo = NULL;
765 context_bind_fbo(context, target, 0);
767 else
769 context->current_fbo = context_find_fbo_entry(context, target,
770 render_targets, depth_stencil, color_location, ds_location);
771 context_apply_fbo_entry(context, target, context->current_fbo);
775 /* Context activation is done by the caller. */
776 void context_apply_fbo_state_blit(struct wined3d_context *context, GLenum target,
777 struct wined3d_resource *rt, unsigned int rt_sub_resource_idx,
778 struct wined3d_resource *ds, unsigned int ds_sub_resource_idx, DWORD location)
780 struct wined3d_rendertarget_info ds_info = {{0}};
782 memset(context->blit_targets, 0, sizeof(context->blit_targets));
783 if (rt)
785 context->blit_targets[0].resource = rt;
786 context->blit_targets[0].sub_resource_idx = rt_sub_resource_idx;
787 context->blit_targets[0].layer_count = 1;
790 if (ds)
792 ds_info.resource = ds;
793 ds_info.sub_resource_idx = ds_sub_resource_idx;
794 ds_info.layer_count = 1;
797 context_apply_fbo_state(context, target, context->blit_targets, &ds_info, location, location);
800 /* Context activation is done by the caller. */
801 void context_alloc_occlusion_query(struct wined3d_context *context, struct wined3d_occlusion_query *query)
803 const struct wined3d_gl_info *gl_info = context->gl_info;
805 if (context->free_occlusion_query_count)
807 query->id = context->free_occlusion_queries[--context->free_occlusion_query_count];
809 else
811 if (gl_info->supported[ARB_OCCLUSION_QUERY])
813 GL_EXTCALL(glGenQueries(1, &query->id));
814 checkGLcall("glGenQueries");
816 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context);
818 else
820 WARN("Occlusion queries not supported, not allocating query id.\n");
821 query->id = 0;
825 query->context = context;
826 list_add_head(&context->occlusion_queries, &query->entry);
829 void context_free_occlusion_query(struct wined3d_occlusion_query *query)
831 struct wined3d_context *context = query->context;
833 list_remove(&query->entry);
834 query->context = NULL;
836 if (!wined3d_array_reserve((void **)&context->free_occlusion_queries,
837 &context->free_occlusion_query_size, context->free_occlusion_query_count + 1,
838 sizeof(*context->free_occlusion_queries)))
840 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
841 return;
844 context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
847 /* Context activation is done by the caller. */
848 void context_alloc_fence(struct wined3d_context *context, struct wined3d_fence *fence)
850 const struct wined3d_gl_info *gl_info = context->gl_info;
852 if (context->free_fence_count)
854 fence->object = context->free_fences[--context->free_fence_count];
856 else
858 if (gl_info->supported[ARB_SYNC])
860 /* Using ARB_sync, not much to do here. */
861 fence->object.sync = NULL;
862 TRACE("Allocated sync object in context %p.\n", context);
864 else if (gl_info->supported[APPLE_FENCE])
866 GL_EXTCALL(glGenFencesAPPLE(1, &fence->object.id));
867 checkGLcall("glGenFencesAPPLE");
869 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context);
871 else if(gl_info->supported[NV_FENCE])
873 GL_EXTCALL(glGenFencesNV(1, &fence->object.id));
874 checkGLcall("glGenFencesNV");
876 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context);
878 else
880 WARN("Fences not supported, not allocating fence.\n");
881 fence->object.id = 0;
885 fence->context = context;
886 list_add_head(&context->fences, &fence->entry);
889 void context_free_fence(struct wined3d_fence *fence)
891 struct wined3d_context *context = fence->context;
893 list_remove(&fence->entry);
894 fence->context = NULL;
896 if (!wined3d_array_reserve((void **)&context->free_fences,
897 &context->free_fence_size, context->free_fence_count + 1,
898 sizeof(*context->free_fences)))
900 ERR("Failed to grow free list, leaking fence %u in context %p.\n", fence->object.id, context);
901 return;
904 context->free_fences[context->free_fence_count++] = fence->object;
907 /* Context activation is done by the caller. */
908 void context_alloc_timestamp_query(struct wined3d_context *context, struct wined3d_timestamp_query *query)
910 const struct wined3d_gl_info *gl_info = context->gl_info;
912 if (context->free_timestamp_query_count)
914 query->id = context->free_timestamp_queries[--context->free_timestamp_query_count];
916 else
918 GL_EXTCALL(glGenQueries(1, &query->id));
919 checkGLcall("glGenQueries");
921 TRACE("Allocated timestamp query %u in context %p.\n", query->id, context);
924 query->context = context;
925 list_add_head(&context->timestamp_queries, &query->entry);
928 void context_free_timestamp_query(struct wined3d_timestamp_query *query)
930 struct wined3d_context *context = query->context;
932 list_remove(&query->entry);
933 query->context = NULL;
935 if (!wined3d_array_reserve((void **)&context->free_timestamp_queries,
936 &context->free_timestamp_query_size, context->free_timestamp_query_count + 1,
937 sizeof(*context->free_timestamp_queries)))
939 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
940 return;
943 context->free_timestamp_queries[context->free_timestamp_query_count++] = query->id;
946 void context_alloc_so_statistics_query(struct wined3d_context *context,
947 struct wined3d_so_statistics_query *query)
949 const struct wined3d_gl_info *gl_info = context->gl_info;
951 if (context->free_so_statistics_query_count)
953 query->u = context->free_so_statistics_queries[--context->free_so_statistics_query_count];
955 else
957 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
958 checkGLcall("glGenQueries");
960 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
961 query->u.id[0], query->u.id[1], context);
964 query->context = context;
965 list_add_head(&context->so_statistics_queries, &query->entry);
968 void context_free_so_statistics_query(struct wined3d_so_statistics_query *query)
970 struct wined3d_context *context = query->context;
972 list_remove(&query->entry);
973 query->context = NULL;
975 if (!wined3d_array_reserve((void **)&context->free_so_statistics_queries,
976 &context->free_so_statistics_query_size, context->free_so_statistics_query_count + 1,
977 sizeof(*context->free_so_statistics_queries)))
979 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
980 query->u.id[0], query->u.id[1], context);
981 return;
984 context->free_so_statistics_queries[context->free_so_statistics_query_count++] = query->u;
987 void context_alloc_pipeline_statistics_query(struct wined3d_context *context,
988 struct wined3d_pipeline_statistics_query *query)
990 const struct wined3d_gl_info *gl_info = context->gl_info;
992 if (context->free_pipeline_statistics_query_count)
994 query->u = context->free_pipeline_statistics_queries[--context->free_pipeline_statistics_query_count];
996 else
998 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
999 checkGLcall("glGenQueries");
1002 query->context = context;
1003 list_add_head(&context->pipeline_statistics_queries, &query->entry);
1006 void context_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query *query)
1008 struct wined3d_context *context = query->context;
1010 list_remove(&query->entry);
1011 query->context = NULL;
1013 if (!wined3d_array_reserve((void **)&context->free_pipeline_statistics_queries,
1014 &context->free_pipeline_statistics_query_size, context->free_pipeline_statistics_query_count + 1,
1015 sizeof(*context->free_pipeline_statistics_queries)))
1017 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context);
1018 return;
1021 context->free_pipeline_statistics_queries[context->free_pipeline_statistics_query_count++] = query->u;
1024 typedef void (context_fbo_entry_func_t)(struct wined3d_context *context, struct fbo_entry *entry);
1026 static void context_enum_fbo_entries(const struct wined3d_device *device,
1027 GLuint name, BOOL rb_namespace, context_fbo_entry_func_t *callback)
1029 unsigned int i, j;
1031 for (i = 0; i < device->context_count; ++i)
1033 struct wined3d_context *context = device->contexts[i];
1034 const struct wined3d_gl_info *gl_info = context->gl_info;
1035 struct fbo_entry *entry, *entry2;
1037 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
1039 for (j = 0; j < gl_info->limits.buffers + 1; ++j)
1041 if (entry->key.objects[j].object == name
1042 && !(entry->key.rb_namespace & (1 << j)) == !rb_namespace)
1044 callback(context, entry);
1045 break;
1052 static void context_queue_fbo_entry_destruction(struct wined3d_context *context, struct fbo_entry *entry)
1054 list_remove(&entry->entry);
1055 list_add_head(&context->fbo_destroy_list, &entry->entry);
1058 void context_resource_released(const struct wined3d_device *device,
1059 struct wined3d_resource *resource, enum wined3d_resource_type type)
1061 struct wined3d_texture *texture;
1062 UINT i;
1064 if (!device->d3d_initialized)
1065 return;
1067 switch (type)
1069 case WINED3D_RTYPE_TEXTURE_2D:
1070 case WINED3D_RTYPE_TEXTURE_3D:
1071 texture = texture_from_resource(resource);
1073 for (i = 0; i < device->context_count; ++i)
1075 struct wined3d_context *context = device->contexts[i];
1076 if (context->current_rt.texture == texture)
1078 context->current_rt.texture = NULL;
1079 context->current_rt.sub_resource_idx = 0;
1082 break;
1084 default:
1085 break;
1089 void context_gl_resource_released(struct wined3d_device *device,
1090 GLuint name, BOOL rb_namespace)
1092 context_enum_fbo_entries(device, name, rb_namespace, context_queue_fbo_entry_destruction);
1095 void context_texture_update(struct wined3d_context *context, const struct wined3d_texture *texture)
1097 const struct wined3d_gl_info *gl_info = context->gl_info;
1098 struct fbo_entry *entry = context->current_fbo;
1099 unsigned int i;
1101 if (!entry || context->rebind_fbo) return;
1103 for (i = 0; i < gl_info->limits.buffers + 1; ++i)
1105 if (texture->texture_rgb.name == entry->key.objects[i].object
1106 || texture->texture_srgb.name == entry->key.objects[i].object)
1108 TRACE("Updated texture %p is bound as attachment %u to the current FBO.\n", texture, i);
1109 context->rebind_fbo = TRUE;
1110 return;
1115 static BOOL context_restore_pixel_format(struct wined3d_context *ctx)
1117 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1118 BOOL ret = FALSE;
1120 if (ctx->restore_pf && IsWindow(ctx->restore_pf_win))
1122 if (ctx->gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1124 HDC dc = GetDCEx(ctx->restore_pf_win, 0, DCX_USESTYLE | DCX_CACHE);
1125 if (dc)
1127 if (!(ret = GL_EXTCALL(wglSetPixelFormatWINE(dc, ctx->restore_pf))))
1129 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
1130 ctx->restore_pf, ctx->restore_pf_win);
1132 ReleaseDC(ctx->restore_pf_win, dc);
1135 else
1137 ERR("can't restore pixel format %d on window %p\n", ctx->restore_pf, ctx->restore_pf_win);
1141 ctx->restore_pf = 0;
1142 ctx->restore_pf_win = NULL;
1143 return ret;
1146 static BOOL context_set_pixel_format(struct wined3d_context *context)
1148 const struct wined3d_gl_info *gl_info = context->gl_info;
1149 BOOL private = context->hdc_is_private;
1150 int format = context->pixel_format;
1151 HDC dc = context->hdc;
1152 int current;
1154 if (private && context->hdc_has_format)
1155 return TRUE;
1157 if (!private && WindowFromDC(dc) != context->win_handle)
1158 return FALSE;
1160 current = gl_info->gl_ops.wgl.p_wglGetPixelFormat(dc);
1161 if (current == format) goto success;
1163 if (!current)
1165 if (!SetPixelFormat(dc, format, NULL))
1167 /* This may also happen if the dc belongs to a destroyed window. */
1168 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1169 format, dc, GetLastError());
1170 return FALSE;
1173 context->restore_pf = 0;
1174 context->restore_pf_win = private ? NULL : WindowFromDC(dc);
1175 goto success;
1178 /* By default WGL doesn't allow pixel format adjustments but we need it
1179 * here. For this reason there's a Wine specific wglSetPixelFormat()
1180 * which allows us to set the pixel format multiple times. Only use it
1181 * when really needed. */
1182 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1184 HWND win;
1186 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
1188 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1189 format, dc);
1190 return FALSE;
1193 win = private ? NULL : WindowFromDC(dc);
1194 if (win != context->restore_pf_win)
1196 context_restore_pixel_format(context);
1198 context->restore_pf = private ? 0 : current;
1199 context->restore_pf_win = win;
1202 goto success;
1205 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1206 * continue using the old format. There's a big chance that the old
1207 * format works although with a performance hit and perhaps rendering
1208 * errors. */
1209 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1210 format, dc, current);
1211 return TRUE;
1213 success:
1214 if (private)
1215 context->hdc_has_format = TRUE;
1216 return TRUE;
1219 static BOOL context_set_gl_context(struct wined3d_context *ctx)
1221 struct wined3d_swapchain *swapchain = ctx->swapchain;
1222 BOOL backup = FALSE;
1224 if (!context_set_pixel_format(ctx))
1226 WARN("Failed to set pixel format %d on device context %p.\n",
1227 ctx->pixel_format, ctx->hdc);
1228 backup = TRUE;
1231 if (backup || !wglMakeCurrent(ctx->hdc, ctx->glCtx))
1233 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1234 ctx->glCtx, ctx->hdc, GetLastError());
1235 ctx->valid = 0;
1236 WARN("Trying fallback to the backup window.\n");
1238 /* FIXME: If the context is destroyed it's no longer associated with
1239 * a swapchain, so we can't use the swapchain to get a backup dc. To
1240 * make this work windowless contexts would need to be handled by the
1241 * device. */
1242 if (ctx->destroyed || !swapchain)
1244 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx);
1245 context_set_current(NULL);
1246 return FALSE;
1249 if (!(ctx->hdc = swapchain_get_backup_dc(swapchain)))
1251 context_set_current(NULL);
1252 return FALSE;
1255 ctx->hdc_is_private = TRUE;
1256 ctx->hdc_has_format = FALSE;
1258 if (!context_set_pixel_format(ctx))
1260 ERR("Failed to set pixel format %d on device context %p.\n",
1261 ctx->pixel_format, ctx->hdc);
1262 context_set_current(NULL);
1263 return FALSE;
1266 if (!wglMakeCurrent(ctx->hdc, ctx->glCtx))
1268 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1269 ctx->hdc, GetLastError());
1270 context_set_current(NULL);
1271 return FALSE;
1274 ctx->valid = 1;
1276 ctx->needs_set = 0;
1277 return TRUE;
1280 static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx)
1282 if (!wglMakeCurrent(dc, gl_ctx))
1284 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1285 gl_ctx, dc, GetLastError());
1286 context_set_current(NULL);
1290 static void context_update_window(struct wined3d_context *context)
1292 if (!context->swapchain)
1293 return;
1295 if (context->win_handle == context->swapchain->win_handle)
1296 return;
1298 TRACE("Updating context %p window from %p to %p.\n",
1299 context, context->win_handle, context->swapchain->win_handle);
1301 if (context->hdc)
1302 wined3d_release_dc(context->win_handle, context->hdc);
1304 context->win_handle = context->swapchain->win_handle;
1305 context->hdc_is_private = FALSE;
1306 context->hdc_has_format = FALSE;
1307 context->needs_set = 1;
1308 context->valid = 1;
1310 if (!(context->hdc = GetDCEx(context->win_handle, 0, DCX_USESTYLE | DCX_CACHE)))
1312 ERR("Failed to get a device context for window %p.\n", context->win_handle);
1313 context->valid = 0;
1317 static void context_destroy_gl_resources(struct wined3d_context *context)
1319 struct wined3d_pipeline_statistics_query *pipeline_statistics_query;
1320 const struct wined3d_gl_info *gl_info = context->gl_info;
1321 struct wined3d_so_statistics_query *so_statistics_query;
1322 struct wined3d_timestamp_query *timestamp_query;
1323 struct wined3d_occlusion_query *occlusion_query;
1324 struct fbo_entry *entry, *entry2;
1325 struct wined3d_fence *fence;
1326 HGLRC restore_ctx;
1327 HDC restore_dc;
1328 unsigned int i;
1330 restore_ctx = wglGetCurrentContext();
1331 restore_dc = wglGetCurrentDC();
1333 if (restore_ctx == context->glCtx)
1334 restore_ctx = NULL;
1335 else if (context->valid)
1336 context_set_gl_context(context);
1338 LIST_FOR_EACH_ENTRY(so_statistics_query, &context->so_statistics_queries,
1339 struct wined3d_so_statistics_query, entry)
1341 if (context->valid)
1342 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query->u.id), so_statistics_query->u.id));
1343 so_statistics_query->context = NULL;
1346 LIST_FOR_EACH_ENTRY(pipeline_statistics_query, &context->pipeline_statistics_queries,
1347 struct wined3d_pipeline_statistics_query, entry)
1349 if (context->valid)
1350 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query->u.id), pipeline_statistics_query->u.id));
1351 pipeline_statistics_query->context = NULL;
1354 LIST_FOR_EACH_ENTRY(timestamp_query, &context->timestamp_queries, struct wined3d_timestamp_query, entry)
1356 if (context->valid)
1357 GL_EXTCALL(glDeleteQueries(1, &timestamp_query->id));
1358 timestamp_query->context = NULL;
1361 LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
1363 if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
1364 GL_EXTCALL(glDeleteQueries(1, &occlusion_query->id));
1365 occlusion_query->context = NULL;
1368 LIST_FOR_EACH_ENTRY(fence, &context->fences, struct wined3d_fence, entry)
1370 if (context->valid)
1372 if (gl_info->supported[ARB_SYNC])
1374 if (fence->object.sync)
1375 GL_EXTCALL(glDeleteSync(fence->object.sync));
1377 else if (gl_info->supported[APPLE_FENCE])
1379 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence->object.id));
1381 else if (gl_info->supported[NV_FENCE])
1383 GL_EXTCALL(glDeleteFencesNV(1, &fence->object.id));
1386 fence->context = NULL;
1389 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
1391 if (!context->valid) entry->id = 0;
1392 context_destroy_fbo_entry(context, entry);
1395 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
1397 if (!context->valid) entry->id = 0;
1398 context_destroy_fbo_entry(context, entry);
1401 if (context->valid)
1403 if (context->dummy_arbfp_prog)
1405 GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
1408 if (gl_info->supported[WINED3D_GL_PRIMITIVE_QUERY])
1410 for (i = 0; i < context->free_so_statistics_query_count; ++i)
1412 union wined3d_gl_so_statistics_query *q = &context->free_so_statistics_queries[i];
1413 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1417 if (gl_info->supported[ARB_PIPELINE_STATISTICS_QUERY])
1419 for (i = 0; i < context->free_pipeline_statistics_query_count; ++i)
1421 union wined3d_gl_pipeline_statistics_query *q = &context->free_pipeline_statistics_queries[i];
1422 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1426 if (gl_info->supported[ARB_TIMER_QUERY])
1427 GL_EXTCALL(glDeleteQueries(context->free_timestamp_query_count, context->free_timestamp_queries));
1429 if (gl_info->supported[ARB_OCCLUSION_QUERY])
1430 GL_EXTCALL(glDeleteQueries(context->free_occlusion_query_count, context->free_occlusion_queries));
1432 if (gl_info->supported[ARB_SYNC])
1434 for (i = 0; i < context->free_fence_count; ++i)
1436 GL_EXTCALL(glDeleteSync(context->free_fences[i].sync));
1439 else if (gl_info->supported[APPLE_FENCE])
1441 for (i = 0; i < context->free_fence_count; ++i)
1443 GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_fences[i].id));
1446 else if (gl_info->supported[NV_FENCE])
1448 for (i = 0; i < context->free_fence_count; ++i)
1450 GL_EXTCALL(glDeleteFencesNV(1, &context->free_fences[i].id));
1454 checkGLcall("context cleanup");
1457 heap_free(context->free_so_statistics_queries);
1458 heap_free(context->free_pipeline_statistics_queries);
1459 heap_free(context->free_timestamp_queries);
1460 heap_free(context->free_occlusion_queries);
1461 heap_free(context->free_fences);
1463 context_restore_pixel_format(context);
1464 if (restore_ctx)
1466 context_restore_gl_context(gl_info, restore_dc, restore_ctx);
1468 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1470 ERR("Failed to disable GL context.\n");
1473 wined3d_release_dc(context->win_handle, context->hdc);
1475 if (!wglDeleteContext(context->glCtx))
1477 DWORD err = GetLastError();
1478 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
1482 DWORD context_get_tls_idx(void)
1484 return wined3d_context_tls_idx;
1487 void context_set_tls_idx(DWORD idx)
1489 wined3d_context_tls_idx = idx;
1492 struct wined3d_context *context_get_current(void)
1494 return TlsGetValue(wined3d_context_tls_idx);
1497 BOOL context_set_current(struct wined3d_context *ctx)
1499 struct wined3d_context *old = context_get_current();
1501 if (old == ctx)
1503 TRACE("Already using D3D context %p.\n", ctx);
1504 return TRUE;
1507 if (old)
1509 if (old->destroyed)
1511 TRACE("Switching away from destroyed context %p.\n", old);
1512 context_destroy_gl_resources(old);
1513 heap_free((void *)old->gl_info);
1514 heap_free(old);
1516 else
1518 if (wglGetCurrentContext())
1520 const struct wined3d_gl_info *gl_info = old->gl_info;
1521 TRACE("Flushing context %p before switching to %p.\n", old, ctx);
1522 gl_info->gl_ops.gl.p_glFlush();
1524 old->current = 0;
1528 if (ctx)
1530 if (!ctx->valid)
1532 ERR("Trying to make invalid context %p current\n", ctx);
1533 return FALSE;
1536 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
1537 if (!context_set_gl_context(ctx))
1538 return FALSE;
1539 ctx->current = 1;
1541 else if (wglGetCurrentContext())
1543 TRACE("Clearing current D3D context.\n");
1544 if (!wglMakeCurrent(NULL, NULL))
1546 DWORD err = GetLastError();
1547 ERR("Failed to clear current GL context, last error %#x.\n", err);
1548 TlsSetValue(wined3d_context_tls_idx, NULL);
1549 return FALSE;
1553 return TlsSetValue(wined3d_context_tls_idx, ctx);
1556 void context_release(struct wined3d_context *context)
1558 TRACE("Releasing context %p, level %u.\n", context, context->level);
1560 if (WARN_ON(d3d))
1562 if (!context->level)
1563 WARN("Context %p is not active.\n", context);
1564 else if (context != context_get_current())
1565 WARN("Context %p is not the current context.\n", context);
1568 if (!--context->level)
1570 if (context_restore_pixel_format(context))
1571 context->needs_set = 1;
1572 if (context->restore_ctx)
1574 TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
1575 context_restore_gl_context(context->gl_info, context->restore_dc, context->restore_ctx);
1576 context->restore_ctx = NULL;
1577 context->restore_dc = NULL;
1580 if (context->destroy_delayed)
1582 TRACE("Destroying context %p.\n", context);
1583 context_destroy(context->device, context);
1588 /* This is used when a context for render target A is active, but a separate context is
1589 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1590 * A to avoid breaking caller code. */
1591 void context_restore(struct wined3d_context *context, struct wined3d_texture *texture, unsigned int sub_resource_idx)
1593 if (context->current_rt.texture != texture || context->current_rt.sub_resource_idx != sub_resource_idx)
1595 context_release(context);
1596 context = context_acquire(texture->resource.device, texture, sub_resource_idx);
1599 context_release(context);
1602 static void context_enter(struct wined3d_context *context)
1604 TRACE("Entering context %p, level %u.\n", context, context->level + 1);
1606 if (!context->level++)
1608 const struct wined3d_context *current_context = context_get_current();
1609 HGLRC current_gl = wglGetCurrentContext();
1611 if (current_gl && (!current_context || current_context->glCtx != current_gl))
1613 TRACE("Another GL context (%p on device context %p) is already current.\n",
1614 current_gl, wglGetCurrentDC());
1615 context->restore_ctx = current_gl;
1616 context->restore_dc = wglGetCurrentDC();
1617 context->needs_set = 1;
1619 else if (!context->needs_set && !(context->hdc_is_private && context->hdc_has_format)
1620 && context->pixel_format != context->gl_info->gl_ops.wgl.p_wglGetPixelFormat(context->hdc))
1621 context->needs_set = 1;
1625 void context_invalidate_compute_state(struct wined3d_context *context, DWORD state_id)
1627 DWORD representative = context->state_table[state_id].representative - STATE_COMPUTE_OFFSET;
1628 unsigned int index, shift;
1630 index = representative / (sizeof(*context->dirty_compute_states) * CHAR_BIT);
1631 shift = representative & (sizeof(*context->dirty_compute_states) * CHAR_BIT - 1);
1632 context->dirty_compute_states[index] |= (1u << shift);
1635 void context_invalidate_state(struct wined3d_context *context, DWORD state)
1637 DWORD rep = context->state_table[state].representative;
1638 DWORD idx;
1639 BYTE shift;
1641 if (isStateDirty(context, rep)) return;
1643 context->dirtyArray[context->numDirtyEntries++] = rep;
1644 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
1645 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
1646 context->isStateDirty[idx] |= (1u << shift);
1649 /* This function takes care of wined3d pixel format selection. */
1650 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1651 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1652 BOOL auxBuffers)
1654 unsigned int cfg_count = device->adapter->cfg_count;
1655 unsigned int current_value;
1656 PIXELFORMATDESCRIPTOR pfd;
1657 int iPixelFormat = 0;
1658 unsigned int i;
1660 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1661 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1662 auxBuffers);
1664 current_value = 0;
1665 for (i = 0; i < cfg_count; ++i)
1667 const struct wined3d_pixel_format *cfg = &device->adapter->cfgs[i];
1668 unsigned int value;
1670 /* For now only accept RGBA formats. Perhaps some day we will
1671 * allow floating point formats for pbuffers. */
1672 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1673 continue;
1674 /* In window mode we need a window drawable format and double buffering. */
1675 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1676 continue;
1677 if (cfg->redSize < color_format->red_size)
1678 continue;
1679 if (cfg->greenSize < color_format->green_size)
1680 continue;
1681 if (cfg->blueSize < color_format->blue_size)
1682 continue;
1683 if (cfg->alphaSize < color_format->alpha_size)
1684 continue;
1685 if (cfg->depthSize < ds_format->depth_size)
1686 continue;
1687 if (ds_format->stencil_size && cfg->stencilSize != ds_format->stencil_size)
1688 continue;
1689 /* Check multisampling support. */
1690 if (cfg->numSamples)
1691 continue;
1693 value = 1;
1694 /* We try to locate a format which matches our requirements exactly. In case of
1695 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1696 if (cfg->depthSize == ds_format->depth_size)
1697 value += 1;
1698 if (cfg->stencilSize == ds_format->stencil_size)
1699 value += 2;
1700 if (cfg->alphaSize == color_format->alpha_size)
1701 value += 4;
1702 /* We like to have aux buffers in backbuffer mode */
1703 if (auxBuffers && cfg->auxBuffers)
1704 value += 8;
1705 if (cfg->redSize == color_format->red_size
1706 && cfg->greenSize == color_format->green_size
1707 && cfg->blueSize == color_format->blue_size)
1708 value += 16;
1710 if (value > current_value)
1712 iPixelFormat = cfg->iPixelFormat;
1713 current_value = value;
1717 if (!iPixelFormat)
1719 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1721 memset(&pfd, 0, sizeof(pfd));
1722 pfd.nSize = sizeof(pfd);
1723 pfd.nVersion = 1;
1724 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1725 pfd.iPixelType = PFD_TYPE_RGBA;
1726 pfd.cAlphaBits = color_format->alpha_size;
1727 pfd.cColorBits = color_format->red_size + color_format->green_size
1728 + color_format->blue_size + color_format->alpha_size;
1729 pfd.cDepthBits = ds_format->depth_size;
1730 pfd.cStencilBits = ds_format->stencil_size;
1731 pfd.iLayerType = PFD_MAIN_PLANE;
1733 if (!(iPixelFormat = ChoosePixelFormat(hdc, &pfd)))
1735 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1736 ERR("Can't find a suitable pixel format.\n");
1737 return 0;
1741 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1742 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1743 return iPixelFormat;
1746 /* Context activation is done by the caller. */
1747 void context_bind_dummy_textures(const struct wined3d_device *device, const struct wined3d_context *context)
1749 const struct wined3d_dummy_textures *textures = &context->device->dummy_textures;
1750 const struct wined3d_gl_info *gl_info = context->gl_info;
1751 unsigned int i;
1753 for (i = 0; i < gl_info->limits.combined_samplers; ++i)
1755 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + i));
1757 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
1759 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1760 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
1762 if (gl_info->supported[EXT_TEXTURE3D])
1763 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
1765 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1766 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
1768 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
1769 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
1771 if (gl_info->supported[EXT_TEXTURE_ARRAY])
1772 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
1774 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1775 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
1777 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1779 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
1780 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
1784 checkGLcall("bind dummy textures");
1787 void wined3d_check_gl_call(const struct wined3d_gl_info *gl_info,
1788 const char *file, unsigned int line, const char *name)
1790 GLint err;
1792 if (gl_info->supported[ARB_DEBUG_OUTPUT] || (err = gl_info->gl_ops.gl.p_glGetError()) == GL_NO_ERROR)
1794 TRACE("%s call ok %s / %u.\n", name, file, line);
1795 return;
1800 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1801 debug_glerror(err), err, name, file,line);
1802 err = gl_info->gl_ops.gl.p_glGetError();
1803 } while (err != GL_NO_ERROR);
1806 static BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1808 return gl_info->supported[ARB_DEBUG_OUTPUT]
1809 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1812 static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1813 GLenum severity, GLsizei length, const char *message, void *ctx)
1815 switch (type)
1817 case GL_DEBUG_TYPE_ERROR_ARB:
1818 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1819 break;
1821 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1822 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1823 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1824 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1825 break;
1827 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1828 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1829 break;
1831 default:
1832 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1833 break;
1837 HGLRC context_create_wgl_attribs(const struct wined3d_gl_info *gl_info, HDC hdc, HGLRC share_ctx)
1839 HGLRC ctx;
1840 unsigned int ctx_attrib_idx = 0;
1841 GLint ctx_attribs[7], ctx_flags = 0;
1843 if (context_debug_output_enabled(gl_info))
1844 ctx_flags = WGL_CONTEXT_DEBUG_BIT_ARB;
1845 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
1846 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version >> 16;
1847 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MINOR_VERSION_ARB;
1848 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version & 0xffff;
1849 if (gl_info->selected_gl_version >= MAKEDWORD_VERSION(3, 2))
1850 ctx_flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1851 if (ctx_flags)
1853 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1854 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1856 ctx_attribs[ctx_attrib_idx] = 0;
1858 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1860 if (ctx_flags & WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB)
1862 ctx_attribs[ctx_attrib_idx - 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1863 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1864 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1865 GetLastError());
1868 return ctx;
1871 struct wined3d_context *context_create(struct wined3d_swapchain *swapchain,
1872 struct wined3d_texture *target, const struct wined3d_format *ds_format)
1874 struct wined3d_device *device = swapchain->device;
1875 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
1876 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1877 const struct wined3d_format *color_format;
1878 struct wined3d_context *ret;
1879 BOOL auxBuffers = FALSE;
1880 HGLRC ctx, share_ctx;
1881 DWORD target_usage;
1882 unsigned int i;
1883 DWORD state;
1885 TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
1887 wined3d_from_cs(device->cs);
1889 if (!(ret = heap_alloc_zero(sizeof(*ret))))
1890 return NULL;
1892 ret->free_timestamp_query_size = 4;
1893 if (!(ret->free_timestamp_queries = heap_calloc(ret->free_timestamp_query_size,
1894 sizeof(*ret->free_timestamp_queries))))
1895 goto out;
1896 list_init(&ret->timestamp_queries);
1898 ret->free_occlusion_query_size = 4;
1899 if (!(ret->free_occlusion_queries = heap_calloc(ret->free_occlusion_query_size,
1900 sizeof(*ret->free_occlusion_queries))))
1901 goto out;
1902 list_init(&ret->occlusion_queries);
1904 ret->free_fence_size = 4;
1905 if (!(ret->free_fences = heap_calloc(ret->free_fence_size, sizeof(*ret->free_fences))))
1906 goto out;
1907 list_init(&ret->fences);
1909 list_init(&ret->so_statistics_queries);
1911 list_init(&ret->pipeline_statistics_queries);
1913 list_init(&ret->fbo_list);
1914 list_init(&ret->fbo_destroy_list);
1916 if (!device->shader_backend->shader_allocate_context_data(ret))
1918 ERR("Failed to allocate shader backend context data.\n");
1919 goto out;
1921 if (!device->adapter->fragment_pipe->allocate_context_data(ret))
1923 ERR("Failed to allocate fragment pipeline context data.\n");
1924 goto out;
1927 for (i = 0; i < ARRAY_SIZE(ret->tex_unit_map); ++i)
1928 ret->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
1929 for (i = 0; i < ARRAY_SIZE(ret->rev_tex_unit_map); ++i)
1930 ret->rev_tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
1931 if (gl_info->limits.graphics_samplers >= MAX_COMBINED_SAMPLERS)
1933 /* Initialize the texture unit mapping to a 1:1 mapping. */
1934 unsigned int base, count;
1936 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_PIXEL, &base, &count);
1937 if (base + MAX_FRAGMENT_SAMPLERS > ARRAY_SIZE(ret->rev_tex_unit_map))
1939 ERR("Unexpected texture unit base index %u.\n", base);
1940 goto out;
1942 for (i = 0; i < min(count, MAX_FRAGMENT_SAMPLERS); ++i)
1944 ret->tex_unit_map[i] = base + i;
1945 ret->rev_tex_unit_map[base + i] = i;
1948 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_VERTEX, &base, &count);
1949 if (base + MAX_VERTEX_SAMPLERS > ARRAY_SIZE(ret->rev_tex_unit_map))
1951 ERR("Unexpected texture unit base index %u.\n", base);
1952 goto out;
1954 for (i = 0; i < min(count, MAX_VERTEX_SAMPLERS); ++i)
1956 ret->tex_unit_map[MAX_FRAGMENT_SAMPLERS + i] = base + i;
1957 ret->rev_tex_unit_map[base + i] = MAX_FRAGMENT_SAMPLERS + i;
1961 if (!(ret->texture_type = heap_calloc(gl_info->limits.combined_samplers,
1962 sizeof(*ret->texture_type))))
1963 goto out;
1965 if (!(ret->hdc = GetDCEx(swapchain->win_handle, 0, DCX_USESTYLE | DCX_CACHE)))
1967 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1969 if ((ret->hdc = swapchain_get_backup_dc(swapchain)))
1970 ret->hdc_is_private = TRUE;
1971 else
1973 ERR("Failed to retrieve a device context.\n");
1974 goto out;
1978 color_format = target->resource.format;
1979 target_usage = target->resource.usage;
1981 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1982 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1983 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1985 auxBuffers = TRUE;
1987 if (color_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1988 color_format = wined3d_get_format(gl_info, WINED3DFMT_B4G4R4A4_UNORM, target_usage);
1989 else if (color_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1990 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM, target_usage);
1993 /* DirectDraw supports 8bit paletted render targets and these are used by
1994 * old games like StarCraft and C&C. Most modern hardware doesn't support
1995 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1996 * conversion (ab)uses the alpha component for storing the palette index.
1997 * For this reason we require a format with 8bit alpha, so request
1998 * A8R8G8B8. */
1999 if (color_format->id == WINED3DFMT_P8_UINT)
2000 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM, target_usage);
2002 /* When using FBOs for off-screen rendering, we only use the drawable for
2003 * presentation blits, and don't do any rendering to it. That means we
2004 * don't need depth or stencil buffers, and can mostly ignore the render
2005 * target format. This wouldn't necessarily be quite correct for 10bpc
2006 * display modes, but we don't currently support those.
2007 * Using the same format regardless of the color/depth/stencil targets
2008 * makes it much less likely that different wined3d instances will set
2009 * conflicting pixel formats. */
2010 if (wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
2012 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM, target_usage);
2013 ds_format = wined3d_get_format(gl_info, WINED3DFMT_UNKNOWN, WINED3DUSAGE_DEPTHSTENCIL);
2016 /* Try to find a pixel format which matches our requirements. */
2017 if (!(ret->pixel_format = context_choose_pixel_format(device, ret->hdc, color_format, ds_format, auxBuffers)))
2018 goto out;
2020 ret->gl_info = gl_info;
2021 ret->win_handle = swapchain->win_handle;
2023 context_enter(ret);
2025 if (!context_set_pixel_format(ret))
2027 ERR("Failed to set pixel format %d on device context %p.\n", ret->pixel_format, ret->hdc);
2028 context_release(ret);
2029 goto out;
2032 share_ctx = device->context_count ? device->contexts[0]->glCtx : NULL;
2033 if (gl_info->p_wglCreateContextAttribsARB)
2035 if (!(ctx = context_create_wgl_attribs(gl_info, ret->hdc, share_ctx)))
2036 goto out;
2038 else
2040 if (!(ctx = wglCreateContext(ret->hdc)))
2042 ERR("Failed to create a WGL context.\n");
2043 context_release(ret);
2044 goto out;
2047 if (share_ctx && !wglShareLists(share_ctx, ctx))
2049 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
2050 context_release(ret);
2051 if (!wglDeleteContext(ctx))
2052 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2053 goto out;
2057 if (!device_context_add(device, ret))
2059 ERR("Failed to add the newly created context to the context list\n");
2060 context_release(ret);
2061 if (!wglDeleteContext(ctx))
2062 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2063 goto out;
2066 ret->d3d_info = d3d_info;
2067 ret->state_table = device->StateTable;
2069 /* Mark all states dirty to force a proper initialization of the states on
2070 * the first use of the context. Compute states do not need initialization. */
2071 for (state = 0; state <= STATE_HIGHEST; ++state)
2073 if (ret->state_table[state].representative && !STATE_IS_COMPUTE(state))
2074 context_invalidate_state(ret, state);
2077 ret->device = device;
2078 ret->swapchain = swapchain;
2079 ret->current_rt.texture = target;
2080 ret->current_rt.sub_resource_idx = 0;
2081 ret->tid = GetCurrentThreadId();
2083 ret->render_offscreen = wined3d_resource_is_offscreen(&target->resource);
2084 ret->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
2085 ret->valid = 1;
2087 ret->glCtx = ctx;
2088 ret->hdc_has_format = TRUE;
2089 ret->needs_set = 1;
2091 /* Set up the context defaults */
2092 if (!context_set_current(ret))
2094 ERR("Cannot activate context to set up defaults.\n");
2095 device_context_remove(device, ret);
2096 context_release(ret);
2097 if (!wglDeleteContext(ctx))
2098 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2099 goto out;
2102 if (context_debug_output_enabled(gl_info))
2104 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback, ret));
2105 if (TRACE_ON(d3d_synchronous))
2106 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2107 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
2108 if (ERR_ON(d3d))
2110 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR,
2111 GL_DONT_CARE, 0, NULL, GL_TRUE));
2113 if (FIXME_ON(d3d))
2115 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
2116 GL_DONT_CARE, 0, NULL, GL_TRUE));
2117 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
2118 GL_DONT_CARE, 0, NULL, GL_TRUE));
2119 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY,
2120 GL_DONT_CARE, 0, NULL, GL_TRUE));
2122 if (WARN_ON(d3d_perf))
2124 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE,
2125 GL_DONT_CARE, 0, NULL, GL_TRUE));
2129 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2130 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
2132 TRACE("Setting up the screen\n");
2134 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2136 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
2137 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2139 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
2140 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2142 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2143 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2145 else
2147 GLuint vao;
2149 GL_EXTCALL(glGenVertexArrays(1, &vao));
2150 GL_EXTCALL(glBindVertexArray(vao));
2151 checkGLcall("creating VAO");
2154 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
2155 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2156 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2157 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2159 if (gl_info->supported[ARB_VERTEX_BLEND])
2161 /* Direct3D always uses n-1 weights for n world matrices and uses
2162 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
2163 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
2164 * enabled as well. */
2165 gl_info->gl_ops.gl.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB);
2166 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
2168 if (gl_info->supported[NV_TEXTURE_SHADER2])
2170 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2171 * the previous texture where to source the offset from is always unit - 1.
2173 for (i = 1; i < gl_info->limits.textures; ++i)
2175 context_active_texture(ret, gl_info, i);
2176 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
2177 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + i - 1);
2178 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2181 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2183 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2184 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2185 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2186 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2187 * is ever assigned.
2189 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2190 * program and the dummy program is destroyed when the context is destroyed.
2192 static const char dummy_program[] =
2193 "!!ARBfp1.0\n"
2194 "MOV result.color, fragment.color.primary;\n"
2195 "END\n";
2196 GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
2197 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
2198 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2201 if (gl_info->supported[ARB_POINT_SPRITE])
2203 for (i = 0; i < gl_info->limits.textures; ++i)
2205 context_active_texture(ret, gl_info, i);
2206 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2207 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2211 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2213 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2215 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2217 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2219 if (!(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART))
2221 if (gl_info->supported[ARB_ES3_COMPATIBILITY])
2223 gl_info->gl_ops.gl.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
2224 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2226 else
2228 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2231 if (!(d3d_info->wined3d_creation_flags & WINED3D_LEGACY_CUBEMAP_FILTERING)
2232 && gl_info->supported[ARB_SEAMLESS_CUBE_MAP])
2234 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
2235 checkGLcall("enable seamless cube map filtering");
2237 if (gl_info->supported[ARB_CLIP_CONTROL])
2238 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT));
2239 device->shader_backend->shader_init_context_state(ret);
2240 ret->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
2241 | (1u << WINED3D_SHADER_TYPE_VERTEX)
2242 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
2243 | (1u << WINED3D_SHADER_TYPE_HULL)
2244 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
2245 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
2247 /* If this happens to be the first context for the device, dummy textures
2248 * are not created yet. In that case, they will be created (and bound) by
2249 * create_dummy_textures right after this context is initialized. */
2250 if (device->dummy_textures.tex_2d)
2251 context_bind_dummy_textures(device, ret);
2253 TRACE("Created context %p.\n", ret);
2255 return ret;
2257 out:
2258 if (ret->hdc)
2259 wined3d_release_dc(swapchain->win_handle, ret->hdc);
2260 device->shader_backend->shader_free_context_data(ret);
2261 device->adapter->fragment_pipe->free_context_data(ret);
2262 heap_free(ret->texture_type);
2263 heap_free(ret->free_fences);
2264 heap_free(ret->free_occlusion_queries);
2265 heap_free(ret->free_timestamp_queries);
2266 heap_free(ret);
2267 return NULL;
2270 void context_destroy(struct wined3d_device *device, struct wined3d_context *context)
2272 BOOL destroy;
2274 TRACE("Destroying ctx %p\n", context);
2276 wined3d_from_cs(device->cs);
2278 /* We delay destroying a context when it is active. The context_release()
2279 * function invokes context_destroy() again while leaving the last level. */
2280 if (context->level)
2282 TRACE("Delaying destruction of context %p.\n", context);
2283 context->destroy_delayed = 1;
2284 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2285 context->swapchain = NULL;
2286 return;
2289 if (context->tid == GetCurrentThreadId() || !context->current)
2291 context_destroy_gl_resources(context);
2292 TlsSetValue(wined3d_context_tls_idx, NULL);
2293 destroy = TRUE;
2295 else
2297 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2298 in wined3d_adapter may go away in the meantime */
2299 struct wined3d_gl_info *gl_info = heap_alloc(sizeof(*gl_info));
2300 *gl_info = *context->gl_info;
2301 context->gl_info = gl_info;
2302 context->destroyed = 1;
2303 destroy = FALSE;
2306 device->shader_backend->shader_free_context_data(context);
2307 device->adapter->fragment_pipe->free_context_data(context);
2308 heap_free(context->texture_type);
2309 device_context_remove(device, context);
2310 if (destroy)
2311 heap_free(context);
2314 const DWORD *context_get_tex_unit_mapping(const struct wined3d_context *context,
2315 const struct wined3d_shader_version *shader_version, unsigned int *base, unsigned int *count)
2317 const struct wined3d_gl_info *gl_info = context->gl_info;
2319 if (!shader_version)
2321 *base = 0;
2322 *count = MAX_TEXTURES;
2323 return context->tex_unit_map;
2326 if (shader_version->major >= 4)
2328 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, shader_version->type, base, count);
2329 return NULL;
2332 switch (shader_version->type)
2334 case WINED3D_SHADER_TYPE_PIXEL:
2335 *base = 0;
2336 *count = MAX_FRAGMENT_SAMPLERS;
2337 break;
2338 case WINED3D_SHADER_TYPE_VERTEX:
2339 *base = MAX_FRAGMENT_SAMPLERS;
2340 *count = MAX_VERTEX_SAMPLERS;
2341 break;
2342 default:
2343 ERR("Unhandled shader type %#x.\n", shader_version->type);
2344 *base = 0;
2345 *count = 0;
2348 return context->tex_unit_map;
2351 static void context_get_rt_size(const struct wined3d_context *context, SIZE *size)
2353 const struct wined3d_texture *rt = context->current_rt.texture;
2354 unsigned int level;
2356 if (rt->swapchain)
2358 RECT window_size;
2360 GetClientRect(context->win_handle, &window_size);
2361 size->cx = window_size.right - window_size.left;
2362 size->cy = window_size.bottom - window_size.top;
2364 return;
2367 level = context->current_rt.sub_resource_idx % rt->level_count;
2368 size->cx = wined3d_texture_get_level_width(rt, level);
2369 size->cy = wined3d_texture_get_level_height(rt, level);
2372 void context_enable_clip_distances(struct wined3d_context *context, unsigned int enable_mask)
2374 const struct wined3d_gl_info *gl_info = context->gl_info;
2375 unsigned int clip_distance_count = gl_info->limits.user_clip_distances;
2376 unsigned int i, disable_mask, current_mask;
2378 disable_mask = ~enable_mask;
2379 enable_mask &= (1u << clip_distance_count) - 1;
2380 disable_mask &= (1u << clip_distance_count) - 1;
2381 current_mask = context->clip_distance_mask;
2382 context->clip_distance_mask = enable_mask;
2384 enable_mask &= ~current_mask;
2385 while (enable_mask)
2387 i = wined3d_bit_scan(&enable_mask);
2388 gl_info->gl_ops.gl.p_glEnable(GL_CLIP_DISTANCE0 + i);
2390 disable_mask &= current_mask;
2391 while (disable_mask)
2393 i = wined3d_bit_scan(&disable_mask);
2394 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_DISTANCE0 + i);
2396 checkGLcall("toggle clip distances");
2399 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2401 return rt_mask & (1u << 31);
2404 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2406 return rt_mask & ~(1u << 31);
2409 /* Context activation is done by the caller. */
2410 static void context_apply_draw_buffers(struct wined3d_context *context, DWORD rt_mask)
2412 const struct wined3d_gl_info *gl_info = context->gl_info;
2413 GLenum draw_buffers[MAX_RENDER_TARGET_VIEWS];
2415 if (!rt_mask)
2417 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2419 else if (is_rt_mask_onscreen(rt_mask))
2421 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2423 else
2425 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2427 unsigned int i = 0;
2429 while (rt_mask)
2431 if (rt_mask & 1)
2432 draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2433 else
2434 draw_buffers[i] = GL_NONE;
2436 rt_mask >>= 1;
2437 ++i;
2440 if (gl_info->supported[ARB_DRAW_BUFFERS])
2442 GL_EXTCALL(glDrawBuffers(i, draw_buffers));
2444 else
2446 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffers[0]);
2449 else
2451 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2455 checkGLcall("apply draw buffers");
2458 /* Context activation is done by the caller. */
2459 void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
2461 const struct wined3d_gl_info *gl_info = context->gl_info;
2462 DWORD *current_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2463 DWORD new_mask = context_generate_rt_mask(buffer);
2465 if (new_mask == *current_mask)
2466 return;
2468 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2469 checkGLcall("glDrawBuffer()");
2471 *current_mask = new_mask;
2474 /* Context activation is done by the caller. */
2475 void context_active_texture(struct wined3d_context *context, const struct wined3d_gl_info *gl_info, unsigned int unit)
2477 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + unit));
2478 checkGLcall("glActiveTexture");
2479 context->active_texture = unit;
2482 void context_bind_bo(struct wined3d_context *context, GLenum binding, GLuint name)
2484 const struct wined3d_gl_info *gl_info = context->gl_info;
2486 if (binding == GL_ELEMENT_ARRAY_BUFFER)
2487 context_invalidate_state(context, STATE_INDEXBUFFER);
2489 GL_EXTCALL(glBindBuffer(binding, name));
2492 void context_bind_texture(struct wined3d_context *context, GLenum target, GLuint name)
2494 const struct wined3d_dummy_textures *textures = &context->device->dummy_textures;
2495 const struct wined3d_gl_info *gl_info = context->gl_info;
2496 DWORD unit = context->active_texture;
2497 DWORD old_texture_type = context->texture_type[unit];
2499 if (name)
2501 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2503 else
2505 target = GL_NONE;
2508 if (old_texture_type != target)
2510 switch (old_texture_type)
2512 case GL_NONE:
2513 /* nothing to do */
2514 break;
2515 case GL_TEXTURE_2D:
2516 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
2517 break;
2518 case GL_TEXTURE_2D_ARRAY:
2519 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
2520 break;
2521 case GL_TEXTURE_RECTANGLE_ARB:
2522 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
2523 break;
2524 case GL_TEXTURE_CUBE_MAP:
2525 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
2526 break;
2527 case GL_TEXTURE_CUBE_MAP_ARRAY:
2528 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
2529 break;
2530 case GL_TEXTURE_3D:
2531 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
2532 break;
2533 case GL_TEXTURE_BUFFER:
2534 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
2535 break;
2536 case GL_TEXTURE_2D_MULTISAMPLE:
2537 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
2538 break;
2539 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2540 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
2541 break;
2542 default:
2543 ERR("Unexpected texture target %#x.\n", old_texture_type);
2546 context->texture_type[unit] = target;
2549 checkGLcall("bind texture");
2552 void *context_map_bo_address(struct wined3d_context *context,
2553 const struct wined3d_bo_address *data, size_t size, GLenum binding, DWORD flags)
2555 const struct wined3d_gl_info *gl_info;
2556 BYTE *memory;
2558 if (!data->buffer_object)
2559 return data->addr;
2561 gl_info = context->gl_info;
2562 context_bind_bo(context, binding, data->buffer_object);
2564 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2566 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
2567 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
2569 else
2571 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
2572 memory += (INT_PTR)data->addr;
2575 context_bind_bo(context, binding, 0);
2576 checkGLcall("Map buffer object");
2578 return memory;
2581 void context_unmap_bo_address(struct wined3d_context *context,
2582 const struct wined3d_bo_address *data, GLenum binding)
2584 const struct wined3d_gl_info *gl_info;
2586 if (!data->buffer_object)
2587 return;
2589 gl_info = context->gl_info;
2590 context_bind_bo(context, binding, data->buffer_object);
2591 GL_EXTCALL(glUnmapBuffer(binding));
2592 context_bind_bo(context, binding, 0);
2593 checkGLcall("Unmap buffer object");
2596 void context_copy_bo_address(struct wined3d_context *context,
2597 const struct wined3d_bo_address *dst, GLenum dst_binding,
2598 const struct wined3d_bo_address *src, GLenum src_binding, size_t size)
2600 const struct wined3d_gl_info *gl_info;
2601 BYTE *dst_ptr, *src_ptr;
2603 gl_info = context->gl_info;
2605 if (dst->buffer_object && src->buffer_object)
2607 if (gl_info->supported[ARB_COPY_BUFFER])
2609 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER, src->buffer_object));
2610 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER, dst->buffer_object));
2611 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
2612 (GLintptr)src->addr, (GLintptr)dst->addr, size));
2613 checkGLcall("direct buffer copy");
2615 else
2617 src_ptr = context_map_bo_address(context, src, size, src_binding, WINED3D_MAP_READ);
2618 dst_ptr = context_map_bo_address(context, dst, size, dst_binding, WINED3D_MAP_WRITE);
2620 memcpy(dst_ptr, src_ptr, size);
2622 context_unmap_bo_address(context, dst, dst_binding);
2623 context_unmap_bo_address(context, src, src_binding);
2626 else if (!dst->buffer_object && src->buffer_object)
2628 context_bind_bo(context, src_binding, src->buffer_object);
2629 GL_EXTCALL(glGetBufferSubData(src_binding, (GLintptr)src->addr, size, dst->addr));
2630 checkGLcall("buffer download");
2632 else if (dst->buffer_object && !src->buffer_object)
2634 context_bind_bo(context, dst_binding, dst->buffer_object);
2635 GL_EXTCALL(glBufferSubData(dst_binding, (GLintptr)dst->addr, size, src->addr));
2636 checkGLcall("buffer upload");
2638 else
2640 memcpy(dst->addr, src->addr, size);
2644 static void context_set_render_offscreen(struct wined3d_context *context, BOOL offscreen)
2646 if (context->render_offscreen == offscreen)
2647 return;
2649 context_invalidate_state(context, STATE_VIEWPORT);
2650 context_invalidate_state(context, STATE_SCISSORRECT);
2651 if (!context->gl_info->supported[ARB_CLIP_CONTROL])
2653 context_invalidate_state(context, STATE_FRONTFACE);
2654 context_invalidate_state(context, STATE_POINTSPRITECOORDORIGIN);
2655 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2657 context_invalidate_state(context, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN));
2658 if (context->gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2659 context_invalidate_state(context, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
2660 context->render_offscreen = offscreen;
2663 static BOOL match_depth_stencil_format(const struct wined3d_format *existing,
2664 const struct wined3d_format *required)
2666 if (existing == required)
2667 return TRUE;
2668 if ((existing->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FLOAT)
2669 != (required->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FLOAT))
2670 return FALSE;
2671 if (existing->depth_size < required->depth_size)
2672 return FALSE;
2673 /* If stencil bits are used the exact amount is required - otherwise
2674 * wrapping won't work correctly. */
2675 if (required->stencil_size && required->stencil_size != existing->stencil_size)
2676 return FALSE;
2677 return TRUE;
2680 /* Context activation is done by the caller. */
2681 static void context_validate_onscreen_formats(struct wined3d_context *context,
2682 const struct wined3d_rendertarget_view *depth_stencil)
2684 /* Onscreen surfaces are always in a swapchain */
2685 struct wined3d_swapchain *swapchain = context->current_rt.texture->swapchain;
2687 if (context->render_offscreen || !depth_stencil) return;
2688 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->format)) return;
2690 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2691 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2692 * format. */
2693 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2695 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2696 if (!(wined3d_texture_load_location(context->current_rt.texture, context->current_rt.sub_resource_idx,
2697 context, WINED3D_LOCATION_TEXTURE_RGB)))
2698 ERR("Failed to load location.\n");
2699 swapchain->render_to_fbo = TRUE;
2700 swapchain_update_draw_bindings(swapchain);
2701 context_set_render_offscreen(context, TRUE);
2704 GLenum context_get_offscreen_gl_buffer(const struct wined3d_context *context)
2706 switch (wined3d_settings.offscreen_rendering_mode)
2708 case ORM_FBO:
2709 return GL_COLOR_ATTACHMENT0;
2711 case ORM_BACKBUFFER:
2712 return context->aux_buffers > 0 ? GL_AUX0 : GL_BACK;
2714 default:
2715 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
2716 return GL_BACK;
2720 static DWORD context_generate_rt_mask_no_fbo(const struct wined3d_context *context, struct wined3d_resource *rt)
2722 if (!rt || rt->format->id == WINED3DFMT_NULL)
2723 return 0;
2724 else if (rt->type != WINED3D_RTYPE_BUFFER && texture_from_resource(rt)->swapchain)
2725 return context_generate_rt_mask_from_resource(rt);
2726 else
2727 return context_generate_rt_mask(context_get_offscreen_gl_buffer(context));
2730 /* Context activation is done by the caller. */
2731 void context_apply_blit_state(struct wined3d_context *context, const struct wined3d_device *device)
2733 const struct wined3d_gl_info *gl_info = context->gl_info;
2734 struct wined3d_texture *rt = context->current_rt.texture;
2735 DWORD rt_mask, *cur_mask;
2736 unsigned int sampler;
2737 SIZE rt_size;
2739 TRACE("Setting up context %p for blitting.\n", context);
2741 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2743 if (context->render_offscreen)
2745 wined3d_texture_load(rt, context, FALSE);
2747 context_apply_fbo_state_blit(context, GL_FRAMEBUFFER, &rt->resource,
2748 context->current_rt.sub_resource_idx, NULL, 0, rt->resource.draw_binding);
2749 if (rt->resource.format->id != WINED3DFMT_NULL)
2750 rt_mask = 1;
2751 else
2752 rt_mask = 0;
2754 else
2756 context->current_fbo = NULL;
2757 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
2758 rt_mask = context_generate_rt_mask_from_resource(&rt->resource);
2761 else
2763 rt_mask = context_generate_rt_mask_no_fbo(context, &rt->resource);
2766 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2768 if (rt_mask != *cur_mask)
2770 context_apply_draw_buffers(context, rt_mask);
2771 *cur_mask = rt_mask;
2774 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2776 context_check_fbo_status(context, GL_FRAMEBUFFER);
2778 context_invalidate_state(context, STATE_FRAMEBUFFER);
2780 context_get_rt_size(context, &rt_size);
2782 if (context->last_was_blit)
2784 if (context->blit_w != rt_size.cx || context->blit_h != rt_size.cy)
2786 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
2787 context->blit_w = rt_size.cx;
2788 context->blit_h = rt_size.cy;
2789 /* No need to dirtify here, the states are still dirtified because
2790 * they weren't applied since the last context_apply_blit_state()
2791 * call. */
2793 checkGLcall("blit state application");
2794 TRACE("Context is already set up for blitting, nothing to do.\n");
2795 return;
2797 context->last_was_blit = TRUE;
2799 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
2800 GL_EXTCALL(glBindSampler(0, 0));
2801 context_active_texture(context, gl_info, 0);
2803 sampler = context->rev_tex_unit_map[0];
2804 if (sampler != WINED3D_UNMAPPED_STAGE)
2806 if (sampler < MAX_TEXTURES)
2808 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
2809 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2811 context_invalidate_state(context, STATE_SAMPLER(sampler));
2814 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2816 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
2817 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
2819 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
2820 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZENABLE));
2821 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2822 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2823 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
2824 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CULLMODE));
2825 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
2826 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILENABLE));
2827 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
2828 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2829 if (gl_info->supported[ARB_POINT_SPRITE])
2831 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
2832 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
2834 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2835 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
2836 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
2837 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
2838 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
2840 context->last_was_rhw = TRUE;
2841 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
2843 context_enable_clip_distances(context, 0);
2844 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
2846 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
2847 if (gl_info->supported[ARB_CLIP_CONTROL])
2848 GL_EXTCALL(glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE));
2849 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
2850 context_invalidate_state(context, STATE_VIEWPORT);
2852 device->shader_backend->shader_disable(device->shader_priv, context);
2854 context->blit_w = rt_size.cx;
2855 context->blit_h = rt_size.cy;
2857 checkGLcall("blit state application");
2860 static void context_apply_blit_projection(const struct wined3d_context *context, unsigned int w, unsigned int h)
2862 const struct wined3d_gl_info *gl_info = context->gl_info;
2863 const GLdouble projection[] =
2865 2.0 / w, 0.0, 0.0, 0.0,
2866 0.0, 2.0 / h, 0.0, 0.0,
2867 0.0, 0.0, 2.0, 0.0,
2868 -1.0, -1.0, -1.0, 1.0,
2871 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
2872 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
2875 /* Setup OpenGL states for fixed-function blitting. */
2876 /* Context activation is done by the caller. */
2877 void context_apply_ffp_blit_state(struct wined3d_context *context, const struct wined3d_device *device)
2879 const struct wined3d_gl_info *gl_info = context->gl_info;
2880 unsigned int i, sampler;
2882 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2883 ERR("Applying fixed-function state without legacy context support.\n");
2885 if (context->last_was_ffp_blit)
2887 SIZE rt_size;
2889 context_get_rt_size(context, &rt_size);
2890 if (context->blit_w != rt_size.cx || context->blit_h != rt_size.cy)
2891 context_apply_blit_projection(context, rt_size.cx, rt_size.cy);
2892 context_apply_blit_state(context, device);
2894 checkGLcall("ffp blit state application");
2895 return;
2897 context->last_was_ffp_blit = TRUE;
2899 context_apply_blit_state(context, device);
2901 /* Disable all textures. The caller can then bind a texture it wants to blit
2902 * from. */
2903 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
2905 context_active_texture(context, gl_info, i);
2907 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2908 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2909 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2910 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2911 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2912 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2914 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2916 sampler = context->rev_tex_unit_map[i];
2917 if (sampler != WINED3D_UNMAPPED_STAGE)
2919 if (sampler < MAX_TEXTURES)
2920 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2921 context_invalidate_state(context, STATE_SAMPLER(sampler));
2925 context_active_texture(context, gl_info, 0);
2927 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2928 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2929 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2930 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2931 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2932 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2934 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2935 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
2936 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
2938 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
2939 gl_info->gl_ops.gl.p_glLoadIdentity();
2941 /* Setup transforms. */
2942 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
2943 gl_info->gl_ops.gl.p_glLoadIdentity();
2944 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2945 context_apply_blit_projection(context, context->blit_w, context->blit_h);
2946 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2948 /* Other misc states. */
2949 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
2950 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
2951 glDisableWINE(GL_FOG);
2952 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
2954 if (gl_info->supported[EXT_SECONDARY_COLOR])
2956 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
2957 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
2959 checkGLcall("ffp blit state application");
2962 static BOOL have_framebuffer_attachment(unsigned int rt_count, struct wined3d_rendertarget_view * const *rts,
2963 const struct wined3d_rendertarget_view *ds)
2965 unsigned int i;
2967 if (ds)
2968 return TRUE;
2970 for (i = 0; i < rt_count; ++i)
2972 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
2973 return TRUE;
2976 return FALSE;
2979 /* Context activation is done by the caller. */
2980 BOOL context_apply_clear_state(struct wined3d_context *context, const struct wined3d_state *state,
2981 UINT rt_count, const struct wined3d_fb_state *fb)
2983 struct wined3d_rendertarget_view * const *rts = fb->render_targets;
2984 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
2985 const struct wined3d_gl_info *gl_info = context->gl_info;
2986 DWORD rt_mask = 0, *cur_mask;
2987 unsigned int i;
2989 if (isStateDirty(context, STATE_FRAMEBUFFER) || fb != state->fb
2990 || rt_count != gl_info->limits.buffers)
2992 if (!have_framebuffer_attachment(rt_count, rts, dsv))
2994 WARN("Invalid render target config, need at least one attachment.\n");
2995 return FALSE;
2998 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3000 struct wined3d_rendertarget_info ds_info = {{0}};
3002 context_validate_onscreen_formats(context, dsv);
3004 if (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource))
3006 memset(context->blit_targets, 0, sizeof(context->blit_targets));
3007 for (i = 0; i < rt_count; ++i)
3009 if (rts[i])
3011 context->blit_targets[i].gl_view = rts[i]->gl_view;
3012 context->blit_targets[i].resource = rts[i]->resource;
3013 context->blit_targets[i].sub_resource_idx = rts[i]->sub_resource_idx;
3014 context->blit_targets[i].layer_count = rts[i]->layer_count;
3016 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3017 rt_mask |= (1u << i);
3020 if (dsv)
3022 ds_info.gl_view = dsv->gl_view;
3023 ds_info.resource = dsv->resource;
3024 ds_info.sub_resource_idx = dsv->sub_resource_idx;
3025 ds_info.layer_count = dsv->layer_count;
3028 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets, &ds_info,
3029 rt_count ? rts[0]->resource->draw_binding : 0,
3030 dsv ? dsv->resource->draw_binding : 0);
3032 else
3034 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, &ds_info,
3035 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3036 rt_mask = context_generate_rt_mask_from_resource(rts[0]->resource);
3039 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3040 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3041 * state management allows this */
3042 context_invalidate_state(context, STATE_FRAMEBUFFER);
3044 else
3046 rt_mask = context_generate_rt_mask_no_fbo(context, rt_count ? rts[0]->resource : NULL);
3049 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3050 && (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource)))
3052 for (i = 0; i < rt_count; ++i)
3054 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3055 rt_mask |= (1u << i);
3058 else
3060 rt_mask = context_generate_rt_mask_no_fbo(context, rt_count ? rts[0]->resource : NULL);
3063 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
3065 if (rt_mask != *cur_mask)
3067 context_apply_draw_buffers(context, rt_mask);
3068 *cur_mask = rt_mask;
3069 context_invalidate_state(context, STATE_FRAMEBUFFER);
3072 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3074 context_check_fbo_status(context, GL_FRAMEBUFFER);
3077 context->last_was_blit = FALSE;
3078 context->last_was_ffp_blit = FALSE;
3080 /* Blending and clearing should be orthogonal, but tests on the nvidia
3081 * driver show that disabling blending when clearing improves the clearing
3082 * performance incredibly. */
3083 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3084 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
3085 if (rt_count && gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3087 if (needs_srgb_write(context, state, fb))
3088 gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
3089 else
3090 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3091 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3093 checkGLcall("setting up state for clear");
3095 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
3096 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
3097 context_invalidate_state(context, STATE_SCISSORRECT);
3099 return TRUE;
3102 static DWORD find_draw_buffers_mask(const struct wined3d_context *context, const struct wined3d_state *state)
3104 struct wined3d_rendertarget_view * const *rts = state->fb->render_targets;
3105 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3106 DWORD rt_mask, mask;
3107 unsigned int i;
3109 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3110 return context_generate_rt_mask_no_fbo(context, rts[0]->resource);
3111 else if (!context->render_offscreen)
3112 return context_generate_rt_mask_from_resource(rts[0]->resource);
3114 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
3115 rt_mask &= context->d3d_info->valid_rt_mask;
3117 mask = rt_mask;
3118 while (mask)
3120 i = wined3d_bit_scan(&mask);
3121 if (!rts[i] || rts[i]->format->id == WINED3DFMT_NULL)
3122 rt_mask &= ~(1u << i);
3125 return rt_mask;
3128 /* Context activation is done by the caller. */
3129 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3131 DWORD rt_mask = find_draw_buffers_mask(context, state);
3132 const struct wined3d_fb_state *fb = state->fb;
3133 DWORD color_location = 0;
3134 DWORD *cur_mask;
3136 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3138 struct wined3d_rendertarget_info ds_info = {{0}};
3140 if (!context->render_offscreen)
3142 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, &ds_info,
3143 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3145 else
3147 unsigned int i;
3149 memset(context->blit_targets, 0, sizeof(context->blit_targets));
3150 for (i = 0; i < context->gl_info->limits.buffers; ++i)
3152 if (!fb->render_targets[i])
3153 continue;
3155 context->blit_targets[i].gl_view = fb->render_targets[i]->gl_view;
3156 context->blit_targets[i].resource = fb->render_targets[i]->resource;
3157 context->blit_targets[i].sub_resource_idx = fb->render_targets[i]->sub_resource_idx;
3158 context->blit_targets[i].layer_count = fb->render_targets[i]->layer_count;
3160 if (!color_location)
3161 color_location = fb->render_targets[i]->resource->draw_binding;
3164 if (fb->depth_stencil)
3166 ds_info.gl_view = fb->depth_stencil->gl_view;
3167 ds_info.resource = fb->depth_stencil->resource;
3168 ds_info.sub_resource_idx = fb->depth_stencil->sub_resource_idx;
3169 ds_info.layer_count = fb->depth_stencil->layer_count;
3172 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets, &ds_info,
3173 color_location, fb->depth_stencil ? fb->depth_stencil->resource->draw_binding : 0);
3177 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
3178 if (rt_mask != *cur_mask)
3180 context_apply_draw_buffers(context, rt_mask);
3181 *cur_mask = rt_mask;
3183 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
3186 static void context_map_stage(struct wined3d_context *context, DWORD stage, DWORD unit)
3188 DWORD i = context->rev_tex_unit_map[unit];
3189 DWORD j = context->tex_unit_map[stage];
3191 TRACE("Mapping stage %u to unit %u.\n", stage, unit);
3192 context->tex_unit_map[stage] = unit;
3193 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
3194 context->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
3196 context->rev_tex_unit_map[unit] = stage;
3197 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
3198 context->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
3201 static void context_invalidate_texture_stage(struct wined3d_context *context, DWORD stage)
3203 DWORD i;
3205 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
3206 context_invalidate_state(context, STATE_TEXTURESTAGE(stage, i));
3209 static void context_update_fixed_function_usage_map(struct wined3d_context *context,
3210 const struct wined3d_state *state)
3212 UINT i, start, end;
3214 context->fixed_function_usage_map = 0;
3215 for (i = 0; i < MAX_TEXTURES; ++i)
3217 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
3218 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
3219 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
3220 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
3221 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
3222 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
3223 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
3224 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
3226 /* Not used, and disable higher stages. */
3227 if (color_op == WINED3D_TOP_DISABLE)
3228 break;
3230 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
3231 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
3232 || ((color_arg3 == WINED3DTA_TEXTURE)
3233 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
3234 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
3235 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
3236 || ((alpha_arg3 == WINED3DTA_TEXTURE)
3237 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
3238 context->fixed_function_usage_map |= (1u << i);
3240 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
3241 && i < MAX_TEXTURES - 1)
3242 context->fixed_function_usage_map |= (1u << (i + 1));
3245 if (i < context->lowest_disabled_stage)
3247 start = i;
3248 end = context->lowest_disabled_stage;
3250 else
3252 start = context->lowest_disabled_stage;
3253 end = i;
3256 context->lowest_disabled_stage = i;
3257 for (i = start + 1; i < end; ++i)
3259 context_invalidate_state(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3263 static void context_map_fixed_function_samplers(struct wined3d_context *context,
3264 const struct wined3d_state *state)
3266 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3267 unsigned int i, tex;
3268 WORD ffu_map;
3270 ffu_map = context->fixed_function_usage_map;
3272 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
3273 || context->lowest_disabled_stage <= d3d_info->limits.ffp_textures)
3275 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3277 if (!(ffu_map & 1))
3278 continue;
3280 if (context->tex_unit_map[i] != i)
3282 context_map_stage(context, i, i);
3283 context_invalidate_state(context, STATE_SAMPLER(i));
3284 context_invalidate_texture_stage(context, i);
3287 return;
3290 /* Now work out the mapping */
3291 tex = 0;
3292 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3294 if (!(ffu_map & 1))
3295 continue;
3297 if (context->tex_unit_map[i] != tex)
3299 context_map_stage(context, i, tex);
3300 context_invalidate_state(context, STATE_SAMPLER(i));
3301 context_invalidate_texture_stage(context, i);
3304 ++tex;
3308 static void context_map_psamplers(struct wined3d_context *context, const struct wined3d_state *state)
3310 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3311 const struct wined3d_shader_resource_info *resource_info =
3312 state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3313 unsigned int i;
3315 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3317 if (resource_info[i].type && context->tex_unit_map[i] != i)
3319 context_map_stage(context, i, i);
3320 context_invalidate_state(context, STATE_SAMPLER(i));
3321 if (i < d3d_info->limits.ffp_blend_stages)
3322 context_invalidate_texture_stage(context, i);
3327 static BOOL context_unit_free_for_vs(const struct wined3d_context *context,
3328 const struct wined3d_shader_resource_info *ps_resource_info, DWORD unit)
3330 DWORD current_mapping = context->rev_tex_unit_map[unit];
3332 /* Not currently used */
3333 if (current_mapping == WINED3D_UNMAPPED_STAGE)
3334 return TRUE;
3336 if (current_mapping < MAX_FRAGMENT_SAMPLERS)
3338 /* Used by a fragment sampler */
3340 if (!ps_resource_info)
3342 /* No pixel shader, check fixed function */
3343 return current_mapping >= MAX_TEXTURES || !(context->fixed_function_usage_map & (1u << current_mapping));
3346 /* Pixel shader, check the shader's sampler map */
3347 return !ps_resource_info[current_mapping].type;
3350 return TRUE;
3353 static void context_map_vsamplers(struct wined3d_context *context, BOOL ps, const struct wined3d_state *state)
3355 const struct wined3d_shader_resource_info *vs_resource_info =
3356 state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info;
3357 const struct wined3d_shader_resource_info *ps_resource_info = NULL;
3358 const struct wined3d_gl_info *gl_info = context->gl_info;
3359 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.graphics_samplers) - 1;
3360 int i;
3362 /* Note that we only care if a resource is used or not, not the
3363 * resource's specific type. Otherwise we'd need to call
3364 * shader_update_samplers() here for 1.x pixelshaders. */
3365 if (ps)
3366 ps_resource_info = state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3368 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
3370 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
3371 if (vs_resource_info[i].type)
3373 while (start >= 0)
3375 if (context_unit_free_for_vs(context, ps_resource_info, start))
3377 if (context->tex_unit_map[vsampler_idx] != start)
3379 context_map_stage(context, vsampler_idx, start);
3380 context_invalidate_state(context, STATE_SAMPLER(vsampler_idx));
3383 --start;
3384 break;
3387 --start;
3389 if (context->tex_unit_map[vsampler_idx] == WINED3D_UNMAPPED_STAGE)
3390 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i);
3395 static void context_update_tex_unit_map(struct wined3d_context *context, const struct wined3d_state *state)
3397 const struct wined3d_gl_info *gl_info = context->gl_info;
3398 BOOL vs = use_vs(state);
3399 BOOL ps = use_ps(state);
3401 if (!ps)
3402 context_update_fixed_function_usage_map(context, state);
3404 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3405 * need a 1:1 map at the moment.
3406 * When the mapping of a stage is changed, sampler and ALL texture stage
3407 * states have to be reset. */
3409 if (gl_info->limits.graphics_samplers >= MAX_COMBINED_SAMPLERS)
3410 return;
3412 if (ps)
3413 context_map_psamplers(context, state);
3414 else
3415 context_map_fixed_function_samplers(context, state);
3417 if (vs)
3418 context_map_vsamplers(context, ps, state);
3421 /* Context activation is done by the caller. */
3422 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3424 DWORD rt_mask, *cur_mask;
3426 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
3428 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
3429 rt_mask = find_draw_buffers_mask(context, state);
3430 if (rt_mask != *cur_mask)
3432 context_apply_draw_buffers(context, rt_mask);
3433 *cur_mask = rt_mask;
3437 static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
3439 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
3440 *regnum = WINED3D_FFP_POSITION;
3441 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
3442 *regnum = WINED3D_FFP_BLENDWEIGHT;
3443 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
3444 *regnum = WINED3D_FFP_BLENDINDICES;
3445 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
3446 *regnum = WINED3D_FFP_NORMAL;
3447 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
3448 *regnum = WINED3D_FFP_PSIZE;
3449 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
3450 *regnum = WINED3D_FFP_DIFFUSE;
3451 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
3452 *regnum = WINED3D_FFP_SPECULAR;
3453 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
3454 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
3455 else
3457 WARN("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage), usage_idx);
3458 *regnum = ~0u;
3459 return FALSE;
3462 return TRUE;
3465 /* Context activation is done by the caller. */
3466 void wined3d_stream_info_from_declaration(struct wined3d_stream_info *stream_info,
3467 const struct wined3d_state *state, const struct wined3d_gl_info *gl_info,
3468 const struct wined3d_d3d_info *d3d_info)
3470 /* We need to deal with frequency data! */
3471 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
3472 BOOL generic_attributes = d3d_info->ffp_generic_attributes;
3473 BOOL use_vshader = use_vs(state);
3474 unsigned int i;
3476 stream_info->use_map = 0;
3477 stream_info->swizzle_map = 0;
3478 stream_info->position_transformed = 0;
3480 if (!declaration)
3481 return;
3483 stream_info->position_transformed = declaration->position_transformed;
3485 /* Translate the declaration into strided data. */
3486 for (i = 0; i < declaration->element_count; ++i)
3488 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
3489 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
3490 BOOL stride_used;
3491 unsigned int idx;
3493 TRACE("%p Element %p (%u of %u).\n", declaration->elements,
3494 element, i + 1, declaration->element_count);
3496 if (!stream->buffer)
3497 continue;
3499 TRACE("offset %u input_slot %u usage_idx %d.\n", element->offset, element->input_slot, element->usage_idx);
3501 if (use_vshader)
3503 if (element->output_slot == WINED3D_OUTPUT_SLOT_UNUSED)
3505 stride_used = FALSE;
3507 else if (element->output_slot == WINED3D_OUTPUT_SLOT_SEMANTIC)
3509 /* TODO: Assuming vertexdeclarations are usually used with the
3510 * same or a similar shader, it might be worth it to store the
3511 * last used output slot and try that one first. */
3512 stride_used = vshader_get_input(state->shader[WINED3D_SHADER_TYPE_VERTEX],
3513 element->usage, element->usage_idx, &idx);
3515 else
3517 idx = element->output_slot;
3518 stride_used = TRUE;
3521 else
3523 if (!generic_attributes && !element->ffp_valid)
3525 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3526 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
3527 stride_used = FALSE;
3529 else
3531 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
3535 if (stride_used)
3537 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3538 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3539 use_vshader ? "shader": "fixed function", idx,
3540 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
3541 element->offset, stream->stride, debug_d3dformat(element->format->id),
3542 debug_d3dinput_classification(element->input_slot_class), element->instance_data_step_rate);
3544 stream_info->elements[idx].format = element->format;
3545 stream_info->elements[idx].data.buffer_object = 0;
3546 stream_info->elements[idx].data.addr = (BYTE *)NULL + stream->offset + element->offset;
3547 stream_info->elements[idx].stride = stream->stride;
3548 stream_info->elements[idx].stream_idx = element->input_slot;
3549 if (stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
3551 stream_info->elements[idx].divisor = 1;
3553 else if (element->input_slot_class == WINED3D_INPUT_PER_INSTANCE_DATA)
3555 stream_info->elements[idx].divisor = element->instance_data_step_rate;
3556 if (!element->instance_data_step_rate)
3557 FIXME("Instance step rate 0 not implemented.\n");
3559 else
3561 stream_info->elements[idx].divisor = 0;
3564 if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
3565 && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
3567 stream_info->swizzle_map |= 1u << idx;
3569 stream_info->use_map |= 1u << idx;
3574 /* Context activation is done by the caller. */
3575 static void context_update_stream_info(struct wined3d_context *context, const struct wined3d_state *state)
3577 struct wined3d_stream_info *stream_info = &context->stream_info;
3578 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3579 const struct wined3d_gl_info *gl_info = context->gl_info;
3580 DWORD prev_all_vbo = stream_info->all_vbo;
3581 unsigned int i;
3582 WORD map;
3584 wined3d_stream_info_from_declaration(stream_info, state, gl_info, d3d_info);
3586 stream_info->all_vbo = 1;
3587 context->buffer_fence_count = 0;
3588 for (i = 0, map = stream_info->use_map; map; map >>= 1, ++i)
3590 struct wined3d_stream_info_element *element;
3591 struct wined3d_bo_address data;
3592 struct wined3d_buffer *buffer;
3594 if (!(map & 1))
3595 continue;
3597 element = &stream_info->elements[i];
3598 buffer = state->streams[element->stream_idx].buffer;
3600 /* We can't use VBOs if the base vertex index is negative. OpenGL
3601 * doesn't accept negative offsets (or rather offsets bigger than the
3602 * VBO, because the pointer is unsigned), so use system memory
3603 * sources. In most sane cases the pointer - offset will still be > 0,
3604 * otherwise it will wrap around to some big value. Hope that with the
3605 * indices the driver wraps it back internally. If not,
3606 * draw_primitive_immediate_mode() is needed, including a vertex buffer
3607 * path. */
3608 if (state->load_base_vertex_index < 0)
3610 WARN_(d3d_perf)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3611 state->load_base_vertex_index);
3612 element->data.buffer_object = 0;
3613 element->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3614 if ((UINT_PTR)element->data.addr < -state->load_base_vertex_index * element->stride)
3615 FIXME("System memory vertex data load offset is negative!\n");
3617 else
3619 wined3d_buffer_load(buffer, context, state);
3620 wined3d_buffer_get_memory(buffer, &data, buffer->locations);
3621 element->data.buffer_object = data.buffer_object;
3622 element->data.addr += (ULONG_PTR)data.addr;
3625 if (!element->data.buffer_object)
3626 stream_info->all_vbo = 0;
3628 if (buffer->fence)
3629 context->buffer_fences[context->buffer_fence_count++] = buffer->fence;
3631 TRACE("Load array %u {%#x:%p}.\n", i, element->data.buffer_object, element->data.addr);
3634 if (prev_all_vbo != stream_info->all_vbo)
3635 context_invalidate_state(context, STATE_INDEXBUFFER);
3637 context->use_immediate_mode_draw = FALSE;
3639 if (stream_info->all_vbo)
3640 return;
3642 if (use_vs(state))
3644 if (state->vertex_declaration->half_float_conv_needed)
3646 TRACE("Using immediate mode draw with vertex shaders for FLOAT16 conversion.\n");
3647 context->use_immediate_mode_draw = TRUE;
3650 else
3652 WORD slow_mask = -!d3d_info->ffp_generic_attributes & (1u << WINED3D_FFP_PSIZE);
3653 slow_mask |= -(!gl_info->supported[ARB_VERTEX_ARRAY_BGRA] && !d3d_info->ffp_generic_attributes)
3654 & ((1u << WINED3D_FFP_DIFFUSE) | (1u << WINED3D_FFP_SPECULAR) | (1u << WINED3D_FFP_BLENDWEIGHT));
3656 if ((stream_info->position_transformed && !d3d_info->xyzrhw)
3657 || (stream_info->use_map & slow_mask))
3658 context->use_immediate_mode_draw = TRUE;
3662 /* Context activation is done by the caller. */
3663 static void context_preload_texture(struct wined3d_context *context,
3664 const struct wined3d_state *state, unsigned int idx)
3666 struct wined3d_texture *texture;
3668 if (!(texture = state->textures[idx]))
3669 return;
3671 wined3d_texture_load(texture, context, state->sampler_states[idx][WINED3D_SAMP_SRGB_TEXTURE]);
3674 /* Context activation is done by the caller. */
3675 static void context_preload_textures(struct wined3d_context *context, const struct wined3d_state *state)
3677 unsigned int i;
3679 if (use_vs(state))
3681 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
3683 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info[i].type)
3684 context_preload_texture(context, state, MAX_FRAGMENT_SAMPLERS + i);
3688 if (use_ps(state))
3690 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3692 if (state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info[i].type)
3693 context_preload_texture(context, state, i);
3696 else
3698 WORD ffu_map = context->fixed_function_usage_map;
3700 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3702 if (ffu_map & 1)
3703 context_preload_texture(context, state, i);
3708 static void context_load_shader_resources(struct wined3d_context *context, const struct wined3d_state *state,
3709 unsigned int shader_mask)
3711 struct wined3d_shader_sampler_map_entry *entry;
3712 struct wined3d_shader_resource_view *view;
3713 struct wined3d_shader *shader;
3714 unsigned int i, j;
3716 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
3718 if (!(shader_mask & (1u << i)))
3719 continue;
3721 if (!(shader = state->shader[i]))
3722 continue;
3724 for (j = 0; j < WINED3D_MAX_CBS; ++j)
3726 if (state->cb[i][j])
3727 wined3d_buffer_load(state->cb[i][j], context, state);
3730 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
3732 entry = &shader->reg_maps.sampler_map.entries[j];
3734 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
3735 continue;
3737 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3738 wined3d_buffer_load(buffer_from_resource(view->resource), context, state);
3739 else
3740 wined3d_texture_load(texture_from_resource(view->resource), context, FALSE);
3745 static void context_bind_shader_resources(struct wined3d_context *context,
3746 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
3748 unsigned int bind_idx, shader_sampler_count, base, count, i;
3749 const struct wined3d_device *device = context->device;
3750 struct wined3d_shader_sampler_map_entry *entry;
3751 struct wined3d_shader_resource_view *view;
3752 const struct wined3d_shader *shader;
3753 struct wined3d_sampler *sampler;
3754 const DWORD *tex_unit_map;
3756 if (!(shader = state->shader[shader_type]))
3757 return;
3759 tex_unit_map = context_get_tex_unit_mapping(context,
3760 &shader->reg_maps.shader_version, &base, &count);
3762 shader_sampler_count = shader->reg_maps.sampler_map.count;
3763 if (shader_sampler_count > count)
3764 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3765 shader, shader_sampler_count, count);
3766 count = min(shader_sampler_count, count);
3768 for (i = 0; i < count; ++i)
3770 entry = &shader->reg_maps.sampler_map.entries[i];
3771 bind_idx = base + entry->bind_idx;
3772 if (tex_unit_map)
3773 bind_idx = tex_unit_map[bind_idx];
3775 if (!(view = state->shader_resource_view[shader_type][entry->resource_idx]))
3777 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
3778 continue;
3781 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
3782 sampler = device->default_sampler;
3783 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
3784 sampler = device->null_sampler;
3785 wined3d_shader_resource_view_bind(view, bind_idx, sampler, context);
3789 static void context_load_unordered_access_resources(struct wined3d_context *context,
3790 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3792 struct wined3d_unordered_access_view *view;
3793 struct wined3d_texture *texture;
3794 struct wined3d_buffer *buffer;
3795 unsigned int i;
3797 context->uses_uavs = 0;
3799 if (!shader)
3800 return;
3802 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3804 if (!(view = views[i]))
3805 continue;
3807 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3809 buffer = buffer_from_resource(view->resource);
3810 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);
3811 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
3813 else
3815 texture = texture_from_resource(view->resource);
3816 wined3d_texture_load(texture, context, FALSE);
3817 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_TEXTURE_RGB);
3820 context->uses_uavs = 1;
3824 static void context_bind_unordered_access_views(struct wined3d_context *context,
3825 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3827 const struct wined3d_gl_info *gl_info = context->gl_info;
3828 struct wined3d_unordered_access_view *view;
3829 GLuint texture_name;
3830 unsigned int i;
3831 GLint level;
3833 if (!shader)
3834 return;
3836 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3838 if (!(view = views[i]))
3840 if (shader->reg_maps.uav_resource_info[i].type)
3841 WARN("No unordered access view bound at index %u.\n", i);
3842 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3843 continue;
3846 if (view->gl_view.name)
3848 texture_name = view->gl_view.name;
3849 level = 0;
3851 else if (view->resource->type != WINED3D_RTYPE_BUFFER)
3853 struct wined3d_texture *texture = texture_from_resource(view->resource);
3854 texture_name = wined3d_texture_get_texture_name(texture, context, FALSE);
3855 level = view->desc.u.texture.level_idx;
3857 else
3859 FIXME("Unsupported buffer unordered access view.\n");
3860 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3861 continue;
3864 GL_EXTCALL(glBindImageTexture(i, texture_name, level, GL_TRUE, 0, GL_READ_WRITE,
3865 view->format->glInternal));
3867 if (view->counter_bo)
3868 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, i, view->counter_bo));
3870 checkGLcall("Bind unordered access views");
3873 static void context_load_stream_output_buffers(struct wined3d_context *context,
3874 const struct wined3d_state *state)
3876 unsigned int i;
3878 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3880 struct wined3d_buffer *buffer;
3881 if (!(buffer = state->stream_output[i].buffer))
3882 continue;
3884 wined3d_buffer_load(buffer, context, state);
3885 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
3889 /* Context activation is done by the caller. */
3890 static BOOL context_apply_draw_state(struct wined3d_context *context,
3891 const struct wined3d_device *device, const struct wined3d_state *state)
3893 const struct StateEntry *state_table = context->state_table;
3894 const struct wined3d_gl_info *gl_info = context->gl_info;
3895 const struct wined3d_fb_state *fb = state->fb;
3896 unsigned int i;
3897 WORD map;
3899 if (!have_framebuffer_attachment(gl_info->limits.buffers, fb->render_targets, fb->depth_stencil))
3901 if (!gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
3903 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
3904 return FALSE;
3907 context_set_render_offscreen(context, TRUE);
3910 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && isStateDirty(context, STATE_FRAMEBUFFER))
3912 context_validate_onscreen_formats(context, fb->depth_stencil);
3915 /* Preload resources before FBO setup. Texture preload in particular may
3916 * result in changes to the current FBO, due to using e.g. FBO blits for
3917 * updating a resource location. */
3918 context_update_tex_unit_map(context, state);
3919 context_preload_textures(context, state);
3920 context_load_shader_resources(context, state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
3921 context_load_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_PIXEL],
3922 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3923 context_load_stream_output_buffers(context, state);
3924 /* TODO: Right now the dependency on the vertex shader is necessary
3925 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3926 * the current VS but maybe it's possible to relax the coupling in some
3927 * situations at least. */
3928 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC)
3929 || isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
3931 context_update_stream_info(context, state);
3933 else
3935 for (i = 0, map = context->stream_info.use_map; map; map >>= 1, ++i)
3937 if (map & 1)
3938 wined3d_buffer_load(state->streams[context->stream_info.elements[i].stream_idx].buffer,
3939 context, state);
3941 /* Loading the buffers above may have invalidated the stream info. */
3942 if (isStateDirty(context, STATE_STREAMSRC))
3943 context_update_stream_info(context, state);
3945 if (state->index_buffer)
3947 if (context->stream_info.all_vbo)
3948 wined3d_buffer_load(state->index_buffer, context, state);
3949 else
3950 wined3d_buffer_load_sysmem(state->index_buffer, context);
3953 for (i = 0; i < context->numDirtyEntries; ++i)
3955 DWORD rep = context->dirtyArray[i];
3956 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
3957 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
3958 context->isStateDirty[idx] &= ~(1u << shift);
3959 state_table[rep].apply(context, state, rep);
3962 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
3964 device->shader_backend->shader_select(device->shader_priv, context, state);
3965 context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3968 if (context->constant_update_mask)
3970 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
3971 context->constant_update_mask = 0;
3974 if (context->update_shader_resource_bindings)
3976 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
3977 context_bind_shader_resources(context, state, i);
3978 context->update_shader_resource_bindings = 0;
3979 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
3980 context->update_compute_shader_resource_bindings = 1;
3983 if (context->update_unordered_access_view_bindings)
3985 context_bind_unordered_access_views(context,
3986 state->shader[WINED3D_SHADER_TYPE_PIXEL],
3987 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3988 context->update_unordered_access_view_bindings = 0;
3989 context->update_compute_unordered_access_view_bindings = 1;
3992 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3994 context_check_fbo_status(context, GL_FRAMEBUFFER);
3997 context->numDirtyEntries = 0; /* This makes the whole list clean */
3998 context->last_was_blit = FALSE;
3999 context->last_was_ffp_blit = FALSE;
4001 return TRUE;
4004 static void context_apply_compute_state(struct wined3d_context *context,
4005 const struct wined3d_device *device, const struct wined3d_state *state)
4007 const struct StateEntry *state_table = context->state_table;
4008 const struct wined3d_gl_info *gl_info = context->gl_info;
4009 unsigned int state_id, i;
4011 context_load_shader_resources(context, state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
4012 context_load_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4013 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4015 for (i = 0, state_id = STATE_COMPUTE_OFFSET; i < ARRAY_SIZE(context->dirty_compute_states); ++i)
4017 unsigned int dirty_mask = context->dirty_compute_states[i];
4018 while (dirty_mask)
4020 unsigned int current_state_id = state_id + wined3d_bit_scan(&dirty_mask);
4021 state_table[current_state_id].apply(context, state, current_state_id);
4023 state_id += sizeof(*context->dirty_compute_states) * CHAR_BIT;
4025 memset(context->dirty_compute_states, 0, sizeof(*context->dirty_compute_states));
4027 if (context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
4029 device->shader_backend->shader_select_compute(device->shader_priv, context, state);
4030 context->shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
4033 if (context->update_compute_shader_resource_bindings)
4035 context_bind_shader_resources(context, state, WINED3D_SHADER_TYPE_COMPUTE);
4036 context->update_compute_shader_resource_bindings = 0;
4037 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4038 context->update_shader_resource_bindings = 1;
4041 if (context->update_compute_unordered_access_view_bindings)
4043 context_bind_unordered_access_views(context,
4044 state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4045 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4046 context->update_compute_unordered_access_view_bindings = 0;
4047 context->update_unordered_access_view_bindings = 1;
4050 /* Updates to currently bound render targets aren't necessarily coherent
4051 * between the graphics and compute pipelines. Unbind any currently bound
4052 * FBO here to ensure preceding updates to its attachments by the graphics
4053 * pipeline are visible to the compute pipeline.
4055 * Without this, the bloom effect in Nier:Automata is too bright on the
4056 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4057 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
4058 context_invalidate_state(context, STATE_FRAMEBUFFER);
4060 context->last_was_blit = FALSE;
4061 context->last_was_ffp_blit = FALSE;
4064 static BOOL use_transform_feedback(const struct wined3d_state *state)
4066 const struct wined3d_shader *shader;
4067 if (!(shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
4068 return FALSE;
4069 return shader->u.gs.so_desc.element_count;
4072 void context_end_transform_feedback(struct wined3d_context *context)
4074 const struct wined3d_gl_info *gl_info = context->gl_info;
4075 if (context->transform_feedback_active)
4077 GL_EXTCALL(glEndTransformFeedback());
4078 checkGLcall("glEndTransformFeedback");
4079 context->transform_feedback_active = 0;
4080 context->transform_feedback_paused = 0;
4084 static void context_pause_transform_feedback(struct wined3d_context *context, BOOL force)
4086 const struct wined3d_gl_info *gl_info = context->gl_info;
4088 if (!context->transform_feedback_active || context->transform_feedback_paused)
4089 return;
4091 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK2])
4093 GL_EXTCALL(glPauseTransformFeedback());
4094 checkGLcall("glPauseTransformFeedback");
4095 context->transform_feedback_paused = 1;
4096 return;
4099 WARN("Cannot pause transform feedback operations.\n");
4101 if (force)
4102 context_end_transform_feedback(context);
4105 static void context_setup_target(struct wined3d_context *context,
4106 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4108 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
4110 render_offscreen = wined3d_resource_is_offscreen(&texture->resource);
4111 if (context->current_rt.texture == texture
4112 && context->current_rt.sub_resource_idx == sub_resource_idx
4113 && render_offscreen == old_render_offscreen)
4114 return;
4116 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4117 * the alpha blend state changes with different render target formats. */
4118 if (!context->current_rt.texture)
4120 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
4122 else
4124 const struct wined3d_format *old = context->current_rt.texture->resource.format;
4125 const struct wined3d_format *new = texture->resource.format;
4127 if (old->id != new->id)
4129 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4130 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
4131 || !(texture->resource.format_flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
4132 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
4134 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
4135 if ((context->current_rt.texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE)
4136 != (texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE))
4137 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
4140 /* When switching away from an offscreen render target, and we're not
4141 * using FBOs, we have to read the drawable into the texture. This is
4142 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4143 * There are some things that need care though. PreLoad needs a GL context,
4144 * and FindContext is called before the context is activated. It also
4145 * has to be called with the old rendertarget active, otherwise a
4146 * wrong drawable is read. */
4147 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4148 && old_render_offscreen && (context->current_rt.texture != texture
4149 || context->current_rt.sub_resource_idx != sub_resource_idx))
4151 unsigned int prev_sub_resource_idx = context->current_rt.sub_resource_idx;
4152 struct wined3d_texture *prev_texture = context->current_rt.texture;
4154 /* Read the back buffer of the old drawable into the destination texture. */
4155 if (prev_texture->texture_srgb.name)
4156 wined3d_texture_load(prev_texture, context, TRUE);
4157 wined3d_texture_load(prev_texture, context, FALSE);
4158 wined3d_texture_invalidate_location(prev_texture, prev_sub_resource_idx, WINED3D_LOCATION_DRAWABLE);
4162 context->current_rt.texture = texture;
4163 context->current_rt.sub_resource_idx = sub_resource_idx;
4164 context_set_render_offscreen(context, render_offscreen);
4167 static void context_activate(struct wined3d_context *context,
4168 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4170 context_enter(context);
4171 context_update_window(context);
4172 context_setup_target(context, texture, sub_resource_idx);
4173 if (!context->valid)
4174 return;
4176 if (context != context_get_current())
4178 if (!context_set_current(context))
4179 ERR("Failed to activate the new context.\n");
4181 else if (context->needs_set)
4183 context_set_gl_context(context);
4187 struct wined3d_context *context_acquire(const struct wined3d_device *device,
4188 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4190 struct wined3d_context *current_context = context_get_current();
4191 struct wined3d_context *context;
4192 BOOL swapchain_texture;
4194 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device, texture, sub_resource_idx);
4196 wined3d_from_cs(device->cs);
4198 if (current_context && current_context->destroyed)
4199 current_context = NULL;
4201 swapchain_texture = texture && texture->swapchain;
4202 if (!texture)
4204 if (current_context
4205 && current_context->current_rt.texture
4206 && current_context->device == device)
4208 texture = current_context->current_rt.texture;
4209 sub_resource_idx = current_context->current_rt.sub_resource_idx;
4211 else
4213 struct wined3d_swapchain *swapchain = device->swapchains[0];
4215 if (swapchain->back_buffers)
4216 texture = swapchain->back_buffers[0];
4217 else
4218 texture = swapchain->front_buffer;
4219 sub_resource_idx = 0;
4223 if (current_context && current_context->current_rt.texture == texture)
4225 context = current_context;
4227 else if (swapchain_texture)
4229 TRACE("Rendering onscreen.\n");
4231 context = swapchain_get_context(texture->swapchain);
4233 else
4235 TRACE("Rendering offscreen.\n");
4237 /* Stay with the current context if possible. Otherwise use the
4238 * context for the primary swapchain. */
4239 if (current_context && current_context->device == device)
4240 context = current_context;
4241 else
4242 context = swapchain_get_context(device->swapchains[0]);
4245 context_activate(context, texture, sub_resource_idx);
4247 return context;
4250 struct wined3d_context *context_reacquire(const struct wined3d_device *device,
4251 struct wined3d_context *context)
4253 struct wined3d_context *acquired_context;
4255 wined3d_from_cs(device->cs);
4257 if (!context || context->tid != GetCurrentThreadId())
4258 return NULL;
4260 if (context->current_rt.texture)
4262 context_activate(context, context->current_rt.texture, context->current_rt.sub_resource_idx);
4263 return context;
4266 acquired_context = context_acquire(device, NULL, 0);
4267 if (acquired_context != context)
4268 ERR("Acquired context %p instead of %p.\n", acquired_context, context);
4269 return acquired_context;
4272 void dispatch_compute(struct wined3d_device *device, const struct wined3d_state *state,
4273 const struct wined3d_dispatch_parameters *parameters)
4275 const struct wined3d_gl_info *gl_info;
4276 struct wined3d_context *context;
4278 context = context_acquire(device, NULL, 0);
4279 if (!context->valid)
4281 context_release(context);
4282 WARN("Invalid context, skipping dispatch.\n");
4283 return;
4285 gl_info = context->gl_info;
4287 if (!gl_info->supported[ARB_COMPUTE_SHADER])
4289 context_release(context);
4290 FIXME("OpenGL implementation does not support compute shaders.\n");
4291 return;
4294 if (parameters->indirect)
4295 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
4297 context_apply_compute_state(context, device, state);
4299 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
4301 context_release(context);
4302 WARN("No compute shader bound, skipping dispatch.\n");
4303 return;
4306 if (parameters->indirect)
4308 const struct wined3d_indirect_dispatch_parameters *indirect = &parameters->u.indirect;
4309 struct wined3d_buffer *buffer = indirect->buffer;
4311 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, buffer->buffer_object));
4312 GL_EXTCALL(glDispatchComputeIndirect((GLintptr)indirect->offset));
4313 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0));
4315 else
4317 const struct wined3d_direct_dispatch_parameters *direct = &parameters->u.direct;
4318 GL_EXTCALL(glDispatchCompute(direct->group_count_x, direct->group_count_y, direct->group_count_z));
4320 checkGLcall("dispatch compute");
4322 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4323 checkGLcall("glMemoryBarrier");
4325 if (wined3d_settings.strict_draw_ordering)
4326 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
4328 context_release(context);
4331 /* Context activation is done by the caller. */
4332 static void draw_primitive_arrays(struct wined3d_context *context, const struct wined3d_state *state,
4333 const void *idx_data, unsigned int idx_size, int base_vertex_idx, unsigned int start_idx,
4334 unsigned int count, unsigned int start_instance, unsigned int instance_count)
4336 const struct wined3d_ffp_attrib_ops *ops = &context->d3d_info->ffp_attrib_ops;
4337 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4338 const struct wined3d_stream_info *si = &context->stream_info;
4339 unsigned int instanced_elements[ARRAY_SIZE(si->elements)];
4340 const struct wined3d_gl_info *gl_info = context->gl_info;
4341 unsigned int instanced_element_count = 0;
4342 GLenum mode = state->gl_primitive_type;
4343 const void *indices;
4344 unsigned int i, j;
4346 indices = (const char *)idx_data + idx_size * start_idx;
4348 if (!instance_count)
4350 if (!idx_size)
4352 gl_info->gl_ops.gl.p_glDrawArrays(mode, start_idx, count);
4353 checkGLcall("glDrawArrays");
4354 return;
4357 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4359 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4360 checkGLcall("glDrawElementsBaseVertex");
4361 return;
4364 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4365 checkGLcall("glDrawElements");
4366 return;
4369 if (start_instance && !(gl_info->supported[ARB_BASE_INSTANCE] && gl_info->supported[ARB_INSTANCED_ARRAYS]))
4370 FIXME("Start instance (%u) not supported.\n", start_instance);
4372 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
4374 if (!idx_size)
4376 if (gl_info->supported[ARB_BASE_INSTANCE])
4378 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode, start_idx, count, instance_count, start_instance));
4379 checkGLcall("glDrawArraysInstancedBaseInstance");
4380 return;
4383 GL_EXTCALL(glDrawArraysInstanced(mode, start_idx, count, instance_count));
4384 checkGLcall("glDrawArraysInstanced");
4385 return;
4388 if (gl_info->supported[ARB_BASE_INSTANCE])
4390 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode, count, idx_type,
4391 indices, instance_count, base_vertex_idx, start_instance));
4392 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4393 return;
4395 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4397 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode, count, idx_type,
4398 indices, instance_count, base_vertex_idx));
4399 checkGLcall("glDrawElementsInstancedBaseVertex");
4400 return;
4403 GL_EXTCALL(glDrawElementsInstanced(mode, count, idx_type, indices, instance_count));
4404 checkGLcall("glDrawElementsInstanced");
4405 return;
4408 /* Instancing emulation by mixing immediate mode and arrays. */
4410 /* This is a nasty thing. MSDN says no hardware supports this and
4411 * applications have to use software vertex processing. We don't support
4412 * this for now.
4414 * Shouldn't be too hard to support with OpenGL, in theory just call
4415 * glDrawArrays() instead of drawElements(). But the stream fequency value
4416 * has a different meaning in that situation. */
4417 if (!idx_size)
4419 FIXME("Non-indexed instanced drawing is not supported.\n");
4420 return;
4423 for (i = 0; i < ARRAY_SIZE(si->elements); ++i)
4425 if (!(si->use_map & (1u << i)))
4426 continue;
4428 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
4429 instanced_elements[instanced_element_count++] = i;
4432 for (i = 0; i < instance_count; ++i)
4434 /* Specify the instanced attributes using immediate mode calls. */
4435 for (j = 0; j < instanced_element_count; ++j)
4437 const struct wined3d_stream_info_element *element;
4438 unsigned int element_idx;
4439 const BYTE *ptr;
4441 element_idx = instanced_elements[j];
4442 element = &si->elements[element_idx];
4443 ptr = element->data.addr + element->stride * i;
4444 if (element->data.buffer_object)
4445 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(state->streams[element->stream_idx].buffer, context);
4446 ops->generic[element->format->emit_idx](element_idx, ptr);
4449 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4451 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4452 checkGLcall("glDrawElementsBaseVertex");
4454 else
4456 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4457 checkGLcall("glDrawElements");
4462 static unsigned int get_stride_idx(const void *idx_data, unsigned int idx_size,
4463 unsigned int base_vertex_idx, unsigned int start_idx, unsigned int vertex_idx)
4465 if (!idx_data)
4466 return start_idx + vertex_idx;
4467 if (idx_size == 2)
4468 return ((const WORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4469 return ((const DWORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4472 /* Context activation is done by the caller. */
4473 static void draw_primitive_immediate_mode(struct wined3d_context *context, const struct wined3d_state *state,
4474 const struct wined3d_stream_info *si, const void *idx_data, unsigned int idx_size,
4475 int base_vertex_idx, unsigned int start_idx, unsigned int vertex_count, unsigned int instance_count)
4477 const BYTE *position = NULL, *normal = NULL, *diffuse = NULL, *specular = NULL;
4478 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
4479 unsigned int coord_idx, stride_idx, texture_idx, vertex_idx;
4480 const struct wined3d_gl_info *gl_info = context->gl_info;
4481 const struct wined3d_stream_info_element *element;
4482 const BYTE *tex_coords[WINED3DDP_MAXTEXCOORD];
4483 unsigned int texture_unit, texture_stages;
4484 const struct wined3d_ffp_attrib_ops *ops;
4485 unsigned int untracked_material_count;
4486 unsigned int tex_mask = 0;
4487 BOOL specular_fog = FALSE;
4488 BOOL ps = use_ps(state);
4489 const void *ptr;
4491 static unsigned int once;
4493 if (!once++)
4494 FIXME_(d3d_perf)("Drawing using immediate mode.\n");
4495 else
4496 WARN_(d3d_perf)("Drawing using immediate mode.\n");
4498 if (!idx_size && idx_data)
4499 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4501 if (instance_count)
4502 FIXME("Instancing not implemented.\n");
4504 /* Immediate mode drawing can't make use of indices in a VBO - get the
4505 * data from the index buffer. */
4506 if (idx_size)
4507 idx_data = wined3d_buffer_load_sysmem(state->index_buffer, context) + state->index_offset;
4509 ops = &d3d_info->ffp_attrib_ops;
4511 gl_info->gl_ops.gl.p_glBegin(state->gl_primitive_type);
4513 if (use_vs(state) || d3d_info->ffp_generic_attributes)
4515 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4517 unsigned int use_map = si->use_map;
4518 unsigned int element_idx;
4520 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4521 for (element_idx = MAX_ATTRIBS - 1; use_map; use_map &= ~(1u << element_idx), --element_idx)
4523 if (!(use_map & 1u << element_idx))
4524 continue;
4526 ptr = si->elements[element_idx].data.addr + si->elements[element_idx].stride * stride_idx;
4527 ops->generic[si->elements[element_idx].format->emit_idx](element_idx, ptr);
4531 gl_info->gl_ops.gl.p_glEnd();
4532 return;
4535 if (si->use_map & (1u << WINED3D_FFP_POSITION))
4536 position = si->elements[WINED3D_FFP_POSITION].data.addr;
4538 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
4539 normal = si->elements[WINED3D_FFP_NORMAL].data.addr;
4540 else
4541 gl_info->gl_ops.gl.p_glNormal3f(0.0f, 0.0f, 0.0f);
4543 untracked_material_count = context->num_untracked_materials;
4544 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
4546 element = &si->elements[WINED3D_FFP_DIFFUSE];
4547 diffuse = element->data.addr;
4549 if (untracked_material_count && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
4550 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element->format->id));
4552 else
4554 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4557 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
4559 element = &si->elements[WINED3D_FFP_SPECULAR];
4560 specular = element->data.addr;
4562 /* Special case where the fog density is stored in the specular alpha channel. */
4563 if (state->render_states[WINED3D_RS_FOGENABLE]
4564 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
4565 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
4566 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
4568 if (gl_info->supported[EXT_FOG_COORD])
4570 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
4571 specular_fog = TRUE;
4572 else
4573 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element->format->id));
4575 else
4577 static unsigned int once;
4579 if (!once++)
4580 FIXME("Implement fog for transformed vertices in software.\n");
4584 else if (gl_info->supported[EXT_SECONDARY_COLOR])
4586 GL_EXTCALL(glSecondaryColor3fEXT)(0.0f, 0.0f, 0.0f);
4589 texture_stages = d3d_info->limits.ffp_blend_stages;
4590 for (texture_idx = 0; texture_idx < texture_stages; ++texture_idx)
4592 if (!gl_info->supported[ARB_MULTITEXTURE] && texture_idx > 0)
4594 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4595 continue;
4598 if (!ps && !state->textures[texture_idx])
4599 continue;
4601 texture_unit = context->tex_unit_map[texture_idx];
4602 if (texture_unit == WINED3D_UNMAPPED_STAGE)
4603 continue;
4605 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4606 if (coord_idx > 7)
4608 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx, texture_idx);
4609 continue;
4612 if (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))
4614 tex_coords[coord_idx] = si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].data.addr;
4615 tex_mask |= (1u << texture_idx);
4617 else
4619 TRACE("Setting default coordinates for texture %u.\n", texture_idx);
4620 if (gl_info->supported[ARB_MULTITEXTURE])
4621 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_unit, 0.0f, 0.0f, 0.0f, 1.0f));
4622 else
4623 gl_info->gl_ops.gl.p_glTexCoord4f(0.0f, 0.0f, 0.0f, 1.0f);
4627 /* Blending data and point sizes are not supported by this function. They
4628 * are not supported by the fixed function pipeline at all. A FIXME for
4629 * them is printed after decoding the vertex declaration. */
4630 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4632 unsigned int tmp_tex_mask;
4634 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4636 if (normal)
4638 ptr = normal + stride_idx * si->elements[WINED3D_FFP_NORMAL].stride;
4639 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptr);
4642 if (diffuse)
4644 ptr = diffuse + stride_idx * si->elements[WINED3D_FFP_DIFFUSE].stride;
4645 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptr);
4647 if (untracked_material_count)
4649 struct wined3d_color color;
4650 unsigned int i;
4652 wined3d_color_from_d3dcolor(&color, *(const DWORD *)ptr);
4653 for (i = 0; i < untracked_material_count; ++i)
4655 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], &color.r);
4660 if (specular)
4662 ptr = specular + stride_idx * si->elements[WINED3D_FFP_SPECULAR].stride;
4663 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptr);
4665 if (specular_fog)
4666 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD *)ptr >> 24)));
4669 tmp_tex_mask = tex_mask;
4670 for (texture_idx = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture_idx)
4672 if (!(tmp_tex_mask & 1))
4673 continue;
4675 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4676 ptr = tex_coords[coord_idx] + (stride_idx * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
4677 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
4678 GL_TEXTURE0_ARB + context->tex_unit_map[texture_idx], ptr);
4681 if (position)
4683 ptr = position + stride_idx * si->elements[WINED3D_FFP_POSITION].stride;
4684 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptr);
4688 gl_info->gl_ops.gl.p_glEnd();
4689 checkGLcall("draw immediate mode");
4692 static void draw_indirect(struct wined3d_context *context, const struct wined3d_state *state,
4693 const struct wined3d_indirect_draw_parameters *parameters, unsigned int idx_size)
4695 const struct wined3d_gl_info *gl_info = context->gl_info;
4696 struct wined3d_buffer *buffer = parameters->buffer;
4697 const void *offset;
4699 if (!gl_info->supported[ARB_DRAW_INDIRECT])
4701 FIXME("OpenGL implementation does not support indirect draws.\n");
4702 return;
4705 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer->buffer_object));
4707 offset = (void *)(GLintptr)parameters->offset;
4708 if (idx_size)
4710 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4711 if (state->index_offset)
4712 FIXME("Ignoring index offset %u.\n", state->index_offset);
4713 GL_EXTCALL(glDrawElementsIndirect(state->gl_primitive_type, idx_type, offset));
4715 else
4717 GL_EXTCALL(glDrawArraysIndirect(state->gl_primitive_type, offset));
4720 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0));
4722 checkGLcall("draw indirect");
4725 static void remove_vbos(struct wined3d_context *context,
4726 const struct wined3d_state *state, struct wined3d_stream_info *s)
4728 unsigned int i;
4730 for (i = 0; i < ARRAY_SIZE(s->elements); ++i)
4732 struct wined3d_stream_info_element *e;
4734 if (!(s->use_map & (1u << i)))
4735 continue;
4737 e = &s->elements[i];
4738 if (e->data.buffer_object)
4740 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
4741 e->data.buffer_object = 0;
4742 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(vb, context);
4747 static GLenum gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
4749 GLenum gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4750 switch (gl_primitive_type)
4752 case GL_POINTS:
4753 return GL_POINTS;
4755 case GL_LINE_STRIP:
4756 case GL_LINE_STRIP_ADJACENCY:
4757 case GL_LINES_ADJACENCY:
4758 case GL_LINES:
4759 return GL_LINES;
4761 case GL_TRIANGLE_FAN:
4762 case GL_TRIANGLE_STRIP:
4763 case GL_TRIANGLE_STRIP_ADJACENCY:
4764 case GL_TRIANGLES_ADJACENCY:
4765 case GL_TRIANGLES:
4766 return GL_TRIANGLES;
4768 default:
4769 return gl_primitive_type;
4773 /* Routine common to the draw primitive and draw indexed primitive routines */
4774 void draw_primitive(struct wined3d_device *device, const struct wined3d_state *state,
4775 const struct wined3d_draw_parameters *parameters)
4777 BOOL emulation = FALSE, rasterizer_discard = FALSE;
4778 const struct wined3d_fb_state *fb = state->fb;
4779 const struct wined3d_stream_info *stream_info;
4780 struct wined3d_rendertarget_view *dsv, *rtv;
4781 struct wined3d_stream_info si_emulated;
4782 struct wined3d_fence *ib_fence = NULL;
4783 const struct wined3d_gl_info *gl_info;
4784 struct wined3d_context *context;
4785 unsigned int i, idx_size = 0;
4786 const void *idx_data = NULL;
4788 if (!parameters->indirect && !parameters->u.direct.index_count)
4789 return;
4791 if (!(rtv = fb->render_targets[0]))
4792 rtv = fb->depth_stencil;
4793 if (rtv)
4794 context = context_acquire(device, wined3d_texture_from_resource(rtv->resource), rtv->sub_resource_idx);
4795 else
4796 context = context_acquire(device, NULL, 0);
4797 if (!context->valid)
4799 context_release(context);
4800 WARN("Invalid context, skipping draw.\n");
4801 return;
4803 gl_info = context->gl_info;
4805 if (!use_transform_feedback(state))
4806 context_pause_transform_feedback(context, TRUE);
4808 for (i = 0; i < gl_info->limits.buffers; ++i)
4810 if (!(rtv = fb->render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
4811 continue;
4813 if (state->render_states[WINED3D_RS_COLORWRITEENABLE])
4815 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
4816 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
4818 else
4820 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
4824 if ((dsv = fb->depth_stencil))
4826 /* Note that this depends on the context_acquire() call above to set
4827 * context->render_offscreen properly. We don't currently take the
4828 * Z-compare function into account, but we could skip loading the
4829 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
4830 * that we never copy the stencil data.*/
4831 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4833 if (state->render_states[WINED3D_RS_ZWRITEENABLE] || state->render_states[WINED3D_RS_ZENABLE])
4834 wined3d_rendertarget_view_load_location(dsv, context, location);
4835 else
4836 wined3d_rendertarget_view_prepare_location(dsv, context, location);
4839 if (parameters->indirect)
4840 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
4842 if (!context_apply_draw_state(context, device, state))
4844 context_release(context);
4845 WARN("Unable to apply draw state, skipping draw.\n");
4846 return;
4849 if (dsv && state->render_states[WINED3D_RS_ZWRITEENABLE])
4851 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4853 wined3d_rendertarget_view_validate_location(dsv, location);
4854 wined3d_rendertarget_view_invalidate_location(dsv, ~location);
4857 stream_info = &context->stream_info;
4859 if (parameters->indexed)
4861 struct wined3d_buffer *index_buffer = state->index_buffer;
4862 if (!index_buffer->buffer_object || !stream_info->all_vbo)
4864 idx_data = index_buffer->resource.heap_memory;
4866 else
4868 ib_fence = index_buffer->fence;
4869 idx_data = NULL;
4871 idx_data = (const BYTE *)idx_data + state->index_offset;
4873 if (state->index_format == WINED3DFMT_R16_UINT)
4874 idx_size = 2;
4875 else
4876 idx_size = 4;
4879 if (!use_vs(state))
4881 if (!stream_info->position_transformed && context->num_untracked_materials
4882 && state->render_states[WINED3D_RS_LIGHTING])
4884 static BOOL warned;
4886 if (!warned++)
4887 FIXME("Using software emulation because not all material properties could be tracked.\n");
4888 else
4889 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
4890 emulation = TRUE;
4892 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
4894 static BOOL warned;
4896 /* Either write a pipeline replacement shader or convert the
4897 * specular alpha from unsigned byte to a float in the vertex
4898 * buffer. */
4899 if (!warned++)
4900 FIXME("Using software emulation because manual fog coordinates are provided.\n");
4901 else
4902 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
4903 emulation = TRUE;
4906 if (emulation)
4908 si_emulated = context->stream_info;
4909 remove_vbos(context, state, &si_emulated);
4910 stream_info = &si_emulated;
4914 if (use_transform_feedback(state))
4916 const struct wined3d_shader *shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
4918 if (is_rasterization_disabled(shader))
4920 glEnable(GL_RASTERIZER_DISCARD);
4921 checkGLcall("enable rasterizer discard");
4922 rasterizer_discard = TRUE;
4925 if (context->transform_feedback_paused)
4927 GL_EXTCALL(glResumeTransformFeedback());
4928 checkGLcall("glResumeTransformFeedback");
4929 context->transform_feedback_paused = 0;
4931 else if (!context->transform_feedback_active)
4933 GLenum mode = gl_tfb_primitive_type_from_d3d(shader->u.gs.output_type);
4934 GL_EXTCALL(glBeginTransformFeedback(mode));
4935 checkGLcall("glBeginTransformFeedback");
4936 context->transform_feedback_active = 1;
4940 if (state->gl_primitive_type == GL_PATCHES)
4942 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES, state->gl_patch_vertices));
4943 checkGLcall("glPatchParameteri");
4946 if (parameters->indirect)
4948 if (!context->use_immediate_mode_draw && !emulation)
4949 draw_indirect(context, state, &parameters->u.indirect, idx_size);
4950 else
4951 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
4953 else
4955 unsigned int instance_count = parameters->u.direct.instance_count;
4956 if (context->instance_count)
4957 instance_count = context->instance_count;
4959 if (context->use_immediate_mode_draw || emulation)
4960 draw_primitive_immediate_mode(context, state, stream_info, idx_data,
4961 idx_size, parameters->u.direct.base_vertex_idx,
4962 parameters->u.direct.start_idx, parameters->u.direct.index_count, instance_count);
4963 else
4964 draw_primitive_arrays(context, state, idx_data, idx_size, parameters->u.direct.base_vertex_idx,
4965 parameters->u.direct.start_idx, parameters->u.direct.index_count,
4966 parameters->u.direct.start_instance, instance_count);
4969 if (context->uses_uavs)
4971 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4972 checkGLcall("glMemoryBarrier");
4975 context_pause_transform_feedback(context, FALSE);
4977 if (rasterizer_discard)
4979 glDisable(GL_RASTERIZER_DISCARD);
4980 checkGLcall("disable rasterizer discard");
4983 if (ib_fence)
4984 wined3d_fence_issue(ib_fence, device);
4985 for (i = 0; i < context->buffer_fence_count; ++i)
4986 wined3d_fence_issue(context->buffer_fences[i], device);
4988 if (wined3d_settings.strict_draw_ordering)
4989 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
4991 context_release(context);