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