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