i965: Rename CACHE_NEW_*_PROG to BRW_NEW_*_PROG_DATA.
[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 point_sprite_enables;
41 uint32_t flat_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 &flat_enables,
69 &urb_entry_read_length);
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 BRW_SF_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 if (input_index < 16)
97 dw4 |= (GEN9_SBE_ACTIVE_COMPONENT_XYZW << (input_index << 1));
98 else
99 dw5 |= (GEN9_SBE_ACTIVE_COMPONENT_XYZW << (input_index << 1));
100
101 ++input_index;
102 }
103 }
104 BEGIN_BATCH(sbe_cmd_length);
105 OUT_BATCH(_3DSTATE_SBE << 16 | (sbe_cmd_length - 2));
106 OUT_BATCH(dw1);
107 OUT_BATCH(point_sprite_enables);
108 OUT_BATCH(flat_enables);
109 if (sbe_cmd_length >= 6) {
110 OUT_BATCH(dw4);
111 OUT_BATCH(dw5);
112 }
113 ADVANCE_BATCH();
114
115 BEGIN_BATCH(11);
116 OUT_BATCH(_3DSTATE_SBE_SWIZ << 16 | (11 - 2));
117
118 /* Output DWords 1 through 8: */
119 for (int i = 0; i < 8; i++) {
120 OUT_BATCH(attr_overrides[i * 2] | attr_overrides[i * 2 + 1] << 16);
121 }
122
123 OUT_BATCH(0); /* wrapshortest enables 0-7 */
124 OUT_BATCH(0); /* wrapshortest enables 8-15 */
125 ADVANCE_BATCH();
126 }
127
128 const struct brw_tracked_state gen8_sbe_state = {
129 .dirty = {
130 .mesa = _NEW_BUFFERS |
131 _NEW_LIGHT |
132 _NEW_POINT |
133 _NEW_PROGRAM,
134 .brw = BRW_NEW_CONTEXT |
135 BRW_NEW_FRAGMENT_PROGRAM |
136 BRW_NEW_VUE_MAP_GEOM_OUT,
137 .cache = BRW_NEW_FS_PROG_DATA,
138 },
139 .emit = upload_sbe,
140 };
141
142 static void
143 upload_sf(struct brw_context *brw)
144 {
145 struct gl_context *ctx = &brw->ctx;
146 uint32_t dw1 = 0, dw2 = 0, dw3 = 0;
147 float point_size;
148
149 dw1 = GEN6_SF_STATISTICS_ENABLE;
150
151 if (brw->sf.viewport_transform_enable)
152 dw1 |= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
153
154 /* _NEW_LINE */
155 uint32_t line_width_u3_7 =
156 U_FIXED(CLAMP(ctx->Line.Width, 0.0, ctx->Const.MaxLineWidth), 7);
157 if (line_width_u3_7 == 0)
158 line_width_u3_7 = 1;
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 /* 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.125, 255.875), 3);
174
175 /* _NEW_PROGRAM | _NEW_POINT */
176 if (!(ctx->VertexProgram.PointSizeEnabled || ctx->Point._Attenuated))
177 dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
178
179 /* _NEW_POINT | _NEW_MULTISAMPLE */
180 if ((ctx->Point.SmoothFlag || ctx->Multisample._Enabled) &&
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_CONTEXT,
212 .cache = 0,
213 },
214 .emit = upload_sf,
215 };
216
217 static void
218 upload_raster(struct brw_context *brw)
219 {
220 struct gl_context *ctx = &brw->ctx;
221 uint32_t dw1 = 0;
222
223 /* _NEW_BUFFERS */
224 bool render_to_fbo = _mesa_is_user_fbo(brw->ctx.DrawBuffer);
225
226 /* _NEW_POLYGON */
227 if ((ctx->Polygon.FrontFace == GL_CCW) ^ render_to_fbo)
228 dw1 |= GEN8_RASTER_FRONT_WINDING_CCW;
229
230 if (ctx->Polygon.CullFlag) {
231 switch (ctx->Polygon.CullFaceMode) {
232 case GL_FRONT:
233 dw1 |= GEN8_RASTER_CULL_FRONT;
234 break;
235 case GL_BACK:
236 dw1 |= GEN8_RASTER_CULL_BACK;
237 break;
238 case GL_FRONT_AND_BACK:
239 dw1 |= GEN8_RASTER_CULL_BOTH;
240 break;
241 default:
242 unreachable("not reached");
243 }
244 } else {
245 dw1 |= GEN8_RASTER_CULL_NONE;
246 }
247
248 /* _NEW_POINT */
249 if (ctx->Point.SmoothFlag)
250 dw1 |= GEN8_RASTER_SMOOTH_POINT_ENABLE;
251
252 if (ctx->Multisample._Enabled)
253 dw1 |= GEN8_RASTER_API_MULTISAMPLE_ENABLE;
254
255 if (ctx->Polygon.OffsetFill)
256 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
257
258 if (ctx->Polygon.OffsetLine)
259 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
260
261 if (ctx->Polygon.OffsetPoint)
262 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
263
264 switch (ctx->Polygon.FrontMode) {
265 case GL_FILL:
266 dw1 |= GEN6_SF_FRONT_SOLID;
267 break;
268 case GL_LINE:
269 dw1 |= GEN6_SF_FRONT_WIREFRAME;
270 break;
271 case GL_POINT:
272 dw1 |= GEN6_SF_FRONT_POINT;
273 break;
274
275 default:
276 unreachable("not reached");
277 }
278
279 switch (ctx->Polygon.BackMode) {
280 case GL_FILL:
281 dw1 |= GEN6_SF_BACK_SOLID;
282 break;
283 case GL_LINE:
284 dw1 |= GEN6_SF_BACK_WIREFRAME;
285 break;
286 case GL_POINT:
287 dw1 |= GEN6_SF_BACK_POINT;
288 break;
289 default:
290 unreachable("not reached");
291 }
292
293 /* _NEW_LINE */
294 if (ctx->Line.SmoothFlag)
295 dw1 |= GEN8_RASTER_LINE_AA_ENABLE;
296
297 /* _NEW_SCISSOR */
298 if (ctx->Scissor.EnableFlags)
299 dw1 |= GEN8_RASTER_SCISSOR_ENABLE;
300
301 /* _NEW_TRANSFORM */
302 if (!ctx->Transform.DepthClamp) {
303 if (brw->gen >= 9) {
304 dw1 |= GEN9_RASTER_VIEWPORT_Z_NEAR_CLIP_TEST_ENABLE |
305 GEN9_RASTER_VIEWPORT_Z_FAR_CLIP_TEST_ENABLE;
306 } else {
307 dw1 |= GEN8_RASTER_VIEWPORT_Z_CLIP_TEST_ENABLE;
308 }
309 }
310
311 BEGIN_BATCH(5);
312 OUT_BATCH(_3DSTATE_RASTER << 16 | (5 - 2));
313 OUT_BATCH(dw1);
314 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
315 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
316 OUT_BATCH_F(0.0);
317 ADVANCE_BATCH();
318 }
319
320 const struct brw_tracked_state gen8_raster_state = {
321 .dirty = {
322 .mesa = _NEW_BUFFERS |
323 _NEW_LINE |
324 _NEW_MULTISAMPLE |
325 _NEW_POINT |
326 _NEW_POLYGON |
327 _NEW_SCISSOR |
328 _NEW_TRANSFORM,
329 .brw = BRW_NEW_CONTEXT,
330 .cache = 0,
331 },
332 .emit = upload_raster,
333 };