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 static 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 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
115 GLenum fbo_target
, DWORD format_flags
, GLuint rb
)
117 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
119 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
120 checkGLcall("glFramebufferRenderbuffer()");
123 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
125 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
126 checkGLcall("glFramebufferRenderbuffer()");
130 /* GL locking is done by the caller */
131 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
132 GLenum fbo_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
134 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
136 TRACE("Attach depth stencil %p\n", depth_stencil
);
140 DWORD format_flags
= depth_stencil
->resource
.format
->flags
;
142 if (depth_stencil
->current_renderbuffer
)
144 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
145 format_flags
, depth_stencil
->current_renderbuffer
->id
);
151 case SFLAG_INTEXTURE
:
152 case SFLAG_INSRGBTEX
:
153 surface_prepare_texture(depth_stencil
, context
, FALSE
);
155 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
157 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
158 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
159 depth_stencil
->texture_level
);
160 checkGLcall("glFramebufferTexture2D()");
163 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
165 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
166 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
167 depth_stencil
->texture_level
);
168 checkGLcall("glFramebufferTexture2D()");
172 case SFLAG_INRB_MULTISAMPLE
:
173 surface_prepare_rb(depth_stencil
, gl_info
, TRUE
);
174 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
175 format_flags
, depth_stencil
->rb_multisample
);
178 case SFLAG_INRB_RESOLVED
:
179 surface_prepare_rb(depth_stencil
, gl_info
, FALSE
);
180 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
181 format_flags
, depth_stencil
->rb_resolved
);
185 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
190 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
192 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
193 checkGLcall("glFramebufferTexture2D()");
196 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
198 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
199 checkGLcall("glFramebufferTexture2D()");
204 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
205 checkGLcall("glFramebufferTexture2D()");
207 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
208 checkGLcall("glFramebufferTexture2D()");
212 /* GL locking is done by the caller */
213 static void context_attach_surface_fbo(struct wined3d_context
*context
,
214 GLenum fbo_target
, DWORD idx
, struct wined3d_surface
*surface
, DWORD location
)
216 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
218 TRACE("Attach surface %p to %u\n", surface
, idx
);
220 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
226 case SFLAG_INTEXTURE
:
227 case SFLAG_INSRGBTEX
:
228 srgb
= location
== SFLAG_INSRGBTEX
;
229 surface_prepare_texture(surface
, context
, srgb
);
230 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
231 surface
->texture_target
, surface_get_texture_name(surface
, gl_info
, srgb
),
232 surface
->texture_level
);
233 checkGLcall("glFramebufferTexture2D()");
236 case SFLAG_INRB_MULTISAMPLE
:
237 surface_prepare_rb(surface
, gl_info
, TRUE
);
238 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
239 GL_RENDERBUFFER
, surface
->rb_multisample
);
240 checkGLcall("glFramebufferRenderbuffer()");
243 case SFLAG_INRB_RESOLVED
:
244 surface_prepare_rb(surface
, gl_info
, FALSE
);
245 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
246 GL_RENDERBUFFER
, surface
->rb_resolved
);
247 checkGLcall("glFramebufferRenderbuffer()");
251 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
257 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
258 checkGLcall("glFramebufferTexture2D()");
262 /* GL locking is done by the caller */
263 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
265 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
268 if (!FIXME_ON(d3d
)) return;
270 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
271 if (status
== GL_FRAMEBUFFER_COMPLETE
)
273 TRACE("FBO complete\n");
277 const struct wined3d_surface
*attachment
;
280 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
282 if (!context
->current_fbo
)
284 ERR("FBO 0 is incomplete, driver bug?\n");
288 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context
->current_fbo
->location
),
289 context
->current_fbo
->location
);
291 /* Dump the FBO attachments */
292 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
294 attachment
= context
->current_fbo
->render_targets
[i
];
297 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
298 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
299 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
302 attachment
= context
->current_fbo
->depth_stencil
;
305 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
306 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
307 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
312 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
314 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
315 return buffer
? (1 << 31) | buffer
: 0;
318 static inline DWORD
context_generate_rt_mask_from_surface(const struct wined3d_surface
*target
)
320 return (1 << 31) | surface_get_gl_buffer(target
);
323 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
324 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
326 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
327 struct fbo_entry
*entry
;
329 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
330 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
331 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
332 entry
->depth_stencil
= depth_stencil
;
333 entry
->location
= location
;
334 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
335 entry
->attached
= FALSE
;
341 /* GL locking is done by the caller */
342 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
343 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
344 DWORD location
, struct fbo_entry
*entry
)
346 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
348 context_bind_fbo(context
, target
, &entry
->id
);
349 context_clean_fbo_attachments(gl_info
, target
);
351 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
352 entry
->depth_stencil
= depth_stencil
;
353 entry
->location
= location
;
354 entry
->attached
= FALSE
;
357 /* GL locking is done by the caller */
358 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
362 TRACE("Destroy FBO %d\n", entry
->id
);
363 context_destroy_fbo(context
, &entry
->id
);
365 --context
->fbo_entry_count
;
366 list_remove(&entry
->entry
);
367 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
368 HeapFree(GetProcessHeap(), 0, entry
);
372 /* GL locking is done by the caller */
373 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
374 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
376 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
377 struct fbo_entry
*entry
;
379 if (depth_stencil
&& render_targets
&& render_targets
[0])
381 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
382 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
384 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
385 depth_stencil
= NULL
;
389 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
391 if (!memcmp(entry
->render_targets
,
392 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
393 && entry
->depth_stencil
== depth_stencil
&& entry
->location
== location
)
395 list_remove(&entry
->entry
);
396 list_add_head(&context
->fbo_list
, &entry
->entry
);
401 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
403 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, location
);
404 list_add_head(&context
->fbo_list
, &entry
->entry
);
405 ++context
->fbo_entry_count
;
409 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
410 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
, entry
);
411 list_remove(&entry
->entry
);
412 list_add_head(&context
->fbo_list
, &entry
->entry
);
418 /* GL locking is done by the caller */
419 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
421 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
424 context_bind_fbo(context
, target
, &entry
->id
);
426 if (entry
->attached
) return;
428 /* Apply render targets */
429 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
431 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->location
);
434 /* Apply depth targets */
435 if (entry
->depth_stencil
)
436 surface_set_compatible_renderbuffer(entry
->depth_stencil
, entry
->render_targets
[0]);
437 context_attach_depth_stencil_fbo(context
, target
, entry
->depth_stencil
, entry
->location
);
439 entry
->attached
= TRUE
;
442 /* GL locking is done by the caller */
443 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
444 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
446 struct fbo_entry
*entry
, *entry2
;
448 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
450 context_destroy_fbo_entry(context
, entry
);
453 if (context
->rebind_fbo
)
455 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
456 context
->rebind_fbo
= FALSE
;
459 if (location
== SFLAG_INDRAWABLE
)
461 context
->current_fbo
= NULL
;
462 context_bind_fbo(context
, target
, NULL
);
466 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
);
467 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
471 /* GL locking is done by the caller */
472 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
473 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
475 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
477 context
->blit_targets
[0] = render_target
;
479 memset(&context
->blit_targets
[1], 0, clear_size
);
480 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
);
483 /* Context activation is done by the caller. */
484 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
486 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
488 if (context
->free_occlusion_query_count
)
490 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
494 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
497 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
498 checkGLcall("glGenQueriesARB");
501 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
505 WARN("Occlusion queries not supported, not allocating query id.\n");
510 query
->context
= context
;
511 list_add_head(&context
->occlusion_queries
, &query
->entry
);
514 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
516 struct wined3d_context
*context
= query
->context
;
518 list_remove(&query
->entry
);
519 query
->context
= NULL
;
521 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
523 UINT new_size
= context
->free_occlusion_query_size
<< 1;
524 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
525 new_size
* sizeof(*context
->free_occlusion_queries
));
529 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
533 context
->free_occlusion_query_size
= new_size
;
534 context
->free_occlusion_queries
= new_data
;
537 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
540 /* Context activation is done by the caller. */
541 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
543 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
545 if (context
->free_event_query_count
)
547 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
551 if (gl_info
->supported
[ARB_SYNC
])
553 /* Using ARB_sync, not much to do here. */
554 query
->object
.sync
= NULL
;
555 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
557 else if (gl_info
->supported
[APPLE_FENCE
])
560 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
561 checkGLcall("glGenFencesAPPLE");
564 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
566 else if(gl_info
->supported
[NV_FENCE
])
569 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
570 checkGLcall("glGenFencesNV");
573 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
577 WARN("Event queries not supported, not allocating query id.\n");
578 query
->object
.id
= 0;
582 query
->context
= context
;
583 list_add_head(&context
->event_queries
, &query
->entry
);
586 void context_free_event_query(struct wined3d_event_query
*query
)
588 struct wined3d_context
*context
= query
->context
;
590 list_remove(&query
->entry
);
591 query
->context
= NULL
;
593 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
595 UINT new_size
= context
->free_event_query_size
<< 1;
596 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
597 new_size
* sizeof(*context
->free_event_queries
));
601 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
605 context
->free_event_query_size
= new_size
;
606 context
->free_event_queries
= new_data
;
609 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
612 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
614 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
615 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
619 for (i
= 0; i
< device
->context_count
; ++i
)
621 struct wined3d_context
*context
= device
->contexts
[i
];
622 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
623 struct fbo_entry
*entry
, *entry2
;
625 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
627 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
631 if (entry
->depth_stencil
== surface
)
633 callback(context
, entry
);
637 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
639 if (entry
->render_targets
[j
] == surface
)
641 callback(context
, entry
);
649 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
651 list_remove(&entry
->entry
);
652 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
655 void context_resource_released(const struct wined3d_device
*device
,
656 struct wined3d_resource
*resource
, WINED3DRESOURCETYPE type
)
658 if (!device
->d3d_initialized
) return;
662 case WINED3DRTYPE_SURFACE
:
663 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
664 context_queue_fbo_entry_destruction
);
672 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
674 entry
->attached
= FALSE
;
677 void context_resource_unloaded(const struct wined3d_device
*device
,
678 struct wined3d_resource
*resource
, WINED3DRESOURCETYPE type
)
682 case WINED3DRTYPE_SURFACE
:
683 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
684 context_detach_fbo_entry
);
692 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
694 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
695 struct fbo_entry
*entry
= context
->current_fbo
;
698 if (!entry
|| context
->rebind_fbo
) return;
700 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
702 if (surface
== entry
->render_targets
[i
])
704 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
705 context
->rebind_fbo
= TRUE
;
710 if (surface
== entry
->depth_stencil
)
712 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
713 context
->rebind_fbo
= TRUE
;
717 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
719 int current
= GetPixelFormat(dc
);
721 if (current
== format
) return TRUE
;
725 if (!SetPixelFormat(dc
, format
, NULL
))
727 /* This may also happen if the dc belongs to a destroyed window. */
728 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
729 format
, dc
, GetLastError());
735 /* By default WGL doesn't allow pixel format adjustments but we need it
736 * here. For this reason there's a Wine specific wglSetPixelFormat()
737 * which allows us to set the pixel format multiple times. Only use it
738 * when really needed. */
739 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
741 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
, NULL
)))
743 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
750 /* OpenGL doesn't allow pixel format adjustments. Print an error and
751 * continue using the old format. There's a big chance that the old
752 * format works although with a performance hit and perhaps rendering
754 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
755 format
, dc
, current
);
759 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
761 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
764 if (!context_set_pixel_format(ctx
->gl_info
, ctx
->hdc
, ctx
->pixel_format
))
766 WARN("Failed to set pixel format %d on device context %p.\n",
767 ctx
->pixel_format
, ctx
->hdc
);
771 if (backup
|| !pwglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
775 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
776 ctx
->glCtx
, ctx
->hdc
, GetLastError());
778 WARN("Trying fallback to the backup window.\n");
780 /* FIXME: If the context is destroyed it's no longer associated with
781 * a swapchain, so we can't use the swapchain to get a backup dc. To
782 * make this work windowless contexts would need to be handled by the
786 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
787 context_set_current(NULL
);
791 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
793 context_set_current(NULL
);
797 if (!context_set_pixel_format(ctx
->gl_info
, dc
, ctx
->pixel_format
))
799 ERR("Failed to set pixel format %d on device context %p.\n",
800 ctx
->pixel_format
, dc
);
801 context_set_current(NULL
);
805 if (!pwglMakeCurrent(dc
, ctx
->glCtx
))
807 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
809 context_set_current(NULL
);
816 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
, int pf
)
818 if (!context_set_pixel_format(gl_info
, dc
, pf
))
820 ERR("Failed to restore pixel format %d on device context %p.\n", pf
, dc
);
821 context_set_current(NULL
);
825 if (!pwglMakeCurrent(dc
, gl_ctx
))
827 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
828 gl_ctx
, dc
, GetLastError());
829 context_set_current(NULL
);
833 static void context_update_window(struct wined3d_context
*context
)
835 if (context
->win_handle
== context
->swapchain
->win_handle
)
838 TRACE("Updating context %p window from %p to %p.\n",
839 context
, context
->win_handle
, context
->swapchain
->win_handle
);
843 /* You'd figure ReleaseDC() would fail if the DC doesn't match the
844 * window. However, that's not what actually happens, and there are
845 * user32 tests that confirm ReleaseDC() with the wrong window is
846 * supposed to succeed. So explicitly check that the DC belongs to
847 * the window, since we want to avoid releasing a DC that belongs to
848 * some other window if the original window was already destroyed. */
849 if (WindowFromDC(context
->hdc
) != context
->win_handle
)
851 WARN("DC %p does not belong to window %p.\n",
852 context
->hdc
, context
->win_handle
);
854 else if (!ReleaseDC(context
->win_handle
, context
->hdc
))
856 ERR("Failed to release device context %p, last error %#x.\n",
857 context
->hdc
, GetLastError());
860 else context
->valid
= 1;
862 context
->win_handle
= context
->swapchain
->win_handle
;
864 if (!(context
->hdc
= GetDC(context
->win_handle
)))
866 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
870 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
872 ERR("Failed to set pixel format %d on device context %p.\n",
873 context
->pixel_format
, context
->hdc
);
877 context_set_gl_context(context
);
885 /* Do not call while under the GL lock. */
886 static void context_destroy_gl_resources(struct wined3d_context
*context
)
888 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
889 struct wined3d_occlusion_query
*occlusion_query
;
890 struct wined3d_event_query
*event_query
;
891 struct fbo_entry
*entry
, *entry2
;
897 restore_ctx
= pwglGetCurrentContext();
898 restore_dc
= pwglGetCurrentDC();
899 restore_pf
= GetPixelFormat(restore_dc
);
901 if (context
->valid
&& restore_ctx
!= context
->glCtx
)
902 context_set_gl_context(context
);
903 else restore_ctx
= NULL
;
907 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
909 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
910 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
911 occlusion_query
->context
= NULL
;
914 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
918 if (gl_info
->supported
[ARB_SYNC
])
920 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
922 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
923 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
925 event_query
->context
= NULL
;
928 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
930 if (!context
->valid
) entry
->id
= 0;
931 context_destroy_fbo_entry(context
, entry
);
934 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
936 if (!context
->valid
) entry
->id
= 0;
937 context_destroy_fbo_entry(context
, entry
);
942 if (context
->dummy_arbfp_prog
)
944 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
947 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
948 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
950 if (gl_info
->supported
[ARB_SYNC
])
952 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
954 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
957 else if (gl_info
->supported
[APPLE_FENCE
])
959 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
961 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
964 else if (gl_info
->supported
[NV_FENCE
])
966 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
968 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
972 checkGLcall("context cleanup");
977 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
978 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
982 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
, restore_pf
);
984 else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL
, NULL
))
986 ERR("Failed to disable GL context.\n");
989 ReleaseDC(context
->win_handle
, context
->hdc
);
991 if (!pwglDeleteContext(context
->glCtx
))
993 DWORD err
= GetLastError();
994 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
998 DWORD
context_get_tls_idx(void)
1000 return wined3d_context_tls_idx
;
1003 void context_set_tls_idx(DWORD idx
)
1005 wined3d_context_tls_idx
= idx
;
1008 struct wined3d_context
*context_get_current(void)
1010 return TlsGetValue(wined3d_context_tls_idx
);
1013 /* Do not call while under the GL lock. */
1014 BOOL
context_set_current(struct wined3d_context
*ctx
)
1016 struct wined3d_context
*old
= context_get_current();
1020 TRACE("Already using D3D context %p.\n", ctx
);
1028 TRACE("Switching away from destroyed context %p.\n", old
);
1029 context_destroy_gl_resources(old
);
1030 HeapFree(GetProcessHeap(), 0, old
);
1042 ERR("Trying to make invalid context %p current\n", ctx
);
1046 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1047 if (!context_set_gl_context(ctx
))
1051 else if(pwglGetCurrentContext())
1053 TRACE("Clearing current D3D context.\n");
1054 if (!pwglMakeCurrent(NULL
, NULL
))
1056 DWORD err
= GetLastError();
1057 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1058 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1063 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1066 void context_release(struct wined3d_context
*context
)
1068 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1072 if (!context
->level
)
1073 WARN("Context %p is not active.\n", context
);
1074 else if (context
!= context_get_current())
1075 WARN("Context %p is not the current context.\n", context
);
1078 if (!--context
->level
&& context
->restore_ctx
)
1080 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1081 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
, context
->restore_pf
);
1082 context
->restore_ctx
= NULL
;
1083 context
->restore_dc
= NULL
;
1087 static void context_enter(struct wined3d_context
*context
)
1089 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1091 if (!context
->level
++)
1093 const struct wined3d_context
*current_context
= context_get_current();
1094 HGLRC current_gl
= pwglGetCurrentContext();
1096 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1098 TRACE("Another GL context (%p on device context %p) is already current.\n",
1099 current_gl
, pwglGetCurrentDC());
1100 context
->restore_ctx
= current_gl
;
1101 context
->restore_dc
= pwglGetCurrentDC();
1102 context
->restore_pf
= GetPixelFormat(context
->restore_dc
);
1107 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1109 DWORD rep
= context
->state_table
[state
].representative
;
1113 if (isStateDirty(context
, rep
)) return;
1115 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1116 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1117 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1118 context
->isStateDirty
[idx
] |= (1 << shift
);
1121 /* This function takes care of wined3d pixel format selection. */
1122 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1123 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1124 BOOL auxBuffers
, BOOL findCompatible
)
1127 unsigned int matchtry
;
1128 BYTE redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1129 BYTE depthBits
=0, stencilBits
=0;
1139 /* First, try without alpha match buffers. MacOS supports aux buffers only
1140 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
1141 * Then try without aux buffers - this is the most common cause for not
1142 * finding a pixel format. Also some drivers(the open source ones)
1143 * only offer 32 bit ARB pixel formats. First try without an exact alpha
1144 * match, then try without an exact alpha and color match.
1146 { TRUE
, TRUE
, TRUE
},
1147 { TRUE
, FALSE
, TRUE
},
1148 { FALSE
, TRUE
, TRUE
},
1149 { FALSE
, FALSE
, TRUE
},
1150 { TRUE
, FALSE
, FALSE
},
1151 { FALSE
, FALSE
, FALSE
},
1154 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1157 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1158 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1159 auxBuffers
, findCompatible
);
1161 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1163 ERR("Unable to get color bits for format %s (%#x)!\n",
1164 debug_d3dformat(color_format
->id
), color_format
->id
);
1168 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1170 for (matchtry
= 0; matchtry
< (sizeof(matches
) / sizeof(*matches
)) && !iPixelFormat
; ++matchtry
)
1172 for (i
= 0; i
< cfg_count
; ++i
)
1174 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1175 BOOL exactDepthMatch
= TRUE
;
1177 /* For now only accept RGBA formats. Perhaps some day we will
1178 * allow floating point formats for pbuffers. */
1179 if(cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1182 /* In window mode we need a window drawable format and double buffering. */
1183 if(!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1186 /* We like to have aux buffers in backbuffer mode */
1187 if(auxBuffers
&& !cfg
->auxBuffers
&& matches
[matchtry
].require_aux
)
1190 if(matches
[matchtry
].exact_color
) {
1191 if(cfg
->redSize
!= redBits
)
1193 if(cfg
->greenSize
!= greenBits
)
1195 if(cfg
->blueSize
!= blueBits
)
1198 if(cfg
->redSize
< redBits
)
1200 if(cfg
->greenSize
< greenBits
)
1202 if(cfg
->blueSize
< blueBits
)
1205 if(matches
[matchtry
].exact_alpha
) {
1206 if(cfg
->alphaSize
!= alphaBits
)
1209 if(cfg
->alphaSize
< alphaBits
)
1213 /* We try to locate a format which matches our requirements exactly. In case of
1214 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1215 if(cfg
->depthSize
< depthBits
)
1217 else if(cfg
->depthSize
> depthBits
)
1218 exactDepthMatch
= FALSE
;
1220 /* In all cases make sure the number of stencil bits matches our requirements
1221 * even when we don't need stencil because it could affect performance EXCEPT
1222 * on cards which don't offer depth formats without stencil like the i915 drivers
1224 if (stencilBits
!= cfg
->stencilSize
1225 && !(device
->adapter
->brokenStencil
&& stencilBits
<= cfg
->stencilSize
))
1228 /* Check multisampling support */
1229 if (cfg
->numSamples
)
1232 /* When we have passed all the checks then we have found a format which matches our
1233 * requirements. Note that we only check for a limit number of capabilities right now,
1234 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
1235 * can still differ in things like multisampling, stereo, SRGB and other flags.
1238 /* Exit the loop as we have found a format :) */
1239 if(exactDepthMatch
) {
1240 iPixelFormat
= cfg
->iPixelFormat
;
1242 } else if(!iPixelFormat
) {
1243 /* In the end we might end up with a format which doesn't exactly match our depth
1244 * requirements. Accept the first format we found because formats with higher iPixelFormat
1245 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
1246 iPixelFormat
= cfg
->iPixelFormat
;
1251 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1252 if(!iPixelFormat
&& !findCompatible
) {
1253 ERR("Can't find a suitable iPixelFormat\n");
1255 } else if(!iPixelFormat
) {
1256 PIXELFORMATDESCRIPTOR pfd
;
1258 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1259 /* PixelFormat selection */
1260 ZeroMemory(&pfd
, sizeof(pfd
));
1261 pfd
.nSize
= sizeof(pfd
);
1263 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1264 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1265 pfd
.cAlphaBits
= alphaBits
;
1266 pfd
.cColorBits
= colorBits
;
1267 pfd
.cDepthBits
= depthBits
;
1268 pfd
.cStencilBits
= stencilBits
;
1269 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1271 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1273 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1274 ERR("Can't find a suitable iPixelFormat\n");
1279 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1280 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1281 return iPixelFormat
;
1284 /* GL locking is done by the caller */
1285 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1287 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1288 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1290 for (i
= 0; i
< count
; ++i
)
1292 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1293 checkGLcall("glActiveTextureARB");
1295 glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1296 checkGLcall("glBindTexture");
1298 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1300 glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1301 checkGLcall("glBindTexture");
1304 if (gl_info
->supported
[EXT_TEXTURE3D
])
1306 glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1307 checkGLcall("glBindTexture");
1310 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1312 glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1313 checkGLcall("glBindTexture");
1318 /* Do not call while under the GL lock. */
1319 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1320 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1322 struct wined3d_device
*device
= swapchain
->device
;
1323 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1324 const struct wined3d_format
*color_format
;
1325 struct wined3d_context
*ret
;
1326 BOOL auxBuffers
= FALSE
;
1334 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1336 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1339 ERR("Failed to allocate context memory.\n");
1343 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1344 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1345 if (!ret
->blit_targets
)
1348 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1349 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1350 if (!ret
->draw_buffers
)
1353 ret
->free_occlusion_query_size
= 4;
1354 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1355 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1356 if (!ret
->free_occlusion_queries
)
1359 list_init(&ret
->occlusion_queries
);
1361 ret
->free_event_query_size
= 4;
1362 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1363 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1364 if (!ret
->free_event_queries
)
1367 list_init(&ret
->event_queries
);
1368 list_init(&ret
->fbo_list
);
1369 list_init(&ret
->fbo_destroy_list
);
1371 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1373 WARN("Failed to retireve device context, trying swapchain backup.\n");
1375 if (!(hdc
= swapchain_get_backup_dc(swapchain
)))
1377 ERR("Failed to retrieve a device context.\n");
1382 color_format
= target
->resource
.format
;
1384 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1385 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1386 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1390 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1391 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1392 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1393 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1396 /* DirectDraw supports 8bit paletted render targets and these are used by
1397 * old games like StarCraft and C&C. Most modern hardware doesn't support
1398 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1399 * conversion (ab)uses the alpha component for storing the palette index.
1400 * For this reason we require a format with 8bit alpha, so request
1402 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1403 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1405 /* Try to find a pixel format which matches our requirements. */
1406 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1408 /* Try to locate a compatible format if we weren't able to find anything. */
1411 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1412 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1415 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1418 ERR("Can't find a suitable pixel format.\n");
1424 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1426 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1427 context_release(ret
);
1431 if (!(ctx
= pwglCreateContext(hdc
)))
1433 ERR("Failed to create a WGL context.\n");
1434 context_release(ret
);
1438 if (device
->context_count
)
1440 if (!pwglShareLists(device
->contexts
[0]->glCtx
, ctx
))
1442 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1443 device
->contexts
[0]->glCtx
, ctx
, GetLastError());
1444 context_release(ret
);
1445 if (!pwglDeleteContext(ctx
))
1446 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1451 if (!device_context_add(device
, ret
))
1453 ERR("Failed to add the newly created context to the context list\n");
1454 context_release(ret
);
1455 if (!pwglDeleteContext(ctx
))
1456 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1460 ret
->gl_info
= gl_info
;
1461 ret
->state_table
= device
->StateTable
;
1463 /* Mark all states dirty to force a proper initialization of the states
1464 * on the first use of the context. */
1465 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1467 if (ret
->state_table
[state
].representative
)
1468 context_invalidate_state(ret
, state
);
1471 ret
->swapchain
= swapchain
;
1472 ret
->current_rt
= target
;
1473 ret
->tid
= GetCurrentThreadId();
1475 ret
->render_offscreen
= surface_is_offscreen(target
);
1476 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1480 ret
->win_handle
= swapchain
->win_handle
;
1482 ret
->pixel_format
= pixel_format
;
1484 /* Set up the context defaults */
1485 if (!context_set_current(ret
))
1487 ERR("Cannot activate context to set up defaults.\n");
1488 device_context_remove(device
, ret
);
1489 context_release(ret
);
1490 if (!pwglDeleteContext(ctx
))
1491 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1495 switch (swapchain
->presentParms
.PresentationInterval
)
1497 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1500 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1501 case WINED3DPRESENT_INTERVAL_ONE
:
1504 case WINED3DPRESENT_INTERVAL_TWO
:
1507 case WINED3DPRESENT_INTERVAL_THREE
:
1510 case WINED3DPRESENT_INTERVAL_FOUR
:
1514 FIXME("Unknown presentation interval %08x\n", swapchain
->presentParms
.PresentationInterval
);
1518 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1520 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1521 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1522 swap_interval
, ret
, GetLastError());
1527 glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1529 TRACE("Setting up the screen\n");
1530 /* Clear the screen */
1531 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
1532 checkGLcall("glClearColor");
1535 glClearStencil(0xffff);
1537 checkGLcall("glClear");
1539 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1540 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1542 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1543 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1545 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1546 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1548 glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1549 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1550 glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1551 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1553 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
1555 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1556 * and textures in DIB sections(due to the memory protection).
1558 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1559 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1561 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1563 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1564 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1565 * GL_VERTEX_BLEND_ARB isn't enabled too
1567 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1568 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1570 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1572 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1573 * the previous texture where to source the offset from is always unit - 1.
1575 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1577 context_active_texture(ret
, gl_info
, s
);
1578 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1579 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1582 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1584 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1585 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1586 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1587 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1590 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1591 * program and the dummy program is destroyed when the context is destroyed.
1593 const char *dummy_program
=
1595 "MOV result.color, fragment.color.primary;\n"
1597 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1598 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1599 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1602 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1604 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1606 context_active_texture(ret
, gl_info
, s
);
1607 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1608 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1612 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1614 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1616 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1618 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1620 device
->frag_pipe
->enable_extension(TRUE
);
1622 /* If this happens to be the first context for the device, dummy textures
1623 * are not created yet. In that case, they will be created (and bound) by
1624 * create_dummy_textures right after this context is initialized. */
1625 if (device
->dummy_texture_2d
[0])
1626 bind_dummy_textures(device
, ret
);
1630 TRACE("Created context %p.\n", ret
);
1635 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1636 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1637 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1638 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1639 HeapFree(GetProcessHeap(), 0, ret
);
1643 /* Do not call while under the GL lock. */
1644 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1648 TRACE("Destroying ctx %p\n", context
);
1650 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1652 context_destroy_gl_resources(context
);
1653 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1658 context
->destroyed
= 1;
1662 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1663 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1664 device_context_remove(device
, context
);
1665 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1668 /* GL locking is done by the caller */
1669 static void set_blit_dimension(UINT width
, UINT height
)
1671 const GLdouble projection
[] =
1673 2.0 / width
, 0.0, 0.0, 0.0,
1674 0.0, 2.0 / height
, 0.0, 0.0,
1676 -1.0, -1.0, -1.0, 1.0,
1679 glMatrixMode(GL_PROJECTION
);
1680 checkGLcall("glMatrixMode(GL_PROJECTION)");
1681 glLoadMatrixd(projection
);
1682 checkGLcall("glLoadMatrixd");
1683 glViewport(0, 0, width
, height
);
1684 checkGLcall("glViewport");
1687 /*****************************************************************************
1690 * Sets up a context for DirectDraw blitting.
1691 * All texture units are disabled, texture unit 0 is set as current unit
1692 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1693 * color writing enabled for all channels
1694 * register combiners disabled, shaders disabled
1695 * world matrix is set to identity, texture matrix 0 too
1696 * projection matrix is setup for drawing screen coordinates
1699 * This: Device to activate the context for
1700 * context: Context to setup
1702 *****************************************************************************/
1703 /* Context activation is done by the caller. */
1704 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
1707 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1708 UINT width
= context
->current_rt
->resource
.width
;
1709 UINT height
= context
->current_rt
->resource
.height
;
1712 TRACE("Setting up context %p for blitting\n", context
);
1713 if(context
->last_was_blit
) {
1714 if(context
->blit_w
!= width
|| context
->blit_h
!= height
) {
1716 set_blit_dimension(width
, height
);
1718 context
->blit_w
= width
; context
->blit_h
= height
;
1719 /* No need to dirtify here, the states are still dirtified because they weren't
1720 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1724 TRACE("Context is already set up for blitting, nothing to do\n");
1727 context
->last_was_blit
= TRUE
;
1729 /* TODO: Use a display list */
1731 /* Disable shaders */
1733 device
->shader_backend
->shader_select(context
, FALSE
, FALSE
);
1736 context_invalidate_state(context
, STATE_VSHADER
);
1737 context_invalidate_state(context
, STATE_PIXELSHADER
);
1739 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1740 * helper functions in between gl calls. This function is full of context_invalidate_state
1741 * which can safely be called from here, we only lock once instead locking/unlocking
1742 * after each GL call.
1746 /* Disable all textures. The caller can then bind a texture it wants to blit
1749 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1750 * function texture unit. No need to care for higher samplers
1752 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1754 sampler
= device
->rev_tex_unit_map
[i
];
1755 context_active_texture(context
, gl_info
, i
);
1757 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1759 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1760 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1762 glDisable(GL_TEXTURE_3D
);
1763 checkGLcall("glDisable GL_TEXTURE_3D");
1764 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1766 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1767 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1769 glDisable(GL_TEXTURE_2D
);
1770 checkGLcall("glDisable GL_TEXTURE_2D");
1772 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1773 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1775 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1777 if (sampler
< MAX_TEXTURES
)
1778 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
));
1779 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1782 context_active_texture(context
, gl_info
, 0);
1784 sampler
= device
->rev_tex_unit_map
[0];
1786 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1788 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1789 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1791 glDisable(GL_TEXTURE_3D
);
1792 checkGLcall("glDisable GL_TEXTURE_3D");
1793 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1795 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1796 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1798 glDisable(GL_TEXTURE_2D
);
1799 checkGLcall("glDisable GL_TEXTURE_2D");
1801 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1803 glMatrixMode(GL_TEXTURE
);
1804 checkGLcall("glMatrixMode(GL_TEXTURE)");
1806 checkGLcall("glLoadIdentity()");
1808 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1810 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1811 GL_TEXTURE_LOD_BIAS_EXT
,
1813 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1816 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1818 if (sampler
< MAX_TEXTURES
)
1820 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
+ sampler
));
1821 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
));
1823 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1826 /* Other misc states */
1827 glDisable(GL_ALPHA_TEST
);
1828 checkGLcall("glDisable(GL_ALPHA_TEST)");
1829 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
));
1830 glDisable(GL_LIGHTING
);
1831 checkGLcall("glDisable GL_LIGHTING");
1832 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_LIGHTING
));
1833 glDisable(GL_DEPTH_TEST
);
1834 checkGLcall("glDisable GL_DEPTH_TEST");
1835 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ZENABLE
));
1836 glDisableWINE(GL_FOG
);
1837 checkGLcall("glDisable GL_FOG");
1838 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_FOGENABLE
));
1839 glDisable(GL_BLEND
);
1840 checkGLcall("glDisable GL_BLEND");
1841 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
));
1842 glDisable(GL_CULL_FACE
);
1843 checkGLcall("glDisable GL_CULL_FACE");
1844 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_CULLMODE
));
1845 glDisable(GL_STENCIL_TEST
);
1846 checkGLcall("glDisable GL_STENCIL_TEST");
1847 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
));
1848 glDisable(GL_SCISSOR_TEST
);
1849 checkGLcall("glDisable GL_SCISSOR_TEST");
1850 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
));
1851 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1853 glDisable(GL_POINT_SPRITE_ARB
);
1854 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1855 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
));
1857 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1858 checkGLcall("glColorMask");
1859 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
));
1860 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1
));
1861 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2
));
1862 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3
));
1863 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1865 glDisable(GL_COLOR_SUM_EXT
);
1866 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
));
1867 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1870 /* Setup transforms */
1871 glMatrixMode(GL_MODELVIEW
);
1872 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1874 checkGLcall("glLoadIdentity()");
1875 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)));
1877 context
->last_was_rhw
= TRUE
;
1878 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
1880 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1881 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1882 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1883 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1884 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1885 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1886 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_CLIPPING
));
1888 set_blit_dimension(width
, height
);
1889 device
->frag_pipe
->enable_extension(FALSE
);
1893 context
->blit_w
= width
; context
->blit_h
= height
;
1894 context_invalidate_state(context
, STATE_VIEWPORT
);
1895 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
));
1898 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
1900 return rt_mask
& (1 << 31);
1903 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
1905 return rt_mask
& ~(1 << 31);
1908 /* Context activation and GL locking are done by the caller. */
1909 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
1913 glDrawBuffer(GL_NONE
);
1914 checkGLcall("glDrawBuffer()");
1916 else if (is_rt_mask_onscreen(rt_mask
))
1918 glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
1919 checkGLcall("glDrawBuffer()");
1923 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1925 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1931 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
1933 context
->draw_buffers
[i
] = GL_NONE
;
1939 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1941 GL_EXTCALL(glDrawBuffersARB(i
, context
->draw_buffers
));
1942 checkGLcall("glDrawBuffers()");
1946 glDrawBuffer(context
->draw_buffers
[0]);
1947 checkGLcall("glDrawBuffer()");
1952 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
1957 /* GL locking is done by the caller. */
1958 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
1960 glDrawBuffer(buffer
);
1961 checkGLcall("glDrawBuffer()");
1962 if (context
->current_fbo
)
1963 context
->current_fbo
->rt_mask
= context_generate_rt_mask(buffer
);
1965 context
->draw_buffers_mask
= context_generate_rt_mask(buffer
);
1968 /* GL locking is done by the caller. */
1969 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
1971 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0
+ unit
));
1972 checkGLcall("glActiveTextureARB");
1973 context
->active_texture
= unit
;
1976 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
1978 DWORD unit
= context
->active_texture
;
1979 DWORD old_texture_type
= context
->texture_type
[unit
];
1983 glBindTexture(target
, name
);
1984 checkGLcall("glBindTexture");
1991 if (old_texture_type
!= target
)
1993 const struct wined3d_device
*device
= context
->swapchain
->device
;
1995 switch (old_texture_type
)
2001 glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2002 checkGLcall("glBindTexture");
2004 case GL_TEXTURE_RECTANGLE_ARB
:
2005 glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2006 checkGLcall("glBindTexture");
2008 case GL_TEXTURE_CUBE_MAP
:
2009 glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2010 checkGLcall("glBindTexture");
2013 glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2014 checkGLcall("glBindTexture");
2017 ERR("Unexpected texture target %#x\n", old_texture_type
);
2020 context
->texture_type
[unit
] = target
;
2024 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2026 if (context
->render_offscreen
== offscreen
) return;
2028 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2029 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
));
2030 context_invalidate_state(context
, STATE_VIEWPORT
);
2031 context_invalidate_state(context
, STATE_SCISSORRECT
);
2032 context_invalidate_state(context
, STATE_FRONTFACE
);
2033 context
->render_offscreen
= offscreen
;
2036 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2037 const struct wined3d_format
*required
)
2039 BYTE existing_depth
, existing_stencil
, required_depth
, required_stencil
;
2041 if (existing
== required
) return TRUE
;
2042 if ((existing
->flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
2044 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
2045 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
2047 if(existing_depth
< required_depth
) return FALSE
;
2048 /* If stencil bits are used the exact amount is required - otherwise wrapping
2049 * won't work correctly */
2050 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
2054 /* The caller provides a context */
2055 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2056 const struct wined3d_surface
*depth_stencil
)
2058 /* Onscreen surfaces are always in a swapchain */
2059 struct wined3d_swapchain
*swapchain
= context
->current_rt
->container
.u
.swapchain
;
2061 if (context
->render_offscreen
|| !depth_stencil
) return;
2062 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->resource
.format
)) return;
2064 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2065 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2067 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2069 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2070 surface_load_location(context
->current_rt
, SFLAG_INTEXTURE
, NULL
);
2071 swapchain
->render_to_fbo
= TRUE
;
2072 swapchain_update_draw_bindings(swapchain
);
2073 context_set_render_offscreen(context
, TRUE
);
2076 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
2078 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2080 else if (rt
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
2081 return context_generate_rt_mask_from_surface(rt
);
2083 return context_generate_rt_mask(device
->offscreenBuffer
);
2086 /* Context activation is done by the caller. */
2087 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2089 struct wined3d_surface
*rt
= context
->current_rt
;
2090 DWORD rt_mask
, old_mask
;
2092 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2094 context_validate_onscreen_formats(context
, NULL
);
2096 if (context
->render_offscreen
)
2098 surface_internal_preload(rt
, SRGB_RGB
);
2101 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->draw_binding
);
2103 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2111 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
2113 rt_mask
= context_generate_rt_mask_from_surface(rt
);
2118 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt
);
2121 old_mask
= context
->current_fbo
? context
->current_fbo
->rt_mask
: context
->draw_buffers_mask
;
2124 if (rt_mask
!= old_mask
)
2126 context_apply_draw_buffers(context
, rt_mask
);
2127 context
->draw_buffers_mask
= rt_mask
;
2130 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2132 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2136 SetupForBlit(device
, context
);
2137 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2140 static BOOL
context_validate_rt_config(UINT rt_count
,
2141 struct wined3d_surface
* const *rts
, const struct wined3d_surface
*ds
)
2145 if (ds
) return TRUE
;
2147 for (i
= 0; i
< rt_count
; ++i
)
2149 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2153 WARN("Invalid render target config, need at least one attachment.\n");
2157 /* Context activation is done by the caller. */
2158 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2159 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2161 DWORD rt_mask
= 0, old_mask
;
2163 struct wined3d_surface
**rts
= fb
->render_targets
;
2165 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2166 || rt_count
!= context
->gl_info
->limits
.buffers
)
2168 if (!context_validate_rt_config(rt_count
, rts
, fb
->depth_stencil
))
2171 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2173 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2177 if (!rt_count
|| surface_is_offscreen(rts
[0]))
2179 for (i
= 0; i
< rt_count
; ++i
)
2181 context
->blit_targets
[i
] = rts
[i
];
2182 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2183 rt_mask
|= (1 << i
);
2185 while (i
< context
->gl_info
->limits
.buffers
)
2187 context
->blit_targets
[i
] = NULL
;
2190 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, fb
->depth_stencil
,
2191 rt_count
? rts
[0]->draw_binding
: SFLAG_INTEXTURE
);
2192 glReadBuffer(GL_NONE
);
2193 checkGLcall("glReadBuffer");
2197 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2198 rt_mask
= context_generate_rt_mask_from_surface(rts
[0]);
2203 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2204 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2205 * state management allows this */
2206 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2210 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt_count
? rts
[0] : NULL
);
2213 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2214 && (!rt_count
|| surface_is_offscreen(rts
[0])))
2216 for (i
= 0; i
< rt_count
; ++i
)
2218 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
) rt_mask
|= (1 << i
);
2223 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt_count
? rts
[0] : NULL
);
2226 old_mask
= context
->current_fbo
? context
->current_fbo
->rt_mask
: context
->draw_buffers_mask
;
2229 if (rt_mask
!= old_mask
)
2231 context_apply_draw_buffers(context
, rt_mask
);
2232 context
->draw_buffers_mask
= rt_mask
;
2233 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2236 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2238 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2241 if (context
->last_was_blit
)
2243 device
->frag_pipe
->enable_extension(TRUE
);
2244 context
->last_was_blit
= FALSE
;
2247 /* Blending and clearing should be orthogonal, but tests on the nvidia
2248 * driver show that disabling blending when clearing improves the clearing
2249 * performance incredibly. */
2250 glDisable(GL_BLEND
);
2251 glEnable(GL_SCISSOR_TEST
);
2252 checkGLcall("glEnable GL_SCISSOR_TEST");
2255 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
));
2256 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
));
2257 context_invalidate_state(context
, STATE_SCISSORRECT
);
2262 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_device
*device
)
2264 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
2265 struct wined3d_surface
**rts
= state
->fb
->render_targets
;
2266 struct wined3d_shader
*ps
= state
->pixel_shader
;
2267 DWORD rt_mask
, rt_mask_bits
;
2270 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return context_generate_rt_mask_no_fbo(device
, rts
[0]);
2271 else if (!context
->render_offscreen
) return context_generate_rt_mask_from_surface(rts
[0]);
2273 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2274 rt_mask
&= device
->valid_rt_mask
;
2275 rt_mask_bits
= rt_mask
;
2277 while (rt_mask_bits
)
2279 rt_mask_bits
&= ~(1 << i
);
2280 if (!rts
[i
] || rts
[i
]->resource
.format
->id
== WINED3DFMT_NULL
)
2281 rt_mask
&= ~(1 << i
);
2289 /* GL locking and context activation are done by the caller */
2290 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2292 const struct wined3d_device
*device
= context
->swapchain
->device
;
2293 const struct wined3d_fb_state
*fb
= state
->fb
;
2294 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2297 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2299 if (!context
->render_offscreen
)
2301 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2305 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, fb
->render_targets
, fb
->depth_stencil
,
2306 fb
->render_targets
[0]->draw_binding
);
2307 glReadBuffer(GL_NONE
);
2308 checkGLcall("glReadBuffer");
2312 old_mask
= context
->current_fbo
? context
->current_fbo
->rt_mask
: context
->draw_buffers_mask
;
2313 if (rt_mask
!= old_mask
)
2315 context_apply_draw_buffers(context
, rt_mask
);
2316 context
->draw_buffers_mask
= rt_mask
;
2320 /* GL locking and context activation are done by the caller */
2321 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2323 const struct wined3d_device
*device
= context
->swapchain
->device
;
2324 DWORD rt_mask
, old_mask
;
2326 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2328 old_mask
= context
->current_fbo
? context
->current_fbo
->rt_mask
: context
->draw_buffers_mask
;
2329 rt_mask
= find_draw_buffers_mask(context
, device
);
2330 if (rt_mask
!= old_mask
)
2332 context_apply_draw_buffers(context
, rt_mask
);
2333 context
->draw_buffers_mask
= rt_mask
;
2337 /* Context activation is done by the caller. */
2338 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
2340 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
2341 const struct StateEntry
*state_table
= context
->state_table
;
2342 const struct wined3d_fb_state
*fb
= state
->fb
;
2345 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
2346 fb
->render_targets
, fb
->depth_stencil
))
2349 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
2351 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2354 /* Preload resources before FBO setup. Texture preload in particular may
2355 * result in changes to the current FBO, due to using e.g. FBO blits for
2356 * updating a resource location. */
2357 device_update_tex_unit_map(device
);
2358 device_preload_textures(device
);
2359 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
))
2360 device_update_stream_info(device
, context
->gl_info
);
2363 if (context
->last_was_blit
)
2365 device
->frag_pipe
->enable_extension(TRUE
);
2368 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2370 DWORD rep
= context
->dirtyArray
[i
];
2371 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2372 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2373 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2374 state_table
[rep
].apply(context
, state
, rep
);
2377 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2379 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2383 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2384 context
->last_was_blit
= FALSE
;
2389 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
2391 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
2393 render_offscreen
= surface_is_offscreen(target
);
2394 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
2396 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2397 * the alpha blend state changes with different render target formats. */
2398 if (!context
->current_rt
)
2400 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
));
2404 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
2405 const struct wined3d_format
*new = target
->resource
.format
;
2407 if (old
->id
!= new->id
)
2409 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2410 if ((old
->alpha_mask
&& !new->alpha_mask
) || (!old
->alpha_mask
&& new->alpha_mask
)
2411 || !(new->flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
2412 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
));
2414 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2415 if ((old
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
2416 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SRGBWRITEENABLE
));
2419 /* When switching away from an offscreen render target, and we're not
2420 * using FBOs, we have to read the drawable into the texture. This is
2421 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2422 * are some things that need care though. PreLoad needs a GL context,
2423 * and FindContext is called before the context is activated. It also
2424 * has to be called with the old rendertarget active, otherwise a
2425 * wrong drawable is read. */
2426 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2427 && old_render_offscreen
&& context
->current_rt
!= target
)
2429 /* Read the back buffer of the old drawable into the destination texture. */
2430 if (context
->current_rt
->texture_name_srgb
)
2431 surface_internal_preload(context
->current_rt
, SRGB_SRGB
);
2432 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2433 surface_modify_location(context
->current_rt
, SFLAG_INDRAWABLE
, FALSE
);
2437 context
->current_rt
= target
;
2438 context_set_render_offscreen(context
, render_offscreen
);
2441 /* Do not call while under the GL lock. */
2442 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
2444 struct wined3d_context
*current_context
= context_get_current();
2445 struct wined3d_context
*context
;
2447 TRACE("device %p, target %p.\n", device
, target
);
2449 if (current_context
&& current_context
->destroyed
)
2450 current_context
= NULL
;
2455 && current_context
->current_rt
2456 && current_context
->swapchain
->device
== device
)
2458 target
= current_context
->current_rt
;
2462 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
2463 if (swapchain
->back_buffers
)
2464 target
= swapchain
->back_buffers
[0];
2466 target
= swapchain
->front_buffer
;
2470 if (current_context
&& current_context
->current_rt
== target
)
2472 context
= current_context
;
2474 else if (target
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
2476 TRACE("Rendering onscreen.\n");
2478 context
= swapchain_get_context(target
->container
.u
.swapchain
);
2482 TRACE("Rendering offscreen.\n");
2484 /* Stay with the current context if possible. Otherwise use the
2485 * context for the primary swapchain. */
2486 if (current_context
&& current_context
->swapchain
->device
== device
)
2487 context
= current_context
;
2489 context
= swapchain_get_context(device
->swapchains
[0]);
2492 context_update_window(context
);
2493 context_setup_target(context
, target
);
2494 context_enter(context
);
2495 if (!context
->valid
) return context
;
2497 if (context
!= current_context
)
2499 if (!context_set_current(context
))
2501 ERR("Failed to activate the new context.\n");
2506 device
->frag_pipe
->enable_extension(!context
->last_was_blit
);
2510 else if (context
->restore_ctx
)
2512 context_set_gl_context(context
);