msvcrt: Improve acosf compatibility with native ucrtbase.
[wine.git] / dlls / wined3d / context_gl.c
bloba51e5ec9095241cdfa65ea81a9afb363f8f6b8a6
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_sync);
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 /* If we're here because we're switching away from a previously
1378 * destroyed context, acquiring a context in order to submit a fence
1379 * is problematic. (In particular, we'd end up back here again in the
1380 * process of switching to the newly acquired context.) */
1381 if (context_gl->c.destroyed)
1383 gl_info->gl_ops.gl.p_glFinish();
1385 else
1387 wined3d_context_gl_submit_command_fence(context_gl);
1388 wined3d_context_gl_wait_command_fence(context_gl,
1389 wined3d_device_gl(context_gl->c.device)->current_fence_id - 1);
1392 if (context_gl->dummy_arbfp_prog)
1393 GL_EXTCALL(glDeleteProgramsARB(1, &context_gl->dummy_arbfp_prog));
1395 if (context_gl->blit_vbo)
1396 GL_EXTCALL(glDeleteBuffers(1, &context_gl->blit_vbo));
1398 for (i = 0; i < context_gl->free_pipeline_statistics_query_count; ++i)
1400 union wined3d_gl_pipeline_statistics_query *q = &context_gl->free_pipeline_statistics_queries[i];
1401 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1404 for (i = 0; i < context_gl->free_so_statistics_query_count; ++i)
1406 union wined3d_gl_so_statistics_query *q = &context_gl->free_so_statistics_queries[i];
1407 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1410 if (context_gl->free_timestamp_query_count)
1411 GL_EXTCALL(glDeleteQueries(context_gl->free_timestamp_query_count, context_gl->free_timestamp_queries));
1413 if (gl_info->supported[ARB_SYNC])
1415 for (i = 0; i < context_gl->free_fence_count; ++i)
1417 GL_EXTCALL(glDeleteSync(context_gl->free_fences[i].sync));
1420 else if (gl_info->supported[APPLE_FENCE])
1422 for (i = 0; i < context_gl->free_fence_count; ++i)
1424 GL_EXTCALL(glDeleteFencesAPPLE(1, &context_gl->free_fences[i].id));
1427 else if (gl_info->supported[NV_FENCE])
1429 for (i = 0; i < context_gl->free_fence_count; ++i)
1431 GL_EXTCALL(glDeleteFencesNV(1, &context_gl->free_fences[i].id));
1435 if (context_gl->free_occlusion_query_count)
1436 GL_EXTCALL(glDeleteQueries(context_gl->free_occlusion_query_count, context_gl->free_occlusion_queries));
1438 checkGLcall("context cleanup");
1440 heap_free(context_gl->submitted.fences);
1441 heap_free(context_gl->free_pipeline_statistics_queries);
1442 heap_free(context_gl->free_so_statistics_queries);
1443 heap_free(context_gl->free_timestamp_queries);
1444 heap_free(context_gl->free_fences);
1445 heap_free(context_gl->free_occlusion_queries);
1447 LIST_FOR_EACH_ENTRY(pipeline_statistics_query, &context_gl->pipeline_statistics_queries,
1448 struct wined3d_pipeline_statistics_query, entry)
1450 if (context_gl->valid)
1451 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query->u.id), pipeline_statistics_query->u.id));
1452 pipeline_statistics_query->context_gl = NULL;
1455 LIST_FOR_EACH_ENTRY(so_statistics_query, &context_gl->so_statistics_queries,
1456 struct wined3d_so_statistics_query, entry)
1458 if (context_gl->valid)
1459 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query->u.id), so_statistics_query->u.id));
1460 so_statistics_query->context_gl = NULL;
1463 LIST_FOR_EACH_ENTRY(timestamp_query, &context_gl->timestamp_queries, struct wined3d_timestamp_query, entry)
1465 if (context_gl->valid)
1466 GL_EXTCALL(glDeleteQueries(1, &timestamp_query->id));
1467 timestamp_query->context_gl = NULL;
1470 LIST_FOR_EACH_ENTRY(fence, &context_gl->fences, struct wined3d_fence, entry)
1472 if (context_gl->valid)
1474 if (gl_info->supported[ARB_SYNC])
1476 if (fence->object.sync)
1477 GL_EXTCALL(glDeleteSync(fence->object.sync));
1479 else if (gl_info->supported[APPLE_FENCE])
1481 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence->object.id));
1483 else if (gl_info->supported[NV_FENCE])
1485 GL_EXTCALL(glDeleteFencesNV(1, &fence->object.id));
1488 fence->context_gl = NULL;
1491 LIST_FOR_EACH_ENTRY(occlusion_query, &context_gl->occlusion_queries, struct wined3d_occlusion_query, entry)
1493 if (context_gl->valid)
1494 GL_EXTCALL(glDeleteQueries(1, &occlusion_query->id));
1495 occlusion_query->context_gl = NULL;
1498 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_destroy_list, struct fbo_entry, entry)
1500 if (!context_gl->valid)
1501 entry->id = 0;
1502 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1505 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_list, struct fbo_entry, entry)
1507 if (!context_gl->valid)
1508 entry->id = 0;
1509 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1512 heap_free(context_gl->texture_type);
1514 wined3d_context_gl_restore_pixel_format(context_gl);
1515 if (restore_ctx)
1516 context_restore_gl_context(gl_info, restore_dc, restore_ctx);
1517 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1518 ERR("Failed to disable GL context.\n");
1520 wined3d_release_dc(context_gl->window, context_gl->dc);
1522 if (!wglDeleteContext(context_gl->gl_ctx))
1524 DWORD err = GetLastError();
1525 ERR("Failed to delete GL context %p, last error %#x.\n", context_gl->gl_ctx, err);
1528 wined3d_context_cleanup(&context_gl->c);
1531 DWORD context_get_tls_idx(void)
1533 return wined3d_context_tls_idx;
1536 void context_set_tls_idx(DWORD idx)
1538 wined3d_context_tls_idx = idx;
1541 struct wined3d_context_gl *wined3d_context_gl_get_current(void)
1543 return TlsGetValue(wined3d_context_tls_idx);
1546 BOOL wined3d_context_gl_set_current(struct wined3d_context_gl *context_gl)
1548 struct wined3d_context_gl *old = wined3d_context_gl_get_current();
1550 if (old == context_gl)
1552 TRACE("Already using D3D context %p.\n", context_gl);
1553 return TRUE;
1556 if (old)
1558 if (old->c.destroyed)
1560 TRACE("Switching away from destroyed context %p.\n", old);
1561 wined3d_context_gl_cleanup(old);
1562 heap_free((void *)old->gl_info);
1563 heap_free(old);
1565 else
1567 if (wglGetCurrentContext())
1569 const struct wined3d_gl_info *gl_info = old->gl_info;
1570 TRACE("Flushing context %p before switching to %p.\n", old, context_gl);
1571 gl_info->gl_ops.gl.p_glFlush();
1573 old->c.current = 0;
1577 if (context_gl)
1579 if (!context_gl->valid)
1581 ERR("Trying to make invalid context %p current.\n", context_gl);
1582 return FALSE;
1585 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n",
1586 context_gl, context_gl->gl_ctx, context_gl->dc);
1587 if (!wined3d_context_gl_set_gl_context(context_gl))
1588 return FALSE;
1589 context_gl->c.current = 1;
1591 else if (wglGetCurrentContext())
1593 TRACE("Clearing current D3D context.\n");
1594 if (!wglMakeCurrent(NULL, NULL))
1596 DWORD err = GetLastError();
1597 ERR("Failed to clear current GL context, last error %#x.\n", err);
1598 TlsSetValue(wined3d_context_tls_idx, NULL);
1599 return FALSE;
1603 return TlsSetValue(wined3d_context_tls_idx, context_gl);
1606 void wined3d_context_gl_release(struct wined3d_context_gl *context_gl)
1608 TRACE("Releasing context %p, level %u.\n", context_gl, context_gl->level);
1610 if (WARN_ON(d3d))
1612 if (!context_gl->level)
1613 WARN("Context %p is not active.\n", context_gl);
1614 else if (context_gl != wined3d_context_gl_get_current())
1615 WARN("Context %p is not the current context.\n", context_gl);
1618 if (!--context_gl->level)
1620 if (wined3d_context_gl_restore_pixel_format(context_gl))
1621 context_gl->needs_set = 1;
1622 if (context_gl->restore_ctx)
1624 TRACE("Restoring GL context %p on device context %p.\n", context_gl->restore_ctx, context_gl->restore_dc);
1625 context_restore_gl_context(context_gl->gl_info, context_gl->restore_dc, context_gl->restore_ctx);
1626 context_gl->restore_ctx = NULL;
1627 context_gl->restore_dc = NULL;
1630 if (context_gl->c.destroy_delayed)
1632 TRACE("Destroying context %p.\n", context_gl);
1633 wined3d_context_gl_destroy(context_gl);
1638 static void wined3d_context_gl_enter(struct wined3d_context_gl *context_gl)
1640 TRACE("Entering context %p, level %u.\n", context_gl, context_gl->level + 1);
1642 if (!context_gl->level++)
1644 const struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
1645 HGLRC current_gl = wglGetCurrentContext();
1647 if (current_gl && (!current_context || current_context->gl_ctx != current_gl))
1649 TRACE("Another GL context (%p on device context %p) is already current.\n",
1650 current_gl, wglGetCurrentDC());
1651 context_gl->restore_ctx = current_gl;
1652 context_gl->restore_dc = wglGetCurrentDC();
1653 context_gl->needs_set = 1;
1655 else if (!context_gl->needs_set && !(context_gl->dc_is_private && context_gl->dc_has_format)
1656 && context_gl->pixel_format != context_gl->gl_info->gl_ops.wgl.p_wglGetPixelFormat(context_gl->dc))
1657 context_gl->needs_set = 1;
1661 /* This function takes care of wined3d pixel format selection. */
1662 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1663 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1664 BOOL auxBuffers)
1666 unsigned int cfg_count = wined3d_adapter_gl(device->adapter)->pixel_format_count;
1667 unsigned int current_value;
1668 PIXELFORMATDESCRIPTOR pfd;
1669 int iPixelFormat = 0;
1670 unsigned int i;
1672 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1673 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1674 auxBuffers);
1676 current_value = 0;
1677 for (i = 0; i < cfg_count; ++i)
1679 const struct wined3d_pixel_format *cfg = &wined3d_adapter_gl(device->adapter)->pixel_formats[i];
1680 unsigned int value;
1682 /* For now only accept RGBA formats. Perhaps some day we will
1683 * allow floating point formats for pbuffers. */
1684 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1685 continue;
1686 /* In window mode we need a window drawable format and double buffering. */
1687 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1688 continue;
1689 if (cfg->redSize < color_format->red_size)
1690 continue;
1691 if (cfg->greenSize < color_format->green_size)
1692 continue;
1693 if (cfg->blueSize < color_format->blue_size)
1694 continue;
1695 if (cfg->alphaSize < color_format->alpha_size)
1696 continue;
1697 if (cfg->depthSize < ds_format->depth_size)
1698 continue;
1699 if (ds_format->stencil_size && cfg->stencilSize != ds_format->stencil_size)
1700 continue;
1701 /* Check multisampling support. */
1702 if (cfg->numSamples)
1703 continue;
1705 value = 1;
1706 /* We try to locate a format which matches our requirements exactly. In case of
1707 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1708 if (cfg->depthSize == ds_format->depth_size)
1709 value += 1;
1710 if (cfg->stencilSize == ds_format->stencil_size)
1711 value += 2;
1712 if (cfg->alphaSize == color_format->alpha_size)
1713 value += 4;
1714 /* We like to have aux buffers in backbuffer mode */
1715 if (auxBuffers && cfg->auxBuffers)
1716 value += 8;
1717 if (cfg->redSize == color_format->red_size
1718 && cfg->greenSize == color_format->green_size
1719 && cfg->blueSize == color_format->blue_size)
1720 value += 16;
1722 if (value > current_value)
1724 iPixelFormat = cfg->iPixelFormat;
1725 current_value = value;
1729 if (!iPixelFormat)
1731 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1733 memset(&pfd, 0, sizeof(pfd));
1734 pfd.nSize = sizeof(pfd);
1735 pfd.nVersion = 1;
1736 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1737 pfd.iPixelType = PFD_TYPE_RGBA;
1738 pfd.cAlphaBits = color_format->alpha_size;
1739 pfd.cColorBits = color_format->red_size + color_format->green_size
1740 + color_format->blue_size + color_format->alpha_size;
1741 pfd.cDepthBits = ds_format->depth_size;
1742 pfd.cStencilBits = ds_format->stencil_size;
1743 pfd.iLayerType = PFD_MAIN_PLANE;
1745 if (!(iPixelFormat = ChoosePixelFormat(hdc, &pfd)))
1747 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1748 ERR("Can't find a suitable pixel format.\n");
1749 return 0;
1753 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1754 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1755 return iPixelFormat;
1758 /* Context activation is done by the caller. */
1759 void wined3d_context_gl_bind_dummy_textures(const struct wined3d_context_gl *context_gl)
1761 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
1762 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1763 unsigned int i;
1765 for (i = 0; i < gl_info->limits.combined_samplers; ++i)
1767 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + i));
1769 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
1770 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
1772 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1773 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
1775 if (gl_info->supported[EXT_TEXTURE3D])
1776 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
1778 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1779 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
1781 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
1782 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
1784 if (gl_info->supported[EXT_TEXTURE_ARRAY])
1786 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
1787 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
1790 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1791 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
1793 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1795 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
1796 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
1800 checkGLcall("bind dummy textures");
1803 void wined3d_check_gl_call(const struct wined3d_gl_info *gl_info,
1804 const char *file, unsigned int line, const char *name)
1806 GLint err;
1808 if (gl_info->supported[ARB_DEBUG_OUTPUT] || (err = gl_info->gl_ops.gl.p_glGetError()) == GL_NO_ERROR)
1810 TRACE("%s call ok %s / %u.\n", name, file, line);
1811 return;
1816 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1817 debug_glerror(err), err, name, file,line);
1818 err = gl_info->gl_ops.gl.p_glGetError();
1819 } while (err != GL_NO_ERROR);
1822 static BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1824 return gl_info->supported[ARB_DEBUG_OUTPUT]
1825 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1828 static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1829 GLenum severity, GLsizei length, const char *message, void *ctx)
1831 switch (type)
1833 case GL_DEBUG_TYPE_ERROR_ARB:
1834 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1835 break;
1837 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1838 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1839 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1840 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1841 break;
1843 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1844 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1845 break;
1847 default:
1848 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1849 break;
1853 HGLRC context_create_wgl_attribs(const struct wined3d_gl_info *gl_info, HDC hdc, HGLRC share_ctx)
1855 HGLRC ctx;
1856 unsigned int ctx_attrib_idx = 0;
1857 GLint ctx_attribs[7], ctx_flags = 0;
1859 if (context_debug_output_enabled(gl_info))
1860 ctx_flags = WGL_CONTEXT_DEBUG_BIT_ARB;
1861 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
1862 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version >> 16;
1863 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MINOR_VERSION_ARB;
1864 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version & 0xffff;
1865 if (ctx_flags)
1867 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1868 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1870 ctx_attribs[ctx_attrib_idx] = 0;
1872 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1874 if (gl_info->selected_gl_version >= MAKEDWORD_VERSION(3, 2))
1876 if (ctx_flags)
1878 ctx_flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1879 ctx_attribs[ctx_attrib_idx - 1] = ctx_flags;
1881 else
1883 ctx_flags = WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1884 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1885 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1886 ctx_attribs[ctx_attrib_idx] = 0;
1888 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1889 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1890 GetLastError());
1893 return ctx;
1896 static BOOL wined3d_context_gl_create_wgl_ctx(struct wined3d_context_gl *context_gl,
1897 struct wined3d_swapchain_gl *swapchain_gl)
1899 const struct wined3d_format *colour_format, *ds_format;
1900 struct wined3d_context *context = &context_gl->c;
1901 const struct wined3d_gl_info *gl_info;
1902 struct wined3d_resource *target;
1903 struct wined3d_adapter *adapter;
1904 unsigned int target_bind_flags;
1905 struct wined3d_device *device;
1906 HGLRC ctx, share_ctx;
1907 unsigned int i;
1909 device = context->device;
1910 adapter = device->adapter;
1911 gl_info = &adapter->gl_info;
1913 target = &context->current_rt.texture->resource;
1914 target_bind_flags = target->bind_flags;
1916 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1918 static const enum wined3d_format_id ds_formats[] =
1920 WINED3DFMT_D24_UNORM_S8_UINT,
1921 WINED3DFMT_D32_UNORM,
1922 WINED3DFMT_R24_UNORM_X8_TYPELESS,
1923 WINED3DFMT_D16_UNORM,
1924 WINED3DFMT_S1_UINT_D15_UNORM,
1927 colour_format = target->format;
1929 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1930 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1931 if (colour_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1932 colour_format = wined3d_get_format(adapter, WINED3DFMT_B4G4R4A4_UNORM, target_bind_flags);
1933 else if (colour_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1934 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1936 /* DirectDraw supports 8bit paletted render targets and these are used by
1937 * old games like StarCraft and C&C. Most modern hardware doesn't support
1938 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1939 * conversion (ab)uses the alpha component for storing the palette index.
1940 * For this reason we require a format with 8bit alpha, so request
1941 * A8R8G8B8. */
1942 if (colour_format->id == WINED3DFMT_P8_UINT)
1943 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1945 /* Try to find a pixel format which matches our requirements. */
1946 if (!swapchain_gl->s.ds_format)
1948 for (i = 0; i < ARRAY_SIZE(ds_formats); ++i)
1950 ds_format = wined3d_get_format(adapter, ds_formats[i], WINED3D_BIND_DEPTH_STENCIL);
1951 if ((context_gl->pixel_format = context_choose_pixel_format(device,
1952 context_gl->dc, colour_format, ds_format, TRUE)))
1954 swapchain_gl->s.ds_format = ds_format;
1955 break;
1958 TRACE("Depth stencil format %s is not supported, trying next format.\n",
1959 debug_d3dformat(ds_format->id));
1962 else
1964 context_gl->pixel_format = context_choose_pixel_format(device,
1965 context_gl->dc, colour_format, swapchain_gl->s.ds_format, TRUE);
1968 else
1970 /* When using FBOs for off-screen rendering, we only use the drawable for
1971 * presentation blits, and don't do any rendering to it. That means we
1972 * don't need depth or stencil buffers, and can mostly ignore the render
1973 * target format. This wouldn't necessarily be quite correct for 10bpc
1974 * display modes, but we don't currently support those.
1975 * Using the same format regardless of the colour/depth/stencil targets
1976 * makes it much less likely that different wined3d instances will set
1977 * conflicting pixel formats. */
1978 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1979 ds_format = wined3d_get_format(adapter, WINED3DFMT_UNKNOWN, WINED3D_BIND_DEPTH_STENCIL);
1980 context_gl->pixel_format = context_choose_pixel_format(device,
1981 context_gl->dc, colour_format, ds_format, FALSE);
1984 if (!context_gl->pixel_format)
1986 ERR("Failed to choose pixel format.\n");
1987 return FALSE;
1990 wined3d_context_gl_enter(context_gl);
1992 if (!wined3d_context_gl_set_pixel_format(context_gl))
1994 context_release(context);
1996 if (context_gl->dc_is_private)
1998 ERR("Failed to set pixel format %d on device context %p.\n", context_gl->pixel_format, context_gl->dc);
2000 return FALSE;
2003 WARN("Failed to set pixel format %d on device context %p, trying backup DC.\n",
2004 context_gl->pixel_format, context_gl->dc);
2006 wined3d_release_dc(context_gl->window, context_gl->dc);
2007 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2009 ERR("Failed to retrieve the backup device context.\n");
2010 return FALSE;
2012 context_gl->dc_is_private = TRUE;
2014 return wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl);
2017 share_ctx = device->context_count ? wined3d_context_gl(device->contexts[0])->gl_ctx : NULL;
2018 if (gl_info->p_wglCreateContextAttribsARB)
2020 if (!(ctx = context_create_wgl_attribs(gl_info, context_gl->dc, share_ctx)))
2022 ERR("Failed to create a WGL context.\n");
2023 context_release(context);
2024 return FALSE;
2027 else
2029 if (!(ctx = wglCreateContext(context_gl->dc)))
2031 ERR("Failed to create a WGL context.\n");
2032 context_release(context);
2033 return FALSE;
2036 if (share_ctx && !wglShareLists(share_ctx, ctx))
2038 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
2039 context_release(context);
2040 if (!wglDeleteContext(ctx))
2041 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2042 return FALSE;
2046 context_gl->dc_has_format = TRUE;
2047 context_gl->needs_set = 1;
2048 context_gl->valid = 1;
2049 context_gl->gl_ctx = ctx;
2051 return TRUE;
2054 HRESULT wined3d_context_gl_init(struct wined3d_context_gl *context_gl, struct wined3d_swapchain_gl *swapchain_gl)
2056 struct wined3d_context *context = &context_gl->c;
2057 const struct wined3d_d3d_info *d3d_info;
2058 const struct wined3d_gl_info *gl_info;
2059 struct wined3d_device *device;
2060 unsigned int i;
2062 TRACE("context_gl %p, swapchain %p.\n", context_gl, swapchain_gl);
2064 wined3d_context_init(&context_gl->c, &swapchain_gl->s);
2066 device = context->device;
2067 gl_info = &device->adapter->gl_info;
2068 context_gl->gl_info = gl_info;
2069 d3d_info = context->d3d_info;
2071 context_gl->tid = GetCurrentThreadId();
2072 context_gl->window = context->swapchain->win_handle;
2073 if (context_gl->window == GetDesktopWindow())
2075 TRACE("Swapchain is created on the desktop window, trying backup device context.\n");
2076 context_gl->dc = NULL;
2078 else if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
2079 WARN("Failed to retrieve device context, trying swapchain backup.\n");
2081 if (!context_gl->dc)
2083 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2085 ERR("Failed to retrieve a device context.\n");
2086 return E_FAIL;
2088 context_gl->dc_is_private = TRUE;
2091 list_init(&context_gl->fbo_list);
2092 list_init(&context_gl->fbo_destroy_list);
2094 list_init(&context_gl->occlusion_queries);
2095 list_init(&context_gl->fences);
2096 list_init(&context_gl->timestamp_queries);
2097 list_init(&context_gl->so_statistics_queries);
2098 list_init(&context_gl->pipeline_statistics_queries);
2100 for (i = 0; i < ARRAY_SIZE(context_gl->tex_unit_map); ++i)
2101 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2102 for (i = 0; i < ARRAY_SIZE(context_gl->rev_tex_unit_map); ++i)
2103 context_gl->rev_tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2104 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
2106 /* Initialize the texture unit mapping to a 1:1 mapping. */
2107 unsigned int base, count;
2109 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_PIXEL, &base, &count);
2110 if (base + WINED3D_MAX_FRAGMENT_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2112 ERR("Unexpected texture unit base index %u.\n", base);
2113 goto fail;
2115 for (i = 0; i < min(count, WINED3D_MAX_FRAGMENT_SAMPLERS); ++i)
2117 context_gl->tex_unit_map[i] = base + i;
2118 context_gl->rev_tex_unit_map[base + i] = i;
2121 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_VERTEX, &base, &count);
2122 if (base + WINED3D_MAX_VERTEX_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2124 ERR("Unexpected texture unit base index %u.\n", base);
2125 goto fail;
2127 for (i = 0; i < min(count, WINED3D_MAX_VERTEX_SAMPLERS); ++i)
2129 context_gl->tex_unit_map[WINED3D_MAX_FRAGMENT_SAMPLERS + i] = base + i;
2130 context_gl->rev_tex_unit_map[base + i] = WINED3D_MAX_FRAGMENT_SAMPLERS + i;
2134 if (!(context_gl->texture_type = heap_calloc(gl_info->limits.combined_samplers,
2135 sizeof(*context_gl->texture_type))))
2136 goto fail;
2138 if (!wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl))
2139 goto fail;
2141 /* Set up the context defaults. */
2143 context->render_offscreen = wined3d_resource_is_offscreen(&context->current_rt.texture->resource);
2144 context_gl->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
2146 if (!wined3d_context_gl_set_current(context_gl))
2148 ERR("Cannot activate context to set up defaults.\n");
2149 context_release(context);
2150 if (!wglDeleteContext(context_gl->gl_ctx))
2151 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context_gl->gl_ctx, GetLastError());
2152 goto fail;
2155 if (context_debug_output_enabled(gl_info))
2157 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback, context));
2158 if (TRACE_ON(d3d_sync))
2159 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2160 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
2161 if (ERR_ON(d3d))
2163 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR,
2164 GL_DONT_CARE, 0, NULL, GL_TRUE));
2166 if (FIXME_ON(d3d))
2168 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
2169 GL_DONT_CARE, 0, NULL, GL_TRUE));
2170 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
2171 GL_DONT_CARE, 0, NULL, GL_TRUE));
2172 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY,
2173 GL_DONT_CARE, 0, NULL, GL_TRUE));
2175 if (WARN_ON(d3d_perf))
2177 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE,
2178 GL_DONT_CARE, 0, NULL, GL_TRUE));
2182 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2183 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &context_gl->aux_buffers);
2185 TRACE("Setting up the screen\n");
2187 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2189 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
2190 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2192 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
2193 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2195 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2196 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2198 else
2200 GLuint vao;
2202 GL_EXTCALL(glGenVertexArrays(1, &vao));
2203 GL_EXTCALL(glBindVertexArray(vao));
2204 checkGLcall("creating VAO");
2207 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
2208 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2209 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2210 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2212 if (gl_info->supported[NV_TEXTURE_SHADER2])
2214 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2215 * the previous texture where to source the offset from is always unit - 1.
2217 for (i = 1; i < gl_info->limits.textures; ++i)
2219 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2220 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
2221 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + i - 1);
2222 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2225 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2227 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2228 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2229 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2230 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2231 * is ever assigned.
2233 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2234 * program and the dummy program is destroyed when the context is destroyed.
2236 static const char dummy_program[] =
2237 "!!ARBfp1.0\n"
2238 "MOV result.color, fragment.color.primary;\n"
2239 "END\n";
2240 GL_EXTCALL(glGenProgramsARB(1, &context_gl->dummy_arbfp_prog));
2241 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, context_gl->dummy_arbfp_prog));
2242 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
2243 GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2246 if (gl_info->supported[ARB_POINT_SPRITE])
2248 for (i = 0; i < gl_info->limits.textures; ++i)
2250 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2251 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2252 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2256 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2258 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2260 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2262 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2264 if (!(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART))
2266 if (gl_info->supported[ARB_ES3_COMPATIBILITY])
2268 gl_info->gl_ops.gl.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
2269 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2271 else
2273 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2276 if (!(d3d_info->wined3d_creation_flags & WINED3D_LEGACY_CUBEMAP_FILTERING)
2277 && gl_info->supported[ARB_SEAMLESS_CUBE_MAP])
2279 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
2280 checkGLcall("enable seamless cube map filtering");
2282 if (gl_info->supported[ARB_CLIP_CONTROL])
2283 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT));
2285 /* If this happens to be the first context for the device, dummy textures
2286 * are not created yet. In that case, they will be created (and bound) by
2287 * create_dummy_textures right after this context is initialized. */
2288 if (wined3d_device_gl(device)->dummy_textures.tex_2d)
2289 wined3d_context_gl_bind_dummy_textures(context_gl);
2291 /* Initialise all rectangles to avoid resetting unused ones later. */
2292 gl_info->gl_ops.gl.p_glScissor(0, 0, 0, 0);
2293 checkGLcall("glScissor");
2295 return WINED3D_OK;
2297 fail:
2298 heap_free(context_gl->texture_type);
2299 wined3d_release_dc(context_gl->window, context_gl->dc);
2300 return E_FAIL;
2303 void wined3d_context_gl_destroy(struct wined3d_context_gl *context_gl)
2305 struct wined3d_device *device = context_gl->c.device;
2307 TRACE("Destroying context %p.\n", context_gl);
2309 wined3d_from_cs(device->cs);
2311 /* We delay destroying a context when it is active. The context_release()
2312 * function invokes wined3d_context_gl_destroy() again while leaving the
2313 * last level. */
2314 if (context_gl->level)
2316 TRACE("Delaying destruction of context %p.\n", context_gl);
2317 context_gl->c.destroy_delayed = 1;
2318 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2319 context_gl->c.swapchain = NULL;
2320 return;
2323 device_context_remove(device, &context_gl->c);
2325 if (context_gl->c.current && context_gl->tid != GetCurrentThreadId())
2327 struct wined3d_gl_info *gl_info;
2329 /* Make a copy of gl_info for wined3d_context_gl_cleanup() use, the
2330 * one in wined3d_adapter may go away in the meantime. */
2331 gl_info = heap_alloc(sizeof(*gl_info));
2332 *gl_info = *context_gl->gl_info;
2333 context_gl->gl_info = gl_info;
2334 context_gl->c.destroyed = 1;
2336 return;
2339 wined3d_context_gl_cleanup(context_gl);
2340 TlsSetValue(context_get_tls_idx(), NULL);
2341 heap_free(context_gl);
2344 const unsigned int *wined3d_context_gl_get_tex_unit_mapping(const struct wined3d_context_gl *context_gl,
2345 const struct wined3d_shader_version *shader_version, unsigned int *base, unsigned int *count)
2347 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2349 if (!shader_version)
2351 *base = 0;
2352 *count = WINED3D_MAX_TEXTURES;
2353 return context_gl->tex_unit_map;
2356 if (shader_version->major >= 4)
2358 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, shader_version->type, base, count);
2359 return NULL;
2362 switch (shader_version->type)
2364 case WINED3D_SHADER_TYPE_PIXEL:
2365 *base = 0;
2366 *count = WINED3D_MAX_FRAGMENT_SAMPLERS;
2367 break;
2368 case WINED3D_SHADER_TYPE_VERTEX:
2369 *base = WINED3D_MAX_FRAGMENT_SAMPLERS;
2370 *count = WINED3D_MAX_VERTEX_SAMPLERS;
2371 break;
2372 default:
2373 ERR("Unhandled shader type %#x.\n", shader_version->type);
2374 *base = 0;
2375 *count = 0;
2378 return context_gl->tex_unit_map;
2381 static void wined3d_context_gl_get_rt_size(const struct wined3d_context_gl *context_gl, SIZE *size)
2383 const struct wined3d_texture *rt = context_gl->c.current_rt.texture;
2384 unsigned int level;
2386 if (rt->swapchain)
2388 RECT window_size;
2390 GetClientRect(context_gl->window, &window_size);
2391 size->cx = window_size.right - window_size.left;
2392 size->cy = window_size.bottom - window_size.top;
2394 return;
2397 level = context_gl->c.current_rt.sub_resource_idx % rt->level_count;
2398 size->cx = wined3d_texture_get_level_width(rt, level);
2399 size->cy = wined3d_texture_get_level_height(rt, level);
2402 void wined3d_context_gl_enable_clip_distances(struct wined3d_context_gl *context_gl, uint32_t enable_mask)
2404 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2405 unsigned int clip_distance_count, i;
2406 uint32_t disable_mask, current_mask;
2408 clip_distance_count = gl_info->limits.user_clip_distances;
2409 disable_mask = ~enable_mask;
2410 enable_mask &= (1u << clip_distance_count) - 1;
2411 disable_mask &= (1u << clip_distance_count) - 1;
2412 current_mask = context_gl->c.clip_distance_mask;
2413 context_gl->c.clip_distance_mask = enable_mask;
2415 enable_mask &= ~current_mask;
2416 while (enable_mask)
2418 i = wined3d_bit_scan(&enable_mask);
2419 gl_info->gl_ops.gl.p_glEnable(GL_CLIP_DISTANCE0 + i);
2421 disable_mask &= current_mask;
2422 while (disable_mask)
2424 i = wined3d_bit_scan(&disable_mask);
2425 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_DISTANCE0 + i);
2427 checkGLcall("toggle clip distances");
2430 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2432 return rt_mask & (1u << 31);
2435 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2437 return rt_mask & ~(1u << 31);
2440 /* Context activation is done by the caller. */
2441 static void wined3d_context_gl_apply_draw_buffers(struct wined3d_context_gl *context_gl, uint32_t rt_mask)
2443 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2444 GLenum draw_buffers[WINED3D_MAX_RENDER_TARGETS];
2446 if (!rt_mask)
2448 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2450 else if (is_rt_mask_onscreen(rt_mask))
2452 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2454 else
2456 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2458 unsigned int i = 0;
2460 while (rt_mask)
2462 if (rt_mask & 1)
2463 draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2464 else
2465 draw_buffers[i] = GL_NONE;
2467 rt_mask >>= 1;
2468 ++i;
2471 if (gl_info->supported[ARB_DRAW_BUFFERS])
2473 GL_EXTCALL(glDrawBuffers(i, draw_buffers));
2475 else
2477 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffers[0]);
2480 else
2482 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2486 checkGLcall("apply draw buffers");
2489 /* Context activation is done by the caller. */
2490 void wined3d_context_gl_set_draw_buffer(struct wined3d_context_gl *context_gl, GLenum buffer)
2492 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2493 struct fbo_entry *current_fbo = context_gl->current_fbo;
2494 uint32_t new_mask = context_generate_rt_mask(buffer);
2495 uint32_t *current_mask;
2497 current_mask = current_fbo ? &current_fbo->rt_mask : &context_gl->draw_buffers_mask;
2498 if (new_mask == *current_mask)
2499 return;
2501 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2502 checkGLcall("glDrawBuffer()");
2504 *current_mask = new_mask;
2507 /* Context activation is done by the caller. */
2508 void wined3d_context_gl_active_texture(struct wined3d_context_gl *context_gl,
2509 const struct wined3d_gl_info *gl_info, unsigned int unit)
2511 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + unit));
2512 checkGLcall("glActiveTexture");
2513 context_gl->active_texture = unit;
2516 void wined3d_context_gl_bind_bo(struct wined3d_context_gl *context_gl, GLenum binding, GLuint name)
2518 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2520 if (binding == GL_ELEMENT_ARRAY_BUFFER)
2521 context_invalidate_state(&context_gl->c, STATE_INDEXBUFFER);
2523 GL_EXTCALL(glBindBuffer(binding, name));
2526 void wined3d_context_gl_bind_texture(struct wined3d_context_gl *context_gl, GLenum target, GLuint name)
2528 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
2529 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2530 GLenum old_texture_type;
2531 unsigned int unit;
2533 if (name)
2534 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2535 else
2536 target = GL_NONE;
2538 unit = context_gl->active_texture;
2539 old_texture_type = context_gl->texture_type[unit];
2540 if (old_texture_type != target)
2542 switch (old_texture_type)
2544 case GL_NONE:
2545 /* nothing to do */
2546 break;
2547 case GL_TEXTURE_1D:
2548 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
2549 break;
2550 case GL_TEXTURE_1D_ARRAY:
2551 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
2552 break;
2553 case GL_TEXTURE_2D:
2554 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
2555 break;
2556 case GL_TEXTURE_2D_ARRAY:
2557 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
2558 break;
2559 case GL_TEXTURE_RECTANGLE_ARB:
2560 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
2561 break;
2562 case GL_TEXTURE_CUBE_MAP:
2563 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
2564 break;
2565 case GL_TEXTURE_CUBE_MAP_ARRAY:
2566 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
2567 break;
2568 case GL_TEXTURE_3D:
2569 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
2570 break;
2571 case GL_TEXTURE_BUFFER:
2572 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
2573 break;
2574 case GL_TEXTURE_2D_MULTISAMPLE:
2575 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
2576 break;
2577 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2578 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
2579 break;
2580 default:
2581 ERR("Unexpected texture target %#x.\n", old_texture_type);
2584 context_gl->texture_type[unit] = target;
2587 checkGLcall("bind texture");
2590 static void wined3d_context_gl_poll_fences(struct wined3d_context_gl *context_gl)
2592 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2593 struct wined3d_command_fence_gl *f;
2594 SIZE_T i;
2596 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2598 f = &context_gl->submitted.fences[i];
2600 if (f->id > device_gl->completed_fence_id)
2602 if (wined3d_fence_test(f->fence, &device_gl->d, 0) != WINED3D_FENCE_OK)
2603 continue;
2604 device_gl->completed_fence_id = f->id;
2607 wined3d_fence_destroy(f->fence);
2608 if (i != context_gl->submitted.fence_count - 1)
2609 *f = context_gl->submitted.fences[context_gl->submitted.fence_count - 1];
2610 --context_gl->submitted.fence_count;
2614 void wined3d_context_gl_wait_command_fence(struct wined3d_context_gl *context_gl, uint64_t id)
2616 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2617 enum wined3d_fence_result ret;
2618 SIZE_T i;
2620 if (id <= device_gl->completed_fence_id
2621 || id > device_gl->current_fence_id) /* In case the fence ID wrapped. */
2622 return;
2624 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2626 if (context_gl->submitted.fences[i].id != id)
2627 continue;
2629 if ((ret = wined3d_fence_wait(context_gl->submitted.fences[i].fence, &device_gl->d)) != WINED3D_FENCE_OK)
2630 ERR("Failed to wait for command fence with id 0x%s, ret %#x.\n", wine_dbgstr_longlong(id), ret);
2631 wined3d_context_gl_poll_fences(context_gl);
2632 return;
2635 ERR("Failed to find fence for command fence with id 0x%s.\n", wine_dbgstr_longlong(id));
2638 void wined3d_context_gl_submit_command_fence(struct wined3d_context_gl *context_gl)
2640 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2641 struct wined3d_command_fence_gl *f;
2642 HRESULT hr;
2644 if (!wined3d_array_reserve((void **)&context_gl->submitted.fences, &context_gl->submitted.fences_size,
2645 context_gl->submitted.fence_count + 1, sizeof(*context_gl->submitted.fences)))
2646 ERR("Failed to grow submitted command buffer array.\n");
2648 f = &context_gl->submitted.fences[context_gl->submitted.fence_count++];
2649 f->id = device_gl->current_fence_id;
2650 if (FAILED(hr = wined3d_fence_create(&device_gl->d, &f->fence)))
2651 ERR("Failed to create fence, hr %#x.\n", hr);
2652 wined3d_fence_issue(f->fence, &device_gl->d);
2654 /* We don't expect this to ever happen, but handle it anyway. */
2655 if (!++device_gl->current_fence_id)
2657 wined3d_context_gl_wait_command_fence(context_gl, device_gl->current_fence_id - 1);
2658 device_gl->completed_fence_id = 0;
2659 device_gl->current_fence_id = 1;
2661 wined3d_context_gl_poll_fences(context_gl);
2664 static void *wined3d_bo_gl_map(struct wined3d_bo_gl *bo,
2665 struct wined3d_context_gl *context_gl, size_t offset, size_t size, uint32_t flags)
2667 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2668 const struct wined3d_gl_info *gl_info;
2669 struct wined3d_bo_user *bo_user;
2670 struct wined3d_bo_gl tmp;
2671 uint8_t *map_ptr;
2673 if (flags & WINED3D_MAP_NOOVERWRITE)
2674 goto map;
2676 if ((flags & WINED3D_MAP_DISCARD) && bo->command_fence_id > device_gl->completed_fence_id)
2678 if (wined3d_context_gl_create_bo(context_gl, bo->size,
2679 bo->binding, bo->usage, bo->coherent, bo->flags, &tmp))
2681 list_move_head(&tmp.users, &bo->users);
2682 wined3d_context_gl_destroy_bo(context_gl, bo);
2683 *bo = tmp;
2684 list_init(&bo->users);
2685 list_move_head(&bo->users, &tmp.users);
2686 LIST_FOR_EACH_ENTRY(bo_user, &bo->users, struct wined3d_bo_user, entry)
2688 bo_user->valid = false;
2691 goto map;
2694 ERR("Failed to create new buffer object.\n");
2697 if (bo->command_fence_id == device_gl->current_fence_id)
2698 wined3d_context_gl_submit_command_fence(context_gl);
2699 wined3d_context_gl_wait_command_fence(context_gl, bo->command_fence_id);
2701 map:
2702 gl_info = context_gl->gl_info;
2703 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2705 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2707 map_ptr = GL_EXTCALL(glMapBufferRange(bo->binding, offset, size, wined3d_resource_gl_map_flags(flags)));
2709 else
2711 map_ptr = GL_EXTCALL(glMapBuffer(bo->binding, wined3d_resource_gl_legacy_map_flags(flags)));
2712 map_ptr += offset;
2715 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2716 checkGLcall("Map buffer object");
2718 return map_ptr;
2721 void *wined3d_context_gl_map_bo_address(struct wined3d_context_gl *context_gl,
2722 const struct wined3d_bo_address *data, size_t size, uint32_t flags)
2724 struct wined3d_bo_gl *bo;
2725 void *map_ptr;
2727 if (!(bo = (struct wined3d_bo_gl *)data->buffer_object))
2728 return data->addr;
2730 if (!(map_ptr = wined3d_bo_gl_map(bo, context_gl, (uintptr_t)data->addr, size, flags)))
2731 ERR("Failed to map bo.\n");
2733 return map_ptr;
2736 void wined3d_context_gl_unmap_bo_address(struct wined3d_context_gl *context_gl,
2737 const struct wined3d_bo_address *data, unsigned int range_count, const struct wined3d_range *ranges)
2739 const struct wined3d_gl_info *gl_info;
2740 struct wined3d_bo_gl *bo;
2741 unsigned int i;
2743 if (!(bo = (struct wined3d_bo_gl *)data->buffer_object))
2744 return;
2746 gl_info = context_gl->gl_info;
2747 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2749 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2751 for (i = 0; i < range_count; ++i)
2753 GL_EXTCALL(glFlushMappedBufferRange(bo->binding,
2754 (UINT_PTR)data->addr + ranges[i].offset, ranges[i].size));
2757 else if (!bo->coherent && gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
2759 for (i = 0; i < range_count; ++i)
2761 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(bo->binding,
2762 (uintptr_t)data->addr + ranges[i].offset, ranges[i].size));
2763 checkGLcall("glFlushMappedBufferRangeAPPLE");
2767 GL_EXTCALL(glUnmapBuffer(bo->binding));
2768 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2769 checkGLcall("Unmap buffer object");
2772 void wined3d_context_gl_copy_bo_address(struct wined3d_context_gl *context_gl,
2773 const struct wined3d_bo_address *dst, const struct wined3d_bo_address *src, size_t size)
2775 const struct wined3d_gl_info *gl_info;
2776 struct wined3d_bo_gl *src_bo, *dst_bo;
2777 struct wined3d_range range;
2778 BYTE *dst_ptr, *src_ptr;
2780 gl_info = context_gl->gl_info;
2781 src_bo = (struct wined3d_bo_gl *)src->buffer_object;
2782 dst_bo = (struct wined3d_bo_gl *)dst->buffer_object;
2784 if (dst_bo && src_bo)
2786 if (gl_info->supported[ARB_COPY_BUFFER])
2788 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER, src_bo->id));
2789 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER, dst_bo->id));
2790 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
2791 (GLintptr)src->addr, (GLintptr)dst->addr, size));
2792 checkGLcall("direct buffer copy");
2794 wined3d_context_gl_reference_bo(context_gl, src_bo);
2795 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2797 else
2799 src_ptr = wined3d_context_gl_map_bo_address(context_gl, src, size, WINED3D_MAP_READ);
2800 dst_ptr = wined3d_context_gl_map_bo_address(context_gl, dst, size, WINED3D_MAP_WRITE);
2802 memcpy(dst_ptr, src_ptr, size);
2804 range.offset = 0;
2805 range.size = size;
2806 wined3d_context_gl_unmap_bo_address(context_gl, dst, 1, &range);
2807 wined3d_context_gl_unmap_bo_address(context_gl, src, 0, NULL);
2810 else if (!dst_bo && src_bo)
2812 wined3d_context_gl_bind_bo(context_gl, src_bo->binding, src_bo->id);
2813 GL_EXTCALL(glGetBufferSubData(src_bo->binding, (GLintptr)src->addr, size, dst->addr));
2814 checkGLcall("buffer download");
2816 wined3d_context_gl_reference_bo(context_gl, src_bo);
2818 else if (dst_bo && !src_bo)
2820 wined3d_context_gl_bind_bo(context_gl, dst_bo->binding, dst_bo->id);
2821 GL_EXTCALL(glBufferSubData(dst_bo->binding, (GLintptr)dst->addr, size, src->addr));
2822 checkGLcall("buffer upload");
2824 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2826 else
2828 memcpy(dst->addr, src->addr, size);
2832 void wined3d_context_gl_destroy_bo(struct wined3d_context_gl *context_gl, struct wined3d_bo_gl *bo)
2834 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2836 TRACE("context_gl %p, bo %p.\n", context_gl, bo);
2838 TRACE("Destroying GL buffer %u.\n", bo->id);
2839 GL_EXTCALL(glDeleteBuffers(1, &bo->id));
2840 checkGLcall("buffer object destruction");
2841 bo->id = 0;
2844 bool wined3d_context_gl_create_bo(struct wined3d_context_gl *context_gl, GLsizeiptr size,
2845 GLenum binding, GLenum usage, bool coherent, GLbitfield flags, struct wined3d_bo_gl *bo)
2847 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2848 GLuint id = 0;
2850 TRACE("context_gl %p, size %lu, binding %#x, usage %#x, coherent %#x, flags %#x, bo %p.\n",
2851 context_gl, size, binding, usage, coherent, flags, bo);
2853 GL_EXTCALL(glGenBuffers(1, &id));
2854 if (!id)
2856 checkGLcall("buffer object creation");
2857 return false;
2859 wined3d_context_gl_bind_bo(context_gl, binding, id);
2861 if (!coherent && gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
2863 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
2864 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
2867 if (gl_info->supported[ARB_BUFFER_STORAGE])
2868 GL_EXTCALL(glBufferStorage(binding, size, NULL, flags | GL_DYNAMIC_STORAGE_BIT));
2869 else
2870 GL_EXTCALL(glBufferData(binding, size, NULL, usage));
2872 wined3d_context_gl_bind_bo(context_gl, binding, 0);
2873 checkGLcall("buffer object creation");
2875 TRACE("Created buffer object %u.\n", id);
2876 bo->id = id;
2877 bo->size = size;
2878 bo->binding = binding;
2879 bo->usage = usage;
2880 bo->flags = flags;
2881 bo->coherent = coherent;
2882 list_init(&bo->users);
2883 bo->command_fence_id = 0;
2885 return true;
2888 static void wined3d_context_gl_set_render_offscreen(struct wined3d_context_gl *context_gl, BOOL offscreen)
2890 if (context_gl->c.render_offscreen == offscreen)
2891 return;
2893 context_invalidate_state(&context_gl->c, STATE_VIEWPORT);
2894 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
2895 if (!context_gl->gl_info->supported[ARB_CLIP_CONTROL])
2897 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
2898 context_invalidate_state(&context_gl->c, STATE_POINTSPRITECOORDORIGIN);
2899 context_invalidate_state(&context_gl->c, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2901 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN));
2902 if (context_gl->gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2903 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
2904 context_gl->c.render_offscreen = offscreen;
2907 GLenum wined3d_context_gl_get_offscreen_gl_buffer(const struct wined3d_context_gl *context_gl)
2909 switch (wined3d_settings.offscreen_rendering_mode)
2911 case ORM_FBO:
2912 return GL_COLOR_ATTACHMENT0;
2914 case ORM_BACKBUFFER:
2915 return context_gl->aux_buffers > 0 ? GL_AUX0 : GL_BACK;
2917 default:
2918 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
2919 return GL_BACK;
2923 static uint32_t wined3d_context_gl_generate_rt_mask_no_fbo(const struct wined3d_context_gl *context_gl,
2924 struct wined3d_resource *rt)
2926 if (!rt || rt->format->id == WINED3DFMT_NULL)
2927 return 0;
2928 else if (rt->type != WINED3D_RTYPE_BUFFER && texture_from_resource(rt)->swapchain)
2929 return context_generate_rt_mask_from_resource(rt);
2930 else
2931 return context_generate_rt_mask(wined3d_context_gl_get_offscreen_gl_buffer(context_gl));
2934 /* Context activation is done by the caller. */
2935 void wined3d_context_gl_apply_blit_state(struct wined3d_context_gl *context_gl, const struct wined3d_device *device)
2937 struct wined3d_context *context = &context_gl->c;
2938 const struct wined3d_gl_info *gl_info;
2939 uint32_t rt_mask, *cur_mask;
2940 struct wined3d_texture *rt;
2941 unsigned int sampler;
2942 SIZE rt_size;
2944 TRACE("Setting up context %p for blitting.\n", context);
2946 gl_info = context_gl->gl_info;
2947 rt = context->current_rt.texture;
2949 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2951 if (context->render_offscreen)
2953 wined3d_texture_load(rt, context, FALSE);
2955 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_FRAMEBUFFER, &rt->resource,
2956 context->current_rt.sub_resource_idx, NULL, 0, rt->resource.draw_binding);
2957 if (rt->resource.format->id != WINED3DFMT_NULL)
2958 rt_mask = 1;
2959 else
2960 rt_mask = 0;
2962 else
2964 context_gl->current_fbo = NULL;
2965 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
2966 rt_mask = context_generate_rt_mask_from_resource(&rt->resource);
2969 else
2971 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, &rt->resource);
2974 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
2976 if (rt_mask != *cur_mask)
2978 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
2979 *cur_mask = rt_mask;
2982 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2983 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
2984 context_invalidate_state(context, STATE_FRAMEBUFFER);
2986 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
2988 if (context->last_was_blit)
2990 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
2992 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
2993 context->viewport_count = WINED3D_MAX_VIEWPORTS;
2994 context_gl->blit_size = rt_size;
2995 /* No need to dirtify here, the states are still dirtified because
2996 * they weren't applied since the last context_apply_blit_state()
2997 * call. */
2999 checkGLcall("blit state application");
3000 TRACE("Context is already set up for blitting, nothing to do.\n");
3001 return;
3003 context->last_was_blit = TRUE;
3005 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
3006 GL_EXTCALL(glBindSampler(0, 0));
3007 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
3009 sampler = context_gl->rev_tex_unit_map[0];
3010 if (sampler != WINED3D_UNMAPPED_STAGE)
3012 if (sampler < WINED3D_MAX_TEXTURES)
3014 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
3015 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
3017 context_invalidate_state(context, STATE_SAMPLER(sampler));
3019 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
3020 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
3022 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3024 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
3025 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
3027 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3028 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3029 context_invalidate_state(context, STATE_BLEND);
3030 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
3031 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
3032 context_invalidate_state(context, STATE_RASTERIZER);
3033 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
3034 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
3035 context_invalidate_state(context, STATE_DEPTH_STENCIL);
3036 if (gl_info->supported[ARB_POINT_SPRITE])
3038 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
3039 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
3041 if (gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3043 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3044 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3047 context->last_was_rhw = TRUE;
3048 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
3050 wined3d_context_gl_enable_clip_distances(context_gl, 0);
3051 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
3053 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
3054 if (gl_info->supported[ARB_CLIP_CONTROL])
3055 GL_EXTCALL(glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE));
3056 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
3057 context->viewport_count = WINED3D_MAX_VIEWPORTS;
3058 context_invalidate_state(context, STATE_VIEWPORT);
3060 device->shader_backend->shader_disable(device->shader_priv, context);
3062 context_gl->blit_size = rt_size;
3064 checkGLcall("blit state application");
3067 static void wined3d_context_gl_apply_blit_projection(const struct wined3d_context_gl *context_gl,
3068 unsigned int w, unsigned int h)
3070 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3071 const GLdouble projection[] =
3073 2.0 / w, 0.0, 0.0, 0.0,
3074 0.0, 2.0 / h, 0.0, 0.0,
3075 0.0, 0.0, 2.0, 0.0,
3076 -1.0, -1.0, -1.0, 1.0,
3079 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
3080 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
3083 /* Setup OpenGL states for fixed-function blitting. */
3084 /* Context activation is done by the caller. */
3085 void wined3d_context_gl_apply_ffp_blit_state(struct wined3d_context_gl *context_gl,
3086 const struct wined3d_device *device)
3088 struct wined3d_context *context = &context_gl->c;
3089 const struct wined3d_gl_info *gl_info;
3090 unsigned int i, sampler;
3092 gl_info = context_gl->gl_info;
3093 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3094 ERR("Applying fixed-function state without legacy context support.\n");
3096 if (context->last_was_ffp_blit)
3098 SIZE rt_size;
3100 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
3101 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
3102 wined3d_context_gl_apply_blit_projection(context_gl, rt_size.cx, rt_size.cy);
3103 wined3d_context_gl_apply_blit_state(context_gl, device);
3105 checkGLcall("ffp blit state application");
3106 return;
3108 context->last_was_ffp_blit = TRUE;
3110 wined3d_context_gl_apply_blit_state(context_gl, device);
3112 /* Disable all textures. The caller can then bind a texture it wants to blit
3113 * from. */
3114 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
3116 wined3d_context_gl_active_texture(context_gl, gl_info, i);
3118 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3119 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3120 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3121 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3122 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3123 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3125 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3127 sampler = context_gl->rev_tex_unit_map[i];
3128 if (sampler != WINED3D_UNMAPPED_STAGE)
3130 if (sampler < WINED3D_MAX_TEXTURES)
3131 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
3132 context_invalidate_state(context, STATE_SAMPLER(sampler));
3136 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
3138 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3139 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3140 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3141 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3142 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3143 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3145 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3146 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
3147 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
3149 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
3150 gl_info->gl_ops.gl.p_glLoadIdentity();
3152 /* Setup transforms. */
3153 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3154 gl_info->gl_ops.gl.p_glLoadIdentity();
3155 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3156 wined3d_context_gl_apply_blit_projection(context_gl, context_gl->blit_size.cx, context_gl->blit_size.cy);
3157 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
3159 /* Other misc states. */
3160 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
3161 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
3162 gl_info->p_glDisableWINE(GL_FOG);
3163 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
3165 if (gl_info->supported[EXT_SECONDARY_COLOR])
3167 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
3168 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
3170 checkGLcall("ffp blit state application");
3173 static BOOL have_framebuffer_attachment(unsigned int rt_count, struct wined3d_rendertarget_view * const *rts,
3174 const struct wined3d_rendertarget_view *ds)
3176 unsigned int i;
3178 if (ds)
3179 return TRUE;
3181 for (i = 0; i < rt_count; ++i)
3183 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3184 return TRUE;
3187 return FALSE;
3190 /* Context activation is done by the caller. */
3191 BOOL wined3d_context_gl_apply_clear_state(struct wined3d_context_gl *context_gl,
3192 const struct wined3d_state *state, unsigned int rt_count, const struct wined3d_fb_state *fb)
3194 struct wined3d_rendertarget_view * const *rts = fb->render_targets;
3195 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3196 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
3197 uint32_t rt_mask = 0, *cur_mask;
3198 unsigned int i;
3200 if (isStateDirty(&context_gl->c, STATE_FRAMEBUFFER) || fb != &state->fb
3201 || rt_count != gl_info->limits.buffers)
3203 if (!have_framebuffer_attachment(rt_count, rts, dsv))
3205 WARN("Invalid render target config, need at least one attachment.\n");
3206 return FALSE;
3209 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3211 struct wined3d_rendertarget_info ds_info = {{0}};
3213 if (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource))
3215 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3216 for (i = 0; i < rt_count; ++i)
3218 if (rts[i])
3220 struct wined3d_rendertarget_view_gl *rtv_gl = wined3d_rendertarget_view_gl(rts[i]);
3221 context_gl->blit_targets[i].gl_view = rtv_gl->gl_view;
3222 context_gl->blit_targets[i].resource = rtv_gl->v.resource;
3223 context_gl->blit_targets[i].sub_resource_idx = rtv_gl->v.sub_resource_idx;
3224 context_gl->blit_targets[i].layer_count = rtv_gl->v.layer_count;
3226 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3227 rt_mask |= (1u << i);
3230 if (dsv)
3232 struct wined3d_rendertarget_view_gl *dsv_gl = wined3d_rendertarget_view_gl(dsv);
3233 ds_info.gl_view = dsv_gl->gl_view;
3234 ds_info.resource = dsv_gl->v.resource;
3235 ds_info.sub_resource_idx = dsv_gl->v.sub_resource_idx;
3236 ds_info.layer_count = dsv_gl->v.layer_count;
3239 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3240 rt_count ? rts[0]->resource->draw_binding : 0, dsv ? dsv->resource->draw_binding : 0);
3242 else
3244 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3245 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3246 rt_mask = context_generate_rt_mask_from_resource(rts[0]->resource);
3249 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3250 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3251 * state management allows this */
3252 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3254 else
3256 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3259 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3260 && (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource)))
3262 for (i = 0; i < rt_count; ++i)
3264 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3265 rt_mask |= (1u << i);
3268 else
3270 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3273 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3275 if (rt_mask != *cur_mask)
3277 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3278 *cur_mask = rt_mask;
3279 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3282 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3283 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3285 context_gl->c.last_was_blit = FALSE;
3286 context_gl->c.last_was_ffp_blit = FALSE;
3288 /* Blending and clearing should be orthogonal, but tests on the nvidia
3289 * driver show that disabling blending when clearing improves the clearing
3290 * performance incredibly. */
3291 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3292 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
3293 if (rt_count && gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3295 if (needs_srgb_write(context_gl->c.d3d_info, state, fb))
3296 gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
3297 else
3298 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3299 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3301 checkGLcall("setting up state for clear");
3303 context_invalidate_state(&context_gl->c, STATE_BLEND);
3304 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3305 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
3307 return TRUE;
3310 static uint32_t find_draw_buffers_mask(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3312 struct wined3d_rendertarget_view * const *rts = state->fb.render_targets;
3313 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3314 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3315 unsigned int rt_mask, mask;
3316 unsigned int i;
3318 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3319 return wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rts[0]->resource);
3320 else if (!context_gl->c.render_offscreen)
3321 return context_generate_rt_mask_from_resource(rts[0]->resource);
3323 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
3324 rt_mask &= (1u << gl_info->limits.buffers) - 1;
3325 if (state->blend_state && state->blend_state->dual_source)
3326 rt_mask = 1;
3328 mask = rt_mask;
3329 while (mask)
3331 i = wined3d_bit_scan(&mask);
3332 if (!rts[i] || rts[i]->format->id == WINED3DFMT_NULL)
3333 rt_mask &= ~(1u << i);
3336 return rt_mask;
3339 /* Context activation is done by the caller. */
3340 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3342 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3343 uint32_t rt_mask = find_draw_buffers_mask(context_gl, state);
3344 const struct wined3d_fb_state *fb = &state->fb;
3345 DWORD color_location = 0;
3346 DWORD *cur_mask;
3348 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3350 struct wined3d_rendertarget_info ds_info = {{0}};
3352 if (!context->render_offscreen)
3354 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3355 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3357 else
3359 const struct wined3d_rendertarget_view_gl *view_gl;
3360 unsigned int i;
3362 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3363 for (i = 0; i < context_gl->gl_info->limits.buffers; ++i)
3365 if (!fb->render_targets[i])
3366 continue;
3368 view_gl = wined3d_rendertarget_view_gl(fb->render_targets[i]);
3369 context_gl->blit_targets[i].gl_view = view_gl->gl_view;
3370 context_gl->blit_targets[i].resource = view_gl->v.resource;
3371 context_gl->blit_targets[i].sub_resource_idx = view_gl->v.sub_resource_idx;
3372 context_gl->blit_targets[i].layer_count = view_gl->v.layer_count;
3374 if (!color_location)
3375 color_location = view_gl->v.resource->draw_binding;
3378 if (fb->depth_stencil)
3380 view_gl = wined3d_rendertarget_view_gl(fb->depth_stencil);
3381 ds_info.gl_view = view_gl->gl_view;
3382 ds_info.resource = view_gl->v.resource;
3383 ds_info.sub_resource_idx = view_gl->v.sub_resource_idx;
3384 ds_info.layer_count = view_gl->v.layer_count;
3387 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3388 color_location, fb->depth_stencil ? fb->depth_stencil->resource->draw_binding : 0);
3392 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3393 if (rt_mask != *cur_mask)
3395 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3396 *cur_mask = rt_mask;
3398 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
3401 static void wined3d_context_gl_map_stage(struct wined3d_context_gl *context_gl, unsigned int stage, unsigned int unit)
3403 unsigned int i = context_gl->rev_tex_unit_map[unit];
3404 unsigned int j = context_gl->tex_unit_map[stage];
3406 TRACE("Mapping stage %u to unit %u.\n", stage, unit);
3407 context_gl->tex_unit_map[stage] = unit;
3408 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
3409 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
3411 context_gl->rev_tex_unit_map[unit] = stage;
3412 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
3413 context_gl->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
3416 static void context_invalidate_texture_stage(struct wined3d_context *context, DWORD stage)
3418 DWORD i;
3420 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
3421 context_invalidate_state(context, STATE_TEXTURESTAGE(stage, i));
3424 static void context_update_fixed_function_usage_map(struct wined3d_context *context,
3425 const struct wined3d_state *state)
3427 UINT i, start, end;
3429 context->fixed_function_usage_map = 0;
3430 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
3432 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
3433 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
3434 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
3435 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
3436 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
3437 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
3438 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
3439 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
3441 /* Not used, and disable higher stages. */
3442 if (color_op == WINED3D_TOP_DISABLE)
3443 break;
3445 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
3446 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
3447 || ((color_arg3 == WINED3DTA_TEXTURE)
3448 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
3449 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
3450 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
3451 || ((alpha_arg3 == WINED3DTA_TEXTURE)
3452 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
3453 context->fixed_function_usage_map |= (1u << i);
3455 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
3456 && i < WINED3D_MAX_TEXTURES - 1)
3457 context->fixed_function_usage_map |= (1u << (i + 1));
3460 if (i < context->lowest_disabled_stage)
3462 start = i;
3463 end = context->lowest_disabled_stage;
3465 else
3467 start = context->lowest_disabled_stage;
3468 end = i;
3471 context->lowest_disabled_stage = i;
3472 for (i = start + 1; i < end; ++i)
3474 context_invalidate_state(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3478 static void wined3d_context_gl_map_fixed_function_samplers(struct wined3d_context_gl *context_gl,
3479 const struct wined3d_state *state)
3481 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3482 unsigned int i, tex;
3483 WORD ffu_map;
3485 ffu_map = context_gl->c.fixed_function_usage_map;
3487 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
3488 || context_gl->c.lowest_disabled_stage <= d3d_info->limits.ffp_textures)
3490 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3492 if (!(ffu_map & 1))
3493 continue;
3495 if (context_gl->tex_unit_map[i] != i)
3497 wined3d_context_gl_map_stage(context_gl, i, i);
3498 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3499 context_invalidate_texture_stage(&context_gl->c, i);
3502 return;
3505 /* Now work out the mapping */
3506 tex = 0;
3507 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3509 if (!(ffu_map & 1))
3510 continue;
3512 if (context_gl->tex_unit_map[i] != tex)
3514 wined3d_context_gl_map_stage(context_gl, i, tex);
3515 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3516 context_invalidate_texture_stage(&context_gl->c, i);
3519 ++tex;
3523 static void wined3d_context_gl_map_psamplers(struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3525 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3526 const struct wined3d_shader_resource_info *resource_info =
3527 state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3528 unsigned int i;
3530 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
3532 if (resource_info[i].type && context_gl->tex_unit_map[i] != i)
3534 wined3d_context_gl_map_stage(context_gl, i, i);
3535 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3536 if (i < d3d_info->limits.ffp_blend_stages)
3537 context_invalidate_texture_stage(&context_gl->c, i);
3542 static BOOL wined3d_context_gl_unit_free_for_vs(const struct wined3d_context_gl *context_gl,
3543 const struct wined3d_shader_resource_info *ps_resource_info, unsigned int unit)
3545 unsigned int current_mapping = context_gl->rev_tex_unit_map[unit];
3547 /* Not currently used */
3548 if (current_mapping == WINED3D_UNMAPPED_STAGE)
3549 return TRUE;
3551 if (current_mapping < WINED3D_MAX_FRAGMENT_SAMPLERS)
3553 /* Used by a fragment sampler */
3555 if (!ps_resource_info)
3557 /* No pixel shader, check fixed function */
3558 return current_mapping >= WINED3D_MAX_TEXTURES
3559 || !(context_gl->c.fixed_function_usage_map & (1u << current_mapping));
3562 /* Pixel shader, check the shader's sampler map */
3563 return !ps_resource_info[current_mapping].type;
3566 return TRUE;
3569 static void wined3d_context_gl_map_vsamplers(struct wined3d_context_gl *context_gl,
3570 BOOL ps, const struct wined3d_state *state)
3572 const struct wined3d_shader_resource_info *vs_resource_info =
3573 state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info;
3574 const struct wined3d_shader_resource_info *ps_resource_info = NULL;
3575 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3576 int start = min(WINED3D_MAX_COMBINED_SAMPLERS, gl_info->limits.graphics_samplers) - 1;
3577 int i;
3579 /* Note that we only care if a resource is used or not, not the
3580 * resource's specific type. Otherwise we'd need to call
3581 * shader_update_samplers() here for 1.x pixelshaders. */
3582 if (ps)
3583 ps_resource_info = state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3585 for (i = 0; i < WINED3D_MAX_VERTEX_SAMPLERS; ++i)
3587 DWORD vsampler_idx = i + WINED3D_MAX_FRAGMENT_SAMPLERS;
3588 if (vs_resource_info[i].type)
3590 while (start >= 0)
3592 if (wined3d_context_gl_unit_free_for_vs(context_gl, ps_resource_info, start))
3594 if (context_gl->tex_unit_map[vsampler_idx] != start)
3596 wined3d_context_gl_map_stage(context_gl, vsampler_idx, start);
3597 context_invalidate_state(&context_gl->c, STATE_SAMPLER(vsampler_idx));
3600 --start;
3601 break;
3604 --start;
3606 if (context_gl->tex_unit_map[vsampler_idx] == WINED3D_UNMAPPED_STAGE)
3607 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i);
3612 static void wined3d_context_gl_update_tex_unit_map(struct wined3d_context_gl *context_gl,
3613 const struct wined3d_state *state)
3615 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3616 BOOL vs = use_vs(state);
3617 BOOL ps = use_ps(state);
3619 if (!ps)
3620 context_update_fixed_function_usage_map(&context_gl->c, state);
3622 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3623 * need a 1:1 map at the moment.
3624 * When the mapping of a stage is changed, sampler and ALL texture stage
3625 * states have to be reset. */
3627 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
3628 return;
3630 if (ps)
3631 wined3d_context_gl_map_psamplers(context_gl, state);
3632 else
3633 wined3d_context_gl_map_fixed_function_samplers(context_gl, state);
3635 if (vs)
3636 wined3d_context_gl_map_vsamplers(context_gl, ps, state);
3639 /* Context activation is done by the caller. */
3640 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3642 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3643 uint32_t rt_mask, *cur_mask;
3645 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
3647 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3648 rt_mask = find_draw_buffers_mask(context_gl, state);
3649 if (rt_mask != *cur_mask)
3651 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3652 *cur_mask = rt_mask;
3656 static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl *context_gl,
3657 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
3659 unsigned int bind_idx, shader_sampler_count, base, count, i;
3660 const struct wined3d_device *device = context_gl->c.device;
3661 struct wined3d_shader_sampler_map_entry *entry;
3662 struct wined3d_shader_resource_view *view;
3663 const struct wined3d_shader *shader;
3664 const unsigned int *tex_unit_map;
3665 struct wined3d_sampler *sampler;
3667 if (!(shader = state->shader[shader_type]))
3668 return;
3670 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl,
3671 &shader->reg_maps.shader_version, &base, &count);
3673 shader_sampler_count = shader->reg_maps.sampler_map.count;
3674 if (shader_sampler_count > count)
3675 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3676 shader, shader_sampler_count, count);
3677 count = min(shader_sampler_count, count);
3679 for (i = 0; i < count; ++i)
3681 entry = &shader->reg_maps.sampler_map.entries[i];
3682 bind_idx = base + entry->bind_idx;
3683 if (tex_unit_map)
3684 bind_idx = tex_unit_map[bind_idx];
3686 if (!(view = state->shader_resource_view[shader_type][entry->resource_idx]))
3688 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
3689 continue;
3692 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
3693 sampler = device->default_sampler;
3694 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
3695 sampler = device->null_sampler;
3696 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view),
3697 bind_idx, wined3d_sampler_gl(sampler), context_gl);
3701 static void wined3d_context_gl_bind_unordered_access_views(struct wined3d_context_gl *context_gl,
3702 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3704 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3705 struct wined3d_unordered_access_view_gl *view_gl;
3706 const struct wined3d_format_gl *format_gl;
3707 GLuint texture_name;
3708 unsigned int i;
3709 GLint level;
3711 if (!shader)
3712 return;
3714 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3716 if (!views[i])
3718 if (shader->reg_maps.uav_resource_info[i].type)
3719 WARN("No unordered access view bound at index %u.\n", i);
3720 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3721 continue;
3724 view_gl = wined3d_unordered_access_view_gl(views[i]);
3725 if (view_gl->gl_view.name)
3727 texture_name = view_gl->gl_view.name;
3728 level = 0;
3730 else if (view_gl->v.resource->type != WINED3D_RTYPE_BUFFER)
3732 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(view_gl->v.resource));
3733 texture_name = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, FALSE);
3734 level = view_gl->v.desc.u.texture.level_idx;
3736 else
3738 FIXME("Unsupported buffer unordered access view.\n");
3739 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3740 continue;
3743 format_gl = wined3d_format_gl(view_gl->v.format);
3744 GL_EXTCALL(glBindImageTexture(i, texture_name, level, GL_TRUE, 0, GL_READ_WRITE,
3745 format_gl->internal));
3747 if (view_gl->counter_bo.id)
3748 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, i, view_gl->counter_bo.id));
3750 checkGLcall("Bind unordered access views");
3753 static void context_gl_load_shader_resources(struct wined3d_context_gl *context_gl,
3754 const struct wined3d_state *state, unsigned int shader_mask)
3756 struct wined3d_shader_sampler_map_entry *entry;
3757 struct wined3d_shader_resource_view_gl *srv_gl;
3758 struct wined3d_shader_resource_view *view;
3759 struct wined3d_buffer_gl *buffer_gl;
3760 struct wined3d_shader *shader;
3761 unsigned int i, j;
3763 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
3765 if (!(shader_mask & (1u << i)))
3766 continue;
3768 if (!(shader = state->shader[i]))
3769 continue;
3771 for (j = 0; j < WINED3D_MAX_CBS; ++j)
3773 if (!state->cb[i][j])
3774 continue;
3776 buffer_gl = wined3d_buffer_gl(state->cb[i][j]);
3777 wined3d_buffer_load(&buffer_gl->b, &context_gl->c, state);
3778 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3779 if (!buffer_gl->bo_user.valid)
3780 device_invalidate_state(context_gl->c.device, STATE_CONSTANT_BUFFER(i));
3783 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
3785 entry = &shader->reg_maps.sampler_map.entries[j];
3787 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
3788 continue;
3790 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3792 buffer_gl = wined3d_buffer_gl(buffer_from_resource(view->resource));
3793 wined3d_buffer_load(&buffer_gl->b, &context_gl->c, state);
3794 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3796 srv_gl = wined3d_shader_resource_view_gl(view);
3797 if (!srv_gl->bo_user.valid)
3798 wined3d_shader_resource_view_gl_update(srv_gl, context_gl);
3800 else
3802 wined3d_texture_load(texture_from_resource(view->resource), &context_gl->c, FALSE);
3808 static void context_gl_load_unordered_access_resources(struct wined3d_context_gl *context_gl,
3809 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3811 struct wined3d_unordered_access_view_gl *uav_gl;
3812 struct wined3d_unordered_access_view *view;
3813 struct wined3d_buffer_gl *buffer_gl;
3814 struct wined3d_texture *texture;
3815 unsigned int i;
3817 context_gl->c.uses_uavs = 0;
3819 if (!shader)
3820 return;
3822 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3824 if (!(view = views[i]))
3825 continue;
3827 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3829 buffer_gl = wined3d_buffer_gl(buffer_from_resource(view->resource));
3830 wined3d_buffer_load_location(&buffer_gl->b, &context_gl->c, WINED3D_LOCATION_BUFFER);
3831 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
3832 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3834 uav_gl = wined3d_unordered_access_view_gl(view);
3835 if (!uav_gl->bo_user.valid)
3836 wined3d_unordered_access_view_gl_update(uav_gl, context_gl);
3838 else
3840 texture = texture_from_resource(view->resource);
3841 wined3d_texture_load(texture, &context_gl->c, FALSE);
3842 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_TEXTURE_RGB);
3845 context_gl->c.uses_uavs = 1;
3849 static void context_gl_load_stream_output_buffers(struct wined3d_context_gl *context_gl,
3850 const struct wined3d_state *state)
3852 unsigned int i;
3854 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3856 struct wined3d_buffer_gl *buffer_gl;
3858 if (!state->stream_output[i].buffer)
3859 continue;
3861 buffer_gl = wined3d_buffer_gl(state->stream_output[i].buffer);
3862 wined3d_buffer_load(&buffer_gl->b, &context_gl->c, state);
3863 wined3d_buffer_invalidate_location(&buffer_gl->b, ~WINED3D_LOCATION_BUFFER);
3864 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3865 if (!buffer_gl->bo_user.valid)
3866 device_invalidate_state(context_gl->c.device, STATE_STREAM_OUTPUT);
3870 /* Context activation is done by the caller. */
3871 static BOOL context_apply_draw_state(struct wined3d_context *context,
3872 const struct wined3d_device *device, const struct wined3d_state *state, BOOL indexed)
3874 const struct wined3d_state_entry *state_table = context->state_table;
3875 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3876 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3877 const struct wined3d_fb_state *fb = &state->fb;
3878 unsigned int i, base;
3879 uint32_t map;
3881 context->uses_fbo_attached_resources = 0;
3883 if (!have_framebuffer_attachment(gl_info->limits.buffers, fb->render_targets, fb->depth_stencil))
3885 if (!gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
3887 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
3888 return FALSE;
3891 wined3d_context_gl_set_render_offscreen(context_gl, TRUE);
3894 /* Preload resources before FBO setup. Texture preload in particular may
3895 * result in changes to the current FBO, due to using e.g. FBO blits for
3896 * updating a resource location. */
3897 wined3d_context_gl_update_tex_unit_map(context_gl, state);
3898 context_preload_textures(context, state);
3899 context_gl_load_shader_resources(context_gl, state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
3900 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_PIXEL],
3901 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3902 context_gl_load_stream_output_buffers(context_gl, state);
3903 /* TODO: Right now the dependency on the vertex shader is necessary
3904 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3905 * the current VS but maybe it's possible to relax the coupling in some
3906 * situations at least. */
3907 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC)
3908 || isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
3910 context_update_stream_info(context, state);
3913 map = context->stream_info.use_map;
3914 while (map)
3916 const struct wined3d_stream_info_element *e;
3917 struct wined3d_buffer_gl *buffer_gl;
3919 e = &context->stream_info.elements[wined3d_bit_scan(&map)];
3920 buffer_gl = wined3d_buffer_gl(state->streams[e->stream_idx].buffer);
3922 if (!buffer_gl->bo_user.valid)
3923 device_invalidate_state(device, STATE_STREAMSRC);
3924 else
3925 wined3d_buffer_load(&buffer_gl->b, context, state);
3926 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3928 /* Loading the buffers above may have invalidated the stream info. */
3929 if (wined3d_context_is_graphics_state_dirty(context, STATE_STREAMSRC))
3930 context_update_stream_info(context, state);
3932 if (indexed && state->index_buffer)
3934 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(state->index_buffer);
3936 if (context->stream_info.all_vbo)
3938 wined3d_buffer_load(&buffer_gl->b, context, state);
3939 if (!buffer_gl->bo_user.valid)
3940 device_invalidate_state(device, STATE_INDEXBUFFER);
3941 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3943 else
3945 wined3d_buffer_load_sysmem(&buffer_gl->b, context);
3949 for (i = 0, base = 0; i < ARRAY_SIZE(context->dirty_graphics_states); ++i)
3951 uint32_t dirty_mask = context->dirty_graphics_states[i];
3953 while (dirty_mask)
3955 unsigned int state_id = base + wined3d_bit_scan(&dirty_mask);
3957 state_table[state_id].apply(context, state, state_id);
3958 context->dirty_graphics_states[i] &= ~(1u << (state_id - base));
3960 base += sizeof(dirty_mask) * CHAR_BIT;
3963 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
3965 device->shader_backend->shader_select(device->shader_priv, context, state);
3966 context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3969 if (context->constant_update_mask)
3971 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
3972 context->constant_update_mask = 0;
3975 if (context->update_shader_resource_bindings)
3977 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
3978 wined3d_context_gl_bind_shader_resources(context_gl, state, i);
3979 context->update_shader_resource_bindings = 0;
3980 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
3981 context->update_compute_shader_resource_bindings = 1;
3984 if (context->update_unordered_access_view_bindings)
3986 wined3d_context_gl_bind_unordered_access_views(context_gl,
3987 state->shader[WINED3D_SHADER_TYPE_PIXEL],
3988 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3989 context->update_unordered_access_view_bindings = 0;
3990 context->update_compute_unordered_access_view_bindings = 1;
3993 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3994 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3996 context->last_was_blit = FALSE;
3997 context->last_was_ffp_blit = FALSE;
3999 return TRUE;
4002 static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl *context_gl,
4003 const struct wined3d_device *device, const struct wined3d_state *state)
4005 const struct wined3d_state_entry *state_table = context_gl->c.state_table;
4006 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4007 unsigned int state_id, i;
4009 context_gl_load_shader_resources(context_gl, state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
4010 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4011 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4013 for (i = 0, state_id = STATE_COMPUTE_OFFSET; i < ARRAY_SIZE(context_gl->c.dirty_compute_states); ++i)
4015 unsigned int dirty_mask = context_gl->c.dirty_compute_states[i];
4017 while (dirty_mask)
4019 unsigned int current_state_id = state_id + wined3d_bit_scan(&dirty_mask);
4020 state_table[current_state_id].apply(&context_gl->c, state, current_state_id);
4022 state_id += sizeof(*context_gl->c.dirty_compute_states) * CHAR_BIT;
4024 memset(context_gl->c.dirty_compute_states, 0, sizeof(*context_gl->c.dirty_compute_states));
4026 if (context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
4028 device->shader_backend->shader_select_compute(device->shader_priv, &context_gl->c, state);
4029 context_gl->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
4032 if (context_gl->c.update_compute_shader_resource_bindings)
4034 wined3d_context_gl_bind_shader_resources(context_gl, state, WINED3D_SHADER_TYPE_COMPUTE);
4035 context_gl->c.update_compute_shader_resource_bindings = 0;
4036 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4037 context_gl->c.update_shader_resource_bindings = 1;
4040 if (context_gl->c.update_compute_unordered_access_view_bindings)
4042 wined3d_context_gl_bind_unordered_access_views(context_gl,
4043 state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4044 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4045 context_gl->c.update_compute_unordered_access_view_bindings = 0;
4046 context_gl->c.update_unordered_access_view_bindings = 1;
4049 /* Updates to currently bound render targets aren't necessarily coherent
4050 * between the graphics and compute pipelines. Unbind any currently bound
4051 * FBO here to ensure preceding updates to its attachments by the graphics
4052 * pipeline are visible to the compute pipeline.
4054 * Without this, the bloom effect in Nier:Automata is too bright on the
4055 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4056 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
4057 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
4059 context_gl->c.last_was_blit = FALSE;
4060 context_gl->c.last_was_ffp_blit = FALSE;
4063 void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl *context_gl)
4065 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4067 if (context_gl->c.transform_feedback_active)
4069 GL_EXTCALL(glEndTransformFeedback());
4070 checkGLcall("glEndTransformFeedback");
4071 context_gl->c.transform_feedback_active = 0;
4072 context_gl->c.transform_feedback_paused = 0;
4076 static void wined3d_context_gl_pause_transform_feedback(struct wined3d_context_gl *context_gl, BOOL force)
4078 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4080 if (!context_gl->c.transform_feedback_active || context_gl->c.transform_feedback_paused)
4081 return;
4083 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK2])
4085 GL_EXTCALL(glPauseTransformFeedback());
4086 checkGLcall("glPauseTransformFeedback");
4087 context_gl->c.transform_feedback_paused = 1;
4088 return;
4091 WARN("Cannot pause transform feedback operations.\n");
4093 if (force)
4094 wined3d_context_gl_end_transform_feedback(context_gl);
4097 static void wined3d_context_gl_setup_target(struct wined3d_context_gl *context_gl,
4098 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4100 BOOL old_render_offscreen = context_gl->c.render_offscreen, render_offscreen;
4102 render_offscreen = wined3d_resource_is_offscreen(&texture->resource);
4103 if (context_gl->c.current_rt.texture == texture
4104 && context_gl->c.current_rt.sub_resource_idx == sub_resource_idx
4105 && render_offscreen == old_render_offscreen)
4106 return;
4108 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4109 * the alpha blend state changes with different render target formats. */
4110 if (!context_gl->c.current_rt.texture)
4112 context_invalidate_state(&context_gl->c, STATE_BLEND);
4114 else
4116 const struct wined3d_format *old = context_gl->c.current_rt.texture->resource.format;
4117 const struct wined3d_format *new = texture->resource.format;
4119 if (old->id != new->id)
4121 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4122 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
4123 || !(texture->resource.format_flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
4124 context_invalidate_state(&context_gl->c, STATE_BLEND);
4126 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
4127 if ((context_gl->c.current_rt.texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE)
4128 != (texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE))
4129 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
4132 /* When switching away from an offscreen render target, and we're not
4133 * using FBOs, we have to read the drawable into the texture. This is
4134 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4135 * There are some things that need care though. PreLoad needs a GL context,
4136 * and FindContext is called before the context is activated. It also
4137 * has to be called with the old rendertarget active, otherwise a
4138 * wrong drawable is read. */
4139 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4140 && old_render_offscreen && (context_gl->c.current_rt.texture != texture
4141 || context_gl->c.current_rt.sub_resource_idx != sub_resource_idx))
4143 struct wined3d_texture_gl *prev_texture = wined3d_texture_gl(context_gl->c.current_rt.texture);
4144 unsigned int prev_sub_resource_idx = context_gl->c.current_rt.sub_resource_idx;
4146 /* Read the back buffer of the old drawable into the destination texture. */
4147 if (prev_texture->texture_srgb.name)
4148 wined3d_texture_load(&prev_texture->t, &context_gl->c, TRUE);
4149 wined3d_texture_load(&prev_texture->t, &context_gl->c, FALSE);
4150 wined3d_texture_invalidate_location(&prev_texture->t, prev_sub_resource_idx, WINED3D_LOCATION_DRAWABLE);
4154 context_gl->c.current_rt.texture = texture;
4155 context_gl->c.current_rt.sub_resource_idx = sub_resource_idx;
4156 wined3d_context_gl_set_render_offscreen(context_gl, render_offscreen);
4159 static void wined3d_context_gl_activate(struct wined3d_context_gl *context_gl,
4160 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4162 wined3d_context_gl_enter(context_gl);
4163 wined3d_context_gl_update_window(context_gl);
4164 wined3d_context_gl_setup_target(context_gl, texture, sub_resource_idx);
4165 if (!context_gl->valid)
4166 return;
4168 if (context_gl != wined3d_context_gl_get_current())
4170 if (!wined3d_context_gl_set_current(context_gl))
4171 ERR("Failed to activate the new context.\n");
4173 else if (context_gl->needs_set)
4175 wined3d_context_gl_set_gl_context(context_gl);
4179 struct wined3d_context *wined3d_context_gl_acquire(const struct wined3d_device *device,
4180 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4182 struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
4183 struct wined3d_context_gl *context_gl;
4185 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device, texture, sub_resource_idx);
4187 if (current_context && current_context->c.destroyed)
4188 current_context = NULL;
4190 if (!texture)
4192 if (current_context
4193 && current_context->c.current_rt.texture
4194 && current_context->c.device == device)
4196 texture = current_context->c.current_rt.texture;
4197 sub_resource_idx = current_context->c.current_rt.sub_resource_idx;
4199 else
4201 struct wined3d_swapchain *swapchain = device->swapchains[0];
4203 if (swapchain->back_buffers)
4204 texture = swapchain->back_buffers[0];
4205 else
4206 texture = swapchain->front_buffer;
4207 sub_resource_idx = 0;
4211 if (current_context && current_context->c.current_rt.texture == texture)
4213 context_gl = current_context;
4215 else if (!wined3d_resource_is_offscreen(&texture->resource))
4217 TRACE("Rendering onscreen.\n");
4219 if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(texture->swapchain))))
4220 return NULL;
4222 else
4224 TRACE("Rendering offscreen.\n");
4226 /* Stay with the current context if possible. Otherwise use the
4227 * context for the primary swapchain. */
4228 if (current_context && current_context->c.device == device)
4229 context_gl = current_context;
4230 else if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(device->swapchains[0]))))
4231 return NULL;
4234 wined3d_context_gl_activate(context_gl, texture, sub_resource_idx);
4236 return &context_gl->c;
4239 struct wined3d_context_gl *wined3d_context_gl_reacquire(struct wined3d_context_gl *context_gl)
4241 struct wined3d_context *acquired_context;
4242 struct wined3d_device *device;
4244 if (!context_gl || context_gl->tid != GetCurrentThreadId())
4245 return NULL;
4247 device = context_gl->c.device;
4248 wined3d_from_cs(device->cs);
4250 if (context_gl->c.current_rt.texture)
4252 wined3d_context_gl_activate(context_gl, context_gl->c.current_rt.texture,
4253 context_gl->c.current_rt.sub_resource_idx);
4254 return context_gl;
4257 acquired_context = context_acquire(device, NULL, 0);
4258 if (acquired_context != &context_gl->c)
4259 ERR("Acquired context %p instead of %p.\n", acquired_context, &context_gl->c);
4260 return wined3d_context_gl(acquired_context);
4263 void dispatch_compute(struct wined3d_device *device, const struct wined3d_state *state,
4264 const struct wined3d_dispatch_parameters *parameters)
4266 const struct wined3d_gl_info *gl_info;
4267 struct wined3d_context_gl *context_gl;
4269 context_gl = wined3d_context_gl(context_acquire(device, NULL, 0));
4270 if (!context_gl->valid)
4272 context_release(&context_gl->c);
4273 WARN("Invalid context, skipping dispatch.\n");
4274 return;
4276 gl_info = context_gl->gl_info;
4278 if (!gl_info->supported[ARB_COMPUTE_SHADER])
4280 context_release(&context_gl->c);
4281 FIXME("OpenGL implementation does not support compute shaders.\n");
4282 return;
4285 if (parameters->indirect)
4286 wined3d_buffer_load(parameters->u.indirect.buffer, &context_gl->c, state);
4288 wined3d_context_gl_apply_compute_state(context_gl, device, state);
4290 if (parameters->indirect)
4292 const struct wined3d_indirect_dispatch_parameters *indirect = &parameters->u.indirect;
4293 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(indirect->buffer);
4295 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, buffer_gl->bo.id));
4296 GL_EXTCALL(glDispatchComputeIndirect((GLintptr)indirect->offset));
4297 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0));
4298 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
4300 else
4302 const struct wined3d_direct_dispatch_parameters *direct = &parameters->u.direct;
4303 GL_EXTCALL(glDispatchCompute(direct->group_count_x, direct->group_count_y, direct->group_count_z));
4305 checkGLcall("dispatch compute");
4307 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4308 checkGLcall("glMemoryBarrier");
4310 context_release(&context_gl->c);
4313 /* Context activation is done by the caller. */
4314 static void wined3d_context_gl_draw_primitive_arrays(struct wined3d_context_gl *context_gl,
4315 const struct wined3d_state *state, const void *idx_data, unsigned int idx_size, int base_vertex_idx,
4316 unsigned int start_idx, unsigned int count, unsigned int start_instance, unsigned int instance_count)
4318 const struct wined3d_ffp_attrib_ops *ops = &context_gl->c.d3d_info->ffp_attrib_ops;
4319 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4320 const struct wined3d_stream_info *si = &context_gl->c.stream_info;
4321 GLenum mode = gl_primitive_type_from_d3d(state->primitive_type);
4322 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4323 unsigned int instanced_elements[ARRAY_SIZE(si->elements)];
4324 unsigned int instanced_element_count = 0;
4325 const void *indices;
4326 unsigned int i, j;
4328 indices = (const char *)idx_data + idx_size * start_idx;
4330 if (!instance_count)
4332 if (!idx_size)
4334 gl_info->gl_ops.gl.p_glDrawArrays(mode, start_idx, count);
4335 checkGLcall("glDrawArrays");
4336 return;
4339 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4341 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4342 checkGLcall("glDrawElementsBaseVertex");
4343 return;
4346 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4347 checkGLcall("glDrawElements");
4348 return;
4351 if (start_instance && !(gl_info->supported[ARB_BASE_INSTANCE] && gl_info->supported[ARB_INSTANCED_ARRAYS]))
4352 FIXME("Start instance (%u) not supported.\n", start_instance);
4354 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
4356 if (!idx_size)
4358 if (gl_info->supported[ARB_BASE_INSTANCE])
4360 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode, start_idx, count, instance_count, start_instance));
4361 checkGLcall("glDrawArraysInstancedBaseInstance");
4362 return;
4365 GL_EXTCALL(glDrawArraysInstanced(mode, start_idx, count, instance_count));
4366 checkGLcall("glDrawArraysInstanced");
4367 return;
4370 if (gl_info->supported[ARB_BASE_INSTANCE])
4372 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode, count, idx_type,
4373 indices, instance_count, base_vertex_idx, start_instance));
4374 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4375 return;
4377 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4379 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode, count, idx_type,
4380 indices, instance_count, base_vertex_idx));
4381 checkGLcall("glDrawElementsInstancedBaseVertex");
4382 return;
4385 GL_EXTCALL(glDrawElementsInstanced(mode, count, idx_type, indices, instance_count));
4386 checkGLcall("glDrawElementsInstanced");
4387 return;
4390 /* Instancing emulation by mixing immediate mode and arrays. */
4392 /* This is a nasty thing. MSDN says no hardware supports this and
4393 * applications have to use software vertex processing. We don't support
4394 * this for now.
4396 * Shouldn't be too hard to support with OpenGL, in theory just call
4397 * glDrawArrays() instead of drawElements(). But the stream frequency value
4398 * has a different meaning in that situation. */
4399 if (!idx_size)
4401 FIXME("Non-indexed instanced drawing is not supported.\n");
4402 return;
4405 for (i = 0; i < ARRAY_SIZE(si->elements); ++i)
4407 if (!(si->use_map & (1u << i)))
4408 continue;
4410 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
4411 instanced_elements[instanced_element_count++] = i;
4414 for (i = 0; i < instance_count; ++i)
4416 /* Specify the instanced attributes using immediate mode calls. */
4417 for (j = 0; j < instanced_element_count; ++j)
4419 const struct wined3d_stream_info_element *element;
4420 unsigned int element_idx;
4421 const BYTE *ptr;
4423 element_idx = instanced_elements[j];
4424 element = &si->elements[element_idx];
4425 ptr = element->data.addr + element->stride * i;
4426 if (element->data.buffer_object)
4427 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(state->streams[element->stream_idx].buffer,
4428 &context_gl->c);
4429 ops->generic[element->format->emit_idx](element_idx, ptr);
4432 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4434 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4435 checkGLcall("glDrawElementsBaseVertex");
4437 else
4439 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4440 checkGLcall("glDrawElements");
4445 static unsigned int get_stride_idx(const void *idx_data, unsigned int idx_size,
4446 unsigned int base_vertex_idx, unsigned int start_idx, unsigned int vertex_idx)
4448 if (!idx_data)
4449 return start_idx + vertex_idx;
4450 if (idx_size == 2)
4451 return ((const WORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4452 return ((const DWORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4455 /* Context activation is done by the caller. */
4456 static void draw_primitive_immediate_mode(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4457 const struct wined3d_stream_info *si, const void *idx_data, unsigned int idx_size,
4458 int base_vertex_idx, unsigned int start_idx, unsigned int vertex_count, unsigned int instance_count)
4460 const BYTE *position = NULL, *normal = NULL, *diffuse = NULL, *specular = NULL;
4461 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
4462 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4463 unsigned int coord_idx, stride_idx, texture_idx, vertex_idx;
4464 const struct wined3d_stream_info_element *element;
4465 const BYTE *tex_coords[WINED3DDP_MAXTEXCOORD];
4466 unsigned int texture_unit, texture_stages;
4467 const struct wined3d_ffp_attrib_ops *ops;
4468 unsigned int untracked_material_count;
4469 unsigned int tex_mask = 0;
4470 BOOL specular_fog = FALSE;
4471 BOOL ps = use_ps(state);
4472 const void *ptr;
4474 static unsigned int once;
4476 if (!once++)
4477 FIXME_(d3d_perf)("Drawing using immediate mode.\n");
4478 else
4479 WARN_(d3d_perf)("Drawing using immediate mode.\n");
4481 if (!idx_size && idx_data)
4482 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4484 if (instance_count)
4485 FIXME("Instancing not implemented.\n");
4487 /* Immediate mode drawing can't make use of indices in a VBO - get the
4488 * data from the index buffer. */
4489 if (idx_size)
4490 idx_data = wined3d_buffer_load_sysmem(state->index_buffer, &context_gl->c) + state->index_offset;
4492 ops = &d3d_info->ffp_attrib_ops;
4494 gl_info->gl_ops.gl.p_glBegin(gl_primitive_type_from_d3d(state->primitive_type));
4496 if (use_vs(state) || d3d_info->ffp_generic_attributes)
4498 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4500 unsigned int use_map = si->use_map;
4501 unsigned int element_idx;
4503 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4504 for (element_idx = MAX_ATTRIBS - 1; use_map; use_map &= ~(1u << element_idx), --element_idx)
4506 if (!(use_map & 1u << element_idx))
4507 continue;
4509 ptr = si->elements[element_idx].data.addr + si->elements[element_idx].stride * stride_idx;
4510 ops->generic[si->elements[element_idx].format->emit_idx](element_idx, ptr);
4514 gl_info->gl_ops.gl.p_glEnd();
4515 return;
4518 if (si->use_map & (1u << WINED3D_FFP_POSITION))
4519 position = si->elements[WINED3D_FFP_POSITION].data.addr;
4521 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
4522 normal = si->elements[WINED3D_FFP_NORMAL].data.addr;
4523 else
4524 gl_info->gl_ops.gl.p_glNormal3f(0.0f, 0.0f, 0.0f);
4526 untracked_material_count = context_gl->untracked_material_count;
4527 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
4529 element = &si->elements[WINED3D_FFP_DIFFUSE];
4530 diffuse = element->data.addr;
4532 if (untracked_material_count && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
4533 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element->format->id));
4535 else
4537 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4540 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
4542 element = &si->elements[WINED3D_FFP_SPECULAR];
4543 specular = element->data.addr;
4545 /* Special case where the fog density is stored in the specular alpha channel. */
4546 if (state->render_states[WINED3D_RS_FOGENABLE]
4547 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
4548 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
4549 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
4551 if (gl_info->supported[EXT_FOG_COORD])
4553 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
4554 specular_fog = TRUE;
4555 else
4556 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element->format->id));
4558 else
4560 static unsigned int once;
4562 if (!once++)
4563 FIXME("Implement fog for transformed vertices in software.\n");
4567 else if (gl_info->supported[EXT_SECONDARY_COLOR])
4569 GL_EXTCALL(glSecondaryColor3fEXT)(0.0f, 0.0f, 0.0f);
4572 texture_stages = d3d_info->limits.ffp_blend_stages;
4573 for (texture_idx = 0; texture_idx < texture_stages; ++texture_idx)
4575 if (!gl_info->supported[ARB_MULTITEXTURE] && texture_idx > 0)
4577 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4578 continue;
4581 if (!ps && !state->textures[texture_idx])
4582 continue;
4584 texture_unit = context_gl->tex_unit_map[texture_idx];
4585 if (texture_unit == WINED3D_UNMAPPED_STAGE)
4586 continue;
4588 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4589 if (coord_idx > 7)
4591 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx, texture_idx);
4592 continue;
4595 if (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))
4597 tex_coords[coord_idx] = si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].data.addr;
4598 tex_mask |= (1u << texture_idx);
4600 else
4602 TRACE("Setting default coordinates for texture %u.\n", texture_idx);
4603 if (gl_info->supported[ARB_MULTITEXTURE])
4604 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_unit, 0.0f, 0.0f, 0.0f, 1.0f));
4605 else
4606 gl_info->gl_ops.gl.p_glTexCoord4f(0.0f, 0.0f, 0.0f, 1.0f);
4610 /* Blending data and point sizes are not supported by this function. They
4611 * are not supported by the fixed function pipeline at all. A FIXME for
4612 * them is printed after decoding the vertex declaration. */
4613 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4615 unsigned int tmp_tex_mask;
4617 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4619 if (normal)
4621 ptr = normal + stride_idx * si->elements[WINED3D_FFP_NORMAL].stride;
4622 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptr);
4625 if (diffuse)
4627 ptr = diffuse + stride_idx * si->elements[WINED3D_FFP_DIFFUSE].stride;
4628 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptr);
4630 if (untracked_material_count)
4632 struct wined3d_color color;
4633 unsigned int i;
4635 wined3d_color_from_d3dcolor(&color, *(const DWORD *)ptr);
4636 for (i = 0; i < untracked_material_count; ++i)
4638 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK,
4639 context_gl->untracked_materials[i], &color.r);
4644 if (specular)
4646 ptr = specular + stride_idx * si->elements[WINED3D_FFP_SPECULAR].stride;
4647 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptr);
4649 if (specular_fog)
4650 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD *)ptr >> 24)));
4653 tmp_tex_mask = tex_mask;
4654 for (texture_idx = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture_idx)
4656 if (!(tmp_tex_mask & 1))
4657 continue;
4659 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4660 ptr = tex_coords[coord_idx] + (stride_idx * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
4661 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
4662 GL_TEXTURE0_ARB + context_gl->tex_unit_map[texture_idx], ptr);
4665 if (position)
4667 ptr = position + stride_idx * si->elements[WINED3D_FFP_POSITION].stride;
4668 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptr);
4672 gl_info->gl_ops.gl.p_glEnd();
4673 checkGLcall("draw immediate mode");
4676 static void wined3d_context_gl_draw_indirect(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4677 const struct wined3d_indirect_draw_parameters *parameters, unsigned int idx_size)
4679 GLenum gl_primitive_type = gl_primitive_type_from_d3d(state->primitive_type);
4680 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(parameters->buffer);
4681 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4682 const void *offset;
4684 if (!gl_info->supported[ARB_DRAW_INDIRECT])
4686 FIXME("OpenGL implementation does not support indirect draws.\n");
4687 return;
4690 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer_gl->bo.id));
4692 offset = (void *)(GLintptr)parameters->offset;
4693 if (idx_size)
4695 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4696 if (state->index_offset)
4697 FIXME("Ignoring index offset %u.\n", state->index_offset);
4698 GL_EXTCALL(glDrawElementsIndirect(gl_primitive_type, idx_type, offset));
4700 else
4702 GL_EXTCALL(glDrawArraysIndirect(gl_primitive_type, offset));
4705 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0));
4706 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
4708 checkGLcall("draw indirect");
4711 static void remove_vbos(struct wined3d_context *context,
4712 const struct wined3d_state *state, struct wined3d_stream_info *s)
4714 unsigned int i;
4716 for (i = 0; i < ARRAY_SIZE(s->elements); ++i)
4718 struct wined3d_stream_info_element *e;
4720 if (!(s->use_map & (1u << i)))
4721 continue;
4723 e = &s->elements[i];
4724 if (e->data.buffer_object)
4726 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
4727 e->data.buffer_object = 0;
4728 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(vb, context);
4733 static GLenum gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
4735 GLenum gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4736 switch (gl_primitive_type)
4738 case GL_POINTS:
4739 return GL_POINTS;
4741 case GL_LINE_STRIP:
4742 case GL_LINE_STRIP_ADJACENCY:
4743 case GL_LINES_ADJACENCY:
4744 case GL_LINES:
4745 return GL_LINES;
4747 case GL_TRIANGLE_FAN:
4748 case GL_TRIANGLE_STRIP:
4749 case GL_TRIANGLE_STRIP_ADJACENCY:
4750 case GL_TRIANGLES_ADJACENCY:
4751 case GL_TRIANGLES:
4752 return GL_TRIANGLES;
4754 default:
4755 return gl_primitive_type;
4759 /* Routine common to the draw primitive and draw indexed primitive routines */
4760 void draw_primitive(struct wined3d_device *device, const struct wined3d_state *state,
4761 const struct wined3d_draw_parameters *parameters)
4763 BOOL emulation = FALSE, rasterizer_discard = FALSE;
4764 const struct wined3d_fb_state *fb = &state->fb;
4765 const struct wined3d_stream_info *stream_info;
4766 struct wined3d_rendertarget_view *dsv, *rtv;
4767 struct wined3d_stream_info si_emulated;
4768 const struct wined3d_gl_info *gl_info;
4769 struct wined3d_context_gl *context_gl;
4770 struct wined3d_context *context;
4771 unsigned int i, idx_size = 0;
4772 const void *idx_data = NULL;
4774 TRACE("device %p, state %p, parameters %p.\n", device, state, parameters);
4776 if (!parameters->indirect && !parameters->u.direct.index_count)
4777 return;
4779 if (!parameters->indirect)
4780 TRACE("base_vertex_idx %d, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
4781 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
4782 parameters->u.direct.index_count, parameters->u.direct.start_instance,
4783 parameters->u.direct.instance_count);
4785 if (!(rtv = fb->render_targets[0]))
4786 rtv = fb->depth_stencil;
4788 if (rtv && rtv->resource->type == WINED3D_RTYPE_BUFFER)
4790 FIXME("Buffer render targets not implemented.\n");
4791 return;
4794 if (rtv)
4795 context = context_acquire(device, wined3d_texture_from_resource(rtv->resource), rtv->sub_resource_idx);
4796 else
4797 context = context_acquire(device, NULL, 0);
4798 context_gl = wined3d_context_gl(context);
4799 if (!context_gl->valid)
4801 context_release(context);
4802 WARN("Invalid context, skipping draw.\n");
4803 return;
4805 gl_info = context_gl->gl_info;
4807 if (!use_transform_feedback(state))
4808 wined3d_context_gl_pause_transform_feedback(context_gl, TRUE);
4810 for (i = 0; i < gl_info->limits.buffers; ++i)
4812 if (!(rtv = fb->render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
4813 continue;
4815 if (wined3d_blend_state_get_writemask(state->blend_state, i))
4817 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
4818 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
4820 else
4822 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
4826 if ((dsv = fb->depth_stencil))
4828 /* Note that this depends on the context_acquire() call above to set
4829 * context->render_offscreen properly. We don't currently take the
4830 * Z-compare function into account, but we could skip loading the
4831 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
4832 * that we never copy the stencil data.*/
4833 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4835 if (wined3d_state_uses_depth_buffer(state))
4836 wined3d_rendertarget_view_load_location(dsv, context, location);
4837 else
4838 wined3d_rendertarget_view_prepare_location(dsv, context, location);
4841 if (parameters->indirect)
4842 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
4844 if (!context_apply_draw_state(context, device, state, parameters->indexed))
4846 context_release(context);
4847 WARN("Unable to apply draw state, skipping draw.\n");
4848 return;
4851 if (dsv && (!state->depth_stencil_state || state->depth_stencil_state->desc.depth_write))
4853 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4855 wined3d_rendertarget_view_validate_location(dsv, location);
4856 wined3d_rendertarget_view_invalidate_location(dsv, ~location);
4859 stream_info = &context->stream_info;
4861 if (parameters->indexed)
4863 struct wined3d_buffer *index_buffer = state->index_buffer;
4864 if (!index_buffer->buffer_object || !stream_info->all_vbo)
4865 idx_data = index_buffer->resource.heap_memory;
4866 else
4867 idx_data = NULL;
4868 idx_data = (const BYTE *)idx_data + state->index_offset;
4870 if (state->index_format == WINED3DFMT_R16_UINT)
4871 idx_size = 2;
4872 else
4873 idx_size = 4;
4876 if (!use_vs(state))
4878 if (!stream_info->position_transformed && context_gl->untracked_material_count
4879 && state->render_states[WINED3D_RS_LIGHTING])
4881 static BOOL warned;
4883 if (!warned++)
4884 FIXME("Using software emulation because not all material properties could be tracked.\n");
4885 else
4886 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
4887 emulation = TRUE;
4889 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
4891 static BOOL warned;
4893 /* Either write a pipeline replacement shader or convert the
4894 * specular alpha from unsigned byte to a float in the vertex
4895 * buffer. */
4896 if (!warned++)
4897 FIXME("Using software emulation because manual fog coordinates are provided.\n");
4898 else
4899 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
4900 emulation = TRUE;
4903 if (emulation)
4905 si_emulated = context->stream_info;
4906 remove_vbos(context, state, &si_emulated);
4907 stream_info = &si_emulated;
4911 if (use_transform_feedback(state))
4913 const struct wined3d_shader *shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
4915 if (is_rasterization_disabled(shader))
4917 glEnable(GL_RASTERIZER_DISCARD);
4918 checkGLcall("enable rasterizer discard");
4919 rasterizer_discard = TRUE;
4922 if (context->transform_feedback_paused)
4924 GL_EXTCALL(glResumeTransformFeedback());
4925 checkGLcall("glResumeTransformFeedback");
4926 context->transform_feedback_paused = 0;
4928 else if (!context->transform_feedback_active)
4930 enum wined3d_primitive_type primitive_type = shader->u.gs.output_type
4931 ? shader->u.gs.output_type : state->primitive_type;
4932 GLenum mode = gl_tfb_primitive_type_from_d3d(primitive_type);
4933 GL_EXTCALL(glBeginTransformFeedback(mode));
4934 checkGLcall("glBeginTransformFeedback");
4935 context->transform_feedback_active = 1;
4939 if (state->primitive_type == WINED3D_PT_PATCH)
4941 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES, state->patch_vertex_count));
4942 checkGLcall("glPatchParameteri");
4945 if (context->uses_fbo_attached_resources)
4947 static unsigned int fixme_once;
4949 if (gl_info->supported[ARB_TEXTURE_BARRIER])
4951 GL_EXTCALL(glTextureBarrier());
4953 else if (gl_info->supported[NV_TEXTURE_BARRIER])
4955 GL_EXTCALL(glTextureBarrierNV());
4957 else
4959 if (!fixme_once++)
4960 FIXME("Sampling attached render targets is not supported.\n");
4962 WARN("Sampling attached render targets is not supported, skipping draw.\n");
4963 context_release(context);
4964 return;
4966 checkGLcall("glTextureBarrier");
4969 if (parameters->indirect)
4971 if (!context->use_immediate_mode_draw && !emulation)
4972 wined3d_context_gl_draw_indirect(context_gl, state, &parameters->u.indirect, idx_size);
4973 else
4974 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
4976 else
4978 unsigned int instance_count = parameters->u.direct.instance_count;
4979 if (context->instance_count)
4980 instance_count = context->instance_count;
4982 if (context->use_immediate_mode_draw || emulation)
4983 draw_primitive_immediate_mode(wined3d_context_gl(context), state, stream_info, idx_data,
4984 idx_size, parameters->u.direct.base_vertex_idx,
4985 parameters->u.direct.start_idx, parameters->u.direct.index_count, instance_count);
4986 else
4987 wined3d_context_gl_draw_primitive_arrays(context_gl, state, idx_data, idx_size,
4988 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
4989 parameters->u.direct.index_count, parameters->u.direct.start_instance, instance_count);
4992 if (context->uses_uavs)
4994 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4995 checkGLcall("glMemoryBarrier");
4998 wined3d_context_gl_pause_transform_feedback(context_gl, FALSE);
5000 if (rasterizer_discard)
5002 glDisable(GL_RASTERIZER_DISCARD);
5003 checkGLcall("disable rasterizer discard");
5006 context_release(context);
5008 TRACE("Draw completed.\n");
5011 void wined3d_context_gl_unload_tex_coords(const struct wined3d_context_gl *context_gl)
5013 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5014 unsigned int texture_idx;
5016 for (texture_idx = 0; texture_idx < gl_info->limits.texture_coords; ++texture_idx)
5018 gl_info->gl_ops.ext.p_glClientActiveTextureARB(GL_TEXTURE0_ARB + texture_idx);
5019 gl_info->gl_ops.gl.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY);
5023 void wined3d_context_gl_load_tex_coords(const struct wined3d_context_gl *context_gl,
5024 const struct wined3d_stream_info *si, GLuint *current_bo, const struct wined3d_state *state)
5026 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5027 const struct wined3d_format_gl *format_gl;
5028 unsigned int mapped_stage = 0;
5029 unsigned int texture_idx;
5030 GLuint bo;
5032 for (texture_idx = 0; texture_idx < context_gl->c.d3d_info->limits.ffp_blend_stages; ++texture_idx)
5034 unsigned int coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
5036 if ((mapped_stage = context_gl->tex_unit_map[texture_idx]) == WINED3D_UNMAPPED_STAGE)
5037 continue;
5039 if (mapped_stage >= gl_info->limits.texture_coords)
5041 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage);
5042 continue;
5045 if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
5047 const struct wined3d_stream_info_element *e = &si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx];
5049 TRACE("Setting up texture %u, idx %u, coord_idx %u, data %s.\n",
5050 texture_idx, mapped_stage, coord_idx, debug_bo_address(&e->data));
5052 bo = wined3d_bo_gl_id(e->data.buffer_object);
5053 if (*current_bo != bo)
5055 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5056 checkGLcall("glBindBuffer");
5057 *current_bo = bo;
5060 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
5061 checkGLcall("glClientActiveTextureARB");
5063 /* The coords to supply depend completely on the fvf/vertex shader. */
5064 format_gl = wined3d_format_gl(e->format);
5065 gl_info->gl_ops.gl.p_glTexCoordPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5066 e->data.addr + state->load_base_vertex_index * e->stride);
5067 gl_info->gl_ops.gl.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY);
5068 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5070 else
5072 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + mapped_stage, 0, 0, 0, 1));
5075 if (gl_info->supported[NV_REGISTER_COMBINERS])
5077 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
5078 for (texture_idx = mapped_stage + 1; texture_idx < gl_info->limits.textures; ++texture_idx)
5080 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
5084 checkGLcall("loadTexCoords");
5087 /* This should match any arrays loaded in wined3d_context_gl_load_vertex_data(). */
5088 static void wined3d_context_gl_unload_vertex_data(struct wined3d_context_gl *context_gl)
5090 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5092 if (!context_gl->c.namedArraysLoaded)
5093 return;
5094 gl_info->gl_ops.gl.p_glDisableClientState(GL_VERTEX_ARRAY);
5095 gl_info->gl_ops.gl.p_glDisableClientState(GL_NORMAL_ARRAY);
5096 gl_info->gl_ops.gl.p_glDisableClientState(GL_COLOR_ARRAY);
5097 if (gl_info->supported[EXT_SECONDARY_COLOR])
5098 gl_info->gl_ops.gl.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5099 wined3d_context_gl_unload_tex_coords(context_gl);
5100 context_gl->c.namedArraysLoaded = FALSE;
5103 static void wined3d_context_gl_load_vertex_data(struct wined3d_context_gl *context_gl,
5104 const struct wined3d_stream_info *si, const struct wined3d_state *state)
5106 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5107 const struct wined3d_stream_info_element *e;
5108 const struct wined3d_format_gl *format_gl;
5109 GLuint current_bo, bo;
5111 TRACE("context_gl %p, si %p, state %p.\n", context_gl, si, state);
5113 /* This is used for the fixed-function pipeline only, and the
5114 * fixed-function pipeline doesn't do instancing. */
5115 context_gl->c.instance_count = 0;
5116 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5118 /* Blend data */
5119 if ((si->use_map & (1u << WINED3D_FFP_BLENDWEIGHT))
5120 || si->use_map & (1u << WINED3D_FFP_BLENDINDICES))
5122 /* TODO: Support vertex blending in immediate mode draws. No need to
5123 * write a FIXME here, this is done after the general vertex
5124 * declaration decoding. */
5125 WARN("Vertex blending not supported.\n");
5128 /* Point Size */
5129 if (si->use_map & (1u << WINED3D_FFP_PSIZE))
5131 /* No such functionality in the fixed-function GL pipeline. */
5132 WARN("Per-vertex point size not supported.\n");
5135 /* Position */
5136 if (si->use_map & (1u << WINED3D_FFP_POSITION))
5138 e = &si->elements[WINED3D_FFP_POSITION];
5139 format_gl = wined3d_format_gl(e->format);
5141 bo = wined3d_bo_gl_id(e->data.buffer_object);
5142 if (current_bo != bo)
5144 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5145 checkGLcall("glBindBuffer");
5146 current_bo = bo;
5149 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n",
5150 format_gl->vtx_format, format_gl->vtx_type, e->stride,
5151 e->data.addr + state->load_base_vertex_index * e->stride);
5152 gl_info->gl_ops.gl.p_glVertexPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5153 e->data.addr + state->load_base_vertex_index * e->stride);
5154 checkGLcall("glVertexPointer(...)");
5155 gl_info->gl_ops.gl.p_glEnableClientState(GL_VERTEX_ARRAY);
5156 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5157 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5160 /* Normals */
5161 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
5163 e = &si->elements[WINED3D_FFP_NORMAL];
5164 format_gl = wined3d_format_gl(e->format);
5166 bo = wined3d_bo_gl_id(e->data.buffer_object);
5167 if (current_bo != bo)
5169 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5170 checkGLcall("glBindBuffer");
5171 current_bo = bo;
5174 TRACE("glNormalPointer(%#x, %#x, %p);\n", format_gl->vtx_type, e->stride,
5175 e->data.addr + state->load_base_vertex_index * e->stride);
5176 gl_info->gl_ops.gl.p_glNormalPointer(format_gl->vtx_type, e->stride,
5177 e->data.addr + state->load_base_vertex_index * e->stride);
5178 checkGLcall("glNormalPointer(...)");
5179 gl_info->gl_ops.gl.p_glEnableClientState(GL_NORMAL_ARRAY);
5180 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5181 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5183 else
5185 gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
5186 checkGLcall("glNormal3f(0, 0, 0)");
5189 /* Diffuse colour */
5190 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
5192 e = &si->elements[WINED3D_FFP_DIFFUSE];
5193 format_gl = wined3d_format_gl(e->format);
5195 bo = wined3d_bo_gl_id(e->data.buffer_object);
5196 if (current_bo != bo)
5198 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5199 checkGLcall("glBindBuffer");
5200 current_bo = bo;
5203 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5204 format_gl->vtx_format, format_gl->vtx_type, e->stride,
5205 e->data.addr + state->load_base_vertex_index * e->stride);
5206 gl_info->gl_ops.gl.p_glColorPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5207 e->data.addr + state->load_base_vertex_index * e->stride);
5208 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5209 gl_info->gl_ops.gl.p_glEnableClientState(GL_COLOR_ARRAY);
5210 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5211 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5213 else
5215 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
5216 checkGLcall("glColor4f(1, 1, 1, 1)");
5219 /* Specular colour */
5220 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
5222 TRACE("Setting specular colour.\n");
5224 e = &si->elements[WINED3D_FFP_SPECULAR];
5226 if (gl_info->supported[EXT_SECONDARY_COLOR])
5228 GLint format;
5229 GLenum type;
5231 format_gl = wined3d_format_gl(e->format);
5232 type = format_gl->vtx_type;
5233 format = format_gl->vtx_format;
5235 bo = wined3d_bo_gl_id(e->data.buffer_object);
5236 if (current_bo != bo)
5238 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5239 checkGLcall("glBindBuffer");
5240 current_bo = bo;
5243 if (format != 4 || (gl_info->quirks & WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA))
5245 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5246 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5247 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5248 * 4 component secondary colors use it
5250 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format, type, e->stride,
5251 e->data.addr + state->load_base_vertex_index * e->stride);
5252 GL_EXTCALL(glSecondaryColorPointerEXT(format, type, e->stride,
5253 e->data.addr + state->load_base_vertex_index * e->stride));
5254 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5256 else
5258 switch (type)
5260 case GL_UNSIGNED_BYTE:
5261 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e->stride,
5262 e->data.addr + state->load_base_vertex_index * e->stride);
5263 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, e->stride,
5264 e->data.addr + state->load_base_vertex_index * e->stride));
5265 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5266 break;
5268 default:
5269 FIXME("Add 4 component specular colour pointers for type %#x.\n", type);
5270 /* Make sure that the right colour component is dropped. */
5271 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type, e->stride,
5272 e->data.addr + state->load_base_vertex_index * e->stride);
5273 GL_EXTCALL(glSecondaryColorPointerEXT(3, type, e->stride,
5274 e->data.addr + state->load_base_vertex_index * e->stride));
5275 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5278 gl_info->gl_ops.gl.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5279 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5280 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5282 else
5284 WARN("Specular colour is not supported in this GL implementation.\n");
5287 else
5289 if (gl_info->supported[EXT_SECONDARY_COLOR])
5291 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
5292 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5294 else
5296 WARN("Specular colour is not supported in this GL implementation.\n");
5300 /* Texture coordinates */
5301 wined3d_context_gl_load_tex_coords(context_gl, si, &current_bo, state);
5304 static void wined3d_context_gl_unload_numbered_array(struct wined3d_context_gl *context_gl, unsigned int i)
5306 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5308 GL_EXTCALL(glDisableVertexAttribArray(i));
5309 checkGLcall("glDisableVertexAttribArray");
5310 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5311 GL_EXTCALL(glVertexAttribDivisor(i, 0));
5313 context_gl->c.numbered_array_mask &= ~(1u << i);
5316 static void wined3d_context_gl_unload_numbered_arrays(struct wined3d_context_gl *context_gl)
5318 uint32_t mask = context_gl->c.numbered_array_mask;
5319 unsigned int i;
5321 while (mask)
5323 i = wined3d_bit_scan(&mask);
5324 wined3d_context_gl_unload_numbered_array(context_gl, i);
5328 static void wined3d_context_gl_load_numbered_arrays(struct wined3d_context_gl *context_gl,
5329 const struct wined3d_stream_info *stream_info, const struct wined3d_state *state)
5331 struct wined3d_context *context = &context_gl->c;
5332 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5333 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5334 GLuint current_bo, bo;
5335 unsigned int i;
5337 /* Default to no instancing. */
5338 context->instance_count = 0;
5339 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5341 for (i = 0; i < MAX_ATTRIBS; ++i)
5343 const struct wined3d_stream_info_element *element = &stream_info->elements[i];
5344 const struct wined3d_stream_state *stream;
5345 const struct wined3d_format_gl *format_gl;
5347 if (!(stream_info->use_map & (1u << i)))
5349 if (context->numbered_array_mask & (1u << i))
5350 wined3d_context_gl_unload_numbered_array(context_gl, i);
5351 if (!use_vs(state) && i == WINED3D_FFP_DIFFUSE)
5353 if (!(context_gl->default_attrib_value_set & (1u << i)) || !context_gl->diffuse_attrib_to_1)
5355 GL_EXTCALL(glVertexAttrib4f(i, 1.0f, 1.0f, 1.0f, 1.0f));
5356 context_gl->diffuse_attrib_to_1 = 1;
5359 else
5361 if (!(context_gl->default_attrib_value_set & (1u << i)))
5363 GL_EXTCALL(glVertexAttrib4f(i, 0.0f, 0.0f, 0.0f, 0.0f));
5364 if (i == WINED3D_FFP_DIFFUSE)
5365 context_gl->diffuse_attrib_to_1 = 0;
5368 context_gl->default_attrib_value_set |= 1u << i;
5369 continue;
5372 format_gl = wined3d_format_gl(element->format);
5373 stream = &state->streams[element->stream_idx];
5374 wined3d_buffer_gl(stream->buffer)->bo_user.valid = true;
5376 if ((stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA) && !context->instance_count)
5377 context->instance_count = state->streams[0].frequency;
5379 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5381 GL_EXTCALL(glVertexAttribDivisor(i, element->divisor));
5383 else if (element->divisor)
5385 /* Unload instanced arrays, they will be loaded using immediate
5386 * mode instead. */
5387 if (context->numbered_array_mask & (1u << i))
5388 wined3d_context_gl_unload_numbered_array(context_gl, i);
5389 context_gl->default_attrib_value_set &= ~(1u << i);
5390 continue;
5393 TRACE("Loading array %u %s.\n", i, debug_bo_address(&element->data));
5395 if (element->stride)
5397 DWORD format_flags = format_gl->f.flags[WINED3D_GL_RES_TYPE_BUFFER];
5399 bo = wined3d_bo_gl_id(element->data.buffer_object);
5400 if (current_bo != bo)
5402 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5403 checkGLcall("glBindBuffer");
5404 current_bo = bo;
5406 /* Use the VBO to find out if a vertex buffer exists, not the vb
5407 * pointer. vb can point to a user pointer data blob. In that case
5408 * current_bo will be 0. If there is a vertex buffer but no vbo we
5409 * won't be load converted attributes anyway. */
5410 if (vs && vs->reg_maps.shader_version.major >= 4 && (format_flags & WINED3DFMT_FLAG_INTEGER))
5412 GL_EXTCALL(glVertexAttribIPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5413 element->stride, element->data.addr + state->load_base_vertex_index * element->stride));
5415 else
5417 GL_EXTCALL(glVertexAttribPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5418 !!(format_flags & WINED3DFMT_FLAG_NORMALISED), element->stride,
5419 element->data.addr + state->load_base_vertex_index * element->stride));
5422 if (!(context->numbered_array_mask & (1u << i)))
5424 GL_EXTCALL(glEnableVertexAttribArray(i));
5425 context->numbered_array_mask |= (1u << i);
5428 else
5430 /* Stride = 0 means always the same values.
5431 * glVertexAttribPointer() doesn't do that. Instead disable the
5432 * pointer and set up the attribute statically. But we have to
5433 * figure out the system memory address. */
5434 const BYTE *ptr = element->data.addr;
5435 if (element->data.buffer_object)
5436 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(stream->buffer, context);
5438 if (context->numbered_array_mask & (1u << i))
5439 wined3d_context_gl_unload_numbered_array(context_gl, i);
5441 switch (format_gl->f.id)
5443 case WINED3DFMT_R32_FLOAT:
5444 GL_EXTCALL(glVertexAttrib1fv(i, (const GLfloat *)ptr));
5445 break;
5446 case WINED3DFMT_R32G32_FLOAT:
5447 GL_EXTCALL(glVertexAttrib2fv(i, (const GLfloat *)ptr));
5448 break;
5449 case WINED3DFMT_R32G32B32_FLOAT:
5450 GL_EXTCALL(glVertexAttrib3fv(i, (const GLfloat *)ptr));
5451 break;
5452 case WINED3DFMT_R32G32B32A32_FLOAT:
5453 GL_EXTCALL(glVertexAttrib4fv(i, (const GLfloat *)ptr));
5454 break;
5455 case WINED3DFMT_R8G8B8A8_UINT:
5456 GL_EXTCALL(glVertexAttrib4ubv(i, ptr));
5457 break;
5458 case WINED3DFMT_B8G8R8A8_UNORM:
5459 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
5461 const DWORD *src = (const DWORD *)ptr;
5462 DWORD c = *src & 0xff00ff00u;
5463 c |= (*src & 0xff0000u) >> 16;
5464 c |= (*src & 0xffu) << 16;
5465 GL_EXTCALL(glVertexAttrib4Nubv(i, (GLubyte *)&c));
5466 break;
5468 /* else fallthrough */
5469 case WINED3DFMT_R8G8B8A8_UNORM:
5470 GL_EXTCALL(glVertexAttrib4Nubv(i, ptr));
5471 break;
5472 case WINED3DFMT_R16G16_SINT:
5473 GL_EXTCALL(glVertexAttrib2sv(i, (const GLshort *)ptr));
5474 break;
5475 case WINED3DFMT_R16G16B16A16_SINT:
5476 GL_EXTCALL(glVertexAttrib4sv(i, (const GLshort *)ptr));
5477 break;
5478 case WINED3DFMT_R16G16_SNORM:
5480 const GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
5481 GL_EXTCALL(glVertexAttrib4Nsv(i, s));
5482 break;
5484 case WINED3DFMT_R16G16_UNORM:
5486 const GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
5487 GL_EXTCALL(glVertexAttrib4Nusv(i, s));
5488 break;
5490 case WINED3DFMT_R16G16B16A16_SNORM:
5491 GL_EXTCALL(glVertexAttrib4Nsv(i, (const GLshort *)ptr));
5492 break;
5493 case WINED3DFMT_R16G16B16A16_UNORM:
5494 GL_EXTCALL(glVertexAttrib4Nusv(i, (const GLushort *)ptr));
5495 break;
5496 case WINED3DFMT_R10G10B10X2_UINT:
5497 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5498 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5499 break;
5500 case WINED3DFMT_R10G10B10X2_SNORM:
5501 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5502 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5503 break;
5504 case WINED3DFMT_R16G16_FLOAT:
5505 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5507 /* Not supported by GL_ARB_half_float_vertex. */
5508 GL_EXTCALL(glVertexAttrib2hvNV(i, (const GLhalfNV *)ptr));
5510 else
5512 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5513 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5514 GL_EXTCALL(glVertexAttrib2f(i, x, y));
5516 break;
5517 case WINED3DFMT_R16G16B16A16_FLOAT:
5518 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5520 /* Not supported by GL_ARB_half_float_vertex. */
5521 GL_EXTCALL(glVertexAttrib4hvNV(i, (const GLhalfNV *)ptr));
5523 else
5525 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5526 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5527 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
5528 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
5529 GL_EXTCALL(glVertexAttrib4f(i, x, y, z, w));
5531 break;
5532 default:
5533 ERR("Unexpected declaration in stride 0 attributes.\n");
5534 break;
5537 context_gl->default_attrib_value_set &= ~(1u << i);
5540 checkGLcall("Loading numbered arrays");
5543 void wined3d_context_gl_update_stream_sources(struct wined3d_context_gl *context_gl,
5544 const struct wined3d_state *state)
5546 if (context_gl->c.use_immediate_mode_draw)
5547 return;
5549 wined3d_context_gl_unload_vertex_data(context_gl);
5550 if (context_gl->c.d3d_info->ffp_generic_attributes || use_vs(state))
5552 TRACE("Loading numbered arrays.\n");
5553 wined3d_context_gl_load_numbered_arrays(context_gl, &context_gl->c.stream_info, state);
5554 return;
5557 TRACE("Loading named arrays.\n");
5558 wined3d_context_gl_unload_numbered_arrays(context_gl);
5559 wined3d_context_gl_load_vertex_data(context_gl, &context_gl->c.stream_info, state);
5560 context_gl->c.namedArraysLoaded = TRUE;
5563 static void apply_texture_blit_state(const struct wined3d_gl_info *gl_info, struct gl_texture *texture,
5564 GLenum target, unsigned int level, enum wined3d_texture_filter_type filter)
5566 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
5567 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
5568 wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
5569 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
5570 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
5571 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
5572 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
5573 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, level);
5575 /* We changed the filtering settings on the texture. Make sure they get
5576 * reset on subsequent draws. */
5577 texture->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
5578 texture->sampler_desc.min_filter = WINED3D_TEXF_POINT;
5579 texture->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
5580 texture->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
5581 texture->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
5582 texture->sampler_desc.srgb_decode = FALSE;
5583 texture->base_level = level;
5586 /* Context activation is done by the caller. */
5587 void wined3d_context_gl_draw_shaded_quad(struct wined3d_context_gl *context_gl, struct wined3d_texture_gl *texture_gl,
5588 unsigned int sub_resource_idx, const RECT *src_rect, const RECT *dst_rect,
5589 enum wined3d_texture_filter_type filter)
5591 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5592 struct wined3d_blt_info info;
5593 unsigned int level, w, h, i;
5594 SIZE dst_size;
5595 struct blit_vertex
5597 float x, y;
5598 struct wined3d_vec3 texcoord;
5600 quad[4];
5602 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5604 level = sub_resource_idx % texture_gl->t.level_count;
5605 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5606 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5607 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5609 wined3d_context_gl_get_rt_size(context_gl, &dst_size);
5610 w = dst_size.cx;
5611 h = dst_size.cy;
5613 quad[0].x = dst_rect->left * 2.0f / w - 1.0f;
5614 quad[0].y = dst_rect->top * 2.0f / h - 1.0f;
5615 quad[0].texcoord = info.texcoords[0];
5617 quad[1].x = dst_rect->right * 2.0f / w - 1.0f;
5618 quad[1].y = dst_rect->top * 2.0f / h - 1.0f;
5619 quad[1].texcoord = info.texcoords[1];
5621 quad[2].x = dst_rect->left * 2.0f / w - 1.0f;
5622 quad[2].y = dst_rect->bottom * 2.0f / h - 1.0f;
5623 quad[2].texcoord = info.texcoords[2];
5625 quad[3].x = dst_rect->right * 2.0f / w - 1.0f;
5626 quad[3].y = dst_rect->bottom * 2.0f / h - 1.0f;
5627 quad[3].texcoord = info.texcoords[3];
5629 /* Draw a quad. */
5630 if (gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
5632 if (!context_gl->blit_vbo)
5633 GL_EXTCALL(glGenBuffers(1, &context_gl->blit_vbo));
5634 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, context_gl->blit_vbo));
5636 wined3d_context_gl_unload_vertex_data(context_gl);
5637 wined3d_context_gl_unload_numbered_arrays(context_gl);
5639 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STREAM_DRAW));
5640 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT, FALSE, sizeof(*quad), NULL));
5641 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT, FALSE, sizeof(*quad),
5642 (void *)FIELD_OFFSET(struct blit_vertex, texcoord)));
5644 GL_EXTCALL(glEnableVertexAttribArray(0));
5645 GL_EXTCALL(glEnableVertexAttribArray(1));
5647 gl_info->gl_ops.gl.p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
5649 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
5650 GL_EXTCALL(glDisableVertexAttribArray(1));
5651 GL_EXTCALL(glDisableVertexAttribArray(0));
5653 else
5655 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5657 for (i = 0; i < ARRAY_SIZE(quad); ++i)
5659 GL_EXTCALL(glVertexAttrib3fv(1, &quad[i].texcoord.x));
5660 GL_EXTCALL(glVertexAttrib2fv(0, &quad[i].x));
5663 gl_info->gl_ops.gl.p_glEnd();
5665 checkGLcall("draw");
5667 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5668 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);
5671 /* Context activation is done by the caller. */
5672 void wined3d_context_gl_draw_textured_quad(struct wined3d_context_gl *context_gl,
5673 struct wined3d_texture_gl *texture_gl, unsigned int sub_resource_idx,
5674 const RECT *src_rect, const RECT *dst_rect, enum wined3d_texture_filter_type filter)
5676 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5677 struct wined3d_blt_info info;
5678 unsigned int level;
5680 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5682 gl_info->gl_ops.gl.p_glEnable(info.bind_target);
5683 checkGLcall("glEnable(bind_target)");
5685 level = sub_resource_idx % texture_gl->t.level_count;
5686 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5687 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5688 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5689 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
5690 checkGLcall("glTexEnvi");
5692 /* Draw a quad. */
5693 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5694 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[0].x);
5695 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->top);
5697 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[1].x);
5698 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->top);
5700 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[2].x);
5701 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->bottom);
5703 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[3].x);
5704 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->bottom);
5705 gl_info->gl_ops.gl.p_glEnd();
5707 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5708 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);