2fc4ffe672842d9ac779aaa22d8b2c9807341b16
[mesa.git] / src / mesa / state_tracker / st_context.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "main/accum.h"
30 #include "main/api_exec.h"
31 #include "main/context.h"
32 #include "main/glthread.h"
33 #include "main/samplerobj.h"
34 #include "main/shaderobj.h"
35 #include "main/version.h"
36 #include "main/vtxfmt.h"
37 #include "main/hash.h"
38 #include "program/prog_cache.h"
39 #include "vbo/vbo.h"
40 #include "glapi/glapi.h"
41 #include "st_manager.h"
42 #include "st_context.h"
43 #include "st_debug.h"
44 #include "st_cb_bitmap.h"
45 #include "st_cb_blit.h"
46 #include "st_cb_bufferobjects.h"
47 #include "st_cb_clear.h"
48 #include "st_cb_compute.h"
49 #include "st_cb_condrender.h"
50 #include "st_cb_copyimage.h"
51 #include "st_cb_drawpixels.h"
52 #include "st_cb_rasterpos.h"
53 #include "st_cb_drawtex.h"
54 #include "st_cb_eglimage.h"
55 #include "st_cb_fbo.h"
56 #include "st_cb_feedback.h"
57 #include "st_cb_memoryobjects.h"
58 #include "st_cb_msaa.h"
59 #include "st_cb_perfmon.h"
60 #include "st_cb_perfquery.h"
61 #include "st_cb_program.h"
62 #include "st_cb_queryobj.h"
63 #include "st_cb_readpixels.h"
64 #include "st_cb_semaphoreobjects.h"
65 #include "st_cb_texture.h"
66 #include "st_cb_xformfb.h"
67 #include "st_cb_flush.h"
68 #include "st_cb_syncobj.h"
69 #include "st_cb_strings.h"
70 #include "st_cb_texturebarrier.h"
71 #include "st_cb_viewport.h"
72 #include "st_atom.h"
73 #include "st_draw.h"
74 #include "st_extensions.h"
75 #include "st_gen_mipmap.h"
76 #include "st_pbo.h"
77 #include "st_program.h"
78 #include "st_sampler_view.h"
79 #include "st_shader_cache.h"
80 #include "st_vdpau.h"
81 #include "st_texture.h"
82 #include "st_util.h"
83 #include "pipe/p_context.h"
84 #include "util/u_cpu_detect.h"
85 #include "util/u_inlines.h"
86 #include "util/u_upload_mgr.h"
87 #include "util/u_vbuf.h"
88 #include "util/u_memory.h"
89 #include "cso_cache/cso_context.h"
90 #include "compiler/glsl/glsl_parser_extras.h"
91
92
93 DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
94
95
96 /**
97 * Called via ctx->Driver.Enable()
98 */
99 static void
100 st_Enable(struct gl_context *ctx, GLenum cap, GLboolean state)
101 {
102 struct st_context *st = st_context(ctx);
103
104 switch (cap) {
105 case GL_DEBUG_OUTPUT:
106 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
107 st_update_debug_callback(st);
108 break;
109 case GL_BLACKHOLE_RENDER_INTEL:
110 st->pipe->set_frontend_noop(st->pipe, ctx->IntelBlackholeRender);
111 break;
112 default:
113 break;
114 }
115 }
116
117
118 /**
119 * Called via ctx->Driver.QueryMemoryInfo()
120 */
121 static void
122 st_query_memory_info(struct gl_context *ctx, struct gl_memory_info *out)
123 {
124 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
125 struct pipe_memory_info info;
126
127 assert(screen->query_memory_info);
128 if (!screen->query_memory_info)
129 return;
130
131 screen->query_memory_info(screen, &info);
132
133 out->total_device_memory = info.total_device_memory;
134 out->avail_device_memory = info.avail_device_memory;
135 out->total_staging_memory = info.total_staging_memory;
136 out->avail_staging_memory = info.avail_staging_memory;
137 out->device_memory_evicted = info.device_memory_evicted;
138 out->nr_device_memory_evictions = info.nr_device_memory_evictions;
139 }
140
141
142 static uint64_t
143 st_get_active_states(struct gl_context *ctx)
144 {
145 struct st_program *vp =
146 st_program(ctx->VertexProgram._Current);
147 struct st_program *tcp =
148 st_program(ctx->TessCtrlProgram._Current);
149 struct st_program *tep =
150 st_program(ctx->TessEvalProgram._Current);
151 struct st_program *gp =
152 st_program(ctx->GeometryProgram._Current);
153 struct st_program *fp =
154 st_program(ctx->FragmentProgram._Current);
155 struct st_program *cp =
156 st_program(ctx->ComputeProgram._Current);
157 uint64_t active_shader_states = 0;
158
159 if (vp)
160 active_shader_states |= vp->affected_states;
161 if (tcp)
162 active_shader_states |= tcp->affected_states;
163 if (tep)
164 active_shader_states |= tep->affected_states;
165 if (gp)
166 active_shader_states |= gp->affected_states;
167 if (fp)
168 active_shader_states |= fp->affected_states;
169 if (cp)
170 active_shader_states |= cp->affected_states;
171
172 /* Mark non-shader-resource shader states as "always active". */
173 return active_shader_states | ~ST_ALL_SHADER_RESOURCES;
174 }
175
176
177 void
178 st_invalidate_buffers(struct st_context *st)
179 {
180 st->dirty |= ST_NEW_BLEND |
181 ST_NEW_DSA |
182 ST_NEW_FB_STATE |
183 ST_NEW_SAMPLE_STATE |
184 ST_NEW_SAMPLE_SHADING |
185 ST_NEW_FS_STATE |
186 ST_NEW_POLY_STIPPLE |
187 ST_NEW_VIEWPORT |
188 ST_NEW_RASTERIZER |
189 ST_NEW_SCISSOR |
190 ST_NEW_WINDOW_RECTANGLES;
191 }
192
193
194 static inline bool
195 st_vp_uses_current_values(const struct gl_context *ctx)
196 {
197 const uint64_t inputs = ctx->VertexProgram._Current->info.inputs_read;
198 return _mesa_draw_current_bits(ctx) & inputs;
199 }
200
201
202 /**
203 * Called via ctx->Driver.UpdateState()
204 */
205 static void
206 st_invalidate_state(struct gl_context *ctx)
207 {
208 GLbitfield new_state = ctx->NewState;
209 struct st_context *st = st_context(ctx);
210
211 if (new_state & _NEW_BUFFERS) {
212 st_invalidate_buffers(st);
213 } else {
214 /* These set a subset of flags set by _NEW_BUFFERS, so we only have to
215 * check them when _NEW_BUFFERS isn't set.
216 */
217 if (new_state & _NEW_PROGRAM)
218 st->dirty |= ST_NEW_RASTERIZER;
219
220 if (new_state & _NEW_FOG)
221 st->dirty |= ST_NEW_FS_STATE;
222 }
223
224 if (new_state & (_NEW_LIGHT |
225 _NEW_POINT))
226 st->dirty |= ST_NEW_RASTERIZER;
227
228 if ((new_state & _NEW_LIGHT) &&
229 (st->lower_flatshade || st->lower_two_sided_color))
230 st->dirty |= ST_NEW_FS_STATE;
231
232 if (new_state & _NEW_PROJECTION &&
233 st_user_clip_planes_enabled(ctx))
234 st->dirty |= ST_NEW_CLIP_STATE;
235
236 if (new_state & _NEW_PIXEL)
237 st->dirty |= ST_NEW_PIXEL_TRANSFER;
238
239 if (new_state & _NEW_CURRENT_ATTRIB && st_vp_uses_current_values(ctx))
240 st->dirty |= ST_NEW_VERTEX_ARRAYS;
241
242 if (st->clamp_frag_depth_in_shader && (new_state & _NEW_VIEWPORT)) {
243 if (ctx->GeometryProgram._Current)
244 st->dirty |= ST_NEW_GS_CONSTANTS;
245 else if (ctx->TessEvalProgram._Current)
246 st->dirty |= ST_NEW_TES_CONSTANTS;
247 else
248 st->dirty |= ST_NEW_VS_CONSTANTS;
249 st->dirty |= ST_NEW_FS_CONSTANTS;
250 }
251
252 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
253 if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT)) {
254 st->dirty |= ST_NEW_VS_STATE;
255 if (st->ctx->API == API_OPENGL_COMPAT && ctx->Version >= 32) {
256 st->dirty |= ST_NEW_GS_STATE | ST_NEW_TES_STATE;
257 }
258 }
259
260 /* Which shaders are dirty will be determined manually. */
261 if (new_state & _NEW_PROGRAM) {
262 st->gfx_shaders_may_be_dirty = true;
263 st->compute_shader_may_be_dirty = true;
264 /* This will mask out unused shader resources. */
265 st->active_states = st_get_active_states(ctx);
266 }
267
268 if (new_state & _NEW_TEXTURE_OBJECT) {
269 st->dirty |= st->active_states &
270 (ST_NEW_SAMPLER_VIEWS |
271 ST_NEW_SAMPLERS |
272 ST_NEW_IMAGE_UNITS);
273 if (ctx->FragmentProgram._Current &&
274 ctx->FragmentProgram._Current->ExternalSamplersUsed) {
275 st->dirty |= ST_NEW_FS_STATE;
276 }
277 }
278 }
279
280
281 /*
282 * In some circumstances (such as running google-chrome) the state
283 * tracker may try to delete a resource view from a context different
284 * than when it was created. We don't want to do that.
285 *
286 * In that situation, st_texture_release_all_sampler_views() calls this
287 * function to transfer the sampler view reference to this context (expected
288 * to be the context which created the view.)
289 */
290 void
291 st_save_zombie_sampler_view(struct st_context *st,
292 struct pipe_sampler_view *view)
293 {
294 struct st_zombie_sampler_view_node *entry;
295
296 assert(view->context == st->pipe);
297
298 entry = MALLOC_STRUCT(st_zombie_sampler_view_node);
299 if (!entry)
300 return;
301
302 entry->view = view;
303
304 /* We need a mutex since this function may be called from one thread
305 * while free_zombie_resource_views() is called from another.
306 */
307 simple_mtx_lock(&st->zombie_sampler_views.mutex);
308 list_addtail(&entry->node, &st->zombie_sampler_views.list.node);
309 simple_mtx_unlock(&st->zombie_sampler_views.mutex);
310 }
311
312
313 /*
314 * Since OpenGL shaders may be shared among contexts, we can wind up
315 * with variants of a shader created with different contexts.
316 * When we go to destroy a gallium shader, we want to free it with the
317 * same context that it was created with, unless the driver reports
318 * PIPE_CAP_SHAREABLE_SHADERS = TRUE.
319 */
320 void
321 st_save_zombie_shader(struct st_context *st,
322 enum pipe_shader_type type,
323 struct pipe_shader_state *shader)
324 {
325 struct st_zombie_shader_node *entry;
326
327 /* we shouldn't be here if the driver supports shareable shaders */
328 assert(!st->has_shareable_shaders);
329
330 entry = MALLOC_STRUCT(st_zombie_shader_node);
331 if (!entry)
332 return;
333
334 entry->shader = shader;
335 entry->type = type;
336
337 /* We need a mutex since this function may be called from one thread
338 * while free_zombie_shaders() is called from another.
339 */
340 simple_mtx_lock(&st->zombie_shaders.mutex);
341 list_addtail(&entry->node, &st->zombie_shaders.list.node);
342 simple_mtx_unlock(&st->zombie_shaders.mutex);
343 }
344
345
346 /*
347 * Free any zombie sampler views that may be attached to this context.
348 */
349 static void
350 free_zombie_sampler_views(struct st_context *st)
351 {
352 struct st_zombie_sampler_view_node *entry, *next;
353
354 if (list_is_empty(&st->zombie_sampler_views.list.node)) {
355 return;
356 }
357
358 simple_mtx_lock(&st->zombie_sampler_views.mutex);
359
360 LIST_FOR_EACH_ENTRY_SAFE(entry, next,
361 &st->zombie_sampler_views.list.node, node) {
362 list_del(&entry->node); // remove this entry from the list
363
364 assert(entry->view->context == st->pipe);
365 pipe_sampler_view_reference(&entry->view, NULL);
366
367 free(entry);
368 }
369
370 assert(list_is_empty(&st->zombie_sampler_views.list.node));
371
372 simple_mtx_unlock(&st->zombie_sampler_views.mutex);
373 }
374
375
376 /*
377 * Free any zombie shaders that may be attached to this context.
378 */
379 static void
380 free_zombie_shaders(struct st_context *st)
381 {
382 struct st_zombie_shader_node *entry, *next;
383
384 if (list_is_empty(&st->zombie_shaders.list.node)) {
385 return;
386 }
387
388 simple_mtx_lock(&st->zombie_shaders.mutex);
389
390 LIST_FOR_EACH_ENTRY_SAFE(entry, next,
391 &st->zombie_shaders.list.node, node) {
392 list_del(&entry->node); // remove this entry from the list
393
394 switch (entry->type) {
395 case PIPE_SHADER_VERTEX:
396 st->pipe->bind_vs_state(st->pipe, NULL);
397 st->pipe->delete_vs_state(st->pipe, entry->shader);
398 break;
399 case PIPE_SHADER_FRAGMENT:
400 st->pipe->bind_fs_state(st->pipe, NULL);
401 st->pipe->delete_fs_state(st->pipe, entry->shader);
402 break;
403 case PIPE_SHADER_GEOMETRY:
404 st->pipe->bind_gs_state(st->pipe, NULL);
405 st->pipe->delete_gs_state(st->pipe, entry->shader);
406 break;
407 case PIPE_SHADER_TESS_CTRL:
408 st->pipe->bind_tcs_state(st->pipe, NULL);
409 st->pipe->delete_tcs_state(st->pipe, entry->shader);
410 break;
411 case PIPE_SHADER_TESS_EVAL:
412 st->pipe->bind_tes_state(st->pipe, NULL);
413 st->pipe->delete_tes_state(st->pipe, entry->shader);
414 break;
415 case PIPE_SHADER_COMPUTE:
416 st->pipe->bind_compute_state(st->pipe, NULL);
417 st->pipe->delete_compute_state(st->pipe, entry->shader);
418 break;
419 default:
420 unreachable("invalid shader type in free_zombie_shaders()");
421 }
422 free(entry);
423 }
424
425 assert(list_is_empty(&st->zombie_shaders.list.node));
426
427 simple_mtx_unlock(&st->zombie_shaders.mutex);
428 }
429
430
431 /*
432 * This function is called periodically to free any zombie objects
433 * which are attached to this context.
434 */
435 void
436 st_context_free_zombie_objects(struct st_context *st)
437 {
438 free_zombie_sampler_views(st);
439 free_zombie_shaders(st);
440 }
441
442
443 static void
444 st_destroy_context_priv(struct st_context *st, bool destroy_pipe)
445 {
446 uint i;
447
448 st_destroy_atoms(st);
449 st_destroy_draw(st);
450 st_destroy_clear(st);
451 st_destroy_bitmap(st);
452 st_destroy_drawpix(st);
453 st_destroy_drawtex(st);
454 st_destroy_perfmon(st);
455 st_destroy_pbo_helpers(st);
456 st_destroy_bound_texture_handles(st);
457 st_destroy_bound_image_handles(st);
458
459 for (i = 0; i < ARRAY_SIZE(st->state.frag_sampler_views); i++) {
460 pipe_sampler_view_reference(&st->state.vert_sampler_views[i], NULL);
461 pipe_sampler_view_reference(&st->state.frag_sampler_views[i], NULL);
462 }
463
464 /* free glReadPixels cache data */
465 st_invalidate_readpix_cache(st);
466 util_throttle_deinit(st->pipe->screen, &st->throttle);
467
468 cso_destroy_context(st->cso_context);
469
470 if (st->pipe && destroy_pipe)
471 st->pipe->destroy(st->pipe);
472
473 free(st);
474 }
475
476
477 static void
478 st_init_driver_flags(struct st_context *st)
479 {
480 struct gl_driver_flags *f = &st->ctx->DriverFlags;
481
482 f->NewArray = ST_NEW_VERTEX_ARRAYS;
483 f->NewRasterizerDiscard = ST_NEW_RASTERIZER;
484 f->NewTileRasterOrder = ST_NEW_RASTERIZER;
485 f->NewUniformBuffer = ST_NEW_UNIFORM_BUFFER;
486 f->NewDefaultTessLevels = ST_NEW_TESS_STATE;
487
488 /* Shader resources */
489 f->NewTextureBuffer = ST_NEW_SAMPLER_VIEWS;
490 if (st->has_hw_atomics)
491 f->NewAtomicBuffer = ST_NEW_HW_ATOMICS | ST_NEW_CS_ATOMICS;
492 else
493 f->NewAtomicBuffer = ST_NEW_ATOMIC_BUFFER;
494 f->NewShaderStorageBuffer = ST_NEW_STORAGE_BUFFER;
495 f->NewImageUnits = ST_NEW_IMAGE_UNITS;
496
497 f->NewShaderConstants[MESA_SHADER_VERTEX] = ST_NEW_VS_CONSTANTS;
498 f->NewShaderConstants[MESA_SHADER_TESS_CTRL] = ST_NEW_TCS_CONSTANTS;
499 f->NewShaderConstants[MESA_SHADER_TESS_EVAL] = ST_NEW_TES_CONSTANTS;
500 f->NewShaderConstants[MESA_SHADER_GEOMETRY] = ST_NEW_GS_CONSTANTS;
501 f->NewShaderConstants[MESA_SHADER_FRAGMENT] = ST_NEW_FS_CONSTANTS;
502 f->NewShaderConstants[MESA_SHADER_COMPUTE] = ST_NEW_CS_CONSTANTS;
503
504 f->NewWindowRectangles = ST_NEW_WINDOW_RECTANGLES;
505 f->NewFramebufferSRGB = ST_NEW_FB_STATE;
506 f->NewScissorRect = ST_NEW_SCISSOR;
507 f->NewScissorTest = ST_NEW_SCISSOR | ST_NEW_RASTERIZER;
508
509 if (st->lower_alpha_test)
510 f->NewAlphaTest = ST_NEW_FS_STATE;
511 else
512 f->NewAlphaTest = ST_NEW_DSA;
513
514 f->NewBlend = ST_NEW_BLEND;
515 f->NewBlendColor = ST_NEW_BLEND_COLOR;
516 f->NewColorMask = ST_NEW_BLEND;
517 f->NewDepth = ST_NEW_DSA;
518 f->NewLogicOp = ST_NEW_BLEND;
519 f->NewStencil = ST_NEW_DSA;
520 f->NewMultisampleEnable = ST_NEW_BLEND | ST_NEW_RASTERIZER |
521 ST_NEW_SAMPLE_STATE | ST_NEW_SAMPLE_SHADING;
522 f->NewSampleAlphaToXEnable = ST_NEW_BLEND;
523 f->NewSampleMask = ST_NEW_SAMPLE_STATE;
524 f->NewSampleLocations = ST_NEW_SAMPLE_STATE;
525 f->NewSampleShading = ST_NEW_SAMPLE_SHADING;
526
527 /* This depends on what the gallium driver wants. */
528 if (st->force_persample_in_shader) {
529 f->NewMultisampleEnable |= ST_NEW_FS_STATE;
530 f->NewSampleShading |= ST_NEW_FS_STATE;
531 } else {
532 f->NewSampleShading |= ST_NEW_RASTERIZER;
533 }
534
535 f->NewClipControl = ST_NEW_VIEWPORT | ST_NEW_RASTERIZER;
536 f->NewClipPlane = ST_NEW_CLIP_STATE;
537
538 if (st->clamp_frag_color_in_shader) {
539 f->NewFragClamp = ST_NEW_FS_STATE;
540 } else {
541 f->NewFragClamp = ST_NEW_RASTERIZER;
542 }
543
544 if (st->clamp_frag_depth_in_shader) {
545 f->NewClipControl |= ST_NEW_VS_STATE | ST_NEW_GS_STATE |
546 ST_NEW_TES_STATE;
547
548 f->NewDepthClamp = ST_NEW_FS_STATE | ST_NEW_VS_STATE |
549 ST_NEW_GS_STATE | ST_NEW_TES_STATE;
550 } else {
551 f->NewDepthClamp = ST_NEW_RASTERIZER;
552 }
553
554 if (st->lower_ucp)
555 f->NewClipPlaneEnable = ST_NEW_VS_STATE;
556 else
557 f->NewClipPlaneEnable = ST_NEW_RASTERIZER;
558
559 f->NewLineState = ST_NEW_RASTERIZER;
560 f->NewPolygonState = ST_NEW_RASTERIZER;
561 f->NewPolygonStipple = ST_NEW_POLY_STIPPLE;
562 f->NewViewport = ST_NEW_VIEWPORT;
563 f->NewNvConservativeRasterization = ST_NEW_RASTERIZER;
564 f->NewNvConservativeRasterizationParams = ST_NEW_RASTERIZER;
565 f->NewIntelConservativeRasterization = ST_NEW_RASTERIZER;
566 }
567
568
569 static struct st_context *
570 st_create_context_priv(struct gl_context *ctx, struct pipe_context *pipe,
571 const struct st_config_options *options, bool no_error)
572 {
573 struct pipe_screen *screen = pipe->screen;
574 uint i;
575 struct st_context *st = ST_CALLOC_STRUCT( st_context);
576
577 st->options = *options;
578
579 ctx->st = st;
580
581 st->ctx = ctx;
582 st->pipe = pipe;
583 st->dirty = ST_ALL_STATES_MASK;
584
585 st->can_bind_const_buffer_as_vertex =
586 screen->get_param(screen, PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX);
587
588 /* st/mesa always uploads zero-stride vertex attribs, and other user
589 * vertex buffers are only possible with a compatibility profile.
590 * So tell the u_vbuf module that user VBOs are not possible with the Core
591 * profile, so that u_vbuf is bypassed completely if there is nothing else
592 * to do.
593 */
594 unsigned cso_flags;
595 switch (ctx->API) {
596 case API_OPENGL_CORE:
597 cso_flags = CSO_NO_USER_VERTEX_BUFFERS;
598 break;
599 case API_OPENGLES:
600 case API_OPENGLES2:
601 cso_flags = CSO_NO_64B_VERTEX_BUFFERS;
602 break;
603 default:
604 cso_flags = 0;
605 break;
606 }
607
608 st->cso_context = cso_create_context(pipe, cso_flags);
609
610 st_init_atoms(st);
611 st_init_clear(st);
612 st_init_pbo_helpers(st);
613
614 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
615 if (pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
616 st->internal_target = PIPE_TEXTURE_2D;
617 else
618 st->internal_target = PIPE_TEXTURE_RECT;
619
620 /* Setup vertex element info for 'struct st_util_vertex'.
621 */
622 {
623 STATIC_ASSERT(sizeof(struct st_util_vertex) == 9 * sizeof(float));
624
625 memset(&st->util_velems, 0, sizeof(st->util_velems));
626 st->util_velems.velems[0].src_offset = 0;
627 st->util_velems.velems[0].vertex_buffer_index = 0;
628 st->util_velems.velems[0].src_format = PIPE_FORMAT_R32G32B32_FLOAT;
629 st->util_velems.velems[1].src_offset = 3 * sizeof(float);
630 st->util_velems.velems[1].vertex_buffer_index = 0;
631 st->util_velems.velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
632 st->util_velems.velems[2].src_offset = 7 * sizeof(float);
633 st->util_velems.velems[2].vertex_buffer_index = 0;
634 st->util_velems.velems[2].src_format = PIPE_FORMAT_R32G32_FLOAT;
635 }
636
637 /* Need these flags:
638 */
639 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
640 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
641
642 if (no_error)
643 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
644
645 ctx->Const.PackedDriverUniformStorage =
646 screen->get_param(screen, PIPE_CAP_PACKED_UNIFORMS);
647
648 ctx->Const.BitmapUsesRed =
649 screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
650 PIPE_TEXTURE_2D, 0, 0,
651 PIPE_BIND_SAMPLER_VIEW);
652
653 st->has_stencil_export =
654 screen->get_param(screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
655 st->has_etc1 = screen->is_format_supported(screen, PIPE_FORMAT_ETC1_RGB8,
656 PIPE_TEXTURE_2D, 0, 0,
657 PIPE_BIND_SAMPLER_VIEW);
658 st->has_etc2 = screen->is_format_supported(screen, PIPE_FORMAT_ETC2_RGB8,
659 PIPE_TEXTURE_2D, 0, 0,
660 PIPE_BIND_SAMPLER_VIEW);
661 st->has_astc_2d_ldr =
662 screen->is_format_supported(screen, PIPE_FORMAT_ASTC_4x4_SRGB,
663 PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW);
664 st->has_astc_5x5_ldr =
665 screen->is_format_supported(screen, PIPE_FORMAT_ASTC_5x5_SRGB,
666 PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW);
667 st->prefer_blit_based_texture_transfer = screen->get_param(screen,
668 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER);
669 st->force_persample_in_shader =
670 screen->get_param(screen, PIPE_CAP_SAMPLE_SHADING) &&
671 !screen->get_param(screen, PIPE_CAP_FORCE_PERSAMPLE_INTERP);
672 st->has_shareable_shaders = screen->get_param(screen,
673 PIPE_CAP_SHAREABLE_SHADERS);
674 st->needs_texcoord_semantic =
675 screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
676 st->apply_texture_swizzle_to_border_color =
677 !!(screen->get_param(screen, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK) &
678 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50 |
679 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600));
680 st->has_time_elapsed =
681 screen->get_param(screen, PIPE_CAP_QUERY_TIME_ELAPSED);
682 st->has_half_float_packing =
683 screen->get_param(screen, PIPE_CAP_TGSI_PACK_HALF_FLOAT);
684 st->has_multi_draw_indirect =
685 screen->get_param(screen, PIPE_CAP_MULTI_DRAW_INDIRECT);
686 st->has_single_pipe_stat =
687 screen->get_param(screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE);
688 st->has_indep_blend_func =
689 screen->get_param(screen, PIPE_CAP_INDEP_BLEND_FUNC);
690 st->needs_rgb_dst_alpha_override =
691 screen->get_param(screen, PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND);
692 st->lower_flatshade =
693 !screen->get_param(screen, PIPE_CAP_FLATSHADE);
694 st->lower_alpha_test =
695 !screen->get_param(screen, PIPE_CAP_ALPHA_TEST);
696 st->lower_point_size =
697 !screen->get_param(screen, PIPE_CAP_POINT_SIZE_FIXED);
698 st->lower_two_sided_color =
699 !screen->get_param(screen, PIPE_CAP_TWO_SIDED_COLOR);
700 st->lower_ucp =
701 !screen->get_param(screen, PIPE_CAP_CLIP_PLANES);
702 st->allow_st_finalize_nir_twice = screen->finalize_nir != NULL;
703
704 st->has_hw_atomics =
705 screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
706 PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS)
707 ? true : false;
708
709 util_throttle_init(&st->throttle,
710 screen->get_param(screen,
711 PIPE_CAP_MAX_TEXTURE_UPLOAD_MEMORY_BUDGET));
712
713 /* GL limits and extensions */
714 st_init_limits(pipe->screen, &ctx->Const, &ctx->Extensions);
715 st_init_extensions(pipe->screen, &ctx->Const,
716 &ctx->Extensions, &st->options, ctx->API);
717
718 /* FIXME: add support for geometry and tessellation shaders for
719 * lower_point_size
720 */
721 assert(!ctx->Extensions.OES_geometry_shader || !st->lower_point_size);
722 assert(!ctx->Extensions.ARB_tessellation_shader || !st->lower_point_size);
723
724 /* FIXME: add support for geometry and tessellation shaders for
725 * lower_ucp
726 */
727 assert(!ctx->Extensions.OES_geometry_shader || !st->lower_ucp);
728 assert(!ctx->Extensions.ARB_tessellation_shader || !st->lower_ucp);
729
730 if (st_have_perfmon(st)) {
731 ctx->Extensions.AMD_performance_monitor = GL_TRUE;
732 }
733
734 if (st_have_perfquery(st)) {
735 ctx->Extensions.INTEL_performance_query = GL_TRUE;
736 }
737
738 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
739 if (screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_UNCLAMPED)) {
740 if (!screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_CLAMPED)) {
741 st->clamp_vert_color_in_shader = GL_TRUE;
742 }
743
744 if (!screen->get_param(screen, PIPE_CAP_FRAGMENT_COLOR_CLAMPED)) {
745 st->clamp_frag_color_in_shader = GL_TRUE;
746 }
747
748 /* For drivers which cannot do color clamping, it's better to just
749 * disable ARB_color_buffer_float in the core profile, because
750 * the clamping is deprecated there anyway. */
751 if (ctx->API == API_OPENGL_CORE &&
752 (st->clamp_frag_color_in_shader || st->clamp_vert_color_in_shader)) {
753 st->clamp_vert_color_in_shader = GL_FALSE;
754 st->clamp_frag_color_in_shader = GL_FALSE;
755 ctx->Extensions.ARB_color_buffer_float = GL_FALSE;
756 }
757 }
758
759 if (screen->get_param(screen, PIPE_CAP_DEPTH_CLIP_DISABLE) == 2)
760 st->clamp_frag_depth_in_shader = true;
761
762 /* called after _mesa_create_context/_mesa_init_point, fix default user
763 * settable max point size up
764 */
765 ctx->Point.MaxSize = MAX2(ctx->Const.MaxPointSize,
766 ctx->Const.MaxPointSizeAA);
767 /* For vertex shaders, make sure not to emit saturate when SM 3.0
768 * is not supported
769 */
770 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitNoSat =
771 !screen->get_param(screen, PIPE_CAP_VERTEX_SHADER_SATURATE);
772
773 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].PositionAlwaysInvariant = options->vs_position_always_invariant;
774
775 enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir)
776 screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
777 PIPE_SHADER_CAP_PREFERRED_IR);
778 ctx->Const.UseNIRGLSLLinker = preferred_ir == PIPE_SHADER_IR_NIR;
779
780 if (ctx->Const.GLSLVersion < 400) {
781 for (i = 0; i < MESA_SHADER_STAGES; i++)
782 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler = true;
783 }
784
785 /* Set which shader types can be compiled at link time. */
786 st->shader_has_one_variant[MESA_SHADER_VERTEX] =
787 st->has_shareable_shaders &&
788 !st->clamp_frag_depth_in_shader &&
789 !st->clamp_vert_color_in_shader &&
790 !st->lower_point_size &&
791 !st->lower_ucp;
792
793 st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
794 st->has_shareable_shaders &&
795 !st->lower_flatshade &&
796 !st->lower_alpha_test &&
797 !st->clamp_frag_color_in_shader &&
798 !st->clamp_frag_depth_in_shader &&
799 !st->force_persample_in_shader &&
800 !st->lower_two_sided_color;
801
802 st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
803 st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] =
804 st->has_shareable_shaders &&
805 !st->clamp_frag_depth_in_shader &&
806 !st->clamp_vert_color_in_shader;
807 st->shader_has_one_variant[MESA_SHADER_GEOMETRY] =
808 st->has_shareable_shaders &&
809 !st->clamp_frag_depth_in_shader &&
810 !st->clamp_vert_color_in_shader;
811 st->shader_has_one_variant[MESA_SHADER_COMPUTE] = st->has_shareable_shaders;
812
813 st->bitmap.cache.empty = true;
814
815 _mesa_override_extensions(ctx);
816 _mesa_compute_version(ctx);
817
818 if (ctx->Version == 0) {
819 /* This can happen when a core profile was requested, but the driver
820 * does not support some features of GL 3.1 or later.
821 */
822 st_destroy_context_priv(st, false);
823 return NULL;
824 }
825
826 /* This must be done after extensions are initialized to enable persistent
827 * mappings immediately.
828 */
829 _vbo_CreateContext(ctx, true);
830
831 _mesa_initialize_dispatch_tables(ctx);
832 _mesa_initialize_vbo_vtxfmt(ctx);
833 st_init_driver_flags(st);
834
835 /* Initialize context's winsys buffers list */
836 list_inithead(&st->winsys_buffers);
837
838 list_inithead(&st->zombie_sampler_views.list.node);
839 simple_mtx_init(&st->zombie_sampler_views.mutex, mtx_plain);
840 list_inithead(&st->zombie_shaders.list.node);
841 simple_mtx_init(&st->zombie_shaders.mutex, mtx_plain);
842
843 return st;
844 }
845
846
847 static void
848 st_emit_string_marker(struct gl_context *ctx, const GLchar *string, GLsizei len)
849 {
850 struct st_context *st = ctx->st;
851 st->pipe->emit_string_marker(st->pipe, string, len);
852 }
853
854
855 static void
856 st_set_background_context(struct gl_context *ctx,
857 struct util_queue_monitoring *queue_info)
858 {
859 struct st_context *st = ctx->st;
860 struct st_manager *smapi =
861 (struct st_manager *) st->iface.st_context_private;
862
863 assert(smapi->set_background_context);
864 smapi->set_background_context(&st->iface, queue_info);
865 }
866
867
868 static void
869 st_get_device_uuid(struct gl_context *ctx, char *uuid)
870 {
871 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
872
873 assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
874 memset(uuid, 0, GL_UUID_SIZE_EXT);
875 screen->get_device_uuid(screen, uuid);
876 }
877
878
879 static void
880 st_get_driver_uuid(struct gl_context *ctx, char *uuid)
881 {
882 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
883
884 assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
885 memset(uuid, 0, GL_UUID_SIZE_EXT);
886 screen->get_driver_uuid(screen, uuid);
887 }
888
889
890 static void
891 st_init_driver_functions(struct pipe_screen *screen,
892 struct dd_function_table *functions)
893 {
894 _mesa_init_sampler_object_functions(functions);
895
896 st_init_draw_functions(functions);
897 st_init_blit_functions(functions);
898 st_init_bufferobject_functions(screen, functions);
899 st_init_clear_functions(functions);
900 st_init_bitmap_functions(functions);
901 st_init_copy_image_functions(functions);
902 st_init_drawpixels_functions(functions);
903 st_init_rasterpos_functions(functions);
904
905 st_init_drawtex_functions(functions);
906
907 st_init_eglimage_functions(functions);
908
909 st_init_fbo_functions(functions);
910 st_init_feedback_functions(functions);
911 st_init_memoryobject_functions(functions);
912 st_init_msaa_functions(functions);
913 st_init_perfmon_functions(functions);
914 st_init_perfquery_functions(functions);
915 st_init_program_functions(functions);
916 st_init_query_functions(functions);
917 st_init_cond_render_functions(functions);
918 st_init_readpixels_functions(functions);
919 st_init_semaphoreobject_functions(functions);
920 st_init_texture_functions(functions);
921 st_init_texture_barrier_functions(functions);
922 st_init_flush_functions(screen, functions);
923 st_init_string_functions(functions);
924 st_init_viewport_functions(functions);
925 st_init_compute_functions(functions);
926
927 st_init_xformfb_functions(functions);
928 st_init_syncobj_functions(functions);
929
930 st_init_vdpau_functions(functions);
931
932 if (screen->get_param(screen, PIPE_CAP_STRING_MARKER))
933 functions->EmitStringMarker = st_emit_string_marker;
934
935 functions->Enable = st_Enable;
936 functions->UpdateState = st_invalidate_state;
937 functions->QueryMemoryInfo = st_query_memory_info;
938 functions->SetBackgroundContext = st_set_background_context;
939 functions->GetDriverUuid = st_get_driver_uuid;
940 functions->GetDeviceUuid = st_get_device_uuid;
941
942 /* GL_ARB_get_program_binary */
943 functions->GetProgramBinaryDriverSHA1 = st_get_program_binary_driver_sha1;
944
945 enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir)
946 screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
947 PIPE_SHADER_CAP_PREFERRED_IR);
948 if (preferred_ir == PIPE_SHADER_IR_NIR) {
949 functions->ShaderCacheSerializeDriverBlob = st_serialise_nir_program;
950 functions->ProgramBinarySerializeDriverBlob =
951 st_serialise_nir_program_binary;
952 functions->ProgramBinaryDeserializeDriverBlob =
953 st_deserialise_nir_program;
954 } else {
955 functions->ShaderCacheSerializeDriverBlob = st_serialise_tgsi_program;
956 functions->ProgramBinarySerializeDriverBlob =
957 st_serialise_tgsi_program_binary;
958 functions->ProgramBinaryDeserializeDriverBlob =
959 st_deserialise_tgsi_program;
960 }
961 }
962
963
964 struct st_context *
965 st_create_context(gl_api api, struct pipe_context *pipe,
966 const struct gl_config *visual,
967 struct st_context *share,
968 const struct st_config_options *options,
969 bool no_error)
970 {
971 struct gl_context *ctx;
972 struct gl_context *shareCtx = share ? share->ctx : NULL;
973 struct dd_function_table funcs;
974 struct st_context *st;
975
976 util_cpu_detect();
977
978 memset(&funcs, 0, sizeof(funcs));
979 st_init_driver_functions(pipe->screen, &funcs);
980
981 ctx = calloc(1, sizeof(struct gl_context));
982 if (!ctx)
983 return NULL;
984
985 if (!_mesa_initialize_context(ctx, api, visual, shareCtx, &funcs)) {
986 free(ctx);
987 return NULL;
988 }
989
990 st_debug_init();
991
992 if (pipe->screen->get_disk_shader_cache)
993 ctx->Cache = pipe->screen->get_disk_shader_cache(pipe->screen);
994
995 /* XXX: need a capability bit in gallium to query if the pipe
996 * driver prefers DP4 or MUL/MAD for vertex transformation.
997 */
998 if (debug_get_option_mesa_mvp_dp4())
999 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = GL_TRUE;
1000
1001 st = st_create_context_priv(ctx, pipe, options, no_error);
1002 if (!st) {
1003 _mesa_destroy_context(ctx);
1004 }
1005
1006 return st;
1007 }
1008
1009
1010 /**
1011 * When we destroy a context, we must examine all texture objects to
1012 * find/release any sampler views created by that context.
1013 *
1014 * This callback is called per-texture object. It releases all the
1015 * texture's sampler views which belong to the context.
1016 */
1017 static void
1018 destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
1019 {
1020 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
1021 struct st_context *st = (struct st_context *) userData;
1022
1023 st_texture_release_context_sampler_view(st, st_texture_object(texObj));
1024 }
1025
1026 static void
1027 destroy_framebuffer_attachment_sampler_cb(GLuint id, void *data, void *userData)
1028 {
1029 struct gl_framebuffer* glfb = (struct gl_framebuffer*) data;
1030 struct st_context *st = (struct st_context *) userData;
1031
1032 for (unsigned i = 0; i < BUFFER_COUNT; i++) {
1033 struct gl_renderbuffer_attachment *att = &glfb->Attachment[i];
1034 if (att->Texture) {
1035 st_texture_release_context_sampler_view(st, st_texture_object(att->Texture));
1036 }
1037 }
1038 }
1039
1040 void
1041 st_destroy_context(struct st_context *st)
1042 {
1043 struct gl_context *ctx = st->ctx;
1044 struct st_framebuffer *stfb, *next;
1045 struct gl_framebuffer *save_drawbuffer;
1046 struct gl_framebuffer *save_readbuffer;
1047
1048 /* Save the current context and draw/read buffers*/
1049 GET_CURRENT_CONTEXT(save_ctx);
1050 if (save_ctx) {
1051 save_drawbuffer = save_ctx->WinSysDrawBuffer;
1052 save_readbuffer = save_ctx->WinSysReadBuffer;
1053 } else {
1054 save_drawbuffer = save_readbuffer = NULL;
1055 }
1056
1057 /*
1058 * We need to bind the context we're deleting so that
1059 * _mesa_reference_texobj_() uses this context when deleting textures.
1060 * Similarly for framebuffer objects, etc.
1061 */
1062 _mesa_make_current(ctx, NULL, NULL);
1063
1064 /* This must be called first so that glthread has a chance to finish */
1065 _mesa_glthread_destroy(ctx);
1066
1067 _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
1068
1069 /* For the fallback textures, free any sampler views belonging to this
1070 * context.
1071 */
1072 for (unsigned i = 0; i < NUM_TEXTURE_TARGETS; i++) {
1073 struct st_texture_object *stObj =
1074 st_texture_object(ctx->Shared->FallbackTex[i]);
1075 if (stObj) {
1076 st_texture_release_context_sampler_view(st, stObj);
1077 }
1078 }
1079
1080 st_context_free_zombie_objects(st);
1081
1082 simple_mtx_destroy(&st->zombie_sampler_views.mutex);
1083 simple_mtx_destroy(&st->zombie_shaders.mutex);
1084
1085 st_release_program(st, &st->fp);
1086 st_release_program(st, &st->gp);
1087 st_release_program(st, &st->vp);
1088 st_release_program(st, &st->tcp);
1089 st_release_program(st, &st->tep);
1090 st_release_program(st, &st->cp);
1091
1092 /* release framebuffer in the winsys buffers list */
1093 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
1094 st_framebuffer_reference(&stfb, NULL);
1095 }
1096
1097 _mesa_HashWalk(ctx->Shared->FrameBuffers, destroy_framebuffer_attachment_sampler_cb, st);
1098
1099 pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
1100 pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
1101
1102 _vbo_DestroyContext(ctx);
1103
1104 st_destroy_program_variants(st);
1105
1106 _mesa_free_context_data(ctx);
1107
1108 /* This will free the st_context too, so 'st' must not be accessed
1109 * afterwards. */
1110 st_destroy_context_priv(st, true);
1111 st = NULL;
1112
1113 free(ctx);
1114
1115 if (save_ctx == ctx) {
1116 /* unbind the context we just deleted */
1117 _mesa_make_current(NULL, NULL, NULL);
1118 } else {
1119 /* Restore the current context and draw/read buffers (may be NULL) */
1120 _mesa_make_current(save_ctx, save_drawbuffer, save_readbuffer);
1121 }
1122 }