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_math.h"
29#include "util/u_memory.h"
30#include "pipe/p_shader_tokens.h"
31#include "draw/draw_context.h"
32#include "draw/draw_vertex.h"
33#include "draw/draw_private.h"
34#include "lp_context.h"
35#include "lp_screen.h"
36#include "lp_setup.h"
37#include "lp_state.h"
38
39
40
41/**
42 * The vertex info describes how to convert the post-transformed vertices
43 * (simple float[][4]) used by the 'draw' module into vertices for
44 * rasterization.
45 *
46 * This function validates the vertex layout.
47 */
48static void
49compute_vertex_info(struct llvmpipe_context *llvmpipe)
50{
51   const struct tgsi_shader_info *fsInfo = &llvmpipe->fs->info.base;
52   struct vertex_info *vinfo = &llvmpipe->vertex_info;
53   int vs_index;
54   uint i;
55
56   draw_prepare_shader_outputs(llvmpipe->draw);
57
58   /*
59    * Those can't actually be 0 (because pos is always at 0).
60    * But use ints anyway to avoid confusion (in vs outputs, they
61    * can very well be at pos 0).
62    */
63   llvmpipe->color_slot[0] = -1;
64   llvmpipe->color_slot[1] = -1;
65   llvmpipe->bcolor_slot[0] = -1;
66   llvmpipe->bcolor_slot[1] = -1;
67   llvmpipe->viewport_index_slot = -1;
68   llvmpipe->layer_slot = -1;
69   llvmpipe->face_slot = -1;
70   llvmpipe->psize_slot = -1;
71
72   /*
73    * Match FS inputs against VS outputs, emitting the necessary
74    * attributes.  Could cache these structs and look them up with a
75    * combination of fragment shader, vertex shader ids.
76    */
77
78   vinfo->num_attribs = 0;
79
80   vs_index = draw_find_shader_output(llvmpipe->draw,
81                                      TGSI_SEMANTIC_POSITION, 0);
82
83   draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
84
85   for (i = 0; i < fsInfo->num_inputs; i++) {
86      /*
87       * Search for each input in current vs output:
88       */
89      vs_index = draw_find_shader_output(llvmpipe->draw,
90                                         fsInfo->input_semantic_name[i],
91                                         fsInfo->input_semantic_index[i]);
92
93      if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_COLOR &&
94          fsInfo->input_semantic_index[i] < 2) {
95         int idx = fsInfo->input_semantic_index[i];
96         llvmpipe->color_slot[idx] = (int)vinfo->num_attribs;
97      }
98
99      if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_FACE) {
100         llvmpipe->face_slot = (int)vinfo->num_attribs;
101         draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
102      /*
103       * For vp index and layer, if the fs requires them but the vs doesn't
104       * provide them, draw (vbuf) will give us the required 0 (slot -1).
105       * (This means in this case we'll also use those slots in setup, which
106       * isn't necessary but they'll contain the correct (0) value.)
107       */
108      } else if (fsInfo->input_semantic_name[i] ==
109                 TGSI_SEMANTIC_VIEWPORT_INDEX) {
110         llvmpipe->viewport_index_slot = (int)vinfo->num_attribs;
111         draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
112      } else if (fsInfo->input_semantic_name[i] == TGSI_SEMANTIC_LAYER) {
113         llvmpipe->layer_slot = (int)vinfo->num_attribs;
114         draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
115      } else {
116         /*
117          * Note that we'd actually want to skip position (as we won't use
118          * the attribute in the fs) but can't. The reason is that we don't
119          * actually have an input/output map for setup (even though it looks
120          * like we do...). Could adjust for this though even without a map
121          * (in llvmpipe_create_fs_state()).
122          */
123         draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
124      }
125   }
126
127   /* Figure out if we need bcolor as well.
128    */
129   for (i = 0; i < 2; i++) {
130      vs_index = draw_find_shader_output(llvmpipe->draw,
131                                         TGSI_SEMANTIC_BCOLOR, i);
132
133      if (vs_index >= 0) {
134         llvmpipe->bcolor_slot[i] = (int)vinfo->num_attribs;
135         draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
136      }
137   }
138
139   /* Figure out if we need pointsize as well.
140    */
141   vs_index = draw_find_shader_output(llvmpipe->draw,
142                                      TGSI_SEMANTIC_PSIZE, 0);
143
144   if (vs_index >= 0) {
145      llvmpipe->psize_slot = (int)vinfo->num_attribs;
146      draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
147   }
148
149   /* Figure out if we need viewport index (if it wasn't already in fs input) */
150   if (llvmpipe->viewport_index_slot < 0) {
151      vs_index = draw_find_shader_output(llvmpipe->draw,
152                                         TGSI_SEMANTIC_VIEWPORT_INDEX,
153                                         0);
154      if (vs_index >= 0) {
155         llvmpipe->viewport_index_slot =(int)vinfo->num_attribs;
156         draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
157      }
158   }
159
160   /* Figure out if we need layer (if it wasn't already in fs input) */
161   if (llvmpipe->layer_slot < 0) {
162      vs_index = draw_find_shader_output(llvmpipe->draw,
163                                         TGSI_SEMANTIC_LAYER,
164                                         0);
165      if (vs_index >= 0) {
166         llvmpipe->layer_slot = (int)vinfo->num_attribs;
167         draw_emit_vertex_attr(vinfo, EMIT_4F, vs_index);
168      }
169   }
170
171   draw_compute_vertex_size(vinfo);
172   lp_setup_set_vertex_info(llvmpipe->setup, vinfo);
173}
174
175
176static void
177check_linear_rasterizer( struct llvmpipe_context *lp )
178{
179   boolean bgr8;
180   boolean permit_linear;
181   boolean single_vp;
182   boolean clipping_changed = FALSE;
183
184   bgr8 = (lp->framebuffer.nr_cbufs == 1 && lp->framebuffer.cbufs[0] &&
185           lp->framebuffer.cbufs[0]->texture->nr_samples == 1 &&
186           lp->framebuffer.cbufs[0]->texture->target == PIPE_TEXTURE_2D &&
187           (lp->framebuffer.cbufs[0]->format == PIPE_FORMAT_B8G8R8A8_UNORM ||
188            lp->framebuffer.cbufs[0]->format == PIPE_FORMAT_B8G8R8X8_UNORM));
189
190   /* permit_linear means guardband, hence fake scissor, which we can only
191    * handle if there's just one vp. */
192   single_vp = lp->viewport_index_slot < 0;
193   permit_linear = (!lp->framebuffer.zsbuf &&
194                    bgr8 &&
195                    single_vp);
196
197   /* Tell draw that we're happy doing our own x/y clipping.
198    */
199   if (lp->permit_linear_rasterizer != permit_linear) {
200      lp->permit_linear_rasterizer = permit_linear;
201      lp_setup_set_linear_mode(lp->setup, permit_linear);
202      clipping_changed = TRUE;
203   }
204
205   if (lp->single_vp != single_vp) {
206      lp->single_vp = single_vp;
207      clipping_changed = TRUE;
208   }
209
210   /* Disable xy clipping in linear mode.
211    *
212    * Use a guard band if we don't have zsbuf.  Could enable
213    * guardband always - this just to be conservative.
214    *
215    * Because we have a layering violation where the draw module emits
216    * state changes to the driver while we're already inside a draw
217    * call, need to be careful about when we make calls back to the
218    * draw module.  Hence the clipping_changed flag which is as much
219    * to prevent flush recursion as it is to short-circuit noop state
220    * changes.
221    */
222   if (clipping_changed) {
223      draw_set_driver_clipping(lp->draw,
224                               FALSE,
225                               FALSE,
226                               permit_linear,
227                               single_vp);
228   }
229}
230
231
232/**
233 * Handle state changes before clears.
234 * Called just prior to clearing (pipe::clear()).
235 */
236void llvmpipe_update_derived_clear( struct llvmpipe_context *llvmpipe )
237{
238   if (llvmpipe->dirty & (LP_NEW_FS |
239                          LP_NEW_FRAMEBUFFER))
240      check_linear_rasterizer(llvmpipe);
241}
242
243
244/**
245 * Handle state changes.
246 * Called just prior to drawing anything (pipe::draw_arrays(), etc).
247 *
248 * Hopefully this will remain quite simple, otherwise need to pull in
249 * something like the gallium frontend mechanism.
250 */
251void llvmpipe_update_derived( struct llvmpipe_context *llvmpipe )
252{
253   struct llvmpipe_screen *lp_screen = llvmpipe_screen(llvmpipe->pipe.screen);
254
255   /* Check for updated textures.
256    */
257   if (llvmpipe->tex_timestamp != lp_screen->timestamp) {
258      llvmpipe->tex_timestamp = lp_screen->timestamp;
259      llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
260   }
261
262   /* This needs LP_NEW_RASTERIZER because of draw_prepare_shader_outputs(). */
263   if (llvmpipe->dirty & (LP_NEW_RASTERIZER |
264                          LP_NEW_FS |
265                          LP_NEW_GS |
266                          LP_NEW_TCS |
267                          LP_NEW_TES |
268                          LP_NEW_VS))
269      compute_vertex_info(llvmpipe);
270
271   if (llvmpipe->dirty & (LP_NEW_FS |
272                          LP_NEW_FRAMEBUFFER |
273                          LP_NEW_BLEND |
274                          LP_NEW_SCISSOR |
275                          LP_NEW_DEPTH_STENCIL_ALPHA |
276                          LP_NEW_RASTERIZER |
277                          LP_NEW_SAMPLER |
278                          LP_NEW_SAMPLER_VIEW |
279                          LP_NEW_OCCLUSION_QUERY))
280      llvmpipe_update_fs(llvmpipe);
281
282   if (llvmpipe->dirty & (LP_NEW_FS |
283                          LP_NEW_FRAMEBUFFER |
284                          LP_NEW_RASTERIZER |
285                          LP_NEW_SAMPLE_MASK |
286                          LP_NEW_DEPTH_STENCIL_ALPHA)) {
287
288      /*
289       * Rasterization is disabled if there is no pixel shader and
290       * both depth and stencil testing are disabled:
291       * http://msdn.microsoft.com/en-us/library/windows/desktop/bb205125
292       * FIXME: set rasterizer_discard in state tracker instead.
293       */
294      boolean null_fs = !llvmpipe->fs ||
295                        llvmpipe->fs->info.base.num_instructions <= 1;
296      boolean discard =
297         (llvmpipe->sample_mask) == 0 ||
298         (llvmpipe->rasterizer ? llvmpipe->rasterizer->rasterizer_discard : FALSE) ||
299         (null_fs &&
300          !llvmpipe->depth_stencil->depth_enabled &&
301          !llvmpipe->depth_stencil->stencil[0].enabled);
302      lp_setup_set_rasterizer_discard(llvmpipe->setup, discard);
303   }
304
305   if (llvmpipe->dirty & (LP_NEW_FS |
306                          LP_NEW_FRAMEBUFFER |
307                          LP_NEW_RASTERIZER))
308      llvmpipe_update_setup( llvmpipe );
309
310   if (llvmpipe->dirty & LP_NEW_SAMPLE_MASK)
311      lp_setup_set_sample_mask(llvmpipe->setup, llvmpipe->sample_mask);
312
313   if (llvmpipe->dirty & LP_NEW_BLEND_COLOR)
314      lp_setup_set_blend_color(llvmpipe->setup,
315                               &llvmpipe->blend_color);
316
317   if (llvmpipe->dirty & LP_NEW_SCISSOR)
318      lp_setup_set_scissors(llvmpipe->setup, llvmpipe->scissors);
319
320   if (llvmpipe->dirty & LP_NEW_DEPTH_STENCIL_ALPHA) {
321      lp_setup_set_alpha_ref_value(llvmpipe->setup,
322                                   llvmpipe->depth_stencil->alpha_ref_value);
323      lp_setup_set_stencil_ref_values(llvmpipe->setup,
324                                      llvmpipe->stencil_ref.ref_value);
325   }
326
327   if (llvmpipe->dirty & LP_NEW_FS_CONSTANTS)
328      lp_setup_set_fs_constants(llvmpipe->setup,
329                                ARRAY_SIZE(llvmpipe->constants[PIPE_SHADER_FRAGMENT]),
330                                llvmpipe->constants[PIPE_SHADER_FRAGMENT]);
331
332   if (llvmpipe->dirty & LP_NEW_FS_SSBOS)
333      lp_setup_set_fs_ssbos(llvmpipe->setup,
334                            ARRAY_SIZE(llvmpipe->ssbos[PIPE_SHADER_FRAGMENT]),
335                            llvmpipe->ssbos[PIPE_SHADER_FRAGMENT]);
336
337   if (llvmpipe->dirty & LP_NEW_FS_IMAGES)
338      lp_setup_set_fs_images(llvmpipe->setup,
339                             ARRAY_SIZE(llvmpipe->images[PIPE_SHADER_FRAGMENT]),
340                             llvmpipe->images[PIPE_SHADER_FRAGMENT]);
341
342   if (llvmpipe->dirty & (LP_NEW_SAMPLER_VIEW))
343      lp_setup_set_fragment_sampler_views(llvmpipe->setup,
344                                          llvmpipe->num_sampler_views[PIPE_SHADER_FRAGMENT],
345                                          llvmpipe->sampler_views[PIPE_SHADER_FRAGMENT]);
346
347   if (llvmpipe->dirty & (LP_NEW_SAMPLER))
348      lp_setup_set_fragment_sampler_state(llvmpipe->setup,
349                                          llvmpipe->num_samplers[PIPE_SHADER_FRAGMENT],
350                                          llvmpipe->samplers[PIPE_SHADER_FRAGMENT]);
351
352   if (llvmpipe->dirty & LP_NEW_VIEWPORT) {
353      /*
354       * Update setup and fragment's view of the active viewport state.
355       *
356       * XXX TODO: It is possible to only loop over the active viewports
357       *           instead of all viewports (PIPE_MAX_VIEWPORTS).
358       */
359      lp_setup_set_viewports(llvmpipe->setup,
360                             PIPE_MAX_VIEWPORTS,
361                             llvmpipe->viewports);
362   }
363
364   llvmpipe_update_derived_clear(llvmpipe);
365
366   llvmpipe->dirty = 0;
367}
368
369