gen6_sf_state.c revision 2de8874ec37bfc548de2e16bbefa51341e25d340
1/*
2 * Copyright © 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 *    Eric Anholt <eric@anholt.net>
25 *
26 */
27
28#include "brw_context.h"
29#include "brw_state.h"
30#include "brw_defines.h"
31#include "brw_util.h"
32#include "main/macros.h"
33#include "main/fbobject.h"
34#include "intel_batchbuffer.h"
35
36/**
37 * Determine the appropriate attribute override value to store into the
38 * 3DSTATE_SF structure for a given fragment shader attribute.  The attribute
39 * override value contains two pieces of information: the location of the
40 * attribute in the VUE (relative to urb_entry_read_offset, see below), and a
41 * flag indicating whether to "swizzle" the attribute based on the direction
42 * the triangle is facing.
43 *
44 * If an attribute is "swizzled", then the given VUE location is used for
45 * front-facing triangles, and the VUE location that immediately follows is
46 * used for back-facing triangles.  We use this to implement the mapping from
47 * gl_FrontColor/gl_BackColor to gl_Color.
48 *
49 * urb_entry_read_offset is the offset into the VUE at which the SF unit is
50 * being instructed to begin reading attribute data.  It can be set to a
51 * nonzero value to prevent the SF unit from wasting time reading elements of
52 * the VUE that are not needed by the fragment shader.  It is measured in
53 * 256-bit increments.
54 */
55uint32_t
56get_attr_override(struct brw_vue_map *vue_map, int urb_entry_read_offset,
57                  int fs_attr, bool two_side_color, uint32_t *max_source_attr)
58{
59   int vs_attr = _mesa_frag_attrib_to_vert_result(fs_attr);
60   if (vs_attr < 0 || vs_attr == VERT_RESULT_HPOS) {
61      /* These attributes will be overwritten by the fragment shader's
62       * interpolation code (see emit_interp() in brw_wm_fp.c), so just let
63       * them reference the first available attribute.
64       */
65      return 0;
66   }
67
68   /* Find the VUE slot for this attribute. */
69   int slot = vue_map->vert_result_to_slot[vs_attr];
70
71   /* If there was only a back color written but not front, use back
72    * as the color instead of undefined
73    */
74   if (slot == -1 && vs_attr == VERT_RESULT_COL0)
75      slot = vue_map->vert_result_to_slot[VERT_RESULT_BFC0];
76   if (slot == -1 && vs_attr == VERT_RESULT_COL1)
77      slot = vue_map->vert_result_to_slot[VERT_RESULT_BFC1];
78
79   if (slot == -1) {
80      /* This attribute does not exist in the VUE--that means that the vertex
81       * shader did not write to it.  Behavior is undefined in this case, so
82       * just reference the first available attribute.
83       */
84      return 0;
85   }
86
87   /* Compute the location of the attribute relative to urb_entry_read_offset.
88    * Each increment of urb_entry_read_offset represents a 256-bit value, so
89    * it counts for two 128-bit VUE slots.
90    */
91   int source_attr = slot - 2 * urb_entry_read_offset;
92   assert(source_attr >= 0 && source_attr < 32);
93
94   /* If we are doing two-sided color, and the VUE slot following this one
95    * represents a back-facing color, then we need to instruct the SF unit to
96    * do back-facing swizzling.
97    */
98   bool swizzling = two_side_color &&
99      ((vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL0 &&
100        vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC0) ||
101       (vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL1 &&
102        vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC1));
103
104   /* Update max_source_attr.  If swizzling, the SF will read this slot + 1. */
105   if (*max_source_attr < source_attr + swizzling)
106      *max_source_attr = source_attr + swizzling;
107
108   if (swizzling) {
109      return source_attr |
110         (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
111   }
112
113   return source_attr;
114}
115
116static void
117upload_sf_state(struct brw_context *brw)
118{
119   struct intel_context *intel = &brw->intel;
120   struct gl_context *ctx = &intel->ctx;
121   uint32_t urb_entry_read_length;
122   /* BRW_NEW_FRAGMENT_PROGRAM */
123   uint32_t num_outputs = _mesa_bitcount_64(brw->fragment_program->Base.InputsRead);
124   /* _NEW_LIGHT */
125   bool shade_model_flat = ctx->Light.ShadeModel == GL_FLAT;
126   uint32_t dw1, dw2, dw3, dw4, dw16, dw17;
127   int i;
128   /* _NEW_BUFFER */
129   bool render_to_fbo = _mesa_is_user_fbo(brw->intel.ctx.DrawBuffer);
130   bool multisampled_fbo = ctx->DrawBuffer->Visual.samples > 1;
131
132   int attr = 0, input_index = 0;
133   int urb_entry_read_offset = 1;
134   float point_size;
135   uint16_t attr_overrides[FRAG_ATTRIB_MAX];
136   uint32_t point_sprite_origin;
137
138   /* CACHE_NEW_VS_PROG */
139   urb_entry_read_length = ((brw->vs.prog_data->vue_map.num_slots + 1) / 2 -
140			    urb_entry_read_offset);
141   if (urb_entry_read_length == 0) {
142      /* Setting the URB entry read length to 0 causes undefined behavior, so
143       * if we have no URB data to read, set it to 1.
144       */
145      urb_entry_read_length = 1;
146   }
147
148   dw1 =
149      GEN6_SF_SWIZZLE_ENABLE |
150      num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT |
151      urb_entry_read_length << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
152      urb_entry_read_offset << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT;
153
154   dw2 = GEN6_SF_STATISTICS_ENABLE |
155         GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
156
157   dw3 = 0;
158   dw4 = 0;
159   dw16 = 0;
160   dw17 = 0;
161
162   /* _NEW_POLYGON */
163   if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
164      dw2 |= GEN6_SF_WINDING_CCW;
165
166   if (ctx->Polygon.OffsetFill)
167       dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
168
169   if (ctx->Polygon.OffsetLine)
170       dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
171
172   if (ctx->Polygon.OffsetPoint)
173       dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
174
175   switch (ctx->Polygon.FrontMode) {
176   case GL_FILL:
177       dw2 |= GEN6_SF_FRONT_SOLID;
178       break;
179
180   case GL_LINE:
181       dw2 |= GEN6_SF_FRONT_WIREFRAME;
182       break;
183
184   case GL_POINT:
185       dw2 |= GEN6_SF_FRONT_POINT;
186       break;
187
188   default:
189       assert(0);
190       break;
191   }
192
193   switch (ctx->Polygon.BackMode) {
194   case GL_FILL:
195       dw2 |= GEN6_SF_BACK_SOLID;
196       break;
197
198   case GL_LINE:
199       dw2 |= GEN6_SF_BACK_WIREFRAME;
200       break;
201
202   case GL_POINT:
203       dw2 |= GEN6_SF_BACK_POINT;
204       break;
205
206   default:
207       assert(0);
208       break;
209   }
210
211   /* _NEW_SCISSOR */
212   if (ctx->Scissor.Enabled)
213      dw3 |= GEN6_SF_SCISSOR_ENABLE;
214
215   /* _NEW_POLYGON */
216   if (ctx->Polygon.CullFlag) {
217      switch (ctx->Polygon.CullFaceMode) {
218      case GL_FRONT:
219	 dw3 |= GEN6_SF_CULL_FRONT;
220	 break;
221      case GL_BACK:
222	 dw3 |= GEN6_SF_CULL_BACK;
223	 break;
224      case GL_FRONT_AND_BACK:
225	 dw3 |= GEN6_SF_CULL_BOTH;
226	 break;
227      default:
228	 assert(0);
229	 break;
230      }
231   } else {
232      dw3 |= GEN6_SF_CULL_NONE;
233   }
234
235   /* _NEW_LINE */
236   {
237      uint32_t line_width_u3_7 = U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7);
238      /* TODO: line width of 0 is not allowed when MSAA enabled */
239      if (line_width_u3_7 == 0)
240         line_width_u3_7 = 1;
241      dw3 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
242   }
243   if (ctx->Line.SmoothFlag) {
244      dw3 |= GEN6_SF_LINE_AA_ENABLE;
245      dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
246      dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
247   }
248   /* _NEW_MULTISAMPLE */
249   if (multisampled_fbo && ctx->Multisample.Enabled)
250      dw3 |= GEN6_SF_MSRAST_ON_PATTERN;
251
252   /* _NEW_PROGRAM | _NEW_POINT */
253   if (!(ctx->VertexProgram.PointSizeEnabled ||
254	 ctx->Point._Attenuated))
255      dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
256
257   /* Clamp to ARB_point_parameters user limits */
258   point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
259
260   /* Clamp to the hardware limits and convert to fixed point */
261   dw4 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
262
263   /*
264    * Window coordinates in an FBO are inverted, which means point
265    * sprite origin must be inverted, too.
266    */
267   if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
268      point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
269   } else {
270      point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
271   }
272   dw1 |= point_sprite_origin;
273
274   /* _NEW_LIGHT */
275   if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
276      dw4 |=
277	 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
278	 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
279	 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
280   } else {
281      dw4 |=
282	 (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
283   }
284
285   /* Create the mapping from the FS inputs we produce to the VS outputs
286    * they source from.
287    */
288   uint32_t max_source_attr = 0;
289   for (; attr < FRAG_ATTRIB_MAX; attr++) {
290      enum glsl_interp_qualifier interp_qualifier =
291         brw->fragment_program->InterpQualifier[attr];
292      bool is_gl_Color = attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1;
293
294      if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
295	 continue;
296
297      /* _NEW_POINT */
298      if (ctx->Point.PointSprite &&
299	  (attr >= FRAG_ATTRIB_TEX0 && attr <= FRAG_ATTRIB_TEX7) &&
300	  ctx->Point.CoordReplace[attr - FRAG_ATTRIB_TEX0]) {
301	 dw16 |= (1 << input_index);
302      }
303
304      if (attr == FRAG_ATTRIB_PNTC)
305	 dw16 |= (1 << input_index);
306
307      /* flat shading */
308      if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
309          (shade_model_flat && is_gl_Color &&
310           interp_qualifier == INTERP_QUALIFIER_NONE))
311         dw17 |= (1 << input_index);
312
313      /* The hardware can only do the overrides on 16 overrides at a
314       * time, and the other up to 16 have to be lined up so that the
315       * input index = the output index.  We'll need to do some
316       * tweaking to make sure that's the case.
317       */
318      assert(input_index < 16 || attr == input_index);
319
320      /* CACHE_NEW_VS_PROG | _NEW_LIGHT | _NEW_PROGRAM */
321      attr_overrides[input_index++] =
322         get_attr_override(&brw->vs.prog_data->vue_map,
323			   urb_entry_read_offset, attr,
324                           ctx->VertexProgram._TwoSideEnabled,
325                           &max_source_attr);
326   }
327
328   for (; input_index < FRAG_ATTRIB_MAX; input_index++)
329      attr_overrides[input_index] = 0;
330
331   BEGIN_BATCH(20);
332   OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
333   OUT_BATCH(dw1);
334   OUT_BATCH(dw2);
335   OUT_BATCH(dw3);
336   OUT_BATCH(dw4);
337   OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant.  copied from gen4 */
338   OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
339   OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
340   for (i = 0; i < 8; i++) {
341      OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
342   }
343   OUT_BATCH(dw16); /* point sprite texcoord bitmask */
344   OUT_BATCH(dw17); /* constant interp bitmask */
345   OUT_BATCH(0); /* wrapshortest enables 0-7 */
346   OUT_BATCH(0); /* wrapshortest enables 8-15 */
347   ADVANCE_BATCH();
348}
349
350const struct brw_tracked_state gen6_sf_state = {
351   .dirty = {
352      .mesa  = (_NEW_LIGHT |
353		_NEW_PROGRAM |
354		_NEW_POLYGON |
355		_NEW_LINE |
356		_NEW_SCISSOR |
357		_NEW_BUFFERS |
358		_NEW_POINT |
359                _NEW_MULTISAMPLE),
360      .brw   = (BRW_NEW_CONTEXT |
361		BRW_NEW_FRAGMENT_PROGRAM),
362      .cache = CACHE_NEW_VS_PROG
363   },
364   .emit = upload_sf_state,
365};
366