gallium: glBitmap code now separe from glDraw/CopyPixels code
[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_queryobj.h"
46 #include "st_cb_rasterpos.h"
47 #include "st_cb_readpixels.h"
48 #include "st_cb_texture.h"
49 #include "st_cb_flush.h"
50 #include "st_cb_strings.h"
51 #include "st_atom.h"
52 #include "st_draw.h"
53 #include "st_extensions.h"
54 #include "st_gen_mipmap.h"
55 #include "st_program.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_winsys.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 st->dirty.mesa = ~0;
98 st->dirty.st = ~0;
99
100 st->cso_context = cso_create_context(pipe);
101
102 st_init_atoms( st );
103 st_init_draw( st );
104 st_init_generate_mipmap(st);
105 st_init_blit(st);
106
107 for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
108 st->state.sampler_list[i] = &st->state.samplers[i];
109
110 /* we want all vertex data to be placed in buffer objects */
111 vbo_use_buffer_objects(ctx);
112
113 /* Need these flags:
114 */
115 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
116 st->ctx->FragmentProgram._UseTexEnvProgram = GL_TRUE;
117
118 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
119
120 st->haveFramebufferSurfaces = GL_TRUE;
121
122 st->pixel_xfer.cache = _mesa_new_program_cache();
123
124 /* GL limits and extensions */
125 st_init_limits(st);
126 st_init_extensions(st);
127
128 return st;
129 }
130
131
132 struct st_context *st_create_context(struct pipe_context *pipe,
133 const __GLcontextModes *visual,
134 struct st_context *share)
135 {
136 GLcontext *ctx;
137 GLcontext *shareCtx = share ? share->ctx : NULL;
138 struct dd_function_table funcs;
139
140 memset(&funcs, 0, sizeof(funcs));
141 st_init_driver_functions(&funcs);
142
143 ctx = _mesa_create_context(visual, shareCtx, &funcs, NULL);
144
145 return st_create_context_priv(ctx, pipe);
146 }
147
148
149 static void st_destroy_context_priv( struct st_context *st )
150 {
151 struct pipe_winsys *ws = st->pipe->winsys;
152 uint i;
153
154 draw_destroy(st->draw);
155 st_destroy_atoms( st );
156 st_destroy_draw( st );
157 st_destroy_generate_mipmap(st);
158 st_destroy_bitmap(st);
159 st_destroy_blit(st);
160 st_destroy_clear(st);
161
162 _vbo_DestroyContext(st->ctx);
163
164 cso_destroy_context(st->cso_context);
165
166 _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
167
168 for (i = 0; i < Elements(st->state.constants); i++) {
169 if (st->state.constants[i].buffer) {
170 pipe_buffer_reference(ws, &st->state.constants[i].buffer, NULL);
171 }
172 }
173
174 st->pipe->destroy( st->pipe );
175 free( st );
176 }
177
178
179 void st_destroy_context( struct st_context *st )
180 {
181 GLcontext *ctx = st->ctx;
182 _mesa_free_context_data(ctx);
183 st_destroy_context_priv(st);
184 free(ctx);
185 }
186
187
188 void st_make_current(struct st_context *st,
189 struct st_framebuffer *draw,
190 struct st_framebuffer *read)
191 {
192 if (st) {
193 GLboolean firstTime = st->ctx->FirstTimeCurrent;
194 _mesa_make_current(st->ctx, &draw->Base, &read->Base);
195 /* Need to initialize viewport here since draw->Base->Width/Height
196 * will still be zero at this point.
197 * This could be improved, but would require rather extensive work
198 * elsewhere (allocate rb surface storage sooner)
199 */
200 if (firstTime) {
201 GLuint w = draw->InitWidth, h = draw->InitHeight;
202 _mesa_set_viewport(st->ctx, 0, 0, w, h);
203 _mesa_set_scissor(st->ctx, 0, 0, w, h);
204
205 }
206 }
207 else {
208 _mesa_make_current(NULL, NULL, NULL);
209 }
210 }
211
212
213 void st_copy_context_state(struct st_context *dst,
214 struct st_context *src,
215 uint mask)
216 {
217 _mesa_copy_context(dst->ctx, src->ctx, mask);
218 }
219
220
221 void st_init_driver_functions(struct dd_function_table *functions)
222 {
223 _mesa_init_glsl_driver_functions(functions);
224
225 st_init_accum_functions(functions);
226 st_init_bitmap_functions(functions);
227 st_init_blit_functions(functions);
228 st_init_bufferobject_functions(functions);
229 st_init_clear_functions(functions);
230 st_init_drawpixels_functions(functions);
231 st_init_fbo_functions(functions);
232 st_init_feedback_functions(functions);
233 st_init_program_functions(functions);
234 st_init_query_functions(functions);
235 st_init_rasterpos_functions(functions);
236 st_init_readpixels_functions(functions);
237 st_init_texture_functions(functions);
238 st_init_flush_functions(functions);
239 st_init_string_functions(functions);
240
241 functions->UpdateState = st_invalidate_state;
242 }