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 "util/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_perfquery.h"
61 #include "st_cb_program.h"
62 #include "st_cb_queryobj.h"
63 #include "st_cb_readpixels.h"
64 #include "st_cb_semaphoreobjects.h"
65 #include "st_cb_texture.h"
66 #include "st_cb_xformfb.h"
67 #include "st_cb_flush.h"
68 #include "st_cb_syncobj.h"
69 #include "st_cb_strings.h"
70 #include "st_cb_texturebarrier.h"
71 #include "st_cb_viewport.h"
74 #include "st_extensions.h"
75 #include "st_gen_mipmap.h"
77 #include "st_program.h"
78 #include "st_sampler_view.h"
79 #include "st_shader_cache.h"
81 #include "st_texture.h"
83 #include "pipe/p_context.h"
84 #include "util/u_cpu_detect.h"
85 #include "util/u_inlines.h"
86 #include "util/u_upload_mgr.h"
87 #include "util/u_vbuf.h"
88 #include "util/u_memory.h"
89 #include "cso_cache/cso_context.h"
90 #include "compiler/glsl/glsl_parser_extras.h"
93 DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4
, "MESA_MVP_DP4", FALSE
)
97 * Called via ctx->Driver.Enable()
100 st_Enable(struct gl_context
*ctx
, GLenum cap
, GLboolean state
)
102 struct st_context
*st
= st_context(ctx
);
105 case GL_DEBUG_OUTPUT
:
106 case GL_DEBUG_OUTPUT_SYNCHRONOUS
:
107 st_update_debug_callback(st
);
109 case GL_BLACKHOLE_RENDER_INTEL
:
110 st
->pipe
->set_frontend_noop(st
->pipe
, ctx
->IntelBlackholeRender
);
119 * Called via ctx->Driver.QueryMemoryInfo()
122 st_query_memory_info(struct gl_context
*ctx
, struct gl_memory_info
*out
)
124 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
125 struct pipe_memory_info info
;
127 assert(screen
->query_memory_info
);
128 if (!screen
->query_memory_info
)
131 screen
->query_memory_info(screen
, &info
);
133 out
->total_device_memory
= info
.total_device_memory
;
134 out
->avail_device_memory
= info
.avail_device_memory
;
135 out
->total_staging_memory
= info
.total_staging_memory
;
136 out
->avail_staging_memory
= info
.avail_staging_memory
;
137 out
->device_memory_evicted
= info
.device_memory_evicted
;
138 out
->nr_device_memory_evictions
= info
.nr_device_memory_evictions
;
143 st_get_active_states(struct gl_context
*ctx
)
145 struct st_program
*vp
=
146 st_program(ctx
->VertexProgram
._Current
);
147 struct st_program
*tcp
=
148 st_program(ctx
->TessCtrlProgram
._Current
);
149 struct st_program
*tep
=
150 st_program(ctx
->TessEvalProgram
._Current
);
151 struct st_program
*gp
=
152 st_program(ctx
->GeometryProgram
._Current
);
153 struct st_program
*fp
=
154 st_program(ctx
->FragmentProgram
._Current
);
155 struct st_program
*cp
=
156 st_program(ctx
->ComputeProgram
._Current
);
157 uint64_t active_shader_states
= 0;
160 active_shader_states
|= vp
->affected_states
;
162 active_shader_states
|= tcp
->affected_states
;
164 active_shader_states
|= tep
->affected_states
;
166 active_shader_states
|= gp
->affected_states
;
168 active_shader_states
|= fp
->affected_states
;
170 active_shader_states
|= cp
->affected_states
;
172 /* Mark non-shader-resource shader states as "always active". */
173 return active_shader_states
| ~ST_ALL_SHADER_RESOURCES
;
178 st_invalidate_buffers(struct st_context
*st
)
180 st
->dirty
|= ST_NEW_BLEND
|
183 ST_NEW_SAMPLE_STATE
|
184 ST_NEW_SAMPLE_SHADING
|
186 ST_NEW_POLY_STIPPLE
|
190 ST_NEW_WINDOW_RECTANGLES
;
195 st_vp_uses_current_values(const struct gl_context
*ctx
)
197 const uint64_t inputs
= ctx
->VertexProgram
._Current
->info
.inputs_read
;
198 return _mesa_draw_current_bits(ctx
) & inputs
;
203 * Called via ctx->Driver.UpdateState()
206 st_invalidate_state(struct gl_context
*ctx
)
208 GLbitfield new_state
= ctx
->NewState
;
209 struct st_context
*st
= st_context(ctx
);
211 if (new_state
& _NEW_BUFFERS
) {
212 st_invalidate_buffers(st
);
214 /* These set a subset of flags set by _NEW_BUFFERS, so we only have to
215 * check them when _NEW_BUFFERS isn't set.
217 if (new_state
& _NEW_PROGRAM
)
218 st
->dirty
|= ST_NEW_RASTERIZER
;
220 if (new_state
& _NEW_FOG
)
221 st
->dirty
|= ST_NEW_FS_STATE
;
223 if (new_state
& _NEW_FRAG_CLAMP
) {
224 if (st
->clamp_frag_color_in_shader
)
225 st
->dirty
|= ST_NEW_FS_STATE
;
227 st
->dirty
|= ST_NEW_RASTERIZER
;
231 if (new_state
& (_NEW_LIGHT
|
233 st
->dirty
|= ST_NEW_RASTERIZER
;
235 if ((new_state
& _NEW_LIGHT
) &&
236 (st
->lower_flatshade
|| st
->lower_two_sided_color
))
237 st
->dirty
|= ST_NEW_FS_STATE
;
239 if (new_state
& _NEW_PROJECTION
&&
240 st_user_clip_planes_enabled(ctx
))
241 st
->dirty
|= ST_NEW_CLIP_STATE
;
243 if (new_state
& _NEW_PIXEL
)
244 st
->dirty
|= ST_NEW_PIXEL_TRANSFER
;
246 if (new_state
& _NEW_CURRENT_ATTRIB
&& st_vp_uses_current_values(ctx
))
247 st
->dirty
|= ST_NEW_VERTEX_ARRAYS
;
249 if (st
->clamp_frag_depth_in_shader
&& (new_state
& _NEW_VIEWPORT
)) {
250 if (ctx
->GeometryProgram
._Current
)
251 st
->dirty
|= ST_NEW_GS_CONSTANTS
;
252 else if (ctx
->TessEvalProgram
._Current
)
253 st
->dirty
|= ST_NEW_TES_CONSTANTS
;
255 st
->dirty
|= ST_NEW_VS_CONSTANTS
;
256 st
->dirty
|= ST_NEW_FS_CONSTANTS
;
259 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
260 if (st
->clamp_vert_color_in_shader
&& (new_state
& _NEW_LIGHT
)) {
261 st
->dirty
|= ST_NEW_VS_STATE
;
262 if (st
->ctx
->API
== API_OPENGL_COMPAT
&& ctx
->Version
>= 32) {
263 st
->dirty
|= ST_NEW_GS_STATE
| ST_NEW_TES_STATE
;
267 /* Which shaders are dirty will be determined manually. */
268 if (new_state
& _NEW_PROGRAM
) {
269 st
->gfx_shaders_may_be_dirty
= true;
270 st
->compute_shader_may_be_dirty
= true;
271 /* This will mask out unused shader resources. */
272 st
->active_states
= st_get_active_states(ctx
);
275 if (new_state
& _NEW_TEXTURE_OBJECT
) {
276 st
->dirty
|= st
->active_states
&
277 (ST_NEW_SAMPLER_VIEWS
|
280 if (ctx
->FragmentProgram
._Current
&&
281 ctx
->FragmentProgram
._Current
->ExternalSamplersUsed
) {
282 st
->dirty
|= ST_NEW_FS_STATE
;
289 * In some circumstances (such as running google-chrome) the state
290 * tracker may try to delete a resource view from a context different
291 * than when it was created. We don't want to do that.
293 * In that situation, st_texture_release_all_sampler_views() calls this
294 * function to transfer the sampler view reference to this context (expected
295 * to be the context which created the view.)
298 st_save_zombie_sampler_view(struct st_context
*st
,
299 struct pipe_sampler_view
*view
)
301 struct st_zombie_sampler_view_node
*entry
;
303 assert(view
->context
== st
->pipe
);
305 entry
= MALLOC_STRUCT(st_zombie_sampler_view_node
);
311 /* We need a mutex since this function may be called from one thread
312 * while free_zombie_resource_views() is called from another.
314 simple_mtx_lock(&st
->zombie_sampler_views
.mutex
);
315 list_addtail(&entry
->node
, &st
->zombie_sampler_views
.list
.node
);
316 simple_mtx_unlock(&st
->zombie_sampler_views
.mutex
);
321 * Since OpenGL shaders may be shared among contexts, we can wind up
322 * with variants of a shader created with different contexts.
323 * When we go to destroy a gallium shader, we want to free it with the
324 * same context that it was created with, unless the driver reports
325 * PIPE_CAP_SHAREABLE_SHADERS = TRUE.
328 st_save_zombie_shader(struct st_context
*st
,
329 enum pipe_shader_type type
,
330 struct pipe_shader_state
*shader
)
332 struct st_zombie_shader_node
*entry
;
334 /* we shouldn't be here if the driver supports shareable shaders */
335 assert(!st
->has_shareable_shaders
);
337 entry
= MALLOC_STRUCT(st_zombie_shader_node
);
341 entry
->shader
= shader
;
344 /* We need a mutex since this function may be called from one thread
345 * while free_zombie_shaders() is called from another.
347 simple_mtx_lock(&st
->zombie_shaders
.mutex
);
348 list_addtail(&entry
->node
, &st
->zombie_shaders
.list
.node
);
349 simple_mtx_unlock(&st
->zombie_shaders
.mutex
);
354 * Free any zombie sampler views that may be attached to this context.
357 free_zombie_sampler_views(struct st_context
*st
)
359 struct st_zombie_sampler_view_node
*entry
, *next
;
361 if (list_is_empty(&st
->zombie_sampler_views
.list
.node
)) {
365 simple_mtx_lock(&st
->zombie_sampler_views
.mutex
);
367 LIST_FOR_EACH_ENTRY_SAFE(entry
, next
,
368 &st
->zombie_sampler_views
.list
.node
, node
) {
369 list_del(&entry
->node
); // remove this entry from the list
371 assert(entry
->view
->context
== st
->pipe
);
372 pipe_sampler_view_reference(&entry
->view
, NULL
);
377 assert(list_is_empty(&st
->zombie_sampler_views
.list
.node
));
379 simple_mtx_unlock(&st
->zombie_sampler_views
.mutex
);
384 * Free any zombie shaders that may be attached to this context.
387 free_zombie_shaders(struct st_context
*st
)
389 struct st_zombie_shader_node
*entry
, *next
;
391 if (list_is_empty(&st
->zombie_shaders
.list
.node
)) {
395 simple_mtx_lock(&st
->zombie_shaders
.mutex
);
397 LIST_FOR_EACH_ENTRY_SAFE(entry
, next
,
398 &st
->zombie_shaders
.list
.node
, node
) {
399 list_del(&entry
->node
); // remove this entry from the list
401 switch (entry
->type
) {
402 case PIPE_SHADER_VERTEX
:
403 st
->pipe
->bind_vs_state(st
->pipe
, NULL
);
404 st
->pipe
->delete_vs_state(st
->pipe
, entry
->shader
);
406 case PIPE_SHADER_FRAGMENT
:
407 st
->pipe
->bind_fs_state(st
->pipe
, NULL
);
408 st
->pipe
->delete_fs_state(st
->pipe
, entry
->shader
);
410 case PIPE_SHADER_GEOMETRY
:
411 st
->pipe
->bind_gs_state(st
->pipe
, NULL
);
412 st
->pipe
->delete_gs_state(st
->pipe
, entry
->shader
);
414 case PIPE_SHADER_TESS_CTRL
:
415 st
->pipe
->bind_tcs_state(st
->pipe
, NULL
);
416 st
->pipe
->delete_tcs_state(st
->pipe
, entry
->shader
);
418 case PIPE_SHADER_TESS_EVAL
:
419 st
->pipe
->bind_tes_state(st
->pipe
, NULL
);
420 st
->pipe
->delete_tes_state(st
->pipe
, entry
->shader
);
422 case PIPE_SHADER_COMPUTE
:
423 st
->pipe
->bind_compute_state(st
->pipe
, NULL
);
424 st
->pipe
->delete_compute_state(st
->pipe
, entry
->shader
);
427 unreachable("invalid shader type in free_zombie_shaders()");
432 assert(list_is_empty(&st
->zombie_shaders
.list
.node
));
434 simple_mtx_unlock(&st
->zombie_shaders
.mutex
);
439 * This function is called periodically to free any zombie objects
440 * which are attached to this context.
443 st_context_free_zombie_objects(struct st_context
*st
)
445 free_zombie_sampler_views(st
);
446 free_zombie_shaders(st
);
451 st_destroy_context_priv(struct st_context
*st
, bool destroy_pipe
)
455 st_destroy_atoms(st
);
457 st_destroy_clear(st
);
458 st_destroy_bitmap(st
);
459 st_destroy_drawpix(st
);
460 st_destroy_drawtex(st
);
461 st_destroy_perfmon(st
);
462 st_destroy_pbo_helpers(st
);
463 st_destroy_bound_texture_handles(st
);
464 st_destroy_bound_image_handles(st
);
466 for (i
= 0; i
< ARRAY_SIZE(st
->state
.frag_sampler_views
); i
++) {
467 pipe_sampler_view_reference(&st
->state
.vert_sampler_views
[i
], NULL
);
468 pipe_sampler_view_reference(&st
->state
.frag_sampler_views
[i
], NULL
);
471 /* free glReadPixels cache data */
472 st_invalidate_readpix_cache(st
);
473 util_throttle_deinit(st
->pipe
->screen
, &st
->throttle
);
475 cso_destroy_context(st
->cso_context
);
477 if (st
->pipe
&& destroy_pipe
)
478 st
->pipe
->destroy(st
->pipe
);
485 st_init_driver_flags(struct st_context
*st
)
487 struct gl_driver_flags
*f
= &st
->ctx
->DriverFlags
;
489 f
->NewArray
= ST_NEW_VERTEX_ARRAYS
;
490 f
->NewRasterizerDiscard
= ST_NEW_RASTERIZER
;
491 f
->NewTileRasterOrder
= ST_NEW_RASTERIZER
;
492 f
->NewUniformBuffer
= ST_NEW_UNIFORM_BUFFER
;
493 f
->NewDefaultTessLevels
= ST_NEW_TESS_STATE
;
495 /* Shader resources */
496 f
->NewTextureBuffer
= ST_NEW_SAMPLER_VIEWS
;
497 if (st
->has_hw_atomics
)
498 f
->NewAtomicBuffer
= ST_NEW_HW_ATOMICS
| ST_NEW_CS_ATOMICS
;
500 f
->NewAtomicBuffer
= ST_NEW_ATOMIC_BUFFER
;
501 f
->NewShaderStorageBuffer
= ST_NEW_STORAGE_BUFFER
;
502 f
->NewImageUnits
= ST_NEW_IMAGE_UNITS
;
504 f
->NewShaderConstants
[MESA_SHADER_VERTEX
] = ST_NEW_VS_CONSTANTS
;
505 f
->NewShaderConstants
[MESA_SHADER_TESS_CTRL
] = ST_NEW_TCS_CONSTANTS
;
506 f
->NewShaderConstants
[MESA_SHADER_TESS_EVAL
] = ST_NEW_TES_CONSTANTS
;
507 f
->NewShaderConstants
[MESA_SHADER_GEOMETRY
] = ST_NEW_GS_CONSTANTS
;
508 f
->NewShaderConstants
[MESA_SHADER_FRAGMENT
] = ST_NEW_FS_CONSTANTS
;
509 f
->NewShaderConstants
[MESA_SHADER_COMPUTE
] = ST_NEW_CS_CONSTANTS
;
511 f
->NewWindowRectangles
= ST_NEW_WINDOW_RECTANGLES
;
512 f
->NewFramebufferSRGB
= ST_NEW_FB_STATE
;
513 f
->NewScissorRect
= ST_NEW_SCISSOR
;
514 f
->NewScissorTest
= ST_NEW_SCISSOR
| ST_NEW_RASTERIZER
;
516 if (st
->lower_alpha_test
)
517 f
->NewAlphaTest
= ST_NEW_FS_STATE
;
519 f
->NewAlphaTest
= ST_NEW_DSA
;
521 f
->NewBlend
= ST_NEW_BLEND
;
522 f
->NewBlendColor
= ST_NEW_BLEND_COLOR
;
523 f
->NewColorMask
= ST_NEW_BLEND
;
524 f
->NewDepth
= ST_NEW_DSA
;
525 f
->NewLogicOp
= ST_NEW_BLEND
;
526 f
->NewStencil
= ST_NEW_DSA
;
527 f
->NewMultisampleEnable
= ST_NEW_BLEND
| ST_NEW_RASTERIZER
|
528 ST_NEW_SAMPLE_STATE
| ST_NEW_SAMPLE_SHADING
;
529 f
->NewSampleAlphaToXEnable
= ST_NEW_BLEND
;
530 f
->NewSampleMask
= ST_NEW_SAMPLE_STATE
;
531 f
->NewSampleLocations
= ST_NEW_SAMPLE_STATE
;
532 f
->NewSampleShading
= ST_NEW_SAMPLE_SHADING
;
534 /* This depends on what the gallium driver wants. */
535 if (st
->force_persample_in_shader
) {
536 f
->NewMultisampleEnable
|= ST_NEW_FS_STATE
;
537 f
->NewSampleShading
|= ST_NEW_FS_STATE
;
539 f
->NewSampleShading
|= ST_NEW_RASTERIZER
;
542 f
->NewClipControl
= ST_NEW_VIEWPORT
| ST_NEW_RASTERIZER
;
543 f
->NewClipPlane
= ST_NEW_CLIP_STATE
;
545 if (st
->clamp_frag_depth_in_shader
) {
546 f
->NewClipControl
|= ST_NEW_VS_STATE
| ST_NEW_GS_STATE
|
549 f
->NewDepthClamp
= ST_NEW_FS_STATE
| ST_NEW_VS_STATE
|
550 ST_NEW_GS_STATE
| ST_NEW_TES_STATE
;
552 f
->NewDepthClamp
= ST_NEW_RASTERIZER
;
556 f
->NewClipPlaneEnable
= ST_NEW_VS_STATE
;
558 f
->NewClipPlaneEnable
= ST_NEW_RASTERIZER
;
560 f
->NewLineState
= ST_NEW_RASTERIZER
;
561 f
->NewPolygonState
= ST_NEW_RASTERIZER
;
562 f
->NewPolygonStipple
= ST_NEW_POLY_STIPPLE
;
563 f
->NewViewport
= ST_NEW_VIEWPORT
;
564 f
->NewNvConservativeRasterization
= ST_NEW_RASTERIZER
;
565 f
->NewNvConservativeRasterizationParams
= ST_NEW_RASTERIZER
;
566 f
->NewIntelConservativeRasterization
= ST_NEW_RASTERIZER
;
570 static struct st_context
*
571 st_create_context_priv(struct gl_context
*ctx
, struct pipe_context
*pipe
,
572 const struct st_config_options
*options
, bool no_error
)
574 struct pipe_screen
*screen
= pipe
->screen
;
576 struct st_context
*st
= ST_CALLOC_STRUCT( st_context
);
578 st
->options
= *options
;
584 st
->dirty
= ST_ALL_STATES_MASK
;
586 st
->can_bind_const_buffer_as_vertex
=
587 screen
->get_param(screen
, PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX
);
589 /* st/mesa always uploads zero-stride vertex attribs, and other user
590 * vertex buffers are only possible with a compatibility profile.
591 * So tell the u_vbuf module that user VBOs are not possible with the Core
592 * profile, so that u_vbuf is bypassed completely if there is nothing else
596 ctx
->API
== API_OPENGL_CORE
? CSO_NO_USER_VERTEX_BUFFERS
: 0;
597 st
->cso_context
= cso_create_context(pipe
, cso_flags
);
601 st_init_pbo_helpers(st
);
603 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
604 if (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_NPOT_TEXTURES
))
605 st
->internal_target
= PIPE_TEXTURE_2D
;
607 st
->internal_target
= PIPE_TEXTURE_RECT
;
609 /* Setup vertex element info for 'struct st_util_vertex'.
612 STATIC_ASSERT(sizeof(struct st_util_vertex
) == 9 * sizeof(float));
614 memset(&st
->util_velems
, 0, sizeof(st
->util_velems
));
615 st
->util_velems
.velems
[0].src_offset
= 0;
616 st
->util_velems
.velems
[0].vertex_buffer_index
= 0;
617 st
->util_velems
.velems
[0].src_format
= PIPE_FORMAT_R32G32B32_FLOAT
;
618 st
->util_velems
.velems
[1].src_offset
= 3 * sizeof(float);
619 st
->util_velems
.velems
[1].vertex_buffer_index
= 0;
620 st
->util_velems
.velems
[1].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
621 st
->util_velems
.velems
[2].src_offset
= 7 * sizeof(float);
622 st
->util_velems
.velems
[2].vertex_buffer_index
= 0;
623 st
->util_velems
.velems
[2].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
628 ctx
->FragmentProgram
._MaintainTexEnvProgram
= GL_TRUE
;
629 ctx
->VertexProgram
._MaintainTnlProgram
= GL_TRUE
;
632 ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR
;
634 ctx
->Const
.PackedDriverUniformStorage
=
635 screen
->get_param(screen
, PIPE_CAP_PACKED_UNIFORMS
);
637 ctx
->Const
.BitmapUsesRed
=
638 screen
->is_format_supported(screen
, PIPE_FORMAT_R8_UNORM
,
639 PIPE_TEXTURE_2D
, 0, 0,
640 PIPE_BIND_SAMPLER_VIEW
);
642 st
->has_stencil_export
=
643 screen
->get_param(screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
);
644 st
->has_etc1
= screen
->is_format_supported(screen
, PIPE_FORMAT_ETC1_RGB8
,
645 PIPE_TEXTURE_2D
, 0, 0,
646 PIPE_BIND_SAMPLER_VIEW
);
647 st
->has_etc2
= screen
->is_format_supported(screen
, PIPE_FORMAT_ETC2_RGB8
,
648 PIPE_TEXTURE_2D
, 0, 0,
649 PIPE_BIND_SAMPLER_VIEW
);
650 st
->has_astc_2d_ldr
=
651 screen
->is_format_supported(screen
, PIPE_FORMAT_ASTC_4x4_SRGB
,
652 PIPE_TEXTURE_2D
, 0, 0, PIPE_BIND_SAMPLER_VIEW
);
653 st
->has_astc_5x5_ldr
=
654 screen
->is_format_supported(screen
, PIPE_FORMAT_ASTC_5x5_SRGB
,
655 PIPE_TEXTURE_2D
, 0, 0, PIPE_BIND_SAMPLER_VIEW
);
656 st
->prefer_blit_based_texture_transfer
= screen
->get_param(screen
,
657 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER
);
658 st
->force_persample_in_shader
=
659 screen
->get_param(screen
, PIPE_CAP_SAMPLE_SHADING
) &&
660 !screen
->get_param(screen
, PIPE_CAP_FORCE_PERSAMPLE_INTERP
);
661 st
->has_shareable_shaders
= screen
->get_param(screen
,
662 PIPE_CAP_SHAREABLE_SHADERS
);
663 st
->needs_texcoord_semantic
=
664 screen
->get_param(screen
, PIPE_CAP_TGSI_TEXCOORD
);
665 st
->apply_texture_swizzle_to_border_color
=
666 !!(screen
->get_param(screen
, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK
) &
667 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50
|
668 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600
));
669 st
->has_time_elapsed
=
670 screen
->get_param(screen
, PIPE_CAP_QUERY_TIME_ELAPSED
);
671 st
->has_half_float_packing
=
672 screen
->get_param(screen
, PIPE_CAP_TGSI_PACK_HALF_FLOAT
);
673 st
->has_multi_draw_indirect
=
674 screen
->get_param(screen
, PIPE_CAP_MULTI_DRAW_INDIRECT
);
675 st
->has_single_pipe_stat
=
676 screen
->get_param(screen
, PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE
);
677 st
->has_indep_blend_func
=
678 screen
->get_param(screen
, PIPE_CAP_INDEP_BLEND_FUNC
);
679 st
->needs_rgb_dst_alpha_override
=
680 screen
->get_param(screen
, PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND
);
681 st
->lower_flatshade
=
682 !screen
->get_param(screen
, PIPE_CAP_FLATSHADE
);
683 st
->lower_alpha_test
=
684 !screen
->get_param(screen
, PIPE_CAP_ALPHA_TEST
);
685 st
->lower_point_size
=
686 !screen
->get_param(screen
, PIPE_CAP_POINT_SIZE_FIXED
);
687 st
->lower_two_sided_color
=
688 !screen
->get_param(screen
, PIPE_CAP_TWO_SIDED_COLOR
);
690 !screen
->get_param(screen
, PIPE_CAP_CLIP_PLANES
);
691 st
->allow_st_finalize_nir_twice
= screen
->finalize_nir
!= NULL
;
694 screen
->get_shader_param(screen
, PIPE_SHADER_FRAGMENT
,
695 PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS
)
698 util_throttle_init(&st
->throttle
,
699 screen
->get_param(screen
,
700 PIPE_CAP_MAX_TEXTURE_UPLOAD_MEMORY_BUDGET
));
702 /* GL limits and extensions */
703 st_init_limits(pipe
->screen
, &ctx
->Const
, &ctx
->Extensions
);
704 st_init_extensions(pipe
->screen
, &ctx
->Const
,
705 &ctx
->Extensions
, &st
->options
, ctx
->API
);
707 /* FIXME: add support for geometry and tessellation shaders for
710 assert(!ctx
->Extensions
.OES_geometry_shader
|| !st
->lower_point_size
);
711 assert(!ctx
->Extensions
.ARB_tessellation_shader
|| !st
->lower_point_size
);
713 /* FIXME: add support for geometry and tessellation shaders for
716 assert(!ctx
->Extensions
.OES_geometry_shader
|| !st
->lower_ucp
);
717 assert(!ctx
->Extensions
.ARB_tessellation_shader
|| !st
->lower_ucp
);
719 if (st_have_perfmon(st
)) {
720 ctx
->Extensions
.AMD_performance_monitor
= GL_TRUE
;
723 if (st_have_perfquery(st
)) {
724 ctx
->Extensions
.INTEL_performance_query
= GL_TRUE
;
727 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
728 if (screen
->get_param(screen
, PIPE_CAP_VERTEX_COLOR_UNCLAMPED
)) {
729 if (!screen
->get_param(screen
, PIPE_CAP_VERTEX_COLOR_CLAMPED
)) {
730 st
->clamp_vert_color_in_shader
= GL_TRUE
;
733 if (!screen
->get_param(screen
, PIPE_CAP_FRAGMENT_COLOR_CLAMPED
)) {
734 st
->clamp_frag_color_in_shader
= GL_TRUE
;
737 /* For drivers which cannot do color clamping, it's better to just
738 * disable ARB_color_buffer_float in the core profile, because
739 * the clamping is deprecated there anyway. */
740 if (ctx
->API
== API_OPENGL_CORE
&&
741 (st
->clamp_frag_color_in_shader
|| st
->clamp_vert_color_in_shader
)) {
742 st
->clamp_vert_color_in_shader
= GL_FALSE
;
743 st
->clamp_frag_color_in_shader
= GL_FALSE
;
744 ctx
->Extensions
.ARB_color_buffer_float
= GL_FALSE
;
748 if (screen
->get_param(screen
, PIPE_CAP_DEPTH_CLIP_DISABLE
) == 2)
749 st
->clamp_frag_depth_in_shader
= true;
751 /* called after _mesa_create_context/_mesa_init_point, fix default user
752 * settable max point size up
754 ctx
->Point
.MaxSize
= MAX2(ctx
->Const
.MaxPointSize
,
755 ctx
->Const
.MaxPointSizeAA
);
756 /* For vertex shaders, make sure not to emit saturate when SM 3.0
759 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].EmitNoSat
=
760 !screen
->get_param(screen
, PIPE_CAP_VERTEX_SHADER_SATURATE
);
762 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].PositionAlwaysInvariant
= options
->vs_position_always_invariant
;
764 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
765 screen
->get_shader_param(screen
, PIPE_SHADER_VERTEX
,
766 PIPE_SHADER_CAP_PREFERRED_IR
);
767 ctx
->Const
.UseNIRGLSLLinker
= preferred_ir
== PIPE_SHADER_IR_NIR
;
769 if (ctx
->Const
.GLSLVersion
< 400) {
770 for (i
= 0; i
< MESA_SHADER_STAGES
; i
++)
771 ctx
->Const
.ShaderCompilerOptions
[i
].EmitNoIndirectSampler
= true;
774 /* Set which shader types can be compiled at link time. */
775 st
->shader_has_one_variant
[MESA_SHADER_VERTEX
] =
776 st
->has_shareable_shaders
&&
777 !st
->clamp_frag_depth_in_shader
&&
778 !st
->clamp_vert_color_in_shader
&&
779 !st
->lower_point_size
&&
782 st
->shader_has_one_variant
[MESA_SHADER_FRAGMENT
] =
783 st
->has_shareable_shaders
&&
784 !st
->lower_flatshade
&&
785 !st
->lower_alpha_test
&&
786 !st
->clamp_frag_color_in_shader
&&
787 !st
->clamp_frag_depth_in_shader
&&
788 !st
->force_persample_in_shader
&&
789 !st
->lower_two_sided_color
;
791 st
->shader_has_one_variant
[MESA_SHADER_TESS_CTRL
] = st
->has_shareable_shaders
;
792 st
->shader_has_one_variant
[MESA_SHADER_TESS_EVAL
] =
793 st
->has_shareable_shaders
&&
794 !st
->clamp_frag_depth_in_shader
&&
795 !st
->clamp_vert_color_in_shader
;
796 st
->shader_has_one_variant
[MESA_SHADER_GEOMETRY
] =
797 st
->has_shareable_shaders
&&
798 !st
->clamp_frag_depth_in_shader
&&
799 !st
->clamp_vert_color_in_shader
;
800 st
->shader_has_one_variant
[MESA_SHADER_COMPUTE
] = st
->has_shareable_shaders
;
802 st
->bitmap
.cache
.empty
= true;
804 _mesa_override_extensions(ctx
);
805 _mesa_compute_version(ctx
);
807 if (ctx
->Version
== 0) {
808 /* This can happen when a core profile was requested, but the driver
809 * does not support some features of GL 3.1 or later.
811 st_destroy_context_priv(st
, false);
815 /* This must be done after extensions are initialized to enable persistent
816 * mappings immediately.
818 _vbo_CreateContext(ctx
, true);
820 _mesa_initialize_dispatch_tables(ctx
);
821 _mesa_initialize_vbo_vtxfmt(ctx
);
822 st_init_driver_flags(st
);
824 /* Initialize context's winsys buffers list */
825 list_inithead(&st
->winsys_buffers
);
827 list_inithead(&st
->zombie_sampler_views
.list
.node
);
828 simple_mtx_init(&st
->zombie_sampler_views
.mutex
, mtx_plain
);
829 list_inithead(&st
->zombie_shaders
.list
.node
);
830 simple_mtx_init(&st
->zombie_shaders
.mutex
, mtx_plain
);
837 st_emit_string_marker(struct gl_context
*ctx
, const GLchar
*string
, GLsizei len
)
839 struct st_context
*st
= ctx
->st
;
840 st
->pipe
->emit_string_marker(st
->pipe
, string
, len
);
845 st_set_background_context(struct gl_context
*ctx
,
846 struct util_queue_monitoring
*queue_info
)
848 struct st_context
*st
= ctx
->st
;
849 struct st_manager
*smapi
=
850 (struct st_manager
*) st
->iface
.st_context_private
;
852 assert(smapi
->set_background_context
);
853 smapi
->set_background_context(&st
->iface
, queue_info
);
858 st_get_device_uuid(struct gl_context
*ctx
, char *uuid
)
860 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
862 assert(GL_UUID_SIZE_EXT
>= PIPE_UUID_SIZE
);
863 memset(uuid
, 0, GL_UUID_SIZE_EXT
);
864 screen
->get_device_uuid(screen
, uuid
);
869 st_get_driver_uuid(struct gl_context
*ctx
, char *uuid
)
871 struct pipe_screen
*screen
= st_context(ctx
)->pipe
->screen
;
873 assert(GL_UUID_SIZE_EXT
>= PIPE_UUID_SIZE
);
874 memset(uuid
, 0, GL_UUID_SIZE_EXT
);
875 screen
->get_driver_uuid(screen
, uuid
);
880 st_init_driver_functions(struct pipe_screen
*screen
,
881 struct dd_function_table
*functions
)
883 _mesa_init_sampler_object_functions(functions
);
885 st_init_draw_functions(functions
);
886 st_init_blit_functions(functions
);
887 st_init_bufferobject_functions(screen
, functions
);
888 st_init_clear_functions(functions
);
889 st_init_bitmap_functions(functions
);
890 st_init_copy_image_functions(functions
);
891 st_init_drawpixels_functions(functions
);
892 st_init_rasterpos_functions(functions
);
894 st_init_drawtex_functions(functions
);
896 st_init_eglimage_functions(functions
);
898 st_init_fbo_functions(functions
);
899 st_init_feedback_functions(functions
);
900 st_init_memoryobject_functions(functions
);
901 st_init_msaa_functions(functions
);
902 st_init_perfmon_functions(functions
);
903 st_init_perfquery_functions(functions
);
904 st_init_program_functions(functions
);
905 st_init_query_functions(functions
);
906 st_init_cond_render_functions(functions
);
907 st_init_readpixels_functions(functions
);
908 st_init_semaphoreobject_functions(functions
);
909 st_init_texture_functions(functions
);
910 st_init_texture_barrier_functions(functions
);
911 st_init_flush_functions(screen
, functions
);
912 st_init_string_functions(functions
);
913 st_init_viewport_functions(functions
);
914 st_init_compute_functions(functions
);
916 st_init_xformfb_functions(functions
);
917 st_init_syncobj_functions(functions
);
919 st_init_vdpau_functions(functions
);
921 if (screen
->get_param(screen
, PIPE_CAP_STRING_MARKER
))
922 functions
->EmitStringMarker
= st_emit_string_marker
;
924 functions
->Enable
= st_Enable
;
925 functions
->UpdateState
= st_invalidate_state
;
926 functions
->QueryMemoryInfo
= st_query_memory_info
;
927 functions
->SetBackgroundContext
= st_set_background_context
;
928 functions
->GetDriverUuid
= st_get_driver_uuid
;
929 functions
->GetDeviceUuid
= st_get_device_uuid
;
931 /* GL_ARB_get_program_binary */
932 functions
->GetProgramBinaryDriverSHA1
= st_get_program_binary_driver_sha1
;
934 enum pipe_shader_ir preferred_ir
= (enum pipe_shader_ir
)
935 screen
->get_shader_param(screen
, PIPE_SHADER_VERTEX
,
936 PIPE_SHADER_CAP_PREFERRED_IR
);
937 if (preferred_ir
== PIPE_SHADER_IR_NIR
) {
938 functions
->ShaderCacheSerializeDriverBlob
= st_serialise_nir_program
;
939 functions
->ProgramBinarySerializeDriverBlob
=
940 st_serialise_nir_program_binary
;
941 functions
->ProgramBinaryDeserializeDriverBlob
=
942 st_deserialise_nir_program
;
944 functions
->ShaderCacheSerializeDriverBlob
= st_serialise_tgsi_program
;
945 functions
->ProgramBinarySerializeDriverBlob
=
946 st_serialise_tgsi_program_binary
;
947 functions
->ProgramBinaryDeserializeDriverBlob
=
948 st_deserialise_tgsi_program
;
954 st_create_context(gl_api api
, struct pipe_context
*pipe
,
955 const struct gl_config
*visual
,
956 struct st_context
*share
,
957 const struct st_config_options
*options
,
960 struct gl_context
*ctx
;
961 struct gl_context
*shareCtx
= share
? share
->ctx
: NULL
;
962 struct dd_function_table funcs
;
963 struct st_context
*st
;
967 memset(&funcs
, 0, sizeof(funcs
));
968 st_init_driver_functions(pipe
->screen
, &funcs
);
970 ctx
= calloc(1, sizeof(struct gl_context
));
974 if (!_mesa_initialize_context(ctx
, api
, visual
, shareCtx
, &funcs
)) {
981 if (pipe
->screen
->get_disk_shader_cache
)
982 ctx
->Cache
= pipe
->screen
->get_disk_shader_cache(pipe
->screen
);
984 /* XXX: need a capability bit in gallium to query if the pipe
985 * driver prefers DP4 or MUL/MAD for vertex transformation.
987 if (debug_get_option_mesa_mvp_dp4())
988 ctx
->Const
.ShaderCompilerOptions
[MESA_SHADER_VERTEX
].OptimizeForAOS
= GL_TRUE
;
990 st
= st_create_context_priv(ctx
, pipe
, options
, no_error
);
992 _mesa_destroy_context(ctx
);
1000 * When we destroy a context, we must examine all texture objects to
1001 * find/release any sampler views created by that context.
1003 * This callback is called per-texture object. It releases all the
1004 * texture's sampler views which belong to the context.
1007 destroy_tex_sampler_cb(GLuint id
, void *data
, void *userData
)
1009 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
1010 struct st_context
*st
= (struct st_context
*) userData
;
1012 st_texture_release_context_sampler_view(st
, st_texture_object(texObj
));
1016 destroy_framebuffer_attachment_sampler_cb(GLuint id
, void *data
, void *userData
)
1018 struct gl_framebuffer
* glfb
= (struct gl_framebuffer
*) data
;
1019 struct st_context
*st
= (struct st_context
*) userData
;
1021 for (unsigned i
= 0; i
< BUFFER_COUNT
; i
++) {
1022 struct gl_renderbuffer_attachment
*att
= &glfb
->Attachment
[i
];
1024 st_texture_release_context_sampler_view(st
, st_texture_object(att
->Texture
));
1030 st_destroy_context(struct st_context
*st
)
1032 struct gl_context
*ctx
= st
->ctx
;
1033 struct st_framebuffer
*stfb
, *next
;
1034 struct gl_framebuffer
*save_drawbuffer
;
1035 struct gl_framebuffer
*save_readbuffer
;
1037 /* Save the current context and draw/read buffers*/
1038 GET_CURRENT_CONTEXT(save_ctx
);
1040 save_drawbuffer
= save_ctx
->WinSysDrawBuffer
;
1041 save_readbuffer
= save_ctx
->WinSysReadBuffer
;
1043 save_drawbuffer
= save_readbuffer
= NULL
;
1047 * We need to bind the context we're deleting so that
1048 * _mesa_reference_texobj_() uses this context when deleting textures.
1049 * Similarly for framebuffer objects, etc.
1051 _mesa_make_current(ctx
, NULL
, NULL
);
1053 /* This must be called first so that glthread has a chance to finish */
1054 _mesa_glthread_destroy(ctx
);
1056 _mesa_HashWalk(ctx
->Shared
->TexObjects
, destroy_tex_sampler_cb
, st
);
1058 /* For the fallback textures, free any sampler views belonging to this
1061 for (unsigned i
= 0; i
< NUM_TEXTURE_TARGETS
; i
++) {
1062 struct st_texture_object
*stObj
=
1063 st_texture_object(ctx
->Shared
->FallbackTex
[i
]);
1065 st_texture_release_context_sampler_view(st
, stObj
);
1069 st_context_free_zombie_objects(st
);
1071 simple_mtx_destroy(&st
->zombie_sampler_views
.mutex
);
1072 simple_mtx_destroy(&st
->zombie_shaders
.mutex
);
1074 st_release_program(st
, &st
->fp
);
1075 st_release_program(st
, &st
->gp
);
1076 st_release_program(st
, &st
->vp
);
1077 st_release_program(st
, &st
->tcp
);
1078 st_release_program(st
, &st
->tep
);
1079 st_release_program(st
, &st
->cp
);
1081 /* release framebuffer in the winsys buffers list */
1082 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
1083 st_framebuffer_reference(&stfb
, NULL
);
1086 _mesa_HashWalk(ctx
->Shared
->FrameBuffers
, destroy_framebuffer_attachment_sampler_cb
, st
);
1088 pipe_sampler_view_reference(&st
->pixel_xfer
.pixelmap_sampler_view
, NULL
);
1089 pipe_resource_reference(&st
->pixel_xfer
.pixelmap_texture
, NULL
);
1091 _vbo_DestroyContext(ctx
);
1093 st_destroy_program_variants(st
);
1095 _mesa_free_context_data(ctx
);
1097 /* This will free the st_context too, so 'st' must not be accessed
1099 st_destroy_context_priv(st
, true);
1104 if (save_ctx
== ctx
) {
1105 /* unbind the context we just deleted */
1106 _mesa_make_current(NULL
, NULL
, NULL
);
1108 /* Restore the current context and draw/read buffers (may be NULL) */
1109 _mesa_make_current(save_ctx
, save_drawbuffer
, save_readbuffer
);