i965: Rename CACHE_NEW_*_PROG to BRW_NEW_*_PROG_DATA.
[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 static bool
133 is_drawing_points(const struct brw_context *brw)
134 {
135 /* Determine if the primitives *reaching the SF* are points */
136 if (brw->geometry_program) {
137 /* BRW_NEW_GEOMETRY_PROGRAM */
138 return brw->geometry_program->OutputType == GL_POINTS;
139 } else {
140 /* BRW_NEW_PRIMITIVE */
141 return brw->primitive == _3DPRIM_POINTLIST;
142 }
143 }
144
145
146 /**
147 * Create the mapping from the FS inputs we produce to the previous pipeline
148 * stage (GS or VS) outputs they source from.
149 */
150 void
151 calculate_attr_overrides(const struct brw_context *brw,
152 uint16_t *attr_overrides,
153 uint32_t *point_sprite_enables,
154 uint32_t *flat_enables,
155 uint32_t *urb_entry_read_length)
156 {
157 const int urb_entry_read_offset = BRW_SF_URB_ENTRY_READ_OFFSET;
158 uint32_t max_source_attr = 0;
159
160 *point_sprite_enables = 0;
161 *flat_enables = 0;
162
163 /* _NEW_LIGHT */
164 bool shade_model_flat = brw->ctx.Light.ShadeModel == GL_FLAT;
165
166 /* From the Ivybridge PRM, Vol 2 Part 1, 3DSTATE_SBE,
167 * description of dw10 Point Sprite Texture Coordinate Enable:
168 *
169 * "This field must be programmed to zero when non-point primitives
170 * are rendered."
171 *
172 * The SandyBridge PRM doesn't explicitly say that point sprite enables
173 * must be programmed to zero when rendering non-point primitives, but
174 * the IvyBridge PRM does, and if we don't, we get garbage.
175 *
176 * This is not required on Haswell, as the hardware ignores this state
177 * when drawing non-points -- although we do still need to be careful to
178 * correctly set the attr overrides.
179 */
180 /* BRW_NEW_PRIMITIVE | BRW_NEW_GEOMETRY_PROGRAM */
181 bool drawing_points = is_drawing_points(brw);
182
183 /* Initialize all the attr_overrides to 0. In the loop below we'll modify
184 * just the ones that correspond to inputs used by the fs.
185 */
186 memset(attr_overrides, 0, 16*sizeof(*attr_overrides));
187
188 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
189 /* BRW_NEW_FRAGMENT_PROGRAM */
190 enum glsl_interp_qualifier interp_qualifier =
191 brw->fragment_program->InterpQualifier[attr];
192 bool is_gl_Color = attr == VARYING_SLOT_COL0 || attr == VARYING_SLOT_COL1;
193 /* BRW_NEW_FS_PROG_DATA */
194 int input_index = brw->wm.prog_data->urb_setup[attr];
195
196 if (input_index < 0)
197 continue;
198
199 /* _NEW_POINT */
200 bool point_sprite = false;
201 if (drawing_points) {
202 if (brw->ctx.Point.PointSprite &&
203 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) &&
204 brw->ctx.Point.CoordReplace[attr - VARYING_SLOT_TEX0]) {
205 point_sprite = true;
206 }
207
208 if (attr == VARYING_SLOT_PNTC)
209 point_sprite = true;
210
211 if (point_sprite)
212 *point_sprite_enables |= (1 << input_index);
213 }
214
215 /* flat shading */
216 if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
217 (shade_model_flat && is_gl_Color &&
218 interp_qualifier == INTERP_QUALIFIER_NONE))
219 *flat_enables |= (1 << input_index);
220
221 /* BRW_NEW_VUE_MAP_GEOM_OUT | _NEW_LIGHT | _NEW_PROGRAM */
222 uint16_t attr_override = point_sprite ? 0 :
223 get_attr_override(&brw->vue_map_geom_out,
224 urb_entry_read_offset, attr,
225 brw->ctx.VertexProgram._TwoSideEnabled,
226 &max_source_attr);
227
228 /* The hardware can only do the overrides on 16 overrides at a
229 * time, and the other up to 16 have to be lined up so that the
230 * input index = the output index. We'll need to do some
231 * tweaking to make sure that's the case.
232 */
233 if (input_index < 16)
234 attr_overrides[input_index] = attr_override;
235 else
236 assert(attr_override == input_index);
237 }
238
239 /* From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
240 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
241 *
242 * "This field should be set to the minimum length required to read the
243 * maximum source attribute. The maximum source attribute is indicated
244 * by the maximum value of the enabled Attribute # Source Attribute if
245 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
246 * enable is not set.
247 * read_length = ceiling((max_source_attr + 1) / 2)
248 *
249 * [errata] Corruption/Hang possible if length programmed larger than
250 * recommended"
251 *
252 * Similar text exists for Ivy Bridge.
253 */
254 *urb_entry_read_length = ALIGN(max_source_attr + 1, 2) / 2;
255 }
256
257
258 static void
259 upload_sf_state(struct brw_context *brw)
260 {
261 struct gl_context *ctx = &brw->ctx;
262 /* BRW_NEW_FS_PROG_DATA */
263 uint32_t num_outputs = brw->wm.prog_data->num_varying_inputs;
264 uint32_t dw1, dw2, dw3, dw4;
265 uint32_t point_sprite_enables;
266 uint32_t flat_enables;
267 int i;
268 /* _NEW_BUFFER */
269 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
270 bool multisampled_fbo = ctx->DrawBuffer->Visual.samples > 1;
271
272 const int urb_entry_read_offset = BRW_SF_URB_ENTRY_READ_OFFSET;
273 float point_size;
274 uint16_t attr_overrides[16];
275 uint32_t point_sprite_origin;
276
277 dw1 = GEN6_SF_SWIZZLE_ENABLE | num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT;
278 dw2 = GEN6_SF_STATISTICS_ENABLE;
279
280 if (brw->sf.viewport_transform_enable)
281 dw2 |= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
282
283 dw3 = 0;
284 dw4 = 0;
285
286 /* _NEW_POLYGON */
287 if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
288 dw2 |= GEN6_SF_WINDING_CCW;
289
290 if (ctx->Polygon.OffsetFill)
291 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
292
293 if (ctx->Polygon.OffsetLine)
294 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
295
296 if (ctx->Polygon.OffsetPoint)
297 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
298
299 switch (ctx->Polygon.FrontMode) {
300 case GL_FILL:
301 dw2 |= GEN6_SF_FRONT_SOLID;
302 break;
303
304 case GL_LINE:
305 dw2 |= GEN6_SF_FRONT_WIREFRAME;
306 break;
307
308 case GL_POINT:
309 dw2 |= GEN6_SF_FRONT_POINT;
310 break;
311
312 default:
313 unreachable("not reached");
314 }
315
316 switch (ctx->Polygon.BackMode) {
317 case GL_FILL:
318 dw2 |= GEN6_SF_BACK_SOLID;
319 break;
320
321 case GL_LINE:
322 dw2 |= GEN6_SF_BACK_WIREFRAME;
323 break;
324
325 case GL_POINT:
326 dw2 |= GEN6_SF_BACK_POINT;
327 break;
328
329 default:
330 unreachable("not reached");
331 }
332
333 /* _NEW_SCISSOR */
334 if (ctx->Scissor.EnableFlags)
335 dw3 |= GEN6_SF_SCISSOR_ENABLE;
336
337 /* _NEW_POLYGON */
338 if (ctx->Polygon.CullFlag) {
339 switch (ctx->Polygon.CullFaceMode) {
340 case GL_FRONT:
341 dw3 |= GEN6_SF_CULL_FRONT;
342 break;
343 case GL_BACK:
344 dw3 |= GEN6_SF_CULL_BACK;
345 break;
346 case GL_FRONT_AND_BACK:
347 dw3 |= GEN6_SF_CULL_BOTH;
348 break;
349 default:
350 unreachable("not reached");
351 }
352 } else {
353 dw3 |= GEN6_SF_CULL_NONE;
354 }
355
356 /* _NEW_LINE */
357 {
358 uint32_t line_width_u3_7 =
359 U_FIXED(CLAMP(ctx->Line.Width, 0.0, ctx->Const.MaxLineWidth), 7);
360 /* TODO: line width of 0 is not allowed when MSAA enabled */
361 if (line_width_u3_7 == 0)
362 line_width_u3_7 = 1;
363 dw3 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
364 }
365 if (ctx->Line.SmoothFlag) {
366 dw3 |= GEN6_SF_LINE_AA_ENABLE;
367 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
368 dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
369 }
370 /* _NEW_MULTISAMPLE */
371 if (multisampled_fbo && ctx->Multisample.Enabled)
372 dw3 |= GEN6_SF_MSRAST_ON_PATTERN;
373
374 /* _NEW_PROGRAM | _NEW_POINT */
375 if (!(ctx->VertexProgram.PointSizeEnabled ||
376 ctx->Point._Attenuated))
377 dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
378
379 /* Clamp to ARB_point_parameters user limits */
380 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
381
382 /* Clamp to the hardware limits and convert to fixed point */
383 dw4 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
384
385 /*
386 * Window coordinates in an FBO are inverted, which means point
387 * sprite origin must be inverted, too.
388 */
389 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
390 point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
391 } else {
392 point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
393 }
394 dw1 |= point_sprite_origin;
395
396 /* _NEW_LIGHT */
397 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
398 dw4 |=
399 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
400 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
401 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
402 } else {
403 dw4 |=
404 (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
405 }
406
407 /* BRW_NEW_VUE_MAP_GEOM_OUT | BRW_NEW_FRAGMENT_PROGRAM |
408 * _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM | BRW_NEW_FS_PROG_DATA
409 */
410 uint32_t urb_entry_read_length;
411 calculate_attr_overrides(brw, attr_overrides, &point_sprite_enables,
412 &flat_enables, &urb_entry_read_length);
413 dw1 |= (urb_entry_read_length << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
414 urb_entry_read_offset << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT);
415
416 BEGIN_BATCH(20);
417 OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
418 OUT_BATCH(dw1);
419 OUT_BATCH(dw2);
420 OUT_BATCH(dw3);
421 OUT_BATCH(dw4);
422 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
423 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
424 OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
425 for (i = 0; i < 8; i++) {
426 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
427 }
428 OUT_BATCH(point_sprite_enables); /* dw16 */
429 OUT_BATCH(flat_enables);
430 OUT_BATCH(0); /* wrapshortest enables 0-7 */
431 OUT_BATCH(0); /* wrapshortest enables 8-15 */
432 ADVANCE_BATCH();
433 }
434
435 const struct brw_tracked_state gen6_sf_state = {
436 .dirty = {
437 .mesa = _NEW_BUFFERS |
438 _NEW_LIGHT |
439 _NEW_LINE |
440 _NEW_MULTISAMPLE |
441 _NEW_POINT |
442 _NEW_POLYGON |
443 _NEW_PROGRAM |
444 _NEW_SCISSOR,
445 .brw = BRW_NEW_CONTEXT |
446 BRW_NEW_FRAGMENT_PROGRAM |
447 BRW_NEW_GEOMETRY_PROGRAM |
448 BRW_NEW_PRIMITIVE |
449 BRW_NEW_VUE_MAP_GEOM_OUT,
450 .cache = BRW_NEW_FS_PROG_DATA
451 },
452 .emit = upload_sf_state,
453 };