st/mesa: implement "zombie" shaders list
[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 #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_semaphoreobjects.h"
64 #include "st_cb_texture.h"
65 #include "st_cb_xformfb.h"
66 #include "st_cb_flush.h"
67 #include "st_cb_syncobj.h"
68 #include "st_cb_strings.h"
69 #include "st_cb_texturebarrier.h"
70 #include "st_cb_viewport.h"
71 #include "st_atom.h"
72 #include "st_draw.h"
73 #include "st_extensions.h"
74 #include "st_gen_mipmap.h"
75 #include "st_pbo.h"
76 #include "st_program.h"
77 #include "st_sampler_view.h"
78 #include "st_shader_cache.h"
79 #include "st_vdpau.h"
80 #include "st_texture.h"
81 #include "st_util.h"
82 #include "pipe/p_context.h"
83 #include "util/u_cpu_detect.h"
84 #include "util/u_inlines.h"
85 #include "util/u_upload_mgr.h"
86 #include "util/u_vbuf.h"
87 #include "cso_cache/cso_context.h"
88
89
90 DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
91
92
93 /**
94 * Called via ctx->Driver.Enable()
95 */
96 static void
97 st_Enable(struct gl_context *ctx, GLenum cap, GLboolean state)
98 {
99 struct st_context *st = st_context(ctx);
100
101 switch (cap) {
102 case GL_DEBUG_OUTPUT:
103 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
104 st_update_debug_callback(st);
105 break;
106 default:
107 break;
108 }
109 }
110
111
112 /**
113 * Called via ctx->Driver.QueryMemoryInfo()
114 */
115 static void
116 st_query_memory_info(struct gl_context *ctx, struct gl_memory_info *out)
117 {
118 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
119 struct pipe_memory_info info;
120
121 assert(screen->query_memory_info);
122 if (!screen->query_memory_info)
123 return;
124
125 screen->query_memory_info(screen, &info);
126
127 out->total_device_memory = info.total_device_memory;
128 out->avail_device_memory = info.avail_device_memory;
129 out->total_staging_memory = info.total_staging_memory;
130 out->avail_staging_memory = info.avail_staging_memory;
131 out->device_memory_evicted = info.device_memory_evicted;
132 out->nr_device_memory_evictions = info.nr_device_memory_evictions;
133 }
134
135
136 static uint64_t
137 st_get_active_states(struct gl_context *ctx)
138 {
139 struct st_vertex_program *vp =
140 st_vertex_program(ctx->VertexProgram._Current);
141 struct st_common_program *tcp =
142 st_common_program(ctx->TessCtrlProgram._Current);
143 struct st_common_program *tep =
144 st_common_program(ctx->TessEvalProgram._Current);
145 struct st_common_program *gp =
146 st_common_program(ctx->GeometryProgram._Current);
147 struct st_fragment_program *fp =
148 st_fragment_program(ctx->FragmentProgram._Current);
149 struct st_compute_program *cp =
150 st_compute_program(ctx->ComputeProgram._Current);
151 uint64_t active_shader_states = 0;
152
153 if (vp)
154 active_shader_states |= vp->affected_states;
155 if (tcp)
156 active_shader_states |= tcp->affected_states;
157 if (tep)
158 active_shader_states |= tep->affected_states;
159 if (gp)
160 active_shader_states |= gp->affected_states;
161 if (fp)
162 active_shader_states |= fp->affected_states;
163 if (cp)
164 active_shader_states |= cp->affected_states;
165
166 /* Mark non-shader-resource shader states as "always active". */
167 return active_shader_states | ~ST_ALL_SHADER_RESOURCES;
168 }
169
170
171 void
172 st_invalidate_buffers(struct st_context *st)
173 {
174 st->dirty |= ST_NEW_BLEND |
175 ST_NEW_DSA |
176 ST_NEW_FB_STATE |
177 ST_NEW_SAMPLE_STATE |
178 ST_NEW_SAMPLE_SHADING |
179 ST_NEW_FS_STATE |
180 ST_NEW_POLY_STIPPLE |
181 ST_NEW_VIEWPORT |
182 ST_NEW_RASTERIZER |
183 ST_NEW_SCISSOR |
184 ST_NEW_WINDOW_RECTANGLES;
185 }
186
187
188 static inline bool
189 st_vp_uses_current_values(const struct gl_context *ctx)
190 {
191 const uint64_t inputs = ctx->VertexProgram._Current->info.inputs_read;
192 return _mesa_draw_current_bits(ctx) & inputs;
193 }
194
195
196 /**
197 * Called via ctx->Driver.UpdateState()
198 */
199 static void
200 st_invalidate_state(struct gl_context *ctx)
201 {
202 GLbitfield new_state = ctx->NewState;
203 struct st_context *st = st_context(ctx);
204
205 if (new_state & _NEW_BUFFERS) {
206 st_invalidate_buffers(st);
207 } else {
208 /* These set a subset of flags set by _NEW_BUFFERS, so we only have to
209 * check them when _NEW_BUFFERS isn't set.
210 */
211 if (new_state & _NEW_PROGRAM)
212 st->dirty |= ST_NEW_RASTERIZER;
213
214 if (new_state & _NEW_FOG)
215 st->dirty |= ST_NEW_FS_STATE;
216
217 if (new_state & _NEW_FRAG_CLAMP) {
218 if (st->clamp_frag_color_in_shader)
219 st->dirty |= ST_NEW_FS_STATE;
220 else
221 st->dirty |= ST_NEW_RASTERIZER;
222 }
223 }
224
225 if (new_state & (_NEW_LIGHT |
226 _NEW_POINT))
227 st->dirty |= ST_NEW_RASTERIZER;
228
229 if (new_state & _NEW_PROJECTION &&
230 st_user_clip_planes_enabled(ctx))
231 st->dirty |= ST_NEW_CLIP_STATE;
232
233 if (new_state & _NEW_PIXEL)
234 st->dirty |= ST_NEW_PIXEL_TRANSFER;
235
236 if (new_state & _NEW_CURRENT_ATTRIB && st_vp_uses_current_values(ctx))
237 st->dirty |= ST_NEW_VERTEX_ARRAYS;
238
239 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
240 if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT))
241 st->dirty |= ST_NEW_VS_STATE;
242
243 /* Which shaders are dirty will be determined manually. */
244 if (new_state & _NEW_PROGRAM) {
245 st->gfx_shaders_may_be_dirty = true;
246 st->compute_shader_may_be_dirty = true;
247 /* This will mask out unused shader resources. */
248 st->active_states = st_get_active_states(ctx);
249 }
250
251 if (new_state & _NEW_TEXTURE_OBJECT) {
252 st->dirty |= st->active_states &
253 (ST_NEW_SAMPLER_VIEWS |
254 ST_NEW_SAMPLERS |
255 ST_NEW_IMAGE_UNITS);
256 if (ctx->FragmentProgram._Current &&
257 ctx->FragmentProgram._Current->ExternalSamplersUsed) {
258 st->dirty |= ST_NEW_FS_STATE;
259 }
260 }
261 }
262
263
264 /*
265 * In some circumstances (such as running google-chrome) the state
266 * tracker may try to delete a resource view from a context different
267 * than when it was created. We don't want to do that.
268 *
269 * In that situation, st_texture_release_all_sampler_views() calls this
270 * function to transfer the sampler view reference to this context (expected
271 * to be the context which created the view.)
272 */
273 void
274 st_save_zombie_sampler_view(struct st_context *st,
275 struct pipe_sampler_view *view)
276 {
277 struct st_zombie_sampler_view_node *entry;
278
279 assert(view->context == st->pipe);
280
281 entry = MALLOC_STRUCT(st_zombie_sampler_view_node);
282 if (!entry)
283 return;
284
285 entry->view = view;
286
287 /* We need a mutex since this function may be called from one thread
288 * while free_zombie_resource_views() is called from another.
289 */
290 mtx_lock(&st->zombie_sampler_views.mutex);
291 LIST_ADDTAIL(&entry->node, &st->zombie_sampler_views.list.node);
292 mtx_unlock(&st->zombie_sampler_views.mutex);
293 }
294
295
296 /*
297 * Since OpenGL shaders may be shared among contexts, we can wind up
298 * with variants of a shader created with different contexts.
299 * When we go to destroy a gallium shader, we want to free it with the
300 * same context that it was created with, unless the driver reports
301 * PIPE_CAP_SHAREABLE_SHADERS = TRUE.
302 */
303 void
304 st_save_zombie_shader(struct st_context *st,
305 enum pipe_shader_type type,
306 struct pipe_shader_state *shader)
307 {
308 struct st_zombie_shader_node *entry;
309
310 /* we shouldn't be here if the driver supports shareable shaders */
311 assert(!st->has_shareable_shaders);
312
313 entry = MALLOC_STRUCT(st_zombie_shader_node);
314 if (!entry)
315 return;
316
317 entry->shader = shader;
318 entry->type = type;
319
320 /* We need a mutex since this function may be called from one thread
321 * while free_zombie_shaders() is called from another.
322 */
323 mtx_lock(&st->zombie_shaders.mutex);
324 LIST_ADDTAIL(&entry->node, &st->zombie_shaders.list.node);
325 mtx_unlock(&st->zombie_shaders.mutex);
326 }
327
328
329 /*
330 * Free any zombie sampler views that may be attached to this context.
331 */
332 static void
333 free_zombie_sampler_views(struct st_context *st)
334 {
335 struct st_zombie_sampler_view_node *entry, *next;
336
337 if (LIST_IS_EMPTY(&st->zombie_sampler_views.list.node)) {
338 return;
339 }
340
341 mtx_lock(&st->zombie_sampler_views.mutex);
342
343 LIST_FOR_EACH_ENTRY_SAFE(entry, next,
344 &st->zombie_sampler_views.list.node, node) {
345 LIST_DEL(&entry->node); // remove this entry from the list
346
347 assert(entry->view->context == st->pipe);
348 pipe_sampler_view_reference(&entry->view, NULL);
349
350 free(entry);
351 }
352
353 assert(LIST_IS_EMPTY(&st->zombie_sampler_views.list.node));
354
355 mtx_unlock(&st->zombie_sampler_views.mutex);
356 }
357
358
359 /*
360 * Free any zombie shaders that may be attached to this context.
361 */
362 static void
363 free_zombie_shaders(struct st_context *st)
364 {
365 struct st_zombie_shader_node *entry, *next;
366
367 if (LIST_IS_EMPTY(&st->zombie_shaders.list.node)) {
368 return;
369 }
370
371 mtx_lock(&st->zombie_shaders.mutex);
372
373 LIST_FOR_EACH_ENTRY_SAFE(entry, next,
374 &st->zombie_shaders.list.node, node) {
375 LIST_DEL(&entry->node); // remove this entry from the list
376
377 switch (entry->type) {
378 case PIPE_SHADER_VERTEX:
379 cso_delete_vertex_shader(st->cso_context, entry->shader);
380 break;
381 case PIPE_SHADER_FRAGMENT:
382 cso_delete_fragment_shader(st->cso_context, entry->shader);
383 break;
384 case PIPE_SHADER_GEOMETRY:
385 cso_delete_geometry_shader(st->cso_context, entry->shader);
386 break;
387 case PIPE_SHADER_TESS_CTRL:
388 cso_delete_tessctrl_shader(st->cso_context, entry->shader);
389 break;
390 case PIPE_SHADER_TESS_EVAL:
391 cso_delete_tesseval_shader(st->cso_context, entry->shader);
392 break;
393 case PIPE_SHADER_COMPUTE:
394 cso_delete_compute_shader(st->cso_context, entry->shader);
395 break;
396 default:
397 unreachable("invalid shader type in free_zombie_shaders()");
398 }
399 free(entry);
400 }
401
402 assert(LIST_IS_EMPTY(&st->zombie_shaders.list.node));
403
404 mtx_unlock(&st->zombie_shaders.mutex);
405 }
406
407
408 /*
409 * This function is called periodically to free any zombie objects
410 * which are attached to this context.
411 */
412 void
413 st_context_free_zombie_objects(struct st_context *st)
414 {
415 free_zombie_sampler_views(st);
416 free_zombie_shaders(st);
417 }
418
419
420 static void
421 st_destroy_context_priv(struct st_context *st, bool destroy_pipe)
422 {
423 uint i;
424
425 st_destroy_atoms(st);
426 st_destroy_draw(st);
427 st_destroy_clear(st);
428 st_destroy_bitmap(st);
429 st_destroy_drawpix(st);
430 st_destroy_drawtex(st);
431 st_destroy_perfmon(st);
432 st_destroy_pbo_helpers(st);
433 st_destroy_bound_texture_handles(st);
434 st_destroy_bound_image_handles(st);
435
436 for (i = 0; i < ARRAY_SIZE(st->state.frag_sampler_views); i++) {
437 pipe_sampler_view_release(st->pipe,
438 &st->state.frag_sampler_views[i]);
439 }
440
441 /* free glReadPixels cache data */
442 st_invalidate_readpix_cache(st);
443 util_throttle_deinit(st->pipe->screen, &st->throttle);
444
445 cso_destroy_context(st->cso_context);
446
447 if (st->pipe && destroy_pipe)
448 st->pipe->destroy(st->pipe);
449
450 free(st);
451 }
452
453
454 static void
455 st_init_driver_flags(struct st_context *st)
456 {
457 struct gl_driver_flags *f = &st->ctx->DriverFlags;
458
459 f->NewArray = ST_NEW_VERTEX_ARRAYS;
460 f->NewRasterizerDiscard = ST_NEW_RASTERIZER;
461 f->NewTileRasterOrder = ST_NEW_RASTERIZER;
462 f->NewUniformBuffer = ST_NEW_UNIFORM_BUFFER;
463 f->NewDefaultTessLevels = ST_NEW_TESS_STATE;
464
465 /* Shader resources */
466 f->NewTextureBuffer = ST_NEW_SAMPLER_VIEWS;
467 if (st->has_hw_atomics)
468 f->NewAtomicBuffer = ST_NEW_HW_ATOMICS | ST_NEW_CS_ATOMICS;
469 else
470 f->NewAtomicBuffer = ST_NEW_ATOMIC_BUFFER;
471 f->NewShaderStorageBuffer = ST_NEW_STORAGE_BUFFER;
472 f->NewImageUnits = ST_NEW_IMAGE_UNITS;
473
474 f->NewShaderConstants[MESA_SHADER_VERTEX] = ST_NEW_VS_CONSTANTS;
475 f->NewShaderConstants[MESA_SHADER_TESS_CTRL] = ST_NEW_TCS_CONSTANTS;
476 f->NewShaderConstants[MESA_SHADER_TESS_EVAL] = ST_NEW_TES_CONSTANTS;
477 f->NewShaderConstants[MESA_SHADER_GEOMETRY] = ST_NEW_GS_CONSTANTS;
478 f->NewShaderConstants[MESA_SHADER_FRAGMENT] = ST_NEW_FS_CONSTANTS;
479 f->NewShaderConstants[MESA_SHADER_COMPUTE] = ST_NEW_CS_CONSTANTS;
480
481 f->NewWindowRectangles = ST_NEW_WINDOW_RECTANGLES;
482 f->NewFramebufferSRGB = ST_NEW_FB_STATE;
483 f->NewScissorRect = ST_NEW_SCISSOR;
484 f->NewScissorTest = ST_NEW_SCISSOR | ST_NEW_RASTERIZER;
485 f->NewAlphaTest = ST_NEW_DSA;
486 f->NewBlend = ST_NEW_BLEND;
487 f->NewBlendColor = ST_NEW_BLEND_COLOR;
488 f->NewColorMask = ST_NEW_BLEND;
489 f->NewDepth = ST_NEW_DSA;
490 f->NewLogicOp = ST_NEW_BLEND;
491 f->NewStencil = ST_NEW_DSA;
492 f->NewMultisampleEnable = ST_NEW_BLEND | ST_NEW_RASTERIZER |
493 ST_NEW_SAMPLE_STATE | ST_NEW_SAMPLE_SHADING;
494 f->NewSampleAlphaToXEnable = ST_NEW_BLEND;
495 f->NewSampleMask = ST_NEW_SAMPLE_STATE;
496 f->NewSampleLocations = ST_NEW_SAMPLE_STATE;
497 f->NewSampleShading = ST_NEW_SAMPLE_SHADING;
498
499 /* This depends on what the gallium driver wants. */
500 if (st->force_persample_in_shader) {
501 f->NewMultisampleEnable |= ST_NEW_FS_STATE;
502 f->NewSampleShading |= ST_NEW_FS_STATE;
503 } else {
504 f->NewSampleShading |= ST_NEW_RASTERIZER;
505 }
506
507 f->NewClipControl = ST_NEW_VIEWPORT | ST_NEW_RASTERIZER;
508 f->NewClipPlane = ST_NEW_CLIP_STATE;
509 f->NewClipPlaneEnable = ST_NEW_RASTERIZER;
510 f->NewDepthClamp = ST_NEW_RASTERIZER;
511 f->NewLineState = ST_NEW_RASTERIZER;
512 f->NewPolygonState = ST_NEW_RASTERIZER;
513 f->NewPolygonStipple = ST_NEW_POLY_STIPPLE;
514 f->NewViewport = ST_NEW_VIEWPORT;
515 f->NewNvConservativeRasterization = ST_NEW_RASTERIZER;
516 f->NewNvConservativeRasterizationParams = ST_NEW_RASTERIZER;
517 }
518
519
520 static struct st_context *
521 st_create_context_priv(struct gl_context *ctx, struct pipe_context *pipe,
522 const struct st_config_options *options, bool no_error)
523 {
524 struct pipe_screen *screen = pipe->screen;
525 uint i;
526 struct st_context *st = ST_CALLOC_STRUCT( st_context);
527
528 st->options = *options;
529
530 ctx->st = st;
531
532 st->ctx = ctx;
533 st->pipe = pipe;
534
535 /* state tracker needs the VBO module */
536 _vbo_CreateContext(ctx);
537
538 st->dirty = ST_ALL_STATES_MASK;
539
540 st->can_bind_const_buffer_as_vertex =
541 screen->get_param(screen, PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX);
542
543 /* st/mesa always uploads zero-stride vertex attribs, and other user
544 * vertex buffers are only possible with a compatibility profile.
545 * So tell the u_vbuf module that user VBOs are not possible with the Core
546 * profile, so that u_vbuf is bypassed completely if there is nothing else
547 * to do.
548 */
549 unsigned vbuf_flags =
550 ctx->API == API_OPENGL_CORE ? U_VBUF_FLAG_NO_USER_VBOS : 0;
551 st->cso_context = cso_create_context(pipe, vbuf_flags);
552
553 st_init_atoms(st);
554 st_init_clear(st);
555 st_init_pbo_helpers(st);
556
557 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
558 if (pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
559 st->internal_target = PIPE_TEXTURE_2D;
560 else
561 st->internal_target = PIPE_TEXTURE_RECT;
562
563 /* Setup vertex element info for 'struct st_util_vertex'.
564 */
565 {
566 STATIC_ASSERT(sizeof(struct st_util_vertex) == 9 * sizeof(float));
567
568 memset(&st->util_velems, 0, sizeof(st->util_velems));
569 st->util_velems[0].src_offset = 0;
570 st->util_velems[0].vertex_buffer_index = 0;
571 st->util_velems[0].src_format = PIPE_FORMAT_R32G32B32_FLOAT;
572 st->util_velems[1].src_offset = 3 * sizeof(float);
573 st->util_velems[1].vertex_buffer_index = 0;
574 st->util_velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
575 st->util_velems[2].src_offset = 7 * sizeof(float);
576 st->util_velems[2].vertex_buffer_index = 0;
577 st->util_velems[2].src_format = PIPE_FORMAT_R32G32_FLOAT;
578 }
579
580 /* we want all vertex data to be placed in buffer objects */
581 vbo_use_buffer_objects(ctx);
582
583
584 /* make sure that no VBOs are left mapped when we're drawing. */
585 vbo_always_unmap_buffers(ctx);
586
587 /* Need these flags:
588 */
589 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
590
591 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
592
593 if (no_error)
594 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
595
596 ctx->Const.PackedDriverUniformStorage =
597 screen->get_param(screen, PIPE_CAP_PACKED_UNIFORMS);
598
599 st->has_stencil_export =
600 screen->get_param(screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
601 st->has_shader_model3 = screen->get_param(screen, PIPE_CAP_SM3);
602 st->has_etc1 = screen->is_format_supported(screen, PIPE_FORMAT_ETC1_RGB8,
603 PIPE_TEXTURE_2D, 0, 0,
604 PIPE_BIND_SAMPLER_VIEW);
605 st->has_etc2 = screen->is_format_supported(screen, PIPE_FORMAT_ETC2_RGB8,
606 PIPE_TEXTURE_2D, 0, 0,
607 PIPE_BIND_SAMPLER_VIEW);
608 st->has_astc_2d_ldr =
609 screen->is_format_supported(screen, PIPE_FORMAT_ASTC_4x4_SRGB,
610 PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW);
611 st->prefer_blit_based_texture_transfer = screen->get_param(screen,
612 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER);
613 st->force_persample_in_shader =
614 screen->get_param(screen, PIPE_CAP_SAMPLE_SHADING) &&
615 !screen->get_param(screen, PIPE_CAP_FORCE_PERSAMPLE_INTERP);
616 st->has_shareable_shaders = screen->get_param(screen,
617 PIPE_CAP_SHAREABLE_SHADERS);
618 st->needs_texcoord_semantic =
619 screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
620 st->apply_texture_swizzle_to_border_color =
621 !!(screen->get_param(screen, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK) &
622 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50 |
623 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600));
624 st->has_time_elapsed =
625 screen->get_param(screen, PIPE_CAP_QUERY_TIME_ELAPSED);
626 st->has_half_float_packing =
627 screen->get_param(screen, PIPE_CAP_TGSI_PACK_HALF_FLOAT);
628 st->has_multi_draw_indirect =
629 screen->get_param(screen, PIPE_CAP_MULTI_DRAW_INDIRECT);
630 st->has_single_pipe_stat =
631 screen->get_param(screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE);
632 st->has_indep_blend_func =
633 screen->get_param(screen, PIPE_CAP_INDEP_BLEND_FUNC);
634 st->needs_rgb_dst_alpha_override =
635 screen->get_param(screen, PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND);
636
637 st->has_hw_atomics =
638 screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
639 PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS)
640 ? true : false;
641
642 util_throttle_init(&st->throttle,
643 screen->get_param(screen,
644 PIPE_CAP_MAX_TEXTURE_UPLOAD_MEMORY_BUDGET));
645
646 /* GL limits and extensions */
647 st_init_limits(pipe->screen, &ctx->Const, &ctx->Extensions, ctx->API);
648 st_init_extensions(pipe->screen, &ctx->Const,
649 &ctx->Extensions, &st->options, ctx->API);
650
651 if (st_have_perfmon(st)) {
652 ctx->Extensions.AMD_performance_monitor = GL_TRUE;
653 }
654
655 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
656 if (screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_UNCLAMPED)) {
657 if (!screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_CLAMPED)) {
658 st->clamp_vert_color_in_shader = GL_TRUE;
659 }
660
661 if (!screen->get_param(screen, PIPE_CAP_FRAGMENT_COLOR_CLAMPED)) {
662 st->clamp_frag_color_in_shader = GL_TRUE;
663 }
664
665 /* For drivers which cannot do color clamping, it's better to just
666 * disable ARB_color_buffer_float in the core profile, because
667 * the clamping is deprecated there anyway. */
668 if (ctx->API == API_OPENGL_CORE &&
669 (st->clamp_frag_color_in_shader || st->clamp_vert_color_in_shader)) {
670 st->clamp_vert_color_in_shader = GL_FALSE;
671 st->clamp_frag_color_in_shader = GL_FALSE;
672 ctx->Extensions.ARB_color_buffer_float = GL_FALSE;
673 }
674 }
675
676 /* called after _mesa_create_context/_mesa_init_point, fix default user
677 * settable max point size up
678 */
679 ctx->Point.MaxSize = MAX2(ctx->Const.MaxPointSize,
680 ctx->Const.MaxPointSizeAA);
681 /* For vertex shaders, make sure not to emit saturate when SM 3.0
682 * is not supported
683 */
684 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitNoSat =
685 !st->has_shader_model3;
686
687 if (!ctx->Extensions.ARB_gpu_shader5) {
688 for (i = 0; i < MESA_SHADER_STAGES; i++)
689 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler = true;
690 }
691
692 /* Set which shader types can be compiled at link time. */
693 st->shader_has_one_variant[MESA_SHADER_VERTEX] =
694 st->has_shareable_shaders &&
695 !st->clamp_vert_color_in_shader;
696
697 st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
698 st->has_shareable_shaders &&
699 !st->clamp_frag_color_in_shader &&
700 !st->force_persample_in_shader;
701
702 st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
703 st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] = st->has_shareable_shaders;
704 st->shader_has_one_variant[MESA_SHADER_GEOMETRY] = st->has_shareable_shaders;
705 st->shader_has_one_variant[MESA_SHADER_COMPUTE] = st->has_shareable_shaders;
706
707 st->bitmap.cache.empty = true;
708
709 _mesa_override_extensions(ctx);
710 _mesa_compute_version(ctx);
711
712 if (ctx->Version == 0) {
713 /* This can happen when a core profile was requested, but the driver
714 * does not support some features of GL 3.1 or later.
715 */
716 st_destroy_context_priv(st, false);
717 return NULL;
718 }
719
720 _mesa_initialize_dispatch_tables(ctx);
721 _mesa_initialize_vbo_vtxfmt(ctx);
722 st_init_driver_flags(st);
723
724 /* Initialize context's winsys buffers list */
725 LIST_INITHEAD(&st->winsys_buffers);
726
727 LIST_INITHEAD(&st->zombie_sampler_views.list.node);
728 mtx_init(&st->zombie_sampler_views.mutex, mtx_plain);
729 LIST_INITHEAD(&st->zombie_shaders.list.node);
730 mtx_init(&st->zombie_shaders.mutex, mtx_plain);
731
732 return st;
733 }
734
735
736 static void
737 st_emit_string_marker(struct gl_context *ctx, const GLchar *string, GLsizei len)
738 {
739 struct st_context *st = ctx->st;
740 st->pipe->emit_string_marker(st->pipe, string, len);
741 }
742
743
744 static void
745 st_set_background_context(struct gl_context *ctx,
746 struct util_queue_monitoring *queue_info)
747 {
748 struct st_context *st = ctx->st;
749 struct st_manager *smapi =
750 (struct st_manager *) st->iface.st_context_private;
751
752 assert(smapi->set_background_context);
753 smapi->set_background_context(&st->iface, queue_info);
754 }
755
756
757 static void
758 st_get_device_uuid(struct gl_context *ctx, char *uuid)
759 {
760 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
761
762 assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
763 memset(uuid, 0, GL_UUID_SIZE_EXT);
764 screen->get_device_uuid(screen, uuid);
765 }
766
767
768 static void
769 st_get_driver_uuid(struct gl_context *ctx, char *uuid)
770 {
771 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
772
773 assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
774 memset(uuid, 0, GL_UUID_SIZE_EXT);
775 screen->get_driver_uuid(screen, uuid);
776 }
777
778
779 static void
780 st_init_driver_functions(struct pipe_screen *screen,
781 struct dd_function_table *functions)
782 {
783 _mesa_init_sampler_object_functions(functions);
784
785 st_init_draw_functions(functions);
786 st_init_blit_functions(functions);
787 st_init_bufferobject_functions(screen, functions);
788 st_init_clear_functions(functions);
789 st_init_bitmap_functions(functions);
790 st_init_copy_image_functions(functions);
791 st_init_drawpixels_functions(functions);
792 st_init_rasterpos_functions(functions);
793
794 st_init_drawtex_functions(functions);
795
796 st_init_eglimage_functions(functions);
797
798 st_init_fbo_functions(functions);
799 st_init_feedback_functions(functions);
800 st_init_memoryobject_functions(functions);
801 st_init_msaa_functions(functions);
802 st_init_perfmon_functions(functions);
803 st_init_program_functions(functions);
804 st_init_query_functions(functions);
805 st_init_cond_render_functions(functions);
806 st_init_readpixels_functions(functions);
807 st_init_semaphoreobject_functions(functions);
808 st_init_texture_functions(functions);
809 st_init_texture_barrier_functions(functions);
810 st_init_flush_functions(screen, functions);
811 st_init_string_functions(functions);
812 st_init_viewport_functions(functions);
813 st_init_compute_functions(functions);
814
815 st_init_xformfb_functions(functions);
816 st_init_syncobj_functions(functions);
817
818 st_init_vdpau_functions(functions);
819
820 if (screen->get_param(screen, PIPE_CAP_STRING_MARKER))
821 functions->EmitStringMarker = st_emit_string_marker;
822
823 functions->Enable = st_Enable;
824 functions->UpdateState = st_invalidate_state;
825 functions->QueryMemoryInfo = st_query_memory_info;
826 functions->SetBackgroundContext = st_set_background_context;
827 functions->GetDriverUuid = st_get_driver_uuid;
828 functions->GetDeviceUuid = st_get_device_uuid;
829
830 /* GL_ARB_get_program_binary */
831 functions->GetProgramBinaryDriverSHA1 = st_get_program_binary_driver_sha1;
832
833 enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir)
834 screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
835 PIPE_SHADER_CAP_PREFERRED_IR);
836 if (preferred_ir == PIPE_SHADER_IR_NIR) {
837 functions->ShaderCacheSerializeDriverBlob = st_serialise_nir_program;
838 functions->ProgramBinarySerializeDriverBlob =
839 st_serialise_nir_program_binary;
840 functions->ProgramBinaryDeserializeDriverBlob =
841 st_deserialise_nir_program;
842 } else {
843 functions->ShaderCacheSerializeDriverBlob = st_serialise_tgsi_program;
844 functions->ProgramBinarySerializeDriverBlob =
845 st_serialise_tgsi_program_binary;
846 functions->ProgramBinaryDeserializeDriverBlob =
847 st_deserialise_tgsi_program;
848 }
849 }
850
851
852 struct st_context *
853 st_create_context(gl_api api, struct pipe_context *pipe,
854 const struct gl_config *visual,
855 struct st_context *share,
856 const struct st_config_options *options,
857 bool no_error)
858 {
859 struct gl_context *ctx;
860 struct gl_context *shareCtx = share ? share->ctx : NULL;
861 struct dd_function_table funcs;
862 struct st_context *st;
863
864 util_cpu_detect();
865
866 memset(&funcs, 0, sizeof(funcs));
867 st_init_driver_functions(pipe->screen, &funcs);
868
869 ctx = calloc(1, sizeof(struct gl_context));
870 if (!ctx)
871 return NULL;
872
873 if (!_mesa_initialize_context(ctx, api, visual, shareCtx, &funcs)) {
874 free(ctx);
875 return NULL;
876 }
877
878 st_debug_init();
879
880 if (pipe->screen->get_disk_shader_cache &&
881 !(ST_DEBUG & DEBUG_TGSI))
882 ctx->Cache = pipe->screen->get_disk_shader_cache(pipe->screen);
883
884 /* XXX: need a capability bit in gallium to query if the pipe
885 * driver prefers DP4 or MUL/MAD for vertex transformation.
886 */
887 if (debug_get_option_mesa_mvp_dp4())
888 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = GL_TRUE;
889
890 st = st_create_context_priv(ctx, pipe, options, no_error);
891 if (!st) {
892 _mesa_destroy_context(ctx);
893 }
894
895 return st;
896 }
897
898
899 /**
900 * When we destroy a context, we must examine all texture objects to
901 * find/release any sampler views created by that context.
902 *
903 * This callback is called per-texture object. It releases all the
904 * texture's sampler views which belong to the context.
905 */
906 static void
907 destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
908 {
909 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
910 struct st_context *st = (struct st_context *) userData;
911
912 st_texture_release_context_sampler_view(st, st_texture_object(texObj));
913 }
914
915
916 void
917 st_destroy_context(struct st_context *st)
918 {
919 struct gl_context *ctx = st->ctx;
920 struct st_framebuffer *stfb, *next;
921
922 GET_CURRENT_CONTEXT(curctx);
923
924 if (curctx == NULL) {
925 /* No current context, but we need one to release
926 * renderbuffer surface when we release framebuffer.
927 * So temporarily bind the context.
928 */
929 _mesa_make_current(ctx, NULL, NULL);
930 }
931
932 st_context_free_zombie_objects(st);
933
934 mtx_destroy(&st->zombie_sampler_views.mutex);
935 mtx_destroy(&st->zombie_shaders.mutex);
936
937 /* This must be called first so that glthread has a chance to finish */
938 _mesa_glthread_destroy(ctx);
939
940 _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
941
942 st_reference_fragprog(st, &st->fp, NULL);
943 st_reference_prog(st, &st->gp, NULL);
944 st_reference_vertprog(st, &st->vp, NULL);
945 st_reference_prog(st, &st->tcp, NULL);
946 st_reference_prog(st, &st->tep, NULL);
947 st_reference_compprog(st, &st->cp, NULL);
948
949 /* release framebuffer in the winsys buffers list */
950 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
951 st_framebuffer_reference(&stfb, NULL);
952 }
953
954 pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
955 pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
956
957 _vbo_DestroyContext(ctx);
958
959 st_destroy_program_variants(st);
960
961 _mesa_free_context_data(ctx);
962
963 /* This will free the st_context too, so 'st' must not be accessed
964 * afterwards. */
965 st_destroy_context_priv(st, true);
966 st = NULL;
967
968 free(ctx);
969 }