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