[g3dvl] move video buffer creation out of video context
[mesa.git] / src / gallium / drivers / softpipe / sp_context.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 * Copyright 2008 VMware, Inc. All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 /* Author:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include "draw/draw_context.h"
34 #include "draw/draw_vbuf.h"
35 #include "pipe/p_defines.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "util/u_inlines.h"
39 #include "tgsi/tgsi_exec.h"
40 #include "vl/vl_video_buffer.h"
41 #include "sp_clear.h"
42 #include "sp_context.h"
43 #include "sp_flush.h"
44 #include "sp_prim_vbuf.h"
45 #include "sp_state.h"
46 #include "sp_surface.h"
47 #include "sp_tile_cache.h"
48 #include "sp_tex_tile_cache.h"
49 #include "sp_texture.h"
50 #include "sp_query.h"
51
52
53
54 /**
55 * Map any drawing surfaces which aren't already mapped
56 */
57 void
58 softpipe_map_transfers(struct softpipe_context *sp)
59 {
60 unsigned i;
61
62 for (i = 0; i < sp->framebuffer.nr_cbufs; i++) {
63 sp_tile_cache_map_transfers(sp->cbuf_cache[i]);
64 }
65
66 sp_tile_cache_map_transfers(sp->zsbuf_cache);
67 }
68
69
70 /**
71 * Unmap any mapped drawing surfaces
72 */
73 void
74 softpipe_unmap_transfers(struct softpipe_context *sp)
75 {
76 uint i;
77
78 for (i = 0; i < sp->framebuffer.nr_cbufs; i++) {
79 sp_tile_cache_unmap_transfers(sp->cbuf_cache[i]);
80 }
81
82 sp_tile_cache_unmap_transfers(sp->zsbuf_cache);
83 }
84
85
86 static void
87 softpipe_destroy( struct pipe_context *pipe )
88 {
89 struct softpipe_context *softpipe = softpipe_context( pipe );
90 uint i;
91
92 if (softpipe->draw)
93 draw_destroy( softpipe->draw );
94
95 if (softpipe->quad.shade)
96 softpipe->quad.shade->destroy( softpipe->quad.shade );
97
98 if (softpipe->quad.depth_test)
99 softpipe->quad.depth_test->destroy( softpipe->quad.depth_test );
100
101 if (softpipe->quad.blend)
102 softpipe->quad.blend->destroy( softpipe->quad.blend );
103
104 if (softpipe->quad.pstipple)
105 softpipe->quad.pstipple->destroy( softpipe->quad.pstipple );
106
107 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
108 sp_destroy_tile_cache(softpipe->cbuf_cache[i]);
109 pipe_surface_reference(&softpipe->framebuffer.cbufs[i], NULL);
110 }
111
112 sp_destroy_tile_cache(softpipe->zsbuf_cache);
113 pipe_surface_reference(&softpipe->framebuffer.zsbuf, NULL);
114
115 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
116 sp_destroy_tex_tile_cache(softpipe->fragment_tex_cache[i]);
117 pipe_sampler_view_reference(&softpipe->fragment_sampler_views[i], NULL);
118 }
119
120 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
121 sp_destroy_tex_tile_cache(softpipe->vertex_tex_cache[i]);
122 pipe_sampler_view_reference(&softpipe->vertex_sampler_views[i], NULL);
123 }
124
125 for (i = 0; i < PIPE_MAX_GEOMETRY_SAMPLERS; i++) {
126 sp_destroy_tex_tile_cache(softpipe->geometry_tex_cache[i]);
127 pipe_sampler_view_reference(&softpipe->geometry_sampler_views[i], NULL);
128 }
129
130 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
131 uint j;
132
133 for (j = 0; j < PIPE_MAX_CONSTANT_BUFFERS; j++) {
134 if (softpipe->constants[i][j]) {
135 pipe_resource_reference(&softpipe->constants[i][j], NULL);
136 }
137 }
138 }
139
140 for (i = 0; i < softpipe->num_vertex_buffers; i++) {
141 pipe_resource_reference(&softpipe->vertex_buffer[i].buffer, NULL);
142 }
143
144 tgsi_exec_machine_destroy(softpipe->fs_machine);
145
146 FREE( softpipe );
147 }
148
149
150 /**
151 * if (the texture is being used as a framebuffer surface)
152 * return SP_REFERENCED_FOR_WRITE
153 * else if (the texture is a bound texture source)
154 * return SP_REFERENCED_FOR_READ
155 * else
156 * return SP_UNREFERENCED
157 */
158 unsigned int
159 softpipe_is_resource_referenced( struct pipe_context *pipe,
160 struct pipe_resource *texture,
161 unsigned level, int layer)
162 {
163 struct softpipe_context *softpipe = softpipe_context( pipe );
164 unsigned i;
165
166 if (texture->target == PIPE_BUFFER)
167 return SP_UNREFERENCED;
168
169 /* check if any of the bound drawing surfaces are this texture */
170 if (softpipe->dirty_render_cache) {
171 for (i = 0; i < softpipe->framebuffer.nr_cbufs; i++) {
172 if (softpipe->framebuffer.cbufs[i] &&
173 softpipe->framebuffer.cbufs[i]->texture == texture) {
174 return SP_REFERENCED_FOR_WRITE;
175 }
176 }
177 if (softpipe->framebuffer.zsbuf &&
178 softpipe->framebuffer.zsbuf->texture == texture) {
179 return SP_REFERENCED_FOR_WRITE;
180 }
181 }
182
183 /* check if any of the tex_cache textures are this texture */
184 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
185 if (softpipe->fragment_tex_cache[i] &&
186 softpipe->fragment_tex_cache[i]->texture == texture)
187 return SP_REFERENCED_FOR_READ;
188 }
189 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
190 if (softpipe->vertex_tex_cache[i] &&
191 softpipe->vertex_tex_cache[i]->texture == texture)
192 return SP_REFERENCED_FOR_READ;
193 }
194 for (i = 0; i < PIPE_MAX_GEOMETRY_SAMPLERS; i++) {
195 if (softpipe->geometry_tex_cache[i] &&
196 softpipe->geometry_tex_cache[i]->texture == texture)
197 return SP_REFERENCED_FOR_READ;
198 }
199
200 return SP_UNREFERENCED;
201 }
202
203
204
205
206 static void
207 softpipe_render_condition( struct pipe_context *pipe,
208 struct pipe_query *query,
209 uint mode )
210 {
211 struct softpipe_context *softpipe = softpipe_context( pipe );
212
213 softpipe->render_cond_query = query;
214 softpipe->render_cond_mode = mode;
215 }
216
217
218
219 struct pipe_context *
220 softpipe_create_context( struct pipe_screen *screen,
221 void *priv )
222 {
223 struct softpipe_context *softpipe = CALLOC_STRUCT(softpipe_context);
224 uint i;
225
226 util_init_math();
227
228 #ifdef PIPE_ARCH_X86
229 softpipe->use_sse = !debug_get_bool_option( "GALLIUM_NOSSE", FALSE );
230 #else
231 softpipe->use_sse = FALSE;
232 #endif
233
234 softpipe->dump_fs = debug_get_bool_option( "SOFTPIPE_DUMP_FS", FALSE );
235 softpipe->dump_gs = debug_get_bool_option( "SOFTPIPE_DUMP_GS", FALSE );
236
237 softpipe->pipe.winsys = NULL;
238 softpipe->pipe.screen = screen;
239 softpipe->pipe.destroy = softpipe_destroy;
240 softpipe->pipe.priv = priv;
241
242 /* state setters */
243 softpipe_init_blend_funcs(&softpipe->pipe);
244 softpipe_init_clip_funcs(&softpipe->pipe);
245 softpipe_init_query_funcs( softpipe );
246 softpipe_init_rasterizer_funcs(&softpipe->pipe);
247 softpipe_init_sampler_funcs(&softpipe->pipe);
248 softpipe_init_shader_funcs(&softpipe->pipe);
249 softpipe_init_streamout_funcs(&softpipe->pipe);
250 softpipe_init_texture_funcs( &softpipe->pipe );
251 softpipe_init_vertex_funcs(&softpipe->pipe);
252
253 softpipe->pipe.set_framebuffer_state = softpipe_set_framebuffer_state;
254
255 softpipe->pipe.draw_vbo = softpipe_draw_vbo;
256 softpipe->pipe.draw_stream_output = softpipe_draw_stream_output;
257
258 softpipe->pipe.clear = softpipe_clear;
259 softpipe->pipe.flush = softpipe_flush_wrapped;
260
261 softpipe->pipe.render_condition = softpipe_render_condition;
262
263 softpipe->pipe.create_video_buffer = vl_video_buffer_create;
264
265 /*
266 * Alloc caches for accessing drawing surfaces and textures.
267 * Must be before quad stage setup!
268 */
269 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++)
270 softpipe->cbuf_cache[i] = sp_create_tile_cache( &softpipe->pipe );
271 softpipe->zsbuf_cache = sp_create_tile_cache( &softpipe->pipe );
272
273 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
274 softpipe->fragment_tex_cache[i] = sp_create_tex_tile_cache( &softpipe->pipe );
275 if (!softpipe->fragment_tex_cache[i])
276 goto fail;
277 }
278
279 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
280 softpipe->vertex_tex_cache[i] = sp_create_tex_tile_cache( &softpipe->pipe );
281 if (!softpipe->vertex_tex_cache[i])
282 goto fail;
283 }
284
285 for (i = 0; i < PIPE_MAX_GEOMETRY_SAMPLERS; i++) {
286 softpipe->geometry_tex_cache[i] = sp_create_tex_tile_cache( &softpipe->pipe );
287 if (!softpipe->geometry_tex_cache[i])
288 goto fail;
289 }
290
291 softpipe->fs_machine = tgsi_exec_machine_create();
292
293 /* setup quad rendering stages */
294 softpipe->quad.shade = sp_quad_shade_stage(softpipe);
295 softpipe->quad.depth_test = sp_quad_depth_test_stage(softpipe);
296 softpipe->quad.blend = sp_quad_blend_stage(softpipe);
297 softpipe->quad.pstipple = sp_quad_polygon_stipple_stage(softpipe);
298
299
300 /*
301 * Create drawing context and plug our rendering stage into it.
302 */
303 softpipe->draw = draw_create(&softpipe->pipe);
304 if (!softpipe->draw)
305 goto fail;
306
307 draw_texture_samplers(softpipe->draw,
308 PIPE_SHADER_VERTEX,
309 PIPE_MAX_VERTEX_SAMPLERS,
310 (struct tgsi_sampler **)
311 softpipe->tgsi.vert_samplers_list);
312
313 draw_texture_samplers(softpipe->draw,
314 PIPE_SHADER_GEOMETRY,
315 PIPE_MAX_GEOMETRY_SAMPLERS,
316 (struct tgsi_sampler **)
317 softpipe->tgsi.geom_samplers_list);
318
319 if (debug_get_bool_option( "SOFTPIPE_NO_RAST", FALSE ))
320 softpipe->no_rast = TRUE;
321
322 softpipe->vbuf_backend = sp_create_vbuf_backend(softpipe);
323 if (!softpipe->vbuf_backend)
324 goto fail;
325
326 softpipe->vbuf = draw_vbuf_stage(softpipe->draw, softpipe->vbuf_backend);
327 if (!softpipe->vbuf)
328 goto fail;
329
330 draw_set_rasterize_stage(softpipe->draw, softpipe->vbuf);
331 draw_set_render(softpipe->draw, softpipe->vbuf_backend);
332
333
334 /* plug in AA line/point stages */
335 draw_install_aaline_stage(softpipe->draw, &softpipe->pipe);
336 draw_install_aapoint_stage(softpipe->draw, &softpipe->pipe);
337
338 /* Do polygon stipple w/ texture map + frag prog? */
339 #if DO_PSTIPPLE_IN_DRAW_MODULE
340 draw_install_pstipple_stage(softpipe->draw, &softpipe->pipe);
341 #endif
342
343 draw_wide_point_sprites(softpipe->draw, TRUE);
344
345 sp_init_surface_functions(softpipe);
346
347 return &softpipe->pipe;
348
349 fail:
350 softpipe_destroy(&softpipe->pipe);
351 return NULL;
352 }