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 SFLAG_INTEXTURE
:
143 case SFLAG_INSRGBTEX
:
144 surface_prepare_texture(depth_stencil
, 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
->texture_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
->texture_name
,
158 depth_stencil
->texture_level
);
159 checkGLcall("glFramebufferTexture2D()");
163 case SFLAG_INRB_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 SFLAG_INRB_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", debug_surflocation(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 SFLAG_INTEXTURE
:
218 case SFLAG_INSRGBTEX
:
219 srgb
= location
== SFLAG_INSRGBTEX
;
220 surface_prepare_texture(surface
, 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 SFLAG_INRB_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 SFLAG_INRB_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", debug_surflocation(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", debug_surflocation(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
== SFLAG_INDRAWABLE
)
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 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
619 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
620 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
624 for (i
= 0; i
< device
->context_count
; ++i
)
626 struct wined3d_context
*context
= device
->contexts
[i
];
627 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
628 struct fbo_entry
*entry
, *entry2
;
630 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
632 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
636 if (entry
->depth_stencil
== surface
)
638 callback(context
, entry
);
642 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
644 if (entry
->render_targets
[j
] == surface
)
646 callback(context
, entry
);
654 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
656 list_remove(&entry
->entry
);
657 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
660 void context_resource_released(const struct wined3d_device
*device
,
661 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
663 if (!device
->d3d_initialized
) return;
667 case WINED3D_RTYPE_SURFACE
:
668 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
669 context_queue_fbo_entry_destruction
);
677 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
679 entry
->attached
= FALSE
;
682 void context_resource_unloaded(const struct wined3d_device
*device
,
683 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
687 case WINED3D_RTYPE_SURFACE
:
688 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
689 context_detach_fbo_entry
);
697 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
699 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
700 struct fbo_entry
*entry
= context
->current_fbo
;
703 if (!entry
|| context
->rebind_fbo
) return;
705 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
707 if (surface
== entry
->render_targets
[i
])
709 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
710 context
->rebind_fbo
= TRUE
;
715 if (surface
== entry
->depth_stencil
)
717 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
718 context
->rebind_fbo
= TRUE
;
722 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
724 int current
= GetPixelFormat(dc
);
726 if (current
== format
) return TRUE
;
730 if (!SetPixelFormat(dc
, format
, NULL
))
732 /* This may also happen if the dc belongs to a destroyed window. */
733 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
734 format
, dc
, GetLastError());
740 /* By default WGL doesn't allow pixel format adjustments but we need it
741 * here. For this reason there's a Wine specific wglSetPixelFormat()
742 * which allows us to set the pixel format multiple times. Only use it
743 * when really needed. */
744 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
746 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
748 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
755 /* OpenGL doesn't allow pixel format adjustments. Print an error and
756 * continue using the old format. There's a big chance that the old
757 * format works although with a performance hit and perhaps rendering
759 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
760 format
, dc
, current
);
764 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
766 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
769 if (!context_set_pixel_format(ctx
->gl_info
, ctx
->hdc
, ctx
->pixel_format
))
771 WARN("Failed to set pixel format %d on device context %p.\n",
772 ctx
->pixel_format
, ctx
->hdc
);
776 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
780 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
781 ctx
->glCtx
, ctx
->hdc
, GetLastError());
783 WARN("Trying fallback to the backup window.\n");
785 /* FIXME: If the context is destroyed it's no longer associated with
786 * a swapchain, so we can't use the swapchain to get a backup dc. To
787 * make this work windowless contexts would need to be handled by the
791 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
792 context_set_current(NULL
);
796 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
798 context_set_current(NULL
);
802 if (!context_set_pixel_format(ctx
->gl_info
, dc
, ctx
->pixel_format
))
804 ERR("Failed to set pixel format %d on device context %p.\n",
805 ctx
->pixel_format
, dc
);
806 context_set_current(NULL
);
810 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
812 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
814 context_set_current(NULL
);
821 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
, int pf
)
823 if (!context_set_pixel_format(gl_info
, dc
, pf
))
825 ERR("Failed to restore pixel format %d on device context %p.\n", pf
, dc
);
826 context_set_current(NULL
);
830 if (!wglMakeCurrent(dc
, gl_ctx
))
832 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
833 gl_ctx
, dc
, GetLastError());
834 context_set_current(NULL
);
838 static void context_update_window(struct wined3d_context
*context
)
840 if (context
->win_handle
== context
->swapchain
->win_handle
)
843 TRACE("Updating context %p window from %p to %p.\n",
844 context
, context
->win_handle
, context
->swapchain
->win_handle
);
848 /* You'd figure ReleaseDC() would fail if the DC doesn't match the
849 * window. However, that's not what actually happens, and there are
850 * user32 tests that confirm ReleaseDC() with the wrong window is
851 * supposed to succeed. So explicitly check that the DC belongs to
852 * the window, since we want to avoid releasing a DC that belongs to
853 * some other window if the original window was already destroyed. */
854 if (WindowFromDC(context
->hdc
) != context
->win_handle
)
856 WARN("DC %p does not belong to window %p.\n",
857 context
->hdc
, context
->win_handle
);
859 else if (!ReleaseDC(context
->win_handle
, context
->hdc
))
861 ERR("Failed to release device context %p, last error %#x.\n",
862 context
->hdc
, GetLastError());
865 else context
->valid
= 1;
867 context
->win_handle
= context
->swapchain
->win_handle
;
869 if (!(context
->hdc
= GetDC(context
->win_handle
)))
871 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
875 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
877 ERR("Failed to set pixel format %d on device context %p.\n",
878 context
->pixel_format
, context
->hdc
);
882 context_set_gl_context(context
);
890 static void context_destroy_gl_resources(struct wined3d_context
*context
)
892 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
893 struct wined3d_occlusion_query
*occlusion_query
;
894 struct wined3d_event_query
*event_query
;
895 struct fbo_entry
*entry
, *entry2
;
901 restore_ctx
= wglGetCurrentContext();
902 restore_dc
= wglGetCurrentDC();
903 restore_pf
= GetPixelFormat(restore_dc
);
905 if (context
->valid
&& restore_ctx
!= context
->glCtx
)
906 context_set_gl_context(context
);
910 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
912 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
913 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
914 occlusion_query
->context
= NULL
;
917 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
921 if (gl_info
->supported
[ARB_SYNC
])
923 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
925 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
926 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
928 event_query
->context
= NULL
;
931 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
933 if (!context
->valid
) entry
->id
= 0;
934 context_destroy_fbo_entry(context
, entry
);
937 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
939 if (!context
->valid
) entry
->id
= 0;
940 context_destroy_fbo_entry(context
, entry
);
945 if (context
->dummy_arbfp_prog
)
947 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
950 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
951 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
953 if (gl_info
->supported
[ARB_SYNC
])
955 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
957 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
960 else if (gl_info
->supported
[APPLE_FENCE
])
962 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
964 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
967 else if (gl_info
->supported
[NV_FENCE
])
969 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
971 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
975 checkGLcall("context cleanup");
978 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
979 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
983 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
, restore_pf
);
985 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
987 ERR("Failed to disable GL context.\n");
990 ReleaseDC(context
->win_handle
, context
->hdc
);
992 if (!wglDeleteContext(context
->glCtx
))
994 DWORD err
= GetLastError();
995 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
999 DWORD
context_get_tls_idx(void)
1001 return wined3d_context_tls_idx
;
1004 void context_set_tls_idx(DWORD idx
)
1006 wined3d_context_tls_idx
= idx
;
1009 struct wined3d_context
*context_get_current(void)
1011 return TlsGetValue(wined3d_context_tls_idx
);
1014 BOOL
context_set_current(struct wined3d_context
*ctx
)
1016 struct wined3d_context
*old
= context_get_current();
1020 TRACE("Already using D3D context %p.\n", ctx
);
1028 TRACE("Switching away from destroyed context %p.\n", old
);
1029 context_destroy_gl_resources(old
);
1030 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1031 HeapFree(GetProcessHeap(), 0, old
);
1043 ERR("Trying to make invalid context %p current\n", ctx
);
1047 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1048 if (!context_set_gl_context(ctx
))
1052 else if(wglGetCurrentContext())
1054 TRACE("Clearing current D3D context.\n");
1055 if (!wglMakeCurrent(NULL
, NULL
))
1057 DWORD err
= GetLastError();
1058 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1059 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1064 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1067 void context_release(struct wined3d_context
*context
)
1069 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1073 if (!context
->level
)
1074 WARN("Context %p is not active.\n", context
);
1075 else if (context
!= context_get_current())
1076 WARN("Context %p is not the current context.\n", context
);
1079 if (!--context
->level
&& context
->restore_ctx
)
1081 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1082 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
, context
->restore_pf
);
1083 context
->restore_ctx
= NULL
;
1084 context
->restore_dc
= NULL
;
1088 static void context_enter(struct wined3d_context
*context
)
1090 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1092 if (!context
->level
++)
1094 const struct wined3d_context
*current_context
= context_get_current();
1095 HGLRC current_gl
= wglGetCurrentContext();
1097 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1099 TRACE("Another GL context (%p on device context %p) is already current.\n",
1100 current_gl
, wglGetCurrentDC());
1101 context
->restore_ctx
= current_gl
;
1102 context
->restore_dc
= wglGetCurrentDC();
1103 context
->restore_pf
= GetPixelFormat(context
->restore_dc
);
1108 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1110 DWORD rep
= context
->state_table
[state
].representative
;
1114 if (isStateDirty(context
, rep
)) return;
1116 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1117 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1118 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1119 context
->isStateDirty
[idx
] |= (1 << shift
);
1122 /* This function takes care of wined3d pixel format selection. */
1123 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1124 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1125 BOOL auxBuffers
, BOOL findCompatible
)
1128 BYTE redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1129 BYTE depthBits
=0, stencilBits
=0;
1130 unsigned int current_value
;
1131 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1134 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1135 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1136 auxBuffers
, findCompatible
);
1138 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1140 ERR("Unable to get color bits for format %s (%#x)!\n",
1141 debug_d3dformat(color_format
->id
), color_format
->id
);
1145 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1148 for (i
= 0; i
< cfg_count
; ++i
)
1150 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1153 /* For now only accept RGBA formats. Perhaps some day we will
1154 * allow floating point formats for pbuffers. */
1155 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1157 /* In window mode we need a window drawable format and double buffering. */
1158 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1160 if (cfg
->redSize
< redBits
)
1162 if (cfg
->greenSize
< greenBits
)
1164 if (cfg
->blueSize
< blueBits
)
1166 if (cfg
->alphaSize
< alphaBits
)
1168 if (cfg
->depthSize
< depthBits
)
1170 if (stencilBits
&& cfg
->stencilSize
!= stencilBits
)
1172 /* Check multisampling support. */
1173 if (cfg
->numSamples
)
1177 /* We try to locate a format which matches our requirements exactly. In case of
1178 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1179 if (cfg
->depthSize
== depthBits
)
1181 if (cfg
->stencilSize
== stencilBits
)
1183 if (cfg
->alphaSize
== alphaBits
)
1185 /* We like to have aux buffers in backbuffer mode */
1186 if (auxBuffers
&& cfg
->auxBuffers
)
1188 if (cfg
->redSize
== redBits
1189 && cfg
->greenSize
== greenBits
1190 && cfg
->blueSize
== blueBits
)
1193 if (value
> current_value
)
1195 iPixelFormat
= cfg
->iPixelFormat
;
1196 current_value
= value
;
1200 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1201 if(!iPixelFormat
&& !findCompatible
) {
1202 ERR("Can't find a suitable iPixelFormat\n");
1204 } else if(!iPixelFormat
) {
1205 PIXELFORMATDESCRIPTOR pfd
;
1207 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1208 /* PixelFormat selection */
1209 ZeroMemory(&pfd
, sizeof(pfd
));
1210 pfd
.nSize
= sizeof(pfd
);
1212 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1213 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1214 pfd
.cAlphaBits
= alphaBits
;
1215 pfd
.cColorBits
= colorBits
;
1216 pfd
.cDepthBits
= depthBits
;
1217 pfd
.cStencilBits
= stencilBits
;
1218 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1220 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1222 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1223 ERR("Can't find a suitable iPixelFormat\n");
1228 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1229 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1230 return iPixelFormat
;
1233 /* Context activation is done by the caller. */
1234 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1236 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1237 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1239 for (i
= 0; i
< count
; ++i
)
1241 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1242 checkGLcall("glActiveTextureARB");
1244 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1245 checkGLcall("glBindTexture");
1247 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1249 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1250 checkGLcall("glBindTexture");
1253 if (gl_info
->supported
[EXT_TEXTURE3D
])
1255 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1256 checkGLcall("glBindTexture");
1259 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1261 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1262 checkGLcall("glBindTexture");
1267 BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1269 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1270 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1273 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1274 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1278 case GL_DEBUG_TYPE_ERROR_ARB
:
1279 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1282 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1283 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1284 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1285 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1288 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1289 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1293 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1298 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1299 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1301 struct wined3d_device
*device
= swapchain
->device
;
1302 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1303 const struct wined3d_format
*color_format
;
1304 struct wined3d_context
*ret
;
1305 BOOL auxBuffers
= FALSE
;
1306 HGLRC ctx
, share_ctx
;
1313 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1315 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1319 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1320 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1321 if (!ret
->blit_targets
)
1324 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1325 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1326 if (!ret
->draw_buffers
)
1329 ret
->free_occlusion_query_size
= 4;
1330 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1331 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1332 if (!ret
->free_occlusion_queries
)
1335 list_init(&ret
->occlusion_queries
);
1337 ret
->free_event_query_size
= 4;
1338 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1339 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1340 if (!ret
->free_event_queries
)
1343 list_init(&ret
->event_queries
);
1344 list_init(&ret
->fbo_list
);
1345 list_init(&ret
->fbo_destroy_list
);
1347 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1349 ERR("Failed to allocate shader backend context data.\n");
1353 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1355 WARN("Failed to retireve device context, trying swapchain backup.\n");
1357 if (!(hdc
= swapchain_get_backup_dc(swapchain
)))
1359 ERR("Failed to retrieve a device context.\n");
1364 color_format
= target
->resource
.format
;
1366 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1367 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1368 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1372 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1373 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1374 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1375 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1378 /* DirectDraw supports 8bit paletted render targets and these are used by
1379 * old games like StarCraft and C&C. Most modern hardware doesn't support
1380 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1381 * conversion (ab)uses the alpha component for storing the palette index.
1382 * For this reason we require a format with 8bit alpha, so request
1384 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1385 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1387 /* Try to find a pixel format which matches our requirements. */
1388 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1390 /* Try to locate a compatible format if we weren't able to find anything. */
1393 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1394 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1397 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1400 ERR("Can't find a suitable pixel format.\n");
1406 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1408 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1409 context_release(ret
);
1413 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1414 if (gl_info
->p_wglCreateContextAttribsARB
)
1416 unsigned int ctx_attrib_idx
= 0;
1417 GLint ctx_attribs
[3];
1419 if (context_debug_output_enabled(gl_info
))
1421 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1422 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_DEBUG_BIT_ARB
;
1424 ctx_attribs
[ctx_attrib_idx
] = 0;
1426 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1428 ERR("Failed to create a WGL context.\n");
1429 context_release(ret
);
1435 if (!(ctx
= wglCreateContext(hdc
)))
1437 ERR("Failed to create a WGL context.\n");
1438 context_release(ret
);
1442 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1444 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1445 context_release(ret
);
1446 if (!wglDeleteContext(ctx
))
1447 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1452 if (!device_context_add(device
, ret
))
1454 ERR("Failed to add the newly created context to the context list\n");
1455 context_release(ret
);
1456 if (!wglDeleteContext(ctx
))
1457 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1461 ret
->gl_info
= gl_info
;
1462 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1463 ret
->state_table
= device
->StateTable
;
1465 /* Mark all states dirty to force a proper initialization of the states
1466 * on the first use of the context. */
1467 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1469 if (ret
->state_table
[state
].representative
)
1470 context_invalidate_state(ret
, state
);
1473 ret
->swapchain
= swapchain
;
1474 ret
->current_rt
= target
;
1475 ret
->tid
= GetCurrentThreadId();
1477 ret
->render_offscreen
= surface_is_offscreen(target
);
1478 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1482 ret
->win_handle
= swapchain
->win_handle
;
1484 ret
->pixel_format
= pixel_format
;
1486 /* Set up the context defaults */
1487 if (!context_set_current(ret
))
1489 ERR("Cannot activate context to set up defaults.\n");
1490 device_context_remove(device
, ret
);
1491 context_release(ret
);
1492 if (!wglDeleteContext(ctx
))
1493 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1497 if (context_debug_output_enabled(gl_info
))
1499 GL_EXTCALL(glDebugMessageCallbackARB(wined3d_debug_callback
, ret
));
1500 if (TRACE_ON(d3d_synchronous
))
1501 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
);
1502 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1505 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR_ARB
,
1506 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1510 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
,
1511 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1512 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
,
1513 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1514 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY_ARB
,
1515 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1517 if (WARN_ON(d3d_perf
))
1519 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE_ARB
,
1520 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1524 switch (swapchain
->desc
.swap_interval
)
1526 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1529 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1530 case WINED3DPRESENT_INTERVAL_ONE
:
1533 case WINED3DPRESENT_INTERVAL_TWO
:
1536 case WINED3DPRESENT_INTERVAL_THREE
:
1539 case WINED3DPRESENT_INTERVAL_FOUR
:
1543 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1547 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1549 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1550 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1551 swap_interval
, ret
, GetLastError());
1554 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1556 TRACE("Setting up the screen\n");
1558 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1559 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1561 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1562 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1564 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1565 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1567 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1568 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1569 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1570 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1572 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1574 /* Direct3D always uses n-1 weights for n world matrices and uses
1575 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1576 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1577 * enabled as well. */
1578 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1579 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1581 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1583 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1584 * the previous texture where to source the offset from is always unit - 1.
1586 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1588 context_active_texture(ret
, gl_info
, s
);
1589 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1590 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1591 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1594 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1596 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1597 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1598 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1599 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1602 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1603 * program and the dummy program is destroyed when the context is destroyed.
1605 const char *dummy_program
=
1607 "MOV result.color, fragment.color.primary;\n"
1609 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1610 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1611 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1614 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1616 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1618 context_active_texture(ret
, gl_info
, s
);
1619 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1620 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1624 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1626 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1628 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1630 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1632 ret
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
1633 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
1634 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
1636 /* If this happens to be the first context for the device, dummy textures
1637 * are not created yet. In that case, they will be created (and bound) by
1638 * create_dummy_textures right after this context is initialized. */
1639 if (device
->dummy_texture_2d
[0])
1640 bind_dummy_textures(device
, ret
);
1642 TRACE("Created context %p.\n", ret
);
1647 device
->shader_backend
->shader_free_context_data(ret
);
1648 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1649 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1650 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1651 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1652 HeapFree(GetProcessHeap(), 0, ret
);
1656 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1660 TRACE("Destroying ctx %p\n", context
);
1662 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1664 context_destroy_gl_resources(context
);
1665 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1670 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1671 in wined3d_adapter may go away in the meantime */
1672 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
1673 *gl_info
= *context
->gl_info
;
1674 context
->gl_info
= gl_info
;
1675 context
->destroyed
= 1;
1679 device
->shader_backend
->shader_free_context_data(context
);
1680 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1681 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1682 device_context_remove(device
, context
);
1683 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1686 /* Context activation is done by the caller. */
1687 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
1689 const GLdouble projection
[] =
1691 2.0 / width
, 0.0, 0.0, 0.0,
1692 0.0, 2.0 / height
, 0.0, 0.0,
1694 -1.0, -1.0, -1.0, 1.0,
1697 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
1698 checkGLcall("glMatrixMode(GL_PROJECTION)");
1699 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
1700 checkGLcall("glLoadMatrixd");
1701 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
1702 checkGLcall("glViewport");
1705 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
1707 const struct wined3d_surface
*rt
= context
->current_rt
;
1709 if (rt
->swapchain
&& rt
->swapchain
->front_buffer
== rt
)
1713 GetClientRect(context
->win_handle
, &window_size
);
1714 size
->cx
= window_size
.right
- window_size
.left
;
1715 size
->cy
= window_size
.bottom
- window_size
.top
;
1720 size
->cx
= rt
->resource
.width
;
1721 size
->cy
= rt
->resource
.height
;
1724 /*****************************************************************************
1727 * Sets up a context for DirectDraw blitting.
1728 * All texture units are disabled, texture unit 0 is set as current unit
1729 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1730 * color writing enabled for all channels
1731 * register combiners disabled, shaders disabled
1732 * world matrix is set to identity, texture matrix 0 too
1733 * projection matrix is setup for drawing screen coordinates
1736 * This: Device to activate the context for
1737 * context: Context to setup
1739 *****************************************************************************/
1740 /* Context activation is done by the caller. */
1741 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
1744 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1748 TRACE("Setting up context %p for blitting\n", context
);
1750 context_get_rt_size(context
, &rt_size
);
1752 if (context
->last_was_blit
)
1754 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
1756 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1757 context
->blit_w
= rt_size
.cx
;
1758 context
->blit_h
= rt_size
.cy
;
1759 /* No need to dirtify here, the states are still dirtified because
1760 * they weren't applied since the last SetupForBlit() call. */
1762 TRACE("Context is already set up for blitting, nothing to do\n");
1765 context
->last_was_blit
= TRUE
;
1767 /* Disable all textures. The caller can then bind a texture it wants to blit
1770 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1771 * function texture unit. No need to care for higher samplers
1773 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1775 sampler
= device
->rev_tex_unit_map
[i
];
1776 context_active_texture(context
, gl_info
, i
);
1778 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1780 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1781 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1783 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1784 checkGLcall("glDisable GL_TEXTURE_3D");
1785 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1787 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1788 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1790 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1791 checkGLcall("glDisable GL_TEXTURE_2D");
1793 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1794 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1796 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1798 if (sampler
< MAX_TEXTURES
)
1799 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1800 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1803 context_active_texture(context
, gl_info
, 0);
1805 sampler
= device
->rev_tex_unit_map
[0];
1807 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1809 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1810 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1812 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1813 checkGLcall("glDisable GL_TEXTURE_3D");
1814 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1816 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1817 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1819 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1820 checkGLcall("glDisable GL_TEXTURE_2D");
1822 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1824 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
1825 checkGLcall("glMatrixMode(GL_TEXTURE)");
1826 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1827 checkGLcall("glLoadIdentity()");
1829 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1831 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1832 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
1833 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1836 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1838 if (sampler
< MAX_TEXTURES
)
1840 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
1841 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1843 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1846 /* Other misc states */
1847 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
1848 checkGLcall("glDisable(GL_ALPHA_TEST)");
1849 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
1850 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
1851 checkGLcall("glDisable GL_LIGHTING");
1852 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
1853 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
1854 checkGLcall("glDisable GL_DEPTH_TEST");
1855 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
1856 glDisableWINE(GL_FOG
);
1857 checkGLcall("glDisable GL_FOG");
1858 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
1859 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
1860 checkGLcall("glDisable GL_BLEND");
1861 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
1862 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
1863 checkGLcall("glDisable GL_CULL_FACE");
1864 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
1865 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
1866 checkGLcall("glDisable GL_STENCIL_TEST");
1867 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
1868 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
1869 checkGLcall("glDisable GL_SCISSOR_TEST");
1870 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
1871 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1873 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
1874 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1875 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
1877 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1878 checkGLcall("glColorMask");
1879 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
1880 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
1881 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
1882 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
1883 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1885 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
1886 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
1887 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1890 /* Setup transforms */
1891 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
1892 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1893 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1894 checkGLcall("glLoadIdentity()");
1895 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
1897 context
->last_was_rhw
= TRUE
;
1898 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
1900 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1901 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1902 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1903 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1904 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1905 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1906 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
1908 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1910 /* Disable shaders */
1911 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
1913 context
->blit_w
= rt_size
.cx
;
1914 context
->blit_h
= rt_size
.cy
;
1915 context_invalidate_state(context
, STATE_VIEWPORT
);
1916 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
1919 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
1921 return rt_mask
& (1 << 31);
1924 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
1926 return rt_mask
& ~(1 << 31);
1929 /* Context activation is done by the caller. */
1930 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
1932 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1936 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
1937 checkGLcall("glDrawBuffer()");
1939 else if (is_rt_mask_onscreen(rt_mask
))
1941 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
1942 checkGLcall("glDrawBuffer()");
1946 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1953 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
1955 context
->draw_buffers
[i
] = GL_NONE
;
1961 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1963 GL_EXTCALL(glDrawBuffersARB(i
, context
->draw_buffers
));
1964 checkGLcall("glDrawBuffers()");
1968 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
1969 checkGLcall("glDrawBuffer()");
1974 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
1979 /* Context activation is done by the caller. */
1980 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
1982 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1983 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
1984 DWORD new_mask
= context_generate_rt_mask(buffer
);
1986 if (new_mask
== *current_mask
)
1989 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
1990 checkGLcall("glDrawBuffer()");
1992 *current_mask
= new_mask
;
1995 /* Context activation is done by the caller. */
1996 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
1998 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0
+ unit
));
1999 checkGLcall("glActiveTextureARB");
2000 context
->active_texture
= unit
;
2003 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2005 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2006 DWORD unit
= context
->active_texture
;
2007 DWORD old_texture_type
= context
->texture_type
[unit
];
2011 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2012 checkGLcall("glBindTexture");
2019 if (old_texture_type
!= target
)
2021 const struct wined3d_device
*device
= context
->swapchain
->device
;
2023 switch (old_texture_type
)
2029 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2030 checkGLcall("glBindTexture");
2032 case GL_TEXTURE_RECTANGLE_ARB
:
2033 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2034 checkGLcall("glBindTexture");
2036 case GL_TEXTURE_CUBE_MAP
:
2037 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2038 checkGLcall("glBindTexture");
2041 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2042 checkGLcall("glBindTexture");
2045 ERR("Unexpected texture target %#x\n", old_texture_type
);
2048 context
->texture_type
[unit
] = target
;
2052 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2054 if (context
->render_offscreen
== offscreen
) return;
2056 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2057 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2058 context_invalidate_state(context
, STATE_VIEWPORT
);
2059 context_invalidate_state(context
, STATE_SCISSORRECT
);
2060 context_invalidate_state(context
, STATE_FRONTFACE
);
2061 context
->render_offscreen
= offscreen
;
2064 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2065 const struct wined3d_format
*required
)
2067 BYTE existing_depth
, existing_stencil
, required_depth
, required_stencil
;
2069 if (existing
== required
) return TRUE
;
2070 if ((existing
->flags
& WINED3DFMT_FLAG_FLOAT
) != (required
->flags
& WINED3DFMT_FLAG_FLOAT
)) return FALSE
;
2072 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
2073 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
2075 if(existing_depth
< required_depth
) return FALSE
;
2076 /* If stencil bits are used the exact amount is required - otherwise wrapping
2077 * won't work correctly */
2078 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
2082 /* The caller provides a context */
2083 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2084 const struct wined3d_surface
*depth_stencil
)
2086 /* Onscreen surfaces are always in a swapchain */
2087 struct wined3d_swapchain
*swapchain
= context
->current_rt
->swapchain
;
2089 if (context
->render_offscreen
|| !depth_stencil
) return;
2090 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->resource
.format
)) return;
2092 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2093 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2095 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2097 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2098 surface_load_location(context
->current_rt
, SFLAG_INTEXTURE
, NULL
);
2099 swapchain
->render_to_fbo
= TRUE
;
2100 swapchain_update_draw_bindings(swapchain
);
2101 context_set_render_offscreen(context
, TRUE
);
2104 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
2106 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2108 else if (rt
->swapchain
)
2109 return context_generate_rt_mask_from_surface(rt
);
2111 return context_generate_rt_mask(device
->offscreenBuffer
);
2114 /* Context activation is done by the caller. */
2115 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2117 struct wined3d_surface
*rt
= context
->current_rt
;
2118 DWORD rt_mask
, *cur_mask
;
2120 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2122 context_validate_onscreen_formats(context
, NULL
);
2124 if (context
->render_offscreen
)
2126 surface_internal_preload(rt
, SRGB_RGB
);
2128 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->draw_binding
);
2129 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2136 context
->current_fbo
= NULL
;
2137 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2138 rt_mask
= context_generate_rt_mask_from_surface(rt
);
2143 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt
);
2146 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2148 if (rt_mask
!= *cur_mask
)
2150 context_apply_draw_buffers(context
, rt_mask
);
2151 *cur_mask
= rt_mask
;
2154 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2156 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2159 SetupForBlit(device
, context
);
2160 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2163 static BOOL
context_validate_rt_config(UINT rt_count
,
2164 struct wined3d_surface
* const *rts
, const struct wined3d_surface
*ds
)
2168 if (ds
) return TRUE
;
2170 for (i
= 0; i
< rt_count
; ++i
)
2172 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2176 WARN("Invalid render target config, need at least one attachment.\n");
2180 /* Context activation is done by the caller. */
2181 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2182 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2184 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2185 DWORD rt_mask
= 0, *cur_mask
;
2187 struct wined3d_surface
**rts
= fb
->render_targets
;
2189 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2190 || rt_count
!= context
->gl_info
->limits
.buffers
)
2192 if (!context_validate_rt_config(rt_count
, rts
, fb
->depth_stencil
))
2195 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2197 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2199 if (!rt_count
|| surface_is_offscreen(rts
[0]))
2201 for (i
= 0; i
< rt_count
; ++i
)
2203 context
->blit_targets
[i
] = rts
[i
];
2204 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
)
2205 rt_mask
|= (1 << i
);
2207 while (i
< context
->gl_info
->limits
.buffers
)
2209 context
->blit_targets
[i
] = NULL
;
2212 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, fb
->depth_stencil
,
2213 rt_count
? rts
[0]->draw_binding
: SFLAG_INTEXTURE
);
2217 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2218 rt_mask
= context_generate_rt_mask_from_surface(rts
[0]);
2221 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2222 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2223 * state management allows this */
2224 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2228 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt_count
? rts
[0] : NULL
);
2231 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2232 && (!rt_count
|| surface_is_offscreen(rts
[0])))
2234 for (i
= 0; i
< rt_count
; ++i
)
2236 if (rts
[i
] && rts
[i
]->resource
.format
->id
!= WINED3DFMT_NULL
) rt_mask
|= (1 << i
);
2241 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt_count
? rts
[0] : NULL
);
2244 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2246 if (rt_mask
!= *cur_mask
)
2248 context_apply_draw_buffers(context
, rt_mask
);
2249 *cur_mask
= rt_mask
;
2250 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2253 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2255 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2258 if (context
->last_was_blit
)
2259 context
->last_was_blit
= FALSE
;
2261 /* Blending and clearing should be orthogonal, but tests on the nvidia
2262 * driver show that disabling blending when clearing improves the clearing
2263 * performance incredibly. */
2264 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2265 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2266 checkGLcall("glEnable GL_SCISSOR_TEST");
2268 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2269 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2270 context_invalidate_state(context
, STATE_SCISSORRECT
);
2275 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_device
*device
)
2277 const struct wined3d_state
*state
= &device
->state
;
2278 struct wined3d_surface
**rts
= state
->fb
->render_targets
;
2279 struct wined3d_shader
*ps
= state
->pixel_shader
;
2280 DWORD rt_mask
, rt_mask_bits
;
2283 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) return context_generate_rt_mask_no_fbo(device
, rts
[0]);
2284 else if (!context
->render_offscreen
) return context_generate_rt_mask_from_surface(rts
[0]);
2286 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2287 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2288 rt_mask_bits
= rt_mask
;
2290 while (rt_mask_bits
)
2292 rt_mask_bits
&= ~(1 << i
);
2293 if (!rts
[i
] || rts
[i
]->resource
.format
->id
== WINED3DFMT_NULL
)
2294 rt_mask
&= ~(1 << i
);
2302 /* Context activation is done by the caller. */
2303 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2305 const struct wined3d_device
*device
= context
->swapchain
->device
;
2306 const struct wined3d_fb_state
*fb
= state
->fb
;
2307 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2310 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2312 if (!context
->render_offscreen
)
2314 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
, SFLAG_INDRAWABLE
);
2318 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, fb
->render_targets
, fb
->depth_stencil
,
2319 fb
->render_targets
[0]->draw_binding
);
2323 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2324 if (rt_mask
!= *cur_mask
)
2326 context_apply_draw_buffers(context
, rt_mask
);
2327 *cur_mask
= rt_mask
;
2331 /* Context activation is done by the caller. */
2332 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2334 const struct wined3d_device
*device
= context
->swapchain
->device
;
2335 DWORD rt_mask
, *cur_mask
;
2337 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2339 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2340 rt_mask
= find_draw_buffers_mask(context
, device
);
2341 if (rt_mask
!= *cur_mask
)
2343 context_apply_draw_buffers(context
, rt_mask
);
2344 *cur_mask
= rt_mask
;
2348 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
2350 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
2351 *regnum
= WINED3D_FFP_POSITION
;
2352 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
2353 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
2354 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
2355 *regnum
= WINED3D_FFP_BLENDINDICES
;
2356 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
2357 *regnum
= WINED3D_FFP_NORMAL
;
2358 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
2359 *regnum
= WINED3D_FFP_PSIZE
;
2360 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
2361 *regnum
= WINED3D_FFP_DIFFUSE
;
2362 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
2363 *regnum
= WINED3D_FFP_SPECULAR
;
2364 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
2365 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
2368 FIXME("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
2376 /* FIXME: Separate buffer loading from declaration decoding */
2377 /* Context activation is done by the caller. */
2378 void context_stream_info_from_declaration(struct wined3d_context
*context
,
2379 const struct wined3d_state
*state
, struct wined3d_stream_info
*stream_info
)
2381 /* We need to deal with frequency data! */
2382 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
2387 stream_info
->use_map
= 0;
2388 stream_info
->swizzle_map
= 0;
2389 stream_info
->all_vbo
= 1;
2391 /* Check for transformed vertices, disable vertex shader if present. */
2392 stream_info
->position_transformed
= declaration
->position_transformed
;
2393 use_vshader
= state
->vertex_shader
&& !declaration
->position_transformed
;
2395 /* Translate the declaration into strided data. */
2396 for (i
= 0; i
< declaration
->element_count
; ++i
)
2398 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
2399 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
2400 struct wined3d_buffer
*buffer
= stream
->buffer
;
2401 struct wined3d_bo_address data
;
2406 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
2407 element
, i
+ 1, declaration
->element_count
);
2409 if (!buffer
) continue;
2411 stride
= stream
->stride
;
2412 TRACE("Stream %u in buffer %p.\n", element
->input_slot
, buffer
);
2413 buffer_get_memory(buffer
, context
, &data
);
2415 /* Can't use vbo's if the base vertex index is negative. OpenGL doesn't accept negative offsets
2416 * (or rather offsets bigger than the vbo, because the pointer is unsigned), so use system memory
2417 * sources. In most sane cases the pointer - offset will still be > 0, otherwise it will wrap
2418 * around to some big value. Hope that with the indices, the driver wraps it back internally. If
2419 * not, drawStridedSlow is needed, including a vertex buffer path. */
2420 if (state
->load_base_vertex_index
< 0)
2422 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
2423 state
->load_base_vertex_index
);
2424 data
.buffer_object
= 0;
2425 data
.addr
= buffer_get_sysmem(buffer
, context
);
2426 if ((UINT_PTR
)data
.addr
< -state
->load_base_vertex_index
* stride
)
2428 FIXME("System memory vertex data load offset is negative!\n");
2431 data
.addr
+= element
->offset
;
2433 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
2437 if (element
->output_slot
== ~0U)
2439 /* TODO: Assuming vertexdeclarations are usually used with the
2440 * same or a similar shader, it might be worth it to store the
2441 * last used output slot and try that one first. */
2442 stride_used
= vshader_get_input(state
->vertex_shader
,
2443 element
->usage
, element
->usage_idx
, &idx
);
2447 idx
= element
->output_slot
;
2453 if (!element
->ffp_valid
)
2455 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
2456 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
2457 stride_used
= FALSE
;
2461 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
2467 TRACE("Load %s array %u [usage %s, usage_idx %u, "
2468 "input_slot %u, offset %u, stride %u, format %s, buffer_object %u].\n",
2469 use_vshader
? "shader": "fixed function", idx
,
2470 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
2471 element
->offset
, stride
, debug_d3dformat(element
->format
->id
), data
.buffer_object
);
2473 data
.addr
+= stream
->offset
;
2475 stream_info
->elements
[idx
].format
= element
->format
;
2476 stream_info
->elements
[idx
].data
= data
;
2477 stream_info
->elements
[idx
].stride
= stride
;
2478 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
2480 if (!context
->gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2481 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
2483 stream_info
->swizzle_map
|= 1 << idx
;
2485 stream_info
->use_map
|= 1 << idx
;
2489 /* Preload the vertex buffers. */
2490 context
->num_buffer_queries
= 0;
2491 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
2493 struct wined3d_stream_info_element
*element
;
2494 struct wined3d_buffer
*buffer
;
2499 element
= &stream_info
->elements
[i
];
2500 buffer
= state
->streams
[element
->stream_idx
].buffer
;
2501 buffer_internal_preload(buffer
, context
, state
);
2503 /* If the preload dropped the buffer object, update the stream info. */
2504 if (buffer
->buffer_object
!= element
->data
.buffer_object
)
2506 element
->data
.buffer_object
= 0;
2507 element
->data
.addr
= buffer_get_sysmem(buffer
, context
)
2508 + (ptrdiff_t)element
->data
.addr
;
2511 if (!buffer
->buffer_object
)
2512 stream_info
->all_vbo
= 0;
2515 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
2519 /* Context activation is done by the caller. */
2520 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2522 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2523 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2524 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
2525 DWORD prev_all_vbo
= stream_info
->all_vbo
;
2527 TRACE("============================= Vertex Declaration =============================\n");
2528 context_stream_info_from_declaration(context
, state
, stream_info
);
2530 if (state
->vertex_shader
&& !stream_info
->position_transformed
)
2532 if (state
->vertex_declaration
->half_float_conv_needed
&& !stream_info
->all_vbo
)
2534 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
2535 context
->use_immediate_mode_draw
= TRUE
;
2539 context
->use_immediate_mode_draw
= FALSE
;
2544 WORD slow_mask
= (1 << WINED3D_FFP_PSIZE
);
2545 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2546 & ((1 << WINED3D_FFP_DIFFUSE
) | (1 << WINED3D_FFP_SPECULAR
));
2548 if (((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
2549 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
2550 context
->use_immediate_mode_draw
= TRUE
;
2552 context
->use_immediate_mode_draw
= FALSE
;
2555 if (prev_all_vbo
!= stream_info
->all_vbo
)
2556 context_invalidate_state(context
, STATE_INDEXBUFFER
);
2559 /* Context activation is done by the caller. */
2560 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
2562 const struct wined3d_state
*state
= &device
->state
;
2563 const struct StateEntry
*state_table
= context
->state_table
;
2564 const struct wined3d_fb_state
*fb
= state
->fb
;
2567 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
2568 fb
->render_targets
, fb
->depth_stencil
))
2571 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
2573 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
2576 /* Preload resources before FBO setup. Texture preload in particular may
2577 * result in changes to the current FBO, due to using e.g. FBO blits for
2578 * updating a resource location. */
2579 device_update_tex_unit_map(device
);
2580 device_preload_textures(device
, context
);
2581 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
))
2582 context_update_stream_info(context
, state
);
2583 if (state
->index_buffer
)
2585 if (context
->stream_info
.all_vbo
)
2586 buffer_internal_preload(state
->index_buffer
, context
, state
);
2588 buffer_get_sysmem(state
->index_buffer
, context
);
2591 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2593 DWORD rep
= context
->dirtyArray
[i
];
2594 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2595 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2596 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2597 state_table
[rep
].apply(context
, state
, rep
);
2600 if (context
->shader_update_mask
)
2602 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
2603 context
->shader_update_mask
= 0;
2606 if (context
->constant_update_mask
)
2608 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
2609 context
->constant_update_mask
= 0;
2612 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2614 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2617 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2618 context
->last_was_blit
= FALSE
;
2623 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
2625 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
2627 render_offscreen
= surface_is_offscreen(target
);
2628 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
2630 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2631 * the alpha blend state changes with different render target formats. */
2632 if (!context
->current_rt
)
2634 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2638 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
2639 const struct wined3d_format
*new = target
->resource
.format
;
2641 if (old
->id
!= new->id
)
2643 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2644 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
2645 || !(new->flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
2646 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2648 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2649 if ((old
->flags
& WINED3DFMT_FLAG_SRGB_WRITE
) != (new->flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
2650 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
2653 /* When switching away from an offscreen render target, and we're not
2654 * using FBOs, we have to read the drawable into the texture. This is
2655 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2656 * are some things that need care though. PreLoad needs a GL context,
2657 * and FindContext is called before the context is activated. It also
2658 * has to be called with the old rendertarget active, otherwise a
2659 * wrong drawable is read. */
2660 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
2661 && old_render_offscreen
&& context
->current_rt
!= target
)
2663 /* Read the back buffer of the old drawable into the destination texture. */
2664 if (context
->current_rt
->texture_name_srgb
)
2665 surface_internal_preload(context
->current_rt
, SRGB_SRGB
);
2666 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2667 surface_invalidate_location(context
->current_rt
, SFLAG_INDRAWABLE
);
2671 context
->current_rt
= target
;
2672 context_set_render_offscreen(context
, render_offscreen
);
2675 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
2677 struct wined3d_context
*current_context
= context_get_current();
2678 struct wined3d_context
*context
;
2680 TRACE("device %p, target %p.\n", device
, target
);
2682 if (current_context
&& current_context
->destroyed
)
2683 current_context
= NULL
;
2688 && current_context
->current_rt
2689 && current_context
->swapchain
->device
== device
)
2691 target
= current_context
->current_rt
;
2695 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
2696 if (swapchain
->back_buffers
)
2697 target
= swapchain
->back_buffers
[0];
2699 target
= swapchain
->front_buffer
;
2703 if (current_context
&& current_context
->current_rt
== target
)
2705 context
= current_context
;
2707 else if (target
->swapchain
)
2709 TRACE("Rendering onscreen.\n");
2711 context
= swapchain_get_context(target
->swapchain
);
2715 TRACE("Rendering offscreen.\n");
2717 /* Stay with the current context if possible. Otherwise use the
2718 * context for the primary swapchain. */
2719 if (current_context
&& current_context
->swapchain
->device
== device
)
2720 context
= current_context
;
2722 context
= swapchain_get_context(device
->swapchains
[0]);
2725 context_update_window(context
);
2726 context_setup_target(context
, target
);
2727 context_enter(context
);
2728 if (!context
->valid
) return context
;
2730 if (context
!= current_context
)
2732 if (!context_set_current(context
))
2733 ERR("Failed to activate the new context.\n");
2735 else if (context
->restore_ctx
)
2737 context_set_gl_context(context
);