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
28 #include "wine/port.h"
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous
);
41 #define WINED3D_MAX_FBO_ENTRIES 64
42 #define WINED3D_ALL_LAYERS (~0u)
44 static DWORD wined3d_context_tls_idx
;
46 /* FBO helper functions */
48 /* Context activation is done by the caller. */
49 static void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint fbo
)
51 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
55 case GL_READ_FRAMEBUFFER
:
56 if (context
->fbo_read_binding
== fbo
) return;
57 context
->fbo_read_binding
= fbo
;
60 case GL_DRAW_FRAMEBUFFER
:
61 if (context
->fbo_draw_binding
== fbo
) return;
62 context
->fbo_draw_binding
= fbo
;
66 if (context
->fbo_read_binding
== fbo
67 && context
->fbo_draw_binding
== fbo
) return;
68 context
->fbo_read_binding
= fbo
;
69 context
->fbo_draw_binding
= fbo
;
73 FIXME("Unhandled target %#x.\n", target
);
77 gl_info
->fbo_ops
.glBindFramebuffer(target
, fbo
);
78 checkGLcall("glBindFramebuffer()");
81 /* Context activation is done by the caller. */
82 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
, GLenum target
)
86 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
88 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
89 checkGLcall("glFramebufferTexture2D()");
91 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
92 checkGLcall("glFramebufferTexture2D()");
94 gl_info
->fbo_ops
.glFramebufferTexture2D(target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
95 checkGLcall("glFramebufferTexture2D()");
98 /* Context activation is done by the caller. */
99 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint fbo
)
101 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
103 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
104 context_clean_fbo_attachments(gl_info
, GL_FRAMEBUFFER
);
105 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
107 gl_info
->fbo_ops
.glDeleteFramebuffers(1, &fbo
);
108 checkGLcall("glDeleteFramebuffers()");
111 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info
*gl_info
,
112 GLenum fbo_target
, DWORD flags
, GLuint rb
)
114 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
116 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
117 checkGLcall("glFramebufferRenderbuffer()");
120 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
122 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rb
);
123 checkGLcall("glFramebufferRenderbuffer()");
127 static void context_attach_gl_texture_fbo(struct wined3d_context
*context
,
128 GLenum fbo_target
, GLenum attachment
, const struct wined3d_fbo_resource
*resource
)
130 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
134 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
, GL_TEXTURE_2D
, 0, 0);
136 else if (resource
->layer
== WINED3D_ALL_LAYERS
)
138 if (!gl_info
->fbo_ops
.glFramebufferTexture
)
140 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
144 gl_info
->fbo_ops
.glFramebufferTexture(fbo_target
, attachment
,
145 resource
->object
, resource
->level
);
147 else if (resource
->target
== GL_TEXTURE_1D_ARRAY
|| resource
->target
== GL_TEXTURE_2D_ARRAY
148 || resource
->target
== GL_TEXTURE_3D
)
150 if (!gl_info
->fbo_ops
.glFramebufferTextureLayer
)
152 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
156 gl_info
->fbo_ops
.glFramebufferTextureLayer(fbo_target
, attachment
,
157 resource
->object
, resource
->level
, resource
->layer
);
159 else if (resource
->target
== GL_TEXTURE_1D
)
161 gl_info
->fbo_ops
.glFramebufferTexture1D(fbo_target
, attachment
,
162 resource
->target
, resource
->object
, resource
->level
);
166 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, attachment
,
167 resource
->target
, resource
->object
, resource
->level
);
169 checkGLcall("attach texture to fbo");
172 /* Context activation is done by the caller. */
173 static void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
174 GLenum fbo_target
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
,
177 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
179 if (resource
->object
)
181 TRACE("Attach depth stencil %u.\n", resource
->object
);
185 context_attach_depth_stencil_rb(gl_info
, fbo_target
,
186 flags
, resource
->object
);
190 if (flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
)
191 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, resource
);
193 if (flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
)
194 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, resource
);
197 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_DEPTH
))
198 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
200 if (!(flags
& WINED3D_FBO_ENTRY_FLAG_STENCIL
))
201 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
205 TRACE("Attach depth stencil 0.\n");
207 context_attach_gl_texture_fbo(context
, fbo_target
, GL_DEPTH_ATTACHMENT
, NULL
);
208 context_attach_gl_texture_fbo(context
, fbo_target
, GL_STENCIL_ATTACHMENT
, NULL
);
212 /* Context activation is done by the caller. */
213 static void context_attach_surface_fbo(struct wined3d_context
*context
,
214 GLenum fbo_target
, DWORD idx
, const struct wined3d_fbo_resource
*resource
, BOOL rb_namespace
)
216 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
218 TRACE("Attach GL object %u to %u.\n", resource
->object
, idx
);
220 if (resource
->object
)
224 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
,
225 GL_RENDERBUFFER
, resource
->object
);
226 checkGLcall("glFramebufferRenderbuffer()");
230 context_attach_gl_texture_fbo(context
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, resource
);
235 context_attach_gl_texture_fbo(context
, fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, NULL
);
239 static void context_dump_fbo_attachment(const struct wined3d_gl_info
*gl_info
, GLenum target
,
247 enum wined3d_gl_extension extension
;
251 {GL_TEXTURE_1D
, GL_TEXTURE_BINDING_1D
, "1d", WINED3D_GL_EXT_NONE
},
252 {GL_TEXTURE_1D_ARRAY
, GL_TEXTURE_BINDING_1D_ARRAY
, "1d-array", EXT_TEXTURE_ARRAY
},
253 {GL_TEXTURE_2D
, GL_TEXTURE_BINDING_2D
, "2d", WINED3D_GL_EXT_NONE
},
254 {GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_BINDING_RECTANGLE_ARB
, "rectangle", ARB_TEXTURE_RECTANGLE
},
255 {GL_TEXTURE_2D_ARRAY
, GL_TEXTURE_BINDING_2D_ARRAY
, "2d-array" , EXT_TEXTURE_ARRAY
},
256 {GL_TEXTURE_CUBE_MAP
, GL_TEXTURE_BINDING_CUBE_MAP
, "cube", ARB_TEXTURE_CUBE_MAP
},
257 {GL_TEXTURE_2D_MULTISAMPLE
, GL_TEXTURE_BINDING_2D_MULTISAMPLE
, "2d-ms", ARB_TEXTURE_MULTISAMPLE
},
258 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY
, "2d-array-ms", ARB_TEXTURE_MULTISAMPLE
},
261 GLint type
, name
, samples
, width
, height
, old_texture
, level
, face
, fmt
, tex_target
;
262 const char *tex_type_str
;
265 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
266 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME
, &name
);
267 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
268 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE
, &type
);
270 if (type
== GL_RENDERBUFFER
)
272 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, name
);
273 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_WIDTH
, &width
);
274 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_HEIGHT
, &height
);
275 if (gl_info
->limits
.samples
> 1)
276 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_SAMPLES
, &samples
);
279 gl_info
->fbo_ops
.glGetRenderbufferParameteriv(GL_RENDERBUFFER
, GL_RENDERBUFFER_INTERNAL_FORMAT
, &fmt
);
280 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
281 debug_fboattachment(attachment
), name
, width
, height
, samples
, fmt
);
283 else if (type
== GL_TEXTURE
)
285 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
286 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL
, &level
);
287 gl_info
->fbo_ops
.glGetFramebufferAttachmentParameteriv(target
, attachment
,
288 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE
, &face
);
290 if (gl_info
->gl_ops
.ext
.p_glGetTextureParameteriv
)
292 GL_EXTCALL(glGetTextureParameteriv(name
, GL_TEXTURE_TARGET
, &tex_target
));
294 for (i
= 0; i
< ARRAY_SIZE(texture_type
); ++i
)
296 if (texture_type
[i
].target
== tex_target
)
298 tex_type_str
= texture_type
[i
].str
;
302 if (i
== ARRAY_SIZE(texture_type
))
303 tex_type_str
= wine_dbg_sprintf("%#x", tex_target
);
307 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP
, &old_texture
);
308 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, name
);
310 tex_target
= GL_TEXTURE_CUBE_MAP
;
311 tex_type_str
= "cube";
317 for (i
= 0; i
< ARRAY_SIZE(texture_type
); ++i
)
319 if (!gl_info
->supported
[texture_type
[i
].extension
])
322 gl_info
->gl_ops
.gl
.p_glGetIntegerv(texture_type
[i
].binding
, &old_texture
);
323 while (gl_info
->gl_ops
.gl
.p_glGetError());
325 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, name
);
326 if (!gl_info
->gl_ops
.gl
.p_glGetError())
328 tex_target
= texture_type
[i
].target
;
329 tex_type_str
= texture_type
[i
].str
;
332 gl_info
->gl_ops
.gl
.p_glBindTexture(texture_type
[i
].target
, old_texture
);
337 FIXME("Cannot find type of texture %d.\n", name
);
342 if (gl_info
->gl_ops
.ext
.p_glGetTextureParameteriv
)
344 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
));
345 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_WIDTH
, &width
));
346 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_HEIGHT
, &height
));
347 GL_EXTCALL(glGetTextureLevelParameteriv(name
, level
, GL_TEXTURE_SAMPLES
, &samples
));
351 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_INTERNAL_FORMAT
, &fmt
);
352 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_WIDTH
, &width
);
353 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_HEIGHT
, &height
);
354 if (gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
355 gl_info
->gl_ops
.gl
.p_glGetTexLevelParameteriv(tex_target
, level
, GL_TEXTURE_SAMPLES
, &samples
);
359 gl_info
->gl_ops
.gl
.p_glBindTexture(tex_target
, old_texture
);
362 FIXME(" %s: %s texture %d, %dx%d, %d samples, format %#x.\n",
363 debug_fboattachment(attachment
), tex_type_str
, name
, width
, height
, samples
, fmt
);
365 else if (type
== GL_NONE
)
367 FIXME(" %s: NONE.\n", debug_fboattachment(attachment
));
371 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment
), type
);
374 checkGLcall("dump FBO attachment");
377 /* Context activation is done by the caller. */
378 void context_check_fbo_status(const struct wined3d_context
*context
, GLenum target
)
380 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
386 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(target
);
387 if (status
== GL_FRAMEBUFFER_COMPLETE
)
389 TRACE("FBO complete.\n");
395 FIXME("FBO status %s (%#x).\n", debug_fbostatus(status
), status
);
397 if (!context
->current_fbo
)
399 ERR("FBO 0 is incomplete, driver bug?\n");
403 context_dump_fbo_attachment(gl_info
, target
, GL_DEPTH_ATTACHMENT
);
404 context_dump_fbo_attachment(gl_info
, target
, GL_STENCIL_ATTACHMENT
);
406 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
407 context_dump_fbo_attachment(gl_info
, target
, GL_COLOR_ATTACHMENT0
+ i
);
411 static inline DWORD
context_generate_rt_mask(GLenum buffer
)
413 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
414 return buffer
? (1u << 31) | buffer
: 0;
417 static inline DWORD
context_generate_rt_mask_from_resource(struct wined3d_resource
*resource
)
419 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
421 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
425 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource
));
428 static inline void context_set_fbo_key_for_render_target(const struct wined3d_context
*context
,
429 struct wined3d_fbo_entry_key
*key
, unsigned int idx
, const struct wined3d_rendertarget_info
*render_target
,
432 unsigned int sub_resource_idx
= render_target
->sub_resource_idx
;
433 struct wined3d_resource
*resource
= render_target
->resource
;
434 struct wined3d_texture
*texture
;
436 if (!resource
|| resource
->format
->id
== WINED3DFMT_NULL
|| resource
->type
== WINED3D_RTYPE_BUFFER
)
438 if (resource
&& resource
->type
== WINED3D_RTYPE_BUFFER
)
439 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource
->type
));
440 key
->objects
[idx
].object
= 0;
441 key
->objects
[idx
].target
= 0;
442 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
446 if (render_target
->gl_view
.name
)
448 key
->objects
[idx
].object
= render_target
->gl_view
.name
;
449 key
->objects
[idx
].target
= render_target
->gl_view
.target
;
450 key
->objects
[idx
].level
= 0;
451 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
455 texture
= wined3d_texture_from_resource(resource
);
456 if (texture
->current_renderbuffer
)
458 key
->objects
[idx
].object
= texture
->current_renderbuffer
->id
;
459 key
->objects
[idx
].target
= 0;
460 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
461 key
->rb_namespace
|= 1 << idx
;
465 key
->objects
[idx
].target
= wined3d_texture_get_sub_resource_target(texture
, sub_resource_idx
);
466 key
->objects
[idx
].level
= sub_resource_idx
% texture
->level_count
;
467 key
->objects
[idx
].layer
= sub_resource_idx
/ texture
->level_count
;
469 if (render_target
->layer_count
!= 1)
470 key
->objects
[idx
].layer
= WINED3D_ALL_LAYERS
;
474 case WINED3D_LOCATION_TEXTURE_RGB
:
475 key
->objects
[idx
].object
= wined3d_texture_get_texture_name(texture
, context
, FALSE
);
478 case WINED3D_LOCATION_TEXTURE_SRGB
:
479 key
->objects
[idx
].object
= wined3d_texture_get_texture_name(texture
, context
, TRUE
);
482 case WINED3D_LOCATION_RB_MULTISAMPLE
:
483 key
->objects
[idx
].object
= texture
->rb_multisample
;
484 key
->objects
[idx
].target
= 0;
485 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
486 key
->rb_namespace
|= 1 << idx
;
489 case WINED3D_LOCATION_RB_RESOLVED
:
490 key
->objects
[idx
].object
= texture
->rb_resolved
;
491 key
->objects
[idx
].target
= 0;
492 key
->objects
[idx
].level
= key
->objects
[idx
].layer
= 0;
493 key
->rb_namespace
|= 1 << idx
;
498 static void context_generate_fbo_key(const struct wined3d_context
*context
,
499 struct wined3d_fbo_entry_key
*key
, const struct wined3d_rendertarget_info
*render_targets
,
500 const struct wined3d_rendertarget_info
*depth_stencil
, DWORD color_location
, DWORD ds_location
)
502 unsigned int buffers
= context
->gl_info
->limits
.buffers
;
505 key
->rb_namespace
= 0;
506 context_set_fbo_key_for_render_target(context
, key
, 0, depth_stencil
, ds_location
);
508 for (i
= 0; i
< buffers
; ++i
)
509 context_set_fbo_key_for_render_target(context
, key
, i
+ 1, &render_targets
[i
], color_location
);
511 memset(&key
->objects
[buffers
+ 1], 0, (ARRAY_SIZE(key
->objects
) - buffers
- 1) * sizeof(*key
->objects
));
514 static struct fbo_entry
*context_create_fbo_entry(const struct wined3d_context
*context
,
515 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
516 DWORD color_location
, DWORD ds_location
)
518 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
519 struct fbo_entry
*entry
;
521 entry
= heap_alloc(sizeof(*entry
));
522 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
524 if (depth_stencil
->resource
)
526 if (depth_stencil
->resource
->format_flags
& WINED3DFMT_FLAG_DEPTH
)
527 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
528 if (depth_stencil
->resource
->format_flags
& WINED3DFMT_FLAG_STENCIL
)
529 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
531 entry
->rt_mask
= context_generate_rt_mask(GL_COLOR_ATTACHMENT0
);
532 gl_info
->fbo_ops
.glGenFramebuffers(1, &entry
->id
);
533 checkGLcall("glGenFramebuffers()");
534 TRACE("Created FBO %u.\n", entry
->id
);
539 /* Context activation is done by the caller. */
540 static void context_reuse_fbo_entry(struct wined3d_context
*context
, GLenum target
,
541 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
542 DWORD color_location
, DWORD ds_location
, struct fbo_entry
*entry
)
544 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
546 context_bind_fbo(context
, target
, entry
->id
);
547 context_clean_fbo_attachments(gl_info
, target
);
549 context_generate_fbo_key(context
, &entry
->key
, render_targets
, depth_stencil
, color_location
, ds_location
);
551 if (depth_stencil
->resource
)
553 if (depth_stencil
->resource
->format_flags
& WINED3DFMT_FLAG_DEPTH
)
554 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_DEPTH
;
555 if (depth_stencil
->resource
->format_flags
& WINED3DFMT_FLAG_STENCIL
)
556 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_STENCIL
;
560 /* Context activation is done by the caller. */
561 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
565 TRACE("Destroy FBO %u.\n", entry
->id
);
566 context_destroy_fbo(context
, entry
->id
);
568 --context
->fbo_entry_count
;
569 list_remove(&entry
->entry
);
573 /* Context activation is done by the caller. */
574 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
, GLenum target
,
575 const struct wined3d_rendertarget_info
*render_targets
, const struct wined3d_rendertarget_info
*depth_stencil
,
576 DWORD color_location
, DWORD ds_location
)
578 static const struct wined3d_rendertarget_info ds_null
= {{0}};
579 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
580 struct wined3d_texture
*rt_texture
, *ds_texture
;
581 struct wined3d_fbo_entry_key fbo_key
;
582 unsigned int i
, ds_level
, rt_level
;
583 struct fbo_entry
*entry
;
585 if (depth_stencil
->resource
&& depth_stencil
->resource
->type
!= WINED3D_RTYPE_BUFFER
586 && render_targets
[0].resource
&& render_targets
[0].resource
->type
!= WINED3D_RTYPE_BUFFER
)
588 rt_texture
= wined3d_texture_from_resource(render_targets
[0].resource
);
589 rt_level
= render_targets
[0].sub_resource_idx
% rt_texture
->level_count
;
590 ds_texture
= wined3d_texture_from_resource(depth_stencil
->resource
);
591 ds_level
= depth_stencil
->sub_resource_idx
% ds_texture
->level_count
;
593 if (wined3d_texture_get_level_width(ds_texture
, ds_level
)
594 < wined3d_texture_get_level_width(rt_texture
, rt_level
)
595 || wined3d_texture_get_level_height(ds_texture
, ds_level
)
596 < wined3d_texture_get_level_height(rt_texture
, rt_level
))
598 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
599 depth_stencil
= &ds_null
;
601 else if (ds_texture
->resource
.multisample_type
!= rt_texture
->resource
.multisample_type
602 || ds_texture
->resource
.multisample_quality
!= rt_texture
->resource
.multisample_quality
)
604 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
605 rt_texture
->resource
.multisample_type
, rt_texture
->resource
.multisample_quality
,
606 ds_texture
->resource
.multisample_type
, ds_texture
->resource
.multisample_quality
);
607 depth_stencil
= &ds_null
;
609 else if (depth_stencil
->resource
->type
== WINED3D_RTYPE_TEXTURE_2D
)
611 wined3d_texture_set_compatible_renderbuffer(ds_texture
, ds_level
, &render_targets
[0]);
615 context_generate_fbo_key(context
, &fbo_key
, render_targets
, depth_stencil
, color_location
, ds_location
);
619 struct wined3d_resource
*resource
;
620 unsigned int width
, height
;
621 const char *resource_type
;
623 TRACE("Dumping FBO attachments:\n");
624 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
626 if ((resource
= render_targets
[i
].resource
))
628 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
630 width
= resource
->size
;
632 resource_type
= "buffer";
636 rt_texture
= wined3d_texture_from_resource(resource
);
637 rt_level
= render_targets
[i
].sub_resource_idx
% rt_texture
->level_count
;
638 width
= wined3d_texture_get_level_pow2_width(rt_texture
, rt_level
);
639 height
= wined3d_texture_get_level_pow2_height(rt_texture
, rt_level
);
640 resource_type
= "texture";
643 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
644 i
, resource
, render_targets
[i
].sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
645 fbo_key
.rb_namespace
& (1 << (i
+ 1)) ? "renderbuffer" : resource_type
,
646 fbo_key
.objects
[i
+ 1].object
, width
, height
, resource
->multisample_type
);
649 if ((resource
= depth_stencil
->resource
))
651 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
653 width
= resource
->size
;
655 resource_type
= "buffer";
659 ds_texture
= wined3d_texture_from_resource(resource
);
660 ds_level
= depth_stencil
->sub_resource_idx
% ds_texture
->level_count
;
661 width
= wined3d_texture_get_level_pow2_width(ds_texture
, ds_level
);
662 height
= wined3d_texture_get_level_pow2_height(ds_texture
, ds_level
);
663 resource_type
= "texture";
666 TRACE(" Depth attachment: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
667 resource
, depth_stencil
->sub_resource_idx
, debug_d3dformat(resource
->format
->id
),
668 fbo_key
.rb_namespace
& (1 << 0) ? "renderbuffer" : resource_type
,
669 fbo_key
.objects
[0].object
, width
, height
, resource
->multisample_type
);
673 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
675 if (memcmp(&fbo_key
, &entry
->key
, sizeof(fbo_key
)))
678 list_remove(&entry
->entry
);
679 list_add_head(&context
->fbo_list
, &entry
->entry
);
683 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
685 entry
= context_create_fbo_entry(context
, render_targets
, depth_stencil
, color_location
, ds_location
);
686 list_add_head(&context
->fbo_list
, &entry
->entry
);
687 ++context
->fbo_entry_count
;
691 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
692 context_reuse_fbo_entry(context
, target
, render_targets
, depth_stencil
, color_location
, ds_location
, entry
);
693 list_remove(&entry
->entry
);
694 list_add_head(&context
->fbo_list
, &entry
->entry
);
700 /* Context activation is done by the caller. */
701 static void context_apply_fbo_entry(struct wined3d_context
*context
, GLenum target
, struct fbo_entry
*entry
)
703 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
704 GLuint read_binding
, draw_binding
;
707 if (entry
->flags
& WINED3D_FBO_ENTRY_FLAG_ATTACHED
)
709 context_bind_fbo(context
, target
, entry
->id
);
713 read_binding
= context
->fbo_read_binding
;
714 draw_binding
= context
->fbo_draw_binding
;
715 context_bind_fbo(context
, GL_FRAMEBUFFER
, entry
->id
);
717 if (gl_info
->supported
[ARB_FRAMEBUFFER_NO_ATTACHMENTS
])
719 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
,
720 GL_FRAMEBUFFER_DEFAULT_WIDTH
, gl_info
->limits
.framebuffer_width
));
721 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
,
722 GL_FRAMEBUFFER_DEFAULT_HEIGHT
, gl_info
->limits
.framebuffer_height
));
723 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
, GL_FRAMEBUFFER_DEFAULT_LAYERS
, 1));
724 GL_EXTCALL(glFramebufferParameteri(GL_FRAMEBUFFER
, GL_FRAMEBUFFER_DEFAULT_SAMPLES
, 1));
727 /* Apply render targets */
728 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
730 context_attach_surface_fbo(context
, target
, i
, &entry
->key
.objects
[i
+ 1],
731 entry
->key
.rb_namespace
& (1 << (i
+ 1)));
734 context_attach_depth_stencil_fbo(context
, target
, &entry
->key
.objects
[0],
735 entry
->key
.rb_namespace
& 0x1, entry
->flags
);
737 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
738 * GL contexts requirements. */
739 gl_info
->gl_ops
.gl
.p_glReadBuffer(GL_NONE
);
740 context_set_draw_buffer(context
, GL_NONE
);
741 if (target
!= GL_FRAMEBUFFER
)
743 if (target
== GL_READ_FRAMEBUFFER
)
744 context_bind_fbo(context
, GL_DRAW_FRAMEBUFFER
, draw_binding
);
746 context_bind_fbo(context
, GL_READ_FRAMEBUFFER
, read_binding
);
749 entry
->flags
|= WINED3D_FBO_ENTRY_FLAG_ATTACHED
;
752 /* Context activation is done by the caller. */
753 static void context_apply_fbo_state(struct wined3d_context
*context
, GLenum target
,
754 const struct wined3d_rendertarget_info
*render_targets
,
755 const struct wined3d_rendertarget_info
*depth_stencil
, DWORD color_location
, DWORD ds_location
)
757 struct fbo_entry
*entry
, *entry2
;
759 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
761 context_destroy_fbo_entry(context
, entry
);
764 if (context
->rebind_fbo
)
766 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
767 context
->rebind_fbo
= FALSE
;
770 if (color_location
== WINED3D_LOCATION_DRAWABLE
)
772 context
->current_fbo
= NULL
;
773 context_bind_fbo(context
, target
, 0);
777 context
->current_fbo
= context_find_fbo_entry(context
, target
,
778 render_targets
, depth_stencil
, color_location
, ds_location
);
779 context_apply_fbo_entry(context
, target
, context
->current_fbo
);
783 /* Context activation is done by the caller. */
784 void context_apply_fbo_state_blit(struct wined3d_context
*context
, GLenum target
,
785 struct wined3d_resource
*rt
, unsigned int rt_sub_resource_idx
,
786 struct wined3d_resource
*ds
, unsigned int ds_sub_resource_idx
, DWORD location
)
788 struct wined3d_rendertarget_info ds_info
= {{0}};
790 memset(context
->blit_targets
, 0, sizeof(context
->blit_targets
));
793 context
->blit_targets
[0].resource
= rt
;
794 context
->blit_targets
[0].sub_resource_idx
= rt_sub_resource_idx
;
795 context
->blit_targets
[0].layer_count
= 1;
800 ds_info
.resource
= ds
;
801 ds_info
.sub_resource_idx
= ds_sub_resource_idx
;
802 ds_info
.layer_count
= 1;
805 context_apply_fbo_state(context
, target
, context
->blit_targets
, &ds_info
, location
, location
);
808 /* Context activation is done by the caller. */
809 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
811 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
813 if (context
->free_occlusion_query_count
)
815 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
819 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
821 GL_EXTCALL(glGenQueries(1, &query
->id
));
822 checkGLcall("glGenQueries");
824 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
828 WARN("Occlusion queries not supported, not allocating query id.\n");
833 query
->context
= context
;
834 list_add_head(&context
->occlusion_queries
, &query
->entry
);
837 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
839 struct wined3d_context
*context
= query
->context
;
841 list_remove(&query
->entry
);
842 query
->context
= NULL
;
844 if (!wined3d_array_reserve((void **)&context
->free_occlusion_queries
,
845 &context
->free_occlusion_query_size
, context
->free_occlusion_query_count
+ 1,
846 sizeof(*context
->free_occlusion_queries
)))
848 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
852 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
855 /* Context activation is done by the caller. */
856 void context_alloc_fence(struct wined3d_context
*context
, struct wined3d_fence
*fence
)
858 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
860 if (context
->free_fence_count
)
862 fence
->object
= context
->free_fences
[--context
->free_fence_count
];
866 if (gl_info
->supported
[ARB_SYNC
])
868 /* Using ARB_sync, not much to do here. */
869 fence
->object
.sync
= NULL
;
870 TRACE("Allocated sync object in context %p.\n", context
);
872 else if (gl_info
->supported
[APPLE_FENCE
])
874 GL_EXTCALL(glGenFencesAPPLE(1, &fence
->object
.id
));
875 checkGLcall("glGenFencesAPPLE");
877 TRACE("Allocated fence %u in context %p.\n", fence
->object
.id
, context
);
879 else if(gl_info
->supported
[NV_FENCE
])
881 GL_EXTCALL(glGenFencesNV(1, &fence
->object
.id
));
882 checkGLcall("glGenFencesNV");
884 TRACE("Allocated fence %u in context %p.\n", fence
->object
.id
, context
);
888 WARN("Fences not supported, not allocating fence.\n");
889 fence
->object
.id
= 0;
893 fence
->context
= context
;
894 list_add_head(&context
->fences
, &fence
->entry
);
897 void context_free_fence(struct wined3d_fence
*fence
)
899 struct wined3d_context
*context
= fence
->context
;
901 list_remove(&fence
->entry
);
902 fence
->context
= NULL
;
904 if (!wined3d_array_reserve((void **)&context
->free_fences
,
905 &context
->free_fence_size
, context
->free_fence_count
+ 1,
906 sizeof(*context
->free_fences
)))
908 ERR("Failed to grow free list, leaking fence %u in context %p.\n", fence
->object
.id
, context
);
912 context
->free_fences
[context
->free_fence_count
++] = fence
->object
;
915 /* Context activation is done by the caller. */
916 void context_alloc_timestamp_query(struct wined3d_context
*context
, struct wined3d_timestamp_query
*query
)
918 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
920 if (context
->free_timestamp_query_count
)
922 query
->id
= context
->free_timestamp_queries
[--context
->free_timestamp_query_count
];
926 GL_EXTCALL(glGenQueries(1, &query
->id
));
927 checkGLcall("glGenQueries");
929 TRACE("Allocated timestamp query %u in context %p.\n", query
->id
, context
);
932 query
->context
= context
;
933 list_add_head(&context
->timestamp_queries
, &query
->entry
);
936 void context_free_timestamp_query(struct wined3d_timestamp_query
*query
)
938 struct wined3d_context
*context
= query
->context
;
940 list_remove(&query
->entry
);
941 query
->context
= NULL
;
943 if (!wined3d_array_reserve((void **)&context
->free_timestamp_queries
,
944 &context
->free_timestamp_query_size
, context
->free_timestamp_query_count
+ 1,
945 sizeof(*context
->free_timestamp_queries
)))
947 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
951 context
->free_timestamp_queries
[context
->free_timestamp_query_count
++] = query
->id
;
954 void context_alloc_so_statistics_query(struct wined3d_context
*context
,
955 struct wined3d_so_statistics_query
*query
)
957 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
959 if (context
->free_so_statistics_query_count
)
961 query
->u
= context
->free_so_statistics_queries
[--context
->free_so_statistics_query_count
];
965 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
966 checkGLcall("glGenQueries");
968 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
969 query
->u
.id
[0], query
->u
.id
[1], context
);
972 query
->context
= context
;
973 list_add_head(&context
->so_statistics_queries
, &query
->entry
);
976 void context_free_so_statistics_query(struct wined3d_so_statistics_query
*query
)
978 struct wined3d_context
*context
= query
->context
;
980 list_remove(&query
->entry
);
981 query
->context
= NULL
;
983 if (!wined3d_array_reserve((void **)&context
->free_so_statistics_queries
,
984 &context
->free_so_statistics_query_size
, context
->free_so_statistics_query_count
+ 1,
985 sizeof(*context
->free_so_statistics_queries
)))
987 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
988 query
->u
.id
[0], query
->u
.id
[1], context
);
992 context
->free_so_statistics_queries
[context
->free_so_statistics_query_count
++] = query
->u
;
995 void context_alloc_pipeline_statistics_query(struct wined3d_context
*context
,
996 struct wined3d_pipeline_statistics_query
*query
)
998 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1000 if (context
->free_pipeline_statistics_query_count
)
1002 query
->u
= context
->free_pipeline_statistics_queries
[--context
->free_pipeline_statistics_query_count
];
1006 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query
->u
.id
), query
->u
.id
));
1007 checkGLcall("glGenQueries");
1010 query
->context
= context
;
1011 list_add_head(&context
->pipeline_statistics_queries
, &query
->entry
);
1014 void context_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query
*query
)
1016 struct wined3d_context
*context
= query
->context
;
1018 list_remove(&query
->entry
);
1019 query
->context
= NULL
;
1021 if (!wined3d_array_reserve((void **)&context
->free_pipeline_statistics_queries
,
1022 &context
->free_pipeline_statistics_query_size
, context
->free_pipeline_statistics_query_count
+ 1,
1023 sizeof(*context
->free_pipeline_statistics_queries
)))
1025 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context
);
1029 context
->free_pipeline_statistics_queries
[context
->free_pipeline_statistics_query_count
++] = query
->u
;
1032 typedef void (context_fbo_entry_func_t
)(struct wined3d_context
*context
, struct fbo_entry
*entry
);
1034 static void context_enum_fbo_entries(const struct wined3d_device
*device
,
1035 GLuint name
, BOOL rb_namespace
, context_fbo_entry_func_t
*callback
)
1039 for (i
= 0; i
< device
->context_count
; ++i
)
1041 struct wined3d_context
*context
= device
->contexts
[i
];
1042 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1043 struct fbo_entry
*entry
, *entry2
;
1045 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1047 for (j
= 0; j
< gl_info
->limits
.buffers
+ 1; ++j
)
1049 if (entry
->key
.objects
[j
].object
== name
1050 && !(entry
->key
.rb_namespace
& (1 << j
)) == !rb_namespace
)
1052 callback(context
, entry
);
1060 static void context_queue_fbo_entry_destruction(struct wined3d_context
*context
, struct fbo_entry
*entry
)
1062 list_remove(&entry
->entry
);
1063 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
1066 void context_resource_released(const struct wined3d_device
*device
, struct wined3d_resource
*resource
)
1070 if (!device
->d3d_initialized
)
1073 for (i
= 0; i
< device
->context_count
; ++i
)
1075 struct wined3d_context
*context
= device
->contexts
[i
];
1077 if (&context
->current_rt
.texture
->resource
== resource
)
1079 context
->current_rt
.texture
= NULL
;
1080 context
->current_rt
.sub_resource_idx
= 0;
1085 void context_gl_resource_released(struct wined3d_device
*device
,
1086 GLuint name
, BOOL rb_namespace
)
1088 context_enum_fbo_entries(device
, name
, rb_namespace
, context_queue_fbo_entry_destruction
);
1091 void context_texture_update(struct wined3d_context
*context
, const struct wined3d_texture
*texture
)
1093 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1094 struct fbo_entry
*entry
= context
->current_fbo
;
1097 if (!entry
|| context
->rebind_fbo
) return;
1099 for (i
= 0; i
< gl_info
->limits
.buffers
+ 1; ++i
)
1101 if (texture
->texture_rgb
.name
== entry
->key
.objects
[i
].object
1102 || texture
->texture_srgb
.name
== entry
->key
.objects
[i
].object
)
1104 TRACE("Updated texture %p is bound as attachment %u to the current FBO.\n", texture
, i
);
1105 context
->rebind_fbo
= TRUE
;
1111 static BOOL
context_restore_pixel_format(struct wined3d_context
*ctx
)
1113 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
1116 if (ctx
->restore_pf
&& IsWindow(ctx
->restore_pf_win
))
1118 if (ctx
->gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1120 HDC dc
= GetDCEx(ctx
->restore_pf_win
, 0, DCX_USESTYLE
| DCX_CACHE
);
1123 if (!(ret
= GL_EXTCALL(wglSetPixelFormatWINE(dc
, ctx
->restore_pf
))))
1125 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
1126 ctx
->restore_pf
, ctx
->restore_pf_win
);
1128 ReleaseDC(ctx
->restore_pf_win
, dc
);
1133 ERR("can't restore pixel format %d on window %p\n", ctx
->restore_pf
, ctx
->restore_pf_win
);
1137 ctx
->restore_pf
= 0;
1138 ctx
->restore_pf_win
= NULL
;
1142 static BOOL
context_set_pixel_format(struct wined3d_context
*context
)
1144 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1145 BOOL
private = context
->hdc_is_private
;
1146 int format
= context
->pixel_format
;
1147 HDC dc
= context
->hdc
;
1150 if (private && context
->hdc_has_format
)
1153 if (!private && WindowFromDC(dc
) != context
->win_handle
)
1156 current
= gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(dc
);
1157 if (current
== format
) goto success
;
1161 if (!SetPixelFormat(dc
, format
, NULL
))
1163 /* This may also happen if the dc belongs to a destroyed window. */
1164 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1165 format
, dc
, GetLastError());
1169 context
->restore_pf
= 0;
1170 context
->restore_pf_win
= private ? NULL
: WindowFromDC(dc
);
1174 /* By default WGL doesn't allow pixel format adjustments but we need it
1175 * here. For this reason there's a Wine specific wglSetPixelFormat()
1176 * which allows us to set the pixel format multiple times. Only use it
1177 * when really needed. */
1178 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
1182 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
)))
1184 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1189 win
= private ? NULL
: WindowFromDC(dc
);
1190 if (win
!= context
->restore_pf_win
)
1192 context_restore_pixel_format(context
);
1194 context
->restore_pf
= private ? 0 : current
;
1195 context
->restore_pf_win
= win
;
1201 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1202 * continue using the old format. There's a big chance that the old
1203 * format works although with a performance hit and perhaps rendering
1205 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1206 format
, dc
, current
);
1211 context
->hdc_has_format
= TRUE
;
1215 static BOOL
context_set_gl_context(struct wined3d_context
*ctx
)
1217 struct wined3d_swapchain
*swapchain
= ctx
->swapchain
;
1218 BOOL backup
= FALSE
;
1220 if (!context_set_pixel_format(ctx
))
1222 WARN("Failed to set pixel format %d on device context %p.\n",
1223 ctx
->pixel_format
, ctx
->hdc
);
1227 if (backup
|| !wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
1229 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1230 ctx
->glCtx
, ctx
->hdc
, GetLastError());
1232 WARN("Trying fallback to the backup window.\n");
1234 /* FIXME: If the context is destroyed it's no longer associated with
1235 * a swapchain, so we can't use the swapchain to get a backup dc. To
1236 * make this work windowless contexts would need to be handled by the
1238 if (ctx
->destroyed
|| !swapchain
)
1240 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx
);
1241 context_set_current(NULL
);
1245 if (!(ctx
->hdc
= swapchain_get_backup_dc(swapchain
)))
1247 context_set_current(NULL
);
1251 ctx
->hdc_is_private
= TRUE
;
1252 ctx
->hdc_has_format
= FALSE
;
1254 if (!context_set_pixel_format(ctx
))
1256 ERR("Failed to set pixel format %d on device context %p.\n",
1257 ctx
->pixel_format
, ctx
->hdc
);
1258 context_set_current(NULL
);
1262 if (!wglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
1264 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1265 ctx
->hdc
, GetLastError());
1266 context_set_current(NULL
);
1276 static void context_restore_gl_context(const struct wined3d_gl_info
*gl_info
, HDC dc
, HGLRC gl_ctx
)
1278 if (!wglMakeCurrent(dc
, gl_ctx
))
1280 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1281 gl_ctx
, dc
, GetLastError());
1282 context_set_current(NULL
);
1286 static void context_update_window(struct wined3d_context
*context
)
1288 if (!context
->swapchain
)
1291 if (context
->win_handle
== context
->swapchain
->win_handle
)
1294 TRACE("Updating context %p window from %p to %p.\n",
1295 context
, context
->win_handle
, context
->swapchain
->win_handle
);
1298 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1300 context
->win_handle
= context
->swapchain
->win_handle
;
1301 context
->hdc_is_private
= FALSE
;
1302 context
->hdc_has_format
= FALSE
;
1303 context
->needs_set
= 1;
1306 if (!(context
->hdc
= GetDCEx(context
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1308 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
1313 static void context_destroy_gl_resources(struct wined3d_context
*context
)
1315 struct wined3d_pipeline_statistics_query
*pipeline_statistics_query
;
1316 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1317 struct wined3d_so_statistics_query
*so_statistics_query
;
1318 struct wined3d_timestamp_query
*timestamp_query
;
1319 struct wined3d_occlusion_query
*occlusion_query
;
1320 struct fbo_entry
*entry
, *entry2
;
1321 struct wined3d_fence
*fence
;
1326 restore_ctx
= wglGetCurrentContext();
1327 restore_dc
= wglGetCurrentDC();
1329 if (restore_ctx
== context
->glCtx
)
1331 else if (context
->valid
)
1332 context_set_gl_context(context
);
1334 LIST_FOR_EACH_ENTRY(so_statistics_query
, &context
->so_statistics_queries
,
1335 struct wined3d_so_statistics_query
, entry
)
1338 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query
->u
.id
), so_statistics_query
->u
.id
));
1339 so_statistics_query
->context
= NULL
;
1342 LIST_FOR_EACH_ENTRY(pipeline_statistics_query
, &context
->pipeline_statistics_queries
,
1343 struct wined3d_pipeline_statistics_query
, entry
)
1346 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query
->u
.id
), pipeline_statistics_query
->u
.id
));
1347 pipeline_statistics_query
->context
= NULL
;
1350 LIST_FOR_EACH_ENTRY(timestamp_query
, &context
->timestamp_queries
, struct wined3d_timestamp_query
, entry
)
1353 GL_EXTCALL(glDeleteQueries(1, ×tamp_query
->id
));
1354 timestamp_query
->context
= NULL
;
1357 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
1359 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1360 GL_EXTCALL(glDeleteQueries(1, &occlusion_query
->id
));
1361 occlusion_query
->context
= NULL
;
1364 LIST_FOR_EACH_ENTRY(fence
, &context
->fences
, struct wined3d_fence
, entry
)
1368 if (gl_info
->supported
[ARB_SYNC
])
1370 if (fence
->object
.sync
)
1371 GL_EXTCALL(glDeleteSync(fence
->object
.sync
));
1373 else if (gl_info
->supported
[APPLE_FENCE
])
1375 GL_EXTCALL(glDeleteFencesAPPLE(1, &fence
->object
.id
));
1377 else if (gl_info
->supported
[NV_FENCE
])
1379 GL_EXTCALL(glDeleteFencesNV(1, &fence
->object
.id
));
1382 fence
->context
= NULL
;
1385 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
1387 if (!context
->valid
) entry
->id
= 0;
1388 context_destroy_fbo_entry(context
, entry
);
1391 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
1393 if (!context
->valid
) entry
->id
= 0;
1394 context_destroy_fbo_entry(context
, entry
);
1399 if (context
->dummy_arbfp_prog
)
1401 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
1404 if (gl_info
->supported
[WINED3D_GL_PRIMITIVE_QUERY
])
1406 for (i
= 0; i
< context
->free_so_statistics_query_count
; ++i
)
1408 union wined3d_gl_so_statistics_query
*q
= &context
->free_so_statistics_queries
[i
];
1409 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1413 if (gl_info
->supported
[ARB_PIPELINE_STATISTICS_QUERY
])
1415 for (i
= 0; i
< context
->free_pipeline_statistics_query_count
; ++i
)
1417 union wined3d_gl_pipeline_statistics_query
*q
= &context
->free_pipeline_statistics_queries
[i
];
1418 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q
->id
), q
->id
));
1422 if (gl_info
->supported
[ARB_TIMER_QUERY
])
1423 GL_EXTCALL(glDeleteQueries(context
->free_timestamp_query_count
, context
->free_timestamp_queries
));
1425 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
1426 GL_EXTCALL(glDeleteQueries(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
1428 if (gl_info
->supported
[ARB_SYNC
])
1430 for (i
= 0; i
< context
->free_fence_count
; ++i
)
1432 GL_EXTCALL(glDeleteSync(context
->free_fences
[i
].sync
));
1435 else if (gl_info
->supported
[APPLE_FENCE
])
1437 for (i
= 0; i
< context
->free_fence_count
; ++i
)
1439 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_fences
[i
].id
));
1442 else if (gl_info
->supported
[NV_FENCE
])
1444 for (i
= 0; i
< context
->free_fence_count
; ++i
)
1446 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_fences
[i
].id
));
1450 if (context
->blit_vbo
)
1451 GL_EXTCALL(glDeleteBuffers(1, &context
->blit_vbo
));
1453 checkGLcall("context cleanup");
1456 heap_free(context
->free_so_statistics_queries
);
1457 heap_free(context
->free_pipeline_statistics_queries
);
1458 heap_free(context
->free_timestamp_queries
);
1459 heap_free(context
->free_occlusion_queries
);
1460 heap_free(context
->free_fences
);
1462 context_restore_pixel_format(context
);
1465 context_restore_gl_context(gl_info
, restore_dc
, restore_ctx
);
1467 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL
, NULL
))
1469 ERR("Failed to disable GL context.\n");
1472 wined3d_release_dc(context
->win_handle
, context
->hdc
);
1474 if (!wglDeleteContext(context
->glCtx
))
1476 DWORD err
= GetLastError();
1477 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
1481 DWORD
context_get_tls_idx(void)
1483 return wined3d_context_tls_idx
;
1486 void context_set_tls_idx(DWORD idx
)
1488 wined3d_context_tls_idx
= idx
;
1491 struct wined3d_context
*context_get_current(void)
1493 return TlsGetValue(wined3d_context_tls_idx
);
1496 BOOL
context_set_current(struct wined3d_context
*ctx
)
1498 struct wined3d_context
*old
= context_get_current();
1502 TRACE("Already using D3D context %p.\n", ctx
);
1510 TRACE("Switching away from destroyed context %p.\n", old
);
1511 context_destroy_gl_resources(old
);
1512 heap_free((void *)old
->gl_info
);
1517 if (wglGetCurrentContext())
1519 const struct wined3d_gl_info
*gl_info
= old
->gl_info
;
1520 TRACE("Flushing context %p before switching to %p.\n", old
, ctx
);
1521 gl_info
->gl_ops
.gl
.p_glFlush();
1531 ERR("Trying to make invalid context %p current\n", ctx
);
1535 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
1536 if (!context_set_gl_context(ctx
))
1540 else if (wglGetCurrentContext())
1542 TRACE("Clearing current D3D context.\n");
1543 if (!wglMakeCurrent(NULL
, NULL
))
1545 DWORD err
= GetLastError();
1546 ERR("Failed to clear current GL context, last error %#x.\n", err
);
1547 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1552 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
1555 void context_release(struct wined3d_context
*context
)
1557 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
1561 if (!context
->level
)
1562 WARN("Context %p is not active.\n", context
);
1563 else if (context
!= context_get_current())
1564 WARN("Context %p is not the current context.\n", context
);
1567 if (!--context
->level
)
1569 if (context_restore_pixel_format(context
))
1570 context
->needs_set
= 1;
1571 if (context
->restore_ctx
)
1573 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
1574 context_restore_gl_context(context
->gl_info
, context
->restore_dc
, context
->restore_ctx
);
1575 context
->restore_ctx
= NULL
;
1576 context
->restore_dc
= NULL
;
1579 if (context
->destroy_delayed
)
1581 TRACE("Destroying context %p.\n", context
);
1582 context_destroy(context
->device
, context
);
1587 /* This is used when a context for render target A is active, but a separate context is
1588 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1589 * A to avoid breaking caller code. */
1590 void context_restore(struct wined3d_context
*context
, struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
1592 if (context
->current_rt
.texture
!= texture
|| context
->current_rt
.sub_resource_idx
!= sub_resource_idx
)
1594 context_release(context
);
1595 context
= context_acquire(texture
->resource
.device
, texture
, sub_resource_idx
);
1598 context_release(context
);
1601 static void context_enter(struct wined3d_context
*context
)
1603 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
1605 if (!context
->level
++)
1607 const struct wined3d_context
*current_context
= context_get_current();
1608 HGLRC current_gl
= wglGetCurrentContext();
1610 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
1612 TRACE("Another GL context (%p on device context %p) is already current.\n",
1613 current_gl
, wglGetCurrentDC());
1614 context
->restore_ctx
= current_gl
;
1615 context
->restore_dc
= wglGetCurrentDC();
1616 context
->needs_set
= 1;
1618 else if (!context
->needs_set
&& !(context
->hdc_is_private
&& context
->hdc_has_format
)
1619 && context
->pixel_format
!= context
->gl_info
->gl_ops
.wgl
.p_wglGetPixelFormat(context
->hdc
))
1620 context
->needs_set
= 1;
1624 void context_invalidate_compute_state(struct wined3d_context
*context
, DWORD state_id
)
1626 DWORD representative
= context
->state_table
[state_id
].representative
- STATE_COMPUTE_OFFSET
;
1627 unsigned int index
, shift
;
1629 index
= representative
/ (sizeof(*context
->dirty_compute_states
) * CHAR_BIT
);
1630 shift
= representative
& (sizeof(*context
->dirty_compute_states
) * CHAR_BIT
- 1);
1631 context
->dirty_compute_states
[index
] |= (1u << shift
);
1634 void context_invalidate_state(struct wined3d_context
*context
, DWORD state
)
1636 DWORD rep
= context
->state_table
[state
].representative
;
1640 if (isStateDirty(context
, rep
)) return;
1642 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1643 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1644 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1645 context
->isStateDirty
[idx
] |= (1u << shift
);
1648 /* This function takes care of wined3d pixel format selection. */
1649 static int context_choose_pixel_format(const struct wined3d_device
*device
, HDC hdc
,
1650 const struct wined3d_format
*color_format
, const struct wined3d_format
*ds_format
,
1653 unsigned int cfg_count
= device
->adapter
->cfg_count
;
1654 unsigned int current_value
;
1655 PIXELFORMATDESCRIPTOR pfd
;
1656 int iPixelFormat
= 0;
1659 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1660 device
, hdc
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
),
1664 for (i
= 0; i
< cfg_count
; ++i
)
1666 const struct wined3d_pixel_format
*cfg
= &device
->adapter
->cfgs
[i
];
1669 /* For now only accept RGBA formats. Perhaps some day we will
1670 * allow floating point formats for pbuffers. */
1671 if (cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1673 /* In window mode we need a window drawable format and double buffering. */
1674 if (!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1676 if (cfg
->redSize
< color_format
->red_size
)
1678 if (cfg
->greenSize
< color_format
->green_size
)
1680 if (cfg
->blueSize
< color_format
->blue_size
)
1682 if (cfg
->alphaSize
< color_format
->alpha_size
)
1684 if (cfg
->depthSize
< ds_format
->depth_size
)
1686 if (ds_format
->stencil_size
&& cfg
->stencilSize
!= ds_format
->stencil_size
)
1688 /* Check multisampling support. */
1689 if (cfg
->numSamples
)
1693 /* We try to locate a format which matches our requirements exactly. In case of
1694 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1695 if (cfg
->depthSize
== ds_format
->depth_size
)
1697 if (cfg
->stencilSize
== ds_format
->stencil_size
)
1699 if (cfg
->alphaSize
== color_format
->alpha_size
)
1701 /* We like to have aux buffers in backbuffer mode */
1702 if (auxBuffers
&& cfg
->auxBuffers
)
1704 if (cfg
->redSize
== color_format
->red_size
1705 && cfg
->greenSize
== color_format
->green_size
1706 && cfg
->blueSize
== color_format
->blue_size
)
1709 if (value
> current_value
)
1711 iPixelFormat
= cfg
->iPixelFormat
;
1712 current_value
= value
;
1718 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1720 memset(&pfd
, 0, sizeof(pfd
));
1721 pfd
.nSize
= sizeof(pfd
);
1723 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1724 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1725 pfd
.cAlphaBits
= color_format
->alpha_size
;
1726 pfd
.cColorBits
= color_format
->red_size
+ color_format
->green_size
1727 + color_format
->blue_size
+ color_format
->alpha_size
;
1728 pfd
.cDepthBits
= ds_format
->depth_size
;
1729 pfd
.cStencilBits
= ds_format
->stencil_size
;
1730 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1732 if (!(iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
)))
1734 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1735 ERR("Can't find a suitable pixel format.\n");
1740 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1741 iPixelFormat
, debug_d3dformat(color_format
->id
), debug_d3dformat(ds_format
->id
));
1742 return iPixelFormat
;
1745 /* Context activation is done by the caller. */
1746 void context_bind_dummy_textures(const struct wined3d_device
*device
, const struct wined3d_context
*context
)
1748 const struct wined3d_dummy_textures
*textures
= &context
->device
->dummy_textures
;
1749 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1752 for (i
= 0; i
< gl_info
->limits
.combined_samplers
; ++i
)
1754 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ i
));
1756 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, textures
->tex_1d
);
1757 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, textures
->tex_2d
);
1759 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1760 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, textures
->tex_rect
);
1762 if (gl_info
->supported
[EXT_TEXTURE3D
])
1763 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, textures
->tex_3d
);
1765 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1766 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, textures
->tex_cube
);
1768 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP_ARRAY
])
1769 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, textures
->tex_cube_array
);
1771 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
1773 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, textures
->tex_1d_array
);
1774 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, textures
->tex_2d_array
);
1777 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
1778 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, textures
->tex_buffer
);
1780 if (gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
1782 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, textures
->tex_2d_ms
);
1783 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, textures
->tex_2d_ms_array
);
1787 checkGLcall("bind dummy textures");
1790 void wined3d_check_gl_call(const struct wined3d_gl_info
*gl_info
,
1791 const char *file
, unsigned int line
, const char *name
)
1795 if (gl_info
->supported
[ARB_DEBUG_OUTPUT
] || (err
= gl_info
->gl_ops
.gl
.p_glGetError()) == GL_NO_ERROR
)
1797 TRACE("%s call ok %s / %u.\n", name
, file
, line
);
1803 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1804 debug_glerror(err
), err
, name
, file
,line
);
1805 err
= gl_info
->gl_ops
.gl
.p_glGetError();
1806 } while (err
!= GL_NO_ERROR
);
1809 static BOOL
context_debug_output_enabled(const struct wined3d_gl_info
*gl_info
)
1811 return gl_info
->supported
[ARB_DEBUG_OUTPUT
]
1812 && (ERR_ON(d3d
) || FIXME_ON(d3d
) || WARN_ON(d3d_perf
));
1815 static void WINE_GLAPI
wined3d_debug_callback(GLenum source
, GLenum type
, GLuint id
,
1816 GLenum severity
, GLsizei length
, const char *message
, void *ctx
)
1820 case GL_DEBUG_TYPE_ERROR_ARB
:
1821 ERR("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1824 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
:
1825 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
:
1826 case GL_DEBUG_TYPE_PORTABILITY_ARB
:
1827 FIXME("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1830 case GL_DEBUG_TYPE_PERFORMANCE_ARB
:
1831 WARN_(d3d_perf
)("%p: %s.\n", ctx
, debugstr_an(message
, length
));
1835 FIXME("ctx %p, type %#x: %s.\n", ctx
, type
, debugstr_an(message
, length
));
1840 HGLRC
context_create_wgl_attribs(const struct wined3d_gl_info
*gl_info
, HDC hdc
, HGLRC share_ctx
)
1843 unsigned int ctx_attrib_idx
= 0;
1844 GLint ctx_attribs
[7], ctx_flags
= 0;
1846 if (context_debug_output_enabled(gl_info
))
1847 ctx_flags
= WGL_CONTEXT_DEBUG_BIT_ARB
;
1848 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MAJOR_VERSION_ARB
;
1849 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
>> 16;
1850 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_MINOR_VERSION_ARB
;
1851 ctx_attribs
[ctx_attrib_idx
++] = gl_info
->selected_gl_version
& 0xffff;
1852 if (gl_info
->selected_gl_version
>= MAKEDWORD_VERSION(3, 2))
1853 ctx_flags
|= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1856 ctx_attribs
[ctx_attrib_idx
++] = WGL_CONTEXT_FLAGS_ARB
;
1857 ctx_attribs
[ctx_attrib_idx
++] = ctx_flags
;
1859 ctx_attribs
[ctx_attrib_idx
] = 0;
1861 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1863 if (ctx_flags
& WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
)
1865 ctx_attribs
[ctx_attrib_idx
- 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
;
1866 if (!(ctx
= gl_info
->p_wglCreateContextAttribsARB(hdc
, share_ctx
, ctx_attribs
)))
1867 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1874 struct wined3d_context
*context_create(struct wined3d_swapchain
*swapchain
,
1875 struct wined3d_texture
*target
, const struct wined3d_format
*ds_format
)
1877 struct wined3d_device
*device
= swapchain
->device
;
1878 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
1879 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1880 const struct wined3d_format
*color_format
;
1881 struct wined3d_context
*ret
;
1882 BOOL auxBuffers
= FALSE
;
1883 HGLRC ctx
, share_ctx
;
1888 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1890 wined3d_from_cs(device
->cs
);
1892 if (!(ret
= heap_alloc_zero(sizeof(*ret
))))
1895 ret
->free_timestamp_query_size
= 4;
1896 if (!(ret
->free_timestamp_queries
= heap_calloc(ret
->free_timestamp_query_size
,
1897 sizeof(*ret
->free_timestamp_queries
))))
1899 list_init(&ret
->timestamp_queries
);
1901 ret
->free_occlusion_query_size
= 4;
1902 if (!(ret
->free_occlusion_queries
= heap_calloc(ret
->free_occlusion_query_size
,
1903 sizeof(*ret
->free_occlusion_queries
))))
1905 list_init(&ret
->occlusion_queries
);
1907 ret
->free_fence_size
= 4;
1908 if (!(ret
->free_fences
= heap_calloc(ret
->free_fence_size
, sizeof(*ret
->free_fences
))))
1910 list_init(&ret
->fences
);
1912 list_init(&ret
->so_statistics_queries
);
1914 list_init(&ret
->pipeline_statistics_queries
);
1916 list_init(&ret
->fbo_list
);
1917 list_init(&ret
->fbo_destroy_list
);
1919 if (!device
->shader_backend
->shader_allocate_context_data(ret
))
1921 ERR("Failed to allocate shader backend context data.\n");
1924 if (!device
->adapter
->fragment_pipe
->allocate_context_data(ret
))
1926 ERR("Failed to allocate fragment pipeline context data.\n");
1930 for (i
= 0; i
< ARRAY_SIZE(ret
->tex_unit_map
); ++i
)
1931 ret
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
1932 for (i
= 0; i
< ARRAY_SIZE(ret
->rev_tex_unit_map
); ++i
)
1933 ret
->rev_tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
1934 if (gl_info
->limits
.graphics_samplers
>= MAX_COMBINED_SAMPLERS
)
1936 /* Initialize the texture unit mapping to a 1:1 mapping. */
1937 unsigned int base
, count
;
1939 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_PIXEL
, &base
, &count
);
1940 if (base
+ MAX_FRAGMENT_SAMPLERS
> ARRAY_SIZE(ret
->rev_tex_unit_map
))
1942 ERR("Unexpected texture unit base index %u.\n", base
);
1945 for (i
= 0; i
< min(count
, MAX_FRAGMENT_SAMPLERS
); ++i
)
1947 ret
->tex_unit_map
[i
] = base
+ i
;
1948 ret
->rev_tex_unit_map
[base
+ i
] = i
;
1951 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, WINED3D_SHADER_TYPE_VERTEX
, &base
, &count
);
1952 if (base
+ MAX_VERTEX_SAMPLERS
> ARRAY_SIZE(ret
->rev_tex_unit_map
))
1954 ERR("Unexpected texture unit base index %u.\n", base
);
1957 for (i
= 0; i
< min(count
, MAX_VERTEX_SAMPLERS
); ++i
)
1959 ret
->tex_unit_map
[MAX_FRAGMENT_SAMPLERS
+ i
] = base
+ i
;
1960 ret
->rev_tex_unit_map
[base
+ i
] = MAX_FRAGMENT_SAMPLERS
+ i
;
1964 if (!(ret
->texture_type
= heap_calloc(gl_info
->limits
.combined_samplers
,
1965 sizeof(*ret
->texture_type
))))
1968 if (!(ret
->hdc
= GetDCEx(swapchain
->win_handle
, 0, DCX_USESTYLE
| DCX_CACHE
)))
1970 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1972 if ((ret
->hdc
= swapchain_get_backup_dc(swapchain
)))
1973 ret
->hdc_is_private
= TRUE
;
1976 ERR("Failed to retrieve a device context.\n");
1981 color_format
= target
->resource
.format
;
1982 target_usage
= target
->resource
.usage
;
1984 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1985 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1986 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1990 if (color_format
->id
== WINED3DFMT_B4G4R4X4_UNORM
)
1991 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B4G4R4A4_UNORM
, target_usage
);
1992 else if (color_format
->id
== WINED3DFMT_B8G8R8X8_UNORM
)
1993 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
, target_usage
);
1996 /* DirectDraw supports 8bit paletted render targets and these are used by
1997 * old games like StarCraft and C&C. Most modern hardware doesn't support
1998 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1999 * conversion (ab)uses the alpha component for storing the palette index.
2000 * For this reason we require a format with 8bit alpha, so request
2002 if (color_format
->id
== WINED3DFMT_P8_UINT
)
2003 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
, target_usage
);
2005 /* When using FBOs for off-screen rendering, we only use the drawable for
2006 * presentation blits, and don't do any rendering to it. That means we
2007 * don't need depth or stencil buffers, and can mostly ignore the render
2008 * target format. This wouldn't necessarily be quite correct for 10bpc
2009 * display modes, but we don't currently support those.
2010 * Using the same format regardless of the color/depth/stencil targets
2011 * makes it much less likely that different wined3d instances will set
2012 * conflicting pixel formats. */
2013 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
)
2015 color_format
= wined3d_get_format(gl_info
, WINED3DFMT_B8G8R8A8_UNORM
, target_usage
);
2016 ds_format
= wined3d_get_format(gl_info
, WINED3DFMT_UNKNOWN
, WINED3DUSAGE_DEPTHSTENCIL
);
2019 /* Try to find a pixel format which matches our requirements. */
2020 if (!(ret
->pixel_format
= context_choose_pixel_format(device
, ret
->hdc
, color_format
, ds_format
, auxBuffers
)))
2023 ret
->gl_info
= gl_info
;
2024 ret
->win_handle
= swapchain
->win_handle
;
2028 if (!context_set_pixel_format(ret
))
2030 ERR("Failed to set pixel format %d on device context %p.\n", ret
->pixel_format
, ret
->hdc
);
2031 context_release(ret
);
2035 share_ctx
= device
->context_count
? device
->contexts
[0]->glCtx
: NULL
;
2036 if (gl_info
->p_wglCreateContextAttribsARB
)
2038 if (!(ctx
= context_create_wgl_attribs(gl_info
, ret
->hdc
, share_ctx
)))
2043 if (!(ctx
= wglCreateContext(ret
->hdc
)))
2045 ERR("Failed to create a WGL context.\n");
2046 context_release(ret
);
2050 if (share_ctx
&& !wglShareLists(share_ctx
, ctx
))
2052 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx
, ctx
, GetLastError());
2053 context_release(ret
);
2054 if (!wglDeleteContext(ctx
))
2055 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
2060 if (!device_context_add(device
, ret
))
2062 ERR("Failed to add the newly created context to the context list\n");
2063 context_release(ret
);
2064 if (!wglDeleteContext(ctx
))
2065 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
2069 ret
->d3d_info
= d3d_info
;
2070 ret
->state_table
= device
->StateTable
;
2072 /* Mark all states dirty to force a proper initialization of the states on
2073 * the first use of the context. Compute states do not need initialization. */
2074 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
2076 if (ret
->state_table
[state
].representative
&& !STATE_IS_COMPUTE(state
))
2077 context_invalidate_state(ret
, state
);
2080 ret
->device
= device
;
2081 ret
->swapchain
= swapchain
;
2082 ret
->current_rt
.texture
= target
;
2083 ret
->current_rt
.sub_resource_idx
= 0;
2084 ret
->tid
= GetCurrentThreadId();
2086 ret
->render_offscreen
= wined3d_resource_is_offscreen(&target
->resource
);
2087 ret
->draw_buffers_mask
= context_generate_rt_mask(GL_BACK
);
2091 ret
->hdc_has_format
= TRUE
;
2094 /* Set up the context defaults */
2095 if (!context_set_current(ret
))
2097 ERR("Cannot activate context to set up defaults.\n");
2098 device_context_remove(device
, ret
);
2099 context_release(ret
);
2100 if (!wglDeleteContext(ctx
))
2101 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, GetLastError());
2105 if (context_debug_output_enabled(gl_info
))
2107 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback
, ret
));
2108 if (TRACE_ON(d3d_synchronous
))
2109 gl_info
->gl_ops
.gl
.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS
);
2110 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DONT_CARE
, GL_DONT_CARE
, 0, NULL
, GL_FALSE
));
2113 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_ERROR
,
2114 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2118 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
,
2119 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2120 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
,
2121 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2122 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PORTABILITY
,
2123 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2125 if (WARN_ON(d3d_perf
))
2127 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE
, GL_DEBUG_TYPE_PERFORMANCE
,
2128 GL_DONT_CARE
, 0, NULL
, GL_TRUE
));
2132 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2133 gl_info
->gl_ops
.gl
.p_glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
2135 TRACE("Setting up the screen\n");
2137 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2139 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
2140 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2142 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
2143 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2145 gl_info
->gl_ops
.gl
.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
2146 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2152 GL_EXTCALL(glGenVertexArrays(1, &vao
));
2153 GL_EXTCALL(glBindVertexArray(vao
));
2154 checkGLcall("creating VAO");
2157 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
2158 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2159 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
2160 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2162 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
2164 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2165 * the previous texture where to source the offset from is always unit - 1.
2167 for (i
= 1; i
< gl_info
->limits
.textures
; ++i
)
2169 context_active_texture(ret
, gl_info
, i
);
2170 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_SHADER_NV
,
2171 GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ i
- 1);
2172 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2175 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
2177 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2178 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2179 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2180 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2183 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2184 * program and the dummy program is destroyed when the context is destroyed.
2186 static const char dummy_program
[] =
2188 "MOV result.color, fragment.color.primary;\n"
2190 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
2191 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
2192 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
2195 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2197 for (i
= 0; i
< gl_info
->limits
.textures
; ++i
)
2199 context_active_texture(ret
, gl_info
, i
);
2200 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
2201 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2205 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
2207 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
2209 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
2211 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
2213 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_NO_PRIMITIVE_RESTART
))
2215 if (gl_info
->supported
[ARB_ES3_COMPATIBILITY
])
2217 gl_info
->gl_ops
.gl
.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX
);
2218 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2222 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2225 if (!(d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_CUBEMAP_FILTERING
)
2226 && gl_info
->supported
[ARB_SEAMLESS_CUBE_MAP
])
2228 gl_info
->gl_ops
.gl
.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS
);
2229 checkGLcall("enable seamless cube map filtering");
2231 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
2232 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN
, GL_LOWER_LEFT
));
2233 device
->shader_backend
->shader_init_context_state(ret
);
2234 ret
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
2235 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
2236 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
2237 | (1u << WINED3D_SHADER_TYPE_HULL
)
2238 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
2239 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
2241 /* If this happens to be the first context for the device, dummy textures
2242 * are not created yet. In that case, they will be created (and bound) by
2243 * create_dummy_textures right after this context is initialized. */
2244 if (device
->dummy_textures
.tex_2d
)
2245 context_bind_dummy_textures(device
, ret
);
2247 /* Initialise all rectangles to avoid resetting unused ones later. */
2248 gl_info
->gl_ops
.gl
.p_glScissor(0, 0, 0, 0);
2249 checkGLcall("glScissor");
2251 TRACE("Created context %p.\n", ret
);
2257 wined3d_release_dc(swapchain
->win_handle
, ret
->hdc
);
2258 device
->shader_backend
->shader_free_context_data(ret
);
2259 device
->adapter
->fragment_pipe
->free_context_data(ret
);
2260 heap_free(ret
->texture_type
);
2261 heap_free(ret
->free_fences
);
2262 heap_free(ret
->free_occlusion_queries
);
2263 heap_free(ret
->free_timestamp_queries
);
2268 void context_destroy(struct wined3d_device
*device
, struct wined3d_context
*context
)
2272 TRACE("Destroying ctx %p\n", context
);
2274 wined3d_from_cs(device
->cs
);
2276 /* We delay destroying a context when it is active. The context_release()
2277 * function invokes context_destroy() again while leaving the last level. */
2280 TRACE("Delaying destruction of context %p.\n", context
);
2281 context
->destroy_delayed
= 1;
2282 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2283 context
->swapchain
= NULL
;
2287 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
2289 context_destroy_gl_resources(context
);
2290 TlsSetValue(wined3d_context_tls_idx
, NULL
);
2295 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2296 in wined3d_adapter may go away in the meantime */
2297 struct wined3d_gl_info
*gl_info
= heap_alloc(sizeof(*gl_info
));
2298 *gl_info
= *context
->gl_info
;
2299 context
->gl_info
= gl_info
;
2300 context
->destroyed
= 1;
2304 device
->shader_backend
->shader_free_context_data(context
);
2305 device
->adapter
->fragment_pipe
->free_context_data(context
);
2306 heap_free(context
->texture_type
);
2307 device_context_remove(device
, context
);
2312 const DWORD
*context_get_tex_unit_mapping(const struct wined3d_context
*context
,
2313 const struct wined3d_shader_version
*shader_version
, unsigned int *base
, unsigned int *count
)
2315 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2317 if (!shader_version
)
2320 *count
= MAX_TEXTURES
;
2321 return context
->tex_unit_map
;
2324 if (shader_version
->major
>= 4)
2326 wined3d_gl_limits_get_texture_unit_range(&gl_info
->limits
, shader_version
->type
, base
, count
);
2330 switch (shader_version
->type
)
2332 case WINED3D_SHADER_TYPE_PIXEL
:
2334 *count
= MAX_FRAGMENT_SAMPLERS
;
2336 case WINED3D_SHADER_TYPE_VERTEX
:
2337 *base
= MAX_FRAGMENT_SAMPLERS
;
2338 *count
= MAX_VERTEX_SAMPLERS
;
2341 ERR("Unhandled shader type %#x.\n", shader_version
->type
);
2346 return context
->tex_unit_map
;
2349 static void context_get_rt_size(const struct wined3d_context
*context
, SIZE
*size
)
2351 const struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2358 GetClientRect(context
->win_handle
, &window_size
);
2359 size
->cx
= window_size
.right
- window_size
.left
;
2360 size
->cy
= window_size
.bottom
- window_size
.top
;
2365 level
= context
->current_rt
.sub_resource_idx
% rt
->level_count
;
2366 size
->cx
= wined3d_texture_get_level_width(rt
, level
);
2367 size
->cy
= wined3d_texture_get_level_height(rt
, level
);
2370 void context_enable_clip_distances(struct wined3d_context
*context
, unsigned int enable_mask
)
2372 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2373 unsigned int clip_distance_count
= gl_info
->limits
.user_clip_distances
;
2374 unsigned int i
, disable_mask
, current_mask
;
2376 disable_mask
= ~enable_mask
;
2377 enable_mask
&= (1u << clip_distance_count
) - 1;
2378 disable_mask
&= (1u << clip_distance_count
) - 1;
2379 current_mask
= context
->clip_distance_mask
;
2380 context
->clip_distance_mask
= enable_mask
;
2382 enable_mask
&= ~current_mask
;
2385 i
= wined3d_bit_scan(&enable_mask
);
2386 gl_info
->gl_ops
.gl
.p_glEnable(GL_CLIP_DISTANCE0
+ i
);
2388 disable_mask
&= current_mask
;
2389 while (disable_mask
)
2391 i
= wined3d_bit_scan(&disable_mask
);
2392 gl_info
->gl_ops
.gl
.p_glDisable(GL_CLIP_DISTANCE0
+ i
);
2394 checkGLcall("toggle clip distances");
2397 static inline BOOL
is_rt_mask_onscreen(DWORD rt_mask
)
2399 return rt_mask
& (1u << 31);
2402 static inline GLenum
draw_buffer_from_rt_mask(DWORD rt_mask
)
2404 return rt_mask
& ~(1u << 31);
2407 /* Context activation is done by the caller. */
2408 static void context_apply_draw_buffers(struct wined3d_context
*context
, DWORD rt_mask
)
2410 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2411 GLenum draw_buffers
[MAX_RENDER_TARGET_VIEWS
];
2415 gl_info
->gl_ops
.gl
.p_glDrawBuffer(GL_NONE
);
2417 else if (is_rt_mask_onscreen(rt_mask
))
2419 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask
));
2423 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2430 draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
2432 draw_buffers
[i
] = GL_NONE
;
2438 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
2440 GL_EXTCALL(glDrawBuffers(i
, draw_buffers
));
2444 gl_info
->gl_ops
.gl
.p_glDrawBuffer(draw_buffers
[0]);
2449 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2453 checkGLcall("apply draw buffers");
2456 /* Context activation is done by the caller. */
2457 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
2459 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2460 DWORD
*current_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2461 DWORD new_mask
= context_generate_rt_mask(buffer
);
2463 if (new_mask
== *current_mask
)
2466 gl_info
->gl_ops
.gl
.p_glDrawBuffer(buffer
);
2467 checkGLcall("glDrawBuffer()");
2469 *current_mask
= new_mask
;
2472 /* Context activation is done by the caller. */
2473 void context_active_texture(struct wined3d_context
*context
, const struct wined3d_gl_info
*gl_info
, unsigned int unit
)
2475 GL_EXTCALL(glActiveTexture(GL_TEXTURE0
+ unit
));
2476 checkGLcall("glActiveTexture");
2477 context
->active_texture
= unit
;
2480 void context_bind_bo(struct wined3d_context
*context
, GLenum binding
, GLuint name
)
2482 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2484 if (binding
== GL_ELEMENT_ARRAY_BUFFER
)
2485 context_invalidate_state(context
, STATE_INDEXBUFFER
);
2487 GL_EXTCALL(glBindBuffer(binding
, name
));
2490 void context_bind_texture(struct wined3d_context
*context
, GLenum target
, GLuint name
)
2492 const struct wined3d_dummy_textures
*textures
= &context
->device
->dummy_textures
;
2493 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2494 DWORD unit
= context
->active_texture
;
2495 DWORD old_texture_type
= context
->texture_type
[unit
];
2499 gl_info
->gl_ops
.gl
.p_glBindTexture(target
, name
);
2506 if (old_texture_type
!= target
)
2508 switch (old_texture_type
)
2514 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D
, textures
->tex_1d
);
2516 case GL_TEXTURE_1D_ARRAY
:
2517 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_1D_ARRAY
, textures
->tex_1d_array
);
2520 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D
, textures
->tex_2d
);
2522 case GL_TEXTURE_2D_ARRAY
:
2523 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_ARRAY
, textures
->tex_2d_array
);
2525 case GL_TEXTURE_RECTANGLE_ARB
:
2526 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, textures
->tex_rect
);
2528 case GL_TEXTURE_CUBE_MAP
:
2529 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP
, textures
->tex_cube
);
2531 case GL_TEXTURE_CUBE_MAP_ARRAY
:
2532 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY
, textures
->tex_cube_array
);
2535 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_3D
, textures
->tex_3d
);
2537 case GL_TEXTURE_BUFFER
:
2538 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_BUFFER
, textures
->tex_buffer
);
2540 case GL_TEXTURE_2D_MULTISAMPLE
:
2541 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE
, textures
->tex_2d_ms
);
2543 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
2544 gl_info
->gl_ops
.gl
.p_glBindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, textures
->tex_2d_ms_array
);
2547 ERR("Unexpected texture target %#x.\n", old_texture_type
);
2550 context
->texture_type
[unit
] = target
;
2553 checkGLcall("bind texture");
2556 void *context_map_bo_address(struct wined3d_context
*context
,
2557 const struct wined3d_bo_address
*data
, size_t size
, GLenum binding
, DWORD flags
)
2559 const struct wined3d_gl_info
*gl_info
;
2562 if (!data
->buffer_object
)
2565 gl_info
= context
->gl_info
;
2566 context_bind_bo(context
, binding
, data
->buffer_object
);
2568 if (gl_info
->supported
[ARB_MAP_BUFFER_RANGE
])
2570 GLbitfield map_flags
= wined3d_resource_gl_map_flags(flags
) & ~GL_MAP_FLUSH_EXPLICIT_BIT
;
2571 memory
= GL_EXTCALL(glMapBufferRange(binding
, (INT_PTR
)data
->addr
, size
, map_flags
));
2575 memory
= GL_EXTCALL(glMapBuffer(binding
, wined3d_resource_gl_legacy_map_flags(flags
)));
2576 memory
+= (INT_PTR
)data
->addr
;
2579 context_bind_bo(context
, binding
, 0);
2580 checkGLcall("Map buffer object");
2585 void context_unmap_bo_address(struct wined3d_context
*context
,
2586 const struct wined3d_bo_address
*data
, GLenum binding
)
2588 const struct wined3d_gl_info
*gl_info
;
2590 if (!data
->buffer_object
)
2593 gl_info
= context
->gl_info
;
2594 context_bind_bo(context
, binding
, data
->buffer_object
);
2595 GL_EXTCALL(glUnmapBuffer(binding
));
2596 context_bind_bo(context
, binding
, 0);
2597 checkGLcall("Unmap buffer object");
2600 void context_copy_bo_address(struct wined3d_context
*context
,
2601 const struct wined3d_bo_address
*dst
, GLenum dst_binding
,
2602 const struct wined3d_bo_address
*src
, GLenum src_binding
, size_t size
)
2604 const struct wined3d_gl_info
*gl_info
;
2605 BYTE
*dst_ptr
, *src_ptr
;
2607 gl_info
= context
->gl_info
;
2609 if (dst
->buffer_object
&& src
->buffer_object
)
2611 if (gl_info
->supported
[ARB_COPY_BUFFER
])
2613 GL_EXTCALL(glBindBuffer(GL_COPY_READ_BUFFER
, src
->buffer_object
));
2614 GL_EXTCALL(glBindBuffer(GL_COPY_WRITE_BUFFER
, dst
->buffer_object
));
2615 GL_EXTCALL(glCopyBufferSubData(GL_COPY_READ_BUFFER
, GL_COPY_WRITE_BUFFER
,
2616 (GLintptr
)src
->addr
, (GLintptr
)dst
->addr
, size
));
2617 checkGLcall("direct buffer copy");
2621 src_ptr
= context_map_bo_address(context
, src
, size
, src_binding
, WINED3D_MAP_READ
);
2622 dst_ptr
= context_map_bo_address(context
, dst
, size
, dst_binding
, WINED3D_MAP_WRITE
);
2624 memcpy(dst_ptr
, src_ptr
, size
);
2626 context_unmap_bo_address(context
, dst
, dst_binding
);
2627 context_unmap_bo_address(context
, src
, src_binding
);
2630 else if (!dst
->buffer_object
&& src
->buffer_object
)
2632 context_bind_bo(context
, src_binding
, src
->buffer_object
);
2633 GL_EXTCALL(glGetBufferSubData(src_binding
, (GLintptr
)src
->addr
, size
, dst
->addr
));
2634 checkGLcall("buffer download");
2636 else if (dst
->buffer_object
&& !src
->buffer_object
)
2638 context_bind_bo(context
, dst_binding
, dst
->buffer_object
);
2639 GL_EXTCALL(glBufferSubData(dst_binding
, (GLintptr
)dst
->addr
, size
, src
->addr
));
2640 checkGLcall("buffer upload");
2644 memcpy(dst
->addr
, src
->addr
, size
);
2648 static void context_set_render_offscreen(struct wined3d_context
*context
, BOOL offscreen
)
2650 if (context
->render_offscreen
== offscreen
)
2653 context_invalidate_state(context
, STATE_VIEWPORT
);
2654 context_invalidate_state(context
, STATE_SCISSORRECT
);
2655 if (!context
->gl_info
->supported
[ARB_CLIP_CONTROL
])
2657 context_invalidate_state(context
, STATE_FRONTFACE
);
2658 context_invalidate_state(context
, STATE_POINTSPRITECOORDORIGIN
);
2659 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2661 context_invalidate_state(context
, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN
));
2662 if (context
->gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
2663 context_invalidate_state(context
, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
));
2664 context
->render_offscreen
= offscreen
;
2667 static BOOL
match_depth_stencil_format(const struct wined3d_format
*existing
,
2668 const struct wined3d_format
*required
)
2670 if (existing
== required
)
2672 if ((existing
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
)
2673 != (required
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_FLOAT
))
2675 if (existing
->depth_size
< required
->depth_size
)
2677 /* If stencil bits are used the exact amount is required - otherwise
2678 * wrapping won't work correctly. */
2679 if (required
->stencil_size
&& required
->stencil_size
!= existing
->stencil_size
)
2684 /* Context activation is done by the caller. */
2685 static void context_validate_onscreen_formats(struct wined3d_context
*context
,
2686 const struct wined3d_rendertarget_view
*depth_stencil
)
2688 /* Onscreen surfaces are always in a swapchain */
2689 struct wined3d_swapchain
*swapchain
= context
->current_rt
.texture
->swapchain
;
2691 if (context
->render_offscreen
|| !depth_stencil
) return;
2692 if (match_depth_stencil_format(swapchain
->ds_format
, depth_stencil
->format
)) return;
2694 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2695 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2697 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2699 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2700 if (!(wined3d_texture_load_location(context
->current_rt
.texture
, context
->current_rt
.sub_resource_idx
,
2701 context
, WINED3D_LOCATION_TEXTURE_RGB
)))
2702 ERR("Failed to load location.\n");
2703 swapchain
->render_to_fbo
= TRUE
;
2704 swapchain_update_draw_bindings(swapchain
);
2705 context_set_render_offscreen(context
, TRUE
);
2708 GLenum
context_get_offscreen_gl_buffer(const struct wined3d_context
*context
)
2710 switch (wined3d_settings
.offscreen_rendering_mode
)
2713 return GL_COLOR_ATTACHMENT0
;
2715 case ORM_BACKBUFFER
:
2716 return context
->aux_buffers
> 0 ? GL_AUX0
: GL_BACK
;
2719 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings
.offscreen_rendering_mode
);
2724 static DWORD
context_generate_rt_mask_no_fbo(const struct wined3d_context
*context
, struct wined3d_resource
*rt
)
2726 if (!rt
|| rt
->format
->id
== WINED3DFMT_NULL
)
2728 else if (rt
->type
!= WINED3D_RTYPE_BUFFER
&& texture_from_resource(rt
)->swapchain
)
2729 return context_generate_rt_mask_from_resource(rt
);
2731 return context_generate_rt_mask(context_get_offscreen_gl_buffer(context
));
2734 /* Context activation is done by the caller. */
2735 void context_apply_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2737 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2738 struct wined3d_texture
*rt
= context
->current_rt
.texture
;
2739 DWORD rt_mask
, *cur_mask
;
2740 unsigned int sampler
;
2743 TRACE("Setting up context %p for blitting.\n", context
);
2745 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2747 if (context
->render_offscreen
)
2749 wined3d_texture_load(rt
, context
, FALSE
);
2751 context_apply_fbo_state_blit(context
, GL_FRAMEBUFFER
, &rt
->resource
,
2752 context
->current_rt
.sub_resource_idx
, NULL
, 0, rt
->resource
.draw_binding
);
2753 if (rt
->resource
.format
->id
!= WINED3DFMT_NULL
)
2760 context
->current_fbo
= NULL
;
2761 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
2762 rt_mask
= context_generate_rt_mask_from_resource(&rt
->resource
);
2767 rt_mask
= context_generate_rt_mask_no_fbo(context
, &rt
->resource
);
2770 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
2772 if (rt_mask
!= *cur_mask
)
2774 context_apply_draw_buffers(context
, rt_mask
);
2775 *cur_mask
= rt_mask
;
2778 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
2780 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
2782 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
2784 context_get_rt_size(context
, &rt_size
);
2786 if (context
->last_was_blit
)
2788 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
2790 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, rt_size
.cx
, rt_size
.cy
);
2791 context
->blit_w
= rt_size
.cx
;
2792 context
->blit_h
= rt_size
.cy
;
2793 /* No need to dirtify here, the states are still dirtified because
2794 * they weren't applied since the last context_apply_blit_state()
2797 checkGLcall("blit state application");
2798 TRACE("Context is already set up for blitting, nothing to do.\n");
2801 context
->last_was_blit
= TRUE
;
2803 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
2804 GL_EXTCALL(glBindSampler(0, 0));
2805 context_active_texture(context
, gl_info
, 0);
2807 sampler
= context
->rev_tex_unit_map
[0];
2808 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2810 if (sampler
< MAX_TEXTURES
)
2812 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_TEXTURE0
+ sampler
));
2813 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2815 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2818 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2820 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
2821 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
));
2823 gl_info
->gl_ops
.gl
.p_glDisable(GL_DEPTH_TEST
);
2824 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ZENABLE
));
2825 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
2826 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
2827 gl_info
->gl_ops
.gl
.p_glDisable(GL_CULL_FACE
);
2828 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CULLMODE
));
2829 gl_info
->gl_ops
.gl
.p_glDisable(GL_STENCIL_TEST
);
2830 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_STENCILENABLE
));
2831 gl_info
->gl_ops
.gl
.p_glDisable(GL_SCISSOR_TEST
);
2832 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
2833 if (gl_info
->supported
[ARB_POINT_SPRITE
])
2835 gl_info
->gl_ops
.gl
.p_glDisable(GL_POINT_SPRITE_ARB
);
2836 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
));
2838 if (gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
2840 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
2841 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
2843 gl_info
->gl_ops
.gl
.p_glColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
2844 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE
));
2845 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1
));
2846 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2
));
2847 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3
));
2849 context
->last_was_rhw
= TRUE
;
2850 context_invalidate_state(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
2852 context_enable_clip_distances(context
, 0);
2853 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_CLIPPING
));
2855 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
2856 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
2857 GL_EXTCALL(glClipControl(GL_LOWER_LEFT
, GL_NEGATIVE_ONE_TO_ONE
));
2858 gl_info
->gl_ops
.gl
.p_glViewport(0, 0, rt_size
.cx
, rt_size
.cy
);
2859 context_invalidate_state(context
, STATE_VIEWPORT
);
2861 device
->shader_backend
->shader_disable(device
->shader_priv
, context
);
2863 context
->blit_w
= rt_size
.cx
;
2864 context
->blit_h
= rt_size
.cy
;
2866 checkGLcall("blit state application");
2869 static void context_apply_blit_projection(const struct wined3d_context
*context
, unsigned int w
, unsigned int h
)
2871 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2872 const GLdouble projection
[] =
2874 2.0 / w
, 0.0, 0.0, 0.0,
2875 0.0, 2.0 / h
, 0.0, 0.0,
2877 -1.0, -1.0, -1.0, 1.0,
2880 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_PROJECTION
);
2881 gl_info
->gl_ops
.gl
.p_glLoadMatrixd(projection
);
2884 /* Setup OpenGL states for fixed-function blitting. */
2885 /* Context activation is done by the caller. */
2886 void context_apply_ffp_blit_state(struct wined3d_context
*context
, const struct wined3d_device
*device
)
2888 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2889 unsigned int i
, sampler
;
2891 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2892 ERR("Applying fixed-function state without legacy context support.\n");
2894 if (context
->last_was_ffp_blit
)
2898 context_get_rt_size(context
, &rt_size
);
2899 if (context
->blit_w
!= rt_size
.cx
|| context
->blit_h
!= rt_size
.cy
)
2900 context_apply_blit_projection(context
, rt_size
.cx
, rt_size
.cy
);
2901 context_apply_blit_state(context
, device
);
2903 checkGLcall("ffp blit state application");
2906 context
->last_was_ffp_blit
= TRUE
;
2908 context_apply_blit_state(context
, device
);
2910 /* Disable all textures. The caller can then bind a texture it wants to blit
2912 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
2914 context_active_texture(context
, gl_info
, i
);
2916 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2917 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2918 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2919 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2920 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2921 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2923 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2925 sampler
= context
->rev_tex_unit_map
[i
];
2926 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
2928 if (sampler
< MAX_TEXTURES
)
2929 context_invalidate_state(context
, STATE_TEXTURESTAGE(sampler
, WINED3D_TSS_COLOR_OP
));
2930 context_invalidate_state(context
, STATE_SAMPLER(sampler
));
2934 context_active_texture(context
, gl_info
, 0);
2936 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
2937 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
2938 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_3D
);
2939 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
2940 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_RECTANGLE_ARB
);
2941 gl_info
->gl_ops
.gl
.p_glDisable(GL_TEXTURE_2D
);
2943 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
2944 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
2945 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
, GL_TEXTURE_LOD_BIAS_EXT
, 0.0f
);
2947 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_TEXTURE
);
2948 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2950 /* Setup transforms. */
2951 gl_info
->gl_ops
.gl
.p_glMatrixMode(GL_MODELVIEW
);
2952 gl_info
->gl_ops
.gl
.p_glLoadIdentity();
2953 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2954 context_apply_blit_projection(context
, context
->blit_w
, context
->blit_h
);
2955 context_invalidate_state(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
2957 /* Other misc states. */
2958 gl_info
->gl_ops
.gl
.p_glDisable(GL_LIGHTING
);
2959 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_LIGHTING
));
2960 glDisableWINE(GL_FOG
);
2961 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
));
2963 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
2965 gl_info
->gl_ops
.gl
.p_glDisable(GL_COLOR_SUM_EXT
);
2966 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SPECULARENABLE
));
2968 checkGLcall("ffp blit state application");
2971 static BOOL
have_framebuffer_attachment(unsigned int rt_count
, struct wined3d_rendertarget_view
* const *rts
,
2972 const struct wined3d_rendertarget_view
*ds
)
2979 for (i
= 0; i
< rt_count
; ++i
)
2981 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
2988 /* Context activation is done by the caller. */
2989 BOOL
context_apply_clear_state(struct wined3d_context
*context
, const struct wined3d_state
*state
,
2990 UINT rt_count
, const struct wined3d_fb_state
*fb
)
2992 struct wined3d_rendertarget_view
* const *rts
= fb
->render_targets
;
2993 struct wined3d_rendertarget_view
*dsv
= fb
->depth_stencil
;
2994 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2995 DWORD rt_mask
= 0, *cur_mask
;
2998 if (isStateDirty(context
, STATE_FRAMEBUFFER
) || fb
!= state
->fb
2999 || rt_count
!= gl_info
->limits
.buffers
)
3001 if (!have_framebuffer_attachment(rt_count
, rts
, dsv
))
3003 WARN("Invalid render target config, need at least one attachment.\n");
3007 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3009 struct wined3d_rendertarget_info ds_info
= {{0}};
3011 context_validate_onscreen_formats(context
, dsv
);
3013 if (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
))
3015 memset(context
->blit_targets
, 0, sizeof(context
->blit_targets
));
3016 for (i
= 0; i
< rt_count
; ++i
)
3020 context
->blit_targets
[i
].gl_view
= rts
[i
]->gl_view
;
3021 context
->blit_targets
[i
].resource
= rts
[i
]->resource
;
3022 context
->blit_targets
[i
].sub_resource_idx
= rts
[i
]->sub_resource_idx
;
3023 context
->blit_targets
[i
].layer_count
= rts
[i
]->layer_count
;
3025 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3026 rt_mask
|= (1u << i
);
3031 ds_info
.gl_view
= dsv
->gl_view
;
3032 ds_info
.resource
= dsv
->resource
;
3033 ds_info
.sub_resource_idx
= dsv
->sub_resource_idx
;
3034 ds_info
.layer_count
= dsv
->layer_count
;
3037 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, &ds_info
,
3038 rt_count
? rts
[0]->resource
->draw_binding
: 0,
3039 dsv
? dsv
->resource
->draw_binding
: 0);
3043 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, &ds_info
,
3044 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
3045 rt_mask
= context_generate_rt_mask_from_resource(rts
[0]->resource
);
3048 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
3049 * next draw. Otherwise we could mark the framebuffer state clean here, once the
3050 * state management allows this */
3051 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
3055 rt_mask
= context_generate_rt_mask_no_fbo(context
, rt_count
? rts
[0]->resource
: NULL
);
3058 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
3059 && (!rt_count
|| wined3d_resource_is_offscreen(rts
[0]->resource
)))
3061 for (i
= 0; i
< rt_count
; ++i
)
3063 if (rts
[i
] && rts
[i
]->format
->id
!= WINED3DFMT_NULL
)
3064 rt_mask
|= (1u << i
);
3069 rt_mask
= context_generate_rt_mask_no_fbo(context
, rt_count
? rts
[0]->resource
: NULL
);
3072 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
3074 if (rt_mask
!= *cur_mask
)
3076 context_apply_draw_buffers(context
, rt_mask
);
3077 *cur_mask
= rt_mask
;
3078 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
3081 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3083 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
3086 context
->last_was_blit
= FALSE
;
3087 context
->last_was_ffp_blit
= FALSE
;
3089 /* Blending and clearing should be orthogonal, but tests on the nvidia
3090 * driver show that disabling blending when clearing improves the clearing
3091 * performance incredibly. */
3092 gl_info
->gl_ops
.gl
.p_glDisable(GL_BLEND
);
3093 gl_info
->gl_ops
.gl
.p_glEnable(GL_SCISSOR_TEST
);
3094 if (rt_count
&& gl_info
->supported
[ARB_FRAMEBUFFER_SRGB
])
3096 if (needs_srgb_write(context
, state
, fb
))
3097 gl_info
->gl_ops
.gl
.p_glEnable(GL_FRAMEBUFFER_SRGB
);
3099 gl_info
->gl_ops
.gl
.p_glDisable(GL_FRAMEBUFFER_SRGB
);
3100 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
3102 checkGLcall("setting up state for clear");
3104 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
3105 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE
));
3106 context_invalidate_state(context
, STATE_SCISSORRECT
);
3111 static DWORD
find_draw_buffers_mask(const struct wined3d_context
*context
, const struct wined3d_state
*state
)
3113 struct wined3d_rendertarget_view
* const *rts
= state
->fb
->render_targets
;
3114 struct wined3d_shader
*ps
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
3115 DWORD rt_mask
, mask
;
3118 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
3119 return context_generate_rt_mask_no_fbo(context
, rts
[0]->resource
);
3120 else if (!context
->render_offscreen
)
3121 return context_generate_rt_mask_from_resource(rts
[0]->resource
);
3123 rt_mask
= ps
? ps
->reg_maps
.rt_mask
: 1;
3124 rt_mask
&= context
->d3d_info
->valid_rt_mask
;
3129 i
= wined3d_bit_scan(&mask
);
3130 if (!rts
[i
] || rts
[i
]->format
->id
== WINED3DFMT_NULL
)
3131 rt_mask
&= ~(1u << i
);
3137 /* Context activation is done by the caller. */
3138 void context_state_fb(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3140 DWORD rt_mask
= find_draw_buffers_mask(context
, state
);
3141 const struct wined3d_fb_state
*fb
= state
->fb
;
3142 DWORD color_location
= 0;
3145 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
3147 struct wined3d_rendertarget_info ds_info
= {{0}};
3149 if (!context
->render_offscreen
)
3151 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, NULL
, &ds_info
,
3152 WINED3D_LOCATION_DRAWABLE
, WINED3D_LOCATION_DRAWABLE
);
3158 memset(context
->blit_targets
, 0, sizeof(context
->blit_targets
));
3159 for (i
= 0; i
< context
->gl_info
->limits
.buffers
; ++i
)
3161 if (!fb
->render_targets
[i
])
3164 context
->blit_targets
[i
].gl_view
= fb
->render_targets
[i
]->gl_view
;
3165 context
->blit_targets
[i
].resource
= fb
->render_targets
[i
]->resource
;
3166 context
->blit_targets
[i
].sub_resource_idx
= fb
->render_targets
[i
]->sub_resource_idx
;
3167 context
->blit_targets
[i
].layer_count
= fb
->render_targets
[i
]->layer_count
;
3169 if (!color_location
)
3170 color_location
= fb
->render_targets
[i
]->resource
->draw_binding
;
3173 if (fb
->depth_stencil
)
3175 ds_info
.gl_view
= fb
->depth_stencil
->gl_view
;
3176 ds_info
.resource
= fb
->depth_stencil
->resource
;
3177 ds_info
.sub_resource_idx
= fb
->depth_stencil
->sub_resource_idx
;
3178 ds_info
.layer_count
= fb
->depth_stencil
->layer_count
;
3181 context_apply_fbo_state(context
, GL_FRAMEBUFFER
, context
->blit_targets
, &ds_info
,
3182 color_location
, fb
->depth_stencil
? fb
->depth_stencil
->resource
->draw_binding
: 0);
3186 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
3187 if (rt_mask
!= *cur_mask
)
3189 context_apply_draw_buffers(context
, rt_mask
);
3190 *cur_mask
= rt_mask
;
3192 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
3195 static void context_map_stage(struct wined3d_context
*context
, DWORD stage
, DWORD unit
)
3197 DWORD i
= context
->rev_tex_unit_map
[unit
];
3198 DWORD j
= context
->tex_unit_map
[stage
];
3200 TRACE("Mapping stage %u to unit %u.\n", stage
, unit
);
3201 context
->tex_unit_map
[stage
] = unit
;
3202 if (i
!= WINED3D_UNMAPPED_STAGE
&& i
!= stage
)
3203 context
->tex_unit_map
[i
] = WINED3D_UNMAPPED_STAGE
;
3205 context
->rev_tex_unit_map
[unit
] = stage
;
3206 if (j
!= WINED3D_UNMAPPED_STAGE
&& j
!= unit
)
3207 context
->rev_tex_unit_map
[j
] = WINED3D_UNMAPPED_STAGE
;
3210 static void context_invalidate_texture_stage(struct wined3d_context
*context
, DWORD stage
)
3214 for (i
= 0; i
<= WINED3D_HIGHEST_TEXTURE_STATE
; ++i
)
3215 context_invalidate_state(context
, STATE_TEXTURESTAGE(stage
, i
));
3218 static void context_update_fixed_function_usage_map(struct wined3d_context
*context
,
3219 const struct wined3d_state
*state
)
3223 context
->fixed_function_usage_map
= 0;
3224 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
3226 enum wined3d_texture_op color_op
= state
->texture_states
[i
][WINED3D_TSS_COLOR_OP
];
3227 enum wined3d_texture_op alpha_op
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_OP
];
3228 DWORD color_arg1
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG1
] & WINED3DTA_SELECTMASK
;
3229 DWORD color_arg2
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG2
] & WINED3DTA_SELECTMASK
;
3230 DWORD color_arg3
= state
->texture_states
[i
][WINED3D_TSS_COLOR_ARG0
] & WINED3DTA_SELECTMASK
;
3231 DWORD alpha_arg1
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG1
] & WINED3DTA_SELECTMASK
;
3232 DWORD alpha_arg2
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG2
] & WINED3DTA_SELECTMASK
;
3233 DWORD alpha_arg3
= state
->texture_states
[i
][WINED3D_TSS_ALPHA_ARG0
] & WINED3DTA_SELECTMASK
;
3235 /* Not used, and disable higher stages. */
3236 if (color_op
== WINED3D_TOP_DISABLE
)
3239 if (((color_arg1
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG2
)
3240 || ((color_arg2
== WINED3DTA_TEXTURE
) && color_op
!= WINED3D_TOP_SELECT_ARG1
)
3241 || ((color_arg3
== WINED3DTA_TEXTURE
)
3242 && (color_op
== WINED3D_TOP_MULTIPLY_ADD
|| color_op
== WINED3D_TOP_LERP
))
3243 || ((alpha_arg1
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG2
)
3244 || ((alpha_arg2
== WINED3DTA_TEXTURE
) && alpha_op
!= WINED3D_TOP_SELECT_ARG1
)
3245 || ((alpha_arg3
== WINED3DTA_TEXTURE
)
3246 && (alpha_op
== WINED3D_TOP_MULTIPLY_ADD
|| alpha_op
== WINED3D_TOP_LERP
)))
3247 context
->fixed_function_usage_map
|= (1u << i
);
3249 if ((color_op
== WINED3D_TOP_BUMPENVMAP
|| color_op
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
3250 && i
< MAX_TEXTURES
- 1)
3251 context
->fixed_function_usage_map
|= (1u << (i
+ 1));
3254 if (i
< context
->lowest_disabled_stage
)
3257 end
= context
->lowest_disabled_stage
;
3261 start
= context
->lowest_disabled_stage
;
3265 context
->lowest_disabled_stage
= i
;
3266 for (i
= start
+ 1; i
< end
; ++i
)
3268 context_invalidate_state(context
, STATE_TEXTURESTAGE(i
, WINED3D_TSS_COLOR_OP
));
3272 static void context_map_fixed_function_samplers(struct wined3d_context
*context
,
3273 const struct wined3d_state
*state
)
3275 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3276 unsigned int i
, tex
;
3279 ffu_map
= context
->fixed_function_usage_map
;
3281 if (d3d_info
->limits
.ffp_textures
== d3d_info
->limits
.ffp_blend_stages
3282 || context
->lowest_disabled_stage
<= d3d_info
->limits
.ffp_textures
)
3284 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3289 if (context
->tex_unit_map
[i
] != i
)
3291 context_map_stage(context
, i
, i
);
3292 context_invalidate_state(context
, STATE_SAMPLER(i
));
3293 context_invalidate_texture_stage(context
, i
);
3299 /* Now work out the mapping */
3301 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3306 if (context
->tex_unit_map
[i
] != tex
)
3308 context_map_stage(context
, i
, tex
);
3309 context_invalidate_state(context
, STATE_SAMPLER(i
));
3310 context_invalidate_texture_stage(context
, i
);
3317 static void context_map_psamplers(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3319 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3320 const struct wined3d_shader_resource_info
*resource_info
=
3321 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3324 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3326 if (resource_info
[i
].type
&& context
->tex_unit_map
[i
] != i
)
3328 context_map_stage(context
, i
, i
);
3329 context_invalidate_state(context
, STATE_SAMPLER(i
));
3330 if (i
< d3d_info
->limits
.ffp_blend_stages
)
3331 context_invalidate_texture_stage(context
, i
);
3336 static BOOL
context_unit_free_for_vs(const struct wined3d_context
*context
,
3337 const struct wined3d_shader_resource_info
*ps_resource_info
, DWORD unit
)
3339 DWORD current_mapping
= context
->rev_tex_unit_map
[unit
];
3341 /* Not currently used */
3342 if (current_mapping
== WINED3D_UNMAPPED_STAGE
)
3345 if (current_mapping
< MAX_FRAGMENT_SAMPLERS
)
3347 /* Used by a fragment sampler */
3349 if (!ps_resource_info
)
3351 /* No pixel shader, check fixed function */
3352 return current_mapping
>= MAX_TEXTURES
|| !(context
->fixed_function_usage_map
& (1u << current_mapping
));
3355 /* Pixel shader, check the shader's sampler map */
3356 return !ps_resource_info
[current_mapping
].type
;
3362 static void context_map_vsamplers(struct wined3d_context
*context
, BOOL ps
, const struct wined3d_state
*state
)
3364 const struct wined3d_shader_resource_info
*vs_resource_info
=
3365 state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
;
3366 const struct wined3d_shader_resource_info
*ps_resource_info
= NULL
;
3367 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3368 int start
= min(MAX_COMBINED_SAMPLERS
, gl_info
->limits
.graphics_samplers
) - 1;
3371 /* Note that we only care if a resource is used or not, not the
3372 * resource's specific type. Otherwise we'd need to call
3373 * shader_update_samplers() here for 1.x pixelshaders. */
3375 ps_resource_info
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
;
3377 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3379 DWORD vsampler_idx
= i
+ MAX_FRAGMENT_SAMPLERS
;
3380 if (vs_resource_info
[i
].type
)
3384 if (context_unit_free_for_vs(context
, ps_resource_info
, start
))
3386 if (context
->tex_unit_map
[vsampler_idx
] != start
)
3388 context_map_stage(context
, vsampler_idx
, start
);
3389 context_invalidate_state(context
, STATE_SAMPLER(vsampler_idx
));
3398 if (context
->tex_unit_map
[vsampler_idx
] == WINED3D_UNMAPPED_STAGE
)
3399 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i
);
3404 static void context_update_tex_unit_map(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3406 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3407 BOOL vs
= use_vs(state
);
3408 BOOL ps
= use_ps(state
);
3411 context_update_fixed_function_usage_map(context
, state
);
3413 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3414 * need a 1:1 map at the moment.
3415 * When the mapping of a stage is changed, sampler and ALL texture stage
3416 * states have to be reset. */
3418 if (gl_info
->limits
.graphics_samplers
>= MAX_COMBINED_SAMPLERS
)
3422 context_map_psamplers(context
, state
);
3424 context_map_fixed_function_samplers(context
, state
);
3427 context_map_vsamplers(context
, ps
, state
);
3430 /* Context activation is done by the caller. */
3431 void context_state_drawbuf(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
3433 DWORD rt_mask
, *cur_mask
;
3435 if (isStateDirty(context
, STATE_FRAMEBUFFER
)) return;
3437 cur_mask
= context
->current_fbo
? &context
->current_fbo
->rt_mask
: &context
->draw_buffers_mask
;
3438 rt_mask
= find_draw_buffers_mask(context
, state
);
3439 if (rt_mask
!= *cur_mask
)
3441 context_apply_draw_buffers(context
, rt_mask
);
3442 *cur_mask
= rt_mask
;
3446 static BOOL
fixed_get_input(BYTE usage
, BYTE usage_idx
, unsigned int *regnum
)
3448 if ((usage
== WINED3D_DECL_USAGE_POSITION
|| usage
== WINED3D_DECL_USAGE_POSITIONT
) && !usage_idx
)
3449 *regnum
= WINED3D_FFP_POSITION
;
3450 else if (usage
== WINED3D_DECL_USAGE_BLEND_WEIGHT
&& !usage_idx
)
3451 *regnum
= WINED3D_FFP_BLENDWEIGHT
;
3452 else if (usage
== WINED3D_DECL_USAGE_BLEND_INDICES
&& !usage_idx
)
3453 *regnum
= WINED3D_FFP_BLENDINDICES
;
3454 else if (usage
== WINED3D_DECL_USAGE_NORMAL
&& !usage_idx
)
3455 *regnum
= WINED3D_FFP_NORMAL
;
3456 else if (usage
== WINED3D_DECL_USAGE_PSIZE
&& !usage_idx
)
3457 *regnum
= WINED3D_FFP_PSIZE
;
3458 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& !usage_idx
)
3459 *regnum
= WINED3D_FFP_DIFFUSE
;
3460 else if (usage
== WINED3D_DECL_USAGE_COLOR
&& usage_idx
== 1)
3461 *regnum
= WINED3D_FFP_SPECULAR
;
3462 else if (usage
== WINED3D_DECL_USAGE_TEXCOORD
&& usage_idx
< WINED3DDP_MAXTEXCOORD
)
3463 *regnum
= WINED3D_FFP_TEXCOORD0
+ usage_idx
;
3466 WARN("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage
), usage_idx
);
3474 /* Context activation is done by the caller. */
3475 void wined3d_stream_info_from_declaration(struct wined3d_stream_info
*stream_info
,
3476 const struct wined3d_state
*state
, const struct wined3d_gl_info
*gl_info
,
3477 const struct wined3d_d3d_info
*d3d_info
)
3479 /* We need to deal with frequency data! */
3480 struct wined3d_vertex_declaration
*declaration
= state
->vertex_declaration
;
3481 BOOL generic_attributes
= d3d_info
->ffp_generic_attributes
;
3482 BOOL use_vshader
= use_vs(state
);
3485 stream_info
->use_map
= 0;
3486 stream_info
->swizzle_map
= 0;
3487 stream_info
->position_transformed
= 0;
3492 stream_info
->position_transformed
= declaration
->position_transformed
;
3494 /* Translate the declaration into strided data. */
3495 for (i
= 0; i
< declaration
->element_count
; ++i
)
3497 const struct wined3d_vertex_declaration_element
*element
= &declaration
->elements
[i
];
3498 const struct wined3d_stream_state
*stream
= &state
->streams
[element
->input_slot
];
3502 TRACE("%p Element %p (%u of %u).\n", declaration
->elements
,
3503 element
, i
+ 1, declaration
->element_count
);
3505 if (!stream
->buffer
)
3508 TRACE("offset %u input_slot %u usage_idx %d.\n", element
->offset
, element
->input_slot
, element
->usage_idx
);
3512 if (element
->output_slot
== WINED3D_OUTPUT_SLOT_UNUSED
)
3514 stride_used
= FALSE
;
3516 else if (element
->output_slot
== WINED3D_OUTPUT_SLOT_SEMANTIC
)
3518 /* TODO: Assuming vertexdeclarations are usually used with the
3519 * same or a similar shader, it might be worth it to store the
3520 * last used output slot and try that one first. */
3521 stride_used
= vshader_get_input(state
->shader
[WINED3D_SHADER_TYPE_VERTEX
],
3522 element
->usage
, element
->usage_idx
, &idx
);
3526 idx
= element
->output_slot
;
3532 if (!generic_attributes
&& !element
->ffp_valid
)
3534 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3535 debug_d3dformat(element
->format
->id
), debug_d3ddeclusage(element
->usage
));
3536 stride_used
= FALSE
;
3540 stride_used
= fixed_get_input(element
->usage
, element
->usage_idx
, &idx
);
3546 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3547 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3548 use_vshader
? "shader": "fixed function", idx
,
3549 debug_d3ddeclusage(element
->usage
), element
->usage_idx
, element
->input_slot
,
3550 element
->offset
, stream
->stride
, debug_d3dformat(element
->format
->id
),
3551 debug_d3dinput_classification(element
->input_slot_class
), element
->instance_data_step_rate
);
3553 stream_info
->elements
[idx
].format
= element
->format
;
3554 stream_info
->elements
[idx
].data
.buffer_object
= 0;
3555 stream_info
->elements
[idx
].data
.addr
= (BYTE
*)NULL
+ stream
->offset
+ element
->offset
;
3556 stream_info
->elements
[idx
].stride
= stream
->stride
;
3557 stream_info
->elements
[idx
].stream_idx
= element
->input_slot
;
3558 if (stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
3560 stream_info
->elements
[idx
].divisor
= 1;
3562 else if (element
->input_slot_class
== WINED3D_INPUT_PER_INSTANCE_DATA
)
3564 stream_info
->elements
[idx
].divisor
= element
->instance_data_step_rate
;
3565 if (!element
->instance_data_step_rate
)
3566 FIXME("Instance step rate 0 not implemented.\n");
3570 stream_info
->elements
[idx
].divisor
= 0;
3573 if (!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
]
3574 && element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
3576 stream_info
->swizzle_map
|= 1u << idx
;
3578 stream_info
->use_map
|= 1u << idx
;
3583 /* Context activation is done by the caller. */
3584 static void context_update_stream_info(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3586 struct wined3d_stream_info
*stream_info
= &context
->stream_info
;
3587 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
3588 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3589 DWORD prev_all_vbo
= stream_info
->all_vbo
;
3593 wined3d_stream_info_from_declaration(stream_info
, state
, gl_info
, d3d_info
);
3595 stream_info
->all_vbo
= 1;
3596 context
->buffer_fence_count
= 0;
3597 for (i
= 0, map
= stream_info
->use_map
; map
; map
>>= 1, ++i
)
3599 struct wined3d_stream_info_element
*element
;
3600 struct wined3d_bo_address data
;
3601 struct wined3d_buffer
*buffer
;
3606 element
= &stream_info
->elements
[i
];
3607 buffer
= state
->streams
[element
->stream_idx
].buffer
;
3609 /* We can't use VBOs if the base vertex index is negative. OpenGL
3610 * doesn't accept negative offsets (or rather offsets bigger than the
3611 * VBO, because the pointer is unsigned), so use system memory
3612 * sources. In most sane cases the pointer - offset will still be > 0,
3613 * otherwise it will wrap around to some big value. Hope that with the
3614 * indices the driver wraps it back internally. If not,
3615 * draw_primitive_immediate_mode() is needed, including a vertex buffer
3617 if (state
->load_base_vertex_index
< 0)
3619 WARN_(d3d_perf
)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3620 state
->load_base_vertex_index
);
3621 element
->data
.buffer_object
= 0;
3622 element
->data
.addr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(buffer
, context
);
3623 if ((UINT_PTR
)element
->data
.addr
< -state
->load_base_vertex_index
* element
->stride
)
3624 FIXME("System memory vertex data load offset is negative!\n");
3628 wined3d_buffer_load(buffer
, context
, state
);
3629 wined3d_buffer_get_memory(buffer
, &data
, buffer
->locations
);
3630 element
->data
.buffer_object
= data
.buffer_object
;
3631 element
->data
.addr
+= (ULONG_PTR
)data
.addr
;
3634 if (!element
->data
.buffer_object
)
3635 stream_info
->all_vbo
= 0;
3638 context
->buffer_fences
[context
->buffer_fence_count
++] = buffer
->fence
;
3640 TRACE("Load array %u {%#x:%p}.\n", i
, element
->data
.buffer_object
, element
->data
.addr
);
3643 if (prev_all_vbo
!= stream_info
->all_vbo
)
3644 context_invalidate_state(context
, STATE_INDEXBUFFER
);
3646 context
->use_immediate_mode_draw
= FALSE
;
3648 if (stream_info
->all_vbo
)
3653 if (state
->vertex_declaration
->half_float_conv_needed
)
3655 TRACE("Using immediate mode draw with vertex shaders for FLOAT16 conversion.\n");
3656 context
->use_immediate_mode_draw
= TRUE
;
3661 WORD slow_mask
= -!d3d_info
->ffp_generic_attributes
& (1u << WINED3D_FFP_PSIZE
);
3662 slow_mask
|= -(!gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
] && !d3d_info
->ffp_generic_attributes
)
3663 & ((1u << WINED3D_FFP_DIFFUSE
) | (1u << WINED3D_FFP_SPECULAR
) | (1u << WINED3D_FFP_BLENDWEIGHT
));
3665 if ((stream_info
->position_transformed
&& !d3d_info
->xyzrhw
)
3666 || (stream_info
->use_map
& slow_mask
))
3667 context
->use_immediate_mode_draw
= TRUE
;
3671 /* Context activation is done by the caller. */
3672 static void context_preload_texture(struct wined3d_context
*context
,
3673 const struct wined3d_state
*state
, unsigned int idx
)
3675 struct wined3d_texture
*texture
;
3677 if (!(texture
= state
->textures
[idx
]))
3680 wined3d_texture_load(texture
, context
, state
->sampler_states
[idx
][WINED3D_SAMP_SRGB_TEXTURE
]);
3683 /* Context activation is done by the caller. */
3684 static void context_preload_textures(struct wined3d_context
*context
, const struct wined3d_state
*state
)
3690 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
)
3692 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.resource_info
[i
].type
)
3693 context_preload_texture(context
, state
, MAX_FRAGMENT_SAMPLERS
+ i
);
3699 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
)
3701 if (state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.resource_info
[i
].type
)
3702 context_preload_texture(context
, state
, i
);
3707 WORD ffu_map
= context
->fixed_function_usage_map
;
3709 for (i
= 0; ffu_map
; ffu_map
>>= 1, ++i
)
3712 context_preload_texture(context
, state
, i
);
3717 static void context_load_shader_resources(struct wined3d_context
*context
, const struct wined3d_state
*state
,
3718 unsigned int shader_mask
)
3720 struct wined3d_shader_sampler_map_entry
*entry
;
3721 struct wined3d_shader_resource_view
*view
;
3722 struct wined3d_shader
*shader
;
3725 for (i
= 0; i
< WINED3D_SHADER_TYPE_COUNT
; ++i
)
3727 if (!(shader_mask
& (1u << i
)))
3730 if (!(shader
= state
->shader
[i
]))
3733 for (j
= 0; j
< WINED3D_MAX_CBS
; ++j
)
3735 if (state
->cb
[i
][j
])
3736 wined3d_buffer_load(state
->cb
[i
][j
], context
, state
);
3739 for (j
= 0; j
< shader
->reg_maps
.sampler_map
.count
; ++j
)
3741 entry
= &shader
->reg_maps
.sampler_map
.entries
[j
];
3743 if (!(view
= state
->shader_resource_view
[i
][entry
->resource_idx
]))
3746 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3747 wined3d_buffer_load(buffer_from_resource(view
->resource
), context
, state
);
3749 wined3d_texture_load(texture_from_resource(view
->resource
), context
, FALSE
);
3754 static void context_bind_shader_resources(struct wined3d_context
*context
,
3755 const struct wined3d_state
*state
, enum wined3d_shader_type shader_type
)
3757 unsigned int bind_idx
, shader_sampler_count
, base
, count
, i
;
3758 const struct wined3d_device
*device
= context
->device
;
3759 struct wined3d_shader_sampler_map_entry
*entry
;
3760 struct wined3d_shader_resource_view
*view
;
3761 const struct wined3d_shader
*shader
;
3762 struct wined3d_sampler
*sampler
;
3763 const DWORD
*tex_unit_map
;
3765 if (!(shader
= state
->shader
[shader_type
]))
3768 tex_unit_map
= context_get_tex_unit_mapping(context
,
3769 &shader
->reg_maps
.shader_version
, &base
, &count
);
3771 shader_sampler_count
= shader
->reg_maps
.sampler_map
.count
;
3772 if (shader_sampler_count
> count
)
3773 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3774 shader
, shader_sampler_count
, count
);
3775 count
= min(shader_sampler_count
, count
);
3777 for (i
= 0; i
< count
; ++i
)
3779 entry
= &shader
->reg_maps
.sampler_map
.entries
[i
];
3780 bind_idx
= base
+ entry
->bind_idx
;
3782 bind_idx
= tex_unit_map
[bind_idx
];
3784 if (!(view
= state
->shader_resource_view
[shader_type
][entry
->resource_idx
]))
3786 WARN("No resource view bound at index %u, %u.\n", shader_type
, entry
->resource_idx
);
3790 if (entry
->sampler_idx
== WINED3D_SAMPLER_DEFAULT
)
3791 sampler
= device
->default_sampler
;
3792 else if (!(sampler
= state
->sampler
[shader_type
][entry
->sampler_idx
]))
3793 sampler
= device
->null_sampler
;
3794 wined3d_shader_resource_view_bind(view
, bind_idx
, sampler
, context
);
3798 static void context_load_unordered_access_resources(struct wined3d_context
*context
,
3799 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
3801 struct wined3d_unordered_access_view
*view
;
3802 struct wined3d_texture
*texture
;
3803 struct wined3d_buffer
*buffer
;
3806 context
->uses_uavs
= 0;
3811 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
3813 if (!(view
= views
[i
]))
3816 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
3818 buffer
= buffer_from_resource(view
->resource
);
3819 wined3d_buffer_load_location(buffer
, context
, WINED3D_LOCATION_BUFFER
);
3820 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_BUFFER
);
3824 texture
= texture_from_resource(view
->resource
);
3825 wined3d_texture_load(texture
, context
, FALSE
);
3826 wined3d_unordered_access_view_invalidate_location(view
, ~WINED3D_LOCATION_TEXTURE_RGB
);
3829 context
->uses_uavs
= 1;
3833 static void context_bind_unordered_access_views(struct wined3d_context
*context
,
3834 const struct wined3d_shader
*shader
, struct wined3d_unordered_access_view
* const *views
)
3836 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3837 struct wined3d_unordered_access_view
*view
;
3838 GLuint texture_name
;
3845 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
3847 if (!(view
= views
[i
]))
3849 if (shader
->reg_maps
.uav_resource_info
[i
].type
)
3850 WARN("No unordered access view bound at index %u.\n", i
);
3851 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
3855 if (view
->gl_view
.name
)
3857 texture_name
= view
->gl_view
.name
;
3860 else if (view
->resource
->type
!= WINED3D_RTYPE_BUFFER
)
3862 struct wined3d_texture
*texture
= texture_from_resource(view
->resource
);
3863 texture_name
= wined3d_texture_get_texture_name(texture
, context
, FALSE
);
3864 level
= view
->desc
.u
.texture
.level_idx
;
3868 FIXME("Unsupported buffer unordered access view.\n");
3869 GL_EXTCALL(glBindImageTexture(i
, 0, 0, GL_FALSE
, 0, GL_READ_WRITE
, GL_R8
));
3873 GL_EXTCALL(glBindImageTexture(i
, texture_name
, level
, GL_TRUE
, 0, GL_READ_WRITE
,
3874 view
->format
->glInternal
));
3876 if (view
->counter_bo
)
3877 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER
, i
, view
->counter_bo
));
3879 checkGLcall("Bind unordered access views");
3882 static void context_load_stream_output_buffers(struct wined3d_context
*context
,
3883 const struct wined3d_state
*state
)
3887 for (i
= 0; i
< ARRAY_SIZE(state
->stream_output
); ++i
)
3889 struct wined3d_buffer
*buffer
;
3890 if (!(buffer
= state
->stream_output
[i
].buffer
))
3893 wined3d_buffer_load(buffer
, context
, state
);
3894 wined3d_buffer_invalidate_location(buffer
, ~WINED3D_LOCATION_BUFFER
);
3898 /* Context activation is done by the caller. */
3899 static BOOL
context_apply_draw_state(struct wined3d_context
*context
,
3900 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
3902 const struct StateEntry
*state_table
= context
->state_table
;
3903 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3904 const struct wined3d_fb_state
*fb
= state
->fb
;
3908 if (!have_framebuffer_attachment(gl_info
->limits
.buffers
, fb
->render_targets
, fb
->depth_stencil
))
3910 if (!gl_info
->supported
[ARB_FRAMEBUFFER_NO_ATTACHMENTS
])
3912 FIXME("OpenGL implementation does not support framebuffers with no attachments.\n");
3916 context_set_render_offscreen(context
, TRUE
);
3919 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
&& isStateDirty(context
, STATE_FRAMEBUFFER
))
3921 context_validate_onscreen_formats(context
, fb
->depth_stencil
);
3924 /* Preload resources before FBO setup. Texture preload in particular may
3925 * result in changes to the current FBO, due to using e.g. FBO blits for
3926 * updating a resource location. */
3927 context_update_tex_unit_map(context
, state
);
3928 context_preload_textures(context
, state
);
3929 context_load_shader_resources(context
, state
, ~(1u << WINED3D_SHADER_TYPE_COMPUTE
));
3930 context_load_unordered_access_resources(context
, state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
3931 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
3932 context_load_stream_output_buffers(context
, state
);
3933 /* TODO: Right now the dependency on the vertex shader is necessary
3934 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3935 * the current VS but maybe it's possible to relax the coupling in some
3936 * situations at least. */
3937 if (isStateDirty(context
, STATE_VDECL
) || isStateDirty(context
, STATE_STREAMSRC
)
3938 || isStateDirty(context
, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
)))
3940 context_update_stream_info(context
, state
);
3944 for (i
= 0, map
= context
->stream_info
.use_map
; map
; map
>>= 1, ++i
)
3947 wined3d_buffer_load(state
->streams
[context
->stream_info
.elements
[i
].stream_idx
].buffer
,
3950 /* Loading the buffers above may have invalidated the stream info. */
3951 if (isStateDirty(context
, STATE_STREAMSRC
))
3952 context_update_stream_info(context
, state
);
3954 if (state
->index_buffer
)
3956 if (context
->stream_info
.all_vbo
)
3957 wined3d_buffer_load(state
->index_buffer
, context
, state
);
3959 wined3d_buffer_load_sysmem(state
->index_buffer
, context
);
3962 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
3964 DWORD rep
= context
->dirtyArray
[i
];
3965 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
3966 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
3967 context
->isStateDirty
[idx
] &= ~(1u << shift
);
3968 state_table
[rep
].apply(context
, state
, rep
);
3971 if (context
->shader_update_mask
& ~(1u << WINED3D_SHADER_TYPE_COMPUTE
))
3973 device
->shader_backend
->shader_select(device
->shader_priv
, context
, state
);
3974 context
->shader_update_mask
&= 1u << WINED3D_SHADER_TYPE_COMPUTE
;
3977 if (context
->constant_update_mask
)
3979 device
->shader_backend
->shader_load_constants(device
->shader_priv
, context
, state
);
3980 context
->constant_update_mask
= 0;
3983 if (context
->update_shader_resource_bindings
)
3985 for (i
= 0; i
< WINED3D_SHADER_TYPE_GRAPHICS_COUNT
; ++i
)
3986 context_bind_shader_resources(context
, state
, i
);
3987 context
->update_shader_resource_bindings
= 0;
3988 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
3989 context
->update_compute_shader_resource_bindings
= 1;
3992 if (context
->update_unordered_access_view_bindings
)
3994 context_bind_unordered_access_views(context
,
3995 state
->shader
[WINED3D_SHADER_TYPE_PIXEL
],
3996 state
->unordered_access_view
[WINED3D_PIPELINE_GRAPHICS
]);
3997 context
->update_unordered_access_view_bindings
= 0;
3998 context
->update_compute_unordered_access_view_bindings
= 1;
4001 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
4003 context_check_fbo_status(context
, GL_FRAMEBUFFER
);
4006 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
4007 context
->last_was_blit
= FALSE
;
4008 context
->last_was_ffp_blit
= FALSE
;
4013 static void context_apply_compute_state(struct wined3d_context
*context
,
4014 const struct wined3d_device
*device
, const struct wined3d_state
*state
)
4016 const struct StateEntry
*state_table
= context
->state_table
;
4017 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4018 unsigned int state_id
, i
;
4020 context_load_shader_resources(context
, state
, 1u << WINED3D_SHADER_TYPE_COMPUTE
);
4021 context_load_unordered_access_resources(context
, state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
4022 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
4024 for (i
= 0, state_id
= STATE_COMPUTE_OFFSET
; i
< ARRAY_SIZE(context
->dirty_compute_states
); ++i
)
4026 unsigned int dirty_mask
= context
->dirty_compute_states
[i
];
4029 unsigned int current_state_id
= state_id
+ wined3d_bit_scan(&dirty_mask
);
4030 state_table
[current_state_id
].apply(context
, state
, current_state_id
);
4032 state_id
+= sizeof(*context
->dirty_compute_states
) * CHAR_BIT
;
4034 memset(context
->dirty_compute_states
, 0, sizeof(*context
->dirty_compute_states
));
4036 if (context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_COMPUTE
))
4038 device
->shader_backend
->shader_select_compute(device
->shader_priv
, context
, state
);
4039 context
->shader_update_mask
&= ~(1u << WINED3D_SHADER_TYPE_COMPUTE
);
4042 if (context
->update_compute_shader_resource_bindings
)
4044 context_bind_shader_resources(context
, state
, WINED3D_SHADER_TYPE_COMPUTE
);
4045 context
->update_compute_shader_resource_bindings
= 0;
4046 if (gl_info
->limits
.combined_samplers
== gl_info
->limits
.graphics_samplers
)
4047 context
->update_shader_resource_bindings
= 1;
4050 if (context
->update_compute_unordered_access_view_bindings
)
4052 context_bind_unordered_access_views(context
,
4053 state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
],
4054 state
->unordered_access_view
[WINED3D_PIPELINE_COMPUTE
]);
4055 context
->update_compute_unordered_access_view_bindings
= 0;
4056 context
->update_unordered_access_view_bindings
= 1;
4059 /* Updates to currently bound render targets aren't necessarily coherent
4060 * between the graphics and compute pipelines. Unbind any currently bound
4061 * FBO here to ensure preceding updates to its attachments by the graphics
4062 * pipeline are visible to the compute pipeline.
4064 * Without this, the bloom effect in Nier:Automata is too bright on the
4065 * Mesa radeonsi driver, and presumably on other Mesa based drivers. */
4066 context_bind_fbo(context
, GL_FRAMEBUFFER
, 0);
4067 context_invalidate_state(context
, STATE_FRAMEBUFFER
);
4069 context
->last_was_blit
= FALSE
;
4070 context
->last_was_ffp_blit
= FALSE
;
4073 static BOOL
use_transform_feedback(const struct wined3d_state
*state
)
4075 const struct wined3d_shader
*shader
;
4076 if (!(shader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]))
4078 return shader
->u
.gs
.so_desc
.element_count
;
4081 void context_end_transform_feedback(struct wined3d_context
*context
)
4083 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4084 if (context
->transform_feedback_active
)
4086 GL_EXTCALL(glEndTransformFeedback());
4087 checkGLcall("glEndTransformFeedback");
4088 context
->transform_feedback_active
= 0;
4089 context
->transform_feedback_paused
= 0;
4093 static void context_pause_transform_feedback(struct wined3d_context
*context
, BOOL force
)
4095 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4097 if (!context
->transform_feedback_active
|| context
->transform_feedback_paused
)
4100 if (gl_info
->supported
[ARB_TRANSFORM_FEEDBACK2
])
4102 GL_EXTCALL(glPauseTransformFeedback());
4103 checkGLcall("glPauseTransformFeedback");
4104 context
->transform_feedback_paused
= 1;
4108 WARN("Cannot pause transform feedback operations.\n");
4111 context_end_transform_feedback(context
);
4114 static void context_setup_target(struct wined3d_context
*context
,
4115 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4117 BOOL old_render_offscreen
= context
->render_offscreen
, render_offscreen
;
4119 render_offscreen
= wined3d_resource_is_offscreen(&texture
->resource
);
4120 if (context
->current_rt
.texture
== texture
4121 && context
->current_rt
.sub_resource_idx
== sub_resource_idx
4122 && render_offscreen
== old_render_offscreen
)
4125 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
4126 * the alpha blend state changes with different render target formats. */
4127 if (!context
->current_rt
.texture
)
4129 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
4133 const struct wined3d_format
*old
= context
->current_rt
.texture
->resource
.format
;
4134 const struct wined3d_format
*new = texture
->resource
.format
;
4136 if (old
->id
!= new->id
)
4138 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
4139 if ((old
->alpha_size
&& !new->alpha_size
) || (!old
->alpha_size
&& new->alpha_size
)
4140 || !(texture
->resource
.format_flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
4141 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE
));
4143 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
4144 if ((context
->current_rt
.texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
)
4145 != (texture
->resource
.format_flags
& WINED3DFMT_FLAG_SRGB_WRITE
))
4146 context_invalidate_state(context
, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
));
4149 /* When switching away from an offscreen render target, and we're not
4150 * using FBOs, we have to read the drawable into the texture. This is
4151 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4152 * There are some things that need care though. PreLoad needs a GL context,
4153 * and FindContext is called before the context is activated. It also
4154 * has to be called with the old rendertarget active, otherwise a
4155 * wrong drawable is read. */
4156 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
4157 && old_render_offscreen
&& (context
->current_rt
.texture
!= texture
4158 || context
->current_rt
.sub_resource_idx
!= sub_resource_idx
))
4160 unsigned int prev_sub_resource_idx
= context
->current_rt
.sub_resource_idx
;
4161 struct wined3d_texture
*prev_texture
= context
->current_rt
.texture
;
4163 /* Read the back buffer of the old drawable into the destination texture. */
4164 if (prev_texture
->texture_srgb
.name
)
4165 wined3d_texture_load(prev_texture
, context
, TRUE
);
4166 wined3d_texture_load(prev_texture
, context
, FALSE
);
4167 wined3d_texture_invalidate_location(prev_texture
, prev_sub_resource_idx
, WINED3D_LOCATION_DRAWABLE
);
4171 context
->current_rt
.texture
= texture
;
4172 context
->current_rt
.sub_resource_idx
= sub_resource_idx
;
4173 context_set_render_offscreen(context
, render_offscreen
);
4176 static void context_activate(struct wined3d_context
*context
,
4177 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4179 context_enter(context
);
4180 context_update_window(context
);
4181 context_setup_target(context
, texture
, sub_resource_idx
);
4182 if (!context
->valid
)
4185 if (context
!= context_get_current())
4187 if (!context_set_current(context
))
4188 ERR("Failed to activate the new context.\n");
4190 else if (context
->needs_set
)
4192 context_set_gl_context(context
);
4196 struct wined3d_context
*context_acquire(const struct wined3d_device
*device
,
4197 struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
4199 struct wined3d_context
*current_context
= context_get_current();
4200 struct wined3d_context
*context
;
4201 BOOL swapchain_texture
;
4203 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device
, texture
, sub_resource_idx
);
4205 wined3d_from_cs(device
->cs
);
4207 if (current_context
&& current_context
->destroyed
)
4208 current_context
= NULL
;
4210 swapchain_texture
= texture
&& texture
->swapchain
;
4214 && current_context
->current_rt
.texture
4215 && current_context
->device
== device
)
4217 texture
= current_context
->current_rt
.texture
;
4218 sub_resource_idx
= current_context
->current_rt
.sub_resource_idx
;
4222 struct wined3d_swapchain
*swapchain
= device
->swapchains
[0];
4224 if (swapchain
->back_buffers
)
4225 texture
= swapchain
->back_buffers
[0];
4227 texture
= swapchain
->front_buffer
;
4228 sub_resource_idx
= 0;
4232 if (current_context
&& current_context
->current_rt
.texture
== texture
)
4234 context
= current_context
;
4236 else if (swapchain_texture
)
4238 TRACE("Rendering onscreen.\n");
4240 context
= swapchain_get_context(texture
->swapchain
);
4244 TRACE("Rendering offscreen.\n");
4246 /* Stay with the current context if possible. Otherwise use the
4247 * context for the primary swapchain. */
4248 if (current_context
&& current_context
->device
== device
)
4249 context
= current_context
;
4251 context
= swapchain_get_context(device
->swapchains
[0]);
4254 context_activate(context
, texture
, sub_resource_idx
);
4259 struct wined3d_context
*context_reacquire(const struct wined3d_device
*device
,
4260 struct wined3d_context
*context
)
4262 struct wined3d_context
*acquired_context
;
4264 wined3d_from_cs(device
->cs
);
4266 if (!context
|| context
->tid
!= GetCurrentThreadId())
4269 if (context
->current_rt
.texture
)
4271 context_activate(context
, context
->current_rt
.texture
, context
->current_rt
.sub_resource_idx
);
4275 acquired_context
= context_acquire(device
, NULL
, 0);
4276 if (acquired_context
!= context
)
4277 ERR("Acquired context %p instead of %p.\n", acquired_context
, context
);
4278 return acquired_context
;
4281 void dispatch_compute(struct wined3d_device
*device
, const struct wined3d_state
*state
,
4282 const struct wined3d_dispatch_parameters
*parameters
)
4284 const struct wined3d_gl_info
*gl_info
;
4285 struct wined3d_context
*context
;
4287 context
= context_acquire(device
, NULL
, 0);
4288 if (!context
->valid
)
4290 context_release(context
);
4291 WARN("Invalid context, skipping dispatch.\n");
4294 gl_info
= context
->gl_info
;
4296 if (!gl_info
->supported
[ARB_COMPUTE_SHADER
])
4298 context_release(context
);
4299 FIXME("OpenGL implementation does not support compute shaders.\n");
4303 if (parameters
->indirect
)
4304 wined3d_buffer_load(parameters
->u
.indirect
.buffer
, context
, state
);
4306 context_apply_compute_state(context
, device
, state
);
4308 if (!state
->shader
[WINED3D_SHADER_TYPE_COMPUTE
])
4310 context_release(context
);
4311 WARN("No compute shader bound, skipping dispatch.\n");
4315 if (parameters
->indirect
)
4317 const struct wined3d_indirect_dispatch_parameters
*indirect
= ¶meters
->u
.indirect
;
4318 struct wined3d_buffer
*buffer
= indirect
->buffer
;
4320 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, buffer
->buffer_object
));
4321 GL_EXTCALL(glDispatchComputeIndirect((GLintptr
)indirect
->offset
));
4322 GL_EXTCALL(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER
, 0));
4326 const struct wined3d_direct_dispatch_parameters
*direct
= ¶meters
->u
.direct
;
4327 GL_EXTCALL(glDispatchCompute(direct
->group_count_x
, direct
->group_count_y
, direct
->group_count_z
));
4329 checkGLcall("dispatch compute");
4331 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS
));
4332 checkGLcall("glMemoryBarrier");
4334 context_release(context
);
4337 /* Context activation is done by the caller. */
4338 static void draw_primitive_arrays(struct wined3d_context
*context
, const struct wined3d_state
*state
,
4339 const void *idx_data
, unsigned int idx_size
, int base_vertex_idx
, unsigned int start_idx
,
4340 unsigned int count
, unsigned int start_instance
, unsigned int instance_count
)
4342 const struct wined3d_ffp_attrib_ops
*ops
= &context
->d3d_info
->ffp_attrib_ops
;
4343 GLenum idx_type
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
4344 const struct wined3d_stream_info
*si
= &context
->stream_info
;
4345 unsigned int instanced_elements
[ARRAY_SIZE(si
->elements
)];
4346 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4347 unsigned int instanced_element_count
= 0;
4348 GLenum mode
= state
->gl_primitive_type
;
4349 const void *indices
;
4352 indices
= (const char *)idx_data
+ idx_size
* start_idx
;
4354 if (!instance_count
)
4358 gl_info
->gl_ops
.gl
.p_glDrawArrays(mode
, start_idx
, count
);
4359 checkGLcall("glDrawArrays");
4363 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4365 GL_EXTCALL(glDrawElementsBaseVertex(mode
, count
, idx_type
, indices
, base_vertex_idx
));
4366 checkGLcall("glDrawElementsBaseVertex");
4370 gl_info
->gl_ops
.gl
.p_glDrawElements(mode
, count
, idx_type
, indices
);
4371 checkGLcall("glDrawElements");
4375 if (start_instance
&& !(gl_info
->supported
[ARB_BASE_INSTANCE
] && gl_info
->supported
[ARB_INSTANCED_ARRAYS
]))
4376 FIXME("Start instance (%u) not supported.\n", start_instance
);
4378 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
4382 if (gl_info
->supported
[ARB_BASE_INSTANCE
])
4384 GL_EXTCALL(glDrawArraysInstancedBaseInstance(mode
, start_idx
, count
, instance_count
, start_instance
));
4385 checkGLcall("glDrawArraysInstancedBaseInstance");
4389 GL_EXTCALL(glDrawArraysInstanced(mode
, start_idx
, count
, instance_count
));
4390 checkGLcall("glDrawArraysInstanced");
4394 if (gl_info
->supported
[ARB_BASE_INSTANCE
])
4396 GL_EXTCALL(glDrawElementsInstancedBaseVertexBaseInstance(mode
, count
, idx_type
,
4397 indices
, instance_count
, base_vertex_idx
, start_instance
));
4398 checkGLcall("glDrawElementsInstancedBaseVertexBaseInstance");
4401 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4403 GL_EXTCALL(glDrawElementsInstancedBaseVertex(mode
, count
, idx_type
,
4404 indices
, instance_count
, base_vertex_idx
));
4405 checkGLcall("glDrawElementsInstancedBaseVertex");
4409 GL_EXTCALL(glDrawElementsInstanced(mode
, count
, idx_type
, indices
, instance_count
));
4410 checkGLcall("glDrawElementsInstanced");
4414 /* Instancing emulation by mixing immediate mode and arrays. */
4416 /* This is a nasty thing. MSDN says no hardware supports this and
4417 * applications have to use software vertex processing. We don't support
4420 * Shouldn't be too hard to support with OpenGL, in theory just call
4421 * glDrawArrays() instead of drawElements(). But the stream fequency value
4422 * has a different meaning in that situation. */
4425 FIXME("Non-indexed instanced drawing is not supported.\n");
4429 for (i
= 0; i
< ARRAY_SIZE(si
->elements
); ++i
)
4431 if (!(si
->use_map
& (1u << i
)))
4434 if (state
->streams
[si
->elements
[i
].stream_idx
].flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
)
4435 instanced_elements
[instanced_element_count
++] = i
;
4438 for (i
= 0; i
< instance_count
; ++i
)
4440 /* Specify the instanced attributes using immediate mode calls. */
4441 for (j
= 0; j
< instanced_element_count
; ++j
)
4443 const struct wined3d_stream_info_element
*element
;
4444 unsigned int element_idx
;
4447 element_idx
= instanced_elements
[j
];
4448 element
= &si
->elements
[element_idx
];
4449 ptr
= element
->data
.addr
+ element
->stride
* i
;
4450 if (element
->data
.buffer_object
)
4451 ptr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(state
->streams
[element
->stream_idx
].buffer
, context
);
4452 ops
->generic
[element
->format
->emit_idx
](element_idx
, ptr
);
4455 if (gl_info
->supported
[ARB_DRAW_ELEMENTS_BASE_VERTEX
])
4457 GL_EXTCALL(glDrawElementsBaseVertex(mode
, count
, idx_type
, indices
, base_vertex_idx
));
4458 checkGLcall("glDrawElementsBaseVertex");
4462 gl_info
->gl_ops
.gl
.p_glDrawElements(mode
, count
, idx_type
, indices
);
4463 checkGLcall("glDrawElements");
4468 static unsigned int get_stride_idx(const void *idx_data
, unsigned int idx_size
,
4469 unsigned int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_idx
)
4472 return start_idx
+ vertex_idx
;
4474 return ((const WORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
4475 return ((const DWORD
*)idx_data
)[start_idx
+ vertex_idx
] + base_vertex_idx
;
4478 /* Context activation is done by the caller. */
4479 static void draw_primitive_immediate_mode(struct wined3d_context
*context
, const struct wined3d_state
*state
,
4480 const struct wined3d_stream_info
*si
, const void *idx_data
, unsigned int idx_size
,
4481 int base_vertex_idx
, unsigned int start_idx
, unsigned int vertex_count
, unsigned int instance_count
)
4483 const BYTE
*position
= NULL
, *normal
= NULL
, *diffuse
= NULL
, *specular
= NULL
;
4484 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
4485 unsigned int coord_idx
, stride_idx
, texture_idx
, vertex_idx
;
4486 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4487 const struct wined3d_stream_info_element
*element
;
4488 const BYTE
*tex_coords
[WINED3DDP_MAXTEXCOORD
];
4489 unsigned int texture_unit
, texture_stages
;
4490 const struct wined3d_ffp_attrib_ops
*ops
;
4491 unsigned int untracked_material_count
;
4492 unsigned int tex_mask
= 0;
4493 BOOL specular_fog
= FALSE
;
4494 BOOL ps
= use_ps(state
);
4497 static unsigned int once
;
4500 FIXME_(d3d_perf
)("Drawing using immediate mode.\n");
4502 WARN_(d3d_perf
)("Drawing using immediate mode.\n");
4504 if (!idx_size
&& idx_data
)
4505 ERR("Non-NULL idx_data with 0 idx_size, this should never happen.\n");
4508 FIXME("Instancing not implemented.\n");
4510 /* Immediate mode drawing can't make use of indices in a VBO - get the
4511 * data from the index buffer. */
4513 idx_data
= wined3d_buffer_load_sysmem(state
->index_buffer
, context
) + state
->index_offset
;
4515 ops
= &d3d_info
->ffp_attrib_ops
;
4517 gl_info
->gl_ops
.gl
.p_glBegin(state
->gl_primitive_type
);
4519 if (use_vs(state
) || d3d_info
->ffp_generic_attributes
)
4521 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
4523 unsigned int use_map
= si
->use_map
;
4524 unsigned int element_idx
;
4526 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
4527 for (element_idx
= MAX_ATTRIBS
- 1; use_map
; use_map
&= ~(1u << element_idx
), --element_idx
)
4529 if (!(use_map
& 1u << element_idx
))
4532 ptr
= si
->elements
[element_idx
].data
.addr
+ si
->elements
[element_idx
].stride
* stride_idx
;
4533 ops
->generic
[si
->elements
[element_idx
].format
->emit_idx
](element_idx
, ptr
);
4537 gl_info
->gl_ops
.gl
.p_glEnd();
4541 if (si
->use_map
& (1u << WINED3D_FFP_POSITION
))
4542 position
= si
->elements
[WINED3D_FFP_POSITION
].data
.addr
;
4544 if (si
->use_map
& (1u << WINED3D_FFP_NORMAL
))
4545 normal
= si
->elements
[WINED3D_FFP_NORMAL
].data
.addr
;
4547 gl_info
->gl_ops
.gl
.p_glNormal3f(0.0f
, 0.0f
, 0.0f
);
4549 untracked_material_count
= context
->num_untracked_materials
;
4550 if (si
->use_map
& (1u << WINED3D_FFP_DIFFUSE
))
4552 element
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
4553 diffuse
= element
->data
.addr
;
4555 if (untracked_material_count
&& element
->format
->id
!= WINED3DFMT_B8G8R8A8_UNORM
)
4556 FIXME("Implement diffuse color tracking from %s.\n", debug_d3dformat(element
->format
->id
));
4560 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
4563 if (si
->use_map
& (1u << WINED3D_FFP_SPECULAR
))
4565 element
= &si
->elements
[WINED3D_FFP_SPECULAR
];
4566 specular
= element
->data
.addr
;
4568 /* Special case where the fog density is stored in the specular alpha channel. */
4569 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
4570 && (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
4571 || si
->elements
[WINED3D_FFP_POSITION
].format
->id
== WINED3DFMT_R32G32B32A32_FLOAT
)
4572 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
4574 if (gl_info
->supported
[EXT_FOG_COORD
])
4576 if (element
->format
->id
== WINED3DFMT_B8G8R8A8_UNORM
)
4577 specular_fog
= TRUE
;
4579 FIXME("Implement fog coordinates from %s.\n", debug_d3dformat(element
->format
->id
));
4583 static unsigned int once
;
4586 FIXME("Implement fog for transformed vertices in software.\n");
4590 else if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
4592 GL_EXTCALL(glSecondaryColor3fEXT
)(0.0f
, 0.0f
, 0.0f
);
4595 texture_stages
= d3d_info
->limits
.ffp_blend_stages
;
4596 for (texture_idx
= 0; texture_idx
< texture_stages
; ++texture_idx
)
4598 if (!gl_info
->supported
[ARB_MULTITEXTURE
] && texture_idx
> 0)
4600 FIXME("Program using multiple concurrent textures which this OpenGL implementation doesn't support.\n");
4604 if (!ps
&& !state
->textures
[texture_idx
])
4607 texture_unit
= context
->tex_unit_map
[texture_idx
];
4608 if (texture_unit
== WINED3D_UNMAPPED_STAGE
)
4611 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
4614 TRACE("Skipping generated coordinates (%#x) for texture %u.\n", coord_idx
, texture_idx
);
4618 if (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
)))
4620 tex_coords
[coord_idx
] = si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].data
.addr
;
4621 tex_mask
|= (1u << texture_idx
);
4625 TRACE("Setting default coordinates for texture %u.\n", texture_idx
);
4626 if (gl_info
->supported
[ARB_MULTITEXTURE
])
4627 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_unit
, 0.0f
, 0.0f
, 0.0f
, 1.0f
));
4629 gl_info
->gl_ops
.gl
.p_glTexCoord4f(0.0f
, 0.0f
, 0.0f
, 1.0f
);
4633 /* Blending data and point sizes are not supported by this function. They
4634 * are not supported by the fixed function pipeline at all. A FIXME for
4635 * them is printed after decoding the vertex declaration. */
4636 for (vertex_idx
= 0; vertex_idx
< vertex_count
; ++vertex_idx
)
4638 unsigned int tmp_tex_mask
;
4640 stride_idx
= get_stride_idx(idx_data
, idx_size
, base_vertex_idx
, start_idx
, vertex_idx
);
4644 ptr
= normal
+ stride_idx
* si
->elements
[WINED3D_FFP_NORMAL
].stride
;
4645 ops
->normal
[si
->elements
[WINED3D_FFP_NORMAL
].format
->emit_idx
](ptr
);
4650 ptr
= diffuse
+ stride_idx
* si
->elements
[WINED3D_FFP_DIFFUSE
].stride
;
4651 ops
->diffuse
[si
->elements
[WINED3D_FFP_DIFFUSE
].format
->emit_idx
](ptr
);
4653 if (untracked_material_count
)
4655 struct wined3d_color color
;
4658 wined3d_color_from_d3dcolor(&color
, *(const DWORD
*)ptr
);
4659 for (i
= 0; i
< untracked_material_count
; ++i
)
4661 gl_info
->gl_ops
.gl
.p_glMaterialfv(GL_FRONT_AND_BACK
, context
->untracked_materials
[i
], &color
.r
);
4668 ptr
= specular
+ stride_idx
* si
->elements
[WINED3D_FFP_SPECULAR
].stride
;
4669 ops
->specular
[si
->elements
[WINED3D_FFP_SPECULAR
].format
->emit_idx
](ptr
);
4672 GL_EXTCALL(glFogCoordfEXT((float)(*(const DWORD
*)ptr
>> 24)));
4675 tmp_tex_mask
= tex_mask
;
4676 for (texture_idx
= 0; tmp_tex_mask
; tmp_tex_mask
>>= 1, ++texture_idx
)
4678 if (!(tmp_tex_mask
& 1))
4681 coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
4682 ptr
= tex_coords
[coord_idx
] + (stride_idx
* si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].stride
);
4683 ops
->texcoord
[si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
].format
->emit_idx
](
4684 GL_TEXTURE0_ARB
+ context
->tex_unit_map
[texture_idx
], ptr
);
4689 ptr
= position
+ stride_idx
* si
->elements
[WINED3D_FFP_POSITION
].stride
;
4690 ops
->position
[si
->elements
[WINED3D_FFP_POSITION
].format
->emit_idx
](ptr
);
4694 gl_info
->gl_ops
.gl
.p_glEnd();
4695 checkGLcall("draw immediate mode");
4698 static void draw_indirect(struct wined3d_context
*context
, const struct wined3d_state
*state
,
4699 const struct wined3d_indirect_draw_parameters
*parameters
, unsigned int idx_size
)
4701 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4702 struct wined3d_buffer
*buffer
= parameters
->buffer
;
4705 if (!gl_info
->supported
[ARB_DRAW_INDIRECT
])
4707 FIXME("OpenGL implementation does not support indirect draws.\n");
4711 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER
, buffer
->buffer_object
));
4713 offset
= (void *)(GLintptr
)parameters
->offset
;
4716 GLenum idx_type
= idx_size
== 2 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_INT
;
4717 if (state
->index_offset
)
4718 FIXME("Ignoring index offset %u.\n", state
->index_offset
);
4719 GL_EXTCALL(glDrawElementsIndirect(state
->gl_primitive_type
, idx_type
, offset
));
4723 GL_EXTCALL(glDrawArraysIndirect(state
->gl_primitive_type
, offset
));
4726 GL_EXTCALL(glBindBuffer(GL_DRAW_INDIRECT_BUFFER
, 0));
4728 checkGLcall("draw indirect");
4731 static void remove_vbos(struct wined3d_context
*context
,
4732 const struct wined3d_state
*state
, struct wined3d_stream_info
*s
)
4736 for (i
= 0; i
< ARRAY_SIZE(s
->elements
); ++i
)
4738 struct wined3d_stream_info_element
*e
;
4740 if (!(s
->use_map
& (1u << i
)))
4743 e
= &s
->elements
[i
];
4744 if (e
->data
.buffer_object
)
4746 struct wined3d_buffer
*vb
= state
->streams
[e
->stream_idx
].buffer
;
4747 e
->data
.buffer_object
= 0;
4748 e
->data
.addr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(vb
, context
);
4753 static GLenum
gl_tfb_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
4755 GLenum gl_primitive_type
= gl_primitive_type_from_d3d(primitive_type
);
4756 switch (gl_primitive_type
)
4762 case GL_LINE_STRIP_ADJACENCY
:
4763 case GL_LINES_ADJACENCY
:
4767 case GL_TRIANGLE_FAN
:
4768 case GL_TRIANGLE_STRIP
:
4769 case GL_TRIANGLE_STRIP_ADJACENCY
:
4770 case GL_TRIANGLES_ADJACENCY
:
4772 return GL_TRIANGLES
;
4775 return gl_primitive_type
;
4779 /* Routine common to the draw primitive and draw indexed primitive routines */
4780 void draw_primitive(struct wined3d_device
*device
, const struct wined3d_state
*state
,
4781 const struct wined3d_draw_parameters
*parameters
)
4783 BOOL emulation
= FALSE
, rasterizer_discard
= FALSE
;
4784 const struct wined3d_fb_state
*fb
= state
->fb
;
4785 const struct wined3d_stream_info
*stream_info
;
4786 struct wined3d_rendertarget_view
*dsv
, *rtv
;
4787 struct wined3d_stream_info si_emulated
;
4788 struct wined3d_fence
*ib_fence
= NULL
;
4789 const struct wined3d_gl_info
*gl_info
;
4790 struct wined3d_context
*context
;
4791 unsigned int i
, idx_size
= 0;
4792 const void *idx_data
= NULL
;
4794 if (!parameters
->indirect
&& !parameters
->u
.direct
.index_count
)
4797 if (!(rtv
= fb
->render_targets
[0]))
4798 rtv
= fb
->depth_stencil
;
4800 context
= context_acquire(device
, wined3d_texture_from_resource(rtv
->resource
), rtv
->sub_resource_idx
);
4802 context
= context_acquire(device
, NULL
, 0);
4803 if (!context
->valid
)
4805 context_release(context
);
4806 WARN("Invalid context, skipping draw.\n");
4809 gl_info
= context
->gl_info
;
4811 if (!use_transform_feedback(state
))
4812 context_pause_transform_feedback(context
, TRUE
);
4814 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
4816 if (!(rtv
= fb
->render_targets
[i
]) || rtv
->format
->id
== WINED3DFMT_NULL
)
4819 if (state
->render_states
[WINED3D_RS_COLORWRITEENABLE
])
4821 wined3d_rendertarget_view_load_location(rtv
, context
, rtv
->resource
->draw_binding
);
4822 wined3d_rendertarget_view_invalidate_location(rtv
, ~rtv
->resource
->draw_binding
);
4826 wined3d_rendertarget_view_prepare_location(rtv
, context
, rtv
->resource
->draw_binding
);
4830 if ((dsv
= fb
->depth_stencil
))
4832 /* Note that this depends on the context_acquire() call above to set
4833 * context->render_offscreen properly. We don't currently take the
4834 * Z-compare function into account, but we could skip loading the
4835 * depthstencil for D3DCMP_NEVER and D3DCMP_ALWAYS as well. Also note
4836 * that we never copy the stencil data.*/
4837 DWORD location
= context
->render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
4839 if (state
->render_states
[WINED3D_RS_ZWRITEENABLE
] || state
->render_states
[WINED3D_RS_ZENABLE
])
4840 wined3d_rendertarget_view_load_location(dsv
, context
, location
);
4842 wined3d_rendertarget_view_prepare_location(dsv
, context
, location
);
4845 if (parameters
->indirect
)
4846 wined3d_buffer_load(parameters
->u
.indirect
.buffer
, context
, state
);
4848 if (!context_apply_draw_state(context
, device
, state
))
4850 context_release(context
);
4851 WARN("Unable to apply draw state, skipping draw.\n");
4855 if (dsv
&& state
->render_states
[WINED3D_RS_ZWRITEENABLE
])
4857 DWORD location
= context
->render_offscreen
? dsv
->resource
->draw_binding
: WINED3D_LOCATION_DRAWABLE
;
4859 wined3d_rendertarget_view_validate_location(dsv
, location
);
4860 wined3d_rendertarget_view_invalidate_location(dsv
, ~location
);
4863 stream_info
= &context
->stream_info
;
4865 if (parameters
->indexed
)
4867 struct wined3d_buffer
*index_buffer
= state
->index_buffer
;
4868 if (!index_buffer
->buffer_object
|| !stream_info
->all_vbo
)
4870 idx_data
= index_buffer
->resource
.heap_memory
;
4874 ib_fence
= index_buffer
->fence
;
4877 idx_data
= (const BYTE
*)idx_data
+ state
->index_offset
;
4879 if (state
->index_format
== WINED3DFMT_R16_UINT
)
4887 if (!stream_info
->position_transformed
&& context
->num_untracked_materials
4888 && state
->render_states
[WINED3D_RS_LIGHTING
])
4893 FIXME("Using software emulation because not all material properties could be tracked.\n");
4895 WARN_(d3d_perf
)("Using software emulation because not all material properties could be tracked.\n");
4898 else if (context
->fog_coord
&& state
->render_states
[WINED3D_RS_FOGENABLE
])
4902 /* Either write a pipeline replacement shader or convert the
4903 * specular alpha from unsigned byte to a float in the vertex
4906 FIXME("Using software emulation because manual fog coordinates are provided.\n");
4908 WARN_(d3d_perf
)("Using software emulation because manual fog coordinates are provided.\n");
4914 si_emulated
= context
->stream_info
;
4915 remove_vbos(context
, state
, &si_emulated
);
4916 stream_info
= &si_emulated
;
4920 if (use_transform_feedback(state
))
4922 const struct wined3d_shader
*shader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
4924 if (is_rasterization_disabled(shader
))
4926 glEnable(GL_RASTERIZER_DISCARD
);
4927 checkGLcall("enable rasterizer discard");
4928 rasterizer_discard
= TRUE
;
4931 if (context
->transform_feedback_paused
)
4933 GL_EXTCALL(glResumeTransformFeedback());
4934 checkGLcall("glResumeTransformFeedback");
4935 context
->transform_feedback_paused
= 0;
4937 else if (!context
->transform_feedback_active
)
4939 GLenum mode
= gl_tfb_primitive_type_from_d3d(shader
->u
.gs
.output_type
);
4940 GL_EXTCALL(glBeginTransformFeedback(mode
));
4941 checkGLcall("glBeginTransformFeedback");
4942 context
->transform_feedback_active
= 1;
4946 if (state
->gl_primitive_type
== GL_PATCHES
)
4948 GL_EXTCALL(glPatchParameteri(GL_PATCH_VERTICES
, state
->gl_patch_vertices
));
4949 checkGLcall("glPatchParameteri");
4952 if (parameters
->indirect
)
4954 if (!context
->use_immediate_mode_draw
&& !emulation
)
4955 draw_indirect(context
, state
, ¶meters
->u
.indirect
, idx_size
);
4957 FIXME("Indirect draws with immediate mode/emulation are not supported.\n");
4961 unsigned int instance_count
= parameters
->u
.direct
.instance_count
;
4962 if (context
->instance_count
)
4963 instance_count
= context
->instance_count
;
4965 if (context
->use_immediate_mode_draw
|| emulation
)
4966 draw_primitive_immediate_mode(context
, state
, stream_info
, idx_data
,
4967 idx_size
, parameters
->u
.direct
.base_vertex_idx
,
4968 parameters
->u
.direct
.start_idx
, parameters
->u
.direct
.index_count
, instance_count
);
4970 draw_primitive_arrays(context
, state
, idx_data
, idx_size
, parameters
->u
.direct
.base_vertex_idx
,
4971 parameters
->u
.direct
.start_idx
, parameters
->u
.direct
.index_count
,
4972 parameters
->u
.direct
.start_instance
, instance_count
);
4975 if (context
->uses_uavs
)
4977 GL_EXTCALL(glMemoryBarrier(GL_ALL_BARRIER_BITS
));
4978 checkGLcall("glMemoryBarrier");
4981 context_pause_transform_feedback(context
, FALSE
);
4983 if (rasterizer_discard
)
4985 glDisable(GL_RASTERIZER_DISCARD
);
4986 checkGLcall("disable rasterizer discard");
4990 wined3d_fence_issue(ib_fence
, device
);
4991 for (i
= 0; i
< context
->buffer_fence_count
; ++i
)
4992 wined3d_fence_issue(context
->buffer_fences
[i
], device
);
4994 context_release(context
);
4997 void context_unload_tex_coords(const struct wined3d_context
*context
)
4999 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5000 unsigned int texture_idx
;
5002 for (texture_idx
= 0; texture_idx
< gl_info
->limits
.texture_coords
; ++texture_idx
)
5004 gl_info
->gl_ops
.ext
.p_glClientActiveTextureARB(GL_TEXTURE0_ARB
+ texture_idx
);
5005 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_TEXTURE_COORD_ARRAY
);
5009 void context_load_tex_coords(const struct wined3d_context
*context
, const struct wined3d_stream_info
*si
,
5010 GLuint
*current_bo
, const struct wined3d_state
*state
)
5012 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5013 unsigned int mapped_stage
= 0;
5014 unsigned int texture_idx
;
5016 for (texture_idx
= 0; texture_idx
< context
->d3d_info
->limits
.ffp_blend_stages
; ++texture_idx
)
5018 unsigned int coord_idx
= state
->texture_states
[texture_idx
][WINED3D_TSS_TEXCOORD_INDEX
];
5020 if ((mapped_stage
= context
->tex_unit_map
[texture_idx
]) == WINED3D_UNMAPPED_STAGE
)
5023 if (mapped_stage
>= gl_info
->limits
.texture_coords
)
5025 FIXME("Attempted to load unsupported texture coordinate %u.\n", mapped_stage
);
5029 if (coord_idx
< MAX_TEXTURES
&& (si
->use_map
& (1u << (WINED3D_FFP_TEXCOORD0
+ coord_idx
))))
5031 const struct wined3d_stream_info_element
*e
= &si
->elements
[WINED3D_FFP_TEXCOORD0
+ coord_idx
];
5033 TRACE("Setting up texture %u, idx %d, coord_idx %u, data {%#x:%p}.\n",
5034 texture_idx
, mapped_stage
, coord_idx
, e
->data
.buffer_object
, e
->data
.addr
);
5036 if (*current_bo
!= e
->data
.buffer_object
)
5038 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, e
->data
.buffer_object
));
5039 checkGLcall("glBindBuffer");
5040 *current_bo
= e
->data
.buffer_object
;
5043 GL_EXTCALL(glClientActiveTextureARB(GL_TEXTURE0_ARB
+ mapped_stage
));
5044 checkGLcall("glClientActiveTextureARB");
5046 /* The coords to supply depend completely on the fvf/vertex shader. */
5047 gl_info
->gl_ops
.gl
.p_glTexCoordPointer(e
->format
->gl_vtx_format
, e
->format
->gl_vtx_type
, e
->stride
,
5048 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5049 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
5053 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ mapped_stage
, 0, 0, 0, 1));
5056 if (gl_info
->supported
[NV_REGISTER_COMBINERS
])
5058 /* The number of the mapped stages increases monotonically, so it's fine to use the last used one. */
5059 for (texture_idx
= mapped_stage
+ 1; texture_idx
< gl_info
->limits
.textures
; ++texture_idx
)
5061 GL_EXTCALL(glMultiTexCoord4fARB(GL_TEXTURE0_ARB
+ texture_idx
, 0, 0, 0, 1));
5065 checkGLcall("loadTexCoords");
5068 /* This should match any arrays loaded in context_load_vertex_data(). */
5069 static void context_unload_vertex_data(struct wined3d_context
*context
)
5071 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5073 if (!context
->namedArraysLoaded
)
5075 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_VERTEX_ARRAY
);
5076 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_NORMAL_ARRAY
);
5077 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_COLOR_ARRAY
);
5078 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5079 gl_info
->gl_ops
.gl
.p_glDisableClientState(GL_SECONDARY_COLOR_ARRAY_EXT
);
5080 context_unload_tex_coords(context
);
5081 context
->namedArraysLoaded
= FALSE
;
5084 static void context_load_vertex_data(struct wined3d_context
*context
,
5085 const struct wined3d_stream_info
*si
, const struct wined3d_state
*state
)
5087 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5088 const struct wined3d_stream_info_element
*e
;
5091 TRACE("context %p, si %p, state %p.\n", context
, si
, state
);
5093 /* This is used for the fixed-function pipeline only, and the
5094 * fixed-function pipeline doesn't do instancing. */
5095 context
->instance_count
= 0;
5096 current_bo
= gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
] ? ~0u : 0;
5099 if ((si
->use_map
& (1u << WINED3D_FFP_BLENDWEIGHT
))
5100 || si
->use_map
& (1u << WINED3D_FFP_BLENDINDICES
))
5102 /* TODO: Support vertex blending in immediate mode draws. No need to
5103 * write a FIXME here, this is done after the general vertex
5104 * declaration decoding. */
5105 WARN("Vertex blending not supported.\n");
5109 if (si
->use_map
& (1u << WINED3D_FFP_PSIZE
))
5111 /* No such functionality in the fixed-function GL pipeline. */
5112 WARN("Per-vertex point size not supported.\n");
5116 if (si
->use_map
& (1u << WINED3D_FFP_POSITION
))
5118 e
= &si
->elements
[WINED3D_FFP_POSITION
];
5120 if (current_bo
!= e
->data
.buffer_object
)
5122 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, e
->data
.buffer_object
));
5123 checkGLcall("glBindBuffer");
5124 current_bo
= e
->data
.buffer_object
;
5127 TRACE("glVertexPointer(%#x, %#x, %#x, %p);\n",
5128 e
->format
->gl_vtx_format
, e
->format
->gl_vtx_type
, e
->stride
,
5129 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5130 gl_info
->gl_ops
.gl
.p_glVertexPointer(e
->format
->gl_vtx_format
, e
->format
->gl_vtx_type
, e
->stride
,
5131 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5132 checkGLcall("glVertexPointer(...)");
5133 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_VERTEX_ARRAY
);
5134 checkGLcall("glEnableClientState(GL_VERTEX_ARRAY)");
5138 if (si
->use_map
& (1u << WINED3D_FFP_NORMAL
))
5140 e
= &si
->elements
[WINED3D_FFP_NORMAL
];
5142 if (current_bo
!= e
->data
.buffer_object
)
5144 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, e
->data
.buffer_object
));
5145 checkGLcall("glBindBuffer");
5146 current_bo
= e
->data
.buffer_object
;
5149 TRACE("glNormalPointer(%#x, %#x, %p);\n", e
->format
->gl_vtx_type
, e
->stride
,
5150 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5151 gl_info
->gl_ops
.gl
.p_glNormalPointer(e
->format
->gl_vtx_type
, e
->stride
,
5152 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5153 checkGLcall("glNormalPointer(...)");
5154 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_NORMAL_ARRAY
);
5155 checkGLcall("glEnableClientState(GL_NORMAL_ARRAY)");
5160 gl_info
->gl_ops
.gl
.p_glNormal3f(0, 0, 0);
5161 checkGLcall("glNormal3f(0, 0, 0)");
5164 /* Diffuse colour */
5165 if (si
->use_map
& (1u << WINED3D_FFP_DIFFUSE
))
5167 e
= &si
->elements
[WINED3D_FFP_DIFFUSE
];
5169 if (current_bo
!= e
->data
.buffer_object
)
5171 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, e
->data
.buffer_object
));
5172 checkGLcall("glBindBuffer");
5173 current_bo
= e
->data
.buffer_object
;
5176 TRACE("glColorPointer(%#x, %#x %#x, %p);\n",
5177 e
->format
->gl_vtx_format
, e
->format
->gl_vtx_type
, e
->stride
,
5178 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5179 gl_info
->gl_ops
.gl
.p_glColorPointer(e
->format
->gl_vtx_format
, e
->format
->gl_vtx_type
, e
->stride
,
5180 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5181 checkGLcall("glColorPointer(4, GL_UNSIGNED_BYTE, ...)");
5182 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_COLOR_ARRAY
);
5183 checkGLcall("glEnableClientState(GL_COLOR_ARRAY)");
5188 gl_info
->gl_ops
.gl
.p_glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
5189 checkGLcall("glColor4f(1, 1, 1, 1)");
5192 /* Specular colour */
5193 if (si
->use_map
& (1u << WINED3D_FFP_SPECULAR
))
5195 TRACE("Setting specular colour.\n");
5197 e
= &si
->elements
[WINED3D_FFP_SPECULAR
];
5199 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5201 GLenum type
= e
->format
->gl_vtx_type
;
5202 GLint format
= e
->format
->gl_vtx_format
;
5204 if (current_bo
!= e
->data
.buffer_object
)
5206 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, e
->data
.buffer_object
));
5207 checkGLcall("glBindBuffer");
5208 current_bo
= e
->data
.buffer_object
;
5211 if (format
!= 4 || (gl_info
->quirks
& WINED3D_QUIRK_ALLOWS_SPECULAR_ALPHA
))
5213 /* Usually specular colors only allow 3 components, since they have no alpha. In D3D, the specular alpha
5214 * contains the fog coordinate, which is passed to GL with GL_EXT_fog_coord. However, the fixed function
5215 * vertex pipeline can pass the specular alpha through, and pixel shaders can read it. So it GL accepts
5216 * 4 component secondary colors use it
5218 TRACE("glSecondaryColorPointer(%#x, %#x, %#x, %p);\n", format
, type
, e
->stride
,
5219 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5220 GL_EXTCALL(glSecondaryColorPointerEXT(format
, type
, e
->stride
,
5221 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
));
5222 checkGLcall("glSecondaryColorPointerEXT(format, type, ...)");
5228 case GL_UNSIGNED_BYTE
:
5229 TRACE("glSecondaryColorPointer(3, GL_UNSIGNED_BYTE, %#x, %p);\n", e
->stride
,
5230 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5231 GL_EXTCALL(glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE
, e
->stride
,
5232 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
));
5233 checkGLcall("glSecondaryColorPointerEXT(3, GL_UNSIGNED_BYTE, ...)");
5237 FIXME("Add 4 component specular colour pointers for type %#x.\n", type
);
5238 /* Make sure that the right colour component is dropped. */
5239 TRACE("glSecondaryColorPointer(3, %#x, %#x, %p);\n", type
, e
->stride
,
5240 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
);
5241 GL_EXTCALL(glSecondaryColorPointerEXT(3, type
, e
->stride
,
5242 e
->data
.addr
+ state
->load_base_vertex_index
* e
->stride
));
5243 checkGLcall("glSecondaryColorPointerEXT(3, type, ...)");
5246 gl_info
->gl_ops
.gl
.p_glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT
);
5247 checkGLcall("glEnableClientState(GL_SECONDARY_COLOR_ARRAY_EXT)");
5251 WARN("Specular colour is not supported in this GL implementation.\n");
5256 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
5258 GL_EXTCALL(glSecondaryColor3fEXT
)(0, 0, 0);
5259 checkGLcall("glSecondaryColor3fEXT(0, 0, 0)");
5263 WARN("Specular colour is not supported in this GL implementation.\n");
5267 /* Texture coordinates */
5268 context_load_tex_coords(context
, si
, ¤t_bo
, state
);
5271 static void context_unload_numbered_array(struct wined3d_context
*context
, unsigned int i
)
5273 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5275 GL_EXTCALL(glDisableVertexAttribArray(i
));
5276 checkGLcall("glDisableVertexAttribArray");
5277 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
5278 GL_EXTCALL(glVertexAttribDivisor(i
, 0));
5280 context
->numbered_array_mask
&= ~(1u << i
);
5283 static void context_unload_numbered_arrays(struct wined3d_context
*context
)
5287 while (context
->numbered_array_mask
)
5289 i
= wined3d_bit_scan(&context
->numbered_array_mask
);
5290 context_unload_numbered_array(context
, i
);
5294 static void context_load_numbered_arrays(struct wined3d_context
*context
,
5295 const struct wined3d_stream_info
*stream_info
, const struct wined3d_state
*state
)
5297 const struct wined3d_shader
*vs
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
5298 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5302 /* Default to no instancing. */
5303 context
->instance_count
= 0;
5304 current_bo
= gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
] ? ~0u : 0;
5306 for (i
= 0; i
< MAX_ATTRIBS
; ++i
)
5308 const struct wined3d_stream_info_element
*element
= &stream_info
->elements
[i
];
5309 const struct wined3d_stream_state
*stream
;
5311 if (!(stream_info
->use_map
& (1u << i
)))
5313 if (context
->numbered_array_mask
& (1u << i
))
5314 context_unload_numbered_array(context
, i
);
5315 if (!use_vs(state
) && i
== WINED3D_FFP_DIFFUSE
)
5316 GL_EXTCALL(glVertexAttrib4f(i
, 1.0f
, 1.0f
, 1.0f
, 1.0f
));
5318 GL_EXTCALL(glVertexAttrib4f(i
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
5322 stream
= &state
->streams
[element
->stream_idx
];
5324 if ((stream
->flags
& WINED3DSTREAMSOURCE_INSTANCEDATA
) && !context
->instance_count
)
5325 context
->instance_count
= state
->streams
[0].frequency
? state
->streams
[0].frequency
: 1;
5327 if (gl_info
->supported
[ARB_INSTANCED_ARRAYS
])
5329 GL_EXTCALL(glVertexAttribDivisor(i
, element
->divisor
));
5331 else if (element
->divisor
)
5333 /* Unload instanced arrays, they will be loaded using immediate
5335 if (context
->numbered_array_mask
& (1u << i
))
5336 context_unload_numbered_array(context
, i
);
5340 TRACE("Loading array %u [VBO=%u].\n", i
, element
->data
.buffer_object
);
5342 if (element
->stride
)
5344 if (current_bo
!= element
->data
.buffer_object
)
5346 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, element
->data
.buffer_object
));
5347 checkGLcall("glBindBuffer");
5348 current_bo
= element
->data
.buffer_object
;
5350 /* Use the VBO to find out if a vertex buffer exists, not the vb
5351 * pointer. vb can point to a user pointer data blob. In that case
5352 * current_bo will be 0. If there is a vertex buffer but no vbo we
5353 * won't be load converted attributes anyway. */
5354 if (vs
&& vs
->reg_maps
.shader_version
.major
>= 4
5355 && (element
->format
->flags
[WINED3D_GL_RES_TYPE_BUFFER
] & WINED3DFMT_FLAG_INTEGER
))
5357 GL_EXTCALL(glVertexAttribIPointer(i
, element
->format
->gl_vtx_format
, element
->format
->gl_vtx_type
,
5358 element
->stride
, element
->data
.addr
+ state
->load_base_vertex_index
* element
->stride
));
5362 GL_EXTCALL(glVertexAttribPointer(i
, element
->format
->gl_vtx_format
, element
->format
->gl_vtx_type
,
5363 element
->format
->gl_normalized
, element
->stride
,
5364 element
->data
.addr
+ state
->load_base_vertex_index
* element
->stride
));
5367 if (!(context
->numbered_array_mask
& (1u << i
)))
5369 GL_EXTCALL(glEnableVertexAttribArray(i
));
5370 context
->numbered_array_mask
|= (1u << i
);
5375 /* Stride = 0 means always the same values.
5376 * glVertexAttribPointer() doesn't do that. Instead disable the
5377 * pointer and set up the attribute statically. But we have to
5378 * figure out the system memory address. */
5379 const BYTE
*ptr
= element
->data
.addr
;
5380 if (element
->data
.buffer_object
)
5381 ptr
+= (ULONG_PTR
)wined3d_buffer_load_sysmem(stream
->buffer
, context
);
5383 if (context
->numbered_array_mask
& (1u << i
))
5384 context_unload_numbered_array(context
, i
);
5386 switch (element
->format
->id
)
5388 case WINED3DFMT_R32_FLOAT
:
5389 GL_EXTCALL(glVertexAttrib1fv(i
, (const GLfloat
*)ptr
));
5391 case WINED3DFMT_R32G32_FLOAT
:
5392 GL_EXTCALL(glVertexAttrib2fv(i
, (const GLfloat
*)ptr
));
5394 case WINED3DFMT_R32G32B32_FLOAT
:
5395 GL_EXTCALL(glVertexAttrib3fv(i
, (const GLfloat
*)ptr
));
5397 case WINED3DFMT_R32G32B32A32_FLOAT
:
5398 GL_EXTCALL(glVertexAttrib4fv(i
, (const GLfloat
*)ptr
));
5400 case WINED3DFMT_R8G8B8A8_UINT
:
5401 GL_EXTCALL(glVertexAttrib4ubv(i
, ptr
));
5403 case WINED3DFMT_B8G8R8A8_UNORM
:
5404 if (gl_info
->supported
[ARB_VERTEX_ARRAY_BGRA
])
5406 const DWORD
*src
= (const DWORD
*)ptr
;
5407 DWORD c
= *src
& 0xff00ff00u
;
5408 c
|= (*src
& 0xff0000u
) >> 16;
5409 c
|= (*src
& 0xffu
) << 16;
5410 GL_EXTCALL(glVertexAttrib4Nubv(i
, (GLubyte
*)&c
));
5413 /* else fallthrough */
5414 case WINED3DFMT_R8G8B8A8_UNORM
:
5415 GL_EXTCALL(glVertexAttrib4Nubv(i
, ptr
));
5417 case WINED3DFMT_R16G16_SINT
:
5418 GL_EXTCALL(glVertexAttrib2sv(i
, (const GLshort
*)ptr
));
5420 case WINED3DFMT_R16G16B16A16_SINT
:
5421 GL_EXTCALL(glVertexAttrib4sv(i
, (const GLshort
*)ptr
));
5423 case WINED3DFMT_R16G16_SNORM
:
5425 const GLshort s
[4] = {((const GLshort
*)ptr
)[0], ((const GLshort
*)ptr
)[1], 0, 1};
5426 GL_EXTCALL(glVertexAttrib4Nsv(i
, s
));
5429 case WINED3DFMT_R16G16_UNORM
:
5431 const GLushort s
[4] = {((const GLushort
*)ptr
)[0], ((const GLushort
*)ptr
)[1], 0, 1};
5432 GL_EXTCALL(glVertexAttrib4Nusv(i
, s
));
5435 case WINED3DFMT_R16G16B16A16_SNORM
:
5436 GL_EXTCALL(glVertexAttrib4Nsv(i
, (const GLshort
*)ptr
));
5438 case WINED3DFMT_R16G16B16A16_UNORM
:
5439 GL_EXTCALL(glVertexAttrib4Nusv(i
, (const GLushort
*)ptr
));
5441 case WINED3DFMT_R10G10B10X2_UINT
:
5442 FIXME("Unsure about WINED3DDECLTYPE_UDEC3.\n");
5443 /*glVertexAttrib3usvARB(i, (const GLushort *)ptr); Does not exist */
5445 case WINED3DFMT_R10G10B10X2_SNORM
:
5446 FIXME("Unsure about WINED3DDECLTYPE_DEC3N.\n");
5447 /*glVertexAttrib3NusvARB(i, (const GLushort *)ptr); Does not exist */
5449 case WINED3DFMT_R16G16_FLOAT
:
5450 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
5452 /* Not supported by GL_ARB_half_float_vertex. */
5453 GL_EXTCALL(glVertexAttrib2hvNV(i
, (const GLhalfNV
*)ptr
));
5457 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
5458 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
5459 GL_EXTCALL(glVertexAttrib2f(i
, x
, y
));
5462 case WINED3DFMT_R16G16B16A16_FLOAT
:
5463 if (gl_info
->supported
[NV_HALF_FLOAT
] && gl_info
->supported
[NV_VERTEX_PROGRAM
])
5465 /* Not supported by GL_ARB_half_float_vertex. */
5466 GL_EXTCALL(glVertexAttrib4hvNV(i
, (const GLhalfNV
*)ptr
));
5470 float x
= float_16_to_32(((const unsigned short *)ptr
) + 0);
5471 float y
= float_16_to_32(((const unsigned short *)ptr
) + 1);
5472 float z
= float_16_to_32(((const unsigned short *)ptr
) + 2);
5473 float w
= float_16_to_32(((const unsigned short *)ptr
) + 3);
5474 GL_EXTCALL(glVertexAttrib4f(i
, x
, y
, z
, w
));
5478 ERR("Unexpected declaration in stride 0 attributes.\n");
5484 checkGLcall("Loading numbered arrays");
5487 void context_update_stream_sources(struct wined3d_context
*context
, const struct wined3d_state
*state
)
5490 if (context
->use_immediate_mode_draw
)
5493 context_unload_vertex_data(context
);
5494 if (context
->d3d_info
->ffp_generic_attributes
|| use_vs(state
))
5496 TRACE("Loading numbered arrays.\n");
5497 context_load_numbered_arrays(context
, &context
->stream_info
, state
);
5501 TRACE("Loading named arrays.\n");
5502 context_unload_numbered_arrays(context
);
5503 context_load_vertex_data(context
, &context
->stream_info
, state
);
5504 context
->namedArraysLoaded
= TRUE
;
5507 static void apply_texture_blit_state(const struct wined3d_gl_info
*gl_info
, struct gl_texture
*texture
,
5508 GLenum target
, unsigned int level
, enum wined3d_texture_filter_type filter
)
5510 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(filter
));
5511 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
5512 wined3d_gl_min_mip_filter(filter
, WINED3D_TEXF_NONE
));
5513 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
5514 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
5515 if (gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
5516 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
);
5517 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, level
);
5519 /* We changed the filtering settings on the texture. Make sure they get
5520 * reset on subsequent draws. */
5521 texture
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
5522 texture
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
5523 texture
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
5524 texture
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
5525 texture
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
5526 texture
->sampler_desc
.srgb_decode
= FALSE
;
5527 texture
->base_level
= level
;
5530 /* Context activation is done by the caller. */
5531 void context_draw_shaded_quad(struct wined3d_context
*context
, struct wined3d_texture
*texture
,
5532 unsigned int sub_resource_idx
, const RECT
*src_rect
, const RECT
*dst_rect
,
5533 enum wined3d_texture_filter_type filter
)
5535 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5536 struct wined3d_blt_info info
;
5537 unsigned int level
, w
, h
, i
;
5542 struct wined3d_vec3 texcoord
;
5546 texture2d_get_blt_info(texture
, sub_resource_idx
, src_rect
, &info
);
5548 level
= sub_resource_idx
% texture
->level_count
;
5549 context_bind_texture(context
, info
.bind_target
, texture
->texture_rgb
.name
);
5550 apply_texture_blit_state(gl_info
, &texture
->texture_rgb
, info
.bind_target
, level
, filter
);
5551 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, level
);
5553 context_get_rt_size(context
, &dst_size
);
5557 quad
[0].x
= dst_rect
->left
* 2.0f
/ w
- 1.0f
;
5558 quad
[0].y
= dst_rect
->top
* 2.0f
/ h
- 1.0f
;
5559 quad
[0].texcoord
= info
.texcoords
[0];
5561 quad
[1].x
= dst_rect
->right
* 2.0f
/ w
- 1.0f
;
5562 quad
[1].y
= dst_rect
->top
* 2.0f
/ h
- 1.0f
;
5563 quad
[1].texcoord
= info
.texcoords
[1];
5565 quad
[2].x
= dst_rect
->left
* 2.0f
/ w
- 1.0f
;
5566 quad
[2].y
= dst_rect
->bottom
* 2.0f
/ h
- 1.0f
;
5567 quad
[2].texcoord
= info
.texcoords
[2];
5569 quad
[3].x
= dst_rect
->right
* 2.0f
/ w
- 1.0f
;
5570 quad
[3].y
= dst_rect
->bottom
* 2.0f
/ h
- 1.0f
;
5571 quad
[3].texcoord
= info
.texcoords
[3];
5574 if (gl_info
->supported
[ARB_VERTEX_BUFFER_OBJECT
])
5576 if (!context
->blit_vbo
)
5577 GL_EXTCALL(glGenBuffers(1, &context
->blit_vbo
));
5578 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, context
->blit_vbo
));
5580 context_unload_vertex_data(context
);
5581 context_unload_numbered_arrays(context
);
5583 GL_EXTCALL(glBufferData(GL_ARRAY_BUFFER
, sizeof(quad
), quad
, GL_STREAM_DRAW
));
5584 GL_EXTCALL(glVertexAttribPointer(0, 2, GL_FLOAT
, FALSE
, sizeof(*quad
), NULL
));
5585 GL_EXTCALL(glVertexAttribPointer(1, 3, GL_FLOAT
, FALSE
, sizeof(*quad
),
5586 (void *)FIELD_OFFSET(struct blit_vertex
, texcoord
)));
5588 GL_EXTCALL(glEnableVertexAttribArray(0));
5589 GL_EXTCALL(glEnableVertexAttribArray(1));
5591 gl_info
->gl_ops
.gl
.p_glDrawArrays(GL_TRIANGLE_STRIP
, 0, 4);
5593 GL_EXTCALL(glBindBuffer(GL_ARRAY_BUFFER
, 0));
5594 GL_EXTCALL(glDisableVertexAttribArray(1));
5595 GL_EXTCALL(glDisableVertexAttribArray(0));
5599 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
5601 for (i
= 0; i
< ARRAY_SIZE(quad
); ++i
)
5603 GL_EXTCALL(glVertexAttrib3fv(1, &quad
[i
].texcoord
.x
));
5604 GL_EXTCALL(glVertexAttrib2fv(0, &quad
[i
].x
));
5607 gl_info
->gl_ops
.gl
.p_glEnd();
5609 checkGLcall("draw");
5611 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
5612 context_bind_texture(context
, info
.bind_target
, 0);
5615 /* Context activation is done by the caller. */
5616 void context_draw_textured_quad(struct wined3d_context
*context
, struct wined3d_texture
*texture
,
5617 unsigned int sub_resource_idx
, const RECT
*src_rect
, const RECT
*dst_rect
,
5618 enum wined3d_texture_filter_type filter
)
5620 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5621 struct wined3d_blt_info info
;
5624 texture2d_get_blt_info(texture
, sub_resource_idx
, src_rect
, &info
);
5626 gl_info
->gl_ops
.gl
.p_glEnable(info
.bind_target
);
5627 checkGLcall("glEnable(bind_target)");
5629 level
= sub_resource_idx
% texture
->level_count
;
5630 context_bind_texture(context
, info
.bind_target
, texture
->texture_rgb
.name
);
5631 apply_texture_blit_state(gl_info
, &texture
->texture_rgb
, info
.bind_target
, level
, filter
);
5632 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, level
);
5633 gl_info
->gl_ops
.gl
.p_glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
5634 checkGLcall("glTexEnvi");
5637 gl_info
->gl_ops
.gl
.p_glBegin(GL_TRIANGLE_STRIP
);
5638 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[0].x
);
5639 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->top
);
5641 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[1].x
);
5642 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->top
);
5644 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[2].x
);
5645 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->left
, dst_rect
->bottom
);
5647 gl_info
->gl_ops
.gl
.p_glTexCoord3fv(&info
.texcoords
[3].x
);
5648 gl_info
->gl_ops
.gl
.p_glVertex2i(dst_rect
->right
, dst_rect
->bottom
);
5649 gl_info
->gl_ops
.gl
.p_glEnd();
5651 gl_info
->gl_ops
.gl
.p_glTexParameteri(info
.bind_target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
5652 context_bind_texture(context
, info
.bind_target
, 0);