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