blob: 0245fd92b3eb32b18f158e13e88fb383e397e8ef [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"
Marek Olšák76d9a992012-04-13 19:27:45 +020066#include "util/u_upload_mgr.h"
Brian339e7ec2008-03-11 18:54:31 -060067#include "cso_cache/cso_context.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010068
Brian6f27aab2007-08-10 11:37:21 -060069
Brian Paul6b97fe52010-08-06 15:09:51 -060070DEBUG_GET_ONCE_BOOL_OPTION(mesa_mvp_dp4, "MESA_MVP_DP4", FALSE)
Jakob Bornecrantz99611f02010-08-05 17:13:41 -070071
72
Briand7755092007-10-31 11:08:07 -060073/**
74 * Called via ctx->Driver.UpdateState()
75 */
Kristian Høgsbergf9995b32010-10-12 12:26:10 -040076void st_invalidate_state(struct gl_context * ctx, GLuint new_state)
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010077{
78 struct st_context *st = st_context(ctx);
79
Marek Olšákbc1c8362012-01-23 03:11:17 +010080 /* Replace _NEW_FRAG_CLAMP with ST_NEW_FRAGMENT_PROGRAM for the fallback. */
81 if (st->clamp_frag_color_in_shader && (new_state & _NEW_FRAG_CLAMP)) {
82 new_state &= ~_NEW_FRAG_CLAMP;
83 st->dirty.st |= ST_NEW_FRAGMENT_PROGRAM;
84 }
85
86 /* Update the vertex shader if ctx->Light._ClampVertexColor was changed. */
87 if (st->clamp_vert_color_in_shader && (new_state & _NEW_LIGHT)) {
88 st->dirty.st |= ST_NEW_VERTEX_PROGRAM;
89 }
90
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010091 st->dirty.mesa |= new_state;
92 st->dirty.st |= ST_NEW_MESA;
Briand7755092007-10-31 11:08:07 -060093
94 /* This is the only core Mesa module we depend upon.
95 * No longer use swrast, swsetup, tnl.
96 */
97 _vbo_InvalidateState(ctx, new_state);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010098}
99
100
Brian Paul1a820f52008-09-17 14:32:33 -0600101/**
102 * Check for multisample env var override.
103 */
104int
105st_get_msaa(void)
106{
107 const char *msaa = _mesa_getenv("__GL_FSAA_MODE");
108 if (msaa)
109 return atoi(msaa);
110 return 0;
111}
112
113
Brian91564ee2007-11-05 16:15:43 -0700114static struct st_context *
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400115st_create_context_priv( struct gl_context *ctx, struct pipe_context *pipe )
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100116{
Brian339e7ec2008-03-11 18:54:31 -0600117 uint i;
Brian Paulf1a59a62009-02-12 10:14:36 -0700118 struct st_context *st = ST_CALLOC_STRUCT( st_context );
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100119
120 ctx->st = st;
121
122 st->ctx = ctx;
Keith Whitwell943964a2007-06-14 18:23:43 +0100123 st->pipe = pipe;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100124
Keith Whitwellb02ef742009-10-05 15:50:11 +0100125 /* XXX: this is one-off, per-screen init: */
126 st_debug_init();
127
Brian8984a282007-10-31 11:35:50 -0600128 /* state tracker needs the VBO module */
129 _vbo_CreateContext(ctx);
Brian8984a282007-10-31 11:35:50 -0600130
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100131 st->dirty.mesa = ~0;
132 st->dirty.st = ~0;
133
Marek Olšák76d9a992012-04-13 19:27:45 +0200134 st->uploader = u_upload_create(st->pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER);
Brian339e7ec2008-03-11 18:54:31 -0600135 st->cso_context = cso_create_context(pipe);
Zack Rusin97803272007-09-14 04:08:58 -0400136
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100137 st_init_atoms( st );
Brianc62b1972008-03-28 14:53:47 -0600138 st_init_bitmap(st);
Brian Paulce5c8672008-04-03 12:54:32 -0600139 st_init_clear(st);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100140 st_init_draw( st );
Brian62abcb92008-02-08 14:54:18 -0700141 st_init_generate_mipmap(st);
Brian0df877a2008-03-18 17:16:23 -0600142 st_init_blit(st);
Brian61d02152007-08-02 20:40:19 -0600143
Luca Barbieri3070e0e2010-08-06 07:39:21 +0200144 if(pipe->screen->get_param(pipe->screen, PIPE_CAP_NPOT_TEXTURES))
145 st->internal_target = PIPE_TEXTURE_2D;
146 else
147 st->internal_target = PIPE_TEXTURE_RECT;
148
Roland Scheideggerae7b7bf2010-03-09 15:09:01 +0100149 for (i = 0; i < 3; i++) {
150 memset(&st->velems_util_draw[i], 0, sizeof(struct pipe_vertex_element));
151 st->velems_util_draw[i].src_offset = i * 4 * sizeof(float);
152 st->velems_util_draw[i].instance_divisor = 0;
153 st->velems_util_draw[i].vertex_buffer_index = 0;
154 st->velems_util_draw[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
155 }
156
Brian1e6d1ab2007-08-17 15:27:18 +0100157 /* we want all vertex data to be placed in buffer objects */
158 vbo_use_buffer_objects(ctx);
159
Brian Paul2997b452011-10-25 17:41:12 -0600160
161 /* make sure that no VBOs are left mapped when we're drawing. */
162 vbo_always_unmap_buffers(ctx);
163
Brian6da92342007-08-06 20:53:28 +0100164 /* Need these flags:
165 */
166 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
Brian6da92342007-08-06 20:53:28 +0100167
Briane3bdd662007-08-16 17:33:49 -0600168 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
169
Briand6a739f2007-10-30 16:13:37 -0600170 st->pixel_xfer.cache = _mesa_new_program_cache();
Briandf174bd2007-10-30 12:24:05 -0600171
Brian Paul1a820f52008-09-17 14:32:33 -0600172 st->force_msaa = st_get_msaa();
173
Brianc6499a72007-11-05 18:04:30 -0700174 /* GL limits and extensions */
175 st_init_limits(st);
176 st_init_extensions(st);
Brianaf3d6c82007-08-21 20:15:00 -0600177
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100178 return st;
179}
180
181
Chia-I Wu57c65432010-06-23 17:40:49 +0800182struct st_context *st_create_context(gl_api api, struct pipe_context *pipe,
Kristian Høgsbergd3491e72010-10-12 11:58:47 -0400183 const struct gl_config *visual,
Brian91564ee2007-11-05 16:15:43 -0700184 struct st_context *share)
Brian80d26582007-11-01 17:46:04 -0600185{
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400186 struct gl_context *ctx;
187 struct gl_context *shareCtx = share ? share->ctx : NULL;
Brian91564ee2007-11-05 16:15:43 -0700188 struct dd_function_table funcs;
189
Brian Paulc628fd72010-11-23 10:28:43 -0700190 /* Sanity checks */
191 assert(MESA_SHADER_VERTEX == PIPE_SHADER_VERTEX);
192 assert(MESA_SHADER_FRAGMENT == PIPE_SHADER_FRAGMENT);
193 assert(MESA_SHADER_GEOMETRY == PIPE_SHADER_GEOMETRY);
194
Brian91564ee2007-11-05 16:15:43 -0700195 memset(&funcs, 0, sizeof(funcs));
196 st_init_driver_functions(&funcs);
197
Brian Paul6f2f4492011-02-08 19:25:04 -0700198 ctx = _mesa_create_context(api, visual, shareCtx, &funcs, NULL);
Emil Velikov1ab5e152011-07-11 15:42:15 +0100199 if (!ctx) {
200 return NULL;
201 }
Brian91564ee2007-11-05 16:15:43 -0700202
Keith Whitwellb6e82562009-05-05 12:12:28 +0100203 /* XXX: need a capability bit in gallium to query if the pipe
204 * driver prefers DP4 or MUL/MAD for vertex transformation.
205 */
Jakob Bornecrantz99611f02010-08-05 17:13:41 -0700206 if (debug_get_option_mesa_mvp_dp4())
Keith Whitwellb6e82562009-05-05 12:12:28 +0100207 _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
208
Brian91564ee2007-11-05 16:15:43 -0700209 return st_create_context_priv(ctx, pipe);
Brian80d26582007-11-01 17:46:04 -0600210}
211
212
Brian91564ee2007-11-05 16:15:43 -0700213static void st_destroy_context_priv( struct st_context *st )
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100214{
Brian6128c932007-12-26 07:16:12 -0700215 uint i;
216
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100217 st_destroy_atoms( st );
218 st_destroy_draw( st );
Brian0df877a2008-03-18 17:16:23 -0600219 st_destroy_generate_mipmap(st);
220 st_destroy_blit(st);
Brian85e4ec62008-03-20 09:13:51 -0600221 st_destroy_clear(st);
Brian Paul414b5352008-06-17 16:57:02 -0600222 st_destroy_bitmap(st);
Brian Paul90ce8462008-05-20 13:38:45 -0600223 st_destroy_drawpix(st);
Brian Paul4424c662008-08-12 18:02:44 -0600224 st_destroy_drawtex(st);
Brian61d02152007-08-02 20:40:19 -0600225
Marek Olšák588fa882011-02-09 01:10:11 +0100226 /* Unreference any user vertex buffers. */
José Fonseca0c501ba2011-04-07 12:19:14 +0100227 for (i = 0; i < st->num_user_attribs; i++) {
228 pipe_resource_reference(&st->user_attrib[i].buffer, NULL);
Marek Olšák588fa882011-02-09 01:10:11 +0100229 }
230
Michal Krolb8030c62010-03-12 14:37:36 +0100231 for (i = 0; i < Elements(st->state.sampler_views); i++) {
232 pipe_sampler_view_reference(&st->state.sampler_views[i], NULL);
Brian Paul87afc9b2008-05-16 16:04:30 -0600233 }
234
Brian Paul8f6d9e12008-08-14 15:38:09 -0600235 if (st->default_texture) {
236 st->ctx->Driver.DeleteTexture(st->ctx, st->default_texture);
237 st->default_texture = NULL;
238 }
239
Marek Olšák76d9a992012-04-13 19:27:45 +0200240 u_upload_destroy(st->uploader);
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500241 free( st );
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100242}
243
244
Brian91564ee2007-11-05 16:15:43 -0700245void st_destroy_context( struct st_context *st )
246{
Brian Paula770d402008-04-24 12:11:24 -0600247 struct pipe_context *pipe = st->pipe;
248 struct cso_context *cso = st->cso_context;
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400249 struct gl_context *ctx = st->ctx;
Brian Paulf104e4d2009-05-11 16:09:39 -0600250 GLuint i;
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600251
252 /* need to unbind and destroy CSO objects before anything else */
Brian Paula770d402008-04-24 12:11:24 -0600253 cso_release_all(st->cso_context);
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600254
Brian Paul69fd6762008-05-20 14:38:22 -0600255 st_reference_fragprog(st, &st->fp, NULL);
256 st_reference_vertprog(st, &st->vp, NULL);
257
Brian Paulf104e4d2009-05-11 16:09:39 -0600258 /* release framebuffer surfaces */
259 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
260 pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
261 }
262 pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
263
Tilman Sauerbeck965c8a32010-10-31 15:55:51 +0100264 pipe->set_index_buffer(pipe, NULL);
265
Tilman Sauerbeck646a8b72010-11-02 22:15:15 +0100266 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
Jakob Bornecrantzd72cb9c2010-12-06 20:59:49 +0100267 pipe->set_constant_buffer(pipe, i, 0, NULL);
Tilman Sauerbeck646a8b72010-11-02 22:15:15 +0100268 }
269
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600270 _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
271
Brian Paul753635f2008-09-16 08:51:28 -0600272 _vbo_DestroyContext(st->ctx);
273
Brian Paul3d203b62010-12-13 17:20:12 -0700274 st_destroy_program_variants(st);
275
Brian91564ee2007-11-05 16:15:43 -0700276 _mesa_free_context_data(ctx);
Brian Paula770d402008-04-24 12:11:24 -0600277
Brian91564ee2007-11-05 16:15:43 -0700278 st_destroy_context_priv(st);
Brian Paula770d402008-04-24 12:11:24 -0600279
280 cso_destroy_context(cso);
281
282 pipe->destroy( pipe );
283
Kristian Høgsberg32f2fd12010-02-19 11:58:49 -0500284 free(ctx);
Brian91564ee2007-11-05 16:15:43 -0700285}
286
287
Brian6da92342007-08-06 20:53:28 +0100288void st_init_driver_functions(struct dd_function_table *functions)
289{
Brian Paula37b2212010-06-10 20:23:03 -0600290 _mesa_init_shader_object_functions(functions);
Brian Paul34a5d3b2011-04-10 12:48:28 -0600291 _mesa_init_sampler_object_functions(functions);
Brian601a9ea2007-11-07 08:18:50 -0700292
Brian Paul122c6762011-12-03 10:04:18 -0700293 functions->Accum = _mesa_accum;
294
Brian0df877a2008-03-18 17:16:23 -0600295 st_init_blit_functions(functions);
Brianf6cd3772008-03-20 14:50:17 -0600296 st_init_bufferobject_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100297 st_init_clear_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600298 st_init_bitmap_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100299 st_init_drawpixels_functions(functions);
Brianbee148c2007-09-10 16:28:27 -0600300 st_init_rasterpos_functions(functions);
Chia-I Wu34064752009-09-22 15:00:24 +0800301
Chia-I Wu34064752009-09-22 15:00:24 +0800302 st_init_drawtex_functions(functions);
Chia-I Wu34064752009-09-22 15:00:24 +0800303
Chia-I Wua1aa53b2010-02-25 23:10:47 +0800304 st_init_eglimage_functions(functions);
305
Brian Paul04996bc2008-06-17 13:13:34 -0600306 st_init_fbo_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600307 st_init_feedback_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600308 st_init_program_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600309 st_init_query_functions(functions);
Brian Paulc9b55832009-12-31 14:47:21 -0700310 st_init_cond_render_functions(functions);
Brian6f27aab2007-08-10 11:37:21 -0600311 st_init_readpixels_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100312 st_init_texture_functions(functions);
Marek Olšákaea4ed42011-03-08 11:32:35 +0100313 st_init_texture_barrier_functions(functions);
Keith Whitwell5c2c0562007-08-10 12:57:14 +0100314 st_init_flush_functions(functions);
Keith Whitwell39407fd2007-08-10 16:42:26 +0100315 st_init_string_functions(functions);
Jakob Bornecrantza93f9f32010-05-21 20:37:23 +0100316 st_init_viewport_functions(functions);
Briand7755092007-10-31 11:08:07 -0600317
Brian Paulfef6e362010-05-10 21:11:21 -0600318 st_init_xformfb_functions(functions);
Marek Olšák5257a6d2011-03-05 20:32:28 +0100319 st_init_syncobj_functions(functions);
Brian Paulfef6e362010-05-10 21:11:21 -0600320
Briand7755092007-10-31 11:08:07 -0600321 functions->UpdateState = st_invalidate_state;
Brian6da92342007-08-06 20:53:28 +0100322}