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