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
23 #include "wine/port.h"
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous
);
36 #define WINED3D_MAX_FBO_ENTRIES 64
37 #define WINED3D_ALL_LAYERS (~0u)
39 static DWORD wined3d_context_tls_idx
;
41 /* FBO helper functions */
43 /* Context activation is done by the caller. */
44 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint fbo
)
46 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
50 case GL_READ_FRAMEBUFFER
:
51 if (context
->fbo_read_binding
== fbo
) return;
52 context
->fbo_read_binding
= fbo
;
55 case GL_DRAW_FRAMEBUFFER
:
56 if (context
->fbo_draw_binding
== fbo
) return;
57 context
->fbo_draw_binding
= fbo
;
61 if (context
->fbo_read_binding
== fbo
62 && context
->fbo_draw_binding
== fbo
) return;
63 context
->fbo_read_binding
= fbo
;
64 context
->fbo_draw_binding
= fbo
;
68 FIXME("Unhandled target %#x.\n", target
);
72 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
73 checkGLcall("glBindFramebuffer()");
76 /* Context activation is done by the caller. */
77 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
81 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
83 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
84 checkGLcall("glFramebufferTexture2D()");
86 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
87 checkGLcall("glFramebufferTexture2D()");
89 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
90 checkGLcall("glFramebufferTexture2D()");
93 /* Context activation is done by the caller. */
94 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint fbo
)
96 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
98 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
99 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
100 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
102 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
103 checkGLcall("glDeleteFramebuffers()");
106 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
107 GLenum fbo_target
, DWORD flags
, GLuint rb
)
109 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
111 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
112 checkGLcall("glFramebufferRenderbuffer()");
115 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
117 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
118 checkGLcall("glFramebufferRenderbuffer()");
122 static void context_attach_gl_texture_fbo(struct wined3d_context
*context
,
123 GLenum fbo_target
, GLenum attachment
, const struct wined3d_fbo_resource
*resource
)
125 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
129 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
, GL_TEXTURE_2D
, 0, 0);
131 else if (resource
->layer
== WINED3D_ALL_LAYERS
)
133 if (!gl_info
->fbo_ops
.glFramebufferTexture
)
135 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
139 gl_info
->fbo_ops
.glFramebufferTexture(fbo_target
, attachment
,
140 resource
->object
, resource
->level
);
142 else if (resource
->target
== GL_TEXTURE_2D_ARRAY
|| resource
->target
== GL_TEXTURE_3D
)
144 if (!gl_info
->fbo_ops
.glFramebufferTextureLayer
)
146 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
150 gl_info
->fbo_ops
.glFramebufferTextureLayer(fbo_target
, attachment
,
151 resource
->object
, resource
->level
, resource
->layer
);
155 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
,
156 resource
->target
, resource
->object
, resource
->level
);
158 checkGLcall("attach texture to fbo");
161 /* Context activation is done by the caller. */
162 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
163 GLenum fbo_target
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
,
166 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
168 if (resource
->object
)
170 TRACE("Attach depth stencil %u.\n", resource
->object
);
174 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
175 flags
, resource
->object
);
179 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
180 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, resource
);
182 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
183 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, resource
);
186 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
))
187 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
189 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
))
190 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
194 TRACE("Attach depth stencil 0.\n");
196 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
197 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
201 /* Context activation is done by the caller. */
202 static void context_attach_surface_fbo(struct wined3d_context
*context
,
203 GLenum fbo_target
, DWORD idx
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
)
205 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
207 TRACE("Attach GL object %u to %u.\n", resource
->object
, idx
);
209 if (resource
->object
)
214 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
215 GL_RENDERBUFFER
, resource
->object
);
216 checkGLcall("glFramebufferRenderbuffer()");
220 context_attach_gl_texture_fbo(context
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, resource
);
225 context_attach_gl_texture_fbo(context
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, NULL
);
229 static void context_dump_fbo_attachment(const struct wined3d_gl_info
*gl_info
, GLenum target
,
237 enum wined3d_gl_extension extension
;
241 {GL_TEXTURE_2D
, GL_TEXTURE_BINDING_2D
, "2d", WINED3D_GL_EXT_NONE
},
242 {GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_BINDING_RECTANGLE_ARB
, "rectangle", ARB_TEXTURE_RECTANGLE
},
243 {GL_TEXTURE_2D_ARRAY
, GL_TEXTURE_BINDING_2D_ARRAY
, "2d-array", EXT_TEXTURE_ARRAY
},
246 GLint type
, name
, samples
, width
, height
, old_texture
, level
, face
, fmt
, tex_target
;
248 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
249 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
, &name
);
250 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
251 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
, &type
);
253 if (type
== GL_RENDERBUFFER
)
255 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, name
);
256 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_WIDTH
, &width
);
257 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_HEIGHT
, &height
);
258 if (gl_info
->limits
.samples
> 1)
259 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_SAMPLES
, &samples
);
262 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_INTERNAL_FORMAT
, &fmt
);
263 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
264 debug_fboattachment(attachment
), name
, width
, height
, samples
, fmt
);
266 else if (type
== GL_TEXTURE
)
268 const char *tex_type_str
;
270 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
271 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
, &level
);
272 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
273 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
, &face
);
277 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP
, &old_texture
);
279 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, name
);
280 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
281 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_WIDTH
, &width
);
282 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_HEIGHT
, &height
);
284 tex_target
= GL_TEXTURE_CUBE_MAP
;
285 tex_type_str
= "cube";
292 for (i
= 0; i
< sizeof(texture_type
) / sizeof(*texture_type
); ++i
)
294 if (!gl_info
->supported
[texture_type
[i
].extension
])
297 gl_info
->gl_ops
.gl
.p_glGetIntegerv(texture_type
[i
].binding
, &old_texture
);
298 while (gl_info
->gl_ops
.gl
.p_glGetError());
300 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, name
);
301 if (!gl_info
->gl_ops
.gl
.p_glGetError())
303 tex_target
= texture_type
[i
].target
;
304 tex_type_str
= texture_type
[i
].str
;
307 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, old_texture
);
311 FIXME("Cannot find type of texture %d.\n", name
);
315 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
316 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_WIDTH
, &width
);
317 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_HEIGHT
, &height
);
320 FIXME(" %s: %s texture %d, %dx%d, format %#x.\n", debug_fboattachment(attachment
),
321 tex_type_str
, name
, width
, height
, fmt
);
323 gl_info
->gl_ops
.gl
.p_glBindTexture(tex_target
, old_texture
);
324 checkGLcall("Guess texture type");
326 else if (type
== GL_NONE
)
328 FIXME(" %s: NONE.\n", debug_fboattachment(attachment
));
332 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment
), type
);
336 /* Context activation is done by the caller. */
337 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
339 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
342 if (!FIXME_ON(d3d
)) return;
344 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
345 if (status
== GL_FRAMEBUFFER_COMPLETE
)
347 TRACE("FBO complete\n");
353 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
355 if (!context
->current_fbo
)
357 ERR("FBO 0 is incomplete, driver bug?\n");
361 context_dump_fbo_attachment(gl_info
, target
, GL_DEPTH_ATTACHMENT
);
362 context_dump_fbo_attachment(gl_info
, target
, GL_STENCIL_ATTACHMENT
);
364 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
365 context_dump_fbo_attachment(gl_info
, target
, GL_COLOR_ATTACHMENT0
+ i
);
366 checkGLcall("Dump FBO attachments");
370 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
372 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
373 return buffer
? (1u << 31) | buffer
: 0;
376 static inline DWORD
context_generate_rt_mask_from_resource(struct wined3d_resource
*resource
)
378 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
380 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
384 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource
));
387 static inline void context_set_fbo_key_for_render_target(const struct wined3d_context
*context
,
388 struct wined3d_fbo_entry_key
*key
, unsigned int idx
, struct wined3d_rendertarget_info
*render_target
,
391 unsigned int sub_resource_idx
= render_target
->sub_resource_idx
;
392 struct wined3d_resource
*resource
= render_target
->resource
;
393 struct wined3d_texture
*texture
;
395 if (!resource
|| resource
->format
->id
== WINED3DFMT_NULL
|| resource
->type
== WINED3D_RTYPE_BUFFER
)
397 if (resource
&& resource
->type
== WINED3D_RTYPE_BUFFER
)
398 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
399 key
->objects
[idx
].object
= 0;
400 key
->objects
[idx
].target
= 0;
401 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
405 if (render_target
->gl_view
.name
)
407 key
->objects
[idx
].object
= render_target
->gl_view
.name
;
408 key
->objects
[idx
].target
= render_target
->gl_view
.target
;
409 key
->objects
[idx
].level
= 0;
410 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
414 texture
= wined3d_texture_from_resource(resource
);
415 if (resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
417 struct wined3d_surface
*surface
= texture
->sub_resources
[sub_resource_idx
].u
.surface
;
419 if (surface
->current_renderbuffer
)
421 key
->objects
[idx
].object
= surface
->current_renderbuffer
->id
;
422 key
->objects
[idx
].target
= 0;
423 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
424 key
->rb_namespace
|= 1 << idx
;
428 key
->objects
[idx
].target
= surface
->texture_target
;
429 key
->objects
[idx
].level
= surface
->texture_level
;
430 key
->objects
[idx
].layer
= surface
->texture_layer
;
434 key
->objects
[idx
].target
= texture
->target
;
435 key
->objects
[idx
].level
= sub_resource_idx
% texture
->level_count
;
436 key
->objects
[idx
].layer
= sub_resource_idx
/ texture
->level_count
;
438 if (render_target
->layer_count
!= 1)
439 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
443 case WINED3D_LOCATION_TEXTURE_RGB
:
444 key
->objects
[idx
].object
= wined3d_texture_get_texture_name(texture
, context
, FALSE
);
447 case WINED3D_LOCATION_TEXTURE_SRGB
:
448 key
->objects
[idx
].object
= wined3d_texture_get_texture_name(texture
, context
, TRUE
);
451 case WINED3D_LOCATION_RB_MULTISAMPLE
:
452 key
->objects
[idx
].object
= texture
->rb_multisample
;
453 key
->objects
[idx
].target
= 0;
454 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
455 key
->rb_namespace
|= 1 << idx
;
458 case WINED3D_LOCATION_RB_RESOLVED
:
459 key
->objects
[idx
].object
= texture
->rb_resolved
;
460 key
->objects
[idx
].target
= 0;
461 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
462 key
->rb_namespace
|= 1 << idx
;
467 static void context_generate_fbo_key(const struct wined3d_context
*context
,
468 struct wined3d_fbo_entry_key
*key
, struct wined3d_rendertarget_info
*render_targets
,
469 struct wined3d_surface
*depth_stencil_surface
, DWORD color_location
,
472 struct wined3d_rendertarget_info depth_stencil
= {{0}};
475 key
->rb_namespace
= 0;
476 if (depth_stencil_surface
)
478 depth_stencil
.resource
= &depth_stencil_surface
->container
->resource
;
479 depth_stencil
.sub_resource_idx
= surface_get_sub_resource_idx(depth_stencil_surface
);
480 depth_stencil
.layer_count
= 1;
482 context_set_fbo_key_for_render_target(context
, key
, 0, &depth_stencil
, ds_location
);
484 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
485 context_set_fbo_key_for_render_target(context
, key
, i
+ 1, &render_targets
[i
], color_location
);
488 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
489 struct wined3d_rendertarget_info
*render_targets
, struct wined3d_surface
*depth_stencil
,
490 DWORD color_location
, DWORD ds_location
)
492 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
493 unsigned int object_count
= gl_info
->limits
.buffers
+ 1;
494 struct fbo_entry
*entry
;
496 entry
= HeapAlloc(GetProcessHeap(), 0,
497 FIELD_OFFSET(struct fbo_entry
, key
.objects
[object_count
]));
498 memset(&entry
->key
, 0, FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[object_count
]));
499 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
503 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
)
504 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
505 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_STENCIL
)
506 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
508 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
509 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
510 checkGLcall("glGenFramebuffers()");
511 TRACE("Created FBO %u.\n", entry
->id
);
516 /* Context activation is done by the caller. */
517 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
518 struct wined3d_rendertarget_info
*render_targets
, struct wined3d_surface
*depth_stencil
,
519 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
521 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
523 context_bind_fbo(context
, target
, entry
->id
);
524 context_clean_fbo_attachments(gl_info
, target
);
526 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
530 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
)
531 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
532 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_STENCIL
)
533 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
537 /* Context activation is done by the caller. */
538 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
542 TRACE("Destroy FBO %u.\n", entry
->id
);
543 context_destroy_fbo(context
, entry
->id
);
545 --context
->fbo_entry_count
;
546 list_remove(&entry
->entry
);
547 HeapFree(GetProcessHeap(), 0, entry
);
550 /* Context activation is done by the caller. */
551 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
552 struct wined3d_rendertarget_info
*render_targets
, struct wined3d_surface
*depth_stencil
,
553 DWORD color_location
, DWORD ds_location
)
555 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
556 unsigned int object_count
= gl_info
->limits
.buffers
+ 1;
557 struct wined3d_texture
*rt_texture
, *ds_texture
;
558 struct fbo_entry
*entry
;
559 unsigned int i
, level
;
561 if (depth_stencil
&& render_targets
[0].resource
&& render_targets
[0].resource
->type
!= WINED3D_RTYPE_BUFFER
)
563 rt_texture
= wined3d_texture_from_resource(render_targets
[0].resource
);
564 level
= render_targets
[0].sub_resource_idx
% rt_texture
->level_count
;
565 ds_texture
= depth_stencil
->container
;
567 if (wined3d_texture_get_level_width(ds_texture
, depth_stencil
->texture_level
)
568 < wined3d_texture_get_level_width(rt_texture
, level
)
569 || wined3d_texture_get_level_height(ds_texture
, depth_stencil
->texture_level
)
570 < wined3d_texture_get_level_height(rt_texture
, level
))
572 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
573 depth_stencil
= NULL
;
575 else if (ds_texture
->resource
.multisample_type
!= rt_texture
->resource
.multisample_type
576 || ds_texture
->resource
.multisample_quality
!= rt_texture
->resource
.multisample_quality
)
578 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
579 rt_texture
->resource
.multisample_type
, rt_texture
->resource
.multisample_quality
,
580 ds_texture
->resource
.multisample_type
, ds_texture
->resource
.multisample_quality
);
581 depth_stencil
= NULL
;
584 surface_set_compatible_renderbuffer(depth_stencil
, &render_targets
[0]);
587 context_generate_fbo_key(context
, context
->fbo_key
, render_targets
, depth_stencil
, color_location
,
592 TRACE("Dumping FBO attachments:\n");
593 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
595 struct wined3d_resource
*resource
;
596 if ((resource
= render_targets
[i
].resource
))
598 unsigned int width
, height
;
599 const char *resource_type
;
601 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
603 width
= resource
->size
;
605 resource_type
= "buffer";
609 rt_texture
= wined3d_texture_from_resource(resource
);
610 level
= render_targets
[i
].sub_resource_idx
% rt_texture
->level_count
;
611 width
= wined3d_texture_get_level_pow2_width(rt_texture
, level
);
612 height
= wined3d_texture_get_level_pow2_height(rt_texture
, level
);
613 resource_type
= "texture";
616 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
617 i
, resource
, render_targets
[i
].sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
618 context
->fbo_key
->rb_namespace
& (1 << (i
+ 1)) ? "renderbuffer" : resource_type
,
619 context
->fbo_key
->objects
[i
+ 1].object
, width
, height
, resource
->multisample_type
);
624 ds_texture
= depth_stencil
->container
;
625 TRACE(" Depth attachment: %p format %s, %s %u, %ux%u, %u samples.\n",
626 depth_stencil
, debug_d3dformat(ds_texture
->resource
.format
->id
),
627 context
->fbo_key
->rb_namespace
& (1 << 0) ? "renderbuffer" : "texture",
628 context
->fbo_key
->objects
[0].object
,
629 wined3d_texture_get_level_pow2_width(ds_texture
, depth_stencil
->texture_level
),
630 wined3d_texture_get_level_pow2_height(ds_texture
, depth_stencil
->texture_level
),
631 ds_texture
->resource
.multisample_type
);
635 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
637 if (memcmp(context
->fbo_key
, &entry
->key
, FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[object_count
])))
640 list_remove(&entry
->entry
);
641 list_add_head(&context
->fbo_list
, &entry
->entry
);
645 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
647 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, color_location
, ds_location
);
648 list_add_head(&context
->fbo_list
, &entry
->entry
);
649 ++context
->fbo_entry_count
;
653 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
654 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, color_location
, ds_location
, entry
);
655 list_remove(&entry
->entry
);
656 list_add_head(&context
->fbo_list
, &entry
->entry
);
662 /* Context activation is done by the caller. */
663 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
665 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
667 GLuint read_binding
, draw_binding
;
669 if (entry
->flags
& WINED3D_FBO_ENTRY_FLAG_ATTACHED
)
671 context_bind_fbo(context
, target
, entry
->id
);
675 read_binding
= context
->fbo_read_binding
;
676 draw_binding
= context
->fbo_draw_binding
;
677 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
679 /* Apply render targets */
680 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
682 context_attach_surface_fbo(context
, target
, i
, &entry
->key
.objects
[i
+ 1],
683 entry
->key
.rb_namespace
& (1 << (i
+ 1)));
686 context_attach_depth_stencil_fbo(context
, target
, &entry
->key
.objects
[0],
687 entry
->key
.rb_namespace
& 0x1, entry
->flags
);
689 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
690 * GL contexts requirements. */
691 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
692 context_set_draw_buffer(context
, GL_NONE
);
693 if (target
!= GL_FRAMEBUFFER
)
695 if (target
== GL_READ_FRAMEBUFFER
)
696 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
698 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
701 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_ATTACHED
;
704 /* Context activation is done by the caller. */
705 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
706 struct wined3d_rendertarget_info
*render_targets
, struct wined3d_surface
*depth_stencil
,
707 DWORD color_location
, DWORD ds_location
)
709 struct fbo_entry
*entry
, *entry2
;
711 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
713 context_destroy_fbo_entry(context
, entry
);
716 if (context
->rebind_fbo
)
718 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
719 context
->rebind_fbo
= FALSE
;
722 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
724 context
->current_fbo
= NULL
;
725 context_bind_fbo(context
, target
, 0);
729 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
,
730 color_location
, ds_location
);
731 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
735 /* Context activation is done by the caller. */
736 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
737 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
739 memset(context
->blit_targets
, 0, context
->gl_info
->limits
.buffers
* sizeof(*context
->blit_targets
));
742 context
->blit_targets
[0].resource
= &render_target
->container
->resource
;
743 context
->blit_targets
[0].sub_resource_idx
= surface_get_sub_resource_idx(render_target
);
744 context
->blit_targets
[0].layer_count
= 1;
746 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
, location
);
749 /* Context activation is done by the caller. */
750 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
752 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
754 if (context
->free_occlusion_query_count
)
756 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
760 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
762 GL_EXTCALL(glGenQueries(1, &query
->id
));
763 checkGLcall("glGenQueries");
765 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
769 WARN("Occlusion queries not supported, not allocating query id.\n");
774 query
->context
= context
;
775 list_add_head(&context
->occlusion_queries
, &query
->entry
);
778 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
780 struct wined3d_context
*context
= query
->context
;
782 list_remove(&query
->entry
);
783 query
->context
= NULL
;
785 if (!wined3d_array_reserve((void **)&context
->free_occlusion_queries
,
786 &context
->free_occlusion_query_size
, context
->free_occlusion_query_count
+ 1,
787 sizeof(*context
->free_occlusion_queries
)))
789 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
793 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
796 /* Context activation is done by the caller. */
797 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
799 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
801 if (context
->free_event_query_count
)
803 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
807 if (gl_info
->supported
[ARB_SYNC
])
809 /* Using ARB_sync, not much to do here. */
810 query
->object
.sync
= NULL
;
811 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
813 else if (gl_info
->supported
[APPLE_FENCE
])
815 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
816 checkGLcall("glGenFencesAPPLE");
818 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
820 else if(gl_info
->supported
[NV_FENCE
])
822 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
823 checkGLcall("glGenFencesNV");
825 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
829 WARN("Event queries not supported, not allocating query id.\n");
830 query
->object
.id
= 0;
834 query
->context
= context
;
835 list_add_head(&context
->event_queries
, &query
->entry
);
838 void context_free_event_query(struct wined3d_event_query
*query
)
840 struct wined3d_context
*context
= query
->context
;
842 list_remove(&query
->entry
);
843 query
->context
= NULL
;
845 if (!wined3d_array_reserve((void **)&context
->free_event_queries
,
846 &context
->free_event_query_size
, context
->free_event_query_count
+ 1,
847 sizeof(*context
->free_event_queries
)))
849 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
853 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
856 /* Context activation is done by the caller. */
857 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
859 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
861 if (context
->free_timestamp_query_count
)
863 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
867 GL_EXTCALL(glGenQueries(1, &query
->id
));
868 checkGLcall("glGenQueries");
870 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
873 query
->context
= context
;
874 list_add_head(&context
->timestamp_queries
, &query
->entry
);
877 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
879 struct wined3d_context
*context
= query
->context
;
881 list_remove(&query
->entry
);
882 query
->context
= NULL
;
884 if (!wined3d_array_reserve((void **)&context
->free_timestamp_queries
,
885 &context
->free_timestamp_query_size
, context
->free_timestamp_query_count
+ 1,
886 sizeof(*context
->free_timestamp_queries
)))
888 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
892 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
895 void context_alloc_so_statistics_query(struct wined3d_context
*context
,
896 struct wined3d_so_statistics_query
*query
)
898 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
900 if (context
->free_so_statistics_query_count
)
902 query
->u
= context
->free_so_statistics_queries
[--context
->free_so_statistics_query_count
];
906 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
907 checkGLcall("glGenQueries");
909 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
910 query
->u
.id
[0], query
->u
.id
[1], context
);
913 query
->context
= context
;
914 list_add_head(&context
->so_statistics_queries
, &query
->entry
);
917 void context_free_so_statistics_query(struct wined3d_so_statistics_query
*query
)
919 struct wined3d_context
*context
= query
->context
;
921 list_remove(&query
->entry
);
922 query
->context
= NULL
;
924 if (!wined3d_array_reserve((void **)&context
->free_so_statistics_queries
,
925 &context
->free_so_statistics_query_size
, context
->free_so_statistics_query_count
+ 1,
926 sizeof(*context
->free_so_statistics_queries
)))
928 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
929 query
->u
.id
[0], query
->u
.id
[1], context
);
933 context
->free_so_statistics_queries
[context
->free_so_statistics_query_count
++] = query
->u
;
936 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
938 static void context_enum_fbo_entries(const struct wined3d_device
*device
,
939 GLuint name
, BOOL rb_namespace
, context_fbo_entry_func_t
*callback
)
943 for (i
= 0; i
< device
->context_count
; ++i
)
945 struct wined3d_context
*context
= device
->contexts
[i
];
946 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
947 struct fbo_entry
*entry
, *entry2
;
949 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
953 for (j
= 0; j
< gl_info
->limits
.buffers
+ 1; ++j
)
955 if (entry
->key
.objects
[j
].object
== name
956 && !(entry
->key
.rb_namespace
& (1 << j
)) == !rb_namespace
)
958 callback(context
, entry
);
966 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
968 list_remove(&entry
->entry
);
969 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
972 void context_resource_released(const struct wined3d_device
*device
,
973 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
975 struct wined3d_texture
*texture
;
978 if (!device
->d3d_initialized
)
983 case WINED3D_RTYPE_TEXTURE_2D
:
984 case WINED3D_RTYPE_TEXTURE_3D
:
985 texture
= texture_from_resource(resource
);
987 for (i
= 0; i
< device
->context_count
; ++i
)
989 struct wined3d_context
*context
= device
->contexts
[i
];
990 if (context
->current_rt
.texture
== texture
)
992 context
->current_rt
.texture
= NULL
;
993 context
->current_rt
.sub_resource_idx
= 0;
1003 void context_gl_resource_released(struct wined3d_device
*device
,
1004 GLuint name
, BOOL rb_namespace
)
1006 context_enum_fbo_entries(device
, name
, rb_namespace
, context_queue_fbo_entry_destruction
);
1009 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
1011 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1012 struct fbo_entry
*entry
= context
->current_fbo
;
1015 if (!entry
|| context
->rebind_fbo
) return;
1017 for (i
= 0; i
< gl_info
->limits
.buffers
+ 1; ++i
)
1019 if (surface
->container
->texture_rgb
.name
== entry
->key
.objects
[i
].object
1020 || surface
->container
->texture_srgb
.name
== entry
->key
.objects
[i
].object
)
1022 TRACE("Updated surface %p is bound as attachment %u to the current FBO.\n", surface
, i
);
1023 context
->rebind_fbo
= TRUE
;
1029 static BOOL
context_restore_pixel_format(struct wined3d_context
*ctx
)
1031 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
1034 if (ctx
->restore_pf
&& IsWindow(ctx
->restore_pf_win
))
1036 if (ctx
->gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1038 HDC dc
= GetDCEx(ctx
->restore_pf_win
, 0, DCX_USESTYLE
| DCX_CACHE
);
1041 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, ctx
->restore_pf
))))
1043 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
1044 ctx
->restore_pf
, ctx
->restore_pf_win
);
1046 ReleaseDC(ctx
->restore_pf_win
, dc
);
1051 ERR("can't restore pixel format %d on window %p\n", ctx
->restore_pf
, ctx
->restore_pf_win
);
1055 ctx
->restore_pf
= 0;
1056 ctx
->restore_pf_win
= NULL
;
1060 static BOOL
context_set_pixel_format(struct wined3d_context
*context
, HDC dc
, BOOL
private, int format
)
1062 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1065 if (dc
== context
->hdc
&& context
->hdc_is_private
&& context
->hdc_has_format
)
1068 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
1069 if (current
== format
) goto success
;
1073 if (!SetPixelFormat(dc
, format
, NULL
))
1075 /* This may also happen if the dc belongs to a destroyed window. */
1076 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1077 format
, dc
, GetLastError());
1081 context
->restore_pf
= 0;
1082 context
->restore_pf_win
= private ? NULL
: WindowFromDC(dc
);
1086 /* By default WGL doesn't allow pixel format adjustments but we need it
1087 * here. For this reason there's a Wine specific wglSetPixelFormat()
1088 * which allows us to set the pixel format multiple times. Only use it
1089 * when really needed. */
1090 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1094 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
1096 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1101 win
= private ? NULL
: WindowFromDC(dc
);
1102 if (win
!= context
->restore_pf_win
)
1104 context_restore_pixel_format(context
);
1106 context
->restore_pf
= private ? 0 : current
;
1107 context
->restore_pf_win
= win
;
1113 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1114 * continue using the old format. There's a big chance that the old
1115 * format works although with a performance hit and perhaps rendering
1117 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1118 format
, dc
, current
);
1122 if (dc
== context
->hdc
&& context
->hdc_is_private
)
1123 context
->hdc_has_format
= TRUE
;
1127 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
1129 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
1130 BOOL backup
= FALSE
;
1132 if (!context_set_pixel_format(ctx
, ctx
->hdc
, ctx
->hdc_is_private
, ctx
->pixel_format
))
1134 WARN("Failed to set pixel format %d on device context %p.\n",
1135 ctx
->pixel_format
, ctx
->hdc
);
1139 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
1143 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1144 ctx
->glCtx
, ctx
->hdc
, GetLastError());
1146 WARN("Trying fallback to the backup window.\n");
1148 /* FIXME: If the context is destroyed it's no longer associated with
1149 * a swapchain, so we can't use the swapchain to get a backup dc. To
1150 * make this work windowless contexts would need to be handled by the
1152 if (ctx
->destroyed
|| !swapchain
)
1154 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
1155 context_set_current(NULL
);
1159 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
1161 context_set_current(NULL
);
1165 if (!context_set_pixel_format(ctx
, dc
, TRUE
, ctx
->pixel_format
))
1167 ERR("Failed to set pixel format %d on device context %p.\n",
1168 ctx
->pixel_format
, dc
);
1169 context_set_current(NULL
);
1173 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
1175 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1176 dc
, GetLastError());
1177 context_set_current(NULL
);
1187 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
1189 if (!wglMakeCurrent(dc
, gl_ctx
))
1191 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1192 gl_ctx
, dc
, GetLastError());
1193 context_set_current(NULL
);
1197 static void context_update_window(struct wined3d_context
*context
)
1199 if (!context
->swapchain
)
1202 if (context
->win_handle
== context
->swapchain
->win_handle
)
1205 TRACE("Updating context %p window from %p to %p.\n",
1206 context
, context
->win_handle
, context
->swapchain
->win_handle
);
1209 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1211 context
->win_handle
= context
->swapchain
->win_handle
;
1212 context
->hdc_is_private
= FALSE
;
1213 context
->hdc_has_format
= FALSE
;
1214 context
->needs_set
= 1;
1217 if (!(context
->hdc
= GetDCEx(context
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1219 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
1224 static void context_destroy_gl_resources(struct wined3d_context
*context
)
1226 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1227 struct wined3d_so_statistics_query
*so_statistics_query
;
1228 struct wined3d_timestamp_query
*timestamp_query
;
1229 struct wined3d_occlusion_query
*occlusion_query
;
1230 struct wined3d_event_query
*event_query
;
1231 struct fbo_entry
*entry
, *entry2
;
1236 restore_ctx
= wglGetCurrentContext();
1237 restore_dc
= wglGetCurrentDC();
1239 if (restore_ctx
== context
->glCtx
)
1241 else if (context
->valid
)
1242 context_set_gl_context(context
);
1244 LIST_FOR_EACH_ENTRY(so_statistics_query
, &context
->so_statistics_queries
,
1245 struct wined3d_so_statistics_query
, entry
)
1248 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query
->u
.id
), so_statistics_query
->u
.id
));
1249 so_statistics_query
->context
= NULL
;
1252 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
1255 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
1256 timestamp_query
->context
= NULL
;
1259 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1261 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1262 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1263 occlusion_query
->context
= NULL
;
1266 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
1270 if (gl_info
->supported
[ARB_SYNC
])
1272 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
1274 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
1275 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
1277 event_query
->context
= NULL
;
1280 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
1282 if (!context
->valid
) entry
->id
= 0;
1283 context_destroy_fbo_entry(context
, entry
);
1286 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1288 if (!context
->valid
) entry
->id
= 0;
1289 context_destroy_fbo_entry(context
, entry
);
1294 if (context
->dummy_arbfp_prog
)
1296 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
1299 if (gl_info
->supported
[WINED3D_GL_PRIMITIVE_QUERY
])
1301 for (i
= 0; i
< context
->free_so_statistics_query_count
; ++i
)
1303 union wined3d_gl_so_statistics_query
*q
= &context
->free_so_statistics_queries
[i
];
1304 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1308 if (gl_info
->supported
[ARB_TIMER_QUERY
])
1309 GL_EXTCALL(glDeleteQueries(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
1311 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1312 GL_EXTCALL(glDeleteQueries(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
1314 if (gl_info
->supported
[ARB_SYNC
])
1316 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1318 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
1321 else if (gl_info
->supported
[APPLE_FENCE
])
1323 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1325 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1328 else if (gl_info
->supported
[NV_FENCE
])
1330 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1332 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1336 checkGLcall("context cleanup");
1339 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1340 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1341 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1343 context_restore_pixel_format(context
);
1346 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1348 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1350 ERR("Failed to disable GL context.\n");
1353 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1355 if (!wglDeleteContext(context
->glCtx
))
1357 DWORD err
= GetLastError();
1358 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1362 DWORD
context_get_tls_idx(void)
1364 return wined3d_context_tls_idx
;
1367 void context_set_tls_idx(DWORD idx
)
1369 wined3d_context_tls_idx
= idx
;
1372 struct wined3d_context
*context_get_current(void)
1374 return TlsGetValue(wined3d_context_tls_idx
);
1377 BOOL
context_set_current(struct wined3d_context
*ctx
)
1379 struct wined3d_context
*old
= context_get_current();
1383 TRACE("Already using D3D context %p.\n", ctx
);
1391 TRACE("Switching away from destroyed context %p.\n", old
);
1392 context_destroy_gl_resources(old
);
1393 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1394 HeapFree(GetProcessHeap(), 0, old
);
1398 if (wglGetCurrentContext())
1400 const struct wined3d_gl_info
*gl_info
= old
->gl_info
;
1401 TRACE("Flushing context %p before switching to %p.\n", old
, ctx
);
1402 gl_info
->gl_ops
.gl
.p_glFlush();
1412 ERR("Trying to make invalid context %p current\n", ctx
);
1416 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1417 if (!context_set_gl_context(ctx
))
1421 else if (wglGetCurrentContext())
1423 TRACE("Clearing current D3D context.\n");
1424 if (!wglMakeCurrent(NULL
, NULL
))
1426 DWORD err
= GetLastError();
1427 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1428 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1433 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1436 void context_release(struct wined3d_context
*context
)
1438 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1442 if (!context
->level
)
1443 WARN("Context %p is not active.\n", context
);
1444 else if (context
!= context_get_current())
1445 WARN("Context %p is not the current context.\n", context
);
1448 if (!--context
->level
)
1450 if (context_restore_pixel_format(context
))
1451 context
->needs_set
= 1;
1452 if (context
->restore_ctx
)
1454 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1455 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
);
1456 context
->restore_ctx
= NULL
;
1457 context
->restore_dc
= NULL
;
1460 if (context
->destroy_delayed
)
1462 TRACE("Destroying context %p.\n", context
);
1463 context_destroy(context
->device
, context
);
1468 /* This is used when a context for render target A is active, but a separate context is
1469 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1470 * A to avoid breaking caller code. */
1471 void context_restore(struct wined3d_context
*context
, struct wined3d_surface
*restore
)
1473 if (context
->current_rt
.texture
!= restore
->container
1474 || context
->current_rt
.sub_resource_idx
!= surface_get_sub_resource_idx(restore
))
1476 context_release(context
);
1477 context
= context_acquire(restore
->container
->resource
.device
,
1478 restore
->container
, surface_get_sub_resource_idx(restore
));
1481 context_release(context
);
1484 static void context_enter(struct wined3d_context
*context
)
1486 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1488 if (!context
->level
++)
1490 const struct wined3d_context
*current_context
= context_get_current();
1491 HGLRC current_gl
= wglGetCurrentContext();
1493 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1495 TRACE("Another GL context (%p on device context %p) is already current.\n",
1496 current_gl
, wglGetCurrentDC());
1497 context
->restore_ctx
= current_gl
;
1498 context
->restore_dc
= wglGetCurrentDC();
1499 context
->needs_set
= 1;
1501 else if (!context
->needs_set
&& !(context
->hdc_is_private
&& context
->hdc_has_format
)
1502 && context
->pixel_format
!= context
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context
->hdc
))
1503 context
->needs_set
= 1;
1507 void context_invalidate_compute_state(struct wined3d_context
*context
, DWORD state_id
)
1509 DWORD representative
= context
->state_table
[state_id
].representative
- STATE_COMPUTE_OFFSET
;
1510 unsigned int index
, shift
;
1512 index
= representative
/ (sizeof(*context
->dirty_compute_states
) * CHAR_BIT
);
1513 shift
= representative
& (sizeof(*context
->dirty_compute_states
) * CHAR_BIT
- 1);
1514 context
->dirty_compute_states
[index
] |= (1u << shift
);
1517 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1519 DWORD rep
= context
->state_table
[state
].representative
;
1523 if (isStateDirty(context
, rep
)) return;
1525 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1526 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1527 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1528 context
->isStateDirty
[idx
] |= (1u << shift
);
1531 /* This function takes care of wined3d pixel format selection. */
1532 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1533 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1536 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1537 unsigned int current_value
;
1538 PIXELFORMATDESCRIPTOR pfd
;
1539 int iPixelFormat
= 0;
1542 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1543 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1547 for (i
= 0; i
< cfg_count
; ++i
)
1549 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1552 /* For now only accept RGBA formats. Perhaps some day we will
1553 * allow floating point formats for pbuffers. */
1554 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1556 /* In window mode we need a window drawable format and double buffering. */
1557 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1559 if (cfg
->redSize
< color_format
->red_size
)
1561 if (cfg
->greenSize
< color_format
->green_size
)
1563 if (cfg
->blueSize
< color_format
->blue_size
)
1565 if (cfg
->alphaSize
< color_format
->alpha_size
)
1567 if (cfg
->depthSize
< ds_format
->depth_size
)
1569 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1571 /* Check multisampling support. */
1572 if (cfg
->numSamples
)
1576 /* We try to locate a format which matches our requirements exactly. In case of
1577 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1578 if (cfg
->depthSize
== ds_format
->depth_size
)
1580 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1582 if (cfg
->alphaSize
== color_format
->alpha_size
)
1584 /* We like to have aux buffers in backbuffer mode */
1585 if (auxBuffers
&& cfg
->auxBuffers
)
1587 if (cfg
->redSize
== color_format
->red_size
1588 && cfg
->greenSize
== color_format
->green_size
1589 && cfg
->blueSize
== color_format
->blue_size
)
1592 if (value
> current_value
)
1594 iPixelFormat
= cfg
->iPixelFormat
;
1595 current_value
= value
;
1601 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1603 memset(&pfd
, 0, sizeof(pfd
));
1604 pfd
.nSize
= sizeof(pfd
);
1606 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1607 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1608 pfd
.cAlphaBits
= color_format
->alpha_size
;
1609 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1610 + color_format
->blue_size
+ color_format
->alpha_size
;
1611 pfd
.cDepthBits
= ds_format
->depth_size
;
1612 pfd
.cStencilBits
= ds_format
->stencil_size
;
1613 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1615 if (!(iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
)))
1617 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1618 ERR("Can't find a suitable pixel format.\n");
1623 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1624 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1625 return iPixelFormat
;
1628 /* Context activation is done by the caller. */
1629 void context_bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1631 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1634 for (i
= 0; i
< gl_info
->limits
.combined_samplers
; ++i
)
1636 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1637 checkGLcall("glActiveTexture");
1639 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_textures
.tex_2d
);
1641 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1642 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_textures
.tex_rect
);
1644 if (gl_info
->supported
[EXT_TEXTURE3D
])
1645 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_textures
.tex_3d
);
1647 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1648 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_textures
.tex_cube
);
1650 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
1651 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, device
->dummy_textures
.tex_cube_array
);
1653 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
1654 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, device
->dummy_textures
.tex_2d_array
);
1656 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
1657 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, device
->dummy_textures
.tex_buffer
);
1659 checkGLcall("Bind dummy textures");
1663 void wined3d_check_gl_call(const struct wined3d_gl_info
*gl_info
,
1664 const char *file
, unsigned int line
, const char *name
)
1668 if (gl_info
->supported
[ARB_DEBUG_OUTPUT
] || (err
= gl_info
->gl_ops
.gl
.p_glGetError()) == GL_NO_ERROR
)
1670 TRACE("%s call ok %s / %u.\n", name
, file
, line
);
1676 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1677 debug_glerror(err
), err
, name
, file
,line
);
1678 err
= gl_info
->gl_ops
.gl
.p_glGetError();
1679 } while (err
!= GL_NO_ERROR
);
1682 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1684 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1685 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1688 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1689 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1693 case GL_DEBUG_TYPE_ERROR_ARB
:
1694 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1697 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1698 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1699 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1700 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1703 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1704 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1708 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1713 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1716 unsigned int ctx_attrib_idx
= 0;
1717 GLint ctx_attribs
[7], ctx_flags
= 0;
1719 if (context_debug_output_enabled(gl_info
))
1720 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1721 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1722 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1723 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1724 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1725 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1726 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1729 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1730 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1732 ctx_attribs
[ctx_attrib_idx
] = 0;
1734 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1736 if (ctx_flags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
1738 ctx_attribs
[ctx_attrib_idx
- 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1739 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1740 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1747 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1748 struct wined3d_texture
*target
, const struct wined3d_format
*ds_format
)
1750 struct wined3d_device
*device
= swapchain
->device
;
1751 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
1752 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1753 const struct wined3d_format
*color_format
;
1754 struct wined3d_context
*ret
;
1755 BOOL hdc_is_private
= FALSE
;
1756 BOOL auxBuffers
= FALSE
;
1757 HGLRC ctx
, share_ctx
;
1764 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1766 wined3d_from_cs(device
->cs
);
1768 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1772 if (!(ret
->blit_targets
= wined3d_calloc(gl_info
->limits
.buffers
, sizeof(*ret
->blit_targets
))))
1775 if (!(ret
->draw_buffers
= wined3d_calloc(gl_info
->limits
.buffers
, sizeof(*ret
->draw_buffers
))))
1778 ret
->fbo_key
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1779 FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[gl_info
->limits
.buffers
+ 1]));
1783 ret
->free_timestamp_query_size
= 4;
1784 if (!(ret
->free_timestamp_queries
= wined3d_calloc(ret
->free_timestamp_query_size
,
1785 sizeof(*ret
->free_timestamp_queries
))))
1787 list_init(&ret
->timestamp_queries
);
1789 ret
->free_occlusion_query_size
= 4;
1790 if (!(ret
->free_occlusion_queries
= wined3d_calloc(ret
->free_occlusion_query_size
,
1791 sizeof(*ret
->free_occlusion_queries
))))
1793 list_init(&ret
->occlusion_queries
);
1795 ret
->free_event_query_size
= 4;
1796 if (!(ret
->free_event_queries
= wined3d_calloc(ret
->free_event_query_size
,
1797 sizeof(*ret
->free_event_queries
))))
1799 list_init(&ret
->event_queries
);
1801 list_init(&ret
->so_statistics_queries
);
1803 list_init(&ret
->fbo_list
);
1804 list_init(&ret
->fbo_destroy_list
);
1806 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1808 ERR("Failed to allocate shader backend context data.\n");
1811 if (!device
->adapter
->fragment_pipe
->allocate_context_data(ret
))
1813 ERR("Failed to allocate fragment pipeline context data.\n");
1817 for (i
= 0; i
< ARRAY_SIZE(ret
->tex_unit_map
); ++i
)
1818 ret
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
1819 for (i
= 0; i
< ARRAY_SIZE(ret
->rev_tex_unit_map
); ++i
)
1820 ret
->rev_tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
1821 if (gl_info
->limits
.graphics_samplers
>= MAX_COMBINED_SAMPLERS
)
1823 /* Initialize the texture unit mapping to a 1:1 mapping. */
1824 unsigned int base
, count
;
1826 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_PIXEL
, &base
, &count
);
1827 if (base
+ MAX_FRAGMENT_SAMPLERS
> ARRAY_SIZE(ret
->rev_tex_unit_map
))
1829 ERR("Unexpected texture unit base index %u.\n", base
);
1832 for (i
= 0; i
< min(count
, MAX_FRAGMENT_SAMPLERS
); ++i
)
1834 ret
->tex_unit_map
[i
] = base
+ i
;
1835 ret
->rev_tex_unit_map
[base
+ i
] = i
;
1838 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_VERTEX
, &base
, &count
);
1839 if (base
+ MAX_VERTEX_SAMPLERS
> ARRAY_SIZE(ret
->rev_tex_unit_map
))
1841 ERR("Unexpected texture unit base index %u.\n", base
);
1844 for (i
= 0; i
< min(count
, MAX_VERTEX_SAMPLERS
); ++i
)
1846 ret
->tex_unit_map
[MAX_FRAGMENT_SAMPLERS
+ i
] = base
+ i
;
1847 ret
->rev_tex_unit_map
[base
+ i
] = MAX_FRAGMENT_SAMPLERS
+ i
;
1851 if (!(ret
->texture_type
= wined3d_calloc(gl_info
->limits
.combined_samplers
,
1852 sizeof(*ret
->texture_type
))))
1855 if (!(hdc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1857 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1859 if ((hdc
= swapchain_get_backup_dc(swapchain
)))
1860 hdc_is_private
= TRUE
;
1863 ERR("Failed to retrieve a device context.\n");
1868 color_format
= target
->resource
.format
;
1869 target_usage
= target
->resource
.usage
;
1871 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1872 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1873 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1877 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1878 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
, target_usage
);
1879 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1880 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
, target_usage
);
1883 /* DirectDraw supports 8bit paletted render targets and these are used by
1884 * old games like StarCraft and C&C. Most modern hardware doesn't support
1885 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1886 * conversion (ab)uses the alpha component for storing the palette index.
1887 * For this reason we require a format with 8bit alpha, so request
1889 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1890 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
, target_usage
);
1892 /* When using FBOs for off-screen rendering, we only use the drawable for
1893 * presentation blits, and don't do any rendering to it. That means we
1894 * don't need depth or stencil buffers, and can mostly ignore the render
1895 * target format. This wouldn't necessarily be quite correct for 10bpc
1896 * display modes, but we don't currently support those.
1897 * Using the same format regardless of the color/depth/stencil targets
1898 * makes it much less likely that different wined3d instances will set
1899 * conflicting pixel formats. */
1900 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
)
1902 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
, target_usage
);
1903 ds_format
= wined3d_get_format(gl_info
, WINED3DFMT_UNKNOWN
, WINED3DUSAGE_DEPTHSTENCIL
);
1906 /* Try to find a pixel format which matches our requirements. */
1907 if (!(pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
)))
1910 ret
->gl_info
= gl_info
;
1914 if (!context_set_pixel_format(ret
, hdc
, hdc_is_private
, pixel_format
))
1916 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1917 context_release(ret
);
1921 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1922 if (gl_info
->p_wglCreateContextAttribsARB
)
1924 if (!(ctx
= context_create_wgl_attribs(gl_info
, hdc
, share_ctx
)))
1929 if (!(ctx
= wglCreateContext(hdc
)))
1931 ERR("Failed to create a WGL context.\n");
1932 context_release(ret
);
1936 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1938 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1939 context_release(ret
);
1940 if (!wglDeleteContext(ctx
))
1941 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1946 if (!device_context_add(device
, ret
))
1948 ERR("Failed to add the newly created context to the context list\n");
1949 context_release(ret
);
1950 if (!wglDeleteContext(ctx
))
1951 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1955 ret
->d3d_info
= d3d_info
;
1956 ret
->state_table
= device
->StateTable
;
1958 /* Mark all states dirty to force a proper initialization of the states on
1959 * the first use of the context. Compute states do not need initialization. */
1960 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1962 if (ret
->state_table
[state
].representative
&& !STATE_IS_COMPUTE(state
))
1963 context_invalidate_state(ret
, state
);
1966 ret
->device
= device
;
1967 ret
->swapchain
= swapchain
;
1968 ret
->current_rt
.texture
= target
;
1969 ret
->current_rt
.sub_resource_idx
= 0;
1970 ret
->tid
= GetCurrentThreadId();
1972 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->resource
);
1973 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1977 ret
->win_handle
= swapchain
->win_handle
;
1979 ret
->hdc_is_private
= hdc_is_private
;
1980 ret
->hdc_has_format
= TRUE
;
1981 ret
->pixel_format
= pixel_format
;
1984 /* Set up the context defaults */
1985 if (!context_set_current(ret
))
1987 ERR("Cannot activate context to set up defaults.\n");
1988 device_context_remove(device
, ret
);
1989 context_release(ret
);
1990 if (!wglDeleteContext(ctx
))
1991 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1995 if (context_debug_output_enabled(gl_info
))
1997 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, ret
));
1998 if (TRACE_ON(d3d_synchronous
))
1999 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
2000 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
2003 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
2004 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2008 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
2009 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2010 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
2011 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2012 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
2013 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2015 if (WARN_ON(d3d_perf
))
2017 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
2018 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2022 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2023 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
2025 TRACE("Setting up the screen\n");
2027 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2029 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
2030 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2032 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
2033 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2035 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
2036 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2042 GL_EXTCALL(glGenVertexArrays(1, &vao
));
2043 GL_EXTCALL(glBindVertexArray(vao
));
2044 checkGLcall("creating VAO");
2047 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
2048 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2049 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
2050 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2052 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
2054 /* Direct3D always uses n-1 weights for n world matrices and uses
2055 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
2056 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
2057 * enabled as well. */
2058 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
2059 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
2061 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
2063 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2064 * the previous texture where to source the offset from is always unit - 1.
2066 for (i
= 1; i
< gl_info
->limits
.textures
; ++i
)
2068 context_active_texture(ret
, gl_info
, i
);
2069 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
2070 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ i
- 1);
2071 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2074 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
2076 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2077 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2078 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2079 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2082 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2083 * program and the dummy program is destroyed when the context is destroyed.
2085 static const char dummy_program
[] =
2087 "MOV result.color, fragment.color.primary;\n"
2089 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
2090 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
2091 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
2094 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2096 for (i
= 0; i
< gl_info
->limits
.textures
; ++i
)
2098 context_active_texture(ret
, gl_info
, i
);
2099 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
2100 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2104 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
2106 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
2108 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
2110 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
2112 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_NO_PRIMITIVE_RESTART
))
2114 if (gl_info
->supported
[ARB_ES3_COMPATIBILITY
])
2116 gl_info
->gl_ops
.gl
.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX
);
2117 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2121 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2124 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_CUBEMAP_FILTERING
)
2125 && gl_info
->supported
[ARB_SEAMLESS_CUBE_MAP
])
2127 gl_info
->gl_ops
.gl
.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
2128 checkGLcall("enable seamless cube map filtering");
2130 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
2131 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN
, GL_LOWER_LEFT
));
2132 device
->shader_backend
->shader_init_context_state(ret
);
2133 ret
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
2134 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
2135 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
2136 | (1u << WINED3D_SHADER_TYPE_HULL
)
2137 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
2138 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
2140 /* If this happens to be the first context for the device, dummy textures
2141 * are not created yet. In that case, they will be created (and bound) by
2142 * create_dummy_textures right after this context is initialized. */
2143 if (device
->dummy_textures
.tex_2d
)
2144 context_bind_dummy_textures(device
, ret
);
2146 TRACE("Created context %p.\n", ret
);
2151 if (hdc
) wined3d_release_dc(swapchain
->win_handle
, hdc
);
2152 device
->shader_backend
->shader_free_context_data(ret
);
2153 device
->adapter
->fragment_pipe
->free_context_data(ret
);
2154 HeapFree(GetProcessHeap(), 0, ret
->texture_type
);
2155 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
2156 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
2157 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
2158 HeapFree(GetProcessHeap(), 0, ret
->fbo_key
);
2159 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
2160 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
2161 HeapFree(GetProcessHeap(), 0, ret
);
2165 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
2169 TRACE("Destroying ctx %p\n", context
);
2171 wined3d_from_cs(device
->cs
);
2173 /* We delay destroying a context when it is active. The context_release()
2174 * function invokes context_destroy() again while leaving the last level. */
2177 TRACE("Delaying destruction of context %p.\n", context
);
2178 context
->destroy_delayed
= 1;
2179 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2180 context
->swapchain
= NULL
;
2184 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
2186 context_destroy_gl_resources(context
);
2187 TlsSetValue(wined3d_context_tls_idx
, NULL
);
2192 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2193 in wined3d_adapter may go away in the meantime */
2194 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
2195 *gl_info
= *context
->gl_info
;
2196 context
->gl_info
= gl_info
;
2197 context
->destroyed
= 1;
2201 device
->shader_backend
->shader_free_context_data(context
);
2202 device
->adapter
->fragment_pipe
->free_context_data(context
);
2203 HeapFree(GetProcessHeap(), 0, context
->texture_type
);
2204 HeapFree(GetProcessHeap(), 0, context
->fbo_key
);
2205 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
2206 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
2207 device_context_remove(device
, context
);
2208 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
2211 const DWORD
*context_get_tex_unit_mapping(const struct wined3d_context
*context
,
2212 const struct wined3d_shader_version
*shader_version
, unsigned int *base
, unsigned int *count
)
2214 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2216 if (!shader_version
)
2219 *count
= MAX_TEXTURES
;
2220 return context
->tex_unit_map
;
2223 if (shader_version
->major
>= 4)
2225 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, shader_version
->type
, base
, count
);
2229 switch (shader_version
->type
)
2231 case WINED3D_SHADER_TYPE_PIXEL
:
2233 *count
= MAX_FRAGMENT_SAMPLERS
;
2235 case WINED3D_SHADER_TYPE_VERTEX
:
2236 *base
= MAX_FRAGMENT_SAMPLERS
;
2237 *count
= MAX_VERTEX_SAMPLERS
;
2240 ERR("Unhandled shader type %#x.\n", shader_version
->type
);
2245 return context
->tex_unit_map
;
2248 /* Context activation is done by the caller. */
2249 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
2251 const GLdouble projection
[] =
2253 2.0 / width
, 0.0, 0.0, 0.0,
2254 0.0, 2.0 / height
, 0.0, 0.0,
2256 -1.0, -1.0, -1.0, 1.0,
2259 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
2260 checkGLcall("glMatrixMode(GL_PROJECTION)");
2261 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
2262 checkGLcall("glLoadMatrixd");
2263 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
2264 checkGLcall("glViewport");
2267 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
2269 const struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2276 GetClientRect(context
->win_handle
, &window_size
);
2277 size
->cx
= window_size
.right
- window_size
.left
;
2278 size
->cy
= window_size
.bottom
- window_size
.top
;
2283 level
= context
->current_rt
.sub_resource_idx
% rt
->level_count
;
2284 size
->cx
= wined3d_texture_get_level_width(rt
, level
);
2285 size
->cy
= wined3d_texture_get_level_height(rt
, level
);
2288 /*****************************************************************************
2291 * Sets up a context for DirectDraw blitting.
2292 * All texture units are disabled, texture unit 0 is set as current unit
2293 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
2294 * color writing enabled for all channels
2295 * register combiners disabled, shaders disabled
2296 * world matrix is set to identity, texture matrix 0 too
2297 * projection matrix is setup for drawing screen coordinates
2300 * This: Device to activate the context for
2301 * context: Context to setup
2303 *****************************************************************************/
2304 /* Context activation is done by the caller. */
2305 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
2308 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2312 TRACE("Setting up context %p for blitting\n", context
);
2314 context_get_rt_size(context
, &rt_size
);
2316 if (context
->last_was_blit
)
2318 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
2320 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2321 context
->blit_w
= rt_size
.cx
;
2322 context
->blit_h
= rt_size
.cy
;
2323 /* No need to dirtify here, the states are still dirtified because
2324 * they weren't applied since the last SetupForBlit() call. */
2326 TRACE("Context is already set up for blitting, nothing to do\n");
2329 context
->last_was_blit
= TRUE
;
2331 /* Disable all textures. The caller can then bind a texture it wants to blit
2334 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
2335 * function texture unit. No need to care for higher samplers
2337 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
2339 sampler
= context
->rev_tex_unit_map
[i
];
2340 context_active_texture(context
, gl_info
, i
);
2342 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2344 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2345 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2347 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2348 checkGLcall("glDisable GL_TEXTURE_3D");
2349 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2351 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2352 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2354 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2355 checkGLcall("glDisable GL_TEXTURE_2D");
2357 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2358 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
2360 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2362 if (sampler
< MAX_TEXTURES
)
2363 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2364 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2367 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
2368 GL_EXTCALL(glBindSampler(0, 0));
2369 context_active_texture(context
, gl_info
, 0);
2371 sampler
= context
->rev_tex_unit_map
[0];
2373 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2375 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2376 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2378 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2379 checkGLcall("glDisable GL_TEXTURE_3D");
2380 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2382 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2383 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2385 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2386 checkGLcall("glDisable GL_TEXTURE_2D");
2388 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2390 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
2391 checkGLcall("glMatrixMode(GL_TEXTURE)");
2392 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2393 checkGLcall("glLoadIdentity()");
2395 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
2397 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
2398 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
2399 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2402 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2404 if (sampler
< MAX_TEXTURES
)
2406 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
2407 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2409 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2412 /* Other misc states */
2413 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2414 checkGLcall("glDisable(GL_ALPHA_TEST)");
2415 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
2416 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
2417 checkGLcall("glDisable GL_LIGHTING");
2418 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
2419 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
2420 checkGLcall("glDisable GL_DEPTH_TEST");
2421 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
2422 glDisableWINE(GL_FOG
);
2423 checkGLcall("glDisable GL_FOG");
2424 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
2425 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2426 checkGLcall("glDisable GL_BLEND");
2427 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2428 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
2429 checkGLcall("glDisable GL_CULL_FACE");
2430 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
2431 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
2432 checkGLcall("glDisable GL_STENCIL_TEST");
2433 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
2434 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
2435 checkGLcall("glDisable GL_SCISSOR_TEST");
2436 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2437 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2439 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
2440 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2441 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
2443 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
2444 checkGLcall("glColorMask");
2445 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
2446 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
2447 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
2448 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2449 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2451 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2452 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2453 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2456 /* Setup transforms */
2457 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2458 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2459 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2460 checkGLcall("glLoadIdentity()");
2461 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2463 context
->last_was_rhw
= TRUE
;
2464 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2466 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2467 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2468 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2469 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2470 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2471 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2472 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2474 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
2475 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
2476 GL_EXTCALL(glClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
));
2478 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2480 /* Disable shaders */
2481 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2483 context
->blit_w
= rt_size
.cx
;
2484 context
->blit_h
= rt_size
.cy
;
2485 context_invalidate_state(context
, STATE_VIEWPORT
);
2486 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2489 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2491 return rt_mask
& (1u << 31);
2494 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2496 return rt_mask
& ~(1u << 31);
2499 /* Context activation is done by the caller. */
2500 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2502 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2506 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2507 checkGLcall("glDrawBuffer()");
2509 else if (is_rt_mask_onscreen(rt_mask
))
2511 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2512 checkGLcall("glDrawBuffer()");
2516 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2523 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2525 context
->draw_buffers
[i
] = GL_NONE
;
2531 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2533 GL_EXTCALL(glDrawBuffers(i
, context
->draw_buffers
));
2534 checkGLcall("glDrawBuffers()");
2538 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2539 checkGLcall("glDrawBuffer()");
2544 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2549 /* Context activation is done by the caller. */
2550 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2552 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2553 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2554 DWORD new_mask
= context_generate_rt_mask(buffer
);
2556 if (new_mask
== *current_mask
)
2559 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2560 checkGLcall("glDrawBuffer()");
2562 *current_mask
= new_mask
;
2565 /* Context activation is done by the caller. */
2566 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2568 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2569 checkGLcall("glActiveTexture");
2570 context
->active_texture
= unit
;
2573 void context_bind_bo(struct wined3d_context
*context
, GLenum binding
, GLuint name
)
2575 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2577 if (binding
== GL_ELEMENT_ARRAY_BUFFER
)
2578 context_invalidate_state(context
, STATE_INDEXBUFFER
);
2580 GL_EXTCALL(glBindBuffer(binding
, name
));
2583 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2585 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2586 DWORD unit
= context
->active_texture
;
2587 DWORD old_texture_type
= context
->texture_type
[unit
];
2591 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2592 checkGLcall("glBindTexture");
2599 if (old_texture_type
!= target
)
2601 const struct wined3d_device
*device
= context
->device
;
2603 switch (old_texture_type
)
2609 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_textures
.tex_2d
);
2610 checkGLcall("glBindTexture");
2612 case GL_TEXTURE_2D_ARRAY
:
2613 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, device
->dummy_textures
.tex_2d_array
);
2614 checkGLcall("glBindTexture");
2616 case GL_TEXTURE_RECTANGLE_ARB
:
2617 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_textures
.tex_rect
);
2618 checkGLcall("glBindTexture");
2620 case GL_TEXTURE_CUBE_MAP
:
2621 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_textures
.tex_cube
);
2622 checkGLcall("glBindTexture");
2624 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2625 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, device
->dummy_textures
.tex_cube_array
);
2626 checkGLcall("glBindTexture");
2629 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_textures
.tex_3d
);
2630 checkGLcall("glBindTexture");
2632 case GL_TEXTURE_BUFFER
:
2633 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, device
->dummy_textures
.tex_buffer
);
2634 checkGLcall("glBindTexture");
2637 ERR("Unexpected texture target %#x.\n", old_texture_type
);
2640 context
->texture_type
[unit
] = target
;
2644 void *context_map_bo_address(struct wined3d_context
*context
,
2645 const struct wined3d_bo_address
*data
, size_t size
, GLenum binding
, DWORD flags
)
2647 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2650 if (!data
->buffer_object
)
2653 context_bind_bo(context
, binding
, data
->buffer_object
);
2655 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
2657 GLbitfield map_flags
= wined3d_resource_gl_map_flags(flags
) & ~GL_MAP_FLUSH_EXPLICIT_BIT
;
2658 memory
= GL_EXTCALL(glMapBufferRange(binding
, (INT_PTR
)data
->addr
, size
, map_flags
));
2662 memory
= GL_EXTCALL(glMapBuffer(binding
, wined3d_resource_gl_legacy_map_flags(flags
)));
2663 memory
+= (INT_PTR
)data
->addr
;
2666 context_bind_bo(context
, binding
, 0);
2667 checkGLcall("Map buffer object");
2672 void context_unmap_bo_address(struct wined3d_context
*context
,
2673 const struct wined3d_bo_address
*data
, GLenum binding
)
2675 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2677 if (!data
->buffer_object
)
2680 context_bind_bo(context
, binding
, data
->buffer_object
);
2681 GL_EXTCALL(glUnmapBuffer(binding
));
2682 context_bind_bo(context
, binding
, 0);
2683 checkGLcall("Unmap buffer object");
2686 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2688 if (context
->render_offscreen
== offscreen
)
2691 context_invalidate_state(context
, STATE_VIEWPORT
);
2692 context_invalidate_state(context
, STATE_SCISSORRECT
);
2693 if (!context
->gl_info
->supported
[ARB_CLIP_CONTROL
])
2695 context_invalidate_state(context
, STATE_FRONTFACE
);
2696 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2697 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2699 context_invalidate_state(context
, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN
));
2700 if (context
->gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
2701 context_invalidate_state(context
, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
));
2702 context
->render_offscreen
= offscreen
;
2705 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2706 const struct wined3d_format
*required
)
2708 if (existing
== required
)
2710 if ((existing
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
)
2711 != (required
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
))
2713 if (existing
->depth_size
< required
->depth_size
)
2715 /* If stencil bits are used the exact amount is required - otherwise
2716 * wrapping won't work correctly. */
2717 if (required
->stencil_size
&& required
->stencil_size
!= existing
->stencil_size
)
2722 /* Context activation is done by the caller. */
2723 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2724 const struct wined3d_rendertarget_view
*depth_stencil
)
2726 /* Onscreen surfaces are always in a swapchain */
2727 struct wined3d_swapchain
*swapchain
= context
->current_rt
.texture
->swapchain
;
2729 if (context
->render_offscreen
|| !depth_stencil
) return;
2730 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2732 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2733 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2735 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2737 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2738 if (!(wined3d_texture_load_location(context
->current_rt
.texture
, context
->current_rt
.sub_resource_idx
,
2739 context
, WINED3D_LOCATION_TEXTURE_RGB
)))
2740 ERR("Failed to load location.\n");
2741 swapchain
->render_to_fbo
= TRUE
;
2742 swapchain_update_draw_bindings(swapchain
);
2743 context_set_render_offscreen(context
, TRUE
);
2746 GLenum
context_get_offscreen_gl_buffer(const struct wined3d_context
*context
)
2748 switch (wined3d_settings
.offscreen_rendering_mode
)
2751 return GL_COLOR_ATTACHMENT0
;
2753 case ORM_BACKBUFFER
:
2754 return context
->aux_buffers
> 0 ? GL_AUX0
: GL_BACK
;
2757 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
2762 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_context
*context
, struct wined3d_texture
*rt
)
2764 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2766 else if (rt
->swapchain
)
2767 return context_generate_rt_mask_from_resource(&rt
->resource
);
2769 return context_generate_rt_mask(context_get_offscreen_gl_buffer(context
));
2772 /* Context activation is done by the caller. */
2773 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2775 struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2776 struct wined3d_surface
*surface
;
2777 DWORD rt_mask
, *cur_mask
;
2779 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2781 context_validate_onscreen_formats(context
, NULL
);
2783 if (context
->render_offscreen
)
2785 wined3d_texture_load(rt
, context
, FALSE
);
2787 surface
= rt
->sub_resources
[context
->current_rt
.sub_resource_idx
].u
.surface
;
2788 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, surface
, NULL
, rt
->resource
.draw_binding
);
2789 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2796 context
->current_fbo
= NULL
;
2797 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2798 rt_mask
= context_generate_rt_mask_from_resource(&rt
->resource
);
2803 rt_mask
= context_generate_rt_mask_no_fbo(context
, rt
);
2806 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2808 if (rt_mask
!= *cur_mask
)
2810 context_apply_draw_buffers(context
, rt_mask
);
2811 *cur_mask
= rt_mask
;
2814 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2816 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2819 SetupForBlit(device
, context
);
2820 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2823 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2824 const struct wined3d_rendertarget_view
*ds
)
2828 if (ds
) return TRUE
;
2830 for (i
= 0; i
< rt_count
; ++i
)
2832 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2836 WARN("Invalid render target config, need at least one attachment.\n");
2840 /* Context activation is done by the caller. */
2841 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_state
*state
,
2842 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2844 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2845 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
2846 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2847 DWORD rt_mask
= 0, *cur_mask
;
2850 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= state
->fb
2851 || rt_count
!= gl_info
->limits
.buffers
)
2853 if (!context_validate_rt_config(rt_count
, rts
, dsv
))
2856 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2858 context_validate_onscreen_formats(context
, dsv
);
2860 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2862 memset(context
->blit_targets
, 0, gl_info
->limits
.buffers
* sizeof(*context
->blit_targets
));
2863 for (i
= 0; i
< rt_count
; ++i
)
2867 context
->blit_targets
[i
].gl_view
= rts
[i
]->gl_view
;
2868 context
->blit_targets
[i
].resource
= rts
[i
]->resource
;
2869 context
->blit_targets
[i
].sub_resource_idx
= rts
[i
]->sub_resource_idx
;
2870 context
->blit_targets
[i
].layer_count
= rts
[i
]->layer_count
;
2872 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2873 rt_mask
|= (1u << i
);
2875 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2876 wined3d_rendertarget_view_get_surface(dsv
),
2877 rt_count
? rts
[0]->resource
->draw_binding
: 0,
2878 dsv
? dsv
->resource
->draw_binding
: 0);
2882 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2883 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2884 rt_mask
= context_generate_rt_mask_from_resource(rts
[0]->resource
);
2887 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2888 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2889 * state management allows this */
2890 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2894 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2895 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0])->container
: NULL
);
2898 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2899 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2901 for (i
= 0; i
< rt_count
; ++i
)
2903 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2904 rt_mask
|= (1u << i
);
2909 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2910 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0])->container
: NULL
);
2913 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2915 if (rt_mask
!= *cur_mask
)
2917 context_apply_draw_buffers(context
, rt_mask
);
2918 *cur_mask
= rt_mask
;
2919 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2922 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2924 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2927 context
->last_was_blit
= FALSE
;
2929 /* Blending and clearing should be orthogonal, but tests on the nvidia
2930 * driver show that disabling blending when clearing improves the clearing
2931 * performance incredibly. */
2932 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2933 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2934 if (rt_count
&& gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
2936 if (needs_srgb_write(context
, state
, fb
))
2937 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
2939 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
2940 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
2942 checkGLcall("setting up state for clear");
2944 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2945 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2946 context_invalidate_state(context
, STATE_SCISSORRECT
);
2951 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_state
*state
)
2953 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
2954 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2955 DWORD rt_mask
, rt_mask_bits
;
2958 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2959 return context_generate_rt_mask_no_fbo(context
, wined3d_rendertarget_view_get_surface(rts
[0])->container
);
2960 else if (!context
->render_offscreen
)
2961 return context_generate_rt_mask_from_resource(rts
[0]->resource
);
2963 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2964 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2965 rt_mask_bits
= rt_mask
;
2967 while (rt_mask_bits
)
2969 rt_mask_bits
&= ~(1u << i
);
2970 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
2971 rt_mask
&= ~(1u << i
);
2979 /* Context activation is done by the caller. */
2980 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2982 DWORD rt_mask
= find_draw_buffers_mask(context
, state
);
2983 const struct wined3d_fb_state
*fb
= state
->fb
;
2986 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2988 if (!context
->render_offscreen
)
2990 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2991 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2997 memset(context
->blit_targets
, 0, context
->gl_info
->limits
.buffers
* sizeof (*context
->blit_targets
));
2998 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
3000 if (fb
->render_targets
[i
])
3002 context
->blit_targets
[i
].gl_view
= fb
->render_targets
[i
]->gl_view
;
3003 context
->blit_targets
[i
].resource
= fb
->render_targets
[i
]->resource
;
3004 context
->blit_targets
[i
].sub_resource_idx
= fb
->render_targets
[i
]->sub_resource_idx
;
3005 context
->blit_targets
[i
].layer_count
= fb
->render_targets
[i
]->layer_count
;
3008 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
3009 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
3010 fb
->render_targets
[0] ? fb
->render_targets
[0]->resource
->draw_binding
: 0,
3011 fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
3015 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
3016 if (rt_mask
!= *cur_mask
)
3018 context_apply_draw_buffers(context
, rt_mask
);
3019 *cur_mask
= rt_mask
;
3021 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
3024 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
3026 DWORD i
= context
->rev_tex_unit_map
[unit
];
3027 DWORD j
= context
->tex_unit_map
[stage
];
3029 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
3030 context
->tex_unit_map
[stage
] = unit
;
3031 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
3032 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
3034 context
->rev_tex_unit_map
[unit
] = stage
;
3035 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
3036 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
3039 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
3043 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
3044 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
3047 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
3048 const struct wined3d_state
*state
)
3052 context
->fixed_function_usage_map
= 0;
3053 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
3055 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
3056 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
3057 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
3058 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
3059 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
3060 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
3061 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
3062 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
3064 /* Not used, and disable higher stages. */
3065 if (color_op
== WINED3D_TOP_DISABLE
)
3068 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
3069 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
3070 || ((color_arg3
== WINED3DTA_TEXTURE
)
3071 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
3072 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
3073 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
3074 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
3075 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
3076 context
->fixed_function_usage_map
|= (1u << i
);
3078 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
3079 && i
< MAX_TEXTURES
- 1)
3080 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
3083 if (i
< context
->lowest_disabled_stage
)
3086 end
= context
->lowest_disabled_stage
;
3090 start
= context
->lowest_disabled_stage
;
3094 context
->lowest_disabled_stage
= i
;
3095 for (i
= start
+ 1; i
< end
; ++i
)
3097 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3101 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
3102 const struct wined3d_state
*state
)
3104 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3105 unsigned int i
, tex
;
3108 ffu_map
= context
->fixed_function_usage_map
;
3110 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
3111 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
3113 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3118 if (context
->tex_unit_map
[i
] != i
)
3120 context_map_stage(context
, i
, i
);
3121 context_invalidate_state(context
, STATE_SAMPLER(i
));
3122 context_invalidate_texture_stage(context
, i
);
3128 /* Now work out the mapping */
3130 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3135 if (context
->tex_unit_map
[i
] != tex
)
3137 context_map_stage(context
, i
, tex
);
3138 context_invalidate_state(context
, STATE_SAMPLER(i
));
3139 context_invalidate_texture_stage(context
, i
);
3146 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3148 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3149 const struct wined3d_shader_resource_info
*resource_info
=
3150 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3153 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3155 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
3157 context_map_stage(context
, i
, i
);
3158 context_invalidate_state(context
, STATE_SAMPLER(i
));
3159 if (i
< d3d_info
->limits
.ffp_blend_stages
)
3160 context_invalidate_texture_stage(context
, i
);
3165 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
3166 const struct wined3d_shader_resource_info
*ps_resource_info
, DWORD unit
)
3168 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
3170 /* Not currently used */
3171 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
3174 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
3176 /* Used by a fragment sampler */
3178 if (!ps_resource_info
)
3180 /* No pixel shader, check fixed function */
3181 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1u << current_mapping
));
3184 /* Pixel shader, check the shader's sampler map */
3185 return !ps_resource_info
[current_mapping
].type
;
3191 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
3193 const struct wined3d_shader_resource_info
*vs_resource_info
=
3194 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
3195 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
3196 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3197 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.graphics_samplers
) - 1;
3200 /* Note that we only care if a resource is used or not, not the
3201 * resource's specific type. Otherwise we'd need to call
3202 * shader_update_samplers() here for 1.x pixelshaders. */
3204 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3206 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3208 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
3209 if (vs_resource_info
[i
].type
)
3213 if (context_unit_free_for_vs(context
, ps_resource_info
, start
))
3215 if (context
->tex_unit_map
[vsampler_idx
] != start
)
3217 context_map_stage(context
, vsampler_idx
, start
);
3218 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
3227 if (context
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
3228 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
3233 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3235 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3236 BOOL vs
= use_vs(state
);
3237 BOOL ps
= use_ps(state
);
3240 context_update_fixed_function_usage_map(context
, state
);
3242 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3243 * need a 1:1 map at the moment.
3244 * When the mapping of a stage is changed, sampler and ALL texture stage
3245 * states have to be reset. */
3247 if (gl_info
->limits
.graphics_samplers
>= MAX_COMBINED_SAMPLERS
)
3251 context_map_psamplers(context
, state
);
3253 context_map_fixed_function_samplers(context
, state
);
3256 context_map_vsamplers(context
, ps
, state
);
3259 /* Context activation is done by the caller. */
3260 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3262 DWORD rt_mask
, *cur_mask
;
3264 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
3266 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
3267 rt_mask
= find_draw_buffers_mask(context
, state
);
3268 if (rt_mask
!= *cur_mask
)
3270 context_apply_draw_buffers(context
, rt_mask
);
3271 *cur_mask
= rt_mask
;
3275 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
3277 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
3278 *regnum
= WINED3D_FFP_POSITION
;
3279 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
3280 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
3281 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
3282 *regnum
= WINED3D_FFP_BLENDINDICES
;
3283 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
3284 *regnum
= WINED3D_FFP_NORMAL
;
3285 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
3286 *regnum
= WINED3D_FFP_PSIZE
;
3287 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
3288 *regnum
= WINED3D_FFP_DIFFUSE
;
3289 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
3290 *regnum
= WINED3D_FFP_SPECULAR
;
3291 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
3292 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
3295 WARN("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
3303 /* Context activation is done by the caller. */
3304 void wined3d_stream_info_from_declaration(struct wined3d_stream_info
*stream_info
,
3305 const struct wined3d_state
*state
, const struct wined3d_gl_info
*gl_info
,
3306 const struct wined3d_d3d_info
*d3d_info
)
3308 /* We need to deal with frequency data! */
3309 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
3310 BOOL generic_attributes
= d3d_info
->ffp_generic_attributes
;
3311 BOOL use_vshader
= use_vs(state
);
3314 stream_info
->use_map
= 0;
3315 stream_info
->swizzle_map
= 0;
3316 stream_info
->position_transformed
= 0;
3321 stream_info
->position_transformed
= declaration
->position_transformed
;
3323 /* Translate the declaration into strided data. */
3324 for (i
= 0; i
< declaration
->element_count
; ++i
)
3326 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
3327 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
3331 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
3332 element
, i
+ 1, declaration
->element_count
);
3334 if (!stream
->buffer
)
3337 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
3341 if (element
->output_slot
== WINED3D_OUTPUT_SLOT_UNUSED
)
3343 stride_used
= FALSE
;
3345 else if (element
->output_slot
== WINED3D_OUTPUT_SLOT_SEMANTIC
)
3347 /* TODO: Assuming vertexdeclarations are usually used with the
3348 * same or a similar shader, it might be worth it to store the
3349 * last used output slot and try that one first. */
3350 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
3351 element
->usage
, element
->usage_idx
, &idx
);
3355 idx
= element
->output_slot
;
3361 if (!generic_attributes
&& !element
->ffp_valid
)
3363 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3364 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
3365 stride_used
= FALSE
;
3369 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
3375 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3376 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3377 use_vshader
? "shader": "fixed function", idx
,
3378 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
3379 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
),
3380 debug_d3dinput_classification(element
->input_slot_class
), element
->instance_data_step_rate
);
3382 stream_info
->elements
[idx
].format
= element
->format
;
3383 stream_info
->elements
[idx
].data
.buffer_object
= 0;
3384 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
3385 stream_info
->elements
[idx
].stride
= stream
->stride
;
3386 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
3387 if (stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
3389 stream_info
->elements
[idx
].divisor
= 1;
3391 else if (element
->input_slot_class
== WINED3D_INPUT_PER_INSTANCE_DATA
)
3393 stream_info
->elements
[idx
].divisor
= element
->instance_data_step_rate
;
3394 if (!element
->instance_data_step_rate
)
3395 FIXME("Instance step rate 0 not implemented.\n");
3399 stream_info
->elements
[idx
].divisor
= 0;
3402 if (!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3403 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
3405 stream_info
->swizzle_map
|= 1u << idx
;
3407 stream_info
->use_map
|= 1u << idx
;
3412 /* Context activation is done by the caller. */
3413 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3415 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
3416 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3417 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3418 DWORD prev_all_vbo
= stream_info
->all_vbo
;
3422 wined3d_stream_info_from_declaration(stream_info
, state
, gl_info
, d3d_info
);
3424 stream_info
->all_vbo
= 1;
3425 context
->num_buffer_queries
= 0;
3426 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
3428 struct wined3d_stream_info_element
*element
;
3429 struct wined3d_bo_address data
;
3430 struct wined3d_buffer
*buffer
;
3435 element
= &stream_info
->elements
[i
];
3436 buffer
= state
->streams
[element
->stream_idx
].buffer
;
3438 /* We can't use VBOs if the base vertex index is negative. OpenGL
3439 * doesn't accept negative offsets (or rather offsets bigger than the
3440 * VBO, because the pointer is unsigned), so use system memory
3441 * sources. In most sane cases the pointer - offset will still be > 0,
3442 * otherwise it will wrap around to some big value. Hope that with the
3443 * indices the driver wraps it back internally. If not,
3444 * draw_primitive_immediate_mode() is needed, including a vertex buffer
3446 if (state
->load_base_vertex_index
< 0)
3448 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3449 state
->load_base_vertex_index
);
3450 element
->data
.buffer_object
= 0;
3451 element
->data
.addr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(buffer
, context
);
3452 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
3453 FIXME("System memory vertex data load offset is negative!\n");
3457 wined3d_buffer_load(buffer
, context
, state
);
3458 wined3d_buffer_get_memory(buffer
, &data
, buffer
->locations
);
3459 element
->data
.buffer_object
= data
.buffer_object
;
3460 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
3463 if (!element
->data
.buffer_object
)
3464 stream_info
->all_vbo
= 0;
3467 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
3469 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
3472 if (prev_all_vbo
!= stream_info
->all_vbo
)
3473 context_invalidate_state(context
, STATE_INDEXBUFFER
);
3475 context
->use_immediate_mode_draw
= FALSE
;
3477 if (stream_info
->all_vbo
)
3482 if (state
->vertex_declaration
->half_float_conv_needed
)
3484 TRACE("Using immediate mode draw with vertex shaders for FLOAT16 conversion.\n");
3485 context
->use_immediate_mode_draw
= TRUE
;
3490 WORD slow_mask
= -!d3d_info
->ffp_generic_attributes
& (1u << WINED3D_FFP_PSIZE
);
3491 slow_mask
|= -(!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
] && !d3d_info
->ffp_generic_attributes
)
3492 & ((1u << WINED3D_FFP_DIFFUSE
) | (1u << WINED3D_FFP_SPECULAR
) | (1u << WINED3D_FFP_BLENDWEIGHT
));
3494 if ((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
3495 || (stream_info
->use_map
& slow_mask
))
3496 context
->use_immediate_mode_draw
= TRUE
;
3500 /* Context activation is done by the caller. */
3501 static void context_preload_texture(struct wined3d_context
*context
,
3502 const struct wined3d_state
*state
, unsigned int idx
)
3504 struct wined3d_texture
*texture
;
3506 if (!(texture
= state
->textures
[idx
]))
3509 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
3512 /* Context activation is done by the caller. */
3513 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3519 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3521 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
3522 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
3528 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3530 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
3531 context_preload_texture(context
, state
, i
);
3536 WORD ffu_map
= context
->fixed_function_usage_map
;
3538 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3541 context_preload_texture(context
, state
, i
);
3546 static void context_load_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
,
3547 unsigned int shader_mask
)
3549 struct wined3d_shader_sampler_map_entry
*entry
;
3550 struct wined3d_shader_resource_view
*view
;
3551 struct wined3d_shader
*shader
;
3554 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3556 if (!(shader_mask
& (1u << i
)))
3559 if (!(shader
= state
->shader
[i
]))
3562 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3564 if (state
->cb
[i
][j
])
3565 wined3d_buffer_load(state
->cb
[i
][j
], context
, state
);
3568 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
3570 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3572 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
3575 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3576 wined3d_buffer_load(buffer_from_resource(view
->resource
), context
, state
);
3578 wined3d_texture_load(texture_from_resource(view
->resource
), context
, FALSE
);
3583 static void context_bind_shader_resources(struct wined3d_context
*context
,
3584 const struct wined3d_state
*state
, enum wined3d_shader_type shader_type
)
3586 unsigned int bind_idx
, shader_sampler_count
, base
, count
, i
;
3587 const struct wined3d_device
*device
= context
->device
;
3588 struct wined3d_shader_sampler_map_entry
*entry
;
3589 struct wined3d_shader_resource_view
*view
;
3590 const struct wined3d_shader
*shader
;
3591 struct wined3d_sampler
*sampler
;
3592 const DWORD
*tex_unit_map
;
3594 if (!(shader
= state
->shader
[shader_type
]))
3597 tex_unit_map
= context_get_tex_unit_mapping(context
,
3598 &shader
->reg_maps
.shader_version
, &base
, &count
);
3600 shader_sampler_count
= shader
->reg_maps
.sampler_map
.count
;
3601 if (shader_sampler_count
> count
)
3602 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3603 shader
, shader_sampler_count
, count
);
3604 count
= min(shader_sampler_count
, count
);
3606 for (i
= 0; i
< count
; ++i
)
3608 entry
= &shader
->reg_maps
.sampler_map
.entries
[i
];
3609 bind_idx
= base
+ entry
->bind_idx
;
3611 bind_idx
= tex_unit_map
[bind_idx
];
3613 if (!(view
= state
->shader_resource_view
[shader_type
][entry
->resource_idx
]))
3615 WARN("No resource view bound at index %u, %u.\n", shader_type
, entry
->resource_idx
);
3619 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
3620 sampler
= device
->default_sampler
;
3621 else if (!(sampler
= state
->sampler
[shader_type
][entry
->sampler_idx
]))
3622 sampler
= device
->null_sampler
;
3623 wined3d_shader_resource_view_bind(view
, bind_idx
, sampler
, context
);
3627 static void context_load_unordered_access_resources(struct wined3d_context
*context
,
3628 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
3630 struct wined3d_unordered_access_view
*view
;
3631 struct wined3d_texture
*texture
;
3632 struct wined3d_buffer
*buffer
;
3635 context
->uses_uavs
= 0;
3640 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
3642 if (!(view
= views
[i
]))
3645 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3647 buffer
= buffer_from_resource(view
->resource
);
3648 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
3649 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_BUFFER
);
3653 texture
= texture_from_resource(view
->resource
);
3654 wined3d_texture_load(texture
, context
, FALSE
);
3655 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_TEXTURE_RGB
);
3658 context
->uses_uavs
= 1;
3662 static void context_bind_unordered_access_views(struct wined3d_context
*context
,
3663 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
3665 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3666 struct wined3d_unordered_access_view
*view
;
3667 GLuint texture_name
;
3674 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
3676 if (!(view
= views
[i
]))
3678 if (shader
->reg_maps
.uav_resource_info
[i
].type
)
3679 WARN("No unordered access view bound at index %u.\n", i
);
3680 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
3684 if (view
->gl_view
.name
)
3686 texture_name
= view
->gl_view
.name
;
3689 else if (view
->resource
->type
!= WINED3D_RTYPE_BUFFER
)
3691 struct wined3d_texture
*texture
= texture_from_resource(view
->resource
);
3692 texture_name
= wined3d_texture_get_texture_name(texture
, context
, FALSE
);
3693 level
= view
->desc
.u
.texture
.level_idx
;
3697 FIXME("Unsupported buffer unordered access view.\n");
3698 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
3702 GL_EXTCALL(glBindImageTexture(i
, texture_name
, level
, GL_TRUE
, 0, GL_READ_WRITE
,
3703 view
->format
->glInternal
));
3705 if (view
->counter_bo
)
3706 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER
, i
, view
->counter_bo
));
3708 checkGLcall("Bind unordered access views");
3711 static void context_load_stream_output_buffers(struct wined3d_context
*context
,
3712 const struct wined3d_state
*state
)
3716 for (i
= 0; i
< ARRAY_SIZE(state
->stream_output
); ++i
)
3718 struct wined3d_buffer
*buffer
;
3719 if (!(buffer
= state
->stream_output
[i
].buffer
))
3722 wined3d_buffer_load(buffer
, context
, state
);
3723 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
3727 /* Context activation is done by the caller. */
3728 BOOL
context_apply_draw_state(struct wined3d_context
*context
,
3729 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
3731 const struct StateEntry
*state_table
= context
->state_table
;
3732 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3733 const struct wined3d_fb_state
*fb
= state
->fb
;
3737 if (!context_validate_rt_config(gl_info
->limits
.buffers
, fb
->render_targets
, fb
->depth_stencil
))
3740 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3742 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3745 /* Preload resources before FBO setup. Texture preload in particular may
3746 * result in changes to the current FBO, due to using e.g. FBO blits for
3747 * updating a resource location. */
3748 context_update_tex_unit_map(context
, state
);
3749 context_preload_textures(context
, state
);
3750 context_load_shader_resources(context
, state
, ~(1u << WINED3D_SHADER_TYPE_COMPUTE
));
3751 context_load_unordered_access_resources(context
, state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
3752 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
3753 context_load_stream_output_buffers(context
, state
);
3754 /* TODO: Right now the dependency on the vertex shader is necessary
3755 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3756 * the current VS but maybe it's possible to relax the coupling in some
3757 * situations at least. */
3758 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
3759 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
3761 context_update_stream_info(context
, state
);
3765 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3768 wined3d_buffer_load(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
,
3771 /* Loading the buffers above may have invalidated the stream info. */
3772 if (isStateDirty(context
, STATE_STREAMSRC
))
3773 context_update_stream_info(context
, state
);
3775 if (state
->index_buffer
)
3777 if (context
->stream_info
.all_vbo
)
3778 wined3d_buffer_load(state
->index_buffer
, context
, state
);
3780 wined3d_buffer_load_sysmem(state
->index_buffer
, context
);
3783 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3785 DWORD rep
= context
->dirtyArray
[i
];
3786 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3787 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3788 context
->isStateDirty
[idx
] &= ~(1u << shift
);
3789 state_table
[rep
].apply(context
, state
, rep
);
3792 if (context
->shader_update_mask
& ~(1u << WINED3D_SHADER_TYPE_COMPUTE
))
3794 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3795 context
->shader_update_mask
&= 1u << WINED3D_SHADER_TYPE_COMPUTE
;
3798 if (context
->constant_update_mask
)
3800 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3801 context
->constant_update_mask
= 0;
3804 if (context
->update_shader_resource_bindings
)
3806 for (i
= 0; i
< WINED3D_SHADER_TYPE_GRAPHICS_COUNT
; ++i
)
3807 context_bind_shader_resources(context
, state
, i
);
3808 context
->update_shader_resource_bindings
= 0;
3809 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
3810 context
->update_compute_shader_resource_bindings
= 1;
3813 if (context
->update_unordered_access_view_bindings
)
3815 context_bind_unordered_access_views(context
,
3816 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
3817 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
3818 context
->update_unordered_access_view_bindings
= 0;
3819 context
->update_compute_unordered_access_view_bindings
= 1;
3822 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3824 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3827 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3828 context
->last_was_blit
= FALSE
;
3833 void context_apply_compute_state(struct wined3d_context
*context
,
3834 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
3836 const struct StateEntry
*state_table
= context
->state_table
;
3837 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3838 unsigned int state_id
, i
, j
;
3840 context_load_shader_resources(context
, state
, 1u << WINED3D_SHADER_TYPE_COMPUTE
);
3841 context_load_unordered_access_resources(context
, state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
3842 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
3844 for (i
= 0, state_id
= STATE_COMPUTE_OFFSET
; i
< ARRAY_SIZE(context
->dirty_compute_states
); ++i
)
3846 for (j
= 0; j
< sizeof(*context
->dirty_compute_states
) * CHAR_BIT
; ++j
, ++state_id
)
3848 if (context
->dirty_compute_states
[i
] & (1u << j
))
3849 state_table
[state_id
].apply(context
, state
, state_id
);
3852 memset(context
->dirty_compute_states
, 0, sizeof(*context
->dirty_compute_states
));
3854 if (context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_COMPUTE
))
3856 device
->shader_backend
->shader_select_compute(device
->shader_priv
, context
, state
);
3857 context
->shader_update_mask
&= ~(1u << WINED3D_SHADER_TYPE_COMPUTE
);
3860 if (context
->update_compute_shader_resource_bindings
)
3862 context_bind_shader_resources(context
, state
, WINED3D_SHADER_TYPE_COMPUTE
);
3863 context
->update_compute_shader_resource_bindings
= 0;
3864 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
3865 context
->update_shader_resource_bindings
= 1;
3868 if (context
->update_compute_unordered_access_view_bindings
)
3870 context_bind_unordered_access_views(context
,
3871 state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
3872 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
3873 context
->update_compute_unordered_access_view_bindings
= 0;
3874 context
->update_unordered_access_view_bindings
= 1;
3877 context
->last_was_blit
= FALSE
;
3880 void context_end_transform_feedback(struct wined3d_context
*context
)
3882 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3883 if (context
->transform_feedback_active
)
3885 GL_EXTCALL(glEndTransformFeedback());
3886 checkGLcall("glEndTransformFeedback");
3887 context
->transform_feedback_active
= 0;
3888 context
->transform_feedback_paused
= 0;
3892 static void context_setup_target(struct wined3d_context
*context
,
3893 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
3895 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3897 render_offscreen
= wined3d_resource_is_offscreen(&texture
->resource
);
3898 if (context
->current_rt
.texture
== texture
3899 && context
->current_rt
.sub_resource_idx
== sub_resource_idx
3900 && render_offscreen
== old_render_offscreen
)
3903 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3904 * the alpha blend state changes with different render target formats. */
3905 if (!context
->current_rt
.texture
)
3907 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3911 const struct wined3d_format
*old
= context
->current_rt
.texture
->resource
.format
;
3912 const struct wined3d_format
*new = texture
->resource
.format
;
3914 if (old
->id
!= new->id
)
3916 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3917 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3918 || !(texture
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3919 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3921 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3922 if ((context
->current_rt
.texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
3923 != (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3924 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3927 /* When switching away from an offscreen render target, and we're not
3928 * using FBOs, we have to read the drawable into the texture. This is
3929 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
3930 * There are some things that need care though. PreLoad needs a GL context,
3931 * and FindContext is called before the context is activated. It also
3932 * has to be called with the old rendertarget active, otherwise a
3933 * wrong drawable is read. */
3934 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
3935 && old_render_offscreen
&& (context
->current_rt
.texture
!= texture
3936 || context
->current_rt
.sub_resource_idx
!= sub_resource_idx
))
3938 unsigned int prev_sub_resource_idx
= context
->current_rt
.sub_resource_idx
;
3939 struct wined3d_texture
*prev_texture
= context
->current_rt
.texture
;
3941 /* Read the back buffer of the old drawable into the destination texture. */
3942 if (prev_texture
->texture_srgb
.name
)
3943 wined3d_texture_load(prev_texture
, context
, TRUE
);
3944 wined3d_texture_load(prev_texture
, context
, FALSE
);
3945 wined3d_texture_invalidate_location(prev_texture
, prev_sub_resource_idx
, WINED3D_LOCATION_DRAWABLE
);
3949 context
->current_rt
.texture
= texture
;
3950 context
->current_rt
.sub_resource_idx
= sub_resource_idx
;
3951 context_set_render_offscreen(context
, render_offscreen
);
3954 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
,
3955 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
3957 struct wined3d_context
*current_context
= context_get_current();
3958 struct wined3d_context
*context
;
3959 BOOL swapchain_texture
;
3961 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device
, texture
, sub_resource_idx
);
3963 wined3d_from_cs(device
->cs
);
3965 if (current_context
&& current_context
->destroyed
)
3966 current_context
= NULL
;
3968 swapchain_texture
= texture
&& texture
->swapchain
;
3972 && current_context
->current_rt
.texture
3973 && current_context
->device
== device
)
3975 texture
= current_context
->current_rt
.texture
;
3976 sub_resource_idx
= current_context
->current_rt
.sub_resource_idx
;
3980 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
3982 if (swapchain
->back_buffers
)
3983 texture
= swapchain
->back_buffers
[0];
3985 texture
= swapchain
->front_buffer
;
3986 sub_resource_idx
= 0;
3990 if (current_context
&& current_context
->current_rt
.texture
== texture
)
3992 context
= current_context
;
3994 else if (swapchain_texture
)
3996 TRACE("Rendering onscreen.\n");
3998 context
= swapchain_get_context(texture
->swapchain
);
4002 TRACE("Rendering offscreen.\n");
4004 /* Stay with the current context if possible. Otherwise use the
4005 * context for the primary swapchain. */
4006 if (current_context
&& current_context
->device
== device
)
4007 context
= current_context
;
4009 context
= swapchain_get_context(device
->swapchains
[0]);
4012 context_enter(context
);
4013 context_update_window(context
);
4014 context_setup_target(context
, texture
, sub_resource_idx
);
4015 if (!context
->valid
)
4018 if (context
!= current_context
)
4020 if (!context_set_current(context
))
4021 ERR("Failed to activate the new context.\n");
4023 else if (context
->needs_set
)
4025 context_set_gl_context(context
);
4031 struct wined3d_context
*context_reacquire(const struct wined3d_device
*device
,
4032 struct wined3d_context
*context
)
4034 struct wined3d_context
*current_context
;
4036 if (!context
|| context
->tid
!= GetCurrentThreadId())
4039 current_context
= context_acquire(device
, context
->current_rt
.texture
,
4040 context
->current_rt
.sub_resource_idx
);
4041 if (current_context
!= context
)
4042 ERR("Acquired context %p instead of %p.\n", current_context
, context
);
4043 return current_context
;