st/mesa: move around some code in st_context.c
[mesa.git] / src / mesa / state_tracker / st_context.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/imports.h"
29 #include "main/accum.h"
30 #include "main/api_exec.h"
31 #include "main/context.h"
32 #include "main/glthread.h"
33 #include "main/samplerobj.h"
34 #include "main/shaderobj.h"
35 #include "main/version.h"
36 #include "main/vtxfmt.h"
37 #include "main/hash.h"
38 #include "program/prog_cache.h"
39 #include "vbo/vbo.h"
40 #include "glapi/glapi.h"
41 #include "st_manager.h"
42 #include "st_context.h"
43 #include "st_debug.h"
44 #include "st_cb_bitmap.h"
45 #include "st_cb_blit.h"
46 #include "st_cb_bufferobjects.h"
47 #include "st_cb_clear.h"
48 #include "st_cb_compute.h"
49 #include "st_cb_condrender.h"
50 #include "st_cb_copyimage.h"
51 #include "st_cb_drawpixels.h"
52 #include "st_cb_rasterpos.h"
53 #include "st_cb_drawtex.h"
54 #include "st_cb_eglimage.h"
55 #include "st_cb_fbo.h"
56 #include "st_cb_feedback.h"
57 #include "st_cb_memoryobjects.h"
58 #include "st_cb_msaa.h"
59 #include "st_cb_perfmon.h"
60 #include "st_cb_program.h"
61 #include "st_cb_queryobj.h"
62 #include "st_cb_readpixels.h"
63 #include "st_cb_semaphoreobjects.h"
64 #include "st_cb_texture.h"
65 #include "st_cb_xformfb.h"
66 #include "st_cb_flush.h"
67 #include "st_cb_syncobj.h"
68 #include "st_cb_strings.h"
69 #include "st_cb_texturebarrier.h"
70 #include "st_cb_viewport.h"
71 #include "st_atom.h"
72 #include "st_draw.h"
73 #include "st_extensions.h"
74 #include "st_gen_mipmap.h"
75 #include "st_pbo.h"
76 #include "st_program.h"
77 #include "st_sampler_view.h"
78 #include "st_shader_cache.h"
79 #include "st_vdpau.h"
80 #include "st_texture.h"
81 #include "st_util.h"
82 #include "pipe/p_context.h"
83 #include "util/u_cpu_detect.h"
84 #include "util/u_inlines.h"
85 #include "util/u_upload_mgr.h"
86 #include "util/u_vbuf.h"
87 #include "cso_cache/cso_context.h"
88
89
90 DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
91
92
93 /**
94 * Called via ctx->Driver.Enable()
95 */
96 static void
97 st_Enable(struct gl_context *ctx, GLenum cap, GLboolean state)
98 {
99 struct st_context *st = st_context(ctx);
100
101 switch (cap) {
102 case GL_DEBUG_OUTPUT:
103 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
104 st_update_debug_callback(st);
105 break;
106 default:
107 break;
108 }
109 }
110
111
112 /**
113 * Called via ctx->Driver.QueryMemoryInfo()
114 */
115 static void
116 st_query_memory_info(struct gl_context *ctx, struct gl_memory_info *out)
117 {
118 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
119 struct pipe_memory_info info;
120
121 assert(screen->query_memory_info);
122 if (!screen->query_memory_info)
123 return;
124
125 screen->query_memory_info(screen, &info);
126
127 out->total_device_memory = info.total_device_memory;
128 out->avail_device_memory = info.avail_device_memory;
129 out->total_staging_memory = info.total_staging_memory;
130 out->avail_staging_memory = info.avail_staging_memory;
131 out->device_memory_evicted = info.device_memory_evicted;
132 out->nr_device_memory_evictions = info.nr_device_memory_evictions;
133 }
134
135
136 static uint64_t
137 st_get_active_states(struct gl_context *ctx)
138 {
139 struct st_vertex_program *vp =
140 st_vertex_program(ctx->VertexProgram._Current);
141 struct st_common_program *tcp =
142 st_common_program(ctx->TessCtrlProgram._Current);
143 struct st_common_program *tep =
144 st_common_program(ctx->TessEvalProgram._Current);
145 struct st_common_program *gp =
146 st_common_program(ctx->GeometryProgram._Current);
147 struct st_fragment_program *fp =
148 st_fragment_program(ctx->FragmentProgram._Current);
149 struct st_compute_program *cp =
150 st_compute_program(ctx->ComputeProgram._Current);
151 uint64_t active_shader_states = 0;
152
153 if (vp)
154 active_shader_states |= vp->affected_states;
155 if (tcp)
156 active_shader_states |= tcp->affected_states;
157 if (tep)
158 active_shader_states |= tep->affected_states;
159 if (gp)
160 active_shader_states |= gp->affected_states;
161 if (fp)
162 active_shader_states |= fp->affected_states;
163 if (cp)
164 active_shader_states |= cp->affected_states;
165
166 /* Mark non-shader-resource shader states as "always active". */
167 return active_shader_states | ~ST_ALL_SHADER_RESOURCES;
168 }
169
170
171 void
172 st_invalidate_buffers(struct st_context *st)
173 {
174 st->dirty |= ST_NEW_BLEND |
175 ST_NEW_DSA |
176 ST_NEW_FB_STATE |
177 ST_NEW_SAMPLE_STATE |
178 ST_NEW_SAMPLE_SHADING |
179 ST_NEW_FS_STATE |
180 ST_NEW_POLY_STIPPLE |
181 ST_NEW_VIEWPORT |
182 ST_NEW_RASTERIZER |
183 ST_NEW_SCISSOR |
184 ST_NEW_WINDOW_RECTANGLES;
185 }
186
187
188 static inline bool
189 st_vp_uses_current_values(const struct gl_context *ctx)
190 {
191 const uint64_t inputs = ctx->VertexProgram._Current->info.inputs_read;
192 return _mesa_draw_current_bits(ctx) & inputs;
193 }
194
195
196 /**
197 * Called via ctx->Driver.UpdateState()
198 */
199 static void
200 st_invalidate_state(struct gl_context *ctx)
201 {
202 GLbitfield new_state = ctx->NewState;
203 struct st_context *st = st_context(ctx);
204
205 if (new_state & _NEW_BUFFERS) {
206 st_invalidate_buffers(st);
207 } else {
208 /* These set a subset of flags set by _NEW_BUFFERS, so we only have to
209 * check them when _NEW_BUFFERS isn't set.
210 */
211 if (new_state & _NEW_PROGRAM)
212 st->dirty |= ST_NEW_RASTERIZER;
213
214 if (new_state & _NEW_FOG)
215 st->dirty |= ST_NEW_FS_STATE;
216
217 if (new_state & _NEW_FRAG_CLAMP) {
218 if (st->clamp_frag_color_in_shader)
219 st->dirty |= ST_NEW_FS_STATE;
220 else
221 st->dirty |= ST_NEW_RASTERIZER;
222 }
223 }
224
225 if (new_state & (_NEW_LIGHT |
226 _NEW_POINT))
227 st->dirty |= ST_NEW_RASTERIZER;
228
229 if (new_state & _NEW_PROJECTION &&
230 st_user_clip_planes_enabled(ctx))
231 st->dirty |= ST_NEW_CLIP_STATE;
232
233 if (new_state & _NEW_PIXEL)
234 st->dirty |= ST_NEW_PIXEL_TRANSFER;
235
236 if (new_state & _NEW_CURRENT_ATTRIB && st_vp_uses_current_values(ctx))
237 st->dirty |= ST_NEW_VERTEX_ARRAYS;
238
239 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
240 if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT))
241 st->dirty |= ST_NEW_VS_STATE;
242
243 /* Which shaders are dirty will be determined manually. */
244 if (new_state & _NEW_PROGRAM) {
245 st->gfx_shaders_may_be_dirty = true;
246 st->compute_shader_may_be_dirty = true;
247 /* This will mask out unused shader resources. */
248 st->active_states = st_get_active_states(ctx);
249 }
250
251 if (new_state & _NEW_TEXTURE_OBJECT) {
252 st->dirty |= st->active_states &
253 (ST_NEW_SAMPLER_VIEWS |
254 ST_NEW_SAMPLERS |
255 ST_NEW_IMAGE_UNITS);
256 if (ctx->FragmentProgram._Current &&
257 ctx->FragmentProgram._Current->ExternalSamplersUsed) {
258 st->dirty |= ST_NEW_FS_STATE;
259 }
260 }
261 }
262
263
264 static void
265 st_destroy_context_priv(struct st_context *st, bool destroy_pipe)
266 {
267 uint i;
268
269 st_destroy_atoms(st);
270 st_destroy_draw(st);
271 st_destroy_clear(st);
272 st_destroy_bitmap(st);
273 st_destroy_drawpix(st);
274 st_destroy_drawtex(st);
275 st_destroy_perfmon(st);
276 st_destroy_pbo_helpers(st);
277 st_destroy_bound_texture_handles(st);
278 st_destroy_bound_image_handles(st);
279
280 for (i = 0; i < ARRAY_SIZE(st->state.frag_sampler_views); i++) {
281 pipe_sampler_view_release(st->pipe,
282 &st->state.frag_sampler_views[i]);
283 }
284
285 /* free glReadPixels cache data */
286 st_invalidate_readpix_cache(st);
287 util_throttle_deinit(st->pipe->screen, &st->throttle);
288
289 cso_destroy_context(st->cso_context);
290
291 if (st->pipe && destroy_pipe)
292 st->pipe->destroy(st->pipe);
293
294 free(st);
295 }
296
297
298 static void
299 st_init_driver_flags(struct st_context *st)
300 {
301 struct gl_driver_flags *f = &st->ctx->DriverFlags;
302
303 f->NewArray = ST_NEW_VERTEX_ARRAYS;
304 f->NewRasterizerDiscard = ST_NEW_RASTERIZER;
305 f->NewTileRasterOrder = ST_NEW_RASTERIZER;
306 f->NewUniformBuffer = ST_NEW_UNIFORM_BUFFER;
307 f->NewDefaultTessLevels = ST_NEW_TESS_STATE;
308
309 /* Shader resources */
310 f->NewTextureBuffer = ST_NEW_SAMPLER_VIEWS;
311 if (st->has_hw_atomics)
312 f->NewAtomicBuffer = ST_NEW_HW_ATOMICS | ST_NEW_CS_ATOMICS;
313 else
314 f->NewAtomicBuffer = ST_NEW_ATOMIC_BUFFER;
315 f->NewShaderStorageBuffer = ST_NEW_STORAGE_BUFFER;
316 f->NewImageUnits = ST_NEW_IMAGE_UNITS;
317
318 f->NewShaderConstants[MESA_SHADER_VERTEX] = ST_NEW_VS_CONSTANTS;
319 f->NewShaderConstants[MESA_SHADER_TESS_CTRL] = ST_NEW_TCS_CONSTANTS;
320 f->NewShaderConstants[MESA_SHADER_TESS_EVAL] = ST_NEW_TES_CONSTANTS;
321 f->NewShaderConstants[MESA_SHADER_GEOMETRY] = ST_NEW_GS_CONSTANTS;
322 f->NewShaderConstants[MESA_SHADER_FRAGMENT] = ST_NEW_FS_CONSTANTS;
323 f->NewShaderConstants[MESA_SHADER_COMPUTE] = ST_NEW_CS_CONSTANTS;
324
325 f->NewWindowRectangles = ST_NEW_WINDOW_RECTANGLES;
326 f->NewFramebufferSRGB = ST_NEW_FB_STATE;
327 f->NewScissorRect = ST_NEW_SCISSOR;
328 f->NewScissorTest = ST_NEW_SCISSOR | ST_NEW_RASTERIZER;
329 f->NewAlphaTest = ST_NEW_DSA;
330 f->NewBlend = ST_NEW_BLEND;
331 f->NewBlendColor = ST_NEW_BLEND_COLOR;
332 f->NewColorMask = ST_NEW_BLEND;
333 f->NewDepth = ST_NEW_DSA;
334 f->NewLogicOp = ST_NEW_BLEND;
335 f->NewStencil = ST_NEW_DSA;
336 f->NewMultisampleEnable = ST_NEW_BLEND | ST_NEW_RASTERIZER |
337 ST_NEW_SAMPLE_STATE | ST_NEW_SAMPLE_SHADING;
338 f->NewSampleAlphaToXEnable = ST_NEW_BLEND;
339 f->NewSampleMask = ST_NEW_SAMPLE_STATE;
340 f->NewSampleLocations = ST_NEW_SAMPLE_STATE;
341 f->NewSampleShading = ST_NEW_SAMPLE_SHADING;
342
343 /* This depends on what the gallium driver wants. */
344 if (st->force_persample_in_shader) {
345 f->NewMultisampleEnable |= ST_NEW_FS_STATE;
346 f->NewSampleShading |= ST_NEW_FS_STATE;
347 } else {
348 f->NewSampleShading |= ST_NEW_RASTERIZER;
349 }
350
351 f->NewClipControl = ST_NEW_VIEWPORT | ST_NEW_RASTERIZER;
352 f->NewClipPlane = ST_NEW_CLIP_STATE;
353 f->NewClipPlaneEnable = ST_NEW_RASTERIZER;
354 f->NewDepthClamp = ST_NEW_RASTERIZER;
355 f->NewLineState = ST_NEW_RASTERIZER;
356 f->NewPolygonState = ST_NEW_RASTERIZER;
357 f->NewPolygonStipple = ST_NEW_POLY_STIPPLE;
358 f->NewViewport = ST_NEW_VIEWPORT;
359 f->NewNvConservativeRasterization = ST_NEW_RASTERIZER;
360 f->NewNvConservativeRasterizationParams = ST_NEW_RASTERIZER;
361 }
362
363
364 static struct st_context *
365 st_create_context_priv(struct gl_context *ctx, struct pipe_context *pipe,
366 const struct st_config_options *options, bool no_error)
367 {
368 struct pipe_screen *screen = pipe->screen;
369 uint i;
370 struct st_context *st = ST_CALLOC_STRUCT( st_context);
371
372 st->options = *options;
373
374 ctx->st = st;
375
376 st->ctx = ctx;
377 st->pipe = pipe;
378
379 /* state tracker needs the VBO module */
380 _vbo_CreateContext(ctx);
381
382 st->dirty = ST_ALL_STATES_MASK;
383
384 st->can_bind_const_buffer_as_vertex =
385 screen->get_param(screen, PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX);
386
387 /* st/mesa always uploads zero-stride vertex attribs, and other user
388 * vertex buffers are only possible with a compatibility profile.
389 * So tell the u_vbuf module that user VBOs are not possible with the Core
390 * profile, so that u_vbuf is bypassed completely if there is nothing else
391 * to do.
392 */
393 unsigned vbuf_flags =
394 ctx->API == API_OPENGL_CORE ? U_VBUF_FLAG_NO_USER_VBOS : 0;
395 st->cso_context = cso_create_context(pipe, vbuf_flags);
396
397 st_init_atoms(st);
398 st_init_clear(st);
399 st_init_pbo_helpers(st);
400
401 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
402 if (pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
403 st->internal_target = PIPE_TEXTURE_2D;
404 else
405 st->internal_target = PIPE_TEXTURE_RECT;
406
407 /* Setup vertex element info for 'struct st_util_vertex'.
408 */
409 {
410 STATIC_ASSERT(sizeof(struct st_util_vertex) == 9 * sizeof(float));
411
412 memset(&st->util_velems, 0, sizeof(st->util_velems));
413 st->util_velems[0].src_offset = 0;
414 st->util_velems[0].vertex_buffer_index = 0;
415 st->util_velems[0].src_format = PIPE_FORMAT_R32G32B32_FLOAT;
416 st->util_velems[1].src_offset = 3 * sizeof(float);
417 st->util_velems[1].vertex_buffer_index = 0;
418 st->util_velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
419 st->util_velems[2].src_offset = 7 * sizeof(float);
420 st->util_velems[2].vertex_buffer_index = 0;
421 st->util_velems[2].src_format = PIPE_FORMAT_R32G32_FLOAT;
422 }
423
424 /* we want all vertex data to be placed in buffer objects */
425 vbo_use_buffer_objects(ctx);
426
427
428 /* make sure that no VBOs are left mapped when we're drawing. */
429 vbo_always_unmap_buffers(ctx);
430
431 /* Need these flags:
432 */
433 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
434
435 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
436
437 if (no_error)
438 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
439
440 ctx->Const.PackedDriverUniformStorage =
441 screen->get_param(screen, PIPE_CAP_PACKED_UNIFORMS);
442
443 st->has_stencil_export =
444 screen->get_param(screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
445 st->has_shader_model3 = screen->get_param(screen, PIPE_CAP_SM3);
446 st->has_etc1 = screen->is_format_supported(screen, PIPE_FORMAT_ETC1_RGB8,
447 PIPE_TEXTURE_2D, 0, 0,
448 PIPE_BIND_SAMPLER_VIEW);
449 st->has_etc2 = screen->is_format_supported(screen, PIPE_FORMAT_ETC2_RGB8,
450 PIPE_TEXTURE_2D, 0, 0,
451 PIPE_BIND_SAMPLER_VIEW);
452 st->has_astc_2d_ldr =
453 screen->is_format_supported(screen, PIPE_FORMAT_ASTC_4x4_SRGB,
454 PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW);
455 st->prefer_blit_based_texture_transfer = screen->get_param(screen,
456 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER);
457 st->force_persample_in_shader =
458 screen->get_param(screen, PIPE_CAP_SAMPLE_SHADING) &&
459 !screen->get_param(screen, PIPE_CAP_FORCE_PERSAMPLE_INTERP);
460 st->has_shareable_shaders = screen->get_param(screen,
461 PIPE_CAP_SHAREABLE_SHADERS);
462 st->needs_texcoord_semantic =
463 screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
464 st->apply_texture_swizzle_to_border_color =
465 !!(screen->get_param(screen, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK) &
466 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50 |
467 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600));
468 st->has_time_elapsed =
469 screen->get_param(screen, PIPE_CAP_QUERY_TIME_ELAPSED);
470 st->has_half_float_packing =
471 screen->get_param(screen, PIPE_CAP_TGSI_PACK_HALF_FLOAT);
472 st->has_multi_draw_indirect =
473 screen->get_param(screen, PIPE_CAP_MULTI_DRAW_INDIRECT);
474 st->has_single_pipe_stat =
475 screen->get_param(screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE);
476 st->has_indep_blend_func =
477 screen->get_param(screen, PIPE_CAP_INDEP_BLEND_FUNC);
478 st->needs_rgb_dst_alpha_override =
479 screen->get_param(screen, PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND);
480
481 st->has_hw_atomics =
482 screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
483 PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS)
484 ? true : false;
485
486 util_throttle_init(&st->throttle,
487 screen->get_param(screen,
488 PIPE_CAP_MAX_TEXTURE_UPLOAD_MEMORY_BUDGET));
489
490 /* GL limits and extensions */
491 st_init_limits(pipe->screen, &ctx->Const, &ctx->Extensions, ctx->API);
492 st_init_extensions(pipe->screen, &ctx->Const,
493 &ctx->Extensions, &st->options, ctx->API);
494
495 if (st_have_perfmon(st)) {
496 ctx->Extensions.AMD_performance_monitor = GL_TRUE;
497 }
498
499 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
500 if (screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_UNCLAMPED)) {
501 if (!screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_CLAMPED)) {
502 st->clamp_vert_color_in_shader = GL_TRUE;
503 }
504
505 if (!screen->get_param(screen, PIPE_CAP_FRAGMENT_COLOR_CLAMPED)) {
506 st->clamp_frag_color_in_shader = GL_TRUE;
507 }
508
509 /* For drivers which cannot do color clamping, it's better to just
510 * disable ARB_color_buffer_float in the core profile, because
511 * the clamping is deprecated there anyway. */
512 if (ctx->API == API_OPENGL_CORE &&
513 (st->clamp_frag_color_in_shader || st->clamp_vert_color_in_shader)) {
514 st->clamp_vert_color_in_shader = GL_FALSE;
515 st->clamp_frag_color_in_shader = GL_FALSE;
516 ctx->Extensions.ARB_color_buffer_float = GL_FALSE;
517 }
518 }
519
520 /* called after _mesa_create_context/_mesa_init_point, fix default user
521 * settable max point size up
522 */
523 ctx->Point.MaxSize = MAX2(ctx->Const.MaxPointSize,
524 ctx->Const.MaxPointSizeAA);
525 /* For vertex shaders, make sure not to emit saturate when SM 3.0
526 * is not supported
527 */
528 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitNoSat =
529 !st->has_shader_model3;
530
531 if (!ctx->Extensions.ARB_gpu_shader5) {
532 for (i = 0; i < MESA_SHADER_STAGES; i++)
533 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler = true;
534 }
535
536 /* Set which shader types can be compiled at link time. */
537 st->shader_has_one_variant[MESA_SHADER_VERTEX] =
538 st->has_shareable_shaders &&
539 !st->clamp_vert_color_in_shader;
540
541 st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
542 st->has_shareable_shaders &&
543 !st->clamp_frag_color_in_shader &&
544 !st->force_persample_in_shader;
545
546 st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
547 st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] = st->has_shareable_shaders;
548 st->shader_has_one_variant[MESA_SHADER_GEOMETRY] = st->has_shareable_shaders;
549 st->shader_has_one_variant[MESA_SHADER_COMPUTE] = st->has_shareable_shaders;
550
551 st->bitmap.cache.empty = true;
552
553 _mesa_override_extensions(ctx);
554 _mesa_compute_version(ctx);
555
556 if (ctx->Version == 0) {
557 /* This can happen when a core profile was requested, but the driver
558 * does not support some features of GL 3.1 or later.
559 */
560 st_destroy_context_priv(st, false);
561 return NULL;
562 }
563
564 _mesa_initialize_dispatch_tables(ctx);
565 _mesa_initialize_vbo_vtxfmt(ctx);
566 st_init_driver_flags(st);
567
568 /* Initialize context's winsys buffers list */
569 LIST_INITHEAD(&st->winsys_buffers);
570
571 return st;
572 }
573
574
575 static void
576 st_emit_string_marker(struct gl_context *ctx, const GLchar *string, GLsizei len)
577 {
578 struct st_context *st = ctx->st;
579 st->pipe->emit_string_marker(st->pipe, string, len);
580 }
581
582
583 static void
584 st_set_background_context(struct gl_context *ctx,
585 struct util_queue_monitoring *queue_info)
586 {
587 struct st_context *st = ctx->st;
588 struct st_manager *smapi =
589 (struct st_manager *) st->iface.st_context_private;
590
591 assert(smapi->set_background_context);
592 smapi->set_background_context(&st->iface, queue_info);
593 }
594
595
596 static void
597 st_get_device_uuid(struct gl_context *ctx, char *uuid)
598 {
599 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
600
601 assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
602 memset(uuid, 0, GL_UUID_SIZE_EXT);
603 screen->get_device_uuid(screen, uuid);
604 }
605
606
607 static void
608 st_get_driver_uuid(struct gl_context *ctx, char *uuid)
609 {
610 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
611
612 assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
613 memset(uuid, 0, GL_UUID_SIZE_EXT);
614 screen->get_driver_uuid(screen, uuid);
615 }
616
617
618 static void
619 st_init_driver_functions(struct pipe_screen *screen,
620 struct dd_function_table *functions)
621 {
622 _mesa_init_sampler_object_functions(functions);
623
624 st_init_draw_functions(functions);
625 st_init_blit_functions(functions);
626 st_init_bufferobject_functions(screen, functions);
627 st_init_clear_functions(functions);
628 st_init_bitmap_functions(functions);
629 st_init_copy_image_functions(functions);
630 st_init_drawpixels_functions(functions);
631 st_init_rasterpos_functions(functions);
632
633 st_init_drawtex_functions(functions);
634
635 st_init_eglimage_functions(functions);
636
637 st_init_fbo_functions(functions);
638 st_init_feedback_functions(functions);
639 st_init_memoryobject_functions(functions);
640 st_init_msaa_functions(functions);
641 st_init_perfmon_functions(functions);
642 st_init_program_functions(functions);
643 st_init_query_functions(functions);
644 st_init_cond_render_functions(functions);
645 st_init_readpixels_functions(functions);
646 st_init_semaphoreobject_functions(functions);
647 st_init_texture_functions(functions);
648 st_init_texture_barrier_functions(functions);
649 st_init_flush_functions(screen, functions);
650 st_init_string_functions(functions);
651 st_init_viewport_functions(functions);
652 st_init_compute_functions(functions);
653
654 st_init_xformfb_functions(functions);
655 st_init_syncobj_functions(functions);
656
657 st_init_vdpau_functions(functions);
658
659 if (screen->get_param(screen, PIPE_CAP_STRING_MARKER))
660 functions->EmitStringMarker = st_emit_string_marker;
661
662 functions->Enable = st_Enable;
663 functions->UpdateState = st_invalidate_state;
664 functions->QueryMemoryInfo = st_query_memory_info;
665 functions->SetBackgroundContext = st_set_background_context;
666 functions->GetDriverUuid = st_get_driver_uuid;
667 functions->GetDeviceUuid = st_get_device_uuid;
668
669 /* GL_ARB_get_program_binary */
670 functions->GetProgramBinaryDriverSHA1 = st_get_program_binary_driver_sha1;
671
672 enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir)
673 screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
674 PIPE_SHADER_CAP_PREFERRED_IR);
675 if (preferred_ir == PIPE_SHADER_IR_NIR) {
676 functions->ShaderCacheSerializeDriverBlob = st_serialise_nir_program;
677 functions->ProgramBinarySerializeDriverBlob =
678 st_serialise_nir_program_binary;
679 functions->ProgramBinaryDeserializeDriverBlob =
680 st_deserialise_nir_program;
681 } else {
682 functions->ShaderCacheSerializeDriverBlob = st_serialise_tgsi_program;
683 functions->ProgramBinarySerializeDriverBlob =
684 st_serialise_tgsi_program_binary;
685 functions->ProgramBinaryDeserializeDriverBlob =
686 st_deserialise_tgsi_program;
687 }
688 }
689
690
691 struct st_context *
692 st_create_context(gl_api api, struct pipe_context *pipe,
693 const struct gl_config *visual,
694 struct st_context *share,
695 const struct st_config_options *options,
696 bool no_error)
697 {
698 struct gl_context *ctx;
699 struct gl_context *shareCtx = share ? share->ctx : NULL;
700 struct dd_function_table funcs;
701 struct st_context *st;
702
703 util_cpu_detect();
704
705 memset(&funcs, 0, sizeof(funcs));
706 st_init_driver_functions(pipe->screen, &funcs);
707
708 ctx = calloc(1, sizeof(struct gl_context));
709 if (!ctx)
710 return NULL;
711
712 if (!_mesa_initialize_context(ctx, api, visual, shareCtx, &funcs)) {
713 free(ctx);
714 return NULL;
715 }
716
717 st_debug_init();
718
719 if (pipe->screen->get_disk_shader_cache &&
720 !(ST_DEBUG & DEBUG_TGSI))
721 ctx->Cache = pipe->screen->get_disk_shader_cache(pipe->screen);
722
723 /* XXX: need a capability bit in gallium to query if the pipe
724 * driver prefers DP4 or MUL/MAD for vertex transformation.
725 */
726 if (debug_get_option_mesa_mvp_dp4())
727 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = GL_TRUE;
728
729 st = st_create_context_priv(ctx, pipe, options, no_error);
730 if (!st) {
731 _mesa_destroy_context(ctx);
732 }
733
734 return st;
735 }
736
737
738 /**
739 * When we destroy a context, we must examine all texture objects to
740 * find/release any sampler views created by that context.
741 *
742 * This callback is called per-texture object. It releases all the
743 * texture's sampler views which belong to the context.
744 */
745 static void
746 destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
747 {
748 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
749 struct st_context *st = (struct st_context *) userData;
750
751 st_texture_release_sampler_view(st, st_texture_object(texObj));
752 }
753
754
755 void
756 st_destroy_context(struct st_context *st)
757 {
758 struct gl_context *ctx = st->ctx;
759 struct st_framebuffer *stfb, *next;
760
761 GET_CURRENT_CONTEXT(curctx);
762
763 if (curctx == NULL) {
764 /* No current context, but we need one to release
765 * renderbuffer surface when we release framebuffer.
766 * So temporarily bind the context.
767 */
768 _mesa_make_current(ctx, NULL, NULL);
769 }
770
771 /* This must be called first so that glthread has a chance to finish */
772 _mesa_glthread_destroy(ctx);
773
774 _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
775
776 st_reference_fragprog(st, &st->fp, NULL);
777 st_reference_prog(st, &st->gp, NULL);
778 st_reference_vertprog(st, &st->vp, NULL);
779 st_reference_prog(st, &st->tcp, NULL);
780 st_reference_prog(st, &st->tep, NULL);
781 st_reference_compprog(st, &st->cp, NULL);
782
783 /* release framebuffer in the winsys buffers list */
784 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
785 st_framebuffer_reference(&stfb, NULL);
786 }
787
788 pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
789 pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
790
791 _vbo_DestroyContext(ctx);
792
793 st_destroy_program_variants(st);
794
795 _mesa_free_context_data(ctx);
796
797 /* This will free the st_context too, so 'st' must not be accessed
798 * afterwards. */
799 st_destroy_context_priv(st, true);
800 st = NULL;
801
802 free(ctx);
803 }