2 * Context and render target management in wined3d
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006, 2008 Henri Verbeet
9 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wined3d_private.h"
28 #include "wined3d_gl.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
31 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
32 WINE_DECLARE_DEBUG_CHANNEL(d3d_sync
);
34 #define WINED3D_MAX_FBO_ENTRIES 64
35 #define WINED3D_ALL_LAYERS (~0u)
37 static DWORD wined3d_context_tls_idx
;
39 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
40 * actually have the same values in GL and D3D. */
41 static GLenum
gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
43 switch (primitive_type
)
45 case WINED3D_PT_POINTLIST
:
48 case WINED3D_PT_LINELIST
:
51 case WINED3D_PT_LINESTRIP
:
54 case WINED3D_PT_TRIANGLELIST
:
57 case WINED3D_PT_TRIANGLESTRIP
:
58 return GL_TRIANGLE_STRIP
;
60 case WINED3D_PT_TRIANGLEFAN
:
61 return GL_TRIANGLE_FAN
;
63 case WINED3D_PT_LINELIST_ADJ
:
64 return GL_LINES_ADJACENCY_ARB
;
66 case WINED3D_PT_LINESTRIP_ADJ
:
67 return GL_LINE_STRIP_ADJACENCY_ARB
;
69 case WINED3D_PT_TRIANGLELIST_ADJ
:
70 return GL_TRIANGLES_ADJACENCY_ARB
;
72 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
73 return GL_TRIANGLE_STRIP_ADJACENCY_ARB
;
75 case WINED3D_PT_PATCH
:
79 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
80 case WINED3D_PT_UNDEFINED
:
85 /* FBO helper functions */
87 /* Context activation is done by the caller. */
88 static void wined3d_context_gl_bind_fbo(struct wined3d_context_gl
*context_gl
, GLenum target
, GLuint fbo
)
90 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
92 TRACE("context_gl %p, target %#x, fbo %u.\n", context_gl
, target
, fbo
);
96 case GL_READ_FRAMEBUFFER
:
97 if (context_gl
->fbo_read_binding
== fbo
)
99 context_gl
->fbo_read_binding
= fbo
;
102 case GL_DRAW_FRAMEBUFFER
:
103 if (context_gl
->fbo_draw_binding
== fbo
)
105 context_gl
->fbo_draw_binding
= fbo
;
109 if (context_gl
->fbo_read_binding
== fbo
110 && context_gl
->fbo_draw_binding
== fbo
)
112 context_gl
->fbo_read_binding
= fbo
;
113 context_gl
->fbo_draw_binding
= fbo
;
117 FIXME("Unhandled target %#x.\n", target
);
121 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
122 checkGLcall("glBindFramebuffer()");
125 /* Context activation is done by the caller. */
126 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
130 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
132 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
133 checkGLcall("glFramebufferTexture2D()");
135 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
136 checkGLcall("glFramebufferTexture2D()");
138 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
139 checkGLcall("glFramebufferTexture2D()");
142 /* Context activation is done by the caller. */
143 static void wined3d_context_gl_destroy_fbo(struct wined3d_context_gl
*context_gl
, GLuint fbo
)
145 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
147 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, fbo
);
148 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
149 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
151 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
152 checkGLcall("glDeleteFramebuffers()");
155 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
156 GLenum fbo_target
, uint32_t flags
, GLuint rb
)
158 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
160 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
161 checkGLcall("glFramebufferRenderbuffer()");
164 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
166 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
167 checkGLcall("glFramebufferRenderbuffer()");
171 static void wined3d_context_gl_attach_gl_texture_fbo(struct wined3d_context_gl
*context_gl
,
172 GLenum fbo_target
, GLenum attachment
, const struct wined3d_fbo_resource
*resource
)
174 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
178 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
, GL_TEXTURE_2D
, 0, 0);
180 else if (resource
->layer
== WINED3D_ALL_LAYERS
)
182 if (!gl_info
->fbo_ops
.glFramebufferTexture
)
184 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
188 gl_info
->fbo_ops
.glFramebufferTexture(fbo_target
, attachment
,
189 resource
->object
, resource
->level
);
191 else if (resource
->target
== GL_TEXTURE_1D_ARRAY
|| resource
->target
== GL_TEXTURE_2D_ARRAY
192 || resource
->target
== GL_TEXTURE_3D
)
194 if (!gl_info
->fbo_ops
.glFramebufferTextureLayer
)
196 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
200 gl_info
->fbo_ops
.glFramebufferTextureLayer(fbo_target
, attachment
,
201 resource
->object
, resource
->level
, resource
->layer
);
203 else if (resource
->target
== GL_TEXTURE_1D
)
205 gl_info
->fbo_ops
.glFramebufferTexture1D(fbo_target
, attachment
,
206 resource
->target
, resource
->object
, resource
->level
);
210 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
,
211 resource
->target
, resource
->object
, resource
->level
);
213 checkGLcall("attach texture to fbo");
216 /* Context activation is done by the caller. */
217 static void wined3d_context_gl_attach_depth_stencil_fbo(struct wined3d_context_gl
*context_gl
,
218 GLenum fbo_target
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
,
221 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
223 if (resource
->object
)
225 TRACE("Attach depth stencil %u.\n", resource
->object
);
229 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
230 flags
, resource
->object
);
234 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
235 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_DEPTH_ATTACHMENT
, resource
);
237 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
238 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_STENCIL_ATTACHMENT
, resource
);
241 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
))
242 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
244 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
))
245 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
249 TRACE("Attach depth stencil 0.\n");
251 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
252 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
256 /* Context activation is done by the caller. */
257 static void wined3d_context_gl_attach_surface_fbo(struct wined3d_context_gl
*context_gl
,
258 GLenum fbo_target
, unsigned int idx
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
)
260 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
262 TRACE("Attach GL object %u to %u.\n", resource
->object
, idx
);
264 if (resource
->object
)
268 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
269 GL_RENDERBUFFER
, resource
->object
);
270 checkGLcall("glFramebufferRenderbuffer()");
274 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, resource
);
279 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, NULL
);
283 static void context_dump_fbo_attachment(const struct wined3d_gl_info
*gl_info
, GLenum target
,
291 enum wined3d_gl_extension extension
;
295 {GL_TEXTURE_1D
, GL_TEXTURE_BINDING_1D
, "1d", WINED3D_GL_EXT_NONE
},
296 {GL_TEXTURE_1D_ARRAY
, GL_TEXTURE_BINDING_1D_ARRAY
, "1d-array", EXT_TEXTURE_ARRAY
},
297 {GL_TEXTURE_2D
, GL_TEXTURE_BINDING_2D
, "2d", WINED3D_GL_EXT_NONE
},
298 {GL_TEXTURE_2D_ARRAY
, GL_TEXTURE_BINDING_2D_ARRAY
, "2d-array" , EXT_TEXTURE_ARRAY
},
299 {GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_BINDING_CUBE_MAP
, "cube", ARB_TEXTURE_CUBE_MAP
},
300 {GL_TEXTURE_2D_MULTISAMPLE
, GL_TEXTURE_BINDING_2D_MULTISAMPLE
, "2d-ms", ARB_TEXTURE_MULTISAMPLE
},
301 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY
, "2d-array-ms", ARB_TEXTURE_MULTISAMPLE
},
304 GLint type
, name
, samples
, width
, height
, old_texture
, level
, face
, fmt
, tex_target
;
305 const char *tex_type_str
= NULL
;
308 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
309 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
, &name
);
310 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
311 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
, &type
);
313 if (type
== GL_RENDERBUFFER
)
315 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, name
);
316 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_WIDTH
, &width
);
317 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_HEIGHT
, &height
);
318 if (gl_info
->limits
.samples
> 1)
319 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_SAMPLES
, &samples
);
322 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_INTERNAL_FORMAT
, &fmt
);
323 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
324 debug_fboattachment(attachment
), name
, width
, height
, samples
, fmt
);
326 else if (type
== GL_TEXTURE
)
328 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
329 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
, &level
);
330 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
331 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
, &face
);
333 if (gl_info
->gl_ops
.ext
.p_glGetTextureParameteriv
)
335 GL_EXTCALL(glGetTextureParameteriv(name
, GL_TEXTURE_TARGET
, &tex_target
));
337 for (i
= 0; i
< ARRAY_SIZE(texture_type
); ++i
)
339 if (texture_type
[i
].target
== tex_target
)
341 tex_type_str
= texture_type
[i
].str
;
345 if (i
== ARRAY_SIZE(texture_type
))
346 tex_type_str
= wine_dbg_sprintf("%#x", tex_target
);
350 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP
, &old_texture
);
351 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, name
);
353 tex_target
= GL_TEXTURE_CUBE_MAP
;
354 tex_type_str
= "cube";
358 for (i
= 0; i
< ARRAY_SIZE(texture_type
); ++i
)
360 if (!gl_info
->supported
[texture_type
[i
].extension
])
363 gl_info
->gl_ops
.gl
.p_glGetIntegerv(texture_type
[i
].binding
, &old_texture
);
364 while (gl_info
->gl_ops
.gl
.p_glGetError());
366 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, name
);
367 if (!gl_info
->gl_ops
.gl
.p_glGetError())
369 tex_target
= texture_type
[i
].target
;
370 tex_type_str
= texture_type
[i
].str
;
373 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, old_texture
);
378 FIXME("Cannot find type of texture %d.\n", name
);
383 if (gl_info
->gl_ops
.ext
.p_glGetTextureParameteriv
)
385 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
));
386 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_WIDTH
, &width
));
387 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_HEIGHT
, &height
));
388 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_SAMPLES
, &samples
));
392 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
393 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_WIDTH
, &width
);
394 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_HEIGHT
, &height
);
395 if (gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
396 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_SAMPLES
, &samples
);
400 gl_info
->gl_ops
.gl
.p_glBindTexture(tex_target
, old_texture
);
403 FIXME(" %s: %s texture %d, %dx%d, %d samples, format %#x.\n",
404 debug_fboattachment(attachment
), tex_type_str
, name
, width
, height
, samples
, fmt
);
406 else if (type
== GL_NONE
)
408 FIXME(" %s: NONE.\n", debug_fboattachment(attachment
));
412 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment
), type
);
415 checkGLcall("dump FBO attachment");
418 /* Context activation is done by the caller. */
419 void wined3d_context_gl_check_fbo_status(const struct wined3d_context_gl
*context_gl
, GLenum target
)
421 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
427 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
428 if (status
== GL_FRAMEBUFFER_COMPLETE
)
430 TRACE("FBO complete.\n");
436 FIXME("FBO status %s (%#x).\n", debug_fbostatus(status
), status
);
438 if (!context_gl
->current_fbo
)
440 ERR("FBO 0 is incomplete, driver bug?\n");
444 context_dump_fbo_attachment(gl_info
, target
, GL_DEPTH_ATTACHMENT
);
445 context_dump_fbo_attachment(gl_info
, target
, GL_STENCIL_ATTACHMENT
);
447 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
448 context_dump_fbo_attachment(gl_info
, target
, GL_COLOR_ATTACHMENT0
+ i
);
452 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
454 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
455 return buffer
? (1u << 31) | buffer
: 0;
458 static inline DWORD
context_generate_rt_mask_from_resource(struct wined3d_resource
*resource
)
460 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
462 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
466 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource
));
469 /* Context activation is done by the caller. */
470 static void wined3d_context_gl_set_draw_buffer(struct wined3d_context_gl
*context_gl
, GLenum buffer
)
472 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
473 struct fbo_entry
*current_fbo
= context_gl
->current_fbo
;
474 uint32_t new_mask
= context_generate_rt_mask(buffer
);
475 uint32_t *current_mask
;
477 current_mask
= current_fbo
? ¤t_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
478 if (new_mask
== *current_mask
)
481 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
482 checkGLcall("glDrawBuffer()");
484 *current_mask
= new_mask
;
487 static inline void wined3d_context_gl_set_fbo_key_for_render_target(const struct wined3d_context_gl
*context_gl
,
488 struct wined3d_fbo_entry_key
*key
, unsigned int idx
, const struct wined3d_rendertarget_info
*render_target
,
491 unsigned int sub_resource_idx
= render_target
->sub_resource_idx
;
492 struct wined3d_resource
*resource
= render_target
->resource
;
493 struct wined3d_texture_gl
*texture_gl
;
495 if (!resource
|| resource
->format
->id
== WINED3DFMT_NULL
|| resource
->type
== WINED3D_RTYPE_BUFFER
)
497 if (resource
&& resource
->type
== WINED3D_RTYPE_BUFFER
)
498 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
499 key
->objects
[idx
].object
= 0;
500 key
->objects
[idx
].target
= 0;
501 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
505 if (render_target
->gl_view
.name
)
507 key
->objects
[idx
].object
= render_target
->gl_view
.name
;
508 key
->objects
[idx
].target
= render_target
->gl_view
.target
;
509 key
->objects
[idx
].level
= 0;
510 key
->objects
[idx
].layer
= (render_target
->layer_count
== 1 ? 0 : WINED3D_ALL_LAYERS
);
514 texture_gl
= wined3d_texture_gl(wined3d_texture_from_resource(resource
));
515 if (texture_gl
->current_renderbuffer
)
517 key
->objects
[idx
].object
= texture_gl
->current_renderbuffer
->id
;
518 key
->objects
[idx
].target
= 0;
519 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
520 key
->rb_namespace
|= 1 << idx
;
524 key
->objects
[idx
].target
= wined3d_texture_gl_get_sub_resource_target(texture_gl
, sub_resource_idx
);
525 key
->objects
[idx
].level
= sub_resource_idx
% texture_gl
->t
.level_count
;
526 key
->objects
[idx
].layer
= sub_resource_idx
/ texture_gl
->t
.level_count
;
528 if (render_target
->layer_count
!= 1)
529 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
533 case WINED3D_LOCATION_TEXTURE_RGB
:
534 key
->objects
[idx
].object
= wined3d_texture_gl_get_texture_name(texture_gl
, &context_gl
->c
, FALSE
);
537 case WINED3D_LOCATION_TEXTURE_SRGB
:
538 key
->objects
[idx
].object
= wined3d_texture_gl_get_texture_name(texture_gl
, &context_gl
->c
, TRUE
);
541 case WINED3D_LOCATION_RB_MULTISAMPLE
:
542 key
->objects
[idx
].object
= texture_gl
->rb_multisample
;
543 key
->objects
[idx
].target
= 0;
544 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
545 key
->rb_namespace
|= 1 << idx
;
548 case WINED3D_LOCATION_RB_RESOLVED
:
549 key
->objects
[idx
].object
= texture_gl
->rb_resolved
;
550 key
->objects
[idx
].target
= 0;
551 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
552 key
->rb_namespace
|= 1 << idx
;
557 static void wined3d_context_gl_generate_fbo_key(const struct wined3d_context_gl
*context_gl
,
558 struct wined3d_fbo_entry_key
*key
, const struct wined3d_rendertarget_info
*render_targets
,
559 const struct wined3d_rendertarget_info
*depth_stencil
, DWORD color_location
, DWORD ds_location
)
561 unsigned int buffers
= context_gl
->gl_info
->limits
.buffers
;
564 key
->rb_namespace
= 0;
565 wined3d_context_gl_set_fbo_key_for_render_target(context_gl
, key
, 0, depth_stencil
, ds_location
);
567 for (i
= 0; i
< buffers
; ++i
)
568 wined3d_context_gl_set_fbo_key_for_render_target(context_gl
, key
, i
+ 1, &render_targets
[i
], color_location
);
570 memset(&key
->objects
[buffers
+ 1], 0, (ARRAY_SIZE(key
->objects
) - buffers
- 1) * sizeof(*key
->objects
));
573 static struct fbo_entry
*wined3d_context_gl_create_fbo_entry(const struct wined3d_context_gl
*context_gl
,
574 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
575 DWORD color_location
, DWORD ds_location
)
577 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
578 struct fbo_entry
*entry
;
580 entry
= malloc(sizeof(*entry
));
581 wined3d_context_gl_generate_fbo_key(context_gl
, &entry
->key
,
582 render_targets
, depth_stencil
, color_location
, ds_location
);
584 if (depth_stencil
->resource
)
586 if (depth_stencil
->resource
->format
->depth_size
)
587 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
588 if (depth_stencil
->resource
->format
->stencil_size
)
589 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
591 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
592 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
593 checkGLcall("glGenFramebuffers()");
594 TRACE("Created FBO %u.\n", entry
->id
);
599 /* Context activation is done by the caller. */
600 static void wined3d_context_gl_reuse_fbo_entry(struct wined3d_context_gl
*context_gl
, GLenum target
,
601 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
602 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
604 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
606 wined3d_context_gl_bind_fbo(context_gl
, target
, entry
->id
);
607 context_clean_fbo_attachments(gl_info
, target
);
609 wined3d_context_gl_generate_fbo_key(context_gl
, &entry
->key
,
610 render_targets
, depth_stencil
, color_location
, ds_location
);
612 if (depth_stencil
->resource
)
614 if (depth_stencil
->resource
->format
->depth_size
)
615 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
616 if (depth_stencil
->resource
->format
->stencil_size
)
617 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
621 /* Context activation is done by the caller. */
622 static void wined3d_context_gl_destroy_fbo_entry(struct wined3d_context_gl
*context_gl
, struct fbo_entry
*entry
)
626 TRACE("Destroy FBO %u.\n", entry
->id
);
627 wined3d_context_gl_destroy_fbo(context_gl
, entry
->id
);
629 --context_gl
->fbo_entry_count
;
630 list_remove(&entry
->entry
);
634 /* Context activation is done by the caller. */
635 static struct fbo_entry
*wined3d_context_gl_find_fbo_entry(struct wined3d_context_gl
*context_gl
, GLenum target
,
636 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
637 DWORD color_location
, DWORD ds_location
)
639 static const struct wined3d_rendertarget_info ds_null
= {{0}};
640 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
641 struct wined3d_texture
*rt_texture
, *ds_texture
;
642 struct wined3d_fbo_entry_key fbo_key
;
643 unsigned int i
, ds_level
, rt_level
;
644 struct fbo_entry
*entry
;
646 if (depth_stencil
->resource
&& depth_stencil
->resource
->type
!= WINED3D_RTYPE_BUFFER
647 && render_targets
[0].resource
&& render_targets
[0].resource
->type
!= WINED3D_RTYPE_BUFFER
648 && render_targets
[0].resource
->format
->id
!= WINED3DFMT_NULL
)
650 rt_texture
= wined3d_texture_from_resource(render_targets
[0].resource
);
651 rt_level
= render_targets
[0].sub_resource_idx
% rt_texture
->level_count
;
652 ds_texture
= wined3d_texture_from_resource(depth_stencil
->resource
);
653 ds_level
= depth_stencil
->sub_resource_idx
% ds_texture
->level_count
;
655 if (wined3d_texture_get_level_width(ds_texture
, ds_level
)
656 < wined3d_texture_get_level_width(rt_texture
, rt_level
)
657 || wined3d_texture_get_level_height(ds_texture
, ds_level
)
658 < wined3d_texture_get_level_height(rt_texture
, rt_level
))
660 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
661 depth_stencil
= &ds_null
;
663 else if (ds_texture
->resource
.multisample_type
!= rt_texture
->resource
.multisample_type
664 || (ds_texture
->resource
.multisample_type
665 && ds_texture
->resource
.multisample_quality
!= rt_texture
->resource
.multisample_quality
))
667 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
668 rt_texture
->resource
.multisample_type
, rt_texture
->resource
.multisample_quality
,
669 ds_texture
->resource
.multisample_type
, ds_texture
->resource
.multisample_quality
);
670 depth_stencil
= &ds_null
;
672 else if (depth_stencil
->resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
674 wined3d_texture_gl_set_compatible_renderbuffer(wined3d_texture_gl(ds_texture
),
675 context_gl
, ds_level
, &render_targets
[0]);
679 wined3d_context_gl_generate_fbo_key(context_gl
, &fbo_key
,
680 render_targets
, depth_stencil
, color_location
, ds_location
);
684 struct wined3d_resource
*resource
;
685 unsigned int width
, height
;
686 const char *resource_type
;
688 TRACE("Dumping FBO attachments:\n");
689 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
691 if ((resource
= render_targets
[i
].resource
))
693 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
695 width
= resource
->size
;
697 resource_type
= "buffer";
701 rt_texture
= wined3d_texture_from_resource(resource
);
702 rt_level
= render_targets
[i
].sub_resource_idx
% rt_texture
->level_count
;
703 width
= wined3d_texture_get_level_width(rt_texture
, rt_level
);
704 height
= wined3d_texture_get_level_height(rt_texture
, rt_level
);
705 resource_type
= "texture";
708 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
709 i
, resource
, render_targets
[i
].sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
710 fbo_key
.rb_namespace
& (1 << (i
+ 1)) ? "renderbuffer" : resource_type
,
711 fbo_key
.objects
[i
+ 1].object
, width
, height
, resource
->multisample_type
);
714 if ((resource
= depth_stencil
->resource
))
716 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
718 width
= resource
->size
;
720 resource_type
= "buffer";
724 ds_texture
= wined3d_texture_from_resource(resource
);
725 ds_level
= depth_stencil
->sub_resource_idx
% ds_texture
->level_count
;
726 width
= wined3d_texture_get_level_width(ds_texture
, ds_level
);
727 height
= wined3d_texture_get_level_height(ds_texture
, ds_level
);
728 resource_type
= "texture";
731 TRACE(" Depth attachment: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
732 resource
, depth_stencil
->sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
733 fbo_key
.rb_namespace
& (1 << 0) ? "renderbuffer" : resource_type
,
734 fbo_key
.objects
[0].object
, width
, height
, resource
->multisample_type
);
738 LIST_FOR_EACH_ENTRY(entry
, &context_gl
->fbo_list
, struct fbo_entry
, entry
)
740 if (memcmp(&fbo_key
, &entry
->key
, sizeof(fbo_key
)))
743 list_remove(&entry
->entry
);
744 list_add_head(&context_gl
->fbo_list
, &entry
->entry
);
748 if (context_gl
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
750 entry
= wined3d_context_gl_create_fbo_entry(context_gl
,
751 render_targets
, depth_stencil
, color_location
, ds_location
);
752 list_add_head(&context_gl
->fbo_list
, &entry
->entry
);
753 ++context_gl
->fbo_entry_count
;
757 entry
= LIST_ENTRY(list_tail(&context_gl
->fbo_list
), struct fbo_entry
, entry
);
758 wined3d_context_gl_reuse_fbo_entry(context_gl
, target
, render_targets
,
759 depth_stencil
, color_location
, ds_location
, entry
);
760 list_remove(&entry
->entry
);
761 list_add_head(&context_gl
->fbo_list
, &entry
->entry
);
767 /* Context activation is done by the caller. */
768 static void wined3d_context_gl_apply_fbo_entry(struct wined3d_context_gl
*context_gl
,
769 GLenum target
, struct fbo_entry
*entry
)
771 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
772 GLuint read_binding
, draw_binding
;
775 if (entry
->flags
& WINED3D_FBO_ENTRY_FLAG_ATTACHED
)
777 wined3d_context_gl_bind_fbo(context_gl
, target
, entry
->id
);
781 read_binding
= context_gl
->fbo_read_binding
;
782 draw_binding
= context_gl
->fbo_draw_binding
;
783 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, entry
->id
);
785 if (gl_info
->supported
[ARB_FRAMEBUFFER_NO_ATTACHMENTS
])
787 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
,
788 GL_FRAMEBUFFER_DEFAULT_WIDTH
, gl_info
->limits
.framebuffer_width
));
789 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
,
790 GL_FRAMEBUFFER_DEFAULT_HEIGHT
, gl_info
->limits
.framebuffer_height
));
791 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
, GL_FRAMEBUFFER_DEFAULT_LAYERS
, 1));
792 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
, GL_FRAMEBUFFER_DEFAULT_SAMPLES
, 1));
795 /* Apply render targets */
796 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
798 wined3d_context_gl_attach_surface_fbo(context_gl
, target
, i
,
799 &entry
->key
.objects
[i
+ 1], entry
->key
.rb_namespace
& (1 << (i
+ 1)));
802 wined3d_context_gl_attach_depth_stencil_fbo(context_gl
, target
,
803 &entry
->key
.objects
[0], entry
->key
.rb_namespace
& 0x1, entry
->flags
);
805 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
806 * GL contexts requirements. */
807 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
808 wined3d_context_gl_set_draw_buffer(context_gl
, GL_NONE
);
809 if (target
!= GL_FRAMEBUFFER
)
811 if (target
== GL_READ_FRAMEBUFFER
)
812 wined3d_context_gl_bind_fbo(context_gl
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
814 wined3d_context_gl_bind_fbo(context_gl
, GL_READ_FRAMEBUFFER
, read_binding
);
817 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_ATTACHED
;
820 /* Context activation is done by the caller. */
821 static void wined3d_context_gl_apply_fbo_state(struct wined3d_context_gl
*context_gl
, GLenum target
,
822 const struct wined3d_rendertarget_info
*render_targets
,
823 const struct wined3d_rendertarget_info
*depth_stencil
, DWORD color_location
, DWORD ds_location
)
825 struct fbo_entry
*entry
, *entry2
;
827 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_destroy_list
, struct fbo_entry
, entry
)
829 wined3d_context_gl_destroy_fbo_entry(context_gl
, entry
);
832 if (context_gl
->rebind_fbo
)
834 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
835 context_gl
->rebind_fbo
= FALSE
;
838 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
840 context_gl
->current_fbo
= NULL
;
841 wined3d_context_gl_bind_fbo(context_gl
, target
, 0);
845 context_gl
->current_fbo
= wined3d_context_gl_find_fbo_entry(context_gl
,
846 target
, render_targets
, depth_stencil
, color_location
, ds_location
);
847 wined3d_context_gl_apply_fbo_entry(context_gl
, target
, context_gl
->current_fbo
);
851 /* Context activation is done by the caller. */
852 void wined3d_context_gl_apply_fbo_state_explicit(struct wined3d_context_gl
*context_gl
, GLenum target
,
853 struct wined3d_resource
*rt
, unsigned int rt_sub_resource_idx
,
854 struct wined3d_resource
*ds
, unsigned int ds_sub_resource_idx
, uint32_t location
)
856 struct wined3d_rendertarget_info ds_info
= {{0}};
858 memset(context_gl
->blit_targets
, 0, sizeof(context_gl
->blit_targets
));
861 context_gl
->blit_targets
[0].resource
= rt
;
862 context_gl
->blit_targets
[0].sub_resource_idx
= rt_sub_resource_idx
;
863 context_gl
->blit_targets
[0].layer_count
= 1;
868 ds_info
.resource
= ds
;
869 ds_info
.sub_resource_idx
= ds_sub_resource_idx
;
870 ds_info
.layer_count
= 1;
873 wined3d_context_gl_apply_fbo_state(context_gl
, target
, context_gl
->blit_targets
, &ds_info
, location
, location
);
876 /* Context activation is done by the caller. */
877 void wined3d_context_gl_alloc_occlusion_query(struct wined3d_context_gl
*context_gl
,
878 struct wined3d_occlusion_query
*query
)
880 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
882 if (context_gl
->free_occlusion_query_count
)
884 query
->id
= context_gl
->free_occlusion_queries
[--context_gl
->free_occlusion_query_count
];
888 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
890 GL_EXTCALL(glGenQueries(1, &query
->id
));
891 checkGLcall("glGenQueries");
893 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context_gl
);
897 WARN("Occlusion queries not supported, not allocating query id.\n");
902 query
->context_gl
= context_gl
;
903 list_add_head(&context_gl
->occlusion_queries
, &query
->entry
);
906 void wined3d_context_gl_free_occlusion_query(struct wined3d_occlusion_query
*query
)
908 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
910 list_remove(&query
->entry
);
911 query
->context_gl
= NULL
;
913 if (!wined3d_array_reserve((void **)&context_gl
->free_occlusion_queries
,
914 &context_gl
->free_occlusion_query_size
, context_gl
->free_occlusion_query_count
+ 1,
915 sizeof(*context_gl
->free_occlusion_queries
)))
917 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context_gl
);
921 context_gl
->free_occlusion_queries
[context_gl
->free_occlusion_query_count
++] = query
->id
;
924 /* Context activation is done by the caller. */
925 void wined3d_context_gl_alloc_fence(struct wined3d_context_gl
*context_gl
, struct wined3d_fence
*fence
)
927 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
929 if (context_gl
->free_fence_count
)
931 fence
->object
= context_gl
->free_fences
[--context_gl
->free_fence_count
];
935 if (gl_info
->supported
[ARB_SYNC
])
937 /* Using ARB_sync, not much to do here. */
938 fence
->object
.sync
= NULL
;
939 TRACE("Allocated sync object in context %p.\n", context_gl
);
941 else if (gl_info
->supported
[APPLE_FENCE
])
943 GL_EXTCALL(glGenFencesAPPLE(1, &fence
->object
.id
));
944 checkGLcall("glGenFencesAPPLE");
946 TRACE("Allocated fence %u in context %p.\n", fence
->object
.id
, context_gl
);
948 else if(gl_info
->supported
[NV_FENCE
])
950 GL_EXTCALL(glGenFencesNV(1, &fence
->object
.id
));
951 checkGLcall("glGenFencesNV");
953 TRACE("Allocated fence %u in context %p.\n", fence
->object
.id
, context_gl
);
957 WARN("Fences not supported, not allocating fence.\n");
958 fence
->object
.id
= 0;
962 fence
->context_gl
= context_gl
;
963 list_add_head(&context_gl
->fences
, &fence
->entry
);
966 void wined3d_context_gl_free_fence(struct wined3d_fence
*fence
)
968 struct wined3d_context_gl
*context_gl
= fence
->context_gl
;
970 list_remove(&fence
->entry
);
971 fence
->context_gl
= NULL
;
973 if (!wined3d_array_reserve((void **)&context_gl
->free_fences
,
974 &context_gl
->free_fence_size
, context_gl
->free_fence_count
+ 1,
975 sizeof(*context_gl
->free_fences
)))
977 ERR("Failed to grow free list, leaking fence %u in context %p.\n", fence
->object
.id
, context_gl
);
981 context_gl
->free_fences
[context_gl
->free_fence_count
++] = fence
->object
;
984 /* Context activation is done by the caller. */
985 void wined3d_context_gl_alloc_timestamp_query(struct wined3d_context_gl
*context_gl
,
986 struct wined3d_timestamp_query
*query
)
988 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
990 if (context_gl
->free_timestamp_query_count
)
992 query
->id
= context_gl
->free_timestamp_queries
[--context_gl
->free_timestamp_query_count
];
996 GL_EXTCALL(glGenQueries(1, &query
->id
));
997 checkGLcall("glGenQueries");
999 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context_gl
);
1002 query
->context_gl
= context_gl
;
1003 list_add_head(&context_gl
->timestamp_queries
, &query
->entry
);
1006 void wined3d_context_gl_free_timestamp_query(struct wined3d_timestamp_query
*query
)
1008 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
1010 list_remove(&query
->entry
);
1011 query
->context_gl
= NULL
;
1013 if (!wined3d_array_reserve((void **)&context_gl
->free_timestamp_queries
,
1014 &context_gl
->free_timestamp_query_size
, context_gl
->free_timestamp_query_count
+ 1,
1015 sizeof(*context_gl
->free_timestamp_queries
)))
1017 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context_gl
);
1021 context_gl
->free_timestamp_queries
[context_gl
->free_timestamp_query_count
++] = query
->id
;
1024 void wined3d_context_gl_alloc_so_statistics_query(struct wined3d_context_gl
*context_gl
,
1025 struct wined3d_so_statistics_query
*query
)
1027 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1029 if (context_gl
->free_so_statistics_query_count
)
1031 query
->u
= context_gl
->free_so_statistics_queries
[--context_gl
->free_so_statistics_query_count
];
1035 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
1036 checkGLcall("glGenQueries");
1038 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
1039 query
->u
.id
[0], query
->u
.id
[1], context_gl
);
1042 query
->context_gl
= context_gl
;
1043 list_add_head(&context_gl
->so_statistics_queries
, &query
->entry
);
1046 void wined3d_context_gl_free_so_statistics_query(struct wined3d_so_statistics_query
*query
)
1048 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
1050 list_remove(&query
->entry
);
1051 query
->context_gl
= NULL
;
1053 if (!wined3d_array_reserve((void **)&context_gl
->free_so_statistics_queries
,
1054 &context_gl
->free_so_statistics_query_size
, context_gl
->free_so_statistics_query_count
+ 1,
1055 sizeof(*context_gl
->free_so_statistics_queries
)))
1057 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
1058 query
->u
.id
[0], query
->u
.id
[1], context_gl
);
1062 context_gl
->free_so_statistics_queries
[context_gl
->free_so_statistics_query_count
++] = query
->u
;
1065 void wined3d_context_gl_alloc_pipeline_statistics_query(struct wined3d_context_gl
*context_gl
,
1066 struct wined3d_pipeline_statistics_query
*query
)
1068 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1070 if (context_gl
->free_pipeline_statistics_query_count
)
1072 query
->u
= context_gl
->free_pipeline_statistics_queries
[--context_gl
->free_pipeline_statistics_query_count
];
1076 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
1077 checkGLcall("glGenQueries");
1080 query
->context_gl
= context_gl
;
1081 list_add_head(&context_gl
->pipeline_statistics_queries
, &query
->entry
);
1084 void wined3d_context_gl_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query
*query
)
1086 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
1088 list_remove(&query
->entry
);
1089 query
->context_gl
= NULL
;
1091 if (!wined3d_array_reserve((void **)&context_gl
->free_pipeline_statistics_queries
,
1092 &context_gl
->free_pipeline_statistics_query_size
, context_gl
->free_pipeline_statistics_query_count
+ 1,
1093 sizeof(*context_gl
->free_pipeline_statistics_queries
)))
1095 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context_gl
);
1099 context_gl
->free_pipeline_statistics_queries
[context_gl
->free_pipeline_statistics_query_count
++] = query
->u
;
1102 typedef void (context_fbo_entry_func_t
)(struct wined3d_context_gl
*context_gl
, struct fbo_entry
*entry
);
1104 static void wined3d_context_gl_enum_fbo_entries(const struct wined3d_device
*device
,
1105 GLuint name
, BOOL rb_namespace
, context_fbo_entry_func_t
*callback
)
1109 for (i
= 0; i
< device
->context_count
; ++i
)
1111 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(device
->contexts
[i
]);
1112 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1113 struct fbo_entry
*entry
, *entry2
;
1115 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_list
, struct fbo_entry
, entry
)
1117 for (j
= 0; j
< gl_info
->limits
.buffers
+ 1; ++j
)
1119 if (entry
->key
.objects
[j
].object
== name
1120 && !(entry
->key
.rb_namespace
& (1 << j
)) == !rb_namespace
)
1122 callback(context_gl
, entry
);
1130 static void wined3d_context_gl_queue_fbo_entry_destruction(struct wined3d_context_gl
*context_gl
,
1131 struct fbo_entry
*entry
)
1133 list_remove(&entry
->entry
);
1134 list_add_head(&context_gl
->fbo_destroy_list
, &entry
->entry
);
1137 void context_gl_resource_released(struct wined3d_device
*device
, GLuint name
, BOOL rb_namespace
)
1139 wined3d_context_gl_enum_fbo_entries(device
, name
, rb_namespace
,
1140 wined3d_context_gl_queue_fbo_entry_destruction
);
1143 void wined3d_context_gl_texture_update(struct wined3d_context_gl
*context_gl
,
1144 const struct wined3d_texture_gl
*texture_gl
)
1146 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1147 struct fbo_entry
*entry
= context_gl
->current_fbo
;
1150 if (!entry
|| context_gl
->rebind_fbo
)
1153 for (i
= 0; i
< gl_info
->limits
.buffers
+ 1; ++i
)
1155 if (texture_gl
->texture_rgb
.name
== entry
->key
.objects
[i
].object
1156 || texture_gl
->texture_srgb
.name
== entry
->key
.objects
[i
].object
)
1158 TRACE("Updated texture %p is bound as attachment %u to the current FBO.\n", texture_gl
, i
);
1159 context_gl
->rebind_fbo
= TRUE
;
1165 static BOOL
wined3d_context_gl_restore_pixel_format(struct wined3d_context_gl
*context_gl
)
1167 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1170 if (context_gl
->restore_pf
&& IsWindow(context_gl
->restore_pf_win
))
1172 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1174 HDC dc
= GetDCEx(context_gl
->restore_pf_win
, 0, DCX_USESTYLE
| DCX_CACHE
);
1177 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, context_gl
->restore_pf
))))
1179 ERR("Failed to restore pixel format %d on window %p.\n",
1180 context_gl
->restore_pf
, context_gl
->restore_pf_win
);
1182 ReleaseDC(context_gl
->restore_pf_win
, dc
);
1187 ERR("Unable to restore pixel format %d on window %p.\n",
1188 context_gl
->restore_pf
, context_gl
->restore_pf_win
);
1192 context_gl
->restore_pf
= 0;
1193 context_gl
->restore_pf_win
= NULL
;
1197 static BOOL
wined3d_context_gl_set_pixel_format(struct wined3d_context_gl
*context_gl
)
1199 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1200 BOOL
private = context_gl
->dc_is_private
;
1201 int format
= context_gl
->pixel_format
;
1202 HDC dc
= context_gl
->dc
;
1206 if (private && context_gl
->dc_has_format
)
1209 if (!private && WindowFromDC(dc
) != context_gl
->window
)
1212 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
1213 if ((current
== format
) || (!current
&& context_gl
->internal_format_set
))
1216 /* By default WGL doesn't allow pixel format adjustments but we need it
1217 * here. For this reason there's a Wine specific wglSetPixelFormat()
1218 * which allows us to set the pixel format multiple times. Use it when we
1219 * can, because even though no pixel format may currently be set, the
1220 * application may try to set one later. */
1221 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1223 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
1225 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1229 context_gl
->internal_format_set
= 1;
1233 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1234 * continue using the old format. There's a big chance that the old
1235 * format works although with a performance hit and perhaps rendering
1237 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1238 format
, dc
, current
);
1241 else if (!SetPixelFormat(dc
, format
, NULL
))
1243 /* This may also happen if the dc belongs to a destroyed window. */
1244 WARN("Failed to set pixel format %d on device context %p, last error %#lx.\n",
1245 format
, dc
, GetLastError());
1249 win
= private ? NULL
: WindowFromDC(dc
);
1250 if (win
!= context_gl
->restore_pf_win
)
1251 wined3d_context_gl_restore_pixel_format(context_gl
);
1252 context_gl
->restore_pf
= private ? 0 : current
;
1253 context_gl
->restore_pf_win
= win
;
1257 context_gl
->dc_has_format
= TRUE
;
1261 static BOOL
wined3d_context_gl_set_gl_context(struct wined3d_context_gl
*context_gl
)
1263 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
1264 BOOL backup
= FALSE
;
1266 TRACE("context_gl %p.\n", context_gl
);
1268 if (!wined3d_context_gl_set_pixel_format(context_gl
))
1270 WARN("Failed to set pixel format %d on device context %p.\n",
1271 context_gl
->pixel_format
, context_gl
->dc
);
1275 if (backup
|| !wglMakeCurrent(context_gl
->dc
, context_gl
->gl_ctx
))
1277 WARN("Failed to make GL context %p current on device context %p, last error %#lx.\n",
1278 context_gl
->gl_ctx
, context_gl
->dc
, GetLastError());
1279 context_gl
->valid
= 0;
1280 WARN("Trying fallback to the backup window.\n");
1282 if (context_gl
->c
.destroyed
)
1284 FIXME("Unable to get backup dc for destroyed context %p.\n", context_gl
);
1285 wined3d_context_gl_set_current(NULL
);
1289 if (!(context_gl
->dc
= wined3d_device_gl_get_backup_dc(device_gl
)))
1291 wined3d_context_gl_set_current(NULL
);
1295 TRACE("Using backup DC %p.\n", context_gl
->dc
);
1296 context_gl
->dc_is_private
= TRUE
;
1297 context_gl
->dc_has_format
= FALSE
;
1299 if (!wined3d_context_gl_set_pixel_format(context_gl
))
1301 ERR("Failed to set pixel format %d on device context %p.\n",
1302 context_gl
->pixel_format
, context_gl
->dc
);
1303 wined3d_context_gl_set_current(NULL
);
1307 if (!wglMakeCurrent(context_gl
->dc
, context_gl
->gl_ctx
))
1309 ERR("Fallback to backup window (dc %p) failed too, last error %#lx.\n",
1310 context_gl
->dc
, GetLastError());
1311 wined3d_context_gl_set_current(NULL
);
1315 context_gl
->valid
= 1;
1317 context_gl
->needs_set
= 0;
1322 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
1324 if (!wglMakeCurrent(dc
, gl_ctx
))
1326 ERR("Failed to restore GL context %p on device context %p, last error %#lx.\n",
1327 gl_ctx
, dc
, GetLastError());
1328 wined3d_context_gl_set_current(NULL
);
1332 static void wined3d_context_gl_update_window(struct wined3d_context_gl
*context_gl
)
1334 if (!context_gl
->c
.swapchain
)
1337 if (context_gl
->window
== context_gl
->c
.swapchain
->win_handle
)
1340 TRACE("Updating context %p window from %p to %p.\n",
1341 context_gl
, context_gl
->window
, context_gl
->c
.swapchain
->win_handle
);
1344 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
1346 context_gl
->window
= context_gl
->c
.swapchain
->win_handle
;
1347 context_gl
->dc_is_private
= FALSE
;
1348 context_gl
->dc_has_format
= FALSE
;
1349 context_gl
->needs_set
= 1;
1350 context_gl
->valid
= 1;
1351 context_gl
->internal_format_set
= 0;
1353 if (!(context_gl
->dc
= GetDCEx(context_gl
->window
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1355 ERR("Failed to get a device context for window %p.\n", context_gl
->window
);
1356 context_gl
->valid
= 0;
1360 static void wined3d_context_gl_cleanup(struct wined3d_context_gl
*context_gl
)
1362 struct wined3d_pipeline_statistics_query
*pipeline_statistics_query
;
1363 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1364 struct wined3d_so_statistics_query
*so_statistics_query
;
1365 struct wined3d_timestamp_query
*timestamp_query
;
1366 struct wined3d_occlusion_query
*occlusion_query
;
1367 struct wined3d_context_gl
*current
;
1368 struct fbo_entry
*entry
, *entry2
;
1369 struct wined3d_fence
*fence
;
1374 TRACE("context_gl %p.\n", context_gl
);
1376 restore_ctx
= wglGetCurrentContext();
1377 restore_dc
= wglGetCurrentDC();
1379 if (context_gl
->valid
&& context_gl
->gl_ctx
!= restore_ctx
)
1381 /* Attempting to restore a GL context corresponding to a wined3d
1382 * context is not particularly useful. Worse, when we're called from
1383 * wined3d_context_gl_destroy(), we subsequently clear the "current
1384 * D3D context" TLS value, which would cause
1385 * wined3d_context_gl_enter() to consider the GL context a non-D3D
1387 if ((current
= wined3d_context_gl_get_current()) && current
->gl_ctx
== restore_ctx
)
1389 wined3d_context_gl_set_gl_context(context_gl
);
1396 if (context_gl
->valid
)
1398 /* If we're here because we're switching away from a previously
1399 * destroyed context, acquiring a context in order to submit a fence
1400 * is problematic. In particular, we'd end up back here again in the
1401 * process of switching to the newly acquired context.
1403 * If fences aren't supported there should be nothing to wait for
1404 * anyway, so just do nothing in that case. */
1405 if (context_gl
->c
.destroyed
)
1407 gl_info
->gl_ops
.gl
.p_glFinish();
1409 else if (context_gl
->c
.d3d_info
->fences
)
1411 wined3d_context_gl_submit_command_fence(context_gl
);
1412 wined3d_context_gl_wait_command_fence(context_gl
,
1413 wined3d_device_gl(context_gl
->c
.device
)->current_fence_id
- 1);
1416 if (context_gl
->dummy_arbfp_prog
)
1417 GL_EXTCALL(glDeleteProgramsARB(1, &context_gl
->dummy_arbfp_prog
));
1419 if (context_gl
->blit_vbo
)
1420 GL_EXTCALL(glDeleteBuffers(1, &context_gl
->blit_vbo
));
1422 for (i
= 0; i
< context_gl
->free_pipeline_statistics_query_count
; ++i
)
1424 union wined3d_gl_pipeline_statistics_query
*q
= &context_gl
->free_pipeline_statistics_queries
[i
];
1425 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1428 for (i
= 0; i
< context_gl
->free_so_statistics_query_count
; ++i
)
1430 union wined3d_gl_so_statistics_query
*q
= &context_gl
->free_so_statistics_queries
[i
];
1431 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1434 if (context_gl
->free_timestamp_query_count
)
1435 GL_EXTCALL(glDeleteQueries(context_gl
->free_timestamp_query_count
, context_gl
->free_timestamp_queries
));
1437 if (gl_info
->supported
[ARB_SYNC
])
1439 for (i
= 0; i
< context_gl
->free_fence_count
; ++i
)
1441 GL_EXTCALL(glDeleteSync(context_gl
->free_fences
[i
].sync
));
1444 else if (gl_info
->supported
[APPLE_FENCE
])
1446 for (i
= 0; i
< context_gl
->free_fence_count
; ++i
)
1448 GL_EXTCALL(glDeleteFencesAPPLE(1, &context_gl
->free_fences
[i
].id
));
1451 else if (gl_info
->supported
[NV_FENCE
])
1453 for (i
= 0; i
< context_gl
->free_fence_count
; ++i
)
1455 GL_EXTCALL(glDeleteFencesNV(1, &context_gl
->free_fences
[i
].id
));
1459 if (context_gl
->free_occlusion_query_count
)
1460 GL_EXTCALL(glDeleteQueries(context_gl
->free_occlusion_query_count
, context_gl
->free_occlusion_queries
));
1462 checkGLcall("context cleanup");
1464 free(context_gl
->submitted
.fences
);
1465 free(context_gl
->free_pipeline_statistics_queries
);
1466 free(context_gl
->free_so_statistics_queries
);
1467 free(context_gl
->free_timestamp_queries
);
1468 free(context_gl
->free_fences
);
1469 free(context_gl
->free_occlusion_queries
);
1471 LIST_FOR_EACH_ENTRY(pipeline_statistics_query
, &context_gl
->pipeline_statistics_queries
,
1472 struct wined3d_pipeline_statistics_query
, entry
)
1474 if (context_gl
->valid
)
1475 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query
->u
.id
), pipeline_statistics_query
->u
.id
));
1476 pipeline_statistics_query
->context_gl
= NULL
;
1479 LIST_FOR_EACH_ENTRY(so_statistics_query
, &context_gl
->so_statistics_queries
,
1480 struct wined3d_so_statistics_query
, entry
)
1482 if (context_gl
->valid
)
1483 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query
->u
.id
), so_statistics_query
->u
.id
));
1484 so_statistics_query
->context_gl
= NULL
;
1487 LIST_FOR_EACH_ENTRY(timestamp_query
, &context_gl
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
1489 if (context_gl
->valid
)
1490 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
1491 timestamp_query
->context_gl
= NULL
;
1494 LIST_FOR_EACH_ENTRY(fence
, &context_gl
->fences
, struct wined3d_fence
, entry
)
1496 if (context_gl
->valid
)
1498 if (gl_info
->supported
[ARB_SYNC
])
1500 if (fence
->object
.sync
)
1501 GL_EXTCALL(glDeleteSync(fence
->object
.sync
));
1503 else if (gl_info
->supported
[APPLE_FENCE
])
1505 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence
->object
.id
));
1507 else if (gl_info
->supported
[NV_FENCE
])
1509 GL_EXTCALL(glDeleteFencesNV(1, &fence
->object
.id
));
1512 fence
->context_gl
= NULL
;
1515 LIST_FOR_EACH_ENTRY(occlusion_query
, &context_gl
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1517 if (context_gl
->valid
)
1518 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1519 occlusion_query
->context_gl
= NULL
;
1522 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_destroy_list
, struct fbo_entry
, entry
)
1524 if (!context_gl
->valid
)
1526 wined3d_context_gl_destroy_fbo_entry(context_gl
, entry
);
1529 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_list
, struct fbo_entry
, entry
)
1531 if (!context_gl
->valid
)
1533 wined3d_context_gl_destroy_fbo_entry(context_gl
, entry
);
1536 free(context_gl
->texture_type
);
1538 wined3d_context_gl_restore_pixel_format(context_gl
);
1540 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1541 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1542 ERR("Failed to disable GL context.\n");
1544 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
1546 if (!wglDeleteContext(context_gl
->gl_ctx
))
1548 unsigned int err
= GetLastError();
1549 ERR("Failed to delete GL context %p, last error %#x.\n", context_gl
->gl_ctx
, err
);
1552 wined3d_context_cleanup(&context_gl
->c
);
1555 DWORD
context_get_tls_idx(void)
1557 return wined3d_context_tls_idx
;
1560 void context_set_tls_idx(DWORD idx
)
1562 wined3d_context_tls_idx
= idx
;
1565 struct wined3d_context_gl
*wined3d_context_gl_get_current(void)
1567 return TlsGetValue(wined3d_context_tls_idx
);
1570 BOOL
wined3d_context_gl_set_current(struct wined3d_context_gl
*context_gl
)
1572 struct wined3d_context_gl
*old
= wined3d_context_gl_get_current();
1574 if (old
== context_gl
)
1576 TRACE("Already using D3D context %p.\n", context_gl
);
1582 if (old
->c
.destroyed
)
1584 TRACE("Switching away from destroyed context %p.\n", old
);
1585 wined3d_context_gl_cleanup(old
);
1586 free((void *)old
->gl_info
);
1591 if (wglGetCurrentContext())
1593 const struct wined3d_gl_info
*gl_info
= old
->gl_info
;
1594 TRACE("Flushing context %p before switching to %p.\n", old
, context_gl
);
1595 gl_info
->gl_ops
.gl
.p_glFlush();
1603 if (!context_gl
->valid
)
1605 ERR("Trying to make invalid context %p current.\n", context_gl
);
1609 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n",
1610 context_gl
, context_gl
->gl_ctx
, context_gl
->dc
);
1611 if (!wined3d_context_gl_set_gl_context(context_gl
))
1613 context_gl
->c
.current
= 1;
1615 else if (wglGetCurrentContext())
1617 TRACE("Clearing current D3D context.\n");
1618 if (!wglMakeCurrent(NULL
, NULL
))
1620 unsigned int err
= GetLastError();
1621 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1622 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1627 return TlsSetValue(wined3d_context_tls_idx
, context_gl
);
1630 void wined3d_context_gl_release(struct wined3d_context_gl
*context_gl
)
1632 TRACE("Releasing context %p, level %u.\n", context_gl
, context_gl
->level
);
1636 if (!context_gl
->level
)
1637 WARN("Context %p is not active.\n", context_gl
);
1638 else if (context_gl
!= wined3d_context_gl_get_current())
1639 WARN("Context %p is not the current context.\n", context_gl
);
1642 if (!--context_gl
->level
)
1644 if (wined3d_context_gl_restore_pixel_format(context_gl
))
1645 context_gl
->needs_set
= 1;
1646 if (context_gl
->restore_ctx
)
1648 TRACE("Restoring GL context %p on device context %p.\n", context_gl
->restore_ctx
, context_gl
->restore_dc
);
1649 context_restore_gl_context(context_gl
->gl_info
, context_gl
->restore_dc
, context_gl
->restore_ctx
);
1650 context_gl
->restore_ctx
= NULL
;
1651 context_gl
->restore_dc
= NULL
;
1654 if (context_gl
->c
.destroy_delayed
)
1656 TRACE("Destroying context %p.\n", context_gl
);
1657 wined3d_context_gl_destroy(context_gl
);
1662 static void wined3d_context_gl_enter(struct wined3d_context_gl
*context_gl
)
1664 TRACE("Entering context %p, level %u.\n", context_gl
, context_gl
->level
+ 1);
1666 if (!context_gl
->level
++)
1668 const struct wined3d_context_gl
*current_context
= wined3d_context_gl_get_current();
1669 HGLRC current_gl
= wglGetCurrentContext();
1671 if (current_gl
&& (!current_context
|| current_context
->gl_ctx
!= current_gl
))
1673 TRACE("Another GL context (%p on device context %p) is already current.\n",
1674 current_gl
, wglGetCurrentDC());
1675 context_gl
->restore_ctx
= current_gl
;
1676 context_gl
->restore_dc
= wglGetCurrentDC();
1677 context_gl
->needs_set
= 1;
1679 else if (!context_gl
->needs_set
&& !(context_gl
->dc_is_private
&& context_gl
->dc_has_format
))
1681 int current
= context_gl
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context_gl
->dc
);
1683 if ((current
&& current
!= context_gl
->pixel_format
) || (!current
&& !context_gl
->internal_format_set
))
1684 context_gl
->needs_set
= 1;
1689 /* This function takes care of wined3d pixel format selection. */
1690 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1691 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1692 bool aux_buffers
, bool swap_effect_copy
)
1694 unsigned int cfg_count
= wined3d_adapter_gl(device
->adapter
)->pixel_format_count
;
1695 unsigned int current_value
;
1696 PIXELFORMATDESCRIPTOR pfd
;
1697 int iPixelFormat
= 0;
1700 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, swap_effect_copy %#x.\n",
1701 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1702 aux_buffers
, swap_effect_copy
);
1705 for (i
= 0; i
< cfg_count
; ++i
)
1707 const struct wined3d_pixel_format
*cfg
= &wined3d_adapter_gl(device
->adapter
)->pixel_formats
[i
];
1710 /* For now only accept RGBA formats. Perhaps some day we will
1711 * allow floating point formats for pbuffers. */
1712 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1714 /* In window mode we need a window drawable format and double buffering. */
1715 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1717 if (cfg
->redSize
< color_format
->red_size
)
1719 if (cfg
->greenSize
< color_format
->green_size
)
1721 if (cfg
->blueSize
< color_format
->blue_size
)
1723 if (cfg
->alphaSize
< color_format
->alpha_size
)
1725 if (cfg
->depthSize
< ds_format
->depth_size
)
1727 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1729 /* Check multisampling support. */
1730 if (cfg
->numSamples
)
1734 if (swap_effect_copy
&& cfg
->swap_method
== WGL_SWAP_COPY_ARB
)
1736 /* We try to locate a format which matches our requirements exactly. In case of
1737 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1738 if (cfg
->depthSize
== ds_format
->depth_size
)
1740 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1742 if (cfg
->alphaSize
== color_format
->alpha_size
)
1744 /* We like to have aux buffers in backbuffer mode */
1745 if (aux_buffers
&& cfg
->auxBuffers
)
1747 if (cfg
->redSize
== color_format
->red_size
1748 && cfg
->greenSize
== color_format
->green_size
1749 && cfg
->blueSize
== color_format
->blue_size
)
1752 if (value
> current_value
)
1754 iPixelFormat
= cfg
->iPixelFormat
;
1755 current_value
= value
;
1761 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1763 memset(&pfd
, 0, sizeof(pfd
));
1764 pfd
.nSize
= sizeof(pfd
);
1766 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1767 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1768 pfd
.cAlphaBits
= color_format
->alpha_size
;
1769 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1770 + color_format
->blue_size
+ color_format
->alpha_size
;
1771 pfd
.cDepthBits
= ds_format
->depth_size
;
1772 pfd
.cStencilBits
= ds_format
->stencil_size
;
1773 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1775 if (!(iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
)))
1777 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1778 ERR("Can't find a suitable pixel format.\n");
1783 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1784 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1785 return iPixelFormat
;
1788 /* Context activation is done by the caller. */
1789 void wined3d_context_gl_bind_dummy_textures(const struct wined3d_context_gl
*context_gl
)
1791 const struct wined3d_dummy_textures
*textures
= &wined3d_device_gl(context_gl
->c
.device
)->dummy_textures
;
1792 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1795 for (i
= 0; i
< gl_info
->limits
.combined_samplers
; ++i
)
1797 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1799 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, textures
->tex_1d
);
1800 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, textures
->tex_2d
);
1802 if (gl_info
->supported
[EXT_TEXTURE3D
])
1803 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, textures
->tex_3d
);
1805 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1806 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, textures
->tex_cube
);
1808 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
1809 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, textures
->tex_cube_array
);
1811 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
1813 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, textures
->tex_1d_array
);
1814 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, textures
->tex_2d_array
);
1817 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
1818 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, textures
->tex_buffer
);
1820 if (gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
1822 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, textures
->tex_2d_ms
);
1823 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, textures
->tex_2d_ms_array
);
1827 checkGLcall("bind dummy textures");
1830 void wined3d_check_gl_call(const struct wined3d_gl_info
*gl_info
,
1831 const char *file
, unsigned int line
, const char *name
)
1835 if (gl_info
->supported
[ARB_DEBUG_OUTPUT
] || (err
= gl_info
->gl_ops
.gl
.p_glGetError()) == GL_NO_ERROR
)
1837 TRACE("%s call ok %s / %u.\n", name
, file
, line
);
1843 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1844 debug_glerror(err
), err
, name
, file
,line
);
1845 err
= gl_info
->gl_ops
.gl
.p_glGetError();
1846 } while (err
!= GL_NO_ERROR
);
1849 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1851 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1852 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1855 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1856 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1860 case GL_DEBUG_TYPE_ERROR_ARB
:
1861 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1864 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1865 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1866 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1867 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1870 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1871 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1875 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1880 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1883 unsigned int ctx_attrib_idx
= 0;
1884 GLint ctx_attribs
[7], ctx_flags
= 0;
1886 if (context_debug_output_enabled(gl_info
))
1887 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1888 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1889 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1890 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1891 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1894 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1895 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1897 ctx_attribs
[ctx_attrib_idx
] = 0;
1899 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1901 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1905 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1906 ctx_attribs
[ctx_attrib_idx
- 1] = ctx_flags
;
1910 ctx_flags
= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1911 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1912 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1913 ctx_attribs
[ctx_attrib_idx
] = 0;
1915 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1916 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#lx.\n",
1923 static BOOL
wined3d_context_gl_create_wgl_ctx(struct wined3d_context_gl
*context_gl
,
1924 struct wined3d_swapchain_gl
*swapchain_gl
, BOOL
*new_drawable
)
1926 enum wined3d_swap_effect swap_effect
= swapchain_gl
->s
.state
.desc
.swap_effect
;
1927 const struct wined3d_format
*colour_format
, *ds_format
;
1928 struct wined3d_context
*context
= &context_gl
->c
;
1929 const struct wined3d_gl_info
*gl_info
;
1930 struct wined3d_resource
*target
;
1931 struct wined3d_adapter
*adapter
;
1932 unsigned int target_bind_flags
;
1933 struct wined3d_device
*device
;
1934 bool swap_effect_copy
;
1935 HGLRC ctx
, share_ctx
;
1937 device
= context
->device
;
1938 adapter
= device
->adapter
;
1939 gl_info
= &wined3d_adapter_gl(adapter
)->gl_info
;
1941 target
= &context
->current_rt
.texture
->resource
;
1942 target_bind_flags
= target
->bind_flags
;
1944 swap_effect_copy
= swap_effect
== WINED3D_SWAP_EFFECT_COPY
|| swap_effect
== WINED3D_SWAP_EFFECT_COPY_VSYNC
;
1946 *new_drawable
= !GetPixelFormat(context_gl
->dc
);
1948 /* When using FBOs for off-screen rendering, we only use the drawable for
1949 * presentation blits, and don't do any rendering to it. That means we
1950 * don't need depth or stencil buffers, and can mostly ignore the render
1951 * target format. This wouldn't necessarily be quite correct for 10bpc
1952 * display modes, but we don't currently support those.
1953 * Using the same format regardless of the colour/depth/stencil targets
1954 * makes it much less likely that different wined3d instances will set
1955 * conflicting pixel formats. */
1956 colour_format
= wined3d_get_format(adapter
, WINED3DFMT_B8G8R8A8_UNORM
, target_bind_flags
);
1957 ds_format
= wined3d_get_format(adapter
, WINED3DFMT_UNKNOWN
, WINED3D_BIND_DEPTH_STENCIL
);
1958 context_gl
->pixel_format
= context_choose_pixel_format(device
,
1959 context_gl
->dc
, colour_format
, ds_format
, false, swap_effect_copy
);
1961 if (!context_gl
->pixel_format
)
1963 ERR("Failed to choose pixel format.\n");
1967 wined3d_context_gl_enter(context_gl
);
1969 if (!wined3d_context_gl_set_pixel_format(context_gl
))
1971 context_release(context
);
1973 if (context_gl
->dc_is_private
)
1975 ERR("Failed to set pixel format %d on device context %p.\n", context_gl
->pixel_format
, context_gl
->dc
);
1980 WARN("Failed to set pixel format %d on device context %p, trying backup DC.\n",
1981 context_gl
->pixel_format
, context_gl
->dc
);
1983 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
1984 if (!(context_gl
->dc
= wined3d_device_gl_get_backup_dc(wined3d_device_gl(device
))))
1986 ERR("Failed to retrieve the backup device context.\n");
1989 context_gl
->dc_is_private
= TRUE
;
1991 return wined3d_context_gl_create_wgl_ctx(context_gl
, swapchain_gl
, new_drawable
);
1994 share_ctx
= device
->context_count
? wined3d_context_gl(device
->contexts
[0])->gl_ctx
: NULL
;
1995 if (gl_info
->p_wglCreateContextAttribsARB
)
1997 if (!(ctx
= context_create_wgl_attribs(gl_info
, context_gl
->dc
, share_ctx
)))
1999 ERR("Failed to create a WGL context.\n");
2000 context_release(context
);
2006 if (!(ctx
= wglCreateContext(context_gl
->dc
)))
2008 ERR("Failed to create a WGL context.\n");
2009 context_release(context
);
2013 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
2015 ERR("wglShareLists(%p, %p) failed, last error %#lx.\n", share_ctx
, ctx
, GetLastError());
2016 context_release(context
);
2017 if (!wglDeleteContext(ctx
))
2018 ERR("wglDeleteContext(%p) failed, last error %#lx.\n", ctx
, GetLastError());
2023 context_gl
->dc_has_format
= TRUE
;
2024 context_gl
->needs_set
= 1;
2025 context_gl
->valid
= 1;
2026 context_gl
->gl_ctx
= ctx
;
2031 HRESULT
wined3d_context_gl_init(struct wined3d_context_gl
*context_gl
, struct wined3d_swapchain_gl
*swapchain_gl
)
2033 struct wined3d_context
*context
= &context_gl
->c
;
2034 const struct wined3d_d3d_info
*d3d_info
;
2035 const struct wined3d_gl_info
*gl_info
;
2036 struct wined3d_device
*device
;
2040 TRACE("context_gl %p, swapchain %p.\n", context_gl
, swapchain_gl
);
2042 wined3d_context_init(&context_gl
->c
, &swapchain_gl
->s
);
2044 device
= context
->device
;
2045 gl_info
= &wined3d_adapter_gl(device
->adapter
)->gl_info
;
2046 context_gl
->gl_info
= gl_info
;
2047 d3d_info
= context
->d3d_info
;
2049 context_gl
->tid
= GetCurrentThreadId();
2050 context_gl
->window
= context
->swapchain
->win_handle
;
2051 if (context_gl
->window
== GetDesktopWindow())
2053 TRACE("Swapchain is created on the desktop window, trying backup device context.\n");
2054 context_gl
->dc
= NULL
;
2056 else if (!(context_gl
->dc
= GetDCEx(context_gl
->window
, 0, DCX_USESTYLE
| DCX_CACHE
)))
2057 WARN("Failed to retrieve device context, trying swapchain backup.\n");
2059 if (!context_gl
->dc
)
2061 if (!(context_gl
->dc
= wined3d_device_gl_get_backup_dc(wined3d_device_gl(device
))))
2063 ERR("Failed to retrieve a device context.\n");
2066 context_gl
->dc_is_private
= TRUE
;
2069 list_init(&context_gl
->fbo_list
);
2070 list_init(&context_gl
->fbo_destroy_list
);
2072 list_init(&context_gl
->occlusion_queries
);
2073 list_init(&context_gl
->fences
);
2074 list_init(&context_gl
->timestamp_queries
);
2075 list_init(&context_gl
->so_statistics_queries
);
2076 list_init(&context_gl
->pipeline_statistics_queries
);
2078 for (i
= 0; i
< ARRAY_SIZE(context_gl
->tex_unit_map
); ++i
)
2079 context_gl
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2080 for (i
= 0; i
< ARRAY_SIZE(context_gl
->rev_tex_unit_map
); ++i
)
2081 context_gl
->rev_tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2082 if (gl_info
->limits
.graphics_samplers
>= WINED3D_MAX_COMBINED_SAMPLERS
)
2084 /* Initialize the texture unit mapping to a 1:1 mapping. */
2085 unsigned int base
, count
;
2087 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_PIXEL
, &base
, &count
);
2088 if (base
+ WINED3D_MAX_FRAGMENT_SAMPLERS
> ARRAY_SIZE(context_gl
->rev_tex_unit_map
))
2090 ERR("Unexpected texture unit base index %u.\n", base
);
2093 for (i
= 0; i
< min(count
, WINED3D_MAX_FRAGMENT_SAMPLERS
); ++i
)
2095 context_gl
->tex_unit_map
[i
] = base
+ i
;
2096 context_gl
->rev_tex_unit_map
[base
+ i
] = i
;
2099 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_VERTEX
, &base
, &count
);
2100 if (base
+ WINED3D_MAX_VERTEX_SAMPLERS
> ARRAY_SIZE(context_gl
->rev_tex_unit_map
))
2102 ERR("Unexpected texture unit base index %u.\n", base
);
2105 for (i
= 0; i
< min(count
, WINED3D_MAX_VERTEX_SAMPLERS
); ++i
)
2107 context_gl
->tex_unit_map
[WINED3D_MAX_FRAGMENT_SAMPLERS
+ i
] = base
+ i
;
2108 context_gl
->rev_tex_unit_map
[base
+ i
] = WINED3D_MAX_FRAGMENT_SAMPLERS
+ i
;
2112 if (!(context_gl
->texture_type
= calloc(gl_info
->limits
.combined_samplers
,
2113 sizeof(*context_gl
->texture_type
))))
2116 if (!wined3d_context_gl_create_wgl_ctx(context_gl
, swapchain_gl
, &new_drawable
))
2119 /* Set up the context defaults. */
2121 context_gl
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
2123 if (!wined3d_context_gl_set_current(context_gl
))
2125 ERR("Cannot activate context to set up defaults.\n");
2126 context_release(context
);
2127 if (!wglDeleteContext(context_gl
->gl_ctx
))
2128 ERR("wglDeleteContext(%p) failed, last error %#lx.\n", context_gl
->gl_ctx
, GetLastError());
2132 if (context_debug_output_enabled(gl_info
))
2134 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, context
));
2135 if (TRACE_ON(d3d_sync
))
2136 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
2137 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
2140 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
2141 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2145 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
2146 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2147 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
2148 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2149 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
2150 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2152 if (WARN_ON(d3d_perf
))
2154 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
2155 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2159 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2160 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &context_gl
->aux_buffers
);
2162 TRACE("Setting up the screen\n");
2164 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2166 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
2167 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2169 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
2170 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2172 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
2173 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2179 GL_EXTCALL(glGenVertexArrays(1, &vao
));
2180 GL_EXTCALL(glBindVertexArray(vao
));
2181 checkGLcall("creating VAO");
2184 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
2185 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2186 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
2187 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2189 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
2191 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2192 * the previous texture where to source the offset from is always unit - 1.
2194 for (i
= 1; i
< gl_info
->limits
.ffp_textures
; ++i
)
2196 wined3d_context_gl_active_texture(context_gl
, gl_info
, i
);
2197 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
2198 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ i
- 1);
2199 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2202 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
2204 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2205 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2206 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2207 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2210 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2211 * program and the dummy program is destroyed when the context is destroyed.
2213 static const char dummy_program
[] =
2215 "MOV result.color, fragment.color.primary;\n"
2217 GL_EXTCALL(glGenProgramsARB(1, &context_gl
->dummy_arbfp_prog
));
2218 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, context_gl
->dummy_arbfp_prog
));
2219 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
,
2220 GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
2223 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2225 for (i
= 0; i
< gl_info
->limits
.ffp_textures
; ++i
)
2227 wined3d_context_gl_active_texture(context_gl
, gl_info
, i
);
2228 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
2229 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2233 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
2235 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
2237 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
2239 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
2241 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_NO_PRIMITIVE_RESTART
))
2243 if (gl_info
->supported
[ARB_ES3_COMPATIBILITY
])
2245 gl_info
->gl_ops
.gl
.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX
);
2246 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2250 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2253 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_CUBEMAP_FILTERING
)
2254 && gl_info
->supported
[ARB_SEAMLESS_CUBE_MAP
])
2256 gl_info
->gl_ops
.gl
.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
2257 checkGLcall("enable seamless cube map filtering");
2259 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
2260 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN
, GL_LOWER_LEFT
));
2262 /* If this happens to be the first context for the device, dummy textures
2263 * are not created yet. In that case, they will be created (and bound) by
2264 * create_dummy_textures right after this context is initialized. */
2265 if (wined3d_device_gl(device
)->dummy_textures
.tex_2d
)
2266 wined3d_context_gl_bind_dummy_textures(context_gl
);
2268 /* Initialise all rectangles to avoid resetting unused ones later. */
2269 gl_info
->gl_ops
.gl
.p_glScissor(0, 0, 0, 0);
2270 checkGLcall("glScissor");
2274 gl_info
->gl_ops
.gl
.p_glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
2275 gl_info
->gl_ops
.gl
.p_glClearDepth(1.0f
);
2276 gl_info
->gl_ops
.gl
.p_glClearStencil(0);
2277 gl_info
->gl_ops
.gl
.p_glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
2278 checkGLcall("glClear");
2283 free(context_gl
->texture_type
);
2284 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
2288 void wined3d_context_gl_destroy(struct wined3d_context_gl
*context_gl
)
2290 struct wined3d_device
*device
= context_gl
->c
.device
;
2292 TRACE("Destroying context %p.\n", context_gl
);
2294 wined3d_from_cs(device
->cs
);
2296 /* We delay destroying a context when it is active. The context_release()
2297 * function invokes wined3d_context_gl_destroy() again while leaving the
2299 if (context_gl
->level
)
2301 TRACE("Delaying destruction of context %p.\n", context_gl
);
2302 context_gl
->c
.destroy_delayed
= 1;
2303 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2304 context_gl
->c
.swapchain
= NULL
;
2305 context_gl
->c
.device
= NULL
;
2309 device_context_remove(device
, &context_gl
->c
);
2311 if (context_gl
->c
.current
&& context_gl
->tid
!= GetCurrentThreadId())
2313 struct wined3d_gl_info
*gl_info
;
2315 /* Make a copy of gl_info for wined3d_context_gl_cleanup() use, the
2316 * one in wined3d_adapter may go away in the meantime. */
2317 gl_info
= malloc(sizeof(*gl_info
));
2318 *gl_info
= *context_gl
->gl_info
;
2319 context_gl
->gl_info
= gl_info
;
2320 context_gl
->c
.destroyed
= 1;
2325 wined3d_context_gl_cleanup(context_gl
);
2326 TlsSetValue(context_get_tls_idx(), NULL
);
2330 const unsigned int *wined3d_context_gl_get_tex_unit_mapping(const struct wined3d_context_gl
*context_gl
,
2331 const struct wined3d_shader_version
*shader_version
, unsigned int *base
, unsigned int *count
)
2333 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2335 if (!shader_version
)
2338 *count
= WINED3D_MAX_FFP_TEXTURES
;
2339 return context_gl
->tex_unit_map
;
2342 if (shader_version
->major
>= 4)
2344 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, shader_version
->type
, base
, count
);
2348 switch (shader_version
->type
)
2350 case WINED3D_SHADER_TYPE_PIXEL
:
2352 *count
= WINED3D_MAX_FRAGMENT_SAMPLERS
;
2354 case WINED3D_SHADER_TYPE_VERTEX
:
2355 *base
= WINED3D_MAX_FRAGMENT_SAMPLERS
;
2356 *count
= WINED3D_MAX_VERTEX_SAMPLERS
;
2359 ERR("Unhandled shader type %#x.\n", shader_version
->type
);
2364 return context_gl
->tex_unit_map
;
2367 static void wined3d_context_gl_get_rt_size(const struct wined3d_context_gl
*context_gl
, SIZE
*size
)
2369 const struct wined3d_texture
*rt
= context_gl
->c
.current_rt
.texture
;
2376 GetClientRect(context_gl
->window
, &window_size
);
2377 size
->cx
= window_size
.right
- window_size
.left
;
2378 size
->cy
= window_size
.bottom
- window_size
.top
;
2383 level
= context_gl
->c
.current_rt
.sub_resource_idx
% rt
->level_count
;
2384 size
->cx
= wined3d_texture_get_level_width(rt
, level
);
2385 size
->cy
= wined3d_texture_get_level_height(rt
, level
);
2388 void wined3d_context_gl_enable_clip_distances(struct wined3d_context_gl
*context_gl
, uint32_t enable_mask
)
2390 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2391 unsigned int clip_distance_count
, i
;
2392 uint32_t disable_mask
, current_mask
;
2394 clip_distance_count
= gl_info
->limits
.user_clip_distances
;
2395 disable_mask
= ~enable_mask
;
2396 enable_mask
&= wined3d_mask_from_size(clip_distance_count
);
2397 disable_mask
&= wined3d_mask_from_size(clip_distance_count
);
2398 current_mask
= context_gl
->c
.clip_distance_mask
;
2399 context_gl
->c
.clip_distance_mask
= enable_mask
;
2401 enable_mask
&= ~current_mask
;
2404 i
= wined3d_bit_scan(&enable_mask
);
2405 gl_info
->gl_ops
.gl
.p_glEnable(GL_CLIP_DISTANCE0
+ i
);
2407 disable_mask
&= current_mask
;
2408 while (disable_mask
)
2410 i
= wined3d_bit_scan(&disable_mask
);
2411 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_DISTANCE0
+ i
);
2413 checkGLcall("toggle clip distances");
2416 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2418 return rt_mask
& (1u << 31);
2421 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2423 return rt_mask
& ~(1u << 31);
2426 /* Context activation is done by the caller. */
2427 static void wined3d_context_gl_apply_draw_buffers(struct wined3d_context_gl
*context_gl
, uint32_t rt_mask
)
2429 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2430 GLenum draw_buffers
[WINED3D_MAX_RENDER_TARGETS
];
2434 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2436 else if (is_rt_mask_onscreen(rt_mask
))
2438 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2447 draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2449 draw_buffers
[i
] = GL_NONE
;
2455 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2457 GL_EXTCALL(glDrawBuffers(i
, draw_buffers
));
2461 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffers
[0]);
2465 checkGLcall("apply draw buffers");
2468 /* Context activation is done by the caller. */
2469 void wined3d_context_gl_active_texture(struct wined3d_context_gl
*context_gl
,
2470 const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2472 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2473 checkGLcall("glActiveTexture");
2474 context_gl
->active_texture
= unit
;
2477 void wined3d_context_gl_bind_bo(struct wined3d_context_gl
*context_gl
, GLenum binding
, GLuint name
)
2479 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2481 if (binding
== GL_ELEMENT_ARRAY_BUFFER
)
2482 context_invalidate_state(&context_gl
->c
, STATE_INDEXBUFFER
);
2484 GL_EXTCALL(glBindBuffer(binding
, name
));
2487 void wined3d_context_gl_bind_texture(struct wined3d_context_gl
*context_gl
, GLenum target
, GLuint name
)
2489 const struct wined3d_dummy_textures
*textures
= &wined3d_device_gl(context_gl
->c
.device
)->dummy_textures
;
2490 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2491 GLenum old_texture_type
;
2495 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2499 unit
= context_gl
->active_texture
;
2500 old_texture_type
= context_gl
->texture_type
[unit
];
2501 if (old_texture_type
!= target
)
2503 switch (old_texture_type
)
2509 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, textures
->tex_1d
);
2511 case GL_TEXTURE_1D_ARRAY
:
2512 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, textures
->tex_1d_array
);
2515 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, textures
->tex_2d
);
2517 case GL_TEXTURE_2D_ARRAY
:
2518 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, textures
->tex_2d_array
);
2520 case GL_TEXTURE_CUBE_MAP
:
2521 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, textures
->tex_cube
);
2523 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2524 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, textures
->tex_cube_array
);
2527 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, textures
->tex_3d
);
2529 case GL_TEXTURE_BUFFER
:
2530 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, textures
->tex_buffer
);
2532 case GL_TEXTURE_2D_MULTISAMPLE
:
2533 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, textures
->tex_2d_ms
);
2535 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2536 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, textures
->tex_2d_ms_array
);
2539 ERR("Unexpected texture target %#x.\n", old_texture_type
);
2542 context_gl
->texture_type
[unit
] = target
;
2545 checkGLcall("bind texture");
2548 GLuint64
wined3d_device_gl_get_dummy_bindless_handle(const struct wined3d_device_gl
*device_gl
,
2549 enum wined3d_shader_resource_type type
)
2553 case WINED3D_SHADER_RESOURCE_BUFFER
:
2554 return device_gl
->dummy_textures
.bindless
.tex_buffer
;
2555 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
2556 return device_gl
->dummy_textures
.bindless
.tex_1d
;
2557 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2558 return device_gl
->dummy_textures
.bindless
.tex_2d
;
2559 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2560 return device_gl
->dummy_textures
.bindless
.tex_3d
;
2561 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
2562 return device_gl
->dummy_textures
.bindless
.tex_cube
;
2563 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
:
2564 return device_gl
->dummy_textures
.bindless
.tex_1d_array
;
2565 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
2566 return device_gl
->dummy_textures
.bindless
.tex_2d_array
;
2567 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY
:
2568 return device_gl
->dummy_textures
.bindless
.tex_cube_array
;
2569 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
:
2570 return device_gl
->dummy_textures
.bindless
.tex_2d_ms
;
2571 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY
:
2572 return device_gl
->dummy_textures
.bindless
.tex_2d_array
;
2574 FIXME("Unhandled resource type %#x.\n", type
);
2579 static void wined3d_context_gl_poll_fences(struct wined3d_context_gl
*context_gl
)
2581 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2582 struct wined3d_command_fence_gl
*f
;
2585 for (i
= 0; i
< context_gl
->submitted
.fence_count
; ++i
)
2587 f
= &context_gl
->submitted
.fences
[i
];
2589 if (f
->id
> device_gl
->completed_fence_id
)
2591 if (wined3d_fence_test(f
->fence
, &device_gl
->d
, 0) != WINED3D_FENCE_OK
)
2593 device_gl
->completed_fence_id
= f
->id
;
2596 wined3d_fence_destroy(f
->fence
);
2597 if (i
!= context_gl
->submitted
.fence_count
- 1)
2598 *f
= context_gl
->submitted
.fences
[context_gl
->submitted
.fence_count
- 1];
2599 --context_gl
->submitted
.fence_count
;
2603 static void wined3d_device_gl_free_memory(struct wined3d_device_gl
*device_gl
, struct wined3d_allocator_block
*block
)
2605 assert(block
->chunk
->allocator
== &device_gl
->allocator
);
2606 wined3d_device_gl_allocator_lock(device_gl
);
2607 wined3d_allocator_block_free(block
);
2608 wined3d_device_gl_allocator_unlock(device_gl
);
2611 static void wined3d_context_gl_cleanup_resources(struct wined3d_context_gl
*context_gl
)
2613 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2614 struct wined3d_retired_block_gl
*r
, *blocks
;
2615 SIZE_T count
, i
= 0;
2618 wined3d_context_gl_poll_fences(context_gl
);
2619 id
= device_gl
->completed_fence_id
;
2621 blocks
= device_gl
->retired_blocks
;
2622 count
= device_gl
->retired_block_count
;
2627 if (r
->fence_id
> id
)
2633 wined3d_device_gl_free_memory(device_gl
, r
->block
);
2637 device_gl
->retired_block_count
= count
;
2640 void wined3d_context_gl_wait_command_fence(struct wined3d_context_gl
*context_gl
, uint64_t id
)
2642 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2643 enum wined3d_fence_result ret
;
2646 if (id
<= device_gl
->completed_fence_id
2647 || id
> device_gl
->current_fence_id
) /* In case the fence ID wrapped. */
2650 for (i
= 0; i
< context_gl
->submitted
.fence_count
; ++i
)
2652 if (context_gl
->submitted
.fences
[i
].id
!= id
)
2655 if ((ret
= wined3d_fence_wait(context_gl
->submitted
.fences
[i
].fence
, &device_gl
->d
)) != WINED3D_FENCE_OK
)
2656 ERR("Failed to wait for command fence with id 0x%s, ret %#x.\n", wine_dbgstr_longlong(id
), ret
);
2657 wined3d_context_gl_cleanup_resources(context_gl
);
2661 ERR("Failed to find fence for command fence with id 0x%s.\n", wine_dbgstr_longlong(id
));
2664 void wined3d_context_gl_submit_command_fence(struct wined3d_context_gl
*context_gl
)
2666 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2667 struct wined3d_command_fence_gl
*f
;
2670 if (!wined3d_array_reserve((void **)&context_gl
->submitted
.fences
, &context_gl
->submitted
.fences_size
,
2671 context_gl
->submitted
.fence_count
+ 1, sizeof(*context_gl
->submitted
.fences
)))
2672 ERR("Failed to grow submitted command buffer array.\n");
2674 f
= &context_gl
->submitted
.fences
[context_gl
->submitted
.fence_count
++];
2675 f
->id
= device_gl
->current_fence_id
;
2676 if (FAILED(hr
= wined3d_fence_create(&device_gl
->d
, &f
->fence
)))
2677 ERR("Failed to create fence, hr %#lx.\n", hr
);
2678 wined3d_fence_issue(f
->fence
, &device_gl
->d
);
2680 /* We don't expect this to ever happen, but handle it anyway. */
2681 if (!++device_gl
->current_fence_id
)
2683 wined3d_context_gl_wait_command_fence(context_gl
, device_gl
->current_fence_id
- 1);
2684 device_gl
->completed_fence_id
= 0;
2685 device_gl
->current_fence_id
= 1;
2687 device_gl
->retired_bo_size
= 0;
2688 wined3d_context_gl_cleanup_resources(context_gl
);
2691 static void wined3d_context_gl_destroy_allocator_block(struct wined3d_context_gl
*context_gl
,
2692 struct wined3d_allocator_block
*block
, uint64_t fence_id
)
2694 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2695 struct wined3d_retired_block_gl
*r
;
2697 if (device_gl
->completed_fence_id
>= fence_id
)
2699 wined3d_device_gl_free_memory(device_gl
, block
);
2700 TRACE("Freed block %p.\n", block
);
2704 if (!wined3d_array_reserve((void **)&device_gl
->retired_blocks
,
2705 &device_gl
->retired_blocks_size
, device_gl
->retired_block_count
+ 1,
2706 sizeof(*device_gl
->retired_blocks
)))
2708 ERR("Leaking block %p.\n", block
);
2712 r
= &device_gl
->retired_blocks
[device_gl
->retired_block_count
++];
2714 r
->fence_id
= fence_id
;
2717 /* We always have buffer storage here. */
2718 GLuint
wined3d_context_gl_allocate_vram_chunk_buffer(struct wined3d_context_gl
*context_gl
,
2719 unsigned int pool
, size_t size
)
2721 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2725 TRACE("context_gl %p, pool %u, size %Iu.\n", context_gl
, pool
, size
);
2727 GL_EXTCALL(glGenBuffers(1, &id
));
2730 checkGLcall("buffer object creation");
2733 wined3d_context_gl_bind_bo(context_gl
, GL_PIXEL_UNPACK_BUFFER
, id
);
2735 flags
= wined3d_device_gl_get_memory_type_flags(pool
) | GL_DYNAMIC_STORAGE_BIT
;
2736 if (flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))
2737 flags
|= GL_MAP_PERSISTENT_BIT
| GL_MAP_COHERENT_BIT
;
2738 GL_EXTCALL(glBufferStorage(GL_PIXEL_UNPACK_BUFFER
, size
, NULL
, flags
));
2740 checkGLcall("buffer object creation");
2742 TRACE("Created buffer object %u.\n", id
);
2747 static void *wined3d_allocator_chunk_gl_map(struct wined3d_allocator_chunk_gl
*chunk_gl
,
2748 struct wined3d_context_gl
*context_gl
)
2750 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2753 TRACE("chunk %p, gl_buffer %u, map_ptr %p.\n", chunk_gl
, chunk_gl
->gl_buffer
, chunk_gl
->c
.map_ptr
);
2755 wined3d_allocator_chunk_gl_lock(chunk_gl
);
2757 if (!chunk_gl
->c
.map_ptr
)
2759 unsigned int flags
= wined3d_device_gl_get_memory_type_flags(chunk_gl
->memory_type
) & ~GL_CLIENT_STORAGE_BIT
;
2761 flags
|= GL_MAP_PERSISTENT_BIT
| GL_MAP_COHERENT_BIT
;
2762 if (!(flags
& GL_MAP_READ_BIT
))
2763 flags
|= GL_MAP_UNSYNCHRONIZED_BIT
;
2764 if (flags
& GL_MAP_WRITE_BIT
)
2765 flags
|= GL_MAP_FLUSH_EXPLICIT_BIT
;
2766 wined3d_context_gl_bind_bo(context_gl
, GL_PIXEL_UNPACK_BUFFER
, chunk_gl
->gl_buffer
);
2767 chunk_gl
->c
.map_ptr
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER
,
2768 0, WINED3D_ALLOCATOR_CHUNK_SIZE
, flags
));
2769 if (!chunk_gl
->c
.map_ptr
)
2771 wined3d_allocator_chunk_gl_unlock(chunk_gl
);
2772 ERR("Failed to map chunk memory.\n");
2776 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, WINED3D_ALLOCATOR_CHUNK_SIZE
);
2779 ++chunk_gl
->c
.map_count
;
2780 map_ptr
= chunk_gl
->c
.map_ptr
;
2782 wined3d_allocator_chunk_gl_unlock(chunk_gl
);
2787 static void wined3d_allocator_chunk_gl_unmap(struct wined3d_allocator_chunk_gl
*chunk_gl
,
2788 struct wined3d_context_gl
*context_gl
)
2790 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2792 TRACE("chunk_gl %p, context_gl %p.\n", chunk_gl
, context_gl
);
2794 wined3d_allocator_chunk_gl_lock(chunk_gl
);
2796 if (!--chunk_gl
->c
.map_count
)
2798 wined3d_context_gl_bind_bo(context_gl
, GL_PIXEL_UNPACK_BUFFER
, chunk_gl
->gl_buffer
);
2799 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER
));
2800 chunk_gl
->c
.map_ptr
= NULL
;
2802 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, -WINED3D_ALLOCATOR_CHUNK_SIZE
);
2805 wined3d_allocator_chunk_gl_unlock(chunk_gl
);
2808 static void *wined3d_bo_gl_map(struct wined3d_bo_gl
*bo
, struct wined3d_context_gl
*context_gl
, uint32_t flags
)
2810 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2811 const struct wined3d_gl_info
*gl_info
;
2812 struct wined3d_bo_user
*bo_user
;
2813 struct wined3d_bo_gl tmp
;
2815 if (flags
& WINED3D_MAP_NOOVERWRITE
)
2818 if ((flags
& WINED3D_MAP_DISCARD
) && bo
->command_fence_id
> device_gl
->completed_fence_id
)
2820 if (wined3d_device_gl_create_bo(device_gl
, context_gl
, bo
->size
,
2821 bo
->binding
, bo
->usage
, bo
->b
.coherent
, bo
->flags
, &tmp
))
2823 LIST_FOR_EACH_ENTRY(bo_user
, &bo
->b
.users
, struct wined3d_bo_user
, entry
)
2824 bo_user
->valid
= false;
2825 list_init(&bo
->b
.users
);
2827 wined3d_context_gl_destroy_bo(context_gl
, bo
);
2829 list_init(&bo
->b
.users
);
2834 ERR("Failed to create new buffer object.\n");
2837 if (context_gl
->c
.d3d_info
->fences
)
2839 if (bo
->command_fence_id
== device_gl
->current_fence_id
)
2840 wined3d_context_gl_submit_command_fence(context_gl
);
2841 wined3d_context_gl_wait_command_fence(context_gl
, bo
->command_fence_id
);
2846 return bo
->b
.map_ptr
;
2850 struct wined3d_allocator_chunk_gl
*chunk_gl
= wined3d_allocator_chunk_gl(bo
->memory
->chunk
);
2852 if (!(bo
->b
.map_ptr
= wined3d_allocator_chunk_gl_map(chunk_gl
, context_gl
)))
2853 ERR("Failed to map chunk.\n");
2854 return bo
->b
.map_ptr
;
2857 gl_info
= context_gl
->gl_info
;
2858 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
2860 if (gl_info
->supported
[ARB_BUFFER_STORAGE
])
2862 GLbitfield gl_flags
;
2864 /* When mapping the bo persistently, we need to use the access flags
2865 * used to create the bo, instead of the access flags passed to the
2866 * map call. Otherwise, if for example the initial map call that
2867 * caused the bo to be persistently mapped was a read-only map,
2868 * subsequent write access to the bo would be undefined.
2870 * Note that we use GL_MAP_PERSISTENT_BIT for non-persistent maps here
2871 * as well, in order to allow draws to succeed while referenced buffer
2872 * resources are mapped. On the other hand, we don't want to use the
2873 * access flags used to create the bo for non-persistent maps, because
2874 * that may imply dropping GL_MAP_UNSYNCHRONIZED_BIT. */
2875 gl_flags
= bo
->flags
& ~GL_CLIENT_STORAGE_BIT
;
2876 if (!(gl_flags
& GL_MAP_READ_BIT
))
2877 gl_flags
|= GL_MAP_UNSYNCHRONIZED_BIT
;
2878 if (gl_flags
& GL_MAP_WRITE_BIT
)
2879 gl_flags
|= GL_MAP_FLUSH_EXPLICIT_BIT
;
2880 gl_flags
|= GL_MAP_PERSISTENT_BIT
| GL_MAP_COHERENT_BIT
;
2882 bo
->b
.map_ptr
= GL_EXTCALL(glMapBufferRange(bo
->binding
, 0, bo
->size
, gl_flags
));
2884 else if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
2886 bo
->b
.map_ptr
= GL_EXTCALL(glMapBufferRange(bo
->binding
, 0, bo
->size
, wined3d_resource_gl_map_flags(bo
, flags
)));
2890 bo
->b
.map_ptr
= GL_EXTCALL(glMapBuffer(bo
->binding
, wined3d_resource_gl_legacy_map_flags(flags
)));
2894 adapter_adjust_mapped_memory(device_gl
->d
.adapter
, bo
->size
);
2896 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, 0);
2897 checkGLcall("Map buffer object");
2899 return bo
->b
.map_ptr
;
2902 static void wined3d_bo_gl_unmap(struct wined3d_bo_gl
*bo
, struct wined3d_context_gl
*context_gl
)
2904 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2906 if (context_gl
->c
.d3d_info
->persistent_map
2907 && context_gl
->c
.device
->adapter
->mapped_size
<= MAX_PERSISTENT_MAPPED_BYTES
)
2909 TRACE("Not unmapping BO %p.\n", bo
);
2913 wined3d_device_bo_map_lock(context_gl
->c
.device
);
2914 /* The mapping is still in use by the client (viz. for an accelerated
2915 * NOOVERWRITE map). The client will trigger another unmap request when the
2916 * d3d application requests to unmap the BO. */
2917 if (bo
->b
.client_map_count
)
2919 wined3d_device_bo_map_unlock(context_gl
->c
.device
);
2920 assert(context_gl
->c
.d3d_info
->persistent_map
);
2921 TRACE("BO %p is still in use by a client thread; not unmapping.\n", bo
);
2924 bo
->b
.map_ptr
= NULL
;
2925 wined3d_device_bo_map_unlock(context_gl
->c
.device
);
2929 struct wined3d_allocator_chunk_gl
*chunk_gl
= wined3d_allocator_chunk_gl(bo
->memory
->chunk
);
2931 wined3d_allocator_chunk_gl_unmap(chunk_gl
, context_gl
);
2935 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
2936 GL_EXTCALL(glUnmapBuffer(bo
->binding
));
2937 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, 0);
2938 checkGLcall("Unmap buffer object");
2940 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, -bo
->size
);
2943 void *wined3d_context_gl_map_bo_address(struct wined3d_context_gl
*context_gl
,
2944 const struct wined3d_bo_address
*data
, size_t size
, uint32_t flags
)
2946 struct wined3d_bo
*bo
;
2949 if (!(bo
= data
->buffer_object
))
2952 if (!(map_ptr
= wined3d_bo_gl_map(wined3d_bo_gl(bo
), context_gl
, flags
)))
2954 ERR("Failed to map bo.\n");
2958 return (uint8_t *)map_ptr
+ bo
->buffer_offset
+ (uintptr_t)data
->addr
;
2961 static void flush_bo_ranges(struct wined3d_context_gl
*context_gl
, const struct wined3d_const_bo_address
*data
,
2962 unsigned int range_count
, const struct wined3d_range
*ranges
)
2964 const struct wined3d_gl_info
*gl_info
;
2965 struct wined3d_bo_gl
*bo
;
2968 if (!data
->buffer_object
|| data
->buffer_object
->coherent
)
2970 bo
= wined3d_bo_gl(data
->buffer_object
);
2972 gl_info
= context_gl
->gl_info
;
2973 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
2975 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
2977 /* The offset passed to glFlushMappedBufferRange() is relative to the
2978 * mapped range, but we map the whole buffer anyway. */
2979 for (i
= 0; i
< range_count
; ++i
)
2981 GL_EXTCALL(glFlushMappedBufferRange(bo
->binding
,
2982 bo
->b
.buffer_offset
+ (uintptr_t)data
->addr
+ ranges
[i
].offset
, ranges
[i
].size
));
2985 else if (gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
])
2987 for (i
= 0; i
< range_count
; ++i
)
2989 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(bo
->binding
,
2990 bo
->b
.buffer_offset
+ (uintptr_t)data
->addr
+ ranges
[i
].offset
, ranges
[i
].size
));
2991 checkGLcall("glFlushMappedBufferRangeAPPLE");
2995 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, 0);
2996 checkGLcall("Flush buffer object");
2999 void wined3d_context_gl_unmap_bo_address(struct wined3d_context_gl
*context_gl
,
3000 const struct wined3d_bo_address
*data
, unsigned int range_count
, const struct wined3d_range
*ranges
)
3002 struct wined3d_bo_gl
*bo
;
3004 if (!data
->buffer_object
)
3006 bo
= wined3d_bo_gl(data
->buffer_object
);
3008 assert(bo
->b
.map_ptr
);
3010 flush_bo_ranges(context_gl
, wined3d_const_bo_address(data
), range_count
, ranges
);
3011 wined3d_bo_gl_unmap(bo
, context_gl
);
3014 void wined3d_context_gl_flush_bo_address(struct wined3d_context_gl
*context_gl
,
3015 const struct wined3d_const_bo_address
*data
, size_t size
)
3017 struct wined3d_range range
;
3019 TRACE("context_gl %p, data %s, size %Iu.\n", context_gl
, debug_const_bo_address(data
), size
);
3021 range
.offset
= (uintptr_t)data
->addr
;
3024 flush_bo_ranges(context_gl
, data
, 1, &range
);
3027 void wined3d_context_gl_copy_bo_address(struct wined3d_context_gl
*context_gl
,
3028 const struct wined3d_bo_address
*dst
, const struct wined3d_bo_address
*src
,
3029 unsigned int range_count
, const struct wined3d_range
*ranges
, uint32_t map_flags
)
3031 const struct wined3d_gl_info
*gl_info
;
3032 struct wined3d_bo_gl
*src_bo
, *dst_bo
;
3033 BYTE
*dst_ptr
, *src_ptr
;
3036 gl_info
= context_gl
->gl_info
;
3037 src_bo
= src
->buffer_object
? wined3d_bo_gl(src
->buffer_object
) : NULL
;
3038 dst_bo
= dst
->buffer_object
? wined3d_bo_gl(dst
->buffer_object
) : NULL
;
3040 if (dst_bo
&& src_bo
)
3042 if (gl_info
->supported
[ARB_COPY_BUFFER
])
3044 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER
, src_bo
->id
));
3045 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER
, dst_bo
->id
));
3047 for (i
= 0; i
< range_count
; ++i
)
3048 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
,
3049 src_bo
->b
.buffer_offset
+ (GLintptr
)src
->addr
+ ranges
[i
].offset
,
3050 dst_bo
->b
.buffer_offset
+ (GLintptr
)dst
->addr
+ ranges
[i
].offset
, ranges
[i
].size
));
3051 checkGLcall("direct buffer copy");
3053 wined3d_context_gl_reference_bo(context_gl
, src_bo
);
3054 wined3d_context_gl_reference_bo(context_gl
, dst_bo
);
3058 src_ptr
= wined3d_context_gl_map_bo_address(context_gl
, src
,
3059 src_bo
->size
- (uintptr_t)src
->addr
, WINED3D_MAP_READ
);
3060 dst_ptr
= wined3d_context_gl_map_bo_address(context_gl
, dst
,
3061 dst_bo
->size
- (uintptr_t)dst
->addr
, map_flags
);
3063 for (i
= 0; i
< range_count
; ++i
)
3064 memcpy(dst_ptr
+ ranges
[i
].offset
, src_ptr
+ ranges
[i
].offset
, ranges
[i
].size
);
3066 wined3d_context_gl_unmap_bo_address(context_gl
, dst
, range_count
, ranges
);
3067 wined3d_context_gl_unmap_bo_address(context_gl
, src
, 0, NULL
);
3070 else if (!dst_bo
&& src_bo
)
3072 wined3d_context_gl_bind_bo(context_gl
, src_bo
->binding
, src_bo
->id
);
3073 for (i
= 0; i
< range_count
; ++i
)
3074 GL_EXTCALL(glGetBufferSubData(src_bo
->binding
,
3075 src_bo
->b
.buffer_offset
+ (GLintptr
)src
->addr
+ ranges
[i
].offset
,
3076 ranges
[i
].size
, dst
->addr
+ ranges
[i
].offset
));
3077 checkGLcall("buffer download");
3079 wined3d_context_gl_reference_bo(context_gl
, src_bo
);
3081 else if (dst_bo
&& !src_bo
)
3083 if ((map_flags
& WINED3D_MAP_DISCARD
) && (dst_bo
->flags
& GL_MAP_WRITE_BIT
))
3085 dst_ptr
= wined3d_context_gl_map_bo_address(context_gl
, dst
, dst_bo
->size
, map_flags
);
3086 memcpy(dst_ptr
, src
->addr
, dst_bo
->size
);
3087 wined3d_context_gl_unmap_bo_address(context_gl
, dst
, range_count
, ranges
);
3091 wined3d_context_gl_bind_bo(context_gl
, dst_bo
->binding
, dst_bo
->id
);
3092 for (i
= 0; i
< range_count
; ++i
)
3093 GL_EXTCALL(glBufferSubData(dst_bo
->binding
,
3094 dst_bo
->b
.buffer_offset
+ (GLintptr
)dst
->addr
+ ranges
[i
].offset
,
3095 ranges
[i
].size
, src
->addr
+ ranges
[i
].offset
));
3096 checkGLcall("buffer upload");
3098 wined3d_context_gl_reference_bo(context_gl
, dst_bo
);
3103 for (i
= 0; i
< range_count
; ++i
)
3104 memcpy(dst
->addr
+ ranges
[i
].offset
, src
->addr
+ ranges
[i
].offset
, ranges
[i
].size
);
3108 void wined3d_context_gl_destroy_bo(struct wined3d_context_gl
*context_gl
, struct wined3d_bo_gl
*bo
)
3110 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
3111 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3113 TRACE("context_gl %p, bo %p.\n", context_gl
, bo
);
3115 assert(list_empty(&bo
->b
.users
));
3119 unsigned int order
= bo
->memory
->order
;
3122 wined3d_allocator_chunk_gl_unmap(wined3d_allocator_chunk_gl(bo
->memory
->chunk
), context_gl
);
3123 wined3d_context_gl_destroy_allocator_block(context_gl
, bo
->memory
, bo
->command_fence_id
);
3125 if (bo
->command_fence_id
== device_gl
->current_fence_id
)
3127 device_gl
->retired_bo_size
+= WINED3D_ALLOCATOR_CHUNK_SIZE
>> order
;
3128 if (device_gl
->retired_bo_size
> WINED3D_RETIRED_BO_SIZE_THRESHOLD
)
3129 wined3d_context_gl_submit_command_fence(context_gl
);
3138 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
3139 GL_EXTCALL(glUnmapBuffer(bo
->binding
));
3140 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, -bo
->size
);
3143 TRACE("Destroying GL buffer %u.\n", bo
->id
);
3145 GL_EXTCALL(glDeleteBuffers(1, &bo
->id
));
3146 checkGLcall("buffer object destruction");
3150 GLenum
wined3d_context_gl_get_offscreen_gl_buffer(const struct wined3d_context_gl
*context_gl
)
3152 return GL_COLOR_ATTACHMENT0
;
3155 static uint32_t wined3d_context_gl_generate_rt_mask_no_fbo(const struct wined3d_context_gl
*context_gl
,
3156 struct wined3d_resource
*rt
)
3158 if (!rt
|| rt
->format
->id
== WINED3DFMT_NULL
)
3160 else if (rt
->type
!= WINED3D_RTYPE_BUFFER
&& texture_from_resource(rt
)->swapchain
)
3161 return context_generate_rt_mask_from_resource(rt
);
3163 return context_generate_rt_mask(wined3d_context_gl_get_offscreen_gl_buffer(context_gl
));
3166 /* Context activation is done by the caller. */
3167 void wined3d_context_gl_apply_blit_state(struct wined3d_context_gl
*context_gl
, const struct wined3d_device
*device
)
3169 struct wined3d_context
*context
= &context_gl
->c
;
3170 const struct wined3d_gl_info
*gl_info
;
3171 unsigned int sampler
;
3174 TRACE("Setting up context %p for blitting.\n", context
);
3176 gl_info
= context_gl
->gl_info
;
3178 wined3d_context_gl_get_rt_size(context_gl
, &rt_size
);
3180 if (context
->last_was_blit
)
3182 if (context_gl
->blit_size
.cx
!= rt_size
.cx
|| context_gl
->blit_size
.cy
!= rt_size
.cy
)
3184 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, rt_size
.cx
, rt_size
.cy
);
3185 context
->viewport_count
= WINED3D_MAX_VIEWPORTS
;
3186 context_gl
->blit_size
= rt_size
;
3187 /* No need to dirtify here, the states are still dirtified because
3188 * they weren't applied since the last context_apply_blit_state()
3191 checkGLcall("blit state application");
3192 TRACE("Context is already set up for blitting, nothing to do.\n");
3195 context
->last_was_blit
= TRUE
;
3197 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
3198 GL_EXTCALL(glBindSampler(0, 0));
3199 wined3d_context_gl_active_texture(context_gl
, gl_info
, 0);
3201 sampler
= context_gl
->rev_tex_unit_map
[0];
3202 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
3204 if (sampler
< WINED3D_MAX_FFP_TEXTURES
)
3206 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
3207 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
3210 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
3211 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
3213 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
3215 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
3216 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
3218 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
3219 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3220 context_invalidate_state(context
, STATE_BLEND
);
3221 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
3222 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
3223 context_invalidate_state(context
, STATE_RASTERIZER
);
3224 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
3225 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
3226 context_invalidate_state(context
, STATE_DEPTH_STENCIL
);
3227 if (gl_info
->supported
[ARB_POINT_SPRITE
])
3229 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
3230 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
3232 if (gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
3234 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
3235 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3238 context
->last_was_rhw
= TRUE
;
3239 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
3241 wined3d_context_gl_enable_clip_distances(context_gl
, 0);
3242 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
3244 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
3245 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
3246 GL_EXTCALL(glClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
));
3247 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, rt_size
.cx
, rt_size
.cy
);
3248 context
->viewport_count
= WINED3D_MAX_VIEWPORTS
;
3249 context_invalidate_state(context
, STATE_VIEWPORT
);
3251 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
3253 context_gl
->blit_size
= rt_size
;
3255 checkGLcall("blit state application");
3258 static void wined3d_context_gl_apply_blit_projection(const struct wined3d_context_gl
*context_gl
,
3259 unsigned int w
, unsigned int h
)
3261 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3262 const GLdouble projection
[] =
3264 2.0 / w
, 0.0, 0.0, 0.0,
3265 0.0, 2.0 / h
, 0.0, 0.0,
3267 -1.0, -1.0, -1.0, 1.0,
3270 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
3271 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
3274 /* Setup OpenGL states for fixed-function blitting. */
3275 /* Context activation is done by the caller. */
3276 void wined3d_context_gl_apply_ffp_blit_state(struct wined3d_context_gl
*context_gl
,
3277 const struct wined3d_device
*device
)
3279 struct wined3d_context
*context
= &context_gl
->c
;
3280 const struct wined3d_gl_info
*gl_info
;
3281 unsigned int i
, sampler
;
3283 gl_info
= context_gl
->gl_info
;
3284 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
3285 ERR("Applying fixed-function state without legacy context support.\n");
3287 if (context
->last_was_ffp_blit
)
3291 wined3d_context_gl_get_rt_size(context_gl
, &rt_size
);
3292 if (context_gl
->blit_size
.cx
!= rt_size
.cx
|| context_gl
->blit_size
.cy
!= rt_size
.cy
)
3293 wined3d_context_gl_apply_blit_projection(context_gl
, rt_size
.cx
, rt_size
.cy
);
3294 wined3d_context_gl_apply_blit_state(context_gl
, device
);
3296 checkGLcall("ffp blit state application");
3299 context
->last_was_ffp_blit
= TRUE
;
3301 wined3d_context_gl_apply_blit_state(context_gl
, device
);
3303 /* Disable all textures. The caller can then bind a texture it wants to blit
3305 for (i
= gl_info
->limits
.ffp_textures
- 1; i
> 0 ; --i
)
3307 wined3d_context_gl_active_texture(context_gl
, gl_info
, i
);
3309 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
3310 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
3311 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
3312 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
3314 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
3316 sampler
= context_gl
->rev_tex_unit_map
[i
];
3317 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
3319 if (sampler
< WINED3D_MAX_FFP_TEXTURES
)
3320 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
3324 wined3d_context_gl_active_texture(context_gl
, gl_info
, 0);
3326 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
3327 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
3328 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
3329 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
3331 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
3332 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
3333 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
3335 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
3336 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
3338 /* Setup transforms. */
3339 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
3340 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
3341 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3342 wined3d_context_gl_apply_blit_projection(context_gl
, context_gl
->blit_size
.cx
, context_gl
->blit_size
.cy
);
3343 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
3345 /* Other misc states. */
3346 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
3347 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
3348 gl_info
->p_glDisableWINE(GL_FOG
);
3349 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
3351 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
3353 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
3354 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
3356 checkGLcall("ffp blit state application");
3359 static BOOL
have_framebuffer_attachment(unsigned int rt_count
, struct wined3d_rendertarget_view
* const *rts
,
3360 const struct wined3d_rendertarget_view
*ds
)
3367 for (i
= 0; i
< rt_count
; ++i
)
3369 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3376 /* Context activation is done by the caller. */
3377 BOOL
wined3d_context_gl_apply_clear_state(struct wined3d_context_gl
*context_gl
,
3378 const struct wined3d_state
*state
, unsigned int rt_count
, const struct wined3d_fb_state
*fb
)
3380 struct wined3d_rendertarget_view
* const *rts
= fb
->render_targets
;
3381 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3382 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
3383 uint32_t rt_mask
= 0, *cur_mask
;
3386 if (isStateDirty(&context_gl
->c
, STATE_FRAMEBUFFER
) || fb
!= &state
->fb
3387 || rt_count
!= gl_info
->limits
.buffers
)
3389 struct wined3d_rendertarget_info ds_info
= {{0}};
3391 if (!have_framebuffer_attachment(rt_count
, rts
, dsv
))
3393 WARN("Invalid render target config, need at least one attachment.\n");
3397 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
3399 memset(context_gl
->blit_targets
, 0, sizeof(context_gl
->blit_targets
));
3400 for (i
= 0; i
< rt_count
; ++i
)
3404 struct wined3d_rendertarget_view_gl
*rtv_gl
= wined3d_rendertarget_view_gl(rts
[i
]);
3405 context_gl
->blit_targets
[i
].gl_view
= rtv_gl
->gl_view
;
3406 context_gl
->blit_targets
[i
].resource
= rtv_gl
->v
.resource
;
3407 context_gl
->blit_targets
[i
].sub_resource_idx
= rtv_gl
->v
.sub_resource_idx
;
3408 context_gl
->blit_targets
[i
].layer_count
= rtv_gl
->v
.layer_count
;
3410 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3411 rt_mask
|= (1u << i
);
3416 struct wined3d_rendertarget_view_gl
*dsv_gl
= wined3d_rendertarget_view_gl(dsv
);
3417 ds_info
.gl_view
= dsv_gl
->gl_view
;
3418 ds_info
.resource
= dsv_gl
->v
.resource
;
3419 ds_info
.sub_resource_idx
= dsv_gl
->v
.sub_resource_idx
;
3420 ds_info
.layer_count
= dsv_gl
->v
.layer_count
;
3423 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, context_gl
->blit_targets
, &ds_info
,
3424 rt_count
? rts
[0]->resource
->draw_binding
: 0, dsv
? dsv
->resource
->draw_binding
: 0);
3428 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, NULL
, &ds_info
,
3429 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
3430 rt_mask
= context_generate_rt_mask_from_resource(rts
[0]->resource
);
3433 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3434 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3435 * state management allows this */
3436 context_invalidate_state(&context_gl
->c
, STATE_FRAMEBUFFER
);
3438 else if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
3440 for (i
= 0; i
< rt_count
; ++i
)
3442 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3443 rt_mask
|= (1u << i
);
3448 rt_mask
= wined3d_context_gl_generate_rt_mask_no_fbo(context_gl
, rt_count
? rts
[0]->resource
: NULL
);
3451 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3453 if (rt_mask
!= *cur_mask
)
3455 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3456 *cur_mask
= rt_mask
;
3457 context_invalidate_state(&context_gl
->c
, STATE_FRAMEBUFFER
);
3460 wined3d_context_gl_check_fbo_status(context_gl
, GL_FRAMEBUFFER
);
3462 context_gl
->c
.last_was_blit
= FALSE
;
3463 context_gl
->c
.last_was_ffp_blit
= FALSE
;
3465 /* Blending and clearing should be orthogonal, but tests on the nvidia
3466 * driver show that disabling blending when clearing improves the clearing
3467 * performance incredibly. */
3468 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
3469 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
3470 if (rt_count
&& gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
3472 if (needs_srgb_write(context_gl
->c
.d3d_info
, state
, fb
))
3473 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
3475 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
3476 context_invalidate_state(&context_gl
->c
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3478 checkGLcall("setting up state for clear");
3480 context_invalidate_state(&context_gl
->c
, STATE_BLEND
);
3481 context_invalidate_state(&context_gl
->c
, STATE_RASTERIZER
);
3482 context_invalidate_state(&context_gl
->c
, STATE_SCISSORRECT
);
3487 static uint32_t find_draw_buffers_mask(const struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
)
3489 struct wined3d_rendertarget_view
* const *rts
= state
->fb
.render_targets
;
3490 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
3491 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3492 unsigned int rt_mask
, mask
;
3495 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
3496 rt_mask
&= wined3d_mask_from_size(gl_info
->limits
.buffers
);
3497 if (state
->blend_state
&& state
->blend_state
->dual_source
)
3503 i
= wined3d_bit_scan(&mask
);
3504 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
3505 rt_mask
&= ~(1u << i
);
3511 void context_gl_apply_texture_draw_state(struct wined3d_context_gl
*context_gl
,
3512 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, unsigned int location
)
3514 const struct wined3d_format
*format
= texture
->resource
.format
;
3515 struct wined3d_context
*context
= &context_gl
->c
;
3516 uint32_t rt_mask
, *cur_mask
;
3518 TRACE("context_gl %p, texture %p, sub_resource_idx %u, location %s.\n",
3519 context_gl
, texture
, sub_resource_idx
, wined3d_debug_location(location
));
3521 if (format
->depth_size
|| format
->stencil_size
)
3522 wined3d_context_gl_apply_fbo_state_explicit(context_gl
, GL_DRAW_FRAMEBUFFER
, NULL
,
3523 0, &texture
->resource
, sub_resource_idx
, location
);
3525 wined3d_context_gl_apply_fbo_state_explicit(context_gl
, GL_DRAW_FRAMEBUFFER
, &texture
->resource
,
3526 sub_resource_idx
, NULL
, 0, location
);
3528 if (location
!= WINED3D_LOCATION_DRAWABLE
)
3530 if (texture
->resource
.format
->id
== WINED3DFMT_NULL
|| format
->depth_size
|| format
->stencil_size
)
3537 rt_mask
= context_generate_rt_mask_from_resource(&texture
->resource
);
3540 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3541 if (rt_mask
!= *cur_mask
)
3543 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3544 *cur_mask
= rt_mask
;
3547 wined3d_context_gl_check_fbo_status(context_gl
, GL_DRAW_FRAMEBUFFER
);
3549 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
3552 /* Context activation is done by the caller. */
3553 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3555 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
3556 uint32_t rt_mask
= find_draw_buffers_mask(context_gl
, state
);
3557 const struct wined3d_rendertarget_view_gl
*view_gl
;
3558 struct wined3d_rendertarget_info ds_info
= {{0}};
3559 const struct wined3d_fb_state
*fb
= &state
->fb
;
3560 DWORD color_location
= 0;
3563 memset(context_gl
->blit_targets
, 0, sizeof(context_gl
->blit_targets
));
3564 for (unsigned int i
= 0; i
< context_gl
->gl_info
->limits
.buffers
; ++i
)
3566 if (!fb
->render_targets
[i
])
3569 view_gl
= wined3d_rendertarget_view_gl(fb
->render_targets
[i
]);
3570 context_gl
->blit_targets
[i
].gl_view
= view_gl
->gl_view
;
3571 context_gl
->blit_targets
[i
].resource
= view_gl
->v
.resource
;
3572 context_gl
->blit_targets
[i
].sub_resource_idx
= view_gl
->v
.sub_resource_idx
;
3573 context_gl
->blit_targets
[i
].layer_count
= view_gl
->v
.layer_count
;
3575 if (!color_location
)
3576 color_location
= view_gl
->v
.resource
->draw_binding
;
3579 if (fb
->depth_stencil
)
3581 view_gl
= wined3d_rendertarget_view_gl(fb
->depth_stencil
);
3582 ds_info
.gl_view
= view_gl
->gl_view
;
3583 ds_info
.resource
= view_gl
->v
.resource
;
3584 ds_info
.sub_resource_idx
= view_gl
->v
.sub_resource_idx
;
3585 ds_info
.layer_count
= view_gl
->v
.layer_count
;
3588 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, context_gl
->blit_targets
, &ds_info
,
3589 color_location
, fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
3591 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3592 if (rt_mask
!= *cur_mask
)
3594 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3595 *cur_mask
= rt_mask
;
3599 static void wined3d_context_gl_map_stage(struct wined3d_context_gl
*context_gl
, unsigned int stage
, unsigned int unit
)
3601 unsigned int i
= context_gl
->rev_tex_unit_map
[unit
];
3602 unsigned int j
= context_gl
->tex_unit_map
[stage
];
3604 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
3605 context_gl
->tex_unit_map
[stage
] = unit
;
3606 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
3607 context_gl
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
3609 context_gl
->rev_tex_unit_map
[unit
] = stage
;
3610 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
3611 context_gl
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
3614 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
3618 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
3619 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
3622 static bool use_ffp_ps(const struct wined3d_state
*state
)
3624 struct wined3d_shader
*vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
3628 if (vs
&& vs
->reg_maps
.shader_version
.major
>= 4)
3633 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
3634 const struct wined3d_state
*state
)
3636 UINT i
= 0, start
, end
;
3638 context
->fixed_function_usage_map
= 0;
3640 if (use_ffp_ps(state
))
3642 for (i
= 0; i
< WINED3D_MAX_FFP_TEXTURES
; ++i
)
3644 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
3645 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
3646 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
3647 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
3648 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
3649 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
3650 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
3651 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
3653 /* Not used, and disable higher stages. */
3654 if (color_op
== WINED3D_TOP_DISABLE
)
3657 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
3658 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
3659 || ((color_arg3
== WINED3DTA_TEXTURE
)
3660 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
3661 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
3662 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
3663 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
3664 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
3665 context
->fixed_function_usage_map
|= (1u << i
);
3667 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
3668 && i
< WINED3D_MAX_FFP_TEXTURES
- 1)
3669 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
3673 if (i
< context
->lowest_disabled_stage
)
3676 end
= context
->lowest_disabled_stage
;
3680 start
= context
->lowest_disabled_stage
;
3684 context
->lowest_disabled_stage
= i
;
3685 for (i
= start
+ 1; i
< end
; ++i
)
3687 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3691 static void wined3d_context_gl_map_fixed_function_samplers(struct wined3d_context_gl
*context_gl
,
3692 const struct wined3d_state
*state
)
3694 const struct wined3d_d3d_info
*d3d_info
= context_gl
->c
.d3d_info
;
3695 unsigned int i
, tex
;
3698 ffu_map
= context_gl
->c
.fixed_function_usage_map
;
3700 if (d3d_info
->ffp_fragment_caps
.max_textures
== d3d_info
->ffp_fragment_caps
.max_blend_stages
3701 || context_gl
->c
.lowest_disabled_stage
<= d3d_info
->ffp_fragment_caps
.max_textures
)
3705 i
= wined3d_bit_scan(&ffu_map
);
3706 if (context_gl
->tex_unit_map
[i
] != i
)
3708 wined3d_context_gl_map_stage(context_gl
, i
, i
);
3709 context_invalidate_state(&context_gl
->c
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
3710 context_invalidate_texture_stage(&context_gl
->c
, i
);
3716 /* Now work out the mapping */
3720 i
= wined3d_bit_scan(&ffu_map
);
3721 if (context_gl
->tex_unit_map
[i
] != tex
)
3723 wined3d_context_gl_map_stage(context_gl
, i
, tex
);
3724 context_invalidate_state(&context_gl
->c
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
3725 context_invalidate_texture_stage(&context_gl
->c
, i
);
3732 static void wined3d_context_gl_map_psamplers(struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
)
3734 const struct wined3d_d3d_info
*d3d_info
= context_gl
->c
.d3d_info
;
3735 const struct wined3d_shader_resource_info
*resource_info
=
3736 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3739 for (i
= 0; i
< WINED3D_MAX_FRAGMENT_SAMPLERS
; ++i
)
3741 if (resource_info
[i
].type
&& context_gl
->tex_unit_map
[i
] != i
)
3743 wined3d_context_gl_map_stage(context_gl
, i
, i
);
3744 context_invalidate_state(&context_gl
->c
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
3745 if (i
< d3d_info
->ffp_fragment_caps
.max_blend_stages
)
3746 context_invalidate_texture_stage(&context_gl
->c
, i
);
3751 static BOOL
wined3d_context_gl_unit_free_for_vs(const struct wined3d_context_gl
*context_gl
,
3752 const struct wined3d_shader_resource_info
*ps_resource_info
, unsigned int unit
)
3754 unsigned int current_mapping
= context_gl
->rev_tex_unit_map
[unit
];
3756 /* Not currently used */
3757 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
3760 if (current_mapping
< WINED3D_MAX_FRAGMENT_SAMPLERS
)
3762 /* Used by a fragment sampler */
3764 if (!ps_resource_info
)
3766 /* No pixel shader, check fixed function */
3767 return current_mapping
>= WINED3D_MAX_FFP_TEXTURES
3768 || !(context_gl
->c
.fixed_function_usage_map
& (1u << current_mapping
));
3771 /* Pixel shader, check the shader's sampler map */
3772 return !ps_resource_info
[current_mapping
].type
;
3778 static void wined3d_context_gl_map_vsamplers(struct wined3d_context_gl
*context_gl
,
3779 BOOL ps
, const struct wined3d_state
*state
)
3781 const struct wined3d_shader_resource_info
*vs_resource_info
=
3782 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
3783 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
3784 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3785 int start
= min(WINED3D_MAX_COMBINED_SAMPLERS
, gl_info
->limits
.graphics_samplers
) - 1;
3788 /* Note that we only care if a resource is used or not, not the
3789 * resource's specific type. Otherwise we'd need to call
3790 * shader_update_samplers() here for 1.x pixelshaders. */
3792 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3794 for (i
= 0; i
< WINED3D_MAX_VERTEX_SAMPLERS
; ++i
)
3796 DWORD vsampler_idx
= i
+ WINED3D_MAX_FRAGMENT_SAMPLERS
;
3797 if (vs_resource_info
[i
].type
)
3801 if (wined3d_context_gl_unit_free_for_vs(context_gl
, ps_resource_info
, start
))
3803 if (context_gl
->tex_unit_map
[vsampler_idx
] != start
)
3805 wined3d_context_gl_map_stage(context_gl
, vsampler_idx
, start
);
3806 context_invalidate_state(&context_gl
->c
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
3815 if (context_gl
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
3816 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
3821 static void wined3d_context_gl_update_tex_unit_map(struct wined3d_context_gl
*context_gl
,
3822 const struct wined3d_state
*state
)
3824 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3825 BOOL vs
= use_vs(state
);
3826 BOOL ps
= use_ps(state
);
3829 context_update_fixed_function_usage_map(&context_gl
->c
, state
);
3831 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3832 * need a 1:1 map at the moment.
3833 * When the mapping of a stage is changed, sampler and ALL texture stage
3834 * states have to be reset. */
3836 if (gl_info
->limits
.graphics_samplers
>= WINED3D_MAX_COMBINED_SAMPLERS
)
3840 wined3d_context_gl_map_psamplers(context_gl
, state
);
3842 wined3d_context_gl_map_fixed_function_samplers(context_gl
, state
);
3845 wined3d_context_gl_map_vsamplers(context_gl
, ps
, state
);
3848 /* Context activation is done by the caller. */
3849 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3851 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
3852 uint32_t rt_mask
, *cur_mask
;
3854 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
3856 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3857 rt_mask
= find_draw_buffers_mask(context_gl
, state
);
3858 if (rt_mask
!= *cur_mask
)
3860 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3861 *cur_mask
= rt_mask
;
3865 static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl
*context_gl
,
3866 const struct wined3d_state
*state
, enum wined3d_shader_type shader_type
)
3868 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3869 unsigned int bind_idx
, shader_sampler_count
, base
, count
, i
;
3870 const struct wined3d_device
*device
= context_gl
->c
.device
;
3871 struct wined3d_shader_sampler_map_entry
*entry
;
3872 struct wined3d_shader_resource_view
*view
;
3873 const struct wined3d_shader
*shader
;
3874 const unsigned int *tex_unit_map
;
3875 struct wined3d_sampler
*sampler
;
3877 if (!(shader
= state
->shader
[shader_type
]))
3879 if (shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
3881 uint32_t ffu_map
= context_gl
->c
.fixed_function_usage_map
;
3885 i
= wined3d_bit_scan(&ffu_map
);
3886 bind_idx
= context_gl
->tex_unit_map
[i
];
3888 view
= state
->shader_resource_view
[WINED3D_SHADER_TYPE_PIXEL
][i
];
3889 sampler
= state
->sampler
[WINED3D_SHADER_TYPE_PIXEL
][i
];
3893 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view
),
3894 bind_idx
, wined3d_sampler_gl(sampler
), context_gl
);
3898 WARN("No resource view bound at index %u.\n", i
);
3899 wined3d_context_gl_active_texture(context_gl
, gl_info
, bind_idx
);
3900 wined3d_context_gl_bind_texture(context_gl
, GL_NONE
, 0);
3901 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
3902 GL_EXTCALL(glBindSampler(bind_idx
, 0));
3910 if (gl_info
->supported
[ARB_BINDLESS_TEXTURE
])
3913 tex_unit_map
= wined3d_context_gl_get_tex_unit_mapping(context_gl
,
3914 &shader
->reg_maps
.shader_version
, &base
, &count
);
3916 shader_sampler_count
= shader
->reg_maps
.sampler_map
.count
;
3917 if (shader_sampler_count
> count
)
3918 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3919 shader
, shader_sampler_count
, count
);
3920 count
= min(shader_sampler_count
, count
);
3922 for (i
= 0; i
< count
; ++i
)
3924 entry
= &shader
->reg_maps
.sampler_map
.entries
[i
];
3925 bind_idx
= base
+ entry
->bind_idx
;
3927 bind_idx
= tex_unit_map
[bind_idx
];
3929 if ((view
= state
->shader_resource_view
[shader_type
][entry
->resource_idx
]))
3931 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
3932 sampler
= device
->default_sampler
;
3933 else if (!(sampler
= state
->sampler
[shader_type
][entry
->sampler_idx
]))
3934 sampler
= device
->null_sampler
;
3935 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view
),
3936 bind_idx
, wined3d_sampler_gl(sampler
), context_gl
);
3940 WARN("No resource view bound at index %u, %u.\n", shader_type
, entry
->resource_idx
);
3941 wined3d_context_gl_active_texture(context_gl
, gl_info
, bind_idx
);
3942 wined3d_context_gl_bind_texture(context_gl
, GL_NONE
, 0);
3943 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
3944 GL_EXTCALL(glBindSampler(bind_idx
, 0));
3949 static void wined3d_context_gl_bind_unordered_access_views(struct wined3d_context_gl
*context_gl
,
3950 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
3952 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3953 struct wined3d_unordered_access_view_gl
*view_gl
;
3954 const struct wined3d_format_gl
*format_gl
;
3955 GLuint texture_name
;
3962 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
3966 if (shader
->reg_maps
.uav_resource_info
[i
].type
)
3967 WARN("No unordered access view bound at index %u.\n", i
);
3968 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
3972 view_gl
= wined3d_unordered_access_view_gl(views
[i
]);
3973 if (view_gl
->gl_view
.name
)
3975 texture_name
= view_gl
->gl_view
.name
;
3978 else if (view_gl
->v
.resource
->type
!= WINED3D_RTYPE_BUFFER
)
3980 struct wined3d_texture_gl
*texture_gl
= wined3d_texture_gl(texture_from_resource(view_gl
->v
.resource
));
3981 texture_name
= wined3d_texture_gl_get_texture_name(texture_gl
, &context_gl
->c
, FALSE
);
3982 level
= view_gl
->v
.desc
.u
.texture
.level_idx
;
3986 FIXME("Unsupported buffer unordered access view.\n");
3987 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
3991 format_gl
= wined3d_format_gl(view_gl
->v
.format
);
3992 GL_EXTCALL(glBindImageTexture(i
, texture_name
, level
, GL_TRUE
, 0, GL_READ_WRITE
,
3993 format_gl
->internal
));
3995 if (view_gl
->counter_bo
.id
)
3996 GL_EXTCALL(glBindBufferRange(GL_ATOMIC_COUNTER_BUFFER
, i
, view_gl
->counter_bo
.id
,
3997 view_gl
->counter_bo
.b
.buffer_offset
, view_gl
->counter_bo
.size
));
3999 checkGLcall("Bind unordered access views");
4002 static bool is_resource_rtv_bound(const struct wined3d_state
*state
, const struct wined3d_resource
*resource
)
4006 if (state
->fb
.depth_stencil
&& state
->fb
.depth_stencil
->resource
== resource
)
4009 if (!resource
->rtv_bind_count_device
)
4012 for (i
= 0; i
< ARRAY_SIZE(state
->fb
.render_targets
); ++i
)
4014 if (state
->fb
.render_targets
[i
] && state
->fb
.render_targets
[i
]->resource
== resource
)
4021 static void context_gl_load_shader_resources(struct wined3d_context_gl
*context_gl
,
4022 const struct wined3d_state
*state
, unsigned int shader_mask
)
4024 struct wined3d_shader_sampler_map_entry
*entry
;
4025 struct wined3d_shader_resource_view_gl
*srv_gl
;
4026 struct wined3d_shader_resource_view
*view
;
4027 struct wined3d_sampler
*sampler
;
4028 struct wined3d_shader
*shader
;
4029 struct wined3d_buffer
*buffer
;
4032 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
4034 if (!(shader_mask
& (1u << i
)))
4037 if (!(shader
= state
->shader
[i
]))
4039 if (i
== WINED3D_SHADER_TYPE_PIXEL
)
4041 uint32_t ffu_map
= context_gl
->c
.fixed_function_usage_map
;
4045 i
= wined3d_bit_scan(&ffu_map
);
4047 view
= state
->shader_resource_view
[WINED3D_SHADER_TYPE_PIXEL
][i
];
4048 sampler
= state
->sampler
[WINED3D_SHADER_TYPE_PIXEL
][i
];
4051 wined3d_texture_load(texture_from_resource(view
->resource
),
4052 &context_gl
->c
, sampler
->desc
.srgb_decode
);
4059 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
4061 if (!state
->cb
[i
][j
].buffer
)
4064 buffer
= state
->cb
[i
][j
].buffer
;
4065 wined3d_buffer_load(buffer
, &context_gl
->c
, state
);
4066 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4067 if (!buffer
->bo_user
.valid
)
4068 device_invalidate_state(context_gl
->c
.device
, STATE_CONSTANT_BUFFER(i
));
4071 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
4073 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
4075 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
4078 if (is_resource_rtv_bound(state
, view
->resource
))
4079 context_gl
->c
.uses_fbo_attached_resources
= 1;
4081 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
4083 buffer
= buffer_from_resource(view
->resource
);
4084 wined3d_buffer_load(buffer
, &context_gl
->c
, state
);
4085 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4087 srv_gl
= wined3d_shader_resource_view_gl(view
);
4088 if (!srv_gl
->bo_user
.valid
)
4089 wined3d_shader_resource_view_gl_update(srv_gl
, context_gl
);
4095 if (entry
->sampler_idx
!= WINED3D_SAMPLER_DEFAULT
4096 && (sampler
= state
->sampler
[i
][entry
->sampler_idx
]))
4097 srgb
= sampler
->desc
.srgb_decode
;
4099 wined3d_texture_load(texture_from_resource(view
->resource
), &context_gl
->c
, srgb
);
4105 static void context_gl_load_unordered_access_resources(struct wined3d_context_gl
*context_gl
,
4106 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
4108 struct wined3d_unordered_access_view_gl
*uav_gl
;
4109 struct wined3d_unordered_access_view
*view
;
4110 struct wined3d_texture
*texture
;
4111 struct wined3d_buffer
*buffer
;
4114 context_gl
->c
.uses_uavs
= 0;
4119 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
4121 if (!(view
= views
[i
]))
4124 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
4126 buffer
= buffer_from_resource(view
->resource
);
4127 wined3d_buffer_acquire_bo_for_write(buffer
, &context_gl
->c
);
4128 wined3d_buffer_load_location(buffer
, &context_gl
->c
, WINED3D_LOCATION_BUFFER
);
4129 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_BUFFER
);
4130 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4132 uav_gl
= wined3d_unordered_access_view_gl(view
);
4133 if (!uav_gl
->bo_user
.valid
)
4134 wined3d_unordered_access_view_gl_update(uav_gl
, context_gl
);
4138 texture
= texture_from_resource(view
->resource
);
4139 wined3d_texture_load(texture
, &context_gl
->c
, FALSE
);
4140 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_TEXTURE_RGB
);
4143 context_gl
->c
.uses_uavs
= 1;
4147 static void context_gl_load_stream_output_buffers(struct wined3d_context_gl
*context_gl
,
4148 const struct wined3d_state
*state
)
4150 struct wined3d_buffer
*buffer
;
4153 for (i
= 0; i
< ARRAY_SIZE(state
->stream_output
); ++i
)
4155 if (!(buffer
= state
->stream_output
[i
].buffer
))
4158 wined3d_buffer_acquire_bo_for_write(buffer
, &context_gl
->c
);
4160 wined3d_buffer_load(buffer
, &context_gl
->c
, state
);
4161 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
4162 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4163 if (!buffer
->bo_user
.valid
)
4164 device_invalidate_state(context_gl
->c
.device
, STATE_STREAM_OUTPUT
);
4168 /* Context activation is done by the caller. */
4169 static BOOL
context_apply_draw_state(struct wined3d_context
*context
,
4170 const struct wined3d_device
*device
, const struct wined3d_state
*state
, BOOL indexed
)
4172 const struct wined3d_state_entry
*state_table
= context
->state_table
;
4173 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
4174 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4175 const struct wined3d_fb_state
*fb
= &state
->fb
;
4176 unsigned int i
, base
;
4179 context
->uses_fbo_attached_resources
= 0;
4181 if (!have_framebuffer_attachment(gl_info
->limits
.buffers
, fb
->render_targets
, fb
->depth_stencil
))
4183 if (!gl_info
->supported
[ARB_FRAMEBUFFER_NO_ATTACHMENTS
])
4185 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
4190 /* Preload resources before FBO setup. Texture preload in particular may
4191 * result in changes to the current FBO, due to using e.g. FBO blits for
4192 * updating a resource location. */
4193 wined3d_context_gl_update_tex_unit_map(context_gl
, state
);
4194 context_gl_load_shader_resources(context_gl
, state
, ~(1u << WINED3D_SHADER_TYPE_COMPUTE
));
4195 context_gl_load_unordered_access_resources(context_gl
, state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
4196 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
4197 context_gl_load_stream_output_buffers(context_gl
, state
);
4198 /* TODO: Right now the dependency on the vertex shader is necessary
4199 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
4200 * the current VS but maybe it's possible to relax the coupling in some
4201 * situations at least. */
4202 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
4203 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
4205 context_update_stream_info(context
, state
);
4208 map
= context
->stream_info
.use_map
;
4211 const struct wined3d_stream_info_element
*e
;
4212 struct wined3d_buffer
*buffer
;
4214 e
= &context
->stream_info
.elements
[wined3d_bit_scan(&map
)];
4215 buffer
= state
->streams
[e
->stream_idx
].buffer
;
4217 if (!buffer
->bo_user
.valid
)
4218 device_invalidate_state(device
, STATE_STREAMSRC
);
4220 wined3d_buffer_load(buffer
, context
, state
);
4222 /* Loading the buffers above may have invalidated the stream info. */
4223 if (wined3d_context_is_graphics_state_dirty(context
, STATE_STREAMSRC
))
4224 context_update_stream_info(context
, state
);
4226 map
= context
->stream_info
.use_map
;
4229 const struct wined3d_stream_info_element
*e
;
4230 struct wined3d_buffer
*buffer
;
4232 e
= &context
->stream_info
.elements
[wined3d_bit_scan(&map
)];
4233 buffer
= state
->streams
[e
->stream_idx
].buffer
;
4235 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4238 if (indexed
&& state
->index_buffer
)
4240 struct wined3d_buffer
*buffer
= state
->index_buffer
;
4242 if (context
->stream_info
.all_vbo
)
4244 wined3d_buffer_load(buffer
, context
, state
);
4245 if (!buffer
->bo_user
.valid
)
4246 device_invalidate_state(device
, STATE_INDEXBUFFER
);
4247 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4251 wined3d_buffer_load_sysmem(buffer
, context
);
4255 for (i
= 0, base
= 0; i
< ARRAY_SIZE(context
->dirty_graphics_states
); ++i
)
4257 uint32_t dirty_mask
= context
->dirty_graphics_states
[i
];
4261 unsigned int state_id
= base
+ wined3d_bit_scan(&dirty_mask
);
4263 state_table
[state_id
].apply(context
, state
, state_id
);
4265 base
+= sizeof(dirty_mask
) * CHAR_BIT
;
4268 memset(context
->dirty_graphics_states
, 0, sizeof(context
->dirty_graphics_states
));
4270 if (context
->update_shader_resource_bindings
)
4272 for (i
= 0; i
< WINED3D_SHADER_TYPE_GRAPHICS_COUNT
; ++i
)
4273 wined3d_context_gl_bind_shader_resources(context_gl
, state
, i
);
4274 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
4275 context
->update_compute_shader_resource_bindings
= 1;
4278 if (context
->update_unordered_access_view_bindings
)
4280 wined3d_context_gl_bind_unordered_access_views(context_gl
,
4281 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
4282 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
4283 context
->update_unordered_access_view_bindings
= 0;
4284 context
->update_compute_unordered_access_view_bindings
= 1;
4287 wined3d_context_gl_check_fbo_status(context_gl
, GL_FRAMEBUFFER
);
4289 device
->shader_backend
->shader_apply_draw_state(device
->shader_priv
, context
, state
);
4290 context
->shader_update_mask
&= 1u << WINED3D_SHADER_TYPE_COMPUTE
;
4291 context
->constant_update_mask
= 0;
4292 context
->update_shader_resource_bindings
= 0;
4294 context
->last_was_blit
= FALSE
;
4295 context
->last_was_ffp_blit
= FALSE
;
4300 static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl
*context_gl
,
4301 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
4303 const struct wined3d_state_entry
*state_table
= context_gl
->c
.state_table
;
4304 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4305 unsigned int state_id
, i
;
4307 context_gl_load_shader_resources(context_gl
, state
, 1u << WINED3D_SHADER_TYPE_COMPUTE
);
4308 context_gl_load_unordered_access_resources(context_gl
, state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
4309 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
4311 for (i
= 0, state_id
= STATE_COMPUTE_OFFSET
; i
< ARRAY_SIZE(context_gl
->c
.dirty_compute_states
); ++i
)
4313 unsigned int dirty_mask
= context_gl
->c
.dirty_compute_states
[i
];
4317 unsigned int current_state_id
= state_id
+ wined3d_bit_scan(&dirty_mask
);
4318 state_table
[current_state_id
].apply(&context_gl
->c
, state
, current_state_id
);
4320 state_id
+= sizeof(*context_gl
->c
.dirty_compute_states
) * CHAR_BIT
;
4322 memset(context_gl
->c
.dirty_compute_states
, 0, sizeof(*context_gl
->c
.dirty_compute_states
));
4324 device
->shader_backend
->shader_apply_compute_state(device
->shader_priv
, &context_gl
->c
, state
);
4326 if (context_gl
->c
.update_compute_shader_resource_bindings
)
4328 wined3d_context_gl_bind_shader_resources(context_gl
, state
, WINED3D_SHADER_TYPE_COMPUTE
);
4329 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
4330 context_gl
->c
.update_shader_resource_bindings
= 1;
4333 if (context_gl
->c
.update_compute_unordered_access_view_bindings
)
4335 wined3d_context_gl_bind_unordered_access_views(context_gl
,
4336 state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
4337 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
4338 context_gl
->c
.update_compute_unordered_access_view_bindings
= 0;
4339 context_gl
->c
.update_unordered_access_view_bindings
= 1;
4342 /* Updates to currently bound render targets aren't necessarily coherent
4343 * between the graphics and compute pipelines. Unbind any currently bound
4344 * FBO here to ensure preceding updates to its attachments by the graphics
4345 * pipeline are visible to the compute pipeline.
4347 * Without this, the bloom effect in Nier:Automata is too bright on the
4348 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4349 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
4350 context_invalidate_state(&context_gl
->c
, STATE_FRAMEBUFFER
);
4352 context_gl
->c
.last_was_blit
= FALSE
;
4353 context_gl
->c
.last_was_ffp_blit
= FALSE
;
4354 context_gl
->c
.shader_update_mask
&= ~(1u << WINED3D_SHADER_TYPE_COMPUTE
);
4355 context_gl
->c
.update_compute_shader_resource_bindings
= 0;
4358 void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl
*context_gl
)
4360 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4362 if (context_gl
->c
.transform_feedback_active
)
4364 GL_EXTCALL(glEndTransformFeedback());
4365 checkGLcall("glEndTransformFeedback");
4366 context_gl
->c
.transform_feedback_active
= 0;
4367 context_gl
->c
.transform_feedback_paused
= 0;
4371 static void wined3d_context_gl_pause_transform_feedback(struct wined3d_context_gl
*context_gl
, BOOL force
)
4373 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4375 if (!context_gl
->c
.transform_feedback_active
|| context_gl
->c
.transform_feedback_paused
)
4378 if (gl_info
->supported
[ARB_TRANSFORM_FEEDBACK2
])
4380 GL_EXTCALL(glPauseTransformFeedback());
4381 checkGLcall("glPauseTransformFeedback");
4382 context_gl
->c
.transform_feedback_paused
= 1;
4386 WARN("Cannot pause transform feedback operations.\n");
4389 wined3d_context_gl_end_transform_feedback(context_gl
);
4392 static void wined3d_context_gl_setup_target(struct wined3d_context_gl
*context_gl
,
4393 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4395 if (context_gl
->c
.current_rt
.texture
== texture
4396 && context_gl
->c
.current_rt
.sub_resource_idx
== sub_resource_idx
)
4399 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4400 * the alpha blend state changes with different render target formats. */
4401 if (!context_gl
->c
.current_rt
.texture
)
4403 context_invalidate_state(&context_gl
->c
, STATE_BLEND
);
4407 const struct wined3d_format
*old
= context_gl
->c
.current_rt
.texture
->resource
.format
;
4408 const struct wined3d_format
*new = texture
->resource
.format
;
4410 if (old
->id
!= new->id
)
4412 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4413 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
4414 || !(texture
->resource
.format_caps
& WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING
))
4415 context_invalidate_state(&context_gl
->c
, STATE_BLEND
);
4419 context_gl
->c
.current_rt
.texture
= texture
;
4420 context_gl
->c
.current_rt
.sub_resource_idx
= sub_resource_idx
;
4423 static void wined3d_context_gl_activate(struct wined3d_context_gl
*context_gl
,
4424 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4426 wined3d_context_gl_enter(context_gl
);
4427 if (texture
&& texture
->swapchain
&& texture
->swapchain
!= context_gl
->c
.swapchain
)
4429 TRACE("Switching context_gl %p from swapchain %p to swapchain %p.\n",
4430 context_gl
, context_gl
->c
.swapchain
, texture
->swapchain
);
4431 context_gl
->c
.swapchain
= texture
->swapchain
;
4433 wined3d_context_gl_update_window(context_gl
);
4434 wined3d_context_gl_setup_target(context_gl
, texture
, sub_resource_idx
);
4435 if (!context_gl
->valid
)
4438 if (context_gl
!= wined3d_context_gl_get_current())
4440 if (!wined3d_context_gl_set_current(context_gl
))
4441 ERR("Failed to activate the new context.\n");
4443 else if (context_gl
->needs_set
)
4445 wined3d_context_gl_set_gl_context(context_gl
);
4449 struct wined3d_context
*wined3d_context_gl_acquire(const struct wined3d_device
*device
,
4450 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4452 struct wined3d_context_gl
*current_context
= wined3d_context_gl_get_current();
4453 struct wined3d_context_gl
*context_gl
;
4455 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device
, texture
, sub_resource_idx
);
4457 if (current_context
&& current_context
->c
.destroyed
)
4458 current_context
= NULL
;
4463 && current_context
->c
.current_rt
.texture
4464 && current_context
->c
.device
== device
)
4466 texture
= current_context
->c
.current_rt
.texture
;
4467 sub_resource_idx
= current_context
->c
.current_rt
.sub_resource_idx
;
4471 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
4473 if (swapchain
->back_buffers
)
4474 texture
= swapchain
->back_buffers
[0];
4476 texture
= swapchain
->front_buffer
;
4477 sub_resource_idx
= 0;
4481 if (current_context
&& current_context
->c
.current_rt
.texture
== texture
)
4483 context_gl
= current_context
;
4485 else if (!wined3d_resource_is_offscreen(&texture
->resource
))
4487 TRACE("Rendering onscreen.\n");
4489 if (!(context_gl
= wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(texture
->swapchain
))))
4494 TRACE("Rendering offscreen.\n");
4496 /* Stay with the current context if possible. Otherwise use the
4497 * context for the primary swapchain. */
4498 if (current_context
&& current_context
->c
.device
== device
)
4499 context_gl
= current_context
;
4500 else if (!(context_gl
= wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(device
->swapchains
[0]))))
4504 wined3d_context_gl_activate(context_gl
, texture
, sub_resource_idx
);
4506 return &context_gl
->c
;
4509 struct wined3d_context_gl
*wined3d_context_gl_reacquire(struct wined3d_context_gl
*context_gl
)
4511 struct wined3d_context
*acquired_context
;
4512 struct wined3d_device
*device
;
4514 if (!context_gl
|| context_gl
->tid
!= GetCurrentThreadId())
4517 device
= context_gl
->c
.device
;
4518 wined3d_from_cs(device
->cs
);
4520 if (context_gl
->c
.current_rt
.texture
)
4522 wined3d_context_gl_activate(context_gl
, context_gl
->c
.current_rt
.texture
,
4523 context_gl
->c
.current_rt
.sub_resource_idx
);
4527 acquired_context
= context_acquire(device
, NULL
, 0);
4528 if (acquired_context
!= &context_gl
->c
)
4529 ERR("Acquired context %p instead of %p.\n", acquired_context
, &context_gl
->c
);
4530 return wined3d_context_gl(acquired_context
);
4533 void dispatch_compute(struct wined3d_device
*device
, const struct wined3d_state
*state
,
4534 const struct wined3d_dispatch_parameters
*parameters
)
4536 const struct wined3d_gl_info
*gl_info
;
4537 struct wined3d_context_gl
*context_gl
;
4539 context_gl
= wined3d_context_gl(context_acquire(device
, NULL
, 0));
4540 if (!context_gl
->valid
)
4542 context_release(&context_gl
->c
);
4543 WARN("Invalid context, skipping dispatch.\n");
4546 gl_info
= context_gl
->gl_info
;
4548 if (!gl_info
->supported
[ARB_COMPUTE_SHADER
])
4550 context_release(&context_gl
->c
);
4551 FIXME("OpenGL implementation does not support compute shaders.\n");
4555 if (parameters
->indirect
)
4556 wined3d_buffer_load(parameters
->u
.indirect
.buffer
, &context_gl
->c
, state
);
4558 wined3d_context_gl_apply_compute_state(context_gl
, device
, state
);
4560 if (parameters
->indirect
)
4562 const struct wined3d_indirect_dispatch_parameters
*indirect
= ¶meters
->u
.indirect
;
4563 struct wined3d_bo_gl
*bo_gl
= wined3d_bo_gl(indirect
->buffer
->buffer_object
);
4565 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, bo_gl
->id
));
4566 GL_EXTCALL(glDispatchComputeIndirect(bo_gl
->b
.buffer_offset
+ (GLintptr
)indirect
->offset
));
4567 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0));
4568 wined3d_context_gl_reference_bo(context_gl
, bo_gl
);
4572 const struct wined3d_direct_dispatch_parameters
*direct
= ¶meters
->u
.direct
;
4573 GL_EXTCALL(glDispatchCompute(direct
->group_count_x
, direct
->group_count_y
, direct
->group_count_z
));
4575 checkGLcall("dispatch compute");
4577 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS
));
4578 checkGLcall("glMemoryBarrier");
4580 context_release(&context_gl
->c
);
4583 /* Context activation is done by the caller. */
4584 static void wined3d_context_gl_draw_primitive_arrays(struct wined3d_context_gl
*context_gl
,
4585 const struct wined3d_state
*state
, const void *idx_data
, unsigned int idx_size
, int base_vertex_idx
,
4586 unsigned int start_idx
, unsigned int count
, unsigned int start_instance
, unsigned int instance_count
)
4588 GLenum idx_type
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
4589 const struct wined3d_stream_info
*si
= &context_gl
->c
.stream_info
;
4590 GLenum mode
= gl_primitive_type_from_d3d(state
->primitive_type
);
4591 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4592 unsigned int instanced_elements
[ARRAY_SIZE(si
->elements
)];
4593 const struct wined3d_ffp_attrib_ops
*ops
;
4594 unsigned int instanced_element_count
= 0;
4595 const void *indices
;
4598 ops
= &gl_info
->ffp_attrib_ops
;
4600 indices
= (const char *)idx_data
+ idx_size
* start_idx
;
4602 if (!instance_count
)
4606 gl_info
->gl_ops
.gl
.p_glDrawArrays(mode
, start_idx
, count
);
4607 checkGLcall("glDrawArrays");
4611 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4613 GL_EXTCALL(glDrawElementsBaseVertex(mode
, count
, idx_type
, indices
, base_vertex_idx
));
4614 checkGLcall("glDrawElementsBaseVertex");
4618 gl_info
->gl_ops
.gl
.p_glDrawElements(mode
, count
, idx_type
, indices
);
4619 checkGLcall("glDrawElements");
4623 if (start_instance
&& !(gl_info
->supported
[ARB_BASE_INSTANCE
] && gl_info
->supported
[ARB_INSTANCED_ARRAYS
]))
4624 FIXME("Start instance (%u) not supported.\n", start_instance
);
4626 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
4630 if (gl_info
->supported
[ARB_BASE_INSTANCE
])
4632 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode
, start_idx
, count
, instance_count
, start_instance
));
4633 checkGLcall("glDrawArraysInstancedBaseInstance");
4637 GL_EXTCALL(glDrawArraysInstanced(mode
, start_idx
, count
, instance_count
));
4638 checkGLcall("glDrawArraysInstanced");
4642 if (gl_info
->supported
[ARB_BASE_INSTANCE
])
4644 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode
, count
, idx_type
,
4645 indices
, instance_count
, base_vertex_idx
, start_instance
));
4646 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4649 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4651 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode
, count
, idx_type
,
4652 indices
, instance_count
, base_vertex_idx
));
4653 checkGLcall("glDrawElementsInstancedBaseVertex");
4657 GL_EXTCALL(glDrawElementsInstanced(mode
, count
, idx_type
, indices
, instance_count
));
4658 checkGLcall("glDrawElementsInstanced");
4662 /* Instancing emulation by mixing immediate mode and arrays. */
4664 /* This is a nasty thing. MSDN says no hardware supports this and
4665 * applications have to use software vertex processing. We don't support
4668 * Shouldn't be too hard to support with OpenGL, in theory just call
4669 * glDrawArrays() instead of drawElements(). But the stream frequency value
4670 * has a different meaning in that situation. */
4673 FIXME("Non-indexed instanced drawing is not supported.\n");
4677 for (i
= 0; i
< ARRAY_SIZE(si
->elements
); ++i
)
4679 if (!(si
->use_map
& (1u << i
)))
4682 if (state
->streams
[si
->elements
[i
].stream_idx
].flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
4683 instanced_elements
[instanced_element_count
++] = i
;
4686 for (i
= 0; i
< instance_count
; ++i
)
4688 /* Specify the instanced attributes using immediate mode calls. */
4689 for (j
= 0; j
< instanced_element_count
; ++j
)
4691 const struct wined3d_stream_info_element
*element
;
4692 unsigned int element_idx
;
4695 element_idx
= instanced_elements
[j
];
4696 element
= &si
->elements
[element_idx
];
4697 ptr
= element
->data
.addr
+ element
->stride
* i
;
4698 if (element
->data
.buffer_object
)
4699 ptr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(state
->streams
[element
->stream_idx
].buffer
,
4701 ops
->generic
[element
->format
->emit_idx
](element_idx
, ptr
);
4704 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4706 GL_EXTCALL(glDrawElementsBaseVertex(mode
, count
, idx_type
, indices
, base_vertex_idx
));
4707 checkGLcall("glDrawElementsBaseVertex");
4711 gl_info
->gl_ops
.gl
.p_glDrawElements(mode
, count
, idx_type
, indices
);
4712 checkGLcall("glDrawElements");
4717 static unsigned int get_stride_idx(const void *idx_data
, unsigned int idx_size
,
4718 unsigned int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_idx
)
4721 return start_idx
+ vertex_idx
;
4723 return ((const WORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
4724 return ((const DWORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
4727 /* Context activation is done by the caller. */
4728 static void draw_primitive_immediate_mode(struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
,
4729 const struct wined3d_stream_info
*si
, const void *idx_data
, unsigned int idx_size
,
4730 int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_count
, unsigned int instance_count
)
4732 const BYTE
*position
= NULL
, *normal
= NULL
, *diffuse
= NULL
, *specular
= NULL
;
4733 const struct wined3d_d3d_info
*d3d_info
= context_gl
->c
.d3d_info
;
4734 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4735 unsigned int coord_idx
, stride_idx
, texture_idx
, vertex_idx
;
4736 const struct wined3d_stream_info_element
*element
;
4737 const BYTE
*tex_coords
[WINED3DDP_MAXTEXCOORD
];
4738 unsigned int texture_unit
, texture_stages
;
4739 const struct wined3d_ffp_attrib_ops
*ops
;
4740 unsigned int untracked_material_count
;
4741 unsigned int tex_mask
= 0;
4742 BOOL specular_fog
= FALSE
;
4743 BOOL ps
= use_ps(state
);
4746 static unsigned int once
;
4749 FIXME_(d3d_perf
)("Drawing using immediate mode.\n");
4751 WARN_(d3d_perf
)("Drawing using immediate mode.\n");
4753 if (!idx_size
&& idx_data
)
4754 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4757 FIXME("Instancing not implemented.\n");
4759 /* Immediate mode drawing can't make use of indices in a VBO - get the
4760 * data from the index buffer. */
4762 idx_data
= (uint8_t *)wined3d_buffer_load_sysmem(state
->index_buffer
, &context_gl
->c
) + state
->index_offset
;
4764 ops
= &gl_info
->ffp_attrib_ops
;
4766 gl_info
->gl_ops
.gl
.p_glBegin(gl_primitive_type_from_d3d(state
->primitive_type
));
4768 if (use_vs(state
) || d3d_info
->ffp_generic_attributes
)
4770 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
4772 unsigned int use_map
= si
->use_map
;
4773 unsigned int element_idx
;
4775 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
4776 for (element_idx
= gl_info
->limits
.vertex_attribs
- 1; use_map
;
4777 use_map
&= ~(1u << element_idx
), --element_idx
)
4779 if (!(use_map
& 1u << element_idx
))
4782 ptr
= si
->elements
[element_idx
].data
.addr
+ si
->elements
[element_idx
].stride
* stride_idx
;
4783 ops
->generic
[si
->elements
[element_idx
].format
->emit_idx
](element_idx
, ptr
);
4787 gl_info
->gl_ops
.gl
.p_glEnd();
4791 if (si
->use_map
& (1u << WINED3D_FFP_POSITION
))
4792 position
= si
->elements
[WINED3D_FFP_POSITION
].data
.addr
;
4794 if (si
->use_map
& (1u << WINED3D_FFP_NORMAL
))
4795 normal
= si
->elements
[WINED3D_FFP_NORMAL
].data
.addr
;
4797 gl_info
->gl_ops
.gl
.p_glNormal3f(0.0f
, 0.0f
, 0.0f
);
4799 untracked_material_count
= context_gl
->untracked_material_count
;
4800 if (si
->use_map
& (1u << WINED3D_FFP_DIFFUSE
))
4802 element
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
4803 diffuse
= element
->data
.addr
;
4805 if (untracked_material_count
&& element
->format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
4806 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element
->format
->id
));
4810 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
4813 if (si
->use_map
& (1u << WINED3D_FFP_SPECULAR
))
4815 element
= &si
->elements
[WINED3D_FFP_SPECULAR
];
4816 specular
= element
->data
.addr
;
4818 /* Special case where the fog density is stored in the specular alpha channel. */
4819 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
4820 && (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
4821 || si
->elements
[WINED3D_FFP_POSITION
].format
->id
== WINED3DFMT_R32G32B32A32_FLOAT
)
4822 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
4824 if (gl_info
->supported
[EXT_FOG_COORD
])
4826 if (element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
4827 specular_fog
= TRUE
;
4829 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element
->format
->id
));
4833 static unsigned int once
;
4836 FIXME("Implement fog for transformed vertices in software.\n");
4840 else if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
4842 GL_EXTCALL(glSecondaryColor3fEXT
)(0.0f
, 0.0f
, 0.0f
);
4845 texture_stages
= d3d_info
->ffp_fragment_caps
.max_blend_stages
;
4846 for (texture_idx
= 0; texture_idx
< texture_stages
; ++texture_idx
)
4848 if (!gl_info
->supported
[ARB_MULTITEXTURE
] && texture_idx
> 0)
4850 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4854 if (!ps
&& !wined3d_state_get_ffp_texture(state
, texture_idx
))
4857 texture_unit
= context_gl
->tex_unit_map
[texture_idx
];
4858 if (texture_unit
== WINED3D_UNMAPPED_STAGE
)
4861 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
4864 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx
, texture_idx
);
4868 if (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
)))
4870 tex_coords
[coord_idx
] = si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].data
.addr
;
4871 tex_mask
|= (1u << texture_idx
);
4875 TRACE("Setting default coordinates for texture %u.\n", texture_idx
);
4876 if (gl_info
->supported
[ARB_MULTITEXTURE
])
4877 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_unit
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
4879 gl_info
->gl_ops
.gl
.p_glTexCoord4f(0.0f
, 0.0f
, 0.0f
, 1.0f
);
4883 /* Blending data and point sizes are not supported by this function. They
4884 * are not supported by the fixed function pipeline at all. A FIXME for
4885 * them is printed after decoding the vertex declaration. */
4886 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
4888 uint32_t tmp_tex_mask
;
4890 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
4894 ptr
= normal
+ stride_idx
* si
->elements
[WINED3D_FFP_NORMAL
].stride
;
4895 ops
->normal
[si
->elements
[WINED3D_FFP_NORMAL
].format
->emit_idx
](ptr
);
4900 ptr
= diffuse
+ stride_idx
* si
->elements
[WINED3D_FFP_DIFFUSE
].stride
;
4901 ops
->diffuse
[si
->elements
[WINED3D_FFP_DIFFUSE
].format
->emit_idx
](ptr
);
4903 if (untracked_material_count
)
4905 struct wined3d_color color
;
4908 wined3d_color_from_d3dcolor(&color
, *(const DWORD
*)ptr
);
4909 for (i
= 0; i
< untracked_material_count
; ++i
)
4911 gl_info
->gl_ops
.gl
.p_glMaterialfv(GL_FRONT_AND_BACK
,
4912 context_gl
->untracked_materials
[i
], &color
.r
);
4919 ptr
= specular
+ stride_idx
* si
->elements
[WINED3D_FFP_SPECULAR
].stride
;
4920 ops
->specular
[si
->elements
[WINED3D_FFP_SPECULAR
].format
->emit_idx
](ptr
);
4923 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD
*)ptr
>> 24)));
4926 tmp_tex_mask
= tex_mask
;
4927 while (tmp_tex_mask
)
4929 texture_idx
= wined3d_bit_scan(&tmp_tex_mask
);
4930 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
4931 ptr
= tex_coords
[coord_idx
] + (stride_idx
* si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].stride
);
4932 ops
->texcoord
[si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].format
->emit_idx
](
4933 GL_TEXTURE0_ARB
+ context_gl
->tex_unit_map
[texture_idx
], ptr
);
4938 ptr
= position
+ stride_idx
* si
->elements
[WINED3D_FFP_POSITION
].stride
;
4939 ops
->position
[si
->elements
[WINED3D_FFP_POSITION
].format
->emit_idx
](ptr
);
4943 gl_info
->gl_ops
.gl
.p_glEnd();
4944 checkGLcall("draw immediate mode");
4947 static void wined3d_context_gl_draw_indirect(struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
,
4948 const struct wined3d_indirect_draw_parameters
*parameters
, unsigned int idx_size
)
4950 struct wined3d_bo_gl
*bo_gl
= wined3d_bo_gl(parameters
->buffer
->buffer_object
);
4951 GLenum gl_primitive_type
= gl_primitive_type_from_d3d(state
->primitive_type
);
4952 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4955 if (!gl_info
->supported
[ARB_DRAW_INDIRECT
])
4957 FIXME("OpenGL implementation does not support indirect draws.\n");
4961 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER
, bo_gl
->id
));
4963 offset
= (const uint8_t *)bo_gl
->b
.buffer_offset
+ parameters
->offset
;
4966 GLenum idx_type
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
4967 if (state
->index_offset
)
4968 FIXME("Ignoring index offset %u.\n", state
->index_offset
);
4969 GL_EXTCALL(glDrawElementsIndirect(gl_primitive_type
, idx_type
, offset
));
4973 GL_EXTCALL(glDrawArraysIndirect(gl_primitive_type
, offset
));
4976 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER
, 0));
4977 wined3d_context_gl_reference_bo(context_gl
, bo_gl
);
4979 checkGLcall("draw indirect");
4982 static void remove_vbos(struct wined3d_context
*context
,
4983 const struct wined3d_state
*state
, struct wined3d_stream_info
*s
)
4987 for (i
= 0; i
< ARRAY_SIZE(s
->elements
); ++i
)
4989 struct wined3d_stream_info_element
*e
;
4991 if (!(s
->use_map
& (1u << i
)))
4994 e
= &s
->elements
[i
];
4995 if (e
->data
.buffer_object
)
4997 struct wined3d_buffer
*vb
= state
->streams
[e
->stream_idx
].buffer
;
4998 e
->data
.buffer_object
= 0;
4999 e
->data
.addr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(vb
, context
);
5004 static GLenum
gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
5006 GLenum gl_primitive_type
= gl_primitive_type_from_d3d(primitive_type
);
5007 switch (gl_primitive_type
)
5013 case GL_LINE_STRIP_ADJACENCY
:
5014 case GL_LINES_ADJACENCY
:
5018 case GL_TRIANGLE_FAN
:
5019 case GL_TRIANGLE_STRIP
:
5020 case GL_TRIANGLE_STRIP_ADJACENCY
:
5021 case GL_TRIANGLES_ADJACENCY
:
5023 return GL_TRIANGLES
;
5026 return gl_primitive_type
;
5030 /* Routine common to the draw primitive and draw indexed primitive routines */
5031 void draw_primitive(struct wined3d_device
*device
, const struct wined3d_state
*state
,
5032 const struct wined3d_draw_parameters
*parameters
)
5034 BOOL emulation
= FALSE
, rasterizer_discard
= FALSE
;
5035 const struct wined3d_fb_state
*fb
= &state
->fb
;
5036 const struct wined3d_stream_info
*stream_info
;
5037 struct wined3d_rendertarget_view
*dsv
, *rtv
;
5038 struct wined3d_stream_info si_emulated
;
5039 const struct wined3d_gl_info
*gl_info
;
5040 struct wined3d_context_gl
*context_gl
;
5041 struct wined3d_context
*context
;
5042 unsigned int i
, idx_size
= 0;
5043 const void *idx_data
= NULL
;
5045 TRACE("device %p, state %p, parameters %p.\n", device
, state
, parameters
);
5047 if (!parameters
->indirect
&& !parameters
->u
.direct
.index_count
)
5050 if (!parameters
->indirect
)
5051 TRACE("base_vertex_idx %d, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
5052 parameters
->u
.direct
.base_vertex_idx
, parameters
->u
.direct
.start_idx
,
5053 parameters
->u
.direct
.index_count
, parameters
->u
.direct
.start_instance
,
5054 parameters
->u
.direct
.instance_count
);
5056 if (!(rtv
= fb
->render_targets
[0]))
5057 rtv
= fb
->depth_stencil
;
5059 if (rtv
&& rtv
->resource
->type
== WINED3D_RTYPE_BUFFER
)
5061 FIXME("Buffer render targets not implemented.\n");
5066 context
= context_acquire(device
, wined3d_texture_from_resource(rtv
->resource
), rtv
->sub_resource_idx
);
5068 context
= context_acquire(device
, NULL
, 0);
5069 context_gl
= wined3d_context_gl(context
);
5070 if (!context_gl
->valid
)
5072 context_release(context
);
5073 WARN("Invalid context, skipping draw.\n");
5076 gl_info
= context_gl
->gl_info
;
5078 if (!use_transform_feedback(state
))
5079 wined3d_context_gl_pause_transform_feedback(context_gl
, TRUE
);
5081 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
5083 if (!(rtv
= fb
->render_targets
[i
]) || rtv
->format
->id
== WINED3DFMT_NULL
)
5086 if (wined3d_blend_state_get_writemask(state
->blend_state
, i
))
5088 wined3d_rendertarget_view_load_location(rtv
, context
, rtv
->resource
->draw_binding
);
5089 wined3d_rendertarget_view_invalidate_location(rtv
, ~rtv
->resource
->draw_binding
);
5093 wined3d_rendertarget_view_prepare_location(rtv
, context
, rtv
->resource
->draw_binding
);
5097 if ((dsv
= fb
->depth_stencil
))
5099 /* We don't currently take the Z-compare function into account,
5100 * but we could skip loading the depthstencil for D3DCMP_NEVER and
5101 * D3DCMP_ALWAYS as well.
5102 * Also note that we never copy the stencil data. */
5103 uint32_t location
= dsv
->resource
->draw_binding
;
5105 if (wined3d_state_uses_depth_buffer(state
))
5106 wined3d_rendertarget_view_load_location(dsv
, context
, location
);
5108 wined3d_rendertarget_view_prepare_location(dsv
, context
, location
);
5111 if (parameters
->indirect
)
5112 wined3d_buffer_load(parameters
->u
.indirect
.buffer
, context
, state
);
5114 if (!context_apply_draw_state(context
, device
, state
, parameters
->indexed
))
5116 context_release(context
);
5117 WARN("Unable to apply draw state, skipping draw.\n");
5121 if (dsv
&& (!state
->depth_stencil_state
|| state
->depth_stencil_state
->writes_ds
))
5123 uint32_t location
= dsv
->resource
->draw_binding
;
5125 wined3d_rendertarget_view_validate_location(dsv
, location
);
5126 wined3d_rendertarget_view_invalidate_location(dsv
, ~location
);
5129 stream_info
= &context
->stream_info
;
5131 if (parameters
->indexed
)
5133 struct wined3d_buffer
*index_buffer
= state
->index_buffer
;
5134 struct wined3d_bo
*bo
= index_buffer
->buffer_object
;
5136 if (!bo
|| !stream_info
->all_vbo
)
5137 idx_data
= index_buffer
->resource
.heap_memory
;
5139 idx_data
= (void *)bo
->buffer_offset
;
5140 idx_data
= (const BYTE
*)idx_data
+ state
->index_offset
;
5142 if (state
->index_format
== WINED3DFMT_R16_UINT
)
5150 if (!stream_info
->position_transformed
&& context_gl
->untracked_material_count
5151 && state
->render_states
[WINED3D_RS_LIGHTING
])
5156 FIXME("Using software emulation because not all material properties could be tracked.\n");
5158 WARN_(d3d_perf
)("Using software emulation because not all material properties could be tracked.\n");
5161 else if (context
->fog_coord
&& state
->render_states
[WINED3D_RS_FOGENABLE
])
5165 /* Either write a pipeline replacement shader or convert the
5166 * specular alpha from unsigned byte to a float in the vertex
5169 FIXME("Using software emulation because manual fog coordinates are provided.\n");
5171 WARN_(d3d_perf
)("Using software emulation because manual fog coordinates are provided.\n");
5177 si_emulated
= context
->stream_info
;
5178 remove_vbos(context
, state
, &si_emulated
);
5179 stream_info
= &si_emulated
;
5183 if (use_transform_feedback(state
))
5185 const struct wined3d_shader
*shader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
5187 if (is_rasterization_disabled(shader
))
5189 glEnable(GL_RASTERIZER_DISCARD
);
5190 checkGLcall("enable rasterizer discard");
5191 rasterizer_discard
= TRUE
;
5194 if (context
->transform_feedback_paused
)
5196 GL_EXTCALL(glResumeTransformFeedback());
5197 checkGLcall("glResumeTransformFeedback");
5198 context
->transform_feedback_paused
= 0;
5200 else if (!context
->transform_feedback_active
)
5202 enum wined3d_primitive_type primitive_type
= shader
->u
.gs
.output_type
5203 ? shader
->u
.gs
.output_type
: state
->primitive_type
;
5204 GLenum mode
= gl_tfb_primitive_type_from_d3d(primitive_type
);
5205 GL_EXTCALL(glBeginTransformFeedback(mode
));
5206 checkGLcall("glBeginTransformFeedback");
5207 context
->transform_feedback_active
= 1;
5211 if (state
->primitive_type
== WINED3D_PT_PATCH
)
5213 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES
, state
->patch_vertex_count
));
5214 checkGLcall("glPatchParameteri");
5217 if (context
->uses_fbo_attached_resources
)
5219 static unsigned int fixme_once
;
5221 if (gl_info
->supported
[ARB_TEXTURE_BARRIER
])
5223 GL_EXTCALL(glTextureBarrier());
5225 else if (gl_info
->supported
[NV_TEXTURE_BARRIER
])
5227 GL_EXTCALL(glTextureBarrierNV());
5232 FIXME("Sampling attached render targets is not supported.\n");
5234 WARN("Sampling attached render targets is not supported, skipping draw.\n");
5235 context_release(context
);
5238 checkGLcall("glTextureBarrier");
5241 if (parameters
->indirect
)
5243 if (!context
->use_immediate_mode_draw
&& !emulation
)
5244 wined3d_context_gl_draw_indirect(context_gl
, state
, ¶meters
->u
.indirect
, idx_size
);
5246 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
5250 unsigned int instance_count
= parameters
->u
.direct
.instance_count
;
5252 if (context
->use_immediate_mode_draw
|| emulation
)
5253 draw_primitive_immediate_mode(wined3d_context_gl(context
), state
, stream_info
, idx_data
,
5254 idx_size
, parameters
->u
.direct
.base_vertex_idx
,
5255 parameters
->u
.direct
.start_idx
, parameters
->u
.direct
.index_count
, instance_count
);
5257 wined3d_context_gl_draw_primitive_arrays(context_gl
, state
, idx_data
, idx_size
,
5258 parameters
->u
.direct
.base_vertex_idx
, parameters
->u
.direct
.start_idx
,
5259 parameters
->u
.direct
.index_count
, parameters
->u
.direct
.start_instance
, instance_count
);
5262 if (context
->uses_uavs
)
5264 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS
));
5265 checkGLcall("glMemoryBarrier");
5268 if (rasterizer_discard
)
5270 glDisable(GL_RASTERIZER_DISCARD
);
5271 checkGLcall("disable rasterizer discard");
5274 context_release(context
);
5276 TRACE("Draw completed.\n");
5279 void wined3d_context_gl_unload_tex_coords(const struct wined3d_context_gl
*context_gl
)
5281 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5282 unsigned int texture_idx
;
5284 for (texture_idx
= 0; texture_idx
< gl_info
->limits
.texture_coords
; ++texture_idx
)
5286 gl_info
->gl_ops
.ext
.p_glClientActiveTextureARB(GL_TEXTURE0_ARB
+ texture_idx
);
5287 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
5291 static const void *get_vertex_attrib_pointer(const struct wined3d_stream_info_element
*element
,
5292 const struct wined3d_state
*state
)
5294 const uint8_t *offset
= element
->data
.addr
+ state
->load_base_vertex_index
* element
->stride
;
5296 if (element
->data
.buffer_object
)
5297 offset
+= element
->data
.buffer_object
->buffer_offset
;
5301 void wined3d_context_gl_load_tex_coords(const struct wined3d_context_gl
*context_gl
,
5302 const struct wined3d_stream_info
*si
, GLuint
*current_bo
, const struct wined3d_state
*state
)
5304 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5305 const struct wined3d_format_gl
*format_gl
;
5306 unsigned int mapped_stage
= 0;
5307 unsigned int texture_idx
;
5310 for (texture_idx
= 0; texture_idx
< context_gl
->c
.d3d_info
->ffp_fragment_caps
.max_blend_stages
; ++texture_idx
)
5312 unsigned int coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
5314 if ((mapped_stage
= context_gl
->tex_unit_map
[texture_idx
]) == WINED3D_UNMAPPED_STAGE
)
5317 if (mapped_stage
>= gl_info
->limits
.texture_coords
)
5319 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage
);
5323 if (coord_idx
< WINED3D_MAX_FFP_TEXTURES
&& (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
))))
5325 const struct wined3d_stream_info_element
*e
= &si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
];
5327 TRACE("Setting up texture %u, idx %u, coord_idx %u, data %s.\n",
5328 texture_idx
, mapped_stage
, coord_idx
, debug_bo_address(&e
->data
));
5330 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5331 if (*current_bo
!= bo
)
5333 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5334 checkGLcall("glBindBuffer");
5338 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB
+ mapped_stage
));
5339 checkGLcall("glClientActiveTextureARB");
5341 /* The coords to supply depend completely on the fvf/vertex shader. */
5342 format_gl
= wined3d_format_gl(e
->format
);
5343 gl_info
->gl_ops
.gl
.p_glTexCoordPointer(format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
,
5344 get_vertex_attrib_pointer(e
, state
));
5345 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
5347 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5351 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ mapped_stage
, 0, 0, 0, 1));
5354 if (gl_info
->supported
[NV_REGISTER_COMBINERS
])
5356 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
5357 for (texture_idx
= mapped_stage
+ 1; texture_idx
< gl_info
->limits
.ffp_textures
; ++texture_idx
)
5359 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_idx
, 0, 0, 0, 1));
5363 checkGLcall("loadTexCoords");
5366 /* This should match any arrays loaded in wined3d_context_gl_load_vertex_data(). */
5367 static void wined3d_context_gl_unload_vertex_data(struct wined3d_context_gl
*context_gl
)
5369 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5371 if (!context_gl
->c
.namedArraysLoaded
)
5373 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_VERTEX_ARRAY
);
5374 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_NORMAL_ARRAY
);
5375 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_COLOR_ARRAY
);
5376 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5377 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT
);
5378 wined3d_context_gl_unload_tex_coords(context_gl
);
5379 context_gl
->c
.namedArraysLoaded
= FALSE
;
5382 static void wined3d_context_gl_load_vertex_data(struct wined3d_context_gl
*context_gl
,
5383 const struct wined3d_stream_info
*si
, const struct wined3d_state
*state
)
5385 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5386 const struct wined3d_stream_info_element
*e
;
5387 const struct wined3d_format_gl
*format_gl
;
5388 GLuint current_bo
, bo
;
5391 TRACE("context_gl %p, si %p, state %p.\n", context_gl
, si
, state
);
5393 /* This is used for the fixed-function pipeline only, and the
5394 * fixed-function pipeline doesn't do instancing. */
5395 current_bo
= gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
] ? ~0u : 0;
5398 if ((si
->use_map
& (1u << WINED3D_FFP_BLENDWEIGHT
))
5399 || si
->use_map
& (1u << WINED3D_FFP_BLENDINDICES
))
5401 /* TODO: Support vertex blending in immediate mode draws. No need to
5402 * write a FIXME here, this is done after the general vertex
5403 * declaration decoding. */
5404 WARN("Vertex blending not supported.\n");
5408 if (si
->use_map
& (1u << WINED3D_FFP_PSIZE
))
5410 /* No such functionality in the fixed-function GL pipeline. */
5411 WARN("Per-vertex point size not supported.\n");
5415 if (si
->use_map
& (1u << WINED3D_FFP_POSITION
))
5417 e
= &si
->elements
[WINED3D_FFP_POSITION
];
5418 format_gl
= wined3d_format_gl(e
->format
);
5419 offset
= get_vertex_attrib_pointer(e
, state
);
5421 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5422 if (current_bo
!= bo
)
5424 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5425 checkGLcall("glBindBuffer");
5429 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n", format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5430 gl_info
->gl_ops
.gl
.p_glVertexPointer(format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5431 checkGLcall("glVertexPointer(...)");
5432 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_VERTEX_ARRAY
);
5433 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5435 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5439 if (si
->use_map
& (1u << WINED3D_FFP_NORMAL
))
5441 e
= &si
->elements
[WINED3D_FFP_NORMAL
];
5442 format_gl
= wined3d_format_gl(e
->format
);
5443 offset
= get_vertex_attrib_pointer(e
, state
);
5445 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5446 if (current_bo
!= bo
)
5448 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5449 checkGLcall("glBindBuffer");
5453 TRACE("glNormalPointer(%#x, %#x, %p);\n", format_gl
->vtx_type
, e
->stride
, offset
);
5454 gl_info
->gl_ops
.gl
.p_glNormalPointer(format_gl
->vtx_type
, e
->stride
, offset
);
5455 checkGLcall("glNormalPointer(...)");
5456 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_NORMAL_ARRAY
);
5457 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5459 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5463 gl_info
->gl_ops
.gl
.p_glNormal3f(0, 0, 0);
5464 checkGLcall("glNormal3f(0, 0, 0)");
5467 /* Diffuse colour */
5468 if (si
->use_map
& (1u << WINED3D_FFP_DIFFUSE
))
5470 e
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
5471 format_gl
= wined3d_format_gl(e
->format
);
5472 offset
= get_vertex_attrib_pointer(e
, state
);
5474 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5475 if (current_bo
!= bo
)
5477 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5478 checkGLcall("glBindBuffer");
5482 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5483 format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5484 gl_info
->gl_ops
.gl
.p_glColorPointer(format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5485 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5486 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_COLOR_ARRAY
);
5487 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5489 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5493 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
5494 checkGLcall("glColor4f(1, 1, 1, 1)");
5497 /* Specular colour */
5498 if (si
->use_map
& (1u << WINED3D_FFP_SPECULAR
))
5500 TRACE("Setting specular colour.\n");
5502 e
= &si
->elements
[WINED3D_FFP_SPECULAR
];
5503 offset
= get_vertex_attrib_pointer(e
, state
);
5505 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5510 format_gl
= wined3d_format_gl(e
->format
);
5511 type
= format_gl
->vtx_type
;
5512 format
= format_gl
->vtx_format
;
5514 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5515 if (current_bo
!= bo
)
5517 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5518 checkGLcall("glBindBuffer");
5522 if (format
!= 4 || (gl_info
->quirks
& WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA
))
5524 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5525 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5526 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5527 * 4 component secondary colors use it
5529 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format
, type
, e
->stride
, offset
);
5530 GL_EXTCALL(glSecondaryColorPointerEXT(format
, type
, e
->stride
, offset
));
5531 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5537 case GL_UNSIGNED_BYTE
:
5538 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e
->stride
, offset
);
5539 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE
, e
->stride
, offset
));
5540 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5544 FIXME("Add 4 component specular colour pointers for type %#x.\n", type
);
5545 /* Make sure that the right colour component is dropped. */
5546 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type
, e
->stride
, offset
);
5547 GL_EXTCALL(glSecondaryColorPointerEXT(3, type
, e
->stride
, offset
));
5548 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5551 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT
);
5552 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5554 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5558 WARN("Specular colour is not supported in this GL implementation.\n");
5563 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5565 GL_EXTCALL(glSecondaryColor3fEXT
)(0, 0, 0);
5566 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5570 WARN("Specular colour is not supported in this GL implementation.\n");
5574 /* Texture coordinates */
5575 wined3d_context_gl_load_tex_coords(context_gl
, si
, ¤t_bo
, state
);
5578 static void wined3d_context_gl_unload_numbered_array(struct wined3d_context_gl
*context_gl
, unsigned int i
)
5580 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5582 GL_EXTCALL(glDisableVertexAttribArray(i
));
5583 checkGLcall("glDisableVertexAttribArray");
5584 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
5585 GL_EXTCALL(glVertexAttribDivisor(i
, 0));
5587 context_gl
->c
.numbered_array_mask
&= ~(1u << i
);
5590 static void wined3d_context_gl_unload_numbered_arrays(struct wined3d_context_gl
*context_gl
)
5592 uint32_t mask
= context_gl
->c
.numbered_array_mask
;
5597 i
= wined3d_bit_scan(&mask
);
5598 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5602 static void wined3d_context_gl_load_numbered_arrays(struct wined3d_context_gl
*context_gl
,
5603 const struct wined3d_stream_info
*stream_info
, const struct wined3d_state
*state
)
5605 struct wined3d_context
*context
= &context_gl
->c
;
5606 const struct wined3d_shader
*vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
5607 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5608 GLuint current_bo
, bo
;
5611 /* Default to no instancing. */
5612 current_bo
= gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
] ? ~0u : 0;
5614 if (stream_info
->use_map
& ~wined3d_mask_from_size(gl_info
->limits
.vertex_attribs
))
5616 static unsigned int once
;
5619 FIXME("More than the supported %u vertex attributes are in use.\n", gl_info
->limits
.vertex_attribs
);
5622 for (i
= 0; i
< gl_info
->limits
.vertex_attribs
; ++i
)
5624 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[i
];
5625 const struct wined3d_stream_state
*stream
;
5626 const struct wined3d_format_gl
*format_gl
;
5628 if (!(stream_info
->use_map
& (1u << i
)))
5630 if (context
->numbered_array_mask
& (1u << i
))
5631 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5632 if (!use_vs(state
) && i
== WINED3D_FFP_DIFFUSE
)
5634 if (!(context_gl
->default_attrib_value_set
& (1u << i
)) || !context_gl
->diffuse_attrib_to_1
)
5636 GL_EXTCALL(glVertexAttrib4f(i
, 1.0f
, 1.0f
, 1.0f
, 1.0f
));
5637 context_gl
->diffuse_attrib_to_1
= 1;
5642 if (!(context_gl
->default_attrib_value_set
& (1u << i
)))
5644 GL_EXTCALL(glVertexAttrib4f(i
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
5645 if (i
== WINED3D_FFP_DIFFUSE
)
5646 context_gl
->diffuse_attrib_to_1
= 0;
5649 context_gl
->default_attrib_value_set
|= 1u << i
;
5653 format_gl
= wined3d_format_gl(element
->format
);
5654 stream
= &state
->streams
[element
->stream_idx
];
5656 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
5658 unsigned int divisor
= 0;
5660 if (element
->instanced
)
5661 divisor
= element
->divisor
? element
->divisor
: UINT_MAX
;
5662 GL_EXTCALL(glVertexAttribDivisor(i
, divisor
));
5664 else if (element
->divisor
)
5666 /* Unload instanced arrays, they will be loaded using immediate
5668 if (context
->numbered_array_mask
& (1u << i
))
5669 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5670 context_gl
->default_attrib_value_set
&= ~(1u << i
);
5674 TRACE("Loading array %u %s.\n", i
, debug_bo_address(&element
->data
));
5676 if (element
->stride
)
5678 const void *offset
= get_vertex_attrib_pointer(element
, state
);
5679 unsigned int format_attrs
= format_gl
->f
.attrs
;
5681 bo
= wined3d_bo_gl_id(element
->data
.buffer_object
);
5682 if (current_bo
!= bo
)
5684 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5685 checkGLcall("glBindBuffer");
5689 wined3d_buffer_validate_user(stream
->buffer
);
5691 /* Use the VBO to find out if a vertex buffer exists, not the vb
5692 * pointer. vb can point to a user pointer data blob. In that case
5693 * current_bo will be 0. If there is a vertex buffer but no vbo we
5694 * won't be load converted attributes anyway. */
5695 if (vs
&& vs
->reg_maps
.shader_version
.major
>= 4 && (format_attrs
& WINED3D_FORMAT_ATTR_INTEGER
))
5697 GL_EXTCALL(glVertexAttribIPointer(i
, format_gl
->vtx_format
,
5698 format_gl
->vtx_type
, element
->stride
, offset
));
5702 GL_EXTCALL(glVertexAttribPointer(i
, format_gl
->vtx_format
, format_gl
->vtx_type
,
5703 !!(format_attrs
& WINED3D_FORMAT_ATTR_NORMALISED
), element
->stride
, offset
));
5706 if (!(context
->numbered_array_mask
& (1u << i
)))
5708 GL_EXTCALL(glEnableVertexAttribArray(i
));
5709 context
->numbered_array_mask
|= (1u << i
);
5714 /* Stride = 0 means always the same values.
5715 * glVertexAttribPointer() doesn't do that. Instead disable the
5716 * pointer and set up the attribute statically. But we have to
5717 * figure out the system memory address. */
5718 const BYTE
*ptr
= element
->data
.addr
;
5719 if (element
->data
.buffer_object
)
5720 ptr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(stream
->buffer
, context
);
5722 if (context
->numbered_array_mask
& (1u << i
))
5723 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5725 switch (format_gl
->f
.id
)
5727 case WINED3DFMT_R32_FLOAT
:
5728 GL_EXTCALL(glVertexAttrib1fv(i
, (const GLfloat
*)ptr
));
5730 case WINED3DFMT_R32G32_FLOAT
:
5731 GL_EXTCALL(glVertexAttrib2fv(i
, (const GLfloat
*)ptr
));
5733 case WINED3DFMT_R32G32B32_FLOAT
:
5734 GL_EXTCALL(glVertexAttrib3fv(i
, (const GLfloat
*)ptr
));
5736 case WINED3DFMT_R32G32B32A32_FLOAT
:
5737 GL_EXTCALL(glVertexAttrib4fv(i
, (const GLfloat
*)ptr
));
5739 case WINED3DFMT_R8G8B8A8_UINT
:
5740 GL_EXTCALL(glVertexAttrib4ubv(i
, ptr
));
5742 case WINED3DFMT_B8G8R8A8_UNORM
:
5743 if (gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
])
5745 const DWORD
*src
= (const DWORD
*)ptr
;
5746 DWORD c
= *src
& 0xff00ff00u
;
5747 c
|= (*src
& 0xff0000u
) >> 16;
5748 c
|= (*src
& 0xffu
) << 16;
5749 GL_EXTCALL(glVertexAttrib4Nubv(i
, (GLubyte
*)&c
));
5752 /* else fallthrough */
5753 case WINED3DFMT_R8G8B8A8_UNORM
:
5754 GL_EXTCALL(glVertexAttrib4Nubv(i
, ptr
));
5756 case WINED3DFMT_R16G16_SINT
:
5757 GL_EXTCALL(glVertexAttrib2sv(i
, (const GLshort
*)ptr
));
5759 case WINED3DFMT_R16G16B16A16_SINT
:
5760 GL_EXTCALL(glVertexAttrib4sv(i
, (const GLshort
*)ptr
));
5762 case WINED3DFMT_R16G16_SNORM
:
5764 const GLshort s
[4] = {((const GLshort
*)ptr
)[0], ((const GLshort
*)ptr
)[1], 0, 1};
5765 GL_EXTCALL(glVertexAttrib4Nsv(i
, s
));
5768 case WINED3DFMT_R16G16_UNORM
:
5770 const GLushort s
[4] = {((const GLushort
*)ptr
)[0], ((const GLushort
*)ptr
)[1], 0, 1};
5771 GL_EXTCALL(glVertexAttrib4Nusv(i
, s
));
5774 case WINED3DFMT_R16G16B16A16_SNORM
:
5775 GL_EXTCALL(glVertexAttrib4Nsv(i
, (const GLshort
*)ptr
));
5777 case WINED3DFMT_R16G16B16A16_UNORM
:
5778 GL_EXTCALL(glVertexAttrib4Nusv(i
, (const GLushort
*)ptr
));
5780 case WINED3DFMT_R10G10B10X2_UINT
:
5781 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5782 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5784 case WINED3DFMT_R10G10B10X2_SNORM
:
5785 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5786 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5788 case WINED3DFMT_R16G16_FLOAT
:
5789 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
5791 /* Not supported by GL_ARB_half_float_vertex. */
5792 GL_EXTCALL(glVertexAttrib2hvNV(i
, (const GLhalfNV
*)ptr
));
5796 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
5797 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
5798 GL_EXTCALL(glVertexAttrib2f(i
, x
, y
));
5801 case WINED3DFMT_R16G16B16A16_FLOAT
:
5802 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
5804 /* Not supported by GL_ARB_half_float_vertex. */
5805 GL_EXTCALL(glVertexAttrib4hvNV(i
, (const GLhalfNV
*)ptr
));
5809 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
5810 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
5811 float z
= float_16_to_32(((const unsigned short *)ptr
) + 2);
5812 float w
= float_16_to_32(((const unsigned short *)ptr
) + 3);
5813 GL_EXTCALL(glVertexAttrib4f(i
, x
, y
, z
, w
));
5817 ERR("Unexpected declaration in stride 0 attributes.\n");
5821 context_gl
->default_attrib_value_set
&= ~(1u << i
);
5824 checkGLcall("Loading numbered arrays");
5827 void wined3d_context_gl_update_stream_sources(struct wined3d_context_gl
*context_gl
,
5828 const struct wined3d_state
*state
)
5830 if (context_gl
->c
.use_immediate_mode_draw
)
5833 wined3d_context_gl_unload_vertex_data(context_gl
);
5834 if (context_gl
->c
.d3d_info
->ffp_generic_attributes
|| use_vs(state
))
5836 TRACE("Loading numbered arrays.\n");
5837 wined3d_context_gl_load_numbered_arrays(context_gl
, &context_gl
->c
.stream_info
, state
);
5841 TRACE("Loading named arrays.\n");
5842 wined3d_context_gl_unload_numbered_arrays(context_gl
);
5843 wined3d_context_gl_load_vertex_data(context_gl
, &context_gl
->c
.stream_info
, state
);
5844 context_gl
->c
.namedArraysLoaded
= TRUE
;
5847 static void apply_texture_blit_state(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*texture
,
5848 GLenum target
, unsigned int level
, enum wined3d_texture_filter_type filter
)
5850 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(filter
));
5851 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
5852 wined3d_gl_min_mip_filter(filter
, WINED3D_TEXF_NONE
));
5853 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
5854 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
5855 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
5856 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
);
5857 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, level
);
5859 /* We changed the filtering settings on the texture. Make sure they get
5860 * reset on subsequent draws. */
5861 texture
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
5862 texture
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
5863 texture
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
5864 texture
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
5865 texture
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
5866 texture
->sampler_desc
.srgb_decode
= FALSE
;
5867 texture
->sampler_desc
.mip_base_level
= level
;
5870 /* Context activation is done by the caller. */
5871 void wined3d_context_gl_draw_shaded_quad(struct wined3d_context_gl
*context_gl
, struct wined3d_texture_gl
*texture_gl
,
5872 unsigned int sub_resource_idx
, const RECT
*src_rect
, const RECT
*dst_rect
,
5873 enum wined3d_texture_filter_type filter
)
5875 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5876 struct wined3d_blt_info info
;
5877 unsigned int level
, w
, h
, i
;
5882 struct wined3d_vec3 texcoord
;
5886 texture2d_get_blt_info(texture_gl
, sub_resource_idx
, src_rect
, &info
);
5888 level
= sub_resource_idx
% texture_gl
->t
.level_count
;
5889 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, texture_gl
->texture_rgb
.name
);
5890 apply_texture_blit_state(gl_info
, &texture_gl
->texture_rgb
, info
.bind_target
, level
, filter
);
5891 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, level
);
5893 wined3d_context_gl_pause_transform_feedback(context_gl
, FALSE
);
5895 wined3d_context_gl_get_rt_size(context_gl
, &dst_size
);
5899 quad
[0].x
= dst_rect
->left
* 2.0f
/ w
- 1.0f
;
5900 quad
[0].y
= dst_rect
->top
* 2.0f
/ h
- 1.0f
;
5901 quad
[0].texcoord
= info
.texcoords
[0];
5903 quad
[1].x
= dst_rect
->right
* 2.0f
/ w
- 1.0f
;
5904 quad
[1].y
= dst_rect
->top
* 2.0f
/ h
- 1.0f
;
5905 quad
[1].texcoord
= info
.texcoords
[1];
5907 quad
[2].x
= dst_rect
->left
* 2.0f
/ w
- 1.0f
;
5908 quad
[2].y
= dst_rect
->bottom
* 2.0f
/ h
- 1.0f
;
5909 quad
[2].texcoord
= info
.texcoords
[2];
5911 quad
[3].x
= dst_rect
->right
* 2.0f
/ w
- 1.0f
;
5912 quad
[3].y
= dst_rect
->bottom
* 2.0f
/ h
- 1.0f
;
5913 quad
[3].texcoord
= info
.texcoords
[3];
5916 if (gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
5918 if (!context_gl
->blit_vbo
)
5919 GL_EXTCALL(glGenBuffers(1, &context_gl
->blit_vbo
));
5920 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, context_gl
->blit_vbo
));
5922 wined3d_context_gl_unload_vertex_data(context_gl
);
5923 wined3d_context_gl_unload_numbered_arrays(context_gl
);
5925 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER
, sizeof(quad
), quad
, GL_STREAM_DRAW
));
5926 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT
, FALSE
, sizeof(*quad
), NULL
));
5927 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT
, FALSE
, sizeof(*quad
),
5928 (void *)FIELD_OFFSET(struct blit_vertex
, texcoord
)));
5930 GL_EXTCALL(glEnableVertexAttribArray(0));
5931 GL_EXTCALL(glEnableVertexAttribArray(1));
5933 gl_info
->gl_ops
.gl
.p_glDrawArrays(GL_TRIANGLE_STRIP
, 0, 4);
5935 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, 0));
5936 GL_EXTCALL(glDisableVertexAttribArray(1));
5937 GL_EXTCALL(glDisableVertexAttribArray(0));
5941 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
5943 for (i
= 0; i
< ARRAY_SIZE(quad
); ++i
)
5945 GL_EXTCALL(glVertexAttrib3fv(1, &quad
[i
].texcoord
.x
));
5946 GL_EXTCALL(glVertexAttrib2fv(0, &quad
[i
].x
));
5949 gl_info
->gl_ops
.gl
.p_glEnd();
5951 checkGLcall("draw");
5953 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, texture_gl
->t
.level_count
- 1);
5954 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, 0);
5957 /* Context activation is done by the caller. */
5958 void wined3d_context_gl_draw_textured_quad(struct wined3d_context_gl
*context_gl
,
5959 struct wined3d_texture_gl
*texture_gl
, unsigned int sub_resource_idx
,
5960 const RECT
*src_rect
, const RECT
*dst_rect
, enum wined3d_texture_filter_type filter
)
5962 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5963 struct wined3d_blt_info info
;
5966 texture2d_get_blt_info(texture_gl
, sub_resource_idx
, src_rect
, &info
);
5968 gl_info
->gl_ops
.gl
.p_glEnable(info
.bind_target
);
5969 checkGLcall("glEnable(bind_target)");
5971 level
= sub_resource_idx
% texture_gl
->t
.level_count
;
5972 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, texture_gl
->texture_rgb
.name
);
5973 apply_texture_blit_state(gl_info
, &texture_gl
->texture_rgb
, info
.bind_target
, level
, filter
);
5974 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, level
);
5975 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
5976 checkGLcall("glTexEnvi");
5978 wined3d_context_gl_pause_transform_feedback(context_gl
, FALSE
);
5981 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
5982 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[0].x
);
5983 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->top
);
5985 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[1].x
);
5986 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->top
);
5988 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[2].x
);
5989 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->bottom
);
5991 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[3].x
);
5992 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->bottom
);
5993 gl_info
->gl_ops
.gl
.p_glEnd();
5995 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, texture_gl
->t
.level_count
- 1);
5996 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, 0);