i965: Enable ARB_stencil_texturing for Haswell
[mesa.git] / src / mesa / drivers / dri / i965 / gen8_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(struct brw_context *brw)
34 {
35 struct gl_context *ctx = &brw->ctx;
36 /* BRW_NEW_FS_PROG_DATA */
37 uint32_t num_outputs = brw->wm.prog_data->num_varying_inputs;
38 uint16_t attr_overrides[VARYING_SLOT_MAX];
39 uint32_t urb_entry_read_length;
40 uint32_t urb_entry_read_offset;
41 uint32_t point_sprite_enables;
42 int sbe_cmd_length;
43
44 uint32_t dw1 =
45 GEN7_SBE_SWIZZLE_ENABLE |
46 num_outputs << GEN7_SBE_NUM_OUTPUTS_SHIFT;
47 uint32_t dw4 = 0;
48 uint32_t dw5 = 0;
49
50 /* _NEW_BUFFERS */
51 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
52
53 /* _NEW_POINT
54 *
55 * Window coordinates in an FBO are inverted, which means point
56 * sprite origin must be inverted.
57 */
58 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo)
59 dw1 |= GEN6_SF_POINT_SPRITE_LOWERLEFT;
60 else
61 dw1 |= GEN6_SF_POINT_SPRITE_UPPERLEFT;
62
63 /* BRW_NEW_VUE_MAP_GEOM_OUT | BRW_NEW_FRAGMENT_PROGRAM |
64 * _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM | BRW_NEW_FS_PROG_DATA
65 */
66 calculate_attr_overrides(brw, attr_overrides,
67 &point_sprite_enables,
68 &urb_entry_read_length,
69 &urb_entry_read_offset);
70
71 /* Typically, the URB entry read length and offset should be programmed in
72 * 3DSTATE_VS and 3DSTATE_GS; SBE inherits it from the last active stage
73 * which produces geometry. However, we don't know the proper value until
74 * we call calculate_attr_overrides().
75 *
76 * To fit with our existing code, we override the inherited values and
77 * specify it here directly, as we did on previous generations.
78 */
79 dw1 |=
80 urb_entry_read_length << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
81 urb_entry_read_offset << GEN8_SBE_URB_ENTRY_READ_OFFSET_SHIFT |
82 GEN8_SBE_FORCE_URB_ENTRY_READ_LENGTH |
83 GEN8_SBE_FORCE_URB_ENTRY_READ_OFFSET;
84
85 if (brw->gen == 8) {
86 sbe_cmd_length = 4;
87 } else {
88 sbe_cmd_length = 6;
89
90 /* prepare the active component dwords */
91 int input_index = 0;
92 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
93 if (!(brw->fragment_program->Base.InputsRead & BITFIELD64_BIT(attr)))
94 continue;
95
96 assert(input_index < 32);
97
98 if (input_index < 16)
99 dw4 |= (GEN9_SBE_ACTIVE_COMPONENT_XYZW << (input_index << 1));
100 else
101 dw5 |= (GEN9_SBE_ACTIVE_COMPONENT_XYZW << ((input_index - 16) << 1));
102
103 ++input_index;
104 }
105 }
106 BEGIN_BATCH(sbe_cmd_length);
107 OUT_BATCH(_3DSTATE_SBE << 16 | (sbe_cmd_length - 2));
108 OUT_BATCH(dw1);
109 OUT_BATCH(point_sprite_enables);
110 OUT_BATCH(brw->wm.prog_data->flat_inputs);
111 if (sbe_cmd_length >= 6) {
112 OUT_BATCH(dw4);
113 OUT_BATCH(dw5);
114 }
115 ADVANCE_BATCH();
116
117 BEGIN_BATCH(11);
118 OUT_BATCH(_3DSTATE_SBE_SWIZ << 16 | (11 - 2));
119
120 /* Output DWords 1 through 8: */
121 for (int i = 0; i < 8; i++) {
122 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
123 }
124
125 OUT_BATCH(0); /* wrapshortest enables 0-7 */
126 OUT_BATCH(0); /* wrapshortest enables 8-15 */
127 ADVANCE_BATCH();
128 }
129
130 const struct brw_tracked_state gen8_sbe_state = {
131 .dirty = {
132 .mesa = _NEW_BUFFERS |
133 _NEW_LIGHT |
134 _NEW_POINT |
135 _NEW_PROGRAM,
136 .brw = BRW_NEW_BLORP |
137 BRW_NEW_CONTEXT |
138 BRW_NEW_FRAGMENT_PROGRAM |
139 BRW_NEW_FS_PROG_DATA |
140 BRW_NEW_VUE_MAP_GEOM_OUT,
141 },
142 .emit = upload_sbe,
143 };
144
145 static void
146 upload_sf(struct brw_context *brw)
147 {
148 struct gl_context *ctx = &brw->ctx;
149 uint32_t dw1 = 0, dw2 = 0, dw3 = 0;
150 float point_size;
151
152 dw1 = GEN6_SF_STATISTICS_ENABLE;
153
154 if (brw->sf.viewport_transform_enable)
155 dw1 |= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
156
157 /* _NEW_LINE */
158 uint32_t line_width_u3_7 = brw_get_line_width(brw);
159 if (brw->gen >= 9 || brw->is_cherryview) {
160 dw1 |= line_width_u3_7 << GEN9_SF_LINE_WIDTH_SHIFT;
161 } else {
162 dw2 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
163 }
164
165 if (ctx->Line.SmoothFlag) {
166 dw2 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
167 }
168
169 /* _NEW_POINT - Clamp to ARB_point_parameters user limits */
170 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
171
172 /* Clamp to the hardware limits and convert to fixed point */
173 dw3 |= U_FIXED(CLAMP(point_size, 0.125f, 255.875f), 3);
174
175 /* _NEW_PROGRAM | _NEW_POINT, BRW_NEW_VUE_MAP_GEOM_OUT */
176 if (use_state_point_size(brw))
177 dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
178
179 /* _NEW_POINT | _NEW_MULTISAMPLE */
180 if ((ctx->Point.SmoothFlag || _mesa_is_multisample_enabled(ctx)) &&
181 !ctx->Point.PointSprite) {
182 dw3 |= GEN8_SF_SMOOTH_POINT_ENABLE;
183 }
184
185 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
186
187 /* _NEW_LIGHT */
188 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
189 dw3 |= (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
190 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
191 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
192 } else {
193 dw3 |= (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
194 }
195
196 BEGIN_BATCH(4);
197 OUT_BATCH(_3DSTATE_SF << 16 | (4 - 2));
198 OUT_BATCH(dw1);
199 OUT_BATCH(dw2);
200 OUT_BATCH(dw3);
201 ADVANCE_BATCH();
202 }
203
204 const struct brw_tracked_state gen8_sf_state = {
205 .dirty = {
206 .mesa = _NEW_LIGHT |
207 _NEW_PROGRAM |
208 _NEW_LINE |
209 _NEW_MULTISAMPLE |
210 _NEW_POINT,
211 .brw = BRW_NEW_BLORP |
212 BRW_NEW_CONTEXT |
213 BRW_NEW_VUE_MAP_GEOM_OUT,
214 },
215 .emit = upload_sf,
216 };
217
218 static void
219 upload_raster(struct brw_context *brw)
220 {
221 struct gl_context *ctx = &brw->ctx;
222 uint32_t dw1 = 0;
223
224 /* _NEW_BUFFERS */
225 bool render_to_fbo = _mesa_is_user_fbo(brw->ctx.DrawBuffer);
226
227 /* _NEW_POLYGON */
228 if (ctx->Polygon._FrontBit == render_to_fbo)
229 dw1 |= GEN8_RASTER_FRONT_WINDING_CCW;
230
231 if (ctx->Polygon.CullFlag) {
232 switch (ctx->Polygon.CullFaceMode) {
233 case GL_FRONT:
234 dw1 |= GEN8_RASTER_CULL_FRONT;
235 break;
236 case GL_BACK:
237 dw1 |= GEN8_RASTER_CULL_BACK;
238 break;
239 case GL_FRONT_AND_BACK:
240 dw1 |= GEN8_RASTER_CULL_BOTH;
241 break;
242 default:
243 unreachable("not reached");
244 }
245 } else {
246 dw1 |= GEN8_RASTER_CULL_NONE;
247 }
248
249 /* _NEW_POINT */
250 if (ctx->Point.SmoothFlag)
251 dw1 |= GEN8_RASTER_SMOOTH_POINT_ENABLE;
252
253 if (_mesa_is_multisample_enabled(ctx))
254 dw1 |= GEN8_RASTER_API_MULTISAMPLE_ENABLE;
255
256 if (ctx->Polygon.OffsetFill)
257 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
258
259 if (ctx->Polygon.OffsetLine)
260 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
261
262 if (ctx->Polygon.OffsetPoint)
263 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
264
265 switch (ctx->Polygon.FrontMode) {
266 case GL_FILL:
267 dw1 |= GEN6_SF_FRONT_SOLID;
268 break;
269 case GL_LINE:
270 dw1 |= GEN6_SF_FRONT_WIREFRAME;
271 break;
272 case GL_POINT:
273 dw1 |= GEN6_SF_FRONT_POINT;
274 break;
275
276 default:
277 unreachable("not reached");
278 }
279
280 switch (ctx->Polygon.BackMode) {
281 case GL_FILL:
282 dw1 |= GEN6_SF_BACK_SOLID;
283 break;
284 case GL_LINE:
285 dw1 |= GEN6_SF_BACK_WIREFRAME;
286 break;
287 case GL_POINT:
288 dw1 |= GEN6_SF_BACK_POINT;
289 break;
290 default:
291 unreachable("not reached");
292 }
293
294 /* _NEW_LINE */
295 if (ctx->Line.SmoothFlag)
296 dw1 |= GEN8_RASTER_LINE_AA_ENABLE;
297
298 /* _NEW_SCISSOR */
299 if (ctx->Scissor.EnableFlags)
300 dw1 |= GEN8_RASTER_SCISSOR_ENABLE;
301
302 /* _NEW_TRANSFORM */
303 if (!ctx->Transform.DepthClamp) {
304 if (brw->gen >= 9) {
305 dw1 |= GEN9_RASTER_VIEWPORT_Z_NEAR_CLIP_TEST_ENABLE |
306 GEN9_RASTER_VIEWPORT_Z_FAR_CLIP_TEST_ENABLE;
307 } else {
308 dw1 |= GEN8_RASTER_VIEWPORT_Z_CLIP_TEST_ENABLE;
309 }
310 }
311
312 BEGIN_BATCH(5);
313 OUT_BATCH(_3DSTATE_RASTER << 16 | (5 - 2));
314 OUT_BATCH(dw1);
315 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
316 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
317 OUT_BATCH_F(ctx->Polygon.OffsetClamp); /* global depth offset clamp */
318 ADVANCE_BATCH();
319 }
320
321 const struct brw_tracked_state gen8_raster_state = {
322 .dirty = {
323 .mesa = _NEW_BUFFERS |
324 _NEW_LINE |
325 _NEW_MULTISAMPLE |
326 _NEW_POINT |
327 _NEW_POLYGON |
328 _NEW_SCISSOR |
329 _NEW_TRANSFORM,
330 .brw = BRW_NEW_BLORP |
331 BRW_NEW_CONTEXT,
332 },
333 .emit = upload_raster,
334 };