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