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 static DWORD wined3d_context_tls_idx
;
38 /* FBO helper functions */
40 /* Context activation is done by the caller. */
41 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint
*fbo
)
43 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
54 gl_info
->fbo_ops
.glGenFramebuffers(1, fbo
);
55 checkGLcall("glGenFramebuffers()");
56 TRACE("Created FBO %u.\n", *fbo
);
63 case GL_READ_FRAMEBUFFER
:
64 if (context
->fbo_read_binding
== f
) return;
65 context
->fbo_read_binding
= f
;
68 case GL_DRAW_FRAMEBUFFER
:
69 if (context
->fbo_draw_binding
== f
) return;
70 context
->fbo_draw_binding
= f
;
74 if (context
->fbo_read_binding
== f
75 && context
->fbo_draw_binding
== f
) return;
76 context
->fbo_read_binding
= f
;
77 context
->fbo_draw_binding
= f
;
81 FIXME("Unhandled target %#x.\n", target
);
85 gl_info
->fbo_ops
.glBindFramebuffer(target
, f
);
86 checkGLcall("glBindFramebuffer()");
89 /* Context activation is done by the caller. */
90 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
94 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
96 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
97 checkGLcall("glFramebufferTexture2D()");
99 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
100 checkGLcall("glFramebufferTexture2D()");
102 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
103 checkGLcall("glFramebufferTexture2D()");
106 /* Context activation is done by the caller. */
107 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint
*fbo
)
109 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
111 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
112 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
113 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
115 gl_info
->fbo_ops
.glDeleteFramebuffers(1, fbo
);
116 checkGLcall("glDeleteFramebuffers()");
119 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
120 GLenum fbo_target
, DWORD format_flags
, GLuint rb
)
122 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
124 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
125 checkGLcall("glFramebufferRenderbuffer()");
128 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
130 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
131 checkGLcall("glFramebufferRenderbuffer()");
135 /* Context activation is done by the caller. */
136 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
137 GLenum fbo_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
139 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
141 TRACE("Attach depth stencil %p\n", depth_stencil
);
145 DWORD format_flags
= depth_stencil
->resource
.format
->flags
;
147 if (depth_stencil
->current_renderbuffer
)
149 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
150 format_flags
, depth_stencil
->current_renderbuffer
->id
);
156 case SFLAG_INTEXTURE
:
157 case SFLAG_INSRGBTEX
:
158 surface_prepare_texture(depth_stencil
, context
, FALSE
);
160 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
162 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
163 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
164 depth_stencil
->texture_level
);
165 checkGLcall("glFramebufferTexture2D()");
168 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
170 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
171 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
172 depth_stencil
->texture_level
);
173 checkGLcall("glFramebufferTexture2D()");
177 case SFLAG_INRB_MULTISAMPLE
:
178 surface_prepare_rb(depth_stencil
, gl_info
, TRUE
);
179 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
180 format_flags
, depth_stencil
->rb_multisample
);
183 case SFLAG_INRB_RESOLVED
:
184 surface_prepare_rb(depth_stencil
, gl_info
, FALSE
);
185 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
186 format_flags
, depth_stencil
->rb_resolved
);
190 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
195 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
197 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
198 checkGLcall("glFramebufferTexture2D()");
201 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
203 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
204 checkGLcall("glFramebufferTexture2D()");
209 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
210 checkGLcall("glFramebufferTexture2D()");
212 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
213 checkGLcall("glFramebufferTexture2D()");
217 /* Context activation is done by the caller. */
218 static void context_attach_surface_fbo(struct wined3d_context
*context
,
219 GLenum fbo_target
, DWORD idx
, struct wined3d_surface
*surface
, DWORD location
)
221 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
223 TRACE("Attach surface %p to %u\n", surface
, idx
);
225 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
231 case SFLAG_INTEXTURE
:
232 case SFLAG_INSRGBTEX
:
233 srgb
= location
== SFLAG_INSRGBTEX
;
234 surface_prepare_texture(surface
, context
, srgb
);
235 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
236 surface
->texture_target
, surface_get_texture_name(surface
, gl_info
, srgb
),
237 surface
->texture_level
);
238 checkGLcall("glFramebufferTexture2D()");
241 case SFLAG_INRB_MULTISAMPLE
:
242 surface_prepare_rb(surface
, gl_info
, TRUE
);
243 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
244 GL_RENDERBUFFER
, surface
->rb_multisample
);
245 checkGLcall("glFramebufferRenderbuffer()");
248 case SFLAG_INRB_RESOLVED
:
249 surface_prepare_rb(surface
, gl_info
, FALSE
);
250 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
251 GL_RENDERBUFFER
, surface
->rb_resolved
);
252 checkGLcall("glFramebufferRenderbuffer()");
256 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
262 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
263 checkGLcall("glFramebufferTexture2D()");
267 /* Context activation is done by the caller. */
268 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
270 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
273 if (!FIXME_ON(d3d
)) return;
275 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
276 if (status
== GL_FRAMEBUFFER_COMPLETE
)
278 TRACE("FBO complete\n");
282 const struct wined3d_surface
*attachment
;
285 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
287 if (!context
->current_fbo
)
289 ERR("FBO 0 is incomplete, driver bug?\n");
293 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context
->current_fbo
->location
),
294 context
->current_fbo
->location
);
296 /* Dump the FBO attachments */
297 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
299 attachment
= context
->current_fbo
->render_targets
[i
];
302 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
303 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
304 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
307 attachment
= context
->current_fbo
->depth_stencil
;
310 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
311 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
312 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
317 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
319 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
320 return buffer
? (1 << 31) | buffer
: 0;
323 static inline DWORD
context_generate_rt_mask_from_surface(const struct wined3d_surface
*target
)
325 return (1 << 31) | surface_get_gl_buffer(target
);
328 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
329 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
331 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
332 struct fbo_entry
*entry
;
334 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
335 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
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
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
340 entry
->attached
= FALSE
;
346 /* Context activation is done by the caller. */
347 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
348 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
349 DWORD location
, struct fbo_entry
*entry
)
351 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
353 context_bind_fbo(context
, target
, &entry
->id
);
354 context_clean_fbo_attachments(gl_info
, target
);
356 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
357 entry
->depth_stencil
= depth_stencil
;
358 entry
->location
= location
;
359 entry
->attached
= FALSE
;
362 /* Context activation is done by the caller. */
363 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
367 TRACE("Destroy FBO %d\n", entry
->id
);
368 context_destroy_fbo(context
, &entry
->id
);
370 --context
->fbo_entry_count
;
371 list_remove(&entry
->entry
);
372 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
373 HeapFree(GetProcessHeap(), 0, entry
);
376 /* Context activation is done by the caller. */
377 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
378 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
380 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
381 struct fbo_entry
*entry
;
383 if (depth_stencil
&& render_targets
&& render_targets
[0])
385 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
386 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
388 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
389 depth_stencil
= NULL
;
393 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
395 if (!memcmp(entry
->render_targets
,
396 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
397 && entry
->depth_stencil
== depth_stencil
&& entry
->location
== location
)
399 list_remove(&entry
->entry
);
400 list_add_head(&context
->fbo_list
, &entry
->entry
);
405 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
407 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, location
);
408 list_add_head(&context
->fbo_list
, &entry
->entry
);
409 ++context
->fbo_entry_count
;
413 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
414 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
, entry
);
415 list_remove(&entry
->entry
);
416 list_add_head(&context
->fbo_list
, &entry
->entry
);
422 /* Context activation is done by the caller. */
423 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
425 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
427 GLuint read_binding
, draw_binding
;
431 context_bind_fbo(context
, target
, &entry
->id
);
435 read_binding
= context
->fbo_read_binding
;
436 draw_binding
= context
->fbo_draw_binding
;
437 context_bind_fbo(context
, GL_FRAMEBUFFER
, &entry
->id
);
439 /* Apply render targets */
440 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
442 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->location
);
445 /* Apply depth targets */
446 if (entry
->depth_stencil
)
447 surface_set_compatible_renderbuffer(entry
->depth_stencil
, entry
->render_targets
[0]);
448 context_attach_depth_stencil_fbo(context
, target
, entry
->depth_stencil
, entry
->location
);
450 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
451 * GL contexts requirements. */
452 glReadBuffer(GL_NONE
);
453 context_set_draw_buffer(context
, GL_NONE
);
454 if (target
!= GL_FRAMEBUFFER
)
456 if (target
== GL_READ_FRAMEBUFFER
)
457 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
? &draw_binding
: NULL
);
459 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
? &read_binding
: NULL
);
462 entry
->attached
= TRUE
;
465 /* Context activation is done by the caller. */
466 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
467 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
469 struct fbo_entry
*entry
, *entry2
;
471 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
473 context_destroy_fbo_entry(context
, entry
);
476 if (context
->rebind_fbo
)
478 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
479 context
->rebind_fbo
= FALSE
;
482 if (location
== SFLAG_INDRAWABLE
)
484 context
->current_fbo
= NULL
;
485 context_bind_fbo(context
, target
, NULL
);
489 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
);
490 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
494 /* Context activation is done by the caller. */
495 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
496 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
498 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
500 context
->blit_targets
[0] = render_target
;
502 memset(&context
->blit_targets
[1], 0, clear_size
);
503 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
);
506 /* Context activation is done by the caller. */
507 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
509 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
511 if (context
->free_occlusion_query_count
)
513 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
517 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
519 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
520 checkGLcall("glGenQueriesARB");
522 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
526 WARN("Occlusion queries not supported, not allocating query id.\n");
531 query
->context
= context
;
532 list_add_head(&context
->occlusion_queries
, &query
->entry
);
535 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
537 struct wined3d_context
*context
= query
->context
;
539 list_remove(&query
->entry
);
540 query
->context
= NULL
;
542 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
544 UINT new_size
= context
->free_occlusion_query_size
<< 1;
545 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
546 new_size
* sizeof(*context
->free_occlusion_queries
));
550 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
554 context
->free_occlusion_query_size
= new_size
;
555 context
->free_occlusion_queries
= new_data
;
558 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
561 /* Context activation is done by the caller. */
562 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
564 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
566 if (context
->free_event_query_count
)
568 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
572 if (gl_info
->supported
[ARB_SYNC
])
574 /* Using ARB_sync, not much to do here. */
575 query
->object
.sync
= NULL
;
576 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
578 else if (gl_info
->supported
[APPLE_FENCE
])
580 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
581 checkGLcall("glGenFencesAPPLE");
583 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
585 else if(gl_info
->supported
[NV_FENCE
])
587 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
588 checkGLcall("glGenFencesNV");
590 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
594 WARN("Event queries not supported, not allocating query id.\n");
595 query
->object
.id
= 0;
599 query
->context
= context
;
600 list_add_head(&context
->event_queries
, &query
->entry
);
603 void context_free_event_query(struct wined3d_event_query
*query
)
605 struct wined3d_context
*context
= query
->context
;
607 list_remove(&query
->entry
);
608 query
->context
= NULL
;
610 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
612 UINT new_size
= context
->free_event_query_size
<< 1;
613 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
614 new_size
* sizeof(*context
->free_event_queries
));
618 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
622 context
->free_event_query_size
= new_size
;
623 context
->free_event_queries
= new_data
;
626 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
629 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
631 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
632 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
636 for (i
= 0; i
< device
->context_count
; ++i
)
638 struct wined3d_context
*context
= device
->contexts
[i
];
639 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
640 struct fbo_entry
*entry
, *entry2
;
642 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
644 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
648 if (entry
->depth_stencil
== surface
)
650 callback(context
, entry
);
654 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
656 if (entry
->render_targets
[j
] == surface
)
658 callback(context
, entry
);
666 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
668 list_remove(&entry
->entry
);
669 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
672 void context_resource_released(const struct wined3d_device
*device
,
673 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
675 if (!device
->d3d_initialized
) return;
679 case WINED3D_RTYPE_SURFACE
:
680 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
681 context_queue_fbo_entry_destruction
);
689 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
691 entry
->attached
= FALSE
;
694 void context_resource_unloaded(const struct wined3d_device
*device
,
695 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
699 case WINED3D_RTYPE_SURFACE
:
700 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
701 context_detach_fbo_entry
);
709 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
711 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
712 struct fbo_entry
*entry
= context
->current_fbo
;
715 if (!entry
|| context
->rebind_fbo
) return;
717 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
719 if (surface
== entry
->render_targets
[i
])
721 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
722 context
->rebind_fbo
= TRUE
;
727 if (surface
== entry
->depth_stencil
)
729 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
730 context
->rebind_fbo
= TRUE
;
734 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
736 int current
= GetPixelFormat(dc
);
738 if (current
== format
) return TRUE
;
742 if (!SetPixelFormat(dc
, format
, NULL
))
744 /* This may also happen if the dc belongs to a destroyed window. */
745 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
746 format
, dc
, GetLastError());
752 /* By default WGL doesn't allow pixel format adjustments but we need it
753 * here. For this reason there's a Wine specific wglSetPixelFormat()
754 * which allows us to set the pixel format multiple times. Only use it
755 * when really needed. */
756 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
758 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
760 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
767 /* OpenGL doesn't allow pixel format adjustments. Print an error and
768 * continue using the old format. There's a big chance that the old
769 * format works although with a performance hit and perhaps rendering
771 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
772 format
, dc
, current
);
776 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
778 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
781 if (!context_set_pixel_format(ctx
->gl_info
, ctx
->hdc
, ctx
->pixel_format
))
783 WARN("Failed to set pixel format %d on device context %p.\n",
784 ctx
->pixel_format
, ctx
->hdc
);
788 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
792 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
793 ctx
->glCtx
, ctx
->hdc
, GetLastError());
795 WARN("Trying fallback to the backup window.\n");
797 /* FIXME: If the context is destroyed it's no longer associated with
798 * a swapchain, so we can't use the swapchain to get a backup dc. To
799 * make this work windowless contexts would need to be handled by the
803 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
804 context_set_current(NULL
);
808 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
810 context_set_current(NULL
);
814 if (!context_set_pixel_format(ctx
->gl_info
, dc
, ctx
->pixel_format
))
816 ERR("Failed to set pixel format %d on device context %p.\n",
817 ctx
->pixel_format
, dc
);
818 context_set_current(NULL
);
822 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
824 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
826 context_set_current(NULL
);
833 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
, int pf
)
835 if (!context_set_pixel_format(gl_info
, dc
, pf
))
837 ERR("Failed to restore pixel format %d on device context %p.\n", pf
, dc
);
838 context_set_current(NULL
);
842 if (!wglMakeCurrent(dc
, gl_ctx
))
844 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
845 gl_ctx
, dc
, GetLastError());
846 context_set_current(NULL
);
850 static void context_update_window(struct wined3d_context
*context
)
852 if (context
->win_handle
== context
->swapchain
->win_handle
)
855 TRACE("Updating context %p window from %p to %p.\n",
856 context
, context
->win_handle
, context
->swapchain
->win_handle
);
860 /* You'd figure ReleaseDC() would fail if the DC doesn't match the
861 * window. However, that's not what actually happens, and there are
862 * user32 tests that confirm ReleaseDC() with the wrong window is
863 * supposed to succeed. So explicitly check that the DC belongs to
864 * the window, since we want to avoid releasing a DC that belongs to
865 * some other window if the original window was already destroyed. */
866 if (WindowFromDC(context
->hdc
) != context
->win_handle
)
868 WARN("DC %p does not belong to window %p.\n",
869 context
->hdc
, context
->win_handle
);
871 else if (!ReleaseDC(context
->win_handle
, context
->hdc
))
873 ERR("Failed to release device context %p, last error %#x.\n",
874 context
->hdc
, GetLastError());
877 else context
->valid
= 1;
879 context
->win_handle
= context
->swapchain
->win_handle
;
881 if (!(context
->hdc
= GetDC(context
->win_handle
)))
883 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
887 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
889 ERR("Failed to set pixel format %d on device context %p.\n",
890 context
->pixel_format
, context
->hdc
);
894 context_set_gl_context(context
);
902 /* Do not call while under the GL lock. */
903 static void context_destroy_gl_resources(struct wined3d_context
*context
)
905 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
906 struct wined3d_occlusion_query
*occlusion_query
;
907 struct wined3d_event_query
*event_query
;
908 struct fbo_entry
*entry
, *entry2
;
914 restore_ctx
= wglGetCurrentContext();
915 restore_dc
= wglGetCurrentDC();
916 restore_pf
= GetPixelFormat(restore_dc
);
918 if (context
->valid
&& restore_ctx
!= context
->glCtx
)
919 context_set_gl_context(context
);
923 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
925 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
926 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
927 occlusion_query
->context
= NULL
;
930 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
934 if (gl_info
->supported
[ARB_SYNC
])
936 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
938 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
939 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
941 event_query
->context
= NULL
;
944 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
946 if (!context
->valid
) entry
->id
= 0;
947 context_destroy_fbo_entry(context
, entry
);
950 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
952 if (!context
->valid
) entry
->id
= 0;
953 context_destroy_fbo_entry(context
, entry
);
958 if (context
->dummy_arbfp_prog
)
960 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
963 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
964 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
966 if (gl_info
->supported
[ARB_SYNC
])
968 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
970 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
973 else if (gl_info
->supported
[APPLE_FENCE
])
975 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
977 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
980 else if (gl_info
->supported
[NV_FENCE
])
982 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
984 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
988 checkGLcall("context cleanup");
991 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
992 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
996 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
, restore_pf
);
998 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1000 ERR("Failed to disable GL context.\n");
1003 ReleaseDC(context
->win_handle
, context
->hdc
);
1005 if (!wglDeleteContext(context
->glCtx
))
1007 DWORD err
= GetLastError();
1008 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1012 DWORD
context_get_tls_idx(void)
1014 return wined3d_context_tls_idx
;
1017 void context_set_tls_idx(DWORD idx
)
1019 wined3d_context_tls_idx
= idx
;
1022 struct wined3d_context
*context_get_current(void)
1024 return TlsGetValue(wined3d_context_tls_idx
);
1027 /* Do not call while under the GL lock. */
1028 BOOL
context_set_current(struct wined3d_context
*ctx
)
1030 struct wined3d_context
*old
= context_get_current();
1034 TRACE("Already using D3D context %p.\n", ctx
);
1042 TRACE("Switching away from destroyed context %p.\n", old
);
1043 context_destroy_gl_resources(old
);
1044 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1045 HeapFree(GetProcessHeap(), 0, old
);
1057 ERR("Trying to make invalid context %p current\n", ctx
);
1061 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1062 if (!context_set_gl_context(ctx
))
1066 else if(wglGetCurrentContext())
1068 TRACE("Clearing current D3D context.\n");
1069 if (!wglMakeCurrent(NULL
, NULL
))
1071 DWORD err
= GetLastError();
1072 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1073 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1078 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1081 void context_release(struct wined3d_context
*context
)
1083 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1087 if (!context
->level
)
1088 WARN("Context %p is not active.\n", context
);
1089 else if (context
!= context_get_current())
1090 WARN("Context %p is not the current context.\n", context
);
1093 if (!--context
->level
&& context
->restore_ctx
)
1095 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1096 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
, context
->restore_pf
);
1097 context
->restore_ctx
= NULL
;
1098 context
->restore_dc
= NULL
;
1102 static void context_enter(struct wined3d_context
*context
)
1104 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1106 if (!context
->level
++)
1108 const struct wined3d_context
*current_context
= context_get_current();
1109 HGLRC current_gl
= wglGetCurrentContext();
1111 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1113 TRACE("Another GL context (%p on device context %p) is already current.\n",
1114 current_gl
, wglGetCurrentDC());
1115 context
->restore_ctx
= current_gl
;
1116 context
->restore_dc
= wglGetCurrentDC();
1117 context
->restore_pf
= GetPixelFormat(context
->restore_dc
);
1122 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1124 DWORD rep
= context
->state_table
[state
].representative
;
1128 if (isStateDirty(context
, rep
)) return;
1130 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1131 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1132 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1133 context
->isStateDirty
[idx
] |= (1 << shift
);
1136 /* This function takes care of wined3d pixel format selection. */
1137 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1138 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1139 BOOL auxBuffers
, BOOL findCompatible
)
1142 BYTE redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1143 BYTE depthBits
=0, stencilBits
=0;
1144 unsigned int current_value
;
1145 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1148 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1149 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1150 auxBuffers
, findCompatible
);
1152 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1154 ERR("Unable to get color bits for format %s (%#x)!\n",
1155 debug_d3dformat(color_format
->id
), color_format
->id
);
1159 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1162 for (i
= 0; i
< cfg_count
; ++i
)
1164 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1167 /* For now only accept RGBA formats. Perhaps some day we will
1168 * allow floating point formats for pbuffers. */
1169 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1171 /* In window mode we need a window drawable format and double buffering. */
1172 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1174 if (cfg
->redSize
< redBits
)
1176 if (cfg
->greenSize
< greenBits
)
1178 if (cfg
->blueSize
< blueBits
)
1180 if (cfg
->alphaSize
< alphaBits
)
1182 if (cfg
->depthSize
< depthBits
)
1184 if (stencilBits
&& cfg
->stencilSize
!= stencilBits
)
1186 /* Check multisampling support. */
1187 if (cfg
->numSamples
)
1191 /* We try to locate a format which matches our requirements exactly. In case of
1192 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1193 if (cfg
->depthSize
== depthBits
)
1195 if (cfg
->stencilSize
== stencilBits
)
1197 if (cfg
->alphaSize
== alphaBits
)
1199 /* We like to have aux buffers in backbuffer mode */
1200 if (auxBuffers
&& cfg
->auxBuffers
)
1202 if (cfg
->redSize
== redBits
1203 && cfg
->greenSize
== greenBits
1204 && cfg
->blueSize
== blueBits
)
1207 if (value
> current_value
)
1209 iPixelFormat
= cfg
->iPixelFormat
;
1210 current_value
= value
;
1214 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1215 if(!iPixelFormat
&& !findCompatible
) {
1216 ERR("Can't find a suitable iPixelFormat\n");
1218 } else if(!iPixelFormat
) {
1219 PIXELFORMATDESCRIPTOR pfd
;
1221 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1222 /* PixelFormat selection */
1223 ZeroMemory(&pfd
, sizeof(pfd
));
1224 pfd
.nSize
= sizeof(pfd
);
1226 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1227 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1228 pfd
.cAlphaBits
= alphaBits
;
1229 pfd
.cColorBits
= colorBits
;
1230 pfd
.cDepthBits
= depthBits
;
1231 pfd
.cStencilBits
= stencilBits
;
1232 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1234 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1236 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1237 ERR("Can't find a suitable iPixelFormat\n");
1242 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1243 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1244 return iPixelFormat
;
1247 /* Context activation is done by the caller. */
1248 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1250 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1251 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1253 for (i
= 0; i
< count
; ++i
)
1255 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1256 checkGLcall("glActiveTextureARB");
1258 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1259 checkGLcall("glBindTexture");
1261 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1263 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1264 checkGLcall("glBindTexture");
1267 if (gl_info
->supported
[EXT_TEXTURE3D
])
1269 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1270 checkGLcall("glBindTexture");
1273 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1275 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1276 checkGLcall("glBindTexture");
1281 BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1283 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1284 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1287 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1288 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1292 case GL_DEBUG_TYPE_ERROR_ARB
:
1293 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1296 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1297 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1298 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1299 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1302 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1303 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1307 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1312 /* Do not call while under the GL lock. */
1313 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1314 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1316 struct wined3d_device
*device
= swapchain
->device
;
1317 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1318 const struct wined3d_format
*color_format
;
1319 struct wined3d_context
*ret
;
1320 BOOL auxBuffers
= FALSE
;
1321 HGLRC ctx
, share_ctx
;
1328 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1330 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1334 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1335 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1336 if (!ret
->blit_targets
)
1339 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1340 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1341 if (!ret
->draw_buffers
)
1344 ret
->free_occlusion_query_size
= 4;
1345 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1346 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1347 if (!ret
->free_occlusion_queries
)
1350 list_init(&ret
->occlusion_queries
);
1352 ret
->free_event_query_size
= 4;
1353 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1354 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1355 if (!ret
->free_event_queries
)
1358 list_init(&ret
->event_queries
);
1359 list_init(&ret
->fbo_list
);
1360 list_init(&ret
->fbo_destroy_list
);
1362 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1364 WARN("Failed to retireve device context, trying swapchain backup.\n");
1366 if (!(hdc
= swapchain_get_backup_dc(swapchain
)))
1368 ERR("Failed to retrieve a device context.\n");
1373 color_format
= target
->resource
.format
;
1375 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1376 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1377 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1381 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1382 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1383 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1384 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1387 /* DirectDraw supports 8bit paletted render targets and these are used by
1388 * old games like StarCraft and C&C. Most modern hardware doesn't support
1389 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1390 * conversion (ab)uses the alpha component for storing the palette index.
1391 * For this reason we require a format with 8bit alpha, so request
1393 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1394 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1396 /* Try to find a pixel format which matches our requirements. */
1397 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1399 /* Try to locate a compatible format if we weren't able to find anything. */
1402 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1403 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1406 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1409 ERR("Can't find a suitable pixel format.\n");
1415 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1417 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1418 context_release(ret
);
1422 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1423 if (gl_info
->p_wglCreateContextAttribsARB
)
1425 unsigned int ctx_attrib_idx
= 0;
1426 GLint ctx_attribs
[3];
1428 if (context_debug_output_enabled(gl_info
))
1430 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1431 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_DEBUG_BIT_ARB
;
1433 ctx_attribs
[ctx_attrib_idx
] = 0;
1435 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1437 ERR("Failed to create a WGL context.\n");
1438 context_release(ret
);
1444 if (!(ctx
= wglCreateContext(hdc
)))
1446 ERR("Failed to create a WGL context.\n");
1447 context_release(ret
);
1451 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1453 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1454 context_release(ret
);
1455 if (!wglDeleteContext(ctx
))
1456 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1461 if (!device_context_add(device
, ret
))
1463 ERR("Failed to add the newly created context to the context list\n");
1464 context_release(ret
);
1465 if (!wglDeleteContext(ctx
))
1466 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1470 ret
->gl_info
= gl_info
;
1471 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1472 ret
->state_table
= device
->StateTable
;
1474 /* Mark all states dirty to force a proper initialization of the states
1475 * on the first use of the context. */
1476 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1478 if (ret
->state_table
[state
].representative
)
1479 context_invalidate_state(ret
, state
);
1482 ret
->swapchain
= swapchain
;
1483 ret
->current_rt
= target
;
1484 ret
->tid
= GetCurrentThreadId();
1486 ret
->render_offscreen
= surface_is_offscreen(target
);
1487 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1491 ret
->win_handle
= swapchain
->win_handle
;
1493 ret
->pixel_format
= pixel_format
;
1495 /* Set up the context defaults */
1496 if (!context_set_current(ret
))
1498 ERR("Cannot activate context to set up defaults.\n");
1499 device_context_remove(device
, ret
);
1500 context_release(ret
);
1501 if (!wglDeleteContext(ctx
))
1502 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1506 if (context_debug_output_enabled(gl_info
))
1508 GL_EXTCALL(glDebugMessageCallbackARB(wined3d_debug_callback
, ret
));
1509 if (TRACE_ON(d3d_synchronous
))
1510 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
);
1511 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1514 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR_ARB
,
1515 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1519 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
,
1520 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1521 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
,
1522 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1523 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY_ARB
,
1524 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1526 if (WARN_ON(d3d_perf
))
1528 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE_ARB
,
1529 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1533 switch (swapchain
->desc
.swap_interval
)
1535 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1538 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1539 case WINED3DPRESENT_INTERVAL_ONE
:
1542 case WINED3DPRESENT_INTERVAL_TWO
:
1545 case WINED3DPRESENT_INTERVAL_THREE
:
1548 case WINED3DPRESENT_INTERVAL_FOUR
:
1552 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1556 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1558 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1559 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1560 swap_interval
, ret
, GetLastError());
1563 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1565 TRACE("Setting up the screen\n");
1567 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1568 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1570 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1571 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1573 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1574 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1576 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1577 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1578 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1579 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1581 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
1583 /* Most textures will use client storage if supported. Exceptions are
1584 * non-native power of 2 textures and textures in DIB sections. */
1585 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1586 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1588 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1590 /* Direct3D always uses n-1 weights for n world matrices and uses
1591 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1592 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1593 * enabled as well. */
1594 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1595 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1597 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1599 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1600 * the previous texture where to source the offset from is always unit - 1.
1602 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1604 context_active_texture(ret
, gl_info
, s
);
1605 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1606 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1607 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1610 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1612 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1613 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1614 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1615 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1618 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1619 * program and the dummy program is destroyed when the context is destroyed.
1621 const char *dummy_program
=
1623 "MOV result.color, fragment.color.primary;\n"
1625 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1626 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1627 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1630 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1632 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1634 context_active_texture(ret
, gl_info
, s
);
1635 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1636 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1640 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1642 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1644 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1646 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1648 ret
->select_shader
= 1;
1650 /* If this happens to be the first context for the device, dummy textures
1651 * are not created yet. In that case, they will be created (and bound) by
1652 * create_dummy_textures right after this context is initialized. */
1653 if (device
->dummy_texture_2d
[0])
1654 bind_dummy_textures(device
, ret
);
1656 TRACE("Created context %p.\n", ret
);
1661 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1662 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1663 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1664 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1665 HeapFree(GetProcessHeap(), 0, ret
);
1669 /* Do not call while under the GL lock. */
1670 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1674 TRACE("Destroying ctx %p\n", context
);
1676 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1678 context_destroy_gl_resources(context
);
1679 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1684 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1685 in wined3d_adapter may go away in the meantime */
1686 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
1687 *gl_info
= *context
->gl_info
;
1688 context
->gl_info
= gl_info
;
1689 context
->destroyed
= 1;
1693 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1694 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1695 device_context_remove(device
, context
);
1696 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1699 /* Context activation is done by the caller. */
1700 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
1702 const GLdouble projection
[] =
1704 2.0 / width
, 0.0, 0.0, 0.0,
1705 0.0, 2.0 / height
, 0.0, 0.0,
1707 -1.0, -1.0, -1.0, 1.0,
1710 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
1711 checkGLcall("glMatrixMode(GL_PROJECTION)");
1712 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
1713 checkGLcall("glLoadMatrixd");
1714 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
1715 checkGLcall("glViewport");
1718 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
1720 const struct wined3d_surface
*rt
= context
->current_rt
;
1722 if (rt
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
1723 && rt
->container
.u
.swapchain
->front_buffer
== rt
)
1727 GetClientRect(context
->win_handle
, &window_size
);
1728 size
->cx
= window_size
.right
- window_size
.left
;
1729 size
->cy
= window_size
.bottom
- window_size
.top
;
1734 size
->cx
= rt
->resource
.width
;
1735 size
->cy
= rt
->resource
.height
;
1738 /*****************************************************************************
1741 * Sets up a context for DirectDraw blitting.
1742 * All texture units are disabled, texture unit 0 is set as current unit
1743 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1744 * color writing enabled for all channels
1745 * register combiners disabled, shaders disabled
1746 * world matrix is set to identity, texture matrix 0 too
1747 * projection matrix is setup for drawing screen coordinates
1750 * This: Device to activate the context for
1751 * context: Context to setup
1753 *****************************************************************************/
1754 /* Context activation is done by the caller. */
1755 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
1758 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1762 TRACE("Setting up context %p for blitting\n", context
);
1764 context_get_rt_size(context
, &rt_size
);
1766 if (context
->last_was_blit
)
1768 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
1770 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1771 context
->blit_w
= rt_size
.cx
;
1772 context
->blit_h
= rt_size
.cy
;
1773 /* No need to dirtify here, the states are still dirtified because
1774 * they weren't applied since the last SetupForBlit() call. */
1776 TRACE("Context is already set up for blitting, nothing to do\n");
1779 context
->last_was_blit
= TRUE
;
1781 /* Disable all textures. The caller can then bind a texture it wants to blit
1784 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1785 * function texture unit. No need to care for higher samplers
1787 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1789 sampler
= device
->rev_tex_unit_map
[i
];
1790 context_active_texture(context
, gl_info
, i
);
1792 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1794 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1795 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1797 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1798 checkGLcall("glDisable GL_TEXTURE_3D");
1799 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1801 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1802 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1804 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1805 checkGLcall("glDisable GL_TEXTURE_2D");
1807 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1808 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1810 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1812 if (sampler
< MAX_TEXTURES
)
1813 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1814 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1817 context_active_texture(context
, gl_info
, 0);
1819 sampler
= device
->rev_tex_unit_map
[0];
1821 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1823 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1824 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1826 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1827 checkGLcall("glDisable GL_TEXTURE_3D");
1828 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1830 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1831 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1833 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1834 checkGLcall("glDisable GL_TEXTURE_2D");
1836 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1838 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
1839 checkGLcall("glMatrixMode(GL_TEXTURE)");
1840 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1841 checkGLcall("glLoadIdentity()");
1843 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1845 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1846 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
1847 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1850 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1852 if (sampler
< MAX_TEXTURES
)
1854 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
1855 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1857 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1860 /* Other misc states */
1861 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
1862 checkGLcall("glDisable(GL_ALPHA_TEST)");
1863 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
1864 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
1865 checkGLcall("glDisable GL_LIGHTING");
1866 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
1867 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
1868 checkGLcall("glDisable GL_DEPTH_TEST");
1869 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
1870 glDisableWINE(GL_FOG
);
1871 checkGLcall("glDisable GL_FOG");
1872 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
1873 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
1874 checkGLcall("glDisable GL_BLEND");
1875 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
1876 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
1877 checkGLcall("glDisable GL_CULL_FACE");
1878 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
1879 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
1880 checkGLcall("glDisable GL_STENCIL_TEST");
1881 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
1882 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
1883 checkGLcall("glDisable GL_SCISSOR_TEST");
1884 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
1885 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1887 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
1888 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1889 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
1891 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1892 checkGLcall("glColorMask");
1893 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
1894 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
1895 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
1896 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
1897 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1899 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
1900 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
1901 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1904 /* Setup transforms */
1905 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
1906 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1907 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1908 checkGLcall("glLoadIdentity()");
1909 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
1911 context
->last_was_rhw
= TRUE
;
1912 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
1914 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1915 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1916 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1917 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1918 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1919 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1920 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
1922 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1924 /* Disable shaders */
1925 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
1926 context
->select_shader
= 1;
1927 context
->load_constants
= 1;
1929 context
->blit_w
= rt_size
.cx
;
1930 context
->blit_h
= rt_size
.cy
;
1931 context_invalidate_state(context
, STATE_VIEWPORT
);
1932 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
1935 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
1937 return rt_mask
& (1 << 31);
1940 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
1942 return rt_mask
& ~(1 << 31);
1945 /* Context activation is done by the caller. */
1946 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
1948 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1952 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
1953 checkGLcall("glDrawBuffer()");
1955 else if (is_rt_mask_onscreen(rt_mask
))
1957 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
1958 checkGLcall("glDrawBuffer()");
1962 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1969 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
1971 context
->draw_buffers
[i
] = GL_NONE
;
1977 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1979 GL_EXTCALL(glDrawBuffersARB(i
, context
->draw_buffers
));
1980 checkGLcall("glDrawBuffers()");
1984 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
1985 checkGLcall("glDrawBuffer()");
1990 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
1995 /* Context activation is done by the caller. */
1996 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
1998 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1999 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2000 DWORD new_mask
= context_generate_rt_mask(buffer
);
2002 if (new_mask
== *current_mask
)
2005 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2006 checkGLcall("glDrawBuffer()");
2008 *current_mask
= new_mask
;
2011 /* Context activation is done by the caller. */
2012 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2014 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0
+ unit
));
2015 checkGLcall("glActiveTextureARB");
2016 context
->active_texture
= unit
;
2019 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2021 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2022 DWORD unit
= context
->active_texture
;
2023 DWORD old_texture_type
= context
->texture_type
[unit
];
2027 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2028 checkGLcall("glBindTexture");
2035 if (old_texture_type
!= target
)
2037 const struct wined3d_device
*device
= context
->swapchain
->device
;
2039 switch (old_texture_type
)
2045 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2046 checkGLcall("glBindTexture");
2048 case GL_TEXTURE_RECTANGLE_ARB
:
2049 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2050 checkGLcall("glBindTexture");
2052 case GL_TEXTURE_CUBE_MAP
:
2053 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2054 checkGLcall("glBindTexture");
2057 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2058 checkGLcall("glBindTexture");
2061 ERR("Unexpected texture target %#x\n", old_texture_type
);
2064 context
->texture_type
[unit
] = target
;
2068 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2070 if (context
->render_offscreen
== offscreen
) return;
2072 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2073 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2074 context_invalidate_state(context
, STATE_VIEWPORT
);
2075 context_invalidate_state(context
, STATE_SCISSORRECT
);
2076 context_invalidate_state(context
, STATE_FRONTFACE
);
2077 context
->render_offscreen
= offscreen
;
2080 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2081 const struct wined3d_format
*required
)
2083 BYTE existing_depth
, existing_stencil
, required_depth
, required_stencil
;
2085 if (existing
== required
) return TRUE
;
2086 if ((existing
->flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
2088 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
2089 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
2091 if(existing_depth
< required_depth
) return FALSE
;
2092 /* If stencil bits are used the exact amount is required - otherwise wrapping
2093 * won't work correctly */
2094 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
2098 /* The caller provides a context */
2099 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2100 const struct wined3d_surface
*depth_stencil
)
2102 /* Onscreen surfaces are always in a swapchain */
2103 struct wined3d_swapchain
*swapchain
= context
->current_rt
->container
.u
.swapchain
;
2105 if (context
->render_offscreen
|| !depth_stencil
) return;
2106 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->resource
.format
)) return;
2108 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2109 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2111 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2113 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2114 surface_load_location(context
->current_rt
, SFLAG_INTEXTURE
, NULL
);
2115 swapchain
->render_to_fbo
= TRUE
;
2116 swapchain_update_draw_bindings(swapchain
);
2117 context_set_render_offscreen(context
, TRUE
);
2120 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
2122 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2124 else if (rt
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
2125 return context_generate_rt_mask_from_surface(rt
);
2127 return context_generate_rt_mask(device
->offscreenBuffer
);
2130 /* Context activation is done by the caller. */
2131 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2133 struct wined3d_surface
*rt
= context
->current_rt
;
2134 DWORD rt_mask
, *cur_mask
;
2136 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2138 context_validate_onscreen_formats(context
, NULL
);
2140 if (context
->render_offscreen
)
2142 surface_internal_preload(rt
, SRGB_RGB
);
2144 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->draw_binding
);
2145 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2152 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
2153 rt_mask
= context_generate_rt_mask_from_surface(rt
);
2158 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt
);
2161 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2163 if (rt_mask
!= *cur_mask
)
2165 context_apply_draw_buffers(context
, rt_mask
);
2166 *cur_mask
= rt_mask
;
2169 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2171 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2174 SetupForBlit(device
, context
);
2175 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2178 static BOOL
context_validate_rt_config(UINT rt_count
,
2179 struct wined3d_surface
* const *rts
, const struct wined3d_surface
*ds
)
2183 if (ds
) return TRUE
;
2185 for (i
= 0; i
< rt_count
; ++i
)
2187 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2191 WARN("Invalid render target config, need at least one attachment.\n");
2195 /* Context activation is done by the caller. */
2196 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2197 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2199 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2200 DWORD rt_mask
= 0, *cur_mask
;
2202 struct wined3d_surface
**rts
= fb
->render_targets
;
2204 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2205 || rt_count
!= context
->gl_info
->limits
.buffers
)
2207 if (!context_validate_rt_config(rt_count
, rts
, fb
->depth_stencil
))
2210 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2212 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2214 if (!rt_count
|| surface_is_offscreen(rts
[0]))
2216 for (i
= 0; i
< rt_count
; ++i
)
2218 context
->blit_targets
[i
] = rts
[i
];
2219 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2220 rt_mask
|= (1 << i
);
2222 while (i
< context
->gl_info
->limits
.buffers
)
2224 context
->blit_targets
[i
] = NULL
;
2227 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, fb
->depth_stencil
,
2228 rt_count
? rts
[0]->draw_binding
: SFLAG_INTEXTURE
);
2232 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2233 rt_mask
= context_generate_rt_mask_from_surface(rts
[0]);
2236 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2237 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2238 * state management allows this */
2239 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2243 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt_count
? rts
[0] : NULL
);
2246 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2247 && (!rt_count
|| surface_is_offscreen(rts
[0])))
2249 for (i
= 0; i
< rt_count
; ++i
)
2251 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
) rt_mask
|= (1 << i
);
2256 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt_count
? rts
[0] : NULL
);
2259 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2261 if (rt_mask
!= *cur_mask
)
2263 context_apply_draw_buffers(context
, rt_mask
);
2264 *cur_mask
= rt_mask
;
2265 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2268 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2270 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2273 if (context
->last_was_blit
)
2274 context
->last_was_blit
= FALSE
;
2276 /* Blending and clearing should be orthogonal, but tests on the nvidia
2277 * driver show that disabling blending when clearing improves the clearing
2278 * performance incredibly. */
2279 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2280 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2281 checkGLcall("glEnable GL_SCISSOR_TEST");
2283 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2284 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2285 context_invalidate_state(context
, STATE_SCISSORRECT
);
2290 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_device
*device
)
2292 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
2293 struct wined3d_surface
**rts
= state
->fb
->render_targets
;
2294 struct wined3d_shader
*ps
= state
->pixel_shader
;
2295 DWORD rt_mask
, rt_mask_bits
;
2298 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return context_generate_rt_mask_no_fbo(device
, rts
[0]);
2299 else if (!context
->render_offscreen
) return context_generate_rt_mask_from_surface(rts
[0]);
2301 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2302 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2303 rt_mask_bits
= rt_mask
;
2305 while (rt_mask_bits
)
2307 rt_mask_bits
&= ~(1 << i
);
2308 if (!rts
[i
] || rts
[i
]->resource
.format
->id
== WINED3DFMT_NULL
)
2309 rt_mask
&= ~(1 << i
);
2317 /* Context activation is done by the caller. */
2318 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2320 const struct wined3d_device
*device
= context
->swapchain
->device
;
2321 const struct wined3d_fb_state
*fb
= state
->fb
;
2322 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2325 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2327 if (!context
->render_offscreen
)
2329 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2333 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, fb
->render_targets
, fb
->depth_stencil
,
2334 fb
->render_targets
[0]->draw_binding
);
2338 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2339 if (rt_mask
!= *cur_mask
)
2341 context_apply_draw_buffers(context
, rt_mask
);
2342 *cur_mask
= rt_mask
;
2346 /* Context activation is done by the caller. */
2347 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2349 const struct wined3d_device
*device
= context
->swapchain
->device
;
2350 DWORD rt_mask
, *cur_mask
;
2352 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2354 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2355 rt_mask
= find_draw_buffers_mask(context
, device
);
2356 if (rt_mask
!= *cur_mask
)
2358 context_apply_draw_buffers(context
, rt_mask
);
2359 *cur_mask
= rt_mask
;
2363 /* Context activation is done by the caller. */
2364 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
2366 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
2367 const struct StateEntry
*state_table
= context
->state_table
;
2368 const struct wined3d_fb_state
*fb
= state
->fb
;
2371 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
2372 fb
->render_targets
, fb
->depth_stencil
))
2375 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
2377 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2380 /* Preload resources before FBO setup. Texture preload in particular may
2381 * result in changes to the current FBO, due to using e.g. FBO blits for
2382 * updating a resource location. */
2383 device_update_tex_unit_map(device
);
2384 device_preload_textures(device
);
2385 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
))
2386 device_update_stream_info(device
, context
->gl_info
);
2387 if (state
->index_buffer
)
2389 if (device
->stream_info
.all_vbo
)
2390 wined3d_buffer_preload(state
->index_buffer
);
2392 buffer_get_sysmem(state
->index_buffer
, context
->gl_info
);
2395 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2397 DWORD rep
= context
->dirtyArray
[i
];
2398 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2399 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2400 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2401 state_table
[rep
].apply(context
, state
, rep
);
2404 if (context
->select_shader
)
2406 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
2407 context
->select_shader
= 0;
2410 if (context
->load_constants
)
2412 device
->shader_backend
->shader_load_constants(device
->shader_priv
,
2414 context
->load_constants
= 0;
2417 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2419 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2422 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2423 context
->last_was_blit
= FALSE
;
2428 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
2430 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
2432 render_offscreen
= surface_is_offscreen(target
);
2433 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
2435 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2436 * the alpha blend state changes with different render target formats. */
2437 if (!context
->current_rt
)
2439 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2443 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
2444 const struct wined3d_format
*new = target
->resource
.format
;
2446 if (old
->id
!= new->id
)
2448 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2449 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
2450 || !(new->flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
2451 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2453 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2454 if ((old
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
2455 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
2458 /* When switching away from an offscreen render target, and we're not
2459 * using FBOs, we have to read the drawable into the texture. This is
2460 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2461 * are some things that need care though. PreLoad needs a GL context,
2462 * and FindContext is called before the context is activated. It also
2463 * has to be called with the old rendertarget active, otherwise a
2464 * wrong drawable is read. */
2465 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2466 && old_render_offscreen
&& context
->current_rt
!= target
)
2468 /* Read the back buffer of the old drawable into the destination texture. */
2469 if (context
->current_rt
->texture_name_srgb
)
2470 surface_internal_preload(context
->current_rt
, SRGB_SRGB
);
2471 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2472 surface_modify_location(context
->current_rt
, SFLAG_INDRAWABLE
, FALSE
);
2476 context
->current_rt
= target
;
2477 context_set_render_offscreen(context
, render_offscreen
);
2480 /* Do not call while under the GL lock. */
2481 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
2483 struct wined3d_context
*current_context
= context_get_current();
2484 struct wined3d_context
*context
;
2486 TRACE("device %p, target %p.\n", device
, target
);
2488 if (current_context
&& current_context
->destroyed
)
2489 current_context
= NULL
;
2494 && current_context
->current_rt
2495 && current_context
->swapchain
->device
== device
)
2497 target
= current_context
->current_rt
;
2501 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
2502 if (swapchain
->back_buffers
)
2503 target
= swapchain
->back_buffers
[0];
2505 target
= swapchain
->front_buffer
;
2509 if (current_context
&& current_context
->current_rt
== target
)
2511 context
= current_context
;
2513 else if (target
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
2515 TRACE("Rendering onscreen.\n");
2517 context
= swapchain_get_context(target
->container
.u
.swapchain
);
2521 TRACE("Rendering offscreen.\n");
2523 /* Stay with the current context if possible. Otherwise use the
2524 * context for the primary swapchain. */
2525 if (current_context
&& current_context
->swapchain
->device
== device
)
2526 context
= current_context
;
2528 context
= swapchain_get_context(device
->swapchains
[0]);
2531 context_update_window(context
);
2532 context_setup_target(context
, target
);
2533 context_enter(context
);
2534 if (!context
->valid
) return context
;
2536 if (context
!= current_context
)
2538 if (!context_set_current(context
))
2539 ERR("Failed to activate the new context.\n");
2541 else if (context
->restore_ctx
)
2543 context_set_gl_context(context
);