6a9fa602c6a3d0d59fcba9c98434b7e9968e2faf
[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 "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 */
55 static uint32_t
56 get_attr_override(const 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 /* Find the VUE slot for this attribute. */
60 int slot = vue_map->varying_to_slot[fs_attr];
61
62 /* If there was only a back color written but not front, use back
63 * as the color instead of undefined
64 */
65 if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
66 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
67 if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
68 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
69
70 if (slot == -1) {
71 /* This attribute does not exist in the VUE--that means that the vertex
72 * shader did not write to it. This means that either:
73 *
74 * (a) This attribute is a texture coordinate, and it is going to be
75 * replaced with point coordinates (as a consequence of a call to
76 * glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE)), so the
77 * hardware will ignore whatever attribute override we supply.
78 *
79 * (b) This attribute is read by the fragment shader but not written by
80 * the vertex shader, so its value is undefined. Therefore the
81 * attribute override we supply doesn't matter.
82 *
83 * In either case the attribute override we supply doesn't matter, so
84 * just reference the first available attribute.
85 */
86 return 0;
87 }
88
89 /* Compute the location of the attribute relative to urb_entry_read_offset.
90 * Each increment of urb_entry_read_offset represents a 256-bit value, so
91 * it counts for two 128-bit VUE slots.
92 */
93 int source_attr = slot - 2 * urb_entry_read_offset;
94 assert(source_attr >= 0 && source_attr < 32);
95
96 /* If we are doing two-sided color, and the VUE slot following this one
97 * represents a back-facing color, then we need to instruct the SF unit to
98 * do back-facing swizzling.
99 */
100 bool swizzling = two_side_color &&
101 ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
102 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
103 (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
104 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1));
105
106 /* Update max_source_attr. If swizzling, the SF will read this slot + 1. */
107 if (*max_source_attr < source_attr + swizzling)
108 *max_source_attr = source_attr + swizzling;
109
110 if (swizzling) {
111 return source_attr |
112 (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
113 }
114
115 return source_attr;
116 }
117
118
119 /**
120 * Create the mapping from the FS inputs we produce to the previous pipeline
121 * stage (GS or VS) outputs they source from.
122 */
123 void
124 calculate_attr_overrides(const struct brw_context *brw,
125 uint16_t *attr_overrides,
126 uint32_t *point_sprite_enables,
127 uint32_t *flat_enables,
128 uint32_t *urb_entry_read_length)
129 {
130 const int urb_entry_read_offset = BRW_SF_URB_ENTRY_READ_OFFSET;
131 uint32_t max_source_attr = 0;
132
133 /* _NEW_LIGHT */
134 bool shade_model_flat = brw->ctx.Light.ShadeModel == GL_FLAT;
135
136 /* Initialize all the attr_overrides to 0. In the loop below we'll modify
137 * just the ones that correspond to inputs used by the fs.
138 */
139 memset(attr_overrides, 0, 16*sizeof(*attr_overrides));
140
141 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
142 enum glsl_interp_qualifier interp_qualifier =
143 brw->fragment_program->InterpQualifier[attr];
144 bool is_gl_Color = attr == VARYING_SLOT_COL0 || attr == VARYING_SLOT_COL1;
145 /* CACHE_NEW_WM_PROG */
146 int input_index = brw->wm.prog_data->urb_setup[attr];
147
148 if (input_index < 0)
149 continue;
150
151 /* _NEW_POINT */
152 if (brw->ctx.Point.PointSprite &&
153 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) &&
154 brw->ctx.Point.CoordReplace[attr - VARYING_SLOT_TEX0]) {
155 *point_sprite_enables |= (1 << input_index);
156 }
157
158 if (attr == VARYING_SLOT_PNTC)
159 *point_sprite_enables |= (1 << input_index);
160
161 /* flat shading */
162 if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
163 (shade_model_flat && is_gl_Color &&
164 interp_qualifier == INTERP_QUALIFIER_NONE))
165 *flat_enables |= (1 << input_index);
166
167 /* BRW_NEW_VUE_MAP_GEOM_OUT | _NEW_LIGHT | _NEW_PROGRAM */
168 uint16_t attr_override =
169 get_attr_override(&brw->vue_map_geom_out,
170 urb_entry_read_offset, attr,
171 brw->ctx.VertexProgram._TwoSideEnabled,
172 &max_source_attr);
173
174 /* The hardware can only do the overrides on 16 overrides at a
175 * time, and the other up to 16 have to be lined up so that the
176 * input index = the output index. We'll need to do some
177 * tweaking to make sure that's the case.
178 */
179 if (input_index < 16)
180 attr_overrides[input_index] = attr_override;
181 else
182 assert(attr_override == input_index);
183 }
184
185 /* From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
186 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
187 *
188 * "This field should be set to the minimum length required to read the
189 * maximum source attribute. The maximum source attribute is indicated
190 * by the maximum value of the enabled Attribute # Source Attribute if
191 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
192 * enable is not set.
193 * read_length = ceiling((max_source_attr + 1) / 2)
194 *
195 * [errata] Corruption/Hang possible if length programmed larger than
196 * recommended"
197 *
198 * Similar text exists for Ivy Bridge.
199 */
200 *urb_entry_read_length = ALIGN(max_source_attr + 1, 2) / 2;
201 }
202
203
204 static void
205 upload_sf_state(struct brw_context *brw)
206 {
207 struct gl_context *ctx = &brw->ctx;
208 /* CACHE_NEW_WM_PROG */
209 uint32_t num_outputs = brw->wm.prog_data->num_varying_inputs;
210 uint32_t dw1, dw2, dw3, dw4, dw16, dw17;
211 int i;
212 /* _NEW_BUFFER */
213 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
214 bool multisampled_fbo = ctx->DrawBuffer->Visual.samples > 1;
215
216 const int urb_entry_read_offset = BRW_SF_URB_ENTRY_READ_OFFSET;
217 float point_size;
218 uint16_t attr_overrides[16];
219 uint32_t point_sprite_origin;
220
221 dw1 = GEN6_SF_SWIZZLE_ENABLE | num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT;
222
223 dw2 = GEN6_SF_STATISTICS_ENABLE |
224 GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
225
226 dw3 = 0;
227 dw4 = 0;
228 dw16 = 0;
229 dw17 = 0;
230
231 /* _NEW_POLYGON */
232 if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
233 dw2 |= GEN6_SF_WINDING_CCW;
234
235 if (ctx->Polygon.OffsetFill)
236 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
237
238 if (ctx->Polygon.OffsetLine)
239 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
240
241 if (ctx->Polygon.OffsetPoint)
242 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
243
244 switch (ctx->Polygon.FrontMode) {
245 case GL_FILL:
246 dw2 |= GEN6_SF_FRONT_SOLID;
247 break;
248
249 case GL_LINE:
250 dw2 |= GEN6_SF_FRONT_WIREFRAME;
251 break;
252
253 case GL_POINT:
254 dw2 |= GEN6_SF_FRONT_POINT;
255 break;
256
257 default:
258 assert(0);
259 break;
260 }
261
262 switch (ctx->Polygon.BackMode) {
263 case GL_FILL:
264 dw2 |= GEN6_SF_BACK_SOLID;
265 break;
266
267 case GL_LINE:
268 dw2 |= GEN6_SF_BACK_WIREFRAME;
269 break;
270
271 case GL_POINT:
272 dw2 |= GEN6_SF_BACK_POINT;
273 break;
274
275 default:
276 assert(0);
277 break;
278 }
279
280 /* _NEW_SCISSOR */
281 if (ctx->Scissor.Enabled)
282 dw3 |= GEN6_SF_SCISSOR_ENABLE;
283
284 /* _NEW_POLYGON */
285 if (ctx->Polygon.CullFlag) {
286 switch (ctx->Polygon.CullFaceMode) {
287 case GL_FRONT:
288 dw3 |= GEN6_SF_CULL_FRONT;
289 break;
290 case GL_BACK:
291 dw3 |= GEN6_SF_CULL_BACK;
292 break;
293 case GL_FRONT_AND_BACK:
294 dw3 |= GEN6_SF_CULL_BOTH;
295 break;
296 default:
297 assert(0);
298 break;
299 }
300 } else {
301 dw3 |= GEN6_SF_CULL_NONE;
302 }
303
304 /* _NEW_LINE */
305 {
306 uint32_t line_width_u3_7 = U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7);
307 /* TODO: line width of 0 is not allowed when MSAA enabled */
308 if (line_width_u3_7 == 0)
309 line_width_u3_7 = 1;
310 dw3 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
311 }
312 if (ctx->Line.SmoothFlag) {
313 dw3 |= GEN6_SF_LINE_AA_ENABLE;
314 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
315 dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
316 }
317 /* _NEW_MULTISAMPLE */
318 if (multisampled_fbo && ctx->Multisample.Enabled)
319 dw3 |= GEN6_SF_MSRAST_ON_PATTERN;
320
321 /* _NEW_PROGRAM | _NEW_POINT */
322 if (!(ctx->VertexProgram.PointSizeEnabled ||
323 ctx->Point._Attenuated))
324 dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
325
326 /* Clamp to ARB_point_parameters user limits */
327 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
328
329 /* Clamp to the hardware limits and convert to fixed point */
330 dw4 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
331
332 /*
333 * Window coordinates in an FBO are inverted, which means point
334 * sprite origin must be inverted, too.
335 */
336 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
337 point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
338 } else {
339 point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
340 }
341 dw1 |= point_sprite_origin;
342
343 /* _NEW_LIGHT */
344 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
345 dw4 |=
346 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
347 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
348 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
349 } else {
350 dw4 |=
351 (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
352 }
353
354 /* BRW_NEW_VUE_MAP_GEOM_OUT | _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM |
355 * CACHE_NEW_WM_PROG
356 */
357 uint32_t urb_entry_read_length;
358 calculate_attr_overrides(brw, attr_overrides, &dw16, &dw17,
359 &urb_entry_read_length);
360 dw1 |= (urb_entry_read_length << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
361 urb_entry_read_offset << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT);
362
363 BEGIN_BATCH(20);
364 OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
365 OUT_BATCH(dw1);
366 OUT_BATCH(dw2);
367 OUT_BATCH(dw3);
368 OUT_BATCH(dw4);
369 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
370 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
371 OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
372 for (i = 0; i < 8; i++) {
373 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
374 }
375 OUT_BATCH(dw16); /* point sprite texcoord bitmask */
376 OUT_BATCH(dw17); /* constant interp bitmask */
377 OUT_BATCH(0); /* wrapshortest enables 0-7 */
378 OUT_BATCH(0); /* wrapshortest enables 8-15 */
379 ADVANCE_BATCH();
380 }
381
382 const struct brw_tracked_state gen6_sf_state = {
383 .dirty = {
384 .mesa = (_NEW_LIGHT |
385 _NEW_PROGRAM |
386 _NEW_POLYGON |
387 _NEW_LINE |
388 _NEW_SCISSOR |
389 _NEW_BUFFERS |
390 _NEW_POINT |
391 _NEW_MULTISAMPLE),
392 .brw = (BRW_NEW_CONTEXT |
393 BRW_NEW_FRAGMENT_PROGRAM |
394 BRW_NEW_VUE_MAP_GEOM_OUT),
395 .cache = CACHE_NEW_WM_PROG
396 },
397 .emit = upload_sf_state,
398 };