2 * Context and render target management in wined3d
4 * Copyright 2007-2008 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
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
31 static DWORD wined3d_context_tls_idx
;
33 /* FBO helper functions */
35 /* GL locking is done by the caller */
36 void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint
*fbo
)
38 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
49 gl_info
->fbo_ops
.glGenFramebuffers(1, fbo
);
50 checkGLcall("glGenFramebuffers()");
51 TRACE("Created FBO %u.\n", *fbo
);
58 case GL_READ_FRAMEBUFFER
:
59 if (context
->fbo_read_binding
== f
) return;
60 context
->fbo_read_binding
= f
;
63 case GL_DRAW_FRAMEBUFFER
:
64 if (context
->fbo_draw_binding
== f
) return;
65 context
->fbo_draw_binding
= f
;
69 if (context
->fbo_read_binding
== f
70 && context
->fbo_draw_binding
== f
) return;
71 context
->fbo_read_binding
= f
;
72 context
->fbo_draw_binding
= f
;
76 FIXME("Unhandled target %#x.\n", target
);
80 gl_info
->fbo_ops
.glBindFramebuffer(target
, f
);
81 checkGLcall("glBindFramebuffer()");
84 /* GL locking is done by the caller */
85 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
89 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
91 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
92 checkGLcall("glFramebufferTexture2D()");
94 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
95 checkGLcall("glFramebufferTexture2D()");
97 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
98 checkGLcall("glFramebufferTexture2D()");
101 /* GL locking is done by the caller */
102 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint
*fbo
)
104 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
106 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
107 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
108 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
110 gl_info
->fbo_ops
.glDeleteFramebuffers(1, fbo
);
111 checkGLcall("glDeleteFramebuffers()");
114 /* GL locking is done by the caller */
115 void context_attach_depth_stencil_fbo(const struct wined3d_context
*context
,
116 GLenum fbo_target
, struct wined3d_surface
*depth_stencil
, BOOL use_render_buffer
)
118 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
120 TRACE("Attach depth stencil %p\n", depth_stencil
);
124 DWORD format_flags
= depth_stencil
->resource
.format
->flags
;
126 if (use_render_buffer
&& depth_stencil
->current_renderbuffer
)
128 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
130 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
,
131 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
132 checkGLcall("glFramebufferRenderbuffer()");
135 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
137 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
,
138 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
139 checkGLcall("glFramebufferRenderbuffer()");
144 surface_prepare_texture(depth_stencil
, gl_info
, FALSE
);
146 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
148 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
149 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
150 depth_stencil
->texture_level
);
151 checkGLcall("glFramebufferTexture2D()");
154 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
156 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
157 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
158 depth_stencil
->texture_level
);
159 checkGLcall("glFramebufferTexture2D()");
163 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
165 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
166 checkGLcall("glFramebufferTexture2D()");
169 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
171 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
172 checkGLcall("glFramebufferTexture2D()");
177 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
178 checkGLcall("glFramebufferTexture2D()");
180 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
181 checkGLcall("glFramebufferTexture2D()");
185 /* GL locking is done by the caller */
186 static void context_attach_surface_fbo(const struct wined3d_context
*context
,
187 GLenum fbo_target
, DWORD idx
, struct wined3d_surface
*surface
, DWORD location
)
189 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
191 TRACE("Attach surface %p to %u\n", surface
, idx
);
193 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
199 case SFLAG_INTEXTURE
:
200 case SFLAG_INSRGBTEX
:
201 srgb
= location
== SFLAG_INSRGBTEX
;
202 surface_prepare_texture(surface
, gl_info
, srgb
);
203 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
204 surface
->texture_target
, surface_get_texture_name(surface
, gl_info
, srgb
),
205 surface
->texture_level
);
209 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
212 checkGLcall("glFramebufferTexture2D()");
216 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
217 checkGLcall("glFramebufferTexture2D()");
221 /* GL locking is done by the caller */
222 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
224 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
227 if (!FIXME_ON(d3d
)) return;
229 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
230 if (status
== GL_FRAMEBUFFER_COMPLETE
)
232 TRACE("FBO complete\n");
236 const struct wined3d_surface
*attachment
;
239 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
241 if (!context
->current_fbo
)
243 ERR("FBO 0 is incomplete, driver bug?\n");
247 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context
->current_fbo
->location
),
248 context
->current_fbo
->location
);
250 /* Dump the FBO attachments */
251 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
253 attachment
= context
->current_fbo
->render_targets
[i
];
256 FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
257 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
258 attachment
->pow2Width
, attachment
->pow2Height
);
261 attachment
= context
->current_fbo
->depth_stencil
;
264 FIXME("\tDepth attachment: (%p) %s %ux%u\n",
265 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
266 attachment
->pow2Width
, attachment
->pow2Height
);
271 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
272 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
274 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
275 struct fbo_entry
*entry
;
277 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
278 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
279 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
280 entry
->depth_stencil
= depth_stencil
;
281 entry
->location
= location
;
282 entry
->attached
= FALSE
;
288 /* GL locking is done by the caller */
289 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
290 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
291 DWORD location
, struct fbo_entry
*entry
)
293 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
295 context_bind_fbo(context
, target
, &entry
->id
);
296 context_clean_fbo_attachments(gl_info
, target
);
298 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
299 entry
->depth_stencil
= depth_stencil
;
300 entry
->location
= location
;
301 entry
->attached
= FALSE
;
304 /* GL locking is done by the caller */
305 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
309 TRACE("Destroy FBO %d\n", entry
->id
);
310 context_destroy_fbo(context
, &entry
->id
);
312 --context
->fbo_entry_count
;
313 list_remove(&entry
->entry
);
314 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
315 HeapFree(GetProcessHeap(), 0, entry
);
319 /* GL locking is done by the caller */
320 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
321 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
323 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
324 struct fbo_entry
*entry
;
326 if (depth_stencil
&& render_targets
&& render_targets
[0])
328 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
329 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
331 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
332 depth_stencil
= NULL
;
336 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
338 if (!memcmp(entry
->render_targets
,
339 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
340 && entry
->depth_stencil
== depth_stencil
&& entry
->location
== location
)
342 list_remove(&entry
->entry
);
343 list_add_head(&context
->fbo_list
, &entry
->entry
);
348 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
350 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, location
);
351 list_add_head(&context
->fbo_list
, &entry
->entry
);
352 ++context
->fbo_entry_count
;
356 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
357 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
, entry
);
358 list_remove(&entry
->entry
);
359 list_add_head(&context
->fbo_list
, &entry
->entry
);
365 /* GL locking is done by the caller */
366 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
368 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
371 context_bind_fbo(context
, target
, &entry
->id
);
373 if (entry
->attached
) return;
375 /* Apply render targets */
376 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
378 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->location
);
381 /* Apply depth targets */
382 if (entry
->depth_stencil
)
383 surface_set_compatible_renderbuffer(entry
->depth_stencil
, entry
->render_targets
[0]);
384 context_attach_depth_stencil_fbo(context
, target
, entry
->depth_stencil
, TRUE
);
386 entry
->attached
= TRUE
;
389 /* GL locking is done by the caller */
390 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
391 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
393 struct fbo_entry
*entry
, *entry2
;
395 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
397 context_destroy_fbo_entry(context
, entry
);
400 if (context
->rebind_fbo
)
402 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
403 context
->rebind_fbo
= FALSE
;
408 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
);
409 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
413 context
->current_fbo
= NULL
;
414 context_bind_fbo(context
, target
, NULL
);
418 /* GL locking is done by the caller */
419 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
420 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
422 if (location
!= SFLAG_INDRAWABLE
|| surface_is_offscreen(render_target
))
424 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
425 context
->blit_targets
[0] = render_target
;
426 if (clear_size
) memset(&context
->blit_targets
[1], 0, clear_size
);
427 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
);
431 context_apply_fbo_state(context
, target
, NULL
, NULL
, location
);
435 /* Context activation is done by the caller. */
436 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
438 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
440 if (context
->free_occlusion_query_count
)
442 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
446 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
449 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
450 checkGLcall("glGenQueriesARB");
453 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
457 WARN("Occlusion queries not supported, not allocating query id.\n");
462 query
->context
= context
;
463 list_add_head(&context
->occlusion_queries
, &query
->entry
);
466 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
468 struct wined3d_context
*context
= query
->context
;
470 list_remove(&query
->entry
);
471 query
->context
= NULL
;
473 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
475 UINT new_size
= context
->free_occlusion_query_size
<< 1;
476 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
477 new_size
* sizeof(*context
->free_occlusion_queries
));
481 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
485 context
->free_occlusion_query_size
= new_size
;
486 context
->free_occlusion_queries
= new_data
;
489 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
492 /* Context activation is done by the caller. */
493 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
495 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
497 if (context
->free_event_query_count
)
499 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
503 if (gl_info
->supported
[ARB_SYNC
])
505 /* Using ARB_sync, not much to do here. */
506 query
->object
.sync
= NULL
;
507 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
509 else if (gl_info
->supported
[APPLE_FENCE
])
512 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
513 checkGLcall("glGenFencesAPPLE");
516 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
518 else if(gl_info
->supported
[NV_FENCE
])
521 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
522 checkGLcall("glGenFencesNV");
525 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
529 WARN("Event queries not supported, not allocating query id.\n");
530 query
->object
.id
= 0;
534 query
->context
= context
;
535 list_add_head(&context
->event_queries
, &query
->entry
);
538 void context_free_event_query(struct wined3d_event_query
*query
)
540 struct wined3d_context
*context
= query
->context
;
542 list_remove(&query
->entry
);
543 query
->context
= NULL
;
545 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
547 UINT new_size
= context
->free_event_query_size
<< 1;
548 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
549 new_size
* sizeof(*context
->free_event_queries
));
553 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
557 context
->free_event_query_size
= new_size
;
558 context
->free_event_queries
= new_data
;
561 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
564 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
566 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
567 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
571 for (i
= 0; i
< device
->context_count
; ++i
)
573 struct wined3d_context
*context
= device
->contexts
[i
];
574 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
575 struct fbo_entry
*entry
, *entry2
;
577 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
579 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
583 if (entry
->depth_stencil
== surface
)
585 callback(context
, entry
);
589 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
591 if (entry
->render_targets
[j
] == surface
)
593 callback(context
, entry
);
601 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
603 list_remove(&entry
->entry
);
604 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
607 void context_resource_released(const struct wined3d_device
*device
,
608 struct wined3d_resource
*resource
, WINED3DRESOURCETYPE type
)
610 if (!device
->d3d_initialized
) return;
614 case WINED3DRTYPE_SURFACE
:
615 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
616 context_queue_fbo_entry_destruction
);
624 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
626 entry
->attached
= FALSE
;
629 void context_resource_unloaded(const struct wined3d_device
*device
,
630 struct wined3d_resource
*resource
, WINED3DRESOURCETYPE type
)
634 case WINED3DRTYPE_SURFACE
:
635 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
636 context_detach_fbo_entry
);
644 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
646 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
647 struct fbo_entry
*entry
= context
->current_fbo
;
650 if (!entry
|| context
->rebind_fbo
) return;
652 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
654 if (surface
== entry
->render_targets
[i
])
656 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
657 context
->rebind_fbo
= TRUE
;
662 if (surface
== entry
->depth_stencil
)
664 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
665 context
->rebind_fbo
= TRUE
;
669 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
671 int current
= GetPixelFormat(dc
);
673 if (current
== format
) return TRUE
;
677 if (!SetPixelFormat(dc
, format
, NULL
))
679 ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
680 format
, dc
, GetLastError());
686 /* By default WGL doesn't allow pixel format adjustments but we need it
687 * here. For this reason there's a Wine specific wglSetPixelFormat()
688 * which allows us to set the pixel format multiple times. Only use it
689 * when really needed. */
690 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
692 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
, NULL
)))
694 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
701 /* OpenGL doesn't allow pixel format adjustments. Print an error and
702 * continue using the old format. There's a big chance that the old
703 * format works although with a performance hit and perhaps rendering
705 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
706 format
, dc
, current
);
710 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
712 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
714 if (!pwglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
718 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
719 ctx
->glCtx
, ctx
->hdc
, GetLastError());
721 WARN("Trying fallback to the backup window.\n");
723 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
725 context_set_current(NULL
);
729 if (!context_set_pixel_format(ctx
->gl_info
, dc
, ctx
->pixel_format
))
731 ERR("Failed to set pixel format %d on device context %p.\n",
732 ctx
->pixel_format
, dc
);
733 context_set_current(NULL
);
737 if (!pwglMakeCurrent(dc
, ctx
->glCtx
))
739 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
741 context_set_current(NULL
);
748 static void context_restore_gl_context(HDC dc
, HGLRC gl_ctx
)
750 if (!pwglMakeCurrent(dc
, gl_ctx
))
752 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
753 gl_ctx
, dc
, GetLastError());
754 context_set_current(NULL
);
758 static void context_update_window(struct wined3d_context
*context
)
760 if (context
->win_handle
== context
->swapchain
->win_handle
)
763 TRACE("Updating context %p window from %p to %p.\n",
764 context
, context
->win_handle
, context
->swapchain
->win_handle
);
768 /* You'd figure ReleaseDC() would fail if the DC doesn't match the
769 * window. However, that's not what actually happens, and there are
770 * user32 tests that confirm ReleaseDC() with the wrong window is
771 * supposed to succeed. So explicitly check that the DC belongs to
772 * the window, since we want to avoid releasing a DC that belongs to
773 * some other window if the original window was already destroyed. */
774 if (WindowFromDC(context
->hdc
) != context
->win_handle
)
776 WARN("DC %p does not belong to window %p.\n",
777 context
->hdc
, context
->win_handle
);
779 else if (!ReleaseDC(context
->win_handle
, context
->hdc
))
781 ERR("Failed to release device context %p, last error %#x.\n",
782 context
->hdc
, GetLastError());
785 else context
->valid
= 1;
787 context
->win_handle
= context
->swapchain
->win_handle
;
789 if (!(context
->hdc
= GetDC(context
->win_handle
)))
791 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
795 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
797 ERR("Failed to set pixel format %d on device context %p.\n",
798 context
->pixel_format
, context
->hdc
);
802 context_set_gl_context(context
);
810 /* Do not call while under the GL lock. */
811 static void context_destroy_gl_resources(struct wined3d_context
*context
)
813 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
814 struct wined3d_occlusion_query
*occlusion_query
;
815 struct wined3d_event_query
*event_query
;
816 struct fbo_entry
*entry
, *entry2
;
821 restore_ctx
= pwglGetCurrentContext();
822 restore_dc
= pwglGetCurrentDC();
824 context_update_window(context
);
825 if (context
->valid
&& restore_ctx
!= context
->glCtx
)
826 context_set_gl_context(context
);
827 else restore_ctx
= NULL
;
831 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
833 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
834 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
835 occlusion_query
->context
= NULL
;
838 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
842 if (gl_info
->supported
[ARB_SYNC
])
844 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
846 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
847 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
849 event_query
->context
= NULL
;
852 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
854 if (!context
->valid
) entry
->id
= 0;
855 context_destroy_fbo_entry(context
, entry
);
858 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
860 if (!context
->valid
) entry
->id
= 0;
861 context_destroy_fbo_entry(context
, entry
);
866 if (context
->dst_fbo
)
868 TRACE("Destroy dst FBO %d\n", context
->dst_fbo
);
869 context_destroy_fbo(context
, &context
->dst_fbo
);
871 if (context
->dummy_arbfp_prog
)
873 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
876 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
877 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
879 if (gl_info
->supported
[ARB_SYNC
])
881 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
883 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
886 else if (gl_info
->supported
[APPLE_FENCE
])
888 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
890 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
893 else if (gl_info
->supported
[NV_FENCE
])
895 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
897 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
901 checkGLcall("context cleanup");
906 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
907 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
911 context_restore_gl_context(restore_dc
, restore_ctx
);
913 else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL
, NULL
))
915 ERR("Failed to disable GL context.\n");
918 ReleaseDC(context
->win_handle
, context
->hdc
);
920 if (!pwglDeleteContext(context
->glCtx
))
922 DWORD err
= GetLastError();
923 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
927 DWORD
context_get_tls_idx(void)
929 return wined3d_context_tls_idx
;
932 void context_set_tls_idx(DWORD idx
)
934 wined3d_context_tls_idx
= idx
;
937 struct wined3d_context
*context_get_current(void)
939 return TlsGetValue(wined3d_context_tls_idx
);
942 /* Do not call while under the GL lock. */
943 BOOL
context_set_current(struct wined3d_context
*ctx
)
945 struct wined3d_context
*old
= context_get_current();
949 TRACE("Already using D3D context %p.\n", ctx
);
957 TRACE("Switching away from destroyed context %p.\n", old
);
958 context_destroy_gl_resources(old
);
959 HeapFree(GetProcessHeap(), 0, old
);
971 ERR("Trying to make invalid context %p current\n", ctx
);
975 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
976 if (!context_set_gl_context(ctx
))
980 else if(pwglGetCurrentContext())
982 TRACE("Clearing current D3D context.\n");
983 if (!pwglMakeCurrent(NULL
, NULL
))
985 DWORD err
= GetLastError();
986 ERR("Failed to clear current GL context, last error %#x.\n", err
);
987 TlsSetValue(wined3d_context_tls_idx
, NULL
);
992 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
995 void context_release(struct wined3d_context
*context
)
997 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1001 if (!context
->level
)
1002 WARN("Context %p is not active.\n", context
);
1003 else if (context
!= context_get_current())
1004 WARN("Context %p is not the current context.\n", context
);
1007 if (!--context
->level
&& context
->restore_ctx
)
1009 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1010 context_restore_gl_context(context
->restore_dc
, context
->restore_ctx
);
1011 context
->restore_ctx
= NULL
;
1012 context
->restore_dc
= NULL
;
1016 static void context_enter(struct wined3d_context
*context
)
1018 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1020 if (!context
->level
++)
1022 const struct wined3d_context
*current_context
= context_get_current();
1023 HGLRC current_gl
= pwglGetCurrentContext();
1025 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1027 TRACE("Another GL context (%p on device context %p) is already current.\n",
1028 current_gl
, pwglGetCurrentDC());
1029 context
->restore_ctx
= current_gl
;
1030 context
->restore_dc
= pwglGetCurrentDC();
1035 static void context_invalidate_state(struct wined3d_context
*context
,
1036 DWORD state
, const struct StateEntry
*state_table
)
1038 DWORD rep
= state_table
[state
].representative
;
1042 if (isStateDirty(context
, rep
)) return;
1044 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1045 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1046 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1047 context
->isStateDirty
[idx
] |= (1 << shift
);
1050 /* This function takes care of WineD3D pixel format selection. */
1051 static int WineD3D_ChoosePixelFormat(struct wined3d_device
*device
, HDC hdc
,
1052 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1053 BOOL auxBuffers
, int numSamples
, BOOL findCompatible
)
1056 unsigned int matchtry
;
1057 BYTE redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1058 BYTE depthBits
=0, stencilBits
=0;
1068 /* First, try without alpha match buffers. MacOS supports aux buffers only
1069 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
1070 * Then try without aux buffers - this is the most common cause for not
1071 * finding a pixel format. Also some drivers(the open source ones)
1072 * only offer 32 bit ARB pixel formats. First try without an exact alpha
1073 * match, then try without an exact alpha and color match.
1075 { TRUE
, TRUE
, TRUE
},
1076 { TRUE
, FALSE
, TRUE
},
1077 { FALSE
, TRUE
, TRUE
},
1078 { FALSE
, FALSE
, TRUE
},
1079 { TRUE
, FALSE
, FALSE
},
1080 { FALSE
, FALSE
, FALSE
},
1084 int nCfgs
= device
->adapter
->nCfgs
;
1086 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, findCompatible=%d\n",
1087 debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1088 auxBuffers
, numSamples
, findCompatible
);
1090 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1092 ERR("Unable to get color bits for format %s (%#x)!\n",
1093 debug_d3dformat(color_format
->id
), color_format
->id
);
1097 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1099 for (matchtry
= 0; matchtry
< (sizeof(matches
) / sizeof(*matches
)) && !iPixelFormat
; ++matchtry
)
1101 for (i
= 0; i
< nCfgs
; ++i
)
1103 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1104 BOOL exactDepthMatch
= TRUE
;
1106 /* For now only accept RGBA formats. Perhaps some day we will
1107 * allow floating point formats for pbuffers. */
1108 if(cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1111 /* In window mode we need a window drawable format and double buffering. */
1112 if(!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1115 /* We like to have aux buffers in backbuffer mode */
1116 if(auxBuffers
&& !cfg
->auxBuffers
&& matches
[matchtry
].require_aux
)
1119 if(matches
[matchtry
].exact_color
) {
1120 if(cfg
->redSize
!= redBits
)
1122 if(cfg
->greenSize
!= greenBits
)
1124 if(cfg
->blueSize
!= blueBits
)
1127 if(cfg
->redSize
< redBits
)
1129 if(cfg
->greenSize
< greenBits
)
1131 if(cfg
->blueSize
< blueBits
)
1134 if(matches
[matchtry
].exact_alpha
) {
1135 if(cfg
->alphaSize
!= alphaBits
)
1138 if(cfg
->alphaSize
< alphaBits
)
1142 /* We try to locate a format which matches our requirements exactly. In case of
1143 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1144 if(cfg
->depthSize
< depthBits
)
1146 else if(cfg
->depthSize
> depthBits
)
1147 exactDepthMatch
= FALSE
;
1149 /* In all cases make sure the number of stencil bits matches our requirements
1150 * even when we don't need stencil because it could affect performance EXCEPT
1151 * on cards which don't offer depth formats without stencil like the i915 drivers
1153 if (stencilBits
!= cfg
->stencilSize
1154 && !(device
->adapter
->brokenStencil
&& stencilBits
<= cfg
->stencilSize
))
1157 /* Check multisampling support */
1158 if(cfg
->numSamples
!= numSamples
)
1161 /* When we have passed all the checks then we have found a format which matches our
1162 * requirements. Note that we only check for a limit number of capabilities right now,
1163 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
1164 * can still differ in things like multisampling, stereo, SRGB and other flags.
1167 /* Exit the loop as we have found a format :) */
1168 if(exactDepthMatch
) {
1169 iPixelFormat
= cfg
->iPixelFormat
;
1171 } else if(!iPixelFormat
) {
1172 /* In the end we might end up with a format which doesn't exactly match our depth
1173 * requirements. Accept the first format we found because formats with higher iPixelFormat
1174 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
1175 iPixelFormat
= cfg
->iPixelFormat
;
1180 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1181 if(!iPixelFormat
&& !findCompatible
) {
1182 ERR("Can't find a suitable iPixelFormat\n");
1184 } else if(!iPixelFormat
) {
1185 PIXELFORMATDESCRIPTOR pfd
;
1187 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1188 /* PixelFormat selection */
1189 ZeroMemory(&pfd
, sizeof(pfd
));
1190 pfd
.nSize
= sizeof(pfd
);
1192 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1193 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1194 pfd
.cAlphaBits
= alphaBits
;
1195 pfd
.cColorBits
= colorBits
;
1196 pfd
.cDepthBits
= depthBits
;
1197 pfd
.cStencilBits
= stencilBits
;
1198 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1200 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1202 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1203 ERR("Can't find a suitable iPixelFormat\n");
1208 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1209 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1210 return iPixelFormat
;
1213 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
1215 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
1216 return buffer
? (1 << 31) | buffer
: 0;
1219 static inline DWORD
context_generate_rt_mask_from_surface(const struct wined3d_surface
*target
)
1221 return (1 << 31) | surface_get_gl_buffer(target
);
1224 /* Do not call while under the GL lock. */
1225 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1226 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1228 struct wined3d_device
*device
= swapchain
->device
;
1229 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1230 const struct wined3d_format
*color_format
;
1231 struct wined3d_context
*ret
;
1232 PIXELFORMATDESCRIPTOR pfd
;
1233 BOOL auxBuffers
= FALSE
;
1242 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1244 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1247 ERR("Failed to allocate context memory.\n");
1251 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1253 WARN("Failed to retireve device context, trying swapchain backup.\n");
1255 if (!(hdc
= swapchain_get_backup_dc(swapchain
)))
1257 ERR("Failed to retrieve a device context.\n");
1262 color_format
= target
->resource
.format
;
1264 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1265 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1266 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1270 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1271 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1272 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1273 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1276 /* DirectDraw supports 8bit paletted render targets and these are used by
1277 * old games like StarCraft and C&C. Most modern hardware doesn't support
1278 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1279 * conversion (ab)uses the alpha component for storing the palette index.
1280 * For this reason we require a format with 8bit alpha, so request
1282 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1283 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1285 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
1286 if (swapchain
->presentParms
.MultiSampleType
&& (swapchain
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_DISCARD
))
1288 if (!gl_info
->supported
[ARB_MULTISAMPLE
])
1289 WARN("The application is requesting multisampling without support.\n");
1292 TRACE("Requesting multisample type %#x.\n", swapchain
->presentParms
.MultiSampleType
);
1293 numSamples
= swapchain
->presentParms
.MultiSampleType
;
1297 /* Try to find a pixel format which matches our requirements. */
1298 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format
, ds_format
,
1299 auxBuffers
, numSamples
, FALSE
/* findCompatible */);
1301 /* Try to locate a compatible format if we weren't able to find anything. */
1304 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1305 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format
, ds_format
,
1306 auxBuffers
, 0 /* numSamples */, TRUE
/* findCompatible */);
1309 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1312 ERR("Can't find a suitable pixel format.\n");
1316 DescribePixelFormat(hdc
, pixel_format
, sizeof(pfd
), &pfd
);
1317 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1319 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1323 ctx
= pwglCreateContext(hdc
);
1324 if (device
->context_count
)
1326 if (!pwglShareLists(device
->contexts
[0]->glCtx
, ctx
))
1328 DWORD err
= GetLastError();
1329 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1330 device
->contexts
[0]->glCtx
, ctx
, err
);
1335 ERR("Failed to create a WGL context\n");
1339 if (!device_context_add(device
, ret
))
1341 ERR("Failed to add the newly created context to the context list\n");
1342 if (!pwglDeleteContext(ctx
))
1344 DWORD err
= GetLastError();
1345 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, err
);
1350 ret
->gl_info
= gl_info
;
1352 /* Mark all states dirty to force a proper initialization of the states
1353 * on the first use of the context. */
1354 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1356 if (device
->StateTable
[state
].representative
)
1357 context_invalidate_state(ret
, state
, device
->StateTable
);
1360 ret
->swapchain
= swapchain
;
1361 ret
->current_rt
= target
;
1362 ret
->tid
= GetCurrentThreadId();
1364 ret
->render_offscreen
= surface_is_offscreen(target
);
1365 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1369 ret
->win_handle
= swapchain
->win_handle
;
1371 ret
->pixel_format
= pixel_format
;
1373 if (device
->shader_backend
->shader_dirtifyable_constants())
1375 /* Create the dirty constants array and initialize them to dirty */
1376 ret
->vshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1377 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1378 ret
->pshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1379 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1380 memset(ret
->vshader_const_dirty
, 1,
1381 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1382 memset(ret
->pshader_const_dirty
, 1,
1383 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1386 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1387 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1388 if (!ret
->blit_targets
) goto out
;
1390 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1391 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1392 if (!ret
->draw_buffers
) goto out
;
1394 ret
->free_occlusion_query_size
= 4;
1395 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1396 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1397 if (!ret
->free_occlusion_queries
) goto out
;
1399 list_init(&ret
->occlusion_queries
);
1401 ret
->free_event_query_size
= 4;
1402 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1403 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1404 if (!ret
->free_event_queries
) goto out
;
1406 list_init(&ret
->event_queries
);
1408 TRACE("Successfully created new context %p\n", ret
);
1410 list_init(&ret
->fbo_list
);
1411 list_init(&ret
->fbo_destroy_list
);
1415 /* Set up the context defaults */
1416 if (!context_set_current(ret
))
1418 ERR("Cannot activate context to set up defaults\n");
1419 context_release(ret
);
1423 switch (swapchain
->presentParms
.PresentationInterval
)
1425 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1428 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1429 case WINED3DPRESENT_INTERVAL_ONE
:
1432 case WINED3DPRESENT_INTERVAL_TWO
:
1435 case WINED3DPRESENT_INTERVAL_THREE
:
1438 case WINED3DPRESENT_INTERVAL_FOUR
:
1442 FIXME("Unknown presentation interval %08x\n", swapchain
->presentParms
.PresentationInterval
);
1446 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1448 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1449 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1450 swap_interval
, ret
, GetLastError());
1455 glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1457 TRACE("Setting up the screen\n");
1458 /* Clear the screen */
1459 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
1460 checkGLcall("glClearColor");
1463 glClearStencil(0xffff);
1465 checkGLcall("glClear");
1467 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1468 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1470 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1471 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1473 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1474 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1476 glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1477 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1478 glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1479 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1481 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
1483 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1484 * and textures in DIB sections(due to the memory protection).
1486 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1487 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1489 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1491 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1492 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1493 * GL_VERTEX_BLEND_ARB isn't enabled too
1495 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1496 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1498 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1500 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1501 * the previous texture where to source the offset from is always unit - 1.
1503 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1505 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1506 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1507 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1510 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1512 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1513 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1514 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1515 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1518 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1519 * program and the dummy program is destroyed when the context is destroyed.
1521 const char *dummy_program
=
1523 "MOV result.color, fragment.color.primary;\n"
1525 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1526 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1527 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1530 for (s
= 0; s
< gl_info
->limits
.point_sprite_units
; ++s
)
1532 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1533 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1534 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1537 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1539 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1541 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1543 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1545 device
->frag_pipe
->enable_extension(TRUE
);
1549 TRACE("Created context %p.\n", ret
);
1554 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1555 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1556 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1557 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1558 HeapFree(GetProcessHeap(), 0, ret
->pshader_const_dirty
);
1559 HeapFree(GetProcessHeap(), 0, ret
->vshader_const_dirty
);
1560 HeapFree(GetProcessHeap(), 0, ret
);
1564 /* Do not call while under the GL lock. */
1565 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1569 TRACE("Destroying ctx %p\n", context
);
1571 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1573 context_destroy_gl_resources(context
);
1574 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1579 context
->destroyed
= 1;
1583 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1584 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1585 HeapFree(GetProcessHeap(), 0, context
->vshader_const_dirty
);
1586 HeapFree(GetProcessHeap(), 0, context
->pshader_const_dirty
);
1587 device_context_remove(device
, context
);
1588 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1591 /* GL locking is done by the caller */
1592 static inline void set_blit_dimension(UINT width
, UINT height
) {
1593 glMatrixMode(GL_PROJECTION
);
1594 checkGLcall("glMatrixMode(GL_PROJECTION)");
1596 checkGLcall("glLoadIdentity()");
1597 glOrtho(0, width
, 0, height
, 0.0, -1.0);
1598 checkGLcall("glOrtho");
1599 glViewport(0, 0, width
, height
);
1600 checkGLcall("glViewport");
1603 /*****************************************************************************
1606 * Sets up a context for DirectDraw blitting.
1607 * All texture units are disabled, texture unit 0 is set as current unit
1608 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1609 * color writing enabled for all channels
1610 * register combiners disabled, shaders disabled
1611 * world matrix is set to identity, texture matrix 0 too
1612 * projection matrix is setup for drawing screen coordinates
1615 * This: Device to activate the context for
1616 * context: Context to setup
1618 *****************************************************************************/
1619 /* Context activation is done by the caller. */
1620 static void SetupForBlit(struct wined3d_device
*device
, struct wined3d_context
*context
)
1623 const struct StateEntry
*StateTable
= device
->StateTable
;
1624 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1625 UINT width
= context
->current_rt
->resource
.width
;
1626 UINT height
= context
->current_rt
->resource
.height
;
1629 TRACE("Setting up context %p for blitting\n", context
);
1630 if(context
->last_was_blit
) {
1631 if(context
->blit_w
!= width
|| context
->blit_h
!= height
) {
1633 set_blit_dimension(width
, height
);
1635 context
->blit_w
= width
; context
->blit_h
= height
;
1636 /* No need to dirtify here, the states are still dirtified because they weren't
1637 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1641 TRACE("Context is already set up for blitting, nothing to do\n");
1644 context
->last_was_blit
= TRUE
;
1646 /* TODO: Use a display list */
1648 /* Disable shaders */
1650 device
->shader_backend
->shader_select(context
, FALSE
, FALSE
);
1653 context_invalidate_state(context
, STATE_VSHADER
, StateTable
);
1654 context_invalidate_state(context
, STATE_PIXELSHADER
, StateTable
);
1656 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1657 * helper functions in between gl calls. This function is full of context_invalidate_state
1658 * which can safely be called from here, we only lock once instead locking/unlocking
1659 * after each GL call.
1663 /* Disable all textures. The caller can then bind a texture it wants to blit
1666 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1667 * function texture unit. No need to care for higher samplers
1669 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1671 sampler
= device
->rev_tex_unit_map
[i
];
1672 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1673 checkGLcall("glActiveTextureARB");
1675 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1677 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1678 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1680 glDisable(GL_TEXTURE_3D
);
1681 checkGLcall("glDisable GL_TEXTURE_3D");
1682 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1684 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1685 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1687 glDisable(GL_TEXTURE_2D
);
1688 checkGLcall("glDisable GL_TEXTURE_2D");
1690 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1691 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1693 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1695 if (sampler
< MAX_TEXTURES
)
1696 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1697 context_invalidate_state(context
, STATE_SAMPLER(sampler
), StateTable
);
1700 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
1701 checkGLcall("glActiveTextureARB");
1703 sampler
= device
->rev_tex_unit_map
[0];
1705 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1707 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1708 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1710 glDisable(GL_TEXTURE_3D
);
1711 checkGLcall("glDisable GL_TEXTURE_3D");
1712 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1714 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1715 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1717 glDisable(GL_TEXTURE_2D
);
1718 checkGLcall("glDisable GL_TEXTURE_2D");
1720 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1722 glMatrixMode(GL_TEXTURE
);
1723 checkGLcall("glMatrixMode(GL_TEXTURE)");
1725 checkGLcall("glLoadIdentity()");
1727 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1729 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1730 GL_TEXTURE_LOD_BIAS_EXT
,
1732 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1735 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1737 if (sampler
< MAX_TEXTURES
)
1739 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
+ sampler
), StateTable
);
1740 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1742 context_invalidate_state(context
, STATE_SAMPLER(sampler
), StateTable
);
1745 /* Other misc states */
1746 glDisable(GL_ALPHA_TEST
);
1747 checkGLcall("glDisable(GL_ALPHA_TEST)");
1748 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
), StateTable
);
1749 glDisable(GL_LIGHTING
);
1750 checkGLcall("glDisable GL_LIGHTING");
1751 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_LIGHTING
), StateTable
);
1752 glDisable(GL_DEPTH_TEST
);
1753 checkGLcall("glDisable GL_DEPTH_TEST");
1754 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ZENABLE
), StateTable
);
1755 glDisableWINE(GL_FOG
);
1756 checkGLcall("glDisable GL_FOG");
1757 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_FOGENABLE
), StateTable
);
1758 glDisable(GL_BLEND
);
1759 checkGLcall("glDisable GL_BLEND");
1760 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1761 glDisable(GL_CULL_FACE
);
1762 checkGLcall("glDisable GL_CULL_FACE");
1763 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_CULLMODE
), StateTable
);
1764 glDisable(GL_STENCIL_TEST
);
1765 checkGLcall("glDisable GL_STENCIL_TEST");
1766 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
), StateTable
);
1767 glDisable(GL_SCISSOR_TEST
);
1768 checkGLcall("glDisable GL_SCISSOR_TEST");
1769 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
1770 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1772 glDisable(GL_POINT_SPRITE_ARB
);
1773 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1774 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
), StateTable
);
1776 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1777 checkGLcall("glColorMask");
1778 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
), StateTable
);
1779 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1
), StateTable
);
1780 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2
), StateTable
);
1781 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3
), StateTable
);
1782 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1784 glDisable(GL_COLOR_SUM_EXT
);
1785 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
1786 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1789 /* Setup transforms */
1790 glMatrixMode(GL_MODELVIEW
);
1791 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1793 checkGLcall("glLoadIdentity()");
1794 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable
);
1796 context
->last_was_rhw
= TRUE
;
1797 context_invalidate_state(context
, STATE_VDECL
, StateTable
); /* because of last_was_rhw = TRUE */
1799 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1800 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1801 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1802 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1803 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1804 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1805 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
1807 set_blit_dimension(width
, height
);
1808 device
->frag_pipe
->enable_extension(FALSE
);
1812 context
->blit_w
= width
; context
->blit_h
= height
;
1813 context_invalidate_state(context
, STATE_VIEWPORT
, StateTable
);
1814 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1817 /* Do not call while under the GL lock. */
1818 static struct wined3d_context
*FindContext(const struct wined3d_device
*device
, const struct wined3d_surface
*target
)
1820 struct wined3d_context
*current_context
= context_get_current();
1821 struct wined3d_context
*context
;
1823 if (current_context
&& current_context
->destroyed
) current_context
= NULL
;
1828 && current_context
->current_rt
1829 && current_context
->swapchain
->device
== device
)
1831 target
= current_context
->current_rt
;
1835 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
1836 if (swapchain
->back_buffers
) target
= swapchain
->back_buffers
[0];
1837 else target
= swapchain
->front_buffer
;
1841 if (current_context
&& current_context
->current_rt
== target
)
1843 context_update_window(current_context
);
1844 return current_context
;
1847 if (target
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
1849 TRACE("Rendering onscreen\n");
1851 context
= swapchain_get_context(target
->container
.u
.swapchain
);
1855 TRACE("Rendering offscreen\n");
1857 /* Stay with the current context if possible. Otherwise use the
1858 * context for the primary swapchain. */
1859 if (current_context
&& current_context
->swapchain
->device
== device
)
1860 context
= current_context
;
1862 context
= swapchain_get_context(device
->swapchains
[0]);
1865 context_update_window(context
);
1870 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
1872 return rt_mask
& (1 << 31);
1875 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
1877 return rt_mask
& ~(1 << 31);
1880 /* Context activation and GL locking are done by the caller. */
1881 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
1885 glDrawBuffer(GL_NONE
);
1886 checkGLcall("glDrawBuffer()");
1888 else if (is_rt_mask_onscreen(rt_mask
))
1890 glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
1891 checkGLcall("glDrawBuffer()");
1895 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1897 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1903 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
1905 context
->draw_buffers
[i
] = GL_NONE
;
1911 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1913 GL_EXTCALL(glDrawBuffersARB(i
, context
->draw_buffers
));
1914 checkGLcall("glDrawBuffers()");
1918 glDrawBuffer(context
->draw_buffers
[0]);
1919 checkGLcall("glDrawBuffer()");
1924 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
1929 /* GL locking is done by the caller. */
1930 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
1932 glDrawBuffer(buffer
);
1933 checkGLcall("glDrawBuffer()");
1934 context
->draw_buffers_mask
= context_generate_rt_mask(buffer
);
1937 static inline void context_set_render_offscreen(struct wined3d_context
*context
, const struct StateEntry
*StateTable
,
1940 if (context
->render_offscreen
== offscreen
) return;
1942 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
, StateTable
);
1943 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1944 context_invalidate_state(context
, STATE_VIEWPORT
, StateTable
);
1945 context_invalidate_state(context
, STATE_SCISSORRECT
, StateTable
);
1946 context_invalidate_state(context
, STATE_FRONTFACE
, StateTable
);
1947 context
->render_offscreen
= offscreen
;
1950 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
1951 const struct wined3d_format
*required
)
1953 BYTE existing_depth
, existing_stencil
, required_depth
, required_stencil
;
1955 if (existing
== required
) return TRUE
;
1956 if ((existing
->flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
1958 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
1959 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
1961 if(existing_depth
< required_depth
) return FALSE
;
1962 /* If stencil bits are used the exact amount is required - otherwise wrapping
1963 * won't work correctly */
1964 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
1968 /* The caller provides a context */
1969 static void context_validate_onscreen_formats(struct wined3d_device
*device
,
1970 struct wined3d_context
*context
, const struct wined3d_surface
*depth_stencil
)
1972 /* Onscreen surfaces are always in a swapchain */
1973 struct wined3d_swapchain
*swapchain
= context
->current_rt
->container
.u
.swapchain
;
1975 if (context
->render_offscreen
|| !depth_stencil
) return;
1976 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->resource
.format
)) return;
1978 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
1979 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
1981 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
1983 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
1984 surface_load_location(context
->current_rt
, SFLAG_INTEXTURE
, NULL
);
1985 swapchain
->render_to_fbo
= TRUE
;
1986 context_set_render_offscreen(context
, device
->StateTable
, TRUE
);
1989 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
1991 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
1993 else if (rt
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
1994 return context_generate_rt_mask_from_surface(rt
);
1996 return context_generate_rt_mask(device
->offscreenBuffer
);
1999 /* Context activation is done by the caller. */
2000 void context_apply_blit_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
2004 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2006 context_validate_onscreen_formats(device
, context
, NULL
);
2008 if (context
->render_offscreen
)
2010 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2013 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, context
->current_rt
, NULL
, SFLAG_INTEXTURE
);
2015 if (context
->current_rt
&& context
->current_rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2023 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
2025 rt_mask
= context_generate_rt_mask_from_surface(context
->current_rt
);
2030 rt_mask
= context_generate_rt_mask_no_fbo(device
, context
->current_rt
);
2034 if (rt_mask
!= context
->draw_buffers_mask
)
2036 context_apply_draw_buffers(context
, rt_mask
);
2037 context
->draw_buffers_mask
= rt_mask
;
2040 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2042 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2046 SetupForBlit(device
, context
);
2047 context_invalidate_state(context
, STATE_FRAMEBUFFER
, device
->StateTable
);
2050 static BOOL
context_validate_rt_config(UINT rt_count
,
2051 struct wined3d_surface
* const *rts
, const struct wined3d_surface
*ds
)
2055 if (ds
) return TRUE
;
2057 for (i
= 0; i
< rt_count
; ++i
)
2059 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2063 WARN("Invalid render target config, need at least one attachment.\n");
2067 /* Context activation is done by the caller. */
2068 BOOL
context_apply_clear_state(struct wined3d_context
*context
, struct wined3d_device
*device
,
2069 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2071 const struct StateEntry
*state_table
= device
->StateTable
;
2074 struct wined3d_surface
**rts
= fb
->render_targets
;
2076 if (!context_validate_rt_config(rt_count
, rts
, fb
->depth_stencil
))
2080 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2082 context_validate_onscreen_formats(device
, context
, fb
->depth_stencil
);
2086 if (!rt_count
|| surface_is_offscreen(rts
[0]))
2088 for (i
= 0; i
< rt_count
; ++i
)
2090 context
->blit_targets
[i
] = rts
[i
];
2091 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2092 rt_mask
|= (1 << i
);
2094 while (i
< context
->gl_info
->limits
.buffers
)
2096 context
->blit_targets
[i
] = NULL
;
2099 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, fb
->depth_stencil
, SFLAG_INTEXTURE
);
2100 glReadBuffer(GL_NONE
);
2101 checkGLcall("glReadBuffer");
2105 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2106 rt_mask
= context_generate_rt_mask_from_surface(rts
[0]);
2111 /* TODO: This is not necessary if the framebuffer is the device's current framebuffer */
2112 context_invalidate_state(context
, STATE_FRAMEBUFFER
, device
->StateTable
);
2116 rt_mask
= context_generate_rt_mask_no_fbo(device
, rts
[0]);
2120 if (rt_mask
!= context
->draw_buffers_mask
)
2122 context_apply_draw_buffers(context
, rt_mask
);
2123 context
->draw_buffers_mask
= rt_mask
;
2126 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2128 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2131 if (context
->last_was_blit
)
2133 device
->frag_pipe
->enable_extension(TRUE
);
2134 context
->last_was_blit
= FALSE
;
2137 /* Blending and clearing should be orthogonal, but tests on the nvidia
2138 * driver show that disabling blending when clearing improves the clearing
2139 * performance incredibly. */
2140 glDisable(GL_BLEND
);
2141 glEnable(GL_SCISSOR_TEST
);
2142 checkGLcall("glEnable GL_SCISSOR_TEST");
2145 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), state_table
);
2146 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), state_table
);
2147 context_invalidate_state(context
, STATE_SCISSORRECT
, state_table
);
2152 static inline DWORD
find_draw_buffers_mask(struct wined3d_context
*context
, struct wined3d_device
*device
)
2154 struct wined3d_shader
*ps
= device
->stateBlock
->state
.pixel_shader
;
2155 struct wined3d_surface
**rts
= device
->fb
.render_targets
;
2156 DWORD rt_mask
, rt_mask_bits
;
2159 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return context_generate_rt_mask_no_fbo(device
, rts
[0]);
2160 else if (!context
->render_offscreen
) return context_generate_rt_mask_from_surface(rts
[0]);
2162 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2163 rt_mask
&= device
->valid_rt_mask
;
2164 rt_mask_bits
= rt_mask
;
2166 while (rt_mask_bits
)
2168 rt_mask_bits
&= ~(1 << i
);
2169 if (!rts
[i
] || rts
[i
]->resource
.format
->id
== WINED3DFMT_NULL
)
2170 rt_mask
&= ~(1 << i
);
2178 /* GL locking and context activation are done by the caller */
2179 void context_state_fb(DWORD state
, struct wined3d_stateblock
*stateblock
, struct wined3d_context
*context
)
2181 struct wined3d_device
*device
= stateblock
->device
;
2182 const struct wined3d_fb_state
*fb
= &device
->fb
;
2183 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2185 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2187 if (!context
->render_offscreen
)
2189 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2193 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, fb
->render_targets
, fb
->depth_stencil
, SFLAG_INTEXTURE
);
2194 glReadBuffer(GL_NONE
);
2195 checkGLcall("glReadBuffer");
2199 if (context
->draw_buffers_mask
!= rt_mask
)
2201 context_apply_draw_buffers(context
, rt_mask
);
2202 context
->draw_buffers_mask
= rt_mask
;
2206 /* GL locking and context activation are done by the caller */
2207 void context_state_drawbuf(DWORD state
, struct wined3d_stateblock
*stateblock
, struct wined3d_context
*context
)
2210 struct wined3d_device
*device
= stateblock
->device
;
2212 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2214 rt_mask
= find_draw_buffers_mask(context
, device
);
2215 if (context
->draw_buffers_mask
!= rt_mask
)
2217 context_apply_draw_buffers(context
, rt_mask
);
2218 context
->draw_buffers_mask
= rt_mask
;
2222 /* Context activation is done by the caller. */
2223 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
2225 const struct StateEntry
*state_table
= device
->StateTable
;
2226 const struct wined3d_fb_state
*fb
= &device
->fb
;
2229 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
2230 fb
->render_targets
, fb
->depth_stencil
))
2233 /* Preload resources before FBO setup. Texture preload in particular may
2234 * result in changes to the current FBO, due to using e.g. FBO blits for
2235 * updating a resource location. */
2236 device_update_tex_unit_map(device
);
2237 device_preload_textures(device
);
2238 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
))
2239 device_update_stream_info(device
, context
->gl_info
);
2242 if (context
->last_was_blit
)
2244 device
->frag_pipe
->enable_extension(TRUE
);
2247 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2249 DWORD rep
= context
->dirtyArray
[i
];
2250 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2251 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2252 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2253 state_table
[rep
].apply(rep
, device
->stateBlock
, context
);
2256 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2258 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2262 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2263 context
->last_was_blit
= FALSE
;
2268 static void context_setup_target(struct wined3d_device
*device
,
2269 struct wined3d_context
*context
, struct wined3d_surface
*target
)
2271 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
2272 const struct StateEntry
*StateTable
= device
->StateTable
;
2274 if (!target
) return;
2275 render_offscreen
= surface_is_offscreen(target
);
2276 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
2278 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2279 * the alpha blend state changes with different render target formats. */
2280 if (!context
->current_rt
)
2282 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2286 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
2287 const struct wined3d_format
*new = target
->resource
.format
;
2289 if (old
->id
!= new->id
)
2291 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2292 if ((old
->alpha_mask
&& !new->alpha_mask
) || (!old
->alpha_mask
&& new->alpha_mask
)
2293 || !(new->flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
2294 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2296 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2297 if ((old
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
2298 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SRGBWRITEENABLE
), StateTable
);
2301 /* When switching away from an offscreen render target, and we're not
2302 * using FBOs, we have to read the drawable into the texture. This is
2303 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2304 * are some things that need care though. PreLoad needs a GL context,
2305 * and FindContext is called before the context is activated. It also
2306 * has to be called with the old rendertarget active, otherwise a
2307 * wrong drawable is read. */
2308 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2309 && old_render_offscreen
&& context
->current_rt
!= target
)
2311 /* Read the back buffer of the old drawable into the destination texture. */
2312 if (context
->current_rt
->texture_name_srgb
)
2313 surface_internal_preload(context
->current_rt
, SRGB_SRGB
);
2314 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2315 surface_modify_location(context
->current_rt
, SFLAG_INDRAWABLE
, FALSE
);
2319 context
->current_rt
= target
;
2320 context_set_render_offscreen(context
, StateTable
, render_offscreen
);
2323 /* Do not call while under the GL lock. */
2324 struct wined3d_context
*context_acquire(struct wined3d_device
*device
, struct wined3d_surface
*target
)
2326 struct wined3d_context
*current_context
= context_get_current();
2327 struct wined3d_context
*context
;
2329 TRACE("device %p, target %p.\n", device
, target
);
2331 context
= FindContext(device
, target
);
2332 context_setup_target(device
, context
, target
);
2333 context_enter(context
);
2334 if (!context
->valid
) return context
;
2336 if (context
!= current_context
)
2338 if (!context_set_current(context
))
2340 ERR("Failed to activate the new context.\n");
2345 device
->frag_pipe
->enable_extension(!context
->last_was_blit
);
2349 if (context
->vshader_const_dirty
)
2351 memset(context
->vshader_const_dirty
, 1,
2352 sizeof(*context
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
2353 device
->highest_dirty_vs_const
= device
->d3d_vshader_constantF
;
2355 if (context
->pshader_const_dirty
)
2357 memset(context
->pshader_const_dirty
, 1,
2358 sizeof(*context
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
2359 device
->highest_dirty_ps_const
= device
->d3d_pshader_constantF
;
2362 else if (context
->restore_ctx
)
2364 context_set_gl_context(context
);