933686845a22282a93f93e2060605150193130d5
[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 = 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 if (slot == -1) {
71 /* This attribute does not exist in the VUE--that means that the vertex
72 * shader did not write to it. Behavior is undefined in this case, so
73 * just reference the first available attribute.
74 */
75 return 0;
76 }
77
78 /* Compute the location of the attribute relative to urb_entry_read_offset.
79 * Each increment of urb_entry_read_offset represents a 256-bit value, so
80 * it counts for two 128-bit VUE slots.
81 */
82 attr_override = slot - 2 * urb_entry_read_offset;
83 assert (attr_override >= 0 && attr_override < 32);
84
85 /* If we are doing two-sided color, and the VUE slot following this one
86 * represents a back-facing color, then we need to instruct the SF unit to
87 * do back-facing swizzling.
88 */
89 if (two_side_color) {
90 if (vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL0 &&
91 vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC0)
92 attr_override |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
93 else if (vue_map->slot_to_vert_result[slot] == VERT_RESULT_COL1 &&
94 vue_map->slot_to_vert_result[slot+1] == VERT_RESULT_BFC1)
95 attr_override |= (ATTRIBUTE_SWIZZLE_INPUTATTR_FACING << ATTRIBUTE_SWIZZLE_SHIFT);
96 }
97
98 return attr_override;
99 }
100
101 static void
102 upload_sf_state(struct brw_context *brw)
103 {
104 struct intel_context *intel = &brw->intel;
105 struct gl_context *ctx = &intel->ctx;
106 struct brw_vue_map vue_map;
107 /* CACHE_NEW_VS_PROG */
108 GLbitfield64 vs_outputs_written = brw->vs.prog_data->outputs_written;
109 uint32_t num_inputs = brw_count_bits(vs_outputs_written);
110 /* BRW_NEW_FRAGMENT_PROGRAM */
111 uint32_t num_outputs = brw_count_bits(brw->fragment_program->Base.InputsRead);
112 uint32_t dw1, dw2, dw3, dw4, dw16, dw17;
113 int i;
114 /* _NEW_BUFFER */
115 GLboolean render_to_fbo = brw->intel.ctx.DrawBuffer->Name != 0;
116 int attr = 0, input_index = 0;
117 int urb_entry_read_offset;
118 int two_side_color = (ctx->Light.Enabled && ctx->Light.Model.TwoSide);
119 float point_size;
120 uint16_t attr_overrides[FRAG_ATTRIB_MAX];
121 int nr_userclip;
122
123 /* _NEW_TRANSFORM */
124 if (ctx->Transform.ClipPlanesEnabled)
125 urb_entry_read_offset = 2;
126 else
127 urb_entry_read_offset = 1;
128 nr_userclip = brw_count_bits(ctx->Transform.ClipPlanesEnabled);
129
130 dw1 =
131 GEN6_SF_SWIZZLE_ENABLE |
132 num_outputs << GEN6_SF_NUM_OUTPUTS_SHIFT |
133 (num_inputs + 1) / 2 << GEN6_SF_URB_ENTRY_READ_LENGTH_SHIFT |
134 urb_entry_read_offset << GEN6_SF_URB_ENTRY_READ_OFFSET_SHIFT;
135 dw2 = GEN6_SF_VIEWPORT_TRANSFORM_ENABLE |
136 GEN6_SF_STATISTICS_ENABLE;
137 dw3 = 0;
138 dw4 = 0;
139 dw16 = 0;
140 dw17 = 0;
141
142 /* _NEW_POLYGON */
143 if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
144 dw2 |= GEN6_SF_WINDING_CCW;
145
146 if (ctx->Polygon.OffsetFill)
147 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
148
149 if (ctx->Polygon.OffsetLine)
150 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
151
152 if (ctx->Polygon.OffsetPoint)
153 dw2 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
154
155 switch (ctx->Polygon.FrontMode) {
156 case GL_FILL:
157 dw2 |= GEN6_SF_FRONT_SOLID;
158 break;
159
160 case GL_LINE:
161 dw2 |= GEN6_SF_FRONT_WIREFRAME;
162 break;
163
164 case GL_POINT:
165 dw2 |= GEN6_SF_FRONT_POINT;
166 break;
167
168 default:
169 assert(0);
170 break;
171 }
172
173 switch (ctx->Polygon.BackMode) {
174 case GL_FILL:
175 dw2 |= GEN6_SF_BACK_SOLID;
176 break;
177
178 case GL_LINE:
179 dw2 |= GEN6_SF_BACK_WIREFRAME;
180 break;
181
182 case GL_POINT:
183 dw2 |= GEN6_SF_BACK_POINT;
184 break;
185
186 default:
187 assert(0);
188 break;
189 }
190
191 /* _NEW_SCISSOR */
192 if (ctx->Scissor.Enabled)
193 dw3 |= GEN6_SF_SCISSOR_ENABLE;
194
195 /* _NEW_POLYGON */
196 if (ctx->Polygon.CullFlag) {
197 switch (ctx->Polygon.CullFaceMode) {
198 case GL_FRONT:
199 dw3 |= GEN6_SF_CULL_FRONT;
200 break;
201 case GL_BACK:
202 dw3 |= GEN6_SF_CULL_BACK;
203 break;
204 case GL_FRONT_AND_BACK:
205 dw3 |= GEN6_SF_CULL_BOTH;
206 break;
207 default:
208 assert(0);
209 break;
210 }
211 } else {
212 dw3 |= GEN6_SF_CULL_NONE;
213 }
214
215 /* _NEW_LINE */
216 dw3 |= U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7) <<
217 GEN6_SF_LINE_WIDTH_SHIFT;
218 if (ctx->Line.SmoothFlag) {
219 dw3 |= GEN6_SF_LINE_AA_ENABLE;
220 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
221 dw3 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
222 }
223
224 /* _NEW_POINT */
225 if (!(ctx->VertexProgram.PointSizeEnabled ||
226 ctx->Point._Attenuated))
227 dw4 |= GEN6_SF_USE_STATE_POINT_WIDTH;
228
229 /* Clamp to ARB_point_parameters user limits */
230 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
231
232 /* Clamp to the hardware limits and convert to fixed point */
233 dw4 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
234
235 if (ctx->Point.SpriteOrigin == GL_LOWER_LEFT)
236 dw1 |= GEN6_SF_POINT_SPRITE_LOWERLEFT;
237
238 /* _NEW_LIGHT */
239 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
240 dw4 |=
241 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
242 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
243 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
244 } else {
245 dw4 |=
246 (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
247 }
248
249 /* flat shading */
250 if (ctx->Light.ShadeModel == GL_FLAT) {
251 dw17 |= ((brw->fragment_program->Base.InputsRead & (FRAG_BIT_COL0 | FRAG_BIT_COL1)) >>
252 ((brw->fragment_program->Base.InputsRead & FRAG_BIT_WPOS) ? 0 : 1));
253 }
254
255 /* Create the mapping from the FS inputs we produce to the VS outputs
256 * they source from.
257 */
258 brw_compute_vue_map(&vue_map, intel, nr_userclip, two_side_color,
259 vs_outputs_written);
260 for (; attr < FRAG_ATTRIB_MAX; attr++) {
261 if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
262 continue;
263
264 /* _NEW_POINT */
265 if (ctx->Point.PointSprite &&
266 (attr >= FRAG_ATTRIB_TEX0 && attr <= FRAG_ATTRIB_TEX7) &&
267 ctx->Point.CoordReplace[attr - FRAG_ATTRIB_TEX0]) {
268 dw16 |= (1 << input_index);
269 }
270
271 if (attr == FRAG_ATTRIB_PNTC)
272 dw16 |= (1 << input_index);
273
274 /* The hardware can only do the overrides on 16 overrides at a
275 * time, and the other up to 16 have to be lined up so that the
276 * input index = the output index. We'll need to do some
277 * tweaking to make sure that's the case.
278 */
279 assert(input_index < 16 || attr == input_index);
280
281 attr_overrides[input_index++] =
282 get_attr_override(&vue_map, urb_entry_read_offset, attr,
283 two_side_color);
284 }
285
286 for (; input_index < FRAG_ATTRIB_MAX; input_index++)
287 attr_overrides[input_index] = 0;
288
289 BEGIN_BATCH(20);
290 OUT_BATCH(_3DSTATE_SF << 16 | (20 - 2));
291 OUT_BATCH(dw1);
292 OUT_BATCH(dw2);
293 OUT_BATCH(dw3);
294 OUT_BATCH(dw4);
295 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
296 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
297 OUT_BATCH_F(0.0); /* XXX: global depth offset clamp */
298 for (i = 0; i < 8; i++) {
299 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
300 }
301 OUT_BATCH(dw16); /* point sprite texcoord bitmask */
302 OUT_BATCH(dw17); /* constant interp bitmask */
303 OUT_BATCH(0); /* wrapshortest enables 0-7 */
304 OUT_BATCH(0); /* wrapshortest enables 8-15 */
305 ADVANCE_BATCH();
306 }
307
308 const struct brw_tracked_state gen6_sf_state = {
309 .dirty = {
310 .mesa = (_NEW_LIGHT |
311 _NEW_POLYGON |
312 _NEW_LINE |
313 _NEW_SCISSOR |
314 _NEW_BUFFERS |
315 _NEW_POINT |
316 _NEW_TRANSFORM),
317 .brw = (BRW_NEW_CONTEXT |
318 BRW_NEW_FRAGMENT_PROGRAM),
319 .cache = CACHE_NEW_VS_PROG
320 },
321 .emit = upload_sf_state,
322 };