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 flags
, GLuint rb
)
108 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
110 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
111 checkGLcall("glFramebufferRenderbuffer()");
114 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
116 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
117 checkGLcall("glFramebufferRenderbuffer()");
121 static void context_attach_gl_texture_fbo(struct wined3d_context
*context
,
122 GLenum fbo_target
, GLenum attachment
, const struct wined3d_fbo_resource
*resource
)
124 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
128 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
, GL_TEXTURE_2D
, 0, 0);
129 checkGLcall("glFramebufferTexture2D()");
131 else if (resource
->target
== GL_TEXTURE_2D_ARRAY
)
133 if (!gl_info
->fbo_ops
.glFramebufferTextureLayer
)
135 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
139 gl_info
->fbo_ops
.glFramebufferTextureLayer(fbo_target
, attachment
,
140 resource
->object
, resource
->level
, resource
->layer
);
141 checkGLcall("glFramebufferTextureLayer()");
145 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
,
146 resource
->target
, resource
->object
, resource
->level
);
147 checkGLcall("glFramebufferTexture2D()");
151 /* Context activation is done by the caller. */
152 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
153 GLenum fbo_target
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
,
156 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
158 if (resource
->object
)
160 TRACE("Attach depth stencil %u.\n", resource
->object
);
164 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
165 flags
, resource
->object
);
169 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
170 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, resource
);
172 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
173 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, resource
);
176 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
))
177 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
179 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
))
180 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
184 TRACE("Attach depth stencil 0.\n");
186 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
187 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
191 /* Context activation is done by the caller. */
192 static void context_attach_surface_fbo(struct wined3d_context
*context
,
193 GLenum fbo_target
, DWORD idx
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
)
195 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
197 TRACE("Attach GL object %u to %u.\n", resource
->object
, idx
);
199 if (resource
->object
)
204 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
205 GL_RENDERBUFFER
, resource
->object
);
206 checkGLcall("glFramebufferRenderbuffer()");
210 context_attach_gl_texture_fbo(context
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, resource
);
215 context_attach_gl_texture_fbo(context
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, NULL
);
219 static void context_dump_fbo_attachment(const struct wined3d_gl_info
*gl_info
, GLenum target
,
227 enum wined3d_gl_extension extension
;
231 {GL_TEXTURE_2D
, GL_TEXTURE_BINDING_2D
, "2d", WINED3D_GL_EXT_NONE
},
232 {GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_BINDING_RECTANGLE_ARB
, "rectangle", ARB_TEXTURE_RECTANGLE
},
233 {GL_TEXTURE_2D_ARRAY
, GL_TEXTURE_BINDING_2D_ARRAY
, "2d-array", EXT_TEXTURE_ARRAY
},
236 GLint type
, name
, samples
, width
, height
, old_texture
, level
, face
, fmt
, tex_target
;
238 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
239 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
, &name
);
240 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
241 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
, &type
);
243 if (type
== GL_RENDERBUFFER
)
245 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, name
);
246 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_WIDTH
, &width
);
247 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_HEIGHT
, &height
);
248 if (gl_info
->limits
.samples
> 1)
249 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_SAMPLES
, &samples
);
252 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_INTERNAL_FORMAT
, &fmt
);
253 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
254 debug_fboattachment(attachment
), name
, width
, height
, samples
, fmt
);
256 else if (type
== GL_TEXTURE
)
258 const char *tex_type_str
;
260 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
261 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
, &level
);
262 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
263 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
, &face
);
267 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP
, &old_texture
);
269 glBindTexture(GL_TEXTURE_CUBE_MAP
, name
);
270 glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
271 glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_WIDTH
, &width
);
272 glGetTexLevelParameteriv(face
, level
, GL_TEXTURE_HEIGHT
, &height
);
274 tex_target
= GL_TEXTURE_CUBE_MAP
;
275 tex_type_str
= "cube";
282 for (i
= 0; i
< sizeof(texture_type
) / sizeof(*texture_type
); ++i
)
284 if (!gl_info
->supported
[texture_type
[i
].extension
])
287 gl_info
->gl_ops
.gl
.p_glGetIntegerv(texture_type
[i
].binding
, &old_texture
);
288 while (gl_info
->gl_ops
.gl
.p_glGetError());
290 glBindTexture(texture_type
[i
].target
, name
);
291 if (!gl_info
->gl_ops
.gl
.p_glGetError())
293 tex_target
= texture_type
[i
].target
;
294 tex_type_str
= texture_type
[i
].str
;
297 glBindTexture(texture_type
[i
].target
, old_texture
);
301 FIXME("Cannot find type of texture %d.\n", name
);
305 glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
306 glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_WIDTH
, &width
);
307 glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_HEIGHT
, &height
);
310 FIXME(" %s: %s texture %d, %dx%d, format %#x.\n", debug_fboattachment(attachment
),
311 tex_type_str
, name
, width
, height
, fmt
);
313 glBindTexture(tex_target
, old_texture
);
314 checkGLcall("Guess texture type");
316 else if (type
== GL_NONE
)
318 FIXME(" %s: NONE.\n", debug_fboattachment(attachment
));
322 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment
), type
);
326 /* Context activation is done by the caller. */
327 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
329 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
332 if (!FIXME_ON(d3d
)) return;
334 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
335 if (status
== GL_FRAMEBUFFER_COMPLETE
)
337 TRACE("FBO complete\n");
343 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
345 if (!context
->current_fbo
)
347 ERR("FBO 0 is incomplete, driver bug?\n");
351 context_dump_fbo_attachment(gl_info
, target
, GL_DEPTH_ATTACHMENT
);
352 context_dump_fbo_attachment(gl_info
, target
, GL_STENCIL_ATTACHMENT
);
354 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
355 context_dump_fbo_attachment(gl_info
, target
, GL_COLOR_ATTACHMENT0
+ i
);
356 checkGLcall("Dump FBO attachments");
360 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
362 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
363 return buffer
? (1u << 31) | buffer
: 0;
366 static inline DWORD
context_generate_rt_mask_from_resource(struct wined3d_resource
*resource
)
368 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
370 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
374 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource
));
377 static inline void context_set_fbo_key_for_surface(const struct wined3d_context
*context
,
378 struct wined3d_fbo_entry_key
*key
, UINT idx
, struct wined3d_surface
*surface
,
383 key
->objects
[idx
].object
= 0;
384 key
->objects
[idx
].target
= 0;
385 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
387 else if (surface
->current_renderbuffer
)
389 key
->objects
[idx
].object
= surface
->current_renderbuffer
->id
;
390 key
->objects
[idx
].target
= 0;
391 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
392 key
->rb_namespace
|= 1 << idx
;
398 case WINED3D_LOCATION_TEXTURE_RGB
:
399 key
->objects
[idx
].object
= surface_get_texture_name(surface
, context
, FALSE
);
400 key
->objects
[idx
].target
= surface
->texture_target
;
401 key
->objects
[idx
].level
= surface
->texture_level
;
402 key
->objects
[idx
].layer
= surface
->texture_layer
;
405 case WINED3D_LOCATION_TEXTURE_SRGB
:
406 key
->objects
[idx
].object
= surface_get_texture_name(surface
, context
, TRUE
);
407 key
->objects
[idx
].target
= surface
->texture_target
;
408 key
->objects
[idx
].level
= surface
->texture_level
;
409 key
->objects
[idx
].layer
= surface
->texture_layer
;
412 case WINED3D_LOCATION_RB_MULTISAMPLE
:
413 key
->objects
[idx
].object
= surface
->container
->rb_multisample
;
414 key
->objects
[idx
].target
= 0;
415 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
416 key
->rb_namespace
|= 1 << idx
;
419 case WINED3D_LOCATION_RB_RESOLVED
:
420 key
->objects
[idx
].object
= surface
->container
->rb_resolved
;
421 key
->objects
[idx
].target
= 0;
422 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
423 key
->rb_namespace
|= 1 << idx
;
429 static void context_generate_fbo_key(const struct wined3d_context
*context
,
430 struct wined3d_fbo_entry_key
*key
, struct wined3d_surface
**render_targets
,
431 struct wined3d_surface
*depth_stencil
, DWORD color_location
,
436 key
->rb_namespace
= 0;
437 context_set_fbo_key_for_surface(context
, key
, 0, depth_stencil
, ds_location
);
439 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
440 context_set_fbo_key_for_surface(context
, key
, i
+ 1, render_targets
[i
], color_location
);
443 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
444 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
445 DWORD color_location
, DWORD ds_location
)
447 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
448 struct fbo_entry
*entry
;
449 UINT object_count
= gl_info
->limits
.buffers
+ 1;
451 entry
= HeapAlloc(GetProcessHeap(), 0,
452 FIELD_OFFSET(struct fbo_entry
, key
.objects
[object_count
]));
453 memset(&entry
->key
, 0, FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[object_count
]));
454 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
458 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
)
459 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
460 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_STENCIL
)
461 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
463 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
464 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
465 checkGLcall("glGenFramebuffers()");
466 TRACE("Created FBO %u.\n", entry
->id
);
471 /* Context activation is done by the caller. */
472 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
473 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
474 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
476 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
478 context_bind_fbo(context
, target
, entry
->id
);
479 context_clean_fbo_attachments(gl_info
, target
);
481 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
485 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_DEPTH
)
486 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
487 if (depth_stencil
->container
->resource
.format_flags
& WINED3DFMT_FLAG_STENCIL
)
488 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
492 /* Context activation is done by the caller. */
493 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
497 TRACE("Destroy FBO %u.\n", entry
->id
);
498 context_destroy_fbo(context
, entry
->id
);
500 --context
->fbo_entry_count
;
501 list_remove(&entry
->entry
);
502 HeapFree(GetProcessHeap(), 0, entry
);
505 /* Context activation is done by the caller. */
506 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
507 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
508 DWORD color_location
, DWORD ds_location
)
510 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
511 unsigned int object_count
= gl_info
->limits
.buffers
+ 1;
512 struct wined3d_texture
*rt_texture
, *ds_texture
;
513 struct fbo_entry
*entry
;
516 if (depth_stencil
&& render_targets
[0])
518 rt_texture
= render_targets
[0]->container
;
519 ds_texture
= depth_stencil
->container
;
521 if (wined3d_texture_get_level_width(ds_texture
, depth_stencil
->texture_level
)
522 < wined3d_texture_get_level_width(rt_texture
, render_targets
[0]->texture_level
)
523 || wined3d_texture_get_level_height(ds_texture
, depth_stencil
->texture_level
)
524 < wined3d_texture_get_level_height(rt_texture
, render_targets
[0]->texture_level
))
526 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
527 depth_stencil
= NULL
;
529 else if (ds_texture
->resource
.multisample_type
!= rt_texture
->resource
.multisample_type
530 || ds_texture
->resource
.multisample_quality
!= rt_texture
->resource
.multisample_quality
)
532 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
533 rt_texture
->resource
.multisample_type
, rt_texture
->resource
.multisample_quality
,
534 ds_texture
->resource
.multisample_type
, ds_texture
->resource
.multisample_quality
);
535 depth_stencil
= NULL
;
538 surface_set_compatible_renderbuffer(depth_stencil
, render_targets
[0]);
541 context_generate_fbo_key(context
, context
->fbo_key
, render_targets
, depth_stencil
, color_location
,
546 TRACE("Dumping FBO attachments:\n");
547 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
549 if (render_targets
[i
])
551 rt_texture
= render_targets
[i
]->container
;
552 TRACE(" Color attachment %u: %p format %s, %s %u, %ux%u, %u samples.\n",
553 i
, render_targets
[i
], debug_d3dformat(rt_texture
->resource
.format
->id
),
554 context
->fbo_key
->rb_namespace
& (1 << (i
+ 1)) ? "renderbuffer" : "texure",
555 context
->fbo_key
->objects
[i
+ 1].object
,
556 wined3d_texture_get_level_pow2_width(rt_texture
, render_targets
[i
]->texture_level
),
557 wined3d_texture_get_level_pow2_height(rt_texture
, render_targets
[i
]->texture_level
),
558 rt_texture
->resource
.multisample_type
);
563 ds_texture
= depth_stencil
->container
;
564 TRACE(" Depth attachment: %p format %s, %s %u, %ux%u, %u samples.\n",
565 depth_stencil
, debug_d3dformat(ds_texture
->resource
.format
->id
),
566 context
->fbo_key
->rb_namespace
& (1 << 0) ? "renderbuffer" : "texure",
567 context
->fbo_key
->objects
[0].object
,
568 wined3d_texture_get_level_pow2_width(ds_texture
, depth_stencil
->texture_level
),
569 wined3d_texture_get_level_pow2_height(ds_texture
, depth_stencil
->texture_level
),
570 ds_texture
->resource
.multisample_type
);
574 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
576 if (memcmp(context
->fbo_key
, &entry
->key
, FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[object_count
])))
579 list_remove(&entry
->entry
);
580 list_add_head(&context
->fbo_list
, &entry
->entry
);
584 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
586 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, color_location
, ds_location
);
587 list_add_head(&context
->fbo_list
, &entry
->entry
);
588 ++context
->fbo_entry_count
;
592 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
593 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, color_location
, ds_location
, entry
);
594 list_remove(&entry
->entry
);
595 list_add_head(&context
->fbo_list
, &entry
->entry
);
601 /* Context activation is done by the caller. */
602 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
604 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
606 GLuint read_binding
, draw_binding
;
608 if (entry
->flags
& WINED3D_FBO_ENTRY_FLAG_ATTACHED
)
610 context_bind_fbo(context
, target
, entry
->id
);
614 read_binding
= context
->fbo_read_binding
;
615 draw_binding
= context
->fbo_draw_binding
;
616 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
618 /* Apply render targets */
619 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
621 context_attach_surface_fbo(context
, target
, i
, &entry
->key
.objects
[i
+ 1],
622 entry
->key
.rb_namespace
& (1 << (i
+ 1)));
625 context_attach_depth_stencil_fbo(context
, target
, &entry
->key
.objects
[0],
626 entry
->key
.rb_namespace
& 0x1, entry
->flags
);
628 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
629 * GL contexts requirements. */
630 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
631 context_set_draw_buffer(context
, GL_NONE
);
632 if (target
!= GL_FRAMEBUFFER
)
634 if (target
== GL_READ_FRAMEBUFFER
)
635 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
637 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
640 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_ATTACHED
;
643 /* Context activation is done by the caller. */
644 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
645 struct wined3d_surface
**render_targets
, struct wined3d_surface
*depth_stencil
,
646 DWORD color_location
, DWORD ds_location
)
648 struct fbo_entry
*entry
, *entry2
;
650 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
652 context_destroy_fbo_entry(context
, entry
);
655 if (context
->rebind_fbo
)
657 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
658 context
->rebind_fbo
= FALSE
;
661 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
663 context
->current_fbo
= NULL
;
664 context_bind_fbo(context
, target
, 0);
668 context
->current_fbo
= context_find_fbo_entry(context
, target
, render_targets
, depth_stencil
,
669 color_location
, ds_location
);
670 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
674 /* Context activation is done by the caller. */
675 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
676 struct wined3d_surface
*render_target
, struct wined3d_surface
*depth_stencil
, DWORD location
)
678 UINT clear_size
= (context
->gl_info
->limits
.buffers
- 1) * sizeof(*context
->blit_targets
);
680 context
->blit_targets
[0] = render_target
;
682 memset(&context
->blit_targets
[1], 0, clear_size
);
683 context_apply_fbo_state(context
, target
, context
->blit_targets
, depth_stencil
, location
, location
);
686 /* Context activation is done by the caller. */
687 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
689 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
691 if (context
->free_occlusion_query_count
)
693 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
697 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
699 GL_EXTCALL(glGenQueries(1, &query
->id
));
700 checkGLcall("glGenQueries");
702 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
706 WARN("Occlusion queries not supported, not allocating query id.\n");
711 query
->context
= context
;
712 list_add_head(&context
->occlusion_queries
, &query
->entry
);
715 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
717 struct wined3d_context
*context
= query
->context
;
719 list_remove(&query
->entry
);
720 query
->context
= NULL
;
722 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
724 UINT new_size
= context
->free_occlusion_query_size
<< 1;
725 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
726 new_size
* sizeof(*context
->free_occlusion_queries
));
730 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
734 context
->free_occlusion_query_size
= new_size
;
735 context
->free_occlusion_queries
= new_data
;
738 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
741 /* Context activation is done by the caller. */
742 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
744 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
746 if (context
->free_event_query_count
)
748 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
752 if (gl_info
->supported
[ARB_SYNC
])
754 /* Using ARB_sync, not much to do here. */
755 query
->object
.sync
= NULL
;
756 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
758 else if (gl_info
->supported
[APPLE_FENCE
])
760 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
761 checkGLcall("glGenFencesAPPLE");
763 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
765 else if(gl_info
->supported
[NV_FENCE
])
767 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
768 checkGLcall("glGenFencesNV");
770 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
774 WARN("Event queries not supported, not allocating query id.\n");
775 query
->object
.id
= 0;
779 query
->context
= context
;
780 list_add_head(&context
->event_queries
, &query
->entry
);
783 void context_free_event_query(struct wined3d_event_query
*query
)
785 struct wined3d_context
*context
= query
->context
;
787 list_remove(&query
->entry
);
788 query
->context
= NULL
;
790 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
792 UINT new_size
= context
->free_event_query_size
<< 1;
793 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
794 new_size
* sizeof(*context
->free_event_queries
));
798 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
802 context
->free_event_query_size
= new_size
;
803 context
->free_event_queries
= new_data
;
806 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
809 /* Context activation is done by the caller. */
810 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
812 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
814 if (context
->free_timestamp_query_count
)
816 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
820 GL_EXTCALL(glGenQueries(1, &query
->id
));
821 checkGLcall("glGenQueries");
823 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
826 query
->context
= context
;
827 list_add_head(&context
->timestamp_queries
, &query
->entry
);
830 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
832 struct wined3d_context
*context
= query
->context
;
834 list_remove(&query
->entry
);
835 query
->context
= NULL
;
837 if (context
->free_timestamp_query_count
>= context
->free_timestamp_query_size
- 1)
839 UINT new_size
= context
->free_timestamp_query_size
<< 1;
840 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_timestamp_queries
,
841 new_size
* sizeof(*context
->free_timestamp_queries
));
845 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
849 context
->free_timestamp_query_size
= new_size
;
850 context
->free_timestamp_queries
= new_data
;
853 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
856 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
858 static void context_enum_fbo_entries(const struct wined3d_device
*device
,
859 GLuint name
, BOOL rb_namespace
, context_fbo_entry_func_t
*callback
)
863 for (i
= 0; i
< device
->context_count
; ++i
)
865 struct wined3d_context
*context
= device
->contexts
[i
];
866 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
867 struct fbo_entry
*entry
, *entry2
;
869 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
873 for (j
= 0; j
< gl_info
->limits
.buffers
+ 1; ++j
)
875 if (entry
->key
.objects
[j
].object
== name
876 && !(entry
->key
.rb_namespace
& (1 << j
)) == !rb_namespace
)
878 callback(context
, entry
);
886 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
888 list_remove(&entry
->entry
);
889 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
892 void context_resource_released(const struct wined3d_device
*device
,
893 struct wined3d_resource
*resource
, enum wined3d_resource_type type
)
895 struct wined3d_texture
*texture
;
898 if (!device
->d3d_initialized
)
903 case WINED3D_RTYPE_TEXTURE_2D
:
904 case WINED3D_RTYPE_TEXTURE_3D
:
905 texture
= texture_from_resource(resource
);
907 for (i
= 0; i
< device
->context_count
; ++i
)
909 struct wined3d_context
*context
= device
->contexts
[i
];
910 if (context
->current_rt
.texture
== texture
)
912 context
->current_rt
.texture
= NULL
;
913 context
->current_rt
.sub_resource_idx
= 0;
923 void context_gl_resource_released(struct wined3d_device
*device
,
924 GLuint name
, BOOL rb_namespace
)
926 context_enum_fbo_entries(device
, name
, rb_namespace
, context_queue_fbo_entry_destruction
);
929 void context_surface_update(struct wined3d_context
*context
, const struct wined3d_surface
*surface
)
931 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
932 struct fbo_entry
*entry
= context
->current_fbo
;
935 if (!entry
|| context
->rebind_fbo
) return;
937 for (i
= 0; i
< gl_info
->limits
.buffers
+ 1; ++i
)
939 if (surface
->container
->texture_rgb
.name
== entry
->key
.objects
[i
].object
940 || surface
->container
->texture_srgb
.name
== entry
->key
.objects
[i
].object
)
942 TRACE("Updated surface %p is bound as attachment %u to the current FBO.\n", surface
, i
);
943 context
->rebind_fbo
= TRUE
;
949 static BOOL
context_restore_pixel_format(struct wined3d_context
*ctx
)
951 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
954 if (ctx
->restore_pf
&& IsWindow(ctx
->restore_pf_win
))
956 if (ctx
->gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
958 HDC dc
= GetDCEx(ctx
->restore_pf_win
, 0, DCX_USESTYLE
| DCX_CACHE
);
961 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, ctx
->restore_pf
))))
963 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
964 ctx
->restore_pf
, ctx
->restore_pf_win
);
966 ReleaseDC(ctx
->restore_pf_win
, dc
);
971 ERR("can't restore pixel format %d on window %p\n", ctx
->restore_pf
, ctx
->restore_pf_win
);
976 ctx
->restore_pf_win
= NULL
;
980 static BOOL
context_set_pixel_format(struct wined3d_context
*context
, HDC dc
, BOOL
private, int format
)
982 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
985 if (dc
== context
->hdc
&& context
->hdc_is_private
&& context
->hdc_has_format
)
988 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
989 if (current
== format
) goto success
;
993 if (!SetPixelFormat(dc
, format
, NULL
))
995 /* This may also happen if the dc belongs to a destroyed window. */
996 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
997 format
, dc
, GetLastError());
1001 context
->restore_pf
= 0;
1002 context
->restore_pf_win
= private ? NULL
: WindowFromDC(dc
);
1006 /* By default WGL doesn't allow pixel format adjustments but we need it
1007 * here. For this reason there's a Wine specific wglSetPixelFormat()
1008 * which allows us to set the pixel format multiple times. Only use it
1009 * when really needed. */
1010 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1014 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
1016 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1021 win
= private ? NULL
: WindowFromDC(dc
);
1022 if (win
!= context
->restore_pf_win
)
1024 context_restore_pixel_format(context
);
1026 context
->restore_pf
= private ? 0 : current
;
1027 context
->restore_pf_win
= win
;
1033 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1034 * continue using the old format. There's a big chance that the old
1035 * format works although with a performance hit and perhaps rendering
1037 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1038 format
, dc
, current
);
1042 if (dc
== context
->hdc
&& context
->hdc_is_private
)
1043 context
->hdc_has_format
= TRUE
;
1047 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
1049 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
1050 BOOL backup
= FALSE
;
1052 if (!context_set_pixel_format(ctx
, ctx
->hdc
, ctx
->hdc_is_private
, ctx
->pixel_format
))
1054 WARN("Failed to set pixel format %d on device context %p.\n",
1055 ctx
->pixel_format
, ctx
->hdc
);
1059 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
1063 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1064 ctx
->glCtx
, ctx
->hdc
, GetLastError());
1066 WARN("Trying fallback to the backup window.\n");
1068 /* FIXME: If the context is destroyed it's no longer associated with
1069 * a swapchain, so we can't use the swapchain to get a backup dc. To
1070 * make this work windowless contexts would need to be handled by the
1074 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
1075 context_set_current(NULL
);
1079 if (!(dc
= swapchain_get_backup_dc(swapchain
)))
1081 context_set_current(NULL
);
1085 if (!context_set_pixel_format(ctx
, dc
, TRUE
, ctx
->pixel_format
))
1087 ERR("Failed to set pixel format %d on device context %p.\n",
1088 ctx
->pixel_format
, dc
);
1089 context_set_current(NULL
);
1093 if (!wglMakeCurrent(dc
, ctx
->glCtx
))
1095 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1096 dc
, GetLastError());
1097 context_set_current(NULL
);
1107 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
1109 if (!wglMakeCurrent(dc
, gl_ctx
))
1111 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1112 gl_ctx
, dc
, GetLastError());
1113 context_set_current(NULL
);
1117 static void context_update_window(struct wined3d_context
*context
)
1119 if (context
->win_handle
== context
->swapchain
->win_handle
)
1122 TRACE("Updating context %p window from %p to %p.\n",
1123 context
, context
->win_handle
, context
->swapchain
->win_handle
);
1126 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1128 context
->win_handle
= context
->swapchain
->win_handle
;
1129 context
->hdc_is_private
= FALSE
;
1130 context
->hdc_has_format
= FALSE
;
1131 context
->needs_set
= 1;
1134 if (!(context
->hdc
= GetDCEx(context
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1136 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
1141 static void context_destroy_gl_resources(struct wined3d_context
*context
)
1143 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1144 struct wined3d_timestamp_query
*timestamp_query
;
1145 struct wined3d_occlusion_query
*occlusion_query
;
1146 struct wined3d_event_query
*event_query
;
1147 struct fbo_entry
*entry
, *entry2
;
1152 restore_ctx
= wglGetCurrentContext();
1153 restore_dc
= wglGetCurrentDC();
1155 if (restore_ctx
== context
->glCtx
)
1157 else if (context
->valid
)
1158 context_set_gl_context(context
);
1160 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
1163 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
1164 timestamp_query
->context
= NULL
;
1167 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1169 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1170 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1171 occlusion_query
->context
= NULL
;
1174 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
1178 if (gl_info
->supported
[ARB_SYNC
])
1180 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
1182 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
1183 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
1185 event_query
->context
= NULL
;
1188 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
1190 if (!context
->valid
) entry
->id
= 0;
1191 context_destroy_fbo_entry(context
, entry
);
1194 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1196 if (!context
->valid
) entry
->id
= 0;
1197 context_destroy_fbo_entry(context
, entry
);
1202 if (context
->dummy_arbfp_prog
)
1204 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
1207 if (gl_info
->supported
[ARB_TIMER_QUERY
])
1208 GL_EXTCALL(glDeleteQueries(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
1210 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1211 GL_EXTCALL(glDeleteQueries(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
1213 if (gl_info
->supported
[ARB_SYNC
])
1215 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1217 GL_EXTCALL(glDeleteSync(context
->free_event_queries
[i
].sync
));
1220 else if (gl_info
->supported
[APPLE_FENCE
])
1222 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1224 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
1227 else if (gl_info
->supported
[NV_FENCE
])
1229 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
1231 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
1235 checkGLcall("context cleanup");
1238 HeapFree(GetProcessHeap(), 0, context
->free_timestamp_queries
);
1239 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
1240 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
1242 context_restore_pixel_format(context
);
1245 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1247 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1249 ERR("Failed to disable GL context.\n");
1252 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1254 if (!wglDeleteContext(context
->glCtx
))
1256 DWORD err
= GetLastError();
1257 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1261 DWORD
context_get_tls_idx(void)
1263 return wined3d_context_tls_idx
;
1266 void context_set_tls_idx(DWORD idx
)
1268 wined3d_context_tls_idx
= idx
;
1271 struct wined3d_context
*context_get_current(void)
1273 return TlsGetValue(wined3d_context_tls_idx
);
1276 BOOL
context_set_current(struct wined3d_context
*ctx
)
1278 struct wined3d_context
*old
= context_get_current();
1282 TRACE("Already using D3D context %p.\n", ctx
);
1290 TRACE("Switching away from destroyed context %p.\n", old
);
1291 context_destroy_gl_resources(old
);
1292 HeapFree(GetProcessHeap(), 0, (void *)old
->gl_info
);
1293 HeapFree(GetProcessHeap(), 0, old
);
1297 if (wglGetCurrentContext())
1299 TRACE("Flushing context %p before switching to %p.\n", old
, ctx
);
1310 ERR("Trying to make invalid context %p current\n", ctx
);
1314 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1315 if (!context_set_gl_context(ctx
))
1319 else if(wglGetCurrentContext())
1321 TRACE("Clearing current D3D context.\n");
1322 if (!wglMakeCurrent(NULL
, NULL
))
1324 DWORD err
= GetLastError();
1325 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1326 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1331 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1334 void context_release(struct wined3d_context
*context
)
1336 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1340 if (!context
->level
)
1341 WARN("Context %p is not active.\n", context
);
1342 else if (context
!= context_get_current())
1343 WARN("Context %p is not the current context.\n", context
);
1346 if (!--context
->level
)
1348 if (context_restore_pixel_format(context
))
1349 context
->needs_set
= 1;
1350 if (context
->restore_ctx
)
1352 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1353 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
);
1354 context
->restore_ctx
= NULL
;
1355 context
->restore_dc
= NULL
;
1360 /* This is used when a context for render target A is active, but a separate context is
1361 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1362 * A to avoid breaking caller code. */
1363 void context_restore(struct wined3d_context
*context
, struct wined3d_surface
*restore
)
1365 if (context
->current_rt
.texture
!= restore
->container
1366 || context
->current_rt
.sub_resource_idx
!= surface_get_sub_resource_idx(restore
))
1368 context_release(context
);
1369 context
= context_acquire(restore
->container
->resource
.device
, restore
);
1372 context_release(context
);
1375 static void context_enter(struct wined3d_context
*context
)
1377 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1379 if (!context
->level
++)
1381 const struct wined3d_context
*current_context
= context_get_current();
1382 HGLRC current_gl
= wglGetCurrentContext();
1384 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1386 TRACE("Another GL context (%p on device context %p) is already current.\n",
1387 current_gl
, wglGetCurrentDC());
1388 context
->restore_ctx
= current_gl
;
1389 context
->restore_dc
= wglGetCurrentDC();
1390 context
->needs_set
= 1;
1392 else if (!context
->needs_set
&& !(context
->hdc_is_private
&& context
->hdc_has_format
)
1393 && context
->pixel_format
!= context
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context
->hdc
))
1394 context
->needs_set
= 1;
1398 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1400 DWORD rep
= context
->state_table
[state
].representative
;
1404 if (isStateDirty(context
, rep
)) return;
1406 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1407 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1408 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1409 context
->isStateDirty
[idx
] |= (1u << shift
);
1412 /* This function takes care of wined3d pixel format selection. */
1413 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1414 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1415 BOOL auxBuffers
, BOOL findCompatible
)
1418 unsigned int current_value
;
1419 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1422 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1423 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1424 auxBuffers
, findCompatible
);
1427 for (i
= 0; i
< cfg_count
; ++i
)
1429 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1432 /* For now only accept RGBA formats. Perhaps some day we will
1433 * allow floating point formats for pbuffers. */
1434 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1436 /* In window mode we need a window drawable format and double buffering. */
1437 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1439 if (cfg
->redSize
< color_format
->red_size
)
1441 if (cfg
->greenSize
< color_format
->green_size
)
1443 if (cfg
->blueSize
< color_format
->blue_size
)
1445 if (cfg
->alphaSize
< color_format
->alpha_size
)
1447 if (cfg
->depthSize
< ds_format
->depth_size
)
1449 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1451 /* Check multisampling support. */
1452 if (cfg
->numSamples
)
1456 /* We try to locate a format which matches our requirements exactly. In case of
1457 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1458 if (cfg
->depthSize
== ds_format
->depth_size
)
1460 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1462 if (cfg
->alphaSize
== color_format
->alpha_size
)
1464 /* We like to have aux buffers in backbuffer mode */
1465 if (auxBuffers
&& cfg
->auxBuffers
)
1467 if (cfg
->redSize
== color_format
->red_size
1468 && cfg
->greenSize
== color_format
->green_size
1469 && cfg
->blueSize
== color_format
->blue_size
)
1472 if (value
> current_value
)
1474 iPixelFormat
= cfg
->iPixelFormat
;
1475 current_value
= value
;
1479 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1480 if(!iPixelFormat
&& !findCompatible
) {
1481 ERR("Can't find a suitable iPixelFormat\n");
1483 } else if(!iPixelFormat
) {
1484 PIXELFORMATDESCRIPTOR pfd
;
1486 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1487 /* PixelFormat selection */
1488 ZeroMemory(&pfd
, sizeof(pfd
));
1489 pfd
.nSize
= sizeof(pfd
);
1491 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1492 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1493 pfd
.cAlphaBits
= color_format
->alpha_size
;
1494 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1495 + color_format
->blue_size
+ color_format
->alpha_size
;
1496 pfd
.cDepthBits
= ds_format
->depth_size
;
1497 pfd
.cStencilBits
= ds_format
->stencil_size
;
1498 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1500 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1502 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1503 ERR("Can't find a suitable iPixelFormat\n");
1508 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1509 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1510 return iPixelFormat
;
1513 /* Context activation is done by the caller. */
1514 static void bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1516 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1517 unsigned int i
, count
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
);
1519 for (i
= 0; i
< count
; ++i
)
1521 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1522 checkGLcall("glActiveTexture");
1524 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[i
]);
1525 checkGLcall("glBindTexture");
1527 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1529 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[i
]);
1530 checkGLcall("glBindTexture");
1533 if (gl_info
->supported
[EXT_TEXTURE3D
])
1535 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[i
]);
1536 checkGLcall("glBindTexture");
1539 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1541 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[i
]);
1542 checkGLcall("glBindTexture");
1545 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
1547 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, device
->dummy_texture_2d_array
[i
]);
1548 checkGLcall("glBindTexture");
1553 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1555 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1556 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1559 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1560 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1564 case GL_DEBUG_TYPE_ERROR_ARB
:
1565 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1568 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1569 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1570 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1571 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1574 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1575 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1579 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1584 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1587 unsigned int ctx_attrib_idx
= 0;
1588 GLint ctx_attribs
[7], ctx_flags
= 0;
1590 if (context_debug_output_enabled(gl_info
))
1591 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1592 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1593 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1594 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1595 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1596 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1597 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1600 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1601 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1603 ctx_attribs
[ctx_attrib_idx
] = 0;
1605 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1607 if (ctx_flags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
1609 ctx_attribs
[ctx_attrib_idx
- 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1610 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1611 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1618 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1619 struct wined3d_texture
*target
, const struct wined3d_format
*ds_format
)
1621 struct wined3d_device
*device
= swapchain
->device
;
1622 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1623 const struct wined3d_format
*color_format
;
1624 struct wined3d_context
*ret
;
1625 BOOL auxBuffers
= FALSE
;
1626 HGLRC ctx
, share_ctx
;
1631 BOOL hdc_is_private
= FALSE
;
1633 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1635 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1639 ret
->blit_targets
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1640 gl_info
->limits
.buffers
* sizeof(*ret
->blit_targets
));
1641 if (!ret
->blit_targets
)
1644 ret
->draw_buffers
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1645 gl_info
->limits
.buffers
* sizeof(*ret
->draw_buffers
));
1646 if (!ret
->draw_buffers
)
1649 ret
->fbo_key
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1650 FIELD_OFFSET(struct wined3d_fbo_entry_key
, objects
[gl_info
->limits
.buffers
+ 1]));
1654 ret
->free_timestamp_query_size
= 4;
1655 ret
->free_timestamp_queries
= HeapAlloc(GetProcessHeap(), 0,
1656 ret
->free_timestamp_query_size
* sizeof(*ret
->free_timestamp_queries
));
1657 if (!ret
->free_timestamp_queries
)
1659 list_init(&ret
->timestamp_queries
);
1661 ret
->free_occlusion_query_size
= 4;
1662 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1663 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1664 if (!ret
->free_occlusion_queries
)
1667 list_init(&ret
->occlusion_queries
);
1669 ret
->free_event_query_size
= 4;
1670 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1671 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1672 if (!ret
->free_event_queries
)
1675 list_init(&ret
->event_queries
);
1676 list_init(&ret
->fbo_list
);
1677 list_init(&ret
->fbo_destroy_list
);
1679 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1681 ERR("Failed to allocate shader backend context data.\n");
1684 if (!device
->adapter
->fragment_pipe
->allocate_context_data(ret
))
1686 ERR("Failed to allocate fragment pipeline context data.\n");
1690 /* Initialize the texture unit mapping to a 1:1 mapping */
1691 for (s
= 0; s
< MAX_COMBINED_SAMPLERS
; ++s
)
1693 if (s
< gl_info
->limits
.combined_samplers
)
1695 ret
->tex_unit_map
[s
] = s
;
1696 ret
->rev_tex_unit_map
[s
] = s
;
1700 ret
->tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1701 ret
->rev_tex_unit_map
[s
] = WINED3D_UNMAPPED_STAGE
;
1705 if (!(hdc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1707 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1709 if ((hdc
= swapchain_get_backup_dc(swapchain
)))
1710 hdc_is_private
= TRUE
;
1713 ERR("Failed to retrieve a device context.\n");
1718 color_format
= target
->resource
.format
;
1720 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1721 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1722 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1726 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1727 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
);
1728 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1729 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1732 /* DirectDraw supports 8bit paletted render targets and these are used by
1733 * old games like StarCraft and C&C. Most modern hardware doesn't support
1734 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1735 * conversion (ab)uses the alpha component for storing the palette index.
1736 * For this reason we require a format with 8bit alpha, so request
1738 if (color_format
->id
== WINED3DFMT_P8_UINT
)
1739 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1741 /* When "always_offscreen" is enabled, we only use the drawable for
1742 * presentation blits, and don't do any rendering to it. That means we
1743 * don't need depth or stencil buffers, and can mostly ignore the render
1744 * target format. This wouldn't necessarily be quite correct for 10bpc
1745 * display modes, but we don't currently support those.
1746 * Using the same format regardless of the color/depth/stencil targets
1747 * makes it much less likely that different wined3d instances will set
1748 * conflicting pixel formats. */
1749 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
1750 && wined3d_settings
.always_offscreen
)
1752 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
);
1753 ds_format
= wined3d_get_format(gl_info
, WINED3DFMT_UNKNOWN
);
1756 /* Try to find a pixel format which matches our requirements. */
1757 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, FALSE
);
1759 /* Try to locate a compatible format if we weren't able to find anything. */
1762 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1763 pixel_format
= context_choose_pixel_format(device
, hdc
, color_format
, ds_format
, auxBuffers
, TRUE
);
1766 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1769 ERR("Can't find a suitable pixel format.\n");
1773 ret
->gl_info
= gl_info
;
1777 if (!context_set_pixel_format(ret
, hdc
, hdc_is_private
, pixel_format
))
1779 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1780 context_release(ret
);
1784 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
1785 if (gl_info
->p_wglCreateContextAttribsARB
)
1787 if (!(ctx
= context_create_wgl_attribs(gl_info
, hdc
, share_ctx
)))
1792 if (!(ctx
= wglCreateContext(hdc
)))
1794 ERR("Failed to create a WGL context.\n");
1795 context_release(ret
);
1799 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
1801 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
1802 context_release(ret
);
1803 if (!wglDeleteContext(ctx
))
1804 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1809 if (!device_context_add(device
, ret
))
1811 ERR("Failed to add the newly created context to the context list\n");
1812 context_release(ret
);
1813 if (!wglDeleteContext(ctx
))
1814 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1818 ret
->d3d_info
= &device
->adapter
->d3d_info
;
1819 ret
->state_table
= device
->StateTable
;
1821 /* Mark all states dirty to force a proper initialization of the states
1822 * on the first use of the context. */
1823 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1825 if (ret
->state_table
[state
].representative
)
1826 context_invalidate_state(ret
, state
);
1829 ret
->swapchain
= swapchain
;
1830 ret
->current_rt
.texture
= target
;
1831 ret
->current_rt
.sub_resource_idx
= 0;
1832 ret
->tid
= GetCurrentThreadId();
1834 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->resource
);
1835 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
1839 ret
->win_handle
= swapchain
->win_handle
;
1841 ret
->hdc_is_private
= hdc_is_private
;
1842 ret
->hdc_has_format
= TRUE
;
1843 ret
->pixel_format
= pixel_format
;
1846 /* Set up the context defaults */
1847 if (!context_set_current(ret
))
1849 ERR("Cannot activate context to set up defaults.\n");
1850 device_context_remove(device
, ret
);
1851 context_release(ret
);
1852 if (!wglDeleteContext(ctx
))
1853 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
1857 if (context_debug_output_enabled(gl_info
))
1859 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, ret
));
1860 if (TRACE_ON(d3d_synchronous
))
1861 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
1862 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
1865 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
1866 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1870 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
1871 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1872 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
1873 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1874 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
1875 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1877 if (WARN_ON(d3d_perf
))
1879 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
1880 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
1884 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1886 TRACE("Setting up the screen\n");
1888 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
1890 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1891 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1893 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1894 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1896 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1897 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1903 GL_EXTCALL(glGenVertexArrays(1, &vao
));
1904 GL_EXTCALL(glBindVertexArray(vao
));
1905 checkGLcall("creating VAO");
1908 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1909 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1910 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
1911 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1913 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1915 /* Direct3D always uses n-1 weights for n world matrices and uses
1916 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1917 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1918 * enabled as well. */
1919 gl_info
->gl_ops
.gl
.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1920 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1922 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1924 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1925 * the previous texture where to source the offset from is always unit - 1.
1927 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1929 context_active_texture(ret
, gl_info
, s
);
1930 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
1931 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1932 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1935 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1937 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1938 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1939 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1940 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1943 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1944 * program and the dummy program is destroyed when the context is destroyed.
1946 static const char dummy_program
[] =
1948 "MOV result.color, fragment.color.primary;\n"
1950 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1951 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1952 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1955 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1957 for (s
= 0; s
< gl_info
->limits
.textures
; ++s
)
1959 context_active_texture(ret
, gl_info
, s
);
1960 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1961 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1965 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1967 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1969 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1971 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1973 device
->shader_backend
->shader_init_context_state(ret
);
1974 ret
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
1975 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
1976 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
1977 | (1u << WINED3D_SHADER_TYPE_HULL
)
1978 | (1u << WINED3D_SHADER_TYPE_DOMAIN
);
1980 /* If this happens to be the first context for the device, dummy textures
1981 * are not created yet. In that case, they will be created (and bound) by
1982 * create_dummy_textures right after this context is initialized. */
1983 if (device
->dummy_texture_2d
[0])
1984 bind_dummy_textures(device
, ret
);
1986 TRACE("Created context %p.\n", ret
);
1991 if (hdc
) wined3d_release_dc(swapchain
->win_handle
, hdc
);
1992 device
->shader_backend
->shader_free_context_data(ret
);
1993 device
->adapter
->fragment_pipe
->free_context_data(ret
);
1994 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1995 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1996 HeapFree(GetProcessHeap(), 0, ret
->free_timestamp_queries
);
1997 HeapFree(GetProcessHeap(), 0, ret
->fbo_key
);
1998 HeapFree(GetProcessHeap(), 0, ret
->draw_buffers
);
1999 HeapFree(GetProcessHeap(), 0, ret
->blit_targets
);
2000 HeapFree(GetProcessHeap(), 0, ret
);
2004 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
2008 TRACE("Destroying ctx %p\n", context
);
2010 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
2012 context_destroy_gl_resources(context
);
2013 TlsSetValue(wined3d_context_tls_idx
, NULL
);
2018 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2019 in wined3d_adapter may go away in the meantime */
2020 struct wined3d_gl_info
*gl_info
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info
));
2021 *gl_info
= *context
->gl_info
;
2022 context
->gl_info
= gl_info
;
2023 context
->destroyed
= 1;
2027 device
->shader_backend
->shader_free_context_data(context
);
2028 device
->adapter
->fragment_pipe
->free_context_data(context
);
2029 HeapFree(GetProcessHeap(), 0, context
->fbo_key
);
2030 HeapFree(GetProcessHeap(), 0, context
->draw_buffers
);
2031 HeapFree(GetProcessHeap(), 0, context
->blit_targets
);
2032 device_context_remove(device
, context
);
2033 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
2036 /* Context activation is done by the caller. */
2037 static void set_blit_dimension(const struct wined3d_gl_info
*gl_info
, UINT width
, UINT height
)
2039 const GLdouble projection
[] =
2041 2.0 / width
, 0.0, 0.0, 0.0,
2042 0.0, 2.0 / height
, 0.0, 0.0,
2044 -1.0, -1.0, -1.0, 1.0,
2047 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
2048 checkGLcall("glMatrixMode(GL_PROJECTION)");
2049 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
2050 checkGLcall("glLoadMatrixd");
2051 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, width
, height
);
2052 checkGLcall("glViewport");
2055 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
2057 const struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2060 if (rt
->swapchain
&& rt
->swapchain
->front_buffer
== rt
)
2064 GetClientRect(context
->win_handle
, &window_size
);
2065 size
->cx
= window_size
.right
- window_size
.left
;
2066 size
->cy
= window_size
.bottom
- window_size
.top
;
2071 level
= context
->current_rt
.sub_resource_idx
% rt
->level_count
;
2072 size
->cx
= wined3d_texture_get_level_width(rt
, level
);
2073 size
->cy
= wined3d_texture_get_level_height(rt
, level
);
2076 /*****************************************************************************
2079 * Sets up a context for DirectDraw blitting.
2080 * All texture units are disabled, texture unit 0 is set as current unit
2081 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
2082 * color writing enabled for all channels
2083 * register combiners disabled, shaders disabled
2084 * world matrix is set to identity, texture matrix 0 too
2085 * projection matrix is setup for drawing screen coordinates
2088 * This: Device to activate the context for
2089 * context: Context to setup
2091 *****************************************************************************/
2092 /* Context activation is done by the caller. */
2093 static void SetupForBlit(const struct wined3d_device
*device
, struct wined3d_context
*context
)
2096 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2100 TRACE("Setting up context %p for blitting\n", context
);
2102 context_get_rt_size(context
, &rt_size
);
2104 if (context
->last_was_blit
)
2106 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
2108 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2109 context
->blit_w
= rt_size
.cx
;
2110 context
->blit_h
= rt_size
.cy
;
2111 /* No need to dirtify here, the states are still dirtified because
2112 * they weren't applied since the last SetupForBlit() call. */
2114 TRACE("Context is already set up for blitting, nothing to do\n");
2117 context
->last_was_blit
= TRUE
;
2119 /* Disable all textures. The caller can then bind a texture it wants to blit
2122 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
2123 * function texture unit. No need to care for higher samplers
2125 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
2127 sampler
= context
->rev_tex_unit_map
[i
];
2128 context_active_texture(context
, gl_info
, i
);
2130 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2132 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2133 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2135 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2136 checkGLcall("glDisable GL_TEXTURE_3D");
2137 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2139 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2140 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2142 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2143 checkGLcall("glDisable GL_TEXTURE_2D");
2145 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2146 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
2148 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2150 if (sampler
< MAX_TEXTURES
)
2151 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2152 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2155 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
2156 GL_EXTCALL(glBindSampler(0, 0));
2157 context_active_texture(context
, gl_info
, 0);
2159 sampler
= context
->rev_tex_unit_map
[0];
2161 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2163 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2164 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2166 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2167 checkGLcall("glDisable GL_TEXTURE_3D");
2168 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2170 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2171 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2173 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2174 checkGLcall("glDisable GL_TEXTURE_2D");
2176 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2178 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
2179 checkGLcall("glMatrixMode(GL_TEXTURE)");
2180 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2181 checkGLcall("glLoadIdentity()");
2183 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
2185 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
2186 GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
2187 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2190 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2192 if (sampler
< MAX_TEXTURES
)
2194 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
2195 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2197 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2200 /* Other misc states */
2201 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2202 checkGLcall("glDisable(GL_ALPHA_TEST)");
2203 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
2204 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
2205 checkGLcall("glDisable GL_LIGHTING");
2206 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
2207 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
2208 checkGLcall("glDisable GL_DEPTH_TEST");
2209 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
2210 glDisableWINE(GL_FOG
);
2211 checkGLcall("glDisable GL_FOG");
2212 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
2213 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2214 checkGLcall("glDisable GL_BLEND");
2215 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2216 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
2217 checkGLcall("glDisable GL_CULL_FACE");
2218 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
2219 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
2220 checkGLcall("glDisable GL_STENCIL_TEST");
2221 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
2222 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
2223 checkGLcall("glDisable GL_SCISSOR_TEST");
2224 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2225 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2227 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
2228 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2229 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
2231 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
2232 checkGLcall("glColorMask");
2233 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
2234 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
2235 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
2236 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2237 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2239 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2240 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2241 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2244 /* Setup transforms */
2245 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2246 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2247 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2248 checkGLcall("glLoadIdentity()");
2249 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2251 context
->last_was_rhw
= TRUE
;
2252 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2254 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
2255 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
2256 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
2257 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
2258 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
2259 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
2260 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2262 set_blit_dimension(gl_info
, rt_size
.cx
, rt_size
.cy
);
2264 /* Disable shaders */
2265 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2267 context
->blit_w
= rt_size
.cx
;
2268 context
->blit_h
= rt_size
.cy
;
2269 context_invalidate_state(context
, STATE_VIEWPORT
);
2270 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2273 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2275 return rt_mask
& (1u << 31);
2278 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2280 return rt_mask
& ~(1u << 31);
2283 /* Context activation is done by the caller. */
2284 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2286 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2290 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2291 checkGLcall("glDrawBuffer()");
2293 else if (is_rt_mask_onscreen(rt_mask
))
2295 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2296 checkGLcall("glDrawBuffer()");
2300 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2307 context
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2309 context
->draw_buffers
[i
] = GL_NONE
;
2315 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2317 GL_EXTCALL(glDrawBuffers(i
, context
->draw_buffers
));
2318 checkGLcall("glDrawBuffers()");
2322 gl_info
->gl_ops
.gl
.p_glDrawBuffer(context
->draw_buffers
[0]);
2323 checkGLcall("glDrawBuffer()");
2328 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2333 /* Context activation is done by the caller. */
2334 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2336 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2337 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2338 DWORD new_mask
= context_generate_rt_mask(buffer
);
2340 if (new_mask
== *current_mask
)
2343 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2344 checkGLcall("glDrawBuffer()");
2346 *current_mask
= new_mask
;
2349 /* Context activation is done by the caller. */
2350 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2352 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2353 checkGLcall("glActiveTexture");
2354 context
->active_texture
= unit
;
2357 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2359 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2360 DWORD unit
= context
->active_texture
;
2361 DWORD old_texture_type
= context
->texture_type
[unit
];
2365 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2366 checkGLcall("glBindTexture");
2373 if (old_texture_type
!= target
)
2375 const struct wined3d_device
*device
= context
->swapchain
->device
;
2377 switch (old_texture_type
)
2383 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, device
->dummy_texture_2d
[unit
]);
2384 checkGLcall("glBindTexture");
2386 case GL_TEXTURE_2D_ARRAY
:
2387 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, device
->dummy_texture_2d_array
[unit
]);
2388 checkGLcall("glBindTexture");
2390 case GL_TEXTURE_RECTANGLE_ARB
:
2391 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, device
->dummy_texture_rect
[unit
]);
2392 checkGLcall("glBindTexture");
2394 case GL_TEXTURE_CUBE_MAP
:
2395 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, device
->dummy_texture_cube
[unit
]);
2396 checkGLcall("glBindTexture");
2399 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, device
->dummy_texture_3d
[unit
]);
2400 checkGLcall("glBindTexture");
2403 ERR("Unexpected texture target %#x.\n", old_texture_type
);
2406 context
->texture_type
[unit
] = target
;
2410 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2412 if (context
->render_offscreen
== offscreen
) return;
2414 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2415 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2416 context_invalidate_state(context
, STATE_VIEWPORT
);
2417 context_invalidate_state(context
, STATE_SCISSORRECT
);
2418 context_invalidate_state(context
, STATE_FRONTFACE
);
2419 context
->render_offscreen
= offscreen
;
2422 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2423 const struct wined3d_format
*required
)
2425 if (existing
== required
)
2427 if ((existing
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
)
2428 != (required
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
))
2430 if (existing
->depth_size
< required
->depth_size
)
2432 /* If stencil bits are used the exact amount is required - otherwise
2433 * wrapping won't work correctly. */
2434 if (required
->stencil_size
&& required
->stencil_size
!= existing
->stencil_size
)
2439 /* Context activation is done by the caller. */
2440 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2441 const struct wined3d_rendertarget_view
*depth_stencil
)
2443 /* Onscreen surfaces are always in a swapchain */
2444 struct wined3d_swapchain
*swapchain
= context
->current_rt
.texture
->swapchain
;
2445 struct wined3d_surface
*surface
;
2447 if (context
->render_offscreen
|| !depth_stencil
) return;
2448 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2450 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2451 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2453 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2455 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2456 surface
= context
->current_rt
.texture
->sub_resources
[context
->current_rt
.sub_resource_idx
].u
.surface
;
2457 surface_load_location(surface
, context
, WINED3D_LOCATION_TEXTURE_RGB
);
2458 swapchain
->render_to_fbo
= TRUE
;
2459 swapchain_update_draw_bindings(swapchain
);
2460 context_set_render_offscreen(context
, TRUE
);
2463 GLenum
context_get_offscreen_gl_buffer(const struct wined3d_context
*context
)
2465 switch (wined3d_settings
.offscreen_rendering_mode
)
2468 return GL_COLOR_ATTACHMENT0
;
2470 case ORM_BACKBUFFER
:
2471 return context
->aux_buffers
> 0 ? GL_AUX0
: GL_BACK
;
2474 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
2479 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_context
*context
, struct wined3d_texture
*rt
)
2481 if (!rt
|| rt
->resource
.format
->id
== WINED3DFMT_NULL
)
2483 else if (rt
->swapchain
)
2484 return context_generate_rt_mask_from_resource(&rt
->resource
);
2486 return context_generate_rt_mask(context_get_offscreen_gl_buffer(context
));
2489 /* Context activation is done by the caller. */
2490 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2492 struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2493 struct wined3d_surface
*surface
;
2494 DWORD rt_mask
, *cur_mask
;
2496 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2498 context_validate_onscreen_formats(context
, NULL
);
2500 if (context
->render_offscreen
)
2502 wined3d_texture_load(rt
, context
, FALSE
);
2504 surface
= rt
->sub_resources
[context
->current_rt
.sub_resource_idx
].u
.surface
;
2505 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, surface
, NULL
, rt
->resource
.draw_binding
);
2506 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2513 context
->current_fbo
= NULL
;
2514 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2515 rt_mask
= context_generate_rt_mask_from_resource(&rt
->resource
);
2520 rt_mask
= context_generate_rt_mask_no_fbo(context
, rt
);
2523 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2525 if (rt_mask
!= *cur_mask
)
2527 context_apply_draw_buffers(context
, rt_mask
);
2528 *cur_mask
= rt_mask
;
2531 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2533 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2536 SetupForBlit(device
, context
);
2537 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2540 static BOOL
context_validate_rt_config(UINT rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2541 const struct wined3d_rendertarget_view
*ds
)
2545 if (ds
) return TRUE
;
2547 for (i
= 0; i
< rt_count
; ++i
)
2549 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2553 WARN("Invalid render target config, need at least one attachment.\n");
2557 /* Context activation is done by the caller. */
2558 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_state
*state
,
2559 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2561 struct wined3d_rendertarget_view
**rts
= fb
->render_targets
;
2562 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
2563 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2564 DWORD rt_mask
= 0, *cur_mask
;
2567 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= state
->fb
2568 || rt_count
!= gl_info
->limits
.buffers
)
2570 if (!context_validate_rt_config(rt_count
, rts
, dsv
))
2573 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2575 context_validate_onscreen_formats(context
, dsv
);
2577 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
2579 for (i
= 0; i
< rt_count
; ++i
)
2581 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(rts
[i
]);
2582 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2583 rt_mask
|= (1u << i
);
2585 while (i
< gl_info
->limits
.buffers
)
2587 context
->blit_targets
[i
] = NULL
;
2590 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2591 wined3d_rendertarget_view_get_surface(dsv
),
2592 rt_count
? rts
[0]->resource
->draw_binding
: 0,
2593 dsv
? dsv
->resource
->draw_binding
: 0);
2597 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2598 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2599 rt_mask
= context_generate_rt_mask_from_resource(rts
[0]->resource
);
2602 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2603 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2604 * state management allows this */
2605 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2609 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2610 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0])->container
: NULL
);
2613 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
2614 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
2616 for (i
= 0; i
< rt_count
; ++i
)
2618 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2619 rt_mask
|= (1u << i
);
2624 rt_mask
= context_generate_rt_mask_no_fbo(context
,
2625 rt_count
? wined3d_rendertarget_view_get_surface(rts
[0])->container
: NULL
);
2628 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2630 if (rt_mask
!= *cur_mask
)
2632 context_apply_draw_buffers(context
, rt_mask
);
2633 *cur_mask
= rt_mask
;
2634 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2637 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2639 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2642 if (context
->last_was_blit
)
2643 context
->last_was_blit
= FALSE
;
2645 /* Blending and clearing should be orthogonal, but tests on the nvidia
2646 * driver show that disabling blending when clearing improves the clearing
2647 * performance incredibly. */
2648 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2649 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
2650 if (rt_count
&& gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
2652 if (needs_srgb_write(context
, state
, fb
))
2653 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
2655 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
2656 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
2658 checkGLcall("setting up state for clear");
2660 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2661 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2662 context_invalidate_state(context
, STATE_SCISSORRECT
);
2667 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_state
*state
)
2669 struct wined3d_rendertarget_view
**rts
= state
->fb
->render_targets
;
2670 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
2671 DWORD rt_mask
, rt_mask_bits
;
2674 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
2675 return context_generate_rt_mask_no_fbo(context
, wined3d_rendertarget_view_get_surface(rts
[0])->container
);
2676 else if (!context
->render_offscreen
)
2677 return context_generate_rt_mask_from_resource(rts
[0]->resource
);
2679 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
2680 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
2681 rt_mask_bits
= rt_mask
;
2683 while (rt_mask_bits
)
2685 rt_mask_bits
&= ~(1u << i
);
2686 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
2687 rt_mask
&= ~(1u << i
);
2695 /* Context activation is done by the caller. */
2696 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2698 DWORD rt_mask
= find_draw_buffers_mask(context
, state
);
2699 const struct wined3d_fb_state
*fb
= state
->fb
;
2702 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2704 if (!context
->render_offscreen
)
2706 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, NULL
,
2707 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
2713 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
2715 context
->blit_targets
[i
] = wined3d_rendertarget_view_get_surface(fb
->render_targets
[i
]);
2717 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
,
2718 wined3d_rendertarget_view_get_surface(fb
->depth_stencil
),
2719 fb
->render_targets
[0] ? fb
->render_targets
[0]->resource
->draw_binding
: 0,
2720 fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
2724 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2725 if (rt_mask
!= *cur_mask
)
2727 context_apply_draw_buffers(context
, rt_mask
);
2728 *cur_mask
= rt_mask
;
2732 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
2734 DWORD i
= context
->rev_tex_unit_map
[unit
];
2735 DWORD j
= context
->tex_unit_map
[stage
];
2737 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
2738 context
->tex_unit_map
[stage
] = unit
;
2739 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
2740 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2742 context
->rev_tex_unit_map
[unit
] = stage
;
2743 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
2744 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
2747 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
2751 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
2752 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
2755 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
2756 const struct wined3d_state
*state
)
2760 context
->fixed_function_usage_map
= 0;
2761 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
2763 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
2764 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
2765 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
2766 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
2767 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
2768 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
2769 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
2770 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
2772 /* Not used, and disable higher stages. */
2773 if (color_op
== WINED3D_TOP_DISABLE
)
2776 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
2777 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
2778 || ((color_arg3
== WINED3DTA_TEXTURE
)
2779 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
2780 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
2781 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
2782 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
2783 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
2784 context
->fixed_function_usage_map
|= (1u << i
);
2786 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
2787 && i
< MAX_TEXTURES
- 1)
2788 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
2791 if (i
< context
->lowest_disabled_stage
)
2794 end
= context
->lowest_disabled_stage
;
2798 start
= context
->lowest_disabled_stage
;
2802 context
->lowest_disabled_stage
= i
;
2803 for (i
= start
+ 1; i
< end
; ++i
)
2805 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
2809 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
2810 const struct wined3d_state
*state
)
2812 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2813 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2814 unsigned int i
, tex
;
2817 context_update_fixed_function_usage_map(context
, state
);
2819 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2822 ffu_map
= context
->fixed_function_usage_map
;
2824 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
2825 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
2827 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2832 if (context
->tex_unit_map
[i
] != i
)
2834 context_map_stage(context
, i
, i
);
2835 context_invalidate_state(context
, STATE_SAMPLER(i
));
2836 context_invalidate_texture_stage(context
, i
);
2842 /* Now work out the mapping */
2844 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
2849 if (context
->tex_unit_map
[i
] != tex
)
2851 context_map_stage(context
, i
, tex
);
2852 context_invalidate_state(context
, STATE_SAMPLER(i
));
2853 context_invalidate_texture_stage(context
, i
);
2860 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2862 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
2863 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2864 const struct wined3d_shader_resource_info
*resource_info
=
2865 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2868 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2871 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
2873 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
2875 context_map_stage(context
, i
, i
);
2876 context_invalidate_state(context
, STATE_SAMPLER(i
));
2877 if (i
< d3d_info
->limits
.ffp_blend_stages
)
2878 context_invalidate_texture_stage(context
, i
);
2883 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
2884 const struct wined3d_shader_resource_info
*ps_resource_info
, DWORD unit
)
2886 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
2888 /* Not currently used */
2889 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
2892 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
2894 /* Used by a fragment sampler */
2896 if (!ps_resource_info
)
2898 /* No pixel shader, check fixed function */
2899 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1u << current_mapping
));
2902 /* Pixel shader, check the shader's sampler map */
2903 return !ps_resource_info
[current_mapping
].type
;
2909 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
2911 const struct wined3d_shader_resource_info
*vs_resource_info
=
2912 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
2913 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
2914 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2915 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.combined_samplers
) - 1;
2918 if (gl_info
->limits
.combined_samplers
>= MAX_COMBINED_SAMPLERS
)
2921 /* Note that we only care if a resource is used or not, not the
2922 * resource's specific type. Otherwise we'd need to call
2923 * shader_update_samplers() here for 1.x pixelshaders. */
2925 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
2927 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
2929 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
2930 if (vs_resource_info
[i
].type
)
2934 if (context_unit_free_for_vs(context
, ps_resource_info
, start
))
2936 if (context
->tex_unit_map
[vsampler_idx
] != start
)
2938 context_map_stage(context
, vsampler_idx
, start
);
2939 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
2948 if (context
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
2949 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
2954 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
2956 BOOL vs
= use_vs(state
);
2957 BOOL ps
= use_ps(state
);
2959 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
2960 * need a 1:1 map at the moment.
2961 * When the mapping of a stage is changed, sampler and ALL texture stage
2962 * states have to be reset. */
2965 context_map_psamplers(context
, state
);
2967 context_map_fixed_function_samplers(context
, state
);
2970 context_map_vsamplers(context
, ps
, state
);
2973 /* Context activation is done by the caller. */
2974 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
2976 DWORD rt_mask
, *cur_mask
;
2978 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
2980 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2981 rt_mask
= find_draw_buffers_mask(context
, state
);
2982 if (rt_mask
!= *cur_mask
)
2984 context_apply_draw_buffers(context
, rt_mask
);
2985 *cur_mask
= rt_mask
;
2989 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
2991 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
2992 *regnum
= WINED3D_FFP_POSITION
;
2993 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
2994 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
2995 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
2996 *regnum
= WINED3D_FFP_BLENDINDICES
;
2997 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
2998 *regnum
= WINED3D_FFP_NORMAL
;
2999 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
3000 *regnum
= WINED3D_FFP_PSIZE
;
3001 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
3002 *regnum
= WINED3D_FFP_DIFFUSE
;
3003 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
3004 *regnum
= WINED3D_FFP_SPECULAR
;
3005 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
3006 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
3009 FIXME("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
3017 /* Context activation is done by the caller. */
3018 void context_stream_info_from_declaration(struct wined3d_context
*context
,
3019 const struct wined3d_state
*state
, struct wined3d_stream_info
*stream_info
)
3021 /* We need to deal with frequency data! */
3022 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
3023 BOOL use_vshader
= use_vs(state
);
3024 BOOL generic_attributes
= context
->d3d_info
->ffp_generic_attributes
;
3027 stream_info
->use_map
= 0;
3028 stream_info
->swizzle_map
= 0;
3029 stream_info
->position_transformed
= declaration
->position_transformed
;
3031 /* Translate the declaration into strided data. */
3032 for (i
= 0; i
< declaration
->element_count
; ++i
)
3034 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
3035 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
3039 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
3040 element
, i
+ 1, declaration
->element_count
);
3042 if (!stream
->buffer
)
3045 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
3049 if (element
->output_slot
== WINED3D_OUTPUT_SLOT_UNUSED
)
3051 stride_used
= FALSE
;
3053 else if (element
->output_slot
== WINED3D_OUTPUT_SLOT_SEMANTIC
)
3055 /* TODO: Assuming vertexdeclarations are usually used with the
3056 * same or a similar shader, it might be worth it to store the
3057 * last used output slot and try that one first. */
3058 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
3059 element
->usage
, element
->usage_idx
, &idx
);
3063 idx
= element
->output_slot
;
3069 if (!generic_attributes
&& !element
->ffp_valid
)
3071 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3072 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
3073 stride_used
= FALSE
;
3077 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
3083 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3084 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3085 use_vshader
? "shader": "fixed function", idx
,
3086 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
3087 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
),
3088 debug_d3dinput_classification(element
->input_slot_class
), element
->instance_data_step_rate
);
3090 stream_info
->elements
[idx
].format
= element
->format
;
3091 stream_info
->elements
[idx
].data
.buffer_object
= 0;
3092 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
3093 stream_info
->elements
[idx
].stride
= stream
->stride
;
3094 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
3095 if (stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
3097 stream_info
->elements
[idx
].divisor
= 1;
3099 else if (element
->input_slot_class
== WINED3D_INPUT_PER_INSTANCE_DATA
)
3101 stream_info
->elements
[idx
].divisor
= element
->instance_data_step_rate
;
3102 if (!element
->instance_data_step_rate
)
3103 FIXME("Instance step rate 0 not implemented.\n");
3107 stream_info
->elements
[idx
].divisor
= 0;
3110 if (!context
->gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3111 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
3113 stream_info
->swizzle_map
|= 1u << idx
;
3115 stream_info
->use_map
|= 1u << idx
;
3120 /* Context activation is done by the caller. */
3121 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3123 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3124 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3125 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
3126 DWORD prev_all_vbo
= stream_info
->all_vbo
;
3130 context_stream_info_from_declaration(context
, state
, stream_info
);
3132 stream_info
->all_vbo
= 1;
3133 context
->num_buffer_queries
= 0;
3134 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
3136 struct wined3d_stream_info_element
*element
;
3137 struct wined3d_bo_address data
;
3138 struct wined3d_buffer
*buffer
;
3143 element
= &stream_info
->elements
[i
];
3144 buffer
= state
->streams
[element
->stream_idx
].buffer
;
3146 /* We can't use VBOs if the base vertex index is negative. OpenGL
3147 * doesn't accept negative offsets (or rather offsets bigger than the
3148 * VBO, because the pointer is unsigned), so use system memory
3149 * sources. In most sane cases the pointer - offset will still be > 0,
3150 * otherwise it will wrap around to some big value. Hope that with the
3151 * indices the driver wraps it back internally. If not,
3152 * draw_primitive_immediate_mode() is needed, including a vertex buffer
3154 if (state
->load_base_vertex_index
< 0)
3156 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3157 state
->load_base_vertex_index
);
3158 element
->data
.buffer_object
= 0;
3159 element
->data
.addr
+= (ULONG_PTR
)buffer_get_sysmem(buffer
, context
);
3160 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
3161 FIXME("System memory vertex data load offset is negative!\n");
3165 buffer_internal_preload(buffer
, context
, state
);
3166 buffer_get_memory(buffer
, context
, &data
);
3167 element
->data
.buffer_object
= data
.buffer_object
;
3168 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
3171 if (!element
->data
.buffer_object
)
3172 stream_info
->all_vbo
= 0;
3175 context
->buffer_queries
[context
->num_buffer_queries
++] = buffer
->query
;
3177 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
3182 if (state
->vertex_declaration
->half_float_conv_needed
&& !stream_info
->all_vbo
)
3184 TRACE("Using immediate mode draw with vertex shaders for FLOAT16 conversion.\n");
3185 context
->use_immediate_mode_draw
= TRUE
;
3189 context
->use_immediate_mode_draw
= FALSE
;
3194 WORD slow_mask
= -!d3d_info
->ffp_generic_attributes
& (1u << WINED3D_FFP_PSIZE
);
3195 slow_mask
|= -!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3196 & ((1u << WINED3D_FFP_DIFFUSE
) | (1u << WINED3D_FFP_SPECULAR
));
3198 if (((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
3199 || (stream_info
->use_map
& slow_mask
)) && !stream_info
->all_vbo
)
3200 context
->use_immediate_mode_draw
= TRUE
;
3202 context
->use_immediate_mode_draw
= FALSE
;
3205 if (prev_all_vbo
!= stream_info
->all_vbo
)
3206 context_invalidate_state(context
, STATE_INDEXBUFFER
);
3209 /* Context activation is done by the caller. */
3210 static void context_preload_texture(struct wined3d_context
*context
,
3211 const struct wined3d_state
*state
, unsigned int idx
)
3213 struct wined3d_texture
*texture
;
3215 if (!(texture
= state
->textures
[idx
]))
3218 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
3221 /* Context activation is done by the caller. */
3222 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3228 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3230 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
3231 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
3237 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3239 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
3240 context_preload_texture(context
, state
, i
);
3245 WORD ffu_map
= context
->fixed_function_usage_map
;
3247 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3250 context_preload_texture(context
, state
, i
);
3255 static void context_load_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3257 struct wined3d_shader_sampler_map_entry
*entry
;
3258 struct wined3d_shader_resource_view
*view
;
3259 struct wined3d_shader
*shader
;
3262 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3264 if (!(shader
= state
->shader
[i
]))
3267 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3269 if (state
->cb
[i
][j
])
3270 buffer_internal_preload(state
->cb
[i
][j
], context
, state
);
3273 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
3275 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3277 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
3279 WARN("No resource view bound at index %u, %u.\n", i
, entry
->resource_idx
);
3283 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3284 buffer_internal_preload(buffer_from_resource(view
->resource
), context
, state
);
3286 wined3d_texture_load(texture_from_resource(view
->resource
), context
, FALSE
);
3291 static void context_bind_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3293 const struct wined3d_device
*device
= context
->swapchain
->device
;
3294 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3295 struct wined3d_shader_sampler_map_entry
*entry
;
3296 struct wined3d_shader_resource_view
*view
;
3297 struct wined3d_sampler
*sampler
;
3298 struct wined3d_texture
*texture
;
3299 struct wined3d_shader
*shader
;
3300 unsigned int i
, j
, count
;
3301 GLuint sampler_name
;
3305 enum wined3d_shader_type type
;
3306 unsigned int base_idx
;
3311 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
3312 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
3315 for (i
= 0; i
< ARRAY_SIZE(shader_types
); ++i
)
3317 if (!(shader
= state
->shader
[shader_types
[i
].type
]))
3320 count
= shader
->reg_maps
.sampler_map
.count
;
3321 if (count
> shader_types
[i
].count
)
3323 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3324 shader
, count
, shader_types
[i
].count
);
3325 count
= shader_types
[i
].count
;
3328 for (j
= 0; j
< count
; ++j
)
3330 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3332 if (!(view
= state
->shader_resource_view
[shader_types
[i
].type
][entry
->resource_idx
]))
3334 WARN("No resource view bound at index %u, %u.\n", shader_types
[i
].type
, entry
->resource_idx
);
3338 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3340 FIXME("Buffer shader resources not supported.\n");
3344 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
3346 sampler_name
= device
->default_sampler
;
3348 else if ((sampler
= state
->sampler
[shader_types
[i
].type
][entry
->sampler_idx
]))
3350 sampler_name
= sampler
->name
;
3354 WARN("No sampler object bound at index %u, %u.\n", shader_types
[i
].type
, entry
->sampler_idx
);
3358 texture
= texture_from_resource(view
->resource
);
3359 context_active_texture(context
, gl_info
, shader_types
[i
].base_idx
+ entry
->bind_idx
);
3360 wined3d_texture_bind(texture
, context
, FALSE
);
3362 GL_EXTCALL(glBindSampler(shader_types
[i
].base_idx
+ entry
->bind_idx
, sampler_name
));
3363 checkGLcall("glBindSampler");
3368 /* Context activation is done by the caller. */
3369 BOOL
context_apply_draw_state(struct wined3d_context
*context
,
3370 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
3372 const struct StateEntry
*state_table
= context
->state_table
;
3373 const struct wined3d_fb_state
*fb
= state
->fb
;
3377 if (!context_validate_rt_config(context
->gl_info
->limits
.buffers
,
3378 fb
->render_targets
, fb
->depth_stencil
))
3381 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3383 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3386 /* Preload resources before FBO setup. Texture preload in particular may
3387 * result in changes to the current FBO, due to using e.g. FBO blits for
3388 * updating a resource location. */
3389 context_update_tex_unit_map(context
, state
);
3390 context_preload_textures(context
, state
);
3391 context_load_shader_resources(context
, state
);
3392 /* TODO: Right now the dependency on the vertex shader is necessary
3393 * since context_stream_info_from_declaration depends on the reg_maps of
3394 * the current VS but maybe it's possible to relax the coupling in some
3395 * situations at least. */
3396 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
3397 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
3399 context_update_stream_info(context
, state
);
3403 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3406 buffer_mark_used(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
);
3409 if (state
->index_buffer
)
3411 if (context
->stream_info
.all_vbo
)
3412 buffer_internal_preload(state
->index_buffer
, context
, state
);
3414 buffer_get_sysmem(state
->index_buffer
, context
);
3417 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3419 DWORD rep
= context
->dirtyArray
[i
];
3420 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3421 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3422 context
->isStateDirty
[idx
] &= ~(1u << shift
);
3423 state_table
[rep
].apply(context
, state
, rep
);
3426 if (context
->shader_update_mask
)
3428 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3429 context
->shader_update_mask
= 0;
3432 if (context
->constant_update_mask
)
3434 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3435 context
->constant_update_mask
= 0;
3438 if (context
->update_shader_resource_bindings
)
3440 context_bind_shader_resources(context
, state
);
3441 context
->update_shader_resource_bindings
= 0;
3444 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3446 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3449 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
3450 context
->last_was_blit
= FALSE
;
3455 static void context_setup_target(struct wined3d_context
*context
,
3456 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
3458 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
3460 render_offscreen
= wined3d_resource_is_offscreen(&texture
->resource
);
3461 if (context
->current_rt
.texture
== texture
3462 && context
->current_rt
.sub_resource_idx
== sub_resource_idx
3463 && render_offscreen
== old_render_offscreen
)
3466 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3467 * the alpha blend state changes with different render target formats. */
3468 if (!context
->current_rt
.texture
)
3470 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3474 const struct wined3d_format
*old
= context
->current_rt
.texture
->resource
.format
;
3475 const struct wined3d_format
*new = texture
->resource
.format
;
3477 if (old
->id
!= new->id
)
3479 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3480 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
3481 || !(texture
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
3482 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3484 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3485 if ((context
->current_rt
.texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
3486 != (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
3487 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3490 /* When switching away from an offscreen render target, and we're not
3491 * using FBOs, we have to read the drawable into the texture. This is
3492 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
3493 * There are some things that need care though. PreLoad needs a GL context,
3494 * and FindContext is called before the context is activated. It also
3495 * has to be called with the old rendertarget active, otherwise a
3496 * wrong drawable is read. */
3497 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
3498 && old_render_offscreen
&& (context
->current_rt
.texture
!= texture
3499 || context
->current_rt
.sub_resource_idx
!= sub_resource_idx
))
3501 unsigned int prev_sub_resource_idx
= context
->current_rt
.sub_resource_idx
;
3502 struct wined3d_texture
*prev_texture
= context
->current_rt
.texture
;
3504 /* Read the back buffer of the old drawable into the destination texture. */
3505 if (prev_texture
->texture_srgb
.name
)
3506 wined3d_texture_load(prev_texture
, context
, TRUE
);
3507 wined3d_texture_load(prev_texture
, context
, FALSE
);
3508 wined3d_texture_invalidate_location(prev_texture
, prev_sub_resource_idx
, WINED3D_LOCATION_DRAWABLE
);
3512 context
->current_rt
.texture
= texture
;
3513 context
->current_rt
.sub_resource_idx
= sub_resource_idx
;
3514 context_set_render_offscreen(context
, render_offscreen
);
3517 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
, struct wined3d_surface
*target
)
3519 struct wined3d_context
*current_context
= context_get_current();
3520 struct wined3d_texture
*target_texture
;
3521 unsigned int target_sub_resource_idx
;
3522 struct wined3d_context
*context
;
3524 TRACE("device %p, target %p.\n", device
, target
);
3526 if (current_context
&& current_context
->destroyed
)
3527 current_context
= NULL
;
3531 target_texture
= target
->container
;
3532 target_sub_resource_idx
= surface_get_sub_resource_idx(target
);
3537 && current_context
->current_rt
.texture
3538 && current_context
->swapchain
->device
== device
)
3540 target_texture
= current_context
->current_rt
.texture
;
3541 target_sub_resource_idx
= current_context
->current_rt
.sub_resource_idx
;
3545 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
3547 if (swapchain
->back_buffers
)
3548 target_texture
= swapchain
->back_buffers
[0];
3550 target_texture
= swapchain
->front_buffer
;
3551 target_sub_resource_idx
= 0;
3555 if (current_context
&& current_context
->current_rt
.texture
== target_texture
)
3557 context
= current_context
;
3559 else if (target_texture
->swapchain
)
3561 TRACE("Rendering onscreen.\n");
3563 context
= swapchain_get_context(target_texture
->swapchain
);
3567 TRACE("Rendering offscreen.\n");
3569 /* Stay with the current context if possible. Otherwise use the
3570 * context for the primary swapchain. */
3571 if (current_context
&& current_context
->swapchain
->device
== device
)
3572 context
= current_context
;
3574 context
= swapchain_get_context(device
->swapchains
[0]);
3577 context_enter(context
);
3578 context_update_window(context
);
3579 context_setup_target(context
, target_texture
, target_sub_resource_idx
);
3580 if (!context
->valid
) return context
;
3582 if (context
!= current_context
)
3584 if (!context_set_current(context
))
3585 ERR("Failed to activate the new context.\n");
3587 else if (context
->needs_set
)
3589 context_set_gl_context(context
);