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