• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "main/imports.h"
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_program.h"
61 #include "st_cb_queryobj.h"
62 #include "st_cb_readpixels.h"
63 #include "st_cb_texture.h"
64 #include "st_cb_xformfb.h"
65 #include "st_cb_flush.h"
66 #include "st_cb_syncobj.h"
67 #include "st_cb_strings.h"
68 #include "st_cb_texturebarrier.h"
69 #include "st_cb_viewport.h"
70 #include "st_atom.h"
71 #include "st_draw.h"
72 #include "st_extensions.h"
73 #include "st_gen_mipmap.h"
74 #include "st_pbo.h"
75 #include "st_program.h"
76 #include "st_sampler_view.h"
77 #include "st_shader_cache.h"
78 #include "st_vdpau.h"
79 #include "st_texture.h"
80 #include "pipe/p_context.h"
81 #include "util/u_inlines.h"
82 #include "util/u_upload_mgr.h"
83 #include "util/u_vbuf.h"
84 #include "cso_cache/cso_context.h"
85 
86 
87 DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
88 
89 
90 /**
91  * Called via ctx->Driver.Enable()
92  */
93 static void
st_Enable(struct gl_context * ctx,GLenum cap,GLboolean state)94 st_Enable(struct gl_context *ctx, GLenum cap, GLboolean state)
95 {
96    struct st_context *st = st_context(ctx);
97 
98    switch (cap) {
99    case GL_DEBUG_OUTPUT:
100    case GL_DEBUG_OUTPUT_SYNCHRONOUS:
101       st_update_debug_callback(st);
102       break;
103    default:
104       break;
105    }
106 }
107 
108 
109 /**
110  * Called via ctx->Driver.QueryMemoryInfo()
111  */
112 static void
st_query_memory_info(struct gl_context * ctx,struct gl_memory_info * out)113 st_query_memory_info(struct gl_context *ctx, struct gl_memory_info *out)
114 {
115    struct pipe_screen *screen = st_context(ctx)->pipe->screen;
116    struct pipe_memory_info info;
117 
118    assert(screen->query_memory_info);
119    if (!screen->query_memory_info)
120       return;
121 
122    screen->query_memory_info(screen, &info);
123 
124    out->total_device_memory = info.total_device_memory;
125    out->avail_device_memory = info.avail_device_memory;
126    out->total_staging_memory = info.total_staging_memory;
127    out->avail_staging_memory = info.avail_staging_memory;
128    out->device_memory_evicted = info.device_memory_evicted;
129    out->nr_device_memory_evictions = info.nr_device_memory_evictions;
130 }
131 
132 
133 uint64_t
st_get_active_states(struct gl_context * ctx)134 st_get_active_states(struct gl_context *ctx)
135 {
136    struct st_vertex_program *vp =
137       st_vertex_program(ctx->VertexProgram._Current);
138    struct st_common_program *tcp =
139       st_common_program(ctx->TessCtrlProgram._Current);
140    struct st_common_program *tep =
141       st_common_program(ctx->TessEvalProgram._Current);
142    struct st_common_program *gp =
143       st_common_program(ctx->GeometryProgram._Current);
144    struct st_fragment_program *fp =
145       st_fragment_program(ctx->FragmentProgram._Current);
146    struct st_compute_program *cp =
147       st_compute_program(ctx->ComputeProgram._Current);
148    uint64_t active_shader_states = 0;
149 
150    if (vp)
151       active_shader_states |= vp->affected_states;
152    if (tcp)
153       active_shader_states |= tcp->affected_states;
154    if (tep)
155       active_shader_states |= tep->affected_states;
156    if (gp)
157       active_shader_states |= gp->affected_states;
158    if (fp)
159       active_shader_states |= fp->affected_states;
160    if (cp)
161       active_shader_states |= cp->affected_states;
162 
163    /* Mark non-shader-resource shader states as "always active". */
164    return active_shader_states | ~ST_ALL_SHADER_RESOURCES;
165 }
166 
167 
168 void
st_invalidate_buffers(struct st_context * st)169 st_invalidate_buffers(struct st_context *st)
170 {
171    st->dirty |= ST_NEW_BLEND |
172                 ST_NEW_DSA |
173                 ST_NEW_FB_STATE |
174                 ST_NEW_SAMPLE_MASK |
175                 ST_NEW_SAMPLE_SHADING |
176                 ST_NEW_FS_STATE |
177                 ST_NEW_POLY_STIPPLE |
178                 ST_NEW_VIEWPORT |
179                 ST_NEW_RASTERIZER |
180                 ST_NEW_SCISSOR |
181                 ST_NEW_WINDOW_RECTANGLES;
182 }
183 
184 
185 /**
186  * Called via ctx->Driver.UpdateState()
187  */
188 static void
st_invalidate_state(struct gl_context * ctx)189 st_invalidate_state(struct gl_context *ctx)
190 {
191    GLbitfield new_state = ctx->NewState;
192    struct st_context *st = st_context(ctx);
193 
194    if (new_state & _NEW_BUFFERS) {
195       st_invalidate_buffers(st);
196    } else {
197       /* These set a subset of flags set by _NEW_BUFFERS, so we only have to
198        * check them when _NEW_BUFFERS isn't set.
199        */
200       if (new_state & _NEW_PROGRAM)
201          st->dirty |= ST_NEW_RASTERIZER;
202 
203       if (new_state & _NEW_FOG)
204          st->dirty |= ST_NEW_FS_STATE;
205 
206       if (new_state & _NEW_FRAG_CLAMP) {
207          if (st->clamp_frag_color_in_shader)
208             st->dirty |= ST_NEW_FS_STATE;
209          else
210             st->dirty |= ST_NEW_RASTERIZER;
211       }
212    }
213 
214    if (new_state & (_NEW_LIGHT |
215                     _NEW_POINT))
216       st->dirty |= ST_NEW_RASTERIZER;
217 
218    if (new_state & _NEW_PROJECTION &&
219        st_user_clip_planes_enabled(ctx))
220       st->dirty |= ST_NEW_CLIP_STATE;
221 
222    if (new_state & _NEW_PIXEL)
223       st->dirty |= ST_NEW_PIXEL_TRANSFER;
224 
225    if (new_state & _NEW_CURRENT_ATTRIB)
226       st->dirty |= ST_NEW_VERTEX_ARRAYS;
227 
228    /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
229    if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT))
230       st->dirty |= ST_NEW_VS_STATE;
231 
232    /* Which shaders are dirty will be determined manually. */
233    if (new_state & _NEW_PROGRAM) {
234       st->gfx_shaders_may_be_dirty = true;
235       st->compute_shader_may_be_dirty = true;
236       /* This will mask out unused shader resources. */
237       st->active_states = st_get_active_states(ctx);
238    }
239 
240    if (new_state & _NEW_TEXTURE_OBJECT) {
241       st->dirty |= st->active_states &
242                    (ST_NEW_SAMPLER_VIEWS |
243                     ST_NEW_SAMPLERS |
244                     ST_NEW_IMAGE_UNITS);
245       if (ctx->FragmentProgram._Current &&
246           ctx->FragmentProgram._Current->ExternalSamplersUsed) {
247          st->dirty |= ST_NEW_FS_STATE;
248       }
249    }
250 }
251 
252 
253 static void
st_destroy_context_priv(struct st_context * st,bool destroy_pipe)254 st_destroy_context_priv(struct st_context *st, bool destroy_pipe)
255 {
256    uint shader, i;
257 
258    st_destroy_atoms(st);
259    st_destroy_draw(st);
260    st_destroy_clear(st);
261    st_destroy_bitmap(st);
262    st_destroy_drawpix(st);
263    st_destroy_drawtex(st);
264    st_destroy_perfmon(st);
265    st_destroy_pbo_helpers(st);
266    st_destroy_bound_texture_handles(st);
267    st_destroy_bound_image_handles(st);
268 
269    for (shader = 0; shader < ARRAY_SIZE(st->state.sampler_views); shader++) {
270       for (i = 0; i < ARRAY_SIZE(st->state.sampler_views[0]); i++) {
271          pipe_sampler_view_release(st->pipe,
272                                    &st->state.sampler_views[shader][i]);
273       }
274    }
275 
276    /* free glDrawPixels cache data */
277    free(st->drawpix_cache.image);
278    pipe_resource_reference(&st->drawpix_cache.texture, NULL);
279 
280    /* free glReadPixels cache data */
281    st_invalidate_readpix_cache(st);
282 
283    cso_destroy_context(st->cso_context);
284 
285    if (st->pipe && destroy_pipe)
286       st->pipe->destroy(st->pipe);
287 
288    free(st);
289 }
290 
291 
292 static void
st_init_driver_flags(struct st_context * st)293 st_init_driver_flags(struct st_context *st)
294 {
295    struct gl_driver_flags *f = &st->ctx->DriverFlags;
296 
297    f->NewArray = ST_NEW_VERTEX_ARRAYS;
298    f->NewRasterizerDiscard = ST_NEW_RASTERIZER;
299    f->NewTileRasterOrder = ST_NEW_RASTERIZER;
300    f->NewUniformBuffer = ST_NEW_UNIFORM_BUFFER;
301    f->NewDefaultTessLevels = ST_NEW_TESS_STATE;
302 
303    /* Shader resources */
304    f->NewTextureBuffer = ST_NEW_SAMPLER_VIEWS;
305    if (st->has_hw_atomics)
306       f->NewAtomicBuffer = ST_NEW_HW_ATOMICS | ST_NEW_CS_ATOMICS;
307    else
308       f->NewAtomicBuffer = ST_NEW_ATOMIC_BUFFER;
309    f->NewShaderStorageBuffer = ST_NEW_STORAGE_BUFFER;
310    f->NewImageUnits = ST_NEW_IMAGE_UNITS;
311 
312    f->NewShaderConstants[MESA_SHADER_VERTEX] = ST_NEW_VS_CONSTANTS;
313    f->NewShaderConstants[MESA_SHADER_TESS_CTRL] = ST_NEW_TCS_CONSTANTS;
314    f->NewShaderConstants[MESA_SHADER_TESS_EVAL] = ST_NEW_TES_CONSTANTS;
315    f->NewShaderConstants[MESA_SHADER_GEOMETRY] = ST_NEW_GS_CONSTANTS;
316    f->NewShaderConstants[MESA_SHADER_FRAGMENT] = ST_NEW_FS_CONSTANTS;
317    f->NewShaderConstants[MESA_SHADER_COMPUTE] = ST_NEW_CS_CONSTANTS;
318 
319    f->NewWindowRectangles = ST_NEW_WINDOW_RECTANGLES;
320    f->NewFramebufferSRGB = ST_NEW_FB_STATE;
321    f->NewScissorRect = ST_NEW_SCISSOR;
322    f->NewScissorTest = ST_NEW_SCISSOR | ST_NEW_RASTERIZER;
323    f->NewAlphaTest = ST_NEW_DSA;
324    f->NewBlend = ST_NEW_BLEND;
325    f->NewBlendColor = ST_NEW_BLEND_COLOR;
326    f->NewColorMask = ST_NEW_BLEND;
327    f->NewDepth = ST_NEW_DSA;
328    f->NewLogicOp = ST_NEW_BLEND;
329    f->NewStencil = ST_NEW_DSA;
330    f->NewMultisampleEnable = ST_NEW_BLEND | ST_NEW_RASTERIZER |
331                              ST_NEW_SAMPLE_MASK | ST_NEW_SAMPLE_SHADING;
332    f->NewSampleAlphaToXEnable = ST_NEW_BLEND;
333    f->NewSampleMask = ST_NEW_SAMPLE_MASK;
334    f->NewSampleShading = ST_NEW_SAMPLE_SHADING;
335 
336    /* This depends on what the gallium driver wants. */
337    if (st->force_persample_in_shader) {
338       f->NewMultisampleEnable |= ST_NEW_FS_STATE;
339       f->NewSampleShading |= ST_NEW_FS_STATE;
340    } else {
341       f->NewSampleShading |= ST_NEW_RASTERIZER;
342    }
343 
344    f->NewClipControl = ST_NEW_VIEWPORT | ST_NEW_RASTERIZER;
345    f->NewClipPlane = ST_NEW_CLIP_STATE;
346    f->NewClipPlaneEnable = ST_NEW_RASTERIZER;
347    f->NewDepthClamp = ST_NEW_RASTERIZER;
348    f->NewLineState = ST_NEW_RASTERIZER;
349    f->NewPolygonState = ST_NEW_RASTERIZER;
350    f->NewPolygonStipple = ST_NEW_POLY_STIPPLE;
351    f->NewViewport = ST_NEW_VIEWPORT;
352 }
353 
354 
355 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)356 st_create_context_priv(struct gl_context *ctx, struct pipe_context *pipe,
357                        const struct st_config_options *options, bool no_error)
358 {
359    struct pipe_screen *screen = pipe->screen;
360    uint i;
361    struct st_context *st = ST_CALLOC_STRUCT( st_context);
362 
363    st->options = *options;
364 
365    ctx->st = st;
366 
367    st->ctx = ctx;
368    st->pipe = pipe;
369 
370    /* state tracker needs the VBO module */
371    _vbo_CreateContext(ctx);
372 
373    st->dirty = ST_ALL_STATES_MASK;
374 
375    st->can_bind_const_buffer_as_vertex =
376       screen->get_param(screen, PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX);
377 
378    /* st/mesa always uploads zero-stride vertex attribs, and other user
379     * vertex buffers are only possible with a compatibility profile.
380     * So tell the u_vbuf module that user VBOs are not possible with the Core
381     * profile, so that u_vbuf is bypassed completely if there is nothing else
382     * to do.
383     */
384    unsigned vbuf_flags =
385       ctx->API == API_OPENGL_CORE ? U_VBUF_FLAG_NO_USER_VBOS : 0;
386    st->cso_context = cso_create_context(pipe, vbuf_flags);
387 
388    st_init_atoms(st);
389    st_init_clear(st);
390    st_init_draw(st);
391    st_init_pbo_helpers(st);
392 
393    /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
394    if (pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
395       st->internal_target = PIPE_TEXTURE_2D;
396    else
397       st->internal_target = PIPE_TEXTURE_RECT;
398 
399    /* Setup vertex element info for 'struct st_util_vertex'.
400     */
401    {
402       const unsigned slot = cso_get_aux_vertex_buffer_slot(st->cso_context);
403 
404       /* If this assertion ever fails all state tracker calls to
405        * cso_get_aux_vertex_buffer_slot() should be audited.  This
406        * particular call would have to be moved to just before each
407        * drawing call.
408        */
409       assert(slot == 0);
410 
411       STATIC_ASSERT(sizeof(struct st_util_vertex) == 9 * sizeof(float));
412 
413       memset(&st->util_velems, 0, sizeof(st->util_velems));
414       st->util_velems[0].src_offset = 0;
415       st->util_velems[0].vertex_buffer_index = slot;
416       st->util_velems[0].src_format = PIPE_FORMAT_R32G32B32_FLOAT;
417       st->util_velems[1].src_offset = 3 * sizeof(float);
418       st->util_velems[1].vertex_buffer_index = slot;
419       st->util_velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
420       st->util_velems[2].src_offset = 7 * sizeof(float);
421       st->util_velems[2].vertex_buffer_index = slot;
422       st->util_velems[2].src_format = PIPE_FORMAT_R32G32_FLOAT;
423    }
424 
425    /* we want all vertex data to be placed in buffer objects */
426    vbo_use_buffer_objects(ctx);
427 
428 
429    /* make sure that no VBOs are left mapped when we're drawing. */
430    vbo_always_unmap_buffers(ctx);
431 
432    /* Need these flags:
433     */
434    ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
435 
436    ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
437 
438    if (no_error)
439       ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
440 
441    st->has_stencil_export =
442       screen->get_param(screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
443    st->has_shader_model3 = screen->get_param(screen, PIPE_CAP_SM3);
444    st->has_etc1 = screen->is_format_supported(screen, PIPE_FORMAT_ETC1_RGB8,
445                                               PIPE_TEXTURE_2D, 0,
446                                               PIPE_BIND_SAMPLER_VIEW);
447    st->has_etc2 = screen->is_format_supported(screen, PIPE_FORMAT_ETC2_RGB8,
448                                               PIPE_TEXTURE_2D, 0,
449                                               PIPE_BIND_SAMPLER_VIEW);
450    st->prefer_blit_based_texture_transfer = screen->get_param(screen,
451                               PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER);
452    st->force_persample_in_shader =
453       screen->get_param(screen, PIPE_CAP_SAMPLE_SHADING) &&
454       !screen->get_param(screen, PIPE_CAP_FORCE_PERSAMPLE_INTERP);
455    st->has_shareable_shaders = screen->get_param(screen,
456                                                  PIPE_CAP_SHAREABLE_SHADERS);
457    st->needs_texcoord_semantic =
458       screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
459    st->apply_texture_swizzle_to_border_color =
460       !!(screen->get_param(screen, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK) &
461          (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50 |
462           PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600));
463    st->has_time_elapsed =
464       screen->get_param(screen, PIPE_CAP_QUERY_TIME_ELAPSED);
465    st->has_half_float_packing =
466       screen->get_param(screen, PIPE_CAP_TGSI_PACK_HALF_FLOAT);
467    st->has_multi_draw_indirect =
468       screen->get_param(screen, PIPE_CAP_MULTI_DRAW_INDIRECT);
469 
470    st->has_hw_atomics =
471       screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
472                                PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS)
473       ? true : false;
474 
475    /* GL limits and extensions */
476    st_init_limits(pipe->screen, &ctx->Const, &ctx->Extensions, ctx->API);
477    st_init_extensions(pipe->screen, &ctx->Const,
478                       &ctx->Extensions, &st->options, ctx->API);
479 
480    if (st_have_perfmon(st)) {
481       ctx->Extensions.AMD_performance_monitor = GL_TRUE;
482    }
483 
484    /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
485    if (screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_UNCLAMPED)) {
486       if (!screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_CLAMPED)) {
487          st->clamp_vert_color_in_shader = GL_TRUE;
488       }
489 
490       if (!screen->get_param(screen, PIPE_CAP_FRAGMENT_COLOR_CLAMPED)) {
491          st->clamp_frag_color_in_shader = GL_TRUE;
492       }
493 
494       /* For drivers which cannot do color clamping, it's better to just
495        * disable ARB_color_buffer_float in the core profile, because
496        * the clamping is deprecated there anyway. */
497       if (ctx->API == API_OPENGL_CORE &&
498           (st->clamp_frag_color_in_shader || st->clamp_vert_color_in_shader)) {
499          st->clamp_vert_color_in_shader = GL_FALSE;
500          st->clamp_frag_color_in_shader = GL_FALSE;
501          ctx->Extensions.ARB_color_buffer_float = GL_FALSE;
502       }
503    }
504 
505    /* called after _mesa_create_context/_mesa_init_point, fix default user
506     * settable max point size up
507     */
508    ctx->Point.MaxSize = MAX2(ctx->Const.MaxPointSize,
509                              ctx->Const.MaxPointSizeAA);
510    /* For vertex shaders, make sure not to emit saturate when SM 3.0
511     * is not supported
512     */
513    ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitNoSat =
514       !st->has_shader_model3;
515 
516    if (!ctx->Extensions.ARB_gpu_shader5) {
517       for (i = 0; i < MESA_SHADER_STAGES; i++)
518          ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler = true;
519    }
520 
521    /* Set which shader types can be compiled at link time. */
522    st->shader_has_one_variant[MESA_SHADER_VERTEX] =
523          st->has_shareable_shaders &&
524          !st->clamp_vert_color_in_shader;
525 
526    st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
527          st->has_shareable_shaders &&
528          !st->clamp_frag_color_in_shader &&
529          !st->force_persample_in_shader;
530 
531    st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
532    st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] = st->has_shareable_shaders;
533    st->shader_has_one_variant[MESA_SHADER_GEOMETRY] = st->has_shareable_shaders;
534    st->shader_has_one_variant[MESA_SHADER_COMPUTE] = st->has_shareable_shaders;
535 
536    st->bitmap.cache.empty = true;
537 
538    _mesa_override_extensions(ctx);
539    _mesa_compute_version(ctx);
540 
541    if (ctx->Version == 0) {
542       /* This can happen when a core profile was requested, but the driver
543        * does not support some features of GL 3.1 or later.
544        */
545       st_destroy_context_priv(st, false);
546       return NULL;
547    }
548 
549    _mesa_initialize_dispatch_tables(ctx);
550    _mesa_initialize_vbo_vtxfmt(ctx);
551    st_init_driver_flags(st);
552 
553    /* Initialize context's winsys buffers list */
554    LIST_INITHEAD(&st->winsys_buffers);
555 
556    return st;
557 }
558 
559 
560 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)561 st_create_context(gl_api api, struct pipe_context *pipe,
562                   const struct gl_config *visual,
563                   struct st_context *share,
564                   const struct st_config_options *options,
565                   bool no_error)
566 {
567    struct gl_context *ctx;
568    struct gl_context *shareCtx = share ? share->ctx : NULL;
569    struct dd_function_table funcs;
570    struct st_context *st;
571 
572    memset(&funcs, 0, sizeof(funcs));
573    st_init_driver_functions(pipe->screen, &funcs);
574 
575    ctx = calloc(1, sizeof(struct gl_context));
576    if (!ctx)
577       return NULL;
578 
579    if (!_mesa_initialize_context(ctx, api, visual, shareCtx, &funcs)) {
580       free(ctx);
581       return NULL;
582    }
583 
584    st_debug_init();
585 
586    if (pipe->screen->get_disk_shader_cache &&
587        !(ST_DEBUG & DEBUG_TGSI))
588       ctx->Cache = pipe->screen->get_disk_shader_cache(pipe->screen);
589 
590    /* XXX: need a capability bit in gallium to query if the pipe
591     * driver prefers DP4 or MUL/MAD for vertex transformation.
592     */
593    if (debug_get_option_mesa_mvp_dp4())
594       ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = GL_TRUE;
595 
596    st = st_create_context_priv(ctx, pipe, options, no_error);
597    if (!st) {
598       _mesa_destroy_context(ctx);
599    }
600 
601    return st;
602 }
603 
604 
605 /**
606  * Callback to release the sampler view attached to a texture object.
607  * Called by _mesa_HashWalk().
608  */
609 static void
destroy_tex_sampler_cb(GLuint id,void * data,void * userData)610 destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
611 {
612    struct gl_texture_object *texObj = (struct gl_texture_object *) data;
613    struct st_context *st = (struct st_context *) userData;
614 
615    st_texture_release_sampler_view(st, st_texture_object(texObj));
616 }
617 
618 
619 void
st_destroy_context(struct st_context * st)620 st_destroy_context(struct st_context *st)
621 {
622    struct gl_context *ctx = st->ctx;
623    struct st_framebuffer *stfb, *next;
624 
625    GET_CURRENT_CONTEXT(curctx);
626 
627    if (curctx == NULL) {
628       /* No current context, but we need one to release
629        * renderbuffer surface when we release framebuffer.
630        * So temporarily bind the context.
631        */
632       _mesa_make_current(ctx, NULL, NULL);
633    }
634 
635    /* This must be called first so that glthread has a chance to finish */
636    _mesa_glthread_destroy(ctx);
637 
638    _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
639 
640    st_reference_fragprog(st, &st->fp, NULL);
641    st_reference_prog(st, &st->gp, NULL);
642    st_reference_vertprog(st, &st->vp, NULL);
643    st_reference_prog(st, &st->tcp, NULL);
644    st_reference_prog(st, &st->tep, NULL);
645    st_reference_compprog(st, &st->cp, NULL);
646 
647    /* release framebuffer in the winsys buffers list */
648    LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
649       st_framebuffer_reference(&stfb, NULL);
650    }
651 
652    pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
653    pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
654 
655    _vbo_DestroyContext(ctx);
656 
657    st_destroy_program_variants(st);
658 
659    _mesa_free_context_data(ctx);
660 
661    /* This will free the st_context too, so 'st' must not be accessed
662     * afterwards. */
663    st_destroy_context_priv(st, true);
664    st = NULL;
665 
666    free(ctx);
667 }
668 
669 
670 static void
st_emit_string_marker(struct gl_context * ctx,const GLchar * string,GLsizei len)671 st_emit_string_marker(struct gl_context *ctx, const GLchar *string, GLsizei len)
672 {
673    struct st_context *st = ctx->st;
674    st->pipe->emit_string_marker(st->pipe, string, len);
675 }
676 
677 
678 static void
st_set_background_context(struct gl_context * ctx,struct util_queue_monitoring * queue_info)679 st_set_background_context(struct gl_context *ctx,
680                           struct util_queue_monitoring *queue_info)
681 {
682    struct st_context *st = ctx->st;
683    struct st_manager *smapi =
684       (struct st_manager *) st->iface.st_context_private;
685 
686    assert(smapi->set_background_context);
687    smapi->set_background_context(&st->iface, queue_info);
688 }
689 
690 
691 static void
st_get_device_uuid(struct gl_context * ctx,char * uuid)692 st_get_device_uuid(struct gl_context *ctx, char *uuid)
693 {
694    struct pipe_screen *screen = st_context(ctx)->pipe->screen;
695 
696    assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
697    memset(uuid, 0, GL_UUID_SIZE_EXT);
698    screen->get_device_uuid(screen, uuid);
699 }
700 
701 
702 static void
st_get_driver_uuid(struct gl_context * ctx,char * uuid)703 st_get_driver_uuid(struct gl_context *ctx, char *uuid)
704 {
705    struct pipe_screen *screen = st_context(ctx)->pipe->screen;
706 
707    assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
708    memset(uuid, 0, GL_UUID_SIZE_EXT);
709    screen->get_driver_uuid(screen, uuid);
710 }
711 
712 
713 void
st_init_driver_functions(struct pipe_screen * screen,struct dd_function_table * functions)714 st_init_driver_functions(struct pipe_screen *screen,
715                          struct dd_function_table *functions)
716 {
717    _mesa_init_shader_object_functions(functions);
718    _mesa_init_sampler_object_functions(functions);
719 
720    st_init_blit_functions(functions);
721    st_init_bufferobject_functions(screen, functions);
722    st_init_clear_functions(functions);
723    st_init_bitmap_functions(functions);
724    st_init_copy_image_functions(functions);
725    st_init_drawpixels_functions(functions);
726    st_init_rasterpos_functions(functions);
727 
728    st_init_drawtex_functions(functions);
729 
730    st_init_eglimage_functions(functions);
731 
732    st_init_fbo_functions(functions);
733    st_init_feedback_functions(functions);
734    st_init_memoryobject_functions(functions);
735    st_init_msaa_functions(functions);
736    st_init_perfmon_functions(functions);
737    st_init_program_functions(functions);
738    st_init_query_functions(functions);
739    st_init_cond_render_functions(functions);
740    st_init_readpixels_functions(functions);
741    st_init_texture_functions(functions);
742    st_init_texture_barrier_functions(functions);
743    st_init_flush_functions(screen, functions);
744    st_init_string_functions(functions);
745    st_init_viewport_functions(functions);
746    st_init_compute_functions(functions);
747 
748    st_init_xformfb_functions(functions);
749    st_init_syncobj_functions(functions);
750 
751    st_init_vdpau_functions(functions);
752 
753    if (screen->get_param(screen, PIPE_CAP_STRING_MARKER))
754       functions->EmitStringMarker = st_emit_string_marker;
755 
756    functions->Enable = st_Enable;
757    functions->UpdateState = st_invalidate_state;
758    functions->QueryMemoryInfo = st_query_memory_info;
759    functions->SetBackgroundContext = st_set_background_context;
760    functions->GetDriverUuid = st_get_driver_uuid;
761    functions->GetDeviceUuid = st_get_device_uuid;
762 
763    /* GL_ARB_get_program_binary */
764    functions->GetProgramBinaryDriverSHA1 = st_get_program_binary_driver_sha1;
765    functions->ProgramBinarySerializeDriverBlob = st_serialise_tgsi_program;
766    functions->ProgramBinaryDeserializeDriverBlob = st_deserialise_tgsi_program;
767 }
768