win32u: Move NtUserDrawCaptionTemp implementation from user32.
[wine.git] / dlls / wined3d / context_gl.c
blob5292297bb850878a95349daa0b4d497b63391984
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 "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
31 WINE_DECLARE_DEBUG_CHANNEL(d3d_sync);
33 #define WINED3D_MAX_FBO_ENTRIES 64
34 #define WINED3D_ALL_LAYERS (~0u)
36 static DWORD wined3d_context_tls_idx;
38 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
39 * actually have the same values in GL and D3D. */
40 static GLenum gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
42 switch (primitive_type)
44 case WINED3D_PT_POINTLIST:
45 return GL_POINTS;
47 case WINED3D_PT_LINELIST:
48 return GL_LINES;
50 case WINED3D_PT_LINESTRIP:
51 return GL_LINE_STRIP;
53 case WINED3D_PT_TRIANGLELIST:
54 return GL_TRIANGLES;
56 case WINED3D_PT_TRIANGLESTRIP:
57 return GL_TRIANGLE_STRIP;
59 case WINED3D_PT_TRIANGLEFAN:
60 return GL_TRIANGLE_FAN;
62 case WINED3D_PT_LINELIST_ADJ:
63 return GL_LINES_ADJACENCY_ARB;
65 case WINED3D_PT_LINESTRIP_ADJ:
66 return GL_LINE_STRIP_ADJACENCY_ARB;
68 case WINED3D_PT_TRIANGLELIST_ADJ:
69 return GL_TRIANGLES_ADJACENCY_ARB;
71 case WINED3D_PT_TRIANGLESTRIP_ADJ:
72 return GL_TRIANGLE_STRIP_ADJACENCY_ARB;
74 case WINED3D_PT_PATCH:
75 return GL_PATCHES;
77 default:
78 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
79 case WINED3D_PT_UNDEFINED:
80 return ~0u;
84 /* FBO helper functions */
86 /* Context activation is done by the caller. */
87 static void wined3d_context_gl_bind_fbo(struct wined3d_context_gl *context_gl, GLenum target, GLuint fbo)
89 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
91 TRACE("context_gl %p, target %#x, fbo %u.\n", context_gl, target, fbo);
93 switch (target)
95 case GL_READ_FRAMEBUFFER:
96 if (context_gl->fbo_read_binding == fbo)
97 return;
98 context_gl->fbo_read_binding = fbo;
99 break;
101 case GL_DRAW_FRAMEBUFFER:
102 if (context_gl->fbo_draw_binding == fbo)
103 return;
104 context_gl->fbo_draw_binding = fbo;
105 break;
107 case GL_FRAMEBUFFER:
108 if (context_gl->fbo_read_binding == fbo
109 && context_gl->fbo_draw_binding == fbo)
110 return;
111 context_gl->fbo_read_binding = fbo;
112 context_gl->fbo_draw_binding = fbo;
113 break;
115 default:
116 FIXME("Unhandled target %#x.\n", target);
117 break;
120 gl_info->fbo_ops.glBindFramebuffer(target, fbo);
121 checkGLcall("glBindFramebuffer()");
124 /* Context activation is done by the caller. */
125 static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info, GLenum target)
127 unsigned int i;
129 for (i = 0; i < gl_info->limits.buffers; ++i)
131 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
132 checkGLcall("glFramebufferTexture2D()");
134 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
135 checkGLcall("glFramebufferTexture2D()");
137 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
138 checkGLcall("glFramebufferTexture2D()");
141 /* Context activation is done by the caller. */
142 static void wined3d_context_gl_destroy_fbo(struct wined3d_context_gl *context_gl, GLuint fbo)
144 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
146 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, fbo);
147 context_clean_fbo_attachments(gl_info, GL_FRAMEBUFFER);
148 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
150 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
151 checkGLcall("glDeleteFramebuffers()");
154 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info *gl_info,
155 GLenum fbo_target, DWORD flags, GLuint rb)
157 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
159 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
160 checkGLcall("glFramebufferRenderbuffer()");
163 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
165 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
166 checkGLcall("glFramebufferRenderbuffer()");
170 static void wined3d_context_gl_attach_gl_texture_fbo(struct wined3d_context_gl *context_gl,
171 GLenum fbo_target, GLenum attachment, const struct wined3d_fbo_resource *resource)
173 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
175 if (!resource)
177 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment, GL_TEXTURE_2D, 0, 0);
179 else if (resource->layer == WINED3D_ALL_LAYERS)
181 if (!gl_info->fbo_ops.glFramebufferTexture)
183 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
184 return;
187 gl_info->fbo_ops.glFramebufferTexture(fbo_target, attachment,
188 resource->object, resource->level);
190 else if (resource->target == GL_TEXTURE_1D_ARRAY || resource->target == GL_TEXTURE_2D_ARRAY
191 || resource->target == GL_TEXTURE_3D)
193 if (!gl_info->fbo_ops.glFramebufferTextureLayer)
195 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
196 return;
199 gl_info->fbo_ops.glFramebufferTextureLayer(fbo_target, attachment,
200 resource->object, resource->level, resource->layer);
202 else if (resource->target == GL_TEXTURE_1D)
204 gl_info->fbo_ops.glFramebufferTexture1D(fbo_target, attachment,
205 resource->target, resource->object, resource->level);
207 else
209 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment,
210 resource->target, resource->object, resource->level);
212 checkGLcall("attach texture to fbo");
215 /* Context activation is done by the caller. */
216 static void wined3d_context_gl_attach_depth_stencil_fbo(struct wined3d_context_gl *context_gl,
217 GLenum fbo_target, const struct wined3d_fbo_resource *resource, BOOL rb_namespace,
218 uint32_t flags)
220 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
222 if (resource->object)
224 TRACE("Attach depth stencil %u.\n", resource->object);
226 if (rb_namespace)
228 context_attach_depth_stencil_rb(gl_info, fbo_target,
229 flags, resource->object);
231 else
233 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
234 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_DEPTH_ATTACHMENT, resource);
236 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
237 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_STENCIL_ATTACHMENT, resource);
240 if (!(flags & WINED3D_FBO_ENTRY_FLAG_DEPTH))
241 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
243 if (!(flags & WINED3D_FBO_ENTRY_FLAG_STENCIL))
244 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
246 else
248 TRACE("Attach depth stencil 0.\n");
250 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
251 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
255 /* Context activation is done by the caller. */
256 static void wined3d_context_gl_attach_surface_fbo(struct wined3d_context_gl *context_gl,
257 GLenum fbo_target, unsigned int idx, const struct wined3d_fbo_resource *resource, BOOL rb_namespace)
259 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
261 TRACE("Attach GL object %u to %u.\n", resource->object, idx);
263 if (resource->object)
265 if (rb_namespace)
267 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
268 GL_RENDERBUFFER, resource->object);
269 checkGLcall("glFramebufferRenderbuffer()");
271 else
273 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_COLOR_ATTACHMENT0 + idx, resource);
276 else
278 wined3d_context_gl_attach_gl_texture_fbo(context_gl, fbo_target, GL_COLOR_ATTACHMENT0 + idx, NULL);
282 static void context_dump_fbo_attachment(const struct wined3d_gl_info *gl_info, GLenum target,
283 GLenum attachment)
285 static const struct
287 GLenum target;
288 GLenum binding;
289 const char *str;
290 enum wined3d_gl_extension extension;
292 texture_type[] =
294 {GL_TEXTURE_1D, GL_TEXTURE_BINDING_1D, "1d", WINED3D_GL_EXT_NONE},
295 {GL_TEXTURE_1D_ARRAY, GL_TEXTURE_BINDING_1D_ARRAY, "1d-array", EXT_TEXTURE_ARRAY},
296 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, "2d", WINED3D_GL_EXT_NONE},
297 {GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_BINDING_RECTANGLE_ARB, "rectangle", ARB_TEXTURE_RECTANGLE},
298 {GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BINDING_2D_ARRAY, "2d-array" , EXT_TEXTURE_ARRAY},
299 {GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BINDING_CUBE_MAP, "cube", ARB_TEXTURE_CUBE_MAP},
300 {GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_BINDING_2D_MULTISAMPLE, "2d-ms", ARB_TEXTURE_MULTISAMPLE},
301 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY, "2d-array-ms", ARB_TEXTURE_MULTISAMPLE},
304 GLint type, name, samples, width, height, old_texture, level, face, fmt, tex_target;
305 const char *tex_type_str = NULL;
306 unsigned int i;
308 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
309 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &name);
310 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
311 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &type);
313 if (type == GL_RENDERBUFFER)
315 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, name);
316 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
317 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
318 if (gl_info->limits.samples > 1)
319 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
320 else
321 samples = 1;
322 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &fmt);
323 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
324 debug_fboattachment(attachment), name, width, height, samples, fmt);
326 else if (type == GL_TEXTURE)
328 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
329 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL, &level);
330 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
331 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE, &face);
333 if (gl_info->gl_ops.ext.p_glGetTextureParameteriv)
335 GL_EXTCALL(glGetTextureParameteriv(name, GL_TEXTURE_TARGET, &tex_target));
337 for (i = 0; i < ARRAY_SIZE(texture_type); ++i)
339 if (texture_type[i].target == tex_target)
341 tex_type_str = texture_type[i].str;
342 break;
345 if (i == ARRAY_SIZE(texture_type))
346 tex_type_str = wine_dbg_sprintf("%#x", tex_target);
348 else if (face)
350 gl_info->gl_ops.gl.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &old_texture);
351 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, name);
353 tex_target = GL_TEXTURE_CUBE_MAP;
354 tex_type_str = "cube";
356 else
358 for (i = 0; i < ARRAY_SIZE(texture_type); ++i)
360 if (!gl_info->supported[texture_type[i].extension])
361 continue;
363 gl_info->gl_ops.gl.p_glGetIntegerv(texture_type[i].binding, &old_texture);
364 while (gl_info->gl_ops.gl.p_glGetError());
366 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, name);
367 if (!gl_info->gl_ops.gl.p_glGetError())
369 tex_target = texture_type[i].target;
370 tex_type_str = texture_type[i].str;
371 break;
373 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, old_texture);
376 if (!tex_type_str)
378 FIXME("Cannot find type of texture %d.\n", name);
379 return;
383 if (gl_info->gl_ops.ext.p_glGetTextureParameteriv)
385 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt));
386 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_WIDTH, &width));
387 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_HEIGHT, &height));
388 GL_EXTCALL(glGetTextureLevelParameteriv(name, level, GL_TEXTURE_SAMPLES, &samples));
390 else
392 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
393 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_WIDTH, &width);
394 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_HEIGHT, &height);
395 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
396 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_SAMPLES, &samples);
397 else
398 samples = 1;
400 gl_info->gl_ops.gl.p_glBindTexture(tex_target, old_texture);
403 FIXME(" %s: %s texture %d, %dx%d, %d samples, format %#x.\n",
404 debug_fboattachment(attachment), tex_type_str, name, width, height, samples, fmt);
406 else if (type == GL_NONE)
408 FIXME(" %s: NONE.\n", debug_fboattachment(attachment));
410 else
412 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment), type);
415 checkGLcall("dump FBO attachment");
418 /* Context activation is done by the caller. */
419 void wined3d_context_gl_check_fbo_status(const struct wined3d_context_gl *context_gl, GLenum target)
421 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
422 GLenum status;
424 if (!FIXME_ON(d3d))
425 return;
427 status = gl_info->fbo_ops.glCheckFramebufferStatus(target);
428 if (status == GL_FRAMEBUFFER_COMPLETE)
430 TRACE("FBO complete.\n");
432 else
434 unsigned int i;
436 FIXME("FBO status %s (%#x).\n", debug_fbostatus(status), status);
438 if (!context_gl->current_fbo)
440 ERR("FBO 0 is incomplete, driver bug?\n");
441 return;
444 context_dump_fbo_attachment(gl_info, target, GL_DEPTH_ATTACHMENT);
445 context_dump_fbo_attachment(gl_info, target, GL_STENCIL_ATTACHMENT);
447 for (i = 0; i < gl_info->limits.buffers; ++i)
448 context_dump_fbo_attachment(gl_info, target, GL_COLOR_ATTACHMENT0 + i);
452 static inline DWORD context_generate_rt_mask(GLenum buffer)
454 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
455 return buffer ? (1u << 31) | buffer : 0;
458 static inline DWORD context_generate_rt_mask_from_resource(struct wined3d_resource *resource)
460 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
462 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
463 return 0;
466 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource));
469 /* Context activation is done by the caller. */
470 static void wined3d_context_gl_set_draw_buffer(struct wined3d_context_gl *context_gl, GLenum buffer)
472 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
473 struct fbo_entry *current_fbo = context_gl->current_fbo;
474 uint32_t new_mask = context_generate_rt_mask(buffer);
475 uint32_t *current_mask;
477 current_mask = current_fbo ? &current_fbo->rt_mask : &context_gl->draw_buffers_mask;
478 if (new_mask == *current_mask)
479 return;
481 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
482 checkGLcall("glDrawBuffer()");
484 *current_mask = new_mask;
487 static inline void wined3d_context_gl_set_fbo_key_for_render_target(const struct wined3d_context_gl *context_gl,
488 struct wined3d_fbo_entry_key *key, unsigned int idx, const struct wined3d_rendertarget_info *render_target,
489 DWORD location)
491 unsigned int sub_resource_idx = render_target->sub_resource_idx;
492 struct wined3d_resource *resource = render_target->resource;
493 struct wined3d_texture_gl *texture_gl;
495 if (!resource || resource->format->id == WINED3DFMT_NULL || resource->type == WINED3D_RTYPE_BUFFER)
497 if (resource && resource->type == WINED3D_RTYPE_BUFFER)
498 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
499 key->objects[idx].object = 0;
500 key->objects[idx].target = 0;
501 key->objects[idx].level = key->objects[idx].layer = 0;
502 return;
505 if (render_target->gl_view.name)
507 key->objects[idx].object = render_target->gl_view.name;
508 key->objects[idx].target = render_target->gl_view.target;
509 key->objects[idx].level = 0;
510 key->objects[idx].layer = WINED3D_ALL_LAYERS;
511 return;
514 texture_gl = wined3d_texture_gl(wined3d_texture_from_resource(resource));
515 if (texture_gl->current_renderbuffer)
517 key->objects[idx].object = texture_gl->current_renderbuffer->id;
518 key->objects[idx].target = 0;
519 key->objects[idx].level = key->objects[idx].layer = 0;
520 key->rb_namespace |= 1 << idx;
521 return;
524 key->objects[idx].target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
525 key->objects[idx].level = sub_resource_idx % texture_gl->t.level_count;
526 key->objects[idx].layer = sub_resource_idx / texture_gl->t.level_count;
528 if (render_target->layer_count != 1)
529 key->objects[idx].layer = WINED3D_ALL_LAYERS;
531 switch (location)
533 case WINED3D_LOCATION_TEXTURE_RGB:
534 key->objects[idx].object = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, FALSE);
535 break;
537 case WINED3D_LOCATION_TEXTURE_SRGB:
538 key->objects[idx].object = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, TRUE);
539 break;
541 case WINED3D_LOCATION_RB_MULTISAMPLE:
542 key->objects[idx].object = texture_gl->rb_multisample;
543 key->objects[idx].target = 0;
544 key->objects[idx].level = key->objects[idx].layer = 0;
545 key->rb_namespace |= 1 << idx;
546 break;
548 case WINED3D_LOCATION_RB_RESOLVED:
549 key->objects[idx].object = texture_gl->rb_resolved;
550 key->objects[idx].target = 0;
551 key->objects[idx].level = key->objects[idx].layer = 0;
552 key->rb_namespace |= 1 << idx;
553 break;
557 static void wined3d_context_gl_generate_fbo_key(const struct wined3d_context_gl *context_gl,
558 struct wined3d_fbo_entry_key *key, const struct wined3d_rendertarget_info *render_targets,
559 const struct wined3d_rendertarget_info *depth_stencil, DWORD color_location, DWORD ds_location)
561 unsigned int buffers = context_gl->gl_info->limits.buffers;
562 unsigned int i;
564 key->rb_namespace = 0;
565 wined3d_context_gl_set_fbo_key_for_render_target(context_gl, key, 0, depth_stencil, ds_location);
567 for (i = 0; i < buffers; ++i)
568 wined3d_context_gl_set_fbo_key_for_render_target(context_gl, key, i + 1, &render_targets[i], color_location);
570 memset(&key->objects[buffers + 1], 0, (ARRAY_SIZE(key->objects) - buffers - 1) * sizeof(*key->objects));
573 static struct fbo_entry *wined3d_context_gl_create_fbo_entry(const struct wined3d_context_gl *context_gl,
574 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
575 DWORD color_location, DWORD ds_location)
577 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
578 struct fbo_entry *entry;
580 entry = heap_alloc(sizeof(*entry));
581 wined3d_context_gl_generate_fbo_key(context_gl, &entry->key,
582 render_targets, depth_stencil, color_location, ds_location);
583 entry->flags = 0;
584 if (depth_stencil->resource)
586 if (depth_stencil->resource->format->depth_size)
587 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
588 if (depth_stencil->resource->format->stencil_size)
589 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
591 entry->rt_mask = context_generate_rt_mask(GL_COLOR_ATTACHMENT0);
592 gl_info->fbo_ops.glGenFramebuffers(1, &entry->id);
593 checkGLcall("glGenFramebuffers()");
594 TRACE("Created FBO %u.\n", entry->id);
596 return entry;
599 /* Context activation is done by the caller. */
600 static void wined3d_context_gl_reuse_fbo_entry(struct wined3d_context_gl *context_gl, GLenum target,
601 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
602 DWORD color_location, DWORD ds_location, struct fbo_entry *entry)
604 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
606 wined3d_context_gl_bind_fbo(context_gl, target, entry->id);
607 context_clean_fbo_attachments(gl_info, target);
609 wined3d_context_gl_generate_fbo_key(context_gl, &entry->key,
610 render_targets, depth_stencil, color_location, ds_location);
611 entry->flags = 0;
612 if (depth_stencil->resource)
614 if (depth_stencil->resource->format->depth_size)
615 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
616 if (depth_stencil->resource->format->stencil_size)
617 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
621 /* Context activation is done by the caller. */
622 static void wined3d_context_gl_destroy_fbo_entry(struct wined3d_context_gl *context_gl, struct fbo_entry *entry)
624 if (entry->id)
626 TRACE("Destroy FBO %u.\n", entry->id);
627 wined3d_context_gl_destroy_fbo(context_gl, entry->id);
629 --context_gl->fbo_entry_count;
630 list_remove(&entry->entry);
631 heap_free(entry);
634 /* Context activation is done by the caller. */
635 static struct fbo_entry *wined3d_context_gl_find_fbo_entry(struct wined3d_context_gl *context_gl, GLenum target,
636 const struct wined3d_rendertarget_info *render_targets, const struct wined3d_rendertarget_info *depth_stencil,
637 DWORD color_location, DWORD ds_location)
639 static const struct wined3d_rendertarget_info ds_null = {{0}};
640 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
641 struct wined3d_texture *rt_texture, *ds_texture;
642 struct wined3d_fbo_entry_key fbo_key;
643 unsigned int i, ds_level, rt_level;
644 struct fbo_entry *entry;
646 if (depth_stencil->resource && depth_stencil->resource->type != WINED3D_RTYPE_BUFFER
647 && render_targets[0].resource && render_targets[0].resource->type != WINED3D_RTYPE_BUFFER
648 && render_targets[0].resource->format->id != WINED3DFMT_NULL)
650 rt_texture = wined3d_texture_from_resource(render_targets[0].resource);
651 rt_level = render_targets[0].sub_resource_idx % rt_texture->level_count;
652 ds_texture = wined3d_texture_from_resource(depth_stencil->resource);
653 ds_level = depth_stencil->sub_resource_idx % ds_texture->level_count;
655 if (wined3d_texture_get_level_width(ds_texture, ds_level)
656 < wined3d_texture_get_level_width(rt_texture, rt_level)
657 || wined3d_texture_get_level_height(ds_texture, ds_level)
658 < wined3d_texture_get_level_height(rt_texture, rt_level))
660 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
661 depth_stencil = &ds_null;
663 else if (ds_texture->resource.multisample_type != rt_texture->resource.multisample_type
664 || (ds_texture->resource.multisample_type
665 && ds_texture->resource.multisample_quality != rt_texture->resource.multisample_quality))
667 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
668 rt_texture->resource.multisample_type, rt_texture->resource.multisample_quality,
669 ds_texture->resource.multisample_type, ds_texture->resource.multisample_quality);
670 depth_stencil = &ds_null;
672 else if (depth_stencil->resource->type == WINED3D_RTYPE_TEXTURE_2D)
674 wined3d_texture_gl_set_compatible_renderbuffer(wined3d_texture_gl(ds_texture),
675 context_gl, ds_level, &render_targets[0]);
679 wined3d_context_gl_generate_fbo_key(context_gl, &fbo_key,
680 render_targets, depth_stencil, color_location, ds_location);
682 if (TRACE_ON(d3d))
684 struct wined3d_resource *resource;
685 unsigned int width, height;
686 const char *resource_type;
688 TRACE("Dumping FBO attachments:\n");
689 for (i = 0; i < gl_info->limits.buffers; ++i)
691 if ((resource = render_targets[i].resource))
693 if (resource->type == WINED3D_RTYPE_BUFFER)
695 width = resource->size;
696 height = 1;
697 resource_type = "buffer";
699 else
701 rt_texture = wined3d_texture_from_resource(resource);
702 rt_level = render_targets[i].sub_resource_idx % rt_texture->level_count;
703 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
704 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
705 resource_type = "texture";
708 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
709 i, resource, render_targets[i].sub_resource_idx, debug_d3dformat(resource->format->id),
710 fbo_key.rb_namespace & (1 << (i + 1)) ? "renderbuffer" : resource_type,
711 fbo_key.objects[i + 1].object, width, height, resource->multisample_type);
714 if ((resource = depth_stencil->resource))
716 if (resource->type == WINED3D_RTYPE_BUFFER)
718 width = resource->size;
719 height = 1;
720 resource_type = "buffer";
722 else
724 ds_texture = wined3d_texture_from_resource(resource);
725 ds_level = depth_stencil->sub_resource_idx % ds_texture->level_count;
726 width = wined3d_texture_get_level_pow2_width(ds_texture, ds_level);
727 height = wined3d_texture_get_level_pow2_height(ds_texture, ds_level);
728 resource_type = "texture";
731 TRACE(" Depth attachment: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
732 resource, depth_stencil->sub_resource_idx, debug_d3dformat(resource->format->id),
733 fbo_key.rb_namespace & (1 << 0) ? "renderbuffer" : resource_type,
734 fbo_key.objects[0].object, width, height, resource->multisample_type);
738 LIST_FOR_EACH_ENTRY(entry, &context_gl->fbo_list, struct fbo_entry, entry)
740 if (memcmp(&fbo_key, &entry->key, sizeof(fbo_key)))
741 continue;
743 list_remove(&entry->entry);
744 list_add_head(&context_gl->fbo_list, &entry->entry);
745 return entry;
748 if (context_gl->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
750 entry = wined3d_context_gl_create_fbo_entry(context_gl,
751 render_targets, depth_stencil, color_location, ds_location);
752 list_add_head(&context_gl->fbo_list, &entry->entry);
753 ++context_gl->fbo_entry_count;
755 else
757 entry = LIST_ENTRY(list_tail(&context_gl->fbo_list), struct fbo_entry, entry);
758 wined3d_context_gl_reuse_fbo_entry(context_gl, target, render_targets,
759 depth_stencil, color_location, ds_location, entry);
760 list_remove(&entry->entry);
761 list_add_head(&context_gl->fbo_list, &entry->entry);
764 return entry;
767 /* Context activation is done by the caller. */
768 static void wined3d_context_gl_apply_fbo_entry(struct wined3d_context_gl *context_gl,
769 GLenum target, struct fbo_entry *entry)
771 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
772 GLuint read_binding, draw_binding;
773 unsigned int i;
775 if (entry->flags & WINED3D_FBO_ENTRY_FLAG_ATTACHED)
777 wined3d_context_gl_bind_fbo(context_gl, target, entry->id);
778 return;
781 read_binding = context_gl->fbo_read_binding;
782 draw_binding = context_gl->fbo_draw_binding;
783 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, entry->id);
785 if (gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
787 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER,
788 GL_FRAMEBUFFER_DEFAULT_WIDTH, gl_info->limits.framebuffer_width));
789 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER,
790 GL_FRAMEBUFFER_DEFAULT_HEIGHT, gl_info->limits.framebuffer_height));
791 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_LAYERS, 1));
792 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES, 1));
795 /* Apply render targets */
796 for (i = 0; i < gl_info->limits.buffers; ++i)
798 wined3d_context_gl_attach_surface_fbo(context_gl, target, i,
799 &entry->key.objects[i + 1], entry->key.rb_namespace & (1 << (i + 1)));
802 wined3d_context_gl_attach_depth_stencil_fbo(context_gl, target,
803 &entry->key.objects[0], entry->key.rb_namespace & 0x1, entry->flags);
805 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
806 * GL contexts requirements. */
807 gl_info->gl_ops.gl.p_glReadBuffer(GL_NONE);
808 wined3d_context_gl_set_draw_buffer(context_gl, GL_NONE);
809 if (target != GL_FRAMEBUFFER)
811 if (target == GL_READ_FRAMEBUFFER)
812 wined3d_context_gl_bind_fbo(context_gl, GL_DRAW_FRAMEBUFFER, draw_binding);
813 else
814 wined3d_context_gl_bind_fbo(context_gl, GL_READ_FRAMEBUFFER, read_binding);
817 entry->flags |= WINED3D_FBO_ENTRY_FLAG_ATTACHED;
820 /* Context activation is done by the caller. */
821 static void wined3d_context_gl_apply_fbo_state(struct wined3d_context_gl *context_gl, GLenum target,
822 const struct wined3d_rendertarget_info *render_targets,
823 const struct wined3d_rendertarget_info *depth_stencil, DWORD color_location, DWORD ds_location)
825 struct fbo_entry *entry, *entry2;
827 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_destroy_list, struct fbo_entry, entry)
829 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
832 if (context_gl->rebind_fbo)
834 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
835 context_gl->rebind_fbo = FALSE;
838 if (color_location == WINED3D_LOCATION_DRAWABLE)
840 context_gl->current_fbo = NULL;
841 wined3d_context_gl_bind_fbo(context_gl, target, 0);
843 else
845 context_gl->current_fbo = wined3d_context_gl_find_fbo_entry(context_gl,
846 target, render_targets, depth_stencil, color_location, ds_location);
847 wined3d_context_gl_apply_fbo_entry(context_gl, target, context_gl->current_fbo);
851 /* Context activation is done by the caller. */
852 void wined3d_context_gl_apply_fbo_state_blit(struct wined3d_context_gl *context_gl, GLenum target,
853 struct wined3d_resource *rt, unsigned int rt_sub_resource_idx,
854 struct wined3d_resource *ds, unsigned int ds_sub_resource_idx, DWORD location)
856 struct wined3d_rendertarget_info ds_info = {{0}};
858 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
859 if (rt)
861 context_gl->blit_targets[0].resource = rt;
862 context_gl->blit_targets[0].sub_resource_idx = rt_sub_resource_idx;
863 context_gl->blit_targets[0].layer_count = 1;
866 if (ds)
868 ds_info.resource = ds;
869 ds_info.sub_resource_idx = ds_sub_resource_idx;
870 ds_info.layer_count = 1;
873 wined3d_context_gl_apply_fbo_state(context_gl, target, context_gl->blit_targets, &ds_info, location, location);
876 /* Context activation is done by the caller. */
877 void wined3d_context_gl_alloc_occlusion_query(struct wined3d_context_gl *context_gl,
878 struct wined3d_occlusion_query *query)
880 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
882 if (context_gl->free_occlusion_query_count)
884 query->id = context_gl->free_occlusion_queries[--context_gl->free_occlusion_query_count];
886 else
888 if (gl_info->supported[ARB_OCCLUSION_QUERY])
890 GL_EXTCALL(glGenQueries(1, &query->id));
891 checkGLcall("glGenQueries");
893 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context_gl);
895 else
897 WARN("Occlusion queries not supported, not allocating query id.\n");
898 query->id = 0;
902 query->context_gl = context_gl;
903 list_add_head(&context_gl->occlusion_queries, &query->entry);
906 void wined3d_context_gl_free_occlusion_query(struct wined3d_occlusion_query *query)
908 struct wined3d_context_gl *context_gl = query->context_gl;
910 list_remove(&query->entry);
911 query->context_gl = NULL;
913 if (!wined3d_array_reserve((void **)&context_gl->free_occlusion_queries,
914 &context_gl->free_occlusion_query_size, context_gl->free_occlusion_query_count + 1,
915 sizeof(*context_gl->free_occlusion_queries)))
917 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context_gl);
918 return;
921 context_gl->free_occlusion_queries[context_gl->free_occlusion_query_count++] = query->id;
924 /* Context activation is done by the caller. */
925 void wined3d_context_gl_alloc_fence(struct wined3d_context_gl *context_gl, struct wined3d_fence *fence)
927 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
929 if (context_gl->free_fence_count)
931 fence->object = context_gl->free_fences[--context_gl->free_fence_count];
933 else
935 if (gl_info->supported[ARB_SYNC])
937 /* Using ARB_sync, not much to do here. */
938 fence->object.sync = NULL;
939 TRACE("Allocated sync object in context %p.\n", context_gl);
941 else if (gl_info->supported[APPLE_FENCE])
943 GL_EXTCALL(glGenFencesAPPLE(1, &fence->object.id));
944 checkGLcall("glGenFencesAPPLE");
946 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context_gl);
948 else if(gl_info->supported[NV_FENCE])
950 GL_EXTCALL(glGenFencesNV(1, &fence->object.id));
951 checkGLcall("glGenFencesNV");
953 TRACE("Allocated fence %u in context %p.\n", fence->object.id, context_gl);
955 else
957 WARN("Fences not supported, not allocating fence.\n");
958 fence->object.id = 0;
962 fence->context_gl = context_gl;
963 list_add_head(&context_gl->fences, &fence->entry);
966 void wined3d_context_gl_free_fence(struct wined3d_fence *fence)
968 struct wined3d_context_gl *context_gl = fence->context_gl;
970 list_remove(&fence->entry);
971 fence->context_gl = NULL;
973 if (!wined3d_array_reserve((void **)&context_gl->free_fences,
974 &context_gl->free_fence_size, context_gl->free_fence_count + 1,
975 sizeof(*context_gl->free_fences)))
977 ERR("Failed to grow free list, leaking fence %u in context %p.\n", fence->object.id, context_gl);
978 return;
981 context_gl->free_fences[context_gl->free_fence_count++] = fence->object;
984 /* Context activation is done by the caller. */
985 void wined3d_context_gl_alloc_timestamp_query(struct wined3d_context_gl *context_gl,
986 struct wined3d_timestamp_query *query)
988 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
990 if (context_gl->free_timestamp_query_count)
992 query->id = context_gl->free_timestamp_queries[--context_gl->free_timestamp_query_count];
994 else
996 GL_EXTCALL(glGenQueries(1, &query->id));
997 checkGLcall("glGenQueries");
999 TRACE("Allocated timestamp query %u in context %p.\n", query->id, context_gl);
1002 query->context_gl = context_gl;
1003 list_add_head(&context_gl->timestamp_queries, &query->entry);
1006 void wined3d_context_gl_free_timestamp_query(struct wined3d_timestamp_query *query)
1008 struct wined3d_context_gl *context_gl = query->context_gl;
1010 list_remove(&query->entry);
1011 query->context_gl = NULL;
1013 if (!wined3d_array_reserve((void **)&context_gl->free_timestamp_queries,
1014 &context_gl->free_timestamp_query_size, context_gl->free_timestamp_query_count + 1,
1015 sizeof(*context_gl->free_timestamp_queries)))
1017 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context_gl);
1018 return;
1021 context_gl->free_timestamp_queries[context_gl->free_timestamp_query_count++] = query->id;
1024 void wined3d_context_gl_alloc_so_statistics_query(struct wined3d_context_gl *context_gl,
1025 struct wined3d_so_statistics_query *query)
1027 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1029 if (context_gl->free_so_statistics_query_count)
1031 query->u = context_gl->free_so_statistics_queries[--context_gl->free_so_statistics_query_count];
1033 else
1035 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
1036 checkGLcall("glGenQueries");
1038 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
1039 query->u.id[0], query->u.id[1], context_gl);
1042 query->context_gl = context_gl;
1043 list_add_head(&context_gl->so_statistics_queries, &query->entry);
1046 void wined3d_context_gl_free_so_statistics_query(struct wined3d_so_statistics_query *query)
1048 struct wined3d_context_gl *context_gl = query->context_gl;
1050 list_remove(&query->entry);
1051 query->context_gl = NULL;
1053 if (!wined3d_array_reserve((void **)&context_gl->free_so_statistics_queries,
1054 &context_gl->free_so_statistics_query_size, context_gl->free_so_statistics_query_count + 1,
1055 sizeof(*context_gl->free_so_statistics_queries)))
1057 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
1058 query->u.id[0], query->u.id[1], context_gl);
1059 return;
1062 context_gl->free_so_statistics_queries[context_gl->free_so_statistics_query_count++] = query->u;
1065 void wined3d_context_gl_alloc_pipeline_statistics_query(struct wined3d_context_gl *context_gl,
1066 struct wined3d_pipeline_statistics_query *query)
1068 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1070 if (context_gl->free_pipeline_statistics_query_count)
1072 query->u = context_gl->free_pipeline_statistics_queries[--context_gl->free_pipeline_statistics_query_count];
1074 else
1076 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
1077 checkGLcall("glGenQueries");
1080 query->context_gl = context_gl;
1081 list_add_head(&context_gl->pipeline_statistics_queries, &query->entry);
1084 void wined3d_context_gl_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query *query)
1086 struct wined3d_context_gl *context_gl = query->context_gl;
1088 list_remove(&query->entry);
1089 query->context_gl = NULL;
1091 if (!wined3d_array_reserve((void **)&context_gl->free_pipeline_statistics_queries,
1092 &context_gl->free_pipeline_statistics_query_size, context_gl->free_pipeline_statistics_query_count + 1,
1093 sizeof(*context_gl->free_pipeline_statistics_queries)))
1095 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context_gl);
1096 return;
1099 context_gl->free_pipeline_statistics_queries[context_gl->free_pipeline_statistics_query_count++] = query->u;
1102 typedef void (context_fbo_entry_func_t)(struct wined3d_context_gl *context_gl, struct fbo_entry *entry);
1104 static void wined3d_context_gl_enum_fbo_entries(const struct wined3d_device *device,
1105 GLuint name, BOOL rb_namespace, context_fbo_entry_func_t *callback)
1107 unsigned int i, j;
1109 for (i = 0; i < device->context_count; ++i)
1111 struct wined3d_context_gl *context_gl = wined3d_context_gl(device->contexts[i]);
1112 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1113 struct fbo_entry *entry, *entry2;
1115 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_list, struct fbo_entry, entry)
1117 for (j = 0; j < gl_info->limits.buffers + 1; ++j)
1119 if (entry->key.objects[j].object == name
1120 && !(entry->key.rb_namespace & (1 << j)) == !rb_namespace)
1122 callback(context_gl, entry);
1123 break;
1130 static void wined3d_context_gl_queue_fbo_entry_destruction(struct wined3d_context_gl *context_gl,
1131 struct fbo_entry *entry)
1133 list_remove(&entry->entry);
1134 list_add_head(&context_gl->fbo_destroy_list, &entry->entry);
1137 void context_gl_resource_released(struct wined3d_device *device, GLuint name, BOOL rb_namespace)
1139 wined3d_context_gl_enum_fbo_entries(device, name, rb_namespace,
1140 wined3d_context_gl_queue_fbo_entry_destruction);
1143 void wined3d_context_gl_texture_update(struct wined3d_context_gl *context_gl,
1144 const struct wined3d_texture_gl *texture_gl)
1146 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1147 struct fbo_entry *entry = context_gl->current_fbo;
1148 unsigned int i;
1150 if (!entry || context_gl->rebind_fbo)
1151 return;
1153 for (i = 0; i < gl_info->limits.buffers + 1; ++i)
1155 if (texture_gl->texture_rgb.name == entry->key.objects[i].object
1156 || texture_gl->texture_srgb.name == entry->key.objects[i].object)
1158 TRACE("Updated texture %p is bound as attachment %u to the current FBO.\n", texture_gl, i);
1159 context_gl->rebind_fbo = TRUE;
1160 return;
1165 static BOOL wined3d_context_gl_restore_pixel_format(struct wined3d_context_gl *context_gl)
1167 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1168 BOOL ret = FALSE;
1170 if (context_gl->restore_pf && IsWindow(context_gl->restore_pf_win))
1172 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1174 HDC dc = GetDCEx(context_gl->restore_pf_win, 0, DCX_USESTYLE | DCX_CACHE);
1175 if (dc)
1177 if (!(ret = GL_EXTCALL(wglSetPixelFormatWINE(dc, context_gl->restore_pf))))
1179 ERR("Failed to restore pixel format %d on window %p.\n",
1180 context_gl->restore_pf, context_gl->restore_pf_win);
1182 ReleaseDC(context_gl->restore_pf_win, dc);
1185 else
1187 ERR("Unable to restore pixel format %d on window %p.\n",
1188 context_gl->restore_pf, context_gl->restore_pf_win);
1192 context_gl->restore_pf = 0;
1193 context_gl->restore_pf_win = NULL;
1194 return ret;
1197 static BOOL wined3d_context_gl_set_pixel_format(struct wined3d_context_gl *context_gl)
1199 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1200 BOOL private = context_gl->dc_is_private;
1201 int format = context_gl->pixel_format;
1202 HDC dc = context_gl->dc;
1203 int current;
1204 HWND win;
1206 if (private && context_gl->dc_has_format)
1207 return TRUE;
1209 if (!private && WindowFromDC(dc) != context_gl->window)
1210 return FALSE;
1212 current = gl_info->gl_ops.wgl.p_wglGetPixelFormat(dc);
1213 if (current == format) goto success;
1215 /* By default WGL doesn't allow pixel format adjustments but we need it
1216 * here. For this reason there's a Wine specific wglSetPixelFormat()
1217 * which allows us to set the pixel format multiple times. Use it when we
1218 * can, because even though no pixel format may currently be set, the
1219 * application may try to set one later. */
1220 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1222 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
1224 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1225 format, dc);
1226 return FALSE;
1229 else if (current)
1231 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1232 * continue using the old format. There's a big chance that the old
1233 * format works although with a performance hit and perhaps rendering
1234 * errors. */
1235 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1236 format, dc, current);
1237 return TRUE;
1239 else if (!SetPixelFormat(dc, format, NULL))
1241 /* This may also happen if the dc belongs to a destroyed window. */
1242 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1243 format, dc, GetLastError());
1244 return FALSE;
1247 win = private ? NULL : WindowFromDC(dc);
1248 if (win != context_gl->restore_pf_win)
1249 wined3d_context_gl_restore_pixel_format(context_gl);
1250 context_gl->restore_pf = private ? 0 : current;
1251 context_gl->restore_pf_win = win;
1253 success:
1254 if (private)
1255 context_gl->dc_has_format = TRUE;
1256 return TRUE;
1259 static BOOL wined3d_context_gl_set_gl_context(struct wined3d_context_gl *context_gl)
1261 struct wined3d_swapchain_gl *swapchain_gl = wined3d_swapchain_gl(context_gl->c.swapchain);
1262 BOOL backup = FALSE;
1264 if (!wined3d_context_gl_set_pixel_format(context_gl))
1266 WARN("Failed to set pixel format %d on device context %p.\n",
1267 context_gl->pixel_format, context_gl->dc);
1268 backup = TRUE;
1271 if (backup || !wglMakeCurrent(context_gl->dc, context_gl->gl_ctx))
1273 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1274 context_gl->gl_ctx, context_gl->dc, GetLastError());
1275 context_gl->valid = 0;
1276 WARN("Trying fallback to the backup window.\n");
1278 /* FIXME: If the context is destroyed it's no longer associated with
1279 * a swapchain, so we can't use the swapchain to get a backup dc. To
1280 * make this work windowless contexts would need to be handled by the
1281 * device. */
1282 if (context_gl->c.destroyed || !swapchain_gl)
1284 FIXME("Unable to get backup dc for destroyed context %p.\n", context_gl);
1285 wined3d_context_gl_set_current(NULL);
1286 return FALSE;
1289 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
1291 wined3d_context_gl_set_current(NULL);
1292 return FALSE;
1295 context_gl->dc_is_private = TRUE;
1296 context_gl->dc_has_format = FALSE;
1298 if (!wined3d_context_gl_set_pixel_format(context_gl))
1300 ERR("Failed to set pixel format %d on device context %p.\n",
1301 context_gl->pixel_format, context_gl->dc);
1302 wined3d_context_gl_set_current(NULL);
1303 return FALSE;
1306 if (!wglMakeCurrent(context_gl->dc, context_gl->gl_ctx))
1308 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1309 context_gl->dc, GetLastError());
1310 wined3d_context_gl_set_current(NULL);
1311 return FALSE;
1314 context_gl->valid = 1;
1316 context_gl->needs_set = 0;
1318 return TRUE;
1321 static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx)
1323 if (!wglMakeCurrent(dc, gl_ctx))
1325 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1326 gl_ctx, dc, GetLastError());
1327 wined3d_context_gl_set_current(NULL);
1331 static void wined3d_context_gl_update_window(struct wined3d_context_gl *context_gl)
1333 if (!context_gl->c.swapchain)
1334 return;
1336 if (context_gl->window == context_gl->c.swapchain->win_handle)
1337 return;
1339 TRACE("Updating context %p window from %p to %p.\n",
1340 context_gl, context_gl->window, context_gl->c.swapchain->win_handle);
1342 if (context_gl->dc)
1343 wined3d_release_dc(context_gl->window, context_gl->dc);
1345 context_gl->window = context_gl->c.swapchain->win_handle;
1346 context_gl->dc_is_private = FALSE;
1347 context_gl->dc_has_format = FALSE;
1348 context_gl->needs_set = 1;
1349 context_gl->valid = 1;
1351 if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
1353 ERR("Failed to get a device context for window %p.\n", context_gl->window);
1354 context_gl->valid = 0;
1358 static void wined3d_context_gl_cleanup(struct wined3d_context_gl *context_gl)
1360 struct wined3d_pipeline_statistics_query *pipeline_statistics_query;
1361 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1362 struct wined3d_so_statistics_query *so_statistics_query;
1363 struct wined3d_timestamp_query *timestamp_query;
1364 struct wined3d_occlusion_query *occlusion_query;
1365 struct wined3d_context_gl *current;
1366 struct fbo_entry *entry, *entry2;
1367 struct wined3d_fence *fence;
1368 HGLRC restore_ctx;
1369 HDC restore_dc;
1370 unsigned int i;
1372 restore_ctx = wglGetCurrentContext();
1373 restore_dc = wglGetCurrentDC();
1375 if (context_gl->valid && context_gl->gl_ctx != restore_ctx)
1377 /* Attempting to restore a GL context corresponding to a wined3d
1378 * context is not particularly useful. Worse, when we're called from
1379 * wined3d_context_gl_destroy(), we subsequently clear the "current
1380 * D3D context" TLS value, which would cause
1381 * wined3d_context_gl_enter() to consider the GL context a non-D3D
1382 * context. */
1383 if ((current = wined3d_context_gl_get_current()) && current->gl_ctx == restore_ctx)
1384 restore_ctx = NULL;
1385 wined3d_context_gl_set_gl_context(context_gl);
1387 else
1389 restore_ctx = NULL;
1392 if (context_gl->valid)
1394 /* If we're here because we're switching away from a previously
1395 * destroyed context, acquiring a context in order to submit a fence
1396 * is problematic. In particular, we'd end up back here again in the
1397 * process of switching to the newly acquired context.
1399 * If fences aren't supported there should be nothing to wait for
1400 * anyway, so just do nothing in that case. */
1401 if (context_gl->c.destroyed)
1403 gl_info->gl_ops.gl.p_glFinish();
1405 else if (context_gl->c.d3d_info->fences)
1407 wined3d_context_gl_submit_command_fence(context_gl);
1408 wined3d_context_gl_wait_command_fence(context_gl,
1409 wined3d_device_gl(context_gl->c.device)->current_fence_id - 1);
1412 if (context_gl->dummy_arbfp_prog)
1413 GL_EXTCALL(glDeleteProgramsARB(1, &context_gl->dummy_arbfp_prog));
1415 if (context_gl->blit_vbo)
1416 GL_EXTCALL(glDeleteBuffers(1, &context_gl->blit_vbo));
1418 for (i = 0; i < context_gl->free_pipeline_statistics_query_count; ++i)
1420 union wined3d_gl_pipeline_statistics_query *q = &context_gl->free_pipeline_statistics_queries[i];
1421 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1424 for (i = 0; i < context_gl->free_so_statistics_query_count; ++i)
1426 union wined3d_gl_so_statistics_query *q = &context_gl->free_so_statistics_queries[i];
1427 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1430 if (context_gl->free_timestamp_query_count)
1431 GL_EXTCALL(glDeleteQueries(context_gl->free_timestamp_query_count, context_gl->free_timestamp_queries));
1433 if (gl_info->supported[ARB_SYNC])
1435 for (i = 0; i < context_gl->free_fence_count; ++i)
1437 GL_EXTCALL(glDeleteSync(context_gl->free_fences[i].sync));
1440 else if (gl_info->supported[APPLE_FENCE])
1442 for (i = 0; i < context_gl->free_fence_count; ++i)
1444 GL_EXTCALL(glDeleteFencesAPPLE(1, &context_gl->free_fences[i].id));
1447 else if (gl_info->supported[NV_FENCE])
1449 for (i = 0; i < context_gl->free_fence_count; ++i)
1451 GL_EXTCALL(glDeleteFencesNV(1, &context_gl->free_fences[i].id));
1455 if (context_gl->free_occlusion_query_count)
1456 GL_EXTCALL(glDeleteQueries(context_gl->free_occlusion_query_count, context_gl->free_occlusion_queries));
1458 checkGLcall("context cleanup");
1460 heap_free(context_gl->submitted.fences);
1461 heap_free(context_gl->free_pipeline_statistics_queries);
1462 heap_free(context_gl->free_so_statistics_queries);
1463 heap_free(context_gl->free_timestamp_queries);
1464 heap_free(context_gl->free_fences);
1465 heap_free(context_gl->free_occlusion_queries);
1467 LIST_FOR_EACH_ENTRY(pipeline_statistics_query, &context_gl->pipeline_statistics_queries,
1468 struct wined3d_pipeline_statistics_query, entry)
1470 if (context_gl->valid)
1471 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query->u.id), pipeline_statistics_query->u.id));
1472 pipeline_statistics_query->context_gl = NULL;
1475 LIST_FOR_EACH_ENTRY(so_statistics_query, &context_gl->so_statistics_queries,
1476 struct wined3d_so_statistics_query, entry)
1478 if (context_gl->valid)
1479 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query->u.id), so_statistics_query->u.id));
1480 so_statistics_query->context_gl = NULL;
1483 LIST_FOR_EACH_ENTRY(timestamp_query, &context_gl->timestamp_queries, struct wined3d_timestamp_query, entry)
1485 if (context_gl->valid)
1486 GL_EXTCALL(glDeleteQueries(1, &timestamp_query->id));
1487 timestamp_query->context_gl = NULL;
1490 LIST_FOR_EACH_ENTRY(fence, &context_gl->fences, struct wined3d_fence, entry)
1492 if (context_gl->valid)
1494 if (gl_info->supported[ARB_SYNC])
1496 if (fence->object.sync)
1497 GL_EXTCALL(glDeleteSync(fence->object.sync));
1499 else if (gl_info->supported[APPLE_FENCE])
1501 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence->object.id));
1503 else if (gl_info->supported[NV_FENCE])
1505 GL_EXTCALL(glDeleteFencesNV(1, &fence->object.id));
1508 fence->context_gl = NULL;
1511 LIST_FOR_EACH_ENTRY(occlusion_query, &context_gl->occlusion_queries, struct wined3d_occlusion_query, entry)
1513 if (context_gl->valid)
1514 GL_EXTCALL(glDeleteQueries(1, &occlusion_query->id));
1515 occlusion_query->context_gl = NULL;
1518 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_destroy_list, struct fbo_entry, entry)
1520 if (!context_gl->valid)
1521 entry->id = 0;
1522 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1525 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context_gl->fbo_list, struct fbo_entry, entry)
1527 if (!context_gl->valid)
1528 entry->id = 0;
1529 wined3d_context_gl_destroy_fbo_entry(context_gl, entry);
1532 heap_free(context_gl->texture_type);
1534 wined3d_context_gl_restore_pixel_format(context_gl);
1535 if (restore_ctx)
1536 context_restore_gl_context(gl_info, restore_dc, restore_ctx);
1537 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1538 ERR("Failed to disable GL context.\n");
1540 wined3d_release_dc(context_gl->window, context_gl->dc);
1542 if (!wglDeleteContext(context_gl->gl_ctx))
1544 DWORD err = GetLastError();
1545 ERR("Failed to delete GL context %p, last error %#x.\n", context_gl->gl_ctx, err);
1548 wined3d_context_cleanup(&context_gl->c);
1551 DWORD context_get_tls_idx(void)
1553 return wined3d_context_tls_idx;
1556 void context_set_tls_idx(DWORD idx)
1558 wined3d_context_tls_idx = idx;
1561 struct wined3d_context_gl *wined3d_context_gl_get_current(void)
1563 return TlsGetValue(wined3d_context_tls_idx);
1566 BOOL wined3d_context_gl_set_current(struct wined3d_context_gl *context_gl)
1568 struct wined3d_context_gl *old = wined3d_context_gl_get_current();
1570 if (old == context_gl)
1572 TRACE("Already using D3D context %p.\n", context_gl);
1573 return TRUE;
1576 if (old)
1578 if (old->c.destroyed)
1580 TRACE("Switching away from destroyed context %p.\n", old);
1581 wined3d_context_gl_cleanup(old);
1582 heap_free((void *)old->gl_info);
1583 heap_free(old);
1585 else
1587 if (wglGetCurrentContext())
1589 const struct wined3d_gl_info *gl_info = old->gl_info;
1590 TRACE("Flushing context %p before switching to %p.\n", old, context_gl);
1591 gl_info->gl_ops.gl.p_glFlush();
1593 old->c.current = 0;
1597 if (context_gl)
1599 if (!context_gl->valid)
1601 ERR("Trying to make invalid context %p current.\n", context_gl);
1602 return FALSE;
1605 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n",
1606 context_gl, context_gl->gl_ctx, context_gl->dc);
1607 if (!wined3d_context_gl_set_gl_context(context_gl))
1608 return FALSE;
1609 context_gl->c.current = 1;
1611 else if (wglGetCurrentContext())
1613 TRACE("Clearing current D3D context.\n");
1614 if (!wglMakeCurrent(NULL, NULL))
1616 DWORD err = GetLastError();
1617 ERR("Failed to clear current GL context, last error %#x.\n", err);
1618 TlsSetValue(wined3d_context_tls_idx, NULL);
1619 return FALSE;
1623 return TlsSetValue(wined3d_context_tls_idx, context_gl);
1626 void wined3d_context_gl_release(struct wined3d_context_gl *context_gl)
1628 TRACE("Releasing context %p, level %u.\n", context_gl, context_gl->level);
1630 if (WARN_ON(d3d))
1632 if (!context_gl->level)
1633 WARN("Context %p is not active.\n", context_gl);
1634 else if (context_gl != wined3d_context_gl_get_current())
1635 WARN("Context %p is not the current context.\n", context_gl);
1638 if (!--context_gl->level)
1640 if (wined3d_context_gl_restore_pixel_format(context_gl))
1641 context_gl->needs_set = 1;
1642 if (context_gl->restore_ctx)
1644 TRACE("Restoring GL context %p on device context %p.\n", context_gl->restore_ctx, context_gl->restore_dc);
1645 context_restore_gl_context(context_gl->gl_info, context_gl->restore_dc, context_gl->restore_ctx);
1646 context_gl->restore_ctx = NULL;
1647 context_gl->restore_dc = NULL;
1650 if (context_gl->c.destroy_delayed)
1652 TRACE("Destroying context %p.\n", context_gl);
1653 wined3d_context_gl_destroy(context_gl);
1658 static void wined3d_context_gl_enter(struct wined3d_context_gl *context_gl)
1660 TRACE("Entering context %p, level %u.\n", context_gl, context_gl->level + 1);
1662 if (!context_gl->level++)
1664 const struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
1665 HGLRC current_gl = wglGetCurrentContext();
1667 if (current_gl && (!current_context || current_context->gl_ctx != current_gl))
1669 TRACE("Another GL context (%p on device context %p) is already current.\n",
1670 current_gl, wglGetCurrentDC());
1671 context_gl->restore_ctx = current_gl;
1672 context_gl->restore_dc = wglGetCurrentDC();
1673 context_gl->needs_set = 1;
1675 else if (!context_gl->needs_set && !(context_gl->dc_is_private && context_gl->dc_has_format)
1676 && context_gl->pixel_format != context_gl->gl_info->gl_ops.wgl.p_wglGetPixelFormat(context_gl->dc))
1677 context_gl->needs_set = 1;
1681 /* This function takes care of wined3d pixel format selection. */
1682 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1683 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1684 bool aux_buffers, bool swap_effect_copy)
1686 unsigned int cfg_count = wined3d_adapter_gl(device->adapter)->pixel_format_count;
1687 unsigned int current_value;
1688 PIXELFORMATDESCRIPTOR pfd;
1689 int iPixelFormat = 0;
1690 unsigned int i;
1692 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, swap_effect_copy %#x.\n",
1693 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1694 aux_buffers, swap_effect_copy);
1696 current_value = 0;
1697 for (i = 0; i < cfg_count; ++i)
1699 const struct wined3d_pixel_format *cfg = &wined3d_adapter_gl(device->adapter)->pixel_formats[i];
1700 unsigned int value;
1702 /* For now only accept RGBA formats. Perhaps some day we will
1703 * allow floating point formats for pbuffers. */
1704 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1705 continue;
1706 /* In window mode we need a window drawable format and double buffering. */
1707 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1708 continue;
1709 if (cfg->redSize < color_format->red_size)
1710 continue;
1711 if (cfg->greenSize < color_format->green_size)
1712 continue;
1713 if (cfg->blueSize < color_format->blue_size)
1714 continue;
1715 if (cfg->alphaSize < color_format->alpha_size)
1716 continue;
1717 if (cfg->depthSize < ds_format->depth_size)
1718 continue;
1719 if (ds_format->stencil_size && cfg->stencilSize != ds_format->stencil_size)
1720 continue;
1721 /* Check multisampling support. */
1722 if (cfg->numSamples)
1723 continue;
1725 value = 1;
1726 if (swap_effect_copy && cfg->swap_method == WGL_SWAP_COPY_ARB)
1727 value += 1;
1728 /* We try to locate a format which matches our requirements exactly. In case of
1729 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1730 if (cfg->depthSize == ds_format->depth_size)
1731 value += 2;
1732 if (cfg->stencilSize == ds_format->stencil_size)
1733 value += 4;
1734 if (cfg->alphaSize == color_format->alpha_size)
1735 value += 8;
1736 /* We like to have aux buffers in backbuffer mode */
1737 if (aux_buffers && cfg->auxBuffers)
1738 value += 16;
1739 if (cfg->redSize == color_format->red_size
1740 && cfg->greenSize == color_format->green_size
1741 && cfg->blueSize == color_format->blue_size)
1742 value += 32;
1744 if (value > current_value)
1746 iPixelFormat = cfg->iPixelFormat;
1747 current_value = value;
1751 if (!iPixelFormat)
1753 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1755 memset(&pfd, 0, sizeof(pfd));
1756 pfd.nSize = sizeof(pfd);
1757 pfd.nVersion = 1;
1758 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1759 pfd.iPixelType = PFD_TYPE_RGBA;
1760 pfd.cAlphaBits = color_format->alpha_size;
1761 pfd.cColorBits = color_format->red_size + color_format->green_size
1762 + color_format->blue_size + color_format->alpha_size;
1763 pfd.cDepthBits = ds_format->depth_size;
1764 pfd.cStencilBits = ds_format->stencil_size;
1765 pfd.iLayerType = PFD_MAIN_PLANE;
1767 if (!(iPixelFormat = ChoosePixelFormat(hdc, &pfd)))
1769 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1770 ERR("Can't find a suitable pixel format.\n");
1771 return 0;
1775 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1776 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1777 return iPixelFormat;
1780 /* Context activation is done by the caller. */
1781 void wined3d_context_gl_bind_dummy_textures(const struct wined3d_context_gl *context_gl)
1783 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
1784 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1785 unsigned int i;
1787 for (i = 0; i < gl_info->limits.combined_samplers; ++i)
1789 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + i));
1791 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
1792 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
1794 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1795 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
1797 if (gl_info->supported[EXT_TEXTURE3D])
1798 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
1800 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1801 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
1803 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
1804 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
1806 if (gl_info->supported[EXT_TEXTURE_ARRAY])
1808 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
1809 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
1812 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1813 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
1815 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1817 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
1818 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
1822 checkGLcall("bind dummy textures");
1825 void wined3d_check_gl_call(const struct wined3d_gl_info *gl_info,
1826 const char *file, unsigned int line, const char *name)
1828 GLint err;
1830 if (gl_info->supported[ARB_DEBUG_OUTPUT] || (err = gl_info->gl_ops.gl.p_glGetError()) == GL_NO_ERROR)
1832 TRACE("%s call ok %s / %u.\n", name, file, line);
1833 return;
1838 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1839 debug_glerror(err), err, name, file,line);
1840 err = gl_info->gl_ops.gl.p_glGetError();
1841 } while (err != GL_NO_ERROR);
1844 static BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1846 return gl_info->supported[ARB_DEBUG_OUTPUT]
1847 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1850 static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1851 GLenum severity, GLsizei length, const char *message, void *ctx)
1853 switch (type)
1855 case GL_DEBUG_TYPE_ERROR_ARB:
1856 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1857 break;
1859 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1860 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1861 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1862 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1863 break;
1865 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1866 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1867 break;
1869 default:
1870 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1871 break;
1875 HGLRC context_create_wgl_attribs(const struct wined3d_gl_info *gl_info, HDC hdc, HGLRC share_ctx)
1877 HGLRC ctx;
1878 unsigned int ctx_attrib_idx = 0;
1879 GLint ctx_attribs[7], ctx_flags = 0;
1881 if (context_debug_output_enabled(gl_info))
1882 ctx_flags = WGL_CONTEXT_DEBUG_BIT_ARB;
1883 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
1884 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version >> 16;
1885 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MINOR_VERSION_ARB;
1886 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version & 0xffff;
1887 if (ctx_flags)
1889 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1890 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1892 ctx_attribs[ctx_attrib_idx] = 0;
1894 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1896 if (gl_info->selected_gl_version >= MAKEDWORD_VERSION(3, 2))
1898 if (ctx_flags)
1900 ctx_flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1901 ctx_attribs[ctx_attrib_idx - 1] = ctx_flags;
1903 else
1905 ctx_flags = WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1906 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1907 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1908 ctx_attribs[ctx_attrib_idx] = 0;
1910 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1911 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1912 GetLastError());
1915 return ctx;
1918 static BOOL wined3d_context_gl_create_wgl_ctx(struct wined3d_context_gl *context_gl,
1919 struct wined3d_swapchain_gl *swapchain_gl, BOOL *new_drawable)
1921 enum wined3d_swap_effect swap_effect = swapchain_gl->s.state.desc.swap_effect;
1922 const struct wined3d_format *colour_format, *ds_format;
1923 struct wined3d_context *context = &context_gl->c;
1924 const struct wined3d_gl_info *gl_info;
1925 struct wined3d_resource *target;
1926 struct wined3d_adapter *adapter;
1927 unsigned int target_bind_flags;
1928 struct wined3d_device *device;
1929 bool swap_effect_copy;
1930 HGLRC ctx, share_ctx;
1931 unsigned int i;
1933 device = context->device;
1934 adapter = device->adapter;
1935 gl_info = &adapter->gl_info;
1937 target = &context->current_rt.texture->resource;
1938 target_bind_flags = target->bind_flags;
1940 swap_effect_copy = swap_effect == WINED3D_SWAP_EFFECT_COPY || swap_effect == WINED3D_SWAP_EFFECT_COPY_VSYNC;
1942 *new_drawable = !GetPixelFormat(context_gl->dc);
1944 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1946 static const enum wined3d_format_id ds_formats[] =
1948 WINED3DFMT_D24_UNORM_S8_UINT,
1949 WINED3DFMT_D32_UNORM,
1950 WINED3DFMT_R24_UNORM_X8_TYPELESS,
1951 WINED3DFMT_D16_UNORM,
1952 WINED3DFMT_S1_UINT_D15_UNORM,
1955 colour_format = target->format;
1957 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1958 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1959 if (colour_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1960 colour_format = wined3d_get_format(adapter, WINED3DFMT_B4G4R4A4_UNORM, target_bind_flags);
1961 else if (colour_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1962 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1964 /* DirectDraw supports 8bit paletted render targets and these are used by
1965 * old games like StarCraft and C&C. Most modern hardware doesn't support
1966 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1967 * conversion (ab)uses the alpha component for storing the palette index.
1968 * For this reason we require a format with 8bit alpha, so request
1969 * A8R8G8B8. */
1970 if (colour_format->id == WINED3DFMT_P8_UINT)
1971 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
1973 /* Try to find a pixel format which matches our requirements. */
1974 if (!swapchain_gl->s.ds_format)
1976 for (i = 0; i < ARRAY_SIZE(ds_formats); ++i)
1978 ds_format = wined3d_get_format(adapter, ds_formats[i], WINED3D_BIND_DEPTH_STENCIL);
1979 if ((context_gl->pixel_format = context_choose_pixel_format(device,
1980 context_gl->dc, colour_format, ds_format, true, swap_effect_copy)))
1982 swapchain_gl->s.ds_format = ds_format;
1983 break;
1986 TRACE("Depth stencil format %s is not supported, trying next format.\n",
1987 debug_d3dformat(ds_format->id));
1990 else
1992 context_gl->pixel_format = context_choose_pixel_format(device,
1993 context_gl->dc, colour_format, swapchain_gl->s.ds_format, true, swap_effect_copy);
1996 else
1998 /* When using FBOs for off-screen rendering, we only use the drawable for
1999 * presentation blits, and don't do any rendering to it. That means we
2000 * don't need depth or stencil buffers, and can mostly ignore the render
2001 * target format. This wouldn't necessarily be quite correct for 10bpc
2002 * display modes, but we don't currently support those.
2003 * Using the same format regardless of the colour/depth/stencil targets
2004 * makes it much less likely that different wined3d instances will set
2005 * conflicting pixel formats. */
2006 colour_format = wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, target_bind_flags);
2007 ds_format = wined3d_get_format(adapter, WINED3DFMT_UNKNOWN, WINED3D_BIND_DEPTH_STENCIL);
2008 context_gl->pixel_format = context_choose_pixel_format(device,
2009 context_gl->dc, colour_format, ds_format, false, swap_effect_copy);
2012 if (!context_gl->pixel_format)
2014 ERR("Failed to choose pixel format.\n");
2015 return FALSE;
2018 wined3d_context_gl_enter(context_gl);
2020 if (!wined3d_context_gl_set_pixel_format(context_gl))
2022 context_release(context);
2024 if (context_gl->dc_is_private)
2026 ERR("Failed to set pixel format %d on device context %p.\n", context_gl->pixel_format, context_gl->dc);
2028 return FALSE;
2031 WARN("Failed to set pixel format %d on device context %p, trying backup DC.\n",
2032 context_gl->pixel_format, context_gl->dc);
2034 wined3d_release_dc(context_gl->window, context_gl->dc);
2035 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2037 ERR("Failed to retrieve the backup device context.\n");
2038 return FALSE;
2040 context_gl->dc_is_private = TRUE;
2042 return wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl, new_drawable);
2045 share_ctx = device->context_count ? wined3d_context_gl(device->contexts[0])->gl_ctx : NULL;
2046 if (gl_info->p_wglCreateContextAttribsARB)
2048 if (!(ctx = context_create_wgl_attribs(gl_info, context_gl->dc, share_ctx)))
2050 ERR("Failed to create a WGL context.\n");
2051 context_release(context);
2052 return FALSE;
2055 else
2057 if (!(ctx = wglCreateContext(context_gl->dc)))
2059 ERR("Failed to create a WGL context.\n");
2060 context_release(context);
2061 return FALSE;
2064 if (share_ctx && !wglShareLists(share_ctx, ctx))
2066 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
2067 context_release(context);
2068 if (!wglDeleteContext(ctx))
2069 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2070 return FALSE;
2074 context_gl->dc_has_format = TRUE;
2075 context_gl->needs_set = 1;
2076 context_gl->valid = 1;
2077 context_gl->gl_ctx = ctx;
2079 return TRUE;
2082 HRESULT wined3d_context_gl_init(struct wined3d_context_gl *context_gl, struct wined3d_swapchain_gl *swapchain_gl)
2084 struct wined3d_context *context = &context_gl->c;
2085 const struct wined3d_d3d_info *d3d_info;
2086 const struct wined3d_gl_info *gl_info;
2087 struct wined3d_device *device;
2088 BOOL new_drawable;
2089 unsigned int i;
2091 TRACE("context_gl %p, swapchain %p.\n", context_gl, swapchain_gl);
2093 wined3d_context_init(&context_gl->c, &swapchain_gl->s);
2095 device = context->device;
2096 gl_info = &device->adapter->gl_info;
2097 context_gl->gl_info = gl_info;
2098 d3d_info = context->d3d_info;
2100 context_gl->tid = GetCurrentThreadId();
2101 context_gl->window = context->swapchain->win_handle;
2102 if (context_gl->window == GetDesktopWindow())
2104 TRACE("Swapchain is created on the desktop window, trying backup device context.\n");
2105 context_gl->dc = NULL;
2107 else if (!(context_gl->dc = GetDCEx(context_gl->window, 0, DCX_USESTYLE | DCX_CACHE)))
2108 WARN("Failed to retrieve device context, trying swapchain backup.\n");
2110 if (!context_gl->dc)
2112 if (!(context_gl->dc = wined3d_swapchain_gl_get_backup_dc(swapchain_gl)))
2114 ERR("Failed to retrieve a device context.\n");
2115 return E_FAIL;
2117 context_gl->dc_is_private = TRUE;
2120 list_init(&context_gl->fbo_list);
2121 list_init(&context_gl->fbo_destroy_list);
2123 list_init(&context_gl->occlusion_queries);
2124 list_init(&context_gl->fences);
2125 list_init(&context_gl->timestamp_queries);
2126 list_init(&context_gl->so_statistics_queries);
2127 list_init(&context_gl->pipeline_statistics_queries);
2129 for (i = 0; i < ARRAY_SIZE(context_gl->tex_unit_map); ++i)
2130 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2131 for (i = 0; i < ARRAY_SIZE(context_gl->rev_tex_unit_map); ++i)
2132 context_gl->rev_tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
2133 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
2135 /* Initialize the texture unit mapping to a 1:1 mapping. */
2136 unsigned int base, count;
2138 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_PIXEL, &base, &count);
2139 if (base + WINED3D_MAX_FRAGMENT_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2141 ERR("Unexpected texture unit base index %u.\n", base);
2142 goto fail;
2144 for (i = 0; i < min(count, WINED3D_MAX_FRAGMENT_SAMPLERS); ++i)
2146 context_gl->tex_unit_map[i] = base + i;
2147 context_gl->rev_tex_unit_map[base + i] = i;
2150 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_VERTEX, &base, &count);
2151 if (base + WINED3D_MAX_VERTEX_SAMPLERS > ARRAY_SIZE(context_gl->rev_tex_unit_map))
2153 ERR("Unexpected texture unit base index %u.\n", base);
2154 goto fail;
2156 for (i = 0; i < min(count, WINED3D_MAX_VERTEX_SAMPLERS); ++i)
2158 context_gl->tex_unit_map[WINED3D_MAX_FRAGMENT_SAMPLERS + i] = base + i;
2159 context_gl->rev_tex_unit_map[base + i] = WINED3D_MAX_FRAGMENT_SAMPLERS + i;
2163 if (!(context_gl->texture_type = heap_calloc(gl_info->limits.combined_samplers,
2164 sizeof(*context_gl->texture_type))))
2165 goto fail;
2167 if (!wined3d_context_gl_create_wgl_ctx(context_gl, swapchain_gl, &new_drawable))
2168 goto fail;
2170 /* Set up the context defaults. */
2172 context->render_offscreen = wined3d_resource_is_offscreen(&context->current_rt.texture->resource);
2173 context_gl->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
2175 if (!wined3d_context_gl_set_current(context_gl))
2177 ERR("Cannot activate context to set up defaults.\n");
2178 context_release(context);
2179 if (!wglDeleteContext(context_gl->gl_ctx))
2180 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context_gl->gl_ctx, GetLastError());
2181 goto fail;
2184 if (context_debug_output_enabled(gl_info))
2186 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback, context));
2187 if (TRACE_ON(d3d_sync))
2188 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2189 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
2190 if (ERR_ON(d3d))
2192 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR,
2193 GL_DONT_CARE, 0, NULL, GL_TRUE));
2195 if (FIXME_ON(d3d))
2197 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
2198 GL_DONT_CARE, 0, NULL, GL_TRUE));
2199 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
2200 GL_DONT_CARE, 0, NULL, GL_TRUE));
2201 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY,
2202 GL_DONT_CARE, 0, NULL, GL_TRUE));
2204 if (WARN_ON(d3d_perf))
2206 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE,
2207 GL_DONT_CARE, 0, NULL, GL_TRUE));
2211 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2212 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &context_gl->aux_buffers);
2214 TRACE("Setting up the screen\n");
2216 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2218 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
2219 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2221 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
2222 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2224 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2225 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2227 else
2229 GLuint vao;
2231 GL_EXTCALL(glGenVertexArrays(1, &vao));
2232 GL_EXTCALL(glBindVertexArray(vao));
2233 checkGLcall("creating VAO");
2236 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
2237 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2238 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2239 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2241 if (gl_info->supported[NV_TEXTURE_SHADER2])
2243 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2244 * the previous texture where to source the offset from is always unit - 1.
2246 for (i = 1; i < gl_info->limits.textures; ++i)
2248 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2249 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
2250 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + i - 1);
2251 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2254 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2256 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2257 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2258 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2259 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2260 * is ever assigned.
2262 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2263 * program and the dummy program is destroyed when the context is destroyed.
2265 static const char dummy_program[] =
2266 "!!ARBfp1.0\n"
2267 "MOV result.color, fragment.color.primary;\n"
2268 "END\n";
2269 GL_EXTCALL(glGenProgramsARB(1, &context_gl->dummy_arbfp_prog));
2270 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, context_gl->dummy_arbfp_prog));
2271 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
2272 GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2275 if (gl_info->supported[ARB_POINT_SPRITE])
2277 for (i = 0; i < gl_info->limits.textures; ++i)
2279 wined3d_context_gl_active_texture(context_gl, gl_info, i);
2280 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2281 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2285 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2287 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2289 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2291 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2293 if (!(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART))
2295 if (gl_info->supported[ARB_ES3_COMPATIBILITY])
2297 gl_info->gl_ops.gl.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
2298 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2300 else
2302 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2305 if (!(d3d_info->wined3d_creation_flags & WINED3D_LEGACY_CUBEMAP_FILTERING)
2306 && gl_info->supported[ARB_SEAMLESS_CUBE_MAP])
2308 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
2309 checkGLcall("enable seamless cube map filtering");
2311 if (gl_info->supported[ARB_CLIP_CONTROL])
2312 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT));
2314 /* If this happens to be the first context for the device, dummy textures
2315 * are not created yet. In that case, they will be created (and bound) by
2316 * create_dummy_textures right after this context is initialized. */
2317 if (wined3d_device_gl(device)->dummy_textures.tex_2d)
2318 wined3d_context_gl_bind_dummy_textures(context_gl);
2320 /* Initialise all rectangles to avoid resetting unused ones later. */
2321 gl_info->gl_ops.gl.p_glScissor(0, 0, 0, 0);
2322 checkGLcall("glScissor");
2324 if (new_drawable)
2326 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
2327 gl_info->gl_ops.gl.p_glClearDepth(1.0f);
2328 gl_info->gl_ops.gl.p_glClearStencil(0);
2329 gl_info->gl_ops.gl.p_glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2330 checkGLcall("glClear");
2332 return WINED3D_OK;
2334 fail:
2335 heap_free(context_gl->texture_type);
2336 wined3d_release_dc(context_gl->window, context_gl->dc);
2337 return E_FAIL;
2340 void wined3d_context_gl_destroy(struct wined3d_context_gl *context_gl)
2342 struct wined3d_device *device = context_gl->c.device;
2344 TRACE("Destroying context %p.\n", context_gl);
2346 wined3d_from_cs(device->cs);
2348 /* We delay destroying a context when it is active. The context_release()
2349 * function invokes wined3d_context_gl_destroy() again while leaving the
2350 * last level. */
2351 if (context_gl->level)
2353 TRACE("Delaying destruction of context %p.\n", context_gl);
2354 context_gl->c.destroy_delayed = 1;
2355 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2356 context_gl->c.swapchain = NULL;
2357 return;
2360 device_context_remove(device, &context_gl->c);
2362 if (context_gl->c.current && context_gl->tid != GetCurrentThreadId())
2364 struct wined3d_gl_info *gl_info;
2366 /* Make a copy of gl_info for wined3d_context_gl_cleanup() use, the
2367 * one in wined3d_adapter may go away in the meantime. */
2368 gl_info = heap_alloc(sizeof(*gl_info));
2369 *gl_info = *context_gl->gl_info;
2370 context_gl->gl_info = gl_info;
2371 context_gl->c.destroyed = 1;
2373 return;
2376 wined3d_context_gl_cleanup(context_gl);
2377 TlsSetValue(context_get_tls_idx(), NULL);
2378 heap_free(context_gl);
2381 const unsigned int *wined3d_context_gl_get_tex_unit_mapping(const struct wined3d_context_gl *context_gl,
2382 const struct wined3d_shader_version *shader_version, unsigned int *base, unsigned int *count)
2384 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2386 if (!shader_version)
2388 *base = 0;
2389 *count = WINED3D_MAX_TEXTURES;
2390 return context_gl->tex_unit_map;
2393 if (shader_version->major >= 4)
2395 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, shader_version->type, base, count);
2396 return NULL;
2399 switch (shader_version->type)
2401 case WINED3D_SHADER_TYPE_PIXEL:
2402 *base = 0;
2403 *count = WINED3D_MAX_FRAGMENT_SAMPLERS;
2404 break;
2405 case WINED3D_SHADER_TYPE_VERTEX:
2406 *base = WINED3D_MAX_FRAGMENT_SAMPLERS;
2407 *count = WINED3D_MAX_VERTEX_SAMPLERS;
2408 break;
2409 default:
2410 ERR("Unhandled shader type %#x.\n", shader_version->type);
2411 *base = 0;
2412 *count = 0;
2415 return context_gl->tex_unit_map;
2418 static void wined3d_context_gl_get_rt_size(const struct wined3d_context_gl *context_gl, SIZE *size)
2420 const struct wined3d_texture *rt = context_gl->c.current_rt.texture;
2421 unsigned int level;
2423 if (rt->swapchain)
2425 RECT window_size;
2427 GetClientRect(context_gl->window, &window_size);
2428 size->cx = window_size.right - window_size.left;
2429 size->cy = window_size.bottom - window_size.top;
2431 return;
2434 level = context_gl->c.current_rt.sub_resource_idx % rt->level_count;
2435 size->cx = wined3d_texture_get_level_width(rt, level);
2436 size->cy = wined3d_texture_get_level_height(rt, level);
2439 void wined3d_context_gl_enable_clip_distances(struct wined3d_context_gl *context_gl, uint32_t enable_mask)
2441 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2442 unsigned int clip_distance_count, i;
2443 uint32_t disable_mask, current_mask;
2445 clip_distance_count = gl_info->limits.user_clip_distances;
2446 disable_mask = ~enable_mask;
2447 enable_mask &= wined3d_mask_from_size(clip_distance_count);
2448 disable_mask &= wined3d_mask_from_size(clip_distance_count);
2449 current_mask = context_gl->c.clip_distance_mask;
2450 context_gl->c.clip_distance_mask = enable_mask;
2452 enable_mask &= ~current_mask;
2453 while (enable_mask)
2455 i = wined3d_bit_scan(&enable_mask);
2456 gl_info->gl_ops.gl.p_glEnable(GL_CLIP_DISTANCE0 + i);
2458 disable_mask &= current_mask;
2459 while (disable_mask)
2461 i = wined3d_bit_scan(&disable_mask);
2462 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_DISTANCE0 + i);
2464 checkGLcall("toggle clip distances");
2467 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2469 return rt_mask & (1u << 31);
2472 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2474 return rt_mask & ~(1u << 31);
2477 /* Context activation is done by the caller. */
2478 static void wined3d_context_gl_apply_draw_buffers(struct wined3d_context_gl *context_gl, uint32_t rt_mask)
2480 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2481 GLenum draw_buffers[WINED3D_MAX_RENDER_TARGETS];
2483 if (!rt_mask)
2485 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2487 else if (is_rt_mask_onscreen(rt_mask))
2489 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2491 else
2493 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2495 unsigned int i = 0;
2497 while (rt_mask)
2499 if (rt_mask & 1)
2500 draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2501 else
2502 draw_buffers[i] = GL_NONE;
2504 rt_mask >>= 1;
2505 ++i;
2508 if (gl_info->supported[ARB_DRAW_BUFFERS])
2510 GL_EXTCALL(glDrawBuffers(i, draw_buffers));
2512 else
2514 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffers[0]);
2517 else
2519 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2523 checkGLcall("apply draw buffers");
2526 /* Context activation is done by the caller. */
2527 void wined3d_context_gl_active_texture(struct wined3d_context_gl *context_gl,
2528 const struct wined3d_gl_info *gl_info, unsigned int unit)
2530 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + unit));
2531 checkGLcall("glActiveTexture");
2532 context_gl->active_texture = unit;
2535 void wined3d_context_gl_bind_bo(struct wined3d_context_gl *context_gl, GLenum binding, GLuint name)
2537 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2539 if (binding == GL_ELEMENT_ARRAY_BUFFER)
2540 context_invalidate_state(&context_gl->c, STATE_INDEXBUFFER);
2542 GL_EXTCALL(glBindBuffer(binding, name));
2545 void wined3d_context_gl_bind_texture(struct wined3d_context_gl *context_gl, GLenum target, GLuint name)
2547 const struct wined3d_dummy_textures *textures = &wined3d_device_gl(context_gl->c.device)->dummy_textures;
2548 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2549 GLenum old_texture_type;
2550 unsigned int unit;
2552 if (name)
2553 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2554 else
2555 target = GL_NONE;
2557 unit = context_gl->active_texture;
2558 old_texture_type = context_gl->texture_type[unit];
2559 if (old_texture_type != target)
2561 switch (old_texture_type)
2563 case GL_NONE:
2564 /* nothing to do */
2565 break;
2566 case GL_TEXTURE_1D:
2567 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, textures->tex_1d);
2568 break;
2569 case GL_TEXTURE_1D_ARRAY:
2570 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, textures->tex_1d_array);
2571 break;
2572 case GL_TEXTURE_2D:
2573 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, textures->tex_2d);
2574 break;
2575 case GL_TEXTURE_2D_ARRAY:
2576 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, textures->tex_2d_array);
2577 break;
2578 case GL_TEXTURE_RECTANGLE_ARB:
2579 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, textures->tex_rect);
2580 break;
2581 case GL_TEXTURE_CUBE_MAP:
2582 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, textures->tex_cube);
2583 break;
2584 case GL_TEXTURE_CUBE_MAP_ARRAY:
2585 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, textures->tex_cube_array);
2586 break;
2587 case GL_TEXTURE_3D:
2588 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, textures->tex_3d);
2589 break;
2590 case GL_TEXTURE_BUFFER:
2591 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, textures->tex_buffer);
2592 break;
2593 case GL_TEXTURE_2D_MULTISAMPLE:
2594 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures->tex_2d_ms);
2595 break;
2596 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2597 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, textures->tex_2d_ms_array);
2598 break;
2599 default:
2600 ERR("Unexpected texture target %#x.\n", old_texture_type);
2603 context_gl->texture_type[unit] = target;
2606 checkGLcall("bind texture");
2609 static void wined3d_context_gl_poll_fences(struct wined3d_context_gl *context_gl)
2611 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2612 struct wined3d_command_fence_gl *f;
2613 SIZE_T i;
2615 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2617 f = &context_gl->submitted.fences[i];
2619 if (f->id > device_gl->completed_fence_id)
2621 if (wined3d_fence_test(f->fence, &device_gl->d, 0) != WINED3D_FENCE_OK)
2622 continue;
2623 device_gl->completed_fence_id = f->id;
2626 wined3d_fence_destroy(f->fence);
2627 if (i != context_gl->submitted.fence_count - 1)
2628 *f = context_gl->submitted.fences[context_gl->submitted.fence_count - 1];
2629 --context_gl->submitted.fence_count;
2633 static void wined3d_device_gl_free_memory(struct wined3d_device_gl *device_gl, struct wined3d_allocator_block *block)
2635 assert(block->chunk->allocator == &device_gl->allocator);
2636 wined3d_device_gl_allocator_lock(device_gl);
2637 wined3d_allocator_block_free(block);
2638 wined3d_device_gl_allocator_unlock(device_gl);
2641 static void wined3d_context_gl_cleanup_resources(struct wined3d_context_gl *context_gl)
2643 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2644 struct wined3d_retired_block_gl *r, *blocks;
2645 SIZE_T count, i = 0;
2646 uint64_t id;
2648 wined3d_context_gl_poll_fences(context_gl);
2649 id = device_gl->completed_fence_id;
2651 blocks = device_gl->retired_blocks;
2652 count = device_gl->retired_block_count;
2653 while (i < count)
2655 r = &blocks[i];
2657 if (r->fence_id > id)
2659 ++i;
2660 continue;
2663 wined3d_device_gl_free_memory(device_gl, r->block);
2664 if (i != --count)
2665 *r = blocks[count];
2666 else
2667 ++i;
2669 device_gl->retired_block_count = count;
2672 void wined3d_context_gl_wait_command_fence(struct wined3d_context_gl *context_gl, uint64_t id)
2674 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2675 enum wined3d_fence_result ret;
2676 SIZE_T i;
2678 if (id <= device_gl->completed_fence_id
2679 || id > device_gl->current_fence_id) /* In case the fence ID wrapped. */
2680 return;
2682 for (i = 0; i < context_gl->submitted.fence_count; ++i)
2684 if (context_gl->submitted.fences[i].id != id)
2685 continue;
2687 if ((ret = wined3d_fence_wait(context_gl->submitted.fences[i].fence, &device_gl->d)) != WINED3D_FENCE_OK)
2688 ERR("Failed to wait for command fence with id 0x%s, ret %#x.\n", wine_dbgstr_longlong(id), ret);
2689 wined3d_context_gl_cleanup_resources(context_gl);
2690 return;
2693 ERR("Failed to find fence for command fence with id 0x%s.\n", wine_dbgstr_longlong(id));
2696 void wined3d_context_gl_submit_command_fence(struct wined3d_context_gl *context_gl)
2698 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2699 struct wined3d_command_fence_gl *f;
2700 HRESULT hr;
2702 if (!wined3d_array_reserve((void **)&context_gl->submitted.fences, &context_gl->submitted.fences_size,
2703 context_gl->submitted.fence_count + 1, sizeof(*context_gl->submitted.fences)))
2704 ERR("Failed to grow submitted command buffer array.\n");
2706 f = &context_gl->submitted.fences[context_gl->submitted.fence_count++];
2707 f->id = device_gl->current_fence_id;
2708 if (FAILED(hr = wined3d_fence_create(&device_gl->d, &f->fence)))
2709 ERR("Failed to create fence, hr %#x.\n", hr);
2710 wined3d_fence_issue(f->fence, &device_gl->d);
2712 /* We don't expect this to ever happen, but handle it anyway. */
2713 if (!++device_gl->current_fence_id)
2715 wined3d_context_gl_wait_command_fence(context_gl, device_gl->current_fence_id - 1);
2716 device_gl->completed_fence_id = 0;
2717 device_gl->current_fence_id = 1;
2719 device_gl->retired_bo_size = 0;
2720 wined3d_context_gl_cleanup_resources(context_gl);
2723 static void wined3d_context_gl_destroy_allocator_block(struct wined3d_context_gl *context_gl,
2724 struct wined3d_allocator_block *block, uint64_t fence_id)
2726 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2727 struct wined3d_retired_block_gl *r;
2729 if (device_gl->completed_fence_id > fence_id)
2731 wined3d_device_gl_free_memory(device_gl, block);
2732 TRACE("Freed block %p.\n", block);
2733 return;
2736 if (!wined3d_array_reserve((void **)&device_gl->retired_blocks,
2737 &device_gl->retired_blocks_size, device_gl->retired_block_count + 1,
2738 sizeof(*device_gl->retired_blocks)))
2740 ERR("Leaking block %p.\n", block);
2741 return;
2744 r = &device_gl->retired_blocks[device_gl->retired_block_count++];
2745 r->block = block;
2746 r->fence_id = fence_id;
2749 /* We always have buffer storage here. */
2750 GLuint wined3d_context_gl_allocate_vram_chunk_buffer(struct wined3d_context_gl *context_gl,
2751 unsigned int pool, size_t size)
2753 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2754 GLbitfield flags;
2755 GLuint id = 0;
2757 TRACE("context_gl %p, pool %u, size %Iu.\n", context_gl, pool, size);
2759 GL_EXTCALL(glGenBuffers(1, &id));
2760 if (!id)
2762 checkGLcall("buffer object creation");
2763 return 0;
2765 wined3d_context_gl_bind_bo(context_gl, GL_PIXEL_UNPACK_BUFFER, id);
2767 flags = wined3d_device_gl_get_memory_type_flags(pool) | GL_DYNAMIC_STORAGE_BIT;
2768 if (flags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))
2769 flags |= GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
2770 GL_EXTCALL(glBufferStorage(GL_PIXEL_UNPACK_BUFFER, size, NULL, flags));
2772 checkGLcall("buffer object creation");
2774 TRACE("Created buffer object %u.\n", id);
2776 return id;
2779 static void *wined3d_allocator_chunk_gl_map(struct wined3d_allocator_chunk_gl *chunk_gl,
2780 struct wined3d_context_gl *context_gl)
2782 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2783 void *map_ptr;
2785 TRACE("chunk %p, gl_buffer %u, map_ptr %p.\n", chunk_gl, chunk_gl->gl_buffer, chunk_gl->c.map_ptr);
2787 wined3d_allocator_chunk_gl_lock(chunk_gl);
2789 if (!chunk_gl->c.map_ptr)
2791 unsigned int flags = wined3d_device_gl_get_memory_type_flags(chunk_gl->memory_type) & ~GL_CLIENT_STORAGE_BIT;
2793 flags |= GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
2794 if (!(flags & GL_MAP_READ_BIT))
2795 flags |= GL_MAP_UNSYNCHRONIZED_BIT;
2796 if (flags & GL_MAP_WRITE_BIT)
2797 flags |= GL_MAP_FLUSH_EXPLICIT_BIT;
2798 wined3d_context_gl_bind_bo(context_gl, GL_PIXEL_UNPACK_BUFFER, chunk_gl->gl_buffer);
2799 chunk_gl->c.map_ptr = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
2800 0, WINED3D_ALLOCATOR_CHUNK_SIZE, flags));
2801 if (!chunk_gl->c.map_ptr)
2803 wined3d_allocator_chunk_gl_unlock(chunk_gl);
2804 ERR("Failed to map chunk memory.\n");
2805 return NULL;
2808 adapter_adjust_mapped_memory(context_gl->c.device->adapter, WINED3D_ALLOCATOR_CHUNK_SIZE);
2811 ++chunk_gl->c.map_count;
2812 map_ptr = chunk_gl->c.map_ptr;
2814 wined3d_allocator_chunk_gl_unlock(chunk_gl);
2816 return map_ptr;
2819 static void wined3d_allocator_chunk_gl_unmap(struct wined3d_allocator_chunk_gl *chunk_gl,
2820 struct wined3d_context_gl *context_gl)
2822 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2824 TRACE("chunk_gl %p, context_gl %p.\n", chunk_gl, context_gl);
2826 wined3d_allocator_chunk_gl_lock(chunk_gl);
2828 if (!--chunk_gl->c.map_count)
2830 wined3d_context_gl_bind_bo(context_gl, GL_PIXEL_UNPACK_BUFFER, chunk_gl->gl_buffer);
2831 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
2832 chunk_gl->c.map_ptr = NULL;
2834 adapter_adjust_mapped_memory(context_gl->c.device->adapter, -WINED3D_ALLOCATOR_CHUNK_SIZE);
2837 wined3d_allocator_chunk_gl_unlock(chunk_gl);
2840 static void *wined3d_bo_gl_map(struct wined3d_bo_gl *bo, struct wined3d_context_gl *context_gl, uint32_t flags)
2842 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
2843 const struct wined3d_gl_info *gl_info;
2844 struct wined3d_bo_user *bo_user;
2845 struct wined3d_bo_gl tmp;
2847 if (flags & WINED3D_MAP_NOOVERWRITE)
2848 goto map;
2850 if ((flags & WINED3D_MAP_DISCARD) && bo->command_fence_id > device_gl->completed_fence_id)
2852 if (wined3d_device_gl_create_bo(device_gl, context_gl, bo->size,
2853 bo->binding, bo->usage, bo->b.coherent, bo->flags, &tmp))
2855 list_move_head(&tmp.b.users, &bo->b.users);
2856 wined3d_context_gl_destroy_bo(context_gl, bo);
2857 *bo = tmp;
2858 list_init(&bo->b.users);
2859 list_move_head(&bo->b.users, &tmp.b.users);
2860 LIST_FOR_EACH_ENTRY(bo_user, &bo->b.users, struct wined3d_bo_user, entry)
2862 bo_user->valid = false;
2865 goto map;
2868 ERR("Failed to create new buffer object.\n");
2871 if (context_gl->c.d3d_info->fences)
2873 if (bo->command_fence_id == device_gl->current_fence_id)
2874 wined3d_context_gl_submit_command_fence(context_gl);
2875 wined3d_context_gl_wait_command_fence(context_gl, bo->command_fence_id);
2878 map:
2879 if (bo->b.map_ptr)
2880 return bo->b.map_ptr;
2882 if (bo->memory)
2884 struct wined3d_allocator_chunk_gl *chunk_gl = wined3d_allocator_chunk_gl(bo->memory->chunk);
2886 if (!(bo->b.map_ptr = wined3d_allocator_chunk_gl_map(chunk_gl, context_gl)))
2887 ERR("Failed to map chunk.\n");
2888 return bo->b.map_ptr;
2891 gl_info = context_gl->gl_info;
2892 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2894 if (gl_info->supported[ARB_BUFFER_STORAGE])
2896 GLbitfield gl_flags;
2898 /* When mapping the bo persistently, we need to use the access flags
2899 * used to create the bo, instead of the access flags passed to the
2900 * map call. Otherwise, if for example the initial map call that
2901 * caused the bo to be persistently mapped was a read-only map,
2902 * subsequent write access to the bo would be undefined.
2904 * Note that we use GL_MAP_PERSISTENT_BIT for non-persistent maps here
2905 * as well, in order to allow draws to succeed while referenced buffer
2906 * resources are mapped. On the other hand, we don't want to use the
2907 * access flags used to create the bo for non-persistent maps, because
2908 * that may imply dropping GL_MAP_UNSYNCHRONIZED_BIT. */
2909 gl_flags = bo->flags & ~GL_CLIENT_STORAGE_BIT;
2910 if (!(gl_flags & GL_MAP_READ_BIT))
2911 gl_flags |= GL_MAP_UNSYNCHRONIZED_BIT;
2912 if (gl_flags & GL_MAP_WRITE_BIT)
2913 gl_flags |= GL_MAP_FLUSH_EXPLICIT_BIT;
2914 gl_flags |= GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
2916 bo->b.map_ptr = GL_EXTCALL(glMapBufferRange(bo->binding, 0, bo->size, gl_flags));
2918 else if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2920 bo->b.map_ptr = GL_EXTCALL(glMapBufferRange(bo->binding, 0, bo->size, wined3d_resource_gl_map_flags(bo, flags)));
2922 else
2924 bo->b.map_ptr = GL_EXTCALL(glMapBuffer(bo->binding, wined3d_resource_gl_legacy_map_flags(flags)));
2927 if (bo->b.map_ptr)
2928 adapter_adjust_mapped_memory(device_gl->d.adapter, bo->size);
2930 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2931 checkGLcall("Map buffer object");
2933 return bo->b.map_ptr;
2936 static void wined3d_bo_gl_unmap(struct wined3d_bo_gl *bo, struct wined3d_context_gl *context_gl)
2938 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2940 if (context_gl->c.d3d_info->persistent_map
2941 && context_gl->c.device->adapter->mapped_size <= MAX_PERSISTENT_MAPPED_BYTES)
2943 TRACE("Not unmapping BO %p.\n", bo);
2944 return;
2947 if (bo->memory)
2949 struct wined3d_allocator_chunk_gl *chunk_gl = wined3d_allocator_chunk_gl(bo->memory->chunk);
2951 wined3d_allocator_chunk_gl_unmap(chunk_gl, context_gl);
2952 if (!chunk_gl->c.map_ptr)
2953 bo->b.map_ptr = NULL;
2954 return;
2957 wined3d_device_bo_map_lock(context_gl->c.device);
2958 /* The mapping is still in use by the client (viz. for an accelerated
2959 * NOOVERWRITE map). The client will trigger another unmap request when the
2960 * d3d application requests to unmap the BO. */
2961 if (bo->b.client_map_count)
2963 wined3d_device_bo_map_unlock(context_gl->c.device);
2964 assert(context_gl->c.d3d_info->persistent_map);
2965 TRACE("BO %p is still in use by a client thread; not unmapping.\n", bo);
2966 return;
2968 bo->b.map_ptr = NULL;
2969 wined3d_device_bo_map_unlock(context_gl->c.device);
2971 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
2972 GL_EXTCALL(glUnmapBuffer(bo->binding));
2973 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
2974 checkGLcall("Unmap buffer object");
2976 adapter_adjust_mapped_memory(context_gl->c.device->adapter, -bo->size);
2979 void *wined3d_context_gl_map_bo_address(struct wined3d_context_gl *context_gl,
2980 const struct wined3d_bo_address *data, size_t size, uint32_t flags)
2982 struct wined3d_bo *bo;
2983 void *map_ptr;
2985 if (!(bo = data->buffer_object))
2986 return data->addr;
2988 if (!(map_ptr = wined3d_bo_gl_map(wined3d_bo_gl(bo), context_gl, flags)))
2990 ERR("Failed to map bo.\n");
2991 return NULL;
2994 return (uint8_t *)map_ptr + bo->buffer_offset + (uintptr_t)data->addr;
2997 static void flush_bo_ranges(struct wined3d_context_gl *context_gl, const struct wined3d_const_bo_address *data,
2998 unsigned int range_count, const struct wined3d_range *ranges)
3000 const struct wined3d_gl_info *gl_info;
3001 struct wined3d_bo_gl *bo;
3002 unsigned int i;
3004 if (!data->buffer_object || data->buffer_object->coherent)
3005 return;
3006 bo = wined3d_bo_gl(data->buffer_object);
3008 gl_info = context_gl->gl_info;
3009 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
3011 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
3013 /* The offset passed to glFlushMappedBufferRange() is relative to the
3014 * mapped range, but we map the whole buffer anyway. */
3015 for (i = 0; i < range_count; ++i)
3017 GL_EXTCALL(glFlushMappedBufferRange(bo->binding,
3018 bo->b.buffer_offset + (uintptr_t)data->addr + ranges[i].offset, ranges[i].size));
3021 else if (gl_info->supported[APPLE_FLUSH_BUFFER_RANGE])
3023 for (i = 0; i < range_count; ++i)
3025 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(bo->binding,
3026 bo->b.buffer_offset + (uintptr_t)data->addr + ranges[i].offset, ranges[i].size));
3027 checkGLcall("glFlushMappedBufferRangeAPPLE");
3031 wined3d_context_gl_bind_bo(context_gl, bo->binding, 0);
3032 checkGLcall("Flush buffer object");
3035 void wined3d_context_gl_unmap_bo_address(struct wined3d_context_gl *context_gl,
3036 const struct wined3d_bo_address *data, unsigned int range_count, const struct wined3d_range *ranges)
3038 struct wined3d_bo_gl *bo;
3040 if (!data->buffer_object)
3041 return;
3042 bo = wined3d_bo_gl(data->buffer_object);
3044 flush_bo_ranges(context_gl, wined3d_const_bo_address(data), range_count, ranges);
3045 wined3d_bo_gl_unmap(bo, context_gl);
3048 void wined3d_context_gl_flush_bo_address(struct wined3d_context_gl *context_gl,
3049 const struct wined3d_const_bo_address *data, size_t size)
3051 struct wined3d_range range;
3053 TRACE("context_gl %p, data %s, size %Iu.\n", context_gl, debug_const_bo_address(data), size);
3055 range.offset = (uintptr_t)data->addr;
3056 range.size = size;
3058 flush_bo_ranges(context_gl, data, 1, &range);
3061 void wined3d_context_gl_copy_bo_address(struct wined3d_context_gl *context_gl,
3062 const struct wined3d_bo_address *dst, const struct wined3d_bo_address *src,
3063 unsigned int range_count, const struct wined3d_range *ranges)
3065 const struct wined3d_gl_info *gl_info;
3066 struct wined3d_bo_gl *src_bo, *dst_bo;
3067 BYTE *dst_ptr, *src_ptr;
3068 unsigned int i;
3070 gl_info = context_gl->gl_info;
3071 src_bo = src->buffer_object ? wined3d_bo_gl(src->buffer_object) : NULL;
3072 dst_bo = dst->buffer_object ? wined3d_bo_gl(dst->buffer_object) : NULL;
3074 if (dst_bo && src_bo)
3076 if (gl_info->supported[ARB_COPY_BUFFER])
3078 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER, src_bo->id));
3079 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER, dst_bo->id));
3081 for (i = 0; i < range_count; ++i)
3082 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
3083 src_bo->b.buffer_offset + (GLintptr)src->addr + ranges[i].offset,
3084 dst_bo->b.buffer_offset + (GLintptr)dst->addr + ranges[i].offset, ranges[i].size));
3085 checkGLcall("direct buffer copy");
3087 wined3d_context_gl_reference_bo(context_gl, src_bo);
3088 wined3d_context_gl_reference_bo(context_gl, dst_bo);
3090 else
3092 src_ptr = wined3d_context_gl_map_bo_address(context_gl, src,
3093 src_bo->size - (uintptr_t)src->addr, WINED3D_MAP_READ);
3094 dst_ptr = wined3d_context_gl_map_bo_address(context_gl, dst,
3095 dst_bo->size - (uintptr_t)dst->addr, WINED3D_MAP_WRITE);
3097 for (i = 0; i < range_count; ++i)
3098 memcpy(dst_ptr + ranges[i].offset, src_ptr + ranges[i].offset, ranges[i].size);
3100 wined3d_context_gl_unmap_bo_address(context_gl, dst, range_count, ranges);
3101 wined3d_context_gl_unmap_bo_address(context_gl, src, 0, NULL);
3104 else if (!dst_bo && src_bo)
3106 wined3d_context_gl_bind_bo(context_gl, src_bo->binding, src_bo->id);
3107 for (i = 0; i < range_count; ++i)
3108 GL_EXTCALL(glGetBufferSubData(src_bo->binding,
3109 src_bo->b.buffer_offset + (GLintptr)src->addr + ranges[i].offset,
3110 ranges[i].size, dst->addr + ranges[i].offset));
3111 checkGLcall("buffer download");
3113 wined3d_context_gl_reference_bo(context_gl, src_bo);
3115 else if (dst_bo && !src_bo)
3117 wined3d_context_gl_bind_bo(context_gl, dst_bo->binding, dst_bo->id);
3118 for (i = 0; i < range_count; ++i)
3119 GL_EXTCALL(glBufferSubData(dst_bo->binding,
3120 dst_bo->b.buffer_offset + (GLintptr)dst->addr + ranges[i].offset,
3121 ranges[i].size, src->addr + ranges[i].offset));
3122 checkGLcall("buffer upload");
3124 wined3d_context_gl_reference_bo(context_gl, dst_bo);
3126 else
3128 for (i = 0; i < range_count; ++i)
3129 memcpy(dst->addr + ranges[i].offset, src->addr + ranges[i].offset, ranges[i].size);
3133 void wined3d_context_gl_destroy_bo(struct wined3d_context_gl *context_gl, struct wined3d_bo_gl *bo)
3135 struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
3136 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3138 TRACE("context_gl %p, bo %p.\n", context_gl, bo);
3140 if (bo->memory)
3142 unsigned int order = bo->memory->order;
3144 if (bo->b.map_ptr)
3145 wined3d_allocator_chunk_gl_unmap(wined3d_allocator_chunk_gl(bo->memory->chunk), context_gl);
3146 wined3d_context_gl_destroy_allocator_block(context_gl, bo->memory, bo->command_fence_id);
3148 if (bo->command_fence_id == device_gl->current_fence_id)
3150 device_gl->retired_bo_size += WINED3D_ALLOCATOR_CHUNK_SIZE >> order;
3151 if (device_gl->retired_bo_size > WINED3D_RETIRED_BO_SIZE_THRESHOLD)
3152 wined3d_context_gl_submit_command_fence(context_gl);
3155 bo->id = 0;
3156 return;
3159 if (bo->b.map_ptr)
3161 wined3d_context_gl_bind_bo(context_gl, bo->binding, bo->id);
3162 GL_EXTCALL(glUnmapBuffer(bo->binding));
3163 adapter_adjust_mapped_memory(context_gl->c.device->adapter, -bo->size);
3166 TRACE("Destroying GL buffer %u.\n", bo->id);
3168 GL_EXTCALL(glDeleteBuffers(1, &bo->id));
3169 checkGLcall("buffer object destruction");
3170 bo->id = 0;
3173 static void wined3d_context_gl_set_render_offscreen(struct wined3d_context_gl *context_gl, BOOL offscreen)
3175 if (context_gl->c.render_offscreen == offscreen)
3176 return;
3178 context_invalidate_state(&context_gl->c, STATE_VIEWPORT);
3179 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
3180 if (!context_gl->gl_info->supported[ARB_CLIP_CONTROL])
3182 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3183 context_invalidate_state(&context_gl->c, STATE_POINTSPRITECOORDORIGIN);
3184 context_invalidate_state(&context_gl->c, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
3186 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN));
3187 if (context_gl->gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
3188 context_invalidate_state(&context_gl->c, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
3189 context_gl->c.render_offscreen = offscreen;
3192 GLenum wined3d_context_gl_get_offscreen_gl_buffer(const struct wined3d_context_gl *context_gl)
3194 switch (wined3d_settings.offscreen_rendering_mode)
3196 case ORM_FBO:
3197 return GL_COLOR_ATTACHMENT0;
3199 case ORM_BACKBUFFER:
3200 return context_gl->aux_buffers > 0 ? GL_AUX0 : GL_BACK;
3202 default:
3203 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
3204 return GL_BACK;
3208 static uint32_t wined3d_context_gl_generate_rt_mask_no_fbo(const struct wined3d_context_gl *context_gl,
3209 struct wined3d_resource *rt)
3211 if (!rt || rt->format->id == WINED3DFMT_NULL)
3212 return 0;
3213 else if (rt->type != WINED3D_RTYPE_BUFFER && texture_from_resource(rt)->swapchain)
3214 return context_generate_rt_mask_from_resource(rt);
3215 else
3216 return context_generate_rt_mask(wined3d_context_gl_get_offscreen_gl_buffer(context_gl));
3219 /* Context activation is done by the caller. */
3220 void wined3d_context_gl_apply_blit_state(struct wined3d_context_gl *context_gl, const struct wined3d_device *device)
3222 struct wined3d_context *context = &context_gl->c;
3223 const struct wined3d_gl_info *gl_info;
3224 uint32_t rt_mask, *cur_mask;
3225 struct wined3d_texture *rt;
3226 unsigned int sampler;
3227 SIZE rt_size;
3229 TRACE("Setting up context %p for blitting.\n", context);
3231 gl_info = context_gl->gl_info;
3232 rt = context->current_rt.texture;
3234 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3236 if (context->render_offscreen)
3238 wined3d_texture_load(rt, context, FALSE);
3240 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_FRAMEBUFFER, &rt->resource,
3241 context->current_rt.sub_resource_idx, NULL, 0, rt->resource.draw_binding);
3242 if (rt->resource.format->id != WINED3DFMT_NULL)
3243 rt_mask = 1;
3244 else
3245 rt_mask = 0;
3247 else
3249 context_gl->current_fbo = NULL;
3250 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
3251 rt_mask = context_generate_rt_mask_from_resource(&rt->resource);
3254 else
3256 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, &rt->resource);
3259 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3261 if (rt_mask != *cur_mask)
3263 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3264 *cur_mask = rt_mask;
3267 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3268 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3269 context_invalidate_state(context, STATE_FRAMEBUFFER);
3271 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
3273 if (context->last_was_blit)
3275 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
3277 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
3278 context->viewport_count = WINED3D_MAX_VIEWPORTS;
3279 context_gl->blit_size = rt_size;
3280 /* No need to dirtify here, the states are still dirtified because
3281 * they weren't applied since the last context_apply_blit_state()
3282 * call. */
3284 checkGLcall("blit state application");
3285 TRACE("Context is already set up for blitting, nothing to do.\n");
3286 return;
3288 context->last_was_blit = TRUE;
3290 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
3291 GL_EXTCALL(glBindSampler(0, 0));
3292 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
3294 sampler = context_gl->rev_tex_unit_map[0];
3295 if (sampler != WINED3D_UNMAPPED_STAGE)
3297 if (sampler < WINED3D_MAX_TEXTURES)
3299 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
3300 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
3302 context_invalidate_state(context, STATE_SAMPLER(sampler));
3304 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
3305 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
3307 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3309 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
3310 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
3312 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3313 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3314 context_invalidate_state(context, STATE_BLEND);
3315 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
3316 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
3317 context_invalidate_state(context, STATE_RASTERIZER);
3318 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
3319 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
3320 context_invalidate_state(context, STATE_DEPTH_STENCIL);
3321 if (gl_info->supported[ARB_POINT_SPRITE])
3323 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
3324 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
3326 if (gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3328 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3329 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3332 context->last_was_rhw = TRUE;
3333 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
3335 wined3d_context_gl_enable_clip_distances(context_gl, 0);
3336 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
3338 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
3339 if (gl_info->supported[ARB_CLIP_CONTROL])
3340 GL_EXTCALL(glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE));
3341 gl_info->gl_ops.gl.p_glViewport(0, 0, rt_size.cx, rt_size.cy);
3342 context->viewport_count = WINED3D_MAX_VIEWPORTS;
3343 context_invalidate_state(context, STATE_VIEWPORT);
3345 device->shader_backend->shader_disable(device->shader_priv, context);
3347 context_gl->blit_size = rt_size;
3349 checkGLcall("blit state application");
3352 static void wined3d_context_gl_apply_blit_projection(const struct wined3d_context_gl *context_gl,
3353 unsigned int w, unsigned int h)
3355 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3356 const GLdouble projection[] =
3358 2.0 / w, 0.0, 0.0, 0.0,
3359 0.0, 2.0 / h, 0.0, 0.0,
3360 0.0, 0.0, 2.0, 0.0,
3361 -1.0, -1.0, -1.0, 1.0,
3364 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
3365 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
3368 /* Setup OpenGL states for fixed-function blitting. */
3369 /* Context activation is done by the caller. */
3370 void wined3d_context_gl_apply_ffp_blit_state(struct wined3d_context_gl *context_gl,
3371 const struct wined3d_device *device)
3373 struct wined3d_context *context = &context_gl->c;
3374 const struct wined3d_gl_info *gl_info;
3375 unsigned int i, sampler;
3377 gl_info = context_gl->gl_info;
3378 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
3379 ERR("Applying fixed-function state without legacy context support.\n");
3381 if (context->last_was_ffp_blit)
3383 SIZE rt_size;
3385 wined3d_context_gl_get_rt_size(context_gl, &rt_size);
3386 if (context_gl->blit_size.cx != rt_size.cx || context_gl->blit_size.cy != rt_size.cy)
3387 wined3d_context_gl_apply_blit_projection(context_gl, rt_size.cx, rt_size.cy);
3388 wined3d_context_gl_apply_blit_state(context_gl, device);
3390 checkGLcall("ffp blit state application");
3391 return;
3393 context->last_was_ffp_blit = TRUE;
3395 wined3d_context_gl_apply_blit_state(context_gl, device);
3397 /* Disable all textures. The caller can then bind a texture it wants to blit
3398 * from. */
3399 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
3401 wined3d_context_gl_active_texture(context_gl, gl_info, i);
3403 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3404 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3405 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3406 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3407 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3408 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3410 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3412 sampler = context_gl->rev_tex_unit_map[i];
3413 if (sampler != WINED3D_UNMAPPED_STAGE)
3415 if (sampler < WINED3D_MAX_TEXTURES)
3416 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
3417 context_invalidate_state(context, STATE_SAMPLER(sampler));
3421 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
3423 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
3424 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3425 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
3426 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3427 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
3428 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
3430 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3431 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
3432 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT, GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
3434 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
3435 gl_info->gl_ops.gl.p_glLoadIdentity();
3437 /* Setup transforms. */
3438 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
3439 gl_info->gl_ops.gl.p_glLoadIdentity();
3440 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3441 wined3d_context_gl_apply_blit_projection(context_gl, context_gl->blit_size.cx, context_gl->blit_size.cy);
3442 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
3444 /* Other misc states. */
3445 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
3446 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
3447 gl_info->p_glDisableWINE(GL_FOG);
3448 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
3450 if (gl_info->supported[EXT_SECONDARY_COLOR])
3452 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
3453 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
3455 checkGLcall("ffp blit state application");
3458 static BOOL have_framebuffer_attachment(unsigned int rt_count, struct wined3d_rendertarget_view * const *rts,
3459 const struct wined3d_rendertarget_view *ds)
3461 unsigned int i;
3463 if (ds)
3464 return TRUE;
3466 for (i = 0; i < rt_count; ++i)
3468 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3469 return TRUE;
3472 return FALSE;
3475 /* Context activation is done by the caller. */
3476 BOOL wined3d_context_gl_apply_clear_state(struct wined3d_context_gl *context_gl,
3477 const struct wined3d_state *state, unsigned int rt_count, const struct wined3d_fb_state *fb)
3479 struct wined3d_rendertarget_view * const *rts = fb->render_targets;
3480 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3481 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
3482 uint32_t rt_mask = 0, *cur_mask;
3483 unsigned int i;
3485 if (isStateDirty(&context_gl->c, STATE_FRAMEBUFFER) || fb != &state->fb
3486 || rt_count != gl_info->limits.buffers)
3488 if (!have_framebuffer_attachment(rt_count, rts, dsv))
3490 WARN("Invalid render target config, need at least one attachment.\n");
3491 return FALSE;
3494 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3496 struct wined3d_rendertarget_info ds_info = {{0}};
3498 if (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource))
3500 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3501 for (i = 0; i < rt_count; ++i)
3503 if (rts[i])
3505 struct wined3d_rendertarget_view_gl *rtv_gl = wined3d_rendertarget_view_gl(rts[i]);
3506 context_gl->blit_targets[i].gl_view = rtv_gl->gl_view;
3507 context_gl->blit_targets[i].resource = rtv_gl->v.resource;
3508 context_gl->blit_targets[i].sub_resource_idx = rtv_gl->v.sub_resource_idx;
3509 context_gl->blit_targets[i].layer_count = rtv_gl->v.layer_count;
3511 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3512 rt_mask |= (1u << i);
3515 if (dsv)
3517 struct wined3d_rendertarget_view_gl *dsv_gl = wined3d_rendertarget_view_gl(dsv);
3518 ds_info.gl_view = dsv_gl->gl_view;
3519 ds_info.resource = dsv_gl->v.resource;
3520 ds_info.sub_resource_idx = dsv_gl->v.sub_resource_idx;
3521 ds_info.layer_count = dsv_gl->v.layer_count;
3524 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3525 rt_count ? rts[0]->resource->draw_binding : 0, dsv ? dsv->resource->draw_binding : 0);
3527 else
3529 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3530 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3531 rt_mask = context_generate_rt_mask_from_resource(rts[0]->resource);
3534 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3535 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3536 * state management allows this */
3537 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3539 else
3541 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3544 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3545 && (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource)))
3547 for (i = 0; i < rt_count; ++i)
3549 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
3550 rt_mask |= (1u << i);
3553 else
3555 rt_mask = wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rt_count ? rts[0]->resource : NULL);
3558 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3560 if (rt_mask != *cur_mask)
3562 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3563 *cur_mask = rt_mask;
3564 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3567 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3568 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
3570 context_gl->c.last_was_blit = FALSE;
3571 context_gl->c.last_was_ffp_blit = FALSE;
3573 /* Blending and clearing should be orthogonal, but tests on the nvidia
3574 * driver show that disabling blending when clearing improves the clearing
3575 * performance incredibly. */
3576 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3577 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
3578 if (rt_count && gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3580 if (needs_srgb_write(context_gl->c.d3d_info, state, fb))
3581 gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
3582 else
3583 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3584 context_invalidate_state(&context_gl->c, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3586 checkGLcall("setting up state for clear");
3588 context_invalidate_state(&context_gl->c, STATE_BLEND);
3589 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3590 context_invalidate_state(&context_gl->c, STATE_SCISSORRECT);
3592 return TRUE;
3595 static uint32_t find_draw_buffers_mask(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3597 struct wined3d_rendertarget_view * const *rts = state->fb.render_targets;
3598 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3599 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3600 unsigned int rt_mask, mask;
3601 unsigned int i;
3603 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3604 return wined3d_context_gl_generate_rt_mask_no_fbo(context_gl, rts[0]->resource);
3605 else if (!context_gl->c.render_offscreen)
3606 return context_generate_rt_mask_from_resource(rts[0]->resource);
3608 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
3609 rt_mask &= wined3d_mask_from_size(gl_info->limits.buffers);
3610 if (state->blend_state && state->blend_state->dual_source)
3611 rt_mask = 1;
3613 mask = rt_mask;
3614 while (mask)
3616 i = wined3d_bit_scan(&mask);
3617 if (!rts[i] || rts[i]->format->id == WINED3DFMT_NULL)
3618 rt_mask &= ~(1u << i);
3621 return rt_mask;
3624 void context_gl_apply_texture_draw_state(struct wined3d_context_gl *context_gl,
3625 struct wined3d_texture *texture, unsigned int sub_resource_idx, unsigned int location)
3627 const struct wined3d_format *format = texture->resource.format;
3628 GLenum buffer;
3630 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3631 return;
3633 if (format->depth_size || format->stencil_size)
3635 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
3636 NULL, 0, &texture->resource, sub_resource_idx, location);
3638 buffer = GL_NONE;
3640 else
3642 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
3643 &texture->resource, sub_resource_idx, NULL, 0, location);
3645 if (location == WINED3D_LOCATION_DRAWABLE)
3647 TRACE("Texture %p is onscreen.\n", texture);
3648 buffer = wined3d_texture_get_gl_buffer(texture);
3650 else
3652 TRACE("Texture %p is offscreen.\n", texture);
3653 buffer = GL_COLOR_ATTACHMENT0;
3657 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
3658 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
3659 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
3662 /* Context activation is done by the caller. */
3663 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3665 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3666 uint32_t rt_mask = find_draw_buffers_mask(context_gl, state);
3667 const struct wined3d_fb_state *fb = &state->fb;
3668 DWORD color_location = 0;
3669 DWORD *cur_mask;
3671 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3673 struct wined3d_rendertarget_info ds_info = {{0}};
3675 if (!context->render_offscreen)
3677 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, NULL, &ds_info,
3678 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3680 else
3682 const struct wined3d_rendertarget_view_gl *view_gl;
3683 unsigned int i;
3685 memset(context_gl->blit_targets, 0, sizeof(context_gl->blit_targets));
3686 for (i = 0; i < context_gl->gl_info->limits.buffers; ++i)
3688 if (!fb->render_targets[i])
3689 continue;
3691 view_gl = wined3d_rendertarget_view_gl(fb->render_targets[i]);
3692 context_gl->blit_targets[i].gl_view = view_gl->gl_view;
3693 context_gl->blit_targets[i].resource = view_gl->v.resource;
3694 context_gl->blit_targets[i].sub_resource_idx = view_gl->v.sub_resource_idx;
3695 context_gl->blit_targets[i].layer_count = view_gl->v.layer_count;
3697 if (!color_location)
3698 color_location = view_gl->v.resource->draw_binding;
3701 if (fb->depth_stencil)
3703 view_gl = wined3d_rendertarget_view_gl(fb->depth_stencil);
3704 ds_info.gl_view = view_gl->gl_view;
3705 ds_info.resource = view_gl->v.resource;
3706 ds_info.sub_resource_idx = view_gl->v.sub_resource_idx;
3707 ds_info.layer_count = view_gl->v.layer_count;
3710 wined3d_context_gl_apply_fbo_state(context_gl, GL_FRAMEBUFFER, context_gl->blit_targets, &ds_info,
3711 color_location, fb->depth_stencil ? fb->depth_stencil->resource->draw_binding : 0);
3715 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3716 if (rt_mask != *cur_mask)
3718 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3719 *cur_mask = rt_mask;
3721 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
3724 static void wined3d_context_gl_map_stage(struct wined3d_context_gl *context_gl, unsigned int stage, unsigned int unit)
3726 unsigned int i = context_gl->rev_tex_unit_map[unit];
3727 unsigned int j = context_gl->tex_unit_map[stage];
3729 TRACE("Mapping stage %u to unit %u.\n", stage, unit);
3730 context_gl->tex_unit_map[stage] = unit;
3731 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
3732 context_gl->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
3734 context_gl->rev_tex_unit_map[unit] = stage;
3735 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
3736 context_gl->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
3739 static void context_invalidate_texture_stage(struct wined3d_context *context, DWORD stage)
3741 DWORD i;
3743 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
3744 context_invalidate_state(context, STATE_TEXTURESTAGE(stage, i));
3747 static void context_update_fixed_function_usage_map(struct wined3d_context *context,
3748 const struct wined3d_state *state)
3750 UINT i, start, end;
3752 context->fixed_function_usage_map = 0;
3753 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
3755 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
3756 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
3757 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
3758 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
3759 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
3760 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
3761 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
3762 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
3764 /* Not used, and disable higher stages. */
3765 if (color_op == WINED3D_TOP_DISABLE)
3766 break;
3768 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
3769 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
3770 || ((color_arg3 == WINED3DTA_TEXTURE)
3771 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
3772 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
3773 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
3774 || ((alpha_arg3 == WINED3DTA_TEXTURE)
3775 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
3776 context->fixed_function_usage_map |= (1u << i);
3778 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
3779 && i < WINED3D_MAX_TEXTURES - 1)
3780 context->fixed_function_usage_map |= (1u << (i + 1));
3783 if (i < context->lowest_disabled_stage)
3785 start = i;
3786 end = context->lowest_disabled_stage;
3788 else
3790 start = context->lowest_disabled_stage;
3791 end = i;
3794 context->lowest_disabled_stage = i;
3795 for (i = start + 1; i < end; ++i)
3797 context_invalidate_state(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3801 static void wined3d_context_gl_map_fixed_function_samplers(struct wined3d_context_gl *context_gl,
3802 const struct wined3d_state *state)
3804 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3805 unsigned int i, tex;
3806 uint32_t ffu_map;
3808 ffu_map = context_gl->c.fixed_function_usage_map;
3810 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
3811 || context_gl->c.lowest_disabled_stage <= d3d_info->limits.ffp_textures)
3813 while (ffu_map)
3815 i = wined3d_bit_scan(&ffu_map);
3816 if (context_gl->tex_unit_map[i] != i)
3818 wined3d_context_gl_map_stage(context_gl, i, i);
3819 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3820 context_invalidate_texture_stage(&context_gl->c, i);
3823 return;
3826 /* Now work out the mapping */
3827 tex = 0;
3828 while (ffu_map)
3830 i = wined3d_bit_scan(&ffu_map);
3831 if (context_gl->tex_unit_map[i] != tex)
3833 wined3d_context_gl_map_stage(context_gl, i, tex);
3834 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3835 context_invalidate_texture_stage(&context_gl->c, i);
3838 ++tex;
3842 static void wined3d_context_gl_map_psamplers(struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
3844 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
3845 const struct wined3d_shader_resource_info *resource_info =
3846 state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3847 unsigned int i;
3849 for (i = 0; i < WINED3D_MAX_FRAGMENT_SAMPLERS; ++i)
3851 if (resource_info[i].type && context_gl->tex_unit_map[i] != i)
3853 wined3d_context_gl_map_stage(context_gl, i, i);
3854 context_invalidate_state(&context_gl->c, STATE_SAMPLER(i));
3855 if (i < d3d_info->limits.ffp_blend_stages)
3856 context_invalidate_texture_stage(&context_gl->c, i);
3861 static BOOL wined3d_context_gl_unit_free_for_vs(const struct wined3d_context_gl *context_gl,
3862 const struct wined3d_shader_resource_info *ps_resource_info, unsigned int unit)
3864 unsigned int current_mapping = context_gl->rev_tex_unit_map[unit];
3866 /* Not currently used */
3867 if (current_mapping == WINED3D_UNMAPPED_STAGE)
3868 return TRUE;
3870 if (current_mapping < WINED3D_MAX_FRAGMENT_SAMPLERS)
3872 /* Used by a fragment sampler */
3874 if (!ps_resource_info)
3876 /* No pixel shader, check fixed function */
3877 return current_mapping >= WINED3D_MAX_TEXTURES
3878 || !(context_gl->c.fixed_function_usage_map & (1u << current_mapping));
3881 /* Pixel shader, check the shader's sampler map */
3882 return !ps_resource_info[current_mapping].type;
3885 return TRUE;
3888 static void wined3d_context_gl_map_vsamplers(struct wined3d_context_gl *context_gl,
3889 BOOL ps, const struct wined3d_state *state)
3891 const struct wined3d_shader_resource_info *vs_resource_info =
3892 state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info;
3893 const struct wined3d_shader_resource_info *ps_resource_info = NULL;
3894 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3895 int start = min(WINED3D_MAX_COMBINED_SAMPLERS, gl_info->limits.graphics_samplers) - 1;
3896 int i;
3898 /* Note that we only care if a resource is used or not, not the
3899 * resource's specific type. Otherwise we'd need to call
3900 * shader_update_samplers() here for 1.x pixelshaders. */
3901 if (ps)
3902 ps_resource_info = state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3904 for (i = 0; i < WINED3D_MAX_VERTEX_SAMPLERS; ++i)
3906 DWORD vsampler_idx = i + WINED3D_MAX_FRAGMENT_SAMPLERS;
3907 if (vs_resource_info[i].type)
3909 while (start >= 0)
3911 if (wined3d_context_gl_unit_free_for_vs(context_gl, ps_resource_info, start))
3913 if (context_gl->tex_unit_map[vsampler_idx] != start)
3915 wined3d_context_gl_map_stage(context_gl, vsampler_idx, start);
3916 context_invalidate_state(&context_gl->c, STATE_SAMPLER(vsampler_idx));
3919 --start;
3920 break;
3923 --start;
3925 if (context_gl->tex_unit_map[vsampler_idx] == WINED3D_UNMAPPED_STAGE)
3926 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i);
3931 static void wined3d_context_gl_update_tex_unit_map(struct wined3d_context_gl *context_gl,
3932 const struct wined3d_state *state)
3934 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3935 BOOL vs = use_vs(state);
3936 BOOL ps = use_ps(state);
3938 if (!ps)
3939 context_update_fixed_function_usage_map(&context_gl->c, state);
3941 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3942 * need a 1:1 map at the moment.
3943 * When the mapping of a stage is changed, sampler and ALL texture stage
3944 * states have to be reset. */
3946 if (gl_info->limits.graphics_samplers >= WINED3D_MAX_COMBINED_SAMPLERS)
3947 return;
3949 if (ps)
3950 wined3d_context_gl_map_psamplers(context_gl, state);
3951 else
3952 wined3d_context_gl_map_fixed_function_samplers(context_gl, state);
3954 if (vs)
3955 wined3d_context_gl_map_vsamplers(context_gl, ps, state);
3958 /* Context activation is done by the caller. */
3959 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3961 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3962 uint32_t rt_mask, *cur_mask;
3964 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
3966 cur_mask = context_gl->current_fbo ? &context_gl->current_fbo->rt_mask : &context_gl->draw_buffers_mask;
3967 rt_mask = find_draw_buffers_mask(context_gl, state);
3968 if (rt_mask != *cur_mask)
3970 wined3d_context_gl_apply_draw_buffers(context_gl, rt_mask);
3971 *cur_mask = rt_mask;
3975 static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl *context_gl,
3976 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
3978 unsigned int bind_idx, shader_sampler_count, base, count, i;
3979 const struct wined3d_device *device = context_gl->c.device;
3980 struct wined3d_shader_sampler_map_entry *entry;
3981 struct wined3d_shader_resource_view *view;
3982 const struct wined3d_shader *shader;
3983 const unsigned int *tex_unit_map;
3984 struct wined3d_sampler *sampler;
3986 if (!(shader = state->shader[shader_type]))
3987 return;
3989 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl,
3990 &shader->reg_maps.shader_version, &base, &count);
3992 shader_sampler_count = shader->reg_maps.sampler_map.count;
3993 if (shader_sampler_count > count)
3994 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3995 shader, shader_sampler_count, count);
3996 count = min(shader_sampler_count, count);
3998 for (i = 0; i < count; ++i)
4000 entry = &shader->reg_maps.sampler_map.entries[i];
4001 bind_idx = base + entry->bind_idx;
4002 if (tex_unit_map)
4003 bind_idx = tex_unit_map[bind_idx];
4005 if (!(view = state->shader_resource_view[shader_type][entry->resource_idx]))
4007 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
4008 continue;
4011 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
4012 sampler = device->default_sampler;
4013 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
4014 sampler = device->null_sampler;
4015 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view),
4016 bind_idx, wined3d_sampler_gl(sampler), context_gl);
4020 static void wined3d_context_gl_bind_unordered_access_views(struct wined3d_context_gl *context_gl,
4021 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
4023 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4024 struct wined3d_unordered_access_view_gl *view_gl;
4025 const struct wined3d_format_gl *format_gl;
4026 GLuint texture_name;
4027 unsigned int i;
4028 GLint level;
4030 if (!shader)
4031 return;
4033 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
4035 if (!views[i])
4037 if (shader->reg_maps.uav_resource_info[i].type)
4038 WARN("No unordered access view bound at index %u.\n", i);
4039 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
4040 continue;
4043 view_gl = wined3d_unordered_access_view_gl(views[i]);
4044 if (view_gl->gl_view.name)
4046 texture_name = view_gl->gl_view.name;
4047 level = 0;
4049 else if (view_gl->v.resource->type != WINED3D_RTYPE_BUFFER)
4051 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(view_gl->v.resource));
4052 texture_name = wined3d_texture_gl_get_texture_name(texture_gl, &context_gl->c, FALSE);
4053 level = view_gl->v.desc.u.texture.level_idx;
4055 else
4057 FIXME("Unsupported buffer unordered access view.\n");
4058 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
4059 continue;
4062 format_gl = wined3d_format_gl(view_gl->v.format);
4063 GL_EXTCALL(glBindImageTexture(i, texture_name, level, GL_TRUE, 0, GL_READ_WRITE,
4064 format_gl->internal));
4066 if (view_gl->counter_bo.id)
4067 GL_EXTCALL(glBindBufferRange(GL_ATOMIC_COUNTER_BUFFER, i, view_gl->counter_bo.id,
4068 view_gl->counter_bo.b.buffer_offset, view_gl->counter_bo.size));
4070 checkGLcall("Bind unordered access views");
4073 static void context_gl_load_shader_resources(struct wined3d_context_gl *context_gl,
4074 const struct wined3d_state *state, unsigned int shader_mask)
4076 struct wined3d_shader_sampler_map_entry *entry;
4077 struct wined3d_shader_resource_view_gl *srv_gl;
4078 struct wined3d_shader_resource_view *view;
4079 struct wined3d_shader *shader;
4080 struct wined3d_buffer *buffer;
4081 unsigned int i, j;
4083 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
4085 if (!(shader_mask & (1u << i)))
4086 continue;
4088 if (!(shader = state->shader[i]))
4089 continue;
4091 for (j = 0; j < WINED3D_MAX_CBS; ++j)
4093 if (!state->cb[i][j].buffer)
4094 continue;
4096 buffer = state->cb[i][j].buffer;
4097 wined3d_buffer_load(buffer, &context_gl->c, state);
4098 wined3d_context_gl_reference_buffer(context_gl, buffer);
4099 if (!buffer->bo_user.valid)
4100 device_invalidate_state(context_gl->c.device, STATE_CONSTANT_BUFFER(i));
4103 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
4105 entry = &shader->reg_maps.sampler_map.entries[j];
4107 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
4108 continue;
4110 if (view->resource->type == WINED3D_RTYPE_BUFFER)
4112 buffer = buffer_from_resource(view->resource);
4113 wined3d_buffer_load(buffer, &context_gl->c, state);
4114 wined3d_context_gl_reference_buffer(context_gl, buffer);
4116 srv_gl = wined3d_shader_resource_view_gl(view);
4117 if (!srv_gl->bo_user.valid)
4118 wined3d_shader_resource_view_gl_update(srv_gl, context_gl);
4120 else
4122 wined3d_texture_load(texture_from_resource(view->resource), &context_gl->c, FALSE);
4128 static void context_gl_load_unordered_access_resources(struct wined3d_context_gl *context_gl,
4129 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
4131 struct wined3d_unordered_access_view_gl *uav_gl;
4132 struct wined3d_unordered_access_view *view;
4133 struct wined3d_texture *texture;
4134 struct wined3d_buffer *buffer;
4135 unsigned int i;
4137 context_gl->c.uses_uavs = 0;
4139 if (!shader)
4140 return;
4142 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
4144 if (!(view = views[i]))
4145 continue;
4147 if (view->resource->type == WINED3D_RTYPE_BUFFER)
4149 buffer = buffer_from_resource(view->resource);
4150 wined3d_buffer_load_location(buffer, &context_gl->c, WINED3D_LOCATION_BUFFER);
4151 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
4152 wined3d_context_gl_reference_buffer(context_gl, buffer);
4154 uav_gl = wined3d_unordered_access_view_gl(view);
4155 if (!uav_gl->bo_user.valid)
4156 wined3d_unordered_access_view_gl_update(uav_gl, context_gl);
4158 else
4160 texture = texture_from_resource(view->resource);
4161 wined3d_texture_load(texture, &context_gl->c, FALSE);
4162 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_TEXTURE_RGB);
4165 context_gl->c.uses_uavs = 1;
4169 static void context_gl_load_stream_output_buffers(struct wined3d_context_gl *context_gl,
4170 const struct wined3d_state *state)
4172 struct wined3d_buffer *buffer;
4173 unsigned int i;
4175 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
4177 if (!(buffer = state->stream_output[i].buffer))
4178 continue;
4180 wined3d_buffer_load(buffer, &context_gl->c, state);
4181 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
4182 wined3d_context_gl_reference_buffer(context_gl, buffer);
4183 if (!buffer->bo_user.valid)
4184 device_invalidate_state(context_gl->c.device, STATE_STREAM_OUTPUT);
4188 /* Context activation is done by the caller. */
4189 static BOOL context_apply_draw_state(struct wined3d_context *context,
4190 const struct wined3d_device *device, const struct wined3d_state *state, BOOL indexed)
4192 const struct wined3d_state_entry *state_table = context->state_table;
4193 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
4194 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4195 const struct wined3d_fb_state *fb = &state->fb;
4196 unsigned int i, base;
4197 uint32_t map;
4199 context->uses_fbo_attached_resources = 0;
4201 if (!have_framebuffer_attachment(gl_info->limits.buffers, fb->render_targets, fb->depth_stencil))
4203 if (!gl_info->supported[ARB_FRAMEBUFFER_NO_ATTACHMENTS])
4205 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
4206 return FALSE;
4209 wined3d_context_gl_set_render_offscreen(context_gl, TRUE);
4212 /* Preload resources before FBO setup. Texture preload in particular may
4213 * result in changes to the current FBO, due to using e.g. FBO blits for
4214 * updating a resource location. */
4215 wined3d_context_gl_update_tex_unit_map(context_gl, state);
4216 context_preload_textures(context, state);
4217 context_gl_load_shader_resources(context_gl, state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
4218 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_PIXEL],
4219 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
4220 context_gl_load_stream_output_buffers(context_gl, state);
4221 /* TODO: Right now the dependency on the vertex shader is necessary
4222 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
4223 * the current VS but maybe it's possible to relax the coupling in some
4224 * situations at least. */
4225 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC)
4226 || isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
4228 context_update_stream_info(context, state);
4231 map = context->stream_info.use_map;
4232 while (map)
4234 const struct wined3d_stream_info_element *e;
4235 struct wined3d_buffer *buffer;
4237 e = &context->stream_info.elements[wined3d_bit_scan(&map)];
4238 buffer = state->streams[e->stream_idx].buffer;
4240 if (!buffer->bo_user.valid)
4241 device_invalidate_state(device, STATE_STREAMSRC);
4242 else
4243 wined3d_buffer_load(buffer, context, state);
4245 /* Loading the buffers above may have invalidated the stream info. */
4246 if (wined3d_context_is_graphics_state_dirty(context, STATE_STREAMSRC))
4247 context_update_stream_info(context, state);
4249 map = context->stream_info.use_map;
4250 while (map)
4252 const struct wined3d_stream_info_element *e;
4253 struct wined3d_buffer *buffer;
4255 e = &context->stream_info.elements[wined3d_bit_scan(&map)];
4256 buffer = state->streams[e->stream_idx].buffer;
4258 wined3d_context_gl_reference_buffer(context_gl, buffer);
4261 if (indexed && state->index_buffer)
4263 struct wined3d_buffer *buffer = state->index_buffer;
4265 if (context->stream_info.all_vbo)
4267 wined3d_buffer_load(buffer, context, state);
4268 if (!buffer->bo_user.valid)
4269 device_invalidate_state(device, STATE_INDEXBUFFER);
4270 wined3d_context_gl_reference_buffer(context_gl, buffer);
4272 else
4274 wined3d_buffer_load_sysmem(buffer, context);
4278 for (i = 0, base = 0; i < ARRAY_SIZE(context->dirty_graphics_states); ++i)
4280 uint32_t dirty_mask = context->dirty_graphics_states[i];
4282 while (dirty_mask)
4284 unsigned int state_id = base + wined3d_bit_scan(&dirty_mask);
4286 state_table[state_id].apply(context, state, state_id);
4288 base += sizeof(dirty_mask) * CHAR_BIT;
4291 memset(context->dirty_graphics_states, 0, sizeof(context->dirty_graphics_states));
4293 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
4295 device->shader_backend->shader_select(device->shader_priv, context, state);
4296 context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
4299 if (context->constant_update_mask)
4301 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
4302 context->constant_update_mask = 0;
4305 if (context->update_shader_resource_bindings)
4307 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
4308 wined3d_context_gl_bind_shader_resources(context_gl, state, i);
4309 context->update_shader_resource_bindings = 0;
4310 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4311 context->update_compute_shader_resource_bindings = 1;
4314 if (context->update_unordered_access_view_bindings)
4316 wined3d_context_gl_bind_unordered_access_views(context_gl,
4317 state->shader[WINED3D_SHADER_TYPE_PIXEL],
4318 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
4319 context->update_unordered_access_view_bindings = 0;
4320 context->update_compute_unordered_access_view_bindings = 1;
4323 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4324 wined3d_context_gl_check_fbo_status(context_gl, GL_FRAMEBUFFER);
4326 context->last_was_blit = FALSE;
4327 context->last_was_ffp_blit = FALSE;
4329 return TRUE;
4332 static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl *context_gl,
4333 const struct wined3d_device *device, const struct wined3d_state *state)
4335 const struct wined3d_state_entry *state_table = context_gl->c.state_table;
4336 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4337 unsigned int state_id, i;
4339 context_gl_load_shader_resources(context_gl, state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
4340 context_gl_load_unordered_access_resources(context_gl, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4341 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4343 for (i = 0, state_id = STATE_COMPUTE_OFFSET; i < ARRAY_SIZE(context_gl->c.dirty_compute_states); ++i)
4345 unsigned int dirty_mask = context_gl->c.dirty_compute_states[i];
4347 while (dirty_mask)
4349 unsigned int current_state_id = state_id + wined3d_bit_scan(&dirty_mask);
4350 state_table[current_state_id].apply(&context_gl->c, state, current_state_id);
4352 state_id += sizeof(*context_gl->c.dirty_compute_states) * CHAR_BIT;
4354 memset(context_gl->c.dirty_compute_states, 0, sizeof(*context_gl->c.dirty_compute_states));
4356 if (context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
4358 device->shader_backend->shader_select_compute(device->shader_priv, &context_gl->c, state);
4359 context_gl->c.shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
4362 if (context_gl->c.update_compute_shader_resource_bindings)
4364 wined3d_context_gl_bind_shader_resources(context_gl, state, WINED3D_SHADER_TYPE_COMPUTE);
4365 context_gl->c.update_compute_shader_resource_bindings = 0;
4366 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
4367 context_gl->c.update_shader_resource_bindings = 1;
4370 if (context_gl->c.update_compute_unordered_access_view_bindings)
4372 wined3d_context_gl_bind_unordered_access_views(context_gl,
4373 state->shader[WINED3D_SHADER_TYPE_COMPUTE],
4374 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
4375 context_gl->c.update_compute_unordered_access_view_bindings = 0;
4376 context_gl->c.update_unordered_access_view_bindings = 1;
4379 /* Updates to currently bound render targets aren't necessarily coherent
4380 * between the graphics and compute pipelines. Unbind any currently bound
4381 * FBO here to ensure preceding updates to its attachments by the graphics
4382 * pipeline are visible to the compute pipeline.
4384 * Without this, the bloom effect in Nier:Automata is too bright on the
4385 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4386 wined3d_context_gl_bind_fbo(context_gl, GL_FRAMEBUFFER, 0);
4387 context_invalidate_state(&context_gl->c, STATE_FRAMEBUFFER);
4389 context_gl->c.last_was_blit = FALSE;
4390 context_gl->c.last_was_ffp_blit = FALSE;
4393 void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl *context_gl)
4395 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4397 if (context_gl->c.transform_feedback_active)
4399 GL_EXTCALL(glEndTransformFeedback());
4400 checkGLcall("glEndTransformFeedback");
4401 context_gl->c.transform_feedback_active = 0;
4402 context_gl->c.transform_feedback_paused = 0;
4406 static void wined3d_context_gl_pause_transform_feedback(struct wined3d_context_gl *context_gl, BOOL force)
4408 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4410 if (!context_gl->c.transform_feedback_active || context_gl->c.transform_feedback_paused)
4411 return;
4413 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK2])
4415 GL_EXTCALL(glPauseTransformFeedback());
4416 checkGLcall("glPauseTransformFeedback");
4417 context_gl->c.transform_feedback_paused = 1;
4418 return;
4421 WARN("Cannot pause transform feedback operations.\n");
4423 if (force)
4424 wined3d_context_gl_end_transform_feedback(context_gl);
4427 static void wined3d_context_gl_setup_target(struct wined3d_context_gl *context_gl,
4428 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4430 BOOL old_render_offscreen = context_gl->c.render_offscreen, render_offscreen;
4432 render_offscreen = wined3d_resource_is_offscreen(&texture->resource);
4433 if (context_gl->c.current_rt.texture == texture
4434 && context_gl->c.current_rt.sub_resource_idx == sub_resource_idx
4435 && render_offscreen == old_render_offscreen)
4436 return;
4438 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4439 * the alpha blend state changes with different render target formats. */
4440 if (!context_gl->c.current_rt.texture)
4442 context_invalidate_state(&context_gl->c, STATE_BLEND);
4444 else
4446 const struct wined3d_format *old = context_gl->c.current_rt.texture->resource.format;
4447 const struct wined3d_format *new = texture->resource.format;
4449 if (old->id != new->id)
4451 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4452 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
4453 || !(texture->resource.format_caps & WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING))
4454 context_invalidate_state(&context_gl->c, STATE_BLEND);
4457 /* When switching away from an offscreen render target, and we're not
4458 * using FBOs, we have to read the drawable into the texture. This is
4459 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4460 * There are some things that need care though. PreLoad needs a GL context,
4461 * and FindContext is called before the context is activated. It also
4462 * has to be called with the old rendertarget active, otherwise a
4463 * wrong drawable is read. */
4464 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4465 && old_render_offscreen && (context_gl->c.current_rt.texture != texture
4466 || context_gl->c.current_rt.sub_resource_idx != sub_resource_idx))
4468 struct wined3d_texture_gl *prev_texture = wined3d_texture_gl(context_gl->c.current_rt.texture);
4469 unsigned int prev_sub_resource_idx = context_gl->c.current_rt.sub_resource_idx;
4471 /* Read the back buffer of the old drawable into the destination texture. */
4472 if (prev_texture->texture_srgb.name)
4473 wined3d_texture_load(&prev_texture->t, &context_gl->c, TRUE);
4474 wined3d_texture_load(&prev_texture->t, &context_gl->c, FALSE);
4475 wined3d_texture_invalidate_location(&prev_texture->t, prev_sub_resource_idx, WINED3D_LOCATION_DRAWABLE);
4479 context_gl->c.current_rt.texture = texture;
4480 context_gl->c.current_rt.sub_resource_idx = sub_resource_idx;
4481 wined3d_context_gl_set_render_offscreen(context_gl, render_offscreen);
4484 static void wined3d_context_gl_activate(struct wined3d_context_gl *context_gl,
4485 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4487 wined3d_context_gl_enter(context_gl);
4488 wined3d_context_gl_update_window(context_gl);
4489 wined3d_context_gl_setup_target(context_gl, texture, sub_resource_idx);
4490 if (!context_gl->valid)
4491 return;
4493 if (context_gl != wined3d_context_gl_get_current())
4495 if (!wined3d_context_gl_set_current(context_gl))
4496 ERR("Failed to activate the new context.\n");
4498 else if (context_gl->needs_set)
4500 wined3d_context_gl_set_gl_context(context_gl);
4504 struct wined3d_context *wined3d_context_gl_acquire(const struct wined3d_device *device,
4505 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4507 struct wined3d_context_gl *current_context = wined3d_context_gl_get_current();
4508 struct wined3d_context_gl *context_gl;
4510 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device, texture, sub_resource_idx);
4512 if (current_context && current_context->c.destroyed)
4513 current_context = NULL;
4515 if (!texture)
4517 if (current_context
4518 && current_context->c.current_rt.texture
4519 && current_context->c.device == device)
4521 texture = current_context->c.current_rt.texture;
4522 sub_resource_idx = current_context->c.current_rt.sub_resource_idx;
4524 else
4526 struct wined3d_swapchain *swapchain = device->swapchains[0];
4528 if (swapchain->back_buffers)
4529 texture = swapchain->back_buffers[0];
4530 else
4531 texture = swapchain->front_buffer;
4532 sub_resource_idx = 0;
4536 if (current_context && current_context->c.current_rt.texture == texture)
4538 context_gl = current_context;
4540 else if (!wined3d_resource_is_offscreen(&texture->resource))
4542 TRACE("Rendering onscreen.\n");
4544 if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(texture->swapchain))))
4545 return NULL;
4547 else
4549 TRACE("Rendering offscreen.\n");
4551 /* Stay with the current context if possible. Otherwise use the
4552 * context for the primary swapchain. */
4553 if (current_context && current_context->c.device == device)
4554 context_gl = current_context;
4555 else if (!(context_gl = wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(device->swapchains[0]))))
4556 return NULL;
4559 wined3d_context_gl_activate(context_gl, texture, sub_resource_idx);
4561 return &context_gl->c;
4564 struct wined3d_context_gl *wined3d_context_gl_reacquire(struct wined3d_context_gl *context_gl)
4566 struct wined3d_context *acquired_context;
4567 struct wined3d_device *device;
4569 if (!context_gl || context_gl->tid != GetCurrentThreadId())
4570 return NULL;
4572 device = context_gl->c.device;
4573 wined3d_from_cs(device->cs);
4575 if (context_gl->c.current_rt.texture)
4577 wined3d_context_gl_activate(context_gl, context_gl->c.current_rt.texture,
4578 context_gl->c.current_rt.sub_resource_idx);
4579 return context_gl;
4582 acquired_context = context_acquire(device, NULL, 0);
4583 if (acquired_context != &context_gl->c)
4584 ERR("Acquired context %p instead of %p.\n", acquired_context, &context_gl->c);
4585 return wined3d_context_gl(acquired_context);
4588 void dispatch_compute(struct wined3d_device *device, const struct wined3d_state *state,
4589 const struct wined3d_dispatch_parameters *parameters)
4591 const struct wined3d_gl_info *gl_info;
4592 struct wined3d_context_gl *context_gl;
4594 context_gl = wined3d_context_gl(context_acquire(device, NULL, 0));
4595 if (!context_gl->valid)
4597 context_release(&context_gl->c);
4598 WARN("Invalid context, skipping dispatch.\n");
4599 return;
4601 gl_info = context_gl->gl_info;
4603 if (!gl_info->supported[ARB_COMPUTE_SHADER])
4605 context_release(&context_gl->c);
4606 FIXME("OpenGL implementation does not support compute shaders.\n");
4607 return;
4610 if (parameters->indirect)
4611 wined3d_buffer_load(parameters->u.indirect.buffer, &context_gl->c, state);
4613 wined3d_context_gl_apply_compute_state(context_gl, device, state);
4615 if (parameters->indirect)
4617 const struct wined3d_indirect_dispatch_parameters *indirect = &parameters->u.indirect;
4618 struct wined3d_bo_gl *bo_gl = wined3d_bo_gl(indirect->buffer->buffer_object);
4620 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, bo_gl->id));
4621 GL_EXTCALL(glDispatchComputeIndirect(bo_gl->b.buffer_offset + (GLintptr)indirect->offset));
4622 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0));
4623 wined3d_context_gl_reference_bo(context_gl, bo_gl);
4625 else
4627 const struct wined3d_direct_dispatch_parameters *direct = &parameters->u.direct;
4628 GL_EXTCALL(glDispatchCompute(direct->group_count_x, direct->group_count_y, direct->group_count_z));
4630 checkGLcall("dispatch compute");
4632 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
4633 checkGLcall("glMemoryBarrier");
4635 context_release(&context_gl->c);
4638 /* Context activation is done by the caller. */
4639 static void wined3d_context_gl_draw_primitive_arrays(struct wined3d_context_gl *context_gl,
4640 const struct wined3d_state *state, const void *idx_data, unsigned int idx_size, int base_vertex_idx,
4641 unsigned int start_idx, unsigned int count, unsigned int start_instance, unsigned int instance_count)
4643 const struct wined3d_ffp_attrib_ops *ops = &context_gl->c.d3d_info->ffp_attrib_ops;
4644 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
4645 const struct wined3d_stream_info *si = &context_gl->c.stream_info;
4646 GLenum mode = gl_primitive_type_from_d3d(state->primitive_type);
4647 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4648 unsigned int instanced_elements[ARRAY_SIZE(si->elements)];
4649 unsigned int instanced_element_count = 0;
4650 const void *indices;
4651 unsigned int i, j;
4653 indices = (const char *)idx_data + idx_size * start_idx;
4655 if (!instance_count)
4657 if (!idx_size)
4659 gl_info->gl_ops.gl.p_glDrawArrays(mode, start_idx, count);
4660 checkGLcall("glDrawArrays");
4661 return;
4664 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4666 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4667 checkGLcall("glDrawElementsBaseVertex");
4668 return;
4671 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4672 checkGLcall("glDrawElements");
4673 return;
4676 if (start_instance && !(gl_info->supported[ARB_BASE_INSTANCE] && gl_info->supported[ARB_INSTANCED_ARRAYS]))
4677 FIXME("Start instance (%u) not supported.\n", start_instance);
4679 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
4681 if (!idx_size)
4683 if (gl_info->supported[ARB_BASE_INSTANCE])
4685 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode, start_idx, count, instance_count, start_instance));
4686 checkGLcall("glDrawArraysInstancedBaseInstance");
4687 return;
4690 GL_EXTCALL(glDrawArraysInstanced(mode, start_idx, count, instance_count));
4691 checkGLcall("glDrawArraysInstanced");
4692 return;
4695 if (gl_info->supported[ARB_BASE_INSTANCE])
4697 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode, count, idx_type,
4698 indices, instance_count, base_vertex_idx, start_instance));
4699 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4700 return;
4702 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4704 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode, count, idx_type,
4705 indices, instance_count, base_vertex_idx));
4706 checkGLcall("glDrawElementsInstancedBaseVertex");
4707 return;
4710 GL_EXTCALL(glDrawElementsInstanced(mode, count, idx_type, indices, instance_count));
4711 checkGLcall("glDrawElementsInstanced");
4712 return;
4715 /* Instancing emulation by mixing immediate mode and arrays. */
4717 /* This is a nasty thing. MSDN says no hardware supports this and
4718 * applications have to use software vertex processing. We don't support
4719 * this for now.
4721 * Shouldn't be too hard to support with OpenGL, in theory just call
4722 * glDrawArrays() instead of drawElements(). But the stream frequency value
4723 * has a different meaning in that situation. */
4724 if (!idx_size)
4726 FIXME("Non-indexed instanced drawing is not supported.\n");
4727 return;
4730 for (i = 0; i < ARRAY_SIZE(si->elements); ++i)
4732 if (!(si->use_map & (1u << i)))
4733 continue;
4735 if (state->streams[si->elements[i].stream_idx].flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
4736 instanced_elements[instanced_element_count++] = i;
4739 for (i = 0; i < instance_count; ++i)
4741 /* Specify the instanced attributes using immediate mode calls. */
4742 for (j = 0; j < instanced_element_count; ++j)
4744 const struct wined3d_stream_info_element *element;
4745 unsigned int element_idx;
4746 const BYTE *ptr;
4748 element_idx = instanced_elements[j];
4749 element = &si->elements[element_idx];
4750 ptr = element->data.addr + element->stride * i;
4751 if (element->data.buffer_object)
4752 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(state->streams[element->stream_idx].buffer,
4753 &context_gl->c);
4754 ops->generic[element->format->emit_idx](element_idx, ptr);
4757 if (gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX])
4759 GL_EXTCALL(glDrawElementsBaseVertex(mode, count, idx_type, indices, base_vertex_idx));
4760 checkGLcall("glDrawElementsBaseVertex");
4762 else
4764 gl_info->gl_ops.gl.p_glDrawElements(mode, count, idx_type, indices);
4765 checkGLcall("glDrawElements");
4770 static unsigned int get_stride_idx(const void *idx_data, unsigned int idx_size,
4771 unsigned int base_vertex_idx, unsigned int start_idx, unsigned int vertex_idx)
4773 if (!idx_data)
4774 return start_idx + vertex_idx;
4775 if (idx_size == 2)
4776 return ((const WORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4777 return ((const DWORD *)idx_data)[start_idx + vertex_idx] + base_vertex_idx;
4780 /* Context activation is done by the caller. */
4781 static void draw_primitive_immediate_mode(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
4782 const struct wined3d_stream_info *si, const void *idx_data, unsigned int idx_size,
4783 int base_vertex_idx, unsigned int start_idx, unsigned int vertex_count, unsigned int instance_count)
4785 const BYTE *position = NULL, *normal = NULL, *diffuse = NULL, *specular = NULL;
4786 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
4787 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
4788 unsigned int coord_idx, stride_idx, texture_idx, vertex_idx;
4789 const struct wined3d_stream_info_element *element;
4790 const BYTE *tex_coords[WINED3DDP_MAXTEXCOORD];
4791 unsigned int texture_unit, texture_stages;
4792 const struct wined3d_ffp_attrib_ops *ops;
4793 unsigned int untracked_material_count;
4794 unsigned int tex_mask = 0;
4795 BOOL specular_fog = FALSE;
4796 BOOL ps = use_ps(state);
4797 const void *ptr;
4799 static unsigned int once;
4801 if (!once++)
4802 FIXME_(d3d_perf)("Drawing using immediate mode.\n");
4803 else
4804 WARN_(d3d_perf)("Drawing using immediate mode.\n");
4806 if (!idx_size && idx_data)
4807 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4809 if (instance_count)
4810 FIXME("Instancing not implemented.\n");
4812 /* Immediate mode drawing can't make use of indices in a VBO - get the
4813 * data from the index buffer. */
4814 if (idx_size)
4815 idx_data = wined3d_buffer_load_sysmem(state->index_buffer, &context_gl->c) + state->index_offset;
4817 ops = &d3d_info->ffp_attrib_ops;
4819 gl_info->gl_ops.gl.p_glBegin(gl_primitive_type_from_d3d(state->primitive_type));
4821 if (use_vs(state) || d3d_info->ffp_generic_attributes)
4823 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4825 unsigned int use_map = si->use_map;
4826 unsigned int element_idx;
4828 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4829 for (element_idx = gl_info->limits.vertex_attribs - 1; use_map;
4830 use_map &= ~(1u << element_idx), --element_idx)
4832 if (!(use_map & 1u << element_idx))
4833 continue;
4835 ptr = si->elements[element_idx].data.addr + si->elements[element_idx].stride * stride_idx;
4836 ops->generic[si->elements[element_idx].format->emit_idx](element_idx, ptr);
4840 gl_info->gl_ops.gl.p_glEnd();
4841 return;
4844 if (si->use_map & (1u << WINED3D_FFP_POSITION))
4845 position = si->elements[WINED3D_FFP_POSITION].data.addr;
4847 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
4848 normal = si->elements[WINED3D_FFP_NORMAL].data.addr;
4849 else
4850 gl_info->gl_ops.gl.p_glNormal3f(0.0f, 0.0f, 0.0f);
4852 untracked_material_count = context_gl->untracked_material_count;
4853 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
4855 element = &si->elements[WINED3D_FFP_DIFFUSE];
4856 diffuse = element->data.addr;
4858 if (untracked_material_count && element->format->id != WINED3DFMT_B8G8R8A8_UNORM)
4859 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element->format->id));
4861 else
4863 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
4866 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
4868 element = &si->elements[WINED3D_FFP_SPECULAR];
4869 specular = element->data.addr;
4871 /* Special case where the fog density is stored in the specular alpha channel. */
4872 if (state->render_states[WINED3D_RS_FOGENABLE]
4873 && (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
4874 || si->elements[WINED3D_FFP_POSITION].format->id == WINED3DFMT_R32G32B32A32_FLOAT)
4875 && state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
4877 if (gl_info->supported[EXT_FOG_COORD])
4879 if (element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
4880 specular_fog = TRUE;
4881 else
4882 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element->format->id));
4884 else
4886 static unsigned int once;
4888 if (!once++)
4889 FIXME("Implement fog for transformed vertices in software.\n");
4893 else if (gl_info->supported[EXT_SECONDARY_COLOR])
4895 GL_EXTCALL(glSecondaryColor3fEXT)(0.0f, 0.0f, 0.0f);
4898 texture_stages = d3d_info->limits.ffp_blend_stages;
4899 for (texture_idx = 0; texture_idx < texture_stages; ++texture_idx)
4901 if (!gl_info->supported[ARB_MULTITEXTURE] && texture_idx > 0)
4903 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4904 continue;
4907 if (!ps && !state->textures[texture_idx])
4908 continue;
4910 texture_unit = context_gl->tex_unit_map[texture_idx];
4911 if (texture_unit == WINED3D_UNMAPPED_STAGE)
4912 continue;
4914 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4915 if (coord_idx > 7)
4917 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx, texture_idx);
4918 continue;
4921 if (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx)))
4923 tex_coords[coord_idx] = si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].data.addr;
4924 tex_mask |= (1u << texture_idx);
4926 else
4928 TRACE("Setting default coordinates for texture %u.\n", texture_idx);
4929 if (gl_info->supported[ARB_MULTITEXTURE])
4930 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_unit, 0.0f, 0.0f, 0.0f, 1.0f));
4931 else
4932 gl_info->gl_ops.gl.p_glTexCoord4f(0.0f, 0.0f, 0.0f, 1.0f);
4936 /* Blending data and point sizes are not supported by this function. They
4937 * are not supported by the fixed function pipeline at all. A FIXME for
4938 * them is printed after decoding the vertex declaration. */
4939 for (vertex_idx = 0; vertex_idx < vertex_count; ++vertex_idx)
4941 uint32_t tmp_tex_mask;
4943 stride_idx = get_stride_idx(idx_data, idx_size, base_vertex_idx, start_idx, vertex_idx);
4945 if (normal)
4947 ptr = normal + stride_idx * si->elements[WINED3D_FFP_NORMAL].stride;
4948 ops->normal[si->elements[WINED3D_FFP_NORMAL].format->emit_idx](ptr);
4951 if (diffuse)
4953 ptr = diffuse + stride_idx * si->elements[WINED3D_FFP_DIFFUSE].stride;
4954 ops->diffuse[si->elements[WINED3D_FFP_DIFFUSE].format->emit_idx](ptr);
4956 if (untracked_material_count)
4958 struct wined3d_color color;
4959 unsigned int i;
4961 wined3d_color_from_d3dcolor(&color, *(const DWORD *)ptr);
4962 for (i = 0; i < untracked_material_count; ++i)
4964 gl_info->gl_ops.gl.p_glMaterialfv(GL_FRONT_AND_BACK,
4965 context_gl->untracked_materials[i], &color.r);
4970 if (specular)
4972 ptr = specular + stride_idx * si->elements[WINED3D_FFP_SPECULAR].stride;
4973 ops->specular[si->elements[WINED3D_FFP_SPECULAR].format->emit_idx](ptr);
4975 if (specular_fog)
4976 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD *)ptr >> 24)));
4979 tmp_tex_mask = tex_mask;
4980 while (tmp_tex_mask)
4982 texture_idx = wined3d_bit_scan(&tmp_tex_mask);
4983 coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
4984 ptr = tex_coords[coord_idx] + (stride_idx * si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].stride);
4985 ops->texcoord[si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx].format->emit_idx](
4986 GL_TEXTURE0_ARB + context_gl->tex_unit_map[texture_idx], ptr);
4989 if (position)
4991 ptr = position + stride_idx * si->elements[WINED3D_FFP_POSITION].stride;
4992 ops->position[si->elements[WINED3D_FFP_POSITION].format->emit_idx](ptr);
4996 gl_info->gl_ops.gl.p_glEnd();
4997 checkGLcall("draw immediate mode");
5000 static void wined3d_context_gl_draw_indirect(struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
5001 const struct wined3d_indirect_draw_parameters *parameters, unsigned int idx_size)
5003 struct wined3d_bo_gl *bo_gl = wined3d_bo_gl(parameters->buffer->buffer_object);
5004 GLenum gl_primitive_type = gl_primitive_type_from_d3d(state->primitive_type);
5005 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5006 const void *offset;
5008 if (!gl_info->supported[ARB_DRAW_INDIRECT])
5010 FIXME("OpenGL implementation does not support indirect draws.\n");
5011 return;
5014 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, bo_gl->id));
5016 offset = (const uint8_t *)bo_gl->b.buffer_offset + parameters->offset;
5017 if (idx_size)
5019 GLenum idx_type = idx_size == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
5020 if (state->index_offset)
5021 FIXME("Ignoring index offset %u.\n", state->index_offset);
5022 GL_EXTCALL(glDrawElementsIndirect(gl_primitive_type, idx_type, offset));
5024 else
5026 GL_EXTCALL(glDrawArraysIndirect(gl_primitive_type, offset));
5029 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0));
5030 wined3d_context_gl_reference_bo(context_gl, bo_gl);
5032 checkGLcall("draw indirect");
5035 static void remove_vbos(struct wined3d_context *context,
5036 const struct wined3d_state *state, struct wined3d_stream_info *s)
5038 unsigned int i;
5040 for (i = 0; i < ARRAY_SIZE(s->elements); ++i)
5042 struct wined3d_stream_info_element *e;
5044 if (!(s->use_map & (1u << i)))
5045 continue;
5047 e = &s->elements[i];
5048 if (e->data.buffer_object)
5050 struct wined3d_buffer *vb = state->streams[e->stream_idx].buffer;
5051 e->data.buffer_object = 0;
5052 e->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(vb, context);
5057 static GLenum gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
5059 GLenum gl_primitive_type = gl_primitive_type_from_d3d(primitive_type);
5060 switch (gl_primitive_type)
5062 case GL_POINTS:
5063 return GL_POINTS;
5065 case GL_LINE_STRIP:
5066 case GL_LINE_STRIP_ADJACENCY:
5067 case GL_LINES_ADJACENCY:
5068 case GL_LINES:
5069 return GL_LINES;
5071 case GL_TRIANGLE_FAN:
5072 case GL_TRIANGLE_STRIP:
5073 case GL_TRIANGLE_STRIP_ADJACENCY:
5074 case GL_TRIANGLES_ADJACENCY:
5075 case GL_TRIANGLES:
5076 return GL_TRIANGLES;
5078 default:
5079 return gl_primitive_type;
5083 /* Routine common to the draw primitive and draw indexed primitive routines */
5084 void draw_primitive(struct wined3d_device *device, const struct wined3d_state *state,
5085 const struct wined3d_draw_parameters *parameters)
5087 BOOL emulation = FALSE, rasterizer_discard = FALSE;
5088 const struct wined3d_fb_state *fb = &state->fb;
5089 const struct wined3d_stream_info *stream_info;
5090 struct wined3d_rendertarget_view *dsv, *rtv;
5091 struct wined3d_stream_info si_emulated;
5092 const struct wined3d_gl_info *gl_info;
5093 struct wined3d_context_gl *context_gl;
5094 struct wined3d_context *context;
5095 unsigned int i, idx_size = 0;
5096 const void *idx_data = NULL;
5098 TRACE("device %p, state %p, parameters %p.\n", device, state, parameters);
5100 if (!parameters->indirect && !parameters->u.direct.index_count)
5101 return;
5103 if (!parameters->indirect)
5104 TRACE("base_vertex_idx %d, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
5105 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
5106 parameters->u.direct.index_count, parameters->u.direct.start_instance,
5107 parameters->u.direct.instance_count);
5109 if (!(rtv = fb->render_targets[0]))
5110 rtv = fb->depth_stencil;
5112 if (rtv && rtv->resource->type == WINED3D_RTYPE_BUFFER)
5114 FIXME("Buffer render targets not implemented.\n");
5115 return;
5118 if (rtv)
5119 context = context_acquire(device, wined3d_texture_from_resource(rtv->resource), rtv->sub_resource_idx);
5120 else
5121 context = context_acquire(device, NULL, 0);
5122 context_gl = wined3d_context_gl(context);
5123 if (!context_gl->valid)
5125 context_release(context);
5126 WARN("Invalid context, skipping draw.\n");
5127 return;
5129 gl_info = context_gl->gl_info;
5131 if (!use_transform_feedback(state))
5132 wined3d_context_gl_pause_transform_feedback(context_gl, TRUE);
5134 for (i = 0; i < gl_info->limits.buffers; ++i)
5136 if (!(rtv = fb->render_targets[i]) || rtv->format->id == WINED3DFMT_NULL)
5137 continue;
5139 if (wined3d_blend_state_get_writemask(state->blend_state, i))
5141 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
5142 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
5144 else
5146 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
5150 if ((dsv = fb->depth_stencil))
5152 /* Note that this depends on the context_acquire() call above to set
5153 * context->render_offscreen properly. We don't currently take the
5154 * Z-compare function into account, but we could skip loading the
5155 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
5156 * that we never copy the stencil data.*/
5157 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5159 if (wined3d_state_uses_depth_buffer(state))
5160 wined3d_rendertarget_view_load_location(dsv, context, location);
5161 else
5162 wined3d_rendertarget_view_prepare_location(dsv, context, location);
5165 if (parameters->indirect)
5166 wined3d_buffer_load(parameters->u.indirect.buffer, context, state);
5168 if (!context_apply_draw_state(context, device, state, parameters->indexed))
5170 context_release(context);
5171 WARN("Unable to apply draw state, skipping draw.\n");
5172 return;
5175 if (dsv && (!state->depth_stencil_state || state->depth_stencil_state->writes_ds))
5177 DWORD location = context->render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5179 wined3d_rendertarget_view_validate_location(dsv, location);
5180 wined3d_rendertarget_view_invalidate_location(dsv, ~location);
5183 stream_info = &context->stream_info;
5185 if (parameters->indexed)
5187 struct wined3d_buffer *index_buffer = state->index_buffer;
5188 struct wined3d_bo *bo = index_buffer->buffer_object;
5190 if (!bo || !stream_info->all_vbo)
5191 idx_data = index_buffer->resource.heap_memory;
5192 else
5193 idx_data = (void *)bo->buffer_offset;
5194 idx_data = (const BYTE *)idx_data + state->index_offset;
5196 if (state->index_format == WINED3DFMT_R16_UINT)
5197 idx_size = 2;
5198 else
5199 idx_size = 4;
5202 if (!use_vs(state))
5204 if (!stream_info->position_transformed && context_gl->untracked_material_count
5205 && state->render_states[WINED3D_RS_LIGHTING])
5207 static BOOL warned;
5209 if (!warned++)
5210 FIXME("Using software emulation because not all material properties could be tracked.\n");
5211 else
5212 WARN_(d3d_perf)("Using software emulation because not all material properties could be tracked.\n");
5213 emulation = TRUE;
5215 else if (context->fog_coord && state->render_states[WINED3D_RS_FOGENABLE])
5217 static BOOL warned;
5219 /* Either write a pipeline replacement shader or convert the
5220 * specular alpha from unsigned byte to a float in the vertex
5221 * buffer. */
5222 if (!warned++)
5223 FIXME("Using software emulation because manual fog coordinates are provided.\n");
5224 else
5225 WARN_(d3d_perf)("Using software emulation because manual fog coordinates are provided.\n");
5226 emulation = TRUE;
5229 if (emulation)
5231 si_emulated = context->stream_info;
5232 remove_vbos(context, state, &si_emulated);
5233 stream_info = &si_emulated;
5237 if (use_transform_feedback(state))
5239 const struct wined3d_shader *shader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5241 if (is_rasterization_disabled(shader))
5243 glEnable(GL_RASTERIZER_DISCARD);
5244 checkGLcall("enable rasterizer discard");
5245 rasterizer_discard = TRUE;
5248 if (context->transform_feedback_paused)
5250 GL_EXTCALL(glResumeTransformFeedback());
5251 checkGLcall("glResumeTransformFeedback");
5252 context->transform_feedback_paused = 0;
5254 else if (!context->transform_feedback_active)
5256 enum wined3d_primitive_type primitive_type = shader->u.gs.output_type
5257 ? shader->u.gs.output_type : state->primitive_type;
5258 GLenum mode = gl_tfb_primitive_type_from_d3d(primitive_type);
5259 GL_EXTCALL(glBeginTransformFeedback(mode));
5260 checkGLcall("glBeginTransformFeedback");
5261 context->transform_feedback_active = 1;
5265 if (state->primitive_type == WINED3D_PT_PATCH)
5267 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES, state->patch_vertex_count));
5268 checkGLcall("glPatchParameteri");
5271 if (context->uses_fbo_attached_resources)
5273 static unsigned int fixme_once;
5275 if (gl_info->supported[ARB_TEXTURE_BARRIER])
5277 GL_EXTCALL(glTextureBarrier());
5279 else if (gl_info->supported[NV_TEXTURE_BARRIER])
5281 GL_EXTCALL(glTextureBarrierNV());
5283 else
5285 if (!fixme_once++)
5286 FIXME("Sampling attached render targets is not supported.\n");
5288 WARN("Sampling attached render targets is not supported, skipping draw.\n");
5289 context_release(context);
5290 return;
5292 checkGLcall("glTextureBarrier");
5295 if (parameters->indirect)
5297 if (!context->use_immediate_mode_draw && !emulation)
5298 wined3d_context_gl_draw_indirect(context_gl, state, &parameters->u.indirect, idx_size);
5299 else
5300 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
5302 else
5304 unsigned int instance_count = parameters->u.direct.instance_count;
5305 if (context->instance_count)
5306 instance_count = context->instance_count;
5308 if (context->use_immediate_mode_draw || emulation)
5309 draw_primitive_immediate_mode(wined3d_context_gl(context), state, stream_info, idx_data,
5310 idx_size, parameters->u.direct.base_vertex_idx,
5311 parameters->u.direct.start_idx, parameters->u.direct.index_count, instance_count);
5312 else
5313 wined3d_context_gl_draw_primitive_arrays(context_gl, state, idx_data, idx_size,
5314 parameters->u.direct.base_vertex_idx, parameters->u.direct.start_idx,
5315 parameters->u.direct.index_count, parameters->u.direct.start_instance, instance_count);
5318 if (context->uses_uavs)
5320 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS));
5321 checkGLcall("glMemoryBarrier");
5324 wined3d_context_gl_pause_transform_feedback(context_gl, FALSE);
5326 if (rasterizer_discard)
5328 glDisable(GL_RASTERIZER_DISCARD);
5329 checkGLcall("disable rasterizer discard");
5332 context_release(context);
5334 TRACE("Draw completed.\n");
5337 void wined3d_context_gl_unload_tex_coords(const struct wined3d_context_gl *context_gl)
5339 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5340 unsigned int texture_idx;
5342 for (texture_idx = 0; texture_idx < gl_info->limits.texture_coords; ++texture_idx)
5344 gl_info->gl_ops.ext.p_glClientActiveTextureARB(GL_TEXTURE0_ARB + texture_idx);
5345 gl_info->gl_ops.gl.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY);
5349 static const void *get_vertex_attrib_pointer(const struct wined3d_stream_info_element *element,
5350 const struct wined3d_state *state)
5352 const uint8_t *offset = element->data.addr + state->load_base_vertex_index * element->stride;
5354 if (element->data.buffer_object)
5355 offset += element->data.buffer_object->buffer_offset;
5356 return offset;
5359 void wined3d_context_gl_load_tex_coords(const struct wined3d_context_gl *context_gl,
5360 const struct wined3d_stream_info *si, GLuint *current_bo, const struct wined3d_state *state)
5362 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5363 const struct wined3d_format_gl *format_gl;
5364 unsigned int mapped_stage = 0;
5365 unsigned int texture_idx;
5366 GLuint bo;
5368 for (texture_idx = 0; texture_idx < context_gl->c.d3d_info->limits.ffp_blend_stages; ++texture_idx)
5370 unsigned int coord_idx = state->texture_states[texture_idx][WINED3D_TSS_TEXCOORD_INDEX];
5372 if ((mapped_stage = context_gl->tex_unit_map[texture_idx]) == WINED3D_UNMAPPED_STAGE)
5373 continue;
5375 if (mapped_stage >= gl_info->limits.texture_coords)
5377 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage);
5378 continue;
5381 if (coord_idx < WINED3D_MAX_TEXTURES && (si->use_map & (1u << (WINED3D_FFP_TEXCOORD0 + coord_idx))))
5383 const struct wined3d_stream_info_element *e = &si->elements[WINED3D_FFP_TEXCOORD0 + coord_idx];
5385 TRACE("Setting up texture %u, idx %u, coord_idx %u, data %s.\n",
5386 texture_idx, mapped_stage, coord_idx, debug_bo_address(&e->data));
5388 bo = wined3d_bo_gl_id(e->data.buffer_object);
5389 if (*current_bo != bo)
5391 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5392 checkGLcall("glBindBuffer");
5393 *current_bo = bo;
5396 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB + mapped_stage));
5397 checkGLcall("glClientActiveTextureARB");
5399 /* The coords to supply depend completely on the fvf/vertex shader. */
5400 format_gl = wined3d_format_gl(e->format);
5401 gl_info->gl_ops.gl.p_glTexCoordPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride,
5402 get_vertex_attrib_pointer(e, state));
5403 gl_info->gl_ops.gl.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY);
5404 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5406 else
5408 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + mapped_stage, 0, 0, 0, 1));
5411 if (gl_info->supported[NV_REGISTER_COMBINERS])
5413 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
5414 for (texture_idx = mapped_stage + 1; texture_idx < gl_info->limits.textures; ++texture_idx)
5416 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB + texture_idx, 0, 0, 0, 1));
5420 checkGLcall("loadTexCoords");
5423 /* This should match any arrays loaded in wined3d_context_gl_load_vertex_data(). */
5424 static void wined3d_context_gl_unload_vertex_data(struct wined3d_context_gl *context_gl)
5426 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5428 if (!context_gl->c.namedArraysLoaded)
5429 return;
5430 gl_info->gl_ops.gl.p_glDisableClientState(GL_VERTEX_ARRAY);
5431 gl_info->gl_ops.gl.p_glDisableClientState(GL_NORMAL_ARRAY);
5432 gl_info->gl_ops.gl.p_glDisableClientState(GL_COLOR_ARRAY);
5433 if (gl_info->supported[EXT_SECONDARY_COLOR])
5434 gl_info->gl_ops.gl.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5435 wined3d_context_gl_unload_tex_coords(context_gl);
5436 context_gl->c.namedArraysLoaded = FALSE;
5439 static void wined3d_context_gl_load_vertex_data(struct wined3d_context_gl *context_gl,
5440 const struct wined3d_stream_info *si, const struct wined3d_state *state)
5442 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5443 const struct wined3d_stream_info_element *e;
5444 const struct wined3d_format_gl *format_gl;
5445 GLuint current_bo, bo;
5446 const void *offset;
5448 TRACE("context_gl %p, si %p, state %p.\n", context_gl, si, state);
5450 /* This is used for the fixed-function pipeline only, and the
5451 * fixed-function pipeline doesn't do instancing. */
5452 context_gl->c.instance_count = 0;
5453 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5455 /* Blend data */
5456 if ((si->use_map & (1u << WINED3D_FFP_BLENDWEIGHT))
5457 || si->use_map & (1u << WINED3D_FFP_BLENDINDICES))
5459 /* TODO: Support vertex blending in immediate mode draws. No need to
5460 * write a FIXME here, this is done after the general vertex
5461 * declaration decoding. */
5462 WARN("Vertex blending not supported.\n");
5465 /* Point Size */
5466 if (si->use_map & (1u << WINED3D_FFP_PSIZE))
5468 /* No such functionality in the fixed-function GL pipeline. */
5469 WARN("Per-vertex point size not supported.\n");
5472 /* Position */
5473 if (si->use_map & (1u << WINED3D_FFP_POSITION))
5475 e = &si->elements[WINED3D_FFP_POSITION];
5476 format_gl = wined3d_format_gl(e->format);
5477 offset = get_vertex_attrib_pointer(e, state);
5479 bo = wined3d_bo_gl_id(e->data.buffer_object);
5480 if (current_bo != bo)
5482 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5483 checkGLcall("glBindBuffer");
5484 current_bo = bo;
5487 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n", format_gl->vtx_format, format_gl->vtx_type, e->stride, offset);
5488 gl_info->gl_ops.gl.p_glVertexPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride, offset);
5489 checkGLcall("glVertexPointer(...)");
5490 gl_info->gl_ops.gl.p_glEnableClientState(GL_VERTEX_ARRAY);
5491 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5492 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5495 /* Normals */
5496 if (si->use_map & (1u << WINED3D_FFP_NORMAL))
5498 e = &si->elements[WINED3D_FFP_NORMAL];
5499 format_gl = wined3d_format_gl(e->format);
5500 offset = get_vertex_attrib_pointer(e, state);
5502 bo = wined3d_bo_gl_id(e->data.buffer_object);
5503 if (current_bo != bo)
5505 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5506 checkGLcall("glBindBuffer");
5507 current_bo = bo;
5510 TRACE("glNormalPointer(%#x, %#x, %p);\n", format_gl->vtx_type, e->stride, offset);
5511 gl_info->gl_ops.gl.p_glNormalPointer(format_gl->vtx_type, e->stride, offset);
5512 checkGLcall("glNormalPointer(...)");
5513 gl_info->gl_ops.gl.p_glEnableClientState(GL_NORMAL_ARRAY);
5514 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5515 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5517 else
5519 gl_info->gl_ops.gl.p_glNormal3f(0, 0, 0);
5520 checkGLcall("glNormal3f(0, 0, 0)");
5523 /* Diffuse colour */
5524 if (si->use_map & (1u << WINED3D_FFP_DIFFUSE))
5526 e = &si->elements[WINED3D_FFP_DIFFUSE];
5527 format_gl = wined3d_format_gl(e->format);
5528 offset = get_vertex_attrib_pointer(e, state);
5530 bo = wined3d_bo_gl_id(e->data.buffer_object);
5531 if (current_bo != bo)
5533 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5534 checkGLcall("glBindBuffer");
5535 current_bo = bo;
5538 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5539 format_gl->vtx_format, format_gl->vtx_type, e->stride, offset);
5540 gl_info->gl_ops.gl.p_glColorPointer(format_gl->vtx_format, format_gl->vtx_type, e->stride, offset);
5541 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5542 gl_info->gl_ops.gl.p_glEnableClientState(GL_COLOR_ARRAY);
5543 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5544 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5546 else
5548 gl_info->gl_ops.gl.p_glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
5549 checkGLcall("glColor4f(1, 1, 1, 1)");
5552 /* Specular colour */
5553 if (si->use_map & (1u << WINED3D_FFP_SPECULAR))
5555 TRACE("Setting specular colour.\n");
5557 e = &si->elements[WINED3D_FFP_SPECULAR];
5558 offset = get_vertex_attrib_pointer(e, state);
5560 if (gl_info->supported[EXT_SECONDARY_COLOR])
5562 GLint format;
5563 GLenum type;
5565 format_gl = wined3d_format_gl(e->format);
5566 type = format_gl->vtx_type;
5567 format = format_gl->vtx_format;
5569 bo = wined3d_bo_gl_id(e->data.buffer_object);
5570 if (current_bo != bo)
5572 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5573 checkGLcall("glBindBuffer");
5574 current_bo = bo;
5577 if (format != 4 || (gl_info->quirks & WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA))
5579 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5580 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5581 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5582 * 4 component secondary colors use it
5584 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format, type, e->stride, offset);
5585 GL_EXTCALL(glSecondaryColorPointerEXT(format, type, e->stride, offset));
5586 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5588 else
5590 switch (type)
5592 case GL_UNSIGNED_BYTE:
5593 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e->stride, offset);
5594 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, e->stride, offset));
5595 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5596 break;
5598 default:
5599 FIXME("Add 4 component specular colour pointers for type %#x.\n", type);
5600 /* Make sure that the right colour component is dropped. */
5601 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type, e->stride, offset);
5602 GL_EXTCALL(glSecondaryColorPointerEXT(3, type, e->stride, offset));
5603 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5606 gl_info->gl_ops.gl.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT);
5607 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5608 state->streams[e->stream_idx].buffer->bo_user.valid = true;
5610 else
5612 WARN("Specular colour is not supported in this GL implementation.\n");
5615 else
5617 if (gl_info->supported[EXT_SECONDARY_COLOR])
5619 GL_EXTCALL(glSecondaryColor3fEXT)(0, 0, 0);
5620 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5622 else
5624 WARN("Specular colour is not supported in this GL implementation.\n");
5628 /* Texture coordinates */
5629 wined3d_context_gl_load_tex_coords(context_gl, si, &current_bo, state);
5632 static void wined3d_context_gl_unload_numbered_array(struct wined3d_context_gl *context_gl, unsigned int i)
5634 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5636 GL_EXTCALL(glDisableVertexAttribArray(i));
5637 checkGLcall("glDisableVertexAttribArray");
5638 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5639 GL_EXTCALL(glVertexAttribDivisor(i, 0));
5641 context_gl->c.numbered_array_mask &= ~(1u << i);
5644 static void wined3d_context_gl_unload_numbered_arrays(struct wined3d_context_gl *context_gl)
5646 uint32_t mask = context_gl->c.numbered_array_mask;
5647 unsigned int i;
5649 while (mask)
5651 i = wined3d_bit_scan(&mask);
5652 wined3d_context_gl_unload_numbered_array(context_gl, i);
5656 static void wined3d_context_gl_load_numbered_arrays(struct wined3d_context_gl *context_gl,
5657 const struct wined3d_stream_info *stream_info, const struct wined3d_state *state)
5659 struct wined3d_context *context = &context_gl->c;
5660 const struct wined3d_shader *vs = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5661 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5662 GLuint current_bo, bo;
5663 unsigned int i;
5665 /* Default to no instancing. */
5666 context->instance_count = 0;
5667 current_bo = gl_info->supported[ARB_VERTEX_BUFFER_OBJECT] ? ~0u : 0;
5669 if (stream_info->use_map & ~wined3d_mask_from_size(gl_info->limits.vertex_attribs))
5671 static unsigned int once;
5673 if (!once++)
5674 FIXME("More than the supported %u vertex attributes are in use.\n", gl_info->limits.vertex_attribs);
5677 for (i = 0; i < gl_info->limits.vertex_attribs; ++i)
5679 const struct wined3d_stream_info_element *element = &stream_info->elements[i];
5680 const void *offset = get_vertex_attrib_pointer(element, state);
5681 const struct wined3d_stream_state *stream;
5682 const struct wined3d_format_gl *format_gl;
5684 if (!(stream_info->use_map & (1u << i)))
5686 if (context->numbered_array_mask & (1u << i))
5687 wined3d_context_gl_unload_numbered_array(context_gl, i);
5688 if (!use_vs(state) && i == WINED3D_FFP_DIFFUSE)
5690 if (!(context_gl->default_attrib_value_set & (1u << i)) || !context_gl->diffuse_attrib_to_1)
5692 GL_EXTCALL(glVertexAttrib4f(i, 1.0f, 1.0f, 1.0f, 1.0f));
5693 context_gl->diffuse_attrib_to_1 = 1;
5696 else
5698 if (!(context_gl->default_attrib_value_set & (1u << i)))
5700 GL_EXTCALL(glVertexAttrib4f(i, 0.0f, 0.0f, 0.0f, 0.0f));
5701 if (i == WINED3D_FFP_DIFFUSE)
5702 context_gl->diffuse_attrib_to_1 = 0;
5705 context_gl->default_attrib_value_set |= 1u << i;
5706 continue;
5709 format_gl = wined3d_format_gl(element->format);
5710 stream = &state->streams[element->stream_idx];
5711 stream->buffer->bo_user.valid = true;
5713 if ((stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA) && !context->instance_count)
5714 context->instance_count = state->streams[0].frequency;
5716 if (gl_info->supported[ARB_INSTANCED_ARRAYS])
5718 unsigned int divisor = 0;
5720 if (element->instanced)
5721 divisor = element->divisor ? element->divisor : UINT_MAX;
5722 GL_EXTCALL(glVertexAttribDivisor(i, divisor));
5724 else if (element->divisor)
5726 /* Unload instanced arrays, they will be loaded using immediate
5727 * mode instead. */
5728 if (context->numbered_array_mask & (1u << i))
5729 wined3d_context_gl_unload_numbered_array(context_gl, i);
5730 context_gl->default_attrib_value_set &= ~(1u << i);
5731 continue;
5734 TRACE("Loading array %u %s.\n", i, debug_bo_address(&element->data));
5736 if (element->stride)
5738 unsigned int format_attrs = format_gl->f.attrs;
5740 bo = wined3d_bo_gl_id(element->data.buffer_object);
5741 if (current_bo != bo)
5743 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, bo));
5744 checkGLcall("glBindBuffer");
5745 current_bo = bo;
5747 /* Use the VBO to find out if a vertex buffer exists, not the vb
5748 * pointer. vb can point to a user pointer data blob. In that case
5749 * current_bo will be 0. If there is a vertex buffer but no vbo we
5750 * won't be load converted attributes anyway. */
5751 if (vs && vs->reg_maps.shader_version.major >= 4 && (format_attrs & WINED3D_FORMAT_ATTR_INTEGER))
5753 GL_EXTCALL(glVertexAttribIPointer(i, format_gl->vtx_format,
5754 format_gl->vtx_type, element->stride, offset));
5756 else
5758 GL_EXTCALL(glVertexAttribPointer(i, format_gl->vtx_format, format_gl->vtx_type,
5759 !!(format_attrs & WINED3D_FORMAT_ATTR_NORMALISED), element->stride, offset));
5762 if (!(context->numbered_array_mask & (1u << i)))
5764 GL_EXTCALL(glEnableVertexAttribArray(i));
5765 context->numbered_array_mask |= (1u << i);
5768 else
5770 /* Stride = 0 means always the same values.
5771 * glVertexAttribPointer() doesn't do that. Instead disable the
5772 * pointer and set up the attribute statically. But we have to
5773 * figure out the system memory address. */
5774 const BYTE *ptr = element->data.addr;
5775 if (element->data.buffer_object)
5776 ptr += (ULONG_PTR)wined3d_buffer_load_sysmem(stream->buffer, context);
5778 if (context->numbered_array_mask & (1u << i))
5779 wined3d_context_gl_unload_numbered_array(context_gl, i);
5781 switch (format_gl->f.id)
5783 case WINED3DFMT_R32_FLOAT:
5784 GL_EXTCALL(glVertexAttrib1fv(i, (const GLfloat *)ptr));
5785 break;
5786 case WINED3DFMT_R32G32_FLOAT:
5787 GL_EXTCALL(glVertexAttrib2fv(i, (const GLfloat *)ptr));
5788 break;
5789 case WINED3DFMT_R32G32B32_FLOAT:
5790 GL_EXTCALL(glVertexAttrib3fv(i, (const GLfloat *)ptr));
5791 break;
5792 case WINED3DFMT_R32G32B32A32_FLOAT:
5793 GL_EXTCALL(glVertexAttrib4fv(i, (const GLfloat *)ptr));
5794 break;
5795 case WINED3DFMT_R8G8B8A8_UINT:
5796 GL_EXTCALL(glVertexAttrib4ubv(i, ptr));
5797 break;
5798 case WINED3DFMT_B8G8R8A8_UNORM:
5799 if (gl_info->supported[ARB_VERTEX_ARRAY_BGRA])
5801 const DWORD *src = (const DWORD *)ptr;
5802 DWORD c = *src & 0xff00ff00u;
5803 c |= (*src & 0xff0000u) >> 16;
5804 c |= (*src & 0xffu) << 16;
5805 GL_EXTCALL(glVertexAttrib4Nubv(i, (GLubyte *)&c));
5806 break;
5808 /* else fallthrough */
5809 case WINED3DFMT_R8G8B8A8_UNORM:
5810 GL_EXTCALL(glVertexAttrib4Nubv(i, ptr));
5811 break;
5812 case WINED3DFMT_R16G16_SINT:
5813 GL_EXTCALL(glVertexAttrib2sv(i, (const GLshort *)ptr));
5814 break;
5815 case WINED3DFMT_R16G16B16A16_SINT:
5816 GL_EXTCALL(glVertexAttrib4sv(i, (const GLshort *)ptr));
5817 break;
5818 case WINED3DFMT_R16G16_SNORM:
5820 const GLshort s[4] = {((const GLshort *)ptr)[0], ((const GLshort *)ptr)[1], 0, 1};
5821 GL_EXTCALL(glVertexAttrib4Nsv(i, s));
5822 break;
5824 case WINED3DFMT_R16G16_UNORM:
5826 const GLushort s[4] = {((const GLushort *)ptr)[0], ((const GLushort *)ptr)[1], 0, 1};
5827 GL_EXTCALL(glVertexAttrib4Nusv(i, s));
5828 break;
5830 case WINED3DFMT_R16G16B16A16_SNORM:
5831 GL_EXTCALL(glVertexAttrib4Nsv(i, (const GLshort *)ptr));
5832 break;
5833 case WINED3DFMT_R16G16B16A16_UNORM:
5834 GL_EXTCALL(glVertexAttrib4Nusv(i, (const GLushort *)ptr));
5835 break;
5836 case WINED3DFMT_R10G10B10X2_UINT:
5837 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5838 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5839 break;
5840 case WINED3DFMT_R10G10B10X2_SNORM:
5841 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5842 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5843 break;
5844 case WINED3DFMT_R16G16_FLOAT:
5845 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5847 /* Not supported by GL_ARB_half_float_vertex. */
5848 GL_EXTCALL(glVertexAttrib2hvNV(i, (const GLhalfNV *)ptr));
5850 else
5852 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5853 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5854 GL_EXTCALL(glVertexAttrib2f(i, x, y));
5856 break;
5857 case WINED3DFMT_R16G16B16A16_FLOAT:
5858 if (gl_info->supported[NV_HALF_FLOAT] && gl_info->supported[NV_VERTEX_PROGRAM])
5860 /* Not supported by GL_ARB_half_float_vertex. */
5861 GL_EXTCALL(glVertexAttrib4hvNV(i, (const GLhalfNV *)ptr));
5863 else
5865 float x = float_16_to_32(((const unsigned short *)ptr) + 0);
5866 float y = float_16_to_32(((const unsigned short *)ptr) + 1);
5867 float z = float_16_to_32(((const unsigned short *)ptr) + 2);
5868 float w = float_16_to_32(((const unsigned short *)ptr) + 3);
5869 GL_EXTCALL(glVertexAttrib4f(i, x, y, z, w));
5871 break;
5872 default:
5873 ERR("Unexpected declaration in stride 0 attributes.\n");
5874 break;
5877 context_gl->default_attrib_value_set &= ~(1u << i);
5880 checkGLcall("Loading numbered arrays");
5883 void wined3d_context_gl_update_stream_sources(struct wined3d_context_gl *context_gl,
5884 const struct wined3d_state *state)
5886 if (context_gl->c.use_immediate_mode_draw)
5887 return;
5889 wined3d_context_gl_unload_vertex_data(context_gl);
5890 if (context_gl->c.d3d_info->ffp_generic_attributes || use_vs(state))
5892 TRACE("Loading numbered arrays.\n");
5893 wined3d_context_gl_load_numbered_arrays(context_gl, &context_gl->c.stream_info, state);
5894 return;
5897 TRACE("Loading named arrays.\n");
5898 wined3d_context_gl_unload_numbered_arrays(context_gl);
5899 wined3d_context_gl_load_vertex_data(context_gl, &context_gl->c.stream_info, state);
5900 context_gl->c.namedArraysLoaded = TRUE;
5903 static void apply_texture_blit_state(const struct wined3d_gl_info *gl_info, struct gl_texture *texture,
5904 GLenum target, unsigned int level, enum wined3d_texture_filter_type filter)
5906 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
5907 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
5908 wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
5909 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
5910 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
5911 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
5912 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
5913 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, level);
5915 /* We changed the filtering settings on the texture. Make sure they get
5916 * reset on subsequent draws. */
5917 texture->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
5918 texture->sampler_desc.min_filter = WINED3D_TEXF_POINT;
5919 texture->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
5920 texture->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
5921 texture->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
5922 texture->sampler_desc.srgb_decode = FALSE;
5923 texture->base_level = level;
5926 /* Context activation is done by the caller. */
5927 void wined3d_context_gl_draw_shaded_quad(struct wined3d_context_gl *context_gl, struct wined3d_texture_gl *texture_gl,
5928 unsigned int sub_resource_idx, const RECT *src_rect, const RECT *dst_rect,
5929 enum wined3d_texture_filter_type filter)
5931 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5932 struct wined3d_blt_info info;
5933 unsigned int level, w, h, i;
5934 SIZE dst_size;
5935 struct blit_vertex
5937 float x, y;
5938 struct wined3d_vec3 texcoord;
5940 quad[4];
5942 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
5944 level = sub_resource_idx % texture_gl->t.level_count;
5945 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
5946 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
5947 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
5949 wined3d_context_gl_get_rt_size(context_gl, &dst_size);
5950 w = dst_size.cx;
5951 h = dst_size.cy;
5953 quad[0].x = dst_rect->left * 2.0f / w - 1.0f;
5954 quad[0].y = dst_rect->top * 2.0f / h - 1.0f;
5955 quad[0].texcoord = info.texcoords[0];
5957 quad[1].x = dst_rect->right * 2.0f / w - 1.0f;
5958 quad[1].y = dst_rect->top * 2.0f / h - 1.0f;
5959 quad[1].texcoord = info.texcoords[1];
5961 quad[2].x = dst_rect->left * 2.0f / w - 1.0f;
5962 quad[2].y = dst_rect->bottom * 2.0f / h - 1.0f;
5963 quad[2].texcoord = info.texcoords[2];
5965 quad[3].x = dst_rect->right * 2.0f / w - 1.0f;
5966 quad[3].y = dst_rect->bottom * 2.0f / h - 1.0f;
5967 quad[3].texcoord = info.texcoords[3];
5969 /* Draw a quad. */
5970 if (gl_info->supported[ARB_VERTEX_BUFFER_OBJECT])
5972 if (!context_gl->blit_vbo)
5973 GL_EXTCALL(glGenBuffers(1, &context_gl->blit_vbo));
5974 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, context_gl->blit_vbo));
5976 wined3d_context_gl_unload_vertex_data(context_gl);
5977 wined3d_context_gl_unload_numbered_arrays(context_gl);
5979 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STREAM_DRAW));
5980 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT, FALSE, sizeof(*quad), NULL));
5981 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT, FALSE, sizeof(*quad),
5982 (void *)FIELD_OFFSET(struct blit_vertex, texcoord)));
5984 GL_EXTCALL(glEnableVertexAttribArray(0));
5985 GL_EXTCALL(glEnableVertexAttribArray(1));
5987 gl_info->gl_ops.gl.p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
5989 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER, 0));
5990 GL_EXTCALL(glDisableVertexAttribArray(1));
5991 GL_EXTCALL(glDisableVertexAttribArray(0));
5993 else
5995 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
5997 for (i = 0; i < ARRAY_SIZE(quad); ++i)
5999 GL_EXTCALL(glVertexAttrib3fv(1, &quad[i].texcoord.x));
6000 GL_EXTCALL(glVertexAttrib2fv(0, &quad[i].x));
6003 gl_info->gl_ops.gl.p_glEnd();
6005 checkGLcall("draw");
6007 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
6008 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);
6011 /* Context activation is done by the caller. */
6012 void wined3d_context_gl_draw_textured_quad(struct wined3d_context_gl *context_gl,
6013 struct wined3d_texture_gl *texture_gl, unsigned int sub_resource_idx,
6014 const RECT *src_rect, const RECT *dst_rect, enum wined3d_texture_filter_type filter)
6016 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6017 struct wined3d_blt_info info;
6018 unsigned int level;
6020 texture2d_get_blt_info(texture_gl, sub_resource_idx, src_rect, &info);
6022 gl_info->gl_ops.gl.p_glEnable(info.bind_target);
6023 checkGLcall("glEnable(bind_target)");
6025 level = sub_resource_idx % texture_gl->t.level_count;
6026 wined3d_context_gl_bind_texture(context_gl, info.bind_target, texture_gl->texture_rgb.name);
6027 apply_texture_blit_state(gl_info, &texture_gl->texture_rgb, info.bind_target, level, filter);
6028 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, level);
6029 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
6030 checkGLcall("glTexEnvi");
6032 /* Draw a quad. */
6033 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
6034 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[0].x);
6035 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->top);
6037 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[1].x);
6038 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->top);
6040 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[2].x);
6041 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->bottom);
6043 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[3].x);
6044 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->bottom);
6045 gl_info->gl_ops.gl.p_glEnd();
6047 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
6048 wined3d_context_gl_bind_texture(context_gl, info.bind_target, 0);