blob: dc1d33f1daeb999b62dff374466d907ac8a2c84c [file] [log] [blame]
Keith Whitwell8e4a95a2007-05-24 10:41:34 +01001/**************************************************************************
2 *
Brianc6499a72007-11-05 18:04:30 -07003 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS 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
Brian1e6d1ab2007-08-17 15:27:18 +010028#include "main/imports.h"
Brian Paul122c6762011-12-03 10:04:18 -070029#include "main/accum.h"
Brian80d26582007-11-01 17:46:04 -060030#include "main/context.h"
Brian Paul34a5d3b2011-04-10 12:48:28 -060031#include "main/samplerobj.h"
Brian Paula37b2212010-06-10 20:23:03 -060032#include "main/shaderobj.h"
Vinson Lee9c98e9e2010-08-04 21:17:13 -070033#include "program/prog_cache.h"
Brian1e6d1ab2007-08-17 15:27:18 +010034#include "vbo/vbo.h"
Brian Paul5ebd8492008-06-17 14:27:36 -060035#include "glapi/glapi.h"
Chia-I Wude8a8792010-02-07 19:20:52 +080036#include "st_context.h"
Keith Whitwellb02ef742009-10-05 15:50:11 +010037#include "st_debug.h"
Brianf6cd3772008-03-20 14:50:17 -060038#include "st_cb_bitmap.h"
Brian0df877a2008-03-18 17:16:23 -060039#include "st_cb_blit.h"
Brianf6cd3772008-03-20 14:50:17 -060040#include "st_cb_bufferobjects.h"
Brian51b300c2007-08-02 10:29:50 -060041#include "st_cb_clear.h"
Brian Paulc9b55832009-12-31 14:47:21 -070042#include "st_cb_condrender.h"
Brian184b6a12007-08-02 14:21:16 -060043#include "st_cb_drawpixels.h"
Brian Pauld7a4d492008-06-20 08:09:59 -060044#include "st_cb_rasterpos.h"
Brian Paul4424c662008-08-12 18:02:44 -060045#include "st_cb_drawtex.h"
Chia-I Wua1aa53b2010-02-25 23:10:47 +080046#include "st_cb_eglimage.h"
Brian64da7512007-08-09 12:27:44 -060047#include "st_cb_fbo.h"
Brianb4bacd12007-09-17 14:24:11 -060048#include "st_cb_feedback.h"
Brian Paul22a3d022008-03-24 11:55:29 -060049#include "st_cb_program.h"
Brian24864742007-08-11 19:57:37 +010050#include "st_cb_queryobj.h"
Brian6f27aab2007-08-10 11:37:21 -060051#include "st_cb_readpixels.h"
Brian4435bae2007-08-06 15:49:11 -060052#include "st_cb_texture.h"
Brian Paulfef6e362010-05-10 21:11:21 -060053#include "st_cb_xformfb.h"
Keith Whitwell5c2c0562007-08-10 12:57:14 +010054#include "st_cb_flush.h"
Marek Olšák5257a6d2011-03-05 20:32:28 +010055#include "st_cb_syncobj.h"
Keith Whitwell39407fd2007-08-10 16:42:26 +010056#include "st_cb_strings.h"
Marek Olšákaea4ed42011-03-08 11:32:35 +010057#include "st_cb_texturebarrier.h"
Jakob Bornecrantza93f9f32010-05-21 20:37:23 +010058#include "st_cb_viewport.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010059#include "st_atom.h"
60#include "st_draw.h"
Brianc6499a72007-11-05 18:04:30 -070061#include "st_extensions.h"
Brian62abcb92008-02-08 14:54:18 -070062#include "st_gen_mipmap.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010063#include "st_program.h"
Keith Whitwell943964a2007-06-14 18:23:43 +010064#include "pipe/p_context.h"
José Fonseca28486882010-02-02 14:42:17 +000065#include "util/u_inlines.h"
Brian339e7ec2008-03-11 18:54:31 -060066#include "cso_cache/cso_context.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010067
Brian6f27aab2007-08-10 11:37:21 -060068
Brian Paul6b97fe52010-08-06 15:09:51 -060069DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
Jakob Bornecrantz99611f02010-08-05 17:13:41 -070070
71
Briand7755092007-10-31 11:08:07 -060072/**
73 * Called via ctx->Driver.UpdateState()
74 */
Kristian Høgsbergf9995b32010-10-12 12:26:10 -040075void st_invalidate_state(struct gl_context * ctx, GLuint new_state)
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010076{
77 struct st_context *st = st_context(ctx);
78
79 st->dirty.mesa |= new_state;
80 st->dirty.st |= ST_NEW_MESA;
Briand7755092007-10-31 11:08:07 -060081
82 /* This is the only core Mesa module we depend upon.
83 * No longer use swrast, swsetup, tnl.
84 */
85 _vbo_InvalidateState(ctx, new_state);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010086}
87
88
Brian Paul1a820f52008-09-17 14:32:33 -060089/**
90 * Check for multisample env var override.
91 */
92int
93st_get_msaa(void)
94{
95 const char *msaa = _mesa_getenv("__GL_FSAA_MODE");
96 if (msaa)
97 return atoi(msaa);
98 return 0;
99}
100
101
Brian91564ee2007-11-05 16:15:43 -0700102static struct st_context *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400103st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe )
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100104{
Brian339e7ec2008-03-11 18:54:31 -0600105 uint i;
Brian Paulf1a59a62009-02-12 10:14:36 -0700106 struct st_context *st = ST_CALLOC_STRUCT( st_context );
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100107
108 ctx->st = st;
109
110 st->ctx = ctx;
Keith Whitwell943964a2007-06-14 18:23:43 +0100111 st->pipe = pipe;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100112
Keith Whitwellb02ef742009-10-05 15:50:11 +0100113 /* XXX: this is one-off, per-screen init: */
114 st_debug_init();
115
Brian8984a282007-10-31 11:35:50 -0600116 /* state tracker needs the VBO module */
117 _vbo_CreateContext(ctx);
Brian8984a282007-10-31 11:35:50 -0600118
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100119 st->dirty.mesa = ~0;
120 st->dirty.st = ~0;
121
Brian339e7ec2008-03-11 18:54:31 -0600122 st->cso_context = cso_create_context(pipe);
Zack Rusin97803272007-09-14 04:08:58 -0400123
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100124 st_init_atoms( st );
Brianc62b1972008-03-28 14:53:47 -0600125 st_init_bitmap(st);
Brian Paulce5c8672008-04-03 12:54:32 -0600126 st_init_clear(st);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100127 st_init_draw( st );
Brian62abcb92008-02-08 14:54:18 -0700128 st_init_generate_mipmap(st);
Brian0df877a2008-03-18 17:16:23 -0600129 st_init_blit(st);
Brian61d02152007-08-02 20:40:19 -0600130
Luca Barbieri3070e0e2010-08-06 07:39:21 +0200131 if(pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
132 st->internal_target = PIPE_TEXTURE_2D;
133 else
134 st->internal_target = PIPE_TEXTURE_RECT;
135
Roland Scheideggerae7b7bf2010-03-09 15:09:01 +0100136 for (i = 0; i < 3; i++) {
137 memset(&st->velems_util_draw[i], 0, sizeof(struct pipe_vertex_element));
138 st->velems_util_draw[i].src_offset = i * 4 * sizeof(float);
139 st->velems_util_draw[i].instance_divisor = 0;
140 st->velems_util_draw[i].vertex_buffer_index = 0;
141 st->velems_util_draw[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
142 }
143
Brian1e6d1ab2007-08-17 15:27:18 +0100144 /* we want all vertex data to be placed in buffer objects */
145 vbo_use_buffer_objects(ctx);
146
Brian Paul2997b452011-10-25 17:41:12 -0600147
148 /* make sure that no VBOs are left mapped when we're drawing. */
149 vbo_always_unmap_buffers(ctx);
150
Brian6da92342007-08-06 20:53:28 +0100151 /* Need these flags:
152 */
153 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
Brian6da92342007-08-06 20:53:28 +0100154
Briane3bdd662007-08-16 17:33:49 -0600155 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
156
Briand6a739f2007-10-30 16:13:37 -0600157 st->pixel_xfer.cache = _mesa_new_program_cache();
Briandf174bd2007-10-30 12:24:05 -0600158
Brian Paul1a820f52008-09-17 14:32:33 -0600159 st->force_msaa = st_get_msaa();
160
Brianc6499a72007-11-05 18:04:30 -0700161 /* GL limits and extensions */
162 st_init_limits(st);
163 st_init_extensions(st);
Brianaf3d6c82007-08-21 20:15:00 -0600164
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100165 return st;
166}
167
168
Chia-I Wu57c65432010-06-23 17:40:49 +0800169struct st_context *st_create_context(gl_api api, struct pipe_context *pipe,
Kristian Høgsbergd3491e72010-10-12 11:58:47 -0400170 const struct gl_config *visual,
Brian91564ee2007-11-05 16:15:43 -0700171 struct st_context *share)
Brian80d26582007-11-01 17:46:04 -0600172{
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400173 struct gl_context *ctx;
174 struct gl_context *shareCtx = share ? share->ctx : NULL;
Brian91564ee2007-11-05 16:15:43 -0700175 struct dd_function_table funcs;
176
Brian Paulc628fd72010-11-23 10:28:43 -0700177 /* Sanity checks */
178 assert(MESA_SHADER_VERTEX == PIPE_SHADER_VERTEX);
179 assert(MESA_SHADER_FRAGMENT == PIPE_SHADER_FRAGMENT);
180 assert(MESA_SHADER_GEOMETRY == PIPE_SHADER_GEOMETRY);
181
Brian91564ee2007-11-05 16:15:43 -0700182 memset(&funcs, 0, sizeof(funcs));
183 st_init_driver_functions(&funcs);
184
Brian Paul6f2f4492011-02-08 19:25:04 -0700185 ctx = _mesa_create_context(api, visual, shareCtx, &funcs, NULL);
Emil Velikov1ab5e152011-07-11 15:42:15 +0100186 if (!ctx) {
187 return NULL;
188 }
Brian91564ee2007-11-05 16:15:43 -0700189
Keith Whitwellb6e82562009-05-05 12:12:28 +0100190 /* XXX: need a capability bit in gallium to query if the pipe
191 * driver prefers DP4 or MUL/MAD for vertex transformation.
192 */
Jakob Bornecrantz99611f02010-08-05 17:13:41 -0700193 if (debug_get_option_mesa_mvp_dp4())
Keith Whitwellb6e82562009-05-05 12:12:28 +0100194 _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
195
Brian91564ee2007-11-05 16:15:43 -0700196 return st_create_context_priv(ctx, pipe);
Brian80d26582007-11-01 17:46:04 -0600197}
198
199
Brian91564ee2007-11-05 16:15:43 -0700200static void st_destroy_context_priv( struct st_context *st )
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100201{
Brian6128c932007-12-26 07:16:12 -0700202 uint i;
203
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100204 st_destroy_atoms( st );
205 st_destroy_draw( st );
Brian0df877a2008-03-18 17:16:23 -0600206 st_destroy_generate_mipmap(st);
207 st_destroy_blit(st);
Brian85e4ec62008-03-20 09:13:51 -0600208 st_destroy_clear(st);
Brian Paul414b5352008-06-17 16:57:02 -0600209 st_destroy_bitmap(st);
Brian Paul90ce8462008-05-20 13:38:45 -0600210 st_destroy_drawpix(st);
Brian Paul4424c662008-08-12 18:02:44 -0600211 st_destroy_drawtex(st);
Brian61d02152007-08-02 20:40:19 -0600212
Marek Olšák588fa882011-02-09 01:10:11 +0100213 /* Unreference any user vertex buffers. */
José Fonseca0c501ba2011-04-07 12:19:14 +0100214 for (i = 0; i < st->num_user_attribs; i++) {
215 pipe_resource_reference(&st->user_attrib[i].buffer, NULL);
Marek Olšák588fa882011-02-09 01:10:11 +0100216 }
217
Michal Krolb8030c62010-03-12 14:37:36 +0100218 for (i = 0; i < Elements(st->state.sampler_views); i++) {
219 pipe_sampler_view_reference(&st->state.sampler_views[i], NULL);
Brian Paul87afc9b2008-05-16 16:04:30 -0600220 }
221
Brian Paul8f6d9e12008-08-14 15:38:09 -0600222 if (st->default_texture) {
223 st->ctx->Driver.DeleteTexture(st->ctx, st->default_texture);
224 st->default_texture = NULL;
225 }
226
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500227 free( st );
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100228}
229
230
Brian91564ee2007-11-05 16:15:43 -0700231void st_destroy_context( struct st_context *st )
232{
Brian Paula770d402008-04-24 12:11:24 -0600233 struct pipe_context *pipe = st->pipe;
234 struct cso_context *cso = st->cso_context;
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400235 struct gl_context *ctx = st->ctx;
Brian Paulf104e4d2009-05-11 16:09:39 -0600236 GLuint i;
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600237
238 /* need to unbind and destroy CSO objects before anything else */
Brian Paula770d402008-04-24 12:11:24 -0600239 cso_release_all(st->cso_context);
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600240
Brian Paul69fd6762008-05-20 14:38:22 -0600241 st_reference_fragprog(st, &st->fp, NULL);
242 st_reference_vertprog(st, &st->vp, NULL);
243
Brian Paulf104e4d2009-05-11 16:09:39 -0600244 /* release framebuffer surfaces */
245 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
246 pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
247 }
248 pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
249
Tilman Sauerbeck965c8a32010-10-31 15:55:51 +0100250 pipe->set_index_buffer(pipe, NULL);
251
Tilman Sauerbeck646a8b72010-11-02 22:15:15 +0100252 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
Jakob Bornecrantzd72cb9c2010-12-06 20:59:49 +0100253 pipe->set_constant_buffer(pipe, i, 0, NULL);
Tilman Sauerbeck646a8b72010-11-02 22:15:15 +0100254 }
255
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600256 _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
257
Brian Paul753635f2008-09-16 08:51:28 -0600258 _vbo_DestroyContext(st->ctx);
259
Brian Paul3d203b62010-12-13 17:20:12 -0700260 st_destroy_program_variants(st);
261
Brian91564ee2007-11-05 16:15:43 -0700262 _mesa_free_context_data(ctx);
Brian Paula770d402008-04-24 12:11:24 -0600263
Brian91564ee2007-11-05 16:15:43 -0700264 st_destroy_context_priv(st);
Brian Paula770d402008-04-24 12:11:24 -0600265
266 cso_destroy_context(cso);
267
268 pipe->destroy( pipe );
269
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500270 free(ctx);
Brian91564ee2007-11-05 16:15:43 -0700271}
272
273
Brian6da92342007-08-06 20:53:28 +0100274void st_init_driver_functions(struct dd_function_table *functions)
275{
Brian Paula37b2212010-06-10 20:23:03 -0600276 _mesa_init_shader_object_functions(functions);
Brian Paul34a5d3b2011-04-10 12:48:28 -0600277 _mesa_init_sampler_object_functions(functions);
Brian601a9ea2007-11-07 08:18:50 -0700278
Brian Paul122c6762011-12-03 10:04:18 -0700279 functions->Accum = _mesa_accum;
280
Brian0df877a2008-03-18 17:16:23 -0600281 st_init_blit_functions(functions);
Brianf6cd3772008-03-20 14:50:17 -0600282 st_init_bufferobject_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100283 st_init_clear_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600284 st_init_bitmap_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100285 st_init_drawpixels_functions(functions);
Brianbee148c2007-09-10 16:28:27 -0600286 st_init_rasterpos_functions(functions);
Chia-I Wu34064752009-09-22 15:00:24 +0800287
Chia-I Wu34064752009-09-22 15:00:24 +0800288 st_init_drawtex_functions(functions);
Chia-I Wu34064752009-09-22 15:00:24 +0800289
Chia-I Wua1aa53b2010-02-25 23:10:47 +0800290 st_init_eglimage_functions(functions);
291
Brian Paul04996bc2008-06-17 13:13:34 -0600292 st_init_fbo_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600293 st_init_feedback_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600294 st_init_program_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600295 st_init_query_functions(functions);
Brian Paulc9b55832009-12-31 14:47:21 -0700296 st_init_cond_render_functions(functions);
Brian6f27aab2007-08-10 11:37:21 -0600297 st_init_readpixels_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100298 st_init_texture_functions(functions);
Marek Olšákaea4ed42011-03-08 11:32:35 +0100299 st_init_texture_barrier_functions(functions);
Keith Whitwell5c2c0562007-08-10 12:57:14 +0100300 st_init_flush_functions(functions);
Keith Whitwell39407fd2007-08-10 16:42:26 +0100301 st_init_string_functions(functions);
Jakob Bornecrantza93f9f32010-05-21 20:37:23 +0100302 st_init_viewport_functions(functions);
Briand7755092007-10-31 11:08:07 -0600303
Brian Paulfef6e362010-05-10 21:11:21 -0600304 st_init_xformfb_functions(functions);
Marek Olšák5257a6d2011-03-05 20:32:28 +0100305 st_init_syncobj_functions(functions);
Brian Paulfef6e362010-05-10 21:11:21 -0600306
Briand7755092007-10-31 11:08:07 -0600307 functions->UpdateState = st_invalidate_state;
Brian6da92342007-08-06 20:53:28 +0100308}