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
23 #include "wine/port.h"
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous
);
36 #define WINED3D_MAX_FBO_ENTRIES 64
38 static DWORD wined3d_context_tls_idx
;
40 /* FBO helper functions */
42 /* Context activation is done by the caller. */
43 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint fbo
)
45 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
49 case GL_READ_FRAMEBUFFER
:
50 if (context
->fbo_read_binding
== fbo
) return;
51 context
->fbo_read_binding
= fbo
;
54 case GL_DRAW_FRAMEBUFFER
:
55 if (context
->fbo_draw_binding
== fbo
) return;
56 context
->fbo_draw_binding
= fbo
;
60 if (context
->fbo_read_binding
== fbo
61 && context
->fbo_draw_binding
== fbo
) return;
62 context
->fbo_read_binding
= fbo
;
63 context
->fbo_draw_binding
= fbo
;
67 FIXME("Unhandled target %#x.\n", target
);
71 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
72 checkGLcall("glBindFramebuffer()");
75 /* Context activation is done by the caller. */
76 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
80 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
82 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
83 checkGLcall("glFramebufferTexture2D()");
85 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
86 checkGLcall("glFramebufferTexture2D()");
88 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
89 checkGLcall("glFramebufferTexture2D()");
92 /* Context activation is done by the caller. */
93 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint fbo
)
95 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
97 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
98 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
99 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
101 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
102 checkGLcall("glDeleteFramebuffers()");
105 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
106 GLenum fbo_target
, DWORD format_flags
, GLuint rb
)
108 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
110 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
111 checkGLcall("glFramebufferRenderbuffer()");
114 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
116 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
117 checkGLcall("glFramebufferRenderbuffer()");
121 /* Context activation is done by the caller. */
122 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
123 GLenum fbo_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
125 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
127 TRACE("Attach depth stencil %p\n", depth_stencil
);
131 DWORD format_flags
= depth_stencil
->resource
.format
->flags
;
133 if (depth_stencil
->current_renderbuffer
)
135 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
136 format_flags
, depth_stencil
->current_renderbuffer
->id
);
142 case WINED3D_LOCATION_TEXTURE_RGB
:
143 case WINED3D_LOCATION_TEXTURE_SRGB
:
144 wined3d_texture_prepare_texture(depth_stencil
->container
, context
, FALSE
);
146 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
148 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
149 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
150 depth_stencil
->texture_level
);
151 checkGLcall("glFramebufferTexture2D()");
154 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
156 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
157 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
158 depth_stencil
->texture_level
);
159 checkGLcall("glFramebufferTexture2D()");
163 case WINED3D_LOCATION_RB_MULTISAMPLE
:
164 surface_prepare_rb(depth_stencil
, gl_info
, TRUE
);
165 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
166 format_flags
, depth_stencil
->rb_multisample
);
169 case WINED3D_LOCATION_RB_RESOLVED
:
170 surface_prepare_rb(depth_stencil
, gl_info
, FALSE
);
171 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
172 format_flags
, depth_stencil
->rb_resolved
);
176 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
181 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
183 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
184 checkGLcall("glFramebufferTexture2D()");
187 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
189 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
190 checkGLcall("glFramebufferTexture2D()");
195 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
196 checkGLcall("glFramebufferTexture2D()");
198 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
199 checkGLcall("glFramebufferTexture2D()");
203 /* Context activation is done by the caller. */
204 static void context_attach_surface_fbo(struct wined3d_context
*context
,
205 GLenum fbo_target
, DWORD idx
, struct wined3d_surface
*surface
, DWORD location
)
207 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
209 TRACE("Attach surface %p to %u\n", surface
, idx
);
211 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
217 case WINED3D_LOCATION_TEXTURE_RGB
:
218 case WINED3D_LOCATION_TEXTURE_SRGB
:
219 srgb
= location
== WINED3D_LOCATION_TEXTURE_SRGB
;
220 wined3d_texture_prepare_texture(surface
->container
, context
, srgb
);
221 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
222 surface
->texture_target
, surface_get_texture_name(surface
, gl_info
, srgb
),
223 surface
->texture_level
);
224 checkGLcall("glFramebufferTexture2D()");
227 case WINED3D_LOCATION_RB_MULTISAMPLE
:
228 surface_prepare_rb(surface
, gl_info
, TRUE
);
229 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
230 GL_RENDERBUFFER
, surface
->rb_multisample
);
231 checkGLcall("glFramebufferRenderbuffer()");
234 case WINED3D_LOCATION_RB_RESOLVED
:
235 surface_prepare_rb(surface
, gl_info
, FALSE
);
236 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
237 GL_RENDERBUFFER
, surface
->rb_resolved
);
238 checkGLcall("glFramebufferRenderbuffer()");
242 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
248 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
249 checkGLcall("glFramebufferTexture2D()");
253 /* Context activation is done by the caller. */
254 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
256 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
259 if (!FIXME_ON(d3d
)) return;
261 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
262 if (status
== GL_FRAMEBUFFER_COMPLETE
)
264 TRACE("FBO complete\n");
268 const struct wined3d_surface
*attachment
;
271 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
273 if (!context
->current_fbo
)
275 ERR("FBO 0 is incomplete, driver bug?\n");
279 FIXME("\tLocation %s (%#x).\n", wined3d_debug_location(context
->current_fbo
->location
),
280 context
->current_fbo
->location
);
282 /* Dump the FBO attachments */
283 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
285 attachment
= context
->current_fbo
->render_targets
[i
];
288 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
289 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
290 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
293 attachment
= context
->current_fbo
->depth_stencil
;
296 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
297 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
298 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
303 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
305 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
306 return buffer
? (1 << 31) | buffer
: 0;
309 static inline DWORD
context_generate_rt_mask_from_surface(const struct wined3d_surface
*target
)
311 return (1 << 31) | surface_get_gl_buffer(target
);
314 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
315 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
317 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
318 struct fbo_entry
*entry
;
320 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
321 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
322 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
323 entry
->depth_stencil
= depth_stencil
;
324 entry
->location
= location
;
325 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
326 entry
->attached
= FALSE
;
327 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
328 checkGLcall("glGenFramebuffers()");
329 TRACE("Created FBO %u.\n", entry
->id
);
334 /* Context activation is done by the caller. */
335 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
336 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
337 DWORD location
, struct fbo_entry
*entry
)
339 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
341 context_bind_fbo(context
, target
, entry
->id
);
342 context_clean_fbo_attachments(gl_info
, target
);
344 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
345 entry
->depth_stencil
= depth_stencil
;
346 entry
->location
= location
;
347 entry
->attached
= FALSE
;
350 /* Context activation is done by the caller. */
351 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
355 TRACE("Destroy FBO %u.\n", entry
->id
);
356 context_destroy_fbo(context
, entry
->id
);
358 --context
->fbo_entry_count
;
359 list_remove(&entry
->entry
);
360 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
361 HeapFree(GetProcessHeap(), 0, entry
);
364 /* Context activation is done by the caller. */
365 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
366 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
368 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
369 struct fbo_entry
*entry
;
371 if (depth_stencil
&& render_targets
&& render_targets
[0])
373 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
374 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
376 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
377 depth_stencil
= NULL
;
381 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
383 if (!memcmp(entry
->render_targets
,
384 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
385 && entry
->depth_stencil
== depth_stencil
&& entry
->location
== location
)
387 list_remove(&entry
->entry
);
388 list_add_head(&context
->fbo_list
, &entry
->entry
);
393 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
395 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, location
);
396 list_add_head(&context
->fbo_list
, &entry
->entry
);
397 ++context
->fbo_entry_count
;
401 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
402 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
, entry
);
403 list_remove(&entry
->entry
);
404 list_add_head(&context
->fbo_list
, &entry
->entry
);
410 /* Context activation is done by the caller. */
411 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
413 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
415 GLuint read_binding
, draw_binding
;
419 context_bind_fbo(context
, target
, entry
->id
);
423 read_binding
= context
->fbo_read_binding
;
424 draw_binding
= context
->fbo_draw_binding
;
425 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
427 /* Apply render targets */
428 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
430 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->location
);
433 /* Apply depth targets */
434 if (entry
->depth_stencil
)
435 surface_set_compatible_renderbuffer(entry
->depth_stencil
, entry
->render_targets
[0]);
436 context_attach_depth_stencil_fbo(context
, target
, entry
->depth_stencil
, entry
->location
);
438 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
439 * GL contexts requirements. */
440 glReadBuffer(GL_NONE
);
441 context_set_draw_buffer(context
, GL_NONE
);
442 if (target
!= GL_FRAMEBUFFER
)
444 if (target
== GL_READ_FRAMEBUFFER
)
445 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
447 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
450 entry
->attached
= TRUE
;
453 /* Context activation is done by the caller. */
454 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
455 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
, DWORD location
)
457 struct fbo_entry
*entry
, *entry2
;
459 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
461 context_destroy_fbo_entry(context
, entry
);
464 if (context
->rebind_fbo
)
466 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
467 context
->rebind_fbo
= FALSE
;
470 if (location
== WINED3D_LOCATION_DRAWABLE
)
472 context
->current_fbo
= NULL
;
473 context_bind_fbo(context
, target
, 0);
477 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
, location
);
478 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
482 /* Context activation is done by the caller. */
483 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
484 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
486 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
488 context
->blit_targets
[0] = render_target
;
490 memset(&context
->blit_targets
[1], 0, clear_size
);
491 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
);
494 /* Context activation is done by the caller. */
495 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
497 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
499 if (context
->free_occlusion_query_count
)
501 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
505 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
507 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
508 checkGLcall("glGenQueriesARB");
510 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
514 WARN("Occlusion queries not supported, not allocating query id.\n");
519 query
->context
= context
;
520 list_add_head(&context
->occlusion_queries
, &query
->entry
);
523 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
525 struct wined3d_context
*context
= query
->context
;
527 list_remove(&query
->entry
);
528 query
->context
= NULL
;
530 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
532 UINT new_size
= context
->free_occlusion_query_size
<< 1;
533 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
534 new_size
* sizeof(*context
->free_occlusion_queries
));
538 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
542 context
->free_occlusion_query_size
= new_size
;
543 context
->free_occlusion_queries
= new_data
;
546 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
549 /* Context activation is done by the caller. */
550 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
552 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
554 if (context
->free_event_query_count
)
556 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
560 if (gl_info
->supported
[ARB_SYNC
])
562 /* Using ARB_sync, not much to do here. */
563 query
->object
.sync
= NULL
;
564 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
566 else if (gl_info
->supported
[APPLE_FENCE
])
568 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
569 checkGLcall("glGenFencesAPPLE");
571 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
573 else if(gl_info
->supported
[NV_FENCE
])
575 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
576 checkGLcall("glGenFencesNV");
578 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
582 WARN("Event queries not supported, not allocating query id.\n");
583 query
->object
.id
= 0;
587 query
->context
= context
;
588 list_add_head(&context
->event_queries
, &query
->entry
);
591 void context_free_event_query(struct wined3d_event_query
*query
)
593 struct wined3d_context
*context
= query
->context
;
595 list_remove(&query
->entry
);
596 query
->context
= NULL
;
598 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
600 UINT new_size
= context
->free_event_query_size
<< 1;
601 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
602 new_size
* sizeof(*context
->free_event_queries
));
606 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
610 context
->free_event_query_size
= new_size
;
611 context
->free_event_queries
= new_data
;
614 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
617 /* Context activation is done by the caller. */
618 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
620 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
622 if (context
->free_timestamp_query_count
)
624 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
628 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
629 checkGLcall("glGenQueriesARB");
631 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
634 query
->context
= context
;
635 list_add_head(&context
->timestamp_queries
, &query
->entry
);
638 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
640 struct wined3d_context
*context
= query
->context
;
642 list_remove(&query
->entry
);
643 query
->context
= NULL
;
645 if (context
->free_timestamp_query_count
>= context
->free_timestamp_query_size
- 1)
647 UINT new_size
= context
->free_timestamp_query_size
<< 1;
648 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_timestamp_queries
,
649 new_size
* sizeof(*context
->free_timestamp_queries
));
653 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
657 context
->free_timestamp_query_size
= new_size
;
658 context
->free_timestamp_queries
= new_data
;
661 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
664 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
666 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
667 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
671 for (i
= 0; i
< device
->context_count
; ++i
)
673 struct wined3d_context
*context
= device
->contexts
[i
];
674 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
675 struct fbo_entry
*entry
, *entry2
;
677 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
679 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
683 if (entry
->depth_stencil
== surface
)
685 callback(context
, entry
);
689 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
691 if (entry
->render_targets
[j
] == surface
)
693 callback(context
, entry
);
701 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
703 list_remove(&entry
->entry
);
704 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
707 void context_resource_released(const struct wined3d_device
*device
,
708 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
710 if (!device
->d3d_initialized
) return;
714 case WINED3D_RTYPE_SURFACE
:
715 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
716 context_queue_fbo_entry_destruction
);
724 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
726 entry
->attached
= FALSE
;
729 void context_resource_unloaded(const struct wined3d_device
*device
,
730 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
734 case WINED3D_RTYPE_SURFACE
:
735 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
736 context_detach_fbo_entry
);
744 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
746 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
747 struct fbo_entry
*entry
= context
->current_fbo
;
750 if (!entry
|| context
->rebind_fbo
) return;
752 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
754 if (surface
== entry
->render_targets
[i
])
756 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
757 context
->rebind_fbo
= TRUE
;
762 if (surface
== entry
->depth_stencil
)
764 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
765 context
->rebind_fbo
= TRUE
;
769 static BOOL
context_restore_pixel_format(struct wined3d_context
*ctx
)
771 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
774 if (ctx
->restore_pf
&& IsWindow(ctx
->restore_pf_win
))
776 if (ctx
->gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
778 HDC dc
= GetDC(ctx
->restore_pf_win
);
781 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, ctx
->restore_pf
))))
783 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
784 ctx
->restore_pf
, ctx
->restore_pf_win
);
786 ReleaseDC(ctx
->restore_pf_win
, dc
);
791 ERR("can't restore pixel format %d on window %p\n", ctx
->restore_pf
, ctx
->restore_pf_win
);
796 ctx
->restore_pf_win
= NULL
;
800 static BOOL
context_set_pixel_format(struct wined3d_context
*context
, HDC dc
, BOOL
private, int format
)
802 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
805 if (dc
== context
->hdc
&& context
->hdc_is_private
&& context
->hdc_has_format
)
808 current
= GetPixelFormat(dc
);
809 if (current
== format
) goto success
;
813 if (!SetPixelFormat(dc
, format
, NULL
))
815 /* This may also happen if the dc belongs to a destroyed window. */
816 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
817 format
, dc
, GetLastError());
821 context
->restore_pf
= 0;
822 context
->restore_pf_win
= private ? NULL
: WindowFromDC(dc
);
826 /* By default WGL doesn't allow pixel format adjustments but we need it
827 * here. For this reason there's a Wine specific wglSetPixelFormat()
828 * which allows us to set the pixel format multiple times. Only use it
829 * when really needed. */
830 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
834 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
836 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
841 win
= private ? NULL
: WindowFromDC(dc
);
842 if (win
!= context
->restore_pf_win
)
844 context_restore_pixel_format(context
);
846 context
->restore_pf
= private ? 0 : current
;
847 context
->restore_pf_win
= win
;
853 /* OpenGL doesn't allow pixel format adjustments. Print an error and
854 * continue using the old format. There's a big chance that the old
855 * format works although with a performance hit and perhaps rendering
857 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
858 format
, dc
, current
);
862 if (dc
== context
->hdc
&& context
->hdc_is_private
)
863 context
->hdc_has_format
= TRUE
;
867 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
869 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
872 if (!context_set_pixel_format(ctx
, ctx
->hdc
, ctx
->hdc_is_private
, ctx
->pixel_format
))
874 WARN("Failed to set pixel format %d on device context %p.\n",
875 ctx
->pixel_format
, ctx
->hdc
);
879 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
883 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
884 ctx
->glCtx
, ctx
->hdc
, GetLastError());
886 WARN("Trying fallback to the backup window.\n");
888 /* FIXME: If the context is destroyed it's no longer associated with
889 * a swapchain, so we can't use the swapchain to get a backup dc. To
890 * make this work windowless contexts would need to be handled by the
894 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
895 context_set_current(NULL
);
899 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
901 context_set_current(NULL
);
905 if (!context_set_pixel_format(ctx
, dc
, TRUE
, ctx
->pixel_format
))
907 ERR("Failed to set pixel format %d on device context %p.\n",
908 ctx
->pixel_format
, dc
);
909 context_set_current(NULL
);
913 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
915 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
917 context_set_current(NULL
);
927 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
929 if (!wglMakeCurrent(dc
, gl_ctx
))
931 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
932 gl_ctx
, dc
, GetLastError());
933 context_set_current(NULL
);
937 static void context_update_window(struct wined3d_context
*context
)
939 if (context
->win_handle
== context
->swapchain
->win_handle
)
942 TRACE("Updating context %p window from %p to %p.\n",
943 context
, context
->win_handle
, context
->swapchain
->win_handle
);
946 wined3d_release_dc(context
->win_handle
, context
->hdc
);
948 context
->win_handle
= context
->swapchain
->win_handle
;
949 context
->hdc_is_private
= FALSE
;
950 context
->hdc_has_format
= FALSE
;
951 context
->needs_set
= 1;
954 if (!(context
->hdc
= GetDC(context
->win_handle
)))
956 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
961 static void context_destroy_gl_resources(struct wined3d_context
*context
)
963 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
964 struct wined3d_timestamp_query
*timestamp_query
;
965 struct wined3d_occlusion_query
*occlusion_query
;
966 struct wined3d_event_query
*event_query
;
967 struct fbo_entry
*entry
, *entry2
;
972 restore_ctx
= wglGetCurrentContext();
973 restore_dc
= wglGetCurrentDC();
975 if (restore_ctx
== context
->glCtx
)
977 else if (context
->valid
)
978 context_set_gl_context(context
);
980 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
983 GL_EXTCALL(glDeleteQueriesARB(1, ×tamp_query
->id
));
984 timestamp_query
->context
= NULL
;
987 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
989 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
990 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
991 occlusion_query
->context
= NULL
;
994 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
998 if (gl_info
->supported
[ARB_SYNC
])
1000 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
1002 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
1003 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
1005 event_query
->context
= NULL
;
1008 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
1010 if (!context
->valid
) entry
->id
= 0;
1011 context_destroy_fbo_entry(context
, entry
);
1014 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1016 if (!context
->valid
) entry
->id
= 0;
1017 context_destroy_fbo_entry(context
, entry
);
1022 if (context
->dummy_arbfp_prog
)
1024 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
1027 if (gl_info
->supported
[ARB_TIMER_QUERY
])
1028 GL_EXTCALL(glDeleteQueriesARB(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
1030 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1031 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
1033 if (gl_info
->supported
[ARB_SYNC
])
1035 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1037 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
1040 else if (gl_info
->supported
[APPLE_FENCE
])
1042 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1044 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1047 else if (gl_info
->supported
[NV_FENCE
])
1049 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1051 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1055 checkGLcall("context cleanup");
1058 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1059 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1060 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1062 context_restore_pixel_format(context
);
1065 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1067 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1069 ERR("Failed to disable GL context.\n");
1072 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1074 if (!wglDeleteContext(context
->glCtx
))
1076 DWORD err
= GetLastError();
1077 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1081 DWORD
context_get_tls_idx(void)
1083 return wined3d_context_tls_idx
;
1086 void context_set_tls_idx(DWORD idx
)
1088 wined3d_context_tls_idx
= idx
;
1091 struct wined3d_context
*context_get_current(void)
1093 return TlsGetValue(wined3d_context_tls_idx
);
1096 BOOL
context_set_current(struct wined3d_context
*ctx
)
1098 struct wined3d_context
*old
= context_get_current();
1102 TRACE("Already using D3D context %p.\n", ctx
);
1110 TRACE("Switching away from destroyed context %p.\n", old
);
1111 context_destroy_gl_resources(old
);
1112 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1113 HeapFree(GetProcessHeap(), 0, old
);
1125 ERR("Trying to make invalid context %p current\n", ctx
);
1129 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1130 if (!context_set_gl_context(ctx
))
1134 else if(wglGetCurrentContext())
1136 TRACE("Clearing current D3D context.\n");
1137 if (!wglMakeCurrent(NULL
, NULL
))
1139 DWORD err
= GetLastError();
1140 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1141 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1146 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1149 void context_release(struct wined3d_context
*context
)
1151 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1155 if (!context
->level
)
1156 WARN("Context %p is not active.\n", context
);
1157 else if (context
!= context_get_current())
1158 WARN("Context %p is not the current context.\n", context
);
1161 if (!--context
->level
)
1163 if (context_restore_pixel_format(context
))
1164 context
->needs_set
= 1;
1165 if (context
->restore_ctx
)
1167 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1168 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
);
1169 context
->restore_ctx
= NULL
;
1170 context
->restore_dc
= NULL
;
1175 static void context_enter(struct wined3d_context
*context
)
1177 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1179 if (!context
->level
++)
1181 const struct wined3d_context
*current_context
= context_get_current();
1182 HGLRC current_gl
= wglGetCurrentContext();
1184 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1186 TRACE("Another GL context (%p on device context %p) is already current.\n",
1187 current_gl
, wglGetCurrentDC());
1188 context
->restore_ctx
= current_gl
;
1189 context
->restore_dc
= wglGetCurrentDC();
1190 context
->needs_set
= 1;
1192 else if (!context
->needs_set
&& !(context
->hdc_is_private
&& context
->hdc_has_format
)
1193 && context
->pixel_format
!= GetPixelFormat(context
->hdc
))
1194 context
->needs_set
= 1;
1198 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1200 DWORD rep
= context
->state_table
[state
].representative
;
1204 if (isStateDirty(context
, rep
)) return;
1206 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1207 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1208 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1209 context
->isStateDirty
[idx
] |= (1 << shift
);
1212 /* This function takes care of wined3d pixel format selection. */
1213 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1214 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1215 BOOL auxBuffers
, BOOL findCompatible
)
1218 BYTE redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1219 BYTE depthBits
=0, stencilBits
=0;
1220 unsigned int current_value
;
1221 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1224 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1225 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1226 auxBuffers
, findCompatible
);
1228 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1230 ERR("Unable to get color bits for format %s (%#x)!\n",
1231 debug_d3dformat(color_format
->id
), color_format
->id
);
1235 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1238 for (i
= 0; i
< cfg_count
; ++i
)
1240 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1243 /* For now only accept RGBA formats. Perhaps some day we will
1244 * allow floating point formats for pbuffers. */
1245 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1247 /* In window mode we need a window drawable format and double buffering. */
1248 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1250 if (cfg
->redSize
< redBits
)
1252 if (cfg
->greenSize
< greenBits
)
1254 if (cfg
->blueSize
< blueBits
)
1256 if (cfg
->alphaSize
< alphaBits
)
1258 if (cfg
->depthSize
< depthBits
)
1260 if (stencilBits
&& cfg
->stencilSize
!= stencilBits
)
1262 /* Check multisampling support. */
1263 if (cfg
->numSamples
)
1267 /* We try to locate a format which matches our requirements exactly. In case of
1268 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1269 if (cfg
->depthSize
== depthBits
)
1271 if (cfg
->stencilSize
== stencilBits
)
1273 if (cfg
->alphaSize
== alphaBits
)
1275 /* We like to have aux buffers in backbuffer mode */
1276 if (auxBuffers
&& cfg
->auxBuffers
)
1278 if (cfg
->redSize
== redBits
1279 && cfg
->greenSize
== greenBits
1280 && cfg
->blueSize
== blueBits
)
1283 if (value
> current_value
)
1285 iPixelFormat
= cfg
->iPixelFormat
;
1286 current_value
= value
;
1290 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1291 if(!iPixelFormat
&& !findCompatible
) {
1292 ERR("Can't find a suitable iPixelFormat\n");
1294 } else if(!iPixelFormat
) {
1295 PIXELFORMATDESCRIPTOR pfd
;
1297 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1298 /* PixelFormat selection */
1299 ZeroMemory(&pfd
, sizeof(pfd
));
1300 pfd
.nSize
= sizeof(pfd
);
1302 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1303 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1304 pfd
.cAlphaBits
= alphaBits
;
1305 pfd
.cColorBits
= colorBits
;
1306 pfd
.cDepthBits
= depthBits
;
1307 pfd
.cStencilBits
= stencilBits
;
1308 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1310 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1312 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1313 ERR("Can't find a suitable iPixelFormat\n");
1318 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1319 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1320 return iPixelFormat
;
1323 /* Context activation is done by the caller. */
1324 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1326 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1327 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1329 for (i
= 0; i
< count
; ++i
)
1331 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1332 checkGLcall("glActiveTexture");
1334 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1335 checkGLcall("glBindTexture");
1337 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1339 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1340 checkGLcall("glBindTexture");
1343 if (gl_info
->supported
[EXT_TEXTURE3D
])
1345 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1346 checkGLcall("glBindTexture");
1349 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1351 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1352 checkGLcall("glBindTexture");
1357 BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1359 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1360 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1363 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1364 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1368 case GL_DEBUG_TYPE_ERROR_ARB
:
1369 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1372 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1373 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1374 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1375 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1378 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1379 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1383 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1388 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1389 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1391 struct wined3d_device
*device
= swapchain
->device
;
1392 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1393 const struct wined3d_format
*color_format
;
1394 struct wined3d_context
*ret
;
1395 BOOL auxBuffers
= FALSE
;
1396 HGLRC ctx
, share_ctx
;
1402 BOOL hdc_is_private
= FALSE
;
1404 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1406 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1410 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1411 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1412 if (!ret
->blit_targets
)
1415 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1416 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1417 if (!ret
->draw_buffers
)
1420 ret
->free_timestamp_query_size
= 4;
1421 ret
->free_timestamp_queries
= HeapAlloc(GetProcessHeap(), 0,
1422 ret
->free_timestamp_query_size
* sizeof(*ret
->free_timestamp_queries
));
1423 if (!ret
->free_timestamp_queries
)
1425 list_init(&ret
->timestamp_queries
);
1427 ret
->free_occlusion_query_size
= 4;
1428 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1429 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1430 if (!ret
->free_occlusion_queries
)
1433 list_init(&ret
->occlusion_queries
);
1435 ret
->free_event_query_size
= 4;
1436 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1437 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1438 if (!ret
->free_event_queries
)
1441 list_init(&ret
->event_queries
);
1442 list_init(&ret
->fbo_list
);
1443 list_init(&ret
->fbo_destroy_list
);
1445 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1447 ERR("Failed to allocate shader backend context data.\n");
1451 /* Initialize the texture unit mapping to a 1:1 mapping */
1452 for (s
= 0; s
< MAX_COMBINED_SAMPLERS
; ++s
)
1454 if (s
< gl_info
->limits
.fragment_samplers
)
1456 ret
->tex_unit_map
[s
] = s
;
1457 ret
->rev_tex_unit_map
[s
] = s
;
1461 ret
->tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1462 ret
->rev_tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1466 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1468 WARN("Failed to retireve device context, trying swapchain backup.\n");
1470 if ((hdc
= swapchain_get_backup_dc(swapchain
)))
1471 hdc_is_private
= TRUE
;
1474 ERR("Failed to retrieve a device context.\n");
1479 color_format
= target
->resource
.format
;
1481 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1482 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1483 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1487 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1488 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1489 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1490 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1493 /* DirectDraw supports 8bit paletted render targets and these are used by
1494 * old games like StarCraft and C&C. Most modern hardware doesn't support
1495 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1496 * conversion (ab)uses the alpha component for storing the palette index.
1497 * For this reason we require a format with 8bit alpha, so request
1499 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1500 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1502 /* Try to find a pixel format which matches our requirements. */
1503 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1505 /* Try to locate a compatible format if we weren't able to find anything. */
1508 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1509 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1512 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1515 ERR("Can't find a suitable pixel format.\n");
1521 ret
->gl_info
= gl_info
;
1523 if (!context_set_pixel_format(ret
, hdc
, hdc_is_private
, pixel_format
))
1525 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1526 context_release(ret
);
1530 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1531 if (gl_info
->p_wglCreateContextAttribsARB
)
1533 unsigned int ctx_attrib_idx
= 0;
1534 GLint ctx_attribs
[3];
1536 if (context_debug_output_enabled(gl_info
))
1538 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1539 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_DEBUG_BIT_ARB
;
1541 ctx_attribs
[ctx_attrib_idx
] = 0;
1543 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1545 ERR("Failed to create a WGL context.\n");
1546 context_release(ret
);
1552 if (!(ctx
= wglCreateContext(hdc
)))
1554 ERR("Failed to create a WGL context.\n");
1555 context_release(ret
);
1559 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1561 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1562 context_release(ret
);
1563 if (!wglDeleteContext(ctx
))
1564 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1569 if (!device_context_add(device
, ret
))
1571 ERR("Failed to add the newly created context to the context list\n");
1572 context_release(ret
);
1573 if (!wglDeleteContext(ctx
))
1574 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1578 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1579 ret
->state_table
= device
->StateTable
;
1581 /* Mark all states dirty to force a proper initialization of the states
1582 * on the first use of the context. */
1583 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1585 if (ret
->state_table
[state
].representative
)
1586 context_invalidate_state(ret
, state
);
1589 ret
->swapchain
= swapchain
;
1590 ret
->current_rt
= target
;
1591 ret
->tid
= GetCurrentThreadId();
1593 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
1594 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1598 ret
->win_handle
= swapchain
->win_handle
;
1600 ret
->hdc_is_private
= hdc_is_private
;
1601 ret
->hdc_has_format
= TRUE
;
1602 ret
->pixel_format
= pixel_format
;
1605 /* Set up the context defaults */
1606 if (!context_set_current(ret
))
1608 ERR("Cannot activate context to set up defaults.\n");
1609 device_context_remove(device
, ret
);
1610 context_release(ret
);
1611 if (!wglDeleteContext(ctx
))
1612 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1616 if (context_debug_output_enabled(gl_info
))
1618 GL_EXTCALL(glDebugMessageCallbackARB(wined3d_debug_callback
, ret
));
1619 if (TRACE_ON(d3d_synchronous
))
1620 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
);
1621 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1624 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR_ARB
,
1625 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1629 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
,
1630 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1631 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
,
1632 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1633 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY_ARB
,
1634 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1636 if (WARN_ON(d3d_perf
))
1638 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE_ARB
,
1639 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1643 switch (swapchain
->desc
.swap_interval
)
1645 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1648 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1649 case WINED3DPRESENT_INTERVAL_ONE
:
1652 case WINED3DPRESENT_INTERVAL_TWO
:
1655 case WINED3DPRESENT_INTERVAL_THREE
:
1658 case WINED3DPRESENT_INTERVAL_FOUR
:
1662 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1666 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1668 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1669 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1670 swap_interval
, ret
, GetLastError());
1673 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1675 TRACE("Setting up the screen\n");
1677 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1678 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1680 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1681 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1683 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1684 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1686 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1687 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1688 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1689 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1691 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1693 /* Direct3D always uses n-1 weights for n world matrices and uses
1694 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1695 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1696 * enabled as well. */
1697 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1698 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1700 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1702 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1703 * the previous texture where to source the offset from is always unit - 1.
1705 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1707 context_active_texture(ret
, gl_info
, s
);
1708 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1709 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1710 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1713 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1715 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1716 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1717 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1718 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1721 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1722 * program and the dummy program is destroyed when the context is destroyed.
1724 static const char dummy_program
[] =
1726 "MOV result.color, fragment.color.primary;\n"
1728 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1729 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1730 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1733 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1735 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1737 context_active_texture(ret
, gl_info
, s
);
1738 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1739 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1743 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1745 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1747 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1749 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1751 ret
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
1752 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
1753 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
1755 /* If this happens to be the first context for the device, dummy textures
1756 * are not created yet. In that case, they will be created (and bound) by
1757 * create_dummy_textures right after this context is initialized. */
1758 if (device
->dummy_texture_2d
[0])
1759 bind_dummy_textures(device
, ret
);
1761 TRACE("Created context %p.\n", ret
);
1766 device
->shader_backend
->shader_free_context_data(ret
);
1767 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1768 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1769 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
1770 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1771 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1772 HeapFree(GetProcessHeap(), 0, ret
);
1776 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1780 TRACE("Destroying ctx %p\n", context
);
1782 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1784 context_destroy_gl_resources(context
);
1785 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1790 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1791 in wined3d_adapter may go away in the meantime */
1792 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
1793 *gl_info
= *context
->gl_info
;
1794 context
->gl_info
= gl_info
;
1795 context
->destroyed
= 1;
1799 device
->shader_backend
->shader_free_context_data(context
);
1800 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1801 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1802 device_context_remove(device
, context
);
1803 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1806 /* Context activation is done by the caller. */
1807 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
1809 const GLdouble projection
[] =
1811 2.0 / width
, 0.0, 0.0, 0.0,
1812 0.0, 2.0 / height
, 0.0, 0.0,
1814 -1.0, -1.0, -1.0, 1.0,
1817 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
1818 checkGLcall("glMatrixMode(GL_PROJECTION)");
1819 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
1820 checkGLcall("glLoadMatrixd");
1821 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
1822 checkGLcall("glViewport");
1825 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
1827 const struct wined3d_surface
*rt
= context
->current_rt
;
1829 if (rt
->container
->swapchain
&& rt
->container
->swapchain
->front_buffer
== rt
->container
)
1833 GetClientRect(context
->win_handle
, &window_size
);
1834 size
->cx
= window_size
.right
- window_size
.left
;
1835 size
->cy
= window_size
.bottom
- window_size
.top
;
1840 size
->cx
= rt
->resource
.width
;
1841 size
->cy
= rt
->resource
.height
;
1844 /*****************************************************************************
1847 * Sets up a context for DirectDraw blitting.
1848 * All texture units are disabled, texture unit 0 is set as current unit
1849 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1850 * color writing enabled for all channels
1851 * register combiners disabled, shaders disabled
1852 * world matrix is set to identity, texture matrix 0 too
1853 * projection matrix is setup for drawing screen coordinates
1856 * This: Device to activate the context for
1857 * context: Context to setup
1859 *****************************************************************************/
1860 /* Context activation is done by the caller. */
1861 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
1864 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1868 TRACE("Setting up context %p for blitting\n", context
);
1870 context_get_rt_size(context
, &rt_size
);
1872 if (context
->last_was_blit
)
1874 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
1876 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1877 context
->blit_w
= rt_size
.cx
;
1878 context
->blit_h
= rt_size
.cy
;
1879 /* No need to dirtify here, the states are still dirtified because
1880 * they weren't applied since the last SetupForBlit() call. */
1882 TRACE("Context is already set up for blitting, nothing to do\n");
1885 context
->last_was_blit
= TRUE
;
1887 /* Disable all textures. The caller can then bind a texture it wants to blit
1890 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1891 * function texture unit. No need to care for higher samplers
1893 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1895 sampler
= context
->rev_tex_unit_map
[i
];
1896 context_active_texture(context
, gl_info
, i
);
1898 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1900 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1901 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1903 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1904 checkGLcall("glDisable GL_TEXTURE_3D");
1905 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1907 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1908 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1910 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1911 checkGLcall("glDisable GL_TEXTURE_2D");
1913 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1914 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1916 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1918 if (sampler
< MAX_TEXTURES
)
1919 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1920 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1923 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
1924 GL_EXTCALL(glBindSampler(0, 0));
1925 context_active_texture(context
, gl_info
, 0);
1927 sampler
= context
->rev_tex_unit_map
[0];
1929 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1931 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1932 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1934 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1935 checkGLcall("glDisable GL_TEXTURE_3D");
1936 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1938 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1939 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1941 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1942 checkGLcall("glDisable GL_TEXTURE_2D");
1944 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1946 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
1947 checkGLcall("glMatrixMode(GL_TEXTURE)");
1948 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1949 checkGLcall("glLoadIdentity()");
1951 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1953 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1954 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
1955 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1958 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1960 if (sampler
< MAX_TEXTURES
)
1962 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
1963 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1965 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1968 /* Other misc states */
1969 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
1970 checkGLcall("glDisable(GL_ALPHA_TEST)");
1971 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
1972 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
1973 checkGLcall("glDisable GL_LIGHTING");
1974 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
1975 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
1976 checkGLcall("glDisable GL_DEPTH_TEST");
1977 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
1978 glDisableWINE(GL_FOG
);
1979 checkGLcall("glDisable GL_FOG");
1980 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
1981 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
1982 checkGLcall("glDisable GL_BLEND");
1983 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
1984 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
1985 checkGLcall("glDisable GL_CULL_FACE");
1986 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
1987 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
1988 checkGLcall("glDisable GL_STENCIL_TEST");
1989 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
1990 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
1991 checkGLcall("glDisable GL_SCISSOR_TEST");
1992 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
1993 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1995 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
1996 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1997 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
1999 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
2000 checkGLcall("glColorMask");
2001 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
2002 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
2003 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
2004 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2005 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2007 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2008 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2009 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2012 /* Setup transforms */
2013 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2014 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2015 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2016 checkGLcall("glLoadIdentity()");
2017 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2019 context
->last_was_rhw
= TRUE
;
2020 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2022 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2023 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2024 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2025 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2026 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2027 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2028 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2030 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2032 /* Disable shaders */
2033 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2035 context
->blit_w
= rt_size
.cx
;
2036 context
->blit_h
= rt_size
.cy
;
2037 context_invalidate_state(context
, STATE_VIEWPORT
);
2038 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2041 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2043 return rt_mask
& (1 << 31);
2046 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2048 return rt_mask
& ~(1 << 31);
2051 /* Context activation is done by the caller. */
2052 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2054 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2058 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2059 checkGLcall("glDrawBuffer()");
2061 else if (is_rt_mask_onscreen(rt_mask
))
2063 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2064 checkGLcall("glDrawBuffer()");
2068 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2075 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2077 context
->draw_buffers
[i
] = GL_NONE
;
2083 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2085 GL_EXTCALL(glDrawBuffers(i
, context
->draw_buffers
));
2086 checkGLcall("glDrawBuffers()");
2090 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2091 checkGLcall("glDrawBuffer()");
2096 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2101 /* Context activation is done by the caller. */
2102 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2104 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2105 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2106 DWORD new_mask
= context_generate_rt_mask(buffer
);
2108 if (new_mask
== *current_mask
)
2111 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2112 checkGLcall("glDrawBuffer()");
2114 *current_mask
= new_mask
;
2117 /* Context activation is done by the caller. */
2118 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2120 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2121 checkGLcall("glActiveTexture");
2122 context
->active_texture
= unit
;
2125 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2127 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2128 DWORD unit
= context
->active_texture
;
2129 DWORD old_texture_type
= context
->texture_type
[unit
];
2133 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2134 checkGLcall("glBindTexture");
2141 if (old_texture_type
!= target
)
2143 const struct wined3d_device
*device
= context
->swapchain
->device
;
2145 switch (old_texture_type
)
2151 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2152 checkGLcall("glBindTexture");
2154 case GL_TEXTURE_RECTANGLE_ARB
:
2155 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2156 checkGLcall("glBindTexture");
2158 case GL_TEXTURE_CUBE_MAP
:
2159 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2160 checkGLcall("glBindTexture");
2163 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2164 checkGLcall("glBindTexture");
2167 ERR("Unexpected texture target %#x\n", old_texture_type
);
2170 context
->texture_type
[unit
] = target
;
2174 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2176 if (context
->render_offscreen
== offscreen
) return;
2178 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2179 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2180 context_invalidate_state(context
, STATE_VIEWPORT
);
2181 context_invalidate_state(context
, STATE_SCISSORRECT
);
2182 context_invalidate_state(context
, STATE_FRONTFACE
);
2183 context
->render_offscreen
= offscreen
;
2186 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2187 const struct wined3d_format
*required
)
2189 BYTE existing_depth
, existing_stencil
, required_depth
, required_stencil
;
2191 if (existing
== required
) return TRUE
;
2192 if ((existing
->flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
2194 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
2195 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
2197 if(existing_depth
< required_depth
) return FALSE
;
2198 /* If stencil bits are used the exact amount is required - otherwise wrapping
2199 * won't work correctly */
2200 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
2204 /* The caller provides a context */
2205 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2206 const struct wined3d_rendertarget_view
*depth_stencil
)
2208 /* Onscreen surfaces are always in a swapchain */
2209 struct wined3d_swapchain
*swapchain
= context
->current_rt
->container
->swapchain
;
2211 if (context
->render_offscreen
|| !depth_stencil
) return;
2212 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2214 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2215 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2217 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2219 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2220 surface_load_location(context
->current_rt
, WINED3D_LOCATION_TEXTURE_RGB
);
2221 swapchain
->render_to_fbo
= TRUE
;
2222 swapchain_update_draw_bindings(swapchain
);
2223 context_set_render_offscreen(context
, TRUE
);
2226 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
2228 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2230 else if (rt
->container
->swapchain
)
2231 return context_generate_rt_mask_from_surface(rt
);
2233 return context_generate_rt_mask(device
->offscreenBuffer
);
2236 /* Context activation is done by the caller. */
2237 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2239 struct wined3d_surface
*rt
= context
->current_rt
;
2240 DWORD rt_mask
, *cur_mask
;
2242 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2244 context_validate_onscreen_formats(context
, NULL
);
2246 if (context
->render_offscreen
)
2248 wined3d_texture_load(rt
->container
, context
, FALSE
);
2250 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->container
->resource
.draw_binding
);
2251 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2258 context
->current_fbo
= NULL
;
2259 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2260 rt_mask
= context_generate_rt_mask_from_surface(rt
);
2265 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt
);
2268 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2270 if (rt_mask
!= *cur_mask
)
2272 context_apply_draw_buffers(context
, rt_mask
);
2273 *cur_mask
= rt_mask
;
2276 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2278 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2281 SetupForBlit(device
, context
);
2282 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2285 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2286 const struct wined3d_rendertarget_view
*ds
)
2290 if (ds
) return TRUE
;
2292 for (i
= 0; i
< rt_count
; ++i
)
2294 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2298 WARN("Invalid render target config, need at least one attachment.\n");
2302 /* Context activation is done by the caller. */
2303 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2304 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2306 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2307 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2308 DWORD rt_mask
= 0, *cur_mask
;
2311 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2312 || rt_count
!= context
->gl_info
->limits
.buffers
)
2314 if (!context_validate_rt_config(rt_count
, rts
, fb
->depth_stencil
))
2317 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2319 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2321 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2323 for (i
= 0; i
< rt_count
; ++i
)
2325 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(rts
[i
]);
2326 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2327 rt_mask
|= (1 << i
);
2329 while (i
< context
->gl_info
->limits
.buffers
)
2331 context
->blit_targets
[i
] = NULL
;
2334 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2335 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2336 rt_count
? rts
[0]->resource
->draw_binding
: WINED3D_LOCATION_TEXTURE_RGB
);
2340 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, WINED3D_LOCATION_DRAWABLE
);
2341 rt_mask
= context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2344 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2345 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2346 * state management allows this */
2347 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2351 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2352 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2355 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2356 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2358 for (i
= 0; i
< rt_count
; ++i
)
2360 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2361 rt_mask
|= (1 << i
);
2366 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2367 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2370 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2372 if (rt_mask
!= *cur_mask
)
2374 context_apply_draw_buffers(context
, rt_mask
);
2375 *cur_mask
= rt_mask
;
2376 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2379 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2381 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2384 if (context
->last_was_blit
)
2385 context
->last_was_blit
= FALSE
;
2387 /* Blending and clearing should be orthogonal, but tests on the nvidia
2388 * driver show that disabling blending when clearing improves the clearing
2389 * performance incredibly. */
2390 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2391 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2392 checkGLcall("glEnable GL_SCISSOR_TEST");
2394 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2395 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2396 context_invalidate_state(context
, STATE_SCISSORRECT
);
2401 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_device
*device
)
2403 const struct wined3d_state
*state
= &device
->state
;
2404 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
2405 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2406 DWORD rt_mask
, rt_mask_bits
;
2409 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2410 return context_generate_rt_mask_no_fbo(device
, wined3d_rendertarget_view_get_surface(rts
[0]));
2411 else if (!context
->render_offscreen
)
2412 return context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2414 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2415 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2416 rt_mask_bits
= rt_mask
;
2418 while (rt_mask_bits
)
2420 rt_mask_bits
&= ~(1 << i
);
2421 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
2422 rt_mask
&= ~(1 << i
);
2430 /* Context activation is done by the caller. */
2431 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2433 const struct wined3d_device
*device
= context
->swapchain
->device
;
2434 const struct wined3d_fb_state
*fb
= state
->fb
;
2435 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2438 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2440 if (!context
->render_offscreen
)
2442 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, WINED3D_LOCATION_DRAWABLE
);
2448 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
2450 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(fb
->render_targets
[i
]);
2452 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2453 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2454 fb
->render_targets
[0]->resource
->draw_binding
);
2458 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2459 if (rt_mask
!= *cur_mask
)
2461 context_apply_draw_buffers(context
, rt_mask
);
2462 *cur_mask
= rt_mask
;
2466 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
2468 DWORD i
= context
->rev_tex_unit_map
[unit
];
2469 DWORD j
= context
->tex_unit_map
[stage
];
2471 context
->tex_unit_map
[stage
] = unit
;
2472 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2473 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2475 context
->rev_tex_unit_map
[unit
] = stage
;
2476 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2477 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2480 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
2484 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2485 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
2488 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
2489 const struct wined3d_state
*state
)
2493 context
->fixed_function_usage_map
= 0;
2494 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2496 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2497 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2498 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2499 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2500 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2501 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2502 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2503 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2505 /* Not used, and disable higher stages. */
2506 if (color_op
== WINED3D_TOP_DISABLE
)
2509 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2510 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2511 || ((color_arg3
== WINED3DTA_TEXTURE
)
2512 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2513 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2514 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2515 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2516 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2517 context
->fixed_function_usage_map
|= (1 << i
);
2519 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2520 && i
< MAX_TEXTURES
- 1)
2521 context
->fixed_function_usage_map
|= (1 << (i
+ 1));
2524 if (i
< context
->lowest_disabled_stage
)
2527 end
= context
->lowest_disabled_stage
;
2531 start
= context
->lowest_disabled_stage
;
2535 context
->lowest_disabled_stage
= i
;
2536 for (i
= start
+ 1; i
< end
; ++i
)
2538 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
2542 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
2543 const struct wined3d_state
*state
)
2545 unsigned int i
, tex
;
2547 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2549 context_update_fixed_function_usage_map(context
, state
);
2550 ffu_map
= context
->fixed_function_usage_map
;
2552 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2553 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2555 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2560 if (context
->tex_unit_map
[i
] != i
)
2562 context_map_stage(context
, i
, i
);
2563 context_invalidate_state(context
, STATE_SAMPLER(i
));
2564 context_invalidate_texture_stage(context
, i
);
2570 /* Now work out the mapping */
2572 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2577 if (context
->tex_unit_map
[i
] != tex
)
2579 context_map_stage(context
, i
, tex
);
2580 context_invalidate_state(context
, STATE_SAMPLER(i
));
2581 context_invalidate_texture_stage(context
, i
);
2588 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2590 const struct wined3d_shader_resource_info
*resource_info
=
2591 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2593 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2595 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2597 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
2599 context_map_stage(context
, i
, i
);
2600 context_invalidate_state(context
, STATE_SAMPLER(i
));
2601 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2602 context_invalidate_texture_stage(context
, i
);
2607 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
2608 const struct wined3d_shader_resource_info
*ps_resource_info
,
2609 const struct wined3d_shader_resource_info
*vs_resource_info
, DWORD unit
)
2611 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
2613 /* Not currently used */
2614 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
2617 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
2619 /* Used by a fragment sampler */
2621 if (!ps_resource_info
)
2623 /* No pixel shader, check fixed function */
2624 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1 << current_mapping
));
2627 /* Pixel shader, check the shader's sampler map */
2628 return !ps_resource_info
[current_mapping
].type
;
2631 /* Used by a vertex sampler */
2632 return !vs_resource_info
[current_mapping
- MAX_FRAGMENT_SAMPLERS
].type
;
2635 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
2637 const struct wined3d_shader_resource_info
*vs_resource_info
=
2638 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
2639 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
2640 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2641 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2644 /* Note that we only care if a resource is used or not, not the
2645 * resource's specific type. Otherwise we'd need to call
2646 * shader_update_samplers() here for 1.x pixelshaders. */
2648 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2650 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
2652 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2653 if (vs_resource_info
[i
].type
)
2655 if (context
->tex_unit_map
[vsampler_idx
] != WINED3D_UNMAPPED_STAGE
)
2657 /* Already mapped somewhere */
2663 if (context_unit_free_for_vs(context
, ps_resource_info
, vs_resource_info
, start
))
2665 context_map_stage(context
, vsampler_idx
, start
);
2666 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
2678 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2680 BOOL vs
= use_vs(state
);
2681 BOOL ps
= use_ps(state
);
2684 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2685 * that would be really messy and require shader recompilation
2686 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2687 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2690 context_map_psamplers(context
, state
);
2692 context_map_fixed_function_samplers(context
, state
);
2695 context_map_vsamplers(context
, ps
, state
);
2698 /* Context activation is done by the caller. */
2699 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2701 const struct wined3d_device
*device
= context
->swapchain
->device
;
2702 DWORD rt_mask
, *cur_mask
;
2704 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2706 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2707 rt_mask
= find_draw_buffers_mask(context
, device
);
2708 if (rt_mask
!= *cur_mask
)
2710 context_apply_draw_buffers(context
, rt_mask
);
2711 *cur_mask
= rt_mask
;
2715 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
2717 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
2718 *regnum
= WINED3D_FFP_POSITION
;
2719 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
2720 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
2721 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
2722 *regnum
= WINED3D_FFP_BLENDINDICES
;
2723 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
2724 *regnum
= WINED3D_FFP_NORMAL
;
2725 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
2726 *regnum
= WINED3D_FFP_PSIZE
;
2727 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
2728 *regnum
= WINED3D_FFP_DIFFUSE
;
2729 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
2730 *regnum
= WINED3D_FFP_SPECULAR
;
2731 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
2732 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
2735 FIXME("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
2743 /* Context activation is done by the caller. */
2744 void context_stream_info_from_declaration(struct wined3d_context
*context
,
2745 const struct wined3d_state
*state
, struct wined3d_stream_info
*stream_info
)
2747 /* We need to deal with frequency data! */
2748 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
2749 BOOL use_vshader
= use_vs(state
);
2752 stream_info
->use_map
= 0;
2753 stream_info
->swizzle_map
= 0;
2754 stream_info
->position_transformed
= declaration
->position_transformed
;
2756 /* Translate the declaration into strided data. */
2757 for (i
= 0; i
< declaration
->element_count
; ++i
)
2759 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
2760 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
2764 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
2765 element
, i
+ 1, declaration
->element_count
);
2767 if (!stream
->buffer
)
2770 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
2774 if (element
->output_slot
== ~0U)
2776 /* TODO: Assuming vertexdeclarations are usually used with the
2777 * same or a similar shader, it might be worth it to store the
2778 * last used output slot and try that one first. */
2779 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
2780 element
->usage
, element
->usage_idx
, &idx
);
2784 idx
= element
->output_slot
;
2790 if (!element
->ffp_valid
)
2792 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
2793 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
2794 stride_used
= FALSE
;
2798 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
2804 TRACE("Load %s array %u [usage %s, usage_idx %u, "
2805 "input_slot %u, offset %u, stride %u, format %s].\n",
2806 use_vshader
? "shader": "fixed function", idx
,
2807 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
2808 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
));
2810 stream_info
->elements
[idx
].format
= element
->format
;
2811 stream_info
->elements
[idx
].data
.buffer_object
= 0;
2812 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
2813 stream_info
->elements
[idx
].stride
= stream
->stride
;
2814 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
2816 if (!context
->gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2817 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
2819 stream_info
->swizzle_map
|= 1 << idx
;
2821 stream_info
->use_map
|= 1 << idx
;
2826 /* Context activation is done by the caller. */
2827 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2829 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2830 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2831 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
2832 DWORD prev_all_vbo
= stream_info
->all_vbo
;
2836 context_stream_info_from_declaration(context
, state
, stream_info
);
2838 stream_info
->all_vbo
= 1;
2839 context
->num_buffer_queries
= 0;
2840 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
2842 struct wined3d_stream_info_element
*element
;
2843 struct wined3d_bo_address data
;
2844 struct wined3d_buffer
*buffer
;
2849 element
= &stream_info
->elements
[i
];
2850 buffer
= state
->streams
[element
->stream_idx
].buffer
;
2852 /* We can't use VBOs if the base vertex index is negative. OpenGL
2853 * doesn't accept negative offsets (or rather offsets bigger than the
2854 * VBO, because the pointer is unsigned), so use system memory
2855 * sources. In most sane cases the pointer - offset will still be > 0,
2856 * otherwise it will wrap around to some big value. Hope that with the
2857 * indices the driver wraps it back internally. If not,
2858 * drawStridedSlow is needed, including a vertex buffer path. */
2859 if (state
->load_base_vertex_index
< 0)
2861 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
2862 state
->load_base_vertex_index
);
2863 element
->data
.buffer_object
= 0;
2864 element
->data
.addr
+= (ULONG_PTR
)buffer_get_sysmem(buffer
, context
);
2865 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
2866 FIXME("System memory vertex data load offset is negative!\n");
2870 buffer_internal_preload(buffer
, context
, state
);
2871 buffer_get_memory(buffer
, context
, &data
);
2872 element
->data
.buffer_object
= data
.buffer_object
;
2873 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
2876 if (!element
->data
.buffer_object
)
2877 stream_info
->all_vbo
= 0;
2880 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
2882 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
2887 if (state
->vertex_declaration
->half_float_conv_needed
&& !stream_info
->all_vbo
)
2889 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
2890 context
->use_immediate_mode_draw
= TRUE
;
2894 context
->use_immediate_mode_draw
= FALSE
;
2899 WORD slow_mask
= (1 << WINED3D_FFP_PSIZE
);
2900 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2901 & ((1 << WINED3D_FFP_DIFFUSE
) | (1 << WINED3D_FFP_SPECULAR
));
2903 if (((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
2904 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
2905 context
->use_immediate_mode_draw
= TRUE
;
2907 context
->use_immediate_mode_draw
= FALSE
;
2910 if (prev_all_vbo
!= stream_info
->all_vbo
)
2911 context_invalidate_state(context
, STATE_INDEXBUFFER
);
2914 /* Context activation is done by the caller. */
2915 static void context_preload_texture(struct wined3d_context
*context
,
2916 const struct wined3d_state
*state
, unsigned int idx
)
2918 struct wined3d_texture
*texture
;
2920 if (!(texture
= state
->textures
[idx
]))
2923 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
2926 /* Context activation is done by the caller. */
2927 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2933 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
2935 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
2936 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
2942 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2944 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
2945 context_preload_texture(context
, state
, i
);
2950 WORD ffu_map
= context
->fixed_function_usage_map
;
2952 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2955 context_preload_texture(context
, state
, i
);
2960 static void context_bind_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2962 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2963 struct wined3d_shader_sampler_map_entry
*entry
;
2964 struct wined3d_shader_resource_view
*view
;
2965 struct wined3d_sampler
*sampler
;
2966 struct wined3d_texture
*texture
;
2967 struct wined3d_shader
*shader
;
2968 unsigned int i
, j
, count
;
2972 enum wined3d_shader_type type
;
2973 unsigned int base_idx
;
2978 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
2979 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
2982 for (i
= 0; i
< ARRAY_SIZE(shader_types
); ++i
)
2984 if (!(shader
= state
->shader
[shader_types
[i
].type
]))
2987 count
= shader
->reg_maps
.sampler_map
.count
;
2988 if (count
> shader_types
[i
].count
)
2990 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
2991 shader
, count
, shader_types
[i
].count
);
2992 count
= shader_types
[i
].count
;
2995 for (j
= 0; j
< count
; ++j
)
2997 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
2999 if (!(view
= state
->shader_resource_view
[shader_types
[i
].type
][entry
->resource_idx
]))
3001 WARN("No resource view bound at index %u, %u.\n", shader_types
[i
].type
, entry
->resource_idx
);
3005 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3007 FIXME("Buffer shader resources not supported.\n");
3011 if (!(sampler
= state
->sampler
[shader_types
[i
].type
][entry
->sampler_idx
]))
3013 WARN("No sampler object bound at index %u, %u.\n", shader_types
[i
].type
, entry
->sampler_idx
);
3017 texture
= wined3d_texture_from_resource(view
->resource
);
3018 wined3d_texture_load(texture
, context
, FALSE
);
3019 context_active_texture(context
, gl_info
, shader_types
[i
].base_idx
+ entry
->bind_idx
);
3020 wined3d_texture_bind(texture
, context
, FALSE
);
3022 GL_EXTCALL(glBindSampler(shader_types
[i
].base_idx
+ entry
->bind_idx
, sampler
->name
));
3023 checkGLcall("glBindSampler");
3028 /* Context activation is done by the caller. */
3029 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
3031 const struct wined3d_state
*state
= &device
->state
;
3032 const struct StateEntry
*state_table
= context
->state_table
;
3033 const struct wined3d_fb_state
*fb
= state
->fb
;
3037 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
3038 fb
->render_targets
, fb
->depth_stencil
))
3041 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3043 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3046 /* Preload resources before FBO setup. Texture preload in particular may
3047 * result in changes to the current FBO, due to using e.g. FBO blits for
3048 * updating a resource location. */
3049 context_update_tex_unit_map(context
, state
);
3050 context_preload_textures(context
, state
);
3051 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
))
3053 context_update_stream_info(context
, state
);
3057 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3060 buffer_mark_used(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
);
3063 if (state
->index_buffer
)
3065 if (context
->stream_info
.all_vbo
)
3066 buffer_internal_preload(state
->index_buffer
, context
, state
);
3068 buffer_get_sysmem(state
->index_buffer
, context
);
3071 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3073 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3075 if (state
->cb
[i
][j
])
3076 buffer_internal_preload(state
->cb
[i
][j
], context
, state
);
3080 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3082 DWORD rep
= context
->dirtyArray
[i
];
3083 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3084 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3085 context
->isStateDirty
[idx
] &= ~(1 << shift
);
3086 state_table
[rep
].apply(context
, state
, rep
);
3089 if (context
->shader_update_mask
)
3091 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3092 context
->shader_update_mask
= 0;
3095 if (context
->constant_update_mask
)
3097 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3098 context
->constant_update_mask
= 0;
3101 if (context
->update_shader_resource_bindings
)
3103 context_bind_shader_resources(context
, state
);
3104 context
->update_shader_resource_bindings
= 0;
3107 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3109 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3112 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3113 context
->last_was_blit
= FALSE
;
3118 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
3120 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3122 render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
3123 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
3125 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3126 * the alpha blend state changes with different render target formats. */
3127 if (!context
->current_rt
)
3129 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3133 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
3134 const struct wined3d_format
*new = target
->resource
.format
;
3136 if (old
->id
!= new->id
)
3138 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3139 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3140 || !(new->flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3141 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3143 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3144 if ((old
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3145 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3148 /* When switching away from an offscreen render target, and we're not
3149 * using FBOs, we have to read the drawable into the texture. This is
3150 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
3151 * There are some things that need care though. PreLoad needs a GL context,
3152 * and FindContext is called before the context is activated. It also
3153 * has to be called with the old rendertarget active, otherwise a
3154 * wrong drawable is read. */
3155 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
3156 && old_render_offscreen
&& context
->current_rt
!= target
)
3158 struct wined3d_texture
*texture
= context
->current_rt
->container
;
3160 /* Read the back buffer of the old drawable into the destination texture. */
3161 if (texture
->texture_srgb
.name
)
3162 wined3d_texture_load(texture
, context
, TRUE
);
3163 wined3d_texture_load(texture
, context
, FALSE
);
3164 surface_invalidate_location(context
->current_rt
, WINED3D_LOCATION_DRAWABLE
);
3168 context
->current_rt
= target
;
3169 context_set_render_offscreen(context
, render_offscreen
);
3172 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
3174 struct wined3d_context
*current_context
= context_get_current();
3175 struct wined3d_context
*context
;
3177 TRACE("device %p, target %p.\n", device
, target
);
3179 if (current_context
&& current_context
->destroyed
)
3180 current_context
= NULL
;
3185 && current_context
->current_rt
3186 && current_context
->swapchain
->device
== device
)
3188 target
= current_context
->current_rt
;
3192 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
3193 if (swapchain
->back_buffers
)
3194 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
3196 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
3200 if (current_context
&& current_context
->current_rt
== target
)
3202 context
= current_context
;
3204 else if (target
->container
->swapchain
)
3206 TRACE("Rendering onscreen.\n");
3208 context
= swapchain_get_context(target
->container
->swapchain
);
3212 TRACE("Rendering offscreen.\n");
3214 /* Stay with the current context if possible. Otherwise use the
3215 * context for the primary swapchain. */
3216 if (current_context
&& current_context
->swapchain
->device
== device
)
3217 context
= current_context
;
3219 context
= swapchain_get_context(device
->swapchains
[0]);
3222 context_enter(context
);
3223 context_update_window(context
);
3224 context_setup_target(context
, target
);
3225 if (!context
->valid
) return context
;
3227 if (context
!= current_context
)
3229 if (!context_set_current(context
))
3230 ERR("Failed to activate the new context.\n");
3232 else if (context
->needs_set
)
3234 context_set_gl_context(context
);