blob: 6e20fd1fda299f9ee787b6e5ded8328bf51b7cb1 [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"
Brian Paulc9b55832009-12-31 14:47:21 -070046#include "st_cb_condrender.h"
Marek Olšákf04f1362015-08-24 02:55:20 +020047#include "st_cb_copyimage.h"
Brian184b6a12007-08-02 14:21:16 -060048#include "st_cb_drawpixels.h"
Brian Pauld7a4d492008-06-20 08:09:59 -060049#include "st_cb_rasterpos.h"
Brian Paul4424c662008-08-12 18:02:44 -060050#include "st_cb_drawtex.h"
Chia-I Wua1aa53b2010-02-25 23:10:47 +080051#include "st_cb_eglimage.h"
Brian64da7512007-08-09 12:27:44 -060052#include "st_cb_fbo.h"
Brianb4bacd12007-09-17 14:24:11 -060053#include "st_cb_feedback.h"
Dave Airlie1d90ee52013-03-04 06:14:18 +100054#include "st_cb_msaa.h"
Christoph Bumiller4cd1cfb2014-07-04 13:02:06 +020055#include "st_cb_perfmon.h"
Brian Paul22a3d022008-03-24 11:55:29 -060056#include "st_cb_program.h"
Brian24864742007-08-11 19:57:37 +010057#include "st_cb_queryobj.h"
Brian6f27aab2007-08-10 11:37:21 -060058#include "st_cb_readpixels.h"
Brian4435bae2007-08-06 15:49:11 -060059#include "st_cb_texture.h"
Brian Paulfef6e362010-05-10 21:11:21 -060060#include "st_cb_xformfb.h"
Keith Whitwell5c2c0562007-08-10 12:57:14 +010061#include "st_cb_flush.h"
Marek Olšák5257a6d2011-03-05 20:32:28 +010062#include "st_cb_syncobj.h"
Keith Whitwell39407fd2007-08-10 16:42:26 +010063#include "st_cb_strings.h"
Marek Olšákaea4ed42011-03-08 11:32:35 +010064#include "st_cb_texturebarrier.h"
Jakob Bornecrantza93f9f32010-05-21 20:37:23 +010065#include "st_cb_viewport.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010066#include "st_atom.h"
67#include "st_draw.h"
Brianc6499a72007-11-05 18:04:30 -070068#include "st_extensions.h"
Brian62abcb92008-02-08 14:54:18 -070069#include "st_gen_mipmap.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010070#include "st_program.h"
Christian König80964222013-05-15 15:10:11 +020071#include "st_vdpau.h"
Christian Königd117ddb2014-03-23 17:01:38 +010072#include "st_texture.h"
Keith Whitwell943964a2007-06-14 18:23:43 +010073#include "pipe/p_context.h"
José Fonseca28486882010-02-02 14:42:17 +000074#include "util/u_inlines.h"
Marek Olšák76d9a992012-04-13 19:27:45 +020075#include "util/u_upload_mgr.h"
Brian339e7ec2008-03-11 18:54:31 -060076#include "cso_cache/cso_context.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010077
Brian6f27aab2007-08-10 11:37:21 -060078
Brian Paul6b97fe52010-08-06 15:09:51 -060079DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
Jakob Bornecrantz99611f02010-08-05 17:13:41 -070080
81
Briand7755092007-10-31 11:08:07 -060082/**
83 * Called via ctx->Driver.UpdateState()
84 */
Kristian Høgsbergf9995b32010-10-12 12:26:10 -040085void st_invalidate_state(struct gl_context * ctx, GLuint new_state)
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010086{
87 struct st_context *st = st_context(ctx);
88
Marek Olšákbc1c8362012-01-23 03:11:17 +010089 /* Replace _NEW_FRAG_CLAMP with ST_NEW_FRAGMENT_PROGRAM for the fallback. */
90 if (st->clamp_frag_color_in_shader && (new_state & _NEW_FRAG_CLAMP)) {
91 new_state &= ~_NEW_FRAG_CLAMP;
92 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
93 }
94
95 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
96 if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT)) {
97 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
98 }
99
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100100 st->dirty.mesa |= new_state;
101 st->dirty.st |= ST_NEW_MESA;
Briand7755092007-10-31 11:08:07 -0600102
103 /* This is the only core Mesa module we depend upon.
104 * No longer use swrast, swsetup, tnl.
105 */
106 _vbo_InvalidateState(ctx, new_state);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100107}
108
Brian Paula46d7572014-09-09 09:56:47 -0600109
110static void
111st_destroy_context_priv(struct st_context *st)
112{
113 uint shader, i;
114
115 st_destroy_atoms( st );
116 st_destroy_draw( st );
117 st_destroy_clear(st);
118 st_destroy_bitmap(st);
119 st_destroy_drawpix(st);
120 st_destroy_drawtex(st);
Christoph Bumiller4cd1cfb2014-07-04 13:02:06 +0200121 st_destroy_perfmon(st);
Brian Paula46d7572014-09-09 09:56:47 -0600122
Brian Paul2f0143c2015-02-28 09:01:33 -0700123 for (shader = 0; shader < ARRAY_SIZE(st->state.sampler_views); shader++) {
124 for (i = 0; i < ARRAY_SIZE(st->state.sampler_views[0]); i++) {
Brian Paula46d7572014-09-09 09:56:47 -0600125 pipe_sampler_view_release(st->pipe,
126 &st->state.sampler_views[shader][i]);
127 }
128 }
129
130 if (st->default_texture) {
131 st->ctx->Driver.DeleteTexture(st->ctx, st->default_texture);
132 st->default_texture = NULL;
133 }
134
135 u_upload_destroy(st->uploader);
136 if (st->indexbuf_uploader) {
137 u_upload_destroy(st->indexbuf_uploader);
138 }
139 if (st->constbuf_uploader) {
140 u_upload_destroy(st->constbuf_uploader);
141 }
Marek Olšák0a60ebe2014-12-07 14:21:41 +0100142
143 cso_destroy_context(st->cso_context);
Brian Paula46d7572014-09-09 09:56:47 -0600144 free( st );
145}
146
147
Brian91564ee2007-11-05 16:15:43 -0700148static struct st_context *
Vadim Girlina6457c02012-04-12 04:30:03 +0400149st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe,
150 const struct st_config_options *options)
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100151{
Marek Olšák8c655f42012-04-24 17:04:18 +0200152 struct pipe_screen *screen = pipe->screen;
Brian339e7ec2008-03-11 18:54:31 -0600153 uint i;
Brian Paulf1a59a62009-02-12 10:14:36 -0700154 struct st_context *st = ST_CALLOC_STRUCT( st_context );
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100155
Vadim Girlina6457c02012-04-12 04:30:03 +0400156 st->options = *options;
157
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100158 ctx->st = st;
159
160 st->ctx = ctx;
Keith Whitwell943964a2007-06-14 18:23:43 +0100161 st->pipe = pipe;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100162
Keith Whitwellb02ef742009-10-05 15:50:11 +0100163 /* XXX: this is one-off, per-screen init: */
164 st_debug_init();
165
Brian8984a282007-10-31 11:35:50 -0600166 /* state tracker needs the VBO module */
167 _vbo_CreateContext(ctx);
Brian8984a282007-10-31 11:35:50 -0600168
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100169 st->dirty.mesa = ~0;
170 st->dirty.st = ~0;
171
Brian Paul282bb872013-10-17 09:28:17 -0600172 /* Create upload manager for vertex data for glBitmap, glDrawPixels,
173 * glClear, etc.
174 */
Marek Olšák76d9a992012-04-13 19:27:45 +0200175 st->uploader = u_upload_create(st->pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER);
Marek Olšák8c655f42012-04-24 17:04:18 +0200176
177 if (!screen->get_param(screen, PIPE_CAP_USER_INDEX_BUFFERS)) {
178 st->indexbuf_uploader = u_upload_create(st->pipe, 128 * 1024, 4,
179 PIPE_BIND_INDEX_BUFFER);
180 }
181
Marek Olšák7a054592012-04-24 20:16:50 +0200182 if (!screen->get_param(screen, PIPE_CAP_USER_CONSTANT_BUFFERS)) {
183 unsigned alignment =
184 screen->get_param(screen, PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT);
185
186 st->constbuf_uploader = u_upload_create(pipe, 128 * 1024, alignment,
187 PIPE_BIND_CONSTANT_BUFFER);
188 }
189
Brian339e7ec2008-03-11 18:54:31 -0600190 st->cso_context = cso_create_context(pipe);
Zack Rusin97803272007-09-14 04:08:58 -0400191
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100192 st_init_atoms( st );
Brianc62b1972008-03-28 14:53:47 -0600193 st_init_bitmap(st);
Brian Paulce5c8672008-04-03 12:54:32 -0600194 st_init_clear(st);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100195 st_init_draw( st );
Brian61d02152007-08-02 20:40:19 -0600196
Brian Paul282bb872013-10-17 09:28:17 -0600197 /* Choose texture target for glDrawPixels, glBitmap, renderbuffers */
198 if (pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
Luca Barbieri3070e0e2010-08-06 07:39:21 +0200199 st->internal_target = PIPE_TEXTURE_2D;
200 else
201 st->internal_target = PIPE_TEXTURE_RECT;
202
Brian Paul84e5cb32012-08-17 14:33:31 -0600203 /* Vertex element objects used for drawing rectangles for glBitmap,
204 * glDrawPixels, glClear, etc.
205 */
Brian Paul2f0143c2015-02-28 09:01:33 -0700206 for (i = 0; i < ARRAY_SIZE(st->velems_util_draw); i++) {
Roland Scheideggerae7b7bf2010-03-09 15:09:01 +0100207 memset(&st->velems_util_draw[i], 0, sizeof(struct pipe_vertex_element));
208 st->velems_util_draw[i].src_offset = i * 4 * sizeof(float);
209 st->velems_util_draw[i].instance_divisor = 0;
Marek Olšáke73bf3b2012-03-29 17:51:50 +0200210 st->velems_util_draw[i].vertex_buffer_index =
211 cso_get_aux_vertex_buffer_slot(st->cso_context);
Roland Scheideggerae7b7bf2010-03-09 15:09:01 +0100212 st->velems_util_draw[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
213 }
214
Brian1e6d1ab2007-08-17 15:27:18 +0100215 /* we want all vertex data to be placed in buffer objects */
216 vbo_use_buffer_objects(ctx);
217
Brian Paul2997b452011-10-25 17:41:12 -0600218
219 /* make sure that no VBOs are left mapped when we're drawing. */
220 vbo_always_unmap_buffers(ctx);
221
Brian6da92342007-08-06 20:53:28 +0100222 /* Need these flags:
223 */
224 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
Brian6da92342007-08-06 20:53:28 +0100225
Briane3bdd662007-08-16 17:33:49 -0600226 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
227
Marek Olšák5ba15d82012-07-09 05:08:36 +0200228 st->has_stencil_export =
229 screen->get_param(screen, PIPE_CAP_SHADER_STENCIL_EXPORT);
Marek Olšák4362bda2013-01-31 22:30:44 +0100230 st->has_shader_model3 = screen->get_param(screen, PIPE_CAP_SM3);
Marek Olšák0127d262014-08-02 21:38:25 +0200231 st->has_etc1 = screen->is_format_supported(screen, PIPE_FORMAT_ETC1_RGB8,
232 PIPE_TEXTURE_2D, 0,
233 PIPE_BIND_SAMPLER_VIEW);
Ilia Mirkin33edda72015-02-16 02:27:37 -0500234 st->has_etc2 = screen->is_format_supported(screen, PIPE_FORMAT_ETC2_RGB8,
235 PIPE_TEXTURE_2D, 0,
236 PIPE_BIND_SAMPLER_VIEW);
Marek Olšák3e10ab62013-03-14 17:18:43 +0100237 st->prefer_blit_based_texture_transfer = screen->get_param(screen,
238 PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER);
Marek Olšákb99645f2015-10-20 00:12:53 +0200239 st->force_persample_in_shader =
240 screen->get_param(screen, PIPE_CAP_SAMPLE_SHADING) &&
241 !screen->get_param(screen, PIPE_CAP_FORCE_PERSAMPLE_INTERP);
Marek Olšákf4e938e2015-09-27 23:36:59 +0200242 st->has_shareable_shaders = screen->get_param(screen,
243 PIPE_CAP_SHAREABLE_SHADERS);
Christoph Bumiller8acaf862013-03-15 22:11:31 +0100244 st->needs_texcoord_semantic =
245 screen->get_param(screen, PIPE_CAP_TGSI_TEXCOORD);
Christoph Bumiller729abfd2013-04-12 13:42:01 +0200246 st->apply_texture_swizzle_to_border_color =
247 !!(screen->get_param(screen, PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK) &
248 (PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_NV50 |
249 PIPE_QUIRK_TEXTURE_BORDER_COLOR_SWIZZLE_R600));
Marek Olšák5c691732014-08-03 04:20:31 +0200250 st->has_time_elapsed =
251 screen->get_param(screen, PIPE_CAP_QUERY_TIME_ELAPSED);
Christoph Bumiller8acaf862013-03-15 22:11:31 +0100252
Brianc6499a72007-11-05 18:04:30 -0700253 /* GL limits and extensions */
Marek Olšák35e755f2014-08-03 04:36:19 +0200254 st_init_limits(st->pipe->screen, &ctx->Const, &ctx->Extensions);
Marek Olšák4155d1c2014-09-12 16:41:19 +0200255 st_init_extensions(st->pipe->screen, &ctx->Const,
Marek Olšák5c691732014-08-03 04:20:31 +0200256 &ctx->Extensions, &st->options, ctx->Mesa_DXTn);
257
Christoph Bumiller4cd1cfb2014-07-04 13:02:06 +0200258 if (st_init_perfmon(st)) {
259 /* GL_AMD_performance_monitor is only enabled when the underlying
260 * driver expose GPU hardware performance counters. */
261 ctx->Extensions.AMD_performance_monitor = GL_TRUE;
262 }
263
Marek Olšák5c691732014-08-03 04:20:31 +0200264 /* Enable shader-based fallbacks for ARB_color_buffer_float if needed. */
265 if (screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_UNCLAMPED)) {
266 if (!screen->get_param(screen, PIPE_CAP_VERTEX_COLOR_CLAMPED)) {
267 st->clamp_vert_color_in_shader = GL_TRUE;
268 }
269
270 if (!screen->get_param(screen, PIPE_CAP_FRAGMENT_COLOR_CLAMPED)) {
271 st->clamp_frag_color_in_shader = GL_TRUE;
272 }
273
274 /* For drivers which cannot do color clamping, it's better to just
275 * disable ARB_color_buffer_float in the core profile, because
276 * the clamping is deprecated there anyway. */
277 if (ctx->API == API_OPENGL_CORE &&
278 (st->clamp_frag_color_in_shader || st->clamp_vert_color_in_shader)) {
279 st->clamp_vert_color_in_shader = GL_FALSE;
280 st->clamp_frag_color_in_shader = GL_FALSE;
281 ctx->Extensions.ARB_color_buffer_float = GL_FALSE;
282 }
283 }
Brianaf3d6c82007-08-21 20:15:00 -0600284
Marek Olšák35e755f2014-08-03 04:36:19 +0200285 /* called after _mesa_create_context/_mesa_init_point, fix default user
286 * settable max point size up
287 */
288 st->ctx->Point.MaxSize = MAX2(ctx->Const.MaxPointSize,
289 ctx->Const.MaxPointSizeAA);
Abdiel Janulgue49e04312014-12-01 14:59:08 +0200290 /* For vertex shaders, make sure not to emit saturate when SM 3.0 is not supported */
291 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].EmitNoSat = !st->has_shader_model3;
Marek Olšák35e755f2014-08-03 04:36:19 +0200292
Tapani Pällif17c8c22015-06-29 09:48:52 +0300293 if (!ctx->Extensions.ARB_gpu_shader5) {
294 for (i = 0; i < MESA_SHADER_STAGES; i++)
295 ctx->Const.ShaderCompilerOptions[i].EmitNoIndirectSampler = true;
296 }
297
Marek Olšáke57dd7a2015-09-28 00:04:39 +0200298 /* Set which shader types can be compiled at link time. */
299 st->shader_has_one_variant[MESA_SHADER_VERTEX] =
300 st->has_shareable_shaders &&
301 !st->clamp_vert_color_in_shader;
302
303 st->shader_has_one_variant[MESA_SHADER_FRAGMENT] =
304 st->has_shareable_shaders &&
305 !st->clamp_frag_color_in_shader &&
306 !st->force_persample_in_shader;
307
308 st->shader_has_one_variant[MESA_SHADER_TESS_CTRL] = st->has_shareable_shaders;
309 st->shader_has_one_variant[MESA_SHADER_TESS_EVAL] = st->has_shareable_shaders;
310 st->shader_has_one_variant[MESA_SHADER_GEOMETRY] = st->has_shareable_shaders;
311
Jordan Justen4bea4cb2012-11-16 10:30:19 -0800312 _mesa_compute_version(ctx);
313
Brian Paula46d7572014-09-09 09:56:47 -0600314 if (ctx->Version == 0) {
315 /* This can happen when a core profile was requested, but the driver
316 * does not support some features of GL 3.1 or later.
317 */
318 st_destroy_context_priv(st);
319 return NULL;
320 }
321
Eric Anholtbe4b1662013-01-17 16:49:57 -0800322 _mesa_initialize_dispatch_tables(ctx);
Jordan Justen4bea4cb2012-11-16 10:30:19 -0800323 _mesa_initialize_vbo_vtxfmt(ctx);
324
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100325 return st;
326}
327
Marek Olšákc5e473f2012-04-16 04:56:12 +0200328static void st_init_driver_flags(struct gl_driver_flags *f)
329{
330 f->NewArray = ST_NEW_VERTEX_ARRAYS;
Marek Olšák99bd76d2013-04-15 13:00:54 +0200331 f->NewRasterizerDiscard = ST_NEW_RASTERIZER;
Marek Olšák77d8fbc2013-04-26 14:21:09 +0200332 f->NewUniformBuffer = ST_NEW_UNIFORM_BUFFER;
Marek Olšákbda90942015-05-18 12:49:10 +0200333 f->NewDefaultTessLevels = ST_NEW_TESS_STATE;
Marek Olšáke39ece02015-07-24 19:47:06 +0200334 f->NewTextureBuffer = ST_NEW_SAMPLER_VIEWS;
Marek Olšákc5e473f2012-04-16 04:56:12 +0200335}
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100336
Chia-I Wu57c65432010-06-23 17:40:49 +0800337struct st_context *st_create_context(gl_api api, struct pipe_context *pipe,
Kristian Høgsbergd3491e72010-10-12 11:58:47 -0400338 const struct gl_config *visual,
Vadim Girlina6457c02012-04-12 04:30:03 +0400339 struct st_context *share,
340 const struct st_config_options *options)
Brian80d26582007-11-01 17:46:04 -0600341{
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400342 struct gl_context *ctx;
343 struct gl_context *shareCtx = share ? share->ctx : NULL;
Brian91564ee2007-11-05 16:15:43 -0700344 struct dd_function_table funcs;
Brian Paula46d7572014-09-09 09:56:47 -0600345 struct st_context *st;
Brian91564ee2007-11-05 16:15:43 -0700346
347 memset(&funcs, 0, sizeof(funcs));
Marek Olšáka0ad1852015-04-29 15:05:19 +0200348 st_init_driver_functions(pipe->screen, &funcs);
Brian91564ee2007-11-05 16:15:43 -0700349
Brian Paul733dba22012-09-29 08:47:56 -0600350 ctx = _mesa_create_context(api, visual, shareCtx, &funcs);
Emil Velikov1ab5e152011-07-11 15:42:15 +0100351 if (!ctx) {
352 return NULL;
353 }
Brian91564ee2007-11-05 16:15:43 -0700354
Marek Olšákc5e473f2012-04-16 04:56:12 +0200355 st_init_driver_flags(&ctx->DriverFlags);
356
Keith Whitwellb6e82562009-05-05 12:12:28 +0100357 /* XXX: need a capability bit in gallium to query if the pipe
358 * driver prefers DP4 or MUL/MAD for vertex transformation.
359 */
Jakob Bornecrantz99611f02010-08-05 17:13:41 -0700360 if (debug_get_option_mesa_mvp_dp4())
Marek Olšák002211f2014-08-03 04:31:56 +0200361 ctx->Const.ShaderCompilerOptions[MESA_SHADER_VERTEX].OptimizeForAOS = GL_TRUE;
Keith Whitwellb6e82562009-05-05 12:12:28 +0100362
Brian Paula46d7572014-09-09 09:56:47 -0600363 st = st_create_context_priv(ctx, pipe, options);
364 if (!st) {
365 _mesa_destroy_context(ctx);
Brian Paul87afc9b2008-05-16 16:04:30 -0600366 }
367
Brian Paula46d7572014-09-09 09:56:47 -0600368 return st;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100369}
370
Christian Königd117ddb2014-03-23 17:01:38 +0100371
372/**
373 * Callback to release the sampler view attached to a texture object.
374 * Called by _mesa_HashWalk().
375 */
376static void
377destroy_tex_sampler_cb(GLuint id, void *data, void *userData)
378{
379 struct gl_texture_object *texObj = (struct gl_texture_object *) data;
380 struct st_context *st = (struct st_context *) userData;
381
382 st_texture_release_sampler_view(st, st_texture_object(texObj));
383}
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100384
Brian91564ee2007-11-05 16:15:43 -0700385void st_destroy_context( struct st_context *st )
386{
Brian Paula770d402008-04-24 12:11:24 -0600387 struct pipe_context *pipe = st->pipe;
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400388 struct gl_context *ctx = st->ctx;
Brian Paulf104e4d2009-05-11 16:09:39 -0600389 GLuint i;
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600390
Christian Königd117ddb2014-03-23 17:01:38 +0100391 _mesa_HashWalk(ctx->Shared->TexObjects, destroy_tex_sampler_cb, st);
392
Brian Paul69fd6762008-05-20 14:38:22 -0600393 st_reference_fragprog(st, &st->fp, NULL);
Brian Pauld1020492014-07-08 16:24:53 -0600394 st_reference_geomprog(st, &st->gp, NULL);
Brian Paul69fd6762008-05-20 14:38:22 -0600395 st_reference_vertprog(st, &st->vp, NULL);
Ilia Mirkinba9fb962014-07-11 22:11:21 -0400396 st_reference_tesscprog(st, &st->tcp, NULL);
397 st_reference_tesseprog(st, &st->tep, NULL);
Brian Paul69fd6762008-05-20 14:38:22 -0600398
Brian Paulf104e4d2009-05-11 16:09:39 -0600399 /* release framebuffer surfaces */
400 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
401 pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
402 }
403 pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
Marek Olšákf15bb3e2015-10-04 02:38:55 +0200404 pipe_sampler_view_reference(&st->pixel_xfer.pixelmap_sampler_view, NULL);
405 pipe_resource_reference(&st->pixel_xfer.pixelmap_texture, NULL);
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600406
Brian Paul753635f2008-09-16 08:51:28 -0600407 _vbo_DestroyContext(st->ctx);
408
Brian Paul3d203b62010-12-13 17:20:12 -0700409 st_destroy_program_variants(st);
410
Brian91564ee2007-11-05 16:15:43 -0700411 _mesa_free_context_data(ctx);
Brian Paula770d402008-04-24 12:11:24 -0600412
José Fonseca63da1aa2012-04-25 11:41:13 +0100413 /* This will free the st_context too, so 'st' must not be accessed
414 * afterwards. */
Brian91564ee2007-11-05 16:15:43 -0700415 st_destroy_context_priv(st);
José Fonseca63da1aa2012-04-25 11:41:13 +0100416 st = NULL;
Brian Paula770d402008-04-24 12:11:24 -0600417
Brian Paula770d402008-04-24 12:11:24 -0600418 pipe->destroy( pipe );
419
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500420 free(ctx);
Brian91564ee2007-11-05 16:15:43 -0700421}
422
423
Marek Olšáka0ad1852015-04-29 15:05:19 +0200424void st_init_driver_functions(struct pipe_screen *screen,
425 struct dd_function_table *functions)
Brian6da92342007-08-06 20:53:28 +0100426{
Brian Paula37b2212010-06-10 20:23:03 -0600427 _mesa_init_shader_object_functions(functions);
Brian Paul34a5d3b2011-04-10 12:48:28 -0600428 _mesa_init_sampler_object_functions(functions);
Brian601a9ea2007-11-07 08:18:50 -0700429
Brian0df877a2008-03-18 17:16:23 -0600430 st_init_blit_functions(functions);
Brianf6cd3772008-03-20 14:50:17 -0600431 st_init_bufferobject_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100432 st_init_clear_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600433 st_init_bitmap_functions(functions);
Marek Olšákf04f1362015-08-24 02:55:20 +0200434 st_init_copy_image_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100435 st_init_drawpixels_functions(functions);
Brianbee148c2007-09-10 16:28:27 -0600436 st_init_rasterpos_functions(functions);
Chia-I Wu34064752009-09-22 15:00:24 +0800437
Chia-I Wu34064752009-09-22 15:00:24 +0800438 st_init_drawtex_functions(functions);
Chia-I Wu34064752009-09-22 15:00:24 +0800439
Chia-I Wua1aa53b2010-02-25 23:10:47 +0800440 st_init_eglimage_functions(functions);
441
Brian Paul04996bc2008-06-17 13:13:34 -0600442 st_init_fbo_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600443 st_init_feedback_functions(functions);
Dave Airlie1d90ee52013-03-04 06:14:18 +1000444 st_init_msaa_functions(functions);
Christoph Bumiller4cd1cfb2014-07-04 13:02:06 +0200445 st_init_perfmon_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600446 st_init_program_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600447 st_init_query_functions(functions);
Brian Paulc9b55832009-12-31 14:47:21 -0700448 st_init_cond_render_functions(functions);
Brian6f27aab2007-08-10 11:37:21 -0600449 st_init_readpixels_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100450 st_init_texture_functions(functions);
Marek Olšákaea4ed42011-03-08 11:32:35 +0100451 st_init_texture_barrier_functions(functions);
Marek Olšáka0ad1852015-04-29 15:05:19 +0200452 st_init_flush_functions(screen, functions);
Keith Whitwell39407fd2007-08-10 16:42:26 +0100453 st_init_string_functions(functions);
Jakob Bornecrantza93f9f32010-05-21 20:37:23 +0100454 st_init_viewport_functions(functions);
Briand7755092007-10-31 11:08:07 -0600455
Brian Paulfef6e362010-05-10 21:11:21 -0600456 st_init_xformfb_functions(functions);
Marek Olšák5257a6d2011-03-05 20:32:28 +0100457 st_init_syncobj_functions(functions);
Brian Paulfef6e362010-05-10 21:11:21 -0600458
Christian König80964222013-05-15 15:10:11 +0200459 st_init_vdpau_functions(functions);
460
Briand7755092007-10-31 11:08:07 -0600461 functions->UpdateState = st_invalidate_state;
Brian6da92342007-08-06 20:53:28 +0100462}