st/mesa: simplify st->ctx, ctx->st usage in a various places
[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/samplerobj.h"
33 #include "main/shaderobj.h"
34 #include "main/version.h"
35 #include "main/vtxfmt.h"
36 #include "main/hash.h"
37 #include "program/prog_cache.h"
38 #include "vbo/vbo.h"
39 #include "glapi/glapi.h"
40 #include "st_context.h"
41 #include "st_debug.h"
42 #include "st_cb_bitmap.h"
43 #include "st_cb_blit.h"
44 #include "st_cb_bufferobjects.h"
45 #include "st_cb_clear.h"
46 #include "st_cb_compute.h"
47 #include "st_cb_condrender.h"
48 #include "st_cb_copyimage.h"
49 #include "st_cb_drawpixels.h"
50 #include "st_cb_rasterpos.h"
51 #include "st_cb_drawtex.h"
52 #include "st_cb_eglimage.h"
53 #include "st_cb_fbo.h"
54 #include "st_cb_feedback.h"
55 #include "st_cb_msaa.h"
56 #include "st_cb_perfmon.h"
57 #include "st_cb_program.h"
58 #include "st_cb_queryobj.h"
59 #include "st_cb_readpixels.h"
60 #include "st_cb_texture.h"
61 #include "st_cb_xformfb.h"
62 #include "st_cb_flush.h"
63 #include "st_cb_syncobj.h"
64 #include "st_cb_strings.h"
65 #include "st_cb_texturebarrier.h"
66 #include "st_cb_viewport.h"
67 #include "st_atom.h"
68 #include "st_draw.h"
69 #include "st_extensions.h"
70 #include "st_gen_mipmap.h"
71 #include "st_program.h"
72 #include "st_vdpau.h"
73 #include "st_texture.h"
74 #include "pipe/p_context.h"
75 #include "util/u_inlines.h"
76 #include "util/u_upload_mgr.h"
77 #include "cso_cache/cso_context.h"
78
79
80 DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
81
82
83 /**
84 * Called via ctx->Driver.Enable()
85 */
86 static void st_Enable(struct gl_context * ctx, GLenum cap, GLboolean state)
87 {
88 struct st_context *st = st_context(ctx);
89
90 switch (cap) {
91 case GL_DEBUG_OUTPUT:
92 st_enable_debug_output(st, state);
93 break;
94 default:
95 break;
96 }
97 }
98
99
100 /**
101 * Called via ctx->Driver.QueryMemoryInfo()
102 */
103 static void
104 st_query_memory_info(struct gl_context *ctx, struct gl_memory_info *out)
105 {
106 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
107 struct pipe_memory_info info;
108
109 assert(screen->query_memory_info);
110 if (!screen->query_memory_info)
111 return;
112
113 screen->query_memory_info(screen, &info);
114
115 out->total_device_memory = info.total_device_memory;
116 out->avail_device_memory = info.avail_device_memory;
117 out->total_staging_memory = info.total_staging_memory;
118 out->avail_staging_memory = info.avail_staging_memory;
119 out->device_memory_evicted = info.device_memory_evicted;
120 out->nr_device_memory_evictions = info.nr_device_memory_evictions;
121 }
122
123
124 /**
125 * Called via ctx->Driver.UpdateState()
126 */
127 void st_invalidate_state(struct gl_context * ctx, GLbitfield new_state)
128 {
129 struct st_context *st = st_context(ctx);
130
131 /* Replace _NEW_FRAG_CLAMP with ST_NEW_FRAGMENT_PROGRAM for the fallback. */
132 if (st->clamp_frag_color_in_shader && (new_state & _NEW_FRAG_CLAMP)) {
133 new_state &= ~_NEW_FRAG_CLAMP;
134 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
135 }
136
137 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
138 if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT)) {
139 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
140 }
141
142 /* Invalidate render and compute pipelines. */
143 st->dirty.mesa |= new_state;
144 st->dirty.st |= ST_NEW_MESA;
145 st->dirty_cp.mesa |= new_state;
146 st->dirty_cp.st |= ST_NEW_MESA;
147
148 /* This is the only core Mesa module we depend upon.
149 * No longer use swrast, swsetup, tnl.
150 */
151 _vbo_InvalidateState(ctx, new_state);
152 }
153
154
155 static void
156 st_destroy_context_priv(struct st_context *st)
157 {
158 uint shader, i;
159
160 st_destroy_atoms( st );
161 st_destroy_draw( st );
162 st_destroy_clear(st);
163 st_destroy_bitmap(st);
164 st_destroy_drawpix(st);
165 st_destroy_drawtex(st);
166 st_destroy_perfmon(st);
167 st_destroy_pbo_upload(st);
168
169 for (shader = 0; shader < ARRAY_SIZE(st->state.sampler_views); shader++) {
170 for (i = 0; i < ARRAY_SIZE(st->state.sampler_views[0]); i++) {
171 pipe_sampler_view_release(st->pipe,
172 &st->state.sampler_views[shader][i]);
173 }
174 }
175
176 if (st->default_texture) {
177 st->ctx->Driver.DeleteTexture(st->ctx, st->default_texture);
178 st->default_texture = NULL;
179 }
180
181 u_upload_destroy(st->uploader);
182 if (st->indexbuf_uploader) {
183 u_upload_destroy(st->indexbuf_uploader);
184 }
185 if (st->constbuf_uploader) {
186 u_upload_destroy(st->constbuf_uploader);
187 }
188
189 cso_destroy_context(st->cso_context);
190 free( st );
191 }
192
193
194 static struct st_context *
195 st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe,
196 const struct st_config_options *options)
197 {
198 struct pipe_screen *screen = pipe->screen;
199 uint i;
200 struct st_context *st = ST_CALLOC_STRUCT( st_context );
201
202 st->options = *options;
203
204 ctx->st = st;
205
206 st->ctx = ctx;
207 st->pipe = pipe;
208
209 /* XXX: this is one-off, per-screen init: */
210 st_debug_init();
211
212 /* state tracker needs the VBO module */
213 _vbo_CreateContext(ctx);
214
215 /* Initialize render and compute pipelines flags */
216 st->dirty.mesa = ~0;
217 st->dirty.st = ~0;
218 st->dirty_cp.mesa = ~0;
219 st->dirty_cp.st = ~0;
220
221 /* Create upload manager for vertex data for glBitmap, glDrawPixels,
222 * glClear, etc.
223 */
224 st->uploader = u_upload_create(st->pipe, 65536, PIPE_BIND_VERTEX_BUFFER,
225 PIPE_USAGE_STREAM);
226
227 if (!screen->get_param(screen, PIPE_CAP_USER_INDEX_BUFFERS)) {
228 st->indexbuf_uploader = u_upload_create(st->pipe, 128 * 1024,
229 PIPE_BIND_INDEX_BUFFER,
230 PIPE_USAGE_STREAM);
231 }
232
233 if (!screen->get_param(screen, PIPE_CAP_USER_CONSTANT_BUFFERS))
234 st->constbuf_uploader = u_upload_create(pipe, 128 * 1024,
235 PIPE_BIND_CONSTANT_BUFFER,
236 PIPE_USAGE_STREAM);
237
238 st->cso_context = cso_create_context(pipe);
239
240 st_init_atoms( st );
241 st_init_clear(st);
242 st_init_draw( st );
243 st_init_pbo_upload(st);
244
245 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
246 if (pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
247 st->internal_target = PIPE_TEXTURE_2D;
248 else
249 st->internal_target = PIPE_TEXTURE_RECT;
250
251 /* Setup vertex element info for 'struct st_util_vertex'.
252 */
253 {
254 const unsigned slot = cso_get_aux_vertex_buffer_slot(st->cso_context);
255
256 /* If this assertion ever fails all state tracker calls to
257 * cso_get_aux_vertex_buffer_slot() should be audited. This
258 * particular call would have to be moved to just before each
259 * drawing call.
260 */
261 assert(slot == 0);
262
263 STATIC_ASSERT(sizeof(struct st_util_vertex) == 9 * sizeof(float));
264
265 memset(&st->util_velems, 0, sizeof(st->util_velems));
266 st->util_velems[0].src_offset = 0;
267 st->util_velems[0].vertex_buffer_index = slot;
268 st->util_velems[0].src_format = PIPE_FORMAT_R32G32B32_FLOAT;
269 st->util_velems[1].src_offset = 3 * sizeof(float);
270 st->util_velems[1].vertex_buffer_index = slot;
271 st->util_velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
272 st->util_velems[2].src_offset = 7 * sizeof(float);
273 st->util_velems[2].vertex_buffer_index = slot;
274 st->util_velems[2].src_format = PIPE_FORMAT_R32G32_FLOAT;
275 }
276
277 /* we want all vertex data to be placed in buffer objects */
278 vbo_use_buffer_objects(ctx);
279
280
281 /* make sure that no VBOs are left mapped when we're drawing. */
282 vbo_always_unmap_buffers(ctx);
283
284 /* Need these flags:
285 */
286 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
287
288 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
289
290 st->has_stencil_export =
291 screen->get_param(screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
292 st->has_shader_model3 = screen->get_param(screen, PIPE_CAP_SM3);
293 st->has_etc1 = screen->is_format_supported(screen, PIPE_FORMAT_ETC1_RGB8,
294 PIPE_TEXTURE_2D, 0,
295 PIPE_BIND_SAMPLER_VIEW);
296 st->has_etc2 = screen->is_format_supported(screen, PIPE_FORMAT_ETC2_RGB8,
297 PIPE_TEXTURE_2D, 0,
298 PIPE_BIND_SAMPLER_VIEW);
299 st->prefer_blit_based_texture_transfer = screen->get_param(screen,
300 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER);
301 st->force_persample_in_shader =
302 screen->get_param(screen, PIPE_CAP_SAMPLE_SHADING) &&
303 !screen->get_param(screen, PIPE_CAP_FORCE_PERSAMPLE_INTERP);
304 st->has_shareable_shaders = screen->get_param(screen,
305 PIPE_CAP_SHAREABLE_SHADERS);
306 st->needs_texcoord_semantic =
307 screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
308 st->apply_texture_swizzle_to_border_color =
309 !!(screen->get_param(screen, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK) &
310 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50 |
311 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600));
312 st->has_time_elapsed =
313 screen->get_param(screen, PIPE_CAP_QUERY_TIME_ELAPSED);
314 st->has_half_float_packing =
315 screen->get_param(screen, PIPE_CAP_TGSI_PACK_HALF_FLOAT);
316 st->has_multi_draw_indirect =
317 screen->get_param(screen, PIPE_CAP_MULTI_DRAW_INDIRECT);
318
319 /* GL limits and extensions */
320 st_init_limits(st->pipe->screen, &ctx->Const, &ctx->Extensions);
321 st_init_extensions(st->pipe->screen, &ctx->Const,
322 &ctx->Extensions, &st->options, ctx->Mesa_DXTn);
323
324 if (st_have_perfmon(st)) {
325 ctx->Extensions.AMD_performance_monitor = GL_TRUE;
326 }
327
328 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
329 if (screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_UNCLAMPED)) {
330 if (!screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_CLAMPED)) {
331 st->clamp_vert_color_in_shader = GL_TRUE;
332 }
333
334 if (!screen->get_param(screen, PIPE_CAP_FRAGMENT_COLOR_CLAMPED)) {
335 st->clamp_frag_color_in_shader = GL_TRUE;
336 }
337
338 /* For drivers which cannot do color clamping, it's better to just
339 * disable ARB_color_buffer_float in the core profile, because
340 * the clamping is deprecated there anyway. */
341 if (ctx->API == API_OPENGL_CORE &&
342 (st->clamp_frag_color_in_shader || st->clamp_vert_color_in_shader)) {
343 st->clamp_vert_color_in_shader = GL_FALSE;
344 st->clamp_frag_color_in_shader = GL_FALSE;
345 ctx->Extensions.ARB_color_buffer_float = GL_FALSE;
346 }
347 }
348
349 /* called after _mesa_create_context/_mesa_init_point, fix default user
350 * settable max point size up
351 */
352 ctx->Point.MaxSize = MAX2(ctx->Const.MaxPointSize,
353 ctx->Const.MaxPointSizeAA);
354 /* For vertex shaders, make sure not to emit saturate when SM 3.0 is not supported */
355 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitNoSat = !st->has_shader_model3;
356
357 if (!ctx->Extensions.ARB_gpu_shader5) {
358 for (i = 0; i < MESA_SHADER_STAGES; i++)
359 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler = true;
360 }
361
362 /* Set which shader types can be compiled at link time. */
363 st->shader_has_one_variant[MESA_SHADER_VERTEX] =
364 st->has_shareable_shaders &&
365 !st->clamp_vert_color_in_shader;
366
367 st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
368 st->has_shareable_shaders &&
369 !st->clamp_frag_color_in_shader &&
370 !st->force_persample_in_shader;
371
372 st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
373 st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] = st->has_shareable_shaders;
374 st->shader_has_one_variant[MESA_SHADER_GEOMETRY] = st->has_shareable_shaders;
375
376 _mesa_compute_version(ctx);
377
378 if (ctx->Version == 0) {
379 /* This can happen when a core profile was requested, but the driver
380 * does not support some features of GL 3.1 or later.
381 */
382 st_destroy_context_priv(st);
383 return NULL;
384 }
385
386 _mesa_initialize_dispatch_tables(ctx);
387 _mesa_initialize_vbo_vtxfmt(ctx);
388
389 return st;
390 }
391
392 static void st_init_driver_flags(struct gl_driver_flags *f)
393 {
394 f->NewArray = ST_NEW_VERTEX_ARRAYS;
395 f->NewRasterizerDiscard = ST_NEW_RASTERIZER;
396 f->NewUniformBuffer = ST_NEW_UNIFORM_BUFFER;
397 f->NewDefaultTessLevels = ST_NEW_TESS_STATE;
398 f->NewTextureBuffer = ST_NEW_SAMPLER_VIEWS;
399 f->NewAtomicBuffer = ST_NEW_ATOMIC_BUFFER;
400 f->NewShaderStorageBuffer = ST_NEW_STORAGE_BUFFER;
401 f->NewImageUnits = ST_NEW_IMAGE_UNITS;
402 }
403
404 struct st_context *st_create_context(gl_api api, struct pipe_context *pipe,
405 const struct gl_config *visual,
406 struct st_context *share,
407 const struct st_config_options *options)
408 {
409 struct gl_context *ctx;
410 struct gl_context *shareCtx = share ? share->ctx : NULL;
411 struct dd_function_table funcs;
412 struct st_context *st;
413
414 memset(&funcs, 0, sizeof(funcs));
415 st_init_driver_functions(pipe->screen, &funcs);
416
417 ctx = _mesa_create_context(api, visual, shareCtx, &funcs);
418 if (!ctx) {
419 return NULL;
420 }
421
422 st_init_driver_flags(&ctx->DriverFlags);
423
424 /* XXX: need a capability bit in gallium to query if the pipe
425 * driver prefers DP4 or MUL/MAD for vertex transformation.
426 */
427 if (debug_get_option_mesa_mvp_dp4())
428 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = GL_TRUE;
429
430 st = st_create_context_priv(ctx, pipe, options);
431 if (!st) {
432 _mesa_destroy_context(ctx);
433 }
434
435 return st;
436 }
437
438
439 /**
440 * Callback to release the sampler view attached to a texture object.
441 * Called by _mesa_HashWalk().
442 */
443 static void
444 destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
445 {
446 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
447 struct st_context *st = (struct st_context *) userData;
448
449 st_texture_release_sampler_view(st, st_texture_object(texObj));
450 }
451
452 void st_destroy_context( struct st_context *st )
453 {
454 struct pipe_context *pipe = st->pipe;
455 struct gl_context *ctx = st->ctx;
456 GLuint i;
457
458 _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
459
460 st_reference_fragprog(st, &st->fp, NULL);
461 st_reference_geomprog(st, &st->gp, NULL);
462 st_reference_vertprog(st, &st->vp, NULL);
463 st_reference_tesscprog(st, &st->tcp, NULL);
464 st_reference_tesseprog(st, &st->tep, NULL);
465 st_reference_compprog(st, &st->cp, NULL);
466
467 /* release framebuffer surfaces */
468 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
469 pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
470 }
471 pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
472 pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
473 pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
474
475 _vbo_DestroyContext(ctx);
476
477 st_destroy_program_variants(st);
478
479 _mesa_free_context_data(ctx);
480
481 /* This will free the st_context too, so 'st' must not be accessed
482 * afterwards. */
483 st_destroy_context_priv(st);
484 st = NULL;
485
486 pipe->destroy( pipe );
487
488 free(ctx);
489 }
490
491 static void
492 st_emit_string_marker(struct gl_context *ctx, const GLchar *string, GLsizei len)
493 {
494 struct st_context *st = ctx->st;
495 st->pipe->emit_string_marker(st->pipe, string, len);
496 }
497
498 void st_init_driver_functions(struct pipe_screen *screen,
499 struct dd_function_table *functions)
500 {
501 _mesa_init_shader_object_functions(functions);
502 _mesa_init_sampler_object_functions(functions);
503
504 st_init_blit_functions(functions);
505 st_init_bufferobject_functions(screen, functions);
506 st_init_clear_functions(functions);
507 st_init_bitmap_functions(functions);
508 st_init_copy_image_functions(functions);
509 st_init_drawpixels_functions(functions);
510 st_init_rasterpos_functions(functions);
511
512 st_init_drawtex_functions(functions);
513
514 st_init_eglimage_functions(functions);
515
516 st_init_fbo_functions(functions);
517 st_init_feedback_functions(functions);
518 st_init_msaa_functions(functions);
519 st_init_perfmon_functions(functions);
520 st_init_program_functions(functions);
521 st_init_query_functions(functions);
522 st_init_cond_render_functions(functions);
523 st_init_readpixels_functions(functions);
524 st_init_texture_functions(functions);
525 st_init_texture_barrier_functions(functions);
526 st_init_flush_functions(screen, functions);
527 st_init_string_functions(functions);
528 st_init_viewport_functions(functions);
529 st_init_compute_functions(functions);
530
531 st_init_xformfb_functions(functions);
532 st_init_syncobj_functions(functions);
533
534 st_init_vdpau_functions(functions);
535
536 if (screen->get_param(screen, PIPE_CAP_STRING_MARKER))
537 functions->EmitStringMarker = st_emit_string_marker;
538
539 functions->Enable = st_Enable;
540 functions->UpdateState = st_invalidate_state;
541 functions->QueryMemoryInfo = st_query_memory_info;
542 }