[WINED3D]
[reactos.git] / reactos / dll / directx / wine / wined3d / context.c
1 /*
2 * Context and render target management in wined3d
3 *
4 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
5 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "wined3d_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
25 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous);
27
28 #define WINED3D_MAX_FBO_ENTRIES 64
29
30 static DWORD wined3d_context_tls_idx;
31
32 /* FBO helper functions */
33
34 /* Context activation is done by the caller. */
35 static void context_bind_fbo(struct wined3d_context *context, GLenum target, GLuint fbo)
36 {
37 const struct wined3d_gl_info *gl_info = context->gl_info;
38
39 switch (target)
40 {
41 case GL_READ_FRAMEBUFFER:
42 if (context->fbo_read_binding == fbo) return;
43 context->fbo_read_binding = fbo;
44 break;
45
46 case GL_DRAW_FRAMEBUFFER:
47 if (context->fbo_draw_binding == fbo) return;
48 context->fbo_draw_binding = fbo;
49 break;
50
51 case GL_FRAMEBUFFER:
52 if (context->fbo_read_binding == fbo
53 && context->fbo_draw_binding == fbo) return;
54 context->fbo_read_binding = fbo;
55 context->fbo_draw_binding = fbo;
56 break;
57
58 default:
59 FIXME("Unhandled target %#x.\n", target);
60 break;
61 }
62
63 gl_info->fbo_ops.glBindFramebuffer(target, fbo);
64 checkGLcall("glBindFramebuffer()");
65 }
66
67 /* Context activation is done by the caller. */
68 static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info, GLenum target)
69 {
70 unsigned int i;
71
72 for (i = 0; i < gl_info->limits.buffers; ++i)
73 {
74 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
75 checkGLcall("glFramebufferTexture2D()");
76 }
77 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
78 checkGLcall("glFramebufferTexture2D()");
79
80 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
81 checkGLcall("glFramebufferTexture2D()");
82 }
83
84 /* Context activation is done by the caller. */
85 static void context_destroy_fbo(struct wined3d_context *context, GLuint fbo)
86 {
87 const struct wined3d_gl_info *gl_info = context->gl_info;
88
89 context_bind_fbo(context, GL_FRAMEBUFFER, fbo);
90 context_clean_fbo_attachments(gl_info, GL_FRAMEBUFFER);
91 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
92
93 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
94 checkGLcall("glDeleteFramebuffers()");
95 }
96
97 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info *gl_info,
98 GLenum fbo_target, DWORD format_flags, GLuint rb)
99 {
100 if (format_flags & WINED3DFMT_FLAG_DEPTH)
101 {
102 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
103 checkGLcall("glFramebufferRenderbuffer()");
104 }
105
106 if (format_flags & WINED3DFMT_FLAG_STENCIL)
107 {
108 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
109 checkGLcall("glFramebufferRenderbuffer()");
110 }
111 }
112
113 /* Context activation is done by the caller. */
114 static void context_attach_depth_stencil_fbo(struct wined3d_context *context,
115 GLenum fbo_target, struct wined3d_surface *depth_stencil, DWORD location)
116 {
117 const struct wined3d_gl_info *gl_info = context->gl_info;
118
119 TRACE("Attach depth stencil %p\n", depth_stencil);
120
121 if (depth_stencil)
122 {
123 DWORD format_flags = depth_stencil->resource.format->flags;
124
125 if (depth_stencil->current_renderbuffer)
126 {
127 context_attach_depth_stencil_rb(gl_info, fbo_target,
128 format_flags, depth_stencil->current_renderbuffer->id);
129 }
130 else
131 {
132 switch (location)
133 {
134 case SFLAG_INTEXTURE:
135 case SFLAG_INSRGBTEX:
136 surface_prepare_texture(depth_stencil, context, FALSE);
137
138 if (format_flags & WINED3DFMT_FLAG_DEPTH)
139 {
140 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT,
141 depth_stencil->texture_target, depth_stencil->texture_name,
142 depth_stencil->texture_level);
143 checkGLcall("glFramebufferTexture2D()");
144 }
145
146 if (format_flags & WINED3DFMT_FLAG_STENCIL)
147 {
148 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT,
149 depth_stencil->texture_target, depth_stencil->texture_name,
150 depth_stencil->texture_level);
151 checkGLcall("glFramebufferTexture2D()");
152 }
153 break;
154
155 case SFLAG_INRB_MULTISAMPLE:
156 surface_prepare_rb(depth_stencil, gl_info, TRUE);
157 context_attach_depth_stencil_rb(gl_info, fbo_target,
158 format_flags, depth_stencil->rb_multisample);
159 break;
160
161 case SFLAG_INRB_RESOLVED:
162 surface_prepare_rb(depth_stencil, gl_info, FALSE);
163 context_attach_depth_stencil_rb(gl_info, fbo_target,
164 format_flags, depth_stencil->rb_resolved);
165 break;
166
167 default:
168 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location), location);
169 break;
170 }
171 }
172
173 if (!(format_flags & WINED3DFMT_FLAG_DEPTH))
174 {
175 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
176 checkGLcall("glFramebufferTexture2D()");
177 }
178
179 if (!(format_flags & WINED3DFMT_FLAG_STENCIL))
180 {
181 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
182 checkGLcall("glFramebufferTexture2D()");
183 }
184 }
185 else
186 {
187 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
188 checkGLcall("glFramebufferTexture2D()");
189
190 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
191 checkGLcall("glFramebufferTexture2D()");
192 }
193 }
194
195 /* Context activation is done by the caller. */
196 static void context_attach_surface_fbo(struct wined3d_context *context,
197 GLenum fbo_target, DWORD idx, struct wined3d_surface *surface, DWORD location)
198 {
199 const struct wined3d_gl_info *gl_info = context->gl_info;
200
201 TRACE("Attach surface %p to %u\n", surface, idx);
202
203 if (surface && surface->resource.format->id != WINED3DFMT_NULL)
204 {
205 BOOL srgb;
206
207 switch (location)
208 {
209 case SFLAG_INTEXTURE:
210 case SFLAG_INSRGBTEX:
211 srgb = location == SFLAG_INSRGBTEX;
212 surface_prepare_texture(surface, context, srgb);
213 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
214 surface->texture_target, surface_get_texture_name(surface, gl_info, srgb),
215 surface->texture_level);
216 checkGLcall("glFramebufferTexture2D()");
217 break;
218
219 case SFLAG_INRB_MULTISAMPLE:
220 surface_prepare_rb(surface, gl_info, TRUE);
221 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
222 GL_RENDERBUFFER, surface->rb_multisample);
223 checkGLcall("glFramebufferRenderbuffer()");
224 break;
225
226 case SFLAG_INRB_RESOLVED:
227 surface_prepare_rb(surface, gl_info, FALSE);
228 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
229 GL_RENDERBUFFER, surface->rb_resolved);
230 checkGLcall("glFramebufferRenderbuffer()");
231 break;
232
233 default:
234 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location), location);
235 break;
236 }
237 }
238 else
239 {
240 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx, GL_TEXTURE_2D, 0, 0);
241 checkGLcall("glFramebufferTexture2D()");
242 }
243 }
244
245 /* Context activation is done by the caller. */
246 void context_check_fbo_status(const struct wined3d_context *context, GLenum target)
247 {
248 const struct wined3d_gl_info *gl_info = context->gl_info;
249 GLenum status;
250
251 if (!FIXME_ON(d3d)) return;
252
253 status = gl_info->fbo_ops.glCheckFramebufferStatus(target);
254 if (status == GL_FRAMEBUFFER_COMPLETE)
255 {
256 TRACE("FBO complete\n");
257 }
258 else
259 {
260 const struct wined3d_surface *attachment;
261 unsigned int i;
262
263 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status), status);
264
265 if (!context->current_fbo)
266 {
267 ERR("FBO 0 is incomplete, driver bug?\n");
268 return;
269 }
270
271 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context->current_fbo->location),
272 context->current_fbo->location);
273
274 /* Dump the FBO attachments */
275 for (i = 0; i < gl_info->limits.buffers; ++i)
276 {
277 attachment = context->current_fbo->render_targets[i];
278 if (attachment)
279 {
280 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
281 i, attachment, debug_d3dformat(attachment->resource.format->id),
282 attachment->pow2Width, attachment->pow2Height, attachment->resource.multisample_type);
283 }
284 }
285 attachment = context->current_fbo->depth_stencil;
286 if (attachment)
287 {
288 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
289 attachment, debug_d3dformat(attachment->resource.format->id),
290 attachment->pow2Width, attachment->pow2Height, attachment->resource.multisample_type);
291 }
292 }
293 }
294
295 static inline DWORD context_generate_rt_mask(GLenum buffer)
296 {
297 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
298 return buffer ? (1 << 31) | buffer : 0;
299 }
300
301 static inline DWORD context_generate_rt_mask_from_surface(const struct wined3d_surface *target)
302 {
303 return (1 << 31) | surface_get_gl_buffer(target);
304 }
305
306 static struct fbo_entry *context_create_fbo_entry(const struct wined3d_context *context,
307 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil, DWORD location)
308 {
309 const struct wined3d_gl_info *gl_info = context->gl_info;
310 struct fbo_entry *entry;
311
312 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
313 entry->render_targets = HeapAlloc(GetProcessHeap(), 0, gl_info->limits.buffers * sizeof(*entry->render_targets));
314 memcpy(entry->render_targets, render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
315 entry->depth_stencil = depth_stencil;
316 entry->location = location;
317 entry->rt_mask = context_generate_rt_mask(GL_COLOR_ATTACHMENT0);
318 entry->attached = FALSE;
319 gl_info->fbo_ops.glGenFramebuffers(1, &entry->id);
320 checkGLcall("glGenFramebuffers()");
321 TRACE("Created FBO %u.\n", entry->id);
322
323 return entry;
324 }
325
326 /* Context activation is done by the caller. */
327 static void context_reuse_fbo_entry(struct wined3d_context *context, GLenum target,
328 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil,
329 DWORD location, struct fbo_entry *entry)
330 {
331 const struct wined3d_gl_info *gl_info = context->gl_info;
332
333 context_bind_fbo(context, target, entry->id);
334 context_clean_fbo_attachments(gl_info, target);
335
336 memcpy(entry->render_targets, render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
337 entry->depth_stencil = depth_stencil;
338 entry->location = location;
339 entry->attached = FALSE;
340 }
341
342 /* Context activation is done by the caller. */
343 static void context_destroy_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
344 {
345 if (entry->id)
346 {
347 TRACE("Destroy FBO %u.\n", entry->id);
348 context_destroy_fbo(context, entry->id);
349 }
350 --context->fbo_entry_count;
351 list_remove(&entry->entry);
352 HeapFree(GetProcessHeap(), 0, entry->render_targets);
353 HeapFree(GetProcessHeap(), 0, entry);
354 }
355
356 /* Context activation is done by the caller. */
357 static struct fbo_entry *context_find_fbo_entry(struct wined3d_context *context, GLenum target,
358 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil, DWORD location)
359 {
360 const struct wined3d_gl_info *gl_info = context->gl_info;
361 struct fbo_entry *entry;
362
363 if (depth_stencil && render_targets && render_targets[0])
364 {
365 if (depth_stencil->resource.width < render_targets[0]->resource.width ||
366 depth_stencil->resource.height < render_targets[0]->resource.height)
367 {
368 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
369 depth_stencil = NULL;
370 }
371 }
372
373 LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
374 {
375 if (!memcmp(entry->render_targets,
376 render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets))
377 && entry->depth_stencil == depth_stencil && entry->location == location)
378 {
379 list_remove(&entry->entry);
380 list_add_head(&context->fbo_list, &entry->entry);
381 return entry;
382 }
383 }
384
385 if (context->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
386 {
387 entry = context_create_fbo_entry(context, render_targets, depth_stencil, location);
388 list_add_head(&context->fbo_list, &entry->entry);
389 ++context->fbo_entry_count;
390 }
391 else
392 {
393 entry = LIST_ENTRY(list_tail(&context->fbo_list), struct fbo_entry, entry);
394 context_reuse_fbo_entry(context, target, render_targets, depth_stencil, location, entry);
395 list_remove(&entry->entry);
396 list_add_head(&context->fbo_list, &entry->entry);
397 }
398
399 return entry;
400 }
401
402 /* Context activation is done by the caller. */
403 static void context_apply_fbo_entry(struct wined3d_context *context, GLenum target, struct fbo_entry *entry)
404 {
405 const struct wined3d_gl_info *gl_info = context->gl_info;
406 unsigned int i;
407 GLuint read_binding, draw_binding;
408
409 if (entry->attached)
410 {
411 context_bind_fbo(context, target, entry->id);
412 return;
413 }
414
415 read_binding = context->fbo_read_binding;
416 draw_binding = context->fbo_draw_binding;
417 context_bind_fbo(context, GL_FRAMEBUFFER, entry->id);
418
419 /* Apply render targets */
420 for (i = 0; i < gl_info->limits.buffers; ++i)
421 {
422 context_attach_surface_fbo(context, target, i, entry->render_targets[i], entry->location);
423 }
424
425 /* Apply depth targets */
426 if (entry->depth_stencil)
427 surface_set_compatible_renderbuffer(entry->depth_stencil, entry->render_targets[0]);
428 context_attach_depth_stencil_fbo(context, target, entry->depth_stencil, entry->location);
429
430 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
431 * GL contexts requirements. */
432 glReadBuffer(GL_NONE);
433 context_set_draw_buffer(context, GL_NONE);
434 if (target != GL_FRAMEBUFFER)
435 {
436 if (target == GL_READ_FRAMEBUFFER)
437 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, draw_binding);
438 else
439 context_bind_fbo(context, GL_READ_FRAMEBUFFER, read_binding);
440 }
441
442 entry->attached = TRUE;
443 }
444
445 /* Context activation 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)
448 {
449 struct fbo_entry *entry, *entry2;
450
451 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
452 {
453 context_destroy_fbo_entry(context, entry);
454 }
455
456 if (context->rebind_fbo)
457 {
458 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
459 context->rebind_fbo = FALSE;
460 }
461
462 if (location == SFLAG_INDRAWABLE)
463 {
464 context->current_fbo = NULL;
465 context_bind_fbo(context, target, 0);
466 }
467 else
468 {
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);
471 }
472 }
473
474 /* Context activation 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)
477 {
478 UINT clear_size = (context->gl_info->limits.buffers - 1) * sizeof(*context->blit_targets);
479
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);
484 }
485
486 /* Context activation is done by the caller. */
487 void context_alloc_occlusion_query(struct wined3d_context *context, struct wined3d_occlusion_query *query)
488 {
489 const struct wined3d_gl_info *gl_info = context->gl_info;
490
491 if (context->free_occlusion_query_count)
492 {
493 query->id = context->free_occlusion_queries[--context->free_occlusion_query_count];
494 }
495 else
496 {
497 if (gl_info->supported[ARB_OCCLUSION_QUERY])
498 {
499 GL_EXTCALL(glGenQueriesARB(1, &query->id));
500 checkGLcall("glGenQueriesARB");
501
502 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context);
503 }
504 else
505 {
506 WARN("Occlusion queries not supported, not allocating query id.\n");
507 query->id = 0;
508 }
509 }
510
511 query->context = context;
512 list_add_head(&context->occlusion_queries, &query->entry);
513 }
514
515 void context_free_occlusion_query(struct wined3d_occlusion_query *query)
516 {
517 struct wined3d_context *context = query->context;
518
519 list_remove(&query->entry);
520 query->context = NULL;
521
522 if (context->free_occlusion_query_count >= context->free_occlusion_query_size - 1)
523 {
524 UINT new_size = context->free_occlusion_query_size << 1;
525 GLuint *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_occlusion_queries,
526 new_size * sizeof(*context->free_occlusion_queries));
527
528 if (!new_data)
529 {
530 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
531 return;
532 }
533
534 context->free_occlusion_query_size = new_size;
535 context->free_occlusion_queries = new_data;
536 }
537
538 context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
539 }
540
541 /* Context activation is done by the caller. */
542 void context_alloc_event_query(struct wined3d_context *context, struct wined3d_event_query *query)
543 {
544 const struct wined3d_gl_info *gl_info = context->gl_info;
545
546 if (context->free_event_query_count)
547 {
548 query->object = context->free_event_queries[--context->free_event_query_count];
549 }
550 else
551 {
552 if (gl_info->supported[ARB_SYNC])
553 {
554 /* Using ARB_sync, not much to do here. */
555 query->object.sync = NULL;
556 TRACE("Allocated event query %p in context %p.\n", query->object.sync, context);
557 }
558 else if (gl_info->supported[APPLE_FENCE])
559 {
560 GL_EXTCALL(glGenFencesAPPLE(1, &query->object.id));
561 checkGLcall("glGenFencesAPPLE");
562
563 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
564 }
565 else if(gl_info->supported[NV_FENCE])
566 {
567 GL_EXTCALL(glGenFencesNV(1, &query->object.id));
568 checkGLcall("glGenFencesNV");
569
570 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
571 }
572 else
573 {
574 WARN("Event queries not supported, not allocating query id.\n");
575 query->object.id = 0;
576 }
577 }
578
579 query->context = context;
580 list_add_head(&context->event_queries, &query->entry);
581 }
582
583 void context_free_event_query(struct wined3d_event_query *query)
584 {
585 struct wined3d_context *context = query->context;
586
587 list_remove(&query->entry);
588 query->context = NULL;
589
590 if (context->free_event_query_count >= context->free_event_query_size - 1)
591 {
592 UINT new_size = context->free_event_query_size << 1;
593 union wined3d_gl_query_object *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_event_queries,
594 new_size * sizeof(*context->free_event_queries));
595
596 if (!new_data)
597 {
598 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->object.id, context);
599 return;
600 }
601
602 context->free_event_query_size = new_size;
603 context->free_event_queries = new_data;
604 }
605
606 context->free_event_queries[context->free_event_query_count++] = query->object;
607 }
608
609 typedef void (context_fbo_entry_func_t)(struct wined3d_context *context, struct fbo_entry *entry);
610
611 static void context_enum_surface_fbo_entries(const struct wined3d_device *device,
612 const struct wined3d_surface *surface, context_fbo_entry_func_t *callback)
613 {
614 UINT i;
615
616 for (i = 0; i < device->context_count; ++i)
617 {
618 struct wined3d_context *context = device->contexts[i];
619 const struct wined3d_gl_info *gl_info = context->gl_info;
620 struct fbo_entry *entry, *entry2;
621
622 if (context->current_rt == surface) context->current_rt = NULL;
623
624 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
625 {
626 UINT j;
627
628 if (entry->depth_stencil == surface)
629 {
630 callback(context, entry);
631 continue;
632 }
633
634 for (j = 0; j < gl_info->limits.buffers; ++j)
635 {
636 if (entry->render_targets[j] == surface)
637 {
638 callback(context, entry);
639 break;
640 }
641 }
642 }
643 }
644 }
645
646 static void context_queue_fbo_entry_destruction(struct wined3d_context *context, struct fbo_entry *entry)
647 {
648 list_remove(&entry->entry);
649 list_add_head(&context->fbo_destroy_list, &entry->entry);
650 }
651
652 void context_resource_released(const struct wined3d_device *device,
653 struct wined3d_resource *resource, enum wined3d_resource_type type)
654 {
655 if (!device->d3d_initialized) return;
656
657 switch (type)
658 {
659 case WINED3D_RTYPE_SURFACE:
660 context_enum_surface_fbo_entries(device, surface_from_resource(resource),
661 context_queue_fbo_entry_destruction);
662 break;
663
664 default:
665 break;
666 }
667 }
668
669 static void context_detach_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
670 {
671 entry->attached = FALSE;
672 }
673
674 void context_resource_unloaded(const struct wined3d_device *device,
675 struct wined3d_resource *resource, enum wined3d_resource_type type)
676 {
677 switch (type)
678 {
679 case WINED3D_RTYPE_SURFACE:
680 context_enum_surface_fbo_entries(device, surface_from_resource(resource),
681 context_detach_fbo_entry);
682 break;
683
684 default:
685 break;
686 }
687 }
688
689 void context_surface_update(struct wined3d_context *context, const struct wined3d_surface *surface)
690 {
691 const struct wined3d_gl_info *gl_info = context->gl_info;
692 struct fbo_entry *entry = context->current_fbo;
693 unsigned int i;
694
695 if (!entry || context->rebind_fbo) return;
696
697 for (i = 0; i < gl_info->limits.buffers; ++i)
698 {
699 if (surface == entry->render_targets[i])
700 {
701 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface, i);
702 context->rebind_fbo = TRUE;
703 return;
704 }
705 }
706
707 if (surface == entry->depth_stencil)
708 {
709 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface);
710 context->rebind_fbo = TRUE;
711 }
712 }
713
714 static BOOL context_set_pixel_format(const struct wined3d_gl_info *gl_info, HDC dc, int format)
715 {
716 int current = GetPixelFormat(dc);
717
718 if (current == format) return TRUE;
719
720 if (!current)
721 {
722 if (!SetPixelFormat(dc, format, NULL))
723 {
724 /* This may also happen if the dc belongs to a destroyed window. */
725 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
726 format, dc, GetLastError());
727 return FALSE;
728 }
729 return TRUE;
730 }
731
732 /* By default WGL doesn't allow pixel format adjustments but we need it
733 * here. For this reason there's a Wine specific wglSetPixelFormat()
734 * which allows us to set the pixel format multiple times. Only use it
735 * when really needed. */
736 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
737 {
738 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
739 {
740 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
741 format, dc);
742 return FALSE;
743 }
744 return TRUE;
745 }
746
747 /* OpenGL doesn't allow pixel format adjustments. Print an error and
748 * continue using the old format. There's a big chance that the old
749 * format works although with a performance hit and perhaps rendering
750 * errors. */
751 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
752 format, dc, current);
753 return TRUE;
754 }
755
756 static BOOL context_set_gl_context(struct wined3d_context *ctx)
757 {
758 struct wined3d_swapchain *swapchain = ctx->swapchain;
759 BOOL backup = FALSE;
760
761 if (!context_set_pixel_format(ctx->gl_info, ctx->hdc, ctx->pixel_format))
762 {
763 WARN("Failed to set pixel format %d on device context %p.\n",
764 ctx->pixel_format, ctx->hdc);
765 backup = TRUE;
766 }
767
768 if (backup || !wglMakeCurrent(ctx->hdc, ctx->glCtx))
769 {
770 HDC dc;
771
772 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
773 ctx->glCtx, ctx->hdc, GetLastError());
774 ctx->valid = 0;
775 WARN("Trying fallback to the backup window.\n");
776
777 /* FIXME: If the context is destroyed it's no longer associated with
778 * a swapchain, so we can't use the swapchain to get a backup dc. To
779 * make this work windowless contexts would need to be handled by the
780 * device. */
781 if (ctx->destroyed)
782 {
783 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx);
784 context_set_current(NULL);
785 return FALSE;
786 }
787
788 if (!(dc = swapchain_get_backup_dc(swapchain)))
789 {
790 context_set_current(NULL);
791 return FALSE;
792 }
793
794 if (!context_set_pixel_format(ctx->gl_info, dc, ctx->pixel_format))
795 {
796 ERR("Failed to set pixel format %d on device context %p.\n",
797 ctx->pixel_format, dc);
798 context_set_current(NULL);
799 return FALSE;
800 }
801
802 if (!wglMakeCurrent(dc, ctx->glCtx))
803 {
804 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
805 dc, GetLastError());
806 context_set_current(NULL);
807 return FALSE;
808 }
809 }
810 return TRUE;
811 }
812
813 static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx, int pf)
814 {
815 if (!context_set_pixel_format(gl_info, dc, pf))
816 {
817 ERR("Failed to restore pixel format %d on device context %p.\n", pf, dc);
818 context_set_current(NULL);
819 return;
820 }
821
822 if (!wglMakeCurrent(dc, gl_ctx))
823 {
824 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
825 gl_ctx, dc, GetLastError());
826 context_set_current(NULL);
827 }
828 }
829
830 static void context_update_window(struct wined3d_context *context)
831 {
832 if (context->win_handle == context->swapchain->win_handle)
833 return;
834
835 TRACE("Updating context %p window from %p to %p.\n",
836 context, context->win_handle, context->swapchain->win_handle);
837
838 if (context->valid)
839 {
840 /* You'd figure ReleaseDC() would fail if the DC doesn't match the
841 * window. However, that's not what actually happens, and there are
842 * user32 tests that confirm ReleaseDC() with the wrong window is
843 * supposed to succeed. So explicitly check that the DC belongs to
844 * the window, since we want to avoid releasing a DC that belongs to
845 * some other window if the original window was already destroyed. */
846 if (WindowFromDC(context->hdc) != context->win_handle)
847 {
848 WARN("DC %p does not belong to window %p.\n",
849 context->hdc, context->win_handle);
850 }
851 else if (!ReleaseDC(context->win_handle, context->hdc))
852 {
853 ERR("Failed to release device context %p, last error %#x.\n",
854 context->hdc, GetLastError());
855 }
856 }
857 else context->valid = 1;
858
859 context->win_handle = context->swapchain->win_handle;
860
861 if (!(context->hdc = GetDC(context->win_handle)))
862 {
863 ERR("Failed to get a device context for window %p.\n", context->win_handle);
864 goto err;
865 }
866
867 if (!context_set_pixel_format(context->gl_info, context->hdc, context->pixel_format))
868 {
869 ERR("Failed to set pixel format %d on device context %p.\n",
870 context->pixel_format, context->hdc);
871 goto err;
872 }
873
874 context_set_gl_context(context);
875
876 return;
877
878 err:
879 context->valid = 0;
880 }
881
882 /* Do not call while under the GL lock. */
883 static void context_destroy_gl_resources(struct wined3d_context *context)
884 {
885 const struct wined3d_gl_info *gl_info = context->gl_info;
886 struct wined3d_occlusion_query *occlusion_query;
887 struct wined3d_event_query *event_query;
888 struct fbo_entry *entry, *entry2;
889 HGLRC restore_ctx;
890 HDC restore_dc;
891 unsigned int i;
892 int restore_pf;
893
894 restore_ctx = wglGetCurrentContext();
895 restore_dc = wglGetCurrentDC();
896 restore_pf = GetPixelFormat(restore_dc);
897
898 if (context->valid && restore_ctx != context->glCtx)
899 context_set_gl_context(context);
900 else
901 restore_ctx = NULL;
902
903 LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
904 {
905 if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
906 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query->id));
907 occlusion_query->context = NULL;
908 }
909
910 LIST_FOR_EACH_ENTRY(event_query, &context->event_queries, struct wined3d_event_query, entry)
911 {
912 if (context->valid)
913 {
914 if (gl_info->supported[ARB_SYNC])
915 {
916 if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
917 }
918 else if (gl_info->supported[APPLE_FENCE]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query->object.id));
919 else if (gl_info->supported[NV_FENCE]) GL_EXTCALL(glDeleteFencesNV(1, &event_query->object.id));
920 }
921 event_query->context = NULL;
922 }
923
924 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
925 {
926 if (!context->valid) entry->id = 0;
927 context_destroy_fbo_entry(context, entry);
928 }
929
930 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
931 {
932 if (!context->valid) entry->id = 0;
933 context_destroy_fbo_entry(context, entry);
934 }
935
936 if (context->valid)
937 {
938 if (context->dummy_arbfp_prog)
939 {
940 GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
941 }
942
943 if (gl_info->supported[ARB_OCCLUSION_QUERY])
944 GL_EXTCALL(glDeleteQueriesARB(context->free_occlusion_query_count, context->free_occlusion_queries));
945
946 if (gl_info->supported[ARB_SYNC])
947 {
948 for (i = 0; i < context->free_event_query_count; ++i)
949 {
950 GL_EXTCALL(glDeleteSync(context->free_event_queries[i].sync));
951 }
952 }
953 else if (gl_info->supported[APPLE_FENCE])
954 {
955 for (i = 0; i < context->free_event_query_count; ++i)
956 {
957 GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_event_queries[i].id));
958 }
959 }
960 else if (gl_info->supported[NV_FENCE])
961 {
962 for (i = 0; i < context->free_event_query_count; ++i)
963 {
964 GL_EXTCALL(glDeleteFencesNV(1, &context->free_event_queries[i].id));
965 }
966 }
967
968 checkGLcall("context cleanup");
969 }
970
971 HeapFree(GetProcessHeap(), 0, context->free_occlusion_queries);
972 HeapFree(GetProcessHeap(), 0, context->free_event_queries);
973
974 if (restore_ctx)
975 {
976 context_restore_gl_context(gl_info, restore_dc, restore_ctx, restore_pf);
977 }
978 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
979 {
980 ERR("Failed to disable GL context.\n");
981 }
982
983 ReleaseDC(context->win_handle, context->hdc);
984
985 if (!wglDeleteContext(context->glCtx))
986 {
987 DWORD err = GetLastError();
988 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
989 }
990 }
991
992 DWORD context_get_tls_idx(void)
993 {
994 return wined3d_context_tls_idx;
995 }
996
997 void context_set_tls_idx(DWORD idx)
998 {
999 wined3d_context_tls_idx = idx;
1000 }
1001
1002 struct wined3d_context *context_get_current(void)
1003 {
1004 return TlsGetValue(wined3d_context_tls_idx);
1005 }
1006
1007 /* Do not call while under the GL lock. */
1008 BOOL context_set_current(struct wined3d_context *ctx)
1009 {
1010 struct wined3d_context *old = context_get_current();
1011
1012 if (old == ctx)
1013 {
1014 TRACE("Already using D3D context %p.\n", ctx);
1015 return TRUE;
1016 }
1017
1018 if (old)
1019 {
1020 if (old->destroyed)
1021 {
1022 TRACE("Switching away from destroyed context %p.\n", old);
1023 context_destroy_gl_resources(old);
1024 HeapFree(GetProcessHeap(), 0, (void *)old->gl_info);
1025 HeapFree(GetProcessHeap(), 0, old);
1026 }
1027 else
1028 {
1029 old->current = 0;
1030 }
1031 }
1032
1033 if (ctx)
1034 {
1035 if (!ctx->valid)
1036 {
1037 ERR("Trying to make invalid context %p current\n", ctx);
1038 return FALSE;
1039 }
1040
1041 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
1042 if (!context_set_gl_context(ctx))
1043 return FALSE;
1044 ctx->current = 1;
1045 }
1046 else if(wglGetCurrentContext())
1047 {
1048 TRACE("Clearing current D3D context.\n");
1049 if (!wglMakeCurrent(NULL, NULL))
1050 {
1051 DWORD err = GetLastError();
1052 ERR("Failed to clear current GL context, last error %#x.\n", err);
1053 TlsSetValue(wined3d_context_tls_idx, NULL);
1054 return FALSE;
1055 }
1056 }
1057
1058 return TlsSetValue(wined3d_context_tls_idx, ctx);
1059 }
1060
1061 void context_release(struct wined3d_context *context)
1062 {
1063 TRACE("Releasing context %p, level %u.\n", context, context->level);
1064
1065 if (WARN_ON(d3d))
1066 {
1067 if (!context->level)
1068 WARN("Context %p is not active.\n", context);
1069 else if (context != context_get_current())
1070 WARN("Context %p is not the current context.\n", context);
1071 }
1072
1073 if (!--context->level && context->restore_ctx)
1074 {
1075 TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
1076 context_restore_gl_context(context->gl_info, context->restore_dc, context->restore_ctx, context->restore_pf);
1077 context->restore_ctx = NULL;
1078 context->restore_dc = NULL;
1079 }
1080 }
1081
1082 static void context_enter(struct wined3d_context *context)
1083 {
1084 TRACE("Entering context %p, level %u.\n", context, context->level + 1);
1085
1086 if (!context->level++)
1087 {
1088 const struct wined3d_context *current_context = context_get_current();
1089 HGLRC current_gl = wglGetCurrentContext();
1090
1091 if (current_gl && (!current_context || current_context->glCtx != current_gl))
1092 {
1093 TRACE("Another GL context (%p on device context %p) is already current.\n",
1094 current_gl, wglGetCurrentDC());
1095 context->restore_ctx = current_gl;
1096 context->restore_dc = wglGetCurrentDC();
1097 context->restore_pf = GetPixelFormat(context->restore_dc);
1098 }
1099 }
1100 }
1101
1102 void context_invalidate_state(struct wined3d_context *context, DWORD state)
1103 {
1104 DWORD rep = context->state_table[state].representative;
1105 DWORD idx;
1106 BYTE shift;
1107
1108 if (isStateDirty(context, rep)) return;
1109
1110 context->dirtyArray[context->numDirtyEntries++] = rep;
1111 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
1112 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
1113 context->isStateDirty[idx] |= (1 << shift);
1114 }
1115
1116 /* This function takes care of wined3d pixel format selection. */
1117 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1118 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1119 BOOL auxBuffers, BOOL findCompatible)
1120 {
1121 int iPixelFormat=0;
1122 BYTE redBits, greenBits, blueBits, alphaBits, colorBits;
1123 BYTE depthBits=0, stencilBits=0;
1124 unsigned int current_value;
1125 unsigned int cfg_count = device->adapter->cfg_count;
1126 unsigned int i;
1127
1128 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1129 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1130 auxBuffers, findCompatible);
1131
1132 if (!getColorBits(color_format, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits))
1133 {
1134 ERR("Unable to get color bits for format %s (%#x)!\n",
1135 debug_d3dformat(color_format->id), color_format->id);
1136 return 0;
1137 }
1138
1139 getDepthStencilBits(ds_format, &depthBits, &stencilBits);
1140
1141 current_value = 0;
1142 for (i = 0; i < cfg_count; ++i)
1143 {
1144 const struct wined3d_pixel_format *cfg = &device->adapter->cfgs[i];
1145 unsigned int value;
1146
1147 /* For now only accept RGBA formats. Perhaps some day we will
1148 * allow floating point formats for pbuffers. */
1149 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1150 continue;
1151 /* In window mode we need a window drawable format and double buffering. */
1152 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1153 continue;
1154 if (cfg->redSize < redBits)
1155 continue;
1156 if (cfg->greenSize < greenBits)
1157 continue;
1158 if (cfg->blueSize < blueBits)
1159 continue;
1160 if (cfg->alphaSize < alphaBits)
1161 continue;
1162 if (cfg->depthSize < depthBits)
1163 continue;
1164 if (stencilBits && cfg->stencilSize != stencilBits)
1165 continue;
1166 /* Check multisampling support. */
1167 if (cfg->numSamples)
1168 continue;
1169
1170 value = 1;
1171 /* We try to locate a format which matches our requirements exactly. In case of
1172 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1173 if (cfg->depthSize == depthBits)
1174 value += 1;
1175 if (cfg->stencilSize == stencilBits)
1176 value += 2;
1177 if (cfg->alphaSize == alphaBits)
1178 value += 4;
1179 /* We like to have aux buffers in backbuffer mode */
1180 if (auxBuffers && cfg->auxBuffers)
1181 value += 8;
1182 if (cfg->redSize == redBits
1183 && cfg->greenSize == greenBits
1184 && cfg->blueSize == blueBits)
1185 value += 16;
1186
1187 if (value > current_value)
1188 {
1189 iPixelFormat = cfg->iPixelFormat;
1190 current_value = value;
1191 }
1192 }
1193
1194 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1195 if(!iPixelFormat && !findCompatible) {
1196 ERR("Can't find a suitable iPixelFormat\n");
1197 return FALSE;
1198 } else if(!iPixelFormat) {
1199 PIXELFORMATDESCRIPTOR pfd;
1200
1201 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1202 /* PixelFormat selection */
1203 ZeroMemory(&pfd, sizeof(pfd));
1204 pfd.nSize = sizeof(pfd);
1205 pfd.nVersion = 1;
1206 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1207 pfd.iPixelType = PFD_TYPE_RGBA;
1208 pfd.cAlphaBits = alphaBits;
1209 pfd.cColorBits = colorBits;
1210 pfd.cDepthBits = depthBits;
1211 pfd.cStencilBits = stencilBits;
1212 pfd.iLayerType = PFD_MAIN_PLANE;
1213
1214 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
1215 if(!iPixelFormat) {
1216 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1217 ERR("Can't find a suitable iPixelFormat\n");
1218 return FALSE;
1219 }
1220 }
1221
1222 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1223 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1224 return iPixelFormat;
1225 }
1226
1227 /* Context activation is done by the caller. */
1228 static void bind_dummy_textures(const struct wined3d_device *device, const struct wined3d_context *context)
1229 {
1230 const struct wined3d_gl_info *gl_info = context->gl_info;
1231 unsigned int i, count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1232
1233 for (i = 0; i < count; ++i)
1234 {
1235 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1236 checkGLcall("glActiveTextureARB");
1237
1238 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
1239 checkGLcall("glBindTexture");
1240
1241 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1242 {
1243 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
1244 checkGLcall("glBindTexture");
1245 }
1246
1247 if (gl_info->supported[EXT_TEXTURE3D])
1248 {
1249 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1250 checkGLcall("glBindTexture");
1251 }
1252
1253 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1254 {
1255 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1256 checkGLcall("glBindTexture");
1257 }
1258 }
1259 }
1260
1261 BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1262 {
1263 return gl_info->supported[ARB_DEBUG_OUTPUT]
1264 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1265 }
1266
1267 static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1268 GLenum severity, GLsizei length, const char *message, void *ctx)
1269 {
1270 switch (type)
1271 {
1272 case GL_DEBUG_TYPE_ERROR_ARB:
1273 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1274 break;
1275
1276 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1277 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1278 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1279 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1280 break;
1281
1282 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1283 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1284 break;
1285
1286 default:
1287 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1288 break;
1289 }
1290 }
1291
1292 /* Do not call while under the GL lock. */
1293 struct wined3d_context *context_create(struct wined3d_swapchain *swapchain,
1294 struct wined3d_surface *target, const struct wined3d_format *ds_format)
1295 {
1296 struct wined3d_device *device = swapchain->device;
1297 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1298 const struct wined3d_format *color_format;
1299 struct wined3d_context *ret;
1300 BOOL auxBuffers = FALSE;
1301 HGLRC ctx, share_ctx;
1302 int pixel_format;
1303 unsigned int s;
1304 int swap_interval;
1305 DWORD state;
1306 HDC hdc;
1307
1308 TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
1309
1310 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
1311 if (!ret)
1312 return NULL;
1313
1314 ret->blit_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1315 gl_info->limits.buffers * sizeof(*ret->blit_targets));
1316 if (!ret->blit_targets)
1317 goto out;
1318
1319 ret->draw_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1320 gl_info->limits.buffers * sizeof(*ret->draw_buffers));
1321 if (!ret->draw_buffers)
1322 goto out;
1323
1324 ret->free_occlusion_query_size = 4;
1325 ret->free_occlusion_queries = HeapAlloc(GetProcessHeap(), 0,
1326 ret->free_occlusion_query_size * sizeof(*ret->free_occlusion_queries));
1327 if (!ret->free_occlusion_queries)
1328 goto out;
1329
1330 list_init(&ret->occlusion_queries);
1331
1332 ret->free_event_query_size = 4;
1333 ret->free_event_queries = HeapAlloc(GetProcessHeap(), 0,
1334 ret->free_event_query_size * sizeof(*ret->free_event_queries));
1335 if (!ret->free_event_queries)
1336 goto out;
1337
1338 list_init(&ret->event_queries);
1339 list_init(&ret->fbo_list);
1340 list_init(&ret->fbo_destroy_list);
1341
1342 if (!device->shader_backend->shader_allocate_context_data(ret))
1343 {
1344 ERR("Failed to allocate shader backend context data.\n");
1345 goto out;
1346 }
1347
1348 if (!(hdc = GetDC(swapchain->win_handle)))
1349 {
1350 WARN("Failed to retireve device context, trying swapchain backup.\n");
1351
1352 if (!(hdc = swapchain_get_backup_dc(swapchain)))
1353 {
1354 ERR("Failed to retrieve a device context.\n");
1355 goto out;
1356 }
1357 }
1358
1359 color_format = target->resource.format;
1360
1361 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1362 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1363 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1364 {
1365 auxBuffers = TRUE;
1366
1367 if (color_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1368 color_format = wined3d_get_format(gl_info, WINED3DFMT_B4G4R4A4_UNORM);
1369 else if (color_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1370 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
1371 }
1372
1373 /* DirectDraw supports 8bit paletted render targets and these are used by
1374 * old games like StarCraft and C&C. Most modern hardware doesn't support
1375 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1376 * conversion (ab)uses the alpha component for storing the palette index.
1377 * For this reason we require a format with 8bit alpha, so request
1378 * A8R8G8B8. */
1379 if (color_format->id == WINED3DFMT_P8_UINT)
1380 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
1381
1382 /* Try to find a pixel format which matches our requirements. */
1383 pixel_format = context_choose_pixel_format(device, hdc, color_format, ds_format, auxBuffers, FALSE);
1384
1385 /* Try to locate a compatible format if we weren't able to find anything. */
1386 if (!pixel_format)
1387 {
1388 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1389 pixel_format = context_choose_pixel_format(device, hdc, color_format, ds_format, auxBuffers, TRUE);
1390 }
1391
1392 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1393 if (!pixel_format)
1394 {
1395 ERR("Can't find a suitable pixel format.\n");
1396 goto out;
1397 }
1398
1399 context_enter(ret);
1400
1401 if (!context_set_pixel_format(gl_info, hdc, pixel_format))
1402 {
1403 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format, hdc);
1404 context_release(ret);
1405 goto out;
1406 }
1407
1408 share_ctx = device->context_count ? device->contexts[0]->glCtx : NULL;
1409 if (gl_info->p_wglCreateContextAttribsARB)
1410 {
1411 unsigned int ctx_attrib_idx = 0;
1412 GLint ctx_attribs[3];
1413
1414 if (context_debug_output_enabled(gl_info))
1415 {
1416 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1417 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_DEBUG_BIT_ARB;
1418 }
1419 ctx_attribs[ctx_attrib_idx] = 0;
1420
1421 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1422 {
1423 ERR("Failed to create a WGL context.\n");
1424 context_release(ret);
1425 goto out;
1426 }
1427 }
1428 else
1429 {
1430 if (!(ctx = wglCreateContext(hdc)))
1431 {
1432 ERR("Failed to create a WGL context.\n");
1433 context_release(ret);
1434 goto out;
1435 }
1436
1437 if (share_ctx && !wglShareLists(share_ctx, ctx))
1438 {
1439 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
1440 context_release(ret);
1441 if (!wglDeleteContext(ctx))
1442 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1443 goto out;
1444 }
1445 }
1446
1447 if (!device_context_add(device, ret))
1448 {
1449 ERR("Failed to add the newly created context to the context list\n");
1450 context_release(ret);
1451 if (!wglDeleteContext(ctx))
1452 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1453 goto out;
1454 }
1455
1456 ret->gl_info = gl_info;
1457 ret->d3d_info = &device->adapter->d3d_info;
1458 ret->state_table = device->StateTable;
1459
1460 /* Mark all states dirty to force a proper initialization of the states
1461 * on the first use of the context. */
1462 for (state = 0; state <= STATE_HIGHEST; ++state)
1463 {
1464 if (ret->state_table[state].representative)
1465 context_invalidate_state(ret, state);
1466 }
1467
1468 ret->swapchain = swapchain;
1469 ret->current_rt = target;
1470 ret->tid = GetCurrentThreadId();
1471
1472 ret->render_offscreen = surface_is_offscreen(target);
1473 ret->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
1474 ret->valid = 1;
1475
1476 ret->glCtx = ctx;
1477 ret->win_handle = swapchain->win_handle;
1478 ret->hdc = hdc;
1479 ret->pixel_format = pixel_format;
1480
1481 /* Set up the context defaults */
1482 if (!context_set_current(ret))
1483 {
1484 ERR("Cannot activate context to set up defaults.\n");
1485 device_context_remove(device, ret);
1486 context_release(ret);
1487 if (!wglDeleteContext(ctx))
1488 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1489 goto out;
1490 }
1491
1492 if (context_debug_output_enabled(gl_info))
1493 {
1494 GL_EXTCALL(glDebugMessageCallbackARB(wined3d_debug_callback, ret));
1495 if (TRACE_ON(d3d_synchronous))
1496 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
1497 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
1498 if (ERR_ON(d3d))
1499 {
1500 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR_ARB,
1501 GL_DONT_CARE, 0, NULL, GL_TRUE));
1502 }
1503 if (FIXME_ON(d3d))
1504 {
1505 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB,
1506 GL_DONT_CARE, 0, NULL, GL_TRUE));
1507 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB,
1508 GL_DONT_CARE, 0, NULL, GL_TRUE));
1509 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY_ARB,
1510 GL_DONT_CARE, 0, NULL, GL_TRUE));
1511 }
1512 if (WARN_ON(d3d_perf))
1513 {
1514 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE_ARB,
1515 GL_DONT_CARE, 0, NULL, GL_TRUE));
1516 }
1517 }
1518
1519 switch (swapchain->desc.swap_interval)
1520 {
1521 case WINED3DPRESENT_INTERVAL_IMMEDIATE:
1522 swap_interval = 0;
1523 break;
1524 case WINED3DPRESENT_INTERVAL_DEFAULT:
1525 case WINED3DPRESENT_INTERVAL_ONE:
1526 swap_interval = 1;
1527 break;
1528 case WINED3DPRESENT_INTERVAL_TWO:
1529 swap_interval = 2;
1530 break;
1531 case WINED3DPRESENT_INTERVAL_THREE:
1532 swap_interval = 3;
1533 break;
1534 case WINED3DPRESENT_INTERVAL_FOUR:
1535 swap_interval = 4;
1536 break;
1537 default:
1538 FIXME("Unknown swap interval %#x.\n", swapchain->desc.swap_interval);
1539 swap_interval = 1;
1540 }
1541
1542 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
1543 {
1544 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
1545 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1546 swap_interval, ret, GetLastError());
1547 }
1548
1549 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
1550
1551 TRACE("Setting up the screen\n");
1552
1553 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1554 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1555
1556 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1557 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1558
1559 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1560 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1561
1562 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
1563 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1564 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);
1565 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1566
1567 if (gl_info->supported[ARB_VERTEX_BLEND])
1568 {
1569 /* Direct3D always uses n-1 weights for n world matrices and uses
1570 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1571 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1572 * enabled as well. */
1573 gl_info->gl_ops.gl.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB);
1574 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1575 }
1576 if (gl_info->supported[NV_TEXTURE_SHADER2])
1577 {
1578 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1579 * the previous texture where to source the offset from is always unit - 1.
1580 */
1581 for (s = 1; s < gl_info->limits.textures; ++s)
1582 {
1583 context_active_texture(ret, gl_info, s);
1584 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
1585 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
1586 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1587 }
1588 }
1589 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
1590 {
1591 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1592 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1593 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1594 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1595 * is ever assigned.
1596 *
1597 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1598 * program and the dummy program is destroyed when the context is destroyed.
1599 */
1600 const char *dummy_program =
1601 "!!ARBfp1.0\n"
1602 "MOV result.color, fragment.color.primary;\n"
1603 "END\n";
1604 GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
1605 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
1606 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
1607 }
1608
1609 if (gl_info->supported[ARB_POINT_SPRITE])
1610 {
1611 for (s = 0; s < gl_info->limits.textures; ++s)
1612 {
1613 context_active_texture(ret, gl_info, s);
1614 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
1615 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1616 }
1617 }
1618
1619 if (gl_info->supported[ARB_PROVOKING_VERTEX])
1620 {
1621 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
1622 }
1623 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
1624 {
1625 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
1626 }
1627 ret->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
1628 | (1 << WINED3D_SHADER_TYPE_VERTEX)
1629 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
1630
1631 /* If this happens to be the first context for the device, dummy textures
1632 * are not created yet. In that case, they will be created (and bound) by
1633 * create_dummy_textures right after this context is initialized. */
1634 if (device->dummy_texture_2d[0])
1635 bind_dummy_textures(device, ret);
1636
1637 TRACE("Created context %p.\n", ret);
1638
1639 return ret;
1640
1641 out:
1642 device->shader_backend->shader_free_context_data(ret);
1643 HeapFree(GetProcessHeap(), 0, ret->free_event_queries);
1644 HeapFree(GetProcessHeap(), 0, ret->free_occlusion_queries);
1645 HeapFree(GetProcessHeap(), 0, ret->draw_buffers);
1646 HeapFree(GetProcessHeap(), 0, ret->blit_targets);
1647 HeapFree(GetProcessHeap(), 0, ret);
1648 return NULL;
1649 }
1650
1651 /* Do not call while under the GL lock. */
1652 void context_destroy(struct wined3d_device *device, struct wined3d_context *context)
1653 {
1654 BOOL destroy;
1655
1656 TRACE("Destroying ctx %p\n", context);
1657
1658 if (context->tid == GetCurrentThreadId() || !context->current)
1659 {
1660 context_destroy_gl_resources(context);
1661 TlsSetValue(wined3d_context_tls_idx, NULL);
1662 destroy = TRUE;
1663 }
1664 else
1665 {
1666 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1667 in wined3d_adapter may go away in the meantime */
1668 struct wined3d_gl_info *gl_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info));
1669 *gl_info = *context->gl_info;
1670 context->gl_info = gl_info;
1671 context->destroyed = 1;
1672 destroy = FALSE;
1673 }
1674
1675 device->shader_backend->shader_free_context_data(context);
1676 HeapFree(GetProcessHeap(), 0, context->draw_buffers);
1677 HeapFree(GetProcessHeap(), 0, context->blit_targets);
1678 device_context_remove(device, context);
1679 if (destroy) HeapFree(GetProcessHeap(), 0, context);
1680 }
1681
1682 /* Context activation is done by the caller. */
1683 static void set_blit_dimension(const struct wined3d_gl_info *gl_info, UINT width, UINT height)
1684 {
1685 const GLdouble projection[] =
1686 {
1687 2.0 / width, 0.0, 0.0, 0.0,
1688 0.0, 2.0 / height, 0.0, 0.0,
1689 0.0, 0.0, 2.0, 0.0,
1690 -1.0, -1.0, -1.0, 1.0,
1691 };
1692
1693 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
1694 checkGLcall("glMatrixMode(GL_PROJECTION)");
1695 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
1696 checkGLcall("glLoadMatrixd");
1697 gl_info->gl_ops.gl.p_glViewport(0, 0, width, height);
1698 checkGLcall("glViewport");
1699 }
1700
1701 static void context_get_rt_size(const struct wined3d_context *context, SIZE *size)
1702 {
1703 const struct wined3d_surface *rt = context->current_rt;
1704
1705 if (rt->swapchain && rt->swapchain->front_buffer == rt)
1706 {
1707 RECT window_size;
1708
1709 GetClientRect(context->win_handle, &window_size);
1710 size->cx = window_size.right - window_size.left;
1711 size->cy = window_size.bottom - window_size.top;
1712
1713 return;
1714 }
1715
1716 size->cx = rt->resource.width;
1717 size->cy = rt->resource.height;
1718 }
1719
1720 /*****************************************************************************
1721 * SetupForBlit
1722 *
1723 * Sets up a context for DirectDraw blitting.
1724 * All texture units are disabled, texture unit 0 is set as current unit
1725 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1726 * color writing enabled for all channels
1727 * register combiners disabled, shaders disabled
1728 * world matrix is set to identity, texture matrix 0 too
1729 * projection matrix is setup for drawing screen coordinates
1730 *
1731 * Params:
1732 * This: Device to activate the context for
1733 * context: Context to setup
1734 *
1735 *****************************************************************************/
1736 /* Context activation is done by the caller. */
1737 static void SetupForBlit(const struct wined3d_device *device, struct wined3d_context *context)
1738 {
1739 int i;
1740 const struct wined3d_gl_info *gl_info = context->gl_info;
1741 DWORD sampler;
1742 SIZE rt_size;
1743
1744 TRACE("Setting up context %p for blitting\n", context);
1745
1746 context_get_rt_size(context, &rt_size);
1747
1748 if (context->last_was_blit)
1749 {
1750 if (context->blit_w != rt_size.cx || context->blit_h != rt_size.cy)
1751 {
1752 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
1753 context->blit_w = rt_size.cx;
1754 context->blit_h = rt_size.cy;
1755 /* No need to dirtify here, the states are still dirtified because
1756 * they weren't applied since the last SetupForBlit() call. */
1757 }
1758 TRACE("Context is already set up for blitting, nothing to do\n");
1759 return;
1760 }
1761 context->last_was_blit = TRUE;
1762
1763 /* Disable all textures. The caller can then bind a texture it wants to blit
1764 * from
1765 *
1766 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1767 * function texture unit. No need to care for higher samplers
1768 */
1769 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
1770 {
1771 sampler = device->rev_tex_unit_map[i];
1772 context_active_texture(context, gl_info, i);
1773
1774 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1775 {
1776 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1777 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1778 }
1779 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
1780 checkGLcall("glDisable GL_TEXTURE_3D");
1781 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1782 {
1783 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
1784 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1785 }
1786 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
1787 checkGLcall("glDisable GL_TEXTURE_2D");
1788
1789 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1790 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1791
1792 if (sampler != WINED3D_UNMAPPED_STAGE)
1793 {
1794 if (sampler < MAX_TEXTURES)
1795 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
1796 context_invalidate_state(context, STATE_SAMPLER(sampler));
1797 }
1798 }
1799 context_active_texture(context, gl_info, 0);
1800
1801 sampler = device->rev_tex_unit_map[0];
1802
1803 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1804 {
1805 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1806 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1807 }
1808 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
1809 checkGLcall("glDisable GL_TEXTURE_3D");
1810 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1811 {
1812 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
1813 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1814 }
1815 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
1816 checkGLcall("glDisable GL_TEXTURE_2D");
1817
1818 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1819
1820 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
1821 checkGLcall("glMatrixMode(GL_TEXTURE)");
1822 gl_info->gl_ops.gl.p_glLoadIdentity();
1823 checkGLcall("glLoadIdentity()");
1824
1825 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1826 {
1827 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1828 GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
1829 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1830 }
1831
1832 if (sampler != WINED3D_UNMAPPED_STAGE)
1833 {
1834 if (sampler < MAX_TEXTURES)
1835 {
1836 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
1837 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
1838 }
1839 context_invalidate_state(context, STATE_SAMPLER(sampler));
1840 }
1841
1842 /* Other misc states */
1843 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
1844 checkGLcall("glDisable(GL_ALPHA_TEST)");
1845 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
1846 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
1847 checkGLcall("glDisable GL_LIGHTING");
1848 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
1849 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
1850 checkGLcall("glDisable GL_DEPTH_TEST");
1851 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZENABLE));
1852 glDisableWINE(GL_FOG);
1853 checkGLcall("glDisable GL_FOG");
1854 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
1855 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
1856 checkGLcall("glDisable GL_BLEND");
1857 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
1858 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
1859 checkGLcall("glDisable GL_CULL_FACE");
1860 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CULLMODE));
1861 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
1862 checkGLcall("glDisable GL_STENCIL_TEST");
1863 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILENABLE));
1864 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
1865 checkGLcall("glDisable GL_SCISSOR_TEST");
1866 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
1867 if (gl_info->supported[ARB_POINT_SPRITE])
1868 {
1869 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
1870 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1871 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
1872 }
1873 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
1874 checkGLcall("glColorMask");
1875 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
1876 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
1877 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
1878 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
1879 if (gl_info->supported[EXT_SECONDARY_COLOR])
1880 {
1881 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
1882 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
1883 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1884 }
1885
1886 /* Setup transforms */
1887 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
1888 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1889 gl_info->gl_ops.gl.p_glLoadIdentity();
1890 checkGLcall("glLoadIdentity()");
1891 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
1892
1893 context->last_was_rhw = TRUE;
1894 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
1895
1896 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
1897 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
1898 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
1899 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
1900 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
1901 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
1902 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
1903
1904 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
1905
1906 /* Disable shaders */
1907 device->shader_backend->shader_disable(device->shader_priv, context);
1908
1909 context->blit_w = rt_size.cx;
1910 context->blit_h = rt_size.cy;
1911 context_invalidate_state(context, STATE_VIEWPORT);
1912 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
1913 }
1914
1915 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
1916 {
1917 return rt_mask & (1 << 31);
1918 }
1919
1920 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
1921 {
1922 return rt_mask & ~(1 << 31);
1923 }
1924
1925 /* Context activation is done by the caller. */
1926 static void context_apply_draw_buffers(struct wined3d_context *context, DWORD rt_mask)
1927 {
1928 const struct wined3d_gl_info *gl_info = context->gl_info;
1929
1930 if (!rt_mask)
1931 {
1932 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
1933 checkGLcall("glDrawBuffer()");
1934 }
1935 else if (is_rt_mask_onscreen(rt_mask))
1936 {
1937 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
1938 checkGLcall("glDrawBuffer()");
1939 }
1940 else
1941 {
1942 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
1943 {
1944 unsigned int i = 0;
1945
1946 while (rt_mask)
1947 {
1948 if (rt_mask & 1)
1949 context->draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
1950 else
1951 context->draw_buffers[i] = GL_NONE;
1952
1953 rt_mask >>= 1;
1954 ++i;
1955 }
1956
1957 if (gl_info->supported[ARB_DRAW_BUFFERS])
1958 {
1959 GL_EXTCALL(glDrawBuffersARB(i, context->draw_buffers));
1960 checkGLcall("glDrawBuffers()");
1961 }
1962 else
1963 {
1964 gl_info->gl_ops.gl.p_glDrawBuffer(context->draw_buffers[0]);
1965 checkGLcall("glDrawBuffer()");
1966 }
1967 }
1968 else
1969 {
1970 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
1971 }
1972 }
1973 }
1974
1975 /* Context activation is done by the caller. */
1976 void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
1977 {
1978 const struct wined3d_gl_info *gl_info = context->gl_info;
1979 DWORD *current_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
1980 DWORD new_mask = context_generate_rt_mask(buffer);
1981
1982 if (new_mask == *current_mask)
1983 return;
1984
1985 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
1986 checkGLcall("glDrawBuffer()");
1987
1988 *current_mask = new_mask;
1989 }
1990
1991 /* Context activation is done by the caller. */
1992 void context_active_texture(struct wined3d_context *context, const struct wined3d_gl_info *gl_info, unsigned int unit)
1993 {
1994 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0 + unit));
1995 checkGLcall("glActiveTextureARB");
1996 context->active_texture = unit;
1997 }
1998
1999 void context_bind_texture(struct wined3d_context *context, GLenum target, GLuint name)
2000 {
2001 const struct wined3d_gl_info *gl_info = context->gl_info;
2002 DWORD unit = context->active_texture;
2003 DWORD old_texture_type = context->texture_type[unit];
2004
2005 if (name)
2006 {
2007 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2008 checkGLcall("glBindTexture");
2009 }
2010 else
2011 {
2012 target = GL_NONE;
2013 }
2014
2015 if (old_texture_type != target)
2016 {
2017 const struct wined3d_device *device = context->swapchain->device;
2018
2019 switch (old_texture_type)
2020 {
2021 case GL_NONE:
2022 /* nothing to do */
2023 break;
2024 case GL_TEXTURE_2D:
2025 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[unit]);
2026 checkGLcall("glBindTexture");
2027 break;
2028 case GL_TEXTURE_RECTANGLE_ARB:
2029 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[unit]);
2030 checkGLcall("glBindTexture");
2031 break;
2032 case GL_TEXTURE_CUBE_MAP:
2033 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[unit]);
2034 checkGLcall("glBindTexture");
2035 break;
2036 case GL_TEXTURE_3D:
2037 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[unit]);
2038 checkGLcall("glBindTexture");
2039 break;
2040 default:
2041 ERR("Unexpected texture target %#x\n", old_texture_type);
2042 }
2043
2044 context->texture_type[unit] = target;
2045 }
2046 }
2047
2048 static void context_set_render_offscreen(struct wined3d_context *context, BOOL offscreen)
2049 {
2050 if (context->render_offscreen == offscreen) return;
2051
2052 context_invalidate_state(context, STATE_POINTSPRITECOORDORIGIN);
2053 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2054 context_invalidate_state(context, STATE_VIEWPORT);
2055 context_invalidate_state(context, STATE_SCISSORRECT);
2056 context_invalidate_state(context, STATE_FRONTFACE);
2057 context->render_offscreen = offscreen;
2058 }
2059
2060 static BOOL match_depth_stencil_format(const struct wined3d_format *existing,
2061 const struct wined3d_format *required)
2062 {
2063 BYTE existing_depth, existing_stencil, required_depth, required_stencil;
2064
2065 if (existing == required) return TRUE;
2066 if ((existing->flags & WINED3DFMT_FLAG_FLOAT) != (required->flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
2067
2068 getDepthStencilBits(existing, &existing_depth, &existing_stencil);
2069 getDepthStencilBits(required, &required_depth, &required_stencil);
2070
2071 if(existing_depth < required_depth) return FALSE;
2072 /* If stencil bits are used the exact amount is required - otherwise wrapping
2073 * won't work correctly */
2074 if(required_stencil && required_stencil != existing_stencil) return FALSE;
2075 return TRUE;
2076 }
2077
2078 /* The caller provides a context */
2079 static void context_validate_onscreen_formats(struct wined3d_context *context,
2080 const struct wined3d_surface *depth_stencil)
2081 {
2082 /* Onscreen surfaces are always in a swapchain */
2083 struct wined3d_swapchain *swapchain = context->current_rt->swapchain;
2084
2085 if (context->render_offscreen || !depth_stencil) return;
2086 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->resource.format)) return;
2087
2088 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2089 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2090 * format. */
2091 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2092
2093 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2094 surface_load_location(context->current_rt, SFLAG_INTEXTURE, NULL);
2095 swapchain->render_to_fbo = TRUE;
2096 swapchain_update_draw_bindings(swapchain);
2097 context_set_render_offscreen(context, TRUE);
2098 }
2099
2100 static DWORD context_generate_rt_mask_no_fbo(const struct wined3d_device *device, const struct wined3d_surface *rt)
2101 {
2102 if (!rt || rt->resource.format->id == WINED3DFMT_NULL)
2103 return 0;
2104 else if (rt->swapchain)
2105 return context_generate_rt_mask_from_surface(rt);
2106 else
2107 return context_generate_rt_mask(device->offscreenBuffer);
2108 }
2109
2110 /* Context activation is done by the caller. */
2111 void context_apply_blit_state(struct wined3d_context *context, const struct wined3d_device *device)
2112 {
2113 struct wined3d_surface *rt = context->current_rt;
2114 DWORD rt_mask, *cur_mask;
2115
2116 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2117 {
2118 context_validate_onscreen_formats(context, NULL);
2119
2120 if (context->render_offscreen)
2121 {
2122 surface_internal_preload(rt, SRGB_RGB);
2123
2124 context_apply_fbo_state_blit(context, GL_FRAMEBUFFER, rt, NULL, rt->draw_binding);
2125 if (rt->resource.format->id != WINED3DFMT_NULL)
2126 rt_mask = 1;
2127 else
2128 rt_mask = 0;
2129 }
2130 else
2131 {
2132 context->current_fbo = NULL;
2133 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
2134 rt_mask = context_generate_rt_mask_from_surface(rt);
2135 }
2136 }
2137 else
2138 {
2139 rt_mask = context_generate_rt_mask_no_fbo(device, rt);
2140 }
2141
2142 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2143
2144 if (rt_mask != *cur_mask)
2145 {
2146 context_apply_draw_buffers(context, rt_mask);
2147 *cur_mask = rt_mask;
2148 }
2149
2150 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2151 {
2152 context_check_fbo_status(context, GL_FRAMEBUFFER);
2153 }
2154
2155 SetupForBlit(device, context);
2156 context_invalidate_state(context, STATE_FRAMEBUFFER);
2157 }
2158
2159 static BOOL context_validate_rt_config(UINT rt_count,
2160 struct wined3d_surface * const *rts, const struct wined3d_surface *ds)
2161 {
2162 unsigned int i;
2163
2164 if (ds) return TRUE;
2165
2166 for (i = 0; i < rt_count; ++i)
2167 {
2168 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL)
2169 return TRUE;
2170 }
2171
2172 WARN("Invalid render target config, need at least one attachment.\n");
2173 return FALSE;
2174 }
2175
2176 /* Context activation is done by the caller. */
2177 BOOL context_apply_clear_state(struct wined3d_context *context, const struct wined3d_device *device,
2178 UINT rt_count, const struct wined3d_fb_state *fb)
2179 {
2180 const struct wined3d_gl_info *gl_info = context->gl_info;
2181 DWORD rt_mask = 0, *cur_mask;
2182 UINT i;
2183 struct wined3d_surface **rts = fb->render_targets;
2184
2185 if (isStateDirty(context, STATE_FRAMEBUFFER) || fb != &device->fb
2186 || rt_count != context->gl_info->limits.buffers)
2187 {
2188 if (!context_validate_rt_config(rt_count, rts, fb->depth_stencil))
2189 return FALSE;
2190
2191 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2192 {
2193 context_validate_onscreen_formats(context, fb->depth_stencil);
2194
2195 if (!rt_count || surface_is_offscreen(rts[0]))
2196 {
2197 for (i = 0; i < rt_count; ++i)
2198 {
2199 context->blit_targets[i] = rts[i];
2200 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL)
2201 rt_mask |= (1 << i);
2202 }
2203 while (i < context->gl_info->limits.buffers)
2204 {
2205 context->blit_targets[i] = NULL;
2206 ++i;
2207 }
2208 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets, fb->depth_stencil,
2209 rt_count ? rts[0]->draw_binding : SFLAG_INTEXTURE);
2210 }
2211 else
2212 {
2213 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL, SFLAG_INDRAWABLE);
2214 rt_mask = context_generate_rt_mask_from_surface(rts[0]);
2215 }
2216
2217 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2218 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2219 * state management allows this */
2220 context_invalidate_state(context, STATE_FRAMEBUFFER);
2221 }
2222 else
2223 {
2224 rt_mask = context_generate_rt_mask_no_fbo(device, rt_count ? rts[0] : NULL);
2225 }
2226 }
2227 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2228 && (!rt_count || surface_is_offscreen(rts[0])))
2229 {
2230 for (i = 0; i < rt_count; ++i)
2231 {
2232 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL) rt_mask |= (1 << i);
2233 }
2234 }
2235 else
2236 {
2237 rt_mask = context_generate_rt_mask_no_fbo(device, rt_count ? rts[0] : NULL);
2238 }
2239
2240 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2241
2242 if (rt_mask != *cur_mask)
2243 {
2244 context_apply_draw_buffers(context, rt_mask);
2245 *cur_mask = rt_mask;
2246 context_invalidate_state(context, STATE_FRAMEBUFFER);
2247 }
2248
2249 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2250 {
2251 context_check_fbo_status(context, GL_FRAMEBUFFER);
2252 }
2253
2254 if (context->last_was_blit)
2255 context->last_was_blit = FALSE;
2256
2257 /* Blending and clearing should be orthogonal, but tests on the nvidia
2258 * driver show that disabling blending when clearing improves the clearing
2259 * performance incredibly. */
2260 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2261 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
2262 checkGLcall("glEnable GL_SCISSOR_TEST");
2263
2264 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2265 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2266 context_invalidate_state(context, STATE_SCISSORRECT);
2267
2268 return TRUE;
2269 }
2270
2271 static DWORD find_draw_buffers_mask(const struct wined3d_context *context, const struct wined3d_device *device)
2272 {
2273 const struct wined3d_state *state = &device->state;
2274 struct wined3d_surface **rts = state->fb->render_targets;
2275 struct wined3d_shader *ps = state->pixel_shader;
2276 DWORD rt_mask, rt_mask_bits;
2277 unsigned int i;
2278
2279 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return context_generate_rt_mask_no_fbo(device, rts[0]);
2280 else if (!context->render_offscreen) return context_generate_rt_mask_from_surface(rts[0]);
2281
2282 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
2283 rt_mask &= context->d3d_info->valid_rt_mask;
2284 rt_mask_bits = rt_mask;
2285 i = 0;
2286 while (rt_mask_bits)
2287 {
2288 rt_mask_bits &= ~(1 << i);
2289 if (!rts[i] || rts[i]->resource.format->id == WINED3DFMT_NULL)
2290 rt_mask &= ~(1 << i);
2291
2292 i++;
2293 }
2294
2295 return rt_mask;
2296 }
2297
2298 /* Context activation is done by the caller. */
2299 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
2300 {
2301 const struct wined3d_device *device = context->swapchain->device;
2302 const struct wined3d_fb_state *fb = state->fb;
2303 DWORD rt_mask = find_draw_buffers_mask(context, device);
2304 DWORD *cur_mask;
2305
2306 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2307 {
2308 if (!context->render_offscreen)
2309 {
2310 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL, SFLAG_INDRAWABLE);
2311 }
2312 else
2313 {
2314 context_apply_fbo_state(context, GL_FRAMEBUFFER, fb->render_targets, fb->depth_stencil,
2315 fb->render_targets[0]->draw_binding);
2316 }
2317 }
2318
2319 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2320 if (rt_mask != *cur_mask)
2321 {
2322 context_apply_draw_buffers(context, rt_mask);
2323 *cur_mask = rt_mask;
2324 }
2325 }
2326
2327 /* Context activation is done by the caller. */
2328 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
2329 {
2330 const struct wined3d_device *device = context->swapchain->device;
2331 DWORD rt_mask, *cur_mask;
2332
2333 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
2334
2335 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2336 rt_mask = find_draw_buffers_mask(context, device);
2337 if (rt_mask != *cur_mask)
2338 {
2339 context_apply_draw_buffers(context, rt_mask);
2340 *cur_mask = rt_mask;
2341 }
2342 }
2343
2344 /* Context activation is done by the caller. */
2345 BOOL context_apply_draw_state(struct wined3d_context *context, struct wined3d_device *device)
2346 {
2347 const struct wined3d_state *state = &device->state;
2348 const struct StateEntry *state_table = context->state_table;
2349 const struct wined3d_fb_state *fb = state->fb;
2350 unsigned int i;
2351
2352 if (!context_validate_rt_config(context->gl_info->limits.buffers,
2353 fb->render_targets, fb->depth_stencil))
2354 return FALSE;
2355
2356 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && isStateDirty(context, STATE_FRAMEBUFFER))
2357 {
2358 context_validate_onscreen_formats(context, fb->depth_stencil);
2359 }
2360
2361 /* Preload resources before FBO setup. Texture preload in particular may
2362 * result in changes to the current FBO, due to using e.g. FBO blits for
2363 * updating a resource location. */
2364 device_update_tex_unit_map(device);
2365 device_preload_textures(device);
2366 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC))
2367 device_update_stream_info(device, context->gl_info);
2368 if (state->index_buffer)
2369 {
2370 if (device->stream_info.all_vbo)
2371 wined3d_buffer_preload(state->index_buffer);
2372 else
2373 buffer_get_sysmem(state->index_buffer, context->gl_info);
2374 }
2375
2376 for (i = 0; i < context->numDirtyEntries; ++i)
2377 {
2378 DWORD rep = context->dirtyArray[i];
2379 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
2380 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
2381 context->isStateDirty[idx] &= ~(1 << shift);
2382 state_table[rep].apply(context, state, rep);
2383 }
2384
2385 if (context->shader_update_mask)
2386 {
2387 device->shader_backend->shader_select(device->shader_priv, context, state);
2388 context->shader_update_mask = 0;
2389 }
2390
2391 if (context->constant_update_mask)
2392 {
2393 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
2394 context->constant_update_mask = 0;
2395 }
2396
2397 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2398 {
2399 context_check_fbo_status(context, GL_FRAMEBUFFER);
2400 }
2401
2402 context->numDirtyEntries = 0; /* This makes the whole list clean */
2403 context->last_was_blit = FALSE;
2404
2405 return TRUE;
2406 }
2407
2408 static void context_setup_target(struct wined3d_context *context, struct wined3d_surface *target)
2409 {
2410 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
2411
2412 render_offscreen = surface_is_offscreen(target);
2413 if (context->current_rt == target && render_offscreen == old_render_offscreen) return;
2414
2415 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2416 * the alpha blend state changes with different render target formats. */
2417 if (!context->current_rt)
2418 {
2419 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2420 }
2421 else
2422 {
2423 const struct wined3d_format *old = context->current_rt->resource.format;
2424 const struct wined3d_format *new = target->resource.format;
2425
2426 if (old->id != new->id)
2427 {
2428 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2429 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
2430 || !(new->flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
2431 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2432
2433 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2434 if ((old->flags & WINED3DFMT_FLAG_SRGB_WRITE) != (new->flags & WINED3DFMT_FLAG_SRGB_WRITE))
2435 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
2436 }
2437
2438 /* When switching away from an offscreen render target, and we're not
2439 * using FBOs, we have to read the drawable into the texture. This is
2440 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2441 * are some things that need care though. PreLoad needs a GL context,
2442 * and FindContext is called before the context is activated. It also
2443 * has to be called with the old rendertarget active, otherwise a
2444 * wrong drawable is read. */
2445 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
2446 && old_render_offscreen && context->current_rt != target)
2447 {
2448 /* Read the back buffer of the old drawable into the destination texture. */
2449 if (context->current_rt->texture_name_srgb)
2450 surface_internal_preload(context->current_rt, SRGB_SRGB);
2451 surface_internal_preload(context->current_rt, SRGB_RGB);
2452 surface_modify_location(context->current_rt, SFLAG_INDRAWABLE, FALSE);
2453 }
2454 }
2455
2456 context->current_rt = target;
2457 context_set_render_offscreen(context, render_offscreen);
2458 }
2459
2460 /* Do not call while under the GL lock. */
2461 struct wined3d_context *context_acquire(const struct wined3d_device *device, struct wined3d_surface *target)
2462 {
2463 struct wined3d_context *current_context = context_get_current();
2464 struct wined3d_context *context;
2465
2466 TRACE("device %p, target %p.\n", device, target);
2467
2468 if (current_context && current_context->destroyed)
2469 current_context = NULL;
2470
2471 if (!target)
2472 {
2473 if (current_context
2474 && current_context->current_rt
2475 && current_context->swapchain->device == device)
2476 {
2477 target = current_context->current_rt;
2478 }
2479 else
2480 {
2481 struct wined3d_swapchain *swapchain = device->swapchains[0];
2482 if (swapchain->back_buffers)
2483 target = swapchain->back_buffers[0];
2484 else
2485 target = swapchain->front_buffer;
2486 }
2487 }
2488
2489 if (current_context && current_context->current_rt == target)
2490 {
2491 context = current_context;
2492 }
2493 else if (target->swapchain)
2494 {
2495 TRACE("Rendering onscreen.\n");
2496
2497 context = swapchain_get_context(target->swapchain);
2498 }
2499 else
2500 {
2501 TRACE("Rendering offscreen.\n");
2502
2503 /* Stay with the current context if possible. Otherwise use the
2504 * context for the primary swapchain. */
2505 if (current_context && current_context->swapchain->device == device)
2506 context = current_context;
2507 else
2508 context = swapchain_get_context(device->swapchains[0]);
2509 }
2510
2511 context_update_window(context);
2512 context_setup_target(context, target);
2513 context_enter(context);
2514 if (!context->valid) return context;
2515
2516 if (context != current_context)
2517 {
2518 if (!context_set_current(context))
2519 ERR("Failed to activate the new context.\n");
2520 }
2521 else if (context->restore_ctx)
2522 {
2523 context_set_gl_context(context);
2524 }
2525
2526 return context;
2527 }