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