replace _mesa_logbase2 with util_logbase2
[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 "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"
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_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"
72 #include "st_atom.h"
73 #include "st_draw.h"
74 #include "st_extensions.h"
75 #include "st_gen_mipmap.h"
76 #include "st_pbo.h"
77 #include "st_program.h"
78 #include "st_sampler_view.h"
79 #include "st_shader_cache.h"
80 #include "st_vdpau.h"
81 #include "st_texture.h"
82 #include "st_util.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 "cso_cache/cso_context.h"
89 #include "compiler/glsl/glsl_parser_extras.h"
90
91
92 DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
93
94
95 /**
96 * Called via ctx->Driver.Enable()
97 */
98 static void
99 st_Enable(struct gl_context *ctx, GLenum cap, GLboolean state)
100 {
101 struct st_context *st = st_context(ctx);
102
103 switch (cap) {
104 case GL_DEBUG_OUTPUT:
105 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
106 st_update_debug_callback(st);
107 break;
108 case GL_BLACKHOLE_RENDER_INTEL:
109 st->pipe->set_frontend_noop(st->pipe, ctx->IntelBlackholeRender);
110 break;
111 default:
112 break;
113 }
114 }
115
116
117 /**
118 * Called via ctx->Driver.QueryMemoryInfo()
119 */
120 static void
121 st_query_memory_info(struct gl_context *ctx, struct gl_memory_info *out)
122 {
123 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
124 struct pipe_memory_info info;
125
126 assert(screen->query_memory_info);
127 if (!screen->query_memory_info)
128 return;
129
130 screen->query_memory_info(screen, &info);
131
132 out->total_device_memory = info.total_device_memory;
133 out->avail_device_memory = info.avail_device_memory;
134 out->total_staging_memory = info.total_staging_memory;
135 out->avail_staging_memory = info.avail_staging_memory;
136 out->device_memory_evicted = info.device_memory_evicted;
137 out->nr_device_memory_evictions = info.nr_device_memory_evictions;
138 }
139
140
141 static uint64_t
142 st_get_active_states(struct gl_context *ctx)
143 {
144 struct st_program *vp =
145 st_program(ctx->VertexProgram._Current);
146 struct st_program *tcp =
147 st_program(ctx->TessCtrlProgram._Current);
148 struct st_program *tep =
149 st_program(ctx->TessEvalProgram._Current);
150 struct st_program *gp =
151 st_program(ctx->GeometryProgram._Current);
152 struct st_program *fp =
153 st_program(ctx->FragmentProgram._Current);
154 struct st_program *cp =
155 st_program(ctx->ComputeProgram._Current);
156 uint64_t active_shader_states = 0;
157
158 if (vp)
159 active_shader_states |= vp->affected_states;
160 if (tcp)
161 active_shader_states |= tcp->affected_states;
162 if (tep)
163 active_shader_states |= tep->affected_states;
164 if (gp)
165 active_shader_states |= gp->affected_states;
166 if (fp)
167 active_shader_states |= fp->affected_states;
168 if (cp)
169 active_shader_states |= cp->affected_states;
170
171 /* Mark non-shader-resource shader states as "always active". */
172 return active_shader_states | ~ST_ALL_SHADER_RESOURCES;
173 }
174
175
176 void
177 st_invalidate_buffers(struct st_context *st)
178 {
179 st->dirty |= ST_NEW_BLEND |
180 ST_NEW_DSA |
181 ST_NEW_FB_STATE |
182 ST_NEW_SAMPLE_STATE |
183 ST_NEW_SAMPLE_SHADING |
184 ST_NEW_FS_STATE |
185 ST_NEW_POLY_STIPPLE |
186 ST_NEW_VIEWPORT |
187 ST_NEW_RASTERIZER |
188 ST_NEW_SCISSOR |
189 ST_NEW_WINDOW_RECTANGLES;
190 }
191
192
193 static inline bool
194 st_vp_uses_current_values(const struct gl_context *ctx)
195 {
196 const uint64_t inputs = ctx->VertexProgram._Current->info.inputs_read;
197 return _mesa_draw_current_bits(ctx) & inputs;
198 }
199
200
201 /**
202 * Called via ctx->Driver.UpdateState()
203 */
204 static void
205 st_invalidate_state(struct gl_context *ctx)
206 {
207 GLbitfield new_state = ctx->NewState;
208 struct st_context *st = st_context(ctx);
209
210 if (new_state & _NEW_BUFFERS) {
211 st_invalidate_buffers(st);
212 } else {
213 /* These set a subset of flags set by _NEW_BUFFERS, so we only have to
214 * check them when _NEW_BUFFERS isn't set.
215 */
216 if (new_state & _NEW_PROGRAM)
217 st->dirty |= ST_NEW_RASTERIZER;
218
219 if (new_state & _NEW_FOG)
220 st->dirty |= ST_NEW_FS_STATE;
221
222 if (new_state & _NEW_FRAG_CLAMP) {
223 if (st->clamp_frag_color_in_shader)
224 st->dirty |= ST_NEW_FS_STATE;
225 else
226 st->dirty |= ST_NEW_RASTERIZER;
227 }
228 }
229
230 if (new_state & (_NEW_LIGHT |
231 _NEW_POINT))
232 st->dirty |= ST_NEW_RASTERIZER;
233
234 if ((new_state & _NEW_LIGHT) &&
235 (st->lower_flatshade || st->lower_two_sided_color))
236 st->dirty |= ST_NEW_FS_STATE;
237
238 if (new_state & _NEW_PROJECTION &&
239 st_user_clip_planes_enabled(ctx))
240 st->dirty |= ST_NEW_CLIP_STATE;
241
242 if (new_state & _NEW_PIXEL)
243 st->dirty |= ST_NEW_PIXEL_TRANSFER;
244
245 if (new_state & _NEW_CURRENT_ATTRIB && st_vp_uses_current_values(ctx))
246 st->dirty |= ST_NEW_VERTEX_ARRAYS;
247
248 if (st->clamp_frag_depth_in_shader && (new_state & _NEW_VIEWPORT)) {
249 if (ctx->GeometryProgram._Current)
250 st->dirty |= ST_NEW_GS_CONSTANTS;
251 else if (ctx->TessEvalProgram._Current)
252 st->dirty |= ST_NEW_TES_CONSTANTS;
253 else
254 st->dirty |= ST_NEW_VS_CONSTANTS;
255 st->dirty |= ST_NEW_FS_CONSTANTS;
256 }
257
258 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
259 if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT)) {
260 st->dirty |= ST_NEW_VS_STATE;
261 if (st->ctx->API == API_OPENGL_COMPAT && ctx->Version >= 32) {
262 st->dirty |= ST_NEW_GS_STATE | ST_NEW_TES_STATE;
263 }
264 }
265
266 /* Which shaders are dirty will be determined manually. */
267 if (new_state & _NEW_PROGRAM) {
268 st->gfx_shaders_may_be_dirty = true;
269 st->compute_shader_may_be_dirty = true;
270 /* This will mask out unused shader resources. */
271 st->active_states = st_get_active_states(ctx);
272 }
273
274 if (new_state & _NEW_TEXTURE_OBJECT) {
275 st->dirty |= st->active_states &
276 (ST_NEW_SAMPLER_VIEWS |
277 ST_NEW_SAMPLERS |
278 ST_NEW_IMAGE_UNITS);
279 if (ctx->FragmentProgram._Current &&
280 ctx->FragmentProgram._Current->ExternalSamplersUsed) {
281 st->dirty |= ST_NEW_FS_STATE;
282 }
283 }
284 }
285
286
287 /*
288 * In some circumstances (such as running google-chrome) the state
289 * tracker may try to delete a resource view from a context different
290 * than when it was created. We don't want to do that.
291 *
292 * In that situation, st_texture_release_all_sampler_views() calls this
293 * function to transfer the sampler view reference to this context (expected
294 * to be the context which created the view.)
295 */
296 void
297 st_save_zombie_sampler_view(struct st_context *st,
298 struct pipe_sampler_view *view)
299 {
300 struct st_zombie_sampler_view_node *entry;
301
302 assert(view->context == st->pipe);
303
304 entry = MALLOC_STRUCT(st_zombie_sampler_view_node);
305 if (!entry)
306 return;
307
308 entry->view = view;
309
310 /* We need a mutex since this function may be called from one thread
311 * while free_zombie_resource_views() is called from another.
312 */
313 simple_mtx_lock(&st->zombie_sampler_views.mutex);
314 list_addtail(&entry->node, &st->zombie_sampler_views.list.node);
315 simple_mtx_unlock(&st->zombie_sampler_views.mutex);
316 }
317
318
319 /*
320 * Since OpenGL shaders may be shared among contexts, we can wind up
321 * with variants of a shader created with different contexts.
322 * When we go to destroy a gallium shader, we want to free it with the
323 * same context that it was created with, unless the driver reports
324 * PIPE_CAP_SHAREABLE_SHADERS = TRUE.
325 */
326 void
327 st_save_zombie_shader(struct st_context *st,
328 enum pipe_shader_type type,
329 struct pipe_shader_state *shader)
330 {
331 struct st_zombie_shader_node *entry;
332
333 /* we shouldn't be here if the driver supports shareable shaders */
334 assert(!st->has_shareable_shaders);
335
336 entry = MALLOC_STRUCT(st_zombie_shader_node);
337 if (!entry)
338 return;
339
340 entry->shader = shader;
341 entry->type = type;
342
343 /* We need a mutex since this function may be called from one thread
344 * while free_zombie_shaders() is called from another.
345 */
346 simple_mtx_lock(&st->zombie_shaders.mutex);
347 list_addtail(&entry->node, &st->zombie_shaders.list.node);
348 simple_mtx_unlock(&st->zombie_shaders.mutex);
349 }
350
351
352 /*
353 * Free any zombie sampler views that may be attached to this context.
354 */
355 static void
356 free_zombie_sampler_views(struct st_context *st)
357 {
358 struct st_zombie_sampler_view_node *entry, *next;
359
360 if (list_is_empty(&st->zombie_sampler_views.list.node)) {
361 return;
362 }
363
364 simple_mtx_lock(&st->zombie_sampler_views.mutex);
365
366 LIST_FOR_EACH_ENTRY_SAFE(entry, next,
367 &st->zombie_sampler_views.list.node, node) {
368 list_del(&entry->node); // remove this entry from the list
369
370 assert(entry->view->context == st->pipe);
371 pipe_sampler_view_reference(&entry->view, NULL);
372
373 free(entry);
374 }
375
376 assert(list_is_empty(&st->zombie_sampler_views.list.node));
377
378 simple_mtx_unlock(&st->zombie_sampler_views.mutex);
379 }
380
381
382 /*
383 * Free any zombie shaders that may be attached to this context.
384 */
385 static void
386 free_zombie_shaders(struct st_context *st)
387 {
388 struct st_zombie_shader_node *entry, *next;
389
390 if (list_is_empty(&st->zombie_shaders.list.node)) {
391 return;
392 }
393
394 simple_mtx_lock(&st->zombie_shaders.mutex);
395
396 LIST_FOR_EACH_ENTRY_SAFE(entry, next,
397 &st->zombie_shaders.list.node, node) {
398 list_del(&entry->node); // remove this entry from the list
399
400 switch (entry->type) {
401 case PIPE_SHADER_VERTEX:
402 st->pipe->bind_vs_state(st->pipe, NULL);
403 st->pipe->delete_vs_state(st->pipe, entry->shader);
404 break;
405 case PIPE_SHADER_FRAGMENT:
406 st->pipe->bind_fs_state(st->pipe, NULL);
407 st->pipe->delete_fs_state(st->pipe, entry->shader);
408 break;
409 case PIPE_SHADER_GEOMETRY:
410 st->pipe->bind_gs_state(st->pipe, NULL);
411 st->pipe->delete_gs_state(st->pipe, entry->shader);
412 break;
413 case PIPE_SHADER_TESS_CTRL:
414 st->pipe->bind_tcs_state(st->pipe, NULL);
415 st->pipe->delete_tcs_state(st->pipe, entry->shader);
416 break;
417 case PIPE_SHADER_TESS_EVAL:
418 st->pipe->bind_tes_state(st->pipe, NULL);
419 st->pipe->delete_tes_state(st->pipe, entry->shader);
420 break;
421 case PIPE_SHADER_COMPUTE:
422 st->pipe->bind_compute_state(st->pipe, NULL);
423 st->pipe->delete_compute_state(st->pipe, entry->shader);
424 break;
425 default:
426 unreachable("invalid shader type in free_zombie_shaders()");
427 }
428 free(entry);
429 }
430
431 assert(list_is_empty(&st->zombie_shaders.list.node));
432
433 simple_mtx_unlock(&st->zombie_shaders.mutex);
434 }
435
436
437 /*
438 * This function is called periodically to free any zombie objects
439 * which are attached to this context.
440 */
441 void
442 st_context_free_zombie_objects(struct st_context *st)
443 {
444 free_zombie_sampler_views(st);
445 free_zombie_shaders(st);
446 }
447
448
449 static void
450 st_destroy_context_priv(struct st_context *st, bool destroy_pipe)
451 {
452 uint i;
453
454 st_destroy_atoms(st);
455 st_destroy_draw(st);
456 st_destroy_clear(st);
457 st_destroy_bitmap(st);
458 st_destroy_drawpix(st);
459 st_destroy_drawtex(st);
460 st_destroy_perfmon(st);
461 st_destroy_pbo_helpers(st);
462 st_destroy_bound_texture_handles(st);
463 st_destroy_bound_image_handles(st);
464
465 for (i = 0; i < ARRAY_SIZE(st->state.frag_sampler_views); i++) {
466 pipe_sampler_view_reference(&st->state.vert_sampler_views[i], NULL);
467 pipe_sampler_view_reference(&st->state.frag_sampler_views[i], NULL);
468 }
469
470 /* free glReadPixels cache data */
471 st_invalidate_readpix_cache(st);
472 util_throttle_deinit(st->pipe->screen, &st->throttle);
473
474 cso_destroy_context(st->cso_context);
475
476 if (st->pipe && destroy_pipe)
477 st->pipe->destroy(st->pipe);
478
479 free(st);
480 }
481
482
483 static void
484 st_init_driver_flags(struct st_context *st)
485 {
486 struct gl_driver_flags *f = &st->ctx->DriverFlags;
487
488 f->NewArray = ST_NEW_VERTEX_ARRAYS;
489 f->NewRasterizerDiscard = ST_NEW_RASTERIZER;
490 f->NewTileRasterOrder = ST_NEW_RASTERIZER;
491 f->NewUniformBuffer = ST_NEW_UNIFORM_BUFFER;
492 f->NewDefaultTessLevels = ST_NEW_TESS_STATE;
493
494 /* Shader resources */
495 f->NewTextureBuffer = ST_NEW_SAMPLER_VIEWS;
496 if (st->has_hw_atomics)
497 f->NewAtomicBuffer = ST_NEW_HW_ATOMICS | ST_NEW_CS_ATOMICS;
498 else
499 f->NewAtomicBuffer = ST_NEW_ATOMIC_BUFFER;
500 f->NewShaderStorageBuffer = ST_NEW_STORAGE_BUFFER;
501 f->NewImageUnits = ST_NEW_IMAGE_UNITS;
502
503 f->NewShaderConstants[MESA_SHADER_VERTEX] = ST_NEW_VS_CONSTANTS;
504 f->NewShaderConstants[MESA_SHADER_TESS_CTRL] = ST_NEW_TCS_CONSTANTS;
505 f->NewShaderConstants[MESA_SHADER_TESS_EVAL] = ST_NEW_TES_CONSTANTS;
506 f->NewShaderConstants[MESA_SHADER_GEOMETRY] = ST_NEW_GS_CONSTANTS;
507 f->NewShaderConstants[MESA_SHADER_FRAGMENT] = ST_NEW_FS_CONSTANTS;
508 f->NewShaderConstants[MESA_SHADER_COMPUTE] = ST_NEW_CS_CONSTANTS;
509
510 f->NewWindowRectangles = ST_NEW_WINDOW_RECTANGLES;
511 f->NewFramebufferSRGB = ST_NEW_FB_STATE;
512 f->NewScissorRect = ST_NEW_SCISSOR;
513 f->NewScissorTest = ST_NEW_SCISSOR | ST_NEW_RASTERIZER;
514
515 if (st->lower_alpha_test)
516 f->NewAlphaTest = ST_NEW_FS_STATE;
517 else
518 f->NewAlphaTest = ST_NEW_DSA;
519
520 f->NewBlend = ST_NEW_BLEND;
521 f->NewBlendColor = ST_NEW_BLEND_COLOR;
522 f->NewColorMask = ST_NEW_BLEND;
523 f->NewDepth = ST_NEW_DSA;
524 f->NewLogicOp = ST_NEW_BLEND;
525 f->NewStencil = ST_NEW_DSA;
526 f->NewMultisampleEnable = ST_NEW_BLEND | ST_NEW_RASTERIZER |
527 ST_NEW_SAMPLE_STATE | ST_NEW_SAMPLE_SHADING;
528 f->NewSampleAlphaToXEnable = ST_NEW_BLEND;
529 f->NewSampleMask = ST_NEW_SAMPLE_STATE;
530 f->NewSampleLocations = ST_NEW_SAMPLE_STATE;
531 f->NewSampleShading = ST_NEW_SAMPLE_SHADING;
532
533 /* This depends on what the gallium driver wants. */
534 if (st->force_persample_in_shader) {
535 f->NewMultisampleEnable |= ST_NEW_FS_STATE;
536 f->NewSampleShading |= ST_NEW_FS_STATE;
537 } else {
538 f->NewSampleShading |= ST_NEW_RASTERIZER;
539 }
540
541 f->NewClipControl = ST_NEW_VIEWPORT | ST_NEW_RASTERIZER;
542 f->NewClipPlane = ST_NEW_CLIP_STATE;
543
544 if (st->clamp_frag_depth_in_shader) {
545 f->NewClipControl |= ST_NEW_VS_STATE | ST_NEW_GS_STATE |
546 ST_NEW_TES_STATE;
547
548 f->NewDepthClamp = ST_NEW_FS_STATE | ST_NEW_VS_STATE |
549 ST_NEW_GS_STATE | ST_NEW_TES_STATE;
550 } else {
551 f->NewDepthClamp = ST_NEW_RASTERIZER;
552 }
553
554 if (st->lower_ucp)
555 f->NewClipPlaneEnable = ST_NEW_VS_STATE;
556 else
557 f->NewClipPlaneEnable = ST_NEW_RASTERIZER;
558
559 f->NewLineState = ST_NEW_RASTERIZER;
560 f->NewPolygonState = ST_NEW_RASTERIZER;
561 f->NewPolygonStipple = ST_NEW_POLY_STIPPLE;
562 f->NewViewport = ST_NEW_VIEWPORT;
563 f->NewNvConservativeRasterization = ST_NEW_RASTERIZER;
564 f->NewNvConservativeRasterizationParams = ST_NEW_RASTERIZER;
565 f->NewIntelConservativeRasterization = ST_NEW_RASTERIZER;
566 }
567
568
569 static struct st_context *
570 st_create_context_priv(struct gl_context *ctx, struct pipe_context *pipe,
571 const struct st_config_options *options, bool no_error)
572 {
573 struct pipe_screen *screen = pipe->screen;
574 uint i;
575 struct st_context *st = ST_CALLOC_STRUCT( st_context);
576
577 st->options = *options;
578
579 ctx->st = st;
580
581 st->ctx = ctx;
582 st->pipe = pipe;
583 st->dirty = ST_ALL_STATES_MASK;
584
585 st->can_bind_const_buffer_as_vertex =
586 screen->get_param(screen, PIPE_CAP_CAN_BIND_CONST_BUFFER_AS_VERTEX);
587
588 /* st/mesa always uploads zero-stride vertex attribs, and other user
589 * vertex buffers are only possible with a compatibility profile.
590 * So tell the u_vbuf module that user VBOs are not possible with the Core
591 * profile, so that u_vbuf is bypassed completely if there is nothing else
592 * to do.
593 */
594 unsigned cso_flags =
595 ctx->API == API_OPENGL_CORE ? CSO_NO_USER_VERTEX_BUFFERS : 0;
596 st->cso_context = cso_create_context(pipe, cso_flags);
597
598 st_init_atoms(st);
599 st_init_clear(st);
600 st_init_pbo_helpers(st);
601
602 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
603 if (pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
604 st->internal_target = PIPE_TEXTURE_2D;
605 else
606 st->internal_target = PIPE_TEXTURE_RECT;
607
608 /* Setup vertex element info for 'struct st_util_vertex'.
609 */
610 {
611 STATIC_ASSERT(sizeof(struct st_util_vertex) == 9 * sizeof(float));
612
613 memset(&st->util_velems, 0, sizeof(st->util_velems));
614 st->util_velems.velems[0].src_offset = 0;
615 st->util_velems.velems[0].vertex_buffer_index = 0;
616 st->util_velems.velems[0].src_format = PIPE_FORMAT_R32G32B32_FLOAT;
617 st->util_velems.velems[1].src_offset = 3 * sizeof(float);
618 st->util_velems.velems[1].vertex_buffer_index = 0;
619 st->util_velems.velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
620 st->util_velems.velems[2].src_offset = 7 * sizeof(float);
621 st->util_velems.velems[2].vertex_buffer_index = 0;
622 st->util_velems.velems[2].src_format = PIPE_FORMAT_R32G32_FLOAT;
623 }
624
625 /* Need these flags:
626 */
627 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
628 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
629
630 if (no_error)
631 ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
632
633 ctx->Const.PackedDriverUniformStorage =
634 screen->get_param(screen, PIPE_CAP_PACKED_UNIFORMS);
635
636 ctx->Const.BitmapUsesRed =
637 screen->is_format_supported(screen, PIPE_FORMAT_R8_UNORM,
638 PIPE_TEXTURE_2D, 0, 0,
639 PIPE_BIND_SAMPLER_VIEW);
640
641 st->has_stencil_export =
642 screen->get_param(screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
643 st->has_etc1 = screen->is_format_supported(screen, PIPE_FORMAT_ETC1_RGB8,
644 PIPE_TEXTURE_2D, 0, 0,
645 PIPE_BIND_SAMPLER_VIEW);
646 st->has_etc2 = screen->is_format_supported(screen, PIPE_FORMAT_ETC2_RGB8,
647 PIPE_TEXTURE_2D, 0, 0,
648 PIPE_BIND_SAMPLER_VIEW);
649 st->has_astc_2d_ldr =
650 screen->is_format_supported(screen, PIPE_FORMAT_ASTC_4x4_SRGB,
651 PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW);
652 st->has_astc_5x5_ldr =
653 screen->is_format_supported(screen, PIPE_FORMAT_ASTC_5x5_SRGB,
654 PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW);
655 st->prefer_blit_based_texture_transfer = screen->get_param(screen,
656 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER);
657 st->force_persample_in_shader =
658 screen->get_param(screen, PIPE_CAP_SAMPLE_SHADING) &&
659 !screen->get_param(screen, PIPE_CAP_FORCE_PERSAMPLE_INTERP);
660 st->has_shareable_shaders = screen->get_param(screen,
661 PIPE_CAP_SHAREABLE_SHADERS);
662 st->needs_texcoord_semantic =
663 screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
664 st->apply_texture_swizzle_to_border_color =
665 !!(screen->get_param(screen, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK) &
666 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50 |
667 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600));
668 st->has_time_elapsed =
669 screen->get_param(screen, PIPE_CAP_QUERY_TIME_ELAPSED);
670 st->has_half_float_packing =
671 screen->get_param(screen, PIPE_CAP_TGSI_PACK_HALF_FLOAT);
672 st->has_multi_draw_indirect =
673 screen->get_param(screen, PIPE_CAP_MULTI_DRAW_INDIRECT);
674 st->has_single_pipe_stat =
675 screen->get_param(screen, PIPE_CAP_QUERY_PIPELINE_STATISTICS_SINGLE);
676 st->has_indep_blend_func =
677 screen->get_param(screen, PIPE_CAP_INDEP_BLEND_FUNC);
678 st->needs_rgb_dst_alpha_override =
679 screen->get_param(screen, PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND);
680 st->lower_flatshade =
681 !screen->get_param(screen, PIPE_CAP_FLATSHADE);
682 st->lower_alpha_test =
683 !screen->get_param(screen, PIPE_CAP_ALPHA_TEST);
684 st->lower_point_size =
685 !screen->get_param(screen, PIPE_CAP_POINT_SIZE_FIXED);
686 st->lower_two_sided_color =
687 !screen->get_param(screen, PIPE_CAP_TWO_SIDED_COLOR);
688 st->lower_ucp =
689 !screen->get_param(screen, PIPE_CAP_CLIP_PLANES);
690 st->allow_st_finalize_nir_twice = screen->finalize_nir != NULL;
691
692 st->has_hw_atomics =
693 screen->get_shader_param(screen, PIPE_SHADER_FRAGMENT,
694 PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS)
695 ? true : false;
696
697 util_throttle_init(&st->throttle,
698 screen->get_param(screen,
699 PIPE_CAP_MAX_TEXTURE_UPLOAD_MEMORY_BUDGET));
700
701 /* GL limits and extensions */
702 st_init_limits(pipe->screen, &ctx->Const, &ctx->Extensions);
703 st_init_extensions(pipe->screen, &ctx->Const,
704 &ctx->Extensions, &st->options, ctx->API);
705
706 /* FIXME: add support for geometry and tessellation shaders for
707 * lower_point_size
708 */
709 assert(!ctx->Extensions.OES_geometry_shader || !st->lower_point_size);
710 assert(!ctx->Extensions.ARB_tessellation_shader || !st->lower_point_size);
711
712 /* FIXME: add support for geometry and tessellation shaders for
713 * lower_ucp
714 */
715 assert(!ctx->Extensions.OES_geometry_shader || !st->lower_ucp);
716 assert(!ctx->Extensions.ARB_tessellation_shader || !st->lower_ucp);
717
718 if (st_have_perfmon(st)) {
719 ctx->Extensions.AMD_performance_monitor = GL_TRUE;
720 }
721
722 if (st_have_perfquery(st)) {
723 ctx->Extensions.INTEL_performance_query = GL_TRUE;
724 }
725
726 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
727 if (screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_UNCLAMPED)) {
728 if (!screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_CLAMPED)) {
729 st->clamp_vert_color_in_shader = GL_TRUE;
730 }
731
732 if (!screen->get_param(screen, PIPE_CAP_FRAGMENT_COLOR_CLAMPED)) {
733 st->clamp_frag_color_in_shader = GL_TRUE;
734 }
735
736 /* For drivers which cannot do color clamping, it's better to just
737 * disable ARB_color_buffer_float in the core profile, because
738 * the clamping is deprecated there anyway. */
739 if (ctx->API == API_OPENGL_CORE &&
740 (st->clamp_frag_color_in_shader || st->clamp_vert_color_in_shader)) {
741 st->clamp_vert_color_in_shader = GL_FALSE;
742 st->clamp_frag_color_in_shader = GL_FALSE;
743 ctx->Extensions.ARB_color_buffer_float = GL_FALSE;
744 }
745 }
746
747 if (screen->get_param(screen, PIPE_CAP_DEPTH_CLIP_DISABLE) == 2)
748 st->clamp_frag_depth_in_shader = true;
749
750 /* called after _mesa_create_context/_mesa_init_point, fix default user
751 * settable max point size up
752 */
753 ctx->Point.MaxSize = MAX2(ctx->Const.MaxPointSize,
754 ctx->Const.MaxPointSizeAA);
755 /* For vertex shaders, make sure not to emit saturate when SM 3.0
756 * is not supported
757 */
758 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitNoSat =
759 !screen->get_param(screen, PIPE_CAP_VERTEX_SHADER_SATURATE);
760
761 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].PositionAlwaysInvariant = options->vs_position_always_invariant;
762
763 enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir)
764 screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
765 PIPE_SHADER_CAP_PREFERRED_IR);
766 ctx->Const.UseNIRGLSLLinker = preferred_ir == PIPE_SHADER_IR_NIR;
767
768 if (ctx->Const.GLSLVersion < 400) {
769 for (i = 0; i < MESA_SHADER_STAGES; i++)
770 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler = true;
771 }
772
773 /* Set which shader types can be compiled at link time. */
774 st->shader_has_one_variant[MESA_SHADER_VERTEX] =
775 st->has_shareable_shaders &&
776 !st->clamp_frag_depth_in_shader &&
777 !st->clamp_vert_color_in_shader &&
778 !st->lower_point_size &&
779 !st->lower_ucp;
780
781 st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
782 st->has_shareable_shaders &&
783 !st->lower_flatshade &&
784 !st->lower_alpha_test &&
785 !st->clamp_frag_color_in_shader &&
786 !st->clamp_frag_depth_in_shader &&
787 !st->force_persample_in_shader &&
788 !st->lower_two_sided_color;
789
790 st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
791 st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] =
792 st->has_shareable_shaders &&
793 !st->clamp_frag_depth_in_shader &&
794 !st->clamp_vert_color_in_shader;
795 st->shader_has_one_variant[MESA_SHADER_GEOMETRY] =
796 st->has_shareable_shaders &&
797 !st->clamp_frag_depth_in_shader &&
798 !st->clamp_vert_color_in_shader;
799 st->shader_has_one_variant[MESA_SHADER_COMPUTE] = st->has_shareable_shaders;
800
801 st->bitmap.cache.empty = true;
802
803 _mesa_override_extensions(ctx);
804 _mesa_compute_version(ctx);
805
806 if (ctx->Version == 0) {
807 /* This can happen when a core profile was requested, but the driver
808 * does not support some features of GL 3.1 or later.
809 */
810 st_destroy_context_priv(st, false);
811 return NULL;
812 }
813
814 /* This must be done after extensions are initialized to enable persistent
815 * mappings immediately.
816 */
817 _vbo_CreateContext(ctx, true);
818
819 _mesa_initialize_dispatch_tables(ctx);
820 _mesa_initialize_vbo_vtxfmt(ctx);
821 st_init_driver_flags(st);
822
823 /* Initialize context's winsys buffers list */
824 list_inithead(&st->winsys_buffers);
825
826 list_inithead(&st->zombie_sampler_views.list.node);
827 simple_mtx_init(&st->zombie_sampler_views.mutex, mtx_plain);
828 list_inithead(&st->zombie_shaders.list.node);
829 simple_mtx_init(&st->zombie_shaders.mutex, mtx_plain);
830
831 return st;
832 }
833
834
835 static void
836 st_emit_string_marker(struct gl_context *ctx, const GLchar *string, GLsizei len)
837 {
838 struct st_context *st = ctx->st;
839 st->pipe->emit_string_marker(st->pipe, string, len);
840 }
841
842
843 static void
844 st_set_background_context(struct gl_context *ctx,
845 struct util_queue_monitoring *queue_info)
846 {
847 struct st_context *st = ctx->st;
848 struct st_manager *smapi =
849 (struct st_manager *) st->iface.st_context_private;
850
851 assert(smapi->set_background_context);
852 smapi->set_background_context(&st->iface, queue_info);
853 }
854
855
856 static void
857 st_get_device_uuid(struct gl_context *ctx, char *uuid)
858 {
859 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
860
861 assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
862 memset(uuid, 0, GL_UUID_SIZE_EXT);
863 screen->get_device_uuid(screen, uuid);
864 }
865
866
867 static void
868 st_get_driver_uuid(struct gl_context *ctx, char *uuid)
869 {
870 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
871
872 assert(GL_UUID_SIZE_EXT >= PIPE_UUID_SIZE);
873 memset(uuid, 0, GL_UUID_SIZE_EXT);
874 screen->get_driver_uuid(screen, uuid);
875 }
876
877
878 static void
879 st_init_driver_functions(struct pipe_screen *screen,
880 struct dd_function_table *functions)
881 {
882 _mesa_init_sampler_object_functions(functions);
883
884 st_init_draw_functions(functions);
885 st_init_blit_functions(functions);
886 st_init_bufferobject_functions(screen, functions);
887 st_init_clear_functions(functions);
888 st_init_bitmap_functions(functions);
889 st_init_copy_image_functions(functions);
890 st_init_drawpixels_functions(functions);
891 st_init_rasterpos_functions(functions);
892
893 st_init_drawtex_functions(functions);
894
895 st_init_eglimage_functions(functions);
896
897 st_init_fbo_functions(functions);
898 st_init_feedback_functions(functions);
899 st_init_memoryobject_functions(functions);
900 st_init_msaa_functions(functions);
901 st_init_perfmon_functions(functions);
902 st_init_perfquery_functions(functions);
903 st_init_program_functions(functions);
904 st_init_query_functions(functions);
905 st_init_cond_render_functions(functions);
906 st_init_readpixels_functions(functions);
907 st_init_semaphoreobject_functions(functions);
908 st_init_texture_functions(functions);
909 st_init_texture_barrier_functions(functions);
910 st_init_flush_functions(screen, functions);
911 st_init_string_functions(functions);
912 st_init_viewport_functions(functions);
913 st_init_compute_functions(functions);
914
915 st_init_xformfb_functions(functions);
916 st_init_syncobj_functions(functions);
917
918 st_init_vdpau_functions(functions);
919
920 if (screen->get_param(screen, PIPE_CAP_STRING_MARKER))
921 functions->EmitStringMarker = st_emit_string_marker;
922
923 functions->Enable = st_Enable;
924 functions->UpdateState = st_invalidate_state;
925 functions->QueryMemoryInfo = st_query_memory_info;
926 functions->SetBackgroundContext = st_set_background_context;
927 functions->GetDriverUuid = st_get_driver_uuid;
928 functions->GetDeviceUuid = st_get_device_uuid;
929
930 /* GL_ARB_get_program_binary */
931 functions->GetProgramBinaryDriverSHA1 = st_get_program_binary_driver_sha1;
932
933 enum pipe_shader_ir preferred_ir = (enum pipe_shader_ir)
934 screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
935 PIPE_SHADER_CAP_PREFERRED_IR);
936 if (preferred_ir == PIPE_SHADER_IR_NIR) {
937 functions->ShaderCacheSerializeDriverBlob = st_serialise_nir_program;
938 functions->ProgramBinarySerializeDriverBlob =
939 st_serialise_nir_program_binary;
940 functions->ProgramBinaryDeserializeDriverBlob =
941 st_deserialise_nir_program;
942 } else {
943 functions->ShaderCacheSerializeDriverBlob = st_serialise_tgsi_program;
944 functions->ProgramBinarySerializeDriverBlob =
945 st_serialise_tgsi_program_binary;
946 functions->ProgramBinaryDeserializeDriverBlob =
947 st_deserialise_tgsi_program;
948 }
949 }
950
951
952 struct st_context *
953 st_create_context(gl_api api, struct pipe_context *pipe,
954 const struct gl_config *visual,
955 struct st_context *share,
956 const struct st_config_options *options,
957 bool no_error)
958 {
959 struct gl_context *ctx;
960 struct gl_context *shareCtx = share ? share->ctx : NULL;
961 struct dd_function_table funcs;
962 struct st_context *st;
963
964 util_cpu_detect();
965
966 memset(&funcs, 0, sizeof(funcs));
967 st_init_driver_functions(pipe->screen, &funcs);
968
969 ctx = calloc(1, sizeof(struct gl_context));
970 if (!ctx)
971 return NULL;
972
973 if (!_mesa_initialize_context(ctx, api, visual, shareCtx, &funcs)) {
974 free(ctx);
975 return NULL;
976 }
977
978 st_debug_init();
979
980 if (pipe->screen->get_disk_shader_cache)
981 ctx->Cache = pipe->screen->get_disk_shader_cache(pipe->screen);
982
983 /* XXX: need a capability bit in gallium to query if the pipe
984 * driver prefers DP4 or MUL/MAD for vertex transformation.
985 */
986 if (debug_get_option_mesa_mvp_dp4())
987 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = GL_TRUE;
988
989 st = st_create_context_priv(ctx, pipe, options, no_error);
990 if (!st) {
991 _mesa_destroy_context(ctx);
992 }
993
994 return st;
995 }
996
997
998 /**
999 * When we destroy a context, we must examine all texture objects to
1000 * find/release any sampler views created by that context.
1001 *
1002 * This callback is called per-texture object. It releases all the
1003 * texture's sampler views which belong to the context.
1004 */
1005 static void
1006 destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
1007 {
1008 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
1009 struct st_context *st = (struct st_context *) userData;
1010
1011 st_texture_release_context_sampler_view(st, st_texture_object(texObj));
1012 }
1013
1014 static void
1015 destroy_framebuffer_attachment_sampler_cb(GLuint id, void *data, void *userData)
1016 {
1017 struct gl_framebuffer* glfb = (struct gl_framebuffer*) data;
1018 struct st_context *st = (struct st_context *) userData;
1019
1020 for (unsigned i = 0; i < BUFFER_COUNT; i++) {
1021 struct gl_renderbuffer_attachment *att = &glfb->Attachment[i];
1022 if (att->Texture) {
1023 st_texture_release_context_sampler_view(st, st_texture_object(att->Texture));
1024 }
1025 }
1026 }
1027
1028 void
1029 st_destroy_context(struct st_context *st)
1030 {
1031 struct gl_context *ctx = st->ctx;
1032 struct st_framebuffer *stfb, *next;
1033 struct gl_framebuffer *save_drawbuffer;
1034 struct gl_framebuffer *save_readbuffer;
1035
1036 /* Save the current context and draw/read buffers*/
1037 GET_CURRENT_CONTEXT(save_ctx);
1038 if (save_ctx) {
1039 save_drawbuffer = save_ctx->WinSysDrawBuffer;
1040 save_readbuffer = save_ctx->WinSysReadBuffer;
1041 } else {
1042 save_drawbuffer = save_readbuffer = NULL;
1043 }
1044
1045 /*
1046 * We need to bind the context we're deleting so that
1047 * _mesa_reference_texobj_() uses this context when deleting textures.
1048 * Similarly for framebuffer objects, etc.
1049 */
1050 _mesa_make_current(ctx, NULL, NULL);
1051
1052 /* This must be called first so that glthread has a chance to finish */
1053 _mesa_glthread_destroy(ctx);
1054
1055 _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
1056
1057 /* For the fallback textures, free any sampler views belonging to this
1058 * context.
1059 */
1060 for (unsigned i = 0; i < NUM_TEXTURE_TARGETS; i++) {
1061 struct st_texture_object *stObj =
1062 st_texture_object(ctx->Shared->FallbackTex[i]);
1063 if (stObj) {
1064 st_texture_release_context_sampler_view(st, stObj);
1065 }
1066 }
1067
1068 st_context_free_zombie_objects(st);
1069
1070 simple_mtx_destroy(&st->zombie_sampler_views.mutex);
1071 simple_mtx_destroy(&st->zombie_shaders.mutex);
1072
1073 st_release_program(st, &st->fp);
1074 st_release_program(st, &st->gp);
1075 st_release_program(st, &st->vp);
1076 st_release_program(st, &st->tcp);
1077 st_release_program(st, &st->tep);
1078 st_release_program(st, &st->cp);
1079
1080 /* release framebuffer in the winsys buffers list */
1081 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
1082 st_framebuffer_reference(&stfb, NULL);
1083 }
1084
1085 _mesa_HashWalk(ctx->Shared->FrameBuffers, destroy_framebuffer_attachment_sampler_cb, st);
1086
1087 pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
1088 pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
1089
1090 _vbo_DestroyContext(ctx);
1091
1092 st_destroy_program_variants(st);
1093
1094 _mesa_free_context_data(ctx);
1095
1096 /* This will free the st_context too, so 'st' must not be accessed
1097 * afterwards. */
1098 st_destroy_context_priv(st, true);
1099 st = NULL;
1100
1101 free(ctx);
1102
1103 if (save_ctx == ctx) {
1104 /* unbind the context we just deleted */
1105 _mesa_make_current(NULL, NULL, NULL);
1106 } else {
1107 /* Restore the current context and draw/read buffers (may be NULL) */
1108 _mesa_make_current(save_ctx, save_drawbuffer, save_readbuffer);
1109 }
1110 }