sp_state_derived.c revision 0a14e9f09fc1cf9d5c277bb239f349203d3bed79
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**************************************************************************
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * All Rights Reserved.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copy of this software and associated documentation files (the
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "Software"), to deal in the Software without restriction, including
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * without limitation the rights to use, copy, modify, merge, publish,
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * distribute, sub license, and/or sell copies of the Software, and to
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * permit persons to whom the Software is furnished to do so, subject to
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the following conditions:
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The above copyright notice and this permission notice (including the
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * next paragraph) shall be included in all copies or substantial portions
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * of the Software.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) **************************************************************************/
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_inlines.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_math.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_memory.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_pstipple.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_shader_tokens.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "draw/draw_context.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "draw/draw_vertex.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sp_context.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sp_screen.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sp_state.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sp_texture.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sp_tex_tile_cache.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Mark the current vertex layout as "invalid".
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * We'll validate the vertex layout later, when we start to actually
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * render a point or line or tri.
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)invalidate_vertex_layout(struct softpipe_context *softpipe)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   softpipe->vertex_info.num_attribs =  0;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The vertex info describes how to convert the post-transformed vertices
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * (simple float[][4]) used by the 'draw' module into vertices for
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * rasterization.
58 *
59 * This function validates the vertex layout and returns a pointer to a
60 * vertex_info object.
61 */
62struct vertex_info *
63softpipe_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 = INTERP_LINEAR;
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         case TGSI_INTERPOLATE_COLOR:
104            assert(fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR);
105            break;
106         default:
107            assert(0);
108         }
109
110         switch (fsInfo->input_semantic_name[i]) {
111         case TGSI_SEMANTIC_POSITION:
112            interp = INTERP_POS;
113            break;
114
115         case TGSI_SEMANTIC_COLOR:
116            if (fsInfo->input_interpolate[i] == TGSI_INTERPOLATE_COLOR) {
117               if (softpipe->rasterizer->flatshade)
118                  interp = INTERP_CONSTANT;
119               else
120                  interp = INTERP_PERSPECTIVE;
121            }
122            break;
123         }
124
125         /* this includes texcoords and varying vars */
126         src = draw_find_shader_output(softpipe->draw,
127                                       fsInfo->input_semantic_name[i],
128                                       fsInfo->input_semantic_index[i]);
129	 if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR && src == 0)
130	   /* try and find a bcolor */
131	   src = draw_find_shader_output(softpipe->draw,
132					 TGSI_SEMANTIC_BCOLOR, fsInfo->input_semantic_index[i]);
133
134         draw_emit_vertex_attr(vinfo, EMIT_4F, interp, src);
135      }
136
137      softpipe->psize_slot = draw_find_shader_output(softpipe->draw,
138                                                 TGSI_SEMANTIC_PSIZE, 0);
139      if (softpipe->psize_slot > 0) {
140         draw_emit_vertex_attr(vinfo, EMIT_4F, INTERP_CONSTANT,
141                               softpipe->psize_slot);
142      }
143
144      draw_compute_vertex_size(vinfo);
145   }
146
147   return vinfo;
148}
149
150
151/**
152 * Called from vbuf module.
153 *
154 * Note that there's actually two different vertex layouts in softpipe.
155 *
156 * The normal one is computed in softpipe_get_vertex_info() above and is
157 * used by the point/line/tri "setup" code.
158 *
159 * The other one (this one) is only used by the vbuf module (which is
160 * not normally used by default but used in testing).  For the vbuf module,
161 * we basically want to pass-through the draw module's vertex layout as-is.
162 * When the softpipe vbuf code begins drawing, the normal vertex layout
163 * will come into play again.
164 */
165struct vertex_info *
166softpipe_get_vbuf_vertex_info(struct softpipe_context *softpipe)
167{
168   (void) softpipe_get_vertex_info(softpipe);
169   return &softpipe->vertex_info_vbuf;
170}
171
172
173/**
174 * Recompute cliprect from scissor bounds, scissor enable and surface size.
175 */
176static void
177compute_cliprect(struct softpipe_context *sp)
178{
179   /* SP_NEW_FRAMEBUFFER
180    */
181   uint surfWidth = sp->framebuffer.width;
182   uint surfHeight = sp->framebuffer.height;
183
184   /* SP_NEW_RASTERIZER
185    */
186   if (sp->rasterizer->scissor) {
187
188      /* SP_NEW_SCISSOR
189       *
190       * clip to scissor rect:
191       */
192      sp->cliprect.minx = MAX2(sp->scissor.minx, 0);
193      sp->cliprect.miny = MAX2(sp->scissor.miny, 0);
194      sp->cliprect.maxx = MIN2(sp->scissor.maxx, surfWidth);
195      sp->cliprect.maxy = MIN2(sp->scissor.maxy, surfHeight);
196   }
197   else {
198      /* clip to surface bounds */
199      sp->cliprect.minx = 0;
200      sp->cliprect.miny = 0;
201      sp->cliprect.maxx = surfWidth;
202      sp->cliprect.maxy = surfHeight;
203   }
204}
205
206
207static void
208update_tgsi_samplers( struct softpipe_context *softpipe )
209{
210   unsigned i, sh;
211
212   softpipe_reset_sampler_variants( softpipe );
213
214   for (sh = 0; sh < Elements(softpipe->tex_cache); sh++) {
215      for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
216         struct softpipe_tex_tile_cache *tc = softpipe->tex_cache[sh][i];
217         if (tc && tc->texture) {
218            struct softpipe_resource *spt = softpipe_resource(tc->texture);
219            if (spt->timestamp != tc->timestamp) {
220               sp_tex_tile_cache_validate_texture( tc );
221               /*
222                 _debug_printf("INV %d %d\n", tc->timestamp, spt->timestamp);
223               */
224               tc->timestamp = spt->timestamp;
225            }
226         }
227      }
228   }
229}
230
231
232static void
233update_fragment_shader(struct softpipe_context *softpipe, unsigned prim)
234{
235   struct sp_fragment_shader_variant_key key;
236
237   memset(&key, 0, sizeof(key));
238
239   if (prim == PIPE_PRIM_TRIANGLES)
240      key.polygon_stipple = softpipe->rasterizer->poly_stipple_enable;
241
242   if (softpipe->fs) {
243      softpipe->fs_variant = softpipe_find_fs_variant(softpipe,
244                                                      softpipe->fs, &key);
245   }
246   else {
247      softpipe->fs_variant = NULL;
248   }
249
250   /* This would be the logical place to pass the fragment shader
251    * to the draw module.  However, doing this here, during state
252    * validation, causes problems with the 'draw' module helpers for
253    * wide/AA/stippled lines.
254    * In principle, the draw's fragment shader should be per-variant
255    * but that doesn't work.  So we use a single draw fragment shader
256    * per fragment shader, not per variant.
257    */
258#if 0
259   if (softpipe->fs_variant) {
260      draw_bind_fragment_shader(softpipe->draw,
261                                softpipe->fs_variant->draw_shader);
262   }
263   else {
264      draw_bind_fragment_shader(softpipe->draw, NULL);
265   }
266#endif
267}
268
269
270/**
271 * This should be called when the polygon stipple pattern changes.
272 * We create a new texture from the stipple pattern and create a new
273 * sampler view.
274 */
275static void
276update_polygon_stipple_pattern(struct softpipe_context *softpipe)
277{
278   struct pipe_resource *tex;
279   struct pipe_sampler_view *view;
280
281   tex = util_pstipple_create_stipple_texture(&softpipe->pipe,
282                                              softpipe->poly_stipple.stipple);
283   pipe_resource_reference(&softpipe->pstipple.texture, tex);
284   pipe_resource_reference(&tex, NULL);
285
286   view = util_pstipple_create_sampler_view(&softpipe->pipe,
287                                            softpipe->pstipple.texture);
288   pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, view);
289   pipe_sampler_view_reference(&view, NULL);
290}
291
292
293/**
294 * Should be called when polygon stipple is enabled/disabled or when
295 * the fragment shader changes.
296 * We add/update the fragment sampler and sampler views to sample from
297 * the polygon stipple texture.  The texture unit that we use depends on
298 * the fragment shader (we need to use a unit not otherwise used by the
299 * shader).
300 */
301static void
302update_polygon_stipple_enable(struct softpipe_context *softpipe, unsigned prim)
303{
304   if (prim == PIPE_PRIM_TRIANGLES &&
305       softpipe->fs_variant->key.polygon_stipple) {
306      const unsigned unit = softpipe->fs_variant->stipple_sampler_unit;
307
308      assert(unit >= softpipe->num_fragment_samplers);
309
310      /* sampler state */
311      softpipe->fragment_samplers[unit] = softpipe->pstipple.sampler;
312
313      /* sampler view */
314      pipe_sampler_view_reference(&softpipe->fragment_sampler_views[unit],
315                                  softpipe->pstipple.sampler_view);
316
317      sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[PIPE_SHADER_FRAGMENT][unit],
318                                         softpipe->pstipple.sampler_view);
319
320      softpipe->dirty |= SP_NEW_SAMPLER;
321   }
322}
323
324
325/* Hopefully this will remain quite simple, otherwise need to pull in
326 * something like the state tracker mechanism.
327 */
328void
329softpipe_update_derived(struct softpipe_context *softpipe, unsigned prim)
330{
331   struct softpipe_screen *sp_screen = softpipe_screen(softpipe->pipe.screen);
332
333   /* Check for updated textures.
334    */
335   if (softpipe->tex_timestamp != sp_screen->timestamp) {
336      softpipe->tex_timestamp = sp_screen->timestamp;
337      softpipe->dirty |= SP_NEW_TEXTURE;
338   }
339
340#if DO_PSTIPPLE_IN_HELPER_MODULE
341   if (softpipe->dirty & SP_NEW_STIPPLE)
342      /* before updating samplers! */
343      update_polygon_stipple_pattern(softpipe);
344#endif
345
346   if (softpipe->dirty & (SP_NEW_RASTERIZER |
347                          SP_NEW_FS))
348      update_fragment_shader(softpipe, prim);
349
350#if DO_PSTIPPLE_IN_HELPER_MODULE
351   if (softpipe->dirty & (SP_NEW_RASTERIZER |
352                          SP_NEW_STIPPLE |
353                          SP_NEW_FS))
354      update_polygon_stipple_enable(softpipe, prim);
355#endif
356
357   if (softpipe->dirty & (SP_NEW_SAMPLER |
358                          SP_NEW_TEXTURE |
359                          SP_NEW_FS |
360                          SP_NEW_VS))
361      update_tgsi_samplers( softpipe );
362
363   if (softpipe->dirty & (SP_NEW_RASTERIZER |
364                          SP_NEW_FS |
365                          SP_NEW_VS))
366      invalidate_vertex_layout( softpipe );
367
368   if (softpipe->dirty & (SP_NEW_SCISSOR |
369                          SP_NEW_RASTERIZER |
370                          SP_NEW_FRAMEBUFFER))
371      compute_cliprect(softpipe);
372
373   if (softpipe->dirty & (SP_NEW_BLEND |
374                          SP_NEW_DEPTH_STENCIL_ALPHA |
375                          SP_NEW_FRAMEBUFFER |
376                          SP_NEW_FS))
377      sp_build_quad_pipeline(softpipe);
378
379   softpipe->dirty = 0;
380}
381