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"
88 #include "compiler/glsl/glsl_parser_extras.h"
91 DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4
, "MESA_MVP_DP4", FALSE
)
95 * Called via ctx->Driver.Enable()
98 st_Enable(struct gl_context
*ctx
, GLenum cap
, GLboolean state
)
100 struct st_context
*st
= st_context(ctx
);
103 case GL_DEBUG_OUTPUT
:
104 case GL_DEBUG_OUTPUT_SYNCHRONOUS
:
105 st_update_debug_callback(st
);
114 * Called via ctx->Driver.QueryMemoryInfo()
117 st_query_memory_info(struct gl_context
*ctx
, struct gl_memory_info
*out
)
119 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
120 struct pipe_memory_info info
;
122 assert(screen
->query_memory_info
);
123 if (!screen
->query_memory_info
)
126 screen
->query_memory_info(screen
, &info
);
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
;
138 st_get_active_states(struct gl_context
*ctx
)
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;
155 active_shader_states
|= vp
->affected_states
;
157 active_shader_states
|= tcp
->affected_states
;
159 active_shader_states
|= tep
->affected_states
;
161 active_shader_states
|= gp
->affected_states
;
163 active_shader_states
|= fp
->affected_states
;
165 active_shader_states
|= cp
->affected_states
;
167 /* Mark non-shader-resource shader states as "always active". */
168 return active_shader_states
| ~ST_ALL_SHADER_RESOURCES
;
173 st_invalidate_buffers(struct st_context
*st
)
175 st
->dirty
|= ST_NEW_BLEND
|
178 ST_NEW_SAMPLE_STATE
|
179 ST_NEW_SAMPLE_SHADING
|
181 ST_NEW_POLY_STIPPLE
|
185 ST_NEW_WINDOW_RECTANGLES
;
190 st_vp_uses_current_values(const struct gl_context
*ctx
)
192 const uint64_t inputs
= ctx
->VertexProgram
._Current
->info
.inputs_read
;
193 return _mesa_draw_current_bits(ctx
) & inputs
;
198 * Called via ctx->Driver.UpdateState()
201 st_invalidate_state(struct gl_context
*ctx
)
203 GLbitfield new_state
= ctx
->NewState
;
204 struct st_context
*st
= st_context(ctx
);
206 if (new_state
& _NEW_BUFFERS
) {
207 st_invalidate_buffers(st
);
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.
212 if (new_state
& _NEW_PROGRAM
)
213 st
->dirty
|= ST_NEW_RASTERIZER
;
215 if (new_state
& _NEW_FOG
)
216 st
->dirty
|= ST_NEW_FS_STATE
;
218 if (new_state
& _NEW_FRAG_CLAMP
) {
219 if (st
->clamp_frag_color_in_shader
)
220 st
->dirty
|= ST_NEW_FS_STATE
;
222 st
->dirty
|= ST_NEW_RASTERIZER
;
226 if (new_state
& (_NEW_LIGHT
|
228 st
->dirty
|= ST_NEW_RASTERIZER
;
230 if (new_state
& _NEW_PROJECTION
&&
231 st_user_clip_planes_enabled(ctx
))
232 st
->dirty
|= ST_NEW_CLIP_STATE
;
234 if (new_state
& _NEW_PIXEL
)
235 st
->dirty
|= ST_NEW_PIXEL_TRANSFER
;
237 if (new_state
& _NEW_CURRENT_ATTRIB
&& st_vp_uses_current_values(ctx
))
238 st
->dirty
|= ST_NEW_VERTEX_ARRAYS
;
240 if (st
->clamp_frag_depth_in_shader
&& (new_state
& _NEW_VIEWPORT
)) {
241 if (ctx
->GeometryProgram
._Current
)
242 st
->dirty
|= ST_NEW_GS_CONSTANTS
;
243 else if (ctx
->TessEvalProgram
._Current
)
244 st
->dirty
|= ST_NEW_TES_CONSTANTS
;
246 st
->dirty
|= ST_NEW_VS_CONSTANTS
;
247 st
->dirty
|= ST_NEW_FS_CONSTANTS
;
250 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
251 if (st
->clamp_vert_color_in_shader
&& (new_state
& _NEW_LIGHT
)) {
252 st
->dirty
|= ST_NEW_VS_STATE
;
253 if (st
->ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Version
>= 32) {
254 st
->dirty
|= ST_NEW_GS_STATE
| ST_NEW_TES_STATE
;
258 /* Which shaders are dirty will be determined manually. */
259 if (new_state
& _NEW_PROGRAM
) {
260 st
->gfx_shaders_may_be_dirty
= true;
261 st
->compute_shader_may_be_dirty
= true;
262 /* This will mask out unused shader resources. */
263 st
->active_states
= st_get_active_states(ctx
);
266 if (new_state
& _NEW_TEXTURE_OBJECT
) {
267 st
->dirty
|= st
->active_states
&
268 (ST_NEW_SAMPLER_VIEWS
|
271 if (ctx
->FragmentProgram
._Current
&&
272 ctx
->FragmentProgram
._Current
->ExternalSamplersUsed
) {
273 st
->dirty
|= ST_NEW_FS_STATE
;
280 * In some circumstances (such as running google-chrome) the state
281 * tracker may try to delete a resource view from a context different
282 * than when it was created. We don't want to do that.
284 * In that situation, st_texture_release_all_sampler_views() calls this
285 * function to transfer the sampler view reference to this context (expected
286 * to be the context which created the view.)
289 st_save_zombie_sampler_view(struct st_context
*st
,
290 struct pipe_sampler_view
*view
)
292 struct st_zombie_sampler_view_node
*entry
;
294 assert(view
->context
== st
->pipe
);
296 entry
= MALLOC_STRUCT(st_zombie_sampler_view_node
);
302 /* We need a mutex since this function may be called from one thread
303 * while free_zombie_resource_views() is called from another.
305 mtx_lock(&st
->zombie_sampler_views
.mutex
);
306 LIST_ADDTAIL(&entry
->node
, &st
->zombie_sampler_views
.list
.node
);
307 mtx_unlock(&st
->zombie_sampler_views
.mutex
);
312 * Since OpenGL shaders may be shared among contexts, we can wind up
313 * with variants of a shader created with different contexts.
314 * When we go to destroy a gallium shader, we want to free it with the
315 * same context that it was created with, unless the driver reports
316 * PIPE_CAP_SHAREABLE_SHADERS = TRUE.
319 st_save_zombie_shader(struct st_context
*st
,
320 enum pipe_shader_type type
,
321 struct pipe_shader_state
*shader
)
323 struct st_zombie_shader_node
*entry
;
325 /* we shouldn't be here if the driver supports shareable shaders */
326 assert(!st
->has_shareable_shaders
);
328 entry
= MALLOC_STRUCT(st_zombie_shader_node
);
332 entry
->shader
= shader
;
335 /* We need a mutex since this function may be called from one thread
336 * while free_zombie_shaders() is called from another.
338 mtx_lock(&st
->zombie_shaders
.mutex
);
339 LIST_ADDTAIL(&entry
->node
, &st
->zombie_shaders
.list
.node
);
340 mtx_unlock(&st
->zombie_shaders
.mutex
);
345 * Free any zombie sampler views that may be attached to this context.
348 free_zombie_sampler_views(struct st_context
*st
)
350 struct st_zombie_sampler_view_node
*entry
, *next
;
352 if (LIST_IS_EMPTY(&st
->zombie_sampler_views
.list
.node
)) {
356 mtx_lock(&st
->zombie_sampler_views
.mutex
);
358 LIST_FOR_EACH_ENTRY_SAFE(entry
, next
,
359 &st
->zombie_sampler_views
.list
.node
, node
) {
360 LIST_DEL(&entry
->node
); // remove this entry from the list
362 assert(entry
->view
->context
== st
->pipe
);
363 pipe_sampler_view_reference(&entry
->view
, NULL
);
368 assert(LIST_IS_EMPTY(&st
->zombie_sampler_views
.list
.node
));
370 mtx_unlock(&st
->zombie_sampler_views
.mutex
);
375 * Free any zombie shaders that may be attached to this context.
378 free_zombie_shaders(struct st_context
*st
)
380 struct st_zombie_shader_node
*entry
, *next
;
382 if (LIST_IS_EMPTY(&st
->zombie_shaders
.list
.node
)) {
386 mtx_lock(&st
->zombie_shaders
.mutex
);
388 LIST_FOR_EACH_ENTRY_SAFE(entry
, next
,
389 &st
->zombie_shaders
.list
.node
, node
) {
390 LIST_DEL(&entry
->node
); // remove this entry from the list
392 switch (entry
->type
) {
393 case PIPE_SHADER_VERTEX
:
394 cso_delete_vertex_shader(st
->cso_context
, entry
->shader
);
396 case PIPE_SHADER_FRAGMENT
:
397 cso_delete_fragment_shader(st
->cso_context
, entry
->shader
);
399 case PIPE_SHADER_GEOMETRY
:
400 cso_delete_geometry_shader(st
->cso_context
, entry
->shader
);
402 case PIPE_SHADER_TESS_CTRL
:
403 cso_delete_tessctrl_shader(st
->cso_context
, entry
->shader
);
405 case PIPE_SHADER_TESS_EVAL
:
406 cso_delete_tesseval_shader(st
->cso_context
, entry
->shader
);
408 case PIPE_SHADER_COMPUTE
:
409 cso_delete_compute_shader(st
->cso_context
, entry
->shader
);
412 unreachable("invalid shader type in free_zombie_shaders()");
417 assert(LIST_IS_EMPTY(&st
->zombie_shaders
.list
.node
));
419 mtx_unlock(&st
->zombie_shaders
.mutex
);
424 * This function is called periodically to free any zombie objects
425 * which are attached to this context.
428 st_context_free_zombie_objects(struct st_context
*st
)
430 free_zombie_sampler_views(st
);
431 free_zombie_shaders(st
);
436 st_destroy_context_priv(struct st_context
*st
, bool destroy_pipe
)
440 st_destroy_atoms(st
);
442 st_destroy_clear(st
);
443 st_destroy_bitmap(st
);
444 st_destroy_drawpix(st
);
445 st_destroy_drawtex(st
);
446 st_destroy_perfmon(st
);
447 st_destroy_pbo_helpers(st
);
448 st_destroy_bound_texture_handles(st
);
449 st_destroy_bound_image_handles(st
);
451 for (i
= 0; i
< ARRAY_SIZE(st
->state
.frag_sampler_views
); i
++) {
452 pipe_sampler_view_reference(&st
->state
.frag_sampler_views
[i
], NULL
);
455 /* free glReadPixels cache data */
456 st_invalidate_readpix_cache(st
);
457 util_throttle_deinit(st
->pipe
->screen
, &st
->throttle
);
459 cso_destroy_context(st
->cso_context
);
461 if (st
->pipe
&& destroy_pipe
)
462 st
->pipe
->destroy(st
->pipe
);
469 st_init_driver_flags(struct st_context
*st
)
471 struct gl_driver_flags
*f
= &st
->ctx
->DriverFlags
;
473 f
->NewArray
= ST_NEW_VERTEX_ARRAYS
;
474 f
->NewRasterizerDiscard
= ST_NEW_RASTERIZER
;
475 f
->NewTileRasterOrder
= ST_NEW_RASTERIZER
;
476 f
->NewUniformBuffer
= ST_NEW_UNIFORM_BUFFER
;
477 f
->NewDefaultTessLevels
= ST_NEW_TESS_STATE
;
479 /* Shader resources */
480 f
->NewTextureBuffer
= ST_NEW_SAMPLER_VIEWS
;
481 if (st
->has_hw_atomics
)
482 f
->NewAtomicBuffer
= ST_NEW_HW_ATOMICS
| ST_NEW_CS_ATOMICS
;
484 f
->NewAtomicBuffer
= ST_NEW_ATOMIC_BUFFER
;
485 f
->NewShaderStorageBuffer
= ST_NEW_STORAGE_BUFFER
;
486 f
->NewImageUnits
= ST_NEW_IMAGE_UNITS
;
488 f
->NewShaderConstants
[MESA_SHADER_VERTEX
] = ST_NEW_VS_CONSTANTS
;
489 f
->NewShaderConstants
[MESA_SHADER_TESS_CTRL
] = ST_NEW_TCS_CONSTANTS
;
490 f
->NewShaderConstants
[MESA_SHADER_TESS_EVAL
] = ST_NEW_TES_CONSTANTS
;
491 f
->NewShaderConstants
[MESA_SHADER_GEOMETRY
] = ST_NEW_GS_CONSTANTS
;
492 f
->NewShaderConstants
[MESA_SHADER_FRAGMENT
] = ST_NEW_FS_CONSTANTS
;
493 f
->NewShaderConstants
[MESA_SHADER_COMPUTE
] = ST_NEW_CS_CONSTANTS
;
495 f
->NewWindowRectangles
= ST_NEW_WINDOW_RECTANGLES
;
496 f
->NewFramebufferSRGB
= ST_NEW_FB_STATE
;
497 f
->NewScissorRect
= ST_NEW_SCISSOR
;
498 f
->NewScissorTest
= ST_NEW_SCISSOR
| ST_NEW_RASTERIZER
;
499 f
->NewAlphaTest
= ST_NEW_DSA
;
500 f
->NewBlend
= ST_NEW_BLEND
;
501 f
->NewBlendColor
= ST_NEW_BLEND_COLOR
;
502 f
->NewColorMask
= ST_NEW_BLEND
;
503 f
->NewDepth
= ST_NEW_DSA
;
504 f
->NewLogicOp
= ST_NEW_BLEND
;
505 f
->NewStencil
= ST_NEW_DSA
;
506 f
->NewMultisampleEnable
= ST_NEW_BLEND
| ST_NEW_RASTERIZER
|
507 ST_NEW_SAMPLE_STATE
| ST_NEW_SAMPLE_SHADING
;
508 f
->NewSampleAlphaToXEnable
= ST_NEW_BLEND
;
509 f
->NewSampleMask
= ST_NEW_SAMPLE_STATE
;
510 f
->NewSampleLocations
= ST_NEW_SAMPLE_STATE
;
511 f
->NewSampleShading
= ST_NEW_SAMPLE_SHADING
;
513 /* This depends on what the gallium driver wants. */
514 if (st
->force_persample_in_shader
) {
515 f
->NewMultisampleEnable
|= ST_NEW_FS_STATE
;
516 f
->NewSampleShading
|= ST_NEW_FS_STATE
;
518 f
->NewSampleShading
|= ST_NEW_RASTERIZER
;
521 f
->NewClipControl
= ST_NEW_VIEWPORT
| ST_NEW_RASTERIZER
;
522 f
->NewClipPlane
= ST_NEW_CLIP_STATE
;
523 f
->NewClipPlaneEnable
= ST_NEW_RASTERIZER
;
525 if (st
->clamp_frag_depth_in_shader
) {
526 f
->NewClipControl
|= ST_NEW_VS_STATE
| ST_NEW_GS_STATE
|
529 f
->NewDepthClamp
= ST_NEW_FS_STATE
| ST_NEW_VS_STATE
|
530 ST_NEW_GS_STATE
| ST_NEW_TES_STATE
;
532 f
->NewDepthClamp
= ST_NEW_RASTERIZER
;
535 f
->NewLineState
= ST_NEW_RASTERIZER
;
536 f
->NewPolygonState
= ST_NEW_RASTERIZER
;
537 f
->NewPolygonStipple
= ST_NEW_POLY_STIPPLE
;
538 f
->NewViewport
= ST_NEW_VIEWPORT
;
539 f
->NewNvConservativeRasterization
= ST_NEW_RASTERIZER
;
540 f
->NewNvConservativeRasterizationParams
= ST_NEW_RASTERIZER
;
541 f
->NewIntelConservativeRasterization
= ST_NEW_RASTERIZER
;
545 static struct st_context
*
546 st_create_context_priv(struct gl_context
*ctx
, struct pipe_context
*pipe
,
547 const struct st_config_options
*options
, bool no_error
)
549 struct pipe_screen
*screen
= pipe
->screen
;
551 struct st_context
*st
= ST_CALLOC_STRUCT( st_context
);
553 st
->options
= *options
;
560 /* state tracker needs the VBO module */
561 _vbo_CreateContext(ctx
);
563 st
->dirty
= ST_ALL_STATES_MASK
;
565 st
->can_bind_const_buffer_as_vertex
=
566 screen
->get_param(screen
, PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX
);
568 /* st/mesa always uploads zero-stride vertex attribs, and other user
569 * vertex buffers are only possible with a compatibility profile.
570 * So tell the u_vbuf module that user VBOs are not possible with the Core
571 * profile, so that u_vbuf is bypassed completely if there is nothing else
574 unsigned vbuf_flags
=
575 ctx
->API
== API_OPENGL_CORE
? U_VBUF_FLAG_NO_USER_VBOS
: 0;
576 st
->cso_context
= cso_create_context(pipe
, vbuf_flags
);
580 st_init_pbo_helpers(st
);
582 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
583 if (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_NPOT_TEXTURES
))
584 st
->internal_target
= PIPE_TEXTURE_2D
;
586 st
->internal_target
= PIPE_TEXTURE_RECT
;
588 /* Setup vertex element info for 'struct st_util_vertex'.
591 STATIC_ASSERT(sizeof(struct st_util_vertex
) == 9 * sizeof(float));
593 memset(&st
->util_velems
, 0, sizeof(st
->util_velems
));
594 st
->util_velems
[0].src_offset
= 0;
595 st
->util_velems
[0].vertex_buffer_index
= 0;
596 st
->util_velems
[0].src_format
= PIPE_FORMAT_R32G32B32_FLOAT
;
597 st
->util_velems
[1].src_offset
= 3 * sizeof(float);
598 st
->util_velems
[1].vertex_buffer_index
= 0;
599 st
->util_velems
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
600 st
->util_velems
[2].src_offset
= 7 * sizeof(float);
601 st
->util_velems
[2].vertex_buffer_index
= 0;
602 st
->util_velems
[2].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
605 /* we want all vertex data to be placed in buffer objects */
606 vbo_use_buffer_objects(ctx
);
609 /* make sure that no VBOs are left mapped when we're drawing. */
610 vbo_always_unmap_buffers(ctx
);
614 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
616 ctx
->VertexProgram
._MaintainTnlProgram
= GL_TRUE
;
619 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
;
621 ctx
->Const
.PackedDriverUniformStorage
=
622 screen
->get_param(screen
, PIPE_CAP_PACKED_UNIFORMS
);
624 st
->has_stencil_export
=
625 screen
->get_param(screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
);
626 st
->has_etc1
= screen
->is_format_supported(screen
, PIPE_FORMAT_ETC1_RGB8
,
627 PIPE_TEXTURE_2D
, 0, 0,
628 PIPE_BIND_SAMPLER_VIEW
);
629 st
->has_etc2
= screen
->is_format_supported(screen
, PIPE_FORMAT_ETC2_RGB8
,
630 PIPE_TEXTURE_2D
, 0, 0,
631 PIPE_BIND_SAMPLER_VIEW
);
632 st
->has_astc_2d_ldr
=
633 screen
->is_format_supported(screen
, PIPE_FORMAT_ASTC_4x4_SRGB
,
634 PIPE_TEXTURE_2D
, 0, 0, PIPE_BIND_SAMPLER_VIEW
);
635 st
->prefer_blit_based_texture_transfer
= screen
->get_param(screen
,
636 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER
);
637 st
->force_persample_in_shader
=
638 screen
->get_param(screen
, PIPE_CAP_SAMPLE_SHADING
) &&
639 !screen
->get_param(screen
, PIPE_CAP_FORCE_PERSAMPLE_INTERP
);
640 st
->has_shareable_shaders
= screen
->get_param(screen
,
641 PIPE_CAP_SHAREABLE_SHADERS
);
642 st
->needs_texcoord_semantic
=
643 screen
->get_param(screen
, PIPE_CAP_TGSI_TEXCOORD
);
644 st
->apply_texture_swizzle_to_border_color
=
645 !!(screen
->get_param(screen
, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK
) &
646 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50
|
647 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600
));
648 st
->has_time_elapsed
=
649 screen
->get_param(screen
, PIPE_CAP_QUERY_TIME_ELAPSED
);
650 st
->has_half_float_packing
=
651 screen
->get_param(screen
, PIPE_CAP_TGSI_PACK_HALF_FLOAT
);
652 st
->has_multi_draw_indirect
=
653 screen
->get_param(screen
, PIPE_CAP_MULTI_DRAW_INDIRECT
);
654 st
->has_single_pipe_stat
=
655 screen
->get_param(screen
, PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE
);
656 st
->has_indep_blend_func
=
657 screen
->get_param(screen
, PIPE_CAP_INDEP_BLEND_FUNC
);
658 st
->needs_rgb_dst_alpha_override
=
659 screen
->get_param(screen
, PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND
);
660 st
->has_signed_vertex_buffer_offset
=
661 screen
->get_param(screen
, PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET
);
664 screen
->get_shader_param(screen
, PIPE_SHADER_FRAGMENT
,
665 PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS
)
668 util_throttle_init(&st
->throttle
,
669 screen
->get_param(screen
,
670 PIPE_CAP_MAX_TEXTURE_UPLOAD_MEMORY_BUDGET
));
672 /* GL limits and extensions */
673 st_init_limits(pipe
->screen
, &ctx
->Const
, &ctx
->Extensions
);
674 st_init_extensions(pipe
->screen
, &ctx
->Const
,
675 &ctx
->Extensions
, &st
->options
, ctx
->API
);
677 if (st_have_perfmon(st
)) {
678 ctx
->Extensions
.AMD_performance_monitor
= GL_TRUE
;
681 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
682 if (screen
->get_param(screen
, PIPE_CAP_VERTEX_COLOR_UNCLAMPED
)) {
683 if (!screen
->get_param(screen
, PIPE_CAP_VERTEX_COLOR_CLAMPED
)) {
684 st
->clamp_vert_color_in_shader
= GL_TRUE
;
687 if (!screen
->get_param(screen
, PIPE_CAP_FRAGMENT_COLOR_CLAMPED
)) {
688 st
->clamp_frag_color_in_shader
= GL_TRUE
;
691 /* For drivers which cannot do color clamping, it's better to just
692 * disable ARB_color_buffer_float in the core profile, because
693 * the clamping is deprecated there anyway. */
694 if (ctx
->API
== API_OPENGL_CORE
&&
695 (st
->clamp_frag_color_in_shader
|| st
->clamp_vert_color_in_shader
)) {
696 st
->clamp_vert_color_in_shader
= GL_FALSE
;
697 st
->clamp_frag_color_in_shader
= GL_FALSE
;
698 ctx
->Extensions
.ARB_color_buffer_float
= GL_FALSE
;
702 if (screen
->get_param(screen
, PIPE_CAP_DEPTH_CLIP_DISABLE
) == 2)
703 st
->clamp_frag_depth_in_shader
= true;
705 /* called after _mesa_create_context/_mesa_init_point, fix default user
706 * settable max point size up
708 ctx
->Point
.MaxSize
= MAX2(ctx
->Const
.MaxPointSize
,
709 ctx
->Const
.MaxPointSizeAA
);
710 /* For vertex shaders, make sure not to emit saturate when SM 3.0
713 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].EmitNoSat
=
714 !screen
->get_param(screen
, PIPE_CAP_VERTEX_SHADER_SATURATE
);
716 if (ctx
->Const
.GLSLVersion
< 400) {
717 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
718 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectSampler
= true;
721 /* Set which shader types can be compiled at link time. */
722 st
->shader_has_one_variant
[MESA_SHADER_VERTEX
] =
723 st
->has_shareable_shaders
&&
724 !st
->clamp_frag_depth_in_shader
&&
725 !st
->clamp_vert_color_in_shader
;
727 st
->shader_has_one_variant
[MESA_SHADER_FRAGMENT
] =
728 st
->has_shareable_shaders
&&
729 !st
->clamp_frag_color_in_shader
&&
730 !st
->clamp_frag_depth_in_shader
&&
731 !st
->force_persample_in_shader
;
733 st
->shader_has_one_variant
[MESA_SHADER_TESS_CTRL
] = st
->has_shareable_shaders
;
734 st
->shader_has_one_variant
[MESA_SHADER_TESS_EVAL
] =
735 st
->has_shareable_shaders
&&
736 !st
->clamp_frag_depth_in_shader
&&
737 !st
->clamp_vert_color_in_shader
;
738 st
->shader_has_one_variant
[MESA_SHADER_GEOMETRY
] =
739 st
->has_shareable_shaders
&&
740 !st
->clamp_frag_depth_in_shader
&&
741 !st
->clamp_vert_color_in_shader
;
742 st
->shader_has_one_variant
[MESA_SHADER_COMPUTE
] = st
->has_shareable_shaders
;
744 st
->bitmap
.cache
.empty
= true;
746 _mesa_override_extensions(ctx
);
747 _mesa_compute_version(ctx
);
749 if (ctx
->Version
== 0) {
750 /* This can happen when a core profile was requested, but the driver
751 * does not support some features of GL 3.1 or later.
753 st_destroy_context_priv(st
, false);
757 _mesa_initialize_dispatch_tables(ctx
);
758 _mesa_initialize_vbo_vtxfmt(ctx
);
759 st_init_driver_flags(st
);
761 /* Initialize context's winsys buffers list */
762 LIST_INITHEAD(&st
->winsys_buffers
);
764 LIST_INITHEAD(&st
->zombie_sampler_views
.list
.node
);
765 mtx_init(&st
->zombie_sampler_views
.mutex
, mtx_plain
);
766 LIST_INITHEAD(&st
->zombie_shaders
.list
.node
);
767 mtx_init(&st
->zombie_shaders
.mutex
, mtx_plain
);
774 st_emit_string_marker(struct gl_context
*ctx
, const GLchar
*string
, GLsizei len
)
776 struct st_context
*st
= ctx
->st
;
777 st
->pipe
->emit_string_marker(st
->pipe
, string
, len
);
782 st_set_background_context(struct gl_context
*ctx
,
783 struct util_queue_monitoring
*queue_info
)
785 struct st_context
*st
= ctx
->st
;
786 struct st_manager
*smapi
=
787 (struct st_manager
*) st
->iface
.st_context_private
;
789 assert(smapi
->set_background_context
);
790 smapi
->set_background_context(&st
->iface
, queue_info
);
795 st_get_device_uuid(struct gl_context
*ctx
, char *uuid
)
797 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
799 assert(GL_UUID_SIZE_EXT
>= PIPE_UUID_SIZE
);
800 memset(uuid
, 0, GL_UUID_SIZE_EXT
);
801 screen
->get_device_uuid(screen
, uuid
);
806 st_get_driver_uuid(struct gl_context
*ctx
, char *uuid
)
808 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
810 assert(GL_UUID_SIZE_EXT
>= PIPE_UUID_SIZE
);
811 memset(uuid
, 0, GL_UUID_SIZE_EXT
);
812 screen
->get_driver_uuid(screen
, uuid
);
817 st_init_driver_functions(struct pipe_screen
*screen
,
818 struct dd_function_table
*functions
)
820 _mesa_init_sampler_object_functions(functions
);
822 st_init_draw_functions(functions
);
823 st_init_blit_functions(functions
);
824 st_init_bufferobject_functions(screen
, functions
);
825 st_init_clear_functions(functions
);
826 st_init_bitmap_functions(functions
);
827 st_init_copy_image_functions(functions
);
828 st_init_drawpixels_functions(functions
);
829 st_init_rasterpos_functions(functions
);
831 st_init_drawtex_functions(functions
);
833 st_init_eglimage_functions(functions
);
835 st_init_fbo_functions(functions
);
836 st_init_feedback_functions(functions
);
837 st_init_memoryobject_functions(functions
);
838 st_init_msaa_functions(functions
);
839 st_init_perfmon_functions(functions
);
840 st_init_program_functions(functions
);
841 st_init_query_functions(functions
);
842 st_init_cond_render_functions(functions
);
843 st_init_readpixels_functions(functions
);
844 st_init_semaphoreobject_functions(functions
);
845 st_init_texture_functions(functions
);
846 st_init_texture_barrier_functions(functions
);
847 st_init_flush_functions(screen
, functions
);
848 st_init_string_functions(functions
);
849 st_init_viewport_functions(functions
);
850 st_init_compute_functions(functions
);
852 st_init_xformfb_functions(functions
);
853 st_init_syncobj_functions(functions
);
855 st_init_vdpau_functions(functions
);
857 if (screen
->get_param(screen
, PIPE_CAP_STRING_MARKER
))
858 functions
->EmitStringMarker
= st_emit_string_marker
;
860 functions
->Enable
= st_Enable
;
861 functions
->UpdateState
= st_invalidate_state
;
862 functions
->QueryMemoryInfo
= st_query_memory_info
;
863 functions
->SetBackgroundContext
= st_set_background_context
;
864 functions
->GetDriverUuid
= st_get_driver_uuid
;
865 functions
->GetDeviceUuid
= st_get_device_uuid
;
867 /* GL_ARB_get_program_binary */
868 functions
->GetProgramBinaryDriverSHA1
= st_get_program_binary_driver_sha1
;
870 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
871 screen
->get_shader_param(screen
, PIPE_SHADER_VERTEX
,
872 PIPE_SHADER_CAP_PREFERRED_IR
);
873 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
874 functions
->ShaderCacheSerializeDriverBlob
= st_serialise_nir_program
;
875 functions
->ProgramBinarySerializeDriverBlob
=
876 st_serialise_nir_program_binary
;
877 functions
->ProgramBinaryDeserializeDriverBlob
=
878 st_deserialise_nir_program
;
880 functions
->ShaderCacheSerializeDriverBlob
= st_serialise_tgsi_program
;
881 functions
->ProgramBinarySerializeDriverBlob
=
882 st_serialise_tgsi_program_binary
;
883 functions
->ProgramBinaryDeserializeDriverBlob
=
884 st_deserialise_tgsi_program
;
890 st_create_context(gl_api api
, struct pipe_context
*pipe
,
891 const struct gl_config
*visual
,
892 struct st_context
*share
,
893 const struct st_config_options
*options
,
896 struct gl_context
*ctx
;
897 struct gl_context
*shareCtx
= share
? share
->ctx
: NULL
;
898 struct dd_function_table funcs
;
899 struct st_context
*st
;
903 memset(&funcs
, 0, sizeof(funcs
));
904 st_init_driver_functions(pipe
->screen
, &funcs
);
906 ctx
= calloc(1, sizeof(struct gl_context
));
910 if (!_mesa_initialize_context(ctx
, api
, visual
, shareCtx
, &funcs
)) {
917 if (pipe
->screen
->get_disk_shader_cache
&&
918 !(ST_DEBUG
& DEBUG_TGSI
))
919 ctx
->Cache
= pipe
->screen
->get_disk_shader_cache(pipe
->screen
);
921 /* XXX: need a capability bit in gallium to query if the pipe
922 * driver prefers DP4 or MUL/MAD for vertex transformation.
924 if (debug_get_option_mesa_mvp_dp4())
925 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].OptimizeForAOS
= GL_TRUE
;
927 st
= st_create_context_priv(ctx
, pipe
, options
, no_error
);
929 _mesa_destroy_context(ctx
);
937 * When we destroy a context, we must examine all texture objects to
938 * find/release any sampler views created by that context.
940 * This callback is called per-texture object. It releases all the
941 * texture's sampler views which belong to the context.
944 destroy_tex_sampler_cb(GLuint id
, void *data
, void *userData
)
946 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
947 struct st_context
*st
= (struct st_context
*) userData
;
949 st_texture_release_context_sampler_view(st
, st_texture_object(texObj
));
953 destroy_framebuffer_attachment_sampler_cb(GLuint id
, void *data
, void *userData
)
955 struct gl_framebuffer
* glfb
= (struct gl_framebuffer
*) data
;
956 struct st_context
*st
= (struct st_context
*) userData
;
958 for (unsigned i
= 0; i
< BUFFER_COUNT
; i
++) {
959 struct gl_renderbuffer_attachment
*att
= &glfb
->Attachment
[i
];
961 st_texture_release_context_sampler_view(st
, st_texture_object(att
->Texture
));
967 st_destroy_context(struct st_context
*st
)
969 struct gl_context
*ctx
= st
->ctx
;
970 struct st_framebuffer
*stfb
, *next
;
971 struct gl_framebuffer
*save_drawbuffer
;
972 struct gl_framebuffer
*save_readbuffer
;
974 /* Save the current context and draw/read buffers*/
975 GET_CURRENT_CONTEXT(save_ctx
);
977 save_drawbuffer
= save_ctx
->WinSysDrawBuffer
;
978 save_readbuffer
= save_ctx
->WinSysReadBuffer
;
980 save_drawbuffer
= save_readbuffer
= NULL
;
984 * We need to bind the context we're deleting so that
985 * _mesa_reference_texobj_() uses this context when deleting textures.
986 * Similarly for framebuffer objects, etc.
988 _mesa_make_current(ctx
, NULL
, NULL
);
990 /* This must be called first so that glthread has a chance to finish */
991 _mesa_glthread_destroy(ctx
);
993 _mesa_HashWalk(ctx
->Shared
->TexObjects
, destroy_tex_sampler_cb
, st
);
995 /* For the fallback textures, free any sampler views belonging to this
998 for (unsigned i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
999 struct st_texture_object
*stObj
=
1000 st_texture_object(ctx
->Shared
->FallbackTex
[i
]);
1002 st_texture_release_context_sampler_view(st
, stObj
);
1006 st_context_free_zombie_objects(st
);
1008 mtx_destroy(&st
->zombie_sampler_views
.mutex
);
1009 mtx_destroy(&st
->zombie_shaders
.mutex
);
1011 st_reference_fragprog(st
, &st
->fp
, NULL
);
1012 st_reference_prog(st
, &st
->gp
, NULL
);
1013 st_reference_vertprog(st
, &st
->vp
, NULL
);
1014 st_reference_prog(st
, &st
->tcp
, NULL
);
1015 st_reference_prog(st
, &st
->tep
, NULL
);
1016 st_reference_compprog(st
, &st
->cp
, NULL
);
1018 /* release framebuffer in the winsys buffers list */
1019 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
1020 st_framebuffer_reference(&stfb
, NULL
);
1023 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, destroy_framebuffer_attachment_sampler_cb
, st
);
1025 pipe_sampler_view_reference(&st
->pixel_xfer
.pixelmap_sampler_view
, NULL
);
1026 pipe_resource_reference(&st
->pixel_xfer
.pixelmap_texture
, NULL
);
1028 _vbo_DestroyContext(ctx
);
1030 st_destroy_program_variants(st
);
1032 _mesa_free_context_data(ctx
, false);
1034 /* This will free the st_context too, so 'st' must not be accessed
1036 st_destroy_context_priv(st
, true);
1041 if (save_ctx
== ctx
) {
1042 /* unbind the context we just deleted */
1043 _mesa_make_current(NULL
, NULL
, NULL
);
1045 /* Restore the current context and draw/read buffers (may be NULL) */
1046 _mesa_make_current(save_ctx
, save_drawbuffer
, save_readbuffer
);