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