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