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"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
30 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
31 WINE_DECLARE_DEBUG_CHANNEL(d3d_sync
);
33 #define WINED3D_MAX_FBO_ENTRIES 64
34 #define WINED3D_ALL_LAYERS (~0u)
36 static DWORD wined3d_context_tls_idx
;
38 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
39 * actually have the same values in GL and D3D. */
40 static GLenum
gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
42 switch (primitive_type
)
44 case WINED3D_PT_POINTLIST
:
47 case WINED3D_PT_LINELIST
:
50 case WINED3D_PT_LINESTRIP
:
53 case WINED3D_PT_TRIANGLELIST
:
56 case WINED3D_PT_TRIANGLESTRIP
:
57 return GL_TRIANGLE_STRIP
;
59 case WINED3D_PT_TRIANGLEFAN
:
60 return GL_TRIANGLE_FAN
;
62 case WINED3D_PT_LINELIST_ADJ
:
63 return GL_LINES_ADJACENCY_ARB
;
65 case WINED3D_PT_LINESTRIP_ADJ
:
66 return GL_LINE_STRIP_ADJACENCY_ARB
;
68 case WINED3D_PT_TRIANGLELIST_ADJ
:
69 return GL_TRIANGLES_ADJACENCY_ARB
;
71 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
72 return GL_TRIANGLE_STRIP_ADJACENCY_ARB
;
74 case WINED3D_PT_PATCH
:
78 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
79 case WINED3D_PT_UNDEFINED
:
84 /* FBO helper functions */
86 /* Context activation is done by the caller. */
87 static void wined3d_context_gl_bind_fbo(struct wined3d_context_gl
*context_gl
, GLenum target
, GLuint fbo
)
89 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
91 TRACE("context_gl %p, target %#x, fbo %u.\n", context_gl
, target
, fbo
);
95 case GL_READ_FRAMEBUFFER
:
96 if (context_gl
->fbo_read_binding
== fbo
)
98 context_gl
->fbo_read_binding
= fbo
;
101 case GL_DRAW_FRAMEBUFFER
:
102 if (context_gl
->fbo_draw_binding
== fbo
)
104 context_gl
->fbo_draw_binding
= fbo
;
108 if (context_gl
->fbo_read_binding
== fbo
109 && context_gl
->fbo_draw_binding
== fbo
)
111 context_gl
->fbo_read_binding
= fbo
;
112 context_gl
->fbo_draw_binding
= fbo
;
116 FIXME("Unhandled target %#x.\n", target
);
120 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
121 checkGLcall("glBindFramebuffer()");
124 /* Context activation is done by the caller. */
125 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
129 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
131 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
132 checkGLcall("glFramebufferTexture2D()");
134 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
135 checkGLcall("glFramebufferTexture2D()");
137 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
138 checkGLcall("glFramebufferTexture2D()");
141 /* Context activation is done by the caller. */
142 static void wined3d_context_gl_destroy_fbo(struct wined3d_context_gl
*context_gl
, GLuint fbo
)
144 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
146 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, fbo
);
147 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
148 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
150 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
151 checkGLcall("glDeleteFramebuffers()");
154 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
155 GLenum fbo_target
, DWORD flags
, GLuint rb
)
157 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
159 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
160 checkGLcall("glFramebufferRenderbuffer()");
163 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
165 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
166 checkGLcall("glFramebufferRenderbuffer()");
170 static void wined3d_context_gl_attach_gl_texture_fbo(struct wined3d_context_gl
*context_gl
,
171 GLenum fbo_target
, GLenum attachment
, const struct wined3d_fbo_resource
*resource
)
173 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
177 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
, GL_TEXTURE_2D
, 0, 0);
179 else if (resource
->layer
== WINED3D_ALL_LAYERS
)
181 if (!gl_info
->fbo_ops
.glFramebufferTexture
)
183 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
187 gl_info
->fbo_ops
.glFramebufferTexture(fbo_target
, attachment
,
188 resource
->object
, resource
->level
);
190 else if (resource
->target
== GL_TEXTURE_1D_ARRAY
|| resource
->target
== GL_TEXTURE_2D_ARRAY
191 || resource
->target
== GL_TEXTURE_3D
)
193 if (!gl_info
->fbo_ops
.glFramebufferTextureLayer
)
195 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
199 gl_info
->fbo_ops
.glFramebufferTextureLayer(fbo_target
, attachment
,
200 resource
->object
, resource
->level
, resource
->layer
);
202 else if (resource
->target
== GL_TEXTURE_1D
)
204 gl_info
->fbo_ops
.glFramebufferTexture1D(fbo_target
, attachment
,
205 resource
->target
, resource
->object
, resource
->level
);
209 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
,
210 resource
->target
, resource
->object
, resource
->level
);
212 checkGLcall("attach texture to fbo");
215 /* Context activation is done by the caller. */
216 static void wined3d_context_gl_attach_depth_stencil_fbo(struct wined3d_context_gl
*context_gl
,
217 GLenum fbo_target
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
,
220 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
222 if (resource
->object
)
224 TRACE("Attach depth stencil %u.\n", resource
->object
);
228 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
229 flags
, resource
->object
);
233 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
234 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_DEPTH_ATTACHMENT
, resource
);
236 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
237 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_STENCIL_ATTACHMENT
, resource
);
240 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
))
241 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
243 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
))
244 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
248 TRACE("Attach depth stencil 0.\n");
250 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
251 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
255 /* Context activation is done by the caller. */
256 static void wined3d_context_gl_attach_surface_fbo(struct wined3d_context_gl
*context_gl
,
257 GLenum fbo_target
, unsigned int idx
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
)
259 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
261 TRACE("Attach GL object %u to %u.\n", resource
->object
, idx
);
263 if (resource
->object
)
267 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
268 GL_RENDERBUFFER
, resource
->object
);
269 checkGLcall("glFramebufferRenderbuffer()");
273 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, resource
);
278 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, NULL
);
282 static void context_dump_fbo_attachment(const struct wined3d_gl_info
*gl_info
, GLenum target
,
290 enum wined3d_gl_extension extension
;
294 {GL_TEXTURE_1D
, GL_TEXTURE_BINDING_1D
, "1d", WINED3D_GL_EXT_NONE
},
295 {GL_TEXTURE_1D_ARRAY
, GL_TEXTURE_BINDING_1D_ARRAY
, "1d-array", EXT_TEXTURE_ARRAY
},
296 {GL_TEXTURE_2D
, GL_TEXTURE_BINDING_2D
, "2d", WINED3D_GL_EXT_NONE
},
297 {GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_BINDING_RECTANGLE_ARB
, "rectangle", ARB_TEXTURE_RECTANGLE
},
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 static inline void wined3d_context_gl_set_fbo_key_for_render_target(const struct wined3d_context_gl
*context_gl
,
470 struct wined3d_fbo_entry_key
*key
, unsigned int idx
, const struct wined3d_rendertarget_info
*render_target
,
473 unsigned int sub_resource_idx
= render_target
->sub_resource_idx
;
474 struct wined3d_resource
*resource
= render_target
->resource
;
475 struct wined3d_texture_gl
*texture_gl
;
477 if (!resource
|| resource
->format
->id
== WINED3DFMT_NULL
|| resource
->type
== WINED3D_RTYPE_BUFFER
)
479 if (resource
&& resource
->type
== WINED3D_RTYPE_BUFFER
)
480 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
481 key
->objects
[idx
].object
= 0;
482 key
->objects
[idx
].target
= 0;
483 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
487 if (render_target
->gl_view
.name
)
489 key
->objects
[idx
].object
= render_target
->gl_view
.name
;
490 key
->objects
[idx
].target
= render_target
->gl_view
.target
;
491 key
->objects
[idx
].level
= 0;
492 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
496 texture_gl
= wined3d_texture_gl(wined3d_texture_from_resource(resource
));
497 if (texture_gl
->current_renderbuffer
)
499 key
->objects
[idx
].object
= texture_gl
->current_renderbuffer
->id
;
500 key
->objects
[idx
].target
= 0;
501 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
502 key
->rb_namespace
|= 1 << idx
;
506 key
->objects
[idx
].target
= wined3d_texture_gl_get_sub_resource_target(texture_gl
, sub_resource_idx
);
507 key
->objects
[idx
].level
= sub_resource_idx
% texture_gl
->t
.level_count
;
508 key
->objects
[idx
].layer
= sub_resource_idx
/ texture_gl
->t
.level_count
;
510 if (render_target
->layer_count
!= 1)
511 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
515 case WINED3D_LOCATION_TEXTURE_RGB
:
516 key
->objects
[idx
].object
= wined3d_texture_gl_get_texture_name(texture_gl
, &context_gl
->c
, FALSE
);
519 case WINED3D_LOCATION_TEXTURE_SRGB
:
520 key
->objects
[idx
].object
= wined3d_texture_gl_get_texture_name(texture_gl
, &context_gl
->c
, TRUE
);
523 case WINED3D_LOCATION_RB_MULTISAMPLE
:
524 key
->objects
[idx
].object
= texture_gl
->rb_multisample
;
525 key
->objects
[idx
].target
= 0;
526 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
527 key
->rb_namespace
|= 1 << idx
;
530 case WINED3D_LOCATION_RB_RESOLVED
:
531 key
->objects
[idx
].object
= texture_gl
->rb_resolved
;
532 key
->objects
[idx
].target
= 0;
533 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
534 key
->rb_namespace
|= 1 << idx
;
539 static void wined3d_context_gl_generate_fbo_key(const struct wined3d_context_gl
*context_gl
,
540 struct wined3d_fbo_entry_key
*key
, const struct wined3d_rendertarget_info
*render_targets
,
541 const struct wined3d_rendertarget_info
*depth_stencil
, DWORD color_location
, DWORD ds_location
)
543 unsigned int buffers
= context_gl
->gl_info
->limits
.buffers
;
546 key
->rb_namespace
= 0;
547 wined3d_context_gl_set_fbo_key_for_render_target(context_gl
, key
, 0, depth_stencil
, ds_location
);
549 for (i
= 0; i
< buffers
; ++i
)
550 wined3d_context_gl_set_fbo_key_for_render_target(context_gl
, key
, i
+ 1, &render_targets
[i
], color_location
);
552 memset(&key
->objects
[buffers
+ 1], 0, (ARRAY_SIZE(key
->objects
) - buffers
- 1) * sizeof(*key
->objects
));
555 static struct fbo_entry
*wined3d_context_gl_create_fbo_entry(const struct wined3d_context_gl
*context_gl
,
556 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
557 DWORD color_location
, DWORD ds_location
)
559 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
560 struct fbo_entry
*entry
;
562 entry
= heap_alloc(sizeof(*entry
));
563 wined3d_context_gl_generate_fbo_key(context_gl
, &entry
->key
,
564 render_targets
, depth_stencil
, color_location
, ds_location
);
566 if (depth_stencil
->resource
)
568 if (depth_stencil
->resource
->format
->depth_size
)
569 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
570 if (depth_stencil
->resource
->format
->stencil_size
)
571 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
573 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
574 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
575 checkGLcall("glGenFramebuffers()");
576 TRACE("Created FBO %u.\n", entry
->id
);
581 /* Context activation is done by the caller. */
582 static void wined3d_context_gl_reuse_fbo_entry(struct wined3d_context_gl
*context_gl
, GLenum target
,
583 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
584 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
586 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
588 wined3d_context_gl_bind_fbo(context_gl
, target
, entry
->id
);
589 context_clean_fbo_attachments(gl_info
, target
);
591 wined3d_context_gl_generate_fbo_key(context_gl
, &entry
->key
,
592 render_targets
, depth_stencil
, color_location
, ds_location
);
594 if (depth_stencil
->resource
)
596 if (depth_stencil
->resource
->format
->depth_size
)
597 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
598 if (depth_stencil
->resource
->format
->stencil_size
)
599 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
603 /* Context activation is done by the caller. */
604 static void wined3d_context_gl_destroy_fbo_entry(struct wined3d_context_gl
*context_gl
, struct fbo_entry
*entry
)
608 TRACE("Destroy FBO %u.\n", entry
->id
);
609 wined3d_context_gl_destroy_fbo(context_gl
, entry
->id
);
611 --context_gl
->fbo_entry_count
;
612 list_remove(&entry
->entry
);
616 /* Context activation is done by the caller. */
617 static struct fbo_entry
*wined3d_context_gl_find_fbo_entry(struct wined3d_context_gl
*context_gl
, GLenum target
,
618 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
619 DWORD color_location
, DWORD ds_location
)
621 static const struct wined3d_rendertarget_info ds_null
= {{0}};
622 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
623 struct wined3d_texture
*rt_texture
, *ds_texture
;
624 struct wined3d_fbo_entry_key fbo_key
;
625 unsigned int i
, ds_level
, rt_level
;
626 struct fbo_entry
*entry
;
628 if (depth_stencil
->resource
&& depth_stencil
->resource
->type
!= WINED3D_RTYPE_BUFFER
629 && render_targets
[0].resource
&& render_targets
[0].resource
->type
!= WINED3D_RTYPE_BUFFER
630 && render_targets
[0].resource
->format
->id
!= WINED3DFMT_NULL
)
632 rt_texture
= wined3d_texture_from_resource(render_targets
[0].resource
);
633 rt_level
= render_targets
[0].sub_resource_idx
% rt_texture
->level_count
;
634 ds_texture
= wined3d_texture_from_resource(depth_stencil
->resource
);
635 ds_level
= depth_stencil
->sub_resource_idx
% ds_texture
->level_count
;
637 if (wined3d_texture_get_level_width(ds_texture
, ds_level
)
638 < wined3d_texture_get_level_width(rt_texture
, rt_level
)
639 || wined3d_texture_get_level_height(ds_texture
, ds_level
)
640 < wined3d_texture_get_level_height(rt_texture
, rt_level
))
642 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
643 depth_stencil
= &ds_null
;
645 else if (ds_texture
->resource
.multisample_type
!= rt_texture
->resource
.multisample_type
646 || (ds_texture
->resource
.multisample_type
647 && ds_texture
->resource
.multisample_quality
!= rt_texture
->resource
.multisample_quality
))
649 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
650 rt_texture
->resource
.multisample_type
, rt_texture
->resource
.multisample_quality
,
651 ds_texture
->resource
.multisample_type
, ds_texture
->resource
.multisample_quality
);
652 depth_stencil
= &ds_null
;
654 else if (depth_stencil
->resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
656 wined3d_texture_gl_set_compatible_renderbuffer(wined3d_texture_gl(ds_texture
),
657 context_gl
, ds_level
, &render_targets
[0]);
661 wined3d_context_gl_generate_fbo_key(context_gl
, &fbo_key
,
662 render_targets
, depth_stencil
, color_location
, ds_location
);
666 struct wined3d_resource
*resource
;
667 unsigned int width
, height
;
668 const char *resource_type
;
670 TRACE("Dumping FBO attachments:\n");
671 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
673 if ((resource
= render_targets
[i
].resource
))
675 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
677 width
= resource
->size
;
679 resource_type
= "buffer";
683 rt_texture
= wined3d_texture_from_resource(resource
);
684 rt_level
= render_targets
[i
].sub_resource_idx
% rt_texture
->level_count
;
685 width
= wined3d_texture_get_level_pow2_width(rt_texture
, rt_level
);
686 height
= wined3d_texture_get_level_pow2_height(rt_texture
, rt_level
);
687 resource_type
= "texture";
690 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
691 i
, resource
, render_targets
[i
].sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
692 fbo_key
.rb_namespace
& (1 << (i
+ 1)) ? "renderbuffer" : resource_type
,
693 fbo_key
.objects
[i
+ 1].object
, width
, height
, resource
->multisample_type
);
696 if ((resource
= depth_stencil
->resource
))
698 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
700 width
= resource
->size
;
702 resource_type
= "buffer";
706 ds_texture
= wined3d_texture_from_resource(resource
);
707 ds_level
= depth_stencil
->sub_resource_idx
% ds_texture
->level_count
;
708 width
= wined3d_texture_get_level_pow2_width(ds_texture
, ds_level
);
709 height
= wined3d_texture_get_level_pow2_height(ds_texture
, ds_level
);
710 resource_type
= "texture";
713 TRACE(" Depth attachment: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
714 resource
, depth_stencil
->sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
715 fbo_key
.rb_namespace
& (1 << 0) ? "renderbuffer" : resource_type
,
716 fbo_key
.objects
[0].object
, width
, height
, resource
->multisample_type
);
720 LIST_FOR_EACH_ENTRY(entry
, &context_gl
->fbo_list
, struct fbo_entry
, entry
)
722 if (memcmp(&fbo_key
, &entry
->key
, sizeof(fbo_key
)))
725 list_remove(&entry
->entry
);
726 list_add_head(&context_gl
->fbo_list
, &entry
->entry
);
730 if (context_gl
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
732 entry
= wined3d_context_gl_create_fbo_entry(context_gl
,
733 render_targets
, depth_stencil
, color_location
, ds_location
);
734 list_add_head(&context_gl
->fbo_list
, &entry
->entry
);
735 ++context_gl
->fbo_entry_count
;
739 entry
= LIST_ENTRY(list_tail(&context_gl
->fbo_list
), struct fbo_entry
, entry
);
740 wined3d_context_gl_reuse_fbo_entry(context_gl
, target
, render_targets
,
741 depth_stencil
, color_location
, ds_location
, entry
);
742 list_remove(&entry
->entry
);
743 list_add_head(&context_gl
->fbo_list
, &entry
->entry
);
749 /* Context activation is done by the caller. */
750 static void wined3d_context_gl_apply_fbo_entry(struct wined3d_context_gl
*context_gl
,
751 GLenum target
, struct fbo_entry
*entry
)
753 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
754 GLuint read_binding
, draw_binding
;
757 if (entry
->flags
& WINED3D_FBO_ENTRY_FLAG_ATTACHED
)
759 wined3d_context_gl_bind_fbo(context_gl
, target
, entry
->id
);
763 read_binding
= context_gl
->fbo_read_binding
;
764 draw_binding
= context_gl
->fbo_draw_binding
;
765 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, entry
->id
);
767 if (gl_info
->supported
[ARB_FRAMEBUFFER_NO_ATTACHMENTS
])
769 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
,
770 GL_FRAMEBUFFER_DEFAULT_WIDTH
, gl_info
->limits
.framebuffer_width
));
771 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
,
772 GL_FRAMEBUFFER_DEFAULT_HEIGHT
, gl_info
->limits
.framebuffer_height
));
773 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
, GL_FRAMEBUFFER_DEFAULT_LAYERS
, 1));
774 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
, GL_FRAMEBUFFER_DEFAULT_SAMPLES
, 1));
777 /* Apply render targets */
778 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
780 wined3d_context_gl_attach_surface_fbo(context_gl
, target
, i
,
781 &entry
->key
.objects
[i
+ 1], entry
->key
.rb_namespace
& (1 << (i
+ 1)));
784 wined3d_context_gl_attach_depth_stencil_fbo(context_gl
, target
,
785 &entry
->key
.objects
[0], entry
->key
.rb_namespace
& 0x1, entry
->flags
);
787 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
788 * GL contexts requirements. */
789 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
790 wined3d_context_gl_set_draw_buffer(context_gl
, GL_NONE
);
791 if (target
!= GL_FRAMEBUFFER
)
793 if (target
== GL_READ_FRAMEBUFFER
)
794 wined3d_context_gl_bind_fbo(context_gl
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
796 wined3d_context_gl_bind_fbo(context_gl
, GL_READ_FRAMEBUFFER
, read_binding
);
799 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_ATTACHED
;
802 /* Context activation is done by the caller. */
803 static void wined3d_context_gl_apply_fbo_state(struct wined3d_context_gl
*context_gl
, GLenum target
,
804 const struct wined3d_rendertarget_info
*render_targets
,
805 const struct wined3d_rendertarget_info
*depth_stencil
, DWORD color_location
, DWORD ds_location
)
807 struct fbo_entry
*entry
, *entry2
;
809 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_destroy_list
, struct fbo_entry
, entry
)
811 wined3d_context_gl_destroy_fbo_entry(context_gl
, entry
);
814 if (context_gl
->rebind_fbo
)
816 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
817 context_gl
->rebind_fbo
= FALSE
;
820 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
822 context_gl
->current_fbo
= NULL
;
823 wined3d_context_gl_bind_fbo(context_gl
, target
, 0);
827 context_gl
->current_fbo
= wined3d_context_gl_find_fbo_entry(context_gl
,
828 target
, render_targets
, depth_stencil
, color_location
, ds_location
);
829 wined3d_context_gl_apply_fbo_entry(context_gl
, target
, context_gl
->current_fbo
);
833 /* Context activation is done by the caller. */
834 void wined3d_context_gl_apply_fbo_state_blit(struct wined3d_context_gl
*context_gl
, GLenum target
,
835 struct wined3d_resource
*rt
, unsigned int rt_sub_resource_idx
,
836 struct wined3d_resource
*ds
, unsigned int ds_sub_resource_idx
, DWORD location
)
838 struct wined3d_rendertarget_info ds_info
= {{0}};
840 memset(context_gl
->blit_targets
, 0, sizeof(context_gl
->blit_targets
));
843 context_gl
->blit_targets
[0].resource
= rt
;
844 context_gl
->blit_targets
[0].sub_resource_idx
= rt_sub_resource_idx
;
845 context_gl
->blit_targets
[0].layer_count
= 1;
850 ds_info
.resource
= ds
;
851 ds_info
.sub_resource_idx
= ds_sub_resource_idx
;
852 ds_info
.layer_count
= 1;
855 wined3d_context_gl_apply_fbo_state(context_gl
, target
, context_gl
->blit_targets
, &ds_info
, location
, location
);
858 /* Context activation is done by the caller. */
859 void wined3d_context_gl_alloc_occlusion_query(struct wined3d_context_gl
*context_gl
,
860 struct wined3d_occlusion_query
*query
)
862 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
864 if (context_gl
->free_occlusion_query_count
)
866 query
->id
= context_gl
->free_occlusion_queries
[--context_gl
->free_occlusion_query_count
];
870 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
872 GL_EXTCALL(glGenQueries(1, &query
->id
));
873 checkGLcall("glGenQueries");
875 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context_gl
);
879 WARN("Occlusion queries not supported, not allocating query id.\n");
884 query
->context_gl
= context_gl
;
885 list_add_head(&context_gl
->occlusion_queries
, &query
->entry
);
888 void wined3d_context_gl_free_occlusion_query(struct wined3d_occlusion_query
*query
)
890 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
892 list_remove(&query
->entry
);
893 query
->context_gl
= NULL
;
895 if (!wined3d_array_reserve((void **)&context_gl
->free_occlusion_queries
,
896 &context_gl
->free_occlusion_query_size
, context_gl
->free_occlusion_query_count
+ 1,
897 sizeof(*context_gl
->free_occlusion_queries
)))
899 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context_gl
);
903 context_gl
->free_occlusion_queries
[context_gl
->free_occlusion_query_count
++] = query
->id
;
906 /* Context activation is done by the caller. */
907 void wined3d_context_gl_alloc_fence(struct wined3d_context_gl
*context_gl
, struct wined3d_fence
*fence
)
909 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
911 if (context_gl
->free_fence_count
)
913 fence
->object
= context_gl
->free_fences
[--context_gl
->free_fence_count
];
917 if (gl_info
->supported
[ARB_SYNC
])
919 /* Using ARB_sync, not much to do here. */
920 fence
->object
.sync
= NULL
;
921 TRACE("Allocated sync object in context %p.\n", context_gl
);
923 else if (gl_info
->supported
[APPLE_FENCE
])
925 GL_EXTCALL(glGenFencesAPPLE(1, &fence
->object
.id
));
926 checkGLcall("glGenFencesAPPLE");
928 TRACE("Allocated fence %u in context %p.\n", fence
->object
.id
, context_gl
);
930 else if(gl_info
->supported
[NV_FENCE
])
932 GL_EXTCALL(glGenFencesNV(1, &fence
->object
.id
));
933 checkGLcall("glGenFencesNV");
935 TRACE("Allocated fence %u in context %p.\n", fence
->object
.id
, context_gl
);
939 WARN("Fences not supported, not allocating fence.\n");
940 fence
->object
.id
= 0;
944 fence
->context_gl
= context_gl
;
945 list_add_head(&context_gl
->fences
, &fence
->entry
);
948 void wined3d_context_gl_free_fence(struct wined3d_fence
*fence
)
950 struct wined3d_context_gl
*context_gl
= fence
->context_gl
;
952 list_remove(&fence
->entry
);
953 fence
->context_gl
= NULL
;
955 if (!wined3d_array_reserve((void **)&context_gl
->free_fences
,
956 &context_gl
->free_fence_size
, context_gl
->free_fence_count
+ 1,
957 sizeof(*context_gl
->free_fences
)))
959 ERR("Failed to grow free list, leaking fence %u in context %p.\n", fence
->object
.id
, context_gl
);
963 context_gl
->free_fences
[context_gl
->free_fence_count
++] = fence
->object
;
966 /* Context activation is done by the caller. */
967 void wined3d_context_gl_alloc_timestamp_query(struct wined3d_context_gl
*context_gl
,
968 struct wined3d_timestamp_query
*query
)
970 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
972 if (context_gl
->free_timestamp_query_count
)
974 query
->id
= context_gl
->free_timestamp_queries
[--context_gl
->free_timestamp_query_count
];
978 GL_EXTCALL(glGenQueries(1, &query
->id
));
979 checkGLcall("glGenQueries");
981 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context_gl
);
984 query
->context_gl
= context_gl
;
985 list_add_head(&context_gl
->timestamp_queries
, &query
->entry
);
988 void wined3d_context_gl_free_timestamp_query(struct wined3d_timestamp_query
*query
)
990 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
992 list_remove(&query
->entry
);
993 query
->context_gl
= NULL
;
995 if (!wined3d_array_reserve((void **)&context_gl
->free_timestamp_queries
,
996 &context_gl
->free_timestamp_query_size
, context_gl
->free_timestamp_query_count
+ 1,
997 sizeof(*context_gl
->free_timestamp_queries
)))
999 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context_gl
);
1003 context_gl
->free_timestamp_queries
[context_gl
->free_timestamp_query_count
++] = query
->id
;
1006 void wined3d_context_gl_alloc_so_statistics_query(struct wined3d_context_gl
*context_gl
,
1007 struct wined3d_so_statistics_query
*query
)
1009 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1011 if (context_gl
->free_so_statistics_query_count
)
1013 query
->u
= context_gl
->free_so_statistics_queries
[--context_gl
->free_so_statistics_query_count
];
1017 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
1018 checkGLcall("glGenQueries");
1020 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
1021 query
->u
.id
[0], query
->u
.id
[1], context_gl
);
1024 query
->context_gl
= context_gl
;
1025 list_add_head(&context_gl
->so_statistics_queries
, &query
->entry
);
1028 void wined3d_context_gl_free_so_statistics_query(struct wined3d_so_statistics_query
*query
)
1030 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
1032 list_remove(&query
->entry
);
1033 query
->context_gl
= NULL
;
1035 if (!wined3d_array_reserve((void **)&context_gl
->free_so_statistics_queries
,
1036 &context_gl
->free_so_statistics_query_size
, context_gl
->free_so_statistics_query_count
+ 1,
1037 sizeof(*context_gl
->free_so_statistics_queries
)))
1039 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
1040 query
->u
.id
[0], query
->u
.id
[1], context_gl
);
1044 context_gl
->free_so_statistics_queries
[context_gl
->free_so_statistics_query_count
++] = query
->u
;
1047 void wined3d_context_gl_alloc_pipeline_statistics_query(struct wined3d_context_gl
*context_gl
,
1048 struct wined3d_pipeline_statistics_query
*query
)
1050 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1052 if (context_gl
->free_pipeline_statistics_query_count
)
1054 query
->u
= context_gl
->free_pipeline_statistics_queries
[--context_gl
->free_pipeline_statistics_query_count
];
1058 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
1059 checkGLcall("glGenQueries");
1062 query
->context_gl
= context_gl
;
1063 list_add_head(&context_gl
->pipeline_statistics_queries
, &query
->entry
);
1066 void wined3d_context_gl_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query
*query
)
1068 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
1070 list_remove(&query
->entry
);
1071 query
->context_gl
= NULL
;
1073 if (!wined3d_array_reserve((void **)&context_gl
->free_pipeline_statistics_queries
,
1074 &context_gl
->free_pipeline_statistics_query_size
, context_gl
->free_pipeline_statistics_query_count
+ 1,
1075 sizeof(*context_gl
->free_pipeline_statistics_queries
)))
1077 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context_gl
);
1081 context_gl
->free_pipeline_statistics_queries
[context_gl
->free_pipeline_statistics_query_count
++] = query
->u
;
1084 typedef void (context_fbo_entry_func_t
)(struct wined3d_context_gl
*context_gl
, struct fbo_entry
*entry
);
1086 static void wined3d_context_gl_enum_fbo_entries(const struct wined3d_device
*device
,
1087 GLuint name
, BOOL rb_namespace
, context_fbo_entry_func_t
*callback
)
1091 for (i
= 0; i
< device
->context_count
; ++i
)
1093 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(device
->contexts
[i
]);
1094 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1095 struct fbo_entry
*entry
, *entry2
;
1097 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_list
, struct fbo_entry
, entry
)
1099 for (j
= 0; j
< gl_info
->limits
.buffers
+ 1; ++j
)
1101 if (entry
->key
.objects
[j
].object
== name
1102 && !(entry
->key
.rb_namespace
& (1 << j
)) == !rb_namespace
)
1104 callback(context_gl
, entry
);
1112 static void wined3d_context_gl_queue_fbo_entry_destruction(struct wined3d_context_gl
*context_gl
,
1113 struct fbo_entry
*entry
)
1115 list_remove(&entry
->entry
);
1116 list_add_head(&context_gl
->fbo_destroy_list
, &entry
->entry
);
1119 void context_gl_resource_released(struct wined3d_device
*device
, GLuint name
, BOOL rb_namespace
)
1121 wined3d_context_gl_enum_fbo_entries(device
, name
, rb_namespace
,
1122 wined3d_context_gl_queue_fbo_entry_destruction
);
1125 void wined3d_context_gl_texture_update(struct wined3d_context_gl
*context_gl
,
1126 const struct wined3d_texture_gl
*texture_gl
)
1128 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1129 struct fbo_entry
*entry
= context_gl
->current_fbo
;
1132 if (!entry
|| context_gl
->rebind_fbo
)
1135 for (i
= 0; i
< gl_info
->limits
.buffers
+ 1; ++i
)
1137 if (texture_gl
->texture_rgb
.name
== entry
->key
.objects
[i
].object
1138 || texture_gl
->texture_srgb
.name
== entry
->key
.objects
[i
].object
)
1140 TRACE("Updated texture %p is bound as attachment %u to the current FBO.\n", texture_gl
, i
);
1141 context_gl
->rebind_fbo
= TRUE
;
1147 static BOOL
wined3d_context_gl_restore_pixel_format(struct wined3d_context_gl
*context_gl
)
1149 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1152 if (context_gl
->restore_pf
&& IsWindow(context_gl
->restore_pf_win
))
1154 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1156 HDC dc
= GetDCEx(context_gl
->restore_pf_win
, 0, DCX_USESTYLE
| DCX_CACHE
);
1159 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, context_gl
->restore_pf
))))
1161 ERR("Failed to restore pixel format %d on window %p.\n",
1162 context_gl
->restore_pf
, context_gl
->restore_pf_win
);
1164 ReleaseDC(context_gl
->restore_pf_win
, dc
);
1169 ERR("Unable to restore pixel format %d on window %p.\n",
1170 context_gl
->restore_pf
, context_gl
->restore_pf_win
);
1174 context_gl
->restore_pf
= 0;
1175 context_gl
->restore_pf_win
= NULL
;
1179 static BOOL
wined3d_context_gl_set_pixel_format(struct wined3d_context_gl
*context_gl
)
1181 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1182 BOOL
private = context_gl
->dc_is_private
;
1183 int format
= context_gl
->pixel_format
;
1184 HDC dc
= context_gl
->dc
;
1188 if (private && context_gl
->dc_has_format
)
1191 if (!private && WindowFromDC(dc
) != context_gl
->window
)
1194 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
1195 if (current
== format
) goto success
;
1197 /* By default WGL doesn't allow pixel format adjustments but we need it
1198 * here. For this reason there's a Wine specific wglSetPixelFormat()
1199 * which allows us to set the pixel format multiple times. Use it when we
1200 * can, because even though no pixel format may currently be set, the
1201 * application may try to set one later. */
1202 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1204 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
1206 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1213 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1214 * continue using the old format. There's a big chance that the old
1215 * format works although with a performance hit and perhaps rendering
1217 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1218 format
, dc
, current
);
1221 else if (!SetPixelFormat(dc
, format
, NULL
))
1223 /* This may also happen if the dc belongs to a destroyed window. */
1224 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1225 format
, dc
, GetLastError());
1229 win
= private ? NULL
: WindowFromDC(dc
);
1230 if (win
!= context_gl
->restore_pf_win
)
1231 wined3d_context_gl_restore_pixel_format(context_gl
);
1232 context_gl
->restore_pf
= private ? 0 : current
;
1233 context_gl
->restore_pf_win
= win
;
1237 context_gl
->dc_has_format
= TRUE
;
1241 static BOOL
wined3d_context_gl_set_gl_context(struct wined3d_context_gl
*context_gl
)
1243 struct wined3d_swapchain_gl
*swapchain_gl
= wined3d_swapchain_gl(context_gl
->c
.swapchain
);
1244 BOOL backup
= FALSE
;
1246 if (!wined3d_context_gl_set_pixel_format(context_gl
))
1248 WARN("Failed to set pixel format %d on device context %p.\n",
1249 context_gl
->pixel_format
, context_gl
->dc
);
1253 if (backup
|| !wglMakeCurrent(context_gl
->dc
, context_gl
->gl_ctx
))
1255 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1256 context_gl
->gl_ctx
, context_gl
->dc
, GetLastError());
1257 context_gl
->valid
= 0;
1258 WARN("Trying fallback to the backup window.\n");
1260 /* FIXME: If the context is destroyed it's no longer associated with
1261 * a swapchain, so we can't use the swapchain to get a backup dc. To
1262 * make this work windowless contexts would need to be handled by the
1264 if (context_gl
->c
.destroyed
|| !swapchain_gl
)
1266 FIXME("Unable to get backup dc for destroyed context %p.\n", context_gl
);
1267 wined3d_context_gl_set_current(NULL
);
1271 if (!(context_gl
->dc
= wined3d_swapchain_gl_get_backup_dc(swapchain_gl
)))
1273 wined3d_context_gl_set_current(NULL
);
1277 context_gl
->dc_is_private
= TRUE
;
1278 context_gl
->dc_has_format
= FALSE
;
1280 if (!wined3d_context_gl_set_pixel_format(context_gl
))
1282 ERR("Failed to set pixel format %d on device context %p.\n",
1283 context_gl
->pixel_format
, context_gl
->dc
);
1284 wined3d_context_gl_set_current(NULL
);
1288 if (!wglMakeCurrent(context_gl
->dc
, context_gl
->gl_ctx
))
1290 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1291 context_gl
->dc
, GetLastError());
1292 wined3d_context_gl_set_current(NULL
);
1296 context_gl
->valid
= 1;
1298 context_gl
->needs_set
= 0;
1303 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
1305 if (!wglMakeCurrent(dc
, gl_ctx
))
1307 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1308 gl_ctx
, dc
, GetLastError());
1309 wined3d_context_gl_set_current(NULL
);
1313 static void wined3d_context_gl_update_window(struct wined3d_context_gl
*context_gl
)
1315 if (!context_gl
->c
.swapchain
)
1318 if (context_gl
->window
== context_gl
->c
.swapchain
->win_handle
)
1321 TRACE("Updating context %p window from %p to %p.\n",
1322 context_gl
, context_gl
->window
, context_gl
->c
.swapchain
->win_handle
);
1325 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
1327 context_gl
->window
= context_gl
->c
.swapchain
->win_handle
;
1328 context_gl
->dc_is_private
= FALSE
;
1329 context_gl
->dc_has_format
= FALSE
;
1330 context_gl
->needs_set
= 1;
1331 context_gl
->valid
= 1;
1333 if (!(context_gl
->dc
= GetDCEx(context_gl
->window
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1335 ERR("Failed to get a device context for window %p.\n", context_gl
->window
);
1336 context_gl
->valid
= 0;
1340 static void wined3d_context_gl_cleanup(struct wined3d_context_gl
*context_gl
)
1342 struct wined3d_pipeline_statistics_query
*pipeline_statistics_query
;
1343 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1344 struct wined3d_so_statistics_query
*so_statistics_query
;
1345 struct wined3d_timestamp_query
*timestamp_query
;
1346 struct wined3d_occlusion_query
*occlusion_query
;
1347 struct wined3d_context_gl
*current
;
1348 struct fbo_entry
*entry
, *entry2
;
1349 struct wined3d_fence
*fence
;
1354 restore_ctx
= wglGetCurrentContext();
1355 restore_dc
= wglGetCurrentDC();
1357 if (context_gl
->valid
&& context_gl
->gl_ctx
!= restore_ctx
)
1359 /* Attempting to restore a GL context corresponding to a wined3d
1360 * context is not particularly useful. Worse, when we're called from
1361 * wined3d_context_gl_destroy(), we subsequently clear the "current
1362 * D3D context" TLS value, which would cause
1363 * wined3d_context_gl_enter() to consider the GL context a non-D3D
1365 if ((current
= wined3d_context_gl_get_current()) && current
->gl_ctx
== restore_ctx
)
1367 wined3d_context_gl_set_gl_context(context_gl
);
1374 if (context_gl
->valid
)
1376 /* If we're here because we're switching away from a previously
1377 * destroyed context, acquiring a context in order to submit a fence
1378 * is problematic. (In particular, we'd end up back here again in the
1379 * process of switching to the newly acquired context.) */
1380 if (context_gl
->c
.destroyed
)
1382 gl_info
->gl_ops
.gl
.p_glFinish();
1386 wined3d_context_gl_submit_command_fence(context_gl
);
1387 wined3d_context_gl_wait_command_fence(context_gl
,
1388 wined3d_device_gl(context_gl
->c
.device
)->current_fence_id
- 1);
1391 if (context_gl
->dummy_arbfp_prog
)
1392 GL_EXTCALL(glDeleteProgramsARB(1, &context_gl
->dummy_arbfp_prog
));
1394 if (context_gl
->blit_vbo
)
1395 GL_EXTCALL(glDeleteBuffers(1, &context_gl
->blit_vbo
));
1397 for (i
= 0; i
< context_gl
->free_pipeline_statistics_query_count
; ++i
)
1399 union wined3d_gl_pipeline_statistics_query
*q
= &context_gl
->free_pipeline_statistics_queries
[i
];
1400 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1403 for (i
= 0; i
< context_gl
->free_so_statistics_query_count
; ++i
)
1405 union wined3d_gl_so_statistics_query
*q
= &context_gl
->free_so_statistics_queries
[i
];
1406 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1409 if (context_gl
->free_timestamp_query_count
)
1410 GL_EXTCALL(glDeleteQueries(context_gl
->free_timestamp_query_count
, context_gl
->free_timestamp_queries
));
1412 if (gl_info
->supported
[ARB_SYNC
])
1414 for (i
= 0; i
< context_gl
->free_fence_count
; ++i
)
1416 GL_EXTCALL(glDeleteSync(context_gl
->free_fences
[i
].sync
));
1419 else if (gl_info
->supported
[APPLE_FENCE
])
1421 for (i
= 0; i
< context_gl
->free_fence_count
; ++i
)
1423 GL_EXTCALL(glDeleteFencesAPPLE(1, &context_gl
->free_fences
[i
].id
));
1426 else if (gl_info
->supported
[NV_FENCE
])
1428 for (i
= 0; i
< context_gl
->free_fence_count
; ++i
)
1430 GL_EXTCALL(glDeleteFencesNV(1, &context_gl
->free_fences
[i
].id
));
1434 if (context_gl
->free_occlusion_query_count
)
1435 GL_EXTCALL(glDeleteQueries(context_gl
->free_occlusion_query_count
, context_gl
->free_occlusion_queries
));
1437 checkGLcall("context cleanup");
1439 heap_free(context_gl
->submitted
.fences
);
1440 heap_free(context_gl
->free_pipeline_statistics_queries
);
1441 heap_free(context_gl
->free_so_statistics_queries
);
1442 heap_free(context_gl
->free_timestamp_queries
);
1443 heap_free(context_gl
->free_fences
);
1444 heap_free(context_gl
->free_occlusion_queries
);
1446 LIST_FOR_EACH_ENTRY(pipeline_statistics_query
, &context_gl
->pipeline_statistics_queries
,
1447 struct wined3d_pipeline_statistics_query
, entry
)
1449 if (context_gl
->valid
)
1450 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query
->u
.id
), pipeline_statistics_query
->u
.id
));
1451 pipeline_statistics_query
->context_gl
= NULL
;
1454 LIST_FOR_EACH_ENTRY(so_statistics_query
, &context_gl
->so_statistics_queries
,
1455 struct wined3d_so_statistics_query
, entry
)
1457 if (context_gl
->valid
)
1458 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query
->u
.id
), so_statistics_query
->u
.id
));
1459 so_statistics_query
->context_gl
= NULL
;
1462 LIST_FOR_EACH_ENTRY(timestamp_query
, &context_gl
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
1464 if (context_gl
->valid
)
1465 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
1466 timestamp_query
->context_gl
= NULL
;
1469 LIST_FOR_EACH_ENTRY(fence
, &context_gl
->fences
, struct wined3d_fence
, entry
)
1471 if (context_gl
->valid
)
1473 if (gl_info
->supported
[ARB_SYNC
])
1475 if (fence
->object
.sync
)
1476 GL_EXTCALL(glDeleteSync(fence
->object
.sync
));
1478 else if (gl_info
->supported
[APPLE_FENCE
])
1480 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence
->object
.id
));
1482 else if (gl_info
->supported
[NV_FENCE
])
1484 GL_EXTCALL(glDeleteFencesNV(1, &fence
->object
.id
));
1487 fence
->context_gl
= NULL
;
1490 LIST_FOR_EACH_ENTRY(occlusion_query
, &context_gl
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1492 if (context_gl
->valid
)
1493 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1494 occlusion_query
->context_gl
= NULL
;
1497 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_destroy_list
, struct fbo_entry
, entry
)
1499 if (!context_gl
->valid
)
1501 wined3d_context_gl_destroy_fbo_entry(context_gl
, entry
);
1504 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_list
, struct fbo_entry
, entry
)
1506 if (!context_gl
->valid
)
1508 wined3d_context_gl_destroy_fbo_entry(context_gl
, entry
);
1511 heap_free(context_gl
->texture_type
);
1513 wined3d_context_gl_restore_pixel_format(context_gl
);
1515 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1516 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1517 ERR("Failed to disable GL context.\n");
1519 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
1521 if (!wglDeleteContext(context_gl
->gl_ctx
))
1523 DWORD err
= GetLastError();
1524 ERR("Failed to delete GL context %p, last error %#x.\n", context_gl
->gl_ctx
, err
);
1527 wined3d_context_cleanup(&context_gl
->c
);
1530 DWORD
context_get_tls_idx(void)
1532 return wined3d_context_tls_idx
;
1535 void context_set_tls_idx(DWORD idx
)
1537 wined3d_context_tls_idx
= idx
;
1540 struct wined3d_context_gl
*wined3d_context_gl_get_current(void)
1542 return TlsGetValue(wined3d_context_tls_idx
);
1545 BOOL
wined3d_context_gl_set_current(struct wined3d_context_gl
*context_gl
)
1547 struct wined3d_context_gl
*old
= wined3d_context_gl_get_current();
1549 if (old
== context_gl
)
1551 TRACE("Already using D3D context %p.\n", context_gl
);
1557 if (old
->c
.destroyed
)
1559 TRACE("Switching away from destroyed context %p.\n", old
);
1560 wined3d_context_gl_cleanup(old
);
1561 heap_free((void *)old
->gl_info
);
1566 if (wglGetCurrentContext())
1568 const struct wined3d_gl_info
*gl_info
= old
->gl_info
;
1569 TRACE("Flushing context %p before switching to %p.\n", old
, context_gl
);
1570 gl_info
->gl_ops
.gl
.p_glFlush();
1578 if (!context_gl
->valid
)
1580 ERR("Trying to make invalid context %p current.\n", context_gl
);
1584 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n",
1585 context_gl
, context_gl
->gl_ctx
, context_gl
->dc
);
1586 if (!wined3d_context_gl_set_gl_context(context_gl
))
1588 context_gl
->c
.current
= 1;
1590 else if (wglGetCurrentContext())
1592 TRACE("Clearing current D3D context.\n");
1593 if (!wglMakeCurrent(NULL
, NULL
))
1595 DWORD err
= GetLastError();
1596 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1597 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1602 return TlsSetValue(wined3d_context_tls_idx
, context_gl
);
1605 void wined3d_context_gl_release(struct wined3d_context_gl
*context_gl
)
1607 TRACE("Releasing context %p, level %u.\n", context_gl
, context_gl
->level
);
1611 if (!context_gl
->level
)
1612 WARN("Context %p is not active.\n", context_gl
);
1613 else if (context_gl
!= wined3d_context_gl_get_current())
1614 WARN("Context %p is not the current context.\n", context_gl
);
1617 if (!--context_gl
->level
)
1619 if (wined3d_context_gl_restore_pixel_format(context_gl
))
1620 context_gl
->needs_set
= 1;
1621 if (context_gl
->restore_ctx
)
1623 TRACE("Restoring GL context %p on device context %p.\n", context_gl
->restore_ctx
, context_gl
->restore_dc
);
1624 context_restore_gl_context(context_gl
->gl_info
, context_gl
->restore_dc
, context_gl
->restore_ctx
);
1625 context_gl
->restore_ctx
= NULL
;
1626 context_gl
->restore_dc
= NULL
;
1629 if (context_gl
->c
.destroy_delayed
)
1631 TRACE("Destroying context %p.\n", context_gl
);
1632 wined3d_context_gl_destroy(context_gl
);
1637 static void wined3d_context_gl_enter(struct wined3d_context_gl
*context_gl
)
1639 TRACE("Entering context %p, level %u.\n", context_gl
, context_gl
->level
+ 1);
1641 if (!context_gl
->level
++)
1643 const struct wined3d_context_gl
*current_context
= wined3d_context_gl_get_current();
1644 HGLRC current_gl
= wglGetCurrentContext();
1646 if (current_gl
&& (!current_context
|| current_context
->gl_ctx
!= current_gl
))
1648 TRACE("Another GL context (%p on device context %p) is already current.\n",
1649 current_gl
, wglGetCurrentDC());
1650 context_gl
->restore_ctx
= current_gl
;
1651 context_gl
->restore_dc
= wglGetCurrentDC();
1652 context_gl
->needs_set
= 1;
1654 else if (!context_gl
->needs_set
&& !(context_gl
->dc_is_private
&& context_gl
->dc_has_format
)
1655 && context_gl
->pixel_format
!= context_gl
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context_gl
->dc
))
1656 context_gl
->needs_set
= 1;
1660 /* This function takes care of wined3d pixel format selection. */
1661 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1662 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1663 bool aux_buffers
, bool swap_effect_copy
)
1665 unsigned int cfg_count
= wined3d_adapter_gl(device
->adapter
)->pixel_format_count
;
1666 unsigned int current_value
;
1667 PIXELFORMATDESCRIPTOR pfd
;
1668 int iPixelFormat
= 0;
1671 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, swap_effect_copy %#x.\n",
1672 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1673 aux_buffers
, swap_effect_copy
);
1676 for (i
= 0; i
< cfg_count
; ++i
)
1678 const struct wined3d_pixel_format
*cfg
= &wined3d_adapter_gl(device
->adapter
)->pixel_formats
[i
];
1681 /* For now only accept RGBA formats. Perhaps some day we will
1682 * allow floating point formats for pbuffers. */
1683 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1685 /* In window mode we need a window drawable format and double buffering. */
1686 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1688 if (cfg
->redSize
< color_format
->red_size
)
1690 if (cfg
->greenSize
< color_format
->green_size
)
1692 if (cfg
->blueSize
< color_format
->blue_size
)
1694 if (cfg
->alphaSize
< color_format
->alpha_size
)
1696 if (cfg
->depthSize
< ds_format
->depth_size
)
1698 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1700 /* Check multisampling support. */
1701 if (cfg
->numSamples
)
1705 if (swap_effect_copy
&& cfg
->swap_method
== WGL_SWAP_COPY_ARB
)
1707 /* We try to locate a format which matches our requirements exactly. In case of
1708 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1709 if (cfg
->depthSize
== ds_format
->depth_size
)
1711 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1713 if (cfg
->alphaSize
== color_format
->alpha_size
)
1715 /* We like to have aux buffers in backbuffer mode */
1716 if (aux_buffers
&& cfg
->auxBuffers
)
1718 if (cfg
->redSize
== color_format
->red_size
1719 && cfg
->greenSize
== color_format
->green_size
1720 && cfg
->blueSize
== color_format
->blue_size
)
1723 if (value
> current_value
)
1725 iPixelFormat
= cfg
->iPixelFormat
;
1726 current_value
= value
;
1732 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1734 memset(&pfd
, 0, sizeof(pfd
));
1735 pfd
.nSize
= sizeof(pfd
);
1737 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1738 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1739 pfd
.cAlphaBits
= color_format
->alpha_size
;
1740 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1741 + color_format
->blue_size
+ color_format
->alpha_size
;
1742 pfd
.cDepthBits
= ds_format
->depth_size
;
1743 pfd
.cStencilBits
= ds_format
->stencil_size
;
1744 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1746 if (!(iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
)))
1748 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1749 ERR("Can't find a suitable pixel format.\n");
1754 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1755 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1756 return iPixelFormat
;
1759 /* Context activation is done by the caller. */
1760 void wined3d_context_gl_bind_dummy_textures(const struct wined3d_context_gl
*context_gl
)
1762 const struct wined3d_dummy_textures
*textures
= &wined3d_device_gl(context_gl
->c
.device
)->dummy_textures
;
1763 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1766 for (i
= 0; i
< gl_info
->limits
.combined_samplers
; ++i
)
1768 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1770 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, textures
->tex_1d
);
1771 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, textures
->tex_2d
);
1773 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1774 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, textures
->tex_rect
);
1776 if (gl_info
->supported
[EXT_TEXTURE3D
])
1777 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, textures
->tex_3d
);
1779 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1780 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, textures
->tex_cube
);
1782 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
1783 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, textures
->tex_cube_array
);
1785 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
1787 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, textures
->tex_1d_array
);
1788 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, textures
->tex_2d_array
);
1791 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
1792 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, textures
->tex_buffer
);
1794 if (gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
1796 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, textures
->tex_2d_ms
);
1797 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, textures
->tex_2d_ms_array
);
1801 checkGLcall("bind dummy textures");
1804 void wined3d_check_gl_call(const struct wined3d_gl_info
*gl_info
,
1805 const char *file
, unsigned int line
, const char *name
)
1809 if (gl_info
->supported
[ARB_DEBUG_OUTPUT
] || (err
= gl_info
->gl_ops
.gl
.p_glGetError()) == GL_NO_ERROR
)
1811 TRACE("%s call ok %s / %u.\n", name
, file
, line
);
1817 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1818 debug_glerror(err
), err
, name
, file
,line
);
1819 err
= gl_info
->gl_ops
.gl
.p_glGetError();
1820 } while (err
!= GL_NO_ERROR
);
1823 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1825 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1826 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1829 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1830 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1834 case GL_DEBUG_TYPE_ERROR_ARB
:
1835 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1838 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1839 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1840 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1841 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1844 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1845 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1849 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1854 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1857 unsigned int ctx_attrib_idx
= 0;
1858 GLint ctx_attribs
[7], ctx_flags
= 0;
1860 if (context_debug_output_enabled(gl_info
))
1861 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1862 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1863 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1864 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1865 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1868 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1869 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1871 ctx_attribs
[ctx_attrib_idx
] = 0;
1873 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1875 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1879 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1880 ctx_attribs
[ctx_attrib_idx
- 1] = ctx_flags
;
1884 ctx_flags
= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1885 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1886 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1887 ctx_attribs
[ctx_attrib_idx
] = 0;
1889 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1890 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1897 static BOOL
wined3d_context_gl_create_wgl_ctx(struct wined3d_context_gl
*context_gl
,
1898 struct wined3d_swapchain_gl
*swapchain_gl
, BOOL
*new_drawable
)
1900 enum wined3d_swap_effect swap_effect
= swapchain_gl
->s
.state
.desc
.swap_effect
;
1901 const struct wined3d_format
*colour_format
, *ds_format
;
1902 struct wined3d_context
*context
= &context_gl
->c
;
1903 const struct wined3d_gl_info
*gl_info
;
1904 struct wined3d_resource
*target
;
1905 struct wined3d_adapter
*adapter
;
1906 unsigned int target_bind_flags
;
1907 struct wined3d_device
*device
;
1908 bool swap_effect_copy
;
1909 HGLRC ctx
, share_ctx
;
1912 device
= context
->device
;
1913 adapter
= device
->adapter
;
1914 gl_info
= &adapter
->gl_info
;
1916 target
= &context
->current_rt
.texture
->resource
;
1917 target_bind_flags
= target
->bind_flags
;
1919 swap_effect_copy
= swap_effect
== WINED3D_SWAP_EFFECT_COPY
|| swap_effect
== WINED3D_SWAP_EFFECT_COPY_VSYNC
;
1921 *new_drawable
= !GetPixelFormat(context_gl
->dc
);
1923 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1925 static const enum wined3d_format_id ds_formats
[] =
1927 WINED3DFMT_D24_UNORM_S8_UINT
,
1928 WINED3DFMT_D32_UNORM
,
1929 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
1930 WINED3DFMT_D16_UNORM
,
1931 WINED3DFMT_S1_UINT_D15_UNORM
,
1934 colour_format
= target
->format
;
1936 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1937 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1938 if (colour_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1939 colour_format
= wined3d_get_format(adapter
, WINED3DFMT_B4G4R4A4_UNORM
, target_bind_flags
);
1940 else if (colour_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1941 colour_format
= wined3d_get_format(adapter
, WINED3DFMT_B8G8R8A8_UNORM
, target_bind_flags
);
1943 /* DirectDraw supports 8bit paletted render targets and these are used by
1944 * old games like StarCraft and C&C. Most modern hardware doesn't support
1945 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1946 * conversion (ab)uses the alpha component for storing the palette index.
1947 * For this reason we require a format with 8bit alpha, so request
1949 if (colour_format
->id
== WINED3DFMT_P8_UINT
)
1950 colour_format
= wined3d_get_format(adapter
, WINED3DFMT_B8G8R8A8_UNORM
, target_bind_flags
);
1952 /* Try to find a pixel format which matches our requirements. */
1953 if (!swapchain_gl
->s
.ds_format
)
1955 for (i
= 0; i
< ARRAY_SIZE(ds_formats
); ++i
)
1957 ds_format
= wined3d_get_format(adapter
, ds_formats
[i
], WINED3D_BIND_DEPTH_STENCIL
);
1958 if ((context_gl
->pixel_format
= context_choose_pixel_format(device
,
1959 context_gl
->dc
, colour_format
, ds_format
, true, swap_effect_copy
)))
1961 swapchain_gl
->s
.ds_format
= ds_format
;
1965 TRACE("Depth stencil format %s is not supported, trying next format.\n",
1966 debug_d3dformat(ds_format
->id
));
1971 context_gl
->pixel_format
= context_choose_pixel_format(device
,
1972 context_gl
->dc
, colour_format
, swapchain_gl
->s
.ds_format
, true, swap_effect_copy
);
1977 /* When using FBOs for off-screen rendering, we only use the drawable for
1978 * presentation blits, and don't do any rendering to it. That means we
1979 * don't need depth or stencil buffers, and can mostly ignore the render
1980 * target format. This wouldn't necessarily be quite correct for 10bpc
1981 * display modes, but we don't currently support those.
1982 * Using the same format regardless of the colour/depth/stencil targets
1983 * makes it much less likely that different wined3d instances will set
1984 * conflicting pixel formats. */
1985 colour_format
= wined3d_get_format(adapter
, WINED3DFMT_B8G8R8A8_UNORM
, target_bind_flags
);
1986 ds_format
= wined3d_get_format(adapter
, WINED3DFMT_UNKNOWN
, WINED3D_BIND_DEPTH_STENCIL
);
1987 context_gl
->pixel_format
= context_choose_pixel_format(device
,
1988 context_gl
->dc
, colour_format
, ds_format
, false, swap_effect_copy
);
1991 if (!context_gl
->pixel_format
)
1993 ERR("Failed to choose pixel format.\n");
1997 wined3d_context_gl_enter(context_gl
);
1999 if (!wined3d_context_gl_set_pixel_format(context_gl
))
2001 context_release(context
);
2003 if (context_gl
->dc_is_private
)
2005 ERR("Failed to set pixel format %d on device context %p.\n", context_gl
->pixel_format
, context_gl
->dc
);
2010 WARN("Failed to set pixel format %d on device context %p, trying backup DC.\n",
2011 context_gl
->pixel_format
, context_gl
->dc
);
2013 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
2014 if (!(context_gl
->dc
= wined3d_swapchain_gl_get_backup_dc(swapchain_gl
)))
2016 ERR("Failed to retrieve the backup device context.\n");
2019 context_gl
->dc_is_private
= TRUE
;
2021 return wined3d_context_gl_create_wgl_ctx(context_gl
, swapchain_gl
, new_drawable
);
2024 share_ctx
= device
->context_count
? wined3d_context_gl(device
->contexts
[0])->gl_ctx
: NULL
;
2025 if (gl_info
->p_wglCreateContextAttribsARB
)
2027 if (!(ctx
= context_create_wgl_attribs(gl_info
, context_gl
->dc
, share_ctx
)))
2029 ERR("Failed to create a WGL context.\n");
2030 context_release(context
);
2036 if (!(ctx
= wglCreateContext(context_gl
->dc
)))
2038 ERR("Failed to create a WGL context.\n");
2039 context_release(context
);
2043 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
2045 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
2046 context_release(context
);
2047 if (!wglDeleteContext(ctx
))
2048 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
2053 context_gl
->dc_has_format
= TRUE
;
2054 context_gl
->needs_set
= 1;
2055 context_gl
->valid
= 1;
2056 context_gl
->gl_ctx
= ctx
;
2061 HRESULT
wined3d_context_gl_init(struct wined3d_context_gl
*context_gl
, struct wined3d_swapchain_gl
*swapchain_gl
)
2063 struct wined3d_context
*context
= &context_gl
->c
;
2064 const struct wined3d_d3d_info
*d3d_info
;
2065 const struct wined3d_gl_info
*gl_info
;
2066 struct wined3d_device
*device
;
2070 TRACE("context_gl %p, swapchain %p.\n", context_gl
, swapchain_gl
);
2072 wined3d_context_init(&context_gl
->c
, &swapchain_gl
->s
);
2074 device
= context
->device
;
2075 gl_info
= &device
->adapter
->gl_info
;
2076 context_gl
->gl_info
= gl_info
;
2077 d3d_info
= context
->d3d_info
;
2079 context_gl
->tid
= GetCurrentThreadId();
2080 context_gl
->window
= context
->swapchain
->win_handle
;
2081 if (context_gl
->window
== GetDesktopWindow())
2083 TRACE("Swapchain is created on the desktop window, trying backup device context.\n");
2084 context_gl
->dc
= NULL
;
2086 else if (!(context_gl
->dc
= GetDCEx(context_gl
->window
, 0, DCX_USESTYLE
| DCX_CACHE
)))
2087 WARN("Failed to retrieve device context, trying swapchain backup.\n");
2089 if (!context_gl
->dc
)
2091 if (!(context_gl
->dc
= wined3d_swapchain_gl_get_backup_dc(swapchain_gl
)))
2093 ERR("Failed to retrieve a device context.\n");
2096 context_gl
->dc_is_private
= TRUE
;
2099 list_init(&context_gl
->fbo_list
);
2100 list_init(&context_gl
->fbo_destroy_list
);
2102 list_init(&context_gl
->occlusion_queries
);
2103 list_init(&context_gl
->fences
);
2104 list_init(&context_gl
->timestamp_queries
);
2105 list_init(&context_gl
->so_statistics_queries
);
2106 list_init(&context_gl
->pipeline_statistics_queries
);
2108 for (i
= 0; i
< ARRAY_SIZE(context_gl
->tex_unit_map
); ++i
)
2109 context_gl
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2110 for (i
= 0; i
< ARRAY_SIZE(context_gl
->rev_tex_unit_map
); ++i
)
2111 context_gl
->rev_tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2112 if (gl_info
->limits
.graphics_samplers
>= WINED3D_MAX_COMBINED_SAMPLERS
)
2114 /* Initialize the texture unit mapping to a 1:1 mapping. */
2115 unsigned int base
, count
;
2117 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_PIXEL
, &base
, &count
);
2118 if (base
+ WINED3D_MAX_FRAGMENT_SAMPLERS
> ARRAY_SIZE(context_gl
->rev_tex_unit_map
))
2120 ERR("Unexpected texture unit base index %u.\n", base
);
2123 for (i
= 0; i
< min(count
, WINED3D_MAX_FRAGMENT_SAMPLERS
); ++i
)
2125 context_gl
->tex_unit_map
[i
] = base
+ i
;
2126 context_gl
->rev_tex_unit_map
[base
+ i
] = i
;
2129 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_VERTEX
, &base
, &count
);
2130 if (base
+ WINED3D_MAX_VERTEX_SAMPLERS
> ARRAY_SIZE(context_gl
->rev_tex_unit_map
))
2132 ERR("Unexpected texture unit base index %u.\n", base
);
2135 for (i
= 0; i
< min(count
, WINED3D_MAX_VERTEX_SAMPLERS
); ++i
)
2137 context_gl
->tex_unit_map
[WINED3D_MAX_FRAGMENT_SAMPLERS
+ i
] = base
+ i
;
2138 context_gl
->rev_tex_unit_map
[base
+ i
] = WINED3D_MAX_FRAGMENT_SAMPLERS
+ i
;
2142 if (!(context_gl
->texture_type
= heap_calloc(gl_info
->limits
.combined_samplers
,
2143 sizeof(*context_gl
->texture_type
))))
2146 if (!wined3d_context_gl_create_wgl_ctx(context_gl
, swapchain_gl
, &new_drawable
))
2149 /* Set up the context defaults. */
2151 context
->render_offscreen
= wined3d_resource_is_offscreen(&context
->current_rt
.texture
->resource
);
2152 context_gl
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
2154 if (!wined3d_context_gl_set_current(context_gl
))
2156 ERR("Cannot activate context to set up defaults.\n");
2157 context_release(context
);
2158 if (!wglDeleteContext(context_gl
->gl_ctx
))
2159 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context_gl
->gl_ctx
, GetLastError());
2163 if (context_debug_output_enabled(gl_info
))
2165 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, context
));
2166 if (TRACE_ON(d3d_sync
))
2167 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
2168 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
2171 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
2172 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2176 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
2177 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2178 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
2179 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2180 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
2181 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2183 if (WARN_ON(d3d_perf
))
2185 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
2186 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2190 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2191 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &context_gl
->aux_buffers
);
2193 TRACE("Setting up the screen\n");
2195 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2197 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
2198 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2200 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
2201 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2203 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
2204 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2210 GL_EXTCALL(glGenVertexArrays(1, &vao
));
2211 GL_EXTCALL(glBindVertexArray(vao
));
2212 checkGLcall("creating VAO");
2215 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
2216 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2217 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
2218 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2220 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
2222 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2223 * the previous texture where to source the offset from is always unit - 1.
2225 for (i
= 1; i
< gl_info
->limits
.textures
; ++i
)
2227 wined3d_context_gl_active_texture(context_gl
, gl_info
, i
);
2228 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
2229 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ i
- 1);
2230 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2233 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
2235 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2236 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2237 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2238 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2241 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2242 * program and the dummy program is destroyed when the context is destroyed.
2244 static const char dummy_program
[] =
2246 "MOV result.color, fragment.color.primary;\n"
2248 GL_EXTCALL(glGenProgramsARB(1, &context_gl
->dummy_arbfp_prog
));
2249 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, context_gl
->dummy_arbfp_prog
));
2250 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
,
2251 GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
2254 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2256 for (i
= 0; i
< gl_info
->limits
.textures
; ++i
)
2258 wined3d_context_gl_active_texture(context_gl
, gl_info
, i
);
2259 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
2260 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2264 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
2266 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
2268 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
2270 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
2272 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_NO_PRIMITIVE_RESTART
))
2274 if (gl_info
->supported
[ARB_ES3_COMPATIBILITY
])
2276 gl_info
->gl_ops
.gl
.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX
);
2277 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2281 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2284 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_CUBEMAP_FILTERING
)
2285 && gl_info
->supported
[ARB_SEAMLESS_CUBE_MAP
])
2287 gl_info
->gl_ops
.gl
.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
2288 checkGLcall("enable seamless cube map filtering");
2290 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
2291 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN
, GL_LOWER_LEFT
));
2293 /* If this happens to be the first context for the device, dummy textures
2294 * are not created yet. In that case, they will be created (and bound) by
2295 * create_dummy_textures right after this context is initialized. */
2296 if (wined3d_device_gl(device
)->dummy_textures
.tex_2d
)
2297 wined3d_context_gl_bind_dummy_textures(context_gl
);
2299 /* Initialise all rectangles to avoid resetting unused ones later. */
2300 gl_info
->gl_ops
.gl
.p_glScissor(0, 0, 0, 0);
2301 checkGLcall("glScissor");
2305 gl_info
->gl_ops
.gl
.p_glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
2306 gl_info
->gl_ops
.gl
.p_glClearDepth(1.0f
);
2307 gl_info
->gl_ops
.gl
.p_glClearStencil(0);
2308 gl_info
->gl_ops
.gl
.p_glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
2309 checkGLcall("glClear");
2314 heap_free(context_gl
->texture_type
);
2315 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
2319 void wined3d_context_gl_destroy(struct wined3d_context_gl
*context_gl
)
2321 struct wined3d_device
*device
= context_gl
->c
.device
;
2323 TRACE("Destroying context %p.\n", context_gl
);
2325 wined3d_from_cs(device
->cs
);
2327 /* We delay destroying a context when it is active. The context_release()
2328 * function invokes wined3d_context_gl_destroy() again while leaving the
2330 if (context_gl
->level
)
2332 TRACE("Delaying destruction of context %p.\n", context_gl
);
2333 context_gl
->c
.destroy_delayed
= 1;
2334 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2335 context_gl
->c
.swapchain
= NULL
;
2339 device_context_remove(device
, &context_gl
->c
);
2341 if (context_gl
->c
.current
&& context_gl
->tid
!= GetCurrentThreadId())
2343 struct wined3d_gl_info
*gl_info
;
2345 /* Make a copy of gl_info for wined3d_context_gl_cleanup() use, the
2346 * one in wined3d_adapter may go away in the meantime. */
2347 gl_info
= heap_alloc(sizeof(*gl_info
));
2348 *gl_info
= *context_gl
->gl_info
;
2349 context_gl
->gl_info
= gl_info
;
2350 context_gl
->c
.destroyed
= 1;
2355 wined3d_context_gl_cleanup(context_gl
);
2356 TlsSetValue(context_get_tls_idx(), NULL
);
2357 heap_free(context_gl
);
2360 const unsigned int *wined3d_context_gl_get_tex_unit_mapping(const struct wined3d_context_gl
*context_gl
,
2361 const struct wined3d_shader_version
*shader_version
, unsigned int *base
, unsigned int *count
)
2363 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2365 if (!shader_version
)
2368 *count
= WINED3D_MAX_TEXTURES
;
2369 return context_gl
->tex_unit_map
;
2372 if (shader_version
->major
>= 4)
2374 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, shader_version
->type
, base
, count
);
2378 switch (shader_version
->type
)
2380 case WINED3D_SHADER_TYPE_PIXEL
:
2382 *count
= WINED3D_MAX_FRAGMENT_SAMPLERS
;
2384 case WINED3D_SHADER_TYPE_VERTEX
:
2385 *base
= WINED3D_MAX_FRAGMENT_SAMPLERS
;
2386 *count
= WINED3D_MAX_VERTEX_SAMPLERS
;
2389 ERR("Unhandled shader type %#x.\n", shader_version
->type
);
2394 return context_gl
->tex_unit_map
;
2397 static void wined3d_context_gl_get_rt_size(const struct wined3d_context_gl
*context_gl
, SIZE
*size
)
2399 const struct wined3d_texture
*rt
= context_gl
->c
.current_rt
.texture
;
2406 GetClientRect(context_gl
->window
, &window_size
);
2407 size
->cx
= window_size
.right
- window_size
.left
;
2408 size
->cy
= window_size
.bottom
- window_size
.top
;
2413 level
= context_gl
->c
.current_rt
.sub_resource_idx
% rt
->level_count
;
2414 size
->cx
= wined3d_texture_get_level_width(rt
, level
);
2415 size
->cy
= wined3d_texture_get_level_height(rt
, level
);
2418 void wined3d_context_gl_enable_clip_distances(struct wined3d_context_gl
*context_gl
, uint32_t enable_mask
)
2420 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2421 unsigned int clip_distance_count
, i
;
2422 uint32_t disable_mask
, current_mask
;
2424 clip_distance_count
= gl_info
->limits
.user_clip_distances
;
2425 disable_mask
= ~enable_mask
;
2426 enable_mask
&= wined3d_mask_from_size(clip_distance_count
);
2427 disable_mask
&= wined3d_mask_from_size(clip_distance_count
);
2428 current_mask
= context_gl
->c
.clip_distance_mask
;
2429 context_gl
->c
.clip_distance_mask
= enable_mask
;
2431 enable_mask
&= ~current_mask
;
2434 i
= wined3d_bit_scan(&enable_mask
);
2435 gl_info
->gl_ops
.gl
.p_glEnable(GL_CLIP_DISTANCE0
+ i
);
2437 disable_mask
&= current_mask
;
2438 while (disable_mask
)
2440 i
= wined3d_bit_scan(&disable_mask
);
2441 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_DISTANCE0
+ i
);
2443 checkGLcall("toggle clip distances");
2446 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2448 return rt_mask
& (1u << 31);
2451 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2453 return rt_mask
& ~(1u << 31);
2456 /* Context activation is done by the caller. */
2457 static void wined3d_context_gl_apply_draw_buffers(struct wined3d_context_gl
*context_gl
, uint32_t rt_mask
)
2459 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2460 GLenum draw_buffers
[WINED3D_MAX_RENDER_TARGETS
];
2464 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2466 else if (is_rt_mask_onscreen(rt_mask
))
2468 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2472 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2479 draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2481 draw_buffers
[i
] = GL_NONE
;
2487 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2489 GL_EXTCALL(glDrawBuffers(i
, draw_buffers
));
2493 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffers
[0]);
2498 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2502 checkGLcall("apply draw buffers");
2505 /* Context activation is done by the caller. */
2506 void wined3d_context_gl_set_draw_buffer(struct wined3d_context_gl
*context_gl
, GLenum buffer
)
2508 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2509 struct fbo_entry
*current_fbo
= context_gl
->current_fbo
;
2510 uint32_t new_mask
= context_generate_rt_mask(buffer
);
2511 uint32_t *current_mask
;
2513 current_mask
= current_fbo
? ¤t_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
2514 if (new_mask
== *current_mask
)
2517 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2518 checkGLcall("glDrawBuffer()");
2520 *current_mask
= new_mask
;
2523 /* Context activation is done by the caller. */
2524 void wined3d_context_gl_active_texture(struct wined3d_context_gl
*context_gl
,
2525 const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2527 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2528 checkGLcall("glActiveTexture");
2529 context_gl
->active_texture
= unit
;
2532 void wined3d_context_gl_bind_bo(struct wined3d_context_gl
*context_gl
, GLenum binding
, GLuint name
)
2534 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2536 if (binding
== GL_ELEMENT_ARRAY_BUFFER
)
2537 context_invalidate_state(&context_gl
->c
, STATE_INDEXBUFFER
);
2539 GL_EXTCALL(glBindBuffer(binding
, name
));
2542 void wined3d_context_gl_bind_texture(struct wined3d_context_gl
*context_gl
, GLenum target
, GLuint name
)
2544 const struct wined3d_dummy_textures
*textures
= &wined3d_device_gl(context_gl
->c
.device
)->dummy_textures
;
2545 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2546 GLenum old_texture_type
;
2550 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2554 unit
= context_gl
->active_texture
;
2555 old_texture_type
= context_gl
->texture_type
[unit
];
2556 if (old_texture_type
!= target
)
2558 switch (old_texture_type
)
2564 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, textures
->tex_1d
);
2566 case GL_TEXTURE_1D_ARRAY
:
2567 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, textures
->tex_1d_array
);
2570 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, textures
->tex_2d
);
2572 case GL_TEXTURE_2D_ARRAY
:
2573 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, textures
->tex_2d_array
);
2575 case GL_TEXTURE_RECTANGLE_ARB
:
2576 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, textures
->tex_rect
);
2578 case GL_TEXTURE_CUBE_MAP
:
2579 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, textures
->tex_cube
);
2581 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2582 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, textures
->tex_cube_array
);
2585 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, textures
->tex_3d
);
2587 case GL_TEXTURE_BUFFER
:
2588 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, textures
->tex_buffer
);
2590 case GL_TEXTURE_2D_MULTISAMPLE
:
2591 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, textures
->tex_2d_ms
);
2593 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2594 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, textures
->tex_2d_ms_array
);
2597 ERR("Unexpected texture target %#x.\n", old_texture_type
);
2600 context_gl
->texture_type
[unit
] = target
;
2603 checkGLcall("bind texture");
2606 static void wined3d_context_gl_poll_fences(struct wined3d_context_gl
*context_gl
)
2608 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2609 struct wined3d_command_fence_gl
*f
;
2612 for (i
= 0; i
< context_gl
->submitted
.fence_count
; ++i
)
2614 f
= &context_gl
->submitted
.fences
[i
];
2616 if (f
->id
> device_gl
->completed_fence_id
)
2618 if (wined3d_fence_test(f
->fence
, &device_gl
->d
, 0) != WINED3D_FENCE_OK
)
2620 device_gl
->completed_fence_id
= f
->id
;
2623 wined3d_fence_destroy(f
->fence
);
2624 if (i
!= context_gl
->submitted
.fence_count
- 1)
2625 *f
= context_gl
->submitted
.fences
[context_gl
->submitted
.fence_count
- 1];
2626 --context_gl
->submitted
.fence_count
;
2630 static void wined3d_device_gl_free_memory(struct wined3d_device_gl
*device_gl
, struct wined3d_allocator_block
*block
)
2632 assert(block
->chunk
->allocator
== &device_gl
->allocator
);
2633 wined3d_device_gl_allocator_lock(device_gl
);
2634 wined3d_allocator_block_free(block
);
2635 wined3d_device_gl_allocator_unlock(device_gl
);
2638 static void wined3d_context_gl_cleanup_resources(struct wined3d_context_gl
*context_gl
)
2640 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2641 struct wined3d_retired_block_gl
*r
, *blocks
;
2642 SIZE_T count
, i
= 0;
2645 wined3d_context_gl_poll_fences(context_gl
);
2646 id
= device_gl
->completed_fence_id
;
2648 blocks
= device_gl
->retired_blocks
;
2649 count
= device_gl
->retired_block_count
;
2654 if (r
->fence_id
> id
)
2660 wined3d_device_gl_free_memory(device_gl
, r
->block
);
2666 device_gl
->retired_block_count
= count
;
2669 void wined3d_context_gl_wait_command_fence(struct wined3d_context_gl
*context_gl
, uint64_t id
)
2671 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2672 enum wined3d_fence_result ret
;
2675 if (id
<= device_gl
->completed_fence_id
2676 || id
> device_gl
->current_fence_id
) /* In case the fence ID wrapped. */
2679 for (i
= 0; i
< context_gl
->submitted
.fence_count
; ++i
)
2681 if (context_gl
->submitted
.fences
[i
].id
!= id
)
2684 if ((ret
= wined3d_fence_wait(context_gl
->submitted
.fences
[i
].fence
, &device_gl
->d
)) != WINED3D_FENCE_OK
)
2685 ERR("Failed to wait for command fence with id 0x%s, ret %#x.\n", wine_dbgstr_longlong(id
), ret
);
2686 wined3d_context_gl_cleanup_resources(context_gl
);
2690 ERR("Failed to find fence for command fence with id 0x%s.\n", wine_dbgstr_longlong(id
));
2693 void wined3d_context_gl_submit_command_fence(struct wined3d_context_gl
*context_gl
)
2695 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2696 struct wined3d_command_fence_gl
*f
;
2699 if (!wined3d_array_reserve((void **)&context_gl
->submitted
.fences
, &context_gl
->submitted
.fences_size
,
2700 context_gl
->submitted
.fence_count
+ 1, sizeof(*context_gl
->submitted
.fences
)))
2701 ERR("Failed to grow submitted command buffer array.\n");
2703 f
= &context_gl
->submitted
.fences
[context_gl
->submitted
.fence_count
++];
2704 f
->id
= device_gl
->current_fence_id
;
2705 if (FAILED(hr
= wined3d_fence_create(&device_gl
->d
, &f
->fence
)))
2706 ERR("Failed to create fence, hr %#x.\n", hr
);
2707 wined3d_fence_issue(f
->fence
, &device_gl
->d
);
2709 /* We don't expect this to ever happen, but handle it anyway. */
2710 if (!++device_gl
->current_fence_id
)
2712 wined3d_context_gl_wait_command_fence(context_gl
, device_gl
->current_fence_id
- 1);
2713 device_gl
->completed_fence_id
= 0;
2714 device_gl
->current_fence_id
= 1;
2716 wined3d_context_gl_cleanup_resources(context_gl
);
2719 static void wined3d_context_gl_destroy_allocator_block(struct wined3d_context_gl
*context_gl
,
2720 struct wined3d_allocator_block
*block
, uint64_t fence_id
)
2722 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2723 struct wined3d_retired_block_gl
*r
;
2725 if (device_gl
->completed_fence_id
> fence_id
)
2727 wined3d_device_gl_free_memory(device_gl
, block
);
2728 TRACE("Freed block %p.\n", block
);
2732 if (!wined3d_array_reserve((void **)&device_gl
->retired_blocks
,
2733 &device_gl
->retired_blocks_size
, device_gl
->retired_block_count
+ 1,
2734 sizeof(*device_gl
->retired_blocks
)))
2736 ERR("Leaking block %p.\n", block
);
2740 r
= &device_gl
->retired_blocks
[device_gl
->retired_block_count
++];
2742 r
->fence_id
= fence_id
;
2745 /* We always have buffer storage here. */
2746 GLuint
wined3d_context_gl_allocate_vram_chunk_buffer(struct wined3d_context_gl
*context_gl
,
2747 unsigned int pool
, size_t size
)
2749 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2753 TRACE("context_gl %p, pool %u, size %Iu.\n", context_gl
, pool
, size
);
2755 GL_EXTCALL(glGenBuffers(1, &id
));
2758 checkGLcall("buffer object creation");
2761 wined3d_context_gl_bind_bo(context_gl
, GL_PIXEL_UNPACK_BUFFER
, id
);
2763 flags
= wined3d_device_gl_get_memory_type_flags(pool
) | GL_DYNAMIC_STORAGE_BIT
;
2764 if (flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))
2765 flags
|= GL_MAP_PERSISTENT_BIT
| GL_MAP_COHERENT_BIT
;
2766 GL_EXTCALL(glBufferStorage(GL_PIXEL_UNPACK_BUFFER
, size
, NULL
, flags
));
2768 checkGLcall("buffer object creation");
2770 TRACE("Created buffer object %u.\n", id
);
2775 static void *wined3d_allocator_chunk_gl_map(struct wined3d_allocator_chunk_gl
*chunk_gl
,
2776 struct wined3d_context_gl
*context_gl
)
2778 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2781 TRACE("chunk %p, gl_buffer %u, map_ptr %p.\n", chunk_gl
, chunk_gl
->gl_buffer
, chunk_gl
->c
.map_ptr
);
2783 wined3d_allocator_chunk_gl_lock(chunk_gl
);
2785 if (!chunk_gl
->c
.map_ptr
)
2787 unsigned int flags
= wined3d_device_gl_get_memory_type_flags(chunk_gl
->memory_type
) & ~GL_CLIENT_STORAGE_BIT
;
2789 flags
|= GL_MAP_PERSISTENT_BIT
| GL_MAP_COHERENT_BIT
;
2790 if (!(flags
& GL_MAP_READ_BIT
))
2791 flags
|= GL_MAP_UNSYNCHRONIZED_BIT
;
2792 if (flags
& GL_MAP_WRITE_BIT
)
2793 flags
|= GL_MAP_FLUSH_EXPLICIT_BIT
;
2794 wined3d_context_gl_bind_bo(context_gl
, GL_PIXEL_UNPACK_BUFFER
, chunk_gl
->gl_buffer
);
2795 chunk_gl
->c
.map_ptr
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER
,
2796 0, WINED3D_ALLOCATOR_CHUNK_SIZE
, flags
));
2797 if (!chunk_gl
->c
.map_ptr
)
2799 wined3d_allocator_chunk_gl_unlock(chunk_gl
);
2800 ERR("Failed to map chunk memory.\n");
2804 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, WINED3D_ALLOCATOR_CHUNK_SIZE
);
2807 ++chunk_gl
->c
.map_count
;
2808 map_ptr
= chunk_gl
->c
.map_ptr
;
2810 wined3d_allocator_chunk_gl_unlock(chunk_gl
);
2815 static void wined3d_allocator_chunk_gl_unmap(struct wined3d_allocator_chunk_gl
*chunk_gl
,
2816 struct wined3d_context_gl
*context_gl
)
2818 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2820 TRACE("chunk_gl %p, context_gl %p.\n", chunk_gl
, context_gl
);
2822 wined3d_allocator_chunk_gl_lock(chunk_gl
);
2824 if (!--chunk_gl
->c
.map_count
)
2826 wined3d_context_gl_bind_bo(context_gl
, GL_PIXEL_UNPACK_BUFFER
, chunk_gl
->gl_buffer
);
2827 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER
));
2828 chunk_gl
->c
.map_ptr
= NULL
;
2830 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, -WINED3D_ALLOCATOR_CHUNK_SIZE
);
2833 wined3d_allocator_chunk_gl_unlock(chunk_gl
);
2836 static void *wined3d_bo_gl_map(struct wined3d_bo_gl
*bo
, struct wined3d_context_gl
*context_gl
, uint32_t flags
)
2838 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2839 const struct wined3d_gl_info
*gl_info
;
2840 struct wined3d_bo_user
*bo_user
;
2841 struct wined3d_bo_gl tmp
;
2843 if (flags
& WINED3D_MAP_NOOVERWRITE
)
2846 if ((flags
& WINED3D_MAP_DISCARD
) && bo
->command_fence_id
> device_gl
->completed_fence_id
)
2848 if (wined3d_device_gl_create_bo(device_gl
, context_gl
, bo
->size
,
2849 bo
->binding
, bo
->usage
, bo
->b
.coherent
, bo
->flags
, &tmp
))
2851 list_move_head(&tmp
.b
.users
, &bo
->b
.users
);
2852 wined3d_context_gl_destroy_bo(context_gl
, bo
);
2854 list_init(&bo
->b
.users
);
2855 list_move_head(&bo
->b
.users
, &tmp
.b
.users
);
2856 LIST_FOR_EACH_ENTRY(bo_user
, &bo
->b
.users
, struct wined3d_bo_user
, entry
)
2858 bo_user
->valid
= false;
2864 ERR("Failed to create new buffer object.\n");
2867 if (bo
->command_fence_id
== device_gl
->current_fence_id
)
2868 wined3d_context_gl_submit_command_fence(context_gl
);
2869 wined3d_context_gl_wait_command_fence(context_gl
, bo
->command_fence_id
);
2873 return bo
->b
.map_ptr
;
2877 struct wined3d_allocator_chunk_gl
*chunk_gl
= wined3d_allocator_chunk_gl(bo
->memory
->chunk
);
2879 if (!(bo
->b
.map_ptr
= wined3d_allocator_chunk_gl_map(chunk_gl
, context_gl
)))
2880 ERR("Failed to map chunk.\n");
2881 return bo
->b
.map_ptr
;
2884 gl_info
= context_gl
->gl_info
;
2885 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
2887 if (gl_info
->supported
[ARB_BUFFER_STORAGE
])
2889 GLbitfield gl_flags
;
2891 /* When mapping the bo persistently, we need to use the access flags
2892 * used to create the bo, instead of the access flags passed to the
2893 * map call. Otherwise, if for example the initial map call that
2894 * caused the bo to be persistently mapped was a read-only map,
2895 * subsequent write access to the bo would be undefined.
2897 * Note that we use GL_MAP_PERSISTENT_BIT for non-persistent maps here
2898 * as well, in order to allow draws to succeed while referenced buffer
2899 * resources are mapped. On the other hand, we don't want to use the
2900 * access flags used to create the bo for non-persistent maps, because
2901 * that may imply dropping GL_MAP_UNSYNCHRONIZED_BIT. */
2902 gl_flags
= bo
->flags
& ~GL_CLIENT_STORAGE_BIT
;
2903 if (!(gl_flags
& GL_MAP_READ_BIT
))
2904 gl_flags
|= GL_MAP_UNSYNCHRONIZED_BIT
;
2905 if (gl_flags
& GL_MAP_WRITE_BIT
)
2906 gl_flags
|= GL_MAP_FLUSH_EXPLICIT_BIT
;
2907 gl_flags
|= GL_MAP_PERSISTENT_BIT
| GL_MAP_COHERENT_BIT
;
2909 bo
->b
.map_ptr
= GL_EXTCALL(glMapBufferRange(bo
->binding
, 0, bo
->size
, gl_flags
));
2911 else if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
2913 bo
->b
.map_ptr
= GL_EXTCALL(glMapBufferRange(bo
->binding
, 0, bo
->size
, wined3d_resource_gl_map_flags(bo
, flags
)));
2917 bo
->b
.map_ptr
= GL_EXTCALL(glMapBuffer(bo
->binding
, wined3d_resource_gl_legacy_map_flags(flags
)));
2921 adapter_adjust_mapped_memory(device_gl
->d
.adapter
, bo
->size
);
2923 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, 0);
2924 checkGLcall("Map buffer object");
2926 return bo
->b
.map_ptr
;
2929 static void wined3d_bo_gl_unmap(struct wined3d_bo_gl
*bo
, struct wined3d_context_gl
*context_gl
)
2931 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2933 if (context_gl
->c
.device
->adapter
->mapped_size
<= MAX_PERSISTENT_MAPPED_BYTES
)
2935 TRACE("Not unmapping BO %p.\n", bo
);
2941 struct wined3d_allocator_chunk_gl
*chunk_gl
= wined3d_allocator_chunk_gl(bo
->memory
->chunk
);
2943 wined3d_allocator_chunk_gl_unmap(chunk_gl
, context_gl
);
2944 if (!chunk_gl
->c
.map_ptr
)
2945 bo
->b
.map_ptr
= NULL
;
2949 wined3d_device_bo_map_lock(context_gl
->c
.device
);
2950 /* The mapping is still in use by the client (viz. for an accelerated
2951 * NOOVERWRITE map). The client will trigger another unmap request when the
2952 * d3d application requests to unmap the BO. */
2953 if (bo
->b
.client_map_count
)
2955 wined3d_device_bo_map_unlock(context_gl
->c
.device
);
2956 TRACE("BO %p is still in use by a client thread; not unmapping.\n", bo
);
2959 bo
->b
.map_ptr
= NULL
;
2960 wined3d_device_bo_map_unlock(context_gl
->c
.device
);
2962 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
2963 GL_EXTCALL(glUnmapBuffer(bo
->binding
));
2964 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, 0);
2965 checkGLcall("Unmap buffer object");
2967 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, -bo
->size
);
2970 void *wined3d_context_gl_map_bo_address(struct wined3d_context_gl
*context_gl
,
2971 const struct wined3d_bo_address
*data
, size_t size
, uint32_t flags
)
2973 struct wined3d_bo
*bo
;
2976 if (!(bo
= data
->buffer_object
))
2979 if (!(map_ptr
= wined3d_bo_gl_map(wined3d_bo_gl(bo
), context_gl
, flags
)))
2981 ERR("Failed to map bo.\n");
2985 return (uint8_t *)map_ptr
+ bo
->buffer_offset
+ (uintptr_t)data
->addr
;
2988 static void flush_bo_ranges(struct wined3d_context_gl
*context_gl
, const struct wined3d_const_bo_address
*data
,
2989 unsigned int range_count
, const struct wined3d_range
*ranges
)
2991 const struct wined3d_gl_info
*gl_info
;
2992 struct wined3d_bo_gl
*bo
;
2995 if (!data
->buffer_object
|| data
->buffer_object
->coherent
)
2997 bo
= wined3d_bo_gl(data
->buffer_object
);
2999 gl_info
= context_gl
->gl_info
;
3000 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
3002 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
3004 /* The offset passed to glFlushMappedBufferRange() is relative to the
3005 * mapped range, but we map the whole buffer anyway. */
3006 for (i
= 0; i
< range_count
; ++i
)
3008 GL_EXTCALL(glFlushMappedBufferRange(bo
->binding
,
3009 bo
->b
.buffer_offset
+ (uintptr_t)data
->addr
+ ranges
[i
].offset
, ranges
[i
].size
));
3012 else if (gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
])
3014 for (i
= 0; i
< range_count
; ++i
)
3016 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(bo
->binding
,
3017 bo
->b
.buffer_offset
+ (uintptr_t)data
->addr
+ ranges
[i
].offset
, ranges
[i
].size
));
3018 checkGLcall("glFlushMappedBufferRangeAPPLE");
3022 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, 0);
3023 checkGLcall("Flush buffer object");
3026 void wined3d_context_gl_unmap_bo_address(struct wined3d_context_gl
*context_gl
,
3027 const struct wined3d_bo_address
*data
, unsigned int range_count
, const struct wined3d_range
*ranges
)
3029 struct wined3d_bo_gl
*bo
;
3031 if (!data
->buffer_object
)
3033 bo
= wined3d_bo_gl(data
->buffer_object
);
3035 flush_bo_ranges(context_gl
, wined3d_const_bo_address(data
), range_count
, ranges
);
3036 wined3d_bo_gl_unmap(bo
, context_gl
);
3039 void wined3d_context_gl_flush_bo_address(struct wined3d_context_gl
*context_gl
,
3040 const struct wined3d_const_bo_address
*data
, size_t size
)
3042 struct wined3d_range range
;
3044 TRACE("context_gl %p, data %s, size %Iu.\n", context_gl
, debug_const_bo_address(data
), size
);
3046 range
.offset
= (uintptr_t)data
->addr
;
3049 flush_bo_ranges(context_gl
, data
, 1, &range
);
3052 void wined3d_context_gl_copy_bo_address(struct wined3d_context_gl
*context_gl
,
3053 const struct wined3d_bo_address
*dst
, const struct wined3d_bo_address
*src
,
3054 unsigned int range_count
, const struct wined3d_range
*ranges
)
3056 const struct wined3d_gl_info
*gl_info
;
3057 struct wined3d_bo_gl
*src_bo
, *dst_bo
;
3058 BYTE
*dst_ptr
, *src_ptr
;
3061 gl_info
= context_gl
->gl_info
;
3062 src_bo
= src
->buffer_object
? wined3d_bo_gl(src
->buffer_object
) : NULL
;
3063 dst_bo
= dst
->buffer_object
? wined3d_bo_gl(dst
->buffer_object
) : NULL
;
3065 if (dst_bo
&& src_bo
)
3067 if (gl_info
->supported
[ARB_COPY_BUFFER
])
3069 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER
, src_bo
->id
));
3070 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER
, dst_bo
->id
));
3072 for (i
= 0; i
< range_count
; ++i
)
3073 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
,
3074 src_bo
->b
.buffer_offset
+ (GLintptr
)src
->addr
+ ranges
[i
].offset
,
3075 dst_bo
->b
.buffer_offset
+ (GLintptr
)dst
->addr
+ ranges
[i
].offset
, ranges
[i
].size
));
3076 checkGLcall("direct buffer copy");
3078 wined3d_context_gl_reference_bo(context_gl
, src_bo
);
3079 wined3d_context_gl_reference_bo(context_gl
, dst_bo
);
3083 src_ptr
= wined3d_context_gl_map_bo_address(context_gl
, src
,
3084 src_bo
->size
- (uintptr_t)src
->addr
, WINED3D_MAP_READ
);
3085 dst_ptr
= wined3d_context_gl_map_bo_address(context_gl
, dst
,
3086 dst_bo
->size
- (uintptr_t)dst
->addr
, WINED3D_MAP_WRITE
);
3088 for (i
= 0; i
< range_count
; ++i
)
3089 memcpy(dst_ptr
+ ranges
[i
].offset
, src_ptr
+ ranges
[i
].offset
, ranges
[i
].size
);
3091 wined3d_context_gl_unmap_bo_address(context_gl
, dst
, range_count
, ranges
);
3092 wined3d_context_gl_unmap_bo_address(context_gl
, src
, 0, NULL
);
3095 else if (!dst_bo
&& src_bo
)
3097 wined3d_context_gl_bind_bo(context_gl
, src_bo
->binding
, src_bo
->id
);
3098 for (i
= 0; i
< range_count
; ++i
)
3099 GL_EXTCALL(glGetBufferSubData(src_bo
->binding
,
3100 src_bo
->b
.buffer_offset
+ (GLintptr
)src
->addr
+ ranges
[i
].offset
,
3101 ranges
[i
].size
, dst
->addr
+ ranges
[i
].offset
));
3102 checkGLcall("buffer download");
3104 wined3d_context_gl_reference_bo(context_gl
, src_bo
);
3106 else if (dst_bo
&& !src_bo
)
3108 wined3d_context_gl_bind_bo(context_gl
, dst_bo
->binding
, dst_bo
->id
);
3109 for (i
= 0; i
< range_count
; ++i
)
3110 GL_EXTCALL(glBufferSubData(dst_bo
->binding
,
3111 dst_bo
->b
.buffer_offset
+ (GLintptr
)dst
->addr
+ ranges
[i
].offset
,
3112 ranges
[i
].size
, src
->addr
+ ranges
[i
].offset
));
3113 checkGLcall("buffer upload");
3115 wined3d_context_gl_reference_bo(context_gl
, dst_bo
);
3119 for (i
= 0; i
< range_count
; ++i
)
3120 memcpy(dst
->addr
+ ranges
[i
].offset
, src
->addr
+ ranges
[i
].offset
, ranges
[i
].size
);
3124 void wined3d_context_gl_destroy_bo(struct wined3d_context_gl
*context_gl
, struct wined3d_bo_gl
*bo
)
3126 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3128 TRACE("context_gl %p, bo %p.\n", context_gl
, bo
);
3133 wined3d_allocator_chunk_gl_unmap(wined3d_allocator_chunk_gl(bo
->memory
->chunk
), context_gl
);
3134 wined3d_context_gl_destroy_allocator_block(context_gl
, bo
->memory
, bo
->command_fence_id
);
3141 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
3142 GL_EXTCALL(glUnmapBuffer(bo
->binding
));
3143 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, -bo
->size
);
3146 TRACE("Destroying GL buffer %u.\n", bo
->id
);
3148 GL_EXTCALL(glDeleteBuffers(1, &bo
->id
));
3149 checkGLcall("buffer object destruction");
3153 static void wined3d_context_gl_set_render_offscreen(struct wined3d_context_gl
*context_gl
, BOOL offscreen
)
3155 if (context_gl
->c
.render_offscreen
== offscreen
)
3158 context_invalidate_state(&context_gl
->c
, STATE_VIEWPORT
);
3159 context_invalidate_state(&context_gl
->c
, STATE_SCISSORRECT
);
3160 if (!context_gl
->gl_info
->supported
[ARB_CLIP_CONTROL
])
3162 context_invalidate_state(&context_gl
->c
, STATE_RASTERIZER
);
3163 context_invalidate_state(&context_gl
->c
, STATE_POINTSPRITECOORDORIGIN
);
3164 context_invalidate_state(&context_gl
->c
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
3166 context_invalidate_state(&context_gl
->c
, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN
));
3167 if (context_gl
->gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
3168 context_invalidate_state(&context_gl
->c
, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
));
3169 context_gl
->c
.render_offscreen
= offscreen
;
3172 GLenum
wined3d_context_gl_get_offscreen_gl_buffer(const struct wined3d_context_gl
*context_gl
)
3174 switch (wined3d_settings
.offscreen_rendering_mode
)
3177 return GL_COLOR_ATTACHMENT0
;
3179 case ORM_BACKBUFFER
:
3180 return context_gl
->aux_buffers
> 0 ? GL_AUX0
: GL_BACK
;
3183 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
3188 static uint32_t wined3d_context_gl_generate_rt_mask_no_fbo(const struct wined3d_context_gl
*context_gl
,
3189 struct wined3d_resource
*rt
)
3191 if (!rt
|| rt
->format
->id
== WINED3DFMT_NULL
)
3193 else if (rt
->type
!= WINED3D_RTYPE_BUFFER
&& texture_from_resource(rt
)->swapchain
)
3194 return context_generate_rt_mask_from_resource(rt
);
3196 return context_generate_rt_mask(wined3d_context_gl_get_offscreen_gl_buffer(context_gl
));
3199 /* Context activation is done by the caller. */
3200 void wined3d_context_gl_apply_blit_state(struct wined3d_context_gl
*context_gl
, const struct wined3d_device
*device
)
3202 struct wined3d_context
*context
= &context_gl
->c
;
3203 const struct wined3d_gl_info
*gl_info
;
3204 uint32_t rt_mask
, *cur_mask
;
3205 struct wined3d_texture
*rt
;
3206 unsigned int sampler
;
3209 TRACE("Setting up context %p for blitting.\n", context
);
3211 gl_info
= context_gl
->gl_info
;
3212 rt
= context
->current_rt
.texture
;
3214 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3216 if (context
->render_offscreen
)
3218 wined3d_texture_load(rt
, context
, FALSE
);
3220 wined3d_context_gl_apply_fbo_state_blit(context_gl
, GL_FRAMEBUFFER
, &rt
->resource
,
3221 context
->current_rt
.sub_resource_idx
, NULL
, 0, rt
->resource
.draw_binding
);
3222 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
3229 context_gl
->current_fbo
= NULL
;
3230 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
3231 rt_mask
= context_generate_rt_mask_from_resource(&rt
->resource
);
3236 rt_mask
= wined3d_context_gl_generate_rt_mask_no_fbo(context_gl
, &rt
->resource
);
3239 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3241 if (rt_mask
!= *cur_mask
)
3243 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3244 *cur_mask
= rt_mask
;
3247 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3248 wined3d_context_gl_check_fbo_status(context_gl
, GL_FRAMEBUFFER
);
3249 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
3251 wined3d_context_gl_get_rt_size(context_gl
, &rt_size
);
3253 if (context
->last_was_blit
)
3255 if (context_gl
->blit_size
.cx
!= rt_size
.cx
|| context_gl
->blit_size
.cy
!= rt_size
.cy
)
3257 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, rt_size
.cx
, rt_size
.cy
);
3258 context
->viewport_count
= WINED3D_MAX_VIEWPORTS
;
3259 context_gl
->blit_size
= rt_size
;
3260 /* No need to dirtify here, the states are still dirtified because
3261 * they weren't applied since the last context_apply_blit_state()
3264 checkGLcall("blit state application");
3265 TRACE("Context is already set up for blitting, nothing to do.\n");
3268 context
->last_was_blit
= TRUE
;
3270 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
3271 GL_EXTCALL(glBindSampler(0, 0));
3272 wined3d_context_gl_active_texture(context_gl
, gl_info
, 0);
3274 sampler
= context_gl
->rev_tex_unit_map
[0];
3275 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
3277 if (sampler
< WINED3D_MAX_TEXTURES
)
3279 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
3280 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
3282 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
3284 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
3285 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
3287 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
3289 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
3290 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
3292 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
3293 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3294 context_invalidate_state(context
, STATE_BLEND
);
3295 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
3296 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
3297 context_invalidate_state(context
, STATE_RASTERIZER
);
3298 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
3299 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
3300 context_invalidate_state(context
, STATE_DEPTH_STENCIL
);
3301 if (gl_info
->supported
[ARB_POINT_SPRITE
])
3303 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
3304 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
3306 if (gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
3308 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
3309 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3312 context
->last_was_rhw
= TRUE
;
3313 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
3315 wined3d_context_gl_enable_clip_distances(context_gl
, 0);
3316 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
3318 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
3319 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
3320 GL_EXTCALL(glClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
));
3321 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, rt_size
.cx
, rt_size
.cy
);
3322 context
->viewport_count
= WINED3D_MAX_VIEWPORTS
;
3323 context_invalidate_state(context
, STATE_VIEWPORT
);
3325 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
3327 context_gl
->blit_size
= rt_size
;
3329 checkGLcall("blit state application");
3332 static void wined3d_context_gl_apply_blit_projection(const struct wined3d_context_gl
*context_gl
,
3333 unsigned int w
, unsigned int h
)
3335 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3336 const GLdouble projection
[] =
3338 2.0 / w
, 0.0, 0.0, 0.0,
3339 0.0, 2.0 / h
, 0.0, 0.0,
3341 -1.0, -1.0, -1.0, 1.0,
3344 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
3345 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
3348 /* Setup OpenGL states for fixed-function blitting. */
3349 /* Context activation is done by the caller. */
3350 void wined3d_context_gl_apply_ffp_blit_state(struct wined3d_context_gl
*context_gl
,
3351 const struct wined3d_device
*device
)
3353 struct wined3d_context
*context
= &context_gl
->c
;
3354 const struct wined3d_gl_info
*gl_info
;
3355 unsigned int i
, sampler
;
3357 gl_info
= context_gl
->gl_info
;
3358 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
3359 ERR("Applying fixed-function state without legacy context support.\n");
3361 if (context
->last_was_ffp_blit
)
3365 wined3d_context_gl_get_rt_size(context_gl
, &rt_size
);
3366 if (context_gl
->blit_size
.cx
!= rt_size
.cx
|| context_gl
->blit_size
.cy
!= rt_size
.cy
)
3367 wined3d_context_gl_apply_blit_projection(context_gl
, rt_size
.cx
, rt_size
.cy
);
3368 wined3d_context_gl_apply_blit_state(context_gl
, device
);
3370 checkGLcall("ffp blit state application");
3373 context
->last_was_ffp_blit
= TRUE
;
3375 wined3d_context_gl_apply_blit_state(context_gl
, device
);
3377 /* Disable all textures. The caller can then bind a texture it wants to blit
3379 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
3381 wined3d_context_gl_active_texture(context_gl
, gl_info
, i
);
3383 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
3384 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
3385 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
3386 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
3387 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
3388 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
3390 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
3392 sampler
= context_gl
->rev_tex_unit_map
[i
];
3393 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
3395 if (sampler
< WINED3D_MAX_TEXTURES
)
3396 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
3397 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
3401 wined3d_context_gl_active_texture(context_gl
, gl_info
, 0);
3403 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
3404 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
3405 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
3406 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
3407 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
3408 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
3410 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
3411 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
3412 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
3414 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
3415 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
3417 /* Setup transforms. */
3418 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
3419 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
3420 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3421 wined3d_context_gl_apply_blit_projection(context_gl
, context_gl
->blit_size
.cx
, context_gl
->blit_size
.cy
);
3422 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
3424 /* Other misc states. */
3425 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
3426 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
3427 gl_info
->p_glDisableWINE(GL_FOG
);
3428 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
3430 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
3432 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
3433 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
3435 checkGLcall("ffp blit state application");
3438 static BOOL
have_framebuffer_attachment(unsigned int rt_count
, struct wined3d_rendertarget_view
* const *rts
,
3439 const struct wined3d_rendertarget_view
*ds
)
3446 for (i
= 0; i
< rt_count
; ++i
)
3448 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3455 /* Context activation is done by the caller. */
3456 BOOL
wined3d_context_gl_apply_clear_state(struct wined3d_context_gl
*context_gl
,
3457 const struct wined3d_state
*state
, unsigned int rt_count
, const struct wined3d_fb_state
*fb
)
3459 struct wined3d_rendertarget_view
* const *rts
= fb
->render_targets
;
3460 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3461 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
3462 uint32_t rt_mask
= 0, *cur_mask
;
3465 if (isStateDirty(&context_gl
->c
, STATE_FRAMEBUFFER
) || fb
!= &state
->fb
3466 || rt_count
!= gl_info
->limits
.buffers
)
3468 if (!have_framebuffer_attachment(rt_count
, rts
, dsv
))
3470 WARN("Invalid render target config, need at least one attachment.\n");
3474 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3476 struct wined3d_rendertarget_info ds_info
= {{0}};
3478 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
3480 memset(context_gl
->blit_targets
, 0, sizeof(context_gl
->blit_targets
));
3481 for (i
= 0; i
< rt_count
; ++i
)
3485 struct wined3d_rendertarget_view_gl
*rtv_gl
= wined3d_rendertarget_view_gl(rts
[i
]);
3486 context_gl
->blit_targets
[i
].gl_view
= rtv_gl
->gl_view
;
3487 context_gl
->blit_targets
[i
].resource
= rtv_gl
->v
.resource
;
3488 context_gl
->blit_targets
[i
].sub_resource_idx
= rtv_gl
->v
.sub_resource_idx
;
3489 context_gl
->blit_targets
[i
].layer_count
= rtv_gl
->v
.layer_count
;
3491 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3492 rt_mask
|= (1u << i
);
3497 struct wined3d_rendertarget_view_gl
*dsv_gl
= wined3d_rendertarget_view_gl(dsv
);
3498 ds_info
.gl_view
= dsv_gl
->gl_view
;
3499 ds_info
.resource
= dsv_gl
->v
.resource
;
3500 ds_info
.sub_resource_idx
= dsv_gl
->v
.sub_resource_idx
;
3501 ds_info
.layer_count
= dsv_gl
->v
.layer_count
;
3504 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, context_gl
->blit_targets
, &ds_info
,
3505 rt_count
? rts
[0]->resource
->draw_binding
: 0, dsv
? dsv
->resource
->draw_binding
: 0);
3509 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, NULL
, &ds_info
,
3510 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
3511 rt_mask
= context_generate_rt_mask_from_resource(rts
[0]->resource
);
3514 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3515 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3516 * state management allows this */
3517 context_invalidate_state(&context_gl
->c
, STATE_FRAMEBUFFER
);
3521 rt_mask
= wined3d_context_gl_generate_rt_mask_no_fbo(context_gl
, rt_count
? rts
[0]->resource
: NULL
);
3524 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
3525 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
3527 for (i
= 0; i
< rt_count
; ++i
)
3529 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3530 rt_mask
|= (1u << i
);
3535 rt_mask
= wined3d_context_gl_generate_rt_mask_no_fbo(context_gl
, rt_count
? rts
[0]->resource
: NULL
);
3538 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3540 if (rt_mask
!= *cur_mask
)
3542 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3543 *cur_mask
= rt_mask
;
3544 context_invalidate_state(&context_gl
->c
, STATE_FRAMEBUFFER
);
3547 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3548 wined3d_context_gl_check_fbo_status(context_gl
, GL_FRAMEBUFFER
);
3550 context_gl
->c
.last_was_blit
= FALSE
;
3551 context_gl
->c
.last_was_ffp_blit
= FALSE
;
3553 /* Blending and clearing should be orthogonal, but tests on the nvidia
3554 * driver show that disabling blending when clearing improves the clearing
3555 * performance incredibly. */
3556 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
3557 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
3558 if (rt_count
&& gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
3560 if (needs_srgb_write(context_gl
->c
.d3d_info
, state
, fb
))
3561 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
3563 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
3564 context_invalidate_state(&context_gl
->c
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3566 checkGLcall("setting up state for clear");
3568 context_invalidate_state(&context_gl
->c
, STATE_BLEND
);
3569 context_invalidate_state(&context_gl
->c
, STATE_RASTERIZER
);
3570 context_invalidate_state(&context_gl
->c
, STATE_SCISSORRECT
);
3575 static uint32_t find_draw_buffers_mask(const struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
)
3577 struct wined3d_rendertarget_view
* const *rts
= state
->fb
.render_targets
;
3578 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
3579 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3580 unsigned int rt_mask
, mask
;
3583 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
3584 return wined3d_context_gl_generate_rt_mask_no_fbo(context_gl
, rts
[0]->resource
);
3585 else if (!context_gl
->c
.render_offscreen
)
3586 return context_generate_rt_mask_from_resource(rts
[0]->resource
);
3588 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
3589 rt_mask
&= wined3d_mask_from_size(gl_info
->limits
.buffers
);
3590 if (state
->blend_state
&& state
->blend_state
->dual_source
)
3596 i
= wined3d_bit_scan(&mask
);
3597 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
3598 rt_mask
&= ~(1u << i
);
3604 /* Context activation is done by the caller. */
3605 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3607 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
3608 uint32_t rt_mask
= find_draw_buffers_mask(context_gl
, state
);
3609 const struct wined3d_fb_state
*fb
= &state
->fb
;
3610 DWORD color_location
= 0;
3613 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3615 struct wined3d_rendertarget_info ds_info
= {{0}};
3617 if (!context
->render_offscreen
)
3619 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, NULL
, &ds_info
,
3620 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
3624 const struct wined3d_rendertarget_view_gl
*view_gl
;
3627 memset(context_gl
->blit_targets
, 0, sizeof(context_gl
->blit_targets
));
3628 for (i
= 0; i
< context_gl
->gl_info
->limits
.buffers
; ++i
)
3630 if (!fb
->render_targets
[i
])
3633 view_gl
= wined3d_rendertarget_view_gl(fb
->render_targets
[i
]);
3634 context_gl
->blit_targets
[i
].gl_view
= view_gl
->gl_view
;
3635 context_gl
->blit_targets
[i
].resource
= view_gl
->v
.resource
;
3636 context_gl
->blit_targets
[i
].sub_resource_idx
= view_gl
->v
.sub_resource_idx
;
3637 context_gl
->blit_targets
[i
].layer_count
= view_gl
->v
.layer_count
;
3639 if (!color_location
)
3640 color_location
= view_gl
->v
.resource
->draw_binding
;
3643 if (fb
->depth_stencil
)
3645 view_gl
= wined3d_rendertarget_view_gl(fb
->depth_stencil
);
3646 ds_info
.gl_view
= view_gl
->gl_view
;
3647 ds_info
.resource
= view_gl
->v
.resource
;
3648 ds_info
.sub_resource_idx
= view_gl
->v
.sub_resource_idx
;
3649 ds_info
.layer_count
= view_gl
->v
.layer_count
;
3652 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, context_gl
->blit_targets
, &ds_info
,
3653 color_location
, fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
3657 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3658 if (rt_mask
!= *cur_mask
)
3660 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3661 *cur_mask
= rt_mask
;
3663 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
3666 static void wined3d_context_gl_map_stage(struct wined3d_context_gl
*context_gl
, unsigned int stage
, unsigned int unit
)
3668 unsigned int i
= context_gl
->rev_tex_unit_map
[unit
];
3669 unsigned int j
= context_gl
->tex_unit_map
[stage
];
3671 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
3672 context_gl
->tex_unit_map
[stage
] = unit
;
3673 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
3674 context_gl
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
3676 context_gl
->rev_tex_unit_map
[unit
] = stage
;
3677 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
3678 context_gl
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
3681 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
3685 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
3686 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
3689 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
3690 const struct wined3d_state
*state
)
3694 context
->fixed_function_usage_map
= 0;
3695 for (i
= 0; i
< WINED3D_MAX_TEXTURES
; ++i
)
3697 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
3698 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
3699 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
3700 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
3701 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
3702 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
3703 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
3704 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
3706 /* Not used, and disable higher stages. */
3707 if (color_op
== WINED3D_TOP_DISABLE
)
3710 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
3711 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
3712 || ((color_arg3
== WINED3DTA_TEXTURE
)
3713 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
3714 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
3715 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
3716 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
3717 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
3718 context
->fixed_function_usage_map
|= (1u << i
);
3720 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
3721 && i
< WINED3D_MAX_TEXTURES
- 1)
3722 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
3725 if (i
< context
->lowest_disabled_stage
)
3728 end
= context
->lowest_disabled_stage
;
3732 start
= context
->lowest_disabled_stage
;
3736 context
->lowest_disabled_stage
= i
;
3737 for (i
= start
+ 1; i
< end
; ++i
)
3739 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3743 static void wined3d_context_gl_map_fixed_function_samplers(struct wined3d_context_gl
*context_gl
,
3744 const struct wined3d_state
*state
)
3746 const struct wined3d_d3d_info
*d3d_info
= context_gl
->c
.d3d_info
;
3747 unsigned int i
, tex
;
3750 ffu_map
= context_gl
->c
.fixed_function_usage_map
;
3752 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
3753 || context_gl
->c
.lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
3757 i
= wined3d_bit_scan(&ffu_map
);
3758 if (context_gl
->tex_unit_map
[i
] != i
)
3760 wined3d_context_gl_map_stage(context_gl
, i
, i
);
3761 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(i
));
3762 context_invalidate_texture_stage(&context_gl
->c
, i
);
3768 /* Now work out the mapping */
3772 i
= wined3d_bit_scan(&ffu_map
);
3773 if (context_gl
->tex_unit_map
[i
] != tex
)
3775 wined3d_context_gl_map_stage(context_gl
, i
, tex
);
3776 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(i
));
3777 context_invalidate_texture_stage(&context_gl
->c
, i
);
3784 static void wined3d_context_gl_map_psamplers(struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
)
3786 const struct wined3d_d3d_info
*d3d_info
= context_gl
->c
.d3d_info
;
3787 const struct wined3d_shader_resource_info
*resource_info
=
3788 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3791 for (i
= 0; i
< WINED3D_MAX_FRAGMENT_SAMPLERS
; ++i
)
3793 if (resource_info
[i
].type
&& context_gl
->tex_unit_map
[i
] != i
)
3795 wined3d_context_gl_map_stage(context_gl
, i
, i
);
3796 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(i
));
3797 if (i
< d3d_info
->limits
.ffp_blend_stages
)
3798 context_invalidate_texture_stage(&context_gl
->c
, i
);
3803 static BOOL
wined3d_context_gl_unit_free_for_vs(const struct wined3d_context_gl
*context_gl
,
3804 const struct wined3d_shader_resource_info
*ps_resource_info
, unsigned int unit
)
3806 unsigned int current_mapping
= context_gl
->rev_tex_unit_map
[unit
];
3808 /* Not currently used */
3809 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
3812 if (current_mapping
< WINED3D_MAX_FRAGMENT_SAMPLERS
)
3814 /* Used by a fragment sampler */
3816 if (!ps_resource_info
)
3818 /* No pixel shader, check fixed function */
3819 return current_mapping
>= WINED3D_MAX_TEXTURES
3820 || !(context_gl
->c
.fixed_function_usage_map
& (1u << current_mapping
));
3823 /* Pixel shader, check the shader's sampler map */
3824 return !ps_resource_info
[current_mapping
].type
;
3830 static void wined3d_context_gl_map_vsamplers(struct wined3d_context_gl
*context_gl
,
3831 BOOL ps
, const struct wined3d_state
*state
)
3833 const struct wined3d_shader_resource_info
*vs_resource_info
=
3834 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
3835 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
3836 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3837 int start
= min(WINED3D_MAX_COMBINED_SAMPLERS
, gl_info
->limits
.graphics_samplers
) - 1;
3840 /* Note that we only care if a resource is used or not, not the
3841 * resource's specific type. Otherwise we'd need to call
3842 * shader_update_samplers() here for 1.x pixelshaders. */
3844 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3846 for (i
= 0; i
< WINED3D_MAX_VERTEX_SAMPLERS
; ++i
)
3848 DWORD vsampler_idx
= i
+ WINED3D_MAX_FRAGMENT_SAMPLERS
;
3849 if (vs_resource_info
[i
].type
)
3853 if (wined3d_context_gl_unit_free_for_vs(context_gl
, ps_resource_info
, start
))
3855 if (context_gl
->tex_unit_map
[vsampler_idx
] != start
)
3857 wined3d_context_gl_map_stage(context_gl
, vsampler_idx
, start
);
3858 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(vsampler_idx
));
3867 if (context_gl
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
3868 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
3873 static void wined3d_context_gl_update_tex_unit_map(struct wined3d_context_gl
*context_gl
,
3874 const struct wined3d_state
*state
)
3876 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3877 BOOL vs
= use_vs(state
);
3878 BOOL ps
= use_ps(state
);
3881 context_update_fixed_function_usage_map(&context_gl
->c
, state
);
3883 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3884 * need a 1:1 map at the moment.
3885 * When the mapping of a stage is changed, sampler and ALL texture stage
3886 * states have to be reset. */
3888 if (gl_info
->limits
.graphics_samplers
>= WINED3D_MAX_COMBINED_SAMPLERS
)
3892 wined3d_context_gl_map_psamplers(context_gl
, state
);
3894 wined3d_context_gl_map_fixed_function_samplers(context_gl
, state
);
3897 wined3d_context_gl_map_vsamplers(context_gl
, ps
, state
);
3900 /* Context activation is done by the caller. */
3901 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3903 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
3904 uint32_t rt_mask
, *cur_mask
;
3906 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
3908 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3909 rt_mask
= find_draw_buffers_mask(context_gl
, state
);
3910 if (rt_mask
!= *cur_mask
)
3912 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3913 *cur_mask
= rt_mask
;
3917 static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl
*context_gl
,
3918 const struct wined3d_state
*state
, enum wined3d_shader_type shader_type
)
3920 unsigned int bind_idx
, shader_sampler_count
, base
, count
, i
;
3921 const struct wined3d_device
*device
= context_gl
->c
.device
;
3922 struct wined3d_shader_sampler_map_entry
*entry
;
3923 struct wined3d_shader_resource_view
*view
;
3924 const struct wined3d_shader
*shader
;
3925 const unsigned int *tex_unit_map
;
3926 struct wined3d_sampler
*sampler
;
3928 if (!(shader
= state
->shader
[shader_type
]))
3931 tex_unit_map
= wined3d_context_gl_get_tex_unit_mapping(context_gl
,
3932 &shader
->reg_maps
.shader_version
, &base
, &count
);
3934 shader_sampler_count
= shader
->reg_maps
.sampler_map
.count
;
3935 if (shader_sampler_count
> count
)
3936 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3937 shader
, shader_sampler_count
, count
);
3938 count
= min(shader_sampler_count
, count
);
3940 for (i
= 0; i
< count
; ++i
)
3942 entry
= &shader
->reg_maps
.sampler_map
.entries
[i
];
3943 bind_idx
= base
+ entry
->bind_idx
;
3945 bind_idx
= tex_unit_map
[bind_idx
];
3947 if (!(view
= state
->shader_resource_view
[shader_type
][entry
->resource_idx
]))
3949 WARN("No resource view bound at index %u, %u.\n", shader_type
, entry
->resource_idx
);
3953 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
3954 sampler
= device
->default_sampler
;
3955 else if (!(sampler
= state
->sampler
[shader_type
][entry
->sampler_idx
]))
3956 sampler
= device
->null_sampler
;
3957 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view
),
3958 bind_idx
, wined3d_sampler_gl(sampler
), context_gl
);
3962 static void wined3d_context_gl_bind_unordered_access_views(struct wined3d_context_gl
*context_gl
,
3963 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
3965 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3966 struct wined3d_unordered_access_view_gl
*view_gl
;
3967 const struct wined3d_format_gl
*format_gl
;
3968 GLuint texture_name
;
3975 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
3979 if (shader
->reg_maps
.uav_resource_info
[i
].type
)
3980 WARN("No unordered access view bound at index %u.\n", i
);
3981 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
3985 view_gl
= wined3d_unordered_access_view_gl(views
[i
]);
3986 if (view_gl
->gl_view
.name
)
3988 texture_name
= view_gl
->gl_view
.name
;
3991 else if (view_gl
->v
.resource
->type
!= WINED3D_RTYPE_BUFFER
)
3993 struct wined3d_texture_gl
*texture_gl
= wined3d_texture_gl(texture_from_resource(view_gl
->v
.resource
));
3994 texture_name
= wined3d_texture_gl_get_texture_name(texture_gl
, &context_gl
->c
, FALSE
);
3995 level
= view_gl
->v
.desc
.u
.texture
.level_idx
;
3999 FIXME("Unsupported buffer unordered access view.\n");
4000 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
4004 format_gl
= wined3d_format_gl(view_gl
->v
.format
);
4005 GL_EXTCALL(glBindImageTexture(i
, texture_name
, level
, GL_TRUE
, 0, GL_READ_WRITE
,
4006 format_gl
->internal
));
4008 if (view_gl
->counter_bo
.id
)
4009 GL_EXTCALL(glBindBufferRange(GL_ATOMIC_COUNTER_BUFFER
, i
, view_gl
->counter_bo
.id
,
4010 view_gl
->counter_bo
.b
.buffer_offset
, view_gl
->counter_bo
.size
));
4012 checkGLcall("Bind unordered access views");
4015 static void context_gl_load_shader_resources(struct wined3d_context_gl
*context_gl
,
4016 const struct wined3d_state
*state
, unsigned int shader_mask
)
4018 struct wined3d_shader_sampler_map_entry
*entry
;
4019 struct wined3d_shader_resource_view_gl
*srv_gl
;
4020 struct wined3d_shader_resource_view
*view
;
4021 struct wined3d_shader
*shader
;
4022 struct wined3d_buffer
*buffer
;
4025 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
4027 if (!(shader_mask
& (1u << i
)))
4030 if (!(shader
= state
->shader
[i
]))
4033 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
4035 if (!state
->cb
[i
][j
].buffer
)
4038 buffer
= state
->cb
[i
][j
].buffer
;
4039 wined3d_buffer_load(buffer
, &context_gl
->c
, state
);
4040 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4041 if (!buffer
->bo_user
.valid
)
4042 device_invalidate_state(context_gl
->c
.device
, STATE_CONSTANT_BUFFER(i
));
4045 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
4047 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
4049 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
4052 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
4054 buffer
= buffer_from_resource(view
->resource
);
4055 wined3d_buffer_load(buffer
, &context_gl
->c
, state
);
4056 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4058 srv_gl
= wined3d_shader_resource_view_gl(view
);
4059 if (!srv_gl
->bo_user
.valid
)
4060 wined3d_shader_resource_view_gl_update(srv_gl
, context_gl
);
4064 wined3d_texture_load(texture_from_resource(view
->resource
), &context_gl
->c
, FALSE
);
4070 static void context_gl_load_unordered_access_resources(struct wined3d_context_gl
*context_gl
,
4071 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
4073 struct wined3d_unordered_access_view_gl
*uav_gl
;
4074 struct wined3d_unordered_access_view
*view
;
4075 struct wined3d_texture
*texture
;
4076 struct wined3d_buffer
*buffer
;
4079 context_gl
->c
.uses_uavs
= 0;
4084 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
4086 if (!(view
= views
[i
]))
4089 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
4091 buffer
= buffer_from_resource(view
->resource
);
4092 wined3d_buffer_load_location(buffer
, &context_gl
->c
, WINED3D_LOCATION_BUFFER
);
4093 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_BUFFER
);
4094 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4096 uav_gl
= wined3d_unordered_access_view_gl(view
);
4097 if (!uav_gl
->bo_user
.valid
)
4098 wined3d_unordered_access_view_gl_update(uav_gl
, context_gl
);
4102 texture
= texture_from_resource(view
->resource
);
4103 wined3d_texture_load(texture
, &context_gl
->c
, FALSE
);
4104 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_TEXTURE_RGB
);
4107 context_gl
->c
.uses_uavs
= 1;
4111 static void context_gl_load_stream_output_buffers(struct wined3d_context_gl
*context_gl
,
4112 const struct wined3d_state
*state
)
4114 struct wined3d_buffer
*buffer
;
4117 for (i
= 0; i
< ARRAY_SIZE(state
->stream_output
); ++i
)
4119 if (!(buffer
= state
->stream_output
[i
].buffer
))
4122 wined3d_buffer_load(buffer
, &context_gl
->c
, state
);
4123 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
4124 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4125 if (!buffer
->bo_user
.valid
)
4126 device_invalidate_state(context_gl
->c
.device
, STATE_STREAM_OUTPUT
);
4130 /* Context activation is done by the caller. */
4131 static BOOL
context_apply_draw_state(struct wined3d_context
*context
,
4132 const struct wined3d_device
*device
, const struct wined3d_state
*state
, BOOL indexed
)
4134 const struct wined3d_state_entry
*state_table
= context
->state_table
;
4135 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
4136 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4137 const struct wined3d_fb_state
*fb
= &state
->fb
;
4138 unsigned int i
, base
;
4141 context
->uses_fbo_attached_resources
= 0;
4143 if (!have_framebuffer_attachment(gl_info
->limits
.buffers
, fb
->render_targets
, fb
->depth_stencil
))
4145 if (!gl_info
->supported
[ARB_FRAMEBUFFER_NO_ATTACHMENTS
])
4147 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
4151 wined3d_context_gl_set_render_offscreen(context_gl
, TRUE
);
4154 /* Preload resources before FBO setup. Texture preload in particular may
4155 * result in changes to the current FBO, due to using e.g. FBO blits for
4156 * updating a resource location. */
4157 wined3d_context_gl_update_tex_unit_map(context_gl
, state
);
4158 context_preload_textures(context
, state
);
4159 context_gl_load_shader_resources(context_gl
, state
, ~(1u << WINED3D_SHADER_TYPE_COMPUTE
));
4160 context_gl_load_unordered_access_resources(context_gl
, state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
4161 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
4162 context_gl_load_stream_output_buffers(context_gl
, state
);
4163 /* TODO: Right now the dependency on the vertex shader is necessary
4164 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
4165 * the current VS but maybe it's possible to relax the coupling in some
4166 * situations at least. */
4167 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
4168 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
4170 context_update_stream_info(context
, state
);
4173 map
= context
->stream_info
.use_map
;
4176 const struct wined3d_stream_info_element
*e
;
4177 struct wined3d_buffer
*buffer
;
4179 e
= &context
->stream_info
.elements
[wined3d_bit_scan(&map
)];
4180 buffer
= state
->streams
[e
->stream_idx
].buffer
;
4182 if (!buffer
->bo_user
.valid
)
4183 device_invalidate_state(device
, STATE_STREAMSRC
);
4185 wined3d_buffer_load(buffer
, context
, state
);
4187 /* Loading the buffers above may have invalidated the stream info. */
4188 if (wined3d_context_is_graphics_state_dirty(context
, STATE_STREAMSRC
))
4189 context_update_stream_info(context
, state
);
4191 map
= context
->stream_info
.use_map
;
4194 const struct wined3d_stream_info_element
*e
;
4195 struct wined3d_buffer
*buffer
;
4197 e
= &context
->stream_info
.elements
[wined3d_bit_scan(&map
)];
4198 buffer
= state
->streams
[e
->stream_idx
].buffer
;
4200 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4203 if (indexed
&& state
->index_buffer
)
4205 struct wined3d_buffer
*buffer
= state
->index_buffer
;
4207 if (context
->stream_info
.all_vbo
)
4209 wined3d_buffer_load(buffer
, context
, state
);
4210 if (!buffer
->bo_user
.valid
)
4211 device_invalidate_state(device
, STATE_INDEXBUFFER
);
4212 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4216 wined3d_buffer_load_sysmem(buffer
, context
);
4220 for (i
= 0, base
= 0; i
< ARRAY_SIZE(context
->dirty_graphics_states
); ++i
)
4222 uint32_t dirty_mask
= context
->dirty_graphics_states
[i
];
4226 unsigned int state_id
= base
+ wined3d_bit_scan(&dirty_mask
);
4228 state_table
[state_id
].apply(context
, state
, state_id
);
4230 base
+= sizeof(dirty_mask
) * CHAR_BIT
;
4233 memset(context
->dirty_graphics_states
, 0, sizeof(context
->dirty_graphics_states
));
4235 if (context
->shader_update_mask
& ~(1u << WINED3D_SHADER_TYPE_COMPUTE
))
4237 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
4238 context
->shader_update_mask
&= 1u << WINED3D_SHADER_TYPE_COMPUTE
;
4241 if (context
->constant_update_mask
)
4243 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
4244 context
->constant_update_mask
= 0;
4247 if (context
->update_shader_resource_bindings
)
4249 for (i
= 0; i
< WINED3D_SHADER_TYPE_GRAPHICS_COUNT
; ++i
)
4250 wined3d_context_gl_bind_shader_resources(context_gl
, state
, i
);
4251 context
->update_shader_resource_bindings
= 0;
4252 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
4253 context
->update_compute_shader_resource_bindings
= 1;
4256 if (context
->update_unordered_access_view_bindings
)
4258 wined3d_context_gl_bind_unordered_access_views(context_gl
,
4259 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
4260 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
4261 context
->update_unordered_access_view_bindings
= 0;
4262 context
->update_compute_unordered_access_view_bindings
= 1;
4265 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
4266 wined3d_context_gl_check_fbo_status(context_gl
, GL_FRAMEBUFFER
);
4268 context
->last_was_blit
= FALSE
;
4269 context
->last_was_ffp_blit
= FALSE
;
4274 static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl
*context_gl
,
4275 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
4277 const struct wined3d_state_entry
*state_table
= context_gl
->c
.state_table
;
4278 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4279 unsigned int state_id
, i
;
4281 context_gl_load_shader_resources(context_gl
, state
, 1u << WINED3D_SHADER_TYPE_COMPUTE
);
4282 context_gl_load_unordered_access_resources(context_gl
, state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
4283 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
4285 for (i
= 0, state_id
= STATE_COMPUTE_OFFSET
; i
< ARRAY_SIZE(context_gl
->c
.dirty_compute_states
); ++i
)
4287 unsigned int dirty_mask
= context_gl
->c
.dirty_compute_states
[i
];
4291 unsigned int current_state_id
= state_id
+ wined3d_bit_scan(&dirty_mask
);
4292 state_table
[current_state_id
].apply(&context_gl
->c
, state
, current_state_id
);
4294 state_id
+= sizeof(*context_gl
->c
.dirty_compute_states
) * CHAR_BIT
;
4296 memset(context_gl
->c
.dirty_compute_states
, 0, sizeof(*context_gl
->c
.dirty_compute_states
));
4298 if (context_gl
->c
.shader_update_mask
& (1u << WINED3D_SHADER_TYPE_COMPUTE
))
4300 device
->shader_backend
->shader_select_compute(device
->shader_priv
, &context_gl
->c
, state
);
4301 context_gl
->c
.shader_update_mask
&= ~(1u << WINED3D_SHADER_TYPE_COMPUTE
);
4304 if (context_gl
->c
.update_compute_shader_resource_bindings
)
4306 wined3d_context_gl_bind_shader_resources(context_gl
, state
, WINED3D_SHADER_TYPE_COMPUTE
);
4307 context_gl
->c
.update_compute_shader_resource_bindings
= 0;
4308 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
4309 context_gl
->c
.update_shader_resource_bindings
= 1;
4312 if (context_gl
->c
.update_compute_unordered_access_view_bindings
)
4314 wined3d_context_gl_bind_unordered_access_views(context_gl
,
4315 state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
4316 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
4317 context_gl
->c
.update_compute_unordered_access_view_bindings
= 0;
4318 context_gl
->c
.update_unordered_access_view_bindings
= 1;
4321 /* Updates to currently bound render targets aren't necessarily coherent
4322 * between the graphics and compute pipelines. Unbind any currently bound
4323 * FBO here to ensure preceding updates to its attachments by the graphics
4324 * pipeline are visible to the compute pipeline.
4326 * Without this, the bloom effect in Nier:Automata is too bright on the
4327 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4328 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
4329 context_invalidate_state(&context_gl
->c
, STATE_FRAMEBUFFER
);
4331 context_gl
->c
.last_was_blit
= FALSE
;
4332 context_gl
->c
.last_was_ffp_blit
= FALSE
;
4335 void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl
*context_gl
)
4337 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4339 if (context_gl
->c
.transform_feedback_active
)
4341 GL_EXTCALL(glEndTransformFeedback());
4342 checkGLcall("glEndTransformFeedback");
4343 context_gl
->c
.transform_feedback_active
= 0;
4344 context_gl
->c
.transform_feedback_paused
= 0;
4348 static void wined3d_context_gl_pause_transform_feedback(struct wined3d_context_gl
*context_gl
, BOOL force
)
4350 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4352 if (!context_gl
->c
.transform_feedback_active
|| context_gl
->c
.transform_feedback_paused
)
4355 if (gl_info
->supported
[ARB_TRANSFORM_FEEDBACK2
])
4357 GL_EXTCALL(glPauseTransformFeedback());
4358 checkGLcall("glPauseTransformFeedback");
4359 context_gl
->c
.transform_feedback_paused
= 1;
4363 WARN("Cannot pause transform feedback operations.\n");
4366 wined3d_context_gl_end_transform_feedback(context_gl
);
4369 static void wined3d_context_gl_setup_target(struct wined3d_context_gl
*context_gl
,
4370 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4372 BOOL old_render_offscreen
= context_gl
->c
.render_offscreen
, render_offscreen
;
4374 render_offscreen
= wined3d_resource_is_offscreen(&texture
->resource
);
4375 if (context_gl
->c
.current_rt
.texture
== texture
4376 && context_gl
->c
.current_rt
.sub_resource_idx
== sub_resource_idx
4377 && render_offscreen
== old_render_offscreen
)
4380 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4381 * the alpha blend state changes with different render target formats. */
4382 if (!context_gl
->c
.current_rt
.texture
)
4384 context_invalidate_state(&context_gl
->c
, STATE_BLEND
);
4388 const struct wined3d_format
*old
= context_gl
->c
.current_rt
.texture
->resource
.format
;
4389 const struct wined3d_format
*new = texture
->resource
.format
;
4391 if (old
->id
!= new->id
)
4393 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4394 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
4395 || !(texture
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
4396 context_invalidate_state(&context_gl
->c
, STATE_BLEND
);
4399 /* When switching away from an offscreen render target, and we're not
4400 * using FBOs, we have to read the drawable into the texture. This is
4401 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4402 * There are some things that need care though. PreLoad needs a GL context,
4403 * and FindContext is called before the context is activated. It also
4404 * has to be called with the old rendertarget active, otherwise a
4405 * wrong drawable is read. */
4406 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
4407 && old_render_offscreen
&& (context_gl
->c
.current_rt
.texture
!= texture
4408 || context_gl
->c
.current_rt
.sub_resource_idx
!= sub_resource_idx
))
4410 struct wined3d_texture_gl
*prev_texture
= wined3d_texture_gl(context_gl
->c
.current_rt
.texture
);
4411 unsigned int prev_sub_resource_idx
= context_gl
->c
.current_rt
.sub_resource_idx
;
4413 /* Read the back buffer of the old drawable into the destination texture. */
4414 if (prev_texture
->texture_srgb
.name
)
4415 wined3d_texture_load(&prev_texture
->t
, &context_gl
->c
, TRUE
);
4416 wined3d_texture_load(&prev_texture
->t
, &context_gl
->c
, FALSE
);
4417 wined3d_texture_invalidate_location(&prev_texture
->t
, prev_sub_resource_idx
, WINED3D_LOCATION_DRAWABLE
);
4421 context_gl
->c
.current_rt
.texture
= texture
;
4422 context_gl
->c
.current_rt
.sub_resource_idx
= sub_resource_idx
;
4423 wined3d_context_gl_set_render_offscreen(context_gl
, render_offscreen
);
4426 static void wined3d_context_gl_activate(struct wined3d_context_gl
*context_gl
,
4427 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4429 wined3d_context_gl_enter(context_gl
);
4430 wined3d_context_gl_update_window(context_gl
);
4431 wined3d_context_gl_setup_target(context_gl
, texture
, sub_resource_idx
);
4432 if (!context_gl
->valid
)
4435 if (context_gl
!= wined3d_context_gl_get_current())
4437 if (!wined3d_context_gl_set_current(context_gl
))
4438 ERR("Failed to activate the new context.\n");
4440 else if (context_gl
->needs_set
)
4442 wined3d_context_gl_set_gl_context(context_gl
);
4446 struct wined3d_context
*wined3d_context_gl_acquire(const struct wined3d_device
*device
,
4447 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4449 struct wined3d_context_gl
*current_context
= wined3d_context_gl_get_current();
4450 struct wined3d_context_gl
*context_gl
;
4452 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device
, texture
, sub_resource_idx
);
4454 if (current_context
&& current_context
->c
.destroyed
)
4455 current_context
= NULL
;
4460 && current_context
->c
.current_rt
.texture
4461 && current_context
->c
.device
== device
)
4463 texture
= current_context
->c
.current_rt
.texture
;
4464 sub_resource_idx
= current_context
->c
.current_rt
.sub_resource_idx
;
4468 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
4470 if (swapchain
->back_buffers
)
4471 texture
= swapchain
->back_buffers
[0];
4473 texture
= swapchain
->front_buffer
;
4474 sub_resource_idx
= 0;
4478 if (current_context
&& current_context
->c
.current_rt
.texture
== texture
)
4480 context_gl
= current_context
;
4482 else if (!wined3d_resource_is_offscreen(&texture
->resource
))
4484 TRACE("Rendering onscreen.\n");
4486 if (!(context_gl
= wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(texture
->swapchain
))))
4491 TRACE("Rendering offscreen.\n");
4493 /* Stay with the current context if possible. Otherwise use the
4494 * context for the primary swapchain. */
4495 if (current_context
&& current_context
->c
.device
== device
)
4496 context_gl
= current_context
;
4497 else if (!(context_gl
= wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(device
->swapchains
[0]))))
4501 wined3d_context_gl_activate(context_gl
, texture
, sub_resource_idx
);
4503 return &context_gl
->c
;
4506 struct wined3d_context_gl
*wined3d_context_gl_reacquire(struct wined3d_context_gl
*context_gl
)
4508 struct wined3d_context
*acquired_context
;
4509 struct wined3d_device
*device
;
4511 if (!context_gl
|| context_gl
->tid
!= GetCurrentThreadId())
4514 device
= context_gl
->c
.device
;
4515 wined3d_from_cs(device
->cs
);
4517 if (context_gl
->c
.current_rt
.texture
)
4519 wined3d_context_gl_activate(context_gl
, context_gl
->c
.current_rt
.texture
,
4520 context_gl
->c
.current_rt
.sub_resource_idx
);
4524 acquired_context
= context_acquire(device
, NULL
, 0);
4525 if (acquired_context
!= &context_gl
->c
)
4526 ERR("Acquired context %p instead of %p.\n", acquired_context
, &context_gl
->c
);
4527 return wined3d_context_gl(acquired_context
);
4530 void dispatch_compute(struct wined3d_device
*device
, const struct wined3d_state
*state
,
4531 const struct wined3d_dispatch_parameters
*parameters
)
4533 const struct wined3d_gl_info
*gl_info
;
4534 struct wined3d_context_gl
*context_gl
;
4536 context_gl
= wined3d_context_gl(context_acquire(device
, NULL
, 0));
4537 if (!context_gl
->valid
)
4539 context_release(&context_gl
->c
);
4540 WARN("Invalid context, skipping dispatch.\n");
4543 gl_info
= context_gl
->gl_info
;
4545 if (!gl_info
->supported
[ARB_COMPUTE_SHADER
])
4547 context_release(&context_gl
->c
);
4548 FIXME("OpenGL implementation does not support compute shaders.\n");
4552 if (parameters
->indirect
)
4553 wined3d_buffer_load(parameters
->u
.indirect
.buffer
, &context_gl
->c
, state
);
4555 wined3d_context_gl_apply_compute_state(context_gl
, device
, state
);
4557 if (parameters
->indirect
)
4559 const struct wined3d_indirect_dispatch_parameters
*indirect
= ¶meters
->u
.indirect
;
4560 struct wined3d_bo_gl
*bo_gl
= wined3d_bo_gl(indirect
->buffer
->buffer_object
);
4562 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, bo_gl
->id
));
4563 GL_EXTCALL(glDispatchComputeIndirect(bo_gl
->b
.buffer_offset
+ (GLintptr
)indirect
->offset
));
4564 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0));
4565 wined3d_context_gl_reference_bo(context_gl
, bo_gl
);
4569 const struct wined3d_direct_dispatch_parameters
*direct
= ¶meters
->u
.direct
;
4570 GL_EXTCALL(glDispatchCompute(direct
->group_count_x
, direct
->group_count_y
, direct
->group_count_z
));
4572 checkGLcall("dispatch compute");
4574 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS
));
4575 checkGLcall("glMemoryBarrier");
4577 context_release(&context_gl
->c
);
4580 /* Context activation is done by the caller. */
4581 static void wined3d_context_gl_draw_primitive_arrays(struct wined3d_context_gl
*context_gl
,
4582 const struct wined3d_state
*state
, const void *idx_data
, unsigned int idx_size
, int base_vertex_idx
,
4583 unsigned int start_idx
, unsigned int count
, unsigned int start_instance
, unsigned int instance_count
)
4585 const struct wined3d_ffp_attrib_ops
*ops
= &context_gl
->c
.d3d_info
->ffp_attrib_ops
;
4586 GLenum idx_type
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
4587 const struct wined3d_stream_info
*si
= &context_gl
->c
.stream_info
;
4588 GLenum mode
= gl_primitive_type_from_d3d(state
->primitive_type
);
4589 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4590 unsigned int instanced_elements
[ARRAY_SIZE(si
->elements
)];
4591 unsigned int instanced_element_count
= 0;
4592 const void *indices
;
4595 indices
= (const char *)idx_data
+ idx_size
* start_idx
;
4597 if (!instance_count
)
4601 gl_info
->gl_ops
.gl
.p_glDrawArrays(mode
, start_idx
, count
);
4602 checkGLcall("glDrawArrays");
4606 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4608 GL_EXTCALL(glDrawElementsBaseVertex(mode
, count
, idx_type
, indices
, base_vertex_idx
));
4609 checkGLcall("glDrawElementsBaseVertex");
4613 gl_info
->gl_ops
.gl
.p_glDrawElements(mode
, count
, idx_type
, indices
);
4614 checkGLcall("glDrawElements");
4618 if (start_instance
&& !(gl_info
->supported
[ARB_BASE_INSTANCE
] && gl_info
->supported
[ARB_INSTANCED_ARRAYS
]))
4619 FIXME("Start instance (%u) not supported.\n", start_instance
);
4621 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
4625 if (gl_info
->supported
[ARB_BASE_INSTANCE
])
4627 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode
, start_idx
, count
, instance_count
, start_instance
));
4628 checkGLcall("glDrawArraysInstancedBaseInstance");
4632 GL_EXTCALL(glDrawArraysInstanced(mode
, start_idx
, count
, instance_count
));
4633 checkGLcall("glDrawArraysInstanced");
4637 if (gl_info
->supported
[ARB_BASE_INSTANCE
])
4639 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode
, count
, idx_type
,
4640 indices
, instance_count
, base_vertex_idx
, start_instance
));
4641 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4644 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4646 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode
, count
, idx_type
,
4647 indices
, instance_count
, base_vertex_idx
));
4648 checkGLcall("glDrawElementsInstancedBaseVertex");
4652 GL_EXTCALL(glDrawElementsInstanced(mode
, count
, idx_type
, indices
, instance_count
));
4653 checkGLcall("glDrawElementsInstanced");
4657 /* Instancing emulation by mixing immediate mode and arrays. */
4659 /* This is a nasty thing. MSDN says no hardware supports this and
4660 * applications have to use software vertex processing. We don't support
4663 * Shouldn't be too hard to support with OpenGL, in theory just call
4664 * glDrawArrays() instead of drawElements(). But the stream frequency value
4665 * has a different meaning in that situation. */
4668 FIXME("Non-indexed instanced drawing is not supported.\n");
4672 for (i
= 0; i
< ARRAY_SIZE(si
->elements
); ++i
)
4674 if (!(si
->use_map
& (1u << i
)))
4677 if (state
->streams
[si
->elements
[i
].stream_idx
].flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
4678 instanced_elements
[instanced_element_count
++] = i
;
4681 for (i
= 0; i
< instance_count
; ++i
)
4683 /* Specify the instanced attributes using immediate mode calls. */
4684 for (j
= 0; j
< instanced_element_count
; ++j
)
4686 const struct wined3d_stream_info_element
*element
;
4687 unsigned int element_idx
;
4690 element_idx
= instanced_elements
[j
];
4691 element
= &si
->elements
[element_idx
];
4692 ptr
= element
->data
.addr
+ element
->stride
* i
;
4693 if (element
->data
.buffer_object
)
4694 ptr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(state
->streams
[element
->stream_idx
].buffer
,
4696 ops
->generic
[element
->format
->emit_idx
](element_idx
, ptr
);
4699 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4701 GL_EXTCALL(glDrawElementsBaseVertex(mode
, count
, idx_type
, indices
, base_vertex_idx
));
4702 checkGLcall("glDrawElementsBaseVertex");
4706 gl_info
->gl_ops
.gl
.p_glDrawElements(mode
, count
, idx_type
, indices
);
4707 checkGLcall("glDrawElements");
4712 static unsigned int get_stride_idx(const void *idx_data
, unsigned int idx_size
,
4713 unsigned int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_idx
)
4716 return start_idx
+ vertex_idx
;
4718 return ((const WORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
4719 return ((const DWORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
4722 /* Context activation is done by the caller. */
4723 static void draw_primitive_immediate_mode(struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
,
4724 const struct wined3d_stream_info
*si
, const void *idx_data
, unsigned int idx_size
,
4725 int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_count
, unsigned int instance_count
)
4727 const BYTE
*position
= NULL
, *normal
= NULL
, *diffuse
= NULL
, *specular
= NULL
;
4728 const struct wined3d_d3d_info
*d3d_info
= context_gl
->c
.d3d_info
;
4729 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4730 unsigned int coord_idx
, stride_idx
, texture_idx
, vertex_idx
;
4731 const struct wined3d_stream_info_element
*element
;
4732 const BYTE
*tex_coords
[WINED3DDP_MAXTEXCOORD
];
4733 unsigned int texture_unit
, texture_stages
;
4734 const struct wined3d_ffp_attrib_ops
*ops
;
4735 unsigned int untracked_material_count
;
4736 unsigned int tex_mask
= 0;
4737 BOOL specular_fog
= FALSE
;
4738 BOOL ps
= use_ps(state
);
4741 static unsigned int once
;
4744 FIXME_(d3d_perf
)("Drawing using immediate mode.\n");
4746 WARN_(d3d_perf
)("Drawing using immediate mode.\n");
4748 if (!idx_size
&& idx_data
)
4749 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4752 FIXME("Instancing not implemented.\n");
4754 /* Immediate mode drawing can't make use of indices in a VBO - get the
4755 * data from the index buffer. */
4757 idx_data
= wined3d_buffer_load_sysmem(state
->index_buffer
, &context_gl
->c
) + state
->index_offset
;
4759 ops
= &d3d_info
->ffp_attrib_ops
;
4761 gl_info
->gl_ops
.gl
.p_glBegin(gl_primitive_type_from_d3d(state
->primitive_type
));
4763 if (use_vs(state
) || d3d_info
->ffp_generic_attributes
)
4765 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
4767 unsigned int use_map
= si
->use_map
;
4768 unsigned int element_idx
;
4770 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
4771 for (element_idx
= MAX_ATTRIBS
- 1; use_map
; use_map
&= ~(1u << element_idx
), --element_idx
)
4773 if (!(use_map
& 1u << element_idx
))
4776 ptr
= si
->elements
[element_idx
].data
.addr
+ si
->elements
[element_idx
].stride
* stride_idx
;
4777 ops
->generic
[si
->elements
[element_idx
].format
->emit_idx
](element_idx
, ptr
);
4781 gl_info
->gl_ops
.gl
.p_glEnd();
4785 if (si
->use_map
& (1u << WINED3D_FFP_POSITION
))
4786 position
= si
->elements
[WINED3D_FFP_POSITION
].data
.addr
;
4788 if (si
->use_map
& (1u << WINED3D_FFP_NORMAL
))
4789 normal
= si
->elements
[WINED3D_FFP_NORMAL
].data
.addr
;
4791 gl_info
->gl_ops
.gl
.p_glNormal3f(0.0f
, 0.0f
, 0.0f
);
4793 untracked_material_count
= context_gl
->untracked_material_count
;
4794 if (si
->use_map
& (1u << WINED3D_FFP_DIFFUSE
))
4796 element
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
4797 diffuse
= element
->data
.addr
;
4799 if (untracked_material_count
&& element
->format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
4800 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element
->format
->id
));
4804 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
4807 if (si
->use_map
& (1u << WINED3D_FFP_SPECULAR
))
4809 element
= &si
->elements
[WINED3D_FFP_SPECULAR
];
4810 specular
= element
->data
.addr
;
4812 /* Special case where the fog density is stored in the specular alpha channel. */
4813 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
4814 && (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
4815 || si
->elements
[WINED3D_FFP_POSITION
].format
->id
== WINED3DFMT_R32G32B32A32_FLOAT
)
4816 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
4818 if (gl_info
->supported
[EXT_FOG_COORD
])
4820 if (element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
4821 specular_fog
= TRUE
;
4823 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element
->format
->id
));
4827 static unsigned int once
;
4830 FIXME("Implement fog for transformed vertices in software.\n");
4834 else if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
4836 GL_EXTCALL(glSecondaryColor3fEXT
)(0.0f
, 0.0f
, 0.0f
);
4839 texture_stages
= d3d_info
->limits
.ffp_blend_stages
;
4840 for (texture_idx
= 0; texture_idx
< texture_stages
; ++texture_idx
)
4842 if (!gl_info
->supported
[ARB_MULTITEXTURE
] && texture_idx
> 0)
4844 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4848 if (!ps
&& !state
->textures
[texture_idx
])
4851 texture_unit
= context_gl
->tex_unit_map
[texture_idx
];
4852 if (texture_unit
== WINED3D_UNMAPPED_STAGE
)
4855 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
4858 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx
, texture_idx
);
4862 if (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
)))
4864 tex_coords
[coord_idx
] = si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].data
.addr
;
4865 tex_mask
|= (1u << texture_idx
);
4869 TRACE("Setting default coordinates for texture %u.\n", texture_idx
);
4870 if (gl_info
->supported
[ARB_MULTITEXTURE
])
4871 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_unit
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
4873 gl_info
->gl_ops
.gl
.p_glTexCoord4f(0.0f
, 0.0f
, 0.0f
, 1.0f
);
4877 /* Blending data and point sizes are not supported by this function. They
4878 * are not supported by the fixed function pipeline at all. A FIXME for
4879 * them is printed after decoding the vertex declaration. */
4880 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
4882 uint32_t tmp_tex_mask
;
4884 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
4888 ptr
= normal
+ stride_idx
* si
->elements
[WINED3D_FFP_NORMAL
].stride
;
4889 ops
->normal
[si
->elements
[WINED3D_FFP_NORMAL
].format
->emit_idx
](ptr
);
4894 ptr
= diffuse
+ stride_idx
* si
->elements
[WINED3D_FFP_DIFFUSE
].stride
;
4895 ops
->diffuse
[si
->elements
[WINED3D_FFP_DIFFUSE
].format
->emit_idx
](ptr
);
4897 if (untracked_material_count
)
4899 struct wined3d_color color
;
4902 wined3d_color_from_d3dcolor(&color
, *(const DWORD
*)ptr
);
4903 for (i
= 0; i
< untracked_material_count
; ++i
)
4905 gl_info
->gl_ops
.gl
.p_glMaterialfv(GL_FRONT_AND_BACK
,
4906 context_gl
->untracked_materials
[i
], &color
.r
);
4913 ptr
= specular
+ stride_idx
* si
->elements
[WINED3D_FFP_SPECULAR
].stride
;
4914 ops
->specular
[si
->elements
[WINED3D_FFP_SPECULAR
].format
->emit_idx
](ptr
);
4917 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD
*)ptr
>> 24)));
4920 tmp_tex_mask
= tex_mask
;
4921 while (tmp_tex_mask
)
4923 texture_idx
= wined3d_bit_scan(&tmp_tex_mask
);
4924 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
4925 ptr
= tex_coords
[coord_idx
] + (stride_idx
* si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].stride
);
4926 ops
->texcoord
[si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].format
->emit_idx
](
4927 GL_TEXTURE0_ARB
+ context_gl
->tex_unit_map
[texture_idx
], ptr
);
4932 ptr
= position
+ stride_idx
* si
->elements
[WINED3D_FFP_POSITION
].stride
;
4933 ops
->position
[si
->elements
[WINED3D_FFP_POSITION
].format
->emit_idx
](ptr
);
4937 gl_info
->gl_ops
.gl
.p_glEnd();
4938 checkGLcall("draw immediate mode");
4941 static void wined3d_context_gl_draw_indirect(struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
,
4942 const struct wined3d_indirect_draw_parameters
*parameters
, unsigned int idx_size
)
4944 struct wined3d_bo_gl
*bo_gl
= wined3d_bo_gl(parameters
->buffer
->buffer_object
);
4945 GLenum gl_primitive_type
= gl_primitive_type_from_d3d(state
->primitive_type
);
4946 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4949 if (!gl_info
->supported
[ARB_DRAW_INDIRECT
])
4951 FIXME("OpenGL implementation does not support indirect draws.\n");
4955 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER
, bo_gl
->id
));
4957 offset
= (const uint8_t *)bo_gl
->b
.buffer_offset
+ parameters
->offset
;
4960 GLenum idx_type
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
4961 if (state
->index_offset
)
4962 FIXME("Ignoring index offset %u.\n", state
->index_offset
);
4963 GL_EXTCALL(glDrawElementsIndirect(gl_primitive_type
, idx_type
, offset
));
4967 GL_EXTCALL(glDrawArraysIndirect(gl_primitive_type
, offset
));
4970 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER
, 0));
4971 wined3d_context_gl_reference_bo(context_gl
, bo_gl
);
4973 checkGLcall("draw indirect");
4976 static void remove_vbos(struct wined3d_context
*context
,
4977 const struct wined3d_state
*state
, struct wined3d_stream_info
*s
)
4981 for (i
= 0; i
< ARRAY_SIZE(s
->elements
); ++i
)
4983 struct wined3d_stream_info_element
*e
;
4985 if (!(s
->use_map
& (1u << i
)))
4988 e
= &s
->elements
[i
];
4989 if (e
->data
.buffer_object
)
4991 struct wined3d_buffer
*vb
= state
->streams
[e
->stream_idx
].buffer
;
4992 e
->data
.buffer_object
= 0;
4993 e
->data
.addr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(vb
, context
);
4998 static GLenum
gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
5000 GLenum gl_primitive_type
= gl_primitive_type_from_d3d(primitive_type
);
5001 switch (gl_primitive_type
)
5007 case GL_LINE_STRIP_ADJACENCY
:
5008 case GL_LINES_ADJACENCY
:
5012 case GL_TRIANGLE_FAN
:
5013 case GL_TRIANGLE_STRIP
:
5014 case GL_TRIANGLE_STRIP_ADJACENCY
:
5015 case GL_TRIANGLES_ADJACENCY
:
5017 return GL_TRIANGLES
;
5020 return gl_primitive_type
;
5024 /* Routine common to the draw primitive and draw indexed primitive routines */
5025 void draw_primitive(struct wined3d_device
*device
, const struct wined3d_state
*state
,
5026 const struct wined3d_draw_parameters
*parameters
)
5028 BOOL emulation
= FALSE
, rasterizer_discard
= FALSE
;
5029 const struct wined3d_fb_state
*fb
= &state
->fb
;
5030 const struct wined3d_stream_info
*stream_info
;
5031 struct wined3d_rendertarget_view
*dsv
, *rtv
;
5032 struct wined3d_stream_info si_emulated
;
5033 const struct wined3d_gl_info
*gl_info
;
5034 struct wined3d_context_gl
*context_gl
;
5035 struct wined3d_context
*context
;
5036 unsigned int i
, idx_size
= 0;
5037 const void *idx_data
= NULL
;
5039 TRACE("device %p, state %p, parameters %p.\n", device
, state
, parameters
);
5041 if (!parameters
->indirect
&& !parameters
->u
.direct
.index_count
)
5044 if (!parameters
->indirect
)
5045 TRACE("base_vertex_idx %d, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
5046 parameters
->u
.direct
.base_vertex_idx
, parameters
->u
.direct
.start_idx
,
5047 parameters
->u
.direct
.index_count
, parameters
->u
.direct
.start_instance
,
5048 parameters
->u
.direct
.instance_count
);
5050 if (!(rtv
= fb
->render_targets
[0]))
5051 rtv
= fb
->depth_stencil
;
5053 if (rtv
&& rtv
->resource
->type
== WINED3D_RTYPE_BUFFER
)
5055 FIXME("Buffer render targets not implemented.\n");
5060 context
= context_acquire(device
, wined3d_texture_from_resource(rtv
->resource
), rtv
->sub_resource_idx
);
5062 context
= context_acquire(device
, NULL
, 0);
5063 context_gl
= wined3d_context_gl(context
);
5064 if (!context_gl
->valid
)
5066 context_release(context
);
5067 WARN("Invalid context, skipping draw.\n");
5070 gl_info
= context_gl
->gl_info
;
5072 if (!use_transform_feedback(state
))
5073 wined3d_context_gl_pause_transform_feedback(context_gl
, TRUE
);
5075 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
5077 if (!(rtv
= fb
->render_targets
[i
]) || rtv
->format
->id
== WINED3DFMT_NULL
)
5080 if (wined3d_blend_state_get_writemask(state
->blend_state
, i
))
5082 wined3d_rendertarget_view_load_location(rtv
, context
, rtv
->resource
->draw_binding
);
5083 wined3d_rendertarget_view_invalidate_location(rtv
, ~rtv
->resource
->draw_binding
);
5087 wined3d_rendertarget_view_prepare_location(rtv
, context
, rtv
->resource
->draw_binding
);
5091 if ((dsv
= fb
->depth_stencil
))
5093 /* Note that this depends on the context_acquire() call above to set
5094 * context->render_offscreen properly. We don't currently take the
5095 * Z-compare function into account, but we could skip loading the
5096 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
5097 * that we never copy the stencil data.*/
5098 DWORD location
= context
->render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
5100 if (wined3d_state_uses_depth_buffer(state
))
5101 wined3d_rendertarget_view_load_location(dsv
, context
, location
);
5103 wined3d_rendertarget_view_prepare_location(dsv
, context
, location
);
5106 if (parameters
->indirect
)
5107 wined3d_buffer_load(parameters
->u
.indirect
.buffer
, context
, state
);
5109 if (!context_apply_draw_state(context
, device
, state
, parameters
->indexed
))
5111 context_release(context
);
5112 WARN("Unable to apply draw state, skipping draw.\n");
5116 if (dsv
&& (!state
->depth_stencil_state
|| state
->depth_stencil_state
->desc
.depth_write
))
5118 DWORD location
= context
->render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
5120 wined3d_rendertarget_view_validate_location(dsv
, location
);
5121 wined3d_rendertarget_view_invalidate_location(dsv
, ~location
);
5124 stream_info
= &context
->stream_info
;
5126 if (parameters
->indexed
)
5128 struct wined3d_buffer
*index_buffer
= state
->index_buffer
;
5129 struct wined3d_bo
*bo
= index_buffer
->buffer_object
;
5131 if (!bo
|| !stream_info
->all_vbo
)
5132 idx_data
= index_buffer
->resource
.heap_memory
;
5134 idx_data
= (void *)bo
->buffer_offset
;
5135 idx_data
= (const BYTE
*)idx_data
+ state
->index_offset
;
5137 if (state
->index_format
== WINED3DFMT_R16_UINT
)
5145 if (!stream_info
->position_transformed
&& context_gl
->untracked_material_count
5146 && state
->render_states
[WINED3D_RS_LIGHTING
])
5151 FIXME("Using software emulation because not all material properties could be tracked.\n");
5153 WARN_(d3d_perf
)("Using software emulation because not all material properties could be tracked.\n");
5156 else if (context
->fog_coord
&& state
->render_states
[WINED3D_RS_FOGENABLE
])
5160 /* Either write a pipeline replacement shader or convert the
5161 * specular alpha from unsigned byte to a float in the vertex
5164 FIXME("Using software emulation because manual fog coordinates are provided.\n");
5166 WARN_(d3d_perf
)("Using software emulation because manual fog coordinates are provided.\n");
5172 si_emulated
= context
->stream_info
;
5173 remove_vbos(context
, state
, &si_emulated
);
5174 stream_info
= &si_emulated
;
5178 if (use_transform_feedback(state
))
5180 const struct wined3d_shader
*shader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
5182 if (is_rasterization_disabled(shader
))
5184 glEnable(GL_RASTERIZER_DISCARD
);
5185 checkGLcall("enable rasterizer discard");
5186 rasterizer_discard
= TRUE
;
5189 if (context
->transform_feedback_paused
)
5191 GL_EXTCALL(glResumeTransformFeedback());
5192 checkGLcall("glResumeTransformFeedback");
5193 context
->transform_feedback_paused
= 0;
5195 else if (!context
->transform_feedback_active
)
5197 enum wined3d_primitive_type primitive_type
= shader
->u
.gs
.output_type
5198 ? shader
->u
.gs
.output_type
: state
->primitive_type
;
5199 GLenum mode
= gl_tfb_primitive_type_from_d3d(primitive_type
);
5200 GL_EXTCALL(glBeginTransformFeedback(mode
));
5201 checkGLcall("glBeginTransformFeedback");
5202 context
->transform_feedback_active
= 1;
5206 if (state
->primitive_type
== WINED3D_PT_PATCH
)
5208 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES
, state
->patch_vertex_count
));
5209 checkGLcall("glPatchParameteri");
5212 if (context
->uses_fbo_attached_resources
)
5214 static unsigned int fixme_once
;
5216 if (gl_info
->supported
[ARB_TEXTURE_BARRIER
])
5218 GL_EXTCALL(glTextureBarrier());
5220 else if (gl_info
->supported
[NV_TEXTURE_BARRIER
])
5222 GL_EXTCALL(glTextureBarrierNV());
5227 FIXME("Sampling attached render targets is not supported.\n");
5229 WARN("Sampling attached render targets is not supported, skipping draw.\n");
5230 context_release(context
);
5233 checkGLcall("glTextureBarrier");
5236 if (parameters
->indirect
)
5238 if (!context
->use_immediate_mode_draw
&& !emulation
)
5239 wined3d_context_gl_draw_indirect(context_gl
, state
, ¶meters
->u
.indirect
, idx_size
);
5241 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
5245 unsigned int instance_count
= parameters
->u
.direct
.instance_count
;
5246 if (context
->instance_count
)
5247 instance_count
= context
->instance_count
;
5249 if (context
->use_immediate_mode_draw
|| emulation
)
5250 draw_primitive_immediate_mode(wined3d_context_gl(context
), state
, stream_info
, idx_data
,
5251 idx_size
, parameters
->u
.direct
.base_vertex_idx
,
5252 parameters
->u
.direct
.start_idx
, parameters
->u
.direct
.index_count
, instance_count
);
5254 wined3d_context_gl_draw_primitive_arrays(context_gl
, state
, idx_data
, idx_size
,
5255 parameters
->u
.direct
.base_vertex_idx
, parameters
->u
.direct
.start_idx
,
5256 parameters
->u
.direct
.index_count
, parameters
->u
.direct
.start_instance
, instance_count
);
5259 if (context
->uses_uavs
)
5261 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS
));
5262 checkGLcall("glMemoryBarrier");
5265 wined3d_context_gl_pause_transform_feedback(context_gl
, FALSE
);
5267 if (rasterizer_discard
)
5269 glDisable(GL_RASTERIZER_DISCARD
);
5270 checkGLcall("disable rasterizer discard");
5273 context_release(context
);
5275 TRACE("Draw completed.\n");
5278 void wined3d_context_gl_unload_tex_coords(const struct wined3d_context_gl
*context_gl
)
5280 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5281 unsigned int texture_idx
;
5283 for (texture_idx
= 0; texture_idx
< gl_info
->limits
.texture_coords
; ++texture_idx
)
5285 gl_info
->gl_ops
.ext
.p_glClientActiveTextureARB(GL_TEXTURE0_ARB
+ texture_idx
);
5286 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
5290 static const void *get_vertex_attrib_pointer(const struct wined3d_stream_info_element
*element
,
5291 const struct wined3d_state
*state
)
5293 const uint8_t *offset
= element
->data
.addr
+ state
->load_base_vertex_index
* element
->stride
;
5295 if (element
->data
.buffer_object
)
5296 offset
+= element
->data
.buffer_object
->buffer_offset
;
5300 void wined3d_context_gl_load_tex_coords(const struct wined3d_context_gl
*context_gl
,
5301 const struct wined3d_stream_info
*si
, GLuint
*current_bo
, const struct wined3d_state
*state
)
5303 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5304 const struct wined3d_format_gl
*format_gl
;
5305 unsigned int mapped_stage
= 0;
5306 unsigned int texture_idx
;
5309 for (texture_idx
= 0; texture_idx
< context_gl
->c
.d3d_info
->limits
.ffp_blend_stages
; ++texture_idx
)
5311 unsigned int coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
5313 if ((mapped_stage
= context_gl
->tex_unit_map
[texture_idx
]) == WINED3D_UNMAPPED_STAGE
)
5316 if (mapped_stage
>= gl_info
->limits
.texture_coords
)
5318 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage
);
5322 if (coord_idx
< WINED3D_MAX_TEXTURES
&& (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
))))
5324 const struct wined3d_stream_info_element
*e
= &si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
];
5326 TRACE("Setting up texture %u, idx %u, coord_idx %u, data %s.\n",
5327 texture_idx
, mapped_stage
, coord_idx
, debug_bo_address(&e
->data
));
5329 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5330 if (*current_bo
!= bo
)
5332 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5333 checkGLcall("glBindBuffer");
5337 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB
+ mapped_stage
));
5338 checkGLcall("glClientActiveTextureARB");
5340 /* The coords to supply depend completely on the fvf/vertex shader. */
5341 format_gl
= wined3d_format_gl(e
->format
);
5342 gl_info
->gl_ops
.gl
.p_glTexCoordPointer(format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
,
5343 get_vertex_attrib_pointer(e
, state
));
5344 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
5345 state
->streams
[e
->stream_idx
].buffer
->bo_user
.valid
= true;
5349 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ mapped_stage
, 0, 0, 0, 1));
5352 if (gl_info
->supported
[NV_REGISTER_COMBINERS
])
5354 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
5355 for (texture_idx
= mapped_stage
+ 1; texture_idx
< gl_info
->limits
.textures
; ++texture_idx
)
5357 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_idx
, 0, 0, 0, 1));
5361 checkGLcall("loadTexCoords");
5364 /* This should match any arrays loaded in wined3d_context_gl_load_vertex_data(). */
5365 static void wined3d_context_gl_unload_vertex_data(struct wined3d_context_gl
*context_gl
)
5367 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5369 if (!context_gl
->c
.namedArraysLoaded
)
5371 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_VERTEX_ARRAY
);
5372 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_NORMAL_ARRAY
);
5373 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_COLOR_ARRAY
);
5374 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5375 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT
);
5376 wined3d_context_gl_unload_tex_coords(context_gl
);
5377 context_gl
->c
.namedArraysLoaded
= FALSE
;
5380 static void wined3d_context_gl_load_vertex_data(struct wined3d_context_gl
*context_gl
,
5381 const struct wined3d_stream_info
*si
, const struct wined3d_state
*state
)
5383 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5384 const struct wined3d_stream_info_element
*e
;
5385 const struct wined3d_format_gl
*format_gl
;
5386 GLuint current_bo
, bo
;
5389 TRACE("context_gl %p, si %p, state %p.\n", context_gl
, si
, state
);
5391 /* This is used for the fixed-function pipeline only, and the
5392 * fixed-function pipeline doesn't do instancing. */
5393 context_gl
->c
.instance_count
= 0;
5394 current_bo
= gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
] ? ~0u : 0;
5397 if ((si
->use_map
& (1u << WINED3D_FFP_BLENDWEIGHT
))
5398 || si
->use_map
& (1u << WINED3D_FFP_BLENDINDICES
))
5400 /* TODO: Support vertex blending in immediate mode draws. No need to
5401 * write a FIXME here, this is done after the general vertex
5402 * declaration decoding. */
5403 WARN("Vertex blending not supported.\n");
5407 if (si
->use_map
& (1u << WINED3D_FFP_PSIZE
))
5409 /* No such functionality in the fixed-function GL pipeline. */
5410 WARN("Per-vertex point size not supported.\n");
5414 if (si
->use_map
& (1u << WINED3D_FFP_POSITION
))
5416 e
= &si
->elements
[WINED3D_FFP_POSITION
];
5417 format_gl
= wined3d_format_gl(e
->format
);
5418 offset
= get_vertex_attrib_pointer(e
, state
);
5420 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5421 if (current_bo
!= bo
)
5423 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5424 checkGLcall("glBindBuffer");
5428 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n", format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5429 gl_info
->gl_ops
.gl
.p_glVertexPointer(format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5430 checkGLcall("glVertexPointer(...)");
5431 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_VERTEX_ARRAY
);
5432 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5433 state
->streams
[e
->stream_idx
].buffer
->bo_user
.valid
= true;
5437 if (si
->use_map
& (1u << WINED3D_FFP_NORMAL
))
5439 e
= &si
->elements
[WINED3D_FFP_NORMAL
];
5440 format_gl
= wined3d_format_gl(e
->format
);
5441 offset
= get_vertex_attrib_pointer(e
, state
);
5443 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5444 if (current_bo
!= bo
)
5446 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5447 checkGLcall("glBindBuffer");
5451 TRACE("glNormalPointer(%#x, %#x, %p);\n", format_gl
->vtx_type
, e
->stride
, offset
);
5452 gl_info
->gl_ops
.gl
.p_glNormalPointer(format_gl
->vtx_type
, e
->stride
, offset
);
5453 checkGLcall("glNormalPointer(...)");
5454 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_NORMAL_ARRAY
);
5455 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5456 state
->streams
[e
->stream_idx
].buffer
->bo_user
.valid
= true;
5460 gl_info
->gl_ops
.gl
.p_glNormal3f(0, 0, 0);
5461 checkGLcall("glNormal3f(0, 0, 0)");
5464 /* Diffuse colour */
5465 if (si
->use_map
& (1u << WINED3D_FFP_DIFFUSE
))
5467 e
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
5468 format_gl
= wined3d_format_gl(e
->format
);
5469 offset
= get_vertex_attrib_pointer(e
, state
);
5471 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5472 if (current_bo
!= bo
)
5474 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5475 checkGLcall("glBindBuffer");
5479 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5480 format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5481 gl_info
->gl_ops
.gl
.p_glColorPointer(format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5482 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5483 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_COLOR_ARRAY
);
5484 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5485 state
->streams
[e
->stream_idx
].buffer
->bo_user
.valid
= true;
5489 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
5490 checkGLcall("glColor4f(1, 1, 1, 1)");
5493 /* Specular colour */
5494 if (si
->use_map
& (1u << WINED3D_FFP_SPECULAR
))
5496 TRACE("Setting specular colour.\n");
5498 e
= &si
->elements
[WINED3D_FFP_SPECULAR
];
5499 offset
= get_vertex_attrib_pointer(e
, state
);
5501 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5506 format_gl
= wined3d_format_gl(e
->format
);
5507 type
= format_gl
->vtx_type
;
5508 format
= format_gl
->vtx_format
;
5510 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5511 if (current_bo
!= bo
)
5513 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5514 checkGLcall("glBindBuffer");
5518 if (format
!= 4 || (gl_info
->quirks
& WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA
))
5520 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5521 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5522 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5523 * 4 component secondary colors use it
5525 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format
, type
, e
->stride
, offset
);
5526 GL_EXTCALL(glSecondaryColorPointerEXT(format
, type
, e
->stride
, offset
));
5527 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5533 case GL_UNSIGNED_BYTE
:
5534 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e
->stride
, offset
);
5535 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE
, e
->stride
, offset
));
5536 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5540 FIXME("Add 4 component specular colour pointers for type %#x.\n", type
);
5541 /* Make sure that the right colour component is dropped. */
5542 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type
, e
->stride
, offset
);
5543 GL_EXTCALL(glSecondaryColorPointerEXT(3, type
, e
->stride
, offset
));
5544 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5547 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT
);
5548 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5549 state
->streams
[e
->stream_idx
].buffer
->bo_user
.valid
= true;
5553 WARN("Specular colour is not supported in this GL implementation.\n");
5558 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5560 GL_EXTCALL(glSecondaryColor3fEXT
)(0, 0, 0);
5561 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5565 WARN("Specular colour is not supported in this GL implementation.\n");
5569 /* Texture coordinates */
5570 wined3d_context_gl_load_tex_coords(context_gl
, si
, ¤t_bo
, state
);
5573 static void wined3d_context_gl_unload_numbered_array(struct wined3d_context_gl
*context_gl
, unsigned int i
)
5575 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5577 GL_EXTCALL(glDisableVertexAttribArray(i
));
5578 checkGLcall("glDisableVertexAttribArray");
5579 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
5580 GL_EXTCALL(glVertexAttribDivisor(i
, 0));
5582 context_gl
->c
.numbered_array_mask
&= ~(1u << i
);
5585 static void wined3d_context_gl_unload_numbered_arrays(struct wined3d_context_gl
*context_gl
)
5587 uint32_t mask
= context_gl
->c
.numbered_array_mask
;
5592 i
= wined3d_bit_scan(&mask
);
5593 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5597 static void wined3d_context_gl_load_numbered_arrays(struct wined3d_context_gl
*context_gl
,
5598 const struct wined3d_stream_info
*stream_info
, const struct wined3d_state
*state
)
5600 struct wined3d_context
*context
= &context_gl
->c
;
5601 const struct wined3d_shader
*vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
5602 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5603 GLuint current_bo
, bo
;
5606 /* Default to no instancing. */
5607 context
->instance_count
= 0;
5608 current_bo
= gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
] ? ~0u : 0;
5610 for (i
= 0; i
< MAX_ATTRIBS
; ++i
)
5612 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[i
];
5613 const void *offset
= get_vertex_attrib_pointer(element
, state
);
5614 const struct wined3d_stream_state
*stream
;
5615 const struct wined3d_format_gl
*format_gl
;
5617 if (!(stream_info
->use_map
& (1u << i
)))
5619 if (context
->numbered_array_mask
& (1u << i
))
5620 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5621 if (!use_vs(state
) && i
== WINED3D_FFP_DIFFUSE
)
5623 if (!(context_gl
->default_attrib_value_set
& (1u << i
)) || !context_gl
->diffuse_attrib_to_1
)
5625 GL_EXTCALL(glVertexAttrib4f(i
, 1.0f
, 1.0f
, 1.0f
, 1.0f
));
5626 context_gl
->diffuse_attrib_to_1
= 1;
5631 if (!(context_gl
->default_attrib_value_set
& (1u << i
)))
5633 GL_EXTCALL(glVertexAttrib4f(i
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
5634 if (i
== WINED3D_FFP_DIFFUSE
)
5635 context_gl
->diffuse_attrib_to_1
= 0;
5638 context_gl
->default_attrib_value_set
|= 1u << i
;
5642 format_gl
= wined3d_format_gl(element
->format
);
5643 stream
= &state
->streams
[element
->stream_idx
];
5644 stream
->buffer
->bo_user
.valid
= true;
5646 if ((stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && !context
->instance_count
)
5647 context
->instance_count
= state
->streams
[0].frequency
;
5649 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
5651 unsigned int divisor
= 0;
5653 if (element
->instanced
)
5654 divisor
= element
->divisor
? element
->divisor
: UINT_MAX
;
5655 GL_EXTCALL(glVertexAttribDivisor(i
, divisor
));
5657 else if (element
->divisor
)
5659 /* Unload instanced arrays, they will be loaded using immediate
5661 if (context
->numbered_array_mask
& (1u << i
))
5662 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5663 context_gl
->default_attrib_value_set
&= ~(1u << i
);
5667 TRACE("Loading array %u %s.\n", i
, debug_bo_address(&element
->data
));
5669 if (element
->stride
)
5671 DWORD format_flags
= format_gl
->f
.flags
[WINED3D_GL_RES_TYPE_BUFFER
];
5673 bo
= wined3d_bo_gl_id(element
->data
.buffer_object
);
5674 if (current_bo
!= bo
)
5676 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5677 checkGLcall("glBindBuffer");
5680 /* Use the VBO to find out if a vertex buffer exists, not the vb
5681 * pointer. vb can point to a user pointer data blob. In that case
5682 * current_bo will be 0. If there is a vertex buffer but no vbo we
5683 * won't be load converted attributes anyway. */
5684 if (vs
&& vs
->reg_maps
.shader_version
.major
>= 4 && (format_flags
& WINED3DFMT_FLAG_INTEGER
))
5686 GL_EXTCALL(glVertexAttribIPointer(i
, format_gl
->vtx_format
,
5687 format_gl
->vtx_type
, element
->stride
, offset
));
5691 GL_EXTCALL(glVertexAttribPointer(i
, format_gl
->vtx_format
, format_gl
->vtx_type
,
5692 !!(format_flags
& WINED3DFMT_FLAG_NORMALISED
), element
->stride
, offset
));
5695 if (!(context
->numbered_array_mask
& (1u << i
)))
5697 GL_EXTCALL(glEnableVertexAttribArray(i
));
5698 context
->numbered_array_mask
|= (1u << i
);
5703 /* Stride = 0 means always the same values.
5704 * glVertexAttribPointer() doesn't do that. Instead disable the
5705 * pointer and set up the attribute statically. But we have to
5706 * figure out the system memory address. */
5707 const BYTE
*ptr
= element
->data
.addr
;
5708 if (element
->data
.buffer_object
)
5709 ptr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(stream
->buffer
, context
);
5711 if (context
->numbered_array_mask
& (1u << i
))
5712 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5714 switch (format_gl
->f
.id
)
5716 case WINED3DFMT_R32_FLOAT
:
5717 GL_EXTCALL(glVertexAttrib1fv(i
, (const GLfloat
*)ptr
));
5719 case WINED3DFMT_R32G32_FLOAT
:
5720 GL_EXTCALL(glVertexAttrib2fv(i
, (const GLfloat
*)ptr
));
5722 case WINED3DFMT_R32G32B32_FLOAT
:
5723 GL_EXTCALL(glVertexAttrib3fv(i
, (const GLfloat
*)ptr
));
5725 case WINED3DFMT_R32G32B32A32_FLOAT
:
5726 GL_EXTCALL(glVertexAttrib4fv(i
, (const GLfloat
*)ptr
));
5728 case WINED3DFMT_R8G8B8A8_UINT
:
5729 GL_EXTCALL(glVertexAttrib4ubv(i
, ptr
));
5731 case WINED3DFMT_B8G8R8A8_UNORM
:
5732 if (gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
])
5734 const DWORD
*src
= (const DWORD
*)ptr
;
5735 DWORD c
= *src
& 0xff00ff00u
;
5736 c
|= (*src
& 0xff0000u
) >> 16;
5737 c
|= (*src
& 0xffu
) << 16;
5738 GL_EXTCALL(glVertexAttrib4Nubv(i
, (GLubyte
*)&c
));
5741 /* else fallthrough */
5742 case WINED3DFMT_R8G8B8A8_UNORM
:
5743 GL_EXTCALL(glVertexAttrib4Nubv(i
, ptr
));
5745 case WINED3DFMT_R16G16_SINT
:
5746 GL_EXTCALL(glVertexAttrib2sv(i
, (const GLshort
*)ptr
));
5748 case WINED3DFMT_R16G16B16A16_SINT
:
5749 GL_EXTCALL(glVertexAttrib4sv(i
, (const GLshort
*)ptr
));
5751 case WINED3DFMT_R16G16_SNORM
:
5753 const GLshort s
[4] = {((const GLshort
*)ptr
)[0], ((const GLshort
*)ptr
)[1], 0, 1};
5754 GL_EXTCALL(glVertexAttrib4Nsv(i
, s
));
5757 case WINED3DFMT_R16G16_UNORM
:
5759 const GLushort s
[4] = {((const GLushort
*)ptr
)[0], ((const GLushort
*)ptr
)[1], 0, 1};
5760 GL_EXTCALL(glVertexAttrib4Nusv(i
, s
));
5763 case WINED3DFMT_R16G16B16A16_SNORM
:
5764 GL_EXTCALL(glVertexAttrib4Nsv(i
, (const GLshort
*)ptr
));
5766 case WINED3DFMT_R16G16B16A16_UNORM
:
5767 GL_EXTCALL(glVertexAttrib4Nusv(i
, (const GLushort
*)ptr
));
5769 case WINED3DFMT_R10G10B10X2_UINT
:
5770 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5771 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5773 case WINED3DFMT_R10G10B10X2_SNORM
:
5774 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5775 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5777 case WINED3DFMT_R16G16_FLOAT
:
5778 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
5780 /* Not supported by GL_ARB_half_float_vertex. */
5781 GL_EXTCALL(glVertexAttrib2hvNV(i
, (const GLhalfNV
*)ptr
));
5785 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
5786 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
5787 GL_EXTCALL(glVertexAttrib2f(i
, x
, y
));
5790 case WINED3DFMT_R16G16B16A16_FLOAT
:
5791 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
5793 /* Not supported by GL_ARB_half_float_vertex. */
5794 GL_EXTCALL(glVertexAttrib4hvNV(i
, (const GLhalfNV
*)ptr
));
5798 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
5799 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
5800 float z
= float_16_to_32(((const unsigned short *)ptr
) + 2);
5801 float w
= float_16_to_32(((const unsigned short *)ptr
) + 3);
5802 GL_EXTCALL(glVertexAttrib4f(i
, x
, y
, z
, w
));
5806 ERR("Unexpected declaration in stride 0 attributes.\n");
5810 context_gl
->default_attrib_value_set
&= ~(1u << i
);
5813 checkGLcall("Loading numbered arrays");
5816 void wined3d_context_gl_update_stream_sources(struct wined3d_context_gl
*context_gl
,
5817 const struct wined3d_state
*state
)
5819 if (context_gl
->c
.use_immediate_mode_draw
)
5822 wined3d_context_gl_unload_vertex_data(context_gl
);
5823 if (context_gl
->c
.d3d_info
->ffp_generic_attributes
|| use_vs(state
))
5825 TRACE("Loading numbered arrays.\n");
5826 wined3d_context_gl_load_numbered_arrays(context_gl
, &context_gl
->c
.stream_info
, state
);
5830 TRACE("Loading named arrays.\n");
5831 wined3d_context_gl_unload_numbered_arrays(context_gl
);
5832 wined3d_context_gl_load_vertex_data(context_gl
, &context_gl
->c
.stream_info
, state
);
5833 context_gl
->c
.namedArraysLoaded
= TRUE
;
5836 static void apply_texture_blit_state(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*texture
,
5837 GLenum target
, unsigned int level
, enum wined3d_texture_filter_type filter
)
5839 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(filter
));
5840 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
5841 wined3d_gl_min_mip_filter(filter
, WINED3D_TEXF_NONE
));
5842 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
5843 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
5844 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
5845 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
);
5846 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, level
);
5848 /* We changed the filtering settings on the texture. Make sure they get
5849 * reset on subsequent draws. */
5850 texture
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
5851 texture
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
5852 texture
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
5853 texture
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
5854 texture
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
5855 texture
->sampler_desc
.srgb_decode
= FALSE
;
5856 texture
->base_level
= level
;
5859 /* Context activation is done by the caller. */
5860 void wined3d_context_gl_draw_shaded_quad(struct wined3d_context_gl
*context_gl
, struct wined3d_texture_gl
*texture_gl
,
5861 unsigned int sub_resource_idx
, const RECT
*src_rect
, const RECT
*dst_rect
,
5862 enum wined3d_texture_filter_type filter
)
5864 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5865 struct wined3d_blt_info info
;
5866 unsigned int level
, w
, h
, i
;
5871 struct wined3d_vec3 texcoord
;
5875 texture2d_get_blt_info(texture_gl
, sub_resource_idx
, src_rect
, &info
);
5877 level
= sub_resource_idx
% texture_gl
->t
.level_count
;
5878 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, texture_gl
->texture_rgb
.name
);
5879 apply_texture_blit_state(gl_info
, &texture_gl
->texture_rgb
, info
.bind_target
, level
, filter
);
5880 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, level
);
5882 wined3d_context_gl_get_rt_size(context_gl
, &dst_size
);
5886 quad
[0].x
= dst_rect
->left
* 2.0f
/ w
- 1.0f
;
5887 quad
[0].y
= dst_rect
->top
* 2.0f
/ h
- 1.0f
;
5888 quad
[0].texcoord
= info
.texcoords
[0];
5890 quad
[1].x
= dst_rect
->right
* 2.0f
/ w
- 1.0f
;
5891 quad
[1].y
= dst_rect
->top
* 2.0f
/ h
- 1.0f
;
5892 quad
[1].texcoord
= info
.texcoords
[1];
5894 quad
[2].x
= dst_rect
->left
* 2.0f
/ w
- 1.0f
;
5895 quad
[2].y
= dst_rect
->bottom
* 2.0f
/ h
- 1.0f
;
5896 quad
[2].texcoord
= info
.texcoords
[2];
5898 quad
[3].x
= dst_rect
->right
* 2.0f
/ w
- 1.0f
;
5899 quad
[3].y
= dst_rect
->bottom
* 2.0f
/ h
- 1.0f
;
5900 quad
[3].texcoord
= info
.texcoords
[3];
5903 if (gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
5905 if (!context_gl
->blit_vbo
)
5906 GL_EXTCALL(glGenBuffers(1, &context_gl
->blit_vbo
));
5907 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, context_gl
->blit_vbo
));
5909 wined3d_context_gl_unload_vertex_data(context_gl
);
5910 wined3d_context_gl_unload_numbered_arrays(context_gl
);
5912 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER
, sizeof(quad
), quad
, GL_STREAM_DRAW
));
5913 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT
, FALSE
, sizeof(*quad
), NULL
));
5914 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT
, FALSE
, sizeof(*quad
),
5915 (void *)FIELD_OFFSET(struct blit_vertex
, texcoord
)));
5917 GL_EXTCALL(glEnableVertexAttribArray(0));
5918 GL_EXTCALL(glEnableVertexAttribArray(1));
5920 gl_info
->gl_ops
.gl
.p_glDrawArrays(GL_TRIANGLE_STRIP
, 0, 4);
5922 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, 0));
5923 GL_EXTCALL(glDisableVertexAttribArray(1));
5924 GL_EXTCALL(glDisableVertexAttribArray(0));
5928 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
5930 for (i
= 0; i
< ARRAY_SIZE(quad
); ++i
)
5932 GL_EXTCALL(glVertexAttrib3fv(1, &quad
[i
].texcoord
.x
));
5933 GL_EXTCALL(glVertexAttrib2fv(0, &quad
[i
].x
));
5936 gl_info
->gl_ops
.gl
.p_glEnd();
5938 checkGLcall("draw");
5940 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, texture_gl
->t
.level_count
- 1);
5941 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, 0);
5944 /* Context activation is done by the caller. */
5945 void wined3d_context_gl_draw_textured_quad(struct wined3d_context_gl
*context_gl
,
5946 struct wined3d_texture_gl
*texture_gl
, unsigned int sub_resource_idx
,
5947 const RECT
*src_rect
, const RECT
*dst_rect
, enum wined3d_texture_filter_type filter
)
5949 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5950 struct wined3d_blt_info info
;
5953 texture2d_get_blt_info(texture_gl
, sub_resource_idx
, src_rect
, &info
);
5955 gl_info
->gl_ops
.gl
.p_glEnable(info
.bind_target
);
5956 checkGLcall("glEnable(bind_target)");
5958 level
= sub_resource_idx
% texture_gl
->t
.level_count
;
5959 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, texture_gl
->texture_rgb
.name
);
5960 apply_texture_blit_state(gl_info
, &texture_gl
->texture_rgb
, info
.bind_target
, level
, filter
);
5961 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, level
);
5962 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
5963 checkGLcall("glTexEnvi");
5966 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
5967 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[0].x
);
5968 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->top
);
5970 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[1].x
);
5971 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->top
);
5973 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[2].x
);
5974 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->bottom
);
5976 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[3].x
);
5977 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->bottom
);
5978 gl_info
->gl_ops
.gl
.p_glEnd();
5980 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, texture_gl
->t
.level_count
- 1);
5981 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, 0);