winex11: Simplify wglSetPixelFormatWINE and export it as a standard extension function.
[wine/multimedia.git] / dlls / wined3d / context.c
blobe53936fa4beadba9108c508d3e8577ca183daddc
1 /*
2 * Context and render target management in wined3d
4 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
5 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <stdio.h>
26 #ifdef HAVE_FLOAT_H
27 # include <float.h>
28 #endif
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
34 static DWORD wined3d_context_tls_idx;
36 /* FBO helper functions */
38 /* GL locking is done by the caller */
39 static void context_bind_fbo(struct wined3d_context *context, GLenum target, GLuint *fbo)
41 const struct wined3d_gl_info *gl_info = context->gl_info;
42 GLuint f;
44 if (!fbo)
46 f = 0;
48 else
50 if (!*fbo)
52 gl_info->fbo_ops.glGenFramebuffers(1, fbo);
53 checkGLcall("glGenFramebuffers()");
54 TRACE("Created FBO %u.\n", *fbo);
56 f = *fbo;
59 switch (target)
61 case GL_READ_FRAMEBUFFER:
62 if (context->fbo_read_binding == f) return;
63 context->fbo_read_binding = f;
64 break;
66 case GL_DRAW_FRAMEBUFFER:
67 if (context->fbo_draw_binding == f) return;
68 context->fbo_draw_binding = f;
69 break;
71 case GL_FRAMEBUFFER:
72 if (context->fbo_read_binding == f
73 && context->fbo_draw_binding == f) return;
74 context->fbo_read_binding = f;
75 context->fbo_draw_binding = f;
76 break;
78 default:
79 FIXME("Unhandled target %#x.\n", target);
80 break;
83 gl_info->fbo_ops.glBindFramebuffer(target, f);
84 checkGLcall("glBindFramebuffer()");
87 /* GL locking is done by the caller */
88 static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info, GLenum target)
90 unsigned int i;
92 for (i = 0; i < gl_info->limits.buffers; ++i)
94 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
95 checkGLcall("glFramebufferTexture2D()");
97 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
98 checkGLcall("glFramebufferTexture2D()");
100 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
101 checkGLcall("glFramebufferTexture2D()");
104 /* GL locking is done by the caller */
105 static void context_destroy_fbo(struct wined3d_context *context, GLuint *fbo)
107 const struct wined3d_gl_info *gl_info = context->gl_info;
109 context_bind_fbo(context, GL_FRAMEBUFFER, fbo);
110 context_clean_fbo_attachments(gl_info, GL_FRAMEBUFFER);
111 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
113 gl_info->fbo_ops.glDeleteFramebuffers(1, fbo);
114 checkGLcall("glDeleteFramebuffers()");
117 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info *gl_info,
118 GLenum fbo_target, DWORD format_flags, GLuint rb)
120 if (format_flags & WINED3DFMT_FLAG_DEPTH)
122 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
123 checkGLcall("glFramebufferRenderbuffer()");
126 if (format_flags & WINED3DFMT_FLAG_STENCIL)
128 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
129 checkGLcall("glFramebufferRenderbuffer()");
133 /* GL locking is done by the caller */
134 static void context_attach_depth_stencil_fbo(struct wined3d_context *context,
135 GLenum fbo_target, struct wined3d_surface *depth_stencil, DWORD location)
137 const struct wined3d_gl_info *gl_info = context->gl_info;
139 TRACE("Attach depth stencil %p\n", depth_stencil);
141 if (depth_stencil)
143 DWORD format_flags = depth_stencil->resource.format->flags;
145 if (depth_stencil->current_renderbuffer)
147 context_attach_depth_stencil_rb(gl_info, fbo_target,
148 format_flags, depth_stencil->current_renderbuffer->id);
150 else
152 switch (location)
154 case SFLAG_INTEXTURE:
155 case SFLAG_INSRGBTEX:
156 surface_prepare_texture(depth_stencil, context, FALSE);
158 if (format_flags & WINED3DFMT_FLAG_DEPTH)
160 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT,
161 depth_stencil->texture_target, depth_stencil->texture_name,
162 depth_stencil->texture_level);
163 checkGLcall("glFramebufferTexture2D()");
166 if (format_flags & WINED3DFMT_FLAG_STENCIL)
168 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT,
169 depth_stencil->texture_target, depth_stencil->texture_name,
170 depth_stencil->texture_level);
171 checkGLcall("glFramebufferTexture2D()");
173 break;
175 case SFLAG_INRB_MULTISAMPLE:
176 surface_prepare_rb(depth_stencil, gl_info, TRUE);
177 context_attach_depth_stencil_rb(gl_info, fbo_target,
178 format_flags, depth_stencil->rb_multisample);
179 break;
181 case SFLAG_INRB_RESOLVED:
182 surface_prepare_rb(depth_stencil, gl_info, FALSE);
183 context_attach_depth_stencil_rb(gl_info, fbo_target,
184 format_flags, depth_stencil->rb_resolved);
185 break;
187 default:
188 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location), location);
189 break;
193 if (!(format_flags & WINED3DFMT_FLAG_DEPTH))
195 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
196 checkGLcall("glFramebufferTexture2D()");
199 if (!(format_flags & WINED3DFMT_FLAG_STENCIL))
201 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
202 checkGLcall("glFramebufferTexture2D()");
205 else
207 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
208 checkGLcall("glFramebufferTexture2D()");
210 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
211 checkGLcall("glFramebufferTexture2D()");
215 /* GL locking is done by the caller */
216 static void context_attach_surface_fbo(struct wined3d_context *context,
217 GLenum fbo_target, DWORD idx, struct wined3d_surface *surface, DWORD location)
219 const struct wined3d_gl_info *gl_info = context->gl_info;
221 TRACE("Attach surface %p to %u\n", surface, idx);
223 if (surface && surface->resource.format->id != WINED3DFMT_NULL)
225 BOOL srgb;
227 switch (location)
229 case SFLAG_INTEXTURE:
230 case SFLAG_INSRGBTEX:
231 srgb = location == SFLAG_INSRGBTEX;
232 surface_prepare_texture(surface, context, srgb);
233 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
234 surface->texture_target, surface_get_texture_name(surface, gl_info, srgb),
235 surface->texture_level);
236 checkGLcall("glFramebufferTexture2D()");
237 break;
239 case SFLAG_INRB_MULTISAMPLE:
240 surface_prepare_rb(surface, gl_info, TRUE);
241 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
242 GL_RENDERBUFFER, surface->rb_multisample);
243 checkGLcall("glFramebufferRenderbuffer()");
244 break;
246 case SFLAG_INRB_RESOLVED:
247 surface_prepare_rb(surface, gl_info, FALSE);
248 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
249 GL_RENDERBUFFER, surface->rb_resolved);
250 checkGLcall("glFramebufferRenderbuffer()");
251 break;
253 default:
254 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location), location);
255 break;
258 else
260 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx, GL_TEXTURE_2D, 0, 0);
261 checkGLcall("glFramebufferTexture2D()");
265 /* GL locking is done by the caller */
266 void context_check_fbo_status(const struct wined3d_context *context, GLenum target)
268 const struct wined3d_gl_info *gl_info = context->gl_info;
269 GLenum status;
271 if (!FIXME_ON(d3d)) return;
273 status = gl_info->fbo_ops.glCheckFramebufferStatus(target);
274 if (status == GL_FRAMEBUFFER_COMPLETE)
276 TRACE("FBO complete\n");
278 else
280 const struct wined3d_surface *attachment;
281 unsigned int i;
283 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status), status);
285 if (!context->current_fbo)
287 ERR("FBO 0 is incomplete, driver bug?\n");
288 return;
291 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context->current_fbo->location),
292 context->current_fbo->location);
294 /* Dump the FBO attachments */
295 for (i = 0; i < gl_info->limits.buffers; ++i)
297 attachment = context->current_fbo->render_targets[i];
298 if (attachment)
300 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
301 i, attachment, debug_d3dformat(attachment->resource.format->id),
302 attachment->pow2Width, attachment->pow2Height, attachment->resource.multisample_type);
305 attachment = context->current_fbo->depth_stencil;
306 if (attachment)
308 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
309 attachment, debug_d3dformat(attachment->resource.format->id),
310 attachment->pow2Width, attachment->pow2Height, attachment->resource.multisample_type);
315 static inline DWORD context_generate_rt_mask(GLenum buffer)
317 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
318 return buffer ? (1 << 31) | buffer : 0;
321 static inline DWORD context_generate_rt_mask_from_surface(const struct wined3d_surface *target)
323 return (1 << 31) | surface_get_gl_buffer(target);
326 static struct fbo_entry *context_create_fbo_entry(const struct wined3d_context *context,
327 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil, DWORD location)
329 const struct wined3d_gl_info *gl_info = context->gl_info;
330 struct fbo_entry *entry;
332 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
333 entry->render_targets = HeapAlloc(GetProcessHeap(), 0, gl_info->limits.buffers * sizeof(*entry->render_targets));
334 memcpy(entry->render_targets, render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
335 entry->depth_stencil = depth_stencil;
336 entry->location = location;
337 entry->rt_mask = context_generate_rt_mask(GL_COLOR_ATTACHMENT0);
338 entry->attached = FALSE;
339 entry->id = 0;
341 return entry;
344 /* GL locking is done by the caller */
345 static void context_reuse_fbo_entry(struct wined3d_context *context, GLenum target,
346 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil,
347 DWORD location, struct fbo_entry *entry)
349 const struct wined3d_gl_info *gl_info = context->gl_info;
351 context_bind_fbo(context, target, &entry->id);
352 context_clean_fbo_attachments(gl_info, target);
354 memcpy(entry->render_targets, render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
355 entry->depth_stencil = depth_stencil;
356 entry->location = location;
357 entry->attached = FALSE;
360 /* GL locking is done by the caller */
361 static void context_destroy_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
363 if (entry->id)
365 TRACE("Destroy FBO %d\n", entry->id);
366 context_destroy_fbo(context, &entry->id);
368 --context->fbo_entry_count;
369 list_remove(&entry->entry);
370 HeapFree(GetProcessHeap(), 0, entry->render_targets);
371 HeapFree(GetProcessHeap(), 0, entry);
375 /* GL locking is done by the caller */
376 static struct fbo_entry *context_find_fbo_entry(struct wined3d_context *context, GLenum target,
377 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil, DWORD location)
379 const struct wined3d_gl_info *gl_info = context->gl_info;
380 struct fbo_entry *entry;
382 if (depth_stencil && render_targets && render_targets[0])
384 if (depth_stencil->resource.width < render_targets[0]->resource.width ||
385 depth_stencil->resource.height < render_targets[0]->resource.height)
387 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
388 depth_stencil = NULL;
392 LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
394 if (!memcmp(entry->render_targets,
395 render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets))
396 && entry->depth_stencil == depth_stencil && entry->location == location)
398 list_remove(&entry->entry);
399 list_add_head(&context->fbo_list, &entry->entry);
400 return entry;
404 if (context->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
406 entry = context_create_fbo_entry(context, render_targets, depth_stencil, location);
407 list_add_head(&context->fbo_list, &entry->entry);
408 ++context->fbo_entry_count;
410 else
412 entry = LIST_ENTRY(list_tail(&context->fbo_list), struct fbo_entry, entry);
413 context_reuse_fbo_entry(context, target, render_targets, depth_stencil, location, entry);
414 list_remove(&entry->entry);
415 list_add_head(&context->fbo_list, &entry->entry);
418 return entry;
421 /* GL locking is done by the caller */
422 static void context_apply_fbo_entry(struct wined3d_context *context, GLenum target, struct fbo_entry *entry)
424 const struct wined3d_gl_info *gl_info = context->gl_info;
425 unsigned int i;
427 context_bind_fbo(context, target, &entry->id);
429 if (entry->attached) return;
431 /* Apply render targets */
432 for (i = 0; i < gl_info->limits.buffers; ++i)
434 context_attach_surface_fbo(context, target, i, entry->render_targets[i], entry->location);
437 /* Apply depth targets */
438 if (entry->depth_stencil)
439 surface_set_compatible_renderbuffer(entry->depth_stencil, entry->render_targets[0]);
440 context_attach_depth_stencil_fbo(context, target, entry->depth_stencil, entry->location);
442 entry->attached = TRUE;
445 /* GL locking is done by the caller */
446 static void context_apply_fbo_state(struct wined3d_context *context, GLenum target,
447 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil, DWORD location)
449 struct fbo_entry *entry, *entry2;
451 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
453 context_destroy_fbo_entry(context, entry);
456 if (context->rebind_fbo)
458 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
459 context->rebind_fbo = FALSE;
462 if (location == SFLAG_INDRAWABLE)
464 context->current_fbo = NULL;
465 context_bind_fbo(context, target, NULL);
467 else
469 context->current_fbo = context_find_fbo_entry(context, target, render_targets, depth_stencil, location);
470 context_apply_fbo_entry(context, target, context->current_fbo);
474 /* GL locking is done by the caller */
475 void context_apply_fbo_state_blit(struct wined3d_context *context, GLenum target,
476 struct wined3d_surface *render_target, struct wined3d_surface *depth_stencil, DWORD location)
478 UINT clear_size = (context->gl_info->limits.buffers - 1) * sizeof(*context->blit_targets);
480 context->blit_targets[0] = render_target;
481 if (clear_size)
482 memset(&context->blit_targets[1], 0, clear_size);
483 context_apply_fbo_state(context, target, context->blit_targets, depth_stencil, location);
486 /* Context activation is done by the caller. */
487 void context_alloc_occlusion_query(struct wined3d_context *context, struct wined3d_occlusion_query *query)
489 const struct wined3d_gl_info *gl_info = context->gl_info;
491 if (context->free_occlusion_query_count)
493 query->id = context->free_occlusion_queries[--context->free_occlusion_query_count];
495 else
497 if (gl_info->supported[ARB_OCCLUSION_QUERY])
499 ENTER_GL();
500 GL_EXTCALL(glGenQueriesARB(1, &query->id));
501 checkGLcall("glGenQueriesARB");
502 LEAVE_GL();
504 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context);
506 else
508 WARN("Occlusion queries not supported, not allocating query id.\n");
509 query->id = 0;
513 query->context = context;
514 list_add_head(&context->occlusion_queries, &query->entry);
517 void context_free_occlusion_query(struct wined3d_occlusion_query *query)
519 struct wined3d_context *context = query->context;
521 list_remove(&query->entry);
522 query->context = NULL;
524 if (context->free_occlusion_query_count >= context->free_occlusion_query_size - 1)
526 UINT new_size = context->free_occlusion_query_size << 1;
527 GLuint *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_occlusion_queries,
528 new_size * sizeof(*context->free_occlusion_queries));
530 if (!new_data)
532 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
533 return;
536 context->free_occlusion_query_size = new_size;
537 context->free_occlusion_queries = new_data;
540 context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
543 /* Context activation is done by the caller. */
544 void context_alloc_event_query(struct wined3d_context *context, struct wined3d_event_query *query)
546 const struct wined3d_gl_info *gl_info = context->gl_info;
548 if (context->free_event_query_count)
550 query->object = context->free_event_queries[--context->free_event_query_count];
552 else
554 if (gl_info->supported[ARB_SYNC])
556 /* Using ARB_sync, not much to do here. */
557 query->object.sync = NULL;
558 TRACE("Allocated event query %p in context %p.\n", query->object.sync, context);
560 else if (gl_info->supported[APPLE_FENCE])
562 ENTER_GL();
563 GL_EXTCALL(glGenFencesAPPLE(1, &query->object.id));
564 checkGLcall("glGenFencesAPPLE");
565 LEAVE_GL();
567 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
569 else if(gl_info->supported[NV_FENCE])
571 ENTER_GL();
572 GL_EXTCALL(glGenFencesNV(1, &query->object.id));
573 checkGLcall("glGenFencesNV");
574 LEAVE_GL();
576 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
578 else
580 WARN("Event queries not supported, not allocating query id.\n");
581 query->object.id = 0;
585 query->context = context;
586 list_add_head(&context->event_queries, &query->entry);
589 void context_free_event_query(struct wined3d_event_query *query)
591 struct wined3d_context *context = query->context;
593 list_remove(&query->entry);
594 query->context = NULL;
596 if (context->free_event_query_count >= context->free_event_query_size - 1)
598 UINT new_size = context->free_event_query_size << 1;
599 union wined3d_gl_query_object *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_event_queries,
600 new_size * sizeof(*context->free_event_queries));
602 if (!new_data)
604 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->object.id, context);
605 return;
608 context->free_event_query_size = new_size;
609 context->free_event_queries = new_data;
612 context->free_event_queries[context->free_event_query_count++] = query->object;
615 typedef void (context_fbo_entry_func_t)(struct wined3d_context *context, struct fbo_entry *entry);
617 static void context_enum_surface_fbo_entries(const struct wined3d_device *device,
618 const struct wined3d_surface *surface, context_fbo_entry_func_t *callback)
620 UINT i;
622 for (i = 0; i < device->context_count; ++i)
624 struct wined3d_context *context = device->contexts[i];
625 const struct wined3d_gl_info *gl_info = context->gl_info;
626 struct fbo_entry *entry, *entry2;
628 if (context->current_rt == surface) context->current_rt = NULL;
630 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
632 UINT j;
634 if (entry->depth_stencil == surface)
636 callback(context, entry);
637 continue;
640 for (j = 0; j < gl_info->limits.buffers; ++j)
642 if (entry->render_targets[j] == surface)
644 callback(context, entry);
645 break;
652 static void context_queue_fbo_entry_destruction(struct wined3d_context *context, struct fbo_entry *entry)
654 list_remove(&entry->entry);
655 list_add_head(&context->fbo_destroy_list, &entry->entry);
658 void context_resource_released(const struct wined3d_device *device,
659 struct wined3d_resource *resource, enum wined3d_resource_type type)
661 if (!device->d3d_initialized) return;
663 switch (type)
665 case WINED3D_RTYPE_SURFACE:
666 context_enum_surface_fbo_entries(device, surface_from_resource(resource),
667 context_queue_fbo_entry_destruction);
668 break;
670 default:
671 break;
675 static void context_detach_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
677 entry->attached = FALSE;
680 void context_resource_unloaded(const struct wined3d_device *device,
681 struct wined3d_resource *resource, enum wined3d_resource_type type)
683 switch (type)
685 case WINED3D_RTYPE_SURFACE:
686 context_enum_surface_fbo_entries(device, surface_from_resource(resource),
687 context_detach_fbo_entry);
688 break;
690 default:
691 break;
695 void context_surface_update(struct wined3d_context *context, const struct wined3d_surface *surface)
697 const struct wined3d_gl_info *gl_info = context->gl_info;
698 struct fbo_entry *entry = context->current_fbo;
699 unsigned int i;
701 if (!entry || context->rebind_fbo) return;
703 for (i = 0; i < gl_info->limits.buffers; ++i)
705 if (surface == entry->render_targets[i])
707 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface, i);
708 context->rebind_fbo = TRUE;
709 return;
713 if (surface == entry->depth_stencil)
715 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface);
716 context->rebind_fbo = TRUE;
720 static BOOL context_set_pixel_format(const struct wined3d_gl_info *gl_info, HDC dc, int format)
722 int current = GetPixelFormat(dc);
724 if (current == format) return TRUE;
726 if (!current)
728 if (!SetPixelFormat(dc, format, NULL))
730 /* This may also happen if the dc belongs to a destroyed window. */
731 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
732 format, dc, GetLastError());
733 return FALSE;
735 return TRUE;
738 /* By default WGL doesn't allow pixel format adjustments but we need it
739 * here. For this reason there's a Wine specific wglSetPixelFormat()
740 * which allows us to set the pixel format multiple times. Only use it
741 * when really needed. */
742 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
744 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
746 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
747 format, dc);
748 return FALSE;
750 return TRUE;
753 /* OpenGL doesn't allow pixel format adjustments. Print an error and
754 * continue using the old format. There's a big chance that the old
755 * format works although with a performance hit and perhaps rendering
756 * errors. */
757 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
758 format, dc, current);
759 return TRUE;
762 static BOOL context_set_gl_context(struct wined3d_context *ctx)
764 struct wined3d_swapchain *swapchain = ctx->swapchain;
765 BOOL backup = FALSE;
767 if (!context_set_pixel_format(ctx->gl_info, ctx->hdc, ctx->pixel_format))
769 WARN("Failed to set pixel format %d on device context %p.\n",
770 ctx->pixel_format, ctx->hdc);
771 backup = TRUE;
774 if (backup || !pwglMakeCurrent(ctx->hdc, ctx->glCtx))
776 HDC dc;
778 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
779 ctx->glCtx, ctx->hdc, GetLastError());
780 ctx->valid = 0;
781 WARN("Trying fallback to the backup window.\n");
783 /* FIXME: If the context is destroyed it's no longer associated with
784 * a swapchain, so we can't use the swapchain to get a backup dc. To
785 * make this work windowless contexts would need to be handled by the
786 * device. */
787 if (ctx->destroyed)
789 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx);
790 context_set_current(NULL);
791 return FALSE;
794 if (!(dc = swapchain_get_backup_dc(swapchain)))
796 context_set_current(NULL);
797 return FALSE;
800 if (!context_set_pixel_format(ctx->gl_info, dc, ctx->pixel_format))
802 ERR("Failed to set pixel format %d on device context %p.\n",
803 ctx->pixel_format, dc);
804 context_set_current(NULL);
805 return FALSE;
808 if (!pwglMakeCurrent(dc, ctx->glCtx))
810 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
811 dc, GetLastError());
812 context_set_current(NULL);
813 return FALSE;
816 return TRUE;
819 static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx, int pf)
821 if (!context_set_pixel_format(gl_info, dc, pf))
823 ERR("Failed to restore pixel format %d on device context %p.\n", pf, dc);
824 context_set_current(NULL);
825 return;
828 if (!pwglMakeCurrent(dc, gl_ctx))
830 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
831 gl_ctx, dc, GetLastError());
832 context_set_current(NULL);
836 static void context_update_window(struct wined3d_context *context)
838 if (context->win_handle == context->swapchain->win_handle)
839 return;
841 TRACE("Updating context %p window from %p to %p.\n",
842 context, context->win_handle, context->swapchain->win_handle);
844 if (context->valid)
846 /* You'd figure ReleaseDC() would fail if the DC doesn't match the
847 * window. However, that's not what actually happens, and there are
848 * user32 tests that confirm ReleaseDC() with the wrong window is
849 * supposed to succeed. So explicitly check that the DC belongs to
850 * the window, since we want to avoid releasing a DC that belongs to
851 * some other window if the original window was already destroyed. */
852 if (WindowFromDC(context->hdc) != context->win_handle)
854 WARN("DC %p does not belong to window %p.\n",
855 context->hdc, context->win_handle);
857 else if (!ReleaseDC(context->win_handle, context->hdc))
859 ERR("Failed to release device context %p, last error %#x.\n",
860 context->hdc, GetLastError());
863 else context->valid = 1;
865 context->win_handle = context->swapchain->win_handle;
867 if (!(context->hdc = GetDC(context->win_handle)))
869 ERR("Failed to get a device context for window %p.\n", context->win_handle);
870 goto err;
873 if (!context_set_pixel_format(context->gl_info, context->hdc, context->pixel_format))
875 ERR("Failed to set pixel format %d on device context %p.\n",
876 context->pixel_format, context->hdc);
877 goto err;
880 context_set_gl_context(context);
882 return;
884 err:
885 context->valid = 0;
888 /* Do not call while under the GL lock. */
889 static void context_destroy_gl_resources(struct wined3d_context *context)
891 const struct wined3d_gl_info *gl_info = context->gl_info;
892 struct wined3d_occlusion_query *occlusion_query;
893 struct wined3d_event_query *event_query;
894 struct fbo_entry *entry, *entry2;
895 HGLRC restore_ctx;
896 HDC restore_dc;
897 unsigned int i;
898 int restore_pf;
900 restore_ctx = pwglGetCurrentContext();
901 restore_dc = pwglGetCurrentDC();
902 restore_pf = GetPixelFormat(restore_dc);
904 if (context->valid && restore_ctx != context->glCtx)
905 context_set_gl_context(context);
906 else restore_ctx = NULL;
908 ENTER_GL();
910 LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
912 if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
913 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query->id));
914 occlusion_query->context = NULL;
917 LIST_FOR_EACH_ENTRY(event_query, &context->event_queries, struct wined3d_event_query, entry)
919 if (context->valid)
921 if (gl_info->supported[ARB_SYNC])
923 if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
925 else if (gl_info->supported[APPLE_FENCE]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query->object.id));
926 else if (gl_info->supported[NV_FENCE]) GL_EXTCALL(glDeleteFencesNV(1, &event_query->object.id));
928 event_query->context = NULL;
931 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
933 if (!context->valid) entry->id = 0;
934 context_destroy_fbo_entry(context, entry);
937 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
939 if (!context->valid) entry->id = 0;
940 context_destroy_fbo_entry(context, entry);
943 if (context->valid)
945 if (context->dummy_arbfp_prog)
947 GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
950 if (gl_info->supported[ARB_OCCLUSION_QUERY])
951 GL_EXTCALL(glDeleteQueriesARB(context->free_occlusion_query_count, context->free_occlusion_queries));
953 if (gl_info->supported[ARB_SYNC])
955 for (i = 0; i < context->free_event_query_count; ++i)
957 GL_EXTCALL(glDeleteSync(context->free_event_queries[i].sync));
960 else if (gl_info->supported[APPLE_FENCE])
962 for (i = 0; i < context->free_event_query_count; ++i)
964 GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_event_queries[i].id));
967 else if (gl_info->supported[NV_FENCE])
969 for (i = 0; i < context->free_event_query_count; ++i)
971 GL_EXTCALL(glDeleteFencesNV(1, &context->free_event_queries[i].id));
975 checkGLcall("context cleanup");
978 LEAVE_GL();
980 HeapFree(GetProcessHeap(), 0, context->free_occlusion_queries);
981 HeapFree(GetProcessHeap(), 0, context->free_event_queries);
983 if (restore_ctx)
985 context_restore_gl_context(gl_info, restore_dc, restore_ctx, restore_pf);
987 else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL, NULL))
989 ERR("Failed to disable GL context.\n");
992 ReleaseDC(context->win_handle, context->hdc);
994 if (!pwglDeleteContext(context->glCtx))
996 DWORD err = GetLastError();
997 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
1001 DWORD context_get_tls_idx(void)
1003 return wined3d_context_tls_idx;
1006 void context_set_tls_idx(DWORD idx)
1008 wined3d_context_tls_idx = idx;
1011 struct wined3d_context *context_get_current(void)
1013 return TlsGetValue(wined3d_context_tls_idx);
1016 /* Do not call while under the GL lock. */
1017 BOOL context_set_current(struct wined3d_context *ctx)
1019 struct wined3d_context *old = context_get_current();
1021 if (old == ctx)
1023 TRACE("Already using D3D context %p.\n", ctx);
1024 return TRUE;
1027 if (old)
1029 if (old->destroyed)
1031 TRACE("Switching away from destroyed context %p.\n", old);
1032 context_destroy_gl_resources(old);
1033 HeapFree(GetProcessHeap(), 0, (void *)old->gl_info);
1034 HeapFree(GetProcessHeap(), 0, old);
1036 else
1038 old->current = 0;
1042 if (ctx)
1044 if (!ctx->valid)
1046 ERR("Trying to make invalid context %p current\n", ctx);
1047 return FALSE;
1050 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
1051 if (!context_set_gl_context(ctx))
1052 return FALSE;
1053 ctx->current = 1;
1055 else if(pwglGetCurrentContext())
1057 TRACE("Clearing current D3D context.\n");
1058 if (!pwglMakeCurrent(NULL, NULL))
1060 DWORD err = GetLastError();
1061 ERR("Failed to clear current GL context, last error %#x.\n", err);
1062 TlsSetValue(wined3d_context_tls_idx, NULL);
1063 return FALSE;
1067 return TlsSetValue(wined3d_context_tls_idx, ctx);
1070 void context_release(struct wined3d_context *context)
1072 TRACE("Releasing context %p, level %u.\n", context, context->level);
1074 if (WARN_ON(d3d))
1076 if (!context->level)
1077 WARN("Context %p is not active.\n", context);
1078 else if (context != context_get_current())
1079 WARN("Context %p is not the current context.\n", context);
1082 if (!--context->level && context->restore_ctx)
1084 TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
1085 context_restore_gl_context(context->gl_info, context->restore_dc, context->restore_ctx, context->restore_pf);
1086 context->restore_ctx = NULL;
1087 context->restore_dc = NULL;
1091 static void context_enter(struct wined3d_context *context)
1093 TRACE("Entering context %p, level %u.\n", context, context->level + 1);
1095 if (!context->level++)
1097 const struct wined3d_context *current_context = context_get_current();
1098 HGLRC current_gl = pwglGetCurrentContext();
1100 if (current_gl && (!current_context || current_context->glCtx != current_gl))
1102 TRACE("Another GL context (%p on device context %p) is already current.\n",
1103 current_gl, pwglGetCurrentDC());
1104 context->restore_ctx = current_gl;
1105 context->restore_dc = pwglGetCurrentDC();
1106 context->restore_pf = GetPixelFormat(context->restore_dc);
1111 void context_invalidate_state(struct wined3d_context *context, DWORD state)
1113 DWORD rep = context->state_table[state].representative;
1114 DWORD idx;
1115 BYTE shift;
1117 if (isStateDirty(context, rep)) return;
1119 context->dirtyArray[context->numDirtyEntries++] = rep;
1120 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
1121 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
1122 context->isStateDirty[idx] |= (1 << shift);
1125 /* This function takes care of wined3d pixel format selection. */
1126 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1127 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1128 BOOL auxBuffers, BOOL findCompatible)
1130 int iPixelFormat=0;
1131 BYTE redBits, greenBits, blueBits, alphaBits, colorBits;
1132 BYTE depthBits=0, stencilBits=0;
1133 unsigned int current_value;
1134 unsigned int cfg_count = device->adapter->cfg_count;
1135 unsigned int i;
1137 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1138 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1139 auxBuffers, findCompatible);
1141 if (!getColorBits(color_format, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits))
1143 ERR("Unable to get color bits for format %s (%#x)!\n",
1144 debug_d3dformat(color_format->id), color_format->id);
1145 return 0;
1148 getDepthStencilBits(ds_format, &depthBits, &stencilBits);
1150 current_value = 0;
1151 for (i = 0; i < cfg_count; ++i)
1153 const struct wined3d_pixel_format *cfg = &device->adapter->cfgs[i];
1154 unsigned int value;
1156 /* For now only accept RGBA formats. Perhaps some day we will
1157 * allow floating point formats for pbuffers. */
1158 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1159 continue;
1160 /* In window mode we need a window drawable format and double buffering. */
1161 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1162 continue;
1163 if (cfg->redSize < redBits)
1164 continue;
1165 if (cfg->greenSize < greenBits)
1166 continue;
1167 if (cfg->blueSize < blueBits)
1168 continue;
1169 if (cfg->alphaSize < alphaBits)
1170 continue;
1171 if (cfg->depthSize < depthBits)
1172 continue;
1173 if (stencilBits && cfg->stencilSize != stencilBits)
1174 continue;
1175 /* Check multisampling support. */
1176 if (cfg->numSamples)
1177 continue;
1179 value = 1;
1180 /* We try to locate a format which matches our requirements exactly. In case of
1181 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1182 if (cfg->depthSize == depthBits)
1183 value += 1;
1184 if (cfg->stencilSize == stencilBits)
1185 value += 2;
1186 if (cfg->alphaSize == alphaBits)
1187 value += 4;
1188 /* We like to have aux buffers in backbuffer mode */
1189 if (auxBuffers && cfg->auxBuffers)
1190 value += 8;
1191 if (cfg->redSize == redBits
1192 && cfg->greenSize == greenBits
1193 && cfg->blueSize == blueBits)
1194 value += 16;
1196 if (value > current_value)
1198 iPixelFormat = cfg->iPixelFormat;
1199 current_value = value;
1203 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1204 if(!iPixelFormat && !findCompatible) {
1205 ERR("Can't find a suitable iPixelFormat\n");
1206 return FALSE;
1207 } else if(!iPixelFormat) {
1208 PIXELFORMATDESCRIPTOR pfd;
1210 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1211 /* PixelFormat selection */
1212 ZeroMemory(&pfd, sizeof(pfd));
1213 pfd.nSize = sizeof(pfd);
1214 pfd.nVersion = 1;
1215 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1216 pfd.iPixelType = PFD_TYPE_RGBA;
1217 pfd.cAlphaBits = alphaBits;
1218 pfd.cColorBits = colorBits;
1219 pfd.cDepthBits = depthBits;
1220 pfd.cStencilBits = stencilBits;
1221 pfd.iLayerType = PFD_MAIN_PLANE;
1223 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
1224 if(!iPixelFormat) {
1225 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1226 ERR("Can't find a suitable iPixelFormat\n");
1227 return FALSE;
1231 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1232 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1233 return iPixelFormat;
1236 /* GL locking is done by the caller */
1237 static void bind_dummy_textures(const struct wined3d_device *device, const struct wined3d_context *context)
1239 const struct wined3d_gl_info *gl_info = context->gl_info;
1240 unsigned int i, count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1242 for (i = 0; i < count; ++i)
1244 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1245 checkGLcall("glActiveTextureARB");
1247 glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
1248 checkGLcall("glBindTexture");
1250 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1252 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
1253 checkGLcall("glBindTexture");
1256 if (gl_info->supported[EXT_TEXTURE3D])
1258 glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1259 checkGLcall("glBindTexture");
1262 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1264 glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1265 checkGLcall("glBindTexture");
1270 /* Do not call while under the GL lock. */
1271 struct wined3d_context *context_create(struct wined3d_swapchain *swapchain,
1272 struct wined3d_surface *target, const struct wined3d_format *ds_format)
1274 struct wined3d_device *device = swapchain->device;
1275 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1276 const struct wined3d_format *color_format;
1277 struct wined3d_context *ret;
1278 BOOL auxBuffers = FALSE;
1279 int pixel_format;
1280 unsigned int s;
1281 int swap_interval;
1282 DWORD state;
1283 HGLRC ctx;
1284 HDC hdc;
1286 TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
1288 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
1289 if (!ret)
1291 ERR("Failed to allocate context memory.\n");
1292 return NULL;
1295 ret->blit_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1296 gl_info->limits.buffers * sizeof(*ret->blit_targets));
1297 if (!ret->blit_targets)
1298 goto out;
1300 ret->draw_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1301 gl_info->limits.buffers * sizeof(*ret->draw_buffers));
1302 if (!ret->draw_buffers)
1303 goto out;
1305 ret->free_occlusion_query_size = 4;
1306 ret->free_occlusion_queries = HeapAlloc(GetProcessHeap(), 0,
1307 ret->free_occlusion_query_size * sizeof(*ret->free_occlusion_queries));
1308 if (!ret->free_occlusion_queries)
1309 goto out;
1311 list_init(&ret->occlusion_queries);
1313 ret->free_event_query_size = 4;
1314 ret->free_event_queries = HeapAlloc(GetProcessHeap(), 0,
1315 ret->free_event_query_size * sizeof(*ret->free_event_queries));
1316 if (!ret->free_event_queries)
1317 goto out;
1319 list_init(&ret->event_queries);
1320 list_init(&ret->fbo_list);
1321 list_init(&ret->fbo_destroy_list);
1323 if (!(hdc = GetDC(swapchain->win_handle)))
1325 WARN("Failed to retireve device context, trying swapchain backup.\n");
1327 if (!(hdc = swapchain_get_backup_dc(swapchain)))
1329 ERR("Failed to retrieve a device context.\n");
1330 goto out;
1334 color_format = target->resource.format;
1336 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1337 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1338 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1340 auxBuffers = TRUE;
1342 if (color_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1343 color_format = wined3d_get_format(gl_info, WINED3DFMT_B4G4R4A4_UNORM);
1344 else if (color_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1345 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
1348 /* DirectDraw supports 8bit paletted render targets and these are used by
1349 * old games like StarCraft and C&C. Most modern hardware doesn't support
1350 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1351 * conversion (ab)uses the alpha component for storing the palette index.
1352 * For this reason we require a format with 8bit alpha, so request
1353 * A8R8G8B8. */
1354 if (color_format->id == WINED3DFMT_P8_UINT)
1355 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
1357 /* Try to find a pixel format which matches our requirements. */
1358 pixel_format = context_choose_pixel_format(device, hdc, color_format, ds_format, auxBuffers, FALSE);
1360 /* Try to locate a compatible format if we weren't able to find anything. */
1361 if (!pixel_format)
1363 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1364 pixel_format = context_choose_pixel_format(device, hdc, color_format, ds_format, auxBuffers, TRUE);
1367 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1368 if (!pixel_format)
1370 ERR("Can't find a suitable pixel format.\n");
1371 goto out;
1374 context_enter(ret);
1376 if (!context_set_pixel_format(gl_info, hdc, pixel_format))
1378 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format, hdc);
1379 context_release(ret);
1380 goto out;
1383 if (!(ctx = pwglCreateContext(hdc)))
1385 ERR("Failed to create a WGL context.\n");
1386 context_release(ret);
1387 goto out;
1390 if (device->context_count)
1392 if (!pwglShareLists(device->contexts[0]->glCtx, ctx))
1394 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1395 device->contexts[0]->glCtx, ctx, GetLastError());
1396 context_release(ret);
1397 if (!pwglDeleteContext(ctx))
1398 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1399 goto out;
1403 if (!device_context_add(device, ret))
1405 ERR("Failed to add the newly created context to the context list\n");
1406 context_release(ret);
1407 if (!pwglDeleteContext(ctx))
1408 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1409 goto out;
1412 ret->gl_info = gl_info;
1413 ret->state_table = device->StateTable;
1415 /* Mark all states dirty to force a proper initialization of the states
1416 * on the first use of the context. */
1417 for (state = 0; state <= STATE_HIGHEST; ++state)
1419 if (ret->state_table[state].representative)
1420 context_invalidate_state(ret, state);
1423 ret->swapchain = swapchain;
1424 ret->current_rt = target;
1425 ret->tid = GetCurrentThreadId();
1427 ret->render_offscreen = surface_is_offscreen(target);
1428 ret->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
1429 ret->valid = 1;
1431 ret->glCtx = ctx;
1432 ret->win_handle = swapchain->win_handle;
1433 ret->hdc = hdc;
1434 ret->pixel_format = pixel_format;
1436 /* Set up the context defaults */
1437 if (!context_set_current(ret))
1439 ERR("Cannot activate context to set up defaults.\n");
1440 device_context_remove(device, ret);
1441 context_release(ret);
1442 if (!pwglDeleteContext(ctx))
1443 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1444 goto out;
1447 switch (swapchain->desc.swap_interval)
1449 case WINED3DPRESENT_INTERVAL_IMMEDIATE:
1450 swap_interval = 0;
1451 break;
1452 case WINED3DPRESENT_INTERVAL_DEFAULT:
1453 case WINED3DPRESENT_INTERVAL_ONE:
1454 swap_interval = 1;
1455 break;
1456 case WINED3DPRESENT_INTERVAL_TWO:
1457 swap_interval = 2;
1458 break;
1459 case WINED3DPRESENT_INTERVAL_THREE:
1460 swap_interval = 3;
1461 break;
1462 case WINED3DPRESENT_INTERVAL_FOUR:
1463 swap_interval = 4;
1464 break;
1465 default:
1466 FIXME("Unknown swap interval %#x.\n", swapchain->desc.swap_interval);
1467 swap_interval = 1;
1470 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
1472 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
1473 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1474 swap_interval, ret, GetLastError());
1477 ENTER_GL();
1479 glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
1481 TRACE("Setting up the screen\n");
1482 /* Clear the screen */
1483 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
1484 checkGLcall("glClearColor");
1485 glClearIndex(0);
1486 glClearDepth(1);
1487 glClearStencil(0xffff);
1489 checkGLcall("glClear");
1491 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1492 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1494 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1495 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1497 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1498 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1500 glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
1501 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1502 glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);
1503 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1505 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1507 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1508 * and textures in DIB sections(due to the memory protection).
1510 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1511 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1513 if (gl_info->supported[ARB_VERTEX_BLEND])
1515 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1516 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1517 * GL_VERTEX_BLEND_ARB isn't enabled too
1519 glEnable(GL_WEIGHT_SUM_UNITY_ARB);
1520 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1522 if (gl_info->supported[NV_TEXTURE_SHADER2])
1524 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1525 * the previous texture where to source the offset from is always unit - 1.
1527 for (s = 1; s < gl_info->limits.textures; ++s)
1529 context_active_texture(ret, gl_info, s);
1530 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
1531 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1534 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
1536 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1537 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1538 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1539 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1540 * is ever assigned.
1542 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1543 * program and the dummy program is destroyed when the context is destroyed.
1545 const char *dummy_program =
1546 "!!ARBfp1.0\n"
1547 "MOV result.color, fragment.color.primary;\n"
1548 "END\n";
1549 GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
1550 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
1551 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
1554 if (gl_info->supported[ARB_POINT_SPRITE])
1556 for (s = 0; s < gl_info->limits.textures; ++s)
1558 context_active_texture(ret, gl_info, s);
1559 glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
1560 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1564 if (gl_info->supported[ARB_PROVOKING_VERTEX])
1566 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
1568 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
1570 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
1572 device->frag_pipe->enable_extension(TRUE);
1574 /* If this happens to be the first context for the device, dummy textures
1575 * are not created yet. In that case, they will be created (and bound) by
1576 * create_dummy_textures right after this context is initialized. */
1577 if (device->dummy_texture_2d[0])
1578 bind_dummy_textures(device, ret);
1580 LEAVE_GL();
1582 TRACE("Created context %p.\n", ret);
1584 return ret;
1586 out:
1587 HeapFree(GetProcessHeap(), 0, ret->free_event_queries);
1588 HeapFree(GetProcessHeap(), 0, ret->free_occlusion_queries);
1589 HeapFree(GetProcessHeap(), 0, ret->draw_buffers);
1590 HeapFree(GetProcessHeap(), 0, ret->blit_targets);
1591 HeapFree(GetProcessHeap(), 0, ret);
1592 return NULL;
1595 /* Do not call while under the GL lock. */
1596 void context_destroy(struct wined3d_device *device, struct wined3d_context *context)
1598 BOOL destroy;
1600 TRACE("Destroying ctx %p\n", context);
1602 if (context->tid == GetCurrentThreadId() || !context->current)
1604 context_destroy_gl_resources(context);
1605 TlsSetValue(wined3d_context_tls_idx, NULL);
1606 destroy = TRUE;
1608 else
1610 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1611 in wined3d_adapter may go away in the meantime */
1612 struct wined3d_gl_info *gl_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info));
1613 *gl_info = *context->gl_info;
1614 context->gl_info = gl_info;
1615 context->destroyed = 1;
1616 destroy = FALSE;
1619 HeapFree(GetProcessHeap(), 0, context->draw_buffers);
1620 HeapFree(GetProcessHeap(), 0, context->blit_targets);
1621 device_context_remove(device, context);
1622 if (destroy) HeapFree(GetProcessHeap(), 0, context);
1625 /* GL locking is done by the caller */
1626 static void set_blit_dimension(UINT width, UINT height)
1628 const GLdouble projection[] =
1630 2.0 / width, 0.0, 0.0, 0.0,
1631 0.0, 2.0 / height, 0.0, 0.0,
1632 0.0, 0.0, 2.0, 0.0,
1633 -1.0, -1.0, -1.0, 1.0,
1636 glMatrixMode(GL_PROJECTION);
1637 checkGLcall("glMatrixMode(GL_PROJECTION)");
1638 glLoadMatrixd(projection);
1639 checkGLcall("glLoadMatrixd");
1640 glViewport(0, 0, width, height);
1641 checkGLcall("glViewport");
1644 static void context_get_rt_size(const struct wined3d_context *context, SIZE *size)
1646 const struct wined3d_surface *rt = context->current_rt;
1648 if (rt->container.type == WINED3D_CONTAINER_SWAPCHAIN
1649 && rt->container.u.swapchain->front_buffer == rt)
1651 RECT window_size;
1653 GetClientRect(context->win_handle, &window_size);
1654 size->cx = window_size.right - window_size.left;
1655 size->cy = window_size.bottom - window_size.top;
1657 return;
1660 size->cx = rt->resource.width;
1661 size->cy = rt->resource.height;
1664 /*****************************************************************************
1665 * SetupForBlit
1667 * Sets up a context for DirectDraw blitting.
1668 * All texture units are disabled, texture unit 0 is set as current unit
1669 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1670 * color writing enabled for all channels
1671 * register combiners disabled, shaders disabled
1672 * world matrix is set to identity, texture matrix 0 too
1673 * projection matrix is setup for drawing screen coordinates
1675 * Params:
1676 * This: Device to activate the context for
1677 * context: Context to setup
1679 *****************************************************************************/
1680 /* Context activation is done by the caller. */
1681 static void SetupForBlit(const struct wined3d_device *device, struct wined3d_context *context)
1683 int i;
1684 const struct wined3d_gl_info *gl_info = context->gl_info;
1685 DWORD sampler;
1686 SIZE rt_size;
1688 TRACE("Setting up context %p for blitting\n", context);
1690 context_get_rt_size(context, &rt_size);
1692 if (context->last_was_blit)
1694 if (context->blit_w != rt_size.cx || context->blit_h != rt_size.cy)
1696 ENTER_GL();
1697 set_blit_dimension(rt_size.cx, rt_size.cy);
1698 LEAVE_GL();
1699 context->blit_w = rt_size.cx;
1700 context->blit_h = rt_size.cy;
1701 /* No need to dirtify here, the states are still dirtified because
1702 * they weren't applied since the last SetupForBlit() call. */
1704 TRACE("Context is already set up for blitting, nothing to do\n");
1705 return;
1707 context->last_was_blit = TRUE;
1709 /* TODO: Use a display list */
1711 /* Disable shaders */
1712 ENTER_GL();
1713 device->shader_backend->shader_select(context, FALSE, FALSE);
1714 LEAVE_GL();
1716 context_invalidate_state(context, STATE_VSHADER);
1717 context_invalidate_state(context, STATE_PIXELSHADER);
1719 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1720 * helper functions in between gl calls. This function is full of context_invalidate_state
1721 * which can safely be called from here, we only lock once instead locking/unlocking
1722 * after each GL call.
1724 ENTER_GL();
1726 /* Disable all textures. The caller can then bind a texture it wants to blit
1727 * from
1729 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1730 * function texture unit. No need to care for higher samplers
1732 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
1734 sampler = device->rev_tex_unit_map[i];
1735 context_active_texture(context, gl_info, i);
1737 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1739 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1740 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1742 glDisable(GL_TEXTURE_3D);
1743 checkGLcall("glDisable GL_TEXTURE_3D");
1744 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1746 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1747 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1749 glDisable(GL_TEXTURE_2D);
1750 checkGLcall("glDisable GL_TEXTURE_2D");
1752 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1753 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1755 if (sampler != WINED3D_UNMAPPED_STAGE)
1757 if (sampler < MAX_TEXTURES)
1758 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
1759 context_invalidate_state(context, STATE_SAMPLER(sampler));
1762 context_active_texture(context, gl_info, 0);
1764 sampler = device->rev_tex_unit_map[0];
1766 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1768 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1769 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1771 glDisable(GL_TEXTURE_3D);
1772 checkGLcall("glDisable GL_TEXTURE_3D");
1773 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1775 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1776 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1778 glDisable(GL_TEXTURE_2D);
1779 checkGLcall("glDisable GL_TEXTURE_2D");
1781 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1783 glMatrixMode(GL_TEXTURE);
1784 checkGLcall("glMatrixMode(GL_TEXTURE)");
1785 glLoadIdentity();
1786 checkGLcall("glLoadIdentity()");
1788 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1790 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1791 GL_TEXTURE_LOD_BIAS_EXT,
1792 0.0f);
1793 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1796 if (sampler != WINED3D_UNMAPPED_STAGE)
1798 if (sampler < MAX_TEXTURES)
1800 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
1801 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
1803 context_invalidate_state(context, STATE_SAMPLER(sampler));
1806 /* Other misc states */
1807 glDisable(GL_ALPHA_TEST);
1808 checkGLcall("glDisable(GL_ALPHA_TEST)");
1809 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
1810 glDisable(GL_LIGHTING);
1811 checkGLcall("glDisable GL_LIGHTING");
1812 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
1813 glDisable(GL_DEPTH_TEST);
1814 checkGLcall("glDisable GL_DEPTH_TEST");
1815 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZENABLE));
1816 glDisableWINE(GL_FOG);
1817 checkGLcall("glDisable GL_FOG");
1818 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
1819 glDisable(GL_BLEND);
1820 checkGLcall("glDisable GL_BLEND");
1821 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
1822 glDisable(GL_CULL_FACE);
1823 checkGLcall("glDisable GL_CULL_FACE");
1824 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CULLMODE));
1825 glDisable(GL_STENCIL_TEST);
1826 checkGLcall("glDisable GL_STENCIL_TEST");
1827 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1828 glDisable(GL_SCISSOR_TEST);
1829 checkGLcall("glDisable GL_SCISSOR_TEST");
1830 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
1831 if (gl_info->supported[ARB_POINT_SPRITE])
1833 glDisable(GL_POINT_SPRITE_ARB);
1834 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1835 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
1837 glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
1838 checkGLcall("glColorMask");
1839 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
1840 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
1841 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
1842 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
1843 if (gl_info->supported[EXT_SECONDARY_COLOR])
1845 glDisable(GL_COLOR_SUM_EXT);
1846 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
1847 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1850 /* Setup transforms */
1851 glMatrixMode(GL_MODELVIEW);
1852 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1853 glLoadIdentity();
1854 checkGLcall("glLoadIdentity()");
1855 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
1857 context->last_was_rhw = TRUE;
1858 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
1860 glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
1861 glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
1862 glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
1863 glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
1864 glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
1865 glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
1866 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
1868 set_blit_dimension(rt_size.cx, rt_size.cy);
1869 device->frag_pipe->enable_extension(FALSE);
1871 LEAVE_GL();
1873 context->blit_w = rt_size.cx;
1874 context->blit_h = rt_size.cy;
1875 context_invalidate_state(context, STATE_VIEWPORT);
1876 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
1879 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
1881 return rt_mask & (1 << 31);
1884 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
1886 return rt_mask & ~(1 << 31);
1889 /* Context activation and GL locking are done by the caller. */
1890 static void context_apply_draw_buffers(struct wined3d_context *context, DWORD rt_mask)
1892 if (!rt_mask)
1894 glDrawBuffer(GL_NONE);
1895 checkGLcall("glDrawBuffer()");
1897 else if (is_rt_mask_onscreen(rt_mask))
1899 glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
1900 checkGLcall("glDrawBuffer()");
1902 else
1904 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
1906 const struct wined3d_gl_info *gl_info = context->gl_info;
1907 unsigned int i = 0;
1909 while (rt_mask)
1911 if (rt_mask & 1)
1912 context->draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
1913 else
1914 context->draw_buffers[i] = GL_NONE;
1916 rt_mask >>= 1;
1917 ++i;
1920 if (gl_info->supported[ARB_DRAW_BUFFERS])
1922 GL_EXTCALL(glDrawBuffersARB(i, context->draw_buffers));
1923 checkGLcall("glDrawBuffers()");
1925 else
1927 glDrawBuffer(context->draw_buffers[0]);
1928 checkGLcall("glDrawBuffer()");
1931 else
1933 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
1938 /* GL locking is done by the caller. */
1939 void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
1941 glDrawBuffer(buffer);
1942 checkGLcall("glDrawBuffer()");
1943 if (context->current_fbo)
1944 context->current_fbo->rt_mask = context_generate_rt_mask(buffer);
1945 else
1946 context->draw_buffers_mask = context_generate_rt_mask(buffer);
1949 /* GL locking is done by the caller. */
1950 void context_active_texture(struct wined3d_context *context, const struct wined3d_gl_info *gl_info, unsigned int unit)
1952 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0 + unit));
1953 checkGLcall("glActiveTextureARB");
1954 context->active_texture = unit;
1957 void context_bind_texture(struct wined3d_context *context, GLenum target, GLuint name)
1959 DWORD unit = context->active_texture;
1960 DWORD old_texture_type = context->texture_type[unit];
1962 if (name)
1964 glBindTexture(target, name);
1965 checkGLcall("glBindTexture");
1967 else
1969 target = GL_NONE;
1972 if (old_texture_type != target)
1974 const struct wined3d_device *device = context->swapchain->device;
1976 switch (old_texture_type)
1978 case GL_NONE:
1979 /* nothing to do */
1980 break;
1981 case GL_TEXTURE_2D:
1982 glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[unit]);
1983 checkGLcall("glBindTexture");
1984 break;
1985 case GL_TEXTURE_RECTANGLE_ARB:
1986 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[unit]);
1987 checkGLcall("glBindTexture");
1988 break;
1989 case GL_TEXTURE_CUBE_MAP:
1990 glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[unit]);
1991 checkGLcall("glBindTexture");
1992 break;
1993 case GL_TEXTURE_3D:
1994 glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[unit]);
1995 checkGLcall("glBindTexture");
1996 break;
1997 default:
1998 ERR("Unexpected texture target %#x\n", old_texture_type);
2001 context->texture_type[unit] = target;
2005 static void context_set_render_offscreen(struct wined3d_context *context, BOOL offscreen)
2007 if (context->render_offscreen == offscreen) return;
2009 context_invalidate_state(context, STATE_POINTSPRITECOORDORIGIN);
2010 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2011 context_invalidate_state(context, STATE_VIEWPORT);
2012 context_invalidate_state(context, STATE_SCISSORRECT);
2013 context_invalidate_state(context, STATE_FRONTFACE);
2014 context->render_offscreen = offscreen;
2017 static BOOL match_depth_stencil_format(const struct wined3d_format *existing,
2018 const struct wined3d_format *required)
2020 BYTE existing_depth, existing_stencil, required_depth, required_stencil;
2022 if (existing == required) return TRUE;
2023 if ((existing->flags & WINED3DFMT_FLAG_FLOAT) != (required->flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
2025 getDepthStencilBits(existing, &existing_depth, &existing_stencil);
2026 getDepthStencilBits(required, &required_depth, &required_stencil);
2028 if(existing_depth < required_depth) return FALSE;
2029 /* If stencil bits are used the exact amount is required - otherwise wrapping
2030 * won't work correctly */
2031 if(required_stencil && required_stencil != existing_stencil) return FALSE;
2032 return TRUE;
2035 /* The caller provides a context */
2036 static void context_validate_onscreen_formats(struct wined3d_context *context,
2037 const struct wined3d_surface *depth_stencil)
2039 /* Onscreen surfaces are always in a swapchain */
2040 struct wined3d_swapchain *swapchain = context->current_rt->container.u.swapchain;
2042 if (context->render_offscreen || !depth_stencil) return;
2043 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->resource.format)) return;
2045 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2046 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2047 * format. */
2048 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2050 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2051 surface_load_location(context->current_rt, SFLAG_INTEXTURE, NULL);
2052 swapchain->render_to_fbo = TRUE;
2053 swapchain_update_draw_bindings(swapchain);
2054 context_set_render_offscreen(context, TRUE);
2057 static DWORD context_generate_rt_mask_no_fbo(const struct wined3d_device *device, const struct wined3d_surface *rt)
2059 if (!rt || rt->resource.format->id == WINED3DFMT_NULL)
2060 return 0;
2061 else if (rt->container.type == WINED3D_CONTAINER_SWAPCHAIN)
2062 return context_generate_rt_mask_from_surface(rt);
2063 else
2064 return context_generate_rt_mask(device->offscreenBuffer);
2067 /* Context activation is done by the caller. */
2068 void context_apply_blit_state(struct wined3d_context *context, const struct wined3d_device *device)
2070 struct wined3d_surface *rt = context->current_rt;
2071 DWORD rt_mask, *cur_mask;
2073 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2075 context_validate_onscreen_formats(context, NULL);
2077 if (context->render_offscreen)
2079 surface_internal_preload(rt, SRGB_RGB);
2081 ENTER_GL();
2082 context_apply_fbo_state_blit(context, GL_FRAMEBUFFER, rt, NULL, rt->draw_binding);
2083 LEAVE_GL();
2084 if (rt->resource.format->id != WINED3DFMT_NULL)
2085 rt_mask = 1;
2086 else
2087 rt_mask = 0;
2089 else
2091 ENTER_GL();
2092 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
2093 LEAVE_GL();
2094 rt_mask = context_generate_rt_mask_from_surface(rt);
2097 else
2099 rt_mask = context_generate_rt_mask_no_fbo(device, rt);
2102 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2104 ENTER_GL();
2105 if (rt_mask != *cur_mask)
2107 context_apply_draw_buffers(context, rt_mask);
2108 *cur_mask = rt_mask;
2111 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2113 context_check_fbo_status(context, GL_FRAMEBUFFER);
2115 LEAVE_GL();
2117 SetupForBlit(device, context);
2118 context_invalidate_state(context, STATE_FRAMEBUFFER);
2121 static BOOL context_validate_rt_config(UINT rt_count,
2122 struct wined3d_surface * const *rts, const struct wined3d_surface *ds)
2124 unsigned int i;
2126 if (ds) return TRUE;
2128 for (i = 0; i < rt_count; ++i)
2130 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL)
2131 return TRUE;
2134 WARN("Invalid render target config, need at least one attachment.\n");
2135 return FALSE;
2138 /* Context activation is done by the caller. */
2139 BOOL context_apply_clear_state(struct wined3d_context *context, const struct wined3d_device *device,
2140 UINT rt_count, const struct wined3d_fb_state *fb)
2142 DWORD rt_mask = 0, *cur_mask;
2143 UINT i;
2144 struct wined3d_surface **rts = fb->render_targets;
2146 if (isStateDirty(context, STATE_FRAMEBUFFER) || fb != &device->fb
2147 || rt_count != context->gl_info->limits.buffers)
2149 if (!context_validate_rt_config(rt_count, rts, fb->depth_stencil))
2150 return FALSE;
2152 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2154 context_validate_onscreen_formats(context, fb->depth_stencil);
2156 ENTER_GL();
2158 if (!rt_count || surface_is_offscreen(rts[0]))
2160 for (i = 0; i < rt_count; ++i)
2162 context->blit_targets[i] = rts[i];
2163 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL)
2164 rt_mask |= (1 << i);
2166 while (i < context->gl_info->limits.buffers)
2168 context->blit_targets[i] = NULL;
2169 ++i;
2171 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets, fb->depth_stencil,
2172 rt_count ? rts[0]->draw_binding : SFLAG_INTEXTURE);
2173 glReadBuffer(GL_NONE);
2174 checkGLcall("glReadBuffer");
2176 else
2178 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL, SFLAG_INDRAWABLE);
2179 rt_mask = context_generate_rt_mask_from_surface(rts[0]);
2182 LEAVE_GL();
2184 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2185 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2186 * state management allows this */
2187 context_invalidate_state(context, STATE_FRAMEBUFFER);
2189 else
2191 rt_mask = context_generate_rt_mask_no_fbo(device, rt_count ? rts[0] : NULL);
2194 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2195 && (!rt_count || surface_is_offscreen(rts[0])))
2197 for (i = 0; i < rt_count; ++i)
2199 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL) rt_mask |= (1 << i);
2202 else
2204 rt_mask = context_generate_rt_mask_no_fbo(device, rt_count ? rts[0] : NULL);
2207 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2209 ENTER_GL();
2210 if (rt_mask != *cur_mask)
2212 context_apply_draw_buffers(context, rt_mask);
2213 *cur_mask = rt_mask;
2214 context_invalidate_state(context, STATE_FRAMEBUFFER);
2217 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2219 context_check_fbo_status(context, GL_FRAMEBUFFER);
2222 if (context->last_was_blit)
2224 device->frag_pipe->enable_extension(TRUE);
2225 context->last_was_blit = FALSE;
2228 /* Blending and clearing should be orthogonal, but tests on the nvidia
2229 * driver show that disabling blending when clearing improves the clearing
2230 * performance incredibly. */
2231 glDisable(GL_BLEND);
2232 glEnable(GL_SCISSOR_TEST);
2233 checkGLcall("glEnable GL_SCISSOR_TEST");
2234 LEAVE_GL();
2236 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2237 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2238 context_invalidate_state(context, STATE_SCISSORRECT);
2240 return TRUE;
2243 static DWORD find_draw_buffers_mask(const struct wined3d_context *context, const struct wined3d_device *device)
2245 const struct wined3d_state *state = &device->stateBlock->state;
2246 struct wined3d_surface **rts = state->fb->render_targets;
2247 struct wined3d_shader *ps = state->pixel_shader;
2248 DWORD rt_mask, rt_mask_bits;
2249 unsigned int i;
2251 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return context_generate_rt_mask_no_fbo(device, rts[0]);
2252 else if (!context->render_offscreen) return context_generate_rt_mask_from_surface(rts[0]);
2254 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
2255 rt_mask &= device->valid_rt_mask;
2256 rt_mask_bits = rt_mask;
2257 i = 0;
2258 while (rt_mask_bits)
2260 rt_mask_bits &= ~(1 << i);
2261 if (!rts[i] || rts[i]->resource.format->id == WINED3DFMT_NULL)
2262 rt_mask &= ~(1 << i);
2264 i++;
2267 return rt_mask;
2270 /* GL locking and context activation are done by the caller */
2271 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
2273 const struct wined3d_device *device = context->swapchain->device;
2274 const struct wined3d_fb_state *fb = state->fb;
2275 DWORD rt_mask = find_draw_buffers_mask(context, device);
2276 DWORD *cur_mask;
2278 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2280 if (!context->render_offscreen)
2282 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL, SFLAG_INDRAWABLE);
2284 else
2286 context_apply_fbo_state(context, GL_FRAMEBUFFER, fb->render_targets, fb->depth_stencil,
2287 fb->render_targets[0]->draw_binding);
2288 glReadBuffer(GL_NONE);
2289 checkGLcall("glReadBuffer");
2293 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2294 if (rt_mask != *cur_mask)
2296 context_apply_draw_buffers(context, rt_mask);
2297 *cur_mask = rt_mask;
2301 /* GL locking and context activation are done by the caller */
2302 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
2304 const struct wined3d_device *device = context->swapchain->device;
2305 DWORD rt_mask, *cur_mask;
2307 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
2309 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2310 rt_mask = find_draw_buffers_mask(context, device);
2311 if (rt_mask != *cur_mask)
2313 context_apply_draw_buffers(context, rt_mask);
2314 *cur_mask = rt_mask;
2318 /* Context activation is done by the caller. */
2319 BOOL context_apply_draw_state(struct wined3d_context *context, struct wined3d_device *device)
2321 const struct wined3d_state *state = &device->stateBlock->state;
2322 const struct StateEntry *state_table = context->state_table;
2323 const struct wined3d_fb_state *fb = state->fb;
2324 unsigned int i;
2326 if (!context_validate_rt_config(context->gl_info->limits.buffers,
2327 fb->render_targets, fb->depth_stencil))
2328 return FALSE;
2330 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && isStateDirty(context, STATE_FRAMEBUFFER))
2332 context_validate_onscreen_formats(context, fb->depth_stencil);
2335 /* Preload resources before FBO setup. Texture preload in particular may
2336 * result in changes to the current FBO, due to using e.g. FBO blits for
2337 * updating a resource location. */
2338 device_update_tex_unit_map(device);
2339 device_preload_textures(device);
2340 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC))
2341 device_update_stream_info(device, context->gl_info);
2342 if (state->index_buffer && !state->user_stream)
2344 if (device->strided_streams.all_vbo)
2345 wined3d_buffer_preload(state->index_buffer);
2346 else
2347 buffer_get_sysmem(state->index_buffer, context->gl_info);
2350 ENTER_GL();
2351 if (context->last_was_blit)
2353 device->frag_pipe->enable_extension(TRUE);
2356 for (i = 0; i < context->numDirtyEntries; ++i)
2358 DWORD rep = context->dirtyArray[i];
2359 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
2360 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
2361 context->isStateDirty[idx] &= ~(1 << shift);
2362 state_table[rep].apply(context, state, rep);
2365 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2367 context_check_fbo_status(context, GL_FRAMEBUFFER);
2370 LEAVE_GL();
2371 context->numDirtyEntries = 0; /* This makes the whole list clean */
2372 context->last_was_blit = FALSE;
2374 return TRUE;
2377 static void context_setup_target(struct wined3d_context *context, struct wined3d_surface *target)
2379 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
2381 render_offscreen = surface_is_offscreen(target);
2382 if (context->current_rt == target && render_offscreen == old_render_offscreen) return;
2384 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2385 * the alpha blend state changes with different render target formats. */
2386 if (!context->current_rt)
2388 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2390 else
2392 const struct wined3d_format *old = context->current_rt->resource.format;
2393 const struct wined3d_format *new = target->resource.format;
2395 if (old->id != new->id)
2397 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2398 if ((old->alpha_mask && !new->alpha_mask) || (!old->alpha_mask && new->alpha_mask)
2399 || !(new->flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
2400 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2402 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2403 if ((old->flags & WINED3DFMT_FLAG_SRGB_WRITE) != (new->flags & WINED3DFMT_FLAG_SRGB_WRITE))
2404 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
2407 /* When switching away from an offscreen render target, and we're not
2408 * using FBOs, we have to read the drawable into the texture. This is
2409 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2410 * are some things that need care though. PreLoad needs a GL context,
2411 * and FindContext is called before the context is activated. It also
2412 * has to be called with the old rendertarget active, otherwise a
2413 * wrong drawable is read. */
2414 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
2415 && old_render_offscreen && context->current_rt != target)
2417 /* Read the back buffer of the old drawable into the destination texture. */
2418 if (context->current_rt->texture_name_srgb)
2419 surface_internal_preload(context->current_rt, SRGB_SRGB);
2420 surface_internal_preload(context->current_rt, SRGB_RGB);
2421 surface_modify_location(context->current_rt, SFLAG_INDRAWABLE, FALSE);
2425 context->current_rt = target;
2426 context_set_render_offscreen(context, render_offscreen);
2429 /* Do not call while under the GL lock. */
2430 struct wined3d_context *context_acquire(const struct wined3d_device *device, struct wined3d_surface *target)
2432 struct wined3d_context *current_context = context_get_current();
2433 struct wined3d_context *context;
2435 TRACE("device %p, target %p.\n", device, target);
2437 if (current_context && current_context->destroyed)
2438 current_context = NULL;
2440 if (!target)
2442 if (current_context
2443 && current_context->current_rt
2444 && current_context->swapchain->device == device)
2446 target = current_context->current_rt;
2448 else
2450 struct wined3d_swapchain *swapchain = device->swapchains[0];
2451 if (swapchain->back_buffers)
2452 target = swapchain->back_buffers[0];
2453 else
2454 target = swapchain->front_buffer;
2458 if (current_context && current_context->current_rt == target)
2460 context = current_context;
2462 else if (target->container.type == WINED3D_CONTAINER_SWAPCHAIN)
2464 TRACE("Rendering onscreen.\n");
2466 context = swapchain_get_context(target->container.u.swapchain);
2468 else
2470 TRACE("Rendering offscreen.\n");
2472 /* Stay with the current context if possible. Otherwise use the
2473 * context for the primary swapchain. */
2474 if (current_context && current_context->swapchain->device == device)
2475 context = current_context;
2476 else
2477 context = swapchain_get_context(device->swapchains[0]);
2480 context_update_window(context);
2481 context_setup_target(context, target);
2482 context_enter(context);
2483 if (!context->valid) return context;
2485 if (context != current_context)
2487 if (!context_set_current(context))
2489 ERR("Failed to activate the new context.\n");
2491 else
2493 ENTER_GL();
2494 device->frag_pipe->enable_extension(!context->last_was_blit);
2495 LEAVE_GL();
2498 else if (context->restore_ctx)
2500 context_set_gl_context(context);
2503 return context;