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 #define GLINFO_LOCATION (*gl_info)
33 static DWORD wined3d_context_tls_idx
;
35 /* FBO helper functions */
37 /* GL locking is done by the caller */
38 void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint
*fbo
)
40 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
51 gl_info
->fbo_ops
.glGenFramebuffers(1, fbo
);
52 checkGLcall("glGenFramebuffers()");
53 TRACE("Created FBO %u.\n", *fbo
);
60 case GL_READ_FRAMEBUFFER
:
61 if (context
->fbo_read_binding
== f
) return;
62 context
->fbo_read_binding
= f
;
65 case GL_DRAW_FRAMEBUFFER
:
66 if (context
->fbo_draw_binding
== f
) return;
67 context
->fbo_draw_binding
= f
;
71 if (context
->fbo_read_binding
== f
72 && context
->fbo_draw_binding
== f
) return;
73 context
->fbo_read_binding
= f
;
74 context
->fbo_draw_binding
= f
;
78 FIXME("Unhandled target %#x.\n", target
);
82 gl_info
->fbo_ops
.glBindFramebuffer(target
, f
);
83 checkGLcall("glBindFramebuffer()");
86 /* GL locking is done by the caller */
87 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
)
91 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
93 gl_info
->fbo_ops
.glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
94 checkGLcall("glFramebufferTexture2D()");
96 gl_info
->fbo_ops
.glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
97 checkGLcall("glFramebufferTexture2D()");
99 gl_info
->fbo_ops
.glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
100 checkGLcall("glFramebufferTexture2D()");
103 /* GL locking is done by the caller */
104 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint
*fbo
)
106 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
108 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
109 context_clean_fbo_attachments(gl_info
);
110 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
112 gl_info
->fbo_ops
.glDeleteFramebuffers(1, fbo
);
113 checkGLcall("glDeleteFramebuffers()");
116 /* GL locking is done by the caller */
117 static void context_apply_attachment_filter_states(IWineD3DSurfaceImpl
*surface
)
119 IWineD3DBaseTextureImpl
*texture_impl
;
121 /* Update base texture states array */
122 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface
*)surface
,
123 &IID_IWineD3DBaseTexture
, (void **)&texture_impl
)))
125 IWineD3DDeviceImpl
*device
= surface
->resource
.device
;
126 BOOL update_minfilter
= FALSE
;
127 BOOL update_magfilter
= FALSE
;
129 if (texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MINFILTER
] != WINED3DTEXF_POINT
130 || texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MIPFILTER
] != WINED3DTEXF_NONE
)
132 texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
;
133 texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_NONE
;
134 update_minfilter
= TRUE
;
137 if (texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MAGFILTER
] != WINED3DTEXF_POINT
)
139 texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_POINT
;
140 update_magfilter
= TRUE
;
143 if (texture_impl
->baseTexture
.bindCount
)
145 WARN("Render targets should not be bound to a sampler\n");
146 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_SAMPLER(texture_impl
->baseTexture
.sampler
));
149 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*)texture_impl
);
151 if (update_minfilter
|| update_magfilter
)
153 GLenum target
, bind_target
;
156 target
= surface
->texture_target
;
157 if (target
== GL_TEXTURE_2D
)
159 bind_target
= GL_TEXTURE_2D
;
160 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
162 else if (target
== GL_TEXTURE_RECTANGLE_ARB
)
164 bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
165 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_binding
);
169 bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
170 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARB
, &old_binding
);
173 glBindTexture(bind_target
, surface
->texture_name
);
174 if (update_minfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
175 if (update_magfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
176 glBindTexture(bind_target
, old_binding
);
179 checkGLcall("apply_attachment_filter_states()");
183 /* GL locking is done by the caller */
184 void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
185 GLenum fbo_target
, IWineD3DSurfaceImpl
*depth_stencil
, BOOL use_render_buffer
)
187 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
189 TRACE("Attach depth stencil %p\n", depth_stencil
);
193 DWORD format_flags
= depth_stencil
->resource
.format_desc
->Flags
;
195 if (use_render_buffer
&& depth_stencil
->current_renderbuffer
)
197 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
199 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
,
200 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
201 checkGLcall("glFramebufferRenderbuffer()");
204 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
206 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
,
207 GL_RENDERBUFFER
, depth_stencil
->current_renderbuffer
->id
);
208 checkGLcall("glFramebufferRenderbuffer()");
213 surface_prepare_texture(depth_stencil
, gl_info
, FALSE
);
214 context_apply_attachment_filter_states(depth_stencil
);
216 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
218 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
219 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
220 depth_stencil
->texture_level
);
221 checkGLcall("glFramebufferTexture2D()");
224 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
226 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
227 depth_stencil
->texture_target
, depth_stencil
->texture_name
,
228 depth_stencil
->texture_level
);
229 checkGLcall("glFramebufferTexture2D()");
233 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
235 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
236 checkGLcall("glFramebufferTexture2D()");
239 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
241 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
242 checkGLcall("glFramebufferTexture2D()");
247 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
248 checkGLcall("glFramebufferTexture2D()");
250 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
251 checkGLcall("glFramebufferTexture2D()");
255 /* GL locking is done by the caller */
256 void context_attach_surface_fbo(const struct wined3d_context
*context
,
257 GLenum fbo_target
, DWORD idx
, IWineD3DSurfaceImpl
*surface
)
259 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
261 TRACE("Attach surface %p to %u\n", surface
, idx
);
265 surface_prepare_texture(surface
, gl_info
, FALSE
);
266 context_apply_attachment_filter_states(surface
);
268 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, surface
->texture_target
,
269 surface
->texture_name
, surface
->texture_level
);
270 checkGLcall("glFramebufferTexture2D()");
274 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
275 checkGLcall("glFramebufferTexture2D()");
279 /* GL locking is done by the caller */
280 static void context_check_fbo_status(struct wined3d_context
*context
, GLenum target
)
282 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
285 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
286 if (status
== GL_FRAMEBUFFER_COMPLETE
)
288 TRACE("FBO complete\n");
290 IWineD3DSurfaceImpl
*attachment
;
292 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
294 if (!context
->current_fbo
)
296 ERR("FBO 0 is incomplete, driver bug?\n");
300 /* Dump the FBO attachments */
301 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
303 attachment
= context
->current_fbo
->render_targets
[i
];
306 FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
307 i
, attachment
, debug_d3dformat(attachment
->resource
.format_desc
->format
),
308 attachment
->pow2Width
, attachment
->pow2Height
);
311 attachment
= context
->current_fbo
->depth_stencil
;
314 FIXME("\tDepth attachment: (%p) %s %ux%u\n",
315 attachment
, debug_d3dformat(attachment
->resource
.format_desc
->format
),
316 attachment
->pow2Width
, attachment
->pow2Height
);
321 static struct fbo_entry
*context_create_fbo_entry(struct wined3d_context
*context
,
322 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
)
324 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
325 struct fbo_entry
*entry
;
327 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
328 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
329 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
330 entry
->depth_stencil
= depth_stencil
;
331 entry
->attached
= FALSE
;
337 /* GL locking is done by the caller */
338 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
339 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
,
340 struct fbo_entry
*entry
)
342 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
344 context_bind_fbo(context
, target
, &entry
->id
);
345 context_clean_fbo_attachments(gl_info
);
347 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
348 entry
->depth_stencil
= depth_stencil
;
349 entry
->attached
= FALSE
;
352 /* GL locking is done by the caller */
353 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
357 TRACE("Destroy FBO %d\n", entry
->id
);
358 context_destroy_fbo(context
, &entry
->id
);
360 --context
->fbo_entry_count
;
361 list_remove(&entry
->entry
);
362 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
363 HeapFree(GetProcessHeap(), 0, entry
);
367 /* GL locking is done by the caller */
368 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
369 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
)
371 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
372 struct fbo_entry
*entry
;
374 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
376 if (!memcmp(entry
->render_targets
,
377 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
378 && entry
->depth_stencil
== depth_stencil
)
380 list_remove(&entry
->entry
);
381 list_add_head(&context
->fbo_list
, &entry
->entry
);
386 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
388 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
);
389 list_add_head(&context
->fbo_list
, &entry
->entry
);
390 ++context
->fbo_entry_count
;
394 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
395 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, entry
);
396 list_remove(&entry
->entry
);
397 list_add_head(&context
->fbo_list
, &entry
->entry
);
403 /* GL locking is done by the caller */
404 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
406 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
409 context_bind_fbo(context
, target
, &entry
->id
);
411 if (!entry
->attached
)
413 /* Apply render targets */
414 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
416 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
]);
419 /* Apply depth targets */
420 if (entry
->depth_stencil
)
422 surface_set_compatible_renderbuffer(entry
->depth_stencil
,
423 entry
->render_targets
[0]->pow2Width
, entry
->render_targets
[0]->pow2Height
);
425 context_attach_depth_stencil_fbo(context
, target
, entry
->depth_stencil
, TRUE
);
427 entry
->attached
= TRUE
;
431 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
433 if (entry
->render_targets
[i
])
434 context_apply_attachment_filter_states(entry
->render_targets
[i
]);
436 if (entry
->depth_stencil
)
437 context_apply_attachment_filter_states(entry
->depth_stencil
);
441 /* GL locking is done by the caller */
442 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
443 IWineD3DSurfaceImpl
**render_targets
, IWineD3DSurfaceImpl
*depth_stencil
)
445 struct fbo_entry
*entry
, *entry2
;
447 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
449 context_destroy_fbo_entry(context
, entry
);
452 if (context
->rebind_fbo
)
454 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
455 context
->rebind_fbo
= FALSE
;
460 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
);
461 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
465 context
->current_fbo
= NULL
;
466 context_bind_fbo(context
, target
, NULL
);
469 context_check_fbo_status(context
, target
);
472 /* GL locking is done by the caller */
473 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
474 IWineD3DSurfaceImpl
*render_target
, IWineD3DSurfaceImpl
*depth_stencil
)
476 if (surface_is_offscreen(render_target
))
478 context
->blit_targets
[0] = render_target
;
479 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
);
483 context_apply_fbo_state(context
, target
, NULL
, NULL
);
487 /* Context activation is done by the caller. */
488 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
490 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
492 if (context
->free_occlusion_query_count
)
494 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
498 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
501 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
502 checkGLcall("glGenQueriesARB");
505 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
509 WARN("Occlusion queries not supported, not allocating query id.\n");
514 query
->context
= context
;
515 list_add_head(&context
->occlusion_queries
, &query
->entry
);
518 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
520 struct wined3d_context
*context
= query
->context
;
522 list_remove(&query
->entry
);
523 query
->context
= NULL
;
525 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
527 UINT new_size
= context
->free_occlusion_query_size
<< 1;
528 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
529 new_size
* sizeof(*context
->free_occlusion_queries
));
533 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
537 context
->free_occlusion_query_size
= new_size
;
538 context
->free_occlusion_queries
= new_data
;
541 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
544 /* Context activation is done by the caller. */
545 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
547 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
549 if (context
->free_event_query_count
)
551 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
555 if (gl_info
->supported
[ARB_SYNC
])
557 /* Using ARB_sync, not much to do here. */
558 query
->object
.sync
= NULL
;
559 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
561 else if (gl_info
->supported
[APPLE_FENCE
])
564 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
565 checkGLcall("glGenFencesAPPLE");
568 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
570 else if(gl_info
->supported
[NV_FENCE
])
573 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
574 checkGLcall("glGenFencesNV");
577 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
581 WARN("Event queries not supported, not allocating query id.\n");
582 query
->object
.id
= 0;
586 query
->context
= context
;
587 list_add_head(&context
->event_queries
, &query
->entry
);
590 void context_free_event_query(struct wined3d_event_query
*query
)
592 struct wined3d_context
*context
= query
->context
;
594 list_remove(&query
->entry
);
595 query
->context
= NULL
;
597 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
599 UINT new_size
= context
->free_event_query_size
<< 1;
600 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
601 new_size
* sizeof(*context
->free_event_queries
));
605 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
609 context
->free_event_query_size
= new_size
;
610 context
->free_event_queries
= new_data
;
613 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
616 void context_resource_released(IWineD3DDevice
*iface
, IWineD3DResource
*resource
, WINED3DRESOURCETYPE type
)
618 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
621 if (!This
->d3d_initialized
) return;
625 case WINED3DRTYPE_SURFACE
:
627 for (i
= 0; i
< This
->numContexts
; ++i
)
629 struct wined3d_context
*context
= This
->contexts
[i
];
630 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
631 struct fbo_entry
*entry
, *entry2
;
633 if (context
->current_rt
== (IWineD3DSurfaceImpl
*)resource
) context
->current_rt
= NULL
;
635 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
639 if (entry
->depth_stencil
== (IWineD3DSurfaceImpl
*)resource
)
641 list_remove(&entry
->entry
);
642 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
646 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
648 if (entry
->render_targets
[j
] == (IWineD3DSurfaceImpl
*)resource
)
650 list_remove(&entry
->entry
);
651 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
666 void context_surface_update(struct wined3d_context
*context
, IWineD3DSurfaceImpl
*surface
)
668 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
669 struct fbo_entry
*entry
= context
->current_fbo
;
672 if (!entry
|| context
->rebind_fbo
) return;
674 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
676 if (surface
== entry
->render_targets
[i
])
678 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
679 context
->rebind_fbo
= TRUE
;
684 if (surface
== entry
->depth_stencil
)
686 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
687 context
->rebind_fbo
= TRUE
;
691 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
693 int current
= GetPixelFormat(dc
);
695 if (current
== format
) return TRUE
;
699 if (!SetPixelFormat(dc
, format
, NULL
))
701 ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
702 format
, dc
, GetLastError());
708 /* By default WGL doesn't allow pixel format adjustments but we need it
709 * here. For this reason there's a Wine specific wglSetPixelFormat()
710 * which allows us to set the pixel format multiple times. Only use it
711 * when really needed. */
712 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
714 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
, NULL
)))
716 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
723 /* OpenGL doesn't allow pixel format adjustments. Print an error and
724 * continue using the old format. There's a big chance that the old
725 * format works although with a performance hit and perhaps rendering
727 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
728 format
, dc
, current
);
732 static void context_update_window(struct wined3d_context
*context
)
734 TRACE("Updating context %p window from %p to %p.\n",
735 context
, context
->win_handle
, context
->swapchain
->win_handle
);
739 if (!ReleaseDC(context
->win_handle
, context
->hdc
))
741 ERR("Failed to release device context %p, last error %#x.\n",
742 context
->hdc
, GetLastError());
745 else context
->valid
= 1;
747 context
->win_handle
= context
->swapchain
->win_handle
;
749 if (!(context
->hdc
= GetDC(context
->win_handle
)))
751 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
755 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
757 ERR("Failed to set pixel format %d on device context %p.\n",
758 context
->pixel_format
, context
->hdc
);
762 if (!pwglMakeCurrent(context
->hdc
, context
->glCtx
))
764 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
765 context
->glCtx
, context
->hdc
, GetLastError());
775 static void context_validate(struct wined3d_context
*context
)
777 HWND wnd
= WindowFromDC(context
->hdc
);
779 if (wnd
!= context
->win_handle
)
781 WARN("DC %p belongs to window %p instead of %p.\n",
782 context
->hdc
, wnd
, context
->win_handle
);
786 if (context
->win_handle
!= context
->swapchain
->win_handle
)
787 context_update_window(context
);
790 static void context_destroy_gl_resources(struct wined3d_context
*context
)
792 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
793 struct wined3d_occlusion_query
*occlusion_query
;
794 struct wined3d_event_query
*event_query
;
795 struct fbo_entry
*entry
, *entry2
;
800 restore_ctx
= pwglGetCurrentContext();
801 restore_dc
= pwglGetCurrentDC();
803 context_validate(context
);
804 if (context
->valid
&& restore_ctx
!= context
->glCtx
) pwglMakeCurrent(context
->hdc
, context
->glCtx
);
805 else restore_ctx
= NULL
;
809 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
811 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
812 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
813 occlusion_query
->context
= NULL
;
816 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
820 if (gl_info
->supported
[ARB_SYNC
])
822 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
824 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
825 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
827 event_query
->context
= NULL
;
830 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
832 if (!context
->valid
) entry
->id
= 0;
833 context_destroy_fbo_entry(context
, entry
);
836 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
838 if (!context
->valid
) entry
->id
= 0;
839 context_destroy_fbo_entry(context
, entry
);
844 if (context
->src_fbo
)
846 TRACE("Destroy src FBO %d\n", context
->src_fbo
);
847 context_destroy_fbo(context
, &context
->src_fbo
);
849 if (context
->dst_fbo
)
851 TRACE("Destroy dst FBO %d\n", context
->dst_fbo
);
852 context_destroy_fbo(context
, &context
->dst_fbo
);
854 if (context
->dummy_arbfp_prog
)
856 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
859 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
860 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
862 if (gl_info
->supported
[ARB_SYNC
])
864 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
866 else if (gl_info
->supported
[APPLE_FENCE
])
868 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
870 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
873 else if (gl_info
->supported
[NV_FENCE
])
875 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
877 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
881 checkGLcall("context cleanup");
886 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
887 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
891 if (!pwglMakeCurrent(restore_dc
, restore_ctx
))
893 DWORD err
= GetLastError();
894 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
895 restore_ctx
, restore_dc
, err
);
898 else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL
, NULL
))
900 ERR("Failed to disable GL context.\n");
903 ReleaseDC(context
->win_handle
, context
->hdc
);
905 if (!pwglDeleteContext(context
->glCtx
))
907 DWORD err
= GetLastError();
908 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
912 DWORD
context_get_tls_idx(void)
914 return wined3d_context_tls_idx
;
917 void context_set_tls_idx(DWORD idx
)
919 wined3d_context_tls_idx
= idx
;
922 struct wined3d_context
*context_get_current(void)
924 return TlsGetValue(wined3d_context_tls_idx
);
927 BOOL
context_set_current(struct wined3d_context
*ctx
)
929 struct wined3d_context
*old
= context_get_current();
933 TRACE("Already using D3D context %p.\n", ctx
);
941 TRACE("Switching away from destroyed context %p.\n", old
);
942 context_destroy_gl_resources(old
);
943 HeapFree(GetProcessHeap(), 0, old
);
953 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
954 if (!pwglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
956 DWORD err
= GetLastError();
957 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
958 ctx
->glCtx
, ctx
->hdc
, err
);
959 TlsSetValue(wined3d_context_tls_idx
, NULL
);
964 else if(pwglGetCurrentContext())
966 TRACE("Clearing current D3D context.\n");
967 if (!pwglMakeCurrent(NULL
, NULL
))
969 DWORD err
= GetLastError();
970 ERR("Failed to clear current GL context, last error %#x.\n", err
);
971 TlsSetValue(wined3d_context_tls_idx
, NULL
);
976 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
979 void context_release(struct wined3d_context
*context
)
981 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
986 WARN("Context %p is not active.\n", context
);
987 else if (context
!= context_get_current())
988 WARN("Context %p is not the current context.\n", context
);
991 if (!--context
->level
&& context
->restore_ctx
)
993 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
994 if (!pwglMakeCurrent(context
->restore_dc
, context
->restore_ctx
))
996 DWORD err
= GetLastError();
997 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
998 context
->restore_ctx
, context
->restore_dc
, err
);
1000 context
->restore_ctx
= NULL
;
1001 context
->restore_dc
= NULL
;
1005 static void context_enter(struct wined3d_context
*context
)
1007 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1009 if (!context
->level
++)
1011 const struct wined3d_context
*current_context
= context_get_current();
1012 HGLRC current_gl
= pwglGetCurrentContext();
1014 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1016 TRACE("Another GL context (%p on device context %p) is already current.\n",
1017 current_gl
, pwglGetCurrentDC());
1018 context
->restore_ctx
= current_gl
;
1019 context
->restore_dc
= pwglGetCurrentDC();
1024 /*****************************************************************************
1025 * Context_MarkStateDirty
1027 * Marks a state in a context dirty. Only one context, opposed to
1028 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
1032 * context: Context to mark the state dirty in
1033 * state: State to mark dirty
1034 * StateTable: Pointer to the state table in use(for state grouping)
1036 *****************************************************************************/
1037 static void Context_MarkStateDirty(struct wined3d_context
*context
, DWORD state
, const struct StateEntry
*StateTable
)
1039 DWORD rep
= StateTable
[state
].representative
;
1043 if (isStateDirty(context
, rep
)) return;
1045 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1046 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1047 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1048 context
->isStateDirty
[idx
] |= (1 << shift
);
1051 /* This function takes care of WineD3D pixel format selection. */
1052 static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl
*This
, HDC hdc
,
1053 const struct wined3d_format_desc
*color_format_desc
, const struct wined3d_format_desc
*ds_format_desc
,
1054 BOOL auxBuffers
, int numSamples
, BOOL findCompatible
)
1057 unsigned int matchtry
;
1058 short redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1059 short depthBits
=0, stencilBits
=0;
1066 /* First, try without alpha match buffers. MacOS supports aux buffers only
1067 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
1068 * Then try without aux buffers - this is the most common cause for not
1069 * finding a pixel format. Also some drivers(the open source ones)
1070 * only offer 32 bit ARB pixel formats. First try without an exact alpha
1071 * match, then try without an exact alpha and color match.
1073 { TRUE
, TRUE
, TRUE
},
1074 { TRUE
, FALSE
, TRUE
},
1075 { FALSE
, TRUE
, TRUE
},
1076 { FALSE
, FALSE
, TRUE
},
1077 { TRUE
, FALSE
, FALSE
},
1078 { FALSE
, FALSE
, FALSE
},
1082 int nCfgs
= This
->adapter
->nCfgs
;
1084 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, findCompatible=%d\n",
1085 debug_d3dformat(color_format_desc
->format
), debug_d3dformat(ds_format_desc
->format
),
1086 auxBuffers
, numSamples
, findCompatible
);
1088 if (!getColorBits(color_format_desc
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1090 ERR("Unable to get color bits for format %s (%#x)!\n",
1091 debug_d3dformat(color_format_desc
->format
), color_format_desc
->format
);
1095 getDepthStencilBits(ds_format_desc
, &depthBits
, &stencilBits
);
1097 for(matchtry
= 0; matchtry
< (sizeof(matches
) / sizeof(matches
[0])) && !iPixelFormat
; matchtry
++) {
1098 for(i
=0; i
<nCfgs
; i
++) {
1099 BOOL exactDepthMatch
= TRUE
;
1100 WineD3D_PixelFormat
*cfg
= &This
->adapter
->cfgs
[i
];
1102 /* For now only accept RGBA formats. Perhaps some day we will
1103 * allow floating point formats for pbuffers. */
1104 if(cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1107 /* In window mode we need a window drawable format and double buffering. */
1108 if(!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1111 /* We like to have aux buffers in backbuffer mode */
1112 if(auxBuffers
&& !cfg
->auxBuffers
&& matches
[matchtry
].require_aux
)
1115 if(matches
[matchtry
].exact_color
) {
1116 if(cfg
->redSize
!= redBits
)
1118 if(cfg
->greenSize
!= greenBits
)
1120 if(cfg
->blueSize
!= blueBits
)
1123 if(cfg
->redSize
< redBits
)
1125 if(cfg
->greenSize
< greenBits
)
1127 if(cfg
->blueSize
< blueBits
)
1130 if(matches
[matchtry
].exact_alpha
) {
1131 if(cfg
->alphaSize
!= alphaBits
)
1134 if(cfg
->alphaSize
< alphaBits
)
1138 /* We try to locate a format which matches our requirements exactly. In case of
1139 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1140 if(cfg
->depthSize
< depthBits
)
1142 else if(cfg
->depthSize
> depthBits
)
1143 exactDepthMatch
= FALSE
;
1145 /* In all cases make sure the number of stencil bits matches our requirements
1146 * even when we don't need stencil because it could affect performance EXCEPT
1147 * on cards which don't offer depth formats without stencil like the i915 drivers
1149 if(stencilBits
!= cfg
->stencilSize
&& !(This
->adapter
->brokenStencil
&& stencilBits
<= cfg
->stencilSize
))
1152 /* Check multisampling support */
1153 if(cfg
->numSamples
!= numSamples
)
1156 /* When we have passed all the checks then we have found a format which matches our
1157 * requirements. Note that we only check for a limit number of capabilities right now,
1158 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
1159 * can still differ in things like multisampling, stereo, SRGB and other flags.
1162 /* Exit the loop as we have found a format :) */
1163 if(exactDepthMatch
) {
1164 iPixelFormat
= cfg
->iPixelFormat
;
1166 } else if(!iPixelFormat
) {
1167 /* In the end we might end up with a format which doesn't exactly match our depth
1168 * requirements. Accept the first format we found because formats with higher iPixelFormat
1169 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
1170 iPixelFormat
= cfg
->iPixelFormat
;
1175 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1176 if(!iPixelFormat
&& !findCompatible
) {
1177 ERR("Can't find a suitable iPixelFormat\n");
1179 } else if(!iPixelFormat
) {
1180 PIXELFORMATDESCRIPTOR pfd
;
1182 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1183 /* PixelFormat selection */
1184 ZeroMemory(&pfd
, sizeof(pfd
));
1185 pfd
.nSize
= sizeof(pfd
);
1187 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1188 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1189 pfd
.cAlphaBits
= alphaBits
;
1190 pfd
.cColorBits
= colorBits
;
1191 pfd
.cDepthBits
= depthBits
;
1192 pfd
.cStencilBits
= stencilBits
;
1193 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1195 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1197 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1198 ERR("Can't find a suitable iPixelFormat\n");
1203 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1204 iPixelFormat
, debug_d3dformat(color_format_desc
->format
), debug_d3dformat(ds_format_desc
->format
));
1205 return iPixelFormat
;
1208 /*****************************************************************************
1211 * Creates a new context.
1214 * This: Device to activate the context for
1215 * target: Surface this context will render to
1216 * win_handle: handle to the window which we are drawing to
1217 * pPresentParameters: contains the pixelformats to use for onscreen rendering
1219 *****************************************************************************/
1220 struct wined3d_context
*context_create(IWineD3DSwapChainImpl
*swapchain
, IWineD3DSurfaceImpl
*target
,
1221 const struct wined3d_format_desc
*ds_format_desc
)
1223 IWineD3DDeviceImpl
*device
= swapchain
->device
;
1224 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1225 const struct wined3d_format_desc
*color_format_desc
;
1226 struct wined3d_context
*ret
;
1227 PIXELFORMATDESCRIPTOR pfd
;
1228 BOOL auxBuffers
= FALSE
;
1236 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1238 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1241 ERR("Failed to allocate context memory.\n");
1245 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1247 ERR("Failed to retrieve a device context.\n");
1251 color_format_desc
= target
->resource
.format_desc
;
1253 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1254 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1255 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1259 if (color_format_desc
->format
== WINED3DFMT_B4G4R4X4_UNORM
)
1260 color_format_desc
= getFormatDescEntry(WINED3DFMT_B4G4R4A4_UNORM
, gl_info
);
1261 else if (color_format_desc
->format
== WINED3DFMT_B8G8R8X8_UNORM
)
1262 color_format_desc
= getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM
, gl_info
);
1265 /* DirectDraw supports 8bit paletted render targets and these are used by
1266 * old games like Starcraft and C&C. Most modern hardware doesn't support
1267 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1268 * conversion (ab)uses the alpha component for storing the palette index.
1269 * For this reason we require a format with 8bit alpha, so request
1271 if (color_format_desc
->format
== WINED3DFMT_P8_UINT
)
1272 color_format_desc
= getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM
, gl_info
);
1274 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
1275 if (swapchain
->presentParms
.MultiSampleType
&& (swapchain
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_DISCARD
))
1277 if (!gl_info
->supported
[ARB_MULTISAMPLE
])
1278 WARN("The application is requesting multisampling without support.\n");
1281 TRACE("Requesting multisample type %#x.\n", swapchain
->presentParms
.MultiSampleType
);
1282 numSamples
= swapchain
->presentParms
.MultiSampleType
;
1286 /* Try to find a pixel format which matches our requirements. */
1287 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format_desc
, ds_format_desc
,
1288 auxBuffers
, numSamples
, FALSE
/* findCompatible */);
1290 /* Try to locate a compatible format if we weren't able to find anything. */
1293 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1294 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format_desc
, ds_format_desc
,
1295 auxBuffers
, 0 /* numSamples */, TRUE
/* findCompatible */);
1298 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1301 ERR("Can't find a suitable pixel format.\n");
1305 DescribePixelFormat(hdc
, pixel_format
, sizeof(pfd
), &pfd
);
1306 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1308 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1312 ctx
= pwglCreateContext(hdc
);
1313 if (device
->numContexts
)
1315 if (!pwglShareLists(device
->contexts
[0]->glCtx
, ctx
))
1317 DWORD err
= GetLastError();
1318 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1319 device
->contexts
[0]->glCtx
, ctx
, err
);
1324 ERR("Failed to create a WGL context\n");
1328 if (!device_context_add(device
, ret
))
1330 ERR("Failed to add the newly created context to the context list\n");
1331 if (!pwglDeleteContext(ctx
))
1333 DWORD err
= GetLastError();
1334 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, err
);
1339 ret
->gl_info
= gl_info
;
1341 /* Mark all states dirty to force a proper initialization of the states
1342 * on the first use of the context. */
1343 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1345 if (device
->StateTable
[state
].representative
)
1346 Context_MarkStateDirty(ret
, state
, device
->StateTable
);
1349 ret
->swapchain
= swapchain
;
1350 ret
->current_rt
= target
;
1351 ret
->tid
= GetCurrentThreadId();
1353 ret
->render_offscreen
= surface_is_offscreen(target
);
1354 ret
->draw_buffer_dirty
= TRUE
;
1358 ret
->win_handle
= swapchain
->win_handle
;
1360 ret
->pixel_format
= pixel_format
;
1362 if (device
->shader_backend
->shader_dirtifyable_constants((IWineD3DDevice
*)device
))
1364 /* Create the dirty constants array and initialize them to dirty */
1365 ret
->vshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1366 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1367 ret
->pshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1368 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1369 memset(ret
->vshader_const_dirty
, 1,
1370 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1371 memset(ret
->pshader_const_dirty
, 1,
1372 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1375 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1376 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1377 if (!ret
->blit_targets
) goto out
;
1379 ret
->free_occlusion_query_size
= 4;
1380 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1381 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1382 if (!ret
->free_occlusion_queries
) goto out
;
1384 list_init(&ret
->occlusion_queries
);
1386 ret
->free_event_query_size
= 4;
1387 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1388 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1389 if (!ret
->free_event_queries
) goto out
;
1391 list_init(&ret
->event_queries
);
1393 TRACE("Successfully created new context %p\n", ret
);
1395 list_init(&ret
->fbo_list
);
1396 list_init(&ret
->fbo_destroy_list
);
1400 /* Set up the context defaults */
1401 if (!context_set_current(ret
))
1403 ERR("Cannot activate context to set up defaults\n");
1404 context_release(ret
);
1410 glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1412 TRACE("Setting up the screen\n");
1413 /* Clear the screen */
1414 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
1415 checkGLcall("glClearColor");
1418 glClearStencil(0xffff);
1420 checkGLcall("glClear");
1422 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1423 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1425 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1426 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1428 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1429 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1431 glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1432 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1433 glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1434 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1436 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
1438 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1439 * and textures in DIB sections(due to the memory protection).
1441 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1442 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1444 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1446 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1447 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1448 * GL_VERTEX_BLEND_ARB isn't enabled too
1450 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1451 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1453 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1455 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1456 * the previous texture where to source the offset from is always unit - 1.
1458 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1460 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1461 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1462 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1465 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1467 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1468 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1469 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1470 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1473 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1474 * program and the dummy program is destroyed when the context is destroyed.
1476 const char *dummy_program
=
1478 "MOV result.color, fragment.color.primary;\n"
1480 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1481 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1482 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1485 for (s
= 0; s
< gl_info
->limits
.point_sprite_units
; ++s
)
1487 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1488 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1489 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1492 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1494 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1496 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1498 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1503 device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, TRUE
);
1505 TRACE("Created context %p.\n", ret
);
1510 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1511 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1512 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1513 HeapFree(GetProcessHeap(), 0, ret
->pshader_const_dirty
);
1514 HeapFree(GetProcessHeap(), 0, ret
->vshader_const_dirty
);
1515 HeapFree(GetProcessHeap(), 0, ret
);
1519 /*****************************************************************************
1522 * Destroys a wined3d context
1525 * This: Device to activate the context for
1526 * context: Context to destroy
1528 *****************************************************************************/
1529 void context_destroy(IWineD3DDeviceImpl
*This
, struct wined3d_context
*context
)
1533 TRACE("Destroying ctx %p\n", context
);
1535 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1537 context_destroy_gl_resources(context
);
1538 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1543 context
->destroyed
= 1;
1547 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1548 HeapFree(GetProcessHeap(), 0, context
->vshader_const_dirty
);
1549 HeapFree(GetProcessHeap(), 0, context
->pshader_const_dirty
);
1550 device_context_remove(This
, context
);
1551 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1554 /* GL locking is done by the caller */
1555 static inline void set_blit_dimension(UINT width
, UINT height
) {
1556 glMatrixMode(GL_PROJECTION
);
1557 checkGLcall("glMatrixMode(GL_PROJECTION)");
1559 checkGLcall("glLoadIdentity()");
1560 glOrtho(0, width
, height
, 0, 0.0, -1.0);
1561 checkGLcall("glOrtho");
1562 glViewport(0, 0, width
, height
);
1563 checkGLcall("glViewport");
1566 /*****************************************************************************
1569 * Sets up a context for DirectDraw blitting.
1570 * All texture units are disabled, texture unit 0 is set as current unit
1571 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1572 * color writing enabled for all channels
1573 * register combiners disabled, shaders disabled
1574 * world matrix is set to identity, texture matrix 0 too
1575 * projection matrix is setup for drawing screen coordinates
1578 * This: Device to activate the context for
1579 * context: Context to setup
1581 *****************************************************************************/
1582 /* Context activation is done by the caller. */
1583 static void SetupForBlit(IWineD3DDeviceImpl
*This
, struct wined3d_context
*context
)
1586 const struct StateEntry
*StateTable
= This
->StateTable
;
1587 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1588 UINT width
= context
->current_rt
->currentDesc
.Width
;
1589 UINT height
= context
->current_rt
->currentDesc
.Height
;
1592 TRACE("Setting up context %p for blitting\n", context
);
1593 if(context
->last_was_blit
) {
1594 if(context
->blit_w
!= width
|| context
->blit_h
!= height
) {
1596 set_blit_dimension(width
, height
);
1598 context
->blit_w
= width
; context
->blit_h
= height
;
1599 /* No need to dirtify here, the states are still dirtified because they weren't
1600 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1604 TRACE("Context is already set up for blitting, nothing to do\n");
1607 context
->last_was_blit
= TRUE
;
1609 /* TODO: Use a display list */
1611 /* Disable shaders */
1613 This
->shader_backend
->shader_select(context
, FALSE
, FALSE
);
1616 Context_MarkStateDirty(context
, STATE_VSHADER
, StateTable
);
1617 Context_MarkStateDirty(context
, STATE_PIXELSHADER
, StateTable
);
1619 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1620 * helper functions in between gl calls. This function is full of Context_MarkStateDirty
1621 * which can safely be called from here, we only lock once instead locking/unlocking
1622 * after each GL call.
1626 /* Disable all textures. The caller can then bind a texture it wants to blit
1629 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1630 * function texture unit. No need to care for higher samplers
1632 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1634 sampler
= This
->rev_tex_unit_map
[i
];
1635 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1636 checkGLcall("glActiveTextureARB");
1638 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1640 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1641 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1643 glDisable(GL_TEXTURE_3D
);
1644 checkGLcall("glDisable GL_TEXTURE_3D");
1645 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1647 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1648 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1650 glDisable(GL_TEXTURE_2D
);
1651 checkGLcall("glDisable GL_TEXTURE_2D");
1653 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1654 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1656 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1658 if (sampler
< MAX_TEXTURES
) {
1659 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1661 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
1664 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
1665 checkGLcall("glActiveTextureARB");
1667 sampler
= This
->rev_tex_unit_map
[0];
1669 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1671 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1672 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1674 glDisable(GL_TEXTURE_3D
);
1675 checkGLcall("glDisable GL_TEXTURE_3D");
1676 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1678 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1679 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1681 glDisable(GL_TEXTURE_2D
);
1682 checkGLcall("glDisable GL_TEXTURE_2D");
1684 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1686 glMatrixMode(GL_TEXTURE
);
1687 checkGLcall("glMatrixMode(GL_TEXTURE)");
1689 checkGLcall("glLoadIdentity()");
1691 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1693 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1694 GL_TEXTURE_LOD_BIAS_EXT
,
1696 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
1699 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1701 if (sampler
< MAX_TEXTURES
) {
1702 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
+ sampler
), StateTable
);
1703 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1705 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
1708 /* Other misc states */
1709 glDisable(GL_ALPHA_TEST
);
1710 checkGLcall("glDisable(GL_ALPHA_TEST)");
1711 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
), StateTable
);
1712 glDisable(GL_LIGHTING
);
1713 checkGLcall("glDisable GL_LIGHTING");
1714 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_LIGHTING
), StateTable
);
1715 glDisable(GL_DEPTH_TEST
);
1716 checkGLcall("glDisable GL_DEPTH_TEST");
1717 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ZENABLE
), StateTable
);
1718 glDisableWINE(GL_FOG
);
1719 checkGLcall("glDisable GL_FOG");
1720 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
), StateTable
);
1721 glDisable(GL_BLEND
);
1722 checkGLcall("glDisable GL_BLEND");
1723 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1724 glDisable(GL_CULL_FACE
);
1725 checkGLcall("glDisable GL_CULL_FACE");
1726 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CULLMODE
), StateTable
);
1727 glDisable(GL_STENCIL_TEST
);
1728 checkGLcall("glDisable GL_STENCIL_TEST");
1729 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
), StateTable
);
1730 glDisable(GL_SCISSOR_TEST
);
1731 checkGLcall("glDisable GL_SCISSOR_TEST");
1732 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
1733 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1735 glDisable(GL_POINT_SPRITE_ARB
);
1736 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1737 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
), StateTable
);
1739 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1740 checkGLcall("glColorMask");
1741 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
), StateTable
);
1742 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1
), StateTable
);
1743 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2
), StateTable
);
1744 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3
), StateTable
);
1745 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1747 glDisable(GL_COLOR_SUM_EXT
);
1748 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
1749 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1752 /* Setup transforms */
1753 glMatrixMode(GL_MODELVIEW
);
1754 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1756 checkGLcall("glLoadIdentity()");
1757 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable
);
1759 context
->last_was_rhw
= TRUE
;
1760 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
); /* because of last_was_rhw = TRUE */
1762 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1763 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1764 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1765 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1766 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1767 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1768 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
1770 set_blit_dimension(width
, height
);
1774 context
->blit_w
= width
; context
->blit_h
= height
;
1775 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
1776 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1779 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, FALSE
);
1782 /*****************************************************************************
1783 * findThreadContextForSwapChain
1785 * Searches a swapchain for all contexts and picks one for the thread tid.
1786 * If none can be found the swapchain is requested to create a new context
1788 *****************************************************************************/
1789 static struct wined3d_context
*findThreadContextForSwapChain(IWineD3DSwapChain
*swapchain
, DWORD tid
)
1793 for(i
= 0; i
< ((IWineD3DSwapChainImpl
*) swapchain
)->num_contexts
; i
++) {
1794 if(((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
]->tid
== tid
) {
1795 return ((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
];
1800 /* Create a new context for the thread */
1801 return swapchain_create_context_for_thread(swapchain
);
1804 /*****************************************************************************
1807 * Finds a context for the current render target and thread
1810 * target: Render target to find the context for
1811 * tid: Thread to activate the context for
1813 * Returns: The needed context
1815 *****************************************************************************/
1816 static struct wined3d_context
*FindContext(IWineD3DDeviceImpl
*This
, IWineD3DSurfaceImpl
*target
)
1818 IWineD3DSwapChain
*swapchain
= NULL
;
1819 struct wined3d_context
*current_context
= context_get_current();
1820 DWORD tid
= GetCurrentThreadId();
1821 struct wined3d_context
*context
;
1823 if (current_context
&& current_context
->destroyed
) current_context
= NULL
;
1828 && current_context
->current_rt
1829 && current_context
->swapchain
->device
== This
)
1831 target
= current_context
->current_rt
;
1835 IWineD3DSwapChainImpl
*swapchain
= (IWineD3DSwapChainImpl
*)This
->swapchains
[0];
1836 if (swapchain
->back_buffers
) target
= swapchain
->back_buffers
[0];
1837 else target
= swapchain
->front_buffer
;
1841 if (current_context
&& current_context
->current_rt
== target
)
1843 context_validate(current_context
);
1844 return current_context
;
1847 if (target
->Flags
& SFLAG_SWAPCHAIN
)
1849 TRACE("Rendering onscreen\n");
1851 swapchain
= (IWineD3DSwapChain
*)target
->container
;
1852 context
= findThreadContextForSwapChain(swapchain
, tid
);
1856 TRACE("Rendering offscreen\n");
1858 /* Stay with the currently active context. */
1859 if (current_context
&& current_context
->swapchain
->device
== This
)
1861 context
= current_context
;
1865 /* This may happen if the app jumps straight into offscreen rendering
1866 * Start using the context of the primary swapchain. tid == 0 is no problem
1867 * for findThreadContextForSwapChain.
1869 * Can also happen on thread switches - in that case findThreadContextForSwapChain
1870 * is perfect to call. */
1871 context
= findThreadContextForSwapChain(This
->swapchains
[0], tid
);
1875 context_validate(context
);
1880 /* Context activation is done by the caller. */
1881 static void context_apply_draw_buffer(struct wined3d_context
*context
, BOOL blit
)
1883 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1884 IWineD3DSurfaceImpl
*rt
= context
->current_rt
;
1885 IWineD3DDeviceImpl
*device
;
1887 device
= rt
->resource
.device
;
1888 if (!surface_is_offscreen(rt
))
1891 glDrawBuffer(surface_get_gl_buffer(rt
));
1892 checkGLcall("glDrawBuffers()");
1898 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1904 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
1906 if (device
->render_targets
[i
])
1907 device
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
1909 device
->draw_buffers
[i
] = GL_NONE
;
1912 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1914 GL_EXTCALL(glDrawBuffersARB(gl_info
->limits
.buffers
, device
->draw_buffers
));
1915 checkGLcall("glDrawBuffers()");
1919 glDrawBuffer(device
->draw_buffers
[0]);
1920 checkGLcall("glDrawBuffer()");
1923 glDrawBuffer(GL_COLOR_ATTACHMENT0
);
1924 checkGLcall("glDrawBuffer()");
1929 glDrawBuffer(device
->offscreenBuffer
);
1930 checkGLcall("glDrawBuffer()");
1936 /* GL locking is done by the caller. */
1937 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
1939 glDrawBuffer(buffer
);
1940 checkGLcall("glDrawBuffer()");
1941 context
->draw_buffer_dirty
= TRUE
;
1944 static inline void context_set_render_offscreen(struct wined3d_context
*context
, const struct StateEntry
*StateTable
,
1947 if (context
->render_offscreen
== offscreen
) return;
1949 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1950 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
);
1951 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
1952 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
1953 Context_MarkStateDirty(context
, STATE_FRONTFACE
, StateTable
);
1954 context
->render_offscreen
= offscreen
;
1957 static BOOL
match_depth_stencil_format(const struct wined3d_format_desc
*existing
,
1958 const struct wined3d_format_desc
*required
)
1960 short existing_depth
, existing_stencil
, required_depth
, required_stencil
;
1962 if(existing
== required
) return TRUE
;
1963 if((existing
->Flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->Flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
1965 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
1966 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
1968 if(existing_depth
< required_depth
) return FALSE
;
1969 /* If stencil bits are used the exact amount is required - otherwise wrapping
1970 * won't work correctly */
1971 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
1974 /* The caller provides a context */
1975 static void context_validate_onscreen_formats(IWineD3DDeviceImpl
*device
, struct wined3d_context
*context
)
1977 /* Onscreen surfaces are always in a swapchain */
1978 IWineD3DSurfaceImpl
*depth_stencil
= device
->depth_stencil
;
1979 IWineD3DSwapChainImpl
*swapchain
= (IWineD3DSwapChainImpl
*)context
->current_rt
->container
;
1981 if (!depth_stencil
) return;
1982 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->resource
.format_desc
)) return;
1984 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
1985 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
1987 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
1989 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
1990 IWineD3DSurface_LoadLocation((IWineD3DSurface
*)context
->current_rt
, SFLAG_INTEXTURE
, NULL
);
1991 swapchain
->render_to_fbo
= TRUE
;
1992 context_set_render_offscreen(context
, device
->StateTable
, TRUE
);
1995 /* Context activation is done by the caller. */
1996 void context_apply_blit_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
)
1998 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2000 if (context
->render_offscreen
)
2002 FIXME("Applying blit state for an offscreen target with ORM_FBO. This should be avoided.\n");
2003 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2006 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, context
->current_rt
, NULL
);
2011 context_validate_onscreen_formats(device
, context
);
2014 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
2018 context
->draw_buffer_dirty
= TRUE
;
2021 if (context
->draw_buffer_dirty
)
2023 context_apply_draw_buffer(context
, TRUE
);
2024 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2025 context
->draw_buffer_dirty
= FALSE
;
2028 SetupForBlit(device
, context
);
2031 /* Context activation is done by the caller. */
2032 void context_apply_clear_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
,
2033 IWineD3DSurfaceImpl
*render_target
, IWineD3DSurfaceImpl
*depth_stencil
)
2035 const struct StateEntry
*state_table
= device
->StateTable
;
2038 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2040 if (!context
->render_offscreen
) context_validate_onscreen_formats(device
, context
);
2042 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, render_target
, depth_stencil
);
2046 if (!surface_is_offscreen(render_target
))
2047 buffer
= surface_get_gl_buffer(render_target
);
2048 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2049 buffer
= GL_COLOR_ATTACHMENT0
;
2051 buffer
= device
->offscreenBuffer
;
2054 context_set_draw_buffer(context
, buffer
);
2057 if (context
->last_was_blit
)
2059 device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, TRUE
);
2062 /* Blending and clearing should be orthogonal, but tests on the nvidia
2063 * driver show that disabling blending when clearing improves the clearing
2064 * performance incredibly. */
2066 glDisable(GL_BLEND
);
2067 glEnable(GL_SCISSOR_TEST
);
2068 checkGLcall("glEnable GL_SCISSOR_TEST");
2071 context
->last_was_blit
= FALSE
;
2072 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), state_table
);
2073 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), state_table
);
2074 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, state_table
);
2077 /* Context activation is done by the caller. */
2078 void context_apply_draw_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
)
2080 const struct StateEntry
*state_table
= device
->StateTable
;
2083 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2085 if (!context
->render_offscreen
)
2087 context_validate_onscreen_formats(device
, context
);
2089 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
);
2095 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, device
->render_targets
, device
->depth_stencil
);
2100 if (context
->draw_buffer_dirty
)
2102 context_apply_draw_buffer(context
, FALSE
);
2103 context
->draw_buffer_dirty
= FALSE
;
2106 if (context
->last_was_blit
)
2108 device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, TRUE
);
2111 IWineD3DDeviceImpl_FindTexUnitMap(device
);
2112 device_preload_textures(device
);
2113 if (isStateDirty(context
, STATE_VDECL
))
2114 device_update_stream_info(device
, context
->gl_info
);
2117 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2119 DWORD rep
= context
->dirtyArray
[i
];
2120 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2121 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2122 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2123 state_table
[rep
].apply(rep
, device
->stateBlock
, context
);
2126 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2127 context
->last_was_blit
= FALSE
;
2130 static void context_setup_target(IWineD3DDeviceImpl
*device
,
2131 struct wined3d_context
*context
, IWineD3DSurfaceImpl
*target
)
2133 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
2134 const struct StateEntry
*StateTable
= device
->StateTable
;
2136 if (!target
) return;
2137 else if (context
->current_rt
== target
) return;
2138 render_offscreen
= surface_is_offscreen(target
);
2140 context_set_render_offscreen(context
, StateTable
, render_offscreen
);
2142 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2143 * the alpha blend state changes with different render target formats. */
2144 if (!context
->current_rt
)
2146 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2150 const struct wined3d_format_desc
*old
= context
->current_rt
->resource
.format_desc
;
2151 const struct wined3d_format_desc
*new = target
->resource
.format_desc
;
2153 if (old
->format
!= new->format
)
2155 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2156 if ((old
->alpha_mask
&& !new->alpha_mask
) || (!old
->alpha_mask
&& new->alpha_mask
)
2157 || !(new->Flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
2159 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
2163 /* When switching away from an offscreen render target, and we're not
2164 * using FBOs, we have to read the drawable into the texture. This is
2165 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2166 * are some things that need care though. PreLoad needs a GL context,
2167 * and FindContext is called before the context is activated. It also
2168 * has to be called with the old rendertarget active, otherwise a
2169 * wrong drawable is read. */
2170 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2171 && old_render_offscreen
&& context
->current_rt
!= target
)
2173 BOOL oldInDraw
= device
->isInDraw
;
2175 /* surface_internal_preload() requires a context to load the
2176 * texture, so it will call context_acquire(). Set isInDraw to true
2177 * to signal surface_internal_preload() that it has a context. */
2179 /* FIXME: This is just broken. There's no guarantee whatsoever
2180 * that the currently active context, if any, is appropriate for
2181 * reading back the render target. We should probably call
2182 * context_set_current(context) here and then rely on
2183 * context_acquire() doing the right thing. */
2184 device
->isInDraw
= TRUE
;
2186 /* Read the back buffer of the old drawable into the destination texture. */
2187 if (context
->current_rt
->texture_name_srgb
)
2189 surface_internal_preload(context
->current_rt
, SRGB_BOTH
);
2193 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2196 IWineD3DSurface_ModifyLocation((IWineD3DSurface
*)context
->current_rt
, SFLAG_INDRAWABLE
, FALSE
);
2198 device
->isInDraw
= oldInDraw
;
2202 context
->draw_buffer_dirty
= TRUE
;
2203 context
->current_rt
= target
;
2206 /*****************************************************************************
2209 * Finds a rendering context and drawable matching the device and render
2210 * target for the current thread, activates them and puts them into the
2214 * This: Device to activate the context for
2215 * target: Requested render target
2216 * usage: Prepares the context for blitting, drawing or other actions
2218 *****************************************************************************/
2219 struct wined3d_context
*context_acquire(IWineD3DDeviceImpl
*device
, IWineD3DSurfaceImpl
*target
)
2221 struct wined3d_context
*current_context
= context_get_current();
2222 struct wined3d_context
*context
;
2224 TRACE("device %p, target %p.\n", device
, target
);
2226 context
= FindContext(device
, target
);
2227 context_setup_target(device
, context
, target
);
2228 context_enter(context
);
2229 if (!context
->valid
) return context
;
2231 if (context
!= current_context
)
2233 if (!context_set_current(context
)) ERR("Failed to activate the new context.\n");
2234 else device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, !context
->last_was_blit
);
2236 if (context
->vshader_const_dirty
)
2238 memset(context
->vshader_const_dirty
, 1,
2239 sizeof(*context
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
2240 device
->highest_dirty_vs_const
= device
->d3d_vshader_constantF
;
2242 if (context
->pshader_const_dirty
)
2244 memset(context
->pshader_const_dirty
, 1,
2245 sizeof(*context
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
2246 device
->highest_dirty_ps_const
= device
->d3d_pshader_constantF
;
2249 else if (context
->restore_ctx
)
2251 if (!pwglMakeCurrent(context
->hdc
, context
->glCtx
))
2253 DWORD err
= GetLastError();
2254 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
2255 context
->hdc
, context
->glCtx
, err
);