2 * Context and render target management in wined3d
4 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
5 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wined3d_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
25 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous
);
28 #define WINED3D_MAX_FBO_ENTRIES 64
30 static DWORD wined3d_context_tls_idx
;
32 /* FBO helper functions */
34 /* Context activation is done by the caller. */
35 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint fbo
)
37 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
41 case GL_READ_FRAMEBUFFER
:
42 if (context
->fbo_read_binding
== fbo
) return;
43 context
->fbo_read_binding
= fbo
;
46 case GL_DRAW_FRAMEBUFFER
:
47 if (context
->fbo_draw_binding
== fbo
) return;
48 context
->fbo_draw_binding
= fbo
;
52 if (context
->fbo_read_binding
== fbo
53 && context
->fbo_draw_binding
== fbo
) return;
54 context
->fbo_read_binding
= fbo
;
55 context
->fbo_draw_binding
= fbo
;
59 FIXME("Unhandled target %#x.\n", target
);
63 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
64 checkGLcall("glBindFramebuffer()");
67 /* Context activation is done by the caller. */
68 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
72 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
74 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
75 checkGLcall("glFramebufferTexture2D()");
77 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
78 checkGLcall("glFramebufferTexture2D()");
80 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
81 checkGLcall("glFramebufferTexture2D()");
84 /* Context activation is done by the caller. */
85 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint fbo
)
87 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
89 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
90 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
91 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
93 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
94 checkGLcall("glDeleteFramebuffers()");
97 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
98 GLenum fbo_target
, DWORD format_flags
, GLuint rb
)
100 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
102 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
103 checkGLcall("glFramebufferRenderbuffer()");
106 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
108 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
109 checkGLcall("glFramebufferRenderbuffer()");
113 /* Context activation is done by the caller. */
114 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
115 GLenum fbo_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
117 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
119 TRACE("Attach depth stencil %p\n", depth_stencil
);
123 DWORD format_flags
= depth_stencil
->resource
.format
->flags
;
125 if (depth_stencil
->current_renderbuffer
)
127 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
128 format_flags
, depth_stencil
->current_renderbuffer
->id
);
134 case SFLAG_INTEXTURE
:
135 case SFLAG_INSRGBTEX
:
136 surface_prepare_texture(depth_stencil
, context
, FALSE
);
138 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
140 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
141 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
142 depth_stencil
->texture_level
);
143 checkGLcall("glFramebufferTexture2D()");
146 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
148 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
149 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
150 depth_stencil
->texture_level
);
151 checkGLcall("glFramebufferTexture2D()");
155 case SFLAG_INRB_MULTISAMPLE
:
156 surface_prepare_rb(depth_stencil
, gl_info
, TRUE
);
157 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
158 format_flags
, depth_stencil
->rb_multisample
);
161 case SFLAG_INRB_RESOLVED
:
162 surface_prepare_rb(depth_stencil
, gl_info
, FALSE
);
163 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
164 format_flags
, depth_stencil
->rb_resolved
);
168 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
173 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
175 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
176 checkGLcall("glFramebufferTexture2D()");
179 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
181 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
182 checkGLcall("glFramebufferTexture2D()");
187 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
188 checkGLcall("glFramebufferTexture2D()");
190 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
191 checkGLcall("glFramebufferTexture2D()");
195 /* Context activation is done by the caller. */
196 static void context_attach_surface_fbo(struct wined3d_context
*context
,
197 GLenum fbo_target
, DWORD idx
, struct wined3d_surface
*surface
, DWORD location
)
199 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
201 TRACE("Attach surface %p to %u\n", surface
, idx
);
203 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
209 case SFLAG_INTEXTURE
:
210 case SFLAG_INSRGBTEX
:
211 srgb
= location
== SFLAG_INSRGBTEX
;
212 surface_prepare_texture(surface
, context
, srgb
);
213 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
214 surface
->texture_target
, surface_get_texture_name(surface
, gl_info
, srgb
),
215 surface
->texture_level
);
216 checkGLcall("glFramebufferTexture2D()");
219 case SFLAG_INRB_MULTISAMPLE
:
220 surface_prepare_rb(surface
, gl_info
, TRUE
);
221 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
222 GL_RENDERBUFFER
, surface
->rb_multisample
);
223 checkGLcall("glFramebufferRenderbuffer()");
226 case SFLAG_INRB_RESOLVED
:
227 surface_prepare_rb(surface
, gl_info
, FALSE
);
228 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
229 GL_RENDERBUFFER
, surface
->rb_resolved
);
230 checkGLcall("glFramebufferRenderbuffer()");
234 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
240 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
241 checkGLcall("glFramebufferTexture2D()");
245 /* Context activation is done by the caller. */
246 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
248 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
251 if (!FIXME_ON(d3d
)) return;
253 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
254 if (status
== GL_FRAMEBUFFER_COMPLETE
)
256 TRACE("FBO complete\n");
260 const struct wined3d_surface
*attachment
;
263 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
265 if (!context
->current_fbo
)
267 ERR("FBO 0 is incomplete, driver bug?\n");
271 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context
->current_fbo
->location
),
272 context
->current_fbo
->location
);
274 /* Dump the FBO attachments */
275 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
277 attachment
= context
->current_fbo
->render_targets
[i
];
280 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
281 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
282 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
285 attachment
= context
->current_fbo
->depth_stencil
;
288 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
289 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
290 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
295 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
297 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
298 return buffer
? (1 << 31) | buffer
: 0;
301 static inline DWORD
context_generate_rt_mask_from_surface(const struct wined3d_surface
*target
)
303 return (1 << 31) | surface_get_gl_buffer(target
);
306 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
307 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
309 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
310 struct fbo_entry
*entry
;
312 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
313 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
314 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
315 entry
->depth_stencil
= depth_stencil
;
316 entry
->location
= location
;
317 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
318 entry
->attached
= FALSE
;
319 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
320 checkGLcall("glGenFramebuffers()");
321 TRACE("Created FBO %u.\n", entry
->id
);
326 /* Context activation is done by the caller. */
327 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
328 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
329 DWORD location
, struct fbo_entry
*entry
)
331 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
333 context_bind_fbo(context
, target
, entry
->id
);
334 context_clean_fbo_attachments(gl_info
, target
);
336 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
337 entry
->depth_stencil
= depth_stencil
;
338 entry
->location
= location
;
339 entry
->attached
= FALSE
;
342 /* Context activation is done by the caller. */
343 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
347 TRACE("Destroy FBO %u.\n", entry
->id
);
348 context_destroy_fbo(context
, entry
->id
);
350 --context
->fbo_entry_count
;
351 list_remove(&entry
->entry
);
352 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
353 HeapFree(GetProcessHeap(), 0, entry
);
356 /* Context activation is done by the caller. */
357 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
358 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
360 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
361 struct fbo_entry
*entry
;
363 if (depth_stencil
&& render_targets
&& render_targets
[0])
365 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
366 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
368 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
369 depth_stencil
= NULL
;
373 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
375 if (!memcmp(entry
->render_targets
,
376 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
377 && entry
->depth_stencil
== depth_stencil
&& entry
->location
== location
)
379 list_remove(&entry
->entry
);
380 list_add_head(&context
->fbo_list
, &entry
->entry
);
385 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
387 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, location
);
388 list_add_head(&context
->fbo_list
, &entry
->entry
);
389 ++context
->fbo_entry_count
;
393 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
394 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
, entry
);
395 list_remove(&entry
->entry
);
396 list_add_head(&context
->fbo_list
, &entry
->entry
);
402 /* Context activation is done by the caller. */
403 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
405 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
407 GLuint read_binding
, draw_binding
;
411 context_bind_fbo(context
, target
, entry
->id
);
415 read_binding
= context
->fbo_read_binding
;
416 draw_binding
= context
->fbo_draw_binding
;
417 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
419 /* Apply render targets */
420 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
422 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->location
);
425 /* Apply depth targets */
426 if (entry
->depth_stencil
)
427 surface_set_compatible_renderbuffer(entry
->depth_stencil
, entry
->render_targets
[0]);
428 context_attach_depth_stencil_fbo(context
, target
, entry
->depth_stencil
, entry
->location
);
430 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
431 * GL contexts requirements. */
432 glReadBuffer(GL_NONE
);
433 context_set_draw_buffer(context
, GL_NONE
);
434 if (target
!= GL_FRAMEBUFFER
)
436 if (target
== GL_READ_FRAMEBUFFER
)
437 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
439 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
442 entry
->attached
= TRUE
;
445 /* Context activation is done by the caller. */
446 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
447 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
449 struct fbo_entry
*entry
, *entry2
;
451 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
453 context_destroy_fbo_entry(context
, entry
);
456 if (context
->rebind_fbo
)
458 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
459 context
->rebind_fbo
= FALSE
;
462 if (location
== SFLAG_INDRAWABLE
)
464 context
->current_fbo
= NULL
;
465 context_bind_fbo(context
, target
, 0);
469 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
);
470 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
474 /* Context activation is done by the caller. */
475 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
476 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
478 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
480 context
->blit_targets
[0] = render_target
;
482 memset(&context
->blit_targets
[1], 0, clear_size
);
483 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
);
486 /* Context activation is done by the caller. */
487 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
489 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
491 if (context
->free_occlusion_query_count
)
493 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
497 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
499 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
500 checkGLcall("glGenQueriesARB");
502 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
506 WARN("Occlusion queries not supported, not allocating query id.\n");
511 query
->context
= context
;
512 list_add_head(&context
->occlusion_queries
, &query
->entry
);
515 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
517 struct wined3d_context
*context
= query
->context
;
519 list_remove(&query
->entry
);
520 query
->context
= NULL
;
522 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
524 UINT new_size
= context
->free_occlusion_query_size
<< 1;
525 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
526 new_size
* sizeof(*context
->free_occlusion_queries
));
530 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
534 context
->free_occlusion_query_size
= new_size
;
535 context
->free_occlusion_queries
= new_data
;
538 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
541 /* Context activation is done by the caller. */
542 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
544 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
546 if (context
->free_event_query_count
)
548 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
552 if (gl_info
->supported
[ARB_SYNC
])
554 /* Using ARB_sync, not much to do here. */
555 query
->object
.sync
= NULL
;
556 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
558 else if (gl_info
->supported
[APPLE_FENCE
])
560 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
561 checkGLcall("glGenFencesAPPLE");
563 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
565 else if(gl_info
->supported
[NV_FENCE
])
567 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
568 checkGLcall("glGenFencesNV");
570 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
574 WARN("Event queries not supported, not allocating query id.\n");
575 query
->object
.id
= 0;
579 query
->context
= context
;
580 list_add_head(&context
->event_queries
, &query
->entry
);
583 void context_free_event_query(struct wined3d_event_query
*query
)
585 struct wined3d_context
*context
= query
->context
;
587 list_remove(&query
->entry
);
588 query
->context
= NULL
;
590 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
592 UINT new_size
= context
->free_event_query_size
<< 1;
593 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
594 new_size
* sizeof(*context
->free_event_queries
));
598 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
602 context
->free_event_query_size
= new_size
;
603 context
->free_event_queries
= new_data
;
606 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
609 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
611 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
612 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
616 for (i
= 0; i
< device
->context_count
; ++i
)
618 struct wined3d_context
*context
= device
->contexts
[i
];
619 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
620 struct fbo_entry
*entry
, *entry2
;
622 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
624 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
628 if (entry
->depth_stencil
== surface
)
630 callback(context
, entry
);
634 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
636 if (entry
->render_targets
[j
] == surface
)
638 callback(context
, entry
);
646 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
648 list_remove(&entry
->entry
);
649 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
652 void context_resource_released(const struct wined3d_device
*device
,
653 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
655 if (!device
->d3d_initialized
) return;
659 case WINED3D_RTYPE_SURFACE
:
660 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
661 context_queue_fbo_entry_destruction
);
669 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
671 entry
->attached
= FALSE
;
674 void context_resource_unloaded(const struct wined3d_device
*device
,
675 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
679 case WINED3D_RTYPE_SURFACE
:
680 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
681 context_detach_fbo_entry
);
689 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
691 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
692 struct fbo_entry
*entry
= context
->current_fbo
;
695 if (!entry
|| context
->rebind_fbo
) return;
697 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
699 if (surface
== entry
->render_targets
[i
])
701 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
702 context
->rebind_fbo
= TRUE
;
707 if (surface
== entry
->depth_stencil
)
709 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
710 context
->rebind_fbo
= TRUE
;
714 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
716 int current
= GetPixelFormat(dc
);
718 if (current
== format
) return TRUE
;
722 if (!SetPixelFormat(dc
, format
, NULL
))
724 /* This may also happen if the dc belongs to a destroyed window. */
725 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
726 format
, dc
, GetLastError());
732 /* By default WGL doesn't allow pixel format adjustments but we need it
733 * here. For this reason there's a Wine specific wglSetPixelFormat()
734 * which allows us to set the pixel format multiple times. Only use it
735 * when really needed. */
736 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
738 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
740 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
747 /* OpenGL doesn't allow pixel format adjustments. Print an error and
748 * continue using the old format. There's a big chance that the old
749 * format works although with a performance hit and perhaps rendering
751 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
752 format
, dc
, current
);
756 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
758 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
761 if (!context_set_pixel_format(ctx
->gl_info
, ctx
->hdc
, ctx
->pixel_format
))
763 WARN("Failed to set pixel format %d on device context %p.\n",
764 ctx
->pixel_format
, ctx
->hdc
);
768 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
772 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
773 ctx
->glCtx
, ctx
->hdc
, GetLastError());
775 WARN("Trying fallback to the backup window.\n");
777 /* FIXME: If the context is destroyed it's no longer associated with
778 * a swapchain, so we can't use the swapchain to get a backup dc. To
779 * make this work windowless contexts would need to be handled by the
783 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
784 context_set_current(NULL
);
788 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
790 context_set_current(NULL
);
794 if (!context_set_pixel_format(ctx
->gl_info
, dc
, ctx
->pixel_format
))
796 ERR("Failed to set pixel format %d on device context %p.\n",
797 ctx
->pixel_format
, dc
);
798 context_set_current(NULL
);
802 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
804 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
806 context_set_current(NULL
);
813 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
, int pf
)
815 if (!context_set_pixel_format(gl_info
, dc
, pf
))
817 ERR("Failed to restore pixel format %d on device context %p.\n", pf
, dc
);
818 context_set_current(NULL
);
822 if (!wglMakeCurrent(dc
, gl_ctx
))
824 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
825 gl_ctx
, dc
, GetLastError());
826 context_set_current(NULL
);
830 static void context_update_window(struct wined3d_context
*context
)
832 if (context
->win_handle
== context
->swapchain
->win_handle
)
835 TRACE("Updating context %p window from %p to %p.\n",
836 context
, context
->win_handle
, context
->swapchain
->win_handle
);
840 /* You'd figure ReleaseDC() would fail if the DC doesn't match the
841 * window. However, that's not what actually happens, and there are
842 * user32 tests that confirm ReleaseDC() with the wrong window is
843 * supposed to succeed. So explicitly check that the DC belongs to
844 * the window, since we want to avoid releasing a DC that belongs to
845 * some other window if the original window was already destroyed. */
846 if (WindowFromDC(context
->hdc
) != context
->win_handle
)
848 WARN("DC %p does not belong to window %p.\n",
849 context
->hdc
, context
->win_handle
);
851 else if (!ReleaseDC(context
->win_handle
, context
->hdc
))
853 ERR("Failed to release device context %p, last error %#x.\n",
854 context
->hdc
, GetLastError());
857 else context
->valid
= 1;
859 context
->win_handle
= context
->swapchain
->win_handle
;
861 if (!(context
->hdc
= GetDC(context
->win_handle
)))
863 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
867 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
869 ERR("Failed to set pixel format %d on device context %p.\n",
870 context
->pixel_format
, context
->hdc
);
874 context_set_gl_context(context
);
882 /* Do not call while under the GL lock. */
883 static void context_destroy_gl_resources(struct wined3d_context
*context
)
885 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
886 struct wined3d_occlusion_query
*occlusion_query
;
887 struct wined3d_event_query
*event_query
;
888 struct fbo_entry
*entry
, *entry2
;
894 restore_ctx
= wglGetCurrentContext();
895 restore_dc
= wglGetCurrentDC();
896 restore_pf
= GetPixelFormat(restore_dc
);
898 if (context
->valid
&& restore_ctx
!= context
->glCtx
)
899 context_set_gl_context(context
);
903 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
905 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
906 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
907 occlusion_query
->context
= NULL
;
910 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
914 if (gl_info
->supported
[ARB_SYNC
])
916 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
918 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
919 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
921 event_query
->context
= NULL
;
924 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
926 if (!context
->valid
) entry
->id
= 0;
927 context_destroy_fbo_entry(context
, entry
);
930 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
932 if (!context
->valid
) entry
->id
= 0;
933 context_destroy_fbo_entry(context
, entry
);
938 if (context
->dummy_arbfp_prog
)
940 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
943 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
944 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
946 if (gl_info
->supported
[ARB_SYNC
])
948 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
950 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
953 else if (gl_info
->supported
[APPLE_FENCE
])
955 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
957 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
960 else if (gl_info
->supported
[NV_FENCE
])
962 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
964 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
968 checkGLcall("context cleanup");
971 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
972 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
976 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
, restore_pf
);
978 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
980 ERR("Failed to disable GL context.\n");
983 ReleaseDC(context
->win_handle
, context
->hdc
);
985 if (!wglDeleteContext(context
->glCtx
))
987 DWORD err
= GetLastError();
988 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
992 DWORD
context_get_tls_idx(void)
994 return wined3d_context_tls_idx
;
997 void context_set_tls_idx(DWORD idx
)
999 wined3d_context_tls_idx
= idx
;
1002 struct wined3d_context
*context_get_current(void)
1004 return TlsGetValue(wined3d_context_tls_idx
);
1007 /* Do not call while under the GL lock. */
1008 BOOL
context_set_current(struct wined3d_context
*ctx
)
1010 struct wined3d_context
*old
= context_get_current();
1014 TRACE("Already using D3D context %p.\n", ctx
);
1022 TRACE("Switching away from destroyed context %p.\n", old
);
1023 context_destroy_gl_resources(old
);
1024 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1025 HeapFree(GetProcessHeap(), 0, old
);
1037 ERR("Trying to make invalid context %p current\n", ctx
);
1041 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1042 if (!context_set_gl_context(ctx
))
1046 else if(wglGetCurrentContext())
1048 TRACE("Clearing current D3D context.\n");
1049 if (!wglMakeCurrent(NULL
, NULL
))
1051 DWORD err
= GetLastError();
1052 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1053 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1058 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1061 void context_release(struct wined3d_context
*context
)
1063 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1067 if (!context
->level
)
1068 WARN("Context %p is not active.\n", context
);
1069 else if (context
!= context_get_current())
1070 WARN("Context %p is not the current context.\n", context
);
1073 if (!--context
->level
&& context
->restore_ctx
)
1075 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1076 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
, context
->restore_pf
);
1077 context
->restore_ctx
= NULL
;
1078 context
->restore_dc
= NULL
;
1082 static void context_enter(struct wined3d_context
*context
)
1084 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1086 if (!context
->level
++)
1088 const struct wined3d_context
*current_context
= context_get_current();
1089 HGLRC current_gl
= wglGetCurrentContext();
1091 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1093 TRACE("Another GL context (%p on device context %p) is already current.\n",
1094 current_gl
, wglGetCurrentDC());
1095 context
->restore_ctx
= current_gl
;
1096 context
->restore_dc
= wglGetCurrentDC();
1097 context
->restore_pf
= GetPixelFormat(context
->restore_dc
);
1102 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1104 DWORD rep
= context
->state_table
[state
].representative
;
1108 if (isStateDirty(context
, rep
)) return;
1110 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1111 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1112 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1113 context
->isStateDirty
[idx
] |= (1 << shift
);
1116 /* This function takes care of wined3d pixel format selection. */
1117 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1118 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1119 BOOL auxBuffers
, BOOL findCompatible
)
1122 BYTE redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1123 BYTE depthBits
=0, stencilBits
=0;
1124 unsigned int current_value
;
1125 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1128 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1129 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1130 auxBuffers
, findCompatible
);
1132 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1134 ERR("Unable to get color bits for format %s (%#x)!\n",
1135 debug_d3dformat(color_format
->id
), color_format
->id
);
1139 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1142 for (i
= 0; i
< cfg_count
; ++i
)
1144 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1147 /* For now only accept RGBA formats. Perhaps some day we will
1148 * allow floating point formats for pbuffers. */
1149 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1151 /* In window mode we need a window drawable format and double buffering. */
1152 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1154 if (cfg
->redSize
< redBits
)
1156 if (cfg
->greenSize
< greenBits
)
1158 if (cfg
->blueSize
< blueBits
)
1160 if (cfg
->alphaSize
< alphaBits
)
1162 if (cfg
->depthSize
< depthBits
)
1164 if (stencilBits
&& cfg
->stencilSize
!= stencilBits
)
1166 /* Check multisampling support. */
1167 if (cfg
->numSamples
)
1171 /* We try to locate a format which matches our requirements exactly. In case of
1172 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1173 if (cfg
->depthSize
== depthBits
)
1175 if (cfg
->stencilSize
== stencilBits
)
1177 if (cfg
->alphaSize
== alphaBits
)
1179 /* We like to have aux buffers in backbuffer mode */
1180 if (auxBuffers
&& cfg
->auxBuffers
)
1182 if (cfg
->redSize
== redBits
1183 && cfg
->greenSize
== greenBits
1184 && cfg
->blueSize
== blueBits
)
1187 if (value
> current_value
)
1189 iPixelFormat
= cfg
->iPixelFormat
;
1190 current_value
= value
;
1194 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1195 if(!iPixelFormat
&& !findCompatible
) {
1196 ERR("Can't find a suitable iPixelFormat\n");
1198 } else if(!iPixelFormat
) {
1199 PIXELFORMATDESCRIPTOR pfd
;
1201 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1202 /* PixelFormat selection */
1203 ZeroMemory(&pfd
, sizeof(pfd
));
1204 pfd
.nSize
= sizeof(pfd
);
1206 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1207 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1208 pfd
.cAlphaBits
= alphaBits
;
1209 pfd
.cColorBits
= colorBits
;
1210 pfd
.cDepthBits
= depthBits
;
1211 pfd
.cStencilBits
= stencilBits
;
1212 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1214 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1216 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1217 ERR("Can't find a suitable iPixelFormat\n");
1222 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1223 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1224 return iPixelFormat
;
1227 /* Context activation is done by the caller. */
1228 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1230 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1231 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1233 for (i
= 0; i
< count
; ++i
)
1235 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1236 checkGLcall("glActiveTextureARB");
1238 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1239 checkGLcall("glBindTexture");
1241 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1243 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1244 checkGLcall("glBindTexture");
1247 if (gl_info
->supported
[EXT_TEXTURE3D
])
1249 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1250 checkGLcall("glBindTexture");
1253 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1255 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1256 checkGLcall("glBindTexture");
1261 BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1263 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1264 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1267 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1268 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1272 case GL_DEBUG_TYPE_ERROR_ARB
:
1273 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1276 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1277 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1278 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1279 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1282 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1283 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1287 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1292 /* Do not call while under the GL lock. */
1293 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1294 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1296 struct wined3d_device
*device
= swapchain
->device
;
1297 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1298 const struct wined3d_format
*color_format
;
1299 struct wined3d_context
*ret
;
1300 BOOL auxBuffers
= FALSE
;
1301 HGLRC ctx
, share_ctx
;
1308 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1310 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1314 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1315 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1316 if (!ret
->blit_targets
)
1319 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1320 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1321 if (!ret
->draw_buffers
)
1324 ret
->free_occlusion_query_size
= 4;
1325 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1326 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1327 if (!ret
->free_occlusion_queries
)
1330 list_init(&ret
->occlusion_queries
);
1332 ret
->free_event_query_size
= 4;
1333 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1334 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1335 if (!ret
->free_event_queries
)
1338 list_init(&ret
->event_queries
);
1339 list_init(&ret
->fbo_list
);
1340 list_init(&ret
->fbo_destroy_list
);
1342 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1344 ERR("Failed to allocate shader backend context data.\n");
1348 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1350 WARN("Failed to retireve device context, trying swapchain backup.\n");
1352 if (!(hdc
= swapchain_get_backup_dc(swapchain
)))
1354 ERR("Failed to retrieve a device context.\n");
1359 color_format
= target
->resource
.format
;
1361 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1362 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1363 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1367 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1368 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1369 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1370 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1373 /* DirectDraw supports 8bit paletted render targets and these are used by
1374 * old games like StarCraft and C&C. Most modern hardware doesn't support
1375 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1376 * conversion (ab)uses the alpha component for storing the palette index.
1377 * For this reason we require a format with 8bit alpha, so request
1379 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1380 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1382 /* Try to find a pixel format which matches our requirements. */
1383 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1385 /* Try to locate a compatible format if we weren't able to find anything. */
1388 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1389 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1392 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1395 ERR("Can't find a suitable pixel format.\n");
1401 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1403 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1404 context_release(ret
);
1408 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1409 if (gl_info
->p_wglCreateContextAttribsARB
)
1411 unsigned int ctx_attrib_idx
= 0;
1412 GLint ctx_attribs
[3];
1414 if (context_debug_output_enabled(gl_info
))
1416 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1417 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_DEBUG_BIT_ARB
;
1419 ctx_attribs
[ctx_attrib_idx
] = 0;
1421 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1423 ERR("Failed to create a WGL context.\n");
1424 context_release(ret
);
1430 if (!(ctx
= wglCreateContext(hdc
)))
1432 ERR("Failed to create a WGL context.\n");
1433 context_release(ret
);
1437 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1439 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1440 context_release(ret
);
1441 if (!wglDeleteContext(ctx
))
1442 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1447 if (!device_context_add(device
, ret
))
1449 ERR("Failed to add the newly created context to the context list\n");
1450 context_release(ret
);
1451 if (!wglDeleteContext(ctx
))
1452 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1456 ret
->gl_info
= gl_info
;
1457 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1458 ret
->state_table
= device
->StateTable
;
1460 /* Mark all states dirty to force a proper initialization of the states
1461 * on the first use of the context. */
1462 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1464 if (ret
->state_table
[state
].representative
)
1465 context_invalidate_state(ret
, state
);
1468 ret
->swapchain
= swapchain
;
1469 ret
->current_rt
= target
;
1470 ret
->tid
= GetCurrentThreadId();
1472 ret
->render_offscreen
= surface_is_offscreen(target
);
1473 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1477 ret
->win_handle
= swapchain
->win_handle
;
1479 ret
->pixel_format
= pixel_format
;
1481 /* Set up the context defaults */
1482 if (!context_set_current(ret
))
1484 ERR("Cannot activate context to set up defaults.\n");
1485 device_context_remove(device
, ret
);
1486 context_release(ret
);
1487 if (!wglDeleteContext(ctx
))
1488 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1492 if (context_debug_output_enabled(gl_info
))
1494 GL_EXTCALL(glDebugMessageCallbackARB(wined3d_debug_callback
, ret
));
1495 if (TRACE_ON(d3d_synchronous
))
1496 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
);
1497 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1500 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR_ARB
,
1501 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1505 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
,
1506 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1507 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
,
1508 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1509 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY_ARB
,
1510 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1512 if (WARN_ON(d3d_perf
))
1514 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE_ARB
,
1515 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1519 switch (swapchain
->desc
.swap_interval
)
1521 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1524 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1525 case WINED3DPRESENT_INTERVAL_ONE
:
1528 case WINED3DPRESENT_INTERVAL_TWO
:
1531 case WINED3DPRESENT_INTERVAL_THREE
:
1534 case WINED3DPRESENT_INTERVAL_FOUR
:
1538 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1542 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1544 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1545 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1546 swap_interval
, ret
, GetLastError());
1549 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1551 TRACE("Setting up the screen\n");
1553 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1554 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1556 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1557 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1559 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1560 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1562 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1563 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1564 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1565 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1567 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1569 /* Direct3D always uses n-1 weights for n world matrices and uses
1570 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1571 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1572 * enabled as well. */
1573 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1574 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1576 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1578 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1579 * the previous texture where to source the offset from is always unit - 1.
1581 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1583 context_active_texture(ret
, gl_info
, s
);
1584 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1585 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1586 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1589 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1591 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1592 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1593 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1594 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1597 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1598 * program and the dummy program is destroyed when the context is destroyed.
1600 const char *dummy_program
=
1602 "MOV result.color, fragment.color.primary;\n"
1604 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1605 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1606 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1609 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1611 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1613 context_active_texture(ret
, gl_info
, s
);
1614 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1615 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1619 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1621 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1623 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1625 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1627 ret
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
1628 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
1629 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
1631 /* If this happens to be the first context for the device, dummy textures
1632 * are not created yet. In that case, they will be created (and bound) by
1633 * create_dummy_textures right after this context is initialized. */
1634 if (device
->dummy_texture_2d
[0])
1635 bind_dummy_textures(device
, ret
);
1637 TRACE("Created context %p.\n", ret
);
1642 device
->shader_backend
->shader_free_context_data(ret
);
1643 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1644 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1645 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1646 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1647 HeapFree(GetProcessHeap(), 0, ret
);
1651 /* Do not call while under the GL lock. */
1652 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1656 TRACE("Destroying ctx %p\n", context
);
1658 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1660 context_destroy_gl_resources(context
);
1661 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1666 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1667 in wined3d_adapter may go away in the meantime */
1668 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
1669 *gl_info
= *context
->gl_info
;
1670 context
->gl_info
= gl_info
;
1671 context
->destroyed
= 1;
1675 device
->shader_backend
->shader_free_context_data(context
);
1676 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1677 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1678 device_context_remove(device
, context
);
1679 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1682 /* Context activation is done by the caller. */
1683 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
1685 const GLdouble projection
[] =
1687 2.0 / width
, 0.0, 0.0, 0.0,
1688 0.0, 2.0 / height
, 0.0, 0.0,
1690 -1.0, -1.0, -1.0, 1.0,
1693 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
1694 checkGLcall("glMatrixMode(GL_PROJECTION)");
1695 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
1696 checkGLcall("glLoadMatrixd");
1697 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
1698 checkGLcall("glViewport");
1701 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
1703 const struct wined3d_surface
*rt
= context
->current_rt
;
1705 if (rt
->swapchain
&& rt
->swapchain
->front_buffer
== rt
)
1709 GetClientRect(context
->win_handle
, &window_size
);
1710 size
->cx
= window_size
.right
- window_size
.left
;
1711 size
->cy
= window_size
.bottom
- window_size
.top
;
1716 size
->cx
= rt
->resource
.width
;
1717 size
->cy
= rt
->resource
.height
;
1720 /*****************************************************************************
1723 * Sets up a context for DirectDraw blitting.
1724 * All texture units are disabled, texture unit 0 is set as current unit
1725 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1726 * color writing enabled for all channels
1727 * register combiners disabled, shaders disabled
1728 * world matrix is set to identity, texture matrix 0 too
1729 * projection matrix is setup for drawing screen coordinates
1732 * This: Device to activate the context for
1733 * context: Context to setup
1735 *****************************************************************************/
1736 /* Context activation is done by the caller. */
1737 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
1740 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1744 TRACE("Setting up context %p for blitting\n", context
);
1746 context_get_rt_size(context
, &rt_size
);
1748 if (context
->last_was_blit
)
1750 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
1752 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1753 context
->blit_w
= rt_size
.cx
;
1754 context
->blit_h
= rt_size
.cy
;
1755 /* No need to dirtify here, the states are still dirtified because
1756 * they weren't applied since the last SetupForBlit() call. */
1758 TRACE("Context is already set up for blitting, nothing to do\n");
1761 context
->last_was_blit
= TRUE
;
1763 /* Disable all textures. The caller can then bind a texture it wants to blit
1766 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1767 * function texture unit. No need to care for higher samplers
1769 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1771 sampler
= device
->rev_tex_unit_map
[i
];
1772 context_active_texture(context
, gl_info
, i
);
1774 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1776 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1777 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1779 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1780 checkGLcall("glDisable GL_TEXTURE_3D");
1781 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1783 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1784 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1786 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1787 checkGLcall("glDisable GL_TEXTURE_2D");
1789 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1790 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1792 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1794 if (sampler
< MAX_TEXTURES
)
1795 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1796 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1799 context_active_texture(context
, gl_info
, 0);
1801 sampler
= device
->rev_tex_unit_map
[0];
1803 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1805 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1806 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1808 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1809 checkGLcall("glDisable GL_TEXTURE_3D");
1810 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1812 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1813 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1815 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1816 checkGLcall("glDisable GL_TEXTURE_2D");
1818 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1820 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
1821 checkGLcall("glMatrixMode(GL_TEXTURE)");
1822 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1823 checkGLcall("glLoadIdentity()");
1825 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1827 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1828 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
1829 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1832 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1834 if (sampler
< MAX_TEXTURES
)
1836 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
1837 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1839 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1842 /* Other misc states */
1843 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
1844 checkGLcall("glDisable(GL_ALPHA_TEST)");
1845 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
1846 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
1847 checkGLcall("glDisable GL_LIGHTING");
1848 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
1849 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
1850 checkGLcall("glDisable GL_DEPTH_TEST");
1851 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
1852 glDisableWINE(GL_FOG
);
1853 checkGLcall("glDisable GL_FOG");
1854 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
1855 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
1856 checkGLcall("glDisable GL_BLEND");
1857 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
1858 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
1859 checkGLcall("glDisable GL_CULL_FACE");
1860 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
1861 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
1862 checkGLcall("glDisable GL_STENCIL_TEST");
1863 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
1864 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
1865 checkGLcall("glDisable GL_SCISSOR_TEST");
1866 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
1867 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1869 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
1870 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1871 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
1873 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1874 checkGLcall("glColorMask");
1875 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
1876 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
1877 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
1878 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
1879 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1881 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
1882 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
1883 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1886 /* Setup transforms */
1887 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
1888 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1889 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1890 checkGLcall("glLoadIdentity()");
1891 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
1893 context
->last_was_rhw
= TRUE
;
1894 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
1896 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1897 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1898 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1899 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1900 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1901 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1902 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
1904 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1906 /* Disable shaders */
1907 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
1909 context
->blit_w
= rt_size
.cx
;
1910 context
->blit_h
= rt_size
.cy
;
1911 context_invalidate_state(context
, STATE_VIEWPORT
);
1912 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
1915 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
1917 return rt_mask
& (1 << 31);
1920 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
1922 return rt_mask
& ~(1 << 31);
1925 /* Context activation is done by the caller. */
1926 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
1928 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1932 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
1933 checkGLcall("glDrawBuffer()");
1935 else if (is_rt_mask_onscreen(rt_mask
))
1937 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
1938 checkGLcall("glDrawBuffer()");
1942 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1949 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
1951 context
->draw_buffers
[i
] = GL_NONE
;
1957 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1959 GL_EXTCALL(glDrawBuffersARB(i
, context
->draw_buffers
));
1960 checkGLcall("glDrawBuffers()");
1964 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
1965 checkGLcall("glDrawBuffer()");
1970 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
1975 /* Context activation is done by the caller. */
1976 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
1978 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1979 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
1980 DWORD new_mask
= context_generate_rt_mask(buffer
);
1982 if (new_mask
== *current_mask
)
1985 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
1986 checkGLcall("glDrawBuffer()");
1988 *current_mask
= new_mask
;
1991 /* Context activation is done by the caller. */
1992 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
1994 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0
+ unit
));
1995 checkGLcall("glActiveTextureARB");
1996 context
->active_texture
= unit
;
1999 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2001 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2002 DWORD unit
= context
->active_texture
;
2003 DWORD old_texture_type
= context
->texture_type
[unit
];
2007 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2008 checkGLcall("glBindTexture");
2015 if (old_texture_type
!= target
)
2017 const struct wined3d_device
*device
= context
->swapchain
->device
;
2019 switch (old_texture_type
)
2025 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2026 checkGLcall("glBindTexture");
2028 case GL_TEXTURE_RECTANGLE_ARB
:
2029 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2030 checkGLcall("glBindTexture");
2032 case GL_TEXTURE_CUBE_MAP
:
2033 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2034 checkGLcall("glBindTexture");
2037 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2038 checkGLcall("glBindTexture");
2041 ERR("Unexpected texture target %#x\n", old_texture_type
);
2044 context
->texture_type
[unit
] = target
;
2048 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2050 if (context
->render_offscreen
== offscreen
) return;
2052 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2053 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2054 context_invalidate_state(context
, STATE_VIEWPORT
);
2055 context_invalidate_state(context
, STATE_SCISSORRECT
);
2056 context_invalidate_state(context
, STATE_FRONTFACE
);
2057 context
->render_offscreen
= offscreen
;
2060 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2061 const struct wined3d_format
*required
)
2063 BYTE existing_depth
, existing_stencil
, required_depth
, required_stencil
;
2065 if (existing
== required
) return TRUE
;
2066 if ((existing
->flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
2068 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
2069 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
2071 if(existing_depth
< required_depth
) return FALSE
;
2072 /* If stencil bits are used the exact amount is required - otherwise wrapping
2073 * won't work correctly */
2074 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
2078 /* The caller provides a context */
2079 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2080 const struct wined3d_surface
*depth_stencil
)
2082 /* Onscreen surfaces are always in a swapchain */
2083 struct wined3d_swapchain
*swapchain
= context
->current_rt
->swapchain
;
2085 if (context
->render_offscreen
|| !depth_stencil
) return;
2086 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->resource
.format
)) return;
2088 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2089 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2091 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2093 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2094 surface_load_location(context
->current_rt
, SFLAG_INTEXTURE
, NULL
);
2095 swapchain
->render_to_fbo
= TRUE
;
2096 swapchain_update_draw_bindings(swapchain
);
2097 context_set_render_offscreen(context
, TRUE
);
2100 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
2102 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2104 else if (rt
->swapchain
)
2105 return context_generate_rt_mask_from_surface(rt
);
2107 return context_generate_rt_mask(device
->offscreenBuffer
);
2110 /* Context activation is done by the caller. */
2111 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2113 struct wined3d_surface
*rt
= context
->current_rt
;
2114 DWORD rt_mask
, *cur_mask
;
2116 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2118 context_validate_onscreen_formats(context
, NULL
);
2120 if (context
->render_offscreen
)
2122 surface_internal_preload(rt
, SRGB_RGB
);
2124 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->draw_binding
);
2125 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2132 context
->current_fbo
= NULL
;
2133 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2134 rt_mask
= context_generate_rt_mask_from_surface(rt
);
2139 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt
);
2142 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2144 if (rt_mask
!= *cur_mask
)
2146 context_apply_draw_buffers(context
, rt_mask
);
2147 *cur_mask
= rt_mask
;
2150 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2152 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2155 SetupForBlit(device
, context
);
2156 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2159 static BOOL
context_validate_rt_config(UINT rt_count
,
2160 struct wined3d_surface
* const *rts
, const struct wined3d_surface
*ds
)
2164 if (ds
) return TRUE
;
2166 for (i
= 0; i
< rt_count
; ++i
)
2168 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2172 WARN("Invalid render target config, need at least one attachment.\n");
2176 /* Context activation is done by the caller. */
2177 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2178 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2180 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2181 DWORD rt_mask
= 0, *cur_mask
;
2183 struct wined3d_surface
**rts
= fb
->render_targets
;
2185 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2186 || rt_count
!= context
->gl_info
->limits
.buffers
)
2188 if (!context_validate_rt_config(rt_count
, rts
, fb
->depth_stencil
))
2191 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2193 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2195 if (!rt_count
|| surface_is_offscreen(rts
[0]))
2197 for (i
= 0; i
< rt_count
; ++i
)
2199 context
->blit_targets
[i
] = rts
[i
];
2200 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2201 rt_mask
|= (1 << i
);
2203 while (i
< context
->gl_info
->limits
.buffers
)
2205 context
->blit_targets
[i
] = NULL
;
2208 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, fb
->depth_stencil
,
2209 rt_count
? rts
[0]->draw_binding
: SFLAG_INTEXTURE
);
2213 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2214 rt_mask
= context_generate_rt_mask_from_surface(rts
[0]);
2217 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2218 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2219 * state management allows this */
2220 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2224 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt_count
? rts
[0] : NULL
);
2227 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2228 && (!rt_count
|| surface_is_offscreen(rts
[0])))
2230 for (i
= 0; i
< rt_count
; ++i
)
2232 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
) rt_mask
|= (1 << i
);
2237 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt_count
? rts
[0] : NULL
);
2240 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2242 if (rt_mask
!= *cur_mask
)
2244 context_apply_draw_buffers(context
, rt_mask
);
2245 *cur_mask
= rt_mask
;
2246 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2249 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2251 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2254 if (context
->last_was_blit
)
2255 context
->last_was_blit
= FALSE
;
2257 /* Blending and clearing should be orthogonal, but tests on the nvidia
2258 * driver show that disabling blending when clearing improves the clearing
2259 * performance incredibly. */
2260 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2261 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2262 checkGLcall("glEnable GL_SCISSOR_TEST");
2264 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2265 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2266 context_invalidate_state(context
, STATE_SCISSORRECT
);
2271 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_device
*device
)
2273 const struct wined3d_state
*state
= &device
->state
;
2274 struct wined3d_surface
**rts
= state
->fb
->render_targets
;
2275 struct wined3d_shader
*ps
= state
->pixel_shader
;
2276 DWORD rt_mask
, rt_mask_bits
;
2279 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return context_generate_rt_mask_no_fbo(device
, rts
[0]);
2280 else if (!context
->render_offscreen
) return context_generate_rt_mask_from_surface(rts
[0]);
2282 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2283 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2284 rt_mask_bits
= rt_mask
;
2286 while (rt_mask_bits
)
2288 rt_mask_bits
&= ~(1 << i
);
2289 if (!rts
[i
] || rts
[i
]->resource
.format
->id
== WINED3DFMT_NULL
)
2290 rt_mask
&= ~(1 << i
);
2298 /* Context activation is done by the caller. */
2299 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2301 const struct wined3d_device
*device
= context
->swapchain
->device
;
2302 const struct wined3d_fb_state
*fb
= state
->fb
;
2303 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2306 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2308 if (!context
->render_offscreen
)
2310 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2314 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, fb
->render_targets
, fb
->depth_stencil
,
2315 fb
->render_targets
[0]->draw_binding
);
2319 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2320 if (rt_mask
!= *cur_mask
)
2322 context_apply_draw_buffers(context
, rt_mask
);
2323 *cur_mask
= rt_mask
;
2327 /* Context activation is done by the caller. */
2328 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2330 const struct wined3d_device
*device
= context
->swapchain
->device
;
2331 DWORD rt_mask
, *cur_mask
;
2333 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2335 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2336 rt_mask
= find_draw_buffers_mask(context
, device
);
2337 if (rt_mask
!= *cur_mask
)
2339 context_apply_draw_buffers(context
, rt_mask
);
2340 *cur_mask
= rt_mask
;
2344 /* Context activation is done by the caller. */
2345 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
2347 const struct wined3d_state
*state
= &device
->state
;
2348 const struct StateEntry
*state_table
= context
->state_table
;
2349 const struct wined3d_fb_state
*fb
= state
->fb
;
2352 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
2353 fb
->render_targets
, fb
->depth_stencil
))
2356 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
2358 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2361 /* Preload resources before FBO setup. Texture preload in particular may
2362 * result in changes to the current FBO, due to using e.g. FBO blits for
2363 * updating a resource location. */
2364 device_update_tex_unit_map(device
);
2365 device_preload_textures(device
);
2366 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
))
2367 device_update_stream_info(device
, context
->gl_info
);
2368 if (state
->index_buffer
)
2370 if (device
->stream_info
.all_vbo
)
2371 wined3d_buffer_preload(state
->index_buffer
);
2373 buffer_get_sysmem(state
->index_buffer
, context
->gl_info
);
2376 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2378 DWORD rep
= context
->dirtyArray
[i
];
2379 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2380 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2381 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2382 state_table
[rep
].apply(context
, state
, rep
);
2385 if (context
->shader_update_mask
)
2387 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
2388 context
->shader_update_mask
= 0;
2391 if (context
->constant_update_mask
)
2393 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
2394 context
->constant_update_mask
= 0;
2397 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2399 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2402 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2403 context
->last_was_blit
= FALSE
;
2408 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
2410 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
2412 render_offscreen
= surface_is_offscreen(target
);
2413 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
2415 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2416 * the alpha blend state changes with different render target formats. */
2417 if (!context
->current_rt
)
2419 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2423 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
2424 const struct wined3d_format
*new = target
->resource
.format
;
2426 if (old
->id
!= new->id
)
2428 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2429 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
2430 || !(new->flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
2431 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2433 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2434 if ((old
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
2435 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
2438 /* When switching away from an offscreen render target, and we're not
2439 * using FBOs, we have to read the drawable into the texture. This is
2440 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2441 * are some things that need care though. PreLoad needs a GL context,
2442 * and FindContext is called before the context is activated. It also
2443 * has to be called with the old rendertarget active, otherwise a
2444 * wrong drawable is read. */
2445 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2446 && old_render_offscreen
&& context
->current_rt
!= target
)
2448 /* Read the back buffer of the old drawable into the destination texture. */
2449 if (context
->current_rt
->texture_name_srgb
)
2450 surface_internal_preload(context
->current_rt
, SRGB_SRGB
);
2451 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2452 surface_modify_location(context
->current_rt
, SFLAG_INDRAWABLE
, FALSE
);
2456 context
->current_rt
= target
;
2457 context_set_render_offscreen(context
, render_offscreen
);
2460 /* Do not call while under the GL lock. */
2461 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
2463 struct wined3d_context
*current_context
= context_get_current();
2464 struct wined3d_context
*context
;
2466 TRACE("device %p, target %p.\n", device
, target
);
2468 if (current_context
&& current_context
->destroyed
)
2469 current_context
= NULL
;
2474 && current_context
->current_rt
2475 && current_context
->swapchain
->device
== device
)
2477 target
= current_context
->current_rt
;
2481 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
2482 if (swapchain
->back_buffers
)
2483 target
= swapchain
->back_buffers
[0];
2485 target
= swapchain
->front_buffer
;
2489 if (current_context
&& current_context
->current_rt
== target
)
2491 context
= current_context
;
2493 else if (target
->swapchain
)
2495 TRACE("Rendering onscreen.\n");
2497 context
= swapchain_get_context(target
->swapchain
);
2501 TRACE("Rendering offscreen.\n");
2503 /* Stay with the current context if possible. Otherwise use the
2504 * context for the primary swapchain. */
2505 if (current_context
&& current_context
->swapchain
->device
== device
)
2506 context
= current_context
;
2508 context
= swapchain_get_context(device
->swapchains
[0]);
2511 context_update_window(context
);
2512 context_setup_target(context
, target
);
2513 context_enter(context
);
2514 if (!context
->valid
) return context
;
2516 if (context
!= current_context
)
2518 if (!context_set_current(context
))
2519 ERR("Failed to activate the new context.\n");
2521 else if (context
->restore_ctx
)
2523 context_set_gl_context(context
);