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