2 * Context and render target management in wined3d
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006, 2008 Henri Verbeet
9 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
10 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wined3d_private.h"
28 #include "wined3d_gl.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
31 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
32 WINE_DECLARE_DEBUG_CHANNEL(d3d_sync
);
34 #define WINED3D_MAX_FBO_ENTRIES 64
35 #define WINED3D_ALL_LAYERS (~0u)
37 static DWORD wined3d_context_tls_idx
;
39 /* Note that except for WINED3DPT_POINTLIST and WINED3DPT_LINELIST these
40 * actually have the same values in GL and D3D. */
41 static GLenum
gl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
43 switch (primitive_type
)
45 case WINED3D_PT_POINTLIST
:
48 case WINED3D_PT_LINELIST
:
51 case WINED3D_PT_LINESTRIP
:
54 case WINED3D_PT_TRIANGLELIST
:
57 case WINED3D_PT_TRIANGLESTRIP
:
58 return GL_TRIANGLE_STRIP
;
60 case WINED3D_PT_TRIANGLEFAN
:
61 return GL_TRIANGLE_FAN
;
63 case WINED3D_PT_LINELIST_ADJ
:
64 return GL_LINES_ADJACENCY_ARB
;
66 case WINED3D_PT_LINESTRIP_ADJ
:
67 return GL_LINE_STRIP_ADJACENCY_ARB
;
69 case WINED3D_PT_TRIANGLELIST_ADJ
:
70 return GL_TRIANGLES_ADJACENCY_ARB
;
72 case WINED3D_PT_TRIANGLESTRIP_ADJ
:
73 return GL_TRIANGLE_STRIP_ADJACENCY_ARB
;
75 case WINED3D_PT_PATCH
:
79 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
80 case WINED3D_PT_UNDEFINED
:
85 /* FBO helper functions */
87 /* Context activation is done by the caller. */
88 static void wined3d_context_gl_bind_fbo(struct wined3d_context_gl
*context_gl
, GLenum target
, GLuint fbo
)
90 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
92 TRACE("context_gl %p, target %#x, fbo %u.\n", context_gl
, target
, fbo
);
96 case GL_READ_FRAMEBUFFER
:
97 if (context_gl
->fbo_read_binding
== fbo
)
99 context_gl
->fbo_read_binding
= fbo
;
102 case GL_DRAW_FRAMEBUFFER
:
103 if (context_gl
->fbo_draw_binding
== fbo
)
105 context_gl
->fbo_draw_binding
= fbo
;
109 if (context_gl
->fbo_read_binding
== fbo
110 && context_gl
->fbo_draw_binding
== fbo
)
112 context_gl
->fbo_read_binding
= fbo
;
113 context_gl
->fbo_draw_binding
= fbo
;
117 FIXME("Unhandled target %#x.\n", target
);
121 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
122 checkGLcall("glBindFramebuffer()");
125 /* Context activation is done by the caller. */
126 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
130 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
132 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
133 checkGLcall("glFramebufferTexture2D()");
135 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
136 checkGLcall("glFramebufferTexture2D()");
138 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
139 checkGLcall("glFramebufferTexture2D()");
142 /* Context activation is done by the caller. */
143 static void wined3d_context_gl_destroy_fbo(struct wined3d_context_gl
*context_gl
, GLuint fbo
)
145 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
147 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, fbo
);
148 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
149 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
151 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
152 checkGLcall("glDeleteFramebuffers()");
155 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
156 GLenum fbo_target
, uint32_t flags
, GLuint rb
)
158 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
160 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
161 checkGLcall("glFramebufferRenderbuffer()");
164 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
166 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
167 checkGLcall("glFramebufferRenderbuffer()");
171 static void wined3d_context_gl_attach_gl_texture_fbo(struct wined3d_context_gl
*context_gl
,
172 GLenum fbo_target
, GLenum attachment
, const struct wined3d_fbo_resource
*resource
)
174 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
178 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
, GL_TEXTURE_2D
, 0, 0);
180 else if (resource
->layer
== WINED3D_ALL_LAYERS
)
182 if (!gl_info
->fbo_ops
.glFramebufferTexture
)
184 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
188 gl_info
->fbo_ops
.glFramebufferTexture(fbo_target
, attachment
,
189 resource
->object
, resource
->level
);
191 else if (resource
->target
== GL_TEXTURE_1D_ARRAY
|| resource
->target
== GL_TEXTURE_2D_ARRAY
192 || resource
->target
== GL_TEXTURE_3D
)
194 if (!gl_info
->fbo_ops
.glFramebufferTextureLayer
)
196 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
200 gl_info
->fbo_ops
.glFramebufferTextureLayer(fbo_target
, attachment
,
201 resource
->object
, resource
->level
, resource
->layer
);
203 else if (resource
->target
== GL_TEXTURE_1D
)
205 gl_info
->fbo_ops
.glFramebufferTexture1D(fbo_target
, attachment
,
206 resource
->target
, resource
->object
, resource
->level
);
210 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
,
211 resource
->target
, resource
->object
, resource
->level
);
213 checkGLcall("attach texture to fbo");
216 /* Context activation is done by the caller. */
217 static void wined3d_context_gl_attach_depth_stencil_fbo(struct wined3d_context_gl
*context_gl
,
218 GLenum fbo_target
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
,
221 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
223 if (resource
->object
)
225 TRACE("Attach depth stencil %u.\n", resource
->object
);
229 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
230 flags
, resource
->object
);
234 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
235 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_DEPTH_ATTACHMENT
, resource
);
237 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
238 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_STENCIL_ATTACHMENT
, resource
);
241 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
))
242 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
244 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
))
245 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
249 TRACE("Attach depth stencil 0.\n");
251 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
252 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
256 /* Context activation is done by the caller. */
257 static void wined3d_context_gl_attach_surface_fbo(struct wined3d_context_gl
*context_gl
,
258 GLenum fbo_target
, unsigned int idx
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
)
260 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
262 TRACE("Attach GL object %u to %u.\n", resource
->object
, idx
);
264 if (resource
->object
)
268 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
269 GL_RENDERBUFFER
, resource
->object
);
270 checkGLcall("glFramebufferRenderbuffer()");
274 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, resource
);
279 wined3d_context_gl_attach_gl_texture_fbo(context_gl
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, NULL
);
283 static void context_dump_fbo_attachment(const struct wined3d_gl_info
*gl_info
, GLenum target
,
291 enum wined3d_gl_extension extension
;
295 {GL_TEXTURE_1D
, GL_TEXTURE_BINDING_1D
, "1d", WINED3D_GL_EXT_NONE
},
296 {GL_TEXTURE_1D_ARRAY
, GL_TEXTURE_BINDING_1D_ARRAY
, "1d-array", EXT_TEXTURE_ARRAY
},
297 {GL_TEXTURE_2D
, GL_TEXTURE_BINDING_2D
, "2d", WINED3D_GL_EXT_NONE
},
298 {GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_BINDING_RECTANGLE_ARB
, "rectangle", ARB_TEXTURE_RECTANGLE
},
299 {GL_TEXTURE_2D_ARRAY
, GL_TEXTURE_BINDING_2D_ARRAY
, "2d-array" , EXT_TEXTURE_ARRAY
},
300 {GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_BINDING_CUBE_MAP
, "cube", ARB_TEXTURE_CUBE_MAP
},
301 {GL_TEXTURE_2D_MULTISAMPLE
, GL_TEXTURE_BINDING_2D_MULTISAMPLE
, "2d-ms", ARB_TEXTURE_MULTISAMPLE
},
302 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY
, "2d-array-ms", ARB_TEXTURE_MULTISAMPLE
},
305 GLint type
, name
, samples
, width
, height
, old_texture
, level
, face
, fmt
, tex_target
;
306 const char *tex_type_str
= NULL
;
309 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
310 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
, &name
);
311 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
312 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
, &type
);
314 if (type
== GL_RENDERBUFFER
)
316 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, name
);
317 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_WIDTH
, &width
);
318 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_HEIGHT
, &height
);
319 if (gl_info
->limits
.samples
> 1)
320 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_SAMPLES
, &samples
);
323 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_INTERNAL_FORMAT
, &fmt
);
324 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
325 debug_fboattachment(attachment
), name
, width
, height
, samples
, fmt
);
327 else if (type
== GL_TEXTURE
)
329 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
330 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
, &level
);
331 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
332 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
, &face
);
334 if (gl_info
->gl_ops
.ext
.p_glGetTextureParameteriv
)
336 GL_EXTCALL(glGetTextureParameteriv(name
, GL_TEXTURE_TARGET
, &tex_target
));
338 for (i
= 0; i
< ARRAY_SIZE(texture_type
); ++i
)
340 if (texture_type
[i
].target
== tex_target
)
342 tex_type_str
= texture_type
[i
].str
;
346 if (i
== ARRAY_SIZE(texture_type
))
347 tex_type_str
= wine_dbg_sprintf("%#x", tex_target
);
351 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP
, &old_texture
);
352 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, name
);
354 tex_target
= GL_TEXTURE_CUBE_MAP
;
355 tex_type_str
= "cube";
359 for (i
= 0; i
< ARRAY_SIZE(texture_type
); ++i
)
361 if (!gl_info
->supported
[texture_type
[i
].extension
])
364 gl_info
->gl_ops
.gl
.p_glGetIntegerv(texture_type
[i
].binding
, &old_texture
);
365 while (gl_info
->gl_ops
.gl
.p_glGetError());
367 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, name
);
368 if (!gl_info
->gl_ops
.gl
.p_glGetError())
370 tex_target
= texture_type
[i
].target
;
371 tex_type_str
= texture_type
[i
].str
;
374 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, old_texture
);
379 FIXME("Cannot find type of texture %d.\n", name
);
384 if (gl_info
->gl_ops
.ext
.p_glGetTextureParameteriv
)
386 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
));
387 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_WIDTH
, &width
));
388 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_HEIGHT
, &height
));
389 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_SAMPLES
, &samples
));
393 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
394 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_WIDTH
, &width
);
395 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_HEIGHT
, &height
);
396 if (gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
397 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_SAMPLES
, &samples
);
401 gl_info
->gl_ops
.gl
.p_glBindTexture(tex_target
, old_texture
);
404 FIXME(" %s: %s texture %d, %dx%d, %d samples, format %#x.\n",
405 debug_fboattachment(attachment
), tex_type_str
, name
, width
, height
, samples
, fmt
);
407 else if (type
== GL_NONE
)
409 FIXME(" %s: NONE.\n", debug_fboattachment(attachment
));
413 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment
), type
);
416 checkGLcall("dump FBO attachment");
419 /* Context activation is done by the caller. */
420 void wined3d_context_gl_check_fbo_status(const struct wined3d_context_gl
*context_gl
, GLenum target
)
422 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
428 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
429 if (status
== GL_FRAMEBUFFER_COMPLETE
)
431 TRACE("FBO complete.\n");
437 FIXME("FBO status %s (%#x).\n", debug_fbostatus(status
), status
);
439 if (!context_gl
->current_fbo
)
441 ERR("FBO 0 is incomplete, driver bug?\n");
445 context_dump_fbo_attachment(gl_info
, target
, GL_DEPTH_ATTACHMENT
);
446 context_dump_fbo_attachment(gl_info
, target
, GL_STENCIL_ATTACHMENT
);
448 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
449 context_dump_fbo_attachment(gl_info
, target
, GL_COLOR_ATTACHMENT0
+ i
);
453 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
455 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
456 return buffer
? (1u << 31) | buffer
: 0;
459 static inline DWORD
context_generate_rt_mask_from_resource(struct wined3d_resource
*resource
)
461 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
463 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
467 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource
));
470 /* Context activation is done by the caller. */
471 static void wined3d_context_gl_set_draw_buffer(struct wined3d_context_gl
*context_gl
, GLenum buffer
)
473 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
474 struct fbo_entry
*current_fbo
= context_gl
->current_fbo
;
475 uint32_t new_mask
= context_generate_rt_mask(buffer
);
476 uint32_t *current_mask
;
478 current_mask
= current_fbo
? ¤t_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
479 if (new_mask
== *current_mask
)
482 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
483 checkGLcall("glDrawBuffer()");
485 *current_mask
= new_mask
;
488 static inline void wined3d_context_gl_set_fbo_key_for_render_target(const struct wined3d_context_gl
*context_gl
,
489 struct wined3d_fbo_entry_key
*key
, unsigned int idx
, const struct wined3d_rendertarget_info
*render_target
,
492 unsigned int sub_resource_idx
= render_target
->sub_resource_idx
;
493 struct wined3d_resource
*resource
= render_target
->resource
;
494 struct wined3d_texture_gl
*texture_gl
;
496 if (!resource
|| resource
->format
->id
== WINED3DFMT_NULL
|| resource
->type
== WINED3D_RTYPE_BUFFER
)
498 if (resource
&& resource
->type
== WINED3D_RTYPE_BUFFER
)
499 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
500 key
->objects
[idx
].object
= 0;
501 key
->objects
[idx
].target
= 0;
502 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
506 if (render_target
->gl_view
.name
)
508 key
->objects
[idx
].object
= render_target
->gl_view
.name
;
509 key
->objects
[idx
].target
= render_target
->gl_view
.target
;
510 key
->objects
[idx
].level
= 0;
511 key
->objects
[idx
].layer
= (render_target
->layer_count
== 1 ? 0 : WINED3D_ALL_LAYERS
);
515 texture_gl
= wined3d_texture_gl(wined3d_texture_from_resource(resource
));
516 if (texture_gl
->current_renderbuffer
)
518 key
->objects
[idx
].object
= texture_gl
->current_renderbuffer
->id
;
519 key
->objects
[idx
].target
= 0;
520 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
521 key
->rb_namespace
|= 1 << idx
;
525 key
->objects
[idx
].target
= wined3d_texture_gl_get_sub_resource_target(texture_gl
, sub_resource_idx
);
526 key
->objects
[idx
].level
= sub_resource_idx
% texture_gl
->t
.level_count
;
527 key
->objects
[idx
].layer
= sub_resource_idx
/ texture_gl
->t
.level_count
;
529 if (render_target
->layer_count
!= 1)
530 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
534 case WINED3D_LOCATION_TEXTURE_RGB
:
535 key
->objects
[idx
].object
= wined3d_texture_gl_get_texture_name(texture_gl
, &context_gl
->c
, FALSE
);
538 case WINED3D_LOCATION_TEXTURE_SRGB
:
539 key
->objects
[idx
].object
= wined3d_texture_gl_get_texture_name(texture_gl
, &context_gl
->c
, TRUE
);
542 case WINED3D_LOCATION_RB_MULTISAMPLE
:
543 key
->objects
[idx
].object
= texture_gl
->rb_multisample
;
544 key
->objects
[idx
].target
= 0;
545 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
546 key
->rb_namespace
|= 1 << idx
;
549 case WINED3D_LOCATION_RB_RESOLVED
:
550 key
->objects
[idx
].object
= texture_gl
->rb_resolved
;
551 key
->objects
[idx
].target
= 0;
552 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
553 key
->rb_namespace
|= 1 << idx
;
558 static void wined3d_context_gl_generate_fbo_key(const struct wined3d_context_gl
*context_gl
,
559 struct wined3d_fbo_entry_key
*key
, const struct wined3d_rendertarget_info
*render_targets
,
560 const struct wined3d_rendertarget_info
*depth_stencil
, DWORD color_location
, DWORD ds_location
)
562 unsigned int buffers
= context_gl
->gl_info
->limits
.buffers
;
565 key
->rb_namespace
= 0;
566 wined3d_context_gl_set_fbo_key_for_render_target(context_gl
, key
, 0, depth_stencil
, ds_location
);
568 for (i
= 0; i
< buffers
; ++i
)
569 wined3d_context_gl_set_fbo_key_for_render_target(context_gl
, key
, i
+ 1, &render_targets
[i
], color_location
);
571 memset(&key
->objects
[buffers
+ 1], 0, (ARRAY_SIZE(key
->objects
) - buffers
- 1) * sizeof(*key
->objects
));
574 static struct fbo_entry
*wined3d_context_gl_create_fbo_entry(const struct wined3d_context_gl
*context_gl
,
575 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
576 DWORD color_location
, DWORD ds_location
)
578 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
579 struct fbo_entry
*entry
;
581 entry
= heap_alloc(sizeof(*entry
));
582 wined3d_context_gl_generate_fbo_key(context_gl
, &entry
->key
,
583 render_targets
, depth_stencil
, color_location
, ds_location
);
585 if (depth_stencil
->resource
)
587 if (depth_stencil
->resource
->format
->depth_size
)
588 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
589 if (depth_stencil
->resource
->format
->stencil_size
)
590 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
592 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
593 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
594 checkGLcall("glGenFramebuffers()");
595 TRACE("Created FBO %u.\n", entry
->id
);
600 /* Context activation is done by the caller. */
601 static void wined3d_context_gl_reuse_fbo_entry(struct wined3d_context_gl
*context_gl
, GLenum target
,
602 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
603 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
605 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
607 wined3d_context_gl_bind_fbo(context_gl
, target
, entry
->id
);
608 context_clean_fbo_attachments(gl_info
, target
);
610 wined3d_context_gl_generate_fbo_key(context_gl
, &entry
->key
,
611 render_targets
, depth_stencil
, color_location
, ds_location
);
613 if (depth_stencil
->resource
)
615 if (depth_stencil
->resource
->format
->depth_size
)
616 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
617 if (depth_stencil
->resource
->format
->stencil_size
)
618 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
622 /* Context activation is done by the caller. */
623 static void wined3d_context_gl_destroy_fbo_entry(struct wined3d_context_gl
*context_gl
, struct fbo_entry
*entry
)
627 TRACE("Destroy FBO %u.\n", entry
->id
);
628 wined3d_context_gl_destroy_fbo(context_gl
, entry
->id
);
630 --context_gl
->fbo_entry_count
;
631 list_remove(&entry
->entry
);
635 /* Context activation is done by the caller. */
636 static struct fbo_entry
*wined3d_context_gl_find_fbo_entry(struct wined3d_context_gl
*context_gl
, GLenum target
,
637 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
638 DWORD color_location
, DWORD ds_location
)
640 static const struct wined3d_rendertarget_info ds_null
= {{0}};
641 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
642 struct wined3d_texture
*rt_texture
, *ds_texture
;
643 struct wined3d_fbo_entry_key fbo_key
;
644 unsigned int i
, ds_level
, rt_level
;
645 struct fbo_entry
*entry
;
647 if (depth_stencil
->resource
&& depth_stencil
->resource
->type
!= WINED3D_RTYPE_BUFFER
648 && render_targets
[0].resource
&& render_targets
[0].resource
->type
!= WINED3D_RTYPE_BUFFER
649 && render_targets
[0].resource
->format
->id
!= WINED3DFMT_NULL
)
651 rt_texture
= wined3d_texture_from_resource(render_targets
[0].resource
);
652 rt_level
= render_targets
[0].sub_resource_idx
% rt_texture
->level_count
;
653 ds_texture
= wined3d_texture_from_resource(depth_stencil
->resource
);
654 ds_level
= depth_stencil
->sub_resource_idx
% ds_texture
->level_count
;
656 if (wined3d_texture_get_level_width(ds_texture
, ds_level
)
657 < wined3d_texture_get_level_width(rt_texture
, rt_level
)
658 || wined3d_texture_get_level_height(ds_texture
, ds_level
)
659 < wined3d_texture_get_level_height(rt_texture
, rt_level
))
661 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
662 depth_stencil
= &ds_null
;
664 else if (ds_texture
->resource
.multisample_type
!= rt_texture
->resource
.multisample_type
665 || (ds_texture
->resource
.multisample_type
666 && ds_texture
->resource
.multisample_quality
!= rt_texture
->resource
.multisample_quality
))
668 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
669 rt_texture
->resource
.multisample_type
, rt_texture
->resource
.multisample_quality
,
670 ds_texture
->resource
.multisample_type
, ds_texture
->resource
.multisample_quality
);
671 depth_stencil
= &ds_null
;
673 else if (depth_stencil
->resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
675 wined3d_texture_gl_set_compatible_renderbuffer(wined3d_texture_gl(ds_texture
),
676 context_gl
, ds_level
, &render_targets
[0]);
680 wined3d_context_gl_generate_fbo_key(context_gl
, &fbo_key
,
681 render_targets
, depth_stencil
, color_location
, ds_location
);
685 struct wined3d_resource
*resource
;
686 unsigned int width
, height
;
687 const char *resource_type
;
689 TRACE("Dumping FBO attachments:\n");
690 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
692 if ((resource
= render_targets
[i
].resource
))
694 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
696 width
= resource
->size
;
698 resource_type
= "buffer";
702 rt_texture
= wined3d_texture_from_resource(resource
);
703 rt_level
= render_targets
[i
].sub_resource_idx
% rt_texture
->level_count
;
704 width
= wined3d_texture_get_level_pow2_width(rt_texture
, rt_level
);
705 height
= wined3d_texture_get_level_pow2_height(rt_texture
, rt_level
);
706 resource_type
= "texture";
709 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
710 i
, resource
, render_targets
[i
].sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
711 fbo_key
.rb_namespace
& (1 << (i
+ 1)) ? "renderbuffer" : resource_type
,
712 fbo_key
.objects
[i
+ 1].object
, width
, height
, resource
->multisample_type
);
715 if ((resource
= depth_stencil
->resource
))
717 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
719 width
= resource
->size
;
721 resource_type
= "buffer";
725 ds_texture
= wined3d_texture_from_resource(resource
);
726 ds_level
= depth_stencil
->sub_resource_idx
% ds_texture
->level_count
;
727 width
= wined3d_texture_get_level_pow2_width(ds_texture
, ds_level
);
728 height
= wined3d_texture_get_level_pow2_height(ds_texture
, ds_level
);
729 resource_type
= "texture";
732 TRACE(" Depth attachment: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
733 resource
, depth_stencil
->sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
734 fbo_key
.rb_namespace
& (1 << 0) ? "renderbuffer" : resource_type
,
735 fbo_key
.objects
[0].object
, width
, height
, resource
->multisample_type
);
739 LIST_FOR_EACH_ENTRY(entry
, &context_gl
->fbo_list
, struct fbo_entry
, entry
)
741 if (memcmp(&fbo_key
, &entry
->key
, sizeof(fbo_key
)))
744 list_remove(&entry
->entry
);
745 list_add_head(&context_gl
->fbo_list
, &entry
->entry
);
749 if (context_gl
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
751 entry
= wined3d_context_gl_create_fbo_entry(context_gl
,
752 render_targets
, depth_stencil
, color_location
, ds_location
);
753 list_add_head(&context_gl
->fbo_list
, &entry
->entry
);
754 ++context_gl
->fbo_entry_count
;
758 entry
= LIST_ENTRY(list_tail(&context_gl
->fbo_list
), struct fbo_entry
, entry
);
759 wined3d_context_gl_reuse_fbo_entry(context_gl
, target
, render_targets
,
760 depth_stencil
, color_location
, ds_location
, entry
);
761 list_remove(&entry
->entry
);
762 list_add_head(&context_gl
->fbo_list
, &entry
->entry
);
768 /* Context activation is done by the caller. */
769 static void wined3d_context_gl_apply_fbo_entry(struct wined3d_context_gl
*context_gl
,
770 GLenum target
, struct fbo_entry
*entry
)
772 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
773 GLuint read_binding
, draw_binding
;
776 if (entry
->flags
& WINED3D_FBO_ENTRY_FLAG_ATTACHED
)
778 wined3d_context_gl_bind_fbo(context_gl
, target
, entry
->id
);
782 read_binding
= context_gl
->fbo_read_binding
;
783 draw_binding
= context_gl
->fbo_draw_binding
;
784 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, entry
->id
);
786 if (gl_info
->supported
[ARB_FRAMEBUFFER_NO_ATTACHMENTS
])
788 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
,
789 GL_FRAMEBUFFER_DEFAULT_WIDTH
, gl_info
->limits
.framebuffer_width
));
790 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
,
791 GL_FRAMEBUFFER_DEFAULT_HEIGHT
, gl_info
->limits
.framebuffer_height
));
792 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
, GL_FRAMEBUFFER_DEFAULT_LAYERS
, 1));
793 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
, GL_FRAMEBUFFER_DEFAULT_SAMPLES
, 1));
796 /* Apply render targets */
797 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
799 wined3d_context_gl_attach_surface_fbo(context_gl
, target
, i
,
800 &entry
->key
.objects
[i
+ 1], entry
->key
.rb_namespace
& (1 << (i
+ 1)));
803 wined3d_context_gl_attach_depth_stencil_fbo(context_gl
, target
,
804 &entry
->key
.objects
[0], entry
->key
.rb_namespace
& 0x1, entry
->flags
);
806 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
807 * GL contexts requirements. */
808 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
809 wined3d_context_gl_set_draw_buffer(context_gl
, GL_NONE
);
810 if (target
!= GL_FRAMEBUFFER
)
812 if (target
== GL_READ_FRAMEBUFFER
)
813 wined3d_context_gl_bind_fbo(context_gl
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
815 wined3d_context_gl_bind_fbo(context_gl
, GL_READ_FRAMEBUFFER
, read_binding
);
818 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_ATTACHED
;
821 /* Context activation is done by the caller. */
822 static void wined3d_context_gl_apply_fbo_state(struct wined3d_context_gl
*context_gl
, GLenum target
,
823 const struct wined3d_rendertarget_info
*render_targets
,
824 const struct wined3d_rendertarget_info
*depth_stencil
, DWORD color_location
, DWORD ds_location
)
826 struct fbo_entry
*entry
, *entry2
;
828 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_destroy_list
, struct fbo_entry
, entry
)
830 wined3d_context_gl_destroy_fbo_entry(context_gl
, entry
);
833 if (context_gl
->rebind_fbo
)
835 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
836 context_gl
->rebind_fbo
= FALSE
;
839 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
841 context_gl
->current_fbo
= NULL
;
842 wined3d_context_gl_bind_fbo(context_gl
, target
, 0);
846 context_gl
->current_fbo
= wined3d_context_gl_find_fbo_entry(context_gl
,
847 target
, render_targets
, depth_stencil
, color_location
, ds_location
);
848 wined3d_context_gl_apply_fbo_entry(context_gl
, target
, context_gl
->current_fbo
);
852 /* Context activation is done by the caller. */
853 void wined3d_context_gl_apply_fbo_state_explicit(struct wined3d_context_gl
*context_gl
, GLenum target
,
854 struct wined3d_resource
*rt
, unsigned int rt_sub_resource_idx
,
855 struct wined3d_resource
*ds
, unsigned int ds_sub_resource_idx
, uint32_t location
)
857 struct wined3d_rendertarget_info ds_info
= {{0}};
859 memset(context_gl
->blit_targets
, 0, sizeof(context_gl
->blit_targets
));
862 context_gl
->blit_targets
[0].resource
= rt
;
863 context_gl
->blit_targets
[0].sub_resource_idx
= rt_sub_resource_idx
;
864 context_gl
->blit_targets
[0].layer_count
= 1;
869 ds_info
.resource
= ds
;
870 ds_info
.sub_resource_idx
= ds_sub_resource_idx
;
871 ds_info
.layer_count
= 1;
874 wined3d_context_gl_apply_fbo_state(context_gl
, target
, context_gl
->blit_targets
, &ds_info
, location
, location
);
877 /* Context activation is done by the caller. */
878 void wined3d_context_gl_alloc_occlusion_query(struct wined3d_context_gl
*context_gl
,
879 struct wined3d_occlusion_query
*query
)
881 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
883 if (context_gl
->free_occlusion_query_count
)
885 query
->id
= context_gl
->free_occlusion_queries
[--context_gl
->free_occlusion_query_count
];
889 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
891 GL_EXTCALL(glGenQueries(1, &query
->id
));
892 checkGLcall("glGenQueries");
894 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context_gl
);
898 WARN("Occlusion queries not supported, not allocating query id.\n");
903 query
->context_gl
= context_gl
;
904 list_add_head(&context_gl
->occlusion_queries
, &query
->entry
);
907 void wined3d_context_gl_free_occlusion_query(struct wined3d_occlusion_query
*query
)
909 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
911 list_remove(&query
->entry
);
912 query
->context_gl
= NULL
;
914 if (!wined3d_array_reserve((void **)&context_gl
->free_occlusion_queries
,
915 &context_gl
->free_occlusion_query_size
, context_gl
->free_occlusion_query_count
+ 1,
916 sizeof(*context_gl
->free_occlusion_queries
)))
918 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context_gl
);
922 context_gl
->free_occlusion_queries
[context_gl
->free_occlusion_query_count
++] = query
->id
;
925 /* Context activation is done by the caller. */
926 void wined3d_context_gl_alloc_fence(struct wined3d_context_gl
*context_gl
, struct wined3d_fence
*fence
)
928 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
930 if (context_gl
->free_fence_count
)
932 fence
->object
= context_gl
->free_fences
[--context_gl
->free_fence_count
];
936 if (gl_info
->supported
[ARB_SYNC
])
938 /* Using ARB_sync, not much to do here. */
939 fence
->object
.sync
= NULL
;
940 TRACE("Allocated sync object in context %p.\n", context_gl
);
942 else if (gl_info
->supported
[APPLE_FENCE
])
944 GL_EXTCALL(glGenFencesAPPLE(1, &fence
->object
.id
));
945 checkGLcall("glGenFencesAPPLE");
947 TRACE("Allocated fence %u in context %p.\n", fence
->object
.id
, context_gl
);
949 else if(gl_info
->supported
[NV_FENCE
])
951 GL_EXTCALL(glGenFencesNV(1, &fence
->object
.id
));
952 checkGLcall("glGenFencesNV");
954 TRACE("Allocated fence %u in context %p.\n", fence
->object
.id
, context_gl
);
958 WARN("Fences not supported, not allocating fence.\n");
959 fence
->object
.id
= 0;
963 fence
->context_gl
= context_gl
;
964 list_add_head(&context_gl
->fences
, &fence
->entry
);
967 void wined3d_context_gl_free_fence(struct wined3d_fence
*fence
)
969 struct wined3d_context_gl
*context_gl
= fence
->context_gl
;
971 list_remove(&fence
->entry
);
972 fence
->context_gl
= NULL
;
974 if (!wined3d_array_reserve((void **)&context_gl
->free_fences
,
975 &context_gl
->free_fence_size
, context_gl
->free_fence_count
+ 1,
976 sizeof(*context_gl
->free_fences
)))
978 ERR("Failed to grow free list, leaking fence %u in context %p.\n", fence
->object
.id
, context_gl
);
982 context_gl
->free_fences
[context_gl
->free_fence_count
++] = fence
->object
;
985 /* Context activation is done by the caller. */
986 void wined3d_context_gl_alloc_timestamp_query(struct wined3d_context_gl
*context_gl
,
987 struct wined3d_timestamp_query
*query
)
989 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
991 if (context_gl
->free_timestamp_query_count
)
993 query
->id
= context_gl
->free_timestamp_queries
[--context_gl
->free_timestamp_query_count
];
997 GL_EXTCALL(glGenQueries(1, &query
->id
));
998 checkGLcall("glGenQueries");
1000 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context_gl
);
1003 query
->context_gl
= context_gl
;
1004 list_add_head(&context_gl
->timestamp_queries
, &query
->entry
);
1007 void wined3d_context_gl_free_timestamp_query(struct wined3d_timestamp_query
*query
)
1009 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
1011 list_remove(&query
->entry
);
1012 query
->context_gl
= NULL
;
1014 if (!wined3d_array_reserve((void **)&context_gl
->free_timestamp_queries
,
1015 &context_gl
->free_timestamp_query_size
, context_gl
->free_timestamp_query_count
+ 1,
1016 sizeof(*context_gl
->free_timestamp_queries
)))
1018 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context_gl
);
1022 context_gl
->free_timestamp_queries
[context_gl
->free_timestamp_query_count
++] = query
->id
;
1025 void wined3d_context_gl_alloc_so_statistics_query(struct wined3d_context_gl
*context_gl
,
1026 struct wined3d_so_statistics_query
*query
)
1028 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1030 if (context_gl
->free_so_statistics_query_count
)
1032 query
->u
= context_gl
->free_so_statistics_queries
[--context_gl
->free_so_statistics_query_count
];
1036 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
1037 checkGLcall("glGenQueries");
1039 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
1040 query
->u
.id
[0], query
->u
.id
[1], context_gl
);
1043 query
->context_gl
= context_gl
;
1044 list_add_head(&context_gl
->so_statistics_queries
, &query
->entry
);
1047 void wined3d_context_gl_free_so_statistics_query(struct wined3d_so_statistics_query
*query
)
1049 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
1051 list_remove(&query
->entry
);
1052 query
->context_gl
= NULL
;
1054 if (!wined3d_array_reserve((void **)&context_gl
->free_so_statistics_queries
,
1055 &context_gl
->free_so_statistics_query_size
, context_gl
->free_so_statistics_query_count
+ 1,
1056 sizeof(*context_gl
->free_so_statistics_queries
)))
1058 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
1059 query
->u
.id
[0], query
->u
.id
[1], context_gl
);
1063 context_gl
->free_so_statistics_queries
[context_gl
->free_so_statistics_query_count
++] = query
->u
;
1066 void wined3d_context_gl_alloc_pipeline_statistics_query(struct wined3d_context_gl
*context_gl
,
1067 struct wined3d_pipeline_statistics_query
*query
)
1069 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1071 if (context_gl
->free_pipeline_statistics_query_count
)
1073 query
->u
= context_gl
->free_pipeline_statistics_queries
[--context_gl
->free_pipeline_statistics_query_count
];
1077 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
1078 checkGLcall("glGenQueries");
1081 query
->context_gl
= context_gl
;
1082 list_add_head(&context_gl
->pipeline_statistics_queries
, &query
->entry
);
1085 void wined3d_context_gl_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query
*query
)
1087 struct wined3d_context_gl
*context_gl
= query
->context_gl
;
1089 list_remove(&query
->entry
);
1090 query
->context_gl
= NULL
;
1092 if (!wined3d_array_reserve((void **)&context_gl
->free_pipeline_statistics_queries
,
1093 &context_gl
->free_pipeline_statistics_query_size
, context_gl
->free_pipeline_statistics_query_count
+ 1,
1094 sizeof(*context_gl
->free_pipeline_statistics_queries
)))
1096 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context_gl
);
1100 context_gl
->free_pipeline_statistics_queries
[context_gl
->free_pipeline_statistics_query_count
++] = query
->u
;
1103 typedef void (context_fbo_entry_func_t
)(struct wined3d_context_gl
*context_gl
, struct fbo_entry
*entry
);
1105 static void wined3d_context_gl_enum_fbo_entries(const struct wined3d_device
*device
,
1106 GLuint name
, BOOL rb_namespace
, context_fbo_entry_func_t
*callback
)
1110 for (i
= 0; i
< device
->context_count
; ++i
)
1112 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(device
->contexts
[i
]);
1113 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1114 struct fbo_entry
*entry
, *entry2
;
1116 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_list
, struct fbo_entry
, entry
)
1118 for (j
= 0; j
< gl_info
->limits
.buffers
+ 1; ++j
)
1120 if (entry
->key
.objects
[j
].object
== name
1121 && !(entry
->key
.rb_namespace
& (1 << j
)) == !rb_namespace
)
1123 callback(context_gl
, entry
);
1131 static void wined3d_context_gl_queue_fbo_entry_destruction(struct wined3d_context_gl
*context_gl
,
1132 struct fbo_entry
*entry
)
1134 list_remove(&entry
->entry
);
1135 list_add_head(&context_gl
->fbo_destroy_list
, &entry
->entry
);
1138 void context_gl_resource_released(struct wined3d_device
*device
, GLuint name
, BOOL rb_namespace
)
1140 wined3d_context_gl_enum_fbo_entries(device
, name
, rb_namespace
,
1141 wined3d_context_gl_queue_fbo_entry_destruction
);
1144 void wined3d_context_gl_texture_update(struct wined3d_context_gl
*context_gl
,
1145 const struct wined3d_texture_gl
*texture_gl
)
1147 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1148 struct fbo_entry
*entry
= context_gl
->current_fbo
;
1151 if (!entry
|| context_gl
->rebind_fbo
)
1154 for (i
= 0; i
< gl_info
->limits
.buffers
+ 1; ++i
)
1156 if (texture_gl
->texture_rgb
.name
== entry
->key
.objects
[i
].object
1157 || texture_gl
->texture_srgb
.name
== entry
->key
.objects
[i
].object
)
1159 TRACE("Updated texture %p is bound as attachment %u to the current FBO.\n", texture_gl
, i
);
1160 context_gl
->rebind_fbo
= TRUE
;
1166 static BOOL
wined3d_context_gl_restore_pixel_format(struct wined3d_context_gl
*context_gl
)
1168 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1171 if (context_gl
->restore_pf
&& IsWindow(context_gl
->restore_pf_win
))
1173 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1175 HDC dc
= GetDCEx(context_gl
->restore_pf_win
, 0, DCX_USESTYLE
| DCX_CACHE
);
1178 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, context_gl
->restore_pf
))))
1180 ERR("Failed to restore pixel format %d on window %p.\n",
1181 context_gl
->restore_pf
, context_gl
->restore_pf_win
);
1183 ReleaseDC(context_gl
->restore_pf_win
, dc
);
1188 ERR("Unable to restore pixel format %d on window %p.\n",
1189 context_gl
->restore_pf
, context_gl
->restore_pf_win
);
1193 context_gl
->restore_pf
= 0;
1194 context_gl
->restore_pf_win
= NULL
;
1198 static BOOL
wined3d_context_gl_set_pixel_format(struct wined3d_context_gl
*context_gl
)
1200 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1201 BOOL
private = context_gl
->dc_is_private
;
1202 int format
= context_gl
->pixel_format
;
1203 HDC dc
= context_gl
->dc
;
1207 if (private && context_gl
->dc_has_format
)
1210 if (!private && WindowFromDC(dc
) != context_gl
->window
)
1213 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
1214 if ((current
== format
) || (!current
&& context_gl
->internal_format_set
))
1217 /* By default WGL doesn't allow pixel format adjustments but we need it
1218 * here. For this reason there's a Wine specific wglSetPixelFormat()
1219 * which allows us to set the pixel format multiple times. Use it when we
1220 * can, because even though no pixel format may currently be set, the
1221 * application may try to set one later. */
1222 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1224 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
1226 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1230 context_gl
->internal_format_set
= 1;
1234 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1235 * continue using the old format. There's a big chance that the old
1236 * format works although with a performance hit and perhaps rendering
1238 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1239 format
, dc
, current
);
1242 else if (!SetPixelFormat(dc
, format
, NULL
))
1244 /* This may also happen if the dc belongs to a destroyed window. */
1245 WARN("Failed to set pixel format %d on device context %p, last error %#lx.\n",
1246 format
, dc
, GetLastError());
1250 win
= private ? NULL
: WindowFromDC(dc
);
1251 if (win
!= context_gl
->restore_pf_win
)
1252 wined3d_context_gl_restore_pixel_format(context_gl
);
1253 context_gl
->restore_pf
= private ? 0 : current
;
1254 context_gl
->restore_pf_win
= win
;
1258 context_gl
->dc_has_format
= TRUE
;
1262 static BOOL
wined3d_context_gl_set_gl_context(struct wined3d_context_gl
*context_gl
)
1264 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
1265 BOOL backup
= FALSE
;
1267 TRACE("context_gl %p.\n", context_gl
);
1269 if (!wined3d_context_gl_set_pixel_format(context_gl
))
1271 WARN("Failed to set pixel format %d on device context %p.\n",
1272 context_gl
->pixel_format
, context_gl
->dc
);
1276 if (backup
|| !wglMakeCurrent(context_gl
->dc
, context_gl
->gl_ctx
))
1278 WARN("Failed to make GL context %p current on device context %p, last error %#lx.\n",
1279 context_gl
->gl_ctx
, context_gl
->dc
, GetLastError());
1280 context_gl
->valid
= 0;
1281 WARN("Trying fallback to the backup window.\n");
1283 if (context_gl
->c
.destroyed
)
1285 FIXME("Unable to get backup dc for destroyed context %p.\n", context_gl
);
1286 wined3d_context_gl_set_current(NULL
);
1290 if (!(context_gl
->dc
= wined3d_device_gl_get_backup_dc(device_gl
)))
1292 wined3d_context_gl_set_current(NULL
);
1296 TRACE("Using backup DC %p.\n", context_gl
->dc
);
1297 context_gl
->dc_is_private
= TRUE
;
1298 context_gl
->dc_has_format
= FALSE
;
1300 if (!wined3d_context_gl_set_pixel_format(context_gl
))
1302 ERR("Failed to set pixel format %d on device context %p.\n",
1303 context_gl
->pixel_format
, context_gl
->dc
);
1304 wined3d_context_gl_set_current(NULL
);
1308 if (!wglMakeCurrent(context_gl
->dc
, context_gl
->gl_ctx
))
1310 ERR("Fallback to backup window (dc %p) failed too, last error %#lx.\n",
1311 context_gl
->dc
, GetLastError());
1312 wined3d_context_gl_set_current(NULL
);
1316 context_gl
->valid
= 1;
1318 context_gl
->needs_set
= 0;
1323 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
1325 if (!wglMakeCurrent(dc
, gl_ctx
))
1327 ERR("Failed to restore GL context %p on device context %p, last error %#lx.\n",
1328 gl_ctx
, dc
, GetLastError());
1329 wined3d_context_gl_set_current(NULL
);
1333 static void wined3d_context_gl_update_window(struct wined3d_context_gl
*context_gl
)
1335 if (!context_gl
->c
.swapchain
)
1338 if (context_gl
->window
== context_gl
->c
.swapchain
->win_handle
)
1341 TRACE("Updating context %p window from %p to %p.\n",
1342 context_gl
, context_gl
->window
, context_gl
->c
.swapchain
->win_handle
);
1345 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
1347 context_gl
->window
= context_gl
->c
.swapchain
->win_handle
;
1348 context_gl
->dc_is_private
= FALSE
;
1349 context_gl
->dc_has_format
= FALSE
;
1350 context_gl
->needs_set
= 1;
1351 context_gl
->valid
= 1;
1352 context_gl
->internal_format_set
= 0;
1354 if (!(context_gl
->dc
= GetDCEx(context_gl
->window
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1356 ERR("Failed to get a device context for window %p.\n", context_gl
->window
);
1357 context_gl
->valid
= 0;
1361 static void wined3d_context_gl_cleanup(struct wined3d_context_gl
*context_gl
)
1363 struct wined3d_pipeline_statistics_query
*pipeline_statistics_query
;
1364 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1365 struct wined3d_so_statistics_query
*so_statistics_query
;
1366 struct wined3d_timestamp_query
*timestamp_query
;
1367 struct wined3d_occlusion_query
*occlusion_query
;
1368 struct wined3d_context_gl
*current
;
1369 struct fbo_entry
*entry
, *entry2
;
1370 struct wined3d_fence
*fence
;
1375 TRACE("context_gl %p.\n", context_gl
);
1377 restore_ctx
= wglGetCurrentContext();
1378 restore_dc
= wglGetCurrentDC();
1380 if (context_gl
->valid
&& context_gl
->gl_ctx
!= restore_ctx
)
1382 /* Attempting to restore a GL context corresponding to a wined3d
1383 * context is not particularly useful. Worse, when we're called from
1384 * wined3d_context_gl_destroy(), we subsequently clear the "current
1385 * D3D context" TLS value, which would cause
1386 * wined3d_context_gl_enter() to consider the GL context a non-D3D
1388 if ((current
= wined3d_context_gl_get_current()) && current
->gl_ctx
== restore_ctx
)
1390 wined3d_context_gl_set_gl_context(context_gl
);
1397 if (context_gl
->valid
)
1399 /* If we're here because we're switching away from a previously
1400 * destroyed context, acquiring a context in order to submit a fence
1401 * is problematic. In particular, we'd end up back here again in the
1402 * process of switching to the newly acquired context.
1404 * If fences aren't supported there should be nothing to wait for
1405 * anyway, so just do nothing in that case. */
1406 if (context_gl
->c
.destroyed
)
1408 gl_info
->gl_ops
.gl
.p_glFinish();
1410 else if (context_gl
->c
.d3d_info
->fences
)
1412 wined3d_context_gl_submit_command_fence(context_gl
);
1413 wined3d_context_gl_wait_command_fence(context_gl
,
1414 wined3d_device_gl(context_gl
->c
.device
)->current_fence_id
- 1);
1417 if (context_gl
->dummy_arbfp_prog
)
1418 GL_EXTCALL(glDeleteProgramsARB(1, &context_gl
->dummy_arbfp_prog
));
1420 if (context_gl
->blit_vbo
)
1421 GL_EXTCALL(glDeleteBuffers(1, &context_gl
->blit_vbo
));
1423 for (i
= 0; i
< context_gl
->free_pipeline_statistics_query_count
; ++i
)
1425 union wined3d_gl_pipeline_statistics_query
*q
= &context_gl
->free_pipeline_statistics_queries
[i
];
1426 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1429 for (i
= 0; i
< context_gl
->free_so_statistics_query_count
; ++i
)
1431 union wined3d_gl_so_statistics_query
*q
= &context_gl
->free_so_statistics_queries
[i
];
1432 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1435 if (context_gl
->free_timestamp_query_count
)
1436 GL_EXTCALL(glDeleteQueries(context_gl
->free_timestamp_query_count
, context_gl
->free_timestamp_queries
));
1438 if (gl_info
->supported
[ARB_SYNC
])
1440 for (i
= 0; i
< context_gl
->free_fence_count
; ++i
)
1442 GL_EXTCALL(glDeleteSync(context_gl
->free_fences
[i
].sync
));
1445 else if (gl_info
->supported
[APPLE_FENCE
])
1447 for (i
= 0; i
< context_gl
->free_fence_count
; ++i
)
1449 GL_EXTCALL(glDeleteFencesAPPLE(1, &context_gl
->free_fences
[i
].id
));
1452 else if (gl_info
->supported
[NV_FENCE
])
1454 for (i
= 0; i
< context_gl
->free_fence_count
; ++i
)
1456 GL_EXTCALL(glDeleteFencesNV(1, &context_gl
->free_fences
[i
].id
));
1460 if (context_gl
->free_occlusion_query_count
)
1461 GL_EXTCALL(glDeleteQueries(context_gl
->free_occlusion_query_count
, context_gl
->free_occlusion_queries
));
1463 checkGLcall("context cleanup");
1465 heap_free(context_gl
->submitted
.fences
);
1466 heap_free(context_gl
->free_pipeline_statistics_queries
);
1467 heap_free(context_gl
->free_so_statistics_queries
);
1468 heap_free(context_gl
->free_timestamp_queries
);
1469 heap_free(context_gl
->free_fences
);
1470 heap_free(context_gl
->free_occlusion_queries
);
1472 LIST_FOR_EACH_ENTRY(pipeline_statistics_query
, &context_gl
->pipeline_statistics_queries
,
1473 struct wined3d_pipeline_statistics_query
, entry
)
1475 if (context_gl
->valid
)
1476 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query
->u
.id
), pipeline_statistics_query
->u
.id
));
1477 pipeline_statistics_query
->context_gl
= NULL
;
1480 LIST_FOR_EACH_ENTRY(so_statistics_query
, &context_gl
->so_statistics_queries
,
1481 struct wined3d_so_statistics_query
, entry
)
1483 if (context_gl
->valid
)
1484 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query
->u
.id
), so_statistics_query
->u
.id
));
1485 so_statistics_query
->context_gl
= NULL
;
1488 LIST_FOR_EACH_ENTRY(timestamp_query
, &context_gl
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
1490 if (context_gl
->valid
)
1491 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
1492 timestamp_query
->context_gl
= NULL
;
1495 LIST_FOR_EACH_ENTRY(fence
, &context_gl
->fences
, struct wined3d_fence
, entry
)
1497 if (context_gl
->valid
)
1499 if (gl_info
->supported
[ARB_SYNC
])
1501 if (fence
->object
.sync
)
1502 GL_EXTCALL(glDeleteSync(fence
->object
.sync
));
1504 else if (gl_info
->supported
[APPLE_FENCE
])
1506 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence
->object
.id
));
1508 else if (gl_info
->supported
[NV_FENCE
])
1510 GL_EXTCALL(glDeleteFencesNV(1, &fence
->object
.id
));
1513 fence
->context_gl
= NULL
;
1516 LIST_FOR_EACH_ENTRY(occlusion_query
, &context_gl
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1518 if (context_gl
->valid
)
1519 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1520 occlusion_query
->context_gl
= NULL
;
1523 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_destroy_list
, struct fbo_entry
, entry
)
1525 if (!context_gl
->valid
)
1527 wined3d_context_gl_destroy_fbo_entry(context_gl
, entry
);
1530 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context_gl
->fbo_list
, struct fbo_entry
, entry
)
1532 if (!context_gl
->valid
)
1534 wined3d_context_gl_destroy_fbo_entry(context_gl
, entry
);
1537 heap_free(context_gl
->texture_type
);
1539 wined3d_context_gl_restore_pixel_format(context_gl
);
1541 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1542 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1543 ERR("Failed to disable GL context.\n");
1545 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
1547 if (!wglDeleteContext(context_gl
->gl_ctx
))
1549 unsigned int err
= GetLastError();
1550 ERR("Failed to delete GL context %p, last error %#x.\n", context_gl
->gl_ctx
, err
);
1553 wined3d_context_cleanup(&context_gl
->c
);
1556 DWORD
context_get_tls_idx(void)
1558 return wined3d_context_tls_idx
;
1561 void context_set_tls_idx(DWORD idx
)
1563 wined3d_context_tls_idx
= idx
;
1566 struct wined3d_context_gl
*wined3d_context_gl_get_current(void)
1568 return TlsGetValue(wined3d_context_tls_idx
);
1571 BOOL
wined3d_context_gl_set_current(struct wined3d_context_gl
*context_gl
)
1573 struct wined3d_context_gl
*old
= wined3d_context_gl_get_current();
1575 if (old
== context_gl
)
1577 TRACE("Already using D3D context %p.\n", context_gl
);
1583 if (old
->c
.destroyed
)
1585 TRACE("Switching away from destroyed context %p.\n", old
);
1586 wined3d_context_gl_cleanup(old
);
1587 heap_free((void *)old
->gl_info
);
1592 if (wglGetCurrentContext())
1594 const struct wined3d_gl_info
*gl_info
= old
->gl_info
;
1595 TRACE("Flushing context %p before switching to %p.\n", old
, context_gl
);
1596 gl_info
->gl_ops
.gl
.p_glFlush();
1604 if (!context_gl
->valid
)
1606 ERR("Trying to make invalid context %p current.\n", context_gl
);
1610 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n",
1611 context_gl
, context_gl
->gl_ctx
, context_gl
->dc
);
1612 if (!wined3d_context_gl_set_gl_context(context_gl
))
1614 context_gl
->c
.current
= 1;
1616 else if (wglGetCurrentContext())
1618 TRACE("Clearing current D3D context.\n");
1619 if (!wglMakeCurrent(NULL
, NULL
))
1621 unsigned int err
= GetLastError();
1622 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1623 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1628 return TlsSetValue(wined3d_context_tls_idx
, context_gl
);
1631 void wined3d_context_gl_release(struct wined3d_context_gl
*context_gl
)
1633 TRACE("Releasing context %p, level %u.\n", context_gl
, context_gl
->level
);
1637 if (!context_gl
->level
)
1638 WARN("Context %p is not active.\n", context_gl
);
1639 else if (context_gl
!= wined3d_context_gl_get_current())
1640 WARN("Context %p is not the current context.\n", context_gl
);
1643 if (!--context_gl
->level
)
1645 if (wined3d_context_gl_restore_pixel_format(context_gl
))
1646 context_gl
->needs_set
= 1;
1647 if (context_gl
->restore_ctx
)
1649 TRACE("Restoring GL context %p on device context %p.\n", context_gl
->restore_ctx
, context_gl
->restore_dc
);
1650 context_restore_gl_context(context_gl
->gl_info
, context_gl
->restore_dc
, context_gl
->restore_ctx
);
1651 context_gl
->restore_ctx
= NULL
;
1652 context_gl
->restore_dc
= NULL
;
1655 if (context_gl
->c
.destroy_delayed
)
1657 TRACE("Destroying context %p.\n", context_gl
);
1658 wined3d_context_gl_destroy(context_gl
);
1663 static void wined3d_context_gl_enter(struct wined3d_context_gl
*context_gl
)
1665 TRACE("Entering context %p, level %u.\n", context_gl
, context_gl
->level
+ 1);
1667 if (!context_gl
->level
++)
1669 const struct wined3d_context_gl
*current_context
= wined3d_context_gl_get_current();
1670 HGLRC current_gl
= wglGetCurrentContext();
1672 if (current_gl
&& (!current_context
|| current_context
->gl_ctx
!= current_gl
))
1674 TRACE("Another GL context (%p on device context %p) is already current.\n",
1675 current_gl
, wglGetCurrentDC());
1676 context_gl
->restore_ctx
= current_gl
;
1677 context_gl
->restore_dc
= wglGetCurrentDC();
1678 context_gl
->needs_set
= 1;
1680 else if (!context_gl
->needs_set
&& !(context_gl
->dc_is_private
&& context_gl
->dc_has_format
))
1682 int current
= context_gl
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context_gl
->dc
);
1684 if ((current
&& current
!= context_gl
->pixel_format
) || (!current
&& !context_gl
->internal_format_set
))
1685 context_gl
->needs_set
= 1;
1690 /* This function takes care of wined3d pixel format selection. */
1691 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1692 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1693 bool aux_buffers
, bool swap_effect_copy
)
1695 unsigned int cfg_count
= wined3d_adapter_gl(device
->adapter
)->pixel_format_count
;
1696 unsigned int current_value
;
1697 PIXELFORMATDESCRIPTOR pfd
;
1698 int iPixelFormat
= 0;
1701 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, swap_effect_copy %#x.\n",
1702 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1703 aux_buffers
, swap_effect_copy
);
1706 for (i
= 0; i
< cfg_count
; ++i
)
1708 const struct wined3d_pixel_format
*cfg
= &wined3d_adapter_gl(device
->adapter
)->pixel_formats
[i
];
1711 /* For now only accept RGBA formats. Perhaps some day we will
1712 * allow floating point formats for pbuffers. */
1713 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1715 /* In window mode we need a window drawable format and double buffering. */
1716 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1718 if (cfg
->redSize
< color_format
->red_size
)
1720 if (cfg
->greenSize
< color_format
->green_size
)
1722 if (cfg
->blueSize
< color_format
->blue_size
)
1724 if (cfg
->alphaSize
< color_format
->alpha_size
)
1726 if (cfg
->depthSize
< ds_format
->depth_size
)
1728 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1730 /* Check multisampling support. */
1731 if (cfg
->numSamples
)
1735 if (swap_effect_copy
&& cfg
->swap_method
== WGL_SWAP_COPY_ARB
)
1737 /* We try to locate a format which matches our requirements exactly. In case of
1738 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1739 if (cfg
->depthSize
== ds_format
->depth_size
)
1741 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1743 if (cfg
->alphaSize
== color_format
->alpha_size
)
1745 /* We like to have aux buffers in backbuffer mode */
1746 if (aux_buffers
&& cfg
->auxBuffers
)
1748 if (cfg
->redSize
== color_format
->red_size
1749 && cfg
->greenSize
== color_format
->green_size
1750 && cfg
->blueSize
== color_format
->blue_size
)
1753 if (value
> current_value
)
1755 iPixelFormat
= cfg
->iPixelFormat
;
1756 current_value
= value
;
1762 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1764 memset(&pfd
, 0, sizeof(pfd
));
1765 pfd
.nSize
= sizeof(pfd
);
1767 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1768 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1769 pfd
.cAlphaBits
= color_format
->alpha_size
;
1770 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1771 + color_format
->blue_size
+ color_format
->alpha_size
;
1772 pfd
.cDepthBits
= ds_format
->depth_size
;
1773 pfd
.cStencilBits
= ds_format
->stencil_size
;
1774 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1776 if (!(iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
)))
1778 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1779 ERR("Can't find a suitable pixel format.\n");
1784 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1785 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1786 return iPixelFormat
;
1789 /* Context activation is done by the caller. */
1790 void wined3d_context_gl_bind_dummy_textures(const struct wined3d_context_gl
*context_gl
)
1792 const struct wined3d_dummy_textures
*textures
= &wined3d_device_gl(context_gl
->c
.device
)->dummy_textures
;
1793 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1796 for (i
= 0; i
< gl_info
->limits
.combined_samplers
; ++i
)
1798 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1800 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, textures
->tex_1d
);
1801 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, textures
->tex_2d
);
1803 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1804 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, textures
->tex_rect
);
1806 if (gl_info
->supported
[EXT_TEXTURE3D
])
1807 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, textures
->tex_3d
);
1809 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1810 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, textures
->tex_cube
);
1812 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
1813 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, textures
->tex_cube_array
);
1815 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
1817 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, textures
->tex_1d_array
);
1818 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, textures
->tex_2d_array
);
1821 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
1822 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, textures
->tex_buffer
);
1824 if (gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
1826 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, textures
->tex_2d_ms
);
1827 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, textures
->tex_2d_ms_array
);
1831 checkGLcall("bind dummy textures");
1834 void wined3d_check_gl_call(const struct wined3d_gl_info
*gl_info
,
1835 const char *file
, unsigned int line
, const char *name
)
1839 if (gl_info
->supported
[ARB_DEBUG_OUTPUT
] || (err
= gl_info
->gl_ops
.gl
.p_glGetError()) == GL_NO_ERROR
)
1841 TRACE("%s call ok %s / %u.\n", name
, file
, line
);
1847 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1848 debug_glerror(err
), err
, name
, file
,line
);
1849 err
= gl_info
->gl_ops
.gl
.p_glGetError();
1850 } while (err
!= GL_NO_ERROR
);
1853 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1855 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1856 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1859 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1860 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1864 case GL_DEBUG_TYPE_ERROR_ARB
:
1865 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1868 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1869 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1870 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1871 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1874 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1875 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1879 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1884 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1887 unsigned int ctx_attrib_idx
= 0;
1888 GLint ctx_attribs
[7], ctx_flags
= 0;
1890 if (context_debug_output_enabled(gl_info
))
1891 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1892 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1893 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1894 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1895 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1898 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1899 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1901 ctx_attribs
[ctx_attrib_idx
] = 0;
1903 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1905 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1909 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1910 ctx_attribs
[ctx_attrib_idx
- 1] = ctx_flags
;
1914 ctx_flags
= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1915 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1916 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1917 ctx_attribs
[ctx_attrib_idx
] = 0;
1919 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1920 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#lx.\n",
1927 static BOOL
wined3d_context_gl_create_wgl_ctx(struct wined3d_context_gl
*context_gl
,
1928 struct wined3d_swapchain_gl
*swapchain_gl
, BOOL
*new_drawable
)
1930 enum wined3d_swap_effect swap_effect
= swapchain_gl
->s
.state
.desc
.swap_effect
;
1931 const struct wined3d_format
*colour_format
, *ds_format
;
1932 struct wined3d_context
*context
= &context_gl
->c
;
1933 const struct wined3d_gl_info
*gl_info
;
1934 struct wined3d_resource
*target
;
1935 struct wined3d_adapter
*adapter
;
1936 unsigned int target_bind_flags
;
1937 struct wined3d_device
*device
;
1938 bool swap_effect_copy
;
1939 HGLRC ctx
, share_ctx
;
1942 device
= context
->device
;
1943 adapter
= device
->adapter
;
1944 gl_info
= &wined3d_adapter_gl(adapter
)->gl_info
;
1946 target
= &context
->current_rt
.texture
->resource
;
1947 target_bind_flags
= target
->bind_flags
;
1949 swap_effect_copy
= swap_effect
== WINED3D_SWAP_EFFECT_COPY
|| swap_effect
== WINED3D_SWAP_EFFECT_COPY_VSYNC
;
1951 *new_drawable
= !GetPixelFormat(context_gl
->dc
);
1953 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1955 static const enum wined3d_format_id ds_formats
[] =
1957 WINED3DFMT_D24_UNORM_S8_UINT
,
1958 WINED3DFMT_D32_UNORM
,
1959 WINED3DFMT_R24_UNORM_X8_TYPELESS
,
1960 WINED3DFMT_D16_UNORM
,
1961 WINED3DFMT_S1_UINT_D15_UNORM
,
1964 colour_format
= target
->format
;
1966 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1967 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1968 if (colour_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1969 colour_format
= wined3d_get_format(adapter
, WINED3DFMT_B4G4R4A4_UNORM
, target_bind_flags
);
1970 else if (colour_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1971 colour_format
= wined3d_get_format(adapter
, WINED3DFMT_B8G8R8A8_UNORM
, target_bind_flags
);
1973 /* DirectDraw supports 8bit paletted render targets and these are used by
1974 * old games like StarCraft and C&C. Most modern hardware doesn't support
1975 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1976 * conversion (ab)uses the alpha component for storing the palette index.
1977 * For this reason we require a format with 8bit alpha, so request
1979 if (colour_format
->id
== WINED3DFMT_P8_UINT
)
1980 colour_format
= wined3d_get_format(adapter
, WINED3DFMT_B8G8R8A8_UNORM
, target_bind_flags
);
1982 /* Try to find a pixel format which matches our requirements. */
1983 if (!swapchain_gl
->s
.ds_format
)
1985 for (i
= 0; i
< ARRAY_SIZE(ds_formats
); ++i
)
1987 ds_format
= wined3d_get_format(adapter
, ds_formats
[i
], WINED3D_BIND_DEPTH_STENCIL
);
1988 if ((context_gl
->pixel_format
= context_choose_pixel_format(device
,
1989 context_gl
->dc
, colour_format
, ds_format
, true, swap_effect_copy
)))
1991 swapchain_gl
->s
.ds_format
= ds_format
;
1995 TRACE("Depth stencil format %s is not supported, trying next format.\n",
1996 debug_d3dformat(ds_format
->id
));
2001 context_gl
->pixel_format
= context_choose_pixel_format(device
,
2002 context_gl
->dc
, colour_format
, swapchain_gl
->s
.ds_format
, true, swap_effect_copy
);
2007 /* When using FBOs for off-screen rendering, we only use the drawable for
2008 * presentation blits, and don't do any rendering to it. That means we
2009 * don't need depth or stencil buffers, and can mostly ignore the render
2010 * target format. This wouldn't necessarily be quite correct for 10bpc
2011 * display modes, but we don't currently support those.
2012 * Using the same format regardless of the colour/depth/stencil targets
2013 * makes it much less likely that different wined3d instances will set
2014 * conflicting pixel formats. */
2015 colour_format
= wined3d_get_format(adapter
, WINED3DFMT_B8G8R8A8_UNORM
, target_bind_flags
);
2016 ds_format
= wined3d_get_format(adapter
, WINED3DFMT_UNKNOWN
, WINED3D_BIND_DEPTH_STENCIL
);
2017 context_gl
->pixel_format
= context_choose_pixel_format(device
,
2018 context_gl
->dc
, colour_format
, ds_format
, false, swap_effect_copy
);
2021 if (!context_gl
->pixel_format
)
2023 ERR("Failed to choose pixel format.\n");
2027 wined3d_context_gl_enter(context_gl
);
2029 if (!wined3d_context_gl_set_pixel_format(context_gl
))
2031 context_release(context
);
2033 if (context_gl
->dc_is_private
)
2035 ERR("Failed to set pixel format %d on device context %p.\n", context_gl
->pixel_format
, context_gl
->dc
);
2040 WARN("Failed to set pixel format %d on device context %p, trying backup DC.\n",
2041 context_gl
->pixel_format
, context_gl
->dc
);
2043 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
2044 if (!(context_gl
->dc
= wined3d_device_gl_get_backup_dc(wined3d_device_gl(device
))))
2046 ERR("Failed to retrieve the backup device context.\n");
2049 context_gl
->dc_is_private
= TRUE
;
2051 return wined3d_context_gl_create_wgl_ctx(context_gl
, swapchain_gl
, new_drawable
);
2054 share_ctx
= device
->context_count
? wined3d_context_gl(device
->contexts
[0])->gl_ctx
: NULL
;
2055 if (gl_info
->p_wglCreateContextAttribsARB
)
2057 if (!(ctx
= context_create_wgl_attribs(gl_info
, context_gl
->dc
, share_ctx
)))
2059 ERR("Failed to create a WGL context.\n");
2060 context_release(context
);
2066 if (!(ctx
= wglCreateContext(context_gl
->dc
)))
2068 ERR("Failed to create a WGL context.\n");
2069 context_release(context
);
2073 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
2075 ERR("wglShareLists(%p, %p) failed, last error %#lx.\n", share_ctx
, ctx
, GetLastError());
2076 context_release(context
);
2077 if (!wglDeleteContext(ctx
))
2078 ERR("wglDeleteContext(%p) failed, last error %#lx.\n", ctx
, GetLastError());
2083 context_gl
->dc_has_format
= TRUE
;
2084 context_gl
->needs_set
= 1;
2085 context_gl
->valid
= 1;
2086 context_gl
->gl_ctx
= ctx
;
2091 HRESULT
wined3d_context_gl_init(struct wined3d_context_gl
*context_gl
, struct wined3d_swapchain_gl
*swapchain_gl
)
2093 struct wined3d_context
*context
= &context_gl
->c
;
2094 const struct wined3d_d3d_info
*d3d_info
;
2095 const struct wined3d_gl_info
*gl_info
;
2096 struct wined3d_device
*device
;
2100 TRACE("context_gl %p, swapchain %p.\n", context_gl
, swapchain_gl
);
2102 wined3d_context_init(&context_gl
->c
, &swapchain_gl
->s
);
2104 device
= context
->device
;
2105 gl_info
= &wined3d_adapter_gl(device
->adapter
)->gl_info
;
2106 context_gl
->gl_info
= gl_info
;
2107 d3d_info
= context
->d3d_info
;
2109 context_gl
->tid
= GetCurrentThreadId();
2110 context_gl
->window
= context
->swapchain
->win_handle
;
2111 if (context_gl
->window
== GetDesktopWindow())
2113 TRACE("Swapchain is created on the desktop window, trying backup device context.\n");
2114 context_gl
->dc
= NULL
;
2116 else if (!(context_gl
->dc
= GetDCEx(context_gl
->window
, 0, DCX_USESTYLE
| DCX_CACHE
)))
2117 WARN("Failed to retrieve device context, trying swapchain backup.\n");
2119 if (!context_gl
->dc
)
2121 if (!(context_gl
->dc
= wined3d_device_gl_get_backup_dc(wined3d_device_gl(device
))))
2123 ERR("Failed to retrieve a device context.\n");
2126 context_gl
->dc_is_private
= TRUE
;
2129 list_init(&context_gl
->fbo_list
);
2130 list_init(&context_gl
->fbo_destroy_list
);
2132 list_init(&context_gl
->occlusion_queries
);
2133 list_init(&context_gl
->fences
);
2134 list_init(&context_gl
->timestamp_queries
);
2135 list_init(&context_gl
->so_statistics_queries
);
2136 list_init(&context_gl
->pipeline_statistics_queries
);
2138 for (i
= 0; i
< ARRAY_SIZE(context_gl
->tex_unit_map
); ++i
)
2139 context_gl
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2140 for (i
= 0; i
< ARRAY_SIZE(context_gl
->rev_tex_unit_map
); ++i
)
2141 context_gl
->rev_tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
2142 if (gl_info
->limits
.graphics_samplers
>= WINED3D_MAX_COMBINED_SAMPLERS
)
2144 /* Initialize the texture unit mapping to a 1:1 mapping. */
2145 unsigned int base
, count
;
2147 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_PIXEL
, &base
, &count
);
2148 if (base
+ WINED3D_MAX_FRAGMENT_SAMPLERS
> ARRAY_SIZE(context_gl
->rev_tex_unit_map
))
2150 ERR("Unexpected texture unit base index %u.\n", base
);
2153 for (i
= 0; i
< min(count
, WINED3D_MAX_FRAGMENT_SAMPLERS
); ++i
)
2155 context_gl
->tex_unit_map
[i
] = base
+ i
;
2156 context_gl
->rev_tex_unit_map
[base
+ i
] = i
;
2159 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_VERTEX
, &base
, &count
);
2160 if (base
+ WINED3D_MAX_VERTEX_SAMPLERS
> ARRAY_SIZE(context_gl
->rev_tex_unit_map
))
2162 ERR("Unexpected texture unit base index %u.\n", base
);
2165 for (i
= 0; i
< min(count
, WINED3D_MAX_VERTEX_SAMPLERS
); ++i
)
2167 context_gl
->tex_unit_map
[WINED3D_MAX_FRAGMENT_SAMPLERS
+ i
] = base
+ i
;
2168 context_gl
->rev_tex_unit_map
[base
+ i
] = WINED3D_MAX_FRAGMENT_SAMPLERS
+ i
;
2172 if (!(context_gl
->texture_type
= heap_calloc(gl_info
->limits
.combined_samplers
,
2173 sizeof(*context_gl
->texture_type
))))
2176 if (!wined3d_context_gl_create_wgl_ctx(context_gl
, swapchain_gl
, &new_drawable
))
2179 /* Set up the context defaults. */
2181 context
->render_offscreen
= wined3d_resource_is_offscreen(&context
->current_rt
.texture
->resource
);
2182 context_gl
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
2184 if (!wined3d_context_gl_set_current(context_gl
))
2186 ERR("Cannot activate context to set up defaults.\n");
2187 context_release(context
);
2188 if (!wglDeleteContext(context_gl
->gl_ctx
))
2189 ERR("wglDeleteContext(%p) failed, last error %#lx.\n", context_gl
->gl_ctx
, GetLastError());
2193 if (context_debug_output_enabled(gl_info
))
2195 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, context
));
2196 if (TRACE_ON(d3d_sync
))
2197 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
2198 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
2201 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
2202 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2206 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
2207 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2208 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
2209 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2210 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
2211 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2213 if (WARN_ON(d3d_perf
))
2215 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
2216 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2220 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2221 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &context_gl
->aux_buffers
);
2223 TRACE("Setting up the screen\n");
2225 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2227 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
2228 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2230 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
2231 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2233 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
2234 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2240 GL_EXTCALL(glGenVertexArrays(1, &vao
));
2241 GL_EXTCALL(glBindVertexArray(vao
));
2242 checkGLcall("creating VAO");
2245 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
2246 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2247 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
2248 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2250 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
2252 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2253 * the previous texture where to source the offset from is always unit - 1.
2255 for (i
= 1; i
< gl_info
->limits
.ffp_textures
; ++i
)
2257 wined3d_context_gl_active_texture(context_gl
, gl_info
, i
);
2258 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
2259 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ i
- 1);
2260 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2263 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
2265 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2266 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2267 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2268 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2271 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2272 * program and the dummy program is destroyed when the context is destroyed.
2274 static const char dummy_program
[] =
2276 "MOV result.color, fragment.color.primary;\n"
2278 GL_EXTCALL(glGenProgramsARB(1, &context_gl
->dummy_arbfp_prog
));
2279 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, context_gl
->dummy_arbfp_prog
));
2280 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
,
2281 GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
2284 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2286 for (i
= 0; i
< gl_info
->limits
.ffp_textures
; ++i
)
2288 wined3d_context_gl_active_texture(context_gl
, gl_info
, i
);
2289 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
2290 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2294 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
2296 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
2298 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
2300 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
2302 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_NO_PRIMITIVE_RESTART
))
2304 if (gl_info
->supported
[ARB_ES3_COMPATIBILITY
])
2306 gl_info
->gl_ops
.gl
.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX
);
2307 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2311 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2314 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_CUBEMAP_FILTERING
)
2315 && gl_info
->supported
[ARB_SEAMLESS_CUBE_MAP
])
2317 gl_info
->gl_ops
.gl
.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
2318 checkGLcall("enable seamless cube map filtering");
2320 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
2321 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN
, GL_LOWER_LEFT
));
2323 /* If this happens to be the first context for the device, dummy textures
2324 * are not created yet. In that case, they will be created (and bound) by
2325 * create_dummy_textures right after this context is initialized. */
2326 if (wined3d_device_gl(device
)->dummy_textures
.tex_2d
)
2327 wined3d_context_gl_bind_dummy_textures(context_gl
);
2329 /* Initialise all rectangles to avoid resetting unused ones later. */
2330 gl_info
->gl_ops
.gl
.p_glScissor(0, 0, 0, 0);
2331 checkGLcall("glScissor");
2335 gl_info
->gl_ops
.gl
.p_glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
2336 gl_info
->gl_ops
.gl
.p_glClearDepth(1.0f
);
2337 gl_info
->gl_ops
.gl
.p_glClearStencil(0);
2338 gl_info
->gl_ops
.gl
.p_glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
2339 checkGLcall("glClear");
2344 heap_free(context_gl
->texture_type
);
2345 wined3d_release_dc(context_gl
->window
, context_gl
->dc
);
2349 void wined3d_context_gl_destroy(struct wined3d_context_gl
*context_gl
)
2351 struct wined3d_device
*device
= context_gl
->c
.device
;
2353 TRACE("Destroying context %p.\n", context_gl
);
2355 wined3d_from_cs(device
->cs
);
2357 /* We delay destroying a context when it is active. The context_release()
2358 * function invokes wined3d_context_gl_destroy() again while leaving the
2360 if (context_gl
->level
)
2362 TRACE("Delaying destruction of context %p.\n", context_gl
);
2363 context_gl
->c
.destroy_delayed
= 1;
2364 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2365 context_gl
->c
.swapchain
= NULL
;
2366 context_gl
->c
.device
= NULL
;
2370 device_context_remove(device
, &context_gl
->c
);
2372 if (context_gl
->c
.current
&& context_gl
->tid
!= GetCurrentThreadId())
2374 struct wined3d_gl_info
*gl_info
;
2376 /* Make a copy of gl_info for wined3d_context_gl_cleanup() use, the
2377 * one in wined3d_adapter may go away in the meantime. */
2378 gl_info
= heap_alloc(sizeof(*gl_info
));
2379 *gl_info
= *context_gl
->gl_info
;
2380 context_gl
->gl_info
= gl_info
;
2381 context_gl
->c
.destroyed
= 1;
2386 wined3d_context_gl_cleanup(context_gl
);
2387 TlsSetValue(context_get_tls_idx(), NULL
);
2388 heap_free(context_gl
);
2391 const unsigned int *wined3d_context_gl_get_tex_unit_mapping(const struct wined3d_context_gl
*context_gl
,
2392 const struct wined3d_shader_version
*shader_version
, unsigned int *base
, unsigned int *count
)
2394 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2396 if (!shader_version
)
2399 *count
= WINED3D_MAX_FFP_TEXTURES
;
2400 return context_gl
->tex_unit_map
;
2403 if (shader_version
->major
>= 4)
2405 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, shader_version
->type
, base
, count
);
2409 switch (shader_version
->type
)
2411 case WINED3D_SHADER_TYPE_PIXEL
:
2413 *count
= WINED3D_MAX_FRAGMENT_SAMPLERS
;
2415 case WINED3D_SHADER_TYPE_VERTEX
:
2416 *base
= WINED3D_MAX_FRAGMENT_SAMPLERS
;
2417 *count
= WINED3D_MAX_VERTEX_SAMPLERS
;
2420 ERR("Unhandled shader type %#x.\n", shader_version
->type
);
2425 return context_gl
->tex_unit_map
;
2428 static void wined3d_context_gl_get_rt_size(const struct wined3d_context_gl
*context_gl
, SIZE
*size
)
2430 const struct wined3d_texture
*rt
= context_gl
->c
.current_rt
.texture
;
2437 GetClientRect(context_gl
->window
, &window_size
);
2438 size
->cx
= window_size
.right
- window_size
.left
;
2439 size
->cy
= window_size
.bottom
- window_size
.top
;
2444 level
= context_gl
->c
.current_rt
.sub_resource_idx
% rt
->level_count
;
2445 size
->cx
= wined3d_texture_get_level_width(rt
, level
);
2446 size
->cy
= wined3d_texture_get_level_height(rt
, level
);
2449 void wined3d_context_gl_enable_clip_distances(struct wined3d_context_gl
*context_gl
, uint32_t enable_mask
)
2451 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2452 unsigned int clip_distance_count
, i
;
2453 uint32_t disable_mask
, current_mask
;
2455 clip_distance_count
= gl_info
->limits
.user_clip_distances
;
2456 disable_mask
= ~enable_mask
;
2457 enable_mask
&= wined3d_mask_from_size(clip_distance_count
);
2458 disable_mask
&= wined3d_mask_from_size(clip_distance_count
);
2459 current_mask
= context_gl
->c
.clip_distance_mask
;
2460 context_gl
->c
.clip_distance_mask
= enable_mask
;
2462 enable_mask
&= ~current_mask
;
2465 i
= wined3d_bit_scan(&enable_mask
);
2466 gl_info
->gl_ops
.gl
.p_glEnable(GL_CLIP_DISTANCE0
+ i
);
2468 disable_mask
&= current_mask
;
2469 while (disable_mask
)
2471 i
= wined3d_bit_scan(&disable_mask
);
2472 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_DISTANCE0
+ i
);
2474 checkGLcall("toggle clip distances");
2477 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2479 return rt_mask
& (1u << 31);
2482 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2484 return rt_mask
& ~(1u << 31);
2487 /* Context activation is done by the caller. */
2488 static void wined3d_context_gl_apply_draw_buffers(struct wined3d_context_gl
*context_gl
, uint32_t rt_mask
)
2490 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2491 GLenum draw_buffers
[WINED3D_MAX_RENDER_TARGETS
];
2495 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2497 else if (is_rt_mask_onscreen(rt_mask
))
2499 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2503 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2510 draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2512 draw_buffers
[i
] = GL_NONE
;
2518 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2520 GL_EXTCALL(glDrawBuffers(i
, draw_buffers
));
2524 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffers
[0]);
2529 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2533 checkGLcall("apply draw buffers");
2536 /* Context activation is done by the caller. */
2537 void wined3d_context_gl_active_texture(struct wined3d_context_gl
*context_gl
,
2538 const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2540 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2541 checkGLcall("glActiveTexture");
2542 context_gl
->active_texture
= unit
;
2545 void wined3d_context_gl_bind_bo(struct wined3d_context_gl
*context_gl
, GLenum binding
, GLuint name
)
2547 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2549 if (binding
== GL_ELEMENT_ARRAY_BUFFER
)
2550 context_invalidate_state(&context_gl
->c
, STATE_INDEXBUFFER
);
2552 GL_EXTCALL(glBindBuffer(binding
, name
));
2555 void wined3d_context_gl_bind_texture(struct wined3d_context_gl
*context_gl
, GLenum target
, GLuint name
)
2557 const struct wined3d_dummy_textures
*textures
= &wined3d_device_gl(context_gl
->c
.device
)->dummy_textures
;
2558 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2559 GLenum old_texture_type
;
2563 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2567 unit
= context_gl
->active_texture
;
2568 old_texture_type
= context_gl
->texture_type
[unit
];
2569 if (old_texture_type
!= target
)
2571 switch (old_texture_type
)
2577 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, textures
->tex_1d
);
2579 case GL_TEXTURE_1D_ARRAY
:
2580 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, textures
->tex_1d_array
);
2583 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, textures
->tex_2d
);
2585 case GL_TEXTURE_2D_ARRAY
:
2586 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, textures
->tex_2d_array
);
2588 case GL_TEXTURE_RECTANGLE_ARB
:
2589 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, textures
->tex_rect
);
2591 case GL_TEXTURE_CUBE_MAP
:
2592 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, textures
->tex_cube
);
2594 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2595 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, textures
->tex_cube_array
);
2598 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, textures
->tex_3d
);
2600 case GL_TEXTURE_BUFFER
:
2601 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, textures
->tex_buffer
);
2603 case GL_TEXTURE_2D_MULTISAMPLE
:
2604 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, textures
->tex_2d_ms
);
2606 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2607 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, textures
->tex_2d_ms_array
);
2610 ERR("Unexpected texture target %#x.\n", old_texture_type
);
2613 context_gl
->texture_type
[unit
] = target
;
2616 checkGLcall("bind texture");
2619 static void wined3d_context_gl_poll_fences(struct wined3d_context_gl
*context_gl
)
2621 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2622 struct wined3d_command_fence_gl
*f
;
2625 for (i
= 0; i
< context_gl
->submitted
.fence_count
; ++i
)
2627 f
= &context_gl
->submitted
.fences
[i
];
2629 if (f
->id
> device_gl
->completed_fence_id
)
2631 if (wined3d_fence_test(f
->fence
, &device_gl
->d
, 0) != WINED3D_FENCE_OK
)
2633 device_gl
->completed_fence_id
= f
->id
;
2636 wined3d_fence_destroy(f
->fence
);
2637 if (i
!= context_gl
->submitted
.fence_count
- 1)
2638 *f
= context_gl
->submitted
.fences
[context_gl
->submitted
.fence_count
- 1];
2639 --context_gl
->submitted
.fence_count
;
2643 static void wined3d_device_gl_free_memory(struct wined3d_device_gl
*device_gl
, struct wined3d_allocator_block
*block
)
2645 assert(block
->chunk
->allocator
== &device_gl
->allocator
);
2646 wined3d_device_gl_allocator_lock(device_gl
);
2647 wined3d_allocator_block_free(block
);
2648 wined3d_device_gl_allocator_unlock(device_gl
);
2651 static void wined3d_context_gl_cleanup_resources(struct wined3d_context_gl
*context_gl
)
2653 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2654 struct wined3d_retired_block_gl
*r
, *blocks
;
2655 SIZE_T count
, i
= 0;
2658 wined3d_context_gl_poll_fences(context_gl
);
2659 id
= device_gl
->completed_fence_id
;
2661 blocks
= device_gl
->retired_blocks
;
2662 count
= device_gl
->retired_block_count
;
2667 if (r
->fence_id
> id
)
2673 wined3d_device_gl_free_memory(device_gl
, r
->block
);
2677 device_gl
->retired_block_count
= count
;
2680 void wined3d_context_gl_wait_command_fence(struct wined3d_context_gl
*context_gl
, uint64_t id
)
2682 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2683 enum wined3d_fence_result ret
;
2686 if (id
<= device_gl
->completed_fence_id
2687 || id
> device_gl
->current_fence_id
) /* In case the fence ID wrapped. */
2690 for (i
= 0; i
< context_gl
->submitted
.fence_count
; ++i
)
2692 if (context_gl
->submitted
.fences
[i
].id
!= id
)
2695 if ((ret
= wined3d_fence_wait(context_gl
->submitted
.fences
[i
].fence
, &device_gl
->d
)) != WINED3D_FENCE_OK
)
2696 ERR("Failed to wait for command fence with id 0x%s, ret %#x.\n", wine_dbgstr_longlong(id
), ret
);
2697 wined3d_context_gl_cleanup_resources(context_gl
);
2701 ERR("Failed to find fence for command fence with id 0x%s.\n", wine_dbgstr_longlong(id
));
2704 void wined3d_context_gl_submit_command_fence(struct wined3d_context_gl
*context_gl
)
2706 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2707 struct wined3d_command_fence_gl
*f
;
2710 if (!wined3d_array_reserve((void **)&context_gl
->submitted
.fences
, &context_gl
->submitted
.fences_size
,
2711 context_gl
->submitted
.fence_count
+ 1, sizeof(*context_gl
->submitted
.fences
)))
2712 ERR("Failed to grow submitted command buffer array.\n");
2714 f
= &context_gl
->submitted
.fences
[context_gl
->submitted
.fence_count
++];
2715 f
->id
= device_gl
->current_fence_id
;
2716 if (FAILED(hr
= wined3d_fence_create(&device_gl
->d
, &f
->fence
)))
2717 ERR("Failed to create fence, hr %#lx.\n", hr
);
2718 wined3d_fence_issue(f
->fence
, &device_gl
->d
);
2720 /* We don't expect this to ever happen, but handle it anyway. */
2721 if (!++device_gl
->current_fence_id
)
2723 wined3d_context_gl_wait_command_fence(context_gl
, device_gl
->current_fence_id
- 1);
2724 device_gl
->completed_fence_id
= 0;
2725 device_gl
->current_fence_id
= 1;
2727 device_gl
->retired_bo_size
= 0;
2728 wined3d_context_gl_cleanup_resources(context_gl
);
2731 static void wined3d_context_gl_destroy_allocator_block(struct wined3d_context_gl
*context_gl
,
2732 struct wined3d_allocator_block
*block
, uint64_t fence_id
)
2734 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2735 struct wined3d_retired_block_gl
*r
;
2737 if (device_gl
->completed_fence_id
>= fence_id
)
2739 wined3d_device_gl_free_memory(device_gl
, block
);
2740 TRACE("Freed block %p.\n", block
);
2744 if (!wined3d_array_reserve((void **)&device_gl
->retired_blocks
,
2745 &device_gl
->retired_blocks_size
, device_gl
->retired_block_count
+ 1,
2746 sizeof(*device_gl
->retired_blocks
)))
2748 ERR("Leaking block %p.\n", block
);
2752 r
= &device_gl
->retired_blocks
[device_gl
->retired_block_count
++];
2754 r
->fence_id
= fence_id
;
2757 /* We always have buffer storage here. */
2758 GLuint
wined3d_context_gl_allocate_vram_chunk_buffer(struct wined3d_context_gl
*context_gl
,
2759 unsigned int pool
, size_t size
)
2761 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2765 TRACE("context_gl %p, pool %u, size %Iu.\n", context_gl
, pool
, size
);
2767 GL_EXTCALL(glGenBuffers(1, &id
));
2770 checkGLcall("buffer object creation");
2773 wined3d_context_gl_bind_bo(context_gl
, GL_PIXEL_UNPACK_BUFFER
, id
);
2775 flags
= wined3d_device_gl_get_memory_type_flags(pool
) | GL_DYNAMIC_STORAGE_BIT
;
2776 if (flags
& (GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
))
2777 flags
|= GL_MAP_PERSISTENT_BIT
| GL_MAP_COHERENT_BIT
;
2778 GL_EXTCALL(glBufferStorage(GL_PIXEL_UNPACK_BUFFER
, size
, NULL
, flags
));
2780 checkGLcall("buffer object creation");
2782 TRACE("Created buffer object %u.\n", id
);
2787 static void *wined3d_allocator_chunk_gl_map(struct wined3d_allocator_chunk_gl
*chunk_gl
,
2788 struct wined3d_context_gl
*context_gl
)
2790 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2793 TRACE("chunk %p, gl_buffer %u, map_ptr %p.\n", chunk_gl
, chunk_gl
->gl_buffer
, chunk_gl
->c
.map_ptr
);
2795 wined3d_allocator_chunk_gl_lock(chunk_gl
);
2797 if (!chunk_gl
->c
.map_ptr
)
2799 unsigned int flags
= wined3d_device_gl_get_memory_type_flags(chunk_gl
->memory_type
) & ~GL_CLIENT_STORAGE_BIT
;
2801 flags
|= GL_MAP_PERSISTENT_BIT
| GL_MAP_COHERENT_BIT
;
2802 if (!(flags
& GL_MAP_READ_BIT
))
2803 flags
|= GL_MAP_UNSYNCHRONIZED_BIT
;
2804 if (flags
& GL_MAP_WRITE_BIT
)
2805 flags
|= GL_MAP_FLUSH_EXPLICIT_BIT
;
2806 wined3d_context_gl_bind_bo(context_gl
, GL_PIXEL_UNPACK_BUFFER
, chunk_gl
->gl_buffer
);
2807 chunk_gl
->c
.map_ptr
= GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER
,
2808 0, WINED3D_ALLOCATOR_CHUNK_SIZE
, flags
));
2809 if (!chunk_gl
->c
.map_ptr
)
2811 wined3d_allocator_chunk_gl_unlock(chunk_gl
);
2812 ERR("Failed to map chunk memory.\n");
2816 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, WINED3D_ALLOCATOR_CHUNK_SIZE
);
2819 ++chunk_gl
->c
.map_count
;
2820 map_ptr
= chunk_gl
->c
.map_ptr
;
2822 wined3d_allocator_chunk_gl_unlock(chunk_gl
);
2827 static void wined3d_allocator_chunk_gl_unmap(struct wined3d_allocator_chunk_gl
*chunk_gl
,
2828 struct wined3d_context_gl
*context_gl
)
2830 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2832 TRACE("chunk_gl %p, context_gl %p.\n", chunk_gl
, context_gl
);
2834 wined3d_allocator_chunk_gl_lock(chunk_gl
);
2836 if (!--chunk_gl
->c
.map_count
)
2838 wined3d_context_gl_bind_bo(context_gl
, GL_PIXEL_UNPACK_BUFFER
, chunk_gl
->gl_buffer
);
2839 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER
));
2840 chunk_gl
->c
.map_ptr
= NULL
;
2842 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, -WINED3D_ALLOCATOR_CHUNK_SIZE
);
2845 wined3d_allocator_chunk_gl_unlock(chunk_gl
);
2848 static void *wined3d_bo_gl_map(struct wined3d_bo_gl
*bo
, struct wined3d_context_gl
*context_gl
, uint32_t flags
)
2850 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
2851 const struct wined3d_gl_info
*gl_info
;
2852 struct wined3d_bo_user
*bo_user
;
2853 struct wined3d_bo_gl tmp
;
2855 if (flags
& WINED3D_MAP_NOOVERWRITE
)
2858 if ((flags
& WINED3D_MAP_DISCARD
) && bo
->command_fence_id
> device_gl
->completed_fence_id
)
2860 if (wined3d_device_gl_create_bo(device_gl
, context_gl
, bo
->size
,
2861 bo
->binding
, bo
->usage
, bo
->b
.coherent
, bo
->flags
, &tmp
))
2863 LIST_FOR_EACH_ENTRY(bo_user
, &bo
->b
.users
, struct wined3d_bo_user
, entry
)
2864 bo_user
->valid
= false;
2865 list_init(&bo
->b
.users
);
2867 wined3d_context_gl_destroy_bo(context_gl
, bo
);
2869 list_init(&bo
->b
.users
);
2874 ERR("Failed to create new buffer object.\n");
2877 if (context_gl
->c
.d3d_info
->fences
)
2879 if (bo
->command_fence_id
== device_gl
->current_fence_id
)
2880 wined3d_context_gl_submit_command_fence(context_gl
);
2881 wined3d_context_gl_wait_command_fence(context_gl
, bo
->command_fence_id
);
2886 return bo
->b
.map_ptr
;
2890 struct wined3d_allocator_chunk_gl
*chunk_gl
= wined3d_allocator_chunk_gl(bo
->memory
->chunk
);
2892 if (!(bo
->b
.map_ptr
= wined3d_allocator_chunk_gl_map(chunk_gl
, context_gl
)))
2893 ERR("Failed to map chunk.\n");
2894 return bo
->b
.map_ptr
;
2897 gl_info
= context_gl
->gl_info
;
2898 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
2900 if (gl_info
->supported
[ARB_BUFFER_STORAGE
])
2902 GLbitfield gl_flags
;
2904 /* When mapping the bo persistently, we need to use the access flags
2905 * used to create the bo, instead of the access flags passed to the
2906 * map call. Otherwise, if for example the initial map call that
2907 * caused the bo to be persistently mapped was a read-only map,
2908 * subsequent write access to the bo would be undefined.
2910 * Note that we use GL_MAP_PERSISTENT_BIT for non-persistent maps here
2911 * as well, in order to allow draws to succeed while referenced buffer
2912 * resources are mapped. On the other hand, we don't want to use the
2913 * access flags used to create the bo for non-persistent maps, because
2914 * that may imply dropping GL_MAP_UNSYNCHRONIZED_BIT. */
2915 gl_flags
= bo
->flags
& ~GL_CLIENT_STORAGE_BIT
;
2916 if (!(gl_flags
& GL_MAP_READ_BIT
))
2917 gl_flags
|= GL_MAP_UNSYNCHRONIZED_BIT
;
2918 if (gl_flags
& GL_MAP_WRITE_BIT
)
2919 gl_flags
|= GL_MAP_FLUSH_EXPLICIT_BIT
;
2920 gl_flags
|= GL_MAP_PERSISTENT_BIT
| GL_MAP_COHERENT_BIT
;
2922 bo
->b
.map_ptr
= GL_EXTCALL(glMapBufferRange(bo
->binding
, 0, bo
->size
, gl_flags
));
2924 else if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
2926 bo
->b
.map_ptr
= GL_EXTCALL(glMapBufferRange(bo
->binding
, 0, bo
->size
, wined3d_resource_gl_map_flags(bo
, flags
)));
2930 bo
->b
.map_ptr
= GL_EXTCALL(glMapBuffer(bo
->binding
, wined3d_resource_gl_legacy_map_flags(flags
)));
2934 adapter_adjust_mapped_memory(device_gl
->d
.adapter
, bo
->size
);
2936 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, 0);
2937 checkGLcall("Map buffer object");
2939 return bo
->b
.map_ptr
;
2942 static void wined3d_bo_gl_unmap(struct wined3d_bo_gl
*bo
, struct wined3d_context_gl
*context_gl
)
2944 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
2946 if (context_gl
->c
.d3d_info
->persistent_map
2947 && context_gl
->c
.device
->adapter
->mapped_size
<= MAX_PERSISTENT_MAPPED_BYTES
)
2949 TRACE("Not unmapping BO %p.\n", bo
);
2953 wined3d_device_bo_map_lock(context_gl
->c
.device
);
2954 /* The mapping is still in use by the client (viz. for an accelerated
2955 * NOOVERWRITE map). The client will trigger another unmap request when the
2956 * d3d application requests to unmap the BO. */
2957 if (bo
->b
.client_map_count
)
2959 wined3d_device_bo_map_unlock(context_gl
->c
.device
);
2960 assert(context_gl
->c
.d3d_info
->persistent_map
);
2961 TRACE("BO %p is still in use by a client thread; not unmapping.\n", bo
);
2964 bo
->b
.map_ptr
= NULL
;
2965 wined3d_device_bo_map_unlock(context_gl
->c
.device
);
2969 struct wined3d_allocator_chunk_gl
*chunk_gl
= wined3d_allocator_chunk_gl(bo
->memory
->chunk
);
2971 wined3d_allocator_chunk_gl_unmap(chunk_gl
, context_gl
);
2975 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
2976 GL_EXTCALL(glUnmapBuffer(bo
->binding
));
2977 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, 0);
2978 checkGLcall("Unmap buffer object");
2980 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, -bo
->size
);
2983 void *wined3d_context_gl_map_bo_address(struct wined3d_context_gl
*context_gl
,
2984 const struct wined3d_bo_address
*data
, size_t size
, uint32_t flags
)
2986 struct wined3d_bo
*bo
;
2989 if (!(bo
= data
->buffer_object
))
2992 if (!(map_ptr
= wined3d_bo_gl_map(wined3d_bo_gl(bo
), context_gl
, flags
)))
2994 ERR("Failed to map bo.\n");
2998 return (uint8_t *)map_ptr
+ bo
->buffer_offset
+ (uintptr_t)data
->addr
;
3001 static void flush_bo_ranges(struct wined3d_context_gl
*context_gl
, const struct wined3d_const_bo_address
*data
,
3002 unsigned int range_count
, const struct wined3d_range
*ranges
)
3004 const struct wined3d_gl_info
*gl_info
;
3005 struct wined3d_bo_gl
*bo
;
3008 if (!data
->buffer_object
|| data
->buffer_object
->coherent
)
3010 bo
= wined3d_bo_gl(data
->buffer_object
);
3012 gl_info
= context_gl
->gl_info
;
3013 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
3015 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
3017 /* The offset passed to glFlushMappedBufferRange() is relative to the
3018 * mapped range, but we map the whole buffer anyway. */
3019 for (i
= 0; i
< range_count
; ++i
)
3021 GL_EXTCALL(glFlushMappedBufferRange(bo
->binding
,
3022 bo
->b
.buffer_offset
+ (uintptr_t)data
->addr
+ ranges
[i
].offset
, ranges
[i
].size
));
3025 else if (gl_info
->supported
[APPLE_FLUSH_BUFFER_RANGE
])
3027 for (i
= 0; i
< range_count
; ++i
)
3029 GL_EXTCALL(glFlushMappedBufferRangeAPPLE(bo
->binding
,
3030 bo
->b
.buffer_offset
+ (uintptr_t)data
->addr
+ ranges
[i
].offset
, ranges
[i
].size
));
3031 checkGLcall("glFlushMappedBufferRangeAPPLE");
3035 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, 0);
3036 checkGLcall("Flush buffer object");
3039 void wined3d_context_gl_unmap_bo_address(struct wined3d_context_gl
*context_gl
,
3040 const struct wined3d_bo_address
*data
, unsigned int range_count
, const struct wined3d_range
*ranges
)
3042 struct wined3d_bo_gl
*bo
;
3044 if (!data
->buffer_object
)
3046 bo
= wined3d_bo_gl(data
->buffer_object
);
3048 assert(bo
->b
.map_ptr
);
3050 flush_bo_ranges(context_gl
, wined3d_const_bo_address(data
), range_count
, ranges
);
3051 wined3d_bo_gl_unmap(bo
, context_gl
);
3054 void wined3d_context_gl_flush_bo_address(struct wined3d_context_gl
*context_gl
,
3055 const struct wined3d_const_bo_address
*data
, size_t size
)
3057 struct wined3d_range range
;
3059 TRACE("context_gl %p, data %s, size %Iu.\n", context_gl
, debug_const_bo_address(data
), size
);
3061 range
.offset
= (uintptr_t)data
->addr
;
3064 flush_bo_ranges(context_gl
, data
, 1, &range
);
3067 void wined3d_context_gl_copy_bo_address(struct wined3d_context_gl
*context_gl
,
3068 const struct wined3d_bo_address
*dst
, const struct wined3d_bo_address
*src
,
3069 unsigned int range_count
, const struct wined3d_range
*ranges
, uint32_t map_flags
)
3071 const struct wined3d_gl_info
*gl_info
;
3072 struct wined3d_bo_gl
*src_bo
, *dst_bo
;
3073 BYTE
*dst_ptr
, *src_ptr
;
3076 gl_info
= context_gl
->gl_info
;
3077 src_bo
= src
->buffer_object
? wined3d_bo_gl(src
->buffer_object
) : NULL
;
3078 dst_bo
= dst
->buffer_object
? wined3d_bo_gl(dst
->buffer_object
) : NULL
;
3080 if (dst_bo
&& src_bo
)
3082 if (gl_info
->supported
[ARB_COPY_BUFFER
])
3084 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER
, src_bo
->id
));
3085 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER
, dst_bo
->id
));
3087 for (i
= 0; i
< range_count
; ++i
)
3088 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
,
3089 src_bo
->b
.buffer_offset
+ (GLintptr
)src
->addr
+ ranges
[i
].offset
,
3090 dst_bo
->b
.buffer_offset
+ (GLintptr
)dst
->addr
+ ranges
[i
].offset
, ranges
[i
].size
));
3091 checkGLcall("direct buffer copy");
3093 wined3d_context_gl_reference_bo(context_gl
, src_bo
);
3094 wined3d_context_gl_reference_bo(context_gl
, dst_bo
);
3098 src_ptr
= wined3d_context_gl_map_bo_address(context_gl
, src
,
3099 src_bo
->size
- (uintptr_t)src
->addr
, WINED3D_MAP_READ
);
3100 dst_ptr
= wined3d_context_gl_map_bo_address(context_gl
, dst
,
3101 dst_bo
->size
- (uintptr_t)dst
->addr
, map_flags
);
3103 for (i
= 0; i
< range_count
; ++i
)
3104 memcpy(dst_ptr
+ ranges
[i
].offset
, src_ptr
+ ranges
[i
].offset
, ranges
[i
].size
);
3106 wined3d_context_gl_unmap_bo_address(context_gl
, dst
, range_count
, ranges
);
3107 wined3d_context_gl_unmap_bo_address(context_gl
, src
, 0, NULL
);
3110 else if (!dst_bo
&& src_bo
)
3112 wined3d_context_gl_bind_bo(context_gl
, src_bo
->binding
, src_bo
->id
);
3113 for (i
= 0; i
< range_count
; ++i
)
3114 GL_EXTCALL(glGetBufferSubData(src_bo
->binding
,
3115 src_bo
->b
.buffer_offset
+ (GLintptr
)src
->addr
+ ranges
[i
].offset
,
3116 ranges
[i
].size
, dst
->addr
+ ranges
[i
].offset
));
3117 checkGLcall("buffer download");
3119 wined3d_context_gl_reference_bo(context_gl
, src_bo
);
3121 else if (dst_bo
&& !src_bo
)
3123 if ((map_flags
& WINED3D_MAP_DISCARD
) && (dst_bo
->flags
& GL_MAP_WRITE_BIT
))
3125 dst_ptr
= wined3d_context_gl_map_bo_address(context_gl
, dst
, dst_bo
->size
, map_flags
);
3126 memcpy(dst_ptr
, src
->addr
, dst_bo
->size
);
3127 wined3d_context_gl_unmap_bo_address(context_gl
, dst
, range_count
, ranges
);
3131 wined3d_context_gl_bind_bo(context_gl
, dst_bo
->binding
, dst_bo
->id
);
3132 for (i
= 0; i
< range_count
; ++i
)
3133 GL_EXTCALL(glBufferSubData(dst_bo
->binding
,
3134 dst_bo
->b
.buffer_offset
+ (GLintptr
)dst
->addr
+ ranges
[i
].offset
,
3135 ranges
[i
].size
, src
->addr
+ ranges
[i
].offset
));
3136 checkGLcall("buffer upload");
3138 wined3d_context_gl_reference_bo(context_gl
, dst_bo
);
3143 for (i
= 0; i
< range_count
; ++i
)
3144 memcpy(dst
->addr
+ ranges
[i
].offset
, src
->addr
+ ranges
[i
].offset
, ranges
[i
].size
);
3148 void wined3d_context_gl_destroy_bo(struct wined3d_context_gl
*context_gl
, struct wined3d_bo_gl
*bo
)
3150 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(context_gl
->c
.device
);
3151 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3153 TRACE("context_gl %p, bo %p.\n", context_gl
, bo
);
3155 assert(list_empty(&bo
->b
.users
));
3159 unsigned int order
= bo
->memory
->order
;
3162 wined3d_allocator_chunk_gl_unmap(wined3d_allocator_chunk_gl(bo
->memory
->chunk
), context_gl
);
3163 wined3d_context_gl_destroy_allocator_block(context_gl
, bo
->memory
, bo
->command_fence_id
);
3165 if (bo
->command_fence_id
== device_gl
->current_fence_id
)
3167 device_gl
->retired_bo_size
+= WINED3D_ALLOCATOR_CHUNK_SIZE
>> order
;
3168 if (device_gl
->retired_bo_size
> WINED3D_RETIRED_BO_SIZE_THRESHOLD
)
3169 wined3d_context_gl_submit_command_fence(context_gl
);
3178 wined3d_context_gl_bind_bo(context_gl
, bo
->binding
, bo
->id
);
3179 GL_EXTCALL(glUnmapBuffer(bo
->binding
));
3180 adapter_adjust_mapped_memory(context_gl
->c
.device
->adapter
, -bo
->size
);
3183 TRACE("Destroying GL buffer %u.\n", bo
->id
);
3185 GL_EXTCALL(glDeleteBuffers(1, &bo
->id
));
3186 checkGLcall("buffer object destruction");
3190 static void wined3d_context_gl_set_render_offscreen(struct wined3d_context_gl
*context_gl
, BOOL offscreen
)
3192 if (context_gl
->c
.render_offscreen
== offscreen
)
3195 context_invalidate_state(&context_gl
->c
, STATE_VIEWPORT
);
3196 context_invalidate_state(&context_gl
->c
, STATE_SCISSORRECT
);
3197 if (!context_gl
->gl_info
->supported
[ARB_CLIP_CONTROL
])
3199 context_invalidate_state(&context_gl
->c
, STATE_RASTERIZER
);
3200 context_invalidate_state(&context_gl
->c
, STATE_POINTSPRITECOORDORIGIN
);
3201 context_invalidate_state(&context_gl
->c
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
3203 context_invalidate_state(&context_gl
->c
, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN
));
3204 if (context_gl
->gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
3205 context_invalidate_state(&context_gl
->c
, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
));
3206 context_gl
->c
.render_offscreen
= offscreen
;
3209 GLenum
wined3d_context_gl_get_offscreen_gl_buffer(const struct wined3d_context_gl
*context_gl
)
3211 switch (wined3d_settings
.offscreen_rendering_mode
)
3214 return GL_COLOR_ATTACHMENT0
;
3216 case ORM_BACKBUFFER
:
3217 return context_gl
->aux_buffers
> 0 ? GL_AUX0
: GL_BACK
;
3220 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
3225 static uint32_t wined3d_context_gl_generate_rt_mask_no_fbo(const struct wined3d_context_gl
*context_gl
,
3226 struct wined3d_resource
*rt
)
3228 if (!rt
|| rt
->format
->id
== WINED3DFMT_NULL
)
3230 else if (rt
->type
!= WINED3D_RTYPE_BUFFER
&& texture_from_resource(rt
)->swapchain
)
3231 return context_generate_rt_mask_from_resource(rt
);
3233 return context_generate_rt_mask(wined3d_context_gl_get_offscreen_gl_buffer(context_gl
));
3236 /* Context activation is done by the caller. */
3237 void wined3d_context_gl_apply_blit_state(struct wined3d_context_gl
*context_gl
, const struct wined3d_device
*device
)
3239 struct wined3d_context
*context
= &context_gl
->c
;
3240 const struct wined3d_gl_info
*gl_info
;
3241 unsigned int sampler
;
3244 TRACE("Setting up context %p for blitting.\n", context
);
3246 gl_info
= context_gl
->gl_info
;
3248 wined3d_context_gl_get_rt_size(context_gl
, &rt_size
);
3250 if (context
->last_was_blit
)
3252 if (context_gl
->blit_size
.cx
!= rt_size
.cx
|| context_gl
->blit_size
.cy
!= rt_size
.cy
)
3254 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, rt_size
.cx
, rt_size
.cy
);
3255 context
->viewport_count
= WINED3D_MAX_VIEWPORTS
;
3256 context_gl
->blit_size
= rt_size
;
3257 /* No need to dirtify here, the states are still dirtified because
3258 * they weren't applied since the last context_apply_blit_state()
3261 checkGLcall("blit state application");
3262 TRACE("Context is already set up for blitting, nothing to do.\n");
3265 context
->last_was_blit
= TRUE
;
3267 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
3268 GL_EXTCALL(glBindSampler(0, 0));
3269 wined3d_context_gl_active_texture(context_gl
, gl_info
, 0);
3271 sampler
= context_gl
->rev_tex_unit_map
[0];
3272 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
3274 if (sampler
< WINED3D_MAX_FFP_TEXTURES
)
3276 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
3277 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
3279 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
3281 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
3282 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
3284 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
3286 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
3287 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
3289 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
3290 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
3291 context_invalidate_state(context
, STATE_BLEND
);
3292 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
3293 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
3294 context_invalidate_state(context
, STATE_RASTERIZER
);
3295 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
3296 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
3297 context_invalidate_state(context
, STATE_DEPTH_STENCIL
);
3298 if (gl_info
->supported
[ARB_POINT_SPRITE
])
3300 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
3301 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
3303 if (gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
3305 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
3306 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3309 context
->last_was_rhw
= TRUE
;
3310 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
3312 wined3d_context_gl_enable_clip_distances(context_gl
, 0);
3313 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
3315 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
3316 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
3317 GL_EXTCALL(glClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
));
3318 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, rt_size
.cx
, rt_size
.cy
);
3319 context
->viewport_count
= WINED3D_MAX_VIEWPORTS
;
3320 context_invalidate_state(context
, STATE_VIEWPORT
);
3322 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
3324 context_gl
->blit_size
= rt_size
;
3326 checkGLcall("blit state application");
3329 static void wined3d_context_gl_apply_blit_projection(const struct wined3d_context_gl
*context_gl
,
3330 unsigned int w
, unsigned int h
)
3332 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3333 const GLdouble projection
[] =
3335 2.0 / w
, 0.0, 0.0, 0.0,
3336 0.0, 2.0 / h
, 0.0, 0.0,
3338 -1.0, -1.0, -1.0, 1.0,
3341 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
3342 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
3345 /* Setup OpenGL states for fixed-function blitting. */
3346 /* Context activation is done by the caller. */
3347 void wined3d_context_gl_apply_ffp_blit_state(struct wined3d_context_gl
*context_gl
,
3348 const struct wined3d_device
*device
)
3350 struct wined3d_context
*context
= &context_gl
->c
;
3351 const struct wined3d_gl_info
*gl_info
;
3352 unsigned int i
, sampler
;
3354 gl_info
= context_gl
->gl_info
;
3355 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
3356 ERR("Applying fixed-function state without legacy context support.\n");
3358 if (context
->last_was_ffp_blit
)
3362 wined3d_context_gl_get_rt_size(context_gl
, &rt_size
);
3363 if (context_gl
->blit_size
.cx
!= rt_size
.cx
|| context_gl
->blit_size
.cy
!= rt_size
.cy
)
3364 wined3d_context_gl_apply_blit_projection(context_gl
, rt_size
.cx
, rt_size
.cy
);
3365 wined3d_context_gl_apply_blit_state(context_gl
, device
);
3367 checkGLcall("ffp blit state application");
3370 context
->last_was_ffp_blit
= TRUE
;
3372 wined3d_context_gl_apply_blit_state(context_gl
, device
);
3374 /* Disable all textures. The caller can then bind a texture it wants to blit
3376 for (i
= gl_info
->limits
.ffp_textures
- 1; i
> 0 ; --i
)
3378 wined3d_context_gl_active_texture(context_gl
, gl_info
, i
);
3380 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
3381 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
3382 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
3383 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
3384 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
3385 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
3387 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
3389 sampler
= context_gl
->rev_tex_unit_map
[i
];
3390 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
3392 if (sampler
< WINED3D_MAX_FFP_TEXTURES
)
3393 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
3394 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
3398 wined3d_context_gl_active_texture(context_gl
, gl_info
, 0);
3400 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
3401 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
3402 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
3403 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
3404 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
3405 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
3407 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
3408 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
3409 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
3411 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
3412 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
3414 /* Setup transforms. */
3415 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
3416 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
3417 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
3418 wined3d_context_gl_apply_blit_projection(context_gl
, context_gl
->blit_size
.cx
, context_gl
->blit_size
.cy
);
3419 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
3421 /* Other misc states. */
3422 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
3423 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
3424 gl_info
->p_glDisableWINE(GL_FOG
);
3425 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
3427 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
3429 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
3430 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
3432 checkGLcall("ffp blit state application");
3435 static BOOL
have_framebuffer_attachment(unsigned int rt_count
, struct wined3d_rendertarget_view
* const *rts
,
3436 const struct wined3d_rendertarget_view
*ds
)
3443 for (i
= 0; i
< rt_count
; ++i
)
3445 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3452 /* Context activation is done by the caller. */
3453 BOOL
wined3d_context_gl_apply_clear_state(struct wined3d_context_gl
*context_gl
,
3454 const struct wined3d_state
*state
, unsigned int rt_count
, const struct wined3d_fb_state
*fb
)
3456 struct wined3d_rendertarget_view
* const *rts
= fb
->render_targets
;
3457 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3458 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
3459 uint32_t rt_mask
= 0, *cur_mask
;
3462 if (isStateDirty(&context_gl
->c
, STATE_FRAMEBUFFER
) || fb
!= &state
->fb
3463 || rt_count
!= gl_info
->limits
.buffers
)
3465 if (!have_framebuffer_attachment(rt_count
, rts
, dsv
))
3467 WARN("Invalid render target config, need at least one attachment.\n");
3471 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3473 struct wined3d_rendertarget_info ds_info
= {{0}};
3475 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
3477 memset(context_gl
->blit_targets
, 0, sizeof(context_gl
->blit_targets
));
3478 for (i
= 0; i
< rt_count
; ++i
)
3482 struct wined3d_rendertarget_view_gl
*rtv_gl
= wined3d_rendertarget_view_gl(rts
[i
]);
3483 context_gl
->blit_targets
[i
].gl_view
= rtv_gl
->gl_view
;
3484 context_gl
->blit_targets
[i
].resource
= rtv_gl
->v
.resource
;
3485 context_gl
->blit_targets
[i
].sub_resource_idx
= rtv_gl
->v
.sub_resource_idx
;
3486 context_gl
->blit_targets
[i
].layer_count
= rtv_gl
->v
.layer_count
;
3488 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3489 rt_mask
|= (1u << i
);
3494 struct wined3d_rendertarget_view_gl
*dsv_gl
= wined3d_rendertarget_view_gl(dsv
);
3495 ds_info
.gl_view
= dsv_gl
->gl_view
;
3496 ds_info
.resource
= dsv_gl
->v
.resource
;
3497 ds_info
.sub_resource_idx
= dsv_gl
->v
.sub_resource_idx
;
3498 ds_info
.layer_count
= dsv_gl
->v
.layer_count
;
3501 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, context_gl
->blit_targets
, &ds_info
,
3502 rt_count
? rts
[0]->resource
->draw_binding
: 0, dsv
? dsv
->resource
->draw_binding
: 0);
3506 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, NULL
, &ds_info
,
3507 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
3508 rt_mask
= context_generate_rt_mask_from_resource(rts
[0]->resource
);
3511 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3512 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3513 * state management allows this */
3514 context_invalidate_state(&context_gl
->c
, STATE_FRAMEBUFFER
);
3518 rt_mask
= wined3d_context_gl_generate_rt_mask_no_fbo(context_gl
, rt_count
? rts
[0]->resource
: NULL
);
3521 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
3522 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
3524 for (i
= 0; i
< rt_count
; ++i
)
3526 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3527 rt_mask
|= (1u << i
);
3532 rt_mask
= wined3d_context_gl_generate_rt_mask_no_fbo(context_gl
, rt_count
? rts
[0]->resource
: NULL
);
3535 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3537 if (rt_mask
!= *cur_mask
)
3539 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3540 *cur_mask
= rt_mask
;
3541 context_invalidate_state(&context_gl
->c
, STATE_FRAMEBUFFER
);
3544 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3545 wined3d_context_gl_check_fbo_status(context_gl
, GL_FRAMEBUFFER
);
3547 context_gl
->c
.last_was_blit
= FALSE
;
3548 context_gl
->c
.last_was_ffp_blit
= FALSE
;
3550 /* Blending and clearing should be orthogonal, but tests on the nvidia
3551 * driver show that disabling blending when clearing improves the clearing
3552 * performance incredibly. */
3553 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
3554 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
3555 if (rt_count
&& gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
3557 if (needs_srgb_write(context_gl
->c
.d3d_info
, state
, fb
))
3558 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
3560 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
3561 context_invalidate_state(&context_gl
->c
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3563 checkGLcall("setting up state for clear");
3565 context_invalidate_state(&context_gl
->c
, STATE_BLEND
);
3566 context_invalidate_state(&context_gl
->c
, STATE_RASTERIZER
);
3567 context_invalidate_state(&context_gl
->c
, STATE_SCISSORRECT
);
3572 static uint32_t find_draw_buffers_mask(const struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
)
3574 struct wined3d_rendertarget_view
* const *rts
= state
->fb
.render_targets
;
3575 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
3576 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3577 unsigned int rt_mask
, mask
;
3580 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
3581 return wined3d_context_gl_generate_rt_mask_no_fbo(context_gl
, rts
[0]->resource
);
3582 else if (!context_gl
->c
.render_offscreen
)
3583 return context_generate_rt_mask_from_resource(rts
[0]->resource
);
3585 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
3586 rt_mask
&= wined3d_mask_from_size(gl_info
->limits
.buffers
);
3587 if (state
->blend_state
&& state
->blend_state
->dual_source
)
3593 i
= wined3d_bit_scan(&mask
);
3594 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
3595 rt_mask
&= ~(1u << i
);
3601 void context_gl_apply_texture_draw_state(struct wined3d_context_gl
*context_gl
,
3602 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, unsigned int location
)
3604 const struct wined3d_format
*format
= texture
->resource
.format
;
3605 struct wined3d_context
*context
= &context_gl
->c
;
3606 uint32_t rt_mask
, *cur_mask
;
3608 TRACE("context_gl %p, texture %p, sub_resource_idx %u, location %s.\n",
3609 context_gl
, texture
, sub_resource_idx
, wined3d_debug_location(location
));
3611 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3613 if (format
->depth_size
|| format
->stencil_size
)
3614 wined3d_context_gl_apply_fbo_state_explicit(context_gl
, GL_DRAW_FRAMEBUFFER
, NULL
,
3615 0, &texture
->resource
, sub_resource_idx
, location
);
3617 wined3d_context_gl_apply_fbo_state_explicit(context_gl
, GL_DRAW_FRAMEBUFFER
, &texture
->resource
,
3618 sub_resource_idx
, NULL
, 0, location
);
3621 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3623 if (location
!= WINED3D_LOCATION_DRAWABLE
)
3625 if (texture
->resource
.format
->id
== WINED3DFMT_NULL
|| format
->depth_size
|| format
->stencil_size
)
3632 rt_mask
= context_generate_rt_mask_from_resource(&texture
->resource
);
3637 rt_mask
= wined3d_context_gl_generate_rt_mask_no_fbo(context_gl
, &texture
->resource
);
3640 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3641 if (rt_mask
!= *cur_mask
)
3643 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3644 *cur_mask
= rt_mask
;
3647 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3648 wined3d_context_gl_check_fbo_status(context_gl
, GL_DRAW_FRAMEBUFFER
);
3650 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
3653 /* Context activation is done by the caller. */
3654 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3656 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
3657 uint32_t rt_mask
= find_draw_buffers_mask(context_gl
, state
);
3658 const struct wined3d_fb_state
*fb
= &state
->fb
;
3659 DWORD color_location
= 0;
3662 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3664 struct wined3d_rendertarget_info ds_info
= {{0}};
3666 if (!context
->render_offscreen
)
3668 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, NULL
, &ds_info
,
3669 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
3673 const struct wined3d_rendertarget_view_gl
*view_gl
;
3676 memset(context_gl
->blit_targets
, 0, sizeof(context_gl
->blit_targets
));
3677 for (i
= 0; i
< context_gl
->gl_info
->limits
.buffers
; ++i
)
3679 if (!fb
->render_targets
[i
])
3682 view_gl
= wined3d_rendertarget_view_gl(fb
->render_targets
[i
]);
3683 context_gl
->blit_targets
[i
].gl_view
= view_gl
->gl_view
;
3684 context_gl
->blit_targets
[i
].resource
= view_gl
->v
.resource
;
3685 context_gl
->blit_targets
[i
].sub_resource_idx
= view_gl
->v
.sub_resource_idx
;
3686 context_gl
->blit_targets
[i
].layer_count
= view_gl
->v
.layer_count
;
3688 if (!color_location
)
3689 color_location
= view_gl
->v
.resource
->draw_binding
;
3692 if (fb
->depth_stencil
)
3694 view_gl
= wined3d_rendertarget_view_gl(fb
->depth_stencil
);
3695 ds_info
.gl_view
= view_gl
->gl_view
;
3696 ds_info
.resource
= view_gl
->v
.resource
;
3697 ds_info
.sub_resource_idx
= view_gl
->v
.sub_resource_idx
;
3698 ds_info
.layer_count
= view_gl
->v
.layer_count
;
3701 wined3d_context_gl_apply_fbo_state(context_gl
, GL_FRAMEBUFFER
, context_gl
->blit_targets
, &ds_info
,
3702 color_location
, fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
3706 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3707 if (rt_mask
!= *cur_mask
)
3709 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3710 *cur_mask
= rt_mask
;
3712 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
3715 static void wined3d_context_gl_map_stage(struct wined3d_context_gl
*context_gl
, unsigned int stage
, unsigned int unit
)
3717 unsigned int i
= context_gl
->rev_tex_unit_map
[unit
];
3718 unsigned int j
= context_gl
->tex_unit_map
[stage
];
3720 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
3721 context_gl
->tex_unit_map
[stage
] = unit
;
3722 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
3723 context_gl
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
3725 context_gl
->rev_tex_unit_map
[unit
] = stage
;
3726 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
3727 context_gl
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
3730 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
3734 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
3735 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
3738 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
3739 const struct wined3d_state
*state
)
3743 context
->fixed_function_usage_map
= 0;
3744 for (i
= 0; i
< WINED3D_MAX_FFP_TEXTURES
; ++i
)
3746 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
3747 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
3748 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
3749 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
3750 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
3751 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
3752 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
3753 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
3755 /* Not used, and disable higher stages. */
3756 if (color_op
== WINED3D_TOP_DISABLE
)
3759 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
3760 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
3761 || ((color_arg3
== WINED3DTA_TEXTURE
)
3762 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
3763 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
3764 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
3765 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
3766 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
3767 context
->fixed_function_usage_map
|= (1u << i
);
3769 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
3770 && i
< WINED3D_MAX_FFP_TEXTURES
- 1)
3771 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
3774 if (i
< context
->lowest_disabled_stage
)
3777 end
= context
->lowest_disabled_stage
;
3781 start
= context
->lowest_disabled_stage
;
3785 context
->lowest_disabled_stage
= i
;
3786 for (i
= start
+ 1; i
< end
; ++i
)
3788 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3792 static void wined3d_context_gl_map_fixed_function_samplers(struct wined3d_context_gl
*context_gl
,
3793 const struct wined3d_state
*state
)
3795 const struct wined3d_d3d_info
*d3d_info
= context_gl
->c
.d3d_info
;
3796 unsigned int i
, tex
;
3799 ffu_map
= context_gl
->c
.fixed_function_usage_map
;
3801 if (d3d_info
->ffp_fragment_caps
.max_textures
== d3d_info
->ffp_fragment_caps
.max_blend_stages
3802 || context_gl
->c
.lowest_disabled_stage
<= d3d_info
->ffp_fragment_caps
.max_textures
)
3806 i
= wined3d_bit_scan(&ffu_map
);
3807 if (context_gl
->tex_unit_map
[i
] != i
)
3809 wined3d_context_gl_map_stage(context_gl
, i
, i
);
3810 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(i
));
3811 context_invalidate_texture_stage(&context_gl
->c
, i
);
3817 /* Now work out the mapping */
3821 i
= wined3d_bit_scan(&ffu_map
);
3822 if (context_gl
->tex_unit_map
[i
] != tex
)
3824 wined3d_context_gl_map_stage(context_gl
, i
, tex
);
3825 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(i
));
3826 context_invalidate_texture_stage(&context_gl
->c
, i
);
3833 static void wined3d_context_gl_map_psamplers(struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
)
3835 const struct wined3d_d3d_info
*d3d_info
= context_gl
->c
.d3d_info
;
3836 const struct wined3d_shader_resource_info
*resource_info
=
3837 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3840 for (i
= 0; i
< WINED3D_MAX_FRAGMENT_SAMPLERS
; ++i
)
3842 if (resource_info
[i
].type
&& context_gl
->tex_unit_map
[i
] != i
)
3844 wined3d_context_gl_map_stage(context_gl
, i
, i
);
3845 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(i
));
3846 if (i
< d3d_info
->ffp_fragment_caps
.max_blend_stages
)
3847 context_invalidate_texture_stage(&context_gl
->c
, i
);
3852 static BOOL
wined3d_context_gl_unit_free_for_vs(const struct wined3d_context_gl
*context_gl
,
3853 const struct wined3d_shader_resource_info
*ps_resource_info
, unsigned int unit
)
3855 unsigned int current_mapping
= context_gl
->rev_tex_unit_map
[unit
];
3857 /* Not currently used */
3858 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
3861 if (current_mapping
< WINED3D_MAX_FRAGMENT_SAMPLERS
)
3863 /* Used by a fragment sampler */
3865 if (!ps_resource_info
)
3867 /* No pixel shader, check fixed function */
3868 return current_mapping
>= WINED3D_MAX_FFP_TEXTURES
3869 || !(context_gl
->c
.fixed_function_usage_map
& (1u << current_mapping
));
3872 /* Pixel shader, check the shader's sampler map */
3873 return !ps_resource_info
[current_mapping
].type
;
3879 static void wined3d_context_gl_map_vsamplers(struct wined3d_context_gl
*context_gl
,
3880 BOOL ps
, const struct wined3d_state
*state
)
3882 const struct wined3d_shader_resource_info
*vs_resource_info
=
3883 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
3884 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
3885 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3886 int start
= min(WINED3D_MAX_COMBINED_SAMPLERS
, gl_info
->limits
.graphics_samplers
) - 1;
3889 /* Note that we only care if a resource is used or not, not the
3890 * resource's specific type. Otherwise we'd need to call
3891 * shader_update_samplers() here for 1.x pixelshaders. */
3893 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3895 for (i
= 0; i
< WINED3D_MAX_VERTEX_SAMPLERS
; ++i
)
3897 DWORD vsampler_idx
= i
+ WINED3D_MAX_FRAGMENT_SAMPLERS
;
3898 if (vs_resource_info
[i
].type
)
3902 if (wined3d_context_gl_unit_free_for_vs(context_gl
, ps_resource_info
, start
))
3904 if (context_gl
->tex_unit_map
[vsampler_idx
] != start
)
3906 wined3d_context_gl_map_stage(context_gl
, vsampler_idx
, start
);
3907 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(vsampler_idx
));
3916 if (context_gl
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
3917 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
3922 static void wined3d_context_gl_update_tex_unit_map(struct wined3d_context_gl
*context_gl
,
3923 const struct wined3d_state
*state
)
3925 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
3926 BOOL vs
= use_vs(state
);
3927 BOOL ps
= use_ps(state
);
3930 context_update_fixed_function_usage_map(&context_gl
->c
, state
);
3932 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3933 * need a 1:1 map at the moment.
3934 * When the mapping of a stage is changed, sampler and ALL texture stage
3935 * states have to be reset. */
3937 if (gl_info
->limits
.graphics_samplers
>= WINED3D_MAX_COMBINED_SAMPLERS
)
3941 wined3d_context_gl_map_psamplers(context_gl
, state
);
3943 wined3d_context_gl_map_fixed_function_samplers(context_gl
, state
);
3946 wined3d_context_gl_map_vsamplers(context_gl
, ps
, state
);
3949 /* Context activation is done by the caller. */
3950 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3952 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
3953 uint32_t rt_mask
, *cur_mask
;
3955 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
3957 cur_mask
= context_gl
->current_fbo
? &context_gl
->current_fbo
->rt_mask
: &context_gl
->draw_buffers_mask
;
3958 rt_mask
= find_draw_buffers_mask(context_gl
, state
);
3959 if (rt_mask
!= *cur_mask
)
3961 wined3d_context_gl_apply_draw_buffers(context_gl
, rt_mask
);
3962 *cur_mask
= rt_mask
;
3966 static void wined3d_context_gl_bind_shader_resources(struct wined3d_context_gl
*context_gl
,
3967 const struct wined3d_state
*state
, enum wined3d_shader_type shader_type
)
3969 unsigned int bind_idx
, shader_sampler_count
, base
, count
, i
;
3970 const struct wined3d_device
*device
= context_gl
->c
.device
;
3971 struct wined3d_shader_sampler_map_entry
*entry
;
3972 struct wined3d_shader_resource_view
*view
;
3973 const struct wined3d_shader
*shader
;
3974 const unsigned int *tex_unit_map
;
3975 struct wined3d_sampler
*sampler
;
3977 if (!(shader
= state
->shader
[shader_type
]))
3980 tex_unit_map
= wined3d_context_gl_get_tex_unit_mapping(context_gl
,
3981 &shader
->reg_maps
.shader_version
, &base
, &count
);
3983 shader_sampler_count
= shader
->reg_maps
.sampler_map
.count
;
3984 if (shader_sampler_count
> count
)
3985 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3986 shader
, shader_sampler_count
, count
);
3987 count
= min(shader_sampler_count
, count
);
3989 for (i
= 0; i
< count
; ++i
)
3991 entry
= &shader
->reg_maps
.sampler_map
.entries
[i
];
3992 bind_idx
= base
+ entry
->bind_idx
;
3994 bind_idx
= tex_unit_map
[bind_idx
];
3996 if (!(view
= state
->shader_resource_view
[shader_type
][entry
->resource_idx
]))
3998 WARN("No resource view bound at index %u, %u.\n", shader_type
, entry
->resource_idx
);
4002 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
4003 sampler
= device
->default_sampler
;
4004 else if (!(sampler
= state
->sampler
[shader_type
][entry
->sampler_idx
]))
4005 sampler
= device
->null_sampler
;
4006 wined3d_shader_resource_view_gl_bind(wined3d_shader_resource_view_gl(view
),
4007 bind_idx
, wined3d_sampler_gl(sampler
), context_gl
);
4011 static void wined3d_context_gl_bind_unordered_access_views(struct wined3d_context_gl
*context_gl
,
4012 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
4014 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4015 struct wined3d_unordered_access_view_gl
*view_gl
;
4016 const struct wined3d_format_gl
*format_gl
;
4017 GLuint texture_name
;
4024 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
4028 if (shader
->reg_maps
.uav_resource_info
[i
].type
)
4029 WARN("No unordered access view bound at index %u.\n", i
);
4030 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
4034 view_gl
= wined3d_unordered_access_view_gl(views
[i
]);
4035 if (view_gl
->gl_view
.name
)
4037 texture_name
= view_gl
->gl_view
.name
;
4040 else if (view_gl
->v
.resource
->type
!= WINED3D_RTYPE_BUFFER
)
4042 struct wined3d_texture_gl
*texture_gl
= wined3d_texture_gl(texture_from_resource(view_gl
->v
.resource
));
4043 texture_name
= wined3d_texture_gl_get_texture_name(texture_gl
, &context_gl
->c
, FALSE
);
4044 level
= view_gl
->v
.desc
.u
.texture
.level_idx
;
4048 FIXME("Unsupported buffer unordered access view.\n");
4049 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
4053 format_gl
= wined3d_format_gl(view_gl
->v
.format
);
4054 GL_EXTCALL(glBindImageTexture(i
, texture_name
, level
, GL_TRUE
, 0, GL_READ_WRITE
,
4055 format_gl
->internal
));
4057 if (view_gl
->counter_bo
.id
)
4058 GL_EXTCALL(glBindBufferRange(GL_ATOMIC_COUNTER_BUFFER
, i
, view_gl
->counter_bo
.id
,
4059 view_gl
->counter_bo
.b
.buffer_offset
, view_gl
->counter_bo
.size
));
4061 checkGLcall("Bind unordered access views");
4064 static void context_gl_load_shader_resources(struct wined3d_context_gl
*context_gl
,
4065 const struct wined3d_state
*state
, unsigned int shader_mask
)
4067 struct wined3d_shader_sampler_map_entry
*entry
;
4068 struct wined3d_shader_resource_view_gl
*srv_gl
;
4069 struct wined3d_shader_resource_view
*view
;
4070 struct wined3d_shader
*shader
;
4071 struct wined3d_buffer
*buffer
;
4074 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
4076 if (!(shader_mask
& (1u << i
)))
4079 if (!(shader
= state
->shader
[i
]))
4082 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
4084 if (!state
->cb
[i
][j
].buffer
)
4087 buffer
= state
->cb
[i
][j
].buffer
;
4088 wined3d_buffer_load(buffer
, &context_gl
->c
, state
);
4089 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4090 if (!buffer
->bo_user
.valid
)
4091 device_invalidate_state(context_gl
->c
.device
, STATE_CONSTANT_BUFFER(i
));
4094 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
4096 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
4098 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
4101 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
4103 buffer
= buffer_from_resource(view
->resource
);
4104 wined3d_buffer_load(buffer
, &context_gl
->c
, state
);
4105 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4107 srv_gl
= wined3d_shader_resource_view_gl(view
);
4108 if (!srv_gl
->bo_user
.valid
)
4109 wined3d_shader_resource_view_gl_update(srv_gl
, context_gl
);
4113 wined3d_texture_load(texture_from_resource(view
->resource
), &context_gl
->c
, FALSE
);
4119 static void context_gl_load_unordered_access_resources(struct wined3d_context_gl
*context_gl
,
4120 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
4122 struct wined3d_unordered_access_view_gl
*uav_gl
;
4123 struct wined3d_unordered_access_view
*view
;
4124 struct wined3d_texture
*texture
;
4125 struct wined3d_buffer
*buffer
;
4128 context_gl
->c
.uses_uavs
= 0;
4133 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
4135 if (!(view
= views
[i
]))
4138 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
4140 buffer
= buffer_from_resource(view
->resource
);
4141 wined3d_buffer_acquire_bo_for_write(buffer
, &context_gl
->c
);
4142 wined3d_buffer_load_location(buffer
, &context_gl
->c
, WINED3D_LOCATION_BUFFER
);
4143 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_BUFFER
);
4144 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4146 uav_gl
= wined3d_unordered_access_view_gl(view
);
4147 if (!uav_gl
->bo_user
.valid
)
4148 wined3d_unordered_access_view_gl_update(uav_gl
, context_gl
);
4152 texture
= texture_from_resource(view
->resource
);
4153 wined3d_texture_load(texture
, &context_gl
->c
, FALSE
);
4154 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_TEXTURE_RGB
);
4157 context_gl
->c
.uses_uavs
= 1;
4161 static void context_gl_load_stream_output_buffers(struct wined3d_context_gl
*context_gl
,
4162 const struct wined3d_state
*state
)
4164 struct wined3d_buffer
*buffer
;
4167 for (i
= 0; i
< ARRAY_SIZE(state
->stream_output
); ++i
)
4169 if (!(buffer
= state
->stream_output
[i
].buffer
))
4172 wined3d_buffer_acquire_bo_for_write(buffer
, &context_gl
->c
);
4174 wined3d_buffer_load(buffer
, &context_gl
->c
, state
);
4175 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
4176 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4177 if (!buffer
->bo_user
.valid
)
4178 device_invalidate_state(context_gl
->c
.device
, STATE_STREAM_OUTPUT
);
4182 /* Context activation is done by the caller. */
4183 static BOOL
context_apply_draw_state(struct wined3d_context
*context
,
4184 const struct wined3d_device
*device
, const struct wined3d_state
*state
, BOOL indexed
)
4186 const struct wined3d_state_entry
*state_table
= context
->state_table
;
4187 struct wined3d_context_gl
*context_gl
= wined3d_context_gl(context
);
4188 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4189 const struct wined3d_fb_state
*fb
= &state
->fb
;
4190 unsigned int i
, base
;
4193 context
->uses_fbo_attached_resources
= 0;
4195 if (!have_framebuffer_attachment(gl_info
->limits
.buffers
, fb
->render_targets
, fb
->depth_stencil
))
4197 if (!gl_info
->supported
[ARB_FRAMEBUFFER_NO_ATTACHMENTS
])
4199 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
4203 wined3d_context_gl_set_render_offscreen(context_gl
, TRUE
);
4206 /* Preload resources before FBO setup. Texture preload in particular may
4207 * result in changes to the current FBO, due to using e.g. FBO blits for
4208 * updating a resource location. */
4209 wined3d_context_gl_update_tex_unit_map(context_gl
, state
);
4210 context_preload_textures(context
, state
);
4211 context_gl_load_shader_resources(context_gl
, state
, ~(1u << WINED3D_SHADER_TYPE_COMPUTE
));
4212 context_gl_load_unordered_access_resources(context_gl
, state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
4213 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
4214 context_gl_load_stream_output_buffers(context_gl
, state
);
4215 /* TODO: Right now the dependency on the vertex shader is necessary
4216 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
4217 * the current VS but maybe it's possible to relax the coupling in some
4218 * situations at least. */
4219 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
4220 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
4222 context_update_stream_info(context
, state
);
4225 map
= context
->stream_info
.use_map
;
4228 const struct wined3d_stream_info_element
*e
;
4229 struct wined3d_buffer
*buffer
;
4231 e
= &context
->stream_info
.elements
[wined3d_bit_scan(&map
)];
4232 buffer
= state
->streams
[e
->stream_idx
].buffer
;
4234 if (!buffer
->bo_user
.valid
)
4235 device_invalidate_state(device
, STATE_STREAMSRC
);
4237 wined3d_buffer_load(buffer
, context
, state
);
4239 /* Loading the buffers above may have invalidated the stream info. */
4240 if (wined3d_context_is_graphics_state_dirty(context
, STATE_STREAMSRC
))
4241 context_update_stream_info(context
, state
);
4243 map
= context
->stream_info
.use_map
;
4246 const struct wined3d_stream_info_element
*e
;
4247 struct wined3d_buffer
*buffer
;
4249 e
= &context
->stream_info
.elements
[wined3d_bit_scan(&map
)];
4250 buffer
= state
->streams
[e
->stream_idx
].buffer
;
4252 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4255 if (indexed
&& state
->index_buffer
)
4257 struct wined3d_buffer
*buffer
= state
->index_buffer
;
4259 if (context
->stream_info
.all_vbo
)
4261 wined3d_buffer_load(buffer
, context
, state
);
4262 if (!buffer
->bo_user
.valid
)
4263 device_invalidate_state(device
, STATE_INDEXBUFFER
);
4264 wined3d_context_gl_reference_buffer(context_gl
, buffer
);
4268 wined3d_buffer_load_sysmem(buffer
, context
);
4272 for (i
= 0, base
= 0; i
< ARRAY_SIZE(context
->dirty_graphics_states
); ++i
)
4274 uint32_t dirty_mask
= context
->dirty_graphics_states
[i
];
4278 unsigned int state_id
= base
+ wined3d_bit_scan(&dirty_mask
);
4280 state_table
[state_id
].apply(context
, state
, state_id
);
4282 base
+= sizeof(dirty_mask
) * CHAR_BIT
;
4285 memset(context
->dirty_graphics_states
, 0, sizeof(context
->dirty_graphics_states
));
4287 if (context
->shader_update_mask
& ~(1u << WINED3D_SHADER_TYPE_COMPUTE
))
4289 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
4290 context
->shader_update_mask
&= 1u << WINED3D_SHADER_TYPE_COMPUTE
;
4293 if (context
->update_shader_resource_bindings
)
4295 for (i
= 0; i
< WINED3D_SHADER_TYPE_GRAPHICS_COUNT
; ++i
)
4296 wined3d_context_gl_bind_shader_resources(context_gl
, state
, i
);
4297 context
->update_shader_resource_bindings
= 0;
4298 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
4299 context
->update_compute_shader_resource_bindings
= 1;
4302 if (context
->update_unordered_access_view_bindings
)
4304 wined3d_context_gl_bind_unordered_access_views(context_gl
,
4305 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
4306 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
4307 context
->update_unordered_access_view_bindings
= 0;
4308 context
->update_compute_unordered_access_view_bindings
= 1;
4311 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
4312 wined3d_context_gl_check_fbo_status(context_gl
, GL_FRAMEBUFFER
);
4314 if (context
->constant_update_mask
)
4316 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
4317 context
->constant_update_mask
= 0;
4320 context
->last_was_blit
= FALSE
;
4321 context
->last_was_ffp_blit
= FALSE
;
4326 static void wined3d_context_gl_apply_compute_state(struct wined3d_context_gl
*context_gl
,
4327 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
4329 const struct wined3d_state_entry
*state_table
= context_gl
->c
.state_table
;
4330 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4331 unsigned int state_id
, i
;
4333 context_gl_load_shader_resources(context_gl
, state
, 1u << WINED3D_SHADER_TYPE_COMPUTE
);
4334 context_gl_load_unordered_access_resources(context_gl
, state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
4335 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
4337 for (i
= 0, state_id
= STATE_COMPUTE_OFFSET
; i
< ARRAY_SIZE(context_gl
->c
.dirty_compute_states
); ++i
)
4339 unsigned int dirty_mask
= context_gl
->c
.dirty_compute_states
[i
];
4343 unsigned int current_state_id
= state_id
+ wined3d_bit_scan(&dirty_mask
);
4344 state_table
[current_state_id
].apply(&context_gl
->c
, state
, current_state_id
);
4346 state_id
+= sizeof(*context_gl
->c
.dirty_compute_states
) * CHAR_BIT
;
4348 memset(context_gl
->c
.dirty_compute_states
, 0, sizeof(*context_gl
->c
.dirty_compute_states
));
4350 if (context_gl
->c
.shader_update_mask
& (1u << WINED3D_SHADER_TYPE_COMPUTE
))
4352 device
->shader_backend
->shader_select_compute(device
->shader_priv
, &context_gl
->c
, state
);
4353 context_gl
->c
.shader_update_mask
&= ~(1u << WINED3D_SHADER_TYPE_COMPUTE
);
4356 if (context_gl
->c
.update_compute_shader_resource_bindings
)
4358 wined3d_context_gl_bind_shader_resources(context_gl
, state
, WINED3D_SHADER_TYPE_COMPUTE
);
4359 context_gl
->c
.update_compute_shader_resource_bindings
= 0;
4360 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
4361 context_gl
->c
.update_shader_resource_bindings
= 1;
4364 if (context_gl
->c
.update_compute_unordered_access_view_bindings
)
4366 wined3d_context_gl_bind_unordered_access_views(context_gl
,
4367 state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
4368 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
4369 context_gl
->c
.update_compute_unordered_access_view_bindings
= 0;
4370 context_gl
->c
.update_unordered_access_view_bindings
= 1;
4373 /* Updates to currently bound render targets aren't necessarily coherent
4374 * between the graphics and compute pipelines. Unbind any currently bound
4375 * FBO here to ensure preceding updates to its attachments by the graphics
4376 * pipeline are visible to the compute pipeline.
4378 * Without this, the bloom effect in Nier:Automata is too bright on the
4379 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4380 wined3d_context_gl_bind_fbo(context_gl
, GL_FRAMEBUFFER
, 0);
4381 context_invalidate_state(&context_gl
->c
, STATE_FRAMEBUFFER
);
4383 context_gl
->c
.last_was_blit
= FALSE
;
4384 context_gl
->c
.last_was_ffp_blit
= FALSE
;
4387 void wined3d_context_gl_end_transform_feedback(struct wined3d_context_gl
*context_gl
)
4389 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4391 if (context_gl
->c
.transform_feedback_active
)
4393 GL_EXTCALL(glEndTransformFeedback());
4394 checkGLcall("glEndTransformFeedback");
4395 context_gl
->c
.transform_feedback_active
= 0;
4396 context_gl
->c
.transform_feedback_paused
= 0;
4400 static void wined3d_context_gl_pause_transform_feedback(struct wined3d_context_gl
*context_gl
, BOOL force
)
4402 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4404 if (!context_gl
->c
.transform_feedback_active
|| context_gl
->c
.transform_feedback_paused
)
4407 if (gl_info
->supported
[ARB_TRANSFORM_FEEDBACK2
])
4409 GL_EXTCALL(glPauseTransformFeedback());
4410 checkGLcall("glPauseTransformFeedback");
4411 context_gl
->c
.transform_feedback_paused
= 1;
4415 WARN("Cannot pause transform feedback operations.\n");
4418 wined3d_context_gl_end_transform_feedback(context_gl
);
4421 static void wined3d_context_gl_setup_target(struct wined3d_context_gl
*context_gl
,
4422 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4424 BOOL old_render_offscreen
= context_gl
->c
.render_offscreen
, render_offscreen
;
4426 render_offscreen
= wined3d_resource_is_offscreen(&texture
->resource
);
4427 if (context_gl
->c
.current_rt
.texture
== texture
4428 && context_gl
->c
.current_rt
.sub_resource_idx
== sub_resource_idx
4429 && render_offscreen
== old_render_offscreen
)
4432 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4433 * the alpha blend state changes with different render target formats. */
4434 if (!context_gl
->c
.current_rt
.texture
)
4436 context_invalidate_state(&context_gl
->c
, STATE_BLEND
);
4440 const struct wined3d_format
*old
= context_gl
->c
.current_rt
.texture
->resource
.format
;
4441 const struct wined3d_format
*new = texture
->resource
.format
;
4443 if (old
->id
!= new->id
)
4445 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4446 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
4447 || !(texture
->resource
.format_caps
& WINED3D_FORMAT_CAP_POSTPIXELSHADER_BLENDING
))
4448 context_invalidate_state(&context_gl
->c
, STATE_BLEND
);
4451 /* When switching away from an offscreen render target, and we're not
4452 * using FBOs, we have to read the drawable into the texture. This is
4453 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4454 * There are some things that need care though. PreLoad needs a GL context,
4455 * and FindContext is called before the context is activated. It also
4456 * has to be called with the old rendertarget active, otherwise a
4457 * wrong drawable is read. */
4458 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
4459 && old_render_offscreen
&& (context_gl
->c
.current_rt
.texture
!= texture
4460 || context_gl
->c
.current_rt
.sub_resource_idx
!= sub_resource_idx
))
4462 struct wined3d_texture_gl
*prev_texture
= wined3d_texture_gl(context_gl
->c
.current_rt
.texture
);
4463 unsigned int prev_sub_resource_idx
= context_gl
->c
.current_rt
.sub_resource_idx
;
4465 /* Read the back buffer of the old drawable into the destination texture. */
4466 if (prev_texture
->texture_srgb
.name
)
4467 wined3d_texture_load(&prev_texture
->t
, &context_gl
->c
, TRUE
);
4468 wined3d_texture_load(&prev_texture
->t
, &context_gl
->c
, FALSE
);
4469 wined3d_texture_invalidate_location(&prev_texture
->t
, prev_sub_resource_idx
, WINED3D_LOCATION_DRAWABLE
);
4473 context_gl
->c
.current_rt
.texture
= texture
;
4474 context_gl
->c
.current_rt
.sub_resource_idx
= sub_resource_idx
;
4475 wined3d_context_gl_set_render_offscreen(context_gl
, render_offscreen
);
4478 static void wined3d_context_gl_activate(struct wined3d_context_gl
*context_gl
,
4479 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4481 wined3d_context_gl_enter(context_gl
);
4482 if (texture
&& texture
->swapchain
&& texture
->swapchain
!= context_gl
->c
.swapchain
)
4484 TRACE("Switching context_gl %p from swapchain %p to swapchain %p.\n",
4485 context_gl
, context_gl
->c
.swapchain
, texture
->swapchain
);
4486 context_gl
->c
.swapchain
= texture
->swapchain
;
4488 wined3d_context_gl_update_window(context_gl
);
4489 wined3d_context_gl_setup_target(context_gl
, texture
, sub_resource_idx
);
4490 if (!context_gl
->valid
)
4493 if (context_gl
!= wined3d_context_gl_get_current())
4495 if (!wined3d_context_gl_set_current(context_gl
))
4496 ERR("Failed to activate the new context.\n");
4498 else if (context_gl
->needs_set
)
4500 wined3d_context_gl_set_gl_context(context_gl
);
4504 struct wined3d_context
*wined3d_context_gl_acquire(const struct wined3d_device
*device
,
4505 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4507 struct wined3d_context_gl
*current_context
= wined3d_context_gl_get_current();
4508 struct wined3d_context_gl
*context_gl
;
4510 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device
, texture
, sub_resource_idx
);
4512 if (current_context
&& current_context
->c
.destroyed
)
4513 current_context
= NULL
;
4518 && current_context
->c
.current_rt
.texture
4519 && current_context
->c
.device
== device
)
4521 texture
= current_context
->c
.current_rt
.texture
;
4522 sub_resource_idx
= current_context
->c
.current_rt
.sub_resource_idx
;
4526 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
4528 if (swapchain
->back_buffers
)
4529 texture
= swapchain
->back_buffers
[0];
4531 texture
= swapchain
->front_buffer
;
4532 sub_resource_idx
= 0;
4536 if (current_context
&& current_context
->c
.current_rt
.texture
== texture
)
4538 context_gl
= current_context
;
4540 else if (!wined3d_resource_is_offscreen(&texture
->resource
))
4542 TRACE("Rendering onscreen.\n");
4544 if (!(context_gl
= wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(texture
->swapchain
))))
4549 TRACE("Rendering offscreen.\n");
4551 /* Stay with the current context if possible. Otherwise use the
4552 * context for the primary swapchain. */
4553 if (current_context
&& current_context
->c
.device
== device
)
4554 context_gl
= current_context
;
4555 else if (!(context_gl
= wined3d_swapchain_gl_get_context(wined3d_swapchain_gl(device
->swapchains
[0]))))
4559 wined3d_context_gl_activate(context_gl
, texture
, sub_resource_idx
);
4561 return &context_gl
->c
;
4564 struct wined3d_context_gl
*wined3d_context_gl_reacquire(struct wined3d_context_gl
*context_gl
)
4566 struct wined3d_context
*acquired_context
;
4567 struct wined3d_device
*device
;
4569 if (!context_gl
|| context_gl
->tid
!= GetCurrentThreadId())
4572 device
= context_gl
->c
.device
;
4573 wined3d_from_cs(device
->cs
);
4575 if (context_gl
->c
.current_rt
.texture
)
4577 wined3d_context_gl_activate(context_gl
, context_gl
->c
.current_rt
.texture
,
4578 context_gl
->c
.current_rt
.sub_resource_idx
);
4582 acquired_context
= context_acquire(device
, NULL
, 0);
4583 if (acquired_context
!= &context_gl
->c
)
4584 ERR("Acquired context %p instead of %p.\n", acquired_context
, &context_gl
->c
);
4585 return wined3d_context_gl(acquired_context
);
4588 void dispatch_compute(struct wined3d_device
*device
, const struct wined3d_state
*state
,
4589 const struct wined3d_dispatch_parameters
*parameters
)
4591 const struct wined3d_gl_info
*gl_info
;
4592 struct wined3d_context_gl
*context_gl
;
4594 context_gl
= wined3d_context_gl(context_acquire(device
, NULL
, 0));
4595 if (!context_gl
->valid
)
4597 context_release(&context_gl
->c
);
4598 WARN("Invalid context, skipping dispatch.\n");
4601 gl_info
= context_gl
->gl_info
;
4603 if (!gl_info
->supported
[ARB_COMPUTE_SHADER
])
4605 context_release(&context_gl
->c
);
4606 FIXME("OpenGL implementation does not support compute shaders.\n");
4610 if (parameters
->indirect
)
4611 wined3d_buffer_load(parameters
->u
.indirect
.buffer
, &context_gl
->c
, state
);
4613 wined3d_context_gl_apply_compute_state(context_gl
, device
, state
);
4615 if (parameters
->indirect
)
4617 const struct wined3d_indirect_dispatch_parameters
*indirect
= ¶meters
->u
.indirect
;
4618 struct wined3d_bo_gl
*bo_gl
= wined3d_bo_gl(indirect
->buffer
->buffer_object
);
4620 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, bo_gl
->id
));
4621 GL_EXTCALL(glDispatchComputeIndirect(bo_gl
->b
.buffer_offset
+ (GLintptr
)indirect
->offset
));
4622 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0));
4623 wined3d_context_gl_reference_bo(context_gl
, bo_gl
);
4627 const struct wined3d_direct_dispatch_parameters
*direct
= ¶meters
->u
.direct
;
4628 GL_EXTCALL(glDispatchCompute(direct
->group_count_x
, direct
->group_count_y
, direct
->group_count_z
));
4630 checkGLcall("dispatch compute");
4632 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS
));
4633 checkGLcall("glMemoryBarrier");
4635 context_release(&context_gl
->c
);
4638 /* Context activation is done by the caller. */
4639 static void wined3d_context_gl_draw_primitive_arrays(struct wined3d_context_gl
*context_gl
,
4640 const struct wined3d_state
*state
, const void *idx_data
, unsigned int idx_size
, int base_vertex_idx
,
4641 unsigned int start_idx
, unsigned int count
, unsigned int start_instance
, unsigned int instance_count
)
4643 GLenum idx_type
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
4644 const struct wined3d_stream_info
*si
= &context_gl
->c
.stream_info
;
4645 GLenum mode
= gl_primitive_type_from_d3d(state
->primitive_type
);
4646 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4647 unsigned int instanced_elements
[ARRAY_SIZE(si
->elements
)];
4648 const struct wined3d_ffp_attrib_ops
*ops
;
4649 unsigned int instanced_element_count
= 0;
4650 const void *indices
;
4653 ops
= &gl_info
->ffp_attrib_ops
;
4655 indices
= (const char *)idx_data
+ idx_size
* start_idx
;
4657 if (!instance_count
)
4661 gl_info
->gl_ops
.gl
.p_glDrawArrays(mode
, start_idx
, count
);
4662 checkGLcall("glDrawArrays");
4666 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4668 GL_EXTCALL(glDrawElementsBaseVertex(mode
, count
, idx_type
, indices
, base_vertex_idx
));
4669 checkGLcall("glDrawElementsBaseVertex");
4673 gl_info
->gl_ops
.gl
.p_glDrawElements(mode
, count
, idx_type
, indices
);
4674 checkGLcall("glDrawElements");
4678 if (start_instance
&& !(gl_info
->supported
[ARB_BASE_INSTANCE
] && gl_info
->supported
[ARB_INSTANCED_ARRAYS
]))
4679 FIXME("Start instance (%u) not supported.\n", start_instance
);
4681 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
4685 if (gl_info
->supported
[ARB_BASE_INSTANCE
])
4687 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode
, start_idx
, count
, instance_count
, start_instance
));
4688 checkGLcall("glDrawArraysInstancedBaseInstance");
4692 GL_EXTCALL(glDrawArraysInstanced(mode
, start_idx
, count
, instance_count
));
4693 checkGLcall("glDrawArraysInstanced");
4697 if (gl_info
->supported
[ARB_BASE_INSTANCE
])
4699 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode
, count
, idx_type
,
4700 indices
, instance_count
, base_vertex_idx
, start_instance
));
4701 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4704 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4706 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode
, count
, idx_type
,
4707 indices
, instance_count
, base_vertex_idx
));
4708 checkGLcall("glDrawElementsInstancedBaseVertex");
4712 GL_EXTCALL(glDrawElementsInstanced(mode
, count
, idx_type
, indices
, instance_count
));
4713 checkGLcall("glDrawElementsInstanced");
4717 /* Instancing emulation by mixing immediate mode and arrays. */
4719 /* This is a nasty thing. MSDN says no hardware supports this and
4720 * applications have to use software vertex processing. We don't support
4723 * Shouldn't be too hard to support with OpenGL, in theory just call
4724 * glDrawArrays() instead of drawElements(). But the stream frequency value
4725 * has a different meaning in that situation. */
4728 FIXME("Non-indexed instanced drawing is not supported.\n");
4732 for (i
= 0; i
< ARRAY_SIZE(si
->elements
); ++i
)
4734 if (!(si
->use_map
& (1u << i
)))
4737 if (state
->streams
[si
->elements
[i
].stream_idx
].flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
4738 instanced_elements
[instanced_element_count
++] = i
;
4741 for (i
= 0; i
< instance_count
; ++i
)
4743 /* Specify the instanced attributes using immediate mode calls. */
4744 for (j
= 0; j
< instanced_element_count
; ++j
)
4746 const struct wined3d_stream_info_element
*element
;
4747 unsigned int element_idx
;
4750 element_idx
= instanced_elements
[j
];
4751 element
= &si
->elements
[element_idx
];
4752 ptr
= element
->data
.addr
+ element
->stride
* i
;
4753 if (element
->data
.buffer_object
)
4754 ptr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(state
->streams
[element
->stream_idx
].buffer
,
4756 ops
->generic
[element
->format
->emit_idx
](element_idx
, ptr
);
4759 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4761 GL_EXTCALL(glDrawElementsBaseVertex(mode
, count
, idx_type
, indices
, base_vertex_idx
));
4762 checkGLcall("glDrawElementsBaseVertex");
4766 gl_info
->gl_ops
.gl
.p_glDrawElements(mode
, count
, idx_type
, indices
);
4767 checkGLcall("glDrawElements");
4772 static unsigned int get_stride_idx(const void *idx_data
, unsigned int idx_size
,
4773 unsigned int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_idx
)
4776 return start_idx
+ vertex_idx
;
4778 return ((const WORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
4779 return ((const DWORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
4782 /* Context activation is done by the caller. */
4783 static void draw_primitive_immediate_mode(struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
,
4784 const struct wined3d_stream_info
*si
, const void *idx_data
, unsigned int idx_size
,
4785 int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_count
, unsigned int instance_count
)
4787 const BYTE
*position
= NULL
, *normal
= NULL
, *diffuse
= NULL
, *specular
= NULL
;
4788 const struct wined3d_d3d_info
*d3d_info
= context_gl
->c
.d3d_info
;
4789 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
4790 unsigned int coord_idx
, stride_idx
, texture_idx
, vertex_idx
;
4791 const struct wined3d_stream_info_element
*element
;
4792 const BYTE
*tex_coords
[WINED3DDP_MAXTEXCOORD
];
4793 unsigned int texture_unit
, texture_stages
;
4794 const struct wined3d_ffp_attrib_ops
*ops
;
4795 unsigned int untracked_material_count
;
4796 unsigned int tex_mask
= 0;
4797 BOOL specular_fog
= FALSE
;
4798 BOOL ps
= use_ps(state
);
4801 static unsigned int once
;
4804 FIXME_(d3d_perf
)("Drawing using immediate mode.\n");
4806 WARN_(d3d_perf
)("Drawing using immediate mode.\n");
4808 if (!idx_size
&& idx_data
)
4809 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4812 FIXME("Instancing not implemented.\n");
4814 /* Immediate mode drawing can't make use of indices in a VBO - get the
4815 * data from the index buffer. */
4817 idx_data
= (uint8_t *)wined3d_buffer_load_sysmem(state
->index_buffer
, &context_gl
->c
) + state
->index_offset
;
4819 ops
= &gl_info
->ffp_attrib_ops
;
4821 gl_info
->gl_ops
.gl
.p_glBegin(gl_primitive_type_from_d3d(state
->primitive_type
));
4823 if (use_vs(state
) || d3d_info
->ffp_generic_attributes
)
4825 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
4827 unsigned int use_map
= si
->use_map
;
4828 unsigned int element_idx
;
4830 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
4831 for (element_idx
= gl_info
->limits
.vertex_attribs
- 1; use_map
;
4832 use_map
&= ~(1u << element_idx
), --element_idx
)
4834 if (!(use_map
& 1u << element_idx
))
4837 ptr
= si
->elements
[element_idx
].data
.addr
+ si
->elements
[element_idx
].stride
* stride_idx
;
4838 ops
->generic
[si
->elements
[element_idx
].format
->emit_idx
](element_idx
, ptr
);
4842 gl_info
->gl_ops
.gl
.p_glEnd();
4846 if (si
->use_map
& (1u << WINED3D_FFP_POSITION
))
4847 position
= si
->elements
[WINED3D_FFP_POSITION
].data
.addr
;
4849 if (si
->use_map
& (1u << WINED3D_FFP_NORMAL
))
4850 normal
= si
->elements
[WINED3D_FFP_NORMAL
].data
.addr
;
4852 gl_info
->gl_ops
.gl
.p_glNormal3f(0.0f
, 0.0f
, 0.0f
);
4854 untracked_material_count
= context_gl
->untracked_material_count
;
4855 if (si
->use_map
& (1u << WINED3D_FFP_DIFFUSE
))
4857 element
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
4858 diffuse
= element
->data
.addr
;
4860 if (untracked_material_count
&& element
->format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
4861 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element
->format
->id
));
4865 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
4868 if (si
->use_map
& (1u << WINED3D_FFP_SPECULAR
))
4870 element
= &si
->elements
[WINED3D_FFP_SPECULAR
];
4871 specular
= element
->data
.addr
;
4873 /* Special case where the fog density is stored in the specular alpha channel. */
4874 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
4875 && (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
4876 || si
->elements
[WINED3D_FFP_POSITION
].format
->id
== WINED3DFMT_R32G32B32A32_FLOAT
)
4877 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
4879 if (gl_info
->supported
[EXT_FOG_COORD
])
4881 if (element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
4882 specular_fog
= TRUE
;
4884 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element
->format
->id
));
4888 static unsigned int once
;
4891 FIXME("Implement fog for transformed vertices in software.\n");
4895 else if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
4897 GL_EXTCALL(glSecondaryColor3fEXT
)(0.0f
, 0.0f
, 0.0f
);
4900 texture_stages
= d3d_info
->ffp_fragment_caps
.max_blend_stages
;
4901 for (texture_idx
= 0; texture_idx
< texture_stages
; ++texture_idx
)
4903 if (!gl_info
->supported
[ARB_MULTITEXTURE
] && texture_idx
> 0)
4905 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4909 if (!ps
&& !wined3d_state_get_ffp_texture(state
, texture_idx
))
4912 texture_unit
= context_gl
->tex_unit_map
[texture_idx
];
4913 if (texture_unit
== WINED3D_UNMAPPED_STAGE
)
4916 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
4919 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx
, texture_idx
);
4923 if (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
)))
4925 tex_coords
[coord_idx
] = si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].data
.addr
;
4926 tex_mask
|= (1u << texture_idx
);
4930 TRACE("Setting default coordinates for texture %u.\n", texture_idx
);
4931 if (gl_info
->supported
[ARB_MULTITEXTURE
])
4932 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_unit
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
4934 gl_info
->gl_ops
.gl
.p_glTexCoord4f(0.0f
, 0.0f
, 0.0f
, 1.0f
);
4938 /* Blending data and point sizes are not supported by this function. They
4939 * are not supported by the fixed function pipeline at all. A FIXME for
4940 * them is printed after decoding the vertex declaration. */
4941 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
4943 uint32_t tmp_tex_mask
;
4945 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
4949 ptr
= normal
+ stride_idx
* si
->elements
[WINED3D_FFP_NORMAL
].stride
;
4950 ops
->normal
[si
->elements
[WINED3D_FFP_NORMAL
].format
->emit_idx
](ptr
);
4955 ptr
= diffuse
+ stride_idx
* si
->elements
[WINED3D_FFP_DIFFUSE
].stride
;
4956 ops
->diffuse
[si
->elements
[WINED3D_FFP_DIFFUSE
].format
->emit_idx
](ptr
);
4958 if (untracked_material_count
)
4960 struct wined3d_color color
;
4963 wined3d_color_from_d3dcolor(&color
, *(const DWORD
*)ptr
);
4964 for (i
= 0; i
< untracked_material_count
; ++i
)
4966 gl_info
->gl_ops
.gl
.p_glMaterialfv(GL_FRONT_AND_BACK
,
4967 context_gl
->untracked_materials
[i
], &color
.r
);
4974 ptr
= specular
+ stride_idx
* si
->elements
[WINED3D_FFP_SPECULAR
].stride
;
4975 ops
->specular
[si
->elements
[WINED3D_FFP_SPECULAR
].format
->emit_idx
](ptr
);
4978 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD
*)ptr
>> 24)));
4981 tmp_tex_mask
= tex_mask
;
4982 while (tmp_tex_mask
)
4984 texture_idx
= wined3d_bit_scan(&tmp_tex_mask
);
4985 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
4986 ptr
= tex_coords
[coord_idx
] + (stride_idx
* si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].stride
);
4987 ops
->texcoord
[si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].format
->emit_idx
](
4988 GL_TEXTURE0_ARB
+ context_gl
->tex_unit_map
[texture_idx
], ptr
);
4993 ptr
= position
+ stride_idx
* si
->elements
[WINED3D_FFP_POSITION
].stride
;
4994 ops
->position
[si
->elements
[WINED3D_FFP_POSITION
].format
->emit_idx
](ptr
);
4998 gl_info
->gl_ops
.gl
.p_glEnd();
4999 checkGLcall("draw immediate mode");
5002 static void wined3d_context_gl_draw_indirect(struct wined3d_context_gl
*context_gl
, const struct wined3d_state
*state
,
5003 const struct wined3d_indirect_draw_parameters
*parameters
, unsigned int idx_size
)
5005 struct wined3d_bo_gl
*bo_gl
= wined3d_bo_gl(parameters
->buffer
->buffer_object
);
5006 GLenum gl_primitive_type
= gl_primitive_type_from_d3d(state
->primitive_type
);
5007 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5010 if (!gl_info
->supported
[ARB_DRAW_INDIRECT
])
5012 FIXME("OpenGL implementation does not support indirect draws.\n");
5016 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER
, bo_gl
->id
));
5018 offset
= (const uint8_t *)bo_gl
->b
.buffer_offset
+ parameters
->offset
;
5021 GLenum idx_type
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
5022 if (state
->index_offset
)
5023 FIXME("Ignoring index offset %u.\n", state
->index_offset
);
5024 GL_EXTCALL(glDrawElementsIndirect(gl_primitive_type
, idx_type
, offset
));
5028 GL_EXTCALL(glDrawArraysIndirect(gl_primitive_type
, offset
));
5031 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER
, 0));
5032 wined3d_context_gl_reference_bo(context_gl
, bo_gl
);
5034 checkGLcall("draw indirect");
5037 static void remove_vbos(struct wined3d_context
*context
,
5038 const struct wined3d_state
*state
, struct wined3d_stream_info
*s
)
5042 for (i
= 0; i
< ARRAY_SIZE(s
->elements
); ++i
)
5044 struct wined3d_stream_info_element
*e
;
5046 if (!(s
->use_map
& (1u << i
)))
5049 e
= &s
->elements
[i
];
5050 if (e
->data
.buffer_object
)
5052 struct wined3d_buffer
*vb
= state
->streams
[e
->stream_idx
].buffer
;
5053 e
->data
.buffer_object
= 0;
5054 e
->data
.addr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(vb
, context
);
5059 static GLenum
gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
5061 GLenum gl_primitive_type
= gl_primitive_type_from_d3d(primitive_type
);
5062 switch (gl_primitive_type
)
5068 case GL_LINE_STRIP_ADJACENCY
:
5069 case GL_LINES_ADJACENCY
:
5073 case GL_TRIANGLE_FAN
:
5074 case GL_TRIANGLE_STRIP
:
5075 case GL_TRIANGLE_STRIP_ADJACENCY
:
5076 case GL_TRIANGLES_ADJACENCY
:
5078 return GL_TRIANGLES
;
5081 return gl_primitive_type
;
5085 /* Routine common to the draw primitive and draw indexed primitive routines */
5086 void draw_primitive(struct wined3d_device
*device
, const struct wined3d_state
*state
,
5087 const struct wined3d_draw_parameters
*parameters
)
5089 BOOL emulation
= FALSE
, rasterizer_discard
= FALSE
;
5090 const struct wined3d_fb_state
*fb
= &state
->fb
;
5091 const struct wined3d_stream_info
*stream_info
;
5092 struct wined3d_rendertarget_view
*dsv
, *rtv
;
5093 struct wined3d_stream_info si_emulated
;
5094 const struct wined3d_gl_info
*gl_info
;
5095 struct wined3d_context_gl
*context_gl
;
5096 struct wined3d_context
*context
;
5097 unsigned int i
, idx_size
= 0;
5098 const void *idx_data
= NULL
;
5100 TRACE("device %p, state %p, parameters %p.\n", device
, state
, parameters
);
5102 if (!parameters
->indirect
&& !parameters
->u
.direct
.index_count
)
5105 if (!parameters
->indirect
)
5106 TRACE("base_vertex_idx %d, start_idx %u, index_count %u, start_instance %u, instance_count %u.\n",
5107 parameters
->u
.direct
.base_vertex_idx
, parameters
->u
.direct
.start_idx
,
5108 parameters
->u
.direct
.index_count
, parameters
->u
.direct
.start_instance
,
5109 parameters
->u
.direct
.instance_count
);
5111 if (!(rtv
= fb
->render_targets
[0]))
5112 rtv
= fb
->depth_stencil
;
5114 if (rtv
&& rtv
->resource
->type
== WINED3D_RTYPE_BUFFER
)
5116 FIXME("Buffer render targets not implemented.\n");
5121 context
= context_acquire(device
, wined3d_texture_from_resource(rtv
->resource
), rtv
->sub_resource_idx
);
5123 context
= context_acquire(device
, NULL
, 0);
5124 context_gl
= wined3d_context_gl(context
);
5125 if (!context_gl
->valid
)
5127 context_release(context
);
5128 WARN("Invalid context, skipping draw.\n");
5131 gl_info
= context_gl
->gl_info
;
5133 if (!use_transform_feedback(state
))
5134 wined3d_context_gl_pause_transform_feedback(context_gl
, TRUE
);
5136 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
5138 if (!(rtv
= fb
->render_targets
[i
]) || rtv
->format
->id
== WINED3DFMT_NULL
)
5141 if (wined3d_blend_state_get_writemask(state
->blend_state
, i
))
5143 wined3d_rendertarget_view_load_location(rtv
, context
, rtv
->resource
->draw_binding
);
5144 wined3d_rendertarget_view_invalidate_location(rtv
, ~rtv
->resource
->draw_binding
);
5148 wined3d_rendertarget_view_prepare_location(rtv
, context
, rtv
->resource
->draw_binding
);
5152 if ((dsv
= fb
->depth_stencil
))
5154 /* Note that this depends on the context_acquire() call above to set
5155 * context->render_offscreen properly. We don't currently take the
5156 * Z-compare function into account, but we could skip loading the
5157 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
5158 * that we never copy the stencil data.*/
5159 DWORD location
= context
->render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
5161 if (wined3d_state_uses_depth_buffer(state
))
5162 wined3d_rendertarget_view_load_location(dsv
, context
, location
);
5164 wined3d_rendertarget_view_prepare_location(dsv
, context
, location
);
5167 if (parameters
->indirect
)
5168 wined3d_buffer_load(parameters
->u
.indirect
.buffer
, context
, state
);
5170 if (!context_apply_draw_state(context
, device
, state
, parameters
->indexed
))
5172 context_release(context
);
5173 WARN("Unable to apply draw state, skipping draw.\n");
5177 if (dsv
&& (!state
->depth_stencil_state
|| state
->depth_stencil_state
->writes_ds
))
5179 DWORD location
= context
->render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
5181 wined3d_rendertarget_view_validate_location(dsv
, location
);
5182 wined3d_rendertarget_view_invalidate_location(dsv
, ~location
);
5185 stream_info
= &context
->stream_info
;
5187 if (parameters
->indexed
)
5189 struct wined3d_buffer
*index_buffer
= state
->index_buffer
;
5190 struct wined3d_bo
*bo
= index_buffer
->buffer_object
;
5192 if (!bo
|| !stream_info
->all_vbo
)
5193 idx_data
= index_buffer
->resource
.heap_memory
;
5195 idx_data
= (void *)bo
->buffer_offset
;
5196 idx_data
= (const BYTE
*)idx_data
+ state
->index_offset
;
5198 if (state
->index_format
== WINED3DFMT_R16_UINT
)
5206 if (!stream_info
->position_transformed
&& context_gl
->untracked_material_count
5207 && state
->render_states
[WINED3D_RS_LIGHTING
])
5212 FIXME("Using software emulation because not all material properties could be tracked.\n");
5214 WARN_(d3d_perf
)("Using software emulation because not all material properties could be tracked.\n");
5217 else if (context
->fog_coord
&& state
->render_states
[WINED3D_RS_FOGENABLE
])
5221 /* Either write a pipeline replacement shader or convert the
5222 * specular alpha from unsigned byte to a float in the vertex
5225 FIXME("Using software emulation because manual fog coordinates are provided.\n");
5227 WARN_(d3d_perf
)("Using software emulation because manual fog coordinates are provided.\n");
5233 si_emulated
= context
->stream_info
;
5234 remove_vbos(context
, state
, &si_emulated
);
5235 stream_info
= &si_emulated
;
5239 if (use_transform_feedback(state
))
5241 const struct wined3d_shader
*shader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
5243 if (is_rasterization_disabled(shader
))
5245 glEnable(GL_RASTERIZER_DISCARD
);
5246 checkGLcall("enable rasterizer discard");
5247 rasterizer_discard
= TRUE
;
5250 if (context
->transform_feedback_paused
)
5252 GL_EXTCALL(glResumeTransformFeedback());
5253 checkGLcall("glResumeTransformFeedback");
5254 context
->transform_feedback_paused
= 0;
5256 else if (!context
->transform_feedback_active
)
5258 enum wined3d_primitive_type primitive_type
= shader
->u
.gs
.output_type
5259 ? shader
->u
.gs
.output_type
: state
->primitive_type
;
5260 GLenum mode
= gl_tfb_primitive_type_from_d3d(primitive_type
);
5261 GL_EXTCALL(glBeginTransformFeedback(mode
));
5262 checkGLcall("glBeginTransformFeedback");
5263 context
->transform_feedback_active
= 1;
5267 if (state
->primitive_type
== WINED3D_PT_PATCH
)
5269 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES
, state
->patch_vertex_count
));
5270 checkGLcall("glPatchParameteri");
5273 if (context
->uses_fbo_attached_resources
)
5275 static unsigned int fixme_once
;
5277 if (gl_info
->supported
[ARB_TEXTURE_BARRIER
])
5279 GL_EXTCALL(glTextureBarrier());
5281 else if (gl_info
->supported
[NV_TEXTURE_BARRIER
])
5283 GL_EXTCALL(glTextureBarrierNV());
5288 FIXME("Sampling attached render targets is not supported.\n");
5290 WARN("Sampling attached render targets is not supported, skipping draw.\n");
5291 context_release(context
);
5294 checkGLcall("glTextureBarrier");
5297 if (parameters
->indirect
)
5299 if (!context
->use_immediate_mode_draw
&& !emulation
)
5300 wined3d_context_gl_draw_indirect(context_gl
, state
, ¶meters
->u
.indirect
, idx_size
);
5302 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
5306 unsigned int instance_count
= parameters
->u
.direct
.instance_count
;
5308 if (context
->use_immediate_mode_draw
|| emulation
)
5309 draw_primitive_immediate_mode(wined3d_context_gl(context
), state
, stream_info
, idx_data
,
5310 idx_size
, parameters
->u
.direct
.base_vertex_idx
,
5311 parameters
->u
.direct
.start_idx
, parameters
->u
.direct
.index_count
, instance_count
);
5313 wined3d_context_gl_draw_primitive_arrays(context_gl
, state
, idx_data
, idx_size
,
5314 parameters
->u
.direct
.base_vertex_idx
, parameters
->u
.direct
.start_idx
,
5315 parameters
->u
.direct
.index_count
, parameters
->u
.direct
.start_instance
, instance_count
);
5318 if (context
->uses_uavs
)
5320 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS
));
5321 checkGLcall("glMemoryBarrier");
5324 if (rasterizer_discard
)
5326 glDisable(GL_RASTERIZER_DISCARD
);
5327 checkGLcall("disable rasterizer discard");
5330 context_release(context
);
5332 TRACE("Draw completed.\n");
5335 void wined3d_context_gl_unload_tex_coords(const struct wined3d_context_gl
*context_gl
)
5337 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5338 unsigned int texture_idx
;
5340 for (texture_idx
= 0; texture_idx
< gl_info
->limits
.texture_coords
; ++texture_idx
)
5342 gl_info
->gl_ops
.ext
.p_glClientActiveTextureARB(GL_TEXTURE0_ARB
+ texture_idx
);
5343 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
5347 static const void *get_vertex_attrib_pointer(const struct wined3d_stream_info_element
*element
,
5348 const struct wined3d_state
*state
)
5350 const uint8_t *offset
= element
->data
.addr
+ state
->load_base_vertex_index
* element
->stride
;
5352 if (element
->data
.buffer_object
)
5353 offset
+= element
->data
.buffer_object
->buffer_offset
;
5357 void wined3d_context_gl_load_tex_coords(const struct wined3d_context_gl
*context_gl
,
5358 const struct wined3d_stream_info
*si
, GLuint
*current_bo
, const struct wined3d_state
*state
)
5360 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5361 const struct wined3d_format_gl
*format_gl
;
5362 unsigned int mapped_stage
= 0;
5363 unsigned int texture_idx
;
5366 for (texture_idx
= 0; texture_idx
< context_gl
->c
.d3d_info
->ffp_fragment_caps
.max_blend_stages
; ++texture_idx
)
5368 unsigned int coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
5370 if ((mapped_stage
= context_gl
->tex_unit_map
[texture_idx
]) == WINED3D_UNMAPPED_STAGE
)
5373 if (mapped_stage
>= gl_info
->limits
.texture_coords
)
5375 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage
);
5379 if (coord_idx
< WINED3D_MAX_FFP_TEXTURES
&& (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
))))
5381 const struct wined3d_stream_info_element
*e
= &si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
];
5383 TRACE("Setting up texture %u, idx %u, coord_idx %u, data %s.\n",
5384 texture_idx
, mapped_stage
, coord_idx
, debug_bo_address(&e
->data
));
5386 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5387 if (*current_bo
!= bo
)
5389 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5390 checkGLcall("glBindBuffer");
5394 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB
+ mapped_stage
));
5395 checkGLcall("glClientActiveTextureARB");
5397 /* The coords to supply depend completely on the fvf/vertex shader. */
5398 format_gl
= wined3d_format_gl(e
->format
);
5399 gl_info
->gl_ops
.gl
.p_glTexCoordPointer(format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
,
5400 get_vertex_attrib_pointer(e
, state
));
5401 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
5403 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5407 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ mapped_stage
, 0, 0, 0, 1));
5410 if (gl_info
->supported
[NV_REGISTER_COMBINERS
])
5412 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
5413 for (texture_idx
= mapped_stage
+ 1; texture_idx
< gl_info
->limits
.ffp_textures
; ++texture_idx
)
5415 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_idx
, 0, 0, 0, 1));
5419 checkGLcall("loadTexCoords");
5422 /* This should match any arrays loaded in wined3d_context_gl_load_vertex_data(). */
5423 static void wined3d_context_gl_unload_vertex_data(struct wined3d_context_gl
*context_gl
)
5425 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5427 if (!context_gl
->c
.namedArraysLoaded
)
5429 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_VERTEX_ARRAY
);
5430 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_NORMAL_ARRAY
);
5431 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_COLOR_ARRAY
);
5432 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5433 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT
);
5434 wined3d_context_gl_unload_tex_coords(context_gl
);
5435 context_gl
->c
.namedArraysLoaded
= FALSE
;
5438 static void wined3d_context_gl_load_vertex_data(struct wined3d_context_gl
*context_gl
,
5439 const struct wined3d_stream_info
*si
, const struct wined3d_state
*state
)
5441 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5442 const struct wined3d_stream_info_element
*e
;
5443 const struct wined3d_format_gl
*format_gl
;
5444 GLuint current_bo
, bo
;
5447 TRACE("context_gl %p, si %p, state %p.\n", context_gl
, si
, state
);
5449 /* This is used for the fixed-function pipeline only, and the
5450 * fixed-function pipeline doesn't do instancing. */
5451 current_bo
= gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
] ? ~0u : 0;
5454 if ((si
->use_map
& (1u << WINED3D_FFP_BLENDWEIGHT
))
5455 || si
->use_map
& (1u << WINED3D_FFP_BLENDINDICES
))
5457 /* TODO: Support vertex blending in immediate mode draws. No need to
5458 * write a FIXME here, this is done after the general vertex
5459 * declaration decoding. */
5460 WARN("Vertex blending not supported.\n");
5464 if (si
->use_map
& (1u << WINED3D_FFP_PSIZE
))
5466 /* No such functionality in the fixed-function GL pipeline. */
5467 WARN("Per-vertex point size not supported.\n");
5471 if (si
->use_map
& (1u << WINED3D_FFP_POSITION
))
5473 e
= &si
->elements
[WINED3D_FFP_POSITION
];
5474 format_gl
= wined3d_format_gl(e
->format
);
5475 offset
= get_vertex_attrib_pointer(e
, state
);
5477 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5478 if (current_bo
!= bo
)
5480 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5481 checkGLcall("glBindBuffer");
5485 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n", format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5486 gl_info
->gl_ops
.gl
.p_glVertexPointer(format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5487 checkGLcall("glVertexPointer(...)");
5488 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_VERTEX_ARRAY
);
5489 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5491 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5495 if (si
->use_map
& (1u << WINED3D_FFP_NORMAL
))
5497 e
= &si
->elements
[WINED3D_FFP_NORMAL
];
5498 format_gl
= wined3d_format_gl(e
->format
);
5499 offset
= get_vertex_attrib_pointer(e
, state
);
5501 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5502 if (current_bo
!= bo
)
5504 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5505 checkGLcall("glBindBuffer");
5509 TRACE("glNormalPointer(%#x, %#x, %p);\n", format_gl
->vtx_type
, e
->stride
, offset
);
5510 gl_info
->gl_ops
.gl
.p_glNormalPointer(format_gl
->vtx_type
, e
->stride
, offset
);
5511 checkGLcall("glNormalPointer(...)");
5512 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_NORMAL_ARRAY
);
5513 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5515 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5519 gl_info
->gl_ops
.gl
.p_glNormal3f(0, 0, 0);
5520 checkGLcall("glNormal3f(0, 0, 0)");
5523 /* Diffuse colour */
5524 if (si
->use_map
& (1u << WINED3D_FFP_DIFFUSE
))
5526 e
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
5527 format_gl
= wined3d_format_gl(e
->format
);
5528 offset
= get_vertex_attrib_pointer(e
, state
);
5530 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5531 if (current_bo
!= bo
)
5533 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5534 checkGLcall("glBindBuffer");
5538 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5539 format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5540 gl_info
->gl_ops
.gl
.p_glColorPointer(format_gl
->vtx_format
, format_gl
->vtx_type
, e
->stride
, offset
);
5541 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5542 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_COLOR_ARRAY
);
5543 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5545 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5549 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
5550 checkGLcall("glColor4f(1, 1, 1, 1)");
5553 /* Specular colour */
5554 if (si
->use_map
& (1u << WINED3D_FFP_SPECULAR
))
5556 TRACE("Setting specular colour.\n");
5558 e
= &si
->elements
[WINED3D_FFP_SPECULAR
];
5559 offset
= get_vertex_attrib_pointer(e
, state
);
5561 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5566 format_gl
= wined3d_format_gl(e
->format
);
5567 type
= format_gl
->vtx_type
;
5568 format
= format_gl
->vtx_format
;
5570 bo
= wined3d_bo_gl_id(e
->data
.buffer_object
);
5571 if (current_bo
!= bo
)
5573 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5574 checkGLcall("glBindBuffer");
5578 if (format
!= 4 || (gl_info
->quirks
& WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA
))
5580 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5581 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5582 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5583 * 4 component secondary colors use it
5585 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format
, type
, e
->stride
, offset
);
5586 GL_EXTCALL(glSecondaryColorPointerEXT(format
, type
, e
->stride
, offset
));
5587 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5593 case GL_UNSIGNED_BYTE
:
5594 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e
->stride
, offset
);
5595 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE
, e
->stride
, offset
));
5596 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5600 FIXME("Add 4 component specular colour pointers for type %#x.\n", type
);
5601 /* Make sure that the right colour component is dropped. */
5602 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type
, e
->stride
, offset
);
5603 GL_EXTCALL(glSecondaryColorPointerEXT(3, type
, e
->stride
, offset
));
5604 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5607 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT
);
5608 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5610 wined3d_buffer_validate_user(state
->streams
[e
->stream_idx
].buffer
);
5614 WARN("Specular colour is not supported in this GL implementation.\n");
5619 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5621 GL_EXTCALL(glSecondaryColor3fEXT
)(0, 0, 0);
5622 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5626 WARN("Specular colour is not supported in this GL implementation.\n");
5630 /* Texture coordinates */
5631 wined3d_context_gl_load_tex_coords(context_gl
, si
, ¤t_bo
, state
);
5634 static void wined3d_context_gl_unload_numbered_array(struct wined3d_context_gl
*context_gl
, unsigned int i
)
5636 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5638 GL_EXTCALL(glDisableVertexAttribArray(i
));
5639 checkGLcall("glDisableVertexAttribArray");
5640 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
5641 GL_EXTCALL(glVertexAttribDivisor(i
, 0));
5643 context_gl
->c
.numbered_array_mask
&= ~(1u << i
);
5646 static void wined3d_context_gl_unload_numbered_arrays(struct wined3d_context_gl
*context_gl
)
5648 uint32_t mask
= context_gl
->c
.numbered_array_mask
;
5653 i
= wined3d_bit_scan(&mask
);
5654 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5658 static void wined3d_context_gl_load_numbered_arrays(struct wined3d_context_gl
*context_gl
,
5659 const struct wined3d_stream_info
*stream_info
, const struct wined3d_state
*state
)
5661 struct wined3d_context
*context
= &context_gl
->c
;
5662 const struct wined3d_shader
*vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
5663 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5664 GLuint current_bo
, bo
;
5667 /* Default to no instancing. */
5668 current_bo
= gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
] ? ~0u : 0;
5670 if (stream_info
->use_map
& ~wined3d_mask_from_size(gl_info
->limits
.vertex_attribs
))
5672 static unsigned int once
;
5675 FIXME("More than the supported %u vertex attributes are in use.\n", gl_info
->limits
.vertex_attribs
);
5678 for (i
= 0; i
< gl_info
->limits
.vertex_attribs
; ++i
)
5680 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[i
];
5681 const struct wined3d_stream_state
*stream
;
5682 const struct wined3d_format_gl
*format_gl
;
5684 if (!(stream_info
->use_map
& (1u << i
)))
5686 if (context
->numbered_array_mask
& (1u << i
))
5687 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5688 if (!use_vs(state
) && i
== WINED3D_FFP_DIFFUSE
)
5690 if (!(context_gl
->default_attrib_value_set
& (1u << i
)) || !context_gl
->diffuse_attrib_to_1
)
5692 GL_EXTCALL(glVertexAttrib4f(i
, 1.0f
, 1.0f
, 1.0f
, 1.0f
));
5693 context_gl
->diffuse_attrib_to_1
= 1;
5698 if (!(context_gl
->default_attrib_value_set
& (1u << i
)))
5700 GL_EXTCALL(glVertexAttrib4f(i
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
5701 if (i
== WINED3D_FFP_DIFFUSE
)
5702 context_gl
->diffuse_attrib_to_1
= 0;
5705 context_gl
->default_attrib_value_set
|= 1u << i
;
5709 format_gl
= wined3d_format_gl(element
->format
);
5710 stream
= &state
->streams
[element
->stream_idx
];
5712 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
5714 unsigned int divisor
= 0;
5716 if (element
->instanced
)
5717 divisor
= element
->divisor
? element
->divisor
: UINT_MAX
;
5718 GL_EXTCALL(glVertexAttribDivisor(i
, divisor
));
5720 else if (element
->divisor
)
5722 /* Unload instanced arrays, they will be loaded using immediate
5724 if (context
->numbered_array_mask
& (1u << i
))
5725 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5726 context_gl
->default_attrib_value_set
&= ~(1u << i
);
5730 TRACE("Loading array %u %s.\n", i
, debug_bo_address(&element
->data
));
5732 if (element
->stride
)
5734 const void *offset
= get_vertex_attrib_pointer(element
, state
);
5735 unsigned int format_attrs
= format_gl
->f
.attrs
;
5737 bo
= wined3d_bo_gl_id(element
->data
.buffer_object
);
5738 if (current_bo
!= bo
)
5740 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, bo
));
5741 checkGLcall("glBindBuffer");
5745 wined3d_buffer_validate_user(stream
->buffer
);
5747 /* Use the VBO to find out if a vertex buffer exists, not the vb
5748 * pointer. vb can point to a user pointer data blob. In that case
5749 * current_bo will be 0. If there is a vertex buffer but no vbo we
5750 * won't be load converted attributes anyway. */
5751 if (vs
&& vs
->reg_maps
.shader_version
.major
>= 4 && (format_attrs
& WINED3D_FORMAT_ATTR_INTEGER
))
5753 GL_EXTCALL(glVertexAttribIPointer(i
, format_gl
->vtx_format
,
5754 format_gl
->vtx_type
, element
->stride
, offset
));
5758 GL_EXTCALL(glVertexAttribPointer(i
, format_gl
->vtx_format
, format_gl
->vtx_type
,
5759 !!(format_attrs
& WINED3D_FORMAT_ATTR_NORMALISED
), element
->stride
, offset
));
5762 if (!(context
->numbered_array_mask
& (1u << i
)))
5764 GL_EXTCALL(glEnableVertexAttribArray(i
));
5765 context
->numbered_array_mask
|= (1u << i
);
5770 /* Stride = 0 means always the same values.
5771 * glVertexAttribPointer() doesn't do that. Instead disable the
5772 * pointer and set up the attribute statically. But we have to
5773 * figure out the system memory address. */
5774 const BYTE
*ptr
= element
->data
.addr
;
5775 if (element
->data
.buffer_object
)
5776 ptr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(stream
->buffer
, context
);
5778 if (context
->numbered_array_mask
& (1u << i
))
5779 wined3d_context_gl_unload_numbered_array(context_gl
, i
);
5781 switch (format_gl
->f
.id
)
5783 case WINED3DFMT_R32_FLOAT
:
5784 GL_EXTCALL(glVertexAttrib1fv(i
, (const GLfloat
*)ptr
));
5786 case WINED3DFMT_R32G32_FLOAT
:
5787 GL_EXTCALL(glVertexAttrib2fv(i
, (const GLfloat
*)ptr
));
5789 case WINED3DFMT_R32G32B32_FLOAT
:
5790 GL_EXTCALL(glVertexAttrib3fv(i
, (const GLfloat
*)ptr
));
5792 case WINED3DFMT_R32G32B32A32_FLOAT
:
5793 GL_EXTCALL(glVertexAttrib4fv(i
, (const GLfloat
*)ptr
));
5795 case WINED3DFMT_R8G8B8A8_UINT
:
5796 GL_EXTCALL(glVertexAttrib4ubv(i
, ptr
));
5798 case WINED3DFMT_B8G8R8A8_UNORM
:
5799 if (gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
])
5801 const DWORD
*src
= (const DWORD
*)ptr
;
5802 DWORD c
= *src
& 0xff00ff00u
;
5803 c
|= (*src
& 0xff0000u
) >> 16;
5804 c
|= (*src
& 0xffu
) << 16;
5805 GL_EXTCALL(glVertexAttrib4Nubv(i
, (GLubyte
*)&c
));
5808 /* else fallthrough */
5809 case WINED3DFMT_R8G8B8A8_UNORM
:
5810 GL_EXTCALL(glVertexAttrib4Nubv(i
, ptr
));
5812 case WINED3DFMT_R16G16_SINT
:
5813 GL_EXTCALL(glVertexAttrib2sv(i
, (const GLshort
*)ptr
));
5815 case WINED3DFMT_R16G16B16A16_SINT
:
5816 GL_EXTCALL(glVertexAttrib4sv(i
, (const GLshort
*)ptr
));
5818 case WINED3DFMT_R16G16_SNORM
:
5820 const GLshort s
[4] = {((const GLshort
*)ptr
)[0], ((const GLshort
*)ptr
)[1], 0, 1};
5821 GL_EXTCALL(glVertexAttrib4Nsv(i
, s
));
5824 case WINED3DFMT_R16G16_UNORM
:
5826 const GLushort s
[4] = {((const GLushort
*)ptr
)[0], ((const GLushort
*)ptr
)[1], 0, 1};
5827 GL_EXTCALL(glVertexAttrib4Nusv(i
, s
));
5830 case WINED3DFMT_R16G16B16A16_SNORM
:
5831 GL_EXTCALL(glVertexAttrib4Nsv(i
, (const GLshort
*)ptr
));
5833 case WINED3DFMT_R16G16B16A16_UNORM
:
5834 GL_EXTCALL(glVertexAttrib4Nusv(i
, (const GLushort
*)ptr
));
5836 case WINED3DFMT_R10G10B10X2_UINT
:
5837 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5838 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5840 case WINED3DFMT_R10G10B10X2_SNORM
:
5841 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5842 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5844 case WINED3DFMT_R16G16_FLOAT
:
5845 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
5847 /* Not supported by GL_ARB_half_float_vertex. */
5848 GL_EXTCALL(glVertexAttrib2hvNV(i
, (const GLhalfNV
*)ptr
));
5852 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
5853 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
5854 GL_EXTCALL(glVertexAttrib2f(i
, x
, y
));
5857 case WINED3DFMT_R16G16B16A16_FLOAT
:
5858 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
5860 /* Not supported by GL_ARB_half_float_vertex. */
5861 GL_EXTCALL(glVertexAttrib4hvNV(i
, (const GLhalfNV
*)ptr
));
5865 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
5866 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
5867 float z
= float_16_to_32(((const unsigned short *)ptr
) + 2);
5868 float w
= float_16_to_32(((const unsigned short *)ptr
) + 3);
5869 GL_EXTCALL(glVertexAttrib4f(i
, x
, y
, z
, w
));
5873 ERR("Unexpected declaration in stride 0 attributes.\n");
5877 context_gl
->default_attrib_value_set
&= ~(1u << i
);
5880 checkGLcall("Loading numbered arrays");
5883 void wined3d_context_gl_update_stream_sources(struct wined3d_context_gl
*context_gl
,
5884 const struct wined3d_state
*state
)
5886 if (context_gl
->c
.use_immediate_mode_draw
)
5889 wined3d_context_gl_unload_vertex_data(context_gl
);
5890 if (context_gl
->c
.d3d_info
->ffp_generic_attributes
|| use_vs(state
))
5892 TRACE("Loading numbered arrays.\n");
5893 wined3d_context_gl_load_numbered_arrays(context_gl
, &context_gl
->c
.stream_info
, state
);
5897 TRACE("Loading named arrays.\n");
5898 wined3d_context_gl_unload_numbered_arrays(context_gl
);
5899 wined3d_context_gl_load_vertex_data(context_gl
, &context_gl
->c
.stream_info
, state
);
5900 context_gl
->c
.namedArraysLoaded
= TRUE
;
5903 static void apply_texture_blit_state(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*texture
,
5904 GLenum target
, unsigned int level
, enum wined3d_texture_filter_type filter
)
5906 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(filter
));
5907 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
5908 wined3d_gl_min_mip_filter(filter
, WINED3D_TEXF_NONE
));
5909 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
5910 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
5911 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
5912 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
);
5913 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, level
);
5915 /* We changed the filtering settings on the texture. Make sure they get
5916 * reset on subsequent draws. */
5917 texture
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
5918 texture
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
5919 texture
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
5920 texture
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
5921 texture
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
5922 texture
->sampler_desc
.srgb_decode
= FALSE
;
5923 texture
->sampler_desc
.mip_base_level
= level
;
5926 /* Context activation is done by the caller. */
5927 void wined3d_context_gl_draw_shaded_quad(struct wined3d_context_gl
*context_gl
, struct wined3d_texture_gl
*texture_gl
,
5928 unsigned int sub_resource_idx
, const RECT
*src_rect
, const RECT
*dst_rect
,
5929 enum wined3d_texture_filter_type filter
)
5931 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
5932 struct wined3d_blt_info info
;
5933 unsigned int level
, w
, h
, i
;
5938 struct wined3d_vec3 texcoord
;
5942 texture2d_get_blt_info(texture_gl
, sub_resource_idx
, src_rect
, &info
);
5944 level
= sub_resource_idx
% texture_gl
->t
.level_count
;
5945 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, texture_gl
->texture_rgb
.name
);
5946 apply_texture_blit_state(gl_info
, &texture_gl
->texture_rgb
, info
.bind_target
, level
, filter
);
5947 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, level
);
5949 wined3d_context_gl_pause_transform_feedback(context_gl
, FALSE
);
5951 wined3d_context_gl_get_rt_size(context_gl
, &dst_size
);
5955 quad
[0].x
= dst_rect
->left
* 2.0f
/ w
- 1.0f
;
5956 quad
[0].y
= dst_rect
->top
* 2.0f
/ h
- 1.0f
;
5957 quad
[0].texcoord
= info
.texcoords
[0];
5959 quad
[1].x
= dst_rect
->right
* 2.0f
/ w
- 1.0f
;
5960 quad
[1].y
= dst_rect
->top
* 2.0f
/ h
- 1.0f
;
5961 quad
[1].texcoord
= info
.texcoords
[1];
5963 quad
[2].x
= dst_rect
->left
* 2.0f
/ w
- 1.0f
;
5964 quad
[2].y
= dst_rect
->bottom
* 2.0f
/ h
- 1.0f
;
5965 quad
[2].texcoord
= info
.texcoords
[2];
5967 quad
[3].x
= dst_rect
->right
* 2.0f
/ w
- 1.0f
;
5968 quad
[3].y
= dst_rect
->bottom
* 2.0f
/ h
- 1.0f
;
5969 quad
[3].texcoord
= info
.texcoords
[3];
5972 if (gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
5974 if (!context_gl
->blit_vbo
)
5975 GL_EXTCALL(glGenBuffers(1, &context_gl
->blit_vbo
));
5976 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, context_gl
->blit_vbo
));
5978 wined3d_context_gl_unload_vertex_data(context_gl
);
5979 wined3d_context_gl_unload_numbered_arrays(context_gl
);
5981 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER
, sizeof(quad
), quad
, GL_STREAM_DRAW
));
5982 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT
, FALSE
, sizeof(*quad
), NULL
));
5983 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT
, FALSE
, sizeof(*quad
),
5984 (void *)FIELD_OFFSET(struct blit_vertex
, texcoord
)));
5986 GL_EXTCALL(glEnableVertexAttribArray(0));
5987 GL_EXTCALL(glEnableVertexAttribArray(1));
5989 gl_info
->gl_ops
.gl
.p_glDrawArrays(GL_TRIANGLE_STRIP
, 0, 4);
5991 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, 0));
5992 GL_EXTCALL(glDisableVertexAttribArray(1));
5993 GL_EXTCALL(glDisableVertexAttribArray(0));
5997 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
5999 for (i
= 0; i
< ARRAY_SIZE(quad
); ++i
)
6001 GL_EXTCALL(glVertexAttrib3fv(1, &quad
[i
].texcoord
.x
));
6002 GL_EXTCALL(glVertexAttrib2fv(0, &quad
[i
].x
));
6005 gl_info
->gl_ops
.gl
.p_glEnd();
6007 checkGLcall("draw");
6009 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, texture_gl
->t
.level_count
- 1);
6010 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, 0);
6013 /* Context activation is done by the caller. */
6014 void wined3d_context_gl_draw_textured_quad(struct wined3d_context_gl
*context_gl
,
6015 struct wined3d_texture_gl
*texture_gl
, unsigned int sub_resource_idx
,
6016 const RECT
*src_rect
, const RECT
*dst_rect
, enum wined3d_texture_filter_type filter
)
6018 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
6019 struct wined3d_blt_info info
;
6022 texture2d_get_blt_info(texture_gl
, sub_resource_idx
, src_rect
, &info
);
6024 gl_info
->gl_ops
.gl
.p_glEnable(info
.bind_target
);
6025 checkGLcall("glEnable(bind_target)");
6027 level
= sub_resource_idx
% texture_gl
->t
.level_count
;
6028 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, texture_gl
->texture_rgb
.name
);
6029 apply_texture_blit_state(gl_info
, &texture_gl
->texture_rgb
, info
.bind_target
, level
, filter
);
6030 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, level
);
6031 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
6032 checkGLcall("glTexEnvi");
6034 wined3d_context_gl_pause_transform_feedback(context_gl
, FALSE
);
6037 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
6038 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[0].x
);
6039 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->top
);
6041 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[1].x
);
6042 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->top
);
6044 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[2].x
);
6045 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->bottom
);
6047 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[3].x
);
6048 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->bottom
);
6049 gl_info
->gl_ops
.gl
.p_glEnd();
6051 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, texture_gl
->t
.level_count
- 1);
6052 wined3d_context_gl_bind_texture(context_gl
, info
.bind_target
, 0);