st/mesa: implement a simple cache for glDrawPixels
[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 /* free glDrawPixels cache data */
190 free(st->drawpix_cache.image);
191 pipe_resource_reference(&st->drawpix_cache.texture, NULL);
192
193 cso_destroy_context(st->cso_context);
194 free( st );
195 }
196
197
198 static struct st_context *
199 st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe,
200 const struct st_config_options *options)
201 {
202 struct pipe_screen *screen = pipe->screen;
203 uint i;
204 struct st_context *st = ST_CALLOC_STRUCT( st_context );
205
206 st->options = *options;
207
208 ctx->st = st;
209
210 st->ctx = ctx;
211 st->pipe = pipe;
212
213 /* XXX: this is one-off, per-screen init: */
214 st_debug_init();
215
216 /* state tracker needs the VBO module */
217 _vbo_CreateContext(ctx);
218
219 /* Initialize render and compute pipelines flags */
220 st->dirty.mesa = ~0;
221 st->dirty.st = ~0;
222 st->dirty_cp.mesa = ~0;
223 st->dirty_cp.st = ~0;
224
225 /* Create upload manager for vertex data for glBitmap, glDrawPixels,
226 * glClear, etc.
227 */
228 st->uploader = u_upload_create(st->pipe, 65536, PIPE_BIND_VERTEX_BUFFER,
229 PIPE_USAGE_STREAM);
230
231 if (!screen->get_param(screen, PIPE_CAP_USER_INDEX_BUFFERS)) {
232 st->indexbuf_uploader = u_upload_create(st->pipe, 128 * 1024,
233 PIPE_BIND_INDEX_BUFFER,
234 PIPE_USAGE_STREAM);
235 }
236
237 if (!screen->get_param(screen, PIPE_CAP_USER_CONSTANT_BUFFERS))
238 st->constbuf_uploader = u_upload_create(pipe, 128 * 1024,
239 PIPE_BIND_CONSTANT_BUFFER,
240 PIPE_USAGE_STREAM);
241
242 st->cso_context = cso_create_context(pipe);
243
244 st_init_atoms( st );
245 st_init_clear(st);
246 st_init_draw( st );
247 st_init_pbo_upload(st);
248
249 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
250 if (pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
251 st->internal_target = PIPE_TEXTURE_2D;
252 else
253 st->internal_target = PIPE_TEXTURE_RECT;
254
255 /* Setup vertex element info for 'struct st_util_vertex'.
256 */
257 {
258 const unsigned slot = cso_get_aux_vertex_buffer_slot(st->cso_context);
259
260 /* If this assertion ever fails all state tracker calls to
261 * cso_get_aux_vertex_buffer_slot() should be audited. This
262 * particular call would have to be moved to just before each
263 * drawing call.
264 */
265 assert(slot == 0);
266
267 STATIC_ASSERT(sizeof(struct st_util_vertex) == 9 * sizeof(float));
268
269 memset(&st->util_velems, 0, sizeof(st->util_velems));
270 st->util_velems[0].src_offset = 0;
271 st->util_velems[0].vertex_buffer_index = slot;
272 st->util_velems[0].src_format = PIPE_FORMAT_R32G32B32_FLOAT;
273 st->util_velems[1].src_offset = 3 * sizeof(float);
274 st->util_velems[1].vertex_buffer_index = slot;
275 st->util_velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
276 st->util_velems[2].src_offset = 7 * sizeof(float);
277 st->util_velems[2].vertex_buffer_index = slot;
278 st->util_velems[2].src_format = PIPE_FORMAT_R32G32_FLOAT;
279 }
280
281 /* we want all vertex data to be placed in buffer objects */
282 vbo_use_buffer_objects(ctx);
283
284
285 /* make sure that no VBOs are left mapped when we're drawing. */
286 vbo_always_unmap_buffers(ctx);
287
288 /* Need these flags:
289 */
290 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
291
292 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
293
294 st->has_stencil_export =
295 screen->get_param(screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
296 st->has_shader_model3 = screen->get_param(screen, PIPE_CAP_SM3);
297 st->has_etc1 = screen->is_format_supported(screen, PIPE_FORMAT_ETC1_RGB8,
298 PIPE_TEXTURE_2D, 0,
299 PIPE_BIND_SAMPLER_VIEW);
300 st->has_etc2 = screen->is_format_supported(screen, PIPE_FORMAT_ETC2_RGB8,
301 PIPE_TEXTURE_2D, 0,
302 PIPE_BIND_SAMPLER_VIEW);
303 st->prefer_blit_based_texture_transfer = screen->get_param(screen,
304 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER);
305 st->force_persample_in_shader =
306 screen->get_param(screen, PIPE_CAP_SAMPLE_SHADING) &&
307 !screen->get_param(screen, PIPE_CAP_FORCE_PERSAMPLE_INTERP);
308 st->has_shareable_shaders = screen->get_param(screen,
309 PIPE_CAP_SHAREABLE_SHADERS);
310 st->needs_texcoord_semantic =
311 screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
312 st->apply_texture_swizzle_to_border_color =
313 !!(screen->get_param(screen, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK) &
314 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50 |
315 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600));
316 st->has_time_elapsed =
317 screen->get_param(screen, PIPE_CAP_QUERY_TIME_ELAPSED);
318 st->has_half_float_packing =
319 screen->get_param(screen, PIPE_CAP_TGSI_PACK_HALF_FLOAT);
320 st->has_multi_draw_indirect =
321 screen->get_param(screen, PIPE_CAP_MULTI_DRAW_INDIRECT);
322
323 /* GL limits and extensions */
324 st_init_limits(st->pipe->screen, &ctx->Const, &ctx->Extensions);
325 st_init_extensions(st->pipe->screen, &ctx->Const,
326 &ctx->Extensions, &st->options, ctx->Mesa_DXTn);
327
328 if (st_have_perfmon(st)) {
329 ctx->Extensions.AMD_performance_monitor = GL_TRUE;
330 }
331
332 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
333 if (screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_UNCLAMPED)) {
334 if (!screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_CLAMPED)) {
335 st->clamp_vert_color_in_shader = GL_TRUE;
336 }
337
338 if (!screen->get_param(screen, PIPE_CAP_FRAGMENT_COLOR_CLAMPED)) {
339 st->clamp_frag_color_in_shader = GL_TRUE;
340 }
341
342 /* For drivers which cannot do color clamping, it's better to just
343 * disable ARB_color_buffer_float in the core profile, because
344 * the clamping is deprecated there anyway. */
345 if (ctx->API == API_OPENGL_CORE &&
346 (st->clamp_frag_color_in_shader || st->clamp_vert_color_in_shader)) {
347 st->clamp_vert_color_in_shader = GL_FALSE;
348 st->clamp_frag_color_in_shader = GL_FALSE;
349 ctx->Extensions.ARB_color_buffer_float = GL_FALSE;
350 }
351 }
352
353 /* called after _mesa_create_context/_mesa_init_point, fix default user
354 * settable max point size up
355 */
356 ctx->Point.MaxSize = MAX2(ctx->Const.MaxPointSize,
357 ctx->Const.MaxPointSizeAA);
358 /* For vertex shaders, make sure not to emit saturate when SM 3.0 is not supported */
359 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitNoSat = !st->has_shader_model3;
360
361 if (!ctx->Extensions.ARB_gpu_shader5) {
362 for (i = 0; i < MESA_SHADER_STAGES; i++)
363 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler = true;
364 }
365
366 /* Set which shader types can be compiled at link time. */
367 st->shader_has_one_variant[MESA_SHADER_VERTEX] =
368 st->has_shareable_shaders &&
369 !st->clamp_vert_color_in_shader;
370
371 st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
372 st->has_shareable_shaders &&
373 !st->clamp_frag_color_in_shader &&
374 !st->force_persample_in_shader;
375
376 st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
377 st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] = st->has_shareable_shaders;
378 st->shader_has_one_variant[MESA_SHADER_GEOMETRY] = st->has_shareable_shaders;
379
380 _mesa_compute_version(ctx);
381
382 if (ctx->Version == 0) {
383 /* This can happen when a core profile was requested, but the driver
384 * does not support some features of GL 3.1 or later.
385 */
386 st_destroy_context_priv(st);
387 return NULL;
388 }
389
390 _mesa_initialize_dispatch_tables(ctx);
391 _mesa_initialize_vbo_vtxfmt(ctx);
392
393 return st;
394 }
395
396 static void st_init_driver_flags(struct gl_driver_flags *f)
397 {
398 f->NewArray = ST_NEW_VERTEX_ARRAYS;
399 f->NewRasterizerDiscard = ST_NEW_RASTERIZER;
400 f->NewUniformBuffer = ST_NEW_UNIFORM_BUFFER;
401 f->NewDefaultTessLevels = ST_NEW_TESS_STATE;
402 f->NewTextureBuffer = ST_NEW_SAMPLER_VIEWS;
403 f->NewAtomicBuffer = ST_NEW_ATOMIC_BUFFER;
404 f->NewShaderStorageBuffer = ST_NEW_STORAGE_BUFFER;
405 f->NewImageUnits = ST_NEW_IMAGE_UNITS;
406 }
407
408 struct st_context *st_create_context(gl_api api, struct pipe_context *pipe,
409 const struct gl_config *visual,
410 struct st_context *share,
411 const struct st_config_options *options)
412 {
413 struct gl_context *ctx;
414 struct gl_context *shareCtx = share ? share->ctx : NULL;
415 struct dd_function_table funcs;
416 struct st_context *st;
417
418 memset(&funcs, 0, sizeof(funcs));
419 st_init_driver_functions(pipe->screen, &funcs);
420
421 ctx = _mesa_create_context(api, visual, shareCtx, &funcs);
422 if (!ctx) {
423 return NULL;
424 }
425
426 st_init_driver_flags(&ctx->DriverFlags);
427
428 /* XXX: need a capability bit in gallium to query if the pipe
429 * driver prefers DP4 or MUL/MAD for vertex transformation.
430 */
431 if (debug_get_option_mesa_mvp_dp4())
432 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = GL_TRUE;
433
434 st = st_create_context_priv(ctx, pipe, options);
435 if (!st) {
436 _mesa_destroy_context(ctx);
437 }
438
439 return st;
440 }
441
442
443 /**
444 * Callback to release the sampler view attached to a texture object.
445 * Called by _mesa_HashWalk().
446 */
447 static void
448 destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
449 {
450 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
451 struct st_context *st = (struct st_context *) userData;
452
453 st_texture_release_sampler_view(st, st_texture_object(texObj));
454 }
455
456 void st_destroy_context( struct st_context *st )
457 {
458 struct pipe_context *pipe = st->pipe;
459 struct gl_context *ctx = st->ctx;
460 GLuint i;
461
462 _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
463
464 st_reference_fragprog(st, &st->fp, NULL);
465 st_reference_geomprog(st, &st->gp, NULL);
466 st_reference_vertprog(st, &st->vp, NULL);
467 st_reference_tesscprog(st, &st->tcp, NULL);
468 st_reference_tesseprog(st, &st->tep, NULL);
469 st_reference_compprog(st, &st->cp, NULL);
470
471 /* release framebuffer surfaces */
472 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
473 pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
474 }
475 pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
476 pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
477 pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
478
479 _vbo_DestroyContext(ctx);
480
481 st_destroy_program_variants(st);
482
483 _mesa_free_context_data(ctx);
484
485 /* This will free the st_context too, so 'st' must not be accessed
486 * afterwards. */
487 st_destroy_context_priv(st);
488 st = NULL;
489
490 pipe->destroy( pipe );
491
492 free(ctx);
493 }
494
495 static void
496 st_emit_string_marker(struct gl_context *ctx, const GLchar *string, GLsizei len)
497 {
498 struct st_context *st = ctx->st;
499 st->pipe->emit_string_marker(st->pipe, string, len);
500 }
501
502 void st_init_driver_functions(struct pipe_screen *screen,
503 struct dd_function_table *functions)
504 {
505 _mesa_init_shader_object_functions(functions);
506 _mesa_init_sampler_object_functions(functions);
507
508 st_init_blit_functions(functions);
509 st_init_bufferobject_functions(screen, functions);
510 st_init_clear_functions(functions);
511 st_init_bitmap_functions(functions);
512 st_init_copy_image_functions(functions);
513 st_init_drawpixels_functions(functions);
514 st_init_rasterpos_functions(functions);
515
516 st_init_drawtex_functions(functions);
517
518 st_init_eglimage_functions(functions);
519
520 st_init_fbo_functions(functions);
521 st_init_feedback_functions(functions);
522 st_init_msaa_functions(functions);
523 st_init_perfmon_functions(functions);
524 st_init_program_functions(functions);
525 st_init_query_functions(functions);
526 st_init_cond_render_functions(functions);
527 st_init_readpixels_functions(functions);
528 st_init_texture_functions(functions);
529 st_init_texture_barrier_functions(functions);
530 st_init_flush_functions(screen, functions);
531 st_init_string_functions(functions);
532 st_init_viewport_functions(functions);
533 st_init_compute_functions(functions);
534
535 st_init_xformfb_functions(functions);
536 st_init_syncobj_functions(functions);
537
538 st_init_vdpau_functions(functions);
539
540 if (screen->get_param(screen, PIPE_CAP_STRING_MARKER))
541 functions->EmitStringMarker = st_emit_string_marker;
542
543 functions->Enable = st_Enable;
544 functions->UpdateState = st_invalidate_state;
545 functions->QueryMemoryInfo = st_query_memory_info;
546 }