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