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