softpipe: fix mapping of "special" vs outputs
[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->vertex_info.num_attribs = 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 and returns a pointer to a
61 * vertex_info object.
62 */
63 struct vertex_info *
64 softpipe_get_vertex_info(struct softpipe_context *softpipe)
65 {
66 struct vertex_info *vinfo = &softpipe->vertex_info;
67 int vs_index;
68
69 if (vinfo->num_attribs == 0) {
70 /* compute vertex layout now */
71 const struct tgsi_shader_info *fsInfo = &softpipe->fs_variant->info;
72 struct vertex_info *vinfo_vbuf = &softpipe->vertex_info_vbuf;
73 const uint num = draw_num_shader_outputs(softpipe->draw);
74 uint i;
75
76 /* Tell draw_vbuf to simply emit the whole post-xform vertex
77 * as-is. No longer any need to try and emit draw vertex_header
78 * info.
79 */
80 vinfo_vbuf->num_attribs = 0;
81 for (i = 0; i < num; i++) {
82 draw_emit_vertex_attr(vinfo_vbuf, EMIT_4F, INTERP_PERSPECTIVE, i);
83 }
84 draw_compute_vertex_size(vinfo_vbuf);
85
86 softpipe->viewport_index_slot = 0;
87 softpipe->layer_slot = 0;
88 softpipe->psize_slot = 0;
89
90 /*
91 * Loop over fragment shader inputs, searching for the matching output
92 * from the vertex shader.
93 */
94 vinfo->num_attribs = 0;
95 for (i = 0; i < fsInfo->num_inputs; i++) {
96 int src;
97 enum interp_mode interp = INTERP_LINEAR;
98
99 switch (fsInfo->input_interpolate[i]) {
100 case TGSI_INTERPOLATE_CONSTANT:
101 interp = INTERP_CONSTANT;
102 break;
103 case TGSI_INTERPOLATE_LINEAR:
104 interp = INTERP_LINEAR;
105 break;
106 case TGSI_INTERPOLATE_PERSPECTIVE:
107 interp = INTERP_PERSPECTIVE;
108 break;
109 case TGSI_INTERPOLATE_COLOR:
110 assert(fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR);
111 break;
112 default:
113 assert(0);
114 }
115
116 switch (fsInfo->input_semantic_name[i]) {
117 case TGSI_SEMANTIC_POSITION:
118 interp = INTERP_POS;
119 break;
120
121 case TGSI_SEMANTIC_COLOR:
122 if (fsInfo->input_interpolate[i] == TGSI_INTERPOLATE_COLOR) {
123 if (softpipe->rasterizer->flatshade)
124 interp = INTERP_CONSTANT;
125 else
126 interp = INTERP_PERSPECTIVE;
127 }
128 break;
129 }
130
131 /* this includes texcoords and varying vars */
132 src = draw_find_shader_output(softpipe->draw,
133 fsInfo->input_semantic_name[i],
134 fsInfo->input_semantic_index[i]);
135 if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR && src == -1)
136 /*
137 * try and find a bcolor.
138 * Note that if there's both front and back color, draw will
139 * have copied back to front color already.
140 */
141 src = draw_find_shader_output(softpipe->draw,
142 TGSI_SEMANTIC_BCOLOR,
143 fsInfo->input_semantic_index[i]);
144
145 draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
146 }
147
148 /* Figure out if we need pointsize as well. */
149 vs_index = draw_find_shader_output(softpipe->draw,
150 TGSI_SEMANTIC_PSIZE, 0);
151
152 if (vs_index >= 0) {
153 softpipe->psize_slot = vs_index;
154 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
155 }
156
157 /* Figure out if we need viewport index */
158 vs_index = draw_find_shader_output(softpipe->draw,
159 TGSI_SEMANTIC_VIEWPORT_INDEX,
160 0);
161 if (vs_index >= 0) {
162 softpipe->viewport_index_slot = vs_index;
163 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
164 }
165
166 /* Figure out if we need layer */
167 vs_index = draw_find_shader_output(softpipe->draw,
168 TGSI_SEMANTIC_LAYER,
169 0);
170 if (vs_index >= 0) {
171 softpipe->layer_slot = vs_index;
172 draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT, vs_index);
173 }
174
175 draw_compute_vertex_size(vinfo);
176 }
177
178 return vinfo;
179 }
180
181
182 /**
183 * Called from vbuf module.
184 *
185 * Note that there's actually two different vertex layouts in softpipe.
186 *
187 * The normal one is computed in softpipe_get_vertex_info() above and is
188 * used by the point/line/tri "setup" code.
189 *
190 * The other one (this one) is only used by the vbuf module (which is
191 * not normally used by default but used in testing). For the vbuf module,
192 * we basically want to pass-through the draw module's vertex layout as-is.
193 * When the softpipe vbuf code begins drawing, the normal vertex layout
194 * will come into play again.
195 */
196 struct vertex_info *
197 softpipe_get_vbuf_vertex_info(struct softpipe_context *softpipe)
198 {
199 (void) softpipe_get_vertex_info(softpipe);
200 return &softpipe->vertex_info_vbuf;
201 }
202
203
204 /**
205 * Recompute cliprect from scissor bounds, scissor enable and surface size.
206 */
207 static void
208 compute_cliprect(struct softpipe_context *sp)
209 {
210 unsigned i;
211 /* SP_NEW_FRAMEBUFFER
212 */
213 uint surfWidth = sp->framebuffer.width;
214 uint surfHeight = sp->framebuffer.height;
215
216 for (i = 0; i < PIPE_MAX_VIEWPORTS; i++) {
217 /* SP_NEW_RASTERIZER
218 */
219 if (sp->rasterizer->scissor) {
220
221 /* SP_NEW_SCISSOR
222 *
223 * clip to scissor rect:
224 */
225 sp->cliprect[i].minx = MAX2(sp->scissors[i].minx, 0);
226 sp->cliprect[i].miny = MAX2(sp->scissors[i].miny, 0);
227 sp->cliprect[i].maxx = MIN2(sp->scissors[i].maxx, surfWidth);
228 sp->cliprect[i].maxy = MIN2(sp->scissors[i].maxy, surfHeight);
229 }
230 else {
231 /* clip to surface bounds */
232 sp->cliprect[i].minx = 0;
233 sp->cliprect[i].miny = 0;
234 sp->cliprect[i].maxx = surfWidth;
235 sp->cliprect[i].maxy = surfHeight;
236 }
237 }
238 }
239
240
241 static void
242 set_shader_sampler(struct softpipe_context *softpipe,
243 unsigned shader,
244 int max_sampler)
245 {
246 int i;
247 for (i = 0; i <= max_sampler; i++) {
248 softpipe->tgsi.sampler[shader]->sp_sampler[i] =
249 (struct sp_sampler *)(softpipe->samplers[shader][i]);
250 }
251 }
252
253 static void
254 update_tgsi_samplers( struct softpipe_context *softpipe )
255 {
256 unsigned i, sh;
257
258 set_shader_sampler(softpipe, PIPE_SHADER_VERTEX,
259 softpipe->vs->max_sampler);
260 set_shader_sampler(softpipe, PIPE_SHADER_FRAGMENT,
261 softpipe->fs_variant->info.file_max[TGSI_FILE_SAMPLER]);
262 if (softpipe->gs) {
263 set_shader_sampler(softpipe, PIPE_SHADER_GEOMETRY,
264 softpipe->gs->max_sampler);
265 }
266
267 /* XXX is this really necessary here??? */
268 for (sh = 0; sh < Elements(softpipe->tex_cache); sh++) {
269 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
270 struct softpipe_tex_tile_cache *tc = softpipe->tex_cache[sh][i];
271 if (tc && tc->texture) {
272 struct softpipe_resource *spt = softpipe_resource(tc->texture);
273 if (spt->timestamp != tc->timestamp) {
274 sp_tex_tile_cache_validate_texture( tc );
275 /*
276 _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
277 */
278 tc->timestamp = spt->timestamp;
279 }
280 }
281 }
282 }
283 }
284
285
286 static void
287 update_fragment_shader(struct softpipe_context *softpipe, unsigned prim)
288 {
289 struct sp_fragment_shader_variant_key key;
290
291 memset(&key, 0, sizeof(key));
292
293 if (prim == PIPE_PRIM_TRIANGLES)
294 key.polygon_stipple = softpipe->rasterizer->poly_stipple_enable;
295
296 if (softpipe->fs) {
297 softpipe->fs_variant = softpipe_find_fs_variant(softpipe,
298 softpipe->fs, &key);
299
300 /* prepare the TGSI interpreter for FS execution */
301 softpipe->fs_variant->prepare(softpipe->fs_variant,
302 softpipe->fs_machine,
303 (struct tgsi_sampler *) softpipe->
304 tgsi.sampler[PIPE_SHADER_FRAGMENT]);
305 }
306 else {
307 softpipe->fs_variant = NULL;
308 }
309
310 /* This would be the logical place to pass the fragment shader
311 * to the draw module. However, doing this here, during state
312 * validation, causes problems with the 'draw' module helpers for
313 * wide/AA/stippled lines.
314 * In principle, the draw's fragment shader should be per-variant
315 * but that doesn't work. So we use a single draw fragment shader
316 * per fragment shader, not per variant.
317 */
318 #if 0
319 if (softpipe->fs_variant) {
320 draw_bind_fragment_shader(softpipe->draw,
321 softpipe->fs_variant->draw_shader);
322 }
323 else {
324 draw_bind_fragment_shader(softpipe->draw, NULL);
325 }
326 #endif
327 }
328
329
330 /**
331 * This should be called when the polygon stipple pattern changes.
332 * We create a new texture from the stipple pattern and create a new
333 * sampler view.
334 */
335 static void
336 update_polygon_stipple_pattern(struct softpipe_context *softpipe)
337 {
338 struct pipe_resource *tex;
339 struct pipe_sampler_view *view;
340
341 tex = util_pstipple_create_stipple_texture(&softpipe->pipe,
342 softpipe->poly_stipple.stipple);
343 pipe_resource_reference(&softpipe->pstipple.texture, tex);
344 pipe_resource_reference(&tex, NULL);
345
346 view = util_pstipple_create_sampler_view(&softpipe->pipe,
347 softpipe->pstipple.texture);
348 pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, view);
349 pipe_sampler_view_reference(&view, NULL);
350 }
351
352
353 /**
354 * Should be called when polygon stipple is enabled/disabled or when
355 * the fragment shader changes.
356 * We add/update the fragment sampler and sampler views to sample from
357 * the polygon stipple texture. The texture unit that we use depends on
358 * the fragment shader (we need to use a unit not otherwise used by the
359 * shader).
360 */
361 static void
362 update_polygon_stipple_enable(struct softpipe_context *softpipe, unsigned prim)
363 {
364 if (prim == PIPE_PRIM_TRIANGLES &&
365 softpipe->fs_variant->key.polygon_stipple) {
366 const unsigned unit = softpipe->fs_variant->stipple_sampler_unit;
367
368 /* sampler state */
369 softpipe->samplers[PIPE_SHADER_FRAGMENT][unit] = softpipe->pstipple.sampler;
370
371 /* sampler view state */
372 softpipe_set_sampler_views(&softpipe->pipe, PIPE_SHADER_FRAGMENT,
373 unit, 1, &softpipe->pstipple.sampler_view);
374
375 softpipe->dirty |= SP_NEW_SAMPLER;
376 }
377 }
378
379
380 /* Hopefully this will remain quite simple, otherwise need to pull in
381 * something like the state tracker mechanism.
382 */
383 void
384 softpipe_update_derived(struct softpipe_context *softpipe, unsigned prim)
385 {
386 struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);
387
388 /* Check for updated textures.
389 */
390 if (softpipe->tex_timestamp != sp_screen->timestamp) {
391 softpipe->tex_timestamp = sp_screen->timestamp;
392 softpipe->dirty |= SP_NEW_TEXTURE;
393 }
394
395 #if DO_PSTIPPLE_IN_HELPER_MODULE
396 if (softpipe->dirty & SP_NEW_STIPPLE)
397 /* before updating samplers! */
398 update_polygon_stipple_pattern(softpipe);
399 #endif
400
401 if (softpipe->dirty & (SP_NEW_RASTERIZER |
402 SP_NEW_FS))
403 update_fragment_shader(softpipe, prim);
404
405 #if DO_PSTIPPLE_IN_HELPER_MODULE
406 if (softpipe->dirty & (SP_NEW_RASTERIZER |
407 SP_NEW_STIPPLE |
408 SP_NEW_FS))
409 update_polygon_stipple_enable(softpipe, prim);
410 #endif
411
412 /* TODO: this looks suboptimal */
413 if (softpipe->dirty & (SP_NEW_SAMPLER |
414 SP_NEW_TEXTURE |
415 SP_NEW_FS |
416 SP_NEW_VS))
417 update_tgsi_samplers( softpipe );
418
419 if (softpipe->dirty & (SP_NEW_RASTERIZER |
420 SP_NEW_FS |
421 SP_NEW_VS))
422 invalidate_vertex_layout( softpipe );
423
424 if (softpipe->dirty & (SP_NEW_SCISSOR |
425 SP_NEW_RASTERIZER |
426 SP_NEW_FRAMEBUFFER))
427 compute_cliprect(softpipe);
428
429 if (softpipe->dirty & (SP_NEW_BLEND |
430 SP_NEW_DEPTH_STENCIL_ALPHA |
431 SP_NEW_FRAMEBUFFER |
432 SP_NEW_STIPPLE |
433 SP_NEW_FS))
434 sp_build_quad_pipeline(softpipe);
435
436 softpipe->dirty = 0;
437 }