2 * Context and render target management in wined3d
4 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
5 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wined3d_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
25 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous
);
28 #define WINED3D_MAX_FBO_ENTRIES 64
29 #define WINED3D_ALL_LAYERS (~0u)
31 static DWORD wined3d_context_tls_idx
;
33 /* FBO helper functions */
35 /* Context activation is done by the caller. */
36 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint fbo
)
38 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
42 case GL_READ_FRAMEBUFFER
:
43 if (context
->fbo_read_binding
== fbo
) return;
44 context
->fbo_read_binding
= fbo
;
47 case GL_DRAW_FRAMEBUFFER
:
48 if (context
->fbo_draw_binding
== fbo
) return;
49 context
->fbo_draw_binding
= fbo
;
53 if (context
->fbo_read_binding
== fbo
54 && context
->fbo_draw_binding
== fbo
) return;
55 context
->fbo_read_binding
= fbo
;
56 context
->fbo_draw_binding
= fbo
;
60 FIXME("Unhandled target %#x.\n", target
);
64 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
65 checkGLcall("glBindFramebuffer()");
68 /* Context activation is done by the caller. */
69 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
73 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
75 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
76 checkGLcall("glFramebufferTexture2D()");
78 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
79 checkGLcall("glFramebufferTexture2D()");
81 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
82 checkGLcall("glFramebufferTexture2D()");
85 /* Context activation is done by the caller. */
86 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint fbo
)
88 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
90 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
91 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
92 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
94 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
95 checkGLcall("glDeleteFramebuffers()");
98 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
99 GLenum fbo_target
, DWORD flags
, GLuint rb
)
101 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
103 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
104 checkGLcall("glFramebufferRenderbuffer()");
107 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
109 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
110 checkGLcall("glFramebufferRenderbuffer()");
114 static void context_attach_gl_texture_fbo(struct wined3d_context
*context
,
115 GLenum fbo_target
, GLenum attachment
, const struct wined3d_fbo_resource
*resource
)
117 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
121 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
, GL_TEXTURE_2D
, 0, 0);
123 else if (resource
->layer
== WINED3D_ALL_LAYERS
)
125 if (!gl_info
->fbo_ops
.glFramebufferTexture
)
127 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
131 gl_info
->fbo_ops
.glFramebufferTexture(fbo_target
, attachment
,
132 resource
->object
, resource
->level
);
134 else if (resource
->target
== GL_TEXTURE_1D_ARRAY
|| resource
->target
== GL_TEXTURE_2D_ARRAY
||
135 resource
->target
== GL_TEXTURE_3D
)
137 if (!gl_info
->fbo_ops
.glFramebufferTextureLayer
)
139 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
143 gl_info
->fbo_ops
.glFramebufferTextureLayer(fbo_target
, attachment
,
144 resource
->object
, resource
->level
, resource
->layer
);
146 else if (resource
->target
== GL_TEXTURE_1D
)
148 gl_info
->fbo_ops
.glFramebufferTexture1D(fbo_target
, attachment
,
149 resource
->target
, resource
->object
, resource
->level
);
150 checkGLcall("glFramebufferTexture1D()");
154 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
,
155 resource
->target
, resource
->object
, resource
->level
);
157 checkGLcall("attach texture to fbo");
160 /* Context activation is done by the caller. */
161 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
162 GLenum fbo_target
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
,
165 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
167 if (resource
->object
)
169 TRACE("Attach depth stencil %u.\n", resource
->object
);
173 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
174 flags
, resource
->object
);
178 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
179 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, resource
);
181 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
182 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, resource
);
185 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
))
186 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
188 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
))
189 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
193 TRACE("Attach depth stencil 0.\n");
195 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
196 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
200 /* Context activation is done by the caller. */
201 static void context_attach_surface_fbo(struct wined3d_context
*context
,
202 GLenum fbo_target
, DWORD idx
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
)
204 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
206 TRACE("Attach GL object %u to %u.\n", resource
->object
, idx
);
208 if (resource
->object
)
213 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
214 GL_RENDERBUFFER
, resource
->object
);
215 checkGLcall("glFramebufferRenderbuffer()");
219 context_attach_gl_texture_fbo(context
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, resource
);
224 context_attach_gl_texture_fbo(context
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, NULL
);
228 static void context_dump_fbo_attachment(const struct wined3d_gl_info
*gl_info
, GLenum target
,
236 enum wined3d_gl_extension extension
;
240 {GL_TEXTURE_2D
, GL_TEXTURE_BINDING_2D
, "2d", WINED3D_GL_EXT_NONE
},
241 {GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_BINDING_RECTANGLE_ARB
, "rectangle", ARB_TEXTURE_RECTANGLE
},
242 {GL_TEXTURE_2D_ARRAY
, GL_TEXTURE_BINDING_2D_ARRAY
, "2d-array", EXT_TEXTURE_ARRAY
},
245 GLint type
, name
, samples
, width
, height
, old_texture
, level
, face
, fmt
, tex_target
;
247 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
248 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
, &name
);
249 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
250 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
, &type
);
252 if (type
== GL_RENDERBUFFER
)
254 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, name
);
255 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_WIDTH
, &width
);
256 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_HEIGHT
, &height
);
257 if (gl_info
->limits
.samples
> 1)
258 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_SAMPLES
, &samples
);
261 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_INTERNAL_FORMAT
, &fmt
);
262 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
263 debug_fboattachment(attachment
), name
, width
, height
, samples
, fmt
);
265 else if (type
== GL_TEXTURE
)
267 const char *tex_type_str
;
269 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
270 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
, &level
);
271 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
272 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
, &face
);
276 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP
, &old_texture
);
278 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, name
);
279 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
280 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_WIDTH
, &width
);
281 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_HEIGHT
, &height
);
283 tex_target
= GL_TEXTURE_CUBE_MAP
;
284 tex_type_str
= "cube";
291 for (i
= 0; i
< sizeof(texture_type
) / sizeof(*texture_type
); ++i
)
293 if (!gl_info
->supported
[texture_type
[i
].extension
])
296 gl_info
->gl_ops
.gl
.p_glGetIntegerv(texture_type
[i
].binding
, &old_texture
);
297 while (gl_info
->gl_ops
.gl
.p_glGetError());
299 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, name
);
300 if (!gl_info
->gl_ops
.gl
.p_glGetError())
302 tex_target
= texture_type
[i
].target
;
303 tex_type_str
= texture_type
[i
].str
;
306 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, old_texture
);
310 FIXME("Cannot find type of texture %d.\n", name
);
314 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
315 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_WIDTH
, &width
);
316 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_HEIGHT
, &height
);
319 FIXME(" %s: %s texture %d, %dx%d, format %#x.\n", debug_fboattachment(attachment
),
320 tex_type_str
, name
, width
, height
, fmt
);
322 gl_info
->gl_ops
.gl
.p_glBindTexture(tex_target
, old_texture
);
323 checkGLcall("Guess texture type");
325 else if (type
== GL_NONE
)
327 FIXME(" %s: NONE.\n", debug_fboattachment(attachment
));
331 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment
), type
);
335 /* Context activation is done by the caller. */
336 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
338 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
341 if (!FIXME_ON(d3d
)) return;
343 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
344 if (status
== GL_FRAMEBUFFER_COMPLETE
)
346 TRACE("FBO complete\n");
352 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
354 if (!context
->current_fbo
)
356 ERR("FBO 0 is incomplete, driver bug?\n");
360 context_dump_fbo_attachment(gl_info
, target
, GL_DEPTH_ATTACHMENT
);
361 context_dump_fbo_attachment(gl_info
, target
, GL_STENCIL_ATTACHMENT
);
363 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
364 context_dump_fbo_attachment(gl_info
, target
, GL_COLOR_ATTACHMENT0
+ i
);
365 checkGLcall("Dump FBO attachments");
369 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
371 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
372 return buffer
? (1u << 31) | buffer
: 0;
375 static inline DWORD
context_generate_rt_mask_from_resource(struct wined3d_resource
*resource
)
377 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
379 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
383 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource
));
386 static inline void context_set_fbo_key_for_render_target(const struct wined3d_context
*context
,
387 struct wined3d_fbo_entry_key
*key
, unsigned int idx
, struct wined3d_rendertarget_info
*render_target
,
390 unsigned int sub_resource_idx
= render_target
->sub_resource_idx
;
391 struct wined3d_resource
*resource
= render_target
->resource
;
392 struct wined3d_texture
*texture
;
394 if (!resource
|| resource
->format
->id
== WINED3DFMT_NULL
|| resource
->type
== WINED3D_RTYPE_BUFFER
)
396 if (resource
&& resource
->type
== WINED3D_RTYPE_BUFFER
)
397 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
398 key
->objects
[idx
].object
= 0;
399 key
->objects
[idx
].target
= 0;
400 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
404 if (render_target
->gl_view
.name
)
406 key
->objects
[idx
].object
= render_target
->gl_view
.name
;
407 key
->objects
[idx
].target
= render_target
->gl_view
.target
;
408 key
->objects
[idx
].level
= 0;
409 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
413 texture
= wined3d_texture_from_resource(resource
);
414 if (resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
416 struct wined3d_surface
*surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
418 if (surface
->current_renderbuffer
)
420 key
->objects
[idx
].object
= surface
->current_renderbuffer
->id
;
421 key
->objects
[idx
].target
= 0;
422 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
423 key
->rb_namespace
|= 1 << idx
;
427 key
->objects
[idx
].target
= surface
->texture_target
;
428 key
->objects
[idx
].level
= surface
->texture_level
;
429 key
->objects
[idx
].layer
= surface
->texture_layer
;
433 key
->objects
[idx
].target
= texture
->target
;
434 key
->objects
[idx
].level
= sub_resource_idx
% texture
->level_count
;
435 key
->objects
[idx
].layer
= sub_resource_idx
/ texture
->level_count
;
437 if (render_target
->layer_count
!= 1)
438 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
442 case WINED3D_LOCATION_TEXTURE_RGB
:
443 key
->objects
[idx
].object
= wined3d_texture_get_texture_name(texture
, context
, FALSE
);
446 case WINED3D_LOCATION_TEXTURE_SRGB
:
447 key
->objects
[idx
].object
= wined3d_texture_get_texture_name(texture
, context
, TRUE
);
450 case WINED3D_LOCATION_RB_MULTISAMPLE
:
451 key
->objects
[idx
].object
= texture
->rb_multisample
;
452 key
->objects
[idx
].target
= 0;
453 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
454 key
->rb_namespace
|= 1 << idx
;
457 case WINED3D_LOCATION_RB_RESOLVED
:
458 key
->objects
[idx
].object
= texture
->rb_resolved
;
459 key
->objects
[idx
].target
= 0;
460 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
461 key
->rb_namespace
|= 1 << idx
;
466 static void context_generate_fbo_key(const struct wined3d_context
*context
,
467 struct wined3d_fbo_entry_key
*key
, struct wined3d_rendertarget_info
*render_targets
,
468 struct wined3d_surface
*depth_stencil_surface
, DWORD color_location
,
471 struct wined3d_rendertarget_info depth_stencil
= {{0}};
474 key
->rb_namespace
= 0;
475 if (depth_stencil_surface
)
477 depth_stencil
.resource
= &depth_stencil_surface
->container
->resource
;
478 depth_stencil
.sub_resource_idx
= surface_get_sub_resource_idx(depth_stencil_surface
);
479 depth_stencil
.layer_count
= 1;
481 context_set_fbo_key_for_render_target(context
, key
, 0, &depth_stencil
, ds_location
);
483 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
484 context_set_fbo_key_for_render_target(context
, key
, i
+ 1, &render_targets
[i
], color_location
);
487 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
488 struct wined3d_rendertarget_info
*render_targets
, struct wined3d_surface
*depth_stencil
,
489 DWORD color_location
, DWORD ds_location
)
491 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
492 unsigned int object_count
= gl_info
->limits
.buffers
+ 1;
493 struct fbo_entry
*entry
;
495 entry
= HeapAlloc(GetProcessHeap(), 0,
496 FIELD_OFFSET(struct fbo_entry
, key
.objects
[object_count
]));
497 memset(&entry
->key
, 0, FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[object_count
]));
498 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
502 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
)
503 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
504 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_STENCIL
)
505 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
507 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
508 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
509 checkGLcall("glGenFramebuffers()");
510 TRACE("Created FBO %u.\n", entry
->id
);
515 /* Context activation is done by the caller. */
516 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
517 struct wined3d_rendertarget_info
*render_targets
, struct wined3d_surface
*depth_stencil
,
518 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
520 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
522 context_bind_fbo(context
, target
, entry
->id
);
523 context_clean_fbo_attachments(gl_info
, target
);
525 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
529 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
)
530 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
531 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_STENCIL
)
532 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
536 /* Context activation is done by the caller. */
537 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
541 TRACE("Destroy FBO %u.\n", entry
->id
);
542 context_destroy_fbo(context
, entry
->id
);
544 --context
->fbo_entry_count
;
545 list_remove(&entry
->entry
);
546 HeapFree(GetProcessHeap(), 0, entry
);
549 /* Context activation is done by the caller. */
550 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
551 struct wined3d_rendertarget_info
*render_targets
, struct wined3d_surface
*depth_stencil
,
552 DWORD color_location
, DWORD ds_location
)
554 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
555 unsigned int object_count
= gl_info
->limits
.buffers
+ 1;
556 struct wined3d_texture
*rt_texture
, *ds_texture
;
557 struct fbo_entry
*entry
;
558 unsigned int i
, level
;
560 if (depth_stencil
&& render_targets
[0].resource
&& render_targets
[0].resource
->type
!= WINED3D_RTYPE_BUFFER
)
562 rt_texture
= wined3d_texture_from_resource(render_targets
[0].resource
);
563 level
= render_targets
[0].sub_resource_idx
% rt_texture
->level_count
;
564 ds_texture
= depth_stencil
->container
;
566 if (wined3d_texture_get_level_width(ds_texture
, depth_stencil
->texture_level
)
567 < wined3d_texture_get_level_width(rt_texture
, level
)
568 || wined3d_texture_get_level_height(ds_texture
, depth_stencil
->texture_level
)
569 < wined3d_texture_get_level_height(rt_texture
, level
))
571 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
572 depth_stencil
= NULL
;
574 else if (ds_texture
->resource
.multisample_type
!= rt_texture
->resource
.multisample_type
575 || ds_texture
->resource
.multisample_quality
!= rt_texture
->resource
.multisample_quality
)
577 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
578 rt_texture
->resource
.multisample_type
, rt_texture
->resource
.multisample_quality
,
579 ds_texture
->resource
.multisample_type
, ds_texture
->resource
.multisample_quality
);
580 depth_stencil
= NULL
;
583 surface_set_compatible_renderbuffer(depth_stencil
, &render_targets
[0]);
586 context_generate_fbo_key(context
, context
->fbo_key
, render_targets
, depth_stencil
, color_location
,
591 TRACE("Dumping FBO attachments:\n");
592 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
594 struct wined3d_resource
*resource
;
595 if ((resource
= render_targets
[i
].resource
))
597 unsigned int width
, height
;
598 const char *resource_type
;
600 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
602 width
= resource
->size
;
604 resource_type
= "buffer";
608 rt_texture
= wined3d_texture_from_resource(resource
);
609 level
= render_targets
[i
].sub_resource_idx
% rt_texture
->level_count
;
610 width
= wined3d_texture_get_level_pow2_width(rt_texture
, level
);
611 height
= wined3d_texture_get_level_pow2_height(rt_texture
, level
);
612 resource_type
= "texture";
615 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
616 i
, resource
, render_targets
[i
].sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
617 context
->fbo_key
->rb_namespace
& (1 << (i
+ 1)) ? "renderbuffer" : resource_type
,
618 context
->fbo_key
->objects
[i
+ 1].object
, width
, height
, resource
->multisample_type
);
623 ds_texture
= depth_stencil
->container
;
624 TRACE(" Depth attachment: %p format %s, %s %u, %ux%u, %u samples.\n",
625 depth_stencil
, debug_d3dformat(ds_texture
->resource
.format
->id
),
626 context
->fbo_key
->rb_namespace
& (1 << 0) ? "renderbuffer" : "texture",
627 context
->fbo_key
->objects
[0].object
,
628 wined3d_texture_get_level_pow2_width(ds_texture
, depth_stencil
->texture_level
),
629 wined3d_texture_get_level_pow2_height(ds_texture
, depth_stencil
->texture_level
),
630 ds_texture
->resource
.multisample_type
);
634 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
636 if (memcmp(context
->fbo_key
, &entry
->key
, FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[object_count
])))
639 list_remove(&entry
->entry
);
640 list_add_head(&context
->fbo_list
, &entry
->entry
);
644 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
646 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, color_location
, ds_location
);
647 list_add_head(&context
->fbo_list
, &entry
->entry
);
648 ++context
->fbo_entry_count
;
652 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
653 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, color_location
, ds_location
, entry
);
654 list_remove(&entry
->entry
);
655 list_add_head(&context
->fbo_list
, &entry
->entry
);
661 /* Context activation is done by the caller. */
662 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
664 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
666 GLuint read_binding
, draw_binding
;
668 if (entry
->flags
& WINED3D_FBO_ENTRY_FLAG_ATTACHED
)
670 context_bind_fbo(context
, target
, entry
->id
);
674 read_binding
= context
->fbo_read_binding
;
675 draw_binding
= context
->fbo_draw_binding
;
676 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
678 /* Apply render targets */
679 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
681 context_attach_surface_fbo(context
, target
, i
, &entry
->key
.objects
[i
+ 1],
682 entry
->key
.rb_namespace
& (1 << (i
+ 1)));
685 context_attach_depth_stencil_fbo(context
, target
, &entry
->key
.objects
[0],
686 entry
->key
.rb_namespace
& 0x1, entry
->flags
);
688 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
689 * GL contexts requirements. */
690 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
691 context_set_draw_buffer(context
, GL_NONE
);
692 if (target
!= GL_FRAMEBUFFER
)
694 if (target
== GL_READ_FRAMEBUFFER
)
695 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
697 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
700 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_ATTACHED
;
703 /* Context activation is done by the caller. */
704 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
705 struct wined3d_rendertarget_info
*render_targets
, struct wined3d_surface
*depth_stencil
,
706 DWORD color_location
, DWORD ds_location
)
708 struct fbo_entry
*entry
, *entry2
;
710 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
712 context_destroy_fbo_entry(context
, entry
);
715 if (context
->rebind_fbo
)
717 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
718 context
->rebind_fbo
= FALSE
;
721 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
723 context
->current_fbo
= NULL
;
724 context_bind_fbo(context
, target
, 0);
728 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
,
729 color_location
, ds_location
);
730 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
734 /* Context activation is done by the caller. */
735 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
736 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
738 memset(context
->blit_targets
, 0, context
->gl_info
->limits
.buffers
* sizeof(*context
->blit_targets
));
741 context
->blit_targets
[0].resource
= &render_target
->container
->resource
;
742 context
->blit_targets
[0].sub_resource_idx
= surface_get_sub_resource_idx(render_target
);
743 context
->blit_targets
[0].layer_count
= 1;
745 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
, location
);
748 /* Context activation is done by the caller. */
749 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
751 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
753 if (context
->free_occlusion_query_count
)
755 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
759 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
761 GL_EXTCALL(glGenQueries(1, &query
->id
));
762 checkGLcall("glGenQueries");
764 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
768 WARN("Occlusion queries not supported, not allocating query id.\n");
773 query
->context
= context
;
774 list_add_head(&context
->occlusion_queries
, &query
->entry
);
777 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
779 struct wined3d_context
*context
= query
->context
;
781 list_remove(&query
->entry
);
782 query
->context
= NULL
;
784 if (!wined3d_array_reserve((void **)&context
->free_occlusion_queries
,
785 &context
->free_occlusion_query_size
, context
->free_occlusion_query_count
+ 1,
786 sizeof(*context
->free_occlusion_queries
)))
788 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
792 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
795 /* Context activation is done by the caller. */
796 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
798 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
800 if (context
->free_event_query_count
)
802 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
806 if (gl_info
->supported
[ARB_SYNC
])
808 /* Using ARB_sync, not much to do here. */
809 query
->object
.sync
= NULL
;
810 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
812 else if (gl_info
->supported
[APPLE_FENCE
])
814 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
815 checkGLcall("glGenFencesAPPLE");
817 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
819 else if(gl_info
->supported
[NV_FENCE
])
821 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
822 checkGLcall("glGenFencesNV");
824 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
828 WARN("Event queries not supported, not allocating query id.\n");
829 query
->object
.id
= 0;
833 query
->context
= context
;
834 list_add_head(&context
->event_queries
, &query
->entry
);
837 void context_free_event_query(struct wined3d_event_query
*query
)
839 struct wined3d_context
*context
= query
->context
;
841 list_remove(&query
->entry
);
842 query
->context
= NULL
;
844 if (!wined3d_array_reserve((void **)&context
->free_event_queries
,
845 &context
->free_event_query_size
, context
->free_event_query_count
+ 1,
846 sizeof(*context
->free_event_queries
)))
848 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
852 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
855 /* Context activation is done by the caller. */
856 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
858 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
860 if (context
->free_timestamp_query_count
)
862 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
866 GL_EXTCALL(glGenQueries(1, &query
->id
));
867 checkGLcall("glGenQueries");
869 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
872 query
->context
= context
;
873 list_add_head(&context
->timestamp_queries
, &query
->entry
);
876 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
878 struct wined3d_context
*context
= query
->context
;
880 list_remove(&query
->entry
);
881 query
->context
= NULL
;
883 if (!wined3d_array_reserve((void **)&context
->free_timestamp_queries
,
884 &context
->free_timestamp_query_size
, context
->free_timestamp_query_count
+ 1,
885 sizeof(*context
->free_timestamp_queries
)))
887 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
891 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
894 void context_alloc_so_statistics_query(struct wined3d_context
*context
,
895 struct wined3d_so_statistics_query
*query
)
897 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
899 if (context
->free_so_statistics_query_count
)
901 query
->u
= context
->free_so_statistics_queries
[--context
->free_so_statistics_query_count
];
905 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
906 checkGLcall("glGenQueries");
908 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
909 query
->u
.id
[0], query
->u
.id
[1], context
);
912 query
->context
= context
;
913 list_add_head(&context
->so_statistics_queries
, &query
->entry
);
916 void context_free_so_statistics_query(struct wined3d_so_statistics_query
*query
)
918 struct wined3d_context
*context
= query
->context
;
920 list_remove(&query
->entry
);
921 query
->context
= NULL
;
923 if (!wined3d_array_reserve((void **)&context
->free_so_statistics_queries
,
924 &context
->free_so_statistics_query_size
, context
->free_so_statistics_query_count
+ 1,
925 sizeof(*context
->free_so_statistics_queries
)))
927 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
928 query
->u
.id
[0], query
->u
.id
[1], context
);
932 context
->free_so_statistics_queries
[context
->free_so_statistics_query_count
++] = query
->u
;
935 void context_alloc_pipeline_statistics_query(struct wined3d_context
*context
,
936 struct wined3d_pipeline_statistics_query
*query
)
938 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
940 if (context
->free_pipeline_statistics_query_count
)
942 query
->u
= context
->free_pipeline_statistics_queries
[--context
->free_pipeline_statistics_query_count
];
946 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
947 checkGLcall("glGenQueries");
950 query
->context
= context
;
951 list_add_head(&context
->pipeline_statistics_queries
, &query
->entry
);
954 void context_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query
*query
)
956 struct wined3d_context
*context
= query
->context
;
958 list_remove(&query
->entry
);
959 query
->context
= NULL
;
961 if (!wined3d_array_reserve((void **)&context
->free_pipeline_statistics_queries
,
962 &context
->free_pipeline_statistics_query_size
, context
->free_pipeline_statistics_query_count
+ 1,
963 sizeof(*context
->free_pipeline_statistics_queries
)))
965 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context
);
969 context
->free_pipeline_statistics_queries
[context
->free_pipeline_statistics_query_count
++] = query
->u
;
972 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
974 static void context_enum_fbo_entries(const struct wined3d_device
*device
,
975 GLuint name
, BOOL rb_namespace
, context_fbo_entry_func_t
*callback
)
979 for (i
= 0; i
< device
->context_count
; ++i
)
981 struct wined3d_context
*context
= device
->contexts
[i
];
982 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
983 struct fbo_entry
*entry
, *entry2
;
985 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
989 for (j
= 0; j
< gl_info
->limits
.buffers
+ 1; ++j
)
991 if (entry
->key
.objects
[j
].object
== name
992 && !(entry
->key
.rb_namespace
& (1 << j
)) == !rb_namespace
)
994 callback(context
, entry
);
1002 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
1004 list_remove(&entry
->entry
);
1005 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
1008 void context_resource_released(const struct wined3d_device
*device
,
1009 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
1011 struct wined3d_texture
*texture
;
1014 if (!device
->d3d_initialized
)
1019 case WINED3D_RTYPE_TEXTURE_2D
:
1020 case WINED3D_RTYPE_TEXTURE_3D
:
1021 texture
= texture_from_resource(resource
);
1023 for (i
= 0; i
< device
->context_count
; ++i
)
1025 struct wined3d_context
*context
= device
->contexts
[i
];
1026 if (context
->current_rt
.texture
== texture
)
1028 context
->current_rt
.texture
= NULL
;
1029 context
->current_rt
.sub_resource_idx
= 0;
1039 void context_gl_resource_released(struct wined3d_device
*device
,
1040 GLuint name
, BOOL rb_namespace
)
1042 context_enum_fbo_entries(device
, name
, rb_namespace
, context_queue_fbo_entry_destruction
);
1045 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
1047 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1048 struct fbo_entry
*entry
= context
->current_fbo
;
1051 if (!entry
|| context
->rebind_fbo
) return;
1053 for (i
= 0; i
< gl_info
->limits
.buffers
+ 1; ++i
)
1055 if (surface
->container
->texture_rgb
.name
== entry
->key
.objects
[i
].object
1056 || surface
->container
->texture_srgb
.name
== entry
->key
.objects
[i
].object
)
1058 TRACE("Updated surface %p is bound as attachment %u to the current FBO.\n", surface
, i
);
1059 context
->rebind_fbo
= TRUE
;
1065 static BOOL
context_restore_pixel_format(struct wined3d_context
*ctx
)
1067 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
1070 if (ctx
->restore_pf
&& IsWindow(ctx
->restore_pf_win
))
1072 if (ctx
->gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1074 HDC dc
= GetDCEx(ctx
->restore_pf_win
, 0, DCX_USESTYLE
| DCX_CACHE
);
1077 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, ctx
->restore_pf
))))
1079 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
1080 ctx
->restore_pf
, ctx
->restore_pf_win
);
1082 ReleaseDC(ctx
->restore_pf_win
, dc
);
1087 ERR("can't restore pixel format %d on window %p\n", ctx
->restore_pf
, ctx
->restore_pf_win
);
1091 ctx
->restore_pf
= 0;
1092 ctx
->restore_pf_win
= NULL
;
1096 static BOOL
context_set_pixel_format(struct wined3d_context
*context
, HDC dc
, BOOL
private, int format
)
1098 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1101 if (dc
== context
->hdc
&& context
->hdc_is_private
&& context
->hdc_has_format
)
1104 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
1105 if (current
== format
) goto success
;
1109 if (!SetPixelFormat(dc
, format
, NULL
))
1111 /* This may also happen if the dc belongs to a destroyed window. */
1112 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1113 format
, dc
, GetLastError());
1117 context
->restore_pf
= 0;
1118 context
->restore_pf_win
= private ? NULL
: WindowFromDC(dc
);
1122 /* By default WGL doesn't allow pixel format adjustments but we need it
1123 * here. For this reason there's a Wine specific wglSetPixelFormat()
1124 * which allows us to set the pixel format multiple times. Only use it
1125 * when really needed. */
1126 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1130 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
1132 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1137 win
= private ? NULL
: WindowFromDC(dc
);
1138 if (win
!= context
->restore_pf_win
)
1140 context_restore_pixel_format(context
);
1142 context
->restore_pf
= private ? 0 : current
;
1143 context
->restore_pf_win
= win
;
1149 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1150 * continue using the old format. There's a big chance that the old
1151 * format works although with a performance hit and perhaps rendering
1153 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1154 format
, dc
, current
);
1158 if (dc
== context
->hdc
&& context
->hdc_is_private
)
1159 context
->hdc_has_format
= TRUE
;
1163 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
1165 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
1166 BOOL backup
= FALSE
;
1168 if (!context_set_pixel_format(ctx
, ctx
->hdc
, ctx
->hdc_is_private
, ctx
->pixel_format
))
1170 WARN("Failed to set pixel format %d on device context %p.\n",
1171 ctx
->pixel_format
, ctx
->hdc
);
1175 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
1179 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1180 ctx
->glCtx
, ctx
->hdc
, GetLastError());
1182 WARN("Trying fallback to the backup window.\n");
1184 /* FIXME: If the context is destroyed it's no longer associated with
1185 * a swapchain, so we can't use the swapchain to get a backup dc. To
1186 * make this work windowless contexts would need to be handled by the
1188 if (ctx
->destroyed
|| !swapchain
)
1190 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
1191 context_set_current(NULL
);
1195 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
1197 context_set_current(NULL
);
1201 if (!context_set_pixel_format(ctx
, dc
, TRUE
, ctx
->pixel_format
))
1203 ERR("Failed to set pixel format %d on device context %p.\n",
1204 ctx
->pixel_format
, dc
);
1205 context_set_current(NULL
);
1209 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
1211 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1212 dc
, GetLastError());
1213 context_set_current(NULL
);
1223 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
1225 if (!wglMakeCurrent(dc
, gl_ctx
))
1227 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1228 gl_ctx
, dc
, GetLastError());
1229 context_set_current(NULL
);
1233 static void context_update_window(struct wined3d_context
*context
)
1235 if (!context
->swapchain
)
1238 if (context
->win_handle
== context
->swapchain
->win_handle
)
1241 TRACE("Updating context %p window from %p to %p.\n",
1242 context
, context
->win_handle
, context
->swapchain
->win_handle
);
1245 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1247 context
->win_handle
= context
->swapchain
->win_handle
;
1248 context
->hdc_is_private
= FALSE
;
1249 context
->hdc_has_format
= FALSE
;
1250 context
->needs_set
= 1;
1253 if (!(context
->hdc
= GetDCEx(context
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1255 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
1260 static void context_destroy_gl_resources(struct wined3d_context
*context
)
1262 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1263 struct wined3d_so_statistics_query
*so_statistics_query
;
1264 struct wined3d_pipeline_statistics_query
*pipeline_statistics_query
;
1265 struct wined3d_timestamp_query
*timestamp_query
;
1266 struct wined3d_occlusion_query
*occlusion_query
;
1267 struct wined3d_event_query
*event_query
;
1268 struct fbo_entry
*entry
, *entry2
;
1273 restore_ctx
= wglGetCurrentContext();
1274 restore_dc
= wglGetCurrentDC();
1276 if (restore_ctx
== context
->glCtx
)
1278 else if (context
->valid
)
1279 context_set_gl_context(context
);
1281 LIST_FOR_EACH_ENTRY(so_statistics_query
, &context
->so_statistics_queries
,
1282 struct wined3d_so_statistics_query
, entry
)
1285 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query
->u
.id
), so_statistics_query
->u
.id
));
1286 so_statistics_query
->context
= NULL
;
1289 LIST_FOR_EACH_ENTRY(pipeline_statistics_query
, &context
->pipeline_statistics_queries
,
1290 struct wined3d_pipeline_statistics_query
, entry
)
1293 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query
->u
.id
), pipeline_statistics_query
->u
.id
));
1294 pipeline_statistics_query
->context
= NULL
;
1297 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
1300 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
1301 timestamp_query
->context
= NULL
;
1304 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1306 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1307 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1308 occlusion_query
->context
= NULL
;
1311 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
1315 if (gl_info
->supported
[ARB_SYNC
])
1317 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
1319 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
1320 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
1322 event_query
->context
= NULL
;
1325 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
1327 if (!context
->valid
) entry
->id
= 0;
1328 context_destroy_fbo_entry(context
, entry
);
1331 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1333 if (!context
->valid
) entry
->id
= 0;
1334 context_destroy_fbo_entry(context
, entry
);
1339 if (context
->dummy_arbfp_prog
)
1341 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
1344 if (gl_info
->supported
[WINED3D_GL_PRIMITIVE_QUERY
])
1346 for (i
= 0; i
< context
->free_so_statistics_query_count
; ++i
)
1348 union wined3d_gl_so_statistics_query
*q
= &context
->free_so_statistics_queries
[i
];
1349 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1353 if (gl_info
->supported
[ARB_PIPELINE_STATISTICS_QUERY
])
1355 for (i
= 0; i
< context
->free_pipeline_statistics_query_count
; ++i
)
1357 union wined3d_gl_pipeline_statistics_query
*q
= &context
->free_pipeline_statistics_queries
[i
];
1358 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1362 if (gl_info
->supported
[ARB_TIMER_QUERY
])
1363 GL_EXTCALL(glDeleteQueries(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
1365 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1366 GL_EXTCALL(glDeleteQueries(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
1368 if (gl_info
->supported
[ARB_SYNC
])
1370 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1372 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
1375 else if (gl_info
->supported
[APPLE_FENCE
])
1377 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1379 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1382 else if (gl_info
->supported
[NV_FENCE
])
1384 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1386 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1390 checkGLcall("context cleanup");
1393 HeapFree(GetProcessHeap(), 0, context
->free_so_statistics_queries
);
1394 HeapFree(GetProcessHeap(), 0, context
->free_pipeline_statistics_queries
);
1395 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1396 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1397 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1399 context_restore_pixel_format(context
);
1402 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1404 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1406 ERR("Failed to disable GL context.\n");
1409 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1411 if (!wglDeleteContext(context
->glCtx
))
1413 DWORD err
= GetLastError();
1414 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1418 DWORD
context_get_tls_idx(void)
1420 return wined3d_context_tls_idx
;
1423 void context_set_tls_idx(DWORD idx
)
1425 wined3d_context_tls_idx
= idx
;
1428 struct wined3d_context
*context_get_current(void)
1430 return TlsGetValue(wined3d_context_tls_idx
);
1433 BOOL
context_set_current(struct wined3d_context
*ctx
)
1435 struct wined3d_context
*old
= context_get_current();
1439 TRACE("Already using D3D context %p.\n", ctx
);
1447 TRACE("Switching away from destroyed context %p.\n", old
);
1448 context_destroy_gl_resources(old
);
1449 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1450 HeapFree(GetProcessHeap(), 0, old
);
1454 if (wglGetCurrentContext())
1456 const struct wined3d_gl_info
*gl_info
= old
->gl_info
;
1457 TRACE("Flushing context %p before switching to %p.\n", old
, ctx
);
1458 gl_info
->gl_ops
.gl
.p_glFlush();
1468 ERR("Trying to make invalid context %p current\n", ctx
);
1472 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1473 if (!context_set_gl_context(ctx
))
1477 else if (wglGetCurrentContext())
1479 TRACE("Clearing current D3D context.\n");
1480 if (!wglMakeCurrent(NULL
, NULL
))
1482 DWORD err
= GetLastError();
1483 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1484 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1489 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1492 void context_release(struct wined3d_context
*context
)
1494 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1498 if (!context
->level
)
1499 WARN("Context %p is not active.\n", context
);
1500 else if (context
!= context_get_current())
1501 WARN("Context %p is not the current context.\n", context
);
1504 if (!--context
->level
)
1506 if (context_restore_pixel_format(context
))
1507 context
->needs_set
= 1;
1508 if (context
->restore_ctx
)
1510 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1511 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
);
1512 context
->restore_ctx
= NULL
;
1513 context
->restore_dc
= NULL
;
1516 if (context
->destroy_delayed
)
1518 TRACE("Destroying context %p.\n", context
);
1519 context_destroy(context
->device
, context
);
1524 /* This is used when a context for render target A is active, but a separate context is
1525 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1526 * A to avoid breaking caller code. */
1527 void context_restore(struct wined3d_context
*context
, struct wined3d_surface
*restore
)
1529 if (context
->current_rt
.texture
!= restore
->container
1530 || context
->current_rt
.sub_resource_idx
!= surface_get_sub_resource_idx(restore
))
1532 context_release(context
);
1533 context
= context_acquire(restore
->container
->resource
.device
,
1534 restore
->container
, surface_get_sub_resource_idx(restore
));
1537 context_release(context
);
1540 static void context_enter(struct wined3d_context
*context
)
1542 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1544 if (!context
->level
++)
1546 const struct wined3d_context
*current_context
= context_get_current();
1547 HGLRC current_gl
= wglGetCurrentContext();
1549 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1551 TRACE("Another GL context (%p on device context %p) is already current.\n",
1552 current_gl
, wglGetCurrentDC());
1553 context
->restore_ctx
= current_gl
;
1554 context
->restore_dc
= wglGetCurrentDC();
1555 context
->needs_set
= 1;
1557 else if (!context
->needs_set
&& !(context
->hdc_is_private
&& context
->hdc_has_format
)
1558 && context
->pixel_format
!= context
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context
->hdc
))
1559 context
->needs_set
= 1;
1563 void context_invalidate_compute_state(struct wined3d_context
*context
, DWORD state_id
)
1565 DWORD representative
= context
->state_table
[state_id
].representative
- STATE_COMPUTE_OFFSET
;
1566 unsigned int index
, shift
;
1568 index
= representative
/ (sizeof(*context
->dirty_compute_states
) * CHAR_BIT
);
1569 shift
= representative
& (sizeof(*context
->dirty_compute_states
) * CHAR_BIT
- 1);
1570 context
->dirty_compute_states
[index
] |= (1u << shift
);
1573 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1575 DWORD rep
= context
->state_table
[state
].representative
;
1579 if (isStateDirty(context
, rep
)) return;
1581 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1582 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1583 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1584 context
->isStateDirty
[idx
] |= (1u << shift
);
1587 /* This function takes care of wined3d pixel format selection. */
1588 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1589 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1592 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1593 unsigned int current_value
;
1594 PIXELFORMATDESCRIPTOR pfd
;
1595 int iPixelFormat
= 0;
1598 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1599 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1603 for (i
= 0; i
< cfg_count
; ++i
)
1605 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1608 /* For now only accept RGBA formats. Perhaps some day we will
1609 * allow floating point formats for pbuffers. */
1610 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1612 /* In window mode we need a window drawable format and double buffering. */
1613 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1615 if (cfg
->redSize
< color_format
->red_size
)
1617 if (cfg
->greenSize
< color_format
->green_size
)
1619 if (cfg
->blueSize
< color_format
->blue_size
)
1621 if (cfg
->alphaSize
< color_format
->alpha_size
)
1623 if (cfg
->depthSize
< ds_format
->depth_size
)
1625 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1627 /* Check multisampling support. */
1628 if (cfg
->numSamples
)
1632 /* We try to locate a format which matches our requirements exactly. In case of
1633 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1634 if (cfg
->depthSize
== ds_format
->depth_size
)
1636 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1638 if (cfg
->alphaSize
== color_format
->alpha_size
)
1640 /* We like to have aux buffers in backbuffer mode */
1641 if (auxBuffers
&& cfg
->auxBuffers
)
1643 if (cfg
->redSize
== color_format
->red_size
1644 && cfg
->greenSize
== color_format
->green_size
1645 && cfg
->blueSize
== color_format
->blue_size
)
1648 if (value
> current_value
)
1650 iPixelFormat
= cfg
->iPixelFormat
;
1651 current_value
= value
;
1657 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1659 memset(&pfd
, 0, sizeof(pfd
));
1660 pfd
.nSize
= sizeof(pfd
);
1662 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1663 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1664 pfd
.cAlphaBits
= color_format
->alpha_size
;
1665 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1666 + color_format
->blue_size
+ color_format
->alpha_size
;
1667 pfd
.cDepthBits
= ds_format
->depth_size
;
1668 pfd
.cStencilBits
= ds_format
->stencil_size
;
1669 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1671 if (!(iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
)))
1673 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1674 ERR("Can't find a suitable pixel format.\n");
1679 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1680 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1681 return iPixelFormat
;
1684 /* Context activation is done by the caller. */
1685 void context_bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1687 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1690 for (i
= 0; i
< gl_info
->limits
.combined_samplers
; ++i
)
1692 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1693 checkGLcall("glActiveTexture");
1695 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, device
->dummy_textures
.tex_1d
);
1696 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_textures
.tex_2d
);
1698 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1699 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_textures
.tex_rect
);
1701 if (gl_info
->supported
[EXT_TEXTURE3D
])
1702 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_textures
.tex_3d
);
1704 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1705 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_textures
.tex_cube
);
1707 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
1708 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, device
->dummy_textures
.tex_cube_array
);
1710 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
1712 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, device
->dummy_textures
.tex_1d_array
);
1713 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, device
->dummy_textures
.tex_2d_array
);
1716 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
1717 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, device
->dummy_textures
.tex_buffer
);
1719 checkGLcall("Bind dummy textures");
1723 void wined3d_check_gl_call(const struct wined3d_gl_info
*gl_info
,
1724 const char *file
, unsigned int line
, const char *name
)
1728 if (gl_info
->supported
[ARB_DEBUG_OUTPUT
] || (err
= gl_info
->gl_ops
.gl
.p_glGetError()) == GL_NO_ERROR
)
1730 TRACE("%s call ok %s / %u.\n", name
, file
, line
);
1736 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1737 debug_glerror(err
), err
, name
, file
,line
);
1738 err
= gl_info
->gl_ops
.gl
.p_glGetError();
1739 } while (err
!= GL_NO_ERROR
);
1742 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1744 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1745 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1748 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1749 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1753 case GL_DEBUG_TYPE_ERROR_ARB
:
1754 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1757 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1758 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1759 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1760 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1763 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1764 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1768 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1773 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1776 unsigned int ctx_attrib_idx
= 0;
1777 GLint ctx_attribs
[7], ctx_flags
= 0;
1779 if (context_debug_output_enabled(gl_info
))
1780 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1781 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1782 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1783 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1784 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1785 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1786 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1789 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1790 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1792 ctx_attribs
[ctx_attrib_idx
] = 0;
1794 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1796 if (ctx_flags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
1798 ctx_attribs
[ctx_attrib_idx
- 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1799 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1800 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1807 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1808 struct wined3d_texture
*target
, const struct wined3d_format
*ds_format
)
1810 struct wined3d_device
*device
= swapchain
->device
;
1811 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
1812 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1813 const struct wined3d_format
*color_format
;
1814 struct wined3d_context
*ret
;
1815 BOOL hdc_is_private
= FALSE
;
1816 BOOL auxBuffers
= FALSE
;
1817 HGLRC ctx
, share_ctx
;
1824 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1826 wined3d_from_cs(device
->cs
);
1828 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1832 if (!(ret
->blit_targets
= wined3d_calloc(gl_info
->limits
.buffers
, sizeof(*ret
->blit_targets
))))
1835 if (!(ret
->draw_buffers
= wined3d_calloc(gl_info
->limits
.buffers
, sizeof(*ret
->draw_buffers
))))
1838 ret
->fbo_key
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1839 FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[gl_info
->limits
.buffers
+ 1]));
1843 ret
->free_timestamp_query_size
= 4;
1844 if (!(ret
->free_timestamp_queries
= wined3d_calloc(ret
->free_timestamp_query_size
,
1845 sizeof(*ret
->free_timestamp_queries
))))
1847 list_init(&ret
->timestamp_queries
);
1849 ret
->free_occlusion_query_size
= 4;
1850 if (!(ret
->free_occlusion_queries
= wined3d_calloc(ret
->free_occlusion_query_size
,
1851 sizeof(*ret
->free_occlusion_queries
))))
1853 list_init(&ret
->occlusion_queries
);
1855 ret
->free_event_query_size
= 4;
1856 if (!(ret
->free_event_queries
= wined3d_calloc(ret
->free_event_query_size
,
1857 sizeof(*ret
->free_event_queries
))))
1859 list_init(&ret
->event_queries
);
1861 list_init(&ret
->so_statistics_queries
);
1863 list_init(&ret
->pipeline_statistics_queries
);
1865 list_init(&ret
->fbo_list
);
1866 list_init(&ret
->fbo_destroy_list
);
1868 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1870 ERR("Failed to allocate shader backend context data.\n");
1873 if (!device
->adapter
->fragment_pipe
->allocate_context_data(ret
))
1875 ERR("Failed to allocate fragment pipeline context data.\n");
1879 for (i
= 0; i
< ARRAY_SIZE(ret
->tex_unit_map
); ++i
)
1880 ret
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
1881 for (i
= 0; i
< ARRAY_SIZE(ret
->rev_tex_unit_map
); ++i
)
1882 ret
->rev_tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
1883 if (gl_info
->limits
.graphics_samplers
>= MAX_COMBINED_SAMPLERS
)
1885 /* Initialize the texture unit mapping to a 1:1 mapping. */
1886 unsigned int base
, count
;
1888 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_PIXEL
, &base
, &count
);
1889 if (base
+ MAX_FRAGMENT_SAMPLERS
> ARRAY_SIZE(ret
->rev_tex_unit_map
))
1891 ERR("Unexpected texture unit base index %u.\n", base
);
1894 for (i
= 0; i
< min(count
, MAX_FRAGMENT_SAMPLERS
); ++i
)
1896 ret
->tex_unit_map
[i
] = base
+ i
;
1897 ret
->rev_tex_unit_map
[base
+ i
] = i
;
1900 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_VERTEX
, &base
, &count
);
1901 if (base
+ MAX_VERTEX_SAMPLERS
> ARRAY_SIZE(ret
->rev_tex_unit_map
))
1903 ERR("Unexpected texture unit base index %u.\n", base
);
1906 for (i
= 0; i
< min(count
, MAX_VERTEX_SAMPLERS
); ++i
)
1908 ret
->tex_unit_map
[MAX_FRAGMENT_SAMPLERS
+ i
] = base
+ i
;
1909 ret
->rev_tex_unit_map
[base
+ i
] = MAX_FRAGMENT_SAMPLERS
+ i
;
1913 if (!(ret
->texture_type
= wined3d_calloc(gl_info
->limits
.combined_samplers
,
1914 sizeof(*ret
->texture_type
))))
1917 if (!(hdc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1919 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1921 if ((hdc
= swapchain_get_backup_dc(swapchain
)))
1922 hdc_is_private
= TRUE
;
1925 ERR("Failed to retrieve a device context.\n");
1930 color_format
= target
->resource
.format
;
1931 target_usage
= target
->resource
.usage
;
1933 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1934 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1935 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1939 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1940 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
, target_usage
);
1941 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1942 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
, target_usage
);
1945 /* DirectDraw supports 8bit paletted render targets and these are used by
1946 * old games like StarCraft and C&C. Most modern hardware doesn't support
1947 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1948 * conversion (ab)uses the alpha component for storing the palette index.
1949 * For this reason we require a format with 8bit alpha, so request
1951 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1952 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
, target_usage
);
1954 /* When using FBOs for off-screen rendering, we only use the drawable for
1955 * presentation blits, and don't do any rendering to it. That means we
1956 * don't need depth or stencil buffers, and can mostly ignore the render
1957 * target format. This wouldn't necessarily be quite correct for 10bpc
1958 * display modes, but we don't currently support those.
1959 * Using the same format regardless of the color/depth/stencil targets
1960 * makes it much less likely that different wined3d instances will set
1961 * conflicting pixel formats. */
1962 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
)
1964 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
, target_usage
);
1965 ds_format
= wined3d_get_format(gl_info
, WINED3DFMT_UNKNOWN
, WINED3DUSAGE_DEPTHSTENCIL
);
1968 /* Try to find a pixel format which matches our requirements. */
1969 if (!(pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
)))
1972 ret
->gl_info
= gl_info
;
1976 if (!context_set_pixel_format(ret
, hdc
, hdc_is_private
, pixel_format
))
1978 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1979 context_release(ret
);
1983 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1984 if (gl_info
->p_wglCreateContextAttribsARB
)
1986 if (!(ctx
= context_create_wgl_attribs(gl_info
, hdc
, share_ctx
)))
1991 if (!(ctx
= wglCreateContext(hdc
)))
1993 ERR("Failed to create a WGL context.\n");
1994 context_release(ret
);
1998 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
2000 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
2001 context_release(ret
);
2002 if (!wglDeleteContext(ctx
))
2003 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
2008 if (!device_context_add(device
, ret
))
2010 ERR("Failed to add the newly created context to the context list\n");
2011 context_release(ret
);
2012 if (!wglDeleteContext(ctx
))
2013 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
2017 ret
->d3d_info
= d3d_info
;
2018 ret
->state_table
= device
->StateTable
;
2020 /* Mark all states dirty to force a proper initialization of the states on
2021 * the first use of the context. Compute states do not need initialization. */
2022 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
2024 if (ret
->state_table
[state
].representative
&& !STATE_IS_COMPUTE(state
))
2025 context_invalidate_state(ret
, state
);
2028 ret
->device
= device
;
2029 ret
->swapchain
= swapchain
;
2030 ret
->current_rt
.texture
= target
;
2031 ret
->current_rt
.sub_resource_idx
= 0;
2032 ret
->tid
= GetCurrentThreadId();
2034 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->resource
);
2035 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
2039 ret
->win_handle
= swapchain
->win_handle
;
2041 ret
->hdc_is_private
= hdc_is_private
;
2042 ret
->hdc_has_format
= TRUE
;
2043 ret
->pixel_format
= pixel_format
;
2046 /* Set up the context defaults */
2047 if (!context_set_current(ret
))
2049 ERR("Cannot activate context to set up defaults.\n");
2050 device_context_remove(device
, ret
);
2051 context_release(ret
);
2052 if (!wglDeleteContext(ctx
))
2053 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
2057 if (context_debug_output_enabled(gl_info
))
2059 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, ret
));
2060 if (TRACE_ON(d3d_synchronous
))
2061 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
2062 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
2065 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
2066 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2070 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
2071 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2072 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
2073 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2074 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
2075 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2077 if (WARN_ON(d3d_perf
))
2079 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
2080 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2084 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2085 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
2087 TRACE("Setting up the screen\n");
2089 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2091 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
2092 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2094 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
2095 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2097 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
2098 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2104 GL_EXTCALL(glGenVertexArrays(1, &vao
));
2105 GL_EXTCALL(glBindVertexArray(vao
));
2106 checkGLcall("creating VAO");
2109 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
2110 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2111 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
2112 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2114 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
2116 /* Direct3D always uses n-1 weights for n world matrices and uses
2117 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
2118 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
2119 * enabled as well. */
2120 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
2121 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
2123 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
2125 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2126 * the previous texture where to source the offset from is always unit - 1.
2128 for (i
= 1; i
< gl_info
->limits
.textures
; ++i
)
2130 context_active_texture(ret
, gl_info
, i
);
2131 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
2132 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ i
- 1);
2133 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2136 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
2138 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2139 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2140 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2141 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2144 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2145 * program and the dummy program is destroyed when the context is destroyed.
2147 static const char dummy_program
[] =
2149 "MOV result.color, fragment.color.primary;\n"
2151 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
2152 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
2153 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
2156 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2158 for (i
= 0; i
< gl_info
->limits
.textures
; ++i
)
2160 context_active_texture(ret
, gl_info
, i
);
2161 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
2162 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2166 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
2168 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
2170 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
2172 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
2174 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_NO_PRIMITIVE_RESTART
))
2176 if (gl_info
->supported
[ARB_ES3_COMPATIBILITY
])
2178 gl_info
->gl_ops
.gl
.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX
);
2179 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2183 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2186 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_CUBEMAP_FILTERING
)
2187 && gl_info
->supported
[ARB_SEAMLESS_CUBE_MAP
])
2189 gl_info
->gl_ops
.gl
.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
2190 checkGLcall("enable seamless cube map filtering");
2192 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
2193 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN
, GL_LOWER_LEFT
));
2194 device
->shader_backend
->shader_init_context_state(ret
);
2195 ret
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
2196 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
2197 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
2198 | (1u << WINED3D_SHADER_TYPE_HULL
)
2199 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
2200 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
2202 /* If this happens to be the first context for the device, dummy textures
2203 * are not created yet. In that case, they will be created (and bound) by
2204 * create_dummy_textures right after this context is initialized. */
2205 if (device
->dummy_textures
.tex_2d
)
2206 context_bind_dummy_textures(device
, ret
);
2208 TRACE("Created context %p.\n", ret
);
2213 if (hdc
) wined3d_release_dc(swapchain
->win_handle
, hdc
);
2214 device
->shader_backend
->shader_free_context_data(ret
);
2215 device
->adapter
->fragment_pipe
->free_context_data(ret
);
2216 HeapFree(GetProcessHeap(), 0, ret
->texture_type
);
2217 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
2218 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
2219 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
2220 HeapFree(GetProcessHeap(), 0, ret
->fbo_key
);
2221 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
2222 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
2223 HeapFree(GetProcessHeap(), 0, ret
);
2227 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
2231 TRACE("Destroying ctx %p\n", context
);
2233 wined3d_from_cs(device
->cs
);
2235 /* We delay destroying a context when it is active. The context_release()
2236 * function invokes context_destroy() again while leaving the last level. */
2239 TRACE("Delaying destruction of context %p.\n", context
);
2240 context
->destroy_delayed
= 1;
2241 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2242 context
->swapchain
= NULL
;
2246 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
2248 context_destroy_gl_resources(context
);
2249 TlsSetValue(wined3d_context_tls_idx
, NULL
);
2254 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2255 in wined3d_adapter may go away in the meantime */
2256 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
2257 *gl_info
= *context
->gl_info
;
2258 context
->gl_info
= gl_info
;
2259 context
->destroyed
= 1;
2263 device
->shader_backend
->shader_free_context_data(context
);
2264 device
->adapter
->fragment_pipe
->free_context_data(context
);
2265 HeapFree(GetProcessHeap(), 0, context
->texture_type
);
2266 HeapFree(GetProcessHeap(), 0, context
->fbo_key
);
2267 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
2268 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
2269 device_context_remove(device
, context
);
2270 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
2273 const DWORD
*context_get_tex_unit_mapping(const struct wined3d_context
*context
,
2274 const struct wined3d_shader_version
*shader_version
, unsigned int *base
, unsigned int *count
)
2276 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2278 if (!shader_version
)
2281 *count
= MAX_TEXTURES
;
2282 return context
->tex_unit_map
;
2285 if (shader_version
->major
>= 4)
2287 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, shader_version
->type
, base
, count
);
2291 switch (shader_version
->type
)
2293 case WINED3D_SHADER_TYPE_PIXEL
:
2295 *count
= MAX_FRAGMENT_SAMPLERS
;
2297 case WINED3D_SHADER_TYPE_VERTEX
:
2298 *base
= MAX_FRAGMENT_SAMPLERS
;
2299 *count
= MAX_VERTEX_SAMPLERS
;
2302 ERR("Unhandled shader type %#x.\n", shader_version
->type
);
2307 return context
->tex_unit_map
;
2310 /* Context activation is done by the caller. */
2311 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
2313 const GLdouble projection
[] =
2315 2.0 / width
, 0.0, 0.0, 0.0,
2316 0.0, 2.0 / height
, 0.0, 0.0,
2318 -1.0, -1.0, -1.0, 1.0,
2321 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
2322 checkGLcall("glMatrixMode(GL_PROJECTION)");
2323 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
2324 checkGLcall("glLoadMatrixd");
2325 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
2326 checkGLcall("glViewport");
2329 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
2331 const struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2338 GetClientRect(context
->win_handle
, &window_size
);
2339 size
->cx
= window_size
.right
- window_size
.left
;
2340 size
->cy
= window_size
.bottom
- window_size
.top
;
2345 level
= context
->current_rt
.sub_resource_idx
% rt
->level_count
;
2346 size
->cx
= wined3d_texture_get_level_width(rt
, level
);
2347 size
->cy
= wined3d_texture_get_level_height(rt
, level
);
2350 /*****************************************************************************
2353 * Sets up a context for DirectDraw blitting.
2354 * All texture units are disabled, texture unit 0 is set as current unit
2355 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
2356 * color writing enabled for all channels
2357 * register combiners disabled, shaders disabled
2358 * world matrix is set to identity, texture matrix 0 too
2359 * projection matrix is setup for drawing screen coordinates
2362 * This: Device to activate the context for
2363 * context: Context to setup
2365 *****************************************************************************/
2366 /* Context activation is done by the caller. */
2367 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
2370 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2374 TRACE("Setting up context %p for blitting\n", context
);
2376 context_get_rt_size(context
, &rt_size
);
2378 if (context
->last_was_blit
)
2380 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
2382 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2383 context
->blit_w
= rt_size
.cx
;
2384 context
->blit_h
= rt_size
.cy
;
2385 /* No need to dirtify here, the states are still dirtified because
2386 * they weren't applied since the last SetupForBlit() call. */
2388 TRACE("Context is already set up for blitting, nothing to do\n");
2391 context
->last_was_blit
= TRUE
;
2393 /* Disable all textures. The caller can then bind a texture it wants to blit
2396 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
2397 * function texture unit. No need to care for higher samplers
2399 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
2401 sampler
= context
->rev_tex_unit_map
[i
];
2402 context_active_texture(context
, gl_info
, i
);
2404 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2406 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2407 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2409 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2410 checkGLcall("glDisable GL_TEXTURE_3D");
2411 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2413 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2414 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2416 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2417 checkGLcall("glDisable GL_TEXTURE_2D");
2419 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2420 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
2422 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2424 if (sampler
< MAX_TEXTURES
)
2425 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2426 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2429 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
2430 GL_EXTCALL(glBindSampler(0, 0));
2431 context_active_texture(context
, gl_info
, 0);
2433 sampler
= context
->rev_tex_unit_map
[0];
2435 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2437 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2438 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2440 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2441 checkGLcall("glDisable GL_TEXTURE_3D");
2442 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2444 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2445 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2447 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2448 checkGLcall("glDisable GL_TEXTURE_2D");
2450 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2452 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
2453 checkGLcall("glMatrixMode(GL_TEXTURE)");
2454 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2455 checkGLcall("glLoadIdentity()");
2457 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
2459 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
2460 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
2461 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2464 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2466 if (sampler
< MAX_TEXTURES
)
2468 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
2469 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2471 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2474 /* Other misc states */
2475 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2476 checkGLcall("glDisable(GL_ALPHA_TEST)");
2477 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
2478 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
2479 checkGLcall("glDisable GL_LIGHTING");
2480 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
2481 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
2482 checkGLcall("glDisable GL_DEPTH_TEST");
2483 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
2484 glDisableWINE(GL_FOG
);
2485 checkGLcall("glDisable GL_FOG");
2486 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
2487 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2488 checkGLcall("glDisable GL_BLEND");
2489 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2490 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
2491 checkGLcall("glDisable GL_CULL_FACE");
2492 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
2493 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
2494 checkGLcall("glDisable GL_STENCIL_TEST");
2495 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
2496 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
2497 checkGLcall("glDisable GL_SCISSOR_TEST");
2498 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2499 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2501 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
2502 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2503 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
2505 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
2506 checkGLcall("glColorMask");
2507 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
2508 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
2509 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
2510 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2511 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2513 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2514 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2515 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2518 /* Setup transforms */
2519 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2520 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2521 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2522 checkGLcall("glLoadIdentity()");
2523 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2525 context
->last_was_rhw
= TRUE
;
2526 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2528 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2529 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2530 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2531 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2532 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2533 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2534 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2536 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
2537 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
2538 GL_EXTCALL(glClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
));
2540 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2542 /* Disable shaders */
2543 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2545 context
->blit_w
= rt_size
.cx
;
2546 context
->blit_h
= rt_size
.cy
;
2547 context_invalidate_state(context
, STATE_VIEWPORT
);
2548 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2551 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2553 return rt_mask
& (1u << 31);
2556 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2558 return rt_mask
& ~(1u << 31);
2561 /* Context activation is done by the caller. */
2562 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2564 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2568 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2569 checkGLcall("glDrawBuffer()");
2571 else if (is_rt_mask_onscreen(rt_mask
))
2573 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2574 checkGLcall("glDrawBuffer()");
2578 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2585 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2587 context
->draw_buffers
[i
] = GL_NONE
;
2593 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2595 GL_EXTCALL(glDrawBuffers(i
, context
->draw_buffers
));
2596 checkGLcall("glDrawBuffers()");
2600 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2601 checkGLcall("glDrawBuffer()");
2606 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2611 /* Context activation is done by the caller. */
2612 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2614 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2615 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2616 DWORD new_mask
= context_generate_rt_mask(buffer
);
2618 if (new_mask
== *current_mask
)
2621 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2622 checkGLcall("glDrawBuffer()");
2624 *current_mask
= new_mask
;
2627 /* Context activation is done by the caller. */
2628 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2630 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2631 checkGLcall("glActiveTexture");
2632 context
->active_texture
= unit
;
2635 void context_bind_bo(struct wined3d_context
*context
, GLenum binding
, GLuint name
)
2637 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2639 if (binding
== GL_ELEMENT_ARRAY_BUFFER
)
2640 context_invalidate_state(context
, STATE_INDEXBUFFER
);
2642 GL_EXTCALL(glBindBuffer(binding
, name
));
2645 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2647 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2648 DWORD unit
= context
->active_texture
;
2649 DWORD old_texture_type
= context
->texture_type
[unit
];
2653 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2654 checkGLcall("glBindTexture");
2661 if (old_texture_type
!= target
)
2663 const struct wined3d_device
*device
= context
->device
;
2665 switch (old_texture_type
)
2671 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, device
->dummy_textures
.tex_1d
);
2672 checkGLcall("glBindTexture");
2674 case GL_TEXTURE_1D_ARRAY
:
2675 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, device
->dummy_textures
.tex_1d_array
);
2676 checkGLcall("glBindTexture");
2679 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_textures
.tex_2d
);
2680 checkGLcall("glBindTexture");
2682 case GL_TEXTURE_2D_ARRAY
:
2683 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, device
->dummy_textures
.tex_2d_array
);
2684 checkGLcall("glBindTexture");
2686 case GL_TEXTURE_RECTANGLE_ARB
:
2687 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_textures
.tex_rect
);
2688 checkGLcall("glBindTexture");
2690 case GL_TEXTURE_CUBE_MAP
:
2691 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_textures
.tex_cube
);
2692 checkGLcall("glBindTexture");
2694 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2695 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, device
->dummy_textures
.tex_cube_array
);
2696 checkGLcall("glBindTexture");
2699 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_textures
.tex_3d
);
2700 checkGLcall("glBindTexture");
2702 case GL_TEXTURE_BUFFER
:
2703 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, device
->dummy_textures
.tex_buffer
);
2704 checkGLcall("glBindTexture");
2707 ERR("Unexpected texture target %#x.\n", old_texture_type
);
2710 context
->texture_type
[unit
] = target
;
2714 void *context_map_bo_address(struct wined3d_context
*context
,
2715 const struct wined3d_bo_address
*data
, size_t size
, GLenum binding
, DWORD flags
)
2717 const struct wined3d_gl_info
*gl_info
;
2720 if (!data
->buffer_object
)
2723 gl_info
= context
->gl_info
;
2725 context_bind_bo(context
, binding
, data
->buffer_object
);
2727 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
2729 GLbitfield map_flags
= wined3d_resource_gl_map_flags(flags
) & ~GL_MAP_FLUSH_EXPLICIT_BIT
;
2730 memory
= GL_EXTCALL(glMapBufferRange(binding
, (INT_PTR
)data
->addr
, size
, map_flags
));
2734 memory
= GL_EXTCALL(glMapBuffer(binding
, wined3d_resource_gl_legacy_map_flags(flags
)));
2735 memory
+= (INT_PTR
)data
->addr
;
2738 context_bind_bo(context
, binding
, 0);
2739 checkGLcall("Map buffer object");
2744 void context_unmap_bo_address(struct wined3d_context
*context
,
2745 const struct wined3d_bo_address
*data
, GLenum binding
)
2747 const struct wined3d_gl_info
*gl_info
;
2749 if (!data
->buffer_object
)
2752 gl_info
= context
->gl_info
;
2754 context_bind_bo(context
, binding
, data
->buffer_object
);
2755 GL_EXTCALL(glUnmapBuffer(binding
));
2756 context_bind_bo(context
, binding
, 0);
2757 checkGLcall("Unmap buffer object");
2760 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2762 if (context
->render_offscreen
== offscreen
)
2765 context_invalidate_state(context
, STATE_VIEWPORT
);
2766 context_invalidate_state(context
, STATE_SCISSORRECT
);
2767 if (!context
->gl_info
->supported
[ARB_CLIP_CONTROL
])
2769 context_invalidate_state(context
, STATE_FRONTFACE
);
2770 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2771 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2773 context_invalidate_state(context
, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN
));
2774 if (context
->gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
2775 context_invalidate_state(context
, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
));
2776 context
->render_offscreen
= offscreen
;
2779 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2780 const struct wined3d_format
*required
)
2782 if (existing
== required
)
2784 if ((existing
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
)
2785 != (required
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
))
2787 if (existing
->depth_size
< required
->depth_size
)
2789 /* If stencil bits are used the exact amount is required - otherwise
2790 * wrapping won't work correctly. */
2791 if (required
->stencil_size
&& required
->stencil_size
!= existing
->stencil_size
)
2796 /* Context activation is done by the caller. */
2797 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2798 const struct wined3d_rendertarget_view
*depth_stencil
)
2800 /* Onscreen surfaces are always in a swapchain */
2801 struct wined3d_swapchain
*swapchain
= context
->current_rt
.texture
->swapchain
;
2803 if (context
->render_offscreen
|| !depth_stencil
) return;
2804 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2806 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2807 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2809 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2811 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2812 if (!(wined3d_texture_load_location(context
->current_rt
.texture
, context
->current_rt
.sub_resource_idx
,
2813 context
, WINED3D_LOCATION_TEXTURE_RGB
)))
2814 ERR("Failed to load location.\n");
2815 swapchain
->render_to_fbo
= TRUE
;
2816 swapchain_update_draw_bindings(swapchain
);
2817 context_set_render_offscreen(context
, TRUE
);
2820 GLenum
context_get_offscreen_gl_buffer(const struct wined3d_context
*context
)
2822 switch (wined3d_settings
.offscreen_rendering_mode
)
2825 return GL_COLOR_ATTACHMENT0
;
2827 case ORM_BACKBUFFER
:
2828 return context
->aux_buffers
> 0 ? GL_AUX0
: GL_BACK
;
2831 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
2836 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_context
*context
, struct wined3d_texture
*rt
)
2838 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2840 else if (rt
->swapchain
)
2841 return context_generate_rt_mask_from_resource(&rt
->resource
);
2843 return context_generate_rt_mask(context_get_offscreen_gl_buffer(context
));
2846 /* Context activation is done by the caller. */
2847 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2849 struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2850 struct wined3d_surface
*surface
;
2851 DWORD rt_mask
, *cur_mask
;
2853 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2855 context_validate_onscreen_formats(context
, NULL
);
2857 if (context
->render_offscreen
)
2859 wined3d_texture_load(rt
, context
, FALSE
);
2861 surface
= rt
->sub_resources
[context
->current_rt
.sub_resource_idx
].u
.surface
;
2862 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, surface
, NULL
, rt
->resource
.draw_binding
);
2863 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2870 context
->current_fbo
= NULL
;
2871 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2872 rt_mask
= context_generate_rt_mask_from_resource(&rt
->resource
);
2877 rt_mask
= context_generate_rt_mask_no_fbo(context
, rt
);
2880 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2882 if (rt_mask
!= *cur_mask
)
2884 context_apply_draw_buffers(context
, rt_mask
);
2885 *cur_mask
= rt_mask
;
2888 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2890 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2893 SetupForBlit(device
, context
);
2894 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2897 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2898 const struct wined3d_rendertarget_view
*ds
)
2902 if (ds
) return TRUE
;
2904 for (i
= 0; i
< rt_count
; ++i
)
2906 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2910 WARN("Invalid render target config, need at least one attachment.\n");
2914 /* Context activation is done by the caller. */
2915 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_state
*state
,
2916 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2918 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2919 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
2920 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2921 DWORD rt_mask
= 0, *cur_mask
;
2924 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= state
->fb
2925 || rt_count
!= gl_info
->limits
.buffers
)
2927 if (!context_validate_rt_config(rt_count
, rts
, dsv
))
2930 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2932 context_validate_onscreen_formats(context
, dsv
);
2934 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2936 memset(context
->blit_targets
, 0, gl_info
->limits
.buffers
* sizeof(*context
->blit_targets
));
2937 for (i
= 0; i
< rt_count
; ++i
)
2941 context
->blit_targets
[i
].gl_view
= rts
[i
]->gl_view
;
2942 context
->blit_targets
[i
].resource
= rts
[i
]->resource
;
2943 context
->blit_targets
[i
].sub_resource_idx
= rts
[i
]->sub_resource_idx
;
2944 context
->blit_targets
[i
].layer_count
= rts
[i
]->layer_count
;
2946 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2947 rt_mask
|= (1u << i
);
2949 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2950 wined3d_rendertarget_view_get_surface(dsv
),
2951 rt_count
? rts
[0]->resource
->draw_binding
: 0,
2952 dsv
? dsv
->resource
->draw_binding
: 0);
2956 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2957 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2958 rt_mask
= context_generate_rt_mask_from_resource(rts
[0]->resource
);
2961 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2962 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2963 * state management allows this */
2964 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2968 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2969 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0])->container
: NULL
);
2972 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2973 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2975 for (i
= 0; i
< rt_count
; ++i
)
2977 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2978 rt_mask
|= (1u << i
);
2983 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2984 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0])->container
: NULL
);
2987 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2989 if (rt_mask
!= *cur_mask
)
2991 context_apply_draw_buffers(context
, rt_mask
);
2992 *cur_mask
= rt_mask
;
2993 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2996 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2998 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3001 context
->last_was_blit
= FALSE
;
3003 /* Blending and clearing should be orthogonal, but tests on the nvidia
3004 * driver show that disabling blending when clearing improves the clearing
3005 * performance incredibly. */
3006 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
3007 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
3008 if (rt_count
&& gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
3010 if (needs_srgb_write(context
, state
, fb
))
3011 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
3013 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
3014 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3016 checkGLcall("setting up state for clear");
3018 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3019 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
3020 context_invalidate_state(context
, STATE_SCISSORRECT
);
3025 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_state
*state
)
3027 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
3028 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
3029 DWORD rt_mask
, rt_mask_bits
;
3032 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
3033 return context_generate_rt_mask_no_fbo(context
, wined3d_rendertarget_view_get_surface(rts
[0])->container
);
3034 else if (!context
->render_offscreen
)
3035 return context_generate_rt_mask_from_resource(rts
[0]->resource
);
3037 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
3038 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
3039 rt_mask_bits
= rt_mask
;
3041 while (rt_mask_bits
)
3043 rt_mask_bits
&= ~(1u << i
);
3044 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
3045 rt_mask
&= ~(1u << i
);
3053 /* Context activation is done by the caller. */
3054 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3056 DWORD rt_mask
= find_draw_buffers_mask(context
, state
);
3057 const struct wined3d_fb_state
*fb
= state
->fb
;
3060 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3062 if (!context
->render_offscreen
)
3064 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
3065 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
3071 memset(context
->blit_targets
, 0, context
->gl_info
->limits
.buffers
* sizeof (*context
->blit_targets
));
3072 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
3074 if (fb
->render_targets
[i
])
3076 context
->blit_targets
[i
].gl_view
= fb
->render_targets
[i
]->gl_view
;
3077 context
->blit_targets
[i
].resource
= fb
->render_targets
[i
]->resource
;
3078 context
->blit_targets
[i
].sub_resource_idx
= fb
->render_targets
[i
]->sub_resource_idx
;
3079 context
->blit_targets
[i
].layer_count
= fb
->render_targets
[i
]->layer_count
;
3082 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
3083 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
3084 fb
->render_targets
[0] ? fb
->render_targets
[0]->resource
->draw_binding
: 0,
3085 fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
3089 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
3090 if (rt_mask
!= *cur_mask
)
3092 context_apply_draw_buffers(context
, rt_mask
);
3093 *cur_mask
= rt_mask
;
3095 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
3098 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
3100 DWORD i
= context
->rev_tex_unit_map
[unit
];
3101 DWORD j
= context
->tex_unit_map
[stage
];
3103 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
3104 context
->tex_unit_map
[stage
] = unit
;
3105 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
3106 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
3108 context
->rev_tex_unit_map
[unit
] = stage
;
3109 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
3110 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
3113 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
3117 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
3118 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
3121 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
3122 const struct wined3d_state
*state
)
3126 context
->fixed_function_usage_map
= 0;
3127 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
3129 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
3130 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
3131 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
3132 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
3133 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
3134 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
3135 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
3136 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
3138 /* Not used, and disable higher stages. */
3139 if (color_op
== WINED3D_TOP_DISABLE
)
3142 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
3143 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
3144 || ((color_arg3
== WINED3DTA_TEXTURE
)
3145 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
3146 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
3147 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
3148 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
3149 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
3150 context
->fixed_function_usage_map
|= (1u << i
);
3152 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
3153 && i
< MAX_TEXTURES
- 1)
3154 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
3157 if (i
< context
->lowest_disabled_stage
)
3160 end
= context
->lowest_disabled_stage
;
3164 start
= context
->lowest_disabled_stage
;
3168 context
->lowest_disabled_stage
= i
;
3169 for (i
= start
+ 1; i
< end
; ++i
)
3171 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3175 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
3176 const struct wined3d_state
*state
)
3178 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3179 unsigned int i
, tex
;
3182 ffu_map
= context
->fixed_function_usage_map
;
3184 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
3185 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
3187 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3192 if (context
->tex_unit_map
[i
] != i
)
3194 context_map_stage(context
, i
, i
);
3195 context_invalidate_state(context
, STATE_SAMPLER(i
));
3196 context_invalidate_texture_stage(context
, i
);
3202 /* Now work out the mapping */
3204 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3209 if (context
->tex_unit_map
[i
] != tex
)
3211 context_map_stage(context
, i
, tex
);
3212 context_invalidate_state(context
, STATE_SAMPLER(i
));
3213 context_invalidate_texture_stage(context
, i
);
3220 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3222 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3223 const struct wined3d_shader_resource_info
*resource_info
=
3224 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3227 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3229 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
3231 context_map_stage(context
, i
, i
);
3232 context_invalidate_state(context
, STATE_SAMPLER(i
));
3233 if (i
< d3d_info
->limits
.ffp_blend_stages
)
3234 context_invalidate_texture_stage(context
, i
);
3239 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
3240 const struct wined3d_shader_resource_info
*ps_resource_info
, DWORD unit
)
3242 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
3244 /* Not currently used */
3245 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
3248 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
3250 /* Used by a fragment sampler */
3252 if (!ps_resource_info
)
3254 /* No pixel shader, check fixed function */
3255 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1u << current_mapping
));
3258 /* Pixel shader, check the shader's sampler map */
3259 return !ps_resource_info
[current_mapping
].type
;
3265 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
3267 const struct wined3d_shader_resource_info
*vs_resource_info
=
3268 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
3269 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
3270 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3271 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.graphics_samplers
) - 1;
3274 /* Note that we only care if a resource is used or not, not the
3275 * resource's specific type. Otherwise we'd need to call
3276 * shader_update_samplers() here for 1.x pixelshaders. */
3278 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3280 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3282 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
3283 if (vs_resource_info
[i
].type
)
3287 if (context_unit_free_for_vs(context
, ps_resource_info
, start
))
3289 if (context
->tex_unit_map
[vsampler_idx
] != start
)
3291 context_map_stage(context
, vsampler_idx
, start
);
3292 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
3301 if (context
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
3302 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
3307 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3309 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3310 BOOL vs
= use_vs(state
);
3311 BOOL ps
= use_ps(state
);
3314 context_update_fixed_function_usage_map(context
, state
);
3316 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3317 * need a 1:1 map at the moment.
3318 * When the mapping of a stage is changed, sampler and ALL texture stage
3319 * states have to be reset. */
3321 if (gl_info
->limits
.graphics_samplers
>= MAX_COMBINED_SAMPLERS
)
3325 context_map_psamplers(context
, state
);
3327 context_map_fixed_function_samplers(context
, state
);
3330 context_map_vsamplers(context
, ps
, state
);
3333 /* Context activation is done by the caller. */
3334 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3336 DWORD rt_mask
, *cur_mask
;
3338 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
3340 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
3341 rt_mask
= find_draw_buffers_mask(context
, state
);
3342 if (rt_mask
!= *cur_mask
)
3344 context_apply_draw_buffers(context
, rt_mask
);
3345 *cur_mask
= rt_mask
;
3349 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
3351 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
3352 *regnum
= WINED3D_FFP_POSITION
;
3353 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
3354 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
3355 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
3356 *regnum
= WINED3D_FFP_BLENDINDICES
;
3357 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
3358 *regnum
= WINED3D_FFP_NORMAL
;
3359 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
3360 *regnum
= WINED3D_FFP_PSIZE
;
3361 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
3362 *regnum
= WINED3D_FFP_DIFFUSE
;
3363 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
3364 *regnum
= WINED3D_FFP_SPECULAR
;
3365 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
3366 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
3369 WARN("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
3377 /* Context activation is done by the caller. */
3378 void wined3d_stream_info_from_declaration(struct wined3d_stream_info
*stream_info
,
3379 const struct wined3d_state
*state
, const struct wined3d_gl_info
*gl_info
,
3380 const struct wined3d_d3d_info
*d3d_info
)
3382 /* We need to deal with frequency data! */
3383 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
3384 BOOL generic_attributes
= d3d_info
->ffp_generic_attributes
;
3385 BOOL use_vshader
= use_vs(state
);
3388 stream_info
->use_map
= 0;
3389 stream_info
->swizzle_map
= 0;
3390 stream_info
->position_transformed
= 0;
3395 stream_info
->position_transformed
= declaration
->position_transformed
;
3397 /* Translate the declaration into strided data. */
3398 for (i
= 0; i
< declaration
->element_count
; ++i
)
3400 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
3401 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
3405 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
3406 element
, i
+ 1, declaration
->element_count
);
3408 if (!stream
->buffer
)
3411 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
3415 if (element
->output_slot
== WINED3D_OUTPUT_SLOT_UNUSED
)
3417 stride_used
= FALSE
;
3419 else if (element
->output_slot
== WINED3D_OUTPUT_SLOT_SEMANTIC
)
3421 /* TODO: Assuming vertexdeclarations are usually used with the
3422 * same or a similar shader, it might be worth it to store the
3423 * last used output slot and try that one first. */
3424 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
3425 element
->usage
, element
->usage_idx
, &idx
);
3429 idx
= element
->output_slot
;
3435 if (!generic_attributes
&& !element
->ffp_valid
)
3437 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3438 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
3439 stride_used
= FALSE
;
3443 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
3449 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3450 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3451 use_vshader
? "shader": "fixed function", idx
,
3452 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
3453 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
),
3454 debug_d3dinput_classification(element
->input_slot_class
), element
->instance_data_step_rate
);
3456 stream_info
->elements
[idx
].format
= element
->format
;
3457 stream_info
->elements
[idx
].data
.buffer_object
= 0;
3458 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
3459 stream_info
->elements
[idx
].stride
= stream
->stride
;
3460 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
3461 if (stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
3463 stream_info
->elements
[idx
].divisor
= 1;
3465 else if (element
->input_slot_class
== WINED3D_INPUT_PER_INSTANCE_DATA
)
3467 stream_info
->elements
[idx
].divisor
= element
->instance_data_step_rate
;
3468 if (!element
->instance_data_step_rate
)
3469 FIXME("Instance step rate 0 not implemented.\n");
3473 stream_info
->elements
[idx
].divisor
= 0;
3476 if (!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3477 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
3479 stream_info
->swizzle_map
|= 1u << idx
;
3481 stream_info
->use_map
|= 1u << idx
;
3486 /* Context activation is done by the caller. */
3487 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3489 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
3490 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3491 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3492 DWORD prev_all_vbo
= stream_info
->all_vbo
;
3496 wined3d_stream_info_from_declaration(stream_info
, state
, gl_info
, d3d_info
);
3498 stream_info
->all_vbo
= 1;
3499 context
->num_buffer_queries
= 0;
3500 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
3502 struct wined3d_stream_info_element
*element
;
3503 struct wined3d_bo_address data
;
3504 struct wined3d_buffer
*buffer
;
3509 element
= &stream_info
->elements
[i
];
3510 buffer
= state
->streams
[element
->stream_idx
].buffer
;
3512 /* We can't use VBOs if the base vertex index is negative. OpenGL
3513 * doesn't accept negative offsets (or rather offsets bigger than the
3514 * VBO, because the pointer is unsigned), so use system memory
3515 * sources. In most sane cases the pointer - offset will still be > 0,
3516 * otherwise it will wrap around to some big value. Hope that with the
3517 * indices the driver wraps it back internally. If not,
3518 * draw_primitive_immediate_mode() is needed, including a vertex buffer
3520 if (state
->load_base_vertex_index
< 0)
3522 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3523 state
->load_base_vertex_index
);
3524 element
->data
.buffer_object
= 0;
3525 element
->data
.addr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(buffer
, context
);
3526 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
3527 FIXME("System memory vertex data load offset is negative!\n");
3531 wined3d_buffer_load(buffer
, context
, state
);
3532 wined3d_buffer_get_memory(buffer
, &data
, buffer
->locations
);
3533 element
->data
.buffer_object
= data
.buffer_object
;
3534 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
3537 if (!element
->data
.buffer_object
)
3538 stream_info
->all_vbo
= 0;
3541 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
3543 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
3546 if (prev_all_vbo
!= stream_info
->all_vbo
)
3547 context_invalidate_state(context
, STATE_INDEXBUFFER
);
3549 context
->use_immediate_mode_draw
= FALSE
;
3551 if (stream_info
->all_vbo
)
3556 if (state
->vertex_declaration
->half_float_conv_needed
)
3558 TRACE("Using immediate mode draw with vertex shaders for FLOAT16 conversion.\n");
3559 context
->use_immediate_mode_draw
= TRUE
;
3564 WORD slow_mask
= -!d3d_info
->ffp_generic_attributes
& (1u << WINED3D_FFP_PSIZE
);
3565 slow_mask
|= -(!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
] && !d3d_info
->ffp_generic_attributes
)
3566 & ((1u << WINED3D_FFP_DIFFUSE
) | (1u << WINED3D_FFP_SPECULAR
) | (1u << WINED3D_FFP_BLENDWEIGHT
));
3568 if ((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
3569 || (stream_info
->use_map
& slow_mask
))
3570 context
->use_immediate_mode_draw
= TRUE
;
3574 /* Context activation is done by the caller. */
3575 static void context_preload_texture(struct wined3d_context
*context
,
3576 const struct wined3d_state
*state
, unsigned int idx
)
3578 struct wined3d_texture
*texture
;
3580 if (!(texture
= state
->textures
[idx
]))
3583 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
3586 /* Context activation is done by the caller. */
3587 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3593 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3595 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
3596 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
3602 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3604 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
3605 context_preload_texture(context
, state
, i
);
3610 WORD ffu_map
= context
->fixed_function_usage_map
;
3612 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3615 context_preload_texture(context
, state
, i
);
3620 static void context_load_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
,
3621 unsigned int shader_mask
)
3623 struct wined3d_shader_sampler_map_entry
*entry
;
3624 struct wined3d_shader_resource_view
*view
;
3625 struct wined3d_shader
*shader
;
3628 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3630 if (!(shader_mask
& (1u << i
)))
3633 if (!(shader
= state
->shader
[i
]))
3636 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3638 if (state
->cb
[i
][j
])
3639 wined3d_buffer_load(state
->cb
[i
][j
], context
, state
);
3642 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
3644 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3646 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
3649 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3650 wined3d_buffer_load(buffer_from_resource(view
->resource
), context
, state
);
3652 wined3d_texture_load(texture_from_resource(view
->resource
), context
, FALSE
);
3657 static void context_bind_shader_resources(struct wined3d_context
*context
,
3658 const struct wined3d_state
*state
, enum wined3d_shader_type shader_type
)
3660 unsigned int bind_idx
, shader_sampler_count
, base
, count
, i
;
3661 const struct wined3d_device
*device
= context
->device
;
3662 struct wined3d_shader_sampler_map_entry
*entry
;
3663 struct wined3d_shader_resource_view
*view
;
3664 const struct wined3d_shader
*shader
;
3665 struct wined3d_sampler
*sampler
;
3666 const DWORD
*tex_unit_map
;
3668 if (!(shader
= state
->shader
[shader_type
]))
3671 tex_unit_map
= context_get_tex_unit_mapping(context
,
3672 &shader
->reg_maps
.shader_version
, &base
, &count
);
3674 shader_sampler_count
= shader
->reg_maps
.sampler_map
.count
;
3675 if (shader_sampler_count
> count
)
3676 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3677 shader
, shader_sampler_count
, count
);
3678 count
= min(shader_sampler_count
, count
);
3680 for (i
= 0; i
< count
; ++i
)
3682 entry
= &shader
->reg_maps
.sampler_map
.entries
[i
];
3683 bind_idx
= base
+ entry
->bind_idx
;
3685 bind_idx
= tex_unit_map
[bind_idx
];
3687 if (!(view
= state
->shader_resource_view
[shader_type
][entry
->resource_idx
]))
3689 WARN("No resource view bound at index %u, %u.\n", shader_type
, entry
->resource_idx
);
3693 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
3694 sampler
= device
->default_sampler
;
3695 else if (!(sampler
= state
->sampler
[shader_type
][entry
->sampler_idx
]))
3696 sampler
= device
->null_sampler
;
3697 wined3d_shader_resource_view_bind(view
, bind_idx
, sampler
, context
);
3701 static void context_load_unordered_access_resources(struct wined3d_context
*context
,
3702 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
3704 struct wined3d_unordered_access_view
*view
;
3705 struct wined3d_texture
*texture
;
3706 struct wined3d_buffer
*buffer
;
3709 context
->uses_uavs
= 0;
3714 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
3716 if (!(view
= views
[i
]))
3719 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3721 buffer
= buffer_from_resource(view
->resource
);
3722 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
3723 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_BUFFER
);
3727 texture
= texture_from_resource(view
->resource
);
3728 wined3d_texture_load(texture
, context
, FALSE
);
3729 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_TEXTURE_RGB
);
3732 context
->uses_uavs
= 1;
3736 static void context_bind_unordered_access_views(struct wined3d_context
*context
,
3737 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
3739 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3740 struct wined3d_unordered_access_view
*view
;
3741 GLuint texture_name
;
3748 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
3750 if (!(view
= views
[i
]))
3752 if (shader
->reg_maps
.uav_resource_info
[i
].type
)
3753 WARN("No unordered access view bound at index %u.\n", i
);
3754 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
3758 if (view
->gl_view
.name
)
3760 texture_name
= view
->gl_view
.name
;
3763 else if (view
->resource
->type
!= WINED3D_RTYPE_BUFFER
)
3765 struct wined3d_texture
*texture
= texture_from_resource(view
->resource
);
3766 texture_name
= wined3d_texture_get_texture_name(texture
, context
, FALSE
);
3767 level
= view
->desc
.u
.texture
.level_idx
;
3771 FIXME("Unsupported buffer unordered access view.\n");
3772 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
3776 GL_EXTCALL(glBindImageTexture(i
, texture_name
, level
, GL_TRUE
, 0, GL_READ_WRITE
,
3777 view
->format
->glInternal
));
3779 if (view
->counter_bo
)
3780 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER
, i
, view
->counter_bo
));
3782 checkGLcall("Bind unordered access views");
3785 static void context_load_stream_output_buffers(struct wined3d_context
*context
,
3786 const struct wined3d_state
*state
)
3790 for (i
= 0; i
< ARRAY_SIZE(state
->stream_output
); ++i
)
3792 struct wined3d_buffer
*buffer
;
3793 if (!(buffer
= state
->stream_output
[i
].buffer
))
3796 wined3d_buffer_load(buffer
, context
, state
);
3797 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
3801 /* Context activation is done by the caller. */
3802 BOOL
context_apply_draw_state(struct wined3d_context
*context
,
3803 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
3805 const struct StateEntry
*state_table
= context
->state_table
;
3806 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3807 const struct wined3d_fb_state
*fb
= state
->fb
;
3811 if (!context_validate_rt_config(gl_info
->limits
.buffers
, fb
->render_targets
, fb
->depth_stencil
))
3814 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3816 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3819 /* Preload resources before FBO setup. Texture preload in particular may
3820 * result in changes to the current FBO, due to using e.g. FBO blits for
3821 * updating a resource location. */
3822 context_update_tex_unit_map(context
, state
);
3823 context_preload_textures(context
, state
);
3824 context_load_shader_resources(context
, state
, ~(1u << WINED3D_SHADER_TYPE_COMPUTE
));
3825 context_load_unordered_access_resources(context
, state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
3826 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
3827 context_load_stream_output_buffers(context
, state
);
3828 /* TODO: Right now the dependency on the vertex shader is necessary
3829 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3830 * the current VS but maybe it's possible to relax the coupling in some
3831 * situations at least. */
3832 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
3833 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
3835 context_update_stream_info(context
, state
);
3839 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3842 wined3d_buffer_load(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
,
3845 /* Loading the buffers above may have invalidated the stream info. */
3846 if (isStateDirty(context
, STATE_STREAMSRC
))
3847 context_update_stream_info(context
, state
);
3849 if (state
->index_buffer
)
3851 if (context
->stream_info
.all_vbo
)
3852 wined3d_buffer_load(state
->index_buffer
, context
, state
);
3854 wined3d_buffer_load_sysmem(state
->index_buffer
, context
);
3857 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3859 DWORD rep
= context
->dirtyArray
[i
];
3860 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3861 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3862 context
->isStateDirty
[idx
] &= ~(1u << shift
);
3863 state_table
[rep
].apply(context
, state
, rep
);
3866 if (context
->shader_update_mask
& ~(1u << WINED3D_SHADER_TYPE_COMPUTE
))
3868 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3869 context
->shader_update_mask
&= 1u << WINED3D_SHADER_TYPE_COMPUTE
;
3872 if (context
->constant_update_mask
)
3874 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3875 context
->constant_update_mask
= 0;
3878 if (context
->update_shader_resource_bindings
)
3880 for (i
= 0; i
< WINED3D_SHADER_TYPE_GRAPHICS_COUNT
; ++i
)
3881 context_bind_shader_resources(context
, state
, i
);
3882 context
->update_shader_resource_bindings
= 0;
3883 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
3884 context
->update_compute_shader_resource_bindings
= 1;
3887 if (context
->update_unordered_access_view_bindings
)
3889 context_bind_unordered_access_views(context
,
3890 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
3891 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
3892 context
->update_unordered_access_view_bindings
= 0;
3893 context
->update_compute_unordered_access_view_bindings
= 1;
3896 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3898 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3901 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3902 context
->last_was_blit
= FALSE
;
3907 void context_apply_compute_state(struct wined3d_context
*context
,
3908 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
3910 const struct StateEntry
*state_table
= context
->state_table
;
3911 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3912 unsigned int state_id
, i
, j
;
3914 context_load_shader_resources(context
, state
, 1u << WINED3D_SHADER_TYPE_COMPUTE
);
3915 context_load_unordered_access_resources(context
, state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
3916 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
3918 for (i
= 0, state_id
= STATE_COMPUTE_OFFSET
; i
< ARRAY_SIZE(context
->dirty_compute_states
); ++i
)
3920 for (j
= 0; j
< sizeof(*context
->dirty_compute_states
) * CHAR_BIT
; ++j
, ++state_id
)
3922 if (context
->dirty_compute_states
[i
] & (1u << j
))
3923 state_table
[state_id
].apply(context
, state
, state_id
);
3926 memset(context
->dirty_compute_states
, 0, sizeof(*context
->dirty_compute_states
));
3928 if (context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_COMPUTE
))
3930 device
->shader_backend
->shader_select_compute(device
->shader_priv
, context
, state
);
3931 context
->shader_update_mask
&= ~(1u << WINED3D_SHADER_TYPE_COMPUTE
);
3934 if (context
->update_compute_shader_resource_bindings
)
3936 context_bind_shader_resources(context
, state
, WINED3D_SHADER_TYPE_COMPUTE
);
3937 context
->update_compute_shader_resource_bindings
= 0;
3938 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
3939 context
->update_shader_resource_bindings
= 1;
3942 if (context
->update_compute_unordered_access_view_bindings
)
3944 context_bind_unordered_access_views(context
,
3945 state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
3946 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
3947 context
->update_compute_unordered_access_view_bindings
= 0;
3948 context
->update_unordered_access_view_bindings
= 1;
3951 context
->last_was_blit
= FALSE
;
3954 void context_end_transform_feedback(struct wined3d_context
*context
)
3956 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3957 if (context
->transform_feedback_active
)
3959 GL_EXTCALL(glEndTransformFeedback());
3960 checkGLcall("glEndTransformFeedback");
3961 context
->transform_feedback_active
= 0;
3962 context
->transform_feedback_paused
= 0;
3966 static void context_setup_target(struct wined3d_context
*context
,
3967 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
3969 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3971 render_offscreen
= wined3d_resource_is_offscreen(&texture
->resource
);
3972 if (context
->current_rt
.texture
== texture
3973 && context
->current_rt
.sub_resource_idx
== sub_resource_idx
3974 && render_offscreen
== old_render_offscreen
)
3977 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3978 * the alpha blend state changes with different render target formats. */
3979 if (!context
->current_rt
.texture
)
3981 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3985 const struct wined3d_format
*old
= context
->current_rt
.texture
->resource
.format
;
3986 const struct wined3d_format
*new = texture
->resource
.format
;
3988 if (old
->id
!= new->id
)
3990 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3991 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3992 || !(texture
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3993 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3995 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3996 if ((context
->current_rt
.texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
3997 != (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3998 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
4001 /* When switching away from an offscreen render target, and we're not
4002 * using FBOs, we have to read the drawable into the texture. This is
4003 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4004 * There are some things that need care though. PreLoad needs a GL context,
4005 * and FindContext is called before the context is activated. It also
4006 * has to be called with the old rendertarget active, otherwise a
4007 * wrong drawable is read. */
4008 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
4009 && old_render_offscreen
&& (context
->current_rt
.texture
!= texture
4010 || context
->current_rt
.sub_resource_idx
!= sub_resource_idx
))
4012 unsigned int prev_sub_resource_idx
= context
->current_rt
.sub_resource_idx
;
4013 struct wined3d_texture
*prev_texture
= context
->current_rt
.texture
;
4015 /* Read the back buffer of the old drawable into the destination texture. */
4016 if (prev_texture
->texture_srgb
.name
)
4017 wined3d_texture_load(prev_texture
, context
, TRUE
);
4018 wined3d_texture_load(prev_texture
, context
, FALSE
);
4019 wined3d_texture_invalidate_location(prev_texture
, prev_sub_resource_idx
, WINED3D_LOCATION_DRAWABLE
);
4023 context
->current_rt
.texture
= texture
;
4024 context
->current_rt
.sub_resource_idx
= sub_resource_idx
;
4025 context_set_render_offscreen(context
, render_offscreen
);
4028 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
,
4029 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4031 struct wined3d_context
*current_context
= context_get_current();
4032 struct wined3d_context
*context
;
4033 BOOL swapchain_texture
;
4035 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device
, texture
, sub_resource_idx
);
4037 wined3d_from_cs(device
->cs
);
4039 if (current_context
&& current_context
->destroyed
)
4040 current_context
= NULL
;
4042 swapchain_texture
= texture
&& texture
->swapchain
;
4046 && current_context
->current_rt
.texture
4047 && current_context
->device
== device
)
4049 texture
= current_context
->current_rt
.texture
;
4050 sub_resource_idx
= current_context
->current_rt
.sub_resource_idx
;
4054 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
4056 if (swapchain
->back_buffers
)
4057 texture
= swapchain
->back_buffers
[0];
4059 texture
= swapchain
->front_buffer
;
4060 sub_resource_idx
= 0;
4064 if (current_context
&& current_context
->current_rt
.texture
== texture
)
4066 context
= current_context
;
4068 else if (swapchain_texture
)
4070 TRACE("Rendering onscreen.\n");
4072 context
= swapchain_get_context(texture
->swapchain
);
4076 TRACE("Rendering offscreen.\n");
4078 /* Stay with the current context if possible. Otherwise use the
4079 * context for the primary swapchain. */
4080 if (current_context
&& current_context
->device
== device
)
4081 context
= current_context
;
4083 context
= swapchain_get_context(device
->swapchains
[0]);
4086 context_enter(context
);
4087 context_update_window(context
);
4088 context_setup_target(context
, texture
, sub_resource_idx
);
4089 if (!context
->valid
)
4092 if (context
!= current_context
)
4094 if (!context_set_current(context
))
4095 ERR("Failed to activate the new context.\n");
4097 else if (context
->needs_set
)
4099 context_set_gl_context(context
);
4105 struct wined3d_context
*context_reacquire(const struct wined3d_device
*device
,
4106 struct wined3d_context
*context
)
4108 struct wined3d_context
*current_context
;
4110 if (!context
|| context
->tid
!= GetCurrentThreadId())
4113 current_context
= context_acquire(device
, context
->current_rt
.texture
,
4114 context
->current_rt
.sub_resource_idx
);
4115 if (current_context
!= context
)
4116 ERR("Acquired context %p instead of %p.\n", current_context
, context
);
4117 return current_context
;