1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "main/imports.h"
29 #include "main/context.h"
31 #include "shader/shader_api.h"
32 #include "glapi/glapi.h"
33 #include "st_context.h"
34 #include "st_public.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"
43 #include "st_cb_drawpixels.h"
44 #include "st_cb_rasterpos.h"
46 #if FEATURE_OES_draw_texture
47 #include "st_cb_drawtex.h"
49 #include "st_cb_eglimage.h"
50 #include "st_cb_fbo.h"
52 #include "st_cb_feedback.h"
54 #include "st_cb_program.h"
55 #include "st_cb_queryobj.h"
56 #include "st_cb_readpixels.h"
57 #include "st_cb_texture.h"
58 #include "st_cb_flush.h"
59 #include "st_cb_strings.h"
62 #include "st_extensions.h"
63 #include "st_gen_mipmap.h"
64 #include "st_program.h"
65 #include "pipe/p_context.h"
66 #include "util/u_inlines.h"
67 #include "util/u_rect.h"
68 #include "draw/draw_context.h"
69 #include "cso_cache/cso_context.h"
73 * Called via ctx->Driver.UpdateState()
75 void st_invalidate_state(GLcontext * ctx, GLuint new_state)
77 struct st_context *st = st_context(ctx);
79 st->dirty.mesa |= new_state;
80 st->dirty.st |= ST_NEW_MESA;
82 /* This is the only core Mesa module we depend upon.
83 * No longer use swrast, swsetup, tnl.
85 _vbo_InvalidateState(ctx, new_state);
90 * Check for multisample env var override.
95 const char *msaa = _mesa_getenv("__GL_FSAA_MODE");
102 /** Default method for pipe_context::surface_copy() */
104 st_surface_copy(struct pipe_context *pipe,
105 struct pipe_surface *dst,
106 unsigned dst_x, unsigned dst_y,
107 struct pipe_surface *src,
108 unsigned src_x, unsigned src_y,
109 unsigned w, unsigned h)
111 util_surface_copy(pipe, FALSE, dst, dst_x, dst_y, src, src_x, src_y, w, h);
115 static struct st_context *
116 st_create_context_priv( GLcontext *ctx, struct pipe_context *pipe )
119 struct st_context *st = ST_CALLOC_STRUCT( st_context );
126 /* XXX: this is one-off, per-screen init: */
129 /* state tracker needs the VBO module */
130 _vbo_CreateContext(ctx);
132 #if FEATURE_feedback || FEATURE_drawpix
133 st->draw = draw_create(); /* for selection/feedback */
135 /* Disable draw options that might convert points/lines to tris, etc.
136 * as that would foul-up feedback/selection mode.
138 draw_wide_line_threshold(st->draw, 1000.0f);
139 draw_wide_point_threshold(st->draw, 1000.0f);
140 draw_enable_line_stipple(st->draw, FALSE);
141 draw_enable_point_sprites(st->draw, FALSE);
147 st->cso_context = cso_create_context(pipe);
153 st_init_generate_mipmap(st);
156 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
157 st->state.sampler_list[i] = &st->state.samplers[i];
159 for (i = 0; i < 3; i++) {
160 memset(&st->velems_util_draw[i], 0, sizeof(struct pipe_vertex_element));
161 st->velems_util_draw[i].src_offset = i * 4 * sizeof(float);
162 st->velems_util_draw[i].instance_divisor = 0;
163 st->velems_util_draw[i].vertex_buffer_index = 0;
164 st->velems_util_draw[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
167 /* we want all vertex data to be placed in buffer objects */
168 vbo_use_buffer_objects(ctx);
172 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
174 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
176 st->pixel_xfer.cache = _mesa_new_program_cache();
178 st->force_msaa = st_get_msaa();
180 /* GL limits and extensions */
182 st_init_extensions(st);
184 /* plug in helper driver functions if needed */
185 if (!pipe->surface_copy)
186 pipe->surface_copy = st_surface_copy;
192 struct st_context *st_create_context(struct pipe_context *pipe,
193 const __GLcontextModes *visual,
194 struct st_context *share)
197 GLcontext *shareCtx = share ? share->ctx : NULL;
198 struct dd_function_table funcs;
200 memset(&funcs, 0, sizeof(funcs));
201 st_init_driver_functions(&funcs);
203 ctx = _mesa_create_context(visual, shareCtx, &funcs, NULL);
205 /* XXX: need a capability bit in gallium to query if the pipe
206 * driver prefers DP4 or MUL/MAD for vertex transformation.
208 if (debug_get_bool_option("MESA_MVP_DP4", FALSE))
209 _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
211 return st_create_context_priv(ctx, pipe);
215 static void st_destroy_context_priv( struct st_context *st )
219 #if FEATURE_feedback || FEATURE_drawpix
220 draw_destroy(st->draw);
222 st_destroy_atoms( st );
223 st_destroy_draw( st );
224 st_destroy_generate_mipmap(st);
225 #if FEATURE_EXT_framebuffer_blit
228 st_destroy_clear(st);
230 st_destroy_bitmap(st);
231 st_destroy_drawpix(st);
233 #if FEATURE_OES_draw_texture
234 st_destroy_drawtex(st);
237 for (i = 0; i < Elements(st->state.sampler_views); i++) {
238 pipe_sampler_view_reference(&st->state.sampler_views[i], NULL);
241 for (i = 0; i < Elements(st->state.constants); i++) {
242 if (st->state.constants[i]) {
243 pipe_buffer_reference(&st->state.constants[i], NULL);
247 if (st->default_texture) {
248 st->ctx->Driver.DeleteTexture(st->ctx, st->default_texture);
249 st->default_texture = NULL;
256 void st_destroy_context( struct st_context *st )
258 struct pipe_context *pipe = st->pipe;
259 struct cso_context *cso = st->cso_context;
260 GLcontext *ctx = st->ctx;
263 /* need to unbind and destroy CSO objects before anything else */
264 cso_release_all(st->cso_context);
266 st_reference_fragprog(st, &st->fp, NULL);
267 st_reference_vertprog(st, &st->vp, NULL);
269 /* release framebuffer surfaces */
270 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
271 pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
273 pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
275 _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
277 _vbo_DestroyContext(st->ctx);
279 _mesa_free_context_data(ctx);
281 st_destroy_context_priv(st);
283 cso_destroy_context(cso);
285 pipe->destroy( pipe );
292 st_make_current(struct st_context *st,
293 struct st_framebuffer *draw,
294 struct st_framebuffer *read,
295 void *winsys_drawable_handle )
297 /* Call this periodically to detect when the user has begun using
298 * GL rendering from multiple threads.
300 _glapi_check_multithread();
303 if (!_mesa_make_current(st->ctx, &draw->Base, &read->Base)) {
304 st->pipe->priv = NULL;
308 _mesa_check_init_viewport(st->ctx, draw->InitWidth, draw->InitHeight);
309 st->winsys_drawable_handle = winsys_drawable_handle;
314 return _mesa_make_current(NULL, NULL, NULL);
318 struct st_context *st_get_current(void)
320 GET_CURRENT_CONTEXT(ctx);
322 return (ctx == NULL) ? NULL : ctx->st;
325 void st_copy_context_state(struct st_context *dst,
326 struct st_context *src,
329 _mesa_copy_context(dst->ctx, src->ctx, mask);
334 st_proc st_get_proc_address(const char *procname)
336 return (st_proc) _glapi_get_proc_address(procname);
341 void st_init_driver_functions(struct dd_function_table *functions)
343 _mesa_init_glsl_driver_functions(functions);
346 st_init_accum_functions(functions);
348 #if FEATURE_EXT_framebuffer_blit
349 st_init_blit_functions(functions);
351 st_init_bufferobject_functions(functions);
352 st_init_clear_functions(functions);
354 st_init_bitmap_functions(functions);
355 st_init_drawpixels_functions(functions);
356 st_init_rasterpos_functions(functions);
359 #if FEATURE_OES_draw_texture
360 st_init_drawtex_functions(functions);
363 st_init_eglimage_functions(functions);
365 st_init_fbo_functions(functions);
367 st_init_feedback_functions(functions);
369 st_init_program_functions(functions);
371 st_init_query_functions(functions);
373 st_init_cond_render_functions(functions);
374 st_init_readpixels_functions(functions);
375 st_init_texture_functions(functions);
376 st_init_flush_functions(functions);
377 st_init_string_functions(functions);
379 functions->UpdateState = st_invalidate_state;