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 (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
484 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
486 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
487 depth_stencil
= NULL
;
489 else if (depth_stencil
->container
->resource
.multisample_type
490 != render_targets
[0]->container
->resource
.multisample_type
491 || depth_stencil
->container
->resource
.multisample_quality
492 != render_targets
[0]->container
->resource
.multisample_quality
)
494 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
495 render_targets
[0]->container
->resource
.multisample_type
,
496 render_targets
[0]->container
->resource
.multisample_quality
,
497 depth_stencil
->container
->resource
.multisample_type
,
498 depth_stencil
->container
->resource
.multisample_quality
);
499 depth_stencil
= NULL
;
502 surface_set_compatible_renderbuffer(depth_stencil
, render_targets
[0]);
505 context_generate_fbo_key(context
, context
->fbo_key
, render_targets
, depth_stencil
, color_location
,
510 TRACE("Dumping FBO attachments:\n");
511 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
513 if (render_targets
[i
])
515 TRACE(" Color attachment %u: (%p) %s gl obj %u(%s) %ux%u %u samples.\n",
516 i
, render_targets
[i
], debug_d3dformat(render_targets
[i
]->container
->resource
.format
->id
),
517 context
->fbo_key
->objects
[i
+ 1].object
,
518 context
->fbo_key
->rb_namespace
& (1 << (i
+ 1)) ? "rb" : "texure",
519 render_targets
[i
]->pow2Width
, render_targets
[i
]->pow2Height
,
520 render_targets
[i
]->container
->resource
.multisample_type
);
525 TRACE(" Depth attachment: (%p) %s gl obj %u(%s) %ux%u %u samples.\n",
526 depth_stencil
, debug_d3dformat(depth_stencil
->container
->resource
.format
->id
),
527 context
->fbo_key
->objects
[0].object
,
528 context
->fbo_key
->rb_namespace
& (1 << 0) ? "rb" : "texure",
529 depth_stencil
->pow2Width
, depth_stencil
->pow2Height
,
530 depth_stencil
->container
->resource
.multisample_type
);
534 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
536 if (memcmp(context
->fbo_key
, &entry
->key
, FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[object_count
])))
539 list_remove(&entry
->entry
);
540 list_add_head(&context
->fbo_list
, &entry
->entry
);
544 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
546 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, color_location
, ds_location
);
547 list_add_head(&context
->fbo_list
, &entry
->entry
);
548 ++context
->fbo_entry_count
;
552 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
553 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, color_location
, ds_location
, entry
);
554 list_remove(&entry
->entry
);
555 list_add_head(&context
->fbo_list
, &entry
->entry
);
561 /* Context activation is done by the caller. */
562 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
564 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
566 GLuint read_binding
, draw_binding
;
568 if (entry
->flags
& WINED3D_FBO_ENTRY_FLAG_ATTACHED
)
570 context_bind_fbo(context
, target
, entry
->id
);
574 read_binding
= context
->fbo_read_binding
;
575 draw_binding
= context
->fbo_draw_binding
;
576 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
578 /* Apply render targets */
579 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
581 context_attach_surface_fbo(context
, target
, i
, &entry
->key
.objects
[i
+ 1],
582 entry
->key
.rb_namespace
& (1 << (i
+ 1)));
585 context_attach_depth_stencil_fbo(context
, target
, &entry
->key
.objects
[0],
586 entry
->key
.rb_namespace
& 0x1, entry
->flags
);
588 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
589 * GL contexts requirements. */
590 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
591 context_set_draw_buffer(context
, GL_NONE
);
592 if (target
!= GL_FRAMEBUFFER
)
594 if (target
== GL_READ_FRAMEBUFFER
)
595 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
597 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
600 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_ATTACHED
;
603 /* Context activation is done by the caller. */
604 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
605 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
606 DWORD color_location
, DWORD ds_location
)
608 struct fbo_entry
*entry
, *entry2
;
610 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
612 context_destroy_fbo_entry(context
, entry
);
615 if (context
->rebind_fbo
)
617 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
618 context
->rebind_fbo
= FALSE
;
621 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
623 context
->current_fbo
= NULL
;
624 context_bind_fbo(context
, target
, 0);
628 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
,
629 color_location
, ds_location
);
630 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
634 /* Context activation is done by the caller. */
635 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
636 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
638 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
640 context
->blit_targets
[0] = render_target
;
642 memset(&context
->blit_targets
[1], 0, clear_size
);
643 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
, location
);
646 /* Context activation is done by the caller. */
647 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
649 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
651 if (context
->free_occlusion_query_count
)
653 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
657 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
659 GL_EXTCALL(glGenQueries(1, &query
->id
));
660 checkGLcall("glGenQueries");
662 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
666 WARN("Occlusion queries not supported, not allocating query id.\n");
671 query
->context
= context
;
672 list_add_head(&context
->occlusion_queries
, &query
->entry
);
675 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
677 struct wined3d_context
*context
= query
->context
;
679 list_remove(&query
->entry
);
680 query
->context
= NULL
;
682 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
684 UINT new_size
= context
->free_occlusion_query_size
<< 1;
685 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
686 new_size
* sizeof(*context
->free_occlusion_queries
));
690 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
694 context
->free_occlusion_query_size
= new_size
;
695 context
->free_occlusion_queries
= new_data
;
698 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
701 /* Context activation is done by the caller. */
702 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
704 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
706 if (context
->free_event_query_count
)
708 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
712 if (gl_info
->supported
[ARB_SYNC
])
714 /* Using ARB_sync, not much to do here. */
715 query
->object
.sync
= NULL
;
716 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
718 else if (gl_info
->supported
[APPLE_FENCE
])
720 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
721 checkGLcall("glGenFencesAPPLE");
723 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
725 else if(gl_info
->supported
[NV_FENCE
])
727 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
728 checkGLcall("glGenFencesNV");
730 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
734 WARN("Event queries not supported, not allocating query id.\n");
735 query
->object
.id
= 0;
739 query
->context
= context
;
740 list_add_head(&context
->event_queries
, &query
->entry
);
743 void context_free_event_query(struct wined3d_event_query
*query
)
745 struct wined3d_context
*context
= query
->context
;
747 list_remove(&query
->entry
);
748 query
->context
= NULL
;
750 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
752 UINT new_size
= context
->free_event_query_size
<< 1;
753 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
754 new_size
* sizeof(*context
->free_event_queries
));
758 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
762 context
->free_event_query_size
= new_size
;
763 context
->free_event_queries
= new_data
;
766 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
769 /* Context activation is done by the caller. */
770 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
772 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
774 if (context
->free_timestamp_query_count
)
776 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
780 GL_EXTCALL(glGenQueries(1, &query
->id
));
781 checkGLcall("glGenQueries");
783 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
786 query
->context
= context
;
787 list_add_head(&context
->timestamp_queries
, &query
->entry
);
790 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
792 struct wined3d_context
*context
= query
->context
;
794 list_remove(&query
->entry
);
795 query
->context
= NULL
;
797 if (context
->free_timestamp_query_count
>= context
->free_timestamp_query_size
- 1)
799 UINT new_size
= context
->free_timestamp_query_size
<< 1;
800 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_timestamp_queries
,
801 new_size
* sizeof(*context
->free_timestamp_queries
));
805 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
809 context
->free_timestamp_query_size
= new_size
;
810 context
->free_timestamp_queries
= new_data
;
813 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
816 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
818 static void context_enum_fbo_entries(const struct wined3d_device
*device
,
819 GLuint name
, BOOL rb_namespace
, context_fbo_entry_func_t
*callback
)
823 for (i
= 0; i
< device
->context_count
; ++i
)
825 struct wined3d_context
*context
= device
->contexts
[i
];
826 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
827 struct fbo_entry
*entry
, *entry2
;
829 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
833 for (j
= 0; j
< gl_info
->limits
.buffers
+ 1; ++j
)
835 if (entry
->key
.objects
[j
].object
== name
836 && !(entry
->key
.rb_namespace
& (1 << j
)) == !rb_namespace
)
838 callback(context
, entry
);
846 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
848 list_remove(&entry
->entry
);
849 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
852 void context_resource_released(const struct wined3d_device
*device
,
853 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
856 struct wined3d_surface
*surface
;
858 if (!device
->d3d_initialized
)
863 case WINED3D_RTYPE_SURFACE
:
864 surface
= surface_from_resource(resource
);
866 for (i
= 0; i
< device
->context_count
; ++i
)
868 struct wined3d_context
*context
= device
->contexts
[i
];
869 if (context
->current_rt
== surface
)
870 context
->current_rt
= NULL
;
879 void context_gl_resource_released(struct wined3d_device
*device
,
880 GLuint name
, BOOL rb_namespace
)
882 context_enum_fbo_entries(device
, name
, rb_namespace
, context_queue_fbo_entry_destruction
);
885 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
887 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
888 struct fbo_entry
*entry
= context
->current_fbo
;
891 if (!entry
|| context
->rebind_fbo
) return;
893 for (i
= 0; i
< gl_info
->limits
.buffers
+ 1; ++i
)
895 if (surface
->container
->texture_rgb
.name
== entry
->key
.objects
[i
].object
896 || surface
->container
->texture_srgb
.name
== entry
->key
.objects
[i
].object
)
898 TRACE("Updated surface %p is bound as attachment %u to the current FBO.\n", surface
, i
);
899 context
->rebind_fbo
= TRUE
;
905 static BOOL
context_restore_pixel_format(struct wined3d_context
*ctx
)
907 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
910 if (ctx
->restore_pf
&& IsWindow(ctx
->restore_pf_win
))
912 if (ctx
->gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
914 HDC dc
= GetDCEx(ctx
->restore_pf_win
, 0, DCX_USESTYLE
| DCX_CACHE
);
917 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, ctx
->restore_pf
))))
919 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
920 ctx
->restore_pf
, ctx
->restore_pf_win
);
922 ReleaseDC(ctx
->restore_pf_win
, dc
);
927 ERR("can't restore pixel format %d on window %p\n", ctx
->restore_pf
, ctx
->restore_pf_win
);
932 ctx
->restore_pf_win
= NULL
;
936 static BOOL
context_set_pixel_format(struct wined3d_context
*context
, HDC dc
, BOOL
private, int format
)
938 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
941 if (dc
== context
->hdc
&& context
->hdc_is_private
&& context
->hdc_has_format
)
944 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
945 if (current
== format
) goto success
;
949 if (!SetPixelFormat(dc
, format
, NULL
))
951 /* This may also happen if the dc belongs to a destroyed window. */
952 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
953 format
, dc
, GetLastError());
957 context
->restore_pf
= 0;
958 context
->restore_pf_win
= private ? NULL
: WindowFromDC(dc
);
962 /* By default WGL doesn't allow pixel format adjustments but we need it
963 * here. For this reason there's a Wine specific wglSetPixelFormat()
964 * which allows us to set the pixel format multiple times. Only use it
965 * when really needed. */
966 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
970 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
972 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
977 win
= private ? NULL
: WindowFromDC(dc
);
978 if (win
!= context
->restore_pf_win
)
980 context_restore_pixel_format(context
);
982 context
->restore_pf
= private ? 0 : current
;
983 context
->restore_pf_win
= win
;
989 /* OpenGL doesn't allow pixel format adjustments. Print an error and
990 * continue using the old format. There's a big chance that the old
991 * format works although with a performance hit and perhaps rendering
993 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
994 format
, dc
, current
);
998 if (dc
== context
->hdc
&& context
->hdc_is_private
)
999 context
->hdc_has_format
= TRUE
;
1003 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
1005 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
1006 BOOL backup
= FALSE
;
1008 if (!context_set_pixel_format(ctx
, ctx
->hdc
, ctx
->hdc_is_private
, ctx
->pixel_format
))
1010 WARN("Failed to set pixel format %d on device context %p.\n",
1011 ctx
->pixel_format
, ctx
->hdc
);
1015 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
1019 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1020 ctx
->glCtx
, ctx
->hdc
, GetLastError());
1022 WARN("Trying fallback to the backup window.\n");
1024 /* FIXME: If the context is destroyed it's no longer associated with
1025 * a swapchain, so we can't use the swapchain to get a backup dc. To
1026 * make this work windowless contexts would need to be handled by the
1030 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
1031 context_set_current(NULL
);
1035 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
1037 context_set_current(NULL
);
1041 if (!context_set_pixel_format(ctx
, dc
, TRUE
, ctx
->pixel_format
))
1043 ERR("Failed to set pixel format %d on device context %p.\n",
1044 ctx
->pixel_format
, dc
);
1045 context_set_current(NULL
);
1049 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
1051 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1052 dc
, GetLastError());
1053 context_set_current(NULL
);
1063 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
1065 if (!wglMakeCurrent(dc
, gl_ctx
))
1067 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1068 gl_ctx
, dc
, GetLastError());
1069 context_set_current(NULL
);
1073 static void context_update_window(struct wined3d_context
*context
)
1075 if (context
->win_handle
== context
->swapchain
->win_handle
)
1078 TRACE("Updating context %p window from %p to %p.\n",
1079 context
, context
->win_handle
, context
->swapchain
->win_handle
);
1082 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1084 context
->win_handle
= context
->swapchain
->win_handle
;
1085 context
->hdc_is_private
= FALSE
;
1086 context
->hdc_has_format
= FALSE
;
1087 context
->needs_set
= 1;
1090 if (!(context
->hdc
= GetDCEx(context
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1092 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
1097 static void context_destroy_gl_resources(struct wined3d_context
*context
)
1099 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1100 struct wined3d_timestamp_query
*timestamp_query
;
1101 struct wined3d_occlusion_query
*occlusion_query
;
1102 struct wined3d_event_query
*event_query
;
1103 struct fbo_entry
*entry
, *entry2
;
1108 restore_ctx
= wglGetCurrentContext();
1109 restore_dc
= wglGetCurrentDC();
1111 if (restore_ctx
== context
->glCtx
)
1113 else if (context
->valid
)
1114 context_set_gl_context(context
);
1116 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
1119 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
1120 timestamp_query
->context
= NULL
;
1123 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1125 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1126 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1127 occlusion_query
->context
= NULL
;
1130 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
1134 if (gl_info
->supported
[ARB_SYNC
])
1136 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
1138 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
1139 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
1141 event_query
->context
= NULL
;
1144 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
1146 if (!context
->valid
) entry
->id
= 0;
1147 context_destroy_fbo_entry(context
, entry
);
1150 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1152 if (!context
->valid
) entry
->id
= 0;
1153 context_destroy_fbo_entry(context
, entry
);
1158 if (context
->dummy_arbfp_prog
)
1160 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
1163 if (gl_info
->supported
[ARB_TIMER_QUERY
])
1164 GL_EXTCALL(glDeleteQueries(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
1166 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1167 GL_EXTCALL(glDeleteQueries(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
1169 if (gl_info
->supported
[ARB_SYNC
])
1171 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1173 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
1176 else if (gl_info
->supported
[APPLE_FENCE
])
1178 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1180 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1183 else if (gl_info
->supported
[NV_FENCE
])
1185 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1187 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1191 checkGLcall("context cleanup");
1194 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1195 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1196 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1198 context_restore_pixel_format(context
);
1201 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1203 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1205 ERR("Failed to disable GL context.\n");
1208 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1210 if (!wglDeleteContext(context
->glCtx
))
1212 DWORD err
= GetLastError();
1213 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1217 DWORD
context_get_tls_idx(void)
1219 return wined3d_context_tls_idx
;
1222 void context_set_tls_idx(DWORD idx
)
1224 wined3d_context_tls_idx
= idx
;
1227 struct wined3d_context
*context_get_current(void)
1229 return TlsGetValue(wined3d_context_tls_idx
);
1232 BOOL
context_set_current(struct wined3d_context
*ctx
)
1234 struct wined3d_context
*old
= context_get_current();
1238 TRACE("Already using D3D context %p.\n", ctx
);
1246 TRACE("Switching away from destroyed context %p.\n", old
);
1247 context_destroy_gl_resources(old
);
1248 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1249 HeapFree(GetProcessHeap(), 0, old
);
1253 if (wglGetCurrentContext())
1255 TRACE("Flushing context %p before switching to %p.\n", old
, ctx
);
1266 ERR("Trying to make invalid context %p current\n", ctx
);
1270 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1271 if (!context_set_gl_context(ctx
))
1275 else if(wglGetCurrentContext())
1277 TRACE("Clearing current D3D context.\n");
1278 if (!wglMakeCurrent(NULL
, NULL
))
1280 DWORD err
= GetLastError();
1281 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1282 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1287 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1290 void context_release(struct wined3d_context
*context
)
1292 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1296 if (!context
->level
)
1297 WARN("Context %p is not active.\n", context
);
1298 else if (context
!= context_get_current())
1299 WARN("Context %p is not the current context.\n", context
);
1302 if (!--context
->level
)
1304 if (context_restore_pixel_format(context
))
1305 context
->needs_set
= 1;
1306 if (context
->restore_ctx
)
1308 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1309 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
);
1310 context
->restore_ctx
= NULL
;
1311 context
->restore_dc
= NULL
;
1316 /* This is used when a context for render target A is active, but a separate context is
1317 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1318 * A to avoid breaking caller code. */
1319 void context_restore(struct wined3d_context
*context
, struct wined3d_surface
*restore
)
1321 if (context
->current_rt
!= restore
)
1323 context_release(context
);
1324 context
= context_acquire(restore
->container
->resource
.device
, restore
);
1327 context_release(context
);
1330 static void context_enter(struct wined3d_context
*context
)
1332 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1334 if (!context
->level
++)
1336 const struct wined3d_context
*current_context
= context_get_current();
1337 HGLRC current_gl
= wglGetCurrentContext();
1339 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1341 TRACE("Another GL context (%p on device context %p) is already current.\n",
1342 current_gl
, wglGetCurrentDC());
1343 context
->restore_ctx
= current_gl
;
1344 context
->restore_dc
= wglGetCurrentDC();
1345 context
->needs_set
= 1;
1347 else if (!context
->needs_set
&& !(context
->hdc_is_private
&& context
->hdc_has_format
)
1348 && context
->pixel_format
!= context
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context
->hdc
))
1349 context
->needs_set
= 1;
1353 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1355 DWORD rep
= context
->state_table
[state
].representative
;
1359 if (isStateDirty(context
, rep
)) return;
1361 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1362 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1363 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1364 context
->isStateDirty
[idx
] |= (1u << shift
);
1367 /* This function takes care of wined3d pixel format selection. */
1368 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1369 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1370 BOOL auxBuffers
, BOOL findCompatible
)
1373 unsigned int current_value
;
1374 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1377 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1378 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1379 auxBuffers
, findCompatible
);
1382 for (i
= 0; i
< cfg_count
; ++i
)
1384 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1387 /* For now only accept RGBA formats. Perhaps some day we will
1388 * allow floating point formats for pbuffers. */
1389 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1391 /* In window mode we need a window drawable format and double buffering. */
1392 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1394 if (cfg
->redSize
< color_format
->red_size
)
1396 if (cfg
->greenSize
< color_format
->green_size
)
1398 if (cfg
->blueSize
< color_format
->blue_size
)
1400 if (cfg
->alphaSize
< color_format
->alpha_size
)
1402 if (cfg
->depthSize
< ds_format
->depth_size
)
1404 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1406 /* Check multisampling support. */
1407 if (cfg
->numSamples
)
1411 /* We try to locate a format which matches our requirements exactly. In case of
1412 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1413 if (cfg
->depthSize
== ds_format
->depth_size
)
1415 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1417 if (cfg
->alphaSize
== color_format
->alpha_size
)
1419 /* We like to have aux buffers in backbuffer mode */
1420 if (auxBuffers
&& cfg
->auxBuffers
)
1422 if (cfg
->redSize
== color_format
->red_size
1423 && cfg
->greenSize
== color_format
->green_size
1424 && cfg
->blueSize
== color_format
->blue_size
)
1427 if (value
> current_value
)
1429 iPixelFormat
= cfg
->iPixelFormat
;
1430 current_value
= value
;
1434 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1435 if(!iPixelFormat
&& !findCompatible
) {
1436 ERR("Can't find a suitable iPixelFormat\n");
1438 } else if(!iPixelFormat
) {
1439 PIXELFORMATDESCRIPTOR pfd
;
1441 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1442 /* PixelFormat selection */
1443 ZeroMemory(&pfd
, sizeof(pfd
));
1444 pfd
.nSize
= sizeof(pfd
);
1446 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1447 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1448 pfd
.cAlphaBits
= color_format
->alpha_size
;
1449 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1450 + color_format
->blue_size
+ color_format
->alpha_size
;
1451 pfd
.cDepthBits
= ds_format
->depth_size
;
1452 pfd
.cStencilBits
= ds_format
->stencil_size
;
1453 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1455 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1457 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1458 ERR("Can't find a suitable iPixelFormat\n");
1463 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1464 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1465 return iPixelFormat
;
1468 /* Context activation is done by the caller. */
1469 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1471 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1472 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1474 for (i
= 0; i
< count
; ++i
)
1476 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1477 checkGLcall("glActiveTexture");
1479 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1480 checkGLcall("glBindTexture");
1482 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1484 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1485 checkGLcall("glBindTexture");
1488 if (gl_info
->supported
[EXT_TEXTURE3D
])
1490 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1491 checkGLcall("glBindTexture");
1494 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1496 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1497 checkGLcall("glBindTexture");
1502 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1504 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1505 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1508 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1509 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1513 case GL_DEBUG_TYPE_ERROR_ARB
:
1514 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1517 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1518 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1519 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1520 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1523 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1524 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1528 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1533 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1536 unsigned int ctx_attrib_idx
= 0;
1537 GLint ctx_attribs
[7], ctx_flags
= 0;
1539 if (context_debug_output_enabled(gl_info
))
1540 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1541 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1542 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1543 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1544 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1545 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1546 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1549 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1550 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1552 ctx_attribs
[ctx_attrib_idx
] = 0;
1554 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1556 if (ctx_flags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
1558 ctx_attribs
[ctx_attrib_idx
- 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1559 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1560 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1567 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1568 struct wined3d_texture
*target
, const struct wined3d_format
*ds_format
)
1570 struct wined3d_device
*device
= swapchain
->device
;
1571 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1572 const struct wined3d_format
*color_format
;
1573 struct wined3d_context
*ret
;
1574 BOOL auxBuffers
= FALSE
;
1575 HGLRC ctx
, share_ctx
;
1581 BOOL hdc_is_private
= FALSE
;
1583 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1585 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1589 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1590 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1591 if (!ret
->blit_targets
)
1594 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1595 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1596 if (!ret
->draw_buffers
)
1599 ret
->fbo_key
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1600 FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[gl_info
->limits
.buffers
+ 1]));
1604 ret
->free_timestamp_query_size
= 4;
1605 ret
->free_timestamp_queries
= HeapAlloc(GetProcessHeap(), 0,
1606 ret
->free_timestamp_query_size
* sizeof(*ret
->free_timestamp_queries
));
1607 if (!ret
->free_timestamp_queries
)
1609 list_init(&ret
->timestamp_queries
);
1611 ret
->free_occlusion_query_size
= 4;
1612 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1613 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1614 if (!ret
->free_occlusion_queries
)
1617 list_init(&ret
->occlusion_queries
);
1619 ret
->free_event_query_size
= 4;
1620 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1621 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1622 if (!ret
->free_event_queries
)
1625 list_init(&ret
->event_queries
);
1626 list_init(&ret
->fbo_list
);
1627 list_init(&ret
->fbo_destroy_list
);
1629 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1631 ERR("Failed to allocate shader backend context data.\n");
1634 if (!device
->adapter
->fragment_pipe
->allocate_context_data(ret
))
1636 ERR("Failed to allocate fragment pipeline context data.\n");
1640 /* Initialize the texture unit mapping to a 1:1 mapping */
1641 for (s
= 0; s
< MAX_COMBINED_SAMPLERS
; ++s
)
1643 if (s
< gl_info
->limits
.combined_samplers
)
1645 ret
->tex_unit_map
[s
] = s
;
1646 ret
->rev_tex_unit_map
[s
] = s
;
1650 ret
->tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1651 ret
->rev_tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1655 if (!(hdc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1657 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1659 if ((hdc
= swapchain_get_backup_dc(swapchain
)))
1660 hdc_is_private
= TRUE
;
1663 ERR("Failed to retrieve a device context.\n");
1668 color_format
= target
->resource
.format
;
1670 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1671 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1672 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1676 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1677 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1678 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1679 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1682 /* DirectDraw supports 8bit paletted render targets and these are used by
1683 * old games like StarCraft and C&C. Most modern hardware doesn't support
1684 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1685 * conversion (ab)uses the alpha component for storing the palette index.
1686 * For this reason we require a format with 8bit alpha, so request
1688 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1689 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1691 /* When "always_offscreen" is enabled, we only use the drawable for
1692 * presentation blits, and don't do any rendering to it. That means we
1693 * don't need depth or stencil buffers, and can mostly ignore the render
1694 * target format. This wouldn't necessarily be quite correct for 10bpc
1695 * display modes, but we don't currently support those.
1696 * Using the same format regardless of the color/depth/stencil targets
1697 * makes it much less likely that different wined3d instances will set
1698 * conflicting pixel formats. */
1699 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
1700 && wined3d_settings
.always_offscreen
)
1702 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1703 ds_format
= wined3d_get_format(gl_info
, WINED3DFMT_UNKNOWN
);
1706 /* Try to find a pixel format which matches our requirements. */
1707 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1709 /* Try to locate a compatible format if we weren't able to find anything. */
1712 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1713 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1716 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1719 ERR("Can't find a suitable pixel format.\n");
1723 ret
->gl_info
= gl_info
;
1727 if (!context_set_pixel_format(ret
, hdc
, hdc_is_private
, pixel_format
))
1729 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1730 context_release(ret
);
1734 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1735 if (gl_info
->p_wglCreateContextAttribsARB
)
1737 if (!(ctx
= context_create_wgl_attribs(gl_info
, hdc
, share_ctx
)))
1742 if (!(ctx
= wglCreateContext(hdc
)))
1744 ERR("Failed to create a WGL context.\n");
1745 context_release(ret
);
1749 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1751 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1752 context_release(ret
);
1753 if (!wglDeleteContext(ctx
))
1754 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1759 if (!device_context_add(device
, ret
))
1761 ERR("Failed to add the newly created context to the context list\n");
1762 context_release(ret
);
1763 if (!wglDeleteContext(ctx
))
1764 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1768 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1769 ret
->state_table
= device
->StateTable
;
1771 /* Mark all states dirty to force a proper initialization of the states
1772 * on the first use of the context. */
1773 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1775 if (ret
->state_table
[state
].representative
)
1776 context_invalidate_state(ret
, state
);
1779 ret
->swapchain
= swapchain
;
1780 ret
->current_rt
= target
->sub_resources
[0].u
.surface
;
1781 ret
->tid
= GetCurrentThreadId();
1783 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->resource
);
1784 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1788 ret
->win_handle
= swapchain
->win_handle
;
1790 ret
->hdc_is_private
= hdc_is_private
;
1791 ret
->hdc_has_format
= TRUE
;
1792 ret
->pixel_format
= pixel_format
;
1795 /* Set up the context defaults */
1796 if (!context_set_current(ret
))
1798 ERR("Cannot activate context to set up defaults.\n");
1799 device_context_remove(device
, ret
);
1800 context_release(ret
);
1801 if (!wglDeleteContext(ctx
))
1802 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1806 if (context_debug_output_enabled(gl_info
))
1808 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, ret
));
1809 if (TRACE_ON(d3d_synchronous
))
1810 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
1811 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1814 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
1815 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1819 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
1820 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1821 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
1822 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1823 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
1824 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1826 if (WARN_ON(d3d_perf
))
1828 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
1829 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1833 switch (swapchain
->desc
.swap_interval
)
1835 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1838 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1839 case WINED3DPRESENT_INTERVAL_ONE
:
1842 case WINED3DPRESENT_INTERVAL_TWO
:
1845 case WINED3DPRESENT_INTERVAL_THREE
:
1848 case WINED3DPRESENT_INTERVAL_FOUR
:
1852 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1856 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1858 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1859 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1860 swap_interval
, ret
, GetLastError());
1863 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1865 TRACE("Setting up the screen\n");
1867 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1868 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1870 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1871 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1873 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1874 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1876 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1877 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1878 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
1879 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1881 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
1885 GL_EXTCALL(glGenVertexArrays(1, &vao
));
1886 GL_EXTCALL(glBindVertexArray(vao
));
1887 checkGLcall("creating VAO");
1890 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1892 /* Direct3D always uses n-1 weights for n world matrices and uses
1893 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1894 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1895 * enabled as well. */
1896 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1897 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1899 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1901 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1902 * the previous texture where to source the offset from is always unit - 1.
1904 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1906 context_active_texture(ret
, gl_info
, s
);
1907 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1908 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1909 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1912 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1914 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1915 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1916 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1917 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1920 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1921 * program and the dummy program is destroyed when the context is destroyed.
1923 static const char dummy_program
[] =
1925 "MOV result.color, fragment.color.primary;\n"
1927 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1928 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1929 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1932 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1934 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1936 context_active_texture(ret
, gl_info
, s
);
1937 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1938 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1942 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1944 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1946 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1948 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1950 device
->shader_backend
->shader_init_context_state(ret
);
1951 ret
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
1952 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
1953 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
);
1955 /* If this happens to be the first context for the device, dummy textures
1956 * are not created yet. In that case, they will be created (and bound) by
1957 * create_dummy_textures right after this context is initialized. */
1958 if (device
->dummy_texture_2d
[0])
1959 bind_dummy_textures(device
, ret
);
1961 TRACE("Created context %p.\n", ret
);
1966 if (hdc
) wined3d_release_dc(swapchain
->win_handle
, hdc
);
1967 device
->shader_backend
->shader_free_context_data(ret
);
1968 device
->adapter
->fragment_pipe
->free_context_data(ret
);
1969 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1970 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1971 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
1972 HeapFree(GetProcessHeap(), 0, ret
->fbo_key
);
1973 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1974 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1975 HeapFree(GetProcessHeap(), 0, ret
);
1979 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1983 TRACE("Destroying ctx %p\n", context
);
1985 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1987 context_destroy_gl_resources(context
);
1988 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1993 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1994 in wined3d_adapter may go away in the meantime */
1995 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
1996 *gl_info
= *context
->gl_info
;
1997 context
->gl_info
= gl_info
;
1998 context
->destroyed
= 1;
2002 device
->shader_backend
->shader_free_context_data(context
);
2003 device
->adapter
->fragment_pipe
->free_context_data(context
);
2004 HeapFree(GetProcessHeap(), 0, context
->fbo_key
);
2005 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
2006 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
2007 device_context_remove(device
, context
);
2008 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
2011 /* Context activation is done by the caller. */
2012 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
2014 const GLdouble projection
[] =
2016 2.0 / width
, 0.0, 0.0, 0.0,
2017 0.0, 2.0 / height
, 0.0, 0.0,
2019 -1.0, -1.0, -1.0, 1.0,
2022 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
2023 checkGLcall("glMatrixMode(GL_PROJECTION)");
2024 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
2025 checkGLcall("glLoadMatrixd");
2026 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
2027 checkGLcall("glViewport");
2030 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
2032 const struct wined3d_surface
*rt
= context
->current_rt
;
2034 if (rt
->container
->swapchain
&& rt
->container
->swapchain
->front_buffer
== rt
->container
)
2038 GetClientRect(context
->win_handle
, &window_size
);
2039 size
->cx
= window_size
.right
- window_size
.left
;
2040 size
->cy
= window_size
.bottom
- window_size
.top
;
2045 size
->cx
= rt
->resource
.width
;
2046 size
->cy
= rt
->resource
.height
;
2049 /*****************************************************************************
2052 * Sets up a context for DirectDraw blitting.
2053 * All texture units are disabled, texture unit 0 is set as current unit
2054 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
2055 * color writing enabled for all channels
2056 * register combiners disabled, shaders disabled
2057 * world matrix is set to identity, texture matrix 0 too
2058 * projection matrix is setup for drawing screen coordinates
2061 * This: Device to activate the context for
2062 * context: Context to setup
2064 *****************************************************************************/
2065 /* Context activation is done by the caller. */
2066 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
2069 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2073 TRACE("Setting up context %p for blitting\n", context
);
2075 context_get_rt_size(context
, &rt_size
);
2077 if (context
->last_was_blit
)
2079 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
2081 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2082 context
->blit_w
= rt_size
.cx
;
2083 context
->blit_h
= rt_size
.cy
;
2084 /* No need to dirtify here, the states are still dirtified because
2085 * they weren't applied since the last SetupForBlit() call. */
2087 TRACE("Context is already set up for blitting, nothing to do\n");
2090 context
->last_was_blit
= TRUE
;
2092 /* Disable all textures. The caller can then bind a texture it wants to blit
2095 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
2096 * function texture unit. No need to care for higher samplers
2098 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
2100 sampler
= context
->rev_tex_unit_map
[i
];
2101 context_active_texture(context
, gl_info
, i
);
2103 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2105 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2106 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2108 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2109 checkGLcall("glDisable GL_TEXTURE_3D");
2110 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2112 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2113 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2115 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2116 checkGLcall("glDisable GL_TEXTURE_2D");
2118 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2119 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
2121 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2123 if (sampler
< MAX_TEXTURES
)
2124 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2125 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2128 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
2129 GL_EXTCALL(glBindSampler(0, 0));
2130 context_active_texture(context
, gl_info
, 0);
2132 sampler
= context
->rev_tex_unit_map
[0];
2134 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2136 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2137 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2139 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2140 checkGLcall("glDisable GL_TEXTURE_3D");
2141 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2143 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2144 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2146 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2147 checkGLcall("glDisable GL_TEXTURE_2D");
2149 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2151 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
2152 checkGLcall("glMatrixMode(GL_TEXTURE)");
2153 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2154 checkGLcall("glLoadIdentity()");
2156 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
2158 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
2159 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
2160 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2163 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2165 if (sampler
< MAX_TEXTURES
)
2167 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
2168 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2170 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2173 /* Other misc states */
2174 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2175 checkGLcall("glDisable(GL_ALPHA_TEST)");
2176 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
2177 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
2178 checkGLcall("glDisable GL_LIGHTING");
2179 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
2180 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
2181 checkGLcall("glDisable GL_DEPTH_TEST");
2182 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
2183 glDisableWINE(GL_FOG
);
2184 checkGLcall("glDisable GL_FOG");
2185 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
2186 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2187 checkGLcall("glDisable GL_BLEND");
2188 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2189 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
2190 checkGLcall("glDisable GL_CULL_FACE");
2191 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
2192 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
2193 checkGLcall("glDisable GL_STENCIL_TEST");
2194 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
2195 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
2196 checkGLcall("glDisable GL_SCISSOR_TEST");
2197 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2198 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2200 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
2201 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2202 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
2204 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
2205 checkGLcall("glColorMask");
2206 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
2207 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
2208 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
2209 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2210 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2212 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2213 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2214 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2217 /* Setup transforms */
2218 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2219 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2220 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2221 checkGLcall("glLoadIdentity()");
2222 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2224 context
->last_was_rhw
= TRUE
;
2225 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2227 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2228 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2229 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2230 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2231 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2232 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2233 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2235 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2237 /* Disable shaders */
2238 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2240 context
->blit_w
= rt_size
.cx
;
2241 context
->blit_h
= rt_size
.cy
;
2242 context_invalidate_state(context
, STATE_VIEWPORT
);
2243 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2246 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2248 return rt_mask
& (1u << 31);
2251 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2253 return rt_mask
& ~(1u << 31);
2256 /* Context activation is done by the caller. */
2257 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2259 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2263 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2264 checkGLcall("glDrawBuffer()");
2266 else if (is_rt_mask_onscreen(rt_mask
))
2268 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2269 checkGLcall("glDrawBuffer()");
2273 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2280 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2282 context
->draw_buffers
[i
] = GL_NONE
;
2288 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2290 GL_EXTCALL(glDrawBuffers(i
, context
->draw_buffers
));
2291 checkGLcall("glDrawBuffers()");
2295 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2296 checkGLcall("glDrawBuffer()");
2301 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2306 /* Context activation is done by the caller. */
2307 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2309 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2310 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2311 DWORD new_mask
= context_generate_rt_mask(buffer
);
2313 if (new_mask
== *current_mask
)
2316 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2317 checkGLcall("glDrawBuffer()");
2319 *current_mask
= new_mask
;
2322 /* Context activation is done by the caller. */
2323 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2325 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2326 checkGLcall("glActiveTexture");
2327 context
->active_texture
= unit
;
2330 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2332 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2333 DWORD unit
= context
->active_texture
;
2334 DWORD old_texture_type
= context
->texture_type
[unit
];
2338 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2339 checkGLcall("glBindTexture");
2346 if (old_texture_type
!= target
)
2348 const struct wined3d_device
*device
= context
->swapchain
->device
;
2350 switch (old_texture_type
)
2356 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2357 checkGLcall("glBindTexture");
2359 case GL_TEXTURE_RECTANGLE_ARB
:
2360 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2361 checkGLcall("glBindTexture");
2363 case GL_TEXTURE_CUBE_MAP
:
2364 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2365 checkGLcall("glBindTexture");
2368 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2369 checkGLcall("glBindTexture");
2372 ERR("Unexpected texture target %#x\n", old_texture_type
);
2375 context
->texture_type
[unit
] = target
;
2379 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2381 if (context
->render_offscreen
== offscreen
) return;
2383 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2384 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2385 context_invalidate_state(context
, STATE_VIEWPORT
);
2386 context_invalidate_state(context
, STATE_SCISSORRECT
);
2387 context_invalidate_state(context
, STATE_FRONTFACE
);
2388 context
->render_offscreen
= offscreen
;
2391 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2392 const struct wined3d_format
*required
)
2394 if (existing
== required
)
2396 if ((existing
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
)
2397 != (required
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
))
2399 if (existing
->depth_size
< required
->depth_size
)
2401 /* If stencil bits are used the exact amount is required - otherwise
2402 * wrapping won't work correctly. */
2403 if (required
->stencil_size
&& required
->stencil_size
!= existing
->stencil_size
)
2408 /* Context activation is done by the caller. */
2409 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2410 const struct wined3d_rendertarget_view
*depth_stencil
)
2412 /* Onscreen surfaces are always in a swapchain */
2413 struct wined3d_swapchain
*swapchain
= context
->current_rt
->container
->swapchain
;
2415 if (context
->render_offscreen
|| !depth_stencil
) return;
2416 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2418 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2419 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2421 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2423 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2424 surface_load_location(context
->current_rt
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
2425 swapchain
->render_to_fbo
= TRUE
;
2426 swapchain_update_draw_bindings(swapchain
);
2427 context_set_render_offscreen(context
, TRUE
);
2430 GLenum
context_get_offscreen_gl_buffer(const struct wined3d_context
*context
)
2432 switch (wined3d_settings
.offscreen_rendering_mode
)
2435 return GL_COLOR_ATTACHMENT0
;
2437 case ORM_BACKBUFFER
:
2438 return context
->aux_buffers
> 0 ? GL_AUX0
: GL_BACK
;
2441 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
2446 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_context
*context
, const struct wined3d_surface
*rt
)
2448 if (!rt
|| rt
->container
->resource
.format
->id
== WINED3DFMT_NULL
)
2450 else if (rt
->container
->swapchain
)
2451 return context_generate_rt_mask_from_resource(&rt
->container
->resource
);
2453 return context_generate_rt_mask(context_get_offscreen_gl_buffer(context
));
2456 /* Context activation is done by the caller. */
2457 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2459 struct wined3d_surface
*rt
= context
->current_rt
;
2460 DWORD rt_mask
, *cur_mask
;
2462 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2464 context_validate_onscreen_formats(context
, NULL
);
2466 if (context
->render_offscreen
)
2468 wined3d_texture_load(rt
->container
, context
, FALSE
);
2470 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->container
->resource
.draw_binding
);
2471 if (rt
->container
->resource
.format
->id
!= WINED3DFMT_NULL
)
2478 context
->current_fbo
= NULL
;
2479 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2480 rt_mask
= context_generate_rt_mask_from_resource(&rt
->container
->resource
);
2485 rt_mask
= context_generate_rt_mask_no_fbo(context
, rt
);
2488 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2490 if (rt_mask
!= *cur_mask
)
2492 context_apply_draw_buffers(context
, rt_mask
);
2493 *cur_mask
= rt_mask
;
2496 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2498 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2501 SetupForBlit(device
, context
);
2502 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2505 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2506 const struct wined3d_rendertarget_view
*ds
)
2510 if (ds
) return TRUE
;
2512 for (i
= 0; i
< rt_count
; ++i
)
2514 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2518 WARN("Invalid render target config, need at least one attachment.\n");
2522 /* Context activation is done by the caller. */
2523 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2524 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2526 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2527 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
2528 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2529 DWORD rt_mask
= 0, *cur_mask
;
2532 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2533 || rt_count
!= context
->gl_info
->limits
.buffers
)
2535 if (!context_validate_rt_config(rt_count
, rts
, dsv
))
2538 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2540 context_validate_onscreen_formats(context
, dsv
);
2542 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2544 for (i
= 0; i
< rt_count
; ++i
)
2546 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(rts
[i
]);
2547 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2548 rt_mask
|= (1u << i
);
2550 while (i
< context
->gl_info
->limits
.buffers
)
2552 context
->blit_targets
[i
] = NULL
;
2555 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2556 wined3d_rendertarget_view_get_surface(dsv
),
2557 rt_count
? rts
[0]->resource
->draw_binding
: 0,
2558 dsv
? dsv
->resource
->draw_binding
: 0);
2562 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2563 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2564 rt_mask
= context_generate_rt_mask_from_resource(rts
[0]->resource
);
2567 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2568 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2569 * state management allows this */
2570 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2574 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2575 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2578 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2579 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2581 for (i
= 0; i
< rt_count
; ++i
)
2583 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2584 rt_mask
|= (1u << i
);
2589 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2590 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2593 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2595 if (rt_mask
!= *cur_mask
)
2597 context_apply_draw_buffers(context
, rt_mask
);
2598 *cur_mask
= rt_mask
;
2599 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2602 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2604 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2607 if (context
->last_was_blit
)
2608 context
->last_was_blit
= FALSE
;
2610 /* Blending and clearing should be orthogonal, but tests on the nvidia
2611 * driver show that disabling blending when clearing improves the clearing
2612 * performance incredibly. */
2613 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2614 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2615 if (rt_count
&& gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
2617 if (needs_srgb_write(context
, &device
->state
, fb
))
2618 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
2620 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
2621 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
2623 checkGLcall("setting up state for clear");
2625 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2626 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2627 context_invalidate_state(context
, STATE_SCISSORRECT
);
2632 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_state
*state
)
2634 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
2635 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2636 DWORD rt_mask
, rt_mask_bits
;
2639 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2640 return context_generate_rt_mask_no_fbo(context
, wined3d_rendertarget_view_get_surface(rts
[0]));
2641 else if (!context
->render_offscreen
)
2642 return context_generate_rt_mask_from_resource(rts
[0]->resource
);
2644 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2645 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2646 rt_mask_bits
= rt_mask
;
2648 while (rt_mask_bits
)
2650 rt_mask_bits
&= ~(1u << i
);
2651 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
2652 rt_mask
&= ~(1u << i
);
2660 /* Context activation is done by the caller. */
2661 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2663 DWORD rt_mask
= find_draw_buffers_mask(context
, state
);
2664 const struct wined3d_fb_state
*fb
= state
->fb
;
2667 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2669 if (!context
->render_offscreen
)
2671 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2672 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2678 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
2680 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(fb
->render_targets
[i
]);
2682 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2683 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2684 fb
->render_targets
[0] ? fb
->render_targets
[0]->resource
->draw_binding
: 0,
2685 fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
2689 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2690 if (rt_mask
!= *cur_mask
)
2692 context_apply_draw_buffers(context
, rt_mask
);
2693 *cur_mask
= rt_mask
;
2697 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
2699 DWORD i
= context
->rev_tex_unit_map
[unit
];
2700 DWORD j
= context
->tex_unit_map
[stage
];
2702 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
2703 context
->tex_unit_map
[stage
] = unit
;
2704 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2705 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2707 context
->rev_tex_unit_map
[unit
] = stage
;
2708 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2709 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2712 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
2716 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2717 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
2720 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
2721 const struct wined3d_state
*state
)
2725 context
->fixed_function_usage_map
= 0;
2726 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2728 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2729 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2730 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2731 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2732 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2733 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2734 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2735 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2737 /* Not used, and disable higher stages. */
2738 if (color_op
== WINED3D_TOP_DISABLE
)
2741 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2742 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2743 || ((color_arg3
== WINED3DTA_TEXTURE
)
2744 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2745 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2746 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2747 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2748 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2749 context
->fixed_function_usage_map
|= (1u << i
);
2751 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2752 && i
< MAX_TEXTURES
- 1)
2753 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
2756 if (i
< context
->lowest_disabled_stage
)
2759 end
= context
->lowest_disabled_stage
;
2763 start
= context
->lowest_disabled_stage
;
2767 context
->lowest_disabled_stage
= i
;
2768 for (i
= start
+ 1; i
< end
; ++i
)
2770 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
2774 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
2775 const struct wined3d_state
*state
)
2777 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2778 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2779 unsigned int i
, tex
;
2782 context_update_fixed_function_usage_map(context
, state
);
2784 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2787 ffu_map
= context
->fixed_function_usage_map
;
2789 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2790 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2792 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2797 if (context
->tex_unit_map
[i
] != i
)
2799 context_map_stage(context
, i
, i
);
2800 context_invalidate_state(context
, STATE_SAMPLER(i
));
2801 context_invalidate_texture_stage(context
, i
);
2807 /* Now work out the mapping */
2809 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2814 if (context
->tex_unit_map
[i
] != tex
)
2816 context_map_stage(context
, i
, tex
);
2817 context_invalidate_state(context
, STATE_SAMPLER(i
));
2818 context_invalidate_texture_stage(context
, i
);
2825 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2827 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2828 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2829 const struct wined3d_shader_resource_info
*resource_info
=
2830 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2833 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2836 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2838 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
2840 context_map_stage(context
, i
, i
);
2841 context_invalidate_state(context
, STATE_SAMPLER(i
));
2842 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2843 context_invalidate_texture_stage(context
, i
);
2848 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
2849 const struct wined3d_shader_resource_info
*ps_resource_info
, DWORD unit
)
2851 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
2853 /* Not currently used */
2854 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
2857 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
2859 /* Used by a fragment sampler */
2861 if (!ps_resource_info
)
2863 /* No pixel shader, check fixed function */
2864 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1u << current_mapping
));
2867 /* Pixel shader, check the shader's sampler map */
2868 return !ps_resource_info
[current_mapping
].type
;
2874 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
2876 const struct wined3d_shader_resource_info
*vs_resource_info
=
2877 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
2878 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
2879 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2880 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2883 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2886 /* Note that we only care if a resource is used or not, not the
2887 * resource's specific type. Otherwise we'd need to call
2888 * shader_update_samplers() here for 1.x pixelshaders. */
2890 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2892 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
2894 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2895 if (vs_resource_info
[i
].type
)
2899 if (context_unit_free_for_vs(context
, ps_resource_info
, start
))
2901 if (context
->tex_unit_map
[vsampler_idx
] != start
)
2903 context_map_stage(context
, vsampler_idx
, start
);
2904 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
2913 if (context
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
2914 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
2919 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2921 BOOL vs
= use_vs(state
);
2922 BOOL ps
= use_ps(state
);
2924 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
2925 * need a 1:1 map at the moment.
2926 * When the mapping of a stage is changed, sampler and ALL texture stage
2927 * states have to be reset. */
2930 context_map_psamplers(context
, state
);
2932 context_map_fixed_function_samplers(context
, state
);
2935 context_map_vsamplers(context
, ps
, state
);
2938 /* Context activation is done by the caller. */
2939 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2941 DWORD rt_mask
, *cur_mask
;
2943 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2945 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2946 rt_mask
= find_draw_buffers_mask(context
, state
);
2947 if (rt_mask
!= *cur_mask
)
2949 context_apply_draw_buffers(context
, rt_mask
);
2950 *cur_mask
= rt_mask
;
2954 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
2956 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
2957 *regnum
= WINED3D_FFP_POSITION
;
2958 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
2959 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
2960 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
2961 *regnum
= WINED3D_FFP_BLENDINDICES
;
2962 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
2963 *regnum
= WINED3D_FFP_NORMAL
;
2964 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
2965 *regnum
= WINED3D_FFP_PSIZE
;
2966 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
2967 *regnum
= WINED3D_FFP_DIFFUSE
;
2968 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
2969 *regnum
= WINED3D_FFP_SPECULAR
;
2970 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
2971 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
2974 FIXME("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
2982 /* Context activation is done by the caller. */
2983 void context_stream_info_from_declaration(struct wined3d_context
*context
,
2984 const struct wined3d_state
*state
, struct wined3d_stream_info
*stream_info
)
2986 /* We need to deal with frequency data! */
2987 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
2988 BOOL use_vshader
= use_vs(state
);
2989 BOOL generic_attributes
= context
->d3d_info
->ffp_generic_attributes
;
2992 stream_info
->use_map
= 0;
2993 stream_info
->swizzle_map
= 0;
2994 stream_info
->position_transformed
= declaration
->position_transformed
;
2996 /* Translate the declaration into strided data. */
2997 for (i
= 0; i
< declaration
->element_count
; ++i
)
2999 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
3000 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
3004 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
3005 element
, i
+ 1, declaration
->element_count
);
3007 if (!stream
->buffer
)
3010 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
3014 if (element
->output_slot
== WINED3D_OUTPUT_SLOT_UNUSED
)
3016 stride_used
= FALSE
;
3018 else if (element
->output_slot
== WINED3D_OUTPUT_SLOT_SEMANTIC
)
3020 /* TODO: Assuming vertexdeclarations are usually used with the
3021 * same or a similar shader, it might be worth it to store the
3022 * last used output slot and try that one first. */
3023 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
3024 element
->usage
, element
->usage_idx
, &idx
);
3028 idx
= element
->output_slot
;
3034 if (!generic_attributes
&& !element
->ffp_valid
)
3036 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3037 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
3038 stride_used
= FALSE
;
3042 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
3048 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3049 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3050 use_vshader
? "shader": "fixed function", idx
,
3051 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
3052 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
),
3053 debug_d3dinput_classification(element
->input_slot_class
), element
->instance_data_step_rate
);
3055 stream_info
->elements
[idx
].format
= element
->format
;
3056 stream_info
->elements
[idx
].data
.buffer_object
= 0;
3057 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
3058 stream_info
->elements
[idx
].stride
= stream
->stride
;
3059 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
3060 if (stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
3062 stream_info
->elements
[idx
].divisor
= 1;
3064 else if (element
->input_slot_class
== WINED3D_INPUT_PER_INSTANCE_DATA
)
3066 stream_info
->elements
[idx
].divisor
= element
->instance_data_step_rate
;
3067 if (!element
->instance_data_step_rate
)
3068 FIXME("Instance step rate 0 not implemented.\n");
3072 stream_info
->elements
[idx
].divisor
= 0;
3075 if (!context
->gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3076 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
3078 stream_info
->swizzle_map
|= 1u << idx
;
3080 stream_info
->use_map
|= 1u << idx
;
3085 /* Context activation is done by the caller. */
3086 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3088 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3089 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3090 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
3091 DWORD prev_all_vbo
= stream_info
->all_vbo
;
3095 context_stream_info_from_declaration(context
, state
, stream_info
);
3097 stream_info
->all_vbo
= 1;
3098 context
->num_buffer_queries
= 0;
3099 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
3101 struct wined3d_stream_info_element
*element
;
3102 struct wined3d_bo_address data
;
3103 struct wined3d_buffer
*buffer
;
3108 element
= &stream_info
->elements
[i
];
3109 buffer
= state
->streams
[element
->stream_idx
].buffer
;
3111 /* We can't use VBOs if the base vertex index is negative. OpenGL
3112 * doesn't accept negative offsets (or rather offsets bigger than the
3113 * VBO, because the pointer is unsigned), so use system memory
3114 * sources. In most sane cases the pointer - offset will still be > 0,
3115 * otherwise it will wrap around to some big value. Hope that with the
3116 * indices the driver wraps it back internally. If not,
3117 * drawStridedSlow is needed, including a vertex buffer path. */
3118 if (state
->load_base_vertex_index
< 0)
3120 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3121 state
->load_base_vertex_index
);
3122 element
->data
.buffer_object
= 0;
3123 element
->data
.addr
+= (ULONG_PTR
)buffer_get_sysmem(buffer
, context
);
3124 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
3125 FIXME("System memory vertex data load offset is negative!\n");
3129 buffer_internal_preload(buffer
, context
, state
);
3130 buffer_get_memory(buffer
, context
, &data
);
3131 element
->data
.buffer_object
= data
.buffer_object
;
3132 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
3135 if (!element
->data
.buffer_object
)
3136 stream_info
->all_vbo
= 0;
3139 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
3141 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
3146 if (state
->vertex_declaration
->half_float_conv_needed
&& !stream_info
->all_vbo
)
3148 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
3149 context
->use_immediate_mode_draw
= TRUE
;
3153 context
->use_immediate_mode_draw
= FALSE
;
3158 WORD slow_mask
= -!d3d_info
->ffp_generic_attributes
& (1u << WINED3D_FFP_PSIZE
);
3159 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3160 & ((1u << WINED3D_FFP_DIFFUSE
) | (1u << WINED3D_FFP_SPECULAR
));
3162 if (((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
3163 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
3164 context
->use_immediate_mode_draw
= TRUE
;
3166 context
->use_immediate_mode_draw
= FALSE
;
3169 if (prev_all_vbo
!= stream_info
->all_vbo
)
3170 context_invalidate_state(context
, STATE_INDEXBUFFER
);
3173 /* Context activation is done by the caller. */
3174 static void context_preload_texture(struct wined3d_context
*context
,
3175 const struct wined3d_state
*state
, unsigned int idx
)
3177 struct wined3d_texture
*texture
;
3179 if (!(texture
= state
->textures
[idx
]))
3182 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
3185 /* Context activation is done by the caller. */
3186 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3192 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3194 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
3195 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
3201 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3203 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
3204 context_preload_texture(context
, state
, i
);
3209 WORD ffu_map
= context
->fixed_function_usage_map
;
3211 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3214 context_preload_texture(context
, state
, i
);
3219 static void context_load_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3221 struct wined3d_shader_sampler_map_entry
*entry
;
3222 struct wined3d_shader_resource_view
*view
;
3223 struct wined3d_shader
*shader
;
3226 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3228 if (!(shader
= state
->shader
[i
]))
3231 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3233 if (state
->cb
[i
][j
])
3234 buffer_internal_preload(state
->cb
[i
][j
], context
, state
);
3237 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
3239 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3241 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
3243 WARN("No resource view bound at index %u, %u.\n", i
, entry
->resource_idx
);
3247 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3248 buffer_internal_preload(buffer_from_resource(view
->resource
), context
, state
);
3250 wined3d_texture_load(wined3d_texture_from_resource(view
->resource
), context
, FALSE
);
3255 static void context_bind_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3257 const struct wined3d_device
*device
= context
->swapchain
->device
;
3258 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3259 struct wined3d_shader_sampler_map_entry
*entry
;
3260 struct wined3d_shader_resource_view
*view
;
3261 struct wined3d_sampler
*sampler
;
3262 struct wined3d_texture
*texture
;
3263 struct wined3d_shader
*shader
;
3264 unsigned int i
, j
, count
;
3265 GLuint sampler_name
;
3269 enum wined3d_shader_type type
;
3270 unsigned int base_idx
;
3275 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
3276 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
3279 for (i
= 0; i
< ARRAY_SIZE(shader_types
); ++i
)
3281 if (!(shader
= state
->shader
[shader_types
[i
].type
]))
3284 count
= shader
->reg_maps
.sampler_map
.count
;
3285 if (count
> shader_types
[i
].count
)
3287 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3288 shader
, count
, shader_types
[i
].count
);
3289 count
= shader_types
[i
].count
;
3292 for (j
= 0; j
< count
; ++j
)
3294 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3296 if (!(view
= state
->shader_resource_view
[shader_types
[i
].type
][entry
->resource_idx
]))
3298 WARN("No resource view bound at index %u, %u.\n", shader_types
[i
].type
, entry
->resource_idx
);
3302 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3304 FIXME("Buffer shader resources not supported.\n");
3308 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
3310 sampler_name
= device
->default_sampler
;
3312 else if ((sampler
= state
->sampler
[shader_types
[i
].type
][entry
->sampler_idx
]))
3314 sampler_name
= sampler
->name
;
3318 WARN("No sampler object bound at index %u, %u.\n", shader_types
[i
].type
, entry
->sampler_idx
);
3322 texture
= wined3d_texture_from_resource(view
->resource
);
3323 context_active_texture(context
, gl_info
, shader_types
[i
].base_idx
+ entry
->bind_idx
);
3324 wined3d_texture_bind(texture
, context
, FALSE
);
3326 GL_EXTCALL(glBindSampler(shader_types
[i
].base_idx
+ entry
->bind_idx
, sampler_name
));
3327 checkGLcall("glBindSampler");
3332 /* Context activation is done by the caller. */
3333 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
3335 const struct wined3d_state
*state
= &device
->state
;
3336 const struct StateEntry
*state_table
= context
->state_table
;
3337 const struct wined3d_fb_state
*fb
= state
->fb
;
3341 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
3342 fb
->render_targets
, fb
->depth_stencil
))
3345 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3347 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3350 /* Preload resources before FBO setup. Texture preload in particular may
3351 * result in changes to the current FBO, due to using e.g. FBO blits for
3352 * updating a resource location. */
3353 context_update_tex_unit_map(context
, state
);
3354 context_preload_textures(context
, state
);
3355 context_load_shader_resources(context
, state
);
3356 /* TODO: Right now the dependency on the vertex shader is necessary
3357 * since context_stream_info_from_declaration depends on the reg_maps of
3358 * the current VS but maybe it's possible to relax the coupling in some
3359 * situations at least. */
3360 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
3361 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
3363 context_update_stream_info(context
, state
);
3367 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3370 buffer_mark_used(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
);
3373 if (state
->index_buffer
)
3375 if (context
->stream_info
.all_vbo
)
3376 buffer_internal_preload(state
->index_buffer
, context
, state
);
3378 buffer_get_sysmem(state
->index_buffer
, context
);
3381 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3383 DWORD rep
= context
->dirtyArray
[i
];
3384 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3385 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3386 context
->isStateDirty
[idx
] &= ~(1u << shift
);
3387 state_table
[rep
].apply(context
, state
, rep
);
3390 if (context
->shader_update_mask
)
3392 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3393 context
->shader_update_mask
= 0;
3396 if (context
->constant_update_mask
)
3398 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3399 context
->constant_update_mask
= 0;
3402 if (context
->update_shader_resource_bindings
)
3404 context_bind_shader_resources(context
, state
);
3405 context
->update_shader_resource_bindings
= 0;
3408 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3410 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3413 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3414 context
->last_was_blit
= FALSE
;
3419 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
3421 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3423 render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
3424 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
3426 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3427 * the alpha blend state changes with different render target formats. */
3428 if (!context
->current_rt
)
3430 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3434 const struct wined3d_format
*old
= context
->current_rt
->container
->resource
.format
;
3435 const struct wined3d_format
*new = target
->container
->resource
.format
;
3437 if (old
->id
!= new->id
)
3439 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3440 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3441 || !(target
->container
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3442 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3444 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3445 if ((context
->current_rt
->container
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
3446 != (target
->container
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3447 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3450 /* When switching away from an offscreen render target, and we're not
3451 * using FBOs, we have to read the drawable into the texture. This is
3452 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
3453 * There are some things that need care though. PreLoad needs a GL context,
3454 * and FindContext is called before the context is activated. It also
3455 * has to be called with the old rendertarget active, otherwise a
3456 * wrong drawable is read. */
3457 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
3458 && old_render_offscreen
&& context
->current_rt
!= target
)
3460 struct wined3d_texture
*texture
= context
->current_rt
->container
;
3462 /* Read the back buffer of the old drawable into the destination texture. */
3463 if (texture
->texture_srgb
.name
)
3464 wined3d_texture_load(texture
, context
, TRUE
);
3465 wined3d_texture_load(texture
, context
, FALSE
);
3466 surface_invalidate_location(context
->current_rt
, WINED3D_LOCATION_DRAWABLE
);
3470 context
->current_rt
= target
;
3471 context_set_render_offscreen(context
, render_offscreen
);
3474 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
3476 struct wined3d_context
*current_context
= context_get_current();
3477 struct wined3d_context
*context
;
3479 TRACE("device %p, target %p.\n", device
, target
);
3481 if (current_context
&& current_context
->destroyed
)
3482 current_context
= NULL
;
3487 && current_context
->current_rt
3488 && current_context
->swapchain
->device
== device
)
3490 target
= current_context
->current_rt
;
3494 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
3495 if (swapchain
->back_buffers
)
3496 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
3498 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
3502 if (current_context
&& current_context
->current_rt
== target
)
3504 context
= current_context
;
3506 else if (target
->container
->swapchain
)
3508 TRACE("Rendering onscreen.\n");
3510 context
= swapchain_get_context(target
->container
->swapchain
);
3514 TRACE("Rendering offscreen.\n");
3516 /* Stay with the current context if possible. Otherwise use the
3517 * context for the primary swapchain. */
3518 if (current_context
&& current_context
->swapchain
->device
== device
)
3519 context
= current_context
;
3521 context
= swapchain_get_context(device
->swapchains
[0]);
3524 context_enter(context
);
3525 context_update_window(context
);
3526 context_setup_target(context
, target
);
3527 if (!context
->valid
) return context
;
3529 if (context
!= current_context
)
3531 if (!context_set_current(context
))
3532 ERR("Failed to activate the new context.\n");
3534 else if (context
->needs_set
)
3536 context_set_gl_context(context
);