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 wined3d_texture_prepare_texture(depth_stencil
->container
, context
, FALSE
);
138 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
140 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
141 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
142 depth_stencil
->texture_level
);
143 checkGLcall("glFramebufferTexture2D()");
146 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
148 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
149 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
150 depth_stencil
->texture_level
);
151 checkGLcall("glFramebufferTexture2D()");
155 case WINED3D_LOCATION_RB_MULTISAMPLE
:
156 surface_prepare_rb(depth_stencil
, gl_info
, TRUE
);
157 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
158 format_flags
, depth_stencil
->rb_multisample
);
161 case WINED3D_LOCATION_RB_RESOLVED
:
162 surface_prepare_rb(depth_stencil
, gl_info
, FALSE
);
163 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
164 format_flags
, depth_stencil
->rb_resolved
);
168 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
173 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
175 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
176 checkGLcall("glFramebufferTexture2D()");
179 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
181 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
182 checkGLcall("glFramebufferTexture2D()");
187 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
188 checkGLcall("glFramebufferTexture2D()");
190 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
191 checkGLcall("glFramebufferTexture2D()");
195 /* Context activation is done by the caller. */
196 static void context_attach_surface_fbo(struct wined3d_context
*context
,
197 GLenum fbo_target
, DWORD idx
, struct wined3d_surface
*surface
, DWORD location
)
199 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
201 TRACE("Attach surface %p to %u\n", surface
, idx
);
203 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
209 case WINED3D_LOCATION_TEXTURE_RGB
:
210 case WINED3D_LOCATION_TEXTURE_SRGB
:
211 srgb
= location
== WINED3D_LOCATION_TEXTURE_SRGB
;
212 wined3d_texture_prepare_texture(surface
->container
, context
, srgb
);
213 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
214 surface
->texture_target
, surface_get_texture_name(surface
, gl_info
, srgb
),
215 surface
->texture_level
);
216 checkGLcall("glFramebufferTexture2D()");
219 case WINED3D_LOCATION_RB_MULTISAMPLE
:
220 surface_prepare_rb(surface
, gl_info
, TRUE
);
221 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
222 GL_RENDERBUFFER
, surface
->rb_multisample
);
223 checkGLcall("glFramebufferRenderbuffer()");
226 case WINED3D_LOCATION_RB_RESOLVED
:
227 surface_prepare_rb(surface
, gl_info
, FALSE
);
228 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
229 GL_RENDERBUFFER
, surface
->rb_resolved
);
230 checkGLcall("glFramebufferRenderbuffer()");
234 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
240 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
241 checkGLcall("glFramebufferTexture2D()");
245 /* Context activation is done by the caller. */
246 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
248 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
251 if (!FIXME_ON(d3d
)) return;
253 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
254 if (status
== GL_FRAMEBUFFER_COMPLETE
)
256 TRACE("FBO complete\n");
260 const struct wined3d_surface
*attachment
;
263 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
265 if (!context
->current_fbo
)
267 ERR("FBO 0 is incomplete, driver bug?\n");
271 FIXME("\tColor Location %s (%#x).\n", wined3d_debug_location(context
->current_fbo
->color_location
),
272 context
->current_fbo
->color_location
);
273 FIXME("\tDepth Stencil Location %s (%#x).\n", wined3d_debug_location(context
->current_fbo
->ds_location
),
274 context
->current_fbo
->ds_location
);
276 /* Dump the FBO attachments */
277 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
279 attachment
= context
->current_fbo
->render_targets
[i
];
282 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
283 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
284 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
287 attachment
= context
->current_fbo
->depth_stencil
;
290 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
291 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
292 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
297 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
299 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
300 return buffer
? (1 << 31) | buffer
: 0;
303 static inline DWORD
context_generate_rt_mask_from_surface(const struct wined3d_surface
*target
)
305 return (1 << 31) | surface_get_gl_buffer(target
);
308 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
309 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
310 DWORD color_location
, DWORD ds_location
)
312 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
313 struct fbo_entry
*entry
;
315 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
316 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
317 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
318 entry
->depth_stencil
= depth_stencil
;
319 entry
->color_location
= color_location
;
320 entry
->ds_location
= ds_location
;
321 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
322 entry
->attached
= FALSE
;
323 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
324 checkGLcall("glGenFramebuffers()");
325 TRACE("Created FBO %u.\n", entry
->id
);
330 /* Context activation is done by the caller. */
331 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
332 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
333 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
335 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
337 context_bind_fbo(context
, target
, entry
->id
);
338 context_clean_fbo_attachments(gl_info
, target
);
340 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
341 entry
->depth_stencil
= depth_stencil
;
342 entry
->color_location
= color_location
;
343 entry
->ds_location
= ds_location
;
344 entry
->attached
= FALSE
;
347 /* Context activation is done by the caller. */
348 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
352 TRACE("Destroy FBO %u.\n", entry
->id
);
353 context_destroy_fbo(context
, entry
->id
);
355 --context
->fbo_entry_count
;
356 list_remove(&entry
->entry
);
357 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
358 HeapFree(GetProcessHeap(), 0, entry
);
361 /* Context activation is done by the caller. */
362 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
363 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
364 DWORD color_location
, DWORD ds_location
)
366 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
367 struct fbo_entry
*entry
;
369 if (depth_stencil
&& render_targets
&& render_targets
[0])
371 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
372 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
374 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
375 depth_stencil
= NULL
;
379 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
381 if (!memcmp(entry
->render_targets
,
382 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
383 && entry
->depth_stencil
== depth_stencil
&& entry
->color_location
== color_location
384 && entry
->ds_location
== ds_location
)
386 list_remove(&entry
->entry
);
387 list_add_head(&context
->fbo_list
, &entry
->entry
);
392 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
394 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, color_location
, ds_location
);
395 list_add_head(&context
->fbo_list
, &entry
->entry
);
396 ++context
->fbo_entry_count
;
400 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
401 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, color_location
, ds_location
, entry
);
402 list_remove(&entry
->entry
);
403 list_add_head(&context
->fbo_list
, &entry
->entry
);
409 /* Context activation is done by the caller. */
410 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
412 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
414 GLuint read_binding
, draw_binding
;
415 struct wined3d_surface
*depth_stencil
= entry
->depth_stencil
;
419 context_bind_fbo(context
, target
, entry
->id
);
423 read_binding
= context
->fbo_read_binding
;
424 draw_binding
= context
->fbo_draw_binding
;
425 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
427 /* Apply render targets */
428 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
430 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->color_location
);
433 if (depth_stencil
&& entry
->render_targets
[0]
434 && (depth_stencil
->resource
.multisample_type
435 != entry
->render_targets
[0]->resource
.multisample_type
436 || depth_stencil
->resource
.multisample_quality
437 != entry
->render_targets
[0]->resource
.multisample_quality
))
439 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
440 entry
->render_targets
[0]->resource
.multisample_quality
,
441 entry
->render_targets
[0]->resource
.multisample_type
,
442 depth_stencil
->resource
.multisample_quality
, depth_stencil
->resource
.multisample_type
);
443 depth_stencil
= NULL
;
447 surface_set_compatible_renderbuffer(depth_stencil
, entry
->render_targets
[0]);
448 context_attach_depth_stencil_fbo(context
, target
, depth_stencil
, entry
->ds_location
);
450 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
451 * GL contexts requirements. */
452 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
453 context_set_draw_buffer(context
, GL_NONE
);
454 if (target
!= GL_FRAMEBUFFER
)
456 if (target
== GL_READ_FRAMEBUFFER
)
457 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
459 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
462 entry
->attached
= TRUE
;
465 /* Context activation is done by the caller. */
466 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
467 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
468 DWORD color_location
, DWORD ds_location
)
470 struct fbo_entry
*entry
, *entry2
;
472 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
474 context_destroy_fbo_entry(context
, entry
);
477 if (context
->rebind_fbo
)
479 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
480 context
->rebind_fbo
= FALSE
;
483 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
485 context
->current_fbo
= NULL
;
486 context_bind_fbo(context
, target
, 0);
490 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
,
491 color_location
, ds_location
);
492 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
496 /* Context activation is done by the caller. */
497 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
498 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
500 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
502 context
->blit_targets
[0] = render_target
;
504 memset(&context
->blit_targets
[1], 0, clear_size
);
505 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
, location
);
508 /* Context activation is done by the caller. */
509 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
511 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
513 if (context
->free_occlusion_query_count
)
515 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
519 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
521 GL_EXTCALL(glGenQueries(1, &query
->id
));
522 checkGLcall("glGenQueries");
524 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
528 WARN("Occlusion queries not supported, not allocating query id.\n");
533 query
->context
= context
;
534 list_add_head(&context
->occlusion_queries
, &query
->entry
);
537 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
539 struct wined3d_context
*context
= query
->context
;
541 list_remove(&query
->entry
);
542 query
->context
= NULL
;
544 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
546 UINT new_size
= context
->free_occlusion_query_size
<< 1;
547 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
548 new_size
* sizeof(*context
->free_occlusion_queries
));
552 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
556 context
->free_occlusion_query_size
= new_size
;
557 context
->free_occlusion_queries
= new_data
;
560 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
563 /* Context activation is done by the caller. */
564 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
566 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
568 if (context
->free_event_query_count
)
570 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
574 if (gl_info
->supported
[ARB_SYNC
])
576 /* Using ARB_sync, not much to do here. */
577 query
->object
.sync
= NULL
;
578 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
580 else if (gl_info
->supported
[APPLE_FENCE
])
582 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
583 checkGLcall("glGenFencesAPPLE");
585 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
587 else if(gl_info
->supported
[NV_FENCE
])
589 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
590 checkGLcall("glGenFencesNV");
592 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
596 WARN("Event queries not supported, not allocating query id.\n");
597 query
->object
.id
= 0;
601 query
->context
= context
;
602 list_add_head(&context
->event_queries
, &query
->entry
);
605 void context_free_event_query(struct wined3d_event_query
*query
)
607 struct wined3d_context
*context
= query
->context
;
609 list_remove(&query
->entry
);
610 query
->context
= NULL
;
612 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
614 UINT new_size
= context
->free_event_query_size
<< 1;
615 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
616 new_size
* sizeof(*context
->free_event_queries
));
620 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
624 context
->free_event_query_size
= new_size
;
625 context
->free_event_queries
= new_data
;
628 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
631 /* Context activation is done by the caller. */
632 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
634 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
636 if (context
->free_timestamp_query_count
)
638 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
642 GL_EXTCALL(glGenQueries(1, &query
->id
));
643 checkGLcall("glGenQueries");
645 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
648 query
->context
= context
;
649 list_add_head(&context
->timestamp_queries
, &query
->entry
);
652 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
654 struct wined3d_context
*context
= query
->context
;
656 list_remove(&query
->entry
);
657 query
->context
= NULL
;
659 if (context
->free_timestamp_query_count
>= context
->free_timestamp_query_size
- 1)
661 UINT new_size
= context
->free_timestamp_query_size
<< 1;
662 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_timestamp_queries
,
663 new_size
* sizeof(*context
->free_timestamp_queries
));
667 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
671 context
->free_timestamp_query_size
= new_size
;
672 context
->free_timestamp_queries
= new_data
;
675 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
678 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
680 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
681 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
685 for (i
= 0; i
< device
->context_count
; ++i
)
687 struct wined3d_context
*context
= device
->contexts
[i
];
688 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
689 struct fbo_entry
*entry
, *entry2
;
691 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
693 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
697 if (entry
->depth_stencil
== surface
)
699 callback(context
, entry
);
703 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
705 if (entry
->render_targets
[j
] == surface
)
707 callback(context
, entry
);
715 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
717 list_remove(&entry
->entry
);
718 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
721 void context_resource_released(const struct wined3d_device
*device
,
722 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
724 if (!device
->d3d_initialized
) return;
728 case WINED3D_RTYPE_SURFACE
:
729 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
730 context_queue_fbo_entry_destruction
);
738 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
740 entry
->attached
= FALSE
;
743 void context_resource_unloaded(const struct wined3d_device
*device
,
744 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
748 case WINED3D_RTYPE_SURFACE
:
749 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
750 context_detach_fbo_entry
);
758 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
760 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
761 struct fbo_entry
*entry
= context
->current_fbo
;
764 if (!entry
|| context
->rebind_fbo
) return;
766 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
768 if (surface
== entry
->render_targets
[i
])
770 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
771 context
->rebind_fbo
= TRUE
;
776 if (surface
== entry
->depth_stencil
)
778 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
779 context
->rebind_fbo
= TRUE
;
783 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
785 int current
= GetPixelFormat(dc
);
787 if (current
== format
) return TRUE
;
791 if (!SetPixelFormat(dc
, format
, NULL
))
793 /* This may also happen if the dc belongs to a destroyed window. */
794 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
795 format
, dc
, GetLastError());
801 /* By default WGL doesn't allow pixel format adjustments but we need it
802 * here. For this reason there's a Wine specific wglSetPixelFormat()
803 * which allows us to set the pixel format multiple times. Only use it
804 * when really needed. */
805 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
807 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
809 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
816 /* OpenGL doesn't allow pixel format adjustments. Print an error and
817 * continue using the old format. There's a big chance that the old
818 * format works although with a performance hit and perhaps rendering
820 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
821 format
, dc
, current
);
825 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
827 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
830 if (!context_set_pixel_format(ctx
->gl_info
, ctx
->hdc
, ctx
->pixel_format
))
832 WARN("Failed to set pixel format %d on device context %p.\n",
833 ctx
->pixel_format
, ctx
->hdc
);
837 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
841 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
842 ctx
->glCtx
, ctx
->hdc
, GetLastError());
844 WARN("Trying fallback to the backup window.\n");
846 /* FIXME: If the context is destroyed it's no longer associated with
847 * a swapchain, so we can't use the swapchain to get a backup dc. To
848 * make this work windowless contexts would need to be handled by the
852 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
853 context_set_current(NULL
);
857 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
859 context_set_current(NULL
);
863 if (!context_set_pixel_format(ctx
->gl_info
, dc
, ctx
->pixel_format
))
865 ERR("Failed to set pixel format %d on device context %p.\n",
866 ctx
->pixel_format
, dc
);
867 context_set_current(NULL
);
871 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
873 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
875 context_set_current(NULL
);
885 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
, int pf
)
887 if (!context_set_pixel_format(gl_info
, dc
, pf
))
889 ERR("Failed to restore pixel format %d on device context %p.\n", pf
, dc
);
890 context_set_current(NULL
);
894 if (!wglMakeCurrent(dc
, gl_ctx
))
896 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
897 gl_ctx
, dc
, GetLastError());
898 context_set_current(NULL
);
902 static void context_update_window(struct wined3d_context
*context
)
904 if (context
->win_handle
== context
->swapchain
->win_handle
)
907 TRACE("Updating context %p window from %p to %p.\n",
908 context
, context
->win_handle
, context
->swapchain
->win_handle
);
911 wined3d_release_dc(context
->win_handle
, context
->hdc
);
913 context
->win_handle
= context
->swapchain
->win_handle
;
914 context
->needs_set
= 1;
917 if (!(context
->hdc
= GetDC(context
->win_handle
)))
919 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
924 static void context_destroy_gl_resources(struct wined3d_context
*context
)
926 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
927 struct wined3d_timestamp_query
*timestamp_query
;
928 struct wined3d_occlusion_query
*occlusion_query
;
929 struct wined3d_event_query
*event_query
;
930 struct fbo_entry
*entry
, *entry2
;
936 restore_ctx
= wglGetCurrentContext();
937 restore_dc
= wglGetCurrentDC();
938 restore_pf
= GetPixelFormat(restore_dc
);
940 if (restore_ctx
== context
->glCtx
)
942 else if (context
->valid
)
943 context_set_gl_context(context
);
945 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
948 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
949 timestamp_query
->context
= NULL
;
952 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
954 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
955 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
956 occlusion_query
->context
= NULL
;
959 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
963 if (gl_info
->supported
[ARB_SYNC
])
965 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
967 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
968 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
970 event_query
->context
= NULL
;
973 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
975 if (!context
->valid
) entry
->id
= 0;
976 context_destroy_fbo_entry(context
, entry
);
979 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
981 if (!context
->valid
) entry
->id
= 0;
982 context_destroy_fbo_entry(context
, entry
);
987 if (context
->dummy_arbfp_prog
)
989 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
992 if (gl_info
->supported
[ARB_TIMER_QUERY
])
993 GL_EXTCALL(glDeleteQueries(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
995 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
996 GL_EXTCALL(glDeleteQueries(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
998 if (gl_info
->supported
[ARB_SYNC
])
1000 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1002 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
1005 else if (gl_info
->supported
[APPLE_FENCE
])
1007 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1009 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1012 else if (gl_info
->supported
[NV_FENCE
])
1014 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1016 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1020 checkGLcall("context cleanup");
1023 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1024 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1025 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1029 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
, restore_pf
);
1031 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1033 ERR("Failed to disable GL context.\n");
1036 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1038 if (!wglDeleteContext(context
->glCtx
))
1040 DWORD err
= GetLastError();
1041 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1045 DWORD
context_get_tls_idx(void)
1047 return wined3d_context_tls_idx
;
1050 void context_set_tls_idx(DWORD idx
)
1052 wined3d_context_tls_idx
= idx
;
1055 struct wined3d_context
*context_get_current(void)
1057 return TlsGetValue(wined3d_context_tls_idx
);
1060 BOOL
context_set_current(struct wined3d_context
*ctx
)
1062 struct wined3d_context
*old
= context_get_current();
1066 TRACE("Already using D3D context %p.\n", ctx
);
1074 TRACE("Switching away from destroyed context %p.\n", old
);
1075 context_destroy_gl_resources(old
);
1076 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1077 HeapFree(GetProcessHeap(), 0, old
);
1089 ERR("Trying to make invalid context %p current\n", ctx
);
1093 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1094 if (!context_set_gl_context(ctx
))
1098 else if(wglGetCurrentContext())
1100 TRACE("Clearing current D3D context.\n");
1101 if (!wglMakeCurrent(NULL
, NULL
))
1103 DWORD err
= GetLastError();
1104 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1105 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1110 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1113 void context_release(struct wined3d_context
*context
)
1115 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1119 if (!context
->level
)
1120 WARN("Context %p is not active.\n", context
);
1121 else if (context
!= context_get_current())
1122 WARN("Context %p is not the current context.\n", context
);
1125 if (!--context
->level
&& context
->restore_ctx
)
1127 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1128 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
, context
->restore_pf
);
1129 context
->restore_ctx
= NULL
;
1130 context
->restore_dc
= NULL
;
1134 static void context_enter(struct wined3d_context
*context
)
1136 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1138 if (!context
->level
++)
1140 const struct wined3d_context
*current_context
= context_get_current();
1141 HGLRC current_gl
= wglGetCurrentContext();
1143 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1145 TRACE("Another GL context (%p on device context %p) is already current.\n",
1146 current_gl
, wglGetCurrentDC());
1147 context
->restore_ctx
= current_gl
;
1148 context
->restore_dc
= wglGetCurrentDC();
1149 context
->restore_pf
= GetPixelFormat(context
->restore_dc
);
1150 context
->needs_set
= 1;
1155 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1157 DWORD rep
= context
->state_table
[state
].representative
;
1161 if (isStateDirty(context
, rep
)) return;
1163 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1164 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1165 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1166 context
->isStateDirty
[idx
] |= (1 << shift
);
1169 /* This function takes care of wined3d pixel format selection. */
1170 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1171 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1172 BOOL auxBuffers
, BOOL findCompatible
)
1175 BYTE redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1176 BYTE depthBits
=0, stencilBits
=0;
1177 unsigned int current_value
;
1178 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1181 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1182 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1183 auxBuffers
, findCompatible
);
1185 if (!getColorBits(color_format
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1187 ERR("Unable to get color bits for format %s (%#x)!\n",
1188 debug_d3dformat(color_format
->id
), color_format
->id
);
1192 getDepthStencilBits(ds_format
, &depthBits
, &stencilBits
);
1195 for (i
= 0; i
< cfg_count
; ++i
)
1197 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1200 /* For now only accept RGBA formats. Perhaps some day we will
1201 * allow floating point formats for pbuffers. */
1202 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1204 /* In window mode we need a window drawable format and double buffering. */
1205 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1207 if (cfg
->redSize
< redBits
)
1209 if (cfg
->greenSize
< greenBits
)
1211 if (cfg
->blueSize
< blueBits
)
1213 if (cfg
->alphaSize
< alphaBits
)
1215 if (cfg
->depthSize
< depthBits
)
1217 if (stencilBits
&& cfg
->stencilSize
!= stencilBits
)
1219 /* Check multisampling support. */
1220 if (cfg
->numSamples
)
1224 /* We try to locate a format which matches our requirements exactly. In case of
1225 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1226 if (cfg
->depthSize
== depthBits
)
1228 if (cfg
->stencilSize
== stencilBits
)
1230 if (cfg
->alphaSize
== alphaBits
)
1232 /* We like to have aux buffers in backbuffer mode */
1233 if (auxBuffers
&& cfg
->auxBuffers
)
1235 if (cfg
->redSize
== redBits
1236 && cfg
->greenSize
== greenBits
1237 && cfg
->blueSize
== blueBits
)
1240 if (value
> current_value
)
1242 iPixelFormat
= cfg
->iPixelFormat
;
1243 current_value
= value
;
1247 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1248 if(!iPixelFormat
&& !findCompatible
) {
1249 ERR("Can't find a suitable iPixelFormat\n");
1251 } else if(!iPixelFormat
) {
1252 PIXELFORMATDESCRIPTOR pfd
;
1254 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1255 /* PixelFormat selection */
1256 ZeroMemory(&pfd
, sizeof(pfd
));
1257 pfd
.nSize
= sizeof(pfd
);
1259 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1260 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1261 pfd
.cAlphaBits
= alphaBits
;
1262 pfd
.cColorBits
= colorBits
;
1263 pfd
.cDepthBits
= depthBits
;
1264 pfd
.cStencilBits
= stencilBits
;
1265 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1267 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1269 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1270 ERR("Can't find a suitable iPixelFormat\n");
1275 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1276 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1277 return iPixelFormat
;
1280 /* Context activation is done by the caller. */
1281 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1283 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1284 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1286 for (i
= 0; i
< count
; ++i
)
1288 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1289 checkGLcall("glActiveTexture");
1291 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1292 checkGLcall("glBindTexture");
1294 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1296 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1297 checkGLcall("glBindTexture");
1300 if (gl_info
->supported
[EXT_TEXTURE3D
])
1302 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1303 checkGLcall("glBindTexture");
1306 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1308 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1309 checkGLcall("glBindTexture");
1314 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1316 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1317 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1320 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1321 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1325 case GL_DEBUG_TYPE_ERROR_ARB
:
1326 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1329 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1330 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1331 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1332 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1335 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1336 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1340 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1345 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1348 unsigned int ctx_attrib_idx
= 0;
1349 GLint ctx_attribs
[7], ctx_flags
= 0;
1351 if (context_debug_output_enabled(gl_info
))
1352 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1353 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1354 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1355 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1356 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1357 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1358 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1361 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1362 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1364 ctx_attribs
[ctx_attrib_idx
] = 0;
1366 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1368 if (ctx_flags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
1370 ctx_attribs
[ctx_attrib_idx
- 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1371 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1372 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1379 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1380 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1382 struct wined3d_device
*device
= swapchain
->device
;
1383 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1384 const struct wined3d_format
*color_format
;
1385 struct wined3d_context
*ret
;
1386 BOOL auxBuffers
= FALSE
;
1387 HGLRC ctx
, share_ctx
;
1394 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1396 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1400 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1401 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1402 if (!ret
->blit_targets
)
1405 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1406 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1407 if (!ret
->draw_buffers
)
1410 ret
->free_timestamp_query_size
= 4;
1411 ret
->free_timestamp_queries
= HeapAlloc(GetProcessHeap(), 0,
1412 ret
->free_timestamp_query_size
* sizeof(*ret
->free_timestamp_queries
));
1413 if (!ret
->free_timestamp_queries
)
1415 list_init(&ret
->timestamp_queries
);
1417 ret
->free_occlusion_query_size
= 4;
1418 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1419 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1420 if (!ret
->free_occlusion_queries
)
1423 list_init(&ret
->occlusion_queries
);
1425 ret
->free_event_query_size
= 4;
1426 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1427 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1428 if (!ret
->free_event_queries
)
1431 list_init(&ret
->event_queries
);
1432 list_init(&ret
->fbo_list
);
1433 list_init(&ret
->fbo_destroy_list
);
1435 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1437 ERR("Failed to allocate shader backend context data.\n");
1440 if (!device
->adapter
->fragment_pipe
->allocate_context_data(ret
))
1442 ERR("Failed to allocate fragment pipeline context data.\n");
1446 #if defined(STAGING_CSMT)
1447 ret
->current_fb
.render_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1448 sizeof(*ret
->current_fb
.render_targets
) * gl_info
->limits
.buffers
);
1449 ret
->current_fb
.rt_size
= gl_info
->limits
.buffers
;
1450 if (!ret
->current_fb
.render_targets
)
1452 if (device
->context_count
)
1453 ret
->offscreenBuffer
= device
->contexts
[0]->offscreenBuffer
;
1455 #endif /* STAGING_CSMT */
1456 /* Initialize the texture unit mapping to a 1:1 mapping */
1457 for (s
= 0; s
< MAX_COMBINED_SAMPLERS
; ++s
)
1459 if (s
< gl_info
->limits
.fragment_samplers
)
1461 ret
->tex_unit_map
[s
] = s
;
1462 ret
->rev_tex_unit_map
[s
] = s
;
1466 ret
->tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1467 ret
->rev_tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1471 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1473 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1475 if (!(hdc
= swapchain_get_backup_dc(swapchain
)))
1477 ERR("Failed to retrieve a device context.\n");
1482 color_format
= target
->resource
.format
;
1484 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1485 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1486 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1490 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1491 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1492 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1493 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1496 /* DirectDraw supports 8bit paletted render targets and these are used by
1497 * old games like StarCraft and C&C. Most modern hardware doesn't support
1498 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1499 * conversion (ab)uses the alpha component for storing the palette index.
1500 * For this reason we require a format with 8bit alpha, so request
1502 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1503 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1505 /* Try to find a pixel format which matches our requirements. */
1506 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1508 /* Try to locate a compatible format if we weren't able to find anything. */
1511 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1512 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1515 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1518 ERR("Can't find a suitable pixel format.\n");
1524 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1526 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1527 context_release(ret
);
1531 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1532 if (gl_info
->p_wglCreateContextAttribsARB
)
1534 if (!(ctx
= context_create_wgl_attribs(gl_info
, hdc
, share_ctx
)))
1539 if (!(ctx
= wglCreateContext(hdc
)))
1541 ERR("Failed to create a WGL context.\n");
1542 context_release(ret
);
1546 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1548 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1549 context_release(ret
);
1550 if (!wglDeleteContext(ctx
))
1551 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1556 if (!device_context_add(device
, ret
))
1558 ERR("Failed to add the newly created context to the context list\n");
1559 context_release(ret
);
1560 if (!wglDeleteContext(ctx
))
1561 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1565 ret
->gl_info
= gl_info
;
1566 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1567 ret
->state_table
= device
->StateTable
;
1569 /* Mark all states dirty to force a proper initialization of the states
1570 * on the first use of the context. */
1571 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1573 if (ret
->state_table
[state
].representative
)
1574 context_invalidate_state(ret
, state
);
1577 ret
->swapchain
= swapchain
;
1578 ret
->current_rt
= target
;
1579 ret
->tid
= GetCurrentThreadId();
1581 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
1582 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1586 ret
->win_handle
= swapchain
->win_handle
;
1588 ret
->pixel_format
= pixel_format
;
1591 /* Set up the context defaults */
1592 if (!context_set_current(ret
))
1594 ERR("Cannot activate context to set up defaults.\n");
1595 device_context_remove(device
, ret
);
1596 context_release(ret
);
1597 if (!wglDeleteContext(ctx
))
1598 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1602 if (context_debug_output_enabled(gl_info
))
1604 GL_EXTCALL(glDebugMessageCallbackARB(wined3d_debug_callback
, ret
));
1605 if (TRACE_ON(d3d_synchronous
))
1606 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB
);
1607 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1610 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR_ARB
,
1611 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1615 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
,
1616 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1617 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
,
1618 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1619 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY_ARB
,
1620 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1622 if (WARN_ON(d3d_perf
))
1624 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE_ARB
,
1625 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1629 switch (swapchain
->desc
.swap_interval
)
1631 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1634 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1635 case WINED3DPRESENT_INTERVAL_ONE
:
1638 case WINED3DPRESENT_INTERVAL_TWO
:
1641 case WINED3DPRESENT_INTERVAL_THREE
:
1644 case WINED3DPRESENT_INTERVAL_FOUR
:
1648 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1652 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1654 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1655 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1656 swap_interval
, ret
, GetLastError());
1659 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1661 TRACE("Setting up the screen\n");
1663 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1664 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1666 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1667 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1669 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1670 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1672 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1673 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1674 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
1675 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1677 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1679 /* Direct3D always uses n-1 weights for n world matrices and uses
1680 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1681 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1682 * enabled as well. */
1683 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1684 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1686 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1688 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1689 * the previous texture where to source the offset from is always unit - 1.
1691 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1693 context_active_texture(ret
, gl_info
, s
);
1694 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1695 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1696 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1699 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1701 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1702 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1703 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1704 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1707 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1708 * program and the dummy program is destroyed when the context is destroyed.
1710 static const char dummy_program
[] =
1712 "MOV result.color, fragment.color.primary;\n"
1714 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1715 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1716 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1719 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1721 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1723 context_active_texture(ret
, gl_info
, s
);
1724 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1725 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1729 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1731 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1733 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1735 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1737 device
->shader_backend
->shader_init_context_state(ret
);
1738 ret
->shader_update_mask
= (1 << WINED3D_SHADER_TYPE_PIXEL
)
1739 | (1 << WINED3D_SHADER_TYPE_VERTEX
)
1740 | (1 << WINED3D_SHADER_TYPE_GEOMETRY
);
1742 /* If this happens to be the first context for the device, dummy textures
1743 * are not created yet. In that case, they will be created (and bound) by
1744 * create_dummy_textures right after this context is initialized. */
1745 if (device
->dummy_texture_2d
[0])
1746 bind_dummy_textures(device
, ret
);
1748 TRACE("Created context %p.\n", ret
);
1753 device
->shader_backend
->shader_free_context_data(ret
);
1754 device
->adapter
->fragment_pipe
->free_context_data(ret
);
1755 #if defined(STAGING_CSMT)
1756 HeapFree(GetProcessHeap(), 0, ret
->current_fb
.render_targets
);
1757 #endif /* STAGING_CSMT */
1758 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1759 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1760 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
1761 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1762 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1763 HeapFree(GetProcessHeap(), 0, ret
);
1767 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1771 TRACE("Destroying ctx %p\n", context
);
1773 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1775 context_destroy_gl_resources(context
);
1776 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1781 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1782 in wined3d_adapter may go away in the meantime */
1783 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
1784 *gl_info
= *context
->gl_info
;
1785 context
->gl_info
= gl_info
;
1786 context
->destroyed
= 1;
1790 device
->shader_backend
->shader_free_context_data(context
);
1791 device
->adapter
->fragment_pipe
->free_context_data(context
);
1792 #if defined(STAGING_CSMT)
1793 HeapFree(GetProcessHeap(), 0, context
->current_fb
.render_targets
);
1794 #endif /* STAGING_CSMT */
1795 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1796 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1797 device_context_remove(device
, context
);
1798 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1801 /* Context activation is done by the caller. */
1802 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
1804 const GLdouble projection
[] =
1806 2.0 / width
, 0.0, 0.0, 0.0,
1807 0.0, 2.0 / height
, 0.0, 0.0,
1809 -1.0, -1.0, -1.0, 1.0,
1812 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
1813 checkGLcall("glMatrixMode(GL_PROJECTION)");
1814 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
1815 checkGLcall("glLoadMatrixd");
1816 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
1817 checkGLcall("glViewport");
1820 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
1822 const struct wined3d_surface
*rt
= context
->current_rt
;
1824 if (rt
->container
->swapchain
&& rt
->container
->swapchain
->front_buffer
== rt
->container
)
1828 GetClientRect(context
->win_handle
, &window_size
);
1829 size
->cx
= window_size
.right
- window_size
.left
;
1830 size
->cy
= window_size
.bottom
- window_size
.top
;
1835 size
->cx
= rt
->resource
.width
;
1836 size
->cy
= rt
->resource
.height
;
1839 /*****************************************************************************
1842 * Sets up a context for DirectDraw blitting.
1843 * All texture units are disabled, texture unit 0 is set as current unit
1844 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1845 * color writing enabled for all channels
1846 * register combiners disabled, shaders disabled
1847 * world matrix is set to identity, texture matrix 0 too
1848 * projection matrix is setup for drawing screen coordinates
1851 * This: Device to activate the context for
1852 * context: Context to setup
1854 *****************************************************************************/
1855 /* Context activation is done by the caller. */
1856 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
1859 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1863 TRACE("Setting up context %p for blitting\n", context
);
1865 context_get_rt_size(context
, &rt_size
);
1867 if (context
->last_was_blit
)
1869 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
1871 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1872 context
->blit_w
= rt_size
.cx
;
1873 context
->blit_h
= rt_size
.cy
;
1874 /* No need to dirtify here, the states are still dirtified because
1875 * they weren't applied since the last SetupForBlit() call. */
1877 TRACE("Context is already set up for blitting, nothing to do\n");
1880 context
->last_was_blit
= TRUE
;
1882 /* Disable all textures. The caller can then bind a texture it wants to blit
1885 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1886 * function texture unit. No need to care for higher samplers
1888 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1890 sampler
= context
->rev_tex_unit_map
[i
];
1891 context_active_texture(context
, gl_info
, i
);
1893 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1895 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1896 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1898 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1899 checkGLcall("glDisable GL_TEXTURE_3D");
1900 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1902 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1903 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1905 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1906 checkGLcall("glDisable GL_TEXTURE_2D");
1908 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1909 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1911 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1913 if (sampler
< MAX_TEXTURES
)
1914 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1915 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1918 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
1919 GL_EXTCALL(glBindSampler(0, 0));
1920 context_active_texture(context
, gl_info
, 0);
1922 sampler
= context
->rev_tex_unit_map
[0];
1924 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1926 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1927 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1929 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1930 checkGLcall("glDisable GL_TEXTURE_3D");
1931 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1933 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1934 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1936 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1937 checkGLcall("glDisable GL_TEXTURE_2D");
1939 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1941 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
1942 checkGLcall("glMatrixMode(GL_TEXTURE)");
1943 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
1944 checkGLcall("glLoadIdentity()");
1946 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1948 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1949 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
1950 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
1953 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1955 if (sampler
< MAX_TEXTURES
)
1957 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
1958 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1960 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1963 /* Other misc states */
1964 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
1965 checkGLcall("glDisable(GL_ALPHA_TEST)");
1966 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
1967 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
1968 checkGLcall("glDisable GL_LIGHTING");
1969 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
1970 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
1971 checkGLcall("glDisable GL_DEPTH_TEST");
1972 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
1973 glDisableWINE(GL_FOG
);
1974 checkGLcall("glDisable GL_FOG");
1975 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
1976 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
1977 checkGLcall("glDisable GL_BLEND");
1978 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
1979 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
1980 checkGLcall("glDisable GL_CULL_FACE");
1981 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
1982 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
1983 checkGLcall("glDisable GL_STENCIL_TEST");
1984 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
1985 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
1986 checkGLcall("glDisable GL_SCISSOR_TEST");
1987 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
1988 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1990 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
1991 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1992 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
1994 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1995 checkGLcall("glColorMask");
1996 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
1997 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
1998 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
1999 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2000 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2002 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2003 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2004 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2007 /* Setup transforms */
2008 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2009 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2010 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2011 checkGLcall("glLoadIdentity()");
2012 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2014 context
->last_was_rhw
= TRUE
;
2015 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2017 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2018 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2019 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2020 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2021 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2022 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2023 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2025 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2027 /* Disable shaders */
2028 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2030 context
->blit_w
= rt_size
.cx
;
2031 context
->blit_h
= rt_size
.cy
;
2032 context_invalidate_state(context
, STATE_VIEWPORT
);
2033 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2036 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2038 return rt_mask
& (1 << 31);
2041 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2043 return rt_mask
& ~(1 << 31);
2046 /* Context activation is done by the caller. */
2047 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2049 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2053 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2054 checkGLcall("glDrawBuffer()");
2056 else if (is_rt_mask_onscreen(rt_mask
))
2058 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2059 checkGLcall("glDrawBuffer()");
2063 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2070 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2072 context
->draw_buffers
[i
] = GL_NONE
;
2078 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2080 GL_EXTCALL(glDrawBuffers(i
, context
->draw_buffers
));
2081 checkGLcall("glDrawBuffers()");
2085 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2086 checkGLcall("glDrawBuffer()");
2091 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2096 /* Context activation is done by the caller. */
2097 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2099 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2100 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2101 DWORD new_mask
= context_generate_rt_mask(buffer
);
2103 if (new_mask
== *current_mask
)
2106 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2107 checkGLcall("glDrawBuffer()");
2109 *current_mask
= new_mask
;
2112 /* Context activation is done by the caller. */
2113 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2115 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2116 checkGLcall("glActiveTexture");
2117 context
->active_texture
= unit
;
2120 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2122 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2123 DWORD unit
= context
->active_texture
;
2124 DWORD old_texture_type
= context
->texture_type
[unit
];
2128 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2129 checkGLcall("glBindTexture");
2136 if (old_texture_type
!= target
)
2138 const struct wined3d_device
*device
= context
->swapchain
->device
;
2140 switch (old_texture_type
)
2146 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2147 checkGLcall("glBindTexture");
2149 case GL_TEXTURE_RECTANGLE_ARB
:
2150 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2151 checkGLcall("glBindTexture");
2153 case GL_TEXTURE_CUBE_MAP
:
2154 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2155 checkGLcall("glBindTexture");
2158 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2159 checkGLcall("glBindTexture");
2162 ERR("Unexpected texture target %#x\n", old_texture_type
);
2165 context
->texture_type
[unit
] = target
;
2169 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2171 if (context
->render_offscreen
== offscreen
) return;
2173 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2174 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2175 context_invalidate_state(context
, STATE_VIEWPORT
);
2176 context_invalidate_state(context
, STATE_SCISSORRECT
);
2177 context_invalidate_state(context
, STATE_FRONTFACE
);
2178 context
->render_offscreen
= offscreen
;
2181 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2182 const struct wined3d_format
*required
)
2184 BYTE existing_depth
, existing_stencil
, required_depth
, required_stencil
;
2186 if (existing
== required
)
2188 if ((existing
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
)
2189 != (required
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
))
2192 getDepthStencilBits(existing
, &existing_depth
, &existing_stencil
);
2193 getDepthStencilBits(required
, &required_depth
, &required_stencil
);
2195 if(existing_depth
< required_depth
) return FALSE
;
2196 /* If stencil bits are used the exact amount is required - otherwise wrapping
2197 * won't work correctly */
2198 if(required_stencil
&& required_stencil
!= existing_stencil
) return FALSE
;
2202 #if defined(STAGING_CSMT)
2203 /* Context activation is done by the caller. */
2204 #else /* STAGING_CSMT */
2205 /* The caller provides a context */
2206 #endif /* STAGING_CSMT */
2207 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2208 const struct wined3d_rendertarget_view
*depth_stencil
)
2210 /* Onscreen surfaces are always in a swapchain */
2211 struct wined3d_swapchain
*swapchain
= context
->current_rt
->container
->swapchain
;
2213 if (context
->render_offscreen
|| !depth_stencil
) return;
2214 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2216 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2217 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2219 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2221 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2222 #if defined(STAGING_CSMT)
2223 wined3d_resource_load_location(&context
->current_rt
->resource
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
2224 swapchain
->render_to_fbo
= TRUE
;
2225 swapchain_update_draw_bindings(swapchain
);
2226 context_set_render_offscreen(context
, TRUE
);
2229 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_context
*context
, const struct wined3d_surface
*rt
)
2231 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2233 else if (rt
->container
->swapchain
)
2234 return context_generate_rt_mask_from_surface(rt
);
2236 return context_generate_rt_mask(context
->offscreenBuffer
);
2237 #else /* STAGING_CSMT */
2238 surface_load_location(context
->current_rt
, WINED3D_LOCATION_TEXTURE_RGB
);
2239 swapchain
->render_to_fbo
= TRUE
;
2240 swapchain_update_draw_bindings(swapchain
);
2241 context_set_render_offscreen(context
, TRUE
);
2244 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
2246 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2248 else if (rt
->container
->swapchain
)
2249 return context_generate_rt_mask_from_surface(rt
);
2251 return context_generate_rt_mask(device
->offscreenBuffer
);
2252 #endif /* STAGING_CSMT */
2255 /* Context activation is done by the caller. */
2256 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2258 struct wined3d_surface
*rt
= context
->current_rt
;
2259 DWORD rt_mask
, *cur_mask
;
2261 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2263 context_validate_onscreen_formats(context
, NULL
);
2265 if (context
->render_offscreen
)
2267 wined3d_texture_load(rt
->container
, context
, FALSE
);
2269 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->container
->resource
.draw_binding
);
2270 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2277 context
->current_fbo
= NULL
;
2278 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2279 rt_mask
= context_generate_rt_mask_from_surface(rt
);
2284 #if defined(STAGING_CSMT)
2285 rt_mask
= context_generate_rt_mask_no_fbo(context
, rt
);
2286 #else /* STAGING_CSMT */
2287 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt
);
2288 #endif /* STAGING_CSMT */
2291 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2293 if (rt_mask
!= *cur_mask
)
2295 context_apply_draw_buffers(context
, rt_mask
);
2296 *cur_mask
= rt_mask
;
2299 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2301 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2304 SetupForBlit(device
, context
);
2305 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2308 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2309 const struct wined3d_rendertarget_view
*ds
)
2313 if (ds
) return TRUE
;
2315 for (i
= 0; i
< rt_count
; ++i
)
2317 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2321 WARN("Invalid render target config, need at least one attachment.\n");
2325 /* Context activation is done by the caller. */
2326 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2327 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2329 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2330 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
2331 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2332 DWORD rt_mask
= 0, *cur_mask
;
2335 #if defined(STAGING_CSMT)
2336 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || !wined3d_fb_equal(fb
, &context
->current_fb
)
2337 #else /* STAGING_CSMT */
2338 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2339 #endif /* STAGING_CSMT */
2340 || rt_count
!= context
->gl_info
->limits
.buffers
)
2342 if (!context_validate_rt_config(rt_count
, rts
, dsv
))
2345 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2347 context_validate_onscreen_formats(context
, dsv
);
2349 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2351 for (i
= 0; i
< rt_count
; ++i
)
2353 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(rts
[i
]);
2354 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2355 rt_mask
|= (1 << i
);
2357 while (i
< context
->gl_info
->limits
.buffers
)
2359 context
->blit_targets
[i
] = NULL
;
2362 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2363 wined3d_rendertarget_view_get_surface(dsv
),
2364 rt_count
? rts
[0]->resource
->draw_binding
: 0,
2365 dsv
? dsv
->resource
->draw_binding
: 0);
2369 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2370 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2371 rt_mask
= context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2374 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2375 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2376 * state management allows this */
2377 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2381 #if defined(STAGING_CSMT)
2382 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2383 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2386 wined3d_fb_copy(&context
->current_fb
, fb
);
2387 #else /* STAGING_CSMT */
2388 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2389 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2391 #endif /* STAGING_CSMT */
2393 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2394 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2396 for (i
= 0; i
< rt_count
; ++i
)
2398 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2399 rt_mask
|= (1 << i
);
2404 #if defined(STAGING_CSMT)
2405 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2406 #else /* STAGING_CSMT */
2407 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2408 #endif /* STAGING_CSMT */
2409 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2412 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2414 if (rt_mask
!= *cur_mask
)
2416 context_apply_draw_buffers(context
, rt_mask
);
2417 *cur_mask
= rt_mask
;
2418 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2421 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2423 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2426 if (context
->last_was_blit
)
2427 context
->last_was_blit
= FALSE
;
2429 /* Blending and clearing should be orthogonal, but tests on the nvidia
2430 * driver show that disabling blending when clearing improves the clearing
2431 * performance incredibly. */
2432 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2433 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2434 checkGLcall("glEnable GL_SCISSOR_TEST");
2436 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2437 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2438 context_invalidate_state(context
, STATE_SCISSORRECT
);
2443 #if defined(STAGING_CSMT)
2444 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_state
*state
)
2446 struct wined3d_rendertarget_view
**rts
= state
->fb
.render_targets
;
2447 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2448 DWORD rt_mask
, rt_mask_bits
;
2451 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2452 return context_generate_rt_mask_no_fbo(context
, wined3d_rendertarget_view_get_surface(rts
[0]));
2453 #else /* STAGING_CSMT */
2454 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_device
*device
)
2456 const struct wined3d_state
*state
= &device
->state
;
2457 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
2458 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2459 DWORD rt_mask
, rt_mask_bits
;
2462 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2463 return context_generate_rt_mask_no_fbo(device
, wined3d_rendertarget_view_get_surface(rts
[0]));
2464 #endif /* STAGING_CSMT */
2465 else if (!context
->render_offscreen
)
2466 return context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2468 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2469 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2470 rt_mask_bits
= rt_mask
;
2472 while (rt_mask_bits
)
2474 rt_mask_bits
&= ~(1 << i
);
2475 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
2476 rt_mask
&= ~(1 << i
);
2484 /* Context activation is done by the caller. */
2485 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2487 #if defined(STAGING_CSMT)
2488 const struct wined3d_fb_state
*fb
= &state
->fb
;
2489 DWORD rt_mask
= find_draw_buffers_mask(context
, state
);
2490 #else /* STAGING_CSMT */
2491 const struct wined3d_device
*device
= context
->swapchain
->device
;
2492 const struct wined3d_fb_state
*fb
= state
->fb
;
2493 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2494 #endif /* STAGING_CSMT */
2497 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2499 if (!context
->render_offscreen
)
2501 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2502 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2508 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
2510 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(fb
->render_targets
[i
]);
2512 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2513 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2514 fb
->render_targets
[0]->resource
->draw_binding
,
2515 fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
2519 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2520 if (rt_mask
!= *cur_mask
)
2522 context_apply_draw_buffers(context
, rt_mask
);
2523 *cur_mask
= rt_mask
;
2525 #if defined(STAGING_CSMT)
2527 wined3d_fb_copy(&context
->current_fb
, &state
->fb
);
2528 #endif /* STAGING_CSMT */
2531 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
2533 DWORD i
= context
->rev_tex_unit_map
[unit
];
2534 DWORD j
= context
->tex_unit_map
[stage
];
2536 context
->tex_unit_map
[stage
] = unit
;
2537 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2538 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2540 context
->rev_tex_unit_map
[unit
] = stage
;
2541 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2542 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2545 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
2549 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2550 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
2553 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
2554 const struct wined3d_state
*state
)
2558 context
->fixed_function_usage_map
= 0;
2559 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2561 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2562 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2563 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2564 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2565 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2566 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2567 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2568 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2570 /* Not used, and disable higher stages. */
2571 if (color_op
== WINED3D_TOP_DISABLE
)
2574 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2575 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2576 || ((color_arg3
== WINED3DTA_TEXTURE
)
2577 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2578 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2579 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2580 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2581 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2582 context
->fixed_function_usage_map
|= (1 << i
);
2584 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2585 && i
< MAX_TEXTURES
- 1)
2586 context
->fixed_function_usage_map
|= (1 << (i
+ 1));
2589 if (i
< context
->lowest_disabled_stage
)
2592 end
= context
->lowest_disabled_stage
;
2596 start
= context
->lowest_disabled_stage
;
2600 context
->lowest_disabled_stage
= i
;
2601 for (i
= start
+ 1; i
< end
; ++i
)
2603 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
2607 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
2608 const struct wined3d_state
*state
)
2610 unsigned int i
, tex
;
2612 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2614 context_update_fixed_function_usage_map(context
, state
);
2615 ffu_map
= context
->fixed_function_usage_map
;
2617 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2618 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2620 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2625 if (context
->tex_unit_map
[i
] != i
)
2627 context_map_stage(context
, i
, i
);
2628 context_invalidate_state(context
, STATE_SAMPLER(i
));
2629 context_invalidate_texture_stage(context
, i
);
2635 /* Now work out the mapping */
2637 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2642 if (context
->tex_unit_map
[i
] != tex
)
2644 context_map_stage(context
, i
, tex
);
2645 context_invalidate_state(context
, STATE_SAMPLER(i
));
2646 context_invalidate_texture_stage(context
, i
);
2653 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2655 const struct wined3d_shader_resource_info
*resource_info
=
2656 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2658 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2660 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2662 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
2664 context_map_stage(context
, i
, i
);
2665 context_invalidate_state(context
, STATE_SAMPLER(i
));
2666 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2667 context_invalidate_texture_stage(context
, i
);
2672 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
2673 const struct wined3d_shader_resource_info
*ps_resource_info
,
2674 const struct wined3d_shader_resource_info
*vs_resource_info
, DWORD unit
)
2676 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
2678 /* Not currently used */
2679 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
2682 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
2684 /* Used by a fragment sampler */
2686 if (!ps_resource_info
)
2688 /* No pixel shader, check fixed function */
2689 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1 << current_mapping
));
2692 /* Pixel shader, check the shader's sampler map */
2693 return !ps_resource_info
[current_mapping
].type
;
2696 /* Used by a vertex sampler */
2697 return !vs_resource_info
[current_mapping
- MAX_FRAGMENT_SAMPLERS
].type
;
2700 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
2702 const struct wined3d_shader_resource_info
*vs_resource_info
=
2703 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
2704 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
2705 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2706 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2709 /* Note that we only care if a resource is used or not, not the
2710 * resource's specific type. Otherwise we'd need to call
2711 * shader_update_samplers() here for 1.x pixelshaders. */
2713 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2715 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
2717 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2718 if (vs_resource_info
[i
].type
)
2720 if (context
->tex_unit_map
[vsampler_idx
] != WINED3D_UNMAPPED_STAGE
)
2722 /* Already mapped somewhere */
2728 if (context_unit_free_for_vs(context
, ps_resource_info
, vs_resource_info
, start
))
2730 context_map_stage(context
, vsampler_idx
, start
);
2731 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
2743 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2745 BOOL vs
= use_vs(state
);
2746 BOOL ps
= use_ps(state
);
2749 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2750 * that would be really messy and require shader recompilation
2751 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2752 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2755 context_map_psamplers(context
, state
);
2757 context_map_fixed_function_samplers(context
, state
);
2760 context_map_vsamplers(context
, ps
, state
);
2763 /* Context activation is done by the caller. */
2764 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2766 #if defined(STAGING_CSMT)
2767 DWORD rt_mask
, *cur_mask
;
2769 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2771 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2772 rt_mask
= find_draw_buffers_mask(context
, state
);
2773 #else /* STAGING_CSMT */
2774 const struct wined3d_device
*device
= context
->swapchain
->device
;
2775 DWORD rt_mask
, *cur_mask
;
2777 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2779 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2780 rt_mask
= find_draw_buffers_mask(context
, device
);
2781 #endif /* STAGING_CSMT */
2782 if (rt_mask
!= *cur_mask
)
2784 context_apply_draw_buffers(context
, rt_mask
);
2785 *cur_mask
= rt_mask
;
2789 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
2791 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
2792 *regnum
= WINED3D_FFP_POSITION
;
2793 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
2794 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
2795 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
2796 *regnum
= WINED3D_FFP_BLENDINDICES
;
2797 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
2798 *regnum
= WINED3D_FFP_NORMAL
;
2799 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
2800 *regnum
= WINED3D_FFP_PSIZE
;
2801 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
2802 *regnum
= WINED3D_FFP_DIFFUSE
;
2803 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
2804 *regnum
= WINED3D_FFP_SPECULAR
;
2805 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
2806 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
2809 FIXME("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
2817 /* Context activation is done by the caller. */
2818 void context_stream_info_from_declaration(struct wined3d_context
*context
,
2819 const struct wined3d_state
*state
, struct wined3d_stream_info
*stream_info
)
2821 /* We need to deal with frequency data! */
2822 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
2823 BOOL use_vshader
= use_vs(state
);
2824 BOOL generic_attributes
= context
->d3d_info
->ffp_generic_attributes
;
2827 stream_info
->use_map
= 0;
2828 stream_info
->swizzle_map
= 0;
2829 stream_info
->position_transformed
= declaration
->position_transformed
;
2831 /* Translate the declaration into strided data. */
2832 for (i
= 0; i
< declaration
->element_count
; ++i
)
2834 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
2835 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
2839 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
2840 element
, i
+ 1, declaration
->element_count
);
2842 if (!stream
->buffer
)
2845 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
2849 if (element
->output_slot
== WINED3D_OUTPUT_SLOT_UNUSED
)
2851 stride_used
= FALSE
;
2853 else if (element
->output_slot
== WINED3D_OUTPUT_SLOT_SEMANTIC
)
2855 /* TODO: Assuming vertexdeclarations are usually used with the
2856 * same or a similar shader, it might be worth it to store the
2857 * last used output slot and try that one first. */
2858 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
2859 element
->usage
, element
->usage_idx
, &idx
);
2863 idx
= element
->output_slot
;
2869 if (!generic_attributes
&& !element
->ffp_valid
)
2871 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
2872 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
2873 stride_used
= FALSE
;
2877 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
2883 TRACE("Load %s array %u [usage %s, usage_idx %u, "
2884 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
2885 use_vshader
? "shader": "fixed function", idx
,
2886 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
2887 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
),
2888 debug_d3dinput_classification(element
->input_slot_class
), element
->instance_data_step_rate
);
2890 stream_info
->elements
[idx
].format
= element
->format
;
2891 stream_info
->elements
[idx
].data
.buffer_object
= 0;
2892 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
2893 stream_info
->elements
[idx
].stride
= stream
->stride
;
2894 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
2895 if (stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
2897 stream_info
->elements
[idx
].divisor
= 1;
2899 else if (element
->input_slot_class
== WINED3D_INPUT_PER_INSTANCE_DATA
)
2901 stream_info
->elements
[idx
].divisor
= element
->instance_data_step_rate
;
2902 if (!element
->instance_data_step_rate
)
2903 FIXME("Instance step rate 0 not implemented.\n");
2907 stream_info
->elements
[idx
].divisor
= 0;
2910 if (!context
->gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2911 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
2913 stream_info
->swizzle_map
|= 1 << idx
;
2915 stream_info
->use_map
|= 1 << idx
;
2920 /* Context activation is done by the caller. */
2921 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2923 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2924 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2925 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
2926 DWORD prev_all_vbo
= stream_info
->all_vbo
;
2930 context_stream_info_from_declaration(context
, state
, stream_info
);
2932 stream_info
->all_vbo
= 1;
2933 context
->num_buffer_queries
= 0;
2934 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
2936 struct wined3d_stream_info_element
*element
;
2937 struct wined3d_bo_address data
;
2938 struct wined3d_buffer
*buffer
;
2943 element
= &stream_info
->elements
[i
];
2944 buffer
= state
->streams
[element
->stream_idx
].buffer
;
2946 /* We can't use VBOs if the base vertex index is negative. OpenGL
2947 * doesn't accept negative offsets (or rather offsets bigger than the
2948 * VBO, because the pointer is unsigned), so use system memory
2949 * sources. In most sane cases the pointer - offset will still be > 0,
2950 * otherwise it will wrap around to some big value. Hope that with the
2951 * indices the driver wraps it back internally. If not,
2952 * drawStridedSlow is needed, including a vertex buffer path. */
2953 if (state
->load_base_vertex_index
< 0)
2955 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
2956 state
->load_base_vertex_index
);
2957 element
->data
.buffer_object
= 0;
2958 element
->data
.addr
+= (ULONG_PTR
)buffer_get_sysmem(buffer
, context
);
2959 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
2960 FIXME("System memory vertex data load offset is negative!\n");
2964 buffer_internal_preload(buffer
, context
, state
);
2965 buffer_get_memory(buffer
, context
, &data
);
2966 element
->data
.buffer_object
= data
.buffer_object
;
2967 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
2970 if (!element
->data
.buffer_object
)
2971 stream_info
->all_vbo
= 0;
2974 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
2976 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
2981 if (state
->vertex_declaration
->half_float_conv_needed
&& !stream_info
->all_vbo
)
2983 #if defined(STAGING_CSMT)
2984 TRACE("Using draw_strided_slow with vertex shaders for FLOAT16 conversion.\n");
2985 #else /* STAGING_CSMT */
2986 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
2987 #endif /* STAGING_CSMT */
2988 context
->use_immediate_mode_draw
= TRUE
;
2992 context
->use_immediate_mode_draw
= FALSE
;
2997 WORD slow_mask
= (1 << WINED3D_FFP_PSIZE
);
2998 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2999 & ((1 << WINED3D_FFP_DIFFUSE
) | (1 << WINED3D_FFP_SPECULAR
));
3001 if (((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
3002 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
3003 context
->use_immediate_mode_draw
= TRUE
;
3005 context
->use_immediate_mode_draw
= FALSE
;
3008 if (prev_all_vbo
!= stream_info
->all_vbo
)
3009 context_invalidate_state(context
, STATE_INDEXBUFFER
);
3012 /* Context activation is done by the caller. */
3013 static void context_preload_texture(struct wined3d_context
*context
,
3014 const struct wined3d_state
*state
, unsigned int idx
)
3016 struct wined3d_texture
*texture
;
3018 if (!(texture
= state
->textures
[idx
]))
3021 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
3024 /* Context activation is done by the caller. */
3025 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3031 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3033 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
3034 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
3040 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3042 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
3043 context_preload_texture(context
, state
, i
);
3048 WORD ffu_map
= context
->fixed_function_usage_map
;
3050 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3053 context_preload_texture(context
, state
, i
);
3058 static void context_bind_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3060 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3061 struct wined3d_shader_sampler_map_entry
*entry
;
3062 struct wined3d_shader_resource_view
*view
;
3063 struct wined3d_sampler
*sampler
;
3064 struct wined3d_texture
*texture
;
3065 struct wined3d_shader
*shader
;
3066 unsigned int i
, j
, count
;
3070 enum wined3d_shader_type type
;
3071 unsigned int base_idx
;
3076 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
3077 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
3080 for (i
= 0; i
< ARRAY_SIZE(shader_types
); ++i
)
3082 if (!(shader
= state
->shader
[shader_types
[i
].type
]))
3085 count
= shader
->reg_maps
.sampler_map
.count
;
3086 if (count
> shader_types
[i
].count
)
3088 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3089 shader
, count
, shader_types
[i
].count
);
3090 count
= shader_types
[i
].count
;
3093 for (j
= 0; j
< count
; ++j
)
3095 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3097 if (!(view
= state
->shader_resource_view
[shader_types
[i
].type
][entry
->resource_idx
]))
3099 WARN("No resource view bound at index %u, %u.\n", shader_types
[i
].type
, entry
->resource_idx
);
3103 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3105 FIXME("Buffer shader resources not supported.\n");
3109 if (!(sampler
= state
->sampler
[shader_types
[i
].type
][entry
->sampler_idx
]))
3111 WARN("No sampler object bound at index %u, %u.\n", shader_types
[i
].type
, entry
->sampler_idx
);
3115 texture
= wined3d_texture_from_resource(view
->resource
);
3116 wined3d_texture_load(texture
, context
, FALSE
);
3117 context_active_texture(context
, gl_info
, shader_types
[i
].base_idx
+ entry
->bind_idx
);
3118 wined3d_texture_bind(texture
, context
, FALSE
);
3120 GL_EXTCALL(glBindSampler(shader_types
[i
].base_idx
+ entry
->bind_idx
, sampler
->name
));
3121 checkGLcall("glBindSampler");
3126 /* Context activation is done by the caller. */
3127 #if defined(STAGING_CSMT)
3128 BOOL
context_apply_draw_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
3129 const struct wined3d_state
*state
)
3131 const struct StateEntry
*state_table
= context
->state_table
;
3132 const struct wined3d_fb_state
*fb
= &state
->fb
;
3133 #else /* STAGING_CSMT */
3134 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
3136 const struct wined3d_state
*state
= &device
->state
;
3137 const struct StateEntry
*state_table
= context
->state_table
;
3138 const struct wined3d_fb_state
*fb
= state
->fb
;
3139 #endif /* STAGING_CSMT */
3143 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
3144 fb
->render_targets
, fb
->depth_stencil
))
3147 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3149 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3152 /* Preload resources before FBO setup. Texture preload in particular may
3153 * result in changes to the current FBO, due to using e.g. FBO blits for
3154 * updating a resource location. */
3155 context_update_tex_unit_map(context
, state
);
3156 context_preload_textures(context
, state
);
3157 /* TODO: Right now the dependency on the vertex shader is necessary
3158 * since context_stream_info_from_declaration depends on the reg_maps of
3159 * the current VS but maybe it's possible to relax the coupling in some
3160 * situations at least. */
3161 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
3162 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
3164 context_update_stream_info(context
, state
);
3168 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3171 #if defined(STAGING_CSMT)
3172 buffer_internal_preload(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
,
3175 /* PreLoad may kick buffers out of vram. */
3176 if (isStateDirty(context
, STATE_STREAMSRC
))
3177 context_update_stream_info(context
, state
);
3178 #else /* STAGING_CSMT */
3179 buffer_mark_used(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
);
3181 #endif /* STAGING_CSMT */
3183 if (state
->index_buffer
)
3185 if (context
->stream_info
.all_vbo
)
3186 buffer_internal_preload(state
->index_buffer
, context
, state
);
3188 buffer_get_sysmem(state
->index_buffer
, context
);
3191 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3193 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3195 if (state
->cb
[i
][j
])
3196 buffer_internal_preload(state
->cb
[i
][j
], context
, state
);
3200 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3202 DWORD rep
= context
->dirtyArray
[i
];
3203 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3204 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3205 context
->isStateDirty
[idx
] &= ~(1 << shift
);
3206 state_table
[rep
].apply(context
, state
, rep
);
3209 if (context
->shader_update_mask
)
3211 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3212 context
->shader_update_mask
= 0;
3215 if (context
->constant_update_mask
)
3217 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3218 context
->constant_update_mask
= 0;
3221 if (context
->update_shader_resource_bindings
)
3223 context_bind_shader_resources(context
, state
);
3224 context
->update_shader_resource_bindings
= 0;
3227 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3229 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3232 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3233 context
->last_was_blit
= FALSE
;
3238 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
3240 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3242 render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
3243 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
3245 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3246 * the alpha blend state changes with different render target formats. */
3247 if (!context
->current_rt
)
3249 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3253 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
3254 const struct wined3d_format
*new = target
->resource
.format
;
3256 if (old
->id
!= new->id
)
3258 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3259 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3260 || !(target
->container
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3261 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3263 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3264 if ((context
->current_rt
->container
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
3265 != (target
->container
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3266 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3269 /* When switching away from an offscreen render target, and we're not
3270 * using FBOs, we have to read the drawable into the texture. This is
3271 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
3272 * There are some things that need care though. PreLoad needs a GL context,
3273 * and FindContext is called before the context is activated. It also
3274 * has to be called with the old rendertarget active, otherwise a
3275 * wrong drawable is read. */
3276 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
3277 && old_render_offscreen
&& context
->current_rt
!= target
)
3279 struct wined3d_texture
*texture
= context
->current_rt
->container
;
3281 /* Read the back buffer of the old drawable into the destination texture. */
3282 if (texture
->texture_srgb
.name
)
3283 wined3d_texture_load(texture
, context
, TRUE
);
3284 wined3d_texture_load(texture
, context
, FALSE
);
3285 #if defined(STAGING_CSMT)
3286 wined3d_resource_invalidate_location(&context
->current_rt
->resource
, WINED3D_LOCATION_DRAWABLE
);
3287 #else /* STAGING_CSMT */
3288 surface_invalidate_location(context
->current_rt
, WINED3D_LOCATION_DRAWABLE
);
3289 #endif /* STAGING_CSMT */
3293 context
->current_rt
= target
;
3294 context_set_render_offscreen(context
, render_offscreen
);
3297 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
3299 struct wined3d_context
*current_context
= context_get_current();
3300 struct wined3d_context
*context
;
3302 TRACE("device %p, target %p.\n", device
, target
);
3304 if (current_context
&& current_context
->destroyed
)
3305 current_context
= NULL
;
3310 && current_context
->current_rt
3311 && current_context
->swapchain
->device
== device
)
3313 target
= current_context
->current_rt
;
3317 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
3318 if (swapchain
->back_buffers
)
3319 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
3321 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
3325 if (current_context
&& current_context
->current_rt
== target
)
3327 context
= current_context
;
3329 else if (target
->container
->swapchain
)
3331 TRACE("Rendering onscreen.\n");
3333 context
= swapchain_get_context(target
->container
->swapchain
);
3337 TRACE("Rendering offscreen.\n");
3339 /* Stay with the current context if possible. Otherwise use the
3340 * context for the primary swapchain. */
3341 if (current_context
&& current_context
->swapchain
->device
== device
)
3342 context
= current_context
;
3344 context
= swapchain_get_context(device
->swapchains
[0]);
3347 context_enter(context
);
3348 context_update_window(context
);
3349 context_setup_target(context
, target
);
3350 if (!context
->valid
) return context
;
3352 if (context
!= current_context
)
3354 if (!context_set_current(context
))
3355 ERR("Failed to activate the new context.\n");
3357 else if (context
->needs_set
)
3359 context_set_gl_context(context
);