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
38 static DWORD wined3d_context_tls_idx
;
40 /* FBO helper functions */
42 /* Context activation is done by the caller. */
43 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint fbo
)
45 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
49 case GL_READ_FRAMEBUFFER
:
50 if (context
->fbo_read_binding
== fbo
) return;
51 context
->fbo_read_binding
= fbo
;
54 case GL_DRAW_FRAMEBUFFER
:
55 if (context
->fbo_draw_binding
== fbo
) return;
56 context
->fbo_draw_binding
= fbo
;
60 if (context
->fbo_read_binding
== fbo
61 && context
->fbo_draw_binding
== fbo
) return;
62 context
->fbo_read_binding
= fbo
;
63 context
->fbo_draw_binding
= fbo
;
67 FIXME("Unhandled target %#x.\n", target
);
71 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
72 checkGLcall("glBindFramebuffer()");
75 /* Context activation is done by the caller. */
76 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
80 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
82 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
83 checkGLcall("glFramebufferTexture2D()");
85 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
86 checkGLcall("glFramebufferTexture2D()");
88 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
89 checkGLcall("glFramebufferTexture2D()");
92 /* Context activation is done by the caller. */
93 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint fbo
)
95 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
97 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
98 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
99 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
101 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
102 checkGLcall("glDeleteFramebuffers()");
105 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
106 GLenum fbo_target
, DWORD flags
, GLuint rb
)
108 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
110 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
111 checkGLcall("glFramebufferRenderbuffer()");
114 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
116 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
117 checkGLcall("glFramebufferRenderbuffer()");
121 /* Context activation is done by the caller. */
122 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
123 GLenum fbo_target
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
,
126 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
128 if (resource
->object
)
130 TRACE("Attach depth stencil %u.\n", resource
->object
);
134 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
135 flags
, resource
->object
);
139 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
141 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
142 resource
->target
, resource
->object
, resource
->level
);
143 checkGLcall("glFramebufferTexture2D()");
146 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
148 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
149 resource
->target
, resource
->object
, resource
->level
);
150 checkGLcall("glFramebufferTexture2D()");
154 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
))
156 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
157 checkGLcall("glFramebufferTexture2D()");
160 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
))
162 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
163 checkGLcall("glFramebufferTexture2D()");
168 TRACE("Attach depth stencil 0.\n");
170 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
171 checkGLcall("glFramebufferTexture2D()");
173 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
174 checkGLcall("glFramebufferTexture2D()");
178 /* Context activation is done by the caller. */
179 static void context_attach_surface_fbo(struct wined3d_context
*context
,
180 GLenum fbo_target
, DWORD idx
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
)
182 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
184 TRACE("Attach GL object %u to %u.\n", resource
->object
, idx
);
186 if (resource
->object
)
191 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
192 GL_RENDERBUFFER
, resource
->object
);
193 checkGLcall("glFramebufferRenderbuffer()");
197 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
198 resource
->target
, resource
->object
, resource
->level
);
199 checkGLcall("glFramebufferTexture2D()");
204 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
205 checkGLcall("glFramebufferTexture2D()");
209 static void context_dump_fbo_attachment(const struct wined3d_gl_info
*gl_info
, GLenum target
,
212 GLint type
, name
, samples
, width
, height
, old_texture
, level
, face
, fmt
, tex_target
;
214 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
215 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
, &name
);
216 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
217 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
, &type
);
219 if (type
== GL_RENDERBUFFER
)
221 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, name
);
222 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_WIDTH
, &width
);
223 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_HEIGHT
, &height
);
224 if (gl_info
->limits
.samples
> 1)
225 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_SAMPLES
, &samples
);
228 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_INTERNAL_FORMAT
, &fmt
);
229 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
230 debug_fboattachment(attachment
), name
, width
, height
, samples
, fmt
);
232 else if (type
== GL_TEXTURE
)
234 const char *tex_type_str
;
236 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
237 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
, &level
);
238 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
239 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
, &face
);
243 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP
, &old_texture
);
245 glBindTexture(GL_TEXTURE_CUBE_MAP
, name
);
246 glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
247 glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_WIDTH
, &width
);
248 glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_HEIGHT
, &height
);
250 tex_target
= GL_TEXTURE_CUBE_MAP
;
251 tex_type_str
= "cube";
255 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_texture
);
256 while (gl_info
->gl_ops
.gl
.p_glGetError());
258 glBindTexture(GL_TEXTURE_2D
, name
);
259 if (!gl_info
->gl_ops
.gl
.p_glGetError())
261 tex_target
= GL_TEXTURE_2D
;
266 glBindTexture(GL_TEXTURE_2D
, old_texture
);
267 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_texture
);
269 glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, name
);
270 if (gl_info
->gl_ops
.gl
.p_glGetError())
272 glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, old_texture
);
273 FIXME("Cannot find type of texture %d.\n", name
);
276 tex_target
= GL_TEXTURE_RECTANGLE_ARB
;
277 tex_type_str
= "rectangle";
280 glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
281 glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_WIDTH
, &width
);
282 glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_HEIGHT
, &height
);
285 FIXME(" %s: %s texture %d, %dx%d, format %#x.\n", debug_fboattachment(attachment
),
286 tex_type_str
, name
, width
, height
, fmt
);
288 glBindTexture(tex_target
, old_texture
);
290 else if (type
== GL_NONE
)
292 FIXME("\t%s: NONE.\n", debug_fboattachment(attachment
));
295 ERR("\t%s: Unknown attachment %#x.\n", debug_fboattachment(attachment
), type
);
298 /* Context activation is done by the caller. */
299 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
301 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
304 if (!FIXME_ON(d3d
)) return;
306 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
307 if (status
== GL_FRAMEBUFFER_COMPLETE
)
309 TRACE("FBO complete\n");
315 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
317 if (!context
->current_fbo
)
319 ERR("FBO 0 is incomplete, driver bug?\n");
323 context_dump_fbo_attachment(gl_info
, target
, GL_DEPTH_ATTACHMENT
);
324 context_dump_fbo_attachment(gl_info
, target
, GL_STENCIL_ATTACHMENT
);
326 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
327 context_dump_fbo_attachment(gl_info
, target
, GL_COLOR_ATTACHMENT0
+ i
);
328 checkGLcall("Dump FBO attachments");
332 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
334 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
335 return buffer
? (1u << 31) | buffer
: 0;
338 static inline DWORD
context_generate_rt_mask_from_resource(struct wined3d_resource
*resource
)
340 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
342 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
346 return (1u << 31) | wined3d_texture_get_gl_buffer(wined3d_texture_from_resource(resource
));
349 static inline void context_set_fbo_key_for_surface(const struct wined3d_context
*context
,
350 struct wined3d_fbo_entry_key
*key
, UINT idx
, struct wined3d_surface
*surface
,
355 key
->objects
[idx
].object
= 0;
356 key
->objects
[idx
].level
= key
->objects
[idx
].target
= 0;
358 else if (surface
->current_renderbuffer
)
360 key
->objects
[idx
].object
= surface
->current_renderbuffer
->id
;
361 key
->objects
[idx
].level
= key
->objects
[idx
].target
= 0;
362 key
->rb_namespace
|= 1 << idx
;
368 case WINED3D_LOCATION_TEXTURE_RGB
:
369 key
->objects
[idx
].object
= surface_get_texture_name(surface
, context
, FALSE
);
370 key
->objects
[idx
].level
= surface
->texture_level
;
371 key
->objects
[idx
].target
= surface
->texture_target
;
374 case WINED3D_LOCATION_TEXTURE_SRGB
:
375 key
->objects
[idx
].object
= surface_get_texture_name(surface
, context
, TRUE
);
376 key
->objects
[idx
].level
= surface
->texture_level
;
377 key
->objects
[idx
].target
= surface
->texture_target
;
380 case WINED3D_LOCATION_RB_MULTISAMPLE
:
381 key
->objects
[idx
].object
= surface
->rb_multisample
;
382 key
->objects
[idx
].level
= key
->objects
[idx
].target
= 0;
383 key
->rb_namespace
|= 1 << idx
;
386 case WINED3D_LOCATION_RB_RESOLVED
:
387 key
->objects
[idx
].object
= surface
->rb_resolved
;
388 key
->objects
[idx
].level
= key
->objects
[idx
].target
= 0;
389 key
->rb_namespace
|= 1 << idx
;
395 static void context_generate_fbo_key(const struct wined3d_context
*context
,
396 struct wined3d_fbo_entry_key
*key
, struct wined3d_surface
**render_targets
,
397 struct wined3d_surface
*depth_stencil
, DWORD color_location
,
402 key
->rb_namespace
= 0;
403 context_set_fbo_key_for_surface(context
, key
, 0, depth_stencil
, ds_location
);
405 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
406 context_set_fbo_key_for_surface(context
, key
, i
+ 1, render_targets
[i
], color_location
);
409 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
410 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
411 DWORD color_location
, DWORD ds_location
)
413 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
414 struct fbo_entry
*entry
;
415 UINT object_count
= gl_info
->limits
.buffers
+ 1;
417 entry
= HeapAlloc(GetProcessHeap(), 0,
418 FIELD_OFFSET(struct fbo_entry
, key
.objects
[object_count
]));
419 memset(&entry
->key
, 0, FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[object_count
]));
420 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
424 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
)
425 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
426 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_STENCIL
)
427 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
429 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
430 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
431 checkGLcall("glGenFramebuffers()");
432 TRACE("Created FBO %u.\n", entry
->id
);
437 /* Context activation is done by the caller. */
438 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
439 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
440 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
442 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
444 context_bind_fbo(context
, target
, entry
->id
);
445 context_clean_fbo_attachments(gl_info
, target
);
447 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
451 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
)
452 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
453 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_STENCIL
)
454 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
458 /* Context activation is done by the caller. */
459 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
463 TRACE("Destroy FBO %u.\n", entry
->id
);
464 context_destroy_fbo(context
, entry
->id
);
466 --context
->fbo_entry_count
;
467 list_remove(&entry
->entry
);
468 HeapFree(GetProcessHeap(), 0, entry
);
471 /* Context activation is done by the caller. */
472 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
473 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
474 DWORD color_location
, DWORD ds_location
)
476 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
477 struct fbo_entry
*entry
;
478 UINT object_count
= gl_info
->limits
.buffers
+ 1;
481 if (depth_stencil
&& render_targets
[0])
483 if (wined3d_texture_get_level_width(depth_stencil
->container
, depth_stencil
->texture_level
)
484 < wined3d_texture_get_level_width(render_targets
[0]->container
, render_targets
[0]->texture_level
)
485 || wined3d_texture_get_level_height(depth_stencil
->container
, depth_stencil
->texture_level
)
486 < wined3d_texture_get_level_height(render_targets
[0]->container
, render_targets
[0]->texture_level
))
488 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
489 depth_stencil
= NULL
;
491 else if (depth_stencil
->container
->resource
.multisample_type
492 != render_targets
[0]->container
->resource
.multisample_type
493 || depth_stencil
->container
->resource
.multisample_quality
494 != render_targets
[0]->container
->resource
.multisample_quality
)
496 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
497 render_targets
[0]->container
->resource
.multisample_type
,
498 render_targets
[0]->container
->resource
.multisample_quality
,
499 depth_stencil
->container
->resource
.multisample_type
,
500 depth_stencil
->container
->resource
.multisample_quality
);
501 depth_stencil
= NULL
;
504 surface_set_compatible_renderbuffer(depth_stencil
, render_targets
[0]);
507 context_generate_fbo_key(context
, context
->fbo_key
, render_targets
, depth_stencil
, color_location
,
512 TRACE("Dumping FBO attachments:\n");
513 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
515 if (render_targets
[i
])
517 TRACE(" Color attachment %u: (%p) %s gl obj %u(%s) %ux%u %u samples.\n",
518 i
, render_targets
[i
], debug_d3dformat(render_targets
[i
]->container
->resource
.format
->id
),
519 context
->fbo_key
->objects
[i
+ 1].object
,
520 context
->fbo_key
->rb_namespace
& (1 << (i
+ 1)) ? "rb" : "texure",
521 render_targets
[i
]->pow2Width
, render_targets
[i
]->pow2Height
,
522 render_targets
[i
]->container
->resource
.multisample_type
);
527 TRACE(" Depth attachment: (%p) %s gl obj %u(%s) %ux%u %u samples.\n",
528 depth_stencil
, debug_d3dformat(depth_stencil
->container
->resource
.format
->id
),
529 context
->fbo_key
->objects
[0].object
,
530 context
->fbo_key
->rb_namespace
& (1 << 0) ? "rb" : "texure",
531 depth_stencil
->pow2Width
, depth_stencil
->pow2Height
,
532 depth_stencil
->container
->resource
.multisample_type
);
536 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
538 if (memcmp(context
->fbo_key
, &entry
->key
, FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[object_count
])))
541 list_remove(&entry
->entry
);
542 list_add_head(&context
->fbo_list
, &entry
->entry
);
546 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
548 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, color_location
, ds_location
);
549 list_add_head(&context
->fbo_list
, &entry
->entry
);
550 ++context
->fbo_entry_count
;
554 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
555 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, color_location
, ds_location
, entry
);
556 list_remove(&entry
->entry
);
557 list_add_head(&context
->fbo_list
, &entry
->entry
);
563 /* Context activation is done by the caller. */
564 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
566 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
568 GLuint read_binding
, draw_binding
;
570 if (entry
->flags
& WINED3D_FBO_ENTRY_FLAG_ATTACHED
)
572 context_bind_fbo(context
, target
, entry
->id
);
576 read_binding
= context
->fbo_read_binding
;
577 draw_binding
= context
->fbo_draw_binding
;
578 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
580 /* Apply render targets */
581 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
583 context_attach_surface_fbo(context
, target
, i
, &entry
->key
.objects
[i
+ 1],
584 entry
->key
.rb_namespace
& (1 << (i
+ 1)));
587 context_attach_depth_stencil_fbo(context
, target
, &entry
->key
.objects
[0],
588 entry
->key
.rb_namespace
& 0x1, entry
->flags
);
590 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
591 * GL contexts requirements. */
592 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
593 context_set_draw_buffer(context
, GL_NONE
);
594 if (target
!= GL_FRAMEBUFFER
)
596 if (target
== GL_READ_FRAMEBUFFER
)
597 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
599 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
602 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_ATTACHED
;
605 /* Context activation is done by the caller. */
606 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
607 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
608 DWORD color_location
, DWORD ds_location
)
610 struct fbo_entry
*entry
, *entry2
;
612 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
614 context_destroy_fbo_entry(context
, entry
);
617 if (context
->rebind_fbo
)
619 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
620 context
->rebind_fbo
= FALSE
;
623 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
625 context
->current_fbo
= NULL
;
626 context_bind_fbo(context
, target
, 0);
630 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
,
631 color_location
, ds_location
);
632 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
636 /* Context activation is done by the caller. */
637 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
638 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
640 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
642 context
->blit_targets
[0] = render_target
;
644 memset(&context
->blit_targets
[1], 0, clear_size
);
645 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
, location
);
648 /* Context activation is done by the caller. */
649 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
651 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
653 if (context
->free_occlusion_query_count
)
655 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
659 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
661 GL_EXTCALL(glGenQueries(1, &query
->id
));
662 checkGLcall("glGenQueries");
664 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
668 WARN("Occlusion queries not supported, not allocating query id.\n");
673 query
->context
= context
;
674 list_add_head(&context
->occlusion_queries
, &query
->entry
);
677 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
679 struct wined3d_context
*context
= query
->context
;
681 list_remove(&query
->entry
);
682 query
->context
= NULL
;
684 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
686 UINT new_size
= context
->free_occlusion_query_size
<< 1;
687 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
688 new_size
* sizeof(*context
->free_occlusion_queries
));
692 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
696 context
->free_occlusion_query_size
= new_size
;
697 context
->free_occlusion_queries
= new_data
;
700 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
703 /* Context activation is done by the caller. */
704 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
706 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
708 if (context
->free_event_query_count
)
710 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
714 if (gl_info
->supported
[ARB_SYNC
])
716 /* Using ARB_sync, not much to do here. */
717 query
->object
.sync
= NULL
;
718 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
720 else if (gl_info
->supported
[APPLE_FENCE
])
722 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
723 checkGLcall("glGenFencesAPPLE");
725 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
727 else if(gl_info
->supported
[NV_FENCE
])
729 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
730 checkGLcall("glGenFencesNV");
732 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
736 WARN("Event queries not supported, not allocating query id.\n");
737 query
->object
.id
= 0;
741 query
->context
= context
;
742 list_add_head(&context
->event_queries
, &query
->entry
);
745 void context_free_event_query(struct wined3d_event_query
*query
)
747 struct wined3d_context
*context
= query
->context
;
749 list_remove(&query
->entry
);
750 query
->context
= NULL
;
752 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
754 UINT new_size
= context
->free_event_query_size
<< 1;
755 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
756 new_size
* sizeof(*context
->free_event_queries
));
760 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
764 context
->free_event_query_size
= new_size
;
765 context
->free_event_queries
= new_data
;
768 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
771 /* Context activation is done by the caller. */
772 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
774 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
776 if (context
->free_timestamp_query_count
)
778 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
782 GL_EXTCALL(glGenQueries(1, &query
->id
));
783 checkGLcall("glGenQueries");
785 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
788 query
->context
= context
;
789 list_add_head(&context
->timestamp_queries
, &query
->entry
);
792 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
794 struct wined3d_context
*context
= query
->context
;
796 list_remove(&query
->entry
);
797 query
->context
= NULL
;
799 if (context
->free_timestamp_query_count
>= context
->free_timestamp_query_size
- 1)
801 UINT new_size
= context
->free_timestamp_query_size
<< 1;
802 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_timestamp_queries
,
803 new_size
* sizeof(*context
->free_timestamp_queries
));
807 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
811 context
->free_timestamp_query_size
= new_size
;
812 context
->free_timestamp_queries
= new_data
;
815 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
818 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
820 static void context_enum_fbo_entries(const struct wined3d_device
*device
,
821 GLuint name
, BOOL rb_namespace
, context_fbo_entry_func_t
*callback
)
825 for (i
= 0; i
< device
->context_count
; ++i
)
827 struct wined3d_context
*context
= device
->contexts
[i
];
828 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
829 struct fbo_entry
*entry
, *entry2
;
831 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
835 for (j
= 0; j
< gl_info
->limits
.buffers
+ 1; ++j
)
837 if (entry
->key
.objects
[j
].object
== name
838 && !(entry
->key
.rb_namespace
& (1 << j
)) == !rb_namespace
)
840 callback(context
, entry
);
848 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
850 list_remove(&entry
->entry
);
851 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
854 void context_resource_released(const struct wined3d_device
*device
,
855 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
857 struct wined3d_texture
*texture
;
860 if (!device
->d3d_initialized
)
865 case WINED3D_RTYPE_TEXTURE_2D
:
866 case WINED3D_RTYPE_TEXTURE_3D
:
867 texture
= wined3d_texture_from_resource(resource
);
869 for (i
= 0; i
< device
->context_count
; ++i
)
871 struct wined3d_context
*context
= device
->contexts
[i
];
872 if (context
->current_rt
.texture
== texture
)
874 context
->current_rt
.texture
= NULL
;
875 context
->current_rt
.sub_resource_idx
= 0;
885 void context_gl_resource_released(struct wined3d_device
*device
,
886 GLuint name
, BOOL rb_namespace
)
888 context_enum_fbo_entries(device
, name
, rb_namespace
, context_queue_fbo_entry_destruction
);
891 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
893 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
894 struct fbo_entry
*entry
= context
->current_fbo
;
897 if (!entry
|| context
->rebind_fbo
) return;
899 for (i
= 0; i
< gl_info
->limits
.buffers
+ 1; ++i
)
901 if (surface
->container
->texture_rgb
.name
== entry
->key
.objects
[i
].object
902 || surface
->container
->texture_srgb
.name
== entry
->key
.objects
[i
].object
)
904 TRACE("Updated surface %p is bound as attachment %u to the current FBO.\n", surface
, i
);
905 context
->rebind_fbo
= TRUE
;
911 static BOOL
context_restore_pixel_format(struct wined3d_context
*ctx
)
913 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
916 if (ctx
->restore_pf
&& IsWindow(ctx
->restore_pf_win
))
918 if (ctx
->gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
920 HDC dc
= GetDCEx(ctx
->restore_pf_win
, 0, DCX_USESTYLE
| DCX_CACHE
);
923 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, ctx
->restore_pf
))))
925 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
926 ctx
->restore_pf
, ctx
->restore_pf_win
);
928 ReleaseDC(ctx
->restore_pf_win
, dc
);
933 ERR("can't restore pixel format %d on window %p\n", ctx
->restore_pf
, ctx
->restore_pf_win
);
938 ctx
->restore_pf_win
= NULL
;
942 static BOOL
context_set_pixel_format(struct wined3d_context
*context
, HDC dc
, BOOL
private, int format
)
944 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
947 if (dc
== context
->hdc
&& context
->hdc_is_private
&& context
->hdc_has_format
)
950 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
951 if (current
== format
) goto success
;
955 if (!SetPixelFormat(dc
, format
, NULL
))
957 /* This may also happen if the dc belongs to a destroyed window. */
958 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
959 format
, dc
, GetLastError());
963 context
->restore_pf
= 0;
964 context
->restore_pf_win
= private ? NULL
: WindowFromDC(dc
);
968 /* By default WGL doesn't allow pixel format adjustments but we need it
969 * here. For this reason there's a Wine specific wglSetPixelFormat()
970 * which allows us to set the pixel format multiple times. Only use it
971 * when really needed. */
972 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
976 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
978 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
983 win
= private ? NULL
: WindowFromDC(dc
);
984 if (win
!= context
->restore_pf_win
)
986 context_restore_pixel_format(context
);
988 context
->restore_pf
= private ? 0 : current
;
989 context
->restore_pf_win
= win
;
995 /* OpenGL doesn't allow pixel format adjustments. Print an error and
996 * continue using the old format. There's a big chance that the old
997 * format works although with a performance hit and perhaps rendering
999 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1000 format
, dc
, current
);
1004 if (dc
== context
->hdc
&& context
->hdc_is_private
)
1005 context
->hdc_has_format
= TRUE
;
1009 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
1011 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
1012 BOOL backup
= FALSE
;
1014 if (!context_set_pixel_format(ctx
, ctx
->hdc
, ctx
->hdc_is_private
, ctx
->pixel_format
))
1016 WARN("Failed to set pixel format %d on device context %p.\n",
1017 ctx
->pixel_format
, ctx
->hdc
);
1021 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
1025 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1026 ctx
->glCtx
, ctx
->hdc
, GetLastError());
1028 WARN("Trying fallback to the backup window.\n");
1030 /* FIXME: If the context is destroyed it's no longer associated with
1031 * a swapchain, so we can't use the swapchain to get a backup dc. To
1032 * make this work windowless contexts would need to be handled by the
1036 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
1037 context_set_current(NULL
);
1041 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
1043 context_set_current(NULL
);
1047 if (!context_set_pixel_format(ctx
, dc
, TRUE
, ctx
->pixel_format
))
1049 ERR("Failed to set pixel format %d on device context %p.\n",
1050 ctx
->pixel_format
, dc
);
1051 context_set_current(NULL
);
1055 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
1057 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1058 dc
, GetLastError());
1059 context_set_current(NULL
);
1069 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
1071 if (!wglMakeCurrent(dc
, gl_ctx
))
1073 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1074 gl_ctx
, dc
, GetLastError());
1075 context_set_current(NULL
);
1079 static void context_update_window(struct wined3d_context
*context
)
1081 if (context
->win_handle
== context
->swapchain
->win_handle
)
1084 TRACE("Updating context %p window from %p to %p.\n",
1085 context
, context
->win_handle
, context
->swapchain
->win_handle
);
1088 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1090 context
->win_handle
= context
->swapchain
->win_handle
;
1091 context
->hdc_is_private
= FALSE
;
1092 context
->hdc_has_format
= FALSE
;
1093 context
->needs_set
= 1;
1096 if (!(context
->hdc
= GetDCEx(context
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1098 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
1103 static void context_destroy_gl_resources(struct wined3d_context
*context
)
1105 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1106 struct wined3d_timestamp_query
*timestamp_query
;
1107 struct wined3d_occlusion_query
*occlusion_query
;
1108 struct wined3d_event_query
*event_query
;
1109 struct fbo_entry
*entry
, *entry2
;
1114 restore_ctx
= wglGetCurrentContext();
1115 restore_dc
= wglGetCurrentDC();
1117 if (restore_ctx
== context
->glCtx
)
1119 else if (context
->valid
)
1120 context_set_gl_context(context
);
1122 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
1125 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
1126 timestamp_query
->context
= NULL
;
1129 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1131 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1132 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1133 occlusion_query
->context
= NULL
;
1136 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
1140 if (gl_info
->supported
[ARB_SYNC
])
1142 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
1144 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
1145 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
1147 event_query
->context
= NULL
;
1150 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
1152 if (!context
->valid
) entry
->id
= 0;
1153 context_destroy_fbo_entry(context
, entry
);
1156 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1158 if (!context
->valid
) entry
->id
= 0;
1159 context_destroy_fbo_entry(context
, entry
);
1164 if (context
->dummy_arbfp_prog
)
1166 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
1169 if (gl_info
->supported
[ARB_TIMER_QUERY
])
1170 GL_EXTCALL(glDeleteQueries(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
1172 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1173 GL_EXTCALL(glDeleteQueries(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
1175 if (gl_info
->supported
[ARB_SYNC
])
1177 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1179 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
1182 else if (gl_info
->supported
[APPLE_FENCE
])
1184 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1186 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1189 else if (gl_info
->supported
[NV_FENCE
])
1191 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1193 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1197 checkGLcall("context cleanup");
1200 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1201 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1202 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1204 context_restore_pixel_format(context
);
1207 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1209 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1211 ERR("Failed to disable GL context.\n");
1214 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1216 if (!wglDeleteContext(context
->glCtx
))
1218 DWORD err
= GetLastError();
1219 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1223 DWORD
context_get_tls_idx(void)
1225 return wined3d_context_tls_idx
;
1228 void context_set_tls_idx(DWORD idx
)
1230 wined3d_context_tls_idx
= idx
;
1233 struct wined3d_context
*context_get_current(void)
1235 return TlsGetValue(wined3d_context_tls_idx
);
1238 BOOL
context_set_current(struct wined3d_context
*ctx
)
1240 struct wined3d_context
*old
= context_get_current();
1244 TRACE("Already using D3D context %p.\n", ctx
);
1252 TRACE("Switching away from destroyed context %p.\n", old
);
1253 context_destroy_gl_resources(old
);
1254 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1255 HeapFree(GetProcessHeap(), 0, old
);
1259 if (wglGetCurrentContext())
1261 TRACE("Flushing context %p before switching to %p.\n", old
, ctx
);
1272 ERR("Trying to make invalid context %p current\n", ctx
);
1276 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1277 if (!context_set_gl_context(ctx
))
1281 else if(wglGetCurrentContext())
1283 TRACE("Clearing current D3D context.\n");
1284 if (!wglMakeCurrent(NULL
, NULL
))
1286 DWORD err
= GetLastError();
1287 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1288 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1293 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1296 void context_release(struct wined3d_context
*context
)
1298 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1302 if (!context
->level
)
1303 WARN("Context %p is not active.\n", context
);
1304 else if (context
!= context_get_current())
1305 WARN("Context %p is not the current context.\n", context
);
1308 if (!--context
->level
)
1310 if (context_restore_pixel_format(context
))
1311 context
->needs_set
= 1;
1312 if (context
->restore_ctx
)
1314 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1315 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
);
1316 context
->restore_ctx
= NULL
;
1317 context
->restore_dc
= NULL
;
1322 /* This is used when a context for render target A is active, but a separate context is
1323 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1324 * A to avoid breaking caller code. */
1325 void context_restore(struct wined3d_context
*context
, struct wined3d_surface
*restore
)
1327 if (context
->current_rt
.texture
!= restore
->container
1328 || context
->current_rt
.sub_resource_idx
!= surface_get_sub_resource_idx(restore
))
1330 context_release(context
);
1331 context
= context_acquire(restore
->container
->resource
.device
, restore
);
1334 context_release(context
);
1337 static void context_enter(struct wined3d_context
*context
)
1339 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1341 if (!context
->level
++)
1343 const struct wined3d_context
*current_context
= context_get_current();
1344 HGLRC current_gl
= wglGetCurrentContext();
1346 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1348 TRACE("Another GL context (%p on device context %p) is already current.\n",
1349 current_gl
, wglGetCurrentDC());
1350 context
->restore_ctx
= current_gl
;
1351 context
->restore_dc
= wglGetCurrentDC();
1352 context
->needs_set
= 1;
1354 else if (!context
->needs_set
&& !(context
->hdc_is_private
&& context
->hdc_has_format
)
1355 && context
->pixel_format
!= context
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context
->hdc
))
1356 context
->needs_set
= 1;
1360 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1362 DWORD rep
= context
->state_table
[state
].representative
;
1366 if (isStateDirty(context
, rep
)) return;
1368 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1369 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1370 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1371 context
->isStateDirty
[idx
] |= (1u << shift
);
1374 /* This function takes care of wined3d pixel format selection. */
1375 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1376 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1377 BOOL auxBuffers
, BOOL findCompatible
)
1380 unsigned int current_value
;
1381 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1384 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1385 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1386 auxBuffers
, findCompatible
);
1389 for (i
= 0; i
< cfg_count
; ++i
)
1391 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1394 /* For now only accept RGBA formats. Perhaps some day we will
1395 * allow floating point formats for pbuffers. */
1396 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1398 /* In window mode we need a window drawable format and double buffering. */
1399 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1401 if (cfg
->redSize
< color_format
->red_size
)
1403 if (cfg
->greenSize
< color_format
->green_size
)
1405 if (cfg
->blueSize
< color_format
->blue_size
)
1407 if (cfg
->alphaSize
< color_format
->alpha_size
)
1409 if (cfg
->depthSize
< ds_format
->depth_size
)
1411 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1413 /* Check multisampling support. */
1414 if (cfg
->numSamples
)
1418 /* We try to locate a format which matches our requirements exactly. In case of
1419 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1420 if (cfg
->depthSize
== ds_format
->depth_size
)
1422 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1424 if (cfg
->alphaSize
== color_format
->alpha_size
)
1426 /* We like to have aux buffers in backbuffer mode */
1427 if (auxBuffers
&& cfg
->auxBuffers
)
1429 if (cfg
->redSize
== color_format
->red_size
1430 && cfg
->greenSize
== color_format
->green_size
1431 && cfg
->blueSize
== color_format
->blue_size
)
1434 if (value
> current_value
)
1436 iPixelFormat
= cfg
->iPixelFormat
;
1437 current_value
= value
;
1441 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1442 if(!iPixelFormat
&& !findCompatible
) {
1443 ERR("Can't find a suitable iPixelFormat\n");
1445 } else if(!iPixelFormat
) {
1446 PIXELFORMATDESCRIPTOR pfd
;
1448 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1449 /* PixelFormat selection */
1450 ZeroMemory(&pfd
, sizeof(pfd
));
1451 pfd
.nSize
= sizeof(pfd
);
1453 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1454 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1455 pfd
.cAlphaBits
= color_format
->alpha_size
;
1456 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1457 + color_format
->blue_size
+ color_format
->alpha_size
;
1458 pfd
.cDepthBits
= ds_format
->depth_size
;
1459 pfd
.cStencilBits
= ds_format
->stencil_size
;
1460 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1462 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1464 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1465 ERR("Can't find a suitable iPixelFormat\n");
1470 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1471 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1472 return iPixelFormat
;
1475 /* Context activation is done by the caller. */
1476 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1478 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1479 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1481 for (i
= 0; i
< count
; ++i
)
1483 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1484 checkGLcall("glActiveTexture");
1486 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1487 checkGLcall("glBindTexture");
1489 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1491 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1492 checkGLcall("glBindTexture");
1495 if (gl_info
->supported
[EXT_TEXTURE3D
])
1497 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1498 checkGLcall("glBindTexture");
1501 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1503 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1504 checkGLcall("glBindTexture");
1509 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1511 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1512 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1515 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1516 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1520 case GL_DEBUG_TYPE_ERROR_ARB
:
1521 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1524 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1525 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1526 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1527 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1530 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1531 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1535 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1540 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1543 unsigned int ctx_attrib_idx
= 0;
1544 GLint ctx_attribs
[7], ctx_flags
= 0;
1546 if (context_debug_output_enabled(gl_info
))
1547 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1548 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1549 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1550 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1551 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1552 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1553 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1556 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1557 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1559 ctx_attribs
[ctx_attrib_idx
] = 0;
1561 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1563 if (ctx_flags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
1565 ctx_attribs
[ctx_attrib_idx
- 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1566 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1567 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1574 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1575 struct wined3d_texture
*target
, const struct wined3d_format
*ds_format
)
1577 struct wined3d_device
*device
= swapchain
->device
;
1578 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1579 const struct wined3d_format
*color_format
;
1580 struct wined3d_context
*ret
;
1581 BOOL auxBuffers
= FALSE
;
1582 HGLRC ctx
, share_ctx
;
1588 BOOL hdc_is_private
= FALSE
;
1590 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1592 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1596 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1597 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1598 if (!ret
->blit_targets
)
1601 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1602 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1603 if (!ret
->draw_buffers
)
1606 ret
->fbo_key
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1607 FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[gl_info
->limits
.buffers
+ 1]));
1611 ret
->free_timestamp_query_size
= 4;
1612 ret
->free_timestamp_queries
= HeapAlloc(GetProcessHeap(), 0,
1613 ret
->free_timestamp_query_size
* sizeof(*ret
->free_timestamp_queries
));
1614 if (!ret
->free_timestamp_queries
)
1616 list_init(&ret
->timestamp_queries
);
1618 ret
->free_occlusion_query_size
= 4;
1619 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1620 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1621 if (!ret
->free_occlusion_queries
)
1624 list_init(&ret
->occlusion_queries
);
1626 ret
->free_event_query_size
= 4;
1627 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1628 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1629 if (!ret
->free_event_queries
)
1632 list_init(&ret
->event_queries
);
1633 list_init(&ret
->fbo_list
);
1634 list_init(&ret
->fbo_destroy_list
);
1636 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1638 ERR("Failed to allocate shader backend context data.\n");
1641 if (!device
->adapter
->fragment_pipe
->allocate_context_data(ret
))
1643 ERR("Failed to allocate fragment pipeline context data.\n");
1647 /* Initialize the texture unit mapping to a 1:1 mapping */
1648 for (s
= 0; s
< MAX_COMBINED_SAMPLERS
; ++s
)
1650 if (s
< gl_info
->limits
.combined_samplers
)
1652 ret
->tex_unit_map
[s
] = s
;
1653 ret
->rev_tex_unit_map
[s
] = s
;
1657 ret
->tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1658 ret
->rev_tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1662 if (!(hdc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1664 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1666 if ((hdc
= swapchain_get_backup_dc(swapchain
)))
1667 hdc_is_private
= TRUE
;
1670 ERR("Failed to retrieve a device context.\n");
1675 color_format
= target
->resource
.format
;
1677 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1678 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1679 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1683 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1684 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1685 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1686 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1689 /* DirectDraw supports 8bit paletted render targets and these are used by
1690 * old games like StarCraft and C&C. Most modern hardware doesn't support
1691 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1692 * conversion (ab)uses the alpha component for storing the palette index.
1693 * For this reason we require a format with 8bit alpha, so request
1695 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1696 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1698 /* When "always_offscreen" is enabled, we only use the drawable for
1699 * presentation blits, and don't do any rendering to it. That means we
1700 * don't need depth or stencil buffers, and can mostly ignore the render
1701 * target format. This wouldn't necessarily be quite correct for 10bpc
1702 * display modes, but we don't currently support those.
1703 * Using the same format regardless of the color/depth/stencil targets
1704 * makes it much less likely that different wined3d instances will set
1705 * conflicting pixel formats. */
1706 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
1707 && wined3d_settings
.always_offscreen
)
1709 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1710 ds_format
= wined3d_get_format(gl_info
, WINED3DFMT_UNKNOWN
);
1713 /* Try to find a pixel format which matches our requirements. */
1714 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1716 /* Try to locate a compatible format if we weren't able to find anything. */
1719 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1720 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1723 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1726 ERR("Can't find a suitable pixel format.\n");
1730 ret
->gl_info
= gl_info
;
1734 if (!context_set_pixel_format(ret
, hdc
, hdc_is_private
, pixel_format
))
1736 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1737 context_release(ret
);
1741 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1742 if (gl_info
->p_wglCreateContextAttribsARB
)
1744 if (!(ctx
= context_create_wgl_attribs(gl_info
, hdc
, share_ctx
)))
1749 if (!(ctx
= wglCreateContext(hdc
)))
1751 ERR("Failed to create a WGL context.\n");
1752 context_release(ret
);
1756 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1758 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1759 context_release(ret
);
1760 if (!wglDeleteContext(ctx
))
1761 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1766 if (!device_context_add(device
, ret
))
1768 ERR("Failed to add the newly created context to the context list\n");
1769 context_release(ret
);
1770 if (!wglDeleteContext(ctx
))
1771 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1775 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1776 ret
->state_table
= device
->StateTable
;
1778 /* Mark all states dirty to force a proper initialization of the states
1779 * on the first use of the context. */
1780 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1782 if (ret
->state_table
[state
].representative
)
1783 context_invalidate_state(ret
, state
);
1786 ret
->swapchain
= swapchain
;
1787 ret
->current_rt
.texture
= target
;
1788 ret
->current_rt
.sub_resource_idx
= 0;
1789 ret
->tid
= GetCurrentThreadId();
1791 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->resource
);
1792 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1796 ret
->win_handle
= swapchain
->win_handle
;
1798 ret
->hdc_is_private
= hdc_is_private
;
1799 ret
->hdc_has_format
= TRUE
;
1800 ret
->pixel_format
= pixel_format
;
1803 /* Set up the context defaults */
1804 if (!context_set_current(ret
))
1806 ERR("Cannot activate context to set up defaults.\n");
1807 device_context_remove(device
, ret
);
1808 context_release(ret
);
1809 if (!wglDeleteContext(ctx
))
1810 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1814 if (context_debug_output_enabled(gl_info
))
1816 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, ret
));
1817 if (TRACE_ON(d3d_synchronous
))
1818 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
1819 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1822 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
1823 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1827 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
1828 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1829 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
1830 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1831 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
1832 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1834 if (WARN_ON(d3d_perf
))
1836 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
1837 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1841 switch (swapchain
->desc
.swap_interval
)
1843 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1846 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1847 case WINED3DPRESENT_INTERVAL_ONE
:
1850 case WINED3DPRESENT_INTERVAL_TWO
:
1853 case WINED3DPRESENT_INTERVAL_THREE
:
1856 case WINED3DPRESENT_INTERVAL_FOUR
:
1860 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1864 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1866 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1867 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1868 swap_interval
, ret
, GetLastError());
1871 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1873 TRACE("Setting up the screen\n");
1875 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
1877 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1878 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1880 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1881 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1883 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1884 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1890 GL_EXTCALL(glGenVertexArrays(1, &vao
));
1891 GL_EXTCALL(glBindVertexArray(vao
));
1892 checkGLcall("creating VAO");
1895 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1896 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1897 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
1898 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1900 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1902 /* Direct3D always uses n-1 weights for n world matrices and uses
1903 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1904 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1905 * enabled as well. */
1906 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1907 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1909 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1911 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1912 * the previous texture where to source the offset from is always unit - 1.
1914 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1916 context_active_texture(ret
, gl_info
, s
);
1917 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1918 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1919 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1922 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1924 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1925 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1926 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1927 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1930 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1931 * program and the dummy program is destroyed when the context is destroyed.
1933 static const char dummy_program
[] =
1935 "MOV result.color, fragment.color.primary;\n"
1937 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1938 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1939 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1942 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1944 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1946 context_active_texture(ret
, gl_info
, s
);
1947 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1948 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1952 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1954 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1956 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1958 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1960 device
->shader_backend
->shader_init_context_state(ret
);
1961 ret
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
1962 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
1963 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
1964 | (1u << WINED3D_SHADER_TYPE_HULL
);
1966 /* If this happens to be the first context for the device, dummy textures
1967 * are not created yet. In that case, they will be created (and bound) by
1968 * create_dummy_textures right after this context is initialized. */
1969 if (device
->dummy_texture_2d
[0])
1970 bind_dummy_textures(device
, ret
);
1972 TRACE("Created context %p.\n", ret
);
1977 if (hdc
) wined3d_release_dc(swapchain
->win_handle
, hdc
);
1978 device
->shader_backend
->shader_free_context_data(ret
);
1979 device
->adapter
->fragment_pipe
->free_context_data(ret
);
1980 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1981 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1982 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
1983 HeapFree(GetProcessHeap(), 0, ret
->fbo_key
);
1984 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1985 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1986 HeapFree(GetProcessHeap(), 0, ret
);
1990 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1994 TRACE("Destroying ctx %p\n", context
);
1996 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1998 context_destroy_gl_resources(context
);
1999 TlsSetValue(wined3d_context_tls_idx
, NULL
);
2004 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2005 in wined3d_adapter may go away in the meantime */
2006 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
2007 *gl_info
= *context
->gl_info
;
2008 context
->gl_info
= gl_info
;
2009 context
->destroyed
= 1;
2013 device
->shader_backend
->shader_free_context_data(context
);
2014 device
->adapter
->fragment_pipe
->free_context_data(context
);
2015 HeapFree(GetProcessHeap(), 0, context
->fbo_key
);
2016 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
2017 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
2018 device_context_remove(device
, context
);
2019 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
2022 /* Context activation is done by the caller. */
2023 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
2025 const GLdouble projection
[] =
2027 2.0 / width
, 0.0, 0.0, 0.0,
2028 0.0, 2.0 / height
, 0.0, 0.0,
2030 -1.0, -1.0, -1.0, 1.0,
2033 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
2034 checkGLcall("glMatrixMode(GL_PROJECTION)");
2035 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
2036 checkGLcall("glLoadMatrixd");
2037 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
2038 checkGLcall("glViewport");
2041 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
2043 const struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2046 if (rt
->swapchain
&& rt
->swapchain
->front_buffer
== rt
)
2050 GetClientRect(context
->win_handle
, &window_size
);
2051 size
->cx
= window_size
.right
- window_size
.left
;
2052 size
->cy
= window_size
.bottom
- window_size
.top
;
2057 level
= context
->current_rt
.sub_resource_idx
% rt
->level_count
;
2058 size
->cx
= wined3d_texture_get_level_width(rt
, level
);
2059 size
->cy
= wined3d_texture_get_level_height(rt
, level
);
2062 /*****************************************************************************
2065 * Sets up a context for DirectDraw blitting.
2066 * All texture units are disabled, texture unit 0 is set as current unit
2067 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
2068 * color writing enabled for all channels
2069 * register combiners disabled, shaders disabled
2070 * world matrix is set to identity, texture matrix 0 too
2071 * projection matrix is setup for drawing screen coordinates
2074 * This: Device to activate the context for
2075 * context: Context to setup
2077 *****************************************************************************/
2078 /* Context activation is done by the caller. */
2079 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
2082 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2086 TRACE("Setting up context %p for blitting\n", context
);
2088 context_get_rt_size(context
, &rt_size
);
2090 if (context
->last_was_blit
)
2092 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
2094 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2095 context
->blit_w
= rt_size
.cx
;
2096 context
->blit_h
= rt_size
.cy
;
2097 /* No need to dirtify here, the states are still dirtified because
2098 * they weren't applied since the last SetupForBlit() call. */
2100 TRACE("Context is already set up for blitting, nothing to do\n");
2103 context
->last_was_blit
= TRUE
;
2105 /* Disable all textures. The caller can then bind a texture it wants to blit
2108 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
2109 * function texture unit. No need to care for higher samplers
2111 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
2113 sampler
= context
->rev_tex_unit_map
[i
];
2114 context_active_texture(context
, gl_info
, i
);
2116 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2118 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2119 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2121 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2122 checkGLcall("glDisable GL_TEXTURE_3D");
2123 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2125 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2126 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2128 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2129 checkGLcall("glDisable GL_TEXTURE_2D");
2131 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2132 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
2134 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2136 if (sampler
< MAX_TEXTURES
)
2137 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2138 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2141 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
2142 GL_EXTCALL(glBindSampler(0, 0));
2143 context_active_texture(context
, gl_info
, 0);
2145 sampler
= context
->rev_tex_unit_map
[0];
2147 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2149 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2150 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2152 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2153 checkGLcall("glDisable GL_TEXTURE_3D");
2154 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2156 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2157 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2159 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2160 checkGLcall("glDisable GL_TEXTURE_2D");
2162 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2164 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
2165 checkGLcall("glMatrixMode(GL_TEXTURE)");
2166 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2167 checkGLcall("glLoadIdentity()");
2169 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
2171 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
2172 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
2173 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2176 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2178 if (sampler
< MAX_TEXTURES
)
2180 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
2181 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2183 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2186 /* Other misc states */
2187 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2188 checkGLcall("glDisable(GL_ALPHA_TEST)");
2189 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
2190 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
2191 checkGLcall("glDisable GL_LIGHTING");
2192 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
2193 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
2194 checkGLcall("glDisable GL_DEPTH_TEST");
2195 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
2196 glDisableWINE(GL_FOG
);
2197 checkGLcall("glDisable GL_FOG");
2198 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
2199 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2200 checkGLcall("glDisable GL_BLEND");
2201 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2202 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
2203 checkGLcall("glDisable GL_CULL_FACE");
2204 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
2205 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
2206 checkGLcall("glDisable GL_STENCIL_TEST");
2207 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
2208 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
2209 checkGLcall("glDisable GL_SCISSOR_TEST");
2210 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2211 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2213 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
2214 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2215 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
2217 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
2218 checkGLcall("glColorMask");
2219 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
2220 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
2221 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
2222 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2223 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2225 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2226 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2227 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2230 /* Setup transforms */
2231 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2232 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2233 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2234 checkGLcall("glLoadIdentity()");
2235 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2237 context
->last_was_rhw
= TRUE
;
2238 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2240 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2241 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2242 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2243 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2244 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2245 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2246 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2248 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2250 /* Disable shaders */
2251 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2253 context
->blit_w
= rt_size
.cx
;
2254 context
->blit_h
= rt_size
.cy
;
2255 context_invalidate_state(context
, STATE_VIEWPORT
);
2256 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2259 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2261 return rt_mask
& (1u << 31);
2264 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2266 return rt_mask
& ~(1u << 31);
2269 /* Context activation is done by the caller. */
2270 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2272 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2276 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2277 checkGLcall("glDrawBuffer()");
2279 else if (is_rt_mask_onscreen(rt_mask
))
2281 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2282 checkGLcall("glDrawBuffer()");
2286 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2293 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2295 context
->draw_buffers
[i
] = GL_NONE
;
2301 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2303 GL_EXTCALL(glDrawBuffers(i
, context
->draw_buffers
));
2304 checkGLcall("glDrawBuffers()");
2308 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2309 checkGLcall("glDrawBuffer()");
2314 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2319 /* Context activation is done by the caller. */
2320 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2322 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2323 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2324 DWORD new_mask
= context_generate_rt_mask(buffer
);
2326 if (new_mask
== *current_mask
)
2329 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2330 checkGLcall("glDrawBuffer()");
2332 *current_mask
= new_mask
;
2335 /* Context activation is done by the caller. */
2336 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2338 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2339 checkGLcall("glActiveTexture");
2340 context
->active_texture
= unit
;
2343 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2345 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2346 DWORD unit
= context
->active_texture
;
2347 DWORD old_texture_type
= context
->texture_type
[unit
];
2351 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2352 checkGLcall("glBindTexture");
2359 if (old_texture_type
!= target
)
2361 const struct wined3d_device
*device
= context
->swapchain
->device
;
2363 switch (old_texture_type
)
2369 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2370 checkGLcall("glBindTexture");
2372 case GL_TEXTURE_RECTANGLE_ARB
:
2373 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2374 checkGLcall("glBindTexture");
2376 case GL_TEXTURE_CUBE_MAP
:
2377 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2378 checkGLcall("glBindTexture");
2381 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2382 checkGLcall("glBindTexture");
2385 ERR("Unexpected texture target %#x\n", old_texture_type
);
2388 context
->texture_type
[unit
] = target
;
2392 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2394 if (context
->render_offscreen
== offscreen
) return;
2396 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2397 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2398 context_invalidate_state(context
, STATE_VIEWPORT
);
2399 context_invalidate_state(context
, STATE_SCISSORRECT
);
2400 context_invalidate_state(context
, STATE_FRONTFACE
);
2401 context
->render_offscreen
= offscreen
;
2404 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2405 const struct wined3d_format
*required
)
2407 if (existing
== required
)
2409 if ((existing
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
)
2410 != (required
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
))
2412 if (existing
->depth_size
< required
->depth_size
)
2414 /* If stencil bits are used the exact amount is required - otherwise
2415 * wrapping won't work correctly. */
2416 if (required
->stencil_size
&& required
->stencil_size
!= existing
->stencil_size
)
2421 /* Context activation is done by the caller. */
2422 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2423 const struct wined3d_rendertarget_view
*depth_stencil
)
2425 /* Onscreen surfaces are always in a swapchain */
2426 struct wined3d_swapchain
*swapchain
= context
->current_rt
.texture
->swapchain
;
2427 struct wined3d_surface
*surface
;
2429 if (context
->render_offscreen
|| !depth_stencil
) return;
2430 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2432 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2433 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2435 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2437 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2438 surface
= context
->current_rt
.texture
->sub_resources
[context
->current_rt
.sub_resource_idx
].u
.surface
;
2439 surface_load_location(surface
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
2440 swapchain
->render_to_fbo
= TRUE
;
2441 swapchain_update_draw_bindings(swapchain
);
2442 context_set_render_offscreen(context
, TRUE
);
2445 GLenum
context_get_offscreen_gl_buffer(const struct wined3d_context
*context
)
2447 switch (wined3d_settings
.offscreen_rendering_mode
)
2450 return GL_COLOR_ATTACHMENT0
;
2452 case ORM_BACKBUFFER
:
2453 return context
->aux_buffers
> 0 ? GL_AUX0
: GL_BACK
;
2456 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
2461 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_context
*context
, struct wined3d_texture
*rt
)
2463 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2465 else if (rt
->swapchain
)
2466 return context_generate_rt_mask_from_resource(&rt
->resource
);
2468 return context_generate_rt_mask(context_get_offscreen_gl_buffer(context
));
2471 /* Context activation is done by the caller. */
2472 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2474 struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2475 struct wined3d_surface
*surface
;
2476 DWORD rt_mask
, *cur_mask
;
2478 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2480 context_validate_onscreen_formats(context
, NULL
);
2482 if (context
->render_offscreen
)
2484 wined3d_texture_load(rt
, context
, FALSE
);
2486 surface
= rt
->sub_resources
[context
->current_rt
.sub_resource_idx
].u
.surface
;
2487 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, surface
, NULL
, rt
->resource
.draw_binding
);
2488 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2495 context
->current_fbo
= NULL
;
2496 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2497 rt_mask
= context_generate_rt_mask_from_resource(&rt
->resource
);
2502 rt_mask
= context_generate_rt_mask_no_fbo(context
, rt
);
2505 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2507 if (rt_mask
!= *cur_mask
)
2509 context_apply_draw_buffers(context
, rt_mask
);
2510 *cur_mask
= rt_mask
;
2513 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2515 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2518 SetupForBlit(device
, context
);
2519 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2522 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2523 const struct wined3d_rendertarget_view
*ds
)
2527 if (ds
) return TRUE
;
2529 for (i
= 0; i
< rt_count
; ++i
)
2531 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2535 WARN("Invalid render target config, need at least one attachment.\n");
2539 /* Context activation is done by the caller. */
2540 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2541 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2543 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2544 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
2545 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2546 DWORD rt_mask
= 0, *cur_mask
;
2549 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2550 || rt_count
!= context
->gl_info
->limits
.buffers
)
2552 if (!context_validate_rt_config(rt_count
, rts
, dsv
))
2555 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2557 context_validate_onscreen_formats(context
, dsv
);
2559 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2561 for (i
= 0; i
< rt_count
; ++i
)
2563 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(rts
[i
]);
2564 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2565 rt_mask
|= (1u << i
);
2567 while (i
< context
->gl_info
->limits
.buffers
)
2569 context
->blit_targets
[i
] = NULL
;
2572 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2573 wined3d_rendertarget_view_get_surface(dsv
),
2574 rt_count
? rts
[0]->resource
->draw_binding
: 0,
2575 dsv
? dsv
->resource
->draw_binding
: 0);
2579 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2580 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2581 rt_mask
= context_generate_rt_mask_from_resource(rts
[0]->resource
);
2584 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2585 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2586 * state management allows this */
2587 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2591 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2592 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0])->container
: NULL
);
2595 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2596 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2598 for (i
= 0; i
< rt_count
; ++i
)
2600 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2601 rt_mask
|= (1u << i
);
2606 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2607 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0])->container
: NULL
);
2610 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2612 if (rt_mask
!= *cur_mask
)
2614 context_apply_draw_buffers(context
, rt_mask
);
2615 *cur_mask
= rt_mask
;
2616 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2619 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2621 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2624 if (context
->last_was_blit
)
2625 context
->last_was_blit
= FALSE
;
2627 /* Blending and clearing should be orthogonal, but tests on the nvidia
2628 * driver show that disabling blending when clearing improves the clearing
2629 * performance incredibly. */
2630 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2631 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2632 if (rt_count
&& gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
2634 if (needs_srgb_write(context
, &device
->state
, fb
))
2635 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
2637 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
2638 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
2640 checkGLcall("setting up state for clear");
2642 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2643 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2644 context_invalidate_state(context
, STATE_SCISSORRECT
);
2649 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_state
*state
)
2651 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
2652 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2653 DWORD rt_mask
, rt_mask_bits
;
2656 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2657 return context_generate_rt_mask_no_fbo(context
, wined3d_rendertarget_view_get_surface(rts
[0])->container
);
2658 else if (!context
->render_offscreen
)
2659 return context_generate_rt_mask_from_resource(rts
[0]->resource
);
2661 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2662 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2663 rt_mask_bits
= rt_mask
;
2665 while (rt_mask_bits
)
2667 rt_mask_bits
&= ~(1u << i
);
2668 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
2669 rt_mask
&= ~(1u << i
);
2677 /* Context activation is done by the caller. */
2678 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2680 DWORD rt_mask
= find_draw_buffers_mask(context
, state
);
2681 const struct wined3d_fb_state
*fb
= state
->fb
;
2684 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2686 if (!context
->render_offscreen
)
2688 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2689 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2695 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
2697 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(fb
->render_targets
[i
]);
2699 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2700 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2701 fb
->render_targets
[0] ? fb
->render_targets
[0]->resource
->draw_binding
: 0,
2702 fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
2706 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2707 if (rt_mask
!= *cur_mask
)
2709 context_apply_draw_buffers(context
, rt_mask
);
2710 *cur_mask
= rt_mask
;
2714 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
2716 DWORD i
= context
->rev_tex_unit_map
[unit
];
2717 DWORD j
= context
->tex_unit_map
[stage
];
2719 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
2720 context
->tex_unit_map
[stage
] = unit
;
2721 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2722 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2724 context
->rev_tex_unit_map
[unit
] = stage
;
2725 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2726 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2729 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
2733 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2734 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
2737 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
2738 const struct wined3d_state
*state
)
2742 context
->fixed_function_usage_map
= 0;
2743 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2745 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2746 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2747 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2748 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2749 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2750 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2751 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2752 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2754 /* Not used, and disable higher stages. */
2755 if (color_op
== WINED3D_TOP_DISABLE
)
2758 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2759 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2760 || ((color_arg3
== WINED3DTA_TEXTURE
)
2761 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2762 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2763 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2764 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2765 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2766 context
->fixed_function_usage_map
|= (1u << i
);
2768 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2769 && i
< MAX_TEXTURES
- 1)
2770 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
2773 if (i
< context
->lowest_disabled_stage
)
2776 end
= context
->lowest_disabled_stage
;
2780 start
= context
->lowest_disabled_stage
;
2784 context
->lowest_disabled_stage
= i
;
2785 for (i
= start
+ 1; i
< end
; ++i
)
2787 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
2791 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
2792 const struct wined3d_state
*state
)
2794 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2795 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2796 unsigned int i
, tex
;
2799 context_update_fixed_function_usage_map(context
, state
);
2801 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2804 ffu_map
= context
->fixed_function_usage_map
;
2806 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2807 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2809 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2814 if (context
->tex_unit_map
[i
] != i
)
2816 context_map_stage(context
, i
, i
);
2817 context_invalidate_state(context
, STATE_SAMPLER(i
));
2818 context_invalidate_texture_stage(context
, i
);
2824 /* Now work out the mapping */
2826 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2831 if (context
->tex_unit_map
[i
] != tex
)
2833 context_map_stage(context
, i
, tex
);
2834 context_invalidate_state(context
, STATE_SAMPLER(i
));
2835 context_invalidate_texture_stage(context
, i
);
2842 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2844 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2845 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2846 const struct wined3d_shader_resource_info
*resource_info
=
2847 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2850 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2853 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2855 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
2857 context_map_stage(context
, i
, i
);
2858 context_invalidate_state(context
, STATE_SAMPLER(i
));
2859 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2860 context_invalidate_texture_stage(context
, i
);
2865 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
2866 const struct wined3d_shader_resource_info
*ps_resource_info
, DWORD unit
)
2868 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
2870 /* Not currently used */
2871 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
2874 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
2876 /* Used by a fragment sampler */
2878 if (!ps_resource_info
)
2880 /* No pixel shader, check fixed function */
2881 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1u << current_mapping
));
2884 /* Pixel shader, check the shader's sampler map */
2885 return !ps_resource_info
[current_mapping
].type
;
2891 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
2893 const struct wined3d_shader_resource_info
*vs_resource_info
=
2894 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
2895 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
2896 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2897 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2900 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2903 /* Note that we only care if a resource is used or not, not the
2904 * resource's specific type. Otherwise we'd need to call
2905 * shader_update_samplers() here for 1.x pixelshaders. */
2907 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2909 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
2911 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2912 if (vs_resource_info
[i
].type
)
2916 if (context_unit_free_for_vs(context
, ps_resource_info
, start
))
2918 if (context
->tex_unit_map
[vsampler_idx
] != start
)
2920 context_map_stage(context
, vsampler_idx
, start
);
2921 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
2930 if (context
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
2931 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
2936 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2938 BOOL vs
= use_vs(state
);
2939 BOOL ps
= use_ps(state
);
2941 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
2942 * need a 1:1 map at the moment.
2943 * When the mapping of a stage is changed, sampler and ALL texture stage
2944 * states have to be reset. */
2947 context_map_psamplers(context
, state
);
2949 context_map_fixed_function_samplers(context
, state
);
2952 context_map_vsamplers(context
, ps
, state
);
2955 /* Context activation is done by the caller. */
2956 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2958 DWORD rt_mask
, *cur_mask
;
2960 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2962 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2963 rt_mask
= find_draw_buffers_mask(context
, state
);
2964 if (rt_mask
!= *cur_mask
)
2966 context_apply_draw_buffers(context
, rt_mask
);
2967 *cur_mask
= rt_mask
;
2971 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
2973 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
2974 *regnum
= WINED3D_FFP_POSITION
;
2975 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
2976 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
2977 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
2978 *regnum
= WINED3D_FFP_BLENDINDICES
;
2979 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
2980 *regnum
= WINED3D_FFP_NORMAL
;
2981 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
2982 *regnum
= WINED3D_FFP_PSIZE
;
2983 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
2984 *regnum
= WINED3D_FFP_DIFFUSE
;
2985 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
2986 *regnum
= WINED3D_FFP_SPECULAR
;
2987 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
2988 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
2991 FIXME("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
2999 /* Context activation is done by the caller. */
3000 void context_stream_info_from_declaration(struct wined3d_context
*context
,
3001 const struct wined3d_state
*state
, struct wined3d_stream_info
*stream_info
)
3003 /* We need to deal with frequency data! */
3004 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
3005 BOOL use_vshader
= use_vs(state
);
3006 BOOL generic_attributes
= context
->d3d_info
->ffp_generic_attributes
;
3009 stream_info
->use_map
= 0;
3010 stream_info
->swizzle_map
= 0;
3011 stream_info
->position_transformed
= declaration
->position_transformed
;
3013 /* Translate the declaration into strided data. */
3014 for (i
= 0; i
< declaration
->element_count
; ++i
)
3016 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
3017 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
3021 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
3022 element
, i
+ 1, declaration
->element_count
);
3024 if (!stream
->buffer
)
3027 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
3031 if (element
->output_slot
== WINED3D_OUTPUT_SLOT_UNUSED
)
3033 stride_used
= FALSE
;
3035 else if (element
->output_slot
== WINED3D_OUTPUT_SLOT_SEMANTIC
)
3037 /* TODO: Assuming vertexdeclarations are usually used with the
3038 * same or a similar shader, it might be worth it to store the
3039 * last used output slot and try that one first. */
3040 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
3041 element
->usage
, element
->usage_idx
, &idx
);
3045 idx
= element
->output_slot
;
3051 if (!generic_attributes
&& !element
->ffp_valid
)
3053 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3054 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
3055 stride_used
= FALSE
;
3059 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
3065 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3066 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3067 use_vshader
? "shader": "fixed function", idx
,
3068 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
3069 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
),
3070 debug_d3dinput_classification(element
->input_slot_class
), element
->instance_data_step_rate
);
3072 stream_info
->elements
[idx
].format
= element
->format
;
3073 stream_info
->elements
[idx
].data
.buffer_object
= 0;
3074 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
3075 stream_info
->elements
[idx
].stride
= stream
->stride
;
3076 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
3077 if (stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
3079 stream_info
->elements
[idx
].divisor
= 1;
3081 else if (element
->input_slot_class
== WINED3D_INPUT_PER_INSTANCE_DATA
)
3083 stream_info
->elements
[idx
].divisor
= element
->instance_data_step_rate
;
3084 if (!element
->instance_data_step_rate
)
3085 FIXME("Instance step rate 0 not implemented.\n");
3089 stream_info
->elements
[idx
].divisor
= 0;
3092 if (!context
->gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3093 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
3095 stream_info
->swizzle_map
|= 1u << idx
;
3097 stream_info
->use_map
|= 1u << idx
;
3102 /* Context activation is done by the caller. */
3103 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3105 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3106 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3107 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
3108 DWORD prev_all_vbo
= stream_info
->all_vbo
;
3112 context_stream_info_from_declaration(context
, state
, stream_info
);
3114 stream_info
->all_vbo
= 1;
3115 context
->num_buffer_queries
= 0;
3116 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
3118 struct wined3d_stream_info_element
*element
;
3119 struct wined3d_bo_address data
;
3120 struct wined3d_buffer
*buffer
;
3125 element
= &stream_info
->elements
[i
];
3126 buffer
= state
->streams
[element
->stream_idx
].buffer
;
3128 /* We can't use VBOs if the base vertex index is negative. OpenGL
3129 * doesn't accept negative offsets (or rather offsets bigger than the
3130 * VBO, because the pointer is unsigned), so use system memory
3131 * sources. In most sane cases the pointer - offset will still be > 0,
3132 * otherwise it will wrap around to some big value. Hope that with the
3133 * indices the driver wraps it back internally. If not,
3134 * drawStridedSlow is needed, including a vertex buffer path. */
3135 if (state
->load_base_vertex_index
< 0)
3137 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3138 state
->load_base_vertex_index
);
3139 element
->data
.buffer_object
= 0;
3140 element
->data
.addr
+= (ULONG_PTR
)buffer_get_sysmem(buffer
, context
);
3141 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
3142 FIXME("System memory vertex data load offset is negative!\n");
3146 buffer_internal_preload(buffer
, context
, state
);
3147 buffer_get_memory(buffer
, context
, &data
);
3148 element
->data
.buffer_object
= data
.buffer_object
;
3149 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
3152 if (!element
->data
.buffer_object
)
3153 stream_info
->all_vbo
= 0;
3156 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
3158 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
3163 if (state
->vertex_declaration
->half_float_conv_needed
&& !stream_info
->all_vbo
)
3165 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
3166 context
->use_immediate_mode_draw
= TRUE
;
3170 context
->use_immediate_mode_draw
= FALSE
;
3175 WORD slow_mask
= -!d3d_info
->ffp_generic_attributes
& (1u << WINED3D_FFP_PSIZE
);
3176 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3177 & ((1u << WINED3D_FFP_DIFFUSE
) | (1u << WINED3D_FFP_SPECULAR
));
3179 if (((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
3180 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
3181 context
->use_immediate_mode_draw
= TRUE
;
3183 context
->use_immediate_mode_draw
= FALSE
;
3186 if (prev_all_vbo
!= stream_info
->all_vbo
)
3187 context_invalidate_state(context
, STATE_INDEXBUFFER
);
3190 /* Context activation is done by the caller. */
3191 static void context_preload_texture(struct wined3d_context
*context
,
3192 const struct wined3d_state
*state
, unsigned int idx
)
3194 struct wined3d_texture
*texture
;
3196 if (!(texture
= state
->textures
[idx
]))
3199 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
3202 /* Context activation is done by the caller. */
3203 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3209 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3211 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
3212 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
3218 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3220 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
3221 context_preload_texture(context
, state
, i
);
3226 WORD ffu_map
= context
->fixed_function_usage_map
;
3228 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3231 context_preload_texture(context
, state
, i
);
3236 static void context_load_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3238 struct wined3d_shader_sampler_map_entry
*entry
;
3239 struct wined3d_shader_resource_view
*view
;
3240 struct wined3d_shader
*shader
;
3243 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3245 if (!(shader
= state
->shader
[i
]))
3248 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3250 if (state
->cb
[i
][j
])
3251 buffer_internal_preload(state
->cb
[i
][j
], context
, state
);
3254 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
3256 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3258 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
3260 WARN("No resource view bound at index %u, %u.\n", i
, entry
->resource_idx
);
3264 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3265 buffer_internal_preload(buffer_from_resource(view
->resource
), context
, state
);
3267 wined3d_texture_load(wined3d_texture_from_resource(view
->resource
), context
, FALSE
);
3272 static void context_bind_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3274 const struct wined3d_device
*device
= context
->swapchain
->device
;
3275 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3276 struct wined3d_shader_sampler_map_entry
*entry
;
3277 struct wined3d_shader_resource_view
*view
;
3278 struct wined3d_sampler
*sampler
;
3279 struct wined3d_texture
*texture
;
3280 struct wined3d_shader
*shader
;
3281 unsigned int i
, j
, count
;
3282 GLuint sampler_name
;
3286 enum wined3d_shader_type type
;
3287 unsigned int base_idx
;
3292 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
3293 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
3296 for (i
= 0; i
< ARRAY_SIZE(shader_types
); ++i
)
3298 if (!(shader
= state
->shader
[shader_types
[i
].type
]))
3301 count
= shader
->reg_maps
.sampler_map
.count
;
3302 if (count
> shader_types
[i
].count
)
3304 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3305 shader
, count
, shader_types
[i
].count
);
3306 count
= shader_types
[i
].count
;
3309 for (j
= 0; j
< count
; ++j
)
3311 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3313 if (!(view
= state
->shader_resource_view
[shader_types
[i
].type
][entry
->resource_idx
]))
3315 WARN("No resource view bound at index %u, %u.\n", shader_types
[i
].type
, entry
->resource_idx
);
3319 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3321 FIXME("Buffer shader resources not supported.\n");
3325 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
3327 sampler_name
= device
->default_sampler
;
3329 else if ((sampler
= state
->sampler
[shader_types
[i
].type
][entry
->sampler_idx
]))
3331 sampler_name
= sampler
->name
;
3335 WARN("No sampler object bound at index %u, %u.\n", shader_types
[i
].type
, entry
->sampler_idx
);
3339 texture
= wined3d_texture_from_resource(view
->resource
);
3340 context_active_texture(context
, gl_info
, shader_types
[i
].base_idx
+ entry
->bind_idx
);
3341 wined3d_texture_bind(texture
, context
, FALSE
);
3343 GL_EXTCALL(glBindSampler(shader_types
[i
].base_idx
+ entry
->bind_idx
, sampler_name
));
3344 checkGLcall("glBindSampler");
3349 /* Context activation is done by the caller. */
3350 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
3352 const struct wined3d_state
*state
= &device
->state
;
3353 const struct StateEntry
*state_table
= context
->state_table
;
3354 const struct wined3d_fb_state
*fb
= state
->fb
;
3358 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
3359 fb
->render_targets
, fb
->depth_stencil
))
3362 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3364 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3367 /* Preload resources before FBO setup. Texture preload in particular may
3368 * result in changes to the current FBO, due to using e.g. FBO blits for
3369 * updating a resource location. */
3370 context_update_tex_unit_map(context
, state
);
3371 context_preload_textures(context
, state
);
3372 context_load_shader_resources(context
, state
);
3373 /* TODO: Right now the dependency on the vertex shader is necessary
3374 * since context_stream_info_from_declaration depends on the reg_maps of
3375 * the current VS but maybe it's possible to relax the coupling in some
3376 * situations at least. */
3377 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
3378 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
3380 context_update_stream_info(context
, state
);
3384 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3387 buffer_mark_used(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
);
3390 if (state
->index_buffer
)
3392 if (context
->stream_info
.all_vbo
)
3393 buffer_internal_preload(state
->index_buffer
, context
, state
);
3395 buffer_get_sysmem(state
->index_buffer
, context
);
3398 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3400 DWORD rep
= context
->dirtyArray
[i
];
3401 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3402 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3403 context
->isStateDirty
[idx
] &= ~(1u << shift
);
3404 state_table
[rep
].apply(context
, state
, rep
);
3407 if (context
->shader_update_mask
)
3409 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3410 context
->shader_update_mask
= 0;
3413 if (context
->constant_update_mask
)
3415 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3416 context
->constant_update_mask
= 0;
3419 if (context
->update_shader_resource_bindings
)
3421 context_bind_shader_resources(context
, state
);
3422 context
->update_shader_resource_bindings
= 0;
3425 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3427 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3430 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3431 context
->last_was_blit
= FALSE
;
3436 static void context_setup_target(struct wined3d_context
*context
,
3437 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
3439 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3441 render_offscreen
= wined3d_resource_is_offscreen(&texture
->resource
);
3442 if (context
->current_rt
.texture
== texture
3443 && context
->current_rt
.sub_resource_idx
== sub_resource_idx
3444 && render_offscreen
== old_render_offscreen
)
3447 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3448 * the alpha blend state changes with different render target formats. */
3449 if (!context
->current_rt
.texture
)
3451 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3455 const struct wined3d_format
*old
= context
->current_rt
.texture
->resource
.format
;
3456 const struct wined3d_format
*new = texture
->resource
.format
;
3458 if (old
->id
!= new->id
)
3460 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3461 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3462 || !(texture
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3463 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3465 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3466 if ((context
->current_rt
.texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
3467 != (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3468 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3471 /* When switching away from an offscreen render target, and we're not
3472 * using FBOs, we have to read the drawable into the texture. This is
3473 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
3474 * There are some things that need care though. PreLoad needs a GL context,
3475 * and FindContext is called before the context is activated. It also
3476 * has to be called with the old rendertarget active, otherwise a
3477 * wrong drawable is read. */
3478 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
3479 && old_render_offscreen
&& (context
->current_rt
.texture
!= texture
3480 || context
->current_rt
.sub_resource_idx
!= sub_resource_idx
))
3482 unsigned int prev_sub_resource_idx
= context
->current_rt
.sub_resource_idx
;
3483 struct wined3d_texture
*prev_texture
= context
->current_rt
.texture
;
3485 /* Read the back buffer of the old drawable into the destination texture. */
3486 if (prev_texture
->texture_srgb
.name
)
3487 wined3d_texture_load(prev_texture
, context
, TRUE
);
3488 wined3d_texture_load(prev_texture
, context
, FALSE
);
3489 wined3d_texture_invalidate_location(prev_texture
, prev_sub_resource_idx
, WINED3D_LOCATION_DRAWABLE
);
3493 context
->current_rt
.texture
= texture
;
3494 context
->current_rt
.sub_resource_idx
= sub_resource_idx
;
3495 context_set_render_offscreen(context
, render_offscreen
);
3498 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
3500 struct wined3d_context
*current_context
= context_get_current();
3501 struct wined3d_texture
*target_texture
;
3502 unsigned int target_sub_resource_idx
;
3503 struct wined3d_context
*context
;
3505 TRACE("device %p, target %p.\n", device
, target
);
3507 if (current_context
&& current_context
->destroyed
)
3508 current_context
= NULL
;
3512 target_texture
= target
->container
;
3513 target_sub_resource_idx
= surface_get_sub_resource_idx(target
);
3518 && current_context
->current_rt
.texture
3519 && current_context
->swapchain
->device
== device
)
3521 target_texture
= current_context
->current_rt
.texture
;
3522 target_sub_resource_idx
= current_context
->current_rt
.sub_resource_idx
;
3526 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
3528 if (swapchain
->back_buffers
)
3529 target_texture
= swapchain
->back_buffers
[0];
3531 target_texture
= swapchain
->front_buffer
;
3532 target_sub_resource_idx
= 0;
3536 if (current_context
&& current_context
->current_rt
.texture
== target_texture
)
3538 context
= current_context
;
3540 else if (target_texture
->swapchain
)
3542 TRACE("Rendering onscreen.\n");
3544 context
= swapchain_get_context(target_texture
->swapchain
);
3548 TRACE("Rendering offscreen.\n");
3550 /* Stay with the current context if possible. Otherwise use the
3551 * context for the primary swapchain. */
3552 if (current_context
&& current_context
->swapchain
->device
== device
)
3553 context
= current_context
;
3555 context
= swapchain_get_context(device
->swapchains
[0]);
3558 context_enter(context
);
3559 context_update_window(context
);
3560 context_setup_target(context
, target_texture
, target_sub_resource_idx
);
3561 if (!context
->valid
) return context
;
3563 if (context
!= current_context
)
3565 if (!context_set_current(context
))
3566 ERR("Failed to activate the new context.\n");
3568 else if (context
->needs_set
)
3570 context_set_gl_context(context
);