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