Merge branch 'mesa_7_6_branch'
[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_context.h"
40 #include "st_cb_accum.h"
41 #include "st_cb_bitmap.h"
42 #include "st_cb_blit.h"
43 #include "st_cb_bufferobjects.h"
44 #include "st_cb_clear.h"
45 #if FEATURE_drawpix
46 #include "st_cb_drawpixels.h"
47 #include "st_cb_rasterpos.h"
48 #endif
49 #ifdef FEATURE_OES_draw_texture
50 #include "st_cb_drawtex.h"
51 #endif
52 #include "st_cb_fbo.h"
53 #include "st_cb_get.h"
54 #if FEATURE_feedback
55 #include "st_cb_feedback.h"
56 #endif
57 #include "st_cb_program.h"
58 #include "st_cb_queryobj.h"
59 #include "st_cb_readpixels.h"
60 #include "st_cb_texture.h"
61 #include "st_cb_flush.h"
62 #include "st_cb_strings.h"
63 #include "st_cb_viewport.h"
64 #include "st_atom.h"
65 #include "st_draw.h"
66 #include "st_extensions.h"
67 #include "st_gen_mipmap.h"
68 #include "st_program.h"
69 #include "pipe/p_context.h"
70 #include "draw/draw_context.h"
71 #include "cso_cache/cso_cache.h"
72 #include "cso_cache/cso_context.h"
73
74
75 /**
76 * Called via ctx->Driver.UpdateState()
77 */
78 void 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;
84
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);
89 }
90
91
92 /**
93 * Check for multisample env var override.
94 */
95 int
96 st_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
105 static struct st_context *
106 st_create_context_priv( GLcontext *ctx, struct pipe_context *pipe )
107 {
108 uint i;
109 struct st_context *st = ST_CALLOC_STRUCT( st_context );
110
111 ctx->st = st;
112
113 st->ctx = ctx;
114 st->pipe = pipe;
115
116 /* state tracker needs the VBO module */
117 _vbo_CreateContext(ctx);
118
119 #if FEATURE_feedback || FEATURE_drawpix
120 st->draw = draw_create(); /* for selection/feedback */
121
122 /* 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);
129 #endif
130
131 st->dirty.mesa = ~0;
132 st->dirty.st = ~0;
133
134 st->cso_context = cso_create_context(pipe);
135
136 st_init_atoms( st );
137 st_init_bitmap(st);
138 st_init_clear(st);
139 st_init_draw( st );
140 st_init_generate_mipmap(st);
141 st_init_blit(st);
142
143 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
144 st->state.sampler_list[i] = &st->state.samplers[i];
145
146 /* we want all vertex data to be placed in buffer objects */
147 vbo_use_buffer_objects(ctx);
148
149 /* Need these flags:
150 */
151 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
152
153 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
154
155 st->pixel_xfer.cache = _mesa_new_program_cache();
156
157 st->force_msaa = st_get_msaa();
158
159 /* GL limits and extensions */
160 st_init_limits(st);
161 st_init_extensions(st);
162
163 return st;
164 }
165
166
167 struct st_context *st_create_context(struct pipe_context *pipe,
168 const __GLcontextModes *visual,
169 struct st_context *share)
170 {
171 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
180 /* 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
186 return st_create_context_priv(ctx, pipe);
187 }
188
189
190 static void st_destroy_context_priv( struct st_context *st )
191 {
192 uint i;
193
194 #if FEATURE_feedback || FEATURE_drawpix
195 draw_destroy(st->draw);
196 #endif
197 st_destroy_atoms( st );
198 st_destroy_draw( st );
199 st_destroy_generate_mipmap(st);
200 #if FEATURE_EXT_framebuffer_blit
201 st_destroy_blit(st);
202 #endif
203 st_destroy_clear(st);
204 #if FEATURE_drawpix
205 st_destroy_bitmap(st);
206 st_destroy_drawpix(st);
207 #endif
208 #ifdef FEATURE_OES_draw_texture
209 st_destroy_drawtex(st);
210 #endif
211
212 for (i = 0; i < Elements(st->state.sampler_texture); i++) {
213 pipe_texture_reference(&st->state.sampler_texture[i], NULL);
214 }
215
216 for (i = 0; i < Elements(st->state.constants); i++) {
217 if (st->state.constants[i].buffer) {
218 pipe_buffer_reference(&st->state.constants[i].buffer, NULL);
219 }
220 }
221
222 if (st->default_texture) {
223 st->ctx->Driver.DeleteTexture(st->ctx, st->default_texture);
224 st->default_texture = NULL;
225 }
226
227 _mesa_free( st );
228 }
229
230
231 void st_destroy_context( struct st_context *st )
232 {
233 struct pipe_context *pipe = st->pipe;
234 struct cso_context *cso = st->cso_context;
235 GLcontext *ctx = st->ctx;
236 GLuint i;
237
238 /* need to unbind and destroy CSO objects before anything else */
239 cso_release_all(st->cso_context);
240
241 st_reference_fragprog(st, &st->fp, NULL);
242 st_reference_vertprog(st, &st->vp, NULL);
243
244 /* 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
250 _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
251
252 _vbo_DestroyContext(st->ctx);
253
254 _mesa_free_context_data(ctx);
255
256 st_destroy_context_priv(st);
257
258 cso_destroy_context(cso);
259
260 pipe->destroy( pipe );
261
262 _mesa_free(ctx);
263 }
264
265
266 GLboolean
267 st_make_current(struct st_context *st,
268 struct st_framebuffer *draw,
269 struct st_framebuffer *read)
270 {
271 /* Call this periodically to detect when the user has begun using
272 * GL rendering from multiple threads.
273 */
274 _glapi_check_multithread();
275
276 if (st) {
277 if (!_mesa_make_current(st->ctx, &draw->Base, &read->Base))
278 return GL_FALSE;
279
280 _mesa_check_init_viewport(st->ctx, draw->InitWidth, draw->InitHeight);
281
282 return GL_TRUE;
283 }
284 else {
285 return _mesa_make_current(NULL, NULL, NULL);
286 }
287 }
288
289 struct st_context *st_get_current(void)
290 {
291 GET_CURRENT_CONTEXT(ctx);
292
293 return (ctx == NULL) ? NULL : ctx->st;
294 }
295
296 void st_copy_context_state(struct st_context *dst,
297 struct st_context *src,
298 uint mask)
299 {
300 _mesa_copy_context(dst->ctx, src->ctx, mask);
301 }
302
303
304
305 st_proc st_get_proc_address(const char *procname)
306 {
307 return (st_proc) _glapi_get_proc_address(procname);
308 }
309
310
311
312 void st_init_driver_functions(struct dd_function_table *functions)
313 {
314 _mesa_init_glsl_driver_functions(functions);
315
316 #if FEATURE_accum
317 st_init_accum_functions(functions);
318 #endif
319 #if FEATURE_EXT_framebuffer_blit
320 st_init_blit_functions(functions);
321 #endif
322 st_init_bufferobject_functions(functions);
323 st_init_clear_functions(functions);
324 #if FEATURE_drawpix
325 st_init_bitmap_functions(functions);
326 st_init_drawpixels_functions(functions);
327 st_init_rasterpos_functions(functions);
328 #endif
329 st_init_fbo_functions(functions);
330 st_init_get_functions(functions);
331 #if FEATURE_feedback
332 st_init_feedback_functions(functions);
333 #endif
334 st_init_program_functions(functions);
335 #if FEATURE_queryobj
336 st_init_query_functions(functions);
337 #endif
338 st_init_readpixels_functions(functions);
339 st_init_texture_functions(functions);
340 st_init_flush_functions(functions);
341 st_init_string_functions(functions);
342 st_init_viewport_functions(functions);
343
344 functions->UpdateState = st_invalidate_state;
345 }