iris: Fix NOS mechanism
[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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_state.c
25 *
26 * ============================= GENXML CODE =============================
27 * [This file is compiled once per generation.]
28 * =======================================================================
29 *
30 * This is the main state upload code.
31 *
32 * Gallium uses Constant State Objects, or CSOs, for most state. Large,
33 * complex, or highly reusable state can be created once, and bound and
34 * rebound multiple times. This is modeled with the pipe->create_*_state()
35 * and pipe->bind_*_state() hooks. Highly dynamic or inexpensive state is
36 * streamed out on the fly, via pipe->set_*_state() hooks.
37 *
38 * OpenGL involves frequently mutating context state, which is mirrored in
39 * core Mesa by highly mutable data structures. However, most applications
40 * typically draw the same things over and over - from frame to frame, most
41 * of the same objects are still visible and need to be redrawn. So, rather
42 * than inventing new state all the time, applications usually mutate to swap
43 * between known states that we've seen before.
44 *
45 * Gallium isolates us from this mutation by tracking API state, and
46 * distilling it into a set of Constant State Objects, or CSOs. Large,
47 * complex, or typically reusable state can be created once, then reused
48 * multiple times. Drivers can create and store their own associated data.
49 * This create/bind model corresponds to the pipe->create_*_state() and
50 * pipe->bind_*_state() driver hooks.
51 *
52 * Some state is cheap to create, or expected to be highly dynamic. Rather
53 * than creating and caching piles of CSOs for these, Gallium simply streams
54 * them out, via the pipe->set_*_state() driver hooks.
55 *
56 * To reduce draw time overhead, we try to compute as much state at create
57 * time as possible. Wherever possible, we translate the Gallium pipe state
58 * to 3DSTATE commands, and store those commands in the CSO. At draw time,
59 * we can simply memcpy them into a batch buffer.
60 *
61 * No hardware matches the abstraction perfectly, so some commands require
62 * information from multiple CSOs. In this case, we can store two copies
63 * of the packet (one in each CSO), and simply | together their DWords at
64 * draw time. Sometimes the second set is trivial (one or two fields), so
65 * we simply pack it at draw time.
66 *
67 * There are two main components in the file below. First, the CSO hooks
68 * create/bind/track state. The second are the draw-time upload functions,
69 * iris_upload_render_state() and iris_upload_compute_state(), which read
70 * the context state and emit the commands into the actual batch.
71 */
72
73 #include <stdio.h>
74 #include <errno.h>
75
76 #if HAVE_VALGRIND
77 #include <valgrind.h>
78 #include <memcheck.h>
79 #define VG(x) x
80 #ifndef NDEBUG
81 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
82 #endif
83 #else
84 #define VG(x)
85 #endif
86
87 #include "pipe/p_defines.h"
88 #include "pipe/p_state.h"
89 #include "pipe/p_context.h"
90 #include "pipe/p_screen.h"
91 #include "util/u_inlines.h"
92 #include "util/u_format.h"
93 #include "util/u_framebuffer.h"
94 #include "util/u_transfer.h"
95 #include "util/u_upload_mgr.h"
96 #include "util/u_viewport.h"
97 #include "i915_drm.h"
98 #include "nir.h"
99 #include "intel/compiler/brw_compiler.h"
100 #include "intel/common/gen_l3_config.h"
101 #include "intel/common/gen_sample_positions.h"
102 #include "iris_batch.h"
103 #include "iris_context.h"
104 #include "iris_pipe.h"
105 #include "iris_resource.h"
106
107 #define __gen_address_type struct iris_address
108 #define __gen_user_data struct iris_batch
109
110 #define ARRAY_BYTES(x) (sizeof(uint32_t) * ARRAY_SIZE(x))
111
112 static uint64_t
113 __gen_combine_address(struct iris_batch *batch, void *location,
114 struct iris_address addr, uint32_t delta)
115 {
116 uint64_t result = addr.offset + delta;
117
118 if (addr.bo) {
119 iris_use_pinned_bo(batch, addr.bo, addr.write);
120 /* Assume this is a general address, not relative to a base. */
121 result += addr.bo->gtt_offset;
122 }
123
124 return result;
125 }
126
127 #define __genxml_cmd_length(cmd) cmd ## _length
128 #define __genxml_cmd_length_bias(cmd) cmd ## _length_bias
129 #define __genxml_cmd_header(cmd) cmd ## _header
130 #define __genxml_cmd_pack(cmd) cmd ## _pack
131
132 #define _iris_pack_command(batch, cmd, dst, name) \
133 for (struct cmd name = { __genxml_cmd_header(cmd) }, \
134 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
135 ({ __genxml_cmd_pack(cmd)(batch, (void *)_dst, &name); \
136 _dst = NULL; \
137 }))
138
139 #define iris_pack_command(cmd, dst, name) \
140 _iris_pack_command(NULL, cmd, dst, name)
141
142 #define iris_pack_state(cmd, dst, name) \
143 for (struct cmd name = {}, \
144 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
145 __genxml_cmd_pack(cmd)(NULL, (void *)_dst, &name), \
146 _dst = NULL)
147
148 #define iris_emit_cmd(batch, cmd, name) \
149 _iris_pack_command(batch, cmd, iris_get_command_space(batch, 4 * __genxml_cmd_length(cmd)), name)
150
151 #define iris_emit_merge(batch, dwords0, dwords1, num_dwords) \
152 do { \
153 uint32_t *dw = iris_get_command_space(batch, 4 * num_dwords); \
154 for (uint32_t i = 0; i < num_dwords; i++) \
155 dw[i] = (dwords0)[i] | (dwords1)[i]; \
156 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, num_dwords)); \
157 } while (0)
158
159 #include "genxml/genX_pack.h"
160 #include "genxml/gen_macros.h"
161 #include "genxml/genX_bits.h"
162
163 #define MOCS_WB (2 << 1)
164
165 /**
166 * Statically assert that PIPE_* enums match the hardware packets.
167 * (As long as they match, we don't need to translate them.)
168 */
169 UNUSED static void pipe_asserts()
170 {
171 #define PIPE_ASSERT(x) STATIC_ASSERT((int)x)
172
173 /* pipe_logicop happens to match the hardware. */
174 PIPE_ASSERT(PIPE_LOGICOP_CLEAR == LOGICOP_CLEAR);
175 PIPE_ASSERT(PIPE_LOGICOP_NOR == LOGICOP_NOR);
176 PIPE_ASSERT(PIPE_LOGICOP_AND_INVERTED == LOGICOP_AND_INVERTED);
177 PIPE_ASSERT(PIPE_LOGICOP_COPY_INVERTED == LOGICOP_COPY_INVERTED);
178 PIPE_ASSERT(PIPE_LOGICOP_AND_REVERSE == LOGICOP_AND_REVERSE);
179 PIPE_ASSERT(PIPE_LOGICOP_INVERT == LOGICOP_INVERT);
180 PIPE_ASSERT(PIPE_LOGICOP_XOR == LOGICOP_XOR);
181 PIPE_ASSERT(PIPE_LOGICOP_NAND == LOGICOP_NAND);
182 PIPE_ASSERT(PIPE_LOGICOP_AND == LOGICOP_AND);
183 PIPE_ASSERT(PIPE_LOGICOP_EQUIV == LOGICOP_EQUIV);
184 PIPE_ASSERT(PIPE_LOGICOP_NOOP == LOGICOP_NOOP);
185 PIPE_ASSERT(PIPE_LOGICOP_OR_INVERTED == LOGICOP_OR_INVERTED);
186 PIPE_ASSERT(PIPE_LOGICOP_COPY == LOGICOP_COPY);
187 PIPE_ASSERT(PIPE_LOGICOP_OR_REVERSE == LOGICOP_OR_REVERSE);
188 PIPE_ASSERT(PIPE_LOGICOP_OR == LOGICOP_OR);
189 PIPE_ASSERT(PIPE_LOGICOP_SET == LOGICOP_SET);
190
191 /* pipe_blend_func happens to match the hardware. */
192 PIPE_ASSERT(PIPE_BLENDFACTOR_ONE == BLENDFACTOR_ONE);
193 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_COLOR == BLENDFACTOR_SRC_COLOR);
194 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA == BLENDFACTOR_SRC_ALPHA);
195 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_ALPHA == BLENDFACTOR_DST_ALPHA);
196 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_COLOR == BLENDFACTOR_DST_COLOR);
197 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE == BLENDFACTOR_SRC_ALPHA_SATURATE);
198 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_COLOR == BLENDFACTOR_CONST_COLOR);
199 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_ALPHA == BLENDFACTOR_CONST_ALPHA);
200 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_COLOR == BLENDFACTOR_SRC1_COLOR);
201 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_ALPHA == BLENDFACTOR_SRC1_ALPHA);
202 PIPE_ASSERT(PIPE_BLENDFACTOR_ZERO == BLENDFACTOR_ZERO);
203 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_COLOR == BLENDFACTOR_INV_SRC_COLOR);
204 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_ALPHA == BLENDFACTOR_INV_SRC_ALPHA);
205 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_ALPHA == BLENDFACTOR_INV_DST_ALPHA);
206 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_COLOR == BLENDFACTOR_INV_DST_COLOR);
207 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_COLOR == BLENDFACTOR_INV_CONST_COLOR);
208 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_ALPHA == BLENDFACTOR_INV_CONST_ALPHA);
209 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_COLOR == BLENDFACTOR_INV_SRC1_COLOR);
210 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_ALPHA == BLENDFACTOR_INV_SRC1_ALPHA);
211
212 /* pipe_blend_func happens to match the hardware. */
213 PIPE_ASSERT(PIPE_BLEND_ADD == BLENDFUNCTION_ADD);
214 PIPE_ASSERT(PIPE_BLEND_SUBTRACT == BLENDFUNCTION_SUBTRACT);
215 PIPE_ASSERT(PIPE_BLEND_REVERSE_SUBTRACT == BLENDFUNCTION_REVERSE_SUBTRACT);
216 PIPE_ASSERT(PIPE_BLEND_MIN == BLENDFUNCTION_MIN);
217 PIPE_ASSERT(PIPE_BLEND_MAX == BLENDFUNCTION_MAX);
218
219 /* pipe_stencil_op happens to match the hardware. */
220 PIPE_ASSERT(PIPE_STENCIL_OP_KEEP == STENCILOP_KEEP);
221 PIPE_ASSERT(PIPE_STENCIL_OP_ZERO == STENCILOP_ZERO);
222 PIPE_ASSERT(PIPE_STENCIL_OP_REPLACE == STENCILOP_REPLACE);
223 PIPE_ASSERT(PIPE_STENCIL_OP_INCR == STENCILOP_INCRSAT);
224 PIPE_ASSERT(PIPE_STENCIL_OP_DECR == STENCILOP_DECRSAT);
225 PIPE_ASSERT(PIPE_STENCIL_OP_INCR_WRAP == STENCILOP_INCR);
226 PIPE_ASSERT(PIPE_STENCIL_OP_DECR_WRAP == STENCILOP_DECR);
227 PIPE_ASSERT(PIPE_STENCIL_OP_INVERT == STENCILOP_INVERT);
228
229 /* pipe_sprite_coord_mode happens to match 3DSTATE_SBE */
230 PIPE_ASSERT(PIPE_SPRITE_COORD_UPPER_LEFT == UPPERLEFT);
231 PIPE_ASSERT(PIPE_SPRITE_COORD_LOWER_LEFT == LOWERLEFT);
232 #undef PIPE_ASSERT
233 }
234
235 static unsigned
236 translate_prim_type(enum pipe_prim_type prim, uint8_t verts_per_patch)
237 {
238 static const unsigned map[] = {
239 [PIPE_PRIM_POINTS] = _3DPRIM_POINTLIST,
240 [PIPE_PRIM_LINES] = _3DPRIM_LINELIST,
241 [PIPE_PRIM_LINE_LOOP] = _3DPRIM_LINELOOP,
242 [PIPE_PRIM_LINE_STRIP] = _3DPRIM_LINESTRIP,
243 [PIPE_PRIM_TRIANGLES] = _3DPRIM_TRILIST,
244 [PIPE_PRIM_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
245 [PIPE_PRIM_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
246 [PIPE_PRIM_QUADS] = _3DPRIM_QUADLIST,
247 [PIPE_PRIM_QUAD_STRIP] = _3DPRIM_QUADSTRIP,
248 [PIPE_PRIM_POLYGON] = _3DPRIM_POLYGON,
249 [PIPE_PRIM_LINES_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
250 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
251 [PIPE_PRIM_TRIANGLES_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
252 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
253 [PIPE_PRIM_PATCHES] = _3DPRIM_PATCHLIST_1 - 1,
254 };
255
256 return map[prim] + (prim == PIPE_PRIM_PATCHES ? verts_per_patch : 0);
257 }
258
259 static unsigned
260 translate_compare_func(enum pipe_compare_func pipe_func)
261 {
262 static const unsigned map[] = {
263 [PIPE_FUNC_NEVER] = COMPAREFUNCTION_NEVER,
264 [PIPE_FUNC_LESS] = COMPAREFUNCTION_LESS,
265 [PIPE_FUNC_EQUAL] = COMPAREFUNCTION_EQUAL,
266 [PIPE_FUNC_LEQUAL] = COMPAREFUNCTION_LEQUAL,
267 [PIPE_FUNC_GREATER] = COMPAREFUNCTION_GREATER,
268 [PIPE_FUNC_NOTEQUAL] = COMPAREFUNCTION_NOTEQUAL,
269 [PIPE_FUNC_GEQUAL] = COMPAREFUNCTION_GEQUAL,
270 [PIPE_FUNC_ALWAYS] = COMPAREFUNCTION_ALWAYS,
271 };
272 return map[pipe_func];
273 }
274
275 static unsigned
276 translate_shadow_func(enum pipe_compare_func pipe_func)
277 {
278 /* Gallium specifies the result of shadow comparisons as:
279 *
280 * 1 if ref <op> texel,
281 * 0 otherwise.
282 *
283 * The hardware does:
284 *
285 * 0 if texel <op> ref,
286 * 1 otherwise.
287 *
288 * So we need to flip the operator and also negate.
289 */
290 static const unsigned map[] = {
291 [PIPE_FUNC_NEVER] = PREFILTEROPALWAYS,
292 [PIPE_FUNC_LESS] = PREFILTEROPLEQUAL,
293 [PIPE_FUNC_EQUAL] = PREFILTEROPNOTEQUAL,
294 [PIPE_FUNC_LEQUAL] = PREFILTEROPLESS,
295 [PIPE_FUNC_GREATER] = PREFILTEROPGEQUAL,
296 [PIPE_FUNC_NOTEQUAL] = PREFILTEROPEQUAL,
297 [PIPE_FUNC_GEQUAL] = PREFILTEROPGREATER,
298 [PIPE_FUNC_ALWAYS] = PREFILTEROPNEVER,
299 };
300 return map[pipe_func];
301 }
302
303 static unsigned
304 translate_cull_mode(unsigned pipe_face)
305 {
306 static const unsigned map[4] = {
307 [PIPE_FACE_NONE] = CULLMODE_NONE,
308 [PIPE_FACE_FRONT] = CULLMODE_FRONT,
309 [PIPE_FACE_BACK] = CULLMODE_BACK,
310 [PIPE_FACE_FRONT_AND_BACK] = CULLMODE_BOTH,
311 };
312 return map[pipe_face];
313 }
314
315 static unsigned
316 translate_fill_mode(unsigned pipe_polymode)
317 {
318 static const unsigned map[4] = {
319 [PIPE_POLYGON_MODE_FILL] = FILL_MODE_SOLID,
320 [PIPE_POLYGON_MODE_LINE] = FILL_MODE_WIREFRAME,
321 [PIPE_POLYGON_MODE_POINT] = FILL_MODE_POINT,
322 [PIPE_POLYGON_MODE_FILL_RECTANGLE] = FILL_MODE_SOLID,
323 };
324 return map[pipe_polymode];
325 }
326
327 static unsigned
328 translate_mip_filter(enum pipe_tex_mipfilter pipe_mip)
329 {
330 static const unsigned map[] = {
331 [PIPE_TEX_MIPFILTER_NEAREST] = MIPFILTER_NEAREST,
332 [PIPE_TEX_MIPFILTER_LINEAR] = MIPFILTER_LINEAR,
333 [PIPE_TEX_MIPFILTER_NONE] = MIPFILTER_NONE,
334 };
335 return map[pipe_mip];
336 }
337
338 static uint32_t
339 translate_wrap(unsigned pipe_wrap)
340 {
341 static const unsigned map[] = {
342 [PIPE_TEX_WRAP_REPEAT] = TCM_WRAP,
343 [PIPE_TEX_WRAP_CLAMP] = TCM_HALF_BORDER,
344 [PIPE_TEX_WRAP_CLAMP_TO_EDGE] = TCM_CLAMP,
345 [PIPE_TEX_WRAP_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
346 [PIPE_TEX_WRAP_MIRROR_REPEAT] = TCM_MIRROR,
347 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
348
349 /* These are unsupported. */
350 [PIPE_TEX_WRAP_MIRROR_CLAMP] = -1,
351 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER] = -1,
352 };
353 return map[pipe_wrap];
354 }
355
356 static struct iris_address
357 ro_bo(struct iris_bo *bo, uint64_t offset)
358 {
359 /* CSOs must pass NULL for bo! Otherwise it will add the BO to the
360 * validation list at CSO creation time, instead of draw time.
361 */
362 return (struct iris_address) { .bo = bo, .offset = offset };
363 }
364
365 static struct iris_address
366 rw_bo(struct iris_bo *bo, uint64_t offset)
367 {
368 /* CSOs must pass NULL for bo! Otherwise it will add the BO to the
369 * validation list at CSO creation time, instead of draw time.
370 */
371 return (struct iris_address) { .bo = bo, .offset = offset, .write = true };
372 }
373
374 /**
375 * Allocate space for some indirect state.
376 *
377 * Return a pointer to the map (to fill it out) and a state ref (for
378 * referring to the state in GPU commands).
379 */
380 static void *
381 upload_state(struct u_upload_mgr *uploader,
382 struct iris_state_ref *ref,
383 unsigned size,
384 unsigned alignment)
385 {
386 void *p = NULL;
387 u_upload_alloc(uploader, 0, size, alignment, &ref->offset, &ref->res, &p);
388 return p;
389 }
390
391 /**
392 * Stream out temporary/short-lived state.
393 *
394 * This allocates space, pins the BO, and includes the BO address in the
395 * returned offset (which works because all state lives in 32-bit memory
396 * zones).
397 */
398 static uint32_t *
399 stream_state(struct iris_batch *batch,
400 struct u_upload_mgr *uploader,
401 struct pipe_resource **out_res,
402 unsigned size,
403 unsigned alignment,
404 uint32_t *out_offset)
405 {
406 void *ptr = NULL;
407
408 u_upload_alloc(uploader, 0, size, alignment, out_offset, out_res, &ptr);
409
410 struct iris_bo *bo = iris_resource_bo(*out_res);
411 iris_use_pinned_bo(batch, bo, false);
412
413 *out_offset += iris_bo_offset_from_base_address(bo);
414
415 return ptr;
416 }
417
418 /**
419 * stream_state() + memcpy.
420 */
421 static uint32_t
422 emit_state(struct iris_batch *batch,
423 struct u_upload_mgr *uploader,
424 struct pipe_resource **out_res,
425 const void *data,
426 unsigned size,
427 unsigned alignment)
428 {
429 unsigned offset = 0;
430 uint32_t *map =
431 stream_state(batch, uploader, out_res, size, alignment, &offset);
432
433 if (map)
434 memcpy(map, data, size);
435
436 return offset;
437 }
438
439 /**
440 * Did field 'x' change between 'old_cso' and 'new_cso'?
441 *
442 * (If so, we may want to set some dirty flags.)
443 */
444 #define cso_changed(x) (!old_cso || (old_cso->x != new_cso->x))
445 #define cso_changed_memcmp(x) \
446 (!old_cso || memcmp(old_cso->x, new_cso->x, sizeof(old_cso->x)) != 0)
447
448 static void
449 flush_for_state_base_change(struct iris_batch *batch)
450 {
451 /* Flush before emitting STATE_BASE_ADDRESS.
452 *
453 * This isn't documented anywhere in the PRM. However, it seems to be
454 * necessary prior to changing the surface state base adress. We've
455 * seen issues in Vulkan where we get GPU hangs when using multi-level
456 * command buffers which clear depth, reset state base address, and then
457 * go render stuff.
458 *
459 * Normally, in GL, we would trust the kernel to do sufficient stalls
460 * and flushes prior to executing our batch. However, it doesn't seem
461 * as if the kernel's flushing is always sufficient and we don't want to
462 * rely on it.
463 *
464 * We make this an end-of-pipe sync instead of a normal flush because we
465 * do not know the current status of the GPU. On Haswell at least,
466 * having a fast-clear operation in flight at the same time as a normal
467 * rendering operation can cause hangs. Since the kernel's flushing is
468 * insufficient, we need to ensure that any rendering operations from
469 * other processes are definitely complete before we try to do our own
470 * rendering. It's a bit of a big hammer but it appears to work.
471 */
472 iris_emit_end_of_pipe_sync(batch,
473 PIPE_CONTROL_RENDER_TARGET_FLUSH |
474 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
475 PIPE_CONTROL_DATA_CACHE_FLUSH);
476 }
477
478 static void
479 _iris_emit_lri(struct iris_batch *batch, uint32_t reg, uint32_t val)
480 {
481 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
482 lri.RegisterOffset = reg;
483 lri.DataDWord = val;
484 }
485 }
486 #define iris_emit_lri(b, r, v) _iris_emit_lri(b, GENX(r##_num), v)
487
488 static void
489 _iris_emit_lrr(struct iris_batch *batch, uint32_t src, uint32_t dst)
490 {
491 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_REG), lrr) {
492 lrr.SourceRegisterAddress = src;
493 lrr.DestinationRegisterAddress = dst;
494 }
495 }
496
497 static void
498 emit_pipeline_select(struct iris_batch *batch, uint32_t pipeline)
499 {
500 #if GEN_GEN >= 8 && GEN_GEN < 10
501 /* From the Broadwell PRM, Volume 2a: Instructions, PIPELINE_SELECT:
502 *
503 * Software must clear the COLOR_CALC_STATE Valid field in
504 * 3DSTATE_CC_STATE_POINTERS command prior to send a PIPELINE_SELECT
505 * with Pipeline Select set to GPGPU.
506 *
507 * The internal hardware docs recommend the same workaround for Gen9
508 * hardware too.
509 */
510 if (pipeline == GPGPU)
511 iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), t);
512 #endif
513
514
515 /* From "BXML » GT » MI » vol1a GPU Overview » [Instruction]
516 * PIPELINE_SELECT [DevBWR+]":
517 *
518 * "Project: DEVSNB+
519 *
520 * Software must ensure all the write caches are flushed through a
521 * stalling PIPE_CONTROL command followed by another PIPE_CONTROL
522 * command to invalidate read only caches prior to programming
523 * MI_PIPELINE_SELECT command to change the Pipeline Select Mode."
524 */
525 iris_emit_pipe_control_flush(batch,
526 PIPE_CONTROL_RENDER_TARGET_FLUSH |
527 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
528 PIPE_CONTROL_DATA_CACHE_FLUSH |
529 PIPE_CONTROL_CS_STALL);
530
531 iris_emit_pipe_control_flush(batch,
532 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
533 PIPE_CONTROL_CONST_CACHE_INVALIDATE |
534 PIPE_CONTROL_STATE_CACHE_INVALIDATE |
535 PIPE_CONTROL_INSTRUCTION_INVALIDATE);
536
537 iris_emit_cmd(batch, GENX(PIPELINE_SELECT), sel) {
538 #if GEN_GEN >= 9
539 sel.MaskBits = 3;
540 #endif
541 sel.PipelineSelection = pipeline;
542 }
543 }
544
545 UNUSED static void
546 init_glk_barrier_mode(struct iris_batch *batch, uint32_t value)
547 {
548 #if GEN_GEN == 9
549 /* Project: DevGLK
550 *
551 * "This chicken bit works around a hardware issue with barrier
552 * logic encountered when switching between GPGPU and 3D pipelines.
553 * To workaround the issue, this mode bit should be set after a
554 * pipeline is selected."
555 */
556 uint32_t reg_val;
557 iris_pack_state(GENX(SLICE_COMMON_ECO_CHICKEN1), &reg_val, reg) {
558 reg.GLKBarrierMode = value;
559 reg.GLKBarrierModeMask = 1;
560 }
561 iris_emit_lri(batch, SLICE_COMMON_ECO_CHICKEN1, reg_val);
562 #endif
563 }
564
565 static void
566 init_state_base_address(struct iris_batch *batch)
567 {
568 flush_for_state_base_change(batch);
569
570 /* We program most base addresses once at context initialization time.
571 * Each base address points at a 4GB memory zone, and never needs to
572 * change. See iris_bufmgr.h for a description of the memory zones.
573 *
574 * The one exception is Surface State Base Address, which needs to be
575 * updated occasionally. See iris_binder.c for the details there.
576 */
577 iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
578 #if 0
579 // XXX: MOCS is stupid for this.
580 sba.GeneralStateMemoryObjectControlState = MOCS_WB;
581 sba.StatelessDataPortAccessMemoryObjectControlState = MOCS_WB;
582 sba.DynamicStateMemoryObjectControlState = MOCS_WB;
583 sba.IndirectObjectMemoryObjectControlState = MOCS_WB;
584 sba.InstructionMemoryObjectControlState = MOCS_WB;
585 sba.BindlessSurfaceStateMemoryObjectControlState = MOCS_WB;
586 #endif
587
588 sba.GeneralStateBaseAddressModifyEnable = true;
589 sba.DynamicStateBaseAddressModifyEnable = true;
590 sba.IndirectObjectBaseAddressModifyEnable = true;
591 sba.InstructionBaseAddressModifyEnable = true;
592 sba.GeneralStateBufferSizeModifyEnable = true;
593 sba.DynamicStateBufferSizeModifyEnable = true;
594 sba.BindlessSurfaceStateBaseAddressModifyEnable = true;
595 sba.IndirectObjectBufferSizeModifyEnable = true;
596 sba.InstructionBuffersizeModifyEnable = true;
597
598 sba.InstructionBaseAddress = ro_bo(NULL, IRIS_MEMZONE_SHADER_START);
599 sba.DynamicStateBaseAddress = ro_bo(NULL, IRIS_MEMZONE_DYNAMIC_START);
600
601 sba.GeneralStateBufferSize = 0xfffff;
602 sba.IndirectObjectBufferSize = 0xfffff;
603 sba.InstructionBufferSize = 0xfffff;
604 sba.DynamicStateBufferSize = 0xfffff;
605 }
606 }
607
608 /**
609 * Upload the initial GPU state for a render context.
610 *
611 * This sets some invariant state that needs to be programmed a particular
612 * way, but we never actually change.
613 */
614 static void
615 iris_init_render_context(struct iris_screen *screen,
616 struct iris_batch *batch,
617 struct iris_vtable *vtbl,
618 struct pipe_debug_callback *dbg)
619 {
620 UNUSED const struct gen_device_info *devinfo = &screen->devinfo;
621 uint32_t reg_val;
622
623 emit_pipeline_select(batch, _3D);
624
625 init_state_base_address(batch);
626
627 // XXX: INSTPM on Gen8
628 iris_pack_state(GENX(CS_DEBUG_MODE2), &reg_val, reg) {
629 reg.CONSTANT_BUFFERAddressOffsetDisable = true;
630 reg.CONSTANT_BUFFERAddressOffsetDisableMask = true;
631 }
632 iris_emit_lri(batch, CS_DEBUG_MODE2, reg_val);
633
634 #if GEN_GEN == 9
635 iris_pack_state(GENX(CACHE_MODE_1), &reg_val, reg) {
636 reg.FloatBlendOptimizationEnable = true;
637 reg.FloatBlendOptimizationEnableMask = true;
638 reg.PartialResolveDisableInVC = true;
639 reg.PartialResolveDisableInVCMask = true;
640 }
641 iris_emit_lri(batch, CACHE_MODE_1, reg_val);
642
643 if (devinfo->is_geminilake)
644 init_glk_barrier_mode(batch, GLK_BARRIER_MODE_3D_HULL);
645 #endif
646
647 #if GEN_GEN == 11
648 iris_pack_state(GENX(SAMPLER_MODE), &reg_val, reg) {
649 reg.HeaderlessMessageforPreemptableContexts = 1;
650 reg.HeaderlessMessageforPreemptableContextsMask = 1;
651 }
652 iris_emit_lri(batch, SAMPLER_MODE, reg_val);
653
654 // XXX: 3D_MODE?
655 #endif
656
657 /* 3DSTATE_DRAWING_RECTANGLE is non-pipelined, so we want to avoid
658 * changing it dynamically. We set it to the maximum size here, and
659 * instead include the render target dimensions in the viewport, so
660 * viewport extents clipping takes care of pruning stray geometry.
661 */
662 iris_emit_cmd(batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
663 rect.ClippedDrawingRectangleXMax = UINT16_MAX;
664 rect.ClippedDrawingRectangleYMax = UINT16_MAX;
665 }
666
667 /* Set the initial MSAA sample positions. */
668 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_PATTERN), pat) {
669 GEN_SAMPLE_POS_1X(pat._1xSample);
670 GEN_SAMPLE_POS_2X(pat._2xSample);
671 GEN_SAMPLE_POS_4X(pat._4xSample);
672 GEN_SAMPLE_POS_8X(pat._8xSample);
673 GEN_SAMPLE_POS_16X(pat._16xSample);
674 }
675
676 /* Use the legacy AA line coverage computation. */
677 iris_emit_cmd(batch, GENX(3DSTATE_AA_LINE_PARAMETERS), foo);
678
679 /* Disable chromakeying (it's for media) */
680 iris_emit_cmd(batch, GENX(3DSTATE_WM_CHROMAKEY), foo);
681
682 /* We want regular rendering, not special HiZ operations. */
683 iris_emit_cmd(batch, GENX(3DSTATE_WM_HZ_OP), foo);
684
685 /* No polygon stippling offsets are necessary. */
686 // XXX: may need to set an offset for origin-UL framebuffers
687 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_OFFSET), foo);
688
689 /* Set a static partitioning of the push constant area. */
690 // XXX: this may be a bad idea...could starve the push ringbuffers...
691 for (int i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
692 iris_emit_cmd(batch, GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
693 alloc._3DCommandSubOpcode = 18 + i;
694 alloc.ConstantBufferOffset = 6 * i;
695 alloc.ConstantBufferSize = i == MESA_SHADER_FRAGMENT ? 8 : 6;
696 }
697 }
698 }
699
700 static void
701 iris_init_compute_context(struct iris_screen *screen,
702 struct iris_batch *batch,
703 struct iris_vtable *vtbl,
704 struct pipe_debug_callback *dbg)
705 {
706 UNUSED const struct gen_device_info *devinfo = &screen->devinfo;
707
708 emit_pipeline_select(batch, GPGPU);
709
710 const bool has_slm = true;
711 const bool wants_dc_cache = true;
712
713 const struct gen_l3_weights w =
714 gen_get_default_l3_weights(devinfo, wants_dc_cache, has_slm);
715 const struct gen_l3_config *cfg = gen_get_l3_config(devinfo, w);
716
717 uint32_t reg_val;
718 iris_pack_state(GENX(L3CNTLREG), &reg_val, reg) {
719 reg.SLMEnable = has_slm;
720 #if GEN_GEN == 11
721 /* WA_1406697149: Bit 9 "Error Detection Behavior Control" must be set
722 * in L3CNTLREG register. The default setting of the bit is not the
723 * desirable behavior.
724 */
725 reg.ErrorDetectionBehaviorControl = true;
726 #endif
727 reg.URBAllocation = cfg->n[GEN_L3P_URB];
728 reg.ROAllocation = cfg->n[GEN_L3P_RO];
729 reg.DCAllocation = cfg->n[GEN_L3P_DC];
730 reg.AllAllocation = cfg->n[GEN_L3P_ALL];
731 }
732 iris_emit_lri(batch, L3CNTLREG, reg_val);
733
734 init_state_base_address(batch);
735
736 #if GEN_GEN == 9
737 if (devinfo->is_geminilake)
738 init_glk_barrier_mode(batch, GLK_BARRIER_MODE_GPGPU);
739 #endif
740 }
741
742 struct iris_vertex_buffer_state {
743 /** The 3DSTATE_VERTEX_BUFFERS hardware packet. */
744 uint32_t vertex_buffers[1 + 33 * GENX(VERTEX_BUFFER_STATE_length)];
745
746 /** The resource to source vertex data from. */
747 struct pipe_resource *resources[33];
748
749 /** The number of bound vertex buffers. */
750 unsigned num_buffers;
751 };
752
753 struct iris_depth_buffer_state {
754 /* Depth/HiZ/Stencil related hardware packets. */
755 uint32_t packets[GENX(3DSTATE_DEPTH_BUFFER_length) +
756 GENX(3DSTATE_STENCIL_BUFFER_length) +
757 GENX(3DSTATE_HIER_DEPTH_BUFFER_length) +
758 GENX(3DSTATE_CLEAR_PARAMS_length)];
759 };
760
761 /**
762 * Generation-specific context state (ice->state.genx->...).
763 *
764 * Most state can go in iris_context directly, but these encode hardware
765 * packets which vary by generation.
766 */
767 struct iris_genx_state {
768 /** SF_CLIP_VIEWPORT */
769 uint32_t sf_cl_vp[GENX(SF_CLIP_VIEWPORT_length) * IRIS_MAX_VIEWPORTS];
770
771 struct iris_vertex_buffer_state vertex_buffers;
772 struct iris_depth_buffer_state depth_buffer;
773
774 uint32_t so_buffers[4 * GENX(3DSTATE_SO_BUFFER_length)];
775 uint32_t streamout[4 * GENX(3DSTATE_STREAMOUT_length)];
776 };
777
778 /**
779 * The pipe->set_blend_color() driver hook.
780 *
781 * This corresponds to our COLOR_CALC_STATE.
782 */
783 static void
784 iris_set_blend_color(struct pipe_context *ctx,
785 const struct pipe_blend_color *state)
786 {
787 struct iris_context *ice = (struct iris_context *) ctx;
788
789 /* Our COLOR_CALC_STATE is exactly pipe_blend_color, so just memcpy */
790 memcpy(&ice->state.blend_color, state, sizeof(struct pipe_blend_color));
791 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
792 }
793
794 /**
795 * Gallium CSO for blend state (see pipe_blend_state).
796 */
797 struct iris_blend_state {
798 /** Partial 3DSTATE_PS_BLEND */
799 uint32_t ps_blend[GENX(3DSTATE_PS_BLEND_length)];
800
801 /** Partial BLEND_STATE */
802 uint32_t blend_state[GENX(BLEND_STATE_length) +
803 BRW_MAX_DRAW_BUFFERS * GENX(BLEND_STATE_ENTRY_length)];
804
805 bool alpha_to_coverage; /* for shader key */
806 };
807
808 /**
809 * The pipe->create_blend_state() driver hook.
810 *
811 * Translates a pipe_blend_state into iris_blend_state.
812 */
813 static void *
814 iris_create_blend_state(struct pipe_context *ctx,
815 const struct pipe_blend_state *state)
816 {
817 struct iris_blend_state *cso = malloc(sizeof(struct iris_blend_state));
818 uint32_t *blend_entry = cso->blend_state + GENX(BLEND_STATE_length);
819
820 cso->alpha_to_coverage = state->alpha_to_coverage;
821
822 bool indep_alpha_blend = false;
823
824 for (int i = 0; i < BRW_MAX_DRAW_BUFFERS; i++) {
825 const struct pipe_rt_blend_state *rt =
826 &state->rt[state->independent_blend_enable ? i : 0];
827
828 if (rt->rgb_func != rt->alpha_func ||
829 rt->rgb_src_factor != rt->alpha_src_factor ||
830 rt->rgb_dst_factor != rt->alpha_dst_factor)
831 indep_alpha_blend = true;
832
833 iris_pack_state(GENX(BLEND_STATE_ENTRY), blend_entry, be) {
834 be.LogicOpEnable = state->logicop_enable;
835 be.LogicOpFunction = state->logicop_func;
836
837 be.PreBlendSourceOnlyClampEnable = false;
838 be.ColorClampRange = COLORCLAMP_RTFORMAT;
839 be.PreBlendColorClampEnable = true;
840 be.PostBlendColorClampEnable = true;
841
842 be.ColorBufferBlendEnable = rt->blend_enable;
843
844 be.ColorBlendFunction = rt->rgb_func;
845 be.AlphaBlendFunction = rt->alpha_func;
846 be.SourceBlendFactor = rt->rgb_src_factor;
847 be.SourceAlphaBlendFactor = rt->alpha_src_factor;
848 be.DestinationBlendFactor = rt->rgb_dst_factor;
849 be.DestinationAlphaBlendFactor = rt->alpha_dst_factor;
850
851 be.WriteDisableRed = !(rt->colormask & PIPE_MASK_R);
852 be.WriteDisableGreen = !(rt->colormask & PIPE_MASK_G);
853 be.WriteDisableBlue = !(rt->colormask & PIPE_MASK_B);
854 be.WriteDisableAlpha = !(rt->colormask & PIPE_MASK_A);
855 }
856 blend_entry += GENX(BLEND_STATE_ENTRY_length);
857 }
858
859 iris_pack_command(GENX(3DSTATE_PS_BLEND), cso->ps_blend, pb) {
860 /* pb.HasWriteableRT is filled in at draw time. */
861 /* pb.AlphaTestEnable is filled in at draw time. */
862 pb.AlphaToCoverageEnable = state->alpha_to_coverage;
863 pb.IndependentAlphaBlendEnable = indep_alpha_blend;
864
865 pb.ColorBufferBlendEnable = state->rt[0].blend_enable;
866
867 pb.SourceBlendFactor = state->rt[0].rgb_src_factor;
868 pb.SourceAlphaBlendFactor = state->rt[0].alpha_src_factor;
869 pb.DestinationBlendFactor = state->rt[0].rgb_dst_factor;
870 pb.DestinationAlphaBlendFactor = state->rt[0].alpha_dst_factor;
871 }
872
873 iris_pack_state(GENX(BLEND_STATE), cso->blend_state, bs) {
874 bs.AlphaToCoverageEnable = state->alpha_to_coverage;
875 bs.IndependentAlphaBlendEnable = indep_alpha_blend;
876 bs.AlphaToOneEnable = state->alpha_to_one;
877 bs.AlphaToCoverageDitherEnable = state->alpha_to_coverage;
878 bs.ColorDitherEnable = state->dither;
879 /* bl.AlphaTestEnable and bs.AlphaTestFunction are filled in later. */
880 }
881
882
883 return cso;
884 }
885
886 /**
887 * The pipe->bind_blend_state() driver hook.
888 *
889 * Bind a blending CSO and flag related dirty bits.
890 */
891 static void
892 iris_bind_blend_state(struct pipe_context *ctx, void *state)
893 {
894 struct iris_context *ice = (struct iris_context *) ctx;
895 ice->state.cso_blend = state;
896 ice->state.dirty |= IRIS_DIRTY_PS_BLEND;
897 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
898 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_BLEND];
899 }
900
901 /**
902 * Gallium CSO for depth, stencil, and alpha testing state.
903 */
904 struct iris_depth_stencil_alpha_state {
905 /** Partial 3DSTATE_WM_DEPTH_STENCIL. */
906 uint32_t wmds[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
907
908 /** Outbound to BLEND_STATE, 3DSTATE_PS_BLEND, COLOR_CALC_STATE. */
909 struct pipe_alpha_state alpha;
910
911 /** Outbound to resolve and cache set tracking. */
912 bool depth_writes_enabled;
913 bool stencil_writes_enabled;
914 };
915
916 /**
917 * The pipe->create_depth_stencil_alpha_state() driver hook.
918 *
919 * We encode most of 3DSTATE_WM_DEPTH_STENCIL, and just save off the alpha
920 * testing state since we need pieces of it in a variety of places.
921 */
922 static void *
923 iris_create_zsa_state(struct pipe_context *ctx,
924 const struct pipe_depth_stencil_alpha_state *state)
925 {
926 struct iris_depth_stencil_alpha_state *cso =
927 malloc(sizeof(struct iris_depth_stencil_alpha_state));
928
929 bool two_sided_stencil = state->stencil[1].enabled;
930
931 cso->alpha = state->alpha;
932 cso->depth_writes_enabled = state->depth.writemask;
933 cso->stencil_writes_enabled =
934 state->stencil[0].writemask != 0 ||
935 (two_sided_stencil && state->stencil[1].writemask != 1);
936
937 /* The state tracker needs to optimize away EQUAL writes for us. */
938 assert(!(state->depth.func == PIPE_FUNC_EQUAL && state->depth.writemask));
939
940 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), cso->wmds, wmds) {
941 wmds.StencilFailOp = state->stencil[0].fail_op;
942 wmds.StencilPassDepthFailOp = state->stencil[0].zfail_op;
943 wmds.StencilPassDepthPassOp = state->stencil[0].zpass_op;
944 wmds.StencilTestFunction =
945 translate_compare_func(state->stencil[0].func);
946 wmds.BackfaceStencilFailOp = state->stencil[1].fail_op;
947 wmds.BackfaceStencilPassDepthFailOp = state->stencil[1].zfail_op;
948 wmds.BackfaceStencilPassDepthPassOp = state->stencil[1].zpass_op;
949 wmds.BackfaceStencilTestFunction =
950 translate_compare_func(state->stencil[1].func);
951 wmds.DepthTestFunction = translate_compare_func(state->depth.func);
952 wmds.DoubleSidedStencilEnable = two_sided_stencil;
953 wmds.StencilTestEnable = state->stencil[0].enabled;
954 wmds.StencilBufferWriteEnable =
955 state->stencil[0].writemask != 0 ||
956 (two_sided_stencil && state->stencil[1].writemask != 0);
957 wmds.DepthTestEnable = state->depth.enabled;
958 wmds.DepthBufferWriteEnable = state->depth.writemask;
959 wmds.StencilTestMask = state->stencil[0].valuemask;
960 wmds.StencilWriteMask = state->stencil[0].writemask;
961 wmds.BackfaceStencilTestMask = state->stencil[1].valuemask;
962 wmds.BackfaceStencilWriteMask = state->stencil[1].writemask;
963 /* wmds.[Backface]StencilReferenceValue are merged later */
964 }
965
966 return cso;
967 }
968
969 /**
970 * The pipe->bind_depth_stencil_alpha_state() driver hook.
971 *
972 * Bind a depth/stencil/alpha CSO and flag related dirty bits.
973 */
974 static void
975 iris_bind_zsa_state(struct pipe_context *ctx, void *state)
976 {
977 struct iris_context *ice = (struct iris_context *) ctx;
978 struct iris_depth_stencil_alpha_state *old_cso = ice->state.cso_zsa;
979 struct iris_depth_stencil_alpha_state *new_cso = state;
980
981 if (new_cso) {
982 if (cso_changed(alpha.ref_value))
983 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
984
985 if (cso_changed(alpha.enabled))
986 ice->state.dirty |= IRIS_DIRTY_PS_BLEND | IRIS_DIRTY_BLEND_STATE;
987
988 if (cso_changed(alpha.func))
989 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
990
991 ice->state.depth_writes_enabled = new_cso->depth_writes_enabled;
992 ice->state.stencil_writes_enabled = new_cso->stencil_writes_enabled;
993 }
994
995 ice->state.cso_zsa = new_cso;
996 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
997 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
998 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_DEPTH_STENCIL_ALPHA];
999 }
1000
1001 /**
1002 * Gallium CSO for rasterizer state.
1003 */
1004 struct iris_rasterizer_state {
1005 uint32_t sf[GENX(3DSTATE_SF_length)];
1006 uint32_t clip[GENX(3DSTATE_CLIP_length)];
1007 uint32_t raster[GENX(3DSTATE_RASTER_length)];
1008 uint32_t wm[GENX(3DSTATE_WM_length)];
1009 uint32_t line_stipple[GENX(3DSTATE_LINE_STIPPLE_length)];
1010
1011 uint8_t num_clip_plane_consts;
1012 bool clip_halfz; /* for CC_VIEWPORT */
1013 bool depth_clip_near; /* for CC_VIEWPORT */
1014 bool depth_clip_far; /* for CC_VIEWPORT */
1015 bool flatshade; /* for shader state */
1016 bool flatshade_first; /* for stream output */
1017 bool clamp_fragment_color; /* for shader state */
1018 bool light_twoside; /* for shader state */
1019 bool rasterizer_discard; /* for 3DSTATE_STREAMOUT */
1020 bool half_pixel_center; /* for 3DSTATE_MULTISAMPLE */
1021 bool line_stipple_enable;
1022 bool poly_stipple_enable;
1023 bool multisample;
1024 bool force_persample_interp;
1025 enum pipe_sprite_coord_mode sprite_coord_mode; /* PIPE_SPRITE_* */
1026 uint16_t sprite_coord_enable;
1027 };
1028
1029 static float
1030 get_line_width(const struct pipe_rasterizer_state *state)
1031 {
1032 float line_width = state->line_width;
1033
1034 /* From the OpenGL 4.4 spec:
1035 *
1036 * "The actual width of non-antialiased lines is determined by rounding
1037 * the supplied width to the nearest integer, then clamping it to the
1038 * implementation-dependent maximum non-antialiased line width."
1039 */
1040 if (!state->multisample && !state->line_smooth)
1041 line_width = roundf(state->line_width);
1042
1043 if (!state->multisample && state->line_smooth && line_width < 1.5f) {
1044 /* For 1 pixel line thickness or less, the general anti-aliasing
1045 * algorithm gives up, and a garbage line is generated. Setting a
1046 * Line Width of 0.0 specifies the rasterization of the "thinnest"
1047 * (one-pixel-wide), non-antialiased lines.
1048 *
1049 * Lines rendered with zero Line Width are rasterized using the
1050 * "Grid Intersection Quantization" rules as specified by the
1051 * "Zero-Width (Cosmetic) Line Rasterization" section of the docs.
1052 */
1053 line_width = 0.0f;
1054 }
1055
1056 return line_width;
1057 }
1058
1059 /**
1060 * The pipe->create_rasterizer_state() driver hook.
1061 */
1062 static void *
1063 iris_create_rasterizer_state(struct pipe_context *ctx,
1064 const struct pipe_rasterizer_state *state)
1065 {
1066 struct iris_rasterizer_state *cso =
1067 malloc(sizeof(struct iris_rasterizer_state));
1068
1069 #if 0
1070 point_quad_rasterization -> SBE?
1071
1072 not necessary?
1073 {
1074 poly_smooth
1075 bottom_edge_rule
1076
1077 offset_units_unscaled - cap not exposed
1078 }
1079 #endif
1080
1081 // XXX: it may make more sense just to store the pipe_rasterizer_state,
1082 // we're copying a lot of booleans here. But we don't need all of them...
1083
1084 cso->multisample = state->multisample;
1085 cso->force_persample_interp = state->force_persample_interp;
1086 cso->clip_halfz = state->clip_halfz;
1087 cso->depth_clip_near = state->depth_clip_near;
1088 cso->depth_clip_far = state->depth_clip_far;
1089 cso->flatshade = state->flatshade;
1090 cso->flatshade_first = state->flatshade_first;
1091 cso->clamp_fragment_color = state->clamp_fragment_color;
1092 cso->light_twoside = state->light_twoside;
1093 cso->rasterizer_discard = state->rasterizer_discard;
1094 cso->half_pixel_center = state->half_pixel_center;
1095 cso->sprite_coord_mode = state->sprite_coord_mode;
1096 cso->sprite_coord_enable = state->sprite_coord_enable;
1097 cso->line_stipple_enable = state->line_stipple_enable;
1098 cso->poly_stipple_enable = state->poly_stipple_enable;
1099
1100 if (state->clip_plane_enable != 0)
1101 cso->num_clip_plane_consts = util_logbase2(state->clip_plane_enable) + 1;
1102 else
1103 cso->num_clip_plane_consts = 0;
1104
1105 float line_width = get_line_width(state);
1106
1107 iris_pack_command(GENX(3DSTATE_SF), cso->sf, sf) {
1108 sf.StatisticsEnable = true;
1109 sf.ViewportTransformEnable = true;
1110 sf.AALineDistanceMode = AALINEDISTANCE_TRUE;
1111 sf.LineEndCapAntialiasingRegionWidth =
1112 state->line_smooth ? _10pixels : _05pixels;
1113 sf.LastPixelEnable = state->line_last_pixel;
1114 sf.LineWidth = line_width;
1115 sf.SmoothPointEnable = state->point_smooth || state->multisample;
1116 sf.PointWidthSource = state->point_size_per_vertex ? Vertex : State;
1117 sf.PointWidth = state->point_size;
1118
1119 if (state->flatshade_first) {
1120 sf.TriangleFanProvokingVertexSelect = 1;
1121 } else {
1122 sf.TriangleStripListProvokingVertexSelect = 2;
1123 sf.TriangleFanProvokingVertexSelect = 2;
1124 sf.LineStripListProvokingVertexSelect = 1;
1125 }
1126 }
1127
1128 iris_pack_command(GENX(3DSTATE_RASTER), cso->raster, rr) {
1129 rr.FrontWinding = state->front_ccw ? CounterClockwise : Clockwise;
1130 rr.CullMode = translate_cull_mode(state->cull_face);
1131 rr.FrontFaceFillMode = translate_fill_mode(state->fill_front);
1132 rr.BackFaceFillMode = translate_fill_mode(state->fill_back);
1133 rr.DXMultisampleRasterizationEnable = state->multisample;
1134 rr.GlobalDepthOffsetEnableSolid = state->offset_tri;
1135 rr.GlobalDepthOffsetEnableWireframe = state->offset_line;
1136 rr.GlobalDepthOffsetEnablePoint = state->offset_point;
1137 rr.GlobalDepthOffsetConstant = state->offset_units * 2;
1138 rr.GlobalDepthOffsetScale = state->offset_scale;
1139 rr.GlobalDepthOffsetClamp = state->offset_clamp;
1140 rr.SmoothPointEnable = state->point_smooth || state->multisample;
1141 rr.AntialiasingEnable = state->line_smooth;
1142 rr.ScissorRectangleEnable = state->scissor;
1143 rr.ViewportZNearClipTestEnable = state->depth_clip_near;
1144 rr.ViewportZFarClipTestEnable = state->depth_clip_far;
1145 //rr.ConservativeRasterizationEnable = not yet supported by Gallium...
1146 }
1147
1148 iris_pack_command(GENX(3DSTATE_CLIP), cso->clip, cl) {
1149 /* cl.NonPerspectiveBarycentricEnable is filled in at draw time from
1150 * the FS program; cl.ForceZeroRTAIndexEnable is filled in from the FB.
1151 */
1152 cl.EarlyCullEnable = true;
1153 cl.UserClipDistanceClipTestEnableBitmask = state->clip_plane_enable;
1154 cl.ForceUserClipDistanceClipTestEnableBitmask = true;
1155 cl.APIMode = state->clip_halfz ? APIMODE_D3D : APIMODE_OGL;
1156 cl.GuardbandClipTestEnable = true;
1157 cl.ClipEnable = true;
1158 cl.ViewportXYClipTestEnable = state->point_tri_clip;
1159 cl.MinimumPointWidth = 0.125;
1160 cl.MaximumPointWidth = 255.875;
1161
1162 if (state->flatshade_first) {
1163 cl.TriangleFanProvokingVertexSelect = 1;
1164 } else {
1165 cl.TriangleStripListProvokingVertexSelect = 2;
1166 cl.TriangleFanProvokingVertexSelect = 2;
1167 cl.LineStripListProvokingVertexSelect = 1;
1168 }
1169 }
1170
1171 iris_pack_command(GENX(3DSTATE_WM), cso->wm, wm) {
1172 /* wm.BarycentricInterpolationMode and wm.EarlyDepthStencilControl are
1173 * filled in at draw time from the FS program.
1174 */
1175 wm.LineAntialiasingRegionWidth = _10pixels;
1176 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
1177 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
1178 wm.LineStippleEnable = state->line_stipple_enable;
1179 wm.PolygonStippleEnable = state->poly_stipple_enable;
1180 }
1181
1182 /* Remap from 0..255 back to 1..256 */
1183 const unsigned line_stipple_factor = state->line_stipple_factor + 1;
1184
1185 iris_pack_command(GENX(3DSTATE_LINE_STIPPLE), cso->line_stipple, line) {
1186 line.LineStipplePattern = state->line_stipple_pattern;
1187 line.LineStippleInverseRepeatCount = 1.0f / line_stipple_factor;
1188 line.LineStippleRepeatCount = line_stipple_factor;
1189 }
1190
1191 return cso;
1192 }
1193
1194 /**
1195 * The pipe->bind_rasterizer_state() driver hook.
1196 *
1197 * Bind a rasterizer CSO and flag related dirty bits.
1198 */
1199 static void
1200 iris_bind_rasterizer_state(struct pipe_context *ctx, void *state)
1201 {
1202 struct iris_context *ice = (struct iris_context *) ctx;
1203 struct iris_rasterizer_state *old_cso = ice->state.cso_rast;
1204 struct iris_rasterizer_state *new_cso = state;
1205
1206 if (new_cso) {
1207 /* Try to avoid re-emitting 3DSTATE_LINE_STIPPLE, it's non-pipelined */
1208 if (cso_changed_memcmp(line_stipple))
1209 ice->state.dirty |= IRIS_DIRTY_LINE_STIPPLE;
1210
1211 if (cso_changed(half_pixel_center))
1212 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
1213
1214 if (cso_changed(line_stipple_enable) || cso_changed(poly_stipple_enable))
1215 ice->state.dirty |= IRIS_DIRTY_WM;
1216
1217 if (cso_changed(rasterizer_discard) || cso_changed(flatshade_first))
1218 ice->state.dirty |= IRIS_DIRTY_STREAMOUT;
1219
1220 if (cso_changed(depth_clip_near) || cso_changed(depth_clip_far) ||
1221 cso_changed(clip_halfz))
1222 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
1223
1224 if (cso_changed(sprite_coord_enable) ||
1225 cso_changed(sprite_coord_mode) ||
1226 cso_changed(light_twoside))
1227 ice->state.dirty |= IRIS_DIRTY_SBE;
1228 }
1229
1230 ice->state.cso_rast = new_cso;
1231 ice->state.dirty |= IRIS_DIRTY_RASTER;
1232 ice->state.dirty |= IRIS_DIRTY_CLIP;
1233 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_RASTERIZER];
1234 }
1235
1236 /**
1237 * Return true if the given wrap mode requires the border color to exist.
1238 *
1239 * (We can skip uploading it if the sampler isn't going to use it.)
1240 */
1241 static bool
1242 wrap_mode_needs_border_color(unsigned wrap_mode)
1243 {
1244 return wrap_mode == TCM_CLAMP_BORDER || wrap_mode == TCM_HALF_BORDER;
1245 }
1246
1247 /**
1248 * Gallium CSO for sampler state.
1249 */
1250 struct iris_sampler_state {
1251 union pipe_color_union border_color;
1252 bool needs_border_color;
1253
1254 uint32_t sampler_state[GENX(SAMPLER_STATE_length)];
1255 };
1256
1257 /**
1258 * The pipe->create_sampler_state() driver hook.
1259 *
1260 * We fill out SAMPLER_STATE (except for the border color pointer), and
1261 * store that on the CPU. It doesn't make sense to upload it to a GPU
1262 * buffer object yet, because 3DSTATE_SAMPLER_STATE_POINTERS requires
1263 * all bound sampler states to be in contiguous memor.
1264 */
1265 static void *
1266 iris_create_sampler_state(struct pipe_context *ctx,
1267 const struct pipe_sampler_state *state)
1268 {
1269 struct iris_sampler_state *cso = CALLOC_STRUCT(iris_sampler_state);
1270
1271 if (!cso)
1272 return NULL;
1273
1274 STATIC_ASSERT(PIPE_TEX_FILTER_NEAREST == MAPFILTER_NEAREST);
1275 STATIC_ASSERT(PIPE_TEX_FILTER_LINEAR == MAPFILTER_LINEAR);
1276
1277 unsigned wrap_s = translate_wrap(state->wrap_s);
1278 unsigned wrap_t = translate_wrap(state->wrap_t);
1279 unsigned wrap_r = translate_wrap(state->wrap_r);
1280
1281 memcpy(&cso->border_color, &state->border_color, sizeof(cso->border_color));
1282
1283 cso->needs_border_color = wrap_mode_needs_border_color(wrap_s) ||
1284 wrap_mode_needs_border_color(wrap_t) ||
1285 wrap_mode_needs_border_color(wrap_r);
1286
1287 float min_lod = state->min_lod;
1288 unsigned mag_img_filter = state->mag_img_filter;
1289
1290 // XXX: explain this code ported from ilo...I don't get it at all...
1291 if (state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE &&
1292 state->min_lod > 0.0f) {
1293 min_lod = 0.0f;
1294 mag_img_filter = state->min_img_filter;
1295 }
1296
1297 iris_pack_state(GENX(SAMPLER_STATE), cso->sampler_state, samp) {
1298 samp.TCXAddressControlMode = wrap_s;
1299 samp.TCYAddressControlMode = wrap_t;
1300 samp.TCZAddressControlMode = wrap_r;
1301 samp.CubeSurfaceControlMode = state->seamless_cube_map;
1302 samp.NonnormalizedCoordinateEnable = !state->normalized_coords;
1303 samp.MinModeFilter = state->min_img_filter;
1304 samp.MagModeFilter = mag_img_filter;
1305 samp.MipModeFilter = translate_mip_filter(state->min_mip_filter);
1306 samp.MaximumAnisotropy = RATIO21;
1307
1308 if (state->max_anisotropy >= 2) {
1309 if (state->min_img_filter == PIPE_TEX_FILTER_LINEAR) {
1310 samp.MinModeFilter = MAPFILTER_ANISOTROPIC;
1311 samp.AnisotropicAlgorithm = EWAApproximation;
1312 }
1313
1314 if (state->mag_img_filter == PIPE_TEX_FILTER_LINEAR)
1315 samp.MagModeFilter = MAPFILTER_ANISOTROPIC;
1316
1317 samp.MaximumAnisotropy =
1318 MIN2((state->max_anisotropy - 2) / 2, RATIO161);
1319 }
1320
1321 /* Set address rounding bits if not using nearest filtering. */
1322 if (state->min_img_filter != PIPE_TEX_FILTER_NEAREST) {
1323 samp.UAddressMinFilterRoundingEnable = true;
1324 samp.VAddressMinFilterRoundingEnable = true;
1325 samp.RAddressMinFilterRoundingEnable = true;
1326 }
1327
1328 if (state->mag_img_filter != PIPE_TEX_FILTER_NEAREST) {
1329 samp.UAddressMagFilterRoundingEnable = true;
1330 samp.VAddressMagFilterRoundingEnable = true;
1331 samp.RAddressMagFilterRoundingEnable = true;
1332 }
1333
1334 if (state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
1335 samp.ShadowFunction = translate_shadow_func(state->compare_func);
1336
1337 const float hw_max_lod = GEN_GEN >= 7 ? 14 : 13;
1338
1339 samp.LODPreClampMode = CLAMP_MODE_OGL;
1340 samp.MinLOD = CLAMP(min_lod, 0, hw_max_lod);
1341 samp.MaxLOD = CLAMP(state->max_lod, 0, hw_max_lod);
1342 samp.TextureLODBias = CLAMP(state->lod_bias, -16, 15);
1343
1344 /* .BorderColorPointer is filled in by iris_bind_sampler_states. */
1345 }
1346
1347 return cso;
1348 }
1349
1350 /**
1351 * The pipe->bind_sampler_states() driver hook.
1352 *
1353 * Now that we know all the sampler states, we upload them all into a
1354 * contiguous area of GPU memory, for 3DSTATE_SAMPLER_STATE_POINTERS_*.
1355 * We also fill out the border color state pointers at this point.
1356 *
1357 * We could defer this work to draw time, but we assume that binding
1358 * will be less frequent than drawing.
1359 */
1360 // XXX: this may be a bad idea, need to make sure that st/mesa calls us
1361 // XXX: with the complete set of shaders. If it makes multiple calls to
1362 // XXX: things one at a time, we could waste a lot of time assembling things.
1363 // XXX: it doesn't even BUY us anything to do it here, because we only flag
1364 // XXX: IRIS_DIRTY_SAMPLER_STATE when this is called...
1365 static void
1366 iris_bind_sampler_states(struct pipe_context *ctx,
1367 enum pipe_shader_type p_stage,
1368 unsigned start, unsigned count,
1369 void **states)
1370 {
1371 struct iris_context *ice = (struct iris_context *) ctx;
1372 gl_shader_stage stage = stage_from_pipe(p_stage);
1373 struct iris_shader_state *shs = &ice->state.shaders[stage];
1374
1375 assert(start + count <= IRIS_MAX_TEXTURE_SAMPLERS);
1376 shs->num_samplers = MAX2(shs->num_samplers, start + count);
1377
1378 for (int i = 0; i < count; i++) {
1379 shs->samplers[start + i] = states[i];
1380 }
1381
1382 // XXX: count may include NULLs
1383
1384 /* Assemble the SAMPLER_STATEs into a contiguous table that lives
1385 * in the dynamic state memory zone, so we can point to it via the
1386 * 3DSTATE_SAMPLER_STATE_POINTERS_* commands.
1387 */
1388 uint32_t *map =
1389 upload_state(ice->state.dynamic_uploader, &shs->sampler_table,
1390 count * 4 * GENX(SAMPLER_STATE_length), 32);
1391 if (unlikely(!map))
1392 return;
1393
1394 struct pipe_resource *res = shs->sampler_table.res;
1395 shs->sampler_table.offset +=
1396 iris_bo_offset_from_base_address(iris_resource_bo(res));
1397
1398 /* Make sure all land in the same BO */
1399 iris_border_color_pool_reserve(ice, IRIS_MAX_TEXTURE_SAMPLERS);
1400
1401 for (int i = 0; i < count; i++) {
1402 struct iris_sampler_state *state = shs->samplers[i];
1403
1404 if (!state) {
1405 memset(map, 0, 4 * GENX(SAMPLER_STATE_length));
1406 } else if (!state->needs_border_color) {
1407 memcpy(map, state->sampler_state, 4 * GENX(SAMPLER_STATE_length));
1408 } else {
1409 ice->state.need_border_colors = true;
1410
1411 /* Stream out the border color and merge the pointer. */
1412 uint32_t offset =
1413 iris_upload_border_color(ice, &state->border_color);
1414
1415 uint32_t dynamic[GENX(SAMPLER_STATE_length)];
1416 iris_pack_state(GENX(SAMPLER_STATE), dynamic, dyns) {
1417 dyns.BorderColorPointer = offset;
1418 }
1419
1420 for (uint32_t j = 0; j < GENX(SAMPLER_STATE_length); j++)
1421 map[j] = state->sampler_state[j] | dynamic[j];
1422 }
1423
1424 map += GENX(SAMPLER_STATE_length);
1425 }
1426
1427 ice->state.dirty |= IRIS_DIRTY_SAMPLER_STATES_VS << stage;
1428 }
1429
1430 static enum isl_channel_select
1431 fmt_swizzle(const struct iris_format_info *fmt, enum pipe_swizzle swz)
1432 {
1433 switch (swz) {
1434 case PIPE_SWIZZLE_X: return fmt->swizzle.r;
1435 case PIPE_SWIZZLE_Y: return fmt->swizzle.g;
1436 case PIPE_SWIZZLE_Z: return fmt->swizzle.b;
1437 case PIPE_SWIZZLE_W: return fmt->swizzle.a;
1438 case PIPE_SWIZZLE_1: return SCS_ONE;
1439 case PIPE_SWIZZLE_0: return SCS_ZERO;
1440 default: unreachable("invalid swizzle");
1441 }
1442 }
1443
1444 static void
1445 fill_buffer_surface_state(struct isl_device *isl_dev,
1446 struct iris_bo *bo,
1447 void *map,
1448 enum isl_format format,
1449 unsigned offset,
1450 unsigned size)
1451 {
1452 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
1453 const unsigned cpp = fmtl->bpb / 8;
1454
1455 /* The ARB_texture_buffer_specification says:
1456 *
1457 * "The number of texels in the buffer texture's texel array is given by
1458 *
1459 * floor(<buffer_size> / (<components> * sizeof(<base_type>)),
1460 *
1461 * where <buffer_size> is the size of the buffer object, in basic
1462 * machine units and <components> and <base_type> are the element count
1463 * and base data type for elements, as specified in Table X.1. The
1464 * number of texels in the texel array is then clamped to the
1465 * implementation-dependent limit MAX_TEXTURE_BUFFER_SIZE_ARB."
1466 *
1467 * We need to clamp the size in bytes to MAX_TEXTURE_BUFFER_SIZE * stride,
1468 * so that when ISL divides by stride to obtain the number of texels, that
1469 * texel count is clamped to MAX_TEXTURE_BUFFER_SIZE.
1470 */
1471 unsigned final_size =
1472 MIN3(size, bo->size - offset, IRIS_MAX_TEXTURE_BUFFER_SIZE * cpp);
1473
1474 isl_buffer_fill_state(isl_dev, map,
1475 .address = bo->gtt_offset + offset,
1476 .size_B = final_size,
1477 .format = format,
1478 .stride_B = cpp,
1479 .mocs = MOCS_WB);
1480 }
1481
1482 /**
1483 * The pipe->create_sampler_view() driver hook.
1484 */
1485 static struct pipe_sampler_view *
1486 iris_create_sampler_view(struct pipe_context *ctx,
1487 struct pipe_resource *tex,
1488 const struct pipe_sampler_view *tmpl)
1489 {
1490 struct iris_context *ice = (struct iris_context *) ctx;
1491 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1492 const struct gen_device_info *devinfo = &screen->devinfo;
1493 struct iris_sampler_view *isv = calloc(1, sizeof(struct iris_sampler_view));
1494
1495 if (!isv)
1496 return NULL;
1497
1498 /* initialize base object */
1499 isv->base = *tmpl;
1500 isv->base.context = ctx;
1501 isv->base.texture = NULL;
1502 pipe_reference_init(&isv->base.reference, 1);
1503 pipe_resource_reference(&isv->base.texture, tex);
1504
1505 void *map = upload_state(ice->state.surface_uploader, &isv->surface_state,
1506 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1507 if (!unlikely(map))
1508 return NULL;
1509
1510 struct iris_bo *state_bo = iris_resource_bo(isv->surface_state.res);
1511 isv->surface_state.offset += iris_bo_offset_from_base_address(state_bo);
1512
1513 if (util_format_is_depth_or_stencil(tmpl->format)) {
1514 struct iris_resource *zres, *sres;
1515 const struct util_format_description *desc =
1516 util_format_description(tmpl->format);
1517
1518 iris_get_depth_stencil_resources(tex, &zres, &sres);
1519
1520 tex = util_format_has_depth(desc) ? &zres->base : &sres->base;
1521 }
1522
1523 isv->res = (struct iris_resource *) tex;
1524
1525 isl_surf_usage_flags_t usage = ISL_SURF_USAGE_TEXTURE_BIT;
1526
1527 if (isv->base.target == PIPE_TEXTURE_CUBE ||
1528 isv->base.target == PIPE_TEXTURE_CUBE_ARRAY)
1529 usage |= ISL_SURF_USAGE_CUBE_BIT;
1530
1531 const struct iris_format_info fmt =
1532 iris_format_for_usage(devinfo, tmpl->format, usage);
1533
1534 isv->view = (struct isl_view) {
1535 .format = fmt.fmt,
1536 .swizzle = (struct isl_swizzle) {
1537 .r = fmt_swizzle(&fmt, tmpl->swizzle_r),
1538 .g = fmt_swizzle(&fmt, tmpl->swizzle_g),
1539 .b = fmt_swizzle(&fmt, tmpl->swizzle_b),
1540 .a = fmt_swizzle(&fmt, tmpl->swizzle_a),
1541 },
1542 .usage = usage,
1543 };
1544
1545 /* Fill out SURFACE_STATE for this view. */
1546 if (tmpl->target != PIPE_BUFFER) {
1547 isv->view.base_level = tmpl->u.tex.first_level;
1548 isv->view.levels = tmpl->u.tex.last_level - tmpl->u.tex.first_level + 1;
1549 // XXX: do I need to port f9fd0cf4790cb2a530e75d1a2206dbb9d8af7cb2?
1550 isv->view.base_array_layer = tmpl->u.tex.first_layer;
1551 isv->view.array_len =
1552 tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1;
1553
1554 isl_surf_fill_state(&screen->isl_dev, map,
1555 .surf = &isv->res->surf, .view = &isv->view,
1556 .mocs = MOCS_WB,
1557 .address = isv->res->bo->gtt_offset);
1558 // .aux_surf =
1559 // .clear_color = clear_color,
1560 } else {
1561 fill_buffer_surface_state(&screen->isl_dev, isv->res->bo, map,
1562 isv->view.format, tmpl->u.buf.offset,
1563 tmpl->u.buf.size);
1564 }
1565
1566 return &isv->base;
1567 }
1568
1569 static void
1570 iris_sampler_view_destroy(struct pipe_context *ctx,
1571 struct pipe_sampler_view *state)
1572 {
1573 struct iris_sampler_view *isv = (void *) state;
1574 pipe_resource_reference(&state->texture, NULL);
1575 pipe_resource_reference(&isv->surface_state.res, NULL);
1576 free(isv);
1577 }
1578
1579 /**
1580 * The pipe->create_surface() driver hook.
1581 *
1582 * In Gallium nomenclature, "surfaces" are a view of a resource that
1583 * can be bound as a render target or depth/stencil buffer.
1584 */
1585 static struct pipe_surface *
1586 iris_create_surface(struct pipe_context *ctx,
1587 struct pipe_resource *tex,
1588 const struct pipe_surface *tmpl)
1589 {
1590 struct iris_context *ice = (struct iris_context *) ctx;
1591 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1592 const struct gen_device_info *devinfo = &screen->devinfo;
1593 struct iris_surface *surf = calloc(1, sizeof(struct iris_surface));
1594 struct pipe_surface *psurf = &surf->base;
1595 struct iris_resource *res = (struct iris_resource *) tex;
1596
1597 if (!surf)
1598 return NULL;
1599
1600 pipe_reference_init(&psurf->reference, 1);
1601 pipe_resource_reference(&psurf->texture, tex);
1602 psurf->context = ctx;
1603 psurf->format = tmpl->format;
1604 psurf->width = tex->width0;
1605 psurf->height = tex->height0;
1606 psurf->texture = tex;
1607 psurf->u.tex.first_layer = tmpl->u.tex.first_layer;
1608 psurf->u.tex.last_layer = tmpl->u.tex.last_layer;
1609 psurf->u.tex.level = tmpl->u.tex.level;
1610
1611 isl_surf_usage_flags_t usage = 0;
1612 if (tmpl->writable)
1613 usage = ISL_SURF_USAGE_STORAGE_BIT;
1614 else if (util_format_is_depth_or_stencil(tmpl->format))
1615 usage = ISL_SURF_USAGE_DEPTH_BIT;
1616 else
1617 usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
1618
1619 const struct iris_format_info fmt =
1620 iris_format_for_usage(devinfo, psurf->format, usage);
1621
1622 if ((usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) &&
1623 !isl_format_supports_rendering(devinfo, fmt.fmt)) {
1624 /* Framebuffer validation will reject this invalid case, but it
1625 * hasn't had the opportunity yet. In the meantime, we need to
1626 * avoid hitting ISL asserts about unsupported formats below.
1627 */
1628 free(surf);
1629 return NULL;
1630 }
1631
1632 surf->view = (struct isl_view) {
1633 .format = fmt.fmt,
1634 .base_level = tmpl->u.tex.level,
1635 .levels = 1,
1636 .base_array_layer = tmpl->u.tex.first_layer,
1637 .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
1638 .swizzle = ISL_SWIZZLE_IDENTITY,
1639 .usage = usage,
1640 };
1641
1642 /* Bail early for depth/stencil - we don't want SURFACE_STATE for them. */
1643 if (res->surf.usage & (ISL_SURF_USAGE_DEPTH_BIT |
1644 ISL_SURF_USAGE_STENCIL_BIT))
1645 return psurf;
1646
1647
1648 void *map = upload_state(ice->state.surface_uploader, &surf->surface_state,
1649 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1650 if (!unlikely(map))
1651 return NULL;
1652
1653 struct iris_bo *state_bo = iris_resource_bo(surf->surface_state.res);
1654 surf->surface_state.offset += iris_bo_offset_from_base_address(state_bo);
1655
1656 isl_surf_fill_state(&screen->isl_dev, map,
1657 .surf = &res->surf, .view = &surf->view,
1658 .mocs = MOCS_WB,
1659 .address = res->bo->gtt_offset);
1660 // .aux_surf =
1661 // .clear_color = clear_color,
1662
1663 return psurf;
1664 }
1665
1666 /**
1667 * The pipe->set_shader_images() driver hook.
1668 */
1669 static void
1670 iris_set_shader_images(struct pipe_context *ctx,
1671 enum pipe_shader_type p_stage,
1672 unsigned start_slot, unsigned count,
1673 const struct pipe_image_view *p_images)
1674 {
1675 struct iris_context *ice = (struct iris_context *) ctx;
1676 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1677 const struct gen_device_info *devinfo = &screen->devinfo;
1678 gl_shader_stage stage = stage_from_pipe(p_stage);
1679 struct iris_shader_state *shs = &ice->state.shaders[stage];
1680
1681 shs->num_images = MAX2(shs->num_images, start_slot + count);
1682
1683 for (unsigned i = 0; i < count; i++) {
1684 if (p_images && p_images[i].resource) {
1685 const struct pipe_image_view *img = &p_images[i];
1686 struct iris_resource *res = (void *) img->resource;
1687 pipe_resource_reference(&shs->image[start_slot + i].res, &res->base);
1688
1689 res->bind_history |= PIPE_BIND_SHADER_IMAGE;
1690
1691 // XXX: these are not retained forever, use a separate uploader?
1692 void *map =
1693 upload_state(ice->state.surface_uploader,
1694 &shs->image[start_slot + i].surface_state,
1695 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1696 if (!unlikely(map)) {
1697 pipe_resource_reference(&shs->image[start_slot + i].res, NULL);
1698 return;
1699 }
1700
1701 struct iris_bo *surf_state_bo =
1702 iris_resource_bo(shs->image[start_slot + i].surface_state.res);
1703 shs->image[start_slot + i].surface_state.offset +=
1704 iris_bo_offset_from_base_address(surf_state_bo);
1705
1706 isl_surf_usage_flags_t usage = ISL_SURF_USAGE_STORAGE_BIT;
1707 enum isl_format isl_format =
1708 iris_format_for_usage(devinfo, img->format, usage).fmt;
1709
1710 if (img->shader_access & PIPE_IMAGE_ACCESS_READ)
1711 isl_format = isl_lower_storage_image_format(devinfo, isl_format);
1712
1713 shs->image[start_slot + i].access = img->shader_access;
1714
1715 if (res->base.target != PIPE_BUFFER) {
1716 struct isl_view view = {
1717 .format = isl_format,
1718 .base_level = img->u.tex.level,
1719 .levels = 1,
1720 .base_array_layer = img->u.tex.first_layer,
1721 .array_len = img->u.tex.last_layer - img->u.tex.first_layer + 1,
1722 .swizzle = ISL_SWIZZLE_IDENTITY,
1723 .usage = usage,
1724 };
1725
1726 isl_surf_fill_state(&screen->isl_dev, map,
1727 .surf = &res->surf, .view = &view,
1728 .mocs = MOCS_WB,
1729 .address = res->bo->gtt_offset);
1730 // .aux_surf =
1731 // .clear_color = clear_color,
1732 } else {
1733 fill_buffer_surface_state(&screen->isl_dev, res->bo, map,
1734 isl_format, img->u.buf.offset,
1735 img->u.buf.size);
1736 }
1737 } else {
1738 pipe_resource_reference(&shs->image[start_slot + i].res, NULL);
1739 pipe_resource_reference(&shs->image[start_slot + i].surface_state.res,
1740 NULL);
1741 }
1742 }
1743
1744 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
1745 }
1746
1747
1748 /**
1749 * The pipe->set_sampler_views() driver hook.
1750 */
1751 static void
1752 iris_set_sampler_views(struct pipe_context *ctx,
1753 enum pipe_shader_type p_stage,
1754 unsigned start, unsigned count,
1755 struct pipe_sampler_view **views)
1756 {
1757 struct iris_context *ice = (struct iris_context *) ctx;
1758 gl_shader_stage stage = stage_from_pipe(p_stage);
1759 struct iris_shader_state *shs = &ice->state.shaders[stage];
1760
1761 unsigned i;
1762 for (i = 0; i < count; i++) {
1763 pipe_sampler_view_reference((struct pipe_sampler_view **)
1764 &shs->textures[i], views[i]);
1765 struct iris_sampler_view *view = (void *) views[i];
1766 if (view)
1767 view->res->bind_history |= PIPE_BIND_SAMPLER_VIEW;
1768 }
1769 for (; i < shs->num_textures; i++) {
1770 pipe_sampler_view_reference((struct pipe_sampler_view **)
1771 &shs->textures[i], NULL);
1772 }
1773
1774 shs->num_textures = count;
1775
1776 ice->state.dirty |= (IRIS_DIRTY_BINDINGS_VS << stage);
1777 }
1778
1779 /**
1780 * The pipe->set_tess_state() driver hook.
1781 */
1782 static void
1783 iris_set_tess_state(struct pipe_context *ctx,
1784 const float default_outer_level[4],
1785 const float default_inner_level[2])
1786 {
1787 struct iris_context *ice = (struct iris_context *) ctx;
1788
1789 memcpy(&ice->state.default_outer_level[0], &default_outer_level[0], 4 * sizeof(float));
1790 memcpy(&ice->state.default_inner_level[0], &default_inner_level[0], 2 * sizeof(float));
1791
1792 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_TCS;
1793 }
1794
1795 static void
1796 iris_surface_destroy(struct pipe_context *ctx, struct pipe_surface *p_surf)
1797 {
1798 struct iris_surface *surf = (void *) p_surf;
1799 pipe_resource_reference(&p_surf->texture, NULL);
1800 pipe_resource_reference(&surf->surface_state.res, NULL);
1801 free(surf);
1802 }
1803
1804 static void
1805 iris_set_clip_state(struct pipe_context *ctx,
1806 const struct pipe_clip_state *state)
1807 {
1808 struct iris_context *ice = (struct iris_context *) ctx;
1809 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_VERTEX];
1810
1811 memcpy(&ice->state.clip_planes, state, sizeof(*state));
1812
1813 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_VS;
1814 shs->cbuf0_needs_upload = true;
1815 }
1816
1817 /**
1818 * The pipe->set_polygon_stipple() driver hook.
1819 */
1820 static void
1821 iris_set_polygon_stipple(struct pipe_context *ctx,
1822 const struct pipe_poly_stipple *state)
1823 {
1824 struct iris_context *ice = (struct iris_context *) ctx;
1825 memcpy(&ice->state.poly_stipple, state, sizeof(*state));
1826 ice->state.dirty |= IRIS_DIRTY_POLYGON_STIPPLE;
1827 }
1828
1829 /**
1830 * The pipe->set_sample_mask() driver hook.
1831 */
1832 static void
1833 iris_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
1834 {
1835 struct iris_context *ice = (struct iris_context *) ctx;
1836
1837 /* We only support 16x MSAA, so we have 16 bits of sample maks.
1838 * st/mesa may pass us 0xffffffff though, meaning "enable all samples".
1839 */
1840 ice->state.sample_mask = sample_mask & 0xffff;
1841 ice->state.dirty |= IRIS_DIRTY_SAMPLE_MASK;
1842 }
1843
1844 /**
1845 * The pipe->set_scissor_states() driver hook.
1846 *
1847 * This corresponds to our SCISSOR_RECT state structures. It's an
1848 * exact match, so we just store them, and memcpy them out later.
1849 */
1850 static void
1851 iris_set_scissor_states(struct pipe_context *ctx,
1852 unsigned start_slot,
1853 unsigned num_scissors,
1854 const struct pipe_scissor_state *rects)
1855 {
1856 struct iris_context *ice = (struct iris_context *) ctx;
1857
1858 for (unsigned i = 0; i < num_scissors; i++) {
1859 if (rects[i].minx == rects[i].maxx || rects[i].miny == rects[i].maxy) {
1860 /* If the scissor was out of bounds and got clamped to 0 width/height
1861 * at the bounds, the subtraction of 1 from maximums could produce a
1862 * negative number and thus not clip anything. Instead, just provide
1863 * a min > max scissor inside the bounds, which produces the expected
1864 * no rendering.
1865 */
1866 ice->state.scissors[start_slot + i] = (struct pipe_scissor_state) {
1867 .minx = 1, .maxx = 0, .miny = 1, .maxy = 0,
1868 };
1869 } else {
1870 ice->state.scissors[start_slot + i] = (struct pipe_scissor_state) {
1871 .minx = rects[i].minx, .miny = rects[i].miny,
1872 .maxx = rects[i].maxx - 1, .maxy = rects[i].maxy - 1,
1873 };
1874 }
1875 }
1876
1877 ice->state.dirty |= IRIS_DIRTY_SCISSOR_RECT;
1878 }
1879
1880 /**
1881 * The pipe->set_stencil_ref() driver hook.
1882 *
1883 * This is added to 3DSTATE_WM_DEPTH_STENCIL dynamically at draw time.
1884 */
1885 static void
1886 iris_set_stencil_ref(struct pipe_context *ctx,
1887 const struct pipe_stencil_ref *state)
1888 {
1889 struct iris_context *ice = (struct iris_context *) ctx;
1890 memcpy(&ice->state.stencil_ref, state, sizeof(*state));
1891 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
1892 }
1893
1894 static float
1895 viewport_extent(const struct pipe_viewport_state *state, int axis, float sign)
1896 {
1897 return copysignf(state->scale[axis], sign) + state->translate[axis];
1898 }
1899
1900 #if 0
1901 static void
1902 calculate_guardband_size(uint32_t fb_width, uint32_t fb_height,
1903 float m00, float m11, float m30, float m31,
1904 float *xmin, float *xmax,
1905 float *ymin, float *ymax)
1906 {
1907 /* According to the "Vertex X,Y Clamping and Quantization" section of the
1908 * Strips and Fans documentation:
1909 *
1910 * "The vertex X and Y screen-space coordinates are also /clamped/ to the
1911 * fixed-point "guardband" range supported by the rasterization hardware"
1912 *
1913 * and
1914 *
1915 * "In almost all circumstances, if an object’s vertices are actually
1916 * modified by this clamping (i.e., had X or Y coordinates outside of
1917 * the guardband extent the rendered object will not match the intended
1918 * result. Therefore software should take steps to ensure that this does
1919 * not happen - e.g., by clipping objects such that they do not exceed
1920 * these limits after the Drawing Rectangle is applied."
1921 *
1922 * I believe the fundamental restriction is that the rasterizer (in
1923 * the SF/WM stages) have a limit on the number of pixels that can be
1924 * rasterized. We need to ensure any coordinates beyond the rasterizer
1925 * limit are handled by the clipper. So effectively that limit becomes
1926 * the clipper's guardband size.
1927 *
1928 * It goes on to say:
1929 *
1930 * "In addition, in order to be correctly rendered, objects must have a
1931 * screenspace bounding box not exceeding 8K in the X or Y direction.
1932 * This additional restriction must also be comprehended by software,
1933 * i.e., enforced by use of clipping."
1934 *
1935 * This makes no sense. Gen7+ hardware supports 16K render targets,
1936 * and you definitely need to be able to draw polygons that fill the
1937 * surface. Our assumption is that the rasterizer was limited to 8K
1938 * on Sandybridge, which only supports 8K surfaces, and it was actually
1939 * increased to 16K on Ivybridge and later.
1940 *
1941 * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
1942 */
1943 const float gb_size = GEN_GEN >= 7 ? 16384.0f : 8192.0f;
1944
1945 if (m00 != 0 && m11 != 0) {
1946 /* First, we compute the screen-space render area */
1947 const float ss_ra_xmin = MIN3( 0, m30 + m00, m30 - m00);
1948 const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
1949 const float ss_ra_ymin = MIN3( 0, m31 + m11, m31 - m11);
1950 const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
1951
1952 /* We want the guardband to be centered on that */
1953 const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
1954 const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
1955 const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
1956 const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
1957
1958 /* Now we need it in native device coordinates */
1959 const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
1960 const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
1961 const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
1962 const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
1963
1964 /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
1965 * flipped upside-down. X should be fine though.
1966 */
1967 assert(ndc_gb_xmin <= ndc_gb_xmax);
1968 *xmin = ndc_gb_xmin;
1969 *xmax = ndc_gb_xmax;
1970 *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
1971 *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
1972 } else {
1973 /* The viewport scales to 0, so nothing will be rendered. */
1974 *xmin = 0.0f;
1975 *xmax = 0.0f;
1976 *ymin = 0.0f;
1977 *ymax = 0.0f;
1978 }
1979 }
1980 #endif
1981
1982 /**
1983 * The pipe->set_viewport_states() driver hook.
1984 *
1985 * This corresponds to our SF_CLIP_VIEWPORT states. We can't calculate
1986 * the guardband yet, as we need the framebuffer dimensions, but we can
1987 * at least fill out the rest.
1988 */
1989 static void
1990 iris_set_viewport_states(struct pipe_context *ctx,
1991 unsigned start_slot,
1992 unsigned count,
1993 const struct pipe_viewport_state *states)
1994 {
1995 struct iris_context *ice = (struct iris_context *) ctx;
1996 struct iris_genx_state *genx = ice->state.genx;
1997 uint32_t *vp_map =
1998 &genx->sf_cl_vp[start_slot * GENX(SF_CLIP_VIEWPORT_length)];
1999
2000 for (unsigned i = 0; i < count; i++) {
2001 const struct pipe_viewport_state *state = &states[i];
2002
2003 memcpy(&ice->state.viewports[start_slot + i], state, sizeof(*state));
2004
2005 iris_pack_state(GENX(SF_CLIP_VIEWPORT), vp_map, vp) {
2006 vp.ViewportMatrixElementm00 = state->scale[0];
2007 vp.ViewportMatrixElementm11 = state->scale[1];
2008 vp.ViewportMatrixElementm22 = state->scale[2];
2009 vp.ViewportMatrixElementm30 = state->translate[0];
2010 vp.ViewportMatrixElementm31 = state->translate[1];
2011 vp.ViewportMatrixElementm32 = state->translate[2];
2012 /* XXX: in i965 this is computed based on the drawbuffer size,
2013 * but we don't have that here...
2014 */
2015 vp.XMinClipGuardband = -1.0;
2016 vp.XMaxClipGuardband = 1.0;
2017 vp.YMinClipGuardband = -1.0;
2018 vp.YMaxClipGuardband = 1.0;
2019 vp.XMinViewPort = viewport_extent(state, 0, -1.0f);
2020 vp.XMaxViewPort = viewport_extent(state, 0, 1.0f) - 1;
2021 vp.YMinViewPort = viewport_extent(state, 1, -1.0f);
2022 vp.YMaxViewPort = viewport_extent(state, 1, 1.0f) - 1;
2023 }
2024
2025 vp_map += GENX(SF_CLIP_VIEWPORT_length);
2026 }
2027
2028 ice->state.dirty |= IRIS_DIRTY_SF_CL_VIEWPORT;
2029
2030 if (ice->state.cso_rast && (!ice->state.cso_rast->depth_clip_near ||
2031 !ice->state.cso_rast->depth_clip_far))
2032 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
2033 }
2034
2035 /**
2036 * The pipe->set_framebuffer_state() driver hook.
2037 *
2038 * Sets the current draw FBO, including color render targets, depth,
2039 * and stencil buffers.
2040 */
2041 static void
2042 iris_set_framebuffer_state(struct pipe_context *ctx,
2043 const struct pipe_framebuffer_state *state)
2044 {
2045 struct iris_context *ice = (struct iris_context *) ctx;
2046 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
2047 struct isl_device *isl_dev = &screen->isl_dev;
2048 struct pipe_framebuffer_state *cso = &ice->state.framebuffer;
2049 struct iris_resource *zres;
2050 struct iris_resource *stencil_res;
2051
2052 unsigned samples = util_framebuffer_get_num_samples(state);
2053
2054 if (cso->samples != samples) {
2055 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
2056 }
2057
2058 if (cso->nr_cbufs != state->nr_cbufs) {
2059 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
2060 }
2061
2062 if ((cso->layers == 0) != (state->layers == 0)) {
2063 ice->state.dirty |= IRIS_DIRTY_CLIP;
2064 }
2065
2066 util_copy_framebuffer_state(cso, state);
2067 cso->samples = samples;
2068
2069 struct iris_depth_buffer_state *cso_z = &ice->state.genx->depth_buffer;
2070
2071 struct isl_view view = {
2072 .base_level = 0,
2073 .levels = 1,
2074 .base_array_layer = 0,
2075 .array_len = 1,
2076 .swizzle = ISL_SWIZZLE_IDENTITY,
2077 };
2078
2079 struct isl_depth_stencil_hiz_emit_info info = {
2080 .view = &view,
2081 .mocs = MOCS_WB,
2082 };
2083
2084 if (cso->zsbuf) {
2085 iris_get_depth_stencil_resources(cso->zsbuf->texture, &zres,
2086 &stencil_res);
2087
2088 view.base_level = cso->zsbuf->u.tex.level;
2089 view.base_array_layer = cso->zsbuf->u.tex.first_layer;
2090 view.array_len =
2091 cso->zsbuf->u.tex.last_layer - cso->zsbuf->u.tex.first_layer + 1;
2092
2093 if (zres) {
2094 view.usage |= ISL_SURF_USAGE_DEPTH_BIT;
2095
2096 info.depth_surf = &zres->surf;
2097 info.depth_address = zres->bo->gtt_offset;
2098 info.hiz_usage = ISL_AUX_USAGE_NONE;
2099
2100 view.format = zres->surf.format;
2101 }
2102
2103 if (stencil_res) {
2104 view.usage |= ISL_SURF_USAGE_STENCIL_BIT;
2105 info.stencil_surf = &stencil_res->surf;
2106 info.stencil_address = stencil_res->bo->gtt_offset;
2107 if (!zres)
2108 view.format = stencil_res->surf.format;
2109 }
2110 }
2111
2112 isl_emit_depth_stencil_hiz_s(isl_dev, cso_z->packets, &info);
2113
2114 /* Make a null surface for unbound buffers */
2115 void *null_surf_map =
2116 upload_state(ice->state.surface_uploader, &ice->state.null_fb,
2117 4 * GENX(RENDER_SURFACE_STATE_length), 64);
2118 isl_null_fill_state(&screen->isl_dev, null_surf_map,
2119 isl_extent3d(MAX2(cso->width, 1),
2120 MAX2(cso->height, 1),
2121 cso->layers ? cso->layers : 1));
2122 ice->state.null_fb.offset +=
2123 iris_bo_offset_from_base_address(iris_resource_bo(ice->state.null_fb.res));
2124
2125 ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
2126
2127 /* Render target change */
2128 ice->state.dirty |= IRIS_DIRTY_BINDINGS_FS;
2129
2130 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_FRAMEBUFFER];
2131
2132 #if GEN_GEN == 11
2133 // XXX: we may want to flag IRIS_DIRTY_MULTISAMPLE (or SAMPLE_MASK?)
2134 // XXX: see commit 979fc1bc9bcc64027ff2cfafd285676f31b930a6
2135
2136 /* The PIPE_CONTROL command description says:
2137 *
2138 * "Whenever a Binding Table Index (BTI) used by a Render Target Message
2139 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
2140 * Target Cache Flush by enabling this bit. When render target flush
2141 * is set due to new association of BTI, PS Scoreboard Stall bit must
2142 * be set in this packet."
2143 */
2144 // XXX: does this need to happen at 3DSTATE_BTP_PS time?
2145 iris_emit_pipe_control_flush(&ice->batches[IRIS_BATCH_RENDER],
2146 PIPE_CONTROL_RENDER_TARGET_FLUSH |
2147 PIPE_CONTROL_STALL_AT_SCOREBOARD);
2148 #endif
2149 }
2150
2151 static void
2152 upload_ubo_surf_state(struct iris_context *ice,
2153 struct iris_const_buffer *cbuf,
2154 unsigned buffer_size)
2155 {
2156 struct pipe_context *ctx = &ice->ctx;
2157 struct iris_screen *screen = (struct iris_screen *) ctx->screen;
2158
2159 // XXX: these are not retained forever, use a separate uploader?
2160 void *map =
2161 upload_state(ice->state.surface_uploader, &cbuf->surface_state,
2162 4 * GENX(RENDER_SURFACE_STATE_length), 64);
2163 if (!unlikely(map)) {
2164 pipe_resource_reference(&cbuf->data.res, NULL);
2165 return;
2166 }
2167
2168 struct iris_resource *res = (void *) cbuf->data.res;
2169 struct iris_bo *surf_bo = iris_resource_bo(cbuf->surface_state.res);
2170 cbuf->surface_state.offset += iris_bo_offset_from_base_address(surf_bo);
2171
2172 isl_buffer_fill_state(&screen->isl_dev, map,
2173 .address = res->bo->gtt_offset + cbuf->data.offset,
2174 .size_B = MIN2(buffer_size,
2175 res->bo->size - cbuf->data.offset),
2176 .format = ISL_FORMAT_R32G32B32A32_FLOAT,
2177 .stride_B = 1,
2178 .mocs = MOCS_WB)
2179 }
2180
2181 /**
2182 * The pipe->set_constant_buffer() driver hook.
2183 *
2184 * This uploads any constant data in user buffers, and references
2185 * any UBO resources containing constant data.
2186 */
2187 static void
2188 iris_set_constant_buffer(struct pipe_context *ctx,
2189 enum pipe_shader_type p_stage, unsigned index,
2190 const struct pipe_constant_buffer *input)
2191 {
2192 struct iris_context *ice = (struct iris_context *) ctx;
2193 gl_shader_stage stage = stage_from_pipe(p_stage);
2194 struct iris_shader_state *shs = &ice->state.shaders[stage];
2195 struct iris_const_buffer *cbuf = &shs->constbuf[index];
2196
2197 if (input && input->buffer) {
2198 assert(index > 0);
2199
2200 pipe_resource_reference(&cbuf->data.res, input->buffer);
2201 cbuf->data.offset = input->buffer_offset;
2202
2203 struct iris_resource *res = (void *) cbuf->data.res;
2204 res->bind_history |= PIPE_BIND_CONSTANT_BUFFER;
2205
2206 upload_ubo_surf_state(ice, cbuf, input->buffer_size);
2207 } else {
2208 pipe_resource_reference(&cbuf->data.res, NULL);
2209 pipe_resource_reference(&cbuf->surface_state.res, NULL);
2210 }
2211
2212 if (index == 0) {
2213 if (input)
2214 memcpy(&shs->cbuf0, input, sizeof(shs->cbuf0));
2215 else
2216 memset(&shs->cbuf0, 0, sizeof(shs->cbuf0));
2217
2218 shs->cbuf0_needs_upload = true;
2219 }
2220
2221 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_VS << stage;
2222 // XXX: maybe not necessary all the time...?
2223 // XXX: we need 3DS_BTP to commit these changes, and if we fell back to
2224 // XXX: pull model we may need actual new bindings...
2225 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
2226 }
2227
2228 static void
2229 upload_uniforms(struct iris_context *ice,
2230 gl_shader_stage stage)
2231 {
2232 struct iris_shader_state *shs = &ice->state.shaders[stage];
2233 struct iris_const_buffer *cbuf = &shs->constbuf[0];
2234 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2235
2236 unsigned upload_size = shader->num_system_values * sizeof(uint32_t) +
2237 shs->cbuf0.buffer_size;
2238
2239 if (upload_size == 0)
2240 return;
2241
2242 uint32_t *map =
2243 upload_state(ice->ctx.const_uploader, &cbuf->data, upload_size, 64);
2244
2245 for (int i = 0; i < shader->num_system_values; i++) {
2246 uint32_t sysval = shader->system_values[i];
2247 uint32_t value = 0;
2248
2249 if (BRW_PARAM_BUILTIN_IS_CLIP_PLANE(sysval)) {
2250 int plane = BRW_PARAM_BUILTIN_CLIP_PLANE_IDX(sysval);
2251 int comp = BRW_PARAM_BUILTIN_CLIP_PLANE_COMP(sysval);
2252 value = fui(ice->state.clip_planes.ucp[plane][comp]);
2253 } else {
2254 assert(!"unhandled system value");
2255 }
2256
2257 *map++ = value;
2258 }
2259
2260 if (shs->cbuf0.user_buffer) {
2261 memcpy(map, shs->cbuf0.user_buffer, shs->cbuf0.buffer_size);
2262 }
2263
2264 upload_ubo_surf_state(ice, cbuf, upload_size);
2265 }
2266
2267 /**
2268 * The pipe->set_shader_buffers() driver hook.
2269 *
2270 * This binds SSBOs and ABOs. Unfortunately, we need to stream out
2271 * SURFACE_STATE here, as the buffer offset may change each time.
2272 */
2273 static void
2274 iris_set_shader_buffers(struct pipe_context *ctx,
2275 enum pipe_shader_type p_stage,
2276 unsigned start_slot, unsigned count,
2277 const struct pipe_shader_buffer *buffers)
2278 {
2279 struct iris_context *ice = (struct iris_context *) ctx;
2280 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
2281 gl_shader_stage stage = stage_from_pipe(p_stage);
2282 struct iris_shader_state *shs = &ice->state.shaders[stage];
2283
2284 for (unsigned i = 0; i < count; i++) {
2285 if (buffers && buffers[i].buffer) {
2286 const struct pipe_shader_buffer *buffer = &buffers[i];
2287 struct iris_resource *res = (void *) buffer->buffer;
2288 pipe_resource_reference(&shs->ssbo[start_slot + i], &res->base);
2289
2290 res->bind_history |= PIPE_BIND_SHADER_BUFFER;
2291
2292 // XXX: these are not retained forever, use a separate uploader?
2293 void *map =
2294 upload_state(ice->state.surface_uploader,
2295 &shs->ssbo_surface_state[start_slot + i],
2296 4 * GENX(RENDER_SURFACE_STATE_length), 64);
2297 if (!unlikely(map)) {
2298 pipe_resource_reference(&shs->ssbo[start_slot + i], NULL);
2299 return;
2300 }
2301
2302 struct iris_bo *surf_state_bo =
2303 iris_resource_bo(shs->ssbo_surface_state[start_slot + i].res);
2304 shs->ssbo_surface_state[start_slot + i].offset +=
2305 iris_bo_offset_from_base_address(surf_state_bo);
2306
2307 isl_buffer_fill_state(&screen->isl_dev, map,
2308 .address =
2309 res->bo->gtt_offset + buffer->buffer_offset,
2310 .size_B =
2311 MIN2(buffer->buffer_size,
2312 res->bo->size - buffer->buffer_offset),
2313 .format = ISL_FORMAT_RAW,
2314 .stride_B = 1,
2315 .mocs = MOCS_WB);
2316 } else {
2317 pipe_resource_reference(&shs->ssbo[start_slot + i], NULL);
2318 pipe_resource_reference(&shs->ssbo_surface_state[start_slot + i].res,
2319 NULL);
2320 }
2321 }
2322
2323 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
2324 }
2325
2326 static void
2327 iris_delete_state(struct pipe_context *ctx, void *state)
2328 {
2329 free(state);
2330 }
2331
2332 static void
2333 iris_free_vertex_buffers(struct iris_vertex_buffer_state *cso)
2334 {
2335 for (unsigned i = 0; i < cso->num_buffers; i++)
2336 pipe_resource_reference(&cso->resources[i], NULL);
2337 }
2338
2339 /**
2340 * The pipe->set_vertex_buffers() driver hook.
2341 *
2342 * This translates pipe_vertex_buffer to our 3DSTATE_VERTEX_BUFFERS packet.
2343 */
2344 static void
2345 iris_set_vertex_buffers(struct pipe_context *ctx,
2346 unsigned start_slot, unsigned count,
2347 const struct pipe_vertex_buffer *buffers)
2348 {
2349 struct iris_context *ice = (struct iris_context *) ctx;
2350 struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
2351
2352 iris_free_vertex_buffers(&ice->state.genx->vertex_buffers);
2353
2354 if (!buffers)
2355 count = 0;
2356
2357 cso->num_buffers = count;
2358
2359 iris_pack_command(GENX(3DSTATE_VERTEX_BUFFERS), cso->vertex_buffers, vb) {
2360 vb.DWordLength = 4 * MAX2(cso->num_buffers, 1) - 1;
2361 }
2362
2363 uint32_t *vb_pack_dest = &cso->vertex_buffers[1];
2364
2365 if (count == 0) {
2366 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
2367 vb.VertexBufferIndex = start_slot;
2368 vb.NullVertexBuffer = true;
2369 vb.AddressModifyEnable = true;
2370 }
2371 }
2372
2373 for (unsigned i = 0; i < count; i++) {
2374 assert(!buffers[i].is_user_buffer);
2375
2376 pipe_resource_reference(&cso->resources[i], buffers[i].buffer.resource);
2377 struct iris_resource *res = (void *) cso->resources[i];
2378
2379 if (res)
2380 res->bind_history |= PIPE_BIND_VERTEX_BUFFER;
2381
2382 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
2383 vb.VertexBufferIndex = start_slot + i;
2384 vb.MOCS = MOCS_WB;
2385 vb.AddressModifyEnable = true;
2386 vb.BufferPitch = buffers[i].stride;
2387 if (res) {
2388 vb.BufferSize = res->bo->size;
2389 vb.BufferStartingAddress =
2390 ro_bo(NULL, res->bo->gtt_offset + buffers[i].buffer_offset);
2391 } else {
2392 vb.NullVertexBuffer = true;
2393 }
2394 }
2395
2396 vb_pack_dest += GENX(VERTEX_BUFFER_STATE_length);
2397 }
2398
2399 ice->state.dirty |= IRIS_DIRTY_VERTEX_BUFFERS;
2400 }
2401
2402 /**
2403 * Gallium CSO for vertex elements.
2404 */
2405 struct iris_vertex_element_state {
2406 uint32_t vertex_elements[1 + 33 * GENX(VERTEX_ELEMENT_STATE_length)];
2407 uint32_t vf_instancing[33 * GENX(3DSTATE_VF_INSTANCING_length)];
2408 unsigned count;
2409 };
2410
2411 /**
2412 * The pipe->create_vertex_elements() driver hook.
2413 *
2414 * This translates pipe_vertex_element to our 3DSTATE_VERTEX_ELEMENTS
2415 * and 3DSTATE_VF_INSTANCING commands. SGVs are handled at draw time.
2416 */
2417 static void *
2418 iris_create_vertex_elements(struct pipe_context *ctx,
2419 unsigned count,
2420 const struct pipe_vertex_element *state)
2421 {
2422 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
2423 const struct gen_device_info *devinfo = &screen->devinfo;
2424 struct iris_vertex_element_state *cso =
2425 malloc(sizeof(struct iris_vertex_element_state));
2426
2427 cso->count = count;
2428
2429 /* TODO:
2430 * - create edge flag one
2431 * - create SGV ones
2432 * - if those are necessary, use count + 1/2/3... OR in the length
2433 */
2434 iris_pack_command(GENX(3DSTATE_VERTEX_ELEMENTS), cso->vertex_elements, ve) {
2435 ve.DWordLength =
2436 1 + GENX(VERTEX_ELEMENT_STATE_length) * MAX2(count, 1) - 2;
2437 }
2438
2439 uint32_t *ve_pack_dest = &cso->vertex_elements[1];
2440 uint32_t *vfi_pack_dest = cso->vf_instancing;
2441
2442 if (count == 0) {
2443 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
2444 ve.Valid = true;
2445 ve.SourceElementFormat = ISL_FORMAT_R32G32B32A32_FLOAT;
2446 ve.Component0Control = VFCOMP_STORE_0;
2447 ve.Component1Control = VFCOMP_STORE_0;
2448 ve.Component2Control = VFCOMP_STORE_0;
2449 ve.Component3Control = VFCOMP_STORE_1_FP;
2450 }
2451
2452 iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
2453 }
2454 }
2455
2456 for (int i = 0; i < count; i++) {
2457 const struct iris_format_info fmt =
2458 iris_format_for_usage(devinfo, state[i].src_format, 0);
2459 unsigned comp[4] = { VFCOMP_STORE_SRC, VFCOMP_STORE_SRC,
2460 VFCOMP_STORE_SRC, VFCOMP_STORE_SRC };
2461
2462 switch (isl_format_get_num_channels(fmt.fmt)) {
2463 case 0: comp[0] = VFCOMP_STORE_0;
2464 case 1: comp[1] = VFCOMP_STORE_0;
2465 case 2: comp[2] = VFCOMP_STORE_0;
2466 case 3:
2467 comp[3] = isl_format_has_int_channel(fmt.fmt) ? VFCOMP_STORE_1_INT
2468 : VFCOMP_STORE_1_FP;
2469 break;
2470 }
2471 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
2472 ve.VertexBufferIndex = state[i].vertex_buffer_index;
2473 ve.Valid = true;
2474 ve.SourceElementOffset = state[i].src_offset;
2475 ve.SourceElementFormat = fmt.fmt;
2476 ve.Component0Control = comp[0];
2477 ve.Component1Control = comp[1];
2478 ve.Component2Control = comp[2];
2479 ve.Component3Control = comp[3];
2480 }
2481
2482 iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
2483 vi.VertexElementIndex = i;
2484 vi.InstancingEnable = state[i].instance_divisor > 0;
2485 vi.InstanceDataStepRate = state[i].instance_divisor;
2486 }
2487
2488 ve_pack_dest += GENX(VERTEX_ELEMENT_STATE_length);
2489 vfi_pack_dest += GENX(3DSTATE_VF_INSTANCING_length);
2490 }
2491
2492 return cso;
2493 }
2494
2495 /**
2496 * The pipe->bind_vertex_elements_state() driver hook.
2497 */
2498 static void
2499 iris_bind_vertex_elements_state(struct pipe_context *ctx, void *state)
2500 {
2501 struct iris_context *ice = (struct iris_context *) ctx;
2502 struct iris_vertex_element_state *old_cso = ice->state.cso_vertex_elements;
2503 struct iris_vertex_element_state *new_cso = state;
2504
2505 /* 3DSTATE_VF_SGVs overrides the last VE, so if the count is changing,
2506 * we need to re-emit it to ensure we're overriding the right one.
2507 */
2508 if (new_cso && cso_changed(count))
2509 ice->state.dirty |= IRIS_DIRTY_VF_SGVS;
2510
2511 ice->state.cso_vertex_elements = state;
2512 ice->state.dirty |= IRIS_DIRTY_VERTEX_ELEMENTS;
2513 }
2514
2515 /**
2516 * Gallium CSO for stream output (transform feedback) targets.
2517 */
2518 struct iris_stream_output_target {
2519 struct pipe_stream_output_target base;
2520
2521 uint32_t so_buffer[GENX(3DSTATE_SO_BUFFER_length)];
2522
2523 /** Storage holding the offset where we're writing in the buffer */
2524 struct iris_state_ref offset;
2525 };
2526
2527 /**
2528 * The pipe->create_stream_output_target() driver hook.
2529 *
2530 * "Target" here refers to a destination buffer. We translate this into
2531 * a 3DSTATE_SO_BUFFER packet. We can handle most fields, but don't yet
2532 * know which buffer this represents, or whether we ought to zero the
2533 * write-offsets, or append. Those are handled in the set() hook.
2534 */
2535 static struct pipe_stream_output_target *
2536 iris_create_stream_output_target(struct pipe_context *ctx,
2537 struct pipe_resource *p_res,
2538 unsigned buffer_offset,
2539 unsigned buffer_size)
2540 {
2541 struct iris_resource *res = (void *) p_res;
2542 struct iris_stream_output_target *cso = calloc(1, sizeof(*cso));
2543 if (!cso)
2544 return NULL;
2545
2546 res->bind_history |= PIPE_BIND_STREAM_OUTPUT;
2547
2548 pipe_reference_init(&cso->base.reference, 1);
2549 pipe_resource_reference(&cso->base.buffer, p_res);
2550 cso->base.buffer_offset = buffer_offset;
2551 cso->base.buffer_size = buffer_size;
2552 cso->base.context = ctx;
2553
2554 upload_state(ctx->stream_uploader, &cso->offset, 4 * sizeof(uint32_t), 4);
2555
2556 iris_pack_command(GENX(3DSTATE_SO_BUFFER), cso->so_buffer, sob) {
2557 sob.SurfaceBaseAddress =
2558 rw_bo(NULL, res->bo->gtt_offset + buffer_offset);
2559 sob.SOBufferEnable = true;
2560 sob.StreamOffsetWriteEnable = true;
2561 sob.StreamOutputBufferOffsetAddressEnable = true;
2562 sob.MOCS = MOCS_WB; // XXX: MOCS
2563
2564 sob.SurfaceSize = MAX2(buffer_size / 4, 1) - 1;
2565
2566 /* .SOBufferIndex, .StreamOffset, and .StreamOutputBufferOffsetAddress
2567 * are filled in later when we have stream IDs.
2568 */
2569 }
2570
2571 return &cso->base;
2572 }
2573
2574 static void
2575 iris_stream_output_target_destroy(struct pipe_context *ctx,
2576 struct pipe_stream_output_target *state)
2577 {
2578 struct iris_stream_output_target *cso = (void *) state;
2579
2580 pipe_resource_reference(&cso->base.buffer, NULL);
2581 pipe_resource_reference(&cso->offset.res, NULL);
2582
2583 free(cso);
2584 }
2585
2586 /**
2587 * The pipe->set_stream_output_targets() driver hook.
2588 *
2589 * At this point, we know which targets are bound to a particular index,
2590 * and also whether we want to append or start over. We can finish the
2591 * 3DSTATE_SO_BUFFER packets we started earlier.
2592 */
2593 static void
2594 iris_set_stream_output_targets(struct pipe_context *ctx,
2595 unsigned num_targets,
2596 struct pipe_stream_output_target **targets,
2597 const unsigned *offsets)
2598 {
2599 struct iris_context *ice = (struct iris_context *) ctx;
2600 struct iris_genx_state *genx = ice->state.genx;
2601 uint32_t *so_buffers = genx->so_buffers;
2602
2603 const bool active = num_targets > 0;
2604 if (ice->state.streamout_active != active) {
2605 ice->state.streamout_active = active;
2606 ice->state.dirty |= IRIS_DIRTY_STREAMOUT;
2607
2608 /* We only emit 3DSTATE_SO_DECL_LIST when streamout is active, because
2609 * it's a non-pipelined command. If we're switching streamout on, we
2610 * may have missed emitting it earlier, so do so now. (We're already
2611 * taking a stall to update 3DSTATE_SO_BUFFERS anyway...)
2612 */
2613 if (active)
2614 ice->state.dirty |= IRIS_DIRTY_SO_DECL_LIST;
2615 }
2616
2617 for (int i = 0; i < 4; i++) {
2618 pipe_so_target_reference(&ice->state.so_target[i],
2619 i < num_targets ? targets[i] : NULL);
2620 }
2621
2622 /* No need to update 3DSTATE_SO_BUFFER unless SOL is active. */
2623 if (!active)
2624 return;
2625
2626 for (unsigned i = 0; i < 4; i++,
2627 so_buffers += GENX(3DSTATE_SO_BUFFER_length)) {
2628
2629 if (i >= num_targets || !targets[i]) {
2630 iris_pack_command(GENX(3DSTATE_SO_BUFFER), so_buffers, sob)
2631 sob.SOBufferIndex = i;
2632 continue;
2633 }
2634
2635 struct iris_stream_output_target *tgt = (void *) targets[i];
2636
2637 /* Note that offsets[i] will either be 0, causing us to zero
2638 * the value in the buffer, or 0xFFFFFFFF, which happens to mean
2639 * "continue appending at the existing offset."
2640 */
2641 assert(offsets[i] == 0 || offsets[i] == 0xFFFFFFFF);
2642
2643 uint32_t dynamic[GENX(3DSTATE_SO_BUFFER_length)];
2644 iris_pack_state(GENX(3DSTATE_SO_BUFFER), dynamic, dyns) {
2645 dyns.SOBufferIndex = i;
2646 dyns.StreamOffset = offsets[i];
2647 dyns.StreamOutputBufferOffsetAddress =
2648 rw_bo(NULL, iris_resource_bo(tgt->offset.res)->gtt_offset + tgt->offset.offset + i * sizeof(uint32_t));
2649 }
2650
2651 for (uint32_t j = 0; j < GENX(3DSTATE_SO_BUFFER_length); j++) {
2652 so_buffers[j] = tgt->so_buffer[j] | dynamic[j];
2653 }
2654 }
2655
2656 ice->state.dirty |= IRIS_DIRTY_SO_BUFFERS;
2657 }
2658
2659 /**
2660 * An iris-vtable helper for encoding the 3DSTATE_SO_DECL_LIST and
2661 * 3DSTATE_STREAMOUT packets.
2662 *
2663 * 3DSTATE_SO_DECL_LIST is a list of shader outputs we want the streamout
2664 * hardware to record. We can create it entirely based on the shader, with
2665 * no dynamic state dependencies.
2666 *
2667 * 3DSTATE_STREAMOUT is an annoying mix of shader-based information and
2668 * state-based settings. We capture the shader-related ones here, and merge
2669 * the rest in at draw time.
2670 */
2671 static uint32_t *
2672 iris_create_so_decl_list(const struct pipe_stream_output_info *info,
2673 const struct brw_vue_map *vue_map)
2674 {
2675 struct GENX(SO_DECL) so_decl[MAX_VERTEX_STREAMS][128];
2676 int buffer_mask[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
2677 int next_offset[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
2678 int decls[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
2679 int max_decls = 0;
2680 STATIC_ASSERT(ARRAY_SIZE(so_decl[0]) >= MAX_PROGRAM_OUTPUTS);
2681
2682 memset(so_decl, 0, sizeof(so_decl));
2683
2684 /* Construct the list of SO_DECLs to be emitted. The formatting of the
2685 * command feels strange -- each dword pair contains a SO_DECL per stream.
2686 */
2687 for (unsigned i = 0; i < info->num_outputs; i++) {
2688 const struct pipe_stream_output *output = &info->output[i];
2689 const int buffer = output->output_buffer;
2690 const int varying = output->register_index;
2691 const unsigned stream_id = output->stream;
2692 assert(stream_id < MAX_VERTEX_STREAMS);
2693
2694 buffer_mask[stream_id] |= 1 << buffer;
2695
2696 assert(vue_map->varying_to_slot[varying] >= 0);
2697
2698 /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
2699 * array. Instead, it simply increments DstOffset for the following
2700 * input by the number of components that should be skipped.
2701 *
2702 * Our hardware is unusual in that it requires us to program SO_DECLs
2703 * for fake "hole" components, rather than simply taking the offset
2704 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
2705 * program as many size = 4 holes as we can, then a final hole to
2706 * accommodate the final 1, 2, or 3 remaining.
2707 */
2708 int skip_components = output->dst_offset - next_offset[buffer];
2709
2710 while (skip_components > 0) {
2711 so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
2712 .HoleFlag = 1,
2713 .OutputBufferSlot = output->output_buffer,
2714 .ComponentMask = (1 << MIN2(skip_components, 4)) - 1,
2715 };
2716 skip_components -= 4;
2717 }
2718
2719 next_offset[buffer] = output->dst_offset + output->num_components;
2720
2721 so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
2722 .OutputBufferSlot = output->output_buffer,
2723 .RegisterIndex = vue_map->varying_to_slot[varying],
2724 .ComponentMask =
2725 ((1 << output->num_components) - 1) << output->start_component,
2726 };
2727
2728 if (decls[stream_id] > max_decls)
2729 max_decls = decls[stream_id];
2730 }
2731
2732 unsigned dwords = GENX(3DSTATE_STREAMOUT_length) + (3 + 2 * max_decls);
2733 uint32_t *map = ralloc_size(NULL, sizeof(uint32_t) * dwords);
2734 uint32_t *so_decl_map = map + GENX(3DSTATE_STREAMOUT_length);
2735
2736 iris_pack_command(GENX(3DSTATE_STREAMOUT), map, sol) {
2737 int urb_entry_read_offset = 0;
2738 int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
2739 urb_entry_read_offset;
2740
2741 /* We always read the whole vertex. This could be reduced at some
2742 * point by reading less and offsetting the register index in the
2743 * SO_DECLs.
2744 */
2745 sol.Stream0VertexReadOffset = urb_entry_read_offset;
2746 sol.Stream0VertexReadLength = urb_entry_read_length - 1;
2747 sol.Stream1VertexReadOffset = urb_entry_read_offset;
2748 sol.Stream1VertexReadLength = urb_entry_read_length - 1;
2749 sol.Stream2VertexReadOffset = urb_entry_read_offset;
2750 sol.Stream2VertexReadLength = urb_entry_read_length - 1;
2751 sol.Stream3VertexReadOffset = urb_entry_read_offset;
2752 sol.Stream3VertexReadLength = urb_entry_read_length - 1;
2753
2754 /* Set buffer pitches; 0 means unbound. */
2755 sol.Buffer0SurfacePitch = 4 * info->stride[0];
2756 sol.Buffer1SurfacePitch = 4 * info->stride[1];
2757 sol.Buffer2SurfacePitch = 4 * info->stride[2];
2758 sol.Buffer3SurfacePitch = 4 * info->stride[3];
2759 }
2760
2761 iris_pack_command(GENX(3DSTATE_SO_DECL_LIST), so_decl_map, list) {
2762 list.DWordLength = 3 + 2 * max_decls - 2;
2763 list.StreamtoBufferSelects0 = buffer_mask[0];
2764 list.StreamtoBufferSelects1 = buffer_mask[1];
2765 list.StreamtoBufferSelects2 = buffer_mask[2];
2766 list.StreamtoBufferSelects3 = buffer_mask[3];
2767 list.NumEntries0 = decls[0];
2768 list.NumEntries1 = decls[1];
2769 list.NumEntries2 = decls[2];
2770 list.NumEntries3 = decls[3];
2771 }
2772
2773 for (int i = 0; i < max_decls; i++) {
2774 iris_pack_state(GENX(SO_DECL_ENTRY), so_decl_map + 3 + i * 2, entry) {
2775 entry.Stream0Decl = so_decl[0][i];
2776 entry.Stream1Decl = so_decl[1][i];
2777 entry.Stream2Decl = so_decl[2][i];
2778 entry.Stream3Decl = so_decl[3][i];
2779 }
2780 }
2781
2782 return map;
2783 }
2784
2785 static void
2786 iris_compute_sbe_urb_read_interval(uint64_t fs_input_slots,
2787 const struct brw_vue_map *last_vue_map,
2788 bool two_sided_color,
2789 unsigned *out_offset,
2790 unsigned *out_length)
2791 {
2792 /* The compiler computes the first URB slot without considering COL/BFC
2793 * swizzling (because it doesn't know whether it's enabled), so we need
2794 * to do that here too. This may result in a smaller offset, which
2795 * should be safe.
2796 */
2797 const unsigned first_slot =
2798 brw_compute_first_urb_slot_required(fs_input_slots, last_vue_map);
2799
2800 /* This becomes the URB read offset (counted in pairs of slots). */
2801 assert(first_slot % 2 == 0);
2802 *out_offset = first_slot / 2;
2803
2804 /* We need to adjust the inputs read to account for front/back color
2805 * swizzling, as it can make the URB length longer.
2806 */
2807 for (int c = 0; c <= 1; c++) {
2808 if (fs_input_slots & (VARYING_BIT_COL0 << c)) {
2809 /* If two sided color is enabled, the fragment shader's gl_Color
2810 * (COL0) input comes from either the gl_FrontColor (COL0) or
2811 * gl_BackColor (BFC0) input varyings. Mark BFC as used, too.
2812 */
2813 if (two_sided_color)
2814 fs_input_slots |= (VARYING_BIT_BFC0 << c);
2815
2816 /* If front color isn't written, we opt to give them back color
2817 * instead of an undefined value. Switch from COL to BFC.
2818 */
2819 if (last_vue_map->varying_to_slot[VARYING_SLOT_COL0 + c] == -1) {
2820 fs_input_slots &= ~(VARYING_BIT_COL0 << c);
2821 fs_input_slots |= (VARYING_BIT_BFC0 << c);
2822 }
2823 }
2824 }
2825
2826 /* Compute the minimum URB Read Length necessary for the FS inputs.
2827 *
2828 * From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
2829 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
2830 *
2831 * "This field should be set to the minimum length required to read the
2832 * maximum source attribute. The maximum source attribute is indicated
2833 * by the maximum value of the enabled Attribute # Source Attribute if
2834 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
2835 * enable is not set.
2836 * read_length = ceiling((max_source_attr + 1) / 2)
2837 *
2838 * [errata] Corruption/Hang possible if length programmed larger than
2839 * recommended"
2840 *
2841 * Similar text exists for Ivy Bridge.
2842 *
2843 * We find the last URB slot that's actually read by the FS.
2844 */
2845 unsigned last_read_slot = last_vue_map->num_slots - 1;
2846 while (last_read_slot > first_slot && !(fs_input_slots &
2847 (1ull << last_vue_map->slot_to_varying[last_read_slot])))
2848 --last_read_slot;
2849
2850 /* The URB read length is the difference of the two, counted in pairs. */
2851 *out_length = DIV_ROUND_UP(last_read_slot - first_slot + 1, 2);
2852 }
2853
2854 static void
2855 iris_emit_sbe_swiz(struct iris_batch *batch,
2856 const struct iris_context *ice,
2857 unsigned urb_read_offset,
2858 unsigned sprite_coord_enables)
2859 {
2860 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) attr_overrides[16] = {};
2861 const struct brw_wm_prog_data *wm_prog_data = (void *)
2862 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2863 const struct brw_vue_map *vue_map = ice->shaders.last_vue_map;
2864 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2865
2866 /* XXX: this should be generated when putting programs in place */
2867
2868 // XXX: raster->sprite_coord_enable
2869
2870 for (int fs_attr = 0; fs_attr < VARYING_SLOT_MAX; fs_attr++) {
2871 const int input_index = wm_prog_data->urb_setup[fs_attr];
2872 if (input_index < 0 || input_index >= 16)
2873 continue;
2874
2875 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) *attr =
2876 &attr_overrides[input_index];
2877 int slot = vue_map->varying_to_slot[fs_attr];
2878
2879 /* Viewport and Layer are stored in the VUE header. We need to override
2880 * them to zero if earlier stages didn't write them, as GL requires that
2881 * they read back as zero when not explicitly set.
2882 */
2883 switch (fs_attr) {
2884 case VARYING_SLOT_VIEWPORT:
2885 case VARYING_SLOT_LAYER:
2886 attr->ComponentOverrideX = true;
2887 attr->ComponentOverrideW = true;
2888 attr->ConstantSource = CONST_0000;
2889
2890 if (!(vue_map->slots_valid & VARYING_BIT_LAYER))
2891 attr->ComponentOverrideY = true;
2892 if (!(vue_map->slots_valid & VARYING_BIT_VIEWPORT))
2893 attr->ComponentOverrideZ = true;
2894 continue;
2895
2896 case VARYING_SLOT_PRIMITIVE_ID:
2897 /* Override if the previous shader stage didn't write gl_PrimitiveID. */
2898 if (slot == -1) {
2899 attr->ComponentOverrideX = true;
2900 attr->ComponentOverrideY = true;
2901 attr->ComponentOverrideZ = true;
2902 attr->ComponentOverrideW = true;
2903 attr->ConstantSource = PRIM_ID;
2904 continue;
2905 }
2906
2907 default:
2908 break;
2909 }
2910
2911 if (sprite_coord_enables & (1 << input_index))
2912 continue;
2913
2914 /* If there was only a back color written but not front, use back
2915 * as the color instead of undefined.
2916 */
2917 if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
2918 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
2919 if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
2920 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
2921
2922 /* Not written by the previous stage - undefined. */
2923 if (slot == -1) {
2924 attr->ComponentOverrideX = true;
2925 attr->ComponentOverrideY = true;
2926 attr->ComponentOverrideZ = true;
2927 attr->ComponentOverrideW = true;
2928 attr->ConstantSource = CONST_0001_FLOAT;
2929 continue;
2930 }
2931
2932 /* Compute the location of the attribute relative to the read offset,
2933 * which is counted in 256-bit increments (two 128-bit VUE slots).
2934 */
2935 const int source_attr = slot - 2 * urb_read_offset;
2936 assert(source_attr >= 0 && source_attr <= 32);
2937 attr->SourceAttribute = source_attr;
2938
2939 /* If we are doing two-sided color, and the VUE slot following this one
2940 * represents a back-facing color, then we need to instruct the SF unit
2941 * to do back-facing swizzling.
2942 */
2943 if (cso_rast->light_twoside &&
2944 ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
2945 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
2946 (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
2947 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1)))
2948 attr->SwizzleSelect = INPUTATTR_FACING;
2949 }
2950
2951 iris_emit_cmd(batch, GENX(3DSTATE_SBE_SWIZ), sbes) {
2952 for (int i = 0; i < 16; i++)
2953 sbes.Attribute[i] = attr_overrides[i];
2954 }
2955 }
2956
2957 static unsigned
2958 iris_calculate_point_sprite_overrides(const struct brw_wm_prog_data *prog_data,
2959 const struct iris_rasterizer_state *cso)
2960 {
2961 unsigned overrides = 0;
2962
2963 if (prog_data->urb_setup[VARYING_SLOT_PNTC] != -1)
2964 overrides |= 1 << prog_data->urb_setup[VARYING_SLOT_PNTC];
2965
2966 for (int i = 0; i < 8; i++) {
2967 if ((cso->sprite_coord_enable & (1 << i)) &&
2968 prog_data->urb_setup[VARYING_SLOT_TEX0 + i] != -1)
2969 overrides |= 1 << prog_data->urb_setup[VARYING_SLOT_TEX0 + i];
2970 }
2971
2972 return overrides;
2973 }
2974
2975 static void
2976 iris_emit_sbe(struct iris_batch *batch, const struct iris_context *ice)
2977 {
2978 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2979 const struct brw_wm_prog_data *wm_prog_data = (void *)
2980 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2981 const struct shader_info *fs_info =
2982 iris_get_shader_info(ice, MESA_SHADER_FRAGMENT);
2983
2984 unsigned urb_read_offset, urb_read_length;
2985 iris_compute_sbe_urb_read_interval(fs_info->inputs_read,
2986 ice->shaders.last_vue_map,
2987 cso_rast->light_twoside,
2988 &urb_read_offset, &urb_read_length);
2989
2990 unsigned sprite_coord_overrides =
2991 iris_calculate_point_sprite_overrides(wm_prog_data, cso_rast);
2992
2993 iris_emit_cmd(batch, GENX(3DSTATE_SBE), sbe) {
2994 sbe.AttributeSwizzleEnable = true;
2995 sbe.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
2996 sbe.PointSpriteTextureCoordinateOrigin = cso_rast->sprite_coord_mode;
2997 sbe.VertexURBEntryReadOffset = urb_read_offset;
2998 sbe.VertexURBEntryReadLength = urb_read_length;
2999 sbe.ForceVertexURBEntryReadOffset = true;
3000 sbe.ForceVertexURBEntryReadLength = true;
3001 sbe.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
3002 sbe.PointSpriteTextureCoordinateEnable = sprite_coord_overrides;
3003
3004 for (int i = 0; i < 32; i++) {
3005 sbe.AttributeActiveComponentFormat[i] = ACTIVE_COMPONENT_XYZW;
3006 }
3007 }
3008
3009 iris_emit_sbe_swiz(batch, ice, urb_read_offset, sprite_coord_overrides);
3010 }
3011
3012 /* ------------------------------------------------------------------- */
3013
3014 /**
3015 * Populate VS program key fields based on the current state.
3016 */
3017 static void
3018 iris_populate_vs_key(const struct iris_context *ice,
3019 const struct shader_info *info,
3020 struct brw_vs_prog_key *key)
3021 {
3022 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
3023
3024 if (info->clip_distance_array_size == 0 &&
3025 (info->outputs_written & (VARYING_BIT_POS | VARYING_BIT_CLIP_VERTEX)))
3026 key->nr_userclip_plane_consts = cso_rast->num_clip_plane_consts;
3027 }
3028
3029 /**
3030 * Populate TCS program key fields based on the current state.
3031 */
3032 static void
3033 iris_populate_tcs_key(const struct iris_context *ice,
3034 struct brw_tcs_prog_key *key)
3035 {
3036 }
3037
3038 /**
3039 * Populate TES program key fields based on the current state.
3040 */
3041 static void
3042 iris_populate_tes_key(const struct iris_context *ice,
3043 struct brw_tes_prog_key *key)
3044 {
3045 }
3046
3047 /**
3048 * Populate GS program key fields based on the current state.
3049 */
3050 static void
3051 iris_populate_gs_key(const struct iris_context *ice,
3052 struct brw_gs_prog_key *key)
3053 {
3054 }
3055
3056 /**
3057 * Populate FS program key fields based on the current state.
3058 */
3059 static void
3060 iris_populate_fs_key(const struct iris_context *ice,
3061 struct brw_wm_prog_key *key)
3062 {
3063 /* XXX: dirty flags? */
3064 const struct pipe_framebuffer_state *fb = &ice->state.framebuffer;
3065 const struct iris_depth_stencil_alpha_state *zsa = ice->state.cso_zsa;
3066 const struct iris_rasterizer_state *rast = ice->state.cso_rast;
3067 const struct iris_blend_state *blend = ice->state.cso_blend;
3068
3069 key->nr_color_regions = fb->nr_cbufs;
3070
3071 key->clamp_fragment_color = rast->clamp_fragment_color;
3072
3073 key->replicate_alpha = fb->nr_cbufs > 1 &&
3074 (zsa->alpha.enabled || blend->alpha_to_coverage);
3075
3076 /* XXX: only bother if COL0/1 are read */
3077 key->flat_shade = rast->flatshade;
3078
3079 key->persample_interp = rast->force_persample_interp;
3080 key->multisample_fbo = rast->multisample && fb->samples > 1;
3081
3082 key->coherent_fb_fetch = true;
3083
3084 // XXX: uint64_t input_slots_valid; - for >16 inputs
3085
3086 // XXX: key->force_dual_color_blend for unigine
3087 // XXX: respect hint for high_quality_derivatives:1;
3088 }
3089
3090 static void
3091 iris_populate_cs_key(const struct iris_context *ice,
3092 struct brw_cs_prog_key *key)
3093 {
3094 }
3095
3096 #if 0
3097 // XXX: these need to go in INIT_THREAD_DISPATCH_FIELDS
3098 pkt.SamplerCount = \
3099 DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4); \
3100
3101 #endif
3102
3103 static uint64_t
3104 KSP(const struct iris_compiled_shader *shader)
3105 {
3106 struct iris_resource *res = (void *) shader->assembly.res;
3107 return iris_bo_offset_from_base_address(res->bo) + shader->assembly.offset;
3108 }
3109
3110 // Gen11 workaround table #2056 WABTPPrefetchDisable suggests to disable
3111 // prefetching of binding tables in A0 and B0 steppings. XXX: Revisit
3112 // this WA on C0 stepping.
3113
3114 #define INIT_THREAD_DISPATCH_FIELDS(pkt, prefix, stage) \
3115 pkt.KernelStartPointer = KSP(shader); \
3116 pkt.BindingTableEntryCount = GEN_GEN == 11 ? 0 : \
3117 prog_data->binding_table.size_bytes / 4; \
3118 pkt.FloatingPointMode = prog_data->use_alt_mode; \
3119 \
3120 pkt.DispatchGRFStartRegisterForURBData = \
3121 prog_data->dispatch_grf_start_reg; \
3122 pkt.prefix##URBEntryReadLength = vue_prog_data->urb_read_length; \
3123 pkt.prefix##URBEntryReadOffset = 0; \
3124 \
3125 pkt.StatisticsEnable = true; \
3126 pkt.Enable = true; \
3127 \
3128 if (prog_data->total_scratch) { \
3129 uint32_t scratch_addr = \
3130 iris_get_scratch_space(ice, prog_data->total_scratch, stage); \
3131 pkt.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11; \
3132 pkt.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr); \
3133 }
3134
3135 /**
3136 * Encode most of 3DSTATE_VS based on the compiled shader.
3137 */
3138 static void
3139 iris_store_vs_state(struct iris_context *ice,
3140 const struct gen_device_info *devinfo,
3141 struct iris_compiled_shader *shader)
3142 {
3143 struct brw_stage_prog_data *prog_data = shader->prog_data;
3144 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
3145
3146 iris_pack_command(GENX(3DSTATE_VS), shader->derived_data, vs) {
3147 INIT_THREAD_DISPATCH_FIELDS(vs, Vertex, MESA_SHADER_VERTEX);
3148 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
3149 vs.SIMD8DispatchEnable = true;
3150 vs.UserClipDistanceCullTestEnableBitmask =
3151 vue_prog_data->cull_distance_mask;
3152 }
3153 }
3154
3155 /**
3156 * Encode most of 3DSTATE_HS based on the compiled shader.
3157 */
3158 static void
3159 iris_store_tcs_state(struct iris_context *ice,
3160 const struct gen_device_info *devinfo,
3161 struct iris_compiled_shader *shader)
3162 {
3163 struct brw_stage_prog_data *prog_data = shader->prog_data;
3164 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
3165 struct brw_tcs_prog_data *tcs_prog_data = (void *) prog_data;
3166
3167 iris_pack_command(GENX(3DSTATE_HS), shader->derived_data, hs) {
3168 INIT_THREAD_DISPATCH_FIELDS(hs, Vertex, MESA_SHADER_TESS_CTRL);
3169
3170 hs.InstanceCount = tcs_prog_data->instances - 1;
3171 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
3172 hs.IncludeVertexHandles = true;
3173 }
3174 }
3175
3176 /**
3177 * Encode 3DSTATE_TE and most of 3DSTATE_DS based on the compiled shader.
3178 */
3179 static void
3180 iris_store_tes_state(struct iris_context *ice,
3181 const struct gen_device_info *devinfo,
3182 struct iris_compiled_shader *shader)
3183 {
3184 struct brw_stage_prog_data *prog_data = shader->prog_data;
3185 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
3186 struct brw_tes_prog_data *tes_prog_data = (void *) prog_data;
3187
3188 uint32_t *te_state = (void *) shader->derived_data;
3189 uint32_t *ds_state = te_state + GENX(3DSTATE_TE_length);
3190
3191 iris_pack_command(GENX(3DSTATE_TE), te_state, te) {
3192 te.Partitioning = tes_prog_data->partitioning;
3193 te.OutputTopology = tes_prog_data->output_topology;
3194 te.TEDomain = tes_prog_data->domain;
3195 te.TEEnable = true;
3196 te.MaximumTessellationFactorOdd = 63.0;
3197 te.MaximumTessellationFactorNotOdd = 64.0;
3198 }
3199
3200 iris_pack_command(GENX(3DSTATE_DS), ds_state, ds) {
3201 INIT_THREAD_DISPATCH_FIELDS(ds, Patch, MESA_SHADER_TESS_EVAL);
3202
3203 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
3204 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
3205 ds.ComputeWCoordinateEnable =
3206 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
3207
3208 ds.UserClipDistanceCullTestEnableBitmask =
3209 vue_prog_data->cull_distance_mask;
3210 }
3211
3212 }
3213
3214 /**
3215 * Encode most of 3DSTATE_GS based on the compiled shader.
3216 */
3217 static void
3218 iris_store_gs_state(struct iris_context *ice,
3219 const struct gen_device_info *devinfo,
3220 struct iris_compiled_shader *shader)
3221 {
3222 struct brw_stage_prog_data *prog_data = shader->prog_data;
3223 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
3224 struct brw_gs_prog_data *gs_prog_data = (void *) prog_data;
3225
3226 iris_pack_command(GENX(3DSTATE_GS), shader->derived_data, gs) {
3227 INIT_THREAD_DISPATCH_FIELDS(gs, Vertex, MESA_SHADER_GEOMETRY);
3228
3229 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
3230 gs.OutputTopology = gs_prog_data->output_topology;
3231 gs.ControlDataHeaderSize =
3232 gs_prog_data->control_data_header_size_hwords;
3233 gs.InstanceControl = gs_prog_data->invocations - 1;
3234 gs.DispatchMode = DISPATCH_MODE_SIMD8;
3235 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
3236 gs.ControlDataFormat = gs_prog_data->control_data_format;
3237 gs.ReorderMode = TRAILING;
3238 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
3239 gs.MaximumNumberofThreads =
3240 GEN_GEN == 8 ? (devinfo->max_gs_threads / 2 - 1)
3241 : (devinfo->max_gs_threads - 1);
3242
3243 if (gs_prog_data->static_vertex_count != -1) {
3244 gs.StaticOutput = true;
3245 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count;
3246 }
3247 gs.IncludeVertexHandles = vue_prog_data->include_vue_handles;
3248
3249 gs.UserClipDistanceCullTestEnableBitmask =
3250 vue_prog_data->cull_distance_mask;
3251
3252 const int urb_entry_write_offset = 1;
3253 const uint32_t urb_entry_output_length =
3254 DIV_ROUND_UP(vue_prog_data->vue_map.num_slots, 2) -
3255 urb_entry_write_offset;
3256
3257 gs.VertexURBEntryOutputReadOffset = urb_entry_write_offset;
3258 gs.VertexURBEntryOutputLength = MAX2(urb_entry_output_length, 1);
3259 }
3260 }
3261
3262 /**
3263 * Encode most of 3DSTATE_PS and 3DSTATE_PS_EXTRA based on the shader.
3264 */
3265 static void
3266 iris_store_fs_state(struct iris_context *ice,
3267 const struct gen_device_info *devinfo,
3268 struct iris_compiled_shader *shader)
3269 {
3270 struct brw_stage_prog_data *prog_data = shader->prog_data;
3271 struct brw_wm_prog_data *wm_prog_data = (void *) shader->prog_data;
3272
3273 uint32_t *ps_state = (void *) shader->derived_data;
3274 uint32_t *psx_state = ps_state + GENX(3DSTATE_PS_length);
3275
3276 iris_pack_command(GENX(3DSTATE_PS), ps_state, ps) {
3277 ps.VectorMaskEnable = true;
3278 //ps.SamplerCount = ...
3279 // XXX: WABTPPrefetchDisable, see above, drop at C0
3280 ps.BindingTableEntryCount = GEN_GEN == 11 ? 0 :
3281 prog_data->binding_table.size_bytes / 4;
3282 ps.FloatingPointMode = prog_data->use_alt_mode;
3283 ps.MaximumNumberofThreadsPerPSD = 64 - (GEN_GEN == 8 ? 2 : 1);
3284
3285 ps.PushConstantEnable = shader->num_system_values > 0 ||
3286 prog_data->ubo_ranges[0].length > 0;
3287
3288 /* From the documentation for this packet:
3289 * "If the PS kernel does not need the Position XY Offsets to
3290 * compute a Position Value, then this field should be programmed
3291 * to POSOFFSET_NONE."
3292 *
3293 * "SW Recommendation: If the PS kernel needs the Position Offsets
3294 * to compute a Position XY value, this field should match Position
3295 * ZW Interpolation Mode to ensure a consistent position.xyzw
3296 * computation."
3297 *
3298 * We only require XY sample offsets. So, this recommendation doesn't
3299 * look useful at the moment. We might need this in future.
3300 */
3301 ps.PositionXYOffsetSelect =
3302 wm_prog_data->uses_pos_offset ? POSOFFSET_SAMPLE : POSOFFSET_NONE;
3303 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
3304 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
3305 ps._32PixelDispatchEnable = wm_prog_data->dispatch_32;
3306
3307 // XXX: Disable SIMD32 with 16x MSAA
3308
3309 ps.DispatchGRFStartRegisterForConstantSetupData0 =
3310 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 0);
3311 ps.DispatchGRFStartRegisterForConstantSetupData1 =
3312 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 1);
3313 ps.DispatchGRFStartRegisterForConstantSetupData2 =
3314 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 2);
3315
3316 ps.KernelStartPointer0 =
3317 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 0);
3318 ps.KernelStartPointer1 =
3319 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 1);
3320 ps.KernelStartPointer2 =
3321 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
3322
3323 if (prog_data->total_scratch) {
3324 uint32_t scratch_addr =
3325 iris_get_scratch_space(ice, prog_data->total_scratch,
3326 MESA_SHADER_FRAGMENT);
3327 ps.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11;
3328 ps.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr);
3329 }
3330 }
3331
3332 iris_pack_command(GENX(3DSTATE_PS_EXTRA), psx_state, psx) {
3333 psx.PixelShaderValid = true;
3334 psx.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
3335 // XXX: alpha test / alpha to coverage :/
3336 psx.PixelShaderKillsPixel = wm_prog_data->uses_kill ||
3337 wm_prog_data->uses_omask;
3338 psx.AttributeEnable = wm_prog_data->num_varying_inputs != 0;
3339 psx.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
3340 psx.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
3341 psx.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
3342
3343 if (wm_prog_data->uses_sample_mask) {
3344 /* TODO: conservative rasterization */
3345 if (wm_prog_data->post_depth_coverage)
3346 psx.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
3347 else
3348 psx.InputCoverageMaskState = ICMS_NORMAL;
3349 }
3350
3351 psx.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
3352 psx.PixelShaderPullsBary = wm_prog_data->pulls_bary;
3353 psx.PixelShaderComputesStencil = wm_prog_data->computed_stencil;
3354
3355 // XXX: UAV bit
3356 }
3357 }
3358
3359 /**
3360 * Compute the size of the derived data (shader command packets).
3361 *
3362 * This must match the data written by the iris_store_xs_state() functions.
3363 */
3364 static void
3365 iris_store_cs_state(struct iris_context *ice,
3366 const struct gen_device_info *devinfo,
3367 struct iris_compiled_shader *shader)
3368 {
3369 struct brw_stage_prog_data *prog_data = shader->prog_data;
3370 struct brw_cs_prog_data *cs_prog_data = (void *) shader->prog_data;
3371 void *map = shader->derived_data;
3372
3373 iris_pack_state(GENX(INTERFACE_DESCRIPTOR_DATA), map, desc) {
3374 desc.KernelStartPointer = KSP(shader);
3375 desc.ConstantURBEntryReadLength = cs_prog_data->push.per_thread.regs;
3376 desc.NumberofThreadsinGPGPUThreadGroup = cs_prog_data->threads;
3377 desc.SharedLocalMemorySize =
3378 encode_slm_size(GEN_GEN, prog_data->total_shared);
3379 desc.BarrierEnable = cs_prog_data->uses_barrier;
3380 desc.CrossThreadConstantDataReadLength =
3381 cs_prog_data->push.cross_thread.regs;
3382 }
3383 }
3384
3385 static unsigned
3386 iris_derived_program_state_size(enum iris_program_cache_id cache_id)
3387 {
3388 assert(cache_id <= IRIS_CACHE_BLORP);
3389
3390 static const unsigned dwords[] = {
3391 [IRIS_CACHE_VS] = GENX(3DSTATE_VS_length),
3392 [IRIS_CACHE_TCS] = GENX(3DSTATE_HS_length),
3393 [IRIS_CACHE_TES] = GENX(3DSTATE_TE_length) + GENX(3DSTATE_DS_length),
3394 [IRIS_CACHE_GS] = GENX(3DSTATE_GS_length),
3395 [IRIS_CACHE_FS] =
3396 GENX(3DSTATE_PS_length) + GENX(3DSTATE_PS_EXTRA_length),
3397 [IRIS_CACHE_CS] = GENX(INTERFACE_DESCRIPTOR_DATA_length),
3398 [IRIS_CACHE_BLORP] = 0,
3399 };
3400
3401 return sizeof(uint32_t) * dwords[cache_id];
3402 }
3403
3404 /**
3405 * Create any state packets corresponding to the given shader stage
3406 * (i.e. 3DSTATE_VS) and save them as "derived data" in the shader variant.
3407 * This means that we can look up a program in the in-memory cache and
3408 * get most of the state packet without having to reconstruct it.
3409 */
3410 static void
3411 iris_store_derived_program_state(struct iris_context *ice,
3412 enum iris_program_cache_id cache_id,
3413 struct iris_compiled_shader *shader)
3414 {
3415 struct iris_screen *screen = (void *) ice->ctx.screen;
3416 const struct gen_device_info *devinfo = &screen->devinfo;
3417
3418 switch (cache_id) {
3419 case IRIS_CACHE_VS:
3420 iris_store_vs_state(ice, devinfo, shader);
3421 break;
3422 case IRIS_CACHE_TCS:
3423 iris_store_tcs_state(ice, devinfo, shader);
3424 break;
3425 case IRIS_CACHE_TES:
3426 iris_store_tes_state(ice, devinfo, shader);
3427 break;
3428 case IRIS_CACHE_GS:
3429 iris_store_gs_state(ice, devinfo, shader);
3430 break;
3431 case IRIS_CACHE_FS:
3432 iris_store_fs_state(ice, devinfo, shader);
3433 break;
3434 case IRIS_CACHE_CS:
3435 iris_store_cs_state(ice, devinfo, shader);
3436 case IRIS_CACHE_BLORP:
3437 break;
3438 default:
3439 break;
3440 }
3441 }
3442
3443 /* ------------------------------------------------------------------- */
3444
3445 /**
3446 * Configure the URB.
3447 *
3448 * XXX: write a real comment.
3449 */
3450 static void
3451 iris_upload_urb_config(struct iris_context *ice, struct iris_batch *batch)
3452 {
3453 const struct gen_device_info *devinfo = &batch->screen->devinfo;
3454 const unsigned push_size_kB = 32;
3455 unsigned entries[4];
3456 unsigned start[4];
3457 unsigned size[4];
3458
3459 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
3460 if (!ice->shaders.prog[i]) {
3461 size[i] = 1;
3462 } else {
3463 struct brw_vue_prog_data *vue_prog_data =
3464 (void *) ice->shaders.prog[i]->prog_data;
3465 size[i] = vue_prog_data->urb_entry_size;
3466 }
3467 assert(size[i] != 0);
3468 }
3469
3470 gen_get_urb_config(devinfo, 1024 * push_size_kB,
3471 1024 * ice->shaders.urb_size,
3472 ice->shaders.prog[MESA_SHADER_TESS_EVAL] != NULL,
3473 ice->shaders.prog[MESA_SHADER_GEOMETRY] != NULL,
3474 size, entries, start);
3475
3476 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
3477 iris_emit_cmd(batch, GENX(3DSTATE_URB_VS), urb) {
3478 urb._3DCommandSubOpcode += i;
3479 urb.VSURBStartingAddress = start[i];
3480 urb.VSURBEntryAllocationSize = size[i] - 1;
3481 urb.VSNumberofURBEntries = entries[i];
3482 }
3483 }
3484 }
3485
3486 static const uint32_t push_constant_opcodes[] = {
3487 [MESA_SHADER_VERTEX] = 21,
3488 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
3489 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
3490 [MESA_SHADER_GEOMETRY] = 22,
3491 [MESA_SHADER_FRAGMENT] = 23,
3492 [MESA_SHADER_COMPUTE] = 0,
3493 };
3494
3495 static uint32_t
3496 use_null_surface(struct iris_batch *batch, struct iris_context *ice)
3497 {
3498 struct iris_bo *state_bo = iris_resource_bo(ice->state.unbound_tex.res);
3499
3500 iris_use_pinned_bo(batch, state_bo, false);
3501
3502 return ice->state.unbound_tex.offset;
3503 }
3504
3505 static uint32_t
3506 use_null_fb_surface(struct iris_batch *batch, struct iris_context *ice)
3507 {
3508 /* If set_framebuffer_state() was never called, fall back to 1x1x1 */
3509 if (!ice->state.null_fb.res)
3510 return use_null_surface(batch, ice);
3511
3512 struct iris_bo *state_bo = iris_resource_bo(ice->state.null_fb.res);
3513
3514 iris_use_pinned_bo(batch, state_bo, false);
3515
3516 return ice->state.null_fb.offset;
3517 }
3518
3519 /**
3520 * Add a surface to the validation list, as well as the buffer containing
3521 * the corresponding SURFACE_STATE.
3522 *
3523 * Returns the binding table entry (offset to SURFACE_STATE).
3524 */
3525 static uint32_t
3526 use_surface(struct iris_batch *batch,
3527 struct pipe_surface *p_surf,
3528 bool writeable)
3529 {
3530 struct iris_surface *surf = (void *) p_surf;
3531
3532 iris_use_pinned_bo(batch, iris_resource_bo(p_surf->texture), writeable);
3533 iris_use_pinned_bo(batch, iris_resource_bo(surf->surface_state.res), false);
3534
3535 return surf->surface_state.offset;
3536 }
3537
3538 static uint32_t
3539 use_sampler_view(struct iris_batch *batch, struct iris_sampler_view *isv)
3540 {
3541 iris_use_pinned_bo(batch, isv->res->bo, false);
3542 iris_use_pinned_bo(batch, iris_resource_bo(isv->surface_state.res), false);
3543
3544 return isv->surface_state.offset;
3545 }
3546
3547 static uint32_t
3548 use_const_buffer(struct iris_batch *batch,
3549 struct iris_context *ice,
3550 struct iris_const_buffer *cbuf)
3551 {
3552 if (!cbuf->surface_state.res)
3553 return use_null_surface(batch, ice);
3554
3555 iris_use_pinned_bo(batch, iris_resource_bo(cbuf->data.res), false);
3556 iris_use_pinned_bo(batch, iris_resource_bo(cbuf->surface_state.res), false);
3557
3558 return cbuf->surface_state.offset;
3559 }
3560
3561 static uint32_t
3562 use_ssbo(struct iris_batch *batch, struct iris_context *ice,
3563 struct iris_shader_state *shs, int i)
3564 {
3565 if (!shs->ssbo[i])
3566 return use_null_surface(batch, ice);
3567
3568 struct iris_state_ref *surf_state = &shs->ssbo_surface_state[i];
3569
3570 iris_use_pinned_bo(batch, iris_resource_bo(shs->ssbo[i]), true);
3571 iris_use_pinned_bo(batch, iris_resource_bo(surf_state->res), false);
3572
3573 return surf_state->offset;
3574 }
3575
3576 static uint32_t
3577 use_image(struct iris_batch *batch, struct iris_context *ice,
3578 struct iris_shader_state *shs, int i)
3579 {
3580 if (!shs->image[i].res)
3581 return use_null_surface(batch, ice);
3582
3583 struct iris_state_ref *surf_state = &shs->image[i].surface_state;
3584
3585 iris_use_pinned_bo(batch, iris_resource_bo(shs->image[i].res),
3586 shs->image[i].access & PIPE_IMAGE_ACCESS_WRITE);
3587 iris_use_pinned_bo(batch, iris_resource_bo(surf_state->res), false);
3588
3589 return surf_state->offset;
3590 }
3591
3592 #define push_bt_entry(addr) \
3593 assert(addr >= binder_addr); \
3594 assert(s < prog_data->binding_table.size_bytes / sizeof(uint32_t)); \
3595 if (!pin_only) bt_map[s++] = (addr) - binder_addr;
3596
3597 #define bt_assert(section, exists) \
3598 if (!pin_only) assert(prog_data->binding_table.section == \
3599 (exists) ? s : 0xd0d0d0d0)
3600
3601 /**
3602 * Populate the binding table for a given shader stage.
3603 *
3604 * This fills out the table of pointers to surfaces required by the shader,
3605 * and also adds those buffers to the validation list so the kernel can make
3606 * resident before running our batch.
3607 */
3608 static void
3609 iris_populate_binding_table(struct iris_context *ice,
3610 struct iris_batch *batch,
3611 gl_shader_stage stage,
3612 bool pin_only)
3613 {
3614 const struct iris_binder *binder = &ice->state.binder;
3615 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3616 if (!shader)
3617 return;
3618
3619 UNUSED struct brw_stage_prog_data *prog_data = shader->prog_data;
3620 struct iris_shader_state *shs = &ice->state.shaders[stage];
3621 uint32_t binder_addr = binder->bo->gtt_offset;
3622
3623 //struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
3624 uint32_t *bt_map = binder->map + binder->bt_offset[stage];
3625 int s = 0;
3626
3627 const struct shader_info *info = iris_get_shader_info(ice, stage);
3628 if (!info) {
3629 /* TCS passthrough doesn't need a binding table. */
3630 assert(stage == MESA_SHADER_TESS_CTRL);
3631 return;
3632 }
3633
3634 if (stage == MESA_SHADER_COMPUTE) {
3635 /* surface for gl_NumWorkGroups */
3636 struct iris_state_ref *grid_data = &ice->state.grid_size;
3637 struct iris_state_ref *grid_state = &ice->state.grid_surf_state;
3638 iris_use_pinned_bo(batch, iris_resource_bo(grid_data->res), false);
3639 iris_use_pinned_bo(batch, iris_resource_bo(grid_state->res), false);
3640 push_bt_entry(grid_state->offset);
3641 }
3642
3643 if (stage == MESA_SHADER_FRAGMENT) {
3644 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
3645 /* Note that cso_fb->nr_cbufs == fs_key->nr_color_regions. */
3646 if (cso_fb->nr_cbufs) {
3647 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
3648 uint32_t addr =
3649 cso_fb->cbufs[i] ? use_surface(batch, cso_fb->cbufs[i], true)
3650 : use_null_fb_surface(batch, ice);
3651 push_bt_entry(addr);
3652 }
3653 } else {
3654 uint32_t addr = use_null_fb_surface(batch, ice);
3655 push_bt_entry(addr);
3656 }
3657 }
3658
3659 bt_assert(texture_start, info->num_textures > 0);
3660
3661 for (int i = 0; i < info->num_textures; i++) {
3662 struct iris_sampler_view *view = shs->textures[i];
3663 uint32_t addr = view ? use_sampler_view(batch, view)
3664 : use_null_surface(batch, ice);
3665 push_bt_entry(addr);
3666 }
3667
3668 bt_assert(image_start, info->num_images > 0);
3669
3670 for (int i = 0; i < info->num_images; i++) {
3671 uint32_t addr = use_image(batch, ice, shs, i);
3672 push_bt_entry(addr);
3673 }
3674
3675 const int num_ubos = iris_get_shader_num_ubos(ice, stage);
3676
3677 bt_assert(ubo_start, num_ubos > 0);
3678
3679 for (int i = 0; i < num_ubos; i++) {
3680 uint32_t addr = use_const_buffer(batch, ice, &shs->constbuf[i]);
3681 push_bt_entry(addr);
3682 }
3683
3684 bt_assert(ssbo_start, info->num_abos + info->num_ssbos > 0);
3685
3686 /* XXX: st is wasting 16 binding table slots for ABOs. Should add a cap
3687 * for changing nir_lower_atomics_to_ssbos setting and buffer_base offset
3688 * in st_atom_storagebuf.c so it'll compact them into one range, with
3689 * SSBOs starting at info->num_abos. Ideally it'd reset num_abos to 0 too
3690 */
3691 if (info->num_abos + info->num_ssbos > 0) {
3692 for (int i = 0; i < IRIS_MAX_ABOS + info->num_ssbos; i++) {
3693 uint32_t addr = use_ssbo(batch, ice, shs, i);
3694 push_bt_entry(addr);
3695 }
3696 }
3697
3698 #if 0
3699 // XXX: not implemented yet
3700 bt_assert(plane_start[1], ...);
3701 bt_assert(plane_start[2], ...);
3702 #endif
3703 }
3704
3705 static void
3706 iris_use_optional_res(struct iris_batch *batch,
3707 struct pipe_resource *res,
3708 bool writeable)
3709 {
3710 if (res) {
3711 struct iris_bo *bo = iris_resource_bo(res);
3712 iris_use_pinned_bo(batch, bo, writeable);
3713 }
3714 }
3715
3716 /* ------------------------------------------------------------------- */
3717
3718 /**
3719 * Pin any BOs which were installed by a previous batch, and restored
3720 * via the hardware logical context mechanism.
3721 *
3722 * We don't need to re-emit all state every batch - the hardware context
3723 * mechanism will save and restore it for us. This includes pointers to
3724 * various BOs...which won't exist unless we ask the kernel to pin them
3725 * by adding them to the validation list.
3726 *
3727 * We can skip buffers if we've re-emitted those packets, as we're
3728 * overwriting those stale pointers with new ones, and don't actually
3729 * refer to the old BOs.
3730 */
3731 static void
3732 iris_restore_render_saved_bos(struct iris_context *ice,
3733 struct iris_batch *batch,
3734 const struct pipe_draw_info *draw)
3735 {
3736 // XXX: whack IRIS_SHADER_DIRTY_BINDING_TABLE on new batch
3737
3738 const uint64_t clean = ~ice->state.dirty;
3739
3740 if (clean & IRIS_DIRTY_CC_VIEWPORT) {
3741 iris_use_optional_res(batch, ice->state.last_res.cc_vp, false);
3742 }
3743
3744 if (clean & IRIS_DIRTY_SF_CL_VIEWPORT) {
3745 iris_use_optional_res(batch, ice->state.last_res.sf_cl_vp, false);
3746 }
3747
3748 if (clean & IRIS_DIRTY_BLEND_STATE) {
3749 iris_use_optional_res(batch, ice->state.last_res.blend, false);
3750 }
3751
3752 if (clean & IRIS_DIRTY_COLOR_CALC_STATE) {
3753 iris_use_optional_res(batch, ice->state.last_res.color_calc, false);
3754 }
3755
3756 if (clean & IRIS_DIRTY_SCISSOR_RECT) {
3757 iris_use_optional_res(batch, ice->state.last_res.scissor, false);
3758 }
3759
3760 if (ice->state.streamout_active && (clean & IRIS_DIRTY_SO_BUFFERS)) {
3761 for (int i = 0; i < 4; i++) {
3762 struct iris_stream_output_target *tgt =
3763 (void *) ice->state.so_target[i];
3764 if (tgt) {
3765 iris_use_pinned_bo(batch, iris_resource_bo(tgt->base.buffer),
3766 true);
3767 iris_use_pinned_bo(batch, iris_resource_bo(tgt->offset.res),
3768 true);
3769 }
3770 }
3771 }
3772
3773 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3774 if (!(clean & (IRIS_DIRTY_CONSTANTS_VS << stage)))
3775 continue;
3776
3777 struct iris_shader_state *shs = &ice->state.shaders[stage];
3778 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3779
3780 if (!shader)
3781 continue;
3782
3783 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
3784
3785 for (int i = 0; i < 4; i++) {
3786 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
3787
3788 if (range->length == 0)
3789 continue;
3790
3791 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
3792 struct iris_resource *res = (void *) cbuf->data.res;
3793
3794 if (res)
3795 iris_use_pinned_bo(batch, res->bo, false);
3796 else
3797 iris_use_pinned_bo(batch, batch->screen->workaround_bo, false);
3798 }
3799 }
3800
3801 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3802 if (clean & (IRIS_DIRTY_BINDINGS_VS << stage)) {
3803 /* Re-pin any buffers referred to by the binding table. */
3804 iris_populate_binding_table(ice, batch, stage, true);
3805 }
3806 }
3807
3808 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3809 struct iris_shader_state *shs = &ice->state.shaders[stage];
3810 struct pipe_resource *res = shs->sampler_table.res;
3811 if (res)
3812 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
3813 }
3814
3815 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3816 if (clean & (IRIS_DIRTY_VS << stage)) {
3817 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3818 if (shader) {
3819 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
3820 iris_use_pinned_bo(batch, bo, false);
3821 }
3822
3823 // XXX: scratch buffer
3824 }
3825 }
3826
3827 if (clean & IRIS_DIRTY_DEPTH_BUFFER) {
3828 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
3829
3830 if (cso_fb->zsbuf) {
3831 struct iris_resource *zres, *sres;
3832 iris_get_depth_stencil_resources(cso_fb->zsbuf->texture,
3833 &zres, &sres);
3834 // XXX: might not be writable...
3835 if (zres)
3836 iris_use_pinned_bo(batch, zres->bo, true);
3837 if (sres)
3838 iris_use_pinned_bo(batch, sres->bo, true);
3839 }
3840 }
3841
3842 if (draw->index_size == 0 && ice->state.last_res.index_buffer) {
3843 /* This draw didn't emit a new index buffer, so we are inheriting the
3844 * older index buffer. This draw didn't need it, but future ones may.
3845 */
3846 struct iris_bo *bo = iris_resource_bo(ice->state.last_res.index_buffer);
3847 iris_use_pinned_bo(batch, bo, false);
3848 }
3849
3850 if (clean & IRIS_DIRTY_VERTEX_BUFFERS) {
3851 struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
3852 for (unsigned i = 0; i < cso->num_buffers; i++) {
3853 struct iris_resource *res = (void *) cso->resources[i];
3854 iris_use_pinned_bo(batch, res->bo, false);
3855 }
3856 }
3857 }
3858
3859 static void
3860 iris_restore_compute_saved_bos(struct iris_context *ice,
3861 struct iris_batch *batch,
3862 const struct pipe_grid_info *grid)
3863 {
3864 const uint64_t clean = ~ice->state.dirty;
3865
3866 const int stage = MESA_SHADER_COMPUTE;
3867 struct iris_shader_state *shs = &ice->state.shaders[stage];
3868
3869 if (clean & IRIS_DIRTY_CONSTANTS_CS) {
3870 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3871
3872 if (shader) {
3873 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
3874 const struct brw_ubo_range *range = &prog_data->ubo_ranges[0];
3875
3876 if (range->length > 0) {
3877 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
3878 struct iris_resource *res = (void *) cbuf->data.res;
3879
3880 if (res)
3881 iris_use_pinned_bo(batch, res->bo, false);
3882 else
3883 iris_use_pinned_bo(batch, batch->screen->workaround_bo, false);
3884 }
3885 }
3886 }
3887
3888 if (clean & IRIS_DIRTY_BINDINGS_CS) {
3889 /* Re-pin any buffers referred to by the binding table. */
3890 iris_populate_binding_table(ice, batch, stage, true);
3891 }
3892
3893 struct pipe_resource *sampler_res = shs->sampler_table.res;
3894 if (sampler_res)
3895 iris_use_pinned_bo(batch, iris_resource_bo(sampler_res), false);
3896
3897 if (clean & IRIS_DIRTY_CS) {
3898 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3899 if (shader) {
3900 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
3901 iris_use_pinned_bo(batch, bo, false);
3902 }
3903
3904 // XXX: scratch buffer
3905 }
3906 }
3907
3908 /**
3909 * Possibly emit STATE_BASE_ADDRESS to update Surface State Base Address.
3910 */
3911 static void
3912 iris_update_surface_base_address(struct iris_batch *batch,
3913 struct iris_binder *binder)
3914 {
3915 if (batch->last_surface_base_address == binder->bo->gtt_offset)
3916 return;
3917
3918 flush_for_state_base_change(batch);
3919
3920 iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
3921 // XXX: sba.SurfaceStateMemoryObjectControlState = MOCS_WB;
3922 sba.SurfaceStateBaseAddressModifyEnable = true;
3923 sba.SurfaceStateBaseAddress = ro_bo(binder->bo, 0);
3924 }
3925
3926 batch->last_surface_base_address = binder->bo->gtt_offset;
3927 }
3928
3929 static void
3930 iris_upload_dirty_render_state(struct iris_context *ice,
3931 struct iris_batch *batch,
3932 const struct pipe_draw_info *draw)
3933 {
3934 const uint64_t dirty = ice->state.dirty;
3935
3936 if (!(dirty & IRIS_ALL_DIRTY_FOR_RENDER))
3937 return;
3938
3939 struct iris_genx_state *genx = ice->state.genx;
3940 struct iris_binder *binder = &ice->state.binder;
3941 struct brw_wm_prog_data *wm_prog_data = (void *)
3942 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
3943
3944 if (dirty & IRIS_DIRTY_CC_VIEWPORT) {
3945 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
3946 uint32_t cc_vp_address;
3947
3948 /* XXX: could avoid streaming for depth_clip [0,1] case. */
3949 uint32_t *cc_vp_map =
3950 stream_state(batch, ice->state.dynamic_uploader,
3951 &ice->state.last_res.cc_vp,
3952 4 * ice->state.num_viewports *
3953 GENX(CC_VIEWPORT_length), 32, &cc_vp_address);
3954 for (int i = 0; i < ice->state.num_viewports; i++) {
3955 float zmin, zmax;
3956 util_viewport_zmin_zmax(&ice->state.viewports[i],
3957 cso_rast->clip_halfz, &zmin, &zmax);
3958 if (cso_rast->depth_clip_near)
3959 zmin = 0.0;
3960 if (cso_rast->depth_clip_far)
3961 zmax = 1.0;
3962
3963 iris_pack_state(GENX(CC_VIEWPORT), cc_vp_map, ccv) {
3964 ccv.MinimumDepth = zmin;
3965 ccv.MaximumDepth = zmax;
3966 }
3967
3968 cc_vp_map += GENX(CC_VIEWPORT_length);
3969 }
3970
3971 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), ptr) {
3972 ptr.CCViewportPointer = cc_vp_address;
3973 }
3974 }
3975
3976 if (dirty & IRIS_DIRTY_SF_CL_VIEWPORT) {
3977 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
3978 ptr.SFClipViewportPointer =
3979 emit_state(batch, ice->state.dynamic_uploader,
3980 &ice->state.last_res.sf_cl_vp,
3981 genx->sf_cl_vp, 4 * GENX(SF_CLIP_VIEWPORT_length) *
3982 ice->state.num_viewports, 64);
3983 }
3984 }
3985
3986 /* XXX: L3 State */
3987
3988 // XXX: this is only flagged at setup, we assume a static configuration
3989 if (dirty & IRIS_DIRTY_URB) {
3990 iris_upload_urb_config(ice, batch);
3991 }
3992
3993 if (dirty & IRIS_DIRTY_BLEND_STATE) {
3994 struct iris_blend_state *cso_blend = ice->state.cso_blend;
3995 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
3996 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
3997 const int header_dwords = GENX(BLEND_STATE_length);
3998 const int rt_dwords = cso_fb->nr_cbufs * GENX(BLEND_STATE_ENTRY_length);
3999 uint32_t blend_offset;
4000 uint32_t *blend_map =
4001 stream_state(batch, ice->state.dynamic_uploader,
4002 &ice->state.last_res.blend,
4003 4 * (header_dwords + rt_dwords), 64, &blend_offset);
4004
4005 uint32_t blend_state_header;
4006 iris_pack_state(GENX(BLEND_STATE), &blend_state_header, bs) {
4007 bs.AlphaTestEnable = cso_zsa->alpha.enabled;
4008 bs.AlphaTestFunction = translate_compare_func(cso_zsa->alpha.func);
4009 }
4010
4011 blend_map[0] = blend_state_header | cso_blend->blend_state[0];
4012 memcpy(&blend_map[1], &cso_blend->blend_state[1], 4 * rt_dwords);
4013
4014 iris_emit_cmd(batch, GENX(3DSTATE_BLEND_STATE_POINTERS), ptr) {
4015 ptr.BlendStatePointer = blend_offset;
4016 ptr.BlendStatePointerValid = true;
4017 }
4018 }
4019
4020 if (dirty & IRIS_DIRTY_COLOR_CALC_STATE) {
4021 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
4022 uint32_t cc_offset;
4023 void *cc_map =
4024 stream_state(batch, ice->state.dynamic_uploader,
4025 &ice->state.last_res.color_calc,
4026 sizeof(uint32_t) * GENX(COLOR_CALC_STATE_length),
4027 64, &cc_offset);
4028 iris_pack_state(GENX(COLOR_CALC_STATE), cc_map, cc) {
4029 cc.AlphaTestFormat = ALPHATEST_FLOAT32;
4030 cc.AlphaReferenceValueAsFLOAT32 = cso->alpha.ref_value;
4031 cc.BlendConstantColorRed = ice->state.blend_color.color[0];
4032 cc.BlendConstantColorGreen = ice->state.blend_color.color[1];
4033 cc.BlendConstantColorBlue = ice->state.blend_color.color[2];
4034 cc.BlendConstantColorAlpha = ice->state.blend_color.color[3];
4035 }
4036 iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
4037 ptr.ColorCalcStatePointer = cc_offset;
4038 ptr.ColorCalcStatePointerValid = true;
4039 }
4040 }
4041
4042 /* Upload constants for TCS passthrough. */
4043 if ((dirty & IRIS_DIRTY_CONSTANTS_TCS) &&
4044 ice->shaders.prog[MESA_SHADER_TESS_CTRL] &&
4045 !ice->shaders.uncompiled[MESA_SHADER_TESS_CTRL]) {
4046 struct iris_compiled_shader *tes_shader = ice->shaders.prog[MESA_SHADER_TESS_EVAL];
4047 assert(tes_shader);
4048
4049 /* Passthrough always copies 2 vec4s, so when uploading data we ensure
4050 * it is in the right layout for TES.
4051 */
4052 float hdr[8] = {};
4053 struct brw_tes_prog_data *tes_prog_data = (void *) tes_shader->prog_data;
4054 switch (tes_prog_data->domain) {
4055 case BRW_TESS_DOMAIN_QUAD:
4056 for (int i = 0; i < 4; i++)
4057 hdr[7 - i] = ice->state.default_outer_level[i];
4058 hdr[3] = ice->state.default_inner_level[0];
4059 hdr[2] = ice->state.default_inner_level[1];
4060 break;
4061 case BRW_TESS_DOMAIN_TRI:
4062 for (int i = 0; i < 3; i++)
4063 hdr[7 - i] = ice->state.default_outer_level[i];
4064 hdr[4] = ice->state.default_inner_level[0];
4065 break;
4066 case BRW_TESS_DOMAIN_ISOLINE:
4067 hdr[7] = ice->state.default_outer_level[1];
4068 hdr[6] = ice->state.default_outer_level[0];
4069 break;
4070 }
4071
4072 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_TESS_CTRL];
4073 struct iris_const_buffer *cbuf = &shs->constbuf[0];
4074 u_upload_data(ice->ctx.const_uploader, 0, sizeof(hdr), 32,
4075 &hdr[0], &cbuf->data.offset,
4076 &cbuf->data.res);
4077 }
4078
4079 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4080 if (!(dirty & (IRIS_DIRTY_CONSTANTS_VS << stage)))
4081 continue;
4082
4083 struct iris_shader_state *shs = &ice->state.shaders[stage];
4084 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
4085
4086 if (!shader)
4087 continue;
4088
4089 if (shs->cbuf0_needs_upload)
4090 upload_uniforms(ice, stage);
4091
4092 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
4093
4094 iris_emit_cmd(batch, GENX(3DSTATE_CONSTANT_VS), pkt) {
4095 pkt._3DCommandSubOpcode = push_constant_opcodes[stage];
4096 if (prog_data) {
4097 /* The Skylake PRM contains the following restriction:
4098 *
4099 * "The driver must ensure The following case does not occur
4100 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
4101 * buffer 3 read length equal to zero committed followed by a
4102 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
4103 * zero committed."
4104 *
4105 * To avoid this, we program the buffers in the highest slots.
4106 * This way, slot 0 is only used if slot 3 is also used.
4107 */
4108 int n = 3;
4109
4110 for (int i = 3; i >= 0; i--) {
4111 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
4112
4113 if (range->length == 0)
4114 continue;
4115
4116 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
4117 struct iris_resource *res = (void *) cbuf->data.res;
4118
4119 assert(cbuf->data.offset % 32 == 0);
4120
4121 pkt.ConstantBody.ReadLength[n] = range->length;
4122 pkt.ConstantBody.Buffer[n] =
4123 res ? ro_bo(res->bo, range->start * 32 + cbuf->data.offset)
4124 : ro_bo(batch->screen->workaround_bo, 0);
4125 n--;
4126 }
4127 }
4128 }
4129 }
4130
4131 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4132 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
4133 iris_emit_cmd(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), ptr) {
4134 ptr._3DCommandSubOpcode = 38 + stage;
4135 ptr.PointertoVSBindingTable = binder->bt_offset[stage];
4136 }
4137 }
4138 }
4139
4140 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4141 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
4142 iris_populate_binding_table(ice, batch, stage, false);
4143 }
4144 }
4145
4146 if (ice->state.need_border_colors)
4147 iris_use_pinned_bo(batch, ice->state.border_color_pool.bo, false);
4148
4149 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4150 if (!(dirty & (IRIS_DIRTY_SAMPLER_STATES_VS << stage)) ||
4151 !ice->shaders.prog[stage])
4152 continue;
4153
4154 struct iris_shader_state *shs = &ice->state.shaders[stage];
4155 struct pipe_resource *res = shs->sampler_table.res;
4156 if (res)
4157 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
4158
4159 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ptr) {
4160 ptr._3DCommandSubOpcode = 43 + stage;
4161 ptr.PointertoVSSamplerState = shs->sampler_table.offset;
4162 }
4163 }
4164
4165 if (dirty & IRIS_DIRTY_MULTISAMPLE) {
4166 iris_emit_cmd(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
4167 ms.PixelLocation =
4168 ice->state.cso_rast->half_pixel_center ? CENTER : UL_CORNER;
4169 if (ice->state.framebuffer.samples > 0)
4170 ms.NumberofMultisamples = ffs(ice->state.framebuffer.samples) - 1;
4171 }
4172 }
4173
4174 if (dirty & IRIS_DIRTY_SAMPLE_MASK) {
4175 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_MASK), ms) {
4176 ms.SampleMask = MAX2(ice->state.sample_mask, 1);
4177 }
4178 }
4179
4180 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4181 if (!(dirty & (IRIS_DIRTY_VS << stage)))
4182 continue;
4183
4184 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
4185
4186 if (shader) {
4187 struct iris_resource *cache = (void *) shader->assembly.res;
4188 iris_use_pinned_bo(batch, cache->bo, false);
4189 iris_batch_emit(batch, shader->derived_data,
4190 iris_derived_program_state_size(stage));
4191 } else {
4192 if (stage == MESA_SHADER_TESS_EVAL) {
4193 iris_emit_cmd(batch, GENX(3DSTATE_HS), hs);
4194 iris_emit_cmd(batch, GENX(3DSTATE_TE), te);
4195 iris_emit_cmd(batch, GENX(3DSTATE_DS), ds);
4196 } else if (stage == MESA_SHADER_GEOMETRY) {
4197 iris_emit_cmd(batch, GENX(3DSTATE_GS), gs);
4198 }
4199 }
4200 }
4201
4202 if (ice->state.streamout_active) {
4203 if (dirty & IRIS_DIRTY_SO_BUFFERS) {
4204 iris_batch_emit(batch, genx->so_buffers,
4205 4 * 4 * GENX(3DSTATE_SO_BUFFER_length));
4206 for (int i = 0; i < 4; i++) {
4207 struct iris_stream_output_target *tgt =
4208 (void *) ice->state.so_target[i];
4209 if (tgt) {
4210 iris_use_pinned_bo(batch, iris_resource_bo(tgt->base.buffer),
4211 true);
4212 iris_use_pinned_bo(batch, iris_resource_bo(tgt->offset.res),
4213 true);
4214 }
4215 }
4216 }
4217
4218 if ((dirty & IRIS_DIRTY_SO_DECL_LIST) && ice->state.streamout) {
4219 uint32_t *decl_list =
4220 ice->state.streamout + GENX(3DSTATE_STREAMOUT_length);
4221 iris_batch_emit(batch, decl_list, 4 * ((decl_list[0] & 0xff) + 2));
4222 }
4223
4224 if (dirty & IRIS_DIRTY_STREAMOUT) {
4225 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
4226
4227 uint32_t dynamic_sol[GENX(3DSTATE_STREAMOUT_length)];
4228 iris_pack_command(GENX(3DSTATE_STREAMOUT), dynamic_sol, sol) {
4229 sol.SOFunctionEnable = true;
4230 sol.SOStatisticsEnable = true;
4231
4232 sol.RenderingDisable = cso_rast->rasterizer_discard &&
4233 !ice->state.prims_generated_query_active;
4234 sol.ReorderMode = cso_rast->flatshade_first ? LEADING : TRAILING;
4235 }
4236
4237 assert(ice->state.streamout);
4238
4239 iris_emit_merge(batch, ice->state.streamout, dynamic_sol,
4240 GENX(3DSTATE_STREAMOUT_length));
4241 }
4242 } else {
4243 if (dirty & IRIS_DIRTY_STREAMOUT) {
4244 iris_emit_cmd(batch, GENX(3DSTATE_STREAMOUT), sol);
4245 }
4246 }
4247
4248 if (dirty & IRIS_DIRTY_CLIP) {
4249 struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
4250 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
4251
4252 bool reject = cso_rast->rasterizer_discard &&
4253 ice->state.prims_generated_query_active;
4254
4255 uint32_t dynamic_clip[GENX(3DSTATE_CLIP_length)];
4256 iris_pack_command(GENX(3DSTATE_CLIP), &dynamic_clip, cl) {
4257 cl.StatisticsEnable = ice->state.statistics_counters_enabled;
4258 cl.ClipMode = reject ? CLIPMODE_REJECT_ALL : CLIPMODE_NORMAL;
4259 if (wm_prog_data->barycentric_interp_modes &
4260 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS)
4261 cl.NonPerspectiveBarycentricEnable = true;
4262
4263 cl.ForceZeroRTAIndexEnable = cso_fb->layers == 0;
4264 cl.MaximumVPIndex = ice->state.num_viewports - 1;
4265 }
4266 iris_emit_merge(batch, cso_rast->clip, dynamic_clip,
4267 ARRAY_SIZE(cso_rast->clip));
4268 }
4269
4270 if (dirty & IRIS_DIRTY_RASTER) {
4271 struct iris_rasterizer_state *cso = ice->state.cso_rast;
4272 iris_batch_emit(batch, cso->raster, sizeof(cso->raster));
4273 iris_batch_emit(batch, cso->sf, sizeof(cso->sf));
4274
4275 }
4276
4277 /* XXX: FS program updates needs to flag IRIS_DIRTY_WM */
4278 if (dirty & IRIS_DIRTY_WM) {
4279 struct iris_rasterizer_state *cso = ice->state.cso_rast;
4280 uint32_t dynamic_wm[GENX(3DSTATE_WM_length)];
4281
4282 iris_pack_command(GENX(3DSTATE_WM), &dynamic_wm, wm) {
4283 wm.StatisticsEnable = ice->state.statistics_counters_enabled;
4284
4285 wm.BarycentricInterpolationMode =
4286 wm_prog_data->barycentric_interp_modes;
4287
4288 if (wm_prog_data->early_fragment_tests)
4289 wm.EarlyDepthStencilControl = EDSC_PREPS;
4290 else if (wm_prog_data->has_side_effects)
4291 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
4292 }
4293 iris_emit_merge(batch, cso->wm, dynamic_wm, ARRAY_SIZE(cso->wm));
4294 }
4295
4296 if (dirty & IRIS_DIRTY_SBE) {
4297 iris_emit_sbe(batch, ice);
4298 }
4299
4300 if (dirty & IRIS_DIRTY_PS_BLEND) {
4301 struct iris_blend_state *cso_blend = ice->state.cso_blend;
4302 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
4303 uint32_t dynamic_pb[GENX(3DSTATE_PS_BLEND_length)];
4304 iris_pack_command(GENX(3DSTATE_PS_BLEND), &dynamic_pb, pb) {
4305 pb.HasWriteableRT = true; // XXX: comes from somewhere :(
4306 pb.AlphaTestEnable = cso_zsa->alpha.enabled;
4307 }
4308
4309 iris_emit_merge(batch, cso_blend->ps_blend, dynamic_pb,
4310 ARRAY_SIZE(cso_blend->ps_blend));
4311 }
4312
4313 if (dirty & IRIS_DIRTY_WM_DEPTH_STENCIL) {
4314 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
4315 struct pipe_stencil_ref *p_stencil_refs = &ice->state.stencil_ref;
4316
4317 uint32_t stencil_refs[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
4318 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), &stencil_refs, wmds) {
4319 wmds.StencilReferenceValue = p_stencil_refs->ref_value[0];
4320 wmds.BackfaceStencilReferenceValue = p_stencil_refs->ref_value[1];
4321 }
4322 iris_emit_merge(batch, cso->wmds, stencil_refs, ARRAY_SIZE(cso->wmds));
4323 }
4324
4325 if (dirty & IRIS_DIRTY_SCISSOR_RECT) {
4326 uint32_t scissor_offset =
4327 emit_state(batch, ice->state.dynamic_uploader,
4328 &ice->state.last_res.scissor,
4329 ice->state.scissors,
4330 sizeof(struct pipe_scissor_state) *
4331 ice->state.num_viewports, 32);
4332
4333 iris_emit_cmd(batch, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
4334 ptr.ScissorRectPointer = scissor_offset;
4335 }
4336 }
4337
4338 if (dirty & IRIS_DIRTY_DEPTH_BUFFER) {
4339 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
4340 struct iris_depth_buffer_state *cso_z = &ice->state.genx->depth_buffer;
4341
4342 iris_batch_emit(batch, cso_z->packets, sizeof(cso_z->packets));
4343
4344 if (cso_fb->zsbuf) {
4345 struct iris_resource *zres, *sres;
4346 iris_get_depth_stencil_resources(cso_fb->zsbuf->texture,
4347 &zres, &sres);
4348 // XXX: might not be writable...
4349 if (zres)
4350 iris_use_pinned_bo(batch, zres->bo, true);
4351 if (sres)
4352 iris_use_pinned_bo(batch, sres->bo, true);
4353 }
4354 }
4355
4356 if (dirty & IRIS_DIRTY_POLYGON_STIPPLE) {
4357 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
4358 for (int i = 0; i < 32; i++) {
4359 poly.PatternRow[i] = ice->state.poly_stipple.stipple[i];
4360 }
4361 }
4362 }
4363
4364 if (dirty & IRIS_DIRTY_LINE_STIPPLE) {
4365 struct iris_rasterizer_state *cso = ice->state.cso_rast;
4366 iris_batch_emit(batch, cso->line_stipple, sizeof(cso->line_stipple));
4367 }
4368
4369 if (dirty & IRIS_DIRTY_VF_TOPOLOGY) {
4370 iris_emit_cmd(batch, GENX(3DSTATE_VF_TOPOLOGY), topo) {
4371 topo.PrimitiveTopologyType =
4372 translate_prim_type(draw->mode, draw->vertices_per_patch);
4373 }
4374 }
4375
4376 if (dirty & IRIS_DIRTY_VERTEX_BUFFERS) {
4377 struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
4378 const unsigned vb_dwords = GENX(VERTEX_BUFFER_STATE_length);
4379
4380 if (cso->num_buffers > 0) {
4381 /* The VF cache designers cut corners, and made the cache key's
4382 * <VertexBufferIndex, Memory Address> tuple only consider the bottom
4383 * 32 bits of the address. If you have two vertex buffers which get
4384 * placed exactly 4 GiB apart and use them in back-to-back draw calls,
4385 * you can get collisions (even within a single batch).
4386 *
4387 * So, we need to do a VF cache invalidate if the buffer for a VB
4388 * slot slot changes [48:32] address bits from the previous time.
4389 */
4390 unsigned flush_flags = 0;
4391
4392 for (unsigned i = 0; i < cso->num_buffers; i++) {
4393 uint16_t high_bits = 0;
4394
4395 struct iris_resource *res = (void *) cso->resources[i];
4396 if (res) {
4397 iris_use_pinned_bo(batch, res->bo, false);
4398
4399 high_bits = res->bo->gtt_offset >> 32ull;
4400 if (high_bits != ice->state.last_vbo_high_bits[i]) {
4401 flush_flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
4402 ice->state.last_vbo_high_bits[i] = high_bits;
4403 }
4404
4405 /* If the buffer was written to by streamout, we may need
4406 * to stall so those writes land and become visible to the
4407 * vertex fetcher.
4408 *
4409 * TODO: This may stall more than necessary.
4410 */
4411 if (res->bind_history & PIPE_BIND_STREAM_OUTPUT)
4412 flush_flags |= PIPE_CONTROL_CS_STALL;
4413 }
4414 }
4415
4416 if (flush_flags)
4417 iris_emit_pipe_control_flush(batch, flush_flags);
4418
4419 iris_batch_emit(batch, cso->vertex_buffers, sizeof(uint32_t) *
4420 (1 + vb_dwords * cso->num_buffers));
4421 }
4422 }
4423
4424 if (dirty & IRIS_DIRTY_VERTEX_ELEMENTS) {
4425 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
4426 const unsigned entries = MAX2(cso->count, 1);
4427 iris_batch_emit(batch, cso->vertex_elements, sizeof(uint32_t) *
4428 (1 + entries * GENX(VERTEX_ELEMENT_STATE_length)));
4429 iris_batch_emit(batch, cso->vf_instancing, sizeof(uint32_t) *
4430 entries * GENX(3DSTATE_VF_INSTANCING_length));
4431 }
4432
4433 if (dirty & IRIS_DIRTY_VF_SGVS) {
4434 const struct brw_vs_prog_data *vs_prog_data = (void *)
4435 ice->shaders.prog[MESA_SHADER_VERTEX]->prog_data;
4436 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
4437
4438 iris_emit_cmd(batch, GENX(3DSTATE_VF_SGVS), sgv) {
4439 if (vs_prog_data->uses_vertexid) {
4440 sgv.VertexIDEnable = true;
4441 sgv.VertexIDComponentNumber = 2;
4442 sgv.VertexIDElementOffset = cso->count;
4443 }
4444
4445 if (vs_prog_data->uses_instanceid) {
4446 sgv.InstanceIDEnable = true;
4447 sgv.InstanceIDComponentNumber = 3;
4448 sgv.InstanceIDElementOffset = cso->count;
4449 }
4450 }
4451 }
4452
4453 if (dirty & IRIS_DIRTY_VF) {
4454 iris_emit_cmd(batch, GENX(3DSTATE_VF), vf) {
4455 if (draw->primitive_restart) {
4456 vf.IndexedDrawCutIndexEnable = true;
4457 vf.CutIndex = draw->restart_index;
4458 }
4459 }
4460 }
4461
4462 // XXX: Gen8 - PMA fix
4463 }
4464
4465 static void
4466 iris_upload_render_state(struct iris_context *ice,
4467 struct iris_batch *batch,
4468 const struct pipe_draw_info *draw)
4469 {
4470 /* Always pin the binder. If we're emitting new binding table pointers,
4471 * we need it. If not, we're probably inheriting old tables via the
4472 * context, and need it anyway. Since true zero-bindings cases are
4473 * practically non-existent, just pin it and avoid last_res tracking.
4474 */
4475 iris_use_pinned_bo(batch, ice->state.binder.bo, false);
4476
4477 if (!batch->contains_draw) {
4478 iris_restore_render_saved_bos(ice, batch, draw);
4479 batch->contains_draw = true;
4480 }
4481
4482 iris_upload_dirty_render_state(ice, batch, draw);
4483
4484 if (draw->index_size > 0) {
4485 unsigned offset;
4486
4487 if (draw->has_user_indices) {
4488 u_upload_data(ice->ctx.stream_uploader, 0,
4489 draw->count * draw->index_size, 4, draw->index.user,
4490 &offset, &ice->state.last_res.index_buffer);
4491 } else {
4492 struct iris_resource *res = (void *) draw->index.resource;
4493 res->bind_history |= PIPE_BIND_INDEX_BUFFER;
4494
4495 pipe_resource_reference(&ice->state.last_res.index_buffer,
4496 draw->index.resource);
4497 offset = 0;
4498 }
4499
4500 struct iris_bo *bo = iris_resource_bo(ice->state.last_res.index_buffer);
4501
4502 iris_emit_cmd(batch, GENX(3DSTATE_INDEX_BUFFER), ib) {
4503 ib.IndexFormat = draw->index_size >> 1;
4504 ib.MOCS = MOCS_WB;
4505 ib.BufferSize = bo->size;
4506 ib.BufferStartingAddress = ro_bo(bo, offset);
4507 }
4508
4509 /* The VF cache key only uses 32-bits, see vertex buffer comment above */
4510 uint16_t high_bits = bo->gtt_offset >> 32ull;
4511 if (high_bits != ice->state.last_index_bo_high_bits) {
4512 iris_emit_pipe_control_flush(batch, PIPE_CONTROL_VF_CACHE_INVALIDATE);
4513 ice->state.last_index_bo_high_bits = high_bits;
4514 }
4515 }
4516
4517 #define _3DPRIM_END_OFFSET 0x2420
4518 #define _3DPRIM_START_VERTEX 0x2430
4519 #define _3DPRIM_VERTEX_COUNT 0x2434
4520 #define _3DPRIM_INSTANCE_COUNT 0x2438
4521 #define _3DPRIM_START_INSTANCE 0x243C
4522 #define _3DPRIM_BASE_VERTEX 0x2440
4523
4524 if (draw->indirect) {
4525 /* We don't support this MultidrawIndirect. */
4526 assert(!draw->indirect->indirect_draw_count);
4527
4528 struct iris_bo *bo = iris_resource_bo(draw->indirect->buffer);
4529 assert(bo);
4530
4531 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4532 lrm.RegisterAddress = _3DPRIM_VERTEX_COUNT;
4533 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 0);
4534 }
4535 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4536 lrm.RegisterAddress = _3DPRIM_INSTANCE_COUNT;
4537 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 4);
4538 }
4539 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4540 lrm.RegisterAddress = _3DPRIM_START_VERTEX;
4541 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 8);
4542 }
4543 if (draw->index_size) {
4544 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4545 lrm.RegisterAddress = _3DPRIM_BASE_VERTEX;
4546 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 12);
4547 }
4548 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4549 lrm.RegisterAddress = _3DPRIM_START_INSTANCE;
4550 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 16);
4551 }
4552 } else {
4553 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4554 lrm.RegisterAddress = _3DPRIM_START_INSTANCE;
4555 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 12);
4556 }
4557 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
4558 lri.RegisterOffset = _3DPRIM_BASE_VERTEX;
4559 lri.DataDWord = 0;
4560 }
4561 }
4562 }
4563
4564 iris_emit_cmd(batch, GENX(3DPRIMITIVE), prim) {
4565 prim.StartInstanceLocation = draw->start_instance;
4566 prim.InstanceCount = draw->instance_count;
4567 prim.VertexCountPerInstance = draw->count;
4568 prim.VertexAccessType = draw->index_size > 0 ? RANDOM : SEQUENTIAL;
4569 prim.PredicateEnable =
4570 ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT;
4571
4572 // XXX: this is probably bonkers.
4573 prim.StartVertexLocation = draw->start;
4574
4575 prim.IndirectParameterEnable = draw->indirect != NULL;
4576
4577 if (draw->index_size) {
4578 prim.BaseVertexLocation += draw->index_bias;
4579 } else {
4580 prim.StartVertexLocation += draw->index_bias;
4581 }
4582
4583 //prim.BaseVertexLocation = ...;
4584 }
4585 }
4586
4587 static void
4588 iris_upload_compute_state(struct iris_context *ice,
4589 struct iris_batch *batch,
4590 const struct pipe_grid_info *grid)
4591 {
4592 const uint64_t dirty = ice->state.dirty;
4593 struct iris_screen *screen = batch->screen;
4594 const struct gen_device_info *devinfo = &screen->devinfo;
4595 struct iris_binder *binder = &ice->state.binder;
4596 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_COMPUTE];
4597 struct iris_compiled_shader *shader =
4598 ice->shaders.prog[MESA_SHADER_COMPUTE];
4599 struct brw_stage_prog_data *prog_data = shader->prog_data;
4600 struct brw_cs_prog_data *cs_prog_data = (void *) prog_data;
4601
4602 if ((dirty & IRIS_DIRTY_CONSTANTS_CS) && shs->cbuf0_needs_upload)
4603 upload_uniforms(ice, MESA_SHADER_COMPUTE);
4604
4605 if (dirty & IRIS_DIRTY_BINDINGS_CS)
4606 iris_populate_binding_table(ice, batch, MESA_SHADER_COMPUTE, false);
4607
4608 iris_use_optional_res(batch, shs->sampler_table.res, false);
4609 iris_use_pinned_bo(batch, iris_resource_bo(shader->assembly.res), false);
4610
4611 if (ice->state.need_border_colors)
4612 iris_use_pinned_bo(batch, ice->state.border_color_pool.bo, false);
4613
4614 if (dirty & IRIS_DIRTY_CS) {
4615 /* The MEDIA_VFE_STATE documentation for Gen8+ says:
4616 *
4617 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
4618 * the only bits that are changed are scoreboard related: Scoreboard
4619 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard Delta. For
4620 * these scoreboard related states, a MEDIA_STATE_FLUSH is
4621 * sufficient."
4622 */
4623 iris_emit_pipe_control_flush(batch, PIPE_CONTROL_CS_STALL);
4624
4625 iris_emit_cmd(batch, GENX(MEDIA_VFE_STATE), vfe) {
4626 if (prog_data->total_scratch) {
4627 uint32_t scratch_addr =
4628 iris_get_scratch_space(ice, prog_data->total_scratch,
4629 MESA_SHADER_COMPUTE);
4630 vfe.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11;
4631 vfe.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr);
4632 }
4633
4634 vfe.MaximumNumberofThreads =
4635 devinfo->max_cs_threads * screen->subslice_total - 1;
4636 #if GEN_GEN < 11
4637 vfe.ResetGatewayTimer =
4638 Resettingrelativetimerandlatchingtheglobaltimestamp;
4639 #endif
4640
4641 vfe.NumberofURBEntries = 2;
4642 vfe.URBEntryAllocationSize = 2;
4643
4644 // XXX: Use Indirect Payload Storage?
4645 vfe.CURBEAllocationSize =
4646 ALIGN(cs_prog_data->push.per_thread.regs * cs_prog_data->threads +
4647 cs_prog_data->push.cross_thread.regs, 2);
4648 }
4649 }
4650
4651 // XXX: hack iris_set_constant_buffers to upload these thread counts
4652 // XXX: along with regular uniforms for compute shaders, somehow.
4653
4654 uint32_t curbe_data_offset = 0;
4655 // TODO: Move subgroup-id into uniforms ubo so we can push uniforms
4656 assert(cs_prog_data->push.cross_thread.dwords == 0 &&
4657 cs_prog_data->push.per_thread.dwords == 1 &&
4658 cs_prog_data->base.param[0] == BRW_PARAM_BUILTIN_SUBGROUP_ID);
4659 struct pipe_resource *curbe_data_res = NULL;
4660 uint32_t *curbe_data_map =
4661 stream_state(batch, ice->state.dynamic_uploader, &curbe_data_res,
4662 ALIGN(cs_prog_data->push.total.size, 64), 64,
4663 &curbe_data_offset);
4664 assert(curbe_data_map);
4665 memset(curbe_data_map, 0x5a, ALIGN(cs_prog_data->push.total.size, 64));
4666 iris_fill_cs_push_const_buffer(cs_prog_data, curbe_data_map);
4667
4668 if (dirty & IRIS_DIRTY_CONSTANTS_CS) {
4669 iris_emit_cmd(batch, GENX(MEDIA_CURBE_LOAD), curbe) {
4670 curbe.CURBETotalDataLength =
4671 ALIGN(cs_prog_data->push.total.size, 64);
4672 curbe.CURBEDataStartAddress = curbe_data_offset;
4673 }
4674 }
4675
4676 if (dirty & (IRIS_DIRTY_SAMPLER_STATES_CS |
4677 IRIS_DIRTY_BINDINGS_CS |
4678 IRIS_DIRTY_CONSTANTS_CS |
4679 IRIS_DIRTY_CS)) {
4680 struct pipe_resource *desc_res = NULL;
4681 uint32_t desc[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
4682
4683 iris_pack_state(GENX(INTERFACE_DESCRIPTOR_DATA), desc, idd) {
4684 idd.SamplerStatePointer = shs->sampler_table.offset;
4685 idd.BindingTablePointer = binder->bt_offset[MESA_SHADER_COMPUTE];
4686 }
4687
4688 for (int i = 0; i < GENX(INTERFACE_DESCRIPTOR_DATA_length); i++)
4689 desc[i] |= ((uint32_t *) shader->derived_data)[i];
4690
4691 iris_emit_cmd(batch, GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), load) {
4692 load.InterfaceDescriptorTotalLength =
4693 GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
4694 load.InterfaceDescriptorDataStartAddress =
4695 emit_state(batch, ice->state.dynamic_uploader,
4696 &desc_res, desc, sizeof(desc), 32);
4697 }
4698
4699 pipe_resource_reference(&desc_res, NULL);
4700 }
4701
4702 uint32_t group_size = grid->block[0] * grid->block[1] * grid->block[2];
4703 uint32_t remainder = group_size & (cs_prog_data->simd_size - 1);
4704 uint32_t right_mask;
4705
4706 if (remainder > 0)
4707 right_mask = ~0u >> (32 - remainder);
4708 else
4709 right_mask = ~0u >> (32 - cs_prog_data->simd_size);
4710
4711 #define GPGPU_DISPATCHDIMX 0x2500
4712 #define GPGPU_DISPATCHDIMY 0x2504
4713 #define GPGPU_DISPATCHDIMZ 0x2508
4714
4715 if (grid->indirect) {
4716 struct iris_state_ref *grid_size = &ice->state.grid_size;
4717 struct iris_bo *bo = iris_resource_bo(grid_size->res);
4718 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4719 lrm.RegisterAddress = GPGPU_DISPATCHDIMX;
4720 lrm.MemoryAddress = ro_bo(bo, grid_size->offset + 0);
4721 }
4722 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4723 lrm.RegisterAddress = GPGPU_DISPATCHDIMY;
4724 lrm.MemoryAddress = ro_bo(bo, grid_size->offset + 4);
4725 }
4726 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4727 lrm.RegisterAddress = GPGPU_DISPATCHDIMZ;
4728 lrm.MemoryAddress = ro_bo(bo, grid_size->offset + 8);
4729 }
4730 }
4731
4732 iris_emit_cmd(batch, GENX(GPGPU_WALKER), ggw) {
4733 ggw.IndirectParameterEnable = grid->indirect != NULL;
4734 ggw.SIMDSize = cs_prog_data->simd_size / 16;
4735 ggw.ThreadDepthCounterMaximum = 0;
4736 ggw.ThreadHeightCounterMaximum = 0;
4737 ggw.ThreadWidthCounterMaximum = cs_prog_data->threads - 1;
4738 ggw.ThreadGroupIDXDimension = grid->grid[0];
4739 ggw.ThreadGroupIDYDimension = grid->grid[1];
4740 ggw.ThreadGroupIDZDimension = grid->grid[2];
4741 ggw.RightExecutionMask = right_mask;
4742 ggw.BottomExecutionMask = 0xffffffff;
4743 }
4744
4745 iris_emit_cmd(batch, GENX(MEDIA_STATE_FLUSH), msf);
4746
4747 if (!batch->contains_draw) {
4748 iris_restore_compute_saved_bos(ice, batch, grid);
4749 batch->contains_draw = true;
4750 }
4751 }
4752
4753 /**
4754 * State module teardown.
4755 */
4756 static void
4757 iris_destroy_state(struct iris_context *ice)
4758 {
4759 iris_free_vertex_buffers(&ice->state.genx->vertex_buffers);
4760
4761 // XXX: unreference resources/surfaces.
4762 for (unsigned i = 0; i < ice->state.framebuffer.nr_cbufs; i++) {
4763 pipe_surface_reference(&ice->state.framebuffer.cbufs[i], NULL);
4764 }
4765 pipe_surface_reference(&ice->state.framebuffer.zsbuf, NULL);
4766
4767 for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
4768 struct iris_shader_state *shs = &ice->state.shaders[stage];
4769 pipe_resource_reference(&shs->sampler_table.res, NULL);
4770 }
4771 free(ice->state.genx);
4772
4773 pipe_resource_reference(&ice->state.unbound_tex.res, NULL);
4774
4775 pipe_resource_reference(&ice->state.last_res.cc_vp, NULL);
4776 pipe_resource_reference(&ice->state.last_res.sf_cl_vp, NULL);
4777 pipe_resource_reference(&ice->state.last_res.color_calc, NULL);
4778 pipe_resource_reference(&ice->state.last_res.scissor, NULL);
4779 pipe_resource_reference(&ice->state.last_res.blend, NULL);
4780 pipe_resource_reference(&ice->state.last_res.index_buffer, NULL);
4781 }
4782
4783 /* ------------------------------------------------------------------- */
4784
4785 static void
4786 iris_load_register_reg32(struct iris_batch *batch, uint32_t src,
4787 uint32_t dst)
4788 {
4789 _iris_emit_lrr(batch, src, dst);
4790 }
4791
4792 static void
4793 iris_load_register_reg64(struct iris_batch *batch, uint32_t src,
4794 uint32_t dst)
4795 {
4796 _iris_emit_lrr(batch, src, dst);
4797 _iris_emit_lrr(batch, src + 4, dst + 4);
4798 }
4799
4800 static void
4801 iris_load_register_imm32(struct iris_batch *batch, uint32_t reg,
4802 uint32_t val)
4803 {
4804 _iris_emit_lri(batch, reg, val);
4805 }
4806
4807 static void
4808 iris_load_register_imm64(struct iris_batch *batch, uint32_t reg,
4809 uint64_t val)
4810 {
4811 _iris_emit_lri(batch, reg + 0, val & 0xffffffff);
4812 _iris_emit_lri(batch, reg + 4, val >> 32);
4813 }
4814
4815 /**
4816 * Emit MI_LOAD_REGISTER_MEM to load a 32-bit MMIO register from a buffer.
4817 */
4818 static void
4819 iris_load_register_mem32(struct iris_batch *batch, uint32_t reg,
4820 struct iris_bo *bo, uint32_t offset)
4821 {
4822 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4823 lrm.RegisterAddress = reg;
4824 lrm.MemoryAddress = ro_bo(bo, offset);
4825 }
4826 }
4827
4828 /**
4829 * Load a 64-bit value from a buffer into a MMIO register via
4830 * two MI_LOAD_REGISTER_MEM commands.
4831 */
4832 static void
4833 iris_load_register_mem64(struct iris_batch *batch, uint32_t reg,
4834 struct iris_bo *bo, uint32_t offset)
4835 {
4836 iris_load_register_mem32(batch, reg + 0, bo, offset + 0);
4837 iris_load_register_mem32(batch, reg + 4, bo, offset + 4);
4838 }
4839
4840 static void
4841 iris_store_register_mem32(struct iris_batch *batch, uint32_t reg,
4842 struct iris_bo *bo, uint32_t offset,
4843 bool predicated)
4844 {
4845 iris_emit_cmd(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
4846 srm.RegisterAddress = reg;
4847 srm.MemoryAddress = rw_bo(bo, offset);
4848 srm.PredicateEnable = predicated;
4849 }
4850 }
4851
4852 static void
4853 iris_store_register_mem64(struct iris_batch *batch, uint32_t reg,
4854 struct iris_bo *bo, uint32_t offset,
4855 bool predicated)
4856 {
4857 iris_store_register_mem32(batch, reg + 0, bo, offset + 0, predicated);
4858 iris_store_register_mem32(batch, reg + 4, bo, offset + 4, predicated);
4859 }
4860
4861 static void
4862 iris_store_data_imm32(struct iris_batch *batch,
4863 struct iris_bo *bo, uint32_t offset,
4864 uint32_t imm)
4865 {
4866 iris_emit_cmd(batch, GENX(MI_STORE_DATA_IMM), sdi) {
4867 sdi.Address = rw_bo(bo, offset);
4868 sdi.ImmediateData = imm;
4869 }
4870 }
4871
4872 static void
4873 iris_store_data_imm64(struct iris_batch *batch,
4874 struct iris_bo *bo, uint32_t offset,
4875 uint64_t imm)
4876 {
4877 /* Can't use iris_emit_cmd because MI_STORE_DATA_IMM has a length of
4878 * 2 in genxml but it's actually variable length and we need 5 DWords.
4879 */
4880 void *map = iris_get_command_space(batch, 4 * 5);
4881 _iris_pack_command(batch, GENX(MI_STORE_DATA_IMM), map, sdi) {
4882 sdi.DWordLength = 5 - 2;
4883 sdi.Address = rw_bo(bo, offset);
4884 sdi.ImmediateData = imm;
4885 }
4886 }
4887
4888 static void
4889 iris_copy_mem_mem(struct iris_batch *batch,
4890 struct iris_bo *dst_bo, uint32_t dst_offset,
4891 struct iris_bo *src_bo, uint32_t src_offset,
4892 unsigned bytes)
4893 {
4894 /* MI_COPY_MEM_MEM operates on DWords. */
4895 assert(bytes % 4 == 0);
4896 assert(dst_offset % 4 == 0);
4897 assert(src_offset % 4 == 0);
4898
4899 for (unsigned i = 0; i < bytes; i += 4) {
4900 iris_emit_cmd(batch, GENX(MI_COPY_MEM_MEM), cp) {
4901 cp.DestinationMemoryAddress = rw_bo(dst_bo, dst_offset + i);
4902 cp.SourceMemoryAddress = ro_bo(src_bo, src_offset + i);
4903 }
4904 }
4905 }
4906
4907 /* ------------------------------------------------------------------- */
4908
4909 static unsigned
4910 flags_to_post_sync_op(uint32_t flags)
4911 {
4912 if (flags & PIPE_CONTROL_WRITE_IMMEDIATE)
4913 return WriteImmediateData;
4914
4915 if (flags & PIPE_CONTROL_WRITE_DEPTH_COUNT)
4916 return WritePSDepthCount;
4917
4918 if (flags & PIPE_CONTROL_WRITE_TIMESTAMP)
4919 return WriteTimestamp;
4920
4921 return 0;
4922 }
4923
4924 /**
4925 * Do the given flags have a Post Sync or LRI Post Sync operation?
4926 */
4927 static enum pipe_control_flags
4928 get_post_sync_flags(enum pipe_control_flags flags)
4929 {
4930 flags &= PIPE_CONTROL_WRITE_IMMEDIATE |
4931 PIPE_CONTROL_WRITE_DEPTH_COUNT |
4932 PIPE_CONTROL_WRITE_TIMESTAMP |
4933 PIPE_CONTROL_LRI_POST_SYNC_OP;
4934
4935 /* Only one "Post Sync Op" is allowed, and it's mutually exclusive with
4936 * "LRI Post Sync Operation". So more than one bit set would be illegal.
4937 */
4938 assert(util_bitcount(flags) <= 1);
4939
4940 return flags;
4941 }
4942
4943 #define IS_COMPUTE_PIPELINE(batch) (batch->name == IRIS_BATCH_COMPUTE)
4944
4945 /**
4946 * Emit a series of PIPE_CONTROL commands, taking into account any
4947 * workarounds necessary to actually accomplish the caller's request.
4948 *
4949 * Unless otherwise noted, spec quotations in this function come from:
4950 *
4951 * Synchronization of the 3D Pipeline > PIPE_CONTROL Command > Programming
4952 * Restrictions for PIPE_CONTROL.
4953 *
4954 * You should not use this function directly. Use the helpers in
4955 * iris_pipe_control.c instead, which may split the pipe control further.
4956 */
4957 static void
4958 iris_emit_raw_pipe_control(struct iris_batch *batch, uint32_t flags,
4959 struct iris_bo *bo, uint32_t offset, uint64_t imm)
4960 {
4961 UNUSED const struct gen_device_info *devinfo = &batch->screen->devinfo;
4962 enum pipe_control_flags post_sync_flags = get_post_sync_flags(flags);
4963 enum pipe_control_flags non_lri_post_sync_flags =
4964 post_sync_flags & ~PIPE_CONTROL_LRI_POST_SYNC_OP;
4965
4966 /* Recursive PIPE_CONTROL workarounds --------------------------------
4967 * (http://knowyourmeme.com/memes/xzibit-yo-dawg)
4968 *
4969 * We do these first because we want to look at the original operation,
4970 * rather than any workarounds we set.
4971 */
4972 if (GEN_GEN == 9 && (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE)) {
4973 /* The PIPE_CONTROL "VF Cache Invalidation Enable" bit description
4974 * lists several workarounds:
4975 *
4976 * "Project: SKL, KBL, BXT
4977 *
4978 * If the VF Cache Invalidation Enable is set to a 1 in a
4979 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields
4980 * sets to 0, with the VF Cache Invalidation Enable set to 0
4981 * needs to be sent prior to the PIPE_CONTROL with VF Cache
4982 * Invalidation Enable set to a 1."
4983 */
4984 iris_emit_raw_pipe_control(batch, 0, NULL, 0, 0);
4985 }
4986
4987 if (GEN_GEN == 9 && IS_COMPUTE_PIPELINE(batch) && post_sync_flags) {
4988 /* Project: SKL / Argument: LRI Post Sync Operation [23]
4989 *
4990 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
4991 * programmed prior to programming a PIPECONTROL command with "LRI
4992 * Post Sync Operation" in GPGPU mode of operation (i.e when
4993 * PIPELINE_SELECT command is set to GPGPU mode of operation)."
4994 *
4995 * The same text exists a few rows below for Post Sync Op.
4996 */
4997 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_CS_STALL, bo, offset, imm);
4998 }
4999
5000 if (GEN_GEN == 10 && (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH)) {
5001 /* Cannonlake:
5002 * "Before sending a PIPE_CONTROL command with bit 12 set, SW must issue
5003 * another PIPE_CONTROL with Render Target Cache Flush Enable (bit 12)
5004 * = 0 and Pipe Control Flush Enable (bit 7) = 1"
5005 */
5006 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_FLUSH_ENABLE, bo,
5007 offset, imm);
5008 }
5009
5010 /* "Flush Types" workarounds ---------------------------------------------
5011 * We do these now because they may add post-sync operations or CS stalls.
5012 */
5013
5014 if (GEN_GEN < 11 && flags & PIPE_CONTROL_VF_CACHE_INVALIDATE) {
5015 /* Project: BDW, SKL+ (stopping at CNL) / Argument: VF Invalidate
5016 *
5017 * "'Post Sync Operation' must be enabled to 'Write Immediate Data' or
5018 * 'Write PS Depth Count' or 'Write Timestamp'."
5019 */
5020 if (!bo) {
5021 flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
5022 post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
5023 non_lri_post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
5024 bo = batch->screen->workaround_bo;
5025 }
5026 }
5027
5028 /* #1130 from Gen10 workarounds page:
5029 *
5030 * "Enable Depth Stall on every Post Sync Op if Render target Cache
5031 * Flush is not enabled in same PIPE CONTROL and Enable Pixel score
5032 * board stall if Render target cache flush is enabled."
5033 *
5034 * Applicable to CNL B0 and C0 steppings only.
5035 *
5036 * The wording here is unclear, and this workaround doesn't look anything
5037 * like the internal bug report recommendations, but leave it be for now...
5038 */
5039 if (GEN_GEN == 10) {
5040 if (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH) {
5041 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
5042 } else if (flags & non_lri_post_sync_flags) {
5043 flags |= PIPE_CONTROL_DEPTH_STALL;
5044 }
5045 }
5046
5047 if (flags & PIPE_CONTROL_DEPTH_STALL) {
5048 /* From the PIPE_CONTROL instruction table, bit 13 (Depth Stall Enable):
5049 *
5050 * "This bit must be DISABLED for operations other than writing
5051 * PS_DEPTH_COUNT."
5052 *
5053 * This seems like nonsense. An Ivybridge workaround requires us to
5054 * emit a PIPE_CONTROL with a depth stall and write immediate post-sync
5055 * operation. Gen8+ requires us to emit depth stalls and depth cache
5056 * flushes together. So, it's hard to imagine this means anything other
5057 * than "we originally intended this to be used for PS_DEPTH_COUNT".
5058 *
5059 * We ignore the supposed restriction and do nothing.
5060 */
5061 }
5062
5063 if (flags & (PIPE_CONTROL_RENDER_TARGET_FLUSH |
5064 PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
5065 /* From the PIPE_CONTROL instruction table, bit 12 and bit 1:
5066 *
5067 * "This bit must be DISABLED for End-of-pipe (Read) fences,
5068 * PS_DEPTH_COUNT or TIMESTAMP queries."
5069 *
5070 * TODO: Implement end-of-pipe checking.
5071 */
5072 assert(!(post_sync_flags & (PIPE_CONTROL_WRITE_DEPTH_COUNT |
5073 PIPE_CONTROL_WRITE_TIMESTAMP)));
5074 }
5075
5076 if (GEN_GEN < 11 && (flags & PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
5077 /* From the PIPE_CONTROL instruction table, bit 1:
5078 *
5079 * "This bit is ignored if Depth Stall Enable is set.
5080 * Further, the render cache is not flushed even if Write Cache
5081 * Flush Enable bit is set."
5082 *
5083 * We assert that the caller doesn't do this combination, to try and
5084 * prevent mistakes. It shouldn't hurt the GPU, though.
5085 *
5086 * We skip this check on Gen11+ as the "Stall at Pixel Scoreboard"
5087 * and "Render Target Flush" combo is explicitly required for BTI
5088 * update workarounds.
5089 */
5090 assert(!(flags & (PIPE_CONTROL_DEPTH_STALL |
5091 PIPE_CONTROL_RENDER_TARGET_FLUSH)));
5092 }
5093
5094 /* PIPE_CONTROL page workarounds ------------------------------------- */
5095
5096 if (GEN_GEN <= 8 && (flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE)) {
5097 /* From the PIPE_CONTROL page itself:
5098 *
5099 * "IVB, HSW, BDW
5100 * Restriction: Pipe_control with CS-stall bit set must be issued
5101 * before a pipe-control command that has the State Cache
5102 * Invalidate bit set."
5103 */
5104 flags |= PIPE_CONTROL_CS_STALL;
5105 }
5106
5107 if (flags & PIPE_CONTROL_FLUSH_LLC) {
5108 /* From the PIPE_CONTROL instruction table, bit 26 (Flush LLC):
5109 *
5110 * "Project: ALL
5111 * SW must always program Post-Sync Operation to "Write Immediate
5112 * Data" when Flush LLC is set."
5113 *
5114 * For now, we just require the caller to do it.
5115 */
5116 assert(flags & PIPE_CONTROL_WRITE_IMMEDIATE);
5117 }
5118
5119 /* "Post-Sync Operation" workarounds -------------------------------- */
5120
5121 /* Project: All / Argument: Global Snapshot Count Reset [19]
5122 *
5123 * "This bit must not be exercised on any product.
5124 * Requires stall bit ([20] of DW1) set."
5125 *
5126 * We don't use this, so we just assert that it isn't used. The
5127 * PIPE_CONTROL instruction page indicates that they intended this
5128 * as a debug feature and don't think it is useful in production,
5129 * but it may actually be usable, should we ever want to.
5130 */
5131 assert((flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET) == 0);
5132
5133 if (flags & (PIPE_CONTROL_MEDIA_STATE_CLEAR |
5134 PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE)) {
5135 /* Project: All / Arguments:
5136 *
5137 * - Generic Media State Clear [16]
5138 * - Indirect State Pointers Disable [16]
5139 *
5140 * "Requires stall bit ([20] of DW1) set."
5141 *
5142 * Also, the PIPE_CONTROL instruction table, bit 16 (Generic Media
5143 * State Clear) says:
5144 *
5145 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
5146 * programmed prior to programming a PIPECONTROL command with "Media
5147 * State Clear" set in GPGPU mode of operation"
5148 *
5149 * This is a subset of the earlier rule, so there's nothing to do.
5150 */
5151 flags |= PIPE_CONTROL_CS_STALL;
5152 }
5153
5154 if (flags & PIPE_CONTROL_STORE_DATA_INDEX) {
5155 /* Project: All / Argument: Store Data Index
5156 *
5157 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
5158 * than '0'."
5159 *
5160 * For now, we just assert that the caller does this. We might want to
5161 * automatically add a write to the workaround BO...
5162 */
5163 assert(non_lri_post_sync_flags != 0);
5164 }
5165
5166 if (flags & PIPE_CONTROL_SYNC_GFDT) {
5167 /* Project: All / Argument: Sync GFDT
5168 *
5169 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
5170 * than '0' or 0x2520[13] must be set."
5171 *
5172 * For now, we just assert that the caller does this.
5173 */
5174 assert(non_lri_post_sync_flags != 0);
5175 }
5176
5177 if (flags & PIPE_CONTROL_TLB_INVALIDATE) {
5178 /* Project: IVB+ / Argument: TLB inv
5179 *
5180 * "Requires stall bit ([20] of DW1) set."
5181 *
5182 * Also, from the PIPE_CONTROL instruction table:
5183 *
5184 * "Project: SKL+
5185 * Post Sync Operation or CS stall must be set to ensure a TLB
5186 * invalidation occurs. Otherwise no cycle will occur to the TLB
5187 * cache to invalidate."
5188 *
5189 * This is not a subset of the earlier rule, so there's nothing to do.
5190 */
5191 flags |= PIPE_CONTROL_CS_STALL;
5192 }
5193
5194 if (GEN_GEN == 9 && devinfo->gt == 4) {
5195 /* TODO: The big Skylake GT4 post sync op workaround */
5196 }
5197
5198 /* "GPGPU specific workarounds" (both post-sync and flush) ------------ */
5199
5200 if (IS_COMPUTE_PIPELINE(batch)) {
5201 if (GEN_GEN >= 9 && (flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE)) {
5202 /* Project: SKL+ / Argument: Tex Invalidate
5203 * "Requires stall bit ([20] of DW) set for all GPGPU Workloads."
5204 */
5205 flags |= PIPE_CONTROL_CS_STALL;
5206 }
5207
5208 if (GEN_GEN == 8 && (post_sync_flags ||
5209 (flags & (PIPE_CONTROL_NOTIFY_ENABLE |
5210 PIPE_CONTROL_DEPTH_STALL |
5211 PIPE_CONTROL_RENDER_TARGET_FLUSH |
5212 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
5213 PIPE_CONTROL_DATA_CACHE_FLUSH)))) {
5214 /* Project: BDW / Arguments:
5215 *
5216 * - LRI Post Sync Operation [23]
5217 * - Post Sync Op [15:14]
5218 * - Notify En [8]
5219 * - Depth Stall [13]
5220 * - Render Target Cache Flush [12]
5221 * - Depth Cache Flush [0]
5222 * - DC Flush Enable [5]
5223 *
5224 * "Requires stall bit ([20] of DW) set for all GPGPU and Media
5225 * Workloads."
5226 */
5227 flags |= PIPE_CONTROL_CS_STALL;
5228
5229 /* Also, from the PIPE_CONTROL instruction table, bit 20:
5230 *
5231 * "Project: BDW
5232 * This bit must be always set when PIPE_CONTROL command is
5233 * programmed by GPGPU and MEDIA workloads, except for the cases
5234 * when only Read Only Cache Invalidation bits are set (State
5235 * Cache Invalidation Enable, Instruction cache Invalidation
5236 * Enable, Texture Cache Invalidation Enable, Constant Cache
5237 * Invalidation Enable). This is to WA FFDOP CG issue, this WA
5238 * need not implemented when FF_DOP_CG is disable via "Fixed
5239 * Function DOP Clock Gate Disable" bit in RC_PSMI_CTRL register."
5240 *
5241 * It sounds like we could avoid CS stalls in some cases, but we
5242 * don't currently bother. This list isn't exactly the list above,
5243 * either...
5244 */
5245 }
5246 }
5247
5248 /* "Stall" workarounds ----------------------------------------------
5249 * These have to come after the earlier ones because we may have added
5250 * some additional CS stalls above.
5251 */
5252
5253 if (GEN_GEN < 9 && (flags & PIPE_CONTROL_CS_STALL)) {
5254 /* Project: PRE-SKL, VLV, CHV
5255 *
5256 * "[All Stepping][All SKUs]:
5257 *
5258 * One of the following must also be set:
5259 *
5260 * - Render Target Cache Flush Enable ([12] of DW1)
5261 * - Depth Cache Flush Enable ([0] of DW1)
5262 * - Stall at Pixel Scoreboard ([1] of DW1)
5263 * - Depth Stall ([13] of DW1)
5264 * - Post-Sync Operation ([13] of DW1)
5265 * - DC Flush Enable ([5] of DW1)"
5266 *
5267 * If we don't already have one of those bits set, we choose to add
5268 * "Stall at Pixel Scoreboard". Some of the other bits require a
5269 * CS stall as a workaround (see above), which would send us into
5270 * an infinite recursion of PIPE_CONTROLs. "Stall at Pixel Scoreboard"
5271 * appears to be safe, so we choose that.
5272 */
5273 const uint32_t wa_bits = PIPE_CONTROL_RENDER_TARGET_FLUSH |
5274 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
5275 PIPE_CONTROL_WRITE_IMMEDIATE |
5276 PIPE_CONTROL_WRITE_DEPTH_COUNT |
5277 PIPE_CONTROL_WRITE_TIMESTAMP |
5278 PIPE_CONTROL_STALL_AT_SCOREBOARD |
5279 PIPE_CONTROL_DEPTH_STALL |
5280 PIPE_CONTROL_DATA_CACHE_FLUSH;
5281 if (!(flags & wa_bits))
5282 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
5283 }
5284
5285 /* Emit --------------------------------------------------------------- */
5286
5287 iris_emit_cmd(batch, GENX(PIPE_CONTROL), pc) {
5288 pc.LRIPostSyncOperation = NoLRIOperation;
5289 pc.PipeControlFlushEnable = flags & PIPE_CONTROL_FLUSH_ENABLE;
5290 pc.DCFlushEnable = flags & PIPE_CONTROL_DATA_CACHE_FLUSH;
5291 pc.StoreDataIndex = 0;
5292 pc.CommandStreamerStallEnable = flags & PIPE_CONTROL_CS_STALL;
5293 pc.GlobalSnapshotCountReset =
5294 flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET;
5295 pc.TLBInvalidate = flags & PIPE_CONTROL_TLB_INVALIDATE;
5296 pc.GenericMediaStateClear = flags & PIPE_CONTROL_MEDIA_STATE_CLEAR;
5297 pc.StallAtPixelScoreboard = flags & PIPE_CONTROL_STALL_AT_SCOREBOARD;
5298 pc.RenderTargetCacheFlushEnable =
5299 flags & PIPE_CONTROL_RENDER_TARGET_FLUSH;
5300 pc.DepthCacheFlushEnable = flags & PIPE_CONTROL_DEPTH_CACHE_FLUSH;
5301 pc.StateCacheInvalidationEnable =
5302 flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE;
5303 pc.VFCacheInvalidationEnable = flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
5304 pc.ConstantCacheInvalidationEnable =
5305 flags & PIPE_CONTROL_CONST_CACHE_INVALIDATE;
5306 pc.PostSyncOperation = flags_to_post_sync_op(flags);
5307 pc.DepthStallEnable = flags & PIPE_CONTROL_DEPTH_STALL;
5308 pc.InstructionCacheInvalidateEnable =
5309 flags & PIPE_CONTROL_INSTRUCTION_INVALIDATE;
5310 pc.NotifyEnable = flags & PIPE_CONTROL_NOTIFY_ENABLE;
5311 pc.IndirectStatePointersDisable =
5312 flags & PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE;
5313 pc.TextureCacheInvalidationEnable =
5314 flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
5315 pc.Address = rw_bo(bo, offset);
5316 pc.ImmediateData = imm;
5317 }
5318 }
5319
5320 void
5321 genX(init_state)(struct iris_context *ice)
5322 {
5323 struct pipe_context *ctx = &ice->ctx;
5324 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
5325
5326 ctx->create_blend_state = iris_create_blend_state;
5327 ctx->create_depth_stencil_alpha_state = iris_create_zsa_state;
5328 ctx->create_rasterizer_state = iris_create_rasterizer_state;
5329 ctx->create_sampler_state = iris_create_sampler_state;
5330 ctx->create_sampler_view = iris_create_sampler_view;
5331 ctx->create_surface = iris_create_surface;
5332 ctx->create_vertex_elements_state = iris_create_vertex_elements;
5333 ctx->bind_blend_state = iris_bind_blend_state;
5334 ctx->bind_depth_stencil_alpha_state = iris_bind_zsa_state;
5335 ctx->bind_sampler_states = iris_bind_sampler_states;
5336 ctx->bind_rasterizer_state = iris_bind_rasterizer_state;
5337 ctx->bind_vertex_elements_state = iris_bind_vertex_elements_state;
5338 ctx->delete_blend_state = iris_delete_state;
5339 ctx->delete_depth_stencil_alpha_state = iris_delete_state;
5340 ctx->delete_rasterizer_state = iris_delete_state;
5341 ctx->delete_sampler_state = iris_delete_state;
5342 ctx->delete_vertex_elements_state = iris_delete_state;
5343 ctx->set_blend_color = iris_set_blend_color;
5344 ctx->set_clip_state = iris_set_clip_state;
5345 ctx->set_constant_buffer = iris_set_constant_buffer;
5346 ctx->set_shader_buffers = iris_set_shader_buffers;
5347 ctx->set_shader_images = iris_set_shader_images;
5348 ctx->set_sampler_views = iris_set_sampler_views;
5349 ctx->set_tess_state = iris_set_tess_state;
5350 ctx->set_framebuffer_state = iris_set_framebuffer_state;
5351 ctx->set_polygon_stipple = iris_set_polygon_stipple;
5352 ctx->set_sample_mask = iris_set_sample_mask;
5353 ctx->set_scissor_states = iris_set_scissor_states;
5354 ctx->set_stencil_ref = iris_set_stencil_ref;
5355 ctx->set_vertex_buffers = iris_set_vertex_buffers;
5356 ctx->set_viewport_states = iris_set_viewport_states;
5357 ctx->sampler_view_destroy = iris_sampler_view_destroy;
5358 ctx->surface_destroy = iris_surface_destroy;
5359 ctx->draw_vbo = iris_draw_vbo;
5360 ctx->launch_grid = iris_launch_grid;
5361 ctx->create_stream_output_target = iris_create_stream_output_target;
5362 ctx->stream_output_target_destroy = iris_stream_output_target_destroy;
5363 ctx->set_stream_output_targets = iris_set_stream_output_targets;
5364
5365 ice->vtbl.destroy_state = iris_destroy_state;
5366 ice->vtbl.init_render_context = iris_init_render_context;
5367 ice->vtbl.init_compute_context = iris_init_compute_context;
5368 ice->vtbl.upload_render_state = iris_upload_render_state;
5369 ice->vtbl.update_surface_base_address = iris_update_surface_base_address;
5370 ice->vtbl.upload_compute_state = iris_upload_compute_state;
5371 ice->vtbl.emit_raw_pipe_control = iris_emit_raw_pipe_control;
5372 ice->vtbl.load_register_reg32 = iris_load_register_reg32;
5373 ice->vtbl.load_register_reg64 = iris_load_register_reg64;
5374 ice->vtbl.load_register_imm32 = iris_load_register_imm32;
5375 ice->vtbl.load_register_imm64 = iris_load_register_imm64;
5376 ice->vtbl.load_register_mem32 = iris_load_register_mem32;
5377 ice->vtbl.load_register_mem64 = iris_load_register_mem64;
5378 ice->vtbl.store_register_mem32 = iris_store_register_mem32;
5379 ice->vtbl.store_register_mem64 = iris_store_register_mem64;
5380 ice->vtbl.store_data_imm32 = iris_store_data_imm32;
5381 ice->vtbl.store_data_imm64 = iris_store_data_imm64;
5382 ice->vtbl.copy_mem_mem = iris_copy_mem_mem;
5383 ice->vtbl.derived_program_state_size = iris_derived_program_state_size;
5384 ice->vtbl.store_derived_program_state = iris_store_derived_program_state;
5385 ice->vtbl.create_so_decl_list = iris_create_so_decl_list;
5386 ice->vtbl.populate_vs_key = iris_populate_vs_key;
5387 ice->vtbl.populate_tcs_key = iris_populate_tcs_key;
5388 ice->vtbl.populate_tes_key = iris_populate_tes_key;
5389 ice->vtbl.populate_gs_key = iris_populate_gs_key;
5390 ice->vtbl.populate_fs_key = iris_populate_fs_key;
5391 ice->vtbl.populate_cs_key = iris_populate_cs_key;
5392
5393 ice->state.dirty = ~0ull;
5394
5395 ice->state.statistics_counters_enabled = true;
5396
5397 ice->state.sample_mask = 0xffff;
5398 ice->state.num_viewports = 1;
5399 ice->state.genx = calloc(1, sizeof(struct iris_genx_state));
5400
5401 /* Make a 1x1x1 null surface for unbound textures */
5402 void *null_surf_map =
5403 upload_state(ice->state.surface_uploader, &ice->state.unbound_tex,
5404 4 * GENX(RENDER_SURFACE_STATE_length), 64);
5405 isl_null_fill_state(&screen->isl_dev, null_surf_map, isl_extent3d(1, 1, 1));
5406 ice->state.unbound_tex.offset +=
5407 iris_bo_offset_from_base_address(iris_resource_bo(ice->state.unbound_tex.res));
5408
5409 /* Default all scissor rectangles to be empty regions. */
5410 for (int i = 0; i < IRIS_MAX_VIEWPORTS; i++) {
5411 ice->state.scissors[i] = (struct pipe_scissor_state) {
5412 .minx = 1, .maxx = 0, .miny = 1, .maxy = 0,
5413 };
5414 }
5415 }