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