glsl: move to compiler/
[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 "main/framebuffer.h"
35 #include "intel_batchbuffer.h"
36
37 /**
38 * Determine the appropriate attribute override value to store into the
39 * 3DSTATE_SF structure for a given fragment shader attribute. The attribute
40 * override value contains two pieces of information: the location of the
41 * attribute in the VUE (relative to urb_entry_read_offset, see below), and a
42 * flag indicating whether to "swizzle" the attribute based on the direction
43 * the triangle is facing.
44 *
45 * If an attribute is "swizzled", then the given VUE location is used for
46 * front-facing triangles, and the VUE location that immediately follows is
47 * used for back-facing triangles. We use this to implement the mapping from
48 * gl_FrontColor/gl_BackColor to gl_Color.
49 *
50 * urb_entry_read_offset is the offset into the VUE at which the SF unit is
51 * being instructed to begin reading attribute data. It can be set to a
52 * nonzero value to prevent the SF unit from wasting time reading elements of
53 * the VUE that are not needed by the fragment shader. It is measured in
54 * 256-bit increments.
55 */
56 static uint32_t
57 get_attr_override(const struct brw_vue_map *vue_map, int urb_entry_read_offset,
58 int fs_attr, bool two_side_color, uint32_t *max_source_attr)
59 {
60 /* Find the VUE slot for this attribute. */
61 int slot = vue_map->varying_to_slot[fs_attr];
62
63 /* Viewport and Layer are stored in the VUE header. We need to override
64 * them to zero if earlier stages didn't write them, as GL requires that
65 * they read back as zero when not explicitly set.
66 */
67 if (fs_attr == VARYING_SLOT_VIEWPORT || fs_attr == VARYING_SLOT_LAYER) {
68 unsigned override =
69 ATTRIBUTE_0_OVERRIDE_X | ATTRIBUTE_0_OVERRIDE_W |
70 ATTRIBUTE_CONST_0000 << ATTRIBUTE_0_CONST_SOURCE_SHIFT;
71
72 if (!(vue_map->slots_valid & VARYING_BIT_LAYER))
73 override |= ATTRIBUTE_0_OVERRIDE_Y;
74 if (!(vue_map->slots_valid & VARYING_BIT_VIEWPORT))
75 override |= ATTRIBUTE_0_OVERRIDE_Z;
76
77 return override;
78 }
79
80 /* If there was only a back color written but not front, use back
81 * as the color instead of undefined
82 */
83 if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
84 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
85 if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
86 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
87
88 if (slot == -1) {
89 /* This attribute does not exist in the VUE--that means that the vertex
90 * shader did not write to it. This means that either:
91 *
92 * (a) This attribute is a texture coordinate, and it is going to be
93 * replaced with point coordinates (as a consequence of a call to
94 * glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE)), so the
95 * hardware will ignore whatever attribute override we supply.
96 *
97 * (b) This attribute is read by the fragment shader but not written by
98 * the vertex shader, so its value is undefined. Therefore the
99 * attribute override we supply doesn't matter.
100 *
101 * (c) This attribute is gl_PrimitiveID, and it wasn't written by the
102 * previous shader stage.
103 *
104 * Note that we don't have to worry about the cases where the attribute
105 * is gl_PointCoord or is undergoing point sprite coordinate
106 * replacement, because in those cases, this function isn't called.
107 *
108 * In case (c), we need to program the attribute overrides so that the
109 * primitive ID will be stored in this slot. In every other case, the
110 * attribute override we supply doesn't matter. So just go ahead and
111 * program primitive ID in every case.
112 */
113 return (ATTRIBUTE_0_OVERRIDE_W |
114 ATTRIBUTE_0_OVERRIDE_Z |
115 ATTRIBUTE_0_OVERRIDE_Y |
116 ATTRIBUTE_0_OVERRIDE_X |
117 (ATTRIBUTE_CONST_PRIM_ID << ATTRIBUTE_0_CONST_SOURCE_SHIFT));
118 }
119
120 /* Compute the location of the attribute relative to urb_entry_read_offset.
121 * Each increment of urb_entry_read_offset represents a 256-bit value, so
122 * it counts for two 128-bit VUE slots.
123 */
124 int source_attr = slot - 2 * urb_entry_read_offset;
125 assert(source_attr >= 0 && source_attr < 32);
126
127 /* If we are doing two-sided color, and the VUE slot following this one
128 * represents a back-facing color, then we need to instruct the SF unit to
129 * do back-facing swizzling.
130 */
131 bool swizzling = two_side_color &&
132 ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
133 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
134 (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
135 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1));
136
137 /* Update max_source_attr. If swizzling, the SF will read this slot + 1. */
138 if (*max_source_attr < source_attr + swizzling)
139 *max_source_attr = source_attr + swizzling;
140
141 if (swizzling) {
142 return source_attr |
143 (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
144 }
145
146 return source_attr;
147 }
148
149
150 static bool
151 is_drawing_points(const struct brw_context *brw)
152 {
153 /* Determine if the primitives *reaching the SF* are points */
154 /* _NEW_POLYGON */
155 if (brw->ctx.Polygon.FrontMode == GL_POINT ||
156 brw->ctx.Polygon.BackMode == GL_POINT) {
157 return true;
158 }
159
160 if (brw->geometry_program) {
161 /* BRW_NEW_GEOMETRY_PROGRAM */
162 return brw->geometry_program->OutputType == GL_POINTS;
163 } else {
164 /* BRW_NEW_PRIMITIVE */
165 return brw->primitive == _3DPRIM_POINTLIST;
166 }
167 }
168
169
170 /**
171 * Create the mapping from the FS inputs we produce to the previous pipeline
172 * stage (GS or VS) outputs they source from.
173 */
174 void
175 calculate_attr_overrides(const struct brw_context *brw,
176 uint16_t *attr_overrides,
177 uint32_t *point_sprite_enables,
178 uint32_t *flat_enables,
179 uint32_t *urb_entry_read_length,
180 uint32_t *urb_entry_read_offset)
181 {
182 uint32_t max_source_attr = 0;
183
184 *point_sprite_enables = 0;
185 *flat_enables = 0;
186
187 *urb_entry_read_offset = BRW_SF_URB_ENTRY_READ_OFFSET;
188
189 /* BRW_NEW_FRAGMENT_PROGRAM
190 *
191 * If the fragment shader reads VARYING_SLOT_LAYER, then we need to pass in
192 * the full vertex header. Otherwise, we can program the SF to start
193 * reading at an offset of 1 (2 varying slots) to skip unnecessary data:
194 * - VARYING_SLOT_PSIZ and BRW_VARYING_SLOT_NDC on gen4-5
195 * - VARYING_SLOT_{PSIZ,LAYER} and VARYING_SLOT_POS on gen6+
196 */
197
198 bool fs_needs_vue_header = brw->fragment_program->Base.InputsRead &
199 (VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT);
200
201 *urb_entry_read_offset = fs_needs_vue_header ? 0 : 1;
202
203 /* _NEW_LIGHT */
204 bool shade_model_flat = brw->ctx.Light.ShadeModel == GL_FLAT;
205
206 /* From the Ivybridge PRM, Vol 2 Part 1, 3DSTATE_SBE,
207 * description of dw10 Point Sprite Texture Coordinate Enable:
208 *
209 * "This field must be programmed to zero when non-point primitives
210 * are rendered."
211 *
212 * The SandyBridge PRM doesn't explicitly say that point sprite enables
213 * must be programmed to zero when rendering non-point primitives, but
214 * the IvyBridge PRM does, and if we don't, we get garbage.
215 *
216 * This is not required on Haswell, as the hardware ignores this state
217 * when drawing non-points -- although we do still need to be careful to
218 * correctly set the attr overrides.
219 */
220 /* BRW_NEW_PRIMITIVE | BRW_NEW_GEOMETRY_PROGRAM */
221 bool drawing_points = is_drawing_points(brw);
222
223 /* Initialize all the attr_overrides to 0. In the loop below we'll modify
224 * just the ones that correspond to inputs used by the fs.
225 */
226 memset(attr_overrides, 0, 16*sizeof(*attr_overrides));
227
228 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
229 /* BRW_NEW_FRAGMENT_PROGRAM */
230 enum glsl_interp_qualifier interp_qualifier =
231 brw->fragment_program->InterpQualifier[attr];
232 bool is_gl_Color = attr == VARYING_SLOT_COL0 || attr == VARYING_SLOT_COL1;
233 /* BRW_NEW_FS_PROG_DATA */
234 int input_index = brw->wm.prog_data->urb_setup[attr];
235
236 if (input_index < 0)
237 continue;
238
239 /* _NEW_POINT */
240 bool point_sprite = false;
241 if (drawing_points) {
242 if (brw->ctx.Point.PointSprite &&
243 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) &&
244 brw->ctx.Point.CoordReplace[attr - VARYING_SLOT_TEX0]) {
245 point_sprite = true;
246 }
247
248 if (attr == VARYING_SLOT_PNTC)
249 point_sprite = true;
250
251 if (point_sprite)
252 *point_sprite_enables |= (1 << input_index);
253 }
254
255 /* flat shading */
256 if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
257 (shade_model_flat && is_gl_Color &&
258 interp_qualifier == INTERP_QUALIFIER_NONE))
259 *flat_enables |= (1 << input_index);
260
261 /* BRW_NEW_VUE_MAP_GEOM_OUT | _NEW_LIGHT | _NEW_PROGRAM */
262 uint16_t attr_override = point_sprite ? 0 :
263 get_attr_override(&brw->vue_map_geom_out,
264 *urb_entry_read_offset, attr,
265 brw->ctx.VertexProgram._TwoSideEnabled,
266 &max_source_attr);
267
268 /* The hardware can only do the overrides on 16 overrides at a
269 * time, and the other up to 16 have to be lined up so that the
270 * input index = the output index. We'll need to do some
271 * tweaking to make sure that's the case.
272 */
273 if (input_index < 16)
274 attr_overrides[input_index] = attr_override;
275 else
276 assert(attr_override == input_index);
277 }
278
279 /* From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
280 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
281 *
282 * "This field should be set to the minimum length required to read the
283 * maximum source attribute. The maximum source attribute is indicated
284 * by the maximum value of the enabled Attribute # Source Attribute if
285 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
286 * enable is not set.
287 * read_length = ceiling((max_source_attr + 1) / 2)
288 *
289 * [errata] Corruption/Hang possible if length programmed larger than
290 * recommended"
291 *
292 * Similar text exists for Ivy Bridge.
293 */
294 *urb_entry_read_length = ALIGN(max_source_attr + 1, 2) / 2;
295 }
296
297
298 static void
299 upload_sf_state(struct brw_context *brw)
300 {
301 struct gl_context *ctx = &brw->ctx;
302 /* BRW_NEW_FS_PROG_DATA */
303 uint32_t num_outputs = brw->wm.prog_data->num_varying_inputs;
304 uint32_t dw1, dw2, dw3, dw4;
305 uint32_t point_sprite_enables;
306 uint32_t flat_enables;
307 int i;
308 /* _NEW_BUFFER */
309 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
310 const bool multisampled_fbo = _mesa_geometric_samples(ctx->DrawBuffer) > 1;
311
312 float point_size;
313 uint16_t attr_overrides[16];
314 uint32_t point_sprite_origin;
315
316 dw1 = GEN6_SF_SWIZZLE_ENABLE | num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT;
317 dw2 = GEN6_SF_STATISTICS_ENABLE;
318
319 if (brw->sf.viewport_transform_enable)
320 dw2 |= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
321
322 dw3 = 0;
323 dw4 = 0;
324
325 /* _NEW_POLYGON */
326 if (ctx->Polygon._FrontBit == render_to_fbo)
327 dw2 |= GEN6_SF_WINDING_CCW;
328
329 if (ctx->Polygon.OffsetFill)
330 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
331
332 if (ctx->Polygon.OffsetLine)
333 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
334
335 if (ctx->Polygon.OffsetPoint)
336 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
337
338 switch (ctx->Polygon.FrontMode) {
339 case GL_FILL:
340 dw2 |= GEN6_SF_FRONT_SOLID;
341 break;
342
343 case GL_LINE:
344 dw2 |= GEN6_SF_FRONT_WIREFRAME;
345 break;
346
347 case GL_POINT:
348 dw2 |= GEN6_SF_FRONT_POINT;
349 break;
350
351 default:
352 unreachable("not reached");
353 }
354
355 switch (ctx->Polygon.BackMode) {
356 case GL_FILL:
357 dw2 |= GEN6_SF_BACK_SOLID;
358 break;
359
360 case GL_LINE:
361 dw2 |= GEN6_SF_BACK_WIREFRAME;
362 break;
363
364 case GL_POINT:
365 dw2 |= GEN6_SF_BACK_POINT;
366 break;
367
368 default:
369 unreachable("not reached");
370 }
371
372 /* _NEW_SCISSOR */
373 if (ctx->Scissor.EnableFlags)
374 dw3 |= GEN6_SF_SCISSOR_ENABLE;
375
376 /* _NEW_POLYGON */
377 if (ctx->Polygon.CullFlag) {
378 switch (ctx->Polygon.CullFaceMode) {
379 case GL_FRONT:
380 dw3 |= GEN6_SF_CULL_FRONT;
381 break;
382 case GL_BACK:
383 dw3 |= GEN6_SF_CULL_BACK;
384 break;
385 case GL_FRONT_AND_BACK:
386 dw3 |= GEN6_SF_CULL_BOTH;
387 break;
388 default:
389 unreachable("not reached");
390 }
391 } else {
392 dw3 |= GEN6_SF_CULL_NONE;
393 }
394
395 /* _NEW_LINE */
396 {
397 uint32_t line_width_u3_7 = brw_get_line_width(brw);
398 dw3 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
399 }
400 if (ctx->Line.SmoothFlag) {
401 dw3 |= GEN6_SF_LINE_AA_ENABLE;
402 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
403 dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
404 }
405 /* _NEW_MULTISAMPLE */
406 if (multisampled_fbo && ctx->Multisample.Enabled)
407 dw3 |= GEN6_SF_MSRAST_ON_PATTERN;
408
409 /* _NEW_PROGRAM | _NEW_POINT */
410 if (!(ctx->VertexProgram.PointSizeEnabled ||
411 ctx->Point._Attenuated))
412 dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
413
414 /* Clamp to ARB_point_parameters user limits */
415 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
416
417 /* Clamp to the hardware limits and convert to fixed point */
418 dw4 |= U_FIXED(CLAMP(point_size, 0.125f, 255.875f), 3);
419
420 /*
421 * Window coordinates in an FBO are inverted, which means point
422 * sprite origin must be inverted, too.
423 */
424 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
425 point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
426 } else {
427 point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
428 }
429 dw1 |= point_sprite_origin;
430
431 /* _NEW_LIGHT */
432 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
433 dw4 |=
434 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
435 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
436 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
437 } else {
438 dw4 |=
439 (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
440 }
441
442 /* BRW_NEW_VUE_MAP_GEOM_OUT | BRW_NEW_FRAGMENT_PROGRAM |
443 * _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM | BRW_NEW_FS_PROG_DATA
444 */
445 uint32_t urb_entry_read_length;
446 uint32_t urb_entry_read_offset;
447 calculate_attr_overrides(brw, attr_overrides, &point_sprite_enables,
448 &flat_enables, &urb_entry_read_length,
449 &urb_entry_read_offset);
450 dw1 |= (urb_entry_read_length << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
451 urb_entry_read_offset << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT);
452
453 BEGIN_BATCH(20);
454 OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
455 OUT_BATCH(dw1);
456 OUT_BATCH(dw2);
457 OUT_BATCH(dw3);
458 OUT_BATCH(dw4);
459 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
460 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
461 OUT_BATCH_F(ctx->Polygon.OffsetClamp); /* global depth offset clamp */
462 for (i = 0; i < 8; i++) {
463 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
464 }
465 OUT_BATCH(point_sprite_enables); /* dw16 */
466 OUT_BATCH(flat_enables);
467 OUT_BATCH(0); /* wrapshortest enables 0-7 */
468 OUT_BATCH(0); /* wrapshortest enables 8-15 */
469 ADVANCE_BATCH();
470 }
471
472 const struct brw_tracked_state gen6_sf_state = {
473 .dirty = {
474 .mesa = _NEW_BUFFERS |
475 _NEW_LIGHT |
476 _NEW_LINE |
477 _NEW_MULTISAMPLE |
478 _NEW_POINT |
479 _NEW_POLYGON |
480 _NEW_PROGRAM |
481 _NEW_SCISSOR,
482 .brw = BRW_NEW_CONTEXT |
483 BRW_NEW_FRAGMENT_PROGRAM |
484 BRW_NEW_FS_PROG_DATA |
485 BRW_NEW_GEOMETRY_PROGRAM |
486 BRW_NEW_PRIMITIVE |
487 BRW_NEW_VUE_MAP_GEOM_OUT,
488 },
489 .emit = upload_sf_state,
490 };