i965: Move VUE map computation to once at VS compile time.
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_sf_state.c
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 "intel_batchbuffer.h"
34
35 /**
36 * Determine the appropriate attribute override value to store into the
37 * 3DSTATE_SF structure for a given fragment shader attribute. The attribute
38 * override value contains two pieces of information: the location of the
39 * attribute in the VUE (relative to urb_entry_read_offset, see below), and a
40 * flag indicating whether to "swizzle" the attribute based on the direction
41 * the triangle is facing.
42 *
43 * If an attribute is "swizzled", then the given VUE location is used for
44 * front-facing triangles, and the VUE location that immediately follows is
45 * used for back-facing triangles. We use this to implement the mapping from
46 * gl_FrontColor/gl_BackColor to gl_Color.
47 *
48 * urb_entry_read_offset is the offset into the VUE at which the SF unit is
49 * being instructed to begin reading attribute data. It can be set to a
50 * nonzero value to prevent the SF unit from wasting time reading elements of
51 * the VUE that are not needed by the fragment shader. It is measured in
52 * 256-bit increments.
53 */
54 uint32_t
55 get_attr_override(struct brw_vue_map *vue_map, int urb_entry_read_offset,
56 int fs_attr, bool two_side_color)
57 {
58 int attr_override, slot;
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 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 attr_override = slot - 2 * urb_entry_read_offset;
92 assert (attr_override >= 0 && attr_override < 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 if (two_side_color) {
99 if (vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL0 &&
100 vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC0)
101 attr_override |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
102 else if (vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL1 &&
103 vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC1)
104 attr_override |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
105 }
106
107 return attr_override;
108 }
109
110 static void
111 upload_sf_state(struct brw_context *brw)
112 {
113 struct intel_context *intel = &brw->intel;
114 struct gl_context *ctx = &intel->ctx;
115 uint32_t urb_entry_read_length;
116 /* BRW_NEW_FRAGMENT_PROGRAM */
117 uint32_t num_outputs = _mesa_bitcount_64(brw->fragment_program->Base.InputsRead);
118 /* _NEW_LIGHT */
119 bool shade_model_flat = ctx->Light.ShadeModel == GL_FLAT;
120 uint32_t dw1, dw2, dw3, dw4, dw16, dw17;
121 int i;
122 /* _NEW_BUFFER */
123 bool render_to_fbo = brw->intel.ctx.DrawBuffer->Name != 0;
124 int attr = 0, input_index = 0;
125 int urb_entry_read_offset = 1;
126 float point_size;
127 uint16_t attr_overrides[FRAG_ATTRIB_MAX];
128 uint32_t point_sprite_origin;
129
130 /* CACHE_NEW_VS_PROG */
131 urb_entry_read_length = ((brw->vs.prog_data->vue_map.num_slots + 1) / 2 -
132 urb_entry_read_offset);
133 if (urb_entry_read_length == 0) {
134 /* Setting the URB entry read length to 0 causes undefined behavior, so
135 * if we have no URB data to read, set it to 1.
136 */
137 urb_entry_read_length = 1;
138 }
139
140 dw1 =
141 GEN6_SF_SWIZZLE_ENABLE |
142 num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT |
143 urb_entry_read_length << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
144 urb_entry_read_offset << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT;
145
146 dw2 = GEN6_SF_STATISTICS_ENABLE |
147 GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
148
149 dw3 = 0;
150 dw4 = 0;
151 dw16 = 0;
152 dw17 = 0;
153
154 /* _NEW_POLYGON */
155 if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
156 dw2 |= GEN6_SF_WINDING_CCW;
157
158 if (ctx->Polygon.OffsetFill)
159 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
160
161 if (ctx->Polygon.OffsetLine)
162 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
163
164 if (ctx->Polygon.OffsetPoint)
165 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
166
167 switch (ctx->Polygon.FrontMode) {
168 case GL_FILL:
169 dw2 |= GEN6_SF_FRONT_SOLID;
170 break;
171
172 case GL_LINE:
173 dw2 |= GEN6_SF_FRONT_WIREFRAME;
174 break;
175
176 case GL_POINT:
177 dw2 |= GEN6_SF_FRONT_POINT;
178 break;
179
180 default:
181 assert(0);
182 break;
183 }
184
185 switch (ctx->Polygon.BackMode) {
186 case GL_FILL:
187 dw2 |= GEN6_SF_BACK_SOLID;
188 break;
189
190 case GL_LINE:
191 dw2 |= GEN6_SF_BACK_WIREFRAME;
192 break;
193
194 case GL_POINT:
195 dw2 |= GEN6_SF_BACK_POINT;
196 break;
197
198 default:
199 assert(0);
200 break;
201 }
202
203 /* _NEW_SCISSOR */
204 if (ctx->Scissor.Enabled)
205 dw3 |= GEN6_SF_SCISSOR_ENABLE;
206
207 /* _NEW_POLYGON */
208 if (ctx->Polygon.CullFlag) {
209 switch (ctx->Polygon.CullFaceMode) {
210 case GL_FRONT:
211 dw3 |= GEN6_SF_CULL_FRONT;
212 break;
213 case GL_BACK:
214 dw3 |= GEN6_SF_CULL_BACK;
215 break;
216 case GL_FRONT_AND_BACK:
217 dw3 |= GEN6_SF_CULL_BOTH;
218 break;
219 default:
220 assert(0);
221 break;
222 }
223 } else {
224 dw3 |= GEN6_SF_CULL_NONE;
225 }
226
227 /* _NEW_LINE */
228 dw3 |= U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7) <<
229 GEN6_SF_LINE_WIDTH_SHIFT;
230 if (ctx->Line.SmoothFlag) {
231 dw3 |= GEN6_SF_LINE_AA_ENABLE;
232 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
233 dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
234 }
235
236 /* _NEW_PROGRAM | _NEW_POINT */
237 if (!(ctx->VertexProgram.PointSizeEnabled ||
238 ctx->Point._Attenuated))
239 dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
240
241 /* Clamp to ARB_point_parameters user limits */
242 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
243
244 /* Clamp to the hardware limits and convert to fixed point */
245 dw4 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
246
247 /*
248 * Window coordinates in an FBO are inverted, which means point
249 * sprite origin must be inverted, too.
250 */
251 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
252 point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
253 } else {
254 point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
255 }
256 dw1 |= point_sprite_origin;
257
258 /* _NEW_LIGHT */
259 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
260 dw4 |=
261 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
262 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
263 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
264 } else {
265 dw4 |=
266 (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
267 }
268
269 /* Create the mapping from the FS inputs we produce to the VS outputs
270 * they source from.
271 */
272 for (; attr < FRAG_ATTRIB_MAX; attr++) {
273 enum glsl_interp_qualifier interp_qualifier =
274 brw->fragment_program->InterpQualifier[attr];
275 bool is_gl_Color = attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1;
276
277 if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
278 continue;
279
280 /* _NEW_POINT */
281 if (ctx->Point.PointSprite &&
282 (attr >= FRAG_ATTRIB_TEX0 && attr <= FRAG_ATTRIB_TEX7) &&
283 ctx->Point.CoordReplace[attr - FRAG_ATTRIB_TEX0]) {
284 dw16 |= (1 << input_index);
285 }
286
287 if (attr == FRAG_ATTRIB_PNTC)
288 dw16 |= (1 << input_index);
289
290 /* flat shading */
291 if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
292 (shade_model_flat && is_gl_Color &&
293 interp_qualifier == INTERP_QUALIFIER_NONE))
294 dw17 |= (1 << input_index);
295
296 /* The hardware can only do the overrides on 16 overrides at a
297 * time, and the other up to 16 have to be lined up so that the
298 * input index = the output index. We'll need to do some
299 * tweaking to make sure that's the case.
300 */
301 assert(input_index < 16 || attr == input_index);
302
303 /* CACHE_NEW_VS_PROG | _NEW_LIGHT | _NEW_PROGRAM */
304 attr_overrides[input_index++] =
305 get_attr_override(&brw->vs.prog_data->vue_map,
306 urb_entry_read_offset, attr,
307 ctx->VertexProgram._TwoSideEnabled);
308 }
309
310 for (; input_index < FRAG_ATTRIB_MAX; input_index++)
311 attr_overrides[input_index] = 0;
312
313 BEGIN_BATCH(20);
314 OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
315 OUT_BATCH(dw1);
316 OUT_BATCH(dw2);
317 OUT_BATCH(dw3);
318 OUT_BATCH(dw4);
319 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
320 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
321 OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
322 for (i = 0; i < 8; i++) {
323 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
324 }
325 OUT_BATCH(dw16); /* point sprite texcoord bitmask */
326 OUT_BATCH(dw17); /* constant interp bitmask */
327 OUT_BATCH(0); /* wrapshortest enables 0-7 */
328 OUT_BATCH(0); /* wrapshortest enables 8-15 */
329 ADVANCE_BATCH();
330 }
331
332 const struct brw_tracked_state gen6_sf_state = {
333 .dirty = {
334 .mesa = (_NEW_LIGHT |
335 _NEW_PROGRAM |
336 _NEW_POLYGON |
337 _NEW_LINE |
338 _NEW_SCISSOR |
339 _NEW_BUFFERS |
340 _NEW_POINT),
341 .brw = (BRW_NEW_CONTEXT |
342 BRW_NEW_FRAGMENT_PROGRAM),
343 .cache = CACHE_NEW_VS_PROG
344 },
345 .emit = upload_sf_state,
346 };