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