1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
40 #include "glapi/glapi.h"
41 #include "st_manager.h"
42 #include "st_context.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"
73 #include "st_extensions.h"
74 #include "st_gen_mipmap.h"
76 #include "st_program.h"
77 #include "st_sampler_view.h"
78 #include "st_shader_cache.h"
80 #include "st_texture.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"
90 DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4
, "MESA_MVP_DP4", FALSE
)
94 * Called via ctx->Driver.Enable()
97 st_Enable(struct gl_context
*ctx
, GLenum cap
, GLboolean state
)
99 struct st_context
*st
= st_context(ctx
);
102 case GL_DEBUG_OUTPUT
:
103 case GL_DEBUG_OUTPUT_SYNCHRONOUS
:
104 st_update_debug_callback(st
);
113 * Called via ctx->Driver.QueryMemoryInfo()
116 st_query_memory_info(struct gl_context
*ctx
, struct gl_memory_info
*out
)
118 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
119 struct pipe_memory_info info
;
121 assert(screen
->query_memory_info
);
122 if (!screen
->query_memory_info
)
125 screen
->query_memory_info(screen
, &info
);
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
;
137 st_get_active_states(struct gl_context
*ctx
)
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;
154 active_shader_states
|= vp
->affected_states
;
156 active_shader_states
|= tcp
->affected_states
;
158 active_shader_states
|= tep
->affected_states
;
160 active_shader_states
|= gp
->affected_states
;
162 active_shader_states
|= fp
->affected_states
;
164 active_shader_states
|= cp
->affected_states
;
166 /* Mark non-shader-resource shader states as "always active". */
167 return active_shader_states
| ~ST_ALL_SHADER_RESOURCES
;
172 st_invalidate_buffers(struct st_context
*st
)
174 st
->dirty
|= ST_NEW_BLEND
|
177 ST_NEW_SAMPLE_STATE
|
178 ST_NEW_SAMPLE_SHADING
|
180 ST_NEW_POLY_STIPPLE
|
184 ST_NEW_WINDOW_RECTANGLES
;
189 st_vp_uses_current_values(const struct gl_context
*ctx
)
191 const uint64_t inputs
= ctx
->VertexProgram
._Current
->info
.inputs_read
;
192 return _mesa_draw_current_bits(ctx
) & inputs
;
197 * Called via ctx->Driver.UpdateState()
200 st_invalidate_state(struct gl_context
*ctx
)
202 GLbitfield new_state
= ctx
->NewState
;
203 struct st_context
*st
= st_context(ctx
);
205 if (new_state
& _NEW_BUFFERS
) {
206 st_invalidate_buffers(st
);
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.
211 if (new_state
& _NEW_PROGRAM
)
212 st
->dirty
|= ST_NEW_RASTERIZER
;
214 if (new_state
& _NEW_FOG
)
215 st
->dirty
|= ST_NEW_FS_STATE
;
217 if (new_state
& _NEW_FRAG_CLAMP
) {
218 if (st
->clamp_frag_color_in_shader
)
219 st
->dirty
|= ST_NEW_FS_STATE
;
221 st
->dirty
|= ST_NEW_RASTERIZER
;
225 if (new_state
& (_NEW_LIGHT
|
227 st
->dirty
|= ST_NEW_RASTERIZER
;
229 if (new_state
& _NEW_PROJECTION
&&
230 st_user_clip_planes_enabled(ctx
))
231 st
->dirty
|= ST_NEW_CLIP_STATE
;
233 if (new_state
& _NEW_PIXEL
)
234 st
->dirty
|= ST_NEW_PIXEL_TRANSFER
;
236 if (new_state
& _NEW_CURRENT_ATTRIB
&& st_vp_uses_current_values(ctx
))
237 st
->dirty
|= ST_NEW_VERTEX_ARRAYS
;
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
;
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
);
251 if (new_state
& _NEW_TEXTURE_OBJECT
) {
252 st
->dirty
|= st
->active_states
&
253 (ST_NEW_SAMPLER_VIEWS
|
256 if (ctx
->FragmentProgram
._Current
&&
257 ctx
->FragmentProgram
._Current
->ExternalSamplersUsed
) {
258 st
->dirty
|= ST_NEW_FS_STATE
;
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.
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.)
274 st_save_zombie_sampler_view(struct st_context
*st
,
275 struct pipe_sampler_view
*view
)
277 struct st_zombie_sampler_view_node
*entry
;
279 assert(view
->context
== st
->pipe
);
281 entry
= MALLOC_STRUCT(st_zombie_sampler_view_node
);
287 /* We need a mutex since this function may be called from one thread
288 * while free_zombie_resource_views() is called from another.
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
);
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.
304 st_save_zombie_shader(struct st_context
*st
,
305 enum pipe_shader_type type
,
306 struct pipe_shader_state
*shader
)
308 struct st_zombie_shader_node
*entry
;
310 /* we shouldn't be here if the driver supports shareable shaders */
311 assert(!st
->has_shareable_shaders
);
313 entry
= MALLOC_STRUCT(st_zombie_shader_node
);
317 entry
->shader
= shader
;
320 /* We need a mutex since this function may be called from one thread
321 * while free_zombie_shaders() is called from another.
323 mtx_lock(&st
->zombie_shaders
.mutex
);
324 LIST_ADDTAIL(&entry
->node
, &st
->zombie_shaders
.list
.node
);
325 mtx_unlock(&st
->zombie_shaders
.mutex
);
330 * Free any zombie sampler views that may be attached to this context.
333 free_zombie_sampler_views(struct st_context
*st
)
335 struct st_zombie_sampler_view_node
*entry
, *next
;
337 if (LIST_IS_EMPTY(&st
->zombie_sampler_views
.list
.node
)) {
341 mtx_lock(&st
->zombie_sampler_views
.mutex
);
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
347 assert(entry
->view
->context
== st
->pipe
);
348 pipe_sampler_view_reference(&entry
->view
, NULL
);
353 assert(LIST_IS_EMPTY(&st
->zombie_sampler_views
.list
.node
));
355 mtx_unlock(&st
->zombie_sampler_views
.mutex
);
360 * Free any zombie shaders that may be attached to this context.
363 free_zombie_shaders(struct st_context
*st
)
365 struct st_zombie_shader_node
*entry
, *next
;
367 if (LIST_IS_EMPTY(&st
->zombie_shaders
.list
.node
)) {
371 mtx_lock(&st
->zombie_shaders
.mutex
);
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
377 switch (entry
->type
) {
378 case PIPE_SHADER_VERTEX
:
379 cso_delete_vertex_shader(st
->cso_context
, entry
->shader
);
381 case PIPE_SHADER_FRAGMENT
:
382 cso_delete_fragment_shader(st
->cso_context
, entry
->shader
);
384 case PIPE_SHADER_GEOMETRY
:
385 cso_delete_geometry_shader(st
->cso_context
, entry
->shader
);
387 case PIPE_SHADER_TESS_CTRL
:
388 cso_delete_tessctrl_shader(st
->cso_context
, entry
->shader
);
390 case PIPE_SHADER_TESS_EVAL
:
391 cso_delete_tesseval_shader(st
->cso_context
, entry
->shader
);
393 case PIPE_SHADER_COMPUTE
:
394 cso_delete_compute_shader(st
->cso_context
, entry
->shader
);
397 unreachable("invalid shader type in free_zombie_shaders()");
402 assert(LIST_IS_EMPTY(&st
->zombie_shaders
.list
.node
));
404 mtx_unlock(&st
->zombie_shaders
.mutex
);
409 * This function is called periodically to free any zombie objects
410 * which are attached to this context.
413 st_context_free_zombie_objects(struct st_context
*st
)
415 free_zombie_sampler_views(st
);
416 free_zombie_shaders(st
);
421 st_destroy_context_priv(struct st_context
*st
, bool destroy_pipe
)
425 st_destroy_atoms(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
);
436 for (i
= 0; i
< ARRAY_SIZE(st
->state
.frag_sampler_views
); i
++) {
437 pipe_sampler_view_reference(&st
->state
.frag_sampler_views
[i
], NULL
);
440 /* free glReadPixels cache data */
441 st_invalidate_readpix_cache(st
);
442 util_throttle_deinit(st
->pipe
->screen
, &st
->throttle
);
444 cso_destroy_context(st
->cso_context
);
446 if (st
->pipe
&& destroy_pipe
)
447 st
->pipe
->destroy(st
->pipe
);
454 st_init_driver_flags(struct st_context
*st
)
456 struct gl_driver_flags
*f
= &st
->ctx
->DriverFlags
;
458 f
->NewArray
= ST_NEW_VERTEX_ARRAYS
;
459 f
->NewRasterizerDiscard
= ST_NEW_RASTERIZER
;
460 f
->NewTileRasterOrder
= ST_NEW_RASTERIZER
;
461 f
->NewUniformBuffer
= ST_NEW_UNIFORM_BUFFER
;
462 f
->NewDefaultTessLevels
= ST_NEW_TESS_STATE
;
464 /* Shader resources */
465 f
->NewTextureBuffer
= ST_NEW_SAMPLER_VIEWS
;
466 if (st
->has_hw_atomics
)
467 f
->NewAtomicBuffer
= ST_NEW_HW_ATOMICS
| ST_NEW_CS_ATOMICS
;
469 f
->NewAtomicBuffer
= ST_NEW_ATOMIC_BUFFER
;
470 f
->NewShaderStorageBuffer
= ST_NEW_STORAGE_BUFFER
;
471 f
->NewImageUnits
= ST_NEW_IMAGE_UNITS
;
473 f
->NewShaderConstants
[MESA_SHADER_VERTEX
] = ST_NEW_VS_CONSTANTS
;
474 f
->NewShaderConstants
[MESA_SHADER_TESS_CTRL
] = ST_NEW_TCS_CONSTANTS
;
475 f
->NewShaderConstants
[MESA_SHADER_TESS_EVAL
] = ST_NEW_TES_CONSTANTS
;
476 f
->NewShaderConstants
[MESA_SHADER_GEOMETRY
] = ST_NEW_GS_CONSTANTS
;
477 f
->NewShaderConstants
[MESA_SHADER_FRAGMENT
] = ST_NEW_FS_CONSTANTS
;
478 f
->NewShaderConstants
[MESA_SHADER_COMPUTE
] = ST_NEW_CS_CONSTANTS
;
480 f
->NewWindowRectangles
= ST_NEW_WINDOW_RECTANGLES
;
481 f
->NewFramebufferSRGB
= ST_NEW_FB_STATE
;
482 f
->NewScissorRect
= ST_NEW_SCISSOR
;
483 f
->NewScissorTest
= ST_NEW_SCISSOR
| ST_NEW_RASTERIZER
;
484 f
->NewAlphaTest
= ST_NEW_DSA
;
485 f
->NewBlend
= ST_NEW_BLEND
;
486 f
->NewBlendColor
= ST_NEW_BLEND_COLOR
;
487 f
->NewColorMask
= ST_NEW_BLEND
;
488 f
->NewDepth
= ST_NEW_DSA
;
489 f
->NewLogicOp
= ST_NEW_BLEND
;
490 f
->NewStencil
= ST_NEW_DSA
;
491 f
->NewMultisampleEnable
= ST_NEW_BLEND
| ST_NEW_RASTERIZER
|
492 ST_NEW_SAMPLE_STATE
| ST_NEW_SAMPLE_SHADING
;
493 f
->NewSampleAlphaToXEnable
= ST_NEW_BLEND
;
494 f
->NewSampleMask
= ST_NEW_SAMPLE_STATE
;
495 f
->NewSampleLocations
= ST_NEW_SAMPLE_STATE
;
496 f
->NewSampleShading
= ST_NEW_SAMPLE_SHADING
;
498 /* This depends on what the gallium driver wants. */
499 if (st
->force_persample_in_shader
) {
500 f
->NewMultisampleEnable
|= ST_NEW_FS_STATE
;
501 f
->NewSampleShading
|= ST_NEW_FS_STATE
;
503 f
->NewSampleShading
|= ST_NEW_RASTERIZER
;
506 f
->NewClipControl
= ST_NEW_VIEWPORT
| ST_NEW_RASTERIZER
;
507 f
->NewClipPlane
= ST_NEW_CLIP_STATE
;
508 f
->NewClipPlaneEnable
= ST_NEW_RASTERIZER
;
509 f
->NewDepthClamp
= ST_NEW_RASTERIZER
;
510 f
->NewLineState
= ST_NEW_RASTERIZER
;
511 f
->NewPolygonState
= ST_NEW_RASTERIZER
;
512 f
->NewPolygonStipple
= ST_NEW_POLY_STIPPLE
;
513 f
->NewViewport
= ST_NEW_VIEWPORT
;
514 f
->NewNvConservativeRasterization
= ST_NEW_RASTERIZER
;
515 f
->NewNvConservativeRasterizationParams
= ST_NEW_RASTERIZER
;
519 static struct st_context
*
520 st_create_context_priv(struct gl_context
*ctx
, struct pipe_context
*pipe
,
521 const struct st_config_options
*options
, bool no_error
)
523 struct pipe_screen
*screen
= pipe
->screen
;
525 struct st_context
*st
= ST_CALLOC_STRUCT( st_context
);
527 st
->options
= *options
;
534 /* state tracker needs the VBO module */
535 _vbo_CreateContext(ctx
);
537 st
->dirty
= ST_ALL_STATES_MASK
;
539 st
->can_bind_const_buffer_as_vertex
=
540 screen
->get_param(screen
, PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX
);
542 /* st/mesa always uploads zero-stride vertex attribs, and other user
543 * vertex buffers are only possible with a compatibility profile.
544 * So tell the u_vbuf module that user VBOs are not possible with the Core
545 * profile, so that u_vbuf is bypassed completely if there is nothing else
548 unsigned vbuf_flags
=
549 ctx
->API
== API_OPENGL_CORE
? U_VBUF_FLAG_NO_USER_VBOS
: 0;
550 st
->cso_context
= cso_create_context(pipe
, vbuf_flags
);
554 st_init_pbo_helpers(st
);
556 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
557 if (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_NPOT_TEXTURES
))
558 st
->internal_target
= PIPE_TEXTURE_2D
;
560 st
->internal_target
= PIPE_TEXTURE_RECT
;
562 /* Setup vertex element info for 'struct st_util_vertex'.
565 STATIC_ASSERT(sizeof(struct st_util_vertex
) == 9 * sizeof(float));
567 memset(&st
->util_velems
, 0, sizeof(st
->util_velems
));
568 st
->util_velems
[0].src_offset
= 0;
569 st
->util_velems
[0].vertex_buffer_index
= 0;
570 st
->util_velems
[0].src_format
= PIPE_FORMAT_R32G32B32_FLOAT
;
571 st
->util_velems
[1].src_offset
= 3 * sizeof(float);
572 st
->util_velems
[1].vertex_buffer_index
= 0;
573 st
->util_velems
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
574 st
->util_velems
[2].src_offset
= 7 * sizeof(float);
575 st
->util_velems
[2].vertex_buffer_index
= 0;
576 st
->util_velems
[2].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
579 /* we want all vertex data to be placed in buffer objects */
580 vbo_use_buffer_objects(ctx
);
583 /* make sure that no VBOs are left mapped when we're drawing. */
584 vbo_always_unmap_buffers(ctx
);
588 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
590 ctx
->VertexProgram
._MaintainTnlProgram
= GL_TRUE
;
593 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
;
595 ctx
->Const
.PackedDriverUniformStorage
=
596 screen
->get_param(screen
, PIPE_CAP_PACKED_UNIFORMS
);
598 st
->has_stencil_export
=
599 screen
->get_param(screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
);
600 st
->has_shader_model3
= screen
->get_param(screen
, PIPE_CAP_SM3
);
601 st
->has_etc1
= screen
->is_format_supported(screen
, PIPE_FORMAT_ETC1_RGB8
,
602 PIPE_TEXTURE_2D
, 0, 0,
603 PIPE_BIND_SAMPLER_VIEW
);
604 st
->has_etc2
= screen
->is_format_supported(screen
, PIPE_FORMAT_ETC2_RGB8
,
605 PIPE_TEXTURE_2D
, 0, 0,
606 PIPE_BIND_SAMPLER_VIEW
);
607 st
->has_astc_2d_ldr
=
608 screen
->is_format_supported(screen
, PIPE_FORMAT_ASTC_4x4_SRGB
,
609 PIPE_TEXTURE_2D
, 0, 0, PIPE_BIND_SAMPLER_VIEW
);
610 st
->prefer_blit_based_texture_transfer
= screen
->get_param(screen
,
611 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER
);
612 st
->force_persample_in_shader
=
613 screen
->get_param(screen
, PIPE_CAP_SAMPLE_SHADING
) &&
614 !screen
->get_param(screen
, PIPE_CAP_FORCE_PERSAMPLE_INTERP
);
615 st
->has_shareable_shaders
= screen
->get_param(screen
,
616 PIPE_CAP_SHAREABLE_SHADERS
);
617 st
->needs_texcoord_semantic
=
618 screen
->get_param(screen
, PIPE_CAP_TGSI_TEXCOORD
);
619 st
->apply_texture_swizzle_to_border_color
=
620 !!(screen
->get_param(screen
, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK
) &
621 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50
|
622 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600
));
623 st
->has_time_elapsed
=
624 screen
->get_param(screen
, PIPE_CAP_QUERY_TIME_ELAPSED
);
625 st
->has_half_float_packing
=
626 screen
->get_param(screen
, PIPE_CAP_TGSI_PACK_HALF_FLOAT
);
627 st
->has_multi_draw_indirect
=
628 screen
->get_param(screen
, PIPE_CAP_MULTI_DRAW_INDIRECT
);
629 st
->has_single_pipe_stat
=
630 screen
->get_param(screen
, PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE
);
631 st
->has_indep_blend_func
=
632 screen
->get_param(screen
, PIPE_CAP_INDEP_BLEND_FUNC
);
633 st
->needs_rgb_dst_alpha_override
=
634 screen
->get_param(screen
, PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND
);
637 screen
->get_shader_param(screen
, PIPE_SHADER_FRAGMENT
,
638 PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS
)
641 util_throttle_init(&st
->throttle
,
642 screen
->get_param(screen
,
643 PIPE_CAP_MAX_TEXTURE_UPLOAD_MEMORY_BUDGET
));
645 /* GL limits and extensions */
646 st_init_limits(pipe
->screen
, &ctx
->Const
, &ctx
->Extensions
, ctx
->API
);
647 st_init_extensions(pipe
->screen
, &ctx
->Const
,
648 &ctx
->Extensions
, &st
->options
, ctx
->API
);
650 if (st_have_perfmon(st
)) {
651 ctx
->Extensions
.AMD_performance_monitor
= GL_TRUE
;
654 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
655 if (screen
->get_param(screen
, PIPE_CAP_VERTEX_COLOR_UNCLAMPED
)) {
656 if (!screen
->get_param(screen
, PIPE_CAP_VERTEX_COLOR_CLAMPED
)) {
657 st
->clamp_vert_color_in_shader
= GL_TRUE
;
660 if (!screen
->get_param(screen
, PIPE_CAP_FRAGMENT_COLOR_CLAMPED
)) {
661 st
->clamp_frag_color_in_shader
= GL_TRUE
;
664 /* For drivers which cannot do color clamping, it's better to just
665 * disable ARB_color_buffer_float in the core profile, because
666 * the clamping is deprecated there anyway. */
667 if (ctx
->API
== API_OPENGL_CORE
&&
668 (st
->clamp_frag_color_in_shader
|| st
->clamp_vert_color_in_shader
)) {
669 st
->clamp_vert_color_in_shader
= GL_FALSE
;
670 st
->clamp_frag_color_in_shader
= GL_FALSE
;
671 ctx
->Extensions
.ARB_color_buffer_float
= GL_FALSE
;
675 /* called after _mesa_create_context/_mesa_init_point, fix default user
676 * settable max point size up
678 ctx
->Point
.MaxSize
= MAX2(ctx
->Const
.MaxPointSize
,
679 ctx
->Const
.MaxPointSizeAA
);
680 /* For vertex shaders, make sure not to emit saturate when SM 3.0
683 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].EmitNoSat
=
684 !st
->has_shader_model3
;
686 if (!ctx
->Extensions
.ARB_gpu_shader5
) {
687 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
688 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectSampler
= true;
691 /* Set which shader types can be compiled at link time. */
692 st
->shader_has_one_variant
[MESA_SHADER_VERTEX
] =
693 st
->has_shareable_shaders
&&
694 !st
->clamp_vert_color_in_shader
;
696 st
->shader_has_one_variant
[MESA_SHADER_FRAGMENT
] =
697 st
->has_shareable_shaders
&&
698 !st
->clamp_frag_color_in_shader
&&
699 !st
->force_persample_in_shader
;
701 st
->shader_has_one_variant
[MESA_SHADER_TESS_CTRL
] = st
->has_shareable_shaders
;
702 st
->shader_has_one_variant
[MESA_SHADER_TESS_EVAL
] = st
->has_shareable_shaders
;
703 st
->shader_has_one_variant
[MESA_SHADER_GEOMETRY
] = st
->has_shareable_shaders
;
704 st
->shader_has_one_variant
[MESA_SHADER_COMPUTE
] = st
->has_shareable_shaders
;
706 st
->bitmap
.cache
.empty
= true;
708 _mesa_override_extensions(ctx
);
709 _mesa_compute_version(ctx
);
711 if (ctx
->Version
== 0) {
712 /* This can happen when a core profile was requested, but the driver
713 * does not support some features of GL 3.1 or later.
715 st_destroy_context_priv(st
, false);
719 _mesa_initialize_dispatch_tables(ctx
);
720 _mesa_initialize_vbo_vtxfmt(ctx
);
721 st_init_driver_flags(st
);
723 /* Initialize context's winsys buffers list */
724 LIST_INITHEAD(&st
->winsys_buffers
);
726 LIST_INITHEAD(&st
->zombie_sampler_views
.list
.node
);
727 mtx_init(&st
->zombie_sampler_views
.mutex
, mtx_plain
);
728 LIST_INITHEAD(&st
->zombie_shaders
.list
.node
);
729 mtx_init(&st
->zombie_shaders
.mutex
, mtx_plain
);
736 st_emit_string_marker(struct gl_context
*ctx
, const GLchar
*string
, GLsizei len
)
738 struct st_context
*st
= ctx
->st
;
739 st
->pipe
->emit_string_marker(st
->pipe
, string
, len
);
744 st_set_background_context(struct gl_context
*ctx
,
745 struct util_queue_monitoring
*queue_info
)
747 struct st_context
*st
= ctx
->st
;
748 struct st_manager
*smapi
=
749 (struct st_manager
*) st
->iface
.st_context_private
;
751 assert(smapi
->set_background_context
);
752 smapi
->set_background_context(&st
->iface
, queue_info
);
757 st_get_device_uuid(struct gl_context
*ctx
, char *uuid
)
759 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
761 assert(GL_UUID_SIZE_EXT
>= PIPE_UUID_SIZE
);
762 memset(uuid
, 0, GL_UUID_SIZE_EXT
);
763 screen
->get_device_uuid(screen
, uuid
);
768 st_get_driver_uuid(struct gl_context
*ctx
, char *uuid
)
770 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
772 assert(GL_UUID_SIZE_EXT
>= PIPE_UUID_SIZE
);
773 memset(uuid
, 0, GL_UUID_SIZE_EXT
);
774 screen
->get_driver_uuid(screen
, uuid
);
779 st_init_driver_functions(struct pipe_screen
*screen
,
780 struct dd_function_table
*functions
)
782 _mesa_init_sampler_object_functions(functions
);
784 st_init_draw_functions(functions
);
785 st_init_blit_functions(functions
);
786 st_init_bufferobject_functions(screen
, functions
);
787 st_init_clear_functions(functions
);
788 st_init_bitmap_functions(functions
);
789 st_init_copy_image_functions(functions
);
790 st_init_drawpixels_functions(functions
);
791 st_init_rasterpos_functions(functions
);
793 st_init_drawtex_functions(functions
);
795 st_init_eglimage_functions(functions
);
797 st_init_fbo_functions(functions
);
798 st_init_feedback_functions(functions
);
799 st_init_memoryobject_functions(functions
);
800 st_init_msaa_functions(functions
);
801 st_init_perfmon_functions(functions
);
802 st_init_program_functions(functions
);
803 st_init_query_functions(functions
);
804 st_init_cond_render_functions(functions
);
805 st_init_readpixels_functions(functions
);
806 st_init_semaphoreobject_functions(functions
);
807 st_init_texture_functions(functions
);
808 st_init_texture_barrier_functions(functions
);
809 st_init_flush_functions(screen
, functions
);
810 st_init_string_functions(functions
);
811 st_init_viewport_functions(functions
);
812 st_init_compute_functions(functions
);
814 st_init_xformfb_functions(functions
);
815 st_init_syncobj_functions(functions
);
817 st_init_vdpau_functions(functions
);
819 if (screen
->get_param(screen
, PIPE_CAP_STRING_MARKER
))
820 functions
->EmitStringMarker
= st_emit_string_marker
;
822 functions
->Enable
= st_Enable
;
823 functions
->UpdateState
= st_invalidate_state
;
824 functions
->QueryMemoryInfo
= st_query_memory_info
;
825 functions
->SetBackgroundContext
= st_set_background_context
;
826 functions
->GetDriverUuid
= st_get_driver_uuid
;
827 functions
->GetDeviceUuid
= st_get_device_uuid
;
829 /* GL_ARB_get_program_binary */
830 functions
->GetProgramBinaryDriverSHA1
= st_get_program_binary_driver_sha1
;
832 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
833 screen
->get_shader_param(screen
, PIPE_SHADER_VERTEX
,
834 PIPE_SHADER_CAP_PREFERRED_IR
);
835 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
836 functions
->ShaderCacheSerializeDriverBlob
= st_serialise_nir_program
;
837 functions
->ProgramBinarySerializeDriverBlob
=
838 st_serialise_nir_program_binary
;
839 functions
->ProgramBinaryDeserializeDriverBlob
=
840 st_deserialise_nir_program
;
842 functions
->ShaderCacheSerializeDriverBlob
= st_serialise_tgsi_program
;
843 functions
->ProgramBinarySerializeDriverBlob
=
844 st_serialise_tgsi_program_binary
;
845 functions
->ProgramBinaryDeserializeDriverBlob
=
846 st_deserialise_tgsi_program
;
852 st_create_context(gl_api api
, struct pipe_context
*pipe
,
853 const struct gl_config
*visual
,
854 struct st_context
*share
,
855 const struct st_config_options
*options
,
858 struct gl_context
*ctx
;
859 struct gl_context
*shareCtx
= share
? share
->ctx
: NULL
;
860 struct dd_function_table funcs
;
861 struct st_context
*st
;
865 memset(&funcs
, 0, sizeof(funcs
));
866 st_init_driver_functions(pipe
->screen
, &funcs
);
868 ctx
= calloc(1, sizeof(struct gl_context
));
872 if (!_mesa_initialize_context(ctx
, api
, visual
, shareCtx
, &funcs
)) {
879 if (pipe
->screen
->get_disk_shader_cache
&&
880 !(ST_DEBUG
& DEBUG_TGSI
))
881 ctx
->Cache
= pipe
->screen
->get_disk_shader_cache(pipe
->screen
);
883 /* XXX: need a capability bit in gallium to query if the pipe
884 * driver prefers DP4 or MUL/MAD for vertex transformation.
886 if (debug_get_option_mesa_mvp_dp4())
887 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].OptimizeForAOS
= GL_TRUE
;
889 st
= st_create_context_priv(ctx
, pipe
, options
, no_error
);
891 _mesa_destroy_context(ctx
);
899 * When we destroy a context, we must examine all texture objects to
900 * find/release any sampler views created by that context.
902 * This callback is called per-texture object. It releases all the
903 * texture's sampler views which belong to the context.
906 destroy_tex_sampler_cb(GLuint id
, void *data
, void *userData
)
908 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
909 struct st_context
*st
= (struct st_context
*) userData
;
911 st_texture_release_context_sampler_view(st
, st_texture_object(texObj
));
916 st_destroy_context(struct st_context
*st
)
918 struct gl_context
*ctx
= st
->ctx
;
919 struct st_framebuffer
*stfb
, *next
;
920 struct gl_framebuffer
*save_drawbuffer
;
921 struct gl_framebuffer
*save_readbuffer
;
923 /* Save the current context and draw/read buffers*/
924 GET_CURRENT_CONTEXT(save_ctx
);
926 save_drawbuffer
= save_ctx
->WinSysDrawBuffer
;
927 save_readbuffer
= save_ctx
->WinSysReadBuffer
;
929 save_drawbuffer
= save_readbuffer
= NULL
;
933 * We need to bind the context we're deleting so that
934 * _mesa_reference_texobj_() uses this context when deleting textures.
935 * Similarly for framebuffer objects, etc.
937 _mesa_make_current(ctx
, NULL
, NULL
);
939 /* This must be called first so that glthread has a chance to finish */
940 _mesa_glthread_destroy(ctx
);
942 _mesa_HashWalk(ctx
->Shared
->TexObjects
, destroy_tex_sampler_cb
, st
);
944 /* For the fallback textures, free any sampler views belonging to this
947 for (unsigned i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
948 struct st_texture_object
*stObj
=
949 st_texture_object(ctx
->Shared
->FallbackTex
[i
]);
951 st_texture_release_context_sampler_view(st
, stObj
);
955 st_context_free_zombie_objects(st
);
957 mtx_destroy(&st
->zombie_sampler_views
.mutex
);
958 mtx_destroy(&st
->zombie_shaders
.mutex
);
960 st_reference_fragprog(st
, &st
->fp
, NULL
);
961 st_reference_prog(st
, &st
->gp
, NULL
);
962 st_reference_vertprog(st
, &st
->vp
, NULL
);
963 st_reference_prog(st
, &st
->tcp
, NULL
);
964 st_reference_prog(st
, &st
->tep
, NULL
);
965 st_reference_compprog(st
, &st
->cp
, NULL
);
967 /* release framebuffer in the winsys buffers list */
968 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
969 st_framebuffer_reference(&stfb
, NULL
);
972 pipe_sampler_view_reference(&st
->pixel_xfer
.pixelmap_sampler_view
, NULL
);
973 pipe_resource_reference(&st
->pixel_xfer
.pixelmap_texture
, NULL
);
975 _vbo_DestroyContext(ctx
);
977 st_destroy_program_variants(st
);
979 _mesa_free_context_data(ctx
);
981 /* This will free the st_context too, so 'st' must not be accessed
983 st_destroy_context_priv(st
, true);
988 if (save_ctx
== ctx
) {
989 /* unbind the context we just deleted */
990 _mesa_make_current(NULL
, NULL
, NULL
);
992 /* Restore the current context and draw/read buffers (may be NULL) */
993 _mesa_make_current(save_ctx
, save_drawbuffer
, save_readbuffer
);