93406141f6597b9f56bd4c84b8fc6b8d1e1b2faf
[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_debug.h"
35 #include "st_cb_accum.h"
36 #include "st_cb_bitmap.h"
37 #include "st_cb_blit.h"
38 #include "st_cb_bufferobjects.h"
39 #include "st_cb_clear.h"
40 #include "st_cb_condrender.h"
41 #if FEATURE_drawpix
42 #include "st_cb_drawpixels.h"
43 #include "st_cb_rasterpos.h"
44 #endif
45 #if FEATURE_OES_draw_texture
46 #include "st_cb_drawtex.h"
47 #endif
48 #include "st_cb_eglimage.h"
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 #if FEATURE_EXT_transform_feedback
58 #include "st_cb_xformfb.h"
59 #endif
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 "util/u_inlines.h"
69 #include "util/u_rect.h"
70 #include "util/u_surface.h"
71 #include "draw/draw_context.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 /** Default method for pipe_context::surface_copy() */
106 static void
107 st_surface_copy(struct pipe_context *pipe,
108 struct pipe_surface *dst,
109 unsigned dst_x, unsigned dst_y,
110 struct pipe_surface *src,
111 unsigned src_x, unsigned src_y,
112 unsigned w, unsigned h)
113 {
114 util_surface_copy(pipe, FALSE, dst, dst_x, dst_y, src, src_x, src_y, w, h);
115 }
116
117
118 static struct st_context *
119 st_create_context_priv( GLcontext *ctx, struct pipe_context *pipe )
120 {
121 uint i;
122 struct st_context *st = ST_CALLOC_STRUCT( st_context );
123
124 ctx->st = st;
125
126 st->ctx = ctx;
127 st->pipe = pipe;
128
129 /* XXX: this is one-off, per-screen init: */
130 st_debug_init();
131
132 /* state tracker needs the VBO module */
133 _vbo_CreateContext(ctx);
134
135 #if FEATURE_feedback || FEATURE_drawpix
136 st->draw = draw_create(pipe); /* for selection/feedback */
137
138 /* Disable draw options that might convert points/lines to tris, etc.
139 * as that would foul-up feedback/selection mode.
140 */
141 draw_wide_line_threshold(st->draw, 1000.0f);
142 draw_wide_point_threshold(st->draw, 1000.0f);
143 draw_enable_line_stipple(st->draw, FALSE);
144 draw_enable_point_sprites(st->draw, FALSE);
145 #endif
146
147 st->dirty.mesa = ~0;
148 st->dirty.st = ~0;
149
150 st->cso_context = cso_create_context(pipe);
151
152 st_init_atoms( st );
153 st_init_bitmap(st);
154 st_init_clear(st);
155 st_init_draw( st );
156 st_init_generate_mipmap(st);
157 st_init_blit(st);
158
159 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
160 st->state.sampler_list[i] = &st->state.samplers[i];
161
162 for (i = 0; i < 3; i++) {
163 memset(&st->velems_util_draw[i], 0, sizeof(struct pipe_vertex_element));
164 st->velems_util_draw[i].src_offset = i * 4 * sizeof(float);
165 st->velems_util_draw[i].instance_divisor = 0;
166 st->velems_util_draw[i].vertex_buffer_index = 0;
167 st->velems_util_draw[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
168 }
169
170 /* we want all vertex data to be placed in buffer objects */
171 vbo_use_buffer_objects(ctx);
172
173 /* Need these flags:
174 */
175 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
176
177 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
178
179 st->pixel_xfer.cache = _mesa_new_program_cache();
180
181 st->force_msaa = st_get_msaa();
182
183 /* GL limits and extensions */
184 st_init_limits(st);
185 st_init_extensions(st);
186
187 /* plug in helper driver functions if needed */
188 if (!pipe->surface_copy)
189 pipe->surface_copy = st_surface_copy;
190
191 return st;
192 }
193
194
195 struct st_context *st_create_context(struct pipe_context *pipe,
196 const __GLcontextModes *visual,
197 struct st_context *share)
198 {
199 GLcontext *ctx;
200 GLcontext *shareCtx = share ? share->ctx : NULL;
201 struct dd_function_table funcs;
202
203 memset(&funcs, 0, sizeof(funcs));
204 st_init_driver_functions(&funcs);
205
206 #if FEATURE_GL
207 ctx = _mesa_create_context_for_api(API_OPENGL,
208 visual, shareCtx, &funcs, NULL);
209 #elif FEATURE_ES1
210 ctx = _mesa_create_context_for_api(API_OPENGLES,
211 visual, shareCtx, &funcs, NULL);
212 #elif FEATURE_ES2
213 ctx = _mesa_create_context_for_api(API_OPENGLES2,
214 visual, shareCtx, &funcs, NULL);
215 #endif
216
217 /* XXX: need a capability bit in gallium to query if the pipe
218 * driver prefers DP4 or MUL/MAD for vertex transformation.
219 */
220 if (debug_get_bool_option("MESA_MVP_DP4", FALSE))
221 _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
222
223 return st_create_context_priv(ctx, pipe);
224 }
225
226
227 static void st_destroy_context_priv( struct st_context *st )
228 {
229 uint i;
230
231 #if FEATURE_feedback || FEATURE_drawpix
232 draw_destroy(st->draw);
233 #endif
234 st_destroy_atoms( st );
235 st_destroy_draw( st );
236 st_destroy_generate_mipmap(st);
237 #if FEATURE_EXT_framebuffer_blit
238 st_destroy_blit(st);
239 #endif
240 st_destroy_clear(st);
241 #if FEATURE_drawpix
242 st_destroy_bitmap(st);
243 st_destroy_drawpix(st);
244 #endif
245 #if FEATURE_OES_draw_texture
246 st_destroy_drawtex(st);
247 #endif
248
249 for (i = 0; i < Elements(st->state.sampler_views); i++) {
250 pipe_sampler_view_reference(&st->state.sampler_views[i], NULL);
251 }
252
253 for (i = 0; i < Elements(st->state.constants); i++) {
254 if (st->state.constants[i]) {
255 pipe_resource_reference(&st->state.constants[i], NULL);
256 }
257 }
258
259 if (st->default_texture) {
260 st->ctx->Driver.DeleteTexture(st->ctx, st->default_texture);
261 st->default_texture = NULL;
262 }
263
264 free( st );
265 }
266
267
268 void st_destroy_context( struct st_context *st )
269 {
270 struct pipe_context *pipe = st->pipe;
271 struct cso_context *cso = st->cso_context;
272 GLcontext *ctx = st->ctx;
273 GLuint i;
274
275 /* need to unbind and destroy CSO objects before anything else */
276 cso_release_all(st->cso_context);
277
278 st_reference_fragprog(st, &st->fp, NULL);
279 st_reference_vertprog(st, &st->vp, NULL);
280
281 /* release framebuffer surfaces */
282 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
283 pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
284 }
285 pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
286
287 _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
288
289 _vbo_DestroyContext(st->ctx);
290
291 _mesa_free_context_data(ctx);
292
293 st_destroy_context_priv(st);
294
295 cso_destroy_context(cso);
296
297 pipe->destroy( pipe );
298
299 free(ctx);
300 }
301
302
303 void st_init_driver_functions(struct dd_function_table *functions)
304 {
305 _mesa_init_glsl_driver_functions(functions);
306
307 #if FEATURE_accum
308 st_init_accum_functions(functions);
309 #endif
310 #if FEATURE_EXT_framebuffer_blit
311 st_init_blit_functions(functions);
312 #endif
313 st_init_bufferobject_functions(functions);
314 st_init_clear_functions(functions);
315 #if FEATURE_drawpix
316 st_init_bitmap_functions(functions);
317 st_init_drawpixels_functions(functions);
318 st_init_rasterpos_functions(functions);
319 #endif
320
321 #if FEATURE_OES_draw_texture
322 st_init_drawtex_functions(functions);
323 #endif
324
325 st_init_eglimage_functions(functions);
326
327 st_init_fbo_functions(functions);
328 #if FEATURE_feedback
329 st_init_feedback_functions(functions);
330 #endif
331 st_init_program_functions(functions);
332 #if FEATURE_queryobj
333 st_init_query_functions(functions);
334 #endif
335 st_init_cond_render_functions(functions);
336 st_init_readpixels_functions(functions);
337 st_init_texture_functions(functions);
338 st_init_flush_functions(functions);
339 st_init_string_functions(functions);
340
341 #if FEATURE_EXT_transform_feedback
342 st_init_xformfb_functions(functions);
343 #endif
344
345 functions->UpdateState = st_invalidate_state;
346 }