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