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