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