mesa: add/update comments in _mesa_copy_buffer_subdata()
[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 "intel_batchbuffer.h"
34
35 /**
36 * Determine the appropriate attribute override value to store into the
37 * 3DSTATE_SF structure for a given fragment shader attribute. The attribute
38 * override value contains two pieces of information: the location of the
39 * attribute in the VUE (relative to urb_entry_read_offset, see below), and a
40 * flag indicating whether to "swizzle" the attribute based on the direction
41 * the triangle is facing.
42 *
43 * If an attribute is "swizzled", then the given VUE location is used for
44 * front-facing triangles, and the VUE location that immediately follows is
45 * used for back-facing triangles. We use this to implement the mapping from
46 * gl_FrontColor/gl_BackColor to gl_Color.
47 *
48 * urb_entry_read_offset is the offset into the VUE at which the SF unit is
49 * being instructed to begin reading attribute data. It can be set to a
50 * nonzero value to prevent the SF unit from wasting time reading elements of
51 * the VUE that are not needed by the fragment shader. It is measured in
52 * 256-bit increments.
53 */
54 uint32_t
55 get_attr_override(struct brw_vue_map *vue_map, int urb_entry_read_offset,
56 int fs_attr, bool two_side_color)
57 {
58 int attr_override, slot;
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 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. Behavior is undefined in this case, so
82 * just reference the first available attribute.
83 */
84 return 0;
85 }
86
87 /* Compute the location of the attribute relative to urb_entry_read_offset.
88 * Each increment of urb_entry_read_offset represents a 256-bit value, so
89 * it counts for two 128-bit VUE slots.
90 */
91 attr_override = slot - 2 * urb_entry_read_offset;
92 assert (attr_override >= 0 && attr_override < 32);
93
94 /* If we are doing two-sided color, and the VUE slot following this one
95 * represents a back-facing color, then we need to instruct the SF unit to
96 * do back-facing swizzling.
97 */
98 if (two_side_color) {
99 if (vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL0 &&
100 vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC0)
101 attr_override |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
102 else if (vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL1 &&
103 vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC1)
104 attr_override |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
105 }
106
107 return attr_override;
108 }
109
110 static void
111 upload_sf_state(struct brw_context *brw)
112 {
113 struct intel_context *intel = &brw->intel;
114 struct gl_context *ctx = &intel->ctx;
115 struct brw_vue_map vue_map;
116 uint32_t urb_entry_read_length;
117 /* CACHE_NEW_VS_PROG */
118 GLbitfield64 vs_outputs_written = brw->vs.prog_data->outputs_written;
119 /* BRW_NEW_FRAGMENT_PROGRAM */
120 uint32_t num_outputs = _mesa_bitcount_64(brw->fragment_program->Base.InputsRead);
121 /* _NEW_LIGHT */
122 bool shade_model_flat = ctx->Light.ShadeModel == GL_FLAT;
123 uint32_t dw1, dw2, dw3, dw4, dw16, dw17;
124 int i;
125 /* _NEW_BUFFER */
126 bool render_to_fbo = brw->intel.ctx.DrawBuffer->Name != 0;
127 int attr = 0, input_index = 0;
128 int urb_entry_read_offset = 1;
129 float point_size;
130 uint16_t attr_overrides[FRAG_ATTRIB_MAX];
131 bool userclip_active;
132
133 /* _NEW_TRANSFORM */
134 userclip_active = (ctx->Transform.ClipPlanesEnabled != 0);
135
136 brw_compute_vue_map(&vue_map, intel, userclip_active, vs_outputs_written);
137 urb_entry_read_length = (vue_map.num_slots + 1)/2 - urb_entry_read_offset;
138 if (urb_entry_read_length == 0) {
139 /* Setting the URB entry read length to 0 causes undefined behavior, so
140 * if we have no URB data to read, set it to 1.
141 */
142 urb_entry_read_length = 1;
143 }
144
145 dw1 =
146 GEN6_SF_SWIZZLE_ENABLE |
147 num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT |
148 urb_entry_read_length << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
149 urb_entry_read_offset << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT;
150
151 dw2 = GEN6_SF_STATISTICS_ENABLE;
152
153 /* Enable viewport transform only if no HiZ operation is progress
154 *
155 * From page 11 of the SandyBridge PRM, Volume 2, Part 1, Section 1.3, "3D
156 * Primitives Overview":
157 * RECTLIST: Viewport Mapping must be DISABLED (as is typical with the
158 * use of screen- space coordinates).
159 */
160 if (!brw->hiz.op)
161 dw2 |= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
162
163 dw3 = 0;
164 dw4 = 0;
165 dw16 = 0;
166 dw17 = 0;
167
168 /* _NEW_POLYGON */
169 if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
170 dw2 |= GEN6_SF_WINDING_CCW;
171
172 if (ctx->Polygon.OffsetFill)
173 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
174
175 if (ctx->Polygon.OffsetLine)
176 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
177
178 if (ctx->Polygon.OffsetPoint)
179 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
180
181 switch (ctx->Polygon.FrontMode) {
182 case GL_FILL:
183 dw2 |= GEN6_SF_FRONT_SOLID;
184 break;
185
186 case GL_LINE:
187 dw2 |= GEN6_SF_FRONT_WIREFRAME;
188 break;
189
190 case GL_POINT:
191 dw2 |= GEN6_SF_FRONT_POINT;
192 break;
193
194 default:
195 assert(0);
196 break;
197 }
198
199 switch (ctx->Polygon.BackMode) {
200 case GL_FILL:
201 dw2 |= GEN6_SF_BACK_SOLID;
202 break;
203
204 case GL_LINE:
205 dw2 |= GEN6_SF_BACK_WIREFRAME;
206 break;
207
208 case GL_POINT:
209 dw2 |= GEN6_SF_BACK_POINT;
210 break;
211
212 default:
213 assert(0);
214 break;
215 }
216
217 /* _NEW_SCISSOR */
218 if (ctx->Scissor.Enabled)
219 dw3 |= GEN6_SF_SCISSOR_ENABLE;
220
221 /* _NEW_POLYGON */
222 if (ctx->Polygon.CullFlag) {
223 switch (ctx->Polygon.CullFaceMode) {
224 case GL_FRONT:
225 dw3 |= GEN6_SF_CULL_FRONT;
226 break;
227 case GL_BACK:
228 dw3 |= GEN6_SF_CULL_BACK;
229 break;
230 case GL_FRONT_AND_BACK:
231 dw3 |= GEN6_SF_CULL_BOTH;
232 break;
233 default:
234 assert(0);
235 break;
236 }
237 } else {
238 dw3 |= GEN6_SF_CULL_NONE;
239 }
240
241 /* _NEW_LINE */
242 dw3 |= U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7) <<
243 GEN6_SF_LINE_WIDTH_SHIFT;
244 if (ctx->Line.SmoothFlag) {
245 dw3 |= GEN6_SF_LINE_AA_ENABLE;
246 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
247 dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
248 }
249
250 /* _NEW_POINT */
251 if (!(ctx->VertexProgram.PointSizeEnabled ||
252 ctx->Point._Attenuated))
253 dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
254
255 /* Clamp to ARB_point_parameters user limits */
256 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
257
258 /* Clamp to the hardware limits and convert to fixed point */
259 dw4 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
260
261 if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT)
262 dw1 |= GEN6_SF_POINT_SPRITE_LOWERLEFT;
263
264 /* _NEW_LIGHT */
265 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
266 dw4 |=
267 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
268 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
269 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
270 } else {
271 dw4 |=
272 (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
273 }
274
275 /* Create the mapping from the FS inputs we produce to the VS outputs
276 * they source from.
277 */
278 for (; attr < FRAG_ATTRIB_MAX; attr++) {
279 enum glsl_interp_qualifier interp_qualifier =
280 brw->fragment_program->InterpQualifier[attr];
281 bool is_gl_Color = attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1;
282
283 if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
284 continue;
285
286 /* _NEW_POINT */
287 if (ctx->Point.PointSprite &&
288 (attr >= FRAG_ATTRIB_TEX0 && attr <= FRAG_ATTRIB_TEX7) &&
289 ctx->Point.CoordReplace[attr - FRAG_ATTRIB_TEX0]) {
290 dw16 |= (1 << input_index);
291 }
292
293 if (attr == FRAG_ATTRIB_PNTC)
294 dw16 |= (1 << input_index);
295
296 /* flat shading */
297 if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
298 (shade_model_flat && is_gl_Color &&
299 interp_qualifier == INTERP_QUALIFIER_NONE))
300 dw17 |= (1 << input_index);
301
302 /* The hardware can only do the overrides on 16 overrides at a
303 * time, and the other up to 16 have to be lined up so that the
304 * input index = the output index. We'll need to do some
305 * tweaking to make sure that's the case.
306 */
307 assert(input_index < 16 || attr == input_index);
308
309 /* _NEW_LIGHT | _NEW_PROGRAM */
310 attr_overrides[input_index++] =
311 get_attr_override(&vue_map, urb_entry_read_offset, attr,
312 ctx->VertexProgram._TwoSideEnabled);
313 }
314
315 for (; input_index < FRAG_ATTRIB_MAX; input_index++)
316 attr_overrides[input_index] = 0;
317
318 BEGIN_BATCH(20);
319 OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
320 OUT_BATCH(dw1);
321 OUT_BATCH(dw2);
322 OUT_BATCH(dw3);
323 OUT_BATCH(dw4);
324 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
325 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
326 OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
327 for (i = 0; i < 8; i++) {
328 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
329 }
330 OUT_BATCH(dw16); /* point sprite texcoord bitmask */
331 OUT_BATCH(dw17); /* constant interp bitmask */
332 OUT_BATCH(0); /* wrapshortest enables 0-7 */
333 OUT_BATCH(0); /* wrapshortest enables 8-15 */
334 ADVANCE_BATCH();
335 }
336
337 const struct brw_tracked_state gen6_sf_state = {
338 .dirty = {
339 .mesa = (_NEW_LIGHT |
340 _NEW_PROGRAM |
341 _NEW_POLYGON |
342 _NEW_LINE |
343 _NEW_SCISSOR |
344 _NEW_BUFFERS |
345 _NEW_POINT |
346 _NEW_TRANSFORM),
347 .brw = (BRW_NEW_CONTEXT |
348 BRW_NEW_FRAGMENT_PROGRAM |
349 BRW_NEW_HIZ),
350 .cache = CACHE_NEW_VS_PROG
351 },
352 .emit = upload_sf_state,
353 };