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
invalidate_vertex_layout(struct softpipe_context * softpipe)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
softpipe_compute_vertex_info(struct softpipe_context * softpipe)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 an 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 an 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 *
softpipe_get_vbuf_vertex_info(struct softpipe_context * softpipe)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
compute_cliprect(struct softpipe_context * sp)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
set_shader_sampler(struct softpipe_context * softpipe,enum pipe_shader_type shader,int max_sampler)284 set_shader_sampler(struct softpipe_context *softpipe,
285                    enum pipe_shader_type 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 void
softpipe_update_compute_samplers(struct softpipe_context * softpipe)296 softpipe_update_compute_samplers(struct softpipe_context *softpipe)
297 {
298    set_shader_sampler(softpipe, PIPE_SHADER_COMPUTE, softpipe->cs->max_sampler);
299 }
300 
301 static void
update_tgsi_samplers(struct softpipe_context * softpipe)302 update_tgsi_samplers( struct softpipe_context *softpipe )
303 {
304    unsigned i, sh;
305 
306    set_shader_sampler(softpipe, PIPE_SHADER_VERTEX,
307                       softpipe->vs->max_sampler);
308    set_shader_sampler(softpipe, PIPE_SHADER_FRAGMENT,
309                       softpipe->fs_variant->info.file_max[TGSI_FILE_SAMPLER]);
310    if (softpipe->gs) {
311       set_shader_sampler(softpipe, PIPE_SHADER_GEOMETRY,
312                          softpipe->gs->max_sampler);
313    }
314 
315    /* XXX is this really necessary here??? */
316    for (sh = 0; sh < ARRAY_SIZE(softpipe->tex_cache); sh++) {
317       for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
318          struct softpipe_tex_tile_cache *tc = softpipe->tex_cache[sh][i];
319          if (tc && tc->texture) {
320             struct softpipe_resource *spt = softpipe_resource(tc->texture);
321             if (spt->timestamp != tc->timestamp) {
322                sp_tex_tile_cache_validate_texture( tc );
323                /*
324                  _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
325                */
326                tc->timestamp = spt->timestamp;
327             }
328          }
329       }
330    }
331 }
332 
333 
334 static void
update_fragment_shader(struct softpipe_context * softpipe,unsigned prim)335 update_fragment_shader(struct softpipe_context *softpipe, unsigned prim)
336 {
337    struct sp_fragment_shader_variant_key key;
338 
339    memset(&key, 0, sizeof(key));
340 
341    if (prim == PIPE_PRIM_TRIANGLES)
342       key.polygon_stipple = softpipe->rasterizer->poly_stipple_enable;
343 
344    if (softpipe->fs) {
345       softpipe->fs_variant = softpipe_find_fs_variant(softpipe,
346                                                       softpipe->fs, &key);
347 
348       /* prepare the TGSI interpreter for FS execution */
349       softpipe->fs_variant->prepare(softpipe->fs_variant,
350                                     softpipe->fs_machine,
351                                     (struct tgsi_sampler *) softpipe->
352                                     tgsi.sampler[PIPE_SHADER_FRAGMENT],
353                                     (struct tgsi_image *)softpipe->tgsi.image[PIPE_SHADER_FRAGMENT],
354                                     (struct tgsi_buffer *)softpipe->tgsi.buffer[PIPE_SHADER_FRAGMENT]);
355    }
356    else {
357       softpipe->fs_variant = NULL;
358    }
359 
360    /* This would be the logical place to pass the fragment shader
361     * to the draw module.  However, doing this here, during state
362     * validation, causes problems with the 'draw' module helpers for
363     * wide/AA/stippled lines.
364     * In principle, the draw's fragment shader should be per-variant
365     * but that doesn't work.  So we use a single draw fragment shader
366     * per fragment shader, not per variant.
367     */
368 #if 0
369    if (softpipe->fs_variant) {
370       draw_bind_fragment_shader(softpipe->draw,
371                                 softpipe->fs_variant->draw_shader);
372    }
373    else {
374       draw_bind_fragment_shader(softpipe->draw, NULL);
375    }
376 #endif
377 }
378 
379 
380 /**
381  * This should be called when the polygon stipple pattern changes.
382  * We create a new texture from the stipple pattern and create a new
383  * sampler view.
384  */
385 static void
update_polygon_stipple_pattern(struct softpipe_context * softpipe)386 update_polygon_stipple_pattern(struct softpipe_context *softpipe)
387 {
388    struct pipe_resource *tex;
389    struct pipe_sampler_view *view;
390 
391    tex = util_pstipple_create_stipple_texture(&softpipe->pipe,
392                                               softpipe->poly_stipple.stipple);
393    pipe_resource_reference(&softpipe->pstipple.texture, tex);
394    pipe_resource_reference(&tex, NULL);
395 
396    view = util_pstipple_create_sampler_view(&softpipe->pipe,
397                                             softpipe->pstipple.texture);
398    pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, view);
399    pipe_sampler_view_reference(&view, NULL);
400 }
401 
402 
403 /**
404  * Should be called when polygon stipple is enabled/disabled or when
405  * the fragment shader changes.
406  * We add/update the fragment sampler and sampler views to sample from
407  * the polygon stipple texture.  The texture unit that we use depends on
408  * the fragment shader (we need to use a unit not otherwise used by the
409  * shader).
410  */
411 static void
update_polygon_stipple_enable(struct softpipe_context * softpipe,unsigned prim)412 update_polygon_stipple_enable(struct softpipe_context *softpipe, unsigned prim)
413 {
414    if (prim == PIPE_PRIM_TRIANGLES &&
415        softpipe->fs_variant->key.polygon_stipple) {
416       const unsigned unit = softpipe->fs_variant->stipple_sampler_unit;
417 
418       /* sampler state */
419       softpipe->samplers[PIPE_SHADER_FRAGMENT][unit] = softpipe->pstipple.sampler;
420 
421       /* sampler view state */
422       softpipe_set_sampler_views(&softpipe->pipe, PIPE_SHADER_FRAGMENT,
423                                  unit, 1, &softpipe->pstipple.sampler_view);
424 
425       softpipe->dirty |= SP_NEW_SAMPLER;
426    }
427 }
428 
429 
430 /* Hopefully this will remain quite simple, otherwise need to pull in
431  * something like the gallium frontend mechanism.
432  */
433 void
softpipe_update_derived(struct softpipe_context * softpipe,unsigned prim)434 softpipe_update_derived(struct softpipe_context *softpipe, unsigned prim)
435 {
436    struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);
437 
438    /* Check for updated textures.
439     */
440    if (softpipe->tex_timestamp != sp_screen->timestamp) {
441       softpipe->tex_timestamp = sp_screen->timestamp;
442       softpipe->dirty |= SP_NEW_TEXTURE;
443    }
444 
445 #if DO_PSTIPPLE_IN_HELPER_MODULE
446    if (softpipe->dirty & SP_NEW_STIPPLE)
447       /* before updating samplers! */
448       update_polygon_stipple_pattern(softpipe);
449 #endif
450 
451    if (softpipe->dirty & (SP_NEW_RASTERIZER |
452                           SP_NEW_FS))
453       update_fragment_shader(softpipe, prim);
454 
455 #if DO_PSTIPPLE_IN_HELPER_MODULE
456    if (softpipe->dirty & (SP_NEW_RASTERIZER |
457                           SP_NEW_STIPPLE |
458                           SP_NEW_FS))
459       update_polygon_stipple_enable(softpipe, prim);
460 #endif
461 
462    /* TODO: this looks suboptimal */
463    if (softpipe->dirty & (SP_NEW_SAMPLER |
464                           SP_NEW_TEXTURE |
465                           SP_NEW_FS |
466                           SP_NEW_VS))
467       update_tgsi_samplers( softpipe );
468 
469    if (softpipe->dirty & (SP_NEW_RASTERIZER |
470                           SP_NEW_FS |
471                           SP_NEW_VS))
472       invalidate_vertex_layout( softpipe );
473 
474    if (softpipe->dirty & (SP_NEW_SCISSOR |
475                           SP_NEW_RASTERIZER |
476                           SP_NEW_FRAMEBUFFER))
477       compute_cliprect(softpipe);
478 
479    if (softpipe->dirty & (SP_NEW_BLEND |
480                           SP_NEW_DEPTH_STENCIL_ALPHA |
481                           SP_NEW_FRAMEBUFFER |
482                           SP_NEW_STIPPLE |
483                           SP_NEW_FS))
484       sp_build_quad_pipeline(softpipe);
485 
486    softpipe->dirty = 0;
487 }
488