blob: 2a1f21c51cad7b46d6a27ddbf191a27ea3ce792b [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"
Brian80d26582007-11-01 17:46:04 -060029#include "main/context.h"
Brian3bf8d2a2007-09-25 15:18:51 -060030#include "main/extensions.h"
Brianc6643022007-12-19 13:45:00 -070031#include "main/matrix.h"
32#include "main/buffers.h"
Brian Paul8cad3182008-06-09 14:56:29 -060033#include "main/scissor.h"
Brian Paul2c378512009-03-07 12:33:11 -070034#include "main/viewport.h"
Brian1e6d1ab2007-08-17 15:27:18 +010035#include "vbo/vbo.h"
Brian2d2bb352007-12-07 17:11:30 -070036#include "shader/shader_api.h"
Brian Paul5ebd8492008-06-17 14:27:36 -060037#include "glapi/glapi.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010038#include "st_public.h"
39#include "st_context.h"
Brian9f797d82007-10-14 11:52:00 -060040#include "st_cb_accum.h"
Brianf6cd3772008-03-20 14:50:17 -060041#include "st_cb_bitmap.h"
Brian0df877a2008-03-18 17:16:23 -060042#include "st_cb_blit.h"
Brianf6cd3772008-03-20 14:50:17 -060043#include "st_cb_bufferobjects.h"
Brian51b300c2007-08-02 10:29:50 -060044#include "st_cb_clear.h"
Brian Pauld7a4d492008-06-20 08:09:59 -060045#if FEATURE_drawpix
Brian184b6a12007-08-02 14:21:16 -060046#include "st_cb_drawpixels.h"
Brian Pauld7a4d492008-06-20 08:09:59 -060047#include "st_cb_rasterpos.h"
48#endif
Brian Paul4424c662008-08-12 18:02:44 -060049#ifdef FEATURE_OES_draw_texture
50#include "st_cb_drawtex.h"
51#endif
Brian64da7512007-08-09 12:27:44 -060052#include "st_cb_fbo.h"
Brian Paule90c0c92008-08-19 14:24:27 -060053#include "st_cb_get.h"
Brian Pauld7a4d492008-06-20 08:09:59 -060054#if FEATURE_feedback
Brianb4bacd12007-09-17 14:24:11 -060055#include "st_cb_feedback.h"
Brian Pauld7a4d492008-06-20 08:09:59 -060056#endif
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"
Keith Whitwell5c2c0562007-08-10 12:57:14 +010061#include "st_cb_flush.h"
Keith Whitwell39407fd2007-08-10 16:42:26 +010062#include "st_cb_strings.h"
Brian Paulf24ec992009-02-23 15:07:31 -070063#include "st_cb_viewport.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010064#include "st_atom.h"
65#include "st_draw.h"
Brianc6499a72007-11-05 18:04:30 -070066#include "st_extensions.h"
Brian62abcb92008-02-08 14:54:18 -070067#include "st_gen_mipmap.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010068#include "st_program.h"
Keith Whitwell943964a2007-06-14 18:23:43 +010069#include "pipe/p_context.h"
José Fonseca6acd63a2008-02-15 17:50:12 +090070#include "draw/draw_context.h"
71#include "cso_cache/cso_cache.h"
Brian339e7ec2008-03-11 18:54:31 -060072#include "cso_cache/cso_context.h"
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010073
Brian6f27aab2007-08-10 11:37:21 -060074
Briand7755092007-10-31 11:08:07 -060075/**
76 * Called via ctx->Driver.UpdateState()
77 */
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010078void st_invalidate_state(GLcontext * ctx, GLuint new_state)
79{
80 struct st_context *st = st_context(ctx);
81
82 st->dirty.mesa |= new_state;
83 st->dirty.st |= ST_NEW_MESA;
Briand7755092007-10-31 11:08:07 -060084
85 /* This is the only core Mesa module we depend upon.
86 * No longer use swrast, swsetup, tnl.
87 */
88 _vbo_InvalidateState(ctx, new_state);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +010089}
90
91
Brian Paul1a820f52008-09-17 14:32:33 -060092/**
93 * Check for multisample env var override.
94 */
95int
96st_get_msaa(void)
97{
98 const char *msaa = _mesa_getenv("__GL_FSAA_MODE");
99 if (msaa)
100 return atoi(msaa);
101 return 0;
102}
103
104
Brian91564ee2007-11-05 16:15:43 -0700105static struct st_context *
106st_create_context_priv( GLcontext *ctx, struct pipe_context *pipe )
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100107{
Brian339e7ec2008-03-11 18:54:31 -0600108 uint i;
Brian Paulf1a59a62009-02-12 10:14:36 -0700109 struct st_context *st = ST_CALLOC_STRUCT( st_context );
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100110
111 ctx->st = st;
112
113 st->ctx = ctx;
Keith Whitwell943964a2007-06-14 18:23:43 +0100114 st->pipe = pipe;
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100115
Brian8984a282007-10-31 11:35:50 -0600116 /* state tracker needs the VBO module */
117 _vbo_CreateContext(ctx);
Brian8984a282007-10-31 11:35:50 -0600118
Brian Paulbae90ed2008-07-10 14:23:27 -0600119#if FEATURE_feedback || FEATURE_drawpix
Brianb4bacd12007-09-17 14:24:11 -0600120 st->draw = draw_create(); /* for selection/feedback */
121
Brian Paul7f56c8e2008-03-25 14:12:55 -0600122 /* Disable draw options that might convert points/lines to tris, etc.
123 * as that would foul-up feedback/selection mode.
124 */
125 draw_wide_line_threshold(st->draw, 1000.0f);
126 draw_wide_point_threshold(st->draw, 1000.0f);
127 draw_enable_line_stipple(st->draw, FALSE);
128 draw_enable_point_sprites(st->draw, FALSE);
Brian Paulbae90ed2008-07-10 14:23:27 -0600129#endif
Brian Paul7f56c8e2008-03-25 14:12:55 -0600130
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100131 st->dirty.mesa = ~0;
132 st->dirty.st = ~0;
133
Brian339e7ec2008-03-11 18:54:31 -0600134 st->cso_context = cso_create_context(pipe);
Zack Rusin97803272007-09-14 04:08:58 -0400135
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100136 st_init_atoms( st );
Brianc62b1972008-03-28 14:53:47 -0600137 st_init_bitmap(st);
Brian Paulce5c8672008-04-03 12:54:32 -0600138 st_init_clear(st);
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100139 st_init_draw( st );
Brian62abcb92008-02-08 14:54:18 -0700140 st_init_generate_mipmap(st);
Brian0df877a2008-03-18 17:16:23 -0600141 st_init_blit(st);
Brian61d02152007-08-02 20:40:19 -0600142
Brian339e7ec2008-03-11 18:54:31 -0600143 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
144 st->state.sampler_list[i] = &st->state.samplers[i];
145
Brian1e6d1ab2007-08-17 15:27:18 +0100146 /* we want all vertex data to be placed in buffer objects */
147 vbo_use_buffer_objects(ctx);
148
Brian6da92342007-08-06 20:53:28 +0100149 /* Need these flags:
150 */
151 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
Brian6da92342007-08-06 20:53:28 +0100152
Briane3bdd662007-08-16 17:33:49 -0600153 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
154
Briand6a739f2007-10-30 16:13:37 -0600155 st->pixel_xfer.cache = _mesa_new_program_cache();
Briandf174bd2007-10-30 12:24:05 -0600156
Brian Paul1a820f52008-09-17 14:32:33 -0600157 st->force_msaa = st_get_msaa();
158
Brianc6499a72007-11-05 18:04:30 -0700159 /* GL limits and extensions */
160 st_init_limits(st);
161 st_init_extensions(st);
Brianaf3d6c82007-08-21 20:15:00 -0600162
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100163 return st;
164}
165
166
Brian91564ee2007-11-05 16:15:43 -0700167struct st_context *st_create_context(struct pipe_context *pipe,
168 const __GLcontextModes *visual,
169 struct st_context *share)
Brian80d26582007-11-01 17:46:04 -0600170{
Brian91564ee2007-11-05 16:15:43 -0700171 GLcontext *ctx;
172 GLcontext *shareCtx = share ? share->ctx : NULL;
173 struct dd_function_table funcs;
174
175 memset(&funcs, 0, sizeof(funcs));
176 st_init_driver_functions(&funcs);
177
178 ctx = _mesa_create_context(visual, shareCtx, &funcs, NULL);
179
Keith Whitwellb6e82562009-05-05 12:12:28 +0100180 /* XXX: need a capability bit in gallium to query if the pipe
181 * driver prefers DP4 or MUL/MAD for vertex transformation.
182 */
183 if (debug_get_bool_option("MESA_MVP_DP4", FALSE))
184 _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
185
Brian91564ee2007-11-05 16:15:43 -0700186 return st_create_context_priv(ctx, pipe);
Brian80d26582007-11-01 17:46:04 -0600187}
188
189
Brian91564ee2007-11-05 16:15:43 -0700190static void st_destroy_context_priv( struct st_context *st )
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100191{
Brian6128c932007-12-26 07:16:12 -0700192 uint i;
193
Brian Paulbae90ed2008-07-10 14:23:27 -0600194#if FEATURE_feedback || FEATURE_drawpix
Brianb4bacd12007-09-17 14:24:11 -0600195 draw_destroy(st->draw);
Brian Paulbae90ed2008-07-10 14:23:27 -0600196#endif
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100197 st_destroy_atoms( st );
198 st_destroy_draw( st );
Brian0df877a2008-03-18 17:16:23 -0600199 st_destroy_generate_mipmap(st);
Brian Paul414b5352008-06-17 16:57:02 -0600200#if FEATURE_EXT_framebuffer_blit
Brian0df877a2008-03-18 17:16:23 -0600201 st_destroy_blit(st);
Brian Paul414b5352008-06-17 16:57:02 -0600202#endif
Brian85e4ec62008-03-20 09:13:51 -0600203 st_destroy_clear(st);
Brian Paul414b5352008-06-17 16:57:02 -0600204#if FEATURE_drawpix
205 st_destroy_bitmap(st);
Brian Paul90ce8462008-05-20 13:38:45 -0600206 st_destroy_drawpix(st);
Brian Paul414b5352008-06-17 16:57:02 -0600207#endif
Brian Paul4424c662008-08-12 18:02:44 -0600208#ifdef FEATURE_OES_draw_texture
209 st_destroy_drawtex(st);
210#endif
Brian61d02152007-08-02 20:40:19 -0600211
Brian Paul87afc9b2008-05-16 16:04:30 -0600212 for (i = 0; i < Elements(st->state.sampler_texture); i++) {
213 pipe_texture_reference(&st->state.sampler_texture[i], NULL);
214 }
215
Brian6128c932007-12-26 07:16:12 -0700216 for (i = 0; i < Elements(st->state.constants); i++) {
217 if (st->state.constants[i].buffer) {
Michel Dänzer5e27cd42009-03-04 11:58:48 +0100218 pipe_buffer_reference(&st->state.constants[i].buffer, NULL);
Brian6128c932007-12-26 07:16:12 -0700219 }
220 }
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
Brian Paul1a2f4dd2009-02-12 10:08:25 -0700227 _mesa_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;
Brian91564ee2007-11-05 16:15:43 -0700235 GLcontext *ctx = st->ctx;
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600236
237 /* need to unbind and destroy CSO objects before anything else */
Brian Paula770d402008-04-24 12:11:24 -0600238 cso_release_all(st->cso_context);
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600239
Brian Paul69fd6762008-05-20 14:38:22 -0600240 st_reference_fragprog(st, &st->fp, NULL);
241 st_reference_vertprog(st, &st->vp, NULL);
242
Brian Paulf2b3f6c2008-04-17 11:12:44 -0600243 _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
244
Brian Paul753635f2008-09-16 08:51:28 -0600245 _vbo_DestroyContext(st->ctx);
246
Brian91564ee2007-11-05 16:15:43 -0700247 _mesa_free_context_data(ctx);
Brian Paula770d402008-04-24 12:11:24 -0600248
Brian91564ee2007-11-05 16:15:43 -0700249 st_destroy_context_priv(st);
Brian Paula770d402008-04-24 12:11:24 -0600250
251 cso_destroy_context(cso);
252
253 pipe->destroy( pipe );
254
Brian Paul1a2f4dd2009-02-12 10:08:25 -0700255 _mesa_free(ctx);
Brian91564ee2007-11-05 16:15:43 -0700256}
257
258
Brian794e03d2007-11-02 13:22:22 -0600259void st_make_current(struct st_context *st,
260 struct st_framebuffer *draw,
261 struct st_framebuffer *read)
262{
Keith Whitwell08d44512009-03-18 19:14:45 +0000263 /* Call this periodically to detect when the user has begun using
264 * GL rendering from multiple threads.
265 */
266 _glapi_check_multithread();
267
Brian794e03d2007-11-02 13:22:22 -0600268 if (st) {
Brianc6643022007-12-19 13:45:00 -0700269 GLboolean firstTime = st->ctx->FirstTimeCurrent;
Brian794e03d2007-11-02 13:22:22 -0600270 _mesa_make_current(st->ctx, &draw->Base, &read->Base);
Brianc6643022007-12-19 13:45:00 -0700271 /* Need to initialize viewport here since draw->Base->Width/Height
272 * will still be zero at this point.
273 * This could be improved, but would require rather extensive work
274 * elsewhere (allocate rb surface storage sooner)
275 */
276 if (firstTime) {
277 GLuint w = draw->InitWidth, h = draw->InitHeight;
278 _mesa_set_viewport(st->ctx, 0, 0, w, h);
279 _mesa_set_scissor(st->ctx, 0, 0, w, h);
280
281 }
Brian794e03d2007-11-02 13:22:22 -0600282 }
283 else {
284 _mesa_make_current(NULL, NULL, NULL);
285 }
286}
287
Thomas Hellstrom3d2bba02009-04-28 14:12:39 +0200288struct st_context *st_get_current(void)
289{
290 GET_CURRENT_CONTEXT(ctx);
291
292 return (ctx == NULL) ? NULL : ctx->st;
293}
Brian794e03d2007-11-02 13:22:22 -0600294
Briane39f1b42007-11-05 15:59:55 -0700295void st_copy_context_state(struct st_context *dst,
296 struct st_context *src,
297 uint mask)
298{
299 _mesa_copy_context(dst->ctx, src->ctx, mask);
300}
301
Keith Whitwell8e4a95a2007-05-24 10:41:34 +0100302
Brian Paul5ebd8492008-06-17 14:27:36 -0600303
304st_proc st_get_proc_address(const char *procname)
305{
306 return (st_proc) _glapi_get_proc_address(procname);
307}
308
309
310
Brian6da92342007-08-06 20:53:28 +0100311void st_init_driver_functions(struct dd_function_table *functions)
312{
Brian601a9ea2007-11-07 08:18:50 -0700313 _mesa_init_glsl_driver_functions(functions);
314
Brian Paul04996bc2008-06-17 13:13:34 -0600315#if FEATURE_accum
Brian9f797d82007-10-14 11:52:00 -0600316 st_init_accum_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600317#endif
318#if FEATURE_EXT_framebuffer_blit
Brian0df877a2008-03-18 17:16:23 -0600319 st_init_blit_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600320#endif
Brianf6cd3772008-03-20 14:50:17 -0600321 st_init_bufferobject_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100322 st_init_clear_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600323#if FEATURE_drawpix
324 st_init_bitmap_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100325 st_init_drawpixels_functions(functions);
Brianbee148c2007-09-10 16:28:27 -0600326 st_init_rasterpos_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600327#endif
328 st_init_fbo_functions(functions);
Brian Paule90c0c92008-08-19 14:24:27 -0600329 st_init_get_functions(functions);
Brian Paul04996bc2008-06-17 13:13:34 -0600330#if FEATURE_feedback
331 st_init_feedback_functions(functions);
332#endif
333 st_init_program_functions(functions);
334#if FEATURE_ARB_occlusion_query
335 st_init_query_functions(functions);
336#endif
Brian6f27aab2007-08-10 11:37:21 -0600337 st_init_readpixels_functions(functions);
Brian6da92342007-08-06 20:53:28 +0100338 st_init_texture_functions(functions);
Keith Whitwell5c2c0562007-08-10 12:57:14 +0100339 st_init_flush_functions(functions);
Keith Whitwell39407fd2007-08-10 16:42:26 +0100340 st_init_string_functions(functions);
Alan Hourihane6b722272009-02-19 18:33:15 +0000341 st_init_viewport_functions(functions);
Briand7755092007-10-31 11:08:07 -0600342
343 functions->UpdateState = st_invalidate_state;
Brian6da92342007-08-06 20:53:28 +0100344}