mshtml/tests: Fix a typo in ok() message.
[wine.git] / dlls / wined3d / context.c
blobc5d2eeaa477d35b96b1ad80aa2e969e5f5283f10
1 /*
2 * Context and render target management in wined3d
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006, 2008 Henri Verbeet
9 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdio.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous);
41 #define WINED3D_MAX_FBO_ENTRIES 64
42 #define WINED3D_ALL_LAYERS (~0u)
44 static DWORD wined3d_context_tls_idx;
46 /* FBO helper functions */
48 /* Context activation is done by the caller. */
49 static void wined3d_context_gl_bind_fbo(struct wined3d_context_gl *context_gl, GLenum target, GLuint fbo)
51 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
53 switch (target)
55 case GL_READ_FRAMEBUFFER:
56 if (context_gl->fbo_read_binding == fbo)
57 return;
58 context_gl->fbo_read_binding = fbo;
59 break;
61 case GL_DRAW_FRAMEBUFFER:
62 if (context_gl->fbo_draw_binding == fbo)
63 return;
64 context_gl->fbo_draw_binding = fbo;
65 break;
67 case GL_FRAMEBUFFER:
68 if (context_gl->fbo_read_binding == fbo
69 && context_gl->fbo_draw_binding == fbo)
70 return;
71 context_gl->fbo_read_binding = fbo;
72 context_gl->fbo_draw_binding = fbo;
73 break;
75 default:
76 FIXME("Unhandled target %#x.\n", target);
77 break;
80 gl_info->fbo_ops.glBindFramebuffer(target, fbo);
81 checkGLcall("glBindFramebuffer()");
84 /* Context activation is done by the caller. */
85 static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info, GLenum target)
87 unsigned int i;
89 for (i = 0; i < gl_info->limits.buffers; ++i)
91 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
92 checkGLcall("glFramebufferTexture2D()");
94 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
95 checkGLcall("glFramebufferTexture2D()");
97 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
98 checkGLcall("glFramebufferTexture2D()");
101 /* Context activation is done by the caller. */
102 static void wined3d_context_gl_destroy_fbo(struct wined3d_context_gl *context_gl, GLuint fbo)
104 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
106 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, fbo);
107 context_clean_fbo_attachments(gl_info, GL_FRAMEBUFFER);
108 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
110 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
111 checkGLcall("glDeleteFramebuffers()");
114 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info *gl_info,
115 GLenum fbo_target, DWORD flags, GLuint rb)
117 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
119 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
120 checkGLcall("glFramebufferRenderbuffer()");
123 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
125 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
126 checkGLcall("glFramebufferRenderbuffer()");
130 static void wined3d_context_gl_attach_gl_texture_fbo(struct wined3d_context_gl *context_gl,
131 GLenum fbo_target, GLenum attachment, const struct wined3d_fbo_resource *resource)
133 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
135 if (!resource)
137 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment, GL_TEXTURE_2D, 0, 0);
139 else if (resource->layer == WINED3D_ALL_LAYERS)
141 if (!gl_info->fbo_ops.glFramebufferTexture)
143 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
144 return;
147 gl_info->fbo_ops.glFramebufferTexture(fbo_target, attachment,
148 resource->object, resource->level);
150 else if (resource->target == GL_TEXTURE_1D_ARRAY || resource->target == GL_TEXTURE_2D_ARRAY
151 || resource->target == GL_TEXTURE_3D)
153 if (!gl_info->fbo_ops.glFramebufferTextureLayer)
155 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
156 return;
159 gl_info->fbo_ops.glFramebufferTextureLayer(fbo_target, attachment,
160 resource->object, resource->level, resource->layer);
162 else if (resource->target == GL_TEXTURE_1D)
164 gl_info->fbo_ops.glFramebufferTexture1D(fbo_target, attachment,
165 resource->target, resource->object, resource->level);
167 else
169 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment,
170 resource->target, resource->object, resource->level);
172 checkGLcall("attach texture to fbo");
175 /* Context activation is done by the caller. */
176 static void wined3d_context_gl_attach_depth_stencil_fbo(struct wined3d_context_gl *context_gl,
177 GLenum fbo_target, const struct wined3d_fbo_resource *resource, BOOL rb_namespace,
178 uint32_t flags)
180 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
182 if (resource->object)
184 TRACE("Attach depth stencil %u.\n", resource->object);
186 if (rb_namespace)
188 context_attach_depth_stencil_rb(gl_info, fbo_target,
189 flags, resource->object);
191 else
193 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
194 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_DEPTH_ATTACHMENT, resource);
196 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
197 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_STENCIL_ATTACHMENT, resource);
200 if (!(flags & WINED3D_FBO_ENTRY_FLAG_DEPTH))
201 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
203 if (!(flags & WINED3D_FBO_ENTRY_FLAG_STENCIL))
204 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
206 else
208 TRACE("Attach depth stencil 0.\n");
210 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
211 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
215 /* Context activation is done by the caller. */
216 static void wined3d_context_gl_attach_surface_fbo(struct wined3d_context_gl *context_gl,
217 GLenum fbo_target, unsigned int idx, const struct wined3d_fbo_resource *resource, BOOL rb_namespace)
219 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
221 TRACE("Attach GL object %u to %u.\n", resource->object, idx);
223 if (resource->object)
225 if (rb_namespace)
227 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
228 GL_RENDERBUFFER, resource->object);
229 checkGLcall("glFramebufferRenderbuffer()");
231 else
233 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_COLOR_ATTACHMENT0 + idx, resource);
236 else
238 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_COLOR_ATTACHMENT0 + idx, NULL);
242 static void context_dump_fbo_attachment(const struct wined3d_gl_info *gl_info, GLenum target,
243 GLenum attachment)
245 static const struct
247 GLenum target;
248 GLenum binding;
249 const char *str;
250 enum wined3d_gl_extension extension;
252 texture_type[] =
254 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D, "1d", WINED3D_GL_EXT_NONE},
255 {GL_TEXTURE_1D_ARRAY, GL_TEXTURE_BINDING_1D_ARRAY, "1d-array", EXT_TEXTURE_ARRAY},
256 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, "2d", WINED3D_GL_EXT_NONE},
257 {GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_BINDING_RECTANGLE_ARB, "rectangle", ARB_TEXTURE_RECTANGLE},
258 {GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BINDING_2D_ARRAY, "2d-array" , EXT_TEXTURE_ARRAY},
259 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, "cube", ARB_TEXTURE_CUBE_MAP},
260 {GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_BINDING_2D_MULTISAMPLE, "2d-ms", ARB_TEXTURE_MULTISAMPLE},
261 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY, "2d-array-ms", ARB_TEXTURE_MULTISAMPLE},
264 GLint type, name, samples, width, height, old_texture, level, face, fmt, tex_target;
265 const char *tex_type_str;
266 unsigned int i;
268 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
269 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &name);
270 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
271 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &type);
273 if (type == GL_RENDERBUFFER)
275 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, name);
276 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
277 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
278 if (gl_info->limits.samples > 1)
279 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
280 else
281 samples = 1;
282 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &fmt);
283 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
284 debug_fboattachment(attachment), name, width, height, samples, fmt);
286 else if (type == GL_TEXTURE)
288 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
289 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL, &level);
290 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
291 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE, &face);
293 if (gl_info->gl_ops.ext.p_glGetTextureParameteriv)
295 GL_EXTCALL(glGetTextureParameteriv(name, GL_TEXTURE_TARGET, &tex_target));
297 for (i = 0; i < ARRAY_SIZE(texture_type); ++i)
299 if (texture_type[i].target == tex_target)
301 tex_type_str = texture_type[i].str;
302 break;
305 if (i == ARRAY_SIZE(texture_type))
306 tex_type_str = wine_dbg_sprintf("%#x", tex_target);
308 else if (face)
310 gl_info->gl_ops.gl.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &old_texture);
311 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, name);
313 tex_target = GL_TEXTURE_CUBE_MAP;
314 tex_type_str = "cube";
316 else
318 tex_type_str = NULL;
320 for (i = 0; i < ARRAY_SIZE(texture_type); ++i)
322 if (!gl_info->supported[texture_type[i].extension])
323 continue;
325 gl_info->gl_ops.gl.p_glGetIntegerv(texture_type[i].binding, &old_texture);
326 while (gl_info->gl_ops.gl.p_glGetError());
328 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, name);
329 if (!gl_info->gl_ops.gl.p_glGetError())
331 tex_target = texture_type[i].target;
332 tex_type_str = texture_type[i].str;
333 break;
335 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, old_texture);
338 if (!tex_type_str)
340 FIXME("Cannot find type of texture %d.\n", name);
341 return;
345 if (gl_info->gl_ops.ext.p_glGetTextureParameteriv)
347 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt));
348 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_WIDTH, &width));
349 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_HEIGHT, &height));
350 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_SAMPLES, &samples));
352 else
354 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
355 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_WIDTH, &width);
356 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_HEIGHT, &height);
357 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
358 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_SAMPLES, &samples);
359 else
360 samples = 1;
362 gl_info->gl_ops.gl.p_glBindTexture(tex_target, old_texture);
365 FIXME(" %s: %s texture %d, %dx%d, %d samples, format %#x.\n",
366 debug_fboattachment(attachment), tex_type_str, name, width, height, samples, fmt);
368 else if (type == GL_NONE)
370 FIXME(" %s: NONE.\n", debug_fboattachment(attachment));
372 else
374 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment), type);
377 checkGLcall("dump FBO attachment");
380 /* Context activation is done by the caller. */
381 void wined3d_context_gl_check_fbo_status(const struct wined3d_context_gl *context_gl, GLenum target)
383 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
384 GLenum status;
386 if (!FIXME_ON(d3d))
387 return;
389 status = gl_info->fbo_ops.glCheckFramebufferStatus(target);
390 if (status == GL_FRAMEBUFFER_COMPLETE)
392 TRACE("FBO complete.\n");
394 else
396 unsigned int i;
398 FIXME("FBO status %s (%#x).\n", debug_fbostatus(status), status);
400 if (!context_gl->current_fbo)
402 ERR("FBO 0 is incomplete, driver bug?\n");
403 return;
406 context_dump_fbo_attachment(gl_info, target, GL_DEPTH_ATTACHMENT);
407 context_dump_fbo_attachment(gl_info, target, GL_STENCIL_ATTACHMENT);
409 for (i = 0; i < gl_info->limits.buffers; ++i)
410 context_dump_fbo_attachment(gl_info, target, GL_COLOR_ATTACHMENT0 + i);
414 static inline DWORD context_generate_rt_mask(GLenum buffer)
416 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
417 return buffer ? (1u << 31) | buffer : 0;
420 static inline DWORD context_generate_rt_mask_from_resource(struct wined3d_resource *resource)
422 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
424 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
425 return 0;
428 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource));
431 static inline void wined3d_context_gl_set_fbo_key_for_render_target(const struct wined3d_context_gl *context_gl,
432 struct wined3d_fbo_entry_key *key, unsigned int idx, const struct wined3d_rendertarget_info *render_target,
433 DWORD location)
435 unsigned int sub_resource_idx = render_target->sub_resource_idx;
436 struct wined3d_resource *resource = render_target->resource;
437 struct wined3d_texture_gl *texture_gl;
439 if (!resource || resource->format->id == WINED3DFMT_NULL || resource->type == WINED3D_RTYPE_BUFFER)
441 if (resource && resource->type == WINED3D_RTYPE_BUFFER)
442 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
443 key->objects[idx].object = 0;
444 key->objects[idx].target = 0;
445 key->objects[idx].level = key->objects[idx].layer = 0;
446 return;
449 if (render_target->gl_view.name)
451 key->objects[idx].object = render_target->gl_view.name;
452 key->objects[idx].target = render_target->gl_view.target;
453 key->objects[idx].level = 0;
454 key->objects[idx].layer = WINED3D_ALL_LAYERS;
455 return;
458 texture_gl = wined3d_texture_gl(wined3d_texture_from_resource(resource));
459 if (texture_gl->current_renderbuffer)
461 key->objects[idx].object = texture_gl->current_renderbuffer->id;
462 key->objects[idx].target = 0;
463 key->objects[idx].level = key->objects[idx].layer = 0;
464 key->rb_namespace |= 1 << idx;
465 return;
468 key->objects[idx].target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
469 key->objects[idx].level = sub_resource_idx % texture_gl->t.level_count;
470 key->objects[idx].layer = sub_resource_idx / texture_gl->t.level_count;
472 if (render_target->layer_count != 1)
473 key->objects[idx].layer = WINED3D_ALL_LAYERS;
475 switch (location)
477 case WINED3D_LOCATION_TEXTURE_RGB:
478 key->objects[idx].object = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, FALSE);
479 break;
481 case WINED3D_LOCATION_TEXTURE_SRGB:
482 key->objects[idx].object = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, TRUE);
483 break;
485 case WINED3D_LOCATION_RB_MULTISAMPLE:
486 key->objects[idx].object = texture_gl->rb_multisample;
487 key->objects[idx].target = 0;
488 key->objects[idx].level = key->objects[idx].layer = 0;
489 key->rb_namespace |= 1 << idx;
490 break;
492 case WINED3D_LOCATION_RB_RESOLVED:
493 key->objects[idx].object = texture_gl->rb_resolved;
494 key->objects[idx].target = 0;
495 key->objects[idx].level = key->objects[idx].layer = 0;
496 key->rb_namespace |= 1 << idx;
497 break;
501 static void wined3d_context_gl_generate_fbo_key(const struct wined3d_context_gl *context_gl,
502 struct wined3d_fbo_entry_key *key, const struct wined3d_rendertarget_info *render_targets,
503 const struct wined3d_rendertarget_info *depth_stencil, DWORD color_location, DWORD ds_location)
505 unsigned int buffers = context_gl->gl_info->limits.buffers;
506 unsigned int i;
508 key->rb_namespace = 0;
509 wined3d_context_gl_set_fbo_key_for_render_target(context_gl, key, 0, depth_stencil, ds_location);
511 for (i = 0; i < buffers; ++i)
512 wined3d_context_gl_set_fbo_key_for_render_target(context_gl, key, i + 1, &render_targets[i], color_location);
514 memset(&key->objects[buffers + 1], 0, (ARRAY_SIZE(key->objects) - buffers - 1) * sizeof(*key->objects));
517 static struct fbo_entry *wined3d_context_gl_create_fbo_entry(const struct wined3d_context_gl *context_gl,
518 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
519 DWORD color_location, DWORD ds_location)
521 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
522 struct fbo_entry *entry;
524 entry = heap_alloc(sizeof(*entry));
525 wined3d_context_gl_generate_fbo_key(context_gl, &entry->key,
526 render_targets, depth_stencil, color_location, ds_location);
527 entry->flags = 0;
528 if (depth_stencil->resource)
530 if (depth_stencil->resource->format_flags & WINED3DFMT_FLAG_DEPTH)
531 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
532 if (depth_stencil->resource->format_flags & WINED3DFMT_FLAG_STENCIL)
533 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
535 entry->rt_mask = context_generate_rt_mask(GL_COLOR_ATTACHMENT0);
536 gl_info->fbo_ops.glGenFramebuffers(1, &entry->id);
537 checkGLcall("glGenFramebuffers()");
538 TRACE("Created FBO %u.\n", entry->id);
540 return entry;
543 /* Context activation is done by the caller. */
544 static void wined3d_context_gl_reuse_fbo_entry(struct wined3d_context_gl *context_gl, GLenum target,
545 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
546 DWORD color_location, DWORD ds_location, struct fbo_entry *entry)
548 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
550 wined3d_context_gl_bind_fbo(context_gl, target, entry->id);
551 context_clean_fbo_attachments(gl_info, target);
553 wined3d_context_gl_generate_fbo_key(context_gl, &entry->key,
554 render_targets, depth_stencil, color_location, ds_location);
555 entry->flags = 0;
556 if (depth_stencil->resource)
558 if (depth_stencil->resource->format_flags & WINED3DFMT_FLAG_DEPTH)
559 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
560 if (depth_stencil->resource->format_flags & WINED3DFMT_FLAG_STENCIL)
561 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
565 /* Context activation is done by the caller. */
566 static void wined3d_context_gl_destroy_fbo_entry(struct wined3d_context_gl *context_gl, struct fbo_entry *entry)
568 if (entry->id)
570 TRACE("Destroy FBO %u.\n", entry->id);
571 wined3d_context_gl_destroy_fbo(context_gl, entry->id);
573 --context_gl->fbo_entry_count;
574 list_remove(&entry->entry);
575 heap_free(entry);
578 /* Context activation is done by the caller. */
579 static struct fbo_entry *wined3d_context_gl_find_fbo_entry(struct wined3d_context_gl *context_gl, GLenum target,
580 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
581 DWORD color_location, DWORD ds_location)
583 static const struct wined3d_rendertarget_info ds_null = {{0}};
584 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
585 struct wined3d_texture *rt_texture, *ds_texture;
586 struct wined3d_fbo_entry_key fbo_key;
587 unsigned int i, ds_level, rt_level;
588 struct fbo_entry *entry;
590 if (depth_stencil->resource && depth_stencil->resource->type != WINED3D_RTYPE_BUFFER
591 && render_targets[0].resource && render_targets[0].resource->type != WINED3D_RTYPE_BUFFER)
593 rt_texture = wined3d_texture_from_resource(render_targets[0].resource);
594 rt_level = render_targets[0].sub_resource_idx % rt_texture->level_count;
595 ds_texture = wined3d_texture_from_resource(depth_stencil->resource);
596 ds_level = depth_stencil->sub_resource_idx % ds_texture->level_count;
598 if (wined3d_texture_get_level_width(ds_texture, ds_level)
599 < wined3d_texture_get_level_width(rt_texture, rt_level)
600 || wined3d_texture_get_level_height(ds_texture, ds_level)
601 < wined3d_texture_get_level_height(rt_texture, rt_level))
603 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
604 depth_stencil = &ds_null;
606 else if (ds_texture->resource.multisample_type != rt_texture->resource.multisample_type
607 || (ds_texture->resource.multisample_type
608 && ds_texture->resource.multisample_quality != rt_texture->resource.multisample_quality))
610 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
611 rt_texture->resource.multisample_type, rt_texture->resource.multisample_quality,
612 ds_texture->resource.multisample_type, ds_texture->resource.multisample_quality);
613 depth_stencil = &ds_null;
615 else if (depth_stencil->resource->type == WINED3D_RTYPE_TEXTURE_2D)
617 wined3d_texture_gl_set_compatible_renderbuffer(wined3d_texture_gl(ds_texture),
618 context_gl, ds_level, &render_targets[0]);
622 wined3d_context_gl_generate_fbo_key(context_gl, &fbo_key,
623 render_targets, depth_stencil, color_location, ds_location);
625 if (TRACE_ON(d3d))
627 struct wined3d_resource *resource;
628 unsigned int width, height;
629 const char *resource_type;
631 TRACE("Dumping FBO attachments:\n");
632 for (i = 0; i < gl_info->limits.buffers; ++i)
634 if ((resource = render_targets[i].resource))
636 if (resource->type == WINED3D_RTYPE_BUFFER)
638 width = resource->size;
639 height = 1;
640 resource_type = "buffer";
642 else
644 rt_texture = wined3d_texture_from_resource(resource);
645 rt_level = render_targets[i].sub_resource_idx % rt_texture->level_count;
646 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
647 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
648 resource_type = "texture";
651 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
652 i, resource, render_targets[i].sub_resource_idx, debug_d3dformat(resource->format->id),
653 fbo_key.rb_namespace & (1 << (i + 1)) ? "renderbuffer" : resource_type,
654 fbo_key.objects[i + 1].object, width, height, resource->multisample_type);
657 if ((resource = depth_stencil->resource))
659 if (resource->type == WINED3D_RTYPE_BUFFER)
661 width = resource->size;
662 height = 1;
663 resource_type = "buffer";
665 else
667 ds_texture = wined3d_texture_from_resource(resource);
668 ds_level = depth_stencil->sub_resource_idx % ds_texture->level_count;
669 width = wined3d_texture_get_level_pow2_width(ds_texture, ds_level);
670 height = wined3d_texture_get_level_pow2_height(ds_texture, ds_level);
671 resource_type = "texture";
674 TRACE(" Depth attachment: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
675 resource, depth_stencil->sub_resource_idx, debug_d3dformat(resource->format->id),
676 fbo_key.rb_namespace & (1 << 0) ? "renderbuffer" : resource_type,
677 fbo_key.objects[0].object, width, height, resource->multisample_type);
681 LIST_FOR_EACH_ENTRY(entry, &context_gl->fbo_list, struct fbo_entry, entry)
683 if (memcmp(&fbo_key, &entry->key, sizeof(fbo_key)))
684 continue;
686 list_remove(&entry->entry);
687 list_add_head(&context_gl->fbo_list, &entry->entry);
688 return entry;
691 if (context_gl->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
693 entry = wined3d_context_gl_create_fbo_entry(context_gl,
694 render_targets, depth_stencil, color_location, ds_location);
695 list_add_head(&context_gl->fbo_list, &entry->entry);
696 ++context_gl->fbo_entry_count;
698 else
700 entry = LIST_ENTRY(list_tail(&context_gl->fbo_list), struct fbo_entry, entry);
701 wined3d_context_gl_reuse_fbo_entry(context_gl, target, render_targets,
702 depth_stencil, color_location, ds_location, entry);
703 list_remove(&entry->entry);
704 list_add_head(&context_gl->fbo_list, &entry->entry);
707 return entry;
710 /* Context activation is done by the caller. */
711 static void wined3d_context_gl_apply_fbo_entry(struct wined3d_context_gl *context_gl,
712 GLenum target, struct fbo_entry *entry)
714 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
715 GLuint read_binding, draw_binding;
716 unsigned int i;
718 if (entry->flags & WINED3D_FBO_ENTRY_FLAG_ATTACHED)
720 wined3d_context_gl_bind_fbo(context_gl, target, entry->id);
721 return;
724 read_binding = context_gl->fbo_read_binding;
725 draw_binding = context_gl->fbo_draw_binding;
726 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, entry->id);
728 if (gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
730 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER,
731 GL_FRAMEBUFFER_DEFAULT_WIDTH, gl_info->limits.framebuffer_width));
732 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER,
733 GL_FRAMEBUFFER_DEFAULT_HEIGHT, gl_info->limits.framebuffer_height));
734 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS, 1));
735 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES, 1));
738 /* Apply render targets */
739 for (i = 0; i < gl_info->limits.buffers; ++i)
741 wined3d_context_gl_attach_surface_fbo(context_gl, target, i,
742 &entry->key.objects[i + 1], entry->key.rb_namespace & (1 << (i + 1)));
745 wined3d_context_gl_attach_depth_stencil_fbo(context_gl, target,
746 &entry->key.objects[0], entry->key.rb_namespace & 0x1, entry->flags);
748 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
749 * GL contexts requirements. */
750 gl_info->gl_ops.gl.p_glReadBuffer(GL_NONE);
751 wined3d_context_gl_set_draw_buffer(context_gl, GL_NONE);
752 if (target != GL_FRAMEBUFFER)
754 if (target == GL_READ_FRAMEBUFFER)
755 wined3d_context_gl_bind_fbo(context_gl, GL_DRAW_FRAMEBUFFER, draw_binding);
756 else
757 wined3d_context_gl_bind_fbo(context_gl, GL_READ_FRAMEBUFFER, read_binding);
760 entry->flags |= WINED3D_FBO_ENTRY_FLAG_ATTACHED;
763 /* Context activation is done by the caller. */
764 static void wined3d_context_gl_apply_fbo_state(struct wined3d_context_gl *context_gl, GLenum target,
765 const struct wined3d_rendertarget_info *render_targets,
766 const struct wined3d_rendertarget_info *depth_stencil, DWORD color_location, DWORD ds_location)
768 struct fbo_entry *entry, *entry2;
770 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_destroy_list, struct fbo_entry, entry)
772 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
775 if (context_gl->rebind_fbo)
777 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
778 context_gl->rebind_fbo = FALSE;
781 if (color_location == WINED3D_LOCATION_DRAWABLE)
783 context_gl->current_fbo = NULL;
784 wined3d_context_gl_bind_fbo(context_gl, target, 0);
786 else
788 context_gl->current_fbo = wined3d_context_gl_find_fbo_entry(context_gl,
789 target, render_targets, depth_stencil, color_location, ds_location);
790 wined3d_context_gl_apply_fbo_entry(context_gl, target, context_gl->current_fbo);
794 /* Context activation is done by the caller. */
795 void wined3d_context_gl_apply_fbo_state_blit(struct wined3d_context_gl *context_gl, GLenum target,
796 struct wined3d_resource *rt, unsigned int rt_sub_resource_idx,
797 struct wined3d_resource *ds, unsigned int ds_sub_resource_idx, DWORD location)
799 struct wined3d_rendertarget_info ds_info = {{0}};
801 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
802 if (rt)
804 context_gl->blit_targets[0].resource = rt;
805 context_gl->blit_targets[0].sub_resource_idx = rt_sub_resource_idx;
806 context_gl->blit_targets[0].layer_count = 1;
809 if (ds)
811 ds_info.resource = ds;
812 ds_info.sub_resource_idx = ds_sub_resource_idx;
813 ds_info.layer_count = 1;
816 wined3d_context_gl_apply_fbo_state(context_gl, target, context_gl->blit_targets, &ds_info, location, location);
819 /* Context activation is done by the caller. */
820 void wined3d_context_gl_alloc_occlusion_query(struct wined3d_context_gl *context_gl,
821 struct wined3d_occlusion_query *query)
823 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
825 if (context_gl->free_occlusion_query_count)
827 query->id = context_gl->free_occlusion_queries[--context_gl->free_occlusion_query_count];
829 else
831 if (gl_info->supported[ARB_OCCLUSION_QUERY])
833 GL_EXTCALL(glGenQueries(1, &query->id));
834 checkGLcall("glGenQueries");
836 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context_gl);
838 else
840 WARN("Occlusion queries not supported, not allocating query id.\n");
841 query->id = 0;
845 query->context_gl = context_gl;
846 list_add_head(&context_gl->occlusion_queries, &query->entry);
849 void wined3d_context_gl_free_occlusion_query(struct wined3d_occlusion_query *query)
851 struct wined3d_context_gl *context_gl = query->context_gl;
853 list_remove(&query->entry);
854 query->context_gl = NULL;
856 if (!wined3d_array_reserve((void **)&context_gl->free_occlusion_queries,
857 &context_gl->free_occlusion_query_size, context_gl->free_occlusion_query_count + 1,
858 sizeof(*context_gl->free_occlusion_queries)))
860 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context_gl);
861 return;
864 context_gl->free_occlusion_queries[context_gl->free_occlusion_query_count++] = query->id;
867 /* Context activation is done by the caller. */
868 void wined3d_context_gl_alloc_fence(struct wined3d_context_gl *context_gl, struct wined3d_fence *fence)
870 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
872 if (context_gl->free_fence_count)
874 fence->object = context_gl->free_fences[--context_gl->free_fence_count];
876 else
878 if (gl_info->supported[ARB_SYNC])
880 /* Using ARB_sync, not much to do here. */
881 fence->object.sync = NULL;
882 TRACE("Allocated sync object in context %p.\n", context_gl);
884 else if (gl_info->supported[APPLE_FENCE])
886 GL_EXTCALL(glGenFencesAPPLE(1, &fence->object.id));
887 checkGLcall("glGenFencesAPPLE");
889 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context_gl);
891 else if(gl_info->supported[NV_FENCE])
893 GL_EXTCALL(glGenFencesNV(1, &fence->object.id));
894 checkGLcall("glGenFencesNV");
896 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context_gl);
898 else
900 WARN("Fences not supported, not allocating fence.\n");
901 fence->object.id = 0;
905 fence->context_gl = context_gl;
906 list_add_head(&context_gl->fences, &fence->entry);
909 void wined3d_context_gl_free_fence(struct wined3d_fence *fence)
911 struct wined3d_context_gl *context_gl = fence->context_gl;
913 list_remove(&fence->entry);
914 fence->context_gl = NULL;
916 if (!wined3d_array_reserve((void **)&context_gl->free_fences,
917 &context_gl->free_fence_size, context_gl->free_fence_count + 1,
918 sizeof(*context_gl->free_fences)))
920 ERR("Failed to grow free list, leaking fence %u in context %p.\n", fence->object.id, context_gl);
921 return;
924 context_gl->free_fences[context_gl->free_fence_count++] = fence->object;
927 /* Context activation is done by the caller. */
928 void wined3d_context_gl_alloc_timestamp_query(struct wined3d_context_gl *context_gl,
929 struct wined3d_timestamp_query *query)
931 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
933 if (context_gl->free_timestamp_query_count)
935 query->id = context_gl->free_timestamp_queries[--context_gl->free_timestamp_query_count];
937 else
939 GL_EXTCALL(glGenQueries(1, &query->id));
940 checkGLcall("glGenQueries");
942 TRACE("Allocated timestamp query %u in context %p.\n", query->id, context_gl);
945 query->context_gl = context_gl;
946 list_add_head(&context_gl->timestamp_queries, &query->entry);
949 void wined3d_context_gl_free_timestamp_query(struct wined3d_timestamp_query *query)
951 struct wined3d_context_gl *context_gl = query->context_gl;
953 list_remove(&query->entry);
954 query->context_gl = NULL;
956 if (!wined3d_array_reserve((void **)&context_gl->free_timestamp_queries,
957 &context_gl->free_timestamp_query_size, context_gl->free_timestamp_query_count + 1,
958 sizeof(*context_gl->free_timestamp_queries)))
960 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context_gl);
961 return;
964 context_gl->free_timestamp_queries[context_gl->free_timestamp_query_count++] = query->id;
967 void wined3d_context_gl_alloc_so_statistics_query(struct wined3d_context_gl *context_gl,
968 struct wined3d_so_statistics_query *query)
970 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
972 if (context_gl->free_so_statistics_query_count)
974 query->u = context_gl->free_so_statistics_queries[--context_gl->free_so_statistics_query_count];
976 else
978 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
979 checkGLcall("glGenQueries");
981 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
982 query->u.id[0], query->u.id[1], context_gl);
985 query->context_gl = context_gl;
986 list_add_head(&context_gl->so_statistics_queries, &query->entry);
989 void wined3d_context_gl_free_so_statistics_query(struct wined3d_so_statistics_query *query)
991 struct wined3d_context_gl *context_gl = query->context_gl;
993 list_remove(&query->entry);
994 query->context_gl = NULL;
996 if (!wined3d_array_reserve((void **)&context_gl->free_so_statistics_queries,
997 &context_gl->free_so_statistics_query_size, context_gl->free_so_statistics_query_count + 1,
998 sizeof(*context_gl->free_so_statistics_queries)))
1000 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
1001 query->u.id[0], query->u.id[1], context_gl);
1002 return;
1005 context_gl->free_so_statistics_queries[context_gl->free_so_statistics_query_count++] = query->u;
1008 void wined3d_context_gl_alloc_pipeline_statistics_query(struct wined3d_context_gl *context_gl,
1009 struct wined3d_pipeline_statistics_query *query)
1011 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1013 if (context_gl->free_pipeline_statistics_query_count)
1015 query->u = context_gl->free_pipeline_statistics_queries[--context_gl->free_pipeline_statistics_query_count];
1017 else
1019 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
1020 checkGLcall("glGenQueries");
1023 query->context_gl = context_gl;
1024 list_add_head(&context_gl->pipeline_statistics_queries, &query->entry);
1027 void wined3d_context_gl_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query *query)
1029 struct wined3d_context_gl *context_gl = query->context_gl;
1031 list_remove(&query->entry);
1032 query->context_gl = NULL;
1034 if (!wined3d_array_reserve((void **)&context_gl->free_pipeline_statistics_queries,
1035 &context_gl->free_pipeline_statistics_query_size, context_gl->free_pipeline_statistics_query_count + 1,
1036 sizeof(*context_gl->free_pipeline_statistics_queries)))
1038 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context_gl);
1039 return;
1042 context_gl->free_pipeline_statistics_queries[context_gl->free_pipeline_statistics_query_count++] = query->u;
1045 typedef void (context_fbo_entry_func_t)(struct wined3d_context_gl *context_gl, struct fbo_entry *entry);
1047 static void wined3d_context_gl_enum_fbo_entries(const struct wined3d_device *device,
1048 GLuint name, BOOL rb_namespace, context_fbo_entry_func_t *callback)
1050 unsigned int i, j;
1052 for (i = 0; i < device->context_count; ++i)
1054 struct wined3d_context_gl *context_gl = wined3d_context_gl(device->contexts[i]);
1055 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1056 struct fbo_entry *entry, *entry2;
1058 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_list, struct fbo_entry, entry)
1060 for (j = 0; j < gl_info->limits.buffers + 1; ++j)
1062 if (entry->key.objects[j].object == name
1063 && !(entry->key.rb_namespace & (1 << j)) == !rb_namespace)
1065 callback(context_gl, entry);
1066 break;
1073 static void wined3d_context_gl_queue_fbo_entry_destruction(struct wined3d_context_gl *context_gl,
1074 struct fbo_entry *entry)
1076 list_remove(&entry->entry);
1077 list_add_head(&context_gl->fbo_destroy_list, &entry->entry);
1080 void context_resource_released(const struct wined3d_device *device, struct wined3d_resource *resource)
1082 unsigned int i;
1084 if (!device->d3d_initialized)
1085 return;
1087 for (i = 0; i < device->context_count; ++i)
1089 struct wined3d_context *context = device->contexts[i];
1091 if (&context->current_rt.texture->resource == resource)
1093 context->current_rt.texture = NULL;
1094 context->current_rt.sub_resource_idx = 0;
1099 void context_gl_resource_released(struct wined3d_device *device, GLuint name, BOOL rb_namespace)
1101 wined3d_context_gl_enum_fbo_entries(device, name, rb_namespace,
1102 wined3d_context_gl_queue_fbo_entry_destruction);
1105 void wined3d_context_gl_texture_update(struct wined3d_context_gl *context_gl,
1106 const struct wined3d_texture_gl *texture_gl)
1108 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1109 struct fbo_entry *entry = context_gl->current_fbo;
1110 unsigned int i;
1112 if (!entry || context_gl->rebind_fbo)
1113 return;
1115 for (i = 0; i < gl_info->limits.buffers + 1; ++i)
1117 if (texture_gl->texture_rgb.name == entry->key.objects[i].object
1118 || texture_gl->texture_srgb.name == entry->key.objects[i].object)
1120 TRACE("Updated texture %p is bound as attachment %u to the current FBO.\n", texture_gl, i);
1121 context_gl->rebind_fbo = TRUE;
1122 return;
1127 static BOOL wined3d_context_gl_restore_pixel_format(struct wined3d_context_gl *context_gl)
1129 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1130 BOOL ret = FALSE;
1132 if (context_gl->restore_pf && IsWindow(context_gl->restore_pf_win))
1134 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1136 HDC dc = GetDCEx(context_gl->restore_pf_win, 0, DCX_USESTYLE | DCX_CACHE);
1137 if (dc)
1139 if (!(ret = GL_EXTCALL(wglSetPixelFormatWINE(dc, context_gl->restore_pf))))
1141 ERR("Failed to restore pixel format %d on window %p.\n",
1142 context_gl->restore_pf, context_gl->restore_pf_win);
1144 ReleaseDC(context_gl->restore_pf_win, dc);
1147 else
1149 ERR("Unable to restore pixel format %d on window %p.\n",
1150 context_gl->restore_pf, context_gl->restore_pf_win);
1154 context_gl->restore_pf = 0;
1155 context_gl->restore_pf_win = NULL;
1156 return ret;
1159 static BOOL wined3d_context_gl_set_pixel_format(struct wined3d_context_gl *context_gl)
1161 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1162 BOOL private = context_gl->dc_is_private;
1163 int format = context_gl->pixel_format;
1164 HDC dc = context_gl->dc;
1165 int current;
1167 if (private && context_gl->dc_has_format)
1168 return TRUE;
1170 if (!private && WindowFromDC(dc) != context_gl->window)
1171 return FALSE;
1173 current = gl_info->gl_ops.wgl.p_wglGetPixelFormat(dc);
1174 if (current == format) goto success;
1176 if (!current)
1178 if (!SetPixelFormat(dc, format, NULL))
1180 /* This may also happen if the dc belongs to a destroyed window. */
1181 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1182 format, dc, GetLastError());
1183 return FALSE;
1186 context_gl->restore_pf = 0;
1187 context_gl->restore_pf_win = private ? NULL : WindowFromDC(dc);
1188 goto success;
1191 /* By default WGL doesn't allow pixel format adjustments but we need it
1192 * here. For this reason there's a Wine specific wglSetPixelFormat()
1193 * which allows us to set the pixel format multiple times. Only use it
1194 * when really needed. */
1195 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1197 HWND win;
1199 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
1201 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1202 format, dc);
1203 return FALSE;
1206 win = private ? NULL : WindowFromDC(dc);
1207 if (win != context_gl->restore_pf_win)
1209 wined3d_context_gl_restore_pixel_format(context_gl);
1211 context_gl->restore_pf = private ? 0 : current;
1212 context_gl->restore_pf_win = win;
1215 goto success;
1218 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1219 * continue using the old format. There's a big chance that the old
1220 * format works although with a performance hit and perhaps rendering
1221 * errors. */
1222 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1223 format, dc, current);
1224 return TRUE;
1226 success:
1227 if (private)
1228 context_gl->dc_has_format = TRUE;
1229 return TRUE;
1232 static BOOL wined3d_context_gl_set_gl_context(struct wined3d_context_gl *context_gl)
1234 struct wined3d_swapchain_gl *swapchain_gl = wined3d_swapchain_gl(context_gl->c.swapchain);
1235 BOOL backup = FALSE;
1237 if (!wined3d_context_gl_set_pixel_format(context_gl))
1239 WARN("Failed to set pixel format %d on device context %p.\n",
1240 context_gl->pixel_format, context_gl->dc);
1241 backup = TRUE;
1244 if (backup || !wglMakeCurrent(context_gl->dc, context_gl->gl_ctx))
1246 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1247 context_gl->gl_ctx, context_gl->dc, GetLastError());
1248 context_gl->valid = 0;
1249 WARN("Trying fallback to the backup window.\n");
1251 /* FIXME: If the context is destroyed it's no longer associated with
1252 * a swapchain, so we can't use the swapchain to get a backup dc. To
1253 * make this work windowless contexts would need to be handled by the
1254 * device. */
1255 if (context_gl->c.destroyed || !swapchain_gl)
1257 FIXME("Unable to get backup dc for destroyed context %p.\n", context_gl);
1258 wined3d_context_gl_set_current(NULL);
1259 return FALSE;
1262 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
1264 wined3d_context_gl_set_current(NULL);
1265 return FALSE;
1268 context_gl->dc_is_private = TRUE;
1269 context_gl->dc_has_format = FALSE;
1271 if (!wined3d_context_gl_set_pixel_format(context_gl))
1273 ERR("Failed to set pixel format %d on device context %p.\n",
1274 context_gl->pixel_format, context_gl->dc);
1275 wined3d_context_gl_set_current(NULL);
1276 return FALSE;
1279 if (!wglMakeCurrent(context_gl->dc, context_gl->gl_ctx))
1281 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1282 context_gl->dc, GetLastError());
1283 wined3d_context_gl_set_current(NULL);
1284 return FALSE;
1287 context_gl->valid = 1;
1289 context_gl->needs_set = 0;
1291 return TRUE;
1294 static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx)
1296 if (!wglMakeCurrent(dc, gl_ctx))
1298 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1299 gl_ctx, dc, GetLastError());
1300 wined3d_context_gl_set_current(NULL);
1304 static void wined3d_context_gl_update_window(struct wined3d_context_gl *context_gl)
1306 if (!context_gl->c.swapchain)
1307 return;
1309 if (context_gl->window == context_gl->c.swapchain->win_handle)
1310 return;
1312 TRACE("Updating context %p window from %p to %p.\n",
1313 context_gl, context_gl->window, context_gl->c.swapchain->win_handle);
1315 if (context_gl->dc)
1316 wined3d_release_dc(context_gl->window, context_gl->dc);
1318 context_gl->window = context_gl->c.swapchain->win_handle;
1319 context_gl->dc_is_private = FALSE;
1320 context_gl->dc_has_format = FALSE;
1321 context_gl->needs_set = 1;
1322 context_gl->valid = 1;
1324 if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
1326 ERR("Failed to get a device context for window %p.\n", context_gl->window);
1327 context_gl->valid = 0;
1331 void wined3d_context_cleanup(struct wined3d_context *context)
1335 static void wined3d_context_gl_cleanup(struct wined3d_context_gl *context_gl)
1337 struct wined3d_pipeline_statistics_query *pipeline_statistics_query;
1338 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1339 struct wined3d_so_statistics_query *so_statistics_query;
1340 struct wined3d_timestamp_query *timestamp_query;
1341 struct wined3d_occlusion_query *occlusion_query;
1342 struct fbo_entry *entry, *entry2;
1343 struct wined3d_fence *fence;
1344 HGLRC restore_ctx;
1345 HDC restore_dc;
1346 unsigned int i;
1348 restore_ctx = wglGetCurrentContext();
1349 restore_dc = wglGetCurrentDC();
1351 if (restore_ctx == context_gl->gl_ctx)
1352 restore_ctx = NULL;
1353 else if (context_gl->valid)
1354 wined3d_context_gl_set_gl_context(context_gl);
1356 if (context_gl->valid)
1358 if (context_gl->dummy_arbfp_prog)
1359 GL_EXTCALL(glDeleteProgramsARB(1, &context_gl->dummy_arbfp_prog));
1361 if (context_gl->blit_vbo)
1362 GL_EXTCALL(glDeleteBuffers(1, &context_gl->blit_vbo));
1364 for (i = 0; i < context_gl->free_pipeline_statistics_query_count; ++i)
1366 union wined3d_gl_pipeline_statistics_query *q = &context_gl->free_pipeline_statistics_queries[i];
1367 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1370 for (i = 0; i < context_gl->free_so_statistics_query_count; ++i)
1372 union wined3d_gl_so_statistics_query *q = &context_gl->free_so_statistics_queries[i];
1373 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1376 if (context_gl->free_timestamp_query_count)
1377 GL_EXTCALL(glDeleteQueries(context_gl->free_timestamp_query_count, context_gl->free_timestamp_queries));
1379 if (gl_info->supported[ARB_SYNC])
1381 for (i = 0; i < context_gl->free_fence_count; ++i)
1383 GL_EXTCALL(glDeleteSync(context_gl->free_fences[i].sync));
1386 else if (gl_info->supported[APPLE_FENCE])
1388 for (i = 0; i < context_gl->free_fence_count; ++i)
1390 GL_EXTCALL(glDeleteFencesAPPLE(1, &context_gl->free_fences[i].id));
1393 else if (gl_info->supported[NV_FENCE])
1395 for (i = 0; i < context_gl->free_fence_count; ++i)
1397 GL_EXTCALL(glDeleteFencesNV(1, &context_gl->free_fences[i].id));
1401 if (context_gl->free_occlusion_query_count)
1402 GL_EXTCALL(glDeleteQueries(context_gl->free_occlusion_query_count, context_gl->free_occlusion_queries));
1404 checkGLcall("context cleanup");
1406 heap_free(context_gl->free_pipeline_statistics_queries);
1407 heap_free(context_gl->free_so_statistics_queries);
1408 heap_free(context_gl->free_timestamp_queries);
1409 heap_free(context_gl->free_fences);
1410 heap_free(context_gl->free_occlusion_queries);
1412 LIST_FOR_EACH_ENTRY(pipeline_statistics_query, &context_gl->pipeline_statistics_queries,
1413 struct wined3d_pipeline_statistics_query, entry)
1415 if (context_gl->valid)
1416 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query->u.id), pipeline_statistics_query->u.id));
1417 pipeline_statistics_query->context_gl = NULL;
1420 LIST_FOR_EACH_ENTRY(so_statistics_query, &context_gl->so_statistics_queries,
1421 struct wined3d_so_statistics_query, entry)
1423 if (context_gl->valid)
1424 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query->u.id), so_statistics_query->u.id));
1425 so_statistics_query->context_gl = NULL;
1428 LIST_FOR_EACH_ENTRY(timestamp_query, &context_gl->timestamp_queries, struct wined3d_timestamp_query, entry)
1430 if (context_gl->valid)
1431 GL_EXTCALL(glDeleteQueries(1, &timestamp_query->id));
1432 timestamp_query->context_gl = NULL;
1435 LIST_FOR_EACH_ENTRY(fence, &context_gl->fences, struct wined3d_fence, entry)
1437 if (context_gl->valid)
1439 if (gl_info->supported[ARB_SYNC])
1441 if (fence->object.sync)
1442 GL_EXTCALL(glDeleteSync(fence->object.sync));
1444 else if (gl_info->supported[APPLE_FENCE])
1446 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence->object.id));
1448 else if (gl_info->supported[NV_FENCE])
1450 GL_EXTCALL(glDeleteFencesNV(1, &fence->object.id));
1453 fence->context_gl = NULL;
1456 LIST_FOR_EACH_ENTRY(occlusion_query, &context_gl->occlusion_queries, struct wined3d_occlusion_query, entry)
1458 if (context_gl->valid)
1459 GL_EXTCALL(glDeleteQueries(1, &occlusion_query->id));
1460 occlusion_query->context_gl = NULL;
1463 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_destroy_list, struct fbo_entry, entry)
1465 if (!context_gl->valid)
1466 entry->id = 0;
1467 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1470 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_list, struct fbo_entry, entry)
1472 if (!context_gl->valid)
1473 entry->id = 0;
1474 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1477 heap_free(context_gl->texture_type);
1479 wined3d_context_gl_restore_pixel_format(context_gl);
1480 if (restore_ctx)
1481 context_restore_gl_context(gl_info, restore_dc, restore_ctx);
1482 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1483 ERR("Failed to disable GL context.\n");
1485 wined3d_release_dc(context_gl->window, context_gl->dc);
1487 if (!wglDeleteContext(context_gl->gl_ctx))
1489 DWORD err = GetLastError();
1490 ERR("Failed to delete GL context %p, last error %#x.\n", context_gl->gl_ctx, err);
1493 wined3d_context_cleanup(&context_gl->c);
1496 void wined3d_context_vk_cleanup(struct wined3d_context_vk *context_vk)
1498 wined3d_context_cleanup(&context_vk->c);
1501 DWORD context_get_tls_idx(void)
1503 return wined3d_context_tls_idx;
1506 void context_set_tls_idx(DWORD idx)
1508 wined3d_context_tls_idx = idx;
1511 struct wined3d_context_gl *wined3d_context_gl_get_current(void)
1513 return TlsGetValue(wined3d_context_tls_idx);
1516 BOOL wined3d_context_gl_set_current(struct wined3d_context_gl *context_gl)
1518 struct wined3d_context_gl *old = wined3d_context_gl_get_current();
1520 if (old == context_gl)
1522 TRACE("Already using D3D context %p.\n", context_gl);
1523 return TRUE;
1526 if (old)
1528 if (old->c.destroyed)
1530 TRACE("Switching away from destroyed context %p.\n", old);
1531 wined3d_context_gl_cleanup(old);
1532 heap_free((void *)old->gl_info);
1533 heap_free(old);
1535 else
1537 if (wglGetCurrentContext())
1539 const struct wined3d_gl_info *gl_info = old->gl_info;
1540 TRACE("Flushing context %p before switching to %p.\n", old, context_gl);
1541 gl_info->gl_ops.gl.p_glFlush();
1543 old->c.current = 0;
1547 if (context_gl)
1549 if (!context_gl->valid)
1551 ERR("Trying to make invalid context %p current.\n", context_gl);
1552 return FALSE;
1555 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n",
1556 context_gl, context_gl->gl_ctx, context_gl->dc);
1557 if (!wined3d_context_gl_set_gl_context(context_gl))
1558 return FALSE;
1559 context_gl->c.current = 1;
1561 else if (wglGetCurrentContext())
1563 TRACE("Clearing current D3D context.\n");
1564 if (!wglMakeCurrent(NULL, NULL))
1566 DWORD err = GetLastError();
1567 ERR("Failed to clear current GL context, last error %#x.\n", err);
1568 TlsSetValue(wined3d_context_tls_idx, NULL);
1569 return FALSE;
1573 return TlsSetValue(wined3d_context_tls_idx, context_gl);
1576 void wined3d_context_gl_release(struct wined3d_context_gl *context_gl)
1578 TRACE("Releasing context %p, level %u.\n", context_gl, context_gl->level);
1580 if (WARN_ON(d3d))
1582 if (!context_gl->level)
1583 WARN("Context %p is not active.\n", context_gl);
1584 else if (context_gl != wined3d_context_gl_get_current())
1585 WARN("Context %p is not the current context.\n", context_gl);
1588 if (!--context_gl->level)
1590 if (wined3d_context_gl_restore_pixel_format(context_gl))
1591 context_gl->needs_set = 1;
1592 if (context_gl->restore_ctx)
1594 TRACE("Restoring GL context %p on device context %p.\n", context_gl->restore_ctx, context_gl->restore_dc);
1595 context_restore_gl_context(context_gl->gl_info, context_gl->restore_dc, context_gl->restore_ctx);
1596 context_gl->restore_ctx = NULL;
1597 context_gl->restore_dc = NULL;
1600 if (context_gl->c.destroy_delayed)
1602 TRACE("Destroying context %p.\n", context_gl);
1603 wined3d_context_gl_destroy(context_gl);
1608 /* This is used when a context for render target A is active, but a separate context is
1609 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1610 * A to avoid breaking caller code. */
1611 void context_restore(struct wined3d_context *context, struct wined3d_texture *texture, unsigned int sub_resource_idx)
1613 if (context->current_rt.texture != texture || context->current_rt.sub_resource_idx != sub_resource_idx)
1615 context_release(context);
1616 context = context_acquire(texture->resource.device, texture, sub_resource_idx);
1619 context_release(context);
1622 static void wined3d_context_gl_enter(struct wined3d_context_gl *context_gl)
1624 TRACE("Entering context %p, level %u.\n", context_gl, context_gl->level + 1);
1626 if (!context_gl->level++)
1628 const struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
1629 HGLRC current_gl = wglGetCurrentContext();
1631 if (current_gl && (!current_context || current_context->gl_ctx != current_gl))
1633 TRACE("Another GL context (%p on device context %p) is already current.\n",
1634 current_gl, wglGetCurrentDC());
1635 context_gl->restore_ctx = current_gl;
1636 context_gl->restore_dc = wglGetCurrentDC();
1637 context_gl->needs_set = 1;
1639 else if (!context_gl->needs_set && !(context_gl->dc_is_private && context_gl->dc_has_format)
1640 && context_gl->pixel_format != context_gl->gl_info->gl_ops.wgl.p_wglGetPixelFormat(context_gl->dc))
1641 context_gl->needs_set = 1;
1645 void context_invalidate_compute_state(struct wined3d_context *context, DWORD state_id)
1647 DWORD representative = context->state_table[state_id].representative - STATE_COMPUTE_OFFSET;
1648 unsigned int index, shift;
1650 index = representative / (sizeof(*context->dirty_compute_states) * CHAR_BIT);
1651 shift = representative & (sizeof(*context->dirty_compute_states) * CHAR_BIT - 1);
1652 context->dirty_compute_states[index] |= (1u << shift);
1655 void context_invalidate_state(struct wined3d_context *context, unsigned int state_id)
1657 unsigned int representative = context->state_table[state_id].representative;
1658 unsigned int index, shift;
1660 index = representative / (sizeof(*context->dirty_graphics_states) * CHAR_BIT);
1661 shift = representative & ((sizeof(*context->dirty_graphics_states) * CHAR_BIT) - 1);
1662 context->dirty_graphics_states[index] |= (1u << shift);
1665 /* This function takes care of wined3d pixel format selection. */
1666 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1667 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1668 BOOL auxBuffers)
1670 unsigned int cfg_count = wined3d_adapter_gl(device->adapter)->pixel_format_count;
1671 unsigned int current_value;
1672 PIXELFORMATDESCRIPTOR pfd;
1673 int iPixelFormat = 0;
1674 unsigned int i;
1676 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1677 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1678 auxBuffers);
1680 current_value = 0;
1681 for (i = 0; i < cfg_count; ++i)
1683 const struct wined3d_pixel_format *cfg = &wined3d_adapter_gl(device->adapter)->pixel_formats[i];
1684 unsigned int value;
1686 /* For now only accept RGBA formats. Perhaps some day we will
1687 * allow floating point formats for pbuffers. */
1688 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1689 continue;
1690 /* In window mode we need a window drawable format and double buffering. */
1691 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1692 continue;
1693 if (cfg->redSize < color_format->red_size)
1694 continue;
1695 if (cfg->greenSize < color_format->green_size)
1696 continue;
1697 if (cfg->blueSize < color_format->blue_size)
1698 continue;
1699 if (cfg->alphaSize < color_format->alpha_size)
1700 continue;
1701 if (cfg->depthSize < ds_format->depth_size)
1702 continue;
1703 if (ds_format->stencil_size && cfg->stencilSize != ds_format->stencil_size)
1704 continue;
1705 /* Check multisampling support. */
1706 if (cfg->numSamples)
1707 continue;
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 += 1;
1714 if (cfg->stencilSize == ds_format->stencil_size)
1715 value += 2;
1716 if (cfg->alphaSize == color_format->alpha_size)
1717 value += 4;
1718 /* We like to have aux buffers in backbuffer mode */
1719 if (auxBuffers && cfg->auxBuffers)
1720 value += 8;
1721 if (cfg->redSize == color_format->red_size
1722 && cfg->greenSize == color_format->green_size
1723 && cfg->blueSize == color_format->blue_size)
1724 value += 16;
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 void wined3d_context_init(struct wined3d_context *context, struct wined3d_swapchain *swapchain)
1902 struct wined3d_device *device = swapchain->device;
1903 DWORD state;
1905 context->d3d_info = &device->adapter->d3d_info;
1906 context->state_table = device->state_table;
1908 /* Mark all states dirty to force a proper initialization of the states on
1909 * the first use of the context. Compute states do not need initialization. */
1910 for (state = 0; state <= STATE_HIGHEST; ++state)
1912 if (context->state_table[state].representative && !STATE_IS_COMPUTE(state))
1913 context_invalidate_state(context, state);
1916 context->device = device;
1917 context->swapchain = swapchain;
1918 context->current_rt.texture = swapchain->front_buffer;
1919 context->current_rt.sub_resource_idx = 0;
1921 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
1922 | (1u << WINED3D_SHADER_TYPE_VERTEX)
1923 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
1924 | (1u << WINED3D_SHADER_TYPE_HULL)
1925 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
1926 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
1929 HRESULT wined3d_context_no3d_init(struct wined3d_context *context_no3d, struct wined3d_swapchain *swapchain)
1931 TRACE("context_no3d %p, swapchain %p.\n", context_no3d, swapchain);
1933 wined3d_context_init(context_no3d, swapchain);
1935 return WINED3D_OK;
1938 static BOOL wined3d_context_gl_create_wgl_ctx(struct wined3d_context_gl *context_gl,
1939 struct wined3d_swapchain_gl *swapchain_gl)
1941 const struct wined3d_format *colour_format, *ds_format;
1942 struct wined3d_context *context = &context_gl->c;
1943 const struct wined3d_gl_info *gl_info;
1944 struct wined3d_resource *target;
1945 struct wined3d_adapter *adapter;
1946 unsigned int target_bind_flags;
1947 struct wined3d_device *device;
1948 HGLRC ctx, share_ctx;
1949 unsigned int i;
1951 device = context->device;
1952 adapter = device->adapter;
1953 gl_info = &adapter->gl_info;
1955 target = &context->current_rt.texture->resource;
1956 target_bind_flags = target->bind_flags;
1958 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1960 static const enum wined3d_format_id ds_formats[] =
1962 WINED3DFMT_D24_UNORM_S8_UINT,
1963 WINED3DFMT_D32_UNORM,
1964 WINED3DFMT_R24_UNORM_X8_TYPELESS,
1965 WINED3DFMT_D16_UNORM,
1966 WINED3DFMT_S1_UINT_D15_UNORM,
1969 colour_format = target->format;
1971 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1972 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1973 if (colour_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1974 colour_format = wined3d_get_format(adapter, WINED3DFMT_B4G4R4A4_UNORM, target_bind_flags);
1975 else if (colour_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1976 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1978 /* DirectDraw supports 8bit paletted render targets and these are used by
1979 * old games like StarCraft and C&C. Most modern hardware doesn't support
1980 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1981 * conversion (ab)uses the alpha component for storing the palette index.
1982 * For this reason we require a format with 8bit alpha, so request
1983 * A8R8G8B8. */
1984 if (colour_format->id == WINED3DFMT_P8_UINT)
1985 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1987 /* Try to find a pixel format which matches our requirements. */
1988 if (!swapchain_gl->s.ds_format)
1990 for (i = 0; i < ARRAY_SIZE(ds_formats); ++i)
1992 ds_format = wined3d_get_format(adapter, ds_formats[i], WINED3D_BIND_DEPTH_STENCIL);
1993 if ((context_gl->pixel_format = context_choose_pixel_format(device,
1994 context_gl->dc, colour_format, ds_format, TRUE)))
1996 swapchain_gl->s.ds_format = ds_format;
1997 break;
2000 TRACE("Depth stencil format %s is not supported, trying next format.\n",
2001 debug_d3dformat(ds_format->id));
2004 else
2006 context_gl->pixel_format = context_choose_pixel_format(device,
2007 context_gl->dc, colour_format, swapchain_gl->s.ds_format, TRUE);
2010 else
2012 /* When using FBOs for off-screen rendering, we only use the drawable for
2013 * presentation blits, and don't do any rendering to it. That means we
2014 * don't need depth or stencil buffers, and can mostly ignore the render
2015 * target format. This wouldn't necessarily be quite correct for 10bpc
2016 * display modes, but we don't currently support those.
2017 * Using the same format regardless of the colour/depth/stencil targets
2018 * makes it much less likely that different wined3d instances will set
2019 * conflicting pixel formats. */
2020 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
2021 ds_format = wined3d_get_format(adapter, WINED3DFMT_UNKNOWN, WINED3D_BIND_DEPTH_STENCIL);
2022 context_gl->pixel_format = context_choose_pixel_format(device,
2023 context_gl->dc, colour_format, ds_format, FALSE);
2026 if (!context_gl->pixel_format)
2028 ERR("Failed to choose pixel format.\n");
2029 return FALSE;
2032 wined3d_context_gl_enter(context_gl);
2034 if (!wined3d_context_gl_set_pixel_format(context_gl))
2036 context_release(context);
2038 if (context_gl->dc_is_private)
2040 ERR("Failed to set pixel format %d on device context %p.\n", context_gl->pixel_format, context_gl->dc);
2042 return FALSE;
2045 WARN("Failed to set pixel format %d on device context %p, trying backup DC.\n",
2046 context_gl->pixel_format, context_gl->dc);
2048 wined3d_release_dc(context_gl->window, context_gl->dc);
2049 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2051 ERR("Failed to retrieve the backup device context.\n");
2052 return E_FAIL;
2054 context_gl->dc_is_private = TRUE;
2056 return wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl);
2059 share_ctx = device->context_count ? wined3d_context_gl(device->contexts[0])->gl_ctx : NULL;
2060 if (gl_info->p_wglCreateContextAttribsARB)
2062 if (!(ctx = context_create_wgl_attribs(gl_info, context_gl->dc, share_ctx)))
2064 ERR("Failed to create a WGL context.\n");
2065 context_release(context);
2066 return FALSE;
2069 else
2071 if (!(ctx = wglCreateContext(context_gl->dc)))
2073 ERR("Failed to create a WGL context.\n");
2074 context_release(context);
2075 return FALSE;
2078 if (share_ctx && !wglShareLists(share_ctx, ctx))
2080 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
2081 context_release(context);
2082 if (!wglDeleteContext(ctx))
2083 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2084 return FALSE;
2088 context_gl->dc_has_format = TRUE;
2089 context_gl->needs_set = 1;
2090 context_gl->valid = 1;
2091 context_gl->gl_ctx = ctx;
2093 return TRUE;
2096 HRESULT wined3d_context_gl_init(struct wined3d_context_gl *context_gl, struct wined3d_swapchain_gl *swapchain_gl)
2098 struct wined3d_context *context = &context_gl->c;
2099 const struct wined3d_d3d_info *d3d_info;
2100 const struct wined3d_gl_info *gl_info;
2101 struct wined3d_device *device;
2102 unsigned int i;
2104 TRACE("context_gl %p, swapchain %p.\n", context_gl, swapchain_gl);
2106 wined3d_context_init(&context_gl->c, &swapchain_gl->s);
2108 device = context->device;
2109 gl_info = &device->adapter->gl_info;
2110 context_gl->gl_info = gl_info;
2111 d3d_info = context->d3d_info;
2113 context_gl->tid = GetCurrentThreadId();
2114 context_gl->window = context->swapchain->win_handle;
2115 if (context_gl->window == GetDesktopWindow())
2117 TRACE("Swapchain is created on the desktop window, trying backup device context.\n");
2118 context_gl->dc = NULL;
2120 else if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
2121 WARN("Failed to retrieve device context, trying swapchain backup.\n");
2123 if (!context_gl->dc)
2125 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2127 ERR("Failed to retrieve a device context.\n");
2128 return E_FAIL;
2130 context_gl->dc_is_private = TRUE;
2133 list_init(&context_gl->fbo_list);
2134 list_init(&context_gl->fbo_destroy_list);
2136 list_init(&context_gl->occlusion_queries);
2137 list_init(&context_gl->fences);
2138 list_init(&context_gl->timestamp_queries);
2139 list_init(&context_gl->so_statistics_queries);
2140 list_init(&context_gl->pipeline_statistics_queries);
2142 for (i = 0; i < ARRAY_SIZE(context_gl->tex_unit_map); ++i)
2143 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2144 for (i = 0; i < ARRAY_SIZE(context_gl->rev_tex_unit_map); ++i)
2145 context_gl->rev_tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2146 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
2148 /* Initialize the texture unit mapping to a 1:1 mapping. */
2149 unsigned int base, count;
2151 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_PIXEL, &base, &count);
2152 if (base + WINED3D_MAX_FRAGMENT_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2154 ERR("Unexpected texture unit base index %u.\n", base);
2155 goto fail;
2157 for (i = 0; i < min(count, WINED3D_MAX_FRAGMENT_SAMPLERS); ++i)
2159 context_gl->tex_unit_map[i] = base + i;
2160 context_gl->rev_tex_unit_map[base + i] = i;
2163 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_VERTEX, &base, &count);
2164 if (base + WINED3D_MAX_VERTEX_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2166 ERR("Unexpected texture unit base index %u.\n", base);
2167 goto fail;
2169 for (i = 0; i < min(count, WINED3D_MAX_VERTEX_SAMPLERS); ++i)
2171 context_gl->tex_unit_map[WINED3D_MAX_FRAGMENT_SAMPLERS + i] = base + i;
2172 context_gl->rev_tex_unit_map[base + i] = WINED3D_MAX_FRAGMENT_SAMPLERS + i;
2176 if (!(context_gl->texture_type = heap_calloc(gl_info->limits.combined_samplers,
2177 sizeof(*context_gl->texture_type))))
2178 goto fail;
2180 if (!wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl))
2181 goto fail;
2183 /* Set up the context defaults. */
2185 context->render_offscreen = wined3d_resource_is_offscreen(&context->current_rt.texture->resource);
2186 context_gl->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
2188 if (!wined3d_context_gl_set_current(context_gl))
2190 ERR("Cannot activate context to set up defaults.\n");
2191 context_release(context);
2192 if (!wglDeleteContext(context_gl->gl_ctx))
2193 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context_gl->gl_ctx, GetLastError());
2194 goto fail;
2197 if (context_debug_output_enabled(gl_info))
2199 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback, context));
2200 if (TRACE_ON(d3d_synchronous))
2201 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2202 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
2203 if (ERR_ON(d3d))
2205 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR,
2206 GL_DONT_CARE, 0, NULL, GL_TRUE));
2208 if (FIXME_ON(d3d))
2210 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
2211 GL_DONT_CARE, 0, NULL, GL_TRUE));
2212 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
2213 GL_DONT_CARE, 0, NULL, GL_TRUE));
2214 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY,
2215 GL_DONT_CARE, 0, NULL, GL_TRUE));
2217 if (WARN_ON(d3d_perf))
2219 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE,
2220 GL_DONT_CARE, 0, NULL, GL_TRUE));
2224 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2225 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &context_gl->aux_buffers);
2227 TRACE("Setting up the screen\n");
2229 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2231 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
2232 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2234 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
2235 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2237 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2238 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2240 else
2242 GLuint vao;
2244 GL_EXTCALL(glGenVertexArrays(1, &vao));
2245 GL_EXTCALL(glBindVertexArray(vao));
2246 checkGLcall("creating VAO");
2249 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
2250 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2251 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2252 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2254 if (gl_info->supported[NV_TEXTURE_SHADER2])
2256 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2257 * the previous texture where to source the offset from is always unit - 1.
2259 for (i = 1; i < gl_info->limits.textures; ++i)
2261 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2262 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
2263 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + i - 1);
2264 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2267 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2269 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2270 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2271 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2272 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2273 * is ever assigned.
2275 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2276 * program and the dummy program is destroyed when the context is destroyed.
2278 static const char dummy_program[] =
2279 "!!ARBfp1.0\n"
2280 "MOV result.color, fragment.color.primary;\n"
2281 "END\n";
2282 GL_EXTCALL(glGenProgramsARB(1, &context_gl->dummy_arbfp_prog));
2283 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, context_gl->dummy_arbfp_prog));
2284 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
2285 GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2288 if (gl_info->supported[ARB_POINT_SPRITE])
2290 for (i = 0; i < gl_info->limits.textures; ++i)
2292 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2293 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2294 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2298 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2300 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2302 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2304 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2306 if (!(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART))
2308 if (gl_info->supported[ARB_ES3_COMPATIBILITY])
2310 gl_info->gl_ops.gl.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
2311 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2313 else
2315 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2318 if (!(d3d_info->wined3d_creation_flags & WINED3D_LEGACY_CUBEMAP_FILTERING)
2319 && gl_info->supported[ARB_SEAMLESS_CUBE_MAP])
2321 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
2322 checkGLcall("enable seamless cube map filtering");
2324 if (gl_info->supported[ARB_CLIP_CONTROL])
2325 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT));
2327 /* If this happens to be the first context for the device, dummy textures
2328 * are not created yet. In that case, they will be created (and bound) by
2329 * create_dummy_textures right after this context is initialized. */
2330 if (wined3d_device_gl(device)->dummy_textures.tex_2d)
2331 wined3d_context_gl_bind_dummy_textures(context_gl);
2333 /* Initialise all rectangles to avoid resetting unused ones later. */
2334 gl_info->gl_ops.gl.p_glScissor(0, 0, 0, 0);
2335 checkGLcall("glScissor");
2337 return WINED3D_OK;
2339 fail:
2340 heap_free(context_gl->texture_type);
2341 wined3d_release_dc(context_gl->window, context_gl->dc);
2342 return E_FAIL;
2345 HRESULT wined3d_context_vk_init(struct wined3d_context_vk *context_vk, struct wined3d_swapchain *swapchain)
2347 TRACE("context_vk %p, swapchain %p.\n", context_vk, swapchain);
2349 wined3d_context_init(&context_vk->c, swapchain);
2351 return WINED3D_OK;
2354 void wined3d_context_gl_destroy(struct wined3d_context_gl *context_gl)
2356 struct wined3d_device *device = context_gl->c.device;
2358 TRACE("Destroying context %p.\n", context_gl);
2360 wined3d_from_cs(device->cs);
2362 /* We delay destroying a context when it is active. The context_release()
2363 * function invokes wined3d_context_gl_destroy() again while leaving the
2364 * last level. */
2365 if (context_gl->level)
2367 TRACE("Delaying destruction of context %p.\n", context_gl);
2368 context_gl->c.destroy_delayed = 1;
2369 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2370 context_gl->c.swapchain = NULL;
2371 return;
2374 device_context_remove(device, &context_gl->c);
2376 if (context_gl->c.current && context_gl->tid != GetCurrentThreadId())
2378 struct wined3d_gl_info *gl_info;
2380 /* Make a copy of gl_info for wined3d_context_gl_cleanup() use, the
2381 * one in wined3d_adapter may go away in the meantime. */
2382 gl_info = heap_alloc(sizeof(*gl_info));
2383 *gl_info = *context_gl->gl_info;
2384 context_gl->gl_info = gl_info;
2385 context_gl->c.destroyed = 1;
2387 return;
2390 wined3d_context_gl_cleanup(context_gl);
2391 TlsSetValue(context_get_tls_idx(), NULL);
2392 heap_free(context_gl);
2395 const unsigned int *wined3d_context_gl_get_tex_unit_mapping(const struct wined3d_context_gl *context_gl,
2396 const struct wined3d_shader_version *shader_version, unsigned int *base, unsigned int *count)
2398 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2400 if (!shader_version)
2402 *base = 0;
2403 *count = WINED3D_MAX_TEXTURES;
2404 return context_gl->tex_unit_map;
2407 if (shader_version->major >= 4)
2409 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, shader_version->type, base, count);
2410 return NULL;
2413 switch (shader_version->type)
2415 case WINED3D_SHADER_TYPE_PIXEL:
2416 *base = 0;
2417 *count = WINED3D_MAX_FRAGMENT_SAMPLERS;
2418 break;
2419 case WINED3D_SHADER_TYPE_VERTEX:
2420 *base = WINED3D_MAX_FRAGMENT_SAMPLERS;
2421 *count = WINED3D_MAX_VERTEX_SAMPLERS;
2422 break;
2423 default:
2424 ERR("Unhandled shader type %#x.\n", shader_version->type);
2425 *base = 0;
2426 *count = 0;
2429 return context_gl->tex_unit_map;
2432 static void wined3d_context_gl_get_rt_size(const struct wined3d_context_gl *context_gl, SIZE *size)
2434 const struct wined3d_texture *rt = context_gl->c.current_rt.texture;
2435 unsigned int level;
2437 if (rt->swapchain)
2439 RECT window_size;
2441 GetClientRect(context_gl->window, &window_size);
2442 size->cx = window_size.right - window_size.left;
2443 size->cy = window_size.bottom - window_size.top;
2445 return;
2448 level = context_gl->c.current_rt.sub_resource_idx % rt->level_count;
2449 size->cx = wined3d_texture_get_level_width(rt, level);
2450 size->cy = wined3d_texture_get_level_height(rt, level);
2453 void wined3d_context_gl_enable_clip_distances(struct wined3d_context_gl *context_gl, uint32_t enable_mask)
2455 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2456 unsigned int clip_distance_count, i;
2457 uint32_t disable_mask, current_mask;
2459 clip_distance_count = gl_info->limits.user_clip_distances;
2460 disable_mask = ~enable_mask;
2461 enable_mask &= (1u << clip_distance_count) - 1;
2462 disable_mask &= (1u << clip_distance_count) - 1;
2463 current_mask = context_gl->c.clip_distance_mask;
2464 context_gl->c.clip_distance_mask = enable_mask;
2466 enable_mask &= ~current_mask;
2467 while (enable_mask)
2469 i = wined3d_bit_scan(&enable_mask);
2470 gl_info->gl_ops.gl.p_glEnable(GL_CLIP_DISTANCE0 + i);
2472 disable_mask &= current_mask;
2473 while (disable_mask)
2475 i = wined3d_bit_scan(&disable_mask);
2476 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_DISTANCE0 + i);
2478 checkGLcall("toggle clip distances");
2481 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2483 return rt_mask & (1u << 31);
2486 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2488 return rt_mask & ~(1u << 31);
2491 /* Context activation is done by the caller. */
2492 static void wined3d_context_gl_apply_draw_buffers(struct wined3d_context_gl *context_gl, uint32_t rt_mask)
2494 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2495 GLenum draw_buffers[MAX_RENDER_TARGET_VIEWS];
2497 if (!rt_mask)
2499 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2501 else if (is_rt_mask_onscreen(rt_mask))
2503 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2505 else
2507 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2509 unsigned int i = 0;
2511 while (rt_mask)
2513 if (rt_mask & 1)
2514 draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2515 else
2516 draw_buffers[i] = GL_NONE;
2518 rt_mask >>= 1;
2519 ++i;
2522 if (gl_info->supported[ARB_DRAW_BUFFERS])
2524 GL_EXTCALL(glDrawBuffers(i, draw_buffers));
2526 else
2528 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffers[0]);
2531 else
2533 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2537 checkGLcall("apply draw buffers");
2540 /* Context activation is done by the caller. */
2541 void wined3d_context_gl_set_draw_buffer(struct wined3d_context_gl *context_gl, GLenum buffer)
2543 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2544 struct fbo_entry *current_fbo = context_gl->current_fbo;
2545 uint32_t new_mask = context_generate_rt_mask(buffer);
2546 uint32_t *current_mask;
2548 current_mask = current_fbo ? &current_fbo->rt_mask : &context_gl->draw_buffers_mask;
2549 if (new_mask == *current_mask)
2550 return;
2552 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2553 checkGLcall("glDrawBuffer()");
2555 *current_mask = new_mask;
2558 /* Context activation is done by the caller. */
2559 void wined3d_context_gl_active_texture(struct wined3d_context_gl *context_gl,
2560 const struct wined3d_gl_info *gl_info, unsigned int unit)
2562 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + unit));
2563 checkGLcall("glActiveTexture");
2564 context_gl->active_texture = unit;
2567 void wined3d_context_gl_bind_bo(struct wined3d_context_gl *context_gl, GLenum binding, GLuint name)
2569 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2571 if (binding == GL_ELEMENT_ARRAY_BUFFER)
2572 context_invalidate_state(&context_gl->c, STATE_INDEXBUFFER);
2574 GL_EXTCALL(glBindBuffer(binding, name));
2577 void wined3d_context_gl_bind_texture(struct wined3d_context_gl *context_gl, GLenum target, GLuint name)
2579 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
2580 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2581 GLenum old_texture_type;
2582 unsigned int unit;
2584 if (name)
2585 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2586 else
2587 target = GL_NONE;
2589 unit = context_gl->active_texture;
2590 old_texture_type = context_gl->texture_type[unit];
2591 if (old_texture_type != target)
2593 switch (old_texture_type)
2595 case GL_NONE:
2596 /* nothing to do */
2597 break;
2598 case GL_TEXTURE_1D:
2599 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
2600 break;
2601 case GL_TEXTURE_1D_ARRAY:
2602 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
2603 break;
2604 case GL_TEXTURE_2D:
2605 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
2606 break;
2607 case GL_TEXTURE_2D_ARRAY:
2608 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
2609 break;
2610 case GL_TEXTURE_RECTANGLE_ARB:
2611 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
2612 break;
2613 case GL_TEXTURE_CUBE_MAP:
2614 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
2615 break;
2616 case GL_TEXTURE_CUBE_MAP_ARRAY:
2617 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
2618 break;
2619 case GL_TEXTURE_3D:
2620 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
2621 break;
2622 case GL_TEXTURE_BUFFER:
2623 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
2624 break;
2625 case GL_TEXTURE_2D_MULTISAMPLE:
2626 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
2627 break;
2628 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2629 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
2630 break;
2631 default:
2632 ERR("Unexpected texture target %#x.\n", old_texture_type);
2635 context_gl->texture_type[unit] = target;
2638 checkGLcall("bind texture");
2641 void *wined3d_context_gl_map_bo_address(struct wined3d_context_gl *context_gl,
2642 const struct wined3d_bo_address *data, size_t size, GLenum binding, DWORD flags)
2644 const struct wined3d_gl_info *gl_info;
2645 BYTE *memory;
2647 if (!data->buffer_object)
2648 return data->addr;
2650 gl_info = context_gl->gl_info;
2651 wined3d_context_gl_bind_bo(context_gl, binding, data->buffer_object);
2653 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2655 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr,
2656 size, wined3d_resource_gl_map_flags(flags)));
2658 else
2660 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
2661 memory += (INT_PTR)data->addr;
2664 wined3d_context_gl_bind_bo(context_gl, binding, 0);
2665 checkGLcall("Map buffer object");
2667 return memory;
2670 void wined3d_context_gl_unmap_bo_address(struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data,
2671 GLenum binding, unsigned int range_count, const struct wined3d_map_range *ranges)
2673 const struct wined3d_gl_info *gl_info;
2674 unsigned int i;
2676 if (!data->buffer_object)
2677 return;
2679 gl_info = context_gl->gl_info;
2680 wined3d_context_gl_bind_bo(context_gl, binding, data->buffer_object);
2682 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2684 for (i = 0; i < range_count; ++i)
2686 GL_EXTCALL(glFlushMappedBufferRange(binding, (UINT_PTR)data->addr + ranges[i].offset, ranges[i].size));
2690 GL_EXTCALL(glUnmapBuffer(binding));
2691 wined3d_context_gl_bind_bo(context_gl, binding, 0);
2692 checkGLcall("Unmap buffer object");
2695 void wined3d_context_gl_copy_bo_address(struct wined3d_context_gl *context_gl,
2696 const struct wined3d_bo_address *dst, GLenum dst_binding,
2697 const struct wined3d_bo_address *src, GLenum src_binding, size_t size)
2699 const struct wined3d_gl_info *gl_info;
2700 struct wined3d_map_range range;
2701 BYTE *dst_ptr, *src_ptr;
2703 gl_info = context_gl->gl_info;
2705 if (dst->buffer_object && src->buffer_object)
2707 if (gl_info->supported[ARB_COPY_BUFFER])
2709 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER, src->buffer_object));
2710 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER, dst->buffer_object));
2711 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
2712 (GLintptr)src->addr, (GLintptr)dst->addr, size));
2713 checkGLcall("direct buffer copy");
2715 else
2717 src_ptr = wined3d_context_gl_map_bo_address(context_gl, src, size, src_binding, WINED3D_MAP_READ);
2718 dst_ptr = wined3d_context_gl_map_bo_address(context_gl, dst, size, dst_binding, WINED3D_MAP_WRITE);
2720 memcpy(dst_ptr, src_ptr, size);
2722 range.offset = 0;
2723 range.size = size;
2724 wined3d_context_gl_unmap_bo_address(context_gl, dst, dst_binding, 1, &range);
2725 wined3d_context_gl_unmap_bo_address(context_gl, src, src_binding, 0, NULL);
2728 else if (!dst->buffer_object && src->buffer_object)
2730 wined3d_context_gl_bind_bo(context_gl, src_binding, src->buffer_object);
2731 GL_EXTCALL(glGetBufferSubData(src_binding, (GLintptr)src->addr, size, dst->addr));
2732 checkGLcall("buffer download");
2734 else if (dst->buffer_object && !src->buffer_object)
2736 wined3d_context_gl_bind_bo(context_gl, dst_binding, dst->buffer_object);
2737 GL_EXTCALL(glBufferSubData(dst_binding, (GLintptr)dst->addr, size, src->addr));
2738 checkGLcall("buffer upload");
2740 else
2742 memcpy(dst->addr, src->addr, size);
2746 static void wined3d_context_gl_set_render_offscreen(struct wined3d_context_gl *context_gl, BOOL offscreen)
2748 if (context_gl->c.render_offscreen == offscreen)
2749 return;
2751 context_invalidate_state(&context_gl->c, STATE_VIEWPORT);
2752 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
2753 if (!context_gl->gl_info->supported[ARB_CLIP_CONTROL])
2755 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
2756 context_invalidate_state(&context_gl->c, STATE_POINTSPRITECOORDORIGIN);
2757 context_invalidate_state(&context_gl->c, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2759 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN));
2760 if (context_gl->gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2761 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
2762 context_gl->c.render_offscreen = offscreen;
2765 GLenum wined3d_context_gl_get_offscreen_gl_buffer(const struct wined3d_context_gl *context_gl)
2767 switch (wined3d_settings.offscreen_rendering_mode)
2769 case ORM_FBO:
2770 return GL_COLOR_ATTACHMENT0;
2772 case ORM_BACKBUFFER:
2773 return context_gl->aux_buffers > 0 ? GL_AUX0 : GL_BACK;
2775 default:
2776 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
2777 return GL_BACK;
2781 static uint32_t wined3d_context_gl_generate_rt_mask_no_fbo(const struct wined3d_context_gl *context_gl,
2782 struct wined3d_resource *rt)
2784 if (!rt || rt->format->id == WINED3DFMT_NULL)
2785 return 0;
2786 else if (rt->type != WINED3D_RTYPE_BUFFER && texture_from_resource(rt)->swapchain)
2787 return context_generate_rt_mask_from_resource(rt);
2788 else
2789 return context_generate_rt_mask(wined3d_context_gl_get_offscreen_gl_buffer(context_gl));
2792 /* Context activation is done by the caller. */
2793 void wined3d_context_gl_apply_blit_state(struct wined3d_context_gl *context_gl, const struct wined3d_device *device)
2795 struct wined3d_context *context = &context_gl->c;
2796 const struct wined3d_gl_info *gl_info;
2797 uint32_t rt_mask, *cur_mask;
2798 struct wined3d_texture *rt;
2799 unsigned int sampler;
2800 SIZE rt_size;
2802 TRACE("Setting up context %p for blitting.\n", context);
2804 gl_info = context_gl->gl_info;
2805 rt = context->current_rt.texture;
2807 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2809 if (context->render_offscreen)
2811 wined3d_texture_load(rt, context, FALSE);
2813 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_FRAMEBUFFER, &rt->resource,
2814 context->current_rt.sub_resource_idx, NULL, 0, rt->resource.draw_binding);
2815 if (rt->resource.format->id != WINED3DFMT_NULL)
2816 rt_mask = 1;
2817 else
2818 rt_mask = 0;
2820 else
2822 context_gl->current_fbo = NULL;
2823 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
2824 rt_mask = context_generate_rt_mask_from_resource(&rt->resource);
2827 else
2829 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, &rt->resource);
2832 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
2834 if (rt_mask != *cur_mask)
2836 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
2837 *cur_mask = rt_mask;
2840 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2841 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
2842 context_invalidate_state(context, STATE_FRAMEBUFFER);
2844 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
2846 if (context->last_was_blit)
2848 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
2850 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
2851 context->viewport_count = WINED3D_MAX_VIEWPORTS;
2852 context_gl->blit_size = rt_size;
2853 /* No need to dirtify here, the states are still dirtified because
2854 * they weren't applied since the last context_apply_blit_state()
2855 * call. */
2857 checkGLcall("blit state application");
2858 TRACE("Context is already set up for blitting, nothing to do.\n");
2859 return;
2861 context->last_was_blit = TRUE;
2863 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
2864 GL_EXTCALL(glBindSampler(0, 0));
2865 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
2867 sampler = context_gl->rev_tex_unit_map[0];
2868 if (sampler != WINED3D_UNMAPPED_STAGE)
2870 if (sampler < WINED3D_MAX_TEXTURES)
2872 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
2873 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2875 context_invalidate_state(context, STATE_SAMPLER(sampler));
2877 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
2878 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
2880 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2882 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
2883 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
2885 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
2886 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZENABLE));
2887 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2888 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2889 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
2890 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CULLMODE));
2891 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
2892 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILENABLE));
2893 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
2894 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2895 if (gl_info->supported[ARB_POINT_SPRITE])
2897 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
2898 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
2900 if (gl_info->supported[ARB_FRAMEBUFFER_SRGB])
2902 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
2903 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
2905 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2906 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
2907 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
2908 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
2909 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
2911 context->last_was_rhw = TRUE;
2912 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
2914 wined3d_context_gl_enable_clip_distances(context_gl, 0);
2915 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
2917 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
2918 if (gl_info->supported[ARB_CLIP_CONTROL])
2919 GL_EXTCALL(glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE));
2920 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
2921 context->viewport_count = WINED3D_MAX_VIEWPORTS;
2922 context_invalidate_state(context, STATE_VIEWPORT);
2924 device->shader_backend->shader_disable(device->shader_priv, context);
2926 context_gl->blit_size = rt_size;
2928 checkGLcall("blit state application");
2931 static void wined3d_context_gl_apply_blit_projection(const struct wined3d_context_gl *context_gl,
2932 unsigned int w, unsigned int h)
2934 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2935 const GLdouble projection[] =
2937 2.0 / w, 0.0, 0.0, 0.0,
2938 0.0, 2.0 / h, 0.0, 0.0,
2939 0.0, 0.0, 2.0, 0.0,
2940 -1.0, -1.0, -1.0, 1.0,
2943 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
2944 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
2947 /* Setup OpenGL states for fixed-function blitting. */
2948 /* Context activation is done by the caller. */
2949 void wined3d_context_gl_apply_ffp_blit_state(struct wined3d_context_gl *context_gl,
2950 const struct wined3d_device *device)
2952 struct wined3d_context *context = &context_gl->c;
2953 const struct wined3d_gl_info *gl_info;
2954 unsigned int i, sampler;
2956 gl_info = context_gl->gl_info;
2957 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2958 ERR("Applying fixed-function state without legacy context support.\n");
2960 if (context->last_was_ffp_blit)
2962 SIZE rt_size;
2964 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
2965 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
2966 wined3d_context_gl_apply_blit_projection(context_gl, rt_size.cx, rt_size.cy);
2967 wined3d_context_gl_apply_blit_state(context_gl, device);
2969 checkGLcall("ffp blit state application");
2970 return;
2972 context->last_was_ffp_blit = TRUE;
2974 wined3d_context_gl_apply_blit_state(context_gl, device);
2976 /* Disable all textures. The caller can then bind a texture it wants to blit
2977 * from. */
2978 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
2980 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2982 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2983 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2984 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2985 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2986 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2987 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2989 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2991 sampler = context_gl->rev_tex_unit_map[i];
2992 if (sampler != WINED3D_UNMAPPED_STAGE)
2994 if (sampler < WINED3D_MAX_TEXTURES)
2995 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2996 context_invalidate_state(context, STATE_SAMPLER(sampler));
3000 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
3002 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3003 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3004 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3005 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3006 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3007 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3009 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3010 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
3011 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
3013 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
3014 gl_info->gl_ops.gl.p_glLoadIdentity();
3016 /* Setup transforms. */
3017 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3018 gl_info->gl_ops.gl.p_glLoadIdentity();
3019 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3020 wined3d_context_gl_apply_blit_projection(context_gl, context_gl->blit_size.cx, context_gl->blit_size.cy);
3021 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
3023 /* Other misc states. */
3024 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
3025 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
3026 gl_info->p_glDisableWINE(GL_FOG);
3027 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
3029 if (gl_info->supported[EXT_SECONDARY_COLOR])
3031 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
3032 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
3034 checkGLcall("ffp blit state application");
3037 static BOOL have_framebuffer_attachment(unsigned int rt_count, struct wined3d_rendertarget_view * const *rts,
3038 const struct wined3d_rendertarget_view *ds)
3040 unsigned int i;
3042 if (ds)
3043 return TRUE;
3045 for (i = 0; i < rt_count; ++i)
3047 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3048 return TRUE;
3051 return FALSE;
3054 /* Context activation is done by the caller. */
3055 BOOL wined3d_context_gl_apply_clear_state(struct wined3d_context_gl *context_gl,
3056 const struct wined3d_state *state, unsigned int rt_count, const struct wined3d_fb_state *fb)
3058 struct wined3d_rendertarget_view * const *rts = fb->render_targets;
3059 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3060 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
3061 uint32_t rt_mask = 0, *cur_mask;
3062 unsigned int i;
3064 if (isStateDirty(&context_gl->c, STATE_FRAMEBUFFER) || fb != state->fb
3065 || rt_count != gl_info->limits.buffers)
3067 if (!have_framebuffer_attachment(rt_count, rts, dsv))
3069 WARN("Invalid render target config, need at least one attachment.\n");
3070 return FALSE;
3073 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3075 struct wined3d_rendertarget_info ds_info = {{0}};
3077 if (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource))
3079 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3080 for (i = 0; i < rt_count; ++i)
3082 if (rts[i])
3084 struct wined3d_rendertarget_view_gl *rtv_gl = wined3d_rendertarget_view_gl(rts[i]);
3085 context_gl->blit_targets[i].gl_view = rtv_gl->gl_view;
3086 context_gl->blit_targets[i].resource = rtv_gl->v.resource;
3087 context_gl->blit_targets[i].sub_resource_idx = rtv_gl->v.sub_resource_idx;
3088 context_gl->blit_targets[i].layer_count = rtv_gl->v.layer_count;
3090 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3091 rt_mask |= (1u << i);
3094 if (dsv)
3096 struct wined3d_rendertarget_view_gl *dsv_gl = wined3d_rendertarget_view_gl(dsv);
3097 ds_info.gl_view = dsv_gl->gl_view;
3098 ds_info.resource = dsv_gl->v.resource;
3099 ds_info.sub_resource_idx = dsv_gl->v.sub_resource_idx;
3100 ds_info.layer_count = dsv_gl->v.layer_count;
3103 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3104 rt_count ? rts[0]->resource->draw_binding : 0, dsv ? dsv->resource->draw_binding : 0);
3106 else
3108 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3109 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3110 rt_mask = context_generate_rt_mask_from_resource(rts[0]->resource);
3113 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3114 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3115 * state management allows this */
3116 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3118 else
3120 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3123 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3124 && (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource)))
3126 for (i = 0; i < rt_count; ++i)
3128 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3129 rt_mask |= (1u << i);
3132 else
3134 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3137 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3139 if (rt_mask != *cur_mask)
3141 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3142 *cur_mask = rt_mask;
3143 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3146 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3147 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3149 context_gl->c.last_was_blit = FALSE;
3150 context_gl->c.last_was_ffp_blit = FALSE;
3152 /* Blending and clearing should be orthogonal, but tests on the nvidia
3153 * driver show that disabling blending when clearing improves the clearing
3154 * performance incredibly. */
3155 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3156 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
3157 if (rt_count && gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3159 if (needs_srgb_write(&context_gl->c, state, fb))
3160 gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
3161 else
3162 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3163 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3165 checkGLcall("setting up state for clear");
3167 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
3168 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
3169 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
3171 return TRUE;
3174 static uint32_t find_draw_buffers_mask(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3176 struct wined3d_rendertarget_view * const *rts = state->fb->render_targets;
3177 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3178 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3179 unsigned int rt_mask, mask;
3180 unsigned int i;
3182 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3183 return wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rts[0]->resource);
3184 else if (!context_gl->c.render_offscreen)
3185 return context_generate_rt_mask_from_resource(rts[0]->resource);
3187 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
3188 rt_mask &= (1u << gl_info->limits.buffers) - 1;
3190 mask = rt_mask;
3191 while (mask)
3193 i = wined3d_bit_scan(&mask);
3194 if (!rts[i] || rts[i]->format->id == WINED3DFMT_NULL)
3195 rt_mask &= ~(1u << i);
3198 return rt_mask;
3201 /* Context activation is done by the caller. */
3202 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3204 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3205 uint32_t rt_mask = find_draw_buffers_mask(context_gl, state);
3206 const struct wined3d_fb_state *fb = state->fb;
3207 DWORD color_location = 0;
3208 DWORD *cur_mask;
3210 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3212 struct wined3d_rendertarget_info ds_info = {{0}};
3214 if (!context->render_offscreen)
3216 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3217 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3219 else
3221 const struct wined3d_rendertarget_view_gl *view_gl;
3222 unsigned int i;
3224 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3225 for (i = 0; i < context_gl->gl_info->limits.buffers; ++i)
3227 if (!fb->render_targets[i])
3228 continue;
3230 view_gl = wined3d_rendertarget_view_gl(fb->render_targets[i]);
3231 context_gl->blit_targets[i].gl_view = view_gl->gl_view;
3232 context_gl->blit_targets[i].resource = view_gl->v.resource;
3233 context_gl->blit_targets[i].sub_resource_idx = view_gl->v.sub_resource_idx;
3234 context_gl->blit_targets[i].layer_count = view_gl->v.layer_count;
3236 if (!color_location)
3237 color_location = view_gl->v.resource->draw_binding;
3240 if (fb->depth_stencil)
3242 view_gl = wined3d_rendertarget_view_gl(fb->depth_stencil);
3243 ds_info.gl_view = view_gl->gl_view;
3244 ds_info.resource = view_gl->v.resource;
3245 ds_info.sub_resource_idx = view_gl->v.sub_resource_idx;
3246 ds_info.layer_count = view_gl->v.layer_count;
3249 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3250 color_location, fb->depth_stencil ? fb->depth_stencil->resource->draw_binding : 0);
3254 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3255 if (rt_mask != *cur_mask)
3257 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3258 *cur_mask = rt_mask;
3260 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
3263 static void wined3d_context_gl_map_stage(struct wined3d_context_gl *context_gl, unsigned int stage, unsigned int unit)
3265 unsigned int i = context_gl->rev_tex_unit_map[unit];
3266 unsigned int j = context_gl->tex_unit_map[stage];
3268 TRACE("Mapping stage %u to unit %u.\n", stage, unit);
3269 context_gl->tex_unit_map[stage] = unit;
3270 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
3271 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
3273 context_gl->rev_tex_unit_map[unit] = stage;
3274 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
3275 context_gl->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
3278 static void context_invalidate_texture_stage(struct wined3d_context *context, DWORD stage)
3280 DWORD i;
3282 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
3283 context_invalidate_state(context, STATE_TEXTURESTAGE(stage, i));
3286 static void context_update_fixed_function_usage_map(struct wined3d_context *context,
3287 const struct wined3d_state *state)
3289 UINT i, start, end;
3291 context->fixed_function_usage_map = 0;
3292 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
3294 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
3295 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
3296 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
3297 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
3298 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
3299 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
3300 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
3301 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
3303 /* Not used, and disable higher stages. */
3304 if (color_op == WINED3D_TOP_DISABLE)
3305 break;
3307 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
3308 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
3309 || ((color_arg3 == WINED3DTA_TEXTURE)
3310 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
3311 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
3312 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
3313 || ((alpha_arg3 == WINED3DTA_TEXTURE)
3314 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
3315 context->fixed_function_usage_map |= (1u << i);
3317 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
3318 && i < WINED3D_MAX_TEXTURES - 1)
3319 context->fixed_function_usage_map |= (1u << (i + 1));
3322 if (i < context->lowest_disabled_stage)
3324 start = i;
3325 end = context->lowest_disabled_stage;
3327 else
3329 start = context->lowest_disabled_stage;
3330 end = i;
3333 context->lowest_disabled_stage = i;
3334 for (i = start + 1; i < end; ++i)
3336 context_invalidate_state(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3340 static void wined3d_context_gl_map_fixed_function_samplers(struct wined3d_context_gl *context_gl,
3341 const struct wined3d_state *state)
3343 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3344 unsigned int i, tex;
3345 WORD ffu_map;
3347 ffu_map = context_gl->c.fixed_function_usage_map;
3349 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
3350 || context_gl->c.lowest_disabled_stage <= d3d_info->limits.ffp_textures)
3352 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3354 if (!(ffu_map & 1))
3355 continue;
3357 if (context_gl->tex_unit_map[i] != i)
3359 wined3d_context_gl_map_stage(context_gl, i, i);
3360 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3361 context_invalidate_texture_stage(&context_gl->c, i);
3364 return;
3367 /* Now work out the mapping */
3368 tex = 0;
3369 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3371 if (!(ffu_map & 1))
3372 continue;
3374 if (context_gl->tex_unit_map[i] != tex)
3376 wined3d_context_gl_map_stage(context_gl, i, tex);
3377 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3378 context_invalidate_texture_stage(&context_gl->c, i);
3381 ++tex;
3385 static void wined3d_context_gl_map_psamplers(struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3387 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3388 const struct wined3d_shader_resource_info *resource_info =
3389 state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3390 unsigned int i;
3392 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
3394 if (resource_info[i].type && context_gl->tex_unit_map[i] != i)
3396 wined3d_context_gl_map_stage(context_gl, i, i);
3397 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3398 if (i < d3d_info->limits.ffp_blend_stages)
3399 context_invalidate_texture_stage(&context_gl->c, i);
3404 static BOOL wined3d_context_gl_unit_free_for_vs(const struct wined3d_context_gl *context_gl,
3405 const struct wined3d_shader_resource_info *ps_resource_info, unsigned int unit)
3407 unsigned int current_mapping = context_gl->rev_tex_unit_map[unit];
3409 /* Not currently used */
3410 if (current_mapping == WINED3D_UNMAPPED_STAGE)
3411 return TRUE;
3413 if (current_mapping < WINED3D_MAX_FRAGMENT_SAMPLERS)
3415 /* Used by a fragment sampler */
3417 if (!ps_resource_info)
3419 /* No pixel shader, check fixed function */
3420 return current_mapping >= WINED3D_MAX_TEXTURES
3421 || !(context_gl->c.fixed_function_usage_map & (1u << current_mapping));
3424 /* Pixel shader, check the shader's sampler map */
3425 return !ps_resource_info[current_mapping].type;
3428 return TRUE;
3431 static void wined3d_context_gl_map_vsamplers(struct wined3d_context_gl *context_gl,
3432 BOOL ps, const struct wined3d_state *state)
3434 const struct wined3d_shader_resource_info *vs_resource_info =
3435 state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info;
3436 const struct wined3d_shader_resource_info *ps_resource_info = NULL;
3437 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3438 int start = min(WINED3D_MAX_COMBINED_SAMPLERS, gl_info->limits.graphics_samplers) - 1;
3439 int i;
3441 /* Note that we only care if a resource is used or not, not the
3442 * resource's specific type. Otherwise we'd need to call
3443 * shader_update_samplers() here for 1.x pixelshaders. */
3444 if (ps)
3445 ps_resource_info = state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3447 for (i = 0; i < WINED3D_MAX_VERTEX_SAMPLERS; ++i)
3449 DWORD vsampler_idx = i + WINED3D_MAX_FRAGMENT_SAMPLERS;
3450 if (vs_resource_info[i].type)
3452 while (start >= 0)
3454 if (wined3d_context_gl_unit_free_for_vs(context_gl, ps_resource_info, start))
3456 if (context_gl->tex_unit_map[vsampler_idx] != start)
3458 wined3d_context_gl_map_stage(context_gl, vsampler_idx, start);
3459 context_invalidate_state(&context_gl->c, STATE_SAMPLER(vsampler_idx));
3462 --start;
3463 break;
3466 --start;
3468 if (context_gl->tex_unit_map[vsampler_idx] == WINED3D_UNMAPPED_STAGE)
3469 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i);
3474 static void wined3d_context_gl_update_tex_unit_map(struct wined3d_context_gl *context_gl,
3475 const struct wined3d_state *state)
3477 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3478 BOOL vs = use_vs(state);
3479 BOOL ps = use_ps(state);
3481 if (!ps)
3482 context_update_fixed_function_usage_map(&context_gl->c, state);
3484 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3485 * need a 1:1 map at the moment.
3486 * When the mapping of a stage is changed, sampler and ALL texture stage
3487 * states have to be reset. */
3489 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
3490 return;
3492 if (ps)
3493 wined3d_context_gl_map_psamplers(context_gl, state);
3494 else
3495 wined3d_context_gl_map_fixed_function_samplers(context_gl, state);
3497 if (vs)
3498 wined3d_context_gl_map_vsamplers(context_gl, ps, state);
3501 /* Context activation is done by the caller. */
3502 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3504 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3505 uint32_t rt_mask, *cur_mask;
3507 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
3509 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3510 rt_mask = find_draw_buffers_mask(context_gl, state);
3511 if (rt_mask != *cur_mask)
3513 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3514 *cur_mask = rt_mask;
3518 static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
3520 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
3521 *regnum = WINED3D_FFP_POSITION;
3522 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
3523 *regnum = WINED3D_FFP_BLENDWEIGHT;
3524 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
3525 *regnum = WINED3D_FFP_BLENDINDICES;
3526 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
3527 *regnum = WINED3D_FFP_NORMAL;
3528 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
3529 *regnum = WINED3D_FFP_PSIZE;
3530 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
3531 *regnum = WINED3D_FFP_DIFFUSE;
3532 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
3533 *regnum = WINED3D_FFP_SPECULAR;
3534 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
3535 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
3536 else
3538 WARN("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage), usage_idx);
3539 *regnum = ~0u;
3540 return FALSE;
3543 return TRUE;
3546 /* Context activation is done by the caller. */
3547 void wined3d_stream_info_from_declaration(struct wined3d_stream_info *stream_info,
3548 const struct wined3d_state *state, const struct wined3d_d3d_info *d3d_info)
3550 /* We need to deal with frequency data! */
3551 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
3552 BOOL generic_attributes = d3d_info->ffp_generic_attributes;
3553 BOOL use_vshader = use_vs(state);
3554 unsigned int i;
3556 stream_info->use_map = 0;
3557 stream_info->swizzle_map = 0;
3558 stream_info->position_transformed = 0;
3560 if (!declaration)
3561 return;
3563 stream_info->position_transformed = declaration->position_transformed;
3565 /* Translate the declaration into strided data. */
3566 for (i = 0; i < declaration->element_count; ++i)
3568 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
3569 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
3570 BOOL stride_used;
3571 unsigned int idx;
3573 TRACE("%p Element %p (%u of %u).\n", declaration->elements,
3574 element, i + 1, declaration->element_count);
3576 if (!stream->buffer)
3577 continue;
3579 TRACE("offset %u input_slot %u usage_idx %d.\n", element->offset, element->input_slot, element->usage_idx);
3581 if (use_vshader)
3583 if (element->output_slot == WINED3D_OUTPUT_SLOT_UNUSED)
3585 stride_used = FALSE;
3587 else if (element->output_slot == WINED3D_OUTPUT_SLOT_SEMANTIC)
3589 /* TODO: Assuming vertexdeclarations are usually used with the
3590 * same or a similar shader, it might be worth it to store the
3591 * last used output slot and try that one first. */
3592 stride_used = vshader_get_input(state->shader[WINED3D_SHADER_TYPE_VERTEX],
3593 element->usage, element->usage_idx, &idx);
3595 else
3597 idx = element->output_slot;
3598 stride_used = TRUE;
3601 else
3603 if (!generic_attributes && !element->ffp_valid)
3605 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3606 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
3607 stride_used = FALSE;
3609 else
3611 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
3615 if (stride_used)
3617 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3618 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3619 use_vshader ? "shader": "fixed function", idx,
3620 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
3621 element->offset, stream->stride, debug_d3dformat(element->format->id),
3622 debug_d3dinput_classification(element->input_slot_class), element->instance_data_step_rate);
3624 stream_info->elements[idx].format = element->format;
3625 stream_info->elements[idx].data.buffer_object = 0;
3626 stream_info->elements[idx].data.addr = (BYTE *)NULL + stream->offset + element->offset;
3627 stream_info->elements[idx].stride = stream->stride;
3628 stream_info->elements[idx].stream_idx = element->input_slot;
3629 if (stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
3631 stream_info->elements[idx].divisor = 1;
3633 else if (element->input_slot_class == WINED3D_INPUT_PER_INSTANCE_DATA)
3635 stream_info->elements[idx].divisor = element->instance_data_step_rate;
3636 if (!element->instance_data_step_rate)
3637 FIXME("Instance step rate 0 not implemented.\n");
3639 else
3641 stream_info->elements[idx].divisor = 0;
3644 if (!d3d_info->vertex_bgra && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
3646 stream_info->swizzle_map |= 1u << idx;
3648 stream_info->use_map |= 1u << idx;
3653 /* Context activation is done by the caller. */
3654 static void context_update_stream_info(struct wined3d_context *context, const struct wined3d_state *state)
3656 struct wined3d_stream_info *stream_info = &context->stream_info;
3657 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3658 DWORD prev_all_vbo = stream_info->all_vbo;
3659 unsigned int i;
3660 WORD map;
3662 wined3d_stream_info_from_declaration(stream_info, state, d3d_info);
3664 stream_info->all_vbo = 1;
3665 context->buffer_fence_count = 0;
3666 for (i = 0, map = stream_info->use_map; map; map >>= 1, ++i)
3668 struct wined3d_stream_info_element *element;
3669 struct wined3d_bo_address data;
3670 struct wined3d_buffer *buffer;
3672 if (!(map & 1))
3673 continue;
3675 element = &stream_info->elements[i];
3676 buffer = state->streams[element->stream_idx].buffer;
3678 /* We can't use VBOs if the base vertex index is negative. OpenGL
3679 * doesn't accept negative offsets (or rather offsets bigger than the
3680 * VBO, because the pointer is unsigned), so use system memory
3681 * sources. In most sane cases the pointer - offset will still be > 0,
3682 * otherwise it will wrap around to some big value. Hope that with the
3683 * indices the driver wraps it back internally. If not,
3684 * draw_primitive_immediate_mode() is needed, including a vertex buffer
3685 * path. */
3686 if (state->load_base_vertex_index < 0)
3688 WARN_(d3d_perf)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3689 state->load_base_vertex_index);
3690 element->data.buffer_object = 0;
3691 element->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3692 if ((UINT_PTR)element->data.addr < -state->load_base_vertex_index * element->stride)
3693 FIXME("System memory vertex data load offset is negative!\n");
3695 else
3697 wined3d_buffer_load(buffer, context, state);
3698 wined3d_buffer_get_memory(buffer, &data, buffer->locations);
3699 element->data.buffer_object = data.buffer_object;
3700 element->data.addr += (ULONG_PTR)data.addr;
3703 if (!element->data.buffer_object)
3704 stream_info->all_vbo = 0;
3706 if (buffer->fence)
3707 context->buffer_fences[context->buffer_fence_count++] = buffer->fence;
3709 TRACE("Load array %u %s.\n", i, debug_bo_address(&element->data));
3712 if (prev_all_vbo != stream_info->all_vbo)
3713 context_invalidate_state(context, STATE_INDEXBUFFER);
3715 context->use_immediate_mode_draw = FALSE;
3717 if (stream_info->all_vbo)
3718 return;
3720 if (!use_vs(state))
3722 WORD slow_mask = -!d3d_info->ffp_generic_attributes & (1u << WINED3D_FFP_PSIZE);
3723 slow_mask |= -(!d3d_info->vertex_bgra && !d3d_info->ffp_generic_attributes)
3724 & ((1u << WINED3D_FFP_DIFFUSE) | (1u << WINED3D_FFP_SPECULAR) | (1u << WINED3D_FFP_BLENDWEIGHT));
3726 if ((stream_info->position_transformed && !d3d_info->xyzrhw)
3727 || (stream_info->use_map & slow_mask))
3728 context->use_immediate_mode_draw = TRUE;
3732 /* Context activation is done by the caller. */
3733 static void context_preload_texture(struct wined3d_context *context,
3734 const struct wined3d_state *state, unsigned int idx)
3736 struct wined3d_texture *texture;
3738 if (!(texture = state->textures[idx]))
3739 return;
3741 if (wined3d_resource_check_fbo_attached(state, &texture->resource, NULL))
3742 context->uses_fbo_attached_resources = 1;
3744 wined3d_texture_load(texture, context, is_srgb_enabled(state->sampler_states[idx]));
3747 /* Context activation is done by the caller. */
3748 static void context_preload_textures(struct wined3d_context *context, const struct wined3d_state *state)
3750 unsigned int i;
3752 if (use_vs(state))
3754 for (i = 0; i < WINED3D_MAX_VERTEX_SAMPLERS; ++i)
3756 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info[i].type)
3757 context_preload_texture(context, state, WINED3D_MAX_FRAGMENT_SAMPLERS + i);
3761 if (use_ps(state))
3763 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
3765 if (state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info[i].type)
3766 context_preload_texture(context, state, i);
3769 else
3771 WORD ffu_map = context->fixed_function_usage_map;
3773 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3775 if (ffu_map & 1)
3776 context_preload_texture(context, state, i);
3781 static void context_load_shader_resources(struct wined3d_context *context, const struct wined3d_state *state,
3782 unsigned int shader_mask)
3784 struct wined3d_shader_sampler_map_entry *entry;
3785 struct wined3d_shader_resource_view *view;
3786 struct wined3d_shader *shader;
3787 unsigned int i, j;
3789 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
3791 if (!(shader_mask & (1u << i)))
3792 continue;
3794 if (!(shader = state->shader[i]))
3795 continue;
3797 for (j = 0; j < WINED3D_MAX_CBS; ++j)
3799 if (state->cb[i][j])
3800 wined3d_buffer_load(state->cb[i][j], context, state);
3803 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
3805 entry = &shader->reg_maps.sampler_map.entries[j];
3807 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
3808 continue;
3810 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3811 wined3d_buffer_load(buffer_from_resource(view->resource), context, state);
3812 else
3813 wined3d_texture_load(texture_from_resource(view->resource), context, FALSE);
3818 static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl *context_gl,
3819 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
3821 unsigned int bind_idx, shader_sampler_count, base, count, i;
3822 const struct wined3d_device *device = context_gl->c.device;
3823 struct wined3d_shader_sampler_map_entry *entry;
3824 struct wined3d_shader_resource_view *view;
3825 const struct wined3d_shader *shader;
3826 const unsigned int *tex_unit_map;
3827 struct wined3d_sampler *sampler;
3829 if (!(shader = state->shader[shader_type]))
3830 return;
3832 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl,
3833 &shader->reg_maps.shader_version, &base, &count);
3835 shader_sampler_count = shader->reg_maps.sampler_map.count;
3836 if (shader_sampler_count > count)
3837 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3838 shader, shader_sampler_count, count);
3839 count = min(shader_sampler_count, count);
3841 for (i = 0; i < count; ++i)
3843 entry = &shader->reg_maps.sampler_map.entries[i];
3844 bind_idx = base + entry->bind_idx;
3845 if (tex_unit_map)
3846 bind_idx = tex_unit_map[bind_idx];
3848 if (!(view = state->shader_resource_view[shader_type][entry->resource_idx]))
3850 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
3851 continue;
3854 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
3855 sampler = device->default_sampler;
3856 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
3857 sampler = device->null_sampler;
3858 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view),
3859 bind_idx, wined3d_sampler_gl(sampler), context_gl);
3863 static void context_load_unordered_access_resources(struct wined3d_context *context,
3864 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3866 struct wined3d_unordered_access_view *view;
3867 struct wined3d_texture *texture;
3868 struct wined3d_buffer *buffer;
3869 unsigned int i;
3871 context->uses_uavs = 0;
3873 if (!shader)
3874 return;
3876 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3878 if (!(view = views[i]))
3879 continue;
3881 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3883 buffer = buffer_from_resource(view->resource);
3884 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);
3885 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
3887 else
3889 texture = texture_from_resource(view->resource);
3890 wined3d_texture_load(texture, context, FALSE);
3891 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_TEXTURE_RGB);
3894 context->uses_uavs = 1;
3898 static void wined3d_context_gl_bind_unordered_access_views(struct wined3d_context_gl *context_gl,
3899 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3901 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3902 struct wined3d_unordered_access_view_gl *view_gl;
3903 const struct wined3d_format_gl *format_gl;
3904 GLuint texture_name;
3905 unsigned int i;
3906 GLint level;
3908 if (!shader)
3909 return;
3911 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3913 if (!views[i])
3915 if (shader->reg_maps.uav_resource_info[i].type)
3916 WARN("No unordered access view bound at index %u.\n", i);
3917 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3918 continue;
3921 view_gl = wined3d_unordered_access_view_gl(views[i]);
3922 if (view_gl->gl_view.name)
3924 texture_name = view_gl->gl_view.name;
3925 level = 0;
3927 else if (view_gl->v.resource->type != WINED3D_RTYPE_BUFFER)
3929 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(view_gl->v.resource));
3930 texture_name = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, FALSE);
3931 level = view_gl->v.desc.u.texture.level_idx;
3933 else
3935 FIXME("Unsupported buffer unordered access view.\n");
3936 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3937 continue;
3940 format_gl = wined3d_format_gl(view_gl->v.format);
3941 GL_EXTCALL(glBindImageTexture(i, texture_name, level, GL_TRUE, 0, GL_READ_WRITE,
3942 format_gl->internal));
3944 if (view_gl->counter_bo)
3945 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, i, view_gl->counter_bo));
3947 checkGLcall("Bind unordered access views");
3950 static void context_load_stream_output_buffers(struct wined3d_context *context,
3951 const struct wined3d_state *state)
3953 unsigned int i;
3955 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3957 struct wined3d_buffer *buffer;
3958 if (!(buffer = state->stream_output[i].buffer))
3959 continue;
3961 wined3d_buffer_load(buffer, context, state);
3962 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
3966 /* Context activation is done by the caller. */
3967 static BOOL context_apply_draw_state(struct wined3d_context *context,
3968 const struct wined3d_device *device, const struct wined3d_state *state)
3970 const struct wined3d_state_entry *state_table = context->state_table;
3971 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3972 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3973 const struct wined3d_fb_state *fb = state->fb;
3974 unsigned int i, base;
3975 WORD map;
3977 context->uses_fbo_attached_resources = 0;
3979 if (!have_framebuffer_attachment(gl_info->limits.buffers, fb->render_targets, fb->depth_stencil))
3981 if (!gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
3983 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
3984 return FALSE;
3987 wined3d_context_gl_set_render_offscreen(context_gl, TRUE);
3990 /* Preload resources before FBO setup. Texture preload in particular may
3991 * result in changes to the current FBO, due to using e.g. FBO blits for
3992 * updating a resource location. */
3993 wined3d_context_gl_update_tex_unit_map(context_gl, state);
3994 context_preload_textures(context, state);
3995 context_load_shader_resources(context, state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
3996 context_load_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_PIXEL],
3997 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3998 context_load_stream_output_buffers(context, state);
3999 /* TODO: Right now the dependency on the vertex shader is necessary
4000 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
4001 * the current VS but maybe it's possible to relax the coupling in some
4002 * situations at least. */
4003 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC)
4004 || isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
4006 context_update_stream_info(context, state);
4008 else
4010 for (i = 0, map = context->stream_info.use_map; map; map >>= 1, ++i)
4012 if (map & 1)
4013 wined3d_buffer_load(state->streams[context->stream_info.elements[i].stream_idx].buffer,
4014 context, state);
4016 /* Loading the buffers above may have invalidated the stream info. */
4017 if (isStateDirty(context, STATE_STREAMSRC))
4018 context_update_stream_info(context, state);
4020 if (state->index_buffer)
4022 if (context->stream_info.all_vbo)
4023 wined3d_buffer_load(state->index_buffer, context, state);
4024 else
4025 wined3d_buffer_load_sysmem(state->index_buffer, context);
4028 for (i = 0, base = 0; i < ARRAY_SIZE(context->dirty_graphics_states); ++i)
4030 uint32_t dirty_mask = context->dirty_graphics_states[i];
4032 while (dirty_mask)
4034 unsigned int state_id = base + wined3d_bit_scan(&dirty_mask);
4036 state_table[state_id].apply(context, state, state_id);
4037 context->dirty_graphics_states[i] &= ~(1u << (state_id - base));
4039 base += sizeof(dirty_mask) * CHAR_BIT;
4042 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
4044 device->shader_backend->shader_select(device->shader_priv, context, state);
4045 context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
4048 if (context->constant_update_mask)
4050 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
4051 context->constant_update_mask = 0;
4054 if (context->update_shader_resource_bindings)
4056 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
4057 wined3d_context_gl_bind_shader_resources(context_gl, state, i);
4058 context->update_shader_resource_bindings = 0;
4059 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4060 context->update_compute_shader_resource_bindings = 1;
4063 if (context->update_unordered_access_view_bindings)
4065 wined3d_context_gl_bind_unordered_access_views(context_gl,
4066 state->shader[WINED3D_SHADER_TYPE_PIXEL],
4067 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
4068 context->update_unordered_access_view_bindings = 0;
4069 context->update_compute_unordered_access_view_bindings = 1;
4072 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4073 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
4075 context->last_was_blit = FALSE;
4076 context->last_was_ffp_blit = FALSE;
4078 return TRUE;
4081 static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl *context_gl,
4082 const struct wined3d_device *device, const struct wined3d_state *state)
4084 const struct wined3d_state_entry *state_table = context_gl->c.state_table;
4085 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4086 unsigned int state_id, i;
4088 context_load_shader_resources(&context_gl->c, state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
4089 context_load_unordered_access_resources(&context_gl->c, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4090 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4092 for (i = 0, state_id = STATE_COMPUTE_OFFSET; i < ARRAY_SIZE(context_gl->c.dirty_compute_states); ++i)
4094 unsigned int dirty_mask = context_gl->c.dirty_compute_states[i];
4096 while (dirty_mask)
4098 unsigned int current_state_id = state_id + wined3d_bit_scan(&dirty_mask);
4099 state_table[current_state_id].apply(&context_gl->c, state, current_state_id);
4101 state_id += sizeof(*context_gl->c.dirty_compute_states) * CHAR_BIT;
4103 memset(context_gl->c.dirty_compute_states, 0, sizeof(*context_gl->c.dirty_compute_states));
4105 if (context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
4107 device->shader_backend->shader_select_compute(device->shader_priv, &context_gl->c, state);
4108 context_gl->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
4111 if (context_gl->c.update_compute_shader_resource_bindings)
4113 wined3d_context_gl_bind_shader_resources(context_gl, state, WINED3D_SHADER_TYPE_COMPUTE);
4114 context_gl->c.update_compute_shader_resource_bindings = 0;
4115 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4116 context_gl->c.update_shader_resource_bindings = 1;
4119 if (context_gl->c.update_compute_unordered_access_view_bindings)
4121 wined3d_context_gl_bind_unordered_access_views(context_gl,
4122 state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4123 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4124 context_gl->c.update_compute_unordered_access_view_bindings = 0;
4125 context_gl->c.update_unordered_access_view_bindings = 1;
4128 /* Updates to currently bound render targets aren't necessarily coherent
4129 * between the graphics and compute pipelines. Unbind any currently bound
4130 * FBO here to ensure preceding updates to its attachments by the graphics
4131 * pipeline are visible to the compute pipeline.
4133 * Without this, the bloom effect in Nier:Automata is too bright on the
4134 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4135 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
4136 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
4138 context_gl->c.last_was_blit = FALSE;
4139 context_gl->c.last_was_ffp_blit = FALSE;
4142 static BOOL use_transform_feedback(const struct wined3d_state *state)
4144 const struct wined3d_shader *shader;
4145 if (!(shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
4146 return FALSE;
4147 return shader->u.gs.so_desc.element_count;
4150 void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl *context_gl)
4152 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4154 if (context_gl->c.transform_feedback_active)
4156 GL_EXTCALL(glEndTransformFeedback());
4157 checkGLcall("glEndTransformFeedback");
4158 context_gl->c.transform_feedback_active = 0;
4159 context_gl->c.transform_feedback_paused = 0;
4163 static void wined3d_context_gl_pause_transform_feedback(struct wined3d_context_gl *context_gl, BOOL force)
4165 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4167 if (!context_gl->c.transform_feedback_active || context_gl->c.transform_feedback_paused)
4168 return;
4170 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK2])
4172 GL_EXTCALL(glPauseTransformFeedback());
4173 checkGLcall("glPauseTransformFeedback");
4174 context_gl->c.transform_feedback_paused = 1;
4175 return;
4178 WARN("Cannot pause transform feedback operations.\n");
4180 if (force)
4181 wined3d_context_gl_end_transform_feedback(context_gl);
4184 static void wined3d_context_gl_setup_target(struct wined3d_context_gl *context_gl,
4185 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4187 BOOL old_render_offscreen = context_gl->c.render_offscreen, render_offscreen;
4189 render_offscreen = wined3d_resource_is_offscreen(&texture->resource);
4190 if (context_gl->c.current_rt.texture == texture
4191 && context_gl->c.current_rt.sub_resource_idx == sub_resource_idx
4192 && render_offscreen == old_render_offscreen)
4193 return;
4195 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4196 * the alpha blend state changes with different render target formats. */
4197 if (!context_gl->c.current_rt.texture)
4199 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
4201 else
4203 const struct wined3d_format *old = context_gl->c.current_rt.texture->resource.format;
4204 const struct wined3d_format *new = texture->resource.format;
4206 if (old->id != new->id)
4208 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4209 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
4210 || !(texture->resource.format_flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
4211 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
4213 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
4214 if ((context_gl->c.current_rt.texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE)
4215 != (texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE))
4216 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
4219 /* When switching away from an offscreen render target, and we're not
4220 * using FBOs, we have to read the drawable into the texture. This is
4221 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4222 * There are some things that need care though. PreLoad needs a GL context,
4223 * and FindContext is called before the context is activated. It also
4224 * has to be called with the old rendertarget active, otherwise a
4225 * wrong drawable is read. */
4226 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4227 && old_render_offscreen && (context_gl->c.current_rt.texture != texture
4228 || context_gl->c.current_rt.sub_resource_idx != sub_resource_idx))
4230 struct wined3d_texture_gl *prev_texture = wined3d_texture_gl(context_gl->c.current_rt.texture);
4231 unsigned int prev_sub_resource_idx = context_gl->c.current_rt.sub_resource_idx;
4233 /* Read the back buffer of the old drawable into the destination texture. */
4234 if (prev_texture->texture_srgb.name)
4235 wined3d_texture_load(&prev_texture->t, &context_gl->c, TRUE);
4236 wined3d_texture_load(&prev_texture->t, &context_gl->c, FALSE);
4237 wined3d_texture_invalidate_location(&prev_texture->t, prev_sub_resource_idx, WINED3D_LOCATION_DRAWABLE);
4241 context_gl->c.current_rt.texture = texture;
4242 context_gl->c.current_rt.sub_resource_idx = sub_resource_idx;
4243 wined3d_context_gl_set_render_offscreen(context_gl, render_offscreen);
4246 static void wined3d_context_gl_activate(struct wined3d_context_gl *context_gl,
4247 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4249 wined3d_context_gl_enter(context_gl);
4250 wined3d_context_gl_update_window(context_gl);
4251 wined3d_context_gl_setup_target(context_gl, texture, sub_resource_idx);
4252 if (!context_gl->valid)
4253 return;
4255 if (context_gl != wined3d_context_gl_get_current())
4257 if (!wined3d_context_gl_set_current(context_gl))
4258 ERR("Failed to activate the new context.\n");
4260 else if (context_gl->needs_set)
4262 wined3d_context_gl_set_gl_context(context_gl);
4266 struct wined3d_context *wined3d_context_gl_acquire(const struct wined3d_device *device,
4267 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4269 struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
4270 struct wined3d_context_gl *context_gl;
4272 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device, texture, sub_resource_idx);
4274 if (current_context && current_context->c.destroyed)
4275 current_context = NULL;
4277 if (!texture)
4279 if (current_context
4280 && current_context->c.current_rt.texture
4281 && current_context->c.device == device)
4283 texture = current_context->c.current_rt.texture;
4284 sub_resource_idx = current_context->c.current_rt.sub_resource_idx;
4286 else
4288 struct wined3d_swapchain *swapchain = device->swapchains[0];
4290 if (swapchain->back_buffers)
4291 texture = swapchain->back_buffers[0];
4292 else
4293 texture = swapchain->front_buffer;
4294 sub_resource_idx = 0;
4298 if (current_context && current_context->c.current_rt.texture == texture)
4300 context_gl = current_context;
4302 else if (!wined3d_resource_is_offscreen(&texture->resource))
4304 TRACE("Rendering onscreen.\n");
4306 if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(texture->swapchain))))
4307 return NULL;
4309 else
4311 TRACE("Rendering offscreen.\n");
4313 /* Stay with the current context if possible. Otherwise use the
4314 * context for the primary swapchain. */
4315 if (current_context && current_context->c.device == device)
4316 context_gl = current_context;
4317 else if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(device->swapchains[0]))))
4318 return NULL;
4321 wined3d_context_gl_activate(context_gl, texture, sub_resource_idx);
4323 return &context_gl->c;
4326 struct wined3d_context_gl *wined3d_context_gl_reacquire(struct wined3d_context_gl *context_gl)
4328 struct wined3d_context *acquired_context;
4329 struct wined3d_device *device;
4331 if (!context_gl || context_gl->tid != GetCurrentThreadId())
4332 return NULL;
4334 device = context_gl->c.device;
4335 wined3d_from_cs(device->cs);
4337 if (context_gl->c.current_rt.texture)
4339 wined3d_context_gl_activate(context_gl, context_gl->c.current_rt.texture,
4340 context_gl->c.current_rt.sub_resource_idx);
4341 return context_gl;
4344 acquired_context = context_acquire(device, NULL, 0);
4345 if (acquired_context != &context_gl->c)
4346 ERR("Acquired context %p instead of %p.\n", acquired_context, &context_gl->c);
4347 return wined3d_context_gl(acquired_context);
4350 void dispatch_compute(struct wined3d_device *device, const struct wined3d_state *state,
4351 const struct wined3d_dispatch_parameters *parameters)
4353 const struct wined3d_gl_info *gl_info;
4354 struct wined3d_context_gl *context_gl;
4356 context_gl = wined3d_context_gl(context_acquire(device, NULL, 0));
4357 if (!context_gl->valid)
4359 context_release(&context_gl->c);
4360 WARN("Invalid context, skipping dispatch.\n");
4361 return;
4363 gl_info = context_gl->gl_info;
4365 if (!gl_info->supported[ARB_COMPUTE_SHADER])
4367 context_release(&context_gl->c);
4368 FIXME("OpenGL implementation does not support compute shaders.\n");
4369 return;
4372 if (parameters->indirect)
4373 wined3d_buffer_load(parameters->u.indirect.buffer, &context_gl->c, state);
4375 wined3d_context_gl_apply_compute_state(context_gl, device, state);
4377 if (!state->shader[WINED3D_SHADER_TYPE_COMPUTE])
4379 context_release(&context_gl->c);
4380 WARN("No compute shader bound, skipping dispatch.\n");
4381 return;
4384 if (parameters->indirect)
4386 const struct wined3d_indirect_dispatch_parameters *indirect = &parameters->u.indirect;
4387 struct wined3d_buffer *buffer = indirect->buffer;
4389 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, buffer->buffer_object));
4390 GL_EXTCALL(glDispatchComputeIndirect((GLintptr)indirect->offset));
4391 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0));
4393 else
4395 const struct wined3d_direct_dispatch_parameters *direct = &parameters->u.direct;
4396 GL_EXTCALL(glDispatchCompute(direct->group_count_x, direct->group_count_y, direct->group_count_z));
4398 checkGLcall("dispatch compute");
4400 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4401 checkGLcall("glMemoryBarrier");
4403 context_release(&context_gl->c);
4406 /* Context activation is done by the caller. */
4407 static void wined3d_context_gl_draw_primitive_arrays(struct wined3d_context_gl *context_gl,
4408 const struct wined3d_state *state, const void *idx_data, unsigned int idx_size, int base_vertex_idx,
4409 unsigned int start_idx, unsigned int count, unsigned int start_instance, unsigned int instance_count)
4411 const struct wined3d_ffp_attrib_ops *ops = &context_gl->c.d3d_info->ffp_attrib_ops;
4412 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4413 const struct wined3d_stream_info *si = &context_gl->c.stream_info;
4414 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4415 unsigned int instanced_elements[ARRAY_SIZE(si->elements)];
4416 unsigned int instanced_element_count = 0;
4417 GLenum mode = state->gl_primitive_type;
4418 const void *indices;
4419 unsigned int i, j;
4421 indices = (const char *)idx_data + idx_size * start_idx;
4423 if (!instance_count)
4425 if (!idx_size)
4427 gl_info->gl_ops.gl.p_glDrawArrays(mode, start_idx, count);
4428 checkGLcall("glDrawArrays");
4429 return;
4432 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4434 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4435 checkGLcall("glDrawElementsBaseVertex");
4436 return;
4439 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4440 checkGLcall("glDrawElements");
4441 return;
4444 if (start_instance && !(gl_info->supported[ARB_BASE_INSTANCE] && gl_info->supported[ARB_INSTANCED_ARRAYS]))
4445 FIXME("Start instance (%u) not supported.\n", start_instance);
4447 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
4449 if (!idx_size)
4451 if (gl_info->supported[ARB_BASE_INSTANCE])
4453 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode, start_idx, count, instance_count, start_instance));
4454 checkGLcall("glDrawArraysInstancedBaseInstance");
4455 return;
4458 GL_EXTCALL(glDrawArraysInstanced(mode, start_idx, count, instance_count));
4459 checkGLcall("glDrawArraysInstanced");
4460 return;
4463 if (gl_info->supported[ARB_BASE_INSTANCE])
4465 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode, count, idx_type,
4466 indices, instance_count, base_vertex_idx, start_instance));
4467 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4468 return;
4470 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4472 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode, count, idx_type,
4473 indices, instance_count, base_vertex_idx));
4474 checkGLcall("glDrawElementsInstancedBaseVertex");
4475 return;
4478 GL_EXTCALL(glDrawElementsInstanced(mode, count, idx_type, indices, instance_count));
4479 checkGLcall("glDrawElementsInstanced");
4480 return;
4483 /* Instancing emulation by mixing immediate mode and arrays. */
4485 /* This is a nasty thing. MSDN says no hardware supports this and
4486 * applications have to use software vertex processing. We don't support
4487 * this for now.
4489 * Shouldn't be too hard to support with OpenGL, in theory just call
4490 * glDrawArrays() instead of drawElements(). But the stream fequency value
4491 * has a different meaning in that situation. */
4492 if (!idx_size)
4494 FIXME("Non-indexed instanced drawing is not supported.\n");
4495 return;
4498 for (i = 0; i < ARRAY_SIZE(si->elements); ++i)
4500 if (!(si->use_map & (1u << i)))
4501 continue;
4503 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
4504 instanced_elements[instanced_element_count++] = i;
4507 for (i = 0; i < instance_count; ++i)
4509 /* Specify the instanced attributes using immediate mode calls. */
4510 for (j = 0; j < instanced_element_count; ++j)
4512 const struct wined3d_stream_info_element *element;
4513 unsigned int element_idx;
4514 const BYTE *ptr;
4516 element_idx = instanced_elements[j];
4517 element = &si->elements[element_idx];
4518 ptr = element->data.addr + element->stride * i;
4519 if (element->data.buffer_object)
4520 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(state->streams[element->stream_idx].buffer,
4521 &context_gl->c);
4522 ops->generic[element->format->emit_idx](element_idx, ptr);
4525 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4527 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4528 checkGLcall("glDrawElementsBaseVertex");
4530 else
4532 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4533 checkGLcall("glDrawElements");
4538 static unsigned int get_stride_idx(const void *idx_data, unsigned int idx_size,
4539 unsigned int base_vertex_idx, unsigned int start_idx, unsigned int vertex_idx)
4541 if (!idx_data)
4542 return start_idx + vertex_idx;
4543 if (idx_size == 2)
4544 return ((const WORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4545 return ((const DWORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4548 /* Context activation is done by the caller. */
4549 static void draw_primitive_immediate_mode(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4550 const struct wined3d_stream_info *si, const void *idx_data, unsigned int idx_size,
4551 int base_vertex_idx, unsigned int start_idx, unsigned int vertex_count, unsigned int instance_count)
4553 const BYTE *position = NULL, *normal = NULL, *diffuse = NULL, *specular = NULL;
4554 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
4555 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4556 unsigned int coord_idx, stride_idx, texture_idx, vertex_idx;
4557 const struct wined3d_stream_info_element *element;
4558 const BYTE *tex_coords[WINED3DDP_MAXTEXCOORD];
4559 unsigned int texture_unit, texture_stages;
4560 const struct wined3d_ffp_attrib_ops *ops;
4561 unsigned int untracked_material_count;
4562 unsigned int tex_mask = 0;
4563 BOOL specular_fog = FALSE;
4564 BOOL ps = use_ps(state);
4565 const void *ptr;
4567 static unsigned int once;
4569 if (!once++)
4570 FIXME_(d3d_perf)("Drawing using immediate mode.\n");
4571 else
4572 WARN_(d3d_perf)("Drawing using immediate mode.\n");
4574 if (!idx_size && idx_data)
4575 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4577 if (instance_count)
4578 FIXME("Instancing not implemented.\n");
4580 /* Immediate mode drawing can't make use of indices in a VBO - get the
4581 * data from the index buffer. */
4582 if (idx_size)
4583 idx_data = wined3d_buffer_load_sysmem(state->index_buffer, &context_gl->c) + state->index_offset;
4585 ops = &d3d_info->ffp_attrib_ops;
4587 gl_info->gl_ops.gl.p_glBegin(state->gl_primitive_type);
4589 if (use_vs(state) || d3d_info->ffp_generic_attributes)
4591 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4593 unsigned int use_map = si->use_map;
4594 unsigned int element_idx;
4596 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4597 for (element_idx = MAX_ATTRIBS - 1; use_map; use_map &= ~(1u << element_idx), --element_idx)
4599 if (!(use_map & 1u << element_idx))
4600 continue;
4602 ptr = si->elements[element_idx].data.addr + si->elements[element_idx].stride * stride_idx;
4603 ops->generic[si->elements[element_idx].format->emit_idx](element_idx, ptr);
4607 gl_info->gl_ops.gl.p_glEnd();
4608 return;
4611 if (si->use_map & (1u << WINED3D_FFP_POSITION))
4612 position = si->elements[WINED3D_FFP_POSITION].data.addr;
4614 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
4615 normal = si->elements[WINED3D_FFP_NORMAL].data.addr;
4616 else
4617 gl_info->gl_ops.gl.p_glNormal3f(0.0f, 0.0f, 0.0f);
4619 untracked_material_count = context_gl->untracked_material_count;
4620 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
4622 element = &si->elements[WINED3D_FFP_DIFFUSE];
4623 diffuse = element->data.addr;
4625 if (untracked_material_count && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
4626 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element->format->id));
4628 else
4630 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4633 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
4635 element = &si->elements[WINED3D_FFP_SPECULAR];
4636 specular = element->data.addr;
4638 /* Special case where the fog density is stored in the specular alpha channel. */
4639 if (state->render_states[WINED3D_RS_FOGENABLE]
4640 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
4641 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
4642 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
4644 if (gl_info->supported[EXT_FOG_COORD])
4646 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
4647 specular_fog = TRUE;
4648 else
4649 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element->format->id));
4651 else
4653 static unsigned int once;
4655 if (!once++)
4656 FIXME("Implement fog for transformed vertices in software.\n");
4660 else if (gl_info->supported[EXT_SECONDARY_COLOR])
4662 GL_EXTCALL(glSecondaryColor3fEXT)(0.0f, 0.0f, 0.0f);
4665 texture_stages = d3d_info->limits.ffp_blend_stages;
4666 for (texture_idx = 0; texture_idx < texture_stages; ++texture_idx)
4668 if (!gl_info->supported[ARB_MULTITEXTURE] && texture_idx > 0)
4670 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4671 continue;
4674 if (!ps && !state->textures[texture_idx])
4675 continue;
4677 texture_unit = context_gl->tex_unit_map[texture_idx];
4678 if (texture_unit == WINED3D_UNMAPPED_STAGE)
4679 continue;
4681 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4682 if (coord_idx > 7)
4684 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx, texture_idx);
4685 continue;
4688 if (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))
4690 tex_coords[coord_idx] = si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].data.addr;
4691 tex_mask |= (1u << texture_idx);
4693 else
4695 TRACE("Setting default coordinates for texture %u.\n", texture_idx);
4696 if (gl_info->supported[ARB_MULTITEXTURE])
4697 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_unit, 0.0f, 0.0f, 0.0f, 1.0f));
4698 else
4699 gl_info->gl_ops.gl.p_glTexCoord4f(0.0f, 0.0f, 0.0f, 1.0f);
4703 /* Blending data and point sizes are not supported by this function. They
4704 * are not supported by the fixed function pipeline at all. A FIXME for
4705 * them is printed after decoding the vertex declaration. */
4706 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4708 unsigned int tmp_tex_mask;
4710 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4712 if (normal)
4714 ptr = normal + stride_idx * si->elements[WINED3D_FFP_NORMAL].stride;
4715 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptr);
4718 if (diffuse)
4720 ptr = diffuse + stride_idx * si->elements[WINED3D_FFP_DIFFUSE].stride;
4721 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptr);
4723 if (untracked_material_count)
4725 struct wined3d_color color;
4726 unsigned int i;
4728 wined3d_color_from_d3dcolor(&color, *(const DWORD *)ptr);
4729 for (i = 0; i < untracked_material_count; ++i)
4731 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK,
4732 context_gl->untracked_materials[i], &color.r);
4737 if (specular)
4739 ptr = specular + stride_idx * si->elements[WINED3D_FFP_SPECULAR].stride;
4740 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptr);
4742 if (specular_fog)
4743 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD *)ptr >> 24)));
4746 tmp_tex_mask = tex_mask;
4747 for (texture_idx = 0; tmp_tex_mask; tmp_tex_mask >>= 1, ++texture_idx)
4749 if (!(tmp_tex_mask & 1))
4750 continue;
4752 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4753 ptr = tex_coords[coord_idx] + (stride_idx * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
4754 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
4755 GL_TEXTURE0_ARB + context_gl->tex_unit_map[texture_idx], ptr);
4758 if (position)
4760 ptr = position + stride_idx * si->elements[WINED3D_FFP_POSITION].stride;
4761 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptr);
4765 gl_info->gl_ops.gl.p_glEnd();
4766 checkGLcall("draw immediate mode");
4769 static void wined3d_context_gl_draw_indirect(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4770 const struct wined3d_indirect_draw_parameters *parameters, unsigned int idx_size)
4772 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4773 struct wined3d_buffer *buffer = parameters->buffer;
4774 const void *offset;
4776 if (!gl_info->supported[ARB_DRAW_INDIRECT])
4778 FIXME("OpenGL implementation does not support indirect draws.\n");
4779 return;
4782 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer->buffer_object));
4784 offset = (void *)(GLintptr)parameters->offset;
4785 if (idx_size)
4787 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4788 if (state->index_offset)
4789 FIXME("Ignoring index offset %u.\n", state->index_offset);
4790 GL_EXTCALL(glDrawElementsIndirect(state->gl_primitive_type, idx_type, offset));
4792 else
4794 GL_EXTCALL(glDrawArraysIndirect(state->gl_primitive_type, offset));
4797 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0));
4799 checkGLcall("draw indirect");
4802 static void remove_vbos(struct wined3d_context *context,
4803 const struct wined3d_state *state, struct wined3d_stream_info *s)
4805 unsigned int i;
4807 for (i = 0; i < ARRAY_SIZE(s->elements); ++i)
4809 struct wined3d_stream_info_element *e;
4811 if (!(s->use_map & (1u << i)))
4812 continue;
4814 e = &s->elements[i];
4815 if (e->data.buffer_object)
4817 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
4818 e->data.buffer_object = 0;
4819 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(vb, context);
4824 static GLenum gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
4826 GLenum gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
4827 switch (gl_primitive_type)
4829 case GL_POINTS:
4830 return GL_POINTS;
4832 case GL_LINE_STRIP:
4833 case GL_LINE_STRIP_ADJACENCY:
4834 case GL_LINES_ADJACENCY:
4835 case GL_LINES:
4836 return GL_LINES;
4838 case GL_TRIANGLE_FAN:
4839 case GL_TRIANGLE_STRIP:
4840 case GL_TRIANGLE_STRIP_ADJACENCY:
4841 case GL_TRIANGLES_ADJACENCY:
4842 case GL_TRIANGLES:
4843 return GL_TRIANGLES;
4845 default:
4846 return gl_primitive_type;
4850 /* Routine common to the draw primitive and draw indexed primitive routines */
4851 void draw_primitive(struct wined3d_device *device, const struct wined3d_state *state,
4852 const struct wined3d_draw_parameters *parameters)
4854 BOOL emulation = FALSE, rasterizer_discard = FALSE;
4855 const struct wined3d_fb_state *fb = state->fb;
4856 const struct wined3d_stream_info *stream_info;
4857 struct wined3d_rendertarget_view *dsv, *rtv;
4858 struct wined3d_stream_info si_emulated;
4859 struct wined3d_fence *ib_fence = NULL;
4860 const struct wined3d_gl_info *gl_info;
4861 struct wined3d_context_gl *context_gl;
4862 struct wined3d_context *context;
4863 unsigned int i, idx_size = 0;
4864 const void *idx_data = NULL;
4866 if (!parameters->indirect && !parameters->u.direct.index_count)
4867 return;
4869 if (!(rtv = fb->render_targets[0]))
4870 rtv = fb->depth_stencil;
4872 if (rtv && rtv->resource->type == WINED3D_RTYPE_BUFFER)
4874 FIXME("Buffer render targets not implemented.\n");
4875 return;
4878 if (rtv)
4879 context = context_acquire(device, wined3d_texture_from_resource(rtv->resource), rtv->sub_resource_idx);
4880 else
4881 context = context_acquire(device, NULL, 0);
4882 context_gl = wined3d_context_gl(context);
4883 if (!context_gl->valid)
4885 context_release(context);
4886 WARN("Invalid context, skipping draw.\n");
4887 return;
4889 gl_info = context_gl->gl_info;
4891 if (!use_transform_feedback(state))
4892 wined3d_context_gl_pause_transform_feedback(context_gl, TRUE);
4894 for (i = 0; i < gl_info->limits.buffers; ++i)
4896 if (!(rtv = fb->render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
4897 continue;
4899 if (state->render_states[WINED3D_RS_COLORWRITEENABLE])
4901 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
4902 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
4904 else
4906 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
4910 if ((dsv = fb->depth_stencil))
4912 /* Note that this depends on the context_acquire() call above to set
4913 * context->render_offscreen properly. We don't currently take the
4914 * Z-compare function into account, but we could skip loading the
4915 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
4916 * that we never copy the stencil data.*/
4917 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4919 if (state->render_states[WINED3D_RS_ZWRITEENABLE] || state->render_states[WINED3D_RS_ZENABLE])
4920 wined3d_rendertarget_view_load_location(dsv, context, location);
4921 else
4922 wined3d_rendertarget_view_prepare_location(dsv, context, location);
4925 if (parameters->indirect)
4926 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
4928 if (!context_apply_draw_state(context, device, state))
4930 context_release(context);
4931 WARN("Unable to apply draw state, skipping draw.\n");
4932 return;
4935 if (dsv && state->render_states[WINED3D_RS_ZWRITEENABLE])
4937 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
4939 wined3d_rendertarget_view_validate_location(dsv, location);
4940 wined3d_rendertarget_view_invalidate_location(dsv, ~location);
4943 stream_info = &context->stream_info;
4945 if (parameters->indexed)
4947 struct wined3d_buffer *index_buffer = state->index_buffer;
4948 if (!index_buffer->buffer_object || !stream_info->all_vbo)
4950 idx_data = index_buffer->resource.heap_memory;
4952 else
4954 ib_fence = index_buffer->fence;
4955 idx_data = NULL;
4957 idx_data = (const BYTE *)idx_data + state->index_offset;
4959 if (state->index_format == WINED3DFMT_R16_UINT)
4960 idx_size = 2;
4961 else
4962 idx_size = 4;
4965 if (!use_vs(state))
4967 if (!stream_info->position_transformed && context_gl->untracked_material_count
4968 && state->render_states[WINED3D_RS_LIGHTING])
4970 static BOOL warned;
4972 if (!warned++)
4973 FIXME("Using software emulation because not all material properties could be tracked.\n");
4974 else
4975 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
4976 emulation = TRUE;
4978 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
4980 static BOOL warned;
4982 /* Either write a pipeline replacement shader or convert the
4983 * specular alpha from unsigned byte to a float in the vertex
4984 * buffer. */
4985 if (!warned++)
4986 FIXME("Using software emulation because manual fog coordinates are provided.\n");
4987 else
4988 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
4989 emulation = TRUE;
4992 if (emulation)
4994 si_emulated = context->stream_info;
4995 remove_vbos(context, state, &si_emulated);
4996 stream_info = &si_emulated;
5000 if (use_transform_feedback(state))
5002 const struct wined3d_shader *shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5004 if (is_rasterization_disabled(shader))
5006 glEnable(GL_RASTERIZER_DISCARD);
5007 checkGLcall("enable rasterizer discard");
5008 rasterizer_discard = TRUE;
5011 if (context->transform_feedback_paused)
5013 GL_EXTCALL(glResumeTransformFeedback());
5014 checkGLcall("glResumeTransformFeedback");
5015 context->transform_feedback_paused = 0;
5017 else if (!context->transform_feedback_active)
5019 enum wined3d_primitive_type primitive_type = shader->u.gs.output_type
5020 ? shader->u.gs.output_type : d3d_primitive_type_from_gl(state->gl_primitive_type);
5021 GLenum mode = gl_tfb_primitive_type_from_d3d(primitive_type);
5022 GL_EXTCALL(glBeginTransformFeedback(mode));
5023 checkGLcall("glBeginTransformFeedback");
5024 context->transform_feedback_active = 1;
5028 if (state->gl_primitive_type == GL_PATCHES)
5030 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES, state->gl_patch_vertices));
5031 checkGLcall("glPatchParameteri");
5034 if (context->uses_fbo_attached_resources)
5036 static unsigned int fixme_once;
5038 if (gl_info->supported[ARB_TEXTURE_BARRIER])
5040 GL_EXTCALL(glTextureBarrier());
5042 else if (gl_info->supported[NV_TEXTURE_BARRIER])
5044 GL_EXTCALL(glTextureBarrierNV());
5046 else
5048 if (!fixme_once++)
5049 FIXME("Sampling attached render targets is not supported.\n");
5051 WARN("Sampling attached render targets is not supported, skipping draw.\n");
5052 context_release(context);
5053 return;
5055 checkGLcall("glTextureBarrier");
5058 if (parameters->indirect)
5060 if (!context->use_immediate_mode_draw && !emulation)
5061 wined3d_context_gl_draw_indirect(context_gl, state, &parameters->u.indirect, idx_size);
5062 else
5063 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
5065 else
5067 unsigned int instance_count = parameters->u.direct.instance_count;
5068 if (context->instance_count)
5069 instance_count = context->instance_count;
5071 if (context->use_immediate_mode_draw || emulation)
5072 draw_primitive_immediate_mode(wined3d_context_gl(context), state, stream_info, idx_data,
5073 idx_size, parameters->u.direct.base_vertex_idx,
5074 parameters->u.direct.start_idx, parameters->u.direct.index_count, instance_count);
5075 else
5076 wined3d_context_gl_draw_primitive_arrays(context_gl, state, idx_data, idx_size,
5077 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
5078 parameters->u.direct.index_count, parameters->u.direct.start_instance, instance_count);
5081 if (context->uses_uavs)
5083 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
5084 checkGLcall("glMemoryBarrier");
5087 wined3d_context_gl_pause_transform_feedback(context_gl, FALSE);
5089 if (rasterizer_discard)
5091 glDisable(GL_RASTERIZER_DISCARD);
5092 checkGLcall("disable rasterizer discard");
5095 if (ib_fence)
5096 wined3d_fence_issue(ib_fence, device);
5097 for (i = 0; i < context->buffer_fence_count; ++i)
5098 wined3d_fence_issue(context->buffer_fences[i], device);
5100 context_release(context);
5103 void wined3d_context_gl_unload_tex_coords(const struct wined3d_context_gl *context_gl)
5105 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5106 unsigned int texture_idx;
5108 for (texture_idx = 0; texture_idx < gl_info->limits.texture_coords; ++texture_idx)
5110 gl_info->gl_ops.ext.p_glClientActiveTextureARB(GL_TEXTURE0_ARB + texture_idx);
5111 gl_info->gl_ops.gl.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY);
5115 void wined3d_context_gl_load_tex_coords(const struct wined3d_context_gl *context_gl,
5116 const struct wined3d_stream_info *si, GLuint *current_bo, const struct wined3d_state *state)
5118 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5119 const struct wined3d_format_gl *format_gl;
5120 unsigned int mapped_stage = 0;
5121 unsigned int texture_idx;
5123 for (texture_idx = 0; texture_idx < context_gl->c.d3d_info->limits.ffp_blend_stages; ++texture_idx)
5125 unsigned int coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
5127 if ((mapped_stage = context_gl->tex_unit_map[texture_idx]) == WINED3D_UNMAPPED_STAGE)
5128 continue;
5130 if (mapped_stage >= gl_info->limits.texture_coords)
5132 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage);
5133 continue;
5136 if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
5138 const struct wined3d_stream_info_element *e = &si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx];
5140 TRACE("Setting up texture %u, idx %u, coord_idx %u, data %s.\n",
5141 texture_idx, mapped_stage, coord_idx, debug_bo_address(&e->data));
5143 if (*current_bo != e->data.buffer_object)
5145 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
5146 checkGLcall("glBindBuffer");
5147 *current_bo = e->data.buffer_object;
5150 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
5151 checkGLcall("glClientActiveTextureARB");
5153 /* The coords to supply depend completely on the fvf/vertex shader. */
5154 format_gl = wined3d_format_gl(e->format);
5155 gl_info->gl_ops.gl.p_glTexCoordPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5156 e->data.addr + state->load_base_vertex_index * e->stride);
5157 gl_info->gl_ops.gl.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY);
5159 else
5161 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + mapped_stage, 0, 0, 0, 1));
5164 if (gl_info->supported[NV_REGISTER_COMBINERS])
5166 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
5167 for (texture_idx = mapped_stage + 1; texture_idx < gl_info->limits.textures; ++texture_idx)
5169 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
5173 checkGLcall("loadTexCoords");
5176 /* This should match any arrays loaded in wined3d_context_gl_load_vertex_data(). */
5177 static void wined3d_context_gl_unload_vertex_data(struct wined3d_context_gl *context_gl)
5179 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5181 if (!context_gl->c.namedArraysLoaded)
5182 return;
5183 gl_info->gl_ops.gl.p_glDisableClientState(GL_VERTEX_ARRAY);
5184 gl_info->gl_ops.gl.p_glDisableClientState(GL_NORMAL_ARRAY);
5185 gl_info->gl_ops.gl.p_glDisableClientState(GL_COLOR_ARRAY);
5186 if (gl_info->supported[EXT_SECONDARY_COLOR])
5187 gl_info->gl_ops.gl.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5188 wined3d_context_gl_unload_tex_coords(context_gl);
5189 context_gl->c.namedArraysLoaded = FALSE;
5192 static void wined3d_context_gl_load_vertex_data(struct wined3d_context_gl *context_gl,
5193 const struct wined3d_stream_info *si, const struct wined3d_state *state)
5195 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5196 const struct wined3d_stream_info_element *e;
5197 const struct wined3d_format_gl *format_gl;
5198 GLuint current_bo;
5200 TRACE("context_gl %p, si %p, state %p.\n", context_gl, si, state);
5202 /* This is used for the fixed-function pipeline only, and the
5203 * fixed-function pipeline doesn't do instancing. */
5204 context_gl->c.instance_count = 0;
5205 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5207 /* Blend data */
5208 if ((si->use_map & (1u << WINED3D_FFP_BLENDWEIGHT))
5209 || si->use_map & (1u << WINED3D_FFP_BLENDINDICES))
5211 /* TODO: Support vertex blending in immediate mode draws. No need to
5212 * write a FIXME here, this is done after the general vertex
5213 * declaration decoding. */
5214 WARN("Vertex blending not supported.\n");
5217 /* Point Size */
5218 if (si->use_map & (1u << WINED3D_FFP_PSIZE))
5220 /* No such functionality in the fixed-function GL pipeline. */
5221 WARN("Per-vertex point size not supported.\n");
5224 /* Position */
5225 if (si->use_map & (1u << WINED3D_FFP_POSITION))
5227 e = &si->elements[WINED3D_FFP_POSITION];
5228 format_gl = wined3d_format_gl(e->format);
5230 if (current_bo != e->data.buffer_object)
5232 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
5233 checkGLcall("glBindBuffer");
5234 current_bo = e->data.buffer_object;
5237 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n",
5238 format_gl->vtx_format, format_gl->vtx_type, e->stride,
5239 e->data.addr + state->load_base_vertex_index * e->stride);
5240 gl_info->gl_ops.gl.p_glVertexPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5241 e->data.addr + state->load_base_vertex_index * e->stride);
5242 checkGLcall("glVertexPointer(...)");
5243 gl_info->gl_ops.gl.p_glEnableClientState(GL_VERTEX_ARRAY);
5244 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5247 /* Normals */
5248 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
5250 e = &si->elements[WINED3D_FFP_NORMAL];
5251 format_gl = wined3d_format_gl(e->format);
5253 if (current_bo != e->data.buffer_object)
5255 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
5256 checkGLcall("glBindBuffer");
5257 current_bo = e->data.buffer_object;
5260 TRACE("glNormalPointer(%#x, %#x, %p);\n", format_gl->vtx_type, e->stride,
5261 e->data.addr + state->load_base_vertex_index * e->stride);
5262 gl_info->gl_ops.gl.p_glNormalPointer(format_gl->vtx_type, e->stride,
5263 e->data.addr + state->load_base_vertex_index * e->stride);
5264 checkGLcall("glNormalPointer(...)");
5265 gl_info->gl_ops.gl.p_glEnableClientState(GL_NORMAL_ARRAY);
5266 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5269 else
5271 gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
5272 checkGLcall("glNormal3f(0, 0, 0)");
5275 /* Diffuse colour */
5276 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
5278 e = &si->elements[WINED3D_FFP_DIFFUSE];
5279 format_gl = wined3d_format_gl(e->format);
5281 if (current_bo != e->data.buffer_object)
5283 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
5284 checkGLcall("glBindBuffer");
5285 current_bo = e->data.buffer_object;
5288 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5289 format_gl->vtx_format, format_gl->vtx_type, e->stride,
5290 e->data.addr + state->load_base_vertex_index * e->stride);
5291 gl_info->gl_ops.gl.p_glColorPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5292 e->data.addr + state->load_base_vertex_index * e->stride);
5293 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5294 gl_info->gl_ops.gl.p_glEnableClientState(GL_COLOR_ARRAY);
5295 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5298 else
5300 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
5301 checkGLcall("glColor4f(1, 1, 1, 1)");
5304 /* Specular colour */
5305 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
5307 TRACE("Setting specular colour.\n");
5309 e = &si->elements[WINED3D_FFP_SPECULAR];
5311 if (gl_info->supported[EXT_SECONDARY_COLOR])
5313 GLint format;
5314 GLenum type;
5316 format_gl = wined3d_format_gl(e->format);
5317 type = format_gl->vtx_type;
5318 format = format_gl->vtx_format;
5320 if (current_bo != e->data.buffer_object)
5322 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, e->data.buffer_object));
5323 checkGLcall("glBindBuffer");
5324 current_bo = e->data.buffer_object;
5327 if (format != 4 || (gl_info->quirks & WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA))
5329 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5330 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5331 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5332 * 4 component secondary colors use it
5334 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format, type, e->stride,
5335 e->data.addr + state->load_base_vertex_index * e->stride);
5336 GL_EXTCALL(glSecondaryColorPointerEXT(format, type, e->stride,
5337 e->data.addr + state->load_base_vertex_index * e->stride));
5338 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5340 else
5342 switch (type)
5344 case GL_UNSIGNED_BYTE:
5345 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e->stride,
5346 e->data.addr + state->load_base_vertex_index * e->stride);
5347 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, e->stride,
5348 e->data.addr + state->load_base_vertex_index * e->stride));
5349 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5350 break;
5352 default:
5353 FIXME("Add 4 component specular colour pointers for type %#x.\n", type);
5354 /* Make sure that the right colour component is dropped. */
5355 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type, e->stride,
5356 e->data.addr + state->load_base_vertex_index * e->stride);
5357 GL_EXTCALL(glSecondaryColorPointerEXT(3, type, e->stride,
5358 e->data.addr + state->load_base_vertex_index * e->stride));
5359 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5362 gl_info->gl_ops.gl.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5363 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5365 else
5367 WARN("Specular colour is not supported in this GL implementation.\n");
5370 else
5372 if (gl_info->supported[EXT_SECONDARY_COLOR])
5374 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
5375 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5377 else
5379 WARN("Specular colour is not supported in this GL implementation.\n");
5383 /* Texture coordinates */
5384 wined3d_context_gl_load_tex_coords(context_gl, si, &current_bo, state);
5387 static void wined3d_context_gl_unload_numbered_array(struct wined3d_context_gl *context_gl, unsigned int i)
5389 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5391 GL_EXTCALL(glDisableVertexAttribArray(i));
5392 checkGLcall("glDisableVertexAttribArray");
5393 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5394 GL_EXTCALL(glVertexAttribDivisor(i, 0));
5396 context_gl->c.numbered_array_mask &= ~(1u << i);
5399 static void wined3d_context_gl_unload_numbered_arrays(struct wined3d_context_gl *context_gl)
5401 uint32_t mask = context_gl->c.numbered_array_mask;
5402 unsigned int i;
5404 while (mask)
5406 i = wined3d_bit_scan(&mask);
5407 wined3d_context_gl_unload_numbered_array(context_gl, i);
5411 static void wined3d_context_gl_load_numbered_arrays(struct wined3d_context_gl *context_gl,
5412 const struct wined3d_stream_info *stream_info, const struct wined3d_state *state)
5414 struct wined3d_context *context = &context_gl->c;
5415 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5416 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5417 GLuint current_bo;
5418 unsigned int i;
5420 /* Default to no instancing. */
5421 context->instance_count = 0;
5422 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5424 for (i = 0; i < MAX_ATTRIBS; ++i)
5426 const struct wined3d_stream_info_element *element = &stream_info->elements[i];
5427 const struct wined3d_stream_state *stream;
5428 const struct wined3d_format_gl *format_gl;
5430 if (!(stream_info->use_map & (1u << i)))
5432 if (context->numbered_array_mask & (1u << i))
5433 wined3d_context_gl_unload_numbered_array(context_gl, i);
5434 if (!use_vs(state) && i == WINED3D_FFP_DIFFUSE)
5436 if (!(context_gl->default_attrib_value_set & (1u << i)) || !context_gl->diffuse_attrib_to_1)
5438 GL_EXTCALL(glVertexAttrib4f(i, 1.0f, 1.0f, 1.0f, 1.0f));
5439 context_gl->diffuse_attrib_to_1 = 1;
5442 else
5444 if (!(context_gl->default_attrib_value_set & (1u << i)))
5446 GL_EXTCALL(glVertexAttrib4f(i, 0.0f, 0.0f, 0.0f, 0.0f));
5447 if (i == WINED3D_FFP_DIFFUSE)
5448 context_gl->diffuse_attrib_to_1 = 0;
5451 context_gl->default_attrib_value_set |= 1u << i;
5452 continue;
5455 format_gl = wined3d_format_gl(element->format);
5456 stream = &state->streams[element->stream_idx];
5458 if ((stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA) && !context->instance_count)
5459 context->instance_count = state->streams[0].frequency;
5461 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5463 GL_EXTCALL(glVertexAttribDivisor(i, element->divisor));
5465 else if (element->divisor)
5467 /* Unload instanced arrays, they will be loaded using immediate
5468 * mode instead. */
5469 if (context->numbered_array_mask & (1u << i))
5470 wined3d_context_gl_unload_numbered_array(context_gl, i);
5471 context_gl->default_attrib_value_set &= ~(1u << i);
5472 continue;
5475 TRACE("Loading array %u %s.\n", i, debug_bo_address(&element->data));
5477 if (element->stride)
5479 DWORD format_flags = format_gl->f.flags[WINED3D_GL_RES_TYPE_BUFFER];
5481 if (current_bo != element->data.buffer_object)
5483 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, element->data.buffer_object));
5484 checkGLcall("glBindBuffer");
5485 current_bo = element->data.buffer_object;
5487 /* Use the VBO to find out if a vertex buffer exists, not the vb
5488 * pointer. vb can point to a user pointer data blob. In that case
5489 * current_bo will be 0. If there is a vertex buffer but no vbo we
5490 * won't be load converted attributes anyway. */
5491 if (vs && vs->reg_maps.shader_version.major >= 4 && (format_flags & WINED3DFMT_FLAG_INTEGER))
5493 GL_EXTCALL(glVertexAttribIPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5494 element->stride, element->data.addr + state->load_base_vertex_index * element->stride));
5496 else
5498 GL_EXTCALL(glVertexAttribPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5499 !!(format_flags & WINED3DFMT_FLAG_NORMALISED), element->stride,
5500 element->data.addr + state->load_base_vertex_index * element->stride));
5503 if (!(context->numbered_array_mask & (1u << i)))
5505 GL_EXTCALL(glEnableVertexAttribArray(i));
5506 context->numbered_array_mask |= (1u << i);
5509 else
5511 /* Stride = 0 means always the same values.
5512 * glVertexAttribPointer() doesn't do that. Instead disable the
5513 * pointer and set up the attribute statically. But we have to
5514 * figure out the system memory address. */
5515 const BYTE *ptr = element->data.addr;
5516 if (element->data.buffer_object)
5517 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(stream->buffer, context);
5519 if (context->numbered_array_mask & (1u << i))
5520 wined3d_context_gl_unload_numbered_array(context_gl, i);
5522 switch (format_gl->f.id)
5524 case WINED3DFMT_R32_FLOAT:
5525 GL_EXTCALL(glVertexAttrib1fv(i, (const GLfloat *)ptr));
5526 break;
5527 case WINED3DFMT_R32G32_FLOAT:
5528 GL_EXTCALL(glVertexAttrib2fv(i, (const GLfloat *)ptr));
5529 break;
5530 case WINED3DFMT_R32G32B32_FLOAT:
5531 GL_EXTCALL(glVertexAttrib3fv(i, (const GLfloat *)ptr));
5532 break;
5533 case WINED3DFMT_R32G32B32A32_FLOAT:
5534 GL_EXTCALL(glVertexAttrib4fv(i, (const GLfloat *)ptr));
5535 break;
5536 case WINED3DFMT_R8G8B8A8_UINT:
5537 GL_EXTCALL(glVertexAttrib4ubv(i, ptr));
5538 break;
5539 case WINED3DFMT_B8G8R8A8_UNORM:
5540 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
5542 const DWORD *src = (const DWORD *)ptr;
5543 DWORD c = *src & 0xff00ff00u;
5544 c |= (*src & 0xff0000u) >> 16;
5545 c |= (*src & 0xffu) << 16;
5546 GL_EXTCALL(glVertexAttrib4Nubv(i, (GLubyte *)&c));
5547 break;
5549 /* else fallthrough */
5550 case WINED3DFMT_R8G8B8A8_UNORM:
5551 GL_EXTCALL(glVertexAttrib4Nubv(i, ptr));
5552 break;
5553 case WINED3DFMT_R16G16_SINT:
5554 GL_EXTCALL(glVertexAttrib2sv(i, (const GLshort *)ptr));
5555 break;
5556 case WINED3DFMT_R16G16B16A16_SINT:
5557 GL_EXTCALL(glVertexAttrib4sv(i, (const GLshort *)ptr));
5558 break;
5559 case WINED3DFMT_R16G16_SNORM:
5561 const GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
5562 GL_EXTCALL(glVertexAttrib4Nsv(i, s));
5563 break;
5565 case WINED3DFMT_R16G16_UNORM:
5567 const GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
5568 GL_EXTCALL(glVertexAttrib4Nusv(i, s));
5569 break;
5571 case WINED3DFMT_R16G16B16A16_SNORM:
5572 GL_EXTCALL(glVertexAttrib4Nsv(i, (const GLshort *)ptr));
5573 break;
5574 case WINED3DFMT_R16G16B16A16_UNORM:
5575 GL_EXTCALL(glVertexAttrib4Nusv(i, (const GLushort *)ptr));
5576 break;
5577 case WINED3DFMT_R10G10B10X2_UINT:
5578 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5579 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5580 break;
5581 case WINED3DFMT_R10G10B10X2_SNORM:
5582 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5583 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5584 break;
5585 case WINED3DFMT_R16G16_FLOAT:
5586 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5588 /* Not supported by GL_ARB_half_float_vertex. */
5589 GL_EXTCALL(glVertexAttrib2hvNV(i, (const GLhalfNV *)ptr));
5591 else
5593 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5594 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5595 GL_EXTCALL(glVertexAttrib2f(i, x, y));
5597 break;
5598 case WINED3DFMT_R16G16B16A16_FLOAT:
5599 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5601 /* Not supported by GL_ARB_half_float_vertex. */
5602 GL_EXTCALL(glVertexAttrib4hvNV(i, (const GLhalfNV *)ptr));
5604 else
5606 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5607 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5608 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
5609 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
5610 GL_EXTCALL(glVertexAttrib4f(i, x, y, z, w));
5612 break;
5613 default:
5614 ERR("Unexpected declaration in stride 0 attributes.\n");
5615 break;
5618 context_gl->default_attrib_value_set &= ~(1u << i);
5621 checkGLcall("Loading numbered arrays");
5624 void wined3d_context_gl_update_stream_sources(struct wined3d_context_gl *context_gl,
5625 const struct wined3d_state *state)
5627 if (context_gl->c.use_immediate_mode_draw)
5628 return;
5630 wined3d_context_gl_unload_vertex_data(context_gl);
5631 if (context_gl->c.d3d_info->ffp_generic_attributes || use_vs(state))
5633 TRACE("Loading numbered arrays.\n");
5634 wined3d_context_gl_load_numbered_arrays(context_gl, &context_gl->c.stream_info, state);
5635 return;
5638 TRACE("Loading named arrays.\n");
5639 wined3d_context_gl_unload_numbered_arrays(context_gl);
5640 wined3d_context_gl_load_vertex_data(context_gl, &context_gl->c.stream_info, state);
5641 context_gl->c.namedArraysLoaded = TRUE;
5644 static void apply_texture_blit_state(const struct wined3d_gl_info *gl_info, struct gl_texture *texture,
5645 GLenum target, unsigned int level, enum wined3d_texture_filter_type filter)
5647 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
5648 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
5649 wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
5650 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
5651 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
5652 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
5653 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
5654 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, level);
5656 /* We changed the filtering settings on the texture. Make sure they get
5657 * reset on subsequent draws. */
5658 texture->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
5659 texture->sampler_desc.min_filter = WINED3D_TEXF_POINT;
5660 texture->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
5661 texture->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
5662 texture->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
5663 texture->sampler_desc.srgb_decode = FALSE;
5664 texture->base_level = level;
5667 /* Context activation is done by the caller. */
5668 void wined3d_context_gl_draw_shaded_quad(struct wined3d_context_gl *context_gl, struct wined3d_texture_gl *texture_gl,
5669 unsigned int sub_resource_idx, const RECT *src_rect, const RECT *dst_rect,
5670 enum wined3d_texture_filter_type filter)
5672 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5673 struct wined3d_blt_info info;
5674 unsigned int level, w, h, i;
5675 SIZE dst_size;
5676 struct blit_vertex
5678 float x, y;
5679 struct wined3d_vec3 texcoord;
5681 quad[4];
5683 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5685 level = sub_resource_idx % texture_gl->t.level_count;
5686 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5687 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5688 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5690 wined3d_context_gl_get_rt_size(context_gl, &dst_size);
5691 w = dst_size.cx;
5692 h = dst_size.cy;
5694 quad[0].x = dst_rect->left * 2.0f / w - 1.0f;
5695 quad[0].y = dst_rect->top * 2.0f / h - 1.0f;
5696 quad[0].texcoord = info.texcoords[0];
5698 quad[1].x = dst_rect->right * 2.0f / w - 1.0f;
5699 quad[1].y = dst_rect->top * 2.0f / h - 1.0f;
5700 quad[1].texcoord = info.texcoords[1];
5702 quad[2].x = dst_rect->left * 2.0f / w - 1.0f;
5703 quad[2].y = dst_rect->bottom * 2.0f / h - 1.0f;
5704 quad[2].texcoord = info.texcoords[2];
5706 quad[3].x = dst_rect->right * 2.0f / w - 1.0f;
5707 quad[3].y = dst_rect->bottom * 2.0f / h - 1.0f;
5708 quad[3].texcoord = info.texcoords[3];
5710 /* Draw a quad. */
5711 if (gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
5713 if (!context_gl->blit_vbo)
5714 GL_EXTCALL(glGenBuffers(1, &context_gl->blit_vbo));
5715 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, context_gl->blit_vbo));
5717 wined3d_context_gl_unload_vertex_data(context_gl);
5718 wined3d_context_gl_unload_numbered_arrays(context_gl);
5720 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STREAM_DRAW));
5721 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT, FALSE, sizeof(*quad), NULL));
5722 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT, FALSE, sizeof(*quad),
5723 (void *)FIELD_OFFSET(struct blit_vertex, texcoord)));
5725 GL_EXTCALL(glEnableVertexAttribArray(0));
5726 GL_EXTCALL(glEnableVertexAttribArray(1));
5728 gl_info->gl_ops.gl.p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
5730 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
5731 GL_EXTCALL(glDisableVertexAttribArray(1));
5732 GL_EXTCALL(glDisableVertexAttribArray(0));
5734 else
5736 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5738 for (i = 0; i < ARRAY_SIZE(quad); ++i)
5740 GL_EXTCALL(glVertexAttrib3fv(1, &quad[i].texcoord.x));
5741 GL_EXTCALL(glVertexAttrib2fv(0, &quad[i].x));
5744 gl_info->gl_ops.gl.p_glEnd();
5746 checkGLcall("draw");
5748 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5749 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);
5752 /* Context activation is done by the caller. */
5753 void wined3d_context_gl_draw_textured_quad(struct wined3d_context_gl *context_gl,
5754 struct wined3d_texture_gl *texture_gl, unsigned int sub_resource_idx,
5755 const RECT *src_rect, const RECT *dst_rect, enum wined3d_texture_filter_type filter)
5757 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5758 struct wined3d_blt_info info;
5759 unsigned int level;
5761 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5763 gl_info->gl_ops.gl.p_glEnable(info.bind_target);
5764 checkGLcall("glEnable(bind_target)");
5766 level = sub_resource_idx % texture_gl->t.level_count;
5767 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5768 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5769 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5770 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
5771 checkGLcall("glTexEnvi");
5773 /* Draw a quad. */
5774 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5775 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[0].x);
5776 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->top);
5778 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[1].x);
5779 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->top);
5781 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[2].x);
5782 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->bottom);
5784 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[3].x);
5785 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->bottom);
5786 gl_info->gl_ops.gl.p_glEnd();
5788 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
5789 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);