[WINED3D] Dereference context slightly later, as seen in wine. This prevents some...
[reactos.git] / reactos / dll / directx / wine / wined3d / context.c
1 /*
2 * Context and render target management in wined3d
3 *
4 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
5 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "wined3d_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
25 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous);
27
28 #define WINED3D_MAX_FBO_ENTRIES 64
29 #define WINED3D_ALL_LAYERS (~0u)
30
31 static DWORD wined3d_context_tls_idx;
32
33 /* FBO helper functions */
34
35 /* Context activation is done by the caller. */
36 static void context_bind_fbo(struct wined3d_context *context, GLenum target, GLuint fbo)
37 {
38 const struct wined3d_gl_info *gl_info = context->gl_info;
39
40 switch (target)
41 {
42 case GL_READ_FRAMEBUFFER:
43 if (context->fbo_read_binding == fbo) return;
44 context->fbo_read_binding = fbo;
45 break;
46
47 case GL_DRAW_FRAMEBUFFER:
48 if (context->fbo_draw_binding == fbo) return;
49 context->fbo_draw_binding = fbo;
50 break;
51
52 case GL_FRAMEBUFFER:
53 if (context->fbo_read_binding == fbo
54 && context->fbo_draw_binding == fbo) return;
55 context->fbo_read_binding = fbo;
56 context->fbo_draw_binding = fbo;
57 break;
58
59 default:
60 FIXME("Unhandled target %#x.\n", target);
61 break;
62 }
63
64 gl_info->fbo_ops.glBindFramebuffer(target, fbo);
65 checkGLcall("glBindFramebuffer()");
66 }
67
68 /* Context activation is done by the caller. */
69 static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info, GLenum target)
70 {
71 unsigned int i;
72
73 for (i = 0; i < gl_info->limits.buffers; ++i)
74 {
75 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
76 checkGLcall("glFramebufferTexture2D()");
77 }
78 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
79 checkGLcall("glFramebufferTexture2D()");
80
81 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
82 checkGLcall("glFramebufferTexture2D()");
83 }
84
85 /* Context activation is done by the caller. */
86 static void context_destroy_fbo(struct wined3d_context *context, GLuint fbo)
87 {
88 const struct wined3d_gl_info *gl_info = context->gl_info;
89
90 context_bind_fbo(context, GL_FRAMEBUFFER, fbo);
91 context_clean_fbo_attachments(gl_info, GL_FRAMEBUFFER);
92 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
93
94 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
95 checkGLcall("glDeleteFramebuffers()");
96 }
97
98 static void context_attach_depth_stencil_rb(const struct wined3d_gl_info *gl_info,
99 GLenum fbo_target, DWORD flags, GLuint rb)
100 {
101 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
102 {
103 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
104 checkGLcall("glFramebufferRenderbuffer()");
105 }
106
107 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
108 {
109 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
110 checkGLcall("glFramebufferRenderbuffer()");
111 }
112 }
113
114 static void context_attach_gl_texture_fbo(struct wined3d_context *context,
115 GLenum fbo_target, GLenum attachment, const struct wined3d_fbo_resource *resource)
116 {
117 const struct wined3d_gl_info *gl_info = context->gl_info;
118
119 if (!resource)
120 {
121 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment, GL_TEXTURE_2D, 0, 0);
122 }
123 else if (resource->layer == WINED3D_ALL_LAYERS)
124 {
125 if (!gl_info->fbo_ops.glFramebufferTexture)
126 {
127 FIXME("OpenGL implementation doesn't support glFramebufferTexture().\n");
128 return;
129 }
130
131 gl_info->fbo_ops.glFramebufferTexture(fbo_target, attachment,
132 resource->object, resource->level);
133 }
134 else if (resource->target == GL_TEXTURE_1D_ARRAY || resource->target == GL_TEXTURE_2D_ARRAY ||
135 resource->target == GL_TEXTURE_3D)
136 {
137 if (!gl_info->fbo_ops.glFramebufferTextureLayer)
138 {
139 FIXME("OpenGL implementation doesn't support glFramebufferTextureLayer().\n");
140 return;
141 }
142
143 gl_info->fbo_ops.glFramebufferTextureLayer(fbo_target, attachment,
144 resource->object, resource->level, resource->layer);
145 }
146 else if (resource->target == GL_TEXTURE_1D)
147 {
148 gl_info->fbo_ops.glFramebufferTexture1D(fbo_target, attachment,
149 resource->target, resource->object, resource->level);
150 checkGLcall("glFramebufferTexture1D()");
151 }
152 else
153 {
154 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, attachment,
155 resource->target, resource->object, resource->level);
156 }
157 checkGLcall("attach texture to fbo");
158 }
159
160 /* Context activation is done by the caller. */
161 static void context_attach_depth_stencil_fbo(struct wined3d_context *context,
162 GLenum fbo_target, const struct wined3d_fbo_resource *resource, BOOL rb_namespace,
163 DWORD flags)
164 {
165 const struct wined3d_gl_info *gl_info = context->gl_info;
166
167 if (resource->object)
168 {
169 TRACE("Attach depth stencil %u.\n", resource->object);
170
171 if (rb_namespace)
172 {
173 context_attach_depth_stencil_rb(gl_info, fbo_target,
174 flags, resource->object);
175 }
176 else
177 {
178 if (flags & WINED3D_FBO_ENTRY_FLAG_DEPTH)
179 context_attach_gl_texture_fbo(context, fbo_target, GL_DEPTH_ATTACHMENT, resource);
180
181 if (flags & WINED3D_FBO_ENTRY_FLAG_STENCIL)
182 context_attach_gl_texture_fbo(context, fbo_target, GL_STENCIL_ATTACHMENT, resource);
183 }
184
185 if (!(flags & WINED3D_FBO_ENTRY_FLAG_DEPTH))
186 context_attach_gl_texture_fbo(context, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
187
188 if (!(flags & WINED3D_FBO_ENTRY_FLAG_STENCIL))
189 context_attach_gl_texture_fbo(context, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
190 }
191 else
192 {
193 TRACE("Attach depth stencil 0.\n");
194
195 context_attach_gl_texture_fbo(context, fbo_target, GL_DEPTH_ATTACHMENT, NULL);
196 context_attach_gl_texture_fbo(context, fbo_target, GL_STENCIL_ATTACHMENT, NULL);
197 }
198 }
199
200 /* Context activation is done by the caller. */
201 static void context_attach_surface_fbo(struct wined3d_context *context,
202 GLenum fbo_target, DWORD idx, const struct wined3d_fbo_resource *resource, BOOL rb_namespace)
203 {
204 const struct wined3d_gl_info *gl_info = context->gl_info;
205
206 TRACE("Attach GL object %u to %u.\n", resource->object, idx);
207
208 if (resource->object)
209 {
210
211 if (rb_namespace)
212 {
213 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
214 GL_RENDERBUFFER, resource->object);
215 checkGLcall("glFramebufferRenderbuffer()");
216 }
217 else
218 {
219 context_attach_gl_texture_fbo(context, fbo_target, GL_COLOR_ATTACHMENT0 + idx, resource);
220 }
221 }
222 else
223 {
224 context_attach_gl_texture_fbo(context, fbo_target, GL_COLOR_ATTACHMENT0 + idx, NULL);
225 }
226 }
227
228 static void context_dump_fbo_attachment(const struct wined3d_gl_info *gl_info, GLenum target,
229 GLenum attachment)
230 {
231 static const struct
232 {
233 GLenum target;
234 GLenum binding;
235 const char *str;
236 enum wined3d_gl_extension extension;
237 }
238 texture_type[] =
239 {
240 {GL_TEXTURE_2D, GL_TEXTURE_BINDING_2D, "2d", WINED3D_GL_EXT_NONE},
241 {GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_BINDING_RECTANGLE_ARB, "rectangle", ARB_TEXTURE_RECTANGLE},
242 {GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BINDING_2D_ARRAY, "2d-array", EXT_TEXTURE_ARRAY},
243 };
244
245 GLint type, name, samples, width, height, old_texture, level, face, fmt, tex_target;
246
247 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
248 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &name);
249 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
250 GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &type);
251
252 if (type == GL_RENDERBUFFER)
253 {
254 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, name);
255 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
256 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
257 if (gl_info->limits.samples > 1)
258 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &samples);
259 else
260 samples = 1;
261 gl_info->fbo_ops.glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_INTERNAL_FORMAT, &fmt);
262 FIXME(" %s: renderbuffer %d, %dx%d, %d samples, format %#x.\n",
263 debug_fboattachment(attachment), name, width, height, samples, fmt);
264 }
265 else if (type == GL_TEXTURE)
266 {
267 const char *tex_type_str;
268
269 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
270 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL, &level);
271 gl_info->fbo_ops.glGetFramebufferAttachmentParameteriv(target, attachment,
272 GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE, &face);
273
274 if (face)
275 {
276 gl_info->gl_ops.gl.p_glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP, &old_texture);
277
278 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, name);
279 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(face, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
280 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(face, level, GL_TEXTURE_WIDTH, &width);
281 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(face, level, GL_TEXTURE_HEIGHT, &height);
282
283 tex_target = GL_TEXTURE_CUBE_MAP;
284 tex_type_str = "cube";
285 }
286 else
287 {
288 unsigned int i;
289
290 tex_type_str = NULL;
291 for (i = 0; i < sizeof(texture_type) / sizeof(*texture_type); ++i)
292 {
293 if (!gl_info->supported[texture_type[i].extension])
294 continue;
295
296 gl_info->gl_ops.gl.p_glGetIntegerv(texture_type[i].binding, &old_texture);
297 while (gl_info->gl_ops.gl.p_glGetError());
298
299 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, name);
300 if (!gl_info->gl_ops.gl.p_glGetError())
301 {
302 tex_target = texture_type[i].target;
303 tex_type_str = texture_type[i].str;
304 break;
305 }
306 gl_info->gl_ops.gl.p_glBindTexture(texture_type[i].target, old_texture);
307 }
308 if (!tex_type_str)
309 {
310 FIXME("Cannot find type of texture %d.\n", name);
311 return;
312 }
313
314 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
315 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_WIDTH, &width);
316 gl_info->gl_ops.gl.p_glGetTexLevelParameteriv(tex_target, level, GL_TEXTURE_HEIGHT, &height);
317 }
318
319 FIXME(" %s: %s texture %d, %dx%d, format %#x.\n", debug_fboattachment(attachment),
320 tex_type_str, name, width, height, fmt);
321
322 gl_info->gl_ops.gl.p_glBindTexture(tex_target, old_texture);
323 checkGLcall("Guess texture type");
324 }
325 else if (type == GL_NONE)
326 {
327 FIXME(" %s: NONE.\n", debug_fboattachment(attachment));
328 }
329 else
330 {
331 ERR(" %s: Unknown attachment %#x.\n", debug_fboattachment(attachment), type);
332 }
333 }
334
335 /* Context activation is done by the caller. */
336 void context_check_fbo_status(const struct wined3d_context *context, GLenum target)
337 {
338 const struct wined3d_gl_info *gl_info = context->gl_info;
339 GLenum status;
340
341 if (!FIXME_ON(d3d)) return;
342
343 status = gl_info->fbo_ops.glCheckFramebufferStatus(target);
344 if (status == GL_FRAMEBUFFER_COMPLETE)
345 {
346 TRACE("FBO complete\n");
347 }
348 else
349 {
350 unsigned int i;
351
352 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status), status);
353
354 if (!context->current_fbo)
355 {
356 ERR("FBO 0 is incomplete, driver bug?\n");
357 return;
358 }
359
360 context_dump_fbo_attachment(gl_info, target, GL_DEPTH_ATTACHMENT);
361 context_dump_fbo_attachment(gl_info, target, GL_STENCIL_ATTACHMENT);
362
363 for (i = 0; i < gl_info->limits.buffers; ++i)
364 context_dump_fbo_attachment(gl_info, target, GL_COLOR_ATTACHMENT0 + i);
365 checkGLcall("Dump FBO attachments");
366 }
367 }
368
369 static inline DWORD context_generate_rt_mask(GLenum buffer)
370 {
371 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
372 return buffer ? (1u << 31) | buffer : 0;
373 }
374
375 static inline DWORD context_generate_rt_mask_from_resource(struct wined3d_resource *resource)
376 {
377 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
378 {
379 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
380 return 0;
381 }
382
383 return (1u << 31) | wined3d_texture_get_gl_buffer(texture_from_resource(resource));
384 }
385
386 static inline void context_set_fbo_key_for_render_target(const struct wined3d_context *context,
387 struct wined3d_fbo_entry_key *key, unsigned int idx, struct wined3d_rendertarget_info *render_target,
388 DWORD location)
389 {
390 unsigned int sub_resource_idx = render_target->sub_resource_idx;
391 struct wined3d_resource *resource = render_target->resource;
392 struct wined3d_texture *texture;
393
394 if (!resource || resource->format->id == WINED3DFMT_NULL || resource->type == WINED3D_RTYPE_BUFFER)
395 {
396 if (resource && resource->type == WINED3D_RTYPE_BUFFER)
397 FIXME("Not implemented for %s resources.\n", debug_d3dresourcetype(resource->type));
398 key->objects[idx].object = 0;
399 key->objects[idx].target = 0;
400 key->objects[idx].level = key->objects[idx].layer = 0;
401 return;
402 }
403
404 if (render_target->gl_view.name)
405 {
406 key->objects[idx].object = render_target->gl_view.name;
407 key->objects[idx].target = render_target->gl_view.target;
408 key->objects[idx].level = 0;
409 key->objects[idx].layer = WINED3D_ALL_LAYERS;
410 return;
411 }
412
413 texture = wined3d_texture_from_resource(resource);
414 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
415 {
416 struct wined3d_surface *surface = texture->sub_resources[sub_resource_idx].u.surface;
417
418 if (surface->current_renderbuffer)
419 {
420 key->objects[idx].object = surface->current_renderbuffer->id;
421 key->objects[idx].target = 0;
422 key->objects[idx].level = key->objects[idx].layer = 0;
423 key->rb_namespace |= 1 << idx;
424 return;
425 }
426
427 key->objects[idx].target = surface->texture_target;
428 key->objects[idx].level = surface->texture_level;
429 key->objects[idx].layer = surface->texture_layer;
430 }
431 else
432 {
433 key->objects[idx].target = texture->target;
434 key->objects[idx].level = sub_resource_idx % texture->level_count;
435 key->objects[idx].layer = sub_resource_idx / texture->level_count;
436 }
437 if (render_target->layer_count != 1)
438 key->objects[idx].layer = WINED3D_ALL_LAYERS;
439
440 switch (location)
441 {
442 case WINED3D_LOCATION_TEXTURE_RGB:
443 key->objects[idx].object = wined3d_texture_get_texture_name(texture, context, FALSE);
444 break;
445
446 case WINED3D_LOCATION_TEXTURE_SRGB:
447 key->objects[idx].object = wined3d_texture_get_texture_name(texture, context, TRUE);
448 break;
449
450 case WINED3D_LOCATION_RB_MULTISAMPLE:
451 key->objects[idx].object = texture->rb_multisample;
452 key->objects[idx].target = 0;
453 key->objects[idx].level = key->objects[idx].layer = 0;
454 key->rb_namespace |= 1 << idx;
455 break;
456
457 case WINED3D_LOCATION_RB_RESOLVED:
458 key->objects[idx].object = texture->rb_resolved;
459 key->objects[idx].target = 0;
460 key->objects[idx].level = key->objects[idx].layer = 0;
461 key->rb_namespace |= 1 << idx;
462 break;
463 }
464 }
465
466 static void context_generate_fbo_key(const struct wined3d_context *context,
467 struct wined3d_fbo_entry_key *key, struct wined3d_rendertarget_info *render_targets,
468 struct wined3d_surface *depth_stencil_surface, DWORD color_location,
469 DWORD ds_location)
470 {
471 struct wined3d_rendertarget_info depth_stencil = {{0}};
472 unsigned int i;
473
474 key->rb_namespace = 0;
475 if (depth_stencil_surface)
476 {
477 depth_stencil.resource = &depth_stencil_surface->container->resource;
478 depth_stencil.sub_resource_idx = surface_get_sub_resource_idx(depth_stencil_surface);
479 depth_stencil.layer_count = 1;
480 }
481 context_set_fbo_key_for_render_target(context, key, 0, &depth_stencil, ds_location);
482
483 for (i = 0; i < context->gl_info->limits.buffers; ++i)
484 context_set_fbo_key_for_render_target(context, key, i + 1, &render_targets[i], color_location);
485 }
486
487 static struct fbo_entry *context_create_fbo_entry(const struct wined3d_context *context,
488 struct wined3d_rendertarget_info *render_targets, struct wined3d_surface *depth_stencil,
489 DWORD color_location, DWORD ds_location)
490 {
491 const struct wined3d_gl_info *gl_info = context->gl_info;
492 unsigned int object_count = gl_info->limits.buffers + 1;
493 struct fbo_entry *entry;
494
495 entry = HeapAlloc(GetProcessHeap(), 0,
496 FIELD_OFFSET(struct fbo_entry, key.objects[object_count]));
497 memset(&entry->key, 0, FIELD_OFFSET(struct wined3d_fbo_entry_key, objects[object_count]));
498 context_generate_fbo_key(context, &entry->key, render_targets, depth_stencil, color_location, ds_location);
499 entry->flags = 0;
500 if (depth_stencil)
501 {
502 if (depth_stencil->container->resource.format_flags & WINED3DFMT_FLAG_DEPTH)
503 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
504 if (depth_stencil->container->resource.format_flags & WINED3DFMT_FLAG_STENCIL)
505 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
506 }
507 entry->rt_mask = context_generate_rt_mask(GL_COLOR_ATTACHMENT0);
508 gl_info->fbo_ops.glGenFramebuffers(1, &entry->id);
509 checkGLcall("glGenFramebuffers()");
510 TRACE("Created FBO %u.\n", entry->id);
511
512 return entry;
513 }
514
515 /* Context activation is done by the caller. */
516 static void context_reuse_fbo_entry(struct wined3d_context *context, GLenum target,
517 struct wined3d_rendertarget_info *render_targets, struct wined3d_surface *depth_stencil,
518 DWORD color_location, DWORD ds_location, struct fbo_entry *entry)
519 {
520 const struct wined3d_gl_info *gl_info = context->gl_info;
521
522 context_bind_fbo(context, target, entry->id);
523 context_clean_fbo_attachments(gl_info, target);
524
525 context_generate_fbo_key(context, &entry->key, render_targets, depth_stencil, color_location, ds_location);
526 entry->flags = 0;
527 if (depth_stencil)
528 {
529 if (depth_stencil->container->resource.format_flags & WINED3DFMT_FLAG_DEPTH)
530 entry->flags |= WINED3D_FBO_ENTRY_FLAG_DEPTH;
531 if (depth_stencil->container->resource.format_flags & WINED3DFMT_FLAG_STENCIL)
532 entry->flags |= WINED3D_FBO_ENTRY_FLAG_STENCIL;
533 }
534 }
535
536 /* Context activation is done by the caller. */
537 static void context_destroy_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
538 {
539 if (entry->id)
540 {
541 TRACE("Destroy FBO %u.\n", entry->id);
542 context_destroy_fbo(context, entry->id);
543 }
544 --context->fbo_entry_count;
545 list_remove(&entry->entry);
546 HeapFree(GetProcessHeap(), 0, entry);
547 }
548
549 /* Context activation is done by the caller. */
550 static struct fbo_entry *context_find_fbo_entry(struct wined3d_context *context, GLenum target,
551 struct wined3d_rendertarget_info *render_targets, struct wined3d_surface *depth_stencil,
552 DWORD color_location, DWORD ds_location)
553 {
554 const struct wined3d_gl_info *gl_info = context->gl_info;
555 unsigned int object_count = gl_info->limits.buffers + 1;
556 struct wined3d_texture *rt_texture, *ds_texture;
557 struct fbo_entry *entry;
558 unsigned int i, level;
559
560 if (depth_stencil && render_targets[0].resource && render_targets[0].resource->type != WINED3D_RTYPE_BUFFER)
561 {
562 rt_texture = wined3d_texture_from_resource(render_targets[0].resource);
563 level = render_targets[0].sub_resource_idx % rt_texture->level_count;
564 ds_texture = depth_stencil->container;
565
566 if (wined3d_texture_get_level_width(ds_texture, depth_stencil->texture_level)
567 < wined3d_texture_get_level_width(rt_texture, level)
568 || wined3d_texture_get_level_height(ds_texture, depth_stencil->texture_level)
569 < wined3d_texture_get_level_height(rt_texture, level))
570 {
571 WARN("Depth stencil is smaller than the primary color buffer, disabling.\n");
572 depth_stencil = NULL;
573 }
574 else if (ds_texture->resource.multisample_type != rt_texture->resource.multisample_type
575 || ds_texture->resource.multisample_quality != rt_texture->resource.multisample_quality)
576 {
577 WARN("Color multisample type %u and quality %u, depth stencil has %u and %u, disabling ds buffer.\n",
578 rt_texture->resource.multisample_type, rt_texture->resource.multisample_quality,
579 ds_texture->resource.multisample_type, ds_texture->resource.multisample_quality);
580 depth_stencil = NULL;
581 }
582 else
583 surface_set_compatible_renderbuffer(depth_stencil, &render_targets[0]);
584 }
585
586 context_generate_fbo_key(context, context->fbo_key, render_targets, depth_stencil, color_location,
587 ds_location);
588
589 if (TRACE_ON(d3d))
590 {
591 TRACE("Dumping FBO attachments:\n");
592 for (i = 0; i < gl_info->limits.buffers; ++i)
593 {
594 struct wined3d_resource *resource;
595 if ((resource = render_targets[i].resource))
596 {
597 unsigned int width, height;
598 const char *resource_type;
599
600 if (resource->type == WINED3D_RTYPE_BUFFER)
601 {
602 width = resource->size;
603 height = 1;
604 resource_type = "buffer";
605 }
606 else
607 {
608 rt_texture = wined3d_texture_from_resource(resource);
609 level = render_targets[i].sub_resource_idx % rt_texture->level_count;
610 width = wined3d_texture_get_level_pow2_width(rt_texture, level);
611 height = wined3d_texture_get_level_pow2_height(rt_texture, level);
612 resource_type = "texture";
613 }
614
615 TRACE(" Color attachment %u: %p, %u format %s, %s %u, %ux%u, %u samples.\n",
616 i, resource, render_targets[i].sub_resource_idx, debug_d3dformat(resource->format->id),
617 context->fbo_key->rb_namespace & (1 << (i + 1)) ? "renderbuffer" : resource_type,
618 context->fbo_key->objects[i + 1].object, width, height, resource->multisample_type);
619 }
620 }
621 if (depth_stencil)
622 {
623 ds_texture = depth_stencil->container;
624 TRACE(" Depth attachment: %p format %s, %s %u, %ux%u, %u samples.\n",
625 depth_stencil, debug_d3dformat(ds_texture->resource.format->id),
626 context->fbo_key->rb_namespace & (1 << 0) ? "renderbuffer" : "texture",
627 context->fbo_key->objects[0].object,
628 wined3d_texture_get_level_pow2_width(ds_texture, depth_stencil->texture_level),
629 wined3d_texture_get_level_pow2_height(ds_texture, depth_stencil->texture_level),
630 ds_texture->resource.multisample_type);
631 }
632 }
633
634 LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
635 {
636 if (memcmp(context->fbo_key, &entry->key, FIELD_OFFSET(struct wined3d_fbo_entry_key, objects[object_count])))
637 continue;
638
639 list_remove(&entry->entry);
640 list_add_head(&context->fbo_list, &entry->entry);
641 return entry;
642 }
643
644 if (context->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
645 {
646 entry = context_create_fbo_entry(context, render_targets, depth_stencil, color_location, ds_location);
647 list_add_head(&context->fbo_list, &entry->entry);
648 ++context->fbo_entry_count;
649 }
650 else
651 {
652 entry = LIST_ENTRY(list_tail(&context->fbo_list), struct fbo_entry, entry);
653 context_reuse_fbo_entry(context, target, render_targets, depth_stencil, color_location, ds_location, entry);
654 list_remove(&entry->entry);
655 list_add_head(&context->fbo_list, &entry->entry);
656 }
657
658 return entry;
659 }
660
661 /* Context activation is done by the caller. */
662 static void context_apply_fbo_entry(struct wined3d_context *context, GLenum target, struct fbo_entry *entry)
663 {
664 const struct wined3d_gl_info *gl_info = context->gl_info;
665 unsigned int i;
666 GLuint read_binding, draw_binding;
667
668 if (entry->flags & WINED3D_FBO_ENTRY_FLAG_ATTACHED)
669 {
670 context_bind_fbo(context, target, entry->id);
671 return;
672 }
673
674 read_binding = context->fbo_read_binding;
675 draw_binding = context->fbo_draw_binding;
676 context_bind_fbo(context, GL_FRAMEBUFFER, entry->id);
677
678 /* Apply render targets */
679 for (i = 0; i < gl_info->limits.buffers; ++i)
680 {
681 context_attach_surface_fbo(context, target, i, &entry->key.objects[i + 1],
682 entry->key.rb_namespace & (1 << (i + 1)));
683 }
684
685 context_attach_depth_stencil_fbo(context, target, &entry->key.objects[0],
686 entry->key.rb_namespace & 0x1, entry->flags);
687
688 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
689 * GL contexts requirements. */
690 gl_info->gl_ops.gl.p_glReadBuffer(GL_NONE);
691 context_set_draw_buffer(context, GL_NONE);
692 if (target != GL_FRAMEBUFFER)
693 {
694 if (target == GL_READ_FRAMEBUFFER)
695 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, draw_binding);
696 else
697 context_bind_fbo(context, GL_READ_FRAMEBUFFER, read_binding);
698 }
699
700 entry->flags |= WINED3D_FBO_ENTRY_FLAG_ATTACHED;
701 }
702
703 /* Context activation is done by the caller. */
704 static void context_apply_fbo_state(struct wined3d_context *context, GLenum target,
705 struct wined3d_rendertarget_info *render_targets, struct wined3d_surface *depth_stencil,
706 DWORD color_location, DWORD ds_location)
707 {
708 struct fbo_entry *entry, *entry2;
709
710 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
711 {
712 context_destroy_fbo_entry(context, entry);
713 }
714
715 if (context->rebind_fbo)
716 {
717 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
718 context->rebind_fbo = FALSE;
719 }
720
721 if (color_location == WINED3D_LOCATION_DRAWABLE)
722 {
723 context->current_fbo = NULL;
724 context_bind_fbo(context, target, 0);
725 }
726 else
727 {
728 context->current_fbo = context_find_fbo_entry(context, target, render_targets, depth_stencil,
729 color_location, ds_location);
730 context_apply_fbo_entry(context, target, context->current_fbo);
731 }
732 }
733
734 /* Context activation is done by the caller. */
735 void context_apply_fbo_state_blit(struct wined3d_context *context, GLenum target,
736 struct wined3d_surface *render_target, struct wined3d_surface *depth_stencil, DWORD location)
737 {
738 memset(context->blit_targets, 0, context->gl_info->limits.buffers * sizeof(*context->blit_targets));
739 if (render_target)
740 {
741 context->blit_targets[0].resource = &render_target->container->resource;
742 context->blit_targets[0].sub_resource_idx = surface_get_sub_resource_idx(render_target);
743 context->blit_targets[0].layer_count = 1;
744 }
745 context_apply_fbo_state(context, target, context->blit_targets, depth_stencil, location, location);
746 }
747
748 /* Context activation is done by the caller. */
749 void context_alloc_occlusion_query(struct wined3d_context *context, struct wined3d_occlusion_query *query)
750 {
751 const struct wined3d_gl_info *gl_info = context->gl_info;
752
753 if (context->free_occlusion_query_count)
754 {
755 query->id = context->free_occlusion_queries[--context->free_occlusion_query_count];
756 }
757 else
758 {
759 if (gl_info->supported[ARB_OCCLUSION_QUERY])
760 {
761 GL_EXTCALL(glGenQueries(1, &query->id));
762 checkGLcall("glGenQueries");
763
764 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context);
765 }
766 else
767 {
768 WARN("Occlusion queries not supported, not allocating query id.\n");
769 query->id = 0;
770 }
771 }
772
773 query->context = context;
774 list_add_head(&context->occlusion_queries, &query->entry);
775 }
776
777 void context_free_occlusion_query(struct wined3d_occlusion_query *query)
778 {
779 struct wined3d_context *context = query->context;
780
781 list_remove(&query->entry);
782 query->context = NULL;
783
784 if (!wined3d_array_reserve((void **)&context->free_occlusion_queries,
785 &context->free_occlusion_query_size, context->free_occlusion_query_count + 1,
786 sizeof(*context->free_occlusion_queries)))
787 {
788 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
789 return;
790 }
791
792 context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
793 }
794
795 /* Context activation is done by the caller. */
796 void context_alloc_event_query(struct wined3d_context *context, struct wined3d_event_query *query)
797 {
798 const struct wined3d_gl_info *gl_info = context->gl_info;
799
800 if (context->free_event_query_count)
801 {
802 query->object = context->free_event_queries[--context->free_event_query_count];
803 }
804 else
805 {
806 if (gl_info->supported[ARB_SYNC])
807 {
808 /* Using ARB_sync, not much to do here. */
809 query->object.sync = NULL;
810 TRACE("Allocated event query %p in context %p.\n", query->object.sync, context);
811 }
812 else if (gl_info->supported[APPLE_FENCE])
813 {
814 GL_EXTCALL(glGenFencesAPPLE(1, &query->object.id));
815 checkGLcall("glGenFencesAPPLE");
816
817 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
818 }
819 else if(gl_info->supported[NV_FENCE])
820 {
821 GL_EXTCALL(glGenFencesNV(1, &query->object.id));
822 checkGLcall("glGenFencesNV");
823
824 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
825 }
826 else
827 {
828 WARN("Event queries not supported, not allocating query id.\n");
829 query->object.id = 0;
830 }
831 }
832
833 query->context = context;
834 list_add_head(&context->event_queries, &query->entry);
835 }
836
837 void context_free_event_query(struct wined3d_event_query *query)
838 {
839 struct wined3d_context *context = query->context;
840
841 list_remove(&query->entry);
842 query->context = NULL;
843
844 if (!wined3d_array_reserve((void **)&context->free_event_queries,
845 &context->free_event_query_size, context->free_event_query_count + 1,
846 sizeof(*context->free_event_queries)))
847 {
848 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->object.id, context);
849 return;
850 }
851
852 context->free_event_queries[context->free_event_query_count++] = query->object;
853 }
854
855 /* Context activation is done by the caller. */
856 void context_alloc_timestamp_query(struct wined3d_context *context, struct wined3d_timestamp_query *query)
857 {
858 const struct wined3d_gl_info *gl_info = context->gl_info;
859
860 if (context->free_timestamp_query_count)
861 {
862 query->id = context->free_timestamp_queries[--context->free_timestamp_query_count];
863 }
864 else
865 {
866 GL_EXTCALL(glGenQueries(1, &query->id));
867 checkGLcall("glGenQueries");
868
869 TRACE("Allocated timestamp query %u in context %p.\n", query->id, context);
870 }
871
872 query->context = context;
873 list_add_head(&context->timestamp_queries, &query->entry);
874 }
875
876 void context_free_timestamp_query(struct wined3d_timestamp_query *query)
877 {
878 struct wined3d_context *context = query->context;
879
880 list_remove(&query->entry);
881 query->context = NULL;
882
883 if (!wined3d_array_reserve((void **)&context->free_timestamp_queries,
884 &context->free_timestamp_query_size, context->free_timestamp_query_count + 1,
885 sizeof(*context->free_timestamp_queries)))
886 {
887 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
888 return;
889 }
890
891 context->free_timestamp_queries[context->free_timestamp_query_count++] = query->id;
892 }
893
894 void context_alloc_so_statistics_query(struct wined3d_context *context,
895 struct wined3d_so_statistics_query *query)
896 {
897 const struct wined3d_gl_info *gl_info = context->gl_info;
898
899 if (context->free_so_statistics_query_count)
900 {
901 query->u = context->free_so_statistics_queries[--context->free_so_statistics_query_count];
902 }
903 else
904 {
905 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
906 checkGLcall("glGenQueries");
907
908 TRACE("Allocated SO statistics queries %u, %u in context %p.\n",
909 query->u.id[0], query->u.id[1], context);
910 }
911
912 query->context = context;
913 list_add_head(&context->so_statistics_queries, &query->entry);
914 }
915
916 void context_free_so_statistics_query(struct wined3d_so_statistics_query *query)
917 {
918 struct wined3d_context *context = query->context;
919
920 list_remove(&query->entry);
921 query->context = NULL;
922
923 if (!wined3d_array_reserve((void **)&context->free_so_statistics_queries,
924 &context->free_so_statistics_query_size, context->free_so_statistics_query_count + 1,
925 sizeof(*context->free_so_statistics_queries)))
926 {
927 ERR("Failed to grow free list, leaking GL queries %u, %u in context %p.\n",
928 query->u.id[0], query->u.id[1], context);
929 return;
930 }
931
932 context->free_so_statistics_queries[context->free_so_statistics_query_count++] = query->u;
933 }
934
935 void context_alloc_pipeline_statistics_query(struct wined3d_context *context,
936 struct wined3d_pipeline_statistics_query *query)
937 {
938 const struct wined3d_gl_info *gl_info = context->gl_info;
939
940 if (context->free_pipeline_statistics_query_count)
941 {
942 query->u = context->free_pipeline_statistics_queries[--context->free_pipeline_statistics_query_count];
943 }
944 else
945 {
946 GL_EXTCALL(glGenQueries(ARRAY_SIZE(query->u.id), query->u.id));
947 checkGLcall("glGenQueries");
948 }
949
950 query->context = context;
951 list_add_head(&context->pipeline_statistics_queries, &query->entry);
952 }
953
954 void context_free_pipeline_statistics_query(struct wined3d_pipeline_statistics_query *query)
955 {
956 struct wined3d_context *context = query->context;
957
958 list_remove(&query->entry);
959 query->context = NULL;
960
961 if (!wined3d_array_reserve((void **)&context->free_pipeline_statistics_queries,
962 &context->free_pipeline_statistics_query_size, context->free_pipeline_statistics_query_count + 1,
963 sizeof(*context->free_pipeline_statistics_queries)))
964 {
965 ERR("Failed to grow free list, leaking GL queries in context %p.\n", context);
966 return;
967 }
968
969 context->free_pipeline_statistics_queries[context->free_pipeline_statistics_query_count++] = query->u;
970 }
971
972 typedef void (context_fbo_entry_func_t)(struct wined3d_context *context, struct fbo_entry *entry);
973
974 static void context_enum_fbo_entries(const struct wined3d_device *device,
975 GLuint name, BOOL rb_namespace, context_fbo_entry_func_t *callback)
976 {
977 UINT i;
978
979 for (i = 0; i < device->context_count; ++i)
980 {
981 struct wined3d_context *context = device->contexts[i];
982 const struct wined3d_gl_info *gl_info = context->gl_info;
983 struct fbo_entry *entry, *entry2;
984
985 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
986 {
987 UINT j;
988
989 for (j = 0; j < gl_info->limits.buffers + 1; ++j)
990 {
991 if (entry->key.objects[j].object == name
992 && !(entry->key.rb_namespace & (1 << j)) == !rb_namespace)
993 {
994 callback(context, entry);
995 break;
996 }
997 }
998 }
999 }
1000 }
1001
1002 static void context_queue_fbo_entry_destruction(struct wined3d_context *context, struct fbo_entry *entry)
1003 {
1004 list_remove(&entry->entry);
1005 list_add_head(&context->fbo_destroy_list, &entry->entry);
1006 }
1007
1008 void context_resource_released(const struct wined3d_device *device,
1009 struct wined3d_resource *resource, enum wined3d_resource_type type)
1010 {
1011 struct wined3d_texture *texture;
1012 UINT i;
1013
1014 if (!device->d3d_initialized)
1015 return;
1016
1017 switch (type)
1018 {
1019 case WINED3D_RTYPE_TEXTURE_2D:
1020 case WINED3D_RTYPE_TEXTURE_3D:
1021 texture = texture_from_resource(resource);
1022
1023 for (i = 0; i < device->context_count; ++i)
1024 {
1025 struct wined3d_context *context = device->contexts[i];
1026 if (context->current_rt.texture == texture)
1027 {
1028 context->current_rt.texture = NULL;
1029 context->current_rt.sub_resource_idx = 0;
1030 }
1031 }
1032 break;
1033
1034 default:
1035 break;
1036 }
1037 }
1038
1039 void context_gl_resource_released(struct wined3d_device *device,
1040 GLuint name, BOOL rb_namespace)
1041 {
1042 context_enum_fbo_entries(device, name, rb_namespace, context_queue_fbo_entry_destruction);
1043 }
1044
1045 void context_surface_update(struct wined3d_context *context, const struct wined3d_surface *surface)
1046 {
1047 const struct wined3d_gl_info *gl_info = context->gl_info;
1048 struct fbo_entry *entry = context->current_fbo;
1049 unsigned int i;
1050
1051 if (!entry || context->rebind_fbo) return;
1052
1053 for (i = 0; i < gl_info->limits.buffers + 1; ++i)
1054 {
1055 if (surface->container->texture_rgb.name == entry->key.objects[i].object
1056 || surface->container->texture_srgb.name == entry->key.objects[i].object)
1057 {
1058 TRACE("Updated surface %p is bound as attachment %u to the current FBO.\n", surface, i);
1059 context->rebind_fbo = TRUE;
1060 return;
1061 }
1062 }
1063 }
1064
1065 static BOOL context_restore_pixel_format(struct wined3d_context *ctx)
1066 {
1067 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1068 BOOL ret = FALSE;
1069
1070 if (ctx->restore_pf && IsWindow(ctx->restore_pf_win))
1071 {
1072 if (ctx->gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1073 {
1074 HDC dc = GetDCEx(ctx->restore_pf_win, 0, DCX_USESTYLE | DCX_CACHE);
1075 if (dc)
1076 {
1077 if (!(ret = GL_EXTCALL(wglSetPixelFormatWINE(dc, ctx->restore_pf))))
1078 {
1079 ERR("wglSetPixelFormatWINE failed to restore pixel format %d on window %p.\n",
1080 ctx->restore_pf, ctx->restore_pf_win);
1081 }
1082 ReleaseDC(ctx->restore_pf_win, dc);
1083 }
1084 }
1085 else
1086 {
1087 ERR("can't restore pixel format %d on window %p\n", ctx->restore_pf, ctx->restore_pf_win);
1088 }
1089 }
1090
1091 ctx->restore_pf = 0;
1092 ctx->restore_pf_win = NULL;
1093 return ret;
1094 }
1095
1096 static BOOL context_set_pixel_format(struct wined3d_context *context, HDC dc, BOOL private, int format)
1097 {
1098 const struct wined3d_gl_info *gl_info = context->gl_info;
1099 int current;
1100
1101 if (dc == context->hdc && context->hdc_is_private && context->hdc_has_format)
1102 return TRUE;
1103
1104 current = gl_info->gl_ops.wgl.p_wglGetPixelFormat(dc);
1105 if (current == format) goto success;
1106
1107 if (!current)
1108 {
1109 if (!SetPixelFormat(dc, format, NULL))
1110 {
1111 /* This may also happen if the dc belongs to a destroyed window. */
1112 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
1113 format, dc, GetLastError());
1114 return FALSE;
1115 }
1116
1117 context->restore_pf = 0;
1118 context->restore_pf_win = private ? NULL : WindowFromDC(dc);
1119 goto success;
1120 }
1121
1122 /* By default WGL doesn't allow pixel format adjustments but we need it
1123 * here. For this reason there's a Wine specific wglSetPixelFormat()
1124 * which allows us to set the pixel format multiple times. Only use it
1125 * when really needed. */
1126 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
1127 {
1128 HWND win;
1129
1130 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
1131 {
1132 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
1133 format, dc);
1134 return FALSE;
1135 }
1136
1137 win = private ? NULL : WindowFromDC(dc);
1138 if (win != context->restore_pf_win)
1139 {
1140 context_restore_pixel_format(context);
1141
1142 context->restore_pf = private ? 0 : current;
1143 context->restore_pf_win = win;
1144 }
1145
1146 goto success;
1147 }
1148
1149 /* OpenGL doesn't allow pixel format adjustments. Print an error and
1150 * continue using the old format. There's a big chance that the old
1151 * format works although with a performance hit and perhaps rendering
1152 * errors. */
1153 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
1154 format, dc, current);
1155 return TRUE;
1156
1157 success:
1158 if (dc == context->hdc && context->hdc_is_private)
1159 context->hdc_has_format = TRUE;
1160 return TRUE;
1161 }
1162
1163 static BOOL context_set_gl_context(struct wined3d_context *ctx)
1164 {
1165 struct wined3d_swapchain *swapchain = ctx->swapchain;
1166 BOOL backup = FALSE;
1167
1168 if (!context_set_pixel_format(ctx, ctx->hdc, ctx->hdc_is_private, ctx->pixel_format))
1169 {
1170 WARN("Failed to set pixel format %d on device context %p.\n",
1171 ctx->pixel_format, ctx->hdc);
1172 backup = TRUE;
1173 }
1174
1175 if (backup || !wglMakeCurrent(ctx->hdc, ctx->glCtx))
1176 {
1177 HDC dc;
1178
1179 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
1180 ctx->glCtx, ctx->hdc, GetLastError());
1181 ctx->valid = 0;
1182 WARN("Trying fallback to the backup window.\n");
1183
1184 /* FIXME: If the context is destroyed it's no longer associated with
1185 * a swapchain, so we can't use the swapchain to get a backup dc. To
1186 * make this work windowless contexts would need to be handled by the
1187 * device. */
1188 if (ctx->destroyed || !swapchain)
1189 {
1190 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx);
1191 context_set_current(NULL);
1192 return FALSE;
1193 }
1194
1195 if (!(dc = swapchain_get_backup_dc(swapchain)))
1196 {
1197 context_set_current(NULL);
1198 return FALSE;
1199 }
1200
1201 if (!context_set_pixel_format(ctx, dc, TRUE, ctx->pixel_format))
1202 {
1203 ERR("Failed to set pixel format %d on device context %p.\n",
1204 ctx->pixel_format, dc);
1205 context_set_current(NULL);
1206 return FALSE;
1207 }
1208
1209 if (!wglMakeCurrent(dc, ctx->glCtx))
1210 {
1211 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
1212 dc, GetLastError());
1213 context_set_current(NULL);
1214 return FALSE;
1215 }
1216
1217 ctx->valid = 1;
1218 }
1219 ctx->needs_set = 0;
1220 return TRUE;
1221 }
1222
1223 static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx)
1224 {
1225 if (!wglMakeCurrent(dc, gl_ctx))
1226 {
1227 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1228 gl_ctx, dc, GetLastError());
1229 context_set_current(NULL);
1230 }
1231 }
1232
1233 static void context_update_window(struct wined3d_context *context)
1234 {
1235 if (!context->swapchain)
1236 return;
1237
1238 if (context->win_handle == context->swapchain->win_handle)
1239 return;
1240
1241 TRACE("Updating context %p window from %p to %p.\n",
1242 context, context->win_handle, context->swapchain->win_handle);
1243
1244 if (context->hdc)
1245 wined3d_release_dc(context->win_handle, context->hdc);
1246
1247 context->win_handle = context->swapchain->win_handle;
1248 context->hdc_is_private = FALSE;
1249 context->hdc_has_format = FALSE;
1250 context->needs_set = 1;
1251 context->valid = 1;
1252
1253 if (!(context->hdc = GetDCEx(context->win_handle, 0, DCX_USESTYLE | DCX_CACHE)))
1254 {
1255 ERR("Failed to get a device context for window %p.\n", context->win_handle);
1256 context->valid = 0;
1257 }
1258 }
1259
1260 static void context_destroy_gl_resources(struct wined3d_context *context)
1261 {
1262 const struct wined3d_gl_info *gl_info = context->gl_info;
1263 struct wined3d_so_statistics_query *so_statistics_query;
1264 struct wined3d_pipeline_statistics_query *pipeline_statistics_query;
1265 struct wined3d_timestamp_query *timestamp_query;
1266 struct wined3d_occlusion_query *occlusion_query;
1267 struct wined3d_event_query *event_query;
1268 struct fbo_entry *entry, *entry2;
1269 HGLRC restore_ctx;
1270 HDC restore_dc;
1271 unsigned int i;
1272
1273 restore_ctx = wglGetCurrentContext();
1274 restore_dc = wglGetCurrentDC();
1275
1276 if (restore_ctx == context->glCtx)
1277 restore_ctx = NULL;
1278 else if (context->valid)
1279 context_set_gl_context(context);
1280
1281 LIST_FOR_EACH_ENTRY(so_statistics_query, &context->so_statistics_queries,
1282 struct wined3d_so_statistics_query, entry)
1283 {
1284 if (context->valid)
1285 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(so_statistics_query->u.id), so_statistics_query->u.id));
1286 so_statistics_query->context = NULL;
1287 }
1288
1289 LIST_FOR_EACH_ENTRY(pipeline_statistics_query, &context->pipeline_statistics_queries,
1290 struct wined3d_pipeline_statistics_query, entry)
1291 {
1292 if (context->valid)
1293 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(pipeline_statistics_query->u.id), pipeline_statistics_query->u.id));
1294 pipeline_statistics_query->context = NULL;
1295 }
1296
1297 LIST_FOR_EACH_ENTRY(timestamp_query, &context->timestamp_queries, struct wined3d_timestamp_query, entry)
1298 {
1299 if (context->valid)
1300 GL_EXTCALL(glDeleteQueries(1, &timestamp_query->id));
1301 timestamp_query->context = NULL;
1302 }
1303
1304 LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
1305 {
1306 if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
1307 GL_EXTCALL(glDeleteQueries(1, &occlusion_query->id));
1308 occlusion_query->context = NULL;
1309 }
1310
1311 LIST_FOR_EACH_ENTRY(event_query, &context->event_queries, struct wined3d_event_query, entry)
1312 {
1313 if (context->valid)
1314 {
1315 if (gl_info->supported[ARB_SYNC])
1316 {
1317 if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
1318 }
1319 else if (gl_info->supported[APPLE_FENCE]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query->object.id));
1320 else if (gl_info->supported[NV_FENCE]) GL_EXTCALL(glDeleteFencesNV(1, &event_query->object.id));
1321 }
1322 event_query->context = NULL;
1323 }
1324
1325 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
1326 {
1327 if (!context->valid) entry->id = 0;
1328 context_destroy_fbo_entry(context, entry);
1329 }
1330
1331 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
1332 {
1333 if (!context->valid) entry->id = 0;
1334 context_destroy_fbo_entry(context, entry);
1335 }
1336
1337 if (context->valid)
1338 {
1339 if (context->dummy_arbfp_prog)
1340 {
1341 GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
1342 }
1343
1344 if (gl_info->supported[WINED3D_GL_PRIMITIVE_QUERY])
1345 {
1346 for (i = 0; i < context->free_so_statistics_query_count; ++i)
1347 {
1348 union wined3d_gl_so_statistics_query *q = &context->free_so_statistics_queries[i];
1349 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1350 }
1351 }
1352
1353 if (gl_info->supported[ARB_PIPELINE_STATISTICS_QUERY])
1354 {
1355 for (i = 0; i < context->free_pipeline_statistics_query_count; ++i)
1356 {
1357 union wined3d_gl_pipeline_statistics_query *q = &context->free_pipeline_statistics_queries[i];
1358 GL_EXTCALL(glDeleteQueries(ARRAY_SIZE(q->id), q->id));
1359 }
1360 }
1361
1362 if (gl_info->supported[ARB_TIMER_QUERY])
1363 GL_EXTCALL(glDeleteQueries(context->free_timestamp_query_count, context->free_timestamp_queries));
1364
1365 if (gl_info->supported[ARB_OCCLUSION_QUERY])
1366 GL_EXTCALL(glDeleteQueries(context->free_occlusion_query_count, context->free_occlusion_queries));
1367
1368 if (gl_info->supported[ARB_SYNC])
1369 {
1370 for (i = 0; i < context->free_event_query_count; ++i)
1371 {
1372 GL_EXTCALL(glDeleteSync(context->free_event_queries[i].sync));
1373 }
1374 }
1375 else if (gl_info->supported[APPLE_FENCE])
1376 {
1377 for (i = 0; i < context->free_event_query_count; ++i)
1378 {
1379 GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_event_queries[i].id));
1380 }
1381 }
1382 else if (gl_info->supported[NV_FENCE])
1383 {
1384 for (i = 0; i < context->free_event_query_count; ++i)
1385 {
1386 GL_EXTCALL(glDeleteFencesNV(1, &context->free_event_queries[i].id));
1387 }
1388 }
1389
1390 checkGLcall("context cleanup");
1391 }
1392
1393 HeapFree(GetProcessHeap(), 0, context->free_so_statistics_queries);
1394 HeapFree(GetProcessHeap(), 0, context->free_pipeline_statistics_queries);
1395 HeapFree(GetProcessHeap(), 0, context->free_timestamp_queries);
1396 HeapFree(GetProcessHeap(), 0, context->free_occlusion_queries);
1397 HeapFree(GetProcessHeap(), 0, context->free_event_queries);
1398
1399 context_restore_pixel_format(context);
1400 if (restore_ctx)
1401 {
1402 context_restore_gl_context(gl_info, restore_dc, restore_ctx);
1403 }
1404 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1405 {
1406 ERR("Failed to disable GL context.\n");
1407 }
1408
1409 wined3d_release_dc(context->win_handle, context->hdc);
1410
1411 if (!wglDeleteContext(context->glCtx))
1412 {
1413 DWORD err = GetLastError();
1414 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
1415 }
1416 }
1417
1418 DWORD context_get_tls_idx(void)
1419 {
1420 return wined3d_context_tls_idx;
1421 }
1422
1423 void context_set_tls_idx(DWORD idx)
1424 {
1425 wined3d_context_tls_idx = idx;
1426 }
1427
1428 struct wined3d_context *context_get_current(void)
1429 {
1430 return TlsGetValue(wined3d_context_tls_idx);
1431 }
1432
1433 BOOL context_set_current(struct wined3d_context *ctx)
1434 {
1435 struct wined3d_context *old = context_get_current();
1436
1437 if (old == ctx)
1438 {
1439 TRACE("Already using D3D context %p.\n", ctx);
1440 return TRUE;
1441 }
1442
1443 if (old)
1444 {
1445 if (old->destroyed)
1446 {
1447 TRACE("Switching away from destroyed context %p.\n", old);
1448 context_destroy_gl_resources(old);
1449 HeapFree(GetProcessHeap(), 0, (void *)old->gl_info);
1450 HeapFree(GetProcessHeap(), 0, old);
1451 }
1452 else
1453 {
1454 if (wglGetCurrentContext())
1455 {
1456 const struct wined3d_gl_info *gl_info = old->gl_info;
1457 TRACE("Flushing context %p before switching to %p.\n", old, ctx);
1458 gl_info->gl_ops.gl.p_glFlush();
1459 }
1460 old->current = 0;
1461 }
1462 }
1463
1464 if (ctx)
1465 {
1466 if (!ctx->valid)
1467 {
1468 ERR("Trying to make invalid context %p current\n", ctx);
1469 return FALSE;
1470 }
1471
1472 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
1473 if (!context_set_gl_context(ctx))
1474 return FALSE;
1475 ctx->current = 1;
1476 }
1477 else if (wglGetCurrentContext())
1478 {
1479 TRACE("Clearing current D3D context.\n");
1480 if (!wglMakeCurrent(NULL, NULL))
1481 {
1482 DWORD err = GetLastError();
1483 ERR("Failed to clear current GL context, last error %#x.\n", err);
1484 TlsSetValue(wined3d_context_tls_idx, NULL);
1485 return FALSE;
1486 }
1487 }
1488
1489 return TlsSetValue(wined3d_context_tls_idx, ctx);
1490 }
1491
1492 void context_release(struct wined3d_context *context)
1493 {
1494 TRACE("Releasing context %p, level %u.\n", context, context->level);
1495
1496 if (WARN_ON(d3d))
1497 {
1498 if (!context->level)
1499 WARN("Context %p is not active.\n", context);
1500 else if (context != context_get_current())
1501 WARN("Context %p is not the current context.\n", context);
1502 }
1503
1504 if (!--context->level)
1505 {
1506 if (context_restore_pixel_format(context))
1507 context->needs_set = 1;
1508 if (context->restore_ctx)
1509 {
1510 TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
1511 context_restore_gl_context(context->gl_info, context->restore_dc, context->restore_ctx);
1512 context->restore_ctx = NULL;
1513 context->restore_dc = NULL;
1514 }
1515
1516 if (context->destroy_delayed)
1517 {
1518 TRACE("Destroying context %p.\n", context);
1519 context_destroy(context->device, context);
1520 }
1521 }
1522 }
1523
1524 /* This is used when a context for render target A is active, but a separate context is
1525 * needed to access the WGL framebuffer for render target B. Re-acquire a context for rt
1526 * A to avoid breaking caller code. */
1527 void context_restore(struct wined3d_context *context, struct wined3d_surface *restore)
1528 {
1529 if (context->current_rt.texture != restore->container
1530 || context->current_rt.sub_resource_idx != surface_get_sub_resource_idx(restore))
1531 {
1532 context_release(context);
1533 context = context_acquire(restore->container->resource.device,
1534 restore->container, surface_get_sub_resource_idx(restore));
1535 }
1536
1537 context_release(context);
1538 }
1539
1540 static void context_enter(struct wined3d_context *context)
1541 {
1542 TRACE("Entering context %p, level %u.\n", context, context->level + 1);
1543
1544 if (!context->level++)
1545 {
1546 const struct wined3d_context *current_context = context_get_current();
1547 HGLRC current_gl = wglGetCurrentContext();
1548
1549 if (current_gl && (!current_context || current_context->glCtx != current_gl))
1550 {
1551 TRACE("Another GL context (%p on device context %p) is already current.\n",
1552 current_gl, wglGetCurrentDC());
1553 context->restore_ctx = current_gl;
1554 context->restore_dc = wglGetCurrentDC();
1555 context->needs_set = 1;
1556 }
1557 else if (!context->needs_set && !(context->hdc_is_private && context->hdc_has_format)
1558 && context->pixel_format != context->gl_info->gl_ops.wgl.p_wglGetPixelFormat(context->hdc))
1559 context->needs_set = 1;
1560 }
1561 }
1562
1563 void context_invalidate_compute_state(struct wined3d_context *context, DWORD state_id)
1564 {
1565 DWORD representative = context->state_table[state_id].representative - STATE_COMPUTE_OFFSET;
1566 unsigned int index, shift;
1567
1568 index = representative / (sizeof(*context->dirty_compute_states) * CHAR_BIT);
1569 shift = representative & (sizeof(*context->dirty_compute_states) * CHAR_BIT - 1);
1570 context->dirty_compute_states[index] |= (1u << shift);
1571 }
1572
1573 void context_invalidate_state(struct wined3d_context *context, DWORD state)
1574 {
1575 DWORD rep = context->state_table[state].representative;
1576 DWORD idx;
1577 BYTE shift;
1578
1579 if (isStateDirty(context, rep)) return;
1580
1581 context->dirtyArray[context->numDirtyEntries++] = rep;
1582 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
1583 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
1584 context->isStateDirty[idx] |= (1u << shift);
1585 }
1586
1587 /* This function takes care of wined3d pixel format selection. */
1588 static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1589 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1590 BOOL auxBuffers)
1591 {
1592 unsigned int cfg_count = device->adapter->cfg_count;
1593 unsigned int current_value;
1594 PIXELFORMATDESCRIPTOR pfd;
1595 int iPixelFormat = 0;
1596 unsigned int i;
1597
1598 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x.\n",
1599 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1600 auxBuffers);
1601
1602 current_value = 0;
1603 for (i = 0; i < cfg_count; ++i)
1604 {
1605 const struct wined3d_pixel_format *cfg = &device->adapter->cfgs[i];
1606 unsigned int value;
1607
1608 /* For now only accept RGBA formats. Perhaps some day we will
1609 * allow floating point formats for pbuffers. */
1610 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1611 continue;
1612 /* In window mode we need a window drawable format and double buffering. */
1613 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1614 continue;
1615 if (cfg->redSize < color_format->red_size)
1616 continue;
1617 if (cfg->greenSize < color_format->green_size)
1618 continue;
1619 if (cfg->blueSize < color_format->blue_size)
1620 continue;
1621 if (cfg->alphaSize < color_format->alpha_size)
1622 continue;
1623 if (cfg->depthSize < ds_format->depth_size)
1624 continue;
1625 if (ds_format->stencil_size && cfg->stencilSize != ds_format->stencil_size)
1626 continue;
1627 /* Check multisampling support. */
1628 if (cfg->numSamples)
1629 continue;
1630
1631 value = 1;
1632 /* We try to locate a format which matches our requirements exactly. In case of
1633 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1634 if (cfg->depthSize == ds_format->depth_size)
1635 value += 1;
1636 if (cfg->stencilSize == ds_format->stencil_size)
1637 value += 2;
1638 if (cfg->alphaSize == color_format->alpha_size)
1639 value += 4;
1640 /* We like to have aux buffers in backbuffer mode */
1641 if (auxBuffers && cfg->auxBuffers)
1642 value += 8;
1643 if (cfg->redSize == color_format->red_size
1644 && cfg->greenSize == color_format->green_size
1645 && cfg->blueSize == color_format->blue_size)
1646 value += 16;
1647
1648 if (value > current_value)
1649 {
1650 iPixelFormat = cfg->iPixelFormat;
1651 current_value = value;
1652 }
1653 }
1654
1655 if (!iPixelFormat)
1656 {
1657 ERR("Trying to locate a compatible pixel format because an exact match failed.\n");
1658
1659 memset(&pfd, 0, sizeof(pfd));
1660 pfd.nSize = sizeof(pfd);
1661 pfd.nVersion = 1;
1662 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1663 pfd.iPixelType = PFD_TYPE_RGBA;
1664 pfd.cAlphaBits = color_format->alpha_size;
1665 pfd.cColorBits = color_format->red_size + color_format->green_size
1666 + color_format->blue_size + color_format->alpha_size;
1667 pfd.cDepthBits = ds_format->depth_size;
1668 pfd.cStencilBits = ds_format->stencil_size;
1669 pfd.iLayerType = PFD_MAIN_PLANE;
1670
1671 if (!(iPixelFormat = ChoosePixelFormat(hdc, &pfd)))
1672 {
1673 /* Something is very wrong as ChoosePixelFormat() barely fails. */
1674 ERR("Can't find a suitable pixel format.\n");
1675 return 0;
1676 }
1677 }
1678
1679 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s.\n",
1680 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1681 return iPixelFormat;
1682 }
1683
1684 /* Context activation is done by the caller. */
1685 void context_bind_dummy_textures(const struct wined3d_device *device, const struct wined3d_context *context)
1686 {
1687 const struct wined3d_gl_info *gl_info = context->gl_info;
1688 unsigned int i;
1689
1690 for (i = 0; i < gl_info->limits.combined_samplers; ++i)
1691 {
1692 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + i));
1693 checkGLcall("glActiveTexture");
1694
1695 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, device->dummy_textures.tex_1d);
1696 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_textures.tex_2d);
1697
1698 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1699 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_textures.tex_rect);
1700
1701 if (gl_info->supported[EXT_TEXTURE3D])
1702 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_textures.tex_3d);
1703
1704 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1705 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_textures.tex_cube);
1706
1707 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
1708 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, device->dummy_textures.tex_cube_array);
1709
1710 if (gl_info->supported[EXT_TEXTURE_ARRAY])
1711 {
1712 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, device->dummy_textures.tex_1d_array);
1713 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_textures.tex_2d_array);
1714 }
1715
1716 if (gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
1717 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, device->dummy_textures.tex_buffer);
1718
1719 checkGLcall("Bind dummy textures");
1720 }
1721 }
1722
1723 void wined3d_check_gl_call(const struct wined3d_gl_info *gl_info,
1724 const char *file, unsigned int line, const char *name)
1725 {
1726 GLint err;
1727
1728 if (gl_info->supported[ARB_DEBUG_OUTPUT] || (err = gl_info->gl_ops.gl.p_glGetError()) == GL_NO_ERROR)
1729 {
1730 TRACE("%s call ok %s / %u.\n", name, file, line);
1731 return;
1732 }
1733
1734 do
1735 {
1736 ERR(">>>>>>> %s (%#x) from %s @ %s / %u.\n",
1737 debug_glerror(err), err, name, file,line);
1738 err = gl_info->gl_ops.gl.p_glGetError();
1739 } while (err != GL_NO_ERROR);
1740 }
1741
1742 static BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1743 {
1744 return gl_info->supported[ARB_DEBUG_OUTPUT]
1745 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1746 }
1747
1748 static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1749 GLenum severity, GLsizei length, const char *message, void *ctx)
1750 {
1751 switch (type)
1752 {
1753 case GL_DEBUG_TYPE_ERROR_ARB:
1754 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1755 break;
1756
1757 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1758 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1759 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1760 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1761 break;
1762
1763 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1764 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1765 break;
1766
1767 default:
1768 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1769 break;
1770 }
1771 }
1772
1773 HGLRC context_create_wgl_attribs(const struct wined3d_gl_info *gl_info, HDC hdc, HGLRC share_ctx)
1774 {
1775 HGLRC ctx;
1776 unsigned int ctx_attrib_idx = 0;
1777 GLint ctx_attribs[7], ctx_flags = 0;
1778
1779 if (context_debug_output_enabled(gl_info))
1780 ctx_flags = WGL_CONTEXT_DEBUG_BIT_ARB;
1781 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MAJOR_VERSION_ARB;
1782 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version >> 16;
1783 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_MINOR_VERSION_ARB;
1784 ctx_attribs[ctx_attrib_idx++] = gl_info->selected_gl_version & 0xffff;
1785 if (gl_info->selected_gl_version >= MAKEDWORD_VERSION(3, 2))
1786 ctx_flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1787 if (ctx_flags)
1788 {
1789 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1790 ctx_attribs[ctx_attrib_idx++] = ctx_flags;
1791 }
1792 ctx_attribs[ctx_attrib_idx] = 0;
1793
1794 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1795 {
1796 if (ctx_flags & WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB)
1797 {
1798 ctx_attribs[ctx_attrib_idx - 1] &= ~WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
1799 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1800 WARN("Failed to create a WGL context with wglCreateContextAttribsARB, last error %#x.\n",
1801 GetLastError());
1802 }
1803 }
1804 return ctx;
1805 }
1806
1807 struct wined3d_context *context_create(struct wined3d_swapchain *swapchain,
1808 struct wined3d_texture *target, const struct wined3d_format *ds_format)
1809 {
1810 struct wined3d_device *device = swapchain->device;
1811 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
1812 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1813 const struct wined3d_format *color_format;
1814 struct wined3d_context *ret;
1815 BOOL hdc_is_private = FALSE;
1816 BOOL auxBuffers = FALSE;
1817 HGLRC ctx, share_ctx;
1818 DWORD target_usage;
1819 int pixel_format;
1820 unsigned int i;
1821 DWORD state;
1822 HDC hdc = 0;
1823
1824 TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
1825
1826 wined3d_from_cs(device->cs);
1827
1828 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
1829 if (!ret)
1830 return NULL;
1831
1832 if (!(ret->blit_targets = wined3d_calloc(gl_info->limits.buffers, sizeof(*ret->blit_targets))))
1833 goto out;
1834
1835 if (!(ret->draw_buffers = wined3d_calloc(gl_info->limits.buffers, sizeof(*ret->draw_buffers))))
1836 goto out;
1837
1838 ret->fbo_key = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1839 FIELD_OFFSET(struct wined3d_fbo_entry_key, objects[gl_info->limits.buffers + 1]));
1840 if (!ret->fbo_key)
1841 goto out;
1842
1843 ret->free_timestamp_query_size = 4;
1844 if (!(ret->free_timestamp_queries = wined3d_calloc(ret->free_timestamp_query_size,
1845 sizeof(*ret->free_timestamp_queries))))
1846 goto out;
1847 list_init(&ret->timestamp_queries);
1848
1849 ret->free_occlusion_query_size = 4;
1850 if (!(ret->free_occlusion_queries = wined3d_calloc(ret->free_occlusion_query_size,
1851 sizeof(*ret->free_occlusion_queries))))
1852 goto out;
1853 list_init(&ret->occlusion_queries);
1854
1855 ret->free_event_query_size = 4;
1856 if (!(ret->free_event_queries = wined3d_calloc(ret->free_event_query_size,
1857 sizeof(*ret->free_event_queries))))
1858 goto out;
1859 list_init(&ret->event_queries);
1860
1861 list_init(&ret->so_statistics_queries);
1862
1863 list_init(&ret->pipeline_statistics_queries);
1864
1865 list_init(&ret->fbo_list);
1866 list_init(&ret->fbo_destroy_list);
1867
1868 if (!device->shader_backend->shader_allocate_context_data(ret))
1869 {
1870 ERR("Failed to allocate shader backend context data.\n");
1871 goto out;
1872 }
1873 if (!device->adapter->fragment_pipe->allocate_context_data(ret))
1874 {
1875 ERR("Failed to allocate fragment pipeline context data.\n");
1876 goto out;
1877 }
1878
1879 for (i = 0; i < ARRAY_SIZE(ret->tex_unit_map); ++i)
1880 ret->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
1881 for (i = 0; i < ARRAY_SIZE(ret->rev_tex_unit_map); ++i)
1882 ret->rev_tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
1883 if (gl_info->limits.graphics_samplers >= MAX_COMBINED_SAMPLERS)
1884 {
1885 /* Initialize the texture unit mapping to a 1:1 mapping. */
1886 unsigned int base, count;
1887
1888 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_PIXEL, &base, &count);
1889 if (base + MAX_FRAGMENT_SAMPLERS > ARRAY_SIZE(ret->rev_tex_unit_map))
1890 {
1891 ERR("Unexpected texture unit base index %u.\n", base);
1892 goto out;
1893 }
1894 for (i = 0; i < min(count, MAX_FRAGMENT_SAMPLERS); ++i)
1895 {
1896 ret->tex_unit_map[i] = base + i;
1897 ret->rev_tex_unit_map[base + i] = i;
1898 }
1899
1900 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_VERTEX, &base, &count);
1901 if (base + MAX_VERTEX_SAMPLERS > ARRAY_SIZE(ret->rev_tex_unit_map))
1902 {
1903 ERR("Unexpected texture unit base index %u.\n", base);
1904 goto out;
1905 }
1906 for (i = 0; i < min(count, MAX_VERTEX_SAMPLERS); ++i)
1907 {
1908 ret->tex_unit_map[MAX_FRAGMENT_SAMPLERS + i] = base + i;
1909 ret->rev_tex_unit_map[base + i] = MAX_FRAGMENT_SAMPLERS + i;
1910 }
1911 }
1912
1913 if (!(ret->texture_type = wined3d_calloc(gl_info->limits.combined_samplers,
1914 sizeof(*ret->texture_type))))
1915 goto out;
1916
1917 if (!(hdc = GetDCEx(swapchain->win_handle, 0, DCX_USESTYLE | DCX_CACHE)))
1918 {
1919 WARN("Failed to retrieve device context, trying swapchain backup.\n");
1920
1921 if ((hdc = swapchain_get_backup_dc(swapchain)))
1922 hdc_is_private = TRUE;
1923 else
1924 {
1925 ERR("Failed to retrieve a device context.\n");
1926 goto out;
1927 }
1928 }
1929
1930 color_format = target->resource.format;
1931 target_usage = target->resource.usage;
1932
1933 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1934 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1935 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1936 {
1937 auxBuffers = TRUE;
1938
1939 if (color_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1940 color_format = wined3d_get_format(gl_info, WINED3DFMT_B4G4R4A4_UNORM, target_usage);
1941 else if (color_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1942 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM, target_usage);
1943 }
1944
1945 /* DirectDraw supports 8bit paletted render targets and these are used by
1946 * old games like StarCraft and C&C. Most modern hardware doesn't support
1947 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1948 * conversion (ab)uses the alpha component for storing the palette index.
1949 * For this reason we require a format with 8bit alpha, so request
1950 * A8R8G8B8. */
1951 if (color_format->id == WINED3DFMT_P8_UINT)
1952 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM, target_usage);
1953
1954 /* When using FBOs for off-screen rendering, we only use the drawable for
1955 * presentation blits, and don't do any rendering to it. That means we
1956 * don't need depth or stencil buffers, and can mostly ignore the render
1957 * target format. This wouldn't necessarily be quite correct for 10bpc
1958 * display modes, but we don't currently support those.
1959 * Using the same format regardless of the color/depth/stencil targets
1960 * makes it much less likely that different wined3d instances will set
1961 * conflicting pixel formats. */
1962 if (wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
1963 {
1964 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM, target_usage);
1965 ds_format = wined3d_get_format(gl_info, WINED3DFMT_UNKNOWN, WINED3DUSAGE_DEPTHSTENCIL);
1966 }
1967
1968 /* Try to find a pixel format which matches our requirements. */
1969 if (!(pixel_format = context_choose_pixel_format(device, hdc, color_format, ds_format, auxBuffers)))
1970 goto out;
1971
1972 ret->gl_info = gl_info;
1973
1974 context_enter(ret);
1975
1976 if (!context_set_pixel_format(ret, hdc, hdc_is_private, pixel_format))
1977 {
1978 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format, hdc);
1979 context_release(ret);
1980 goto out;
1981 }
1982
1983 share_ctx = device->context_count ? device->contexts[0]->glCtx : NULL;
1984 if (gl_info->p_wglCreateContextAttribsARB)
1985 {
1986 if (!(ctx = context_create_wgl_attribs(gl_info, hdc, share_ctx)))
1987 goto out;
1988 }
1989 else
1990 {
1991 if (!(ctx = wglCreateContext(hdc)))
1992 {
1993 ERR("Failed to create a WGL context.\n");
1994 context_release(ret);
1995 goto out;
1996 }
1997
1998 if (share_ctx && !wglShareLists(share_ctx, ctx))
1999 {
2000 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
2001 context_release(ret);
2002 if (!wglDeleteContext(ctx))
2003 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2004 goto out;
2005 }
2006 }
2007
2008 if (!device_context_add(device, ret))
2009 {
2010 ERR("Failed to add the newly created context to the context list\n");
2011 context_release(ret);
2012 if (!wglDeleteContext(ctx))
2013 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2014 goto out;
2015 }
2016
2017 ret->d3d_info = d3d_info;
2018 ret->state_table = device->StateTable;
2019
2020 /* Mark all states dirty to force a proper initialization of the states on
2021 * the first use of the context. Compute states do not need initialization. */
2022 for (state = 0; state <= STATE_HIGHEST; ++state)
2023 {
2024 if (ret->state_table[state].representative && !STATE_IS_COMPUTE(state))
2025 context_invalidate_state(ret, state);
2026 }
2027
2028 ret->device = device;
2029 ret->swapchain = swapchain;
2030 ret->current_rt.texture = target;
2031 ret->current_rt.sub_resource_idx = 0;
2032 ret->tid = GetCurrentThreadId();
2033
2034 ret->render_offscreen = wined3d_resource_is_offscreen(&target->resource);
2035 ret->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
2036 ret->valid = 1;
2037
2038 ret->glCtx = ctx;
2039 ret->win_handle = swapchain->win_handle;
2040 ret->hdc = hdc;
2041 ret->hdc_is_private = hdc_is_private;
2042 ret->hdc_has_format = TRUE;
2043 ret->pixel_format = pixel_format;
2044 ret->needs_set = 1;
2045
2046 /* Set up the context defaults */
2047 if (!context_set_current(ret))
2048 {
2049 ERR("Cannot activate context to set up defaults.\n");
2050 device_context_remove(device, ret);
2051 context_release(ret);
2052 if (!wglDeleteContext(ctx))
2053 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
2054 goto out;
2055 }
2056
2057 if (context_debug_output_enabled(gl_info))
2058 {
2059 GL_EXTCALL(glDebugMessageCallback(wined3d_debug_callback, ret));
2060 if (TRACE_ON(d3d_synchronous))
2061 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
2062 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
2063 if (ERR_ON(d3d))
2064 {
2065 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR,
2066 GL_DONT_CARE, 0, NULL, GL_TRUE));
2067 }
2068 if (FIXME_ON(d3d))
2069 {
2070 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR,
2071 GL_DONT_CARE, 0, NULL, GL_TRUE));
2072 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR,
2073 GL_DONT_CARE, 0, NULL, GL_TRUE));
2074 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY,
2075 GL_DONT_CARE, 0, NULL, GL_TRUE));
2076 }
2077 if (WARN_ON(d3d_perf))
2078 {
2079 GL_EXTCALL(glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE,
2080 GL_DONT_CARE, 0, NULL, GL_TRUE));
2081 }
2082 }
2083
2084 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2085 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
2086
2087 TRACE("Setting up the screen\n");
2088
2089 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2090 {
2091 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
2092 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
2093
2094 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
2095 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
2096
2097 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
2098 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
2099 }
2100 else
2101 {
2102 GLuint vao;
2103
2104 GL_EXTCALL(glGenVertexArrays(1, &vao));
2105 GL_EXTCALL(glBindVertexArray(vao));
2106 checkGLcall("creating VAO");
2107 }
2108
2109 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
2110 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
2111 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
2112 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, 1);");
2113
2114 if (gl_info->supported[ARB_VERTEX_BLEND])
2115 {
2116 /* Direct3D always uses n-1 weights for n world matrices and uses
2117 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
2118 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
2119 * enabled as well. */
2120 gl_info->gl_ops.gl.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB);
2121 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
2122 }
2123 if (gl_info->supported[NV_TEXTURE_SHADER2])
2124 {
2125 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
2126 * the previous texture where to source the offset from is always unit - 1.
2127 */
2128 for (i = 1; i < gl_info->limits.textures; ++i)
2129 {
2130 context_active_texture(ret, gl_info, i);
2131 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
2132 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + i - 1);
2133 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
2134 }
2135 }
2136 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
2137 {
2138 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
2139 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
2140 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
2141 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2142 * is ever assigned.
2143 *
2144 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2145 * program and the dummy program is destroyed when the context is destroyed.
2146 */
2147 static const char dummy_program[] =
2148 "!!ARBfp1.0\n"
2149 "MOV result.color, fragment.color.primary;\n"
2150 "END\n";
2151 GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
2152 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
2153 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2154 }
2155
2156 if (gl_info->supported[ARB_POINT_SPRITE])
2157 {
2158 for (i = 0; i < gl_info->limits.textures; ++i)
2159 {
2160 context_active_texture(ret, gl_info, i);
2161 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2162 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2163 }
2164 }
2165
2166 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2167 {
2168 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2169 }
2170 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2171 {
2172 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2173 }
2174 if (!(d3d_info->wined3d_creation_flags & WINED3D_NO_PRIMITIVE_RESTART))
2175 {
2176 if (gl_info->supported[ARB_ES3_COMPATIBILITY])
2177 {
2178 gl_info->gl_ops.gl.p_glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
2179 checkGLcall("enable GL_PRIMITIVE_RESTART_FIXED_INDEX");
2180 }
2181 else
2182 {
2183 FIXME("OpenGL implementation does not support GL_PRIMITIVE_RESTART_FIXED_INDEX.\n");
2184 }
2185 }
2186 if (!(d3d_info->wined3d_creation_flags & WINED3D_LEGACY_CUBEMAP_FILTERING)
2187 && gl_info->supported[ARB_SEAMLESS_CUBE_MAP])
2188 {
2189 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
2190 checkGLcall("enable seamless cube map filtering");
2191 }
2192 if (gl_info->supported[ARB_CLIP_CONTROL])
2193 GL_EXTCALL(glPointParameteri(GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT));
2194 device->shader_backend->shader_init_context_state(ret);
2195 ret->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
2196 | (1u << WINED3D_SHADER_TYPE_VERTEX)
2197 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
2198 | (1u << WINED3D_SHADER_TYPE_HULL)
2199 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
2200 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
2201
2202 /* If this happens to be the first context for the device, dummy textures
2203 * are not created yet. In that case, they will be created (and bound) by
2204 * create_dummy_textures right after this context is initialized. */
2205 if (device->dummy_textures.tex_2d)
2206 context_bind_dummy_textures(device, ret);
2207
2208 TRACE("Created context %p.\n", ret);
2209
2210 return ret;
2211
2212 out:
2213 if (hdc) wined3d_release_dc(swapchain->win_handle, hdc);
2214 device->shader_backend->shader_free_context_data(ret);
2215 device->adapter->fragment_pipe->free_context_data(ret);
2216 HeapFree(GetProcessHeap(), 0, ret->texture_type);
2217 HeapFree(GetProcessHeap(), 0, ret->free_event_queries);
2218 HeapFree(GetProcessHeap(), 0, ret->free_occlusion_queries);
2219 HeapFree(GetProcessHeap(), 0, ret->free_timestamp_queries);
2220 HeapFree(GetProcessHeap(), 0, ret->fbo_key);
2221 HeapFree(GetProcessHeap(), 0, ret->draw_buffers);
2222 HeapFree(GetProcessHeap(), 0, ret->blit_targets);
2223 HeapFree(GetProcessHeap(), 0, ret);
2224 return NULL;
2225 }
2226
2227 void context_destroy(struct wined3d_device *device, struct wined3d_context *context)
2228 {
2229 BOOL destroy;
2230
2231 TRACE("Destroying ctx %p\n", context);
2232
2233 wined3d_from_cs(device->cs);
2234
2235 /* We delay destroying a context when it is active. The context_release()
2236 * function invokes context_destroy() again while leaving the last level. */
2237 if (context->level)
2238 {
2239 TRACE("Delaying destruction of context %p.\n", context);
2240 context->destroy_delayed = 1;
2241 /* FIXME: Get rid of a pointer to swapchain from wined3d_context. */
2242 context->swapchain = NULL;
2243 return;
2244 }
2245
2246 if (context->tid == GetCurrentThreadId() || !context->current)
2247 {
2248 context_destroy_gl_resources(context);
2249 TlsSetValue(wined3d_context_tls_idx, NULL);
2250 destroy = TRUE;
2251 }
2252 else
2253 {
2254 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2255 in wined3d_adapter may go away in the meantime */
2256 struct wined3d_gl_info *gl_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info));
2257 *gl_info = *context->gl_info;
2258 context->gl_info = gl_info;
2259 context->destroyed = 1;
2260 destroy = FALSE;
2261 }
2262
2263 device->shader_backend->shader_free_context_data(context);
2264 device->adapter->fragment_pipe->free_context_data(context);
2265 HeapFree(GetProcessHeap(), 0, context->texture_type);
2266 HeapFree(GetProcessHeap(), 0, context->fbo_key);
2267 HeapFree(GetProcessHeap(), 0, context->draw_buffers);
2268 HeapFree(GetProcessHeap(), 0, context->blit_targets);
2269 device_context_remove(device, context);
2270 if (destroy) HeapFree(GetProcessHeap(), 0, context);
2271 }
2272
2273 const DWORD *context_get_tex_unit_mapping(const struct wined3d_context *context,
2274 const struct wined3d_shader_version *shader_version, unsigned int *base, unsigned int *count)
2275 {
2276 const struct wined3d_gl_info *gl_info = context->gl_info;
2277
2278 if (!shader_version)
2279 {
2280 *base = 0;
2281 *count = MAX_TEXTURES;
2282 return context->tex_unit_map;
2283 }
2284
2285 if (shader_version->major >= 4)
2286 {
2287 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, shader_version->type, base, count);
2288 return NULL;
2289 }
2290
2291 switch (shader_version->type)
2292 {
2293 case WINED3D_SHADER_TYPE_PIXEL:
2294 *base = 0;
2295 *count = MAX_FRAGMENT_SAMPLERS;
2296 break;
2297 case WINED3D_SHADER_TYPE_VERTEX:
2298 *base = MAX_FRAGMENT_SAMPLERS;
2299 *count = MAX_VERTEX_SAMPLERS;
2300 break;
2301 default:
2302 ERR("Unhandled shader type %#x.\n", shader_version->type);
2303 *base = 0;
2304 *count = 0;
2305 }
2306
2307 return context->tex_unit_map;
2308 }
2309
2310 /* Context activation is done by the caller. */
2311 static void set_blit_dimension(const struct wined3d_gl_info *gl_info, UINT width, UINT height)
2312 {
2313 const GLdouble projection[] =
2314 {
2315 2.0 / width, 0.0, 0.0, 0.0,
2316 0.0, 2.0 / height, 0.0, 0.0,
2317 0.0, 0.0, 2.0, 0.0,
2318 -1.0, -1.0, -1.0, 1.0,
2319 };
2320
2321 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
2322 checkGLcall("glMatrixMode(GL_PROJECTION)");
2323 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
2324 checkGLcall("glLoadMatrixd");
2325 gl_info->gl_ops.gl.p_glViewport(0, 0, width, height);
2326 checkGLcall("glViewport");
2327 }
2328
2329 static void context_get_rt_size(const struct wined3d_context *context, SIZE *size)
2330 {
2331 const struct wined3d_texture *rt = context->current_rt.texture;
2332 unsigned int level;
2333
2334 if (rt->swapchain)
2335 {
2336 RECT window_size;
2337
2338 GetClientRect(context->win_handle, &window_size);
2339 size->cx = window_size.right - window_size.left;
2340 size->cy = window_size.bottom - window_size.top;
2341
2342 return;
2343 }
2344
2345 level = context->current_rt.sub_resource_idx % rt->level_count;
2346 size->cx = wined3d_texture_get_level_width(rt, level);
2347 size->cy = wined3d_texture_get_level_height(rt, level);
2348 }
2349
2350 /*****************************************************************************
2351 * SetupForBlit
2352 *
2353 * Sets up a context for DirectDraw blitting.
2354 * All texture units are disabled, texture unit 0 is set as current unit
2355 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
2356 * color writing enabled for all channels
2357 * register combiners disabled, shaders disabled
2358 * world matrix is set to identity, texture matrix 0 too
2359 * projection matrix is setup for drawing screen coordinates
2360 *
2361 * Params:
2362 * This: Device to activate the context for
2363 * context: Context to setup
2364 *
2365 *****************************************************************************/
2366 /* Context activation is done by the caller. */
2367 static void SetupForBlit(const struct wined3d_device *device, struct wined3d_context *context)
2368 {
2369 int i;
2370 const struct wined3d_gl_info *gl_info = context->gl_info;
2371 DWORD sampler;
2372 SIZE rt_size;
2373
2374 TRACE("Setting up context %p for blitting\n", context);
2375
2376 context_get_rt_size(context, &rt_size);
2377
2378 if (context->last_was_blit)
2379 {
2380 if (context->blit_w != rt_size.cx || context->blit_h != rt_size.cy)
2381 {
2382 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
2383 context->blit_w = rt_size.cx;
2384 context->blit_h = rt_size.cy;
2385 /* No need to dirtify here, the states are still dirtified because
2386 * they weren't applied since the last SetupForBlit() call. */
2387 }
2388 TRACE("Context is already set up for blitting, nothing to do\n");
2389 return;
2390 }
2391 context->last_was_blit = TRUE;
2392
2393 /* Disable all textures. The caller can then bind a texture it wants to blit
2394 * from
2395 *
2396 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
2397 * function texture unit. No need to care for higher samplers
2398 */
2399 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
2400 {
2401 sampler = context->rev_tex_unit_map[i];
2402 context_active_texture(context, gl_info, i);
2403
2404 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2405 {
2406 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2407 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2408 }
2409 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2410 checkGLcall("glDisable GL_TEXTURE_3D");
2411 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2412 {
2413 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2414 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2415 }
2416 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2417 checkGLcall("glDisable GL_TEXTURE_2D");
2418
2419 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2420 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
2421
2422 if (sampler != WINED3D_UNMAPPED_STAGE)
2423 {
2424 if (sampler < MAX_TEXTURES)
2425 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2426 context_invalidate_state(context, STATE_SAMPLER(sampler));
2427 }
2428 }
2429 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
2430 GL_EXTCALL(glBindSampler(0, 0));
2431 context_active_texture(context, gl_info, 0);
2432
2433 sampler = context->rev_tex_unit_map[0];
2434
2435 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2436 {
2437 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2438 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2439 }
2440 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2441 checkGLcall("glDisable GL_TEXTURE_3D");
2442 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2443 {
2444 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2445 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2446 }
2447 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2448 checkGLcall("glDisable GL_TEXTURE_2D");
2449
2450 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2451
2452 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
2453 checkGLcall("glMatrixMode(GL_TEXTURE)");
2454 gl_info->gl_ops.gl.p_glLoadIdentity();
2455 checkGLcall("glLoadIdentity()");
2456
2457 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
2458 {
2459 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
2460 GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
2461 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2462 }
2463
2464 if (sampler != WINED3D_UNMAPPED_STAGE)
2465 {
2466 if (sampler < MAX_TEXTURES)
2467 {
2468 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
2469 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2470 }
2471 context_invalidate_state(context, STATE_SAMPLER(sampler));
2472 }
2473
2474 /* Other misc states */
2475 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
2476 checkGLcall("glDisable(GL_ALPHA_TEST)");
2477 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
2478 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
2479 checkGLcall("glDisable GL_LIGHTING");
2480 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
2481 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
2482 checkGLcall("glDisable GL_DEPTH_TEST");
2483 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZENABLE));
2484 glDisableWINE(GL_FOG);
2485 checkGLcall("glDisable GL_FOG");
2486 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
2487 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2488 checkGLcall("glDisable GL_BLEND");
2489 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2490 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
2491 checkGLcall("glDisable GL_CULL_FACE");
2492 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CULLMODE));
2493 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
2494 checkGLcall("glDisable GL_STENCIL_TEST");
2495 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILENABLE));
2496 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
2497 checkGLcall("glDisable GL_SCISSOR_TEST");
2498 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2499 if (gl_info->supported[ARB_POINT_SPRITE])
2500 {
2501 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
2502 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2503 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
2504 }
2505 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
2506 checkGLcall("glColorMask");
2507 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
2508 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
2509 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
2510 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
2511 if (gl_info->supported[EXT_SECONDARY_COLOR])
2512 {
2513 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
2514 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
2515 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2516 }
2517
2518 /* Setup transforms */
2519 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
2520 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2521 gl_info->gl_ops.gl.p_glLoadIdentity();
2522 checkGLcall("glLoadIdentity()");
2523 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2524
2525 context->last_was_rhw = TRUE;
2526 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
2527
2528 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
2529 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
2530 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
2531 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
2532 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
2533 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
2534 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
2535
2536 /* FIXME: Make draw_textured_quad() able to work with a upper left origin. */
2537 if (gl_info->supported[ARB_CLIP_CONTROL])
2538 GL_EXTCALL(glClipControl(GL_LOWER_LEFT, GL_NEGATIVE_ONE_TO_ONE));
2539
2540 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
2541
2542 /* Disable shaders */
2543 device->shader_backend->shader_disable(device->shader_priv, context);
2544
2545 context->blit_w = rt_size.cx;
2546 context->blit_h = rt_size.cy;
2547 context_invalidate_state(context, STATE_VIEWPORT);
2548 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2549 }
2550
2551 static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2552 {
2553 return rt_mask & (1u << 31);
2554 }
2555
2556 static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2557 {
2558 return rt_mask & ~(1u << 31);
2559 }
2560
2561 /* Context activation is done by the caller. */
2562 static void context_apply_draw_buffers(struct wined3d_context *context, DWORD rt_mask)
2563 {
2564 const struct wined3d_gl_info *gl_info = context->gl_info;
2565
2566 if (!rt_mask)
2567 {
2568 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2569 checkGLcall("glDrawBuffer()");
2570 }
2571 else if (is_rt_mask_onscreen(rt_mask))
2572 {
2573 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2574 checkGLcall("glDrawBuffer()");
2575 }
2576 else
2577 {
2578 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2579 {
2580 unsigned int i = 0;
2581
2582 while (rt_mask)
2583 {
2584 if (rt_mask & 1)
2585 context->draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2586 else
2587 context->draw_buffers[i] = GL_NONE;
2588
2589 rt_mask >>= 1;
2590 ++i;
2591 }
2592
2593 if (gl_info->supported[ARB_DRAW_BUFFERS])
2594 {
2595 GL_EXTCALL(glDrawBuffers(i, context->draw_buffers));
2596 checkGLcall("glDrawBuffers()");
2597 }
2598 else
2599 {
2600 gl_info->gl_ops.gl.p_glDrawBuffer(context->draw_buffers[0]);
2601 checkGLcall("glDrawBuffer()");
2602 }
2603 }
2604 else
2605 {
2606 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2607 }
2608 }
2609 }
2610
2611 /* Context activation is done by the caller. */
2612 void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
2613 {
2614 const struct wined3d_gl_info *gl_info = context->gl_info;
2615 DWORD *current_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2616 DWORD new_mask = context_generate_rt_mask(buffer);
2617
2618 if (new_mask == *current_mask)
2619 return;
2620
2621 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2622 checkGLcall("glDrawBuffer()");
2623
2624 *current_mask = new_mask;
2625 }
2626
2627 /* Context activation is done by the caller. */
2628 void context_active_texture(struct wined3d_context *context, const struct wined3d_gl_info *gl_info, unsigned int unit)
2629 {
2630 GL_EXTCALL(glActiveTexture(GL_TEXTURE0 + unit));
2631 checkGLcall("glActiveTexture");
2632 context->active_texture = unit;
2633 }
2634
2635 void context_bind_bo(struct wined3d_context *context, GLenum binding, GLuint name)
2636 {
2637 const struct wined3d_gl_info *gl_info = context->gl_info;
2638
2639 if (binding == GL_ELEMENT_ARRAY_BUFFER)
2640 context_invalidate_state(context, STATE_INDEXBUFFER);
2641
2642 GL_EXTCALL(glBindBuffer(binding, name));
2643 }
2644
2645 void context_bind_texture(struct wined3d_context *context, GLenum target, GLuint name)
2646 {
2647 const struct wined3d_gl_info *gl_info = context->gl_info;
2648 DWORD unit = context->active_texture;
2649 DWORD old_texture_type = context->texture_type[unit];
2650
2651 if (name)
2652 {
2653 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2654 checkGLcall("glBindTexture");
2655 }
2656 else
2657 {
2658 target = GL_NONE;
2659 }
2660
2661 if (old_texture_type != target)
2662 {
2663 const struct wined3d_device *device = context->device;
2664
2665 switch (old_texture_type)
2666 {
2667 case GL_NONE:
2668 /* nothing to do */
2669 break;
2670 case GL_TEXTURE_1D:
2671 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, device->dummy_textures.tex_1d);
2672 checkGLcall("glBindTexture");
2673 break;
2674 case GL_TEXTURE_1D_ARRAY:
2675 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D_ARRAY, device->dummy_textures.tex_1d_array);
2676 checkGLcall("glBindTexture");
2677 break;
2678 case GL_TEXTURE_2D:
2679 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_textures.tex_2d);
2680 checkGLcall("glBindTexture");
2681 break;
2682 case GL_TEXTURE_2D_ARRAY:
2683 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D_ARRAY, device->dummy_textures.tex_2d_array);
2684 checkGLcall("glBindTexture");
2685 break;
2686 case GL_TEXTURE_RECTANGLE_ARB:
2687 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_textures.tex_rect);
2688 checkGLcall("glBindTexture");
2689 break;
2690 case GL_TEXTURE_CUBE_MAP:
2691 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_textures.tex_cube);
2692 checkGLcall("glBindTexture");
2693 break;
2694 case GL_TEXTURE_CUBE_MAP_ARRAY:
2695 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, device->dummy_textures.tex_cube_array);
2696 checkGLcall("glBindTexture");
2697 break;
2698 case GL_TEXTURE_3D:
2699 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_textures.tex_3d);
2700 checkGLcall("glBindTexture");
2701 break;
2702 case GL_TEXTURE_BUFFER:
2703 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_BUFFER, device->dummy_textures.tex_buffer);
2704 checkGLcall("glBindTexture");
2705 break;
2706 default:
2707 ERR("Unexpected texture target %#x.\n", old_texture_type);
2708 }
2709
2710 context->texture_type[unit] = target;
2711 }
2712 }
2713
2714 void *context_map_bo_address(struct wined3d_context *context,
2715 const struct wined3d_bo_address *data, size_t size, GLenum binding, DWORD flags)
2716 {
2717 const struct wined3d_gl_info *gl_info;
2718 BYTE *memory;
2719
2720 if (!data->buffer_object)
2721 return data->addr;
2722
2723 gl_info = context->gl_info;
2724
2725 context_bind_bo(context, binding, data->buffer_object);
2726
2727 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
2728 {
2729 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
2730 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
2731 }
2732 else
2733 {
2734 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
2735 memory += (INT_PTR)data->addr;
2736 }
2737
2738 context_bind_bo(context, binding, 0);
2739 checkGLcall("Map buffer object");
2740
2741 return memory;
2742 }
2743
2744 void context_unmap_bo_address(struct wined3d_context *context,
2745 const struct wined3d_bo_address *data, GLenum binding)
2746 {
2747 const struct wined3d_gl_info *gl_info;
2748
2749 if (!data->buffer_object)
2750 return;
2751
2752 gl_info = context->gl_info;
2753
2754 context_bind_bo(context, binding, data->buffer_object);
2755 GL_EXTCALL(glUnmapBuffer(binding));
2756 context_bind_bo(context, binding, 0);
2757 checkGLcall("Unmap buffer object");
2758 }
2759
2760 static void context_set_render_offscreen(struct wined3d_context *context, BOOL offscreen)
2761 {
2762 if (context->render_offscreen == offscreen)
2763 return;
2764
2765 context_invalidate_state(context, STATE_VIEWPORT);
2766 context_invalidate_state(context, STATE_SCISSORRECT);
2767 if (!context->gl_info->supported[ARB_CLIP_CONTROL])
2768 {
2769 context_invalidate_state(context, STATE_FRONTFACE);
2770 context_invalidate_state(context, STATE_POINTSPRITECOORDORIGIN);
2771 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2772 }
2773 context_invalidate_state(context, STATE_SHADER(WINED3D_SHADER_TYPE_DOMAIN));
2774 if (context->gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2775 context_invalidate_state(context, STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL));
2776 context->render_offscreen = offscreen;
2777 }
2778
2779 static BOOL match_depth_stencil_format(const struct wined3d_format *existing,
2780 const struct wined3d_format *required)
2781 {
2782 if (existing == required)
2783 return TRUE;
2784 if ((existing->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FLOAT)
2785 != (required->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FLOAT))
2786 return FALSE;
2787 if (existing->depth_size < required->depth_size)
2788 return FALSE;
2789 /* If stencil bits are used the exact amount is required - otherwise
2790 * wrapping won't work correctly. */
2791 if (required->stencil_size && required->stencil_size != existing->stencil_size)
2792 return FALSE;
2793 return TRUE;
2794 }
2795
2796 /* Context activation is done by the caller. */
2797 static void context_validate_onscreen_formats(struct wined3d_context *context,
2798 const struct wined3d_rendertarget_view *depth_stencil)
2799 {
2800 /* Onscreen surfaces are always in a swapchain */
2801 struct wined3d_swapchain *swapchain = context->current_rt.texture->swapchain;
2802
2803 if (context->render_offscreen || !depth_stencil) return;
2804 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->format)) return;
2805
2806 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2807 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2808 * format. */
2809 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2810
2811 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2812 if (!(wined3d_texture_load_location(context->current_rt.texture, context->current_rt.sub_resource_idx,
2813 context, WINED3D_LOCATION_TEXTURE_RGB)))
2814 ERR("Failed to load location.\n");
2815 swapchain->render_to_fbo = TRUE;
2816 swapchain_update_draw_bindings(swapchain);
2817 context_set_render_offscreen(context, TRUE);
2818 }
2819
2820 GLenum context_get_offscreen_gl_buffer(const struct wined3d_context *context)
2821 {
2822 switch (wined3d_settings.offscreen_rendering_mode)
2823 {
2824 case ORM_FBO:
2825 return GL_COLOR_ATTACHMENT0;
2826
2827 case ORM_BACKBUFFER:
2828 return context->aux_buffers > 0 ? GL_AUX0 : GL_BACK;
2829
2830 default:
2831 FIXME("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
2832 return GL_BACK;
2833 }
2834 }
2835
2836 static DWORD context_generate_rt_mask_no_fbo(const struct wined3d_context *context, struct wined3d_texture *rt)
2837 {
2838 if (!rt || rt->resource.format->id == WINED3DFMT_NULL)
2839 return 0;
2840 else if (rt->swapchain)
2841 return context_generate_rt_mask_from_resource(&rt->resource);
2842 else
2843 return context_generate_rt_mask(context_get_offscreen_gl_buffer(context));
2844 }
2845
2846 /* Context activation is done by the caller. */
2847 void context_apply_blit_state(struct wined3d_context *context, const struct wined3d_device *device)
2848 {
2849 struct wined3d_texture *rt = context->current_rt.texture;
2850 struct wined3d_surface *surface;
2851 DWORD rt_mask, *cur_mask;
2852
2853 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2854 {
2855 context_validate_onscreen_formats(context, NULL);
2856
2857 if (context->render_offscreen)
2858 {
2859 wined3d_texture_load(rt, context, FALSE);
2860
2861 surface = rt->sub_resources[context->current_rt.sub_resource_idx].u.surface;
2862 context_apply_fbo_state_blit(context, GL_FRAMEBUFFER, surface, NULL, rt->resource.draw_binding);
2863 if (rt->resource.format->id != WINED3DFMT_NULL)
2864 rt_mask = 1;
2865 else
2866 rt_mask = 0;
2867 }
2868 else
2869 {
2870 context->current_fbo = NULL;
2871 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
2872 rt_mask = context_generate_rt_mask_from_resource(&rt->resource);
2873 }
2874 }
2875 else
2876 {
2877 rt_mask = context_generate_rt_mask_no_fbo(context, rt);
2878 }
2879
2880 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2881
2882 if (rt_mask != *cur_mask)
2883 {
2884 context_apply_draw_buffers(context, rt_mask);
2885 *cur_mask = rt_mask;
2886 }
2887
2888 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2889 {
2890 context_check_fbo_status(context, GL_FRAMEBUFFER);
2891 }
2892
2893 SetupForBlit(device, context);
2894 context_invalidate_state(context, STATE_FRAMEBUFFER);
2895 }
2896
2897 static BOOL context_validate_rt_config(UINT rt_count, struct wined3d_rendertarget_view * const *rts,
2898 const struct wined3d_rendertarget_view *ds)
2899 {
2900 unsigned int i;
2901
2902 if (ds) return TRUE;
2903
2904 for (i = 0; i < rt_count; ++i)
2905 {
2906 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
2907 return TRUE;
2908 }
2909
2910 WARN("Invalid render target config, need at least one attachment.\n");
2911 return FALSE;
2912 }
2913
2914 /* Context activation is done by the caller. */
2915 BOOL context_apply_clear_state(struct wined3d_context *context, const struct wined3d_state *state,
2916 UINT rt_count, const struct wined3d_fb_state *fb)
2917 {
2918 struct wined3d_rendertarget_view **rts = fb->render_targets;
2919 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
2920 const struct wined3d_gl_info *gl_info = context->gl_info;
2921 DWORD rt_mask = 0, *cur_mask;
2922 unsigned int i;
2923
2924 if (isStateDirty(context, STATE_FRAMEBUFFER) || fb != state->fb
2925 || rt_count != gl_info->limits.buffers)
2926 {
2927 if (!context_validate_rt_config(rt_count, rts, dsv))
2928 return FALSE;
2929
2930 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2931 {
2932 context_validate_onscreen_formats(context, dsv);
2933
2934 if (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource))
2935 {
2936 memset(context->blit_targets, 0, gl_info->limits.buffers * sizeof(*context->blit_targets));
2937 for (i = 0; i < rt_count; ++i)
2938 {
2939 if (rts[i])
2940 {
2941 context->blit_targets[i].gl_view = rts[i]->gl_view;
2942 context->blit_targets[i].resource = rts[i]->resource;
2943 context->blit_targets[i].sub_resource_idx = rts[i]->sub_resource_idx;
2944 context->blit_targets[i].layer_count = rts[i]->layer_count;
2945 }
2946 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
2947 rt_mask |= (1u << i);
2948 }
2949 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets,
2950 wined3d_rendertarget_view_get_surface(dsv),
2951 rt_count ? rts[0]->resource->draw_binding : 0,
2952 dsv ? dsv->resource->draw_binding : 0);
2953 }
2954 else
2955 {
2956 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL,
2957 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
2958 rt_mask = context_generate_rt_mask_from_resource(rts[0]->resource);
2959 }
2960
2961 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2962 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2963 * state management allows this */
2964 context_invalidate_state(context, STATE_FRAMEBUFFER);
2965 }
2966 else
2967 {
2968 rt_mask = context_generate_rt_mask_no_fbo(context,
2969 rt_count ? wined3d_rendertarget_view_get_surface(rts[0])->container : NULL);
2970 }
2971 }
2972 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2973 && (!rt_count || wined3d_resource_is_offscreen(rts[0]->resource)))
2974 {
2975 for (i = 0; i < rt_count; ++i)
2976 {
2977 if (rts[i] && rts[i]->format->id != WINED3DFMT_NULL)
2978 rt_mask |= (1u << i);
2979 }
2980 }
2981 else
2982 {
2983 rt_mask = context_generate_rt_mask_no_fbo(context,
2984 rt_count ? wined3d_rendertarget_view_get_surface(rts[0])->container : NULL);
2985 }
2986
2987 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2988
2989 if (rt_mask != *cur_mask)
2990 {
2991 context_apply_draw_buffers(context, rt_mask);
2992 *cur_mask = rt_mask;
2993 context_invalidate_state(context, STATE_FRAMEBUFFER);
2994 }
2995
2996 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2997 {
2998 context_check_fbo_status(context, GL_FRAMEBUFFER);
2999 }
3000
3001 context->last_was_blit = FALSE;
3002
3003 /* Blending and clearing should be orthogonal, but tests on the nvidia
3004 * driver show that disabling blending when clearing improves the clearing
3005 * performance incredibly. */
3006 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
3007 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
3008 if (rt_count && gl_info->supported[ARB_FRAMEBUFFER_SRGB])
3009 {
3010 if (needs_srgb_write(context, state, fb))
3011 gl_info->gl_ops.gl.p_glEnable(GL_FRAMEBUFFER_SRGB);
3012 else
3013 gl_info->gl_ops.gl.p_glDisable(GL_FRAMEBUFFER_SRGB);
3014 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3015 }
3016 checkGLcall("setting up state for clear");
3017
3018 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
3019 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
3020 context_invalidate_state(context, STATE_SCISSORRECT);
3021
3022 return TRUE;
3023 }
3024
3025 static DWORD find_draw_buffers_mask(const struct wined3d_context *context, const struct wined3d_state *state)
3026 {
3027 struct wined3d_rendertarget_view **rts = state->fb->render_targets;
3028 struct wined3d_shader *ps = state->shader[WINED3D_SHADER_TYPE_PIXEL];
3029 DWORD rt_mask, rt_mask_bits;
3030 unsigned int i;
3031
3032 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3033 return context_generate_rt_mask_no_fbo(context, wined3d_rendertarget_view_get_surface(rts[0])->container);
3034 else if (!context->render_offscreen)
3035 return context_generate_rt_mask_from_resource(rts[0]->resource);
3036
3037 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
3038 rt_mask &= context->d3d_info->valid_rt_mask;
3039 rt_mask_bits = rt_mask;
3040 i = 0;
3041 while (rt_mask_bits)
3042 {
3043 rt_mask_bits &= ~(1u << i);
3044 if (!rts[i] || rts[i]->format->id == WINED3DFMT_NULL)
3045 rt_mask &= ~(1u << i);
3046
3047 i++;
3048 }
3049
3050 return rt_mask;
3051 }
3052
3053 /* Context activation is done by the caller. */
3054 void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3055 {
3056 DWORD rt_mask = find_draw_buffers_mask(context, state);
3057 const struct wined3d_fb_state *fb = state->fb;
3058 DWORD *cur_mask;
3059
3060 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3061 {
3062 if (!context->render_offscreen)
3063 {
3064 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL,
3065 WINED3D_LOCATION_DRAWABLE, WINED3D_LOCATION_DRAWABLE);
3066 }
3067 else
3068 {
3069 unsigned int i;
3070
3071 memset(context->blit_targets, 0, context->gl_info->limits.buffers * sizeof (*context->blit_targets));
3072 for (i = 0; i < context->gl_info->limits.buffers; ++i)
3073 {
3074 if (fb->render_targets[i])
3075 {
3076 context->blit_targets[i].gl_view = fb->render_targets[i]->gl_view;
3077 context->blit_targets[i].resource = fb->render_targets[i]->resource;
3078 context->blit_targets[i].sub_resource_idx = fb->render_targets[i]->sub_resource_idx;
3079 context->blit_targets[i].layer_count = fb->render_targets[i]->layer_count;
3080 }
3081 }
3082 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets,
3083 wined3d_rendertarget_view_get_surface(fb->depth_stencil),
3084 fb->render_targets[0] ? fb->render_targets[0]->resource->draw_binding : 0,
3085 fb->depth_stencil ? fb->depth_stencil->resource->draw_binding : 0);
3086 }
3087 }
3088
3089 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
3090 if (rt_mask != *cur_mask)
3091 {
3092 context_apply_draw_buffers(context, rt_mask);
3093 *cur_mask = rt_mask;
3094 }
3095 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
3096 }
3097
3098 static void context_map_stage(struct wined3d_context *context, DWORD stage, DWORD unit)
3099 {
3100 DWORD i = context->rev_tex_unit_map[unit];
3101 DWORD j = context->tex_unit_map[stage];
3102
3103 TRACE("Mapping stage %u to unit %u.\n", stage, unit);
3104 context->tex_unit_map[stage] = unit;
3105 if (i != WINED3D_UNMAPPED_STAGE && i != stage)
3106 context->tex_unit_map[i] = WINED3D_UNMAPPED_STAGE;
3107
3108 context->rev_tex_unit_map[unit] = stage;
3109 if (j != WINED3D_UNMAPPED_STAGE && j != unit)
3110 context->rev_tex_unit_map[j] = WINED3D_UNMAPPED_STAGE;
3111 }
3112
3113 static void context_invalidate_texture_stage(struct wined3d_context *context, DWORD stage)
3114 {
3115 DWORD i;
3116
3117 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
3118 context_invalidate_state(context, STATE_TEXTURESTAGE(stage, i));
3119 }
3120
3121 static void context_update_fixed_function_usage_map(struct wined3d_context *context,
3122 const struct wined3d_state *state)
3123 {
3124 UINT i, start, end;
3125
3126 context->fixed_function_usage_map = 0;
3127 for (i = 0; i < MAX_TEXTURES; ++i)
3128 {
3129 enum wined3d_texture_op color_op = state->texture_states[i][WINED3D_TSS_COLOR_OP];
3130 enum wined3d_texture_op alpha_op = state->texture_states[i][WINED3D_TSS_ALPHA_OP];
3131 DWORD color_arg1 = state->texture_states[i][WINED3D_TSS_COLOR_ARG1] & WINED3DTA_SELECTMASK;
3132 DWORD color_arg2 = state->texture_states[i][WINED3D_TSS_COLOR_ARG2] & WINED3DTA_SELECTMASK;
3133 DWORD color_arg3 = state->texture_states[i][WINED3D_TSS_COLOR_ARG0] & WINED3DTA_SELECTMASK;
3134 DWORD alpha_arg1 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG1] & WINED3DTA_SELECTMASK;
3135 DWORD alpha_arg2 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG2] & WINED3DTA_SELECTMASK;
3136 DWORD alpha_arg3 = state->texture_states[i][WINED3D_TSS_ALPHA_ARG0] & WINED3DTA_SELECTMASK;
3137
3138 /* Not used, and disable higher stages. */
3139 if (color_op == WINED3D_TOP_DISABLE)
3140 break;
3141
3142 if (((color_arg1 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG2)
3143 || ((color_arg2 == WINED3DTA_TEXTURE) && color_op != WINED3D_TOP_SELECT_ARG1)
3144 || ((color_arg3 == WINED3DTA_TEXTURE)
3145 && (color_op == WINED3D_TOP_MULTIPLY_ADD || color_op == WINED3D_TOP_LERP))
3146 || ((alpha_arg1 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG2)
3147 || ((alpha_arg2 == WINED3DTA_TEXTURE) && alpha_op != WINED3D_TOP_SELECT_ARG1)
3148 || ((alpha_arg3 == WINED3DTA_TEXTURE)
3149 && (alpha_op == WINED3D_TOP_MULTIPLY_ADD || alpha_op == WINED3D_TOP_LERP)))
3150 context->fixed_function_usage_map |= (1u << i);
3151
3152 if ((color_op == WINED3D_TOP_BUMPENVMAP || color_op == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
3153 && i < MAX_TEXTURES - 1)
3154 context->fixed_function_usage_map |= (1u << (i + 1));
3155 }
3156
3157 if (i < context->lowest_disabled_stage)
3158 {
3159 start = i;
3160 end = context->lowest_disabled_stage;
3161 }
3162 else
3163 {
3164 start = context->lowest_disabled_stage;
3165 end = i;
3166 }
3167
3168 context->lowest_disabled_stage = i;
3169 for (i = start + 1; i < end; ++i)
3170 {
3171 context_invalidate_state(context, STATE_TEXTURESTAGE(i, WINED3D_TSS_COLOR_OP));
3172 }
3173 }
3174
3175 static void context_map_fixed_function_samplers(struct wined3d_context *context,
3176 const struct wined3d_state *state)
3177 {
3178 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3179 unsigned int i, tex;
3180 WORD ffu_map;
3181
3182 ffu_map = context->fixed_function_usage_map;
3183
3184 if (d3d_info->limits.ffp_textures == d3d_info->limits.ffp_blend_stages
3185 || context->lowest_disabled_stage <= d3d_info->limits.ffp_textures)
3186 {
3187 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3188 {
3189 if (!(ffu_map & 1))
3190 continue;
3191
3192 if (context->tex_unit_map[i] != i)
3193 {
3194 context_map_stage(context, i, i);
3195 context_invalidate_state(context, STATE_SAMPLER(i));
3196 context_invalidate_texture_stage(context, i);
3197 }
3198 }
3199 return;
3200 }
3201
3202 /* Now work out the mapping */
3203 tex = 0;
3204 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3205 {
3206 if (!(ffu_map & 1))
3207 continue;
3208
3209 if (context->tex_unit_map[i] != tex)
3210 {
3211 context_map_stage(context, i, tex);
3212 context_invalidate_state(context, STATE_SAMPLER(i));
3213 context_invalidate_texture_stage(context, i);
3214 }
3215
3216 ++tex;
3217 }
3218 }
3219
3220 static void context_map_psamplers(struct wined3d_context *context, const struct wined3d_state *state)
3221 {
3222 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3223 const struct wined3d_shader_resource_info *resource_info =
3224 state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3225 unsigned int i;
3226
3227 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3228 {
3229 if (resource_info[i].type && context->tex_unit_map[i] != i)
3230 {
3231 context_map_stage(context, i, i);
3232 context_invalidate_state(context, STATE_SAMPLER(i));
3233 if (i < d3d_info->limits.ffp_blend_stages)
3234 context_invalidate_texture_stage(context, i);
3235 }
3236 }
3237 }
3238
3239 static BOOL context_unit_free_for_vs(const struct wined3d_context *context,
3240 const struct wined3d_shader_resource_info *ps_resource_info, DWORD unit)
3241 {
3242 DWORD current_mapping = context->rev_tex_unit_map[unit];
3243
3244 /* Not currently used */
3245 if (current_mapping == WINED3D_UNMAPPED_STAGE)
3246 return TRUE;
3247
3248 if (current_mapping < MAX_FRAGMENT_SAMPLERS)
3249 {
3250 /* Used by a fragment sampler */
3251
3252 if (!ps_resource_info)
3253 {
3254 /* No pixel shader, check fixed function */
3255 return current_mapping >= MAX_TEXTURES || !(context->fixed_function_usage_map & (1u << current_mapping));
3256 }
3257
3258 /* Pixel shader, check the shader's sampler map */
3259 return !ps_resource_info[current_mapping].type;
3260 }
3261
3262 return TRUE;
3263 }
3264
3265 static void context_map_vsamplers(struct wined3d_context *context, BOOL ps, const struct wined3d_state *state)
3266 {
3267 const struct wined3d_shader_resource_info *vs_resource_info =
3268 state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info;
3269 const struct wined3d_shader_resource_info *ps_resource_info = NULL;
3270 const struct wined3d_gl_info *gl_info = context->gl_info;
3271 int start = min(MAX_COMBINED_SAMPLERS, gl_info->limits.graphics_samplers) - 1;
3272 int i;
3273
3274 /* Note that we only care if a resource is used or not, not the
3275 * resource's specific type. Otherwise we'd need to call
3276 * shader_update_samplers() here for 1.x pixelshaders. */
3277 if (ps)
3278 ps_resource_info = state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info;
3279
3280 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
3281 {
3282 DWORD vsampler_idx = i + MAX_FRAGMENT_SAMPLERS;
3283 if (vs_resource_info[i].type)
3284 {
3285 while (start >= 0)
3286 {
3287 if (context_unit_free_for_vs(context, ps_resource_info, start))
3288 {
3289 if (context->tex_unit_map[vsampler_idx] != start)
3290 {
3291 context_map_stage(context, vsampler_idx, start);
3292 context_invalidate_state(context, STATE_SAMPLER(vsampler_idx));
3293 }
3294
3295 --start;
3296 break;
3297 }
3298
3299 --start;
3300 }
3301 if (context->tex_unit_map[vsampler_idx] == WINED3D_UNMAPPED_STAGE)
3302 WARN("Couldn't find a free texture unit for vertex sampler %u.\n", i);
3303 }
3304 }
3305 }
3306
3307 static void context_update_tex_unit_map(struct wined3d_context *context, const struct wined3d_state *state)
3308 {
3309 const struct wined3d_gl_info *gl_info = context->gl_info;
3310 BOOL vs = use_vs(state);
3311 BOOL ps = use_ps(state);
3312
3313 if (!ps)
3314 context_update_fixed_function_usage_map(context, state);
3315
3316 /* Try to go for a 1:1 mapping of the samplers when possible. Pixel shaders
3317 * need a 1:1 map at the moment.
3318 * When the mapping of a stage is changed, sampler and ALL texture stage
3319 * states have to be reset. */
3320
3321 if (gl_info->limits.graphics_samplers >= MAX_COMBINED_SAMPLERS)
3322 return;
3323
3324 if (ps)
3325 context_map_psamplers(context, state);
3326 else
3327 context_map_fixed_function_samplers(context, state);
3328
3329 if (vs)
3330 context_map_vsamplers(context, ps, state);
3331 }
3332
3333 /* Context activation is done by the caller. */
3334 void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
3335 {
3336 DWORD rt_mask, *cur_mask;
3337
3338 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
3339
3340 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
3341 rt_mask = find_draw_buffers_mask(context, state);
3342 if (rt_mask != *cur_mask)
3343 {
3344 context_apply_draw_buffers(context, rt_mask);
3345 *cur_mask = rt_mask;
3346 }
3347 }
3348
3349 static BOOL fixed_get_input(BYTE usage, BYTE usage_idx, unsigned int *regnum)
3350 {
3351 if ((usage == WINED3D_DECL_USAGE_POSITION || usage == WINED3D_DECL_USAGE_POSITIONT) && !usage_idx)
3352 *regnum = WINED3D_FFP_POSITION;
3353 else if (usage == WINED3D_DECL_USAGE_BLEND_WEIGHT && !usage_idx)
3354 *regnum = WINED3D_FFP_BLENDWEIGHT;
3355 else if (usage == WINED3D_DECL_USAGE_BLEND_INDICES && !usage_idx)
3356 *regnum = WINED3D_FFP_BLENDINDICES;
3357 else if (usage == WINED3D_DECL_USAGE_NORMAL && !usage_idx)
3358 *regnum = WINED3D_FFP_NORMAL;
3359 else if (usage == WINED3D_DECL_USAGE_PSIZE && !usage_idx)
3360 *regnum = WINED3D_FFP_PSIZE;
3361 else if (usage == WINED3D_DECL_USAGE_COLOR && !usage_idx)
3362 *regnum = WINED3D_FFP_DIFFUSE;
3363 else if (usage == WINED3D_DECL_USAGE_COLOR && usage_idx == 1)
3364 *regnum = WINED3D_FFP_SPECULAR;
3365 else if (usage == WINED3D_DECL_USAGE_TEXCOORD && usage_idx < WINED3DDP_MAXTEXCOORD)
3366 *regnum = WINED3D_FFP_TEXCOORD0 + usage_idx;
3367 else
3368 {
3369 WARN("Unsupported input stream [usage=%s, usage_idx=%u].\n", debug_d3ddeclusage(usage), usage_idx);
3370 *regnum = ~0u;
3371 return FALSE;
3372 }
3373
3374 return TRUE;
3375 }
3376
3377 /* Context activation is done by the caller. */
3378 void wined3d_stream_info_from_declaration(struct wined3d_stream_info *stream_info,
3379 const struct wined3d_state *state, const struct wined3d_gl_info *gl_info,
3380 const struct wined3d_d3d_info *d3d_info)
3381 {
3382 /* We need to deal with frequency data! */
3383 struct wined3d_vertex_declaration *declaration = state->vertex_declaration;
3384 BOOL generic_attributes = d3d_info->ffp_generic_attributes;
3385 BOOL use_vshader = use_vs(state);
3386 unsigned int i;
3387
3388 stream_info->use_map = 0;
3389 stream_info->swizzle_map = 0;
3390 stream_info->position_transformed = 0;
3391
3392 if (!declaration)
3393 return;
3394
3395 stream_info->position_transformed = declaration->position_transformed;
3396
3397 /* Translate the declaration into strided data. */
3398 for (i = 0; i < declaration->element_count; ++i)
3399 {
3400 const struct wined3d_vertex_declaration_element *element = &declaration->elements[i];
3401 const struct wined3d_stream_state *stream = &state->streams[element->input_slot];
3402 BOOL stride_used;
3403 unsigned int idx;
3404
3405 TRACE("%p Element %p (%u of %u).\n", declaration->elements,
3406 element, i + 1, declaration->element_count);
3407
3408 if (!stream->buffer)
3409 continue;
3410
3411 TRACE("offset %u input_slot %u usage_idx %d.\n", element->offset, element->input_slot, element->usage_idx);
3412
3413 if (use_vshader)
3414 {
3415 if (element->output_slot == WINED3D_OUTPUT_SLOT_UNUSED)
3416 {
3417 stride_used = FALSE;
3418 }
3419 else if (element->output_slot == WINED3D_OUTPUT_SLOT_SEMANTIC)
3420 {
3421 /* TODO: Assuming vertexdeclarations are usually used with the
3422 * same or a similar shader, it might be worth it to store the
3423 * last used output slot and try that one first. */
3424 stride_used = vshader_get_input(state->shader[WINED3D_SHADER_TYPE_VERTEX],
3425 element->usage, element->usage_idx, &idx);
3426 }
3427 else
3428 {
3429 idx = element->output_slot;
3430 stride_used = TRUE;
3431 }
3432 }
3433 else
3434 {
3435 if (!generic_attributes && !element->ffp_valid)
3436 {
3437 WARN("Skipping unsupported fixed function element of format %s and usage %s.\n",
3438 debug_d3dformat(element->format->id), debug_d3ddeclusage(element->usage));
3439 stride_used = FALSE;
3440 }
3441 else
3442 {
3443 stride_used = fixed_get_input(element->usage, element->usage_idx, &idx);
3444 }
3445 }
3446
3447 if (stride_used)
3448 {
3449 TRACE("Load %s array %u [usage %s, usage_idx %u, "
3450 "input_slot %u, offset %u, stride %u, format %s, class %s, step_rate %u].\n",
3451 use_vshader ? "shader": "fixed function", idx,
3452 debug_d3ddeclusage(element->usage), element->usage_idx, element->input_slot,
3453 element->offset, stream->stride, debug_d3dformat(element->format->id),
3454 debug_d3dinput_classification(element->input_slot_class), element->instance_data_step_rate);
3455
3456 stream_info->elements[idx].format = element->format;
3457 stream_info->elements[idx].data.buffer_object = 0;
3458 stream_info->elements[idx].data.addr = (BYTE *)NULL + stream->offset + element->offset;
3459 stream_info->elements[idx].stride = stream->stride;
3460 stream_info->elements[idx].stream_idx = element->input_slot;
3461 if (stream->flags & WINED3DSTREAMSOURCE_INSTANCEDATA)
3462 {
3463 stream_info->elements[idx].divisor = 1;
3464 }
3465 else if (element->input_slot_class == WINED3D_INPUT_PER_INSTANCE_DATA)
3466 {
3467 stream_info->elements[idx].divisor = element->instance_data_step_rate;
3468 if (!element->instance_data_step_rate)
3469 FIXME("Instance step rate 0 not implemented.\n");
3470 }
3471 else
3472 {
3473 stream_info->elements[idx].divisor = 0;
3474 }
3475
3476 if (!gl_info->supported[ARB_VERTEX_ARRAY_BGRA]
3477 && element->format->id == WINED3DFMT_B8G8R8A8_UNORM)
3478 {
3479 stream_info->swizzle_map |= 1u << idx;
3480 }
3481 stream_info->use_map |= 1u << idx;
3482 }
3483 }
3484 }
3485
3486 /* Context activation is done by the caller. */
3487 static void context_update_stream_info(struct wined3d_context *context, const struct wined3d_state *state)
3488 {
3489 struct wined3d_stream_info *stream_info = &context->stream_info;
3490 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
3491 const struct wined3d_gl_info *gl_info = context->gl_info;
3492 DWORD prev_all_vbo = stream_info->all_vbo;
3493 unsigned int i;
3494 WORD map;
3495
3496 wined3d_stream_info_from_declaration(stream_info, state, gl_info, d3d_info);
3497
3498 stream_info->all_vbo = 1;
3499 context->num_buffer_queries = 0;
3500 for (i = 0, map = stream_info->use_map; map; map >>= 1, ++i)
3501 {
3502 struct wined3d_stream_info_element *element;
3503 struct wined3d_bo_address data;
3504 struct wined3d_buffer *buffer;
3505
3506 if (!(map & 1))
3507 continue;
3508
3509 element = &stream_info->elements[i];
3510 buffer = state->streams[element->stream_idx].buffer;
3511
3512 /* We can't use VBOs if the base vertex index is negative. OpenGL
3513 * doesn't accept negative offsets (or rather offsets bigger than the
3514 * VBO, because the pointer is unsigned), so use system memory
3515 * sources. In most sane cases the pointer - offset will still be > 0,
3516 * otherwise it will wrap around to some big value. Hope that with the
3517 * indices the driver wraps it back internally. If not,
3518 * draw_primitive_immediate_mode() is needed, including a vertex buffer
3519 * path. */
3520 if (state->load_base_vertex_index < 0)
3521 {
3522 WARN_(d3d_perf)("load_base_vertex_index is < 0 (%d), not using VBOs.\n",
3523 state->load_base_vertex_index);
3524 element->data.buffer_object = 0;
3525 element->data.addr += (ULONG_PTR)wined3d_buffer_load_sysmem(buffer, context);
3526 if ((UINT_PTR)element->data.addr < -state->load_base_vertex_index * element->stride)
3527 FIXME("System memory vertex data load offset is negative!\n");
3528 }
3529 else
3530 {
3531 wined3d_buffer_load(buffer, context, state);
3532 wined3d_buffer_get_memory(buffer, &data, buffer->locations);
3533 element->data.buffer_object = data.buffer_object;
3534 element->data.addr += (ULONG_PTR)data.addr;
3535 }
3536
3537 if (!element->data.buffer_object)
3538 stream_info->all_vbo = 0;
3539
3540 if (buffer->query)
3541 context->buffer_queries[context->num_buffer_queries++] = buffer->query;
3542
3543 TRACE("Load array %u {%#x:%p}.\n", i, element->data.buffer_object, element->data.addr);
3544 }
3545
3546 if (prev_all_vbo != stream_info->all_vbo)
3547 context_invalidate_state(context, STATE_INDEXBUFFER);
3548
3549 context->use_immediate_mode_draw = FALSE;
3550
3551 if (stream_info->all_vbo)
3552 return;
3553
3554 if (use_vs(state))
3555 {
3556 if (state->vertex_declaration->half_float_conv_needed)
3557 {
3558 TRACE("Using immediate mode draw with vertex shaders for FLOAT16 conversion.\n");
3559 context->use_immediate_mode_draw = TRUE;
3560 }
3561 }
3562 else
3563 {
3564 WORD slow_mask = -!d3d_info->ffp_generic_attributes & (1u << WINED3D_FFP_PSIZE);
3565 slow_mask |= -(!gl_info->supported[ARB_VERTEX_ARRAY_BGRA] && !d3d_info->ffp_generic_attributes)
3566 & ((1u << WINED3D_FFP_DIFFUSE) | (1u << WINED3D_FFP_SPECULAR) | (1u << WINED3D_FFP_BLENDWEIGHT));
3567
3568 if ((stream_info->position_transformed && !d3d_info->xyzrhw)
3569 || (stream_info->use_map & slow_mask))
3570 context->use_immediate_mode_draw = TRUE;
3571 }
3572 }
3573
3574 /* Context activation is done by the caller. */
3575 static void context_preload_texture(struct wined3d_context *context,
3576 const struct wined3d_state *state, unsigned int idx)
3577 {
3578 struct wined3d_texture *texture;
3579
3580 if (!(texture = state->textures[idx]))
3581 return;
3582
3583 wined3d_texture_load(texture, context, state->sampler_states[idx][WINED3D_SAMP_SRGB_TEXTURE]);
3584 }
3585
3586 /* Context activation is done by the caller. */
3587 static void context_preload_textures(struct wined3d_context *context, const struct wined3d_state *state)
3588 {
3589 unsigned int i;
3590
3591 if (use_vs(state))
3592 {
3593 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
3594 {
3595 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.resource_info[i].type)
3596 context_preload_texture(context, state, MAX_FRAGMENT_SAMPLERS + i);
3597 }
3598 }
3599
3600 if (use_ps(state))
3601 {
3602 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
3603 {
3604 if (state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.resource_info[i].type)
3605 context_preload_texture(context, state, i);
3606 }
3607 }
3608 else
3609 {
3610 WORD ffu_map = context->fixed_function_usage_map;
3611
3612 for (i = 0; ffu_map; ffu_map >>= 1, ++i)
3613 {
3614 if (ffu_map & 1)
3615 context_preload_texture(context, state, i);
3616 }
3617 }
3618 }
3619
3620 static void context_load_shader_resources(struct wined3d_context *context, const struct wined3d_state *state,
3621 unsigned int shader_mask)
3622 {
3623 struct wined3d_shader_sampler_map_entry *entry;
3624 struct wined3d_shader_resource_view *view;
3625 struct wined3d_shader *shader;
3626 unsigned int i, j;
3627
3628 for (i = 0; i < WINED3D_SHADER_TYPE_COUNT; ++i)
3629 {
3630 if (!(shader_mask & (1u << i)))
3631 continue;
3632
3633 if (!(shader = state->shader[i]))
3634 continue;
3635
3636 for (j = 0; j < WINED3D_MAX_CBS; ++j)
3637 {
3638 if (state->cb[i][j])
3639 wined3d_buffer_load(state->cb[i][j], context, state);
3640 }
3641
3642 for (j = 0; j < shader->reg_maps.sampler_map.count; ++j)
3643 {
3644 entry = &shader->reg_maps.sampler_map.entries[j];
3645
3646 if (!(view = state->shader_resource_view[i][entry->resource_idx]))
3647 continue;
3648
3649 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3650 wined3d_buffer_load(buffer_from_resource(view->resource), context, state);
3651 else
3652 wined3d_texture_load(texture_from_resource(view->resource), context, FALSE);
3653 }
3654 }
3655 }
3656
3657 static void context_bind_shader_resources(struct wined3d_context *context,
3658 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
3659 {
3660 unsigned int bind_idx, shader_sampler_count, base, count, i;
3661 const struct wined3d_device *device = context->device;
3662 struct wined3d_shader_sampler_map_entry *entry;
3663 struct wined3d_shader_resource_view *view;
3664 const struct wined3d_shader *shader;
3665 struct wined3d_sampler *sampler;
3666 const DWORD *tex_unit_map;
3667
3668 if (!(shader = state->shader[shader_type]))
3669 return;
3670
3671 tex_unit_map = context_get_tex_unit_mapping(context,
3672 &shader->reg_maps.shader_version, &base, &count);
3673
3674 shader_sampler_count = shader->reg_maps.sampler_map.count;
3675 if (shader_sampler_count > count)
3676 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
3677 shader, shader_sampler_count, count);
3678 count = min(shader_sampler_count, count);
3679
3680 for (i = 0; i < count; ++i)
3681 {
3682 entry = &shader->reg_maps.sampler_map.entries[i];
3683 bind_idx = base + entry->bind_idx;
3684 if (tex_unit_map)
3685 bind_idx = tex_unit_map[bind_idx];
3686
3687 if (!(view = state->shader_resource_view[shader_type][entry->resource_idx]))
3688 {
3689 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
3690 continue;
3691 }
3692
3693 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
3694 sampler = device->default_sampler;
3695 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
3696 sampler = device->null_sampler;
3697 wined3d_shader_resource_view_bind(view, bind_idx, sampler, context);
3698 }
3699 }
3700
3701 static void context_load_unordered_access_resources(struct wined3d_context *context,
3702 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3703 {
3704 struct wined3d_unordered_access_view *view;
3705 struct wined3d_texture *texture;
3706 struct wined3d_buffer *buffer;
3707 unsigned int i;
3708
3709 context->uses_uavs = 0;
3710
3711 if (!shader)
3712 return;
3713
3714 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3715 {
3716 if (!(view = views[i]))
3717 continue;
3718
3719 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3720 {
3721 buffer = buffer_from_resource(view->resource);
3722 wined3d_buffer_load_location(buffer, context, WINED3D_LOCATION_BUFFER);
3723 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_BUFFER);
3724 }
3725 else
3726 {
3727 texture = texture_from_resource(view->resource);
3728 wined3d_texture_load(texture, context, FALSE);
3729 wined3d_unordered_access_view_invalidate_location(view, ~WINED3D_LOCATION_TEXTURE_RGB);
3730 }
3731
3732 context->uses_uavs = 1;
3733 }
3734 }
3735
3736 static void context_bind_unordered_access_views(struct wined3d_context *context,
3737 const struct wined3d_shader *shader, struct wined3d_unordered_access_view * const *views)
3738 {
3739 const struct wined3d_gl_info *gl_info = context->gl_info;
3740 struct wined3d_unordered_access_view *view;
3741 GLuint texture_name;
3742 unsigned int i;
3743 GLint level;
3744
3745 if (!shader)
3746 return;
3747
3748 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
3749 {
3750 if (!(view = views[i]))
3751 {
3752 if (shader->reg_maps.uav_resource_info[i].type)
3753 WARN("No unordered access view bound at index %u.\n", i);
3754 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3755 continue;
3756 }
3757
3758 if (view->gl_view.name)
3759 {
3760 texture_name = view->gl_view.name;
3761 level = 0;
3762 }
3763 else if (view->resource->type != WINED3D_RTYPE_BUFFER)
3764 {
3765 struct wined3d_texture *texture = texture_from_resource(view->resource);
3766 texture_name = wined3d_texture_get_texture_name(texture, context, FALSE);
3767 level = view->desc.u.texture.level_idx;
3768 }
3769 else
3770 {
3771 FIXME("Unsupported buffer unordered access view.\n");
3772 GL_EXTCALL(glBindImageTexture(i, 0, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R8));
3773 continue;
3774 }
3775
3776 GL_EXTCALL(glBindImageTexture(i, texture_name, level, GL_TRUE, 0, GL_READ_WRITE,
3777 view->format->glInternal));
3778
3779 if (view->counter_bo)
3780 GL_EXTCALL(glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, i, view->counter_bo));
3781 }
3782 checkGLcall("Bind unordered access views");
3783 }
3784
3785 static void context_load_stream_output_buffers(struct wined3d_context *context,
3786 const struct wined3d_state *state)
3787 {
3788 unsigned int i;
3789
3790 for (i = 0; i < ARRAY_SIZE(state->stream_output); ++i)
3791 {
3792 struct wined3d_buffer *buffer;
3793 if (!(buffer = state->stream_output[i].buffer))
3794 continue;
3795
3796 wined3d_buffer_load(buffer, context, state);
3797 wined3d_buffer_invalidate_location(buffer, ~WINED3D_LOCATION_BUFFER);
3798 }
3799 }
3800
3801 /* Context activation is done by the caller. */
3802 BOOL context_apply_draw_state(struct wined3d_context *context,
3803 const struct wined3d_device *device, const struct wined3d_state *state)
3804 {
3805 const struct StateEntry *state_table = context->state_table;
3806 const struct wined3d_gl_info *gl_info = context->gl_info;
3807 const struct wined3d_fb_state *fb = state->fb;
3808 unsigned int i;
3809 WORD map;
3810
3811 if (!context_validate_rt_config(gl_info->limits.buffers, fb->render_targets, fb->depth_stencil))
3812 return FALSE;
3813
3814 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && isStateDirty(context, STATE_FRAMEBUFFER))
3815 {
3816 context_validate_onscreen_formats(context, fb->depth_stencil);
3817 }
3818
3819 /* Preload resources before FBO setup. Texture preload in particular may
3820 * result in changes to the current FBO, due to using e.g. FBO blits for
3821 * updating a resource location. */
3822 context_update_tex_unit_map(context, state);
3823 context_preload_textures(context, state);
3824 context_load_shader_resources(context, state, ~(1u << WINED3D_SHADER_TYPE_COMPUTE));
3825 context_load_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_PIXEL],
3826 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3827 context_load_stream_output_buffers(context, state);
3828 /* TODO: Right now the dependency on the vertex shader is necessary
3829 * since wined3d_stream_info_from_declaration() depends on the reg_maps of
3830 * the current VS but maybe it's possible to relax the coupling in some
3831 * situations at least. */
3832 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC)
3833 || isStateDirty(context, STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX)))
3834 {
3835 context_update_stream_info(context, state);
3836 }
3837 else
3838 {
3839 for (i = 0, map = context->stream_info.use_map; map; map >>= 1, ++i)
3840 {
3841 if (map & 1)
3842 wined3d_buffer_load(state->streams[context->stream_info.elements[i].stream_idx].buffer,
3843 context, state);
3844 }
3845 /* Loading the buffers above may have invalidated the stream info. */
3846 if (isStateDirty(context, STATE_STREAMSRC))
3847 context_update_stream_info(context, state);
3848 }
3849 if (state->index_buffer)
3850 {
3851 if (context->stream_info.all_vbo)
3852 wined3d_buffer_load(state->index_buffer, context, state);
3853 else
3854 wined3d_buffer_load_sysmem(state->index_buffer, context);
3855 }
3856
3857 for (i = 0; i < context->numDirtyEntries; ++i)
3858 {
3859 DWORD rep = context->dirtyArray[i];
3860 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
3861 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
3862 context->isStateDirty[idx] &= ~(1u << shift);
3863 state_table[rep].apply(context, state, rep);
3864 }
3865
3866 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
3867 {
3868 device->shader_backend->shader_select(device->shader_priv, context, state);
3869 context->shader_update_mask &= 1u << WINED3D_SHADER_TYPE_COMPUTE;
3870 }
3871
3872 if (context->constant_update_mask)
3873 {
3874 device->shader_backend->shader_load_constants(device->shader_priv, context, state);
3875 context->constant_update_mask = 0;
3876 }
3877
3878 if (context->update_shader_resource_bindings)
3879 {
3880 for (i = 0; i < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++i)
3881 context_bind_shader_resources(context, state, i);
3882 context->update_shader_resource_bindings = 0;
3883 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
3884 context->update_compute_shader_resource_bindings = 1;
3885 }
3886
3887 if (context->update_unordered_access_view_bindings)
3888 {
3889 context_bind_unordered_access_views(context,
3890 state->shader[WINED3D_SHADER_TYPE_PIXEL],
3891 state->unordered_access_view[WINED3D_PIPELINE_GRAPHICS]);
3892 context->update_unordered_access_view_bindings = 0;
3893 context->update_compute_unordered_access_view_bindings = 1;
3894 }
3895
3896 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
3897 {
3898 context_check_fbo_status(context, GL_FRAMEBUFFER);
3899 }
3900
3901 context->numDirtyEntries = 0; /* This makes the whole list clean */
3902 context->last_was_blit = FALSE;
3903
3904 return TRUE;
3905 }
3906
3907 void context_apply_compute_state(struct wined3d_context *context,
3908 const struct wined3d_device *device, const struct wined3d_state *state)
3909 {
3910 const struct StateEntry *state_table = context->state_table;
3911 const struct wined3d_gl_info *gl_info = context->gl_info;
3912 unsigned int state_id, i, j;
3913
3914 context_load_shader_resources(context, state, 1u << WINED3D_SHADER_TYPE_COMPUTE);
3915 context_load_unordered_access_resources(context, state->shader[WINED3D_SHADER_TYPE_COMPUTE],
3916 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
3917
3918 for (i = 0, state_id = STATE_COMPUTE_OFFSET; i < ARRAY_SIZE(context->dirty_compute_states); ++i)
3919 {
3920 for (j = 0; j < sizeof(*context->dirty_compute_states) * CHAR_BIT; ++j, ++state_id)
3921 {
3922 if (context->dirty_compute_states[i] & (1u << j))
3923 state_table[state_id].apply(context, state, state_id);
3924 }
3925 }
3926 memset(context->dirty_compute_states, 0, sizeof(*context->dirty_compute_states));
3927
3928 if (context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
3929 {
3930 device->shader_backend->shader_select_compute(device->shader_priv, context, state);
3931 context->shader_update_mask &= ~(1u << WINED3D_SHADER_TYPE_COMPUTE);
3932 }
3933
3934 if (context->update_compute_shader_resource_bindings)
3935 {
3936 context_bind_shader_resources(context, state, WINED3D_SHADER_TYPE_COMPUTE);
3937 context->update_compute_shader_resource_bindings = 0;
3938 if (gl_info->limits.combined_samplers == gl_info->limits.graphics_samplers)
3939 context->update_shader_resource_bindings = 1;
3940 }
3941
3942 if (context->update_compute_unordered_access_view_bindings)
3943 {
3944 context_bind_unordered_access_views(context,
3945 state->shader[WINED3D_SHADER_TYPE_COMPUTE],
3946 state->unordered_access_view[WINED3D_PIPELINE_COMPUTE]);
3947 context->update_compute_unordered_access_view_bindings = 0;
3948 context->update_unordered_access_view_bindings = 1;
3949 }
3950
3951 context->last_was_blit = FALSE;
3952 }
3953
3954 void context_end_transform_feedback(struct wined3d_context *context)
3955 {
3956 const struct wined3d_gl_info *gl_info = context->gl_info;
3957 if (context->transform_feedback_active)
3958 {
3959 GL_EXTCALL(glEndTransformFeedback());
3960 checkGLcall("glEndTransformFeedback");
3961 context->transform_feedback_active = 0;
3962 context->transform_feedback_paused = 0;
3963 }
3964 }
3965
3966 static void context_setup_target(struct wined3d_context *context,
3967 struct wined3d_texture *texture, unsigned int sub_resource_idx)
3968 {
3969 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
3970
3971 render_offscreen = wined3d_resource_is_offscreen(&texture->resource);
3972 if (context->current_rt.texture == texture
3973 && context->current_rt.sub_resource_idx == sub_resource_idx
3974 && render_offscreen == old_render_offscreen)
3975 return;
3976
3977 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
3978 * the alpha blend state changes with different render target formats. */
3979 if (!context->current_rt.texture)
3980 {
3981 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
3982 }
3983 else
3984 {
3985 const struct wined3d_format *old = context->current_rt.texture->resource.format;
3986 const struct wined3d_format *new = texture->resource.format;
3987
3988 if (old->id != new->id)
3989 {
3990 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
3991 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
3992 || !(texture->resource.format_flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
3993 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
3994
3995 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
3996 if ((context->current_rt.texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE)
3997 != (texture->resource.format_flags & WINED3DFMT_FLAG_SRGB_WRITE))
3998 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
3999 }
4000
4001 /* When switching away from an offscreen render target, and we're not
4002 * using FBOs, we have to read the drawable into the texture. This is
4003 * done via PreLoad (and WINED3D_LOCATION_DRAWABLE set on the surface).
4004 * There are some things that need care though. PreLoad needs a GL context,
4005 * and FindContext is called before the context is activated. It also
4006 * has to be called with the old rendertarget active, otherwise a
4007 * wrong drawable is read. */
4008 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
4009 && old_render_offscreen && (context->current_rt.texture != texture
4010 || context->current_rt.sub_resource_idx != sub_resource_idx))
4011 {
4012 unsigned int prev_sub_resource_idx = context->current_rt.sub_resource_idx;
4013 struct wined3d_texture *prev_texture = context->current_rt.texture;
4014
4015 /* Read the back buffer of the old drawable into the destination texture. */
4016 if (prev_texture->texture_srgb.name)
4017 wined3d_texture_load(prev_texture, context, TRUE);
4018 wined3d_texture_load(prev_texture, context, FALSE);
4019 wined3d_texture_invalidate_location(prev_texture, prev_sub_resource_idx, WINED3D_LOCATION_DRAWABLE);
4020 }
4021 }
4022
4023 context->current_rt.texture = texture;
4024 context->current_rt.sub_resource_idx = sub_resource_idx;
4025 context_set_render_offscreen(context, render_offscreen);
4026 }
4027
4028 struct wined3d_context *context_acquire(const struct wined3d_device *device,
4029 struct wined3d_texture *texture, unsigned int sub_resource_idx)
4030 {
4031 struct wined3d_context *current_context = context_get_current();
4032 struct wined3d_context *context;
4033 BOOL swapchain_texture;
4034
4035 TRACE("device %p, texture %p, sub_resource_idx %u.\n", device, texture, sub_resource_idx);
4036
4037 wined3d_from_cs(device->cs);
4038
4039 if (current_context && current_context->destroyed)
4040 current_context = NULL;
4041
4042 swapchain_texture = texture && texture->swapchain;
4043 if (!texture)
4044 {
4045 if (current_context
4046 && current_context->current_rt.texture
4047 && current_context->device == device)
4048 {
4049 texture = current_context->current_rt.texture;
4050 sub_resource_idx = current_context->current_rt.sub_resource_idx;
4051 }
4052 else
4053 {
4054 struct wined3d_swapchain *swapchain = device->swapchains[0];
4055
4056 if (swapchain->back_buffers)
4057 texture = swapchain->back_buffers[0];
4058 else
4059 texture = swapchain->front_buffer;
4060 sub_resource_idx = 0;
4061 }
4062 }
4063
4064 if (current_context && current_context->current_rt.texture == texture)
4065 {
4066 context = current_context;
4067 }
4068 else if (swapchain_texture)
4069 {
4070 TRACE("Rendering onscreen.\n");
4071
4072 context = swapchain_get_context(texture->swapchain);
4073 }
4074 else
4075 {
4076 TRACE("Rendering offscreen.\n");
4077
4078 /* Stay with the current context if possible. Otherwise use the
4079 * context for the primary swapchain. */
4080 if (current_context && current_context->device == device)
4081 context = current_context;
4082 else
4083 context = swapchain_get_context(device->swapchains[0]);
4084 }
4085
4086 context_enter(context);
4087 context_update_window(context);
4088 context_setup_target(context, texture, sub_resource_idx);
4089 if (!context->valid)
4090 return context;
4091
4092 if (context != current_context)
4093 {
4094 if (!context_set_current(context))
4095 ERR("Failed to activate the new context.\n");
4096 }
4097 else if (context->needs_set)
4098 {
4099 context_set_gl_context(context);
4100 }
4101
4102 return context;
4103 }
4104
4105 struct wined3d_context *context_reacquire(const struct wined3d_device *device,
4106 struct wined3d_context *context)
4107 {
4108 struct wined3d_context *current_context;
4109
4110 if (!context || context->tid != GetCurrentThreadId())
4111 return NULL;
4112
4113 current_context = context_acquire(device, context->current_rt.texture,
4114 context->current_rt.sub_resource_idx);
4115 if (current_context != context)
4116 ERR("Acquired context %p instead of %p.\n", current_context, context);
4117 return current_context;
4118 }