28c650838448244a6ea9dc290fe222bc369ed8bf
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_sf_state.c
1 /*
2 * Copyright © 2011 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
24 #include "brw_context.h"
25 #include "brw_state.h"
26 #include "brw_defines.h"
27 #include "brw_util.h"
28 #include "main/macros.h"
29 #include "main/fbobject.h"
30 #include "intel_batchbuffer.h"
31
32 static void
33 upload_sbe_state(struct brw_context *brw)
34 {
35 struct intel_context *intel = &brw->intel;
36 struct gl_context *ctx = &intel->ctx;
37 uint32_t urb_entry_read_length;
38 /* BRW_NEW_FRAGMENT_PROGRAM */
39 uint32_t num_outputs = _mesa_bitcount_64(brw->fragment_program->Base.InputsRead);
40 /* _NEW_LIGHT */
41 bool shade_model_flat = ctx->Light.ShadeModel == GL_FLAT;
42 uint32_t dw1, dw10, dw11;
43 int i;
44 int attr = 0, input_index = 0;
45 int urb_entry_read_offset = 1;
46 uint16_t attr_overrides[FRAG_ATTRIB_MAX];
47 /* _NEW_BUFFERS */
48 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
49 uint32_t point_sprite_origin;
50
51 /* CACHE_NEW_VS_PROG */
52 urb_entry_read_length = ((brw->vs.prog_data->vue_map.num_slots + 1) / 2 -
53 urb_entry_read_offset);
54 if (urb_entry_read_length == 0) {
55 /* Setting the URB entry read length to 0 causes undefined behavior, so
56 * if we have no URB data to read, set it to 1.
57 */
58 urb_entry_read_length = 1;
59 }
60
61 /* FINISHME: Attribute Swizzle Control Mode? */
62 dw1 =
63 GEN7_SBE_SWIZZLE_ENABLE |
64 num_outputs << GEN7_SBE_NUM_OUTPUTS_SHIFT |
65 urb_entry_read_length << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
66 urb_entry_read_offset << GEN7_SBE_URB_ENTRY_READ_OFFSET_SHIFT;
67
68 /* _NEW_POINT
69 *
70 * Window coordinates in an FBO are inverted, which means point
71 * sprite origin must be inverted.
72 */
73 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
74 point_sprite_origin = GEN6_SF_POINT_SPRITE_LOWERLEFT;
75 } else {
76 point_sprite_origin = GEN6_SF_POINT_SPRITE_UPPERLEFT;
77 }
78 dw1 |= point_sprite_origin;
79
80
81 dw10 = 0;
82 dw11 = 0;
83
84 /* Create the mapping from the FS inputs we produce to the VS outputs
85 * they source from.
86 */
87 for (; attr < FRAG_ATTRIB_MAX; attr++) {
88 enum glsl_interp_qualifier interp_qualifier =
89 brw->fragment_program->InterpQualifier[attr];
90 bool is_gl_Color = attr == FRAG_ATTRIB_COL0 || attr == FRAG_ATTRIB_COL1;
91
92 if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
93 continue;
94
95 if (ctx->Point.PointSprite &&
96 attr >= FRAG_ATTRIB_TEX0 && attr <= FRAG_ATTRIB_TEX7 &&
97 ctx->Point.CoordReplace[attr - FRAG_ATTRIB_TEX0]) {
98 dw10 |= (1 << input_index);
99 }
100
101 if (attr == FRAG_ATTRIB_PNTC)
102 dw10 |= (1 << input_index);
103
104 /* flat shading */
105 if (interp_qualifier == INTERP_QUALIFIER_FLAT ||
106 (shade_model_flat && is_gl_Color &&
107 interp_qualifier == INTERP_QUALIFIER_NONE))
108 dw11 |= (1 << input_index);
109
110 /* The hardware can only do the overrides on 16 overrides at a
111 * time, and the other up to 16 have to be lined up so that the
112 * input index = the output index. We'll need to do some
113 * tweaking to make sure that's the case.
114 */
115 assert(input_index < 16 || attr == input_index);
116
117 /* CACHE_NEW_VS_PROG | _NEW_LIGHT | _NEW_PROGRAM */
118 attr_overrides[input_index++] =
119 get_attr_override(&brw->vs.prog_data->vue_map,
120 urb_entry_read_offset, attr,
121 ctx->VertexProgram._TwoSideEnabled);
122 }
123
124 for (; input_index < FRAG_ATTRIB_MAX; input_index++)
125 attr_overrides[input_index] = 0;
126
127 BEGIN_BATCH(14);
128 OUT_BATCH(_3DSTATE_SBE << 16 | (14 - 2));
129 OUT_BATCH(dw1);
130
131 /* Output dwords 2 through 9 */
132 for (i = 0; i < 8; i++) {
133 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
134 }
135
136 OUT_BATCH(dw10); /* point sprite texcoord bitmask */
137 OUT_BATCH(dw11); /* constant interp bitmask */
138 OUT_BATCH(0); /* wrapshortest enables 0-7 */
139 OUT_BATCH(0); /* wrapshortest enables 8-15 */
140 ADVANCE_BATCH();
141 }
142
143 const struct brw_tracked_state gen7_sbe_state = {
144 .dirty = {
145 .mesa = (_NEW_LIGHT |
146 _NEW_POINT |
147 _NEW_PROGRAM),
148 .brw = (BRW_NEW_CONTEXT |
149 BRW_NEW_FRAGMENT_PROGRAM),
150 .cache = CACHE_NEW_VS_PROG
151 },
152 .emit = upload_sbe_state,
153 };
154
155 static void
156 upload_sf_state(struct brw_context *brw)
157 {
158 struct intel_context *intel = &brw->intel;
159 struct gl_context *ctx = &intel->ctx;
160 uint32_t dw1, dw2, dw3;
161 float point_size;
162 /* _NEW_BUFFERS */
163 bool render_to_fbo = _mesa_is_user_fbo(brw->intel.ctx.DrawBuffer);
164 bool multisampled_fbo = ctx->DrawBuffer->Visual.samples > 1;
165
166 dw1 = GEN6_SF_STATISTICS_ENABLE |
167 GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
168
169 /* _NEW_BUFFERS */
170 dw1 |= (brw_depthbuffer_format(brw) << GEN7_SF_DEPTH_BUFFER_SURFACE_FORMAT_SHIFT);
171
172 /* _NEW_POLYGON */
173 if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
174 dw1 |= GEN6_SF_WINDING_CCW;
175
176 if (ctx->Polygon.OffsetFill)
177 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
178
179 if (ctx->Polygon.OffsetLine)
180 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
181
182 if (ctx->Polygon.OffsetPoint)
183 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
184
185 switch (ctx->Polygon.FrontMode) {
186 case GL_FILL:
187 dw1 |= GEN6_SF_FRONT_SOLID;
188 break;
189
190 case GL_LINE:
191 dw1 |= GEN6_SF_FRONT_WIREFRAME;
192 break;
193
194 case GL_POINT:
195 dw1 |= GEN6_SF_FRONT_POINT;
196 break;
197
198 default:
199 assert(0);
200 break;
201 }
202
203 switch (ctx->Polygon.BackMode) {
204 case GL_FILL:
205 dw1 |= GEN6_SF_BACK_SOLID;
206 break;
207
208 case GL_LINE:
209 dw1 |= GEN6_SF_BACK_WIREFRAME;
210 break;
211
212 case GL_POINT:
213 dw1 |= GEN6_SF_BACK_POINT;
214 break;
215
216 default:
217 assert(0);
218 break;
219 }
220
221 dw2 = 0;
222
223 if (ctx->Polygon.CullFlag) {
224 switch (ctx->Polygon.CullFaceMode) {
225 case GL_FRONT:
226 dw2 |= GEN6_SF_CULL_FRONT;
227 break;
228 case GL_BACK:
229 dw2 |= GEN6_SF_CULL_BACK;
230 break;
231 case GL_FRONT_AND_BACK:
232 dw2 |= GEN6_SF_CULL_BOTH;
233 break;
234 default:
235 assert(0);
236 break;
237 }
238 } else {
239 dw2 |= GEN6_SF_CULL_NONE;
240 }
241
242 /* _NEW_SCISSOR */
243 if (ctx->Scissor.Enabled)
244 dw2 |= GEN6_SF_SCISSOR_ENABLE;
245
246 /* _NEW_LINE */
247 {
248 uint32_t line_width_u3_7 = U_FIXED(CLAMP(ctx->Line.Width, 0.0, 7.99), 7);
249 /* TODO: line width of 0 is not allowed when MSAA enabled */
250 if (line_width_u3_7 == 0)
251 line_width_u3_7 = 1;
252 dw2 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
253 }
254 if (ctx->Line.SmoothFlag) {
255 dw2 |= GEN6_SF_LINE_AA_ENABLE;
256 dw2 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
257 }
258 if (ctx->Line.StippleFlag && intel->is_haswell) {
259 dw2 |= HSW_SF_LINE_STIPPLE_ENABLE;
260 }
261 /* _NEW_MULTISAMPLE */
262 if (multisampled_fbo && ctx->Multisample.Enabled)
263 dw2 |= GEN6_SF_MSRAST_ON_PATTERN;
264
265 /* FINISHME: Last Pixel Enable? Vertex Sub Pixel Precision Select?
266 */
267
268 dw3 = GEN6_SF_LINE_AA_MODE_TRUE;
269
270 /* _NEW_PROGRAM | _NEW_POINT */
271 if (!(ctx->VertexProgram.PointSizeEnabled || ctx->Point._Attenuated))
272 dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
273
274 /* Clamp to ARB_point_parameters user limits */
275 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
276
277 /* Clamp to the hardware limits and convert to fixed point */
278 dw3 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
279
280 /* _NEW_LIGHT */
281 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
282 dw3 |=
283 (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
284 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
285 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
286 } else {
287 dw3 |= (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
288 }
289
290 BEGIN_BATCH(7);
291 OUT_BATCH(_3DSTATE_SF << 16 | (7 - 2));
292 OUT_BATCH(dw1);
293 OUT_BATCH(dw2);
294 OUT_BATCH(dw3);
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 ADVANCE_BATCH();
299 }
300
301 const struct brw_tracked_state gen7_sf_state = {
302 .dirty = {
303 .mesa = (_NEW_LIGHT |
304 _NEW_PROGRAM |
305 _NEW_POLYGON |
306 _NEW_LINE |
307 _NEW_SCISSOR |
308 _NEW_BUFFERS |
309 _NEW_POINT |
310 _NEW_MULTISAMPLE),
311 .brw = BRW_NEW_CONTEXT,
312 .cache = CACHE_NEW_VS_PROG
313 },
314 .emit = upload_sf_state,
315 };