2 * Context and render target management in wined3d
4 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
5 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wined3d_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
25 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous
);
28 #define WINED3D_MAX_FBO_ENTRIES 64
30 static DWORD wined3d_context_tls_idx
;
32 /* FBO helper functions */
34 /* Context activation is done by the caller. */
35 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint fbo
)
37 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
41 case GL_READ_FRAMEBUFFER
:
42 if (context
->fbo_read_binding
== fbo
) return;
43 context
->fbo_read_binding
= fbo
;
46 case GL_DRAW_FRAMEBUFFER
:
47 if (context
->fbo_draw_binding
== fbo
) return;
48 context
->fbo_draw_binding
= fbo
;
52 if (context
->fbo_read_binding
== fbo
53 && context
->fbo_draw_binding
== fbo
) return;
54 context
->fbo_read_binding
= fbo
;
55 context
->fbo_draw_binding
= fbo
;
59 FIXME("Unhandled target %#x.\n", target
);
63 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
64 checkGLcall("glBindFramebuffer()");
67 /* Context activation is done by the caller. */
68 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
72 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
74 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
75 checkGLcall("glFramebufferTexture2D()");
77 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
78 checkGLcall("glFramebufferTexture2D()");
80 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
81 checkGLcall("glFramebufferTexture2D()");
84 /* Context activation is done by the caller. */
85 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint fbo
)
87 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
89 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
90 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
91 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
93 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
94 checkGLcall("glDeleteFramebuffers()");
97 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
98 GLenum fbo_target
, DWORD format_flags
, GLuint rb
)
100 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
102 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
103 checkGLcall("glFramebufferRenderbuffer()");
106 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
108 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
109 checkGLcall("glFramebufferRenderbuffer()");
113 /* Context activation is done by the caller. */
114 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
115 GLenum fbo_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
117 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
119 TRACE("Attach depth stencil %p\n", depth_stencil
);
123 DWORD format_flags
= depth_stencil
->container
->resource
.format_flags
;
125 if (depth_stencil
->current_renderbuffer
)
127 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
128 format_flags
, depth_stencil
->current_renderbuffer
->id
);
134 case WINED3D_LOCATION_TEXTURE_RGB
:
135 case WINED3D_LOCATION_TEXTURE_SRGB
:
136 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
138 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
139 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
140 depth_stencil
->texture_level
);
141 checkGLcall("glFramebufferTexture2D()");
144 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
146 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
147 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
148 depth_stencil
->texture_level
);
149 checkGLcall("glFramebufferTexture2D()");
153 case WINED3D_LOCATION_RB_MULTISAMPLE
:
154 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
155 format_flags
, depth_stencil
->rb_multisample
);
158 case WINED3D_LOCATION_RB_RESOLVED
:
159 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
160 format_flags
, depth_stencil
->rb_resolved
);
164 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
169 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
171 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
172 checkGLcall("glFramebufferTexture2D()");
175 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
177 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
178 checkGLcall("glFramebufferTexture2D()");
183 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
184 checkGLcall("glFramebufferTexture2D()");
186 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
187 checkGLcall("glFramebufferTexture2D()");
191 /* Context activation is done by the caller. */
192 static void context_attach_surface_fbo(struct wined3d_context
*context
,
193 GLenum fbo_target
, DWORD idx
, struct wined3d_surface
*surface
, DWORD location
)
195 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
197 TRACE("Attach surface %p to %u\n", surface
, idx
);
199 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
205 case WINED3D_LOCATION_TEXTURE_RGB
:
206 case WINED3D_LOCATION_TEXTURE_SRGB
:
207 srgb
= location
== WINED3D_LOCATION_TEXTURE_SRGB
;
208 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
209 surface
->texture_target
, surface_get_texture_name(surface
, context
, srgb
),
210 surface
->texture_level
);
211 checkGLcall("glFramebufferTexture2D()");
214 case WINED3D_LOCATION_RB_MULTISAMPLE
:
215 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
216 GL_RENDERBUFFER
, surface
->rb_multisample
);
217 checkGLcall("glFramebufferRenderbuffer()");
220 case WINED3D_LOCATION_RB_RESOLVED
:
221 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
222 GL_RENDERBUFFER
, surface
->rb_resolved
);
223 checkGLcall("glFramebufferRenderbuffer()");
227 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
233 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
234 checkGLcall("glFramebufferTexture2D()");
238 /* Context activation is done by the caller. */
239 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
241 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
244 if (!FIXME_ON(d3d
)) return;
246 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
247 if (status
== GL_FRAMEBUFFER_COMPLETE
)
249 TRACE("FBO complete\n");
253 const struct wined3d_surface
*attachment
;
256 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
258 if (!context
->current_fbo
)
260 ERR("FBO 0 is incomplete, driver bug?\n");
264 FIXME("\tColor Location %s (%#x).\n", wined3d_debug_location(context
->current_fbo
->color_location
),
265 context
->current_fbo
->color_location
);
266 FIXME("\tDepth Stencil Location %s (%#x).\n", wined3d_debug_location(context
->current_fbo
->ds_location
),
267 context
->current_fbo
->ds_location
);
269 /* Dump the FBO attachments */
270 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
272 attachment
= context
->current_fbo
->render_targets
[i
];
275 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
276 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
277 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
280 attachment
= context
->current_fbo
->depth_stencil
;
283 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
284 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
285 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
290 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
292 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
293 return buffer
? (1u << 31) | buffer
: 0;
296 static inline DWORD
context_generate_rt_mask_from_surface(const struct wined3d_surface
*target
)
298 return (1u << 31) | surface_get_gl_buffer(target
);
301 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
302 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
303 DWORD color_location
, DWORD ds_location
)
305 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
306 struct fbo_entry
*entry
;
308 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
309 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
310 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
311 entry
->depth_stencil
= depth_stencil
;
312 entry
->color_location
= color_location
;
313 entry
->ds_location
= ds_location
;
314 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
315 entry
->attached
= FALSE
;
316 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
317 checkGLcall("glGenFramebuffers()");
318 TRACE("Created FBO %u.\n", entry
->id
);
323 /* Context activation is done by the caller. */
324 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
325 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
326 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
328 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
330 context_bind_fbo(context
, target
, entry
->id
);
331 context_clean_fbo_attachments(gl_info
, target
);
333 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
334 entry
->depth_stencil
= depth_stencil
;
335 entry
->color_location
= color_location
;
336 entry
->ds_location
= ds_location
;
337 entry
->attached
= FALSE
;
340 /* Context activation is done by the caller. */
341 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
345 TRACE("Destroy FBO %u.\n", entry
->id
);
346 context_destroy_fbo(context
, entry
->id
);
348 --context
->fbo_entry_count
;
349 list_remove(&entry
->entry
);
350 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
351 HeapFree(GetProcessHeap(), 0, entry
);
354 /* Context activation is done by the caller. */
355 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
356 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
357 DWORD color_location
, DWORD ds_location
)
359 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
360 struct fbo_entry
*entry
;
362 if (depth_stencil
&& render_targets
&& render_targets
[0])
364 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
365 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
367 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
368 depth_stencil
= NULL
;
372 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
374 if (!memcmp(entry
->render_targets
,
375 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
376 && entry
->depth_stencil
== depth_stencil
&& entry
->color_location
== color_location
377 && entry
->ds_location
== ds_location
)
379 list_remove(&entry
->entry
);
380 list_add_head(&context
->fbo_list
, &entry
->entry
);
385 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
387 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, color_location
, ds_location
);
388 list_add_head(&context
->fbo_list
, &entry
->entry
);
389 ++context
->fbo_entry_count
;
393 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
394 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, color_location
, ds_location
, entry
);
395 list_remove(&entry
->entry
);
396 list_add_head(&context
->fbo_list
, &entry
->entry
);
402 /* Context activation is done by the caller. */
403 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
405 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
407 GLuint read_binding
, draw_binding
;
408 struct wined3d_surface
*depth_stencil
= entry
->depth_stencil
;
412 context_bind_fbo(context
, target
, entry
->id
);
416 read_binding
= context
->fbo_read_binding
;
417 draw_binding
= context
->fbo_draw_binding
;
418 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
420 /* Apply render targets */
421 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
423 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->color_location
);
426 if (depth_stencil
&& entry
->render_targets
[0]
427 && (depth_stencil
->resource
.multisample_type
428 != entry
->render_targets
[0]->resource
.multisample_type
429 || depth_stencil
->resource
.multisample_quality
430 != entry
->render_targets
[0]->resource
.multisample_quality
))
432 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
433 entry
->render_targets
[0]->resource
.multisample_quality
,
434 entry
->render_targets
[0]->resource
.multisample_type
,
435 depth_stencil
->resource
.multisample_quality
, depth_stencil
->resource
.multisample_type
);
436 depth_stencil
= NULL
;
440 surface_set_compatible_renderbuffer(depth_stencil
, entry
->render_targets
[0]);
441 context_attach_depth_stencil_fbo(context
, target
, depth_stencil
, entry
->ds_location
);
443 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
444 * GL contexts requirements. */
445 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
446 context_set_draw_buffer(context
, GL_NONE
);
447 if (target
!= GL_FRAMEBUFFER
)
449 if (target
== GL_READ_FRAMEBUFFER
)
450 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
452 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
455 entry
->attached
= TRUE
;
458 /* Context activation is done by the caller. */
459 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
460 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
461 DWORD color_location
, DWORD ds_location
)
463 struct fbo_entry
*entry
, *entry2
;
465 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
467 context_destroy_fbo_entry(context
, entry
);
470 if (context
->rebind_fbo
)
472 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
473 context
->rebind_fbo
= FALSE
;
476 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
478 context
->current_fbo
= NULL
;
479 context_bind_fbo(context
, target
, 0);
483 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
,
484 color_location
, ds_location
);
485 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
489 /* Context activation is done by the caller. */
490 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
491 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
493 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
495 context
->blit_targets
[0] = render_target
;
497 memset(&context
->blit_targets
[1], 0, clear_size
);
498 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
, location
);
501 /* Context activation is done by the caller. */
502 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
504 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
506 if (context
->free_occlusion_query_count
)
508 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
512 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
514 GL_EXTCALL(glGenQueries(1, &query
->id
));
515 checkGLcall("glGenQueries");
517 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
521 WARN("Occlusion queries not supported, not allocating query id.\n");
526 query
->context
= context
;
527 list_add_head(&context
->occlusion_queries
, &query
->entry
);
530 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
532 struct wined3d_context
*context
= query
->context
;
534 list_remove(&query
->entry
);
535 query
->context
= NULL
;
537 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
539 UINT new_size
= context
->free_occlusion_query_size
<< 1;
540 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
541 new_size
* sizeof(*context
->free_occlusion_queries
));
545 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
549 context
->free_occlusion_query_size
= new_size
;
550 context
->free_occlusion_queries
= new_data
;
553 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
556 /* Context activation is done by the caller. */
557 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
559 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
561 if (context
->free_event_query_count
)
563 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
567 if (gl_info
->supported
[ARB_SYNC
])
569 /* Using ARB_sync, not much to do here. */
570 query
->object
.sync
= NULL
;
571 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
573 else if (gl_info
->supported
[APPLE_FENCE
])
575 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
576 checkGLcall("glGenFencesAPPLE");
578 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
580 else if(gl_info
->supported
[NV_FENCE
])
582 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
583 checkGLcall("glGenFencesNV");
585 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
589 WARN("Event queries not supported, not allocating query id.\n");
590 query
->object
.id
= 0;
594 query
->context
= context
;
595 list_add_head(&context
->event_queries
, &query
->entry
);
598 void context_free_event_query(struct wined3d_event_query
*query
)
600 struct wined3d_context
*context
= query
->context
;
602 list_remove(&query
->entry
);
603 query
->context
= NULL
;
605 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
607 UINT new_size
= context
->free_event_query_size
<< 1;
608 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
609 new_size
* sizeof(*context
->free_event_queries
));
613 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
617 context
->free_event_query_size
= new_size
;
618 context
->free_event_queries
= new_data
;
621 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
624 /* Context activation is done by the caller. */
625 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
627 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
629 if (context
->free_timestamp_query_count
)
631 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
635 GL_EXTCALL(glGenQueries(1, &query
->id
));
636 checkGLcall("glGenQueries");
638 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
641 query
->context
= context
;
642 list_add_head(&context
->timestamp_queries
, &query
->entry
);
645 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
647 struct wined3d_context
*context
= query
->context
;
649 list_remove(&query
->entry
);
650 query
->context
= NULL
;
652 if (context
->free_timestamp_query_count
>= context
->free_timestamp_query_size
- 1)
654 UINT new_size
= context
->free_timestamp_query_size
<< 1;
655 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_timestamp_queries
,
656 new_size
* sizeof(*context
->free_timestamp_queries
));
660 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
664 context
->free_timestamp_query_size
= new_size
;
665 context
->free_timestamp_queries
= new_data
;
668 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
671 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
673 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
674 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
678 for (i
= 0; i
< device
->context_count
; ++i
)
680 struct wined3d_context
*context
= device
->contexts
[i
];
681 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
682 struct fbo_entry
*entry
, *entry2
;
684 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
686 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
690 if (entry
->depth_stencil
== surface
)
692 callback(context
, entry
);
696 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
698 if (entry
->render_targets
[j
] == surface
)
700 callback(context
, entry
);
708 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
710 list_remove(&entry
->entry
);
711 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
714 void context_resource_released(const struct wined3d_device
*device
,
715 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
717 if (!device
->d3d_initialized
) return;
721 case WINED3D_RTYPE_SURFACE
:
722 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
723 context_queue_fbo_entry_destruction
);
731 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
733 entry
->attached
= FALSE
;
736 void context_resource_unloaded(const struct wined3d_device
*device
,
737 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
741 case WINED3D_RTYPE_SURFACE
:
742 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
743 context_detach_fbo_entry
);
751 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
753 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
754 struct fbo_entry
*entry
= context
->current_fbo
;
757 if (!entry
|| context
->rebind_fbo
) return;
759 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
761 if (surface
== entry
->render_targets
[i
])
763 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
764 context
->rebind_fbo
= TRUE
;
769 if (surface
== entry
->depth_stencil
)
771 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
772 context
->rebind_fbo
= TRUE
;
776 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
778 int current
= GetPixelFormat(dc
);
780 if (current
== format
) return TRUE
;
784 if (!SetPixelFormat(dc
, format
, NULL
))
786 /* This may also happen if the dc belongs to a destroyed window. */
787 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
788 format
, dc
, GetLastError());
794 /* By default WGL doesn't allow pixel format adjustments but we need it
795 * here. For this reason there's a Wine specific wglSetPixelFormat()
796 * which allows us to set the pixel format multiple times. Only use it
797 * when really needed. */
798 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
800 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
802 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
809 /* OpenGL doesn't allow pixel format adjustments. Print an error and
810 * continue using the old format. There's a big chance that the old
811 * format works although with a performance hit and perhaps rendering
813 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
814 format
, dc
, current
);
818 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
820 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
823 if (!context_set_pixel_format(ctx
->gl_info
, ctx
->hdc
, ctx
->pixel_format
))
825 WARN("Failed to set pixel format %d on device context %p.\n",
826 ctx
->pixel_format
, ctx
->hdc
);
830 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
834 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
835 ctx
->glCtx
, ctx
->hdc
, GetLastError());
837 WARN("Trying fallback to the backup window.\n");
839 /* FIXME: If the context is destroyed it's no longer associated with
840 * a swapchain, so we can't use the swapchain to get a backup dc. To
841 * make this work windowless contexts would need to be handled by the
845 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
846 context_set_current(NULL
);
850 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
852 context_set_current(NULL
);
856 if (!context_set_pixel_format(ctx
->gl_info
, dc
, ctx
->pixel_format
))
858 ERR("Failed to set pixel format %d on device context %p.\n",
859 ctx
->pixel_format
, dc
);
860 context_set_current(NULL
);
864 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
866 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
868 context_set_current(NULL
);
878 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
, int pf
)
880 if (!context_set_pixel_format(gl_info
, dc
, pf
))
882 ERR("Failed to restore pixel format %d on device context %p.\n", pf
, dc
);
883 context_set_current(NULL
);
887 if (!wglMakeCurrent(dc
, gl_ctx
))
889 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
890 gl_ctx
, dc
, GetLastError());
891 context_set_current(NULL
);
895 static void context_update_window(struct wined3d_context
*context
)
897 if (context
->win_handle
== context
->swapchain
->win_handle
)
900 TRACE("Updating context %p window from %p to %p.\n",
901 context
, context
->win_handle
, context
->swapchain
->win_handle
);
904 wined3d_release_dc(context
->win_handle
, context
->hdc
);
906 context
->win_handle
= context
->swapchain
->win_handle
;
907 context
->needs_set
= 1;
910 if (!(context
->hdc
= GetDCEx(context
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
912 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
917 static void context_destroy_gl_resources(struct wined3d_context
*context
)
919 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
920 struct wined3d_timestamp_query
*timestamp_query
;
921 struct wined3d_occlusion_query
*occlusion_query
;
922 struct wined3d_event_query
*event_query
;
923 struct fbo_entry
*entry
, *entry2
;
929 restore_ctx
= wglGetCurrentContext();
930 restore_dc
= wglGetCurrentDC();
931 restore_pf
= GetPixelFormat(restore_dc
);
933 if (restore_ctx
== context
->glCtx
)
935 else if (context
->valid
)
936 context_set_gl_context(context
);
938 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
941 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
942 timestamp_query
->context
= NULL
;
945 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
947 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
948 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
949 occlusion_query
->context
= NULL
;
952 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
956 if (gl_info
->supported
[ARB_SYNC
])
958 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
960 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
961 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
963 event_query
->context
= NULL
;
966 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
968 if (!context
->valid
) entry
->id
= 0;
969 context_destroy_fbo_entry(context
, entry
);
972 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
974 if (!context
->valid
) entry
->id
= 0;
975 context_destroy_fbo_entry(context
, entry
);
980 if (context
->dummy_arbfp_prog
)
982 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
985 if (gl_info
->supported
[ARB_TIMER_QUERY
])
986 GL_EXTCALL(glDeleteQueries(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
988 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
989 GL_EXTCALL(glDeleteQueries(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
991 if (gl_info
->supported
[ARB_SYNC
])
993 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
995 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
998 else if (gl_info
->supported
[APPLE_FENCE
])
1000 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1002 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1005 else if (gl_info
->supported
[NV_FENCE
])
1007 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1009 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1013 checkGLcall("context cleanup");
1016 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1017 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1018 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1022 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
, restore_pf
);
1024 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1026 ERR("Failed to disable GL context.\n");
1029 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1031 if (!wglDeleteContext(context
->glCtx
))
1033 DWORD err
= GetLastError();
1034 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1038 DWORD
context_get_tls_idx(void)
1040 return wined3d_context_tls_idx
;
1043 void context_set_tls_idx(DWORD idx
)
1045 wined3d_context_tls_idx
= idx
;
1048 struct wined3d_context
*context_get_current(void)
1050 return TlsGetValue(wined3d_context_tls_idx
);
1053 BOOL
context_set_current(struct wined3d_context
*ctx
)
1055 struct wined3d_context
*old
= context_get_current();
1059 TRACE("Already using D3D context %p.\n", ctx
);
1067 TRACE("Switching away from destroyed context %p.\n", old
);
1068 context_destroy_gl_resources(old
);
1069 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1070 HeapFree(GetProcessHeap(), 0, old
);
1074 if (wglGetCurrentContext())
1076 TRACE("Flushing context %p before switching to %p.\n", old
, ctx
);
1087 ERR("Trying to make invalid context %p current\n", ctx
);
1091 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1092 if (!context_set_gl_context(ctx
))
1096 else if(wglGetCurrentContext())
1098 TRACE("Clearing current D3D context.\n");
1099 if (!wglMakeCurrent(NULL
, NULL
))
1101 DWORD err
= GetLastError();
1102 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1103 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1108 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1111 void context_release(struct wined3d_context
*context
)
1113 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1117 if (!context
->level
)
1118 WARN("Context %p is not active.\n", context
);
1119 else if (context
!= context_get_current())
1120 WARN("Context %p is not the current context.\n", context
);
1123 if (!--context
->level
&& context
->restore_ctx
)
1125 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1126 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
, context
->restore_pf
);
1127 context
->restore_ctx
= NULL
;
1128 context
->restore_dc
= NULL
;
1132 /* This is used when a context for render target A is active, but a separate context is
1133 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1134 * A to avoid breaking caller code. */
1135 void context_restore(struct wined3d_context
*context
, struct wined3d_surface
*restore
)
1137 if (context
->current_rt
!= restore
)
1139 context_release(context
);
1140 context
= context_acquire(restore
->resource
.device
, restore
);
1143 context_release(context
);
1146 static void context_enter(struct wined3d_context
*context
)
1148 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1150 if (!context
->level
++)
1152 const struct wined3d_context
*current_context
= context_get_current();
1153 HGLRC current_gl
= wglGetCurrentContext();
1155 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1157 TRACE("Another GL context (%p on device context %p) is already current.\n",
1158 current_gl
, wglGetCurrentDC());
1159 context
->restore_ctx
= current_gl
;
1160 context
->restore_dc
= wglGetCurrentDC();
1161 context
->restore_pf
= GetPixelFormat(context
->restore_dc
);
1162 context
->needs_set
= 1;
1167 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1169 DWORD rep
= context
->state_table
[state
].representative
;
1173 if (isStateDirty(context
, rep
)) return;
1175 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1176 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1177 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1178 context
->isStateDirty
[idx
] |= (1u << shift
);
1181 /* This function takes care of wined3d pixel format selection. */
1182 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1183 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1184 BOOL auxBuffers
, BOOL findCompatible
)
1187 unsigned int current_value
;
1188 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1191 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1192 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1193 auxBuffers
, findCompatible
);
1196 for (i
= 0; i
< cfg_count
; ++i
)
1198 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1201 /* For now only accept RGBA formats. Perhaps some day we will
1202 * allow floating point formats for pbuffers. */
1203 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1205 /* In window mode we need a window drawable format and double buffering. */
1206 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1208 if (cfg
->redSize
< color_format
->red_size
)
1210 if (cfg
->greenSize
< color_format
->green_size
)
1212 if (cfg
->blueSize
< color_format
->blue_size
)
1214 if (cfg
->alphaSize
< color_format
->alpha_size
)
1216 if (cfg
->depthSize
< ds_format
->depth_size
)
1218 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1220 /* Check multisampling support. */
1221 if (cfg
->numSamples
)
1225 /* We try to locate a format which matches our requirements exactly. In case of
1226 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1227 if (cfg
->depthSize
== ds_format
->depth_size
)
1229 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1231 if (cfg
->alphaSize
== color_format
->alpha_size
)
1233 /* We like to have aux buffers in backbuffer mode */
1234 if (auxBuffers
&& cfg
->auxBuffers
)
1236 if (cfg
->redSize
== color_format
->red_size
1237 && cfg
->greenSize
== color_format
->green_size
1238 && cfg
->blueSize
== color_format
->blue_size
)
1241 if (value
> current_value
)
1243 iPixelFormat
= cfg
->iPixelFormat
;
1244 current_value
= value
;
1248 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1249 if(!iPixelFormat
&& !findCompatible
) {
1250 ERR("Can't find a suitable iPixelFormat\n");
1252 } else if(!iPixelFormat
) {
1253 PIXELFORMATDESCRIPTOR pfd
;
1255 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1256 /* PixelFormat selection */
1257 ZeroMemory(&pfd
, sizeof(pfd
));
1258 pfd
.nSize
= sizeof(pfd
);
1260 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1261 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1262 pfd
.cAlphaBits
= color_format
->alpha_size
;
1263 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1264 + color_format
->blue_size
+ color_format
->alpha_size
;
1265 pfd
.cDepthBits
= ds_format
->depth_size
;
1266 pfd
.cStencilBits
= ds_format
->stencil_size
;
1267 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1269 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1271 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1272 ERR("Can't find a suitable iPixelFormat\n");
1277 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1278 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1279 return iPixelFormat
;
1282 /* Context activation is done by the caller. */
1283 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1285 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1286 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1288 for (i
= 0; i
< count
; ++i
)
1290 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1291 checkGLcall("glActiveTexture");
1293 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1294 checkGLcall("glBindTexture");
1296 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1298 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1299 checkGLcall("glBindTexture");
1302 if (gl_info
->supported
[EXT_TEXTURE3D
])
1304 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1305 checkGLcall("glBindTexture");
1308 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1310 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1311 checkGLcall("glBindTexture");
1316 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1318 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1319 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1322 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1323 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1327 case GL_DEBUG_TYPE_ERROR_ARB
:
1328 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1331 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1332 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1333 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1334 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1337 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1338 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1342 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1347 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1350 unsigned int ctx_attrib_idx
= 0;
1351 GLint ctx_attribs
[7], ctx_flags
= 0;
1353 if (context_debug_output_enabled(gl_info
))
1354 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1355 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1356 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1357 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1358 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1359 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1360 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1363 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1364 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1366 ctx_attribs
[ctx_attrib_idx
] = 0;
1368 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1370 if (ctx_flags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
1372 ctx_attribs
[ctx_attrib_idx
- 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1373 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1374 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1381 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1382 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1384 struct wined3d_device
*device
= swapchain
->device
;
1385 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1386 const struct wined3d_format
*color_format
;
1387 struct wined3d_context
*ret
;
1388 BOOL auxBuffers
= FALSE
;
1389 HGLRC ctx
, share_ctx
;
1396 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1398 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1402 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1403 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1404 if (!ret
->blit_targets
)
1407 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1408 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1409 if (!ret
->draw_buffers
)
1412 ret
->free_timestamp_query_size
= 4;
1413 ret
->free_timestamp_queries
= HeapAlloc(GetProcessHeap(), 0,
1414 ret
->free_timestamp_query_size
* sizeof(*ret
->free_timestamp_queries
));
1415 if (!ret
->free_timestamp_queries
)
1417 list_init(&ret
->timestamp_queries
);
1419 ret
->free_occlusion_query_size
= 4;
1420 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1421 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1422 if (!ret
->free_occlusion_queries
)
1425 list_init(&ret
->occlusion_queries
);
1427 ret
->free_event_query_size
= 4;
1428 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1429 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1430 if (!ret
->free_event_queries
)
1433 list_init(&ret
->event_queries
);
1434 list_init(&ret
->fbo_list
);
1435 list_init(&ret
->fbo_destroy_list
);
1437 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1439 ERR("Failed to allocate shader backend context data.\n");
1442 if (!device
->adapter
->fragment_pipe
->allocate_context_data(ret
))
1444 ERR("Failed to allocate fragment pipeline context data.\n");
1448 #if defined(STAGING_CSMT)
1449 ret
->current_fb
.render_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1450 sizeof(*ret
->current_fb
.render_targets
) * gl_info
->limits
.buffers
);
1451 ret
->current_fb
.rt_size
= gl_info
->limits
.buffers
;
1452 if (!ret
->current_fb
.render_targets
)
1454 if (device
->context_count
)
1455 ret
->offscreenBuffer
= device
->contexts
[0]->offscreenBuffer
;
1457 #endif /* STAGING_CSMT */
1458 /* Initialize the texture unit mapping to a 1:1 mapping */
1459 for (s
= 0; s
< MAX_COMBINED_SAMPLERS
; ++s
)
1461 if (s
< gl_info
->limits
.combined_samplers
)
1463 ret
->tex_unit_map
[s
] = s
;
1464 ret
->rev_tex_unit_map
[s
] = s
;
1468 ret
->tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1469 ret
->rev_tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1473 if (!(hdc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1475 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1477 if (!(hdc
= swapchain_get_backup_dc(swapchain
)))
1479 ERR("Failed to retrieve a device context.\n");
1484 color_format
= target
->resource
.format
;
1486 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1487 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1488 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1492 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1493 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1494 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1495 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1498 /* DirectDraw supports 8bit paletted render targets and these are used by
1499 * old games like StarCraft and C&C. Most modern hardware doesn't support
1500 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1501 * conversion (ab)uses the alpha component for storing the palette index.
1502 * For this reason we require a format with 8bit alpha, so request
1504 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1505 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1507 /* When "always_offscreen" is enabled, we only use the drawable for
1508 * presentation blits, and don't do any rendering to it. That means we
1509 * don't need depth or stencil buffers, and can mostly ignore the render
1510 * target format. This wouldn't necessarily be quite correct for 10bpc
1511 * display modes, but we don't currently support those.
1512 * Using the same format regardless of the color/depth/stencil targets
1513 * makes it much less likely that different wined3d instances will set
1514 * conflicting pixel formats. */
1515 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
1516 && wined3d_settings
.always_offscreen
)
1518 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1519 ds_format
= wined3d_get_format(gl_info
, WINED3DFMT_UNKNOWN
);
1522 /* Try to find a pixel format which matches our requirements. */
1523 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1525 /* Try to locate a compatible format if we weren't able to find anything. */
1528 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1529 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1532 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1535 ERR("Can't find a suitable pixel format.\n");
1541 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1543 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1544 context_release(ret
);
1548 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1549 if (gl_info
->p_wglCreateContextAttribsARB
)
1551 if (!(ctx
= context_create_wgl_attribs(gl_info
, hdc
, share_ctx
)))
1556 if (!(ctx
= wglCreateContext(hdc
)))
1558 ERR("Failed to create a WGL context.\n");
1559 context_release(ret
);
1563 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1565 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1566 context_release(ret
);
1567 if (!wglDeleteContext(ctx
))
1568 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1573 if (!device_context_add(device
, ret
))
1575 ERR("Failed to add the newly created context to the context list\n");
1576 context_release(ret
);
1577 if (!wglDeleteContext(ctx
))
1578 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1582 ret
->gl_info
= gl_info
;
1583 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1584 ret
->state_table
= device
->StateTable
;
1586 /* Mark all states dirty to force a proper initialization of the states
1587 * on the first use of the context. */
1588 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1590 if (ret
->state_table
[state
].representative
)
1591 context_invalidate_state(ret
, state
);
1594 ret
->swapchain
= swapchain
;
1595 ret
->current_rt
= target
;
1596 ret
->tid
= GetCurrentThreadId();
1598 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
1599 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1603 ret
->win_handle
= swapchain
->win_handle
;
1605 ret
->pixel_format
= pixel_format
;
1608 /* Set up the context defaults */
1609 if (!context_set_current(ret
))
1611 ERR("Cannot activate context to set up defaults.\n");
1612 device_context_remove(device
, ret
);
1613 context_release(ret
);
1614 if (!wglDeleteContext(ctx
))
1615 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1619 if (context_debug_output_enabled(gl_info
))
1621 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, ret
));
1622 if (TRACE_ON(d3d_synchronous
))
1623 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
1624 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1627 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
1628 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1632 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
1633 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1634 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
1635 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1636 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
1637 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1639 if (WARN_ON(d3d_perf
))
1641 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
1642 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1646 switch (swapchain
->desc
.swap_interval
)
1648 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1651 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1652 case WINED3DPRESENT_INTERVAL_ONE
:
1655 case WINED3DPRESENT_INTERVAL_TWO
:
1658 case WINED3DPRESENT_INTERVAL_THREE
:
1661 case WINED3DPRESENT_INTERVAL_FOUR
:
1665 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1669 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1671 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1672 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1673 swap_interval
, ret
, GetLastError());
1676 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1678 TRACE("Setting up the screen\n");
1680 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1681 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1683 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1684 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1686 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1687 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1689 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1690 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1691 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
1692 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1694 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
1698 GL_EXTCALL(glGenVertexArrays(1, &vao
));
1699 GL_EXTCALL(glBindVertexArray(vao
));
1700 checkGLcall("creating VAO");
1703 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1705 /* Direct3D always uses n-1 weights for n world matrices and uses
1706 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1707 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1708 * enabled as well. */
1709 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1710 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1712 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1714 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1715 * the previous texture where to source the offset from is always unit - 1.
1717 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1719 context_active_texture(ret
, gl_info
, s
);
1720 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1721 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1722 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1725 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1727 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1728 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1729 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1730 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1733 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1734 * program and the dummy program is destroyed when the context is destroyed.
1736 static const char dummy_program
[] =
1738 "MOV result.color, fragment.color.primary;\n"
1740 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1741 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1742 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1745 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1747 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1749 context_active_texture(ret
, gl_info
, s
);
1750 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1751 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1755 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1757 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1759 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1761 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1763 device
->shader_backend
->shader_init_context_state(ret
);
1764 ret
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
1765 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
1766 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
);
1768 /* If this happens to be the first context for the device, dummy textures
1769 * are not created yet. In that case, they will be created (and bound) by
1770 * create_dummy_textures right after this context is initialized. */
1771 if (device
->dummy_texture_2d
[0])
1772 bind_dummy_textures(device
, ret
);
1774 TRACE("Created context %p.\n", ret
);
1779 if (hdc
) wined3d_release_dc(swapchain
->win_handle
, hdc
);
1780 device
->shader_backend
->shader_free_context_data(ret
);
1781 device
->adapter
->fragment_pipe
->free_context_data(ret
);
1782 #if defined(STAGING_CSMT)
1783 HeapFree(GetProcessHeap(), 0, ret
->current_fb
.render_targets
);
1784 #endif /* STAGING_CSMT */
1785 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1786 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1787 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
1788 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1789 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1790 HeapFree(GetProcessHeap(), 0, ret
);
1794 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1798 TRACE("Destroying ctx %p\n", context
);
1800 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1802 context_destroy_gl_resources(context
);
1803 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1808 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1809 in wined3d_adapter may go away in the meantime */
1810 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
1811 *gl_info
= *context
->gl_info
;
1812 context
->gl_info
= gl_info
;
1813 context
->destroyed
= 1;
1817 device
->shader_backend
->shader_free_context_data(context
);
1818 device
->adapter
->fragment_pipe
->free_context_data(context
);
1819 #if defined(STAGING_CSMT)
1820 HeapFree(GetProcessHeap(), 0, context
->current_fb
.render_targets
);
1821 #endif /* STAGING_CSMT */
1822 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1823 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1824 device_context_remove(device
, context
);
1825 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1828 /* Context activation is done by the caller. */
1829 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
1831 const GLdouble projection
[] =
1833 2.0 / width
, 0.0, 0.0, 0.0,
1834 0.0, 2.0 / height
, 0.0, 0.0,
1836 -1.0, -1.0, -1.0, 1.0,
1839 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
1840 checkGLcall("glMatrixMode(GL_PROJECTION)");
1841 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
1842 checkGLcall("glLoadMatrixd");
1843 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
1844 checkGLcall("glViewport");
1847 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
1849 const struct wined3d_surface
*rt
= context
->current_rt
;
1851 if (rt
->container
->swapchain
&& rt
->container
->swapchain
->front_buffer
== rt
->container
)
1855 GetClientRect(context
->win_handle
, &window_size
);
1856 size
->cx
= window_size
.right
- window_size
.left
;
1857 size
->cy
= window_size
.bottom
- window_size
.top
;
1862 size
->cx
= rt
->resource
.width
;
1863 size
->cy
= rt
->resource
.height
;
1866 /*****************************************************************************
1869 * Sets up a context for DirectDraw blitting.
1870 * All texture units are disabled, texture unit 0 is set as current unit
1871 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1872 * color writing enabled for all channels
1873 * register combiners disabled, shaders disabled
1874 * world matrix is set to identity, texture matrix 0 too
1875 * projection matrix is setup for drawing screen coordinates
1878 * This: Device to activate the context for
1879 * context: Context to setup
1881 *****************************************************************************/
1882 /* Context activation is done by the caller. */
1883 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
1886 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1890 TRACE("Setting up context %p for blitting\n", context
);
1892 context_get_rt_size(context
, &rt_size
);
1894 if (context
->last_was_blit
)
1896 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
1898 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1899 context
->blit_w
= rt_size
.cx
;
1900 context
->blit_h
= rt_size
.cy
;
1901 /* No need to dirtify here, the states are still dirtified because
1902 * they weren't applied since the last SetupForBlit() call. */
1904 TRACE("Context is already set up for blitting, nothing to do\n");
1907 context
->last_was_blit
= TRUE
;
1909 /* Disable all textures. The caller can then bind a texture it wants to blit
1912 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1913 * function texture unit. No need to care for higher samplers
1915 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1917 sampler
= context
->rev_tex_unit_map
[i
];
1918 context_active_texture(context
, gl_info
, i
);
1920 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1922 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1923 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1925 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1926 checkGLcall("glDisable GL_TEXTURE_3D");
1927 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1929 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1930 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1932 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1933 checkGLcall("glDisable GL_TEXTURE_2D");
1935 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1936 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1938 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1940 if (sampler
< MAX_TEXTURES
)
1941 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1942 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1945 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
1946 GL_EXTCALL(glBindSampler(0, 0));
1947 context_active_texture(context
, gl_info
, 0);
1949 sampler
= context
->rev_tex_unit_map
[0];
1951 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1953 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1954 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1956 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1957 checkGLcall("glDisable GL_TEXTURE_3D");
1958 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1960 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1961 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1963 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1964 checkGLcall("glDisable GL_TEXTURE_2D");
1966 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1968 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
1969 checkGLcall("glMatrixMode(GL_TEXTURE)");
1970 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1971 checkGLcall("glLoadIdentity()");
1973 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1975 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1976 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
1977 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1980 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1982 if (sampler
< MAX_TEXTURES
)
1984 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
1985 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1987 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1990 /* Other misc states */
1991 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
1992 checkGLcall("glDisable(GL_ALPHA_TEST)");
1993 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
1994 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
1995 checkGLcall("glDisable GL_LIGHTING");
1996 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
1997 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
1998 checkGLcall("glDisable GL_DEPTH_TEST");
1999 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
2000 glDisableWINE(GL_FOG
);
2001 checkGLcall("glDisable GL_FOG");
2002 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
2003 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2004 checkGLcall("glDisable GL_BLEND");
2005 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2006 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
2007 checkGLcall("glDisable GL_CULL_FACE");
2008 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
2009 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
2010 checkGLcall("glDisable GL_STENCIL_TEST");
2011 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
2012 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
2013 checkGLcall("glDisable GL_SCISSOR_TEST");
2014 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2015 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2017 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
2018 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2019 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
2021 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
2022 checkGLcall("glColorMask");
2023 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
2024 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
2025 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
2026 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2027 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2029 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2030 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2031 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2034 /* Setup transforms */
2035 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2036 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2037 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2038 checkGLcall("glLoadIdentity()");
2039 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2041 context
->last_was_rhw
= TRUE
;
2042 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2044 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2045 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2046 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2047 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2048 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2049 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2050 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2052 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2054 /* Disable shaders */
2055 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2057 context
->blit_w
= rt_size
.cx
;
2058 context
->blit_h
= rt_size
.cy
;
2059 context_invalidate_state(context
, STATE_VIEWPORT
);
2060 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2063 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2065 return rt_mask
& (1u << 31);
2068 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2070 return rt_mask
& ~(1u << 31);
2073 /* Context activation is done by the caller. */
2074 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2076 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2080 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2081 checkGLcall("glDrawBuffer()");
2083 else if (is_rt_mask_onscreen(rt_mask
))
2085 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2086 checkGLcall("glDrawBuffer()");
2090 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2097 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2099 context
->draw_buffers
[i
] = GL_NONE
;
2105 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2107 GL_EXTCALL(glDrawBuffers(i
, context
->draw_buffers
));
2108 checkGLcall("glDrawBuffers()");
2112 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2113 checkGLcall("glDrawBuffer()");
2118 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2123 /* Context activation is done by the caller. */
2124 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2126 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2127 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2128 DWORD new_mask
= context_generate_rt_mask(buffer
);
2130 if (new_mask
== *current_mask
)
2133 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2134 checkGLcall("glDrawBuffer()");
2136 *current_mask
= new_mask
;
2139 /* Context activation is done by the caller. */
2140 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2142 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2143 checkGLcall("glActiveTexture");
2144 context
->active_texture
= unit
;
2147 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2149 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2150 DWORD unit
= context
->active_texture
;
2151 DWORD old_texture_type
= context
->texture_type
[unit
];
2155 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2156 checkGLcall("glBindTexture");
2163 if (old_texture_type
!= target
)
2165 const struct wined3d_device
*device
= context
->swapchain
->device
;
2167 switch (old_texture_type
)
2173 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2174 checkGLcall("glBindTexture");
2176 case GL_TEXTURE_RECTANGLE_ARB
:
2177 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2178 checkGLcall("glBindTexture");
2180 case GL_TEXTURE_CUBE_MAP
:
2181 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2182 checkGLcall("glBindTexture");
2185 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2186 checkGLcall("glBindTexture");
2189 ERR("Unexpected texture target %#x\n", old_texture_type
);
2192 context
->texture_type
[unit
] = target
;
2196 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2198 if (context
->render_offscreen
== offscreen
) return;
2200 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2201 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2202 context_invalidate_state(context
, STATE_VIEWPORT
);
2203 context_invalidate_state(context
, STATE_SCISSORRECT
);
2204 context_invalidate_state(context
, STATE_FRONTFACE
);
2205 context
->render_offscreen
= offscreen
;
2208 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2209 const struct wined3d_format
*required
)
2211 if (existing
== required
)
2213 if ((existing
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
)
2214 != (required
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
))
2216 if (existing
->depth_size
< required
->depth_size
)
2218 /* If stencil bits are used the exact amount is required - otherwise
2219 * wrapping won't work correctly. */
2220 if (required
->stencil_size
&& required
->stencil_size
!= existing
->stencil_size
)
2225 /* Context activation is done by the caller. */
2226 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2227 const struct wined3d_rendertarget_view
*depth_stencil
)
2229 /* Onscreen surfaces are always in a swapchain */
2230 struct wined3d_swapchain
*swapchain
= context
->current_rt
->container
->swapchain
;
2232 if (context
->render_offscreen
|| !depth_stencil
) return;
2233 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2235 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2236 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2238 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2240 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2241 #if defined(STAGING_CSMT)
2242 wined3d_resource_load_location(&context
->current_rt
->resource
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
2243 swapchain
->render_to_fbo
= TRUE
;
2244 swapchain_update_draw_bindings(swapchain
);
2245 context_set_render_offscreen(context
, TRUE
);
2248 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_context
*context
, const struct wined3d_surface
*rt
)
2250 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2252 else if (rt
->container
->swapchain
)
2253 return context_generate_rt_mask_from_surface(rt
);
2255 return context_generate_rt_mask(context
->offscreenBuffer
);
2256 #else /* STAGING_CSMT */
2257 surface_load_location(context
->current_rt
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
2258 swapchain
->render_to_fbo
= TRUE
;
2259 swapchain_update_draw_bindings(swapchain
);
2260 context_set_render_offscreen(context
, TRUE
);
2263 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
2265 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2267 else if (rt
->container
->swapchain
)
2268 return context_generate_rt_mask_from_surface(rt
);
2270 return context_generate_rt_mask(device
->offscreenBuffer
);
2271 #endif /* STAGING_CSMT */
2274 /* Context activation is done by the caller. */
2275 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2277 struct wined3d_surface
*rt
= context
->current_rt
;
2278 DWORD rt_mask
, *cur_mask
;
2280 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2282 context_validate_onscreen_formats(context
, NULL
);
2284 if (context
->render_offscreen
)
2286 wined3d_texture_load(rt
->container
, context
, FALSE
);
2288 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->container
->resource
.draw_binding
);
2289 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2296 context
->current_fbo
= NULL
;
2297 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2298 rt_mask
= context_generate_rt_mask_from_surface(rt
);
2303 #if defined(STAGING_CSMT)
2304 rt_mask
= context_generate_rt_mask_no_fbo(context
, rt
);
2305 #else /* STAGING_CSMT */
2306 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt
);
2307 #endif /* STAGING_CSMT */
2310 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2312 if (rt_mask
!= *cur_mask
)
2314 context_apply_draw_buffers(context
, rt_mask
);
2315 *cur_mask
= rt_mask
;
2318 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2320 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2323 SetupForBlit(device
, context
);
2324 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2327 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2328 const struct wined3d_rendertarget_view
*ds
)
2332 if (ds
) return TRUE
;
2334 for (i
= 0; i
< rt_count
; ++i
)
2336 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2340 WARN("Invalid render target config, need at least one attachment.\n");
2344 /* Context activation is done by the caller. */
2345 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2346 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2348 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2349 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
2350 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2351 DWORD rt_mask
= 0, *cur_mask
;
2354 #if defined(STAGING_CSMT)
2355 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || !wined3d_fb_equal(fb
, &context
->current_fb
)
2356 #else /* STAGING_CSMT */
2357 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2358 #endif /* STAGING_CSMT */
2359 || rt_count
!= context
->gl_info
->limits
.buffers
)
2361 if (!context_validate_rt_config(rt_count
, rts
, dsv
))
2364 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2366 context_validate_onscreen_formats(context
, dsv
);
2368 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2370 for (i
= 0; i
< rt_count
; ++i
)
2372 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(rts
[i
]);
2373 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2374 rt_mask
|= (1u << i
);
2376 while (i
< context
->gl_info
->limits
.buffers
)
2378 context
->blit_targets
[i
] = NULL
;
2381 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2382 wined3d_rendertarget_view_get_surface(dsv
),
2383 rt_count
? rts
[0]->resource
->draw_binding
: 0,
2384 dsv
? dsv
->resource
->draw_binding
: 0);
2388 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2389 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2390 rt_mask
= context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2393 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2394 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2395 * state management allows this */
2396 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2400 #if defined(STAGING_CSMT)
2401 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2402 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2405 wined3d_fb_copy(&context
->current_fb
, fb
);
2406 #else /* STAGING_CSMT */
2407 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2408 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2410 #endif /* STAGING_CSMT */
2412 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2413 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2415 for (i
= 0; i
< rt_count
; ++i
)
2417 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2418 rt_mask
|= (1u << i
);
2423 #if defined(STAGING_CSMT)
2424 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2425 #else /* STAGING_CSMT */
2426 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2427 #endif /* STAGING_CSMT */
2428 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2431 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2433 if (rt_mask
!= *cur_mask
)
2435 context_apply_draw_buffers(context
, rt_mask
);
2436 *cur_mask
= rt_mask
;
2437 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2440 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2442 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2445 if (context
->last_was_blit
)
2446 context
->last_was_blit
= FALSE
;
2448 /* Blending and clearing should be orthogonal, but tests on the nvidia
2449 * driver show that disabling blending when clearing improves the clearing
2450 * performance incredibly. */
2451 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2452 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2453 if (gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
2455 if (!(context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
2456 || device
->state
.render_states
[WINED3D_RS_SRGBWRITEENABLE
])
2457 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
2459 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
2460 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
2462 checkGLcall("setting up state for clear");
2464 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2465 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2466 context_invalidate_state(context
, STATE_SCISSORRECT
);
2471 #if defined(STAGING_CSMT)
2472 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_state
*state
)
2474 struct wined3d_rendertarget_view
**rts
= state
->fb
.render_targets
;
2475 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2476 DWORD rt_mask
, rt_mask_bits
;
2479 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2480 return context_generate_rt_mask_no_fbo(context
, wined3d_rendertarget_view_get_surface(rts
[0]));
2481 #else /* STAGING_CSMT */
2482 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_device
*device
)
2484 const struct wined3d_state
*state
= &device
->state
;
2485 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
2486 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2487 DWORD rt_mask
, rt_mask_bits
;
2490 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2491 return context_generate_rt_mask_no_fbo(device
, wined3d_rendertarget_view_get_surface(rts
[0]));
2492 #endif /* STAGING_CSMT */
2493 else if (!context
->render_offscreen
)
2494 return context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2496 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2497 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2498 rt_mask_bits
= rt_mask
;
2500 while (rt_mask_bits
)
2502 rt_mask_bits
&= ~(1u << i
);
2503 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
2504 rt_mask
&= ~(1u << i
);
2512 /* Context activation is done by the caller. */
2513 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2515 #if defined(STAGING_CSMT)
2516 const struct wined3d_fb_state
*fb
= &state
->fb
;
2517 DWORD rt_mask
= find_draw_buffers_mask(context
, state
);
2518 #else /* STAGING_CSMT */
2519 const struct wined3d_device
*device
= context
->swapchain
->device
;
2520 const struct wined3d_fb_state
*fb
= state
->fb
;
2521 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2522 #endif /* STAGING_CSMT */
2525 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2527 if (!context
->render_offscreen
)
2529 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2530 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2536 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
2538 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(fb
->render_targets
[i
]);
2540 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2541 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2542 fb
->render_targets
[0]->resource
->draw_binding
,
2543 fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
2547 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2548 if (rt_mask
!= *cur_mask
)
2550 context_apply_draw_buffers(context
, rt_mask
);
2551 *cur_mask
= rt_mask
;
2553 #if defined(STAGING_CSMT)
2555 wined3d_fb_copy(&context
->current_fb
, &state
->fb
);
2556 #endif /* STAGING_CSMT */
2559 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
2561 DWORD i
= context
->rev_tex_unit_map
[unit
];
2562 DWORD j
= context
->tex_unit_map
[stage
];
2564 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
2565 context
->tex_unit_map
[stage
] = unit
;
2566 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2567 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2569 context
->rev_tex_unit_map
[unit
] = stage
;
2570 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2571 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2574 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
2578 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2579 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
2582 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
2583 const struct wined3d_state
*state
)
2587 context
->fixed_function_usage_map
= 0;
2588 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2590 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2591 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2592 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2593 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2594 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2595 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2596 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2597 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2599 /* Not used, and disable higher stages. */
2600 if (color_op
== WINED3D_TOP_DISABLE
)
2603 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2604 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2605 || ((color_arg3
== WINED3DTA_TEXTURE
)
2606 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2607 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2608 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2609 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2610 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2611 context
->fixed_function_usage_map
|= (1u << i
);
2613 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2614 && i
< MAX_TEXTURES
- 1)
2615 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
2618 if (i
< context
->lowest_disabled_stage
)
2621 end
= context
->lowest_disabled_stage
;
2625 start
= context
->lowest_disabled_stage
;
2629 context
->lowest_disabled_stage
= i
;
2630 for (i
= start
+ 1; i
< end
; ++i
)
2632 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
2636 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
2637 const struct wined3d_state
*state
)
2639 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2640 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2641 unsigned int i
, tex
;
2644 context_update_fixed_function_usage_map(context
, state
);
2646 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2649 ffu_map
= context
->fixed_function_usage_map
;
2651 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2652 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2654 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2659 if (context
->tex_unit_map
[i
] != i
)
2661 context_map_stage(context
, i
, i
);
2662 context_invalidate_state(context
, STATE_SAMPLER(i
));
2663 context_invalidate_texture_stage(context
, i
);
2669 /* Now work out the mapping */
2671 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2676 if (context
->tex_unit_map
[i
] != tex
)
2678 context_map_stage(context
, i
, tex
);
2679 context_invalidate_state(context
, STATE_SAMPLER(i
));
2680 context_invalidate_texture_stage(context
, i
);
2687 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2689 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2690 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2691 const struct wined3d_shader_resource_info
*resource_info
=
2692 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2695 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2698 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2700 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
2702 context_map_stage(context
, i
, i
);
2703 context_invalidate_state(context
, STATE_SAMPLER(i
));
2704 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2705 context_invalidate_texture_stage(context
, i
);
2710 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
2711 const struct wined3d_shader_resource_info
*ps_resource_info
, DWORD unit
)
2713 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
2715 /* Not currently used */
2716 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
2719 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
2721 /* Used by a fragment sampler */
2723 if (!ps_resource_info
)
2725 /* No pixel shader, check fixed function */
2726 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1u << current_mapping
));
2729 /* Pixel shader, check the shader's sampler map */
2730 return !ps_resource_info
[current_mapping
].type
;
2736 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
2738 const struct wined3d_shader_resource_info
*vs_resource_info
=
2739 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
2740 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
2741 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2742 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2745 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2748 /* Note that we only care if a resource is used or not, not the
2749 * resource's specific type. Otherwise we'd need to call
2750 * shader_update_samplers() here for 1.x pixelshaders. */
2752 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2754 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
2756 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2757 if (vs_resource_info
[i
].type
)
2761 if (context_unit_free_for_vs(context
, ps_resource_info
, start
))
2763 if (context
->tex_unit_map
[vsampler_idx
] != start
)
2765 context_map_stage(context
, vsampler_idx
, start
);
2766 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
2775 if (context
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
2776 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
2781 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2783 BOOL vs
= use_vs(state
);
2784 BOOL ps
= use_ps(state
);
2786 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
2787 * need a 1:1 map at the moment.
2788 * When the mapping of a stage is changed, sampler and ALL texture stage
2789 * states have to be reset. */
2792 context_map_psamplers(context
, state
);
2794 context_map_fixed_function_samplers(context
, state
);
2797 context_map_vsamplers(context
, ps
, state
);
2800 /* Context activation is done by the caller. */
2801 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2803 #if defined(STAGING_CSMT)
2804 DWORD rt_mask
, *cur_mask
;
2806 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2808 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2809 rt_mask
= find_draw_buffers_mask(context
, state
);
2810 #else /* STAGING_CSMT */
2811 const struct wined3d_device
*device
= context
->swapchain
->device
;
2812 DWORD rt_mask
, *cur_mask
;
2814 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2816 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2817 rt_mask
= find_draw_buffers_mask(context
, device
);
2818 #endif /* STAGING_CSMT */
2819 if (rt_mask
!= *cur_mask
)
2821 context_apply_draw_buffers(context
, rt_mask
);
2822 *cur_mask
= rt_mask
;
2826 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
2828 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
2829 *regnum
= WINED3D_FFP_POSITION
;
2830 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
2831 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
2832 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
2833 *regnum
= WINED3D_FFP_BLENDINDICES
;
2834 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
2835 *regnum
= WINED3D_FFP_NORMAL
;
2836 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
2837 *regnum
= WINED3D_FFP_PSIZE
;
2838 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
2839 *regnum
= WINED3D_FFP_DIFFUSE
;
2840 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
2841 *regnum
= WINED3D_FFP_SPECULAR
;
2842 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
2843 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
2846 FIXME("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
2854 /* Context activation is done by the caller. */
2855 void context_stream_info_from_declaration(struct wined3d_context
*context
,
2856 const struct wined3d_state
*state
, struct wined3d_stream_info
*stream_info
)
2858 /* We need to deal with frequency data! */
2859 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
2860 BOOL use_vshader
= use_vs(state
);
2861 BOOL generic_attributes
= context
->d3d_info
->ffp_generic_attributes
;
2864 stream_info
->use_map
= 0;
2865 stream_info
->swizzle_map
= 0;
2866 stream_info
->position_transformed
= declaration
->position_transformed
;
2868 /* Translate the declaration into strided data. */
2869 for (i
= 0; i
< declaration
->element_count
; ++i
)
2871 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
2872 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
2876 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
2877 element
, i
+ 1, declaration
->element_count
);
2879 if (!stream
->buffer
)
2882 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
2886 if (element
->output_slot
== WINED3D_OUTPUT_SLOT_UNUSED
)
2888 stride_used
= FALSE
;
2890 else if (element
->output_slot
== WINED3D_OUTPUT_SLOT_SEMANTIC
)
2892 /* TODO: Assuming vertexdeclarations are usually used with the
2893 * same or a similar shader, it might be worth it to store the
2894 * last used output slot and try that one first. */
2895 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
2896 element
->usage
, element
->usage_idx
, &idx
);
2900 idx
= element
->output_slot
;
2906 if (!generic_attributes
&& !element
->ffp_valid
)
2908 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
2909 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
2910 stride_used
= FALSE
;
2914 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
2920 TRACE("Load %s array %u [usage %s, usage_idx %u, "
2921 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
2922 use_vshader
? "shader": "fixed function", idx
,
2923 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
2924 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
),
2925 debug_d3dinput_classification(element
->input_slot_class
), element
->instance_data_step_rate
);
2927 stream_info
->elements
[idx
].format
= element
->format
;
2928 stream_info
->elements
[idx
].data
.buffer_object
= 0;
2929 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
2930 stream_info
->elements
[idx
].stride
= stream
->stride
;
2931 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
2932 if (stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
2934 stream_info
->elements
[idx
].divisor
= 1;
2936 else if (element
->input_slot_class
== WINED3D_INPUT_PER_INSTANCE_DATA
)
2938 stream_info
->elements
[idx
].divisor
= element
->instance_data_step_rate
;
2939 if (!element
->instance_data_step_rate
)
2940 FIXME("Instance step rate 0 not implemented.\n");
2944 stream_info
->elements
[idx
].divisor
= 0;
2947 if (!context
->gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2948 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
2950 stream_info
->swizzle_map
|= 1u << idx
;
2952 stream_info
->use_map
|= 1u << idx
;
2957 /* Context activation is done by the caller. */
2958 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2960 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2961 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2962 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
2963 DWORD prev_all_vbo
= stream_info
->all_vbo
;
2967 context_stream_info_from_declaration(context
, state
, stream_info
);
2969 stream_info
->all_vbo
= 1;
2970 context
->num_buffer_queries
= 0;
2971 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
2973 struct wined3d_stream_info_element
*element
;
2974 struct wined3d_bo_address data
;
2975 struct wined3d_buffer
*buffer
;
2980 element
= &stream_info
->elements
[i
];
2981 buffer
= state
->streams
[element
->stream_idx
].buffer
;
2983 /* We can't use VBOs if the base vertex index is negative. OpenGL
2984 * doesn't accept negative offsets (or rather offsets bigger than the
2985 * VBO, because the pointer is unsigned), so use system memory
2986 * sources. In most sane cases the pointer - offset will still be > 0,
2987 * otherwise it will wrap around to some big value. Hope that with the
2988 * indices the driver wraps it back internally. If not,
2989 * drawStridedSlow is needed, including a vertex buffer path. */
2990 if (state
->load_base_vertex_index
< 0)
2992 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
2993 state
->load_base_vertex_index
);
2994 element
->data
.buffer_object
= 0;
2995 element
->data
.addr
+= (ULONG_PTR
)buffer_get_sysmem(buffer
, context
);
2996 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
2997 FIXME("System memory vertex data load offset is negative!\n");
3001 buffer_internal_preload(buffer
, context
, state
);
3002 buffer_get_memory(buffer
, context
, &data
);
3003 element
->data
.buffer_object
= data
.buffer_object
;
3004 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
3007 if (!element
->data
.buffer_object
)
3008 stream_info
->all_vbo
= 0;
3011 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
3013 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
3018 if (state
->vertex_declaration
->half_float_conv_needed
&& !stream_info
->all_vbo
)
3020 #if defined(STAGING_CSMT)
3021 TRACE("Using draw_strided_slow with vertex shaders for FLOAT16 conversion.\n");
3022 #else /* STAGING_CSMT */
3023 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
3024 #endif /* STAGING_CSMT */
3025 context
->use_immediate_mode_draw
= TRUE
;
3029 context
->use_immediate_mode_draw
= FALSE
;
3034 WORD slow_mask
= -!d3d_info
->ffp_generic_attributes
& (1u << WINED3D_FFP_PSIZE
);
3035 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3036 & ((1u << WINED3D_FFP_DIFFUSE
) | (1u << WINED3D_FFP_SPECULAR
));
3038 if (((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
3039 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
3040 context
->use_immediate_mode_draw
= TRUE
;
3042 context
->use_immediate_mode_draw
= FALSE
;
3045 if (prev_all_vbo
!= stream_info
->all_vbo
)
3046 context_invalidate_state(context
, STATE_INDEXBUFFER
);
3049 /* Context activation is done by the caller. */
3050 static void context_preload_texture(struct wined3d_context
*context
,
3051 const struct wined3d_state
*state
, unsigned int idx
)
3053 struct wined3d_texture
*texture
;
3055 if (!(texture
= state
->textures
[idx
]))
3058 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
3061 /* Context activation is done by the caller. */
3062 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3068 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3070 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
3071 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
3077 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3079 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
3080 context_preload_texture(context
, state
, i
);
3085 WORD ffu_map
= context
->fixed_function_usage_map
;
3087 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3090 context_preload_texture(context
, state
, i
);
3095 static void context_load_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3097 struct wined3d_shader_sampler_map_entry
*entry
;
3098 struct wined3d_shader_resource_view
*view
;
3099 struct wined3d_shader
*shader
;
3102 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3104 if (!(shader
= state
->shader
[i
]))
3107 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3109 if (state
->cb
[i
][j
])
3110 buffer_internal_preload(state
->cb
[i
][j
], context
, state
);
3113 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
3115 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3117 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
3119 WARN("No resource view bound at index %u, %u.\n", i
, entry
->resource_idx
);
3123 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3124 buffer_internal_preload(buffer_from_resource(view
->resource
), context
, state
);
3126 wined3d_texture_load(wined3d_texture_from_resource(view
->resource
), context
, FALSE
);
3131 static void context_bind_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3133 const struct wined3d_device
*device
= context
->swapchain
->device
;
3134 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3135 struct wined3d_shader_sampler_map_entry
*entry
;
3136 struct wined3d_shader_resource_view
*view
;
3137 struct wined3d_sampler
*sampler
;
3138 struct wined3d_texture
*texture
;
3139 struct wined3d_shader
*shader
;
3140 unsigned int i
, j
, count
;
3141 GLuint sampler_name
;
3145 enum wined3d_shader_type type
;
3146 unsigned int base_idx
;
3151 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
3152 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
3155 for (i
= 0; i
< ARRAY_SIZE(shader_types
); ++i
)
3157 if (!(shader
= state
->shader
[shader_types
[i
].type
]))
3160 count
= shader
->reg_maps
.sampler_map
.count
;
3161 if (count
> shader_types
[i
].count
)
3163 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3164 shader
, count
, shader_types
[i
].count
);
3165 count
= shader_types
[i
].count
;
3168 for (j
= 0; j
< count
; ++j
)
3170 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3172 if (!(view
= state
->shader_resource_view
[shader_types
[i
].type
][entry
->resource_idx
]))
3174 WARN("No resource view bound at index %u, %u.\n", shader_types
[i
].type
, entry
->resource_idx
);
3178 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3180 FIXME("Buffer shader resources not supported.\n");
3184 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
3186 sampler_name
= device
->default_sampler
;
3188 else if ((sampler
= state
->sampler
[shader_types
[i
].type
][entry
->sampler_idx
]))
3190 sampler_name
= sampler
->name
;
3194 WARN("No sampler object bound at index %u, %u.\n", shader_types
[i
].type
, entry
->sampler_idx
);
3198 texture
= wined3d_texture_from_resource(view
->resource
);
3199 context_active_texture(context
, gl_info
, shader_types
[i
].base_idx
+ entry
->bind_idx
);
3200 wined3d_texture_bind(texture
, context
, FALSE
);
3202 GL_EXTCALL(glBindSampler(shader_types
[i
].base_idx
+ entry
->bind_idx
, sampler_name
));
3203 checkGLcall("glBindSampler");
3208 /* Context activation is done by the caller. */
3209 #if defined(STAGING_CSMT)
3210 BOOL
context_apply_draw_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
3211 const struct wined3d_state
*state
)
3213 const struct StateEntry
*state_table
= context
->state_table
;
3214 const struct wined3d_fb_state
*fb
= &state
->fb
;
3215 #else /* STAGING_CSMT */
3216 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
3218 const struct wined3d_state
*state
= &device
->state
;
3219 const struct StateEntry
*state_table
= context
->state_table
;
3220 const struct wined3d_fb_state
*fb
= state
->fb
;
3221 #endif /* STAGING_CSMT */
3225 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
3226 fb
->render_targets
, fb
->depth_stencil
))
3229 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3231 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3234 /* Preload resources before FBO setup. Texture preload in particular may
3235 * result in changes to the current FBO, due to using e.g. FBO blits for
3236 * updating a resource location. */
3237 context_update_tex_unit_map(context
, state
);
3238 context_preload_textures(context
, state
);
3239 context_load_shader_resources(context
, state
);
3240 /* TODO: Right now the dependency on the vertex shader is necessary
3241 * since context_stream_info_from_declaration depends on the reg_maps of
3242 * the current VS but maybe it's possible to relax the coupling in some
3243 * situations at least. */
3244 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
3245 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
3247 context_update_stream_info(context
, state
);
3251 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3254 #if defined(STAGING_CSMT)
3255 buffer_internal_preload(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
,
3258 /* PreLoad may kick buffers out of vram. */
3259 if (isStateDirty(context
, STATE_STREAMSRC
))
3260 context_update_stream_info(context
, state
);
3261 #else /* STAGING_CSMT */
3262 buffer_mark_used(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
);
3264 #endif /* STAGING_CSMT */
3266 if (state
->index_buffer
)
3268 if (context
->stream_info
.all_vbo
)
3269 buffer_internal_preload(state
->index_buffer
, context
, state
);
3271 buffer_get_sysmem(state
->index_buffer
, context
);
3274 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3276 DWORD rep
= context
->dirtyArray
[i
];
3277 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3278 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3279 context
->isStateDirty
[idx
] &= ~(1u << shift
);
3280 state_table
[rep
].apply(context
, state
, rep
);
3283 if (context
->shader_update_mask
)
3285 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3286 context
->shader_update_mask
= 0;
3289 if (context
->constant_update_mask
)
3291 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3292 context
->constant_update_mask
= 0;
3295 if (context
->update_shader_resource_bindings
)
3297 context_bind_shader_resources(context
, state
);
3298 context
->update_shader_resource_bindings
= 0;
3301 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3303 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3306 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3307 context
->last_was_blit
= FALSE
;
3312 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
3314 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3316 render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
3317 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
3319 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3320 * the alpha blend state changes with different render target formats. */
3321 if (!context
->current_rt
)
3323 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3327 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
3328 const struct wined3d_format
*new = target
->resource
.format
;
3330 if (old
->id
!= new->id
)
3332 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3333 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3334 || !(target
->container
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3335 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3337 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3338 if ((context
->current_rt
->container
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
3339 != (target
->container
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3340 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3343 /* When switching away from an offscreen render target, and we're not
3344 * using FBOs, we have to read the drawable into the texture. This is
3345 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
3346 * There are some things that need care though. PreLoad needs a GL context,
3347 * and FindContext is called before the context is activated. It also
3348 * has to be called with the old rendertarget active, otherwise a
3349 * wrong drawable is read. */
3350 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
3351 && old_render_offscreen
&& context
->current_rt
!= target
)
3353 struct wined3d_texture
*texture
= context
->current_rt
->container
;
3355 /* Read the back buffer of the old drawable into the destination texture. */
3356 if (texture
->texture_srgb
.name
)
3357 wined3d_texture_load(texture
, context
, TRUE
);
3358 wined3d_texture_load(texture
, context
, FALSE
);
3359 #if defined(STAGING_CSMT)
3360 wined3d_resource_invalidate_location(&context
->current_rt
->resource
, WINED3D_LOCATION_DRAWABLE
);
3361 #else /* STAGING_CSMT */
3362 surface_invalidate_location(context
->current_rt
, WINED3D_LOCATION_DRAWABLE
);
3363 #endif /* STAGING_CSMT */
3367 context
->current_rt
= target
;
3368 context_set_render_offscreen(context
, render_offscreen
);
3371 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
3373 struct wined3d_context
*current_context
= context_get_current();
3374 struct wined3d_context
*context
;
3376 TRACE("device %p, target %p.\n", device
, target
);
3378 if (current_context
&& current_context
->destroyed
)
3379 current_context
= NULL
;
3384 && current_context
->current_rt
3385 && current_context
->swapchain
->device
== device
)
3387 target
= current_context
->current_rt
;
3391 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
3392 if (swapchain
->back_buffers
)
3393 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
3395 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
3399 if (current_context
&& current_context
->current_rt
== target
)
3401 context
= current_context
;
3403 else if (target
->container
->swapchain
)
3405 TRACE("Rendering onscreen.\n");
3407 context
= swapchain_get_context(target
->container
->swapchain
);
3411 TRACE("Rendering offscreen.\n");
3413 /* Stay with the current context if possible. Otherwise use the
3414 * context for the primary swapchain. */
3415 if (current_context
&& current_context
->swapchain
->device
== device
)
3416 context
= current_context
;
3418 context
= swapchain_get_context(device
->swapchains
[0]);
3421 context_enter(context
);
3422 context_update_window(context
);
3423 context_setup_target(context
, target
);
3424 if (!context
->valid
) return context
;
3426 if (context
!= current_context
)
3428 if (!context_set_current(context
))
3429 ERR("Failed to activate the new context.\n");
3431 else if (context
->needs_set
)
3433 context_set_gl_context(context
);