2 * Context and render target management in wined3d
4 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
5 * Copyright 2009-2010 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 /* Update base texture states array */
118 if (surface
->container
.type
== WINED3D_CONTAINER_TEXTURE
)
120 IWineD3DBaseTextureImpl
*texture
= surface
->container
.u
.texture
;
121 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
122 BOOL update_minfilter
= FALSE
;
123 BOOL update_magfilter
= FALSE
;
124 struct gl_texture
*gl_tex
;
128 case SFLAG_INTEXTURE
:
129 case SFLAG_INSRGBTEX
:
130 gl_tex
= basetexture_get_gl_texture(texture
, location
== SFLAG_INSRGBTEX
);
134 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
135 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*)texture
);
139 if (gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] != WINED3DTEXF_POINT
140 || gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] != WINED3DTEXF_NONE
)
142 gl_tex
->states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
;
143 gl_tex
->states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_NONE
;
144 update_minfilter
= TRUE
;
147 if (gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] != WINED3DTEXF_POINT
)
149 gl_tex
->states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_POINT
;
150 update_magfilter
= TRUE
;
153 if (texture
->baseTexture
.bindCount
)
155 WARN("Render targets should not be bound to a sampler\n");
156 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_SAMPLER(texture
->baseTexture
.sampler
));
159 if (update_minfilter
|| update_magfilter
)
161 GLenum target
, bind_target
;
164 target
= surface
->texture_target
;
165 if (target
== GL_TEXTURE_2D
)
167 bind_target
= GL_TEXTURE_2D
;
168 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
170 else if (target
== GL_TEXTURE_RECTANGLE_ARB
)
172 bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
173 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_binding
);
177 bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
178 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARB
, &old_binding
);
181 glBindTexture(bind_target
, gl_tex
->name
);
182 if (update_minfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
183 if (update_magfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
184 glBindTexture(bind_target
, old_binding
);
187 checkGLcall("apply_attachment_filter_states()");
191 /* GL locking is done by the caller */
192 void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
193 GLenum fbo_target
, IWineD3DSurfaceImpl
*depth_stencil
, BOOL use_render_buffer
)
195 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
197 TRACE("Attach depth stencil %p\n", depth_stencil
);
201 DWORD format_flags
= depth_stencil
->resource
.format
->flags
;
203 if (use_render_buffer
&& depth_stencil
->current_renderbuffer
)
205 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
207 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
,
208 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
209 checkGLcall("glFramebufferRenderbuffer()");
212 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
214 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
,
215 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
216 checkGLcall("glFramebufferRenderbuffer()");
221 surface_prepare_texture(depth_stencil
, gl_info
, FALSE
);
222 context_apply_attachment_filter_states(depth_stencil
, SFLAG_INTEXTURE
);
224 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
226 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
227 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
228 depth_stencil
->texture_level
);
229 checkGLcall("glFramebufferTexture2D()");
232 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
234 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
235 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
236 depth_stencil
->texture_level
);
237 checkGLcall("glFramebufferTexture2D()");
241 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
243 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
244 checkGLcall("glFramebufferTexture2D()");
247 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
249 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
250 checkGLcall("glFramebufferTexture2D()");
255 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
256 checkGLcall("glFramebufferTexture2D()");
258 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
259 checkGLcall("glFramebufferTexture2D()");
263 /* GL locking is done by the caller */
264 static void context_attach_surface_fbo(const struct wined3d_context
*context
,
265 GLenum fbo_target
, DWORD idx
, IWineD3DSurfaceImpl
*surface
, DWORD location
)
267 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
269 TRACE("Attach surface %p to %u\n", surface
, idx
);
271 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
277 case SFLAG_INTEXTURE
:
278 case SFLAG_INSRGBTEX
:
279 srgb
= location
== SFLAG_INSRGBTEX
;
280 surface_prepare_texture(surface
, gl_info
, srgb
);
281 context_apply_attachment_filter_states(surface
, location
);
282 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
283 surface
->texture_target
, surface_get_texture_name(surface
, srgb
),
284 surface
->texture_level
);
288 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location
), location
);
291 checkGLcall("glFramebufferTexture2D()");
295 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
296 checkGLcall("glFramebufferTexture2D()");
300 /* GL locking is done by the caller */
301 static void context_check_fbo_status(struct wined3d_context
*context
, GLenum target
)
303 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
306 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
307 if (status
== GL_FRAMEBUFFER_COMPLETE
)
309 TRACE("FBO complete\n");
311 IWineD3DSurfaceImpl
*attachment
;
313 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
315 if (!context
->current_fbo
)
317 ERR("FBO 0 is incomplete, driver bug?\n");
321 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context
->current_fbo
->location
),
322 context
->current_fbo
->location
);
324 /* Dump the FBO attachments */
325 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
327 attachment
= context
->current_fbo
->render_targets
[i
];
330 FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
331 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
332 attachment
->pow2Width
, attachment
->pow2Height
);
335 attachment
= context
->current_fbo
->depth_stencil
;
338 FIXME("\tDepth attachment: (%p) %s %ux%u\n",
339 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
340 attachment
->pow2Width
, attachment
->pow2Height
);
345 static struct fbo_entry
*context_create_fbo_entry(struct wined3d_context
*context
,
346 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
, DWORD location
)
348 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
349 struct fbo_entry
*entry
;
351 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
352 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
353 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
354 entry
->depth_stencil
= depth_stencil
;
355 entry
->location
= location
;
356 entry
->attached
= FALSE
;
362 /* GL locking is done by the caller */
363 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
364 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
,
365 DWORD location
, struct fbo_entry
*entry
)
367 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
369 context_bind_fbo(context
, target
, &entry
->id
);
370 context_clean_fbo_attachments(gl_info
, target
);
372 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
373 entry
->depth_stencil
= depth_stencil
;
374 entry
->location
= location
;
375 entry
->attached
= FALSE
;
378 /* GL locking is done by the caller */
379 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
383 TRACE("Destroy FBO %d\n", entry
->id
);
384 context_destroy_fbo(context
, &entry
->id
);
386 --context
->fbo_entry_count
;
387 list_remove(&entry
->entry
);
388 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
389 HeapFree(GetProcessHeap(), 0, entry
);
393 /* GL locking is done by the caller */
394 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
395 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
, DWORD location
)
397 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
398 struct fbo_entry
*entry
;
400 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
402 if (!memcmp(entry
->render_targets
,
403 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
404 && entry
->depth_stencil
== depth_stencil
&& entry
->location
== location
)
406 list_remove(&entry
->entry
);
407 list_add_head(&context
->fbo_list
, &entry
->entry
);
412 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
414 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, location
);
415 list_add_head(&context
->fbo_list
, &entry
->entry
);
416 ++context
->fbo_entry_count
;
420 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
421 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
, entry
);
422 list_remove(&entry
->entry
);
423 list_add_head(&context
->fbo_list
, &entry
->entry
);
429 /* GL locking is done by the caller */
430 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
432 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
435 context_bind_fbo(context
, target
, &entry
->id
);
437 if (!entry
->attached
)
439 /* Apply render targets */
440 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
442 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->location
);
445 /* Apply depth targets */
446 if (entry
->depth_stencil
)
448 surface_set_compatible_renderbuffer(entry
->depth_stencil
,
449 entry
->render_targets
[0]->pow2Width
, entry
->render_targets
[0]->pow2Height
);
451 context_attach_depth_stencil_fbo(context
, target
, entry
->depth_stencil
, TRUE
);
453 entry
->attached
= TRUE
;
457 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
459 if (entry
->render_targets
[i
])
460 context_apply_attachment_filter_states(entry
->render_targets
[i
], entry
->location
);
462 if (entry
->depth_stencil
)
463 context_apply_attachment_filter_states(entry
->depth_stencil
, SFLAG_INTEXTURE
);
467 /* GL locking is done by the caller */
468 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
469 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
, DWORD location
)
471 struct fbo_entry
*entry
, *entry2
;
473 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
475 context_destroy_fbo_entry(context
, entry
);
478 if (context
->rebind_fbo
)
480 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
481 context
->rebind_fbo
= FALSE
;
486 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
);
487 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
491 context
->current_fbo
= NULL
;
492 context_bind_fbo(context
, target
, NULL
);
495 context_check_fbo_status(context
, target
);
498 /* GL locking is done by the caller */
499 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
500 IWineD3DSurfaceImpl
*render_target
, IWineD3DSurfaceImpl
*depth_stencil
, DWORD location
)
502 if (location
!= SFLAG_INDRAWABLE
|| surface_is_offscreen(render_target
))
504 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
505 context
->blit_targets
[0] = render_target
;
506 if (clear_size
) memset(&context
->blit_targets
[1], 0, clear_size
);
507 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
);
511 context_apply_fbo_state(context
, target
, NULL
, NULL
, location
);
515 /* Context activation is done by the caller. */
516 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
518 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
520 if (context
->free_occlusion_query_count
)
522 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
526 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
529 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
530 checkGLcall("glGenQueriesARB");
533 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
537 WARN("Occlusion queries not supported, not allocating query id.\n");
542 query
->context
= context
;
543 list_add_head(&context
->occlusion_queries
, &query
->entry
);
546 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
548 struct wined3d_context
*context
= query
->context
;
550 list_remove(&query
->entry
);
551 query
->context
= NULL
;
553 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
555 UINT new_size
= context
->free_occlusion_query_size
<< 1;
556 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
557 new_size
* sizeof(*context
->free_occlusion_queries
));
561 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
565 context
->free_occlusion_query_size
= new_size
;
566 context
->free_occlusion_queries
= new_data
;
569 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
572 /* Context activation is done by the caller. */
573 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
575 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
577 if (context
->free_event_query_count
)
579 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
583 if (gl_info
->supported
[ARB_SYNC
])
585 /* Using ARB_sync, not much to do here. */
586 query
->object
.sync
= NULL
;
587 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
589 else if (gl_info
->supported
[APPLE_FENCE
])
592 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
593 checkGLcall("glGenFencesAPPLE");
596 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
598 else if(gl_info
->supported
[NV_FENCE
])
601 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
602 checkGLcall("glGenFencesNV");
605 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
609 WARN("Event queries not supported, not allocating query id.\n");
610 query
->object
.id
= 0;
614 query
->context
= context
;
615 list_add_head(&context
->event_queries
, &query
->entry
);
618 void context_free_event_query(struct wined3d_event_query
*query
)
620 struct wined3d_context
*context
= query
->context
;
622 list_remove(&query
->entry
);
623 query
->context
= NULL
;
625 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
627 UINT new_size
= context
->free_event_query_size
<< 1;
628 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
629 new_size
* sizeof(*context
->free_event_queries
));
633 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
637 context
->free_event_query_size
= new_size
;
638 context
->free_event_queries
= new_data
;
641 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
644 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
646 static void context_enum_surface_fbo_entries(IWineD3DDeviceImpl
*device
,
647 IWineD3DSurfaceImpl
*surface
, context_fbo_entry_func_t
*callback
)
651 for (i
= 0; i
< device
->numContexts
; ++i
)
653 struct wined3d_context
*context
= device
->contexts
[i
];
654 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
655 struct fbo_entry
*entry
, *entry2
;
657 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
659 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
663 if (entry
->depth_stencil
== surface
)
665 callback(context
, entry
);
669 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
671 if (entry
->render_targets
[j
] == surface
)
673 callback(context
, entry
);
681 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
683 list_remove(&entry
->entry
);
684 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
687 void context_resource_released(struct IWineD3DDeviceImpl
*device
,
688 struct wined3d_resource
*resource
, WINED3DRESOURCETYPE type
)
690 if (!device
->d3d_initialized
) return;
694 case WINED3DRTYPE_SURFACE
:
695 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
696 context_queue_fbo_entry_destruction
);
704 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
706 entry
->attached
= FALSE
;
709 void context_resource_unloaded(struct IWineD3DDeviceImpl
*device
,
710 struct wined3d_resource
*resource
, WINED3DRESOURCETYPE type
)
714 case WINED3DRTYPE_SURFACE
:
715 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
716 context_detach_fbo_entry
);
724 void context_surface_update(struct wined3d_context
*context
, IWineD3DSurfaceImpl
*surface
)
726 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
727 struct fbo_entry
*entry
= context
->current_fbo
;
730 if (!entry
|| context
->rebind_fbo
) return;
732 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
734 if (surface
== entry
->render_targets
[i
])
736 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
737 context
->rebind_fbo
= TRUE
;
742 if (surface
== entry
->depth_stencil
)
744 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
745 context
->rebind_fbo
= TRUE
;
749 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
751 int current
= GetPixelFormat(dc
);
753 if (current
== format
) return TRUE
;
757 if (!SetPixelFormat(dc
, format
, NULL
))
759 ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
760 format
, dc
, GetLastError());
766 /* By default WGL doesn't allow pixel format adjustments but we need it
767 * here. For this reason there's a Wine specific wglSetPixelFormat()
768 * which allows us to set the pixel format multiple times. Only use it
769 * when really needed. */
770 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
772 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
, NULL
)))
774 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
781 /* OpenGL doesn't allow pixel format adjustments. Print an error and
782 * continue using the old format. There's a big chance that the old
783 * format works although with a performance hit and perhaps rendering
785 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
786 format
, dc
, current
);
790 static void context_update_window(struct wined3d_context
*context
)
792 TRACE("Updating context %p window from %p to %p.\n",
793 context
, context
->win_handle
, context
->swapchain
->win_handle
);
797 if (!ReleaseDC(context
->win_handle
, context
->hdc
))
799 ERR("Failed to release device context %p, last error %#x.\n",
800 context
->hdc
, GetLastError());
803 else context
->valid
= 1;
805 context
->win_handle
= context
->swapchain
->win_handle
;
807 if (!(context
->hdc
= GetDC(context
->win_handle
)))
809 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
813 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
815 ERR("Failed to set pixel format %d on device context %p.\n",
816 context
->pixel_format
, context
->hdc
);
820 if (!pwglMakeCurrent(context
->hdc
, context
->glCtx
))
822 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
823 context
->glCtx
, context
->hdc
, GetLastError());
833 /* Do not call while under the GL lock. */
834 static void context_validate(struct wined3d_context
*context
)
836 HWND wnd
= WindowFromDC(context
->hdc
);
838 if (wnd
!= context
->win_handle
)
840 WARN("DC %p belongs to window %p instead of %p.\n",
841 context
->hdc
, wnd
, context
->win_handle
);
845 if (context
->win_handle
!= context
->swapchain
->win_handle
)
846 context_update_window(context
);
849 /* Do not call while under the GL lock. */
850 static void context_destroy_gl_resources(struct wined3d_context
*context
)
852 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
853 struct wined3d_occlusion_query
*occlusion_query
;
854 struct wined3d_event_query
*event_query
;
855 struct fbo_entry
*entry
, *entry2
;
860 restore_ctx
= pwglGetCurrentContext();
861 restore_dc
= pwglGetCurrentDC();
863 context_validate(context
);
864 if (context
->valid
&& restore_ctx
!= context
->glCtx
) pwglMakeCurrent(context
->hdc
, context
->glCtx
);
865 else restore_ctx
= NULL
;
869 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
871 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
872 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
873 occlusion_query
->context
= NULL
;
876 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
880 if (gl_info
->supported
[ARB_SYNC
])
882 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
884 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
885 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
887 event_query
->context
= NULL
;
890 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
892 if (!context
->valid
) entry
->id
= 0;
893 context_destroy_fbo_entry(context
, entry
);
896 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
898 if (!context
->valid
) entry
->id
= 0;
899 context_destroy_fbo_entry(context
, entry
);
904 if (context
->dst_fbo
)
906 TRACE("Destroy dst FBO %d\n", context
->dst_fbo
);
907 context_destroy_fbo(context
, &context
->dst_fbo
);
909 if (context
->dummy_arbfp_prog
)
911 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
914 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
915 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
917 if (gl_info
->supported
[ARB_SYNC
])
919 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
921 else if (gl_info
->supported
[APPLE_FENCE
])
923 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
925 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
928 else if (gl_info
->supported
[NV_FENCE
])
930 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
932 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
936 checkGLcall("context cleanup");
941 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
942 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
946 if (!pwglMakeCurrent(restore_dc
, restore_ctx
))
948 DWORD err
= GetLastError();
949 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
950 restore_ctx
, restore_dc
, err
);
953 else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL
, NULL
))
955 ERR("Failed to disable GL context.\n");
958 ReleaseDC(context
->win_handle
, context
->hdc
);
960 if (!pwglDeleteContext(context
->glCtx
))
962 DWORD err
= GetLastError();
963 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
967 DWORD
context_get_tls_idx(void)
969 return wined3d_context_tls_idx
;
972 void context_set_tls_idx(DWORD idx
)
974 wined3d_context_tls_idx
= idx
;
977 struct wined3d_context
*context_get_current(void)
979 return TlsGetValue(wined3d_context_tls_idx
);
982 /* Do not call while under the GL lock. */
983 BOOL
context_set_current(struct wined3d_context
*ctx
)
985 struct wined3d_context
*old
= context_get_current();
989 TRACE("Already using D3D context %p.\n", ctx
);
997 TRACE("Switching away from destroyed context %p.\n", old
);
998 context_destroy_gl_resources(old
);
999 HeapFree(GetProcessHeap(), 0, old
);
1009 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1010 if (!pwglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
1012 DWORD err
= GetLastError();
1013 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
1014 ctx
->glCtx
, ctx
->hdc
, err
);
1015 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1020 else if(pwglGetCurrentContext())
1022 TRACE("Clearing current D3D context.\n");
1023 if (!pwglMakeCurrent(NULL
, NULL
))
1025 DWORD err
= GetLastError();
1026 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1027 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1032 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1035 void context_release(struct wined3d_context
*context
)
1037 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1041 if (!context
->level
)
1042 WARN("Context %p is not active.\n", context
);
1043 else if (context
!= context_get_current())
1044 WARN("Context %p is not the current context.\n", context
);
1047 if (!--context
->level
&& context
->restore_ctx
)
1049 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1050 if (!pwglMakeCurrent(context
->restore_dc
, context
->restore_ctx
))
1052 DWORD err
= GetLastError();
1053 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1054 context
->restore_ctx
, context
->restore_dc
, err
);
1056 context
->restore_ctx
= NULL
;
1057 context
->restore_dc
= NULL
;
1061 static void context_enter(struct wined3d_context
*context
)
1063 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1065 if (!context
->level
++)
1067 const struct wined3d_context
*current_context
= context_get_current();
1068 HGLRC current_gl
= pwglGetCurrentContext();
1070 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1072 TRACE("Another GL context (%p on device context %p) is already current.\n",
1073 current_gl
, pwglGetCurrentDC());
1074 context
->restore_ctx
= current_gl
;
1075 context
->restore_dc
= pwglGetCurrentDC();
1080 /*****************************************************************************
1081 * Context_MarkStateDirty
1083 * Marks a state in a context dirty. Only one context, opposed to
1084 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
1088 * context: Context to mark the state dirty in
1089 * state: State to mark dirty
1090 * StateTable: Pointer to the state table in use(for state grouping)
1092 *****************************************************************************/
1093 static void Context_MarkStateDirty(struct wined3d_context
*context
, DWORD state
, const struct StateEntry
*StateTable
)
1095 DWORD rep
= StateTable
[state
].representative
;
1099 if (isStateDirty(context
, rep
)) return;
1101 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1102 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1103 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1104 context
->isStateDirty
[idx
] |= (1 << shift
);
1107 /* This function takes care of WineD3D pixel format selection. */
1108 static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl
*This
, HDC hdc
,
1109 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1110 BOOL auxBuffers
, int numSamples
, BOOL findCompatible
)
1113 unsigned int matchtry
;
1114 short redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1115 short depthBits
=0, stencilBits
=0;
1125 /* First, try without alpha match buffers. MacOS supports aux buffers only
1126 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
1127 * Then try without aux buffers - this is the most common cause for not
1128 * finding a pixel format. Also some drivers(the open source ones)
1129 * only offer 32 bit ARB pixel formats. First try without an exact alpha
1130 * match, then try without an exact alpha and color match.
1132 { TRUE
, TRUE
, TRUE
},
1133 { TRUE
, FALSE
, TRUE
},
1134 { FALSE
, TRUE
, TRUE
},
1135 { FALSE
, FALSE
, TRUE
},
1136 { TRUE
, FALSE
, FALSE
},
1137 { FALSE
, FALSE
, FALSE
},
1141 int nCfgs
= This
->adapter
->nCfgs
;
1143 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, findCompatible=%d\n",
1144 debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1145 auxBuffers
, numSamples
, findCompatible
);
1147 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1149 ERR("Unable to get color bits for format %s (%#x)!\n",
1150 debug_d3dformat(color_format
->id
), color_format
->id
);
1154 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1156 for(matchtry
= 0; matchtry
< (sizeof(matches
) / sizeof(matches
[0])) && !iPixelFormat
; matchtry
++) {
1157 for(i
=0; i
<nCfgs
; i
++) {
1158 BOOL exactDepthMatch
= TRUE
;
1159 WineD3D_PixelFormat
*cfg
= &This
->adapter
->cfgs
[i
];
1161 /* For now only accept RGBA formats. Perhaps some day we will
1162 * allow floating point formats for pbuffers. */
1163 if(cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1166 /* In window mode we need a window drawable format and double buffering. */
1167 if(!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1170 /* We like to have aux buffers in backbuffer mode */
1171 if(auxBuffers
&& !cfg
->auxBuffers
&& matches
[matchtry
].require_aux
)
1174 if(matches
[matchtry
].exact_color
) {
1175 if(cfg
->redSize
!= redBits
)
1177 if(cfg
->greenSize
!= greenBits
)
1179 if(cfg
->blueSize
!= blueBits
)
1182 if(cfg
->redSize
< redBits
)
1184 if(cfg
->greenSize
< greenBits
)
1186 if(cfg
->blueSize
< blueBits
)
1189 if(matches
[matchtry
].exact_alpha
) {
1190 if(cfg
->alphaSize
!= alphaBits
)
1193 if(cfg
->alphaSize
< alphaBits
)
1197 /* We try to locate a format which matches our requirements exactly. In case of
1198 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1199 if(cfg
->depthSize
< depthBits
)
1201 else if(cfg
->depthSize
> depthBits
)
1202 exactDepthMatch
= FALSE
;
1204 /* In all cases make sure the number of stencil bits matches our requirements
1205 * even when we don't need stencil because it could affect performance EXCEPT
1206 * on cards which don't offer depth formats without stencil like the i915 drivers
1208 if(stencilBits
!= cfg
->stencilSize
&& !(This
->adapter
->brokenStencil
&& stencilBits
<= cfg
->stencilSize
))
1211 /* Check multisampling support */
1212 if(cfg
->numSamples
!= numSamples
)
1215 /* When we have passed all the checks then we have found a format which matches our
1216 * requirements. Note that we only check for a limit number of capabilities right now,
1217 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
1218 * can still differ in things like multisampling, stereo, SRGB and other flags.
1221 /* Exit the loop as we have found a format :) */
1222 if(exactDepthMatch
) {
1223 iPixelFormat
= cfg
->iPixelFormat
;
1225 } else if(!iPixelFormat
) {
1226 /* In the end we might end up with a format which doesn't exactly match our depth
1227 * requirements. Accept the first format we found because formats with higher iPixelFormat
1228 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
1229 iPixelFormat
= cfg
->iPixelFormat
;
1234 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1235 if(!iPixelFormat
&& !findCompatible
) {
1236 ERR("Can't find a suitable iPixelFormat\n");
1238 } else if(!iPixelFormat
) {
1239 PIXELFORMATDESCRIPTOR pfd
;
1241 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1242 /* PixelFormat selection */
1243 ZeroMemory(&pfd
, sizeof(pfd
));
1244 pfd
.nSize
= sizeof(pfd
);
1246 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1247 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1248 pfd
.cAlphaBits
= alphaBits
;
1249 pfd
.cColorBits
= colorBits
;
1250 pfd
.cDepthBits
= depthBits
;
1251 pfd
.cStencilBits
= stencilBits
;
1252 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1254 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1256 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1257 ERR("Can't find a suitable iPixelFormat\n");
1262 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1263 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1264 return iPixelFormat
;
1267 /* Do not call while under the GL lock. */
1268 struct wined3d_context
*context_create(IWineD3DSwapChainImpl
*swapchain
,
1269 IWineD3DSurfaceImpl
*target
, const struct wined3d_format
*ds_format
)
1271 IWineD3DDeviceImpl
*device
= swapchain
->device
;
1272 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1273 const struct wined3d_format
*color_format
;
1274 struct wined3d_context
*ret
;
1275 PIXELFORMATDESCRIPTOR pfd
;
1276 BOOL auxBuffers
= FALSE
;
1285 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1287 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1290 ERR("Failed to allocate context memory.\n");
1294 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1296 ERR("Failed to retrieve a device context.\n");
1300 color_format
= target
->resource
.format
;
1302 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1303 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1304 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1308 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1309 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1310 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1311 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1314 /* DirectDraw supports 8bit paletted render targets and these are used by
1315 * old games like Starcraft and C&C. Most modern hardware doesn't support
1316 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1317 * conversion (ab)uses the alpha component for storing the palette index.
1318 * For this reason we require a format with 8bit alpha, so request
1320 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1321 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1323 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
1324 if (swapchain
->presentParms
.MultiSampleType
&& (swapchain
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_DISCARD
))
1326 if (!gl_info
->supported
[ARB_MULTISAMPLE
])
1327 WARN("The application is requesting multisampling without support.\n");
1330 TRACE("Requesting multisample type %#x.\n", swapchain
->presentParms
.MultiSampleType
);
1331 numSamples
= swapchain
->presentParms
.MultiSampleType
;
1335 /* Try to find a pixel format which matches our requirements. */
1336 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format
, ds_format
,
1337 auxBuffers
, numSamples
, FALSE
/* findCompatible */);
1339 /* Try to locate a compatible format if we weren't able to find anything. */
1342 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1343 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format
, ds_format
,
1344 auxBuffers
, 0 /* numSamples */, TRUE
/* findCompatible */);
1347 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1350 ERR("Can't find a suitable pixel format.\n");
1354 DescribePixelFormat(hdc
, pixel_format
, sizeof(pfd
), &pfd
);
1355 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1357 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1361 ctx
= pwglCreateContext(hdc
);
1362 if (device
->numContexts
)
1364 if (!pwglShareLists(device
->contexts
[0]->glCtx
, ctx
))
1366 DWORD err
= GetLastError();
1367 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1368 device
->contexts
[0]->glCtx
, ctx
, err
);
1373 ERR("Failed to create a WGL context\n");
1377 if (!device_context_add(device
, ret
))
1379 ERR("Failed to add the newly created context to the context list\n");
1380 if (!pwglDeleteContext(ctx
))
1382 DWORD err
= GetLastError();
1383 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, err
);
1388 ret
->gl_info
= gl_info
;
1390 /* Mark all states dirty to force a proper initialization of the states
1391 * on the first use of the context. */
1392 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1394 if (device
->StateTable
[state
].representative
)
1395 Context_MarkStateDirty(ret
, state
, device
->StateTable
);
1398 ret
->swapchain
= swapchain
;
1399 ret
->current_rt
= target
;
1400 ret
->tid
= GetCurrentThreadId();
1402 ret
->render_offscreen
= surface_is_offscreen(target
);
1403 ret
->draw_buffer_dirty
= TRUE
;
1407 ret
->win_handle
= swapchain
->win_handle
;
1409 ret
->pixel_format
= pixel_format
;
1411 if (device
->shader_backend
->shader_dirtifyable_constants())
1413 /* Create the dirty constants array and initialize them to dirty */
1414 ret
->vshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1415 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1416 ret
->pshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1417 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1418 memset(ret
->vshader_const_dirty
, 1,
1419 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1420 memset(ret
->pshader_const_dirty
, 1,
1421 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1424 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1425 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1426 if (!ret
->blit_targets
) goto out
;
1428 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1429 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1430 if (!ret
->draw_buffers
) goto out
;
1432 ret
->free_occlusion_query_size
= 4;
1433 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1434 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1435 if (!ret
->free_occlusion_queries
) goto out
;
1437 list_init(&ret
->occlusion_queries
);
1439 ret
->free_event_query_size
= 4;
1440 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1441 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1442 if (!ret
->free_event_queries
) goto out
;
1444 list_init(&ret
->event_queries
);
1446 TRACE("Successfully created new context %p\n", ret
);
1448 list_init(&ret
->fbo_list
);
1449 list_init(&ret
->fbo_destroy_list
);
1453 /* Set up the context defaults */
1454 if (!context_set_current(ret
))
1456 ERR("Cannot activate context to set up defaults\n");
1457 context_release(ret
);
1461 switch (swapchain
->presentParms
.PresentationInterval
)
1463 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1466 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1467 case WINED3DPRESENT_INTERVAL_ONE
:
1470 case WINED3DPRESENT_INTERVAL_TWO
:
1473 case WINED3DPRESENT_INTERVAL_THREE
:
1476 case WINED3DPRESENT_INTERVAL_FOUR
:
1480 FIXME("Unknown presentation interval %08x\n", swapchain
->presentParms
.PresentationInterval
);
1484 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1486 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1487 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1488 swap_interval
, ret
, GetLastError());
1493 glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1495 TRACE("Setting up the screen\n");
1496 /* Clear the screen */
1497 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
1498 checkGLcall("glClearColor");
1501 glClearStencil(0xffff);
1503 checkGLcall("glClear");
1505 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1506 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1508 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1509 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1511 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1512 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1514 glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1515 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1516 glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1517 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1519 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
1521 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1522 * and textures in DIB sections(due to the memory protection).
1524 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1525 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1527 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1529 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1530 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1531 * GL_VERTEX_BLEND_ARB isn't enabled too
1533 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1534 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1536 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1538 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1539 * the previous texture where to source the offset from is always unit - 1.
1541 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1543 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1544 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1545 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1548 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1550 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1551 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1552 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1553 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1556 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1557 * program and the dummy program is destroyed when the context is destroyed.
1559 const char *dummy_program
=
1561 "MOV result.color, fragment.color.primary;\n"
1563 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1564 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1565 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1568 for (s
= 0; s
< gl_info
->limits
.point_sprite_units
; ++s
)
1570 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1571 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1572 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1575 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1577 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1579 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1581 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1586 device
->frag_pipe
->enable_extension(TRUE
);
1588 TRACE("Created context %p.\n", ret
);
1593 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1594 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1595 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1596 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1597 HeapFree(GetProcessHeap(), 0, ret
->pshader_const_dirty
);
1598 HeapFree(GetProcessHeap(), 0, ret
->vshader_const_dirty
);
1599 HeapFree(GetProcessHeap(), 0, ret
);
1603 /* Do not call while under the GL lock. */
1604 void context_destroy(IWineD3DDeviceImpl
*This
, struct wined3d_context
*context
)
1608 TRACE("Destroying ctx %p\n", context
);
1610 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1612 context_destroy_gl_resources(context
);
1613 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1618 context
->destroyed
= 1;
1622 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1623 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1624 HeapFree(GetProcessHeap(), 0, context
->vshader_const_dirty
);
1625 HeapFree(GetProcessHeap(), 0, context
->pshader_const_dirty
);
1626 device_context_remove(This
, context
);
1627 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1630 /* GL locking is done by the caller */
1631 static inline void set_blit_dimension(UINT width
, UINT height
) {
1632 glMatrixMode(GL_PROJECTION
);
1633 checkGLcall("glMatrixMode(GL_PROJECTION)");
1635 checkGLcall("glLoadIdentity()");
1636 glOrtho(0, width
, 0, height
, 0.0, -1.0);
1637 checkGLcall("glOrtho");
1638 glViewport(0, 0, width
, height
);
1639 checkGLcall("glViewport");
1642 /*****************************************************************************
1645 * Sets up a context for DirectDraw blitting.
1646 * All texture units are disabled, texture unit 0 is set as current unit
1647 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1648 * color writing enabled for all channels
1649 * register combiners disabled, shaders disabled
1650 * world matrix is set to identity, texture matrix 0 too
1651 * projection matrix is setup for drawing screen coordinates
1654 * This: Device to activate the context for
1655 * context: Context to setup
1657 *****************************************************************************/
1658 /* Context activation is done by the caller. */
1659 static void SetupForBlit(IWineD3DDeviceImpl
*This
, struct wined3d_context
*context
)
1662 const struct StateEntry
*StateTable
= This
->StateTable
;
1663 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1664 UINT width
= context
->current_rt
->currentDesc
.Width
;
1665 UINT height
= context
->current_rt
->currentDesc
.Height
;
1668 TRACE("Setting up context %p for blitting\n", context
);
1669 if(context
->last_was_blit
) {
1670 if(context
->blit_w
!= width
|| context
->blit_h
!= height
) {
1672 set_blit_dimension(width
, height
);
1674 context
->blit_w
= width
; context
->blit_h
= height
;
1675 /* No need to dirtify here, the states are still dirtified because they weren't
1676 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1680 TRACE("Context is already set up for blitting, nothing to do\n");
1683 context
->last_was_blit
= TRUE
;
1685 /* TODO: Use a display list */
1687 /* Disable shaders */
1689 This
->shader_backend
->shader_select(context
, FALSE
, FALSE
);
1692 Context_MarkStateDirty(context
, STATE_VSHADER
, StateTable
);
1693 Context_MarkStateDirty(context
, STATE_PIXELSHADER
, StateTable
);
1695 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1696 * helper functions in between gl calls. This function is full of Context_MarkStateDirty
1697 * which can safely be called from here, we only lock once instead locking/unlocking
1698 * after each GL call.
1702 /* Disable all textures. The caller can then bind a texture it wants to blit
1705 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1706 * function texture unit. No need to care for higher samplers
1708 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1710 sampler
= This
->rev_tex_unit_map
[i
];
1711 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1712 checkGLcall("glActiveTextureARB");
1714 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1716 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1717 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1719 glDisable(GL_TEXTURE_3D
);
1720 checkGLcall("glDisable GL_TEXTURE_3D");
1721 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1723 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1724 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1726 glDisable(GL_TEXTURE_2D
);
1727 checkGLcall("glDisable GL_TEXTURE_2D");
1729 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1730 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1732 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1734 if (sampler
< MAX_TEXTURES
) {
1735 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1737 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
1740 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
1741 checkGLcall("glActiveTextureARB");
1743 sampler
= This
->rev_tex_unit_map
[0];
1745 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1747 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1748 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1750 glDisable(GL_TEXTURE_3D
);
1751 checkGLcall("glDisable GL_TEXTURE_3D");
1752 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1754 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1755 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1757 glDisable(GL_TEXTURE_2D
);
1758 checkGLcall("glDisable GL_TEXTURE_2D");
1760 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1762 glMatrixMode(GL_TEXTURE
);
1763 checkGLcall("glMatrixMode(GL_TEXTURE)");
1765 checkGLcall("glLoadIdentity()");
1767 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1769 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1770 GL_TEXTURE_LOD_BIAS_EXT
,
1772 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1775 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1777 if (sampler
< MAX_TEXTURES
) {
1778 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
+ sampler
), StateTable
);
1779 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1781 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
1784 /* Other misc states */
1785 glDisable(GL_ALPHA_TEST
);
1786 checkGLcall("glDisable(GL_ALPHA_TEST)");
1787 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
), StateTable
);
1788 glDisable(GL_LIGHTING
);
1789 checkGLcall("glDisable GL_LIGHTING");
1790 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_LIGHTING
), StateTable
);
1791 glDisable(GL_DEPTH_TEST
);
1792 checkGLcall("glDisable GL_DEPTH_TEST");
1793 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ZENABLE
), StateTable
);
1794 glDisableWINE(GL_FOG
);
1795 checkGLcall("glDisable GL_FOG");
1796 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
), StateTable
);
1797 glDisable(GL_BLEND
);
1798 checkGLcall("glDisable GL_BLEND");
1799 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1800 glDisable(GL_CULL_FACE
);
1801 checkGLcall("glDisable GL_CULL_FACE");
1802 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CULLMODE
), StateTable
);
1803 glDisable(GL_STENCIL_TEST
);
1804 checkGLcall("glDisable GL_STENCIL_TEST");
1805 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
), StateTable
);
1806 glDisable(GL_SCISSOR_TEST
);
1807 checkGLcall("glDisable GL_SCISSOR_TEST");
1808 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
1809 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1811 glDisable(GL_POINT_SPRITE_ARB
);
1812 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1813 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
), StateTable
);
1815 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1816 checkGLcall("glColorMask");
1817 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
), StateTable
);
1818 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1
), StateTable
);
1819 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2
), StateTable
);
1820 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3
), StateTable
);
1821 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1823 glDisable(GL_COLOR_SUM_EXT
);
1824 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
1825 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1828 /* Setup transforms */
1829 glMatrixMode(GL_MODELVIEW
);
1830 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1832 checkGLcall("glLoadIdentity()");
1833 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable
);
1835 context
->last_was_rhw
= TRUE
;
1836 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
); /* because of last_was_rhw = TRUE */
1838 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1839 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1840 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1841 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1842 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1843 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1844 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
1846 set_blit_dimension(width
, height
);
1850 context
->blit_w
= width
; context
->blit_h
= height
;
1851 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
1852 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1854 This
->frag_pipe
->enable_extension(FALSE
);
1857 /* Do not call while under the GL lock. */
1858 static struct wined3d_context
*FindContext(IWineD3DDeviceImpl
*This
, IWineD3DSurfaceImpl
*target
)
1860 struct wined3d_context
*current_context
= context_get_current();
1861 struct wined3d_context
*context
;
1863 if (current_context
&& current_context
->destroyed
) current_context
= NULL
;
1868 && current_context
->current_rt
1869 && current_context
->swapchain
->device
== This
)
1871 target
= current_context
->current_rt
;
1875 IWineD3DSwapChainImpl
*swapchain
= This
->swapchains
[0];
1876 if (swapchain
->back_buffers
) target
= swapchain
->back_buffers
[0];
1877 else target
= swapchain
->front_buffer
;
1881 if (current_context
&& current_context
->current_rt
== target
)
1883 context_validate(current_context
);
1884 return current_context
;
1887 if (target
->container
.type
== WINED3D_CONTAINER_SWAPCHAIN
)
1889 TRACE("Rendering onscreen\n");
1891 context
= swapchain_get_context(target
->container
.u
.swapchain
);
1895 TRACE("Rendering offscreen\n");
1897 /* Stay with the current context if possible. Otherwise use the
1898 * context for the primary swapchain. */
1899 if (current_context
&& current_context
->swapchain
->device
== This
)
1900 context
= current_context
;
1902 context
= swapchain_get_context(This
->swapchains
[0]);
1905 context_validate(context
);
1910 /* Context activation is done by the caller. */
1911 static void context_apply_draw_buffers(struct wined3d_context
*context
, UINT rt_count
, IWineD3DSurfaceImpl
**rts
)
1913 if (!surface_is_offscreen(rts
[0]))
1916 glDrawBuffer(surface_get_gl_buffer(rts
[0]));
1917 checkGLcall("glDrawBuffer()");
1923 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1925 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1928 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
1930 if (i
< rt_count
&& rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
1931 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
1933 context
->draw_buffers
[i
] = GL_NONE
;
1936 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1938 GL_EXTCALL(glDrawBuffersARB(gl_info
->limits
.buffers
, context
->draw_buffers
));
1939 checkGLcall("glDrawBuffers()");
1943 glDrawBuffer(context
->draw_buffers
[0]);
1944 checkGLcall("glDrawBuffer()");
1949 glDrawBuffer(rts
[0]->resource
.device
->offscreenBuffer
);
1950 checkGLcall("glDrawBuffer()");
1956 /* GL locking is done by the caller. */
1957 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
1959 glDrawBuffer(buffer
);
1960 checkGLcall("glDrawBuffer()");
1961 context
->draw_buffer_dirty
= TRUE
;
1964 static inline void context_set_render_offscreen(struct wined3d_context
*context
, const struct StateEntry
*StateTable
,
1967 if (context
->render_offscreen
== offscreen
) return;
1969 Context_MarkStateDirty(context
, STATE_POINTSPRITECOORDORIGIN
, StateTable
);
1970 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1971 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
);
1972 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
1973 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
1974 Context_MarkStateDirty(context
, STATE_FRONTFACE
, StateTable
);
1975 context
->render_offscreen
= offscreen
;
1978 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
1979 const struct wined3d_format
*required
)
1981 short existing_depth
, existing_stencil
, required_depth
, required_stencil
;
1983 if (existing
== required
) return TRUE
;
1984 if ((existing
->flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
1986 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
1987 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
1989 if(existing_depth
< required_depth
) return FALSE
;
1990 /* If stencil bits are used the exact amount is required - otherwise wrapping
1991 * won't work correctly */
1992 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
1995 /* The caller provides a context */
1996 static void context_validate_onscreen_formats(IWineD3DDeviceImpl
*device
,
1997 struct wined3d_context
*context
, IWineD3DSurfaceImpl
*depth_stencil
)
1999 /* Onscreen surfaces are always in a swapchain */
2000 IWineD3DSwapChainImpl
*swapchain
= context
->current_rt
->container
.u
.swapchain
;
2002 if (context
->render_offscreen
|| !depth_stencil
) return;
2003 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->resource
.format
)) return;
2005 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2006 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2008 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2010 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2011 surface_load_location(context
->current_rt
, SFLAG_INTEXTURE
, NULL
);
2012 swapchain
->render_to_fbo
= TRUE
;
2013 context_set_render_offscreen(context
, device
->StateTable
, TRUE
);
2016 /* Context activation is done by the caller. */
2017 void context_apply_blit_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
)
2019 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2021 context_validate_onscreen_formats(device
, context
, NULL
);
2023 if (context
->render_offscreen
)
2025 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2028 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, context
->current_rt
, NULL
, SFLAG_INTEXTURE
);
2034 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
2038 context
->draw_buffer_dirty
= TRUE
;
2041 if (context
->draw_buffer_dirty
)
2043 context_apply_draw_buffers(context
, 1, &context
->current_rt
);
2044 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2045 context
->draw_buffer_dirty
= FALSE
;
2048 SetupForBlit(device
, context
);
2051 static BOOL
context_validate_rt_config(UINT rt_count
,
2052 IWineD3DSurfaceImpl
**rts
, IWineD3DSurfaceImpl
*ds
)
2056 if (ds
) return TRUE
;
2058 for (i
= 0; i
< rt_count
; ++i
)
2060 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2064 WARN("Invalid render target config, need at least one attachment.\n");
2068 /* Context activation is done by the caller. */
2069 BOOL
context_apply_clear_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
,
2070 UINT rt_count
, IWineD3DSurfaceImpl
**rts
, IWineD3DSurfaceImpl
*depth_stencil
)
2072 const struct StateEntry
*state_table
= device
->StateTable
;
2075 if (!context_validate_rt_config(rt_count
, rts
, depth_stencil
))
2079 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2081 context_validate_onscreen_formats(device
, context
, depth_stencil
);
2085 if (surface_is_offscreen(rts
[0]))
2087 for (i
= 0; i
< rt_count
; ++i
)
2089 context
->blit_targets
[i
] = rts
[i
];
2091 while (i
< context
->gl_info
->limits
.buffers
)
2093 context
->blit_targets
[i
] = NULL
;
2096 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, depth_stencil
, SFLAG_INTEXTURE
);
2100 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2106 context_apply_draw_buffers(context
, rt_count
, rts
);
2107 context
->draw_buffer_dirty
= TRUE
;
2109 if (context
->last_was_blit
)
2111 device
->frag_pipe
->enable_extension(TRUE
);
2114 /* Blending and clearing should be orthogonal, but tests on the nvidia
2115 * driver show that disabling blending when clearing improves the clearing
2116 * performance incredibly. */
2118 glDisable(GL_BLEND
);
2119 glEnable(GL_SCISSOR_TEST
);
2120 checkGLcall("glEnable GL_SCISSOR_TEST");
2123 context
->last_was_blit
= FALSE
;
2124 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), state_table
);
2125 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), state_table
);
2126 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, state_table
);
2131 /* Context activation is done by the caller. */
2132 BOOL
context_apply_draw_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
)
2134 const struct StateEntry
*state_table
= device
->StateTable
;
2137 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
2138 device
->render_targets
, device
->depth_stencil
))
2141 /* Preload resources before FBO setup. Texture preload in particular may
2142 * result in changes to the current FBO, due to using e.g. FBO blits for
2143 * updating a resource location. */
2144 IWineD3DDeviceImpl_FindTexUnitMap(device
);
2145 device_preload_textures(device
);
2146 if (isStateDirty(context
, STATE_VDECL
))
2147 device_update_stream_info(device
, context
->gl_info
);
2149 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2151 context_validate_onscreen_formats(device
, context
, device
->depth_stencil
);
2153 if (!context
->render_offscreen
)
2156 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2162 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, device
->render_targets
,
2163 device
->depth_stencil
, SFLAG_INTEXTURE
);
2168 if (context
->draw_buffer_dirty
)
2170 context_apply_draw_buffers(context
, context
->gl_info
->limits
.buffers
, device
->render_targets
);
2171 context
->draw_buffer_dirty
= FALSE
;
2174 if (context
->last_was_blit
)
2176 device
->frag_pipe
->enable_extension(TRUE
);
2180 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2182 DWORD rep
= context
->dirtyArray
[i
];
2183 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2184 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2185 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2186 state_table
[rep
].apply(rep
, device
->stateBlock
, context
);
2189 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2190 context
->last_was_blit
= FALSE
;
2195 static void context_setup_target(IWineD3DDeviceImpl
*device
,
2196 struct wined3d_context
*context
, IWineD3DSurfaceImpl
*target
)
2198 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
2199 const struct StateEntry
*StateTable
= device
->StateTable
;
2201 if (!target
) return;
2202 render_offscreen
= surface_is_offscreen(target
);
2203 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
2205 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2206 * the alpha blend state changes with different render target formats. */
2207 if (!context
->current_rt
)
2209 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2213 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
2214 const struct wined3d_format
*new = target
->resource
.format
;
2216 if (old
->id
!= new->id
)
2218 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2219 if ((old
->alpha_mask
&& !new->alpha_mask
) || (!old
->alpha_mask
&& new->alpha_mask
)
2220 || !(new->flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
2222 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2224 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2225 if ((old
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
2227 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SRGBWRITEENABLE
), StateTable
);
2231 /* When switching away from an offscreen render target, and we're not
2232 * using FBOs, we have to read the drawable into the texture. This is
2233 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2234 * are some things that need care though. PreLoad needs a GL context,
2235 * and FindContext is called before the context is activated. It also
2236 * has to be called with the old rendertarget active, otherwise a
2237 * wrong drawable is read. */
2238 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2239 && old_render_offscreen
&& context
->current_rt
!= target
)
2241 /* Read the back buffer of the old drawable into the destination texture. */
2242 if (context
->current_rt
->texture_name_srgb
)
2244 surface_internal_preload(context
->current_rt
, SRGB_BOTH
);
2248 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2251 surface_modify_location(context
->current_rt
, SFLAG_INDRAWABLE
, FALSE
);
2255 context
->draw_buffer_dirty
= TRUE
;
2256 context
->current_rt
= target
;
2257 context_set_render_offscreen(context
, StateTable
, render_offscreen
);
2260 /* Do not call while under the GL lock. */
2261 struct wined3d_context
*context_acquire(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*target
)
2263 struct wined3d_context
*current_context
= context_get_current();
2264 struct wined3d_context
*context
;
2266 TRACE("device %p, target %p.\n", device
, target
);
2268 context
= FindContext(device
, target
);
2269 context_setup_target(device
, context
, target
);
2270 context_enter(context
);
2271 if (!context
->valid
) return context
;
2273 if (context
!= current_context
)
2275 if (!context_set_current(context
)) ERR("Failed to activate the new context.\n");
2276 else device
->frag_pipe
->enable_extension(!context
->last_was_blit
);
2278 if (context
->vshader_const_dirty
)
2280 memset(context
->vshader_const_dirty
, 1,
2281 sizeof(*context
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
2282 device
->highest_dirty_vs_const
= device
->d3d_vshader_constantF
;
2284 if (context
->pshader_const_dirty
)
2286 memset(context
->pshader_const_dirty
, 1,
2287 sizeof(*context
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
2288 device
->highest_dirty_ps_const
= device
->d3d_pshader_constantF
;
2291 else if (context
->restore_ctx
)
2293 if (!pwglMakeCurrent(context
->hdc
, context
->glCtx
))
2295 DWORD err
= GetLastError();
2296 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
2297 context
->hdc
, context
->glCtx
, err
);