vk: Add four unit tests for our lock-free data-structures
[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 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(flat_enables);
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_CONTEXT |
137 BRW_NEW_FRAGMENT_PROGRAM |
138 BRW_NEW_FS_PROG_DATA |
139 BRW_NEW_VUE_MAP_GEOM_OUT,
140 },
141 .emit = upload_sbe,
142 };
143
144 static void
145 upload_sf(struct brw_context *brw)
146 {
147 struct gl_context *ctx = &brw->ctx;
148 uint32_t dw1 = 0, dw2 = 0, dw3 = 0;
149 float point_size;
150
151 dw1 = GEN6_SF_STATISTICS_ENABLE;
152
153 if (brw->sf.viewport_transform_enable)
154 dw1 |= GEN6_SF_VIEWPORT_TRANSFORM_ENABLE;
155
156 /* _NEW_LINE */
157 uint32_t line_width_u3_7 = brw_get_line_width(brw);
158 if (brw->gen >= 9 || brw->is_cherryview) {
159 dw1 |= line_width_u3_7 << GEN9_SF_LINE_WIDTH_SHIFT;
160 } else {
161 dw2 |= line_width_u3_7 << GEN6_SF_LINE_WIDTH_SHIFT;
162 }
163
164 if (ctx->Line.SmoothFlag) {
165 dw2 |= GEN6_SF_LINE_END_CAP_WIDTH_1_0;
166 }
167
168 /* Clamp to ARB_point_parameters user limits */
169 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
170
171 /* Clamp to the hardware limits and convert to fixed point */
172 dw3 |= U_FIXED(CLAMP(point_size, 0.125, 255.875), 3);
173
174 /* _NEW_PROGRAM | _NEW_POINT */
175 if (!(ctx->VertexProgram.PointSizeEnabled || ctx->Point._Attenuated))
176 dw3 |= GEN6_SF_USE_STATE_POINT_WIDTH;
177
178 /* _NEW_POINT | _NEW_MULTISAMPLE */
179 if ((ctx->Point.SmoothFlag || ctx->Multisample._Enabled) &&
180 !ctx->Point.PointSprite) {
181 dw3 |= GEN8_SF_SMOOTH_POINT_ENABLE;
182 }
183
184 dw3 |= GEN6_SF_LINE_AA_MODE_TRUE;
185
186 /* _NEW_LIGHT */
187 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
188 dw3 |= (2 << GEN6_SF_TRI_PROVOKE_SHIFT) |
189 (2 << GEN6_SF_TRIFAN_PROVOKE_SHIFT) |
190 (1 << GEN6_SF_LINE_PROVOKE_SHIFT);
191 } else {
192 dw3 |= (1 << GEN6_SF_TRIFAN_PROVOKE_SHIFT);
193 }
194
195 BEGIN_BATCH(4);
196 OUT_BATCH(_3DSTATE_SF << 16 | (4 - 2));
197 OUT_BATCH(dw1);
198 OUT_BATCH(dw2);
199 OUT_BATCH(dw3);
200 ADVANCE_BATCH();
201 }
202
203 const struct brw_tracked_state gen8_sf_state = {
204 .dirty = {
205 .mesa = _NEW_LIGHT |
206 _NEW_PROGRAM |
207 _NEW_LINE |
208 _NEW_MULTISAMPLE |
209 _NEW_POINT,
210 .brw = BRW_NEW_CONTEXT,
211 },
212 .emit = upload_sf,
213 };
214
215 static void
216 upload_raster(struct brw_context *brw)
217 {
218 struct gl_context *ctx = &brw->ctx;
219 uint32_t dw1 = 0;
220
221 /* _NEW_BUFFERS */
222 bool render_to_fbo = _mesa_is_user_fbo(brw->ctx.DrawBuffer);
223
224 /* _NEW_POLYGON */
225 if (ctx->Polygon._FrontBit == render_to_fbo)
226 dw1 |= GEN8_RASTER_FRONT_WINDING_CCW;
227
228 if (ctx->Polygon.CullFlag) {
229 switch (ctx->Polygon.CullFaceMode) {
230 case GL_FRONT:
231 dw1 |= GEN8_RASTER_CULL_FRONT;
232 break;
233 case GL_BACK:
234 dw1 |= GEN8_RASTER_CULL_BACK;
235 break;
236 case GL_FRONT_AND_BACK:
237 dw1 |= GEN8_RASTER_CULL_BOTH;
238 break;
239 default:
240 unreachable("not reached");
241 }
242 } else {
243 dw1 |= GEN8_RASTER_CULL_NONE;
244 }
245
246 /* _NEW_POINT */
247 if (ctx->Point.SmoothFlag)
248 dw1 |= GEN8_RASTER_SMOOTH_POINT_ENABLE;
249
250 if (ctx->Multisample._Enabled)
251 dw1 |= GEN8_RASTER_API_MULTISAMPLE_ENABLE;
252
253 if (ctx->Polygon.OffsetFill)
254 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_SOLID;
255
256 if (ctx->Polygon.OffsetLine)
257 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_WIREFRAME;
258
259 if (ctx->Polygon.OffsetPoint)
260 dw1 |= GEN6_SF_GLOBAL_DEPTH_OFFSET_POINT;
261
262 switch (ctx->Polygon.FrontMode) {
263 case GL_FILL:
264 dw1 |= GEN6_SF_FRONT_SOLID;
265 break;
266 case GL_LINE:
267 dw1 |= GEN6_SF_FRONT_WIREFRAME;
268 break;
269 case GL_POINT:
270 dw1 |= GEN6_SF_FRONT_POINT;
271 break;
272
273 default:
274 unreachable("not reached");
275 }
276
277 switch (ctx->Polygon.BackMode) {
278 case GL_FILL:
279 dw1 |= GEN6_SF_BACK_SOLID;
280 break;
281 case GL_LINE:
282 dw1 |= GEN6_SF_BACK_WIREFRAME;
283 break;
284 case GL_POINT:
285 dw1 |= GEN6_SF_BACK_POINT;
286 break;
287 default:
288 unreachable("not reached");
289 }
290
291 /* _NEW_LINE */
292 if (ctx->Line.SmoothFlag)
293 dw1 |= GEN8_RASTER_LINE_AA_ENABLE;
294
295 /* _NEW_SCISSOR */
296 if (ctx->Scissor.EnableFlags)
297 dw1 |= GEN8_RASTER_SCISSOR_ENABLE;
298
299 /* _NEW_TRANSFORM */
300 if (!ctx->Transform.DepthClamp) {
301 if (brw->gen >= 9) {
302 dw1 |= GEN9_RASTER_VIEWPORT_Z_NEAR_CLIP_TEST_ENABLE |
303 GEN9_RASTER_VIEWPORT_Z_FAR_CLIP_TEST_ENABLE;
304 } else {
305 dw1 |= GEN8_RASTER_VIEWPORT_Z_CLIP_TEST_ENABLE;
306 }
307 }
308
309 BEGIN_BATCH(5);
310 OUT_BATCH(_3DSTATE_RASTER << 16 | (5 - 2));
311 OUT_BATCH(dw1);
312 OUT_BATCH_F(ctx->Polygon.OffsetUnits * 2); /* constant. copied from gen4 */
313 OUT_BATCH_F(ctx->Polygon.OffsetFactor); /* scale */
314 OUT_BATCH_F(ctx->Polygon.OffsetClamp); /* global depth offset clamp */
315 ADVANCE_BATCH();
316 }
317
318 const struct brw_tracked_state gen8_raster_state = {
319 .dirty = {
320 .mesa = _NEW_BUFFERS |
321 _NEW_LINE |
322 _NEW_MULTISAMPLE |
323 _NEW_POINT |
324 _NEW_POLYGON |
325 _NEW_SCISSOR |
326 _NEW_TRANSFORM,
327 .brw = BRW_NEW_CONTEXT,
328 },
329 .emit = upload_raster,
330 };