OSDN Git Service

st/mesa: Make st_cb_accum.h FEATURE_accum aware.
[android-x86/external-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 "vbo/vbo.h"
31 #include "shader/shader_api.h"
32 #include "glapi/glapi.h"
33 #include "st_context.h"
34 #include "st_debug.h"
35 #include "st_cb_accum.h"
36 #include "st_cb_bitmap.h"
37 #include "st_cb_blit.h"
38 #include "st_cb_bufferobjects.h"
39 #include "st_cb_clear.h"
40 #include "st_cb_condrender.h"
41 #if FEATURE_drawpix
42 #include "st_cb_drawpixels.h"
43 #include "st_cb_rasterpos.h"
44 #endif
45 #if FEATURE_OES_draw_texture
46 #include "st_cb_drawtex.h"
47 #endif
48 #include "st_cb_eglimage.h"
49 #include "st_cb_fbo.h"
50 #if FEATURE_feedback
51 #include "st_cb_feedback.h"
52 #endif
53 #include "st_cb_program.h"
54 #include "st_cb_queryobj.h"
55 #include "st_cb_readpixels.h"
56 #include "st_cb_texture.h"
57 #include "st_cb_xformfb.h"
58 #include "st_cb_flush.h"
59 #include "st_cb_strings.h"
60 #include "st_atom.h"
61 #include "st_draw.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 "util/u_surface.h"
69 #include "draw/draw_context.h"
70 #include "cso_cache/cso_context.h"
71
72
73 /**
74  * Called via ctx->Driver.UpdateState()
75  */
76 void 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
90 /**
91  * Check for multisample env var override.
92  */
93 int
94 st_get_msaa(void)
95 {
96    const char *msaa = _mesa_getenv("__GL_FSAA_MODE");
97    if (msaa)
98       return atoi(msaa);
99    return 0;
100 }
101
102
103 /** Default method for pipe_context::surface_copy() */
104 static void
105 st_surface_copy(struct pipe_context *pipe,
106                 struct pipe_surface *dst,
107                 unsigned dst_x, unsigned dst_y,
108                 struct pipe_surface *src,
109                 unsigned src_x, unsigned src_y, 
110                 unsigned w, unsigned h)
111 {
112    util_surface_copy(pipe, FALSE, dst, dst_x, dst_y, src, src_x, src_y, w, h);
113 }
114
115
116 static struct st_context *
117 st_create_context_priv( GLcontext *ctx, struct pipe_context *pipe )
118 {
119    uint i;
120    struct st_context *st = ST_CALLOC_STRUCT( st_context );
121    
122    ctx->st = st;
123
124    st->ctx = ctx;
125    st->pipe = pipe;
126
127    /* XXX: this is one-off, per-screen init: */
128    st_debug_init();
129    
130    /* state tracker needs the VBO module */
131    _vbo_CreateContext(ctx);
132
133 #if FEATURE_feedback || FEATURE_drawpix
134    st->draw = draw_create(pipe); /* for selection/feedback */
135
136    /* Disable draw options that might convert points/lines to tris, etc.
137     * as that would foul-up feedback/selection mode.
138     */
139    draw_wide_line_threshold(st->draw, 1000.0f);
140    draw_wide_point_threshold(st->draw, 1000.0f);
141    draw_enable_line_stipple(st->draw, FALSE);
142    draw_enable_point_sprites(st->draw, FALSE);
143 #endif
144
145    st->dirty.mesa = ~0;
146    st->dirty.st = ~0;
147
148    st->cso_context = cso_create_context(pipe);
149
150    st_init_atoms( st );
151    st_init_bitmap(st);
152    st_init_clear(st);
153    st_init_draw( st );
154    st_init_generate_mipmap(st);
155    st_init_blit(st);
156
157    for (i = 0; i < PIPE_MAX_SAMPLERS; i++)
158       st->state.sampler_list[i] = &st->state.samplers[i];
159
160    for (i = 0; i < 3; i++) {
161       memset(&st->velems_util_draw[i], 0, sizeof(struct pipe_vertex_element));
162       st->velems_util_draw[i].src_offset = i * 4 * sizeof(float);
163       st->velems_util_draw[i].instance_divisor = 0;
164       st->velems_util_draw[i].vertex_buffer_index = 0;
165       st->velems_util_draw[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
166    }
167
168    /* we want all vertex data to be placed in buffer objects */
169    vbo_use_buffer_objects(ctx);
170
171    /* Need these flags:
172     */
173    st->ctx->FragmentProgram._MaintainTexEnvProgram = GL_TRUE;
174
175    st->ctx->VertexProgram._MaintainTnlProgram = GL_TRUE;
176
177    st->pixel_xfer.cache = _mesa_new_program_cache();
178
179    st->force_msaa = st_get_msaa();
180
181    /* GL limits and extensions */
182    st_init_limits(st);
183    st_init_extensions(st);
184
185    /* plug in helper driver functions if needed */
186    if (!pipe->surface_copy)
187       pipe->surface_copy = st_surface_copy;
188
189    return st;
190 }
191
192
193 struct st_context *st_create_context(struct pipe_context *pipe,
194                                      const __GLcontextModes *visual,
195                                      struct st_context *share)
196 {
197    GLcontext *ctx;
198    GLcontext *shareCtx = share ? share->ctx : NULL;
199    struct dd_function_table funcs;
200
201    memset(&funcs, 0, sizeof(funcs));
202    st_init_driver_functions(&funcs);
203
204 #if FEATURE_GL
205    ctx = _mesa_create_context_for_api(API_OPENGL,
206                                       visual, shareCtx, &funcs, NULL);
207 #elif FEATURE_ES1
208    ctx = _mesa_create_context_for_api(API_OPENGLES,
209                                       visual, shareCtx, &funcs, NULL);
210 #elif FEATURE_ES2
211    ctx = _mesa_create_context_for_api(API_OPENGLES2,
212                                       visual, shareCtx, &funcs, NULL);
213 #endif
214
215    /* XXX: need a capability bit in gallium to query if the pipe
216     * driver prefers DP4 or MUL/MAD for vertex transformation.
217     */
218    if (debug_get_bool_option("MESA_MVP_DP4", FALSE))
219       _mesa_set_mvp_with_dp4( ctx, GL_TRUE );
220
221    return st_create_context_priv(ctx, pipe);
222 }
223
224
225 static void st_destroy_context_priv( struct st_context *st )
226 {
227    uint i;
228
229 #if FEATURE_feedback || FEATURE_drawpix
230    draw_destroy(st->draw);
231 #endif
232    st_destroy_atoms( st );
233    st_destroy_draw( st );
234    st_destroy_generate_mipmap(st);
235 #if FEATURE_EXT_framebuffer_blit
236    st_destroy_blit(st);
237 #endif
238    st_destroy_clear(st);
239 #if FEATURE_drawpix
240    st_destroy_bitmap(st);
241    st_destroy_drawpix(st);
242 #endif
243 #if FEATURE_OES_draw_texture
244    st_destroy_drawtex(st);
245 #endif
246
247    for (i = 0; i < Elements(st->state.sampler_views); i++) {
248       pipe_sampler_view_reference(&st->state.sampler_views[i], NULL);
249    }
250
251    for (i = 0; i < Elements(st->state.constants); i++) {
252       if (st->state.constants[i]) {
253          pipe_resource_reference(&st->state.constants[i], NULL);
254       }
255    }
256
257    if (st->default_texture) {
258       st->ctx->Driver.DeleteTexture(st->ctx, st->default_texture);
259       st->default_texture = NULL;
260    }
261
262    free( st );
263 }
264
265  
266 void st_destroy_context( struct st_context *st )
267 {
268    struct pipe_context *pipe = st->pipe;
269    struct cso_context *cso = st->cso_context;
270    GLcontext *ctx = st->ctx;
271    GLuint i;
272
273    /* need to unbind and destroy CSO objects before anything else */
274    cso_release_all(st->cso_context);
275
276    st_reference_fragprog(st, &st->fp, NULL);
277    st_reference_vertprog(st, &st->vp, NULL);
278
279    /* release framebuffer surfaces */
280    for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
281       pipe_surface_reference(&st->state.framebuffer.cbufs[i], NULL);
282    }
283    pipe_surface_reference(&st->state.framebuffer.zsbuf, NULL);
284
285    _mesa_delete_program_cache(st->ctx, st->pixel_xfer.cache);
286
287    _vbo_DestroyContext(st->ctx);
288
289    _mesa_free_context_data(ctx);
290
291    st_destroy_context_priv(st);
292
293    cso_destroy_context(cso);
294
295    pipe->destroy( pipe );
296
297    free(ctx);
298 }
299
300
301 void st_init_driver_functions(struct dd_function_table *functions)
302 {
303    _mesa_init_glsl_driver_functions(functions);
304
305    st_init_accum_functions(functions);
306 #if FEATURE_EXT_framebuffer_blit
307    st_init_blit_functions(functions);
308 #endif
309    st_init_bufferobject_functions(functions);
310    st_init_clear_functions(functions);
311 #if FEATURE_drawpix
312    st_init_bitmap_functions(functions);
313    st_init_drawpixels_functions(functions);
314    st_init_rasterpos_functions(functions);
315 #endif
316
317 #if FEATURE_OES_draw_texture
318    st_init_drawtex_functions(functions);
319 #endif
320
321    st_init_eglimage_functions(functions);
322
323    st_init_fbo_functions(functions);
324 #if FEATURE_feedback
325    st_init_feedback_functions(functions);
326 #endif
327    st_init_program_functions(functions);
328 #if FEATURE_queryobj
329    st_init_query_functions(functions);
330 #endif
331    st_init_cond_render_functions(functions);
332    st_init_readpixels_functions(functions);
333    st_init_texture_functions(functions);
334    st_init_flush_functions(functions);
335    st_init_string_functions(functions);
336
337    st_init_xformfb_functions(functions);
338
339    functions->UpdateState = st_invalidate_state;
340 }