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