softpipe: fix memory leaks
[mesa.git] / src / gallium / drivers / softpipe / sp_state_derived.c
1 /**************************************************************************
2 *
3 * Copyright 2003 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 "util/u_inlines.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_pstipple.h"
32 #include "pipe/p_shader_tokens.h"
33 #include "draw/draw_context.h"
34 #include "draw/draw_vertex.h"
35 #include "sp_context.h"
36 #include "sp_screen.h"
37 #include "sp_state.h"
38 #include "sp_texture.h"
39 #include "sp_tex_tile_cache.h"
40
41
42 /**
43 * Mark the current vertex layout as "invalid".
44 * We'll validate the vertex layout later, when we start to actually
45 * render a point or line or tri.
46 */
47 static void
48 invalidate_vertex_layout(struct softpipe_context *softpipe)
49 {
50 softpipe->vertex_info.num_attribs = 0;
51 }
52
53
54 /**
55 * The vertex info describes how to convert the post-transformed vertices
56 * (simple float[][4]) used by the 'draw' module into vertices for
57 * rasterization.
58 *
59 * This function validates the vertex layout and returns a pointer to a
60 * vertex_info object.
61 */
62 struct vertex_info *
63 softpipe_get_vertex_info(struct softpipe_context *softpipe)
64 {
65 struct vertex_info *vinfo = &softpipe->vertex_info;
66
67 if (vinfo->num_attribs == 0) {
68 /* compute vertex layout now */
69 const struct tgsi_shader_info *fsInfo = &softpipe->fs_variant->info;
70 struct vertex_info *vinfo_vbuf = &softpipe->vertex_info_vbuf;
71 const uint num = draw_num_shader_outputs(softpipe->draw);
72 uint i;
73
74 /* Tell draw_vbuf to simply emit the whole post-xform vertex
75 * as-is. No longer any need to try and emit draw vertex_header
76 * info.
77 */
78 vinfo_vbuf->num_attribs = 0;
79 for (i = 0; i < num; i++) {
80 draw_emit_vertex_attr(vinfo_vbuf, EMIT_4F, INTERP_PERSPECTIVE, i);
81 }
82 draw_compute_vertex_size(vinfo_vbuf);
83
84 /*
85 * Loop over fragment shader inputs, searching for the matching output
86 * from the vertex shader.
87 */
88 vinfo->num_attribs = 0;
89 for (i = 0; i < fsInfo->num_inputs; i++) {
90 int src;
91 enum interp_mode interp;
92
93 switch (fsInfo->input_interpolate[i]) {
94 case TGSI_INTERPOLATE_CONSTANT:
95 interp = INTERP_CONSTANT;
96 break;
97 case TGSI_INTERPOLATE_LINEAR:
98 interp = INTERP_LINEAR;
99 break;
100 case TGSI_INTERPOLATE_PERSPECTIVE:
101 interp = INTERP_PERSPECTIVE;
102 break;
103 default:
104 assert(0);
105 interp = INTERP_LINEAR;
106 }
107
108 switch (fsInfo->input_semantic_name[i]) {
109 case TGSI_SEMANTIC_POSITION:
110 interp = INTERP_POS;
111 break;
112
113 case TGSI_SEMANTIC_COLOR:
114 if (softpipe->rasterizer->flatshade) {
115 interp = INTERP_CONSTANT;
116 }
117 break;
118 }
119
120 /* this includes texcoords and varying vars */
121 src = draw_find_shader_output(softpipe->draw,
122 fsInfo->input_semantic_name[i],
123 fsInfo->input_semantic_index[i]);
124 draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
125 }
126
127 softpipe->psize_slot = draw_find_shader_output(softpipe->draw,
128 TGSI_SEMANTIC_PSIZE, 0);
129 if (softpipe->psize_slot > 0) {
130 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT,
131 softpipe->psize_slot);
132 }
133
134 draw_compute_vertex_size(vinfo);
135 }
136
137 return vinfo;
138 }
139
140
141 /**
142 * Called from vbuf module.
143 *
144 * Note that there's actually two different vertex layouts in softpipe.
145 *
146 * The normal one is computed in softpipe_get_vertex_info() above and is
147 * used by the point/line/tri "setup" code.
148 *
149 * The other one (this one) is only used by the vbuf module (which is
150 * not normally used by default but used in testing). For the vbuf module,
151 * we basically want to pass-through the draw module's vertex layout as-is.
152 * When the softpipe vbuf code begins drawing, the normal vertex layout
153 * will come into play again.
154 */
155 struct vertex_info *
156 softpipe_get_vbuf_vertex_info(struct softpipe_context *softpipe)
157 {
158 (void) softpipe_get_vertex_info(softpipe);
159 return &softpipe->vertex_info_vbuf;
160 }
161
162
163 /**
164 * Recompute cliprect from scissor bounds, scissor enable and surface size.
165 */
166 static void
167 compute_cliprect(struct softpipe_context *sp)
168 {
169 /* SP_NEW_FRAMEBUFFER
170 */
171 uint surfWidth = sp->framebuffer.width;
172 uint surfHeight = sp->framebuffer.height;
173
174 /* SP_NEW_RASTERIZER
175 */
176 if (sp->rasterizer->scissor) {
177
178 /* SP_NEW_SCISSOR
179 *
180 * clip to scissor rect:
181 */
182 sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
183 sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
184 sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
185 sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
186 }
187 else {
188 /* clip to surface bounds */
189 sp->cliprect.minx = 0;
190 sp->cliprect.miny = 0;
191 sp->cliprect.maxx = surfWidth;
192 sp->cliprect.maxy = surfHeight;
193 }
194 }
195
196
197 static void
198 update_tgsi_samplers( struct softpipe_context *softpipe )
199 {
200 unsigned i;
201
202 softpipe_reset_sampler_variants( softpipe );
203
204 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
205 struct softpipe_tex_tile_cache *tc = softpipe->fragment_tex_cache[i];
206 if (tc && tc->texture) {
207 struct softpipe_resource *spt = softpipe_resource(tc->texture);
208 if (spt->timestamp != tc->timestamp) {
209 sp_tex_tile_cache_validate_texture( tc );
210 /*
211 _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
212 */
213 tc->timestamp = spt->timestamp;
214 }
215 }
216 }
217
218 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
219 struct softpipe_tex_tile_cache *tc = softpipe->vertex_tex_cache[i];
220
221 if (tc && tc->texture) {
222 struct softpipe_resource *spt = softpipe_resource(tc->texture);
223
224 if (spt->timestamp != tc->timestamp) {
225 sp_tex_tile_cache_validate_texture(tc);
226 tc->timestamp = spt->timestamp;
227 }
228 }
229 }
230
231 for (i = 0; i < PIPE_MAX_GEOMETRY_SAMPLERS; i++) {
232 struct softpipe_tex_tile_cache *tc = softpipe->geometry_tex_cache[i];
233
234 if (tc && tc->texture) {
235 struct softpipe_resource *spt = softpipe_resource(tc->texture);
236
237 if (spt->timestamp != tc->timestamp) {
238 sp_tex_tile_cache_validate_texture(tc);
239 tc->timestamp = spt->timestamp;
240 }
241 }
242 }
243 }
244
245
246 static void
247 update_fragment_shader(struct softpipe_context *softpipe, unsigned prim)
248 {
249 struct sp_fragment_shader_variant_key key;
250
251 memset(&key, 0, sizeof(key));
252
253 if (prim == PIPE_PRIM_TRIANGLES)
254 key.polygon_stipple = softpipe->rasterizer->poly_stipple_enable;
255
256 if (softpipe->fs) {
257 softpipe->fs_variant = softpipe_find_fs_variant(softpipe,
258 softpipe->fs, &key);
259 }
260 else {
261 softpipe->fs_variant = NULL;
262 }
263
264 /* This would be the logical place to pass the fragment shader
265 * to the draw module. However, doing this here, during state
266 * validation, causes problems with the 'draw' module helpers for
267 * wide/AA/stippled lines.
268 * In principle, the draw's fragment shader should be per-variant
269 * but that doesn't work. So we use a single draw fragment shader
270 * per fragment shader, not per variant.
271 */
272 #if 0
273 if (softpipe->fs_variant) {
274 draw_bind_fragment_shader(softpipe->draw,
275 softpipe->fs_variant->draw_shader);
276 }
277 else {
278 draw_bind_fragment_shader(softpipe->draw, NULL);
279 }
280 #endif
281 }
282
283
284 /**
285 * This should be called when the polygon stipple pattern changes.
286 * We create a new texture from the stipple pattern and create a new
287 * sampler view.
288 */
289 static void
290 update_polygon_stipple_pattern(struct softpipe_context *softpipe)
291 {
292 struct pipe_resource *tex;
293 struct pipe_sampler_view *view;
294
295 tex = util_pstipple_create_stipple_texture(&softpipe->pipe,
296 softpipe->poly_stipple.stipple);
297 pipe_resource_reference(&softpipe->pstipple.texture, tex);
298 pipe_resource_reference(&tex, NULL);
299
300 view = util_pstipple_create_sampler_view(&softpipe->pipe,
301 softpipe->pstipple.texture);
302 pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, view);
303 pipe_sampler_view_reference(&view, NULL);
304 }
305
306
307 /**
308 * Should be called when polygon stipple is enabled/disabled or when
309 * the fragment shader changes.
310 * We add/update the fragment sampler and sampler views to sample from
311 * the polygon stipple texture. The texture unit that we use depends on
312 * the fragment shader (we need to use a unit not otherwise used by the
313 * shader).
314 */
315 static void
316 update_polygon_stipple_enable(struct softpipe_context *softpipe, unsigned prim)
317 {
318 if (prim == PIPE_PRIM_TRIANGLES &&
319 softpipe->fs_variant->key.polygon_stipple) {
320 const unsigned unit = softpipe->fs_variant->stipple_sampler_unit;
321
322 assert(unit >= softpipe->num_fragment_samplers);
323
324 /* sampler state */
325 softpipe->fragment_samplers[unit] = softpipe->pstipple.sampler;
326
327 /* sampler view */
328 pipe_sampler_view_reference(&softpipe->fragment_sampler_views[unit],
329 softpipe->pstipple.sampler_view);
330
331 sp_tex_tile_cache_set_sampler_view(softpipe->fragment_tex_cache[unit],
332 softpipe->pstipple.sampler_view);
333
334 softpipe->dirty |= SP_NEW_SAMPLER;
335 }
336 }
337
338
339 /* Hopefully this will remain quite simple, otherwise need to pull in
340 * something like the state tracker mechanism.
341 */
342 void
343 softpipe_update_derived(struct softpipe_context *softpipe, unsigned prim)
344 {
345 struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);
346
347 /* Check for updated textures.
348 */
349 if (softpipe->tex_timestamp != sp_screen->timestamp) {
350 softpipe->tex_timestamp = sp_screen->timestamp;
351 softpipe->dirty |= SP_NEW_TEXTURE;
352 }
353
354 #if DO_PSTIPPLE_IN_HELPER_MODULE
355 if (softpipe->dirty & SP_NEW_STIPPLE)
356 /* before updating samplers! */
357 update_polygon_stipple_pattern(softpipe);
358 #endif
359
360 if (softpipe->dirty & (SP_NEW_RASTERIZER |
361 SP_NEW_FS))
362 update_fragment_shader(softpipe, prim);
363
364 #if DO_PSTIPPLE_IN_HELPER_MODULE
365 if (softpipe->dirty & (SP_NEW_RASTERIZER |
366 SP_NEW_STIPPLE |
367 SP_NEW_FS))
368 update_polygon_stipple_enable(softpipe, prim);
369 #endif
370
371 if (softpipe->dirty & (SP_NEW_SAMPLER |
372 SP_NEW_TEXTURE |
373 SP_NEW_FS |
374 SP_NEW_VS))
375 update_tgsi_samplers( softpipe );
376
377 if (softpipe->dirty & (SP_NEW_RASTERIZER |
378 SP_NEW_FS |
379 SP_NEW_VS))
380 invalidate_vertex_layout( softpipe );
381
382 if (softpipe->dirty & (SP_NEW_SCISSOR |
383 SP_NEW_RASTERIZER |
384 SP_NEW_FRAMEBUFFER))
385 compute_cliprect(softpipe);
386
387 if (softpipe->dirty & (SP_NEW_BLEND |
388 SP_NEW_DEPTH_STENCIL_ALPHA |
389 SP_NEW_FRAMEBUFFER |
390 SP_NEW_FS))
391 sp_build_quad_pipeline(softpipe);
392
393 softpipe->dirty = 0;
394 }