2 * Context and render target management in wined3d
4 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
5 * Copyright 2009 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(IWineD3DSurfaceImpl
*surface
, DWORD location
)
117 IWineD3DBaseTextureImpl
*texture_impl
;
119 /* Update base texture states array */
120 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface
*)surface
,
121 &IID_IWineD3DBaseTexture
, (void **)&texture_impl
)))
123 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
124 BOOL update_minfilter
= FALSE
;
125 BOOL update_magfilter
= FALSE
;
126 struct gl_texture
*gl_tex
;
130 case SFLAG_INTEXTURE
:
131 gl_tex
= &texture_impl
->baseTexture
.texture_rgb
;
134 case SFLAG_INSRGBTEX
:
135 gl_tex
= &texture_impl
->baseTexture
.texture_srgb
;
139 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
140 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*)texture_impl
);
144 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3DTEXF_POINT
145 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3DTEXF_NONE
)
147 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
;
148 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_NONE
;
149 update_minfilter
= TRUE
;
152 if (gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3DTEXF_POINT
)
154 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_POINT
;
155 update_magfilter
= TRUE
;
158 if (texture_impl
->baseTexture
.bindCount
)
160 WARN("Render targets should not be bound to a sampler\n");
161 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_SAMPLER(texture_impl
->baseTexture
.sampler
));
164 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*)texture_impl
);
166 if (update_minfilter
|| update_magfilter
)
168 GLenum target
, bind_target
;
171 target
= surface
->texture_target
;
172 if (target
== GL_TEXTURE_2D
)
174 bind_target
= GL_TEXTURE_2D
;
175 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
177 else if (target
== GL_TEXTURE_RECTANGLE_ARB
)
179 bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
180 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_binding
);
184 bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
185 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARB
, &old_binding
);
188 glBindTexture(bind_target
, gl_tex
->name
);
189 if (update_minfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
190 if (update_magfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
191 glBindTexture(bind_target
, old_binding
);
194 checkGLcall("apply_attachment_filter_states()");
198 /* GL locking is done by the caller */
199 void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
200 GLenum fbo_target
, IWineD3DSurfaceImpl
*depth_stencil
, BOOL use_render_buffer
)
202 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
204 TRACE("Attach depth stencil %p\n", depth_stencil
);
208 DWORD format_flags
= depth_stencil
->resource
.format_desc
->Flags
;
210 if (use_render_buffer
&& depth_stencil
->current_renderbuffer
)
212 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
214 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
,
215 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
216 checkGLcall("glFramebufferRenderbuffer()");
219 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
221 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
,
222 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
223 checkGLcall("glFramebufferRenderbuffer()");
228 surface_prepare_texture(depth_stencil
, gl_info
, FALSE
);
229 context_apply_attachment_filter_states(depth_stencil
, SFLAG_INTEXTURE
);
231 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
233 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
234 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
235 depth_stencil
->texture_level
);
236 checkGLcall("glFramebufferTexture2D()");
239 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
241 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
242 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
243 depth_stencil
->texture_level
);
244 checkGLcall("glFramebufferTexture2D()");
248 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
250 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
251 checkGLcall("glFramebufferTexture2D()");
254 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
256 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
257 checkGLcall("glFramebufferTexture2D()");
262 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
263 checkGLcall("glFramebufferTexture2D()");
265 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
266 checkGLcall("glFramebufferTexture2D()");
270 /* GL locking is done by the caller */
271 static void context_attach_surface_fbo(const struct wined3d_context
*context
,
272 GLenum fbo_target
, DWORD idx
, IWineD3DSurfaceImpl
*surface
, DWORD location
)
274 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
276 TRACE("Attach surface %p to %u\n", surface
, idx
);
282 case SFLAG_INTEXTURE
:
283 surface_prepare_texture(surface
, gl_info
, FALSE
);
284 context_apply_attachment_filter_states(surface
, location
);
285 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
286 surface
->texture_target
, surface
->texture_name
, surface
->texture_level
);
289 case SFLAG_INSRGBTEX
:
290 surface_prepare_texture(surface
, gl_info
, TRUE
);
291 context_apply_attachment_filter_states(surface
, location
);
292 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
293 surface
->texture_target
, surface
->texture_name_srgb
, surface
->texture_level
);
297 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
300 checkGLcall("glFramebufferTexture2D()");
304 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
305 checkGLcall("glFramebufferTexture2D()");
309 /* GL locking is done by the caller */
310 static void context_check_fbo_status(struct wined3d_context
*context
, GLenum target
)
312 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
315 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
316 if (status
== GL_FRAMEBUFFER_COMPLETE
)
318 TRACE("FBO complete\n");
320 IWineD3DSurfaceImpl
*attachment
;
322 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
324 if (!context
->current_fbo
)
326 ERR("FBO 0 is incomplete, driver bug?\n");
330 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context
->current_fbo
->location
),
331 context
->current_fbo
->location
);
333 /* Dump the FBO attachments */
334 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
336 attachment
= context
->current_fbo
->render_targets
[i
];
339 FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
340 i
, attachment
, debug_d3dformat(attachment
->resource
.format_desc
->format
),
341 attachment
->pow2Width
, attachment
->pow2Height
);
344 attachment
= context
->current_fbo
->depth_stencil
;
347 FIXME("\tDepth attachment: (%p) %s %ux%u\n",
348 attachment
, debug_d3dformat(attachment
->resource
.format_desc
->format
),
349 attachment
->pow2Width
, attachment
->pow2Height
);
354 static struct fbo_entry
*context_create_fbo_entry(struct wined3d_context
*context
,
355 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
, DWORD location
)
357 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
358 struct fbo_entry
*entry
;
360 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
361 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
362 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
363 entry
->depth_stencil
= depth_stencil
;
364 entry
->location
= location
;
365 entry
->attached
= FALSE
;
371 /* GL locking is done by the caller */
372 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
373 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
,
374 DWORD location
, struct fbo_entry
*entry
)
376 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
378 context_bind_fbo(context
, target
, &entry
->id
);
379 context_clean_fbo_attachments(gl_info
, target
);
381 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
382 entry
->depth_stencil
= depth_stencil
;
383 entry
->location
= location
;
384 entry
->attached
= FALSE
;
387 /* GL locking is done by the caller */
388 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
392 TRACE("Destroy FBO %d\n", entry
->id
);
393 context_destroy_fbo(context
, &entry
->id
);
395 --context
->fbo_entry_count
;
396 list_remove(&entry
->entry
);
397 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
398 HeapFree(GetProcessHeap(), 0, entry
);
402 /* GL locking is done by the caller */
403 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
404 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
, DWORD location
)
406 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
407 struct fbo_entry
*entry
;
409 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
411 if (!memcmp(entry
->render_targets
,
412 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
413 && entry
->depth_stencil
== depth_stencil
&& entry
->location
== location
)
415 list_remove(&entry
->entry
);
416 list_add_head(&context
->fbo_list
, &entry
->entry
);
421 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
423 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, location
);
424 list_add_head(&context
->fbo_list
, &entry
->entry
);
425 ++context
->fbo_entry_count
;
429 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
430 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
, entry
);
431 list_remove(&entry
->entry
);
432 list_add_head(&context
->fbo_list
, &entry
->entry
);
438 /* GL locking is done by the caller */
439 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
441 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
444 context_bind_fbo(context
, target
, &entry
->id
);
446 if (!entry
->attached
)
448 /* Apply render targets */
449 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
451 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->location
);
454 /* Apply depth targets */
455 if (entry
->depth_stencil
)
457 surface_set_compatible_renderbuffer(entry
->depth_stencil
,
458 entry
->render_targets
[0]->pow2Width
, entry
->render_targets
[0]->pow2Height
);
460 context_attach_depth_stencil_fbo(context
, target
, entry
->depth_stencil
, TRUE
);
462 entry
->attached
= TRUE
;
466 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
468 if (entry
->render_targets
[i
])
469 context_apply_attachment_filter_states(entry
->render_targets
[i
], entry
->location
);
471 if (entry
->depth_stencil
)
472 context_apply_attachment_filter_states(entry
->depth_stencil
, SFLAG_INTEXTURE
);
476 /* GL locking is done by the caller */
477 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
478 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
, DWORD location
)
480 struct fbo_entry
*entry
, *entry2
;
482 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
484 context_destroy_fbo_entry(context
, entry
);
487 if (context
->rebind_fbo
)
489 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
490 context
->rebind_fbo
= FALSE
;
495 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
);
496 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
500 context
->current_fbo
= NULL
;
501 context_bind_fbo(context
, target
, NULL
);
504 context_check_fbo_status(context
, target
);
507 /* GL locking is done by the caller */
508 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
509 IWineD3DSurfaceImpl
*render_target
, IWineD3DSurfaceImpl
*depth_stencil
, DWORD location
)
511 if (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(IWineD3DDeviceImpl
*device
,
656 IWineD3DSurfaceImpl
*surface
, context_fbo_entry_func_t
*callback
)
660 for (i
= 0; i
< device
->numContexts
; ++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(IWineD3DDeviceImpl
*device
, IWineD3DResource
*resource
, WINED3DRESOURCETYPE type
)
698 if (!device
->d3d_initialized
) return;
702 case WINED3DRTYPE_SURFACE
:
703 context_enum_surface_fbo_entries(device
, (IWineD3DSurfaceImpl
*)resource
,
704 context_queue_fbo_entry_destruction
);
712 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
714 entry
->attached
= FALSE
;
717 void context_resource_unloaded(IWineD3DDeviceImpl
*device
, IWineD3DResource
*resource
, WINED3DRESOURCETYPE type
)
721 case WINED3DRTYPE_SURFACE
:
722 context_enum_surface_fbo_entries(device
, (IWineD3DSurfaceImpl
*)resource
,
723 context_detach_fbo_entry
);
731 void context_surface_update(struct wined3d_context
*context
, IWineD3DSurfaceImpl
*surface
)
733 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
734 struct fbo_entry
*entry
= context
->current_fbo
;
737 if (!entry
|| context
->rebind_fbo
) return;
739 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
741 if (surface
== entry
->render_targets
[i
])
743 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
744 context
->rebind_fbo
= TRUE
;
749 if (surface
== entry
->depth_stencil
)
751 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
752 context
->rebind_fbo
= TRUE
;
756 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
758 int current
= GetPixelFormat(dc
);
760 if (current
== format
) return TRUE
;
764 if (!SetPixelFormat(dc
, format
, NULL
))
766 ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
767 format
, dc
, GetLastError());
773 /* By default WGL doesn't allow pixel format adjustments but we need it
774 * here. For this reason there's a Wine specific wglSetPixelFormat()
775 * which allows us to set the pixel format multiple times. Only use it
776 * when really needed. */
777 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
779 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
, NULL
)))
781 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
788 /* OpenGL doesn't allow pixel format adjustments. Print an error and
789 * continue using the old format. There's a big chance that the old
790 * format works although with a performance hit and perhaps rendering
792 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
793 format
, dc
, current
);
797 static void context_update_window(struct wined3d_context
*context
)
799 TRACE("Updating context %p window from %p to %p.\n",
800 context
, context
->win_handle
, context
->swapchain
->win_handle
);
804 if (!ReleaseDC(context
->win_handle
, context
->hdc
))
806 ERR("Failed to release device context %p, last error %#x.\n",
807 context
->hdc
, GetLastError());
810 else context
->valid
= 1;
812 context
->win_handle
= context
->swapchain
->win_handle
;
814 if (!(context
->hdc
= GetDC(context
->win_handle
)))
816 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
820 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
822 ERR("Failed to set pixel format %d on device context %p.\n",
823 context
->pixel_format
, context
->hdc
);
827 if (!pwglMakeCurrent(context
->hdc
, context
->glCtx
))
829 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
830 context
->glCtx
, context
->hdc
, GetLastError());
840 static void context_validate(struct wined3d_context
*context
)
842 HWND wnd
= WindowFromDC(context
->hdc
);
844 if (wnd
!= context
->win_handle
)
846 WARN("DC %p belongs to window %p instead of %p.\n",
847 context
->hdc
, wnd
, context
->win_handle
);
851 if (context
->win_handle
!= context
->swapchain
->win_handle
)
852 context_update_window(context
);
855 static void context_destroy_gl_resources(struct wined3d_context
*context
)
857 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
858 struct wined3d_occlusion_query
*occlusion_query
;
859 struct wined3d_event_query
*event_query
;
860 struct fbo_entry
*entry
, *entry2
;
865 restore_ctx
= pwglGetCurrentContext();
866 restore_dc
= pwglGetCurrentDC();
868 context_validate(context
);
869 if (context
->valid
&& restore_ctx
!= context
->glCtx
) pwglMakeCurrent(context
->hdc
, context
->glCtx
);
870 else restore_ctx
= NULL
;
874 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
876 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
877 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
878 occlusion_query
->context
= NULL
;
881 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
885 if (gl_info
->supported
[ARB_SYNC
])
887 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
889 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
890 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
892 event_query
->context
= NULL
;
895 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
897 if (!context
->valid
) entry
->id
= 0;
898 context_destroy_fbo_entry(context
, entry
);
901 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
903 if (!context
->valid
) entry
->id
= 0;
904 context_destroy_fbo_entry(context
, entry
);
909 if (context
->dst_fbo
)
911 TRACE("Destroy dst FBO %d\n", context
->dst_fbo
);
912 context_destroy_fbo(context
, &context
->dst_fbo
);
914 if (context
->dummy_arbfp_prog
)
916 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
919 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
920 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
922 if (gl_info
->supported
[ARB_SYNC
])
924 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
926 else if (gl_info
->supported
[APPLE_FENCE
])
928 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
930 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
933 else if (gl_info
->supported
[NV_FENCE
])
935 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
937 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
941 checkGLcall("context cleanup");
946 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
947 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
951 if (!pwglMakeCurrent(restore_dc
, restore_ctx
))
953 DWORD err
= GetLastError();
954 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
955 restore_ctx
, restore_dc
, err
);
958 else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL
, NULL
))
960 ERR("Failed to disable GL context.\n");
963 ReleaseDC(context
->win_handle
, context
->hdc
);
965 if (!pwglDeleteContext(context
->glCtx
))
967 DWORD err
= GetLastError();
968 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
972 DWORD
context_get_tls_idx(void)
974 return wined3d_context_tls_idx
;
977 void context_set_tls_idx(DWORD idx
)
979 wined3d_context_tls_idx
= idx
;
982 struct wined3d_context
*context_get_current(void)
984 return TlsGetValue(wined3d_context_tls_idx
);
987 BOOL
context_set_current(struct wined3d_context
*ctx
)
989 struct wined3d_context
*old
= context_get_current();
993 TRACE("Already using D3D context %p.\n", ctx
);
1001 TRACE("Switching away from destroyed context %p.\n", old
);
1002 context_destroy_gl_resources(old
);
1003 HeapFree(GetProcessHeap(), 0, old
);
1013 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1014 if (!pwglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
1016 DWORD err
= GetLastError();
1017 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
1018 ctx
->glCtx
, ctx
->hdc
, err
);
1019 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1024 else if(pwglGetCurrentContext())
1026 TRACE("Clearing current D3D context.\n");
1027 if (!pwglMakeCurrent(NULL
, NULL
))
1029 DWORD err
= GetLastError();
1030 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1031 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1036 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1039 void context_release(struct wined3d_context
*context
)
1041 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1045 if (!context
->level
)
1046 WARN("Context %p is not active.\n", context
);
1047 else if (context
!= context_get_current())
1048 WARN("Context %p is not the current context.\n", context
);
1051 if (!--context
->level
&& context
->restore_ctx
)
1053 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1054 if (!pwglMakeCurrent(context
->restore_dc
, context
->restore_ctx
))
1056 DWORD err
= GetLastError();
1057 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1058 context
->restore_ctx
, context
->restore_dc
, err
);
1060 context
->restore_ctx
= NULL
;
1061 context
->restore_dc
= NULL
;
1065 static void context_enter(struct wined3d_context
*context
)
1067 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1069 if (!context
->level
++)
1071 const struct wined3d_context
*current_context
= context_get_current();
1072 HGLRC current_gl
= pwglGetCurrentContext();
1074 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1076 TRACE("Another GL context (%p on device context %p) is already current.\n",
1077 current_gl
, pwglGetCurrentDC());
1078 context
->restore_ctx
= current_gl
;
1079 context
->restore_dc
= pwglGetCurrentDC();
1084 /*****************************************************************************
1085 * Context_MarkStateDirty
1087 * Marks a state in a context dirty. Only one context, opposed to
1088 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
1092 * context: Context to mark the state dirty in
1093 * state: State to mark dirty
1094 * StateTable: Pointer to the state table in use(for state grouping)
1096 *****************************************************************************/
1097 static void Context_MarkStateDirty(struct wined3d_context
*context
, DWORD state
, const struct StateEntry
*StateTable
)
1099 DWORD rep
= StateTable
[state
].representative
;
1103 if (isStateDirty(context
, rep
)) return;
1105 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1106 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1107 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1108 context
->isStateDirty
[idx
] |= (1 << shift
);
1111 /* This function takes care of WineD3D pixel format selection. */
1112 static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl
*This
, HDC hdc
,
1113 const struct wined3d_format_desc
*color_format_desc
, const struct wined3d_format_desc
*ds_format_desc
,
1114 BOOL auxBuffers
, int numSamples
, BOOL findCompatible
)
1117 unsigned int matchtry
;
1118 short redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1119 short depthBits
=0, stencilBits
=0;
1126 /* First, try without alpha match buffers. MacOS supports aux buffers only
1127 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
1128 * Then try without aux buffers - this is the most common cause for not
1129 * finding a pixel format. Also some drivers(the open source ones)
1130 * only offer 32 bit ARB pixel formats. First try without an exact alpha
1131 * match, then try without an exact alpha and color match.
1133 { TRUE
, TRUE
, TRUE
},
1134 { TRUE
, FALSE
, TRUE
},
1135 { FALSE
, TRUE
, TRUE
},
1136 { FALSE
, FALSE
, TRUE
},
1137 { TRUE
, FALSE
, FALSE
},
1138 { FALSE
, FALSE
, FALSE
},
1142 int nCfgs
= This
->adapter
->nCfgs
;
1144 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, findCompatible=%d\n",
1145 debug_d3dformat(color_format_desc
->format
), debug_d3dformat(ds_format_desc
->format
),
1146 auxBuffers
, numSamples
, findCompatible
);
1148 if (!getColorBits(color_format_desc
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1150 ERR("Unable to get color bits for format %s (%#x)!\n",
1151 debug_d3dformat(color_format_desc
->format
), color_format_desc
->format
);
1155 getDepthStencilBits(ds_format_desc
, &depthBits
, &stencilBits
);
1157 for(matchtry
= 0; matchtry
< (sizeof(matches
) / sizeof(matches
[0])) && !iPixelFormat
; matchtry
++) {
1158 for(i
=0; i
<nCfgs
; i
++) {
1159 BOOL exactDepthMatch
= TRUE
;
1160 WineD3D_PixelFormat
*cfg
= &This
->adapter
->cfgs
[i
];
1162 /* For now only accept RGBA formats. Perhaps some day we will
1163 * allow floating point formats for pbuffers. */
1164 if(cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1167 /* In window mode we need a window drawable format and double buffering. */
1168 if(!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1171 /* We like to have aux buffers in backbuffer mode */
1172 if(auxBuffers
&& !cfg
->auxBuffers
&& matches
[matchtry
].require_aux
)
1175 if(matches
[matchtry
].exact_color
) {
1176 if(cfg
->redSize
!= redBits
)
1178 if(cfg
->greenSize
!= greenBits
)
1180 if(cfg
->blueSize
!= blueBits
)
1183 if(cfg
->redSize
< redBits
)
1185 if(cfg
->greenSize
< greenBits
)
1187 if(cfg
->blueSize
< blueBits
)
1190 if(matches
[matchtry
].exact_alpha
) {
1191 if(cfg
->alphaSize
!= alphaBits
)
1194 if(cfg
->alphaSize
< alphaBits
)
1198 /* We try to locate a format which matches our requirements exactly. In case of
1199 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1200 if(cfg
->depthSize
< depthBits
)
1202 else if(cfg
->depthSize
> depthBits
)
1203 exactDepthMatch
= FALSE
;
1205 /* In all cases make sure the number of stencil bits matches our requirements
1206 * even when we don't need stencil because it could affect performance EXCEPT
1207 * on cards which don't offer depth formats without stencil like the i915 drivers
1209 if(stencilBits
!= cfg
->stencilSize
&& !(This
->adapter
->brokenStencil
&& stencilBits
<= cfg
->stencilSize
))
1212 /* Check multisampling support */
1213 if(cfg
->numSamples
!= numSamples
)
1216 /* When we have passed all the checks then we have found a format which matches our
1217 * requirements. Note that we only check for a limit number of capabilities right now,
1218 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
1219 * can still differ in things like multisampling, stereo, SRGB and other flags.
1222 /* Exit the loop as we have found a format :) */
1223 if(exactDepthMatch
) {
1224 iPixelFormat
= cfg
->iPixelFormat
;
1226 } else if(!iPixelFormat
) {
1227 /* In the end we might end up with a format which doesn't exactly match our depth
1228 * requirements. Accept the first format we found because formats with higher iPixelFormat
1229 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
1230 iPixelFormat
= cfg
->iPixelFormat
;
1235 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1236 if(!iPixelFormat
&& !findCompatible
) {
1237 ERR("Can't find a suitable iPixelFormat\n");
1239 } else if(!iPixelFormat
) {
1240 PIXELFORMATDESCRIPTOR pfd
;
1242 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1243 /* PixelFormat selection */
1244 ZeroMemory(&pfd
, sizeof(pfd
));
1245 pfd
.nSize
= sizeof(pfd
);
1247 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1248 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1249 pfd
.cAlphaBits
= alphaBits
;
1250 pfd
.cColorBits
= colorBits
;
1251 pfd
.cDepthBits
= depthBits
;
1252 pfd
.cStencilBits
= stencilBits
;
1253 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1255 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1257 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1258 ERR("Can't find a suitable iPixelFormat\n");
1263 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1264 iPixelFormat
, debug_d3dformat(color_format_desc
->format
), debug_d3dformat(ds_format_desc
->format
));
1265 return iPixelFormat
;
1268 /*****************************************************************************
1271 * Creates a new context.
1274 * This: Device to activate the context for
1275 * target: Surface this context will render to
1276 * win_handle: handle to the window which we are drawing to
1277 * pPresentParameters: contains the pixelformats to use for onscreen rendering
1279 *****************************************************************************/
1280 struct wined3d_context
*context_create(IWineD3DSwapChainImpl
*swapchain
, IWineD3DSurfaceImpl
*target
,
1281 const struct wined3d_format_desc
*ds_format_desc
)
1283 IWineD3DDeviceImpl
*device
= swapchain
->device
;
1284 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1285 const struct wined3d_format_desc
*color_format_desc
;
1286 struct wined3d_context
*ret
;
1287 PIXELFORMATDESCRIPTOR pfd
;
1288 BOOL auxBuffers
= FALSE
;
1296 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1298 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1301 ERR("Failed to allocate context memory.\n");
1305 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1307 ERR("Failed to retrieve a device context.\n");
1311 color_format_desc
= target
->resource
.format_desc
;
1313 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1314 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1315 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1319 if (color_format_desc
->format
== WINED3DFMT_B4G4R4X4_UNORM
)
1320 color_format_desc
= getFormatDescEntry(WINED3DFMT_B4G4R4A4_UNORM
, gl_info
);
1321 else if (color_format_desc
->format
== WINED3DFMT_B8G8R8X8_UNORM
)
1322 color_format_desc
= getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM
, gl_info
);
1325 /* DirectDraw supports 8bit paletted render targets and these are used by
1326 * old games like Starcraft and C&C. Most modern hardware doesn't support
1327 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1328 * conversion (ab)uses the alpha component for storing the palette index.
1329 * For this reason we require a format with 8bit alpha, so request
1331 if (color_format_desc
->format
== WINED3DFMT_P8_UINT
)
1332 color_format_desc
= getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM
, gl_info
);
1334 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
1335 if (swapchain
->presentParms
.MultiSampleType
&& (swapchain
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_DISCARD
))
1337 if (!gl_info
->supported
[ARB_MULTISAMPLE
])
1338 WARN("The application is requesting multisampling without support.\n");
1341 TRACE("Requesting multisample type %#x.\n", swapchain
->presentParms
.MultiSampleType
);
1342 numSamples
= swapchain
->presentParms
.MultiSampleType
;
1346 /* Try to find a pixel format which matches our requirements. */
1347 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format_desc
, ds_format_desc
,
1348 auxBuffers
, numSamples
, FALSE
/* findCompatible */);
1350 /* Try to locate a compatible format if we weren't able to find anything. */
1353 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1354 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format_desc
, ds_format_desc
,
1355 auxBuffers
, 0 /* numSamples */, TRUE
/* findCompatible */);
1358 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1361 ERR("Can't find a suitable pixel format.\n");
1365 DescribePixelFormat(hdc
, pixel_format
, sizeof(pfd
), &pfd
);
1366 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1368 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1372 ctx
= pwglCreateContext(hdc
);
1373 if (device
->numContexts
)
1375 if (!pwglShareLists(device
->contexts
[0]->glCtx
, ctx
))
1377 DWORD err
= GetLastError();
1378 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1379 device
->contexts
[0]->glCtx
, ctx
, err
);
1384 ERR("Failed to create a WGL context\n");
1388 if (!device_context_add(device
, ret
))
1390 ERR("Failed to add the newly created context to the context list\n");
1391 if (!pwglDeleteContext(ctx
))
1393 DWORD err
= GetLastError();
1394 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, err
);
1399 ret
->gl_info
= gl_info
;
1401 /* Mark all states dirty to force a proper initialization of the states
1402 * on the first use of the context. */
1403 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1405 if (device
->StateTable
[state
].representative
)
1406 Context_MarkStateDirty(ret
, state
, device
->StateTable
);
1409 ret
->swapchain
= swapchain
;
1410 ret
->current_rt
= target
;
1411 ret
->tid
= GetCurrentThreadId();
1413 ret
->render_offscreen
= surface_is_offscreen(target
);
1414 ret
->draw_buffer_dirty
= TRUE
;
1418 ret
->win_handle
= swapchain
->win_handle
;
1420 ret
->pixel_format
= pixel_format
;
1422 if (device
->shader_backend
->shader_dirtifyable_constants((IWineD3DDevice
*)device
))
1424 /* Create the dirty constants array and initialize them to dirty */
1425 ret
->vshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1426 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1427 ret
->pshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1428 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1429 memset(ret
->vshader_const_dirty
, 1,
1430 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1431 memset(ret
->pshader_const_dirty
, 1,
1432 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1435 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1436 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1437 if (!ret
->blit_targets
) goto out
;
1439 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1440 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1441 if (!ret
->draw_buffers
) goto out
;
1443 ret
->free_occlusion_query_size
= 4;
1444 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1445 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1446 if (!ret
->free_occlusion_queries
) goto out
;
1448 list_init(&ret
->occlusion_queries
);
1450 ret
->free_event_query_size
= 4;
1451 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1452 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1453 if (!ret
->free_event_queries
) goto out
;
1455 list_init(&ret
->event_queries
);
1457 TRACE("Successfully created new context %p\n", ret
);
1459 list_init(&ret
->fbo_list
);
1460 list_init(&ret
->fbo_destroy_list
);
1464 /* Set up the context defaults */
1465 if (!context_set_current(ret
))
1467 ERR("Cannot activate context to set up defaults\n");
1468 context_release(ret
);
1474 glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1476 TRACE("Setting up the screen\n");
1477 /* Clear the screen */
1478 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
1479 checkGLcall("glClearColor");
1482 glClearStencil(0xffff);
1484 checkGLcall("glClear");
1486 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1487 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1489 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1490 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1492 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1493 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1495 glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1496 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1497 glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1498 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1500 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
1502 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1503 * and textures in DIB sections(due to the memory protection).
1505 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1506 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1508 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1510 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1511 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1512 * GL_VERTEX_BLEND_ARB isn't enabled too
1514 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1515 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1517 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1519 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1520 * the previous texture where to source the offset from is always unit - 1.
1522 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1524 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1525 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1526 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1529 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1531 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1532 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1533 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1534 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1537 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1538 * program and the dummy program is destroyed when the context is destroyed.
1540 const char *dummy_program
=
1542 "MOV result.color, fragment.color.primary;\n"
1544 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1545 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1546 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1549 for (s
= 0; s
< gl_info
->limits
.point_sprite_units
; ++s
)
1551 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1552 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1553 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1556 if (gl_info
->supported
[WINED3D_GL_VERSION_2_0
])
1558 /* Windows doesn't support to query the glPointParameteri function pointer, so use the
1559 * NV_POINT_SPRITE extension.
1561 if (glPointParameteri
)
1563 glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN
, GL_UPPER_LEFT
);
1564 checkGLcall("glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT)");
1566 else if (gl_info
->supported
[NV_POINT_SPRITE
])
1568 GL_EXTCALL(glPointParameteriNV(GL_POINT_SPRITE_COORD_ORIGIN
, GL_UPPER_LEFT
));
1569 checkGLcall("glPointParameteriNV(GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT)");
1573 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1575 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1577 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1579 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1584 device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, TRUE
);
1586 TRACE("Created context %p.\n", ret
);
1591 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1592 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1593 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1594 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1595 HeapFree(GetProcessHeap(), 0, ret
->pshader_const_dirty
);
1596 HeapFree(GetProcessHeap(), 0, ret
->vshader_const_dirty
);
1597 HeapFree(GetProcessHeap(), 0, ret
);
1601 /*****************************************************************************
1604 * Destroys a wined3d context
1607 * This: Device to activate the context for
1608 * context: Context to destroy
1610 *****************************************************************************/
1611 void context_destroy(IWineD3DDeviceImpl
*This
, struct wined3d_context
*context
)
1615 TRACE("Destroying ctx %p\n", context
);
1617 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1619 context_destroy_gl_resources(context
);
1620 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1625 context
->destroyed
= 1;
1629 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1630 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1631 HeapFree(GetProcessHeap(), 0, context
->vshader_const_dirty
);
1632 HeapFree(GetProcessHeap(), 0, context
->pshader_const_dirty
);
1633 device_context_remove(This
, context
);
1634 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1637 /* GL locking is done by the caller */
1638 static inline void set_blit_dimension(UINT width
, UINT height
) {
1639 glMatrixMode(GL_PROJECTION
);
1640 checkGLcall("glMatrixMode(GL_PROJECTION)");
1642 checkGLcall("glLoadIdentity()");
1643 glOrtho(0, width
, height
, 0, 0.0, -1.0);
1644 checkGLcall("glOrtho");
1645 glViewport(0, 0, width
, height
);
1646 checkGLcall("glViewport");
1649 /*****************************************************************************
1652 * Sets up a context for DirectDraw blitting.
1653 * All texture units are disabled, texture unit 0 is set as current unit
1654 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1655 * color writing enabled for all channels
1656 * register combiners disabled, shaders disabled
1657 * world matrix is set to identity, texture matrix 0 too
1658 * projection matrix is setup for drawing screen coordinates
1661 * This: Device to activate the context for
1662 * context: Context to setup
1664 *****************************************************************************/
1665 /* Context activation is done by the caller. */
1666 static void SetupForBlit(IWineD3DDeviceImpl
*This
, struct wined3d_context
*context
)
1669 const struct StateEntry
*StateTable
= This
->StateTable
;
1670 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1671 UINT width
= context
->current_rt
->currentDesc
.Width
;
1672 UINT height
= context
->current_rt
->currentDesc
.Height
;
1675 TRACE("Setting up context %p for blitting\n", context
);
1676 if(context
->last_was_blit
) {
1677 if(context
->blit_w
!= width
|| context
->blit_h
!= height
) {
1679 set_blit_dimension(width
, height
);
1681 context
->blit_w
= width
; context
->blit_h
= height
;
1682 /* No need to dirtify here, the states are still dirtified because they weren't
1683 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1687 TRACE("Context is already set up for blitting, nothing to do\n");
1690 context
->last_was_blit
= TRUE
;
1692 /* TODO: Use a display list */
1694 /* Disable shaders */
1696 This
->shader_backend
->shader_select(context
, FALSE
, FALSE
);
1699 Context_MarkStateDirty(context
, STATE_VSHADER
, StateTable
);
1700 Context_MarkStateDirty(context
, STATE_PIXELSHADER
, StateTable
);
1702 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1703 * helper functions in between gl calls. This function is full of Context_MarkStateDirty
1704 * which can safely be called from here, we only lock once instead locking/unlocking
1705 * after each GL call.
1709 /* Disable all textures. The caller can then bind a texture it wants to blit
1712 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1713 * function texture unit. No need to care for higher samplers
1715 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1717 sampler
= This
->rev_tex_unit_map
[i
];
1718 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1719 checkGLcall("glActiveTextureARB");
1721 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1723 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1724 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1726 glDisable(GL_TEXTURE_3D
);
1727 checkGLcall("glDisable GL_TEXTURE_3D");
1728 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1730 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1731 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1733 glDisable(GL_TEXTURE_2D
);
1734 checkGLcall("glDisable GL_TEXTURE_2D");
1736 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1737 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1739 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1741 if (sampler
< MAX_TEXTURES
) {
1742 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1744 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
1747 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
1748 checkGLcall("glActiveTextureARB");
1750 sampler
= This
->rev_tex_unit_map
[0];
1752 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1754 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1755 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1757 glDisable(GL_TEXTURE_3D
);
1758 checkGLcall("glDisable GL_TEXTURE_3D");
1759 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1761 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1762 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1764 glDisable(GL_TEXTURE_2D
);
1765 checkGLcall("glDisable GL_TEXTURE_2D");
1767 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1769 glMatrixMode(GL_TEXTURE
);
1770 checkGLcall("glMatrixMode(GL_TEXTURE)");
1772 checkGLcall("glLoadIdentity()");
1774 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1776 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1777 GL_TEXTURE_LOD_BIAS_EXT
,
1779 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
1782 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1784 if (sampler
< MAX_TEXTURES
) {
1785 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
+ sampler
), StateTable
);
1786 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1788 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
1791 /* Other misc states */
1792 glDisable(GL_ALPHA_TEST
);
1793 checkGLcall("glDisable(GL_ALPHA_TEST)");
1794 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
), StateTable
);
1795 glDisable(GL_LIGHTING
);
1796 checkGLcall("glDisable GL_LIGHTING");
1797 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_LIGHTING
), StateTable
);
1798 glDisable(GL_DEPTH_TEST
);
1799 checkGLcall("glDisable GL_DEPTH_TEST");
1800 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ZENABLE
), StateTable
);
1801 glDisableWINE(GL_FOG
);
1802 checkGLcall("glDisable GL_FOG");
1803 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
), StateTable
);
1804 glDisable(GL_BLEND
);
1805 checkGLcall("glDisable GL_BLEND");
1806 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1807 glDisable(GL_CULL_FACE
);
1808 checkGLcall("glDisable GL_CULL_FACE");
1809 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CULLMODE
), StateTable
);
1810 glDisable(GL_STENCIL_TEST
);
1811 checkGLcall("glDisable GL_STENCIL_TEST");
1812 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
), StateTable
);
1813 glDisable(GL_SCISSOR_TEST
);
1814 checkGLcall("glDisable GL_SCISSOR_TEST");
1815 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
1816 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1818 glDisable(GL_POINT_SPRITE_ARB
);
1819 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1820 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
), StateTable
);
1822 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1823 checkGLcall("glColorMask");
1824 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
), StateTable
);
1825 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1
), StateTable
);
1826 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2
), StateTable
);
1827 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3
), StateTable
);
1828 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1830 glDisable(GL_COLOR_SUM_EXT
);
1831 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
1832 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1835 /* Setup transforms */
1836 glMatrixMode(GL_MODELVIEW
);
1837 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1839 checkGLcall("glLoadIdentity()");
1840 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable
);
1842 context
->last_was_rhw
= TRUE
;
1843 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
); /* because of last_was_rhw = TRUE */
1845 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1846 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1847 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1848 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1849 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1850 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1851 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
1853 set_blit_dimension(width
, height
);
1857 context
->blit_w
= width
; context
->blit_h
= height
;
1858 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
1859 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1862 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, FALSE
);
1865 /*****************************************************************************
1866 * findThreadContextForSwapChain
1868 * Searches a swapchain for all contexts and picks one for the thread tid.
1869 * If none can be found the swapchain is requested to create a new context
1871 *****************************************************************************/
1872 static struct wined3d_context
*findThreadContextForSwapChain(IWineD3DSwapChain
*swapchain
, DWORD tid
)
1876 for(i
= 0; i
< ((IWineD3DSwapChainImpl
*) swapchain
)->num_contexts
; i
++) {
1877 if(((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
]->tid
== tid
) {
1878 return ((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
];
1883 /* Create a new context for the thread */
1884 return swapchain_create_context_for_thread(swapchain
);
1887 /*****************************************************************************
1890 * Finds a context for the current render target and thread
1893 * target: Render target to find the context for
1894 * tid: Thread to activate the context for
1896 * Returns: The needed context
1898 *****************************************************************************/
1899 static struct wined3d_context
*FindContext(IWineD3DDeviceImpl
*This
, IWineD3DSurfaceImpl
*target
)
1901 IWineD3DSwapChain
*swapchain
= NULL
;
1902 struct wined3d_context
*current_context
= context_get_current();
1903 DWORD tid
= GetCurrentThreadId();
1904 struct wined3d_context
*context
;
1906 if (current_context
&& current_context
->destroyed
) current_context
= NULL
;
1911 && current_context
->current_rt
1912 && current_context
->swapchain
->device
== This
)
1914 target
= current_context
->current_rt
;
1918 IWineD3DSwapChainImpl
*swapchain
= (IWineD3DSwapChainImpl
*)This
->swapchains
[0];
1919 if (swapchain
->back_buffers
) target
= swapchain
->back_buffers
[0];
1920 else target
= swapchain
->front_buffer
;
1924 if (current_context
&& current_context
->current_rt
== target
)
1926 context_validate(current_context
);
1927 return current_context
;
1930 if (target
->Flags
& SFLAG_SWAPCHAIN
)
1932 TRACE("Rendering onscreen\n");
1934 swapchain
= (IWineD3DSwapChain
*)target
->container
;
1935 context
= findThreadContextForSwapChain(swapchain
, tid
);
1939 TRACE("Rendering offscreen\n");
1941 /* Stay with the currently active context. */
1942 if (current_context
&& current_context
->swapchain
->device
== This
)
1944 context
= current_context
;
1948 /* This may happen if the app jumps straight into offscreen rendering
1949 * Start using the context of the primary swapchain. tid == 0 is no problem
1950 * for findThreadContextForSwapChain.
1952 * Can also happen on thread switches - in that case findThreadContextForSwapChain
1953 * is perfect to call. */
1954 context
= findThreadContextForSwapChain(This
->swapchains
[0], tid
);
1958 context_validate(context
);
1963 /* Context activation is done by the caller. */
1964 static void context_apply_draw_buffer(struct wined3d_context
*context
, BOOL blit
)
1966 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1967 IWineD3DSurfaceImpl
*rt
= context
->current_rt
;
1968 IWineD3DDeviceImpl
*device
;
1970 device
= rt
->resource
.device
;
1971 if (!surface_is_offscreen(rt
))
1974 glDrawBuffer(surface_get_gl_buffer(rt
));
1975 checkGLcall("glDrawBuffer()");
1981 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1987 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
1989 if (device
->render_targets
[i
])
1990 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
1992 context
->draw_buffers
[i
] = GL_NONE
;
1995 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1997 GL_EXTCALL(glDrawBuffersARB(gl_info
->limits
.buffers
, context
->draw_buffers
));
1998 checkGLcall("glDrawBuffers()");
2002 glDrawBuffer(context
->draw_buffers
[0]);
2003 checkGLcall("glDrawBuffer()");
2006 glDrawBuffer(GL_COLOR_ATTACHMENT0
);
2007 checkGLcall("glDrawBuffer()");
2012 glDrawBuffer(device
->offscreenBuffer
);
2013 checkGLcall("glDrawBuffer()");
2019 /* GL locking is done by the caller. */
2020 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2022 glDrawBuffer(buffer
);
2023 checkGLcall("glDrawBuffer()");
2024 context
->draw_buffer_dirty
= TRUE
;
2027 static inline void context_set_render_offscreen(struct wined3d_context
*context
, const struct StateEntry
*StateTable
,
2030 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2032 if (context
->render_offscreen
== offscreen
) return;
2034 if (gl_info
->supported
[WINED3D_GL_VERSION_2_0
])
2036 /* Windows doesn't support to query the glPointParameteri function pointer, so use the
2037 * NV_POINT_SPRITE extension.
2039 if (glPointParameteri
)
2041 glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN
, offscreen
? GL_LOWER_LEFT
: GL_UPPER_LEFT
);
2042 checkGLcall("glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, ...)");
2044 else if (gl_info
->supported
[NV_POINT_SPRITE
])
2046 GL_EXTCALL(glPointParameteriNV(GL_POINT_SPRITE_COORD_ORIGIN
, offscreen
? GL_LOWER_LEFT
: GL_UPPER_LEFT
));
2047 checkGLcall("glPointParameteriNV(GL_POINT_SPRITE_COORD_ORIGIN, ...)");
2051 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
2052 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
);
2053 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
2054 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
2055 Context_MarkStateDirty(context
, STATE_FRONTFACE
, StateTable
);
2056 context
->render_offscreen
= offscreen
;
2059 static BOOL
match_depth_stencil_format(const struct wined3d_format_desc
*existing
,
2060 const struct wined3d_format_desc
*required
)
2062 short existing_depth
, existing_stencil
, required_depth
, required_stencil
;
2064 if(existing
== required
) return TRUE
;
2065 if((existing
->Flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->Flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
2067 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
2068 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
2070 if(existing_depth
< required_depth
) return FALSE
;
2071 /* If stencil bits are used the exact amount is required - otherwise wrapping
2072 * won't work correctly */
2073 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
2076 /* The caller provides a context */
2077 static void context_validate_onscreen_formats(IWineD3DDeviceImpl
*device
,
2078 struct wined3d_context
*context
, IWineD3DSurfaceImpl
*depth_stencil
)
2080 /* Onscreen surfaces are always in a swapchain */
2081 IWineD3DSwapChainImpl
*swapchain
= (IWineD3DSwapChainImpl
*)context
->current_rt
->container
;
2083 if (context
->render_offscreen
|| !depth_stencil
) return;
2084 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->resource
.format_desc
)) return;
2086 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2087 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2089 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2091 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2092 surface_load_location(context
->current_rt
, SFLAG_INTEXTURE
, NULL
);
2093 swapchain
->render_to_fbo
= TRUE
;
2094 context_set_render_offscreen(context
, device
->StateTable
, TRUE
);
2097 /* Context activation is done by the caller. */
2098 void context_apply_blit_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
)
2100 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2102 context_validate_onscreen_formats(device
, context
, NULL
);
2104 if (context
->render_offscreen
)
2106 FIXME("Applying blit state for an offscreen target with ORM_FBO. This should be avoided.\n");
2107 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2110 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, context
->current_rt
, NULL
, SFLAG_INTEXTURE
);
2116 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
2120 context
->draw_buffer_dirty
= TRUE
;
2123 if (context
->draw_buffer_dirty
)
2125 context_apply_draw_buffer(context
, TRUE
);
2126 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2127 context
->draw_buffer_dirty
= FALSE
;
2130 SetupForBlit(device
, context
);
2133 /* Context activation is done by the caller. */
2134 void context_apply_clear_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
,
2135 UINT rt_count
, IWineD3DSurfaceImpl
**rts
, IWineD3DSurfaceImpl
*depth_stencil
)
2137 const struct StateEntry
*state_table
= device
->StateTable
;
2140 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2142 context_validate_onscreen_formats(device
, context
, depth_stencil
);
2146 if (surface_is_offscreen(rts
[0]))
2148 for (i
= 0; i
< rt_count
; ++i
)
2150 context
->blit_targets
[i
] = rts
[i
];
2152 while (i
< context
->gl_info
->limits
.buffers
)
2154 context
->blit_targets
[i
] = NULL
;
2157 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, depth_stencil
, SFLAG_INTEXTURE
);
2161 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2167 if (!surface_is_offscreen(rts
[0]))
2170 context_set_draw_buffer(context
, surface_get_gl_buffer(rts
[0]));
2177 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2179 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2180 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
2182 if (i
< rt_count
&& rts
[i
])
2183 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2185 context
->draw_buffers
[i
] = GL_NONE
;
2187 GL_EXTCALL(glDrawBuffersARB(gl_info
->limits
.buffers
, context
->draw_buffers
));
2188 checkGLcall("glDrawBuffers()");
2189 context
->draw_buffer_dirty
= TRUE
;
2193 glDrawBuffer(device
->offscreenBuffer
);
2194 checkGLcall("glDrawBuffer()");
2200 if (context
->last_was_blit
)
2202 device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, TRUE
);
2205 /* Blending and clearing should be orthogonal, but tests on the nvidia
2206 * driver show that disabling blending when clearing improves the clearing
2207 * performance incredibly. */
2209 glDisable(GL_BLEND
);
2210 glEnable(GL_SCISSOR_TEST
);
2211 checkGLcall("glEnable GL_SCISSOR_TEST");
2214 context
->last_was_blit
= FALSE
;
2215 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), state_table
);
2216 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), state_table
);
2217 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, state_table
);
2220 /* Context activation is done by the caller. */
2221 void context_apply_draw_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
)
2223 const struct StateEntry
*state_table
= device
->StateTable
;
2226 /* Preload resources before FBO setup. Texture preload in particular may
2227 * result in changes to the current FBO, due to using e.g. FBO blits for
2228 * updating a resource location. */
2229 IWineD3DDeviceImpl_FindTexUnitMap(device
);
2230 device_preload_textures(device
);
2231 if (isStateDirty(context
, STATE_VDECL
))
2232 device_update_stream_info(device
, context
->gl_info
);
2234 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2236 context_validate_onscreen_formats(device
, context
, device
->depth_stencil
);
2238 if (!context
->render_offscreen
)
2241 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2247 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, device
->render_targets
,
2248 device
->depth_stencil
, SFLAG_INTEXTURE
);
2253 if (context
->draw_buffer_dirty
)
2255 context_apply_draw_buffer(context
, FALSE
);
2256 context
->draw_buffer_dirty
= FALSE
;
2259 if (context
->last_was_blit
)
2261 device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, TRUE
);
2265 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2267 DWORD rep
= context
->dirtyArray
[i
];
2268 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2269 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2270 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2271 state_table
[rep
].apply(rep
, device
->stateBlock
, context
);
2274 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2275 context
->last_was_blit
= FALSE
;
2278 static void context_setup_target(IWineD3DDeviceImpl
*device
,
2279 struct wined3d_context
*context
, IWineD3DSurfaceImpl
*target
)
2281 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
2282 const struct StateEntry
*StateTable
= device
->StateTable
;
2284 if (!target
) return;
2285 render_offscreen
= surface_is_offscreen(target
);
2286 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
2288 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2289 * the alpha blend state changes with different render target formats. */
2290 if (!context
->current_rt
)
2292 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2296 const struct wined3d_format_desc
*old
= context
->current_rt
->resource
.format_desc
;
2297 const struct wined3d_format_desc
*new = target
->resource
.format_desc
;
2299 if (old
->format
!= new->format
)
2301 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2302 if ((old
->alpha_mask
&& !new->alpha_mask
) || (!old
->alpha_mask
&& new->alpha_mask
)
2303 || !(new->Flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
2305 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2307 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2308 if ((old
->Flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->Flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
2310 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SRGBWRITEENABLE
), StateTable
);
2314 /* When switching away from an offscreen render target, and we're not
2315 * using FBOs, we have to read the drawable into the texture. This is
2316 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2317 * are some things that need care though. PreLoad needs a GL context,
2318 * and FindContext is called before the context is activated. It also
2319 * has to be called with the old rendertarget active, otherwise a
2320 * wrong drawable is read. */
2321 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2322 && old_render_offscreen
&& context
->current_rt
!= target
)
2324 BOOL oldInDraw
= device
->isInDraw
;
2326 /* surface_internal_preload() requires a context to load the
2327 * texture, so it will call context_acquire(). Set isInDraw to true
2328 * to signal surface_internal_preload() that it has a context. */
2330 /* FIXME: This is just broken. There's no guarantee whatsoever
2331 * that the currently active context, if any, is appropriate for
2332 * reading back the render target. We should probably call
2333 * context_set_current(context) here and then rely on
2334 * context_acquire() doing the right thing. */
2335 device
->isInDraw
= TRUE
;
2337 /* Read the back buffer of the old drawable into the destination texture. */
2338 if (context
->current_rt
->texture_name_srgb
)
2340 surface_internal_preload(context
->current_rt
, SRGB_BOTH
);
2344 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2347 surface_modify_location(context
->current_rt
, SFLAG_INDRAWABLE
, FALSE
);
2349 device
->isInDraw
= oldInDraw
;
2353 context
->draw_buffer_dirty
= TRUE
;
2354 context
->current_rt
= target
;
2355 context_set_render_offscreen(context
, StateTable
, render_offscreen
);
2358 /*****************************************************************************
2361 * Finds a rendering context and drawable matching the device and render
2362 * target for the current thread, activates them and puts them into the
2366 * This: Device to activate the context for
2367 * target: Requested render target
2368 * usage: Prepares the context for blitting, drawing or other actions
2370 *****************************************************************************/
2371 struct wined3d_context
*context_acquire(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*target
)
2373 struct wined3d_context
*current_context
= context_get_current();
2374 struct wined3d_context
*context
;
2376 TRACE("device %p, target %p.\n", device
, target
);
2378 context
= FindContext(device
, target
);
2379 context_setup_target(device
, context
, target
);
2380 context_enter(context
);
2381 if (!context
->valid
) return context
;
2383 if (context
!= current_context
)
2385 if (!context_set_current(context
)) ERR("Failed to activate the new context.\n");
2386 else device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, !context
->last_was_blit
);
2388 if (context
->vshader_const_dirty
)
2390 memset(context
->vshader_const_dirty
, 1,
2391 sizeof(*context
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
2392 device
->highest_dirty_vs_const
= device
->d3d_vshader_constantF
;
2394 if (context
->pshader_const_dirty
)
2396 memset(context
->pshader_const_dirty
, 1,
2397 sizeof(*context
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
2398 device
->highest_dirty_ps_const
= device
->d3d_pshader_constantF
;
2401 else if (context
->restore_ctx
)
2403 if (!pwglMakeCurrent(context
->hdc
, context
->glCtx
))
2405 DWORD err
= GetLastError();
2406 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
2407 context
->hdc
, context
->glCtx
, err
);