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