ws2_32: Add support for hints in GetAddrInfoExW.
[wine.git] / dlls / wined3d / context_gl.c
blob43c086ac75822c55ad29dd345f733cebc271a278
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;
1189 HWND win;
1191 if (private && context_gl->dc_has_format)
1192 return TRUE;
1194 if (!private && WindowFromDC(dc) != context_gl->window)
1195 return FALSE;
1197 current = gl_info->gl_ops.wgl.p_wglGetPixelFormat(dc);
1198 if (current == format) goto success;
1200 /* By default WGL doesn't allow pixel format adjustments but we need it
1201 * here. For this reason there's a Wine specific wglSetPixelFormat()
1202 * which allows us to set the pixel format multiple times. Use it when we
1203 * can, because even though no pixel format may currently be set, the
1204 * application may try to set one later. */
1205 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1207 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
1209 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1210 format, dc);
1211 return FALSE;
1214 else if (current)
1216 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1217 * continue using the old format. There's a big chance that the old
1218 * format works although with a performance hit and perhaps rendering
1219 * errors. */
1220 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1221 format, dc, current);
1222 return TRUE;
1224 else if (!SetPixelFormat(dc, format, NULL))
1226 /* This may also happen if the dc belongs to a destroyed window. */
1227 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1228 format, dc, GetLastError());
1229 return FALSE;
1232 win = private ? NULL : WindowFromDC(dc);
1233 if (win != context_gl->restore_pf_win)
1234 wined3d_context_gl_restore_pixel_format(context_gl);
1235 context_gl->restore_pf = private ? 0 : current;
1236 context_gl->restore_pf_win = win;
1238 success:
1239 if (private)
1240 context_gl->dc_has_format = TRUE;
1241 return TRUE;
1244 static BOOL wined3d_context_gl_set_gl_context(struct wined3d_context_gl *context_gl)
1246 struct wined3d_swapchain_gl *swapchain_gl = wined3d_swapchain_gl(context_gl->c.swapchain);
1247 BOOL backup = FALSE;
1249 if (!wined3d_context_gl_set_pixel_format(context_gl))
1251 WARN("Failed to set pixel format %d on device context %p.\n",
1252 context_gl->pixel_format, context_gl->dc);
1253 backup = TRUE;
1256 if (backup || !wglMakeCurrent(context_gl->dc, context_gl->gl_ctx))
1258 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1259 context_gl->gl_ctx, context_gl->dc, GetLastError());
1260 context_gl->valid = 0;
1261 WARN("Trying fallback to the backup window.\n");
1263 /* FIXME: If the context is destroyed it's no longer associated with
1264 * a swapchain, so we can't use the swapchain to get a backup dc. To
1265 * make this work windowless contexts would need to be handled by the
1266 * device. */
1267 if (context_gl->c.destroyed || !swapchain_gl)
1269 FIXME("Unable to get backup dc for destroyed context %p.\n", context_gl);
1270 wined3d_context_gl_set_current(NULL);
1271 return FALSE;
1274 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
1276 wined3d_context_gl_set_current(NULL);
1277 return FALSE;
1280 context_gl->dc_is_private = TRUE;
1281 context_gl->dc_has_format = FALSE;
1283 if (!wined3d_context_gl_set_pixel_format(context_gl))
1285 ERR("Failed to set pixel format %d on device context %p.\n",
1286 context_gl->pixel_format, context_gl->dc);
1287 wined3d_context_gl_set_current(NULL);
1288 return FALSE;
1291 if (!wglMakeCurrent(context_gl->dc, context_gl->gl_ctx))
1293 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1294 context_gl->dc, GetLastError());
1295 wined3d_context_gl_set_current(NULL);
1296 return FALSE;
1299 context_gl->valid = 1;
1301 context_gl->needs_set = 0;
1303 return TRUE;
1306 static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx)
1308 if (!wglMakeCurrent(dc, gl_ctx))
1310 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1311 gl_ctx, dc, GetLastError());
1312 wined3d_context_gl_set_current(NULL);
1316 static void wined3d_context_gl_update_window(struct wined3d_context_gl *context_gl)
1318 if (!context_gl->c.swapchain)
1319 return;
1321 if (context_gl->window == context_gl->c.swapchain->win_handle)
1322 return;
1324 TRACE("Updating context %p window from %p to %p.\n",
1325 context_gl, context_gl->window, context_gl->c.swapchain->win_handle);
1327 if (context_gl->dc)
1328 wined3d_release_dc(context_gl->window, context_gl->dc);
1330 context_gl->window = context_gl->c.swapchain->win_handle;
1331 context_gl->dc_is_private = FALSE;
1332 context_gl->dc_has_format = FALSE;
1333 context_gl->needs_set = 1;
1334 context_gl->valid = 1;
1336 if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
1338 ERR("Failed to get a device context for window %p.\n", context_gl->window);
1339 context_gl->valid = 0;
1343 static void wined3d_context_gl_cleanup(struct wined3d_context_gl *context_gl)
1345 struct wined3d_pipeline_statistics_query *pipeline_statistics_query;
1346 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1347 struct wined3d_so_statistics_query *so_statistics_query;
1348 struct wined3d_timestamp_query *timestamp_query;
1349 struct wined3d_occlusion_query *occlusion_query;
1350 struct fbo_entry *entry, *entry2;
1351 struct wined3d_fence *fence;
1352 HGLRC restore_ctx;
1353 HDC restore_dc;
1354 unsigned int i;
1356 restore_ctx = wglGetCurrentContext();
1357 restore_dc = wglGetCurrentDC();
1359 if (restore_ctx == context_gl->gl_ctx)
1360 restore_ctx = NULL;
1361 else if (context_gl->valid)
1362 wined3d_context_gl_set_gl_context(context_gl);
1364 if (context_gl->valid)
1366 /* If we're here because we're switching away from a previously
1367 * destroyed context, acquiring a context in order to submit a fence
1368 * is problematic. (In particular, we'd end up back here again in the
1369 * process of switching to the newly acquired context.) */
1370 if (context_gl->c.destroyed)
1372 gl_info->gl_ops.gl.p_glFinish();
1374 else
1376 wined3d_context_gl_submit_command_fence(context_gl);
1377 wined3d_context_gl_wait_command_fence(context_gl,
1378 wined3d_device_gl(context_gl->c.device)->current_fence_id - 1);
1381 if (context_gl->dummy_arbfp_prog)
1382 GL_EXTCALL(glDeleteProgramsARB(1, &context_gl->dummy_arbfp_prog));
1384 if (context_gl->blit_vbo)
1385 GL_EXTCALL(glDeleteBuffers(1, &context_gl->blit_vbo));
1387 for (i = 0; i < context_gl->free_pipeline_statistics_query_count; ++i)
1389 union wined3d_gl_pipeline_statistics_query *q = &context_gl->free_pipeline_statistics_queries[i];
1390 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1393 for (i = 0; i < context_gl->free_so_statistics_query_count; ++i)
1395 union wined3d_gl_so_statistics_query *q = &context_gl->free_so_statistics_queries[i];
1396 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1399 if (context_gl->free_timestamp_query_count)
1400 GL_EXTCALL(glDeleteQueries(context_gl->free_timestamp_query_count, context_gl->free_timestamp_queries));
1402 if (gl_info->supported[ARB_SYNC])
1404 for (i = 0; i < context_gl->free_fence_count; ++i)
1406 GL_EXTCALL(glDeleteSync(context_gl->free_fences[i].sync));
1409 else if (gl_info->supported[APPLE_FENCE])
1411 for (i = 0; i < context_gl->free_fence_count; ++i)
1413 GL_EXTCALL(glDeleteFencesAPPLE(1, &context_gl->free_fences[i].id));
1416 else if (gl_info->supported[NV_FENCE])
1418 for (i = 0; i < context_gl->free_fence_count; ++i)
1420 GL_EXTCALL(glDeleteFencesNV(1, &context_gl->free_fences[i].id));
1424 if (context_gl->free_occlusion_query_count)
1425 GL_EXTCALL(glDeleteQueries(context_gl->free_occlusion_query_count, context_gl->free_occlusion_queries));
1427 checkGLcall("context cleanup");
1429 heap_free(context_gl->submitted.fences);
1430 heap_free(context_gl->free_pipeline_statistics_queries);
1431 heap_free(context_gl->free_so_statistics_queries);
1432 heap_free(context_gl->free_timestamp_queries);
1433 heap_free(context_gl->free_fences);
1434 heap_free(context_gl->free_occlusion_queries);
1436 LIST_FOR_EACH_ENTRY(pipeline_statistics_query, &context_gl->pipeline_statistics_queries,
1437 struct wined3d_pipeline_statistics_query, entry)
1439 if (context_gl->valid)
1440 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query->u.id), pipeline_statistics_query->u.id));
1441 pipeline_statistics_query->context_gl = NULL;
1444 LIST_FOR_EACH_ENTRY(so_statistics_query, &context_gl->so_statistics_queries,
1445 struct wined3d_so_statistics_query, entry)
1447 if (context_gl->valid)
1448 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query->u.id), so_statistics_query->u.id));
1449 so_statistics_query->context_gl = NULL;
1452 LIST_FOR_EACH_ENTRY(timestamp_query, &context_gl->timestamp_queries, struct wined3d_timestamp_query, entry)
1454 if (context_gl->valid)
1455 GL_EXTCALL(glDeleteQueries(1, &timestamp_query->id));
1456 timestamp_query->context_gl = NULL;
1459 LIST_FOR_EACH_ENTRY(fence, &context_gl->fences, struct wined3d_fence, entry)
1461 if (context_gl->valid)
1463 if (gl_info->supported[ARB_SYNC])
1465 if (fence->object.sync)
1466 GL_EXTCALL(glDeleteSync(fence->object.sync));
1468 else if (gl_info->supported[APPLE_FENCE])
1470 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence->object.id));
1472 else if (gl_info->supported[NV_FENCE])
1474 GL_EXTCALL(glDeleteFencesNV(1, &fence->object.id));
1477 fence->context_gl = NULL;
1480 LIST_FOR_EACH_ENTRY(occlusion_query, &context_gl->occlusion_queries, struct wined3d_occlusion_query, entry)
1482 if (context_gl->valid)
1483 GL_EXTCALL(glDeleteQueries(1, &occlusion_query->id));
1484 occlusion_query->context_gl = NULL;
1487 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_destroy_list, struct fbo_entry, entry)
1489 if (!context_gl->valid)
1490 entry->id = 0;
1491 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1494 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_list, struct fbo_entry, entry)
1496 if (!context_gl->valid)
1497 entry->id = 0;
1498 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1501 heap_free(context_gl->texture_type);
1503 wined3d_context_gl_restore_pixel_format(context_gl);
1504 if (restore_ctx)
1505 context_restore_gl_context(gl_info, restore_dc, restore_ctx);
1506 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1507 ERR("Failed to disable GL context.\n");
1509 wined3d_release_dc(context_gl->window, context_gl->dc);
1511 if (!wglDeleteContext(context_gl->gl_ctx))
1513 DWORD err = GetLastError();
1514 ERR("Failed to delete GL context %p, last error %#x.\n", context_gl->gl_ctx, err);
1517 wined3d_context_cleanup(&context_gl->c);
1520 DWORD context_get_tls_idx(void)
1522 return wined3d_context_tls_idx;
1525 void context_set_tls_idx(DWORD idx)
1527 wined3d_context_tls_idx = idx;
1530 struct wined3d_context_gl *wined3d_context_gl_get_current(void)
1532 return TlsGetValue(wined3d_context_tls_idx);
1535 BOOL wined3d_context_gl_set_current(struct wined3d_context_gl *context_gl)
1537 struct wined3d_context_gl *old = wined3d_context_gl_get_current();
1539 if (old == context_gl)
1541 TRACE("Already using D3D context %p.\n", context_gl);
1542 return TRUE;
1545 if (old)
1547 if (old->c.destroyed)
1549 TRACE("Switching away from destroyed context %p.\n", old);
1550 wined3d_context_gl_cleanup(old);
1551 heap_free((void *)old->gl_info);
1552 heap_free(old);
1554 else
1556 if (wglGetCurrentContext())
1558 const struct wined3d_gl_info *gl_info = old->gl_info;
1559 TRACE("Flushing context %p before switching to %p.\n", old, context_gl);
1560 gl_info->gl_ops.gl.p_glFlush();
1562 old->c.current = 0;
1566 if (context_gl)
1568 if (!context_gl->valid)
1570 ERR("Trying to make invalid context %p current.\n", context_gl);
1571 return FALSE;
1574 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n",
1575 context_gl, context_gl->gl_ctx, context_gl->dc);
1576 if (!wined3d_context_gl_set_gl_context(context_gl))
1577 return FALSE;
1578 context_gl->c.current = 1;
1580 else if (wglGetCurrentContext())
1582 TRACE("Clearing current D3D context.\n");
1583 if (!wglMakeCurrent(NULL, NULL))
1585 DWORD err = GetLastError();
1586 ERR("Failed to clear current GL context, last error %#x.\n", err);
1587 TlsSetValue(wined3d_context_tls_idx, NULL);
1588 return FALSE;
1592 return TlsSetValue(wined3d_context_tls_idx, context_gl);
1595 void wined3d_context_gl_release(struct wined3d_context_gl *context_gl)
1597 TRACE("Releasing context %p, level %u.\n", context_gl, context_gl->level);
1599 if (WARN_ON(d3d))
1601 if (!context_gl->level)
1602 WARN("Context %p is not active.\n", context_gl);
1603 else if (context_gl != wined3d_context_gl_get_current())
1604 WARN("Context %p is not the current context.\n", context_gl);
1607 if (!--context_gl->level)
1609 if (wined3d_context_gl_restore_pixel_format(context_gl))
1610 context_gl->needs_set = 1;
1611 if (context_gl->restore_ctx)
1613 TRACE("Restoring GL context %p on device context %p.\n", context_gl->restore_ctx, context_gl->restore_dc);
1614 context_restore_gl_context(context_gl->gl_info, context_gl->restore_dc, context_gl->restore_ctx);
1615 context_gl->restore_ctx = NULL;
1616 context_gl->restore_dc = NULL;
1619 if (context_gl->c.destroy_delayed)
1621 TRACE("Destroying context %p.\n", context_gl);
1622 wined3d_context_gl_destroy(context_gl);
1627 static void wined3d_context_gl_enter(struct wined3d_context_gl *context_gl)
1629 TRACE("Entering context %p, level %u.\n", context_gl, context_gl->level + 1);
1631 if (!context_gl->level++)
1633 const struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
1634 HGLRC current_gl = wglGetCurrentContext();
1636 if (current_gl && (!current_context || current_context->gl_ctx != current_gl))
1638 TRACE("Another GL context (%p on device context %p) is already current.\n",
1639 current_gl, wglGetCurrentDC());
1640 context_gl->restore_ctx = current_gl;
1641 context_gl->restore_dc = wglGetCurrentDC();
1642 context_gl->needs_set = 1;
1644 else if (!context_gl->needs_set && !(context_gl->dc_is_private && context_gl->dc_has_format)
1645 && context_gl->pixel_format != context_gl->gl_info->gl_ops.wgl.p_wglGetPixelFormat(context_gl->dc))
1646 context_gl->needs_set = 1;
1650 /* This function takes care of wined3d pixel format selection. */
1651 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1652 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1653 BOOL auxBuffers)
1655 unsigned int cfg_count = wined3d_adapter_gl(device->adapter)->pixel_format_count;
1656 unsigned int current_value;
1657 PIXELFORMATDESCRIPTOR pfd;
1658 int iPixelFormat = 0;
1659 unsigned int i;
1661 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1662 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1663 auxBuffers);
1665 current_value = 0;
1666 for (i = 0; i < cfg_count; ++i)
1668 const struct wined3d_pixel_format *cfg = &wined3d_adapter_gl(device->adapter)->pixel_formats[i];
1669 unsigned int value;
1671 /* For now only accept RGBA formats. Perhaps some day we will
1672 * allow floating point formats for pbuffers. */
1673 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1674 continue;
1675 /* In window mode we need a window drawable format and double buffering. */
1676 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1677 continue;
1678 if (cfg->redSize < color_format->red_size)
1679 continue;
1680 if (cfg->greenSize < color_format->green_size)
1681 continue;
1682 if (cfg->blueSize < color_format->blue_size)
1683 continue;
1684 if (cfg->alphaSize < color_format->alpha_size)
1685 continue;
1686 if (cfg->depthSize < ds_format->depth_size)
1687 continue;
1688 if (ds_format->stencil_size && cfg->stencilSize != ds_format->stencil_size)
1689 continue;
1690 /* Check multisampling support. */
1691 if (cfg->numSamples)
1692 continue;
1694 value = 1;
1695 /* We try to locate a format which matches our requirements exactly. In case of
1696 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1697 if (cfg->depthSize == ds_format->depth_size)
1698 value += 1;
1699 if (cfg->stencilSize == ds_format->stencil_size)
1700 value += 2;
1701 if (cfg->alphaSize == color_format->alpha_size)
1702 value += 4;
1703 /* We like to have aux buffers in backbuffer mode */
1704 if (auxBuffers && cfg->auxBuffers)
1705 value += 8;
1706 if (cfg->redSize == color_format->red_size
1707 && cfg->greenSize == color_format->green_size
1708 && cfg->blueSize == color_format->blue_size)
1709 value += 16;
1711 if (value > current_value)
1713 iPixelFormat = cfg->iPixelFormat;
1714 current_value = value;
1718 if (!iPixelFormat)
1720 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1722 memset(&pfd, 0, sizeof(pfd));
1723 pfd.nSize = sizeof(pfd);
1724 pfd.nVersion = 1;
1725 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1726 pfd.iPixelType = PFD_TYPE_RGBA;
1727 pfd.cAlphaBits = color_format->alpha_size;
1728 pfd.cColorBits = color_format->red_size + color_format->green_size
1729 + color_format->blue_size + color_format->alpha_size;
1730 pfd.cDepthBits = ds_format->depth_size;
1731 pfd.cStencilBits = ds_format->stencil_size;
1732 pfd.iLayerType = PFD_MAIN_PLANE;
1734 if (!(iPixelFormat = ChoosePixelFormat(hdc, &pfd)))
1736 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1737 ERR("Can't find a suitable pixel format.\n");
1738 return 0;
1742 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1743 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1744 return iPixelFormat;
1747 /* Context activation is done by the caller. */
1748 void wined3d_context_gl_bind_dummy_textures(const struct wined3d_context_gl *context_gl)
1750 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
1751 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1752 unsigned int i;
1754 for (i = 0; i < gl_info->limits.combined_samplers; ++i)
1756 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + i));
1758 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
1759 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
1761 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1762 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
1764 if (gl_info->supported[EXT_TEXTURE3D])
1765 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
1767 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1768 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
1770 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
1771 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
1773 if (gl_info->supported[EXT_TEXTURE_ARRAY])
1775 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
1776 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
1779 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1780 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
1782 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1784 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
1785 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
1789 checkGLcall("bind dummy textures");
1792 void wined3d_check_gl_call(const struct wined3d_gl_info *gl_info,
1793 const char *file, unsigned int line, const char *name)
1795 GLint err;
1797 if (gl_info->supported[ARB_DEBUG_OUTPUT] || (err = gl_info->gl_ops.gl.p_glGetError()) == GL_NO_ERROR)
1799 TRACE("%s call ok %s / %u.\n", name, file, line);
1800 return;
1805 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1806 debug_glerror(err), err, name, file,line);
1807 err = gl_info->gl_ops.gl.p_glGetError();
1808 } while (err != GL_NO_ERROR);
1811 static BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1813 return gl_info->supported[ARB_DEBUG_OUTPUT]
1814 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1817 static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1818 GLenum severity, GLsizei length, const char *message, void *ctx)
1820 switch (type)
1822 case GL_DEBUG_TYPE_ERROR_ARB:
1823 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1824 break;
1826 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1827 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1828 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1829 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1830 break;
1832 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1833 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1834 break;
1836 default:
1837 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1838 break;
1842 HGLRC context_create_wgl_attribs(const struct wined3d_gl_info *gl_info, HDC hdc, HGLRC share_ctx)
1844 HGLRC ctx;
1845 unsigned int ctx_attrib_idx = 0;
1846 GLint ctx_attribs[7], ctx_flags = 0;
1848 if (context_debug_output_enabled(gl_info))
1849 ctx_flags = WGL_CONTEXT_DEBUG_BIT_ARB;
1850 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
1851 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version >> 16;
1852 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MINOR_VERSION_ARB;
1853 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version & 0xffff;
1854 if (ctx_flags)
1856 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1857 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1859 ctx_attribs[ctx_attrib_idx] = 0;
1861 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1863 if (gl_info->selected_gl_version >= MAKEDWORD_VERSION(3, 2))
1865 if (ctx_flags)
1867 ctx_flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1868 ctx_attribs[ctx_attrib_idx - 1] = ctx_flags;
1870 else
1872 ctx_flags = WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1873 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1874 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1875 ctx_attribs[ctx_attrib_idx] = 0;
1877 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1878 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1879 GetLastError());
1882 return ctx;
1885 static BOOL wined3d_context_gl_create_wgl_ctx(struct wined3d_context_gl *context_gl,
1886 struct wined3d_swapchain_gl *swapchain_gl)
1888 const struct wined3d_format *colour_format, *ds_format;
1889 struct wined3d_context *context = &context_gl->c;
1890 const struct wined3d_gl_info *gl_info;
1891 struct wined3d_resource *target;
1892 struct wined3d_adapter *adapter;
1893 unsigned int target_bind_flags;
1894 struct wined3d_device *device;
1895 HGLRC ctx, share_ctx;
1896 unsigned int i;
1898 device = context->device;
1899 adapter = device->adapter;
1900 gl_info = &adapter->gl_info;
1902 target = &context->current_rt.texture->resource;
1903 target_bind_flags = target->bind_flags;
1905 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1907 static const enum wined3d_format_id ds_formats[] =
1909 WINED3DFMT_D24_UNORM_S8_UINT,
1910 WINED3DFMT_D32_UNORM,
1911 WINED3DFMT_R24_UNORM_X8_TYPELESS,
1912 WINED3DFMT_D16_UNORM,
1913 WINED3DFMT_S1_UINT_D15_UNORM,
1916 colour_format = target->format;
1918 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1919 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1920 if (colour_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1921 colour_format = wined3d_get_format(adapter, WINED3DFMT_B4G4R4A4_UNORM, target_bind_flags);
1922 else if (colour_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1923 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1925 /* DirectDraw supports 8bit paletted render targets and these are used by
1926 * old games like StarCraft and C&C. Most modern hardware doesn't support
1927 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1928 * conversion (ab)uses the alpha component for storing the palette index.
1929 * For this reason we require a format with 8bit alpha, so request
1930 * A8R8G8B8. */
1931 if (colour_format->id == WINED3DFMT_P8_UINT)
1932 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1934 /* Try to find a pixel format which matches our requirements. */
1935 if (!swapchain_gl->s.ds_format)
1937 for (i = 0; i < ARRAY_SIZE(ds_formats); ++i)
1939 ds_format = wined3d_get_format(adapter, ds_formats[i], WINED3D_BIND_DEPTH_STENCIL);
1940 if ((context_gl->pixel_format = context_choose_pixel_format(device,
1941 context_gl->dc, colour_format, ds_format, TRUE)))
1943 swapchain_gl->s.ds_format = ds_format;
1944 break;
1947 TRACE("Depth stencil format %s is not supported, trying next format.\n",
1948 debug_d3dformat(ds_format->id));
1951 else
1953 context_gl->pixel_format = context_choose_pixel_format(device,
1954 context_gl->dc, colour_format, swapchain_gl->s.ds_format, TRUE);
1957 else
1959 /* When using FBOs for off-screen rendering, we only use the drawable for
1960 * presentation blits, and don't do any rendering to it. That means we
1961 * don't need depth or stencil buffers, and can mostly ignore the render
1962 * target format. This wouldn't necessarily be quite correct for 10bpc
1963 * display modes, but we don't currently support those.
1964 * Using the same format regardless of the colour/depth/stencil targets
1965 * makes it much less likely that different wined3d instances will set
1966 * conflicting pixel formats. */
1967 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1968 ds_format = wined3d_get_format(adapter, WINED3DFMT_UNKNOWN, WINED3D_BIND_DEPTH_STENCIL);
1969 context_gl->pixel_format = context_choose_pixel_format(device,
1970 context_gl->dc, colour_format, ds_format, FALSE);
1973 if (!context_gl->pixel_format)
1975 ERR("Failed to choose pixel format.\n");
1976 return FALSE;
1979 wined3d_context_gl_enter(context_gl);
1981 if (!wined3d_context_gl_set_pixel_format(context_gl))
1983 context_release(context);
1985 if (context_gl->dc_is_private)
1987 ERR("Failed to set pixel format %d on device context %p.\n", context_gl->pixel_format, context_gl->dc);
1989 return FALSE;
1992 WARN("Failed to set pixel format %d on device context %p, trying backup DC.\n",
1993 context_gl->pixel_format, context_gl->dc);
1995 wined3d_release_dc(context_gl->window, context_gl->dc);
1996 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
1998 ERR("Failed to retrieve the backup device context.\n");
1999 return FALSE;
2001 context_gl->dc_is_private = TRUE;
2003 return wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl);
2006 share_ctx = device->context_count ? wined3d_context_gl(device->contexts[0])->gl_ctx : NULL;
2007 if (gl_info->p_wglCreateContextAttribsARB)
2009 if (!(ctx = context_create_wgl_attribs(gl_info, context_gl->dc, share_ctx)))
2011 ERR("Failed to create a WGL context.\n");
2012 context_release(context);
2013 return FALSE;
2016 else
2018 if (!(ctx = wglCreateContext(context_gl->dc)))
2020 ERR("Failed to create a WGL context.\n");
2021 context_release(context);
2022 return FALSE;
2025 if (share_ctx && !wglShareLists(share_ctx, ctx))
2027 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
2028 context_release(context);
2029 if (!wglDeleteContext(ctx))
2030 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2031 return FALSE;
2035 context_gl->dc_has_format = TRUE;
2036 context_gl->needs_set = 1;
2037 context_gl->valid = 1;
2038 context_gl->gl_ctx = ctx;
2040 return TRUE;
2043 HRESULT wined3d_context_gl_init(struct wined3d_context_gl *context_gl, struct wined3d_swapchain_gl *swapchain_gl)
2045 struct wined3d_context *context = &context_gl->c;
2046 const struct wined3d_d3d_info *d3d_info;
2047 const struct wined3d_gl_info *gl_info;
2048 struct wined3d_device *device;
2049 unsigned int i;
2051 TRACE("context_gl %p, swapchain %p.\n", context_gl, swapchain_gl);
2053 wined3d_context_init(&context_gl->c, &swapchain_gl->s);
2055 device = context->device;
2056 gl_info = &device->adapter->gl_info;
2057 context_gl->gl_info = gl_info;
2058 d3d_info = context->d3d_info;
2060 context_gl->tid = GetCurrentThreadId();
2061 context_gl->window = context->swapchain->win_handle;
2062 if (context_gl->window == GetDesktopWindow())
2064 TRACE("Swapchain is created on the desktop window, trying backup device context.\n");
2065 context_gl->dc = NULL;
2067 else if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
2068 WARN("Failed to retrieve device context, trying swapchain backup.\n");
2070 if (!context_gl->dc)
2072 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2074 ERR("Failed to retrieve a device context.\n");
2075 return E_FAIL;
2077 context_gl->dc_is_private = TRUE;
2080 list_init(&context_gl->fbo_list);
2081 list_init(&context_gl->fbo_destroy_list);
2083 list_init(&context_gl->occlusion_queries);
2084 list_init(&context_gl->fences);
2085 list_init(&context_gl->timestamp_queries);
2086 list_init(&context_gl->so_statistics_queries);
2087 list_init(&context_gl->pipeline_statistics_queries);
2089 for (i = 0; i < ARRAY_SIZE(context_gl->tex_unit_map); ++i)
2090 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2091 for (i = 0; i < ARRAY_SIZE(context_gl->rev_tex_unit_map); ++i)
2092 context_gl->rev_tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2093 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
2095 /* Initialize the texture unit mapping to a 1:1 mapping. */
2096 unsigned int base, count;
2098 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_PIXEL, &base, &count);
2099 if (base + WINED3D_MAX_FRAGMENT_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2101 ERR("Unexpected texture unit base index %u.\n", base);
2102 goto fail;
2104 for (i = 0; i < min(count, WINED3D_MAX_FRAGMENT_SAMPLERS); ++i)
2106 context_gl->tex_unit_map[i] = base + i;
2107 context_gl->rev_tex_unit_map[base + i] = i;
2110 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_VERTEX, &base, &count);
2111 if (base + WINED3D_MAX_VERTEX_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2113 ERR("Unexpected texture unit base index %u.\n", base);
2114 goto fail;
2116 for (i = 0; i < min(count, WINED3D_MAX_VERTEX_SAMPLERS); ++i)
2118 context_gl->tex_unit_map[WINED3D_MAX_FRAGMENT_SAMPLERS + i] = base + i;
2119 context_gl->rev_tex_unit_map[base + i] = WINED3D_MAX_FRAGMENT_SAMPLERS + i;
2123 if (!(context_gl->texture_type = heap_calloc(gl_info->limits.combined_samplers,
2124 sizeof(*context_gl->texture_type))))
2125 goto fail;
2127 if (!wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl))
2128 goto fail;
2130 /* Set up the context defaults. */
2132 context->render_offscreen = wined3d_resource_is_offscreen(&context->current_rt.texture->resource);
2133 context_gl->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
2135 if (!wined3d_context_gl_set_current(context_gl))
2137 ERR("Cannot activate context to set up defaults.\n");
2138 context_release(context);
2139 if (!wglDeleteContext(context_gl->gl_ctx))
2140 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context_gl->gl_ctx, GetLastError());
2141 goto fail;
2144 if (context_debug_output_enabled(gl_info))
2146 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback, context));
2147 if (TRACE_ON(d3d_sync))
2148 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2149 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
2150 if (ERR_ON(d3d))
2152 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR,
2153 GL_DONT_CARE, 0, NULL, GL_TRUE));
2155 if (FIXME_ON(d3d))
2157 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
2158 GL_DONT_CARE, 0, NULL, GL_TRUE));
2159 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
2160 GL_DONT_CARE, 0, NULL, GL_TRUE));
2161 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY,
2162 GL_DONT_CARE, 0, NULL, GL_TRUE));
2164 if (WARN_ON(d3d_perf))
2166 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE,
2167 GL_DONT_CARE, 0, NULL, GL_TRUE));
2171 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2172 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &context_gl->aux_buffers);
2174 TRACE("Setting up the screen\n");
2176 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2178 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
2179 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2181 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
2182 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2184 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2185 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2187 else
2189 GLuint vao;
2191 GL_EXTCALL(glGenVertexArrays(1, &vao));
2192 GL_EXTCALL(glBindVertexArray(vao));
2193 checkGLcall("creating VAO");
2196 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
2197 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2198 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2199 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2201 if (gl_info->supported[NV_TEXTURE_SHADER2])
2203 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2204 * the previous texture where to source the offset from is always unit - 1.
2206 for (i = 1; i < gl_info->limits.textures; ++i)
2208 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2209 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
2210 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + i - 1);
2211 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2214 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2216 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2217 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2218 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2219 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2220 * is ever assigned.
2222 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2223 * program and the dummy program is destroyed when the context is destroyed.
2225 static const char dummy_program[] =
2226 "!!ARBfp1.0\n"
2227 "MOV result.color, fragment.color.primary;\n"
2228 "END\n";
2229 GL_EXTCALL(glGenProgramsARB(1, &context_gl->dummy_arbfp_prog));
2230 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, context_gl->dummy_arbfp_prog));
2231 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
2232 GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2235 if (gl_info->supported[ARB_POINT_SPRITE])
2237 for (i = 0; i < gl_info->limits.textures; ++i)
2239 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2240 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2241 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2245 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2247 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2249 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2251 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2253 if (!(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART))
2255 if (gl_info->supported[ARB_ES3_COMPATIBILITY])
2257 gl_info->gl_ops.gl.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
2258 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2260 else
2262 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2265 if (!(d3d_info->wined3d_creation_flags & WINED3D_LEGACY_CUBEMAP_FILTERING)
2266 && gl_info->supported[ARB_SEAMLESS_CUBE_MAP])
2268 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
2269 checkGLcall("enable seamless cube map filtering");
2271 if (gl_info->supported[ARB_CLIP_CONTROL])
2272 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT));
2274 /* If this happens to be the first context for the device, dummy textures
2275 * are not created yet. In that case, they will be created (and bound) by
2276 * create_dummy_textures right after this context is initialized. */
2277 if (wined3d_device_gl(device)->dummy_textures.tex_2d)
2278 wined3d_context_gl_bind_dummy_textures(context_gl);
2280 /* Initialise all rectangles to avoid resetting unused ones later. */
2281 gl_info->gl_ops.gl.p_glScissor(0, 0, 0, 0);
2282 checkGLcall("glScissor");
2284 return WINED3D_OK;
2286 fail:
2287 heap_free(context_gl->texture_type);
2288 wined3d_release_dc(context_gl->window, context_gl->dc);
2289 return E_FAIL;
2292 void wined3d_context_gl_destroy(struct wined3d_context_gl *context_gl)
2294 struct wined3d_device *device = context_gl->c.device;
2296 TRACE("Destroying context %p.\n", context_gl);
2298 wined3d_from_cs(device->cs);
2300 /* We delay destroying a context when it is active. The context_release()
2301 * function invokes wined3d_context_gl_destroy() again while leaving the
2302 * last level. */
2303 if (context_gl->level)
2305 TRACE("Delaying destruction of context %p.\n", context_gl);
2306 context_gl->c.destroy_delayed = 1;
2307 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2308 context_gl->c.swapchain = NULL;
2309 return;
2312 device_context_remove(device, &context_gl->c);
2314 if (context_gl->c.current && context_gl->tid != GetCurrentThreadId())
2316 struct wined3d_gl_info *gl_info;
2318 /* Make a copy of gl_info for wined3d_context_gl_cleanup() use, the
2319 * one in wined3d_adapter may go away in the meantime. */
2320 gl_info = heap_alloc(sizeof(*gl_info));
2321 *gl_info = *context_gl->gl_info;
2322 context_gl->gl_info = gl_info;
2323 context_gl->c.destroyed = 1;
2325 return;
2328 wined3d_context_gl_cleanup(context_gl);
2329 TlsSetValue(context_get_tls_idx(), NULL);
2330 heap_free(context_gl);
2333 const unsigned int *wined3d_context_gl_get_tex_unit_mapping(const struct wined3d_context_gl *context_gl,
2334 const struct wined3d_shader_version *shader_version, unsigned int *base, unsigned int *count)
2336 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2338 if (!shader_version)
2340 *base = 0;
2341 *count = WINED3D_MAX_TEXTURES;
2342 return context_gl->tex_unit_map;
2345 if (shader_version->major >= 4)
2347 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, shader_version->type, base, count);
2348 return NULL;
2351 switch (shader_version->type)
2353 case WINED3D_SHADER_TYPE_PIXEL:
2354 *base = 0;
2355 *count = WINED3D_MAX_FRAGMENT_SAMPLERS;
2356 break;
2357 case WINED3D_SHADER_TYPE_VERTEX:
2358 *base = WINED3D_MAX_FRAGMENT_SAMPLERS;
2359 *count = WINED3D_MAX_VERTEX_SAMPLERS;
2360 break;
2361 default:
2362 ERR("Unhandled shader type %#x.\n", shader_version->type);
2363 *base = 0;
2364 *count = 0;
2367 return context_gl->tex_unit_map;
2370 static void wined3d_context_gl_get_rt_size(const struct wined3d_context_gl *context_gl, SIZE *size)
2372 const struct wined3d_texture *rt = context_gl->c.current_rt.texture;
2373 unsigned int level;
2375 if (rt->swapchain)
2377 RECT window_size;
2379 GetClientRect(context_gl->window, &window_size);
2380 size->cx = window_size.right - window_size.left;
2381 size->cy = window_size.bottom - window_size.top;
2383 return;
2386 level = context_gl->c.current_rt.sub_resource_idx % rt->level_count;
2387 size->cx = wined3d_texture_get_level_width(rt, level);
2388 size->cy = wined3d_texture_get_level_height(rt, level);
2391 void wined3d_context_gl_enable_clip_distances(struct wined3d_context_gl *context_gl, uint32_t enable_mask)
2393 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2394 unsigned int clip_distance_count, i;
2395 uint32_t disable_mask, current_mask;
2397 clip_distance_count = gl_info->limits.user_clip_distances;
2398 disable_mask = ~enable_mask;
2399 enable_mask &= (1u << clip_distance_count) - 1;
2400 disable_mask &= (1u << clip_distance_count) - 1;
2401 current_mask = context_gl->c.clip_distance_mask;
2402 context_gl->c.clip_distance_mask = enable_mask;
2404 enable_mask &= ~current_mask;
2405 while (enable_mask)
2407 i = wined3d_bit_scan(&enable_mask);
2408 gl_info->gl_ops.gl.p_glEnable(GL_CLIP_DISTANCE0 + i);
2410 disable_mask &= current_mask;
2411 while (disable_mask)
2413 i = wined3d_bit_scan(&disable_mask);
2414 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_DISTANCE0 + i);
2416 checkGLcall("toggle clip distances");
2419 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2421 return rt_mask & (1u << 31);
2424 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2426 return rt_mask & ~(1u << 31);
2429 /* Context activation is done by the caller. */
2430 static void wined3d_context_gl_apply_draw_buffers(struct wined3d_context_gl *context_gl, uint32_t rt_mask)
2432 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2433 GLenum draw_buffers[WINED3D_MAX_RENDER_TARGETS];
2435 if (!rt_mask)
2437 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2439 else if (is_rt_mask_onscreen(rt_mask))
2441 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2443 else
2445 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2447 unsigned int i = 0;
2449 while (rt_mask)
2451 if (rt_mask & 1)
2452 draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2453 else
2454 draw_buffers[i] = GL_NONE;
2456 rt_mask >>= 1;
2457 ++i;
2460 if (gl_info->supported[ARB_DRAW_BUFFERS])
2462 GL_EXTCALL(glDrawBuffers(i, draw_buffers));
2464 else
2466 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffers[0]);
2469 else
2471 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2475 checkGLcall("apply draw buffers");
2478 /* Context activation is done by the caller. */
2479 void wined3d_context_gl_set_draw_buffer(struct wined3d_context_gl *context_gl, GLenum buffer)
2481 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2482 struct fbo_entry *current_fbo = context_gl->current_fbo;
2483 uint32_t new_mask = context_generate_rt_mask(buffer);
2484 uint32_t *current_mask;
2486 current_mask = current_fbo ? &current_fbo->rt_mask : &context_gl->draw_buffers_mask;
2487 if (new_mask == *current_mask)
2488 return;
2490 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2491 checkGLcall("glDrawBuffer()");
2493 *current_mask = new_mask;
2496 /* Context activation is done by the caller. */
2497 void wined3d_context_gl_active_texture(struct wined3d_context_gl *context_gl,
2498 const struct wined3d_gl_info *gl_info, unsigned int unit)
2500 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + unit));
2501 checkGLcall("glActiveTexture");
2502 context_gl->active_texture = unit;
2505 void wined3d_context_gl_bind_bo(struct wined3d_context_gl *context_gl, GLenum binding, GLuint name)
2507 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2509 if (binding == GL_ELEMENT_ARRAY_BUFFER)
2510 context_invalidate_state(&context_gl->c, STATE_INDEXBUFFER);
2512 GL_EXTCALL(glBindBuffer(binding, name));
2515 void wined3d_context_gl_bind_texture(struct wined3d_context_gl *context_gl, GLenum target, GLuint name)
2517 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
2518 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2519 GLenum old_texture_type;
2520 unsigned int unit;
2522 if (name)
2523 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2524 else
2525 target = GL_NONE;
2527 unit = context_gl->active_texture;
2528 old_texture_type = context_gl->texture_type[unit];
2529 if (old_texture_type != target)
2531 switch (old_texture_type)
2533 case GL_NONE:
2534 /* nothing to do */
2535 break;
2536 case GL_TEXTURE_1D:
2537 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
2538 break;
2539 case GL_TEXTURE_1D_ARRAY:
2540 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
2541 break;
2542 case GL_TEXTURE_2D:
2543 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
2544 break;
2545 case GL_TEXTURE_2D_ARRAY:
2546 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
2547 break;
2548 case GL_TEXTURE_RECTANGLE_ARB:
2549 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
2550 break;
2551 case GL_TEXTURE_CUBE_MAP:
2552 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
2553 break;
2554 case GL_TEXTURE_CUBE_MAP_ARRAY:
2555 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
2556 break;
2557 case GL_TEXTURE_3D:
2558 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
2559 break;
2560 case GL_TEXTURE_BUFFER:
2561 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
2562 break;
2563 case GL_TEXTURE_2D_MULTISAMPLE:
2564 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
2565 break;
2566 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2567 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
2568 break;
2569 default:
2570 ERR("Unexpected texture target %#x.\n", old_texture_type);
2573 context_gl->texture_type[unit] = target;
2576 checkGLcall("bind texture");
2579 static void wined3d_context_gl_poll_fences(struct wined3d_context_gl *context_gl)
2581 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2582 struct wined3d_command_fence_gl *f;
2583 SIZE_T i;
2585 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2587 f = &context_gl->submitted.fences[i];
2589 if (f->id > device_gl->completed_fence_id)
2591 if (wined3d_fence_test(f->fence, &device_gl->d, 0) != WINED3D_FENCE_OK)
2592 continue;
2593 device_gl->completed_fence_id = f->id;
2596 wined3d_fence_destroy(f->fence);
2597 if (i != context_gl->submitted.fence_count - 1)
2598 *f = context_gl->submitted.fences[context_gl->submitted.fence_count - 1];
2599 --context_gl->submitted.fence_count;
2603 void wined3d_context_gl_wait_command_fence(struct wined3d_context_gl *context_gl, uint64_t id)
2605 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2606 enum wined3d_fence_result ret;
2607 SIZE_T i;
2609 if (id <= device_gl->completed_fence_id
2610 || id > device_gl->current_fence_id) /* In case the fence ID wrapped. */
2611 return;
2613 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2615 if (context_gl->submitted.fences[i].id != id)
2616 continue;
2618 if ((ret = wined3d_fence_wait(context_gl->submitted.fences[i].fence, &device_gl->d)) != WINED3D_FENCE_OK)
2619 ERR("Failed to wait for command fence with id 0x%s, ret %#x.\n", wine_dbgstr_longlong(id), ret);
2620 wined3d_context_gl_poll_fences(context_gl);
2621 return;
2624 ERR("Failed to find fence for command fence with id 0x%s.\n", wine_dbgstr_longlong(id));
2627 void wined3d_context_gl_submit_command_fence(struct wined3d_context_gl *context_gl)
2629 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2630 struct wined3d_command_fence_gl *f;
2631 HRESULT hr;
2633 if (!wined3d_array_reserve((void **)&context_gl->submitted.fences, &context_gl->submitted.fences_size,
2634 context_gl->submitted.fence_count + 1, sizeof(*context_gl->submitted.fences)))
2635 ERR("Failed to grow submitted command buffer array.\n");
2637 f = &context_gl->submitted.fences[context_gl->submitted.fence_count++];
2638 f->id = device_gl->current_fence_id;
2639 if (FAILED(hr = wined3d_fence_create(&device_gl->d, &f->fence)))
2640 ERR("Failed to create fence, hr %#x.\n", hr);
2641 wined3d_fence_issue(f->fence, &device_gl->d);
2643 /* We don't expect this to ever happen, but handle it anyway. */
2644 if (!++device_gl->current_fence_id)
2646 wined3d_context_gl_wait_command_fence(context_gl, device_gl->current_fence_id - 1);
2647 device_gl->completed_fence_id = 0;
2648 device_gl->current_fence_id = 1;
2650 wined3d_context_gl_poll_fences(context_gl);
2653 static void *wined3d_bo_gl_map(struct wined3d_bo_gl *bo,
2654 struct wined3d_context_gl *context_gl, size_t offset, size_t size, uint32_t flags)
2656 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2657 const struct wined3d_gl_info *gl_info;
2658 struct wined3d_bo_user *bo_user;
2659 struct wined3d_bo_gl tmp;
2660 uint8_t *map_ptr;
2662 if (flags & WINED3D_MAP_NOOVERWRITE)
2663 goto map;
2665 if ((flags & WINED3D_MAP_DISCARD) && bo->command_fence_id > device_gl->completed_fence_id)
2667 if (wined3d_context_gl_create_bo(context_gl, bo->size,
2668 bo->binding, bo->usage, bo->coherent, bo->flags, &tmp))
2670 list_move_head(&tmp.users, &bo->users);
2671 wined3d_context_gl_destroy_bo(context_gl, bo);
2672 *bo = tmp;
2673 list_init(&bo->users);
2674 list_move_head(&bo->users, &tmp.users);
2675 LIST_FOR_EACH_ENTRY(bo_user, &bo->users, struct wined3d_bo_user, entry)
2677 bo_user->valid = false;
2680 goto map;
2683 ERR("Failed to create new buffer object.\n");
2686 if (bo->command_fence_id == device_gl->current_fence_id)
2687 wined3d_context_gl_submit_command_fence(context_gl);
2688 wined3d_context_gl_wait_command_fence(context_gl, bo->command_fence_id);
2690 map:
2691 gl_info = context_gl->gl_info;
2692 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2694 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2696 map_ptr = GL_EXTCALL(glMapBufferRange(bo->binding, offset, size, wined3d_resource_gl_map_flags(flags)));
2698 else
2700 map_ptr = GL_EXTCALL(glMapBuffer(bo->binding, wined3d_resource_gl_legacy_map_flags(flags)));
2701 map_ptr += offset;
2704 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2705 checkGLcall("Map buffer object");
2707 return map_ptr;
2710 void *wined3d_context_gl_map_bo_address(struct wined3d_context_gl *context_gl,
2711 const struct wined3d_bo_address *data, size_t size, uint32_t flags)
2713 struct wined3d_bo_gl *bo;
2714 void *map_ptr;
2716 if (!(bo = (struct wined3d_bo_gl *)data->buffer_object))
2717 return data->addr;
2719 if (!(map_ptr = wined3d_bo_gl_map(bo, context_gl, (uintptr_t)data->addr, size, flags)))
2720 ERR("Failed to map bo.\n");
2722 return map_ptr;
2725 void wined3d_context_gl_unmap_bo_address(struct wined3d_context_gl *context_gl,
2726 const struct wined3d_bo_address *data, unsigned int range_count, const struct wined3d_range *ranges)
2728 const struct wined3d_gl_info *gl_info;
2729 struct wined3d_bo_gl *bo;
2730 unsigned int i;
2732 if (!(bo = (struct wined3d_bo_gl *)data->buffer_object))
2733 return;
2735 gl_info = context_gl->gl_info;
2736 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2738 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2740 for (i = 0; i < range_count; ++i)
2742 GL_EXTCALL(glFlushMappedBufferRange(bo->binding,
2743 (UINT_PTR)data->addr + ranges[i].offset, ranges[i].size));
2746 else if (!bo->coherent && gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
2748 for (i = 0; i < range_count; ++i)
2750 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(bo->binding,
2751 (uintptr_t)data->addr + ranges[i].offset, ranges[i].size));
2752 checkGLcall("glFlushMappedBufferRangeAPPLE");
2756 GL_EXTCALL(glUnmapBuffer(bo->binding));
2757 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2758 checkGLcall("Unmap buffer object");
2761 void wined3d_context_gl_copy_bo_address(struct wined3d_context_gl *context_gl,
2762 const struct wined3d_bo_address *dst, const struct wined3d_bo_address *src, size_t size)
2764 const struct wined3d_gl_info *gl_info;
2765 struct wined3d_bo_gl *src_bo, *dst_bo;
2766 struct wined3d_range range;
2767 BYTE *dst_ptr, *src_ptr;
2769 gl_info = context_gl->gl_info;
2770 src_bo = (struct wined3d_bo_gl *)src->buffer_object;
2771 dst_bo = (struct wined3d_bo_gl *)dst->buffer_object;
2773 if (dst_bo && src_bo)
2775 if (gl_info->supported[ARB_COPY_BUFFER])
2777 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER, src_bo->id));
2778 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER, dst_bo->id));
2779 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
2780 (GLintptr)src->addr, (GLintptr)dst->addr, size));
2781 checkGLcall("direct buffer copy");
2783 wined3d_context_gl_reference_bo(context_gl, src_bo);
2784 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2786 else
2788 src_ptr = wined3d_context_gl_map_bo_address(context_gl, src, size, WINED3D_MAP_READ);
2789 dst_ptr = wined3d_context_gl_map_bo_address(context_gl, dst, size, WINED3D_MAP_WRITE);
2791 memcpy(dst_ptr, src_ptr, size);
2793 range.offset = 0;
2794 range.size = size;
2795 wined3d_context_gl_unmap_bo_address(context_gl, dst, 1, &range);
2796 wined3d_context_gl_unmap_bo_address(context_gl, src, 0, NULL);
2799 else if (!dst_bo && src_bo)
2801 wined3d_context_gl_bind_bo(context_gl, src_bo->binding, src_bo->id);
2802 GL_EXTCALL(glGetBufferSubData(src_bo->binding, (GLintptr)src->addr, size, dst->addr));
2803 checkGLcall("buffer download");
2805 wined3d_context_gl_reference_bo(context_gl, src_bo);
2807 else if (dst_bo && !src_bo)
2809 wined3d_context_gl_bind_bo(context_gl, dst_bo->binding, dst_bo->id);
2810 GL_EXTCALL(glBufferSubData(dst_bo->binding, (GLintptr)dst->addr, size, src->addr));
2811 checkGLcall("buffer upload");
2813 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2815 else
2817 memcpy(dst->addr, src->addr, size);
2821 void wined3d_context_gl_destroy_bo(struct wined3d_context_gl *context_gl, struct wined3d_bo_gl *bo)
2823 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2825 TRACE("context_gl %p, bo %p.\n", context_gl, bo);
2827 TRACE("Destroying GL buffer %u.\n", bo->id);
2828 GL_EXTCALL(glDeleteBuffers(1, &bo->id));
2829 checkGLcall("buffer object destruction");
2830 bo->id = 0;
2833 bool wined3d_context_gl_create_bo(struct wined3d_context_gl *context_gl, GLsizeiptr size,
2834 GLenum binding, GLenum usage, bool coherent, GLbitfield flags, struct wined3d_bo_gl *bo)
2836 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2837 GLuint id = 0;
2839 TRACE("context_gl %p, size %lu, binding %#x, usage %#x, coherent %#x, flags %#x, bo %p.\n",
2840 context_gl, size, binding, usage, coherent, flags, bo);
2842 GL_EXTCALL(glGenBuffers(1, &id));
2843 if (!id)
2845 checkGLcall("buffer object creation");
2846 return false;
2848 wined3d_context_gl_bind_bo(context_gl, binding, id);
2850 if (!coherent && gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
2852 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
2853 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
2856 if (gl_info->supported[ARB_BUFFER_STORAGE])
2857 GL_EXTCALL(glBufferStorage(binding, size, NULL, flags | GL_DYNAMIC_STORAGE_BIT));
2858 else
2859 GL_EXTCALL(glBufferData(binding, size, NULL, usage));
2861 wined3d_context_gl_bind_bo(context_gl, binding, 0);
2862 checkGLcall("buffer object creation");
2864 TRACE("Created buffer object %u.\n", id);
2865 bo->id = id;
2866 bo->size = size;
2867 bo->binding = binding;
2868 bo->usage = usage;
2869 bo->flags = flags;
2870 bo->coherent = coherent;
2871 list_init(&bo->users);
2872 bo->command_fence_id = 0;
2874 return true;
2877 static void wined3d_context_gl_set_render_offscreen(struct wined3d_context_gl *context_gl, BOOL offscreen)
2879 if (context_gl->c.render_offscreen == offscreen)
2880 return;
2882 context_invalidate_state(&context_gl->c, STATE_VIEWPORT);
2883 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
2884 if (!context_gl->gl_info->supported[ARB_CLIP_CONTROL])
2886 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
2887 context_invalidate_state(&context_gl->c, STATE_POINTSPRITECOORDORIGIN);
2888 context_invalidate_state(&context_gl->c, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2890 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN));
2891 if (context_gl->gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2892 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
2893 context_gl->c.render_offscreen = offscreen;
2896 GLenum wined3d_context_gl_get_offscreen_gl_buffer(const struct wined3d_context_gl *context_gl)
2898 switch (wined3d_settings.offscreen_rendering_mode)
2900 case ORM_FBO:
2901 return GL_COLOR_ATTACHMENT0;
2903 case ORM_BACKBUFFER:
2904 return context_gl->aux_buffers > 0 ? GL_AUX0 : GL_BACK;
2906 default:
2907 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
2908 return GL_BACK;
2912 static uint32_t wined3d_context_gl_generate_rt_mask_no_fbo(const struct wined3d_context_gl *context_gl,
2913 struct wined3d_resource *rt)
2915 if (!rt || rt->format->id == WINED3DFMT_NULL)
2916 return 0;
2917 else if (rt->type != WINED3D_RTYPE_BUFFER && texture_from_resource(rt)->swapchain)
2918 return context_generate_rt_mask_from_resource(rt);
2919 else
2920 return context_generate_rt_mask(wined3d_context_gl_get_offscreen_gl_buffer(context_gl));
2923 /* Context activation is done by the caller. */
2924 void wined3d_context_gl_apply_blit_state(struct wined3d_context_gl *context_gl, const struct wined3d_device *device)
2926 struct wined3d_context *context = &context_gl->c;
2927 const struct wined3d_gl_info *gl_info;
2928 uint32_t rt_mask, *cur_mask;
2929 struct wined3d_texture *rt;
2930 unsigned int sampler;
2931 SIZE rt_size;
2933 TRACE("Setting up context %p for blitting.\n", context);
2935 gl_info = context_gl->gl_info;
2936 rt = context->current_rt.texture;
2938 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2940 if (context->render_offscreen)
2942 wined3d_texture_load(rt, context, FALSE);
2944 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_FRAMEBUFFER, &rt->resource,
2945 context->current_rt.sub_resource_idx, NULL, 0, rt->resource.draw_binding);
2946 if (rt->resource.format->id != WINED3DFMT_NULL)
2947 rt_mask = 1;
2948 else
2949 rt_mask = 0;
2951 else
2953 context_gl->current_fbo = NULL;
2954 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
2955 rt_mask = context_generate_rt_mask_from_resource(&rt->resource);
2958 else
2960 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, &rt->resource);
2963 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
2965 if (rt_mask != *cur_mask)
2967 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
2968 *cur_mask = rt_mask;
2971 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2972 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
2973 context_invalidate_state(context, STATE_FRAMEBUFFER);
2975 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
2977 if (context->last_was_blit)
2979 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
2981 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
2982 context->viewport_count = WINED3D_MAX_VIEWPORTS;
2983 context_gl->blit_size = rt_size;
2984 /* No need to dirtify here, the states are still dirtified because
2985 * they weren't applied since the last context_apply_blit_state()
2986 * call. */
2988 checkGLcall("blit state application");
2989 TRACE("Context is already set up for blitting, nothing to do.\n");
2990 return;
2992 context->last_was_blit = TRUE;
2994 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
2995 GL_EXTCALL(glBindSampler(0, 0));
2996 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
2998 sampler = context_gl->rev_tex_unit_map[0];
2999 if (sampler != WINED3D_UNMAPPED_STAGE)
3001 if (sampler < WINED3D_MAX_TEXTURES)
3003 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
3004 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
3006 context_invalidate_state(context, STATE_SAMPLER(sampler));
3008 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
3009 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
3011 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3013 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
3014 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
3016 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3017 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3018 context_invalidate_state(context, STATE_BLEND);
3019 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
3020 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
3021 context_invalidate_state(context, STATE_RASTERIZER);
3022 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
3023 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
3024 context_invalidate_state(context, STATE_DEPTH_STENCIL);
3025 if (gl_info->supported[ARB_POINT_SPRITE])
3027 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
3028 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
3030 if (gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3032 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3033 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3036 context->last_was_rhw = TRUE;
3037 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
3039 wined3d_context_gl_enable_clip_distances(context_gl, 0);
3040 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
3042 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
3043 if (gl_info->supported[ARB_CLIP_CONTROL])
3044 GL_EXTCALL(glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE));
3045 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
3046 context->viewport_count = WINED3D_MAX_VIEWPORTS;
3047 context_invalidate_state(context, STATE_VIEWPORT);
3049 device->shader_backend->shader_disable(device->shader_priv, context);
3051 context_gl->blit_size = rt_size;
3053 checkGLcall("blit state application");
3056 static void wined3d_context_gl_apply_blit_projection(const struct wined3d_context_gl *context_gl,
3057 unsigned int w, unsigned int h)
3059 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3060 const GLdouble projection[] =
3062 2.0 / w, 0.0, 0.0, 0.0,
3063 0.0, 2.0 / h, 0.0, 0.0,
3064 0.0, 0.0, 2.0, 0.0,
3065 -1.0, -1.0, -1.0, 1.0,
3068 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
3069 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
3072 /* Setup OpenGL states for fixed-function blitting. */
3073 /* Context activation is done by the caller. */
3074 void wined3d_context_gl_apply_ffp_blit_state(struct wined3d_context_gl *context_gl,
3075 const struct wined3d_device *device)
3077 struct wined3d_context *context = &context_gl->c;
3078 const struct wined3d_gl_info *gl_info;
3079 unsigned int i, sampler;
3081 gl_info = context_gl->gl_info;
3082 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3083 ERR("Applying fixed-function state without legacy context support.\n");
3085 if (context->last_was_ffp_blit)
3087 SIZE rt_size;
3089 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
3090 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
3091 wined3d_context_gl_apply_blit_projection(context_gl, rt_size.cx, rt_size.cy);
3092 wined3d_context_gl_apply_blit_state(context_gl, device);
3094 checkGLcall("ffp blit state application");
3095 return;
3097 context->last_was_ffp_blit = TRUE;
3099 wined3d_context_gl_apply_blit_state(context_gl, device);
3101 /* Disable all textures. The caller can then bind a texture it wants to blit
3102 * from. */
3103 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
3105 wined3d_context_gl_active_texture(context_gl, gl_info, i);
3107 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3108 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3109 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3110 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3111 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3112 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3114 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3116 sampler = context_gl->rev_tex_unit_map[i];
3117 if (sampler != WINED3D_UNMAPPED_STAGE)
3119 if (sampler < WINED3D_MAX_TEXTURES)
3120 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
3121 context_invalidate_state(context, STATE_SAMPLER(sampler));
3125 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
3127 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3128 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3129 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3130 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3131 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3132 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3134 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3135 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
3136 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
3138 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
3139 gl_info->gl_ops.gl.p_glLoadIdentity();
3141 /* Setup transforms. */
3142 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3143 gl_info->gl_ops.gl.p_glLoadIdentity();
3144 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3145 wined3d_context_gl_apply_blit_projection(context_gl, context_gl->blit_size.cx, context_gl->blit_size.cy);
3146 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
3148 /* Other misc states. */
3149 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
3150 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
3151 gl_info->p_glDisableWINE(GL_FOG);
3152 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
3154 if (gl_info->supported[EXT_SECONDARY_COLOR])
3156 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
3157 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
3159 checkGLcall("ffp blit state application");
3162 static BOOL have_framebuffer_attachment(unsigned int rt_count, struct wined3d_rendertarget_view * const *rts,
3163 const struct wined3d_rendertarget_view *ds)
3165 unsigned int i;
3167 if (ds)
3168 return TRUE;
3170 for (i = 0; i < rt_count; ++i)
3172 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3173 return TRUE;
3176 return FALSE;
3179 /* Context activation is done by the caller. */
3180 BOOL wined3d_context_gl_apply_clear_state(struct wined3d_context_gl *context_gl,
3181 const struct wined3d_state *state, unsigned int rt_count, const struct wined3d_fb_state *fb)
3183 struct wined3d_rendertarget_view * const *rts = fb->render_targets;
3184 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3185 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
3186 uint32_t rt_mask = 0, *cur_mask;
3187 unsigned int i;
3189 if (isStateDirty(&context_gl->c, STATE_FRAMEBUFFER) || fb != &state->fb
3190 || rt_count != gl_info->limits.buffers)
3192 if (!have_framebuffer_attachment(rt_count, rts, dsv))
3194 WARN("Invalid render target config, need at least one attachment.\n");
3195 return FALSE;
3198 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3200 struct wined3d_rendertarget_info ds_info = {{0}};
3202 if (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource))
3204 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3205 for (i = 0; i < rt_count; ++i)
3207 if (rts[i])
3209 struct wined3d_rendertarget_view_gl *rtv_gl = wined3d_rendertarget_view_gl(rts[i]);
3210 context_gl->blit_targets[i].gl_view = rtv_gl->gl_view;
3211 context_gl->blit_targets[i].resource = rtv_gl->v.resource;
3212 context_gl->blit_targets[i].sub_resource_idx = rtv_gl->v.sub_resource_idx;
3213 context_gl->blit_targets[i].layer_count = rtv_gl->v.layer_count;
3215 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3216 rt_mask |= (1u << i);
3219 if (dsv)
3221 struct wined3d_rendertarget_view_gl *dsv_gl = wined3d_rendertarget_view_gl(dsv);
3222 ds_info.gl_view = dsv_gl->gl_view;
3223 ds_info.resource = dsv_gl->v.resource;
3224 ds_info.sub_resource_idx = dsv_gl->v.sub_resource_idx;
3225 ds_info.layer_count = dsv_gl->v.layer_count;
3228 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3229 rt_count ? rts[0]->resource->draw_binding : 0, dsv ? dsv->resource->draw_binding : 0);
3231 else
3233 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3234 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3235 rt_mask = context_generate_rt_mask_from_resource(rts[0]->resource);
3238 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3239 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3240 * state management allows this */
3241 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3243 else
3245 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3248 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3249 && (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource)))
3251 for (i = 0; i < rt_count; ++i)
3253 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3254 rt_mask |= (1u << i);
3257 else
3259 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3262 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3264 if (rt_mask != *cur_mask)
3266 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3267 *cur_mask = rt_mask;
3268 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3271 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3272 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3274 context_gl->c.last_was_blit = FALSE;
3275 context_gl->c.last_was_ffp_blit = FALSE;
3277 /* Blending and clearing should be orthogonal, but tests on the nvidia
3278 * driver show that disabling blending when clearing improves the clearing
3279 * performance incredibly. */
3280 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3281 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
3282 if (rt_count && gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3284 if (needs_srgb_write(context_gl->c.d3d_info, state, fb))
3285 gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
3286 else
3287 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3288 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3290 checkGLcall("setting up state for clear");
3292 context_invalidate_state(&context_gl->c, STATE_BLEND);
3293 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3294 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
3296 return TRUE;
3299 static uint32_t find_draw_buffers_mask(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3301 struct wined3d_rendertarget_view * const *rts = state->fb.render_targets;
3302 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3303 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3304 unsigned int rt_mask, mask;
3305 unsigned int i;
3307 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3308 return wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rts[0]->resource);
3309 else if (!context_gl->c.render_offscreen)
3310 return context_generate_rt_mask_from_resource(rts[0]->resource);
3312 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
3313 rt_mask &= (1u << gl_info->limits.buffers) - 1;
3314 if (state->blend_state && state->blend_state->dual_source)
3315 rt_mask = 1;
3317 mask = rt_mask;
3318 while (mask)
3320 i = wined3d_bit_scan(&mask);
3321 if (!rts[i] || rts[i]->format->id == WINED3DFMT_NULL)
3322 rt_mask &= ~(1u << i);
3325 return rt_mask;
3328 /* Context activation is done by the caller. */
3329 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3331 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3332 uint32_t rt_mask = find_draw_buffers_mask(context_gl, state);
3333 const struct wined3d_fb_state *fb = &state->fb;
3334 DWORD color_location = 0;
3335 DWORD *cur_mask;
3337 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3339 struct wined3d_rendertarget_info ds_info = {{0}};
3341 if (!context->render_offscreen)
3343 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3344 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3346 else
3348 const struct wined3d_rendertarget_view_gl *view_gl;
3349 unsigned int i;
3351 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3352 for (i = 0; i < context_gl->gl_info->limits.buffers; ++i)
3354 if (!fb->render_targets[i])
3355 continue;
3357 view_gl = wined3d_rendertarget_view_gl(fb->render_targets[i]);
3358 context_gl->blit_targets[i].gl_view = view_gl->gl_view;
3359 context_gl->blit_targets[i].resource = view_gl->v.resource;
3360 context_gl->blit_targets[i].sub_resource_idx = view_gl->v.sub_resource_idx;
3361 context_gl->blit_targets[i].layer_count = view_gl->v.layer_count;
3363 if (!color_location)
3364 color_location = view_gl->v.resource->draw_binding;
3367 if (fb->depth_stencil)
3369 view_gl = wined3d_rendertarget_view_gl(fb->depth_stencil);
3370 ds_info.gl_view = view_gl->gl_view;
3371 ds_info.resource = view_gl->v.resource;
3372 ds_info.sub_resource_idx = view_gl->v.sub_resource_idx;
3373 ds_info.layer_count = view_gl->v.layer_count;
3376 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3377 color_location, fb->depth_stencil ? fb->depth_stencil->resource->draw_binding : 0);
3381 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3382 if (rt_mask != *cur_mask)
3384 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3385 *cur_mask = rt_mask;
3387 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
3390 static void wined3d_context_gl_map_stage(struct wined3d_context_gl *context_gl, unsigned int stage, unsigned int unit)
3392 unsigned int i = context_gl->rev_tex_unit_map[unit];
3393 unsigned int j = context_gl->tex_unit_map[stage];
3395 TRACE("Mapping stage %u to unit %u.\n", stage, unit);
3396 context_gl->tex_unit_map[stage] = unit;
3397 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
3398 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
3400 context_gl->rev_tex_unit_map[unit] = stage;
3401 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
3402 context_gl->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
3405 static void context_invalidate_texture_stage(struct wined3d_context *context, DWORD stage)
3407 DWORD i;
3409 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
3410 context_invalidate_state(context, STATE_TEXTURESTAGE(stage, i));
3413 static void context_update_fixed_function_usage_map(struct wined3d_context *context,
3414 const struct wined3d_state *state)
3416 UINT i, start, end;
3418 context->fixed_function_usage_map = 0;
3419 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
3421 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
3422 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
3423 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
3424 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
3425 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
3426 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
3427 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
3428 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
3430 /* Not used, and disable higher stages. */
3431 if (color_op == WINED3D_TOP_DISABLE)
3432 break;
3434 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
3435 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
3436 || ((color_arg3 == WINED3DTA_TEXTURE)
3437 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
3438 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
3439 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
3440 || ((alpha_arg3 == WINED3DTA_TEXTURE)
3441 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
3442 context->fixed_function_usage_map |= (1u << i);
3444 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
3445 && i < WINED3D_MAX_TEXTURES - 1)
3446 context->fixed_function_usage_map |= (1u << (i + 1));
3449 if (i < context->lowest_disabled_stage)
3451 start = i;
3452 end = context->lowest_disabled_stage;
3454 else
3456 start = context->lowest_disabled_stage;
3457 end = i;
3460 context->lowest_disabled_stage = i;
3461 for (i = start + 1; i < end; ++i)
3463 context_invalidate_state(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3467 static void wined3d_context_gl_map_fixed_function_samplers(struct wined3d_context_gl *context_gl,
3468 const struct wined3d_state *state)
3470 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3471 unsigned int i, tex;
3472 WORD ffu_map;
3474 ffu_map = context_gl->c.fixed_function_usage_map;
3476 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
3477 || context_gl->c.lowest_disabled_stage <= d3d_info->limits.ffp_textures)
3479 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3481 if (!(ffu_map & 1))
3482 continue;
3484 if (context_gl->tex_unit_map[i] != i)
3486 wined3d_context_gl_map_stage(context_gl, i, i);
3487 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3488 context_invalidate_texture_stage(&context_gl->c, i);
3491 return;
3494 /* Now work out the mapping */
3495 tex = 0;
3496 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3498 if (!(ffu_map & 1))
3499 continue;
3501 if (context_gl->tex_unit_map[i] != tex)
3503 wined3d_context_gl_map_stage(context_gl, i, tex);
3504 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3505 context_invalidate_texture_stage(&context_gl->c, i);
3508 ++tex;
3512 static void wined3d_context_gl_map_psamplers(struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3514 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3515 const struct wined3d_shader_resource_info *resource_info =
3516 state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3517 unsigned int i;
3519 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
3521 if (resource_info[i].type && context_gl->tex_unit_map[i] != i)
3523 wined3d_context_gl_map_stage(context_gl, i, i);
3524 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3525 if (i < d3d_info->limits.ffp_blend_stages)
3526 context_invalidate_texture_stage(&context_gl->c, i);
3531 static BOOL wined3d_context_gl_unit_free_for_vs(const struct wined3d_context_gl *context_gl,
3532 const struct wined3d_shader_resource_info *ps_resource_info, unsigned int unit)
3534 unsigned int current_mapping = context_gl->rev_tex_unit_map[unit];
3536 /* Not currently used */
3537 if (current_mapping == WINED3D_UNMAPPED_STAGE)
3538 return TRUE;
3540 if (current_mapping < WINED3D_MAX_FRAGMENT_SAMPLERS)
3542 /* Used by a fragment sampler */
3544 if (!ps_resource_info)
3546 /* No pixel shader, check fixed function */
3547 return current_mapping >= WINED3D_MAX_TEXTURES
3548 || !(context_gl->c.fixed_function_usage_map & (1u << current_mapping));
3551 /* Pixel shader, check the shader's sampler map */
3552 return !ps_resource_info[current_mapping].type;
3555 return TRUE;
3558 static void wined3d_context_gl_map_vsamplers(struct wined3d_context_gl *context_gl,
3559 BOOL ps, const struct wined3d_state *state)
3561 const struct wined3d_shader_resource_info *vs_resource_info =
3562 state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info;
3563 const struct wined3d_shader_resource_info *ps_resource_info = NULL;
3564 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3565 int start = min(WINED3D_MAX_COMBINED_SAMPLERS, gl_info->limits.graphics_samplers) - 1;
3566 int i;
3568 /* Note that we only care if a resource is used or not, not the
3569 * resource's specific type. Otherwise we'd need to call
3570 * shader_update_samplers() here for 1.x pixelshaders. */
3571 if (ps)
3572 ps_resource_info = state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3574 for (i = 0; i < WINED3D_MAX_VERTEX_SAMPLERS; ++i)
3576 DWORD vsampler_idx = i + WINED3D_MAX_FRAGMENT_SAMPLERS;
3577 if (vs_resource_info[i].type)
3579 while (start >= 0)
3581 if (wined3d_context_gl_unit_free_for_vs(context_gl, ps_resource_info, start))
3583 if (context_gl->tex_unit_map[vsampler_idx] != start)
3585 wined3d_context_gl_map_stage(context_gl, vsampler_idx, start);
3586 context_invalidate_state(&context_gl->c, STATE_SAMPLER(vsampler_idx));
3589 --start;
3590 break;
3593 --start;
3595 if (context_gl->tex_unit_map[vsampler_idx] == WINED3D_UNMAPPED_STAGE)
3596 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i);
3601 static void wined3d_context_gl_update_tex_unit_map(struct wined3d_context_gl *context_gl,
3602 const struct wined3d_state *state)
3604 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3605 BOOL vs = use_vs(state);
3606 BOOL ps = use_ps(state);
3608 if (!ps)
3609 context_update_fixed_function_usage_map(&context_gl->c, state);
3611 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3612 * need a 1:1 map at the moment.
3613 * When the mapping of a stage is changed, sampler and ALL texture stage
3614 * states have to be reset. */
3616 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
3617 return;
3619 if (ps)
3620 wined3d_context_gl_map_psamplers(context_gl, state);
3621 else
3622 wined3d_context_gl_map_fixed_function_samplers(context_gl, state);
3624 if (vs)
3625 wined3d_context_gl_map_vsamplers(context_gl, ps, state);
3628 /* Context activation is done by the caller. */
3629 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3631 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3632 uint32_t rt_mask, *cur_mask;
3634 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
3636 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3637 rt_mask = find_draw_buffers_mask(context_gl, state);
3638 if (rt_mask != *cur_mask)
3640 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3641 *cur_mask = rt_mask;
3645 static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl *context_gl,
3646 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
3648 unsigned int bind_idx, shader_sampler_count, base, count, i;
3649 const struct wined3d_device *device = context_gl->c.device;
3650 struct wined3d_shader_sampler_map_entry *entry;
3651 struct wined3d_shader_resource_view *view;
3652 const struct wined3d_shader *shader;
3653 const unsigned int *tex_unit_map;
3654 struct wined3d_sampler *sampler;
3656 if (!(shader = state->shader[shader_type]))
3657 return;
3659 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl,
3660 &shader->reg_maps.shader_version, &base, &count);
3662 shader_sampler_count = shader->reg_maps.sampler_map.count;
3663 if (shader_sampler_count > count)
3664 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3665 shader, shader_sampler_count, count);
3666 count = min(shader_sampler_count, count);
3668 for (i = 0; i < count; ++i)
3670 entry = &shader->reg_maps.sampler_map.entries[i];
3671 bind_idx = base + entry->bind_idx;
3672 if (tex_unit_map)
3673 bind_idx = tex_unit_map[bind_idx];
3675 if (!(view = state->shader_resource_view[shader_type][entry->resource_idx]))
3677 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
3678 continue;
3681 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
3682 sampler = device->default_sampler;
3683 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
3684 sampler = device->null_sampler;
3685 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view),
3686 bind_idx, wined3d_sampler_gl(sampler), context_gl);
3690 static void wined3d_context_gl_bind_unordered_access_views(struct wined3d_context_gl *context_gl,
3691 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3693 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3694 struct wined3d_unordered_access_view_gl *view_gl;
3695 const struct wined3d_format_gl *format_gl;
3696 GLuint texture_name;
3697 unsigned int i;
3698 GLint level;
3700 if (!shader)
3701 return;
3703 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3705 if (!views[i])
3707 if (shader->reg_maps.uav_resource_info[i].type)
3708 WARN("No unordered access view bound at index %u.\n", i);
3709 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3710 continue;
3713 view_gl = wined3d_unordered_access_view_gl(views[i]);
3714 if (view_gl->gl_view.name)
3716 texture_name = view_gl->gl_view.name;
3717 level = 0;
3719 else if (view_gl->v.resource->type != WINED3D_RTYPE_BUFFER)
3721 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(view_gl->v.resource));
3722 texture_name = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, FALSE);
3723 level = view_gl->v.desc.u.texture.level_idx;
3725 else
3727 FIXME("Unsupported buffer unordered access view.\n");
3728 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3729 continue;
3732 format_gl = wined3d_format_gl(view_gl->v.format);
3733 GL_EXTCALL(glBindImageTexture(i, texture_name, level, GL_TRUE, 0, GL_READ_WRITE,
3734 format_gl->internal));
3736 if (view_gl->counter_bo.id)
3737 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, i, view_gl->counter_bo.id));
3739 checkGLcall("Bind unordered access views");
3742 static void context_gl_load_shader_resources(struct wined3d_context_gl *context_gl,
3743 const struct wined3d_state *state, unsigned int shader_mask)
3745 struct wined3d_shader_sampler_map_entry *entry;
3746 struct wined3d_shader_resource_view_gl *srv_gl;
3747 struct wined3d_shader_resource_view *view;
3748 struct wined3d_buffer_gl *buffer_gl;
3749 struct wined3d_shader *shader;
3750 unsigned int i, j;
3752 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
3754 if (!(shader_mask & (1u << i)))
3755 continue;
3757 if (!(shader = state->shader[i]))
3758 continue;
3760 for (j = 0; j < WINED3D_MAX_CBS; ++j)
3762 if (!state->cb[i][j])
3763 continue;
3765 buffer_gl = wined3d_buffer_gl(state->cb[i][j]);
3766 wined3d_buffer_load(&buffer_gl->b, &context_gl->c, state);
3767 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3768 if (!buffer_gl->bo_user.valid)
3769 device_invalidate_state(context_gl->c.device, STATE_CONSTANT_BUFFER(i));
3772 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
3774 entry = &shader->reg_maps.sampler_map.entries[j];
3776 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
3777 continue;
3779 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3781 buffer_gl = wined3d_buffer_gl(buffer_from_resource(view->resource));
3782 wined3d_buffer_load(&buffer_gl->b, &context_gl->c, state);
3783 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3785 srv_gl = wined3d_shader_resource_view_gl(view);
3786 if (!srv_gl->bo_user.valid)
3787 wined3d_shader_resource_view_gl_update(srv_gl, context_gl);
3789 else
3791 wined3d_texture_load(texture_from_resource(view->resource), &context_gl->c, FALSE);
3797 static void context_gl_load_unordered_access_resources(struct wined3d_context_gl *context_gl,
3798 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3800 struct wined3d_unordered_access_view_gl *uav_gl;
3801 struct wined3d_unordered_access_view *view;
3802 struct wined3d_buffer_gl *buffer_gl;
3803 struct wined3d_texture *texture;
3804 unsigned int i;
3806 context_gl->c.uses_uavs = 0;
3808 if (!shader)
3809 return;
3811 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3813 if (!(view = views[i]))
3814 continue;
3816 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3818 buffer_gl = wined3d_buffer_gl(buffer_from_resource(view->resource));
3819 wined3d_buffer_load_location(&buffer_gl->b, &context_gl->c, WINED3D_LOCATION_BUFFER);
3820 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
3821 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3823 uav_gl = wined3d_unordered_access_view_gl(view);
3824 if (!uav_gl->bo_user.valid)
3825 wined3d_unordered_access_view_gl_update(uav_gl, context_gl);
3827 else
3829 texture = texture_from_resource(view->resource);
3830 wined3d_texture_load(texture, &context_gl->c, FALSE);
3831 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_TEXTURE_RGB);
3834 context_gl->c.uses_uavs = 1;
3838 static void context_gl_load_stream_output_buffers(struct wined3d_context_gl *context_gl,
3839 const struct wined3d_state *state)
3841 unsigned int i;
3843 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3845 struct wined3d_buffer_gl *buffer_gl;
3847 if (!state->stream_output[i].buffer)
3848 continue;
3850 buffer_gl = wined3d_buffer_gl(state->stream_output[i].buffer);
3851 wined3d_buffer_load(&buffer_gl->b, &context_gl->c, state);
3852 wined3d_buffer_invalidate_location(&buffer_gl->b, ~WINED3D_LOCATION_BUFFER);
3853 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3854 if (!buffer_gl->bo_user.valid)
3855 device_invalidate_state(context_gl->c.device, STATE_STREAM_OUTPUT);
3859 /* Context activation is done by the caller. */
3860 static BOOL context_apply_draw_state(struct wined3d_context *context,
3861 const struct wined3d_device *device, const struct wined3d_state *state, BOOL indexed)
3863 const struct wined3d_state_entry *state_table = context->state_table;
3864 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3865 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3866 const struct wined3d_fb_state *fb = &state->fb;
3867 unsigned int i, base;
3868 uint32_t map;
3870 context->uses_fbo_attached_resources = 0;
3872 if (!have_framebuffer_attachment(gl_info->limits.buffers, fb->render_targets, fb->depth_stencil))
3874 if (!gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
3876 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
3877 return FALSE;
3880 wined3d_context_gl_set_render_offscreen(context_gl, TRUE);
3883 /* Preload resources before FBO setup. Texture preload in particular may
3884 * result in changes to the current FBO, due to using e.g. FBO blits for
3885 * updating a resource location. */
3886 wined3d_context_gl_update_tex_unit_map(context_gl, state);
3887 context_preload_textures(context, state);
3888 context_gl_load_shader_resources(context_gl, state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
3889 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_PIXEL],
3890 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3891 context_gl_load_stream_output_buffers(context_gl, state);
3892 /* TODO: Right now the dependency on the vertex shader is necessary
3893 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3894 * the current VS but maybe it's possible to relax the coupling in some
3895 * situations at least. */
3896 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC)
3897 || isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
3899 context_update_stream_info(context, state);
3902 map = context->stream_info.use_map;
3903 while (map)
3905 const struct wined3d_stream_info_element *e;
3906 struct wined3d_buffer_gl *buffer_gl;
3908 e = &context->stream_info.elements[wined3d_bit_scan(&map)];
3909 buffer_gl = wined3d_buffer_gl(state->streams[e->stream_idx].buffer);
3911 if (!buffer_gl->bo_user.valid)
3912 device_invalidate_state(device, STATE_STREAMSRC);
3913 else
3914 wined3d_buffer_load(&buffer_gl->b, context, state);
3915 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3917 /* Loading the buffers above may have invalidated the stream info. */
3918 if (wined3d_context_is_graphics_state_dirty(context, STATE_STREAMSRC))
3919 context_update_stream_info(context, state);
3921 if (indexed && state->index_buffer)
3923 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(state->index_buffer);
3925 if (context->stream_info.all_vbo)
3927 wined3d_buffer_load(&buffer_gl->b, context, state);
3928 if (!buffer_gl->bo_user.valid)
3929 device_invalidate_state(device, STATE_INDEXBUFFER);
3930 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
3932 else
3934 wined3d_buffer_load_sysmem(&buffer_gl->b, context);
3938 for (i = 0, base = 0; i < ARRAY_SIZE(context->dirty_graphics_states); ++i)
3940 uint32_t dirty_mask = context->dirty_graphics_states[i];
3942 while (dirty_mask)
3944 unsigned int state_id = base + wined3d_bit_scan(&dirty_mask);
3946 state_table[state_id].apply(context, state, state_id);
3947 context->dirty_graphics_states[i] &= ~(1u << (state_id - base));
3949 base += sizeof(dirty_mask) * CHAR_BIT;
3952 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
3954 device->shader_backend->shader_select(device->shader_priv, context, state);
3955 context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3958 if (context->constant_update_mask)
3960 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
3961 context->constant_update_mask = 0;
3964 if (context->update_shader_resource_bindings)
3966 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
3967 wined3d_context_gl_bind_shader_resources(context_gl, state, i);
3968 context->update_shader_resource_bindings = 0;
3969 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
3970 context->update_compute_shader_resource_bindings = 1;
3973 if (context->update_unordered_access_view_bindings)
3975 wined3d_context_gl_bind_unordered_access_views(context_gl,
3976 state->shader[WINED3D_SHADER_TYPE_PIXEL],
3977 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3978 context->update_unordered_access_view_bindings = 0;
3979 context->update_compute_unordered_access_view_bindings = 1;
3982 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3983 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3985 context->last_was_blit = FALSE;
3986 context->last_was_ffp_blit = FALSE;
3988 return TRUE;
3991 static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl *context_gl,
3992 const struct wined3d_device *device, const struct wined3d_state *state)
3994 const struct wined3d_state_entry *state_table = context_gl->c.state_table;
3995 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3996 unsigned int state_id, i;
3998 context_gl_load_shader_resources(context_gl, state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
3999 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4000 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4002 for (i = 0, state_id = STATE_COMPUTE_OFFSET; i < ARRAY_SIZE(context_gl->c.dirty_compute_states); ++i)
4004 unsigned int dirty_mask = context_gl->c.dirty_compute_states[i];
4006 while (dirty_mask)
4008 unsigned int current_state_id = state_id + wined3d_bit_scan(&dirty_mask);
4009 state_table[current_state_id].apply(&context_gl->c, state, current_state_id);
4011 state_id += sizeof(*context_gl->c.dirty_compute_states) * CHAR_BIT;
4013 memset(context_gl->c.dirty_compute_states, 0, sizeof(*context_gl->c.dirty_compute_states));
4015 if (context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
4017 device->shader_backend->shader_select_compute(device->shader_priv, &context_gl->c, state);
4018 context_gl->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
4021 if (context_gl->c.update_compute_shader_resource_bindings)
4023 wined3d_context_gl_bind_shader_resources(context_gl, state, WINED3D_SHADER_TYPE_COMPUTE);
4024 context_gl->c.update_compute_shader_resource_bindings = 0;
4025 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4026 context_gl->c.update_shader_resource_bindings = 1;
4029 if (context_gl->c.update_compute_unordered_access_view_bindings)
4031 wined3d_context_gl_bind_unordered_access_views(context_gl,
4032 state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4033 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4034 context_gl->c.update_compute_unordered_access_view_bindings = 0;
4035 context_gl->c.update_unordered_access_view_bindings = 1;
4038 /* Updates to currently bound render targets aren't necessarily coherent
4039 * between the graphics and compute pipelines. Unbind any currently bound
4040 * FBO here to ensure preceding updates to its attachments by the graphics
4041 * pipeline are visible to the compute pipeline.
4043 * Without this, the bloom effect in Nier:Automata is too bright on the
4044 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4045 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
4046 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
4048 context_gl->c.last_was_blit = FALSE;
4049 context_gl->c.last_was_ffp_blit = FALSE;
4052 void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl *context_gl)
4054 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4056 if (context_gl->c.transform_feedback_active)
4058 GL_EXTCALL(glEndTransformFeedback());
4059 checkGLcall("glEndTransformFeedback");
4060 context_gl->c.transform_feedback_active = 0;
4061 context_gl->c.transform_feedback_paused = 0;
4065 static void wined3d_context_gl_pause_transform_feedback(struct wined3d_context_gl *context_gl, BOOL force)
4067 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4069 if (!context_gl->c.transform_feedback_active || context_gl->c.transform_feedback_paused)
4070 return;
4072 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK2])
4074 GL_EXTCALL(glPauseTransformFeedback());
4075 checkGLcall("glPauseTransformFeedback");
4076 context_gl->c.transform_feedback_paused = 1;
4077 return;
4080 WARN("Cannot pause transform feedback operations.\n");
4082 if (force)
4083 wined3d_context_gl_end_transform_feedback(context_gl);
4086 static void wined3d_context_gl_setup_target(struct wined3d_context_gl *context_gl,
4087 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4089 BOOL old_render_offscreen = context_gl->c.render_offscreen, render_offscreen;
4091 render_offscreen = wined3d_resource_is_offscreen(&texture->resource);
4092 if (context_gl->c.current_rt.texture == texture
4093 && context_gl->c.current_rt.sub_resource_idx == sub_resource_idx
4094 && render_offscreen == old_render_offscreen)
4095 return;
4097 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4098 * the alpha blend state changes with different render target formats. */
4099 if (!context_gl->c.current_rt.texture)
4101 context_invalidate_state(&context_gl->c, STATE_BLEND);
4103 else
4105 const struct wined3d_format *old = context_gl->c.current_rt.texture->resource.format;
4106 const struct wined3d_format *new = texture->resource.format;
4108 if (old->id != new->id)
4110 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4111 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
4112 || !(texture->resource.format_flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
4113 context_invalidate_state(&context_gl->c, STATE_BLEND);
4116 /* When switching away from an offscreen render target, and we're not
4117 * using FBOs, we have to read the drawable into the texture. This is
4118 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4119 * There are some things that need care though. PreLoad needs a GL context,
4120 * and FindContext is called before the context is activated. It also
4121 * has to be called with the old rendertarget active, otherwise a
4122 * wrong drawable is read. */
4123 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4124 && old_render_offscreen && (context_gl->c.current_rt.texture != texture
4125 || context_gl->c.current_rt.sub_resource_idx != sub_resource_idx))
4127 struct wined3d_texture_gl *prev_texture = wined3d_texture_gl(context_gl->c.current_rt.texture);
4128 unsigned int prev_sub_resource_idx = context_gl->c.current_rt.sub_resource_idx;
4130 /* Read the back buffer of the old drawable into the destination texture. */
4131 if (prev_texture->texture_srgb.name)
4132 wined3d_texture_load(&prev_texture->t, &context_gl->c, TRUE);
4133 wined3d_texture_load(&prev_texture->t, &context_gl->c, FALSE);
4134 wined3d_texture_invalidate_location(&prev_texture->t, prev_sub_resource_idx, WINED3D_LOCATION_DRAWABLE);
4138 context_gl->c.current_rt.texture = texture;
4139 context_gl->c.current_rt.sub_resource_idx = sub_resource_idx;
4140 wined3d_context_gl_set_render_offscreen(context_gl, render_offscreen);
4143 static void wined3d_context_gl_activate(struct wined3d_context_gl *context_gl,
4144 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4146 wined3d_context_gl_enter(context_gl);
4147 wined3d_context_gl_update_window(context_gl);
4148 wined3d_context_gl_setup_target(context_gl, texture, sub_resource_idx);
4149 if (!context_gl->valid)
4150 return;
4152 if (context_gl != wined3d_context_gl_get_current())
4154 if (!wined3d_context_gl_set_current(context_gl))
4155 ERR("Failed to activate the new context.\n");
4157 else if (context_gl->needs_set)
4159 wined3d_context_gl_set_gl_context(context_gl);
4163 struct wined3d_context *wined3d_context_gl_acquire(const struct wined3d_device *device,
4164 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4166 struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
4167 struct wined3d_context_gl *context_gl;
4169 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device, texture, sub_resource_idx);
4171 if (current_context && current_context->c.destroyed)
4172 current_context = NULL;
4174 if (!texture)
4176 if (current_context
4177 && current_context->c.current_rt.texture
4178 && current_context->c.device == device)
4180 texture = current_context->c.current_rt.texture;
4181 sub_resource_idx = current_context->c.current_rt.sub_resource_idx;
4183 else
4185 struct wined3d_swapchain *swapchain = device->swapchains[0];
4187 if (swapchain->back_buffers)
4188 texture = swapchain->back_buffers[0];
4189 else
4190 texture = swapchain->front_buffer;
4191 sub_resource_idx = 0;
4195 if (current_context && current_context->c.current_rt.texture == texture)
4197 context_gl = current_context;
4199 else if (!wined3d_resource_is_offscreen(&texture->resource))
4201 TRACE("Rendering onscreen.\n");
4203 if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(texture->swapchain))))
4204 return NULL;
4206 else
4208 TRACE("Rendering offscreen.\n");
4210 /* Stay with the current context if possible. Otherwise use the
4211 * context for the primary swapchain. */
4212 if (current_context && current_context->c.device == device)
4213 context_gl = current_context;
4214 else if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(device->swapchains[0]))))
4215 return NULL;
4218 wined3d_context_gl_activate(context_gl, texture, sub_resource_idx);
4220 return &context_gl->c;
4223 struct wined3d_context_gl *wined3d_context_gl_reacquire(struct wined3d_context_gl *context_gl)
4225 struct wined3d_context *acquired_context;
4226 struct wined3d_device *device;
4228 if (!context_gl || context_gl->tid != GetCurrentThreadId())
4229 return NULL;
4231 device = context_gl->c.device;
4232 wined3d_from_cs(device->cs);
4234 if (context_gl->c.current_rt.texture)
4236 wined3d_context_gl_activate(context_gl, context_gl->c.current_rt.texture,
4237 context_gl->c.current_rt.sub_resource_idx);
4238 return context_gl;
4241 acquired_context = context_acquire(device, NULL, 0);
4242 if (acquired_context != &context_gl->c)
4243 ERR("Acquired context %p instead of %p.\n", acquired_context, &context_gl->c);
4244 return wined3d_context_gl(acquired_context);
4247 void dispatch_compute(struct wined3d_device *device, const struct wined3d_state *state,
4248 const struct wined3d_dispatch_parameters *parameters)
4250 const struct wined3d_gl_info *gl_info;
4251 struct wined3d_context_gl *context_gl;
4253 context_gl = wined3d_context_gl(context_acquire(device, NULL, 0));
4254 if (!context_gl->valid)
4256 context_release(&context_gl->c);
4257 WARN("Invalid context, skipping dispatch.\n");
4258 return;
4260 gl_info = context_gl->gl_info;
4262 if (!gl_info->supported[ARB_COMPUTE_SHADER])
4264 context_release(&context_gl->c);
4265 FIXME("OpenGL implementation does not support compute shaders.\n");
4266 return;
4269 if (parameters->indirect)
4270 wined3d_buffer_load(parameters->u.indirect.buffer, &context_gl->c, state);
4272 wined3d_context_gl_apply_compute_state(context_gl, device, state);
4274 if (parameters->indirect)
4276 const struct wined3d_indirect_dispatch_parameters *indirect = &parameters->u.indirect;
4277 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(indirect->buffer);
4279 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, buffer_gl->bo.id));
4280 GL_EXTCALL(glDispatchComputeIndirect((GLintptr)indirect->offset));
4281 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0));
4282 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
4284 else
4286 const struct wined3d_direct_dispatch_parameters *direct = &parameters->u.direct;
4287 GL_EXTCALL(glDispatchCompute(direct->group_count_x, direct->group_count_y, direct->group_count_z));
4289 checkGLcall("dispatch compute");
4291 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4292 checkGLcall("glMemoryBarrier");
4294 context_release(&context_gl->c);
4297 /* Context activation is done by the caller. */
4298 static void wined3d_context_gl_draw_primitive_arrays(struct wined3d_context_gl *context_gl,
4299 const struct wined3d_state *state, const void *idx_data, unsigned int idx_size, int base_vertex_idx,
4300 unsigned int start_idx, unsigned int count, unsigned int start_instance, unsigned int instance_count)
4302 const struct wined3d_ffp_attrib_ops *ops = &context_gl->c.d3d_info->ffp_attrib_ops;
4303 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4304 const struct wined3d_stream_info *si = &context_gl->c.stream_info;
4305 GLenum mode = gl_primitive_type_from_d3d(state->primitive_type);
4306 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4307 unsigned int instanced_elements[ARRAY_SIZE(si->elements)];
4308 unsigned int instanced_element_count = 0;
4309 const void *indices;
4310 unsigned int i, j;
4312 indices = (const char *)idx_data + idx_size * start_idx;
4314 if (!instance_count)
4316 if (!idx_size)
4318 gl_info->gl_ops.gl.p_glDrawArrays(mode, start_idx, count);
4319 checkGLcall("glDrawArrays");
4320 return;
4323 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4325 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4326 checkGLcall("glDrawElementsBaseVertex");
4327 return;
4330 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4331 checkGLcall("glDrawElements");
4332 return;
4335 if (start_instance && !(gl_info->supported[ARB_BASE_INSTANCE] && gl_info->supported[ARB_INSTANCED_ARRAYS]))
4336 FIXME("Start instance (%u) not supported.\n", start_instance);
4338 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
4340 if (!idx_size)
4342 if (gl_info->supported[ARB_BASE_INSTANCE])
4344 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode, start_idx, count, instance_count, start_instance));
4345 checkGLcall("glDrawArraysInstancedBaseInstance");
4346 return;
4349 GL_EXTCALL(glDrawArraysInstanced(mode, start_idx, count, instance_count));
4350 checkGLcall("glDrawArraysInstanced");
4351 return;
4354 if (gl_info->supported[ARB_BASE_INSTANCE])
4356 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode, count, idx_type,
4357 indices, instance_count, base_vertex_idx, start_instance));
4358 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4359 return;
4361 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4363 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode, count, idx_type,
4364 indices, instance_count, base_vertex_idx));
4365 checkGLcall("glDrawElementsInstancedBaseVertex");
4366 return;
4369 GL_EXTCALL(glDrawElementsInstanced(mode, count, idx_type, indices, instance_count));
4370 checkGLcall("glDrawElementsInstanced");
4371 return;
4374 /* Instancing emulation by mixing immediate mode and arrays. */
4376 /* This is a nasty thing. MSDN says no hardware supports this and
4377 * applications have to use software vertex processing. We don't support
4378 * this for now.
4380 * Shouldn't be too hard to support with OpenGL, in theory just call
4381 * glDrawArrays() instead of drawElements(). But the stream frequency value
4382 * has a different meaning in that situation. */
4383 if (!idx_size)
4385 FIXME("Non-indexed instanced drawing is not supported.\n");
4386 return;
4389 for (i = 0; i < ARRAY_SIZE(si->elements); ++i)
4391 if (!(si->use_map & (1u << i)))
4392 continue;
4394 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
4395 instanced_elements[instanced_element_count++] = i;
4398 for (i = 0; i < instance_count; ++i)
4400 /* Specify the instanced attributes using immediate mode calls. */
4401 for (j = 0; j < instanced_element_count; ++j)
4403 const struct wined3d_stream_info_element *element;
4404 unsigned int element_idx;
4405 const BYTE *ptr;
4407 element_idx = instanced_elements[j];
4408 element = &si->elements[element_idx];
4409 ptr = element->data.addr + element->stride * i;
4410 if (element->data.buffer_object)
4411 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(state->streams[element->stream_idx].buffer,
4412 &context_gl->c);
4413 ops->generic[element->format->emit_idx](element_idx, ptr);
4416 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4418 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4419 checkGLcall("glDrawElementsBaseVertex");
4421 else
4423 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4424 checkGLcall("glDrawElements");
4429 static unsigned int get_stride_idx(const void *idx_data, unsigned int idx_size,
4430 unsigned int base_vertex_idx, unsigned int start_idx, unsigned int vertex_idx)
4432 if (!idx_data)
4433 return start_idx + vertex_idx;
4434 if (idx_size == 2)
4435 return ((const WORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4436 return ((const DWORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4439 /* Context activation is done by the caller. */
4440 static void draw_primitive_immediate_mode(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4441 const struct wined3d_stream_info *si, const void *idx_data, unsigned int idx_size,
4442 int base_vertex_idx, unsigned int start_idx, unsigned int vertex_count, unsigned int instance_count)
4444 const BYTE *position = NULL, *normal = NULL, *diffuse = NULL, *specular = NULL;
4445 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
4446 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4447 unsigned int coord_idx, stride_idx, texture_idx, vertex_idx;
4448 const struct wined3d_stream_info_element *element;
4449 const BYTE *tex_coords[WINED3DDP_MAXTEXCOORD];
4450 unsigned int texture_unit, texture_stages;
4451 const struct wined3d_ffp_attrib_ops *ops;
4452 unsigned int untracked_material_count;
4453 unsigned int tex_mask = 0;
4454 BOOL specular_fog = FALSE;
4455 BOOL ps = use_ps(state);
4456 const void *ptr;
4458 static unsigned int once;
4460 if (!once++)
4461 FIXME_(d3d_perf)("Drawing using immediate mode.\n");
4462 else
4463 WARN_(d3d_perf)("Drawing using immediate mode.\n");
4465 if (!idx_size && idx_data)
4466 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4468 if (instance_count)
4469 FIXME("Instancing not implemented.\n");
4471 /* Immediate mode drawing can't make use of indices in a VBO - get the
4472 * data from the index buffer. */
4473 if (idx_size)
4474 idx_data = wined3d_buffer_load_sysmem(state->index_buffer, &context_gl->c) + state->index_offset;
4476 ops = &d3d_info->ffp_attrib_ops;
4478 gl_info->gl_ops.gl.p_glBegin(gl_primitive_type_from_d3d(state->primitive_type));
4480 if (use_vs(state) || d3d_info->ffp_generic_attributes)
4482 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4484 unsigned int use_map = si->use_map;
4485 unsigned int element_idx;
4487 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4488 for (element_idx = MAX_ATTRIBS - 1; use_map; use_map &= ~(1u << element_idx), --element_idx)
4490 if (!(use_map & 1u << element_idx))
4491 continue;
4493 ptr = si->elements[element_idx].data.addr + si->elements[element_idx].stride * stride_idx;
4494 ops->generic[si->elements[element_idx].format->emit_idx](element_idx, ptr);
4498 gl_info->gl_ops.gl.p_glEnd();
4499 return;
4502 if (si->use_map & (1u << WINED3D_FFP_POSITION))
4503 position = si->elements[WINED3D_FFP_POSITION].data.addr;
4505 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
4506 normal = si->elements[WINED3D_FFP_NORMAL].data.addr;
4507 else
4508 gl_info->gl_ops.gl.p_glNormal3f(0.0f, 0.0f, 0.0f);
4510 untracked_material_count = context_gl->untracked_material_count;
4511 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
4513 element = &si->elements[WINED3D_FFP_DIFFUSE];
4514 diffuse = element->data.addr;
4516 if (untracked_material_count && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
4517 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element->format->id));
4519 else
4521 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4524 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
4526 element = &si->elements[WINED3D_FFP_SPECULAR];
4527 specular = element->data.addr;
4529 /* Special case where the fog density is stored in the specular alpha channel. */
4530 if (state->render_states[WINED3D_RS_FOGENABLE]
4531 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
4532 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
4533 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
4535 if (gl_info->supported[EXT_FOG_COORD])
4537 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
4538 specular_fog = TRUE;
4539 else
4540 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element->format->id));
4542 else
4544 static unsigned int once;
4546 if (!once++)
4547 FIXME("Implement fog for transformed vertices in software.\n");
4551 else if (gl_info->supported[EXT_SECONDARY_COLOR])
4553 GL_EXTCALL(glSecondaryColor3fEXT)(0.0f, 0.0f, 0.0f);
4556 texture_stages = d3d_info->limits.ffp_blend_stages;
4557 for (texture_idx = 0; texture_idx < texture_stages; ++texture_idx)
4559 if (!gl_info->supported[ARB_MULTITEXTURE] && texture_idx > 0)
4561 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4562 continue;
4565 if (!ps && !state->textures[texture_idx])
4566 continue;
4568 texture_unit = context_gl->tex_unit_map[texture_idx];
4569 if (texture_unit == WINED3D_UNMAPPED_STAGE)
4570 continue;
4572 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4573 if (coord_idx > 7)
4575 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx, texture_idx);
4576 continue;
4579 if (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))
4581 tex_coords[coord_idx] = si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].data.addr;
4582 tex_mask |= (1u << texture_idx);
4584 else
4586 TRACE("Setting default coordinates for texture %u.\n", texture_idx);
4587 if (gl_info->supported[ARB_MULTITEXTURE])
4588 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_unit, 0.0f, 0.0f, 0.0f, 1.0f));
4589 else
4590 gl_info->gl_ops.gl.p_glTexCoord4f(0.0f, 0.0f, 0.0f, 1.0f);
4594 /* Blending data and point sizes are not supported by this function. They
4595 * are not supported by the fixed function pipeline at all. A FIXME for
4596 * them is printed after decoding the vertex declaration. */
4597 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4599 unsigned int tmp_tex_mask;
4601 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4603 if (normal)
4605 ptr = normal + stride_idx * si->elements[WINED3D_FFP_NORMAL].stride;
4606 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptr);
4609 if (diffuse)
4611 ptr = diffuse + stride_idx * si->elements[WINED3D_FFP_DIFFUSE].stride;
4612 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptr);
4614 if (untracked_material_count)
4616 struct wined3d_color color;
4617 unsigned int i;
4619 wined3d_color_from_d3dcolor(&color, *(const DWORD *)ptr);
4620 for (i = 0; i < untracked_material_count; ++i)
4622 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK,
4623 context_gl->untracked_materials[i], &color.r);
4628 if (specular)
4630 ptr = specular + stride_idx * si->elements[WINED3D_FFP_SPECULAR].stride;
4631 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptr);
4633 if (specular_fog)
4634 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD *)ptr >> 24)));
4637 tmp_tex_mask = tex_mask;
4638 for (texture_idx = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture_idx)
4640 if (!(tmp_tex_mask & 1))
4641 continue;
4643 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4644 ptr = tex_coords[coord_idx] + (stride_idx * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
4645 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
4646 GL_TEXTURE0_ARB + context_gl->tex_unit_map[texture_idx], ptr);
4649 if (position)
4651 ptr = position + stride_idx * si->elements[WINED3D_FFP_POSITION].stride;
4652 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptr);
4656 gl_info->gl_ops.gl.p_glEnd();
4657 checkGLcall("draw immediate mode");
4660 static void wined3d_context_gl_draw_indirect(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4661 const struct wined3d_indirect_draw_parameters *parameters, unsigned int idx_size)
4663 GLenum gl_primitive_type = gl_primitive_type_from_d3d(state->primitive_type);
4664 struct wined3d_buffer_gl *buffer_gl = wined3d_buffer_gl(parameters->buffer);
4665 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4666 const void *offset;
4668 if (!gl_info->supported[ARB_DRAW_INDIRECT])
4670 FIXME("OpenGL implementation does not support indirect draws.\n");
4671 return;
4674 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer_gl->bo.id));
4676 offset = (void *)(GLintptr)parameters->offset;
4677 if (idx_size)
4679 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4680 if (state->index_offset)
4681 FIXME("Ignoring index offset %u.\n", state->index_offset);
4682 GL_EXTCALL(glDrawElementsIndirect(gl_primitive_type, idx_type, offset));
4684 else
4686 GL_EXTCALL(glDrawArraysIndirect(gl_primitive_type, offset));
4689 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0));
4690 wined3d_context_gl_reference_bo(context_gl, &buffer_gl->bo);
4692 checkGLcall("draw indirect");
4695 static void remove_vbos(struct wined3d_context *context,
4696 const struct wined3d_state *state, struct wined3d_stream_info *s)
4698 unsigned int i;
4700 for (i = 0; i < ARRAY_SIZE(s->elements); ++i)
4702 struct wined3d_stream_info_element *e;
4704 if (!(s->use_map & (1u << i)))
4705 continue;
4707 e = &s->elements[i];
4708 if (e->data.buffer_object)
4710 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
4711 e->data.buffer_object = 0;
4712 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(vb, context);
4717 static GLenum gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
4719 GLenum gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4720 switch (gl_primitive_type)
4722 case GL_POINTS:
4723 return GL_POINTS;
4725 case GL_LINE_STRIP:
4726 case GL_LINE_STRIP_ADJACENCY:
4727 case GL_LINES_ADJACENCY:
4728 case GL_LINES:
4729 return GL_LINES;
4731 case GL_TRIANGLE_FAN:
4732 case GL_TRIANGLE_STRIP:
4733 case GL_TRIANGLE_STRIP_ADJACENCY:
4734 case GL_TRIANGLES_ADJACENCY:
4735 case GL_TRIANGLES:
4736 return GL_TRIANGLES;
4738 default:
4739 return gl_primitive_type;
4743 /* Routine common to the draw primitive and draw indexed primitive routines */
4744 void draw_primitive(struct wined3d_device *device, const struct wined3d_state *state,
4745 const struct wined3d_draw_parameters *parameters)
4747 BOOL emulation = FALSE, rasterizer_discard = FALSE;
4748 const struct wined3d_fb_state *fb = &state->fb;
4749 const struct wined3d_stream_info *stream_info;
4750 struct wined3d_rendertarget_view *dsv, *rtv;
4751 struct wined3d_stream_info si_emulated;
4752 const struct wined3d_gl_info *gl_info;
4753 struct wined3d_context_gl *context_gl;
4754 struct wined3d_context *context;
4755 unsigned int i, idx_size = 0;
4756 const void *idx_data = NULL;
4758 TRACE("device %p, state %p, parameters %p.\n", device, state, parameters);
4760 if (!parameters->indirect && !parameters->u.direct.index_count)
4761 return;
4763 if (!parameters->indirect)
4764 TRACE("base_vertex_idx %d, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
4765 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
4766 parameters->u.direct.index_count, parameters->u.direct.start_instance,
4767 parameters->u.direct.instance_count);
4769 if (!(rtv = fb->render_targets[0]))
4770 rtv = fb->depth_stencil;
4772 if (rtv && rtv->resource->type == WINED3D_RTYPE_BUFFER)
4774 FIXME("Buffer render targets not implemented.\n");
4775 return;
4778 if (rtv)
4779 context = context_acquire(device, wined3d_texture_from_resource(rtv->resource), rtv->sub_resource_idx);
4780 else
4781 context = context_acquire(device, NULL, 0);
4782 context_gl = wined3d_context_gl(context);
4783 if (!context_gl->valid)
4785 context_release(context);
4786 WARN("Invalid context, skipping draw.\n");
4787 return;
4789 gl_info = context_gl->gl_info;
4791 if (!use_transform_feedback(state))
4792 wined3d_context_gl_pause_transform_feedback(context_gl, TRUE);
4794 for (i = 0; i < gl_info->limits.buffers; ++i)
4796 if (!(rtv = fb->render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
4797 continue;
4799 if (wined3d_blend_state_get_writemask(state->blend_state, i))
4801 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
4802 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
4804 else
4806 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
4810 if ((dsv = fb->depth_stencil))
4812 /* Note that this depends on the context_acquire() call above to set
4813 * context->render_offscreen properly. We don't currently take the
4814 * Z-compare function into account, but we could skip loading the
4815 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
4816 * that we never copy the stencil data.*/
4817 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4819 if (wined3d_state_uses_depth_buffer(state))
4820 wined3d_rendertarget_view_load_location(dsv, context, location);
4821 else
4822 wined3d_rendertarget_view_prepare_location(dsv, context, location);
4825 if (parameters->indirect)
4826 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
4828 if (!context_apply_draw_state(context, device, state, parameters->indexed))
4830 context_release(context);
4831 WARN("Unable to apply draw state, skipping draw.\n");
4832 return;
4835 if (dsv && (!state->depth_stencil_state || state->depth_stencil_state->desc.depth_write))
4837 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4839 wined3d_rendertarget_view_validate_location(dsv, location);
4840 wined3d_rendertarget_view_invalidate_location(dsv, ~location);
4843 stream_info = &context->stream_info;
4845 if (parameters->indexed)
4847 struct wined3d_buffer *index_buffer = state->index_buffer;
4848 if (!index_buffer->buffer_object || !stream_info->all_vbo)
4849 idx_data = index_buffer->resource.heap_memory;
4850 else
4851 idx_data = NULL;
4852 idx_data = (const BYTE *)idx_data + state->index_offset;
4854 if (state->index_format == WINED3DFMT_R16_UINT)
4855 idx_size = 2;
4856 else
4857 idx_size = 4;
4860 if (!use_vs(state))
4862 if (!stream_info->position_transformed && context_gl->untracked_material_count
4863 && state->render_states[WINED3D_RS_LIGHTING])
4865 static BOOL warned;
4867 if (!warned++)
4868 FIXME("Using software emulation because not all material properties could be tracked.\n");
4869 else
4870 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
4871 emulation = TRUE;
4873 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
4875 static BOOL warned;
4877 /* Either write a pipeline replacement shader or convert the
4878 * specular alpha from unsigned byte to a float in the vertex
4879 * buffer. */
4880 if (!warned++)
4881 FIXME("Using software emulation because manual fog coordinates are provided.\n");
4882 else
4883 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
4884 emulation = TRUE;
4887 if (emulation)
4889 si_emulated = context->stream_info;
4890 remove_vbos(context, state, &si_emulated);
4891 stream_info = &si_emulated;
4895 if (use_transform_feedback(state))
4897 const struct wined3d_shader *shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
4899 if (is_rasterization_disabled(shader))
4901 glEnable(GL_RASTERIZER_DISCARD);
4902 checkGLcall("enable rasterizer discard");
4903 rasterizer_discard = TRUE;
4906 if (context->transform_feedback_paused)
4908 GL_EXTCALL(glResumeTransformFeedback());
4909 checkGLcall("glResumeTransformFeedback");
4910 context->transform_feedback_paused = 0;
4912 else if (!context->transform_feedback_active)
4914 enum wined3d_primitive_type primitive_type = shader->u.gs.output_type
4915 ? shader->u.gs.output_type : state->primitive_type;
4916 GLenum mode = gl_tfb_primitive_type_from_d3d(primitive_type);
4917 GL_EXTCALL(glBeginTransformFeedback(mode));
4918 checkGLcall("glBeginTransformFeedback");
4919 context->transform_feedback_active = 1;
4923 if (state->primitive_type == WINED3D_PT_PATCH)
4925 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES, state->patch_vertex_count));
4926 checkGLcall("glPatchParameteri");
4929 if (context->uses_fbo_attached_resources)
4931 static unsigned int fixme_once;
4933 if (gl_info->supported[ARB_TEXTURE_BARRIER])
4935 GL_EXTCALL(glTextureBarrier());
4937 else if (gl_info->supported[NV_TEXTURE_BARRIER])
4939 GL_EXTCALL(glTextureBarrierNV());
4941 else
4943 if (!fixme_once++)
4944 FIXME("Sampling attached render targets is not supported.\n");
4946 WARN("Sampling attached render targets is not supported, skipping draw.\n");
4947 context_release(context);
4948 return;
4950 checkGLcall("glTextureBarrier");
4953 if (parameters->indirect)
4955 if (!context->use_immediate_mode_draw && !emulation)
4956 wined3d_context_gl_draw_indirect(context_gl, state, &parameters->u.indirect, idx_size);
4957 else
4958 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
4960 else
4962 unsigned int instance_count = parameters->u.direct.instance_count;
4963 if (context->instance_count)
4964 instance_count = context->instance_count;
4966 if (context->use_immediate_mode_draw || emulation)
4967 draw_primitive_immediate_mode(wined3d_context_gl(context), state, stream_info, idx_data,
4968 idx_size, parameters->u.direct.base_vertex_idx,
4969 parameters->u.direct.start_idx, parameters->u.direct.index_count, instance_count);
4970 else
4971 wined3d_context_gl_draw_primitive_arrays(context_gl, state, idx_data, idx_size,
4972 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
4973 parameters->u.direct.index_count, parameters->u.direct.start_instance, instance_count);
4976 if (context->uses_uavs)
4978 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4979 checkGLcall("glMemoryBarrier");
4982 wined3d_context_gl_pause_transform_feedback(context_gl, FALSE);
4984 if (rasterizer_discard)
4986 glDisable(GL_RASTERIZER_DISCARD);
4987 checkGLcall("disable rasterizer discard");
4990 context_release(context);
4992 TRACE("Draw completed.\n");
4995 void wined3d_context_gl_unload_tex_coords(const struct wined3d_context_gl *context_gl)
4997 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4998 unsigned int texture_idx;
5000 for (texture_idx = 0; texture_idx < gl_info->limits.texture_coords; ++texture_idx)
5002 gl_info->gl_ops.ext.p_glClientActiveTextureARB(GL_TEXTURE0_ARB + texture_idx);
5003 gl_info->gl_ops.gl.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY);
5007 void wined3d_context_gl_load_tex_coords(const struct wined3d_context_gl *context_gl,
5008 const struct wined3d_stream_info *si, GLuint *current_bo, const struct wined3d_state *state)
5010 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5011 const struct wined3d_format_gl *format_gl;
5012 unsigned int mapped_stage = 0;
5013 unsigned int texture_idx;
5014 GLuint bo;
5016 for (texture_idx = 0; texture_idx < context_gl->c.d3d_info->limits.ffp_blend_stages; ++texture_idx)
5018 unsigned int coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
5020 if ((mapped_stage = context_gl->tex_unit_map[texture_idx]) == WINED3D_UNMAPPED_STAGE)
5021 continue;
5023 if (mapped_stage >= gl_info->limits.texture_coords)
5025 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage);
5026 continue;
5029 if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
5031 const struct wined3d_stream_info_element *e = &si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx];
5033 TRACE("Setting up texture %u, idx %u, coord_idx %u, data %s.\n",
5034 texture_idx, mapped_stage, coord_idx, debug_bo_address(&e->data));
5036 bo = wined3d_bo_gl_id(e->data.buffer_object);
5037 if (*current_bo != bo)
5039 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5040 checkGLcall("glBindBuffer");
5041 *current_bo = bo;
5044 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
5045 checkGLcall("glClientActiveTextureARB");
5047 /* The coords to supply depend completely on the fvf/vertex shader. */
5048 format_gl = wined3d_format_gl(e->format);
5049 gl_info->gl_ops.gl.p_glTexCoordPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5050 e->data.addr + state->load_base_vertex_index * e->stride);
5051 gl_info->gl_ops.gl.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY);
5052 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5054 else
5056 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + mapped_stage, 0, 0, 0, 1));
5059 if (gl_info->supported[NV_REGISTER_COMBINERS])
5061 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
5062 for (texture_idx = mapped_stage + 1; texture_idx < gl_info->limits.textures; ++texture_idx)
5064 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
5068 checkGLcall("loadTexCoords");
5071 /* This should match any arrays loaded in wined3d_context_gl_load_vertex_data(). */
5072 static void wined3d_context_gl_unload_vertex_data(struct wined3d_context_gl *context_gl)
5074 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5076 if (!context_gl->c.namedArraysLoaded)
5077 return;
5078 gl_info->gl_ops.gl.p_glDisableClientState(GL_VERTEX_ARRAY);
5079 gl_info->gl_ops.gl.p_glDisableClientState(GL_NORMAL_ARRAY);
5080 gl_info->gl_ops.gl.p_glDisableClientState(GL_COLOR_ARRAY);
5081 if (gl_info->supported[EXT_SECONDARY_COLOR])
5082 gl_info->gl_ops.gl.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5083 wined3d_context_gl_unload_tex_coords(context_gl);
5084 context_gl->c.namedArraysLoaded = FALSE;
5087 static void wined3d_context_gl_load_vertex_data(struct wined3d_context_gl *context_gl,
5088 const struct wined3d_stream_info *si, const struct wined3d_state *state)
5090 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5091 const struct wined3d_stream_info_element *e;
5092 const struct wined3d_format_gl *format_gl;
5093 GLuint current_bo, bo;
5095 TRACE("context_gl %p, si %p, state %p.\n", context_gl, si, state);
5097 /* This is used for the fixed-function pipeline only, and the
5098 * fixed-function pipeline doesn't do instancing. */
5099 context_gl->c.instance_count = 0;
5100 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5102 /* Blend data */
5103 if ((si->use_map & (1u << WINED3D_FFP_BLENDWEIGHT))
5104 || si->use_map & (1u << WINED3D_FFP_BLENDINDICES))
5106 /* TODO: Support vertex blending in immediate mode draws. No need to
5107 * write a FIXME here, this is done after the general vertex
5108 * declaration decoding. */
5109 WARN("Vertex blending not supported.\n");
5112 /* Point Size */
5113 if (si->use_map & (1u << WINED3D_FFP_PSIZE))
5115 /* No such functionality in the fixed-function GL pipeline. */
5116 WARN("Per-vertex point size not supported.\n");
5119 /* Position */
5120 if (si->use_map & (1u << WINED3D_FFP_POSITION))
5122 e = &si->elements[WINED3D_FFP_POSITION];
5123 format_gl = wined3d_format_gl(e->format);
5125 bo = wined3d_bo_gl_id(e->data.buffer_object);
5126 if (current_bo != bo)
5128 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5129 checkGLcall("glBindBuffer");
5130 current_bo = bo;
5133 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n",
5134 format_gl->vtx_format, format_gl->vtx_type, e->stride,
5135 e->data.addr + state->load_base_vertex_index * e->stride);
5136 gl_info->gl_ops.gl.p_glVertexPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5137 e->data.addr + state->load_base_vertex_index * e->stride);
5138 checkGLcall("glVertexPointer(...)");
5139 gl_info->gl_ops.gl.p_glEnableClientState(GL_VERTEX_ARRAY);
5140 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5141 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5144 /* Normals */
5145 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
5147 e = &si->elements[WINED3D_FFP_NORMAL];
5148 format_gl = wined3d_format_gl(e->format);
5150 bo = wined3d_bo_gl_id(e->data.buffer_object);
5151 if (current_bo != bo)
5153 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5154 checkGLcall("glBindBuffer");
5155 current_bo = bo;
5158 TRACE("glNormalPointer(%#x, %#x, %p);\n", format_gl->vtx_type, e->stride,
5159 e->data.addr + state->load_base_vertex_index * e->stride);
5160 gl_info->gl_ops.gl.p_glNormalPointer(format_gl->vtx_type, e->stride,
5161 e->data.addr + state->load_base_vertex_index * e->stride);
5162 checkGLcall("glNormalPointer(...)");
5163 gl_info->gl_ops.gl.p_glEnableClientState(GL_NORMAL_ARRAY);
5164 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5165 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5167 else
5169 gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
5170 checkGLcall("glNormal3f(0, 0, 0)");
5173 /* Diffuse colour */
5174 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
5176 e = &si->elements[WINED3D_FFP_DIFFUSE];
5177 format_gl = wined3d_format_gl(e->format);
5179 bo = wined3d_bo_gl_id(e->data.buffer_object);
5180 if (current_bo != bo)
5182 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5183 checkGLcall("glBindBuffer");
5184 current_bo = bo;
5187 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5188 format_gl->vtx_format, format_gl->vtx_type, e->stride,
5189 e->data.addr + state->load_base_vertex_index * e->stride);
5190 gl_info->gl_ops.gl.p_glColorPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5191 e->data.addr + state->load_base_vertex_index * e->stride);
5192 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5193 gl_info->gl_ops.gl.p_glEnableClientState(GL_COLOR_ARRAY);
5194 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5195 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5197 else
5199 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
5200 checkGLcall("glColor4f(1, 1, 1, 1)");
5203 /* Specular colour */
5204 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
5206 TRACE("Setting specular colour.\n");
5208 e = &si->elements[WINED3D_FFP_SPECULAR];
5210 if (gl_info->supported[EXT_SECONDARY_COLOR])
5212 GLint format;
5213 GLenum type;
5215 format_gl = wined3d_format_gl(e->format);
5216 type = format_gl->vtx_type;
5217 format = format_gl->vtx_format;
5219 bo = wined3d_bo_gl_id(e->data.buffer_object);
5220 if (current_bo != bo)
5222 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5223 checkGLcall("glBindBuffer");
5224 current_bo = bo;
5227 if (format != 4 || (gl_info->quirks & WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA))
5229 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5230 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5231 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5232 * 4 component secondary colors use it
5234 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format, type, e->stride,
5235 e->data.addr + state->load_base_vertex_index * e->stride);
5236 GL_EXTCALL(glSecondaryColorPointerEXT(format, type, e->stride,
5237 e->data.addr + state->load_base_vertex_index * e->stride));
5238 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5240 else
5242 switch (type)
5244 case GL_UNSIGNED_BYTE:
5245 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e->stride,
5246 e->data.addr + state->load_base_vertex_index * e->stride);
5247 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, e->stride,
5248 e->data.addr + state->load_base_vertex_index * e->stride));
5249 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5250 break;
5252 default:
5253 FIXME("Add 4 component specular colour pointers for type %#x.\n", type);
5254 /* Make sure that the right colour component is dropped. */
5255 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type, e->stride,
5256 e->data.addr + state->load_base_vertex_index * e->stride);
5257 GL_EXTCALL(glSecondaryColorPointerEXT(3, type, e->stride,
5258 e->data.addr + state->load_base_vertex_index * e->stride));
5259 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5262 gl_info->gl_ops.gl.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5263 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5264 wined3d_buffer_gl(state->streams[e->stream_idx].buffer)->bo_user.valid = true;
5266 else
5268 WARN("Specular colour is not supported in this GL implementation.\n");
5271 else
5273 if (gl_info->supported[EXT_SECONDARY_COLOR])
5275 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
5276 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5278 else
5280 WARN("Specular colour is not supported in this GL implementation.\n");
5284 /* Texture coordinates */
5285 wined3d_context_gl_load_tex_coords(context_gl, si, &current_bo, state);
5288 static void wined3d_context_gl_unload_numbered_array(struct wined3d_context_gl *context_gl, unsigned int i)
5290 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5292 GL_EXTCALL(glDisableVertexAttribArray(i));
5293 checkGLcall("glDisableVertexAttribArray");
5294 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5295 GL_EXTCALL(glVertexAttribDivisor(i, 0));
5297 context_gl->c.numbered_array_mask &= ~(1u << i);
5300 static void wined3d_context_gl_unload_numbered_arrays(struct wined3d_context_gl *context_gl)
5302 uint32_t mask = context_gl->c.numbered_array_mask;
5303 unsigned int i;
5305 while (mask)
5307 i = wined3d_bit_scan(&mask);
5308 wined3d_context_gl_unload_numbered_array(context_gl, i);
5312 static void wined3d_context_gl_load_numbered_arrays(struct wined3d_context_gl *context_gl,
5313 const struct wined3d_stream_info *stream_info, const struct wined3d_state *state)
5315 struct wined3d_context *context = &context_gl->c;
5316 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5317 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5318 GLuint current_bo, bo;
5319 unsigned int i;
5321 /* Default to no instancing. */
5322 context->instance_count = 0;
5323 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5325 for (i = 0; i < MAX_ATTRIBS; ++i)
5327 const struct wined3d_stream_info_element *element = &stream_info->elements[i];
5328 const struct wined3d_stream_state *stream;
5329 const struct wined3d_format_gl *format_gl;
5331 if (!(stream_info->use_map & (1u << i)))
5333 if (context->numbered_array_mask & (1u << i))
5334 wined3d_context_gl_unload_numbered_array(context_gl, i);
5335 if (!use_vs(state) && i == WINED3D_FFP_DIFFUSE)
5337 if (!(context_gl->default_attrib_value_set & (1u << i)) || !context_gl->diffuse_attrib_to_1)
5339 GL_EXTCALL(glVertexAttrib4f(i, 1.0f, 1.0f, 1.0f, 1.0f));
5340 context_gl->diffuse_attrib_to_1 = 1;
5343 else
5345 if (!(context_gl->default_attrib_value_set & (1u << i)))
5347 GL_EXTCALL(glVertexAttrib4f(i, 0.0f, 0.0f, 0.0f, 0.0f));
5348 if (i == WINED3D_FFP_DIFFUSE)
5349 context_gl->diffuse_attrib_to_1 = 0;
5352 context_gl->default_attrib_value_set |= 1u << i;
5353 continue;
5356 format_gl = wined3d_format_gl(element->format);
5357 stream = &state->streams[element->stream_idx];
5358 wined3d_buffer_gl(stream->buffer)->bo_user.valid = true;
5360 if ((stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA) && !context->instance_count)
5361 context->instance_count = state->streams[0].frequency;
5363 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5365 unsigned int divisor = 0;
5367 if (element->instanced)
5368 divisor = element->divisor ? element->divisor : UINT_MAX;
5369 GL_EXTCALL(glVertexAttribDivisor(i, divisor));
5371 else if (element->divisor)
5373 /* Unload instanced arrays, they will be loaded using immediate
5374 * mode instead. */
5375 if (context->numbered_array_mask & (1u << i))
5376 wined3d_context_gl_unload_numbered_array(context_gl, i);
5377 context_gl->default_attrib_value_set &= ~(1u << i);
5378 continue;
5381 TRACE("Loading array %u %s.\n", i, debug_bo_address(&element->data));
5383 if (element->stride)
5385 DWORD format_flags = format_gl->f.flags[WINED3D_GL_RES_TYPE_BUFFER];
5387 bo = wined3d_bo_gl_id(element->data.buffer_object);
5388 if (current_bo != bo)
5390 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5391 checkGLcall("glBindBuffer");
5392 current_bo = bo;
5394 /* Use the VBO to find out if a vertex buffer exists, not the vb
5395 * pointer. vb can point to a user pointer data blob. In that case
5396 * current_bo will be 0. If there is a vertex buffer but no vbo we
5397 * won't be load converted attributes anyway. */
5398 if (vs && vs->reg_maps.shader_version.major >= 4 && (format_flags & WINED3DFMT_FLAG_INTEGER))
5400 GL_EXTCALL(glVertexAttribIPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5401 element->stride, element->data.addr + state->load_base_vertex_index * element->stride));
5403 else
5405 GL_EXTCALL(glVertexAttribPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5406 !!(format_flags & WINED3DFMT_FLAG_NORMALISED), element->stride,
5407 element->data.addr + state->load_base_vertex_index * element->stride));
5410 if (!(context->numbered_array_mask & (1u << i)))
5412 GL_EXTCALL(glEnableVertexAttribArray(i));
5413 context->numbered_array_mask |= (1u << i);
5416 else
5418 /* Stride = 0 means always the same values.
5419 * glVertexAttribPointer() doesn't do that. Instead disable the
5420 * pointer and set up the attribute statically. But we have to
5421 * figure out the system memory address. */
5422 const BYTE *ptr = element->data.addr;
5423 if (element->data.buffer_object)
5424 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(stream->buffer, context);
5426 if (context->numbered_array_mask & (1u << i))
5427 wined3d_context_gl_unload_numbered_array(context_gl, i);
5429 switch (format_gl->f.id)
5431 case WINED3DFMT_R32_FLOAT:
5432 GL_EXTCALL(glVertexAttrib1fv(i, (const GLfloat *)ptr));
5433 break;
5434 case WINED3DFMT_R32G32_FLOAT:
5435 GL_EXTCALL(glVertexAttrib2fv(i, (const GLfloat *)ptr));
5436 break;
5437 case WINED3DFMT_R32G32B32_FLOAT:
5438 GL_EXTCALL(glVertexAttrib3fv(i, (const GLfloat *)ptr));
5439 break;
5440 case WINED3DFMT_R32G32B32A32_FLOAT:
5441 GL_EXTCALL(glVertexAttrib4fv(i, (const GLfloat *)ptr));
5442 break;
5443 case WINED3DFMT_R8G8B8A8_UINT:
5444 GL_EXTCALL(glVertexAttrib4ubv(i, ptr));
5445 break;
5446 case WINED3DFMT_B8G8R8A8_UNORM:
5447 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
5449 const DWORD *src = (const DWORD *)ptr;
5450 DWORD c = *src & 0xff00ff00u;
5451 c |= (*src & 0xff0000u) >> 16;
5452 c |= (*src & 0xffu) << 16;
5453 GL_EXTCALL(glVertexAttrib4Nubv(i, (GLubyte *)&c));
5454 break;
5456 /* else fallthrough */
5457 case WINED3DFMT_R8G8B8A8_UNORM:
5458 GL_EXTCALL(glVertexAttrib4Nubv(i, ptr));
5459 break;
5460 case WINED3DFMT_R16G16_SINT:
5461 GL_EXTCALL(glVertexAttrib2sv(i, (const GLshort *)ptr));
5462 break;
5463 case WINED3DFMT_R16G16B16A16_SINT:
5464 GL_EXTCALL(glVertexAttrib4sv(i, (const GLshort *)ptr));
5465 break;
5466 case WINED3DFMT_R16G16_SNORM:
5468 const GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
5469 GL_EXTCALL(glVertexAttrib4Nsv(i, s));
5470 break;
5472 case WINED3DFMT_R16G16_UNORM:
5474 const GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
5475 GL_EXTCALL(glVertexAttrib4Nusv(i, s));
5476 break;
5478 case WINED3DFMT_R16G16B16A16_SNORM:
5479 GL_EXTCALL(glVertexAttrib4Nsv(i, (const GLshort *)ptr));
5480 break;
5481 case WINED3DFMT_R16G16B16A16_UNORM:
5482 GL_EXTCALL(glVertexAttrib4Nusv(i, (const GLushort *)ptr));
5483 break;
5484 case WINED3DFMT_R10G10B10X2_UINT:
5485 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5486 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5487 break;
5488 case WINED3DFMT_R10G10B10X2_SNORM:
5489 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5490 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5491 break;
5492 case WINED3DFMT_R16G16_FLOAT:
5493 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5495 /* Not supported by GL_ARB_half_float_vertex. */
5496 GL_EXTCALL(glVertexAttrib2hvNV(i, (const GLhalfNV *)ptr));
5498 else
5500 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5501 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5502 GL_EXTCALL(glVertexAttrib2f(i, x, y));
5504 break;
5505 case WINED3DFMT_R16G16B16A16_FLOAT:
5506 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5508 /* Not supported by GL_ARB_half_float_vertex. */
5509 GL_EXTCALL(glVertexAttrib4hvNV(i, (const GLhalfNV *)ptr));
5511 else
5513 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5514 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5515 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
5516 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
5517 GL_EXTCALL(glVertexAttrib4f(i, x, y, z, w));
5519 break;
5520 default:
5521 ERR("Unexpected declaration in stride 0 attributes.\n");
5522 break;
5525 context_gl->default_attrib_value_set &= ~(1u << i);
5528 checkGLcall("Loading numbered arrays");
5531 void wined3d_context_gl_update_stream_sources(struct wined3d_context_gl *context_gl,
5532 const struct wined3d_state *state)
5534 if (context_gl->c.use_immediate_mode_draw)
5535 return;
5537 wined3d_context_gl_unload_vertex_data(context_gl);
5538 if (context_gl->c.d3d_info->ffp_generic_attributes || use_vs(state))
5540 TRACE("Loading numbered arrays.\n");
5541 wined3d_context_gl_load_numbered_arrays(context_gl, &context_gl->c.stream_info, state);
5542 return;
5545 TRACE("Loading named arrays.\n");
5546 wined3d_context_gl_unload_numbered_arrays(context_gl);
5547 wined3d_context_gl_load_vertex_data(context_gl, &context_gl->c.stream_info, state);
5548 context_gl->c.namedArraysLoaded = TRUE;
5551 static void apply_texture_blit_state(const struct wined3d_gl_info *gl_info, struct gl_texture *texture,
5552 GLenum target, unsigned int level, enum wined3d_texture_filter_type filter)
5554 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
5555 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
5556 wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
5557 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
5558 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
5559 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
5560 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
5561 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, level);
5563 /* We changed the filtering settings on the texture. Make sure they get
5564 * reset on subsequent draws. */
5565 texture->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
5566 texture->sampler_desc.min_filter = WINED3D_TEXF_POINT;
5567 texture->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
5568 texture->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
5569 texture->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
5570 texture->sampler_desc.srgb_decode = FALSE;
5571 texture->base_level = level;
5574 /* Context activation is done by the caller. */
5575 void wined3d_context_gl_draw_shaded_quad(struct wined3d_context_gl *context_gl, struct wined3d_texture_gl *texture_gl,
5576 unsigned int sub_resource_idx, const RECT *src_rect, const RECT *dst_rect,
5577 enum wined3d_texture_filter_type filter)
5579 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5580 struct wined3d_blt_info info;
5581 unsigned int level, w, h, i;
5582 SIZE dst_size;
5583 struct blit_vertex
5585 float x, y;
5586 struct wined3d_vec3 texcoord;
5588 quad[4];
5590 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5592 level = sub_resource_idx % texture_gl->t.level_count;
5593 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5594 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5595 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5597 wined3d_context_gl_get_rt_size(context_gl, &dst_size);
5598 w = dst_size.cx;
5599 h = dst_size.cy;
5601 quad[0].x = dst_rect->left * 2.0f / w - 1.0f;
5602 quad[0].y = dst_rect->top * 2.0f / h - 1.0f;
5603 quad[0].texcoord = info.texcoords[0];
5605 quad[1].x = dst_rect->right * 2.0f / w - 1.0f;
5606 quad[1].y = dst_rect->top * 2.0f / h - 1.0f;
5607 quad[1].texcoord = info.texcoords[1];
5609 quad[2].x = dst_rect->left * 2.0f / w - 1.0f;
5610 quad[2].y = dst_rect->bottom * 2.0f / h - 1.0f;
5611 quad[2].texcoord = info.texcoords[2];
5613 quad[3].x = dst_rect->right * 2.0f / w - 1.0f;
5614 quad[3].y = dst_rect->bottom * 2.0f / h - 1.0f;
5615 quad[3].texcoord = info.texcoords[3];
5617 /* Draw a quad. */
5618 if (gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
5620 if (!context_gl->blit_vbo)
5621 GL_EXTCALL(glGenBuffers(1, &context_gl->blit_vbo));
5622 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, context_gl->blit_vbo));
5624 wined3d_context_gl_unload_vertex_data(context_gl);
5625 wined3d_context_gl_unload_numbered_arrays(context_gl);
5627 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STREAM_DRAW));
5628 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT, FALSE, sizeof(*quad), NULL));
5629 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT, FALSE, sizeof(*quad),
5630 (void *)FIELD_OFFSET(struct blit_vertex, texcoord)));
5632 GL_EXTCALL(glEnableVertexAttribArray(0));
5633 GL_EXTCALL(glEnableVertexAttribArray(1));
5635 gl_info->gl_ops.gl.p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
5637 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
5638 GL_EXTCALL(glDisableVertexAttribArray(1));
5639 GL_EXTCALL(glDisableVertexAttribArray(0));
5641 else
5643 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5645 for (i = 0; i < ARRAY_SIZE(quad); ++i)
5647 GL_EXTCALL(glVertexAttrib3fv(1, &quad[i].texcoord.x));
5648 GL_EXTCALL(glVertexAttrib2fv(0, &quad[i].x));
5651 gl_info->gl_ops.gl.p_glEnd();
5653 checkGLcall("draw");
5655 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5656 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);
5659 /* Context activation is done by the caller. */
5660 void wined3d_context_gl_draw_textured_quad(struct wined3d_context_gl *context_gl,
5661 struct wined3d_texture_gl *texture_gl, unsigned int sub_resource_idx,
5662 const RECT *src_rect, const RECT *dst_rect, enum wined3d_texture_filter_type filter)
5664 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5665 struct wined3d_blt_info info;
5666 unsigned int level;
5668 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5670 gl_info->gl_ops.gl.p_glEnable(info.bind_target);
5671 checkGLcall("glEnable(bind_target)");
5673 level = sub_resource_idx % texture_gl->t.level_count;
5674 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5675 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5676 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5677 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
5678 checkGLcall("glTexEnvi");
5680 /* Draw a quad. */
5681 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5682 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[0].x);
5683 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->top);
5685 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[1].x);
5686 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->top);
5688 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[2].x);
5689 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->bottom);
5691 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[3].x);
5692 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->bottom);
5693 gl_info->gl_ops.gl.p_glEnd();
5695 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5696 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);