wined3d: Reference the constant buffer bo in context_gl_load_shader_resources().
[wine.git] / dlls / wined3d / context_gl.c
blob83c61834f5a298b649e9e84092bd092e16f21f11
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 "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous);
36 #define WINED3D_MAX_FBO_ENTRIES 64
37 #define WINED3D_ALL_LAYERS (~0u)
39 static DWORD wined3d_context_tls_idx;
41 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
42 * actually have the same values in GL and D3D. */
43 static GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
45 switch (primitive_type)
47 case WINED3D_PT_POINTLIST:
48 return GL_POINTS;
50 case WINED3D_PT_LINELIST:
51 return GL_LINES;
53 case WINED3D_PT_LINESTRIP:
54 return GL_LINE_STRIP;
56 case WINED3D_PT_TRIANGLELIST:
57 return GL_TRIANGLES;
59 case WINED3D_PT_TRIANGLESTRIP:
60 return GL_TRIANGLE_STRIP;
62 case WINED3D_PT_TRIANGLEFAN:
63 return GL_TRIANGLE_FAN;
65 case WINED3D_PT_LINELIST_ADJ:
66 return GL_LINES_ADJACENCY_ARB;
68 case WINED3D_PT_LINESTRIP_ADJ:
69 return GL_LINE_STRIP_ADJACENCY_ARB;
71 case WINED3D_PT_TRIANGLELIST_ADJ:
72 return GL_TRIANGLES_ADJACENCY_ARB;
74 case WINED3D_PT_TRIANGLESTRIP_ADJ:
75 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
77 case WINED3D_PT_PATCH:
78 return GL_PATCHES;
80 default:
81 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
82 case WINED3D_PT_UNDEFINED:
83 return ~0u;
87 /* FBO helper functions */
89 /* Context activation is done by the caller. */
90 static void wined3d_context_gl_bind_fbo(struct wined3d_context_gl *context_gl, GLenum target, GLuint fbo)
92 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
94 TRACE("context_gl %p, target %#x, fbo %u.\n", context_gl, target, fbo);
96 switch (target)
98 case GL_READ_FRAMEBUFFER:
99 if (context_gl->fbo_read_binding == fbo)
100 return;
101 context_gl->fbo_read_binding = fbo;
102 break;
104 case GL_DRAW_FRAMEBUFFER:
105 if (context_gl->fbo_draw_binding == fbo)
106 return;
107 context_gl->fbo_draw_binding = fbo;
108 break;
110 case GL_FRAMEBUFFER:
111 if (context_gl->fbo_read_binding == fbo
112 && context_gl->fbo_draw_binding == fbo)
113 return;
114 context_gl->fbo_read_binding = fbo;
115 context_gl->fbo_draw_binding = fbo;
116 break;
118 default:
119 FIXME("Unhandled target %#x.\n", target);
120 break;
123 gl_info->fbo_ops.glBindFramebuffer(target, fbo);
124 checkGLcall("glBindFramebuffer()");
127 /* Context activation is done by the caller. */
128 static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info, GLenum target)
130 unsigned int i;
132 for (i = 0; i < gl_info->limits.buffers; ++i)
134 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
135 checkGLcall("glFramebufferTexture2D()");
137 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
138 checkGLcall("glFramebufferTexture2D()");
140 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
141 checkGLcall("glFramebufferTexture2D()");
144 /* Context activation is done by the caller. */
145 static void wined3d_context_gl_destroy_fbo(struct wined3d_context_gl *context_gl, GLuint fbo)
147 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
149 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, fbo);
150 context_clean_fbo_attachments(gl_info, GL_FRAMEBUFFER);
151 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
153 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
154 checkGLcall("glDeleteFramebuffers()");
157 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info *gl_info,
158 GLenum fbo_target, DWORD flags, GLuint rb)
160 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
162 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
163 checkGLcall("glFramebufferRenderbuffer()");
166 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
168 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
169 checkGLcall("glFramebufferRenderbuffer()");
173 static void wined3d_context_gl_attach_gl_texture_fbo(struct wined3d_context_gl *context_gl,
174 GLenum fbo_target, GLenum attachment, const struct wined3d_fbo_resource *resource)
176 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
178 if (!resource)
180 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment, GL_TEXTURE_2D, 0, 0);
182 else if (resource->layer == WINED3D_ALL_LAYERS)
184 if (!gl_info->fbo_ops.glFramebufferTexture)
186 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
187 return;
190 gl_info->fbo_ops.glFramebufferTexture(fbo_target, attachment,
191 resource->object, resource->level);
193 else if (resource->target == GL_TEXTURE_1D_ARRAY || resource->target == GL_TEXTURE_2D_ARRAY
194 || resource->target == GL_TEXTURE_3D)
196 if (!gl_info->fbo_ops.glFramebufferTextureLayer)
198 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
199 return;
202 gl_info->fbo_ops.glFramebufferTextureLayer(fbo_target, attachment,
203 resource->object, resource->level, resource->layer);
205 else if (resource->target == GL_TEXTURE_1D)
207 gl_info->fbo_ops.glFramebufferTexture1D(fbo_target, attachment,
208 resource->target, resource->object, resource->level);
210 else
212 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment,
213 resource->target, resource->object, resource->level);
215 checkGLcall("attach texture to fbo");
218 /* Context activation is done by the caller. */
219 static void wined3d_context_gl_attach_depth_stencil_fbo(struct wined3d_context_gl *context_gl,
220 GLenum fbo_target, const struct wined3d_fbo_resource *resource, BOOL rb_namespace,
221 uint32_t flags)
223 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
225 if (resource->object)
227 TRACE("Attach depth stencil %u.\n", resource->object);
229 if (rb_namespace)
231 context_attach_depth_stencil_rb(gl_info, fbo_target,
232 flags, resource->object);
234 else
236 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
237 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_DEPTH_ATTACHMENT, resource);
239 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
240 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_STENCIL_ATTACHMENT, resource);
243 if (!(flags & WINED3D_FBO_ENTRY_FLAG_DEPTH))
244 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
246 if (!(flags & WINED3D_FBO_ENTRY_FLAG_STENCIL))
247 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
249 else
251 TRACE("Attach depth stencil 0.\n");
253 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
254 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
258 /* Context activation is done by the caller. */
259 static void wined3d_context_gl_attach_surface_fbo(struct wined3d_context_gl *context_gl,
260 GLenum fbo_target, unsigned int idx, const struct wined3d_fbo_resource *resource, BOOL rb_namespace)
262 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
264 TRACE("Attach GL object %u to %u.\n", resource->object, idx);
266 if (resource->object)
268 if (rb_namespace)
270 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
271 GL_RENDERBUFFER, resource->object);
272 checkGLcall("glFramebufferRenderbuffer()");
274 else
276 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_COLOR_ATTACHMENT0 + idx, resource);
279 else
281 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_COLOR_ATTACHMENT0 + idx, NULL);
285 static void context_dump_fbo_attachment(const struct wined3d_gl_info *gl_info, GLenum target,
286 GLenum attachment)
288 static const struct
290 GLenum target;
291 GLenum binding;
292 const char *str;
293 enum wined3d_gl_extension extension;
295 texture_type[] =
297 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D, "1d", WINED3D_GL_EXT_NONE},
298 {GL_TEXTURE_1D_ARRAY, GL_TEXTURE_BINDING_1D_ARRAY, "1d-array", EXT_TEXTURE_ARRAY},
299 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, "2d", WINED3D_GL_EXT_NONE},
300 {GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_BINDING_RECTANGLE_ARB, "rectangle", ARB_TEXTURE_RECTANGLE},
301 {GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BINDING_2D_ARRAY, "2d-array" , EXT_TEXTURE_ARRAY},
302 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, "cube", ARB_TEXTURE_CUBE_MAP},
303 {GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_BINDING_2D_MULTISAMPLE, "2d-ms", ARB_TEXTURE_MULTISAMPLE},
304 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY, "2d-array-ms", ARB_TEXTURE_MULTISAMPLE},
307 GLint type, name, samples, width, height, old_texture, level, face, fmt, tex_target;
308 const char *tex_type_str = NULL;
309 unsigned int i;
311 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
312 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &name);
313 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
314 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &type);
316 if (type == GL_RENDERBUFFER)
318 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, name);
319 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
320 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
321 if (gl_info->limits.samples > 1)
322 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
323 else
324 samples = 1;
325 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &fmt);
326 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
327 debug_fboattachment(attachment), name, width, height, samples, fmt);
329 else if (type == GL_TEXTURE)
331 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
332 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL, &level);
333 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
334 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE, &face);
336 if (gl_info->gl_ops.ext.p_glGetTextureParameteriv)
338 GL_EXTCALL(glGetTextureParameteriv(name, GL_TEXTURE_TARGET, &tex_target));
340 for (i = 0; i < ARRAY_SIZE(texture_type); ++i)
342 if (texture_type[i].target == tex_target)
344 tex_type_str = texture_type[i].str;
345 break;
348 if (i == ARRAY_SIZE(texture_type))
349 tex_type_str = wine_dbg_sprintf("%#x", tex_target);
351 else if (face)
353 gl_info->gl_ops.gl.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &old_texture);
354 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, name);
356 tex_target = GL_TEXTURE_CUBE_MAP;
357 tex_type_str = "cube";
359 else
361 for (i = 0; i < ARRAY_SIZE(texture_type); ++i)
363 if (!gl_info->supported[texture_type[i].extension])
364 continue;
366 gl_info->gl_ops.gl.p_glGetIntegerv(texture_type[i].binding, &old_texture);
367 while (gl_info->gl_ops.gl.p_glGetError());
369 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, name);
370 if (!gl_info->gl_ops.gl.p_glGetError())
372 tex_target = texture_type[i].target;
373 tex_type_str = texture_type[i].str;
374 break;
376 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, old_texture);
379 if (!tex_type_str)
381 FIXME("Cannot find type of texture %d.\n", name);
382 return;
386 if (gl_info->gl_ops.ext.p_glGetTextureParameteriv)
388 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt));
389 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_WIDTH, &width));
390 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_HEIGHT, &height));
391 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_SAMPLES, &samples));
393 else
395 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
396 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_WIDTH, &width);
397 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_HEIGHT, &height);
398 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
399 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_SAMPLES, &samples);
400 else
401 samples = 1;
403 gl_info->gl_ops.gl.p_glBindTexture(tex_target, old_texture);
406 FIXME(" %s: %s texture %d, %dx%d, %d samples, format %#x.\n",
407 debug_fboattachment(attachment), tex_type_str, name, width, height, samples, fmt);
409 else if (type == GL_NONE)
411 FIXME(" %s: NONE.\n", debug_fboattachment(attachment));
413 else
415 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment), type);
418 checkGLcall("dump FBO attachment");
421 /* Context activation is done by the caller. */
422 void wined3d_context_gl_check_fbo_status(const struct wined3d_context_gl *context_gl, GLenum target)
424 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
425 GLenum status;
427 if (!FIXME_ON(d3d))
428 return;
430 status = gl_info->fbo_ops.glCheckFramebufferStatus(target);
431 if (status == GL_FRAMEBUFFER_COMPLETE)
433 TRACE("FBO complete.\n");
435 else
437 unsigned int i;
439 FIXME("FBO status %s (%#x).\n", debug_fbostatus(status), status);
441 if (!context_gl->current_fbo)
443 ERR("FBO 0 is incomplete, driver bug?\n");
444 return;
447 context_dump_fbo_attachment(gl_info, target, GL_DEPTH_ATTACHMENT);
448 context_dump_fbo_attachment(gl_info, target, GL_STENCIL_ATTACHMENT);
450 for (i = 0; i < gl_info->limits.buffers; ++i)
451 context_dump_fbo_attachment(gl_info, target, GL_COLOR_ATTACHMENT0 + i);
455 static inline DWORD context_generate_rt_mask(GLenum buffer)
457 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
458 return buffer ? (1u << 31) | buffer : 0;
461 static inline DWORD context_generate_rt_mask_from_resource(struct wined3d_resource *resource)
463 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
465 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
466 return 0;
469 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource));
472 static inline void wined3d_context_gl_set_fbo_key_for_render_target(const struct wined3d_context_gl *context_gl,
473 struct wined3d_fbo_entry_key *key, unsigned int idx, const struct wined3d_rendertarget_info *render_target,
474 DWORD location)
476 unsigned int sub_resource_idx = render_target->sub_resource_idx;
477 struct wined3d_resource *resource = render_target->resource;
478 struct wined3d_texture_gl *texture_gl;
480 if (!resource || resource->format->id == WINED3DFMT_NULL || resource->type == WINED3D_RTYPE_BUFFER)
482 if (resource && resource->type == WINED3D_RTYPE_BUFFER)
483 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
484 key->objects[idx].object = 0;
485 key->objects[idx].target = 0;
486 key->objects[idx].level = key->objects[idx].layer = 0;
487 return;
490 if (render_target->gl_view.name)
492 key->objects[idx].object = render_target->gl_view.name;
493 key->objects[idx].target = render_target->gl_view.target;
494 key->objects[idx].level = 0;
495 key->objects[idx].layer = WINED3D_ALL_LAYERS;
496 return;
499 texture_gl = wined3d_texture_gl(wined3d_texture_from_resource(resource));
500 if (texture_gl->current_renderbuffer)
502 key->objects[idx].object = texture_gl->current_renderbuffer->id;
503 key->objects[idx].target = 0;
504 key->objects[idx].level = key->objects[idx].layer = 0;
505 key->rb_namespace |= 1 << idx;
506 return;
509 key->objects[idx].target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
510 key->objects[idx].level = sub_resource_idx % texture_gl->t.level_count;
511 key->objects[idx].layer = sub_resource_idx / texture_gl->t.level_count;
513 if (render_target->layer_count != 1)
514 key->objects[idx].layer = WINED3D_ALL_LAYERS;
516 switch (location)
518 case WINED3D_LOCATION_TEXTURE_RGB:
519 key->objects[idx].object = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, FALSE);
520 break;
522 case WINED3D_LOCATION_TEXTURE_SRGB:
523 key->objects[idx].object = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, TRUE);
524 break;
526 case WINED3D_LOCATION_RB_MULTISAMPLE:
527 key->objects[idx].object = texture_gl->rb_multisample;
528 key->objects[idx].target = 0;
529 key->objects[idx].level = key->objects[idx].layer = 0;
530 key->rb_namespace |= 1 << idx;
531 break;
533 case WINED3D_LOCATION_RB_RESOLVED:
534 key->objects[idx].object = texture_gl->rb_resolved;
535 key->objects[idx].target = 0;
536 key->objects[idx].level = key->objects[idx].layer = 0;
537 key->rb_namespace |= 1 << idx;
538 break;
542 static void wined3d_context_gl_generate_fbo_key(const struct wined3d_context_gl *context_gl,
543 struct wined3d_fbo_entry_key *key, const struct wined3d_rendertarget_info *render_targets,
544 const struct wined3d_rendertarget_info *depth_stencil, DWORD color_location, DWORD ds_location)
546 unsigned int buffers = context_gl->gl_info->limits.buffers;
547 unsigned int i;
549 key->rb_namespace = 0;
550 wined3d_context_gl_set_fbo_key_for_render_target(context_gl, key, 0, depth_stencil, ds_location);
552 for (i = 0; i < buffers; ++i)
553 wined3d_context_gl_set_fbo_key_for_render_target(context_gl, key, i + 1, &render_targets[i], color_location);
555 memset(&key->objects[buffers + 1], 0, (ARRAY_SIZE(key->objects) - buffers - 1) * sizeof(*key->objects));
558 static struct fbo_entry *wined3d_context_gl_create_fbo_entry(const struct wined3d_context_gl *context_gl,
559 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
560 DWORD color_location, DWORD ds_location)
562 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
563 struct fbo_entry *entry;
565 entry = heap_alloc(sizeof(*entry));
566 wined3d_context_gl_generate_fbo_key(context_gl, &entry->key,
567 render_targets, depth_stencil, color_location, ds_location);
568 entry->flags = 0;
569 if (depth_stencil->resource)
571 if (depth_stencil->resource->format->depth_size)
572 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
573 if (depth_stencil->resource->format->stencil_size)
574 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
576 entry->rt_mask = context_generate_rt_mask(GL_COLOR_ATTACHMENT0);
577 gl_info->fbo_ops.glGenFramebuffers(1, &entry->id);
578 checkGLcall("glGenFramebuffers()");
579 TRACE("Created FBO %u.\n", entry->id);
581 return entry;
584 /* Context activation is done by the caller. */
585 static void wined3d_context_gl_reuse_fbo_entry(struct wined3d_context_gl *context_gl, GLenum target,
586 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
587 DWORD color_location, DWORD ds_location, struct fbo_entry *entry)
589 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
591 wined3d_context_gl_bind_fbo(context_gl, target, entry->id);
592 context_clean_fbo_attachments(gl_info, target);
594 wined3d_context_gl_generate_fbo_key(context_gl, &entry->key,
595 render_targets, depth_stencil, color_location, ds_location);
596 entry->flags = 0;
597 if (depth_stencil->resource)
599 if (depth_stencil->resource->format->depth_size)
600 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
601 if (depth_stencil->resource->format->stencil_size)
602 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
606 /* Context activation is done by the caller. */
607 static void wined3d_context_gl_destroy_fbo_entry(struct wined3d_context_gl *context_gl, struct fbo_entry *entry)
609 if (entry->id)
611 TRACE("Destroy FBO %u.\n", entry->id);
612 wined3d_context_gl_destroy_fbo(context_gl, entry->id);
614 --context_gl->fbo_entry_count;
615 list_remove(&entry->entry);
616 heap_free(entry);
619 /* Context activation is done by the caller. */
620 static struct fbo_entry *wined3d_context_gl_find_fbo_entry(struct wined3d_context_gl *context_gl, GLenum target,
621 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
622 DWORD color_location, DWORD ds_location)
624 static const struct wined3d_rendertarget_info ds_null = {{0}};
625 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
626 struct wined3d_texture *rt_texture, *ds_texture;
627 struct wined3d_fbo_entry_key fbo_key;
628 unsigned int i, ds_level, rt_level;
629 struct fbo_entry *entry;
631 if (depth_stencil->resource && depth_stencil->resource->type != WINED3D_RTYPE_BUFFER
632 && render_targets[0].resource && render_targets[0].resource->type != WINED3D_RTYPE_BUFFER
633 && render_targets[0].resource->format->id != WINED3DFMT_NULL)
635 rt_texture = wined3d_texture_from_resource(render_targets[0].resource);
636 rt_level = render_targets[0].sub_resource_idx % rt_texture->level_count;
637 ds_texture = wined3d_texture_from_resource(depth_stencil->resource);
638 ds_level = depth_stencil->sub_resource_idx % ds_texture->level_count;
640 if (wined3d_texture_get_level_width(ds_texture, ds_level)
641 < wined3d_texture_get_level_width(rt_texture, rt_level)
642 || wined3d_texture_get_level_height(ds_texture, ds_level)
643 < wined3d_texture_get_level_height(rt_texture, rt_level))
645 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
646 depth_stencil = &ds_null;
648 else if (ds_texture->resource.multisample_type != rt_texture->resource.multisample_type
649 || (ds_texture->resource.multisample_type
650 && ds_texture->resource.multisample_quality != rt_texture->resource.multisample_quality))
652 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
653 rt_texture->resource.multisample_type, rt_texture->resource.multisample_quality,
654 ds_texture->resource.multisample_type, ds_texture->resource.multisample_quality);
655 depth_stencil = &ds_null;
657 else if (depth_stencil->resource->type == WINED3D_RTYPE_TEXTURE_2D)
659 wined3d_texture_gl_set_compatible_renderbuffer(wined3d_texture_gl(ds_texture),
660 context_gl, ds_level, &render_targets[0]);
664 wined3d_context_gl_generate_fbo_key(context_gl, &fbo_key,
665 render_targets, depth_stencil, color_location, ds_location);
667 if (TRACE_ON(d3d))
669 struct wined3d_resource *resource;
670 unsigned int width, height;
671 const char *resource_type;
673 TRACE("Dumping FBO attachments:\n");
674 for (i = 0; i < gl_info->limits.buffers; ++i)
676 if ((resource = render_targets[i].resource))
678 if (resource->type == WINED3D_RTYPE_BUFFER)
680 width = resource->size;
681 height = 1;
682 resource_type = "buffer";
684 else
686 rt_texture = wined3d_texture_from_resource(resource);
687 rt_level = render_targets[i].sub_resource_idx % rt_texture->level_count;
688 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
689 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
690 resource_type = "texture";
693 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
694 i, resource, render_targets[i].sub_resource_idx, debug_d3dformat(resource->format->id),
695 fbo_key.rb_namespace & (1 << (i + 1)) ? "renderbuffer" : resource_type,
696 fbo_key.objects[i + 1].object, width, height, resource->multisample_type);
699 if ((resource = depth_stencil->resource))
701 if (resource->type == WINED3D_RTYPE_BUFFER)
703 width = resource->size;
704 height = 1;
705 resource_type = "buffer";
707 else
709 ds_texture = wined3d_texture_from_resource(resource);
710 ds_level = depth_stencil->sub_resource_idx % ds_texture->level_count;
711 width = wined3d_texture_get_level_pow2_width(ds_texture, ds_level);
712 height = wined3d_texture_get_level_pow2_height(ds_texture, ds_level);
713 resource_type = "texture";
716 TRACE(" Depth attachment: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
717 resource, depth_stencil->sub_resource_idx, debug_d3dformat(resource->format->id),
718 fbo_key.rb_namespace & (1 << 0) ? "renderbuffer" : resource_type,
719 fbo_key.objects[0].object, width, height, resource->multisample_type);
723 LIST_FOR_EACH_ENTRY(entry, &context_gl->fbo_list, struct fbo_entry, entry)
725 if (memcmp(&fbo_key, &entry->key, sizeof(fbo_key)))
726 continue;
728 list_remove(&entry->entry);
729 list_add_head(&context_gl->fbo_list, &entry->entry);
730 return entry;
733 if (context_gl->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
735 entry = wined3d_context_gl_create_fbo_entry(context_gl,
736 render_targets, depth_stencil, color_location, ds_location);
737 list_add_head(&context_gl->fbo_list, &entry->entry);
738 ++context_gl->fbo_entry_count;
740 else
742 entry = LIST_ENTRY(list_tail(&context_gl->fbo_list), struct fbo_entry, entry);
743 wined3d_context_gl_reuse_fbo_entry(context_gl, target, render_targets,
744 depth_stencil, color_location, ds_location, entry);
745 list_remove(&entry->entry);
746 list_add_head(&context_gl->fbo_list, &entry->entry);
749 return entry;
752 /* Context activation is done by the caller. */
753 static void wined3d_context_gl_apply_fbo_entry(struct wined3d_context_gl *context_gl,
754 GLenum target, struct fbo_entry *entry)
756 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
757 GLuint read_binding, draw_binding;
758 unsigned int i;
760 if (entry->flags & WINED3D_FBO_ENTRY_FLAG_ATTACHED)
762 wined3d_context_gl_bind_fbo(context_gl, target, entry->id);
763 return;
766 read_binding = context_gl->fbo_read_binding;
767 draw_binding = context_gl->fbo_draw_binding;
768 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, entry->id);
770 if (gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
772 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER,
773 GL_FRAMEBUFFER_DEFAULT_WIDTH, gl_info->limits.framebuffer_width));
774 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER,
775 GL_FRAMEBUFFER_DEFAULT_HEIGHT, gl_info->limits.framebuffer_height));
776 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS, 1));
777 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES, 1));
780 /* Apply render targets */
781 for (i = 0; i < gl_info->limits.buffers; ++i)
783 wined3d_context_gl_attach_surface_fbo(context_gl, target, i,
784 &entry->key.objects[i + 1], entry->key.rb_namespace & (1 << (i + 1)));
787 wined3d_context_gl_attach_depth_stencil_fbo(context_gl, target,
788 &entry->key.objects[0], entry->key.rb_namespace & 0x1, entry->flags);
790 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
791 * GL contexts requirements. */
792 gl_info->gl_ops.gl.p_glReadBuffer(GL_NONE);
793 wined3d_context_gl_set_draw_buffer(context_gl, GL_NONE);
794 if (target != GL_FRAMEBUFFER)
796 if (target == GL_READ_FRAMEBUFFER)
797 wined3d_context_gl_bind_fbo(context_gl, GL_DRAW_FRAMEBUFFER, draw_binding);
798 else
799 wined3d_context_gl_bind_fbo(context_gl, GL_READ_FRAMEBUFFER, read_binding);
802 entry->flags |= WINED3D_FBO_ENTRY_FLAG_ATTACHED;
805 /* Context activation is done by the caller. */
806 static void wined3d_context_gl_apply_fbo_state(struct wined3d_context_gl *context_gl, GLenum target,
807 const struct wined3d_rendertarget_info *render_targets,
808 const struct wined3d_rendertarget_info *depth_stencil, DWORD color_location, DWORD ds_location)
810 struct fbo_entry *entry, *entry2;
812 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_destroy_list, struct fbo_entry, entry)
814 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
817 if (context_gl->rebind_fbo)
819 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
820 context_gl->rebind_fbo = FALSE;
823 if (color_location == WINED3D_LOCATION_DRAWABLE)
825 context_gl->current_fbo = NULL;
826 wined3d_context_gl_bind_fbo(context_gl, target, 0);
828 else
830 context_gl->current_fbo = wined3d_context_gl_find_fbo_entry(context_gl,
831 target, render_targets, depth_stencil, color_location, ds_location);
832 wined3d_context_gl_apply_fbo_entry(context_gl, target, context_gl->current_fbo);
836 /* Context activation is done by the caller. */
837 void wined3d_context_gl_apply_fbo_state_blit(struct wined3d_context_gl *context_gl, GLenum target,
838 struct wined3d_resource *rt, unsigned int rt_sub_resource_idx,
839 struct wined3d_resource *ds, unsigned int ds_sub_resource_idx, DWORD location)
841 struct wined3d_rendertarget_info ds_info = {{0}};
843 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
844 if (rt)
846 context_gl->blit_targets[0].resource = rt;
847 context_gl->blit_targets[0].sub_resource_idx = rt_sub_resource_idx;
848 context_gl->blit_targets[0].layer_count = 1;
851 if (ds)
853 ds_info.resource = ds;
854 ds_info.sub_resource_idx = ds_sub_resource_idx;
855 ds_info.layer_count = 1;
858 wined3d_context_gl_apply_fbo_state(context_gl, target, context_gl->blit_targets, &ds_info, location, location);
861 /* Context activation is done by the caller. */
862 void wined3d_context_gl_alloc_occlusion_query(struct wined3d_context_gl *context_gl,
863 struct wined3d_occlusion_query *query)
865 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
867 if (context_gl->free_occlusion_query_count)
869 query->id = context_gl->free_occlusion_queries[--context_gl->free_occlusion_query_count];
871 else
873 if (gl_info->supported[ARB_OCCLUSION_QUERY])
875 GL_EXTCALL(glGenQueries(1, &query->id));
876 checkGLcall("glGenQueries");
878 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context_gl);
880 else
882 WARN("Occlusion queries not supported, not allocating query id.\n");
883 query->id = 0;
887 query->context_gl = context_gl;
888 list_add_head(&context_gl->occlusion_queries, &query->entry);
891 void wined3d_context_gl_free_occlusion_query(struct wined3d_occlusion_query *query)
893 struct wined3d_context_gl *context_gl = query->context_gl;
895 list_remove(&query->entry);
896 query->context_gl = NULL;
898 if (!wined3d_array_reserve((void **)&context_gl->free_occlusion_queries,
899 &context_gl->free_occlusion_query_size, context_gl->free_occlusion_query_count + 1,
900 sizeof(*context_gl->free_occlusion_queries)))
902 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context_gl);
903 return;
906 context_gl->free_occlusion_queries[context_gl->free_occlusion_query_count++] = query->id;
909 /* Context activation is done by the caller. */
910 void wined3d_context_gl_alloc_fence(struct wined3d_context_gl *context_gl, struct wined3d_fence *fence)
912 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
914 if (context_gl->free_fence_count)
916 fence->object = context_gl->free_fences[--context_gl->free_fence_count];
918 else
920 if (gl_info->supported[ARB_SYNC])
922 /* Using ARB_sync, not much to do here. */
923 fence->object.sync = NULL;
924 TRACE("Allocated sync object in context %p.\n", context_gl);
926 else if (gl_info->supported[APPLE_FENCE])
928 GL_EXTCALL(glGenFencesAPPLE(1, &fence->object.id));
929 checkGLcall("glGenFencesAPPLE");
931 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context_gl);
933 else if(gl_info->supported[NV_FENCE])
935 GL_EXTCALL(glGenFencesNV(1, &fence->object.id));
936 checkGLcall("glGenFencesNV");
938 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context_gl);
940 else
942 WARN("Fences not supported, not allocating fence.\n");
943 fence->object.id = 0;
947 fence->context_gl = context_gl;
948 list_add_head(&context_gl->fences, &fence->entry);
951 void wined3d_context_gl_free_fence(struct wined3d_fence *fence)
953 struct wined3d_context_gl *context_gl = fence->context_gl;
955 list_remove(&fence->entry);
956 fence->context_gl = NULL;
958 if (!wined3d_array_reserve((void **)&context_gl->free_fences,
959 &context_gl->free_fence_size, context_gl->free_fence_count + 1,
960 sizeof(*context_gl->free_fences)))
962 ERR("Failed to grow free list, leaking fence %u in context %p.\n", fence->object.id, context_gl);
963 return;
966 context_gl->free_fences[context_gl->free_fence_count++] = fence->object;
969 /* Context activation is done by the caller. */
970 void wined3d_context_gl_alloc_timestamp_query(struct wined3d_context_gl *context_gl,
971 struct wined3d_timestamp_query *query)
973 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
975 if (context_gl->free_timestamp_query_count)
977 query->id = context_gl->free_timestamp_queries[--context_gl->free_timestamp_query_count];
979 else
981 GL_EXTCALL(glGenQueries(1, &query->id));
982 checkGLcall("glGenQueries");
984 TRACE("Allocated timestamp query %u in context %p.\n", query->id, context_gl);
987 query->context_gl = context_gl;
988 list_add_head(&context_gl->timestamp_queries, &query->entry);
991 void wined3d_context_gl_free_timestamp_query(struct wined3d_timestamp_query *query)
993 struct wined3d_context_gl *context_gl = query->context_gl;
995 list_remove(&query->entry);
996 query->context_gl = NULL;
998 if (!wined3d_array_reserve((void **)&context_gl->free_timestamp_queries,
999 &context_gl->free_timestamp_query_size, context_gl->free_timestamp_query_count + 1,
1000 sizeof(*context_gl->free_timestamp_queries)))
1002 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context_gl);
1003 return;
1006 context_gl->free_timestamp_queries[context_gl->free_timestamp_query_count++] = query->id;
1009 void wined3d_context_gl_alloc_so_statistics_query(struct wined3d_context_gl *context_gl,
1010 struct wined3d_so_statistics_query *query)
1012 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1014 if (context_gl->free_so_statistics_query_count)
1016 query->u = context_gl->free_so_statistics_queries[--context_gl->free_so_statistics_query_count];
1018 else
1020 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
1021 checkGLcall("glGenQueries");
1023 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
1024 query->u.id[0], query->u.id[1], context_gl);
1027 query->context_gl = context_gl;
1028 list_add_head(&context_gl->so_statistics_queries, &query->entry);
1031 void wined3d_context_gl_free_so_statistics_query(struct wined3d_so_statistics_query *query)
1033 struct wined3d_context_gl *context_gl = query->context_gl;
1035 list_remove(&query->entry);
1036 query->context_gl = NULL;
1038 if (!wined3d_array_reserve((void **)&context_gl->free_so_statistics_queries,
1039 &context_gl->free_so_statistics_query_size, context_gl->free_so_statistics_query_count + 1,
1040 sizeof(*context_gl->free_so_statistics_queries)))
1042 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
1043 query->u.id[0], query->u.id[1], context_gl);
1044 return;
1047 context_gl->free_so_statistics_queries[context_gl->free_so_statistics_query_count++] = query->u;
1050 void wined3d_context_gl_alloc_pipeline_statistics_query(struct wined3d_context_gl *context_gl,
1051 struct wined3d_pipeline_statistics_query *query)
1053 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1055 if (context_gl->free_pipeline_statistics_query_count)
1057 query->u = context_gl->free_pipeline_statistics_queries[--context_gl->free_pipeline_statistics_query_count];
1059 else
1061 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
1062 checkGLcall("glGenQueries");
1065 query->context_gl = context_gl;
1066 list_add_head(&context_gl->pipeline_statistics_queries, &query->entry);
1069 void wined3d_context_gl_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query *query)
1071 struct wined3d_context_gl *context_gl = query->context_gl;
1073 list_remove(&query->entry);
1074 query->context_gl = NULL;
1076 if (!wined3d_array_reserve((void **)&context_gl->free_pipeline_statistics_queries,
1077 &context_gl->free_pipeline_statistics_query_size, context_gl->free_pipeline_statistics_query_count + 1,
1078 sizeof(*context_gl->free_pipeline_statistics_queries)))
1080 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context_gl);
1081 return;
1084 context_gl->free_pipeline_statistics_queries[context_gl->free_pipeline_statistics_query_count++] = query->u;
1087 typedef void (context_fbo_entry_func_t)(struct wined3d_context_gl *context_gl, struct fbo_entry *entry);
1089 static void wined3d_context_gl_enum_fbo_entries(const struct wined3d_device *device,
1090 GLuint name, BOOL rb_namespace, context_fbo_entry_func_t *callback)
1092 unsigned int i, j;
1094 for (i = 0; i < device->context_count; ++i)
1096 struct wined3d_context_gl *context_gl = wined3d_context_gl(device->contexts[i]);
1097 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1098 struct fbo_entry *entry, *entry2;
1100 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_list, struct fbo_entry, entry)
1102 for (j = 0; j < gl_info->limits.buffers + 1; ++j)
1104 if (entry->key.objects[j].object == name
1105 && !(entry->key.rb_namespace & (1 << j)) == !rb_namespace)
1107 callback(context_gl, entry);
1108 break;
1115 static void wined3d_context_gl_queue_fbo_entry_destruction(struct wined3d_context_gl *context_gl,
1116 struct fbo_entry *entry)
1118 list_remove(&entry->entry);
1119 list_add_head(&context_gl->fbo_destroy_list, &entry->entry);
1122 void context_gl_resource_released(struct wined3d_device *device, GLuint name, BOOL rb_namespace)
1124 wined3d_context_gl_enum_fbo_entries(device, name, rb_namespace,
1125 wined3d_context_gl_queue_fbo_entry_destruction);
1128 void wined3d_context_gl_texture_update(struct wined3d_context_gl *context_gl,
1129 const struct wined3d_texture_gl *texture_gl)
1131 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1132 struct fbo_entry *entry = context_gl->current_fbo;
1133 unsigned int i;
1135 if (!entry || context_gl->rebind_fbo)
1136 return;
1138 for (i = 0; i < gl_info->limits.buffers + 1; ++i)
1140 if (texture_gl->texture_rgb.name == entry->key.objects[i].object
1141 || texture_gl->texture_srgb.name == entry->key.objects[i].object)
1143 TRACE("Updated texture %p is bound as attachment %u to the current FBO.\n", texture_gl, i);
1144 context_gl->rebind_fbo = TRUE;
1145 return;
1150 static BOOL wined3d_context_gl_restore_pixel_format(struct wined3d_context_gl *context_gl)
1152 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1153 BOOL ret = FALSE;
1155 if (context_gl->restore_pf && IsWindow(context_gl->restore_pf_win))
1157 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1159 HDC dc = GetDCEx(context_gl->restore_pf_win, 0, DCX_USESTYLE | DCX_CACHE);
1160 if (dc)
1162 if (!(ret = GL_EXTCALL(wglSetPixelFormatWINE(dc, context_gl->restore_pf))))
1164 ERR("Failed to restore pixel format %d on window %p.\n",
1165 context_gl->restore_pf, context_gl->restore_pf_win);
1167 ReleaseDC(context_gl->restore_pf_win, dc);
1170 else
1172 ERR("Unable to restore pixel format %d on window %p.\n",
1173 context_gl->restore_pf, context_gl->restore_pf_win);
1177 context_gl->restore_pf = 0;
1178 context_gl->restore_pf_win = NULL;
1179 return ret;
1182 static BOOL wined3d_context_gl_set_pixel_format(struct wined3d_context_gl *context_gl)
1184 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1185 BOOL private = context_gl->dc_is_private;
1186 int format = context_gl->pixel_format;
1187 HDC dc = context_gl->dc;
1188 int current;
1190 if (private && context_gl->dc_has_format)
1191 return TRUE;
1193 if (!private && WindowFromDC(dc) != context_gl->window)
1194 return FALSE;
1196 current = gl_info->gl_ops.wgl.p_wglGetPixelFormat(dc);
1197 if (current == format) goto success;
1199 if (!current)
1201 if (!SetPixelFormat(dc, format, NULL))
1203 /* This may also happen if the dc belongs to a destroyed window. */
1204 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1205 format, dc, GetLastError());
1206 return FALSE;
1209 context_gl->restore_pf = 0;
1210 context_gl->restore_pf_win = private ? NULL : WindowFromDC(dc);
1211 goto success;
1214 /* By default WGL doesn't allow pixel format adjustments but we need it
1215 * here. For this reason there's a Wine specific wglSetPixelFormat()
1216 * which allows us to set the pixel format multiple times. Only use it
1217 * when really needed. */
1218 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1220 HWND win;
1222 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
1224 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1225 format, dc);
1226 return FALSE;
1229 win = private ? NULL : WindowFromDC(dc);
1230 if (win != context_gl->restore_pf_win)
1232 wined3d_context_gl_restore_pixel_format(context_gl);
1234 context_gl->restore_pf = private ? 0 : current;
1235 context_gl->restore_pf_win = win;
1238 goto success;
1241 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1242 * continue using the old format. There's a big chance that the old
1243 * format works although with a performance hit and perhaps rendering
1244 * errors. */
1245 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1246 format, dc, current);
1247 return TRUE;
1249 success:
1250 if (private)
1251 context_gl->dc_has_format = TRUE;
1252 return TRUE;
1255 static BOOL wined3d_context_gl_set_gl_context(struct wined3d_context_gl *context_gl)
1257 struct wined3d_swapchain_gl *swapchain_gl = wined3d_swapchain_gl(context_gl->c.swapchain);
1258 BOOL backup = FALSE;
1260 if (!wined3d_context_gl_set_pixel_format(context_gl))
1262 WARN("Failed to set pixel format %d on device context %p.\n",
1263 context_gl->pixel_format, context_gl->dc);
1264 backup = TRUE;
1267 if (backup || !wglMakeCurrent(context_gl->dc, context_gl->gl_ctx))
1269 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1270 context_gl->gl_ctx, context_gl->dc, GetLastError());
1271 context_gl->valid = 0;
1272 WARN("Trying fallback to the backup window.\n");
1274 /* FIXME: If the context is destroyed it's no longer associated with
1275 * a swapchain, so we can't use the swapchain to get a backup dc. To
1276 * make this work windowless contexts would need to be handled by the
1277 * device. */
1278 if (context_gl->c.destroyed || !swapchain_gl)
1280 FIXME("Unable to get backup dc for destroyed context %p.\n", context_gl);
1281 wined3d_context_gl_set_current(NULL);
1282 return FALSE;
1285 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
1287 wined3d_context_gl_set_current(NULL);
1288 return FALSE;
1291 context_gl->dc_is_private = TRUE;
1292 context_gl->dc_has_format = FALSE;
1294 if (!wined3d_context_gl_set_pixel_format(context_gl))
1296 ERR("Failed to set pixel format %d on device context %p.\n",
1297 context_gl->pixel_format, context_gl->dc);
1298 wined3d_context_gl_set_current(NULL);
1299 return FALSE;
1302 if (!wglMakeCurrent(context_gl->dc, context_gl->gl_ctx))
1304 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1305 context_gl->dc, GetLastError());
1306 wined3d_context_gl_set_current(NULL);
1307 return FALSE;
1310 context_gl->valid = 1;
1312 context_gl->needs_set = 0;
1314 return TRUE;
1317 static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx)
1319 if (!wglMakeCurrent(dc, gl_ctx))
1321 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1322 gl_ctx, dc, GetLastError());
1323 wined3d_context_gl_set_current(NULL);
1327 static void wined3d_context_gl_update_window(struct wined3d_context_gl *context_gl)
1329 if (!context_gl->c.swapchain)
1330 return;
1332 if (context_gl->window == context_gl->c.swapchain->win_handle)
1333 return;
1335 TRACE("Updating context %p window from %p to %p.\n",
1336 context_gl, context_gl->window, context_gl->c.swapchain->win_handle);
1338 if (context_gl->dc)
1339 wined3d_release_dc(context_gl->window, context_gl->dc);
1341 context_gl->window = context_gl->c.swapchain->win_handle;
1342 context_gl->dc_is_private = FALSE;
1343 context_gl->dc_has_format = FALSE;
1344 context_gl->needs_set = 1;
1345 context_gl->valid = 1;
1347 if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
1349 ERR("Failed to get a device context for window %p.\n", context_gl->window);
1350 context_gl->valid = 0;
1354 static void wined3d_context_gl_cleanup(struct wined3d_context_gl *context_gl)
1356 struct wined3d_pipeline_statistics_query *pipeline_statistics_query;
1357 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1358 struct wined3d_so_statistics_query *so_statistics_query;
1359 struct wined3d_timestamp_query *timestamp_query;
1360 struct wined3d_occlusion_query *occlusion_query;
1361 struct fbo_entry *entry, *entry2;
1362 struct wined3d_fence *fence;
1363 HGLRC restore_ctx;
1364 HDC restore_dc;
1365 unsigned int i;
1367 restore_ctx = wglGetCurrentContext();
1368 restore_dc = wglGetCurrentDC();
1370 if (restore_ctx == context_gl->gl_ctx)
1371 restore_ctx = NULL;
1372 else if (context_gl->valid)
1373 wined3d_context_gl_set_gl_context(context_gl);
1375 if (context_gl->valid)
1377 wined3d_context_gl_submit_command_fence(context_gl);
1378 wined3d_context_gl_wait_command_fence(context_gl,
1379 wined3d_device_gl(context_gl->c.device)->current_fence_id - 1);
1381 if (context_gl->dummy_arbfp_prog)
1382 GL_EXTCALL(glDeleteProgramsARB(1, &context_gl->dummy_arbfp_prog));
1384 if (context_gl->blit_vbo)
1385 GL_EXTCALL(glDeleteBuffers(1, &context_gl->blit_vbo));
1387 for (i = 0; i < context_gl->free_pipeline_statistics_query_count; ++i)
1389 union wined3d_gl_pipeline_statistics_query *q = &context_gl->free_pipeline_statistics_queries[i];
1390 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1393 for (i = 0; i < context_gl->free_so_statistics_query_count; ++i)
1395 union wined3d_gl_so_statistics_query *q = &context_gl->free_so_statistics_queries[i];
1396 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1399 if (context_gl->free_timestamp_query_count)
1400 GL_EXTCALL(glDeleteQueries(context_gl->free_timestamp_query_count, context_gl->free_timestamp_queries));
1402 if (gl_info->supported[ARB_SYNC])
1404 for (i = 0; i < context_gl->free_fence_count; ++i)
1406 GL_EXTCALL(glDeleteSync(context_gl->free_fences[i].sync));
1409 else if (gl_info->supported[APPLE_FENCE])
1411 for (i = 0; i < context_gl->free_fence_count; ++i)
1413 GL_EXTCALL(glDeleteFencesAPPLE(1, &context_gl->free_fences[i].id));
1416 else if (gl_info->supported[NV_FENCE])
1418 for (i = 0; i < context_gl->free_fence_count; ++i)
1420 GL_EXTCALL(glDeleteFencesNV(1, &context_gl->free_fences[i].id));
1424 if (context_gl->free_occlusion_query_count)
1425 GL_EXTCALL(glDeleteQueries(context_gl->free_occlusion_query_count, context_gl->free_occlusion_queries));
1427 checkGLcall("context cleanup");
1429 heap_free(context_gl->submitted.fences);
1430 heap_free(context_gl->free_pipeline_statistics_queries);
1431 heap_free(context_gl->free_so_statistics_queries);
1432 heap_free(context_gl->free_timestamp_queries);
1433 heap_free(context_gl->free_fences);
1434 heap_free(context_gl->free_occlusion_queries);
1436 LIST_FOR_EACH_ENTRY(pipeline_statistics_query, &context_gl->pipeline_statistics_queries,
1437 struct wined3d_pipeline_statistics_query, entry)
1439 if (context_gl->valid)
1440 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query->u.id), pipeline_statistics_query->u.id));
1441 pipeline_statistics_query->context_gl = NULL;
1444 LIST_FOR_EACH_ENTRY(so_statistics_query, &context_gl->so_statistics_queries,
1445 struct wined3d_so_statistics_query, entry)
1447 if (context_gl->valid)
1448 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query->u.id), so_statistics_query->u.id));
1449 so_statistics_query->context_gl = NULL;
1452 LIST_FOR_EACH_ENTRY(timestamp_query, &context_gl->timestamp_queries, struct wined3d_timestamp_query, entry)
1454 if (context_gl->valid)
1455 GL_EXTCALL(glDeleteQueries(1, &timestamp_query->id));
1456 timestamp_query->context_gl = NULL;
1459 LIST_FOR_EACH_ENTRY(fence, &context_gl->fences, struct wined3d_fence, entry)
1461 if (context_gl->valid)
1463 if (gl_info->supported[ARB_SYNC])
1465 if (fence->object.sync)
1466 GL_EXTCALL(glDeleteSync(fence->object.sync));
1468 else if (gl_info->supported[APPLE_FENCE])
1470 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence->object.id));
1472 else if (gl_info->supported[NV_FENCE])
1474 GL_EXTCALL(glDeleteFencesNV(1, &fence->object.id));
1477 fence->context_gl = NULL;
1480 LIST_FOR_EACH_ENTRY(occlusion_query, &context_gl->occlusion_queries, struct wined3d_occlusion_query, entry)
1482 if (context_gl->valid)
1483 GL_EXTCALL(glDeleteQueries(1, &occlusion_query->id));
1484 occlusion_query->context_gl = NULL;
1487 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_destroy_list, struct fbo_entry, entry)
1489 if (!context_gl->valid)
1490 entry->id = 0;
1491 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1494 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_list, struct fbo_entry, entry)
1496 if (!context_gl->valid)
1497 entry->id = 0;
1498 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1501 heap_free(context_gl->texture_type);
1503 wined3d_context_gl_restore_pixel_format(context_gl);
1504 if (restore_ctx)
1505 context_restore_gl_context(gl_info, restore_dc, restore_ctx);
1506 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1507 ERR("Failed to disable GL context.\n");
1509 wined3d_release_dc(context_gl->window, context_gl->dc);
1511 if (!wglDeleteContext(context_gl->gl_ctx))
1513 DWORD err = GetLastError();
1514 ERR("Failed to delete GL context %p, last error %#x.\n", context_gl->gl_ctx, err);
1517 wined3d_context_cleanup(&context_gl->c);
1520 DWORD context_get_tls_idx(void)
1522 return wined3d_context_tls_idx;
1525 void context_set_tls_idx(DWORD idx)
1527 wined3d_context_tls_idx = idx;
1530 struct wined3d_context_gl *wined3d_context_gl_get_current(void)
1532 return TlsGetValue(wined3d_context_tls_idx);
1535 BOOL wined3d_context_gl_set_current(struct wined3d_context_gl *context_gl)
1537 struct wined3d_context_gl *old = wined3d_context_gl_get_current();
1539 if (old == context_gl)
1541 TRACE("Already using D3D context %p.\n", context_gl);
1542 return TRUE;
1545 if (old)
1547 if (old->c.destroyed)
1549 TRACE("Switching away from destroyed context %p.\n", old);
1550 wined3d_context_gl_cleanup(old);
1551 heap_free((void *)old->gl_info);
1552 heap_free(old);
1554 else
1556 if (wglGetCurrentContext())
1558 const struct wined3d_gl_info *gl_info = old->gl_info;
1559 TRACE("Flushing context %p before switching to %p.\n", old, context_gl);
1560 gl_info->gl_ops.gl.p_glFlush();
1562 old->c.current = 0;
1566 if (context_gl)
1568 if (!context_gl->valid)
1570 ERR("Trying to make invalid context %p current.\n", context_gl);
1571 return FALSE;
1574 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n",
1575 context_gl, context_gl->gl_ctx, context_gl->dc);
1576 if (!wined3d_context_gl_set_gl_context(context_gl))
1577 return FALSE;
1578 context_gl->c.current = 1;
1580 else if (wglGetCurrentContext())
1582 TRACE("Clearing current D3D context.\n");
1583 if (!wglMakeCurrent(NULL, NULL))
1585 DWORD err = GetLastError();
1586 ERR("Failed to clear current GL context, last error %#x.\n", err);
1587 TlsSetValue(wined3d_context_tls_idx, NULL);
1588 return FALSE;
1592 return TlsSetValue(wined3d_context_tls_idx, context_gl);
1595 void wined3d_context_gl_release(struct wined3d_context_gl *context_gl)
1597 TRACE("Releasing context %p, level %u.\n", context_gl, context_gl->level);
1599 if (WARN_ON(d3d))
1601 if (!context_gl->level)
1602 WARN("Context %p is not active.\n", context_gl);
1603 else if (context_gl != wined3d_context_gl_get_current())
1604 WARN("Context %p is not the current context.\n", context_gl);
1607 if (!--context_gl->level)
1609 if (wined3d_context_gl_restore_pixel_format(context_gl))
1610 context_gl->needs_set = 1;
1611 if (context_gl->restore_ctx)
1613 TRACE("Restoring GL context %p on device context %p.\n", context_gl->restore_ctx, context_gl->restore_dc);
1614 context_restore_gl_context(context_gl->gl_info, context_gl->restore_dc, context_gl->restore_ctx);
1615 context_gl->restore_ctx = NULL;
1616 context_gl->restore_dc = NULL;
1619 if (context_gl->c.destroy_delayed)
1621 TRACE("Destroying context %p.\n", context_gl);
1622 wined3d_context_gl_destroy(context_gl);
1627 static void wined3d_context_gl_enter(struct wined3d_context_gl *context_gl)
1629 TRACE("Entering context %p, level %u.\n", context_gl, context_gl->level + 1);
1631 if (!context_gl->level++)
1633 const struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
1634 HGLRC current_gl = wglGetCurrentContext();
1636 if (current_gl && (!current_context || current_context->gl_ctx != current_gl))
1638 TRACE("Another GL context (%p on device context %p) is already current.\n",
1639 current_gl, wglGetCurrentDC());
1640 context_gl->restore_ctx = current_gl;
1641 context_gl->restore_dc = wglGetCurrentDC();
1642 context_gl->needs_set = 1;
1644 else if (!context_gl->needs_set && !(context_gl->dc_is_private && context_gl->dc_has_format)
1645 && context_gl->pixel_format != context_gl->gl_info->gl_ops.wgl.p_wglGetPixelFormat(context_gl->dc))
1646 context_gl->needs_set = 1;
1650 /* This function takes care of wined3d pixel format selection. */
1651 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1652 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1653 BOOL auxBuffers)
1655 unsigned int cfg_count = wined3d_adapter_gl(device->adapter)->pixel_format_count;
1656 unsigned int current_value;
1657 PIXELFORMATDESCRIPTOR pfd;
1658 int iPixelFormat = 0;
1659 unsigned int i;
1661 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1662 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1663 auxBuffers);
1665 current_value = 0;
1666 for (i = 0; i < cfg_count; ++i)
1668 const struct wined3d_pixel_format *cfg = &wined3d_adapter_gl(device->adapter)->pixel_formats[i];
1669 unsigned int value;
1671 /* For now only accept RGBA formats. Perhaps some day we will
1672 * allow floating point formats for pbuffers. */
1673 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1674 continue;
1675 /* In window mode we need a window drawable format and double buffering. */
1676 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1677 continue;
1678 if (cfg->redSize < color_format->red_size)
1679 continue;
1680 if (cfg->greenSize < color_format->green_size)
1681 continue;
1682 if (cfg->blueSize < color_format->blue_size)
1683 continue;
1684 if (cfg->alphaSize < color_format->alpha_size)
1685 continue;
1686 if (cfg->depthSize < ds_format->depth_size)
1687 continue;
1688 if (ds_format->stencil_size && cfg->stencilSize != ds_format->stencil_size)
1689 continue;
1690 /* Check multisampling support. */
1691 if (cfg->numSamples)
1692 continue;
1694 value = 1;
1695 /* We try to locate a format which matches our requirements exactly. In case of
1696 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1697 if (cfg->depthSize == ds_format->depth_size)
1698 value += 1;
1699 if (cfg->stencilSize == ds_format->stencil_size)
1700 value += 2;
1701 if (cfg->alphaSize == color_format->alpha_size)
1702 value += 4;
1703 /* We like to have aux buffers in backbuffer mode */
1704 if (auxBuffers && cfg->auxBuffers)
1705 value += 8;
1706 if (cfg->redSize == color_format->red_size
1707 && cfg->greenSize == color_format->green_size
1708 && cfg->blueSize == color_format->blue_size)
1709 value += 16;
1711 if (value > current_value)
1713 iPixelFormat = cfg->iPixelFormat;
1714 current_value = value;
1718 if (!iPixelFormat)
1720 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1722 memset(&pfd, 0, sizeof(pfd));
1723 pfd.nSize = sizeof(pfd);
1724 pfd.nVersion = 1;
1725 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1726 pfd.iPixelType = PFD_TYPE_RGBA;
1727 pfd.cAlphaBits = color_format->alpha_size;
1728 pfd.cColorBits = color_format->red_size + color_format->green_size
1729 + color_format->blue_size + color_format->alpha_size;
1730 pfd.cDepthBits = ds_format->depth_size;
1731 pfd.cStencilBits = ds_format->stencil_size;
1732 pfd.iLayerType = PFD_MAIN_PLANE;
1734 if (!(iPixelFormat = ChoosePixelFormat(hdc, &pfd)))
1736 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1737 ERR("Can't find a suitable pixel format.\n");
1738 return 0;
1742 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1743 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1744 return iPixelFormat;
1747 /* Context activation is done by the caller. */
1748 void wined3d_context_gl_bind_dummy_textures(const struct wined3d_context_gl *context_gl)
1750 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
1751 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1752 unsigned int i;
1754 for (i = 0; i < gl_info->limits.combined_samplers; ++i)
1756 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + i));
1758 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
1759 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
1761 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1762 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
1764 if (gl_info->supported[EXT_TEXTURE3D])
1765 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
1767 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1768 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
1770 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
1771 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
1773 if (gl_info->supported[EXT_TEXTURE_ARRAY])
1775 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
1776 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
1779 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1780 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
1782 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1784 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
1785 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
1789 checkGLcall("bind dummy textures");
1792 void wined3d_check_gl_call(const struct wined3d_gl_info *gl_info,
1793 const char *file, unsigned int line, const char *name)
1795 GLint err;
1797 if (gl_info->supported[ARB_DEBUG_OUTPUT] || (err = gl_info->gl_ops.gl.p_glGetError()) == GL_NO_ERROR)
1799 TRACE("%s call ok %s / %u.\n", name, file, line);
1800 return;
1805 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1806 debug_glerror(err), err, name, file,line);
1807 err = gl_info->gl_ops.gl.p_glGetError();
1808 } while (err != GL_NO_ERROR);
1811 static BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1813 return gl_info->supported[ARB_DEBUG_OUTPUT]
1814 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1817 static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1818 GLenum severity, GLsizei length, const char *message, void *ctx)
1820 switch (type)
1822 case GL_DEBUG_TYPE_ERROR_ARB:
1823 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1824 break;
1826 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1827 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1828 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1829 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1830 break;
1832 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1833 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1834 break;
1836 default:
1837 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1838 break;
1842 HGLRC context_create_wgl_attribs(const struct wined3d_gl_info *gl_info, HDC hdc, HGLRC share_ctx)
1844 HGLRC ctx;
1845 unsigned int ctx_attrib_idx = 0;
1846 GLint ctx_attribs[7], ctx_flags = 0;
1848 if (context_debug_output_enabled(gl_info))
1849 ctx_flags = WGL_CONTEXT_DEBUG_BIT_ARB;
1850 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
1851 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version >> 16;
1852 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MINOR_VERSION_ARB;
1853 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version & 0xffff;
1854 if (ctx_flags)
1856 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1857 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1859 ctx_attribs[ctx_attrib_idx] = 0;
1861 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1863 if (gl_info->selected_gl_version >= MAKEDWORD_VERSION(3, 2))
1865 if (ctx_flags)
1867 ctx_flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1868 ctx_attribs[ctx_attrib_idx - 1] = ctx_flags;
1870 else
1872 ctx_flags = WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1873 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1874 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1875 ctx_attribs[ctx_attrib_idx] = 0;
1877 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1878 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1879 GetLastError());
1882 return ctx;
1885 static BOOL wined3d_context_gl_create_wgl_ctx(struct wined3d_context_gl *context_gl,
1886 struct wined3d_swapchain_gl *swapchain_gl)
1888 const struct wined3d_format *colour_format, *ds_format;
1889 struct wined3d_context *context = &context_gl->c;
1890 const struct wined3d_gl_info *gl_info;
1891 struct wined3d_resource *target;
1892 struct wined3d_adapter *adapter;
1893 unsigned int target_bind_flags;
1894 struct wined3d_device *device;
1895 HGLRC ctx, share_ctx;
1896 unsigned int i;
1898 device = context->device;
1899 adapter = device->adapter;
1900 gl_info = &adapter->gl_info;
1902 target = &context->current_rt.texture->resource;
1903 target_bind_flags = target->bind_flags;
1905 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1907 static const enum wined3d_format_id ds_formats[] =
1909 WINED3DFMT_D24_UNORM_S8_UINT,
1910 WINED3DFMT_D32_UNORM,
1911 WINED3DFMT_R24_UNORM_X8_TYPELESS,
1912 WINED3DFMT_D16_UNORM,
1913 WINED3DFMT_S1_UINT_D15_UNORM,
1916 colour_format = target->format;
1918 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1919 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1920 if (colour_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1921 colour_format = wined3d_get_format(adapter, WINED3DFMT_B4G4R4A4_UNORM, target_bind_flags);
1922 else if (colour_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1923 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1925 /* DirectDraw supports 8bit paletted render targets and these are used by
1926 * old games like StarCraft and C&C. Most modern hardware doesn't support
1927 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1928 * conversion (ab)uses the alpha component for storing the palette index.
1929 * For this reason we require a format with 8bit alpha, so request
1930 * A8R8G8B8. */
1931 if (colour_format->id == WINED3DFMT_P8_UINT)
1932 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1934 /* Try to find a pixel format which matches our requirements. */
1935 if (!swapchain_gl->s.ds_format)
1937 for (i = 0; i < ARRAY_SIZE(ds_formats); ++i)
1939 ds_format = wined3d_get_format(adapter, ds_formats[i], WINED3D_BIND_DEPTH_STENCIL);
1940 if ((context_gl->pixel_format = context_choose_pixel_format(device,
1941 context_gl->dc, colour_format, ds_format, TRUE)))
1943 swapchain_gl->s.ds_format = ds_format;
1944 break;
1947 TRACE("Depth stencil format %s is not supported, trying next format.\n",
1948 debug_d3dformat(ds_format->id));
1951 else
1953 context_gl->pixel_format = context_choose_pixel_format(device,
1954 context_gl->dc, colour_format, swapchain_gl->s.ds_format, TRUE);
1957 else
1959 /* When using FBOs for off-screen rendering, we only use the drawable for
1960 * presentation blits, and don't do any rendering to it. That means we
1961 * don't need depth or stencil buffers, and can mostly ignore the render
1962 * target format. This wouldn't necessarily be quite correct for 10bpc
1963 * display modes, but we don't currently support those.
1964 * Using the same format regardless of the colour/depth/stencil targets
1965 * makes it much less likely that different wined3d instances will set
1966 * conflicting pixel formats. */
1967 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1968 ds_format = wined3d_get_format(adapter, WINED3DFMT_UNKNOWN, WINED3D_BIND_DEPTH_STENCIL);
1969 context_gl->pixel_format = context_choose_pixel_format(device,
1970 context_gl->dc, colour_format, ds_format, FALSE);
1973 if (!context_gl->pixel_format)
1975 ERR("Failed to choose pixel format.\n");
1976 return FALSE;
1979 wined3d_context_gl_enter(context_gl);
1981 if (!wined3d_context_gl_set_pixel_format(context_gl))
1983 context_release(context);
1985 if (context_gl->dc_is_private)
1987 ERR("Failed to set pixel format %d on device context %p.\n", context_gl->pixel_format, context_gl->dc);
1989 return FALSE;
1992 WARN("Failed to set pixel format %d on device context %p, trying backup DC.\n",
1993 context_gl->pixel_format, context_gl->dc);
1995 wined3d_release_dc(context_gl->window, context_gl->dc);
1996 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
1998 ERR("Failed to retrieve the backup device context.\n");
1999 return FALSE;
2001 context_gl->dc_is_private = TRUE;
2003 return wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl);
2006 share_ctx = device->context_count ? wined3d_context_gl(device->contexts[0])->gl_ctx : NULL;
2007 if (gl_info->p_wglCreateContextAttribsARB)
2009 if (!(ctx = context_create_wgl_attribs(gl_info, context_gl->dc, share_ctx)))
2011 ERR("Failed to create a WGL context.\n");
2012 context_release(context);
2013 return FALSE;
2016 else
2018 if (!(ctx = wglCreateContext(context_gl->dc)))
2020 ERR("Failed to create a WGL context.\n");
2021 context_release(context);
2022 return FALSE;
2025 if (share_ctx && !wglShareLists(share_ctx, ctx))
2027 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
2028 context_release(context);
2029 if (!wglDeleteContext(ctx))
2030 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2031 return FALSE;
2035 context_gl->dc_has_format = TRUE;
2036 context_gl->needs_set = 1;
2037 context_gl->valid = 1;
2038 context_gl->gl_ctx = ctx;
2040 return TRUE;
2043 HRESULT wined3d_context_gl_init(struct wined3d_context_gl *context_gl, struct wined3d_swapchain_gl *swapchain_gl)
2045 struct wined3d_context *context = &context_gl->c;
2046 const struct wined3d_d3d_info *d3d_info;
2047 const struct wined3d_gl_info *gl_info;
2048 struct wined3d_device *device;
2049 unsigned int i;
2051 TRACE("context_gl %p, swapchain %p.\n", context_gl, swapchain_gl);
2053 wined3d_context_init(&context_gl->c, &swapchain_gl->s);
2055 device = context->device;
2056 gl_info = &device->adapter->gl_info;
2057 context_gl->gl_info = gl_info;
2058 d3d_info = context->d3d_info;
2060 context_gl->tid = GetCurrentThreadId();
2061 context_gl->window = context->swapchain->win_handle;
2062 if (context_gl->window == GetDesktopWindow())
2064 TRACE("Swapchain is created on the desktop window, trying backup device context.\n");
2065 context_gl->dc = NULL;
2067 else if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
2068 WARN("Failed to retrieve device context, trying swapchain backup.\n");
2070 if (!context_gl->dc)
2072 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2074 ERR("Failed to retrieve a device context.\n");
2075 return E_FAIL;
2077 context_gl->dc_is_private = TRUE;
2080 list_init(&context_gl->fbo_list);
2081 list_init(&context_gl->fbo_destroy_list);
2083 list_init(&context_gl->occlusion_queries);
2084 list_init(&context_gl->fences);
2085 list_init(&context_gl->timestamp_queries);
2086 list_init(&context_gl->so_statistics_queries);
2087 list_init(&context_gl->pipeline_statistics_queries);
2089 for (i = 0; i < ARRAY_SIZE(context_gl->tex_unit_map); ++i)
2090 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2091 for (i = 0; i < ARRAY_SIZE(context_gl->rev_tex_unit_map); ++i)
2092 context_gl->rev_tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2093 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
2095 /* Initialize the texture unit mapping to a 1:1 mapping. */
2096 unsigned int base, count;
2098 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_PIXEL, &base, &count);
2099 if (base + WINED3D_MAX_FRAGMENT_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2101 ERR("Unexpected texture unit base index %u.\n", base);
2102 goto fail;
2104 for (i = 0; i < min(count, WINED3D_MAX_FRAGMENT_SAMPLERS); ++i)
2106 context_gl->tex_unit_map[i] = base + i;
2107 context_gl->rev_tex_unit_map[base + i] = i;
2110 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_VERTEX, &base, &count);
2111 if (base + WINED3D_MAX_VERTEX_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2113 ERR("Unexpected texture unit base index %u.\n", base);
2114 goto fail;
2116 for (i = 0; i < min(count, WINED3D_MAX_VERTEX_SAMPLERS); ++i)
2118 context_gl->tex_unit_map[WINED3D_MAX_FRAGMENT_SAMPLERS + i] = base + i;
2119 context_gl->rev_tex_unit_map[base + i] = WINED3D_MAX_FRAGMENT_SAMPLERS + i;
2123 if (!(context_gl->texture_type = heap_calloc(gl_info->limits.combined_samplers,
2124 sizeof(*context_gl->texture_type))))
2125 goto fail;
2127 if (!wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl))
2128 goto fail;
2130 /* Set up the context defaults. */
2132 context->render_offscreen = wined3d_resource_is_offscreen(&context->current_rt.texture->resource);
2133 context_gl->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
2135 if (!wined3d_context_gl_set_current(context_gl))
2137 ERR("Cannot activate context to set up defaults.\n");
2138 context_release(context);
2139 if (!wglDeleteContext(context_gl->gl_ctx))
2140 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context_gl->gl_ctx, GetLastError());
2141 goto fail;
2144 if (context_debug_output_enabled(gl_info))
2146 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback, context));
2147 if (TRACE_ON(d3d_synchronous))
2148 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2149 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
2150 if (ERR_ON(d3d))
2152 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR,
2153 GL_DONT_CARE, 0, NULL, GL_TRUE));
2155 if (FIXME_ON(d3d))
2157 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
2158 GL_DONT_CARE, 0, NULL, GL_TRUE));
2159 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
2160 GL_DONT_CARE, 0, NULL, GL_TRUE));
2161 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY,
2162 GL_DONT_CARE, 0, NULL, GL_TRUE));
2164 if (WARN_ON(d3d_perf))
2166 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE,
2167 GL_DONT_CARE, 0, NULL, GL_TRUE));
2171 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2172 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &context_gl->aux_buffers);
2174 TRACE("Setting up the screen\n");
2176 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2178 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
2179 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2181 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
2182 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2184 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2185 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2187 else
2189 GLuint vao;
2191 GL_EXTCALL(glGenVertexArrays(1, &vao));
2192 GL_EXTCALL(glBindVertexArray(vao));
2193 checkGLcall("creating VAO");
2196 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
2197 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2198 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2199 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2201 if (gl_info->supported[NV_TEXTURE_SHADER2])
2203 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2204 * the previous texture where to source the offset from is always unit - 1.
2206 for (i = 1; i < gl_info->limits.textures; ++i)
2208 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2209 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
2210 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + i - 1);
2211 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2214 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2216 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2217 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2218 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2219 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2220 * is ever assigned.
2222 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2223 * program and the dummy program is destroyed when the context is destroyed.
2225 static const char dummy_program[] =
2226 "!!ARBfp1.0\n"
2227 "MOV result.color, fragment.color.primary;\n"
2228 "END\n";
2229 GL_EXTCALL(glGenProgramsARB(1, &context_gl->dummy_arbfp_prog));
2230 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, context_gl->dummy_arbfp_prog));
2231 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
2232 GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2235 if (gl_info->supported[ARB_POINT_SPRITE])
2237 for (i = 0; i < gl_info->limits.textures; ++i)
2239 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2240 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2241 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2245 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2247 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2249 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2251 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2253 if (!(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART))
2255 if (gl_info->supported[ARB_ES3_COMPATIBILITY])
2257 gl_info->gl_ops.gl.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
2258 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2260 else
2262 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2265 if (!(d3d_info->wined3d_creation_flags & WINED3D_LEGACY_CUBEMAP_FILTERING)
2266 && gl_info->supported[ARB_SEAMLESS_CUBE_MAP])
2268 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
2269 checkGLcall("enable seamless cube map filtering");
2271 if (gl_info->supported[ARB_CLIP_CONTROL])
2272 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT));
2274 /* If this happens to be the first context for the device, dummy textures
2275 * are not created yet. In that case, they will be created (and bound) by
2276 * create_dummy_textures right after this context is initialized. */
2277 if (wined3d_device_gl(device)->dummy_textures.tex_2d)
2278 wined3d_context_gl_bind_dummy_textures(context_gl);
2280 /* Initialise all rectangles to avoid resetting unused ones later. */
2281 gl_info->gl_ops.gl.p_glScissor(0, 0, 0, 0);
2282 checkGLcall("glScissor");
2284 return WINED3D_OK;
2286 fail:
2287 heap_free(context_gl->texture_type);
2288 wined3d_release_dc(context_gl->window, context_gl->dc);
2289 return E_FAIL;
2292 void wined3d_context_gl_destroy(struct wined3d_context_gl *context_gl)
2294 struct wined3d_device *device = context_gl->c.device;
2296 TRACE("Destroying context %p.\n", context_gl);
2298 wined3d_from_cs(device->cs);
2300 /* We delay destroying a context when it is active. The context_release()
2301 * function invokes wined3d_context_gl_destroy() again while leaving the
2302 * last level. */
2303 if (context_gl->level)
2305 TRACE("Delaying destruction of context %p.\n", context_gl);
2306 context_gl->c.destroy_delayed = 1;
2307 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2308 context_gl->c.swapchain = NULL;
2309 return;
2312 device_context_remove(device, &context_gl->c);
2314 if (context_gl->c.current && context_gl->tid != GetCurrentThreadId())
2316 struct wined3d_gl_info *gl_info;
2318 /* Make a copy of gl_info for wined3d_context_gl_cleanup() use, the
2319 * one in wined3d_adapter may go away in the meantime. */
2320 gl_info = heap_alloc(sizeof(*gl_info));
2321 *gl_info = *context_gl->gl_info;
2322 context_gl->gl_info = gl_info;
2323 context_gl->c.destroyed = 1;
2325 return;
2328 wined3d_context_gl_cleanup(context_gl);
2329 TlsSetValue(context_get_tls_idx(), NULL);
2330 heap_free(context_gl);
2333 const unsigned int *wined3d_context_gl_get_tex_unit_mapping(const struct wined3d_context_gl *context_gl,
2334 const struct wined3d_shader_version *shader_version, unsigned int *base, unsigned int *count)
2336 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2338 if (!shader_version)
2340 *base = 0;
2341 *count = WINED3D_MAX_TEXTURES;
2342 return context_gl->tex_unit_map;
2345 if (shader_version->major >= 4)
2347 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, shader_version->type, base, count);
2348 return NULL;
2351 switch (shader_version->type)
2353 case WINED3D_SHADER_TYPE_PIXEL:
2354 *base = 0;
2355 *count = WINED3D_MAX_FRAGMENT_SAMPLERS;
2356 break;
2357 case WINED3D_SHADER_TYPE_VERTEX:
2358 *base = WINED3D_MAX_FRAGMENT_SAMPLERS;
2359 *count = WINED3D_MAX_VERTEX_SAMPLERS;
2360 break;
2361 default:
2362 ERR("Unhandled shader type %#x.\n", shader_version->type);
2363 *base = 0;
2364 *count = 0;
2367 return context_gl->tex_unit_map;
2370 static void wined3d_context_gl_get_rt_size(const struct wined3d_context_gl *context_gl, SIZE *size)
2372 const struct wined3d_texture *rt = context_gl->c.current_rt.texture;
2373 unsigned int level;
2375 if (rt->swapchain)
2377 RECT window_size;
2379 GetClientRect(context_gl->window, &window_size);
2380 size->cx = window_size.right - window_size.left;
2381 size->cy = window_size.bottom - window_size.top;
2383 return;
2386 level = context_gl->c.current_rt.sub_resource_idx % rt->level_count;
2387 size->cx = wined3d_texture_get_level_width(rt, level);
2388 size->cy = wined3d_texture_get_level_height(rt, level);
2391 void wined3d_context_gl_enable_clip_distances(struct wined3d_context_gl *context_gl, uint32_t enable_mask)
2393 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2394 unsigned int clip_distance_count, i;
2395 uint32_t disable_mask, current_mask;
2397 clip_distance_count = gl_info->limits.user_clip_distances;
2398 disable_mask = ~enable_mask;
2399 enable_mask &= (1u << clip_distance_count) - 1;
2400 disable_mask &= (1u << clip_distance_count) - 1;
2401 current_mask = context_gl->c.clip_distance_mask;
2402 context_gl->c.clip_distance_mask = enable_mask;
2404 enable_mask &= ~current_mask;
2405 while (enable_mask)
2407 i = wined3d_bit_scan(&enable_mask);
2408 gl_info->gl_ops.gl.p_glEnable(GL_CLIP_DISTANCE0 + i);
2410 disable_mask &= current_mask;
2411 while (disable_mask)
2413 i = wined3d_bit_scan(&disable_mask);
2414 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_DISTANCE0 + i);
2416 checkGLcall("toggle clip distances");
2419 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2421 return rt_mask & (1u << 31);
2424 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2426 return rt_mask & ~(1u << 31);
2429 /* Context activation is done by the caller. */
2430 static void wined3d_context_gl_apply_draw_buffers(struct wined3d_context_gl *context_gl, uint32_t rt_mask)
2432 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2433 GLenum draw_buffers[WINED3D_MAX_RENDER_TARGETS];
2435 if (!rt_mask)
2437 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2439 else if (is_rt_mask_onscreen(rt_mask))
2441 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2443 else
2445 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2447 unsigned int i = 0;
2449 while (rt_mask)
2451 if (rt_mask & 1)
2452 draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2453 else
2454 draw_buffers[i] = GL_NONE;
2456 rt_mask >>= 1;
2457 ++i;
2460 if (gl_info->supported[ARB_DRAW_BUFFERS])
2462 GL_EXTCALL(glDrawBuffers(i, draw_buffers));
2464 else
2466 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffers[0]);
2469 else
2471 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2475 checkGLcall("apply draw buffers");
2478 /* Context activation is done by the caller. */
2479 void wined3d_context_gl_set_draw_buffer(struct wined3d_context_gl *context_gl, GLenum buffer)
2481 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2482 struct fbo_entry *current_fbo = context_gl->current_fbo;
2483 uint32_t new_mask = context_generate_rt_mask(buffer);
2484 uint32_t *current_mask;
2486 current_mask = current_fbo ? &current_fbo->rt_mask : &context_gl->draw_buffers_mask;
2487 if (new_mask == *current_mask)
2488 return;
2490 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2491 checkGLcall("glDrawBuffer()");
2493 *current_mask = new_mask;
2496 /* Context activation is done by the caller. */
2497 void wined3d_context_gl_active_texture(struct wined3d_context_gl *context_gl,
2498 const struct wined3d_gl_info *gl_info, unsigned int unit)
2500 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + unit));
2501 checkGLcall("glActiveTexture");
2502 context_gl->active_texture = unit;
2505 void wined3d_context_gl_bind_bo(struct wined3d_context_gl *context_gl, GLenum binding, GLuint name)
2507 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2509 if (binding == GL_ELEMENT_ARRAY_BUFFER)
2510 context_invalidate_state(&context_gl->c, STATE_INDEXBUFFER);
2512 GL_EXTCALL(glBindBuffer(binding, name));
2515 void wined3d_context_gl_bind_texture(struct wined3d_context_gl *context_gl, GLenum target, GLuint name)
2517 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
2518 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2519 GLenum old_texture_type;
2520 unsigned int unit;
2522 if (name)
2523 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2524 else
2525 target = GL_NONE;
2527 unit = context_gl->active_texture;
2528 old_texture_type = context_gl->texture_type[unit];
2529 if (old_texture_type != target)
2531 switch (old_texture_type)
2533 case GL_NONE:
2534 /* nothing to do */
2535 break;
2536 case GL_TEXTURE_1D:
2537 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
2538 break;
2539 case GL_TEXTURE_1D_ARRAY:
2540 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
2541 break;
2542 case GL_TEXTURE_2D:
2543 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
2544 break;
2545 case GL_TEXTURE_2D_ARRAY:
2546 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
2547 break;
2548 case GL_TEXTURE_RECTANGLE_ARB:
2549 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
2550 break;
2551 case GL_TEXTURE_CUBE_MAP:
2552 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
2553 break;
2554 case GL_TEXTURE_CUBE_MAP_ARRAY:
2555 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
2556 break;
2557 case GL_TEXTURE_3D:
2558 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
2559 break;
2560 case GL_TEXTURE_BUFFER:
2561 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
2562 break;
2563 case GL_TEXTURE_2D_MULTISAMPLE:
2564 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
2565 break;
2566 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2567 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
2568 break;
2569 default:
2570 ERR("Unexpected texture target %#x.\n", old_texture_type);
2573 context_gl->texture_type[unit] = target;
2576 checkGLcall("bind texture");
2579 static void wined3d_context_gl_poll_fences(struct wined3d_context_gl *context_gl)
2581 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2582 struct wined3d_command_fence_gl *f;
2583 SIZE_T i;
2585 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2587 f = &context_gl->submitted.fences[i];
2589 if (f->id > device_gl->completed_fence_id)
2591 if (wined3d_fence_test(f->fence, &device_gl->d, 0) != WINED3D_FENCE_OK)
2592 continue;
2593 device_gl->completed_fence_id = f->id;
2596 wined3d_fence_destroy(f->fence);
2597 if (i != context_gl->submitted.fence_count - 1)
2598 *f = context_gl->submitted.fences[context_gl->submitted.fence_count - 1];
2599 --context_gl->submitted.fence_count;
2603 void wined3d_context_gl_wait_command_fence(struct wined3d_context_gl *context_gl, uint64_t id)
2605 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2606 enum wined3d_fence_result ret;
2607 SIZE_T i;
2609 if (id <= device_gl->completed_fence_id
2610 || id > device_gl->current_fence_id) /* In case the fence ID wrapped. */
2611 return;
2613 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2615 if (context_gl->submitted.fences[i].id != id)
2616 continue;
2618 if ((ret = wined3d_fence_wait(context_gl->submitted.fences[i].fence, &device_gl->d)) != WINED3D_FENCE_OK)
2619 ERR("Failed to wait for command fence with id 0x%s, ret %#x.\n", wine_dbgstr_longlong(id), ret);
2620 wined3d_context_gl_poll_fences(context_gl);
2621 return;
2624 ERR("Failed to find fence for command fence with id 0x%s.\n", wine_dbgstr_longlong(id));
2627 void wined3d_context_gl_submit_command_fence(struct wined3d_context_gl *context_gl)
2629 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2630 struct wined3d_command_fence_gl *f;
2631 HRESULT hr;
2633 if (!wined3d_array_reserve((void **)&context_gl->submitted.fences, &context_gl->submitted.fences_size,
2634 context_gl->submitted.fence_count + 1, sizeof(*context_gl->submitted.fences)))
2635 ERR("Failed to grow submitted command buffer array.\n");
2637 f = &context_gl->submitted.fences[context_gl->submitted.fence_count++];
2638 f->id = device_gl->current_fence_id;
2639 if (FAILED(hr = wined3d_fence_create(&device_gl->d, &f->fence)))
2640 ERR("Failed to create fence, hr %#x.\n", hr);
2641 wined3d_fence_issue(f->fence, &device_gl->d);
2643 /* We don't expect this to ever happen, but handle it anyway. */
2644 if (!++device_gl->current_fence_id)
2646 wined3d_context_gl_wait_command_fence(context_gl, device_gl->current_fence_id - 1);
2647 device_gl->completed_fence_id = 0;
2648 device_gl->current_fence_id = 1;
2650 wined3d_context_gl_poll_fences(context_gl);
2653 void *wined3d_context_gl_map_bo_address(struct wined3d_context_gl *context_gl,
2654 const struct wined3d_bo_address *data, size_t size, uint32_t flags)
2656 const struct wined3d_gl_info *gl_info;
2657 struct wined3d_bo_gl *bo;
2658 BYTE *memory;
2660 if (!(bo = (struct wined3d_bo_gl *)data->buffer_object))
2661 return data->addr;
2663 gl_info = context_gl->gl_info;
2664 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2666 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2668 memory = GL_EXTCALL(glMapBufferRange(bo->binding, (INT_PTR)data->addr,
2669 size, wined3d_resource_gl_map_flags(flags)));
2671 else
2673 memory = GL_EXTCALL(glMapBuffer(bo->binding, wined3d_resource_gl_legacy_map_flags(flags)));
2674 memory += (INT_PTR)data->addr;
2677 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2678 checkGLcall("Map buffer object");
2680 return memory;
2683 void wined3d_context_gl_unmap_bo_address(struct wined3d_context_gl *context_gl,
2684 const struct wined3d_bo_address *data, unsigned int range_count, const struct wined3d_range *ranges)
2686 const struct wined3d_gl_info *gl_info;
2687 struct wined3d_bo_gl *bo;
2688 unsigned int i;
2690 if (!(bo = (struct wined3d_bo_gl *)data->buffer_object))
2691 return;
2693 gl_info = context_gl->gl_info;
2694 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2696 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2698 for (i = 0; i < range_count; ++i)
2700 GL_EXTCALL(glFlushMappedBufferRange(bo->binding,
2701 (UINT_PTR)data->addr + ranges[i].offset, ranges[i].size));
2705 GL_EXTCALL(glUnmapBuffer(bo->binding));
2706 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2707 checkGLcall("Unmap buffer object");
2710 void wined3d_context_gl_copy_bo_address(struct wined3d_context_gl *context_gl,
2711 const struct wined3d_bo_address *dst, const struct wined3d_bo_address *src, size_t size)
2713 const struct wined3d_gl_info *gl_info;
2714 struct wined3d_bo_gl *src_bo, *dst_bo;
2715 struct wined3d_range range;
2716 BYTE *dst_ptr, *src_ptr;
2718 gl_info = context_gl->gl_info;
2719 src_bo = (struct wined3d_bo_gl *)src->buffer_object;
2720 dst_bo = (struct wined3d_bo_gl *)dst->buffer_object;
2722 if (dst_bo && src_bo)
2724 if (gl_info->supported[ARB_COPY_BUFFER])
2726 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER, src_bo->id));
2727 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER, dst_bo->id));
2728 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
2729 (GLintptr)src->addr, (GLintptr)dst->addr, size));
2730 checkGLcall("direct buffer copy");
2732 wined3d_context_gl_reference_bo(context_gl, src_bo);
2733 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2735 else
2737 src_ptr = wined3d_context_gl_map_bo_address(context_gl, src, size, WINED3D_MAP_READ);
2738 dst_ptr = wined3d_context_gl_map_bo_address(context_gl, dst, size, WINED3D_MAP_WRITE);
2740 memcpy(dst_ptr, src_ptr, size);
2742 range.offset = 0;
2743 range.size = size;
2744 wined3d_context_gl_unmap_bo_address(context_gl, dst, 1, &range);
2745 wined3d_context_gl_unmap_bo_address(context_gl, src, 0, NULL);
2748 else if (!dst_bo && src_bo)
2750 wined3d_context_gl_bind_bo(context_gl, src_bo->binding, src_bo->id);
2751 GL_EXTCALL(glGetBufferSubData(src_bo->binding, (GLintptr)src->addr, size, dst->addr));
2752 checkGLcall("buffer download");
2754 wined3d_context_gl_reference_bo(context_gl, src_bo);
2756 else if (dst_bo && !src_bo)
2758 wined3d_context_gl_bind_bo(context_gl, dst_bo->binding, dst_bo->id);
2759 GL_EXTCALL(glBufferSubData(dst_bo->binding, (GLintptr)dst->addr, size, src->addr));
2760 checkGLcall("buffer upload");
2762 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2764 else
2766 memcpy(dst->addr, src->addr, size);
2770 void wined3d_context_gl_destroy_bo(struct wined3d_context_gl *context_gl, struct wined3d_bo_gl *bo)
2772 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2774 TRACE("context_gl %p, bo %p.\n", context_gl, bo);
2776 TRACE("Destroying GL buffer %u.\n", bo->id);
2777 GL_EXTCALL(glDeleteBuffers(1, &bo->id));
2778 checkGLcall("buffer object destruction");
2779 bo->id = 0;
2782 bool wined3d_context_gl_create_bo(struct wined3d_context_gl *context_gl, GLsizeiptr size,
2783 GLenum binding, GLenum usage, bool coherent, GLbitfield flags, struct wined3d_bo_gl *bo)
2785 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2786 GLuint id = 0;
2788 TRACE("context_gl %p, size %lu, binding %#x, usage %#x, coherent %#x, flags %#x, bo %p.\n",
2789 context_gl, size, binding, usage, coherent, flags, bo);
2791 GL_EXTCALL(glGenBuffers(1, &id));
2792 if (!id)
2794 checkGLcall("buffer object creation");
2795 return false;
2797 wined3d_context_gl_bind_bo(context_gl, binding, id);
2799 if (!coherent && gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
2801 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
2802 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
2805 if (gl_info->supported[ARB_BUFFER_STORAGE])
2806 GL_EXTCALL(glBufferStorage(binding, size, NULL, flags | GL_DYNAMIC_STORAGE_BIT));
2807 else
2808 GL_EXTCALL(glBufferData(binding, size, NULL, usage));
2810 wined3d_context_gl_bind_bo(context_gl, binding, 0);
2811 checkGLcall("buffer object creation");
2813 TRACE("Created buffer object %u.\n", id);
2814 bo->id = id;
2815 bo->binding = binding;
2816 bo->usage = usage;
2817 bo->command_fence_id = 0;
2819 return true;
2822 static void wined3d_context_gl_set_render_offscreen(struct wined3d_context_gl *context_gl, BOOL offscreen)
2824 if (context_gl->c.render_offscreen == offscreen)
2825 return;
2827 context_invalidate_state(&context_gl->c, STATE_VIEWPORT);
2828 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
2829 if (!context_gl->gl_info->supported[ARB_CLIP_CONTROL])
2831 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
2832 context_invalidate_state(&context_gl->c, STATE_POINTSPRITECOORDORIGIN);
2833 context_invalidate_state(&context_gl->c, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2835 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN));
2836 if (context_gl->gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2837 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
2838 context_gl->c.render_offscreen = offscreen;
2841 GLenum wined3d_context_gl_get_offscreen_gl_buffer(const struct wined3d_context_gl *context_gl)
2843 switch (wined3d_settings.offscreen_rendering_mode)
2845 case ORM_FBO:
2846 return GL_COLOR_ATTACHMENT0;
2848 case ORM_BACKBUFFER:
2849 return context_gl->aux_buffers > 0 ? GL_AUX0 : GL_BACK;
2851 default:
2852 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
2853 return GL_BACK;
2857 static uint32_t wined3d_context_gl_generate_rt_mask_no_fbo(const struct wined3d_context_gl *context_gl,
2858 struct wined3d_resource *rt)
2860 if (!rt || rt->format->id == WINED3DFMT_NULL)
2861 return 0;
2862 else if (rt->type != WINED3D_RTYPE_BUFFER && texture_from_resource(rt)->swapchain)
2863 return context_generate_rt_mask_from_resource(rt);
2864 else
2865 return context_generate_rt_mask(wined3d_context_gl_get_offscreen_gl_buffer(context_gl));
2868 /* Context activation is done by the caller. */
2869 void wined3d_context_gl_apply_blit_state(struct wined3d_context_gl *context_gl, const struct wined3d_device *device)
2871 struct wined3d_context *context = &context_gl->c;
2872 const struct wined3d_gl_info *gl_info;
2873 uint32_t rt_mask, *cur_mask;
2874 struct wined3d_texture *rt;
2875 unsigned int sampler;
2876 SIZE rt_size;
2878 TRACE("Setting up context %p for blitting.\n", context);
2880 gl_info = context_gl->gl_info;
2881 rt = context->current_rt.texture;
2883 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2885 if (context->render_offscreen)
2887 wined3d_texture_load(rt, context, FALSE);
2889 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_FRAMEBUFFER, &rt->resource,
2890 context->current_rt.sub_resource_idx, NULL, 0, rt->resource.draw_binding);
2891 if (rt->resource.format->id != WINED3DFMT_NULL)
2892 rt_mask = 1;
2893 else
2894 rt_mask = 0;
2896 else
2898 context_gl->current_fbo = NULL;
2899 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
2900 rt_mask = context_generate_rt_mask_from_resource(&rt->resource);
2903 else
2905 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, &rt->resource);
2908 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
2910 if (rt_mask != *cur_mask)
2912 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
2913 *cur_mask = rt_mask;
2916 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2917 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
2918 context_invalidate_state(context, STATE_FRAMEBUFFER);
2920 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
2922 if (context->last_was_blit)
2924 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
2926 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
2927 context->viewport_count = WINED3D_MAX_VIEWPORTS;
2928 context_gl->blit_size = rt_size;
2929 /* No need to dirtify here, the states are still dirtified because
2930 * they weren't applied since the last context_apply_blit_state()
2931 * call. */
2933 checkGLcall("blit state application");
2934 TRACE("Context is already set up for blitting, nothing to do.\n");
2935 return;
2937 context->last_was_blit = TRUE;
2939 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
2940 GL_EXTCALL(glBindSampler(0, 0));
2941 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
2943 sampler = context_gl->rev_tex_unit_map[0];
2944 if (sampler != WINED3D_UNMAPPED_STAGE)
2946 if (sampler < WINED3D_MAX_TEXTURES)
2948 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
2949 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2951 context_invalidate_state(context, STATE_SAMPLER(sampler));
2953 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
2954 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
2956 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2958 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
2959 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
2961 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2962 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2963 context_invalidate_state(context, STATE_BLEND);
2964 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
2965 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
2966 context_invalidate_state(context, STATE_RASTERIZER);
2967 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
2968 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
2969 context_invalidate_state(context, STATE_DEPTH_STENCIL);
2970 if (gl_info->supported[ARB_POINT_SPRITE])
2972 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
2973 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
2975 if (gl_info->supported[ARB_FRAMEBUFFER_SRGB])
2977 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
2978 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
2981 context->last_was_rhw = TRUE;
2982 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
2984 wined3d_context_gl_enable_clip_distances(context_gl, 0);
2985 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
2987 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
2988 if (gl_info->supported[ARB_CLIP_CONTROL])
2989 GL_EXTCALL(glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE));
2990 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
2991 context->viewport_count = WINED3D_MAX_VIEWPORTS;
2992 context_invalidate_state(context, STATE_VIEWPORT);
2994 device->shader_backend->shader_disable(device->shader_priv, context);
2996 context_gl->blit_size = rt_size;
2998 checkGLcall("blit state application");
3001 static void wined3d_context_gl_apply_blit_projection(const struct wined3d_context_gl *context_gl,
3002 unsigned int w, unsigned int h)
3004 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3005 const GLdouble projection[] =
3007 2.0 / w, 0.0, 0.0, 0.0,
3008 0.0, 2.0 / h, 0.0, 0.0,
3009 0.0, 0.0, 2.0, 0.0,
3010 -1.0, -1.0, -1.0, 1.0,
3013 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
3014 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
3017 /* Setup OpenGL states for fixed-function blitting. */
3018 /* Context activation is done by the caller. */
3019 void wined3d_context_gl_apply_ffp_blit_state(struct wined3d_context_gl *context_gl,
3020 const struct wined3d_device *device)
3022 struct wined3d_context *context = &context_gl->c;
3023 const struct wined3d_gl_info *gl_info;
3024 unsigned int i, sampler;
3026 gl_info = context_gl->gl_info;
3027 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3028 ERR("Applying fixed-function state without legacy context support.\n");
3030 if (context->last_was_ffp_blit)
3032 SIZE rt_size;
3034 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
3035 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
3036 wined3d_context_gl_apply_blit_projection(context_gl, rt_size.cx, rt_size.cy);
3037 wined3d_context_gl_apply_blit_state(context_gl, device);
3039 checkGLcall("ffp blit state application");
3040 return;
3042 context->last_was_ffp_blit = TRUE;
3044 wined3d_context_gl_apply_blit_state(context_gl, device);
3046 /* Disable all textures. The caller can then bind a texture it wants to blit
3047 * from. */
3048 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
3050 wined3d_context_gl_active_texture(context_gl, gl_info, i);
3052 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3053 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3054 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3055 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3056 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3057 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3059 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3061 sampler = context_gl->rev_tex_unit_map[i];
3062 if (sampler != WINED3D_UNMAPPED_STAGE)
3064 if (sampler < WINED3D_MAX_TEXTURES)
3065 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
3066 context_invalidate_state(context, STATE_SAMPLER(sampler));
3070 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
3072 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3073 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3074 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3075 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3076 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3077 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3079 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3080 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
3081 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
3083 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
3084 gl_info->gl_ops.gl.p_glLoadIdentity();
3086 /* Setup transforms. */
3087 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3088 gl_info->gl_ops.gl.p_glLoadIdentity();
3089 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3090 wined3d_context_gl_apply_blit_projection(context_gl, context_gl->blit_size.cx, context_gl->blit_size.cy);
3091 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
3093 /* Other misc states. */
3094 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
3095 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
3096 gl_info->p_glDisableWINE(GL_FOG);
3097 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
3099 if (gl_info->supported[EXT_SECONDARY_COLOR])
3101 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
3102 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
3104 checkGLcall("ffp blit state application");
3107 static BOOL have_framebuffer_attachment(unsigned int rt_count, struct wined3d_rendertarget_view * const *rts,
3108 const struct wined3d_rendertarget_view *ds)
3110 unsigned int i;
3112 if (ds)
3113 return TRUE;
3115 for (i = 0; i < rt_count; ++i)
3117 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3118 return TRUE;
3121 return FALSE;
3124 /* Context activation is done by the caller. */
3125 BOOL wined3d_context_gl_apply_clear_state(struct wined3d_context_gl *context_gl,
3126 const struct wined3d_state *state, unsigned int rt_count, const struct wined3d_fb_state *fb)
3128 struct wined3d_rendertarget_view * const *rts = fb->render_targets;
3129 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3130 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
3131 uint32_t rt_mask = 0, *cur_mask;
3132 unsigned int i;
3134 if (isStateDirty(&context_gl->c, STATE_FRAMEBUFFER) || fb != &state->fb
3135 || rt_count != gl_info->limits.buffers)
3137 if (!have_framebuffer_attachment(rt_count, rts, dsv))
3139 WARN("Invalid render target config, need at least one attachment.\n");
3140 return FALSE;
3143 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3145 struct wined3d_rendertarget_info ds_info = {{0}};
3147 if (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource))
3149 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3150 for (i = 0; i < rt_count; ++i)
3152 if (rts[i])
3154 struct wined3d_rendertarget_view_gl *rtv_gl = wined3d_rendertarget_view_gl(rts[i]);
3155 context_gl->blit_targets[i].gl_view = rtv_gl->gl_view;
3156 context_gl->blit_targets[i].resource = rtv_gl->v.resource;
3157 context_gl->blit_targets[i].sub_resource_idx = rtv_gl->v.sub_resource_idx;
3158 context_gl->blit_targets[i].layer_count = rtv_gl->v.layer_count;
3160 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3161 rt_mask |= (1u << i);
3164 if (dsv)
3166 struct wined3d_rendertarget_view_gl *dsv_gl = wined3d_rendertarget_view_gl(dsv);
3167 ds_info.gl_view = dsv_gl->gl_view;
3168 ds_info.resource = dsv_gl->v.resource;
3169 ds_info.sub_resource_idx = dsv_gl->v.sub_resource_idx;
3170 ds_info.layer_count = dsv_gl->v.layer_count;
3173 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3174 rt_count ? rts[0]->resource->draw_binding : 0, dsv ? dsv->resource->draw_binding : 0);
3176 else
3178 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3179 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3180 rt_mask = context_generate_rt_mask_from_resource(rts[0]->resource);
3183 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3184 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3185 * state management allows this */
3186 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3188 else
3190 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3193 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3194 && (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource)))
3196 for (i = 0; i < rt_count; ++i)
3198 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3199 rt_mask |= (1u << i);
3202 else
3204 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3207 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3209 if (rt_mask != *cur_mask)
3211 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3212 *cur_mask = rt_mask;
3213 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3216 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3217 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3219 context_gl->c.last_was_blit = FALSE;
3220 context_gl->c.last_was_ffp_blit = FALSE;
3222 /* Blending and clearing should be orthogonal, but tests on the nvidia
3223 * driver show that disabling blending when clearing improves the clearing
3224 * performance incredibly. */
3225 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3226 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
3227 if (rt_count && gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3229 if (needs_srgb_write(context_gl->c.d3d_info, state, fb))
3230 gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
3231 else
3232 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3233 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3235 checkGLcall("setting up state for clear");
3237 context_invalidate_state(&context_gl->c, STATE_BLEND);
3238 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3239 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
3241 return TRUE;
3244 static uint32_t find_draw_buffers_mask(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3246 struct wined3d_rendertarget_view * const *rts = state->fb.render_targets;
3247 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3248 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3249 unsigned int rt_mask, mask;
3250 unsigned int i;
3252 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3253 return wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rts[0]->resource);
3254 else if (!context_gl->c.render_offscreen)
3255 return context_generate_rt_mask_from_resource(rts[0]->resource);
3257 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
3258 rt_mask &= (1u << gl_info->limits.buffers) - 1;
3259 if (state->blend_state && state->blend_state->dual_source)
3260 rt_mask = 1;
3262 mask = rt_mask;
3263 while (mask)
3265 i = wined3d_bit_scan(&mask);
3266 if (!rts[i] || rts[i]->format->id == WINED3DFMT_NULL)
3267 rt_mask &= ~(1u << i);
3270 return rt_mask;
3273 /* Context activation is done by the caller. */
3274 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3276 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3277 uint32_t rt_mask = find_draw_buffers_mask(context_gl, state);
3278 const struct wined3d_fb_state *fb = &state->fb;
3279 DWORD color_location = 0;
3280 DWORD *cur_mask;
3282 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3284 struct wined3d_rendertarget_info ds_info = {{0}};
3286 if (!context->render_offscreen)
3288 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3289 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3291 else
3293 const struct wined3d_rendertarget_view_gl *view_gl;
3294 unsigned int i;
3296 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3297 for (i = 0; i < context_gl->gl_info->limits.buffers; ++i)
3299 if (!fb->render_targets[i])
3300 continue;
3302 view_gl = wined3d_rendertarget_view_gl(fb->render_targets[i]);
3303 context_gl->blit_targets[i].gl_view = view_gl->gl_view;
3304 context_gl->blit_targets[i].resource = view_gl->v.resource;
3305 context_gl->blit_targets[i].sub_resource_idx = view_gl->v.sub_resource_idx;
3306 context_gl->blit_targets[i].layer_count = view_gl->v.layer_count;
3308 if (!color_location)
3309 color_location = view_gl->v.resource->draw_binding;
3312 if (fb->depth_stencil)
3314 view_gl = wined3d_rendertarget_view_gl(fb->depth_stencil);
3315 ds_info.gl_view = view_gl->gl_view;
3316 ds_info.resource = view_gl->v.resource;
3317 ds_info.sub_resource_idx = view_gl->v.sub_resource_idx;
3318 ds_info.layer_count = view_gl->v.layer_count;
3321 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3322 color_location, fb->depth_stencil ? fb->depth_stencil->resource->draw_binding : 0);
3326 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3327 if (rt_mask != *cur_mask)
3329 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3330 *cur_mask = rt_mask;
3332 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
3335 static void wined3d_context_gl_map_stage(struct wined3d_context_gl *context_gl, unsigned int stage, unsigned int unit)
3337 unsigned int i = context_gl->rev_tex_unit_map[unit];
3338 unsigned int j = context_gl->tex_unit_map[stage];
3340 TRACE("Mapping stage %u to unit %u.\n", stage, unit);
3341 context_gl->tex_unit_map[stage] = unit;
3342 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
3343 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
3345 context_gl->rev_tex_unit_map[unit] = stage;
3346 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
3347 context_gl->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
3350 static void context_invalidate_texture_stage(struct wined3d_context *context, DWORD stage)
3352 DWORD i;
3354 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
3355 context_invalidate_state(context, STATE_TEXTURESTAGE(stage, i));
3358 static void context_update_fixed_function_usage_map(struct wined3d_context *context,
3359 const struct wined3d_state *state)
3361 UINT i, start, end;
3363 context->fixed_function_usage_map = 0;
3364 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
3366 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
3367 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
3368 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
3369 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
3370 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
3371 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
3372 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
3373 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
3375 /* Not used, and disable higher stages. */
3376 if (color_op == WINED3D_TOP_DISABLE)
3377 break;
3379 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
3380 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
3381 || ((color_arg3 == WINED3DTA_TEXTURE)
3382 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
3383 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
3384 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
3385 || ((alpha_arg3 == WINED3DTA_TEXTURE)
3386 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
3387 context->fixed_function_usage_map |= (1u << i);
3389 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
3390 && i < WINED3D_MAX_TEXTURES - 1)
3391 context->fixed_function_usage_map |= (1u << (i + 1));
3394 if (i < context->lowest_disabled_stage)
3396 start = i;
3397 end = context->lowest_disabled_stage;
3399 else
3401 start = context->lowest_disabled_stage;
3402 end = i;
3405 context->lowest_disabled_stage = i;
3406 for (i = start + 1; i < end; ++i)
3408 context_invalidate_state(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3412 static void wined3d_context_gl_map_fixed_function_samplers(struct wined3d_context_gl *context_gl,
3413 const struct wined3d_state *state)
3415 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3416 unsigned int i, tex;
3417 WORD ffu_map;
3419 ffu_map = context_gl->c.fixed_function_usage_map;
3421 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
3422 || context_gl->c.lowest_disabled_stage <= d3d_info->limits.ffp_textures)
3424 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3426 if (!(ffu_map & 1))
3427 continue;
3429 if (context_gl->tex_unit_map[i] != i)
3431 wined3d_context_gl_map_stage(context_gl, i, i);
3432 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3433 context_invalidate_texture_stage(&context_gl->c, i);
3436 return;
3439 /* Now work out the mapping */
3440 tex = 0;
3441 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3443 if (!(ffu_map & 1))
3444 continue;
3446 if (context_gl->tex_unit_map[i] != tex)
3448 wined3d_context_gl_map_stage(context_gl, i, tex);
3449 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3450 context_invalidate_texture_stage(&context_gl->c, i);
3453 ++tex;
3457 static void wined3d_context_gl_map_psamplers(struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3459 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3460 const struct wined3d_shader_resource_info *resource_info =
3461 state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3462 unsigned int i;
3464 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
3466 if (resource_info[i].type && context_gl->tex_unit_map[i] != i)
3468 wined3d_context_gl_map_stage(context_gl, i, i);
3469 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3470 if (i < d3d_info->limits.ffp_blend_stages)
3471 context_invalidate_texture_stage(&context_gl->c, i);
3476 static BOOL wined3d_context_gl_unit_free_for_vs(const struct wined3d_context_gl *context_gl,
3477 const struct wined3d_shader_resource_info *ps_resource_info, unsigned int unit)
3479 unsigned int current_mapping = context_gl->rev_tex_unit_map[unit];
3481 /* Not currently used */
3482 if (current_mapping == WINED3D_UNMAPPED_STAGE)
3483 return TRUE;
3485 if (current_mapping < WINED3D_MAX_FRAGMENT_SAMPLERS)
3487 /* Used by a fragment sampler */
3489 if (!ps_resource_info)
3491 /* No pixel shader, check fixed function */
3492 return current_mapping >= WINED3D_MAX_TEXTURES
3493 || !(context_gl->c.fixed_function_usage_map & (1u << current_mapping));
3496 /* Pixel shader, check the shader's sampler map */
3497 return !ps_resource_info[current_mapping].type;
3500 return TRUE;
3503 static void wined3d_context_gl_map_vsamplers(struct wined3d_context_gl *context_gl,
3504 BOOL ps, const struct wined3d_state *state)
3506 const struct wined3d_shader_resource_info *vs_resource_info =
3507 state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info;
3508 const struct wined3d_shader_resource_info *ps_resource_info = NULL;
3509 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3510 int start = min(WINED3D_MAX_COMBINED_SAMPLERS, gl_info->limits.graphics_samplers) - 1;
3511 int i;
3513 /* Note that we only care if a resource is used or not, not the
3514 * resource's specific type. Otherwise we'd need to call
3515 * shader_update_samplers() here for 1.x pixelshaders. */
3516 if (ps)
3517 ps_resource_info = state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3519 for (i = 0; i < WINED3D_MAX_VERTEX_SAMPLERS; ++i)
3521 DWORD vsampler_idx = i + WINED3D_MAX_FRAGMENT_SAMPLERS;
3522 if (vs_resource_info[i].type)
3524 while (start >= 0)
3526 if (wined3d_context_gl_unit_free_for_vs(context_gl, ps_resource_info, start))
3528 if (context_gl->tex_unit_map[vsampler_idx] != start)
3530 wined3d_context_gl_map_stage(context_gl, vsampler_idx, start);
3531 context_invalidate_state(&context_gl->c, STATE_SAMPLER(vsampler_idx));
3534 --start;
3535 break;
3538 --start;
3540 if (context_gl->tex_unit_map[vsampler_idx] == WINED3D_UNMAPPED_STAGE)
3541 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i);
3546 static void wined3d_context_gl_update_tex_unit_map(struct wined3d_context_gl *context_gl,
3547 const struct wined3d_state *state)
3549 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3550 BOOL vs = use_vs(state);
3551 BOOL ps = use_ps(state);
3553 if (!ps)
3554 context_update_fixed_function_usage_map(&context_gl->c, state);
3556 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3557 * need a 1:1 map at the moment.
3558 * When the mapping of a stage is changed, sampler and ALL texture stage
3559 * states have to be reset. */
3561 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
3562 return;
3564 if (ps)
3565 wined3d_context_gl_map_psamplers(context_gl, state);
3566 else
3567 wined3d_context_gl_map_fixed_function_samplers(context_gl, state);
3569 if (vs)
3570 wined3d_context_gl_map_vsamplers(context_gl, ps, state);
3573 /* Context activation is done by the caller. */
3574 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3576 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3577 uint32_t rt_mask, *cur_mask;
3579 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
3581 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3582 rt_mask = find_draw_buffers_mask(context_gl, state);
3583 if (rt_mask != *cur_mask)
3585 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3586 *cur_mask = rt_mask;
3590 static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl *context_gl,
3591 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
3593 unsigned int bind_idx, shader_sampler_count, base, count, i;
3594 const struct wined3d_device *device = context_gl->c.device;
3595 struct wined3d_shader_sampler_map_entry *entry;
3596 struct wined3d_shader_resource_view *view;
3597 const struct wined3d_shader *shader;
3598 const unsigned int *tex_unit_map;
3599 struct wined3d_sampler *sampler;
3601 if (!(shader = state->shader[shader_type]))
3602 return;
3604 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl,
3605 &shader->reg_maps.shader_version, &base, &count);
3607 shader_sampler_count = shader->reg_maps.sampler_map.count;
3608 if (shader_sampler_count > count)
3609 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3610 shader, shader_sampler_count, count);
3611 count = min(shader_sampler_count, count);
3613 for (i = 0; i < count; ++i)
3615 entry = &shader->reg_maps.sampler_map.entries[i];
3616 bind_idx = base + entry->bind_idx;
3617 if (tex_unit_map)
3618 bind_idx = tex_unit_map[bind_idx];
3620 if (!(view = state->shader_resource_view[shader_type][entry->resource_idx]))
3622 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
3623 continue;
3626 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
3627 sampler = device->default_sampler;
3628 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
3629 sampler = device->null_sampler;
3630 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view),
3631 bind_idx, wined3d_sampler_gl(sampler), context_gl);
3635 static void wined3d_context_gl_bind_unordered_access_views(struct wined3d_context_gl *context_gl,
3636 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3638 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3639 struct wined3d_unordered_access_view_gl *view_gl;
3640 const struct wined3d_format_gl *format_gl;
3641 GLuint texture_name;
3642 unsigned int i;
3643 GLint level;
3645 if (!shader)
3646 return;
3648 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3650 if (!views[i])
3652 if (shader->reg_maps.uav_resource_info[i].type)
3653 WARN("No unordered access view bound at index %u.\n", i);
3654 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3655 continue;
3658 view_gl = wined3d_unordered_access_view_gl(views[i]);
3659 if (view_gl->gl_view.name)
3661 texture_name = view_gl->gl_view.name;
3662 level = 0;
3664 else if (view_gl->v.resource->type != WINED3D_RTYPE_BUFFER)
3666 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(view_gl->v.resource));
3667 texture_name = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, FALSE);
3668 level = view_gl->v.desc.u.texture.level_idx;
3670 else
3672 FIXME("Unsupported buffer unordered access view.\n");
3673 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3674 continue;
3677 format_gl = wined3d_format_gl(view_gl->v.format);
3678 GL_EXTCALL(glBindImageTexture(i, texture_name, level, GL_TRUE, 0, GL_READ_WRITE,
3679 format_gl->internal));
3681 if (view_gl->counter_bo.id)
3682 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, i, view_gl->counter_bo.id));
3684 checkGLcall("Bind unordered access views");
3687 static void context_gl_load_shader_resources(struct wined3d_context_gl *context_gl,
3688 const struct wined3d_state *state, unsigned int shader_mask)
3690 struct wined3d_shader_sampler_map_entry *entry;
3691 struct wined3d_shader_resource_view *view;
3692 struct wined3d_buffer_gl *buffer_gl;
3693 struct wined3d_shader *shader;
3694 unsigned int i, j;
3696 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
3698 if (!(shader_mask & (1u << i)))
3699 continue;
3701 if (!(shader = state->shader[i]))
3702 continue;
3704 for (j = 0; j < WINED3D_MAX_CBS; ++j)
3706 if (!state->cb[i][j])
3707 continue;
3709 buffer_gl = wined3d_buffer_gl(state->cb[i][j]);
3710 wined3d_buffer_load(&buffer_gl->b, &context_gl->c, state);
3711 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3714 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
3716 entry = &shader->reg_maps.sampler_map.entries[j];
3718 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
3719 continue;
3721 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3723 buffer_gl = wined3d_buffer_gl(buffer_from_resource(view->resource));
3724 wined3d_buffer_load(&buffer_gl->b, &context_gl->c, state);
3725 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3727 else
3729 wined3d_texture_load(texture_from_resource(view->resource), &context_gl->c, FALSE);
3735 static void context_gl_load_unordered_access_resources(struct wined3d_context_gl *context_gl,
3736 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3738 struct wined3d_unordered_access_view *view;
3739 struct wined3d_buffer_gl *buffer_gl;
3740 struct wined3d_texture *texture;
3741 unsigned int i;
3743 context_gl->c.uses_uavs = 0;
3745 if (!shader)
3746 return;
3748 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3750 if (!(view = views[i]))
3751 continue;
3753 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3755 buffer_gl = wined3d_buffer_gl(buffer_from_resource(view->resource));
3756 wined3d_buffer_load_location(&buffer_gl->b, &context_gl->c, WINED3D_LOCATION_BUFFER);
3757 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
3758 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3760 else
3762 texture = texture_from_resource(view->resource);
3763 wined3d_texture_load(texture, &context_gl->c, FALSE);
3764 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_TEXTURE_RGB);
3767 context_gl->c.uses_uavs = 1;
3771 static void context_gl_load_stream_output_buffers(struct wined3d_context_gl *context_gl,
3772 const struct wined3d_state *state)
3774 unsigned int i;
3776 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3778 struct wined3d_buffer_gl *buffer_gl;
3780 if (!state->stream_output[i].buffer)
3781 continue;
3783 buffer_gl = wined3d_buffer_gl(state->stream_output[i].buffer);
3784 wined3d_buffer_load(&buffer_gl->b, &context_gl->c, state);
3785 wined3d_buffer_invalidate_location(&buffer_gl->b, ~WINED3D_LOCATION_BUFFER);
3786 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3790 /* Context activation is done by the caller. */
3791 static BOOL context_apply_draw_state(struct wined3d_context *context,
3792 const struct wined3d_device *device, const struct wined3d_state *state, BOOL indexed)
3794 const struct wined3d_state_entry *state_table = context->state_table;
3795 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3796 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3797 const struct wined3d_fb_state *fb = &state->fb;
3798 unsigned int i, base;
3799 uint32_t map;
3801 context->uses_fbo_attached_resources = 0;
3803 if (!have_framebuffer_attachment(gl_info->limits.buffers, fb->render_targets, fb->depth_stencil))
3805 if (!gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
3807 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
3808 return FALSE;
3811 wined3d_context_gl_set_render_offscreen(context_gl, TRUE);
3814 /* Preload resources before FBO setup. Texture preload in particular may
3815 * result in changes to the current FBO, due to using e.g. FBO blits for
3816 * updating a resource location. */
3817 wined3d_context_gl_update_tex_unit_map(context_gl, state);
3818 context_preload_textures(context, state);
3819 context_gl_load_shader_resources(context_gl, state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
3820 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_PIXEL],
3821 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3822 context_gl_load_stream_output_buffers(context_gl, state);
3823 /* TODO: Right now the dependency on the vertex shader is necessary
3824 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3825 * the current VS but maybe it's possible to relax the coupling in some
3826 * situations at least. */
3827 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC)
3828 || isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
3830 context_update_stream_info(context, state);
3833 map = context->stream_info.use_map;
3834 while (map)
3836 const struct wined3d_stream_info_element *e;
3837 struct wined3d_buffer_gl *buffer_gl;
3839 e = &context->stream_info.elements[wined3d_bit_scan(&map)];
3840 buffer_gl = wined3d_buffer_gl(state->streams[e->stream_idx].buffer);
3842 wined3d_buffer_load(&buffer_gl->b, context, state);
3843 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3845 /* Loading the buffers above may have invalidated the stream info. */
3846 if (wined3d_context_is_graphics_state_dirty(context, STATE_STREAMSRC))
3847 context_update_stream_info(context, state);
3849 if (indexed && state->index_buffer)
3851 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(state->index_buffer);
3853 if (context->stream_info.all_vbo)
3855 wined3d_buffer_load(&buffer_gl->b, context, state);
3856 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3858 else
3860 wined3d_buffer_load_sysmem(&buffer_gl->b, context);
3864 for (i = 0, base = 0; i < ARRAY_SIZE(context->dirty_graphics_states); ++i)
3866 uint32_t dirty_mask = context->dirty_graphics_states[i];
3868 while (dirty_mask)
3870 unsigned int state_id = base + wined3d_bit_scan(&dirty_mask);
3872 state_table[state_id].apply(context, state, state_id);
3873 context->dirty_graphics_states[i] &= ~(1u << (state_id - base));
3875 base += sizeof(dirty_mask) * CHAR_BIT;
3878 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
3880 device->shader_backend->shader_select(device->shader_priv, context, state);
3881 context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3884 if (context->constant_update_mask)
3886 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
3887 context->constant_update_mask = 0;
3890 if (context->update_shader_resource_bindings)
3892 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
3893 wined3d_context_gl_bind_shader_resources(context_gl, state, i);
3894 context->update_shader_resource_bindings = 0;
3895 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
3896 context->update_compute_shader_resource_bindings = 1;
3899 if (context->update_unordered_access_view_bindings)
3901 wined3d_context_gl_bind_unordered_access_views(context_gl,
3902 state->shader[WINED3D_SHADER_TYPE_PIXEL],
3903 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3904 context->update_unordered_access_view_bindings = 0;
3905 context->update_compute_unordered_access_view_bindings = 1;
3908 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3909 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3911 context->last_was_blit = FALSE;
3912 context->last_was_ffp_blit = FALSE;
3914 return TRUE;
3917 static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl *context_gl,
3918 const struct wined3d_device *device, const struct wined3d_state *state)
3920 const struct wined3d_state_entry *state_table = context_gl->c.state_table;
3921 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3922 unsigned int state_id, i;
3924 context_gl_load_shader_resources(context_gl, state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
3925 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
3926 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
3928 for (i = 0, state_id = STATE_COMPUTE_OFFSET; i < ARRAY_SIZE(context_gl->c.dirty_compute_states); ++i)
3930 unsigned int dirty_mask = context_gl->c.dirty_compute_states[i];
3932 while (dirty_mask)
3934 unsigned int current_state_id = state_id + wined3d_bit_scan(&dirty_mask);
3935 state_table[current_state_id].apply(&context_gl->c, state, current_state_id);
3937 state_id += sizeof(*context_gl->c.dirty_compute_states) * CHAR_BIT;
3939 memset(context_gl->c.dirty_compute_states, 0, sizeof(*context_gl->c.dirty_compute_states));
3941 if (context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
3943 device->shader_backend->shader_select_compute(device->shader_priv, &context_gl->c, state);
3944 context_gl->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
3947 if (context_gl->c.update_compute_shader_resource_bindings)
3949 wined3d_context_gl_bind_shader_resources(context_gl, state, WINED3D_SHADER_TYPE_COMPUTE);
3950 context_gl->c.update_compute_shader_resource_bindings = 0;
3951 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
3952 context_gl->c.update_shader_resource_bindings = 1;
3955 if (context_gl->c.update_compute_unordered_access_view_bindings)
3957 wined3d_context_gl_bind_unordered_access_views(context_gl,
3958 state->shader[WINED3D_SHADER_TYPE_COMPUTE],
3959 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
3960 context_gl->c.update_compute_unordered_access_view_bindings = 0;
3961 context_gl->c.update_unordered_access_view_bindings = 1;
3964 /* Updates to currently bound render targets aren't necessarily coherent
3965 * between the graphics and compute pipelines. Unbind any currently bound
3966 * FBO here to ensure preceding updates to its attachments by the graphics
3967 * pipeline are visible to the compute pipeline.
3969 * Without this, the bloom effect in Nier:Automata is too bright on the
3970 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
3971 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
3972 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3974 context_gl->c.last_was_blit = FALSE;
3975 context_gl->c.last_was_ffp_blit = FALSE;
3978 void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl *context_gl)
3980 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3982 if (context_gl->c.transform_feedback_active)
3984 GL_EXTCALL(glEndTransformFeedback());
3985 checkGLcall("glEndTransformFeedback");
3986 context_gl->c.transform_feedback_active = 0;
3987 context_gl->c.transform_feedback_paused = 0;
3991 static void wined3d_context_gl_pause_transform_feedback(struct wined3d_context_gl *context_gl, BOOL force)
3993 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3995 if (!context_gl->c.transform_feedback_active || context_gl->c.transform_feedback_paused)
3996 return;
3998 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK2])
4000 GL_EXTCALL(glPauseTransformFeedback());
4001 checkGLcall("glPauseTransformFeedback");
4002 context_gl->c.transform_feedback_paused = 1;
4003 return;
4006 WARN("Cannot pause transform feedback operations.\n");
4008 if (force)
4009 wined3d_context_gl_end_transform_feedback(context_gl);
4012 static void wined3d_context_gl_setup_target(struct wined3d_context_gl *context_gl,
4013 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4015 BOOL old_render_offscreen = context_gl->c.render_offscreen, render_offscreen;
4017 render_offscreen = wined3d_resource_is_offscreen(&texture->resource);
4018 if (context_gl->c.current_rt.texture == texture
4019 && context_gl->c.current_rt.sub_resource_idx == sub_resource_idx
4020 && render_offscreen == old_render_offscreen)
4021 return;
4023 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4024 * the alpha blend state changes with different render target formats. */
4025 if (!context_gl->c.current_rt.texture)
4027 context_invalidate_state(&context_gl->c, STATE_BLEND);
4029 else
4031 const struct wined3d_format *old = context_gl->c.current_rt.texture->resource.format;
4032 const struct wined3d_format *new = texture->resource.format;
4034 if (old->id != new->id)
4036 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4037 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
4038 || !(texture->resource.format_flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
4039 context_invalidate_state(&context_gl->c, STATE_BLEND);
4041 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
4042 if ((context_gl->c.current_rt.texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE)
4043 != (texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE))
4044 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
4047 /* When switching away from an offscreen render target, and we're not
4048 * using FBOs, we have to read the drawable into the texture. This is
4049 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4050 * There are some things that need care though. PreLoad needs a GL context,
4051 * and FindContext is called before the context is activated. It also
4052 * has to be called with the old rendertarget active, otherwise a
4053 * wrong drawable is read. */
4054 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4055 && old_render_offscreen && (context_gl->c.current_rt.texture != texture
4056 || context_gl->c.current_rt.sub_resource_idx != sub_resource_idx))
4058 struct wined3d_texture_gl *prev_texture = wined3d_texture_gl(context_gl->c.current_rt.texture);
4059 unsigned int prev_sub_resource_idx = context_gl->c.current_rt.sub_resource_idx;
4061 /* Read the back buffer of the old drawable into the destination texture. */
4062 if (prev_texture->texture_srgb.name)
4063 wined3d_texture_load(&prev_texture->t, &context_gl->c, TRUE);
4064 wined3d_texture_load(&prev_texture->t, &context_gl->c, FALSE);
4065 wined3d_texture_invalidate_location(&prev_texture->t, prev_sub_resource_idx, WINED3D_LOCATION_DRAWABLE);
4069 context_gl->c.current_rt.texture = texture;
4070 context_gl->c.current_rt.sub_resource_idx = sub_resource_idx;
4071 wined3d_context_gl_set_render_offscreen(context_gl, render_offscreen);
4074 static void wined3d_context_gl_activate(struct wined3d_context_gl *context_gl,
4075 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4077 wined3d_context_gl_enter(context_gl);
4078 wined3d_context_gl_update_window(context_gl);
4079 wined3d_context_gl_setup_target(context_gl, texture, sub_resource_idx);
4080 if (!context_gl->valid)
4081 return;
4083 if (context_gl != wined3d_context_gl_get_current())
4085 if (!wined3d_context_gl_set_current(context_gl))
4086 ERR("Failed to activate the new context.\n");
4088 else if (context_gl->needs_set)
4090 wined3d_context_gl_set_gl_context(context_gl);
4094 struct wined3d_context *wined3d_context_gl_acquire(const struct wined3d_device *device,
4095 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4097 struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
4098 struct wined3d_context_gl *context_gl;
4100 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device, texture, sub_resource_idx);
4102 if (current_context && current_context->c.destroyed)
4103 current_context = NULL;
4105 if (!texture)
4107 if (current_context
4108 && current_context->c.current_rt.texture
4109 && current_context->c.device == device)
4111 texture = current_context->c.current_rt.texture;
4112 sub_resource_idx = current_context->c.current_rt.sub_resource_idx;
4114 else
4116 struct wined3d_swapchain *swapchain = device->swapchains[0];
4118 if (swapchain->back_buffers)
4119 texture = swapchain->back_buffers[0];
4120 else
4121 texture = swapchain->front_buffer;
4122 sub_resource_idx = 0;
4126 if (current_context && current_context->c.current_rt.texture == texture)
4128 context_gl = current_context;
4130 else if (!wined3d_resource_is_offscreen(&texture->resource))
4132 TRACE("Rendering onscreen.\n");
4134 if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(texture->swapchain))))
4135 return NULL;
4137 else
4139 TRACE("Rendering offscreen.\n");
4141 /* Stay with the current context if possible. Otherwise use the
4142 * context for the primary swapchain. */
4143 if (current_context && current_context->c.device == device)
4144 context_gl = current_context;
4145 else if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(device->swapchains[0]))))
4146 return NULL;
4149 wined3d_context_gl_activate(context_gl, texture, sub_resource_idx);
4151 return &context_gl->c;
4154 struct wined3d_context_gl *wined3d_context_gl_reacquire(struct wined3d_context_gl *context_gl)
4156 struct wined3d_context *acquired_context;
4157 struct wined3d_device *device;
4159 if (!context_gl || context_gl->tid != GetCurrentThreadId())
4160 return NULL;
4162 device = context_gl->c.device;
4163 wined3d_from_cs(device->cs);
4165 if (context_gl->c.current_rt.texture)
4167 wined3d_context_gl_activate(context_gl, context_gl->c.current_rt.texture,
4168 context_gl->c.current_rt.sub_resource_idx);
4169 return context_gl;
4172 acquired_context = context_acquire(device, NULL, 0);
4173 if (acquired_context != &context_gl->c)
4174 ERR("Acquired context %p instead of %p.\n", acquired_context, &context_gl->c);
4175 return wined3d_context_gl(acquired_context);
4178 void dispatch_compute(struct wined3d_device *device, const struct wined3d_state *state,
4179 const struct wined3d_dispatch_parameters *parameters)
4181 const struct wined3d_gl_info *gl_info;
4182 struct wined3d_context_gl *context_gl;
4184 context_gl = wined3d_context_gl(context_acquire(device, NULL, 0));
4185 if (!context_gl->valid)
4187 context_release(&context_gl->c);
4188 WARN("Invalid context, skipping dispatch.\n");
4189 return;
4191 gl_info = context_gl->gl_info;
4193 if (!gl_info->supported[ARB_COMPUTE_SHADER])
4195 context_release(&context_gl->c);
4196 FIXME("OpenGL implementation does not support compute shaders.\n");
4197 return;
4200 if (parameters->indirect)
4201 wined3d_buffer_load(parameters->u.indirect.buffer, &context_gl->c, state);
4203 wined3d_context_gl_apply_compute_state(context_gl, device, state);
4205 if (parameters->indirect)
4207 const struct wined3d_indirect_dispatch_parameters *indirect = &parameters->u.indirect;
4208 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(indirect->buffer);
4210 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, buffer_gl->bo.id));
4211 GL_EXTCALL(glDispatchComputeIndirect((GLintptr)indirect->offset));
4212 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0));
4213 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
4215 else
4217 const struct wined3d_direct_dispatch_parameters *direct = &parameters->u.direct;
4218 GL_EXTCALL(glDispatchCompute(direct->group_count_x, direct->group_count_y, direct->group_count_z));
4220 checkGLcall("dispatch compute");
4222 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4223 checkGLcall("glMemoryBarrier");
4225 context_release(&context_gl->c);
4228 /* Context activation is done by the caller. */
4229 static void wined3d_context_gl_draw_primitive_arrays(struct wined3d_context_gl *context_gl,
4230 const struct wined3d_state *state, const void *idx_data, unsigned int idx_size, int base_vertex_idx,
4231 unsigned int start_idx, unsigned int count, unsigned int start_instance, unsigned int instance_count)
4233 const struct wined3d_ffp_attrib_ops *ops = &context_gl->c.d3d_info->ffp_attrib_ops;
4234 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4235 const struct wined3d_stream_info *si = &context_gl->c.stream_info;
4236 GLenum mode = gl_primitive_type_from_d3d(state->primitive_type);
4237 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4238 unsigned int instanced_elements[ARRAY_SIZE(si->elements)];
4239 unsigned int instanced_element_count = 0;
4240 const void *indices;
4241 unsigned int i, j;
4243 indices = (const char *)idx_data + idx_size * start_idx;
4245 if (!instance_count)
4247 if (!idx_size)
4249 gl_info->gl_ops.gl.p_glDrawArrays(mode, start_idx, count);
4250 checkGLcall("glDrawArrays");
4251 return;
4254 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4256 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4257 checkGLcall("glDrawElementsBaseVertex");
4258 return;
4261 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4262 checkGLcall("glDrawElements");
4263 return;
4266 if (start_instance && !(gl_info->supported[ARB_BASE_INSTANCE] && gl_info->supported[ARB_INSTANCED_ARRAYS]))
4267 FIXME("Start instance (%u) not supported.\n", start_instance);
4269 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
4271 if (!idx_size)
4273 if (gl_info->supported[ARB_BASE_INSTANCE])
4275 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode, start_idx, count, instance_count, start_instance));
4276 checkGLcall("glDrawArraysInstancedBaseInstance");
4277 return;
4280 GL_EXTCALL(glDrawArraysInstanced(mode, start_idx, count, instance_count));
4281 checkGLcall("glDrawArraysInstanced");
4282 return;
4285 if (gl_info->supported[ARB_BASE_INSTANCE])
4287 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode, count, idx_type,
4288 indices, instance_count, base_vertex_idx, start_instance));
4289 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4290 return;
4292 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4294 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode, count, idx_type,
4295 indices, instance_count, base_vertex_idx));
4296 checkGLcall("glDrawElementsInstancedBaseVertex");
4297 return;
4300 GL_EXTCALL(glDrawElementsInstanced(mode, count, idx_type, indices, instance_count));
4301 checkGLcall("glDrawElementsInstanced");
4302 return;
4305 /* Instancing emulation by mixing immediate mode and arrays. */
4307 /* This is a nasty thing. MSDN says no hardware supports this and
4308 * applications have to use software vertex processing. We don't support
4309 * this for now.
4311 * Shouldn't be too hard to support with OpenGL, in theory just call
4312 * glDrawArrays() instead of drawElements(). But the stream frequency value
4313 * has a different meaning in that situation. */
4314 if (!idx_size)
4316 FIXME("Non-indexed instanced drawing is not supported.\n");
4317 return;
4320 for (i = 0; i < ARRAY_SIZE(si->elements); ++i)
4322 if (!(si->use_map & (1u << i)))
4323 continue;
4325 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
4326 instanced_elements[instanced_element_count++] = i;
4329 for (i = 0; i < instance_count; ++i)
4331 /* Specify the instanced attributes using immediate mode calls. */
4332 for (j = 0; j < instanced_element_count; ++j)
4334 const struct wined3d_stream_info_element *element;
4335 unsigned int element_idx;
4336 const BYTE *ptr;
4338 element_idx = instanced_elements[j];
4339 element = &si->elements[element_idx];
4340 ptr = element->data.addr + element->stride * i;
4341 if (element->data.buffer_object)
4342 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(state->streams[element->stream_idx].buffer,
4343 &context_gl->c);
4344 ops->generic[element->format->emit_idx](element_idx, ptr);
4347 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4349 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4350 checkGLcall("glDrawElementsBaseVertex");
4352 else
4354 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4355 checkGLcall("glDrawElements");
4360 static unsigned int get_stride_idx(const void *idx_data, unsigned int idx_size,
4361 unsigned int base_vertex_idx, unsigned int start_idx, unsigned int vertex_idx)
4363 if (!idx_data)
4364 return start_idx + vertex_idx;
4365 if (idx_size == 2)
4366 return ((const WORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4367 return ((const DWORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4370 /* Context activation is done by the caller. */
4371 static void draw_primitive_immediate_mode(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4372 const struct wined3d_stream_info *si, const void *idx_data, unsigned int idx_size,
4373 int base_vertex_idx, unsigned int start_idx, unsigned int vertex_count, unsigned int instance_count)
4375 const BYTE *position = NULL, *normal = NULL, *diffuse = NULL, *specular = NULL;
4376 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
4377 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4378 unsigned int coord_idx, stride_idx, texture_idx, vertex_idx;
4379 const struct wined3d_stream_info_element *element;
4380 const BYTE *tex_coords[WINED3DDP_MAXTEXCOORD];
4381 unsigned int texture_unit, texture_stages;
4382 const struct wined3d_ffp_attrib_ops *ops;
4383 unsigned int untracked_material_count;
4384 unsigned int tex_mask = 0;
4385 BOOL specular_fog = FALSE;
4386 BOOL ps = use_ps(state);
4387 const void *ptr;
4389 static unsigned int once;
4391 if (!once++)
4392 FIXME_(d3d_perf)("Drawing using immediate mode.\n");
4393 else
4394 WARN_(d3d_perf)("Drawing using immediate mode.\n");
4396 if (!idx_size && idx_data)
4397 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4399 if (instance_count)
4400 FIXME("Instancing not implemented.\n");
4402 /* Immediate mode drawing can't make use of indices in a VBO - get the
4403 * data from the index buffer. */
4404 if (idx_size)
4405 idx_data = wined3d_buffer_load_sysmem(state->index_buffer, &context_gl->c) + state->index_offset;
4407 ops = &d3d_info->ffp_attrib_ops;
4409 gl_info->gl_ops.gl.p_glBegin(gl_primitive_type_from_d3d(state->primitive_type));
4411 if (use_vs(state) || d3d_info->ffp_generic_attributes)
4413 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4415 unsigned int use_map = si->use_map;
4416 unsigned int element_idx;
4418 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4419 for (element_idx = MAX_ATTRIBS - 1; use_map; use_map &= ~(1u << element_idx), --element_idx)
4421 if (!(use_map & 1u << element_idx))
4422 continue;
4424 ptr = si->elements[element_idx].data.addr + si->elements[element_idx].stride * stride_idx;
4425 ops->generic[si->elements[element_idx].format->emit_idx](element_idx, ptr);
4429 gl_info->gl_ops.gl.p_glEnd();
4430 return;
4433 if (si->use_map & (1u << WINED3D_FFP_POSITION))
4434 position = si->elements[WINED3D_FFP_POSITION].data.addr;
4436 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
4437 normal = si->elements[WINED3D_FFP_NORMAL].data.addr;
4438 else
4439 gl_info->gl_ops.gl.p_glNormal3f(0.0f, 0.0f, 0.0f);
4441 untracked_material_count = context_gl->untracked_material_count;
4442 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
4444 element = &si->elements[WINED3D_FFP_DIFFUSE];
4445 diffuse = element->data.addr;
4447 if (untracked_material_count && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
4448 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element->format->id));
4450 else
4452 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4455 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
4457 element = &si->elements[WINED3D_FFP_SPECULAR];
4458 specular = element->data.addr;
4460 /* Special case where the fog density is stored in the specular alpha channel. */
4461 if (state->render_states[WINED3D_RS_FOGENABLE]
4462 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
4463 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
4464 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
4466 if (gl_info->supported[EXT_FOG_COORD])
4468 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
4469 specular_fog = TRUE;
4470 else
4471 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element->format->id));
4473 else
4475 static unsigned int once;
4477 if (!once++)
4478 FIXME("Implement fog for transformed vertices in software.\n");
4482 else if (gl_info->supported[EXT_SECONDARY_COLOR])
4484 GL_EXTCALL(glSecondaryColor3fEXT)(0.0f, 0.0f, 0.0f);
4487 texture_stages = d3d_info->limits.ffp_blend_stages;
4488 for (texture_idx = 0; texture_idx < texture_stages; ++texture_idx)
4490 if (!gl_info->supported[ARB_MULTITEXTURE] && texture_idx > 0)
4492 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4493 continue;
4496 if (!ps && !state->textures[texture_idx])
4497 continue;
4499 texture_unit = context_gl->tex_unit_map[texture_idx];
4500 if (texture_unit == WINED3D_UNMAPPED_STAGE)
4501 continue;
4503 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4504 if (coord_idx > 7)
4506 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx, texture_idx);
4507 continue;
4510 if (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))
4512 tex_coords[coord_idx] = si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].data.addr;
4513 tex_mask |= (1u << texture_idx);
4515 else
4517 TRACE("Setting default coordinates for texture %u.\n", texture_idx);
4518 if (gl_info->supported[ARB_MULTITEXTURE])
4519 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_unit, 0.0f, 0.0f, 0.0f, 1.0f));
4520 else
4521 gl_info->gl_ops.gl.p_glTexCoord4f(0.0f, 0.0f, 0.0f, 1.0f);
4525 /* Blending data and point sizes are not supported by this function. They
4526 * are not supported by the fixed function pipeline at all. A FIXME for
4527 * them is printed after decoding the vertex declaration. */
4528 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4530 unsigned int tmp_tex_mask;
4532 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4534 if (normal)
4536 ptr = normal + stride_idx * si->elements[WINED3D_FFP_NORMAL].stride;
4537 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptr);
4540 if (diffuse)
4542 ptr = diffuse + stride_idx * si->elements[WINED3D_FFP_DIFFUSE].stride;
4543 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptr);
4545 if (untracked_material_count)
4547 struct wined3d_color color;
4548 unsigned int i;
4550 wined3d_color_from_d3dcolor(&color, *(const DWORD *)ptr);
4551 for (i = 0; i < untracked_material_count; ++i)
4553 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK,
4554 context_gl->untracked_materials[i], &color.r);
4559 if (specular)
4561 ptr = specular + stride_idx * si->elements[WINED3D_FFP_SPECULAR].stride;
4562 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptr);
4564 if (specular_fog)
4565 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD *)ptr >> 24)));
4568 tmp_tex_mask = tex_mask;
4569 for (texture_idx = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture_idx)
4571 if (!(tmp_tex_mask & 1))
4572 continue;
4574 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4575 ptr = tex_coords[coord_idx] + (stride_idx * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
4576 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
4577 GL_TEXTURE0_ARB + context_gl->tex_unit_map[texture_idx], ptr);
4580 if (position)
4582 ptr = position + stride_idx * si->elements[WINED3D_FFP_POSITION].stride;
4583 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptr);
4587 gl_info->gl_ops.gl.p_glEnd();
4588 checkGLcall("draw immediate mode");
4591 static void wined3d_context_gl_draw_indirect(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4592 const struct wined3d_indirect_draw_parameters *parameters, unsigned int idx_size)
4594 GLenum gl_primitive_type = gl_primitive_type_from_d3d(state->primitive_type);
4595 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(parameters->buffer);
4596 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4597 const void *offset;
4599 if (!gl_info->supported[ARB_DRAW_INDIRECT])
4601 FIXME("OpenGL implementation does not support indirect draws.\n");
4602 return;
4605 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer_gl->bo.id));
4607 offset = (void *)(GLintptr)parameters->offset;
4608 if (idx_size)
4610 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4611 if (state->index_offset)
4612 FIXME("Ignoring index offset %u.\n", state->index_offset);
4613 GL_EXTCALL(glDrawElementsIndirect(gl_primitive_type, idx_type, offset));
4615 else
4617 GL_EXTCALL(glDrawArraysIndirect(gl_primitive_type, offset));
4620 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0));
4621 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
4623 checkGLcall("draw indirect");
4626 static void remove_vbos(struct wined3d_context *context,
4627 const struct wined3d_state *state, struct wined3d_stream_info *s)
4629 unsigned int i;
4631 for (i = 0; i < ARRAY_SIZE(s->elements); ++i)
4633 struct wined3d_stream_info_element *e;
4635 if (!(s->use_map & (1u << i)))
4636 continue;
4638 e = &s->elements[i];
4639 if (e->data.buffer_object)
4641 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
4642 e->data.buffer_object = 0;
4643 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(vb, context);
4648 static GLenum gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
4650 GLenum gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4651 switch (gl_primitive_type)
4653 case GL_POINTS:
4654 return GL_POINTS;
4656 case GL_LINE_STRIP:
4657 case GL_LINE_STRIP_ADJACENCY:
4658 case GL_LINES_ADJACENCY:
4659 case GL_LINES:
4660 return GL_LINES;
4662 case GL_TRIANGLE_FAN:
4663 case GL_TRIANGLE_STRIP:
4664 case GL_TRIANGLE_STRIP_ADJACENCY:
4665 case GL_TRIANGLES_ADJACENCY:
4666 case GL_TRIANGLES:
4667 return GL_TRIANGLES;
4669 default:
4670 return gl_primitive_type;
4674 /* Routine common to the draw primitive and draw indexed primitive routines */
4675 void draw_primitive(struct wined3d_device *device, const struct wined3d_state *state,
4676 const struct wined3d_draw_parameters *parameters)
4678 BOOL emulation = FALSE, rasterizer_discard = FALSE;
4679 const struct wined3d_fb_state *fb = &state->fb;
4680 const struct wined3d_stream_info *stream_info;
4681 struct wined3d_rendertarget_view *dsv, *rtv;
4682 struct wined3d_stream_info si_emulated;
4683 const struct wined3d_gl_info *gl_info;
4684 struct wined3d_context_gl *context_gl;
4685 struct wined3d_context *context;
4686 unsigned int i, idx_size = 0;
4687 const void *idx_data = NULL;
4689 TRACE("device %p, state %p, parameters %p.\n", device, state, parameters);
4691 if (!parameters->indirect && !parameters->u.direct.index_count)
4692 return;
4694 if (!parameters->indirect)
4695 TRACE("base_vertex_idx %d, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
4696 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
4697 parameters->u.direct.index_count, parameters->u.direct.start_instance,
4698 parameters->u.direct.instance_count);
4700 if (!(rtv = fb->render_targets[0]))
4701 rtv = fb->depth_stencil;
4703 if (rtv && rtv->resource->type == WINED3D_RTYPE_BUFFER)
4705 FIXME("Buffer render targets not implemented.\n");
4706 return;
4709 if (rtv)
4710 context = context_acquire(device, wined3d_texture_from_resource(rtv->resource), rtv->sub_resource_idx);
4711 else
4712 context = context_acquire(device, NULL, 0);
4713 context_gl = wined3d_context_gl(context);
4714 if (!context_gl->valid)
4716 context_release(context);
4717 WARN("Invalid context, skipping draw.\n");
4718 return;
4720 gl_info = context_gl->gl_info;
4722 if (!use_transform_feedback(state))
4723 wined3d_context_gl_pause_transform_feedback(context_gl, TRUE);
4725 for (i = 0; i < gl_info->limits.buffers; ++i)
4727 if (!(rtv = fb->render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
4728 continue;
4730 if (wined3d_blend_state_get_writemask(state->blend_state, i))
4732 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
4733 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
4735 else
4737 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
4741 if ((dsv = fb->depth_stencil))
4743 /* Note that this depends on the context_acquire() call above to set
4744 * context->render_offscreen properly. We don't currently take the
4745 * Z-compare function into account, but we could skip loading the
4746 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
4747 * that we never copy the stencil data.*/
4748 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4750 if (wined3d_state_uses_depth_buffer(state))
4751 wined3d_rendertarget_view_load_location(dsv, context, location);
4752 else
4753 wined3d_rendertarget_view_prepare_location(dsv, context, location);
4756 if (parameters->indirect)
4757 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
4759 if (!context_apply_draw_state(context, device, state, parameters->indexed))
4761 context_release(context);
4762 WARN("Unable to apply draw state, skipping draw.\n");
4763 return;
4766 if (dsv && (!state->depth_stencil_state || state->depth_stencil_state->desc.depth_write))
4768 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4770 wined3d_rendertarget_view_validate_location(dsv, location);
4771 wined3d_rendertarget_view_invalidate_location(dsv, ~location);
4774 stream_info = &context->stream_info;
4776 if (parameters->indexed)
4778 struct wined3d_buffer *index_buffer = state->index_buffer;
4779 if (!index_buffer->buffer_object || !stream_info->all_vbo)
4780 idx_data = index_buffer->resource.heap_memory;
4781 else
4782 idx_data = NULL;
4783 idx_data = (const BYTE *)idx_data + state->index_offset;
4785 if (state->index_format == WINED3DFMT_R16_UINT)
4786 idx_size = 2;
4787 else
4788 idx_size = 4;
4791 if (!use_vs(state))
4793 if (!stream_info->position_transformed && context_gl->untracked_material_count
4794 && state->render_states[WINED3D_RS_LIGHTING])
4796 static BOOL warned;
4798 if (!warned++)
4799 FIXME("Using software emulation because not all material properties could be tracked.\n");
4800 else
4801 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
4802 emulation = TRUE;
4804 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
4806 static BOOL warned;
4808 /* Either write a pipeline replacement shader or convert the
4809 * specular alpha from unsigned byte to a float in the vertex
4810 * buffer. */
4811 if (!warned++)
4812 FIXME("Using software emulation because manual fog coordinates are provided.\n");
4813 else
4814 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
4815 emulation = TRUE;
4818 if (emulation)
4820 si_emulated = context->stream_info;
4821 remove_vbos(context, state, &si_emulated);
4822 stream_info = &si_emulated;
4826 if (use_transform_feedback(state))
4828 const struct wined3d_shader *shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
4830 if (is_rasterization_disabled(shader))
4832 glEnable(GL_RASTERIZER_DISCARD);
4833 checkGLcall("enable rasterizer discard");
4834 rasterizer_discard = TRUE;
4837 if (context->transform_feedback_paused)
4839 GL_EXTCALL(glResumeTransformFeedback());
4840 checkGLcall("glResumeTransformFeedback");
4841 context->transform_feedback_paused = 0;
4843 else if (!context->transform_feedback_active)
4845 enum wined3d_primitive_type primitive_type = shader->u.gs.output_type
4846 ? shader->u.gs.output_type : state->primitive_type;
4847 GLenum mode = gl_tfb_primitive_type_from_d3d(primitive_type);
4848 GL_EXTCALL(glBeginTransformFeedback(mode));
4849 checkGLcall("glBeginTransformFeedback");
4850 context->transform_feedback_active = 1;
4854 if (state->primitive_type == WINED3D_PT_PATCH)
4856 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES, state->patch_vertex_count));
4857 checkGLcall("glPatchParameteri");
4860 if (context->uses_fbo_attached_resources)
4862 static unsigned int fixme_once;
4864 if (gl_info->supported[ARB_TEXTURE_BARRIER])
4866 GL_EXTCALL(glTextureBarrier());
4868 else if (gl_info->supported[NV_TEXTURE_BARRIER])
4870 GL_EXTCALL(glTextureBarrierNV());
4872 else
4874 if (!fixme_once++)
4875 FIXME("Sampling attached render targets is not supported.\n");
4877 WARN("Sampling attached render targets is not supported, skipping draw.\n");
4878 context_release(context);
4879 return;
4881 checkGLcall("glTextureBarrier");
4884 if (parameters->indirect)
4886 if (!context->use_immediate_mode_draw && !emulation)
4887 wined3d_context_gl_draw_indirect(context_gl, state, &parameters->u.indirect, idx_size);
4888 else
4889 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
4891 else
4893 unsigned int instance_count = parameters->u.direct.instance_count;
4894 if (context->instance_count)
4895 instance_count = context->instance_count;
4897 if (context->use_immediate_mode_draw || emulation)
4898 draw_primitive_immediate_mode(wined3d_context_gl(context), state, stream_info, idx_data,
4899 idx_size, parameters->u.direct.base_vertex_idx,
4900 parameters->u.direct.start_idx, parameters->u.direct.index_count, instance_count);
4901 else
4902 wined3d_context_gl_draw_primitive_arrays(context_gl, state, idx_data, idx_size,
4903 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
4904 parameters->u.direct.index_count, parameters->u.direct.start_instance, instance_count);
4907 if (context->uses_uavs)
4909 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4910 checkGLcall("glMemoryBarrier");
4913 wined3d_context_gl_pause_transform_feedback(context_gl, FALSE);
4915 if (rasterizer_discard)
4917 glDisable(GL_RASTERIZER_DISCARD);
4918 checkGLcall("disable rasterizer discard");
4921 context_release(context);
4923 TRACE("Draw completed.\n");
4926 void wined3d_context_gl_unload_tex_coords(const struct wined3d_context_gl *context_gl)
4928 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4929 unsigned int texture_idx;
4931 for (texture_idx = 0; texture_idx < gl_info->limits.texture_coords; ++texture_idx)
4933 gl_info->gl_ops.ext.p_glClientActiveTextureARB(GL_TEXTURE0_ARB + texture_idx);
4934 gl_info->gl_ops.gl.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4938 void wined3d_context_gl_load_tex_coords(const struct wined3d_context_gl *context_gl,
4939 const struct wined3d_stream_info *si, GLuint *current_bo, const struct wined3d_state *state)
4941 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4942 const struct wined3d_format_gl *format_gl;
4943 unsigned int mapped_stage = 0;
4944 unsigned int texture_idx;
4945 GLuint bo;
4947 for (texture_idx = 0; texture_idx < context_gl->c.d3d_info->limits.ffp_blend_stages; ++texture_idx)
4949 unsigned int coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4951 if ((mapped_stage = context_gl->tex_unit_map[texture_idx]) == WINED3D_UNMAPPED_STAGE)
4952 continue;
4954 if (mapped_stage >= gl_info->limits.texture_coords)
4956 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage);
4957 continue;
4960 if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
4962 const struct wined3d_stream_info_element *e = &si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx];
4964 TRACE("Setting up texture %u, idx %u, coord_idx %u, data %s.\n",
4965 texture_idx, mapped_stage, coord_idx, debug_bo_address(&e->data));
4967 bo = wined3d_bo_gl_id(e->data.buffer_object);
4968 if (*current_bo != bo)
4970 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
4971 checkGLcall("glBindBuffer");
4972 *current_bo = bo;
4975 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
4976 checkGLcall("glClientActiveTextureARB");
4978 /* The coords to supply depend completely on the fvf/vertex shader. */
4979 format_gl = wined3d_format_gl(e->format);
4980 gl_info->gl_ops.gl.p_glTexCoordPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
4981 e->data.addr + state->load_base_vertex_index * e->stride);
4982 gl_info->gl_ops.gl.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4984 else
4986 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + mapped_stage, 0, 0, 0, 1));
4989 if (gl_info->supported[NV_REGISTER_COMBINERS])
4991 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
4992 for (texture_idx = mapped_stage + 1; texture_idx < gl_info->limits.textures; ++texture_idx)
4994 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
4998 checkGLcall("loadTexCoords");
5001 /* This should match any arrays loaded in wined3d_context_gl_load_vertex_data(). */
5002 static void wined3d_context_gl_unload_vertex_data(struct wined3d_context_gl *context_gl)
5004 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5006 if (!context_gl->c.namedArraysLoaded)
5007 return;
5008 gl_info->gl_ops.gl.p_glDisableClientState(GL_VERTEX_ARRAY);
5009 gl_info->gl_ops.gl.p_glDisableClientState(GL_NORMAL_ARRAY);
5010 gl_info->gl_ops.gl.p_glDisableClientState(GL_COLOR_ARRAY);
5011 if (gl_info->supported[EXT_SECONDARY_COLOR])
5012 gl_info->gl_ops.gl.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5013 wined3d_context_gl_unload_tex_coords(context_gl);
5014 context_gl->c.namedArraysLoaded = FALSE;
5017 static void wined3d_context_gl_load_vertex_data(struct wined3d_context_gl *context_gl,
5018 const struct wined3d_stream_info *si, const struct wined3d_state *state)
5020 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5021 const struct wined3d_stream_info_element *e;
5022 const struct wined3d_format_gl *format_gl;
5023 GLuint current_bo, bo;
5025 TRACE("context_gl %p, si %p, state %p.\n", context_gl, si, state);
5027 /* This is used for the fixed-function pipeline only, and the
5028 * fixed-function pipeline doesn't do instancing. */
5029 context_gl->c.instance_count = 0;
5030 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5032 /* Blend data */
5033 if ((si->use_map & (1u << WINED3D_FFP_BLENDWEIGHT))
5034 || si->use_map & (1u << WINED3D_FFP_BLENDINDICES))
5036 /* TODO: Support vertex blending in immediate mode draws. No need to
5037 * write a FIXME here, this is done after the general vertex
5038 * declaration decoding. */
5039 WARN("Vertex blending not supported.\n");
5042 /* Point Size */
5043 if (si->use_map & (1u << WINED3D_FFP_PSIZE))
5045 /* No such functionality in the fixed-function GL pipeline. */
5046 WARN("Per-vertex point size not supported.\n");
5049 /* Position */
5050 if (si->use_map & (1u << WINED3D_FFP_POSITION))
5052 e = &si->elements[WINED3D_FFP_POSITION];
5053 format_gl = wined3d_format_gl(e->format);
5055 bo = wined3d_bo_gl_id(e->data.buffer_object);
5056 if (current_bo != bo)
5058 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5059 checkGLcall("glBindBuffer");
5060 current_bo = bo;
5063 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n",
5064 format_gl->vtx_format, format_gl->vtx_type, e->stride,
5065 e->data.addr + state->load_base_vertex_index * e->stride);
5066 gl_info->gl_ops.gl.p_glVertexPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5067 e->data.addr + state->load_base_vertex_index * e->stride);
5068 checkGLcall("glVertexPointer(...)");
5069 gl_info->gl_ops.gl.p_glEnableClientState(GL_VERTEX_ARRAY);
5070 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5073 /* Normals */
5074 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
5076 e = &si->elements[WINED3D_FFP_NORMAL];
5077 format_gl = wined3d_format_gl(e->format);
5079 bo = wined3d_bo_gl_id(e->data.buffer_object);
5080 if (current_bo != bo)
5082 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5083 checkGLcall("glBindBuffer");
5084 current_bo = bo;
5087 TRACE("glNormalPointer(%#x, %#x, %p);\n", format_gl->vtx_type, e->stride,
5088 e->data.addr + state->load_base_vertex_index * e->stride);
5089 gl_info->gl_ops.gl.p_glNormalPointer(format_gl->vtx_type, e->stride,
5090 e->data.addr + state->load_base_vertex_index * e->stride);
5091 checkGLcall("glNormalPointer(...)");
5092 gl_info->gl_ops.gl.p_glEnableClientState(GL_NORMAL_ARRAY);
5093 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5096 else
5098 gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
5099 checkGLcall("glNormal3f(0, 0, 0)");
5102 /* Diffuse colour */
5103 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
5105 e = &si->elements[WINED3D_FFP_DIFFUSE];
5106 format_gl = wined3d_format_gl(e->format);
5108 bo = wined3d_bo_gl_id(e->data.buffer_object);
5109 if (current_bo != bo)
5111 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5112 checkGLcall("glBindBuffer");
5113 current_bo = bo;
5116 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5117 format_gl->vtx_format, format_gl->vtx_type, e->stride,
5118 e->data.addr + state->load_base_vertex_index * e->stride);
5119 gl_info->gl_ops.gl.p_glColorPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5120 e->data.addr + state->load_base_vertex_index * e->stride);
5121 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5122 gl_info->gl_ops.gl.p_glEnableClientState(GL_COLOR_ARRAY);
5123 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5126 else
5128 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
5129 checkGLcall("glColor4f(1, 1, 1, 1)");
5132 /* Specular colour */
5133 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
5135 TRACE("Setting specular colour.\n");
5137 e = &si->elements[WINED3D_FFP_SPECULAR];
5139 if (gl_info->supported[EXT_SECONDARY_COLOR])
5141 GLint format;
5142 GLenum type;
5144 format_gl = wined3d_format_gl(e->format);
5145 type = format_gl->vtx_type;
5146 format = format_gl->vtx_format;
5148 bo = wined3d_bo_gl_id(e->data.buffer_object);
5149 if (current_bo != bo)
5151 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5152 checkGLcall("glBindBuffer");
5153 current_bo = bo;
5156 if (format != 4 || (gl_info->quirks & WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA))
5158 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5159 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5160 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5161 * 4 component secondary colors use it
5163 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format, type, e->stride,
5164 e->data.addr + state->load_base_vertex_index * e->stride);
5165 GL_EXTCALL(glSecondaryColorPointerEXT(format, type, e->stride,
5166 e->data.addr + state->load_base_vertex_index * e->stride));
5167 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5169 else
5171 switch (type)
5173 case GL_UNSIGNED_BYTE:
5174 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e->stride,
5175 e->data.addr + state->load_base_vertex_index * e->stride);
5176 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, e->stride,
5177 e->data.addr + state->load_base_vertex_index * e->stride));
5178 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5179 break;
5181 default:
5182 FIXME("Add 4 component specular colour pointers for type %#x.\n", type);
5183 /* Make sure that the right colour component is dropped. */
5184 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type, e->stride,
5185 e->data.addr + state->load_base_vertex_index * e->stride);
5186 GL_EXTCALL(glSecondaryColorPointerEXT(3, type, e->stride,
5187 e->data.addr + state->load_base_vertex_index * e->stride));
5188 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5191 gl_info->gl_ops.gl.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5192 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5194 else
5196 WARN("Specular colour is not supported in this GL implementation.\n");
5199 else
5201 if (gl_info->supported[EXT_SECONDARY_COLOR])
5203 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
5204 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5206 else
5208 WARN("Specular colour is not supported in this GL implementation.\n");
5212 /* Texture coordinates */
5213 wined3d_context_gl_load_tex_coords(context_gl, si, &current_bo, state);
5216 static void wined3d_context_gl_unload_numbered_array(struct wined3d_context_gl *context_gl, unsigned int i)
5218 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5220 GL_EXTCALL(glDisableVertexAttribArray(i));
5221 checkGLcall("glDisableVertexAttribArray");
5222 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5223 GL_EXTCALL(glVertexAttribDivisor(i, 0));
5225 context_gl->c.numbered_array_mask &= ~(1u << i);
5228 static void wined3d_context_gl_unload_numbered_arrays(struct wined3d_context_gl *context_gl)
5230 uint32_t mask = context_gl->c.numbered_array_mask;
5231 unsigned int i;
5233 while (mask)
5235 i = wined3d_bit_scan(&mask);
5236 wined3d_context_gl_unload_numbered_array(context_gl, i);
5240 static void wined3d_context_gl_load_numbered_arrays(struct wined3d_context_gl *context_gl,
5241 const struct wined3d_stream_info *stream_info, const struct wined3d_state *state)
5243 struct wined3d_context *context = &context_gl->c;
5244 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5245 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5246 GLuint current_bo, bo;
5247 unsigned int i;
5249 /* Default to no instancing. */
5250 context->instance_count = 0;
5251 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5253 for (i = 0; i < MAX_ATTRIBS; ++i)
5255 const struct wined3d_stream_info_element *element = &stream_info->elements[i];
5256 const struct wined3d_stream_state *stream;
5257 const struct wined3d_format_gl *format_gl;
5259 if (!(stream_info->use_map & (1u << i)))
5261 if (context->numbered_array_mask & (1u << i))
5262 wined3d_context_gl_unload_numbered_array(context_gl, i);
5263 if (!use_vs(state) && i == WINED3D_FFP_DIFFUSE)
5265 if (!(context_gl->default_attrib_value_set & (1u << i)) || !context_gl->diffuse_attrib_to_1)
5267 GL_EXTCALL(glVertexAttrib4f(i, 1.0f, 1.0f, 1.0f, 1.0f));
5268 context_gl->diffuse_attrib_to_1 = 1;
5271 else
5273 if (!(context_gl->default_attrib_value_set & (1u << i)))
5275 GL_EXTCALL(glVertexAttrib4f(i, 0.0f, 0.0f, 0.0f, 0.0f));
5276 if (i == WINED3D_FFP_DIFFUSE)
5277 context_gl->diffuse_attrib_to_1 = 0;
5280 context_gl->default_attrib_value_set |= 1u << i;
5281 continue;
5284 format_gl = wined3d_format_gl(element->format);
5285 stream = &state->streams[element->stream_idx];
5287 if ((stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA) && !context->instance_count)
5288 context->instance_count = state->streams[0].frequency;
5290 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5292 GL_EXTCALL(glVertexAttribDivisor(i, element->divisor));
5294 else if (element->divisor)
5296 /* Unload instanced arrays, they will be loaded using immediate
5297 * mode instead. */
5298 if (context->numbered_array_mask & (1u << i))
5299 wined3d_context_gl_unload_numbered_array(context_gl, i);
5300 context_gl->default_attrib_value_set &= ~(1u << i);
5301 continue;
5304 TRACE("Loading array %u %s.\n", i, debug_bo_address(&element->data));
5306 if (element->stride)
5308 DWORD format_flags = format_gl->f.flags[WINED3D_GL_RES_TYPE_BUFFER];
5310 bo = wined3d_bo_gl_id(element->data.buffer_object);
5311 if (current_bo != bo)
5313 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5314 checkGLcall("glBindBuffer");
5315 current_bo = bo;
5317 /* Use the VBO to find out if a vertex buffer exists, not the vb
5318 * pointer. vb can point to a user pointer data blob. In that case
5319 * current_bo will be 0. If there is a vertex buffer but no vbo we
5320 * won't be load converted attributes anyway. */
5321 if (vs && vs->reg_maps.shader_version.major >= 4 && (format_flags & WINED3DFMT_FLAG_INTEGER))
5323 GL_EXTCALL(glVertexAttribIPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5324 element->stride, element->data.addr + state->load_base_vertex_index * element->stride));
5326 else
5328 GL_EXTCALL(glVertexAttribPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5329 !!(format_flags & WINED3DFMT_FLAG_NORMALISED), element->stride,
5330 element->data.addr + state->load_base_vertex_index * element->stride));
5333 if (!(context->numbered_array_mask & (1u << i)))
5335 GL_EXTCALL(glEnableVertexAttribArray(i));
5336 context->numbered_array_mask |= (1u << i);
5339 else
5341 /* Stride = 0 means always the same values.
5342 * glVertexAttribPointer() doesn't do that. Instead disable the
5343 * pointer and set up the attribute statically. But we have to
5344 * figure out the system memory address. */
5345 const BYTE *ptr = element->data.addr;
5346 if (element->data.buffer_object)
5347 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(stream->buffer, context);
5349 if (context->numbered_array_mask & (1u << i))
5350 wined3d_context_gl_unload_numbered_array(context_gl, i);
5352 switch (format_gl->f.id)
5354 case WINED3DFMT_R32_FLOAT:
5355 GL_EXTCALL(glVertexAttrib1fv(i, (const GLfloat *)ptr));
5356 break;
5357 case WINED3DFMT_R32G32_FLOAT:
5358 GL_EXTCALL(glVertexAttrib2fv(i, (const GLfloat *)ptr));
5359 break;
5360 case WINED3DFMT_R32G32B32_FLOAT:
5361 GL_EXTCALL(glVertexAttrib3fv(i, (const GLfloat *)ptr));
5362 break;
5363 case WINED3DFMT_R32G32B32A32_FLOAT:
5364 GL_EXTCALL(glVertexAttrib4fv(i, (const GLfloat *)ptr));
5365 break;
5366 case WINED3DFMT_R8G8B8A8_UINT:
5367 GL_EXTCALL(glVertexAttrib4ubv(i, ptr));
5368 break;
5369 case WINED3DFMT_B8G8R8A8_UNORM:
5370 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
5372 const DWORD *src = (const DWORD *)ptr;
5373 DWORD c = *src & 0xff00ff00u;
5374 c |= (*src & 0xff0000u) >> 16;
5375 c |= (*src & 0xffu) << 16;
5376 GL_EXTCALL(glVertexAttrib4Nubv(i, (GLubyte *)&c));
5377 break;
5379 /* else fallthrough */
5380 case WINED3DFMT_R8G8B8A8_UNORM:
5381 GL_EXTCALL(glVertexAttrib4Nubv(i, ptr));
5382 break;
5383 case WINED3DFMT_R16G16_SINT:
5384 GL_EXTCALL(glVertexAttrib2sv(i, (const GLshort *)ptr));
5385 break;
5386 case WINED3DFMT_R16G16B16A16_SINT:
5387 GL_EXTCALL(glVertexAttrib4sv(i, (const GLshort *)ptr));
5388 break;
5389 case WINED3DFMT_R16G16_SNORM:
5391 const GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
5392 GL_EXTCALL(glVertexAttrib4Nsv(i, s));
5393 break;
5395 case WINED3DFMT_R16G16_UNORM:
5397 const GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
5398 GL_EXTCALL(glVertexAttrib4Nusv(i, s));
5399 break;
5401 case WINED3DFMT_R16G16B16A16_SNORM:
5402 GL_EXTCALL(glVertexAttrib4Nsv(i, (const GLshort *)ptr));
5403 break;
5404 case WINED3DFMT_R16G16B16A16_UNORM:
5405 GL_EXTCALL(glVertexAttrib4Nusv(i, (const GLushort *)ptr));
5406 break;
5407 case WINED3DFMT_R10G10B10X2_UINT:
5408 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5409 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5410 break;
5411 case WINED3DFMT_R10G10B10X2_SNORM:
5412 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5413 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5414 break;
5415 case WINED3DFMT_R16G16_FLOAT:
5416 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5418 /* Not supported by GL_ARB_half_float_vertex. */
5419 GL_EXTCALL(glVertexAttrib2hvNV(i, (const GLhalfNV *)ptr));
5421 else
5423 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5424 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5425 GL_EXTCALL(glVertexAttrib2f(i, x, y));
5427 break;
5428 case WINED3DFMT_R16G16B16A16_FLOAT:
5429 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5431 /* Not supported by GL_ARB_half_float_vertex. */
5432 GL_EXTCALL(glVertexAttrib4hvNV(i, (const GLhalfNV *)ptr));
5434 else
5436 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5437 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5438 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
5439 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
5440 GL_EXTCALL(glVertexAttrib4f(i, x, y, z, w));
5442 break;
5443 default:
5444 ERR("Unexpected declaration in stride 0 attributes.\n");
5445 break;
5448 context_gl->default_attrib_value_set &= ~(1u << i);
5451 checkGLcall("Loading numbered arrays");
5454 void wined3d_context_gl_update_stream_sources(struct wined3d_context_gl *context_gl,
5455 const struct wined3d_state *state)
5457 if (context_gl->c.use_immediate_mode_draw)
5458 return;
5460 wined3d_context_gl_unload_vertex_data(context_gl);
5461 if (context_gl->c.d3d_info->ffp_generic_attributes || use_vs(state))
5463 TRACE("Loading numbered arrays.\n");
5464 wined3d_context_gl_load_numbered_arrays(context_gl, &context_gl->c.stream_info, state);
5465 return;
5468 TRACE("Loading named arrays.\n");
5469 wined3d_context_gl_unload_numbered_arrays(context_gl);
5470 wined3d_context_gl_load_vertex_data(context_gl, &context_gl->c.stream_info, state);
5471 context_gl->c.namedArraysLoaded = TRUE;
5474 static void apply_texture_blit_state(const struct wined3d_gl_info *gl_info, struct gl_texture *texture,
5475 GLenum target, unsigned int level, enum wined3d_texture_filter_type filter)
5477 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
5478 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
5479 wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
5480 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
5481 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
5482 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
5483 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
5484 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, level);
5486 /* We changed the filtering settings on the texture. Make sure they get
5487 * reset on subsequent draws. */
5488 texture->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
5489 texture->sampler_desc.min_filter = WINED3D_TEXF_POINT;
5490 texture->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
5491 texture->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
5492 texture->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
5493 texture->sampler_desc.srgb_decode = FALSE;
5494 texture->base_level = level;
5497 /* Context activation is done by the caller. */
5498 void wined3d_context_gl_draw_shaded_quad(struct wined3d_context_gl *context_gl, struct wined3d_texture_gl *texture_gl,
5499 unsigned int sub_resource_idx, const RECT *src_rect, const RECT *dst_rect,
5500 enum wined3d_texture_filter_type filter)
5502 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5503 struct wined3d_blt_info info;
5504 unsigned int level, w, h, i;
5505 SIZE dst_size;
5506 struct blit_vertex
5508 float x, y;
5509 struct wined3d_vec3 texcoord;
5511 quad[4];
5513 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5515 level = sub_resource_idx % texture_gl->t.level_count;
5516 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5517 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5518 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5520 wined3d_context_gl_get_rt_size(context_gl, &dst_size);
5521 w = dst_size.cx;
5522 h = dst_size.cy;
5524 quad[0].x = dst_rect->left * 2.0f / w - 1.0f;
5525 quad[0].y = dst_rect->top * 2.0f / h - 1.0f;
5526 quad[0].texcoord = info.texcoords[0];
5528 quad[1].x = dst_rect->right * 2.0f / w - 1.0f;
5529 quad[1].y = dst_rect->top * 2.0f / h - 1.0f;
5530 quad[1].texcoord = info.texcoords[1];
5532 quad[2].x = dst_rect->left * 2.0f / w - 1.0f;
5533 quad[2].y = dst_rect->bottom * 2.0f / h - 1.0f;
5534 quad[2].texcoord = info.texcoords[2];
5536 quad[3].x = dst_rect->right * 2.0f / w - 1.0f;
5537 quad[3].y = dst_rect->bottom * 2.0f / h - 1.0f;
5538 quad[3].texcoord = info.texcoords[3];
5540 /* Draw a quad. */
5541 if (gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
5543 if (!context_gl->blit_vbo)
5544 GL_EXTCALL(glGenBuffers(1, &context_gl->blit_vbo));
5545 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, context_gl->blit_vbo));
5547 wined3d_context_gl_unload_vertex_data(context_gl);
5548 wined3d_context_gl_unload_numbered_arrays(context_gl);
5550 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STREAM_DRAW));
5551 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT, FALSE, sizeof(*quad), NULL));
5552 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT, FALSE, sizeof(*quad),
5553 (void *)FIELD_OFFSET(struct blit_vertex, texcoord)));
5555 GL_EXTCALL(glEnableVertexAttribArray(0));
5556 GL_EXTCALL(glEnableVertexAttribArray(1));
5558 gl_info->gl_ops.gl.p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
5560 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
5561 GL_EXTCALL(glDisableVertexAttribArray(1));
5562 GL_EXTCALL(glDisableVertexAttribArray(0));
5564 else
5566 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5568 for (i = 0; i < ARRAY_SIZE(quad); ++i)
5570 GL_EXTCALL(glVertexAttrib3fv(1, &quad[i].texcoord.x));
5571 GL_EXTCALL(glVertexAttrib2fv(0, &quad[i].x));
5574 gl_info->gl_ops.gl.p_glEnd();
5576 checkGLcall("draw");
5578 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5579 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);
5582 /* Context activation is done by the caller. */
5583 void wined3d_context_gl_draw_textured_quad(struct wined3d_context_gl *context_gl,
5584 struct wined3d_texture_gl *texture_gl, unsigned int sub_resource_idx,
5585 const RECT *src_rect, const RECT *dst_rect, enum wined3d_texture_filter_type filter)
5587 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5588 struct wined3d_blt_info info;
5589 unsigned int level;
5591 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5593 gl_info->gl_ops.gl.p_glEnable(info.bind_target);
5594 checkGLcall("glEnable(bind_target)");
5596 level = sub_resource_idx % texture_gl->t.level_count;
5597 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5598 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5599 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5600 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
5601 checkGLcall("glTexEnvi");
5603 /* Draw a quad. */
5604 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5605 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[0].x);
5606 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->top);
5608 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[1].x);
5609 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->top);
5611 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[2].x);
5612 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->bottom);
5614 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[3].x);
5615 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->bottom);
5616 gl_info->gl_ops.gl.p_glEnd();
5618 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5619 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);