iris: fix whitespace
[mesa.git] / src / gallium / drivers / iris / iris_state.c
1 /*
2 * Copyright © 2017 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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdio.h>
24 #include <errno.h>
25
26 #ifdef HAVE_VALGRIND
27 #include <valgrind.h>
28 #include <memcheck.h>
29 #define VG(x) x
30 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
31 #else
32 #define VG(x)
33 #endif
34
35 #include "pipe/p_defines.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_screen.h"
39 #include "util/u_inlines.h"
40 #include "util/u_format.h"
41 #include "util/u_transfer.h"
42 #include "util/u_upload_mgr.h"
43 #include "i915_drm.h"
44 #include "nir.h"
45 #include "intel/compiler/brw_compiler.h"
46 #include "intel/common/gen_l3_config.h"
47 #include "intel/common/gen_sample_positions.h"
48 #include "iris_batch.h"
49 #include "iris_context.h"
50 #include "iris_pipe.h"
51 #include "iris_resource.h"
52
53 #define __gen_address_type struct iris_address
54 #define __gen_user_data struct iris_batch
55
56 #define ARRAY_BYTES(x) (sizeof(uint32_t) * ARRAY_SIZE(x))
57
58 static uint64_t
59 __gen_combine_address(struct iris_batch *batch, void *location,
60 struct iris_address addr, uint32_t delta)
61 {
62 uint64_t result = addr.offset + delta;
63
64 if (addr.bo) {
65 iris_use_pinned_bo(batch, addr.bo, addr.write);
66 /* Assume this is a general address, not relative to a base. */
67 result += addr.bo->gtt_offset;
68 }
69
70 return result;
71 }
72
73 #define __genxml_cmd_length(cmd) cmd ## _length
74 #define __genxml_cmd_length_bias(cmd) cmd ## _length_bias
75 #define __genxml_cmd_header(cmd) cmd ## _header
76 #define __genxml_cmd_pack(cmd) cmd ## _pack
77
78 #define _iris_pack_command(batch, cmd, dst, name) \
79 for (struct cmd name = { __genxml_cmd_header(cmd) }, \
80 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
81 ({ __genxml_cmd_pack(cmd)(batch, (void *)_dst, &name); \
82 _dst = NULL; \
83 }))
84
85 #define iris_pack_command(cmd, dst, name) \
86 _iris_pack_command(NULL, cmd, dst, name)
87
88 #define iris_pack_state(cmd, dst, name) \
89 for (struct cmd name = {}, \
90 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
91 __genxml_cmd_pack(cmd)(NULL, (void *)_dst, &name), \
92 _dst = NULL)
93
94 #define iris_emit_cmd(batch, cmd, name) \
95 _iris_pack_command(batch, cmd, iris_get_command_space(batch, 4 * __genxml_cmd_length(cmd)), name)
96
97 #define iris_emit_merge(batch, dwords0, dwords1, num_dwords) \
98 do { \
99 uint32_t *dw = iris_get_command_space(batch, 4 * num_dwords); \
100 for (uint32_t i = 0; i < num_dwords; i++) \
101 dw[i] = (dwords0)[i] | (dwords1)[i]; \
102 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, num_dwords)); \
103 } while (0)
104
105 #include "genxml/genX_pack.h"
106 #include "genxml/gen_macros.h"
107 #include "genxml/genX_bits.h"
108
109 #define MOCS_WB (2 << 1)
110
111 UNUSED static void pipe_asserts()
112 {
113 #define PIPE_ASSERT(x) STATIC_ASSERT((int)x)
114
115 /* pipe_logicop happens to match the hardware. */
116 PIPE_ASSERT(PIPE_LOGICOP_CLEAR == LOGICOP_CLEAR);
117 PIPE_ASSERT(PIPE_LOGICOP_NOR == LOGICOP_NOR);
118 PIPE_ASSERT(PIPE_LOGICOP_AND_INVERTED == LOGICOP_AND_INVERTED);
119 PIPE_ASSERT(PIPE_LOGICOP_COPY_INVERTED == LOGICOP_COPY_INVERTED);
120 PIPE_ASSERT(PIPE_LOGICOP_AND_REVERSE == LOGICOP_AND_REVERSE);
121 PIPE_ASSERT(PIPE_LOGICOP_INVERT == LOGICOP_INVERT);
122 PIPE_ASSERT(PIPE_LOGICOP_XOR == LOGICOP_XOR);
123 PIPE_ASSERT(PIPE_LOGICOP_NAND == LOGICOP_NAND);
124 PIPE_ASSERT(PIPE_LOGICOP_AND == LOGICOP_AND);
125 PIPE_ASSERT(PIPE_LOGICOP_EQUIV == LOGICOP_EQUIV);
126 PIPE_ASSERT(PIPE_LOGICOP_NOOP == LOGICOP_NOOP);
127 PIPE_ASSERT(PIPE_LOGICOP_OR_INVERTED == LOGICOP_OR_INVERTED);
128 PIPE_ASSERT(PIPE_LOGICOP_COPY == LOGICOP_COPY);
129 PIPE_ASSERT(PIPE_LOGICOP_OR_REVERSE == LOGICOP_OR_REVERSE);
130 PIPE_ASSERT(PIPE_LOGICOP_OR == LOGICOP_OR);
131 PIPE_ASSERT(PIPE_LOGICOP_SET == LOGICOP_SET);
132
133 /* pipe_blend_func happens to match the hardware. */
134 PIPE_ASSERT(PIPE_BLENDFACTOR_ONE == BLENDFACTOR_ONE);
135 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_COLOR == BLENDFACTOR_SRC_COLOR);
136 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA == BLENDFACTOR_SRC_ALPHA);
137 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_ALPHA == BLENDFACTOR_DST_ALPHA);
138 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_COLOR == BLENDFACTOR_DST_COLOR);
139 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE == BLENDFACTOR_SRC_ALPHA_SATURATE);
140 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_COLOR == BLENDFACTOR_CONST_COLOR);
141 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_ALPHA == BLENDFACTOR_CONST_ALPHA);
142 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_COLOR == BLENDFACTOR_SRC1_COLOR);
143 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_ALPHA == BLENDFACTOR_SRC1_ALPHA);
144 PIPE_ASSERT(PIPE_BLENDFACTOR_ZERO == BLENDFACTOR_ZERO);
145 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_COLOR == BLENDFACTOR_INV_SRC_COLOR);
146 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_ALPHA == BLENDFACTOR_INV_SRC_ALPHA);
147 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_ALPHA == BLENDFACTOR_INV_DST_ALPHA);
148 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_COLOR == BLENDFACTOR_INV_DST_COLOR);
149 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_COLOR == BLENDFACTOR_INV_CONST_COLOR);
150 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_ALPHA == BLENDFACTOR_INV_CONST_ALPHA);
151 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_COLOR == BLENDFACTOR_INV_SRC1_COLOR);
152 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_ALPHA == BLENDFACTOR_INV_SRC1_ALPHA);
153
154 /* pipe_blend_func happens to match the hardware. */
155 PIPE_ASSERT(PIPE_BLEND_ADD == BLENDFUNCTION_ADD);
156 PIPE_ASSERT(PIPE_BLEND_SUBTRACT == BLENDFUNCTION_SUBTRACT);
157 PIPE_ASSERT(PIPE_BLEND_REVERSE_SUBTRACT == BLENDFUNCTION_REVERSE_SUBTRACT);
158 PIPE_ASSERT(PIPE_BLEND_MIN == BLENDFUNCTION_MIN);
159 PIPE_ASSERT(PIPE_BLEND_MAX == BLENDFUNCTION_MAX);
160
161 /* pipe_stencil_op happens to match the hardware. */
162 PIPE_ASSERT(PIPE_STENCIL_OP_KEEP == STENCILOP_KEEP);
163 PIPE_ASSERT(PIPE_STENCIL_OP_ZERO == STENCILOP_ZERO);
164 PIPE_ASSERT(PIPE_STENCIL_OP_REPLACE == STENCILOP_REPLACE);
165 PIPE_ASSERT(PIPE_STENCIL_OP_INCR == STENCILOP_INCRSAT);
166 PIPE_ASSERT(PIPE_STENCIL_OP_DECR == STENCILOP_DECRSAT);
167 PIPE_ASSERT(PIPE_STENCIL_OP_INCR_WRAP == STENCILOP_INCR);
168 PIPE_ASSERT(PIPE_STENCIL_OP_DECR_WRAP == STENCILOP_DECR);
169 PIPE_ASSERT(PIPE_STENCIL_OP_INVERT == STENCILOP_INVERT);
170
171 /* pipe_sprite_coord_mode happens to match 3DSTATE_SBE */
172 PIPE_ASSERT(PIPE_SPRITE_COORD_UPPER_LEFT == UPPERLEFT);
173 PIPE_ASSERT(PIPE_SPRITE_COORD_LOWER_LEFT == LOWERLEFT);
174 #undef PIPE_ASSERT
175 }
176
177 static unsigned
178 translate_prim_type(enum pipe_prim_type prim, uint8_t verts_per_patch)
179 {
180 static const unsigned map[] = {
181 [PIPE_PRIM_POINTS] = _3DPRIM_POINTLIST,
182 [PIPE_PRIM_LINES] = _3DPRIM_LINELIST,
183 [PIPE_PRIM_LINE_LOOP] = _3DPRIM_LINELOOP,
184 [PIPE_PRIM_LINE_STRIP] = _3DPRIM_LINESTRIP,
185 [PIPE_PRIM_TRIANGLES] = _3DPRIM_TRILIST,
186 [PIPE_PRIM_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
187 [PIPE_PRIM_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
188 [PIPE_PRIM_QUADS] = _3DPRIM_QUADLIST,
189 [PIPE_PRIM_QUAD_STRIP] = _3DPRIM_QUADSTRIP,
190 [PIPE_PRIM_POLYGON] = _3DPRIM_POLYGON,
191 [PIPE_PRIM_LINES_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
192 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
193 [PIPE_PRIM_TRIANGLES_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
194 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
195 [PIPE_PRIM_PATCHES] = _3DPRIM_PATCHLIST_1 - 1,
196 };
197
198 return map[prim] + (prim == PIPE_PRIM_PATCHES ? verts_per_patch : 0);
199 }
200
201 static unsigned
202 translate_compare_func(enum pipe_compare_func pipe_func)
203 {
204 static const unsigned map[] = {
205 [PIPE_FUNC_NEVER] = COMPAREFUNCTION_NEVER,
206 [PIPE_FUNC_LESS] = COMPAREFUNCTION_LESS,
207 [PIPE_FUNC_EQUAL] = COMPAREFUNCTION_EQUAL,
208 [PIPE_FUNC_LEQUAL] = COMPAREFUNCTION_LEQUAL,
209 [PIPE_FUNC_GREATER] = COMPAREFUNCTION_GREATER,
210 [PIPE_FUNC_NOTEQUAL] = COMPAREFUNCTION_NOTEQUAL,
211 [PIPE_FUNC_GEQUAL] = COMPAREFUNCTION_GEQUAL,
212 [PIPE_FUNC_ALWAYS] = COMPAREFUNCTION_ALWAYS,
213 };
214 return map[pipe_func];
215 }
216
217 static unsigned
218 translate_shadow_func(enum pipe_compare_func pipe_func)
219 {
220 /* Gallium specifies the result of shadow comparisons as:
221 *
222 * 1 if ref <op> texel,
223 * 0 otherwise.
224 *
225 * The hardware does:
226 *
227 * 0 if texel <op> ref,
228 * 1 otherwise.
229 *
230 * So we need to flip the operator and also negate.
231 */
232 static const unsigned map[] = {
233 [PIPE_FUNC_NEVER] = PREFILTEROPALWAYS,
234 [PIPE_FUNC_LESS] = PREFILTEROPLEQUAL,
235 [PIPE_FUNC_EQUAL] = PREFILTEROPNOTEQUAL,
236 [PIPE_FUNC_LEQUAL] = PREFILTEROPLESS,
237 [PIPE_FUNC_GREATER] = PREFILTEROPGEQUAL,
238 [PIPE_FUNC_NOTEQUAL] = PREFILTEROPEQUAL,
239 [PIPE_FUNC_GEQUAL] = PREFILTEROPGREATER,
240 [PIPE_FUNC_ALWAYS] = PREFILTEROPNEVER,
241 };
242 return map[pipe_func];
243 }
244
245 static unsigned
246 translate_cull_mode(unsigned pipe_face)
247 {
248 static const unsigned map[4] = {
249 [PIPE_FACE_NONE] = CULLMODE_NONE,
250 [PIPE_FACE_FRONT] = CULLMODE_FRONT,
251 [PIPE_FACE_BACK] = CULLMODE_BACK,
252 [PIPE_FACE_FRONT_AND_BACK] = CULLMODE_BOTH,
253 };
254 return map[pipe_face];
255 }
256
257 static unsigned
258 translate_fill_mode(unsigned pipe_polymode)
259 {
260 static const unsigned map[4] = {
261 [PIPE_POLYGON_MODE_FILL] = FILL_MODE_SOLID,
262 [PIPE_POLYGON_MODE_LINE] = FILL_MODE_WIREFRAME,
263 [PIPE_POLYGON_MODE_POINT] = FILL_MODE_POINT,
264 [PIPE_POLYGON_MODE_FILL_RECTANGLE] = FILL_MODE_SOLID,
265 };
266 return map[pipe_polymode];
267 }
268
269 static struct iris_address
270 ro_bo(struct iris_bo *bo, uint64_t offset)
271 {
272 /* Not for CSOs! */
273 return (struct iris_address) { .bo = bo, .offset = offset };
274 }
275
276 static uint32_t *
277 stream_state(struct iris_batch *batch,
278 struct u_upload_mgr *uploader,
279 unsigned size,
280 unsigned alignment,
281 uint32_t *out_offset)
282 {
283 struct pipe_resource *res = NULL;
284 void *ptr = NULL;
285
286 u_upload_alloc(uploader, 0, size, alignment, out_offset, &res, &ptr);
287
288 struct iris_bo *bo = iris_resource_bo(res);
289 iris_use_pinned_bo(batch, bo, false);
290
291 *out_offset += iris_bo_offset_from_base_address(bo);
292
293 pipe_resource_reference(&res, NULL);
294
295 return ptr;
296 }
297
298 static uint32_t
299 emit_state(struct iris_batch *batch,
300 struct u_upload_mgr *uploader,
301 const void *data,
302 unsigned size,
303 unsigned alignment)
304 {
305 unsigned offset = 0;
306 uint32_t *map = stream_state(batch, uploader, size, alignment, &offset);
307
308 if (map)
309 memcpy(map, data, size);
310
311 return offset;
312 }
313
314 static void
315 iris_init_render_context(struct iris_screen *screen,
316 struct iris_batch *batch,
317 struct iris_vtable *vtbl,
318 struct pipe_debug_callback *dbg)
319 {
320 iris_init_batch(batch, screen, vtbl, dbg, I915_EXEC_RENDER);
321
322 /* XXX: PIPE_CONTROLs */
323
324 iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
325 #if 0
326 // XXX: MOCS is stupid for this.
327 sba.GeneralStateMemoryObjectControlState = MOCS_WB;
328 sba.StatelessDataPortAccessMemoryObjectControlState = MOCS_WB;
329 sba.SurfaceStateMemoryObjectControlState = MOCS_WB;
330 sba.DynamicStateMemoryObjectControlState = MOCS_WB;
331 sba.IndirectObjectMemoryObjectControlState = MOCS_WB;
332 sba.InstructionMemoryObjectControlState = MOCS_WB;
333 sba.BindlessSurfaceStateMemoryObjectControlState = MOCS_WB;
334 #endif
335
336 sba.GeneralStateBaseAddressModifyEnable = true;
337 sba.SurfaceStateBaseAddressModifyEnable = true;
338 sba.DynamicStateBaseAddressModifyEnable = true;
339 sba.IndirectObjectBaseAddressModifyEnable = true;
340 sba.InstructionBaseAddressModifyEnable = true;
341 sba.GeneralStateBufferSizeModifyEnable = true;
342 sba.DynamicStateBufferSizeModifyEnable = true;
343 sba.BindlessSurfaceStateBaseAddressModifyEnable = true;
344 sba.IndirectObjectBufferSizeModifyEnable = true;
345 sba.InstructionBuffersizeModifyEnable = true;
346
347 sba.InstructionBaseAddress = ro_bo(NULL, IRIS_MEMZONE_SHADER_START);
348 sba.SurfaceStateBaseAddress = ro_bo(NULL, IRIS_MEMZONE_SURFACE_START);
349 sba.DynamicStateBaseAddress = ro_bo(NULL, IRIS_MEMZONE_DYNAMIC_START);
350
351 sba.GeneralStateBufferSize = 0xfffff;
352 sba.IndirectObjectBufferSize = 0xfffff;
353 sba.InstructionBufferSize = 0xfffff;
354 sba.DynamicStateBufferSize = 0xfffff;
355 }
356
357 iris_emit_cmd(batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
358 rect.ClippedDrawingRectangleXMax = UINT16_MAX;
359 rect.ClippedDrawingRectangleYMax = UINT16_MAX;
360 }
361 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_PATTERN), pat) {
362 GEN_SAMPLE_POS_1X(pat._1xSample);
363 GEN_SAMPLE_POS_2X(pat._2xSample);
364 GEN_SAMPLE_POS_4X(pat._4xSample);
365 GEN_SAMPLE_POS_8X(pat._8xSample);
366 GEN_SAMPLE_POS_16X(pat._16xSample);
367 }
368 iris_emit_cmd(batch, GENX(3DSTATE_AA_LINE_PARAMETERS), foo);
369 iris_emit_cmd(batch, GENX(3DSTATE_WM_CHROMAKEY), foo);
370 iris_emit_cmd(batch, GENX(3DSTATE_WM_HZ_OP), foo);
371 /* XXX: may need to set an offset for origin-UL framebuffers */
372 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_OFFSET), foo);
373
374 /* Just assign a static partitioning. */
375 for (int i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
376 iris_emit_cmd(batch, GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
377 alloc._3DCommandSubOpcode = 18 + i;
378 alloc.ConstantBufferOffset = 6 * i;
379 alloc.ConstantBufferSize = i == MESA_SHADER_FRAGMENT ? 8 : 6;
380 }
381 }
382 }
383
384 static void
385 iris_launch_grid(struct pipe_context *ctx, const struct pipe_grid_info *info)
386 {
387 }
388
389 static void
390 iris_set_blend_color(struct pipe_context *ctx,
391 const struct pipe_blend_color *state)
392 {
393 struct iris_context *ice = (struct iris_context *) ctx;
394
395 memcpy(&ice->state.blend_color, state, sizeof(struct pipe_blend_color));
396 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
397 }
398
399 struct iris_blend_state {
400 uint32_t ps_blend[GENX(3DSTATE_PS_BLEND_length)];
401 uint32_t blend_state[GENX(BLEND_STATE_length) +
402 BRW_MAX_DRAW_BUFFERS * GENX(BLEND_STATE_ENTRY_length)];
403
404 bool alpha_to_coverage; /* for shader key */
405 };
406
407 static void *
408 iris_create_blend_state(struct pipe_context *ctx,
409 const struct pipe_blend_state *state)
410 {
411 struct iris_blend_state *cso = malloc(sizeof(struct iris_blend_state));
412 uint32_t *blend_state = cso->blend_state;
413
414 cso->alpha_to_coverage = state->alpha_to_coverage;
415
416 iris_pack_command(GENX(3DSTATE_PS_BLEND), cso->ps_blend, pb) {
417 /* pb.HasWriteableRT is filled in at draw time. */
418 /* pb.AlphaTestEnable is filled in at draw time. */
419 pb.AlphaToCoverageEnable = state->alpha_to_coverage;
420 pb.IndependentAlphaBlendEnable = state->independent_blend_enable;
421
422 pb.ColorBufferBlendEnable = state->rt[0].blend_enable;
423
424 pb.SourceBlendFactor = state->rt[0].rgb_src_factor;
425 pb.SourceAlphaBlendFactor = state->rt[0].alpha_func;
426 pb.DestinationBlendFactor = state->rt[0].rgb_dst_factor;
427 pb.DestinationAlphaBlendFactor = state->rt[0].alpha_dst_factor;
428 }
429
430 iris_pack_state(GENX(BLEND_STATE), blend_state, bs) {
431 bs.AlphaToCoverageEnable = state->alpha_to_coverage;
432 bs.IndependentAlphaBlendEnable = state->independent_blend_enable;
433 bs.AlphaToOneEnable = state->alpha_to_one;
434 bs.AlphaToCoverageDitherEnable = state->alpha_to_coverage;
435 bs.ColorDitherEnable = state->dither;
436 /* bl.AlphaTestEnable and bs.AlphaTestFunction are filled in later. */
437 }
438
439 blend_state += GENX(BLEND_STATE_length);
440
441 for (int i = 0; i < BRW_MAX_DRAW_BUFFERS; i++) {
442 iris_pack_state(GENX(BLEND_STATE_ENTRY), blend_state, be) {
443 be.LogicOpEnable = state->logicop_enable;
444 be.LogicOpFunction = state->logicop_func;
445
446 be.PreBlendSourceOnlyClampEnable = false;
447 be.ColorClampRange = COLORCLAMP_RTFORMAT;
448 be.PreBlendColorClampEnable = true;
449 be.PostBlendColorClampEnable = true;
450
451 be.ColorBufferBlendEnable = state->rt[i].blend_enable;
452
453 be.ColorBlendFunction = state->rt[i].rgb_func;
454 be.AlphaBlendFunction = state->rt[i].alpha_func;
455 be.SourceBlendFactor = state->rt[i].rgb_src_factor;
456 be.SourceAlphaBlendFactor = state->rt[i].alpha_func;
457 be.DestinationBlendFactor = state->rt[i].rgb_dst_factor;
458 be.DestinationAlphaBlendFactor = state->rt[i].alpha_dst_factor;
459
460 be.WriteDisableRed = !(state->rt[i].colormask & PIPE_MASK_R);
461 be.WriteDisableGreen = !(state->rt[i].colormask & PIPE_MASK_G);
462 be.WriteDisableBlue = !(state->rt[i].colormask & PIPE_MASK_B);
463 be.WriteDisableAlpha = !(state->rt[i].colormask & PIPE_MASK_A);
464 }
465 blend_state += GENX(BLEND_STATE_ENTRY_length);
466 }
467
468 return cso;
469 }
470
471 static void
472 iris_bind_blend_state(struct pipe_context *ctx, void *state)
473 {
474 struct iris_context *ice = (struct iris_context *) ctx;
475 ice->state.cso_blend = state;
476 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
477 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
478 }
479
480 struct iris_depth_stencil_alpha_state {
481 uint32_t wmds[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
482 uint32_t cc_vp[GENX(CC_VIEWPORT_length)];
483
484 struct pipe_alpha_state alpha; /* to BLEND_STATE, 3DSTATE_PS_BLEND */
485 };
486
487 static void *
488 iris_create_zsa_state(struct pipe_context *ctx,
489 const struct pipe_depth_stencil_alpha_state *state)
490 {
491 struct iris_depth_stencil_alpha_state *cso =
492 malloc(sizeof(struct iris_depth_stencil_alpha_state));
493
494 cso->alpha = state->alpha;
495
496 bool two_sided_stencil = state->stencil[1].enabled;
497
498 /* The state tracker needs to optimize away EQUAL writes for us. */
499 assert(!(state->depth.func == PIPE_FUNC_EQUAL && state->depth.writemask));
500
501 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), cso->wmds, wmds) {
502 wmds.StencilFailOp = state->stencil[0].fail_op;
503 wmds.StencilPassDepthFailOp = state->stencil[0].zfail_op;
504 wmds.StencilPassDepthPassOp = state->stencil[0].zpass_op;
505 wmds.StencilTestFunction =
506 translate_compare_func(state->stencil[0].func);
507 wmds.BackfaceStencilFailOp = state->stencil[1].fail_op;
508 wmds.BackfaceStencilPassDepthFailOp = state->stencil[1].zfail_op;
509 wmds.BackfaceStencilPassDepthPassOp = state->stencil[1].zpass_op;
510 wmds.BackfaceStencilTestFunction =
511 translate_compare_func(state->stencil[1].func);
512 wmds.DepthTestFunction = translate_compare_func(state->depth.func);
513 wmds.DoubleSidedStencilEnable = two_sided_stencil;
514 wmds.StencilTestEnable = state->stencil[0].enabled;
515 wmds.StencilBufferWriteEnable =
516 state->stencil[0].writemask != 0 ||
517 (two_sided_stencil && state->stencil[1].writemask != 0);
518 wmds.DepthTestEnable = state->depth.enabled;
519 wmds.DepthBufferWriteEnable = state->depth.writemask;
520 wmds.StencilTestMask = state->stencil[0].valuemask;
521 wmds.StencilWriteMask = state->stencil[0].writemask;
522 wmds.BackfaceStencilTestMask = state->stencil[1].valuemask;
523 wmds.BackfaceStencilWriteMask = state->stencil[1].writemask;
524 /* wmds.[Backface]StencilReferenceValue are merged later */
525 }
526
527 iris_pack_state(GENX(CC_VIEWPORT), cso->cc_vp, ccvp) {
528 ccvp.MinimumDepth = state->depth.bounds_min;
529 ccvp.MaximumDepth = state->depth.bounds_max;
530 }
531
532 return cso;
533 }
534
535 static void
536 iris_bind_zsa_state(struct pipe_context *ctx, void *state)
537 {
538 struct iris_context *ice = (struct iris_context *) ctx;
539 struct iris_depth_stencil_alpha_state *old_cso = ice->state.cso_zsa;
540 struct iris_depth_stencil_alpha_state *new_cso = state;
541
542 if (new_cso) {
543 if (!old_cso || old_cso->alpha.ref_value != new_cso->alpha.ref_value) {
544 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
545 }
546 }
547
548 ice->state.cso_zsa = new_cso;
549 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
550 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
551 }
552
553 struct iris_rasterizer_state {
554 uint32_t sf[GENX(3DSTATE_SF_length)];
555 uint32_t clip[GENX(3DSTATE_CLIP_length)];
556 uint32_t raster[GENX(3DSTATE_RASTER_length)];
557 uint32_t wm[GENX(3DSTATE_WM_length)];
558 uint32_t line_stipple[GENX(3DSTATE_LINE_STIPPLE_length)];
559
560 bool flatshade; /* for shader state */
561 bool clamp_fragment_color; /* for shader state */
562 bool light_twoside; /* for shader state */
563 bool rasterizer_discard; /* for 3DSTATE_STREAMOUT */
564 bool half_pixel_center; /* for 3DSTATE_MULTISAMPLE */
565 enum pipe_sprite_coord_mode sprite_coord_mode; /* PIPE_SPRITE_* */
566 uint16_t sprite_coord_enable;
567 };
568
569 static void *
570 iris_create_rasterizer_state(struct pipe_context *ctx,
571 const struct pipe_rasterizer_state *state)
572 {
573 struct iris_rasterizer_state *cso =
574 malloc(sizeof(struct iris_rasterizer_state));
575
576 #if 0
577 point_quad_rasterization -> SBE?
578
579 not necessary?
580 {
581 poly_smooth
582 force_persample_interp - ?
583 bottom_edge_rule
584
585 offset_units_unscaled - cap not exposed
586 }
587 #endif
588
589 cso->flatshade = state->flatshade;
590 cso->clamp_fragment_color = state->clamp_fragment_color;
591 cso->light_twoside = state->light_twoside;
592 cso->rasterizer_discard = state->rasterizer_discard;
593 cso->half_pixel_center = state->half_pixel_center;
594 cso->sprite_coord_mode = state->sprite_coord_mode;
595 cso->sprite_coord_enable = state->sprite_coord_enable;
596
597 iris_pack_command(GENX(3DSTATE_SF), cso->sf, sf) {
598 sf.StatisticsEnable = true;
599 sf.ViewportTransformEnable = true;
600 sf.AALineDistanceMode = AALINEDISTANCE_TRUE;
601 sf.LineEndCapAntialiasingRegionWidth =
602 state->line_smooth ? _10pixels : _05pixels;
603 sf.LastPixelEnable = state->line_last_pixel;
604 sf.LineWidth = state->line_width;
605 sf.SmoothPointEnable = state->point_smooth;
606 sf.PointWidthSource = state->point_size_per_vertex ? Vertex : State;
607 sf.PointWidth = state->point_size;
608
609 if (state->flatshade_first) {
610 sf.TriangleStripListProvokingVertexSelect = 2;
611 sf.TriangleFanProvokingVertexSelect = 2;
612 sf.LineStripListProvokingVertexSelect = 1;
613 } else {
614 sf.TriangleFanProvokingVertexSelect = 1;
615 }
616 }
617
618 /* COMPLETE! */
619 iris_pack_command(GENX(3DSTATE_RASTER), cso->raster, rr) {
620 rr.FrontWinding = state->front_ccw ? CounterClockwise : Clockwise;
621 rr.CullMode = translate_cull_mode(state->cull_face);
622 rr.FrontFaceFillMode = translate_fill_mode(state->fill_front);
623 rr.BackFaceFillMode = translate_fill_mode(state->fill_back);
624 rr.DXMultisampleRasterizationEnable = state->multisample;
625 rr.GlobalDepthOffsetEnableSolid = state->offset_tri;
626 rr.GlobalDepthOffsetEnableWireframe = state->offset_line;
627 rr.GlobalDepthOffsetEnablePoint = state->offset_point;
628 rr.GlobalDepthOffsetConstant = state->offset_units;
629 rr.GlobalDepthOffsetScale = state->offset_scale;
630 rr.GlobalDepthOffsetClamp = state->offset_clamp;
631 rr.SmoothPointEnable = state->point_smooth;
632 rr.AntialiasingEnable = state->line_smooth;
633 rr.ScissorRectangleEnable = state->scissor;
634 rr.ViewportZNearClipTestEnable = state->depth_clip_near;
635 rr.ViewportZFarClipTestEnable = state->depth_clip_far;
636 //rr.ConservativeRasterizationEnable = not yet supported by Gallium...
637 }
638
639 iris_pack_command(GENX(3DSTATE_CLIP), cso->clip, cl) {
640 /* cl.NonPerspectiveBarycentricEnable is filled in at draw time from
641 * the FS program; cl.ForceZeroRTAIndexEnable is filled in from the FB.
642 */
643 cl.StatisticsEnable = true;
644 cl.EarlyCullEnable = true;
645 cl.UserClipDistanceClipTestEnableBitmask = state->clip_plane_enable;
646 cl.ForceUserClipDistanceClipTestEnableBitmask = true;
647 cl.APIMode = state->clip_halfz ? APIMODE_D3D : APIMODE_OGL;
648 cl.GuardbandClipTestEnable = true;
649 cl.ClipMode = CLIPMODE_NORMAL;
650 cl.ClipEnable = true;
651 cl.ViewportXYClipTestEnable = state->point_tri_clip;
652 cl.MinimumPointWidth = 0.125;
653 cl.MaximumPointWidth = 255.875;
654
655 if (state->flatshade_first) {
656 cl.TriangleStripListProvokingVertexSelect = 2;
657 cl.TriangleFanProvokingVertexSelect = 2;
658 cl.LineStripListProvokingVertexSelect = 1;
659 } else {
660 cl.TriangleFanProvokingVertexSelect = 1;
661 }
662 }
663
664 iris_pack_command(GENX(3DSTATE_WM), cso->wm, wm) {
665 /* wm.BarycentricInterpolationMode and wm.EarlyDepthStencilControl are
666 * filled in at draw time from the FS program.
667 */
668 wm.LineAntialiasingRegionWidth = _10pixels;
669 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
670 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
671 wm.StatisticsEnable = true;
672 wm.LineStippleEnable = state->line_stipple_enable;
673 wm.PolygonStippleEnable = state->poly_stipple_enable;
674 }
675
676 /* Remap from 0..255 back to 1..256 */
677 const unsigned line_stipple_factor = state->line_stipple_factor + 1;
678
679 iris_pack_command(GENX(3DSTATE_LINE_STIPPLE), cso->line_stipple, line) {
680 line.LineStipplePattern = state->line_stipple_pattern;
681 line.LineStippleInverseRepeatCount = 1.0f / line_stipple_factor;
682 line.LineStippleRepeatCount = line_stipple_factor;
683 }
684
685 return cso;
686 }
687
688 static void
689 iris_bind_rasterizer_state(struct pipe_context *ctx, void *state)
690 {
691 struct iris_context *ice = (struct iris_context *) ctx;
692 struct iris_rasterizer_state *old_cso = ice->state.cso_rast;
693 struct iris_rasterizer_state *new_cso = state;
694
695 if (new_cso) {
696 /* Try to avoid re-emitting 3DSTATE_LINE_STIPPLE, it's non-pipelined */
697 if (!old_cso || memcmp(old_cso->line_stipple, new_cso->line_stipple,
698 sizeof(old_cso->line_stipple)) != 0) {
699 ice->state.dirty |= IRIS_DIRTY_LINE_STIPPLE;
700 }
701
702 if (!old_cso ||
703 old_cso->half_pixel_center != new_cso->half_pixel_center) {
704 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
705 }
706 }
707
708 ice->state.cso_rast = new_cso;
709 ice->state.dirty |= IRIS_DIRTY_RASTER;
710 }
711
712 static uint32_t
713 translate_wrap(unsigned pipe_wrap)
714 {
715 static const unsigned map[] = {
716 [PIPE_TEX_WRAP_REPEAT] = TCM_WRAP,
717 [PIPE_TEX_WRAP_CLAMP] = TCM_HALF_BORDER,
718 [PIPE_TEX_WRAP_CLAMP_TO_EDGE] = TCM_CLAMP,
719 [PIPE_TEX_WRAP_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
720 [PIPE_TEX_WRAP_MIRROR_REPEAT] = TCM_MIRROR,
721 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
722 [PIPE_TEX_WRAP_MIRROR_CLAMP] = -1, // XXX: ???
723 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER] = -1, // XXX: ???
724 };
725 return map[pipe_wrap];
726 }
727
728 /**
729 * Return true if the given wrap mode requires the border color to exist.
730 */
731 static bool
732 wrap_mode_needs_border_color(unsigned wrap_mode)
733 {
734 return wrap_mode == TCM_CLAMP_BORDER || wrap_mode == TCM_HALF_BORDER;
735 }
736
737 static unsigned
738 translate_mip_filter(enum pipe_tex_mipfilter pipe_mip)
739 {
740 static const unsigned map[] = {
741 [PIPE_TEX_MIPFILTER_NEAREST] = MIPFILTER_NEAREST,
742 [PIPE_TEX_MIPFILTER_LINEAR] = MIPFILTER_LINEAR,
743 [PIPE_TEX_MIPFILTER_NONE] = MIPFILTER_NONE,
744 };
745 return map[pipe_mip];
746 }
747
748 struct iris_sampler_state {
749 struct pipe_sampler_state base;
750
751 bool needs_border_color;
752
753 uint32_t sampler_state[GENX(SAMPLER_STATE_length)];
754 };
755
756 static void *
757 iris_create_sampler_state(struct pipe_context *pctx,
758 const struct pipe_sampler_state *state)
759 {
760 struct iris_sampler_state *cso = CALLOC_STRUCT(iris_sampler_state);
761
762 if (!cso)
763 return NULL;
764
765 STATIC_ASSERT(PIPE_TEX_FILTER_NEAREST == MAPFILTER_NEAREST);
766 STATIC_ASSERT(PIPE_TEX_FILTER_LINEAR == MAPFILTER_LINEAR);
767
768 unsigned wrap_s = translate_wrap(state->wrap_s);
769 unsigned wrap_t = translate_wrap(state->wrap_t);
770 unsigned wrap_r = translate_wrap(state->wrap_r);
771
772 cso->needs_border_color = wrap_mode_needs_border_color(wrap_s) ||
773 wrap_mode_needs_border_color(wrap_t) ||
774 wrap_mode_needs_border_color(wrap_r);
775
776 iris_pack_state(GENX(SAMPLER_STATE), cso->sampler_state, samp) {
777 samp.TCXAddressControlMode = wrap_s;
778 samp.TCYAddressControlMode = wrap_t;
779 samp.TCZAddressControlMode = wrap_r;
780 samp.CubeSurfaceControlMode = state->seamless_cube_map;
781 samp.NonnormalizedCoordinateEnable = !state->normalized_coords;
782 samp.MinModeFilter = state->min_img_filter;
783 samp.MagModeFilter = state->mag_img_filter;
784 samp.MipModeFilter = translate_mip_filter(state->min_mip_filter);
785 samp.MaximumAnisotropy = RATIO21;
786
787 if (state->max_anisotropy >= 2) {
788 if (state->min_img_filter == PIPE_TEX_FILTER_LINEAR) {
789 samp.MinModeFilter = MAPFILTER_ANISOTROPIC;
790 samp.AnisotropicAlgorithm = EWAApproximation;
791 }
792
793 if (state->mag_img_filter == PIPE_TEX_FILTER_LINEAR)
794 samp.MagModeFilter = MAPFILTER_ANISOTROPIC;
795
796 samp.MaximumAnisotropy =
797 MIN2((state->max_anisotropy - 2) / 2, RATIO161);
798 }
799
800 /* Set address rounding bits if not using nearest filtering. */
801 if (state->min_img_filter != PIPE_TEX_FILTER_NEAREST) {
802 samp.UAddressMinFilterRoundingEnable = true;
803 samp.VAddressMinFilterRoundingEnable = true;
804 samp.RAddressMinFilterRoundingEnable = true;
805 }
806
807 if (state->mag_img_filter != PIPE_TEX_FILTER_NEAREST) {
808 samp.UAddressMagFilterRoundingEnable = true;
809 samp.VAddressMagFilterRoundingEnable = true;
810 samp.RAddressMagFilterRoundingEnable = true;
811 }
812
813 if (state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
814 samp.ShadowFunction = translate_shadow_func(state->compare_func);
815
816 const float hw_max_lod = GEN_GEN >= 7 ? 14 : 13;
817
818 samp.LODPreClampMode = CLAMP_MODE_OGL;
819 samp.MinLOD = CLAMP(state->min_lod, 0, hw_max_lod);
820 samp.MaxLOD = CLAMP(state->max_lod, 0, hw_max_lod);
821 samp.TextureLODBias = CLAMP(state->lod_bias, -16, 15);
822
823 //samp.BorderColorPointer = <<comes from elsewhere>>
824 }
825
826 return cso;
827 }
828
829 static void
830 iris_bind_sampler_states(struct pipe_context *ctx,
831 enum pipe_shader_type p_stage,
832 unsigned start, unsigned count,
833 void **states)
834 {
835 struct iris_context *ice = (struct iris_context *) ctx;
836 gl_shader_stage stage = stage_from_pipe(p_stage);
837
838 assert(start + count <= IRIS_MAX_TEXTURE_SAMPLERS);
839
840 /* Assemble the SAMPLER_STATEs into a contiguous chunk of memory
841 * relative to Dynamic State Base Address.
842 */
843 void *map = NULL;
844 u_upload_alloc(ice->state.dynamic_uploader, 0,
845 count * 4 * GENX(SAMPLER_STATE_length), 32,
846 &ice->state.sampler_table_offset[stage],
847 &ice->state.sampler_table_resource[stage],
848 &map);
849 if (unlikely(!map))
850 return;
851
852 struct pipe_resource *res = ice->state.sampler_table_resource[stage];
853 ice->state.sampler_table_offset[stage] +=
854 iris_bo_offset_from_base_address(iris_resource_bo(res));
855
856 for (int i = 0; i < count; i++) {
857 struct iris_sampler_state *state = states[i];
858
859 /* Save a pointer to the iris_sampler_state, a few fields need
860 * to inform draw-time decisions.
861 */
862 ice->state.samplers[stage][start + i] = state;
863
864 if (state)
865 memcpy(map, state->sampler_state, 4 * GENX(SAMPLER_STATE_length));
866
867 map += GENX(SAMPLER_STATE_length);
868 }
869
870 ice->state.num_samplers[stage] = count;
871
872 ice->state.dirty |= IRIS_DIRTY_SAMPLER_STATES_VS << stage;
873 }
874
875 struct iris_sampler_view {
876 struct pipe_sampler_view pipe;
877 struct isl_view view;
878
879 /** The resource (BO) holding our SURFACE_STATE. */
880 struct pipe_resource *surface_state_resource;
881 unsigned surface_state_offset;
882
883 //uint32_t surface_state[GENX(RENDER_SURFACE_STATE_length)];
884 };
885
886 /**
887 * Convert an swizzle enumeration (i.e. PIPE_SWIZZLE_X) to one of the Gen7.5+
888 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
889 *
890 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
891 * 0 1 2 3 4 5
892 * 4 5 6 7 0 1
893 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
894 *
895 * which is simply adding 4 then modding by 8 (or anding with 7).
896 *
897 * We then may need to apply workarounds for textureGather hardware bugs.
898 */
899 static enum isl_channel_select
900 pipe_swizzle_to_isl_channel(enum pipe_swizzle swizzle)
901 {
902 return (swizzle + 4) & 7;
903 }
904
905 static struct pipe_sampler_view *
906 iris_create_sampler_view(struct pipe_context *ctx,
907 struct pipe_resource *tex,
908 const struct pipe_sampler_view *tmpl)
909 {
910 struct iris_context *ice = (struct iris_context *) ctx;
911 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
912 struct iris_resource *itex = (struct iris_resource *) tex;
913 struct iris_sampler_view *isv = calloc(1, sizeof(struct iris_sampler_view));
914
915 if (!isv)
916 return NULL;
917
918 /* initialize base object */
919 isv->pipe = *tmpl;
920 isv->pipe.context = ctx;
921 isv->pipe.texture = NULL;
922 pipe_reference_init(&isv->pipe.reference, 1);
923 pipe_resource_reference(&isv->pipe.texture, tex);
924
925 /* XXX: do we need brw_get_texture_swizzle hacks here? */
926
927 isv->view = (struct isl_view) {
928 .format = iris_isl_format_for_pipe_format(tmpl->format),
929 .base_level = tmpl->u.tex.first_level,
930 .levels = tmpl->u.tex.last_level - tmpl->u.tex.first_level + 1,
931 .base_array_layer = tmpl->u.tex.first_layer,
932 .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
933 .swizzle = (struct isl_swizzle) {
934 .r = pipe_swizzle_to_isl_channel(tmpl->swizzle_r),
935 .g = pipe_swizzle_to_isl_channel(tmpl->swizzle_g),
936 .b = pipe_swizzle_to_isl_channel(tmpl->swizzle_b),
937 .a = pipe_swizzle_to_isl_channel(tmpl->swizzle_a),
938 },
939 .usage = ISL_SURF_USAGE_TEXTURE_BIT,
940 };
941
942 void *map = NULL;
943 u_upload_alloc(ice->state.surface_uploader, 0,
944 4 * GENX(RENDER_SURFACE_STATE_length), 64,
945 &isv->surface_state_offset,
946 &isv->surface_state_resource,
947 &map);
948 if (!unlikely(map))
949 return NULL;
950
951 struct iris_bo *state_bo = iris_resource_bo(isv->surface_state_resource);
952 isv->surface_state_offset += iris_bo_offset_from_base_address(state_bo);
953
954 isl_surf_fill_state(&screen->isl_dev, map,
955 .surf = &itex->surf, .view = &isv->view,
956 .mocs = MOCS_WB,
957 .address = itex->bo->gtt_offset);
958 // .aux_surf =
959 // .clear_color = clear_color,
960
961 return &isv->pipe;
962 }
963
964 struct iris_surface {
965 struct pipe_surface pipe;
966 struct isl_view view;
967
968 /** The resource (BO) holding our SURFACE_STATE. */
969 struct pipe_resource *surface_state_resource;
970 unsigned surface_state_offset;
971
972 // uint32_t surface_state[GENX(RENDER_SURFACE_STATE_length)];
973 };
974
975 static struct pipe_surface *
976 iris_create_surface(struct pipe_context *ctx,
977 struct pipe_resource *tex,
978 const struct pipe_surface *tmpl)
979 {
980 struct iris_context *ice = (struct iris_context *) ctx;
981 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
982 struct iris_surface *surf = calloc(1, sizeof(struct iris_surface));
983 struct pipe_surface *psurf = &surf->pipe;
984 struct iris_resource *res = (struct iris_resource *) tex;
985
986 if (!surf)
987 return NULL;
988
989 pipe_reference_init(&psurf->reference, 1);
990 pipe_resource_reference(&psurf->texture, tex);
991 psurf->context = ctx;
992 psurf->format = tmpl->format;
993 psurf->width = tex->width0;
994 psurf->height = tex->height0;
995 psurf->texture = tex;
996 psurf->u.tex.first_layer = tmpl->u.tex.first_layer;
997 psurf->u.tex.last_layer = tmpl->u.tex.last_layer;
998 psurf->u.tex.level = tmpl->u.tex.level;
999
1000 unsigned usage = 0;
1001 if (tmpl->writable)
1002 usage = ISL_SURF_USAGE_STORAGE_BIT;
1003 else if (util_format_is_depth_or_stencil(tmpl->format))
1004 usage = ISL_SURF_USAGE_DEPTH_BIT;
1005 else
1006 usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
1007
1008 surf->view = (struct isl_view) {
1009 .format = iris_isl_format_for_pipe_format(tmpl->format),
1010 .base_level = tmpl->u.tex.level,
1011 .levels = 1,
1012 .base_array_layer = tmpl->u.tex.first_layer,
1013 .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
1014 .swizzle = ISL_SWIZZLE_IDENTITY,
1015 .usage = usage,
1016 };
1017
1018 /* Bail early for depth/stencil */
1019 if (res->surf.usage & (ISL_SURF_USAGE_DEPTH_BIT |
1020 ISL_SURF_USAGE_STENCIL_BIT))
1021 return psurf;
1022
1023 void *map = NULL;
1024 u_upload_alloc(ice->state.surface_uploader, 0,
1025 4 * GENX(RENDER_SURFACE_STATE_length), 64,
1026 &surf->surface_state_offset,
1027 &surf->surface_state_resource,
1028 &map);
1029 if (!unlikely(map))
1030 return NULL;
1031
1032 struct iris_bo *state_bo = iris_resource_bo(surf->surface_state_resource);
1033 surf->surface_state_offset += iris_bo_offset_from_base_address(state_bo);
1034
1035 isl_surf_fill_state(&screen->isl_dev, map,
1036 .surf = &res->surf, .view = &surf->view,
1037 .mocs = MOCS_WB,
1038 .address = res->bo->gtt_offset);
1039 // .aux_surf =
1040 // .clear_color = clear_color,
1041
1042 return psurf;
1043 }
1044
1045 static void
1046 iris_set_sampler_views(struct pipe_context *ctx,
1047 enum pipe_shader_type p_stage,
1048 unsigned start, unsigned count,
1049 struct pipe_sampler_view **views)
1050 {
1051 struct iris_context *ice = (struct iris_context *) ctx;
1052 gl_shader_stage stage = stage_from_pipe(p_stage);
1053
1054 unsigned i;
1055 for (i = 0; i < count; i++) {
1056 pipe_sampler_view_reference((struct pipe_sampler_view **)
1057 &ice->state.textures[stage][i], views[i]);
1058 }
1059 for (; i < ice->state.num_textures[stage]; i++) {
1060 pipe_sampler_view_reference((struct pipe_sampler_view **)
1061 &ice->state.textures[stage][i], NULL);
1062 }
1063
1064 ice->state.num_textures[stage] = count;
1065
1066 // XXX: ice->state.dirty |= (IRIS_DIRTY_BINDING_TABLE_VS << stage);
1067 }
1068
1069 static void
1070 iris_set_clip_state(struct pipe_context *ctx,
1071 const struct pipe_clip_state *state)
1072 {
1073 }
1074
1075 static void
1076 iris_set_polygon_stipple(struct pipe_context *ctx,
1077 const struct pipe_poly_stipple *state)
1078 {
1079 struct iris_context *ice = (struct iris_context *) ctx;
1080 memcpy(&ice->state.poly_stipple, state, sizeof(*state));
1081 ice->state.dirty |= IRIS_DIRTY_POLYGON_STIPPLE;
1082 }
1083
1084 static void
1085 iris_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
1086 {
1087 struct iris_context *ice = (struct iris_context *) ctx;
1088
1089 ice->state.sample_mask = sample_mask;
1090 ice->state.dirty |= IRIS_DIRTY_SAMPLE_MASK;
1091 }
1092
1093 static void
1094 iris_set_scissor_states(struct pipe_context *ctx,
1095 unsigned start_slot,
1096 unsigned num_scissors,
1097 const struct pipe_scissor_state *states)
1098 {
1099 struct iris_context *ice = (struct iris_context *) ctx;
1100
1101 ice->state.num_scissors = num_scissors;
1102
1103 for (unsigned i = 0; i < num_scissors; i++) {
1104 ice->state.scissors[start_slot + i] = states[i];
1105 }
1106
1107 ice->state.dirty |= IRIS_DIRTY_SCISSOR_RECT;
1108 }
1109
1110 static void
1111 iris_set_stencil_ref(struct pipe_context *ctx,
1112 const struct pipe_stencil_ref *state)
1113 {
1114 struct iris_context *ice = (struct iris_context *) ctx;
1115 memcpy(&ice->state.stencil_ref, state, sizeof(*state));
1116 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
1117 }
1118
1119
1120 struct iris_viewport_state {
1121 uint32_t sf_cl_vp[GENX(SF_CLIP_VIEWPORT_length) * IRIS_MAX_VIEWPORTS];
1122 };
1123
1124 static float
1125 viewport_extent(const struct pipe_viewport_state *state, int axis, float sign)
1126 {
1127 return copysignf(state->scale[axis], sign) + state->translate[axis];
1128 }
1129
1130 #if 0
1131 static void
1132 calculate_guardband_size(uint32_t fb_width, uint32_t fb_height,
1133 float m00, float m11, float m30, float m31,
1134 float *xmin, float *xmax,
1135 float *ymin, float *ymax)
1136 {
1137 /* According to the "Vertex X,Y Clamping and Quantization" section of the
1138 * Strips and Fans documentation:
1139 *
1140 * "The vertex X and Y screen-space coordinates are also /clamped/ to the
1141 * fixed-point "guardband" range supported by the rasterization hardware"
1142 *
1143 * and
1144 *
1145 * "In almost all circumstances, if an object’s vertices are actually
1146 * modified by this clamping (i.e., had X or Y coordinates outside of
1147 * the guardband extent the rendered object will not match the intended
1148 * result. Therefore software should take steps to ensure that this does
1149 * not happen - e.g., by clipping objects such that they do not exceed
1150 * these limits after the Drawing Rectangle is applied."
1151 *
1152 * I believe the fundamental restriction is that the rasterizer (in
1153 * the SF/WM stages) have a limit on the number of pixels that can be
1154 * rasterized. We need to ensure any coordinates beyond the rasterizer
1155 * limit are handled by the clipper. So effectively that limit becomes
1156 * the clipper's guardband size.
1157 *
1158 * It goes on to say:
1159 *
1160 * "In addition, in order to be correctly rendered, objects must have a
1161 * screenspace bounding box not exceeding 8K in the X or Y direction.
1162 * This additional restriction must also be comprehended by software,
1163 * i.e., enforced by use of clipping."
1164 *
1165 * This makes no sense. Gen7+ hardware supports 16K render targets,
1166 * and you definitely need to be able to draw polygons that fill the
1167 * surface. Our assumption is that the rasterizer was limited to 8K
1168 * on Sandybridge, which only supports 8K surfaces, and it was actually
1169 * increased to 16K on Ivybridge and later.
1170 *
1171 * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
1172 */
1173 const float gb_size = GEN_GEN >= 7 ? 16384.0f : 8192.0f;
1174
1175 if (m00 != 0 && m11 != 0) {
1176 /* First, we compute the screen-space render area */
1177 const float ss_ra_xmin = MIN3( 0, m30 + m00, m30 - m00);
1178 const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
1179 const float ss_ra_ymin = MIN3( 0, m31 + m11, m31 - m11);
1180 const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
1181
1182 /* We want the guardband to be centered on that */
1183 const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
1184 const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
1185 const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
1186 const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
1187
1188 /* Now we need it in native device coordinates */
1189 const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
1190 const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
1191 const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
1192 const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
1193
1194 /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
1195 * flipped upside-down. X should be fine though.
1196 */
1197 assert(ndc_gb_xmin <= ndc_gb_xmax);
1198 *xmin = ndc_gb_xmin;
1199 *xmax = ndc_gb_xmax;
1200 *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
1201 *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
1202 } else {
1203 /* The viewport scales to 0, so nothing will be rendered. */
1204 *xmin = 0.0f;
1205 *xmax = 0.0f;
1206 *ymin = 0.0f;
1207 *ymax = 0.0f;
1208 }
1209 }
1210 #endif
1211
1212 static void
1213 iris_set_viewport_states(struct pipe_context *ctx,
1214 unsigned start_slot,
1215 unsigned num_viewports,
1216 const struct pipe_viewport_state *state)
1217 {
1218 struct iris_context *ice = (struct iris_context *) ctx;
1219 struct iris_viewport_state *cso =
1220 malloc(sizeof(struct iris_viewport_state));
1221 uint32_t *vp_map = &cso->sf_cl_vp[start_slot];
1222
1223 // XXX: sf_cl_vp is only big enough for one slot, we don't iterate right
1224 for (unsigned i = 0; i < num_viewports; i++) {
1225 iris_pack_state(GENX(SF_CLIP_VIEWPORT), vp_map, vp) {
1226 vp.ViewportMatrixElementm00 = state[i].scale[0];
1227 vp.ViewportMatrixElementm11 = state[i].scale[1];
1228 vp.ViewportMatrixElementm22 = state[i].scale[2];
1229 vp.ViewportMatrixElementm30 = state[i].translate[0];
1230 vp.ViewportMatrixElementm31 = state[i].translate[1];
1231 vp.ViewportMatrixElementm32 = state[i].translate[2];
1232 /* XXX: in i965 this is computed based on the drawbuffer size,
1233 * but we don't have that here...
1234 */
1235 vp.XMinClipGuardband = -1.0;
1236 vp.XMaxClipGuardband = 1.0;
1237 vp.YMinClipGuardband = -1.0;
1238 vp.YMaxClipGuardband = 1.0;
1239 vp.XMinViewPort = viewport_extent(&state[i], 0, -1.0f);
1240 vp.XMaxViewPort = viewport_extent(&state[i], 0, 1.0f) - 1;
1241 vp.YMinViewPort = viewport_extent(&state[i], 1, -1.0f);
1242 vp.YMaxViewPort = viewport_extent(&state[i], 1, 1.0f) - 1;
1243 }
1244
1245 vp_map += GENX(SF_CLIP_VIEWPORT_length);
1246 }
1247
1248 ice->state.cso_vp = cso;
1249 ice->state.num_viewports = num_viewports;
1250 ice->state.dirty |= IRIS_DIRTY_SF_CL_VIEWPORT;
1251 }
1252
1253 struct iris_depth_state
1254 {
1255 uint32_t depth_buffer[GENX(3DSTATE_DEPTH_BUFFER_length)];
1256 uint32_t hier_depth_buffer[GENX(3DSTATE_HIER_DEPTH_BUFFER_length)];
1257 uint32_t stencil_buffer[GENX(3DSTATE_STENCIL_BUFFER_length)];
1258 };
1259
1260 static void
1261 iris_set_framebuffer_state(struct pipe_context *ctx,
1262 const struct pipe_framebuffer_state *state)
1263 {
1264 struct iris_context *ice = (struct iris_context *) ctx;
1265 struct pipe_framebuffer_state *cso = &ice->state.framebuffer;
1266
1267 if (cso->samples != state->samples) {
1268 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
1269 }
1270
1271 if (cso->nr_cbufs != state->nr_cbufs) {
1272 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
1273 }
1274
1275 cso->width = state->width;
1276 cso->height = state->height;
1277 cso->layers = state->layers;
1278 cso->samples = state->samples;
1279
1280 unsigned i;
1281 for (i = 0; i < state->nr_cbufs; i++)
1282 pipe_surface_reference(&cso->cbufs[i], state->cbufs[i]);
1283 for (; i < cso->nr_cbufs; i++)
1284 pipe_surface_reference(&cso->cbufs[i], NULL);
1285
1286 cso->nr_cbufs = state->nr_cbufs;
1287
1288 pipe_surface_reference(&cso->zsbuf, state->zsbuf);
1289
1290 //struct isl_depth_stencil_hiz_emit_info info = {
1291 //.mocs = MOCS_WB,
1292 //};
1293
1294 // XXX: depth buffers
1295 }
1296
1297 static void
1298 iris_set_constant_buffer(struct pipe_context *ctx,
1299 enum pipe_shader_type p_stage, unsigned index,
1300 const struct pipe_constant_buffer *cb)
1301 {
1302 struct iris_context *ice = (struct iris_context *) ctx;
1303 gl_shader_stage stage = stage_from_pipe(p_stage);
1304
1305 util_copy_constant_buffer(&ice->shaders.state[stage].constbuf[index], cb);
1306 }
1307
1308 static void
1309 iris_sampler_view_destroy(struct pipe_context *ctx,
1310 struct pipe_sampler_view *state)
1311 {
1312 struct iris_sampler_view *isv = (void *) state;
1313 pipe_resource_reference(&state->texture, NULL);
1314 pipe_resource_reference(&isv->surface_state_resource, NULL);
1315 free(isv);
1316 }
1317
1318
1319 static void
1320 iris_surface_destroy(struct pipe_context *ctx, struct pipe_surface *p_surf)
1321 {
1322 struct iris_surface *surf = (void *) p_surf;
1323 pipe_resource_reference(&p_surf->texture, NULL);
1324 pipe_resource_reference(&surf->surface_state_resource, NULL);
1325 free(surf);
1326 }
1327
1328 static void
1329 iris_delete_state(struct pipe_context *ctx, void *state)
1330 {
1331 free(state);
1332 }
1333
1334 struct iris_vertex_buffer_state {
1335 uint32_t vertex_buffers[1 + 33 * GENX(VERTEX_BUFFER_STATE_length)];
1336 struct iris_bo *bos[33];
1337 unsigned num_buffers;
1338 };
1339
1340 static void
1341 iris_free_vertex_buffers(struct iris_vertex_buffer_state *cso)
1342 {
1343 if (cso) {
1344 for (unsigned i = 0; i < cso->num_buffers; i++)
1345 iris_bo_unreference(cso->bos[i]);
1346 free(cso);
1347 }
1348 }
1349
1350 static void
1351 iris_set_vertex_buffers(struct pipe_context *ctx,
1352 unsigned start_slot, unsigned count,
1353 const struct pipe_vertex_buffer *buffers)
1354 {
1355 struct iris_context *ice = (struct iris_context *) ctx;
1356 struct iris_vertex_buffer_state *cso =
1357 malloc(sizeof(struct iris_vertex_buffer_state));
1358
1359 /* If there are no buffers, do nothing. We can leave the stale
1360 * 3DSTATE_VERTEX_BUFFERS in place - as long as there are no vertex
1361 * elements that point to them, it should be fine.
1362 */
1363 if (!buffers)
1364 return;
1365
1366 iris_free_vertex_buffers(ice->state.cso_vertex_buffers);
1367
1368 cso->num_buffers = count;
1369
1370 iris_pack_command(GENX(3DSTATE_VERTEX_BUFFERS), cso->vertex_buffers, vb) {
1371 vb.DWordLength = 4 * cso->num_buffers - 1;
1372 }
1373
1374 uint32_t *vb_pack_dest = &cso->vertex_buffers[1];
1375
1376 for (unsigned i = 0; i < count; i++) {
1377 assert(!buffers[i].is_user_buffer);
1378
1379 struct iris_resource *res = (void *) buffers[i].buffer.resource;
1380 iris_bo_reference(res->bo);
1381 cso->bos[i] = res->bo;
1382
1383 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
1384 vb.VertexBufferIndex = start_slot + i;
1385 vb.MOCS = MOCS_WB;
1386 vb.AddressModifyEnable = true;
1387 vb.BufferPitch = buffers[i].stride;
1388 vb.BufferSize = res->bo->size;
1389 vb.BufferStartingAddress =
1390 ro_bo(NULL, res->bo->gtt_offset + buffers[i].buffer_offset);
1391 }
1392
1393 vb_pack_dest += GENX(VERTEX_BUFFER_STATE_length);
1394 }
1395
1396 ice->state.cso_vertex_buffers = cso;
1397 ice->state.dirty |= IRIS_DIRTY_VERTEX_BUFFERS;
1398 }
1399
1400 struct iris_vertex_element_state {
1401 uint32_t vertex_elements[1 + 33 * GENX(VERTEX_ELEMENT_STATE_length)];
1402 uint32_t vf_instancing[GENX(3DSTATE_VF_INSTANCING_length)][33];
1403 unsigned count;
1404 };
1405
1406 static void *
1407 iris_create_vertex_elements(struct pipe_context *ctx,
1408 unsigned count,
1409 const struct pipe_vertex_element *state)
1410 {
1411 struct iris_vertex_element_state *cso =
1412 malloc(sizeof(struct iris_vertex_element_state));
1413
1414 cso->count = count;
1415
1416 /* TODO:
1417 * - create edge flag one
1418 * - create SGV ones
1419 * - if those are necessary, use count + 1/2/3... OR in the length
1420 */
1421 iris_pack_command(GENX(3DSTATE_VERTEX_ELEMENTS), cso->vertex_elements, ve) {
1422 ve.DWordLength = 1 + GENX(VERTEX_ELEMENT_STATE_length) * count;
1423 }
1424
1425 uint32_t *ve_pack_dest = &cso->vertex_elements[1];
1426
1427 for (int i = 0; i < count; i++) {
1428 enum isl_format isl_format =
1429 iris_isl_format_for_pipe_format(state[i].src_format);
1430 unsigned comp[4] = { VFCOMP_STORE_SRC, VFCOMP_STORE_SRC,
1431 VFCOMP_STORE_SRC, VFCOMP_STORE_SRC };
1432
1433 switch (isl_format_get_num_channels(isl_format)) {
1434 case 0: comp[0] = VFCOMP_STORE_0;
1435 case 1: comp[1] = VFCOMP_STORE_0;
1436 case 2: comp[2] = VFCOMP_STORE_0;
1437 case 3:
1438 comp[3] = isl_format_has_int_channel(isl_format) ? VFCOMP_STORE_1_INT
1439 : VFCOMP_STORE_1_FP;
1440 break;
1441 }
1442 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
1443 ve.VertexBufferIndex = state[i].vertex_buffer_index;
1444 ve.Valid = true;
1445 ve.SourceElementOffset = state[i].src_offset;
1446 ve.SourceElementFormat = isl_format;
1447 ve.Component0Control = comp[0];
1448 ve.Component1Control = comp[1];
1449 ve.Component2Control = comp[2];
1450 ve.Component3Control = comp[3];
1451 }
1452
1453 iris_pack_command(GENX(3DSTATE_VF_INSTANCING),
1454 cso->vf_instancing[i], vi) {
1455 vi.VertexElementIndex = i;
1456 vi.InstancingEnable = state[i].instance_divisor > 0;
1457 vi.InstanceDataStepRate = state[i].instance_divisor;
1458 }
1459
1460 ve_pack_dest += GENX(VERTEX_ELEMENT_STATE_length);
1461 }
1462
1463 return cso;
1464 }
1465
1466 static void
1467 iris_bind_vertex_elements_state(struct pipe_context *ctx, void *state)
1468 {
1469 struct iris_context *ice = (struct iris_context *) ctx;
1470
1471 ice->state.cso_vertex_elements = state;
1472 ice->state.dirty |= IRIS_DIRTY_VERTEX_ELEMENTS;
1473 }
1474
1475 static void *
1476 iris_create_compute_state(struct pipe_context *ctx,
1477 const struct pipe_compute_state *state)
1478 {
1479 return malloc(1);
1480 }
1481
1482 static struct pipe_stream_output_target *
1483 iris_create_stream_output_target(struct pipe_context *ctx,
1484 struct pipe_resource *res,
1485 unsigned buffer_offset,
1486 unsigned buffer_size)
1487 {
1488 struct pipe_stream_output_target *t =
1489 CALLOC_STRUCT(pipe_stream_output_target);
1490 if (!t)
1491 return NULL;
1492
1493 pipe_reference_init(&t->reference, 1);
1494 pipe_resource_reference(&t->buffer, res);
1495 t->buffer_offset = buffer_offset;
1496 t->buffer_size = buffer_size;
1497 return t;
1498 }
1499
1500 static void
1501 iris_stream_output_target_destroy(struct pipe_context *ctx,
1502 struct pipe_stream_output_target *t)
1503 {
1504 pipe_resource_reference(&t->buffer, NULL);
1505 free(t);
1506 }
1507
1508 static void
1509 iris_set_stream_output_targets(struct pipe_context *ctx,
1510 unsigned num_targets,
1511 struct pipe_stream_output_target **targets,
1512 const unsigned *offsets)
1513 {
1514 }
1515
1516 static void
1517 iris_compute_sbe_urb_read_interval(uint64_t fs_input_slots,
1518 const struct brw_vue_map *last_vue_map,
1519 bool two_sided_color,
1520 unsigned *out_offset,
1521 unsigned *out_length)
1522 {
1523 /* The compiler computes the first URB slot without considering COL/BFC
1524 * swizzling (because it doesn't know whether it's enabled), so we need
1525 * to do that here too. This may result in a smaller offset, which
1526 * should be safe.
1527 */
1528 const unsigned first_slot =
1529 brw_compute_first_urb_slot_required(fs_input_slots, last_vue_map);
1530
1531 /* This becomes the URB read offset (counted in pairs of slots). */
1532 assert(first_slot % 2 == 0);
1533 *out_offset = first_slot / 2;
1534
1535 /* We need to adjust the inputs read to account for front/back color
1536 * swizzling, as it can make the URB length longer.
1537 */
1538 for (int c = 0; c <= 1; c++) {
1539 if (fs_input_slots & (VARYING_BIT_COL0 << c)) {
1540 /* If two sided color is enabled, the fragment shader's gl_Color
1541 * (COL0) input comes from either the gl_FrontColor (COL0) or
1542 * gl_BackColor (BFC0) input varyings. Mark BFC as used, too.
1543 */
1544 if (two_sided_color)
1545 fs_input_slots |= (VARYING_BIT_BFC0 << c);
1546
1547 /* If front color isn't written, we opt to give them back color
1548 * instead of an undefined value. Switch from COL to BFC.
1549 */
1550 if (last_vue_map->varying_to_slot[VARYING_SLOT_COL0 + c] == -1) {
1551 fs_input_slots &= ~(VARYING_BIT_COL0 << c);
1552 fs_input_slots |= (VARYING_BIT_BFC0 << c);
1553 }
1554 }
1555 }
1556
1557 /* Compute the minimum URB Read Length necessary for the FS inputs.
1558 *
1559 * From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
1560 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
1561 *
1562 * "This field should be set to the minimum length required to read the
1563 * maximum source attribute. The maximum source attribute is indicated
1564 * by the maximum value of the enabled Attribute # Source Attribute if
1565 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
1566 * enable is not set.
1567 * read_length = ceiling((max_source_attr + 1) / 2)
1568 *
1569 * [errata] Corruption/Hang possible if length programmed larger than
1570 * recommended"
1571 *
1572 * Similar text exists for Ivy Bridge.
1573 *
1574 * We find the last URB slot that's actually read by the FS.
1575 */
1576 unsigned last_read_slot = last_vue_map->num_slots - 1;
1577 while (last_read_slot > first_slot && !(fs_input_slots &
1578 (1ull << last_vue_map->slot_to_varying[last_read_slot])))
1579 --last_read_slot;
1580
1581 /* The URB read length is the difference of the two, counted in pairs. */
1582 *out_length = DIV_ROUND_UP(last_read_slot - first_slot + 1, 2);
1583 }
1584
1585 static void
1586 iris_emit_sbe(struct iris_batch *batch, const struct iris_context *ice)
1587 {
1588 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
1589 const struct brw_wm_prog_data *wm_prog_data = (void *)
1590 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
1591 struct pipe_shader_state *p_fs =
1592 (void *) ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
1593 assert(p_fs->type == PIPE_SHADER_IR_NIR);
1594 nir_shader *fs_nir = p_fs->ir.nir;
1595
1596 unsigned urb_read_offset, urb_read_length;
1597 iris_compute_sbe_urb_read_interval(fs_nir->info.inputs_read,
1598 ice->shaders.last_vue_map,
1599 cso_rast->light_twoside,
1600 &urb_read_offset, &urb_read_length);
1601
1602 iris_emit_cmd(batch, GENX(3DSTATE_SBE), sbe) {
1603 sbe.AttributeSwizzleEnable = true;
1604 sbe.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
1605 sbe.PointSpriteTextureCoordinateOrigin = cso_rast->sprite_coord_mode;
1606 sbe.VertexURBEntryReadOffset = urb_read_offset;
1607 sbe.VertexURBEntryReadLength = urb_read_length;
1608 sbe.ForceVertexURBEntryReadOffset = true;
1609 sbe.ForceVertexURBEntryReadLength = true;
1610 sbe.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
1611
1612 for (int i = 0; i < 32; i++) {
1613 sbe.AttributeActiveComponentFormat[i] = ACTIVE_COMPONENT_XYZW;
1614 }
1615 }
1616 }
1617
1618 static void
1619 iris_bind_compute_state(struct pipe_context *ctx, void *state)
1620 {
1621 }
1622
1623 static void
1624 iris_populate_sampler_key(const struct iris_context *ice,
1625 struct brw_sampler_prog_key_data *key)
1626 {
1627 for (int i = 0; i < MAX_SAMPLERS; i++) {
1628 key->swizzles[i] = 0x688; /* XYZW */
1629 }
1630 }
1631
1632 static void
1633 iris_populate_vs_key(const struct iris_context *ice,
1634 struct brw_vs_prog_key *key)
1635 {
1636 memset(key, 0, sizeof(*key));
1637 iris_populate_sampler_key(ice, &key->tex);
1638 }
1639
1640 static void
1641 iris_populate_tcs_key(const struct iris_context *ice,
1642 struct brw_tcs_prog_key *key)
1643 {
1644 memset(key, 0, sizeof(*key));
1645 iris_populate_sampler_key(ice, &key->tex);
1646 }
1647
1648 static void
1649 iris_populate_tes_key(const struct iris_context *ice,
1650 struct brw_tes_prog_key *key)
1651 {
1652 memset(key, 0, sizeof(*key));
1653 iris_populate_sampler_key(ice, &key->tex);
1654 }
1655
1656 static void
1657 iris_populate_gs_key(const struct iris_context *ice,
1658 struct brw_gs_prog_key *key)
1659 {
1660 memset(key, 0, sizeof(*key));
1661 iris_populate_sampler_key(ice, &key->tex);
1662 }
1663
1664 static void
1665 iris_populate_fs_key(const struct iris_context *ice,
1666 struct brw_wm_prog_key *key)
1667 {
1668 memset(key, 0, sizeof(*key));
1669 iris_populate_sampler_key(ice, &key->tex);
1670
1671 /* XXX: dirty flags? */
1672 const struct pipe_framebuffer_state *fb = &ice->state.framebuffer;
1673 const struct iris_depth_stencil_alpha_state *zsa = ice->state.cso_zsa;
1674 const struct iris_rasterizer_state *rast = ice->state.cso_rast;
1675 const struct iris_blend_state *blend = ice->state.cso_blend;
1676
1677 key->nr_color_regions = fb->nr_cbufs;
1678
1679 key->clamp_fragment_color = rast->clamp_fragment_color;
1680
1681 key->replicate_alpha = fb->nr_cbufs > 1 &&
1682 (zsa->alpha.enabled || blend->alpha_to_coverage);
1683
1684 // key->force_dual_color_blend for unigine
1685 #if 0
1686 if (cso_rast->multisample) {
1687 key->persample_interp =
1688 ctx->Multisample.SampleShading &&
1689 (ctx->Multisample.MinSampleShadingValue *
1690 _mesa_geometric_samples(ctx->DrawBuffer) > 1);
1691
1692 key->multisample_fbo = fb->samples > 1;
1693 }
1694 #endif
1695
1696 key->coherent_fb_fetch = true;
1697 }
1698
1699 //pkt.SamplerCount = \
1700 //DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4); \
1701 //pkt.PerThreadScratchSpace = prog_data->total_scratch == 0 ? 0 : \
1702 //ffs(stage_state->per_thread_scratch) - 11; \
1703
1704 static uint64_t
1705 KSP(const struct iris_compiled_shader *shader)
1706 {
1707 struct iris_resource *res = (void *) shader->buffer;
1708 return res->bo->gtt_offset + shader->offset;
1709 }
1710
1711 #define INIT_THREAD_DISPATCH_FIELDS(pkt, prefix) \
1712 pkt.KernelStartPointer = KSP(shader); \
1713 pkt.BindingTableEntryCount = prog_data->binding_table.size_bytes / 4; \
1714 pkt.FloatingPointMode = prog_data->use_alt_mode; \
1715 \
1716 pkt.DispatchGRFStartRegisterForURBData = \
1717 prog_data->dispatch_grf_start_reg; \
1718 pkt.prefix##URBEntryReadLength = vue_prog_data->urb_read_length; \
1719 pkt.prefix##URBEntryReadOffset = 0; \
1720 \
1721 pkt.StatisticsEnable = true; \
1722 pkt.Enable = true;
1723
1724 static void
1725 iris_set_vs_state(const struct gen_device_info *devinfo,
1726 struct iris_compiled_shader *shader)
1727 {
1728 struct brw_stage_prog_data *prog_data = shader->prog_data;
1729 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1730
1731 iris_pack_command(GENX(3DSTATE_VS), shader->derived_data, vs) {
1732 INIT_THREAD_DISPATCH_FIELDS(vs, Vertex);
1733 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
1734 vs.SIMD8DispatchEnable = true;
1735 vs.UserClipDistanceCullTestEnableBitmask =
1736 vue_prog_data->cull_distance_mask;
1737 }
1738 }
1739
1740 static void
1741 iris_set_tcs_state(const struct gen_device_info *devinfo,
1742 struct iris_compiled_shader *shader)
1743 {
1744 struct brw_stage_prog_data *prog_data = shader->prog_data;
1745 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1746 struct brw_tcs_prog_data *tcs_prog_data = (void *) prog_data;
1747
1748 iris_pack_command(GENX(3DSTATE_HS), shader->derived_data, hs) {
1749 INIT_THREAD_DISPATCH_FIELDS(hs, Vertex);
1750
1751 hs.InstanceCount = tcs_prog_data->instances - 1;
1752 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
1753 hs.IncludeVertexHandles = true;
1754 }
1755 }
1756
1757 static void
1758 iris_set_tes_state(const struct gen_device_info *devinfo,
1759 struct iris_compiled_shader *shader)
1760 {
1761 struct brw_stage_prog_data *prog_data = shader->prog_data;
1762 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1763 struct brw_tes_prog_data *tes_prog_data = (void *) prog_data;
1764
1765 uint32_t *te_state = (void *) shader->derived_data;
1766 uint32_t *ds_state = te_state + GENX(3DSTATE_TE_length);
1767
1768 iris_pack_command(GENX(3DSTATE_TE), te_state, te) {
1769 te.Partitioning = tes_prog_data->partitioning;
1770 te.OutputTopology = tes_prog_data->output_topology;
1771 te.TEDomain = tes_prog_data->domain;
1772 te.TEEnable = true;
1773 te.MaximumTessellationFactorOdd = 63.0;
1774 te.MaximumTessellationFactorNotOdd = 64.0;
1775 }
1776
1777 iris_pack_command(GENX(3DSTATE_DS), ds_state, ds) {
1778 INIT_THREAD_DISPATCH_FIELDS(ds, Patch);
1779
1780 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
1781 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
1782 ds.ComputeWCoordinateEnable =
1783 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
1784
1785 ds.UserClipDistanceCullTestEnableBitmask =
1786 vue_prog_data->cull_distance_mask;
1787 }
1788
1789 }
1790
1791 static void
1792 iris_set_gs_state(const struct gen_device_info *devinfo,
1793 struct iris_compiled_shader *shader)
1794 {
1795 struct brw_stage_prog_data *prog_data = shader->prog_data;
1796 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1797 struct brw_gs_prog_data *gs_prog_data = (void *) prog_data;
1798
1799 iris_pack_command(GENX(3DSTATE_GS), shader->derived_data, gs) {
1800 INIT_THREAD_DISPATCH_FIELDS(gs, Vertex);
1801
1802 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
1803 gs.OutputTopology = gs_prog_data->output_topology;
1804 gs.ControlDataHeaderSize =
1805 gs_prog_data->control_data_header_size_hwords;
1806 gs.InstanceControl = gs_prog_data->invocations - 1;
1807 gs.DispatchMode = SIMD8;
1808 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
1809 gs.ControlDataFormat = gs_prog_data->control_data_format;
1810 gs.ReorderMode = TRAILING;
1811 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
1812 gs.MaximumNumberofThreads =
1813 GEN_GEN == 8 ? (devinfo->max_gs_threads / 2 - 1)
1814 : (devinfo->max_gs_threads - 1);
1815
1816 if (gs_prog_data->static_vertex_count != -1) {
1817 gs.StaticOutput = true;
1818 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count;
1819 }
1820 gs.IncludeVertexHandles = vue_prog_data->include_vue_handles;
1821
1822 gs.UserClipDistanceCullTestEnableBitmask =
1823 vue_prog_data->cull_distance_mask;
1824
1825 const int urb_entry_write_offset = 1;
1826 const uint32_t urb_entry_output_length =
1827 DIV_ROUND_UP(vue_prog_data->vue_map.num_slots, 2) -
1828 urb_entry_write_offset;
1829
1830 gs.VertexURBEntryOutputReadOffset = urb_entry_write_offset;
1831 gs.VertexURBEntryOutputLength = MAX2(urb_entry_output_length, 1);
1832 }
1833 }
1834
1835 static void
1836 iris_set_fs_state(const struct gen_device_info *devinfo,
1837 struct iris_compiled_shader *shader)
1838 {
1839 struct brw_stage_prog_data *prog_data = shader->prog_data;
1840 struct brw_wm_prog_data *wm_prog_data = (void *) shader->prog_data;
1841
1842 uint32_t *ps_state = (void *) shader->derived_data;
1843 uint32_t *psx_state = ps_state + GENX(3DSTATE_PS_length);
1844
1845 iris_pack_command(GENX(3DSTATE_PS), ps_state, ps) {
1846 ps.VectorMaskEnable = true;
1847 //ps.SamplerCount = ...
1848 ps.BindingTableEntryCount = prog_data->binding_table.size_bytes / 4;
1849 ps.FloatingPointMode = prog_data->use_alt_mode;
1850 ps.MaximumNumberofThreadsPerPSD = 64 - (GEN_GEN == 8 ? 2 : 1);
1851
1852 ps.PushConstantEnable = prog_data->nr_params > 0 ||
1853 prog_data->ubo_ranges[0].length > 0;
1854
1855 /* From the documentation for this packet:
1856 * "If the PS kernel does not need the Position XY Offsets to
1857 * compute a Position Value, then this field should be programmed
1858 * to POSOFFSET_NONE."
1859 *
1860 * "SW Recommendation: If the PS kernel needs the Position Offsets
1861 * to compute a Position XY value, this field should match Position
1862 * ZW Interpolation Mode to ensure a consistent position.xyzw
1863 * computation."
1864 *
1865 * We only require XY sample offsets. So, this recommendation doesn't
1866 * look useful at the moment. We might need this in future.
1867 */
1868 ps.PositionXYOffsetSelect =
1869 wm_prog_data->uses_pos_offset ? POSOFFSET_SAMPLE : POSOFFSET_NONE;
1870 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
1871 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
1872 ps._32PixelDispatchEnable = wm_prog_data->dispatch_32;
1873
1874 // XXX: Disable SIMD32 with 16x MSAA
1875
1876 ps.DispatchGRFStartRegisterForConstantSetupData0 =
1877 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 0);
1878 ps.DispatchGRFStartRegisterForConstantSetupData1 =
1879 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 1);
1880 ps.DispatchGRFStartRegisterForConstantSetupData2 =
1881 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 2);
1882
1883 ps.KernelStartPointer0 =
1884 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 0);
1885 ps.KernelStartPointer1 =
1886 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 1);
1887 ps.KernelStartPointer2 =
1888 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
1889 }
1890
1891 iris_pack_command(GENX(3DSTATE_PS_EXTRA), psx_state, psx) {
1892 psx.PixelShaderValid = true;
1893 psx.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
1894 psx.PixelShaderKillsPixel = wm_prog_data->uses_kill;
1895 psx.AttributeEnable = wm_prog_data->num_varying_inputs != 0;
1896 psx.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
1897 psx.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
1898 psx.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
1899
1900 if (wm_prog_data->uses_sample_mask) {
1901 /* TODO: conservative rasterization */
1902 if (wm_prog_data->post_depth_coverage)
1903 psx.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
1904 else
1905 psx.InputCoverageMaskState = ICMS_NORMAL;
1906 }
1907
1908 psx.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
1909 psx.PixelShaderPullsBary = wm_prog_data->pulls_bary;
1910 psx.PixelShaderComputesStencil = wm_prog_data->computed_stencil;
1911
1912 // XXX: UAV bit
1913 }
1914 }
1915
1916 static unsigned
1917 iris_derived_program_state_size(enum iris_program_cache_id cache_id)
1918 {
1919 assert(cache_id <= IRIS_CACHE_BLORP);
1920
1921 static const unsigned dwords[] = {
1922 [IRIS_CACHE_VS] = GENX(3DSTATE_VS_length),
1923 [IRIS_CACHE_TCS] = GENX(3DSTATE_HS_length),
1924 [IRIS_CACHE_TES] = GENX(3DSTATE_TE_length) + GENX(3DSTATE_DS_length),
1925 [IRIS_CACHE_GS] = GENX(3DSTATE_GS_length),
1926 [IRIS_CACHE_FS] =
1927 GENX(3DSTATE_PS_length) + GENX(3DSTATE_PS_EXTRA_length),
1928 [IRIS_CACHE_CS] = 0,
1929 [IRIS_CACHE_BLORP] = 0,
1930 };
1931
1932 return sizeof(uint32_t) * dwords[cache_id];
1933 }
1934
1935 static void
1936 iris_set_derived_program_state(const struct gen_device_info *devinfo,
1937 enum iris_program_cache_id cache_id,
1938 struct iris_compiled_shader *shader)
1939 {
1940 switch (cache_id) {
1941 case IRIS_CACHE_VS:
1942 iris_set_vs_state(devinfo, shader);
1943 break;
1944 case IRIS_CACHE_TCS:
1945 iris_set_tcs_state(devinfo, shader);
1946 break;
1947 case IRIS_CACHE_TES:
1948 iris_set_tes_state(devinfo, shader);
1949 break;
1950 case IRIS_CACHE_GS:
1951 iris_set_gs_state(devinfo, shader);
1952 break;
1953 case IRIS_CACHE_FS:
1954 iris_set_fs_state(devinfo, shader);
1955 break;
1956 case IRIS_CACHE_CS:
1957 case IRIS_CACHE_BLORP:
1958 break;
1959 default:
1960 break;
1961 }
1962 }
1963
1964 static void
1965 iris_upload_urb_config(struct iris_context *ice, struct iris_batch *batch)
1966 {
1967 const struct gen_device_info *devinfo = &batch->screen->devinfo;
1968 const unsigned push_size_kB = 32;
1969 unsigned entries[4];
1970 unsigned start[4];
1971 unsigned size[4];
1972
1973 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
1974 if (!ice->shaders.prog[i]) {
1975 size[i] = 1;
1976 } else {
1977 struct brw_vue_prog_data *vue_prog_data =
1978 (void *) ice->shaders.prog[i]->prog_data;
1979 size[i] = vue_prog_data->urb_entry_size;
1980 }
1981 assert(size[i] != 0);
1982 }
1983
1984 gen_get_urb_config(devinfo, 1024 * push_size_kB,
1985 1024 * ice->shaders.urb_size,
1986 ice->shaders.prog[MESA_SHADER_TESS_EVAL] != NULL,
1987 ice->shaders.prog[MESA_SHADER_GEOMETRY] != NULL,
1988 size, entries, start);
1989
1990 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
1991 iris_emit_cmd(batch, GENX(3DSTATE_URB_VS), urb) {
1992 urb._3DCommandSubOpcode += i;
1993 urb.VSURBStartingAddress = start[i];
1994 urb.VSURBEntryAllocationSize = size[i] - 1;
1995 urb.VSNumberofURBEntries = entries[i];
1996 }
1997 }
1998 }
1999
2000 static const uint32_t push_constant_opcodes[] = {
2001 [MESA_SHADER_VERTEX] = 21,
2002 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
2003 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
2004 [MESA_SHADER_GEOMETRY] = 22,
2005 [MESA_SHADER_FRAGMENT] = 23,
2006 [MESA_SHADER_COMPUTE] = 0,
2007 };
2008
2009 /**
2010 * Add a surface to the validation list, as well as the buffer containing
2011 * the corresponding SURFACE_STATE.
2012 *
2013 * Returns the binding table entry (offset to SURFACE_STATE).
2014 */
2015 static uint32_t
2016 use_surface(struct iris_batch *batch,
2017 struct pipe_surface *p_surf,
2018 bool writeable)
2019 {
2020 struct iris_surface *surf = (void *) p_surf;
2021 struct iris_resource *res = (void *) p_surf->texture;
2022 struct iris_resource *state_res = (void *) surf->surface_state_resource;
2023 iris_use_pinned_bo(batch, res->bo, writeable);
2024 iris_use_pinned_bo(batch, state_res->bo, false);
2025
2026 return surf->surface_state_offset;
2027 }
2028
2029 static uint32_t
2030 use_sampler_view(struct iris_batch *batch, struct iris_sampler_view *isv)
2031 {
2032 struct iris_resource *res = (void *) isv->pipe.texture;
2033 struct iris_resource *state_res = (void *) isv->surface_state_resource;
2034 iris_use_pinned_bo(batch, res->bo, false);
2035 iris_use_pinned_bo(batch, state_res->bo, false);
2036
2037 return isv->surface_state_offset;
2038 }
2039
2040 static void
2041 iris_upload_render_state(struct iris_context *ice,
2042 struct iris_batch *batch,
2043 const struct pipe_draw_info *draw)
2044 {
2045 const uint64_t dirty =
2046 unlikely(INTEL_DEBUG & DEBUG_REEMIT) ? ~0ull : ice->state.dirty;
2047
2048 struct brw_wm_prog_data *wm_prog_data = (void *)
2049 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2050
2051 if (dirty & IRIS_DIRTY_CC_VIEWPORT) {
2052 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2053 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), ptr) {
2054 ptr.CCViewportPointer =
2055 emit_state(batch, ice->state.dynamic_uploader,
2056 cso->cc_vp, sizeof(cso->cc_vp), 32);
2057 }
2058 }
2059
2060 if (dirty & IRIS_DIRTY_SF_CL_VIEWPORT) {
2061 struct iris_viewport_state *cso = ice->state.cso_vp;
2062 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
2063 ptr.SFClipViewportPointer =
2064 emit_state(batch, ice->state.dynamic_uploader, cso->sf_cl_vp,
2065 4 * GENX(SF_CLIP_VIEWPORT_length) *
2066 ice->state.num_viewports, 64);
2067 }
2068 }
2069
2070 /* XXX: L3 State */
2071
2072 if (dirty & IRIS_DIRTY_URB) {
2073 iris_upload_urb_config(ice, batch);
2074 }
2075
2076 if (dirty & IRIS_DIRTY_BLEND_STATE) {
2077 struct iris_blend_state *cso_blend = ice->state.cso_blend;
2078 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2079 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
2080 const int num_dwords = 4 * (GENX(BLEND_STATE_length) +
2081 cso_fb->nr_cbufs * GENX(BLEND_STATE_ENTRY_length));
2082 uint32_t blend_offset;
2083 uint32_t *blend_map =
2084 stream_state(batch, ice->state.dynamic_uploader, 4 * num_dwords, 64,
2085 &blend_offset);
2086
2087 uint32_t blend_state_header;
2088 iris_pack_state(GENX(BLEND_STATE), &blend_state_header, bs) {
2089 bs.AlphaTestEnable = cso_zsa->alpha.enabled;
2090 bs.AlphaTestFunction = translate_compare_func(cso_zsa->alpha.func);
2091 }
2092
2093 blend_map[0] = blend_state_header | cso_blend->blend_state[0];
2094 memcpy(&blend_map[1], &cso_blend->blend_state[1],
2095 sizeof(cso_blend->blend_state) - sizeof(uint32_t));
2096
2097 iris_emit_cmd(batch, GENX(3DSTATE_BLEND_STATE_POINTERS), ptr) {
2098 ptr.BlendStatePointer = blend_offset;
2099 ptr.BlendStatePointerValid = true;
2100 }
2101 }
2102
2103 if (dirty & IRIS_DIRTY_COLOR_CALC_STATE) {
2104 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2105 uint32_t cc_offset;
2106 void *cc_map =
2107 stream_state(batch, ice->state.dynamic_uploader,
2108 sizeof(uint32_t) * GENX(COLOR_CALC_STATE_length),
2109 64, &cc_offset);
2110 iris_pack_state(GENX(COLOR_CALC_STATE), cc_map, cc) {
2111 cc.AlphaTestFormat = ALPHATEST_FLOAT32;
2112 cc.AlphaReferenceValueAsFLOAT32 = cso->alpha.ref_value;
2113 cc.BlendConstantColorRed = ice->state.blend_color.color[0];
2114 cc.BlendConstantColorGreen = ice->state.blend_color.color[1];
2115 cc.BlendConstantColorBlue = ice->state.blend_color.color[2];
2116 cc.BlendConstantColorAlpha = ice->state.blend_color.color[3];
2117 }
2118 iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
2119 ptr.ColorCalcStatePointer = cc_offset;
2120 ptr.ColorCalcStatePointerValid = true;
2121 }
2122 }
2123
2124 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2125 // XXX: wrong dirty tracking...
2126 if (!(dirty & (IRIS_DIRTY_CONSTANTS_VS << stage)))
2127 continue;
2128
2129 struct pipe_constant_buffer *cbuf0 =
2130 &ice->shaders.state[stage].constbuf[0];
2131
2132 if (!ice->shaders.prog[stage] || cbuf0->buffer || !cbuf0->buffer_size)
2133 continue;
2134
2135 struct iris_shader_state *shs = &ice->shaders.state[stage];
2136 // XXX: DIV_ROUND_UP(prog_data->nr_params, 8)?
2137 shs->const_size = DIV_ROUND_UP(cbuf0->buffer_size, 32);
2138 u_upload_data(ice->ctx.const_uploader, 0, 32 * shs->const_size, 32,
2139 cbuf0->user_buffer, &shs->const_offset,
2140 &shs->push_resource);
2141 }
2142
2143 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2144 // XXX: wrong dirty tracking...
2145 if (!(dirty & (IRIS_DIRTY_CONSTANTS_VS << stage)))
2146 continue;
2147
2148 struct iris_shader_state *shs = &ice->shaders.state[stage];
2149 struct iris_resource *res = (void *) shs->push_resource;
2150
2151 iris_emit_cmd(batch, GENX(3DSTATE_CONSTANT_VS), pkt) {
2152 pkt._3DCommandSubOpcode = push_constant_opcodes[stage];
2153 if (res) {
2154 pkt.ConstantBody.ReadLength[3] = shs->const_size;
2155 pkt.ConstantBody.Buffer[3] = ro_bo(res->bo, shs->const_offset);
2156 }
2157 }
2158 }
2159
2160 // Surfaces:
2161 // - pull constants
2162 // - ubos/ssbos/abos
2163 // - images
2164 // - textures
2165 // - render targets - write and read
2166 // XXX: 3DSTATE_BINDING_TABLE_POINTERS_XS
2167
2168 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2169 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2170 if (!shader) // XXX: dirty bits...also, emit a disable maybe?
2171 continue;
2172
2173 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2174 uint32_t bt_offset = 0;
2175 uint32_t *bt_map = NULL;
2176 int s = 0;
2177
2178 if (prog_data->binding_table.size_bytes != 0) {
2179 iris_use_pinned_bo(batch, ice->state.binder.bo, false);
2180 bt_map = iris_binder_reserve(&ice->state.binder,
2181 prog_data->binding_table.size_bytes,
2182 &bt_offset);
2183 }
2184
2185 iris_emit_cmd(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), ptr) {
2186 ptr._3DCommandSubOpcode = 38 + stage;
2187 ptr.PointertoVSBindingTable = bt_offset;
2188 }
2189
2190 if (stage == MESA_SHADER_FRAGMENT) {
2191 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2192 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
2193 bt_map[s++] = use_surface(batch, cso_fb->cbufs[i], true);
2194 }
2195 }
2196
2197 assert(prog_data->binding_table.texture_start ==
2198 (ice->state.num_textures[stage] ? s : 0xd0d0d0d0));
2199
2200 for (int i = 0; i < ice->state.num_textures[stage]; i++) {
2201 struct iris_sampler_view *view = ice->state.textures[stage][i];
2202 bt_map[s++] = use_sampler_view(batch, view);
2203 }
2204
2205 #if 0
2206 // XXX: not implemented yet
2207 assert(prog_data->binding_table.pull_constants_start == 0xd0d0d0d0);
2208 assert(prog_data->binding_table.ubo_start == 0xd0d0d0d0);
2209 assert(prog_data->binding_table.ssbo_start == 0xd0d0d0d0);
2210 assert(prog_data->binding_table.image_start == 0xd0d0d0d0);
2211 assert(prog_data->binding_table.shader_time_start == 0xd0d0d0d0);
2212 //assert(prog_data->binding_table.plane_start[1] == 0xd0d0d0d0);
2213 //assert(prog_data->binding_table.plane_start[2] == 0xd0d0d0d0);
2214 #endif
2215 }
2216
2217 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2218 if (!(dirty & (IRIS_DIRTY_SAMPLER_STATES_VS << stage)) ||
2219 !ice->shaders.prog[stage])
2220 continue;
2221
2222 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ptr) {
2223 ptr._3DCommandSubOpcode = 43 + stage;
2224 ptr.PointertoVSSamplerState = ice->state.sampler_table_offset[stage];
2225 }
2226 }
2227
2228 if (dirty & IRIS_DIRTY_MULTISAMPLE) {
2229 iris_emit_cmd(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
2230 ms.PixelLocation =
2231 ice->state.cso_rast->half_pixel_center ? CENTER : UL_CORNER;
2232 if (ice->state.framebuffer.samples > 0)
2233 ms.NumberofMultisamples = ffs(ice->state.framebuffer.samples) - 1;
2234 }
2235 }
2236
2237 if (dirty & IRIS_DIRTY_SAMPLE_MASK) {
2238 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_MASK), ms) {
2239 ms.SampleMask = MAX2(ice->state.sample_mask, 1);
2240 }
2241 }
2242
2243 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2244 if (!(dirty & (IRIS_DIRTY_VS << stage)))
2245 continue;
2246
2247 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2248
2249 if (shader) {
2250 struct iris_resource *cache = (void *) shader->buffer;
2251 iris_use_pinned_bo(batch, cache->bo, false);
2252 iris_batch_emit(batch, shader->derived_data,
2253 iris_derived_program_state_size(stage));
2254 } else {
2255 if (stage == MESA_SHADER_TESS_EVAL) {
2256 iris_emit_cmd(batch, GENX(3DSTATE_HS), hs);
2257 iris_emit_cmd(batch, GENX(3DSTATE_TE), te);
2258 iris_emit_cmd(batch, GENX(3DSTATE_DS), ds);
2259 } else if (stage == MESA_SHADER_GEOMETRY) {
2260 iris_emit_cmd(batch, GENX(3DSTATE_GS), gs);
2261 }
2262 }
2263 }
2264
2265 // XXX: SOL:
2266 // 3DSTATE_STREAMOUT
2267 // 3DSTATE_SO_BUFFER
2268 // 3DSTATE_SO_DECL_LIST
2269
2270 if (dirty & IRIS_DIRTY_CLIP) {
2271 struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2272 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2273
2274 uint32_t dynamic_clip[GENX(3DSTATE_CLIP_length)];
2275 iris_pack_command(GENX(3DSTATE_CLIP), &dynamic_clip, cl) {
2276 if (wm_prog_data->barycentric_interp_modes &
2277 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS)
2278 cl.NonPerspectiveBarycentricEnable = true;
2279
2280 cl.ForceZeroRTAIndexEnable = cso_fb->layers == 0;
2281 }
2282 iris_emit_merge(batch, cso_rast->clip, dynamic_clip,
2283 ARRAY_SIZE(cso_rast->clip));
2284 }
2285
2286 if (dirty & IRIS_DIRTY_RASTER) {
2287 struct iris_rasterizer_state *cso = ice->state.cso_rast;
2288 iris_batch_emit(batch, cso->raster, sizeof(cso->raster));
2289 iris_batch_emit(batch, cso->sf, sizeof(cso->sf));
2290
2291 }
2292
2293 if (dirty & (IRIS_DIRTY_RASTER | IRIS_DIRTY_FS)) {
2294 struct iris_rasterizer_state *cso = ice->state.cso_rast;
2295 uint32_t dynamic_wm[GENX(3DSTATE_WM_length)];
2296
2297 iris_pack_command(GENX(3DSTATE_WM), &dynamic_wm, wm) {
2298 wm.BarycentricInterpolationMode =
2299 wm_prog_data->barycentric_interp_modes;
2300
2301 if (wm_prog_data->early_fragment_tests)
2302 wm.EarlyDepthStencilControl = EDSC_PREPS;
2303 else if (wm_prog_data->has_side_effects)
2304 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
2305 }
2306 iris_emit_merge(batch, cso->wm, dynamic_wm, ARRAY_SIZE(cso->wm));
2307 }
2308
2309 if (1) {
2310 // XXX: 3DSTATE_SBE, 3DSTATE_SBE_SWIZ
2311 // -> iris_raster_state (point sprite texture coordinate origin)
2312 // -> bunch of shader state...
2313 iris_emit_sbe(batch, ice);
2314 iris_emit_cmd(batch, GENX(3DSTATE_SBE_SWIZ), sbe) {
2315 }
2316 }
2317
2318 if (dirty & IRIS_DIRTY_PS_BLEND) {
2319 struct iris_blend_state *cso_blend = ice->state.cso_blend;
2320 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
2321 uint32_t dynamic_pb[GENX(3DSTATE_PS_BLEND_length)];
2322 iris_pack_command(GENX(3DSTATE_PS_BLEND), &dynamic_pb, pb) {
2323 pb.HasWriteableRT = true; // XXX: comes from somewhere :(
2324 pb.AlphaTestEnable = cso_zsa->alpha.enabled;
2325 }
2326
2327 iris_emit_merge(batch, cso_blend->ps_blend, dynamic_pb,
2328 ARRAY_SIZE(cso_blend->ps_blend));
2329 }
2330
2331 if (dirty & IRIS_DIRTY_WM_DEPTH_STENCIL) {
2332 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2333 struct pipe_stencil_ref *p_stencil_refs = &ice->state.stencil_ref;
2334
2335 uint32_t stencil_refs[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
2336 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), &stencil_refs, wmds) {
2337 wmds.StencilReferenceValue = p_stencil_refs->ref_value[0];
2338 wmds.BackfaceStencilReferenceValue = p_stencil_refs->ref_value[1];
2339 }
2340 iris_emit_merge(batch, cso->wmds, stencil_refs, ARRAY_SIZE(cso->wmds));
2341 }
2342
2343 if (dirty & IRIS_DIRTY_SCISSOR) {
2344 // XXX: allocate at set_scissor time?
2345 uint32_t scissor_offset = ice->state.num_scissors == 0 ? 0 :
2346 emit_state(batch, ice->state.dynamic_uploader, ice->state.scissors,
2347 sizeof(struct pipe_scissor_state) *
2348 ice->state.num_scissors, 32);
2349
2350 iris_emit_cmd(batch, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
2351 ptr.ScissorRectPointer = scissor_offset;
2352 }
2353 }
2354
2355 // XXX: 3DSTATE_DEPTH_BUFFER
2356 // XXX: 3DSTATE_HIER_DEPTH_BUFFER
2357 // XXX: 3DSTATE_STENCIL_BUFFER
2358 // XXX: 3DSTATE_CLEAR_PARAMS
2359
2360 if (dirty & IRIS_DIRTY_POLYGON_STIPPLE) {
2361 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
2362 for (int i = 0; i < 32; i++) {
2363 poly.PatternRow[i] = ice->state.poly_stipple.stipple[i];
2364 }
2365 }
2366 }
2367
2368 if (dirty & IRIS_DIRTY_LINE_STIPPLE) {
2369 struct iris_rasterizer_state *cso = ice->state.cso_rast;
2370 iris_batch_emit(batch, cso->line_stipple, sizeof(cso->line_stipple));
2371 }
2372
2373 if (1) {
2374 iris_emit_cmd(batch, GENX(3DSTATE_VF_TOPOLOGY), topo) {
2375 topo.PrimitiveTopologyType =
2376 translate_prim_type(draw->mode, draw->vertices_per_patch);
2377 }
2378 }
2379
2380 if (draw->index_size > 0) {
2381 struct iris_resource *res = (struct iris_resource *)draw->index.resource;
2382
2383 assert(!draw->has_user_indices);
2384
2385 iris_emit_cmd(batch, GENX(3DSTATE_INDEX_BUFFER), ib) {
2386 ib.IndexFormat = draw->index_size;
2387 ib.MOCS = MOCS_WB;
2388 ib.BufferSize = res->bo->size;
2389 ib.BufferStartingAddress = ro_bo(res->bo, 0);
2390 }
2391 }
2392
2393 if (dirty & IRIS_DIRTY_VERTEX_BUFFERS) {
2394 struct iris_vertex_buffer_state *cso = ice->state.cso_vertex_buffers;
2395
2396 STATIC_ASSERT(GENX(VERTEX_BUFFER_STATE_length) == 4);
2397 STATIC_ASSERT((GENX(VERTEX_BUFFER_STATE_BufferStartingAddress_bits) % 32) == 0);
2398
2399 iris_batch_emit(batch, cso->vertex_buffers,
2400 sizeof(uint32_t) * (1 + 4 * cso->num_buffers));
2401
2402 for (unsigned i = 0; i < cso->num_buffers; i++) {
2403 iris_use_pinned_bo(batch, cso->bos[i], false);
2404 }
2405 }
2406
2407 if (dirty & IRIS_DIRTY_VERTEX_ELEMENTS) {
2408 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
2409 iris_batch_emit(batch, cso->vertex_elements, sizeof(uint32_t) *
2410 (1 + cso->count * GENX(VERTEX_ELEMENT_STATE_length)));
2411 for (int i = 0; i < cso->count; i++) {
2412 iris_batch_emit(batch, cso->vf_instancing[i], sizeof(uint32_t) *
2413 (cso->count * GENX(3DSTATE_VF_INSTANCING_length)));
2414 }
2415 for (int i = 0; i < cso->count; i++) {
2416 /* TODO: vertexid, instanceid support */
2417 iris_emit_cmd(batch, GENX(3DSTATE_VF_SGVS), sgvs);
2418 }
2419 }
2420
2421 if (1) {
2422 iris_emit_cmd(batch, GENX(3DSTATE_VF), vf) {
2423 if (draw->primitive_restart) {
2424 vf.IndexedDrawCutIndexEnable = true;
2425 vf.CutIndex = draw->restart_index;
2426 }
2427 }
2428 }
2429
2430 // XXX: Gen8 - PMA fix
2431
2432 assert(!draw->indirect); // XXX: indirect support
2433
2434 iris_emit_cmd(batch, GENX(3DPRIMITIVE), prim) {
2435 prim.StartInstanceLocation = draw->start_instance;
2436 prim.InstanceCount = draw->instance_count;
2437 prim.VertexCountPerInstance = draw->count;
2438 prim.VertexAccessType = draw->index_size > 0 ? RANDOM : SEQUENTIAL;
2439
2440 // XXX: this is probably bonkers.
2441 prim.StartVertexLocation = draw->start;
2442
2443 if (draw->index_size) {
2444 prim.BaseVertexLocation += draw->index_bias;
2445 } else {
2446 prim.StartVertexLocation += draw->index_bias;
2447 }
2448
2449 //prim.BaseVertexLocation = ...;
2450 }
2451 }
2452
2453 static void
2454 iris_destroy_state(struct iris_context *ice)
2455 {
2456 // XXX: unreference resources/surfaces.
2457 for (unsigned i = 0; i < ice->state.framebuffer.nr_cbufs; i++) {
2458 pipe_surface_reference(&ice->state.framebuffer.cbufs[i], NULL);
2459 }
2460 pipe_surface_reference(&ice->state.framebuffer.zsbuf, NULL);
2461 }
2462
2463 static unsigned
2464 flags_to_post_sync_op(uint32_t flags)
2465 {
2466 if (flags & PIPE_CONTROL_WRITE_IMMEDIATE)
2467 return WriteImmediateData;
2468
2469 if (flags & PIPE_CONTROL_WRITE_DEPTH_COUNT)
2470 return WritePSDepthCount;
2471
2472 if (flags & PIPE_CONTROL_WRITE_TIMESTAMP)
2473 return WriteTimestamp;
2474
2475 return 0;
2476 }
2477
2478 /**
2479 * Do the given flags have a Post Sync or LRI Post Sync operation?
2480 */
2481 static enum pipe_control_flags
2482 get_post_sync_flags(enum pipe_control_flags flags)
2483 {
2484 flags &= PIPE_CONTROL_WRITE_IMMEDIATE |
2485 PIPE_CONTROL_WRITE_DEPTH_COUNT |
2486 PIPE_CONTROL_WRITE_TIMESTAMP |
2487 PIPE_CONTROL_LRI_POST_SYNC_OP;
2488
2489 /* Only one "Post Sync Op" is allowed, and it's mutually exclusive with
2490 * "LRI Post Sync Operation". So more than one bit set would be illegal.
2491 */
2492 assert(util_bitcount(flags) <= 1);
2493
2494 return flags;
2495 }
2496
2497 // XXX: compute support
2498 #define IS_COMPUTE_PIPELINE(batch) (batch->ring != I915_EXEC_RENDER)
2499
2500 /**
2501 * Emit a series of PIPE_CONTROL commands, taking into account any
2502 * workarounds necessary to actually accomplish the caller's request.
2503 *
2504 * Unless otherwise noted, spec quotations in this function come from:
2505 *
2506 * Synchronization of the 3D Pipeline > PIPE_CONTROL Command > Programming
2507 * Restrictions for PIPE_CONTROL.
2508 */
2509 static void
2510 iris_emit_raw_pipe_control(struct iris_batch *batch, uint32_t flags,
2511 struct iris_bo *bo, uint32_t offset, uint64_t imm)
2512 {
2513 UNUSED const struct gen_device_info *devinfo = &batch->screen->devinfo;
2514 enum pipe_control_flags post_sync_flags = get_post_sync_flags(flags);
2515 enum pipe_control_flags non_lri_post_sync_flags =
2516 post_sync_flags & ~PIPE_CONTROL_LRI_POST_SYNC_OP;
2517
2518 /* Recursive PIPE_CONTROL workarounds --------------------------------
2519 * (http://knowyourmeme.com/memes/xzibit-yo-dawg)
2520 *
2521 * We do these first because we want to look at the original operation,
2522 * rather than any workarounds we set.
2523 */
2524 if (GEN_GEN == 9 && (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE)) {
2525 /* The PIPE_CONTROL "VF Cache Invalidation Enable" bit description
2526 * lists several workarounds:
2527 *
2528 * "Project: SKL, KBL, BXT
2529 *
2530 * If the VF Cache Invalidation Enable is set to a 1 in a
2531 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields
2532 * sets to 0, with the VF Cache Invalidation Enable set to 0
2533 * needs to be sent prior to the PIPE_CONTROL with VF Cache
2534 * Invalidation Enable set to a 1."
2535 */
2536 iris_emit_raw_pipe_control(batch, 0, NULL, 0, 0);
2537 }
2538
2539 if (GEN_GEN == 9 && IS_COMPUTE_PIPELINE(batch) && post_sync_flags) {
2540 /* Project: SKL / Argument: LRI Post Sync Operation [23]
2541 *
2542 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
2543 * programmed prior to programming a PIPECONTROL command with "LRI
2544 * Post Sync Operation" in GPGPU mode of operation (i.e when
2545 * PIPELINE_SELECT command is set to GPGPU mode of operation)."
2546 *
2547 * The same text exists a few rows below for Post Sync Op.
2548 */
2549 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_CS_STALL, bo, offset, imm);
2550 }
2551
2552 if (GEN_GEN == 10 && (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH)) {
2553 /* Cannonlake:
2554 * "Before sending a PIPE_CONTROL command with bit 12 set, SW must issue
2555 * another PIPE_CONTROL with Render Target Cache Flush Enable (bit 12)
2556 * = 0 and Pipe Control Flush Enable (bit 7) = 1"
2557 */
2558 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_FLUSH_ENABLE, bo,
2559 offset, imm);
2560 }
2561
2562 /* "Flush Types" workarounds ---------------------------------------------
2563 * We do these now because they may add post-sync operations or CS stalls.
2564 */
2565
2566 if (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE) {
2567 /* Project: BDW, SKL+ (stopping at CNL) / Argument: VF Invalidate
2568 *
2569 * "'Post Sync Operation' must be enabled to 'Write Immediate Data' or
2570 * 'Write PS Depth Count' or 'Write Timestamp'."
2571 */
2572 if (!bo) {
2573 flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
2574 post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
2575 non_lri_post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
2576 bo = batch->screen->workaround_bo;
2577 }
2578 }
2579
2580 /* #1130 from Gen10 workarounds page:
2581 *
2582 * "Enable Depth Stall on every Post Sync Op if Render target Cache
2583 * Flush is not enabled in same PIPE CONTROL and Enable Pixel score
2584 * board stall if Render target cache flush is enabled."
2585 *
2586 * Applicable to CNL B0 and C0 steppings only.
2587 *
2588 * The wording here is unclear, and this workaround doesn't look anything
2589 * like the internal bug report recommendations, but leave it be for now...
2590 */
2591 if (GEN_GEN == 10) {
2592 if (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH) {
2593 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
2594 } else if (flags & non_lri_post_sync_flags) {
2595 flags |= PIPE_CONTROL_DEPTH_STALL;
2596 }
2597 }
2598
2599 if (flags & PIPE_CONTROL_DEPTH_STALL) {
2600 /* From the PIPE_CONTROL instruction table, bit 13 (Depth Stall Enable):
2601 *
2602 * "This bit must be DISABLED for operations other than writing
2603 * PS_DEPTH_COUNT."
2604 *
2605 * This seems like nonsense. An Ivybridge workaround requires us to
2606 * emit a PIPE_CONTROL with a depth stall and write immediate post-sync
2607 * operation. Gen8+ requires us to emit depth stalls and depth cache
2608 * flushes together. So, it's hard to imagine this means anything other
2609 * than "we originally intended this to be used for PS_DEPTH_COUNT".
2610 *
2611 * We ignore the supposed restriction and do nothing.
2612 */
2613 }
2614
2615 if (flags & (PIPE_CONTROL_RENDER_TARGET_FLUSH |
2616 PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
2617 /* From the PIPE_CONTROL instruction table, bit 12 and bit 1:
2618 *
2619 * "This bit must be DISABLED for End-of-pipe (Read) fences,
2620 * PS_DEPTH_COUNT or TIMESTAMP queries."
2621 *
2622 * TODO: Implement end-of-pipe checking.
2623 */
2624 assert(!(post_sync_flags & (PIPE_CONTROL_WRITE_DEPTH_COUNT |
2625 PIPE_CONTROL_WRITE_TIMESTAMP)));
2626 }
2627
2628 if (flags & PIPE_CONTROL_STALL_AT_SCOREBOARD) {
2629 /* From the PIPE_CONTROL instruction table, bit 1:
2630 *
2631 * "This bit is ignored if Depth Stall Enable is set.
2632 * Further, the render cache is not flushed even if Write Cache
2633 * Flush Enable bit is set."
2634 *
2635 * We assert that the caller doesn't do this combination, to try and
2636 * prevent mistakes. It shouldn't hurt the GPU, though.
2637 */
2638 assert(!(flags & (PIPE_CONTROL_DEPTH_STALL |
2639 PIPE_CONTROL_RENDER_TARGET_FLUSH)));
2640 }
2641
2642 /* PIPE_CONTROL page workarounds ------------------------------------- */
2643
2644 if (GEN_GEN <= 8 && (flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE)) {
2645 /* From the PIPE_CONTROL page itself:
2646 *
2647 * "IVB, HSW, BDW
2648 * Restriction: Pipe_control with CS-stall bit set must be issued
2649 * before a pipe-control command that has the State Cache
2650 * Invalidate bit set."
2651 */
2652 flags |= PIPE_CONTROL_CS_STALL;
2653 }
2654
2655 if (flags & PIPE_CONTROL_FLUSH_LLC) {
2656 /* From the PIPE_CONTROL instruction table, bit 26 (Flush LLC):
2657 *
2658 * "Project: ALL
2659 * SW must always program Post-Sync Operation to "Write Immediate
2660 * Data" when Flush LLC is set."
2661 *
2662 * For now, we just require the caller to do it.
2663 */
2664 assert(flags & PIPE_CONTROL_WRITE_IMMEDIATE);
2665 }
2666
2667 /* "Post-Sync Operation" workarounds -------------------------------- */
2668
2669 /* Project: All / Argument: Global Snapshot Count Reset [19]
2670 *
2671 * "This bit must not be exercised on any product.
2672 * Requires stall bit ([20] of DW1) set."
2673 *
2674 * We don't use this, so we just assert that it isn't used. The
2675 * PIPE_CONTROL instruction page indicates that they intended this
2676 * as a debug feature and don't think it is useful in production,
2677 * but it may actually be usable, should we ever want to.
2678 */
2679 assert((flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET) == 0);
2680
2681 if (flags & (PIPE_CONTROL_MEDIA_STATE_CLEAR |
2682 PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE)) {
2683 /* Project: All / Arguments:
2684 *
2685 * - Generic Media State Clear [16]
2686 * - Indirect State Pointers Disable [16]
2687 *
2688 * "Requires stall bit ([20] of DW1) set."
2689 *
2690 * Also, the PIPE_CONTROL instruction table, bit 16 (Generic Media
2691 * State Clear) says:
2692 *
2693 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
2694 * programmed prior to programming a PIPECONTROL command with "Media
2695 * State Clear" set in GPGPU mode of operation"
2696 *
2697 * This is a subset of the earlier rule, so there's nothing to do.
2698 */
2699 flags |= PIPE_CONTROL_CS_STALL;
2700 }
2701
2702 if (flags & PIPE_CONTROL_STORE_DATA_INDEX) {
2703 /* Project: All / Argument: Store Data Index
2704 *
2705 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
2706 * than '0'."
2707 *
2708 * For now, we just assert that the caller does this. We might want to
2709 * automatically add a write to the workaround BO...
2710 */
2711 assert(non_lri_post_sync_flags != 0);
2712 }
2713
2714 if (flags & PIPE_CONTROL_SYNC_GFDT) {
2715 /* Project: All / Argument: Sync GFDT
2716 *
2717 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
2718 * than '0' or 0x2520[13] must be set."
2719 *
2720 * For now, we just assert that the caller does this.
2721 */
2722 assert(non_lri_post_sync_flags != 0);
2723 }
2724
2725 if (flags & PIPE_CONTROL_TLB_INVALIDATE) {
2726 /* Project: IVB+ / Argument: TLB inv
2727 *
2728 * "Requires stall bit ([20] of DW1) set."
2729 *
2730 * Also, from the PIPE_CONTROL instruction table:
2731 *
2732 * "Project: SKL+
2733 * Post Sync Operation or CS stall must be set to ensure a TLB
2734 * invalidation occurs. Otherwise no cycle will occur to the TLB
2735 * cache to invalidate."
2736 *
2737 * This is not a subset of the earlier rule, so there's nothing to do.
2738 */
2739 flags |= PIPE_CONTROL_CS_STALL;
2740 }
2741
2742 if (GEN_GEN == 9 && devinfo->gt == 4) {
2743 /* TODO: The big Skylake GT4 post sync op workaround */
2744 }
2745
2746 /* "GPGPU specific workarounds" (both post-sync and flush) ------------ */
2747
2748 if (IS_COMPUTE_PIPELINE(batch)) {
2749 if (GEN_GEN >= 9 && (flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE)) {
2750 /* Project: SKL+ / Argument: Tex Invalidate
2751 * "Requires stall bit ([20] of DW) set for all GPGPU Workloads."
2752 */
2753 flags |= PIPE_CONTROL_CS_STALL;
2754 }
2755
2756 if (GEN_GEN == 8 && (post_sync_flags ||
2757 (flags & (PIPE_CONTROL_NOTIFY_ENABLE |
2758 PIPE_CONTROL_DEPTH_STALL |
2759 PIPE_CONTROL_RENDER_TARGET_FLUSH |
2760 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
2761 PIPE_CONTROL_DATA_CACHE_FLUSH)))) {
2762 /* Project: BDW / Arguments:
2763 *
2764 * - LRI Post Sync Operation [23]
2765 * - Post Sync Op [15:14]
2766 * - Notify En [8]
2767 * - Depth Stall [13]
2768 * - Render Target Cache Flush [12]
2769 * - Depth Cache Flush [0]
2770 * - DC Flush Enable [5]
2771 *
2772 * "Requires stall bit ([20] of DW) set for all GPGPU and Media
2773 * Workloads."
2774 */
2775 flags |= PIPE_CONTROL_CS_STALL;
2776
2777 /* Also, from the PIPE_CONTROL instruction table, bit 20:
2778 *
2779 * "Project: BDW
2780 * This bit must be always set when PIPE_CONTROL command is
2781 * programmed by GPGPU and MEDIA workloads, except for the cases
2782 * when only Read Only Cache Invalidation bits are set (State
2783 * Cache Invalidation Enable, Instruction cache Invalidation
2784 * Enable, Texture Cache Invalidation Enable, Constant Cache
2785 * Invalidation Enable). This is to WA FFDOP CG issue, this WA
2786 * need not implemented when FF_DOP_CG is disable via "Fixed
2787 * Function DOP Clock Gate Disable" bit in RC_PSMI_CTRL register."
2788 *
2789 * It sounds like we could avoid CS stalls in some cases, but we
2790 * don't currently bother. This list isn't exactly the list above,
2791 * either...
2792 */
2793 }
2794 }
2795
2796 /* "Stall" workarounds ----------------------------------------------
2797 * These have to come after the earlier ones because we may have added
2798 * some additional CS stalls above.
2799 */
2800
2801 if (GEN_GEN < 9 && (flags & PIPE_CONTROL_CS_STALL)) {
2802 /* Project: PRE-SKL, VLV, CHV
2803 *
2804 * "[All Stepping][All SKUs]:
2805 *
2806 * One of the following must also be set:
2807 *
2808 * - Render Target Cache Flush Enable ([12] of DW1)
2809 * - Depth Cache Flush Enable ([0] of DW1)
2810 * - Stall at Pixel Scoreboard ([1] of DW1)
2811 * - Depth Stall ([13] of DW1)
2812 * - Post-Sync Operation ([13] of DW1)
2813 * - DC Flush Enable ([5] of DW1)"
2814 *
2815 * If we don't already have one of those bits set, we choose to add
2816 * "Stall at Pixel Scoreboard". Some of the other bits require a
2817 * CS stall as a workaround (see above), which would send us into
2818 * an infinite recursion of PIPE_CONTROLs. "Stall at Pixel Scoreboard"
2819 * appears to be safe, so we choose that.
2820 */
2821 const uint32_t wa_bits = PIPE_CONTROL_RENDER_TARGET_FLUSH |
2822 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
2823 PIPE_CONTROL_WRITE_IMMEDIATE |
2824 PIPE_CONTROL_WRITE_DEPTH_COUNT |
2825 PIPE_CONTROL_WRITE_TIMESTAMP |
2826 PIPE_CONTROL_STALL_AT_SCOREBOARD |
2827 PIPE_CONTROL_DEPTH_STALL |
2828 PIPE_CONTROL_DATA_CACHE_FLUSH;
2829 if (!(flags & wa_bits))
2830 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
2831 }
2832
2833 /* Emit --------------------------------------------------------------- */
2834
2835 iris_emit_cmd(batch, GENX(PIPE_CONTROL), pc) {
2836 pc.LRIPostSyncOperation = NoLRIOperation;
2837 pc.PipeControlFlushEnable = flags & PIPE_CONTROL_FLUSH_ENABLE;
2838 pc.DCFlushEnable = flags & PIPE_CONTROL_DATA_CACHE_FLUSH;
2839 pc.StoreDataIndex = 0;
2840 pc.CommandStreamerStallEnable = flags & PIPE_CONTROL_CS_STALL;
2841 pc.GlobalSnapshotCountReset =
2842 flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET;
2843 pc.TLBInvalidate = flags & PIPE_CONTROL_TLB_INVALIDATE;
2844 pc.GenericMediaStateClear = flags & PIPE_CONTROL_MEDIA_STATE_CLEAR;
2845 pc.StallAtPixelScoreboard = flags & PIPE_CONTROL_STALL_AT_SCOREBOARD;
2846 pc.RenderTargetCacheFlushEnable =
2847 flags & PIPE_CONTROL_RENDER_TARGET_FLUSH;
2848 pc.DepthCacheFlushEnable = flags & PIPE_CONTROL_DEPTH_CACHE_FLUSH;
2849 pc.StateCacheInvalidationEnable =
2850 flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE;
2851 pc.VFCacheInvalidationEnable = flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
2852 pc.ConstantCacheInvalidationEnable =
2853 flags & PIPE_CONTROL_CONST_CACHE_INVALIDATE;
2854 pc.PostSyncOperation = flags_to_post_sync_op(flags);
2855 pc.DepthStallEnable = flags & PIPE_CONTROL_DEPTH_STALL;
2856 pc.InstructionCacheInvalidateEnable =
2857 flags & PIPE_CONTROL_INSTRUCTION_INVALIDATE;
2858 pc.NotifyEnable = flags & PIPE_CONTROL_NOTIFY_ENABLE;
2859 pc.IndirectStatePointersDisable =
2860 flags & PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE;
2861 pc.TextureCacheInvalidationEnable =
2862 flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
2863 pc.Address = ro_bo(bo, offset);
2864 pc.ImmediateData = imm;
2865 }
2866 }
2867
2868 void
2869 genX(init_state)(struct iris_context *ice)
2870 {
2871 struct pipe_context *ctx = &ice->ctx;
2872
2873 ctx->create_blend_state = iris_create_blend_state;
2874 ctx->create_depth_stencil_alpha_state = iris_create_zsa_state;
2875 ctx->create_rasterizer_state = iris_create_rasterizer_state;
2876 ctx->create_sampler_state = iris_create_sampler_state;
2877 ctx->create_sampler_view = iris_create_sampler_view;
2878 ctx->create_surface = iris_create_surface;
2879 ctx->create_vertex_elements_state = iris_create_vertex_elements;
2880 ctx->create_compute_state = iris_create_compute_state;
2881 ctx->bind_blend_state = iris_bind_blend_state;
2882 ctx->bind_depth_stencil_alpha_state = iris_bind_zsa_state;
2883 ctx->bind_sampler_states = iris_bind_sampler_states;
2884 ctx->bind_rasterizer_state = iris_bind_rasterizer_state;
2885 ctx->bind_vertex_elements_state = iris_bind_vertex_elements_state;
2886 ctx->bind_compute_state = iris_bind_compute_state;
2887 ctx->delete_blend_state = iris_delete_state;
2888 ctx->delete_depth_stencil_alpha_state = iris_delete_state;
2889 ctx->delete_fs_state = iris_delete_state;
2890 ctx->delete_rasterizer_state = iris_delete_state;
2891 ctx->delete_sampler_state = iris_delete_state;
2892 ctx->delete_vertex_elements_state = iris_delete_state;
2893 ctx->delete_compute_state = iris_delete_state;
2894 ctx->delete_tcs_state = iris_delete_state;
2895 ctx->delete_tes_state = iris_delete_state;
2896 ctx->delete_gs_state = iris_delete_state;
2897 ctx->delete_vs_state = iris_delete_state;
2898 ctx->set_blend_color = iris_set_blend_color;
2899 ctx->set_clip_state = iris_set_clip_state;
2900 ctx->set_constant_buffer = iris_set_constant_buffer;
2901 ctx->set_sampler_views = iris_set_sampler_views;
2902 ctx->set_framebuffer_state = iris_set_framebuffer_state;
2903 ctx->set_polygon_stipple = iris_set_polygon_stipple;
2904 ctx->set_sample_mask = iris_set_sample_mask;
2905 ctx->set_scissor_states = iris_set_scissor_states;
2906 ctx->set_stencil_ref = iris_set_stencil_ref;
2907 ctx->set_vertex_buffers = iris_set_vertex_buffers;
2908 ctx->set_viewport_states = iris_set_viewport_states;
2909 ctx->sampler_view_destroy = iris_sampler_view_destroy;
2910 ctx->surface_destroy = iris_surface_destroy;
2911 ctx->draw_vbo = iris_draw_vbo;
2912 ctx->launch_grid = iris_launch_grid;
2913 ctx->create_stream_output_target = iris_create_stream_output_target;
2914 ctx->stream_output_target_destroy = iris_stream_output_target_destroy;
2915 ctx->set_stream_output_targets = iris_set_stream_output_targets;
2916
2917 ice->vtbl.destroy_state = iris_destroy_state;
2918 ice->vtbl.init_render_context = iris_init_render_context;
2919 ice->vtbl.upload_render_state = iris_upload_render_state;
2920 ice->vtbl.emit_raw_pipe_control = iris_emit_raw_pipe_control;
2921 ice->vtbl.derived_program_state_size = iris_derived_program_state_size;
2922 ice->vtbl.set_derived_program_state = iris_set_derived_program_state;
2923 ice->vtbl.populate_vs_key = iris_populate_vs_key;
2924 ice->vtbl.populate_tcs_key = iris_populate_tcs_key;
2925 ice->vtbl.populate_tes_key = iris_populate_tes_key;
2926 ice->vtbl.populate_gs_key = iris_populate_gs_key;
2927 ice->vtbl.populate_fs_key = iris_populate_fs_key;
2928
2929 ice->state.dirty = ~0ull;
2930 }