Merge branch 'remove-copyteximage-hook'
[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_decoder.h"
41 #include "vl/vl_video_buffer.h"
42 #include "sp_clear.h"
43 #include "sp_context.h"
44 #include "sp_flush.h"
45 #include "sp_prim_vbuf.h"
46 #include "sp_state.h"
47 #include "sp_surface.h"
48 #include "sp_tile_cache.h"
49 #include "sp_tex_tile_cache.h"
50 #include "sp_texture.h"
51 #include "sp_query.h"
52
53
54
55 /**
56 * Map any drawing surfaces which aren't already mapped
57 */
58 void
59 softpipe_map_transfers(struct softpipe_context *sp)
60 {
61 unsigned i;
62
63 for (i = 0; i < sp->framebuffer.nr_cbufs; i++) {
64 sp_tile_cache_map_transfers(sp->cbuf_cache[i]);
65 }
66
67 sp_tile_cache_map_transfers(sp->zsbuf_cache);
68 }
69
70
71 /**
72 * Unmap any mapped drawing surfaces
73 */
74 void
75 softpipe_unmap_transfers(struct softpipe_context *sp)
76 {
77 uint i;
78
79 for (i = 0; i < sp->framebuffer.nr_cbufs; i++) {
80 sp_tile_cache_unmap_transfers(sp->cbuf_cache[i]);
81 }
82
83 sp_tile_cache_unmap_transfers(sp->zsbuf_cache);
84 }
85
86
87 static void
88 softpipe_destroy( struct pipe_context *pipe )
89 {
90 struct softpipe_context *softpipe = softpipe_context( pipe );
91 uint i;
92
93 if (softpipe->draw)
94 draw_destroy( softpipe->draw );
95
96 if (softpipe->quad.shade)
97 softpipe->quad.shade->destroy( softpipe->quad.shade );
98
99 if (softpipe->quad.depth_test)
100 softpipe->quad.depth_test->destroy( softpipe->quad.depth_test );
101
102 if (softpipe->quad.blend)
103 softpipe->quad.blend->destroy( softpipe->quad.blend );
104
105 if (softpipe->quad.pstipple)
106 softpipe->quad.pstipple->destroy( softpipe->quad.pstipple );
107
108 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
109 sp_destroy_tile_cache(softpipe->cbuf_cache[i]);
110 pipe_surface_reference(&softpipe->framebuffer.cbufs[i], NULL);
111 }
112
113 sp_destroy_tile_cache(softpipe->zsbuf_cache);
114 pipe_surface_reference(&softpipe->framebuffer.zsbuf, NULL);
115
116 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
117 sp_destroy_tex_tile_cache(softpipe->fragment_tex_cache[i]);
118 pipe_sampler_view_reference(&softpipe->fragment_sampler_views[i], NULL);
119 }
120
121 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
122 sp_destroy_tex_tile_cache(softpipe->vertex_tex_cache[i]);
123 pipe_sampler_view_reference(&softpipe->vertex_sampler_views[i], NULL);
124 }
125
126 for (i = 0; i < PIPE_MAX_GEOMETRY_SAMPLERS; i++) {
127 sp_destroy_tex_tile_cache(softpipe->geometry_tex_cache[i]);
128 pipe_sampler_view_reference(&softpipe->geometry_sampler_views[i], NULL);
129 }
130
131 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
132 uint j;
133
134 for (j = 0; j < PIPE_MAX_CONSTANT_BUFFERS; j++) {
135 if (softpipe->constants[i][j]) {
136 pipe_resource_reference(&softpipe->constants[i][j], NULL);
137 }
138 }
139 }
140
141 for (i = 0; i < softpipe->num_vertex_buffers; i++) {
142 pipe_resource_reference(&softpipe->vertex_buffer[i].buffer, NULL);
143 }
144
145 tgsi_exec_machine_destroy(softpipe->fs_machine);
146
147 FREE( softpipe );
148 }
149
150
151 /**
152 * if (the texture is being used as a framebuffer surface)
153 * return SP_REFERENCED_FOR_WRITE
154 * else if (the texture is a bound texture source)
155 * return SP_REFERENCED_FOR_READ
156 * else
157 * return SP_UNREFERENCED
158 */
159 unsigned int
160 softpipe_is_resource_referenced( struct pipe_context *pipe,
161 struct pipe_resource *texture,
162 unsigned level, int layer)
163 {
164 struct softpipe_context *softpipe = softpipe_context( pipe );
165 unsigned i;
166
167 if (texture->target == PIPE_BUFFER)
168 return SP_UNREFERENCED;
169
170 /* check if any of the bound drawing surfaces are this texture */
171 if (softpipe->dirty_render_cache) {
172 for (i = 0; i < softpipe->framebuffer.nr_cbufs; i++) {
173 if (softpipe->framebuffer.cbufs[i] &&
174 softpipe->framebuffer.cbufs[i]->texture == texture) {
175 return SP_REFERENCED_FOR_WRITE;
176 }
177 }
178 if (softpipe->framebuffer.zsbuf &&
179 softpipe->framebuffer.zsbuf->texture == texture) {
180 return SP_REFERENCED_FOR_WRITE;
181 }
182 }
183
184 /* check if any of the tex_cache textures are this texture */
185 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
186 if (softpipe->fragment_tex_cache[i] &&
187 softpipe->fragment_tex_cache[i]->texture == texture)
188 return SP_REFERENCED_FOR_READ;
189 }
190 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
191 if (softpipe->vertex_tex_cache[i] &&
192 softpipe->vertex_tex_cache[i]->texture == texture)
193 return SP_REFERENCED_FOR_READ;
194 }
195 for (i = 0; i < PIPE_MAX_GEOMETRY_SAMPLERS; i++) {
196 if (softpipe->geometry_tex_cache[i] &&
197 softpipe->geometry_tex_cache[i]->texture == texture)
198 return SP_REFERENCED_FOR_READ;
199 }
200
201 return SP_UNREFERENCED;
202 }
203
204
205
206
207 static void
208 softpipe_render_condition( struct pipe_context *pipe,
209 struct pipe_query *query,
210 uint mode )
211 {
212 struct softpipe_context *softpipe = softpipe_context( pipe );
213
214 softpipe->render_cond_query = query;
215 softpipe->render_cond_mode = mode;
216 }
217
218
219
220 struct pipe_context *
221 softpipe_create_context( struct pipe_screen *screen,
222 void *priv )
223 {
224 struct softpipe_context *softpipe = CALLOC_STRUCT(softpipe_context);
225 uint i;
226
227 util_init_math();
228
229 #ifdef PIPE_ARCH_X86
230 softpipe->use_sse = !debug_get_bool_option( "GALLIUM_NOSSE", FALSE );
231 #else
232 softpipe->use_sse = FALSE;
233 #endif
234
235 softpipe->dump_fs = debug_get_bool_option( "SOFTPIPE_DUMP_FS", FALSE );
236 softpipe->dump_gs = debug_get_bool_option( "SOFTPIPE_DUMP_GS", FALSE );
237
238 softpipe->pipe.winsys = NULL;
239 softpipe->pipe.screen = screen;
240 softpipe->pipe.destroy = softpipe_destroy;
241 softpipe->pipe.priv = priv;
242
243 /* state setters */
244 softpipe_init_blend_funcs(&softpipe->pipe);
245 softpipe_init_clip_funcs(&softpipe->pipe);
246 softpipe_init_query_funcs( softpipe );
247 softpipe_init_rasterizer_funcs(&softpipe->pipe);
248 softpipe_init_sampler_funcs(&softpipe->pipe);
249 softpipe_init_shader_funcs(&softpipe->pipe);
250 softpipe_init_streamout_funcs(&softpipe->pipe);
251 softpipe_init_texture_funcs( &softpipe->pipe );
252 softpipe_init_vertex_funcs(&softpipe->pipe);
253
254 softpipe->pipe.set_framebuffer_state = softpipe_set_framebuffer_state;
255
256 softpipe->pipe.draw_vbo = softpipe_draw_vbo;
257 softpipe->pipe.draw_stream_output = softpipe_draw_stream_output;
258
259 softpipe->pipe.clear = softpipe_clear;
260 softpipe->pipe.flush = softpipe_flush_wrapped;
261
262 softpipe->pipe.render_condition = softpipe_render_condition;
263
264 softpipe->pipe.create_video_decoder = vl_create_decoder;
265 softpipe->pipe.create_video_buffer = vl_video_buffer_create;
266
267 /*
268 * Alloc caches for accessing drawing surfaces and textures.
269 * Must be before quad stage setup!
270 */
271 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++)
272 softpipe->cbuf_cache[i] = sp_create_tile_cache( &softpipe->pipe );
273 softpipe->zsbuf_cache = sp_create_tile_cache( &softpipe->pipe );
274
275 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
276 softpipe->fragment_tex_cache[i] = sp_create_tex_tile_cache( &softpipe->pipe );
277 if (!softpipe->fragment_tex_cache[i])
278 goto fail;
279 }
280
281 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
282 softpipe->vertex_tex_cache[i] = sp_create_tex_tile_cache( &softpipe->pipe );
283 if (!softpipe->vertex_tex_cache[i])
284 goto fail;
285 }
286
287 for (i = 0; i < PIPE_MAX_GEOMETRY_SAMPLERS; i++) {
288 softpipe->geometry_tex_cache[i] = sp_create_tex_tile_cache( &softpipe->pipe );
289 if (!softpipe->geometry_tex_cache[i])
290 goto fail;
291 }
292
293 softpipe->fs_machine = tgsi_exec_machine_create();
294
295 /* setup quad rendering stages */
296 softpipe->quad.shade = sp_quad_shade_stage(softpipe);
297 softpipe->quad.depth_test = sp_quad_depth_test_stage(softpipe);
298 softpipe->quad.blend = sp_quad_blend_stage(softpipe);
299 softpipe->quad.pstipple = sp_quad_polygon_stipple_stage(softpipe);
300
301
302 /*
303 * Create drawing context and plug our rendering stage into it.
304 */
305 softpipe->draw = draw_create(&softpipe->pipe);
306 if (!softpipe->draw)
307 goto fail;
308
309 draw_texture_samplers(softpipe->draw,
310 PIPE_SHADER_VERTEX,
311 PIPE_MAX_VERTEX_SAMPLERS,
312 (struct tgsi_sampler **)
313 softpipe->tgsi.vert_samplers_list);
314
315 draw_texture_samplers(softpipe->draw,
316 PIPE_SHADER_GEOMETRY,
317 PIPE_MAX_GEOMETRY_SAMPLERS,
318 (struct tgsi_sampler **)
319 softpipe->tgsi.geom_samplers_list);
320
321 if (debug_get_bool_option( "SOFTPIPE_NO_RAST", FALSE ))
322 softpipe->no_rast = TRUE;
323
324 softpipe->vbuf_backend = sp_create_vbuf_backend(softpipe);
325 if (!softpipe->vbuf_backend)
326 goto fail;
327
328 softpipe->vbuf = draw_vbuf_stage(softpipe->draw, softpipe->vbuf_backend);
329 if (!softpipe->vbuf)
330 goto fail;
331
332 draw_set_rasterize_stage(softpipe->draw, softpipe->vbuf);
333 draw_set_render(softpipe->draw, softpipe->vbuf_backend);
334
335
336 /* plug in AA line/point stages */
337 draw_install_aaline_stage(softpipe->draw, &softpipe->pipe);
338 draw_install_aapoint_stage(softpipe->draw, &softpipe->pipe);
339
340 /* Do polygon stipple w/ texture map + frag prog? */
341 #if DO_PSTIPPLE_IN_DRAW_MODULE
342 draw_install_pstipple_stage(softpipe->draw, &softpipe->pipe);
343 #endif
344
345 draw_wide_point_sprites(softpipe->draw, TRUE);
346
347 sp_init_surface_functions(softpipe);
348
349 return &softpipe->pipe;
350
351 fail:
352 softpipe_destroy(&softpipe->pipe);
353 return NULL;
354 }