Merge branch 'nouveau-gallium-0.1' into darktama-gallium-0.1
[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 "vbo/vbo.h"
32 #include "shader/shader_api.h"
33 #include "st_public.h"
34 #include "st_context.h"
35 #include "st_cb_accum.h"
36 #include "st_cb_bufferobjects.h"
37 #include "st_cb_clear.h"
38 #include "st_cb_drawpixels.h"
39 #include "st_cb_fbo.h"
40 #include "st_cb_feedback.h"
41 #include "st_cb_queryobj.h"
42 #include "st_cb_rasterpos.h"
43 #include "st_cb_readpixels.h"
44 #include "st_cb_texture.h"
45 #include "st_cb_flush.h"
46 #include "st_cb_strings.h"
47 #include "st_atom.h"
48 #include "st_draw.h"
49 #include "st_extensions.h"
50 #include "st_program.h"
51 #include "pipe/p_context.h"
52 #include "pipe/draw/draw_context.h"
53 #include "pipe/cso_cache/cso_cache.h"
54
55
56 /**
57 * Called via ctx->Driver.UpdateState()
58 */
59 void st_invalidate_state(GLcontext * ctx, GLuint new_state)
60 {
61 struct st_context *st = st_context(ctx);
62
63 st->dirty.mesa |= new_state;
64 st->dirty.st |= ST_NEW_MESA;
65
66 /* This is the only core Mesa module we depend upon.
67 * No longer use swrast, swsetup, tnl.
68 */
69 _vbo_InvalidateState(ctx, new_state);
70 }
71
72
73 static struct st_context *
74 st_create_context_priv( GLcontext *ctx, struct pipe_context *pipe )
75 {
76 struct st_context *st = CALLOC_STRUCT( st_context );
77
78 ctx->st = st;
79
80 st->ctx = ctx;
81 st->pipe = pipe;
82
83 /* state tracker needs the VBO module */
84 _vbo_CreateContext(ctx);
85
86 st->draw = draw_create(); /* for selection/feedback */
87
88 st->dirty.mesa = ~0;
89 st->dirty.st = ~0;
90
91 st->cache = cso_cache_create();
92
93 st_init_atoms( st );
94 st_init_draw( st );
95
96 /* we want all vertex data to be placed in buffer objects */
97 vbo_use_buffer_objects(ctx);
98
99 /* Need these flags:
100 */
101 st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
102 st->ctx->FragmentProgram._UseTexEnvProgram = GL_TRUE;
103
104 st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
105
106 st->haveFramebufferSurfaces = GL_TRUE;
107
108 st->pixel_xfer.cache = _mesa_new_program_cache();
109
110 /* GL limits and extensions */
111 st_init_limits(st);
112 st_init_extensions(st);
113
114 return st;
115 }
116
117
118 struct st_context *st_create_context(struct pipe_context *pipe,
119 const __GLcontextModes *visual,
120 struct st_context *share)
121 {
122 GLcontext *ctx;
123 GLcontext *shareCtx = share ? share->ctx : NULL;
124 struct dd_function_table funcs;
125
126 memset(&funcs, 0, sizeof(funcs));
127 st_init_driver_functions(&funcs);
128
129 ctx = _mesa_create_context(visual, shareCtx, &funcs, NULL);
130
131 return st_create_context_priv(ctx, pipe);
132 }
133
134
135 static void st_destroy_context_priv( struct st_context *st )
136 {
137 draw_destroy(st->draw);
138 st_destroy_atoms( st );
139 st_destroy_draw( st );
140
141 _vbo_DestroyContext(st->ctx);
142
143 cso_cache_delete( st->cache );
144
145 _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
146
147 st->pipe->destroy( st->pipe );
148 free( st );
149 }
150
151
152 void st_destroy_context( struct st_context *st )
153 {
154 GLcontext *ctx = st->ctx;
155 _mesa_free_context_data(ctx);
156 st_destroy_context_priv(st);
157 free(ctx);
158 }
159
160
161 void st_make_current(struct st_context *st,
162 struct st_framebuffer *draw,
163 struct st_framebuffer *read)
164 {
165 if (st) {
166 _mesa_make_current(st->ctx, &draw->Base, &read->Base);
167 }
168 else {
169 _mesa_make_current(NULL, NULL, NULL);
170 }
171 }
172
173
174 void st_copy_context_state(struct st_context *dst,
175 struct st_context *src,
176 uint mask)
177 {
178 _mesa_copy_context(dst->ctx, src->ctx, mask);
179 }
180
181
182 void st_init_driver_functions(struct dd_function_table *functions)
183 {
184 _mesa_init_glsl_driver_functions(functions);
185
186 st_init_accum_functions(functions);
187 st_init_bufferobject_functions(functions);
188 st_init_clear_functions(functions);
189 st_init_drawpixels_functions(functions);
190 st_init_fbo_functions(functions);
191 st_init_feedback_functions(functions);
192 st_init_program_functions(functions);
193 st_init_query_functions(functions);
194 st_init_rasterpos_functions(functions);
195 st_init_readpixels_functions(functions);
196 st_init_texture_functions(functions);
197 st_init_flush_functions(functions);
198 st_init_string_functions(functions);
199
200 functions->UpdateState = st_invalidate_state;
201 }