wined3d: Store the EXT_fbo "renderbuffers" list in the texture instead of the surface.
[wine.git] / dlls / wined3d / context.c
blob60bf52ba1296f6b102a4c5ac104112617a727caa
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 struct wined3d_rendertarget_info *render_targets, struct wined3d_surface *depth_stencil,
747 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 struct wined3d_rendertarget_info ds = {{0}};
771 if (depth_stencil)
773 ds.resource = &depth_stencil->container->resource;
774 ds.sub_resource_idx = surface_get_sub_resource_idx(depth_stencil);
775 ds.layer_count = 1;
777 context->current_fbo = context_find_fbo_entry(context, target,
778 render_targets, &ds, color_location, ds_location);
779 context_apply_fbo_entry(context, target, context->current_fbo);
783 /* Context activation is done by the caller. */
784 void context_apply_fbo_state_blit(struct wined3d_context *context, GLenum target,
785 struct wined3d_surface *render_target, struct wined3d_surface *depth_stencil, DWORD location)
787 memset(context->blit_targets, 0, sizeof(context->blit_targets));
788 if (render_target)
790 context->blit_targets[0].resource = &render_target->container->resource;
791 context->blit_targets[0].sub_resource_idx = surface_get_sub_resource_idx(render_target);
792 context->blit_targets[0].layer_count = 1;
794 context_apply_fbo_state(context, target, context->blit_targets, depth_stencil, location, location);
797 /* Context activation is done by the caller. */
798 void context_alloc_occlusion_query(struct wined3d_context *context, struct wined3d_occlusion_query *query)
800 const struct wined3d_gl_info *gl_info = context->gl_info;
802 if (context->free_occlusion_query_count)
804 query->id = context->free_occlusion_queries[--context->free_occlusion_query_count];
806 else
808 if (gl_info->supported[ARB_OCCLUSION_QUERY])
810 GL_EXTCALL(glGenQueries(1, &query->id));
811 checkGLcall("glGenQueries");
813 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context);
815 else
817 WARN("Occlusion queries not supported, not allocating query id.\n");
818 query->id = 0;
822 query->context = context;
823 list_add_head(&context->occlusion_queries, &query->entry);
826 void context_free_occlusion_query(struct wined3d_occlusion_query *query)
828 struct wined3d_context *context = query->context;
830 list_remove(&query->entry);
831 query->context = NULL;
833 if (!wined3d_array_reserve((void **)&context->free_occlusion_queries,
834 &context->free_occlusion_query_size, context->free_occlusion_query_count + 1,
835 sizeof(*context->free_occlusion_queries)))
837 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
838 return;
841 context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
844 /* Context activation is done by the caller. */
845 void context_alloc_fence(struct wined3d_context *context, struct wined3d_fence *fence)
847 const struct wined3d_gl_info *gl_info = context->gl_info;
849 if (context->free_fence_count)
851 fence->object = context->free_fences[--context->free_fence_count];
853 else
855 if (gl_info->supported[ARB_SYNC])
857 /* Using ARB_sync, not much to do here. */
858 fence->object.sync = NULL;
859 TRACE("Allocated sync object in context %p.\n", context);
861 else if (gl_info->supported[APPLE_FENCE])
863 GL_EXTCALL(glGenFencesAPPLE(1, &fence->object.id));
864 checkGLcall("glGenFencesAPPLE");
866 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context);
868 else if(gl_info->supported[NV_FENCE])
870 GL_EXTCALL(glGenFencesNV(1, &fence->object.id));
871 checkGLcall("glGenFencesNV");
873 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context);
875 else
877 WARN("Fences not supported, not allocating fence.\n");
878 fence->object.id = 0;
882 fence->context = context;
883 list_add_head(&context->fences, &fence->entry);
886 void context_free_fence(struct wined3d_fence *fence)
888 struct wined3d_context *context = fence->context;
890 list_remove(&fence->entry);
891 fence->context = NULL;
893 if (!wined3d_array_reserve((void **)&context->free_fences,
894 &context->free_fence_size, context->free_fence_count + 1,
895 sizeof(*context->free_fences)))
897 ERR("Failed to grow free list, leaking fence %u in context %p.\n", fence->object.id, context);
898 return;
901 context->free_fences[context->free_fence_count++] = fence->object;
904 /* Context activation is done by the caller. */
905 void context_alloc_timestamp_query(struct wined3d_context *context, struct wined3d_timestamp_query *query)
907 const struct wined3d_gl_info *gl_info = context->gl_info;
909 if (context->free_timestamp_query_count)
911 query->id = context->free_timestamp_queries[--context->free_timestamp_query_count];
913 else
915 GL_EXTCALL(glGenQueries(1, &query->id));
916 checkGLcall("glGenQueries");
918 TRACE("Allocated timestamp query %u in context %p.\n", query->id, context);
921 query->context = context;
922 list_add_head(&context->timestamp_queries, &query->entry);
925 void context_free_timestamp_query(struct wined3d_timestamp_query *query)
927 struct wined3d_context *context = query->context;
929 list_remove(&query->entry);
930 query->context = NULL;
932 if (!wined3d_array_reserve((void **)&context->free_timestamp_queries,
933 &context->free_timestamp_query_size, context->free_timestamp_query_count + 1,
934 sizeof(*context->free_timestamp_queries)))
936 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
937 return;
940 context->free_timestamp_queries[context->free_timestamp_query_count++] = query->id;
943 void context_alloc_so_statistics_query(struct wined3d_context *context,
944 struct wined3d_so_statistics_query *query)
946 const struct wined3d_gl_info *gl_info = context->gl_info;
948 if (context->free_so_statistics_query_count)
950 query->u = context->free_so_statistics_queries[--context->free_so_statistics_query_count];
952 else
954 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
955 checkGLcall("glGenQueries");
957 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
958 query->u.id[0], query->u.id[1], context);
961 query->context = context;
962 list_add_head(&context->so_statistics_queries, &query->entry);
965 void context_free_so_statistics_query(struct wined3d_so_statistics_query *query)
967 struct wined3d_context *context = query->context;
969 list_remove(&query->entry);
970 query->context = NULL;
972 if (!wined3d_array_reserve((void **)&context->free_so_statistics_queries,
973 &context->free_so_statistics_query_size, context->free_so_statistics_query_count + 1,
974 sizeof(*context->free_so_statistics_queries)))
976 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
977 query->u.id[0], query->u.id[1], context);
978 return;
981 context->free_so_statistics_queries[context->free_so_statistics_query_count++] = query->u;
984 void context_alloc_pipeline_statistics_query(struct wined3d_context *context,
985 struct wined3d_pipeline_statistics_query *query)
987 const struct wined3d_gl_info *gl_info = context->gl_info;
989 if (context->free_pipeline_statistics_query_count)
991 query->u = context->free_pipeline_statistics_queries[--context->free_pipeline_statistics_query_count];
993 else
995 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
996 checkGLcall("glGenQueries");
999 query->context = context;
1000 list_add_head(&context->pipeline_statistics_queries, &query->entry);
1003 void context_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query *query)
1005 struct wined3d_context *context = query->context;
1007 list_remove(&query->entry);
1008 query->context = NULL;
1010 if (!wined3d_array_reserve((void **)&context->free_pipeline_statistics_queries,
1011 &context->free_pipeline_statistics_query_size, context->free_pipeline_statistics_query_count + 1,
1012 sizeof(*context->free_pipeline_statistics_queries)))
1014 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context);
1015 return;
1018 context->free_pipeline_statistics_queries[context->free_pipeline_statistics_query_count++] = query->u;
1021 typedef void (context_fbo_entry_func_t)(struct wined3d_context *context, struct fbo_entry *entry);
1023 static void context_enum_fbo_entries(const struct wined3d_device *device,
1024 GLuint name, BOOL rb_namespace, context_fbo_entry_func_t *callback)
1026 unsigned int i, j;
1028 for (i = 0; i < device->context_count; ++i)
1030 struct wined3d_context *context = device->contexts[i];
1031 const struct wined3d_gl_info *gl_info = context->gl_info;
1032 struct fbo_entry *entry, *entry2;
1034 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
1036 for (j = 0; j < gl_info->limits.buffers + 1; ++j)
1038 if (entry->key.objects[j].object == name
1039 && !(entry->key.rb_namespace & (1 << j)) == !rb_namespace)
1041 callback(context, entry);
1042 break;
1049 static void context_queue_fbo_entry_destruction(struct wined3d_context *context, struct fbo_entry *entry)
1051 list_remove(&entry->entry);
1052 list_add_head(&context->fbo_destroy_list, &entry->entry);
1055 void context_resource_released(const struct wined3d_device *device,
1056 struct wined3d_resource *resource, enum wined3d_resource_type type)
1058 struct wined3d_texture *texture;
1059 UINT i;
1061 if (!device->d3d_initialized)
1062 return;
1064 switch (type)
1066 case WINED3D_RTYPE_TEXTURE_2D:
1067 case WINED3D_RTYPE_TEXTURE_3D:
1068 texture = texture_from_resource(resource);
1070 for (i = 0; i < device->context_count; ++i)
1072 struct wined3d_context *context = device->contexts[i];
1073 if (context->current_rt.texture == texture)
1075 context->current_rt.texture = NULL;
1076 context->current_rt.sub_resource_idx = 0;
1079 break;
1081 default:
1082 break;
1086 void context_gl_resource_released(struct wined3d_device *device,
1087 GLuint name, BOOL rb_namespace)
1089 context_enum_fbo_entries(device, name, rb_namespace, context_queue_fbo_entry_destruction);
1092 void context_surface_update(struct wined3d_context *context, const struct wined3d_surface *surface)
1094 const struct wined3d_gl_info *gl_info = context->gl_info;
1095 struct fbo_entry *entry = context->current_fbo;
1096 unsigned int i;
1098 if (!entry || context->rebind_fbo) return;
1100 for (i = 0; i < gl_info->limits.buffers + 1; ++i)
1102 if (surface->container->texture_rgb.name == entry->key.objects[i].object
1103 || surface->container->texture_srgb.name == entry->key.objects[i].object)
1105 TRACE("Updated surface %p is bound as attachment %u to the current FBO.\n", surface, i);
1106 context->rebind_fbo = TRUE;
1107 return;
1112 static BOOL context_restore_pixel_format(struct wined3d_context *ctx)
1114 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1115 BOOL ret = FALSE;
1117 if (ctx->restore_pf && IsWindow(ctx->restore_pf_win))
1119 if (ctx->gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1121 HDC dc = GetDCEx(ctx->restore_pf_win, 0, DCX_USESTYLE | DCX_CACHE);
1122 if (dc)
1124 if (!(ret = GL_EXTCALL(wglSetPixelFormatWINE(dc, ctx->restore_pf))))
1126 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
1127 ctx->restore_pf, ctx->restore_pf_win);
1129 ReleaseDC(ctx->restore_pf_win, dc);
1132 else
1134 ERR("can't restore pixel format %d on window %p\n", ctx->restore_pf, ctx->restore_pf_win);
1138 ctx->restore_pf = 0;
1139 ctx->restore_pf_win = NULL;
1140 return ret;
1143 static BOOL context_set_pixel_format(struct wined3d_context *context)
1145 const struct wined3d_gl_info *gl_info = context->gl_info;
1146 BOOL private = context->hdc_is_private;
1147 int format = context->pixel_format;
1148 HDC dc = context->hdc;
1149 int current;
1151 if (private && context->hdc_has_format)
1152 return TRUE;
1154 if (!private && WindowFromDC(dc) != context->win_handle)
1155 return FALSE;
1157 current = gl_info->gl_ops.wgl.p_wglGetPixelFormat(dc);
1158 if (current == format) goto success;
1160 if (!current)
1162 if (!SetPixelFormat(dc, format, NULL))
1164 /* This may also happen if the dc belongs to a destroyed window. */
1165 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1166 format, dc, GetLastError());
1167 return FALSE;
1170 context->restore_pf = 0;
1171 context->restore_pf_win = private ? NULL : WindowFromDC(dc);
1172 goto success;
1175 /* By default WGL doesn't allow pixel format adjustments but we need it
1176 * here. For this reason there's a Wine specific wglSetPixelFormat()
1177 * which allows us to set the pixel format multiple times. Only use it
1178 * when really needed. */
1179 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1181 HWND win;
1183 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
1185 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1186 format, dc);
1187 return FALSE;
1190 win = private ? NULL : WindowFromDC(dc);
1191 if (win != context->restore_pf_win)
1193 context_restore_pixel_format(context);
1195 context->restore_pf = private ? 0 : current;
1196 context->restore_pf_win = win;
1199 goto success;
1202 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1203 * continue using the old format. There's a big chance that the old
1204 * format works although with a performance hit and perhaps rendering
1205 * errors. */
1206 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1207 format, dc, current);
1208 return TRUE;
1210 success:
1211 if (private)
1212 context->hdc_has_format = TRUE;
1213 return TRUE;
1216 static BOOL context_set_gl_context(struct wined3d_context *ctx)
1218 struct wined3d_swapchain *swapchain = ctx->swapchain;
1219 BOOL backup = FALSE;
1221 if (!context_set_pixel_format(ctx))
1223 WARN("Failed to set pixel format %d on device context %p.\n",
1224 ctx->pixel_format, ctx->hdc);
1225 backup = TRUE;
1228 if (backup || !wglMakeCurrent(ctx->hdc, ctx->glCtx))
1230 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1231 ctx->glCtx, ctx->hdc, GetLastError());
1232 ctx->valid = 0;
1233 WARN("Trying fallback to the backup window.\n");
1235 /* FIXME: If the context is destroyed it's no longer associated with
1236 * a swapchain, so we can't use the swapchain to get a backup dc. To
1237 * make this work windowless contexts would need to be handled by the
1238 * device. */
1239 if (ctx->destroyed || !swapchain)
1241 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx);
1242 context_set_current(NULL);
1243 return FALSE;
1246 if (!(ctx->hdc = swapchain_get_backup_dc(swapchain)))
1248 context_set_current(NULL);
1249 return FALSE;
1252 ctx->hdc_is_private = TRUE;
1253 ctx->hdc_has_format = FALSE;
1255 if (!context_set_pixel_format(ctx))
1257 ERR("Failed to set pixel format %d on device context %p.\n",
1258 ctx->pixel_format, ctx->hdc);
1259 context_set_current(NULL);
1260 return FALSE;
1263 if (!wglMakeCurrent(ctx->hdc, ctx->glCtx))
1265 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1266 ctx->hdc, GetLastError());
1267 context_set_current(NULL);
1268 return FALSE;
1271 ctx->valid = 1;
1273 ctx->needs_set = 0;
1274 return TRUE;
1277 static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx)
1279 if (!wglMakeCurrent(dc, gl_ctx))
1281 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1282 gl_ctx, dc, GetLastError());
1283 context_set_current(NULL);
1287 static void context_update_window(struct wined3d_context *context)
1289 if (!context->swapchain)
1290 return;
1292 if (context->win_handle == context->swapchain->win_handle)
1293 return;
1295 TRACE("Updating context %p window from %p to %p.\n",
1296 context, context->win_handle, context->swapchain->win_handle);
1298 if (context->hdc)
1299 wined3d_release_dc(context->win_handle, context->hdc);
1301 context->win_handle = context->swapchain->win_handle;
1302 context->hdc_is_private = FALSE;
1303 context->hdc_has_format = FALSE;
1304 context->needs_set = 1;
1305 context->valid = 1;
1307 if (!(context->hdc = GetDCEx(context->win_handle, 0, DCX_USESTYLE | DCX_CACHE)))
1309 ERR("Failed to get a device context for window %p.\n", context->win_handle);
1310 context->valid = 0;
1314 static void context_destroy_gl_resources(struct wined3d_context *context)
1316 struct wined3d_pipeline_statistics_query *pipeline_statistics_query;
1317 const struct wined3d_gl_info *gl_info = context->gl_info;
1318 struct wined3d_so_statistics_query *so_statistics_query;
1319 struct wined3d_timestamp_query *timestamp_query;
1320 struct wined3d_occlusion_query *occlusion_query;
1321 struct fbo_entry *entry, *entry2;
1322 struct wined3d_fence *fence;
1323 HGLRC restore_ctx;
1324 HDC restore_dc;
1325 unsigned int i;
1327 restore_ctx = wglGetCurrentContext();
1328 restore_dc = wglGetCurrentDC();
1330 if (restore_ctx == context->glCtx)
1331 restore_ctx = NULL;
1332 else if (context->valid)
1333 context_set_gl_context(context);
1335 LIST_FOR_EACH_ENTRY(so_statistics_query, &context->so_statistics_queries,
1336 struct wined3d_so_statistics_query, entry)
1338 if (context->valid)
1339 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query->u.id), so_statistics_query->u.id));
1340 so_statistics_query->context = NULL;
1343 LIST_FOR_EACH_ENTRY(pipeline_statistics_query, &context->pipeline_statistics_queries,
1344 struct wined3d_pipeline_statistics_query, entry)
1346 if (context->valid)
1347 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query->u.id), pipeline_statistics_query->u.id));
1348 pipeline_statistics_query->context = NULL;
1351 LIST_FOR_EACH_ENTRY(timestamp_query, &context->timestamp_queries, struct wined3d_timestamp_query, entry)
1353 if (context->valid)
1354 GL_EXTCALL(glDeleteQueries(1, &timestamp_query->id));
1355 timestamp_query->context = NULL;
1358 LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
1360 if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
1361 GL_EXTCALL(glDeleteQueries(1, &occlusion_query->id));
1362 occlusion_query->context = NULL;
1365 LIST_FOR_EACH_ENTRY(fence, &context->fences, struct wined3d_fence, entry)
1367 if (context->valid)
1369 if (gl_info->supported[ARB_SYNC])
1371 if (fence->object.sync)
1372 GL_EXTCALL(glDeleteSync(fence->object.sync));
1374 else if (gl_info->supported[APPLE_FENCE])
1376 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence->object.id));
1378 else if (gl_info->supported[NV_FENCE])
1380 GL_EXTCALL(glDeleteFencesNV(1, &fence->object.id));
1383 fence->context = NULL;
1386 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
1388 if (!context->valid) entry->id = 0;
1389 context_destroy_fbo_entry(context, entry);
1392 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
1394 if (!context->valid) entry->id = 0;
1395 context_destroy_fbo_entry(context, entry);
1398 if (context->valid)
1400 if (context->dummy_arbfp_prog)
1402 GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
1405 if (gl_info->supported[WINED3D_GL_PRIMITIVE_QUERY])
1407 for (i = 0; i < context->free_so_statistics_query_count; ++i)
1409 union wined3d_gl_so_statistics_query *q = &context->free_so_statistics_queries[i];
1410 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1414 if (gl_info->supported[ARB_PIPELINE_STATISTICS_QUERY])
1416 for (i = 0; i < context->free_pipeline_statistics_query_count; ++i)
1418 union wined3d_gl_pipeline_statistics_query *q = &context->free_pipeline_statistics_queries[i];
1419 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1423 if (gl_info->supported[ARB_TIMER_QUERY])
1424 GL_EXTCALL(glDeleteQueries(context->free_timestamp_query_count, context->free_timestamp_queries));
1426 if (gl_info->supported[ARB_OCCLUSION_QUERY])
1427 GL_EXTCALL(glDeleteQueries(context->free_occlusion_query_count, context->free_occlusion_queries));
1429 if (gl_info->supported[ARB_SYNC])
1431 for (i = 0; i < context->free_fence_count; ++i)
1433 GL_EXTCALL(glDeleteSync(context->free_fences[i].sync));
1436 else if (gl_info->supported[APPLE_FENCE])
1438 for (i = 0; i < context->free_fence_count; ++i)
1440 GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_fences[i].id));
1443 else if (gl_info->supported[NV_FENCE])
1445 for (i = 0; i < context->free_fence_count; ++i)
1447 GL_EXTCALL(glDeleteFencesNV(1, &context->free_fences[i].id));
1451 checkGLcall("context cleanup");
1454 heap_free(context->free_so_statistics_queries);
1455 heap_free(context->free_pipeline_statistics_queries);
1456 heap_free(context->free_timestamp_queries);
1457 heap_free(context->free_occlusion_queries);
1458 heap_free(context->free_fences);
1460 context_restore_pixel_format(context);
1461 if (restore_ctx)
1463 context_restore_gl_context(gl_info, restore_dc, restore_ctx);
1465 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1467 ERR("Failed to disable GL context.\n");
1470 wined3d_release_dc(context->win_handle, context->hdc);
1472 if (!wglDeleteContext(context->glCtx))
1474 DWORD err = GetLastError();
1475 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
1479 DWORD context_get_tls_idx(void)
1481 return wined3d_context_tls_idx;
1484 void context_set_tls_idx(DWORD idx)
1486 wined3d_context_tls_idx = idx;
1489 struct wined3d_context *context_get_current(void)
1491 return TlsGetValue(wined3d_context_tls_idx);
1494 BOOL context_set_current(struct wined3d_context *ctx)
1496 struct wined3d_context *old = context_get_current();
1498 if (old == ctx)
1500 TRACE("Already using D3D context %p.\n", ctx);
1501 return TRUE;
1504 if (old)
1506 if (old->destroyed)
1508 TRACE("Switching away from destroyed context %p.\n", old);
1509 context_destroy_gl_resources(old);
1510 heap_free((void *)old->gl_info);
1511 heap_free(old);
1513 else
1515 if (wglGetCurrentContext())
1517 const struct wined3d_gl_info *gl_info = old->gl_info;
1518 TRACE("Flushing context %p before switching to %p.\n", old, ctx);
1519 gl_info->gl_ops.gl.p_glFlush();
1521 old->current = 0;
1525 if (ctx)
1527 if (!ctx->valid)
1529 ERR("Trying to make invalid context %p current\n", ctx);
1530 return FALSE;
1533 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
1534 if (!context_set_gl_context(ctx))
1535 return FALSE;
1536 ctx->current = 1;
1538 else if (wglGetCurrentContext())
1540 TRACE("Clearing current D3D context.\n");
1541 if (!wglMakeCurrent(NULL, NULL))
1543 DWORD err = GetLastError();
1544 ERR("Failed to clear current GL context, last error %#x.\n", err);
1545 TlsSetValue(wined3d_context_tls_idx, NULL);
1546 return FALSE;
1550 return TlsSetValue(wined3d_context_tls_idx, ctx);
1553 void context_release(struct wined3d_context *context)
1555 TRACE("Releasing context %p, level %u.\n", context, context->level);
1557 if (WARN_ON(d3d))
1559 if (!context->level)
1560 WARN("Context %p is not active.\n", context);
1561 else if (context != context_get_current())
1562 WARN("Context %p is not the current context.\n", context);
1565 if (!--context->level)
1567 if (context_restore_pixel_format(context))
1568 context->needs_set = 1;
1569 if (context->restore_ctx)
1571 TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
1572 context_restore_gl_context(context->gl_info, context->restore_dc, context->restore_ctx);
1573 context->restore_ctx = NULL;
1574 context->restore_dc = NULL;
1577 if (context->destroy_delayed)
1579 TRACE("Destroying context %p.\n", context);
1580 context_destroy(context->device, context);
1585 /* This is used when a context for render target A is active, but a separate context is
1586 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1587 * A to avoid breaking caller code. */
1588 void context_restore(struct wined3d_context *context, struct wined3d_surface *restore)
1590 if (context->current_rt.texture != restore->container
1591 || context->current_rt.sub_resource_idx != surface_get_sub_resource_idx(restore))
1593 context_release(context);
1594 context = context_acquire(restore->container->resource.device,
1595 restore->container, surface_get_sub_resource_idx(restore));
1598 context_release(context);
1601 static void context_enter(struct wined3d_context *context)
1603 TRACE("Entering context %p, level %u.\n", context, context->level + 1);
1605 if (!context->level++)
1607 const struct wined3d_context *current_context = context_get_current();
1608 HGLRC current_gl = wglGetCurrentContext();
1610 if (current_gl && (!current_context || current_context->glCtx != current_gl))
1612 TRACE("Another GL context (%p on device context %p) is already current.\n",
1613 current_gl, wglGetCurrentDC());
1614 context->restore_ctx = current_gl;
1615 context->restore_dc = wglGetCurrentDC();
1616 context->needs_set = 1;
1618 else if (!context->needs_set && !(context->hdc_is_private && context->hdc_has_format)
1619 && context->pixel_format != context->gl_info->gl_ops.wgl.p_wglGetPixelFormat(context->hdc))
1620 context->needs_set = 1;
1624 void context_invalidate_compute_state(struct wined3d_context *context, DWORD state_id)
1626 DWORD representative = context->state_table[state_id].representative - STATE_COMPUTE_OFFSET;
1627 unsigned int index, shift;
1629 index = representative / (sizeof(*context->dirty_compute_states) * CHAR_BIT);
1630 shift = representative & (sizeof(*context->dirty_compute_states) * CHAR_BIT - 1);
1631 context->dirty_compute_states[index] |= (1u << shift);
1634 void context_invalidate_state(struct wined3d_context *context, DWORD state)
1636 DWORD rep = context->state_table[state].representative;
1637 DWORD idx;
1638 BYTE shift;
1640 if (isStateDirty(context, rep)) return;
1642 context->dirtyArray[context->numDirtyEntries++] = rep;
1643 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
1644 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
1645 context->isStateDirty[idx] |= (1u << shift);
1648 /* This function takes care of wined3d pixel format selection. */
1649 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1650 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1651 BOOL auxBuffers)
1653 unsigned int cfg_count = device->adapter->cfg_count;
1654 unsigned int current_value;
1655 PIXELFORMATDESCRIPTOR pfd;
1656 int iPixelFormat = 0;
1657 unsigned int i;
1659 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1660 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1661 auxBuffers);
1663 current_value = 0;
1664 for (i = 0; i < cfg_count; ++i)
1666 const struct wined3d_pixel_format *cfg = &device->adapter->cfgs[i];
1667 unsigned int value;
1669 /* For now only accept RGBA formats. Perhaps some day we will
1670 * allow floating point formats for pbuffers. */
1671 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1672 continue;
1673 /* In window mode we need a window drawable format and double buffering. */
1674 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1675 continue;
1676 if (cfg->redSize < color_format->red_size)
1677 continue;
1678 if (cfg->greenSize < color_format->green_size)
1679 continue;
1680 if (cfg->blueSize < color_format->blue_size)
1681 continue;
1682 if (cfg->alphaSize < color_format->alpha_size)
1683 continue;
1684 if (cfg->depthSize < ds_format->depth_size)
1685 continue;
1686 if (ds_format->stencil_size && cfg->stencilSize != ds_format->stencil_size)
1687 continue;
1688 /* Check multisampling support. */
1689 if (cfg->numSamples)
1690 continue;
1692 value = 1;
1693 /* We try to locate a format which matches our requirements exactly. In case of
1694 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1695 if (cfg->depthSize == ds_format->depth_size)
1696 value += 1;
1697 if (cfg->stencilSize == ds_format->stencil_size)
1698 value += 2;
1699 if (cfg->alphaSize == color_format->alpha_size)
1700 value += 4;
1701 /* We like to have aux buffers in backbuffer mode */
1702 if (auxBuffers && cfg->auxBuffers)
1703 value += 8;
1704 if (cfg->redSize == color_format->red_size
1705 && cfg->greenSize == color_format->green_size
1706 && cfg->blueSize == color_format->blue_size)
1707 value += 16;
1709 if (value > current_value)
1711 iPixelFormat = cfg->iPixelFormat;
1712 current_value = value;
1716 if (!iPixelFormat)
1718 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1720 memset(&pfd, 0, sizeof(pfd));
1721 pfd.nSize = sizeof(pfd);
1722 pfd.nVersion = 1;
1723 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1724 pfd.iPixelType = PFD_TYPE_RGBA;
1725 pfd.cAlphaBits = color_format->alpha_size;
1726 pfd.cColorBits = color_format->red_size + color_format->green_size
1727 + color_format->blue_size + color_format->alpha_size;
1728 pfd.cDepthBits = ds_format->depth_size;
1729 pfd.cStencilBits = ds_format->stencil_size;
1730 pfd.iLayerType = PFD_MAIN_PLANE;
1732 if (!(iPixelFormat = ChoosePixelFormat(hdc, &pfd)))
1734 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1735 ERR("Can't find a suitable pixel format.\n");
1736 return 0;
1740 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1741 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1742 return iPixelFormat;
1745 /* Context activation is done by the caller. */
1746 void context_bind_dummy_textures(const struct wined3d_device *device, const struct wined3d_context *context)
1748 const struct wined3d_dummy_textures *textures = &context->device->dummy_textures;
1749 const struct wined3d_gl_info *gl_info = context->gl_info;
1750 unsigned int i;
1752 for (i = 0; i < gl_info->limits.combined_samplers; ++i)
1754 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + i));
1756 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
1758 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1759 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
1761 if (gl_info->supported[EXT_TEXTURE3D])
1762 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
1764 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1765 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
1767 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
1768 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
1770 if (gl_info->supported[EXT_TEXTURE_ARRAY])
1771 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
1773 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1774 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
1776 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1778 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
1779 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
1783 checkGLcall("bind dummy textures");
1786 void wined3d_check_gl_call(const struct wined3d_gl_info *gl_info,
1787 const char *file, unsigned int line, const char *name)
1789 GLint err;
1791 if (gl_info->supported[ARB_DEBUG_OUTPUT] || (err = gl_info->gl_ops.gl.p_glGetError()) == GL_NO_ERROR)
1793 TRACE("%s call ok %s / %u.\n", name, file, line);
1794 return;
1799 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1800 debug_glerror(err), err, name, file,line);
1801 err = gl_info->gl_ops.gl.p_glGetError();
1802 } while (err != GL_NO_ERROR);
1805 static BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1807 return gl_info->supported[ARB_DEBUG_OUTPUT]
1808 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1811 static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1812 GLenum severity, GLsizei length, const char *message, void *ctx)
1814 switch (type)
1816 case GL_DEBUG_TYPE_ERROR_ARB:
1817 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1818 break;
1820 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1821 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1822 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1823 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1824 break;
1826 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1827 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1828 break;
1830 default:
1831 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1832 break;
1836 HGLRC context_create_wgl_attribs(const struct wined3d_gl_info *gl_info, HDC hdc, HGLRC share_ctx)
1838 HGLRC ctx;
1839 unsigned int ctx_attrib_idx = 0;
1840 GLint ctx_attribs[7], ctx_flags = 0;
1842 if (context_debug_output_enabled(gl_info))
1843 ctx_flags = WGL_CONTEXT_DEBUG_BIT_ARB;
1844 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
1845 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version >> 16;
1846 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MINOR_VERSION_ARB;
1847 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version & 0xffff;
1848 if (gl_info->selected_gl_version >= MAKEDWORD_VERSION(3, 2))
1849 ctx_flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1850 if (ctx_flags)
1852 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1853 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1855 ctx_attribs[ctx_attrib_idx] = 0;
1857 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1859 if (ctx_flags & WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB)
1861 ctx_attribs[ctx_attrib_idx - 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1862 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1863 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1864 GetLastError());
1867 return ctx;
1870 struct wined3d_context *context_create(struct wined3d_swapchain *swapchain,
1871 struct wined3d_texture *target, const struct wined3d_format *ds_format)
1873 struct wined3d_device *device = swapchain->device;
1874 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
1875 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1876 const struct wined3d_format *color_format;
1877 struct wined3d_context *ret;
1878 BOOL auxBuffers = FALSE;
1879 HGLRC ctx, share_ctx;
1880 DWORD target_usage;
1881 unsigned int i;
1882 DWORD state;
1884 TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
1886 wined3d_from_cs(device->cs);
1888 if (!(ret = heap_alloc_zero(sizeof(*ret))))
1889 return NULL;
1891 ret->free_timestamp_query_size = 4;
1892 if (!(ret->free_timestamp_queries = heap_calloc(ret->free_timestamp_query_size,
1893 sizeof(*ret->free_timestamp_queries))))
1894 goto out;
1895 list_init(&ret->timestamp_queries);
1897 ret->free_occlusion_query_size = 4;
1898 if (!(ret->free_occlusion_queries = heap_calloc(ret->free_occlusion_query_size,
1899 sizeof(*ret->free_occlusion_queries))))
1900 goto out;
1901 list_init(&ret->occlusion_queries);
1903 ret->free_fence_size = 4;
1904 if (!(ret->free_fences = heap_calloc(ret->free_fence_size, sizeof(*ret->free_fences))))
1905 goto out;
1906 list_init(&ret->fences);
1908 list_init(&ret->so_statistics_queries);
1910 list_init(&ret->pipeline_statistics_queries);
1912 list_init(&ret->fbo_list);
1913 list_init(&ret->fbo_destroy_list);
1915 if (!device->shader_backend->shader_allocate_context_data(ret))
1917 ERR("Failed to allocate shader backend context data.\n");
1918 goto out;
1920 if (!device->adapter->fragment_pipe->allocate_context_data(ret))
1922 ERR("Failed to allocate fragment pipeline context data.\n");
1923 goto out;
1926 for (i = 0; i < ARRAY_SIZE(ret->tex_unit_map); ++i)
1927 ret->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
1928 for (i = 0; i < ARRAY_SIZE(ret->rev_tex_unit_map); ++i)
1929 ret->rev_tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
1930 if (gl_info->limits.graphics_samplers >= MAX_COMBINED_SAMPLERS)
1932 /* Initialize the texture unit mapping to a 1:1 mapping. */
1933 unsigned int base, count;
1935 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_PIXEL, &base, &count);
1936 if (base + MAX_FRAGMENT_SAMPLERS > ARRAY_SIZE(ret->rev_tex_unit_map))
1938 ERR("Unexpected texture unit base index %u.\n", base);
1939 goto out;
1941 for (i = 0; i < min(count, MAX_FRAGMENT_SAMPLERS); ++i)
1943 ret->tex_unit_map[i] = base + i;
1944 ret->rev_tex_unit_map[base + i] = i;
1947 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_VERTEX, &base, &count);
1948 if (base + MAX_VERTEX_SAMPLERS > ARRAY_SIZE(ret->rev_tex_unit_map))
1950 ERR("Unexpected texture unit base index %u.\n", base);
1951 goto out;
1953 for (i = 0; i < min(count, MAX_VERTEX_SAMPLERS); ++i)
1955 ret->tex_unit_map[MAX_FRAGMENT_SAMPLERS + i] = base + i;
1956 ret->rev_tex_unit_map[base + i] = MAX_FRAGMENT_SAMPLERS + i;
1960 if (!(ret->texture_type = heap_calloc(gl_info->limits.combined_samplers,
1961 sizeof(*ret->texture_type))))
1962 goto out;
1964 if (!(ret->hdc = GetDCEx(swapchain->win_handle, 0, DCX_USESTYLE | DCX_CACHE)))
1966 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1968 if ((ret->hdc = swapchain_get_backup_dc(swapchain)))
1969 ret->hdc_is_private = TRUE;
1970 else
1972 ERR("Failed to retrieve a device context.\n");
1973 goto out;
1977 color_format = target->resource.format;
1978 target_usage = target->resource.usage;
1980 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1981 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1982 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1984 auxBuffers = TRUE;
1986 if (color_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1987 color_format = wined3d_get_format(gl_info, WINED3DFMT_B4G4R4A4_UNORM, target_usage);
1988 else if (color_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1989 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM, target_usage);
1992 /* DirectDraw supports 8bit paletted render targets and these are used by
1993 * old games like StarCraft and C&C. Most modern hardware doesn't support
1994 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1995 * conversion (ab)uses the alpha component for storing the palette index.
1996 * For this reason we require a format with 8bit alpha, so request
1997 * A8R8G8B8. */
1998 if (color_format->id == WINED3DFMT_P8_UINT)
1999 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM, target_usage);
2001 /* When using FBOs for off-screen rendering, we only use the drawable for
2002 * presentation blits, and don't do any rendering to it. That means we
2003 * don't need depth or stencil buffers, and can mostly ignore the render
2004 * target format. This wouldn't necessarily be quite correct for 10bpc
2005 * display modes, but we don't currently support those.
2006 * Using the same format regardless of the color/depth/stencil targets
2007 * makes it much less likely that different wined3d instances will set
2008 * conflicting pixel formats. */
2009 if (wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
2011 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM, target_usage);
2012 ds_format = wined3d_get_format(gl_info, WINED3DFMT_UNKNOWN, WINED3DUSAGE_DEPTHSTENCIL);
2015 /* Try to find a pixel format which matches our requirements. */
2016 if (!(ret->pixel_format = context_choose_pixel_format(device, ret->hdc, color_format, ds_format, auxBuffers)))
2017 goto out;
2019 ret->gl_info = gl_info;
2020 ret->win_handle = swapchain->win_handle;
2022 context_enter(ret);
2024 if (!context_set_pixel_format(ret))
2026 ERR("Failed to set pixel format %d on device context %p.\n", ret->pixel_format, ret->hdc);
2027 context_release(ret);
2028 goto out;
2031 share_ctx = device->context_count ? device->contexts[0]->glCtx : NULL;
2032 if (gl_info->p_wglCreateContextAttribsARB)
2034 if (!(ctx = context_create_wgl_attribs(gl_info, ret->hdc, share_ctx)))
2035 goto out;
2037 else
2039 if (!(ctx = wglCreateContext(ret->hdc)))
2041 ERR("Failed to create a WGL context.\n");
2042 context_release(ret);
2043 goto out;
2046 if (share_ctx && !wglShareLists(share_ctx, ctx))
2048 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
2049 context_release(ret);
2050 if (!wglDeleteContext(ctx))
2051 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2052 goto out;
2056 if (!device_context_add(device, ret))
2058 ERR("Failed to add the newly created context to the context list\n");
2059 context_release(ret);
2060 if (!wglDeleteContext(ctx))
2061 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2062 goto out;
2065 ret->d3d_info = d3d_info;
2066 ret->state_table = device->StateTable;
2068 /* Mark all states dirty to force a proper initialization of the states on
2069 * the first use of the context. Compute states do not need initialization. */
2070 for (state = 0; state <= STATE_HIGHEST; ++state)
2072 if (ret->state_table[state].representative && !STATE_IS_COMPUTE(state))
2073 context_invalidate_state(ret, state);
2076 ret->device = device;
2077 ret->swapchain = swapchain;
2078 ret->current_rt.texture = target;
2079 ret->current_rt.sub_resource_idx = 0;
2080 ret->tid = GetCurrentThreadId();
2082 ret->render_offscreen = wined3d_resource_is_offscreen(&target->resource);
2083 ret->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
2084 ret->valid = 1;
2086 ret->glCtx = ctx;
2087 ret->hdc_has_format = TRUE;
2088 ret->needs_set = 1;
2090 /* Set up the context defaults */
2091 if (!context_set_current(ret))
2093 ERR("Cannot activate context to set up defaults.\n");
2094 device_context_remove(device, ret);
2095 context_release(ret);
2096 if (!wglDeleteContext(ctx))
2097 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2098 goto out;
2101 if (context_debug_output_enabled(gl_info))
2103 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback, ret));
2104 if (TRACE_ON(d3d_synchronous))
2105 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2106 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
2107 if (ERR_ON(d3d))
2109 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR,
2110 GL_DONT_CARE, 0, NULL, GL_TRUE));
2112 if (FIXME_ON(d3d))
2114 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
2115 GL_DONT_CARE, 0, NULL, GL_TRUE));
2116 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
2117 GL_DONT_CARE, 0, NULL, GL_TRUE));
2118 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY,
2119 GL_DONT_CARE, 0, NULL, GL_TRUE));
2121 if (WARN_ON(d3d_perf))
2123 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE,
2124 GL_DONT_CARE, 0, NULL, GL_TRUE));
2128 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2129 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
2131 TRACE("Setting up the screen\n");
2133 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2135 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
2136 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2138 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
2139 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2141 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2142 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2144 else
2146 GLuint vao;
2148 GL_EXTCALL(glGenVertexArrays(1, &vao));
2149 GL_EXTCALL(glBindVertexArray(vao));
2150 checkGLcall("creating VAO");
2153 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
2154 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2155 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2156 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2158 if (gl_info->supported[ARB_VERTEX_BLEND])
2160 /* Direct3D always uses n-1 weights for n world matrices and uses
2161 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
2162 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
2163 * enabled as well. */
2164 gl_info->gl_ops.gl.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB);
2165 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
2167 if (gl_info->supported[NV_TEXTURE_SHADER2])
2169 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2170 * the previous texture where to source the offset from is always unit - 1.
2172 for (i = 1; i < gl_info->limits.textures; ++i)
2174 context_active_texture(ret, gl_info, i);
2175 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
2176 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + i - 1);
2177 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2180 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2182 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2183 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2184 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2185 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2186 * is ever assigned.
2188 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2189 * program and the dummy program is destroyed when the context is destroyed.
2191 static const char dummy_program[] =
2192 "!!ARBfp1.0\n"
2193 "MOV result.color, fragment.color.primary;\n"
2194 "END\n";
2195 GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
2196 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
2197 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2200 if (gl_info->supported[ARB_POINT_SPRITE])
2202 for (i = 0; i < gl_info->limits.textures; ++i)
2204 context_active_texture(ret, gl_info, i);
2205 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2206 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2210 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2212 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2214 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2216 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2218 if (!(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART))
2220 if (gl_info->supported[ARB_ES3_COMPATIBILITY])
2222 gl_info->gl_ops.gl.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
2223 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2225 else
2227 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2230 if (!(d3d_info->wined3d_creation_flags & WINED3D_LEGACY_CUBEMAP_FILTERING)
2231 && gl_info->supported[ARB_SEAMLESS_CUBE_MAP])
2233 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
2234 checkGLcall("enable seamless cube map filtering");
2236 if (gl_info->supported[ARB_CLIP_CONTROL])
2237 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT));
2238 device->shader_backend->shader_init_context_state(ret);
2239 ret->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
2240 | (1u << WINED3D_SHADER_TYPE_VERTEX)
2241 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
2242 | (1u << WINED3D_SHADER_TYPE_HULL)
2243 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
2244 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
2246 /* If this happens to be the first context for the device, dummy textures
2247 * are not created yet. In that case, they will be created (and bound) by
2248 * create_dummy_textures right after this context is initialized. */
2249 if (device->dummy_textures.tex_2d)
2250 context_bind_dummy_textures(device, ret);
2252 TRACE("Created context %p.\n", ret);
2254 return ret;
2256 out:
2257 if (ret->hdc)
2258 wined3d_release_dc(swapchain->win_handle, ret->hdc);
2259 device->shader_backend->shader_free_context_data(ret);
2260 device->adapter->fragment_pipe->free_context_data(ret);
2261 heap_free(ret->texture_type);
2262 heap_free(ret->free_fences);
2263 heap_free(ret->free_occlusion_queries);
2264 heap_free(ret->free_timestamp_queries);
2265 heap_free(ret);
2266 return NULL;
2269 void context_destroy(struct wined3d_device *device, struct wined3d_context *context)
2271 BOOL destroy;
2273 TRACE("Destroying ctx %p\n", context);
2275 wined3d_from_cs(device->cs);
2277 /* We delay destroying a context when it is active. The context_release()
2278 * function invokes context_destroy() again while leaving the last level. */
2279 if (context->level)
2281 TRACE("Delaying destruction of context %p.\n", context);
2282 context->destroy_delayed = 1;
2283 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2284 context->swapchain = NULL;
2285 return;
2288 if (context->tid == GetCurrentThreadId() || !context->current)
2290 context_destroy_gl_resources(context);
2291 TlsSetValue(wined3d_context_tls_idx, NULL);
2292 destroy = TRUE;
2294 else
2296 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2297 in wined3d_adapter may go away in the meantime */
2298 struct wined3d_gl_info *gl_info = heap_alloc(sizeof(*gl_info));
2299 *gl_info = *context->gl_info;
2300 context->gl_info = gl_info;
2301 context->destroyed = 1;
2302 destroy = FALSE;
2305 device->shader_backend->shader_free_context_data(context);
2306 device->adapter->fragment_pipe->free_context_data(context);
2307 heap_free(context->texture_type);
2308 device_context_remove(device, context);
2309 if (destroy)
2310 heap_free(context);
2313 const DWORD *context_get_tex_unit_mapping(const struct wined3d_context *context,
2314 const struct wined3d_shader_version *shader_version, unsigned int *base, unsigned int *count)
2316 const struct wined3d_gl_info *gl_info = context->gl_info;
2318 if (!shader_version)
2320 *base = 0;
2321 *count = MAX_TEXTURES;
2322 return context->tex_unit_map;
2325 if (shader_version->major >= 4)
2327 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, shader_version->type, base, count);
2328 return NULL;
2331 switch (shader_version->type)
2333 case WINED3D_SHADER_TYPE_PIXEL:
2334 *base = 0;
2335 *count = MAX_FRAGMENT_SAMPLERS;
2336 break;
2337 case WINED3D_SHADER_TYPE_VERTEX:
2338 *base = MAX_FRAGMENT_SAMPLERS;
2339 *count = MAX_VERTEX_SAMPLERS;
2340 break;
2341 default:
2342 ERR("Unhandled shader type %#x.\n", shader_version->type);
2343 *base = 0;
2344 *count = 0;
2347 return context->tex_unit_map;
2350 /* Context activation is done by the caller. */
2351 static void set_blit_dimension(const struct wined3d_gl_info *gl_info, UINT width, UINT height)
2353 const GLdouble projection[] =
2355 2.0 / width, 0.0, 0.0, 0.0,
2356 0.0, 2.0 / height, 0.0, 0.0,
2357 0.0, 0.0, 2.0, 0.0,
2358 -1.0, -1.0, -1.0, 1.0,
2361 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2363 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
2364 checkGLcall("glMatrixMode(GL_PROJECTION)");
2365 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
2366 checkGLcall("glLoadMatrixd");
2368 gl_info->gl_ops.gl.p_glViewport(0, 0, width, height);
2369 checkGLcall("glViewport");
2372 static void context_get_rt_size(const struct wined3d_context *context, SIZE *size)
2374 const struct wined3d_texture *rt = context->current_rt.texture;
2375 unsigned int level;
2377 if (rt->swapchain)
2379 RECT window_size;
2381 GetClientRect(context->win_handle, &window_size);
2382 size->cx = window_size.right - window_size.left;
2383 size->cy = window_size.bottom - window_size.top;
2385 return;
2388 level = context->current_rt.sub_resource_idx % rt->level_count;
2389 size->cx = wined3d_texture_get_level_width(rt, level);
2390 size->cy = wined3d_texture_get_level_height(rt, level);
2393 void context_enable_clip_distances(struct wined3d_context *context, unsigned int enable_mask)
2395 const struct wined3d_gl_info *gl_info = context->gl_info;
2396 unsigned int clip_distance_count = gl_info->limits.user_clip_distances;
2397 unsigned int i, disable_mask, current_mask;
2399 disable_mask = ~enable_mask;
2400 enable_mask &= (1u << clip_distance_count) - 1;
2401 disable_mask &= (1u << clip_distance_count) - 1;
2402 current_mask = context->clip_distance_mask;
2403 context->clip_distance_mask = enable_mask;
2405 enable_mask &= ~current_mask;
2406 while (enable_mask)
2408 i = wined3d_bit_scan(&enable_mask);
2409 gl_info->gl_ops.gl.p_glEnable(GL_CLIP_DISTANCE0 + i);
2411 disable_mask &= current_mask;
2412 while (disable_mask)
2414 i = wined3d_bit_scan(&disable_mask);
2415 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_DISTANCE0 + i);
2417 checkGLcall("toggle clip distances");
2420 /*****************************************************************************
2421 * SetupForBlit
2423 * Sets up a context for DirectDraw blitting.
2424 * All texture units are disabled, texture unit 0 is set as current unit
2425 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
2426 * color writing enabled for all channels
2427 * register combiners disabled, shaders disabled
2428 * world matrix is set to identity, texture matrix 0 too
2429 * projection matrix is setup for drawing screen coordinates
2431 * Params:
2432 * This: Device to activate the context for
2433 * context: Context to setup
2435 *****************************************************************************/
2436 /* Context activation is done by the caller. */
2437 static void SetupForBlit(const struct wined3d_device *device, struct wined3d_context *context)
2439 const struct wined3d_gl_info *gl_info = context->gl_info;
2440 DWORD sampler;
2441 SIZE rt_size;
2442 int i;
2444 TRACE("Setting up context %p for blitting\n", context);
2446 context_get_rt_size(context, &rt_size);
2448 if (context->last_was_blit)
2450 if (context->blit_w != rt_size.cx || context->blit_h != rt_size.cy)
2452 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
2453 context->blit_w = rt_size.cx;
2454 context->blit_h = rt_size.cy;
2455 /* No need to dirtify here, the states are still dirtified because
2456 * they weren't applied since the last SetupForBlit() call. */
2458 TRACE("Context is already set up for blitting, nothing to do\n");
2459 return;
2461 context->last_was_blit = TRUE;
2463 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2465 /* Disable all textures. The caller can then bind a texture it wants to blit
2466 * from
2468 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
2469 * function texture unit. No need to care for higher samplers
2471 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
2473 sampler = context->rev_tex_unit_map[i];
2474 context_active_texture(context, gl_info, i);
2476 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2478 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2479 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2481 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2482 checkGLcall("glDisable GL_TEXTURE_3D");
2483 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2485 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2486 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2488 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2489 checkGLcall("glDisable GL_TEXTURE_2D");
2491 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2492 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
2494 if (sampler != WINED3D_UNMAPPED_STAGE)
2496 if (sampler < MAX_TEXTURES)
2497 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2498 context_invalidate_state(context, STATE_SAMPLER(sampler));
2502 context_active_texture(context, gl_info, 0);
2503 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2505 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2506 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2508 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2509 checkGLcall("glDisable GL_TEXTURE_3D");
2510 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2512 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2513 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2515 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2516 checkGLcall("glDisable GL_TEXTURE_2D");
2518 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2520 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
2521 checkGLcall("glMatrixMode(GL_TEXTURE)");
2522 gl_info->gl_ops.gl.p_glLoadIdentity();
2523 checkGLcall("glLoadIdentity()");
2525 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
2527 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
2528 GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
2529 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2532 /* Setup transforms */
2533 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
2534 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2535 gl_info->gl_ops.gl.p_glLoadIdentity();
2536 checkGLcall("glLoadIdentity()");
2537 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2539 /* Other misc states */
2540 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
2541 checkGLcall("glDisable(GL_ALPHA_TEST)");
2542 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
2543 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
2544 checkGLcall("glDisable GL_LIGHTING");
2545 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
2546 glDisableWINE(GL_FOG);
2547 checkGLcall("glDisable GL_FOG");
2548 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
2551 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
2552 GL_EXTCALL(glBindSampler(0, 0));
2553 context_active_texture(context, gl_info, 0);
2555 sampler = context->rev_tex_unit_map[0];
2556 if (sampler != WINED3D_UNMAPPED_STAGE)
2558 if (sampler < MAX_TEXTURES)
2560 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
2561 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2563 context_invalidate_state(context, STATE_SAMPLER(sampler));
2566 /* Other misc states */
2567 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
2568 checkGLcall("glDisable GL_DEPTH_TEST");
2569 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZENABLE));
2570 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2571 checkGLcall("glDisable GL_BLEND");
2572 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2573 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
2574 checkGLcall("glDisable GL_CULL_FACE");
2575 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CULLMODE));
2576 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
2577 checkGLcall("glDisable GL_STENCIL_TEST");
2578 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILENABLE));
2579 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
2580 checkGLcall("glDisable GL_SCISSOR_TEST");
2581 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2582 if (gl_info->supported[ARB_POINT_SPRITE])
2584 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
2585 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2586 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
2588 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
2589 checkGLcall("glColorMask");
2590 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
2591 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
2592 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
2593 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
2594 if (gl_info->supported[EXT_SECONDARY_COLOR])
2596 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
2597 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
2598 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2601 context->last_was_rhw = TRUE;
2602 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
2604 context_enable_clip_distances(context, 0);
2605 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
2607 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
2608 if (gl_info->supported[ARB_CLIP_CONTROL])
2609 GL_EXTCALL(glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE));
2611 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
2613 /* Disable shaders */
2614 device->shader_backend->shader_disable(device->shader_priv, context);
2616 context->blit_w = rt_size.cx;
2617 context->blit_h = rt_size.cy;
2618 context_invalidate_state(context, STATE_VIEWPORT);
2619 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2622 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2624 return rt_mask & (1u << 31);
2627 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2629 return rt_mask & ~(1u << 31);
2632 /* Context activation is done by the caller. */
2633 static void context_apply_draw_buffers(struct wined3d_context *context, DWORD rt_mask)
2635 const struct wined3d_gl_info *gl_info = context->gl_info;
2636 GLenum draw_buffers[MAX_RENDER_TARGET_VIEWS];
2638 if (!rt_mask)
2640 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2642 else if (is_rt_mask_onscreen(rt_mask))
2644 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2646 else
2648 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2650 unsigned int i = 0;
2652 while (rt_mask)
2654 if (rt_mask & 1)
2655 draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2656 else
2657 draw_buffers[i] = GL_NONE;
2659 rt_mask >>= 1;
2660 ++i;
2663 if (gl_info->supported[ARB_DRAW_BUFFERS])
2665 GL_EXTCALL(glDrawBuffers(i, draw_buffers));
2667 else
2669 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffers[0]);
2672 else
2674 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2678 checkGLcall("apply draw buffers");
2681 /* Context activation is done by the caller. */
2682 void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
2684 const struct wined3d_gl_info *gl_info = context->gl_info;
2685 DWORD *current_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2686 DWORD new_mask = context_generate_rt_mask(buffer);
2688 if (new_mask == *current_mask)
2689 return;
2691 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2692 checkGLcall("glDrawBuffer()");
2694 *current_mask = new_mask;
2697 /* Context activation is done by the caller. */
2698 void context_active_texture(struct wined3d_context *context, const struct wined3d_gl_info *gl_info, unsigned int unit)
2700 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + unit));
2701 checkGLcall("glActiveTexture");
2702 context->active_texture = unit;
2705 void context_bind_bo(struct wined3d_context *context, GLenum binding, GLuint name)
2707 const struct wined3d_gl_info *gl_info = context->gl_info;
2709 if (binding == GL_ELEMENT_ARRAY_BUFFER)
2710 context_invalidate_state(context, STATE_INDEXBUFFER);
2712 GL_EXTCALL(glBindBuffer(binding, name));
2715 void context_bind_texture(struct wined3d_context *context, GLenum target, GLuint name)
2717 const struct wined3d_dummy_textures *textures = &context->device->dummy_textures;
2718 const struct wined3d_gl_info *gl_info = context->gl_info;
2719 DWORD unit = context->active_texture;
2720 DWORD old_texture_type = context->texture_type[unit];
2722 if (name)
2724 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2726 else
2728 target = GL_NONE;
2731 if (old_texture_type != target)
2733 switch (old_texture_type)
2735 case GL_NONE:
2736 /* nothing to do */
2737 break;
2738 case GL_TEXTURE_2D:
2739 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
2740 break;
2741 case GL_TEXTURE_2D_ARRAY:
2742 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
2743 break;
2744 case GL_TEXTURE_RECTANGLE_ARB:
2745 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
2746 break;
2747 case GL_TEXTURE_CUBE_MAP:
2748 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
2749 break;
2750 case GL_TEXTURE_CUBE_MAP_ARRAY:
2751 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
2752 break;
2753 case GL_TEXTURE_3D:
2754 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
2755 break;
2756 case GL_TEXTURE_BUFFER:
2757 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
2758 break;
2759 case GL_TEXTURE_2D_MULTISAMPLE:
2760 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
2761 break;
2762 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2763 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
2764 break;
2765 default:
2766 ERR("Unexpected texture target %#x.\n", old_texture_type);
2769 context->texture_type[unit] = target;
2772 checkGLcall("bind texture");
2775 void *context_map_bo_address(struct wined3d_context *context,
2776 const struct wined3d_bo_address *data, size_t size, GLenum binding, DWORD flags)
2778 const struct wined3d_gl_info *gl_info;
2779 BYTE *memory;
2781 if (!data->buffer_object)
2782 return data->addr;
2784 gl_info = context->gl_info;
2785 context_bind_bo(context, binding, data->buffer_object);
2787 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2789 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
2790 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
2792 else
2794 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
2795 memory += (INT_PTR)data->addr;
2798 context_bind_bo(context, binding, 0);
2799 checkGLcall("Map buffer object");
2801 return memory;
2804 void context_unmap_bo_address(struct wined3d_context *context,
2805 const struct wined3d_bo_address *data, GLenum binding)
2807 const struct wined3d_gl_info *gl_info;
2809 if (!data->buffer_object)
2810 return;
2812 gl_info = context->gl_info;
2813 context_bind_bo(context, binding, data->buffer_object);
2814 GL_EXTCALL(glUnmapBuffer(binding));
2815 context_bind_bo(context, binding, 0);
2816 checkGLcall("Unmap buffer object");
2819 void context_copy_bo_address(struct wined3d_context *context,
2820 const struct wined3d_bo_address *dst, GLenum dst_binding,
2821 const struct wined3d_bo_address *src, GLenum src_binding, size_t size)
2823 const struct wined3d_gl_info *gl_info;
2824 BYTE *dst_ptr, *src_ptr;
2826 gl_info = context->gl_info;
2828 if (dst->buffer_object && src->buffer_object)
2830 if (gl_info->supported[ARB_COPY_BUFFER])
2832 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER, src->buffer_object));
2833 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER, dst->buffer_object));
2834 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
2835 (GLintptr)src->addr, (GLintptr)dst->addr, size));
2836 checkGLcall("direct buffer copy");
2838 else
2840 src_ptr = context_map_bo_address(context, src, size, src_binding, WINED3D_MAP_READ);
2841 dst_ptr = context_map_bo_address(context, dst, size, dst_binding, WINED3D_MAP_WRITE);
2843 memcpy(dst_ptr, src_ptr, size);
2845 context_unmap_bo_address(context, dst, dst_binding);
2846 context_unmap_bo_address(context, src, src_binding);
2849 else if (!dst->buffer_object && src->buffer_object)
2851 context_bind_bo(context, src_binding, src->buffer_object);
2852 GL_EXTCALL(glGetBufferSubData(src_binding, (GLintptr)src->addr, size, dst->addr));
2853 checkGLcall("buffer download");
2855 else if (dst->buffer_object && !src->buffer_object)
2857 context_bind_bo(context, dst_binding, dst->buffer_object);
2858 GL_EXTCALL(glBufferSubData(dst_binding, (GLintptr)dst->addr, size, src->addr));
2859 checkGLcall("buffer upload");
2861 else
2863 memcpy(dst->addr, src->addr, size);
2867 static void context_set_render_offscreen(struct wined3d_context *context, BOOL offscreen)
2869 if (context->render_offscreen == offscreen)
2870 return;
2872 context_invalidate_state(context, STATE_VIEWPORT);
2873 context_invalidate_state(context, STATE_SCISSORRECT);
2874 if (!context->gl_info->supported[ARB_CLIP_CONTROL])
2876 context_invalidate_state(context, STATE_FRONTFACE);
2877 context_invalidate_state(context, STATE_POINTSPRITECOORDORIGIN);
2878 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2880 context_invalidate_state(context, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN));
2881 if (context->gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2882 context_invalidate_state(context, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
2883 context->render_offscreen = offscreen;
2886 static BOOL match_depth_stencil_format(const struct wined3d_format *existing,
2887 const struct wined3d_format *required)
2889 if (existing == required)
2890 return TRUE;
2891 if ((existing->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FLOAT)
2892 != (required->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FLOAT))
2893 return FALSE;
2894 if (existing->depth_size < required->depth_size)
2895 return FALSE;
2896 /* If stencil bits are used the exact amount is required - otherwise
2897 * wrapping won't work correctly. */
2898 if (required->stencil_size && required->stencil_size != existing->stencil_size)
2899 return FALSE;
2900 return TRUE;
2903 /* Context activation is done by the caller. */
2904 static void context_validate_onscreen_formats(struct wined3d_context *context,
2905 const struct wined3d_rendertarget_view *depth_stencil)
2907 /* Onscreen surfaces are always in a swapchain */
2908 struct wined3d_swapchain *swapchain = context->current_rt.texture->swapchain;
2910 if (context->render_offscreen || !depth_stencil) return;
2911 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->format)) return;
2913 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2914 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2915 * format. */
2916 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2918 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2919 if (!(wined3d_texture_load_location(context->current_rt.texture, context->current_rt.sub_resource_idx,
2920 context, WINED3D_LOCATION_TEXTURE_RGB)))
2921 ERR("Failed to load location.\n");
2922 swapchain->render_to_fbo = TRUE;
2923 swapchain_update_draw_bindings(swapchain);
2924 context_set_render_offscreen(context, TRUE);
2927 GLenum context_get_offscreen_gl_buffer(const struct wined3d_context *context)
2929 switch (wined3d_settings.offscreen_rendering_mode)
2931 case ORM_FBO:
2932 return GL_COLOR_ATTACHMENT0;
2934 case ORM_BACKBUFFER:
2935 return context->aux_buffers > 0 ? GL_AUX0 : GL_BACK;
2937 default:
2938 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
2939 return GL_BACK;
2943 static DWORD context_generate_rt_mask_no_fbo(const struct wined3d_context *context, struct wined3d_texture *rt)
2945 if (!rt || rt->resource.format->id == WINED3DFMT_NULL)
2946 return 0;
2947 else if (rt->swapchain)
2948 return context_generate_rt_mask_from_resource(&rt->resource);
2949 else
2950 return context_generate_rt_mask(context_get_offscreen_gl_buffer(context));
2953 /* Context activation is done by the caller. */
2954 void context_apply_blit_state(struct wined3d_context *context, const struct wined3d_device *device)
2956 struct wined3d_texture *rt = context->current_rt.texture;
2957 struct wined3d_surface *surface;
2958 DWORD rt_mask, *cur_mask;
2960 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2962 if (context->render_offscreen)
2964 wined3d_texture_load(rt, context, FALSE);
2966 surface = rt->sub_resources[context->current_rt.sub_resource_idx].u.surface;
2967 context_apply_fbo_state_blit(context, GL_FRAMEBUFFER, surface, NULL, rt->resource.draw_binding);
2968 if (rt->resource.format->id != WINED3DFMT_NULL)
2969 rt_mask = 1;
2970 else
2971 rt_mask = 0;
2973 else
2975 context->current_fbo = NULL;
2976 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
2977 rt_mask = context_generate_rt_mask_from_resource(&rt->resource);
2980 else
2982 rt_mask = context_generate_rt_mask_no_fbo(context, rt);
2985 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2987 if (rt_mask != *cur_mask)
2989 context_apply_draw_buffers(context, rt_mask);
2990 *cur_mask = rt_mask;
2993 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2995 context_check_fbo_status(context, GL_FRAMEBUFFER);
2998 SetupForBlit(device, context);
2999 context_invalidate_state(context, STATE_FRAMEBUFFER);
3002 static BOOL have_framebuffer_attachment(unsigned int rt_count, struct wined3d_rendertarget_view * const *rts,
3003 const struct wined3d_rendertarget_view *ds)
3005 unsigned int i;
3007 if (ds)
3008 return TRUE;
3010 for (i = 0; i < rt_count; ++i)
3012 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3013 return TRUE;
3016 return FALSE;
3019 /* Context activation is done by the caller. */
3020 BOOL context_apply_clear_state(struct wined3d_context *context, const struct wined3d_state *state,
3021 UINT rt_count, const struct wined3d_fb_state *fb)
3023 struct wined3d_rendertarget_view * const *rts = fb->render_targets;
3024 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
3025 const struct wined3d_gl_info *gl_info = context->gl_info;
3026 DWORD rt_mask = 0, *cur_mask;
3027 unsigned int i;
3029 if (isStateDirty(context, STATE_FRAMEBUFFER) || fb != state->fb
3030 || rt_count != gl_info->limits.buffers)
3032 if (!have_framebuffer_attachment(rt_count, rts, dsv))
3034 WARN("Invalid render target config, need at least one attachment.\n");
3035 return FALSE;
3038 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3040 context_validate_onscreen_formats(context, dsv);
3042 if (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource))
3044 memset(context->blit_targets, 0, sizeof(context->blit_targets));
3045 for (i = 0; i < rt_count; ++i)
3047 if (rts[i])
3049 context->blit_targets[i].gl_view = rts[i]->gl_view;
3050 context->blit_targets[i].resource = rts[i]->resource;
3051 context->blit_targets[i].sub_resource_idx = rts[i]->sub_resource_idx;
3052 context->blit_targets[i].layer_count = rts[i]->layer_count;
3054 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3055 rt_mask |= (1u << i);
3057 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets,
3058 wined3d_rendertarget_view_get_surface(dsv),
3059 rt_count ? rts[0]->resource->draw_binding : 0,
3060 dsv ? dsv->resource->draw_binding : 0);
3062 else
3064 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL,
3065 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3066 rt_mask = context_generate_rt_mask_from_resource(rts[0]->resource);
3069 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3070 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3071 * state management allows this */
3072 context_invalidate_state(context, STATE_FRAMEBUFFER);
3074 else
3076 rt_mask = context_generate_rt_mask_no_fbo(context,
3077 rt_count ? wined3d_rendertarget_view_get_surface(rts[0])->container : NULL);
3080 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3081 && (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource)))
3083 for (i = 0; i < rt_count; ++i)
3085 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3086 rt_mask |= (1u << i);
3089 else
3091 rt_mask = context_generate_rt_mask_no_fbo(context,
3092 rt_count ? wined3d_rendertarget_view_get_surface(rts[0])->container : NULL);
3095 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
3097 if (rt_mask != *cur_mask)
3099 context_apply_draw_buffers(context, rt_mask);
3100 *cur_mask = rt_mask;
3101 context_invalidate_state(context, STATE_FRAMEBUFFER);
3104 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3106 context_check_fbo_status(context, GL_FRAMEBUFFER);
3109 context->last_was_blit = FALSE;
3111 /* Blending and clearing should be orthogonal, but tests on the nvidia
3112 * driver show that disabling blending when clearing improves the clearing
3113 * performance incredibly. */
3114 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3115 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
3116 if (rt_count && gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3118 if (needs_srgb_write(context, state, fb))
3119 gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
3120 else
3121 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3122 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3124 checkGLcall("setting up state for clear");
3126 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
3127 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
3128 context_invalidate_state(context, STATE_SCISSORRECT);
3130 return TRUE;
3133 static DWORD find_draw_buffers_mask(const struct wined3d_context *context, const struct wined3d_state *state)
3135 struct wined3d_rendertarget_view * const *rts = state->fb->render_targets;
3136 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3137 DWORD rt_mask, mask;
3138 unsigned int i;
3140 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3141 return context_generate_rt_mask_no_fbo(context, wined3d_rendertarget_view_get_surface(rts[0])->container);
3142 else if (!context->render_offscreen)
3143 return context_generate_rt_mask_from_resource(rts[0]->resource);
3145 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
3146 rt_mask &= context->d3d_info->valid_rt_mask;
3148 mask = rt_mask;
3149 while (mask)
3151 i = wined3d_bit_scan(&mask);
3152 if (!rts[i] || rts[i]->format->id == WINED3DFMT_NULL)
3153 rt_mask &= ~(1u << i);
3156 return rt_mask;
3159 /* Context activation is done by the caller. */
3160 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3162 DWORD rt_mask = find_draw_buffers_mask(context, state);
3163 const struct wined3d_fb_state *fb = state->fb;
3164 DWORD color_location = 0;
3165 DWORD *cur_mask;
3167 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3169 if (!context->render_offscreen)
3171 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL,
3172 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3174 else
3176 unsigned int i;
3178 memset(context->blit_targets, 0, sizeof(context->blit_targets));
3179 for (i = 0; i < context->gl_info->limits.buffers; ++i)
3181 if (!fb->render_targets[i])
3182 continue;
3184 context->blit_targets[i].gl_view = fb->render_targets[i]->gl_view;
3185 context->blit_targets[i].resource = fb->render_targets[i]->resource;
3186 context->blit_targets[i].sub_resource_idx = fb->render_targets[i]->sub_resource_idx;
3187 context->blit_targets[i].layer_count = fb->render_targets[i]->layer_count;
3189 if (!color_location)
3190 color_location = fb->render_targets[i]->resource->draw_binding;
3192 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets,
3193 wined3d_rendertarget_view_get_surface(fb->depth_stencil),
3194 color_location, fb->depth_stencil ? fb->depth_stencil->resource->draw_binding : 0);
3198 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
3199 if (rt_mask != *cur_mask)
3201 context_apply_draw_buffers(context, rt_mask);
3202 *cur_mask = rt_mask;
3204 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
3207 static void context_map_stage(struct wined3d_context *context, DWORD stage, DWORD unit)
3209 DWORD i = context->rev_tex_unit_map[unit];
3210 DWORD j = context->tex_unit_map[stage];
3212 TRACE("Mapping stage %u to unit %u.\n", stage, unit);
3213 context->tex_unit_map[stage] = unit;
3214 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
3215 context->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
3217 context->rev_tex_unit_map[unit] = stage;
3218 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
3219 context->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
3222 static void context_invalidate_texture_stage(struct wined3d_context *context, DWORD stage)
3224 DWORD i;
3226 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
3227 context_invalidate_state(context, STATE_TEXTURESTAGE(stage, i));
3230 static void context_update_fixed_function_usage_map(struct wined3d_context *context,
3231 const struct wined3d_state *state)
3233 UINT i, start, end;
3235 context->fixed_function_usage_map = 0;
3236 for (i = 0; i < MAX_TEXTURES; ++i)
3238 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
3239 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
3240 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
3241 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
3242 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
3243 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
3244 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
3245 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
3247 /* Not used, and disable higher stages. */
3248 if (color_op == WINED3D_TOP_DISABLE)
3249 break;
3251 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
3252 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
3253 || ((color_arg3 == WINED3DTA_TEXTURE)
3254 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
3255 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
3256 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
3257 || ((alpha_arg3 == WINED3DTA_TEXTURE)
3258 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
3259 context->fixed_function_usage_map |= (1u << i);
3261 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
3262 && i < MAX_TEXTURES - 1)
3263 context->fixed_function_usage_map |= (1u << (i + 1));
3266 if (i < context->lowest_disabled_stage)
3268 start = i;
3269 end = context->lowest_disabled_stage;
3271 else
3273 start = context->lowest_disabled_stage;
3274 end = i;
3277 context->lowest_disabled_stage = i;
3278 for (i = start + 1; i < end; ++i)
3280 context_invalidate_state(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3284 static void context_map_fixed_function_samplers(struct wined3d_context *context,
3285 const struct wined3d_state *state)
3287 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3288 unsigned int i, tex;
3289 WORD ffu_map;
3291 ffu_map = context->fixed_function_usage_map;
3293 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
3294 || context->lowest_disabled_stage <= d3d_info->limits.ffp_textures)
3296 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3298 if (!(ffu_map & 1))
3299 continue;
3301 if (context->tex_unit_map[i] != i)
3303 context_map_stage(context, i, i);
3304 context_invalidate_state(context, STATE_SAMPLER(i));
3305 context_invalidate_texture_stage(context, i);
3308 return;
3311 /* Now work out the mapping */
3312 tex = 0;
3313 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3315 if (!(ffu_map & 1))
3316 continue;
3318 if (context->tex_unit_map[i] != tex)
3320 context_map_stage(context, i, tex);
3321 context_invalidate_state(context, STATE_SAMPLER(i));
3322 context_invalidate_texture_stage(context, i);
3325 ++tex;
3329 static void context_map_psamplers(struct wined3d_context *context, const struct wined3d_state *state)
3331 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3332 const struct wined3d_shader_resource_info *resource_info =
3333 state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3334 unsigned int i;
3336 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3338 if (resource_info[i].type && context->tex_unit_map[i] != i)
3340 context_map_stage(context, i, i);
3341 context_invalidate_state(context, STATE_SAMPLER(i));
3342 if (i < d3d_info->limits.ffp_blend_stages)
3343 context_invalidate_texture_stage(context, i);
3348 static BOOL context_unit_free_for_vs(const struct wined3d_context *context,
3349 const struct wined3d_shader_resource_info *ps_resource_info, DWORD unit)
3351 DWORD current_mapping = context->rev_tex_unit_map[unit];
3353 /* Not currently used */
3354 if (current_mapping == WINED3D_UNMAPPED_STAGE)
3355 return TRUE;
3357 if (current_mapping < MAX_FRAGMENT_SAMPLERS)
3359 /* Used by a fragment sampler */
3361 if (!ps_resource_info)
3363 /* No pixel shader, check fixed function */
3364 return current_mapping >= MAX_TEXTURES || !(context->fixed_function_usage_map & (1u << current_mapping));
3367 /* Pixel shader, check the shader's sampler map */
3368 return !ps_resource_info[current_mapping].type;
3371 return TRUE;
3374 static void context_map_vsamplers(struct wined3d_context *context, BOOL ps, const struct wined3d_state *state)
3376 const struct wined3d_shader_resource_info *vs_resource_info =
3377 state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info;
3378 const struct wined3d_shader_resource_info *ps_resource_info = NULL;
3379 const struct wined3d_gl_info *gl_info = context->gl_info;
3380 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.graphics_samplers) - 1;
3381 int i;
3383 /* Note that we only care if a resource is used or not, not the
3384 * resource's specific type. Otherwise we'd need to call
3385 * shader_update_samplers() here for 1.x pixelshaders. */
3386 if (ps)
3387 ps_resource_info = state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3389 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
3391 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
3392 if (vs_resource_info[i].type)
3394 while (start >= 0)
3396 if (context_unit_free_for_vs(context, ps_resource_info, start))
3398 if (context->tex_unit_map[vsampler_idx] != start)
3400 context_map_stage(context, vsampler_idx, start);
3401 context_invalidate_state(context, STATE_SAMPLER(vsampler_idx));
3404 --start;
3405 break;
3408 --start;
3410 if (context->tex_unit_map[vsampler_idx] == WINED3D_UNMAPPED_STAGE)
3411 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i);
3416 static void context_update_tex_unit_map(struct wined3d_context *context, const struct wined3d_state *state)
3418 const struct wined3d_gl_info *gl_info = context->gl_info;
3419 BOOL vs = use_vs(state);
3420 BOOL ps = use_ps(state);
3422 if (!ps)
3423 context_update_fixed_function_usage_map(context, state);
3425 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3426 * need a 1:1 map at the moment.
3427 * When the mapping of a stage is changed, sampler and ALL texture stage
3428 * states have to be reset. */
3430 if (gl_info->limits.graphics_samplers >= MAX_COMBINED_SAMPLERS)
3431 return;
3433 if (ps)
3434 context_map_psamplers(context, state);
3435 else
3436 context_map_fixed_function_samplers(context, state);
3438 if (vs)
3439 context_map_vsamplers(context, ps, state);
3442 /* Context activation is done by the caller. */
3443 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3445 DWORD rt_mask, *cur_mask;
3447 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
3449 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
3450 rt_mask = find_draw_buffers_mask(context, state);
3451 if (rt_mask != *cur_mask)
3453 context_apply_draw_buffers(context, rt_mask);
3454 *cur_mask = rt_mask;
3458 static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
3460 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
3461 *regnum = WINED3D_FFP_POSITION;
3462 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
3463 *regnum = WINED3D_FFP_BLENDWEIGHT;
3464 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
3465 *regnum = WINED3D_FFP_BLENDINDICES;
3466 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
3467 *regnum = WINED3D_FFP_NORMAL;
3468 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
3469 *regnum = WINED3D_FFP_PSIZE;
3470 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
3471 *regnum = WINED3D_FFP_DIFFUSE;
3472 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
3473 *regnum = WINED3D_FFP_SPECULAR;
3474 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
3475 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
3476 else
3478 WARN("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage), usage_idx);
3479 *regnum = ~0u;
3480 return FALSE;
3483 return TRUE;
3486 /* Context activation is done by the caller. */
3487 void wined3d_stream_info_from_declaration(struct wined3d_stream_info *stream_info,
3488 const struct wined3d_state *state, const struct wined3d_gl_info *gl_info,
3489 const struct wined3d_d3d_info *d3d_info)
3491 /* We need to deal with frequency data! */
3492 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
3493 BOOL generic_attributes = d3d_info->ffp_generic_attributes;
3494 BOOL use_vshader = use_vs(state);
3495 unsigned int i;
3497 stream_info->use_map = 0;
3498 stream_info->swizzle_map = 0;
3499 stream_info->position_transformed = 0;
3501 if (!declaration)
3502 return;
3504 stream_info->position_transformed = declaration->position_transformed;
3506 /* Translate the declaration into strided data. */
3507 for (i = 0; i < declaration->element_count; ++i)
3509 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
3510 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
3511 BOOL stride_used;
3512 unsigned int idx;
3514 TRACE("%p Element %p (%u of %u).\n", declaration->elements,
3515 element, i + 1, declaration->element_count);
3517 if (!stream->buffer)
3518 continue;
3520 TRACE("offset %u input_slot %u usage_idx %d.\n", element->offset, element->input_slot, element->usage_idx);
3522 if (use_vshader)
3524 if (element->output_slot == WINED3D_OUTPUT_SLOT_UNUSED)
3526 stride_used = FALSE;
3528 else if (element->output_slot == WINED3D_OUTPUT_SLOT_SEMANTIC)
3530 /* TODO: Assuming vertexdeclarations are usually used with the
3531 * same or a similar shader, it might be worth it to store the
3532 * last used output slot and try that one first. */
3533 stride_used = vshader_get_input(state->shader[WINED3D_SHADER_TYPE_VERTEX],
3534 element->usage, element->usage_idx, &idx);
3536 else
3538 idx = element->output_slot;
3539 stride_used = TRUE;
3542 else
3544 if (!generic_attributes && !element->ffp_valid)
3546 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3547 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
3548 stride_used = FALSE;
3550 else
3552 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
3556 if (stride_used)
3558 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3559 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3560 use_vshader ? "shader": "fixed function", idx,
3561 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
3562 element->offset, stream->stride, debug_d3dformat(element->format->id),
3563 debug_d3dinput_classification(element->input_slot_class), element->instance_data_step_rate);
3565 stream_info->elements[idx].format = element->format;
3566 stream_info->elements[idx].data.buffer_object = 0;
3567 stream_info->elements[idx].data.addr = (BYTE *)NULL + stream->offset + element->offset;
3568 stream_info->elements[idx].stride = stream->stride;
3569 stream_info->elements[idx].stream_idx = element->input_slot;
3570 if (stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
3572 stream_info->elements[idx].divisor = 1;
3574 else if (element->input_slot_class == WINED3D_INPUT_PER_INSTANCE_DATA)
3576 stream_info->elements[idx].divisor = element->instance_data_step_rate;
3577 if (!element->instance_data_step_rate)
3578 FIXME("Instance step rate 0 not implemented.\n");
3580 else
3582 stream_info->elements[idx].divisor = 0;
3585 if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
3586 && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
3588 stream_info->swizzle_map |= 1u << idx;
3590 stream_info->use_map |= 1u << idx;
3595 /* Context activation is done by the caller. */
3596 static void context_update_stream_info(struct wined3d_context *context, const struct wined3d_state *state)
3598 struct wined3d_stream_info *stream_info = &context->stream_info;
3599 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3600 const struct wined3d_gl_info *gl_info = context->gl_info;
3601 DWORD prev_all_vbo = stream_info->all_vbo;
3602 unsigned int i;
3603 WORD map;
3605 wined3d_stream_info_from_declaration(stream_info, state, gl_info, d3d_info);
3607 stream_info->all_vbo = 1;
3608 context->buffer_fence_count = 0;
3609 for (i = 0, map = stream_info->use_map; map; map >>= 1, ++i)
3611 struct wined3d_stream_info_element *element;
3612 struct wined3d_bo_address data;
3613 struct wined3d_buffer *buffer;
3615 if (!(map & 1))
3616 continue;
3618 element = &stream_info->elements[i];
3619 buffer = state->streams[element->stream_idx].buffer;
3621 /* We can't use VBOs if the base vertex index is negative. OpenGL
3622 * doesn't accept negative offsets (or rather offsets bigger than the
3623 * VBO, because the pointer is unsigned), so use system memory
3624 * sources. In most sane cases the pointer - offset will still be > 0,
3625 * otherwise it will wrap around to some big value. Hope that with the
3626 * indices the driver wraps it back internally. If not,
3627 * draw_primitive_immediate_mode() is needed, including a vertex buffer
3628 * path. */
3629 if (state->load_base_vertex_index < 0)
3631 WARN_(d3d_perf)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3632 state->load_base_vertex_index);
3633 element->data.buffer_object = 0;
3634 element->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3635 if ((UINT_PTR)element->data.addr < -state->load_base_vertex_index * element->stride)
3636 FIXME("System memory vertex data load offset is negative!\n");
3638 else
3640 wined3d_buffer_load(buffer, context, state);
3641 wined3d_buffer_get_memory(buffer, &data, buffer->locations);
3642 element->data.buffer_object = data.buffer_object;
3643 element->data.addr += (ULONG_PTR)data.addr;
3646 if (!element->data.buffer_object)
3647 stream_info->all_vbo = 0;
3649 if (buffer->fence)
3650 context->buffer_fences[context->buffer_fence_count++] = buffer->fence;
3652 TRACE("Load array %u {%#x:%p}.\n", i, element->data.buffer_object, element->data.addr);
3655 if (prev_all_vbo != stream_info->all_vbo)
3656 context_invalidate_state(context, STATE_INDEXBUFFER);
3658 context->use_immediate_mode_draw = FALSE;
3660 if (stream_info->all_vbo)
3661 return;
3663 if (use_vs(state))
3665 if (state->vertex_declaration->half_float_conv_needed)
3667 TRACE("Using immediate mode draw with vertex shaders for FLOAT16 conversion.\n");
3668 context->use_immediate_mode_draw = TRUE;
3671 else
3673 WORD slow_mask = -!d3d_info->ffp_generic_attributes & (1u << WINED3D_FFP_PSIZE);
3674 slow_mask |= -(!gl_info->supported[ARB_VERTEX_ARRAY_BGRA] && !d3d_info->ffp_generic_attributes)
3675 & ((1u << WINED3D_FFP_DIFFUSE) | (1u << WINED3D_FFP_SPECULAR) | (1u << WINED3D_FFP_BLENDWEIGHT));
3677 if ((stream_info->position_transformed && !d3d_info->xyzrhw)
3678 || (stream_info->use_map & slow_mask))
3679 context->use_immediate_mode_draw = TRUE;
3683 /* Context activation is done by the caller. */
3684 static void context_preload_texture(struct wined3d_context *context,
3685 const struct wined3d_state *state, unsigned int idx)
3687 struct wined3d_texture *texture;
3689 if (!(texture = state->textures[idx]))
3690 return;
3692 wined3d_texture_load(texture, context, state->sampler_states[idx][WINED3D_SAMP_SRGB_TEXTURE]);
3695 /* Context activation is done by the caller. */
3696 static void context_preload_textures(struct wined3d_context *context, const struct wined3d_state *state)
3698 unsigned int i;
3700 if (use_vs(state))
3702 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
3704 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info[i].type)
3705 context_preload_texture(context, state, MAX_FRAGMENT_SAMPLERS + i);
3709 if (use_ps(state))
3711 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3713 if (state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info[i].type)
3714 context_preload_texture(context, state, i);
3717 else
3719 WORD ffu_map = context->fixed_function_usage_map;
3721 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3723 if (ffu_map & 1)
3724 context_preload_texture(context, state, i);
3729 static void context_load_shader_resources(struct wined3d_context *context, const struct wined3d_state *state,
3730 unsigned int shader_mask)
3732 struct wined3d_shader_sampler_map_entry *entry;
3733 struct wined3d_shader_resource_view *view;
3734 struct wined3d_shader *shader;
3735 unsigned int i, j;
3737 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
3739 if (!(shader_mask & (1u << i)))
3740 continue;
3742 if (!(shader = state->shader[i]))
3743 continue;
3745 for (j = 0; j < WINED3D_MAX_CBS; ++j)
3747 if (state->cb[i][j])
3748 wined3d_buffer_load(state->cb[i][j], context, state);
3751 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
3753 entry = &shader->reg_maps.sampler_map.entries[j];
3755 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
3756 continue;
3758 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3759 wined3d_buffer_load(buffer_from_resource(view->resource), context, state);
3760 else
3761 wined3d_texture_load(texture_from_resource(view->resource), context, FALSE);
3766 static void context_bind_shader_resources(struct wined3d_context *context,
3767 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
3769 unsigned int bind_idx, shader_sampler_count, base, count, i;
3770 const struct wined3d_device *device = context->device;
3771 struct wined3d_shader_sampler_map_entry *entry;
3772 struct wined3d_shader_resource_view *view;
3773 const struct wined3d_shader *shader;
3774 struct wined3d_sampler *sampler;
3775 const DWORD *tex_unit_map;
3777 if (!(shader = state->shader[shader_type]))
3778 return;
3780 tex_unit_map = context_get_tex_unit_mapping(context,
3781 &shader->reg_maps.shader_version, &base, &count);
3783 shader_sampler_count = shader->reg_maps.sampler_map.count;
3784 if (shader_sampler_count > count)
3785 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3786 shader, shader_sampler_count, count);
3787 count = min(shader_sampler_count, count);
3789 for (i = 0; i < count; ++i)
3791 entry = &shader->reg_maps.sampler_map.entries[i];
3792 bind_idx = base + entry->bind_idx;
3793 if (tex_unit_map)
3794 bind_idx = tex_unit_map[bind_idx];
3796 if (!(view = state->shader_resource_view[shader_type][entry->resource_idx]))
3798 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
3799 continue;
3802 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
3803 sampler = device->default_sampler;
3804 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
3805 sampler = device->null_sampler;
3806 wined3d_shader_resource_view_bind(view, bind_idx, sampler, context);
3810 static void context_load_unordered_access_resources(struct wined3d_context *context,
3811 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3813 struct wined3d_unordered_access_view *view;
3814 struct wined3d_texture *texture;
3815 struct wined3d_buffer *buffer;
3816 unsigned int i;
3818 context->uses_uavs = 0;
3820 if (!shader)
3821 return;
3823 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3825 if (!(view = views[i]))
3826 continue;
3828 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3830 buffer = buffer_from_resource(view->resource);
3831 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);
3832 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
3834 else
3836 texture = texture_from_resource(view->resource);
3837 wined3d_texture_load(texture, context, FALSE);
3838 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_TEXTURE_RGB);
3841 context->uses_uavs = 1;
3845 static void context_bind_unordered_access_views(struct wined3d_context *context,
3846 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3848 const struct wined3d_gl_info *gl_info = context->gl_info;
3849 struct wined3d_unordered_access_view *view;
3850 GLuint texture_name;
3851 unsigned int i;
3852 GLint level;
3854 if (!shader)
3855 return;
3857 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3859 if (!(view = views[i]))
3861 if (shader->reg_maps.uav_resource_info[i].type)
3862 WARN("No unordered access view bound at index %u.\n", i);
3863 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3864 continue;
3867 if (view->gl_view.name)
3869 texture_name = view->gl_view.name;
3870 level = 0;
3872 else if (view->resource->type != WINED3D_RTYPE_BUFFER)
3874 struct wined3d_texture *texture = texture_from_resource(view->resource);
3875 texture_name = wined3d_texture_get_texture_name(texture, context, FALSE);
3876 level = view->desc.u.texture.level_idx;
3878 else
3880 FIXME("Unsupported buffer unordered access view.\n");
3881 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3882 continue;
3885 GL_EXTCALL(glBindImageTexture(i, texture_name, level, GL_TRUE, 0, GL_READ_WRITE,
3886 view->format->glInternal));
3888 if (view->counter_bo)
3889 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, i, view->counter_bo));
3891 checkGLcall("Bind unordered access views");
3894 static void context_load_stream_output_buffers(struct wined3d_context *context,
3895 const struct wined3d_state *state)
3897 unsigned int i;
3899 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3901 struct wined3d_buffer *buffer;
3902 if (!(buffer = state->stream_output[i].buffer))
3903 continue;
3905 wined3d_buffer_load(buffer, context, state);
3906 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
3910 /* Context activation is done by the caller. */
3911 static BOOL context_apply_draw_state(struct wined3d_context *context,
3912 const struct wined3d_device *device, const struct wined3d_state *state)
3914 const struct StateEntry *state_table = context->state_table;
3915 const struct wined3d_gl_info *gl_info = context->gl_info;
3916 const struct wined3d_fb_state *fb = state->fb;
3917 unsigned int i;
3918 WORD map;
3920 if (!have_framebuffer_attachment(gl_info->limits.buffers, fb->render_targets, fb->depth_stencil))
3922 if (!gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
3924 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
3925 return FALSE;
3928 context_set_render_offscreen(context, TRUE);
3931 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && isStateDirty(context, STATE_FRAMEBUFFER))
3933 context_validate_onscreen_formats(context, fb->depth_stencil);
3936 /* Preload resources before FBO setup. Texture preload in particular may
3937 * result in changes to the current FBO, due to using e.g. FBO blits for
3938 * updating a resource location. */
3939 context_update_tex_unit_map(context, state);
3940 context_preload_textures(context, state);
3941 context_load_shader_resources(context, state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
3942 context_load_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_PIXEL],
3943 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3944 context_load_stream_output_buffers(context, state);
3945 /* TODO: Right now the dependency on the vertex shader is necessary
3946 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3947 * the current VS but maybe it's possible to relax the coupling in some
3948 * situations at least. */
3949 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC)
3950 || isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
3952 context_update_stream_info(context, state);
3954 else
3956 for (i = 0, map = context->stream_info.use_map; map; map >>= 1, ++i)
3958 if (map & 1)
3959 wined3d_buffer_load(state->streams[context->stream_info.elements[i].stream_idx].buffer,
3960 context, state);
3962 /* Loading the buffers above may have invalidated the stream info. */
3963 if (isStateDirty(context, STATE_STREAMSRC))
3964 context_update_stream_info(context, state);
3966 if (state->index_buffer)
3968 if (context->stream_info.all_vbo)
3969 wined3d_buffer_load(state->index_buffer, context, state);
3970 else
3971 wined3d_buffer_load_sysmem(state->index_buffer, context);
3974 for (i = 0; i < context->numDirtyEntries; ++i)
3976 DWORD rep = context->dirtyArray[i];
3977 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
3978 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
3979 context->isStateDirty[idx] &= ~(1u << shift);
3980 state_table[rep].apply(context, state, rep);
3983 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
3985 device->shader_backend->shader_select(device->shader_priv, context, state);
3986 context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3989 if (context->constant_update_mask)
3991 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
3992 context->constant_update_mask = 0;
3995 if (context->update_shader_resource_bindings)
3997 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
3998 context_bind_shader_resources(context, state, i);
3999 context->update_shader_resource_bindings = 0;
4000 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4001 context->update_compute_shader_resource_bindings = 1;
4004 if (context->update_unordered_access_view_bindings)
4006 context_bind_unordered_access_views(context,
4007 state->shader[WINED3D_SHADER_TYPE_PIXEL],
4008 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
4009 context->update_unordered_access_view_bindings = 0;
4010 context->update_compute_unordered_access_view_bindings = 1;
4013 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4015 context_check_fbo_status(context, GL_FRAMEBUFFER);
4018 context->numDirtyEntries = 0; /* This makes the whole list clean */
4019 context->last_was_blit = FALSE;
4021 return TRUE;
4024 static void context_apply_compute_state(struct wined3d_context *context,
4025 const struct wined3d_device *device, const struct wined3d_state *state)
4027 const struct StateEntry *state_table = context->state_table;
4028 const struct wined3d_gl_info *gl_info = context->gl_info;
4029 unsigned int state_id, i;
4031 context_load_shader_resources(context, state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
4032 context_load_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4033 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4035 for (i = 0, state_id = STATE_COMPUTE_OFFSET; i < ARRAY_SIZE(context->dirty_compute_states); ++i)
4037 unsigned int dirty_mask = context->dirty_compute_states[i];
4038 while (dirty_mask)
4040 unsigned int current_state_id = state_id + wined3d_bit_scan(&dirty_mask);
4041 state_table[current_state_id].apply(context, state, current_state_id);
4043 state_id += sizeof(*context->dirty_compute_states) * CHAR_BIT;
4045 memset(context->dirty_compute_states, 0, sizeof(*context->dirty_compute_states));
4047 if (context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
4049 device->shader_backend->shader_select_compute(device->shader_priv, context, state);
4050 context->shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
4053 if (context->update_compute_shader_resource_bindings)
4055 context_bind_shader_resources(context, state, WINED3D_SHADER_TYPE_COMPUTE);
4056 context->update_compute_shader_resource_bindings = 0;
4057 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4058 context->update_shader_resource_bindings = 1;
4061 if (context->update_compute_unordered_access_view_bindings)
4063 context_bind_unordered_access_views(context,
4064 state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4065 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4066 context->update_compute_unordered_access_view_bindings = 0;
4067 context->update_unordered_access_view_bindings = 1;
4070 /* Updates to currently bound render targets aren't necessarily coherent
4071 * between the graphics and compute pipelines. Unbind any currently bound
4072 * FBO here to ensure preceding updates to its attachments by the graphics
4073 * pipeline are visible to the compute pipeline.
4075 * Without this, the bloom effect in Nier:Automata is too bright on the
4076 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4077 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
4078 context_invalidate_state(context, STATE_FRAMEBUFFER);
4080 context->last_was_blit = FALSE;
4083 static BOOL use_transform_feedback(const struct wined3d_state *state)
4085 const struct wined3d_shader *shader;
4086 if (!(shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
4087 return FALSE;
4088 return shader->u.gs.so_desc.element_count;
4091 void context_end_transform_feedback(struct wined3d_context *context)
4093 const struct wined3d_gl_info *gl_info = context->gl_info;
4094 if (context->transform_feedback_active)
4096 GL_EXTCALL(glEndTransformFeedback());
4097 checkGLcall("glEndTransformFeedback");
4098 context->transform_feedback_active = 0;
4099 context->transform_feedback_paused = 0;
4103 static void context_pause_transform_feedback(struct wined3d_context *context, BOOL force)
4105 const struct wined3d_gl_info *gl_info = context->gl_info;
4107 if (!context->transform_feedback_active || context->transform_feedback_paused)
4108 return;
4110 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK2])
4112 GL_EXTCALL(glPauseTransformFeedback());
4113 checkGLcall("glPauseTransformFeedback");
4114 context->transform_feedback_paused = 1;
4115 return;
4118 WARN("Cannot pause transform feedback operations.\n");
4120 if (force)
4121 context_end_transform_feedback(context);
4124 static void context_setup_target(struct wined3d_context *context,
4125 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4127 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
4129 render_offscreen = wined3d_resource_is_offscreen(&texture->resource);
4130 if (context->current_rt.texture == texture
4131 && context->current_rt.sub_resource_idx == sub_resource_idx
4132 && render_offscreen == old_render_offscreen)
4133 return;
4135 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4136 * the alpha blend state changes with different render target formats. */
4137 if (!context->current_rt.texture)
4139 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
4141 else
4143 const struct wined3d_format *old = context->current_rt.texture->resource.format;
4144 const struct wined3d_format *new = texture->resource.format;
4146 if (old->id != new->id)
4148 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4149 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
4150 || !(texture->resource.format_flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
4151 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
4153 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
4154 if ((context->current_rt.texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE)
4155 != (texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE))
4156 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
4159 /* When switching away from an offscreen render target, and we're not
4160 * using FBOs, we have to read the drawable into the texture. This is
4161 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4162 * There are some things that need care though. PreLoad needs a GL context,
4163 * and FindContext is called before the context is activated. It also
4164 * has to be called with the old rendertarget active, otherwise a
4165 * wrong drawable is read. */
4166 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4167 && old_render_offscreen && (context->current_rt.texture != texture
4168 || context->current_rt.sub_resource_idx != sub_resource_idx))
4170 unsigned int prev_sub_resource_idx = context->current_rt.sub_resource_idx;
4171 struct wined3d_texture *prev_texture = context->current_rt.texture;
4173 /* Read the back buffer of the old drawable into the destination texture. */
4174 if (prev_texture->texture_srgb.name)
4175 wined3d_texture_load(prev_texture, context, TRUE);
4176 wined3d_texture_load(prev_texture, context, FALSE);
4177 wined3d_texture_invalidate_location(prev_texture, prev_sub_resource_idx, WINED3D_LOCATION_DRAWABLE);
4181 context->current_rt.texture = texture;
4182 context->current_rt.sub_resource_idx = sub_resource_idx;
4183 context_set_render_offscreen(context, render_offscreen);
4186 static void context_activate(struct wined3d_context *context,
4187 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4189 context_enter(context);
4190 context_update_window(context);
4191 context_setup_target(context, texture, sub_resource_idx);
4192 if (!context->valid)
4193 return;
4195 if (context != context_get_current())
4197 if (!context_set_current(context))
4198 ERR("Failed to activate the new context.\n");
4200 else if (context->needs_set)
4202 context_set_gl_context(context);
4206 struct wined3d_context *context_acquire(const struct wined3d_device *device,
4207 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4209 struct wined3d_context *current_context = context_get_current();
4210 struct wined3d_context *context;
4211 BOOL swapchain_texture;
4213 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device, texture, sub_resource_idx);
4215 wined3d_from_cs(device->cs);
4217 if (current_context && current_context->destroyed)
4218 current_context = NULL;
4220 swapchain_texture = texture && texture->swapchain;
4221 if (!texture)
4223 if (current_context
4224 && current_context->current_rt.texture
4225 && current_context->device == device)
4227 texture = current_context->current_rt.texture;
4228 sub_resource_idx = current_context->current_rt.sub_resource_idx;
4230 else
4232 struct wined3d_swapchain *swapchain = device->swapchains[0];
4234 if (swapchain->back_buffers)
4235 texture = swapchain->back_buffers[0];
4236 else
4237 texture = swapchain->front_buffer;
4238 sub_resource_idx = 0;
4242 if (current_context && current_context->current_rt.texture == texture)
4244 context = current_context;
4246 else if (swapchain_texture)
4248 TRACE("Rendering onscreen.\n");
4250 context = swapchain_get_context(texture->swapchain);
4252 else
4254 TRACE("Rendering offscreen.\n");
4256 /* Stay with the current context if possible. Otherwise use the
4257 * context for the primary swapchain. */
4258 if (current_context && current_context->device == device)
4259 context = current_context;
4260 else
4261 context = swapchain_get_context(device->swapchains[0]);
4264 context_activate(context, texture, sub_resource_idx);
4266 return context;
4269 struct wined3d_context *context_reacquire(const struct wined3d_device *device,
4270 struct wined3d_context *context)
4272 struct wined3d_context *acquired_context;
4274 wined3d_from_cs(device->cs);
4276 if (!context || context->tid != GetCurrentThreadId())
4277 return NULL;
4279 if (context->current_rt.texture)
4281 context_activate(context, context->current_rt.texture, context->current_rt.sub_resource_idx);
4282 return context;
4285 acquired_context = context_acquire(device, NULL, 0);
4286 if (acquired_context != context)
4287 ERR("Acquired context %p instead of %p.\n", acquired_context, context);
4288 return acquired_context;
4291 void dispatch_compute(struct wined3d_device *device, const struct wined3d_state *state,
4292 const struct wined3d_dispatch_parameters *parameters)
4294 const struct wined3d_gl_info *gl_info;
4295 struct wined3d_context *context;
4297 context = context_acquire(device, NULL, 0);
4298 if (!context->valid)
4300 context_release(context);
4301 WARN("Invalid context, skipping dispatch.\n");
4302 return;
4304 gl_info = context->gl_info;
4306 if (!gl_info->supported[ARB_COMPUTE_SHADER])
4308 context_release(context);
4309 FIXME("OpenGL implementation does not support compute shaders.\n");
4310 return;
4313 if (parameters->indirect)
4314 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
4316 context_apply_compute_state(context, device, state);
4318 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
4320 context_release(context);
4321 WARN("No compute shader bound, skipping dispatch.\n");
4322 return;
4325 if (parameters->indirect)
4327 const struct wined3d_indirect_dispatch_parameters *indirect = &parameters->u.indirect;
4328 struct wined3d_buffer *buffer = indirect->buffer;
4330 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, buffer->buffer_object));
4331 GL_EXTCALL(glDispatchComputeIndirect((GLintptr)indirect->offset));
4332 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0));
4334 else
4336 const struct wined3d_direct_dispatch_parameters *direct = &parameters->u.direct;
4337 GL_EXTCALL(glDispatchCompute(direct->group_count_x, direct->group_count_y, direct->group_count_z));
4339 checkGLcall("dispatch compute");
4341 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4342 checkGLcall("glMemoryBarrier");
4344 if (wined3d_settings.strict_draw_ordering)
4345 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
4347 context_release(context);
4350 /* Context activation is done by the caller. */
4351 static void draw_primitive_arrays(struct wined3d_context *context, const struct wined3d_state *state,
4352 const void *idx_data, unsigned int idx_size, int base_vertex_idx, unsigned int start_idx,
4353 unsigned int count, unsigned int start_instance, unsigned int instance_count)
4355 const struct wined3d_ffp_attrib_ops *ops = &context->d3d_info->ffp_attrib_ops;
4356 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4357 const struct wined3d_stream_info *si = &context->stream_info;
4358 unsigned int instanced_elements[ARRAY_SIZE(si->elements)];
4359 const struct wined3d_gl_info *gl_info = context->gl_info;
4360 unsigned int instanced_element_count = 0;
4361 GLenum mode = state->gl_primitive_type;
4362 const void *indices;
4363 unsigned int i, j;
4365 indices = (const char *)idx_data + idx_size * start_idx;
4367 if (!instance_count)
4369 if (!idx_size)
4371 gl_info->gl_ops.gl.p_glDrawArrays(mode, start_idx, count);
4372 checkGLcall("glDrawArrays");
4373 return;
4376 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4378 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4379 checkGLcall("glDrawElementsBaseVertex");
4380 return;
4383 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4384 checkGLcall("glDrawElements");
4385 return;
4388 if (start_instance && !(gl_info->supported[ARB_BASE_INSTANCE] && gl_info->supported[ARB_INSTANCED_ARRAYS]))
4389 FIXME("Start instance (%u) not supported.\n", start_instance);
4391 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
4393 if (!idx_size)
4395 if (gl_info->supported[ARB_BASE_INSTANCE])
4397 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode, start_idx, count, instance_count, start_instance));
4398 checkGLcall("glDrawArraysInstancedBaseInstance");
4399 return;
4402 GL_EXTCALL(glDrawArraysInstanced(mode, start_idx, count, instance_count));
4403 checkGLcall("glDrawArraysInstanced");
4404 return;
4407 if (gl_info->supported[ARB_BASE_INSTANCE])
4409 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode, count, idx_type,
4410 indices, instance_count, base_vertex_idx, start_instance));
4411 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4412 return;
4414 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4416 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode, count, idx_type,
4417 indices, instance_count, base_vertex_idx));
4418 checkGLcall("glDrawElementsInstancedBaseVertex");
4419 return;
4422 GL_EXTCALL(glDrawElementsInstanced(mode, count, idx_type, indices, instance_count));
4423 checkGLcall("glDrawElementsInstanced");
4424 return;
4427 /* Instancing emulation by mixing immediate mode and arrays. */
4429 /* This is a nasty thing. MSDN says no hardware supports this and
4430 * applications have to use software vertex processing. We don't support
4431 * this for now.
4433 * Shouldn't be too hard to support with OpenGL, in theory just call
4434 * glDrawArrays() instead of drawElements(). But the stream fequency value
4435 * has a different meaning in that situation. */
4436 if (!idx_size)
4438 FIXME("Non-indexed instanced drawing is not supported.\n");
4439 return;
4442 for (i = 0; i < ARRAY_SIZE(si->elements); ++i)
4444 if (!(si->use_map & (1u << i)))
4445 continue;
4447 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
4448 instanced_elements[instanced_element_count++] = i;
4451 for (i = 0; i < instance_count; ++i)
4453 /* Specify the instanced attributes using immediate mode calls. */
4454 for (j = 0; j < instanced_element_count; ++j)
4456 const struct wined3d_stream_info_element *element;
4457 unsigned int element_idx;
4458 const BYTE *ptr;
4460 element_idx = instanced_elements[j];
4461 element = &si->elements[element_idx];
4462 ptr = element->data.addr + element->stride * i;
4463 if (element->data.buffer_object)
4464 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(state->streams[element->stream_idx].buffer, context);
4465 ops->generic[element->format->emit_idx](element_idx, ptr);
4468 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4470 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4471 checkGLcall("glDrawElementsBaseVertex");
4473 else
4475 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4476 checkGLcall("glDrawElements");
4481 static unsigned int get_stride_idx(const void *idx_data, unsigned int idx_size,
4482 unsigned int base_vertex_idx, unsigned int start_idx, unsigned int vertex_idx)
4484 if (!idx_data)
4485 return start_idx + vertex_idx;
4486 if (idx_size == 2)
4487 return ((const WORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4488 return ((const DWORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4491 /* Context activation is done by the caller. */
4492 static void draw_primitive_immediate_mode(struct wined3d_context *context, const struct wined3d_state *state,
4493 const struct wined3d_stream_info *si, const void *idx_data, unsigned int idx_size,
4494 int base_vertex_idx, unsigned int start_idx, unsigned int vertex_count, unsigned int instance_count)
4496 const BYTE *position = NULL, *normal = NULL, *diffuse = NULL, *specular = NULL;
4497 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
4498 unsigned int coord_idx, stride_idx, texture_idx, vertex_idx;
4499 const struct wined3d_gl_info *gl_info = context->gl_info;
4500 const struct wined3d_stream_info_element *element;
4501 const BYTE *tex_coords[WINED3DDP_MAXTEXCOORD];
4502 unsigned int texture_unit, texture_stages;
4503 const struct wined3d_ffp_attrib_ops *ops;
4504 unsigned int untracked_material_count;
4505 unsigned int tex_mask = 0;
4506 BOOL specular_fog = FALSE;
4507 BOOL ps = use_ps(state);
4508 const void *ptr;
4510 static unsigned int once;
4512 if (!once++)
4513 FIXME_(d3d_perf)("Drawing using immediate mode.\n");
4514 else
4515 WARN_(d3d_perf)("Drawing using immediate mode.\n");
4517 if (!idx_size && idx_data)
4518 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4520 if (instance_count)
4521 FIXME("Instancing not implemented.\n");
4523 /* Immediate mode drawing can't make use of indices in a VBO - get the
4524 * data from the index buffer. */
4525 if (idx_size)
4526 idx_data = wined3d_buffer_load_sysmem(state->index_buffer, context) + state->index_offset;
4528 ops = &d3d_info->ffp_attrib_ops;
4530 gl_info->gl_ops.gl.p_glBegin(state->gl_primitive_type);
4532 if (use_vs(state) || d3d_info->ffp_generic_attributes)
4534 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4536 unsigned int use_map = si->use_map;
4537 unsigned int element_idx;
4539 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4540 for (element_idx = MAX_ATTRIBS - 1; use_map; use_map &= ~(1u << element_idx), --element_idx)
4542 if (!(use_map & 1u << element_idx))
4543 continue;
4545 ptr = si->elements[element_idx].data.addr + si->elements[element_idx].stride * stride_idx;
4546 ops->generic[si->elements[element_idx].format->emit_idx](element_idx, ptr);
4550 gl_info->gl_ops.gl.p_glEnd();
4551 return;
4554 if (si->use_map & (1u << WINED3D_FFP_POSITION))
4555 position = si->elements[WINED3D_FFP_POSITION].data.addr;
4557 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
4558 normal = si->elements[WINED3D_FFP_NORMAL].data.addr;
4559 else
4560 gl_info->gl_ops.gl.p_glNormal3f(0.0f, 0.0f, 0.0f);
4562 untracked_material_count = context->num_untracked_materials;
4563 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
4565 element = &si->elements[WINED3D_FFP_DIFFUSE];
4566 diffuse = element->data.addr;
4568 if (untracked_material_count && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
4569 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element->format->id));
4571 else
4573 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4576 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
4578 element = &si->elements[WINED3D_FFP_SPECULAR];
4579 specular = element->data.addr;
4581 /* Special case where the fog density is stored in the specular alpha channel. */
4582 if (state->render_states[WINED3D_RS_FOGENABLE]
4583 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
4584 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
4585 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
4587 if (gl_info->supported[EXT_FOG_COORD])
4589 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
4590 specular_fog = TRUE;
4591 else
4592 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element->format->id));
4594 else
4596 static unsigned int once;
4598 if (!once++)
4599 FIXME("Implement fog for transformed vertices in software.\n");
4603 else if (gl_info->supported[EXT_SECONDARY_COLOR])
4605 GL_EXTCALL(glSecondaryColor3fEXT)(0.0f, 0.0f, 0.0f);
4608 texture_stages = d3d_info->limits.ffp_blend_stages;
4609 for (texture_idx = 0; texture_idx < texture_stages; ++texture_idx)
4611 if (!gl_info->supported[ARB_MULTITEXTURE] && texture_idx > 0)
4613 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4614 continue;
4617 if (!ps && !state->textures[texture_idx])
4618 continue;
4620 texture_unit = context->tex_unit_map[texture_idx];
4621 if (texture_unit == WINED3D_UNMAPPED_STAGE)
4622 continue;
4624 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4625 if (coord_idx > 7)
4627 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx, texture_idx);
4628 continue;
4631 if (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))
4633 tex_coords[coord_idx] = si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].data.addr;
4634 tex_mask |= (1u << texture_idx);
4636 else
4638 TRACE("Setting default coordinates for texture %u.\n", texture_idx);
4639 if (gl_info->supported[ARB_MULTITEXTURE])
4640 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_unit, 0.0f, 0.0f, 0.0f, 1.0f));
4641 else
4642 gl_info->gl_ops.gl.p_glTexCoord4f(0.0f, 0.0f, 0.0f, 1.0f);
4646 /* Blending data and point sizes are not supported by this function. They
4647 * are not supported by the fixed function pipeline at all. A FIXME for
4648 * them is printed after decoding the vertex declaration. */
4649 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4651 unsigned int tmp_tex_mask;
4653 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4655 if (normal)
4657 ptr = normal + stride_idx * si->elements[WINED3D_FFP_NORMAL].stride;
4658 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptr);
4661 if (diffuse)
4663 ptr = diffuse + stride_idx * si->elements[WINED3D_FFP_DIFFUSE].stride;
4664 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptr);
4666 if (untracked_material_count)
4668 struct wined3d_color color;
4669 unsigned int i;
4671 wined3d_color_from_d3dcolor(&color, *(const DWORD *)ptr);
4672 for (i = 0; i < untracked_material_count; ++i)
4674 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK, context->untracked_materials[i], &color.r);
4679 if (specular)
4681 ptr = specular + stride_idx * si->elements[WINED3D_FFP_SPECULAR].stride;
4682 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptr);
4684 if (specular_fog)
4685 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD *)ptr >> 24)));
4688 tmp_tex_mask = tex_mask;
4689 for (texture_idx = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture_idx)
4691 if (!(tmp_tex_mask & 1))
4692 continue;
4694 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4695 ptr = tex_coords[coord_idx] + (stride_idx * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
4696 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
4697 GL_TEXTURE0_ARB + context->tex_unit_map[texture_idx], ptr);
4700 if (position)
4702 ptr = position + stride_idx * si->elements[WINED3D_FFP_POSITION].stride;
4703 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptr);
4707 gl_info->gl_ops.gl.p_glEnd();
4708 checkGLcall("draw immediate mode");
4711 static void draw_indirect(struct wined3d_context *context, const struct wined3d_state *state,
4712 const struct wined3d_indirect_draw_parameters *parameters, unsigned int idx_size)
4714 const struct wined3d_gl_info *gl_info = context->gl_info;
4715 struct wined3d_buffer *buffer = parameters->buffer;
4716 const void *offset;
4718 if (!gl_info->supported[ARB_DRAW_INDIRECT])
4720 FIXME("OpenGL implementation does not support indirect draws.\n");
4721 return;
4724 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer->buffer_object));
4726 offset = (void *)(GLintptr)parameters->offset;
4727 if (idx_size)
4729 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4730 if (state->index_offset)
4731 FIXME("Ignoring index offset %u.\n", state->index_offset);
4732 GL_EXTCALL(glDrawElementsIndirect(state->gl_primitive_type, idx_type, offset));
4734 else
4736 GL_EXTCALL(glDrawArraysIndirect(state->gl_primitive_type, offset));
4739 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0));
4741 checkGLcall("draw indirect");
4744 static void remove_vbos(struct wined3d_context *context,
4745 const struct wined3d_state *state, struct wined3d_stream_info *s)
4747 unsigned int i;
4749 for (i = 0; i < ARRAY_SIZE(s->elements); ++i)
4751 struct wined3d_stream_info_element *e;
4753 if (!(s->use_map & (1u << i)))
4754 continue;
4756 e = &s->elements[i];
4757 if (e->data.buffer_object)
4759 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
4760 e->data.buffer_object = 0;
4761 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(vb, context);
4766 static GLenum gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
4768 GLenum gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4769 switch (gl_primitive_type)
4771 case GL_POINTS:
4772 return GL_POINTS;
4774 case GL_LINE_STRIP:
4775 case GL_LINE_STRIP_ADJACENCY:
4776 case GL_LINES_ADJACENCY:
4777 case GL_LINES:
4778 return GL_LINES;
4780 case GL_TRIANGLE_FAN:
4781 case GL_TRIANGLE_STRIP:
4782 case GL_TRIANGLE_STRIP_ADJACENCY:
4783 case GL_TRIANGLES_ADJACENCY:
4784 case GL_TRIANGLES:
4785 return GL_TRIANGLES;
4787 default:
4788 return gl_primitive_type;
4792 /* Routine common to the draw primitive and draw indexed primitive routines */
4793 void draw_primitive(struct wined3d_device *device, const struct wined3d_state *state,
4794 const struct wined3d_draw_parameters *parameters)
4796 BOOL emulation = FALSE, rasterizer_discard = FALSE;
4797 const struct wined3d_fb_state *fb = state->fb;
4798 const struct wined3d_stream_info *stream_info;
4799 struct wined3d_rendertarget_view *dsv, *rtv;
4800 struct wined3d_stream_info si_emulated;
4801 struct wined3d_fence *ib_fence = NULL;
4802 const struct wined3d_gl_info *gl_info;
4803 struct wined3d_context *context;
4804 unsigned int i, idx_size = 0;
4805 const void *idx_data = NULL;
4807 if (!parameters->indirect && !parameters->u.direct.index_count)
4808 return;
4810 if (!(rtv = fb->render_targets[0]))
4811 rtv = fb->depth_stencil;
4812 if (rtv)
4813 context = context_acquire(device, wined3d_texture_from_resource(rtv->resource), rtv->sub_resource_idx);
4814 else
4815 context = context_acquire(device, NULL, 0);
4816 if (!context->valid)
4818 context_release(context);
4819 WARN("Invalid context, skipping draw.\n");
4820 return;
4822 gl_info = context->gl_info;
4824 if (!use_transform_feedback(state))
4825 context_pause_transform_feedback(context, TRUE);
4827 for (i = 0; i < gl_info->limits.buffers; ++i)
4829 if (!(rtv = fb->render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
4830 continue;
4832 if (state->render_states[WINED3D_RS_COLORWRITEENABLE])
4834 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
4835 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
4837 else
4839 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
4843 if ((dsv = fb->depth_stencil))
4845 /* Note that this depends on the context_acquire() call above to set
4846 * context->render_offscreen properly. We don't currently take the
4847 * Z-compare function into account, but we could skip loading the
4848 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
4849 * that we never copy the stencil data.*/
4850 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4852 if (state->render_states[WINED3D_RS_ZWRITEENABLE] || state->render_states[WINED3D_RS_ZENABLE])
4853 wined3d_rendertarget_view_load_location(dsv, context, location);
4854 else
4855 wined3d_rendertarget_view_prepare_location(dsv, context, location);
4858 if (parameters->indirect)
4859 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
4861 if (!context_apply_draw_state(context, device, state))
4863 context_release(context);
4864 WARN("Unable to apply draw state, skipping draw.\n");
4865 return;
4868 if (dsv && state->render_states[WINED3D_RS_ZWRITEENABLE])
4870 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4872 wined3d_rendertarget_view_validate_location(dsv, location);
4873 wined3d_rendertarget_view_invalidate_location(dsv, ~location);
4876 stream_info = &context->stream_info;
4878 if (parameters->indexed)
4880 struct wined3d_buffer *index_buffer = state->index_buffer;
4881 if (!index_buffer->buffer_object || !stream_info->all_vbo)
4883 idx_data = index_buffer->resource.heap_memory;
4885 else
4887 ib_fence = index_buffer->fence;
4888 idx_data = NULL;
4890 idx_data = (const BYTE *)idx_data + state->index_offset;
4892 if (state->index_format == WINED3DFMT_R16_UINT)
4893 idx_size = 2;
4894 else
4895 idx_size = 4;
4898 if (!use_vs(state))
4900 if (!stream_info->position_transformed && context->num_untracked_materials
4901 && state->render_states[WINED3D_RS_LIGHTING])
4903 static BOOL warned;
4905 if (!warned++)
4906 FIXME("Using software emulation because not all material properties could be tracked.\n");
4907 else
4908 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
4909 emulation = TRUE;
4911 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
4913 static BOOL warned;
4915 /* Either write a pipeline replacement shader or convert the
4916 * specular alpha from unsigned byte to a float in the vertex
4917 * buffer. */
4918 if (!warned++)
4919 FIXME("Using software emulation because manual fog coordinates are provided.\n");
4920 else
4921 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
4922 emulation = TRUE;
4925 if (emulation)
4927 si_emulated = context->stream_info;
4928 remove_vbos(context, state, &si_emulated);
4929 stream_info = &si_emulated;
4933 if (use_transform_feedback(state))
4935 const struct wined3d_shader *shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
4937 if (is_rasterization_disabled(shader))
4939 glEnable(GL_RASTERIZER_DISCARD);
4940 checkGLcall("enable rasterizer discard");
4941 rasterizer_discard = TRUE;
4944 if (context->transform_feedback_paused)
4946 GL_EXTCALL(glResumeTransformFeedback());
4947 checkGLcall("glResumeTransformFeedback");
4948 context->transform_feedback_paused = 0;
4950 else if (!context->transform_feedback_active)
4952 GLenum mode = gl_tfb_primitive_type_from_d3d(shader->u.gs.output_type);
4953 GL_EXTCALL(glBeginTransformFeedback(mode));
4954 checkGLcall("glBeginTransformFeedback");
4955 context->transform_feedback_active = 1;
4959 if (state->gl_primitive_type == GL_PATCHES)
4961 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES, state->gl_patch_vertices));
4962 checkGLcall("glPatchParameteri");
4965 if (parameters->indirect)
4967 if (!context->use_immediate_mode_draw && !emulation)
4968 draw_indirect(context, state, &parameters->u.indirect, idx_size);
4969 else
4970 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
4972 else
4974 unsigned int instance_count = parameters->u.direct.instance_count;
4975 if (context->instance_count)
4976 instance_count = context->instance_count;
4978 if (context->use_immediate_mode_draw || emulation)
4979 draw_primitive_immediate_mode(context, state, stream_info, idx_data,
4980 idx_size, parameters->u.direct.base_vertex_idx,
4981 parameters->u.direct.start_idx, parameters->u.direct.index_count, instance_count);
4982 else
4983 draw_primitive_arrays(context, state, idx_data, idx_size, parameters->u.direct.base_vertex_idx,
4984 parameters->u.direct.start_idx, parameters->u.direct.index_count,
4985 parameters->u.direct.start_instance, instance_count);
4988 if (context->uses_uavs)
4990 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4991 checkGLcall("glMemoryBarrier");
4994 context_pause_transform_feedback(context, FALSE);
4996 if (rasterizer_discard)
4998 glDisable(GL_RASTERIZER_DISCARD);
4999 checkGLcall("disable rasterizer discard");
5002 if (ib_fence)
5003 wined3d_fence_issue(ib_fence, device);
5004 for (i = 0; i < context->buffer_fence_count; ++i)
5005 wined3d_fence_issue(context->buffer_fences[i], device);
5007 if (wined3d_settings.strict_draw_ordering)
5008 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
5010 context_release(context);