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