blob: e4dd6fa897ceb332027a8f98ac7a6932fb0a4be1 [file] [log] [blame]
Keith Whitwell8e4a95a2007-05-24 10:41:34 +01001/**************************************************************************
2 *
José Fonseca87712852014-01-17 16:27:50 +00003 * Copyright 2007 VMware, Inc.
Keith Whitwell8e4a95a2007-05-24 10:41:34 +01004 * 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.
José Fonseca87712852014-01-17 16:27:50 +000021 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010022 * 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
Brian1e6d1ab2007-08-17 15:27:18 +010028#include "main/imports.h"
Brian Paul122c6762011-12-03 10:04:18 -070029#include "main/accum.h"
Jordan Justen4bea4cb2012-11-16 10:30:19 -080030#include "main/api_exec.h"
Brian80d26582007-11-01 17:46:04 -060031#include "main/context.h"
Brian Paul34a5d3b2011-04-10 12:48:28 -060032#include "main/samplerobj.h"
Brian Paula37b2212010-06-10 20:23:03 -060033#include "main/shaderobj.h"
Jordan Justen4bea4cb2012-11-16 10:30:19 -080034#include "main/version.h"
35#include "main/vtxfmt.h"
Christian Königd117ddb2014-03-23 17:01:38 +010036#include "main/hash.h"
Vinson Lee9c98e9e2010-08-04 21:17:13 -070037#include "program/prog_cache.h"
Brian1e6d1ab2007-08-17 15:27:18 +010038#include "vbo/vbo.h"
Brian Paul5ebd8492008-06-17 14:27:36 -060039#include "glapi/glapi.h"
Chia-I Wude8a8792010-02-07 19:20:52 +080040#include "st_context.h"
Keith Whitwellb02ef742009-10-05 15:50:11 +010041#include "st_debug.h"
Brianf6cd3772008-03-20 14:50:17 -060042#include "st_cb_bitmap.h"
Brian0df877a2008-03-18 17:16:23 -060043#include "st_cb_blit.h"
Brianf6cd3772008-03-20 14:50:17 -060044#include "st_cb_bufferobjects.h"
Brian51b300c2007-08-02 10:29:50 -060045#include "st_cb_clear.h"
Samuel Pitoiset8aa66692016-01-03 15:41:48 +010046#include "st_cb_compute.h"
Brian Paulc9b55832009-12-31 14:47:21 -070047#include "st_cb_condrender.h"
Marek Olšákf04f1362015-08-24 02:55:20 +020048#include "st_cb_copyimage.h"
Brian184b6a12007-08-02 14:21:16 -060049#include "st_cb_drawpixels.h"
Brian Pauld7a4d492008-06-20 08:09:59 -060050#include "st_cb_rasterpos.h"
Brian Paul4424c662008-08-12 18:02:44 -060051#include "st_cb_drawtex.h"
Chia-I Wua1aa53b2010-02-25 23:10:47 +080052#include "st_cb_eglimage.h"
Brian64da7512007-08-09 12:27:44 -060053#include "st_cb_fbo.h"
Brianb4bacd12007-09-17 14:24:11 -060054#include "st_cb_feedback.h"
Dave Airlie1d90ee52013-03-04 06:14:18 +100055#include "st_cb_msaa.h"
Christoph Bumiller4cd1cfb2014-07-04 13:02:06 +020056#include "st_cb_perfmon.h"
Brian Paul22a3d022008-03-24 11:55:29 -060057#include "st_cb_program.h"
Brian24864742007-08-11 19:57:37 +010058#include "st_cb_queryobj.h"
Brian6f27aab2007-08-10 11:37:21 -060059#include "st_cb_readpixels.h"
Brian4435bae2007-08-06 15:49:11 -060060#include "st_cb_texture.h"
Brian Paulfef6e362010-05-10 21:11:21 -060061#include "st_cb_xformfb.h"
Keith Whitwell5c2c0562007-08-10 12:57:14 +010062#include "st_cb_flush.h"
Marek Olšák5257a6d2011-03-05 20:32:28 +010063#include "st_cb_syncobj.h"
Keith Whitwell39407fd2007-08-10 16:42:26 +010064#include "st_cb_strings.h"
Marek Olšákaea4ed42011-03-08 11:32:35 +010065#include "st_cb_texturebarrier.h"
Jakob Bornecrantza93f9f32010-05-21 20:37:23 +010066#include "st_cb_viewport.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010067#include "st_atom.h"
68#include "st_draw.h"
Brianc6499a72007-11-05 18:04:30 -070069#include "st_extensions.h"
Brian62abcb92008-02-08 14:54:18 -070070#include "st_gen_mipmap.h"
Nicolai Hähnle4ecc32b2016-04-25 21:35:10 -050071#include "st_pbo.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010072#include "st_program.h"
Christian König80964222013-05-15 15:10:11 +020073#include "st_vdpau.h"
Christian Königd117ddb2014-03-23 17:01:38 +010074#include "st_texture.h"
Keith Whitwell943964a2007-06-14 18:23:43 +010075#include "pipe/p_context.h"
José Fonseca28486882010-02-02 14:42:17 +000076#include "util/u_inlines.h"
Marek Olšák76d9a992012-04-13 19:27:45 +020077#include "util/u_upload_mgr.h"
Brian339e7ec2008-03-11 18:54:31 -060078#include "cso_cache/cso_context.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010079
Brian6f27aab2007-08-10 11:37:21 -060080
Brian Paul6b97fe52010-08-06 15:09:51 -060081DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
Jakob Bornecrantz99611f02010-08-05 17:13:41 -070082
83
Briand7755092007-10-31 11:08:07 -060084/**
Nicolai Hähnle2123bfc2016-01-04 17:31:05 -050085 * Called via ctx->Driver.Enable()
86 */
87static void st_Enable(struct gl_context * ctx, GLenum cap, GLboolean state)
88{
89 struct st_context *st = st_context(ctx);
90
91 switch (cap) {
92 case GL_DEBUG_OUTPUT:
Nicolai Hähnle084ca0d2016-07-07 09:25:31 +020093 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
94 st_update_debug_callback(st);
Nicolai Hähnle2123bfc2016-01-04 17:31:05 -050095 break;
96 default:
97 break;
98 }
99}
100
101
102/**
Marek Olšák5f51a242016-02-02 01:27:55 +0100103 * Called via ctx->Driver.QueryMemoryInfo()
104 */
105static void
106st_query_memory_info(struct gl_context *ctx, struct gl_memory_info *out)
107{
108 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
109 struct pipe_memory_info info;
110
111 assert(screen->query_memory_info);
112 if (!screen->query_memory_info)
113 return;
114
115 screen->query_memory_info(screen, &info);
116
117 out->total_device_memory = info.total_device_memory;
118 out->avail_device_memory = info.avail_device_memory;
119 out->total_staging_memory = info.total_staging_memory;
120 out->avail_staging_memory = info.avail_staging_memory;
121 out->device_memory_evicted = info.device_memory_evicted;
122 out->nr_device_memory_evictions = info.nr_device_memory_evictions;
123}
124
125
126/**
Briand7755092007-10-31 11:08:07 -0600127 * Called via ctx->Driver.UpdateState()
128 */
Brian Paul4cd1bd42016-01-06 08:33:36 -0700129void st_invalidate_state(struct gl_context * ctx, GLbitfield new_state)
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100130{
131 struct st_context *st = st_context(ctx);
132
Marek Olšákc8fe3b92016-07-17 18:38:38 +0200133 if (new_state & _NEW_BUFFERS) {
134 st->dirty |= ST_NEW_DSA |
135 ST_NEW_FB_STATE |
136 ST_NEW_SAMPLE_MASK |
137 ST_NEW_SAMPLE_SHADING |
138 ST_NEW_FS_STATE |
139 ST_NEW_POLY_STIPPLE |
140 ST_NEW_VIEWPORT |
141 ST_NEW_RASTERIZER |
142 ST_NEW_SCISSOR |
143 ST_NEW_WINDOW_RECTANGLES;
144 } else {
145 /* These set a subset of flags set by _NEW_BUFFERS, so we only have to
146 * check them when _NEW_BUFFERS isn't set.
147 */
148 if (new_state & (_NEW_DEPTH |
149 _NEW_STENCIL))
150 st->dirty |= ST_NEW_DSA;
151
152 if (new_state & _NEW_PROGRAM)
Marek Olšákc7954b12016-07-17 20:59:42 +0200153 st->dirty |= ST_NEW_RASTERIZER;
Marek Olšákc8fe3b92016-07-17 18:38:38 +0200154
155 if (new_state & _NEW_SCISSOR)
156 st->dirty |= ST_NEW_RASTERIZER |
157 ST_NEW_SCISSOR |
158 ST_NEW_WINDOW_RECTANGLES;
159
160 if (new_state & _NEW_FOG)
161 st->dirty |= ST_NEW_FS_STATE;
162
163 if (new_state & _NEW_POLYGONSTIPPLE)
164 st->dirty |= ST_NEW_POLY_STIPPLE;
165
166 if (new_state & _NEW_VIEWPORT)
167 st->dirty |= ST_NEW_VIEWPORT;
168
169 if (new_state & _NEW_FRAG_CLAMP) {
170 if (st->clamp_frag_color_in_shader)
171 st->dirty |= ST_NEW_FS_STATE;
172 else
173 st->dirty |= ST_NEW_RASTERIZER;
174 }
Marek Olšákbc1c8362012-01-23 03:11:17 +0100175 }
176
Marek Olšákc8fe3b92016-07-17 18:38:38 +0200177 if (new_state & _NEW_MULTISAMPLE) {
178 st->dirty |= ST_NEW_BLEND |
179 ST_NEW_SAMPLE_MASK |
180 ST_NEW_SAMPLE_SHADING |
181 ST_NEW_RASTERIZER |
182 ST_NEW_FS_STATE;
183 } else {
184 /* These set a subset of flags set by _NEW_MULTISAMPLE, so we only
185 * have to check them when _NEW_MULTISAMPLE isn't set.
186 */
187 if (new_state & (_NEW_LIGHT |
188 _NEW_LINE |
189 _NEW_POINT |
190 _NEW_POLYGON |
191 _NEW_TRANSFORM))
192 st->dirty |= ST_NEW_RASTERIZER;
193 }
194
195 if (new_state & (_NEW_PROJECTION |
196 _NEW_TRANSFORM))
197 st->dirty |= ST_NEW_CLIP_STATE;
198
199 if (new_state & _NEW_COLOR)
200 st->dirty |= ST_NEW_BLEND |
201 ST_NEW_DSA;
202
203 if (new_state & _NEW_PIXEL)
204 st->dirty |= ST_NEW_PIXEL_TRANSFER;
205
206 if (new_state & _NEW_TEXTURE)
207 st->dirty |= ST_NEW_SAMPLER_VIEWS |
208 ST_NEW_SAMPLERS |
209 ST_NEW_IMAGE_UNITS;
210
211 if (new_state & _NEW_CURRENT_ATTRIB)
212 st->dirty |= ST_NEW_VERTEX_ARRAYS;
213
214 if (new_state & _NEW_PROGRAM_CONSTANTS)
215 st->dirty |= ST_NEW_CONSTANTS;
216
Marek Olšákbc1c8362012-01-23 03:11:17 +0100217 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
Marek Olšákc8fe3b92016-07-17 18:38:38 +0200218 if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT))
219 st->dirty |= ST_NEW_VS_STATE;
Briand7755092007-10-31 11:08:07 -0600220
Marek Olšák79dcd692016-07-17 20:37:58 +0200221 /* Which shaders are dirty will be determined manually. */
222 if (new_state & _NEW_PROGRAM) {
223 st->gfx_shaders_may_be_dirty = true;
224 st->compute_shader_may_be_dirty = true;
225 }
226
Briand7755092007-10-31 11:08:07 -0600227 /* This is the only core Mesa module we depend upon.
228 * No longer use swrast, swsetup, tnl.
229 */
230 _vbo_InvalidateState(ctx, new_state);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100231}
232
Brian Paula46d7572014-09-09 09:56:47 -0600233
234static void
235st_destroy_context_priv(struct st_context *st)
236{
237 uint shader, i;
238
239 st_destroy_atoms( st );
240 st_destroy_draw( st );
241 st_destroy_clear(st);
242 st_destroy_bitmap(st);
243 st_destroy_drawpix(st);
244 st_destroy_drawtex(st);
Christoph Bumiller4cd1cfb2014-07-04 13:02:06 +0200245 st_destroy_perfmon(st);
Nicolai Hähnle4ecc32b2016-04-25 21:35:10 -0500246 st_destroy_pbo_helpers(st);
Brian Paula46d7572014-09-09 09:56:47 -0600247
Brian Paul2f0143c2015-02-28 09:01:33 -0700248 for (shader = 0; shader < ARRAY_SIZE(st->state.sampler_views); shader++) {
249 for (i = 0; i < ARRAY_SIZE(st->state.sampler_views[0]); i++) {
Brian Paula46d7572014-09-09 09:56:47 -0600250 pipe_sampler_view_release(st->pipe,
251 &st->state.sampler_views[shader][i]);
252 }
253 }
254
Brian Paula46d7572014-09-09 09:56:47 -0600255 u_upload_destroy(st->uploader);
256 if (st->indexbuf_uploader) {
257 u_upload_destroy(st->indexbuf_uploader);
258 }
259 if (st->constbuf_uploader) {
260 u_upload_destroy(st->constbuf_uploader);
261 }
Marek Olšák0a60ebe2014-12-07 14:21:41 +0100262
Brian Paul44f48fe2016-02-19 08:51:51 -0700263 /* free glDrawPixels cache data */
264 free(st->drawpix_cache.image);
265 pipe_resource_reference(&st->drawpix_cache.texture, NULL);
266
Nicolai Hähnle615ba112016-06-08 13:22:52 +0200267 /* free glReadPixels cache data */
268 st_invalidate_readpix_cache(st);
269
Marek Olšák0a60ebe2014-12-07 14:21:41 +0100270 cso_destroy_context(st->cso_context);
Brian Paula46d7572014-09-09 09:56:47 -0600271 free( st );
272}
273
274
Brian91564ee2007-11-05 16:15:43 -0700275static struct st_context *
Vadim Girlina6457c02012-04-12 04:30:03 +0400276st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe,
277 const struct st_config_options *options)
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100278{
Marek Olšák8c655f42012-04-24 17:04:18 +0200279 struct pipe_screen *screen = pipe->screen;
Brian339e7ec2008-03-11 18:54:31 -0600280 uint i;
Brian Paulf1a59a62009-02-12 10:14:36 -0700281 struct st_context *st = ST_CALLOC_STRUCT( st_context );
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100282
Vadim Girlina6457c02012-04-12 04:30:03 +0400283 st->options = *options;
284
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100285 ctx->st = st;
286
287 st->ctx = ctx;
Keith Whitwell943964a2007-06-14 18:23:43 +0100288 st->pipe = pipe;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100289
Keith Whitwellb02ef742009-10-05 15:50:11 +0100290 /* XXX: this is one-off, per-screen init: */
291 st_debug_init();
292
Brian8984a282007-10-31 11:35:50 -0600293 /* state tracker needs the VBO module */
294 _vbo_CreateContext(ctx);
Brian8984a282007-10-31 11:35:50 -0600295
Marek Olšákc8fe3b92016-07-17 18:38:38 +0200296 st->dirty = ST_ALL_STATES_MASK;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100297
Brian Paul282bb872013-10-17 09:28:17 -0600298 /* Create upload manager for vertex data for glBitmap, glDrawPixels,
299 * glClear, etc.
300 */
Marek Olšákecb2da12015-12-19 17:54:31 +0100301 st->uploader = u_upload_create(st->pipe, 65536, PIPE_BIND_VERTEX_BUFFER,
302 PIPE_USAGE_STREAM);
Marek Olšák8c655f42012-04-24 17:04:18 +0200303
304 if (!screen->get_param(screen, PIPE_CAP_USER_INDEX_BUFFERS)) {
Marek Olšák37d0aea2015-12-19 17:43:48 +0100305 st->indexbuf_uploader = u_upload_create(st->pipe, 128 * 1024,
Marek Olšákecb2da12015-12-19 17:54:31 +0100306 PIPE_BIND_INDEX_BUFFER,
307 PIPE_USAGE_STREAM);
Marek Olšák8c655f42012-04-24 17:04:18 +0200308 }
309
Marek Olšák37d0aea2015-12-19 17:43:48 +0100310 if (!screen->get_param(screen, PIPE_CAP_USER_CONSTANT_BUFFERS))
311 st->constbuf_uploader = u_upload_create(pipe, 128 * 1024,
Marek Olšákecb2da12015-12-19 17:54:31 +0100312 PIPE_BIND_CONSTANT_BUFFER,
313 PIPE_USAGE_STREAM);
Marek Olšák7a054592012-04-24 20:16:50 +0200314
Brian339e7ec2008-03-11 18:54:31 -0600315 st->cso_context = cso_create_context(pipe);
Zack Rusin97803272007-09-14 04:08:58 -0400316
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100317 st_init_atoms( st );
Brian Paulce5c8672008-04-03 12:54:32 -0600318 st_init_clear(st);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100319 st_init_draw( st );
Nicolai Hähnle4ecc32b2016-04-25 21:35:10 -0500320 st_init_pbo_helpers(st);
Brian61d02152007-08-02 20:40:19 -0600321
Brian Paul282bb872013-10-17 09:28:17 -0600322 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
323 if (pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
Luca Barbieri3070e0e2010-08-06 07:39:21 +0200324 st->internal_target = PIPE_TEXTURE_2D;
325 else
326 st->internal_target = PIPE_TEXTURE_RECT;
327
Brian Paulb63fe052016-02-16 10:22:31 -0700328 /* Setup vertex element info for 'struct st_util_vertex'.
Brian Paul84e5cb32012-08-17 14:33:31 -0600329 */
Brian Paulb63fe052016-02-16 10:22:31 -0700330 {
331 const unsigned slot = cso_get_aux_vertex_buffer_slot(st->cso_context);
332
333 /* If this assertion ever fails all state tracker calls to
334 * cso_get_aux_vertex_buffer_slot() should be audited. This
335 * particular call would have to be moved to just before each
336 * drawing call.
337 */
338 assert(slot == 0);
339
340 STATIC_ASSERT(sizeof(struct st_util_vertex) == 9 * sizeof(float));
341
342 memset(&st->util_velems, 0, sizeof(st->util_velems));
343 st->util_velems[0].src_offset = 0;
344 st->util_velems[0].vertex_buffer_index = slot;
345 st->util_velems[0].src_format = PIPE_FORMAT_R32G32B32_FLOAT;
346 st->util_velems[1].src_offset = 3 * sizeof(float);
347 st->util_velems[1].vertex_buffer_index = slot;
348 st->util_velems[1].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
349 st->util_velems[2].src_offset = 7 * sizeof(float);
350 st->util_velems[2].vertex_buffer_index = slot;
351 st->util_velems[2].src_format = PIPE_FORMAT_R32G32_FLOAT;
Roland Scheideggerae7b7bf2010-03-09 15:09:01 +0100352 }
353
Brian1e6d1ab2007-08-17 15:27:18 +0100354 /* we want all vertex data to be placed in buffer objects */
355 vbo_use_buffer_objects(ctx);
356
Brian Paul2997b452011-10-25 17:41:12 -0600357
358 /* make sure that no VBOs are left mapped when we're drawing. */
359 vbo_always_unmap_buffers(ctx);
360
Brian6da92342007-08-06 20:53:28 +0100361 /* Need these flags:
362 */
Brian Paula0636152016-02-16 10:22:32 -0700363 ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
Brian6da92342007-08-06 20:53:28 +0100364
Brian Paula0636152016-02-16 10:22:32 -0700365 ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
Briane3bdd662007-08-16 17:33:49 -0600366
Marek Olšák5ba15d82012-07-09 05:08:36 +0200367 st->has_stencil_export =
368 screen->get_param(screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
Marek Olšák4362bda2013-01-31 22:30:44 +0100369 st->has_shader_model3 = screen->get_param(screen, PIPE_CAP_SM3);
Marek Olšák0127d262014-08-02 21:38:25 +0200370 st->has_etc1 = screen->is_format_supported(screen, PIPE_FORMAT_ETC1_RGB8,
371 PIPE_TEXTURE_2D, 0,
372 PIPE_BIND_SAMPLER_VIEW);
Ilia Mirkin33edda72015-02-16 02:27:37 -0500373 st->has_etc2 = screen->is_format_supported(screen, PIPE_FORMAT_ETC2_RGB8,
374 PIPE_TEXTURE_2D, 0,
375 PIPE_BIND_SAMPLER_VIEW);
Marek Olšák3e10ab62013-03-14 17:18:43 +0100376 st->prefer_blit_based_texture_transfer = screen->get_param(screen,
377 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER);
Marek Olšákb99645f2015-10-20 00:12:53 +0200378 st->force_persample_in_shader =
379 screen->get_param(screen, PIPE_CAP_SAMPLE_SHADING) &&
380 !screen->get_param(screen, PIPE_CAP_FORCE_PERSAMPLE_INTERP);
Marek Olšákf4e938e2015-09-27 23:36:59 +0200381 st->has_shareable_shaders = screen->get_param(screen,
382 PIPE_CAP_SHAREABLE_SHADERS);
Christoph Bumiller8acaf862013-03-15 22:11:31 +0100383 st->needs_texcoord_semantic =
384 screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
Christoph Bumiller729abfd2013-04-12 13:42:01 +0200385 st->apply_texture_swizzle_to_border_color =
386 !!(screen->get_param(screen, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK) &
387 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50 |
388 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600));
Marek Olšák5c691732014-08-03 04:20:31 +0200389 st->has_time_elapsed =
390 screen->get_param(screen, PIPE_CAP_QUERY_TIME_ELAPSED);
Ilia Mirkin20dee332015-10-29 02:52:57 -0400391 st->has_half_float_packing =
392 screen->get_param(screen, PIPE_CAP_TGSI_PACK_HALF_FLOAT);
Ilia Mirkin2860f202015-12-31 14:11:07 -0500393 st->has_multi_draw_indirect =
394 screen->get_param(screen, PIPE_CAP_MULTI_DRAW_INDIRECT);
Christoph Bumiller8acaf862013-03-15 22:11:31 +0100395
Brianc6499a72007-11-05 18:04:30 -0700396 /* GL limits and extensions */
Marek Olšák35e755f2014-08-03 04:36:19 +0200397 st_init_limits(st->pipe->screen, &ctx->Const, &ctx->Extensions);
Marek Olšák4155d1c2014-09-12 16:41:19 +0200398 st_init_extensions(st->pipe->screen, &ctx->Const,
Marek Olšák5c691732014-08-03 04:20:31 +0200399 &ctx->Extensions, &st->options, ctx->Mesa_DXTn);
400
Nicolai Hähnlebabf6552015-11-25 12:19:03 +0100401 if (st_have_perfmon(st)) {
Christoph Bumiller4cd1cfb2014-07-04 13:02:06 +0200402 ctx->Extensions.AMD_performance_monitor = GL_TRUE;
403 }
404
Marek Olšák5c691732014-08-03 04:20:31 +0200405 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
406 if (screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_UNCLAMPED)) {
407 if (!screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_CLAMPED)) {
408 st->clamp_vert_color_in_shader = GL_TRUE;
409 }
410
411 if (!screen->get_param(screen, PIPE_CAP_FRAGMENT_COLOR_CLAMPED)) {
412 st->clamp_frag_color_in_shader = GL_TRUE;
413 }
414
415 /* For drivers which cannot do color clamping, it's better to just
416 * disable ARB_color_buffer_float in the core profile, because
417 * the clamping is deprecated there anyway. */
418 if (ctx->API == API_OPENGL_CORE &&
419 (st->clamp_frag_color_in_shader || st->clamp_vert_color_in_shader)) {
420 st->clamp_vert_color_in_shader = GL_FALSE;
421 st->clamp_frag_color_in_shader = GL_FALSE;
422 ctx->Extensions.ARB_color_buffer_float = GL_FALSE;
423 }
424 }
Brianaf3d6c82007-08-21 20:15:00 -0600425
Marek Olšák35e755f2014-08-03 04:36:19 +0200426 /* called after _mesa_create_context/_mesa_init_point, fix default user
427 * settable max point size up
428 */
Brian Paula0636152016-02-16 10:22:32 -0700429 ctx->Point.MaxSize = MAX2(ctx->Const.MaxPointSize,
430 ctx->Const.MaxPointSizeAA);
Abdiel Janulgue49e04312014-12-01 14:59:08 +0200431 /* For vertex shaders, make sure not to emit saturate when SM 3.0 is not supported */
432 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitNoSat = !st->has_shader_model3;
Marek Olšák35e755f2014-08-03 04:36:19 +0200433
Tapani Pällif17c8c22015-06-29 09:48:52 +0300434 if (!ctx->Extensions.ARB_gpu_shader5) {
435 for (i = 0; i < MESA_SHADER_STAGES; i++)
436 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler = true;
437 }
438
Marek Olšáke57dd7a2015-09-28 00:04:39 +0200439 /* Set which shader types can be compiled at link time. */
440 st->shader_has_one_variant[MESA_SHADER_VERTEX] =
441 st->has_shareable_shaders &&
442 !st->clamp_vert_color_in_shader;
443
444 st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
445 st->has_shareable_shaders &&
446 !st->clamp_frag_color_in_shader &&
447 !st->force_persample_in_shader;
448
449 st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
450 st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] = st->has_shareable_shaders;
451 st->shader_has_one_variant[MESA_SHADER_GEOMETRY] = st->has_shareable_shaders;
Marek Olšák55b097d2016-06-07 23:03:38 +0200452 st->shader_has_one_variant[MESA_SHADER_COMPUTE] = st->has_shareable_shaders;
Marek Olšáke57dd7a2015-09-28 00:04:39 +0200453
Jordan Justen4bea4cb2012-11-16 10:30:19 -0800454 _mesa_compute_version(ctx);
455
Brian Paula46d7572014-09-09 09:56:47 -0600456 if (ctx->Version == 0) {
457 /* This can happen when a core profile was requested, but the driver
458 * does not support some features of GL 3.1 or later.
459 */
460 st_destroy_context_priv(st);
461 return NULL;
462 }
463
Eric Anholtbe4b1662013-01-17 16:49:57 -0800464 _mesa_initialize_dispatch_tables(ctx);
Jordan Justen4bea4cb2012-11-16 10:30:19 -0800465 _mesa_initialize_vbo_vtxfmt(ctx);
466
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100467 return st;
468}
469
Marek Olšákc5e473f2012-04-16 04:56:12 +0200470static void st_init_driver_flags(struct gl_driver_flags *f)
471{
472 f->NewArray = ST_NEW_VERTEX_ARRAYS;
Marek Olšák99bd76d2013-04-15 13:00:54 +0200473 f->NewRasterizerDiscard = ST_NEW_RASTERIZER;
Marek Olšák77d8fbc2013-04-26 14:21:09 +0200474 f->NewUniformBuffer = ST_NEW_UNIFORM_BUFFER;
Marek Olšákbda90942015-05-18 12:49:10 +0200475 f->NewDefaultTessLevels = ST_NEW_TESS_STATE;
Marek Olšáke39ece02015-07-24 19:47:06 +0200476 f->NewTextureBuffer = ST_NEW_SAMPLER_VIEWS;
Ilia Mirkin9d6f9cc2014-09-19 20:59:17 -0400477 f->NewAtomicBuffer = ST_NEW_ATOMIC_BUFFER;
Ilia Mirkin68800362016-01-18 00:08:24 -0500478 f->NewShaderStorageBuffer = ST_NEW_STORAGE_BUFFER;
Ilia Mirkin78093162016-01-08 19:58:33 -0500479 f->NewImageUnits = ST_NEW_IMAGE_UNITS;
Marek Olšákc5e473f2012-04-16 04:56:12 +0200480}
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100481
Chia-I Wu57c65432010-06-23 17:40:49 +0800482struct st_context *st_create_context(gl_api api, struct pipe_context *pipe,
Kristian Høgsbergd3491e72010-10-12 11:58:47 -0400483 const struct gl_config *visual,
Vadim Girlina6457c02012-04-12 04:30:03 +0400484 struct st_context *share,
485 const struct st_config_options *options)
Brian80d26582007-11-01 17:46:04 -0600486{
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400487 struct gl_context *ctx;
488 struct gl_context *shareCtx = share ? share->ctx : NULL;
Brian91564ee2007-11-05 16:15:43 -0700489 struct dd_function_table funcs;
Brian Paula46d7572014-09-09 09:56:47 -0600490 struct st_context *st;
Brian91564ee2007-11-05 16:15:43 -0700491
492 memset(&funcs, 0, sizeof(funcs));
Marek Olšáka0ad1852015-04-29 15:05:19 +0200493 st_init_driver_functions(pipe->screen, &funcs);
Brian91564ee2007-11-05 16:15:43 -0700494
Emil Velikovdb8790c2016-06-07 17:33:48 +0100495 ctx = calloc(1, sizeof(struct gl_context));
496 if (!ctx)
497 return NULL;
498
499 if (!_mesa_initialize_context(ctx, api, visual, shareCtx, &funcs)) {
500 free(ctx);
Emil Velikov1ab5e152011-07-11 15:42:15 +0100501 return NULL;
502 }
Brian91564ee2007-11-05 16:15:43 -0700503
Marek Olšákc5e473f2012-04-16 04:56:12 +0200504 st_init_driver_flags(&ctx->DriverFlags);
505
Keith Whitwellb6e82562009-05-05 12:12:28 +0100506 /* XXX: need a capability bit in gallium to query if the pipe
507 * driver prefers DP4 or MUL/MAD for vertex transformation.
508 */
Jakob Bornecrantz99611f02010-08-05 17:13:41 -0700509 if (debug_get_option_mesa_mvp_dp4())
Marek Olšák002211f2014-08-03 04:31:56 +0200510 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = GL_TRUE;
Keith Whitwellb6e82562009-05-05 12:12:28 +0100511
Brian Paula46d7572014-09-09 09:56:47 -0600512 st = st_create_context_priv(ctx, pipe, options);
513 if (!st) {
514 _mesa_destroy_context(ctx);
Brian Paul87afc9b2008-05-16 16:04:30 -0600515 }
516
Brian Paula46d7572014-09-09 09:56:47 -0600517 return st;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100518}
519
Christian Königd117ddb2014-03-23 17:01:38 +0100520
521/**
522 * Callback to release the sampler view attached to a texture object.
523 * Called by _mesa_HashWalk().
524 */
525static void
526destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
527{
528 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
529 struct st_context *st = (struct st_context *) userData;
530
531 st_texture_release_sampler_view(st, st_texture_object(texObj));
532}
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100533
Brian91564ee2007-11-05 16:15:43 -0700534void st_destroy_context( struct st_context *st )
535{
Brian Paula770d402008-04-24 12:11:24 -0600536 struct pipe_context *pipe = st->pipe;
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400537 struct gl_context *ctx = st->ctx;
Brian Paulf104e4d2009-05-11 16:09:39 -0600538 GLuint i;
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600539
Christian Königd117ddb2014-03-23 17:01:38 +0100540 _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
541
Brian Paul69fd6762008-05-20 14:38:22 -0600542 st_reference_fragprog(st, &st->fp, NULL);
Brian Pauld1020492014-07-08 16:24:53 -0600543 st_reference_geomprog(st, &st->gp, NULL);
Brian Paul69fd6762008-05-20 14:38:22 -0600544 st_reference_vertprog(st, &st->vp, NULL);
Ilia Mirkinba9fb962014-07-11 22:11:21 -0400545 st_reference_tesscprog(st, &st->tcp, NULL);
546 st_reference_tesseprog(st, &st->tep, NULL);
Samuel Pitoiset7c79c1e2016-01-07 22:02:43 +0100547 st_reference_compprog(st, &st->cp, NULL);
Brian Paul69fd6762008-05-20 14:38:22 -0600548
Brian Paulf104e4d2009-05-11 16:09:39 -0600549 /* release framebuffer surfaces */
550 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
551 pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
552 }
553 pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
Marek Olšákf15bb3e2015-10-04 02:38:55 +0200554 pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
555 pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600556
Brian Paula0636152016-02-16 10:22:32 -0700557 _vbo_DestroyContext(ctx);
Brian Paul753635f2008-09-16 08:51:28 -0600558
Brian Paul3d203b62010-12-13 17:20:12 -0700559 st_destroy_program_variants(st);
560
Brian91564ee2007-11-05 16:15:43 -0700561 _mesa_free_context_data(ctx);
Brian Paula770d402008-04-24 12:11:24 -0600562
José Fonseca63da1aa2012-04-25 11:41:13 +0100563 /* This will free the st_context too, so 'st' must not be accessed
564 * afterwards. */
Brian91564ee2007-11-05 16:15:43 -0700565 st_destroy_context_priv(st);
José Fonseca63da1aa2012-04-25 11:41:13 +0100566 st = NULL;
Brian Paula770d402008-04-24 12:11:24 -0600567
Brian Paula770d402008-04-24 12:11:24 -0600568 pipe->destroy( pipe );
569
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500570 free(ctx);
Brian91564ee2007-11-05 16:15:43 -0700571}
572
Rob Clarkd6408372015-08-10 11:41:29 -0400573static void
574st_emit_string_marker(struct gl_context *ctx, const GLchar *string, GLsizei len)
575{
576 struct st_context *st = ctx->st;
577 st->pipe->emit_string_marker(st->pipe, string, len);
578}
Brian91564ee2007-11-05 16:15:43 -0700579
Marek Olšáka0ad1852015-04-29 15:05:19 +0200580void st_init_driver_functions(struct pipe_screen *screen,
581 struct dd_function_table *functions)
Brian6da92342007-08-06 20:53:28 +0100582{
Brian Paula37b2212010-06-10 20:23:03 -0600583 _mesa_init_shader_object_functions(functions);
Brian Paul34a5d3b2011-04-10 12:48:28 -0600584 _mesa_init_sampler_object_functions(functions);
Brian601a9ea2007-11-07 08:18:50 -0700585
Brian0df877a2008-03-18 17:16:23 -0600586 st_init_blit_functions(functions);
Nicolai Hähnle70e66c52016-01-09 18:05:58 -0500587 st_init_bufferobject_functions(screen, functions);
Brian6da92342007-08-06 20:53:28 +0100588 st_init_clear_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600589 st_init_bitmap_functions(functions);
Marek Olšákf04f1362015-08-24 02:55:20 +0200590 st_init_copy_image_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100591 st_init_drawpixels_functions(functions);
Brianbee148c2007-09-10 16:28:27 -0600592 st_init_rasterpos_functions(functions);
Chia-I Wu34064752009-09-22 15:00:24 +0800593
Chia-I Wu34064752009-09-22 15:00:24 +0800594 st_init_drawtex_functions(functions);
Chia-I Wu34064752009-09-22 15:00:24 +0800595
Chia-I Wua1aa53b2010-02-25 23:10:47 +0800596 st_init_eglimage_functions(functions);
597
Brian Paul04996bc2008-06-17 13:13:34 -0600598 st_init_fbo_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600599 st_init_feedback_functions(functions);
Dave Airlie1d90ee52013-03-04 06:14:18 +1000600 st_init_msaa_functions(functions);
Christoph Bumiller4cd1cfb2014-07-04 13:02:06 +0200601 st_init_perfmon_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600602 st_init_program_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600603 st_init_query_functions(functions);
Brian Paulc9b55832009-12-31 14:47:21 -0700604 st_init_cond_render_functions(functions);
Brian6f27aab2007-08-10 11:37:21 -0600605 st_init_readpixels_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100606 st_init_texture_functions(functions);
Marek Olšákaea4ed42011-03-08 11:32:35 +0100607 st_init_texture_barrier_functions(functions);
Marek Olšáka0ad1852015-04-29 15:05:19 +0200608 st_init_flush_functions(screen, functions);
Keith Whitwell39407fd2007-08-10 16:42:26 +0100609 st_init_string_functions(functions);
Jakob Bornecrantza93f9f32010-05-21 20:37:23 +0100610 st_init_viewport_functions(functions);
Samuel Pitoiset8aa66692016-01-03 15:41:48 +0100611 st_init_compute_functions(functions);
Briand7755092007-10-31 11:08:07 -0600612
Brian Paulfef6e362010-05-10 21:11:21 -0600613 st_init_xformfb_functions(functions);
Marek Olšák5257a6d2011-03-05 20:32:28 +0100614 st_init_syncobj_functions(functions);
Brian Paulfef6e362010-05-10 21:11:21 -0600615
Christian König80964222013-05-15 15:10:11 +0200616 st_init_vdpau_functions(functions);
617
Rob Clarkd6408372015-08-10 11:41:29 -0400618 if (screen->get_param(screen, PIPE_CAP_STRING_MARKER))
619 functions->EmitStringMarker = st_emit_string_marker;
620
Nicolai Hähnle2123bfc2016-01-04 17:31:05 -0500621 functions->Enable = st_Enable;
Briand7755092007-10-31 11:08:07 -0600622 functions->UpdateState = st_invalidate_state;
Marek Olšák5f51a242016-02-02 01:27:55 +0100623 functions->QueryMemoryInfo = st_query_memory_info;
Brian6da92342007-08-06 20:53:28 +0100624}