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 static void context_apply_attachment_filter_states(const struct wined3d_context
*context
,
116 struct wined3d_surface
*surface
, DWORD location
)
118 /* Update base texture states array */
119 if (surface
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
121 struct wined3d_texture
*texture
= surface
->container
.u
.texture
;
122 struct wined3d_device
*device
= surface
->resource
.device
;
123 BOOL update_minfilter
= FALSE
;
124 BOOL update_magfilter
= FALSE
;
125 struct gl_texture
*gl_tex
;
129 case SFLAG_INTEXTURE
:
130 case SFLAG_INSRGBTEX
:
131 gl_tex
= wined3d_texture_get_gl_texture(texture
,
132 context
->gl_info
, location
== SFLAG_INSRGBTEX
);
136 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
140 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3DTEXF_POINT
141 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3DTEXF_NONE
)
143 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
;
144 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_NONE
;
145 update_minfilter
= TRUE
;
148 if (gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3DTEXF_POINT
)
150 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_POINT
;
151 update_magfilter
= TRUE
;
154 if (texture
->bind_count
)
156 WARN("Render targets should not be bound to a sampler\n");
157 device_invalidate_state(device
, STATE_SAMPLER(texture
->sampler
));
160 if (update_minfilter
|| update_magfilter
)
162 GLenum target
, bind_target
;
165 target
= surface
->texture_target
;
166 if (target
== GL_TEXTURE_2D
)
168 bind_target
= GL_TEXTURE_2D
;
169 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
171 else if (target
== GL_TEXTURE_RECTANGLE_ARB
)
173 bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
174 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_binding
);
178 bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
179 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARB
, &old_binding
);
182 glBindTexture(bind_target
, gl_tex
->name
);
183 if (update_minfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
184 if (update_magfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
185 glBindTexture(bind_target
, old_binding
);
188 checkGLcall("apply_attachment_filter_states()");
192 /* GL locking is done by the caller */
193 void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
194 GLenum fbo_target
, struct wined3d_surface
*depth_stencil
, BOOL use_render_buffer
)
196 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
198 TRACE("Attach depth stencil %p\n", depth_stencil
);
202 DWORD format_flags
= depth_stencil
->resource
.format
->flags
;
204 if (use_render_buffer
&& depth_stencil
->current_renderbuffer
)
206 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
208 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
,
209 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
210 checkGLcall("glFramebufferRenderbuffer()");
213 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
215 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
,
216 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
217 checkGLcall("glFramebufferRenderbuffer()");
222 surface_prepare_texture(depth_stencil
, gl_info
, FALSE
);
223 context_apply_attachment_filter_states(context
, depth_stencil
, SFLAG_INTEXTURE
);
225 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
227 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
228 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
229 depth_stencil
->texture_level
);
230 checkGLcall("glFramebufferTexture2D()");
233 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
235 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
236 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
237 depth_stencil
->texture_level
);
238 checkGLcall("glFramebufferTexture2D()");
242 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
244 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
245 checkGLcall("glFramebufferTexture2D()");
248 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
250 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
251 checkGLcall("glFramebufferTexture2D()");
256 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
257 checkGLcall("glFramebufferTexture2D()");
259 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
260 checkGLcall("glFramebufferTexture2D()");
264 /* GL locking is done by the caller */
265 static void context_attach_surface_fbo(const struct wined3d_context
*context
,
266 GLenum fbo_target
, DWORD idx
, struct wined3d_surface
*surface
, DWORD location
)
268 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
270 TRACE("Attach surface %p to %u\n", surface
, idx
);
272 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
278 case SFLAG_INTEXTURE
:
279 case SFLAG_INSRGBTEX
:
280 srgb
= location
== SFLAG_INSRGBTEX
;
281 surface_prepare_texture(surface
, gl_info
, srgb
);
282 context_apply_attachment_filter_states(context
, surface
, location
);
283 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
284 surface
->texture_target
, surface_get_texture_name(surface
, gl_info
, srgb
),
285 surface
->texture_level
);
289 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
292 checkGLcall("glFramebufferTexture2D()");
296 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
297 checkGLcall("glFramebufferTexture2D()");
301 /* GL locking is done by the caller */
302 void context_check_fbo_status(struct wined3d_context
*context
, GLenum target
)
304 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
307 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
308 if (status
== GL_FRAMEBUFFER_COMPLETE
)
310 TRACE("FBO complete\n");
314 struct wined3d_surface
*attachment
;
317 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
319 if (!context
->current_fbo
)
321 ERR("FBO 0 is incomplete, driver bug?\n");
325 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context
->current_fbo
->location
),
326 context
->current_fbo
->location
);
328 /* Dump the FBO attachments */
329 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
331 attachment
= context
->current_fbo
->render_targets
[i
];
334 FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
335 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
336 attachment
->pow2Width
, attachment
->pow2Height
);
339 attachment
= context
->current_fbo
->depth_stencil
;
342 FIXME("\tDepth attachment: (%p) %s %ux%u\n",
343 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
344 attachment
->pow2Width
, attachment
->pow2Height
);
349 static struct fbo_entry
*context_create_fbo_entry(struct wined3d_context
*context
,
350 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
352 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
353 struct fbo_entry
*entry
;
355 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
356 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
357 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
358 entry
->depth_stencil
= depth_stencil
;
359 entry
->location
= location
;
360 entry
->attached
= FALSE
;
366 /* GL locking is done by the caller */
367 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
368 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
369 DWORD location
, struct fbo_entry
*entry
)
371 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
373 context_bind_fbo(context
, target
, &entry
->id
);
374 context_clean_fbo_attachments(gl_info
, target
);
376 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
377 entry
->depth_stencil
= depth_stencil
;
378 entry
->location
= location
;
379 entry
->attached
= FALSE
;
382 /* GL locking is done by the caller */
383 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
387 TRACE("Destroy FBO %d\n", entry
->id
);
388 context_destroy_fbo(context
, &entry
->id
);
390 --context
->fbo_entry_count
;
391 list_remove(&entry
->entry
);
392 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
393 HeapFree(GetProcessHeap(), 0, entry
);
397 /* GL locking is done by the caller */
398 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
399 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
401 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
402 struct fbo_entry
*entry
;
404 if (depth_stencil
&& render_targets
&& render_targets
[0])
406 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
407 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
409 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
410 depth_stencil
= NULL
;
414 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
416 if (!memcmp(entry
->render_targets
,
417 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
418 && entry
->depth_stencil
== depth_stencil
&& entry
->location
== location
)
420 list_remove(&entry
->entry
);
421 list_add_head(&context
->fbo_list
, &entry
->entry
);
426 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
428 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, location
);
429 list_add_head(&context
->fbo_list
, &entry
->entry
);
430 ++context
->fbo_entry_count
;
434 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
435 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
, entry
);
436 list_remove(&entry
->entry
);
437 list_add_head(&context
->fbo_list
, &entry
->entry
);
443 /* GL locking is done by the caller */
444 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
446 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
449 context_bind_fbo(context
, target
, &entry
->id
);
451 if (!entry
->attached
)
453 /* Apply render targets */
454 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
456 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->location
);
459 /* Apply depth targets */
460 if (entry
->depth_stencil
)
461 surface_set_compatible_renderbuffer(entry
->depth_stencil
, entry
->render_targets
[0]);
462 context_attach_depth_stencil_fbo(context
, target
, entry
->depth_stencil
, TRUE
);
464 entry
->attached
= TRUE
;
468 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
470 if (entry
->render_targets
[i
])
471 context_apply_attachment_filter_states(context
, entry
->render_targets
[i
], entry
->location
);
473 if (entry
->depth_stencil
)
474 context_apply_attachment_filter_states(context
, entry
->depth_stencil
, SFLAG_INTEXTURE
);
478 /* GL locking is done by the caller */
479 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
480 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
482 struct fbo_entry
*entry
, *entry2
;
484 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
486 context_destroy_fbo_entry(context
, entry
);
489 if (context
->rebind_fbo
)
491 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
492 context
->rebind_fbo
= FALSE
;
497 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
);
498 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
502 context
->current_fbo
= NULL
;
503 context_bind_fbo(context
, target
, NULL
);
507 /* GL locking is done by the caller */
508 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
509 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
511 if (location
!= SFLAG_INDRAWABLE
|| surface_is_offscreen(render_target
))
513 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
514 context
->blit_targets
[0] = render_target
;
515 if (clear_size
) memset(&context
->blit_targets
[1], 0, clear_size
);
516 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
);
520 context_apply_fbo_state(context
, target
, NULL
, NULL
, location
);
524 /* Context activation is done by the caller. */
525 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
527 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
529 if (context
->free_occlusion_query_count
)
531 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
535 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
538 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
539 checkGLcall("glGenQueriesARB");
542 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
546 WARN("Occlusion queries not supported, not allocating query id.\n");
551 query
->context
= context
;
552 list_add_head(&context
->occlusion_queries
, &query
->entry
);
555 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
557 struct wined3d_context
*context
= query
->context
;
559 list_remove(&query
->entry
);
560 query
->context
= NULL
;
562 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
564 UINT new_size
= context
->free_occlusion_query_size
<< 1;
565 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
566 new_size
* sizeof(*context
->free_occlusion_queries
));
570 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
574 context
->free_occlusion_query_size
= new_size
;
575 context
->free_occlusion_queries
= new_data
;
578 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
581 /* Context activation is done by the caller. */
582 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
584 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
586 if (context
->free_event_query_count
)
588 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
592 if (gl_info
->supported
[ARB_SYNC
])
594 /* Using ARB_sync, not much to do here. */
595 query
->object
.sync
= NULL
;
596 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
598 else if (gl_info
->supported
[APPLE_FENCE
])
601 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
602 checkGLcall("glGenFencesAPPLE");
605 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
607 else if(gl_info
->supported
[NV_FENCE
])
610 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
611 checkGLcall("glGenFencesNV");
614 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
618 WARN("Event queries not supported, not allocating query id.\n");
619 query
->object
.id
= 0;
623 query
->context
= context
;
624 list_add_head(&context
->event_queries
, &query
->entry
);
627 void context_free_event_query(struct wined3d_event_query
*query
)
629 struct wined3d_context
*context
= query
->context
;
631 list_remove(&query
->entry
);
632 query
->context
= NULL
;
634 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
636 UINT new_size
= context
->free_event_query_size
<< 1;
637 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
638 new_size
* sizeof(*context
->free_event_queries
));
642 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
646 context
->free_event_query_size
= new_size
;
647 context
->free_event_queries
= new_data
;
650 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
653 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
655 static void context_enum_surface_fbo_entries(struct wined3d_device
*device
,
656 struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
660 for (i
= 0; i
< device
->context_count
; ++i
)
662 struct wined3d_context
*context
= device
->contexts
[i
];
663 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
664 struct fbo_entry
*entry
, *entry2
;
666 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
668 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
672 if (entry
->depth_stencil
== surface
)
674 callback(context
, entry
);
678 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
680 if (entry
->render_targets
[j
] == surface
)
682 callback(context
, entry
);
690 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
692 list_remove(&entry
->entry
);
693 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
696 void context_resource_released(struct wined3d_device
*device
,
697 struct wined3d_resource
*resource
, WINED3DRESOURCETYPE type
)
699 if (!device
->d3d_initialized
) return;
703 case WINED3DRTYPE_SURFACE
:
704 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
705 context_queue_fbo_entry_destruction
);
713 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
715 entry
->attached
= FALSE
;
718 void context_resource_unloaded(struct wined3d_device
*device
,
719 struct wined3d_resource
*resource
, WINED3DRESOURCETYPE type
)
723 case WINED3DRTYPE_SURFACE
:
724 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
725 context_detach_fbo_entry
);
733 void context_surface_update(struct wined3d_context
*context
, struct wined3d_surface
*surface
)
735 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
736 struct fbo_entry
*entry
= context
->current_fbo
;
739 if (!entry
|| context
->rebind_fbo
) return;
741 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
743 if (surface
== entry
->render_targets
[i
])
745 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
746 context
->rebind_fbo
= TRUE
;
751 if (surface
== entry
->depth_stencil
)
753 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
754 context
->rebind_fbo
= TRUE
;
758 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
760 int current
= GetPixelFormat(dc
);
762 if (current
== format
) return TRUE
;
766 if (!SetPixelFormat(dc
, format
, NULL
))
768 ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
769 format
, dc
, GetLastError());
775 /* By default WGL doesn't allow pixel format adjustments but we need it
776 * here. For this reason there's a Wine specific wglSetPixelFormat()
777 * which allows us to set the pixel format multiple times. Only use it
778 * when really needed. */
779 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
781 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
, NULL
)))
783 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
790 /* OpenGL doesn't allow pixel format adjustments. Print an error and
791 * continue using the old format. There's a big chance that the old
792 * format works although with a performance hit and perhaps rendering
794 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
795 format
, dc
, current
);
799 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
801 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
803 if (!pwglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
805 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
806 ctx
->glCtx
, ctx
->hdc
, GetLastError());
808 WARN("Trying fallback to the backup window.\n");
810 if (!swapchain
->backup_dc
)
812 TRACE("Creating the backup window for swapchain %p.\n", swapchain
);
813 swapchain
->backup_wnd
= CreateWindowA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, "WineD3D fake window",
814 WS_OVERLAPPEDWINDOW
, 10, 10, 10, 10, NULL
, NULL
, NULL
, NULL
);
815 if (!swapchain
->backup_wnd
)
817 ERR("Failed to create a window.\n");
820 swapchain
->backup_dc
= GetDC(swapchain
->backup_wnd
);
821 if (!swapchain
->backup_dc
)
823 ERR("Failed to get a DC.\n");
826 if (!context_set_pixel_format(ctx
->gl_info
, swapchain
->backup_dc
, ctx
->pixel_format
))
828 ERR("Failed to set pixel format %d on device context %p.\n",
829 ctx
->pixel_format
, swapchain
->backup_dc
);
834 if (!pwglMakeCurrent(swapchain
->backup_dc
, ctx
->glCtx
))
836 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
837 swapchain
->backup_dc
, GetLastError());
838 context_set_current(NULL
);
845 if (swapchain
->backup_dc
)
847 ReleaseDC(swapchain
->backup_wnd
, swapchain
->backup_dc
);
848 swapchain
->backup_dc
= NULL
;
850 if (swapchain
->backup_wnd
)
852 DestroyWindow(swapchain
->backup_wnd
);
853 swapchain
->backup_wnd
= NULL
;
855 context_set_current(NULL
);
859 static void context_restore_gl_context(HDC dc
, HGLRC gl_ctx
)
861 if (!pwglMakeCurrent(dc
, gl_ctx
))
863 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
864 gl_ctx
, dc
, GetLastError());
865 context_set_current(NULL
);
869 static void context_update_window(struct wined3d_context
*context
)
871 if (context
->win_handle
== context
->swapchain
->win_handle
)
874 TRACE("Updating context %p window from %p to %p.\n",
875 context
, context
->win_handle
, context
->swapchain
->win_handle
);
879 /* You'd figure ReleaseDC() would fail if the DC doesn't match the
880 * window. However, that's not what actually happens, and there are
881 * user32 tests that confirm ReleaseDC() with the wrong window is
882 * supposed to succeed. So explicitly check that the DC belongs to
883 * the window, since we want to avoid releasing a DC that belongs to
884 * some other window if the original window was already destroyed. */
885 if (WindowFromDC(context
->hdc
) != context
->win_handle
)
887 WARN("DC %p does not belong to window %p.\n",
888 context
->hdc
, context
->win_handle
);
890 else if (!ReleaseDC(context
->win_handle
, context
->hdc
))
892 ERR("Failed to release device context %p, last error %#x.\n",
893 context
->hdc
, GetLastError());
896 else context
->valid
= 1;
898 context
->win_handle
= context
->swapchain
->win_handle
;
900 if (!(context
->hdc
= GetDC(context
->win_handle
)))
902 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
906 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
908 ERR("Failed to set pixel format %d on device context %p.\n",
909 context
->pixel_format
, context
->hdc
);
913 context_set_gl_context(context
);
921 /* Do not call while under the GL lock. */
922 static void context_destroy_gl_resources(struct wined3d_context
*context
)
924 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
925 struct wined3d_occlusion_query
*occlusion_query
;
926 struct wined3d_event_query
*event_query
;
927 struct fbo_entry
*entry
, *entry2
;
932 restore_ctx
= pwglGetCurrentContext();
933 restore_dc
= pwglGetCurrentDC();
935 context_update_window(context
);
936 if (context
->valid
&& restore_ctx
!= context
->glCtx
)
937 context_set_gl_context(context
);
938 else restore_ctx
= NULL
;
942 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
944 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
945 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
946 occlusion_query
->context
= NULL
;
949 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
953 if (gl_info
->supported
[ARB_SYNC
])
955 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
957 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
958 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
960 event_query
->context
= NULL
;
963 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
965 if (!context
->valid
) entry
->id
= 0;
966 context_destroy_fbo_entry(context
, entry
);
969 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
971 if (!context
->valid
) entry
->id
= 0;
972 context_destroy_fbo_entry(context
, entry
);
977 if (context
->dst_fbo
)
979 TRACE("Destroy dst FBO %d\n", context
->dst_fbo
);
980 context_destroy_fbo(context
, &context
->dst_fbo
);
982 if (context
->dummy_arbfp_prog
)
984 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
987 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
988 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
990 if (gl_info
->supported
[ARB_SYNC
])
992 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
994 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
997 else if (gl_info
->supported
[APPLE_FENCE
])
999 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1001 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1004 else if (gl_info
->supported
[NV_FENCE
])
1006 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1008 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1012 checkGLcall("context cleanup");
1017 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1018 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1022 context_restore_gl_context(restore_dc
, restore_ctx
);
1024 else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL
, NULL
))
1026 ERR("Failed to disable GL context.\n");
1029 ReleaseDC(context
->win_handle
, context
->hdc
);
1031 if (!pwglDeleteContext(context
->glCtx
))
1033 DWORD err
= GetLastError();
1034 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1038 DWORD
context_get_tls_idx(void)
1040 return wined3d_context_tls_idx
;
1043 void context_set_tls_idx(DWORD idx
)
1045 wined3d_context_tls_idx
= idx
;
1048 struct wined3d_context
*context_get_current(void)
1050 return TlsGetValue(wined3d_context_tls_idx
);
1053 /* Do not call while under the GL lock. */
1054 BOOL
context_set_current(struct wined3d_context
*ctx
)
1056 struct wined3d_context
*old
= context_get_current();
1060 TRACE("Already using D3D context %p.\n", ctx
);
1068 TRACE("Switching away from destroyed context %p.\n", old
);
1069 context_destroy_gl_resources(old
);
1070 HeapFree(GetProcessHeap(), 0, old
);
1082 ERR("Trying to make invalid context %p current\n", ctx
);
1086 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1087 if (!context_set_gl_context(ctx
))
1091 else if(pwglGetCurrentContext())
1093 TRACE("Clearing current D3D context.\n");
1094 if (!pwglMakeCurrent(NULL
, NULL
))
1096 DWORD err
= GetLastError();
1097 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1098 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1103 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1106 void context_release(struct wined3d_context
*context
)
1108 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1112 if (!context
->level
)
1113 WARN("Context %p is not active.\n", context
);
1114 else if (context
!= context_get_current())
1115 WARN("Context %p is not the current context.\n", context
);
1118 if (!--context
->level
&& context
->restore_ctx
)
1120 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1121 context_restore_gl_context(context
->restore_dc
, context
->restore_ctx
);
1122 context
->restore_ctx
= NULL
;
1123 context
->restore_dc
= NULL
;
1127 static void context_enter(struct wined3d_context
*context
)
1129 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1131 if (!context
->level
++)
1133 const struct wined3d_context
*current_context
= context_get_current();
1134 HGLRC current_gl
= pwglGetCurrentContext();
1136 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1138 TRACE("Another GL context (%p on device context %p) is already current.\n",
1139 current_gl
, pwglGetCurrentDC());
1140 context
->restore_ctx
= current_gl
;
1141 context
->restore_dc
= pwglGetCurrentDC();
1146 static void context_invalidate_state(struct wined3d_context
*context
,
1147 DWORD state
, const struct StateEntry
*state_table
)
1149 DWORD rep
= state_table
[state
].representative
;
1153 if (isStateDirty(context
, rep
)) return;
1155 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1156 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1157 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1158 context
->isStateDirty
[idx
] |= (1 << shift
);
1161 /* This function takes care of WineD3D pixel format selection. */
1162 static int WineD3D_ChoosePixelFormat(struct wined3d_device
*device
, HDC hdc
,
1163 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1164 BOOL auxBuffers
, int numSamples
, BOOL findCompatible
)
1167 unsigned int matchtry
;
1168 BYTE redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1169 BYTE depthBits
=0, stencilBits
=0;
1179 /* First, try without alpha match buffers. MacOS supports aux buffers only
1180 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
1181 * Then try without aux buffers - this is the most common cause for not
1182 * finding a pixel format. Also some drivers(the open source ones)
1183 * only offer 32 bit ARB pixel formats. First try without an exact alpha
1184 * match, then try without an exact alpha and color match.
1186 { TRUE
, TRUE
, TRUE
},
1187 { TRUE
, FALSE
, TRUE
},
1188 { FALSE
, TRUE
, TRUE
},
1189 { FALSE
, FALSE
, TRUE
},
1190 { TRUE
, FALSE
, FALSE
},
1191 { FALSE
, FALSE
, FALSE
},
1195 int nCfgs
= device
->adapter
->nCfgs
;
1197 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, findCompatible=%d\n",
1198 debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1199 auxBuffers
, numSamples
, findCompatible
);
1201 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1203 ERR("Unable to get color bits for format %s (%#x)!\n",
1204 debug_d3dformat(color_format
->id
), color_format
->id
);
1208 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1210 for (matchtry
= 0; matchtry
< (sizeof(matches
) / sizeof(*matches
)) && !iPixelFormat
; ++matchtry
)
1212 for (i
= 0; i
< nCfgs
; ++i
)
1214 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1215 BOOL exactDepthMatch
= TRUE
;
1217 /* For now only accept RGBA formats. Perhaps some day we will
1218 * allow floating point formats for pbuffers. */
1219 if(cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1222 /* In window mode we need a window drawable format and double buffering. */
1223 if(!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1226 /* We like to have aux buffers in backbuffer mode */
1227 if(auxBuffers
&& !cfg
->auxBuffers
&& matches
[matchtry
].require_aux
)
1230 if(matches
[matchtry
].exact_color
) {
1231 if(cfg
->redSize
!= redBits
)
1233 if(cfg
->greenSize
!= greenBits
)
1235 if(cfg
->blueSize
!= blueBits
)
1238 if(cfg
->redSize
< redBits
)
1240 if(cfg
->greenSize
< greenBits
)
1242 if(cfg
->blueSize
< blueBits
)
1245 if(matches
[matchtry
].exact_alpha
) {
1246 if(cfg
->alphaSize
!= alphaBits
)
1249 if(cfg
->alphaSize
< alphaBits
)
1253 /* We try to locate a format which matches our requirements exactly. In case of
1254 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1255 if(cfg
->depthSize
< depthBits
)
1257 else if(cfg
->depthSize
> depthBits
)
1258 exactDepthMatch
= FALSE
;
1260 /* In all cases make sure the number of stencil bits matches our requirements
1261 * even when we don't need stencil because it could affect performance EXCEPT
1262 * on cards which don't offer depth formats without stencil like the i915 drivers
1264 if (stencilBits
!= cfg
->stencilSize
1265 && !(device
->adapter
->brokenStencil
&& stencilBits
<= cfg
->stencilSize
))
1268 /* Check multisampling support */
1269 if(cfg
->numSamples
!= numSamples
)
1272 /* When we have passed all the checks then we have found a format which matches our
1273 * requirements. Note that we only check for a limit number of capabilities right now,
1274 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
1275 * can still differ in things like multisampling, stereo, SRGB and other flags.
1278 /* Exit the loop as we have found a format :) */
1279 if(exactDepthMatch
) {
1280 iPixelFormat
= cfg
->iPixelFormat
;
1282 } else if(!iPixelFormat
) {
1283 /* In the end we might end up with a format which doesn't exactly match our depth
1284 * requirements. Accept the first format we found because formats with higher iPixelFormat
1285 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
1286 iPixelFormat
= cfg
->iPixelFormat
;
1291 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1292 if(!iPixelFormat
&& !findCompatible
) {
1293 ERR("Can't find a suitable iPixelFormat\n");
1295 } else if(!iPixelFormat
) {
1296 PIXELFORMATDESCRIPTOR pfd
;
1298 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1299 /* PixelFormat selection */
1300 ZeroMemory(&pfd
, sizeof(pfd
));
1301 pfd
.nSize
= sizeof(pfd
);
1303 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1304 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1305 pfd
.cAlphaBits
= alphaBits
;
1306 pfd
.cColorBits
= colorBits
;
1307 pfd
.cDepthBits
= depthBits
;
1308 pfd
.cStencilBits
= stencilBits
;
1309 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1311 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1313 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1314 ERR("Can't find a suitable iPixelFormat\n");
1319 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1320 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1321 return iPixelFormat
;
1324 /* Do not call while under the GL lock. */
1325 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1326 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1328 struct wined3d_device
*device
= swapchain
->device
;
1329 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1330 const struct wined3d_format
*color_format
;
1331 struct wined3d_context
*ret
;
1332 PIXELFORMATDESCRIPTOR pfd
;
1333 BOOL auxBuffers
= FALSE
;
1342 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1344 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1347 ERR("Failed to allocate context memory.\n");
1351 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1353 ERR("Failed to retrieve a device context.\n");
1357 color_format
= target
->resource
.format
;
1359 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1360 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1361 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1365 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1366 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1367 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1368 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1371 /* DirectDraw supports 8bit paletted render targets and these are used by
1372 * old games like StarCraft and C&C. Most modern hardware doesn't support
1373 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1374 * conversion (ab)uses the alpha component for storing the palette index.
1375 * For this reason we require a format with 8bit alpha, so request
1377 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1378 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1380 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
1381 if (swapchain
->presentParms
.MultiSampleType
&& (swapchain
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_DISCARD
))
1383 if (!gl_info
->supported
[ARB_MULTISAMPLE
])
1384 WARN("The application is requesting multisampling without support.\n");
1387 TRACE("Requesting multisample type %#x.\n", swapchain
->presentParms
.MultiSampleType
);
1388 numSamples
= swapchain
->presentParms
.MultiSampleType
;
1392 /* Try to find a pixel format which matches our requirements. */
1393 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format
, ds_format
,
1394 auxBuffers
, numSamples
, FALSE
/* findCompatible */);
1396 /* Try to locate a compatible format if we weren't able to find anything. */
1399 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1400 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format
, ds_format
,
1401 auxBuffers
, 0 /* numSamples */, TRUE
/* findCompatible */);
1404 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1407 ERR("Can't find a suitable pixel format.\n");
1411 DescribePixelFormat(hdc
, pixel_format
, sizeof(pfd
), &pfd
);
1412 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1414 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1418 ctx
= pwglCreateContext(hdc
);
1419 if (device
->context_count
)
1421 if (!pwglShareLists(device
->contexts
[0]->glCtx
, ctx
))
1423 DWORD err
= GetLastError();
1424 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1425 device
->contexts
[0]->glCtx
, ctx
, err
);
1430 ERR("Failed to create a WGL context\n");
1434 if (!device_context_add(device
, ret
))
1436 ERR("Failed to add the newly created context to the context list\n");
1437 if (!pwglDeleteContext(ctx
))
1439 DWORD err
= GetLastError();
1440 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, err
);
1445 ret
->gl_info
= gl_info
;
1447 /* Mark all states dirty to force a proper initialization of the states
1448 * on the first use of the context. */
1449 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1451 if (device
->StateTable
[state
].representative
)
1452 context_invalidate_state(ret
, state
, device
->StateTable
);
1455 ret
->swapchain
= swapchain
;
1456 ret
->current_rt
= target
;
1457 ret
->tid
= GetCurrentThreadId();
1459 ret
->render_offscreen
= surface_is_offscreen(target
);
1460 ret
->draw_buffer_dirty
= TRUE
;
1464 ret
->win_handle
= swapchain
->win_handle
;
1466 ret
->pixel_format
= pixel_format
;
1468 if (device
->shader_backend
->shader_dirtifyable_constants())
1470 /* Create the dirty constants array and initialize them to dirty */
1471 ret
->vshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1472 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1473 ret
->pshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1474 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1475 memset(ret
->vshader_const_dirty
, 1,
1476 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1477 memset(ret
->pshader_const_dirty
, 1,
1478 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1481 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1482 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1483 if (!ret
->blit_targets
) goto out
;
1485 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1486 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1487 if (!ret
->draw_buffers
) goto out
;
1489 ret
->free_occlusion_query_size
= 4;
1490 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1491 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1492 if (!ret
->free_occlusion_queries
) goto out
;
1494 list_init(&ret
->occlusion_queries
);
1496 ret
->free_event_query_size
= 4;
1497 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1498 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1499 if (!ret
->free_event_queries
) goto out
;
1501 list_init(&ret
->event_queries
);
1503 TRACE("Successfully created new context %p\n", ret
);
1505 list_init(&ret
->fbo_list
);
1506 list_init(&ret
->fbo_destroy_list
);
1510 /* Set up the context defaults */
1511 if (!context_set_current(ret
))
1513 ERR("Cannot activate context to set up defaults\n");
1514 context_release(ret
);
1518 switch (swapchain
->presentParms
.PresentationInterval
)
1520 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1523 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1524 case WINED3DPRESENT_INTERVAL_ONE
:
1527 case WINED3DPRESENT_INTERVAL_TWO
:
1530 case WINED3DPRESENT_INTERVAL_THREE
:
1533 case WINED3DPRESENT_INTERVAL_FOUR
:
1537 FIXME("Unknown presentation interval %08x\n", swapchain
->presentParms
.PresentationInterval
);
1541 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1543 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1544 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1545 swap_interval
, ret
, GetLastError());
1550 glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1552 TRACE("Setting up the screen\n");
1553 /* Clear the screen */
1554 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
1555 checkGLcall("glClearColor");
1558 glClearStencil(0xffff);
1560 checkGLcall("glClear");
1562 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1563 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1565 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1566 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1568 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1569 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1571 glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1572 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1573 glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1574 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1576 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
1578 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1579 * and textures in DIB sections(due to the memory protection).
1581 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1582 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1584 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1586 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1587 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1588 * GL_VERTEX_BLEND_ARB isn't enabled too
1590 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1591 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1593 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1595 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1596 * the previous texture where to source the offset from is always unit - 1.
1598 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1600 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1601 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1602 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1605 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1607 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1608 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1609 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1610 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1613 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1614 * program and the dummy program is destroyed when the context is destroyed.
1616 const char *dummy_program
=
1618 "MOV result.color, fragment.color.primary;\n"
1620 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1621 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1622 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1625 for (s
= 0; s
< gl_info
->limits
.point_sprite_units
; ++s
)
1627 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1628 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1629 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1632 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1634 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1636 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1638 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1640 device
->frag_pipe
->enable_extension(TRUE
);
1644 TRACE("Created context %p.\n", ret
);
1649 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1650 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1651 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1652 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1653 HeapFree(GetProcessHeap(), 0, ret
->pshader_const_dirty
);
1654 HeapFree(GetProcessHeap(), 0, ret
->vshader_const_dirty
);
1655 HeapFree(GetProcessHeap(), 0, ret
);
1659 /* Do not call while under the GL lock. */
1660 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1664 TRACE("Destroying ctx %p\n", context
);
1666 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1668 context_destroy_gl_resources(context
);
1669 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1674 context
->destroyed
= 1;
1678 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1679 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1680 HeapFree(GetProcessHeap(), 0, context
->vshader_const_dirty
);
1681 HeapFree(GetProcessHeap(), 0, context
->pshader_const_dirty
);
1682 device_context_remove(device
, context
);
1683 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1686 /* GL locking is done by the caller */
1687 static inline void set_blit_dimension(UINT width
, UINT height
) {
1688 glMatrixMode(GL_PROJECTION
);
1689 checkGLcall("glMatrixMode(GL_PROJECTION)");
1691 checkGLcall("glLoadIdentity()");
1692 glOrtho(0, width
, 0, height
, 0.0, -1.0);
1693 checkGLcall("glOrtho");
1694 glViewport(0, 0, width
, height
);
1695 checkGLcall("glViewport");
1698 /*****************************************************************************
1701 * Sets up a context for DirectDraw blitting.
1702 * All texture units are disabled, texture unit 0 is set as current unit
1703 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1704 * color writing enabled for all channels
1705 * register combiners disabled, shaders disabled
1706 * world matrix is set to identity, texture matrix 0 too
1707 * projection matrix is setup for drawing screen coordinates
1710 * This: Device to activate the context for
1711 * context: Context to setup
1713 *****************************************************************************/
1714 /* Context activation is done by the caller. */
1715 static void SetupForBlit(struct wined3d_device
*device
, struct wined3d_context
*context
)
1718 const struct StateEntry
*StateTable
= device
->StateTable
;
1719 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1720 UINT width
= context
->current_rt
->resource
.width
;
1721 UINT height
= context
->current_rt
->resource
.height
;
1724 TRACE("Setting up context %p for blitting\n", context
);
1725 if(context
->last_was_blit
) {
1726 if(context
->blit_w
!= width
|| context
->blit_h
!= height
) {
1728 set_blit_dimension(width
, height
);
1730 context
->blit_w
= width
; context
->blit_h
= height
;
1731 /* No need to dirtify here, the states are still dirtified because they weren't
1732 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1736 TRACE("Context is already set up for blitting, nothing to do\n");
1739 context
->last_was_blit
= TRUE
;
1741 /* TODO: Use a display list */
1743 /* Disable shaders */
1745 device
->shader_backend
->shader_select(context
, FALSE
, FALSE
);
1748 context_invalidate_state(context
, STATE_VSHADER
, StateTable
);
1749 context_invalidate_state(context
, STATE_PIXELSHADER
, StateTable
);
1751 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1752 * helper functions in between gl calls. This function is full of context_invalidate_state
1753 * which can safely be called from here, we only lock once instead locking/unlocking
1754 * after each GL call.
1758 /* Disable all textures. The caller can then bind a texture it wants to blit
1761 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1762 * function texture unit. No need to care for higher samplers
1764 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1766 sampler
= device
->rev_tex_unit_map
[i
];
1767 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1768 checkGLcall("glActiveTextureARB");
1770 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1772 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1773 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1775 glDisable(GL_TEXTURE_3D
);
1776 checkGLcall("glDisable GL_TEXTURE_3D");
1777 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1779 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1780 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1782 glDisable(GL_TEXTURE_2D
);
1783 checkGLcall("glDisable GL_TEXTURE_2D");
1785 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1786 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1788 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1790 if (sampler
< MAX_TEXTURES
)
1791 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1792 context_invalidate_state(context
, STATE_SAMPLER(sampler
), StateTable
);
1795 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
1796 checkGLcall("glActiveTextureARB");
1798 sampler
= device
->rev_tex_unit_map
[0];
1800 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1802 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1803 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1805 glDisable(GL_TEXTURE_3D
);
1806 checkGLcall("glDisable GL_TEXTURE_3D");
1807 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1809 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1810 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1812 glDisable(GL_TEXTURE_2D
);
1813 checkGLcall("glDisable GL_TEXTURE_2D");
1815 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1817 glMatrixMode(GL_TEXTURE
);
1818 checkGLcall("glMatrixMode(GL_TEXTURE)");
1820 checkGLcall("glLoadIdentity()");
1822 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1824 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1825 GL_TEXTURE_LOD_BIAS_EXT
,
1827 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1830 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1832 if (sampler
< MAX_TEXTURES
)
1834 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
+ sampler
), StateTable
);
1835 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1837 context_invalidate_state(context
, STATE_SAMPLER(sampler
), StateTable
);
1840 /* Other misc states */
1841 glDisable(GL_ALPHA_TEST
);
1842 checkGLcall("glDisable(GL_ALPHA_TEST)");
1843 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
), StateTable
);
1844 glDisable(GL_LIGHTING
);
1845 checkGLcall("glDisable GL_LIGHTING");
1846 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_LIGHTING
), StateTable
);
1847 glDisable(GL_DEPTH_TEST
);
1848 checkGLcall("glDisable GL_DEPTH_TEST");
1849 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ZENABLE
), StateTable
);
1850 glDisableWINE(GL_FOG
);
1851 checkGLcall("glDisable GL_FOG");
1852 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_FOGENABLE
), StateTable
);
1853 glDisable(GL_BLEND
);
1854 checkGLcall("glDisable GL_BLEND");
1855 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1856 glDisable(GL_CULL_FACE
);
1857 checkGLcall("glDisable GL_CULL_FACE");
1858 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_CULLMODE
), StateTable
);
1859 glDisable(GL_STENCIL_TEST
);
1860 checkGLcall("glDisable GL_STENCIL_TEST");
1861 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
), StateTable
);
1862 glDisable(GL_SCISSOR_TEST
);
1863 checkGLcall("glDisable GL_SCISSOR_TEST");
1864 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
1865 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1867 glDisable(GL_POINT_SPRITE_ARB
);
1868 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1869 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
), StateTable
);
1871 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1872 checkGLcall("glColorMask");
1873 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
), StateTable
);
1874 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1
), StateTable
);
1875 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2
), StateTable
);
1876 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3
), StateTable
);
1877 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1879 glDisable(GL_COLOR_SUM_EXT
);
1880 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
1881 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1884 /* Setup transforms */
1885 glMatrixMode(GL_MODELVIEW
);
1886 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1888 checkGLcall("glLoadIdentity()");
1889 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable
);
1891 context
->last_was_rhw
= TRUE
;
1892 context_invalidate_state(context
, STATE_VDECL
, StateTable
); /* because of last_was_rhw = TRUE */
1894 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1895 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1896 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1897 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1898 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1899 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1900 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
1902 set_blit_dimension(width
, height
);
1903 device
->frag_pipe
->enable_extension(FALSE
);
1907 context
->blit_w
= width
; context
->blit_h
= height
;
1908 context_invalidate_state(context
, STATE_VIEWPORT
, StateTable
);
1909 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1912 /* Do not call while under the GL lock. */
1913 static struct wined3d_context
*FindContext(struct wined3d_device
*device
, struct wined3d_surface
*target
)
1915 struct wined3d_context
*current_context
= context_get_current();
1916 struct wined3d_context
*context
;
1918 if (current_context
&& current_context
->destroyed
) current_context
= NULL
;
1923 && current_context
->current_rt
1924 && current_context
->swapchain
->device
== device
)
1926 target
= current_context
->current_rt
;
1930 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
1931 if (swapchain
->back_buffers
) target
= swapchain
->back_buffers
[0];
1932 else target
= swapchain
->front_buffer
;
1936 if (current_context
&& current_context
->current_rt
== target
)
1938 context_update_window(current_context
);
1939 return current_context
;
1942 if (target
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
1944 TRACE("Rendering onscreen\n");
1946 context
= swapchain_get_context(target
->container
.u
.swapchain
);
1950 TRACE("Rendering offscreen\n");
1952 /* Stay with the current context if possible. Otherwise use the
1953 * context for the primary swapchain. */
1954 if (current_context
&& current_context
->swapchain
->device
== device
)
1955 context
= current_context
;
1957 context
= swapchain_get_context(device
->swapchains
[0]);
1960 context_update_window(context
);
1965 /* Context activation and GL locking are done by the caller. */
1966 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
, struct wined3d_surface
**rts
)
1970 glDrawBuffer(GL_NONE
);
1971 checkGLcall("glDrawBuffer()");
1973 else if (!surface_is_offscreen(rts
[0]))
1975 glDrawBuffer(surface_get_gl_buffer(rts
[0]));
1976 checkGLcall("glDrawBuffer()");
1980 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1982 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1987 if ((rt_mask
& 1) && rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
1988 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
1990 context
->draw_buffers
[i
] = GL_NONE
;
1996 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1998 GL_EXTCALL(glDrawBuffersARB(i
, context
->draw_buffers
));
1999 checkGLcall("glDrawBuffers()");
2003 glDrawBuffer(context
->draw_buffers
[0]);
2004 checkGLcall("glDrawBuffer()");
2009 glDrawBuffer(rts
[0]->resource
.device
->offscreenBuffer
);
2010 checkGLcall("glDrawBuffer()");
2015 /* GL locking is done by the caller. */
2016 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2018 glDrawBuffer(buffer
);
2019 checkGLcall("glDrawBuffer()");
2020 context
->draw_buffer_dirty
= TRUE
;
2023 static inline void context_set_render_offscreen(struct wined3d_context
*context
, const struct StateEntry
*StateTable
,
2026 if (context
->render_offscreen
== offscreen
) return;
2028 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
, StateTable
);
2029 context_invalidate_state(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
2030 context_invalidate_state(context
, STATE_VIEWPORT
, StateTable
);
2031 context_invalidate_state(context
, STATE_SCISSORRECT
, StateTable
);
2032 context_invalidate_state(context
, STATE_FRONTFACE
, StateTable
);
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_device
*device
,
2056 struct wined3d_context
*context
, 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 context_set_render_offscreen(context
, device
->StateTable
, TRUE
);
2075 /* Context activation is done by the caller. */
2076 void context_apply_blit_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
2078 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2080 context_validate_onscreen_formats(device
, context
, NULL
);
2082 if (context
->render_offscreen
)
2084 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2087 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, context
->current_rt
, NULL
, SFLAG_INTEXTURE
);
2093 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
2097 context
->draw_buffer_dirty
= TRUE
;
2101 if (context
->draw_buffer_dirty
)
2103 context_apply_draw_buffers(context
, 1, &context
->current_rt
);
2104 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2105 context
->draw_buffer_dirty
= FALSE
;
2108 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2110 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2114 SetupForBlit(device
, context
);
2117 static BOOL
context_validate_rt_config(UINT rt_count
,
2118 struct wined3d_surface
**rts
, struct wined3d_surface
*ds
)
2122 if (ds
) return TRUE
;
2124 for (i
= 0; i
< rt_count
; ++i
)
2126 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2130 WARN("Invalid render target config, need at least one attachment.\n");
2134 /* Context activation is done by the caller. */
2135 BOOL
context_apply_clear_state(struct wined3d_context
*context
, struct wined3d_device
*device
,
2136 UINT rt_count
, struct wined3d_surface
**rts
, struct wined3d_surface
*depth_stencil
)
2138 const struct StateEntry
*state_table
= device
->StateTable
;
2142 if (!context_validate_rt_config(rt_count
, rts
, depth_stencil
))
2146 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2148 context_validate_onscreen_formats(device
, context
, depth_stencil
);
2152 if (!rt_count
|| surface_is_offscreen(rts
[0]))
2154 for (i
= 0; i
< rt_count
; ++i
)
2156 context
->blit_targets
[i
] = rts
[i
];
2157 rt_mask
|= (1 << i
);
2159 while (i
< context
->gl_info
->limits
.buffers
)
2161 context
->blit_targets
[i
] = NULL
;
2164 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, depth_stencil
, SFLAG_INTEXTURE
);
2165 glReadBuffer(GL_NONE
);
2166 checkGLcall("glReadBuffer");
2170 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2182 context_apply_draw_buffers(context
, rt_mask
, rts
);
2183 context
->draw_buffer_dirty
= TRUE
;
2185 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2187 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2190 if (context
->last_was_blit
)
2192 device
->frag_pipe
->enable_extension(TRUE
);
2195 /* Blending and clearing should be orthogonal, but tests on the nvidia
2196 * driver show that disabling blending when clearing improves the clearing
2197 * performance incredibly. */
2198 glDisable(GL_BLEND
);
2199 glEnable(GL_SCISSOR_TEST
);
2200 checkGLcall("glEnable GL_SCISSOR_TEST");
2203 context
->last_was_blit
= FALSE
;
2204 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), state_table
);
2205 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), state_table
);
2206 context_invalidate_state(context
, STATE_SCISSORRECT
, state_table
);
2211 /* Context activation is done by the caller. */
2212 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
2214 const struct StateEntry
*state_table
= device
->StateTable
;
2215 const struct wined3d_fb_state
*fb
= &device
->fb
;
2218 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
2219 fb
->render_targets
, fb
->depth_stencil
))
2222 /* Preload resources before FBO setup. Texture preload in particular may
2223 * result in changes to the current FBO, due to using e.g. FBO blits for
2224 * updating a resource location. */
2225 device_update_tex_unit_map(device
);
2226 device_preload_textures(device
);
2227 if (isStateDirty(context
, STATE_VDECL
))
2228 device_update_stream_info(device
, context
->gl_info
);
2230 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2232 context_validate_onscreen_formats(device
, context
, fb
->depth_stencil
);
2234 if (!context
->render_offscreen
)
2237 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2243 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, fb
->render_targets
, fb
->depth_stencil
, SFLAG_INTEXTURE
);
2244 glReadBuffer(GL_NONE
);
2245 checkGLcall("glReadBuffer");
2251 if (context
->draw_buffer_dirty
)
2253 const struct wined3d_shader
*ps
= device
->stateBlock
->state
.pixel_shader
;
2254 DWORD rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2256 rt_mask
&= device
->valid_rt_mask
;
2257 context_apply_draw_buffers(context
, rt_mask
, fb
->render_targets
);
2258 context
->draw_buffer_dirty
= FALSE
;
2261 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2263 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2266 if (context
->last_was_blit
)
2268 device
->frag_pipe
->enable_extension(TRUE
);
2271 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2273 DWORD rep
= context
->dirtyArray
[i
];
2274 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2275 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2276 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2277 state_table
[rep
].apply(rep
, device
->stateBlock
, context
);
2280 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2281 context
->last_was_blit
= FALSE
;
2286 static void context_setup_target(struct wined3d_device
*device
,
2287 struct wined3d_context
*context
, struct wined3d_surface
*target
)
2289 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
2290 const struct StateEntry
*StateTable
= device
->StateTable
;
2292 if (!target
) return;
2293 render_offscreen
= surface_is_offscreen(target
);
2294 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
2296 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2297 * the alpha blend state changes with different render target formats. */
2298 if (!context
->current_rt
)
2300 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2304 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
2305 const struct wined3d_format
*new = target
->resource
.format
;
2307 if (old
->id
!= new->id
)
2309 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2310 if ((old
->alpha_mask
&& !new->alpha_mask
) || (!old
->alpha_mask
&& new->alpha_mask
)
2311 || !(new->flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
2312 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2314 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2315 if ((old
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
2316 context_invalidate_state(context
, STATE_RENDER(WINED3DRS_SRGBWRITEENABLE
), StateTable
);
2319 /* When switching away from an offscreen render target, and we're not
2320 * using FBOs, we have to read the drawable into the texture. This is
2321 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2322 * are some things that need care though. PreLoad needs a GL context,
2323 * and FindContext is called before the context is activated. It also
2324 * has to be called with the old rendertarget active, otherwise a
2325 * wrong drawable is read. */
2326 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2327 && old_render_offscreen
&& context
->current_rt
!= target
)
2329 /* Read the back buffer of the old drawable into the destination texture. */
2330 if (context
->current_rt
->texture_name_srgb
)
2331 surface_internal_preload(context
->current_rt
, SRGB_SRGB
);
2332 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2333 surface_modify_location(context
->current_rt
, SFLAG_INDRAWABLE
, FALSE
);
2337 context
->draw_buffer_dirty
= TRUE
;
2338 context
->current_rt
= target
;
2339 context_set_render_offscreen(context
, StateTable
, render_offscreen
);
2342 /* Do not call while under the GL lock. */
2343 struct wined3d_context
*context_acquire(struct wined3d_device
*device
, struct wined3d_surface
*target
)
2345 struct wined3d_context
*current_context
= context_get_current();
2346 struct wined3d_context
*context
;
2348 TRACE("device %p, target %p.\n", device
, target
);
2350 context
= FindContext(device
, target
);
2351 context_setup_target(device
, context
, target
);
2352 context_enter(context
);
2353 if (!context
->valid
) return context
;
2355 if (context
!= current_context
)
2357 if (!context_set_current(context
))
2359 ERR("Failed to activate the new context.\n");
2364 device
->frag_pipe
->enable_extension(!context
->last_was_blit
);
2368 if (context
->vshader_const_dirty
)
2370 memset(context
->vshader_const_dirty
, 1,
2371 sizeof(*context
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
2372 device
->highest_dirty_vs_const
= device
->d3d_vshader_constantF
;
2374 if (context
->pshader_const_dirty
)
2376 memset(context
->pshader_const_dirty
, 1,
2377 sizeof(*context
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
2378 device
->highest_dirty_ps_const
= device
->d3d_pshader_constantF
;
2381 else if (context
->restore_ctx
)
2383 context_set_gl_context(context
);