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