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