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