2 * Context and render target management in wined3d
4 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
5 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
33 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous
);
36 #define WINED3D_MAX_FBO_ENTRIES 64
38 static DWORD wined3d_context_tls_idx
;
40 /* FBO helper functions */
42 /* Context activation is done by the caller. */
43 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint fbo
)
45 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
49 case GL_READ_FRAMEBUFFER
:
50 if (context
->fbo_read_binding
== fbo
) return;
51 context
->fbo_read_binding
= fbo
;
54 case GL_DRAW_FRAMEBUFFER
:
55 if (context
->fbo_draw_binding
== fbo
) return;
56 context
->fbo_draw_binding
= fbo
;
60 if (context
->fbo_read_binding
== fbo
61 && context
->fbo_draw_binding
== fbo
) return;
62 context
->fbo_read_binding
= fbo
;
63 context
->fbo_draw_binding
= fbo
;
67 FIXME("Unhandled target %#x.\n", target
);
71 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
72 checkGLcall("glBindFramebuffer()");
75 /* Context activation is done by the caller. */
76 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
80 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
82 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
83 checkGLcall("glFramebufferTexture2D()");
85 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
86 checkGLcall("glFramebufferTexture2D()");
88 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
89 checkGLcall("glFramebufferTexture2D()");
92 /* Context activation is done by the caller. */
93 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint fbo
)
95 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
97 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
98 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
99 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
101 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
102 checkGLcall("glDeleteFramebuffers()");
105 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
106 GLenum fbo_target
, DWORD format_flags
, GLuint rb
)
108 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
110 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
111 checkGLcall("glFramebufferRenderbuffer()");
114 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
116 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
117 checkGLcall("glFramebufferRenderbuffer()");
121 /* Context activation is done by the caller. */
122 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
123 GLenum fbo_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
125 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
127 TRACE("Attach depth stencil %p\n", depth_stencil
);
131 DWORD format_flags
= depth_stencil
->container
->resource
.format_flags
;
133 if (depth_stencil
->current_renderbuffer
)
135 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
136 format_flags
, depth_stencil
->current_renderbuffer
->id
);
142 case WINED3D_LOCATION_TEXTURE_RGB
:
143 case WINED3D_LOCATION_TEXTURE_SRGB
:
144 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
146 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
147 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
148 depth_stencil
->texture_level
);
149 checkGLcall("glFramebufferTexture2D()");
152 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
154 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
155 depth_stencil
->texture_target
, depth_stencil
->container
->texture_rgb
.name
,
156 depth_stencil
->texture_level
);
157 checkGLcall("glFramebufferTexture2D()");
161 case WINED3D_LOCATION_RB_MULTISAMPLE
:
162 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
163 format_flags
, depth_stencil
->rb_multisample
);
166 case WINED3D_LOCATION_RB_RESOLVED
:
167 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
168 format_flags
, depth_stencil
->rb_resolved
);
172 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
177 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
179 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
180 checkGLcall("glFramebufferTexture2D()");
183 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
185 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
186 checkGLcall("glFramebufferTexture2D()");
191 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
192 checkGLcall("glFramebufferTexture2D()");
194 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
195 checkGLcall("glFramebufferTexture2D()");
199 /* Context activation is done by the caller. */
200 static void context_attach_surface_fbo(struct wined3d_context
*context
,
201 GLenum fbo_target
, DWORD idx
, struct wined3d_surface
*surface
, DWORD location
)
203 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
205 TRACE("Attach surface %p to %u\n", surface
, idx
);
207 if (surface
&& surface
->resource
.format
->id
!= WINED3DFMT_NULL
)
213 case WINED3D_LOCATION_TEXTURE_RGB
:
214 case WINED3D_LOCATION_TEXTURE_SRGB
:
215 srgb
= location
== WINED3D_LOCATION_TEXTURE_SRGB
;
216 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
217 surface
->texture_target
, surface_get_texture_name(surface
, gl_info
, srgb
),
218 surface
->texture_level
);
219 checkGLcall("glFramebufferTexture2D()");
222 case WINED3D_LOCATION_RB_MULTISAMPLE
:
223 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
224 GL_RENDERBUFFER
, surface
->rb_multisample
);
225 checkGLcall("glFramebufferRenderbuffer()");
228 case WINED3D_LOCATION_RB_RESOLVED
:
229 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
230 GL_RENDERBUFFER
, surface
->rb_resolved
);
231 checkGLcall("glFramebufferRenderbuffer()");
235 ERR("Unsupported location %s (%#x).\n", wined3d_debug_location(location
), location
);
241 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
242 checkGLcall("glFramebufferTexture2D()");
246 /* Context activation is done by the caller. */
247 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
249 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
252 if (!FIXME_ON(d3d
)) return;
254 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
255 if (status
== GL_FRAMEBUFFER_COMPLETE
)
257 TRACE("FBO complete\n");
261 const struct wined3d_surface
*attachment
;
264 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
266 if (!context
->current_fbo
)
268 ERR("FBO 0 is incomplete, driver bug?\n");
272 FIXME("\tColor Location %s (%#x).\n", wined3d_debug_location(context
->current_fbo
->color_location
),
273 context
->current_fbo
->color_location
);
274 FIXME("\tDepth Stencil Location %s (%#x).\n", wined3d_debug_location(context
->current_fbo
->ds_location
),
275 context
->current_fbo
->ds_location
);
277 /* Dump the FBO attachments */
278 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
280 attachment
= context
->current_fbo
->render_targets
[i
];
283 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
284 i
, attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
285 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
288 attachment
= context
->current_fbo
->depth_stencil
;
291 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
292 attachment
, debug_d3dformat(attachment
->resource
.format
->id
),
293 attachment
->pow2Width
, attachment
->pow2Height
, attachment
->resource
.multisample_type
);
298 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
300 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
301 return buffer
? (1u << 31) | buffer
: 0;
304 static inline DWORD
context_generate_rt_mask_from_surface(const struct wined3d_surface
*target
)
306 return (1u << 31) | surface_get_gl_buffer(target
);
309 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
310 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
311 DWORD color_location
, DWORD ds_location
)
313 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
314 struct fbo_entry
*entry
;
316 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
317 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
318 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
319 entry
->depth_stencil
= depth_stencil
;
320 entry
->color_location
= color_location
;
321 entry
->ds_location
= ds_location
;
322 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
323 entry
->attached
= FALSE
;
324 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
325 checkGLcall("glGenFramebuffers()");
326 TRACE("Created FBO %u.\n", entry
->id
);
331 /* Context activation is done by the caller. */
332 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
333 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
334 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
336 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
338 context_bind_fbo(context
, target
, entry
->id
);
339 context_clean_fbo_attachments(gl_info
, target
);
341 memcpy(entry
->render_targets
, render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
342 entry
->depth_stencil
= depth_stencil
;
343 entry
->color_location
= color_location
;
344 entry
->ds_location
= ds_location
;
345 entry
->attached
= FALSE
;
348 /* Context activation is done by the caller. */
349 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
353 TRACE("Destroy FBO %u.\n", entry
->id
);
354 context_destroy_fbo(context
, entry
->id
);
356 --context
->fbo_entry_count
;
357 list_remove(&entry
->entry
);
358 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
359 HeapFree(GetProcessHeap(), 0, entry
);
362 /* Context activation is done by the caller. */
363 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
364 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
365 DWORD color_location
, DWORD ds_location
)
367 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
368 struct fbo_entry
*entry
;
370 if (depth_stencil
&& render_targets
&& render_targets
[0])
372 if (depth_stencil
->resource
.width
< render_targets
[0]->resource
.width
||
373 depth_stencil
->resource
.height
< render_targets
[0]->resource
.height
)
375 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
376 depth_stencil
= NULL
;
380 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
382 if (!memcmp(entry
->render_targets
,
383 render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
384 && entry
->depth_stencil
== depth_stencil
&& entry
->color_location
== color_location
385 && entry
->ds_location
== ds_location
)
387 list_remove(&entry
->entry
);
388 list_add_head(&context
->fbo_list
, &entry
->entry
);
393 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
395 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, color_location
, ds_location
);
396 list_add_head(&context
->fbo_list
, &entry
->entry
);
397 ++context
->fbo_entry_count
;
401 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
402 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, color_location
, ds_location
, entry
);
403 list_remove(&entry
->entry
);
404 list_add_head(&context
->fbo_list
, &entry
->entry
);
410 /* Context activation is done by the caller. */
411 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
413 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
415 GLuint read_binding
, draw_binding
;
416 struct wined3d_surface
*depth_stencil
= entry
->depth_stencil
;
420 context_bind_fbo(context
, target
, entry
->id
);
424 read_binding
= context
->fbo_read_binding
;
425 draw_binding
= context
->fbo_draw_binding
;
426 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
428 /* Apply render targets */
429 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
431 context_attach_surface_fbo(context
, target
, i
, entry
->render_targets
[i
], entry
->color_location
);
434 if (depth_stencil
&& entry
->render_targets
[0]
435 && (depth_stencil
->resource
.multisample_type
436 != entry
->render_targets
[0]->resource
.multisample_type
437 || depth_stencil
->resource
.multisample_quality
438 != entry
->render_targets
[0]->resource
.multisample_quality
))
440 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
441 entry
->render_targets
[0]->resource
.multisample_quality
,
442 entry
->render_targets
[0]->resource
.multisample_type
,
443 depth_stencil
->resource
.multisample_quality
, depth_stencil
->resource
.multisample_type
);
444 depth_stencil
= NULL
;
448 surface_set_compatible_renderbuffer(depth_stencil
, entry
->render_targets
[0]);
449 context_attach_depth_stencil_fbo(context
, target
, depth_stencil
, entry
->ds_location
);
451 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
452 * GL contexts requirements. */
453 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
454 context_set_draw_buffer(context
, GL_NONE
);
455 if (target
!= GL_FRAMEBUFFER
)
457 if (target
== GL_READ_FRAMEBUFFER
)
458 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
460 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
463 entry
->attached
= TRUE
;
466 /* Context activation is done by the caller. */
467 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
468 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
469 DWORD color_location
, DWORD ds_location
)
471 struct fbo_entry
*entry
, *entry2
;
473 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
475 context_destroy_fbo_entry(context
, entry
);
478 if (context
->rebind_fbo
)
480 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
481 context
->rebind_fbo
= FALSE
;
484 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
486 context
->current_fbo
= NULL
;
487 context_bind_fbo(context
, target
, 0);
491 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
,
492 color_location
, ds_location
);
493 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
497 /* Context activation is done by the caller. */
498 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
499 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
501 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
503 context
->blit_targets
[0] = render_target
;
505 memset(&context
->blit_targets
[1], 0, clear_size
);
506 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
, location
);
509 /* Context activation is done by the caller. */
510 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
512 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
514 if (context
->free_occlusion_query_count
)
516 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
520 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
522 GL_EXTCALL(glGenQueries(1, &query
->id
));
523 checkGLcall("glGenQueries");
525 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
529 WARN("Occlusion queries not supported, not allocating query id.\n");
534 query
->context
= context
;
535 list_add_head(&context
->occlusion_queries
, &query
->entry
);
538 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
540 struct wined3d_context
*context
= query
->context
;
542 list_remove(&query
->entry
);
543 query
->context
= NULL
;
545 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
547 UINT new_size
= context
->free_occlusion_query_size
<< 1;
548 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
549 new_size
* sizeof(*context
->free_occlusion_queries
));
553 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
557 context
->free_occlusion_query_size
= new_size
;
558 context
->free_occlusion_queries
= new_data
;
561 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
564 /* Context activation is done by the caller. */
565 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
567 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
569 if (context
->free_event_query_count
)
571 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
575 if (gl_info
->supported
[ARB_SYNC
])
577 /* Using ARB_sync, not much to do here. */
578 query
->object
.sync
= NULL
;
579 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
581 else if (gl_info
->supported
[APPLE_FENCE
])
583 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
584 checkGLcall("glGenFencesAPPLE");
586 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
588 else if(gl_info
->supported
[NV_FENCE
])
590 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
591 checkGLcall("glGenFencesNV");
593 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
597 WARN("Event queries not supported, not allocating query id.\n");
598 query
->object
.id
= 0;
602 query
->context
= context
;
603 list_add_head(&context
->event_queries
, &query
->entry
);
606 void context_free_event_query(struct wined3d_event_query
*query
)
608 struct wined3d_context
*context
= query
->context
;
610 list_remove(&query
->entry
);
611 query
->context
= NULL
;
613 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
615 UINT new_size
= context
->free_event_query_size
<< 1;
616 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
617 new_size
* sizeof(*context
->free_event_queries
));
621 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
625 context
->free_event_query_size
= new_size
;
626 context
->free_event_queries
= new_data
;
629 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
632 /* Context activation is done by the caller. */
633 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
635 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
637 if (context
->free_timestamp_query_count
)
639 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
643 GL_EXTCALL(glGenQueries(1, &query
->id
));
644 checkGLcall("glGenQueries");
646 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
649 query
->context
= context
;
650 list_add_head(&context
->timestamp_queries
, &query
->entry
);
653 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
655 struct wined3d_context
*context
= query
->context
;
657 list_remove(&query
->entry
);
658 query
->context
= NULL
;
660 if (context
->free_timestamp_query_count
>= context
->free_timestamp_query_size
- 1)
662 UINT new_size
= context
->free_timestamp_query_size
<< 1;
663 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_timestamp_queries
,
664 new_size
* sizeof(*context
->free_timestamp_queries
));
668 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
672 context
->free_timestamp_query_size
= new_size
;
673 context
->free_timestamp_queries
= new_data
;
676 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
679 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
681 static void context_enum_surface_fbo_entries(const struct wined3d_device
*device
,
682 const struct wined3d_surface
*surface
, context_fbo_entry_func_t
*callback
)
686 for (i
= 0; i
< device
->context_count
; ++i
)
688 struct wined3d_context
*context
= device
->contexts
[i
];
689 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
690 struct fbo_entry
*entry
, *entry2
;
692 if (context
->current_rt
== surface
) context
->current_rt
= NULL
;
694 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
698 if (entry
->depth_stencil
== surface
)
700 callback(context
, entry
);
704 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
706 if (entry
->render_targets
[j
] == surface
)
708 callback(context
, entry
);
716 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
718 list_remove(&entry
->entry
);
719 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
722 void context_resource_released(const struct wined3d_device
*device
,
723 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
725 if (!device
->d3d_initialized
) return;
729 case WINED3D_RTYPE_SURFACE
:
730 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
731 context_queue_fbo_entry_destruction
);
739 static void context_detach_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
741 entry
->attached
= FALSE
;
744 void context_resource_unloaded(const struct wined3d_device
*device
,
745 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
749 case WINED3D_RTYPE_SURFACE
:
750 context_enum_surface_fbo_entries(device
, surface_from_resource(resource
),
751 context_detach_fbo_entry
);
759 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
761 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
762 struct fbo_entry
*entry
= context
->current_fbo
;
765 if (!entry
|| context
->rebind_fbo
) return;
767 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
769 if (surface
== entry
->render_targets
[i
])
771 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface
, i
);
772 context
->rebind_fbo
= TRUE
;
777 if (surface
== entry
->depth_stencil
)
779 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface
);
780 context
->rebind_fbo
= TRUE
;
784 static BOOL
context_restore_pixel_format(struct wined3d_context
*ctx
)
786 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
789 if (ctx
->restore_pf
&& IsWindow(ctx
->restore_pf_win
))
791 if (ctx
->gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
793 HDC dc
= GetDC(ctx
->restore_pf_win
);
796 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, ctx
->restore_pf
))))
798 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
799 ctx
->restore_pf
, ctx
->restore_pf_win
);
801 ReleaseDC(ctx
->restore_pf_win
, dc
);
806 ERR("can't restore pixel format %d on window %p\n", ctx
->restore_pf
, ctx
->restore_pf_win
);
811 ctx
->restore_pf_win
= NULL
;
815 static BOOL
context_set_pixel_format(struct wined3d_context
*context
, HDC dc
, BOOL
private, int format
)
817 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
820 if (dc
== context
->hdc
&& context
->hdc_is_private
&& context
->hdc_has_format
)
823 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
824 if (current
== format
) goto success
;
828 if (!SetPixelFormat(dc
, format
, NULL
))
830 /* This may also happen if the dc belongs to a destroyed window. */
831 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
832 format
, dc
, GetLastError());
836 context
->restore_pf
= 0;
837 context
->restore_pf_win
= private ? NULL
: WindowFromDC(dc
);
841 /* By default WGL doesn't allow pixel format adjustments but we need it
842 * here. For this reason there's a Wine specific wglSetPixelFormat()
843 * which allows us to set the pixel format multiple times. Only use it
844 * when really needed. */
845 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
849 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
851 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
856 win
= private ? NULL
: WindowFromDC(dc
);
857 if (win
!= context
->restore_pf_win
)
859 context_restore_pixel_format(context
);
861 context
->restore_pf
= private ? 0 : current
;
862 context
->restore_pf_win
= win
;
868 /* OpenGL doesn't allow pixel format adjustments. Print an error and
869 * continue using the old format. There's a big chance that the old
870 * format works although with a performance hit and perhaps rendering
872 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
873 format
, dc
, current
);
877 if (dc
== context
->hdc
&& context
->hdc_is_private
)
878 context
->hdc_has_format
= TRUE
;
882 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
884 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
887 if (!context_set_pixel_format(ctx
, ctx
->hdc
, ctx
->hdc_is_private
, ctx
->pixel_format
))
889 WARN("Failed to set pixel format %d on device context %p.\n",
890 ctx
->pixel_format
, ctx
->hdc
);
894 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
898 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
899 ctx
->glCtx
, ctx
->hdc
, GetLastError());
901 WARN("Trying fallback to the backup window.\n");
903 /* FIXME: If the context is destroyed it's no longer associated with
904 * a swapchain, so we can't use the swapchain to get a backup dc. To
905 * make this work windowless contexts would need to be handled by the
909 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
910 context_set_current(NULL
);
914 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
916 context_set_current(NULL
);
920 if (!context_set_pixel_format(ctx
, dc
, TRUE
, ctx
->pixel_format
))
922 ERR("Failed to set pixel format %d on device context %p.\n",
923 ctx
->pixel_format
, dc
);
924 context_set_current(NULL
);
928 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
930 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
932 context_set_current(NULL
);
942 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
944 if (!wglMakeCurrent(dc
, gl_ctx
))
946 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
947 gl_ctx
, dc
, GetLastError());
948 context_set_current(NULL
);
952 static void context_update_window(struct wined3d_context
*context
)
954 if (context
->win_handle
== context
->swapchain
->win_handle
)
957 TRACE("Updating context %p window from %p to %p.\n",
958 context
, context
->win_handle
, context
->swapchain
->win_handle
);
961 wined3d_release_dc(context
->win_handle
, context
->hdc
);
963 context
->win_handle
= context
->swapchain
->win_handle
;
964 context
->hdc_is_private
= FALSE
;
965 context
->hdc_has_format
= FALSE
;
966 context
->needs_set
= 1;
969 if (!(context
->hdc
= GetDC(context
->win_handle
)))
971 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
976 static void context_destroy_gl_resources(struct wined3d_context
*context
)
978 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
979 struct wined3d_timestamp_query
*timestamp_query
;
980 struct wined3d_occlusion_query
*occlusion_query
;
981 struct wined3d_event_query
*event_query
;
982 struct fbo_entry
*entry
, *entry2
;
987 restore_ctx
= wglGetCurrentContext();
988 restore_dc
= wglGetCurrentDC();
990 if (restore_ctx
== context
->glCtx
)
992 else if (context
->valid
)
993 context_set_gl_context(context
);
995 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
998 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
999 timestamp_query
->context
= NULL
;
1002 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1004 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1005 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1006 occlusion_query
->context
= NULL
;
1009 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
1013 if (gl_info
->supported
[ARB_SYNC
])
1015 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
1017 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
1018 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
1020 event_query
->context
= NULL
;
1023 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
1025 if (!context
->valid
) entry
->id
= 0;
1026 context_destroy_fbo_entry(context
, entry
);
1029 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1031 if (!context
->valid
) entry
->id
= 0;
1032 context_destroy_fbo_entry(context
, entry
);
1037 if (context
->dummy_arbfp_prog
)
1039 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
1042 if (gl_info
->supported
[ARB_TIMER_QUERY
])
1043 GL_EXTCALL(glDeleteQueries(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
1045 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1046 GL_EXTCALL(glDeleteQueries(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
1048 if (gl_info
->supported
[ARB_SYNC
])
1050 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1052 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
1055 else if (gl_info
->supported
[APPLE_FENCE
])
1057 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1059 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1062 else if (gl_info
->supported
[NV_FENCE
])
1064 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1066 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1070 checkGLcall("context cleanup");
1073 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1074 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1075 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1077 context_restore_pixel_format(context
);
1080 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1082 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1084 ERR("Failed to disable GL context.\n");
1087 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1089 if (!wglDeleteContext(context
->glCtx
))
1091 DWORD err
= GetLastError();
1092 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1096 DWORD
context_get_tls_idx(void)
1098 return wined3d_context_tls_idx
;
1101 void context_set_tls_idx(DWORD idx
)
1103 wined3d_context_tls_idx
= idx
;
1106 struct wined3d_context
*context_get_current(void)
1108 return TlsGetValue(wined3d_context_tls_idx
);
1111 BOOL
context_set_current(struct wined3d_context
*ctx
)
1113 struct wined3d_context
*old
= context_get_current();
1117 TRACE("Already using D3D context %p.\n", ctx
);
1125 TRACE("Switching away from destroyed context %p.\n", old
);
1126 context_destroy_gl_resources(old
);
1127 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1128 HeapFree(GetProcessHeap(), 0, old
);
1140 ERR("Trying to make invalid context %p current\n", ctx
);
1144 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1145 if (!context_set_gl_context(ctx
))
1149 else if(wglGetCurrentContext())
1151 TRACE("Clearing current D3D context.\n");
1152 if (!wglMakeCurrent(NULL
, NULL
))
1154 DWORD err
= GetLastError();
1155 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1156 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1161 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1164 void context_release(struct wined3d_context
*context
)
1166 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1170 if (!context
->level
)
1171 WARN("Context %p is not active.\n", context
);
1172 else if (context
!= context_get_current())
1173 WARN("Context %p is not the current context.\n", context
);
1176 if (!--context
->level
)
1178 if (context_restore_pixel_format(context
))
1179 context
->needs_set
= 1;
1180 if (context
->restore_ctx
)
1182 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1183 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
);
1184 context
->restore_ctx
= NULL
;
1185 context
->restore_dc
= NULL
;
1190 /* This is used when a context for render target A is active, but a separate context is
1191 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1192 * A to avoid breaking caller code. */
1193 void context_restore(struct wined3d_context
*context
, struct wined3d_surface
*restore
)
1195 if (context
->current_rt
!= restore
)
1197 context_release(context
);
1198 context
= context_acquire(restore
->resource
.device
, restore
);
1201 context_release(context
);
1204 static void context_enter(struct wined3d_context
*context
)
1206 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1208 if (!context
->level
++)
1210 const struct wined3d_context
*current_context
= context_get_current();
1211 HGLRC current_gl
= wglGetCurrentContext();
1213 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1215 TRACE("Another GL context (%p on device context %p) is already current.\n",
1216 current_gl
, wglGetCurrentDC());
1217 context
->restore_ctx
= current_gl
;
1218 context
->restore_dc
= wglGetCurrentDC();
1219 context
->needs_set
= 1;
1221 else if (!context
->needs_set
&& !(context
->hdc_is_private
&& context
->hdc_has_format
)
1222 && context
->pixel_format
!= context
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context
->hdc
))
1223 context
->needs_set
= 1;
1227 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1229 DWORD rep
= context
->state_table
[state
].representative
;
1233 if (isStateDirty(context
, rep
)) return;
1235 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1236 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1237 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1238 context
->isStateDirty
[idx
] |= (1u << shift
);
1241 /* This function takes care of wined3d pixel format selection. */
1242 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1243 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1244 BOOL auxBuffers
, BOOL findCompatible
)
1247 unsigned int current_value
;
1248 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1251 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1252 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1253 auxBuffers
, findCompatible
);
1256 for (i
= 0; i
< cfg_count
; ++i
)
1258 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1261 /* For now only accept RGBA formats. Perhaps some day we will
1262 * allow floating point formats for pbuffers. */
1263 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1265 /* In window mode we need a window drawable format and double buffering. */
1266 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1268 if (cfg
->redSize
< color_format
->red_size
)
1270 if (cfg
->greenSize
< color_format
->green_size
)
1272 if (cfg
->blueSize
< color_format
->blue_size
)
1274 if (cfg
->alphaSize
< color_format
->alpha_size
)
1276 if (cfg
->depthSize
< ds_format
->depth_size
)
1278 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1280 /* Check multisampling support. */
1281 if (cfg
->numSamples
)
1285 /* We try to locate a format which matches our requirements exactly. In case of
1286 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1287 if (cfg
->depthSize
== ds_format
->depth_size
)
1289 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1291 if (cfg
->alphaSize
== color_format
->alpha_size
)
1293 /* We like to have aux buffers in backbuffer mode */
1294 if (auxBuffers
&& cfg
->auxBuffers
)
1296 if (cfg
->redSize
== color_format
->red_size
1297 && cfg
->greenSize
== color_format
->green_size
1298 && cfg
->blueSize
== color_format
->blue_size
)
1301 if (value
> current_value
)
1303 iPixelFormat
= cfg
->iPixelFormat
;
1304 current_value
= value
;
1308 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1309 if(!iPixelFormat
&& !findCompatible
) {
1310 ERR("Can't find a suitable iPixelFormat\n");
1312 } else if(!iPixelFormat
) {
1313 PIXELFORMATDESCRIPTOR pfd
;
1315 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1316 /* PixelFormat selection */
1317 ZeroMemory(&pfd
, sizeof(pfd
));
1318 pfd
.nSize
= sizeof(pfd
);
1320 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1321 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1322 pfd
.cAlphaBits
= color_format
->alpha_size
;
1323 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1324 + color_format
->blue_size
+ color_format
->alpha_size
;
1325 pfd
.cDepthBits
= ds_format
->depth_size
;
1326 pfd
.cStencilBits
= ds_format
->stencil_size
;
1327 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1329 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1331 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1332 ERR("Can't find a suitable iPixelFormat\n");
1337 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1338 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1339 return iPixelFormat
;
1342 /* Context activation is done by the caller. */
1343 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1345 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1346 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1348 for (i
= 0; i
< count
; ++i
)
1350 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1351 checkGLcall("glActiveTexture");
1353 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1354 checkGLcall("glBindTexture");
1356 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1358 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1359 checkGLcall("glBindTexture");
1362 if (gl_info
->supported
[EXT_TEXTURE3D
])
1364 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1365 checkGLcall("glBindTexture");
1368 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1370 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1371 checkGLcall("glBindTexture");
1376 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1378 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1379 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1382 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1383 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1387 case GL_DEBUG_TYPE_ERROR_ARB
:
1388 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1391 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1392 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1393 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1394 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1397 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1398 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1402 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1407 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1410 unsigned int ctx_attrib_idx
= 0;
1411 GLint ctx_attribs
[7], ctx_flags
= 0;
1413 if (context_debug_output_enabled(gl_info
))
1414 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1415 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1416 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1417 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1418 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1419 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1420 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1423 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1424 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1426 ctx_attribs
[ctx_attrib_idx
] = 0;
1428 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1430 if (ctx_flags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
1432 ctx_attribs
[ctx_attrib_idx
- 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1433 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1434 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1441 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1442 struct wined3d_surface
*target
, const struct wined3d_format
*ds_format
)
1444 struct wined3d_device
*device
= swapchain
->device
;
1445 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1446 const struct wined3d_format
*color_format
;
1447 struct wined3d_context
*ret
;
1448 BOOL auxBuffers
= FALSE
;
1449 HGLRC ctx
, share_ctx
;
1455 BOOL hdc_is_private
= FALSE
;
1457 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1459 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1463 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1464 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1465 if (!ret
->blit_targets
)
1468 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1469 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1470 if (!ret
->draw_buffers
)
1473 ret
->free_timestamp_query_size
= 4;
1474 ret
->free_timestamp_queries
= HeapAlloc(GetProcessHeap(), 0,
1475 ret
->free_timestamp_query_size
* sizeof(*ret
->free_timestamp_queries
));
1476 if (!ret
->free_timestamp_queries
)
1478 list_init(&ret
->timestamp_queries
);
1480 ret
->free_occlusion_query_size
= 4;
1481 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1482 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1483 if (!ret
->free_occlusion_queries
)
1486 list_init(&ret
->occlusion_queries
);
1488 ret
->free_event_query_size
= 4;
1489 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1490 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1491 if (!ret
->free_event_queries
)
1494 list_init(&ret
->event_queries
);
1495 list_init(&ret
->fbo_list
);
1496 list_init(&ret
->fbo_destroy_list
);
1498 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1500 ERR("Failed to allocate shader backend context data.\n");
1503 if (!device
->adapter
->fragment_pipe
->allocate_context_data(ret
))
1505 ERR("Failed to allocate fragment pipeline context data.\n");
1509 /* Initialize the texture unit mapping to a 1:1 mapping */
1510 for (s
= 0; s
< MAX_COMBINED_SAMPLERS
; ++s
)
1512 if (s
< gl_info
->limits
.combined_samplers
)
1514 ret
->tex_unit_map
[s
] = s
;
1515 ret
->rev_tex_unit_map
[s
] = s
;
1519 ret
->tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1520 ret
->rev_tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1524 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1526 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1528 if ((hdc
= swapchain_get_backup_dc(swapchain
)))
1529 hdc_is_private
= TRUE
;
1532 ERR("Failed to retrieve a device context.\n");
1537 color_format
= target
->resource
.format
;
1539 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1540 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1541 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1545 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1546 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1547 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1548 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1551 /* DirectDraw supports 8bit paletted render targets and these are used by
1552 * old games like StarCraft and C&C. Most modern hardware doesn't support
1553 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1554 * conversion (ab)uses the alpha component for storing the palette index.
1555 * For this reason we require a format with 8bit alpha, so request
1557 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1558 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1560 /* When "always_offscreen" is enabled, we only use the drawable for
1561 * presentation blits, and don't do any rendering to it. That means we
1562 * don't need depth or stencil buffers, and can mostly ignore the render
1563 * target format. This wouldn't necessarily be quite correct for 10bpc
1564 * display modes, but we don't currently support those.
1565 * Using the same format regardless of the color/depth/stencil targets
1566 * makes it much less likely that different wined3d instances will set
1567 * conflicting pixel formats. */
1568 if (wined3d_settings
.always_offscreen
)
1570 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1571 ds_format
= wined3d_get_format(gl_info
, WINED3DFMT_UNKNOWN
);
1574 /* Try to find a pixel format which matches our requirements. */
1575 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1577 /* Try to locate a compatible format if we weren't able to find anything. */
1580 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1581 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1584 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1587 ERR("Can't find a suitable pixel format.\n");
1591 ret
->gl_info
= gl_info
;
1595 if (!context_set_pixel_format(ret
, hdc
, hdc_is_private
, pixel_format
))
1597 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1598 context_release(ret
);
1602 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1603 if (gl_info
->p_wglCreateContextAttribsARB
)
1605 if (!(ctx
= context_create_wgl_attribs(gl_info
, hdc
, share_ctx
)))
1610 if (!(ctx
= wglCreateContext(hdc
)))
1612 ERR("Failed to create a WGL context.\n");
1613 context_release(ret
);
1617 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1619 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1620 context_release(ret
);
1621 if (!wglDeleteContext(ctx
))
1622 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1627 if (!device_context_add(device
, ret
))
1629 ERR("Failed to add the newly created context to the context list\n");
1630 context_release(ret
);
1631 if (!wglDeleteContext(ctx
))
1632 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1636 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1637 ret
->state_table
= device
->StateTable
;
1639 /* Mark all states dirty to force a proper initialization of the states
1640 * on the first use of the context. */
1641 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1643 if (ret
->state_table
[state
].representative
)
1644 context_invalidate_state(ret
, state
);
1647 ret
->swapchain
= swapchain
;
1648 ret
->current_rt
= target
;
1649 ret
->tid
= GetCurrentThreadId();
1651 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
1652 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1656 ret
->win_handle
= swapchain
->win_handle
;
1658 ret
->hdc_is_private
= hdc_is_private
;
1659 ret
->hdc_has_format
= TRUE
;
1660 ret
->pixel_format
= pixel_format
;
1663 /* Set up the context defaults */
1664 if (!context_set_current(ret
))
1666 ERR("Cannot activate context to set up defaults.\n");
1667 device_context_remove(device
, ret
);
1668 context_release(ret
);
1669 if (!wglDeleteContext(ctx
))
1670 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1674 if (context_debug_output_enabled(gl_info
))
1676 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, ret
));
1677 if (TRACE_ON(d3d_synchronous
))
1678 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
1679 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1682 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
1683 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1687 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
1688 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1689 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
1690 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1691 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
1692 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1694 if (WARN_ON(d3d_perf
))
1696 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
1697 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1701 switch (swapchain
->desc
.swap_interval
)
1703 case WINED3DPRESENT_INTERVAL_IMMEDIATE
:
1706 case WINED3DPRESENT_INTERVAL_DEFAULT
:
1707 case WINED3DPRESENT_INTERVAL_ONE
:
1710 case WINED3DPRESENT_INTERVAL_TWO
:
1713 case WINED3DPRESENT_INTERVAL_THREE
:
1716 case WINED3DPRESENT_INTERVAL_FOUR
:
1720 FIXME("Unknown swap interval %#x.\n", swapchain
->desc
.swap_interval
);
1724 if (gl_info
->supported
[WGL_EXT_SWAP_CONTROL
])
1726 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval
)))
1727 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1728 swap_interval
, ret
, GetLastError());
1731 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1733 TRACE("Setting up the screen\n");
1735 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1736 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1738 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1739 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1741 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1742 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1744 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1745 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1746 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
1747 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1749 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
1753 GL_EXTCALL(glGenVertexArrays(1, &vao
));
1754 GL_EXTCALL(glBindVertexArray(vao
));
1755 checkGLcall("creating VAO");
1758 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1760 /* Direct3D always uses n-1 weights for n world matrices and uses
1761 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1762 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1763 * enabled as well. */
1764 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1765 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1767 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1769 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1770 * the previous texture where to source the offset from is always unit - 1.
1772 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1774 context_active_texture(ret
, gl_info
, s
);
1775 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1776 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1777 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1780 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1782 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1783 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1784 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1785 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1788 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1789 * program and the dummy program is destroyed when the context is destroyed.
1791 static const char dummy_program
[] =
1793 "MOV result.color, fragment.color.primary;\n"
1795 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1796 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1797 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1800 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1802 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1804 context_active_texture(ret
, gl_info
, s
);
1805 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1806 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1810 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1812 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1814 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1816 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1818 device
->shader_backend
->shader_init_context_state(ret
);
1819 ret
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
1820 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
1821 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
);
1823 /* If this happens to be the first context for the device, dummy textures
1824 * are not created yet. In that case, they will be created (and bound) by
1825 * create_dummy_textures right after this context is initialized. */
1826 if (device
->dummy_texture_2d
[0])
1827 bind_dummy_textures(device
, ret
);
1829 TRACE("Created context %p.\n", ret
);
1834 device
->shader_backend
->shader_free_context_data(ret
);
1835 device
->adapter
->fragment_pipe
->free_context_data(ret
);
1836 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1837 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1838 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
1839 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1840 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
1841 HeapFree(GetProcessHeap(), 0, ret
);
1845 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
1849 TRACE("Destroying ctx %p\n", context
);
1851 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1853 context_destroy_gl_resources(context
);
1854 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1859 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
1860 in wined3d_adapter may go away in the meantime */
1861 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
1862 *gl_info
= *context
->gl_info
;
1863 context
->gl_info
= gl_info
;
1864 context
->destroyed
= 1;
1868 device
->shader_backend
->shader_free_context_data(context
);
1869 device
->adapter
->fragment_pipe
->free_context_data(context
);
1870 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
1871 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
1872 device_context_remove(device
, context
);
1873 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1876 /* Context activation is done by the caller. */
1877 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
1879 const GLdouble projection
[] =
1881 2.0 / width
, 0.0, 0.0, 0.0,
1882 0.0, 2.0 / height
, 0.0, 0.0,
1884 -1.0, -1.0, -1.0, 1.0,
1887 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
1888 checkGLcall("glMatrixMode(GL_PROJECTION)");
1889 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
1890 checkGLcall("glLoadMatrixd");
1891 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
1892 checkGLcall("glViewport");
1895 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
1897 const struct wined3d_surface
*rt
= context
->current_rt
;
1899 if (rt
->container
->swapchain
&& rt
->container
->swapchain
->front_buffer
== rt
->container
)
1903 GetClientRect(context
->win_handle
, &window_size
);
1904 size
->cx
= window_size
.right
- window_size
.left
;
1905 size
->cy
= window_size
.bottom
- window_size
.top
;
1910 size
->cx
= rt
->resource
.width
;
1911 size
->cy
= rt
->resource
.height
;
1914 /*****************************************************************************
1917 * Sets up a context for DirectDraw blitting.
1918 * All texture units are disabled, texture unit 0 is set as current unit
1919 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1920 * color writing enabled for all channels
1921 * register combiners disabled, shaders disabled
1922 * world matrix is set to identity, texture matrix 0 too
1923 * projection matrix is setup for drawing screen coordinates
1926 * This: Device to activate the context for
1927 * context: Context to setup
1929 *****************************************************************************/
1930 /* Context activation is done by the caller. */
1931 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
1934 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1938 TRACE("Setting up context %p for blitting\n", context
);
1940 context_get_rt_size(context
, &rt_size
);
1942 if (context
->last_was_blit
)
1944 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
1946 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
1947 context
->blit_w
= rt_size
.cx
;
1948 context
->blit_h
= rt_size
.cy
;
1949 /* No need to dirtify here, the states are still dirtified because
1950 * they weren't applied since the last SetupForBlit() call. */
1952 TRACE("Context is already set up for blitting, nothing to do\n");
1955 context
->last_was_blit
= TRUE
;
1957 /* Disable all textures. The caller can then bind a texture it wants to blit
1960 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1961 * function texture unit. No need to care for higher samplers
1963 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1965 sampler
= context
->rev_tex_unit_map
[i
];
1966 context_active_texture(context
, gl_info
, i
);
1968 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1970 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1971 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1973 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
1974 checkGLcall("glDisable GL_TEXTURE_3D");
1975 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1977 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1978 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1980 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
1981 checkGLcall("glDisable GL_TEXTURE_2D");
1983 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1984 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1986 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1988 if (sampler
< MAX_TEXTURES
)
1989 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
1990 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
1993 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
1994 GL_EXTCALL(glBindSampler(0, 0));
1995 context_active_texture(context
, gl_info
, 0);
1997 sampler
= context
->rev_tex_unit_map
[0];
1999 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2001 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2002 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2004 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2005 checkGLcall("glDisable GL_TEXTURE_3D");
2006 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2008 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2009 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2011 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2012 checkGLcall("glDisable GL_TEXTURE_2D");
2014 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2016 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
2017 checkGLcall("glMatrixMode(GL_TEXTURE)");
2018 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2019 checkGLcall("glLoadIdentity()");
2021 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
2023 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
2024 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
2025 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2028 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2030 if (sampler
< MAX_TEXTURES
)
2032 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
2033 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2035 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2038 /* Other misc states */
2039 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2040 checkGLcall("glDisable(GL_ALPHA_TEST)");
2041 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
2042 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
2043 checkGLcall("glDisable GL_LIGHTING");
2044 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
2045 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
2046 checkGLcall("glDisable GL_DEPTH_TEST");
2047 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
2048 glDisableWINE(GL_FOG
);
2049 checkGLcall("glDisable GL_FOG");
2050 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
2051 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2052 checkGLcall("glDisable GL_BLEND");
2053 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2054 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
2055 checkGLcall("glDisable GL_CULL_FACE");
2056 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
2057 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
2058 checkGLcall("glDisable GL_STENCIL_TEST");
2059 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
2060 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
2061 checkGLcall("glDisable GL_SCISSOR_TEST");
2062 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2063 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2065 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
2066 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2067 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
2069 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
2070 checkGLcall("glColorMask");
2071 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
2072 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
2073 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
2074 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2075 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2077 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2078 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2079 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2082 /* Setup transforms */
2083 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2084 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2085 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2086 checkGLcall("glLoadIdentity()");
2087 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2089 context
->last_was_rhw
= TRUE
;
2090 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2092 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2093 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2094 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2095 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2096 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2097 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2098 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2100 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2102 /* Disable shaders */
2103 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2105 context
->blit_w
= rt_size
.cx
;
2106 context
->blit_h
= rt_size
.cy
;
2107 context_invalidate_state(context
, STATE_VIEWPORT
);
2108 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2111 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2113 return rt_mask
& (1u << 31);
2116 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2118 return rt_mask
& ~(1u << 31);
2121 /* Context activation is done by the caller. */
2122 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2124 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2128 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2129 checkGLcall("glDrawBuffer()");
2131 else if (is_rt_mask_onscreen(rt_mask
))
2133 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2134 checkGLcall("glDrawBuffer()");
2138 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2145 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2147 context
->draw_buffers
[i
] = GL_NONE
;
2153 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2155 GL_EXTCALL(glDrawBuffers(i
, context
->draw_buffers
));
2156 checkGLcall("glDrawBuffers()");
2160 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2161 checkGLcall("glDrawBuffer()");
2166 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2171 /* Context activation is done by the caller. */
2172 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2174 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2175 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2176 DWORD new_mask
= context_generate_rt_mask(buffer
);
2178 if (new_mask
== *current_mask
)
2181 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2182 checkGLcall("glDrawBuffer()");
2184 *current_mask
= new_mask
;
2187 /* Context activation is done by the caller. */
2188 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2190 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2191 checkGLcall("glActiveTexture");
2192 context
->active_texture
= unit
;
2195 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2197 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2198 DWORD unit
= context
->active_texture
;
2199 DWORD old_texture_type
= context
->texture_type
[unit
];
2203 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2204 checkGLcall("glBindTexture");
2211 if (old_texture_type
!= target
)
2213 const struct wined3d_device
*device
= context
->swapchain
->device
;
2215 switch (old_texture_type
)
2221 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2222 checkGLcall("glBindTexture");
2224 case GL_TEXTURE_RECTANGLE_ARB
:
2225 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2226 checkGLcall("glBindTexture");
2228 case GL_TEXTURE_CUBE_MAP
:
2229 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2230 checkGLcall("glBindTexture");
2233 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2234 checkGLcall("glBindTexture");
2237 ERR("Unexpected texture target %#x\n", old_texture_type
);
2240 context
->texture_type
[unit
] = target
;
2244 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2246 if (context
->render_offscreen
== offscreen
) return;
2248 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2249 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2250 context_invalidate_state(context
, STATE_VIEWPORT
);
2251 context_invalidate_state(context
, STATE_SCISSORRECT
);
2252 context_invalidate_state(context
, STATE_FRONTFACE
);
2253 context
->render_offscreen
= offscreen
;
2256 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2257 const struct wined3d_format
*required
)
2259 if (existing
== required
)
2261 if ((existing
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
)
2262 != (required
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
))
2264 if (existing
->depth_size
< required
->depth_size
)
2266 /* If stencil bits are used the exact amount is required - otherwise
2267 * wrapping won't work correctly. */
2268 if (required
->stencil_size
&& required
->stencil_size
!= existing
->stencil_size
)
2273 /* Context activation is done by the caller. */
2274 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2275 const struct wined3d_rendertarget_view
*depth_stencil
)
2277 /* Onscreen surfaces are always in a swapchain */
2278 struct wined3d_swapchain
*swapchain
= context
->current_rt
->container
->swapchain
;
2280 if (context
->render_offscreen
|| !depth_stencil
) return;
2281 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2283 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2284 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2286 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2288 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2289 surface_load_location(context
->current_rt
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
2290 swapchain
->render_to_fbo
= TRUE
;
2291 swapchain_update_draw_bindings(swapchain
);
2292 context_set_render_offscreen(context
, TRUE
);
2295 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_device
*device
, const struct wined3d_surface
*rt
)
2297 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2299 else if (rt
->container
->swapchain
)
2300 return context_generate_rt_mask_from_surface(rt
);
2302 return context_generate_rt_mask(device
->offscreenBuffer
);
2305 /* Context activation is done by the caller. */
2306 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2308 struct wined3d_surface
*rt
= context
->current_rt
;
2309 DWORD rt_mask
, *cur_mask
;
2311 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2313 context_validate_onscreen_formats(context
, NULL
);
2315 if (context
->render_offscreen
)
2317 wined3d_texture_load(rt
->container
, context
, FALSE
);
2319 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, rt
, NULL
, rt
->container
->resource
.draw_binding
);
2320 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2327 context
->current_fbo
= NULL
;
2328 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2329 rt_mask
= context_generate_rt_mask_from_surface(rt
);
2334 rt_mask
= context_generate_rt_mask_no_fbo(device
, rt
);
2337 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2339 if (rt_mask
!= *cur_mask
)
2341 context_apply_draw_buffers(context
, rt_mask
);
2342 *cur_mask
= rt_mask
;
2345 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2347 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2350 SetupForBlit(device
, context
);
2351 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2354 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2355 const struct wined3d_rendertarget_view
*ds
)
2359 if (ds
) return TRUE
;
2361 for (i
= 0; i
< rt_count
; ++i
)
2363 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2367 WARN("Invalid render target config, need at least one attachment.\n");
2371 /* Context activation is done by the caller. */
2372 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_device
*device
,
2373 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2375 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2376 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
2377 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2378 DWORD rt_mask
= 0, *cur_mask
;
2381 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= &device
->fb
2382 || rt_count
!= context
->gl_info
->limits
.buffers
)
2384 if (!context_validate_rt_config(rt_count
, rts
, dsv
))
2387 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2389 context_validate_onscreen_formats(context
, dsv
);
2391 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2393 for (i
= 0; i
< rt_count
; ++i
)
2395 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(rts
[i
]);
2396 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2397 rt_mask
|= (1u << i
);
2399 while (i
< context
->gl_info
->limits
.buffers
)
2401 context
->blit_targets
[i
] = NULL
;
2404 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2405 wined3d_rendertarget_view_get_surface(dsv
),
2406 rt_count
? rts
[0]->resource
->draw_binding
: 0,
2407 dsv
? dsv
->resource
->draw_binding
: 0);
2411 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2412 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2413 rt_mask
= context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2416 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2417 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2418 * state management allows this */
2419 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2423 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2424 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2427 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2428 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2430 for (i
= 0; i
< rt_count
; ++i
)
2432 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2433 rt_mask
|= (1u << i
);
2438 rt_mask
= context_generate_rt_mask_no_fbo(device
,
2439 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0]) : NULL
);
2442 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2444 if (rt_mask
!= *cur_mask
)
2446 context_apply_draw_buffers(context
, rt_mask
);
2447 *cur_mask
= rt_mask
;
2448 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2451 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2453 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2456 if (context
->last_was_blit
)
2457 context
->last_was_blit
= FALSE
;
2459 /* Blending and clearing should be orthogonal, but tests on the nvidia
2460 * driver show that disabling blending when clearing improves the clearing
2461 * performance incredibly. */
2462 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2463 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2464 checkGLcall("glEnable GL_SCISSOR_TEST");
2466 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2467 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2468 context_invalidate_state(context
, STATE_SCISSORRECT
);
2473 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_device
*device
)
2475 const struct wined3d_state
*state
= &device
->state
;
2476 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
2477 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2478 DWORD rt_mask
, rt_mask_bits
;
2481 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2482 return context_generate_rt_mask_no_fbo(device
, wined3d_rendertarget_view_get_surface(rts
[0]));
2483 else if (!context
->render_offscreen
)
2484 return context_generate_rt_mask_from_surface(wined3d_rendertarget_view_get_surface(rts
[0]));
2486 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2487 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2488 rt_mask_bits
= rt_mask
;
2490 while (rt_mask_bits
)
2492 rt_mask_bits
&= ~(1u << i
);
2493 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
2494 rt_mask
&= ~(1u << i
);
2502 /* Context activation is done by the caller. */
2503 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2505 const struct wined3d_device
*device
= context
->swapchain
->device
;
2506 const struct wined3d_fb_state
*fb
= state
->fb
;
2507 DWORD rt_mask
= find_draw_buffers_mask(context
, device
);
2510 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2512 if (!context
->render_offscreen
)
2514 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2515 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2521 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
2523 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(fb
->render_targets
[i
]);
2525 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2526 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2527 fb
->render_targets
[0]->resource
->draw_binding
,
2528 fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
2532 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2533 if (rt_mask
!= *cur_mask
)
2535 context_apply_draw_buffers(context
, rt_mask
);
2536 *cur_mask
= rt_mask
;
2540 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
2542 DWORD i
= context
->rev_tex_unit_map
[unit
];
2543 DWORD j
= context
->tex_unit_map
[stage
];
2545 context
->tex_unit_map
[stage
] = unit
;
2546 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2547 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2549 context
->rev_tex_unit_map
[unit
] = stage
;
2550 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2551 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2554 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
2558 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2559 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
2562 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
2563 const struct wined3d_state
*state
)
2567 context
->fixed_function_usage_map
= 0;
2568 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2570 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2571 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2572 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2573 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2574 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2575 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2576 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2577 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2579 /* Not used, and disable higher stages. */
2580 if (color_op
== WINED3D_TOP_DISABLE
)
2583 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2584 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2585 || ((color_arg3
== WINED3DTA_TEXTURE
)
2586 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2587 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2588 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2589 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2590 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2591 context
->fixed_function_usage_map
|= (1u << i
);
2593 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2594 && i
< MAX_TEXTURES
- 1)
2595 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
2598 if (i
< context
->lowest_disabled_stage
)
2601 end
= context
->lowest_disabled_stage
;
2605 start
= context
->lowest_disabled_stage
;
2609 context
->lowest_disabled_stage
= i
;
2610 for (i
= start
+ 1; i
< end
; ++i
)
2612 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
2616 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
2617 const struct wined3d_state
*state
)
2619 unsigned int i
, tex
;
2621 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2623 context_update_fixed_function_usage_map(context
, state
);
2624 ffu_map
= context
->fixed_function_usage_map
;
2626 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2627 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2629 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2634 if (context
->tex_unit_map
[i
] != i
)
2636 context_map_stage(context
, i
, i
);
2637 context_invalidate_state(context
, STATE_SAMPLER(i
));
2638 context_invalidate_texture_stage(context
, i
);
2644 /* Now work out the mapping */
2646 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2651 if (context
->tex_unit_map
[i
] != tex
)
2653 context_map_stage(context
, i
, tex
);
2654 context_invalidate_state(context
, STATE_SAMPLER(i
));
2655 context_invalidate_texture_stage(context
, i
);
2662 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2664 const struct wined3d_shader_resource_info
*resource_info
=
2665 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2667 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2669 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2671 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
2673 context_map_stage(context
, i
, i
);
2674 context_invalidate_state(context
, STATE_SAMPLER(i
));
2675 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2676 context_invalidate_texture_stage(context
, i
);
2681 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
2682 const struct wined3d_shader_resource_info
*ps_resource_info
,
2683 const struct wined3d_shader_resource_info
*vs_resource_info
, DWORD unit
)
2685 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
2687 /* Not currently used */
2688 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
2691 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
2693 /* Used by a fragment sampler */
2695 if (!ps_resource_info
)
2697 /* No pixel shader, check fixed function */
2698 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1u << current_mapping
));
2701 /* Pixel shader, check the shader's sampler map */
2702 return !ps_resource_info
[current_mapping
].type
;
2705 /* Used by a vertex sampler */
2706 return !vs_resource_info
[current_mapping
- MAX_FRAGMENT_SAMPLERS
].type
;
2709 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
2711 const struct wined3d_shader_resource_info
*vs_resource_info
=
2712 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
2713 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
2714 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2715 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2718 /* Note that we only care if a resource is used or not, not the
2719 * resource's specific type. Otherwise we'd need to call
2720 * shader_update_samplers() here for 1.x pixelshaders. */
2722 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2724 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
2726 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2727 if (vs_resource_info
[i
].type
)
2729 if (context
->tex_unit_map
[vsampler_idx
] != WINED3D_UNMAPPED_STAGE
)
2731 /* Already mapped somewhere */
2737 if (context_unit_free_for_vs(context
, ps_resource_info
, vs_resource_info
, start
))
2739 context_map_stage(context
, vsampler_idx
, start
);
2740 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
2752 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2754 BOOL vs
= use_vs(state
);
2755 BOOL ps
= use_ps(state
);
2758 * -> Pixel shaders need a 1:1 map. In theory the shader input could be mapped too, but
2759 * that would be really messy and require shader recompilation
2760 * -> When the mapping of a stage is changed, sampler and ALL texture stage states have
2761 * to be reset. Because of that try to work with a 1:1 mapping as much as possible
2764 context_map_psamplers(context
, state
);
2766 context_map_fixed_function_samplers(context
, state
);
2769 context_map_vsamplers(context
, ps
, state
);
2772 /* Context activation is done by the caller. */
2773 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2775 const struct wined3d_device
*device
= context
->swapchain
->device
;
2776 DWORD rt_mask
, *cur_mask
;
2778 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2780 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2781 rt_mask
= find_draw_buffers_mask(context
, device
);
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
|= 1u << idx
;
2915 stream_info
->use_map
|= 1u << 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 TRACE("Using drawStridedSlow with vertex shaders for FLOAT16 conversion.\n");
2984 context
->use_immediate_mode_draw
= TRUE
;
2988 context
->use_immediate_mode_draw
= FALSE
;
2993 WORD slow_mask
= -!d3d_info
->ffp_generic_attributes
& (1u << WINED3D_FFP_PSIZE
);
2994 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
2995 & ((1u << WINED3D_FFP_DIFFUSE
) | (1u << WINED3D_FFP_SPECULAR
));
2997 if (((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
2998 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
2999 context
->use_immediate_mode_draw
= TRUE
;
3001 context
->use_immediate_mode_draw
= FALSE
;
3004 if (prev_all_vbo
!= stream_info
->all_vbo
)
3005 context_invalidate_state(context
, STATE_INDEXBUFFER
);
3008 /* Context activation is done by the caller. */
3009 static void context_preload_texture(struct wined3d_context
*context
,
3010 const struct wined3d_state
*state
, unsigned int idx
)
3012 struct wined3d_texture
*texture
;
3014 if (!(texture
= state
->textures
[idx
]))
3017 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
3020 /* Context activation is done by the caller. */
3021 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3027 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3029 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
3030 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
3036 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3038 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
3039 context_preload_texture(context
, state
, i
);
3044 WORD ffu_map
= context
->fixed_function_usage_map
;
3046 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3049 context_preload_texture(context
, state
, i
);
3054 static void context_load_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3056 struct wined3d_shader_sampler_map_entry
*entry
;
3057 struct wined3d_shader_resource_view
*view
;
3058 struct wined3d_shader
*shader
;
3061 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3063 if (!(shader
= state
->shader
[i
]))
3066 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3068 if (state
->cb
[i
][j
])
3069 buffer_internal_preload(state
->cb
[i
][j
], context
, state
);
3072 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
3074 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3076 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
3078 WARN("No resource view bound at index %u, %u.\n", i
, entry
->resource_idx
);
3082 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3083 buffer_internal_preload(buffer_from_resource(view
->resource
), context
, state
);
3085 wined3d_texture_load(wined3d_texture_from_resource(view
->resource
), context
, FALSE
);
3090 static void context_bind_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3092 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3093 struct wined3d_shader_sampler_map_entry
*entry
;
3094 struct wined3d_shader_resource_view
*view
;
3095 struct wined3d_sampler
*sampler
;
3096 struct wined3d_texture
*texture
;
3097 struct wined3d_shader
*shader
;
3098 unsigned int i
, j
, count
;
3102 enum wined3d_shader_type type
;
3103 unsigned int base_idx
;
3108 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
3109 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
3112 for (i
= 0; i
< ARRAY_SIZE(shader_types
); ++i
)
3114 if (!(shader
= state
->shader
[shader_types
[i
].type
]))
3117 count
= shader
->reg_maps
.sampler_map
.count
;
3118 if (count
> shader_types
[i
].count
)
3120 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3121 shader
, count
, shader_types
[i
].count
);
3122 count
= shader_types
[i
].count
;
3125 for (j
= 0; j
< count
; ++j
)
3127 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3129 if (!(view
= state
->shader_resource_view
[shader_types
[i
].type
][entry
->resource_idx
]))
3131 WARN("No resource view bound at index %u, %u.\n", shader_types
[i
].type
, entry
->resource_idx
);
3135 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3137 FIXME("Buffer shader resources not supported.\n");
3141 if (!(sampler
= state
->sampler
[shader_types
[i
].type
][entry
->sampler_idx
]))
3143 WARN("No sampler object bound at index %u, %u.\n", shader_types
[i
].type
, entry
->sampler_idx
);
3147 texture
= wined3d_texture_from_resource(view
->resource
);
3148 context_active_texture(context
, gl_info
, shader_types
[i
].base_idx
+ entry
->bind_idx
);
3149 wined3d_texture_bind(texture
, context
, FALSE
);
3151 GL_EXTCALL(glBindSampler(shader_types
[i
].base_idx
+ entry
->bind_idx
, sampler
->name
));
3152 checkGLcall("glBindSampler");
3157 /* Context activation is done by the caller. */
3158 BOOL
context_apply_draw_state(struct wined3d_context
*context
, struct wined3d_device
*device
)
3160 const struct wined3d_state
*state
= &device
->state
;
3161 const struct StateEntry
*state_table
= context
->state_table
;
3162 const struct wined3d_fb_state
*fb
= state
->fb
;
3166 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
3167 fb
->render_targets
, fb
->depth_stencil
))
3170 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3172 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3175 /* Preload resources before FBO setup. Texture preload in particular may
3176 * result in changes to the current FBO, due to using e.g. FBO blits for
3177 * updating a resource location. */
3178 context_update_tex_unit_map(context
, state
);
3179 context_preload_textures(context
, state
);
3180 context_load_shader_resources(context
, state
);
3181 /* TODO: Right now the dependency on the vertex shader is necessary
3182 * since context_stream_info_from_declaration depends on the reg_maps of
3183 * the current VS but maybe it's possible to relax the coupling in some
3184 * situations at least. */
3185 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
3186 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
3188 context_update_stream_info(context
, state
);
3192 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3195 buffer_mark_used(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
);
3198 if (state
->index_buffer
)
3200 if (context
->stream_info
.all_vbo
)
3201 buffer_internal_preload(state
->index_buffer
, context
, state
);
3203 buffer_get_sysmem(state
->index_buffer
, context
);
3206 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3208 DWORD rep
= context
->dirtyArray
[i
];
3209 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3210 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3211 context
->isStateDirty
[idx
] &= ~(1u << shift
);
3212 state_table
[rep
].apply(context
, state
, rep
);
3215 if (context
->shader_update_mask
)
3217 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3218 context
->shader_update_mask
= 0;
3221 if (context
->constant_update_mask
)
3223 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3224 context
->constant_update_mask
= 0;
3227 if (context
->update_shader_resource_bindings
)
3229 context_bind_shader_resources(context
, state
);
3230 context
->update_shader_resource_bindings
= 0;
3233 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3235 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3238 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3239 context
->last_was_blit
= FALSE
;
3244 static void context_setup_target(struct wined3d_context
*context
, struct wined3d_surface
*target
)
3246 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3248 render_offscreen
= wined3d_resource_is_offscreen(&target
->container
->resource
);
3249 if (context
->current_rt
== target
&& render_offscreen
== old_render_offscreen
) return;
3251 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3252 * the alpha blend state changes with different render target formats. */
3253 if (!context
->current_rt
)
3255 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3259 const struct wined3d_format
*old
= context
->current_rt
->resource
.format
;
3260 const struct wined3d_format
*new = target
->resource
.format
;
3262 if (old
->id
!= new->id
)
3264 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3265 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3266 || !(target
->container
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3267 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3269 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3270 if ((context
->current_rt
->container
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
3271 != (target
->container
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3272 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3275 /* When switching away from an offscreen render target, and we're not
3276 * using FBOs, we have to read the drawable into the texture. This is
3277 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
3278 * There are some things that need care though. PreLoad needs a GL context,
3279 * and FindContext is called before the context is activated. It also
3280 * has to be called with the old rendertarget active, otherwise a
3281 * wrong drawable is read. */
3282 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
3283 && old_render_offscreen
&& context
->current_rt
!= target
)
3285 struct wined3d_texture
*texture
= context
->current_rt
->container
;
3287 /* Read the back buffer of the old drawable into the destination texture. */
3288 if (texture
->texture_srgb
.name
)
3289 wined3d_texture_load(texture
, context
, TRUE
);
3290 wined3d_texture_load(texture
, context
, FALSE
);
3291 surface_invalidate_location(context
->current_rt
, WINED3D_LOCATION_DRAWABLE
);
3295 context
->current_rt
= target
;
3296 context_set_render_offscreen(context
, render_offscreen
);
3299 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
3301 struct wined3d_context
*current_context
= context_get_current();
3302 struct wined3d_context
*context
;
3304 TRACE("device %p, target %p.\n", device
, target
);
3306 if (current_context
&& current_context
->destroyed
)
3307 current_context
= NULL
;
3312 && current_context
->current_rt
3313 && current_context
->swapchain
->device
== device
)
3315 target
= current_context
->current_rt
;
3319 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
3320 if (swapchain
->back_buffers
)
3321 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->back_buffers
[0], 0));
3323 target
= surface_from_resource(wined3d_texture_get_sub_resource(swapchain
->front_buffer
, 0));
3327 if (current_context
&& current_context
->current_rt
== target
)
3329 context
= current_context
;
3331 else if (target
->container
->swapchain
)
3333 TRACE("Rendering onscreen.\n");
3335 context
= swapchain_get_context(target
->container
->swapchain
);
3339 TRACE("Rendering offscreen.\n");
3341 /* Stay with the current context if possible. Otherwise use the
3342 * context for the primary swapchain. */
3343 if (current_context
&& current_context
->swapchain
->device
== device
)
3344 context
= current_context
;
3346 context
= swapchain_get_context(device
->swapchains
[0]);
3349 context_enter(context
);
3350 context_update_window(context
);
3351 context_setup_target(context
, target
);
3352 if (!context
->valid
) return context
;
3354 if (context
!= current_context
)
3356 if (!context_set_current(context
))
3357 ERR("Failed to activate the new context.\n");
3359 else if (context
->needs_set
)
3361 context_set_gl_context(context
);