st_context.c revision 4a49301e
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 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 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 28#include "main/imports.h" 29#include "main/context.h" 30#include "vbo/vbo.h" 31#include "shader/shader_api.h" 32#include "glapi/glapi.h" 33#include "st_public.h" 34#include "st_debug.h" 35#include "st_context.h" 36#include "st_cb_accum.h" 37#include "st_cb_bitmap.h" 38#include "st_cb_blit.h" 39#include "st_cb_bufferobjects.h" 40#include "st_cb_clear.h" 41#if FEATURE_drawpix 42#include "st_cb_drawpixels.h" 43#include "st_cb_rasterpos.h" 44#endif 45#ifdef FEATURE_OES_draw_texture 46#include "st_cb_drawtex.h" 47#endif 48#include "st_cb_fbo.h" 49#if FEATURE_feedback 50#include "st_cb_feedback.h" 51#endif 52#include "st_cb_program.h" 53#include "st_cb_queryobj.h" 54#include "st_cb_readpixels.h" 55#include "st_cb_texture.h" 56#include "st_cb_flush.h" 57#include "st_cb_strings.h" 58#include "st_cb_viewport.h" 59#include "st_atom.h" 60#include "st_draw.h" 61#include "st_extensions.h" 62#include "st_gen_mipmap.h" 63#include "st_program.h" 64#include "pipe/p_context.h" 65#include "draw/draw_context.h" 66#include "cso_cache/cso_context.h" 67 68 69/** 70 * Called via ctx->Driver.UpdateState() 71 */ 72void st_invalidate_state(GLcontext * ctx, GLuint new_state) 73{ 74 struct st_context *st = st_context(ctx); 75 76 st->dirty.mesa |= new_state; 77 st->dirty.st |= ST_NEW_MESA; 78 79 /* This is the only core Mesa module we depend upon. 80 * No longer use swrast, swsetup, tnl. 81 */ 82 _vbo_InvalidateState(ctx, new_state); 83} 84 85 86/** 87 * Check for multisample env var override. 88 */ 89int 90st_get_msaa(void) 91{ 92 const char *msaa = _mesa_getenv("__GL_FSAA_MODE"); 93 if (msaa) 94 return atoi(msaa); 95 return 0; 96} 97 98 99static struct st_context * 100st_create_context_priv( GLcontext *ctx, struct pipe_context *pipe ) 101{ 102 uint i; 103 struct st_context *st = ST_CALLOC_STRUCT( st_context ); 104 105 ctx->st = st; 106 107 st->ctx = ctx; 108 st->pipe = pipe; 109 110 /* XXX: this is one-off, per-screen init: */ 111 st_debug_init(); 112 113 /* state tracker needs the VBO module */ 114 _vbo_CreateContext(ctx); 115 116#if FEATURE_feedback || FEATURE_drawpix 117 st->draw = draw_create(); /* for selection/feedback */ 118 119 /* Disable draw options that might convert points/lines to tris, etc. 120 * as that would foul-up feedback/selection mode. 121 */ 122 draw_wide_line_threshold(st->draw, 1000.0f); 123 draw_wide_point_threshold(st->draw, 1000.0f); 124 draw_enable_line_stipple(st->draw, FALSE); 125 draw_enable_point_sprites(st->draw, FALSE); 126#endif 127 128 st->dirty.mesa = ~0; 129 st->dirty.st = ~0; 130 131 st->cso_context = cso_create_context(pipe); 132 133 st_init_atoms( st ); 134 st_init_bitmap(st); 135 st_init_clear(st); 136 st_init_draw( st ); 137 st_init_generate_mipmap(st); 138 st_init_blit(st); 139 140 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) 141 st->state.sampler_list[i] = &st->state.samplers[i]; 142 143 /* we want all vertex data to be placed in buffer objects */ 144 vbo_use_buffer_objects(ctx); 145 146 /* Need these flags: 147 */ 148 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE; 149 150 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE; 151 152 st->pixel_xfer.cache = _mesa_new_program_cache(); 153 154 st->force_msaa = st_get_msaa(); 155 156 /* GL limits and extensions */ 157 st_init_limits(st); 158 st_init_extensions(st); 159 160 return st; 161} 162 163 164struct st_context *st_create_context(struct pipe_context *pipe, 165 const __GLcontextModes *visual, 166 struct st_context *share) 167{ 168 GLcontext *ctx; 169 GLcontext *shareCtx = share ? share->ctx : NULL; 170 struct dd_function_table funcs; 171 172 memset(&funcs, 0, sizeof(funcs)); 173 st_init_driver_functions(&funcs); 174 175 ctx = _mesa_create_context(visual, shareCtx, &funcs, NULL); 176 177 /* XXX: need a capability bit in gallium to query if the pipe 178 * driver prefers DP4 or MUL/MAD for vertex transformation. 179 */ 180 if (debug_get_bool_option("MESA_MVP_DP4", FALSE)) 181 _mesa_set_mvp_with_dp4( ctx, GL_TRUE ); 182 183 return st_create_context_priv(ctx, pipe); 184} 185 186 187static void st_destroy_context_priv( struct st_context *st ) 188{ 189 uint i; 190 191#if FEATURE_feedback || FEATURE_drawpix 192 draw_destroy(st->draw); 193#endif 194 st_destroy_atoms( st ); 195 st_destroy_draw( st ); 196 st_destroy_generate_mipmap(st); 197#if FEATURE_EXT_framebuffer_blit 198 st_destroy_blit(st); 199#endif 200 st_destroy_clear(st); 201#if FEATURE_drawpix 202 st_destroy_bitmap(st); 203 st_destroy_drawpix(st); 204#endif 205#ifdef FEATURE_OES_draw_texture 206 st_destroy_drawtex(st); 207#endif 208 209 for (i = 0; i < Elements(st->state.sampler_texture); i++) { 210 pipe_texture_reference(&st->state.sampler_texture[i], NULL); 211 } 212 213 for (i = 0; i < Elements(st->state.constants); i++) { 214 if (st->state.constants[i].buffer) { 215 pipe_buffer_reference(&st->state.constants[i].buffer, NULL); 216 } 217 } 218 219 if (st->default_texture) { 220 st->ctx->Driver.DeleteTexture(st->ctx, st->default_texture); 221 st->default_texture = NULL; 222 } 223 224 _mesa_free( st ); 225} 226 227 228void st_destroy_context( struct st_context *st ) 229{ 230 struct pipe_context *pipe = st->pipe; 231 struct cso_context *cso = st->cso_context; 232 GLcontext *ctx = st->ctx; 233 GLuint i; 234 235 /* need to unbind and destroy CSO objects before anything else */ 236 cso_release_all(st->cso_context); 237 238 st_reference_fragprog(st, &st->fp, NULL); 239 st_reference_vertprog(st, &st->vp, NULL); 240 241 /* release framebuffer surfaces */ 242 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) { 243 pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL); 244 } 245 pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL); 246 247 _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache); 248 249 _vbo_DestroyContext(st->ctx); 250 251 _mesa_free_context_data(ctx); 252 253 st_destroy_context_priv(st); 254 255 cso_destroy_context(cso); 256 257 pipe->destroy( pipe ); 258 259 _mesa_free(ctx); 260} 261 262 263GLboolean 264st_make_current(struct st_context *st, 265 struct st_framebuffer *draw, 266 struct st_framebuffer *read) 267{ 268 /* Call this periodically to detect when the user has begun using 269 * GL rendering from multiple threads. 270 */ 271 _glapi_check_multithread(); 272 273 if (st) { 274 if (!_mesa_make_current(st->ctx, &draw->Base, &read->Base)) 275 return GL_FALSE; 276 277 _mesa_check_init_viewport(st->ctx, draw->InitWidth, draw->InitHeight); 278 279 return GL_TRUE; 280 } 281 else { 282 return _mesa_make_current(NULL, NULL, NULL); 283 } 284} 285 286struct st_context *st_get_current(void) 287{ 288 GET_CURRENT_CONTEXT(ctx); 289 290 return (ctx == NULL) ? NULL : ctx->st; 291} 292 293void st_copy_context_state(struct st_context *dst, 294 struct st_context *src, 295 uint mask) 296{ 297 _mesa_copy_context(dst->ctx, src->ctx, mask); 298} 299 300 301 302st_proc st_get_proc_address(const char *procname) 303{ 304 return (st_proc) _glapi_get_proc_address(procname); 305} 306 307 308 309void st_init_driver_functions(struct dd_function_table *functions) 310{ 311 _mesa_init_glsl_driver_functions(functions); 312 313#if FEATURE_accum 314 st_init_accum_functions(functions); 315#endif 316#if FEATURE_EXT_framebuffer_blit 317 st_init_blit_functions(functions); 318#endif 319 st_init_bufferobject_functions(functions); 320 st_init_clear_functions(functions); 321#if FEATURE_drawpix 322 st_init_bitmap_functions(functions); 323 st_init_drawpixels_functions(functions); 324 st_init_rasterpos_functions(functions); 325#endif 326 st_init_fbo_functions(functions); 327#if FEATURE_feedback 328 st_init_feedback_functions(functions); 329#endif 330 st_init_program_functions(functions); 331#if FEATURE_queryobj 332 st_init_query_functions(functions); 333#endif 334 st_init_readpixels_functions(functions); 335 st_init_texture_functions(functions); 336 st_init_flush_functions(functions); 337 st_init_string_functions(functions); 338 st_init_viewport_functions(functions); 339 340 functions->UpdateState = st_invalidate_state; 341} 342