wined3d: Do not pass an offset and size to wined3d_bo_gl_map().
[wine.git] / dlls / wined3d / context_gl.c
blob0c8ee0f605bdab17eab432d48bb422ad49690204
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 wined3d_context_gl *current;
1351 struct fbo_entry *entry, *entry2;
1352 struct wined3d_fence *fence;
1353 HGLRC restore_ctx;
1354 HDC restore_dc;
1355 unsigned int i;
1357 restore_ctx = wglGetCurrentContext();
1358 restore_dc = wglGetCurrentDC();
1360 if (context_gl->valid && context_gl->gl_ctx != restore_ctx)
1362 /* Attempting to restore a GL context corresponding to a wined3d
1363 * context is not particularly useful. Worse, when we're called from
1364 * wined3d_context_gl_destroy(), we subsequently clear the "current
1365 * D3D context" TLS value, which would cause
1366 * wined3d_context_gl_enter() to consider the GL context a non-D3D
1367 * context. */
1368 if ((current = wined3d_context_gl_get_current()) && current->gl_ctx == restore_ctx)
1369 restore_ctx = NULL;
1370 wined3d_context_gl_set_gl_context(context_gl);
1372 else
1374 restore_ctx = NULL;
1377 if (context_gl->valid)
1379 /* If we're here because we're switching away from a previously
1380 * destroyed context, acquiring a context in order to submit a fence
1381 * is problematic. (In particular, we'd end up back here again in the
1382 * process of switching to the newly acquired context.) */
1383 if (context_gl->c.destroyed)
1385 gl_info->gl_ops.gl.p_glFinish();
1387 else
1389 wined3d_context_gl_submit_command_fence(context_gl);
1390 wined3d_context_gl_wait_command_fence(context_gl,
1391 wined3d_device_gl(context_gl->c.device)->current_fence_id - 1);
1394 if (context_gl->dummy_arbfp_prog)
1395 GL_EXTCALL(glDeleteProgramsARB(1, &context_gl->dummy_arbfp_prog));
1397 if (context_gl->blit_vbo)
1398 GL_EXTCALL(glDeleteBuffers(1, &context_gl->blit_vbo));
1400 for (i = 0; i < context_gl->free_pipeline_statistics_query_count; ++i)
1402 union wined3d_gl_pipeline_statistics_query *q = &context_gl->free_pipeline_statistics_queries[i];
1403 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1406 for (i = 0; i < context_gl->free_so_statistics_query_count; ++i)
1408 union wined3d_gl_so_statistics_query *q = &context_gl->free_so_statistics_queries[i];
1409 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1412 if (context_gl->free_timestamp_query_count)
1413 GL_EXTCALL(glDeleteQueries(context_gl->free_timestamp_query_count, context_gl->free_timestamp_queries));
1415 if (gl_info->supported[ARB_SYNC])
1417 for (i = 0; i < context_gl->free_fence_count; ++i)
1419 GL_EXTCALL(glDeleteSync(context_gl->free_fences[i].sync));
1422 else if (gl_info->supported[APPLE_FENCE])
1424 for (i = 0; i < context_gl->free_fence_count; ++i)
1426 GL_EXTCALL(glDeleteFencesAPPLE(1, &context_gl->free_fences[i].id));
1429 else if (gl_info->supported[NV_FENCE])
1431 for (i = 0; i < context_gl->free_fence_count; ++i)
1433 GL_EXTCALL(glDeleteFencesNV(1, &context_gl->free_fences[i].id));
1437 if (context_gl->free_occlusion_query_count)
1438 GL_EXTCALL(glDeleteQueries(context_gl->free_occlusion_query_count, context_gl->free_occlusion_queries));
1440 checkGLcall("context cleanup");
1442 heap_free(context_gl->submitted.fences);
1443 heap_free(context_gl->free_pipeline_statistics_queries);
1444 heap_free(context_gl->free_so_statistics_queries);
1445 heap_free(context_gl->free_timestamp_queries);
1446 heap_free(context_gl->free_fences);
1447 heap_free(context_gl->free_occlusion_queries);
1449 LIST_FOR_EACH_ENTRY(pipeline_statistics_query, &context_gl->pipeline_statistics_queries,
1450 struct wined3d_pipeline_statistics_query, entry)
1452 if (context_gl->valid)
1453 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query->u.id), pipeline_statistics_query->u.id));
1454 pipeline_statistics_query->context_gl = NULL;
1457 LIST_FOR_EACH_ENTRY(so_statistics_query, &context_gl->so_statistics_queries,
1458 struct wined3d_so_statistics_query, entry)
1460 if (context_gl->valid)
1461 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query->u.id), so_statistics_query->u.id));
1462 so_statistics_query->context_gl = NULL;
1465 LIST_FOR_EACH_ENTRY(timestamp_query, &context_gl->timestamp_queries, struct wined3d_timestamp_query, entry)
1467 if (context_gl->valid)
1468 GL_EXTCALL(glDeleteQueries(1, &timestamp_query->id));
1469 timestamp_query->context_gl = NULL;
1472 LIST_FOR_EACH_ENTRY(fence, &context_gl->fences, struct wined3d_fence, entry)
1474 if (context_gl->valid)
1476 if (gl_info->supported[ARB_SYNC])
1478 if (fence->object.sync)
1479 GL_EXTCALL(glDeleteSync(fence->object.sync));
1481 else if (gl_info->supported[APPLE_FENCE])
1483 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence->object.id));
1485 else if (gl_info->supported[NV_FENCE])
1487 GL_EXTCALL(glDeleteFencesNV(1, &fence->object.id));
1490 fence->context_gl = NULL;
1493 LIST_FOR_EACH_ENTRY(occlusion_query, &context_gl->occlusion_queries, struct wined3d_occlusion_query, entry)
1495 if (context_gl->valid)
1496 GL_EXTCALL(glDeleteQueries(1, &occlusion_query->id));
1497 occlusion_query->context_gl = NULL;
1500 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_destroy_list, struct fbo_entry, entry)
1502 if (!context_gl->valid)
1503 entry->id = 0;
1504 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1507 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_list, struct fbo_entry, entry)
1509 if (!context_gl->valid)
1510 entry->id = 0;
1511 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1514 heap_free(context_gl->texture_type);
1516 wined3d_context_gl_restore_pixel_format(context_gl);
1517 if (restore_ctx)
1518 context_restore_gl_context(gl_info, restore_dc, restore_ctx);
1519 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1520 ERR("Failed to disable GL context.\n");
1522 wined3d_release_dc(context_gl->window, context_gl->dc);
1524 if (!wglDeleteContext(context_gl->gl_ctx))
1526 DWORD err = GetLastError();
1527 ERR("Failed to delete GL context %p, last error %#x.\n", context_gl->gl_ctx, err);
1530 wined3d_context_cleanup(&context_gl->c);
1533 DWORD context_get_tls_idx(void)
1535 return wined3d_context_tls_idx;
1538 void context_set_tls_idx(DWORD idx)
1540 wined3d_context_tls_idx = idx;
1543 struct wined3d_context_gl *wined3d_context_gl_get_current(void)
1545 return TlsGetValue(wined3d_context_tls_idx);
1548 BOOL wined3d_context_gl_set_current(struct wined3d_context_gl *context_gl)
1550 struct wined3d_context_gl *old = wined3d_context_gl_get_current();
1552 if (old == context_gl)
1554 TRACE("Already using D3D context %p.\n", context_gl);
1555 return TRUE;
1558 if (old)
1560 if (old->c.destroyed)
1562 TRACE("Switching away from destroyed context %p.\n", old);
1563 wined3d_context_gl_cleanup(old);
1564 heap_free((void *)old->gl_info);
1565 heap_free(old);
1567 else
1569 if (wglGetCurrentContext())
1571 const struct wined3d_gl_info *gl_info = old->gl_info;
1572 TRACE("Flushing context %p before switching to %p.\n", old, context_gl);
1573 gl_info->gl_ops.gl.p_glFlush();
1575 old->c.current = 0;
1579 if (context_gl)
1581 if (!context_gl->valid)
1583 ERR("Trying to make invalid context %p current.\n", context_gl);
1584 return FALSE;
1587 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n",
1588 context_gl, context_gl->gl_ctx, context_gl->dc);
1589 if (!wined3d_context_gl_set_gl_context(context_gl))
1590 return FALSE;
1591 context_gl->c.current = 1;
1593 else if (wglGetCurrentContext())
1595 TRACE("Clearing current D3D context.\n");
1596 if (!wglMakeCurrent(NULL, NULL))
1598 DWORD err = GetLastError();
1599 ERR("Failed to clear current GL context, last error %#x.\n", err);
1600 TlsSetValue(wined3d_context_tls_idx, NULL);
1601 return FALSE;
1605 return TlsSetValue(wined3d_context_tls_idx, context_gl);
1608 void wined3d_context_gl_release(struct wined3d_context_gl *context_gl)
1610 TRACE("Releasing context %p, level %u.\n", context_gl, context_gl->level);
1612 if (WARN_ON(d3d))
1614 if (!context_gl->level)
1615 WARN("Context %p is not active.\n", context_gl);
1616 else if (context_gl != wined3d_context_gl_get_current())
1617 WARN("Context %p is not the current context.\n", context_gl);
1620 if (!--context_gl->level)
1622 if (wined3d_context_gl_restore_pixel_format(context_gl))
1623 context_gl->needs_set = 1;
1624 if (context_gl->restore_ctx)
1626 TRACE("Restoring GL context %p on device context %p.\n", context_gl->restore_ctx, context_gl->restore_dc);
1627 context_restore_gl_context(context_gl->gl_info, context_gl->restore_dc, context_gl->restore_ctx);
1628 context_gl->restore_ctx = NULL;
1629 context_gl->restore_dc = NULL;
1632 if (context_gl->c.destroy_delayed)
1634 TRACE("Destroying context %p.\n", context_gl);
1635 wined3d_context_gl_destroy(context_gl);
1640 static void wined3d_context_gl_enter(struct wined3d_context_gl *context_gl)
1642 TRACE("Entering context %p, level %u.\n", context_gl, context_gl->level + 1);
1644 if (!context_gl->level++)
1646 const struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
1647 HGLRC current_gl = wglGetCurrentContext();
1649 if (current_gl && (!current_context || current_context->gl_ctx != current_gl))
1651 TRACE("Another GL context (%p on device context %p) is already current.\n",
1652 current_gl, wglGetCurrentDC());
1653 context_gl->restore_ctx = current_gl;
1654 context_gl->restore_dc = wglGetCurrentDC();
1655 context_gl->needs_set = 1;
1657 else if (!context_gl->needs_set && !(context_gl->dc_is_private && context_gl->dc_has_format)
1658 && context_gl->pixel_format != context_gl->gl_info->gl_ops.wgl.p_wglGetPixelFormat(context_gl->dc))
1659 context_gl->needs_set = 1;
1663 /* This function takes care of wined3d pixel format selection. */
1664 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1665 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1666 bool aux_buffers, bool swap_effect_copy)
1668 unsigned int cfg_count = wined3d_adapter_gl(device->adapter)->pixel_format_count;
1669 unsigned int current_value;
1670 PIXELFORMATDESCRIPTOR pfd;
1671 int iPixelFormat = 0;
1672 unsigned int i;
1674 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, swap_effect_copy %#x.\n",
1675 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1676 aux_buffers, swap_effect_copy);
1678 current_value = 0;
1679 for (i = 0; i < cfg_count; ++i)
1681 const struct wined3d_pixel_format *cfg = &wined3d_adapter_gl(device->adapter)->pixel_formats[i];
1682 unsigned int value;
1684 /* For now only accept RGBA formats. Perhaps some day we will
1685 * allow floating point formats for pbuffers. */
1686 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1687 continue;
1688 /* In window mode we need a window drawable format and double buffering. */
1689 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1690 continue;
1691 if (cfg->redSize < color_format->red_size)
1692 continue;
1693 if (cfg->greenSize < color_format->green_size)
1694 continue;
1695 if (cfg->blueSize < color_format->blue_size)
1696 continue;
1697 if (cfg->alphaSize < color_format->alpha_size)
1698 continue;
1699 if (cfg->depthSize < ds_format->depth_size)
1700 continue;
1701 if (ds_format->stencil_size && cfg->stencilSize != ds_format->stencil_size)
1702 continue;
1703 /* Check multisampling support. */
1704 if (cfg->numSamples)
1705 continue;
1707 value = 1;
1708 if (swap_effect_copy && cfg->swap_method == WGL_SWAP_COPY_ARB)
1709 value += 1;
1710 /* We try to locate a format which matches our requirements exactly. In case of
1711 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1712 if (cfg->depthSize == ds_format->depth_size)
1713 value += 2;
1714 if (cfg->stencilSize == ds_format->stencil_size)
1715 value += 4;
1716 if (cfg->alphaSize == color_format->alpha_size)
1717 value += 8;
1718 /* We like to have aux buffers in backbuffer mode */
1719 if (aux_buffers && cfg->auxBuffers)
1720 value += 16;
1721 if (cfg->redSize == color_format->red_size
1722 && cfg->greenSize == color_format->green_size
1723 && cfg->blueSize == color_format->blue_size)
1724 value += 32;
1726 if (value > current_value)
1728 iPixelFormat = cfg->iPixelFormat;
1729 current_value = value;
1733 if (!iPixelFormat)
1735 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1737 memset(&pfd, 0, sizeof(pfd));
1738 pfd.nSize = sizeof(pfd);
1739 pfd.nVersion = 1;
1740 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1741 pfd.iPixelType = PFD_TYPE_RGBA;
1742 pfd.cAlphaBits = color_format->alpha_size;
1743 pfd.cColorBits = color_format->red_size + color_format->green_size
1744 + color_format->blue_size + color_format->alpha_size;
1745 pfd.cDepthBits = ds_format->depth_size;
1746 pfd.cStencilBits = ds_format->stencil_size;
1747 pfd.iLayerType = PFD_MAIN_PLANE;
1749 if (!(iPixelFormat = ChoosePixelFormat(hdc, &pfd)))
1751 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1752 ERR("Can't find a suitable pixel format.\n");
1753 return 0;
1757 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1758 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1759 return iPixelFormat;
1762 /* Context activation is done by the caller. */
1763 void wined3d_context_gl_bind_dummy_textures(const struct wined3d_context_gl *context_gl)
1765 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
1766 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1767 unsigned int i;
1769 for (i = 0; i < gl_info->limits.combined_samplers; ++i)
1771 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + i));
1773 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
1774 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
1776 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1777 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
1779 if (gl_info->supported[EXT_TEXTURE3D])
1780 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
1782 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1783 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
1785 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
1786 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
1788 if (gl_info->supported[EXT_TEXTURE_ARRAY])
1790 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
1791 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
1794 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1795 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
1797 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1799 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
1800 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
1804 checkGLcall("bind dummy textures");
1807 void wined3d_check_gl_call(const struct wined3d_gl_info *gl_info,
1808 const char *file, unsigned int line, const char *name)
1810 GLint err;
1812 if (gl_info->supported[ARB_DEBUG_OUTPUT] || (err = gl_info->gl_ops.gl.p_glGetError()) == GL_NO_ERROR)
1814 TRACE("%s call ok %s / %u.\n", name, file, line);
1815 return;
1820 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1821 debug_glerror(err), err, name, file,line);
1822 err = gl_info->gl_ops.gl.p_glGetError();
1823 } while (err != GL_NO_ERROR);
1826 static BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1828 return gl_info->supported[ARB_DEBUG_OUTPUT]
1829 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1832 static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1833 GLenum severity, GLsizei length, const char *message, void *ctx)
1835 switch (type)
1837 case GL_DEBUG_TYPE_ERROR_ARB:
1838 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1839 break;
1841 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1842 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1843 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1844 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1845 break;
1847 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1848 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1849 break;
1851 default:
1852 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1853 break;
1857 HGLRC context_create_wgl_attribs(const struct wined3d_gl_info *gl_info, HDC hdc, HGLRC share_ctx)
1859 HGLRC ctx;
1860 unsigned int ctx_attrib_idx = 0;
1861 GLint ctx_attribs[7], ctx_flags = 0;
1863 if (context_debug_output_enabled(gl_info))
1864 ctx_flags = WGL_CONTEXT_DEBUG_BIT_ARB;
1865 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
1866 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version >> 16;
1867 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MINOR_VERSION_ARB;
1868 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version & 0xffff;
1869 if (ctx_flags)
1871 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1872 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1874 ctx_attribs[ctx_attrib_idx] = 0;
1876 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1878 if (gl_info->selected_gl_version >= MAKEDWORD_VERSION(3, 2))
1880 if (ctx_flags)
1882 ctx_flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1883 ctx_attribs[ctx_attrib_idx - 1] = ctx_flags;
1885 else
1887 ctx_flags = WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1888 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1889 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1890 ctx_attribs[ctx_attrib_idx] = 0;
1892 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1893 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1894 GetLastError());
1897 return ctx;
1900 static BOOL wined3d_context_gl_create_wgl_ctx(struct wined3d_context_gl *context_gl,
1901 struct wined3d_swapchain_gl *swapchain_gl)
1903 enum wined3d_swap_effect swap_effect = swapchain_gl->s.state.desc.swap_effect;
1904 const struct wined3d_format *colour_format, *ds_format;
1905 struct wined3d_context *context = &context_gl->c;
1906 const struct wined3d_gl_info *gl_info;
1907 struct wined3d_resource *target;
1908 struct wined3d_adapter *adapter;
1909 unsigned int target_bind_flags;
1910 struct wined3d_device *device;
1911 bool swap_effect_copy;
1912 HGLRC ctx, share_ctx;
1913 unsigned int i;
1915 device = context->device;
1916 adapter = device->adapter;
1917 gl_info = &adapter->gl_info;
1919 target = &context->current_rt.texture->resource;
1920 target_bind_flags = target->bind_flags;
1922 swap_effect_copy = swap_effect == WINED3D_SWAP_EFFECT_COPY || swap_effect == WINED3D_SWAP_EFFECT_COPY_VSYNC;
1924 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1926 static const enum wined3d_format_id ds_formats[] =
1928 WINED3DFMT_D24_UNORM_S8_UINT,
1929 WINED3DFMT_D32_UNORM,
1930 WINED3DFMT_R24_UNORM_X8_TYPELESS,
1931 WINED3DFMT_D16_UNORM,
1932 WINED3DFMT_S1_UINT_D15_UNORM,
1935 colour_format = target->format;
1937 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1938 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1939 if (colour_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1940 colour_format = wined3d_get_format(adapter, WINED3DFMT_B4G4R4A4_UNORM, target_bind_flags);
1941 else if (colour_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1942 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1944 /* DirectDraw supports 8bit paletted render targets and these are used by
1945 * old games like StarCraft and C&C. Most modern hardware doesn't support
1946 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1947 * conversion (ab)uses the alpha component for storing the palette index.
1948 * For this reason we require a format with 8bit alpha, so request
1949 * A8R8G8B8. */
1950 if (colour_format->id == WINED3DFMT_P8_UINT)
1951 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1953 /* Try to find a pixel format which matches our requirements. */
1954 if (!swapchain_gl->s.ds_format)
1956 for (i = 0; i < ARRAY_SIZE(ds_formats); ++i)
1958 ds_format = wined3d_get_format(adapter, ds_formats[i], WINED3D_BIND_DEPTH_STENCIL);
1959 if ((context_gl->pixel_format = context_choose_pixel_format(device,
1960 context_gl->dc, colour_format, ds_format, true, swap_effect_copy)))
1962 swapchain_gl->s.ds_format = ds_format;
1963 break;
1966 TRACE("Depth stencil format %s is not supported, trying next format.\n",
1967 debug_d3dformat(ds_format->id));
1970 else
1972 context_gl->pixel_format = context_choose_pixel_format(device,
1973 context_gl->dc, colour_format, swapchain_gl->s.ds_format, true, swap_effect_copy);
1976 else
1978 /* When using FBOs for off-screen rendering, we only use the drawable for
1979 * presentation blits, and don't do any rendering to it. That means we
1980 * don't need depth or stencil buffers, and can mostly ignore the render
1981 * target format. This wouldn't necessarily be quite correct for 10bpc
1982 * display modes, but we don't currently support those.
1983 * Using the same format regardless of the colour/depth/stencil targets
1984 * makes it much less likely that different wined3d instances will set
1985 * conflicting pixel formats. */
1986 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1987 ds_format = wined3d_get_format(adapter, WINED3DFMT_UNKNOWN, WINED3D_BIND_DEPTH_STENCIL);
1988 context_gl->pixel_format = context_choose_pixel_format(device,
1989 context_gl->dc, colour_format, ds_format, false, swap_effect_copy);
1992 if (!context_gl->pixel_format)
1994 ERR("Failed to choose pixel format.\n");
1995 return FALSE;
1998 wined3d_context_gl_enter(context_gl);
2000 if (!wined3d_context_gl_set_pixel_format(context_gl))
2002 context_release(context);
2004 if (context_gl->dc_is_private)
2006 ERR("Failed to set pixel format %d on device context %p.\n", context_gl->pixel_format, context_gl->dc);
2008 return FALSE;
2011 WARN("Failed to set pixel format %d on device context %p, trying backup DC.\n",
2012 context_gl->pixel_format, context_gl->dc);
2014 wined3d_release_dc(context_gl->window, context_gl->dc);
2015 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2017 ERR("Failed to retrieve the backup device context.\n");
2018 return FALSE;
2020 context_gl->dc_is_private = TRUE;
2022 return wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl);
2025 share_ctx = device->context_count ? wined3d_context_gl(device->contexts[0])->gl_ctx : NULL;
2026 if (gl_info->p_wglCreateContextAttribsARB)
2028 if (!(ctx = context_create_wgl_attribs(gl_info, context_gl->dc, share_ctx)))
2030 ERR("Failed to create a WGL context.\n");
2031 context_release(context);
2032 return FALSE;
2035 else
2037 if (!(ctx = wglCreateContext(context_gl->dc)))
2039 ERR("Failed to create a WGL context.\n");
2040 context_release(context);
2041 return FALSE;
2044 if (share_ctx && !wglShareLists(share_ctx, ctx))
2046 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
2047 context_release(context);
2048 if (!wglDeleteContext(ctx))
2049 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2050 return FALSE;
2054 context_gl->dc_has_format = TRUE;
2055 context_gl->needs_set = 1;
2056 context_gl->valid = 1;
2057 context_gl->gl_ctx = ctx;
2059 return TRUE;
2062 HRESULT wined3d_context_gl_init(struct wined3d_context_gl *context_gl, struct wined3d_swapchain_gl *swapchain_gl)
2064 struct wined3d_context *context = &context_gl->c;
2065 const struct wined3d_d3d_info *d3d_info;
2066 const struct wined3d_gl_info *gl_info;
2067 struct wined3d_device *device;
2068 unsigned int i;
2070 TRACE("context_gl %p, swapchain %p.\n", context_gl, swapchain_gl);
2072 wined3d_context_init(&context_gl->c, &swapchain_gl->s);
2074 device = context->device;
2075 gl_info = &device->adapter->gl_info;
2076 context_gl->gl_info = gl_info;
2077 d3d_info = context->d3d_info;
2079 context_gl->tid = GetCurrentThreadId();
2080 context_gl->window = context->swapchain->win_handle;
2081 if (context_gl->window == GetDesktopWindow())
2083 TRACE("Swapchain is created on the desktop window, trying backup device context.\n");
2084 context_gl->dc = NULL;
2086 else if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
2087 WARN("Failed to retrieve device context, trying swapchain backup.\n");
2089 if (!context_gl->dc)
2091 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2093 ERR("Failed to retrieve a device context.\n");
2094 return E_FAIL;
2096 context_gl->dc_is_private = TRUE;
2099 list_init(&context_gl->fbo_list);
2100 list_init(&context_gl->fbo_destroy_list);
2102 list_init(&context_gl->occlusion_queries);
2103 list_init(&context_gl->fences);
2104 list_init(&context_gl->timestamp_queries);
2105 list_init(&context_gl->so_statistics_queries);
2106 list_init(&context_gl->pipeline_statistics_queries);
2108 for (i = 0; i < ARRAY_SIZE(context_gl->tex_unit_map); ++i)
2109 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2110 for (i = 0; i < ARRAY_SIZE(context_gl->rev_tex_unit_map); ++i)
2111 context_gl->rev_tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2112 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
2114 /* Initialize the texture unit mapping to a 1:1 mapping. */
2115 unsigned int base, count;
2117 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_PIXEL, &base, &count);
2118 if (base + WINED3D_MAX_FRAGMENT_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2120 ERR("Unexpected texture unit base index %u.\n", base);
2121 goto fail;
2123 for (i = 0; i < min(count, WINED3D_MAX_FRAGMENT_SAMPLERS); ++i)
2125 context_gl->tex_unit_map[i] = base + i;
2126 context_gl->rev_tex_unit_map[base + i] = i;
2129 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_VERTEX, &base, &count);
2130 if (base + WINED3D_MAX_VERTEX_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2132 ERR("Unexpected texture unit base index %u.\n", base);
2133 goto fail;
2135 for (i = 0; i < min(count, WINED3D_MAX_VERTEX_SAMPLERS); ++i)
2137 context_gl->tex_unit_map[WINED3D_MAX_FRAGMENT_SAMPLERS + i] = base + i;
2138 context_gl->rev_tex_unit_map[base + i] = WINED3D_MAX_FRAGMENT_SAMPLERS + i;
2142 if (!(context_gl->texture_type = heap_calloc(gl_info->limits.combined_samplers,
2143 sizeof(*context_gl->texture_type))))
2144 goto fail;
2146 if (!wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl))
2147 goto fail;
2149 /* Set up the context defaults. */
2151 context->render_offscreen = wined3d_resource_is_offscreen(&context->current_rt.texture->resource);
2152 context_gl->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
2154 if (!wined3d_context_gl_set_current(context_gl))
2156 ERR("Cannot activate context to set up defaults.\n");
2157 context_release(context);
2158 if (!wglDeleteContext(context_gl->gl_ctx))
2159 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context_gl->gl_ctx, GetLastError());
2160 goto fail;
2163 if (context_debug_output_enabled(gl_info))
2165 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback, context));
2166 if (TRACE_ON(d3d_sync))
2167 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2168 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
2169 if (ERR_ON(d3d))
2171 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR,
2172 GL_DONT_CARE, 0, NULL, GL_TRUE));
2174 if (FIXME_ON(d3d))
2176 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
2177 GL_DONT_CARE, 0, NULL, GL_TRUE));
2178 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
2179 GL_DONT_CARE, 0, NULL, GL_TRUE));
2180 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY,
2181 GL_DONT_CARE, 0, NULL, GL_TRUE));
2183 if (WARN_ON(d3d_perf))
2185 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE,
2186 GL_DONT_CARE, 0, NULL, GL_TRUE));
2190 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2191 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &context_gl->aux_buffers);
2193 TRACE("Setting up the screen\n");
2195 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2197 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
2198 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2200 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
2201 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2203 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2204 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2206 else
2208 GLuint vao;
2210 GL_EXTCALL(glGenVertexArrays(1, &vao));
2211 GL_EXTCALL(glBindVertexArray(vao));
2212 checkGLcall("creating VAO");
2215 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
2216 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2217 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2218 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2220 if (gl_info->supported[NV_TEXTURE_SHADER2])
2222 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2223 * the previous texture where to source the offset from is always unit - 1.
2225 for (i = 1; i < gl_info->limits.textures; ++i)
2227 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2228 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
2229 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + i - 1);
2230 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2233 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2235 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2236 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2237 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2238 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2239 * is ever assigned.
2241 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2242 * program and the dummy program is destroyed when the context is destroyed.
2244 static const char dummy_program[] =
2245 "!!ARBfp1.0\n"
2246 "MOV result.color, fragment.color.primary;\n"
2247 "END\n";
2248 GL_EXTCALL(glGenProgramsARB(1, &context_gl->dummy_arbfp_prog));
2249 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, context_gl->dummy_arbfp_prog));
2250 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
2251 GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2254 if (gl_info->supported[ARB_POINT_SPRITE])
2256 for (i = 0; i < gl_info->limits.textures; ++i)
2258 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2259 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2260 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2264 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2266 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2268 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2270 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2272 if (!(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART))
2274 if (gl_info->supported[ARB_ES3_COMPATIBILITY])
2276 gl_info->gl_ops.gl.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
2277 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2279 else
2281 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2284 if (!(d3d_info->wined3d_creation_flags & WINED3D_LEGACY_CUBEMAP_FILTERING)
2285 && gl_info->supported[ARB_SEAMLESS_CUBE_MAP])
2287 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
2288 checkGLcall("enable seamless cube map filtering");
2290 if (gl_info->supported[ARB_CLIP_CONTROL])
2291 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT));
2293 /* If this happens to be the first context for the device, dummy textures
2294 * are not created yet. In that case, they will be created (and bound) by
2295 * create_dummy_textures right after this context is initialized. */
2296 if (wined3d_device_gl(device)->dummy_textures.tex_2d)
2297 wined3d_context_gl_bind_dummy_textures(context_gl);
2299 /* Initialise all rectangles to avoid resetting unused ones later. */
2300 gl_info->gl_ops.gl.p_glScissor(0, 0, 0, 0);
2301 checkGLcall("glScissor");
2303 return WINED3D_OK;
2305 fail:
2306 heap_free(context_gl->texture_type);
2307 wined3d_release_dc(context_gl->window, context_gl->dc);
2308 return E_FAIL;
2311 void wined3d_context_gl_destroy(struct wined3d_context_gl *context_gl)
2313 struct wined3d_device *device = context_gl->c.device;
2315 TRACE("Destroying context %p.\n", context_gl);
2317 wined3d_from_cs(device->cs);
2319 /* We delay destroying a context when it is active. The context_release()
2320 * function invokes wined3d_context_gl_destroy() again while leaving the
2321 * last level. */
2322 if (context_gl->level)
2324 TRACE("Delaying destruction of context %p.\n", context_gl);
2325 context_gl->c.destroy_delayed = 1;
2326 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2327 context_gl->c.swapchain = NULL;
2328 return;
2331 device_context_remove(device, &context_gl->c);
2333 if (context_gl->c.current && context_gl->tid != GetCurrentThreadId())
2335 struct wined3d_gl_info *gl_info;
2337 /* Make a copy of gl_info for wined3d_context_gl_cleanup() use, the
2338 * one in wined3d_adapter may go away in the meantime. */
2339 gl_info = heap_alloc(sizeof(*gl_info));
2340 *gl_info = *context_gl->gl_info;
2341 context_gl->gl_info = gl_info;
2342 context_gl->c.destroyed = 1;
2344 return;
2347 wined3d_context_gl_cleanup(context_gl);
2348 TlsSetValue(context_get_tls_idx(), NULL);
2349 heap_free(context_gl);
2352 const unsigned int *wined3d_context_gl_get_tex_unit_mapping(const struct wined3d_context_gl *context_gl,
2353 const struct wined3d_shader_version *shader_version, unsigned int *base, unsigned int *count)
2355 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2357 if (!shader_version)
2359 *base = 0;
2360 *count = WINED3D_MAX_TEXTURES;
2361 return context_gl->tex_unit_map;
2364 if (shader_version->major >= 4)
2366 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, shader_version->type, base, count);
2367 return NULL;
2370 switch (shader_version->type)
2372 case WINED3D_SHADER_TYPE_PIXEL:
2373 *base = 0;
2374 *count = WINED3D_MAX_FRAGMENT_SAMPLERS;
2375 break;
2376 case WINED3D_SHADER_TYPE_VERTEX:
2377 *base = WINED3D_MAX_FRAGMENT_SAMPLERS;
2378 *count = WINED3D_MAX_VERTEX_SAMPLERS;
2379 break;
2380 default:
2381 ERR("Unhandled shader type %#x.\n", shader_version->type);
2382 *base = 0;
2383 *count = 0;
2386 return context_gl->tex_unit_map;
2389 static void wined3d_context_gl_get_rt_size(const struct wined3d_context_gl *context_gl, SIZE *size)
2391 const struct wined3d_texture *rt = context_gl->c.current_rt.texture;
2392 unsigned int level;
2394 if (rt->swapchain)
2396 RECT window_size;
2398 GetClientRect(context_gl->window, &window_size);
2399 size->cx = window_size.right - window_size.left;
2400 size->cy = window_size.bottom - window_size.top;
2402 return;
2405 level = context_gl->c.current_rt.sub_resource_idx % rt->level_count;
2406 size->cx = wined3d_texture_get_level_width(rt, level);
2407 size->cy = wined3d_texture_get_level_height(rt, level);
2410 void wined3d_context_gl_enable_clip_distances(struct wined3d_context_gl *context_gl, uint32_t enable_mask)
2412 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2413 unsigned int clip_distance_count, i;
2414 uint32_t disable_mask, current_mask;
2416 clip_distance_count = gl_info->limits.user_clip_distances;
2417 disable_mask = ~enable_mask;
2418 enable_mask &= wined3d_mask_from_size(clip_distance_count);
2419 disable_mask &= wined3d_mask_from_size(clip_distance_count);
2420 current_mask = context_gl->c.clip_distance_mask;
2421 context_gl->c.clip_distance_mask = enable_mask;
2423 enable_mask &= ~current_mask;
2424 while (enable_mask)
2426 i = wined3d_bit_scan(&enable_mask);
2427 gl_info->gl_ops.gl.p_glEnable(GL_CLIP_DISTANCE0 + i);
2429 disable_mask &= current_mask;
2430 while (disable_mask)
2432 i = wined3d_bit_scan(&disable_mask);
2433 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_DISTANCE0 + i);
2435 checkGLcall("toggle clip distances");
2438 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2440 return rt_mask & (1u << 31);
2443 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2445 return rt_mask & ~(1u << 31);
2448 /* Context activation is done by the caller. */
2449 static void wined3d_context_gl_apply_draw_buffers(struct wined3d_context_gl *context_gl, uint32_t rt_mask)
2451 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2452 GLenum draw_buffers[WINED3D_MAX_RENDER_TARGETS];
2454 if (!rt_mask)
2456 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2458 else if (is_rt_mask_onscreen(rt_mask))
2460 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2462 else
2464 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2466 unsigned int i = 0;
2468 while (rt_mask)
2470 if (rt_mask & 1)
2471 draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2472 else
2473 draw_buffers[i] = GL_NONE;
2475 rt_mask >>= 1;
2476 ++i;
2479 if (gl_info->supported[ARB_DRAW_BUFFERS])
2481 GL_EXTCALL(glDrawBuffers(i, draw_buffers));
2483 else
2485 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffers[0]);
2488 else
2490 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2494 checkGLcall("apply draw buffers");
2497 /* Context activation is done by the caller. */
2498 void wined3d_context_gl_set_draw_buffer(struct wined3d_context_gl *context_gl, GLenum buffer)
2500 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2501 struct fbo_entry *current_fbo = context_gl->current_fbo;
2502 uint32_t new_mask = context_generate_rt_mask(buffer);
2503 uint32_t *current_mask;
2505 current_mask = current_fbo ? &current_fbo->rt_mask : &context_gl->draw_buffers_mask;
2506 if (new_mask == *current_mask)
2507 return;
2509 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2510 checkGLcall("glDrawBuffer()");
2512 *current_mask = new_mask;
2515 /* Context activation is done by the caller. */
2516 void wined3d_context_gl_active_texture(struct wined3d_context_gl *context_gl,
2517 const struct wined3d_gl_info *gl_info, unsigned int unit)
2519 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + unit));
2520 checkGLcall("glActiveTexture");
2521 context_gl->active_texture = unit;
2524 void wined3d_context_gl_bind_bo(struct wined3d_context_gl *context_gl, GLenum binding, GLuint name)
2526 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2528 if (binding == GL_ELEMENT_ARRAY_BUFFER)
2529 context_invalidate_state(&context_gl->c, STATE_INDEXBUFFER);
2531 GL_EXTCALL(glBindBuffer(binding, name));
2534 void wined3d_context_gl_bind_texture(struct wined3d_context_gl *context_gl, GLenum target, GLuint name)
2536 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
2537 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2538 GLenum old_texture_type;
2539 unsigned int unit;
2541 if (name)
2542 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2543 else
2544 target = GL_NONE;
2546 unit = context_gl->active_texture;
2547 old_texture_type = context_gl->texture_type[unit];
2548 if (old_texture_type != target)
2550 switch (old_texture_type)
2552 case GL_NONE:
2553 /* nothing to do */
2554 break;
2555 case GL_TEXTURE_1D:
2556 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
2557 break;
2558 case GL_TEXTURE_1D_ARRAY:
2559 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
2560 break;
2561 case GL_TEXTURE_2D:
2562 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
2563 break;
2564 case GL_TEXTURE_2D_ARRAY:
2565 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
2566 break;
2567 case GL_TEXTURE_RECTANGLE_ARB:
2568 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
2569 break;
2570 case GL_TEXTURE_CUBE_MAP:
2571 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
2572 break;
2573 case GL_TEXTURE_CUBE_MAP_ARRAY:
2574 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
2575 break;
2576 case GL_TEXTURE_3D:
2577 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
2578 break;
2579 case GL_TEXTURE_BUFFER:
2580 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
2581 break;
2582 case GL_TEXTURE_2D_MULTISAMPLE:
2583 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
2584 break;
2585 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2586 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
2587 break;
2588 default:
2589 ERR("Unexpected texture target %#x.\n", old_texture_type);
2592 context_gl->texture_type[unit] = target;
2595 checkGLcall("bind texture");
2598 static void wined3d_context_gl_poll_fences(struct wined3d_context_gl *context_gl)
2600 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2601 struct wined3d_command_fence_gl *f;
2602 SIZE_T i;
2604 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2606 f = &context_gl->submitted.fences[i];
2608 if (f->id > device_gl->completed_fence_id)
2610 if (wined3d_fence_test(f->fence, &device_gl->d, 0) != WINED3D_FENCE_OK)
2611 continue;
2612 device_gl->completed_fence_id = f->id;
2615 wined3d_fence_destroy(f->fence);
2616 if (i != context_gl->submitted.fence_count - 1)
2617 *f = context_gl->submitted.fences[context_gl->submitted.fence_count - 1];
2618 --context_gl->submitted.fence_count;
2622 void wined3d_context_gl_wait_command_fence(struct wined3d_context_gl *context_gl, uint64_t id)
2624 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2625 enum wined3d_fence_result ret;
2626 SIZE_T i;
2628 if (id <= device_gl->completed_fence_id
2629 || id > device_gl->current_fence_id) /* In case the fence ID wrapped. */
2630 return;
2632 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2634 if (context_gl->submitted.fences[i].id != id)
2635 continue;
2637 if ((ret = wined3d_fence_wait(context_gl->submitted.fences[i].fence, &device_gl->d)) != WINED3D_FENCE_OK)
2638 ERR("Failed to wait for command fence with id 0x%s, ret %#x.\n", wine_dbgstr_longlong(id), ret);
2639 wined3d_context_gl_poll_fences(context_gl);
2640 return;
2643 ERR("Failed to find fence for command fence with id 0x%s.\n", wine_dbgstr_longlong(id));
2646 void wined3d_context_gl_submit_command_fence(struct wined3d_context_gl *context_gl)
2648 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2649 struct wined3d_command_fence_gl *f;
2650 HRESULT hr;
2652 if (!wined3d_array_reserve((void **)&context_gl->submitted.fences, &context_gl->submitted.fences_size,
2653 context_gl->submitted.fence_count + 1, sizeof(*context_gl->submitted.fences)))
2654 ERR("Failed to grow submitted command buffer array.\n");
2656 f = &context_gl->submitted.fences[context_gl->submitted.fence_count++];
2657 f->id = device_gl->current_fence_id;
2658 if (FAILED(hr = wined3d_fence_create(&device_gl->d, &f->fence)))
2659 ERR("Failed to create fence, hr %#x.\n", hr);
2660 wined3d_fence_issue(f->fence, &device_gl->d);
2662 /* We don't expect this to ever happen, but handle it anyway. */
2663 if (!++device_gl->current_fence_id)
2665 wined3d_context_gl_wait_command_fence(context_gl, device_gl->current_fence_id - 1);
2666 device_gl->completed_fence_id = 0;
2667 device_gl->current_fence_id = 1;
2669 wined3d_context_gl_poll_fences(context_gl);
2672 static void *wined3d_bo_gl_map(struct wined3d_bo_gl *bo, struct wined3d_context_gl *context_gl, uint32_t flags)
2674 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2675 const struct wined3d_gl_info *gl_info;
2676 struct wined3d_bo_user *bo_user;
2677 struct wined3d_bo_gl tmp;
2678 uint8_t *map_ptr;
2680 if (flags & WINED3D_MAP_NOOVERWRITE)
2681 goto map;
2683 if ((flags & WINED3D_MAP_DISCARD) && bo->command_fence_id > device_gl->completed_fence_id)
2685 if (wined3d_context_gl_create_bo(context_gl, bo->size,
2686 bo->binding, bo->usage, bo->b.coherent, bo->flags, &tmp))
2688 list_move_head(&tmp.b.users, &bo->b.users);
2689 wined3d_context_gl_destroy_bo(context_gl, bo);
2690 *bo = tmp;
2691 list_init(&bo->b.users);
2692 list_move_head(&bo->b.users, &tmp.b.users);
2693 LIST_FOR_EACH_ENTRY(bo_user, &bo->b.users, struct wined3d_bo_user, entry)
2695 bo_user->valid = false;
2698 goto map;
2701 ERR("Failed to create new buffer object.\n");
2704 if (bo->command_fence_id == device_gl->current_fence_id)
2705 wined3d_context_gl_submit_command_fence(context_gl);
2706 wined3d_context_gl_wait_command_fence(context_gl, bo->command_fence_id);
2708 map:
2709 gl_info = context_gl->gl_info;
2710 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2712 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2714 map_ptr = GL_EXTCALL(glMapBufferRange(bo->binding, 0, bo->size, wined3d_resource_gl_map_flags(bo, flags)));
2716 else
2718 map_ptr = GL_EXTCALL(glMapBuffer(bo->binding, wined3d_resource_gl_legacy_map_flags(flags)));
2721 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2722 checkGLcall("Map buffer object");
2724 return map_ptr;
2727 void *wined3d_context_gl_map_bo_address(struct wined3d_context_gl *context_gl,
2728 const struct wined3d_bo_address *data, size_t size, uint32_t flags)
2730 struct wined3d_bo *bo;
2731 void *map_ptr;
2733 if (!(bo = data->buffer_object))
2734 return data->addr;
2736 if (!(map_ptr = wined3d_bo_gl_map(wined3d_bo_gl(bo), context_gl, flags)))
2738 ERR("Failed to map bo.\n");
2739 return NULL;
2742 return (uint8_t *)map_ptr + (uintptr_t)data->addr;
2745 static void flush_bo_ranges(struct wined3d_context_gl *context_gl, const struct wined3d_const_bo_address *data,
2746 unsigned int range_count, const struct wined3d_range *ranges)
2748 const struct wined3d_gl_info *gl_info;
2749 struct wined3d_bo_gl *bo;
2750 unsigned int i;
2752 if (!data->buffer_object || data->buffer_object->coherent)
2753 return;
2754 bo = wined3d_bo_gl(data->buffer_object);
2756 gl_info = context_gl->gl_info;
2757 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2759 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2761 /* The offset passed to glFlushMappedBufferRange() is relative to the
2762 * mapped range, but we map the whole buffer anyway. */
2763 for (i = 0; i < range_count; ++i)
2765 GL_EXTCALL(glFlushMappedBufferRange(bo->binding,
2766 bo->b.buffer_offset + (uintptr_t)data->addr + ranges[i].offset, ranges[i].size));
2769 else if (gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
2771 for (i = 0; i < range_count; ++i)
2773 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(bo->binding,
2774 (uintptr_t)data->addr + ranges[i].offset, ranges[i].size));
2775 checkGLcall("glFlushMappedBufferRangeAPPLE");
2779 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2780 checkGLcall("Flush buffer object");
2783 void wined3d_context_gl_unmap_bo_address(struct wined3d_context_gl *context_gl,
2784 const struct wined3d_bo_address *data, unsigned int range_count, const struct wined3d_range *ranges)
2786 const struct wined3d_gl_info *gl_info;
2787 struct wined3d_bo_gl *bo;
2789 if (!data->buffer_object)
2790 return;
2791 bo = wined3d_bo_gl(data->buffer_object);
2793 flush_bo_ranges(context_gl, wined3d_const_bo_address(data), range_count, ranges);
2795 gl_info = context_gl->gl_info;
2796 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2797 GL_EXTCALL(glUnmapBuffer(bo->binding));
2798 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2799 checkGLcall("Unmap buffer object");
2802 void wined3d_context_gl_flush_bo_address(struct wined3d_context_gl *context_gl,
2803 const struct wined3d_const_bo_address *data, size_t size)
2805 struct wined3d_range range;
2807 TRACE("context_gl %p, data %s, size %zu.\n", context_gl, debug_const_bo_address(data), size);
2809 range.offset = (uintptr_t)data->addr;
2810 range.size = size;
2812 flush_bo_ranges(context_gl, data, 1, &range);
2815 void wined3d_context_gl_copy_bo_address(struct wined3d_context_gl *context_gl,
2816 const struct wined3d_bo_address *dst, const struct wined3d_bo_address *src, size_t size)
2818 const struct wined3d_gl_info *gl_info;
2819 struct wined3d_bo_gl *src_bo, *dst_bo;
2820 struct wined3d_range range;
2821 BYTE *dst_ptr, *src_ptr;
2823 gl_info = context_gl->gl_info;
2824 src_bo = src->buffer_object ? wined3d_bo_gl(src->buffer_object) : NULL;
2825 dst_bo = dst->buffer_object ? wined3d_bo_gl(dst->buffer_object) : NULL;
2827 if (dst_bo && src_bo)
2829 if (gl_info->supported[ARB_COPY_BUFFER])
2831 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER, src_bo->id));
2832 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER, dst_bo->id));
2833 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
2834 src_bo->b.buffer_offset + (GLintptr)src->addr,
2835 dst_bo->b.buffer_offset + (GLintptr)dst->addr, size));
2836 checkGLcall("direct buffer copy");
2838 wined3d_context_gl_reference_bo(context_gl, src_bo);
2839 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2841 else
2843 src_ptr = wined3d_context_gl_map_bo_address(context_gl, src, size, WINED3D_MAP_READ);
2844 dst_ptr = wined3d_context_gl_map_bo_address(context_gl, dst, size, WINED3D_MAP_WRITE);
2846 memcpy(dst_ptr, src_ptr, size);
2848 range.offset = 0;
2849 range.size = size;
2850 wined3d_context_gl_unmap_bo_address(context_gl, dst, 1, &range);
2851 wined3d_context_gl_unmap_bo_address(context_gl, src, 0, NULL);
2854 else if (!dst_bo && src_bo)
2856 wined3d_context_gl_bind_bo(context_gl, src_bo->binding, src_bo->id);
2857 GL_EXTCALL(glGetBufferSubData(src_bo->binding, src_bo->b.buffer_offset + (GLintptr)src->addr, size, dst->addr));
2858 checkGLcall("buffer download");
2860 wined3d_context_gl_reference_bo(context_gl, src_bo);
2862 else if (dst_bo && !src_bo)
2864 wined3d_context_gl_bind_bo(context_gl, dst_bo->binding, dst_bo->id);
2865 GL_EXTCALL(glBufferSubData(dst_bo->binding, dst_bo->b.buffer_offset + (GLintptr)dst->addr, size, src->addr));
2866 checkGLcall("buffer upload");
2868 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2870 else
2872 memcpy(dst->addr, src->addr, size);
2876 void wined3d_context_gl_destroy_bo(struct wined3d_context_gl *context_gl, struct wined3d_bo_gl *bo)
2878 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2880 TRACE("context_gl %p, bo %p.\n", context_gl, bo);
2882 TRACE("Destroying GL buffer %u.\n", bo->id);
2883 GL_EXTCALL(glDeleteBuffers(1, &bo->id));
2884 checkGLcall("buffer object destruction");
2885 bo->id = 0;
2888 bool wined3d_context_gl_create_bo(struct wined3d_context_gl *context_gl, GLsizeiptr size,
2889 GLenum binding, GLenum usage, bool coherent, GLbitfield flags, struct wined3d_bo_gl *bo)
2891 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2892 GLuint id = 0;
2894 TRACE("context_gl %p, size %lu, binding %#x, usage %#x, coherent %#x, flags %#x, bo %p.\n",
2895 context_gl, size, binding, usage, coherent, flags, bo);
2897 GL_EXTCALL(glGenBuffers(1, &id));
2898 if (!id)
2900 checkGLcall("buffer object creation");
2901 return false;
2903 wined3d_context_gl_bind_bo(context_gl, binding, id);
2905 if (!coherent && gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
2907 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_FLUSHING_UNMAP_APPLE, GL_FALSE));
2908 GL_EXTCALL(glBufferParameteriAPPLE(binding, GL_BUFFER_SERIALIZED_MODIFY_APPLE, GL_FALSE));
2911 if (gl_info->supported[ARB_BUFFER_STORAGE])
2912 GL_EXTCALL(glBufferStorage(binding, size, NULL, flags | GL_DYNAMIC_STORAGE_BIT));
2913 else
2914 GL_EXTCALL(glBufferData(binding, size, NULL, usage));
2916 wined3d_context_gl_bind_bo(context_gl, binding, 0);
2917 checkGLcall("buffer object creation");
2919 TRACE("Created buffer object %u.\n", id);
2920 bo->id = id;
2921 bo->size = size;
2922 bo->binding = binding;
2923 bo->usage = usage;
2924 bo->flags = flags;
2925 bo->b.coherent = coherent;
2926 list_init(&bo->b.users);
2927 bo->command_fence_id = 0;
2928 bo->b.memory_offset = 0;
2929 bo->b.buffer_offset = 0;
2930 bo->b.map_ptr = NULL;
2932 return true;
2935 static void wined3d_context_gl_set_render_offscreen(struct wined3d_context_gl *context_gl, BOOL offscreen)
2937 if (context_gl->c.render_offscreen == offscreen)
2938 return;
2940 context_invalidate_state(&context_gl->c, STATE_VIEWPORT);
2941 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
2942 if (!context_gl->gl_info->supported[ARB_CLIP_CONTROL])
2944 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
2945 context_invalidate_state(&context_gl->c, STATE_POINTSPRITECOORDORIGIN);
2946 context_invalidate_state(&context_gl->c, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2948 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN));
2949 if (context_gl->gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2950 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
2951 context_gl->c.render_offscreen = offscreen;
2954 GLenum wined3d_context_gl_get_offscreen_gl_buffer(const struct wined3d_context_gl *context_gl)
2956 switch (wined3d_settings.offscreen_rendering_mode)
2958 case ORM_FBO:
2959 return GL_COLOR_ATTACHMENT0;
2961 case ORM_BACKBUFFER:
2962 return context_gl->aux_buffers > 0 ? GL_AUX0 : GL_BACK;
2964 default:
2965 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
2966 return GL_BACK;
2970 static uint32_t wined3d_context_gl_generate_rt_mask_no_fbo(const struct wined3d_context_gl *context_gl,
2971 struct wined3d_resource *rt)
2973 if (!rt || rt->format->id == WINED3DFMT_NULL)
2974 return 0;
2975 else if (rt->type != WINED3D_RTYPE_BUFFER && texture_from_resource(rt)->swapchain)
2976 return context_generate_rt_mask_from_resource(rt);
2977 else
2978 return context_generate_rt_mask(wined3d_context_gl_get_offscreen_gl_buffer(context_gl));
2981 /* Context activation is done by the caller. */
2982 void wined3d_context_gl_apply_blit_state(struct wined3d_context_gl *context_gl, const struct wined3d_device *device)
2984 struct wined3d_context *context = &context_gl->c;
2985 const struct wined3d_gl_info *gl_info;
2986 uint32_t rt_mask, *cur_mask;
2987 struct wined3d_texture *rt;
2988 unsigned int sampler;
2989 SIZE rt_size;
2991 TRACE("Setting up context %p for blitting.\n", context);
2993 gl_info = context_gl->gl_info;
2994 rt = context->current_rt.texture;
2996 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2998 if (context->render_offscreen)
3000 wined3d_texture_load(rt, context, FALSE);
3002 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_FRAMEBUFFER, &rt->resource,
3003 context->current_rt.sub_resource_idx, NULL, 0, rt->resource.draw_binding);
3004 if (rt->resource.format->id != WINED3DFMT_NULL)
3005 rt_mask = 1;
3006 else
3007 rt_mask = 0;
3009 else
3011 context_gl->current_fbo = NULL;
3012 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
3013 rt_mask = context_generate_rt_mask_from_resource(&rt->resource);
3016 else
3018 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, &rt->resource);
3021 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3023 if (rt_mask != *cur_mask)
3025 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3026 *cur_mask = rt_mask;
3029 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3030 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3031 context_invalidate_state(context, STATE_FRAMEBUFFER);
3033 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
3035 if (context->last_was_blit)
3037 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
3039 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
3040 context->viewport_count = WINED3D_MAX_VIEWPORTS;
3041 context_gl->blit_size = rt_size;
3042 /* No need to dirtify here, the states are still dirtified because
3043 * they weren't applied since the last context_apply_blit_state()
3044 * call. */
3046 checkGLcall("blit state application");
3047 TRACE("Context is already set up for blitting, nothing to do.\n");
3048 return;
3050 context->last_was_blit = TRUE;
3052 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
3053 GL_EXTCALL(glBindSampler(0, 0));
3054 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
3056 sampler = context_gl->rev_tex_unit_map[0];
3057 if (sampler != WINED3D_UNMAPPED_STAGE)
3059 if (sampler < WINED3D_MAX_TEXTURES)
3061 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
3062 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
3064 context_invalidate_state(context, STATE_SAMPLER(sampler));
3066 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
3067 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
3069 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3071 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
3072 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
3074 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3075 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3076 context_invalidate_state(context, STATE_BLEND);
3077 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
3078 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
3079 context_invalidate_state(context, STATE_RASTERIZER);
3080 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
3081 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
3082 context_invalidate_state(context, STATE_DEPTH_STENCIL);
3083 if (gl_info->supported[ARB_POINT_SPRITE])
3085 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
3086 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
3088 if (gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3090 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3091 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3094 context->last_was_rhw = TRUE;
3095 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
3097 wined3d_context_gl_enable_clip_distances(context_gl, 0);
3098 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
3100 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
3101 if (gl_info->supported[ARB_CLIP_CONTROL])
3102 GL_EXTCALL(glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE));
3103 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
3104 context->viewport_count = WINED3D_MAX_VIEWPORTS;
3105 context_invalidate_state(context, STATE_VIEWPORT);
3107 device->shader_backend->shader_disable(device->shader_priv, context);
3109 context_gl->blit_size = rt_size;
3111 checkGLcall("blit state application");
3114 static void wined3d_context_gl_apply_blit_projection(const struct wined3d_context_gl *context_gl,
3115 unsigned int w, unsigned int h)
3117 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3118 const GLdouble projection[] =
3120 2.0 / w, 0.0, 0.0, 0.0,
3121 0.0, 2.0 / h, 0.0, 0.0,
3122 0.0, 0.0, 2.0, 0.0,
3123 -1.0, -1.0, -1.0, 1.0,
3126 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
3127 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
3130 /* Setup OpenGL states for fixed-function blitting. */
3131 /* Context activation is done by the caller. */
3132 void wined3d_context_gl_apply_ffp_blit_state(struct wined3d_context_gl *context_gl,
3133 const struct wined3d_device *device)
3135 struct wined3d_context *context = &context_gl->c;
3136 const struct wined3d_gl_info *gl_info;
3137 unsigned int i, sampler;
3139 gl_info = context_gl->gl_info;
3140 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3141 ERR("Applying fixed-function state without legacy context support.\n");
3143 if (context->last_was_ffp_blit)
3145 SIZE rt_size;
3147 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
3148 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
3149 wined3d_context_gl_apply_blit_projection(context_gl, rt_size.cx, rt_size.cy);
3150 wined3d_context_gl_apply_blit_state(context_gl, device);
3152 checkGLcall("ffp blit state application");
3153 return;
3155 context->last_was_ffp_blit = TRUE;
3157 wined3d_context_gl_apply_blit_state(context_gl, device);
3159 /* Disable all textures. The caller can then bind a texture it wants to blit
3160 * from. */
3161 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
3163 wined3d_context_gl_active_texture(context_gl, gl_info, i);
3165 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3166 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3167 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3168 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3169 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3170 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3172 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3174 sampler = context_gl->rev_tex_unit_map[i];
3175 if (sampler != WINED3D_UNMAPPED_STAGE)
3177 if (sampler < WINED3D_MAX_TEXTURES)
3178 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
3179 context_invalidate_state(context, STATE_SAMPLER(sampler));
3183 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
3185 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3186 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3187 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3188 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3189 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3190 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3192 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3193 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
3194 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
3196 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
3197 gl_info->gl_ops.gl.p_glLoadIdentity();
3199 /* Setup transforms. */
3200 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3201 gl_info->gl_ops.gl.p_glLoadIdentity();
3202 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3203 wined3d_context_gl_apply_blit_projection(context_gl, context_gl->blit_size.cx, context_gl->blit_size.cy);
3204 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
3206 /* Other misc states. */
3207 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
3208 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
3209 gl_info->p_glDisableWINE(GL_FOG);
3210 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
3212 if (gl_info->supported[EXT_SECONDARY_COLOR])
3214 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
3215 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
3217 checkGLcall("ffp blit state application");
3220 static BOOL have_framebuffer_attachment(unsigned int rt_count, struct wined3d_rendertarget_view * const *rts,
3221 const struct wined3d_rendertarget_view *ds)
3223 unsigned int i;
3225 if (ds)
3226 return TRUE;
3228 for (i = 0; i < rt_count; ++i)
3230 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3231 return TRUE;
3234 return FALSE;
3237 /* Context activation is done by the caller. */
3238 BOOL wined3d_context_gl_apply_clear_state(struct wined3d_context_gl *context_gl,
3239 const struct wined3d_state *state, unsigned int rt_count, const struct wined3d_fb_state *fb)
3241 struct wined3d_rendertarget_view * const *rts = fb->render_targets;
3242 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3243 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
3244 uint32_t rt_mask = 0, *cur_mask;
3245 unsigned int i;
3247 if (isStateDirty(&context_gl->c, STATE_FRAMEBUFFER) || fb != &state->fb
3248 || rt_count != gl_info->limits.buffers)
3250 if (!have_framebuffer_attachment(rt_count, rts, dsv))
3252 WARN("Invalid render target config, need at least one attachment.\n");
3253 return FALSE;
3256 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3258 struct wined3d_rendertarget_info ds_info = {{0}};
3260 if (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource))
3262 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3263 for (i = 0; i < rt_count; ++i)
3265 if (rts[i])
3267 struct wined3d_rendertarget_view_gl *rtv_gl = wined3d_rendertarget_view_gl(rts[i]);
3268 context_gl->blit_targets[i].gl_view = rtv_gl->gl_view;
3269 context_gl->blit_targets[i].resource = rtv_gl->v.resource;
3270 context_gl->blit_targets[i].sub_resource_idx = rtv_gl->v.sub_resource_idx;
3271 context_gl->blit_targets[i].layer_count = rtv_gl->v.layer_count;
3273 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3274 rt_mask |= (1u << i);
3277 if (dsv)
3279 struct wined3d_rendertarget_view_gl *dsv_gl = wined3d_rendertarget_view_gl(dsv);
3280 ds_info.gl_view = dsv_gl->gl_view;
3281 ds_info.resource = dsv_gl->v.resource;
3282 ds_info.sub_resource_idx = dsv_gl->v.sub_resource_idx;
3283 ds_info.layer_count = dsv_gl->v.layer_count;
3286 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3287 rt_count ? rts[0]->resource->draw_binding : 0, dsv ? dsv->resource->draw_binding : 0);
3289 else
3291 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3292 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3293 rt_mask = context_generate_rt_mask_from_resource(rts[0]->resource);
3296 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3297 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3298 * state management allows this */
3299 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3301 else
3303 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3306 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3307 && (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource)))
3309 for (i = 0; i < rt_count; ++i)
3311 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3312 rt_mask |= (1u << i);
3315 else
3317 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3320 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3322 if (rt_mask != *cur_mask)
3324 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3325 *cur_mask = rt_mask;
3326 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3329 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3330 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3332 context_gl->c.last_was_blit = FALSE;
3333 context_gl->c.last_was_ffp_blit = FALSE;
3335 /* Blending and clearing should be orthogonal, but tests on the nvidia
3336 * driver show that disabling blending when clearing improves the clearing
3337 * performance incredibly. */
3338 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3339 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
3340 if (rt_count && gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3342 if (needs_srgb_write(context_gl->c.d3d_info, state, fb))
3343 gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
3344 else
3345 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3346 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3348 checkGLcall("setting up state for clear");
3350 context_invalidate_state(&context_gl->c, STATE_BLEND);
3351 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3352 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
3354 return TRUE;
3357 static uint32_t find_draw_buffers_mask(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3359 struct wined3d_rendertarget_view * const *rts = state->fb.render_targets;
3360 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3361 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3362 unsigned int rt_mask, mask;
3363 unsigned int i;
3365 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3366 return wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rts[0]->resource);
3367 else if (!context_gl->c.render_offscreen)
3368 return context_generate_rt_mask_from_resource(rts[0]->resource);
3370 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
3371 rt_mask &= wined3d_mask_from_size(gl_info->limits.buffers);
3372 if (state->blend_state && state->blend_state->dual_source)
3373 rt_mask = 1;
3375 mask = rt_mask;
3376 while (mask)
3378 i = wined3d_bit_scan(&mask);
3379 if (!rts[i] || rts[i]->format->id == WINED3DFMT_NULL)
3380 rt_mask &= ~(1u << i);
3383 return rt_mask;
3386 /* Context activation is done by the caller. */
3387 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3389 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3390 uint32_t rt_mask = find_draw_buffers_mask(context_gl, state);
3391 const struct wined3d_fb_state *fb = &state->fb;
3392 DWORD color_location = 0;
3393 DWORD *cur_mask;
3395 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3397 struct wined3d_rendertarget_info ds_info = {{0}};
3399 if (!context->render_offscreen)
3401 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3402 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3404 else
3406 const struct wined3d_rendertarget_view_gl *view_gl;
3407 unsigned int i;
3409 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3410 for (i = 0; i < context_gl->gl_info->limits.buffers; ++i)
3412 if (!fb->render_targets[i])
3413 continue;
3415 view_gl = wined3d_rendertarget_view_gl(fb->render_targets[i]);
3416 context_gl->blit_targets[i].gl_view = view_gl->gl_view;
3417 context_gl->blit_targets[i].resource = view_gl->v.resource;
3418 context_gl->blit_targets[i].sub_resource_idx = view_gl->v.sub_resource_idx;
3419 context_gl->blit_targets[i].layer_count = view_gl->v.layer_count;
3421 if (!color_location)
3422 color_location = view_gl->v.resource->draw_binding;
3425 if (fb->depth_stencil)
3427 view_gl = wined3d_rendertarget_view_gl(fb->depth_stencil);
3428 ds_info.gl_view = view_gl->gl_view;
3429 ds_info.resource = view_gl->v.resource;
3430 ds_info.sub_resource_idx = view_gl->v.sub_resource_idx;
3431 ds_info.layer_count = view_gl->v.layer_count;
3434 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3435 color_location, fb->depth_stencil ? fb->depth_stencil->resource->draw_binding : 0);
3439 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3440 if (rt_mask != *cur_mask)
3442 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3443 *cur_mask = rt_mask;
3445 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
3448 static void wined3d_context_gl_map_stage(struct wined3d_context_gl *context_gl, unsigned int stage, unsigned int unit)
3450 unsigned int i = context_gl->rev_tex_unit_map[unit];
3451 unsigned int j = context_gl->tex_unit_map[stage];
3453 TRACE("Mapping stage %u to unit %u.\n", stage, unit);
3454 context_gl->tex_unit_map[stage] = unit;
3455 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
3456 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
3458 context_gl->rev_tex_unit_map[unit] = stage;
3459 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
3460 context_gl->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
3463 static void context_invalidate_texture_stage(struct wined3d_context *context, DWORD stage)
3465 DWORD i;
3467 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
3468 context_invalidate_state(context, STATE_TEXTURESTAGE(stage, i));
3471 static void context_update_fixed_function_usage_map(struct wined3d_context *context,
3472 const struct wined3d_state *state)
3474 UINT i, start, end;
3476 context->fixed_function_usage_map = 0;
3477 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
3479 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
3480 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
3481 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
3482 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
3483 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
3484 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
3485 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
3486 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
3488 /* Not used, and disable higher stages. */
3489 if (color_op == WINED3D_TOP_DISABLE)
3490 break;
3492 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
3493 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
3494 || ((color_arg3 == WINED3DTA_TEXTURE)
3495 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
3496 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
3497 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
3498 || ((alpha_arg3 == WINED3DTA_TEXTURE)
3499 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
3500 context->fixed_function_usage_map |= (1u << i);
3502 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
3503 && i < WINED3D_MAX_TEXTURES - 1)
3504 context->fixed_function_usage_map |= (1u << (i + 1));
3507 if (i < context->lowest_disabled_stage)
3509 start = i;
3510 end = context->lowest_disabled_stage;
3512 else
3514 start = context->lowest_disabled_stage;
3515 end = i;
3518 context->lowest_disabled_stage = i;
3519 for (i = start + 1; i < end; ++i)
3521 context_invalidate_state(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3525 static void wined3d_context_gl_map_fixed_function_samplers(struct wined3d_context_gl *context_gl,
3526 const struct wined3d_state *state)
3528 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3529 unsigned int i, tex;
3530 WORD ffu_map;
3532 ffu_map = context_gl->c.fixed_function_usage_map;
3534 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
3535 || context_gl->c.lowest_disabled_stage <= d3d_info->limits.ffp_textures)
3537 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3539 if (!(ffu_map & 1))
3540 continue;
3542 if (context_gl->tex_unit_map[i] != i)
3544 wined3d_context_gl_map_stage(context_gl, i, i);
3545 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3546 context_invalidate_texture_stage(&context_gl->c, i);
3549 return;
3552 /* Now work out the mapping */
3553 tex = 0;
3554 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3556 if (!(ffu_map & 1))
3557 continue;
3559 if (context_gl->tex_unit_map[i] != tex)
3561 wined3d_context_gl_map_stage(context_gl, i, tex);
3562 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3563 context_invalidate_texture_stage(&context_gl->c, i);
3566 ++tex;
3570 static void wined3d_context_gl_map_psamplers(struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3572 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3573 const struct wined3d_shader_resource_info *resource_info =
3574 state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3575 unsigned int i;
3577 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
3579 if (resource_info[i].type && context_gl->tex_unit_map[i] != i)
3581 wined3d_context_gl_map_stage(context_gl, i, i);
3582 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3583 if (i < d3d_info->limits.ffp_blend_stages)
3584 context_invalidate_texture_stage(&context_gl->c, i);
3589 static BOOL wined3d_context_gl_unit_free_for_vs(const struct wined3d_context_gl *context_gl,
3590 const struct wined3d_shader_resource_info *ps_resource_info, unsigned int unit)
3592 unsigned int current_mapping = context_gl->rev_tex_unit_map[unit];
3594 /* Not currently used */
3595 if (current_mapping == WINED3D_UNMAPPED_STAGE)
3596 return TRUE;
3598 if (current_mapping < WINED3D_MAX_FRAGMENT_SAMPLERS)
3600 /* Used by a fragment sampler */
3602 if (!ps_resource_info)
3604 /* No pixel shader, check fixed function */
3605 return current_mapping >= WINED3D_MAX_TEXTURES
3606 || !(context_gl->c.fixed_function_usage_map & (1u << current_mapping));
3609 /* Pixel shader, check the shader's sampler map */
3610 return !ps_resource_info[current_mapping].type;
3613 return TRUE;
3616 static void wined3d_context_gl_map_vsamplers(struct wined3d_context_gl *context_gl,
3617 BOOL ps, const struct wined3d_state *state)
3619 const struct wined3d_shader_resource_info *vs_resource_info =
3620 state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info;
3621 const struct wined3d_shader_resource_info *ps_resource_info = NULL;
3622 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3623 int start = min(WINED3D_MAX_COMBINED_SAMPLERS, gl_info->limits.graphics_samplers) - 1;
3624 int i;
3626 /* Note that we only care if a resource is used or not, not the
3627 * resource's specific type. Otherwise we'd need to call
3628 * shader_update_samplers() here for 1.x pixelshaders. */
3629 if (ps)
3630 ps_resource_info = state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3632 for (i = 0; i < WINED3D_MAX_VERTEX_SAMPLERS; ++i)
3634 DWORD vsampler_idx = i + WINED3D_MAX_FRAGMENT_SAMPLERS;
3635 if (vs_resource_info[i].type)
3637 while (start >= 0)
3639 if (wined3d_context_gl_unit_free_for_vs(context_gl, ps_resource_info, start))
3641 if (context_gl->tex_unit_map[vsampler_idx] != start)
3643 wined3d_context_gl_map_stage(context_gl, vsampler_idx, start);
3644 context_invalidate_state(&context_gl->c, STATE_SAMPLER(vsampler_idx));
3647 --start;
3648 break;
3651 --start;
3653 if (context_gl->tex_unit_map[vsampler_idx] == WINED3D_UNMAPPED_STAGE)
3654 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i);
3659 static void wined3d_context_gl_update_tex_unit_map(struct wined3d_context_gl *context_gl,
3660 const struct wined3d_state *state)
3662 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3663 BOOL vs = use_vs(state);
3664 BOOL ps = use_ps(state);
3666 if (!ps)
3667 context_update_fixed_function_usage_map(&context_gl->c, state);
3669 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3670 * need a 1:1 map at the moment.
3671 * When the mapping of a stage is changed, sampler and ALL texture stage
3672 * states have to be reset. */
3674 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
3675 return;
3677 if (ps)
3678 wined3d_context_gl_map_psamplers(context_gl, state);
3679 else
3680 wined3d_context_gl_map_fixed_function_samplers(context_gl, state);
3682 if (vs)
3683 wined3d_context_gl_map_vsamplers(context_gl, ps, state);
3686 /* Context activation is done by the caller. */
3687 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3689 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3690 uint32_t rt_mask, *cur_mask;
3692 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
3694 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3695 rt_mask = find_draw_buffers_mask(context_gl, state);
3696 if (rt_mask != *cur_mask)
3698 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3699 *cur_mask = rt_mask;
3703 static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl *context_gl,
3704 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
3706 unsigned int bind_idx, shader_sampler_count, base, count, i;
3707 const struct wined3d_device *device = context_gl->c.device;
3708 struct wined3d_shader_sampler_map_entry *entry;
3709 struct wined3d_shader_resource_view *view;
3710 const struct wined3d_shader *shader;
3711 const unsigned int *tex_unit_map;
3712 struct wined3d_sampler *sampler;
3714 if (!(shader = state->shader[shader_type]))
3715 return;
3717 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl,
3718 &shader->reg_maps.shader_version, &base, &count);
3720 shader_sampler_count = shader->reg_maps.sampler_map.count;
3721 if (shader_sampler_count > count)
3722 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3723 shader, shader_sampler_count, count);
3724 count = min(shader_sampler_count, count);
3726 for (i = 0; i < count; ++i)
3728 entry = &shader->reg_maps.sampler_map.entries[i];
3729 bind_idx = base + entry->bind_idx;
3730 if (tex_unit_map)
3731 bind_idx = tex_unit_map[bind_idx];
3733 if (!(view = state->shader_resource_view[shader_type][entry->resource_idx]))
3735 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
3736 continue;
3739 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
3740 sampler = device->default_sampler;
3741 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
3742 sampler = device->null_sampler;
3743 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view),
3744 bind_idx, wined3d_sampler_gl(sampler), context_gl);
3748 static void wined3d_context_gl_bind_unordered_access_views(struct wined3d_context_gl *context_gl,
3749 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3751 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3752 struct wined3d_unordered_access_view_gl *view_gl;
3753 const struct wined3d_format_gl *format_gl;
3754 GLuint texture_name;
3755 unsigned int i;
3756 GLint level;
3758 if (!shader)
3759 return;
3761 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3763 if (!views[i])
3765 if (shader->reg_maps.uav_resource_info[i].type)
3766 WARN("No unordered access view bound at index %u.\n", i);
3767 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3768 continue;
3771 view_gl = wined3d_unordered_access_view_gl(views[i]);
3772 if (view_gl->gl_view.name)
3774 texture_name = view_gl->gl_view.name;
3775 level = 0;
3777 else if (view_gl->v.resource->type != WINED3D_RTYPE_BUFFER)
3779 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(view_gl->v.resource));
3780 texture_name = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, FALSE);
3781 level = view_gl->v.desc.u.texture.level_idx;
3783 else
3785 FIXME("Unsupported buffer unordered access view.\n");
3786 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3787 continue;
3790 format_gl = wined3d_format_gl(view_gl->v.format);
3791 GL_EXTCALL(glBindImageTexture(i, texture_name, level, GL_TRUE, 0, GL_READ_WRITE,
3792 format_gl->internal));
3794 if (view_gl->counter_bo.id)
3795 GL_EXTCALL(glBindBufferRange(GL_ATOMIC_COUNTER_BUFFER, i, view_gl->counter_bo.id,
3796 view_gl->counter_bo.b.buffer_offset, view_gl->counter_bo.size));
3798 checkGLcall("Bind unordered access views");
3801 static void context_gl_load_shader_resources(struct wined3d_context_gl *context_gl,
3802 const struct wined3d_state *state, unsigned int shader_mask)
3804 struct wined3d_shader_sampler_map_entry *entry;
3805 struct wined3d_shader_resource_view_gl *srv_gl;
3806 struct wined3d_shader_resource_view *view;
3807 struct wined3d_shader *shader;
3808 struct wined3d_buffer *buffer;
3809 unsigned int i, j;
3811 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
3813 if (!(shader_mask & (1u << i)))
3814 continue;
3816 if (!(shader = state->shader[i]))
3817 continue;
3819 for (j = 0; j < WINED3D_MAX_CBS; ++j)
3821 if (!state->cb[i][j].buffer)
3822 continue;
3824 buffer = state->cb[i][j].buffer;
3825 wined3d_buffer_load(buffer, &context_gl->c, state);
3826 wined3d_context_gl_reference_buffer(context_gl, buffer);
3827 if (!buffer->bo_user.valid)
3828 device_invalidate_state(context_gl->c.device, STATE_CONSTANT_BUFFER(i));
3831 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
3833 entry = &shader->reg_maps.sampler_map.entries[j];
3835 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
3836 continue;
3838 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3840 buffer = buffer_from_resource(view->resource);
3841 wined3d_buffer_load(buffer, &context_gl->c, state);
3842 wined3d_context_gl_reference_buffer(context_gl, buffer);
3844 srv_gl = wined3d_shader_resource_view_gl(view);
3845 if (!srv_gl->bo_user.valid)
3846 wined3d_shader_resource_view_gl_update(srv_gl, context_gl);
3848 else
3850 wined3d_texture_load(texture_from_resource(view->resource), &context_gl->c, FALSE);
3856 static void context_gl_load_unordered_access_resources(struct wined3d_context_gl *context_gl,
3857 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3859 struct wined3d_unordered_access_view_gl *uav_gl;
3860 struct wined3d_unordered_access_view *view;
3861 struct wined3d_texture *texture;
3862 struct wined3d_buffer *buffer;
3863 unsigned int i;
3865 context_gl->c.uses_uavs = 0;
3867 if (!shader)
3868 return;
3870 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3872 if (!(view = views[i]))
3873 continue;
3875 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3877 buffer = buffer_from_resource(view->resource);
3878 wined3d_buffer_load_location(buffer, &context_gl->c, WINED3D_LOCATION_BUFFER);
3879 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
3880 wined3d_context_gl_reference_buffer(context_gl, buffer);
3882 uav_gl = wined3d_unordered_access_view_gl(view);
3883 if (!uav_gl->bo_user.valid)
3884 wined3d_unordered_access_view_gl_update(uav_gl, context_gl);
3886 else
3888 texture = texture_from_resource(view->resource);
3889 wined3d_texture_load(texture, &context_gl->c, FALSE);
3890 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_TEXTURE_RGB);
3893 context_gl->c.uses_uavs = 1;
3897 static void context_gl_load_stream_output_buffers(struct wined3d_context_gl *context_gl,
3898 const struct wined3d_state *state)
3900 struct wined3d_buffer *buffer;
3901 unsigned int i;
3903 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3905 if (!(buffer = state->stream_output[i].buffer))
3906 continue;
3908 wined3d_buffer_load(buffer, &context_gl->c, state);
3909 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
3910 wined3d_context_gl_reference_buffer(context_gl, buffer);
3911 if (!buffer->bo_user.valid)
3912 device_invalidate_state(context_gl->c.device, STATE_STREAM_OUTPUT);
3916 /* Context activation is done by the caller. */
3917 static BOOL context_apply_draw_state(struct wined3d_context *context,
3918 const struct wined3d_device *device, const struct wined3d_state *state, BOOL indexed)
3920 const struct wined3d_state_entry *state_table = context->state_table;
3921 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3922 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3923 const struct wined3d_fb_state *fb = &state->fb;
3924 unsigned int i, base;
3925 uint32_t map;
3927 context->uses_fbo_attached_resources = 0;
3929 if (!have_framebuffer_attachment(gl_info->limits.buffers, fb->render_targets, fb->depth_stencil))
3931 if (!gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
3933 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
3934 return FALSE;
3937 wined3d_context_gl_set_render_offscreen(context_gl, TRUE);
3940 /* Preload resources before FBO setup. Texture preload in particular may
3941 * result in changes to the current FBO, due to using e.g. FBO blits for
3942 * updating a resource location. */
3943 wined3d_context_gl_update_tex_unit_map(context_gl, state);
3944 context_preload_textures(context, state);
3945 context_gl_load_shader_resources(context_gl, state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
3946 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_PIXEL],
3947 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3948 context_gl_load_stream_output_buffers(context_gl, state);
3949 /* TODO: Right now the dependency on the vertex shader is necessary
3950 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3951 * the current VS but maybe it's possible to relax the coupling in some
3952 * situations at least. */
3953 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC)
3954 || isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
3956 context_update_stream_info(context, state);
3959 map = context->stream_info.use_map;
3960 while (map)
3962 const struct wined3d_stream_info_element *e;
3963 struct wined3d_buffer *buffer;
3965 e = &context->stream_info.elements[wined3d_bit_scan(&map)];
3966 buffer = state->streams[e->stream_idx].buffer;
3968 if (!buffer->bo_user.valid)
3969 device_invalidate_state(device, STATE_STREAMSRC);
3970 else
3971 wined3d_buffer_load(buffer, context, state);
3973 /* Loading the buffers above may have invalidated the stream info. */
3974 if (wined3d_context_is_graphics_state_dirty(context, STATE_STREAMSRC))
3975 context_update_stream_info(context, state);
3977 map = context->stream_info.use_map;
3978 while (map)
3980 const struct wined3d_stream_info_element *e;
3981 struct wined3d_buffer *buffer;
3983 e = &context->stream_info.elements[wined3d_bit_scan(&map)];
3984 buffer = state->streams[e->stream_idx].buffer;
3986 wined3d_context_gl_reference_buffer(context_gl, buffer);
3989 if (indexed && state->index_buffer)
3991 struct wined3d_buffer *buffer = state->index_buffer;
3993 if (context->stream_info.all_vbo)
3995 wined3d_buffer_load(buffer, context, state);
3996 if (!buffer->bo_user.valid)
3997 device_invalidate_state(device, STATE_INDEXBUFFER);
3998 wined3d_context_gl_reference_buffer(context_gl, buffer);
4000 else
4002 wined3d_buffer_load_sysmem(buffer, context);
4006 for (i = 0, base = 0; i < ARRAY_SIZE(context->dirty_graphics_states); ++i)
4008 uint32_t dirty_mask = context->dirty_graphics_states[i];
4010 while (dirty_mask)
4012 unsigned int state_id = base + wined3d_bit_scan(&dirty_mask);
4014 state_table[state_id].apply(context, state, state_id);
4016 base += sizeof(dirty_mask) * CHAR_BIT;
4019 memset(context->dirty_graphics_states, 0, sizeof(context->dirty_graphics_states));
4021 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
4023 device->shader_backend->shader_select(device->shader_priv, context, state);
4024 context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
4027 if (context->constant_update_mask)
4029 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
4030 context->constant_update_mask = 0;
4033 if (context->update_shader_resource_bindings)
4035 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
4036 wined3d_context_gl_bind_shader_resources(context_gl, state, i);
4037 context->update_shader_resource_bindings = 0;
4038 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4039 context->update_compute_shader_resource_bindings = 1;
4042 if (context->update_unordered_access_view_bindings)
4044 wined3d_context_gl_bind_unordered_access_views(context_gl,
4045 state->shader[WINED3D_SHADER_TYPE_PIXEL],
4046 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
4047 context->update_unordered_access_view_bindings = 0;
4048 context->update_compute_unordered_access_view_bindings = 1;
4051 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4052 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
4054 context->last_was_blit = FALSE;
4055 context->last_was_ffp_blit = FALSE;
4057 return TRUE;
4060 static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl *context_gl,
4061 const struct wined3d_device *device, const struct wined3d_state *state)
4063 const struct wined3d_state_entry *state_table = context_gl->c.state_table;
4064 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4065 unsigned int state_id, i;
4067 context_gl_load_shader_resources(context_gl, state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
4068 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4069 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4071 for (i = 0, state_id = STATE_COMPUTE_OFFSET; i < ARRAY_SIZE(context_gl->c.dirty_compute_states); ++i)
4073 unsigned int dirty_mask = context_gl->c.dirty_compute_states[i];
4075 while (dirty_mask)
4077 unsigned int current_state_id = state_id + wined3d_bit_scan(&dirty_mask);
4078 state_table[current_state_id].apply(&context_gl->c, state, current_state_id);
4080 state_id += sizeof(*context_gl->c.dirty_compute_states) * CHAR_BIT;
4082 memset(context_gl->c.dirty_compute_states, 0, sizeof(*context_gl->c.dirty_compute_states));
4084 if (context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
4086 device->shader_backend->shader_select_compute(device->shader_priv, &context_gl->c, state);
4087 context_gl->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
4090 if (context_gl->c.update_compute_shader_resource_bindings)
4092 wined3d_context_gl_bind_shader_resources(context_gl, state, WINED3D_SHADER_TYPE_COMPUTE);
4093 context_gl->c.update_compute_shader_resource_bindings = 0;
4094 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4095 context_gl->c.update_shader_resource_bindings = 1;
4098 if (context_gl->c.update_compute_unordered_access_view_bindings)
4100 wined3d_context_gl_bind_unordered_access_views(context_gl,
4101 state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4102 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4103 context_gl->c.update_compute_unordered_access_view_bindings = 0;
4104 context_gl->c.update_unordered_access_view_bindings = 1;
4107 /* Updates to currently bound render targets aren't necessarily coherent
4108 * between the graphics and compute pipelines. Unbind any currently bound
4109 * FBO here to ensure preceding updates to its attachments by the graphics
4110 * pipeline are visible to the compute pipeline.
4112 * Without this, the bloom effect in Nier:Automata is too bright on the
4113 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4114 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
4115 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
4117 context_gl->c.last_was_blit = FALSE;
4118 context_gl->c.last_was_ffp_blit = FALSE;
4121 void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl *context_gl)
4123 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4125 if (context_gl->c.transform_feedback_active)
4127 GL_EXTCALL(glEndTransformFeedback());
4128 checkGLcall("glEndTransformFeedback");
4129 context_gl->c.transform_feedback_active = 0;
4130 context_gl->c.transform_feedback_paused = 0;
4134 static void wined3d_context_gl_pause_transform_feedback(struct wined3d_context_gl *context_gl, BOOL force)
4136 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4138 if (!context_gl->c.transform_feedback_active || context_gl->c.transform_feedback_paused)
4139 return;
4141 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK2])
4143 GL_EXTCALL(glPauseTransformFeedback());
4144 checkGLcall("glPauseTransformFeedback");
4145 context_gl->c.transform_feedback_paused = 1;
4146 return;
4149 WARN("Cannot pause transform feedback operations.\n");
4151 if (force)
4152 wined3d_context_gl_end_transform_feedback(context_gl);
4155 static void wined3d_context_gl_setup_target(struct wined3d_context_gl *context_gl,
4156 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4158 BOOL old_render_offscreen = context_gl->c.render_offscreen, render_offscreen;
4160 render_offscreen = wined3d_resource_is_offscreen(&texture->resource);
4161 if (context_gl->c.current_rt.texture == texture
4162 && context_gl->c.current_rt.sub_resource_idx == sub_resource_idx
4163 && render_offscreen == old_render_offscreen)
4164 return;
4166 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4167 * the alpha blend state changes with different render target formats. */
4168 if (!context_gl->c.current_rt.texture)
4170 context_invalidate_state(&context_gl->c, STATE_BLEND);
4172 else
4174 const struct wined3d_format *old = context_gl->c.current_rt.texture->resource.format;
4175 const struct wined3d_format *new = texture->resource.format;
4177 if (old->id != new->id)
4179 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4180 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
4181 || !(texture->resource.format_flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
4182 context_invalidate_state(&context_gl->c, STATE_BLEND);
4185 /* When switching away from an offscreen render target, and we're not
4186 * using FBOs, we have to read the drawable into the texture. This is
4187 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4188 * There are some things that need care though. PreLoad needs a GL context,
4189 * and FindContext is called before the context is activated. It also
4190 * has to be called with the old rendertarget active, otherwise a
4191 * wrong drawable is read. */
4192 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4193 && old_render_offscreen && (context_gl->c.current_rt.texture != texture
4194 || context_gl->c.current_rt.sub_resource_idx != sub_resource_idx))
4196 struct wined3d_texture_gl *prev_texture = wined3d_texture_gl(context_gl->c.current_rt.texture);
4197 unsigned int prev_sub_resource_idx = context_gl->c.current_rt.sub_resource_idx;
4199 /* Read the back buffer of the old drawable into the destination texture. */
4200 if (prev_texture->texture_srgb.name)
4201 wined3d_texture_load(&prev_texture->t, &context_gl->c, TRUE);
4202 wined3d_texture_load(&prev_texture->t, &context_gl->c, FALSE);
4203 wined3d_texture_invalidate_location(&prev_texture->t, prev_sub_resource_idx, WINED3D_LOCATION_DRAWABLE);
4207 context_gl->c.current_rt.texture = texture;
4208 context_gl->c.current_rt.sub_resource_idx = sub_resource_idx;
4209 wined3d_context_gl_set_render_offscreen(context_gl, render_offscreen);
4212 static void wined3d_context_gl_activate(struct wined3d_context_gl *context_gl,
4213 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4215 wined3d_context_gl_enter(context_gl);
4216 wined3d_context_gl_update_window(context_gl);
4217 wined3d_context_gl_setup_target(context_gl, texture, sub_resource_idx);
4218 if (!context_gl->valid)
4219 return;
4221 if (context_gl != wined3d_context_gl_get_current())
4223 if (!wined3d_context_gl_set_current(context_gl))
4224 ERR("Failed to activate the new context.\n");
4226 else if (context_gl->needs_set)
4228 wined3d_context_gl_set_gl_context(context_gl);
4232 struct wined3d_context *wined3d_context_gl_acquire(const struct wined3d_device *device,
4233 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4235 struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
4236 struct wined3d_context_gl *context_gl;
4238 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device, texture, sub_resource_idx);
4240 if (current_context && current_context->c.destroyed)
4241 current_context = NULL;
4243 if (!texture)
4245 if (current_context
4246 && current_context->c.current_rt.texture
4247 && current_context->c.device == device)
4249 texture = current_context->c.current_rt.texture;
4250 sub_resource_idx = current_context->c.current_rt.sub_resource_idx;
4252 else
4254 struct wined3d_swapchain *swapchain = device->swapchains[0];
4256 if (swapchain->back_buffers)
4257 texture = swapchain->back_buffers[0];
4258 else
4259 texture = swapchain->front_buffer;
4260 sub_resource_idx = 0;
4264 if (current_context && current_context->c.current_rt.texture == texture)
4266 context_gl = current_context;
4268 else if (!wined3d_resource_is_offscreen(&texture->resource))
4270 TRACE("Rendering onscreen.\n");
4272 if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(texture->swapchain))))
4273 return NULL;
4275 else
4277 TRACE("Rendering offscreen.\n");
4279 /* Stay with the current context if possible. Otherwise use the
4280 * context for the primary swapchain. */
4281 if (current_context && current_context->c.device == device)
4282 context_gl = current_context;
4283 else if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(device->swapchains[0]))))
4284 return NULL;
4287 wined3d_context_gl_activate(context_gl, texture, sub_resource_idx);
4289 return &context_gl->c;
4292 struct wined3d_context_gl *wined3d_context_gl_reacquire(struct wined3d_context_gl *context_gl)
4294 struct wined3d_context *acquired_context;
4295 struct wined3d_device *device;
4297 if (!context_gl || context_gl->tid != GetCurrentThreadId())
4298 return NULL;
4300 device = context_gl->c.device;
4301 wined3d_from_cs(device->cs);
4303 if (context_gl->c.current_rt.texture)
4305 wined3d_context_gl_activate(context_gl, context_gl->c.current_rt.texture,
4306 context_gl->c.current_rt.sub_resource_idx);
4307 return context_gl;
4310 acquired_context = context_acquire(device, NULL, 0);
4311 if (acquired_context != &context_gl->c)
4312 ERR("Acquired context %p instead of %p.\n", acquired_context, &context_gl->c);
4313 return wined3d_context_gl(acquired_context);
4316 void dispatch_compute(struct wined3d_device *device, const struct wined3d_state *state,
4317 const struct wined3d_dispatch_parameters *parameters)
4319 const struct wined3d_gl_info *gl_info;
4320 struct wined3d_context_gl *context_gl;
4322 context_gl = wined3d_context_gl(context_acquire(device, NULL, 0));
4323 if (!context_gl->valid)
4325 context_release(&context_gl->c);
4326 WARN("Invalid context, skipping dispatch.\n");
4327 return;
4329 gl_info = context_gl->gl_info;
4331 if (!gl_info->supported[ARB_COMPUTE_SHADER])
4333 context_release(&context_gl->c);
4334 FIXME("OpenGL implementation does not support compute shaders.\n");
4335 return;
4338 if (parameters->indirect)
4339 wined3d_buffer_load(parameters->u.indirect.buffer, &context_gl->c, state);
4341 wined3d_context_gl_apply_compute_state(context_gl, device, state);
4343 if (parameters->indirect)
4345 const struct wined3d_indirect_dispatch_parameters *indirect = &parameters->u.indirect;
4346 struct wined3d_bo_gl *bo_gl = wined3d_bo_gl(indirect->buffer->buffer_object);
4348 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, bo_gl->id));
4349 GL_EXTCALL(glDispatchComputeIndirect(bo_gl->b.buffer_offset + (GLintptr)indirect->offset));
4350 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0));
4351 wined3d_context_gl_reference_bo(context_gl, bo_gl);
4353 else
4355 const struct wined3d_direct_dispatch_parameters *direct = &parameters->u.direct;
4356 GL_EXTCALL(glDispatchCompute(direct->group_count_x, direct->group_count_y, direct->group_count_z));
4358 checkGLcall("dispatch compute");
4360 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4361 checkGLcall("glMemoryBarrier");
4363 context_release(&context_gl->c);
4366 /* Context activation is done by the caller. */
4367 static void wined3d_context_gl_draw_primitive_arrays(struct wined3d_context_gl *context_gl,
4368 const struct wined3d_state *state, const void *idx_data, unsigned int idx_size, int base_vertex_idx,
4369 unsigned int start_idx, unsigned int count, unsigned int start_instance, unsigned int instance_count)
4371 const struct wined3d_ffp_attrib_ops *ops = &context_gl->c.d3d_info->ffp_attrib_ops;
4372 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4373 const struct wined3d_stream_info *si = &context_gl->c.stream_info;
4374 GLenum mode = gl_primitive_type_from_d3d(state->primitive_type);
4375 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4376 unsigned int instanced_elements[ARRAY_SIZE(si->elements)];
4377 unsigned int instanced_element_count = 0;
4378 const void *indices;
4379 unsigned int i, j;
4381 indices = (const char *)idx_data + idx_size * start_idx;
4383 if (!instance_count)
4385 if (!idx_size)
4387 gl_info->gl_ops.gl.p_glDrawArrays(mode, start_idx, count);
4388 checkGLcall("glDrawArrays");
4389 return;
4392 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4394 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4395 checkGLcall("glDrawElementsBaseVertex");
4396 return;
4399 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4400 checkGLcall("glDrawElements");
4401 return;
4404 if (start_instance && !(gl_info->supported[ARB_BASE_INSTANCE] && gl_info->supported[ARB_INSTANCED_ARRAYS]))
4405 FIXME("Start instance (%u) not supported.\n", start_instance);
4407 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
4409 if (!idx_size)
4411 if (gl_info->supported[ARB_BASE_INSTANCE])
4413 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode, start_idx, count, instance_count, start_instance));
4414 checkGLcall("glDrawArraysInstancedBaseInstance");
4415 return;
4418 GL_EXTCALL(glDrawArraysInstanced(mode, start_idx, count, instance_count));
4419 checkGLcall("glDrawArraysInstanced");
4420 return;
4423 if (gl_info->supported[ARB_BASE_INSTANCE])
4425 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode, count, idx_type,
4426 indices, instance_count, base_vertex_idx, start_instance));
4427 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4428 return;
4430 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4432 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode, count, idx_type,
4433 indices, instance_count, base_vertex_idx));
4434 checkGLcall("glDrawElementsInstancedBaseVertex");
4435 return;
4438 GL_EXTCALL(glDrawElementsInstanced(mode, count, idx_type, indices, instance_count));
4439 checkGLcall("glDrawElementsInstanced");
4440 return;
4443 /* Instancing emulation by mixing immediate mode and arrays. */
4445 /* This is a nasty thing. MSDN says no hardware supports this and
4446 * applications have to use software vertex processing. We don't support
4447 * this for now.
4449 * Shouldn't be too hard to support with OpenGL, in theory just call
4450 * glDrawArrays() instead of drawElements(). But the stream frequency value
4451 * has a different meaning in that situation. */
4452 if (!idx_size)
4454 FIXME("Non-indexed instanced drawing is not supported.\n");
4455 return;
4458 for (i = 0; i < ARRAY_SIZE(si->elements); ++i)
4460 if (!(si->use_map & (1u << i)))
4461 continue;
4463 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
4464 instanced_elements[instanced_element_count++] = i;
4467 for (i = 0; i < instance_count; ++i)
4469 /* Specify the instanced attributes using immediate mode calls. */
4470 for (j = 0; j < instanced_element_count; ++j)
4472 const struct wined3d_stream_info_element *element;
4473 unsigned int element_idx;
4474 const BYTE *ptr;
4476 element_idx = instanced_elements[j];
4477 element = &si->elements[element_idx];
4478 ptr = element->data.addr + element->stride * i;
4479 if (element->data.buffer_object)
4480 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(state->streams[element->stream_idx].buffer,
4481 &context_gl->c);
4482 ops->generic[element->format->emit_idx](element_idx, ptr);
4485 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4487 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4488 checkGLcall("glDrawElementsBaseVertex");
4490 else
4492 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4493 checkGLcall("glDrawElements");
4498 static unsigned int get_stride_idx(const void *idx_data, unsigned int idx_size,
4499 unsigned int base_vertex_idx, unsigned int start_idx, unsigned int vertex_idx)
4501 if (!idx_data)
4502 return start_idx + vertex_idx;
4503 if (idx_size == 2)
4504 return ((const WORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4505 return ((const DWORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4508 /* Context activation is done by the caller. */
4509 static void draw_primitive_immediate_mode(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4510 const struct wined3d_stream_info *si, const void *idx_data, unsigned int idx_size,
4511 int base_vertex_idx, unsigned int start_idx, unsigned int vertex_count, unsigned int instance_count)
4513 const BYTE *position = NULL, *normal = NULL, *diffuse = NULL, *specular = NULL;
4514 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
4515 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4516 unsigned int coord_idx, stride_idx, texture_idx, vertex_idx;
4517 const struct wined3d_stream_info_element *element;
4518 const BYTE *tex_coords[WINED3DDP_MAXTEXCOORD];
4519 unsigned int texture_unit, texture_stages;
4520 const struct wined3d_ffp_attrib_ops *ops;
4521 unsigned int untracked_material_count;
4522 unsigned int tex_mask = 0;
4523 BOOL specular_fog = FALSE;
4524 BOOL ps = use_ps(state);
4525 const void *ptr;
4527 static unsigned int once;
4529 if (!once++)
4530 FIXME_(d3d_perf)("Drawing using immediate mode.\n");
4531 else
4532 WARN_(d3d_perf)("Drawing using immediate mode.\n");
4534 if (!idx_size && idx_data)
4535 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4537 if (instance_count)
4538 FIXME("Instancing not implemented.\n");
4540 /* Immediate mode drawing can't make use of indices in a VBO - get the
4541 * data from the index buffer. */
4542 if (idx_size)
4543 idx_data = wined3d_buffer_load_sysmem(state->index_buffer, &context_gl->c) + state->index_offset;
4545 ops = &d3d_info->ffp_attrib_ops;
4547 gl_info->gl_ops.gl.p_glBegin(gl_primitive_type_from_d3d(state->primitive_type));
4549 if (use_vs(state) || d3d_info->ffp_generic_attributes)
4551 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4553 unsigned int use_map = si->use_map;
4554 unsigned int element_idx;
4556 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4557 for (element_idx = MAX_ATTRIBS - 1; use_map; use_map &= ~(1u << element_idx), --element_idx)
4559 if (!(use_map & 1u << element_idx))
4560 continue;
4562 ptr = si->elements[element_idx].data.addr + si->elements[element_idx].stride * stride_idx;
4563 ops->generic[si->elements[element_idx].format->emit_idx](element_idx, ptr);
4567 gl_info->gl_ops.gl.p_glEnd();
4568 return;
4571 if (si->use_map & (1u << WINED3D_FFP_POSITION))
4572 position = si->elements[WINED3D_FFP_POSITION].data.addr;
4574 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
4575 normal = si->elements[WINED3D_FFP_NORMAL].data.addr;
4576 else
4577 gl_info->gl_ops.gl.p_glNormal3f(0.0f, 0.0f, 0.0f);
4579 untracked_material_count = context_gl->untracked_material_count;
4580 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
4582 element = &si->elements[WINED3D_FFP_DIFFUSE];
4583 diffuse = element->data.addr;
4585 if (untracked_material_count && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
4586 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element->format->id));
4588 else
4590 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4593 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
4595 element = &si->elements[WINED3D_FFP_SPECULAR];
4596 specular = element->data.addr;
4598 /* Special case where the fog density is stored in the specular alpha channel. */
4599 if (state->render_states[WINED3D_RS_FOGENABLE]
4600 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
4601 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
4602 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
4604 if (gl_info->supported[EXT_FOG_COORD])
4606 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
4607 specular_fog = TRUE;
4608 else
4609 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element->format->id));
4611 else
4613 static unsigned int once;
4615 if (!once++)
4616 FIXME("Implement fog for transformed vertices in software.\n");
4620 else if (gl_info->supported[EXT_SECONDARY_COLOR])
4622 GL_EXTCALL(glSecondaryColor3fEXT)(0.0f, 0.0f, 0.0f);
4625 texture_stages = d3d_info->limits.ffp_blend_stages;
4626 for (texture_idx = 0; texture_idx < texture_stages; ++texture_idx)
4628 if (!gl_info->supported[ARB_MULTITEXTURE] && texture_idx > 0)
4630 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4631 continue;
4634 if (!ps && !state->textures[texture_idx])
4635 continue;
4637 texture_unit = context_gl->tex_unit_map[texture_idx];
4638 if (texture_unit == WINED3D_UNMAPPED_STAGE)
4639 continue;
4641 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4642 if (coord_idx > 7)
4644 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx, texture_idx);
4645 continue;
4648 if (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))
4650 tex_coords[coord_idx] = si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].data.addr;
4651 tex_mask |= (1u << texture_idx);
4653 else
4655 TRACE("Setting default coordinates for texture %u.\n", texture_idx);
4656 if (gl_info->supported[ARB_MULTITEXTURE])
4657 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_unit, 0.0f, 0.0f, 0.0f, 1.0f));
4658 else
4659 gl_info->gl_ops.gl.p_glTexCoord4f(0.0f, 0.0f, 0.0f, 1.0f);
4663 /* Blending data and point sizes are not supported by this function. They
4664 * are not supported by the fixed function pipeline at all. A FIXME for
4665 * them is printed after decoding the vertex declaration. */
4666 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4668 unsigned int tmp_tex_mask;
4670 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4672 if (normal)
4674 ptr = normal + stride_idx * si->elements[WINED3D_FFP_NORMAL].stride;
4675 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptr);
4678 if (diffuse)
4680 ptr = diffuse + stride_idx * si->elements[WINED3D_FFP_DIFFUSE].stride;
4681 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptr);
4683 if (untracked_material_count)
4685 struct wined3d_color color;
4686 unsigned int i;
4688 wined3d_color_from_d3dcolor(&color, *(const DWORD *)ptr);
4689 for (i = 0; i < untracked_material_count; ++i)
4691 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK,
4692 context_gl->untracked_materials[i], &color.r);
4697 if (specular)
4699 ptr = specular + stride_idx * si->elements[WINED3D_FFP_SPECULAR].stride;
4700 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptr);
4702 if (specular_fog)
4703 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD *)ptr >> 24)));
4706 tmp_tex_mask = tex_mask;
4707 for (texture_idx = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture_idx)
4709 if (!(tmp_tex_mask & 1))
4710 continue;
4712 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4713 ptr = tex_coords[coord_idx] + (stride_idx * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
4714 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
4715 GL_TEXTURE0_ARB + context_gl->tex_unit_map[texture_idx], ptr);
4718 if (position)
4720 ptr = position + stride_idx * si->elements[WINED3D_FFP_POSITION].stride;
4721 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptr);
4725 gl_info->gl_ops.gl.p_glEnd();
4726 checkGLcall("draw immediate mode");
4729 static void wined3d_context_gl_draw_indirect(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4730 const struct wined3d_indirect_draw_parameters *parameters, unsigned int idx_size)
4732 struct wined3d_bo_gl *bo_gl = wined3d_bo_gl(parameters->buffer->buffer_object);
4733 GLenum gl_primitive_type = gl_primitive_type_from_d3d(state->primitive_type);
4734 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4735 const void *offset;
4737 if (!gl_info->supported[ARB_DRAW_INDIRECT])
4739 FIXME("OpenGL implementation does not support indirect draws.\n");
4740 return;
4743 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, bo_gl->id));
4745 offset = (const uint8_t *)bo_gl->b.buffer_offset + parameters->offset;
4746 if (idx_size)
4748 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4749 if (state->index_offset)
4750 FIXME("Ignoring index offset %u.\n", state->index_offset);
4751 GL_EXTCALL(glDrawElementsIndirect(gl_primitive_type, idx_type, offset));
4753 else
4755 GL_EXTCALL(glDrawArraysIndirect(gl_primitive_type, offset));
4758 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0));
4759 wined3d_context_gl_reference_bo(context_gl, bo_gl);
4761 checkGLcall("draw indirect");
4764 static void remove_vbos(struct wined3d_context *context,
4765 const struct wined3d_state *state, struct wined3d_stream_info *s)
4767 unsigned int i;
4769 for (i = 0; i < ARRAY_SIZE(s->elements); ++i)
4771 struct wined3d_stream_info_element *e;
4773 if (!(s->use_map & (1u << i)))
4774 continue;
4776 e = &s->elements[i];
4777 if (e->data.buffer_object)
4779 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
4780 e->data.buffer_object = 0;
4781 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(vb, context);
4786 static GLenum gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
4788 GLenum gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4789 switch (gl_primitive_type)
4791 case GL_POINTS:
4792 return GL_POINTS;
4794 case GL_LINE_STRIP:
4795 case GL_LINE_STRIP_ADJACENCY:
4796 case GL_LINES_ADJACENCY:
4797 case GL_LINES:
4798 return GL_LINES;
4800 case GL_TRIANGLE_FAN:
4801 case GL_TRIANGLE_STRIP:
4802 case GL_TRIANGLE_STRIP_ADJACENCY:
4803 case GL_TRIANGLES_ADJACENCY:
4804 case GL_TRIANGLES:
4805 return GL_TRIANGLES;
4807 default:
4808 return gl_primitive_type;
4812 /* Routine common to the draw primitive and draw indexed primitive routines */
4813 void draw_primitive(struct wined3d_device *device, const struct wined3d_state *state,
4814 const struct wined3d_draw_parameters *parameters)
4816 BOOL emulation = FALSE, rasterizer_discard = FALSE;
4817 const struct wined3d_fb_state *fb = &state->fb;
4818 const struct wined3d_stream_info *stream_info;
4819 struct wined3d_rendertarget_view *dsv, *rtv;
4820 struct wined3d_stream_info si_emulated;
4821 const struct wined3d_gl_info *gl_info;
4822 struct wined3d_context_gl *context_gl;
4823 struct wined3d_context *context;
4824 unsigned int i, idx_size = 0;
4825 const void *idx_data = NULL;
4827 TRACE("device %p, state %p, parameters %p.\n", device, state, parameters);
4829 if (!parameters->indirect && !parameters->u.direct.index_count)
4830 return;
4832 if (!parameters->indirect)
4833 TRACE("base_vertex_idx %d, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
4834 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
4835 parameters->u.direct.index_count, parameters->u.direct.start_instance,
4836 parameters->u.direct.instance_count);
4838 if (!(rtv = fb->render_targets[0]))
4839 rtv = fb->depth_stencil;
4841 if (rtv && rtv->resource->type == WINED3D_RTYPE_BUFFER)
4843 FIXME("Buffer render targets not implemented.\n");
4844 return;
4847 if (rtv)
4848 context = context_acquire(device, wined3d_texture_from_resource(rtv->resource), rtv->sub_resource_idx);
4849 else
4850 context = context_acquire(device, NULL, 0);
4851 context_gl = wined3d_context_gl(context);
4852 if (!context_gl->valid)
4854 context_release(context);
4855 WARN("Invalid context, skipping draw.\n");
4856 return;
4858 gl_info = context_gl->gl_info;
4860 if (!use_transform_feedback(state))
4861 wined3d_context_gl_pause_transform_feedback(context_gl, TRUE);
4863 for (i = 0; i < gl_info->limits.buffers; ++i)
4865 if (!(rtv = fb->render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
4866 continue;
4868 if (wined3d_blend_state_get_writemask(state->blend_state, i))
4870 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
4871 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
4873 else
4875 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
4879 if ((dsv = fb->depth_stencil))
4881 /* Note that this depends on the context_acquire() call above to set
4882 * context->render_offscreen properly. We don't currently take the
4883 * Z-compare function into account, but we could skip loading the
4884 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
4885 * that we never copy the stencil data.*/
4886 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4888 if (wined3d_state_uses_depth_buffer(state))
4889 wined3d_rendertarget_view_load_location(dsv, context, location);
4890 else
4891 wined3d_rendertarget_view_prepare_location(dsv, context, location);
4894 if (parameters->indirect)
4895 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
4897 if (!context_apply_draw_state(context, device, state, parameters->indexed))
4899 context_release(context);
4900 WARN("Unable to apply draw state, skipping draw.\n");
4901 return;
4904 if (dsv && (!state->depth_stencil_state || state->depth_stencil_state->desc.depth_write))
4906 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4908 wined3d_rendertarget_view_validate_location(dsv, location);
4909 wined3d_rendertarget_view_invalidate_location(dsv, ~location);
4912 stream_info = &context->stream_info;
4914 if (parameters->indexed)
4916 struct wined3d_buffer *index_buffer = state->index_buffer;
4917 struct wined3d_bo *bo = index_buffer->buffer_object;
4919 if (!bo || !stream_info->all_vbo)
4920 idx_data = index_buffer->resource.heap_memory;
4921 else
4922 idx_data = (void *)bo->buffer_offset;
4923 idx_data = (const BYTE *)idx_data + state->index_offset;
4925 if (state->index_format == WINED3DFMT_R16_UINT)
4926 idx_size = 2;
4927 else
4928 idx_size = 4;
4931 if (!use_vs(state))
4933 if (!stream_info->position_transformed && context_gl->untracked_material_count
4934 && state->render_states[WINED3D_RS_LIGHTING])
4936 static BOOL warned;
4938 if (!warned++)
4939 FIXME("Using software emulation because not all material properties could be tracked.\n");
4940 else
4941 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
4942 emulation = TRUE;
4944 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
4946 static BOOL warned;
4948 /* Either write a pipeline replacement shader or convert the
4949 * specular alpha from unsigned byte to a float in the vertex
4950 * buffer. */
4951 if (!warned++)
4952 FIXME("Using software emulation because manual fog coordinates are provided.\n");
4953 else
4954 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
4955 emulation = TRUE;
4958 if (emulation)
4960 si_emulated = context->stream_info;
4961 remove_vbos(context, state, &si_emulated);
4962 stream_info = &si_emulated;
4966 if (use_transform_feedback(state))
4968 const struct wined3d_shader *shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
4970 if (is_rasterization_disabled(shader))
4972 glEnable(GL_RASTERIZER_DISCARD);
4973 checkGLcall("enable rasterizer discard");
4974 rasterizer_discard = TRUE;
4977 if (context->transform_feedback_paused)
4979 GL_EXTCALL(glResumeTransformFeedback());
4980 checkGLcall("glResumeTransformFeedback");
4981 context->transform_feedback_paused = 0;
4983 else if (!context->transform_feedback_active)
4985 enum wined3d_primitive_type primitive_type = shader->u.gs.output_type
4986 ? shader->u.gs.output_type : state->primitive_type;
4987 GLenum mode = gl_tfb_primitive_type_from_d3d(primitive_type);
4988 GL_EXTCALL(glBeginTransformFeedback(mode));
4989 checkGLcall("glBeginTransformFeedback");
4990 context->transform_feedback_active = 1;
4994 if (state->primitive_type == WINED3D_PT_PATCH)
4996 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES, state->patch_vertex_count));
4997 checkGLcall("glPatchParameteri");
5000 if (context->uses_fbo_attached_resources)
5002 static unsigned int fixme_once;
5004 if (gl_info->supported[ARB_TEXTURE_BARRIER])
5006 GL_EXTCALL(glTextureBarrier());
5008 else if (gl_info->supported[NV_TEXTURE_BARRIER])
5010 GL_EXTCALL(glTextureBarrierNV());
5012 else
5014 if (!fixme_once++)
5015 FIXME("Sampling attached render targets is not supported.\n");
5017 WARN("Sampling attached render targets is not supported, skipping draw.\n");
5018 context_release(context);
5019 return;
5021 checkGLcall("glTextureBarrier");
5024 if (parameters->indirect)
5026 if (!context->use_immediate_mode_draw && !emulation)
5027 wined3d_context_gl_draw_indirect(context_gl, state, &parameters->u.indirect, idx_size);
5028 else
5029 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
5031 else
5033 unsigned int instance_count = parameters->u.direct.instance_count;
5034 if (context->instance_count)
5035 instance_count = context->instance_count;
5037 if (context->use_immediate_mode_draw || emulation)
5038 draw_primitive_immediate_mode(wined3d_context_gl(context), state, stream_info, idx_data,
5039 idx_size, parameters->u.direct.base_vertex_idx,
5040 parameters->u.direct.start_idx, parameters->u.direct.index_count, instance_count);
5041 else
5042 wined3d_context_gl_draw_primitive_arrays(context_gl, state, idx_data, idx_size,
5043 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
5044 parameters->u.direct.index_count, parameters->u.direct.start_instance, instance_count);
5047 if (context->uses_uavs)
5049 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
5050 checkGLcall("glMemoryBarrier");
5053 wined3d_context_gl_pause_transform_feedback(context_gl, FALSE);
5055 if (rasterizer_discard)
5057 glDisable(GL_RASTERIZER_DISCARD);
5058 checkGLcall("disable rasterizer discard");
5061 context_release(context);
5063 TRACE("Draw completed.\n");
5066 void wined3d_context_gl_unload_tex_coords(const struct wined3d_context_gl *context_gl)
5068 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5069 unsigned int texture_idx;
5071 for (texture_idx = 0; texture_idx < gl_info->limits.texture_coords; ++texture_idx)
5073 gl_info->gl_ops.ext.p_glClientActiveTextureARB(GL_TEXTURE0_ARB + texture_idx);
5074 gl_info->gl_ops.gl.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY);
5078 static const void *get_vertex_attrib_pointer(const struct wined3d_stream_info_element *element,
5079 const struct wined3d_state *state)
5081 const uint8_t *offset = element->data.addr + state->load_base_vertex_index * element->stride;
5083 if (element->data.buffer_object)
5084 offset += element->data.buffer_object->buffer_offset;
5085 return offset;
5088 void wined3d_context_gl_load_tex_coords(const struct wined3d_context_gl *context_gl,
5089 const struct wined3d_stream_info *si, GLuint *current_bo, const struct wined3d_state *state)
5091 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5092 const struct wined3d_format_gl *format_gl;
5093 unsigned int mapped_stage = 0;
5094 unsigned int texture_idx;
5095 GLuint bo;
5097 for (texture_idx = 0; texture_idx < context_gl->c.d3d_info->limits.ffp_blend_stages; ++texture_idx)
5099 unsigned int coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
5101 if ((mapped_stage = context_gl->tex_unit_map[texture_idx]) == WINED3D_UNMAPPED_STAGE)
5102 continue;
5104 if (mapped_stage >= gl_info->limits.texture_coords)
5106 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage);
5107 continue;
5110 if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
5112 const struct wined3d_stream_info_element *e = &si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx];
5114 TRACE("Setting up texture %u, idx %u, coord_idx %u, data %s.\n",
5115 texture_idx, mapped_stage, coord_idx, debug_bo_address(&e->data));
5117 bo = wined3d_bo_gl_id(e->data.buffer_object);
5118 if (*current_bo != bo)
5120 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5121 checkGLcall("glBindBuffer");
5122 *current_bo = bo;
5125 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
5126 checkGLcall("glClientActiveTextureARB");
5128 /* The coords to supply depend completely on the fvf/vertex shader. */
5129 format_gl = wined3d_format_gl(e->format);
5130 gl_info->gl_ops.gl.p_glTexCoordPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5131 get_vertex_attrib_pointer(e, state));
5132 gl_info->gl_ops.gl.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY);
5133 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5135 else
5137 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + mapped_stage, 0, 0, 0, 1));
5140 if (gl_info->supported[NV_REGISTER_COMBINERS])
5142 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
5143 for (texture_idx = mapped_stage + 1; texture_idx < gl_info->limits.textures; ++texture_idx)
5145 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
5149 checkGLcall("loadTexCoords");
5152 /* This should match any arrays loaded in wined3d_context_gl_load_vertex_data(). */
5153 static void wined3d_context_gl_unload_vertex_data(struct wined3d_context_gl *context_gl)
5155 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5157 if (!context_gl->c.namedArraysLoaded)
5158 return;
5159 gl_info->gl_ops.gl.p_glDisableClientState(GL_VERTEX_ARRAY);
5160 gl_info->gl_ops.gl.p_glDisableClientState(GL_NORMAL_ARRAY);
5161 gl_info->gl_ops.gl.p_glDisableClientState(GL_COLOR_ARRAY);
5162 if (gl_info->supported[EXT_SECONDARY_COLOR])
5163 gl_info->gl_ops.gl.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5164 wined3d_context_gl_unload_tex_coords(context_gl);
5165 context_gl->c.namedArraysLoaded = FALSE;
5168 static void wined3d_context_gl_load_vertex_data(struct wined3d_context_gl *context_gl,
5169 const struct wined3d_stream_info *si, const struct wined3d_state *state)
5171 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5172 const struct wined3d_stream_info_element *e;
5173 const struct wined3d_format_gl *format_gl;
5174 GLuint current_bo, bo;
5175 const void *offset;
5177 TRACE("context_gl %p, si %p, state %p.\n", context_gl, si, state);
5179 /* This is used for the fixed-function pipeline only, and the
5180 * fixed-function pipeline doesn't do instancing. */
5181 context_gl->c.instance_count = 0;
5182 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5184 /* Blend data */
5185 if ((si->use_map & (1u << WINED3D_FFP_BLENDWEIGHT))
5186 || si->use_map & (1u << WINED3D_FFP_BLENDINDICES))
5188 /* TODO: Support vertex blending in immediate mode draws. No need to
5189 * write a FIXME here, this is done after the general vertex
5190 * declaration decoding. */
5191 WARN("Vertex blending not supported.\n");
5194 /* Point Size */
5195 if (si->use_map & (1u << WINED3D_FFP_PSIZE))
5197 /* No such functionality in the fixed-function GL pipeline. */
5198 WARN("Per-vertex point size not supported.\n");
5201 /* Position */
5202 if (si->use_map & (1u << WINED3D_FFP_POSITION))
5204 e = &si->elements[WINED3D_FFP_POSITION];
5205 format_gl = wined3d_format_gl(e->format);
5206 offset = get_vertex_attrib_pointer(e, state);
5208 bo = wined3d_bo_gl_id(e->data.buffer_object);
5209 if (current_bo != bo)
5211 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5212 checkGLcall("glBindBuffer");
5213 current_bo = bo;
5216 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n", format_gl->vtx_format, format_gl->vtx_type, e->stride, offset);
5217 gl_info->gl_ops.gl.p_glVertexPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride, offset);
5218 checkGLcall("glVertexPointer(...)");
5219 gl_info->gl_ops.gl.p_glEnableClientState(GL_VERTEX_ARRAY);
5220 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5221 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5224 /* Normals */
5225 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
5227 e = &si->elements[WINED3D_FFP_NORMAL];
5228 format_gl = wined3d_format_gl(e->format);
5229 offset = get_vertex_attrib_pointer(e, state);
5231 bo = wined3d_bo_gl_id(e->data.buffer_object);
5232 if (current_bo != bo)
5234 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5235 checkGLcall("glBindBuffer");
5236 current_bo = bo;
5239 TRACE("glNormalPointer(%#x, %#x, %p);\n", format_gl->vtx_type, e->stride, offset);
5240 gl_info->gl_ops.gl.p_glNormalPointer(format_gl->vtx_type, e->stride, offset);
5241 checkGLcall("glNormalPointer(...)");
5242 gl_info->gl_ops.gl.p_glEnableClientState(GL_NORMAL_ARRAY);
5243 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5244 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5246 else
5248 gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
5249 checkGLcall("glNormal3f(0, 0, 0)");
5252 /* Diffuse colour */
5253 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
5255 e = &si->elements[WINED3D_FFP_DIFFUSE];
5256 format_gl = wined3d_format_gl(e->format);
5257 offset = get_vertex_attrib_pointer(e, state);
5259 bo = wined3d_bo_gl_id(e->data.buffer_object);
5260 if (current_bo != bo)
5262 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5263 checkGLcall("glBindBuffer");
5264 current_bo = bo;
5267 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5268 format_gl->vtx_format, format_gl->vtx_type, e->stride, offset);
5269 gl_info->gl_ops.gl.p_glColorPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride, offset);
5270 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5271 gl_info->gl_ops.gl.p_glEnableClientState(GL_COLOR_ARRAY);
5272 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5273 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5275 else
5277 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
5278 checkGLcall("glColor4f(1, 1, 1, 1)");
5281 /* Specular colour */
5282 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
5284 TRACE("Setting specular colour.\n");
5286 e = &si->elements[WINED3D_FFP_SPECULAR];
5287 offset = get_vertex_attrib_pointer(e, state);
5289 if (gl_info->supported[EXT_SECONDARY_COLOR])
5291 GLint format;
5292 GLenum type;
5294 format_gl = wined3d_format_gl(e->format);
5295 type = format_gl->vtx_type;
5296 format = format_gl->vtx_format;
5298 bo = wined3d_bo_gl_id(e->data.buffer_object);
5299 if (current_bo != bo)
5301 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5302 checkGLcall("glBindBuffer");
5303 current_bo = bo;
5306 if (format != 4 || (gl_info->quirks & WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA))
5308 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5309 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5310 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5311 * 4 component secondary colors use it
5313 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format, type, e->stride, offset);
5314 GL_EXTCALL(glSecondaryColorPointerEXT(format, type, e->stride, offset));
5315 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5317 else
5319 switch (type)
5321 case GL_UNSIGNED_BYTE:
5322 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e->stride, offset);
5323 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, e->stride, offset));
5324 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5325 break;
5327 default:
5328 FIXME("Add 4 component specular colour pointers for type %#x.\n", type);
5329 /* Make sure that the right colour component is dropped. */
5330 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type, e->stride, offset);
5331 GL_EXTCALL(glSecondaryColorPointerEXT(3, type, e->stride, offset));
5332 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5335 gl_info->gl_ops.gl.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5336 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5337 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5339 else
5341 WARN("Specular colour is not supported in this GL implementation.\n");
5344 else
5346 if (gl_info->supported[EXT_SECONDARY_COLOR])
5348 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
5349 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5351 else
5353 WARN("Specular colour is not supported in this GL implementation.\n");
5357 /* Texture coordinates */
5358 wined3d_context_gl_load_tex_coords(context_gl, si, &current_bo, state);
5361 static void wined3d_context_gl_unload_numbered_array(struct wined3d_context_gl *context_gl, unsigned int i)
5363 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5365 GL_EXTCALL(glDisableVertexAttribArray(i));
5366 checkGLcall("glDisableVertexAttribArray");
5367 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5368 GL_EXTCALL(glVertexAttribDivisor(i, 0));
5370 context_gl->c.numbered_array_mask &= ~(1u << i);
5373 static void wined3d_context_gl_unload_numbered_arrays(struct wined3d_context_gl *context_gl)
5375 uint32_t mask = context_gl->c.numbered_array_mask;
5376 unsigned int i;
5378 while (mask)
5380 i = wined3d_bit_scan(&mask);
5381 wined3d_context_gl_unload_numbered_array(context_gl, i);
5385 static void wined3d_context_gl_load_numbered_arrays(struct wined3d_context_gl *context_gl,
5386 const struct wined3d_stream_info *stream_info, const struct wined3d_state *state)
5388 struct wined3d_context *context = &context_gl->c;
5389 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5390 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5391 GLuint current_bo, bo;
5392 unsigned int i;
5394 /* Default to no instancing. */
5395 context->instance_count = 0;
5396 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5398 for (i = 0; i < MAX_ATTRIBS; ++i)
5400 const struct wined3d_stream_info_element *element = &stream_info->elements[i];
5401 const void *offset = get_vertex_attrib_pointer(element, state);
5402 const struct wined3d_stream_state *stream;
5403 const struct wined3d_format_gl *format_gl;
5405 if (!(stream_info->use_map & (1u << i)))
5407 if (context->numbered_array_mask & (1u << i))
5408 wined3d_context_gl_unload_numbered_array(context_gl, i);
5409 if (!use_vs(state) && i == WINED3D_FFP_DIFFUSE)
5411 if (!(context_gl->default_attrib_value_set & (1u << i)) || !context_gl->diffuse_attrib_to_1)
5413 GL_EXTCALL(glVertexAttrib4f(i, 1.0f, 1.0f, 1.0f, 1.0f));
5414 context_gl->diffuse_attrib_to_1 = 1;
5417 else
5419 if (!(context_gl->default_attrib_value_set & (1u << i)))
5421 GL_EXTCALL(glVertexAttrib4f(i, 0.0f, 0.0f, 0.0f, 0.0f));
5422 if (i == WINED3D_FFP_DIFFUSE)
5423 context_gl->diffuse_attrib_to_1 = 0;
5426 context_gl->default_attrib_value_set |= 1u << i;
5427 continue;
5430 format_gl = wined3d_format_gl(element->format);
5431 stream = &state->streams[element->stream_idx];
5432 stream->buffer->bo_user.valid = true;
5434 if ((stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA) && !context->instance_count)
5435 context->instance_count = state->streams[0].frequency;
5437 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5439 unsigned int divisor = 0;
5441 if (element->instanced)
5442 divisor = element->divisor ? element->divisor : UINT_MAX;
5443 GL_EXTCALL(glVertexAttribDivisor(i, divisor));
5445 else if (element->divisor)
5447 /* Unload instanced arrays, they will be loaded using immediate
5448 * mode instead. */
5449 if (context->numbered_array_mask & (1u << i))
5450 wined3d_context_gl_unload_numbered_array(context_gl, i);
5451 context_gl->default_attrib_value_set &= ~(1u << i);
5452 continue;
5455 TRACE("Loading array %u %s.\n", i, debug_bo_address(&element->data));
5457 if (element->stride)
5459 DWORD format_flags = format_gl->f.flags[WINED3D_GL_RES_TYPE_BUFFER];
5461 bo = wined3d_bo_gl_id(element->data.buffer_object);
5462 if (current_bo != bo)
5464 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5465 checkGLcall("glBindBuffer");
5466 current_bo = bo;
5468 /* Use the VBO to find out if a vertex buffer exists, not the vb
5469 * pointer. vb can point to a user pointer data blob. In that case
5470 * current_bo will be 0. If there is a vertex buffer but no vbo we
5471 * won't be load converted attributes anyway. */
5472 if (vs && vs->reg_maps.shader_version.major >= 4 && (format_flags & WINED3DFMT_FLAG_INTEGER))
5474 GL_EXTCALL(glVertexAttribIPointer(i, format_gl->vtx_format,
5475 format_gl->vtx_type, element->stride, offset));
5477 else
5479 GL_EXTCALL(glVertexAttribPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5480 !!(format_flags & WINED3DFMT_FLAG_NORMALISED), element->stride, offset));
5483 if (!(context->numbered_array_mask & (1u << i)))
5485 GL_EXTCALL(glEnableVertexAttribArray(i));
5486 context->numbered_array_mask |= (1u << i);
5489 else
5491 /* Stride = 0 means always the same values.
5492 * glVertexAttribPointer() doesn't do that. Instead disable the
5493 * pointer and set up the attribute statically. But we have to
5494 * figure out the system memory address. */
5495 const BYTE *ptr = element->data.addr;
5496 if (element->data.buffer_object)
5497 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(stream->buffer, context);
5499 if (context->numbered_array_mask & (1u << i))
5500 wined3d_context_gl_unload_numbered_array(context_gl, i);
5502 switch (format_gl->f.id)
5504 case WINED3DFMT_R32_FLOAT:
5505 GL_EXTCALL(glVertexAttrib1fv(i, (const GLfloat *)ptr));
5506 break;
5507 case WINED3DFMT_R32G32_FLOAT:
5508 GL_EXTCALL(glVertexAttrib2fv(i, (const GLfloat *)ptr));
5509 break;
5510 case WINED3DFMT_R32G32B32_FLOAT:
5511 GL_EXTCALL(glVertexAttrib3fv(i, (const GLfloat *)ptr));
5512 break;
5513 case WINED3DFMT_R32G32B32A32_FLOAT:
5514 GL_EXTCALL(glVertexAttrib4fv(i, (const GLfloat *)ptr));
5515 break;
5516 case WINED3DFMT_R8G8B8A8_UINT:
5517 GL_EXTCALL(glVertexAttrib4ubv(i, ptr));
5518 break;
5519 case WINED3DFMT_B8G8R8A8_UNORM:
5520 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
5522 const DWORD *src = (const DWORD *)ptr;
5523 DWORD c = *src & 0xff00ff00u;
5524 c |= (*src & 0xff0000u) >> 16;
5525 c |= (*src & 0xffu) << 16;
5526 GL_EXTCALL(glVertexAttrib4Nubv(i, (GLubyte *)&c));
5527 break;
5529 /* else fallthrough */
5530 case WINED3DFMT_R8G8B8A8_UNORM:
5531 GL_EXTCALL(glVertexAttrib4Nubv(i, ptr));
5532 break;
5533 case WINED3DFMT_R16G16_SINT:
5534 GL_EXTCALL(glVertexAttrib2sv(i, (const GLshort *)ptr));
5535 break;
5536 case WINED3DFMT_R16G16B16A16_SINT:
5537 GL_EXTCALL(glVertexAttrib4sv(i, (const GLshort *)ptr));
5538 break;
5539 case WINED3DFMT_R16G16_SNORM:
5541 const GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
5542 GL_EXTCALL(glVertexAttrib4Nsv(i, s));
5543 break;
5545 case WINED3DFMT_R16G16_UNORM:
5547 const GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
5548 GL_EXTCALL(glVertexAttrib4Nusv(i, s));
5549 break;
5551 case WINED3DFMT_R16G16B16A16_SNORM:
5552 GL_EXTCALL(glVertexAttrib4Nsv(i, (const GLshort *)ptr));
5553 break;
5554 case WINED3DFMT_R16G16B16A16_UNORM:
5555 GL_EXTCALL(glVertexAttrib4Nusv(i, (const GLushort *)ptr));
5556 break;
5557 case WINED3DFMT_R10G10B10X2_UINT:
5558 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5559 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5560 break;
5561 case WINED3DFMT_R10G10B10X2_SNORM:
5562 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5563 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5564 break;
5565 case WINED3DFMT_R16G16_FLOAT:
5566 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5568 /* Not supported by GL_ARB_half_float_vertex. */
5569 GL_EXTCALL(glVertexAttrib2hvNV(i, (const GLhalfNV *)ptr));
5571 else
5573 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5574 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5575 GL_EXTCALL(glVertexAttrib2f(i, x, y));
5577 break;
5578 case WINED3DFMT_R16G16B16A16_FLOAT:
5579 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5581 /* Not supported by GL_ARB_half_float_vertex. */
5582 GL_EXTCALL(glVertexAttrib4hvNV(i, (const GLhalfNV *)ptr));
5584 else
5586 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5587 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5588 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
5589 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
5590 GL_EXTCALL(glVertexAttrib4f(i, x, y, z, w));
5592 break;
5593 default:
5594 ERR("Unexpected declaration in stride 0 attributes.\n");
5595 break;
5598 context_gl->default_attrib_value_set &= ~(1u << i);
5601 checkGLcall("Loading numbered arrays");
5604 void wined3d_context_gl_update_stream_sources(struct wined3d_context_gl *context_gl,
5605 const struct wined3d_state *state)
5607 if (context_gl->c.use_immediate_mode_draw)
5608 return;
5610 wined3d_context_gl_unload_vertex_data(context_gl);
5611 if (context_gl->c.d3d_info->ffp_generic_attributes || use_vs(state))
5613 TRACE("Loading numbered arrays.\n");
5614 wined3d_context_gl_load_numbered_arrays(context_gl, &context_gl->c.stream_info, state);
5615 return;
5618 TRACE("Loading named arrays.\n");
5619 wined3d_context_gl_unload_numbered_arrays(context_gl);
5620 wined3d_context_gl_load_vertex_data(context_gl, &context_gl->c.stream_info, state);
5621 context_gl->c.namedArraysLoaded = TRUE;
5624 static void apply_texture_blit_state(const struct wined3d_gl_info *gl_info, struct gl_texture *texture,
5625 GLenum target, unsigned int level, enum wined3d_texture_filter_type filter)
5627 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
5628 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
5629 wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
5630 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
5631 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
5632 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
5633 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
5634 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, level);
5636 /* We changed the filtering settings on the texture. Make sure they get
5637 * reset on subsequent draws. */
5638 texture->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
5639 texture->sampler_desc.min_filter = WINED3D_TEXF_POINT;
5640 texture->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
5641 texture->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
5642 texture->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
5643 texture->sampler_desc.srgb_decode = FALSE;
5644 texture->base_level = level;
5647 /* Context activation is done by the caller. */
5648 void wined3d_context_gl_draw_shaded_quad(struct wined3d_context_gl *context_gl, struct wined3d_texture_gl *texture_gl,
5649 unsigned int sub_resource_idx, const RECT *src_rect, const RECT *dst_rect,
5650 enum wined3d_texture_filter_type filter)
5652 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5653 struct wined3d_blt_info info;
5654 unsigned int level, w, h, i;
5655 SIZE dst_size;
5656 struct blit_vertex
5658 float x, y;
5659 struct wined3d_vec3 texcoord;
5661 quad[4];
5663 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5665 level = sub_resource_idx % texture_gl->t.level_count;
5666 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5667 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5668 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5670 wined3d_context_gl_get_rt_size(context_gl, &dst_size);
5671 w = dst_size.cx;
5672 h = dst_size.cy;
5674 quad[0].x = dst_rect->left * 2.0f / w - 1.0f;
5675 quad[0].y = dst_rect->top * 2.0f / h - 1.0f;
5676 quad[0].texcoord = info.texcoords[0];
5678 quad[1].x = dst_rect->right * 2.0f / w - 1.0f;
5679 quad[1].y = dst_rect->top * 2.0f / h - 1.0f;
5680 quad[1].texcoord = info.texcoords[1];
5682 quad[2].x = dst_rect->left * 2.0f / w - 1.0f;
5683 quad[2].y = dst_rect->bottom * 2.0f / h - 1.0f;
5684 quad[2].texcoord = info.texcoords[2];
5686 quad[3].x = dst_rect->right * 2.0f / w - 1.0f;
5687 quad[3].y = dst_rect->bottom * 2.0f / h - 1.0f;
5688 quad[3].texcoord = info.texcoords[3];
5690 /* Draw a quad. */
5691 if (gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
5693 if (!context_gl->blit_vbo)
5694 GL_EXTCALL(glGenBuffers(1, &context_gl->blit_vbo));
5695 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, context_gl->blit_vbo));
5697 wined3d_context_gl_unload_vertex_data(context_gl);
5698 wined3d_context_gl_unload_numbered_arrays(context_gl);
5700 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STREAM_DRAW));
5701 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT, FALSE, sizeof(*quad), NULL));
5702 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT, FALSE, sizeof(*quad),
5703 (void *)FIELD_OFFSET(struct blit_vertex, texcoord)));
5705 GL_EXTCALL(glEnableVertexAttribArray(0));
5706 GL_EXTCALL(glEnableVertexAttribArray(1));
5708 gl_info->gl_ops.gl.p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
5710 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
5711 GL_EXTCALL(glDisableVertexAttribArray(1));
5712 GL_EXTCALL(glDisableVertexAttribArray(0));
5714 else
5716 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5718 for (i = 0; i < ARRAY_SIZE(quad); ++i)
5720 GL_EXTCALL(glVertexAttrib3fv(1, &quad[i].texcoord.x));
5721 GL_EXTCALL(glVertexAttrib2fv(0, &quad[i].x));
5724 gl_info->gl_ops.gl.p_glEnd();
5726 checkGLcall("draw");
5728 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5729 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);
5732 /* Context activation is done by the caller. */
5733 void wined3d_context_gl_draw_textured_quad(struct wined3d_context_gl *context_gl,
5734 struct wined3d_texture_gl *texture_gl, unsigned int sub_resource_idx,
5735 const RECT *src_rect, const RECT *dst_rect, enum wined3d_texture_filter_type filter)
5737 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5738 struct wined3d_blt_info info;
5739 unsigned int level;
5741 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5743 gl_info->gl_ops.gl.p_glEnable(info.bind_target);
5744 checkGLcall("glEnable(bind_target)");
5746 level = sub_resource_idx % texture_gl->t.level_count;
5747 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5748 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5749 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5750 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
5751 checkGLcall("glTexEnvi");
5753 /* Draw a quad. */
5754 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5755 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[0].x);
5756 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->top);
5758 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[1].x);
5759 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->top);
5761 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[2].x);
5762 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->bottom);
5764 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[3].x);
5765 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->bottom);
5766 gl_info->gl_ops.gl.p_glEnd();
5768 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5769 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);