softpipe: add SSBO/shader atomics support.
[mesa.git] / src / gallium / drivers / softpipe / sp_state_derived.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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_sample.h"
40 #include "sp_tex_tile_cache.h"
41
42
43 /**
44 * Mark the current vertex layout as "invalid".
45 * We'll validate the vertex layout later, when we start to actually
46 * render a point or line or tri.
47 */
48 static void
49 invalidate_vertex_layout(struct softpipe_context *softpipe)
50 {
51 softpipe->setup_info.valid = 0;
52 }
53
54
55 /**
56 * The vertex info describes how to convert the post-transformed vertices
57 * (simple float[][4]) used by the 'draw' module into vertices for
58 * rasterization.
59 *
60 * This function validates the vertex layout.
61 */
62 static void
63 softpipe_compute_vertex_info(struct softpipe_context *softpipe)
64 {
65 struct sp_setup_info *sinfo = &softpipe->setup_info;
66
67 if (sinfo->valid == 0) {
68 const struct tgsi_shader_info *fsInfo = &softpipe->fs_variant->info;
69 struct vertex_info *vinfo = &softpipe->vertex_info;
70 uint i;
71 int vs_index;
72 /*
73 * This doesn't quite work right (wrt face injection, prim id,
74 * wide points) - hit a couple assertions, misrenderings plus
75 * memory corruption. Albeit could fix (the former two) by calling
76 * this "more often" (rasterizer changes etc.). (The latter would
77 * need to be included in draw_prepare_shader_outputs, but it looks
78 * like that would potentially allocate quite some unused additional
79 * vertex outputs.)
80 * draw_prepare_shader_outputs(softpipe->draw);
81 */
82
83 /*
84 * Those can't actually be 0 (because pos is always at 0).
85 * But use ints anyway to avoid confusion (in vs outputs, they
86 * can very well be at pos 0).
87 */
88 softpipe->viewport_index_slot = -1;
89 softpipe->layer_slot = -1;
90 softpipe->psize_slot = -1;
91
92 vinfo->num_attribs = 0;
93
94 /*
95 * Put position always first (setup needs it there).
96 */
97 vs_index = draw_find_shader_output(softpipe->draw,
98 TGSI_SEMANTIC_POSITION, 0);
99
100 draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
101
102 /*
103 * Match FS inputs against VS outputs, emitting the necessary
104 * attributes.
105 */
106 for (i = 0; i < fsInfo->num_inputs; i++) {
107 enum sp_interp_mode interp = SP_INTERP_LINEAR;
108
109 switch (fsInfo->input_interpolate[i]) {
110 case TGSI_INTERPOLATE_CONSTANT:
111 interp = SP_INTERP_CONSTANT;
112 break;
113 case TGSI_INTERPOLATE_LINEAR:
114 interp = SP_INTERP_LINEAR;
115 break;
116 case TGSI_INTERPOLATE_PERSPECTIVE:
117 interp = SP_INTERP_PERSPECTIVE;
118 break;
119 case TGSI_INTERPOLATE_COLOR:
120 assert(fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR);
121 break;
122 default:
123 assert(0);
124 }
125
126 switch (fsInfo->input_semantic_name[i]) {
127 case TGSI_SEMANTIC_POSITION:
128 interp = SP_INTERP_POS;
129 break;
130
131 case TGSI_SEMANTIC_COLOR:
132 if (fsInfo->input_interpolate[i] == TGSI_INTERPOLATE_COLOR) {
133 if (softpipe->rasterizer->flatshade)
134 interp = SP_INTERP_CONSTANT;
135 else
136 interp = SP_INTERP_PERSPECTIVE;
137 }
138 break;
139 }
140
141 /*
142 * Search for each input in current vs output:
143 */
144 vs_index = draw_find_shader_output(softpipe->draw,
145 fsInfo->input_semantic_name[i],
146 fsInfo->input_semantic_index[i]);
147
148 if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR &&
149 vs_index == -1) {
150 /*
151 * try and find a bcolor.
152 * Note that if there's both front and back color, draw will
153 * have copied back to front color already.
154 */
155 vs_index = draw_find_shader_output(softpipe->draw,
156 TGSI_SEMANTIC_BCOLOR,
157 fsInfo->input_semantic_index[i]);
158 }
159
160 sinfo->attrib[i].interp = interp;
161 /* extremely pointless index map */
162 sinfo->attrib[i].src_index = i + 1;
163 /*
164 * For vp index and layer, if the fs requires them but the vs doesn't
165 * provide them, draw (vbuf) will give us the required 0 (slot -1).
166 * (This means in this case we'll also use those slots in setup, which
167 * isn't necessary but they'll contain the correct (0) value.)
168 */
169 if (fsInfo->input_semantic_name[i] ==
170 TGSI_SEMANTIC_VIEWPORT_INDEX) {
171 softpipe->viewport_index_slot = (int)vinfo->num_attribs;
172 draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
173 } else if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_LAYER) {
174 softpipe->layer_slot = (int)vinfo->num_attribs;
175 draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
176 /*
177 * Note that we'd actually want to skip position (as we won't use
178 * the attribute in the fs) but can't. The reason is that we don't
179 * actually have a input/output map for setup (even though it looks
180 * like we do...). Could adjust for this though even without a map.
181 */
182 } else {
183 /*
184 * Note that we'd actually want to skip position (as we won't use
185 * the attribute in the fs) but can't. The reason is that we don't
186 * actually have a input/output map for setup (even though it looks
187 * like we do...). Could adjust for this though even without a map.
188 */
189 draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
190 }
191 }
192
193 /* Figure out if we need pointsize as well.
194 */
195 vs_index = draw_find_shader_output(softpipe->draw,
196 TGSI_SEMANTIC_PSIZE, 0);
197
198 if (vs_index >= 0) {
199 softpipe->psize_slot = (int)vinfo->num_attribs;
200 draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
201 }
202
203 /* Figure out if we need viewport index (if it wasn't already in fs input) */
204 if (softpipe->viewport_index_slot < 0) {
205 vs_index = draw_find_shader_output(softpipe->draw,
206 TGSI_SEMANTIC_VIEWPORT_INDEX,
207 0);
208 if (vs_index >= 0) {
209 softpipe->viewport_index_slot =(int)vinfo->num_attribs;
210 draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
211 }
212 }
213
214 /* Figure out if we need layer (if it wasn't already in fs input) */
215 if (softpipe->layer_slot < 0) {
216 vs_index = draw_find_shader_output(softpipe->draw,
217 TGSI_SEMANTIC_LAYER,
218 0);
219 if (vs_index >= 0) {
220 softpipe->layer_slot = (int)vinfo->num_attribs;
221 draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
222 }
223 }
224
225 draw_compute_vertex_size(vinfo);
226 softpipe->setup_info.valid = 1;
227 }
228 return;
229 }
230
231
232 /**
233 * Called from vbuf module.
234 *
235 * This will trigger validation of the vertex layout (and also compute
236 * the required information for setup).
237 */
238 struct vertex_info *
239 softpipe_get_vbuf_vertex_info(struct softpipe_context *softpipe)
240 {
241 softpipe_compute_vertex_info(softpipe);
242 return &softpipe->vertex_info;
243 }
244
245
246 /**
247 * Recompute cliprect from scissor bounds, scissor enable and surface size.
248 */
249 static void
250 compute_cliprect(struct softpipe_context *sp)
251 {
252 unsigned i;
253 /* SP_NEW_FRAMEBUFFER
254 */
255 uint surfWidth = sp->framebuffer.width;
256 uint surfHeight = sp->framebuffer.height;
257
258 for (i = 0; i < PIPE_MAX_VIEWPORTS; i++) {
259 /* SP_NEW_RASTERIZER
260 */
261 if (sp->rasterizer->scissor) {
262
263 /* SP_NEW_SCISSOR
264 *
265 * clip to scissor rect:
266 */
267 sp->cliprect[i].minx = MAX2(sp->scissors[i].minx, 0);
268 sp->cliprect[i].miny = MAX2(sp->scissors[i].miny, 0);
269 sp->cliprect[i].maxx = MIN2(sp->scissors[i].maxx, surfWidth);
270 sp->cliprect[i].maxy = MIN2(sp->scissors[i].maxy, surfHeight);
271 }
272 else {
273 /* clip to surface bounds */
274 sp->cliprect[i].minx = 0;
275 sp->cliprect[i].miny = 0;
276 sp->cliprect[i].maxx = surfWidth;
277 sp->cliprect[i].maxy = surfHeight;
278 }
279 }
280 }
281
282
283 static void
284 set_shader_sampler(struct softpipe_context *softpipe,
285 unsigned shader,
286 int max_sampler)
287 {
288 int i;
289 for (i = 0; i <= max_sampler; i++) {
290 softpipe->tgsi.sampler[shader]->sp_sampler[i] =
291 (struct sp_sampler *)(softpipe->samplers[shader][i]);
292 }
293 }
294
295 static void
296 update_tgsi_samplers( struct softpipe_context *softpipe )
297 {
298 unsigned i, sh;
299
300 set_shader_sampler(softpipe, PIPE_SHADER_VERTEX,
301 softpipe->vs->max_sampler);
302 set_shader_sampler(softpipe, PIPE_SHADER_FRAGMENT,
303 softpipe->fs_variant->info.file_max[TGSI_FILE_SAMPLER]);
304 if (softpipe->gs) {
305 set_shader_sampler(softpipe, PIPE_SHADER_GEOMETRY,
306 softpipe->gs->max_sampler);
307 }
308
309 /* XXX is this really necessary here??? */
310 for (sh = 0; sh < Elements(softpipe->tex_cache); sh++) {
311 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
312 struct softpipe_tex_tile_cache *tc = softpipe->tex_cache[sh][i];
313 if (tc && tc->texture) {
314 struct softpipe_resource *spt = softpipe_resource(tc->texture);
315 if (spt->timestamp != tc->timestamp) {
316 sp_tex_tile_cache_validate_texture( tc );
317 /*
318 _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
319 */
320 tc->timestamp = spt->timestamp;
321 }
322 }
323 }
324 }
325 }
326
327
328 static void
329 update_fragment_shader(struct softpipe_context *softpipe, unsigned prim)
330 {
331 struct sp_fragment_shader_variant_key key;
332
333 memset(&key, 0, sizeof(key));
334
335 if (prim == PIPE_PRIM_TRIANGLES)
336 key.polygon_stipple = softpipe->rasterizer->poly_stipple_enable;
337
338 if (softpipe->fs) {
339 softpipe->fs_variant = softpipe_find_fs_variant(softpipe,
340 softpipe->fs, &key);
341
342 /* prepare the TGSI interpreter for FS execution */
343 softpipe->fs_variant->prepare(softpipe->fs_variant,
344 softpipe->fs_machine,
345 (struct tgsi_sampler *) softpipe->
346 tgsi.sampler[PIPE_SHADER_FRAGMENT],
347 (struct tgsi_image *)softpipe->tgsi.image[PIPE_SHADER_FRAGMENT],
348 (struct tgsi_buffer *)softpipe->tgsi.buffer[PIPE_SHADER_FRAGMENT]);
349 }
350 else {
351 softpipe->fs_variant = NULL;
352 }
353
354 /* This would be the logical place to pass the fragment shader
355 * to the draw module. However, doing this here, during state
356 * validation, causes problems with the 'draw' module helpers for
357 * wide/AA/stippled lines.
358 * In principle, the draw's fragment shader should be per-variant
359 * but that doesn't work. So we use a single draw fragment shader
360 * per fragment shader, not per variant.
361 */
362 #if 0
363 if (softpipe->fs_variant) {
364 draw_bind_fragment_shader(softpipe->draw,
365 softpipe->fs_variant->draw_shader);
366 }
367 else {
368 draw_bind_fragment_shader(softpipe->draw, NULL);
369 }
370 #endif
371 }
372
373
374 /**
375 * This should be called when the polygon stipple pattern changes.
376 * We create a new texture from the stipple pattern and create a new
377 * sampler view.
378 */
379 static void
380 update_polygon_stipple_pattern(struct softpipe_context *softpipe)
381 {
382 struct pipe_resource *tex;
383 struct pipe_sampler_view *view;
384
385 tex = util_pstipple_create_stipple_texture(&softpipe->pipe,
386 softpipe->poly_stipple.stipple);
387 pipe_resource_reference(&softpipe->pstipple.texture, tex);
388 pipe_resource_reference(&tex, NULL);
389
390 view = util_pstipple_create_sampler_view(&softpipe->pipe,
391 softpipe->pstipple.texture);
392 pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, view);
393 pipe_sampler_view_reference(&view, NULL);
394 }
395
396
397 /**
398 * Should be called when polygon stipple is enabled/disabled or when
399 * the fragment shader changes.
400 * We add/update the fragment sampler and sampler views to sample from
401 * the polygon stipple texture. The texture unit that we use depends on
402 * the fragment shader (we need to use a unit not otherwise used by the
403 * shader).
404 */
405 static void
406 update_polygon_stipple_enable(struct softpipe_context *softpipe, unsigned prim)
407 {
408 if (prim == PIPE_PRIM_TRIANGLES &&
409 softpipe->fs_variant->key.polygon_stipple) {
410 const unsigned unit = softpipe->fs_variant->stipple_sampler_unit;
411
412 /* sampler state */
413 softpipe->samplers[PIPE_SHADER_FRAGMENT][unit] = softpipe->pstipple.sampler;
414
415 /* sampler view state */
416 softpipe_set_sampler_views(&softpipe->pipe, PIPE_SHADER_FRAGMENT,
417 unit, 1, &softpipe->pstipple.sampler_view);
418
419 softpipe->dirty |= SP_NEW_SAMPLER;
420 }
421 }
422
423
424 /* Hopefully this will remain quite simple, otherwise need to pull in
425 * something like the state tracker mechanism.
426 */
427 void
428 softpipe_update_derived(struct softpipe_context *softpipe, unsigned prim)
429 {
430 struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);
431
432 /* Check for updated textures.
433 */
434 if (softpipe->tex_timestamp != sp_screen->timestamp) {
435 softpipe->tex_timestamp = sp_screen->timestamp;
436 softpipe->dirty |= SP_NEW_TEXTURE;
437 }
438
439 #if DO_PSTIPPLE_IN_HELPER_MODULE
440 if (softpipe->dirty & SP_NEW_STIPPLE)
441 /* before updating samplers! */
442 update_polygon_stipple_pattern(softpipe);
443 #endif
444
445 if (softpipe->dirty & (SP_NEW_RASTERIZER |
446 SP_NEW_FS))
447 update_fragment_shader(softpipe, prim);
448
449 #if DO_PSTIPPLE_IN_HELPER_MODULE
450 if (softpipe->dirty & (SP_NEW_RASTERIZER |
451 SP_NEW_STIPPLE |
452 SP_NEW_FS))
453 update_polygon_stipple_enable(softpipe, prim);
454 #endif
455
456 /* TODO: this looks suboptimal */
457 if (softpipe->dirty & (SP_NEW_SAMPLER |
458 SP_NEW_TEXTURE |
459 SP_NEW_FS |
460 SP_NEW_VS))
461 update_tgsi_samplers( softpipe );
462
463 if (softpipe->dirty & (SP_NEW_RASTERIZER |
464 SP_NEW_FS |
465 SP_NEW_VS))
466 invalidate_vertex_layout( softpipe );
467
468 if (softpipe->dirty & (SP_NEW_SCISSOR |
469 SP_NEW_RASTERIZER |
470 SP_NEW_FRAMEBUFFER))
471 compute_cliprect(softpipe);
472
473 if (softpipe->dirty & (SP_NEW_BLEND |
474 SP_NEW_DEPTH_STENCIL_ALPHA |
475 SP_NEW_FRAMEBUFFER |
476 SP_NEW_STIPPLE |
477 SP_NEW_FS))
478 sp_build_quad_pipeline(softpipe);
479
480 softpipe->dirty = 0;
481 }