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