2 * Context and render target management in wined3d
4 * Copyright 2007-2011, 2013 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 "wined3d_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
25 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous
);
28 #define WINED3D_MAX_FBO_ENTRIES 64
30 static DWORD wined3d_context_tls_idx
;
32 /* FBO helper functions */
34 /* Context activation is done by the caller. */
35 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint fbo
)
37 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
41 case GL_READ_FRAMEBUFFER
:
42 if (context
->fbo_read_binding
== fbo
) return;
43 context
->fbo_read_binding
= fbo
;
46 case GL_DRAW_FRAMEBUFFER
:
47 if (context
->fbo_draw_binding
== fbo
) return;
48 context
->fbo_draw_binding
= fbo
;
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
;
59 FIXME("Unhandled target %#x.\n", target
);
63 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
64 checkGLcall("glBindFramebuffer()");
67 /* Context activation is done by the caller. */
68 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
72 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
74 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
75 checkGLcall("glFramebufferTexture2D()");
77 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
78 checkGLcall("glFramebufferTexture2D()");
80 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
81 checkGLcall("glFramebufferTexture2D()");
84 /* Context activation is done by the caller. */
85 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint fbo
)
87 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
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);
93 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
94 checkGLcall("glDeleteFramebuffers()");
97 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
98 GLenum fbo_target
, DWORD format_flags
, GLuint rb
)
100 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
102 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
103 checkGLcall("glFramebufferRenderbuffer()");
106 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
108 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
109 checkGLcall("glFramebufferRenderbuffer()");
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
)
117 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
119 TRACE("Attach depth stencil %p\n", depth_stencil
);
123 DWORD format_flags
= depth_stencil
->resource
.format
->flags
;
125 if (depth_stencil
->current_renderbuffer
)
127 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
128 format_flags
, depth_stencil
->current_renderbuffer
->id
);
134 case WINED3D_LOCATION_TEXTURE_RGB
:
135 case WINED3D_LOCATION_TEXTURE_SRGB
:
136 surface_prepare_texture(depth_stencil
, context
, FALSE
);
138 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
140 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
141 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
142 depth_stencil
->texture_level
);
143 checkGLcall("glFramebufferTexture2D()");
146 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
148 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
149 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
150 depth_stencil
->texture_level
);
151 checkGLcall("glFramebufferTexture2D()");
155 case WINED3D_LOCATION_RB_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
);
161 case WINED3D_LOCATION_RB_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
);
168 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
173 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
175 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
176 checkGLcall("glFramebufferTexture2D()");
179 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
181 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
182 checkGLcall("glFramebufferTexture2D()");
187 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
188 checkGLcall("glFramebufferTexture2D()");
190 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
191 checkGLcall("glFramebufferTexture2D()");
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
)
199 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
201 TRACE("Attach surface %p to %u\n", surface
, idx
);
203 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
209 case WINED3D_LOCATION_TEXTURE_RGB
:
210 case WINED3D_LOCATION_TEXTURE_SRGB
:
211 srgb
= location
== WINED3D_LOCATION_TEXTURE_SRGB
;
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()");
219 case WINED3D_LOCATION_RB_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()");
226 case WINED3D_LOCATION_RB_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()");
234 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
240 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
241 checkGLcall("glFramebufferTexture2D()");
245 /* Context activation is done by the caller. */
246 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
248 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
251 if (!FIXME_ON(d3d
)) return;
253 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
254 if (status
== GL_FRAMEBUFFER_COMPLETE
)
256 TRACE("FBO complete\n");
260 const struct wined3d_surface
*attachment
;
263 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
265 if (!context
->current_fbo
)
267 ERR("FBO 0 is incomplete, driver bug?\n");
271 FIXME("\tLocation %s (%#x).\n", wined3d_debug_location(context
->current_fbo
->location
),
272 context
->current_fbo
->location
);
274 /* Dump the FBO attachments */
275 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
277 attachment
= context
->current_fbo
->render_targets
[i
];
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
);
285 attachment
= context
->current_fbo
->depth_stencil
;
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
);
295 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
297 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
298 return buffer
? (1 << 31) | buffer
: 0;
301 static inline DWORD
context_generate_rt_mask_from_surface(const struct wined3d_surface
*target
)
303 return (1 << 31) | surface_get_gl_buffer(target
);
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
)
309 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
310 struct fbo_entry
*entry
;
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
);
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
)
331 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
333 context_bind_fbo(context
, target
, entry
->id
);
334 context_clean_fbo_attachments(gl_info
, target
);
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
;
342 /* Context activation is done by the caller. */
343 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
347 TRACE("Destroy FBO %u.\n", entry
->id
);
348 context_destroy_fbo(context
, entry
->id
);
350 --context
->fbo_entry_count
;
351 list_remove(&entry
->entry
);
352 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
353 HeapFree(GetProcessHeap(), 0, entry
);
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
)
360 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
361 struct fbo_entry
*entry
;
363 if (depth_stencil
&& render_targets
&& render_targets
[0])
365 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
366 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
368 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
369 depth_stencil
= NULL
;
373 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
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
)
379 list_remove(&entry
->entry
);
380 list_add_head(&context
->fbo_list
, &entry
->entry
);
385 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
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
;
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
);
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
)
405 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
407 GLuint read_binding
, draw_binding
;
411 context_bind_fbo(context
, target
, entry
->id
);
415 read_binding
= context
->fbo_read_binding
;
416 draw_binding
= context
->fbo_draw_binding
;
417 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
419 /* Apply render targets */
420 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
422 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->location
);
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
);
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
)
436 if (target
== GL_READ_FRAMEBUFFER
)
437 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
439 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
442 entry
->attached
= TRUE
;
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
)
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
, 0);
459 context
->rebind_fbo
= FALSE
;
462 if (location
== WINED3D_LOCATION_DRAWABLE
)
464 context
->current_fbo
= NULL
;
465 context_bind_fbo(context
, target
, 0);
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 /* 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
)
478 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
480 context
->blit_targets
[0] = render_target
;
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
];
497 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
499 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
500 checkGLcall("glGenQueriesARB");
502 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
506 WARN("Occlusion queries not supported, not allocating query id.\n");
511 query
->context
= context
;
512 list_add_head(&context
->occlusion_queries
, &query
->entry
);
515 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
517 struct wined3d_context
*context
= query
->context
;
519 list_remove(&query
->entry
);
520 query
->context
= NULL
;
522 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
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
));
530 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
534 context
->free_occlusion_query_size
= new_size
;
535 context
->free_occlusion_queries
= new_data
;
538 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
541 /* Context activation is done by the caller. */
542 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
544 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
546 if (context
->free_event_query_count
)
548 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
552 if (gl_info
->supported
[ARB_SYNC
])
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
);
558 else if (gl_info
->supported
[APPLE_FENCE
])
560 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
561 checkGLcall("glGenFencesAPPLE");
563 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
565 else if(gl_info
->supported
[NV_FENCE
])
567 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
568 checkGLcall("glGenFencesNV");
570 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
574 WARN("Event queries not supported, not allocating query id.\n");
575 query
->object
.id
= 0;
579 query
->context
= context
;
580 list_add_head(&context
->event_queries
, &query
->entry
);
583 void context_free_event_query(struct wined3d_event_query
*query
)
585 struct wined3d_context
*context
= query
->context
;
587 list_remove(&query
->entry
);
588 query
->context
= NULL
;
590 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
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
));
598 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
602 context
->free_event_query_size
= new_size
;
603 context
->free_event_queries
= new_data
;
606 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
609 /* Context activation is done by the caller. */
610 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
612 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
614 if (context
->free_timestamp_query_count
)
616 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
620 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
621 checkGLcall("glGenQueriesARB");
623 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
626 query
->context
= context
;
627 list_add_head(&context
->timestamp_queries
, &query
->entry
);
630 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
632 struct wined3d_context
*context
= query
->context
;
634 list_remove(&query
->entry
);
635 query
->context
= NULL
;
637 if (context
->free_timestamp_query_count
>= context
->free_timestamp_query_size
- 1)
639 UINT new_size
= context
->free_timestamp_query_size
<< 1;
640 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_timestamp_queries
,
641 new_size
* sizeof(*context
->free_timestamp_queries
));
645 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
649 context
->free_timestamp_query_size
= new_size
;
650 context
->free_timestamp_queries
= new_data
;
653 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
656 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
658 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
659 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
663 for (i
= 0; i
< device
->context_count
; ++i
)
665 struct wined3d_context
*context
= device
->contexts
[i
];
666 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
667 struct fbo_entry
*entry
, *entry2
;
669 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
671 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
675 if (entry
->depth_stencil
== surface
)
677 callback(context
, entry
);
681 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
683 if (entry
->render_targets
[j
] == surface
)
685 callback(context
, entry
);
693 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
695 list_remove(&entry
->entry
);
696 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
699 void context_resource_released(const struct wined3d_device
*device
,
700 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
702 if (!device
->d3d_initialized
) return;
706 case WINED3D_RTYPE_SURFACE
:
707 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
708 context_queue_fbo_entry_destruction
);
716 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
718 entry
->attached
= FALSE
;
721 void context_resource_unloaded(const struct wined3d_device
*device
,
722 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
726 case WINED3D_RTYPE_SURFACE
:
727 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
728 context_detach_fbo_entry
);
736 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
738 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
739 struct fbo_entry
*entry
= context
->current_fbo
;
742 if (!entry
|| context
->rebind_fbo
) return;
744 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
746 if (surface
== entry
->render_targets
[i
])
748 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
749 context
->rebind_fbo
= TRUE
;
754 if (surface
== entry
->depth_stencil
)
756 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
757 context
->rebind_fbo
= TRUE
;
761 static BOOL
context_restore_pixel_format(struct wined3d_context
*ctx
)
763 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
766 if (ctx
->restore_pf
&& IsWindow(ctx
->restore_pf_win
))
768 if (ctx
->gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
770 HDC dc
= GetDC(ctx
->restore_pf_win
);
773 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, ctx
->restore_pf
))))
775 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
776 ctx
->restore_pf
, ctx
->restore_pf_win
);
778 ReleaseDC(ctx
->restore_pf_win
, dc
);
783 ERR("can't restore pixel format %d on window %p\n", ctx
->restore_pf
, ctx
->restore_pf_win
);
788 ctx
->restore_pf_win
= NULL
;
792 static BOOL
context_set_pixel_format(struct wined3d_context
*context
, HDC dc
, BOOL
private, int format
)
794 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
797 if (dc
== context
->hdc
&& context
->hdc_is_private
&& context
->hdc_has_format
)
800 current
= GetPixelFormat(dc
);
801 if (current
== format
) goto success
;
805 if (!SetPixelFormat(dc
, format
, NULL
))
807 /* This may also happen if the dc belongs to a destroyed window. */
808 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
809 format
, dc
, GetLastError());
813 context
->restore_pf
= 0;
814 context
->restore_pf_win
= private ? NULL
: WindowFromDC(dc
);
818 /* By default WGL doesn't allow pixel format adjustments but we need it
819 * here. For this reason there's a Wine specific wglSetPixelFormat()
820 * which allows us to set the pixel format multiple times. Only use it
821 * when really needed. */
822 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
826 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
828 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
833 win
= private ? NULL
: WindowFromDC(dc
);
834 if (win
!= context
->restore_pf_win
)
836 context_restore_pixel_format(context
);
838 context
->restore_pf
= private ? 0 : current
;
839 context
->restore_pf_win
= win
;
845 /* OpenGL doesn't allow pixel format adjustments. Print an error and
846 * continue using the old format. There's a big chance that the old
847 * format works although with a performance hit and perhaps rendering
849 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
850 format
, dc
, current
);
854 if (dc
== context
->hdc
&& context
->hdc_is_private
)
855 context
->hdc_has_format
= TRUE
;
859 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
861 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
864 if (!context_set_pixel_format(ctx
, ctx
->hdc
, ctx
->hdc_is_private
, ctx
->pixel_format
))
866 WARN("Failed to set pixel format %d on device context %p.\n",
867 ctx
->pixel_format
, ctx
->hdc
);
871 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
875 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
876 ctx
->glCtx
, ctx
->hdc
, GetLastError());
878 WARN("Trying fallback to the backup window.\n");
880 /* FIXME: If the context is destroyed it's no longer associated with
881 * a swapchain, so we can't use the swapchain to get a backup dc. To
882 * make this work windowless contexts would need to be handled by the
886 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
887 context_set_current(NULL
);
891 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
893 context_set_current(NULL
);
897 if (!context_set_pixel_format(ctx
, dc
, TRUE
, ctx
->pixel_format
))
899 ERR("Failed to set pixel format %d on device context %p.\n",
900 ctx
->pixel_format
, dc
);
901 context_set_current(NULL
);
905 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
907 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
909 context_set_current(NULL
);
919 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
921 if (!wglMakeCurrent(dc
, gl_ctx
))
923 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
924 gl_ctx
, dc
, GetLastError());
925 context_set_current(NULL
);
929 static void context_update_window(struct wined3d_context
*context
)
931 if (context
->win_handle
== context
->swapchain
->win_handle
)
934 TRACE("Updating context %p window from %p to %p.\n",
935 context
, context
->win_handle
, context
->swapchain
->win_handle
);
938 wined3d_release_dc(context
->win_handle
, context
->hdc
);
940 context
->win_handle
= context
->swapchain
->win_handle
;
941 context
->hdc_is_private
= FALSE
;
942 context
->hdc_has_format
= FALSE
;
943 context
->needs_set
= 1;
946 if (!(context
->hdc
= GetDC(context
->win_handle
)))
948 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
953 static void context_destroy_gl_resources(struct wined3d_context
*context
)
955 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
956 struct wined3d_timestamp_query
*timestamp_query
;
957 struct wined3d_occlusion_query
*occlusion_query
;
958 struct wined3d_event_query
*event_query
;
959 struct fbo_entry
*entry
, *entry2
;
964 restore_ctx
= wglGetCurrentContext();
965 restore_dc
= wglGetCurrentDC();
967 if (restore_ctx
== context
->glCtx
)
969 else if (context
->valid
)
970 context_set_gl_context(context
);
972 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
975 GL_EXTCALL(glDeleteQueriesARB(1, ×tamp_query
->id
));
976 timestamp_query
->context
= NULL
;
979 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
981 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
982 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
983 occlusion_query
->context
= NULL
;
986 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
990 if (gl_info
->supported
[ARB_SYNC
])
992 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
994 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
995 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
997 event_query
->context
= NULL
;
1000 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
1002 if (!context
->valid
) entry
->id
= 0;
1003 context_destroy_fbo_entry(context
, entry
);
1006 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1008 if (!context
->valid
) entry
->id
= 0;
1009 context_destroy_fbo_entry(context
, entry
);
1014 if (context
->dummy_arbfp_prog
)
1016 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
1019 if (gl_info
->supported
[ARB_TIMER_QUERY
])
1020 GL_EXTCALL(glDeleteQueriesARB(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
1022 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1023 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
1025 if (gl_info
->supported
[ARB_SYNC
])
1027 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1029 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
1032 else if (gl_info
->supported
[APPLE_FENCE
])
1034 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1036 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1039 else if (gl_info
->supported
[NV_FENCE
])
1041 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1043 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1047 checkGLcall("context cleanup");
1050 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1051 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1052 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1054 context_restore_pixel_format(context
);
1057 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1059 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1061 ERR("Failed to disable GL context.\n");
1064 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1066 if (!wglDeleteContext(context
->glCtx
))
1068 DWORD err
= GetLastError();
1069 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1073 DWORD
context_get_tls_idx(void)
1075 return wined3d_context_tls_idx
;
1078 void context_set_tls_idx(DWORD idx
)
1080 wined3d_context_tls_idx
= idx
;
1083 struct wined3d_context
*context_get_current(void)
1085 return TlsGetValue(wined3d_context_tls_idx
);
1088 BOOL
context_set_current(struct wined3d_context
*ctx
)
1090 struct wined3d_context
*old
= context_get_current();
1094 TRACE("Already using D3D context %p.\n", ctx
);
1102 TRACE("Switching away from destroyed context %p.\n", old
);
1103 context_destroy_gl_resources(old
);
1104 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1105 HeapFree(GetProcessHeap(), 0, old
);
1117 ERR("Trying to make invalid context %p current\n", ctx
);
1121 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1122 if (!context_set_gl_context(ctx
))
1126 else if(wglGetCurrentContext())
1128 TRACE("Clearing current D3D context.\n");
1129 if (!wglMakeCurrent(NULL
, NULL
))
1131 DWORD err
= GetLastError();
1132 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1133 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1138 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1141 void context_release(struct wined3d_context
*context
)
1143 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1147 if (!context
->level
)
1148 WARN("Context %p is not active.\n", context
);
1149 else if (context
!= context_get_current())
1150 WARN("Context %p is not the current context.\n", context
);
1153 if (!--context
->level
)
1155 if (context_restore_pixel_format(context
))
1156 context
->needs_set
= 1;
1157 if (context
->restore_ctx
)
1159 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1160 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
);
1161 context
->restore_ctx
= NULL
;
1162 context
->restore_dc
= NULL
;
1167 static void context_enter(struct wined3d_context
*context
)
1169 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1171 if (!context
->level
++)
1173 const struct wined3d_context
*current_context
= context_get_current();
1174 HGLRC current_gl
= wglGetCurrentContext();
1176 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1178 TRACE("Another GL context (%p on device context %p) is already current.\n",
1179 current_gl
, wglGetCurrentDC());
1180 context
->restore_ctx
= current_gl
;
1181 context
->restore_dc
= wglGetCurrentDC();
1182 context
->needs_set
= 1;
1184 else if (!context
->needs_set
&& !(context
->hdc_is_private
&& context
->hdc_has_format
)
1185 && context
->pixel_format
!= GetPixelFormat(context
->hdc
))
1186 context
->needs_set
= 1;
1190 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1192 DWORD rep
= context
->state_table
[state
].representative
;
1196 if (isStateDirty(context
, rep
)) return;
1198 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1199 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1200 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1201 context
->isStateDirty
[idx
] |= (1 << shift
);
1204 /* This function takes care of wined3d pixel format selection. */
1205 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1206 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1207 BOOL auxBuffers
, BOOL findCompatible
)
1210 BYTE redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1211 BYTE depthBits
=0, stencilBits
=0;
1212 unsigned int current_value
;
1213 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1216 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1217 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1218 auxBuffers
, findCompatible
);
1220 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1222 ERR("Unable to get color bits for format %s (%#x)!\n",
1223 debug_d3dformat(color_format
->id
), color_format
->id
);
1227 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1230 for (i
= 0; i
< cfg_count
; ++i
)
1232 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1235 /* For now only accept RGBA formats. Perhaps some day we will
1236 * allow floating point formats for pbuffers. */
1237 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1239 /* In window mode we need a window drawable format and double buffering. */
1240 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1242 if (cfg
->redSize
< redBits
)
1244 if (cfg
->greenSize
< greenBits
)
1246 if (cfg
->blueSize
< blueBits
)
1248 if (cfg
->alphaSize
< alphaBits
)
1250 if (cfg
->depthSize
< depthBits
)
1252 if (stencilBits
&& cfg
->stencilSize
!= stencilBits
)
1254 /* Check multisampling support. */
1255 if (cfg
->numSamples
)
1259 /* We try to locate a format which matches our requirements exactly. In case of
1260 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1261 if (cfg
->depthSize
== depthBits
)
1263 if (cfg
->stencilSize
== stencilBits
)
1265 if (cfg
->alphaSize
== alphaBits
)
1267 /* We like to have aux buffers in backbuffer mode */
1268 if (auxBuffers
&& cfg
->auxBuffers
)
1270 if (cfg
->redSize
== redBits
1271 && cfg
->greenSize
== greenBits
1272 && cfg
->blueSize
== blueBits
)
1275 if (value
> current_value
)
1277 iPixelFormat
= cfg
->iPixelFormat
;
1278 current_value
= value
;
1282 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1283 if(!iPixelFormat
&& !findCompatible
) {
1284 ERR("Can't find a suitable iPixelFormat\n");
1286 } else if(!iPixelFormat
) {
1287 PIXELFORMATDESCRIPTOR pfd
;
1289 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1290 /* PixelFormat selection */
1291 ZeroMemory(&pfd
, sizeof(pfd
));
1292 pfd
.nSize
= sizeof(pfd
);
1294 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1295 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1296 pfd
.cAlphaBits
= alphaBits
;
1297 pfd
.cColorBits
= colorBits
;
1298 pfd
.cDepthBits
= depthBits
;
1299 pfd
.cStencilBits
= stencilBits
;
1300 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1302 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1304 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1305 ERR("Can't find a suitable iPixelFormat\n");
1310 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1311 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1312 return iPixelFormat
;
1315 /* Context activation is done by the caller. */
1316 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1318 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1319 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1321 for (i
= 0; i
< count
; ++i
)
1323 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1324 checkGLcall("glActiveTextureARB");
1326 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1327 checkGLcall("glBindTexture");
1329 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1331 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1332 checkGLcall("glBindTexture");
1335 if (gl_info
->supported
[EXT_TEXTURE3D
])
1337 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1338 checkGLcall("glBindTexture");
1341 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1343 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1344 checkGLcall("glBindTexture");
1349 BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1351 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1352 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1355 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1356 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1360 case GL_DEBUG_TYPE_ERROR_ARB
:
1361 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1364 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1365 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1366 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1367 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1370 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1371 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1375 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1380 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1381 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1383 struct wined3d_device
*device
= swapchain
->device
;
1384 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1385 const struct wined3d_format
*color_format
;
1386 struct wined3d_context
*ret
;
1387 BOOL auxBuffers
= FALSE
;
1388 HGLRC ctx
, share_ctx
;
1394 BOOL hdc_is_private
= FALSE
;
1396 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1398 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1402 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1403 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1404 if (!ret
->blit_targets
)
1407 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1408 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1409 if (!ret
->draw_buffers
)
1412 ret
->free_timestamp_query_size
= 4;
1413 ret
->free_timestamp_queries
= HeapAlloc(GetProcessHeap(), 0,
1414 ret
->free_timestamp_query_size
* sizeof(*ret
->free_timestamp_queries
));
1415 if (!ret
->free_timestamp_queries
)
1417 list_init(&ret
->timestamp_queries
);
1419 ret
->free_occlusion_query_size
= 4;
1420 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1421 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1422 if (!ret
->free_occlusion_queries
)
1425 list_init(&ret
->occlusion_queries
);
1427 ret
->free_event_query_size
= 4;
1428 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1429 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1430 if (!ret
->free_event_queries
)
1433 list_init(&ret
->event_queries
);
1434 list_init(&ret
->fbo_list
);
1435 list_init(&ret
->fbo_destroy_list
);
1437 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1439 ERR("Failed to allocate shader backend context data.\n");
1443 /* Initialize the texture unit mapping to a 1:1 mapping */
1444 for (s
= 0; s
< MAX_COMBINED_SAMPLERS
; ++s
)
1446 if (s
< gl_info
->limits
.fragment_samplers
)
1448 ret
->tex_unit_map
[s
] = s
;
1449 ret
->rev_tex_unit_map
[s
] = s
;
1453 ret
->tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1454 ret
->rev_tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1458 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1460 WARN("Failed to retireve device context, trying swapchain backup.\n");
1462 if ((hdc
= swapchain_get_backup_dc(swapchain
)))
1463 hdc_is_private
= TRUE
;
1466 ERR("Failed to retrieve a device context.\n");
1471 color_format
= target
->resource
.format
;
1473 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1474 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1475 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1479 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1480 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1481 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1482 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1485 /* DirectDraw supports 8bit paletted render targets and these are used by
1486 * old games like StarCraft and C&C. Most modern hardware doesn't support
1487 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1488 * conversion (ab)uses the alpha component for storing the palette index.
1489 * For this reason we require a format with 8bit alpha, so request
1491 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1492 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1494 /* Try to find a pixel format which matches our requirements. */
1495 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1497 /* Try to locate a compatible format if we weren't able to find anything. */
1500 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1501 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1504 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1507 ERR("Can't find a suitable pixel format.\n");
1513 ret
->gl_info
= gl_info
;
1515 if (!context_set_pixel_format(ret
, hdc
, hdc_is_private
, pixel_format
))
1517 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1518 context_release(ret
);
1522 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1523 if (gl_info
->p_wglCreateContextAttribsARB
)
1525 unsigned int ctx_attrib_idx
= 0;
1526 GLint ctx_attribs
[3];
1528 if (context_debug_output_enabled(gl_info
))
1530 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1531 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_DEBUG_BIT_ARB
;
1533 ctx_attribs
[ctx_attrib_idx
] = 0;
1535 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1537 ERR("Failed to create a WGL context.\n");
1538 context_release(ret
);
1544 if (!(ctx
= wglCreateContext(hdc
)))
1546 ERR("Failed to create a WGL context.\n");
1547 context_release(ret
);
1551 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1553 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1554 context_release(ret
);
1555 if (!wglDeleteContext(ctx
))
1556 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1561 if (!device_context_add(device
, ret
))
1563 ERR("Failed to add the newly created context to the context list\n");
1564 context_release(ret
);
1565 if (!wglDeleteContext(ctx
))
1566 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1570 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1571 ret
->state_table
= device
->StateTable
;
1573 /* Mark all states dirty to force a proper initialization of the states
1574 * on the first use of the context. */
1575 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1577 if (ret
->state_table
[state
].representative
)
1578 context_invalidate_state(ret
, state
);
1581 ret
->swapchain
= swapchain
;
1582 ret
->current_rt
= target
;
1583 ret
->tid
= GetCurrentThreadId();
1585 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
1586 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1590 ret
->win_handle
= swapchain
->win_handle
;
1592 ret
->hdc_is_private
= hdc_is_private
;
1593 ret
->hdc_has_format
= TRUE
;
1594 ret
->pixel_format
= pixel_format
;
1597 /* Set up the context defaults */
1598 if (!context_set_current(ret
))
1600 ERR("Cannot activate context to set up defaults.\n");
1601 device_context_remove(device
, ret
);
1602 context_release(ret
);
1603 if (!wglDeleteContext(ctx
))
1604 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1608 if (context_debug_output_enabled(gl_info
))
1610 GL_EXTCALL(glDebugMessageCallbackARB(wined3d_debug_callback
, ret
));
1611 if (TRACE_ON(d3d_synchronous
))
1612 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
);
1613 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1616 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR_ARB
,
1617 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1621 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
,
1622 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1623 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
,
1624 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1625 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY_ARB
,
1626 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1628 if (WARN_ON(d3d_perf
))
1630 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE_ARB
,
1631 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1635 switch (swapchain
->desc
.swap_interval
)
1637 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1640 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1641 case WINED3DPRESENT_INTERVAL_ONE
:
1644 case WINED3DPRESENT_INTERVAL_TWO
:
1647 case WINED3DPRESENT_INTERVAL_THREE
:
1650 case WINED3DPRESENT_INTERVAL_FOUR
:
1654 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1658 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1660 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1661 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1662 swap_interval
, ret
, GetLastError());
1665 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1667 TRACE("Setting up the screen\n");
1669 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1670 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1672 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1673 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1675 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1676 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1678 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1679 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1680 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1681 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1683 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1685 /* Direct3D always uses n-1 weights for n world matrices and uses
1686 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1687 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1688 * enabled as well. */
1689 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1690 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1692 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1694 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1695 * the previous texture where to source the offset from is always unit - 1.
1697 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1699 context_active_texture(ret
, gl_info
, s
);
1700 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1701 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1702 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1705 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1707 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1708 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1709 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1710 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1713 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1714 * program and the dummy program is destroyed when the context is destroyed.
1716 static const char dummy_program
[] =
1718 "MOV result.color, fragment.color.primary;\n"
1720 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1721 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1722 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1725 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1727 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1729 context_active_texture(ret
, gl_info
, s
);
1730 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1731 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1735 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1737 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1739 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1741 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1743 ret
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
1744 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
1745 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
1747 /* If this happens to be the first context for the device, dummy textures
1748 * are not created yet. In that case, they will be created (and bound) by
1749 * create_dummy_textures right after this context is initialized. */
1750 if (device
->dummy_texture_2d
[0])
1751 bind_dummy_textures(device
, ret
);
1753 TRACE("Created context %p.\n", ret
);
1758 device
->shader_backend
->shader_free_context_data(ret
);
1759 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1760 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1761 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
1762 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1763 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1764 HeapFree(GetProcessHeap(), 0, ret
);
1768 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1772 TRACE("Destroying ctx %p\n", context
);
1774 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1776 context_destroy_gl_resources(context
);
1777 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1782 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1783 in wined3d_adapter may go away in the meantime */
1784 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
1785 *gl_info
= *context
->gl_info
;
1786 context
->gl_info
= gl_info
;
1787 context
->destroyed
= 1;
1791 device
->shader_backend
->shader_free_context_data(context
);
1792 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1793 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1794 device_context_remove(device
, context
);
1795 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1798 /* Context activation is done by the caller. */
1799 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
1801 const GLdouble projection
[] =
1803 2.0 / width
, 0.0, 0.0, 0.0,
1804 0.0, 2.0 / height
, 0.0, 0.0,
1806 -1.0, -1.0, -1.0, 1.0,
1809 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
1810 checkGLcall("glMatrixMode(GL_PROJECTION)");
1811 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
1812 checkGLcall("glLoadMatrixd");
1813 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
1814 checkGLcall("glViewport");
1817 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
1819 const struct wined3d_surface
*rt
= context
->current_rt
;
1821 if (rt
->container
->swapchain
&& rt
->container
->swapchain
->front_buffer
== rt
->container
)
1825 GetClientRect(context
->win_handle
, &window_size
);
1826 size
->cx
= window_size
.right
- window_size
.left
;
1827 size
->cy
= window_size
.bottom
- window_size
.top
;
1832 size
->cx
= rt
->resource
.width
;
1833 size
->cy
= rt
->resource
.height
;
1836 /*****************************************************************************
1839 * Sets up a context for DirectDraw blitting.
1840 * All texture units are disabled, texture unit 0 is set as current unit
1841 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1842 * color writing enabled for all channels
1843 * register combiners disabled, shaders disabled
1844 * world matrix is set to identity, texture matrix 0 too
1845 * projection matrix is setup for drawing screen coordinates
1848 * This: Device to activate the context for
1849 * context: Context to setup
1851 *****************************************************************************/
1852 /* Context activation is done by the caller. */
1853 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
1856 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1860 TRACE("Setting up context %p for blitting\n", context
);
1862 context_get_rt_size(context
, &rt_size
);
1864 if (context
->last_was_blit
)
1866 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
1868 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1869 context
->blit_w
= rt_size
.cx
;
1870 context
->blit_h
= rt_size
.cy
;
1871 /* No need to dirtify here, the states are still dirtified because
1872 * they weren't applied since the last SetupForBlit() call. */
1874 TRACE("Context is already set up for blitting, nothing to do\n");
1877 context
->last_was_blit
= TRUE
;
1879 /* Disable all textures. The caller can then bind a texture it wants to blit
1882 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1883 * function texture unit. No need to care for higher samplers
1885 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1887 sampler
= context
->rev_tex_unit_map
[i
];
1888 context_active_texture(context
, gl_info
, i
);
1890 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1892 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1893 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1895 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1896 checkGLcall("glDisable GL_TEXTURE_3D");
1897 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1899 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1900 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1902 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1903 checkGLcall("glDisable GL_TEXTURE_2D");
1905 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1906 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1908 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1910 if (sampler
< MAX_TEXTURES
)
1911 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1912 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1915 context_active_texture(context
, gl_info
, 0);
1917 sampler
= context
->rev_tex_unit_map
[0];
1919 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1921 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1922 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1924 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1925 checkGLcall("glDisable GL_TEXTURE_3D");
1926 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1928 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1929 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1931 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1932 checkGLcall("glDisable GL_TEXTURE_2D");
1934 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1936 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
1937 checkGLcall("glMatrixMode(GL_TEXTURE)");
1938 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1939 checkGLcall("glLoadIdentity()");
1941 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1943 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1944 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
1945 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1948 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1950 if (sampler
< MAX_TEXTURES
)
1952 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
1953 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1955 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1958 /* Other misc states */
1959 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
1960 checkGLcall("glDisable(GL_ALPHA_TEST)");
1961 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
1962 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
1963 checkGLcall("glDisable GL_LIGHTING");
1964 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
1965 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
1966 checkGLcall("glDisable GL_DEPTH_TEST");
1967 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
1968 glDisableWINE(GL_FOG
);
1969 checkGLcall("glDisable GL_FOG");
1970 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
1971 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
1972 checkGLcall("glDisable GL_BLEND");
1973 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
1974 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
1975 checkGLcall("glDisable GL_CULL_FACE");
1976 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
1977 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
1978 checkGLcall("glDisable GL_STENCIL_TEST");
1979 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
1980 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
1981 checkGLcall("glDisable GL_SCISSOR_TEST");
1982 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
1983 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1985 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
1986 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1987 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
1989 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1990 checkGLcall("glColorMask");
1991 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
1992 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
1993 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
1994 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
1995 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1997 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
1998 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
1999 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2002 /* Setup transforms */
2003 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2004 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2005 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2006 checkGLcall("glLoadIdentity()");
2007 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2009 context
->last_was_rhw
= TRUE
;
2010 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2012 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2013 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2014 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2015 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2016 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2017 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2018 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2020 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2022 /* Disable shaders */
2023 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2025 context
->blit_w
= rt_size
.cx
;
2026 context
->blit_h
= rt_size
.cy
;
2027 context_invalidate_state(context
, STATE_VIEWPORT
);
2028 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2031 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2033 return rt_mask
& (1 << 31);
2036 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2038 return rt_mask
& ~(1 << 31);
2041 /* Context activation is done by the caller. */
2042 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2044 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2048 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2049 checkGLcall("glDrawBuffer()");
2051 else if (is_rt_mask_onscreen(rt_mask
))
2053 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2054 checkGLcall("glDrawBuffer()");
2058 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2065 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2067 context
->draw_buffers
[i
] = GL_NONE
;
2073 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2075 GL_EXTCALL(glDrawBuffersARB(i
, context
->draw_buffers
));
2076 checkGLcall("glDrawBuffers()");
2080 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2081 checkGLcall("glDrawBuffer()");
2086 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2091 /* Context activation is done by the caller. */
2092 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2094 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2095 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2096 DWORD new_mask
= context_generate_rt_mask(buffer
);
2098 if (new_mask
== *current_mask
)
2101 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2102 checkGLcall("glDrawBuffer()");
2104 *current_mask
= new_mask
;
2107 /* Context activation is done by the caller. */
2108 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2110 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0
+ unit
));
2111 checkGLcall("glActiveTextureARB");
2112 context
->active_texture
= unit
;
2115 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2117 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2118 DWORD unit
= context
->active_texture
;
2119 DWORD old_texture_type
= context
->texture_type
[unit
];
2123 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2124 checkGLcall("glBindTexture");
2131 if (old_texture_type
!= target
)
2133 const struct wined3d_device
*device
= context
->swapchain
->device
;
2135 switch (old_texture_type
)
2141 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2142 checkGLcall("glBindTexture");
2144 case GL_TEXTURE_RECTANGLE_ARB
:
2145 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2146 checkGLcall("glBindTexture");
2148 case GL_TEXTURE_CUBE_MAP
:
2149 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2150 checkGLcall("glBindTexture");
2153 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2154 checkGLcall("glBindTexture");
2157 ERR("Unexpected texture target %#x\n", old_texture_type
);
2160 context
->texture_type
[unit
] = target
;
2164 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2166 if (context
->render_offscreen
== offscreen
) return;
2168 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2169 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2170 context_invalidate_state(context
, STATE_VIEWPORT
);
2171 context_invalidate_state(context
, STATE_SCISSORRECT
);
2172 context_invalidate_state(context
, STATE_FRONTFACE
);
2173 context
->render_offscreen
= offscreen
;
2176 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2177 const struct wined3d_format
*required
)
2179 BYTE existing_depth
, existing_stencil
, required_depth
, required_stencil
;
2181 if (existing
== required
) return TRUE
;
2182 if ((existing
->flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
2184 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
2185 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
2187 if(existing_depth
< required_depth
) return FALSE
;
2188 /* If stencil bits are used the exact amount is required - otherwise wrapping
2189 * won't work correctly */
2190 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
2194 /* The caller provides a context */
2195 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2196 const struct wined3d_rendertarget_view
*depth_stencil
)
2198 /* Onscreen surfaces are always in a swapchain */
2199 struct wined3d_swapchain
*swapchain
= context
->current_rt
->container
->swapchain
;
2201 if (context
->render_offscreen
|| !depth_stencil
) return;
2202 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2204 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2205 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2207 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2209 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2210 surface_load_location(context
->current_rt
, WINED3D_LOCATION_TEXTURE_RGB
);
2211 swapchain
->render_to_fbo
= TRUE
;
2212 swapchain_update_draw_bindings(swapchain
);
2213 context_set_render_offscreen(context
, TRUE
);
2216 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
2218 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2220 else if (rt
->container
->swapchain
)
2221 return context_generate_rt_mask_from_surface(rt
);
2223 return context_generate_rt_mask(device
->offscreenBuffer
);
2226 /* Context activation is done by the caller. */
2227 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2229 struct wined3d_surface
*rt
= context
->current_rt
;
2230 DWORD rt_mask
, *cur_mask
;
2232 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2234 context_validate_onscreen_formats(context
, NULL
);
2236 if (context
->render_offscreen
)
2238 wined3d_texture_load(rt
->container
, context
, FALSE
);
2240 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->container
->resource
.draw_binding
);
2241 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2248 context
->current_fbo
= NULL
;
2249 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2250 rt_mask
= context_generate_rt_mask_from_surface(rt
);
2255 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt
);
2258 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2260 if (rt_mask
!= *cur_mask
)
2262 context_apply_draw_buffers(context
, rt_mask
);
2263 *cur_mask
= rt_mask
;
2266 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2268 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2271 SetupForBlit(device
, context
);
2272 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2275 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2276 const struct wined3d_rendertarget_view
*ds
)
2280 if (ds
) return TRUE
;
2282 for (i
= 0; i
< rt_count
; ++i
)
2284 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2288 WARN("Invalid render target config, need at least one attachment.\n");
2292 /* Context activation is done by the caller. */
2293 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2294 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2296 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2297 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2298 DWORD rt_mask
= 0, *cur_mask
;
2301 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2302 || rt_count
!= context
->gl_info
->limits
.buffers
)
2304 if (!context_validate_rt_config(rt_count
, rts
, fb
->depth_stencil
))
2307 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2309 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2311 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2313 for (i
= 0; i
< rt_count
; ++i
)
2315 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(rts
[i
]);
2316 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2317 rt_mask
|= (1 << i
);
2319 while (i
< context
->gl_info
->limits
.buffers
)
2321 context
->blit_targets
[i
] = NULL
;
2324 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2325 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2326 rt_count
? rts
[0]->resource
->draw_binding
: WINED3D_LOCATION_TEXTURE_RGB
);
2330 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, WINED3D_LOCATION_DRAWABLE
);
2331 rt_mask
= context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2334 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2335 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2336 * state management allows this */
2337 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2341 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2342 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2345 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2346 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2348 for (i
= 0; i
< rt_count
; ++i
)
2350 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2351 rt_mask
|= (1 << i
);
2356 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2357 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2360 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2362 if (rt_mask
!= *cur_mask
)
2364 context_apply_draw_buffers(context
, rt_mask
);
2365 *cur_mask
= rt_mask
;
2366 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2369 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2371 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2374 if (context
->last_was_blit
)
2375 context
->last_was_blit
= FALSE
;
2377 /* Blending and clearing should be orthogonal, but tests on the nvidia
2378 * driver show that disabling blending when clearing improves the clearing
2379 * performance incredibly. */
2380 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2381 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2382 checkGLcall("glEnable GL_SCISSOR_TEST");
2384 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2385 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2386 context_invalidate_state(context
, STATE_SCISSORRECT
);
2391 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_device
*device
)
2393 const struct wined3d_state
*state
= &device
->state
;
2394 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
2395 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2396 DWORD rt_mask
, rt_mask_bits
;
2399 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2400 return context_generate_rt_mask_no_fbo(device
, wined3d_rendertarget_view_get_surface(rts
[0]));
2401 else if (!context
->render_offscreen
)
2402 return context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2404 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2405 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2406 rt_mask_bits
= rt_mask
;
2408 while (rt_mask_bits
)
2410 rt_mask_bits
&= ~(1 << i
);
2411 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
2412 rt_mask
&= ~(1 << i
);
2420 /* Context activation is done by the caller. */
2421 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2423 const struct wined3d_device
*device
= context
->swapchain
->device
;
2424 const struct wined3d_fb_state
*fb
= state
->fb
;
2425 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2428 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2430 if (!context
->render_offscreen
)
2432 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, WINED3D_LOCATION_DRAWABLE
);
2438 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
2440 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(fb
->render_targets
[i
]);
2442 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2443 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2444 fb
->render_targets
[0]->resource
->draw_binding
);
2448 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2449 if (rt_mask
!= *cur_mask
)
2451 context_apply_draw_buffers(context
, rt_mask
);
2452 *cur_mask
= rt_mask
;
2456 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
2458 DWORD i
= context
->rev_tex_unit_map
[unit
];
2459 DWORD j
= context
->tex_unit_map
[stage
];
2461 context
->tex_unit_map
[stage
] = unit
;
2462 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2463 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2465 context
->rev_tex_unit_map
[unit
] = stage
;
2466 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2467 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2470 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
2474 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2475 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
2478 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
2479 const struct wined3d_state
*state
)
2483 context
->fixed_function_usage_map
= 0;
2484 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2486 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2487 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2488 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2489 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2490 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2491 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2492 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2493 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2495 /* Not used, and disable higher stages. */
2496 if (color_op
== WINED3D_TOP_DISABLE
)
2499 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2500 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2501 || ((color_arg3
== WINED3DTA_TEXTURE
)
2502 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2503 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2504 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2505 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2506 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2507 context
->fixed_function_usage_map
|= (1 << i
);
2509 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2510 && i
< MAX_TEXTURES
- 1)
2511 context
->fixed_function_usage_map
|= (1 << (i
+ 1));
2514 if (i
< context
->lowest_disabled_stage
)
2517 end
= context
->lowest_disabled_stage
;
2521 start
= context
->lowest_disabled_stage
;
2525 context
->lowest_disabled_stage
= i
;
2526 for (i
= start
+ 1; i
< end
; ++i
)
2528 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
2532 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
2533 const struct wined3d_state
*state
)
2535 unsigned int i
, tex
;
2537 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2539 context_update_fixed_function_usage_map(context
, state
);
2540 ffu_map
= context
->fixed_function_usage_map
;
2542 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2543 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2545 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2550 if (context
->tex_unit_map
[i
] != i
)
2552 context_map_stage(context
, i
, i
);
2553 context_invalidate_state(context
, STATE_SAMPLER(i
));
2554 context_invalidate_texture_stage(context
, i
);
2560 /* Now work out the mapping */
2562 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2567 if (context
->tex_unit_map
[i
] != tex
)
2569 context_map_stage(context
, i
, tex
);
2570 context_invalidate_state(context
, STATE_SAMPLER(i
));
2571 context_invalidate_texture_stage(context
, i
);
2578 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2580 const enum wined3d_sampler_texture_type
*sampler_type
=
2581 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.sampler_type
;
2583 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2585 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2587 if (sampler_type
[i
] && context
->tex_unit_map
[i
] != i
)
2589 context_map_stage(context
, i
, i
);
2590 context_invalidate_state(context
, STATE_SAMPLER(i
));
2591 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2592 context_invalidate_texture_stage(context
, i
);
2597 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
2598 const enum wined3d_sampler_texture_type
*pshader_sampler_tokens
,
2599 const enum wined3d_sampler_texture_type
*vshader_sampler_tokens
, DWORD unit
)
2601 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
2603 /* Not currently used */
2604 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
2607 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
2609 /* Used by a fragment sampler */
2611 if (!pshader_sampler_tokens
)
2613 /* No pixel shader, check fixed function */
2614 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1 << current_mapping
));
2617 /* Pixel shader, check the shader's sampler map */
2618 return !pshader_sampler_tokens
[current_mapping
];
2621 /* Used by a vertex sampler */
2622 return !vshader_sampler_tokens
[current_mapping
- MAX_FRAGMENT_SAMPLERS
];
2625 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
2627 const enum wined3d_sampler_texture_type
*vshader_sampler_type
=
2628 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.sampler_type
;
2629 const enum wined3d_sampler_texture_type
*pshader_sampler_type
= NULL
;
2630 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2631 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2636 /* Note that we only care if a sampler is sampled or not, not the sampler's specific type.
2637 * Otherwise we'd need to call shader_update_samplers() here for 1.x pixelshaders. */
2638 pshader_sampler_type
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.sampler_type
;
2641 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
2642 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2643 if (vshader_sampler_type
[i
])
2645 if (context
->tex_unit_map
[vsampler_idx
] != WINED3D_UNMAPPED_STAGE
)
2647 /* Already mapped somewhere */
2653 if (context_unit_free_for_vs(context
, pshader_sampler_type
, vshader_sampler_type
, start
))
2655 context_map_stage(context
, vsampler_idx
, start
);
2656 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
2668 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2670 BOOL vs
= use_vs(state
);
2671 BOOL ps
= use_ps(state
);
2674 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2675 * that would be really messy and require shader recompilation
2676 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2677 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2680 context_map_psamplers(context
, state
);
2682 context_map_fixed_function_samplers(context
, state
);
2685 context_map_vsamplers(context
, ps
, state
);
2688 /* Context activation is done by the caller. */
2689 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2691 const struct wined3d_device
*device
= context
->swapchain
->device
;
2692 DWORD rt_mask
, *cur_mask
;
2694 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2696 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2697 rt_mask
= find_draw_buffers_mask(context
, device
);
2698 if (rt_mask
!= *cur_mask
)
2700 context_apply_draw_buffers(context
, rt_mask
);
2701 *cur_mask
= rt_mask
;
2705 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
2707 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
2708 *regnum
= WINED3D_FFP_POSITION
;
2709 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
2710 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
2711 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
2712 *regnum
= WINED3D_FFP_BLENDINDICES
;
2713 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
2714 *regnum
= WINED3D_FFP_NORMAL
;
2715 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
2716 *regnum
= WINED3D_FFP_PSIZE
;
2717 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
2718 *regnum
= WINED3D_FFP_DIFFUSE
;
2719 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
2720 *regnum
= WINED3D_FFP_SPECULAR
;
2721 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
2722 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
2725 FIXME("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
2733 /* Context activation is done by the caller. */
2734 void context_stream_info_from_declaration(struct wined3d_context
*context
,
2735 const struct wined3d_state
*state
, struct wined3d_stream_info
*stream_info
)
2737 /* We need to deal with frequency data! */
2738 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
2739 BOOL use_vshader
= use_vs(state
);
2742 stream_info
->use_map
= 0;
2743 stream_info
->swizzle_map
= 0;
2744 stream_info
->position_transformed
= declaration
->position_transformed
;
2746 /* Translate the declaration into strided data. */
2747 for (i
= 0; i
< declaration
->element_count
; ++i
)
2749 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
2750 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
2754 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
2755 element
, i
+ 1, declaration
->element_count
);
2757 if (!stream
->buffer
)
2760 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
2764 if (element
->output_slot
== ~0U)
2766 /* TODO: Assuming vertexdeclarations are usually used with the
2767 * same or a similar shader, it might be worth it to store the
2768 * last used output slot and try that one first. */
2769 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
2770 element
->usage
, element
->usage_idx
, &idx
);
2774 idx
= element
->output_slot
;
2780 if (!element
->ffp_valid
)
2782 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
2783 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
2784 stride_used
= FALSE
;
2788 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
2794 TRACE("Load %s array %u [usage %s, usage_idx %u, "
2795 "input_slot %u, offset %u, stride %u, format %s].\n",
2796 use_vshader
? "shader": "fixed function", idx
,
2797 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
2798 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
));
2800 stream_info
->elements
[idx
].format
= element
->format
;
2801 stream_info
->elements
[idx
].data
.buffer_object
= 0;
2802 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
2803 stream_info
->elements
[idx
].stride
= stream
->stride
;
2804 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
2806 if (!context
->gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2807 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
2809 stream_info
->swizzle_map
|= 1 << idx
;
2811 stream_info
->use_map
|= 1 << idx
;
2816 /* Context activation is done by the caller. */
2817 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2819 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2820 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2821 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
2822 DWORD prev_all_vbo
= stream_info
->all_vbo
;
2826 context_stream_info_from_declaration(context
, state
, stream_info
);
2828 stream_info
->all_vbo
= 1;
2829 context
->num_buffer_queries
= 0;
2830 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
2832 struct wined3d_stream_info_element
*element
;
2833 struct wined3d_bo_address data
;
2834 struct wined3d_buffer
*buffer
;
2839 element
= &stream_info
->elements
[i
];
2840 buffer
= state
->streams
[element
->stream_idx
].buffer
;
2842 /* We can't use VBOs if the base vertex index is negative. OpenGL
2843 * doesn't accept negative offsets (or rather offsets bigger than the
2844 * VBO, because the pointer is unsigned), so use system memory
2845 * sources. In most sane cases the pointer - offset will still be > 0,
2846 * otherwise it will wrap around to some big value. Hope that with the
2847 * indices the driver wraps it back internally. If not,
2848 * drawStridedSlow is needed, including a vertex buffer path. */
2849 if (state
->load_base_vertex_index
< 0)
2851 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
2852 state
->load_base_vertex_index
);
2853 element
->data
.buffer_object
= 0;
2854 element
->data
.addr
+= (ULONG_PTR
)buffer_get_sysmem(buffer
, context
);
2855 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
2856 FIXME("System memory vertex data load offset is negative!\n");
2860 buffer_internal_preload(buffer
, context
, state
);
2861 buffer_get_memory(buffer
, context
, &data
);
2862 element
->data
.buffer_object
= data
.buffer_object
;
2863 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
2866 if (!element
->data
.buffer_object
)
2867 stream_info
->all_vbo
= 0;
2870 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
2872 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
2877 if (state
->vertex_declaration
->half_float_conv_needed
&& !stream_info
->all_vbo
)
2879 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
2880 context
->use_immediate_mode_draw
= TRUE
;
2884 context
->use_immediate_mode_draw
= FALSE
;
2889 WORD slow_mask
= (1 << WINED3D_FFP_PSIZE
);
2890 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2891 & ((1 << WINED3D_FFP_DIFFUSE
) | (1 << WINED3D_FFP_SPECULAR
));
2893 if (((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
2894 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
2895 context
->use_immediate_mode_draw
= TRUE
;
2897 context
->use_immediate_mode_draw
= FALSE
;
2900 if (prev_all_vbo
!= stream_info
->all_vbo
)
2901 context_invalidate_state(context
, STATE_INDEXBUFFER
);
2904 /* Context activation is done by the caller. */
2905 static void context_preload_texture(struct wined3d_context
*context
,
2906 const struct wined3d_state
*state
, unsigned int idx
)
2908 struct wined3d_texture
*texture
;
2910 if (!(texture
= state
->textures
[idx
]))
2913 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
2916 /* Context activation is done by the caller. */
2917 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2923 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
2925 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.sampler_type
[i
])
2926 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
2932 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2934 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.sampler_type
[i
])
2935 context_preload_texture(context
, state
, i
);
2940 WORD ffu_map
= context
->fixed_function_usage_map
;
2942 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2945 context_preload_texture(context
, state
, i
);
2950 /* Context activation is done by the caller. */
2951 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
2953 const struct wined3d_state
*state
= &device
->state
;
2954 const struct StateEntry
*state_table
= context
->state_table
;
2955 const struct wined3d_fb_state
*fb
= state
->fb
;
2959 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
2960 fb
->render_targets
, fb
->depth_stencil
))
2963 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
2965 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2968 /* Preload resources before FBO setup. Texture preload in particular may
2969 * result in changes to the current FBO, due to using e.g. FBO blits for
2970 * updating a resource location. */
2971 context_update_tex_unit_map(context
, state
);
2972 context_preload_textures(context
, state
);
2973 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
))
2975 context_update_stream_info(context
, state
);
2979 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
2982 buffer_mark_used(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
);
2985 if (state
->index_buffer
)
2987 if (context
->stream_info
.all_vbo
)
2988 buffer_internal_preload(state
->index_buffer
, context
, state
);
2990 buffer_get_sysmem(state
->index_buffer
, context
);
2993 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
2995 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
2997 if (state
->cb
[i
][j
])
2998 buffer_internal_preload(state
->cb
[i
][j
], context
, state
);
3002 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3004 DWORD rep
= context
->dirtyArray
[i
];
3005 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3006 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3007 context
->isStateDirty
[idx
] &= ~(1 << shift
);
3008 state_table
[rep
].apply(context
, state
, rep
);
3011 if (context
->shader_update_mask
)
3013 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3014 context
->shader_update_mask
= 0;
3017 if (context
->constant_update_mask
)
3019 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3020 context
->constant_update_mask
= 0;
3023 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3025 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3028 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3029 context
->last_was_blit
= FALSE
;
3034 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
3036 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3038 render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
3039 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
3041 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3042 * the alpha blend state changes with different render target formats. */
3043 if (!context
->current_rt
)
3045 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3049 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
3050 const struct wined3d_format
*new = target
->resource
.format
;
3052 if (old
->id
!= new->id
)
3054 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3055 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3056 || !(new->flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3057 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3059 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3060 if ((old
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3061 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3064 /* When switching away from an offscreen render target, and we're not
3065 * using FBOs, we have to read the drawable into the texture. This is
3066 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
3067 * There are some things that need care though. PreLoad needs a GL context,
3068 * and FindContext is called before the context is activated. It also
3069 * has to be called with the old rendertarget active, otherwise a
3070 * wrong drawable is read. */
3071 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
3072 && old_render_offscreen
&& context
->current_rt
!= target
)
3074 struct wined3d_texture
*texture
= context
->current_rt
->container
;
3076 /* Read the back buffer of the old drawable into the destination texture. */
3077 if (texture
->texture_srgb
.name
)
3078 wined3d_texture_load(texture
, context
, TRUE
);
3079 wined3d_texture_load(texture
, context
, FALSE
);
3080 surface_invalidate_location(context
->current_rt
, WINED3D_LOCATION_DRAWABLE
);
3084 context
->current_rt
= target
;
3085 context_set_render_offscreen(context
, render_offscreen
);
3088 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
3090 struct wined3d_context
*current_context
= context_get_current();
3091 struct wined3d_context
*context
;
3093 TRACE("device %p, target %p.\n", device
, target
);
3095 if (current_context
&& current_context
->destroyed
)
3096 current_context
= NULL
;
3101 && current_context
->current_rt
3102 && current_context
->swapchain
->device
== device
)
3104 target
= current_context
->current_rt
;
3108 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
3109 if (swapchain
->back_buffers
)
3110 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
3112 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
3116 if (current_context
&& current_context
->current_rt
== target
)
3118 context
= current_context
;
3120 else if (target
->container
->swapchain
)
3122 TRACE("Rendering onscreen.\n");
3124 context
= swapchain_get_context(target
->container
->swapchain
);
3128 TRACE("Rendering offscreen.\n");
3130 /* Stay with the current context if possible. Otherwise use the
3131 * context for the primary swapchain. */
3132 if (current_context
&& current_context
->swapchain
->device
== device
)
3133 context
= current_context
;
3135 context
= swapchain_get_context(device
->swapchains
[0]);
3138 context_enter(context
);
3139 context_update_window(context
);
3140 context_setup_target(context
, target
);
3141 if (!context
->valid
) return context
;
3143 if (context
!= current_context
)
3145 if (!context_set_current(context
))
3146 ERR("Failed to activate the new context.\n");
3148 else if (context
->needs_set
)
3150 context_set_gl_context(context
);