softpipe: tell draw about the vertex layout we want
[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 }
348 else {
349 softpipe->fs_variant = NULL;
350 }
351
352 /* This would be the logical place to pass the fragment shader
353 * to the draw module. However, doing this here, during state
354 * validation, causes problems with the 'draw' module helpers for
355 * wide/AA/stippled lines.
356 * In principle, the draw's fragment shader should be per-variant
357 * but that doesn't work. So we use a single draw fragment shader
358 * per fragment shader, not per variant.
359 */
360 #if 0
361 if (softpipe->fs_variant) {
362 draw_bind_fragment_shader(softpipe->draw,
363 softpipe->fs_variant->draw_shader);
364 }
365 else {
366 draw_bind_fragment_shader(softpipe->draw, NULL);
367 }
368 #endif
369 }
370
371
372 /**
373 * This should be called when the polygon stipple pattern changes.
374 * We create a new texture from the stipple pattern and create a new
375 * sampler view.
376 */
377 static void
378 update_polygon_stipple_pattern(struct softpipe_context *softpipe)
379 {
380 struct pipe_resource *tex;
381 struct pipe_sampler_view *view;
382
383 tex = util_pstipple_create_stipple_texture(&softpipe->pipe,
384 softpipe->poly_stipple.stipple);
385 pipe_resource_reference(&softpipe->pstipple.texture, tex);
386 pipe_resource_reference(&tex, NULL);
387
388 view = util_pstipple_create_sampler_view(&softpipe->pipe,
389 softpipe->pstipple.texture);
390 pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, view);
391 pipe_sampler_view_reference(&view, NULL);
392 }
393
394
395 /**
396 * Should be called when polygon stipple is enabled/disabled or when
397 * the fragment shader changes.
398 * We add/update the fragment sampler and sampler views to sample from
399 * the polygon stipple texture. The texture unit that we use depends on
400 * the fragment shader (we need to use a unit not otherwise used by the
401 * shader).
402 */
403 static void
404 update_polygon_stipple_enable(struct softpipe_context *softpipe, unsigned prim)
405 {
406 if (prim == PIPE_PRIM_TRIANGLES &&
407 softpipe->fs_variant->key.polygon_stipple) {
408 const unsigned unit = softpipe->fs_variant->stipple_sampler_unit;
409
410 /* sampler state */
411 softpipe->samplers[PIPE_SHADER_FRAGMENT][unit] = softpipe->pstipple.sampler;
412
413 /* sampler view state */
414 softpipe_set_sampler_views(&softpipe->pipe, PIPE_SHADER_FRAGMENT,
415 unit, 1, &softpipe->pstipple.sampler_view);
416
417 softpipe->dirty |= SP_NEW_SAMPLER;
418 }
419 }
420
421
422 /* Hopefully this will remain quite simple, otherwise need to pull in
423 * something like the state tracker mechanism.
424 */
425 void
426 softpipe_update_derived(struct softpipe_context *softpipe, unsigned prim)
427 {
428 struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);
429
430 /* Check for updated textures.
431 */
432 if (softpipe->tex_timestamp != sp_screen->timestamp) {
433 softpipe->tex_timestamp = sp_screen->timestamp;
434 softpipe->dirty |= SP_NEW_TEXTURE;
435 }
436
437 #if DO_PSTIPPLE_IN_HELPER_MODULE
438 if (softpipe->dirty & SP_NEW_STIPPLE)
439 /* before updating samplers! */
440 update_polygon_stipple_pattern(softpipe);
441 #endif
442
443 if (softpipe->dirty & (SP_NEW_RASTERIZER |
444 SP_NEW_FS))
445 update_fragment_shader(softpipe, prim);
446
447 #if DO_PSTIPPLE_IN_HELPER_MODULE
448 if (softpipe->dirty & (SP_NEW_RASTERIZER |
449 SP_NEW_STIPPLE |
450 SP_NEW_FS))
451 update_polygon_stipple_enable(softpipe, prim);
452 #endif
453
454 /* TODO: this looks suboptimal */
455 if (softpipe->dirty & (SP_NEW_SAMPLER |
456 SP_NEW_TEXTURE |
457 SP_NEW_FS |
458 SP_NEW_VS))
459 update_tgsi_samplers( softpipe );
460
461 if (softpipe->dirty & (SP_NEW_RASTERIZER |
462 SP_NEW_FS |
463 SP_NEW_VS))
464 invalidate_vertex_layout( softpipe );
465
466 if (softpipe->dirty & (SP_NEW_SCISSOR |
467 SP_NEW_RASTERIZER |
468 SP_NEW_FRAMEBUFFER))
469 compute_cliprect(softpipe);
470
471 if (softpipe->dirty & (SP_NEW_BLEND |
472 SP_NEW_DEPTH_STENCIL_ALPHA |
473 SP_NEW_FRAMEBUFFER |
474 SP_NEW_STIPPLE |
475 SP_NEW_FS))
476 sp_build_quad_pipeline(softpipe);
477
478 softpipe->dirty = 0;
479 }