iris: fix some hangs around null framebuffers
[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 #if HAVE_VALGRIND
27 #include <valgrind.h>
28 #include <memcheck.h>
29 #define VG(x) x
30 #ifndef NDEBUG
31 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
32 #endif
33 #else
34 #define VG(x)
35 #endif
36
37 #include "pipe/p_defines.h"
38 #include "pipe/p_state.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_screen.h"
41 #include "util/u_inlines.h"
42 #include "util/u_format.h"
43 #include "util/u_framebuffer.h"
44 #include "util/u_transfer.h"
45 #include "util/u_upload_mgr.h"
46 #include "util/u_viewport.h"
47 #include "i915_drm.h"
48 #include "nir.h"
49 #include "intel/compiler/brw_compiler.h"
50 #include "intel/common/gen_l3_config.h"
51 #include "intel/common/gen_sample_positions.h"
52 #include "iris_batch.h"
53 #include "iris_context.h"
54 #include "iris_pipe.h"
55 #include "iris_resource.h"
56
57 #define __gen_address_type struct iris_address
58 #define __gen_user_data struct iris_batch
59
60 #define ARRAY_BYTES(x) (sizeof(uint32_t) * ARRAY_SIZE(x))
61
62 static uint64_t
63 __gen_combine_address(struct iris_batch *batch, void *location,
64 struct iris_address addr, uint32_t delta)
65 {
66 uint64_t result = addr.offset + delta;
67
68 if (addr.bo) {
69 iris_use_pinned_bo(batch, addr.bo, addr.write);
70 /* Assume this is a general address, not relative to a base. */
71 result += addr.bo->gtt_offset;
72 }
73
74 return result;
75 }
76
77 #define __genxml_cmd_length(cmd) cmd ## _length
78 #define __genxml_cmd_length_bias(cmd) cmd ## _length_bias
79 #define __genxml_cmd_header(cmd) cmd ## _header
80 #define __genxml_cmd_pack(cmd) cmd ## _pack
81
82 #define _iris_pack_command(batch, cmd, dst, name) \
83 for (struct cmd name = { __genxml_cmd_header(cmd) }, \
84 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
85 ({ __genxml_cmd_pack(cmd)(batch, (void *)_dst, &name); \
86 _dst = NULL; \
87 }))
88
89 #define iris_pack_command(cmd, dst, name) \
90 _iris_pack_command(NULL, cmd, dst, name)
91
92 #define iris_pack_state(cmd, dst, name) \
93 for (struct cmd name = {}, \
94 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
95 __genxml_cmd_pack(cmd)(NULL, (void *)_dst, &name), \
96 _dst = NULL)
97
98 #define iris_emit_cmd(batch, cmd, name) \
99 _iris_pack_command(batch, cmd, iris_get_command_space(batch, 4 * __genxml_cmd_length(cmd)), name)
100
101 #define iris_emit_merge(batch, dwords0, dwords1, num_dwords) \
102 do { \
103 uint32_t *dw = iris_get_command_space(batch, 4 * num_dwords); \
104 for (uint32_t i = 0; i < num_dwords; i++) \
105 dw[i] = (dwords0)[i] | (dwords1)[i]; \
106 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, num_dwords)); \
107 } while (0)
108
109 #include "genxml/genX_pack.h"
110 #include "genxml/gen_macros.h"
111 #include "genxml/genX_bits.h"
112
113 #define MOCS_WB (2 << 1)
114
115 UNUSED static void pipe_asserts()
116 {
117 #define PIPE_ASSERT(x) STATIC_ASSERT((int)x)
118
119 /* pipe_logicop happens to match the hardware. */
120 PIPE_ASSERT(PIPE_LOGICOP_CLEAR == LOGICOP_CLEAR);
121 PIPE_ASSERT(PIPE_LOGICOP_NOR == LOGICOP_NOR);
122 PIPE_ASSERT(PIPE_LOGICOP_AND_INVERTED == LOGICOP_AND_INVERTED);
123 PIPE_ASSERT(PIPE_LOGICOP_COPY_INVERTED == LOGICOP_COPY_INVERTED);
124 PIPE_ASSERT(PIPE_LOGICOP_AND_REVERSE == LOGICOP_AND_REVERSE);
125 PIPE_ASSERT(PIPE_LOGICOP_INVERT == LOGICOP_INVERT);
126 PIPE_ASSERT(PIPE_LOGICOP_XOR == LOGICOP_XOR);
127 PIPE_ASSERT(PIPE_LOGICOP_NAND == LOGICOP_NAND);
128 PIPE_ASSERT(PIPE_LOGICOP_AND == LOGICOP_AND);
129 PIPE_ASSERT(PIPE_LOGICOP_EQUIV == LOGICOP_EQUIV);
130 PIPE_ASSERT(PIPE_LOGICOP_NOOP == LOGICOP_NOOP);
131 PIPE_ASSERT(PIPE_LOGICOP_OR_INVERTED == LOGICOP_OR_INVERTED);
132 PIPE_ASSERT(PIPE_LOGICOP_COPY == LOGICOP_COPY);
133 PIPE_ASSERT(PIPE_LOGICOP_OR_REVERSE == LOGICOP_OR_REVERSE);
134 PIPE_ASSERT(PIPE_LOGICOP_OR == LOGICOP_OR);
135 PIPE_ASSERT(PIPE_LOGICOP_SET == LOGICOP_SET);
136
137 /* pipe_blend_func happens to match the hardware. */
138 PIPE_ASSERT(PIPE_BLENDFACTOR_ONE == BLENDFACTOR_ONE);
139 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_COLOR == BLENDFACTOR_SRC_COLOR);
140 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA == BLENDFACTOR_SRC_ALPHA);
141 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_ALPHA == BLENDFACTOR_DST_ALPHA);
142 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_COLOR == BLENDFACTOR_DST_COLOR);
143 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE == BLENDFACTOR_SRC_ALPHA_SATURATE);
144 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_COLOR == BLENDFACTOR_CONST_COLOR);
145 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_ALPHA == BLENDFACTOR_CONST_ALPHA);
146 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_COLOR == BLENDFACTOR_SRC1_COLOR);
147 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_ALPHA == BLENDFACTOR_SRC1_ALPHA);
148 PIPE_ASSERT(PIPE_BLENDFACTOR_ZERO == BLENDFACTOR_ZERO);
149 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_COLOR == BLENDFACTOR_INV_SRC_COLOR);
150 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_ALPHA == BLENDFACTOR_INV_SRC_ALPHA);
151 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_ALPHA == BLENDFACTOR_INV_DST_ALPHA);
152 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_COLOR == BLENDFACTOR_INV_DST_COLOR);
153 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_COLOR == BLENDFACTOR_INV_CONST_COLOR);
154 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_ALPHA == BLENDFACTOR_INV_CONST_ALPHA);
155 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_COLOR == BLENDFACTOR_INV_SRC1_COLOR);
156 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_ALPHA == BLENDFACTOR_INV_SRC1_ALPHA);
157
158 /* pipe_blend_func happens to match the hardware. */
159 PIPE_ASSERT(PIPE_BLEND_ADD == BLENDFUNCTION_ADD);
160 PIPE_ASSERT(PIPE_BLEND_SUBTRACT == BLENDFUNCTION_SUBTRACT);
161 PIPE_ASSERT(PIPE_BLEND_REVERSE_SUBTRACT == BLENDFUNCTION_REVERSE_SUBTRACT);
162 PIPE_ASSERT(PIPE_BLEND_MIN == BLENDFUNCTION_MIN);
163 PIPE_ASSERT(PIPE_BLEND_MAX == BLENDFUNCTION_MAX);
164
165 /* pipe_stencil_op happens to match the hardware. */
166 PIPE_ASSERT(PIPE_STENCIL_OP_KEEP == STENCILOP_KEEP);
167 PIPE_ASSERT(PIPE_STENCIL_OP_ZERO == STENCILOP_ZERO);
168 PIPE_ASSERT(PIPE_STENCIL_OP_REPLACE == STENCILOP_REPLACE);
169 PIPE_ASSERT(PIPE_STENCIL_OP_INCR == STENCILOP_INCRSAT);
170 PIPE_ASSERT(PIPE_STENCIL_OP_DECR == STENCILOP_DECRSAT);
171 PIPE_ASSERT(PIPE_STENCIL_OP_INCR_WRAP == STENCILOP_INCR);
172 PIPE_ASSERT(PIPE_STENCIL_OP_DECR_WRAP == STENCILOP_DECR);
173 PIPE_ASSERT(PIPE_STENCIL_OP_INVERT == STENCILOP_INVERT);
174
175 /* pipe_sprite_coord_mode happens to match 3DSTATE_SBE */
176 PIPE_ASSERT(PIPE_SPRITE_COORD_UPPER_LEFT == UPPERLEFT);
177 PIPE_ASSERT(PIPE_SPRITE_COORD_LOWER_LEFT == LOWERLEFT);
178 #undef PIPE_ASSERT
179 }
180
181 static unsigned
182 translate_prim_type(enum pipe_prim_type prim, uint8_t verts_per_patch)
183 {
184 static const unsigned map[] = {
185 [PIPE_PRIM_POINTS] = _3DPRIM_POINTLIST,
186 [PIPE_PRIM_LINES] = _3DPRIM_LINELIST,
187 [PIPE_PRIM_LINE_LOOP] = _3DPRIM_LINELOOP,
188 [PIPE_PRIM_LINE_STRIP] = _3DPRIM_LINESTRIP,
189 [PIPE_PRIM_TRIANGLES] = _3DPRIM_TRILIST,
190 [PIPE_PRIM_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
191 [PIPE_PRIM_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
192 [PIPE_PRIM_QUADS] = _3DPRIM_QUADLIST,
193 [PIPE_PRIM_QUAD_STRIP] = _3DPRIM_QUADSTRIP,
194 [PIPE_PRIM_POLYGON] = _3DPRIM_POLYGON,
195 [PIPE_PRIM_LINES_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
196 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
197 [PIPE_PRIM_TRIANGLES_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
198 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
199 [PIPE_PRIM_PATCHES] = _3DPRIM_PATCHLIST_1 - 1,
200 };
201
202 return map[prim] + (prim == PIPE_PRIM_PATCHES ? verts_per_patch : 0);
203 }
204
205 static unsigned
206 translate_compare_func(enum pipe_compare_func pipe_func)
207 {
208 static const unsigned map[] = {
209 [PIPE_FUNC_NEVER] = COMPAREFUNCTION_NEVER,
210 [PIPE_FUNC_LESS] = COMPAREFUNCTION_LESS,
211 [PIPE_FUNC_EQUAL] = COMPAREFUNCTION_EQUAL,
212 [PIPE_FUNC_LEQUAL] = COMPAREFUNCTION_LEQUAL,
213 [PIPE_FUNC_GREATER] = COMPAREFUNCTION_GREATER,
214 [PIPE_FUNC_NOTEQUAL] = COMPAREFUNCTION_NOTEQUAL,
215 [PIPE_FUNC_GEQUAL] = COMPAREFUNCTION_GEQUAL,
216 [PIPE_FUNC_ALWAYS] = COMPAREFUNCTION_ALWAYS,
217 };
218 return map[pipe_func];
219 }
220
221 static unsigned
222 translate_shadow_func(enum pipe_compare_func pipe_func)
223 {
224 /* Gallium specifies the result of shadow comparisons as:
225 *
226 * 1 if ref <op> texel,
227 * 0 otherwise.
228 *
229 * The hardware does:
230 *
231 * 0 if texel <op> ref,
232 * 1 otherwise.
233 *
234 * So we need to flip the operator and also negate.
235 */
236 static const unsigned map[] = {
237 [PIPE_FUNC_NEVER] = PREFILTEROPALWAYS,
238 [PIPE_FUNC_LESS] = PREFILTEROPLEQUAL,
239 [PIPE_FUNC_EQUAL] = PREFILTEROPNOTEQUAL,
240 [PIPE_FUNC_LEQUAL] = PREFILTEROPLESS,
241 [PIPE_FUNC_GREATER] = PREFILTEROPGEQUAL,
242 [PIPE_FUNC_NOTEQUAL] = PREFILTEROPEQUAL,
243 [PIPE_FUNC_GEQUAL] = PREFILTEROPGREATER,
244 [PIPE_FUNC_ALWAYS] = PREFILTEROPNEVER,
245 };
246 return map[pipe_func];
247 }
248
249 static unsigned
250 translate_cull_mode(unsigned pipe_face)
251 {
252 static const unsigned map[4] = {
253 [PIPE_FACE_NONE] = CULLMODE_NONE,
254 [PIPE_FACE_FRONT] = CULLMODE_FRONT,
255 [PIPE_FACE_BACK] = CULLMODE_BACK,
256 [PIPE_FACE_FRONT_AND_BACK] = CULLMODE_BOTH,
257 };
258 return map[pipe_face];
259 }
260
261 static unsigned
262 translate_fill_mode(unsigned pipe_polymode)
263 {
264 static const unsigned map[4] = {
265 [PIPE_POLYGON_MODE_FILL] = FILL_MODE_SOLID,
266 [PIPE_POLYGON_MODE_LINE] = FILL_MODE_WIREFRAME,
267 [PIPE_POLYGON_MODE_POINT] = FILL_MODE_POINT,
268 [PIPE_POLYGON_MODE_FILL_RECTANGLE] = FILL_MODE_SOLID,
269 };
270 return map[pipe_polymode];
271 }
272
273 static struct iris_address
274 ro_bo(struct iris_bo *bo, uint64_t offset)
275 {
276 /* Not for CSOs! */
277 return (struct iris_address) { .bo = bo, .offset = offset };
278 }
279
280 static struct iris_address
281 rw_bo(struct iris_bo *bo, uint64_t offset)
282 {
283 /* Not for CSOs! */
284 return (struct iris_address) { .bo = bo, .offset = offset, .write = true };
285 }
286
287 static void *
288 upload_state(struct u_upload_mgr *uploader,
289 struct iris_state_ref *ref,
290 unsigned size,
291 unsigned alignment)
292 {
293 void *p = NULL;
294 u_upload_alloc(uploader, 0, size, alignment, &ref->offset, &ref->res, &p);
295 return p;
296 }
297
298 static uint32_t *
299 stream_state(struct iris_batch *batch,
300 struct u_upload_mgr *uploader,
301 struct pipe_resource **out_res,
302 unsigned size,
303 unsigned alignment,
304 uint32_t *out_offset)
305 {
306 void *ptr = NULL;
307
308 u_upload_alloc(uploader, 0, size, alignment, out_offset, out_res, &ptr);
309
310 struct iris_bo *bo = iris_resource_bo(*out_res);
311 iris_use_pinned_bo(batch, bo, false);
312
313 *out_offset += iris_bo_offset_from_base_address(bo);
314
315 return ptr;
316 }
317
318 static uint32_t
319 emit_state(struct iris_batch *batch,
320 struct u_upload_mgr *uploader,
321 struct pipe_resource **out_res,
322 const void *data,
323 unsigned size,
324 unsigned alignment)
325 {
326 unsigned offset = 0;
327 uint32_t *map =
328 stream_state(batch, uploader, out_res, size, alignment, &offset);
329
330 if (map)
331 memcpy(map, data, size);
332
333 return offset;
334 }
335
336 #define cso_changed(x) (!old_cso || (old_cso->x != new_cso->x))
337 #define cso_changed_memcmp(x) \
338 (!old_cso || memcmp(old_cso->x, new_cso->x, sizeof(old_cso->x)) != 0)
339
340 static void
341 iris_init_render_context(struct iris_screen *screen,
342 struct iris_batch *batch,
343 struct iris_vtable *vtbl,
344 struct pipe_debug_callback *dbg)
345 {
346 iris_init_batch(batch, screen, vtbl, dbg, I915_EXEC_RENDER);
347
348 /* XXX: PIPE_CONTROLs */
349
350 iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
351 #if 0
352 // XXX: MOCS is stupid for this.
353 sba.GeneralStateMemoryObjectControlState = MOCS_WB;
354 sba.StatelessDataPortAccessMemoryObjectControlState = MOCS_WB;
355 sba.SurfaceStateMemoryObjectControlState = MOCS_WB;
356 sba.DynamicStateMemoryObjectControlState = MOCS_WB;
357 sba.IndirectObjectMemoryObjectControlState = MOCS_WB;
358 sba.InstructionMemoryObjectControlState = MOCS_WB;
359 sba.BindlessSurfaceStateMemoryObjectControlState = MOCS_WB;
360 #endif
361
362 sba.GeneralStateBaseAddressModifyEnable = true;
363 sba.SurfaceStateBaseAddressModifyEnable = true;
364 sba.DynamicStateBaseAddressModifyEnable = true;
365 sba.IndirectObjectBaseAddressModifyEnable = true;
366 sba.InstructionBaseAddressModifyEnable = true;
367 sba.GeneralStateBufferSizeModifyEnable = true;
368 sba.DynamicStateBufferSizeModifyEnable = true;
369 sba.BindlessSurfaceStateBaseAddressModifyEnable = true;
370 sba.IndirectObjectBufferSizeModifyEnable = true;
371 sba.InstructionBuffersizeModifyEnable = true;
372
373 sba.InstructionBaseAddress = ro_bo(NULL, IRIS_MEMZONE_SHADER_START);
374 sba.SurfaceStateBaseAddress = ro_bo(NULL, IRIS_MEMZONE_SURFACE_START);
375 sba.DynamicStateBaseAddress = ro_bo(NULL, IRIS_MEMZONE_DYNAMIC_START);
376
377 sba.GeneralStateBufferSize = 0xfffff;
378 sba.IndirectObjectBufferSize = 0xfffff;
379 sba.InstructionBufferSize = 0xfffff;
380 sba.DynamicStateBufferSize = 0xfffff;
381 }
382
383 iris_emit_cmd(batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
384 rect.ClippedDrawingRectangleXMax = UINT16_MAX;
385 rect.ClippedDrawingRectangleYMax = UINT16_MAX;
386 }
387 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_PATTERN), pat) {
388 GEN_SAMPLE_POS_1X(pat._1xSample);
389 GEN_SAMPLE_POS_2X(pat._2xSample);
390 GEN_SAMPLE_POS_4X(pat._4xSample);
391 GEN_SAMPLE_POS_8X(pat._8xSample);
392 GEN_SAMPLE_POS_16X(pat._16xSample);
393 }
394 iris_emit_cmd(batch, GENX(3DSTATE_AA_LINE_PARAMETERS), foo);
395 iris_emit_cmd(batch, GENX(3DSTATE_WM_CHROMAKEY), foo);
396 iris_emit_cmd(batch, GENX(3DSTATE_WM_HZ_OP), foo);
397 /* XXX: may need to set an offset for origin-UL framebuffers */
398 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_OFFSET), foo);
399
400 /* Just assign a static partitioning. */
401 for (int i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
402 iris_emit_cmd(batch, GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
403 alloc._3DCommandSubOpcode = 18 + i;
404 alloc.ConstantBufferOffset = 6 * i;
405 alloc.ConstantBufferSize = i == MESA_SHADER_FRAGMENT ? 8 : 6;
406 }
407 }
408 }
409
410 struct iris_vertex_buffer_state {
411 uint32_t vertex_buffers[1 + 33 * GENX(VERTEX_BUFFER_STATE_length)];
412 struct pipe_resource *resources[33];
413 unsigned num_buffers;
414 };
415
416 struct iris_depth_buffer_state {
417 uint32_t packets[GENX(3DSTATE_DEPTH_BUFFER_length) +
418 GENX(3DSTATE_STENCIL_BUFFER_length) +
419 GENX(3DSTATE_HIER_DEPTH_BUFFER_length) +
420 GENX(3DSTATE_CLEAR_PARAMS_length)];
421 };
422
423 /**
424 * State that can't be stored directly in iris_context because the data
425 * layout varies per generation.
426 */
427 struct iris_genx_state {
428 /** SF_CLIP_VIEWPORT */
429 uint32_t sf_cl_vp[GENX(SF_CLIP_VIEWPORT_length) * IRIS_MAX_VIEWPORTS];
430
431 struct iris_vertex_buffer_state vertex_buffers;
432 struct iris_depth_buffer_state depth_buffer;
433
434 uint32_t so_buffers[4 * GENX(3DSTATE_SO_BUFFER_length)];
435 uint32_t streamout[4 * GENX(3DSTATE_STREAMOUT_length)];
436 };
437
438 static void
439 iris_launch_grid(struct pipe_context *ctx, const struct pipe_grid_info *info)
440 {
441 }
442
443 static void
444 iris_set_blend_color(struct pipe_context *ctx,
445 const struct pipe_blend_color *state)
446 {
447 struct iris_context *ice = (struct iris_context *) ctx;
448
449 memcpy(&ice->state.blend_color, state, sizeof(struct pipe_blend_color));
450 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
451 }
452
453 struct iris_blend_state {
454 /** Partial 3DSTATE_PS_BLEND */
455 uint32_t ps_blend[GENX(3DSTATE_PS_BLEND_length)];
456
457 /** Partial BLEND_STATE */
458 uint32_t blend_state[GENX(BLEND_STATE_length) +
459 BRW_MAX_DRAW_BUFFERS * GENX(BLEND_STATE_ENTRY_length)];
460
461 bool alpha_to_coverage; /* for shader key */
462 };
463
464 static void *
465 iris_create_blend_state(struct pipe_context *ctx,
466 const struct pipe_blend_state *state)
467 {
468 struct iris_blend_state *cso = malloc(sizeof(struct iris_blend_state));
469 uint32_t *blend_state = cso->blend_state;
470
471 cso->alpha_to_coverage = state->alpha_to_coverage;
472
473 iris_pack_command(GENX(3DSTATE_PS_BLEND), cso->ps_blend, pb) {
474 /* pb.HasWriteableRT is filled in at draw time. */
475 /* pb.AlphaTestEnable is filled in at draw time. */
476 pb.AlphaToCoverageEnable = state->alpha_to_coverage;
477 pb.IndependentAlphaBlendEnable = state->independent_blend_enable;
478
479 pb.ColorBufferBlendEnable = state->rt[0].blend_enable;
480
481 pb.SourceBlendFactor = state->rt[0].rgb_src_factor;
482 pb.SourceAlphaBlendFactor = state->rt[0].alpha_func;
483 pb.DestinationBlendFactor = state->rt[0].rgb_dst_factor;
484 pb.DestinationAlphaBlendFactor = state->rt[0].alpha_dst_factor;
485 }
486
487 iris_pack_state(GENX(BLEND_STATE), blend_state, bs) {
488 bs.AlphaToCoverageEnable = state->alpha_to_coverage;
489 bs.IndependentAlphaBlendEnable = state->independent_blend_enable;
490 bs.AlphaToOneEnable = state->alpha_to_one;
491 bs.AlphaToCoverageDitherEnable = state->alpha_to_coverage;
492 bs.ColorDitherEnable = state->dither;
493 /* bl.AlphaTestEnable and bs.AlphaTestFunction are filled in later. */
494 }
495
496 blend_state += GENX(BLEND_STATE_length);
497
498 for (int i = 0; i < BRW_MAX_DRAW_BUFFERS; i++) {
499 iris_pack_state(GENX(BLEND_STATE_ENTRY), blend_state, be) {
500 be.LogicOpEnable = state->logicop_enable;
501 be.LogicOpFunction = state->logicop_func;
502
503 be.PreBlendSourceOnlyClampEnable = false;
504 be.ColorClampRange = COLORCLAMP_RTFORMAT;
505 be.PreBlendColorClampEnable = true;
506 be.PostBlendColorClampEnable = true;
507
508 be.ColorBufferBlendEnable = state->rt[i].blend_enable;
509
510 be.ColorBlendFunction = state->rt[i].rgb_func;
511 be.AlphaBlendFunction = state->rt[i].alpha_func;
512 be.SourceBlendFactor = state->rt[i].rgb_src_factor;
513 be.SourceAlphaBlendFactor = state->rt[i].alpha_func;
514 be.DestinationBlendFactor = state->rt[i].rgb_dst_factor;
515 be.DestinationAlphaBlendFactor = state->rt[i].alpha_dst_factor;
516
517 be.WriteDisableRed = !(state->rt[i].colormask & PIPE_MASK_R);
518 be.WriteDisableGreen = !(state->rt[i].colormask & PIPE_MASK_G);
519 be.WriteDisableBlue = !(state->rt[i].colormask & PIPE_MASK_B);
520 be.WriteDisableAlpha = !(state->rt[i].colormask & PIPE_MASK_A);
521 }
522 blend_state += GENX(BLEND_STATE_ENTRY_length);
523 }
524
525 return cso;
526 }
527
528 static void
529 iris_bind_blend_state(struct pipe_context *ctx, void *state)
530 {
531 struct iris_context *ice = (struct iris_context *) ctx;
532 ice->state.cso_blend = state;
533 ice->state.dirty |= IRIS_DIRTY_PS_BLEND;
534 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
535 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_BLEND];
536 }
537
538 struct iris_depth_stencil_alpha_state {
539 /** Partial 3DSTATE_WM_DEPTH_STENCIL */
540 uint32_t wmds[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
541
542 /** Outbound to BLEND_STATE, 3DSTATE_PS_BLEND, COLOR_CALC_STATE */
543 struct pipe_alpha_state alpha;
544 };
545
546 static void *
547 iris_create_zsa_state(struct pipe_context *ctx,
548 const struct pipe_depth_stencil_alpha_state *state)
549 {
550 struct iris_depth_stencil_alpha_state *cso =
551 malloc(sizeof(struct iris_depth_stencil_alpha_state));
552
553 cso->alpha = state->alpha;
554
555 bool two_sided_stencil = state->stencil[1].enabled;
556
557 /* The state tracker needs to optimize away EQUAL writes for us. */
558 assert(!(state->depth.func == PIPE_FUNC_EQUAL && state->depth.writemask));
559
560 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), cso->wmds, wmds) {
561 wmds.StencilFailOp = state->stencil[0].fail_op;
562 wmds.StencilPassDepthFailOp = state->stencil[0].zfail_op;
563 wmds.StencilPassDepthPassOp = state->stencil[0].zpass_op;
564 wmds.StencilTestFunction =
565 translate_compare_func(state->stencil[0].func);
566 wmds.BackfaceStencilFailOp = state->stencil[1].fail_op;
567 wmds.BackfaceStencilPassDepthFailOp = state->stencil[1].zfail_op;
568 wmds.BackfaceStencilPassDepthPassOp = state->stencil[1].zpass_op;
569 wmds.BackfaceStencilTestFunction =
570 translate_compare_func(state->stencil[1].func);
571 wmds.DepthTestFunction = translate_compare_func(state->depth.func);
572 wmds.DoubleSidedStencilEnable = two_sided_stencil;
573 wmds.StencilTestEnable = state->stencil[0].enabled;
574 wmds.StencilBufferWriteEnable =
575 state->stencil[0].writemask != 0 ||
576 (two_sided_stencil && state->stencil[1].writemask != 0);
577 wmds.DepthTestEnable = state->depth.enabled;
578 wmds.DepthBufferWriteEnable = state->depth.writemask;
579 wmds.StencilTestMask = state->stencil[0].valuemask;
580 wmds.StencilWriteMask = state->stencil[0].writemask;
581 wmds.BackfaceStencilTestMask = state->stencil[1].valuemask;
582 wmds.BackfaceStencilWriteMask = state->stencil[1].writemask;
583 /* wmds.[Backface]StencilReferenceValue are merged later */
584 }
585
586 return cso;
587 }
588
589 static void
590 iris_bind_zsa_state(struct pipe_context *ctx, void *state)
591 {
592 struct iris_context *ice = (struct iris_context *) ctx;
593 struct iris_depth_stencil_alpha_state *old_cso = ice->state.cso_zsa;
594 struct iris_depth_stencil_alpha_state *new_cso = state;
595
596 if (new_cso) {
597 if (cso_changed(alpha.ref_value))
598 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
599
600 if (cso_changed(alpha.enabled))
601 ice->state.dirty |= IRIS_DIRTY_PS_BLEND | IRIS_DIRTY_BLEND_STATE;
602
603 if (cso_changed(alpha.func))
604 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
605 }
606
607 ice->state.cso_zsa = new_cso;
608 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
609 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
610 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_DEPTH_STENCIL_ALPHA];
611 }
612
613 struct iris_rasterizer_state {
614 uint32_t sf[GENX(3DSTATE_SF_length)];
615 uint32_t clip[GENX(3DSTATE_CLIP_length)];
616 uint32_t raster[GENX(3DSTATE_RASTER_length)];
617 uint32_t wm[GENX(3DSTATE_WM_length)];
618 uint32_t line_stipple[GENX(3DSTATE_LINE_STIPPLE_length)];
619
620 bool clip_halfz; /* for CC_VIEWPORT */
621 bool depth_clip_near; /* for CC_VIEWPORT */
622 bool depth_clip_far; /* for CC_VIEWPORT */
623 bool flatshade; /* for shader state */
624 bool flatshade_first; /* for stream output */
625 bool clamp_fragment_color; /* for shader state */
626 bool light_twoside; /* for shader state */
627 bool rasterizer_discard; /* for 3DSTATE_STREAMOUT */
628 bool half_pixel_center; /* for 3DSTATE_MULTISAMPLE */
629 bool line_stipple_enable;
630 bool poly_stipple_enable;
631 bool multisample;
632 bool force_persample_interp;
633 enum pipe_sprite_coord_mode sprite_coord_mode; /* PIPE_SPRITE_* */
634 uint16_t sprite_coord_enable;
635 };
636
637 static void *
638 iris_create_rasterizer_state(struct pipe_context *ctx,
639 const struct pipe_rasterizer_state *state)
640 {
641 struct iris_rasterizer_state *cso =
642 malloc(sizeof(struct iris_rasterizer_state));
643
644 #if 0
645 point_quad_rasterization -> SBE?
646
647 not necessary?
648 {
649 poly_smooth
650 force_persample_interp - ?
651 bottom_edge_rule
652
653 offset_units_unscaled - cap not exposed
654 }
655 #endif
656
657 cso->multisample = state->multisample;
658 cso->force_persample_interp = state->force_persample_interp;
659 cso->clip_halfz = state->clip_halfz;
660 cso->depth_clip_near = state->depth_clip_near;
661 cso->depth_clip_far = state->depth_clip_far;
662 cso->flatshade = state->flatshade;
663 cso->flatshade_first = state->flatshade_first;
664 cso->clamp_fragment_color = state->clamp_fragment_color;
665 cso->light_twoside = state->light_twoside;
666 cso->rasterizer_discard = state->rasterizer_discard;
667 cso->half_pixel_center = state->half_pixel_center;
668 cso->sprite_coord_mode = state->sprite_coord_mode;
669 cso->sprite_coord_enable = state->sprite_coord_enable;
670 cso->line_stipple_enable = state->line_stipple_enable;
671 cso->poly_stipple_enable = state->poly_stipple_enable;
672
673 iris_pack_command(GENX(3DSTATE_SF), cso->sf, sf) {
674 sf.StatisticsEnable = true;
675 sf.ViewportTransformEnable = true;
676 sf.AALineDistanceMode = AALINEDISTANCE_TRUE;
677 sf.LineEndCapAntialiasingRegionWidth =
678 state->line_smooth ? _10pixels : _05pixels;
679 sf.LastPixelEnable = state->line_last_pixel;
680 sf.LineWidth = state->line_width;
681 sf.SmoothPointEnable = state->point_smooth;
682 sf.PointWidthSource = state->point_size_per_vertex ? Vertex : State;
683 sf.PointWidth = state->point_size;
684
685 if (state->flatshade_first) {
686 sf.TriangleFanProvokingVertexSelect = 1;
687 } else {
688 sf.TriangleStripListProvokingVertexSelect = 2;
689 sf.TriangleFanProvokingVertexSelect = 2;
690 sf.LineStripListProvokingVertexSelect = 1;
691 }
692 }
693
694 iris_pack_command(GENX(3DSTATE_RASTER), cso->raster, rr) {
695 rr.FrontWinding = state->front_ccw ? CounterClockwise : Clockwise;
696 rr.CullMode = translate_cull_mode(state->cull_face);
697 rr.FrontFaceFillMode = translate_fill_mode(state->fill_front);
698 rr.BackFaceFillMode = translate_fill_mode(state->fill_back);
699 rr.DXMultisampleRasterizationEnable = state->multisample;
700 rr.GlobalDepthOffsetEnableSolid = state->offset_tri;
701 rr.GlobalDepthOffsetEnableWireframe = state->offset_line;
702 rr.GlobalDepthOffsetEnablePoint = state->offset_point;
703 rr.GlobalDepthOffsetConstant = state->offset_units * 2;
704 rr.GlobalDepthOffsetScale = state->offset_scale;
705 rr.GlobalDepthOffsetClamp = state->offset_clamp;
706 rr.SmoothPointEnable = state->point_smooth;
707 rr.AntialiasingEnable = state->line_smooth;
708 rr.ScissorRectangleEnable = state->scissor;
709 rr.ViewportZNearClipTestEnable = state->depth_clip_near;
710 rr.ViewportZFarClipTestEnable = state->depth_clip_far;
711 //rr.ConservativeRasterizationEnable = not yet supported by Gallium...
712 }
713
714 iris_pack_command(GENX(3DSTATE_CLIP), cso->clip, cl) {
715 /* cl.NonPerspectiveBarycentricEnable is filled in at draw time from
716 * the FS program; cl.ForceZeroRTAIndexEnable is filled in from the FB.
717 */
718 cl.StatisticsEnable = true;
719 cl.EarlyCullEnable = true;
720 cl.UserClipDistanceClipTestEnableBitmask = state->clip_plane_enable;
721 cl.ForceUserClipDistanceClipTestEnableBitmask = true;
722 cl.APIMode = state->clip_halfz ? APIMODE_D3D : APIMODE_OGL;
723 cl.GuardbandClipTestEnable = true;
724 cl.ClipMode = CLIPMODE_NORMAL;
725 cl.ClipEnable = true;
726 cl.ViewportXYClipTestEnable = state->point_tri_clip;
727 cl.MinimumPointWidth = 0.125;
728 cl.MaximumPointWidth = 255.875;
729
730 if (state->flatshade_first) {
731 cl.TriangleFanProvokingVertexSelect = 1;
732 } else {
733 cl.TriangleStripListProvokingVertexSelect = 2;
734 cl.TriangleFanProvokingVertexSelect = 2;
735 cl.LineStripListProvokingVertexSelect = 1;
736 }
737 }
738
739 iris_pack_command(GENX(3DSTATE_WM), cso->wm, wm) {
740 /* wm.BarycentricInterpolationMode and wm.EarlyDepthStencilControl are
741 * filled in at draw time from the FS program.
742 */
743 wm.LineAntialiasingRegionWidth = _10pixels;
744 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
745 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
746 wm.StatisticsEnable = true;
747 wm.LineStippleEnable = state->line_stipple_enable;
748 wm.PolygonStippleEnable = state->poly_stipple_enable;
749 }
750
751 /* Remap from 0..255 back to 1..256 */
752 const unsigned line_stipple_factor = state->line_stipple_factor + 1;
753
754 iris_pack_command(GENX(3DSTATE_LINE_STIPPLE), cso->line_stipple, line) {
755 line.LineStipplePattern = state->line_stipple_pattern;
756 line.LineStippleInverseRepeatCount = 1.0f / line_stipple_factor;
757 line.LineStippleRepeatCount = line_stipple_factor;
758 }
759
760 return cso;
761 }
762
763 static void
764 iris_bind_rasterizer_state(struct pipe_context *ctx, void *state)
765 {
766 struct iris_context *ice = (struct iris_context *) ctx;
767 struct iris_rasterizer_state *old_cso = ice->state.cso_rast;
768 struct iris_rasterizer_state *new_cso = state;
769
770 if (new_cso) {
771 /* Try to avoid re-emitting 3DSTATE_LINE_STIPPLE, it's non-pipelined */
772 if (cso_changed_memcmp(line_stipple))
773 ice->state.dirty |= IRIS_DIRTY_LINE_STIPPLE;
774
775 if (cso_changed(half_pixel_center))
776 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
777
778 if (cso_changed(line_stipple_enable) || cso_changed(poly_stipple_enable))
779 ice->state.dirty |= IRIS_DIRTY_WM;
780
781 if (cso_changed(rasterizer_discard) || cso_changed(flatshade_first))
782 ice->state.dirty |= IRIS_DIRTY_STREAMOUT;
783
784 if (cso_changed(depth_clip_near) || cso_changed(depth_clip_far) ||
785 cso_changed(clip_halfz))
786 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
787
788 if (cso_changed(sprite_coord_enable))
789 ice->state.dirty |= IRIS_DIRTY_SBE;
790 }
791
792 ice->state.cso_rast = new_cso;
793 ice->state.dirty |= IRIS_DIRTY_RASTER;
794 ice->state.dirty |= IRIS_DIRTY_CLIP;
795 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_RASTERIZER];
796 }
797
798 static uint32_t
799 translate_wrap(unsigned pipe_wrap)
800 {
801 static const unsigned map[] = {
802 [PIPE_TEX_WRAP_REPEAT] = TCM_WRAP,
803 [PIPE_TEX_WRAP_CLAMP] = TCM_HALF_BORDER,
804 [PIPE_TEX_WRAP_CLAMP_TO_EDGE] = TCM_CLAMP,
805 [PIPE_TEX_WRAP_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
806 [PIPE_TEX_WRAP_MIRROR_REPEAT] = TCM_MIRROR,
807 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
808
809 /* These are unsupported. */
810 [PIPE_TEX_WRAP_MIRROR_CLAMP] = -1,
811 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER] = -1,
812 };
813 return map[pipe_wrap];
814 }
815
816 /**
817 * Return true if the given wrap mode requires the border color to exist.
818 */
819 static bool
820 wrap_mode_needs_border_color(unsigned wrap_mode)
821 {
822 return wrap_mode == TCM_CLAMP_BORDER || wrap_mode == TCM_HALF_BORDER;
823 }
824
825 static unsigned
826 translate_mip_filter(enum pipe_tex_mipfilter pipe_mip)
827 {
828 static const unsigned map[] = {
829 [PIPE_TEX_MIPFILTER_NEAREST] = MIPFILTER_NEAREST,
830 [PIPE_TEX_MIPFILTER_LINEAR] = MIPFILTER_LINEAR,
831 [PIPE_TEX_MIPFILTER_NONE] = MIPFILTER_NONE,
832 };
833 return map[pipe_mip];
834 }
835
836 struct iris_sampler_state {
837 struct pipe_sampler_state base;
838
839 bool needs_border_color;
840
841 uint32_t sampler_state[GENX(SAMPLER_STATE_length)];
842 };
843
844 static void *
845 iris_create_sampler_state(struct pipe_context *ctx,
846 const struct pipe_sampler_state *state)
847 {
848 struct iris_sampler_state *cso = CALLOC_STRUCT(iris_sampler_state);
849
850 if (!cso)
851 return NULL;
852
853 memcpy(&cso->base, state, sizeof(*state));
854
855 STATIC_ASSERT(PIPE_TEX_FILTER_NEAREST == MAPFILTER_NEAREST);
856 STATIC_ASSERT(PIPE_TEX_FILTER_LINEAR == MAPFILTER_LINEAR);
857
858 unsigned wrap_s = translate_wrap(state->wrap_s);
859 unsigned wrap_t = translate_wrap(state->wrap_t);
860 unsigned wrap_r = translate_wrap(state->wrap_r);
861
862 cso->needs_border_color = wrap_mode_needs_border_color(wrap_s) ||
863 wrap_mode_needs_border_color(wrap_t) ||
864 wrap_mode_needs_border_color(wrap_r);
865
866 iris_pack_state(GENX(SAMPLER_STATE), cso->sampler_state, samp) {
867 samp.TCXAddressControlMode = wrap_s;
868 samp.TCYAddressControlMode = wrap_t;
869 samp.TCZAddressControlMode = wrap_r;
870 samp.CubeSurfaceControlMode = state->seamless_cube_map;
871 samp.NonnormalizedCoordinateEnable = !state->normalized_coords;
872 samp.MinModeFilter = state->min_img_filter;
873 samp.MagModeFilter = state->mag_img_filter;
874 samp.MipModeFilter = translate_mip_filter(state->min_mip_filter);
875 samp.MaximumAnisotropy = RATIO21;
876
877 if (state->max_anisotropy >= 2) {
878 if (state->min_img_filter == PIPE_TEX_FILTER_LINEAR) {
879 samp.MinModeFilter = MAPFILTER_ANISOTROPIC;
880 samp.AnisotropicAlgorithm = EWAApproximation;
881 }
882
883 if (state->mag_img_filter == PIPE_TEX_FILTER_LINEAR)
884 samp.MagModeFilter = MAPFILTER_ANISOTROPIC;
885
886 samp.MaximumAnisotropy =
887 MIN2((state->max_anisotropy - 2) / 2, RATIO161);
888 }
889
890 /* Set address rounding bits if not using nearest filtering. */
891 if (state->min_img_filter != PIPE_TEX_FILTER_NEAREST) {
892 samp.UAddressMinFilterRoundingEnable = true;
893 samp.VAddressMinFilterRoundingEnable = true;
894 samp.RAddressMinFilterRoundingEnable = true;
895 }
896
897 if (state->mag_img_filter != PIPE_TEX_FILTER_NEAREST) {
898 samp.UAddressMagFilterRoundingEnable = true;
899 samp.VAddressMagFilterRoundingEnable = true;
900 samp.RAddressMagFilterRoundingEnable = true;
901 }
902
903 if (state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
904 samp.ShadowFunction = translate_shadow_func(state->compare_func);
905
906 const float hw_max_lod = GEN_GEN >= 7 ? 14 : 13;
907
908 samp.LODPreClampMode = CLAMP_MODE_OGL;
909 samp.MinLOD = CLAMP(state->min_lod, 0, hw_max_lod);
910 samp.MaxLOD = CLAMP(state->max_lod, 0, hw_max_lod);
911 samp.TextureLODBias = CLAMP(state->lod_bias, -16, 15);
912
913 /* .BorderColorPointer is filled in by iris_bind_sampler_states. */
914 }
915
916 return cso;
917 }
918
919 static void
920 iris_bind_sampler_states(struct pipe_context *ctx,
921 enum pipe_shader_type p_stage,
922 unsigned start, unsigned count,
923 void **states)
924 {
925 struct iris_context *ice = (struct iris_context *) ctx;
926 gl_shader_stage stage = stage_from_pipe(p_stage);
927
928 assert(start + count <= IRIS_MAX_TEXTURE_SAMPLERS);
929 ice->state.num_samplers[stage] =
930 MAX2(ice->state.num_samplers[stage], start + count);
931
932 for (int i = 0; i < count; i++) {
933 ice->state.samplers[stage][start + i] = states[i];
934 }
935
936 /* Assemble the SAMPLER_STATEs into a contiguous table that lives
937 * in the dynamic state memory zone, so we can point to it via the
938 * 3DSTATE_SAMPLER_STATE_POINTERS_* commands.
939 */
940 void *map = upload_state(ice->state.dynamic_uploader,
941 &ice->state.sampler_table[stage],
942 count * 4 * GENX(SAMPLER_STATE_length), 32);
943 if (unlikely(!map))
944 return;
945
946 struct pipe_resource *res = ice->state.sampler_table[stage].res;
947 ice->state.sampler_table[stage].offset +=
948 iris_bo_offset_from_base_address(iris_resource_bo(res));
949
950 /* Make sure all land in the same BO */
951 iris_border_color_pool_reserve(ice, IRIS_MAX_TEXTURE_SAMPLERS);
952
953 for (int i = 0; i < count; i++) {
954 struct iris_sampler_state *state = ice->state.samplers[stage][i];
955
956 /* Save a pointer to the iris_sampler_state, a few fields need
957 * to inform draw-time decisions.
958 */
959 ice->state.samplers[stage][start + i] = state;
960
961 if (!state) {
962 memset(map, 0, 4 * GENX(SAMPLER_STATE_length));
963 } else if (!state->needs_border_color) {
964 memcpy(map, state->sampler_state, 4 * GENX(SAMPLER_STATE_length));
965 } else {
966 ice->state.need_border_colors = true;
967
968 /* Stream out the border color and merge the pointer. */
969 uint32_t offset =
970 iris_upload_border_color(ice, &state->base.border_color);
971
972 uint32_t dynamic[GENX(SAMPLER_STATE_length)];
973 iris_pack_state(GENX(SAMPLER_STATE), dynamic, dyns) {
974 dyns.BorderColorPointer = offset;
975 }
976
977 for (uint32_t j = 0; j < GENX(SAMPLER_STATE_length); j++)
978 ((uint32_t *) map)[j] = state->sampler_state[j] | dynamic[j];
979 }
980
981 map += GENX(SAMPLER_STATE_length);
982 }
983
984 ice->state.dirty |= IRIS_DIRTY_SAMPLER_STATES_VS << stage;
985 }
986
987 struct iris_sampler_view {
988 struct pipe_sampler_view pipe;
989 struct isl_view view;
990
991 /** The resource (BO) holding our SURFACE_STATE. */
992 struct iris_state_ref surface_state;
993 };
994
995 /**
996 * Convert an swizzle enumeration (i.e. PIPE_SWIZZLE_X) to one of the Gen7.5+
997 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
998 *
999 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
1000 * 0 1 2 3 4 5
1001 * 4 5 6 7 0 1
1002 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
1003 *
1004 * which is simply adding 4 then modding by 8 (or anding with 7).
1005 *
1006 * We then may need to apply workarounds for textureGather hardware bugs.
1007 */
1008 static enum isl_channel_select
1009 pipe_swizzle_to_isl_channel(enum pipe_swizzle swizzle)
1010 {
1011 return (swizzle + 4) & 7;
1012 }
1013
1014 static struct pipe_sampler_view *
1015 iris_create_sampler_view(struct pipe_context *ctx,
1016 struct pipe_resource *tex,
1017 const struct pipe_sampler_view *tmpl)
1018 {
1019 struct iris_context *ice = (struct iris_context *) ctx;
1020 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1021 struct iris_resource *itex = (struct iris_resource *) tex;
1022 struct iris_sampler_view *isv = calloc(1, sizeof(struct iris_sampler_view));
1023
1024 if (!isv)
1025 return NULL;
1026
1027 /* initialize base object */
1028 isv->pipe = *tmpl;
1029 isv->pipe.context = ctx;
1030 isv->pipe.texture = NULL;
1031 pipe_reference_init(&isv->pipe.reference, 1);
1032 pipe_resource_reference(&isv->pipe.texture, tex);
1033
1034 void *map = upload_state(ice->state.surface_uploader, &isv->surface_state,
1035 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1036 if (!unlikely(map))
1037 return NULL;
1038
1039 struct iris_bo *state_bo = iris_resource_bo(isv->surface_state.res);
1040 isv->surface_state.offset += iris_bo_offset_from_base_address(state_bo);
1041
1042 /* XXX: do we need brw_get_texture_swizzle hacks here? */
1043 isv->view = (struct isl_view) {
1044 .format = iris_isl_format_for_pipe_format(tmpl->format),
1045 .swizzle = (struct isl_swizzle) {
1046 .r = pipe_swizzle_to_isl_channel(tmpl->swizzle_r),
1047 .g = pipe_swizzle_to_isl_channel(tmpl->swizzle_g),
1048 .b = pipe_swizzle_to_isl_channel(tmpl->swizzle_b),
1049 .a = pipe_swizzle_to_isl_channel(tmpl->swizzle_a),
1050 },
1051 .usage = ISL_SURF_USAGE_TEXTURE_BIT |
1052 (itex->surf.usage & ISL_SURF_USAGE_CUBE_BIT),
1053 };
1054
1055 if (tmpl->target != PIPE_BUFFER) {
1056 isv->view.base_level = tmpl->u.tex.first_level;
1057 isv->view.levels = tmpl->u.tex.last_level - tmpl->u.tex.first_level + 1;
1058 isv->view.base_array_layer = tmpl->u.tex.first_layer;
1059 isv->view.array_len =
1060 tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1;
1061
1062 isl_surf_fill_state(&screen->isl_dev, map,
1063 .surf = &itex->surf, .view = &isv->view,
1064 .mocs = MOCS_WB,
1065 .address = itex->bo->gtt_offset);
1066 // .aux_surf =
1067 // .clear_color = clear_color,
1068 } else {
1069 // XXX: what to do about isv->view? other drivers don't use it for bufs
1070 const struct isl_format_layout *fmtl =
1071 isl_format_get_layout(isv->view.format);
1072 const unsigned cpp = fmtl->bpb / 8;
1073
1074 isl_buffer_fill_state(&screen->isl_dev, map,
1075 .address = itex->bo->gtt_offset +
1076 tmpl->u.buf.offset,
1077 // XXX: buffer_texture_range_size from i965?
1078 .size_B = tmpl->u.buf.size,
1079 .format = isv->view.format,
1080 .stride_B = cpp,
1081 .mocs = MOCS_WB);
1082 }
1083
1084 return &isv->pipe;
1085 }
1086
1087 static struct pipe_surface *
1088 iris_create_surface(struct pipe_context *ctx,
1089 struct pipe_resource *tex,
1090 const struct pipe_surface *tmpl)
1091 {
1092 struct iris_context *ice = (struct iris_context *) ctx;
1093 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1094 const struct gen_device_info *devinfo = &screen->devinfo;
1095 struct iris_surface *surf = calloc(1, sizeof(struct iris_surface));
1096 struct pipe_surface *psurf = &surf->pipe;
1097 struct iris_resource *res = (struct iris_resource *) tex;
1098
1099 if (!surf)
1100 return NULL;
1101
1102 pipe_reference_init(&psurf->reference, 1);
1103 pipe_resource_reference(&psurf->texture, tex);
1104 psurf->context = ctx;
1105 psurf->format = tmpl->format;
1106 psurf->width = tex->width0;
1107 psurf->height = tex->height0;
1108 psurf->texture = tex;
1109 psurf->u.tex.first_layer = tmpl->u.tex.first_layer;
1110 psurf->u.tex.last_layer = tmpl->u.tex.last_layer;
1111 psurf->u.tex.level = tmpl->u.tex.level;
1112
1113 enum isl_format isl_format = iris_isl_format_for_pipe_format(psurf->format);
1114
1115 unsigned usage = 0;
1116 if (tmpl->writable)
1117 usage = ISL_SURF_USAGE_STORAGE_BIT;
1118 else if (util_format_is_depth_or_stencil(tmpl->format))
1119 usage = ISL_SURF_USAGE_DEPTH_BIT;
1120 else {
1121 usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
1122
1123 if (!isl_format_supports_rendering(devinfo, isl_format)) {
1124 /* Framebuffer validation will reject this invalid case, but it
1125 * hasn't had the opportunity yet. In the meantime, we need to
1126 * avoid hitting ISL asserts about unsupported formats below.
1127 */
1128 free(surf);
1129 return NULL;
1130 }
1131 }
1132
1133 surf->view = (struct isl_view) {
1134 .format = isl_format,
1135 .base_level = tmpl->u.tex.level,
1136 .levels = 1,
1137 .base_array_layer = tmpl->u.tex.first_layer,
1138 .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
1139 .swizzle = ISL_SWIZZLE_IDENTITY,
1140 .usage = usage,
1141 };
1142
1143 /* Bail early for depth/stencil */
1144 if (res->surf.usage & (ISL_SURF_USAGE_DEPTH_BIT |
1145 ISL_SURF_USAGE_STENCIL_BIT))
1146 return psurf;
1147
1148
1149 void *map = upload_state(ice->state.surface_uploader, &surf->surface_state,
1150 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1151 if (!unlikely(map))
1152 return NULL;
1153
1154 struct iris_bo *state_bo = iris_resource_bo(surf->surface_state.res);
1155 surf->surface_state.offset += iris_bo_offset_from_base_address(state_bo);
1156
1157 isl_surf_fill_state(&screen->isl_dev, map,
1158 .surf = &res->surf, .view = &surf->view,
1159 .mocs = MOCS_WB,
1160 .address = res->bo->gtt_offset);
1161 // .aux_surf =
1162 // .clear_color = clear_color,
1163
1164 return psurf;
1165 }
1166
1167 static void
1168 iris_set_sampler_views(struct pipe_context *ctx,
1169 enum pipe_shader_type p_stage,
1170 unsigned start, unsigned count,
1171 struct pipe_sampler_view **views)
1172 {
1173 struct iris_context *ice = (struct iris_context *) ctx;
1174 gl_shader_stage stage = stage_from_pipe(p_stage);
1175
1176 unsigned i;
1177 for (i = 0; i < count; i++) {
1178 pipe_sampler_view_reference((struct pipe_sampler_view **)
1179 &ice->state.textures[stage][i], views[i]);
1180 }
1181 for (; i < ice->state.num_textures[stage]; i++) {
1182 pipe_sampler_view_reference((struct pipe_sampler_view **)
1183 &ice->state.textures[stage][i], NULL);
1184 }
1185
1186 ice->state.num_textures[stage] = count;
1187
1188 ice->state.dirty |= (IRIS_DIRTY_BINDINGS_VS << stage);
1189 }
1190
1191 static void
1192 iris_set_clip_state(struct pipe_context *ctx,
1193 const struct pipe_clip_state *state)
1194 {
1195 }
1196
1197 static void
1198 iris_set_polygon_stipple(struct pipe_context *ctx,
1199 const struct pipe_poly_stipple *state)
1200 {
1201 struct iris_context *ice = (struct iris_context *) ctx;
1202 memcpy(&ice->state.poly_stipple, state, sizeof(*state));
1203 ice->state.dirty |= IRIS_DIRTY_POLYGON_STIPPLE;
1204 }
1205
1206 static void
1207 iris_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
1208 {
1209 struct iris_context *ice = (struct iris_context *) ctx;
1210
1211 ice->state.sample_mask = sample_mask & 0xffff;
1212 ice->state.dirty |= IRIS_DIRTY_SAMPLE_MASK;
1213 }
1214
1215 static void
1216 iris_set_scissor_states(struct pipe_context *ctx,
1217 unsigned start_slot,
1218 unsigned num_scissors,
1219 const struct pipe_scissor_state *states)
1220 {
1221 struct iris_context *ice = (struct iris_context *) ctx;
1222
1223 for (unsigned i = 0; i < num_scissors; i++) {
1224 ice->state.scissors[start_slot + i] = states[i];
1225 }
1226
1227 ice->state.dirty |= IRIS_DIRTY_SCISSOR_RECT;
1228 }
1229
1230 static void
1231 iris_set_stencil_ref(struct pipe_context *ctx,
1232 const struct pipe_stencil_ref *state)
1233 {
1234 struct iris_context *ice = (struct iris_context *) ctx;
1235 memcpy(&ice->state.stencil_ref, state, sizeof(*state));
1236 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
1237 }
1238
1239 static float
1240 viewport_extent(const struct pipe_viewport_state *state, int axis, float sign)
1241 {
1242 return copysignf(state->scale[axis], sign) + state->translate[axis];
1243 }
1244
1245 #if 0
1246 static void
1247 calculate_guardband_size(uint32_t fb_width, uint32_t fb_height,
1248 float m00, float m11, float m30, float m31,
1249 float *xmin, float *xmax,
1250 float *ymin, float *ymax)
1251 {
1252 /* According to the "Vertex X,Y Clamping and Quantization" section of the
1253 * Strips and Fans documentation:
1254 *
1255 * "The vertex X and Y screen-space coordinates are also /clamped/ to the
1256 * fixed-point "guardband" range supported by the rasterization hardware"
1257 *
1258 * and
1259 *
1260 * "In almost all circumstances, if an object’s vertices are actually
1261 * modified by this clamping (i.e., had X or Y coordinates outside of
1262 * the guardband extent the rendered object will not match the intended
1263 * result. Therefore software should take steps to ensure that this does
1264 * not happen - e.g., by clipping objects such that they do not exceed
1265 * these limits after the Drawing Rectangle is applied."
1266 *
1267 * I believe the fundamental restriction is that the rasterizer (in
1268 * the SF/WM stages) have a limit on the number of pixels that can be
1269 * rasterized. We need to ensure any coordinates beyond the rasterizer
1270 * limit are handled by the clipper. So effectively that limit becomes
1271 * the clipper's guardband size.
1272 *
1273 * It goes on to say:
1274 *
1275 * "In addition, in order to be correctly rendered, objects must have a
1276 * screenspace bounding box not exceeding 8K in the X or Y direction.
1277 * This additional restriction must also be comprehended by software,
1278 * i.e., enforced by use of clipping."
1279 *
1280 * This makes no sense. Gen7+ hardware supports 16K render targets,
1281 * and you definitely need to be able to draw polygons that fill the
1282 * surface. Our assumption is that the rasterizer was limited to 8K
1283 * on Sandybridge, which only supports 8K surfaces, and it was actually
1284 * increased to 16K on Ivybridge and later.
1285 *
1286 * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
1287 */
1288 const float gb_size = GEN_GEN >= 7 ? 16384.0f : 8192.0f;
1289
1290 if (m00 != 0 && m11 != 0) {
1291 /* First, we compute the screen-space render area */
1292 const float ss_ra_xmin = MIN3( 0, m30 + m00, m30 - m00);
1293 const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
1294 const float ss_ra_ymin = MIN3( 0, m31 + m11, m31 - m11);
1295 const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
1296
1297 /* We want the guardband to be centered on that */
1298 const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
1299 const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
1300 const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
1301 const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
1302
1303 /* Now we need it in native device coordinates */
1304 const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
1305 const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
1306 const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
1307 const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
1308
1309 /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
1310 * flipped upside-down. X should be fine though.
1311 */
1312 assert(ndc_gb_xmin <= ndc_gb_xmax);
1313 *xmin = ndc_gb_xmin;
1314 *xmax = ndc_gb_xmax;
1315 *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
1316 *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
1317 } else {
1318 /* The viewport scales to 0, so nothing will be rendered. */
1319 *xmin = 0.0f;
1320 *xmax = 0.0f;
1321 *ymin = 0.0f;
1322 *ymax = 0.0f;
1323 }
1324 }
1325 #endif
1326
1327 static void
1328 iris_set_viewport_states(struct pipe_context *ctx,
1329 unsigned start_slot,
1330 unsigned count,
1331 const struct pipe_viewport_state *states)
1332 {
1333 struct iris_context *ice = (struct iris_context *) ctx;
1334 struct iris_genx_state *genx = ice->state.genx;
1335 uint32_t *vp_map = &genx->sf_cl_vp[start_slot];
1336
1337 for (unsigned i = 0; i < count; i++) {
1338 const struct pipe_viewport_state *state = &states[i];
1339
1340 memcpy(&ice->state.viewports[start_slot + i], state, sizeof(*state));
1341
1342 iris_pack_state(GENX(SF_CLIP_VIEWPORT), vp_map, vp) {
1343 vp.ViewportMatrixElementm00 = state->scale[0];
1344 vp.ViewportMatrixElementm11 = state->scale[1];
1345 vp.ViewportMatrixElementm22 = state->scale[2];
1346 vp.ViewportMatrixElementm30 = state->translate[0];
1347 vp.ViewportMatrixElementm31 = state->translate[1];
1348 vp.ViewportMatrixElementm32 = state->translate[2];
1349 /* XXX: in i965 this is computed based on the drawbuffer size,
1350 * but we don't have that here...
1351 */
1352 vp.XMinClipGuardband = -1.0;
1353 vp.XMaxClipGuardband = 1.0;
1354 vp.YMinClipGuardband = -1.0;
1355 vp.YMaxClipGuardband = 1.0;
1356 vp.XMinViewPort = viewport_extent(state, 0, -1.0f);
1357 vp.XMaxViewPort = viewport_extent(state, 0, 1.0f) - 1;
1358 vp.YMinViewPort = viewport_extent(state, 1, -1.0f);
1359 vp.YMaxViewPort = viewport_extent(state, 1, 1.0f) - 1;
1360 }
1361
1362 vp_map += GENX(SF_CLIP_VIEWPORT_length);
1363 }
1364
1365 ice->state.dirty |= IRIS_DIRTY_SF_CL_VIEWPORT;
1366
1367 if (ice->state.cso_rast && (!ice->state.cso_rast->depth_clip_near ||
1368 !ice->state.cso_rast->depth_clip_far))
1369 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
1370 }
1371
1372 static void
1373 iris_set_framebuffer_state(struct pipe_context *ctx,
1374 const struct pipe_framebuffer_state *state)
1375 {
1376 struct iris_context *ice = (struct iris_context *) ctx;
1377 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1378 struct isl_device *isl_dev = &screen->isl_dev;
1379 struct pipe_framebuffer_state *cso = &ice->state.framebuffer;
1380
1381 unsigned samples = util_framebuffer_get_num_samples(state);
1382
1383 if (cso->samples != samples) {
1384 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
1385 }
1386
1387 if (cso->nr_cbufs != state->nr_cbufs) {
1388 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
1389 }
1390
1391 if ((cso->layers == 0) != (state->layers == 0)) {
1392 ice->state.dirty |= IRIS_DIRTY_CLIP;
1393 }
1394
1395 util_copy_framebuffer_state(cso, state);
1396 cso->samples = samples;
1397
1398 struct iris_depth_buffer_state *cso_z = &ice->state.genx->depth_buffer;
1399
1400 struct isl_view view = {
1401 .base_level = 0,
1402 .levels = 1,
1403 .base_array_layer = 0,
1404 .array_len = 1,
1405 .swizzle = ISL_SWIZZLE_IDENTITY,
1406 };
1407
1408 struct isl_depth_stencil_hiz_emit_info info = {
1409 .view = &view,
1410 .mocs = MOCS_WB,
1411 };
1412
1413 struct iris_resource *zres =
1414 (void *) (cso->zsbuf ? cso->zsbuf->texture : NULL);
1415
1416 if (zres) {
1417 view.usage |= ISL_SURF_USAGE_DEPTH_BIT;
1418
1419 info.depth_surf = &zres->surf;
1420 info.depth_address = zres->bo->gtt_offset;
1421
1422 view.format = zres->surf.format;
1423
1424 view.base_level = cso->zsbuf->u.tex.level;
1425 view.base_array_layer = cso->zsbuf->u.tex.first_layer;
1426 view.array_len =
1427 cso->zsbuf->u.tex.last_layer - cso->zsbuf->u.tex.first_layer + 1;
1428
1429 info.hiz_usage = ISL_AUX_USAGE_NONE;
1430 }
1431
1432 #if 0
1433 if (stencil_mt) {
1434 view.usage |= ISL_SURF_USAGE_STENCIL_BIT;
1435 info.stencil_surf = &stencil_mt->surf;
1436
1437 if (!depth_mt) {
1438 view.base_level = stencil_irb->mt_level - stencil_irb->mt->first_level;
1439 view.base_array_layer = stencil_irb->mt_layer;
1440 view.array_len = MAX2(stencil_irb->layer_count, 1);
1441 view.format = stencil_mt->surf.format;
1442 }
1443
1444 uint32_t stencil_offset = 0;
1445 info.stencil_address = stencil_mt->bo->gtt_offset + stencil_mt->offset;
1446 }
1447 #endif
1448
1449 isl_emit_depth_stencil_hiz_s(isl_dev, cso_z->packets, &info);
1450
1451 /* Make a null surface for unbound buffers */
1452 void *null_surf_map =
1453 upload_state(ice->state.surface_uploader, &ice->state.null_fb,
1454 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1455 isl_null_fill_state(&screen->isl_dev, null_surf_map, isl_extent3d(cso->width, cso->height, cso->layers ? cso->layers : 1));
1456
1457 ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
1458
1459 /* Render target change */
1460 ice->state.dirty |= IRIS_DIRTY_BINDINGS_FS;
1461
1462 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_FRAMEBUFFER];
1463 }
1464
1465 static void
1466 iris_set_constant_buffer(struct pipe_context *ctx,
1467 enum pipe_shader_type p_stage, unsigned index,
1468 const struct pipe_constant_buffer *input)
1469 {
1470 struct iris_context *ice = (struct iris_context *) ctx;
1471 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1472 gl_shader_stage stage = stage_from_pipe(p_stage);
1473 struct iris_shader_state *shs = &ice->shaders.state[stage];
1474 struct iris_const_buffer *cbuf = &shs->constbuf[index];
1475
1476 if (input && (input->buffer || input->user_buffer)) {
1477 if (input->user_buffer) {
1478 u_upload_data(ctx->const_uploader, 0, input->buffer_size, 32,
1479 input->user_buffer, &cbuf->data.offset,
1480 &cbuf->data.res);
1481 } else {
1482 pipe_resource_reference(&cbuf->data.res, input->buffer);
1483 }
1484
1485 // XXX: these are not retained forever, use a separate uploader?
1486 void *map =
1487 upload_state(ice->state.surface_uploader, &cbuf->surface_state,
1488 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1489 if (!unlikely(map)) {
1490 pipe_resource_reference(&cbuf->data.res, NULL);
1491 return;
1492 }
1493
1494 struct iris_resource *res = (void *) cbuf->data.res;
1495 struct iris_bo *surf_bo = iris_resource_bo(cbuf->surface_state.res);
1496 cbuf->surface_state.offset += iris_bo_offset_from_base_address(surf_bo);
1497
1498 isl_buffer_fill_state(&screen->isl_dev, map,
1499 .address = res->bo->gtt_offset + cbuf->data.offset,
1500 .size_B = input->buffer_size,
1501 .format = ISL_FORMAT_R32G32B32A32_FLOAT,
1502 .stride_B = 1,
1503 .mocs = MOCS_WB)
1504 } else {
1505 pipe_resource_reference(&cbuf->data.res, NULL);
1506 pipe_resource_reference(&cbuf->surface_state.res, NULL);
1507 }
1508
1509 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_VS << stage;
1510 // XXX: maybe not necessary all the time...?
1511 // XXX: we need 3DS_BTP to commit these changes, and if we fell back to
1512 // XXX: pull model we may need actual new bindings...
1513 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
1514 }
1515
1516 static void
1517 iris_set_shader_buffers(struct pipe_context *ctx,
1518 enum pipe_shader_type p_stage,
1519 unsigned start_slot, unsigned count,
1520 const struct pipe_shader_buffer *buffers)
1521 {
1522 struct iris_context *ice = (struct iris_context *) ctx;
1523 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1524 gl_shader_stage stage = stage_from_pipe(p_stage);
1525 struct iris_shader_state *shs = &ice->shaders.state[stage];
1526
1527 for (unsigned i = 0; i < count; i++) {
1528 if (buffers && buffers[i].buffer) {
1529 const struct pipe_shader_buffer *buffer = &buffers[i];
1530 struct iris_resource *res = (void *) buffer->buffer;
1531 pipe_resource_reference(&shs->ssbo[start_slot + i], &res->base);
1532
1533 // XXX: these are not retained forever, use a separate uploader?
1534 void *map =
1535 upload_state(ice->state.surface_uploader,
1536 &shs->ssbo_surface_state[start_slot + i],
1537 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1538 if (!unlikely(map)) {
1539 pipe_resource_reference(&shs->ssbo[start_slot + i], NULL);
1540 return;
1541 }
1542
1543 struct iris_bo *surf_state_bo =
1544 iris_resource_bo(shs->ssbo_surface_state[start_slot + i].res);
1545 shs->ssbo_surface_state[start_slot + i].offset +=
1546 iris_bo_offset_from_base_address(surf_state_bo);
1547
1548 isl_buffer_fill_state(&screen->isl_dev, map,
1549 .address =
1550 res->bo->gtt_offset + buffer->buffer_offset,
1551 .size_B = buffer->buffer_size,
1552 .format = ISL_FORMAT_RAW,
1553 .stride_B = 1,
1554 .mocs = MOCS_WB);
1555 } else {
1556 pipe_resource_reference(&shs->ssbo[start_slot + i], NULL);
1557 pipe_resource_reference(&shs->ssbo_surface_state[start_slot + i].res,
1558 NULL);
1559 }
1560 }
1561
1562 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
1563 }
1564
1565 static void
1566 iris_sampler_view_destroy(struct pipe_context *ctx,
1567 struct pipe_sampler_view *state)
1568 {
1569 struct iris_sampler_view *isv = (void *) state;
1570 pipe_resource_reference(&state->texture, NULL);
1571 pipe_resource_reference(&isv->surface_state.res, NULL);
1572 free(isv);
1573 }
1574
1575
1576 static void
1577 iris_surface_destroy(struct pipe_context *ctx, struct pipe_surface *p_surf)
1578 {
1579 struct iris_surface *surf = (void *) p_surf;
1580 pipe_resource_reference(&p_surf->texture, NULL);
1581 pipe_resource_reference(&surf->surface_state.res, NULL);
1582 free(surf);
1583 }
1584
1585 static void
1586 iris_delete_state(struct pipe_context *ctx, void *state)
1587 {
1588 free(state);
1589 }
1590
1591 static void
1592 iris_free_vertex_buffers(struct iris_vertex_buffer_state *cso)
1593 {
1594 for (unsigned i = 0; i < cso->num_buffers; i++)
1595 pipe_resource_reference(&cso->resources[i], NULL);
1596 }
1597
1598 static void
1599 iris_set_vertex_buffers(struct pipe_context *ctx,
1600 unsigned start_slot, unsigned count,
1601 const struct pipe_vertex_buffer *buffers)
1602 {
1603 struct iris_context *ice = (struct iris_context *) ctx;
1604 struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
1605
1606 iris_free_vertex_buffers(&ice->state.genx->vertex_buffers);
1607
1608 if (!buffers)
1609 count = 0;
1610
1611 cso->num_buffers = count;
1612
1613 iris_pack_command(GENX(3DSTATE_VERTEX_BUFFERS), cso->vertex_buffers, vb) {
1614 vb.DWordLength = 4 * MAX2(cso->num_buffers, 1) - 1;
1615 }
1616
1617 uint32_t *vb_pack_dest = &cso->vertex_buffers[1];
1618
1619 if (count == 0) {
1620 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
1621 vb.VertexBufferIndex = start_slot;
1622 vb.NullVertexBuffer = true;
1623 vb.AddressModifyEnable = true;
1624 }
1625 }
1626
1627 for (unsigned i = 0; i < count; i++) {
1628 assert(!buffers[i].is_user_buffer);
1629
1630 pipe_resource_reference(&cso->resources[i], buffers[i].buffer.resource);
1631 struct iris_resource *res = (void *) cso->resources[i];
1632
1633 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
1634 vb.VertexBufferIndex = start_slot + i;
1635 vb.MOCS = MOCS_WB;
1636 vb.AddressModifyEnable = true;
1637 vb.BufferPitch = buffers[i].stride;
1638 vb.BufferSize = res->bo->size;
1639 vb.BufferStartingAddress =
1640 ro_bo(NULL, res->bo->gtt_offset + buffers[i].buffer_offset);
1641 }
1642
1643 vb_pack_dest += GENX(VERTEX_BUFFER_STATE_length);
1644 }
1645
1646 ice->state.dirty |= IRIS_DIRTY_VERTEX_BUFFERS;
1647 }
1648
1649 struct iris_vertex_element_state {
1650 uint32_t vertex_elements[1 + 33 * GENX(VERTEX_ELEMENT_STATE_length)];
1651 uint32_t vf_instancing[33 * GENX(3DSTATE_VF_INSTANCING_length)];
1652 unsigned count;
1653 };
1654
1655 static void *
1656 iris_create_vertex_elements(struct pipe_context *ctx,
1657 unsigned count,
1658 const struct pipe_vertex_element *state)
1659 {
1660 struct iris_vertex_element_state *cso =
1661 malloc(sizeof(struct iris_vertex_element_state));
1662
1663 cso->count = count;
1664
1665 /* TODO:
1666 * - create edge flag one
1667 * - create SGV ones
1668 * - if those are necessary, use count + 1/2/3... OR in the length
1669 */
1670 iris_pack_command(GENX(3DSTATE_VERTEX_ELEMENTS), cso->vertex_elements, ve) {
1671 ve.DWordLength =
1672 1 + GENX(VERTEX_ELEMENT_STATE_length) * MAX2(count, 1) - 2;
1673 }
1674
1675 uint32_t *ve_pack_dest = &cso->vertex_elements[1];
1676 uint32_t *vfi_pack_dest = cso->vf_instancing;
1677
1678 if (count == 0) {
1679 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
1680 ve.Valid = true;
1681 ve.SourceElementFormat = ISL_FORMAT_R32G32B32A32_FLOAT;
1682 ve.Component0Control = VFCOMP_STORE_0;
1683 ve.Component1Control = VFCOMP_STORE_0;
1684 ve.Component2Control = VFCOMP_STORE_0;
1685 ve.Component3Control = VFCOMP_STORE_1_FP;
1686 }
1687
1688 iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
1689 }
1690 }
1691
1692 for (int i = 0; i < count; i++) {
1693 enum isl_format isl_format =
1694 iris_isl_format_for_pipe_format(state[i].src_format);
1695 unsigned comp[4] = { VFCOMP_STORE_SRC, VFCOMP_STORE_SRC,
1696 VFCOMP_STORE_SRC, VFCOMP_STORE_SRC };
1697
1698 switch (isl_format_get_num_channels(isl_format)) {
1699 case 0: comp[0] = VFCOMP_STORE_0;
1700 case 1: comp[1] = VFCOMP_STORE_0;
1701 case 2: comp[2] = VFCOMP_STORE_0;
1702 case 3:
1703 comp[3] = isl_format_has_int_channel(isl_format) ? VFCOMP_STORE_1_INT
1704 : VFCOMP_STORE_1_FP;
1705 break;
1706 }
1707 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
1708 ve.VertexBufferIndex = state[i].vertex_buffer_index;
1709 ve.Valid = true;
1710 ve.SourceElementOffset = state[i].src_offset;
1711 ve.SourceElementFormat = isl_format;
1712 ve.Component0Control = comp[0];
1713 ve.Component1Control = comp[1];
1714 ve.Component2Control = comp[2];
1715 ve.Component3Control = comp[3];
1716 }
1717
1718 iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
1719 vi.VertexElementIndex = i;
1720 vi.InstancingEnable = state[i].instance_divisor > 0;
1721 vi.InstanceDataStepRate = state[i].instance_divisor;
1722 }
1723
1724 ve_pack_dest += GENX(VERTEX_ELEMENT_STATE_length);
1725 vfi_pack_dest += GENX(3DSTATE_VF_INSTANCING_length);
1726 }
1727
1728 return cso;
1729 }
1730
1731 static void
1732 iris_bind_vertex_elements_state(struct pipe_context *ctx, void *state)
1733 {
1734 struct iris_context *ice = (struct iris_context *) ctx;
1735 struct iris_vertex_element_state *old_cso = ice->state.cso_vertex_elements;
1736 struct iris_vertex_element_state *new_cso = state;
1737
1738 if (new_cso && cso_changed(count))
1739 ice->state.dirty |= IRIS_DIRTY_VF_SGVS;
1740
1741 ice->state.cso_vertex_elements = state;
1742 ice->state.dirty |= IRIS_DIRTY_VERTEX_ELEMENTS;
1743 }
1744
1745 static void *
1746 iris_create_compute_state(struct pipe_context *ctx,
1747 const struct pipe_compute_state *state)
1748 {
1749 return malloc(1);
1750 }
1751
1752 struct iris_stream_output_target {
1753 struct pipe_stream_output_target base;
1754
1755 uint32_t so_buffer[GENX(3DSTATE_SO_BUFFER_length)];
1756
1757 struct iris_state_ref offset;
1758 };
1759
1760 static struct pipe_stream_output_target *
1761 iris_create_stream_output_target(struct pipe_context *ctx,
1762 struct pipe_resource *res,
1763 unsigned buffer_offset,
1764 unsigned buffer_size)
1765 {
1766 struct iris_stream_output_target *cso = calloc(1, sizeof(*cso));
1767 if (!cso)
1768 return NULL;
1769
1770 pipe_reference_init(&cso->base.reference, 1);
1771 pipe_resource_reference(&cso->base.buffer, res);
1772 cso->base.buffer_offset = buffer_offset;
1773 cso->base.buffer_size = buffer_size;
1774 cso->base.context = ctx;
1775
1776 upload_state(ctx->stream_uploader, &cso->offset, 4, 4);
1777
1778 iris_pack_command(GENX(3DSTATE_SO_BUFFER), cso->so_buffer, sob) {
1779 sob.SurfaceBaseAddress =
1780 rw_bo(NULL, iris_resource_bo(res)->gtt_offset + buffer_offset);
1781 sob.SOBufferEnable = true;
1782 sob.StreamOffsetWriteEnable = true;
1783 sob.StreamOutputBufferOffsetAddressEnable = true;
1784 sob.MOCS = MOCS_WB; // XXX: MOCS
1785
1786 sob.SurfaceSize = MAX2(buffer_size / 4, 1) - 1;
1787 sob.StreamOutputBufferOffsetAddress =
1788 rw_bo(NULL, iris_resource_bo(cso->offset.res)->gtt_offset + cso->offset.offset);
1789
1790 /* .SOBufferIndex and .StreamOffset are filled in later */
1791 }
1792
1793 return &cso->base;
1794 }
1795
1796 static void
1797 iris_stream_output_target_destroy(struct pipe_context *ctx,
1798 struct pipe_stream_output_target *state)
1799 {
1800 struct iris_stream_output_target *cso = (void *) state;
1801
1802 pipe_resource_reference(&cso->base.buffer, NULL);
1803 pipe_resource_reference(&cso->offset.res, NULL);
1804
1805 free(cso);
1806 }
1807
1808 static void
1809 iris_set_stream_output_targets(struct pipe_context *ctx,
1810 unsigned num_targets,
1811 struct pipe_stream_output_target **targets,
1812 const unsigned *offsets)
1813 {
1814 struct iris_context *ice = (struct iris_context *) ctx;
1815 struct iris_genx_state *genx = ice->state.genx;
1816 uint32_t *so_buffers = genx->so_buffers;
1817
1818 const bool active = num_targets > 0;
1819 if (ice->state.streamout_active != active) {
1820 ice->state.streamout_active = active;
1821 ice->state.dirty |= IRIS_DIRTY_STREAMOUT;
1822 }
1823
1824 for (int i = 0; i < 4; i++) {
1825 pipe_so_target_reference(&ice->state.so_target[i],
1826 i < num_targets ? targets[i] : NULL);
1827 }
1828
1829 /* No need to update 3DSTATE_SO_BUFFER unless SOL is active. */
1830 if (!active)
1831 return;
1832
1833 for (unsigned i = 0; i < 4; i++,
1834 so_buffers += GENX(3DSTATE_SO_BUFFER_length)) {
1835
1836 if (i >= num_targets || !targets[i]) {
1837 iris_pack_command(GENX(3DSTATE_SO_BUFFER), so_buffers, sob)
1838 sob.SOBufferIndex = i;
1839 continue;
1840 }
1841
1842 /* Note that offsets[i] will either be 0, causing us to zero
1843 * the value in the buffer, or 0xFFFFFFFF, which happens to mean
1844 * "continue appending at the existing offset."
1845 */
1846 assert(offsets[i] == 0 || offsets[i] == 0xFFFFFFFF);
1847
1848 uint32_t dynamic[GENX(3DSTATE_SO_BUFFER_length)];
1849 iris_pack_state(GENX(3DSTATE_SO_BUFFER), dynamic, dyns) {
1850 dyns.SOBufferIndex = i;
1851 dyns.StreamOffset = offsets[i];
1852 }
1853
1854 struct iris_stream_output_target *tgt = (void *) targets[i];
1855 for (uint32_t j = 0; j < GENX(3DSTATE_SO_BUFFER_length); j++) {
1856 so_buffers[j] = tgt->so_buffer[j] | dynamic[j];
1857 }
1858 }
1859
1860 ice->state.dirty |= IRIS_DIRTY_SO_BUFFERS;
1861 }
1862
1863 static uint32_t *
1864 iris_create_so_decl_list(const struct pipe_stream_output_info *info,
1865 const struct brw_vue_map *vue_map)
1866 {
1867 struct GENX(SO_DECL) so_decl[MAX_VERTEX_STREAMS][128];
1868 int buffer_mask[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
1869 int next_offset[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
1870 int decls[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
1871 int max_decls = 0;
1872 STATIC_ASSERT(ARRAY_SIZE(so_decl[0]) >= MAX_PROGRAM_OUTPUTS);
1873
1874 memset(so_decl, 0, sizeof(so_decl));
1875
1876 /* Construct the list of SO_DECLs to be emitted. The formatting of the
1877 * command feels strange -- each dword pair contains a SO_DECL per stream.
1878 */
1879 for (unsigned i = 0; i < info->num_outputs; i++) {
1880 const struct pipe_stream_output *output = &info->output[i];
1881 const int buffer = output->output_buffer;
1882 const int varying = output->register_index;
1883 const unsigned stream_id = output->stream;
1884 assert(stream_id < MAX_VERTEX_STREAMS);
1885
1886 buffer_mask[stream_id] |= 1 << buffer;
1887
1888 assert(vue_map->varying_to_slot[varying] >= 0);
1889
1890 /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
1891 * array. Instead, it simply increments DstOffset for the following
1892 * input by the number of components that should be skipped.
1893 *
1894 * Our hardware is unusual in that it requires us to program SO_DECLs
1895 * for fake "hole" components, rather than simply taking the offset
1896 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
1897 * program as many size = 4 holes as we can, then a final hole to
1898 * accommodate the final 1, 2, or 3 remaining.
1899 */
1900 int skip_components = output->dst_offset - next_offset[buffer];
1901
1902 while (skip_components > 0) {
1903 so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
1904 .HoleFlag = 1,
1905 .OutputBufferSlot = output->output_buffer,
1906 .ComponentMask = (1 << MIN2(skip_components, 4)) - 1,
1907 };
1908 skip_components -= 4;
1909 }
1910
1911 next_offset[buffer] = output->dst_offset + output->num_components;
1912
1913 so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
1914 .OutputBufferSlot = output->output_buffer,
1915 .RegisterIndex = vue_map->varying_to_slot[varying],
1916 .ComponentMask =
1917 ((1 << output->num_components) - 1) << output->start_component,
1918 };
1919
1920 if (decls[stream_id] > max_decls)
1921 max_decls = decls[stream_id];
1922 }
1923
1924 unsigned dwords = GENX(3DSTATE_STREAMOUT_length) + (3 + 2 * max_decls);
1925 uint32_t *map = ralloc_size(NULL, sizeof(uint32_t) * dwords);
1926 uint32_t *so_decl_map = map + GENX(3DSTATE_STREAMOUT_length);
1927
1928 iris_pack_command(GENX(3DSTATE_STREAMOUT), map, sol) {
1929 int urb_entry_read_offset = 0;
1930 int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
1931 urb_entry_read_offset;
1932
1933 /* We always read the whole vertex. This could be reduced at some
1934 * point by reading less and offsetting the register index in the
1935 * SO_DECLs.
1936 */
1937 sol.Stream0VertexReadOffset = urb_entry_read_offset;
1938 sol.Stream0VertexReadLength = urb_entry_read_length - 1;
1939 sol.Stream1VertexReadOffset = urb_entry_read_offset;
1940 sol.Stream1VertexReadLength = urb_entry_read_length - 1;
1941 sol.Stream2VertexReadOffset = urb_entry_read_offset;
1942 sol.Stream2VertexReadLength = urb_entry_read_length - 1;
1943 sol.Stream3VertexReadOffset = urb_entry_read_offset;
1944 sol.Stream3VertexReadLength = urb_entry_read_length - 1;
1945
1946 /* Set buffer pitches; 0 means unbound. */
1947 sol.Buffer0SurfacePitch = 4 * info->stride[0];
1948 sol.Buffer1SurfacePitch = 4 * info->stride[1];
1949 sol.Buffer2SurfacePitch = 4 * info->stride[2];
1950 sol.Buffer3SurfacePitch = 4 * info->stride[3];
1951 }
1952
1953 iris_pack_command(GENX(3DSTATE_SO_DECL_LIST), so_decl_map, list) {
1954 list.DWordLength = 3 + 2 * max_decls - 2;
1955 list.StreamtoBufferSelects0 = buffer_mask[0];
1956 list.StreamtoBufferSelects1 = buffer_mask[1];
1957 list.StreamtoBufferSelects2 = buffer_mask[2];
1958 list.StreamtoBufferSelects3 = buffer_mask[3];
1959 list.NumEntries0 = decls[0];
1960 list.NumEntries1 = decls[1];
1961 list.NumEntries2 = decls[2];
1962 list.NumEntries3 = decls[3];
1963 }
1964
1965 for (int i = 0; i < max_decls; i++) {
1966 iris_pack_state(GENX(SO_DECL_ENTRY), so_decl_map + 3 + i * 2, entry) {
1967 entry.Stream0Decl = so_decl[0][i];
1968 entry.Stream1Decl = so_decl[1][i];
1969 entry.Stream2Decl = so_decl[2][i];
1970 entry.Stream3Decl = so_decl[3][i];
1971 }
1972 }
1973
1974 return map;
1975 }
1976
1977 static void
1978 iris_compute_sbe_urb_read_interval(uint64_t fs_input_slots,
1979 const struct brw_vue_map *last_vue_map,
1980 bool two_sided_color,
1981 unsigned *out_offset,
1982 unsigned *out_length)
1983 {
1984 /* The compiler computes the first URB slot without considering COL/BFC
1985 * swizzling (because it doesn't know whether it's enabled), so we need
1986 * to do that here too. This may result in a smaller offset, which
1987 * should be safe.
1988 */
1989 const unsigned first_slot =
1990 brw_compute_first_urb_slot_required(fs_input_slots, last_vue_map);
1991
1992 /* This becomes the URB read offset (counted in pairs of slots). */
1993 assert(first_slot % 2 == 0);
1994 *out_offset = first_slot / 2;
1995
1996 /* We need to adjust the inputs read to account for front/back color
1997 * swizzling, as it can make the URB length longer.
1998 */
1999 for (int c = 0; c <= 1; c++) {
2000 if (fs_input_slots & (VARYING_BIT_COL0 << c)) {
2001 /* If two sided color is enabled, the fragment shader's gl_Color
2002 * (COL0) input comes from either the gl_FrontColor (COL0) or
2003 * gl_BackColor (BFC0) input varyings. Mark BFC as used, too.
2004 */
2005 if (two_sided_color)
2006 fs_input_slots |= (VARYING_BIT_BFC0 << c);
2007
2008 /* If front color isn't written, we opt to give them back color
2009 * instead of an undefined value. Switch from COL to BFC.
2010 */
2011 if (last_vue_map->varying_to_slot[VARYING_SLOT_COL0 + c] == -1) {
2012 fs_input_slots &= ~(VARYING_BIT_COL0 << c);
2013 fs_input_slots |= (VARYING_BIT_BFC0 << c);
2014 }
2015 }
2016 }
2017
2018 /* Compute the minimum URB Read Length necessary for the FS inputs.
2019 *
2020 * From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
2021 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
2022 *
2023 * "This field should be set to the minimum length required to read the
2024 * maximum source attribute. The maximum source attribute is indicated
2025 * by the maximum value of the enabled Attribute # Source Attribute if
2026 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
2027 * enable is not set.
2028 * read_length = ceiling((max_source_attr + 1) / 2)
2029 *
2030 * [errata] Corruption/Hang possible if length programmed larger than
2031 * recommended"
2032 *
2033 * Similar text exists for Ivy Bridge.
2034 *
2035 * We find the last URB slot that's actually read by the FS.
2036 */
2037 unsigned last_read_slot = last_vue_map->num_slots - 1;
2038 while (last_read_slot > first_slot && !(fs_input_slots &
2039 (1ull << last_vue_map->slot_to_varying[last_read_slot])))
2040 --last_read_slot;
2041
2042 /* The URB read length is the difference of the two, counted in pairs. */
2043 *out_length = DIV_ROUND_UP(last_read_slot - first_slot + 1, 2);
2044 }
2045
2046 static void
2047 iris_emit_sbe_swiz(struct iris_batch *batch,
2048 const struct iris_context *ice,
2049 unsigned urb_read_offset,
2050 unsigned sprite_coord_enables)
2051 {
2052 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) attr_overrides[16] = {};
2053 const struct brw_wm_prog_data *wm_prog_data = (void *)
2054 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2055 const struct brw_vue_map *vue_map = ice->shaders.last_vue_map;
2056 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2057
2058 /* XXX: this should be generated when putting programs in place */
2059
2060 // XXX: raster->sprite_coord_enable
2061
2062 for (int fs_attr = 0; fs_attr < VARYING_SLOT_MAX; fs_attr++) {
2063 const int input_index = wm_prog_data->urb_setup[fs_attr];
2064 if (input_index < 0 || input_index >= 16)
2065 continue;
2066
2067 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) *attr =
2068 &attr_overrides[input_index];
2069
2070 /* Viewport and Layer are stored in the VUE header. We need to override
2071 * them to zero if earlier stages didn't write them, as GL requires that
2072 * they read back as zero when not explicitly set.
2073 */
2074 switch (fs_attr) {
2075 case VARYING_SLOT_VIEWPORT:
2076 case VARYING_SLOT_LAYER:
2077 attr->ComponentOverrideX = true;
2078 attr->ComponentOverrideW = true;
2079 attr->ConstantSource = CONST_0000;
2080
2081 if (!(vue_map->slots_valid & VARYING_BIT_LAYER))
2082 attr->ComponentOverrideY = true;
2083 if (!(vue_map->slots_valid & VARYING_BIT_VIEWPORT))
2084 attr->ComponentOverrideZ = true;
2085 continue;
2086
2087 case VARYING_SLOT_PRIMITIVE_ID:
2088 attr->ComponentOverrideX = true;
2089 attr->ComponentOverrideY = true;
2090 attr->ComponentOverrideZ = true;
2091 attr->ComponentOverrideW = true;
2092 attr->ConstantSource = PRIM_ID;
2093 continue;
2094
2095 default:
2096 break;
2097 }
2098
2099 if (sprite_coord_enables & (1 << input_index))
2100 continue;
2101
2102 int slot = vue_map->varying_to_slot[fs_attr];
2103
2104 /* If there was only a back color written but not front, use back
2105 * as the color instead of undefined.
2106 */
2107 if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
2108 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
2109 if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
2110 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
2111
2112 /* Not written by the previous stage - undefined. */
2113 if (slot == -1) {
2114 attr->ComponentOverrideX = true;
2115 attr->ComponentOverrideY = true;
2116 attr->ComponentOverrideZ = true;
2117 attr->ComponentOverrideW = true;
2118 attr->ConstantSource = CONST_0001_FLOAT;
2119 continue;
2120 }
2121
2122 /* Compute the location of the attribute relative to the read offset,
2123 * which is counted in 256-bit increments (two 128-bit VUE slots).
2124 */
2125 const int source_attr = slot - 2 * urb_read_offset;
2126 assert(source_attr >= 0 && source_attr <= 32);
2127 attr->SourceAttribute = source_attr;
2128
2129 /* If we are doing two-sided color, and the VUE slot following this one
2130 * represents a back-facing color, then we need to instruct the SF unit
2131 * to do back-facing swizzling.
2132 */
2133 if (cso_rast->light_twoside &&
2134 ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
2135 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
2136 (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
2137 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1)))
2138 attr->SwizzleSelect = INPUTATTR_FACING;
2139 }
2140
2141 iris_emit_cmd(batch, GENX(3DSTATE_SBE_SWIZ), sbes) {
2142 for (int i = 0; i < 16; i++)
2143 sbes.Attribute[i] = attr_overrides[i];
2144 }
2145 }
2146
2147 static unsigned
2148 iris_calculate_point_sprite_overrides(const struct brw_wm_prog_data *prog_data,
2149 const struct iris_rasterizer_state *cso)
2150 {
2151 unsigned overrides = 0;
2152
2153 if (prog_data->urb_setup[VARYING_SLOT_PNTC] != -1)
2154 overrides |= 1 << prog_data->urb_setup[VARYING_SLOT_PNTC];
2155
2156 for (int i = 0; i < 8; i++) {
2157 if ((cso->sprite_coord_enable & (1 << i)) &&
2158 prog_data->urb_setup[VARYING_SLOT_TEX0 + i] != -1)
2159 overrides |= 1 << prog_data->urb_setup[VARYING_SLOT_TEX0 + i];
2160 }
2161
2162 return overrides;
2163 }
2164
2165 static void
2166 iris_emit_sbe(struct iris_batch *batch, const struct iris_context *ice)
2167 {
2168 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2169 const struct brw_wm_prog_data *wm_prog_data = (void *)
2170 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2171 const struct shader_info *fs_info =
2172 iris_get_shader_info(ice, MESA_SHADER_FRAGMENT);
2173
2174 unsigned urb_read_offset, urb_read_length;
2175 iris_compute_sbe_urb_read_interval(fs_info->inputs_read,
2176 ice->shaders.last_vue_map,
2177 cso_rast->light_twoside,
2178 &urb_read_offset, &urb_read_length);
2179
2180 unsigned sprite_coord_overrides =
2181 iris_calculate_point_sprite_overrides(wm_prog_data, cso_rast);
2182
2183 iris_emit_cmd(batch, GENX(3DSTATE_SBE), sbe) {
2184 sbe.AttributeSwizzleEnable = true;
2185 sbe.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
2186 sbe.PointSpriteTextureCoordinateOrigin = cso_rast->sprite_coord_mode;
2187 sbe.VertexURBEntryReadOffset = urb_read_offset;
2188 sbe.VertexURBEntryReadLength = urb_read_length;
2189 sbe.ForceVertexURBEntryReadOffset = true;
2190 sbe.ForceVertexURBEntryReadLength = true;
2191 sbe.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
2192 sbe.PointSpriteTextureCoordinateEnable = sprite_coord_overrides;
2193
2194 for (int i = 0; i < 32; i++) {
2195 sbe.AttributeActiveComponentFormat[i] = ACTIVE_COMPONENT_XYZW;
2196 }
2197 }
2198
2199 iris_emit_sbe_swiz(batch, ice, urb_read_offset, sprite_coord_overrides);
2200 }
2201
2202 static void
2203 iris_bind_compute_state(struct pipe_context *ctx, void *state)
2204 {
2205 }
2206
2207 static void
2208 iris_populate_sampler_key(const struct iris_context *ice,
2209 struct brw_sampler_prog_key_data *key)
2210 {
2211 for (int i = 0; i < MAX_SAMPLERS; i++) {
2212 key->swizzles[i] = 0x688; /* XYZW */
2213 }
2214 }
2215
2216 static void
2217 iris_populate_vs_key(const struct iris_context *ice,
2218 struct brw_vs_prog_key *key)
2219 {
2220 iris_populate_sampler_key(ice, &key->tex);
2221 }
2222
2223 static void
2224 iris_populate_tcs_key(const struct iris_context *ice,
2225 struct brw_tcs_prog_key *key)
2226 {
2227 iris_populate_sampler_key(ice, &key->tex);
2228 }
2229
2230 static void
2231 iris_populate_tes_key(const struct iris_context *ice,
2232 struct brw_tes_prog_key *key)
2233 {
2234 iris_populate_sampler_key(ice, &key->tex);
2235 }
2236
2237 static void
2238 iris_populate_gs_key(const struct iris_context *ice,
2239 struct brw_gs_prog_key *key)
2240 {
2241 iris_populate_sampler_key(ice, &key->tex);
2242 }
2243
2244 static void
2245 iris_populate_fs_key(const struct iris_context *ice,
2246 struct brw_wm_prog_key *key)
2247 {
2248 iris_populate_sampler_key(ice, &key->tex);
2249
2250 /* XXX: dirty flags? */
2251 const struct pipe_framebuffer_state *fb = &ice->state.framebuffer;
2252 const struct iris_depth_stencil_alpha_state *zsa = ice->state.cso_zsa;
2253 const struct iris_rasterizer_state *rast = ice->state.cso_rast;
2254 const struct iris_blend_state *blend = ice->state.cso_blend;
2255
2256 key->nr_color_regions = fb->nr_cbufs;
2257
2258 key->clamp_fragment_color = rast->clamp_fragment_color;
2259
2260 key->replicate_alpha = fb->nr_cbufs > 1 &&
2261 (zsa->alpha.enabled || blend->alpha_to_coverage);
2262
2263 /* XXX: only bother if COL0/1 are read */
2264 key->flat_shade = rast->flatshade;
2265
2266 key->persample_interp = rast->force_persample_interp;
2267 key->multisample_fbo = rast->multisample && fb->samples > 1;
2268
2269 key->coherent_fb_fetch = true;
2270
2271 // XXX: uint64_t input_slots_valid; - for >16 inputs
2272
2273 // XXX: key->force_dual_color_blend for unigine
2274 // XXX: respect hint for high_quality_derivatives:1;
2275 }
2276
2277 #if 0
2278 // XXX: these need to go in INIT_THREAD_DISPATCH_FIELDS
2279 pkt.SamplerCount = \
2280 DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4); \
2281 pkt.PerThreadScratchSpace = prog_data->total_scratch == 0 ? 0 : \
2282 ffs(stage_state->per_thread_scratch) - 11; \
2283
2284 #endif
2285
2286 static uint64_t
2287 KSP(const struct iris_compiled_shader *shader)
2288 {
2289 struct iris_resource *res = (void *) shader->assembly.res;
2290 return iris_bo_offset_from_base_address(res->bo) + shader->assembly.offset;
2291 }
2292
2293 #define INIT_THREAD_DISPATCH_FIELDS(pkt, prefix) \
2294 pkt.KernelStartPointer = KSP(shader); \
2295 pkt.BindingTableEntryCount = prog_data->binding_table.size_bytes / 4; \
2296 pkt.FloatingPointMode = prog_data->use_alt_mode; \
2297 \
2298 pkt.DispatchGRFStartRegisterForURBData = \
2299 prog_data->dispatch_grf_start_reg; \
2300 pkt.prefix##URBEntryReadLength = vue_prog_data->urb_read_length; \
2301 pkt.prefix##URBEntryReadOffset = 0; \
2302 \
2303 pkt.StatisticsEnable = true; \
2304 pkt.Enable = true;
2305
2306 static void
2307 iris_store_vs_state(const struct gen_device_info *devinfo,
2308 struct iris_compiled_shader *shader)
2309 {
2310 struct brw_stage_prog_data *prog_data = shader->prog_data;
2311 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
2312
2313 iris_pack_command(GENX(3DSTATE_VS), shader->derived_data, vs) {
2314 INIT_THREAD_DISPATCH_FIELDS(vs, Vertex);
2315 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
2316 vs.SIMD8DispatchEnable = true;
2317 vs.UserClipDistanceCullTestEnableBitmask =
2318 vue_prog_data->cull_distance_mask;
2319 }
2320 }
2321
2322 static void
2323 iris_store_tcs_state(const struct gen_device_info *devinfo,
2324 struct iris_compiled_shader *shader)
2325 {
2326 struct brw_stage_prog_data *prog_data = shader->prog_data;
2327 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
2328 struct brw_tcs_prog_data *tcs_prog_data = (void *) prog_data;
2329
2330 iris_pack_command(GENX(3DSTATE_HS), shader->derived_data, hs) {
2331 INIT_THREAD_DISPATCH_FIELDS(hs, Vertex);
2332
2333 hs.InstanceCount = tcs_prog_data->instances - 1;
2334 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
2335 hs.IncludeVertexHandles = true;
2336 }
2337 }
2338
2339 static void
2340 iris_store_tes_state(const struct gen_device_info *devinfo,
2341 struct iris_compiled_shader *shader)
2342 {
2343 struct brw_stage_prog_data *prog_data = shader->prog_data;
2344 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
2345 struct brw_tes_prog_data *tes_prog_data = (void *) prog_data;
2346
2347 uint32_t *te_state = (void *) shader->derived_data;
2348 uint32_t *ds_state = te_state + GENX(3DSTATE_TE_length);
2349
2350 iris_pack_command(GENX(3DSTATE_TE), te_state, te) {
2351 te.Partitioning = tes_prog_data->partitioning;
2352 te.OutputTopology = tes_prog_data->output_topology;
2353 te.TEDomain = tes_prog_data->domain;
2354 te.TEEnable = true;
2355 te.MaximumTessellationFactorOdd = 63.0;
2356 te.MaximumTessellationFactorNotOdd = 64.0;
2357 }
2358
2359 iris_pack_command(GENX(3DSTATE_DS), ds_state, ds) {
2360 INIT_THREAD_DISPATCH_FIELDS(ds, Patch);
2361
2362 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
2363 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
2364 ds.ComputeWCoordinateEnable =
2365 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
2366
2367 ds.UserClipDistanceCullTestEnableBitmask =
2368 vue_prog_data->cull_distance_mask;
2369 }
2370
2371 }
2372
2373 static void
2374 iris_store_gs_state(const struct gen_device_info *devinfo,
2375 struct iris_compiled_shader *shader)
2376 {
2377 struct brw_stage_prog_data *prog_data = shader->prog_data;
2378 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
2379 struct brw_gs_prog_data *gs_prog_data = (void *) prog_data;
2380
2381 iris_pack_command(GENX(3DSTATE_GS), shader->derived_data, gs) {
2382 INIT_THREAD_DISPATCH_FIELDS(gs, Vertex);
2383
2384 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
2385 gs.OutputTopology = gs_prog_data->output_topology;
2386 gs.ControlDataHeaderSize =
2387 gs_prog_data->control_data_header_size_hwords;
2388 gs.InstanceControl = gs_prog_data->invocations - 1;
2389 gs.DispatchMode = DISPATCH_MODE_SIMD8;
2390 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
2391 gs.ControlDataFormat = gs_prog_data->control_data_format;
2392 gs.ReorderMode = TRAILING;
2393 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
2394 gs.MaximumNumberofThreads =
2395 GEN_GEN == 8 ? (devinfo->max_gs_threads / 2 - 1)
2396 : (devinfo->max_gs_threads - 1);
2397
2398 if (gs_prog_data->static_vertex_count != -1) {
2399 gs.StaticOutput = true;
2400 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count;
2401 }
2402 gs.IncludeVertexHandles = vue_prog_data->include_vue_handles;
2403
2404 gs.UserClipDistanceCullTestEnableBitmask =
2405 vue_prog_data->cull_distance_mask;
2406
2407 const int urb_entry_write_offset = 1;
2408 const uint32_t urb_entry_output_length =
2409 DIV_ROUND_UP(vue_prog_data->vue_map.num_slots, 2) -
2410 urb_entry_write_offset;
2411
2412 gs.VertexURBEntryOutputReadOffset = urb_entry_write_offset;
2413 gs.VertexURBEntryOutputLength = MAX2(urb_entry_output_length, 1);
2414 }
2415 }
2416
2417 static void
2418 iris_store_fs_state(const struct gen_device_info *devinfo,
2419 struct iris_compiled_shader *shader)
2420 {
2421 struct brw_stage_prog_data *prog_data = shader->prog_data;
2422 struct brw_wm_prog_data *wm_prog_data = (void *) shader->prog_data;
2423
2424 uint32_t *ps_state = (void *) shader->derived_data;
2425 uint32_t *psx_state = ps_state + GENX(3DSTATE_PS_length);
2426
2427 iris_pack_command(GENX(3DSTATE_PS), ps_state, ps) {
2428 ps.VectorMaskEnable = true;
2429 //ps.SamplerCount = ...
2430 ps.BindingTableEntryCount = prog_data->binding_table.size_bytes / 4;
2431 ps.FloatingPointMode = prog_data->use_alt_mode;
2432 ps.MaximumNumberofThreadsPerPSD = 64 - (GEN_GEN == 8 ? 2 : 1);
2433
2434 ps.PushConstantEnable = prog_data->nr_params > 0 ||
2435 prog_data->ubo_ranges[0].length > 0;
2436
2437 /* From the documentation for this packet:
2438 * "If the PS kernel does not need the Position XY Offsets to
2439 * compute a Position Value, then this field should be programmed
2440 * to POSOFFSET_NONE."
2441 *
2442 * "SW Recommendation: If the PS kernel needs the Position Offsets
2443 * to compute a Position XY value, this field should match Position
2444 * ZW Interpolation Mode to ensure a consistent position.xyzw
2445 * computation."
2446 *
2447 * We only require XY sample offsets. So, this recommendation doesn't
2448 * look useful at the moment. We might need this in future.
2449 */
2450 ps.PositionXYOffsetSelect =
2451 wm_prog_data->uses_pos_offset ? POSOFFSET_SAMPLE : POSOFFSET_NONE;
2452 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
2453 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
2454 ps._32PixelDispatchEnable = wm_prog_data->dispatch_32;
2455
2456 // XXX: Disable SIMD32 with 16x MSAA
2457
2458 ps.DispatchGRFStartRegisterForConstantSetupData0 =
2459 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 0);
2460 ps.DispatchGRFStartRegisterForConstantSetupData1 =
2461 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 1);
2462 ps.DispatchGRFStartRegisterForConstantSetupData2 =
2463 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 2);
2464
2465 ps.KernelStartPointer0 =
2466 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 0);
2467 ps.KernelStartPointer1 =
2468 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 1);
2469 ps.KernelStartPointer2 =
2470 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
2471 }
2472
2473 iris_pack_command(GENX(3DSTATE_PS_EXTRA), psx_state, psx) {
2474 psx.PixelShaderValid = true;
2475 psx.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
2476 psx.PixelShaderKillsPixel = wm_prog_data->uses_kill;
2477 psx.AttributeEnable = wm_prog_data->num_varying_inputs != 0;
2478 psx.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
2479 psx.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
2480 psx.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
2481
2482 if (wm_prog_data->uses_sample_mask) {
2483 /* TODO: conservative rasterization */
2484 if (wm_prog_data->post_depth_coverage)
2485 psx.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
2486 else
2487 psx.InputCoverageMaskState = ICMS_NORMAL;
2488 }
2489
2490 psx.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
2491 psx.PixelShaderPullsBary = wm_prog_data->pulls_bary;
2492 psx.PixelShaderComputesStencil = wm_prog_data->computed_stencil;
2493
2494 // XXX: UAV bit
2495 }
2496 }
2497
2498 static unsigned
2499 iris_derived_program_state_size(enum iris_program_cache_id cache_id)
2500 {
2501 assert(cache_id <= IRIS_CACHE_BLORP);
2502
2503 static const unsigned dwords[] = {
2504 [IRIS_CACHE_VS] = GENX(3DSTATE_VS_length),
2505 [IRIS_CACHE_TCS] = GENX(3DSTATE_HS_length),
2506 [IRIS_CACHE_TES] = GENX(3DSTATE_TE_length) + GENX(3DSTATE_DS_length),
2507 [IRIS_CACHE_GS] = GENX(3DSTATE_GS_length),
2508 [IRIS_CACHE_FS] =
2509 GENX(3DSTATE_PS_length) + GENX(3DSTATE_PS_EXTRA_length),
2510 [IRIS_CACHE_CS] = 0,
2511 [IRIS_CACHE_BLORP] = 0,
2512 };
2513
2514 return sizeof(uint32_t) * dwords[cache_id];
2515 }
2516
2517 static void
2518 iris_store_derived_program_state(const struct gen_device_info *devinfo,
2519 enum iris_program_cache_id cache_id,
2520 struct iris_compiled_shader *shader)
2521 {
2522 switch (cache_id) {
2523 case IRIS_CACHE_VS:
2524 iris_store_vs_state(devinfo, shader);
2525 break;
2526 case IRIS_CACHE_TCS:
2527 iris_store_tcs_state(devinfo, shader);
2528 break;
2529 case IRIS_CACHE_TES:
2530 iris_store_tes_state(devinfo, shader);
2531 break;
2532 case IRIS_CACHE_GS:
2533 iris_store_gs_state(devinfo, shader);
2534 break;
2535 case IRIS_CACHE_FS:
2536 iris_store_fs_state(devinfo, shader);
2537 break;
2538 case IRIS_CACHE_CS:
2539 case IRIS_CACHE_BLORP:
2540 break;
2541 default:
2542 break;
2543 }
2544 }
2545
2546 static void
2547 iris_upload_urb_config(struct iris_context *ice, struct iris_batch *batch)
2548 {
2549 const struct gen_device_info *devinfo = &batch->screen->devinfo;
2550 const unsigned push_size_kB = 32;
2551 unsigned entries[4];
2552 unsigned start[4];
2553 unsigned size[4];
2554
2555 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2556 if (!ice->shaders.prog[i]) {
2557 size[i] = 1;
2558 } else {
2559 struct brw_vue_prog_data *vue_prog_data =
2560 (void *) ice->shaders.prog[i]->prog_data;
2561 size[i] = vue_prog_data->urb_entry_size;
2562 }
2563 assert(size[i] != 0);
2564 }
2565
2566 gen_get_urb_config(devinfo, 1024 * push_size_kB,
2567 1024 * ice->shaders.urb_size,
2568 ice->shaders.prog[MESA_SHADER_TESS_EVAL] != NULL,
2569 ice->shaders.prog[MESA_SHADER_GEOMETRY] != NULL,
2570 size, entries, start);
2571
2572 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2573 iris_emit_cmd(batch, GENX(3DSTATE_URB_VS), urb) {
2574 urb._3DCommandSubOpcode += i;
2575 urb.VSURBStartingAddress = start[i];
2576 urb.VSURBEntryAllocationSize = size[i] - 1;
2577 urb.VSNumberofURBEntries = entries[i];
2578 }
2579 }
2580 }
2581
2582 static const uint32_t push_constant_opcodes[] = {
2583 [MESA_SHADER_VERTEX] = 21,
2584 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
2585 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
2586 [MESA_SHADER_GEOMETRY] = 22,
2587 [MESA_SHADER_FRAGMENT] = 23,
2588 [MESA_SHADER_COMPUTE] = 0,
2589 };
2590
2591 /**
2592 * Add a surface to the validation list, as well as the buffer containing
2593 * the corresponding SURFACE_STATE.
2594 *
2595 * Returns the binding table entry (offset to SURFACE_STATE).
2596 */
2597 static uint32_t
2598 use_surface(struct iris_batch *batch,
2599 struct pipe_surface *p_surf,
2600 bool writeable)
2601 {
2602 struct iris_surface *surf = (void *) p_surf;
2603
2604 iris_use_pinned_bo(batch, iris_resource_bo(p_surf->texture), writeable);
2605 iris_use_pinned_bo(batch, iris_resource_bo(surf->surface_state.res), false);
2606
2607 return surf->surface_state.offset;
2608 }
2609
2610 static uint32_t
2611 use_sampler_view(struct iris_batch *batch, struct iris_sampler_view *isv)
2612 {
2613 iris_use_pinned_bo(batch, iris_resource_bo(isv->pipe.texture), false);
2614 iris_use_pinned_bo(batch, iris_resource_bo(isv->surface_state.res), false);
2615
2616 return isv->surface_state.offset;
2617 }
2618
2619 static uint32_t
2620 use_const_buffer(struct iris_batch *batch, struct iris_const_buffer *cbuf)
2621 {
2622 iris_use_pinned_bo(batch, iris_resource_bo(cbuf->data.res), false);
2623 iris_use_pinned_bo(batch, iris_resource_bo(cbuf->surface_state.res), false);
2624
2625 return cbuf->surface_state.offset;
2626 }
2627
2628 static uint32_t
2629 use_null_surface(struct iris_batch *batch, struct iris_context *ice)
2630 {
2631 struct iris_bo *state_bo = iris_resource_bo(ice->state.unbound_tex.res);
2632
2633 iris_use_pinned_bo(batch, state_bo, false);
2634
2635 return ice->state.unbound_tex.offset;
2636 }
2637
2638 static uint32_t
2639 use_null_fb_surface(struct iris_batch *batch, struct iris_context *ice)
2640 {
2641 struct iris_bo *state_bo = iris_resource_bo(ice->state.null_fb.res);
2642
2643 iris_use_pinned_bo(batch, state_bo, false);
2644
2645 return ice->state.null_fb.offset;
2646 }
2647
2648 static uint32_t
2649 use_ssbo(struct iris_batch *batch, struct iris_context *ice,
2650 struct iris_shader_state *shs, int i)
2651 {
2652 if (!shs->ssbo[i])
2653 return use_null_surface(batch, ice);
2654
2655 struct iris_state_ref *surf_state = &shs->ssbo_surface_state[i];
2656
2657 iris_use_pinned_bo(batch, iris_resource_bo(shs->ssbo[i]), true);
2658 iris_use_pinned_bo(batch, iris_resource_bo(surf_state->res), false);
2659
2660 return surf_state->offset;
2661 }
2662
2663 static void
2664 iris_populate_binding_table(struct iris_context *ice,
2665 struct iris_batch *batch,
2666 gl_shader_stage stage)
2667 {
2668 const struct iris_binder *binder = &batch->binder;
2669 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2670 if (!shader)
2671 return;
2672
2673 const struct shader_info *info = iris_get_shader_info(ice, stage);
2674 struct iris_shader_state *shs = &ice->shaders.state[stage];
2675
2676 // Surfaces:
2677 // - pull constants
2678 // - ubos/ssbos/abos
2679 // - images
2680 // - textures
2681 // - render targets - write and read
2682
2683 //struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2684 uint32_t *bt_map = binder->map + binder->bt_offset[stage];
2685 int s = 0;
2686
2687 if (stage == MESA_SHADER_FRAGMENT) {
2688 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2689 if (cso_fb->nr_cbufs) {
2690 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
2691 if (cso_fb->cbufs[i])
2692 bt_map[s++] = use_surface(batch, cso_fb->cbufs[i], true);
2693 else
2694 bt_map[s++] = use_null_fb_surface(batch, ice);
2695 }
2696 } else
2697 bt_map[s++] = use_null_fb_surface(batch, ice);
2698 }
2699
2700 //assert(prog_data->binding_table.texture_start ==
2701 //(ice->state.num_textures[stage] ? s : 0xd0d0d0d0));
2702
2703 for (int i = 0; i < ice->state.num_textures[stage]; i++) {
2704 struct iris_sampler_view *view = ice->state.textures[stage][i];
2705 bt_map[s++] = view ? use_sampler_view(batch, view)
2706 : use_null_surface(batch, ice);
2707 }
2708
2709 for (int i = 0; i < 1 + info->num_ubos; i++) {
2710 struct iris_const_buffer *cbuf = &shs->constbuf[i];
2711 if (!cbuf->surface_state.res)
2712 break;
2713
2714 bt_map[s++] = use_const_buffer(batch, cbuf);
2715 }
2716
2717 /* XXX: st is wasting 16 binding table slots for ABOs. Should add a cap
2718 * for changing nir_lower_atomics_to_ssbos setting and buffer_base offset
2719 * in st_atom_storagebuf.c so it'll compact them into one range, with
2720 * SSBOs starting at info->num_abos. Ideally it'd reset num_abos to 0 too
2721 */
2722 if (info->num_abos + info->num_ssbos > 0) {
2723 for (int i = 0; i < IRIS_MAX_ABOS + info->num_ssbos; i++) {
2724 bt_map[s++] = use_ssbo(batch, ice, shs, i);
2725 }
2726 }
2727
2728 #if 0
2729 // XXX: not implemented yet
2730 assert(prog_data->binding_table.pull_constants_start == 0xd0d0d0d0);
2731 assert(prog_data->binding_table.ubo_start == 0xd0d0d0d0);
2732 assert(prog_data->binding_table.ssbo_start == 0xd0d0d0d0);
2733 assert(prog_data->binding_table.image_start == 0xd0d0d0d0);
2734 assert(prog_data->binding_table.shader_time_start == 0xd0d0d0d0);
2735 //assert(prog_data->binding_table.plane_start[1] == 0xd0d0d0d0);
2736 //assert(prog_data->binding_table.plane_start[2] == 0xd0d0d0d0);
2737 #endif
2738 }
2739
2740 static void
2741 iris_use_optional_res(struct iris_batch *batch,
2742 struct pipe_resource *res,
2743 bool writeable)
2744 {
2745 if (res) {
2746 struct iris_bo *bo = iris_resource_bo(res);
2747 iris_use_pinned_bo(batch, bo, writeable);
2748 }
2749 }
2750
2751
2752 /**
2753 * Pin any BOs which were installed by a previous batch, and restored
2754 * via the hardware logical context mechanism.
2755 *
2756 * We don't need to re-emit all state every batch - the hardware context
2757 * mechanism will save and restore it for us. This includes pointers to
2758 * various BOs...which won't exist unless we ask the kernel to pin them
2759 * by adding them to the validation list.
2760 *
2761 * We can skip buffers if we've re-emitted those packets, as we're
2762 * overwriting those stale pointers with new ones, and don't actually
2763 * refer to the old BOs.
2764 */
2765 static void
2766 iris_restore_context_saved_bos(struct iris_context *ice,
2767 struct iris_batch *batch,
2768 const struct pipe_draw_info *draw)
2769 {
2770 // XXX: whack IRIS_SHADER_DIRTY_BINDING_TABLE on new batch
2771
2772 const uint64_t clean = ~ice->state.dirty;
2773
2774 if (clean & IRIS_DIRTY_CC_VIEWPORT) {
2775 iris_use_optional_res(batch, ice->state.last_res.cc_vp, false);
2776 }
2777
2778 if (clean & IRIS_DIRTY_SF_CL_VIEWPORT) {
2779 iris_use_optional_res(batch, ice->state.last_res.sf_cl_vp, false);
2780 }
2781
2782 if (clean & IRIS_DIRTY_BLEND_STATE) {
2783 iris_use_optional_res(batch, ice->state.last_res.blend, false);
2784 }
2785
2786 if (clean & IRIS_DIRTY_COLOR_CALC_STATE) {
2787 iris_use_optional_res(batch, ice->state.last_res.color_calc, false);
2788 }
2789
2790 if (clean & IRIS_DIRTY_SCISSOR_RECT) {
2791 iris_use_optional_res(batch, ice->state.last_res.scissor, false);
2792 }
2793
2794 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2795 if (clean & (IRIS_DIRTY_CONSTANTS_VS << stage))
2796 continue;
2797
2798 struct iris_shader_state *shs = &ice->shaders.state[stage];
2799 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2800
2801 if (!shader)
2802 continue;
2803
2804 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2805
2806 for (int i = 0; i < 4; i++) {
2807 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2808
2809 if (range->length == 0)
2810 continue;
2811
2812 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
2813 struct iris_resource *res = (void *) cbuf->data.res;
2814
2815 if (res)
2816 iris_use_pinned_bo(batch, res->bo, false);
2817 else
2818 iris_use_pinned_bo(batch, batch->screen->workaround_bo, false);
2819 }
2820 }
2821
2822 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2823 struct pipe_resource *res = ice->state.sampler_table[stage].res;
2824 if (res)
2825 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
2826 }
2827
2828 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2829 if (clean & (IRIS_DIRTY_VS << stage)) {
2830 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2831 if (shader) {
2832 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
2833 iris_use_pinned_bo(batch, bo, false);
2834 }
2835
2836 // XXX: scratch buffer
2837 }
2838 }
2839
2840 if (clean & IRIS_DIRTY_DEPTH_BUFFER) {
2841 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2842
2843 if (cso_fb->zsbuf) {
2844 struct iris_resource *zres = (void *) cso_fb->zsbuf->texture;
2845 // XXX: depth might not be writable...
2846 iris_use_pinned_bo(batch, zres->bo, true);
2847 }
2848 }
2849
2850 if (draw->index_size > 0) {
2851 // XXX: index buffer
2852 }
2853
2854 if (clean & IRIS_DIRTY_VERTEX_BUFFERS) {
2855 struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
2856 for (unsigned i = 0; i < cso->num_buffers; i++) {
2857 struct iris_resource *res = (void *) cso->resources[i];
2858 iris_use_pinned_bo(batch, res->bo, false);
2859 }
2860 }
2861 }
2862
2863 static void
2864 iris_upload_render_state(struct iris_context *ice,
2865 struct iris_batch *batch,
2866 const struct pipe_draw_info *draw)
2867 {
2868 const uint64_t dirty = ice->state.dirty;
2869
2870 struct iris_genx_state *genx = ice->state.genx;
2871 struct brw_wm_prog_data *wm_prog_data = (void *)
2872 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2873
2874 if (dirty & IRIS_DIRTY_CC_VIEWPORT) {
2875 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2876 uint32_t cc_vp_address;
2877
2878 /* XXX: could avoid streaming for depth_clip [0,1] case. */
2879 uint32_t *cc_vp_map =
2880 stream_state(batch, ice->state.dynamic_uploader,
2881 &ice->state.last_res.cc_vp,
2882 4 * ice->state.num_viewports *
2883 GENX(CC_VIEWPORT_length), 32, &cc_vp_address);
2884 for (int i = 0; i < ice->state.num_viewports; i++) {
2885 float zmin, zmax;
2886 util_viewport_zmin_zmax(&ice->state.viewports[i],
2887 cso_rast->clip_halfz, &zmin, &zmax);
2888 if (cso_rast->depth_clip_near)
2889 zmin = 0.0;
2890 if (cso_rast->depth_clip_far)
2891 zmax = 1.0;
2892
2893 iris_pack_state(GENX(CC_VIEWPORT), cc_vp_map, ccv) {
2894 ccv.MinimumDepth = zmin;
2895 ccv.MaximumDepth = zmax;
2896 }
2897
2898 cc_vp_map += GENX(CC_VIEWPORT_length);
2899 }
2900
2901 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), ptr) {
2902 ptr.CCViewportPointer = cc_vp_address;
2903 }
2904 }
2905
2906 if (dirty & IRIS_DIRTY_SF_CL_VIEWPORT) {
2907 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
2908 ptr.SFClipViewportPointer =
2909 emit_state(batch, ice->state.dynamic_uploader,
2910 &ice->state.last_res.sf_cl_vp,
2911 genx->sf_cl_vp, 4 * GENX(SF_CLIP_VIEWPORT_length) *
2912 ice->state.num_viewports, 64);
2913 }
2914 }
2915
2916 /* XXX: L3 State */
2917
2918 // XXX: this is only flagged at setup, we assume a static configuration
2919 if (dirty & IRIS_DIRTY_URB) {
2920 iris_upload_urb_config(ice, batch);
2921 }
2922
2923 if (dirty & IRIS_DIRTY_BLEND_STATE) {
2924 struct iris_blend_state *cso_blend = ice->state.cso_blend;
2925 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2926 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
2927 const int num_dwords = 4 * (GENX(BLEND_STATE_length) +
2928 cso_fb->nr_cbufs * GENX(BLEND_STATE_ENTRY_length));
2929 uint32_t blend_offset;
2930 uint32_t *blend_map =
2931 stream_state(batch, ice->state.dynamic_uploader,
2932 &ice->state.last_res.blend,
2933 4 * num_dwords, 64, &blend_offset);
2934
2935 uint32_t blend_state_header;
2936 iris_pack_state(GENX(BLEND_STATE), &blend_state_header, bs) {
2937 bs.AlphaTestEnable = cso_zsa->alpha.enabled;
2938 bs.AlphaTestFunction = translate_compare_func(cso_zsa->alpha.func);
2939 }
2940
2941 blend_map[0] = blend_state_header | cso_blend->blend_state[0];
2942 memcpy(&blend_map[1], &cso_blend->blend_state[1],
2943 sizeof(cso_blend->blend_state) - sizeof(uint32_t));
2944
2945 iris_emit_cmd(batch, GENX(3DSTATE_BLEND_STATE_POINTERS), ptr) {
2946 ptr.BlendStatePointer = blend_offset;
2947 ptr.BlendStatePointerValid = true;
2948 }
2949 }
2950
2951 if (dirty & IRIS_DIRTY_COLOR_CALC_STATE) {
2952 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2953 uint32_t cc_offset;
2954 void *cc_map =
2955 stream_state(batch, ice->state.dynamic_uploader,
2956 &ice->state.last_res.color_calc,
2957 sizeof(uint32_t) * GENX(COLOR_CALC_STATE_length),
2958 64, &cc_offset);
2959 iris_pack_state(GENX(COLOR_CALC_STATE), cc_map, cc) {
2960 cc.AlphaTestFormat = ALPHATEST_FLOAT32;
2961 cc.AlphaReferenceValueAsFLOAT32 = cso->alpha.ref_value;
2962 cc.BlendConstantColorRed = ice->state.blend_color.color[0];
2963 cc.BlendConstantColorGreen = ice->state.blend_color.color[1];
2964 cc.BlendConstantColorBlue = ice->state.blend_color.color[2];
2965 cc.BlendConstantColorAlpha = ice->state.blend_color.color[3];
2966 }
2967 iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
2968 ptr.ColorCalcStatePointer = cc_offset;
2969 ptr.ColorCalcStatePointerValid = true;
2970 }
2971 }
2972
2973 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2974 // XXX: wrong dirty tracking...
2975 if (!(dirty & (IRIS_DIRTY_CONSTANTS_VS << stage)))
2976 continue;
2977
2978 struct iris_shader_state *shs = &ice->shaders.state[stage];
2979 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2980
2981 if (!shader)
2982 continue;
2983
2984 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2985
2986 iris_emit_cmd(batch, GENX(3DSTATE_CONSTANT_VS), pkt) {
2987 pkt._3DCommandSubOpcode = push_constant_opcodes[stage];
2988 if (prog_data) {
2989 /* The Skylake PRM contains the following restriction:
2990 *
2991 * "The driver must ensure The following case does not occur
2992 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
2993 * buffer 3 read length equal to zero committed followed by a
2994 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
2995 * zero committed."
2996 *
2997 * To avoid this, we program the buffers in the highest slots.
2998 * This way, slot 0 is only used if slot 3 is also used.
2999 */
3000 int n = 3;
3001
3002 for (int i = 3; i >= 0; i--) {
3003 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
3004
3005 if (range->length == 0)
3006 continue;
3007
3008 // XXX: is range->block a constbuf index? it would be nice
3009 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
3010 struct iris_resource *res = (void *) cbuf->data.res;
3011
3012 assert(cbuf->data.offset % 32 == 0);
3013
3014 pkt.ConstantBody.ReadLength[n] = range->length;
3015 pkt.ConstantBody.Buffer[n] =
3016 res ? ro_bo(res->bo, range->start * 32 + cbuf->data.offset)
3017 : ro_bo(batch->screen->workaround_bo, 0);
3018 n--;
3019 }
3020 }
3021 }
3022 }
3023
3024 struct iris_binder *binder = &batch->binder;
3025
3026 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3027 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
3028 iris_emit_cmd(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), ptr) {
3029 ptr._3DCommandSubOpcode = 38 + stage;
3030 ptr.PointertoVSBindingTable = binder->bt_offset[stage];
3031 }
3032 }
3033 }
3034
3035 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3036 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
3037 iris_populate_binding_table(ice, batch, stage);
3038 }
3039 }
3040
3041 if (ice->state.need_border_colors)
3042 iris_use_pinned_bo(batch, ice->state.border_color_pool.bo, false);
3043
3044 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3045 if (!(dirty & (IRIS_DIRTY_SAMPLER_STATES_VS << stage)) ||
3046 !ice->shaders.prog[stage])
3047 continue;
3048
3049 struct pipe_resource *res = ice->state.sampler_table[stage].res;
3050 if (res)
3051 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
3052
3053 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ptr) {
3054 ptr._3DCommandSubOpcode = 43 + stage;
3055 ptr.PointertoVSSamplerState = ice->state.sampler_table[stage].offset;
3056 }
3057 }
3058
3059 if (dirty & IRIS_DIRTY_MULTISAMPLE) {
3060 iris_emit_cmd(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
3061 ms.PixelLocation =
3062 ice->state.cso_rast->half_pixel_center ? CENTER : UL_CORNER;
3063 if (ice->state.framebuffer.samples > 0)
3064 ms.NumberofMultisamples = ffs(ice->state.framebuffer.samples) - 1;
3065 }
3066 }
3067
3068 if (dirty & IRIS_DIRTY_SAMPLE_MASK) {
3069 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_MASK), ms) {
3070 ms.SampleMask = MAX2(ice->state.sample_mask, 1);
3071 }
3072 }
3073
3074 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3075 if (!(dirty & (IRIS_DIRTY_VS << stage)))
3076 continue;
3077
3078 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3079
3080 if (shader) {
3081 struct iris_resource *cache = (void *) shader->assembly.res;
3082 iris_use_pinned_bo(batch, cache->bo, false);
3083 iris_batch_emit(batch, shader->derived_data,
3084 iris_derived_program_state_size(stage));
3085 } else {
3086 if (stage == MESA_SHADER_TESS_EVAL) {
3087 iris_emit_cmd(batch, GENX(3DSTATE_HS), hs);
3088 iris_emit_cmd(batch, GENX(3DSTATE_TE), te);
3089 iris_emit_cmd(batch, GENX(3DSTATE_DS), ds);
3090 } else if (stage == MESA_SHADER_GEOMETRY) {
3091 iris_emit_cmd(batch, GENX(3DSTATE_GS), gs);
3092 }
3093 }
3094 }
3095
3096 if (ice->state.streamout_active) {
3097 if (dirty & IRIS_DIRTY_SO_BUFFERS) {
3098 iris_batch_emit(batch, genx->so_buffers,
3099 4 * 4 * GENX(3DSTATE_SO_BUFFER_length));
3100 for (int i = 0; i < 4; i++) {
3101 struct iris_stream_output_target *tgt =
3102 (void *) ice->state.so_target[i];
3103 if (tgt) {
3104 iris_use_pinned_bo(batch, iris_resource_bo(tgt->base.buffer),
3105 true);
3106 iris_use_pinned_bo(batch, iris_resource_bo(tgt->offset.res),
3107 true);
3108 }
3109 }
3110 }
3111
3112 if ((dirty & IRIS_DIRTY_SO_DECL_LIST) && ice->state.streamout) {
3113 uint32_t *decl_list =
3114 ice->state.streamout + GENX(3DSTATE_STREAMOUT_length);
3115 iris_batch_emit(batch, decl_list, 4 * ((decl_list[0] & 0xff) + 2));
3116 }
3117
3118 if (dirty & IRIS_DIRTY_STREAMOUT) {
3119 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
3120
3121 uint32_t dynamic_sol[GENX(3DSTATE_STREAMOUT_length)];
3122 iris_pack_command(GENX(3DSTATE_STREAMOUT), dynamic_sol, sol) {
3123 sol.SOFunctionEnable = true;
3124 sol.SOStatisticsEnable = true;
3125
3126 // XXX: GL_PRIMITIVES_GENERATED query
3127 sol.RenderingDisable = cso_rast->rasterizer_discard;
3128 sol.ReorderMode = cso_rast->flatshade_first ? LEADING : TRAILING;
3129 }
3130
3131 assert(ice->state.streamout);
3132
3133 iris_emit_merge(batch, ice->state.streamout, dynamic_sol,
3134 GENX(3DSTATE_STREAMOUT_length));
3135 }
3136 } else {
3137 if (dirty & IRIS_DIRTY_STREAMOUT) {
3138 iris_emit_cmd(batch, GENX(3DSTATE_STREAMOUT), sol);
3139 }
3140 }
3141
3142 if (dirty & IRIS_DIRTY_CLIP) {
3143 struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
3144 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
3145
3146 uint32_t dynamic_clip[GENX(3DSTATE_CLIP_length)];
3147 iris_pack_command(GENX(3DSTATE_CLIP), &dynamic_clip, cl) {
3148 if (wm_prog_data->barycentric_interp_modes &
3149 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS)
3150 cl.NonPerspectiveBarycentricEnable = true;
3151
3152 cl.ForceZeroRTAIndexEnable = cso_fb->layers == 0;
3153 cl.MaximumVPIndex = ice->state.num_viewports - 1;
3154 }
3155 iris_emit_merge(batch, cso_rast->clip, dynamic_clip,
3156 ARRAY_SIZE(cso_rast->clip));
3157 }
3158
3159 if (dirty & IRIS_DIRTY_RASTER) {
3160 struct iris_rasterizer_state *cso = ice->state.cso_rast;
3161 iris_batch_emit(batch, cso->raster, sizeof(cso->raster));
3162 iris_batch_emit(batch, cso->sf, sizeof(cso->sf));
3163
3164 }
3165
3166 /* XXX: FS program updates needs to flag IRIS_DIRTY_WM */
3167 if (dirty & IRIS_DIRTY_WM) {
3168 struct iris_rasterizer_state *cso = ice->state.cso_rast;
3169 uint32_t dynamic_wm[GENX(3DSTATE_WM_length)];
3170
3171 iris_pack_command(GENX(3DSTATE_WM), &dynamic_wm, wm) {
3172 wm.BarycentricInterpolationMode =
3173 wm_prog_data->barycentric_interp_modes;
3174
3175 if (wm_prog_data->early_fragment_tests)
3176 wm.EarlyDepthStencilControl = EDSC_PREPS;
3177 else if (wm_prog_data->has_side_effects)
3178 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
3179 }
3180 iris_emit_merge(batch, cso->wm, dynamic_wm, ARRAY_SIZE(cso->wm));
3181 }
3182
3183 if (1) {
3184 // XXX: 3DSTATE_SBE, 3DSTATE_SBE_SWIZ
3185 // -> iris_raster_state (point sprite texture coordinate origin)
3186 // -> bunch of shader state...
3187 iris_emit_sbe(batch, ice);
3188 }
3189
3190 if (dirty & IRIS_DIRTY_PS_BLEND) {
3191 struct iris_blend_state *cso_blend = ice->state.cso_blend;
3192 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
3193 uint32_t dynamic_pb[GENX(3DSTATE_PS_BLEND_length)];
3194 iris_pack_command(GENX(3DSTATE_PS_BLEND), &dynamic_pb, pb) {
3195 pb.HasWriteableRT = true; // XXX: comes from somewhere :(
3196 pb.AlphaTestEnable = cso_zsa->alpha.enabled;
3197 }
3198
3199 iris_emit_merge(batch, cso_blend->ps_blend, dynamic_pb,
3200 ARRAY_SIZE(cso_blend->ps_blend));
3201 }
3202
3203 if (dirty & IRIS_DIRTY_WM_DEPTH_STENCIL) {
3204 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
3205 struct pipe_stencil_ref *p_stencil_refs = &ice->state.stencil_ref;
3206
3207 uint32_t stencil_refs[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
3208 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), &stencil_refs, wmds) {
3209 wmds.StencilReferenceValue = p_stencil_refs->ref_value[0];
3210 wmds.BackfaceStencilReferenceValue = p_stencil_refs->ref_value[1];
3211 }
3212 iris_emit_merge(batch, cso->wmds, stencil_refs, ARRAY_SIZE(cso->wmds));
3213 }
3214
3215 if (dirty & IRIS_DIRTY_SCISSOR_RECT) {
3216 uint32_t scissor_offset =
3217 emit_state(batch, ice->state.dynamic_uploader,
3218 &ice->state.last_res.scissor,
3219 ice->state.scissors,
3220 sizeof(struct pipe_scissor_state) *
3221 ice->state.num_viewports, 32);
3222
3223 iris_emit_cmd(batch, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
3224 ptr.ScissorRectPointer = scissor_offset;
3225 }
3226 }
3227
3228 if (dirty & IRIS_DIRTY_DEPTH_BUFFER) {
3229 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
3230 struct iris_depth_buffer_state *cso_z = &ice->state.genx->depth_buffer;
3231
3232 iris_batch_emit(batch, cso_z->packets, sizeof(cso_z->packets));
3233
3234 if (cso_fb->zsbuf) {
3235 struct iris_resource *zres = (void *) cso_fb->zsbuf->texture;
3236 // XXX: depth might not be writable...
3237 iris_use_pinned_bo(batch, zres->bo, true);
3238 }
3239 }
3240
3241 if (dirty & IRIS_DIRTY_POLYGON_STIPPLE) {
3242 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
3243 for (int i = 0; i < 32; i++) {
3244 poly.PatternRow[i] = ice->state.poly_stipple.stipple[i];
3245 }
3246 }
3247 }
3248
3249 if (dirty & IRIS_DIRTY_LINE_STIPPLE) {
3250 struct iris_rasterizer_state *cso = ice->state.cso_rast;
3251 iris_batch_emit(batch, cso->line_stipple, sizeof(cso->line_stipple));
3252 }
3253
3254 if (1) {
3255 iris_emit_cmd(batch, GENX(3DSTATE_VF_TOPOLOGY), topo) {
3256 topo.PrimitiveTopologyType =
3257 translate_prim_type(draw->mode, draw->vertices_per_patch);
3258 }
3259 }
3260
3261 if (draw->index_size > 0) {
3262 struct iris_resource *res = NULL;
3263 unsigned offset;
3264
3265 if (draw->has_user_indices) {
3266 u_upload_data(ice->ctx.stream_uploader, 0,
3267 draw->count * draw->index_size, 4, draw->index.user,
3268 &offset, (struct pipe_resource **) &res);
3269 } else {
3270 res = (struct iris_resource *) draw->index.resource;
3271 offset = 0;
3272 }
3273
3274 iris_emit_cmd(batch, GENX(3DSTATE_INDEX_BUFFER), ib) {
3275 ib.IndexFormat = draw->index_size >> 1;
3276 ib.MOCS = MOCS_WB;
3277 ib.BufferSize = res->bo->size;
3278 ib.BufferStartingAddress = ro_bo(res->bo, offset);
3279 }
3280 }
3281
3282 if (dirty & IRIS_DIRTY_VERTEX_BUFFERS) {
3283 struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
3284 const unsigned vb_dwords = GENX(VERTEX_BUFFER_STATE_length);
3285
3286 if (cso->num_buffers > 0) {
3287 iris_batch_emit(batch, cso->vertex_buffers, sizeof(uint32_t) *
3288 (1 + vb_dwords * cso->num_buffers));
3289
3290 for (unsigned i = 0; i < cso->num_buffers; i++) {
3291 struct iris_resource *res = (void *) cso->resources[i];
3292 iris_use_pinned_bo(batch, res->bo, false);
3293 }
3294 }
3295 }
3296
3297 if (dirty & IRIS_DIRTY_VERTEX_ELEMENTS) {
3298 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
3299 const unsigned entries = MAX2(cso->count, 1);
3300 iris_batch_emit(batch, cso->vertex_elements, sizeof(uint32_t) *
3301 (1 + entries * GENX(VERTEX_ELEMENT_STATE_length)));
3302 iris_batch_emit(batch, cso->vf_instancing, sizeof(uint32_t) *
3303 entries * GENX(3DSTATE_VF_INSTANCING_length));
3304 }
3305
3306 if (dirty & IRIS_DIRTY_VF_SGVS) {
3307 const struct brw_vs_prog_data *vs_prog_data = (void *)
3308 ice->shaders.prog[MESA_SHADER_VERTEX]->prog_data;
3309 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
3310
3311 iris_emit_cmd(batch, GENX(3DSTATE_VF_SGVS), sgv) {
3312 if (vs_prog_data->uses_vertexid) {
3313 sgv.VertexIDEnable = true;
3314 sgv.VertexIDComponentNumber = 2;
3315 sgv.VertexIDElementOffset = cso->count;
3316 }
3317
3318 if (vs_prog_data->uses_instanceid) {
3319 sgv.InstanceIDEnable = true;
3320 sgv.InstanceIDComponentNumber = 3;
3321 sgv.InstanceIDElementOffset = cso->count;
3322 }
3323 }
3324 }
3325
3326 if (1) {
3327 iris_emit_cmd(batch, GENX(3DSTATE_VF), vf) {
3328 if (draw->primitive_restart) {
3329 vf.IndexedDrawCutIndexEnable = true;
3330 vf.CutIndex = draw->restart_index;
3331 }
3332 }
3333 }
3334
3335 // XXX: Gen8 - PMA fix
3336
3337 #define _3DPRIM_END_OFFSET 0x2420
3338 #define _3DPRIM_START_VERTEX 0x2430
3339 #define _3DPRIM_VERTEX_COUNT 0x2434
3340 #define _3DPRIM_INSTANCE_COUNT 0x2438
3341 #define _3DPRIM_START_INSTANCE 0x243C
3342 #define _3DPRIM_BASE_VERTEX 0x2440
3343
3344 if (draw->indirect) {
3345 /* We don't support this MultidrawIndirect. */
3346 assert(!draw->indirect->indirect_draw_count);
3347
3348 struct iris_bo *bo = iris_resource_bo(draw->indirect->buffer);
3349 assert(bo);
3350
3351 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
3352 lrm.RegisterAddress = _3DPRIM_VERTEX_COUNT;
3353 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 0);
3354 }
3355 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
3356 lrm.RegisterAddress = _3DPRIM_INSTANCE_COUNT;
3357 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 4);
3358 }
3359 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
3360 lrm.RegisterAddress = _3DPRIM_START_VERTEX;
3361 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 8);
3362 }
3363 if (draw->index_size) {
3364 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
3365 lrm.RegisterAddress = _3DPRIM_BASE_VERTEX;
3366 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 12);
3367 }
3368 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
3369 lrm.RegisterAddress = _3DPRIM_START_INSTANCE;
3370 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 16);
3371 }
3372 } else {
3373 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
3374 lrm.RegisterAddress = _3DPRIM_START_INSTANCE;
3375 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 12);
3376 }
3377 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
3378 lri.RegisterOffset = _3DPRIM_BASE_VERTEX;
3379 lri.DataDWord = 0;
3380 }
3381 }
3382 }
3383
3384 iris_emit_cmd(batch, GENX(3DPRIMITIVE), prim) {
3385 prim.StartInstanceLocation = draw->start_instance;
3386 prim.InstanceCount = draw->instance_count;
3387 prim.VertexCountPerInstance = draw->count;
3388 prim.VertexAccessType = draw->index_size > 0 ? RANDOM : SEQUENTIAL;
3389
3390 // XXX: this is probably bonkers.
3391 prim.StartVertexLocation = draw->start;
3392
3393 prim.IndirectParameterEnable = draw->indirect != NULL;
3394
3395 if (draw->index_size) {
3396 prim.BaseVertexLocation += draw->index_bias;
3397 } else {
3398 prim.StartVertexLocation += draw->index_bias;
3399 }
3400
3401 //prim.BaseVertexLocation = ...;
3402 }
3403
3404 if (!batch->contains_draw) {
3405 iris_restore_context_saved_bos(ice, batch, draw);
3406 batch->contains_draw = true;
3407 }
3408 }
3409
3410 /**
3411 * State module teardown.
3412 */
3413 static void
3414 iris_destroy_state(struct iris_context *ice)
3415 {
3416 iris_free_vertex_buffers(&ice->state.genx->vertex_buffers);
3417
3418 // XXX: unreference resources/surfaces.
3419 for (unsigned i = 0; i < ice->state.framebuffer.nr_cbufs; i++) {
3420 pipe_surface_reference(&ice->state.framebuffer.cbufs[i], NULL);
3421 }
3422 pipe_surface_reference(&ice->state.framebuffer.zsbuf, NULL);
3423
3424 for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
3425 pipe_resource_reference(&ice->state.sampler_table[stage].res, NULL);
3426 }
3427 free(ice->state.genx);
3428
3429 pipe_resource_reference(&ice->state.last_res.cc_vp, NULL);
3430 pipe_resource_reference(&ice->state.last_res.sf_cl_vp, NULL);
3431 pipe_resource_reference(&ice->state.last_res.color_calc, NULL);
3432 pipe_resource_reference(&ice->state.last_res.scissor, NULL);
3433 pipe_resource_reference(&ice->state.last_res.blend, NULL);
3434 }
3435
3436 static unsigned
3437 flags_to_post_sync_op(uint32_t flags)
3438 {
3439 if (flags & PIPE_CONTROL_WRITE_IMMEDIATE)
3440 return WriteImmediateData;
3441
3442 if (flags & PIPE_CONTROL_WRITE_DEPTH_COUNT)
3443 return WritePSDepthCount;
3444
3445 if (flags & PIPE_CONTROL_WRITE_TIMESTAMP)
3446 return WriteTimestamp;
3447
3448 return 0;
3449 }
3450
3451 /**
3452 * Do the given flags have a Post Sync or LRI Post Sync operation?
3453 */
3454 static enum pipe_control_flags
3455 get_post_sync_flags(enum pipe_control_flags flags)
3456 {
3457 flags &= PIPE_CONTROL_WRITE_IMMEDIATE |
3458 PIPE_CONTROL_WRITE_DEPTH_COUNT |
3459 PIPE_CONTROL_WRITE_TIMESTAMP |
3460 PIPE_CONTROL_LRI_POST_SYNC_OP;
3461
3462 /* Only one "Post Sync Op" is allowed, and it's mutually exclusive with
3463 * "LRI Post Sync Operation". So more than one bit set would be illegal.
3464 */
3465 assert(util_bitcount(flags) <= 1);
3466
3467 return flags;
3468 }
3469
3470 // XXX: compute support
3471 #define IS_COMPUTE_PIPELINE(batch) (batch->ring != I915_EXEC_RENDER)
3472
3473 /**
3474 * Emit a series of PIPE_CONTROL commands, taking into account any
3475 * workarounds necessary to actually accomplish the caller's request.
3476 *
3477 * Unless otherwise noted, spec quotations in this function come from:
3478 *
3479 * Synchronization of the 3D Pipeline > PIPE_CONTROL Command > Programming
3480 * Restrictions for PIPE_CONTROL.
3481 */
3482 static void
3483 iris_emit_raw_pipe_control(struct iris_batch *batch, uint32_t flags,
3484 struct iris_bo *bo, uint32_t offset, uint64_t imm)
3485 {
3486 UNUSED const struct gen_device_info *devinfo = &batch->screen->devinfo;
3487 enum pipe_control_flags post_sync_flags = get_post_sync_flags(flags);
3488 enum pipe_control_flags non_lri_post_sync_flags =
3489 post_sync_flags & ~PIPE_CONTROL_LRI_POST_SYNC_OP;
3490
3491 /* Recursive PIPE_CONTROL workarounds --------------------------------
3492 * (http://knowyourmeme.com/memes/xzibit-yo-dawg)
3493 *
3494 * We do these first because we want to look at the original operation,
3495 * rather than any workarounds we set.
3496 */
3497 if (GEN_GEN == 9 && (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE)) {
3498 /* The PIPE_CONTROL "VF Cache Invalidation Enable" bit description
3499 * lists several workarounds:
3500 *
3501 * "Project: SKL, KBL, BXT
3502 *
3503 * If the VF Cache Invalidation Enable is set to a 1 in a
3504 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields
3505 * sets to 0, with the VF Cache Invalidation Enable set to 0
3506 * needs to be sent prior to the PIPE_CONTROL with VF Cache
3507 * Invalidation Enable set to a 1."
3508 */
3509 iris_emit_raw_pipe_control(batch, 0, NULL, 0, 0);
3510 }
3511
3512 if (GEN_GEN == 9 && IS_COMPUTE_PIPELINE(batch) && post_sync_flags) {
3513 /* Project: SKL / Argument: LRI Post Sync Operation [23]
3514 *
3515 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
3516 * programmed prior to programming a PIPECONTROL command with "LRI
3517 * Post Sync Operation" in GPGPU mode of operation (i.e when
3518 * PIPELINE_SELECT command is set to GPGPU mode of operation)."
3519 *
3520 * The same text exists a few rows below for Post Sync Op.
3521 */
3522 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_CS_STALL, bo, offset, imm);
3523 }
3524
3525 if (GEN_GEN == 10 && (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH)) {
3526 /* Cannonlake:
3527 * "Before sending a PIPE_CONTROL command with bit 12 set, SW must issue
3528 * another PIPE_CONTROL with Render Target Cache Flush Enable (bit 12)
3529 * = 0 and Pipe Control Flush Enable (bit 7) = 1"
3530 */
3531 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_FLUSH_ENABLE, bo,
3532 offset, imm);
3533 }
3534
3535 /* "Flush Types" workarounds ---------------------------------------------
3536 * We do these now because they may add post-sync operations or CS stalls.
3537 */
3538
3539 if (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE) {
3540 /* Project: BDW, SKL+ (stopping at CNL) / Argument: VF Invalidate
3541 *
3542 * "'Post Sync Operation' must be enabled to 'Write Immediate Data' or
3543 * 'Write PS Depth Count' or 'Write Timestamp'."
3544 */
3545 if (!bo) {
3546 flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
3547 post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
3548 non_lri_post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
3549 bo = batch->screen->workaround_bo;
3550 }
3551 }
3552
3553 /* #1130 from Gen10 workarounds page:
3554 *
3555 * "Enable Depth Stall on every Post Sync Op if Render target Cache
3556 * Flush is not enabled in same PIPE CONTROL and Enable Pixel score
3557 * board stall if Render target cache flush is enabled."
3558 *
3559 * Applicable to CNL B0 and C0 steppings only.
3560 *
3561 * The wording here is unclear, and this workaround doesn't look anything
3562 * like the internal bug report recommendations, but leave it be for now...
3563 */
3564 if (GEN_GEN == 10) {
3565 if (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH) {
3566 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
3567 } else if (flags & non_lri_post_sync_flags) {
3568 flags |= PIPE_CONTROL_DEPTH_STALL;
3569 }
3570 }
3571
3572 if (flags & PIPE_CONTROL_DEPTH_STALL) {
3573 /* From the PIPE_CONTROL instruction table, bit 13 (Depth Stall Enable):
3574 *
3575 * "This bit must be DISABLED for operations other than writing
3576 * PS_DEPTH_COUNT."
3577 *
3578 * This seems like nonsense. An Ivybridge workaround requires us to
3579 * emit a PIPE_CONTROL with a depth stall and write immediate post-sync
3580 * operation. Gen8+ requires us to emit depth stalls and depth cache
3581 * flushes together. So, it's hard to imagine this means anything other
3582 * than "we originally intended this to be used for PS_DEPTH_COUNT".
3583 *
3584 * We ignore the supposed restriction and do nothing.
3585 */
3586 }
3587
3588 if (flags & (PIPE_CONTROL_RENDER_TARGET_FLUSH |
3589 PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
3590 /* From the PIPE_CONTROL instruction table, bit 12 and bit 1:
3591 *
3592 * "This bit must be DISABLED for End-of-pipe (Read) fences,
3593 * PS_DEPTH_COUNT or TIMESTAMP queries."
3594 *
3595 * TODO: Implement end-of-pipe checking.
3596 */
3597 assert(!(post_sync_flags & (PIPE_CONTROL_WRITE_DEPTH_COUNT |
3598 PIPE_CONTROL_WRITE_TIMESTAMP)));
3599 }
3600
3601 if (flags & PIPE_CONTROL_STALL_AT_SCOREBOARD) {
3602 /* From the PIPE_CONTROL instruction table, bit 1:
3603 *
3604 * "This bit is ignored if Depth Stall Enable is set.
3605 * Further, the render cache is not flushed even if Write Cache
3606 * Flush Enable bit is set."
3607 *
3608 * We assert that the caller doesn't do this combination, to try and
3609 * prevent mistakes. It shouldn't hurt the GPU, though.
3610 */
3611 assert(!(flags & (PIPE_CONTROL_DEPTH_STALL |
3612 PIPE_CONTROL_RENDER_TARGET_FLUSH)));
3613 }
3614
3615 /* PIPE_CONTROL page workarounds ------------------------------------- */
3616
3617 if (GEN_GEN <= 8 && (flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE)) {
3618 /* From the PIPE_CONTROL page itself:
3619 *
3620 * "IVB, HSW, BDW
3621 * Restriction: Pipe_control with CS-stall bit set must be issued
3622 * before a pipe-control command that has the State Cache
3623 * Invalidate bit set."
3624 */
3625 flags |= PIPE_CONTROL_CS_STALL;
3626 }
3627
3628 if (flags & PIPE_CONTROL_FLUSH_LLC) {
3629 /* From the PIPE_CONTROL instruction table, bit 26 (Flush LLC):
3630 *
3631 * "Project: ALL
3632 * SW must always program Post-Sync Operation to "Write Immediate
3633 * Data" when Flush LLC is set."
3634 *
3635 * For now, we just require the caller to do it.
3636 */
3637 assert(flags & PIPE_CONTROL_WRITE_IMMEDIATE);
3638 }
3639
3640 /* "Post-Sync Operation" workarounds -------------------------------- */
3641
3642 /* Project: All / Argument: Global Snapshot Count Reset [19]
3643 *
3644 * "This bit must not be exercised on any product.
3645 * Requires stall bit ([20] of DW1) set."
3646 *
3647 * We don't use this, so we just assert that it isn't used. The
3648 * PIPE_CONTROL instruction page indicates that they intended this
3649 * as a debug feature and don't think it is useful in production,
3650 * but it may actually be usable, should we ever want to.
3651 */
3652 assert((flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET) == 0);
3653
3654 if (flags & (PIPE_CONTROL_MEDIA_STATE_CLEAR |
3655 PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE)) {
3656 /* Project: All / Arguments:
3657 *
3658 * - Generic Media State Clear [16]
3659 * - Indirect State Pointers Disable [16]
3660 *
3661 * "Requires stall bit ([20] of DW1) set."
3662 *
3663 * Also, the PIPE_CONTROL instruction table, bit 16 (Generic Media
3664 * State Clear) says:
3665 *
3666 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
3667 * programmed prior to programming a PIPECONTROL command with "Media
3668 * State Clear" set in GPGPU mode of operation"
3669 *
3670 * This is a subset of the earlier rule, so there's nothing to do.
3671 */
3672 flags |= PIPE_CONTROL_CS_STALL;
3673 }
3674
3675 if (flags & PIPE_CONTROL_STORE_DATA_INDEX) {
3676 /* Project: All / Argument: Store Data Index
3677 *
3678 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
3679 * than '0'."
3680 *
3681 * For now, we just assert that the caller does this. We might want to
3682 * automatically add a write to the workaround BO...
3683 */
3684 assert(non_lri_post_sync_flags != 0);
3685 }
3686
3687 if (flags & PIPE_CONTROL_SYNC_GFDT) {
3688 /* Project: All / Argument: Sync GFDT
3689 *
3690 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
3691 * than '0' or 0x2520[13] must be set."
3692 *
3693 * For now, we just assert that the caller does this.
3694 */
3695 assert(non_lri_post_sync_flags != 0);
3696 }
3697
3698 if (flags & PIPE_CONTROL_TLB_INVALIDATE) {
3699 /* Project: IVB+ / Argument: TLB inv
3700 *
3701 * "Requires stall bit ([20] of DW1) set."
3702 *
3703 * Also, from the PIPE_CONTROL instruction table:
3704 *
3705 * "Project: SKL+
3706 * Post Sync Operation or CS stall must be set to ensure a TLB
3707 * invalidation occurs. Otherwise no cycle will occur to the TLB
3708 * cache to invalidate."
3709 *
3710 * This is not a subset of the earlier rule, so there's nothing to do.
3711 */
3712 flags |= PIPE_CONTROL_CS_STALL;
3713 }
3714
3715 if (GEN_GEN == 9 && devinfo->gt == 4) {
3716 /* TODO: The big Skylake GT4 post sync op workaround */
3717 }
3718
3719 /* "GPGPU specific workarounds" (both post-sync and flush) ------------ */
3720
3721 if (IS_COMPUTE_PIPELINE(batch)) {
3722 if (GEN_GEN >= 9 && (flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE)) {
3723 /* Project: SKL+ / Argument: Tex Invalidate
3724 * "Requires stall bit ([20] of DW) set for all GPGPU Workloads."
3725 */
3726 flags |= PIPE_CONTROL_CS_STALL;
3727 }
3728
3729 if (GEN_GEN == 8 && (post_sync_flags ||
3730 (flags & (PIPE_CONTROL_NOTIFY_ENABLE |
3731 PIPE_CONTROL_DEPTH_STALL |
3732 PIPE_CONTROL_RENDER_TARGET_FLUSH |
3733 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
3734 PIPE_CONTROL_DATA_CACHE_FLUSH)))) {
3735 /* Project: BDW / Arguments:
3736 *
3737 * - LRI Post Sync Operation [23]
3738 * - Post Sync Op [15:14]
3739 * - Notify En [8]
3740 * - Depth Stall [13]
3741 * - Render Target Cache Flush [12]
3742 * - Depth Cache Flush [0]
3743 * - DC Flush Enable [5]
3744 *
3745 * "Requires stall bit ([20] of DW) set for all GPGPU and Media
3746 * Workloads."
3747 */
3748 flags |= PIPE_CONTROL_CS_STALL;
3749
3750 /* Also, from the PIPE_CONTROL instruction table, bit 20:
3751 *
3752 * "Project: BDW
3753 * This bit must be always set when PIPE_CONTROL command is
3754 * programmed by GPGPU and MEDIA workloads, except for the cases
3755 * when only Read Only Cache Invalidation bits are set (State
3756 * Cache Invalidation Enable, Instruction cache Invalidation
3757 * Enable, Texture Cache Invalidation Enable, Constant Cache
3758 * Invalidation Enable). This is to WA FFDOP CG issue, this WA
3759 * need not implemented when FF_DOP_CG is disable via "Fixed
3760 * Function DOP Clock Gate Disable" bit in RC_PSMI_CTRL register."
3761 *
3762 * It sounds like we could avoid CS stalls in some cases, but we
3763 * don't currently bother. This list isn't exactly the list above,
3764 * either...
3765 */
3766 }
3767 }
3768
3769 /* "Stall" workarounds ----------------------------------------------
3770 * These have to come after the earlier ones because we may have added
3771 * some additional CS stalls above.
3772 */
3773
3774 if (GEN_GEN < 9 && (flags & PIPE_CONTROL_CS_STALL)) {
3775 /* Project: PRE-SKL, VLV, CHV
3776 *
3777 * "[All Stepping][All SKUs]:
3778 *
3779 * One of the following must also be set:
3780 *
3781 * - Render Target Cache Flush Enable ([12] of DW1)
3782 * - Depth Cache Flush Enable ([0] of DW1)
3783 * - Stall at Pixel Scoreboard ([1] of DW1)
3784 * - Depth Stall ([13] of DW1)
3785 * - Post-Sync Operation ([13] of DW1)
3786 * - DC Flush Enable ([5] of DW1)"
3787 *
3788 * If we don't already have one of those bits set, we choose to add
3789 * "Stall at Pixel Scoreboard". Some of the other bits require a
3790 * CS stall as a workaround (see above), which would send us into
3791 * an infinite recursion of PIPE_CONTROLs. "Stall at Pixel Scoreboard"
3792 * appears to be safe, so we choose that.
3793 */
3794 const uint32_t wa_bits = PIPE_CONTROL_RENDER_TARGET_FLUSH |
3795 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
3796 PIPE_CONTROL_WRITE_IMMEDIATE |
3797 PIPE_CONTROL_WRITE_DEPTH_COUNT |
3798 PIPE_CONTROL_WRITE_TIMESTAMP |
3799 PIPE_CONTROL_STALL_AT_SCOREBOARD |
3800 PIPE_CONTROL_DEPTH_STALL |
3801 PIPE_CONTROL_DATA_CACHE_FLUSH;
3802 if (!(flags & wa_bits))
3803 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
3804 }
3805
3806 /* Emit --------------------------------------------------------------- */
3807
3808 iris_emit_cmd(batch, GENX(PIPE_CONTROL), pc) {
3809 pc.LRIPostSyncOperation = NoLRIOperation;
3810 pc.PipeControlFlushEnable = flags & PIPE_CONTROL_FLUSH_ENABLE;
3811 pc.DCFlushEnable = flags & PIPE_CONTROL_DATA_CACHE_FLUSH;
3812 pc.StoreDataIndex = 0;
3813 pc.CommandStreamerStallEnable = flags & PIPE_CONTROL_CS_STALL;
3814 pc.GlobalSnapshotCountReset =
3815 flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET;
3816 pc.TLBInvalidate = flags & PIPE_CONTROL_TLB_INVALIDATE;
3817 pc.GenericMediaStateClear = flags & PIPE_CONTROL_MEDIA_STATE_CLEAR;
3818 pc.StallAtPixelScoreboard = flags & PIPE_CONTROL_STALL_AT_SCOREBOARD;
3819 pc.RenderTargetCacheFlushEnable =
3820 flags & PIPE_CONTROL_RENDER_TARGET_FLUSH;
3821 pc.DepthCacheFlushEnable = flags & PIPE_CONTROL_DEPTH_CACHE_FLUSH;
3822 pc.StateCacheInvalidationEnable =
3823 flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE;
3824 pc.VFCacheInvalidationEnable = flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
3825 pc.ConstantCacheInvalidationEnable =
3826 flags & PIPE_CONTROL_CONST_CACHE_INVALIDATE;
3827 pc.PostSyncOperation = flags_to_post_sync_op(flags);
3828 pc.DepthStallEnable = flags & PIPE_CONTROL_DEPTH_STALL;
3829 pc.InstructionCacheInvalidateEnable =
3830 flags & PIPE_CONTROL_INSTRUCTION_INVALIDATE;
3831 pc.NotifyEnable = flags & PIPE_CONTROL_NOTIFY_ENABLE;
3832 pc.IndirectStatePointersDisable =
3833 flags & PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE;
3834 pc.TextureCacheInvalidationEnable =
3835 flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
3836 pc.Address = ro_bo(bo, offset);
3837 pc.ImmediateData = imm;
3838 }
3839 }
3840
3841 void
3842 genX(init_state)(struct iris_context *ice)
3843 {
3844 struct pipe_context *ctx = &ice->ctx;
3845 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
3846
3847 ctx->create_blend_state = iris_create_blend_state;
3848 ctx->create_depth_stencil_alpha_state = iris_create_zsa_state;
3849 ctx->create_rasterizer_state = iris_create_rasterizer_state;
3850 ctx->create_sampler_state = iris_create_sampler_state;
3851 ctx->create_sampler_view = iris_create_sampler_view;
3852 ctx->create_surface = iris_create_surface;
3853 ctx->create_vertex_elements_state = iris_create_vertex_elements;
3854 ctx->create_compute_state = iris_create_compute_state;
3855 ctx->bind_blend_state = iris_bind_blend_state;
3856 ctx->bind_depth_stencil_alpha_state = iris_bind_zsa_state;
3857 ctx->bind_sampler_states = iris_bind_sampler_states;
3858 ctx->bind_rasterizer_state = iris_bind_rasterizer_state;
3859 ctx->bind_vertex_elements_state = iris_bind_vertex_elements_state;
3860 ctx->bind_compute_state = iris_bind_compute_state;
3861 ctx->delete_blend_state = iris_delete_state;
3862 ctx->delete_depth_stencil_alpha_state = iris_delete_state;
3863 ctx->delete_fs_state = iris_delete_state;
3864 ctx->delete_rasterizer_state = iris_delete_state;
3865 ctx->delete_sampler_state = iris_delete_state;
3866 ctx->delete_vertex_elements_state = iris_delete_state;
3867 ctx->delete_compute_state = iris_delete_state;
3868 ctx->delete_tcs_state = iris_delete_state;
3869 ctx->delete_tes_state = iris_delete_state;
3870 ctx->delete_gs_state = iris_delete_state;
3871 ctx->delete_vs_state = iris_delete_state;
3872 ctx->set_blend_color = iris_set_blend_color;
3873 ctx->set_clip_state = iris_set_clip_state;
3874 ctx->set_constant_buffer = iris_set_constant_buffer;
3875 ctx->set_shader_buffers = iris_set_shader_buffers;
3876 ctx->set_sampler_views = iris_set_sampler_views;
3877 ctx->set_framebuffer_state = iris_set_framebuffer_state;
3878 ctx->set_polygon_stipple = iris_set_polygon_stipple;
3879 ctx->set_sample_mask = iris_set_sample_mask;
3880 ctx->set_scissor_states = iris_set_scissor_states;
3881 ctx->set_stencil_ref = iris_set_stencil_ref;
3882 ctx->set_vertex_buffers = iris_set_vertex_buffers;
3883 ctx->set_viewport_states = iris_set_viewport_states;
3884 ctx->sampler_view_destroy = iris_sampler_view_destroy;
3885 ctx->surface_destroy = iris_surface_destroy;
3886 ctx->draw_vbo = iris_draw_vbo;
3887 ctx->launch_grid = iris_launch_grid;
3888 ctx->create_stream_output_target = iris_create_stream_output_target;
3889 ctx->stream_output_target_destroy = iris_stream_output_target_destroy;
3890 ctx->set_stream_output_targets = iris_set_stream_output_targets;
3891
3892 ice->vtbl.destroy_state = iris_destroy_state;
3893 ice->vtbl.init_render_context = iris_init_render_context;
3894 ice->vtbl.upload_render_state = iris_upload_render_state;
3895 ice->vtbl.emit_raw_pipe_control = iris_emit_raw_pipe_control;
3896 ice->vtbl.derived_program_state_size = iris_derived_program_state_size;
3897 ice->vtbl.store_derived_program_state = iris_store_derived_program_state;
3898 ice->vtbl.create_so_decl_list = iris_create_so_decl_list;
3899 ice->vtbl.populate_vs_key = iris_populate_vs_key;
3900 ice->vtbl.populate_tcs_key = iris_populate_tcs_key;
3901 ice->vtbl.populate_tes_key = iris_populate_tes_key;
3902 ice->vtbl.populate_gs_key = iris_populate_gs_key;
3903 ice->vtbl.populate_fs_key = iris_populate_fs_key;
3904
3905 ice->state.dirty = ~0ull;
3906
3907 ice->state.sample_mask = 0xffff;
3908 ice->state.num_viewports = 1;
3909 ice->state.genx = calloc(1, sizeof(struct iris_genx_state));
3910
3911 /* Make a 1x1x1 null surface for unbound textures */
3912 void *null_surf_map =
3913 upload_state(ice->state.surface_uploader, &ice->state.unbound_tex,
3914 4 * GENX(RENDER_SURFACE_STATE_length), 64);
3915 isl_null_fill_state(&screen->isl_dev, null_surf_map, isl_extent3d(1, 1, 1));
3916 }