iris: Don't bother considering if the underlying surface is a cube
[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 force_persample_interp - ?
1076 bottom_edge_rule
1077
1078 offset_units_unscaled - cap not exposed
1079 }
1080 #endif
1081
1082 // XXX: it may make more sense just to store the pipe_rasterizer_state,
1083 // we're copying a lot of booleans here. But we don't need all of them...
1084
1085 cso->multisample = state->multisample;
1086 cso->force_persample_interp = state->force_persample_interp;
1087 cso->clip_halfz = state->clip_halfz;
1088 cso->depth_clip_near = state->depth_clip_near;
1089 cso->depth_clip_far = state->depth_clip_far;
1090 cso->flatshade = state->flatshade;
1091 cso->flatshade_first = state->flatshade_first;
1092 cso->clamp_fragment_color = state->clamp_fragment_color;
1093 cso->light_twoside = state->light_twoside;
1094 cso->rasterizer_discard = state->rasterizer_discard;
1095 cso->half_pixel_center = state->half_pixel_center;
1096 cso->sprite_coord_mode = state->sprite_coord_mode;
1097 cso->sprite_coord_enable = state->sprite_coord_enable;
1098 cso->line_stipple_enable = state->line_stipple_enable;
1099 cso->poly_stipple_enable = state->poly_stipple_enable;
1100
1101 if (state->clip_plane_enable != 0)
1102 cso->num_clip_plane_consts = util_logbase2(state->clip_plane_enable) + 1;
1103 else
1104 cso->num_clip_plane_consts = 0;
1105
1106 float line_width = get_line_width(state);
1107
1108 iris_pack_command(GENX(3DSTATE_SF), cso->sf, sf) {
1109 sf.StatisticsEnable = true;
1110 sf.ViewportTransformEnable = true;
1111 sf.AALineDistanceMode = AALINEDISTANCE_TRUE;
1112 sf.LineEndCapAntialiasingRegionWidth =
1113 state->line_smooth ? _10pixels : _05pixels;
1114 sf.LastPixelEnable = state->line_last_pixel;
1115 sf.LineWidth = line_width;
1116 sf.SmoothPointEnable = state->point_smooth || state->multisample;
1117 sf.PointWidthSource = state->point_size_per_vertex ? Vertex : State;
1118 sf.PointWidth = state->point_size;
1119
1120 if (state->flatshade_first) {
1121 sf.TriangleFanProvokingVertexSelect = 1;
1122 } else {
1123 sf.TriangleStripListProvokingVertexSelect = 2;
1124 sf.TriangleFanProvokingVertexSelect = 2;
1125 sf.LineStripListProvokingVertexSelect = 1;
1126 }
1127 }
1128
1129 iris_pack_command(GENX(3DSTATE_RASTER), cso->raster, rr) {
1130 rr.FrontWinding = state->front_ccw ? CounterClockwise : Clockwise;
1131 rr.CullMode = translate_cull_mode(state->cull_face);
1132 rr.FrontFaceFillMode = translate_fill_mode(state->fill_front);
1133 rr.BackFaceFillMode = translate_fill_mode(state->fill_back);
1134 rr.DXMultisampleRasterizationEnable = state->multisample;
1135 rr.GlobalDepthOffsetEnableSolid = state->offset_tri;
1136 rr.GlobalDepthOffsetEnableWireframe = state->offset_line;
1137 rr.GlobalDepthOffsetEnablePoint = state->offset_point;
1138 rr.GlobalDepthOffsetConstant = state->offset_units * 2;
1139 rr.GlobalDepthOffsetScale = state->offset_scale;
1140 rr.GlobalDepthOffsetClamp = state->offset_clamp;
1141 rr.SmoothPointEnable = state->point_smooth || state->multisample;
1142 rr.AntialiasingEnable = state->line_smooth;
1143 rr.ScissorRectangleEnable = state->scissor;
1144 rr.ViewportZNearClipTestEnable = state->depth_clip_near;
1145 rr.ViewportZFarClipTestEnable = state->depth_clip_far;
1146 //rr.ConservativeRasterizationEnable = not yet supported by Gallium...
1147 }
1148
1149 iris_pack_command(GENX(3DSTATE_CLIP), cso->clip, cl) {
1150 /* cl.NonPerspectiveBarycentricEnable is filled in at draw time from
1151 * the FS program; cl.ForceZeroRTAIndexEnable is filled in from the FB.
1152 */
1153 cl.StatisticsEnable = true;
1154 cl.EarlyCullEnable = true;
1155 cl.UserClipDistanceClipTestEnableBitmask = state->clip_plane_enable;
1156 cl.ForceUserClipDistanceClipTestEnableBitmask = true;
1157 cl.APIMode = state->clip_halfz ? APIMODE_D3D : APIMODE_OGL;
1158 cl.GuardbandClipTestEnable = true;
1159 cl.ClipMode = CLIPMODE_NORMAL;
1160 cl.ClipEnable = true;
1161 cl.ViewportXYClipTestEnable = state->point_tri_clip;
1162 cl.MinimumPointWidth = 0.125;
1163 cl.MaximumPointWidth = 255.875;
1164
1165 if (state->flatshade_first) {
1166 cl.TriangleFanProvokingVertexSelect = 1;
1167 } else {
1168 cl.TriangleStripListProvokingVertexSelect = 2;
1169 cl.TriangleFanProvokingVertexSelect = 2;
1170 cl.LineStripListProvokingVertexSelect = 1;
1171 }
1172 }
1173
1174 iris_pack_command(GENX(3DSTATE_WM), cso->wm, wm) {
1175 /* wm.BarycentricInterpolationMode and wm.EarlyDepthStencilControl are
1176 * filled in at draw time from the FS program.
1177 */
1178 wm.LineAntialiasingRegionWidth = _10pixels;
1179 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
1180 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
1181 wm.LineStippleEnable = state->line_stipple_enable;
1182 wm.PolygonStippleEnable = state->poly_stipple_enable;
1183 }
1184
1185 /* Remap from 0..255 back to 1..256 */
1186 const unsigned line_stipple_factor = state->line_stipple_factor + 1;
1187
1188 iris_pack_command(GENX(3DSTATE_LINE_STIPPLE), cso->line_stipple, line) {
1189 line.LineStipplePattern = state->line_stipple_pattern;
1190 line.LineStippleInverseRepeatCount = 1.0f / line_stipple_factor;
1191 line.LineStippleRepeatCount = line_stipple_factor;
1192 }
1193
1194 return cso;
1195 }
1196
1197 /**
1198 * The pipe->bind_rasterizer_state() driver hook.
1199 *
1200 * Bind a rasterizer CSO and flag related dirty bits.
1201 */
1202 static void
1203 iris_bind_rasterizer_state(struct pipe_context *ctx, void *state)
1204 {
1205 struct iris_context *ice = (struct iris_context *) ctx;
1206 struct iris_rasterizer_state *old_cso = ice->state.cso_rast;
1207 struct iris_rasterizer_state *new_cso = state;
1208
1209 if (new_cso) {
1210 /* Try to avoid re-emitting 3DSTATE_LINE_STIPPLE, it's non-pipelined */
1211 if (cso_changed_memcmp(line_stipple))
1212 ice->state.dirty |= IRIS_DIRTY_LINE_STIPPLE;
1213
1214 if (cso_changed(half_pixel_center))
1215 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
1216
1217 if (cso_changed(line_stipple_enable) || cso_changed(poly_stipple_enable))
1218 ice->state.dirty |= IRIS_DIRTY_WM;
1219
1220 if (cso_changed(rasterizer_discard) || cso_changed(flatshade_first))
1221 ice->state.dirty |= IRIS_DIRTY_STREAMOUT;
1222
1223 if (cso_changed(depth_clip_near) || cso_changed(depth_clip_far) ||
1224 cso_changed(clip_halfz))
1225 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
1226
1227 if (cso_changed(sprite_coord_enable) || cso_changed(light_twoside))
1228 ice->state.dirty |= IRIS_DIRTY_SBE;
1229 }
1230
1231 ice->state.cso_rast = new_cso;
1232 ice->state.dirty |= IRIS_DIRTY_RASTER;
1233 ice->state.dirty |= IRIS_DIRTY_CLIP;
1234 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_RASTERIZER];
1235 }
1236
1237 /**
1238 * Return true if the given wrap mode requires the border color to exist.
1239 *
1240 * (We can skip uploading it if the sampler isn't going to use it.)
1241 */
1242 static bool
1243 wrap_mode_needs_border_color(unsigned wrap_mode)
1244 {
1245 return wrap_mode == TCM_CLAMP_BORDER || wrap_mode == TCM_HALF_BORDER;
1246 }
1247
1248 /**
1249 * Gallium CSO for sampler state.
1250 */
1251 struct iris_sampler_state {
1252 union pipe_color_union border_color;
1253 bool needs_border_color;
1254
1255 uint32_t sampler_state[GENX(SAMPLER_STATE_length)];
1256 };
1257
1258 /**
1259 * The pipe->create_sampler_state() driver hook.
1260 *
1261 * We fill out SAMPLER_STATE (except for the border color pointer), and
1262 * store that on the CPU. It doesn't make sense to upload it to a GPU
1263 * buffer object yet, because 3DSTATE_SAMPLER_STATE_POINTERS requires
1264 * all bound sampler states to be in contiguous memor.
1265 */
1266 static void *
1267 iris_create_sampler_state(struct pipe_context *ctx,
1268 const struct pipe_sampler_state *state)
1269 {
1270 struct iris_sampler_state *cso = CALLOC_STRUCT(iris_sampler_state);
1271
1272 if (!cso)
1273 return NULL;
1274
1275 STATIC_ASSERT(PIPE_TEX_FILTER_NEAREST == MAPFILTER_NEAREST);
1276 STATIC_ASSERT(PIPE_TEX_FILTER_LINEAR == MAPFILTER_LINEAR);
1277
1278 unsigned wrap_s = translate_wrap(state->wrap_s);
1279 unsigned wrap_t = translate_wrap(state->wrap_t);
1280 unsigned wrap_r = translate_wrap(state->wrap_r);
1281
1282 memcpy(&cso->border_color, &state->border_color, sizeof(cso->border_color));
1283
1284 cso->needs_border_color = wrap_mode_needs_border_color(wrap_s) ||
1285 wrap_mode_needs_border_color(wrap_t) ||
1286 wrap_mode_needs_border_color(wrap_r);
1287
1288 float min_lod = state->min_lod;
1289 unsigned mag_img_filter = state->mag_img_filter;
1290
1291 // XXX: explain this code ported from ilo...I don't get it at all...
1292 if (state->min_mip_filter == PIPE_TEX_MIPFILTER_NONE &&
1293 state->min_lod > 0.0f) {
1294 min_lod = 0.0f;
1295 mag_img_filter = state->min_img_filter;
1296 }
1297
1298 iris_pack_state(GENX(SAMPLER_STATE), cso->sampler_state, samp) {
1299 samp.TCXAddressControlMode = wrap_s;
1300 samp.TCYAddressControlMode = wrap_t;
1301 samp.TCZAddressControlMode = wrap_r;
1302 samp.CubeSurfaceControlMode = state->seamless_cube_map;
1303 samp.NonnormalizedCoordinateEnable = !state->normalized_coords;
1304 samp.MinModeFilter = state->min_img_filter;
1305 samp.MagModeFilter = mag_img_filter;
1306 samp.MipModeFilter = translate_mip_filter(state->min_mip_filter);
1307 samp.MaximumAnisotropy = RATIO21;
1308
1309 if (state->max_anisotropy >= 2) {
1310 if (state->min_img_filter == PIPE_TEX_FILTER_LINEAR) {
1311 samp.MinModeFilter = MAPFILTER_ANISOTROPIC;
1312 samp.AnisotropicAlgorithm = EWAApproximation;
1313 }
1314
1315 if (state->mag_img_filter == PIPE_TEX_FILTER_LINEAR)
1316 samp.MagModeFilter = MAPFILTER_ANISOTROPIC;
1317
1318 samp.MaximumAnisotropy =
1319 MIN2((state->max_anisotropy - 2) / 2, RATIO161);
1320 }
1321
1322 /* Set address rounding bits if not using nearest filtering. */
1323 if (state->min_img_filter != PIPE_TEX_FILTER_NEAREST) {
1324 samp.UAddressMinFilterRoundingEnable = true;
1325 samp.VAddressMinFilterRoundingEnable = true;
1326 samp.RAddressMinFilterRoundingEnable = true;
1327 }
1328
1329 if (state->mag_img_filter != PIPE_TEX_FILTER_NEAREST) {
1330 samp.UAddressMagFilterRoundingEnable = true;
1331 samp.VAddressMagFilterRoundingEnable = true;
1332 samp.RAddressMagFilterRoundingEnable = true;
1333 }
1334
1335 if (state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
1336 samp.ShadowFunction = translate_shadow_func(state->compare_func);
1337
1338 const float hw_max_lod = GEN_GEN >= 7 ? 14 : 13;
1339
1340 samp.LODPreClampMode = CLAMP_MODE_OGL;
1341 samp.MinLOD = CLAMP(min_lod, 0, hw_max_lod);
1342 samp.MaxLOD = CLAMP(state->max_lod, 0, hw_max_lod);
1343 samp.TextureLODBias = CLAMP(state->lod_bias, -16, 15);
1344
1345 /* .BorderColorPointer is filled in by iris_bind_sampler_states. */
1346 }
1347
1348 return cso;
1349 }
1350
1351 /**
1352 * The pipe->bind_sampler_states() driver hook.
1353 *
1354 * Now that we know all the sampler states, we upload them all into a
1355 * contiguous area of GPU memory, for 3DSTATE_SAMPLER_STATE_POINTERS_*.
1356 * We also fill out the border color state pointers at this point.
1357 *
1358 * We could defer this work to draw time, but we assume that binding
1359 * will be less frequent than drawing.
1360 */
1361 // XXX: this may be a bad idea, need to make sure that st/mesa calls us
1362 // XXX: with the complete set of shaders. If it makes multiple calls to
1363 // XXX: things one at a time, we could waste a lot of time assembling things.
1364 // XXX: it doesn't even BUY us anything to do it here, because we only flag
1365 // XXX: IRIS_DIRTY_SAMPLER_STATE when this is called...
1366 static void
1367 iris_bind_sampler_states(struct pipe_context *ctx,
1368 enum pipe_shader_type p_stage,
1369 unsigned start, unsigned count,
1370 void **states)
1371 {
1372 struct iris_context *ice = (struct iris_context *) ctx;
1373 gl_shader_stage stage = stage_from_pipe(p_stage);
1374 struct iris_shader_state *shs = &ice->state.shaders[stage];
1375
1376 assert(start + count <= IRIS_MAX_TEXTURE_SAMPLERS);
1377 shs->num_samplers = MAX2(shs->num_samplers, start + count);
1378
1379 for (int i = 0; i < count; i++) {
1380 shs->samplers[start + i] = states[i];
1381 }
1382
1383 // XXX: count may include NULLs
1384
1385 /* Assemble the SAMPLER_STATEs into a contiguous table that lives
1386 * in the dynamic state memory zone, so we can point to it via the
1387 * 3DSTATE_SAMPLER_STATE_POINTERS_* commands.
1388 */
1389 uint32_t *map =
1390 upload_state(ice->state.dynamic_uploader, &shs->sampler_table,
1391 count * 4 * GENX(SAMPLER_STATE_length), 32);
1392 if (unlikely(!map))
1393 return;
1394
1395 struct pipe_resource *res = shs->sampler_table.res;
1396 shs->sampler_table.offset +=
1397 iris_bo_offset_from_base_address(iris_resource_bo(res));
1398
1399 /* Make sure all land in the same BO */
1400 iris_border_color_pool_reserve(ice, IRIS_MAX_TEXTURE_SAMPLERS);
1401
1402 for (int i = 0; i < count; i++) {
1403 struct iris_sampler_state *state = shs->samplers[i];
1404
1405 if (!state) {
1406 memset(map, 0, 4 * GENX(SAMPLER_STATE_length));
1407 } else if (!state->needs_border_color) {
1408 memcpy(map, state->sampler_state, 4 * GENX(SAMPLER_STATE_length));
1409 } else {
1410 ice->state.need_border_colors = true;
1411
1412 /* Stream out the border color and merge the pointer. */
1413 uint32_t offset =
1414 iris_upload_border_color(ice, &state->border_color);
1415
1416 uint32_t dynamic[GENX(SAMPLER_STATE_length)];
1417 iris_pack_state(GENX(SAMPLER_STATE), dynamic, dyns) {
1418 dyns.BorderColorPointer = offset;
1419 }
1420
1421 for (uint32_t j = 0; j < GENX(SAMPLER_STATE_length); j++)
1422 map[j] = state->sampler_state[j] | dynamic[j];
1423 }
1424
1425 map += GENX(SAMPLER_STATE_length);
1426 }
1427
1428 ice->state.dirty |= IRIS_DIRTY_SAMPLER_STATES_VS << stage;
1429 }
1430
1431 static enum isl_channel_select
1432 fmt_swizzle(const struct iris_format_info *fmt, enum pipe_swizzle swz)
1433 {
1434 switch (swz) {
1435 case PIPE_SWIZZLE_X: return fmt->swizzle.r;
1436 case PIPE_SWIZZLE_Y: return fmt->swizzle.g;
1437 case PIPE_SWIZZLE_Z: return fmt->swizzle.b;
1438 case PIPE_SWIZZLE_W: return fmt->swizzle.a;
1439 case PIPE_SWIZZLE_1: return SCS_ONE;
1440 case PIPE_SWIZZLE_0: return SCS_ZERO;
1441 default: unreachable("invalid swizzle");
1442 }
1443 }
1444
1445 static void
1446 fill_buffer_surface_state(struct isl_device *isl_dev,
1447 struct iris_bo *bo,
1448 void *map,
1449 enum isl_format format,
1450 unsigned offset,
1451 unsigned size)
1452 {
1453 const struct isl_format_layout *fmtl = isl_format_get_layout(format);
1454 const unsigned cpp = fmtl->bpb / 8;
1455
1456 /* The ARB_texture_buffer_specification says:
1457 *
1458 * "The number of texels in the buffer texture's texel array is given by
1459 *
1460 * floor(<buffer_size> / (<components> * sizeof(<base_type>)),
1461 *
1462 * where <buffer_size> is the size of the buffer object, in basic
1463 * machine units and <components> and <base_type> are the element count
1464 * and base data type for elements, as specified in Table X.1. The
1465 * number of texels in the texel array is then clamped to the
1466 * implementation-dependent limit MAX_TEXTURE_BUFFER_SIZE_ARB."
1467 *
1468 * We need to clamp the size in bytes to MAX_TEXTURE_BUFFER_SIZE * stride,
1469 * so that when ISL divides by stride to obtain the number of texels, that
1470 * texel count is clamped to MAX_TEXTURE_BUFFER_SIZE.
1471 */
1472 unsigned final_size =
1473 MIN3(size, bo->size - offset, IRIS_MAX_TEXTURE_BUFFER_SIZE * cpp);
1474
1475 isl_buffer_fill_state(isl_dev, map,
1476 .address = bo->gtt_offset + offset,
1477 .size_B = final_size,
1478 .format = format,
1479 .stride_B = cpp,
1480 .mocs = MOCS_WB);
1481 }
1482
1483 /**
1484 * The pipe->create_sampler_view() driver hook.
1485 */
1486 static struct pipe_sampler_view *
1487 iris_create_sampler_view(struct pipe_context *ctx,
1488 struct pipe_resource *tex,
1489 const struct pipe_sampler_view *tmpl)
1490 {
1491 struct iris_context *ice = (struct iris_context *) ctx;
1492 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1493 const struct gen_device_info *devinfo = &screen->devinfo;
1494 struct iris_sampler_view *isv = calloc(1, sizeof(struct iris_sampler_view));
1495
1496 if (!isv)
1497 return NULL;
1498
1499 /* initialize base object */
1500 isv->base = *tmpl;
1501 isv->base.context = ctx;
1502 isv->base.texture = NULL;
1503 pipe_reference_init(&isv->base.reference, 1);
1504 pipe_resource_reference(&isv->base.texture, tex);
1505
1506 void *map = upload_state(ice->state.surface_uploader, &isv->surface_state,
1507 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1508 if (!unlikely(map))
1509 return NULL;
1510
1511 struct iris_bo *state_bo = iris_resource_bo(isv->surface_state.res);
1512 isv->surface_state.offset += iris_bo_offset_from_base_address(state_bo);
1513
1514 if (util_format_is_depth_or_stencil(tmpl->format)) {
1515 struct iris_resource *zres, *sres;
1516 const struct util_format_description *desc =
1517 util_format_description(tmpl->format);
1518
1519 iris_get_depth_stencil_resources(tex, &zres, &sres);
1520
1521 tex = util_format_has_depth(desc) ? &zres->base : &sres->base;
1522 }
1523
1524 isv->res = (struct iris_resource *) tex;
1525
1526 isl_surf_usage_flags_t usage = ISL_SURF_USAGE_TEXTURE_BIT;
1527
1528 if (isv->base.target == PIPE_TEXTURE_CUBE ||
1529 isv->base.target == PIPE_TEXTURE_CUBE_ARRAY)
1530 usage |= ISL_SURF_USAGE_CUBE_BIT;
1531
1532 const struct iris_format_info fmt =
1533 iris_format_for_usage(devinfo, tmpl->format, usage);
1534
1535 isv->view = (struct isl_view) {
1536 .format = fmt.fmt,
1537 .swizzle = (struct isl_swizzle) {
1538 .r = fmt_swizzle(&fmt, tmpl->swizzle_r),
1539 .g = fmt_swizzle(&fmt, tmpl->swizzle_g),
1540 .b = fmt_swizzle(&fmt, tmpl->swizzle_b),
1541 .a = fmt_swizzle(&fmt, tmpl->swizzle_a),
1542 },
1543 .usage = usage,
1544 };
1545
1546 /* Fill out SURFACE_STATE for this view. */
1547 if (tmpl->target != PIPE_BUFFER) {
1548 isv->view.base_level = tmpl->u.tex.first_level;
1549 isv->view.levels = tmpl->u.tex.last_level - tmpl->u.tex.first_level + 1;
1550 // XXX: do I need to port f9fd0cf4790cb2a530e75d1a2206dbb9d8af7cb2?
1551 isv->view.base_array_layer = tmpl->u.tex.first_layer;
1552 isv->view.array_len =
1553 tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1;
1554
1555 isl_surf_fill_state(&screen->isl_dev, map,
1556 .surf = &isv->res->surf, .view = &isv->view,
1557 .mocs = MOCS_WB,
1558 .address = isv->res->bo->gtt_offset);
1559 // .aux_surf =
1560 // .clear_color = clear_color,
1561 } else {
1562 fill_buffer_surface_state(&screen->isl_dev, isv->res->bo, map,
1563 isv->view.format, tmpl->u.buf.offset,
1564 tmpl->u.buf.size);
1565 }
1566
1567 return &isv->base;
1568 }
1569
1570 static void
1571 iris_sampler_view_destroy(struct pipe_context *ctx,
1572 struct pipe_sampler_view *state)
1573 {
1574 struct iris_sampler_view *isv = (void *) state;
1575 pipe_resource_reference(&state->texture, NULL);
1576 pipe_resource_reference(&isv->surface_state.res, NULL);
1577 free(isv);
1578 }
1579
1580 /**
1581 * The pipe->create_surface() driver hook.
1582 *
1583 * In Gallium nomenclature, "surfaces" are a view of a resource that
1584 * can be bound as a render target or depth/stencil buffer.
1585 */
1586 static struct pipe_surface *
1587 iris_create_surface(struct pipe_context *ctx,
1588 struct pipe_resource *tex,
1589 const struct pipe_surface *tmpl)
1590 {
1591 struct iris_context *ice = (struct iris_context *) ctx;
1592 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1593 const struct gen_device_info *devinfo = &screen->devinfo;
1594 struct iris_surface *surf = calloc(1, sizeof(struct iris_surface));
1595 struct pipe_surface *psurf = &surf->base;
1596 struct iris_resource *res = (struct iris_resource *) tex;
1597
1598 if (!surf)
1599 return NULL;
1600
1601 pipe_reference_init(&psurf->reference, 1);
1602 pipe_resource_reference(&psurf->texture, tex);
1603 psurf->context = ctx;
1604 psurf->format = tmpl->format;
1605 psurf->width = tex->width0;
1606 psurf->height = tex->height0;
1607 psurf->texture = tex;
1608 psurf->u.tex.first_layer = tmpl->u.tex.first_layer;
1609 psurf->u.tex.last_layer = tmpl->u.tex.last_layer;
1610 psurf->u.tex.level = tmpl->u.tex.level;
1611
1612 isl_surf_usage_flags_t usage = 0;
1613 if (tmpl->writable)
1614 usage = ISL_SURF_USAGE_STORAGE_BIT;
1615 else if (util_format_is_depth_or_stencil(tmpl->format))
1616 usage = ISL_SURF_USAGE_DEPTH_BIT;
1617 else
1618 usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
1619
1620 const struct iris_format_info fmt =
1621 iris_format_for_usage(devinfo, psurf->format, usage);
1622
1623 if ((usage & ISL_SURF_USAGE_RENDER_TARGET_BIT) &&
1624 !isl_format_supports_rendering(devinfo, fmt.fmt)) {
1625 /* Framebuffer validation will reject this invalid case, but it
1626 * hasn't had the opportunity yet. In the meantime, we need to
1627 * avoid hitting ISL asserts about unsupported formats below.
1628 */
1629 free(surf);
1630 return NULL;
1631 }
1632
1633 surf->view = (struct isl_view) {
1634 .format = fmt.fmt,
1635 .base_level = tmpl->u.tex.level,
1636 .levels = 1,
1637 .base_array_layer = tmpl->u.tex.first_layer,
1638 .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
1639 .swizzle = ISL_SWIZZLE_IDENTITY,
1640 .usage = usage,
1641 };
1642
1643 /* Bail early for depth/stencil - we don't want SURFACE_STATE for them. */
1644 if (res->surf.usage & (ISL_SURF_USAGE_DEPTH_BIT |
1645 ISL_SURF_USAGE_STENCIL_BIT))
1646 return psurf;
1647
1648
1649 void *map = upload_state(ice->state.surface_uploader, &surf->surface_state,
1650 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1651 if (!unlikely(map))
1652 return NULL;
1653
1654 struct iris_bo *state_bo = iris_resource_bo(surf->surface_state.res);
1655 surf->surface_state.offset += iris_bo_offset_from_base_address(state_bo);
1656
1657 isl_surf_fill_state(&screen->isl_dev, map,
1658 .surf = &res->surf, .view = &surf->view,
1659 .mocs = MOCS_WB,
1660 .address = res->bo->gtt_offset);
1661 // .aux_surf =
1662 // .clear_color = clear_color,
1663
1664 return psurf;
1665 }
1666
1667 /**
1668 * The pipe->set_shader_images() driver hook.
1669 */
1670 static void
1671 iris_set_shader_images(struct pipe_context *ctx,
1672 enum pipe_shader_type p_stage,
1673 unsigned start_slot, unsigned count,
1674 const struct pipe_image_view *p_images)
1675 {
1676 struct iris_context *ice = (struct iris_context *) ctx;
1677 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1678 const struct gen_device_info *devinfo = &screen->devinfo;
1679 gl_shader_stage stage = stage_from_pipe(p_stage);
1680 struct iris_shader_state *shs = &ice->state.shaders[stage];
1681
1682 shs->num_images = MAX2(shs->num_images, start_slot + count);
1683
1684 for (unsigned i = 0; i < count; i++) {
1685 if (p_images && p_images[i].resource) {
1686 const struct pipe_image_view *img = &p_images[i];
1687 struct iris_resource *res = (void *) img->resource;
1688 pipe_resource_reference(&shs->image[start_slot + i].res, &res->base);
1689
1690 res->bind_history |= PIPE_BIND_SHADER_IMAGE;
1691
1692 // XXX: these are not retained forever, use a separate uploader?
1693 void *map =
1694 upload_state(ice->state.surface_uploader,
1695 &shs->image[start_slot + i].surface_state,
1696 4 * GENX(RENDER_SURFACE_STATE_length), 64);
1697 if (!unlikely(map)) {
1698 pipe_resource_reference(&shs->image[start_slot + i].res, NULL);
1699 return;
1700 }
1701
1702 struct iris_bo *surf_state_bo =
1703 iris_resource_bo(shs->image[start_slot + i].surface_state.res);
1704 shs->image[start_slot + i].surface_state.offset +=
1705 iris_bo_offset_from_base_address(surf_state_bo);
1706
1707 isl_surf_usage_flags_t usage = ISL_SURF_USAGE_STORAGE_BIT;
1708 enum isl_format isl_format =
1709 iris_format_for_usage(devinfo, img->format, usage).fmt;
1710
1711 if (img->shader_access & PIPE_IMAGE_ACCESS_READ)
1712 isl_format = isl_lower_storage_image_format(devinfo, isl_format);
1713
1714 shs->image[start_slot + i].access = img->shader_access;
1715
1716 if (res->base.target != PIPE_BUFFER) {
1717 struct isl_view view = {
1718 .format = isl_format,
1719 .base_level = img->u.tex.level,
1720 .levels = 1,
1721 .base_array_layer = img->u.tex.first_layer,
1722 .array_len = img->u.tex.last_layer - img->u.tex.first_layer + 1,
1723 .swizzle = ISL_SWIZZLE_IDENTITY,
1724 .usage = usage,
1725 };
1726
1727 isl_surf_fill_state(&screen->isl_dev, map,
1728 .surf = &res->surf, .view = &view,
1729 .mocs = MOCS_WB,
1730 .address = res->bo->gtt_offset);
1731 // .aux_surf =
1732 // .clear_color = clear_color,
1733 } else {
1734 fill_buffer_surface_state(&screen->isl_dev, res->bo, map,
1735 isl_format, img->u.buf.offset,
1736 img->u.buf.size);
1737 }
1738 } else {
1739 pipe_resource_reference(&shs->image[start_slot + i].res, NULL);
1740 pipe_resource_reference(&shs->image[start_slot + i].surface_state.res,
1741 NULL);
1742 }
1743 }
1744
1745 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
1746 }
1747
1748
1749 /**
1750 * The pipe->set_sampler_views() driver hook.
1751 */
1752 static void
1753 iris_set_sampler_views(struct pipe_context *ctx,
1754 enum pipe_shader_type p_stage,
1755 unsigned start, unsigned count,
1756 struct pipe_sampler_view **views)
1757 {
1758 struct iris_context *ice = (struct iris_context *) ctx;
1759 gl_shader_stage stage = stage_from_pipe(p_stage);
1760 struct iris_shader_state *shs = &ice->state.shaders[stage];
1761
1762 unsigned i;
1763 for (i = 0; i < count; i++) {
1764 pipe_sampler_view_reference((struct pipe_sampler_view **)
1765 &shs->textures[i], views[i]);
1766 struct iris_sampler_view *view = (void *) views[i];
1767 if (view)
1768 view->res->bind_history |= PIPE_BIND_SAMPLER_VIEW;
1769 }
1770 for (; i < shs->num_textures; i++) {
1771 pipe_sampler_view_reference((struct pipe_sampler_view **)
1772 &shs->textures[i], NULL);
1773 }
1774
1775 shs->num_textures = count;
1776
1777 ice->state.dirty |= (IRIS_DIRTY_BINDINGS_VS << stage);
1778 }
1779
1780 /**
1781 * The pipe->set_tess_state() driver hook.
1782 */
1783 static void
1784 iris_set_tess_state(struct pipe_context *ctx,
1785 const float default_outer_level[4],
1786 const float default_inner_level[2])
1787 {
1788 struct iris_context *ice = (struct iris_context *) ctx;
1789
1790 memcpy(&ice->state.default_outer_level[0], &default_outer_level[0], 4 * sizeof(float));
1791 memcpy(&ice->state.default_inner_level[0], &default_inner_level[0], 2 * sizeof(float));
1792
1793 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_TCS;
1794 }
1795
1796 static void
1797 iris_surface_destroy(struct pipe_context *ctx, struct pipe_surface *p_surf)
1798 {
1799 struct iris_surface *surf = (void *) p_surf;
1800 pipe_resource_reference(&p_surf->texture, NULL);
1801 pipe_resource_reference(&surf->surface_state.res, NULL);
1802 free(surf);
1803 }
1804
1805 static void
1806 iris_set_clip_state(struct pipe_context *ctx,
1807 const struct pipe_clip_state *state)
1808 {
1809 struct iris_context *ice = (struct iris_context *) ctx;
1810 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_VERTEX];
1811
1812 memcpy(&ice->state.clip_planes, state, sizeof(*state));
1813
1814 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_VS;
1815 shs->cbuf0_needs_upload = true;
1816 }
1817
1818 /**
1819 * The pipe->set_polygon_stipple() driver hook.
1820 */
1821 static void
1822 iris_set_polygon_stipple(struct pipe_context *ctx,
1823 const struct pipe_poly_stipple *state)
1824 {
1825 struct iris_context *ice = (struct iris_context *) ctx;
1826 memcpy(&ice->state.poly_stipple, state, sizeof(*state));
1827 ice->state.dirty |= IRIS_DIRTY_POLYGON_STIPPLE;
1828 }
1829
1830 /**
1831 * The pipe->set_sample_mask() driver hook.
1832 */
1833 static void
1834 iris_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
1835 {
1836 struct iris_context *ice = (struct iris_context *) ctx;
1837
1838 /* We only support 16x MSAA, so we have 16 bits of sample maks.
1839 * st/mesa may pass us 0xffffffff though, meaning "enable all samples".
1840 */
1841 ice->state.sample_mask = sample_mask & 0xffff;
1842 ice->state.dirty |= IRIS_DIRTY_SAMPLE_MASK;
1843 }
1844
1845 /**
1846 * The pipe->set_scissor_states() driver hook.
1847 *
1848 * This corresponds to our SCISSOR_RECT state structures. It's an
1849 * exact match, so we just store them, and memcpy them out later.
1850 */
1851 static void
1852 iris_set_scissor_states(struct pipe_context *ctx,
1853 unsigned start_slot,
1854 unsigned num_scissors,
1855 const struct pipe_scissor_state *rects)
1856 {
1857 struct iris_context *ice = (struct iris_context *) ctx;
1858
1859 for (unsigned i = 0; i < num_scissors; i++) {
1860 if (rects[i].minx == rects[i].maxx || rects[i].miny == rects[i].maxy) {
1861 /* If the scissor was out of bounds and got clamped to 0 width/height
1862 * at the bounds, the subtraction of 1 from maximums could produce a
1863 * negative number and thus not clip anything. Instead, just provide
1864 * a min > max scissor inside the bounds, which produces the expected
1865 * no rendering.
1866 */
1867 ice->state.scissors[start_slot + i] = (struct pipe_scissor_state) {
1868 .minx = 1, .maxx = 0, .miny = 1, .maxy = 0,
1869 };
1870 } else {
1871 ice->state.scissors[start_slot + i] = (struct pipe_scissor_state) {
1872 .minx = rects[i].minx, .miny = rects[i].miny,
1873 .maxx = rects[i].maxx - 1, .maxy = rects[i].maxy - 1,
1874 };
1875 }
1876 }
1877
1878 ice->state.dirty |= IRIS_DIRTY_SCISSOR_RECT;
1879 }
1880
1881 /**
1882 * The pipe->set_stencil_ref() driver hook.
1883 *
1884 * This is added to 3DSTATE_WM_DEPTH_STENCIL dynamically at draw time.
1885 */
1886 static void
1887 iris_set_stencil_ref(struct pipe_context *ctx,
1888 const struct pipe_stencil_ref *state)
1889 {
1890 struct iris_context *ice = (struct iris_context *) ctx;
1891 memcpy(&ice->state.stencil_ref, state, sizeof(*state));
1892 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
1893 }
1894
1895 static float
1896 viewport_extent(const struct pipe_viewport_state *state, int axis, float sign)
1897 {
1898 return copysignf(state->scale[axis], sign) + state->translate[axis];
1899 }
1900
1901 #if 0
1902 static void
1903 calculate_guardband_size(uint32_t fb_width, uint32_t fb_height,
1904 float m00, float m11, float m30, float m31,
1905 float *xmin, float *xmax,
1906 float *ymin, float *ymax)
1907 {
1908 /* According to the "Vertex X,Y Clamping and Quantization" section of the
1909 * Strips and Fans documentation:
1910 *
1911 * "The vertex X and Y screen-space coordinates are also /clamped/ to the
1912 * fixed-point "guardband" range supported by the rasterization hardware"
1913 *
1914 * and
1915 *
1916 * "In almost all circumstances, if an object’s vertices are actually
1917 * modified by this clamping (i.e., had X or Y coordinates outside of
1918 * the guardband extent the rendered object will not match the intended
1919 * result. Therefore software should take steps to ensure that this does
1920 * not happen - e.g., by clipping objects such that they do not exceed
1921 * these limits after the Drawing Rectangle is applied."
1922 *
1923 * I believe the fundamental restriction is that the rasterizer (in
1924 * the SF/WM stages) have a limit on the number of pixels that can be
1925 * rasterized. We need to ensure any coordinates beyond the rasterizer
1926 * limit are handled by the clipper. So effectively that limit becomes
1927 * the clipper's guardband size.
1928 *
1929 * It goes on to say:
1930 *
1931 * "In addition, in order to be correctly rendered, objects must have a
1932 * screenspace bounding box not exceeding 8K in the X or Y direction.
1933 * This additional restriction must also be comprehended by software,
1934 * i.e., enforced by use of clipping."
1935 *
1936 * This makes no sense. Gen7+ hardware supports 16K render targets,
1937 * and you definitely need to be able to draw polygons that fill the
1938 * surface. Our assumption is that the rasterizer was limited to 8K
1939 * on Sandybridge, which only supports 8K surfaces, and it was actually
1940 * increased to 16K on Ivybridge and later.
1941 *
1942 * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
1943 */
1944 const float gb_size = GEN_GEN >= 7 ? 16384.0f : 8192.0f;
1945
1946 if (m00 != 0 && m11 != 0) {
1947 /* First, we compute the screen-space render area */
1948 const float ss_ra_xmin = MIN3( 0, m30 + m00, m30 - m00);
1949 const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
1950 const float ss_ra_ymin = MIN3( 0, m31 + m11, m31 - m11);
1951 const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
1952
1953 /* We want the guardband to be centered on that */
1954 const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
1955 const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
1956 const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
1957 const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
1958
1959 /* Now we need it in native device coordinates */
1960 const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
1961 const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
1962 const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
1963 const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
1964
1965 /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
1966 * flipped upside-down. X should be fine though.
1967 */
1968 assert(ndc_gb_xmin <= ndc_gb_xmax);
1969 *xmin = ndc_gb_xmin;
1970 *xmax = ndc_gb_xmax;
1971 *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
1972 *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
1973 } else {
1974 /* The viewport scales to 0, so nothing will be rendered. */
1975 *xmin = 0.0f;
1976 *xmax = 0.0f;
1977 *ymin = 0.0f;
1978 *ymax = 0.0f;
1979 }
1980 }
1981 #endif
1982
1983 /**
1984 * The pipe->set_viewport_states() driver hook.
1985 *
1986 * This corresponds to our SF_CLIP_VIEWPORT states. We can't calculate
1987 * the guardband yet, as we need the framebuffer dimensions, but we can
1988 * at least fill out the rest.
1989 */
1990 static void
1991 iris_set_viewport_states(struct pipe_context *ctx,
1992 unsigned start_slot,
1993 unsigned count,
1994 const struct pipe_viewport_state *states)
1995 {
1996 struct iris_context *ice = (struct iris_context *) ctx;
1997 struct iris_genx_state *genx = ice->state.genx;
1998 uint32_t *vp_map =
1999 &genx->sf_cl_vp[start_slot * GENX(SF_CLIP_VIEWPORT_length)];
2000
2001 for (unsigned i = 0; i < count; i++) {
2002 const struct pipe_viewport_state *state = &states[i];
2003
2004 memcpy(&ice->state.viewports[start_slot + i], state, sizeof(*state));
2005
2006 iris_pack_state(GENX(SF_CLIP_VIEWPORT), vp_map, vp) {
2007 vp.ViewportMatrixElementm00 = state->scale[0];
2008 vp.ViewportMatrixElementm11 = state->scale[1];
2009 vp.ViewportMatrixElementm22 = state->scale[2];
2010 vp.ViewportMatrixElementm30 = state->translate[0];
2011 vp.ViewportMatrixElementm31 = state->translate[1];
2012 vp.ViewportMatrixElementm32 = state->translate[2];
2013 /* XXX: in i965 this is computed based on the drawbuffer size,
2014 * but we don't have that here...
2015 */
2016 vp.XMinClipGuardband = -1.0;
2017 vp.XMaxClipGuardband = 1.0;
2018 vp.YMinClipGuardband = -1.0;
2019 vp.YMaxClipGuardband = 1.0;
2020 vp.XMinViewPort = viewport_extent(state, 0, -1.0f);
2021 vp.XMaxViewPort = viewport_extent(state, 0, 1.0f) - 1;
2022 vp.YMinViewPort = viewport_extent(state, 1, -1.0f);
2023 vp.YMaxViewPort = viewport_extent(state, 1, 1.0f) - 1;
2024 }
2025
2026 vp_map += GENX(SF_CLIP_VIEWPORT_length);
2027 }
2028
2029 ice->state.dirty |= IRIS_DIRTY_SF_CL_VIEWPORT;
2030
2031 if (ice->state.cso_rast && (!ice->state.cso_rast->depth_clip_near ||
2032 !ice->state.cso_rast->depth_clip_far))
2033 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
2034 }
2035
2036 /**
2037 * The pipe->set_framebuffer_state() driver hook.
2038 *
2039 * Sets the current draw FBO, including color render targets, depth,
2040 * and stencil buffers.
2041 */
2042 static void
2043 iris_set_framebuffer_state(struct pipe_context *ctx,
2044 const struct pipe_framebuffer_state *state)
2045 {
2046 struct iris_context *ice = (struct iris_context *) ctx;
2047 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
2048 struct isl_device *isl_dev = &screen->isl_dev;
2049 struct pipe_framebuffer_state *cso = &ice->state.framebuffer;
2050 struct iris_resource *zres;
2051 struct iris_resource *stencil_res;
2052
2053 unsigned samples = util_framebuffer_get_num_samples(state);
2054
2055 if (cso->samples != samples) {
2056 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
2057 }
2058
2059 if (cso->nr_cbufs != state->nr_cbufs) {
2060 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
2061 }
2062
2063 if ((cso->layers == 0) != (state->layers == 0)) {
2064 ice->state.dirty |= IRIS_DIRTY_CLIP;
2065 }
2066
2067 util_copy_framebuffer_state(cso, state);
2068 cso->samples = samples;
2069
2070 struct iris_depth_buffer_state *cso_z = &ice->state.genx->depth_buffer;
2071
2072 struct isl_view view = {
2073 .base_level = 0,
2074 .levels = 1,
2075 .base_array_layer = 0,
2076 .array_len = 1,
2077 .swizzle = ISL_SWIZZLE_IDENTITY,
2078 };
2079
2080 struct isl_depth_stencil_hiz_emit_info info = {
2081 .view = &view,
2082 .mocs = MOCS_WB,
2083 };
2084
2085 if (cso->zsbuf) {
2086 iris_get_depth_stencil_resources(cso->zsbuf->texture, &zres,
2087 &stencil_res);
2088
2089 view.base_level = cso->zsbuf->u.tex.level;
2090 view.base_array_layer = cso->zsbuf->u.tex.first_layer;
2091 view.array_len =
2092 cso->zsbuf->u.tex.last_layer - cso->zsbuf->u.tex.first_layer + 1;
2093
2094 if (zres) {
2095 view.usage |= ISL_SURF_USAGE_DEPTH_BIT;
2096
2097 info.depth_surf = &zres->surf;
2098 info.depth_address = zres->bo->gtt_offset;
2099 info.hiz_usage = ISL_AUX_USAGE_NONE;
2100
2101 view.format = zres->surf.format;
2102 }
2103
2104 if (stencil_res) {
2105 view.usage |= ISL_SURF_USAGE_STENCIL_BIT;
2106 info.stencil_surf = &stencil_res->surf;
2107 info.stencil_address = stencil_res->bo->gtt_offset;
2108 if (!zres)
2109 view.format = stencil_res->surf.format;
2110 }
2111 }
2112
2113 isl_emit_depth_stencil_hiz_s(isl_dev, cso_z->packets, &info);
2114
2115 /* Make a null surface for unbound buffers */
2116 void *null_surf_map =
2117 upload_state(ice->state.surface_uploader, &ice->state.null_fb,
2118 4 * GENX(RENDER_SURFACE_STATE_length), 64);
2119 isl_null_fill_state(&screen->isl_dev, null_surf_map,
2120 isl_extent3d(MAX2(cso->width, 1),
2121 MAX2(cso->height, 1),
2122 cso->layers ? cso->layers : 1));
2123 ice->state.null_fb.offset +=
2124 iris_bo_offset_from_base_address(iris_resource_bo(ice->state.null_fb.res));
2125
2126 ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
2127
2128 /* Render target change */
2129 ice->state.dirty |= IRIS_DIRTY_BINDINGS_FS;
2130
2131 ice->state.dirty |= ice->state.dirty_for_nos[IRIS_NOS_FRAMEBUFFER];
2132
2133 #if GEN_GEN == 11
2134 // XXX: we may want to flag IRIS_DIRTY_MULTISAMPLE (or SAMPLE_MASK?)
2135 // XXX: see commit 979fc1bc9bcc64027ff2cfafd285676f31b930a6
2136
2137 /* The PIPE_CONTROL command description says:
2138 *
2139 * "Whenever a Binding Table Index (BTI) used by a Render Target Message
2140 * points to a different RENDER_SURFACE_STATE, SW must issue a Render
2141 * Target Cache Flush by enabling this bit. When render target flush
2142 * is set due to new association of BTI, PS Scoreboard Stall bit must
2143 * be set in this packet."
2144 */
2145 // XXX: does this need to happen at 3DSTATE_BTP_PS time?
2146 iris_emit_pipe_control_flush(&ice->batches[IRIS_BATCH_RENDER],
2147 PIPE_CONTROL_RENDER_TARGET_FLUSH |
2148 PIPE_CONTROL_STALL_AT_SCOREBOARD);
2149 #endif
2150 }
2151
2152 static void
2153 upload_ubo_surf_state(struct iris_context *ice,
2154 struct iris_const_buffer *cbuf,
2155 unsigned buffer_size)
2156 {
2157 struct pipe_context *ctx = &ice->ctx;
2158 struct iris_screen *screen = (struct iris_screen *) ctx->screen;
2159
2160 // XXX: these are not retained forever, use a separate uploader?
2161 void *map =
2162 upload_state(ice->state.surface_uploader, &cbuf->surface_state,
2163 4 * GENX(RENDER_SURFACE_STATE_length), 64);
2164 if (!unlikely(map)) {
2165 pipe_resource_reference(&cbuf->data.res, NULL);
2166 return;
2167 }
2168
2169 struct iris_resource *res = (void *) cbuf->data.res;
2170 struct iris_bo *surf_bo = iris_resource_bo(cbuf->surface_state.res);
2171 cbuf->surface_state.offset += iris_bo_offset_from_base_address(surf_bo);
2172
2173 isl_buffer_fill_state(&screen->isl_dev, map,
2174 .address = res->bo->gtt_offset + cbuf->data.offset,
2175 .size_B = MIN2(buffer_size,
2176 res->bo->size - cbuf->data.offset),
2177 .format = ISL_FORMAT_R32G32B32A32_FLOAT,
2178 .stride_B = 1,
2179 .mocs = MOCS_WB)
2180 }
2181
2182 /**
2183 * The pipe->set_constant_buffer() driver hook.
2184 *
2185 * This uploads any constant data in user buffers, and references
2186 * any UBO resources containing constant data.
2187 */
2188 static void
2189 iris_set_constant_buffer(struct pipe_context *ctx,
2190 enum pipe_shader_type p_stage, unsigned index,
2191 const struct pipe_constant_buffer *input)
2192 {
2193 struct iris_context *ice = (struct iris_context *) ctx;
2194 gl_shader_stage stage = stage_from_pipe(p_stage);
2195 struct iris_shader_state *shs = &ice->state.shaders[stage];
2196 struct iris_const_buffer *cbuf = &shs->constbuf[index];
2197
2198 if (input && input->buffer) {
2199 assert(index > 0);
2200
2201 pipe_resource_reference(&cbuf->data.res, input->buffer);
2202 cbuf->data.offset = input->buffer_offset;
2203
2204 struct iris_resource *res = (void *) cbuf->data.res;
2205 res->bind_history |= PIPE_BIND_CONSTANT_BUFFER;
2206
2207 upload_ubo_surf_state(ice, cbuf, input->buffer_size);
2208 } else {
2209 pipe_resource_reference(&cbuf->data.res, NULL);
2210 pipe_resource_reference(&cbuf->surface_state.res, NULL);
2211 }
2212
2213 if (index == 0) {
2214 if (input)
2215 memcpy(&shs->cbuf0, input, sizeof(shs->cbuf0));
2216 else
2217 memset(&shs->cbuf0, 0, sizeof(shs->cbuf0));
2218
2219 shs->cbuf0_needs_upload = true;
2220 }
2221
2222 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_VS << stage;
2223 // XXX: maybe not necessary all the time...?
2224 // XXX: we need 3DS_BTP to commit these changes, and if we fell back to
2225 // XXX: pull model we may need actual new bindings...
2226 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
2227 }
2228
2229 static void
2230 upload_uniforms(struct iris_context *ice,
2231 gl_shader_stage stage)
2232 {
2233 struct iris_shader_state *shs = &ice->state.shaders[stage];
2234 struct iris_const_buffer *cbuf = &shs->constbuf[0];
2235 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2236
2237 unsigned upload_size = shader->num_system_values * sizeof(uint32_t) +
2238 shs->cbuf0.buffer_size;
2239
2240 if (upload_size == 0)
2241 return;
2242
2243 uint32_t *map =
2244 upload_state(ice->ctx.const_uploader, &cbuf->data, upload_size, 64);
2245
2246 for (int i = 0; i < shader->num_system_values; i++) {
2247 uint32_t sysval = shader->system_values[i];
2248 uint32_t value = 0;
2249
2250 if (BRW_PARAM_BUILTIN_IS_CLIP_PLANE(sysval)) {
2251 int plane = BRW_PARAM_BUILTIN_CLIP_PLANE_IDX(sysval);
2252 int comp = BRW_PARAM_BUILTIN_CLIP_PLANE_COMP(sysval);
2253 value = fui(ice->state.clip_planes.ucp[plane][comp]);
2254 } else {
2255 assert(!"unhandled system value");
2256 }
2257
2258 *map++ = value;
2259 }
2260
2261 if (shs->cbuf0.user_buffer) {
2262 memcpy(map, shs->cbuf0.user_buffer, shs->cbuf0.buffer_size);
2263 }
2264
2265 upload_ubo_surf_state(ice, cbuf, upload_size);
2266 }
2267
2268 /**
2269 * The pipe->set_shader_buffers() driver hook.
2270 *
2271 * This binds SSBOs and ABOs. Unfortunately, we need to stream out
2272 * SURFACE_STATE here, as the buffer offset may change each time.
2273 */
2274 static void
2275 iris_set_shader_buffers(struct pipe_context *ctx,
2276 enum pipe_shader_type p_stage,
2277 unsigned start_slot, unsigned count,
2278 const struct pipe_shader_buffer *buffers)
2279 {
2280 struct iris_context *ice = (struct iris_context *) ctx;
2281 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
2282 gl_shader_stage stage = stage_from_pipe(p_stage);
2283 struct iris_shader_state *shs = &ice->state.shaders[stage];
2284
2285 for (unsigned i = 0; i < count; i++) {
2286 if (buffers && buffers[i].buffer) {
2287 const struct pipe_shader_buffer *buffer = &buffers[i];
2288 struct iris_resource *res = (void *) buffer->buffer;
2289 pipe_resource_reference(&shs->ssbo[start_slot + i], &res->base);
2290
2291 res->bind_history |= PIPE_BIND_SHADER_BUFFER;
2292
2293 // XXX: these are not retained forever, use a separate uploader?
2294 void *map =
2295 upload_state(ice->state.surface_uploader,
2296 &shs->ssbo_surface_state[start_slot + i],
2297 4 * GENX(RENDER_SURFACE_STATE_length), 64);
2298 if (!unlikely(map)) {
2299 pipe_resource_reference(&shs->ssbo[start_slot + i], NULL);
2300 return;
2301 }
2302
2303 struct iris_bo *surf_state_bo =
2304 iris_resource_bo(shs->ssbo_surface_state[start_slot + i].res);
2305 shs->ssbo_surface_state[start_slot + i].offset +=
2306 iris_bo_offset_from_base_address(surf_state_bo);
2307
2308 isl_buffer_fill_state(&screen->isl_dev, map,
2309 .address =
2310 res->bo->gtt_offset + buffer->buffer_offset,
2311 .size_B =
2312 MIN2(buffer->buffer_size,
2313 res->bo->size - buffer->buffer_offset),
2314 .format = ISL_FORMAT_RAW,
2315 .stride_B = 1,
2316 .mocs = MOCS_WB);
2317 } else {
2318 pipe_resource_reference(&shs->ssbo[start_slot + i], NULL);
2319 pipe_resource_reference(&shs->ssbo_surface_state[start_slot + i].res,
2320 NULL);
2321 }
2322 }
2323
2324 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
2325 }
2326
2327 static void
2328 iris_delete_state(struct pipe_context *ctx, void *state)
2329 {
2330 free(state);
2331 }
2332
2333 static void
2334 iris_free_vertex_buffers(struct iris_vertex_buffer_state *cso)
2335 {
2336 for (unsigned i = 0; i < cso->num_buffers; i++)
2337 pipe_resource_reference(&cso->resources[i], NULL);
2338 }
2339
2340 /**
2341 * The pipe->set_vertex_buffers() driver hook.
2342 *
2343 * This translates pipe_vertex_buffer to our 3DSTATE_VERTEX_BUFFERS packet.
2344 */
2345 static void
2346 iris_set_vertex_buffers(struct pipe_context *ctx,
2347 unsigned start_slot, unsigned count,
2348 const struct pipe_vertex_buffer *buffers)
2349 {
2350 struct iris_context *ice = (struct iris_context *) ctx;
2351 struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
2352
2353 iris_free_vertex_buffers(&ice->state.genx->vertex_buffers);
2354
2355 if (!buffers)
2356 count = 0;
2357
2358 cso->num_buffers = count;
2359
2360 iris_pack_command(GENX(3DSTATE_VERTEX_BUFFERS), cso->vertex_buffers, vb) {
2361 vb.DWordLength = 4 * MAX2(cso->num_buffers, 1) - 1;
2362 }
2363
2364 uint32_t *vb_pack_dest = &cso->vertex_buffers[1];
2365
2366 if (count == 0) {
2367 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
2368 vb.VertexBufferIndex = start_slot;
2369 vb.NullVertexBuffer = true;
2370 vb.AddressModifyEnable = true;
2371 }
2372 }
2373
2374 for (unsigned i = 0; i < count; i++) {
2375 assert(!buffers[i].is_user_buffer);
2376
2377 pipe_resource_reference(&cso->resources[i], buffers[i].buffer.resource);
2378 struct iris_resource *res = (void *) cso->resources[i];
2379
2380 if (res)
2381 res->bind_history |= PIPE_BIND_VERTEX_BUFFER;
2382
2383 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
2384 vb.VertexBufferIndex = start_slot + i;
2385 vb.MOCS = MOCS_WB;
2386 vb.AddressModifyEnable = true;
2387 vb.BufferPitch = buffers[i].stride;
2388 if (res) {
2389 vb.BufferSize = res->bo->size;
2390 vb.BufferStartingAddress =
2391 ro_bo(NULL, res->bo->gtt_offset + buffers[i].buffer_offset);
2392 } else {
2393 vb.NullVertexBuffer = true;
2394 }
2395 }
2396
2397 vb_pack_dest += GENX(VERTEX_BUFFER_STATE_length);
2398 }
2399
2400 ice->state.dirty |= IRIS_DIRTY_VERTEX_BUFFERS;
2401 }
2402
2403 /**
2404 * Gallium CSO for vertex elements.
2405 */
2406 struct iris_vertex_element_state {
2407 uint32_t vertex_elements[1 + 33 * GENX(VERTEX_ELEMENT_STATE_length)];
2408 uint32_t vf_instancing[33 * GENX(3DSTATE_VF_INSTANCING_length)];
2409 unsigned count;
2410 };
2411
2412 /**
2413 * The pipe->create_vertex_elements() driver hook.
2414 *
2415 * This translates pipe_vertex_element to our 3DSTATE_VERTEX_ELEMENTS
2416 * and 3DSTATE_VF_INSTANCING commands. SGVs are handled at draw time.
2417 */
2418 static void *
2419 iris_create_vertex_elements(struct pipe_context *ctx,
2420 unsigned count,
2421 const struct pipe_vertex_element *state)
2422 {
2423 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
2424 const struct gen_device_info *devinfo = &screen->devinfo;
2425 struct iris_vertex_element_state *cso =
2426 malloc(sizeof(struct iris_vertex_element_state));
2427
2428 cso->count = count;
2429
2430 /* TODO:
2431 * - create edge flag one
2432 * - create SGV ones
2433 * - if those are necessary, use count + 1/2/3... OR in the length
2434 */
2435 iris_pack_command(GENX(3DSTATE_VERTEX_ELEMENTS), cso->vertex_elements, ve) {
2436 ve.DWordLength =
2437 1 + GENX(VERTEX_ELEMENT_STATE_length) * MAX2(count, 1) - 2;
2438 }
2439
2440 uint32_t *ve_pack_dest = &cso->vertex_elements[1];
2441 uint32_t *vfi_pack_dest = cso->vf_instancing;
2442
2443 if (count == 0) {
2444 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
2445 ve.Valid = true;
2446 ve.SourceElementFormat = ISL_FORMAT_R32G32B32A32_FLOAT;
2447 ve.Component0Control = VFCOMP_STORE_0;
2448 ve.Component1Control = VFCOMP_STORE_0;
2449 ve.Component2Control = VFCOMP_STORE_0;
2450 ve.Component3Control = VFCOMP_STORE_1_FP;
2451 }
2452
2453 iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
2454 }
2455 }
2456
2457 for (int i = 0; i < count; i++) {
2458 const struct iris_format_info fmt =
2459 iris_format_for_usage(devinfo, state[i].src_format, 0);
2460 unsigned comp[4] = { VFCOMP_STORE_SRC, VFCOMP_STORE_SRC,
2461 VFCOMP_STORE_SRC, VFCOMP_STORE_SRC };
2462
2463 switch (isl_format_get_num_channels(fmt.fmt)) {
2464 case 0: comp[0] = VFCOMP_STORE_0;
2465 case 1: comp[1] = VFCOMP_STORE_0;
2466 case 2: comp[2] = VFCOMP_STORE_0;
2467 case 3:
2468 comp[3] = isl_format_has_int_channel(fmt.fmt) ? VFCOMP_STORE_1_INT
2469 : VFCOMP_STORE_1_FP;
2470 break;
2471 }
2472 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
2473 ve.VertexBufferIndex = state[i].vertex_buffer_index;
2474 ve.Valid = true;
2475 ve.SourceElementOffset = state[i].src_offset;
2476 ve.SourceElementFormat = fmt.fmt;
2477 ve.Component0Control = comp[0];
2478 ve.Component1Control = comp[1];
2479 ve.Component2Control = comp[2];
2480 ve.Component3Control = comp[3];
2481 }
2482
2483 iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
2484 vi.VertexElementIndex = i;
2485 vi.InstancingEnable = state[i].instance_divisor > 0;
2486 vi.InstanceDataStepRate = state[i].instance_divisor;
2487 }
2488
2489 ve_pack_dest += GENX(VERTEX_ELEMENT_STATE_length);
2490 vfi_pack_dest += GENX(3DSTATE_VF_INSTANCING_length);
2491 }
2492
2493 return cso;
2494 }
2495
2496 /**
2497 * The pipe->bind_vertex_elements_state() driver hook.
2498 */
2499 static void
2500 iris_bind_vertex_elements_state(struct pipe_context *ctx, void *state)
2501 {
2502 struct iris_context *ice = (struct iris_context *) ctx;
2503 struct iris_vertex_element_state *old_cso = ice->state.cso_vertex_elements;
2504 struct iris_vertex_element_state *new_cso = state;
2505
2506 /* 3DSTATE_VF_SGVs overrides the last VE, so if the count is changing,
2507 * we need to re-emit it to ensure we're overriding the right one.
2508 */
2509 if (new_cso && cso_changed(count))
2510 ice->state.dirty |= IRIS_DIRTY_VF_SGVS;
2511
2512 ice->state.cso_vertex_elements = state;
2513 ice->state.dirty |= IRIS_DIRTY_VERTEX_ELEMENTS;
2514 }
2515
2516 /**
2517 * Gallium CSO for stream output (transform feedback) targets.
2518 */
2519 struct iris_stream_output_target {
2520 struct pipe_stream_output_target base;
2521
2522 uint32_t so_buffer[GENX(3DSTATE_SO_BUFFER_length)];
2523
2524 /** Storage holding the offset where we're writing in the buffer */
2525 struct iris_state_ref offset;
2526 };
2527
2528 /**
2529 * The pipe->create_stream_output_target() driver hook.
2530 *
2531 * "Target" here refers to a destination buffer. We translate this into
2532 * a 3DSTATE_SO_BUFFER packet. We can handle most fields, but don't yet
2533 * know which buffer this represents, or whether we ought to zero the
2534 * write-offsets, or append. Those are handled in the set() hook.
2535 */
2536 static struct pipe_stream_output_target *
2537 iris_create_stream_output_target(struct pipe_context *ctx,
2538 struct pipe_resource *p_res,
2539 unsigned buffer_offset,
2540 unsigned buffer_size)
2541 {
2542 struct iris_resource *res = (void *) p_res;
2543 struct iris_stream_output_target *cso = calloc(1, sizeof(*cso));
2544 if (!cso)
2545 return NULL;
2546
2547 res->bind_history |= PIPE_BIND_STREAM_OUTPUT;
2548
2549 pipe_reference_init(&cso->base.reference, 1);
2550 pipe_resource_reference(&cso->base.buffer, p_res);
2551 cso->base.buffer_offset = buffer_offset;
2552 cso->base.buffer_size = buffer_size;
2553 cso->base.context = ctx;
2554
2555 upload_state(ctx->stream_uploader, &cso->offset, 4 * sizeof(uint32_t), 4);
2556
2557 iris_pack_command(GENX(3DSTATE_SO_BUFFER), cso->so_buffer, sob) {
2558 sob.SurfaceBaseAddress =
2559 rw_bo(NULL, res->bo->gtt_offset + buffer_offset);
2560 sob.SOBufferEnable = true;
2561 sob.StreamOffsetWriteEnable = true;
2562 sob.StreamOutputBufferOffsetAddressEnable = true;
2563 sob.MOCS = MOCS_WB; // XXX: MOCS
2564
2565 sob.SurfaceSize = MAX2(buffer_size / 4, 1) - 1;
2566
2567 /* .SOBufferIndex, .StreamOffset, and .StreamOutputBufferOffsetAddress
2568 * are filled in later when we have stream IDs.
2569 */
2570 }
2571
2572 return &cso->base;
2573 }
2574
2575 static void
2576 iris_stream_output_target_destroy(struct pipe_context *ctx,
2577 struct pipe_stream_output_target *state)
2578 {
2579 struct iris_stream_output_target *cso = (void *) state;
2580
2581 pipe_resource_reference(&cso->base.buffer, NULL);
2582 pipe_resource_reference(&cso->offset.res, NULL);
2583
2584 free(cso);
2585 }
2586
2587 /**
2588 * The pipe->set_stream_output_targets() driver hook.
2589 *
2590 * At this point, we know which targets are bound to a particular index,
2591 * and also whether we want to append or start over. We can finish the
2592 * 3DSTATE_SO_BUFFER packets we started earlier.
2593 */
2594 static void
2595 iris_set_stream_output_targets(struct pipe_context *ctx,
2596 unsigned num_targets,
2597 struct pipe_stream_output_target **targets,
2598 const unsigned *offsets)
2599 {
2600 struct iris_context *ice = (struct iris_context *) ctx;
2601 struct iris_genx_state *genx = ice->state.genx;
2602 uint32_t *so_buffers = genx->so_buffers;
2603
2604 const bool active = num_targets > 0;
2605 if (ice->state.streamout_active != active) {
2606 ice->state.streamout_active = active;
2607 ice->state.dirty |= IRIS_DIRTY_STREAMOUT;
2608
2609 /* We only emit 3DSTATE_SO_DECL_LIST when streamout is active, because
2610 * it's a non-pipelined command. If we're switching streamout on, we
2611 * may have missed emitting it earlier, so do so now. (We're already
2612 * taking a stall to update 3DSTATE_SO_BUFFERS anyway...)
2613 */
2614 if (active)
2615 ice->state.dirty |= IRIS_DIRTY_SO_DECL_LIST;
2616 }
2617
2618 for (int i = 0; i < 4; i++) {
2619 pipe_so_target_reference(&ice->state.so_target[i],
2620 i < num_targets ? targets[i] : NULL);
2621 }
2622
2623 /* No need to update 3DSTATE_SO_BUFFER unless SOL is active. */
2624 if (!active)
2625 return;
2626
2627 for (unsigned i = 0; i < 4; i++,
2628 so_buffers += GENX(3DSTATE_SO_BUFFER_length)) {
2629
2630 if (i >= num_targets || !targets[i]) {
2631 iris_pack_command(GENX(3DSTATE_SO_BUFFER), so_buffers, sob)
2632 sob.SOBufferIndex = i;
2633 continue;
2634 }
2635
2636 struct iris_stream_output_target *tgt = (void *) targets[i];
2637
2638 /* Note that offsets[i] will either be 0, causing us to zero
2639 * the value in the buffer, or 0xFFFFFFFF, which happens to mean
2640 * "continue appending at the existing offset."
2641 */
2642 assert(offsets[i] == 0 || offsets[i] == 0xFFFFFFFF);
2643
2644 uint32_t dynamic[GENX(3DSTATE_SO_BUFFER_length)];
2645 iris_pack_state(GENX(3DSTATE_SO_BUFFER), dynamic, dyns) {
2646 dyns.SOBufferIndex = i;
2647 dyns.StreamOffset = offsets[i];
2648 dyns.StreamOutputBufferOffsetAddress =
2649 rw_bo(NULL, iris_resource_bo(tgt->offset.res)->gtt_offset + tgt->offset.offset + i * sizeof(uint32_t));
2650 }
2651
2652 for (uint32_t j = 0; j < GENX(3DSTATE_SO_BUFFER_length); j++) {
2653 so_buffers[j] = tgt->so_buffer[j] | dynamic[j];
2654 }
2655 }
2656
2657 ice->state.dirty |= IRIS_DIRTY_SO_BUFFERS;
2658 }
2659
2660 /**
2661 * An iris-vtable helper for encoding the 3DSTATE_SO_DECL_LIST and
2662 * 3DSTATE_STREAMOUT packets.
2663 *
2664 * 3DSTATE_SO_DECL_LIST is a list of shader outputs we want the streamout
2665 * hardware to record. We can create it entirely based on the shader, with
2666 * no dynamic state dependencies.
2667 *
2668 * 3DSTATE_STREAMOUT is an annoying mix of shader-based information and
2669 * state-based settings. We capture the shader-related ones here, and merge
2670 * the rest in at draw time.
2671 */
2672 static uint32_t *
2673 iris_create_so_decl_list(const struct pipe_stream_output_info *info,
2674 const struct brw_vue_map *vue_map)
2675 {
2676 struct GENX(SO_DECL) so_decl[MAX_VERTEX_STREAMS][128];
2677 int buffer_mask[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
2678 int next_offset[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
2679 int decls[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
2680 int max_decls = 0;
2681 STATIC_ASSERT(ARRAY_SIZE(so_decl[0]) >= MAX_PROGRAM_OUTPUTS);
2682
2683 memset(so_decl, 0, sizeof(so_decl));
2684
2685 /* Construct the list of SO_DECLs to be emitted. The formatting of the
2686 * command feels strange -- each dword pair contains a SO_DECL per stream.
2687 */
2688 for (unsigned i = 0; i < info->num_outputs; i++) {
2689 const struct pipe_stream_output *output = &info->output[i];
2690 const int buffer = output->output_buffer;
2691 const int varying = output->register_index;
2692 const unsigned stream_id = output->stream;
2693 assert(stream_id < MAX_VERTEX_STREAMS);
2694
2695 buffer_mask[stream_id] |= 1 << buffer;
2696
2697 assert(vue_map->varying_to_slot[varying] >= 0);
2698
2699 /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
2700 * array. Instead, it simply increments DstOffset for the following
2701 * input by the number of components that should be skipped.
2702 *
2703 * Our hardware is unusual in that it requires us to program SO_DECLs
2704 * for fake "hole" components, rather than simply taking the offset
2705 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
2706 * program as many size = 4 holes as we can, then a final hole to
2707 * accommodate the final 1, 2, or 3 remaining.
2708 */
2709 int skip_components = output->dst_offset - next_offset[buffer];
2710
2711 while (skip_components > 0) {
2712 so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
2713 .HoleFlag = 1,
2714 .OutputBufferSlot = output->output_buffer,
2715 .ComponentMask = (1 << MIN2(skip_components, 4)) - 1,
2716 };
2717 skip_components -= 4;
2718 }
2719
2720 next_offset[buffer] = output->dst_offset + output->num_components;
2721
2722 so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
2723 .OutputBufferSlot = output->output_buffer,
2724 .RegisterIndex = vue_map->varying_to_slot[varying],
2725 .ComponentMask =
2726 ((1 << output->num_components) - 1) << output->start_component,
2727 };
2728
2729 if (decls[stream_id] > max_decls)
2730 max_decls = decls[stream_id];
2731 }
2732
2733 unsigned dwords = GENX(3DSTATE_STREAMOUT_length) + (3 + 2 * max_decls);
2734 uint32_t *map = ralloc_size(NULL, sizeof(uint32_t) * dwords);
2735 uint32_t *so_decl_map = map + GENX(3DSTATE_STREAMOUT_length);
2736
2737 iris_pack_command(GENX(3DSTATE_STREAMOUT), map, sol) {
2738 int urb_entry_read_offset = 0;
2739 int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
2740 urb_entry_read_offset;
2741
2742 /* We always read the whole vertex. This could be reduced at some
2743 * point by reading less and offsetting the register index in the
2744 * SO_DECLs.
2745 */
2746 sol.Stream0VertexReadOffset = urb_entry_read_offset;
2747 sol.Stream0VertexReadLength = urb_entry_read_length - 1;
2748 sol.Stream1VertexReadOffset = urb_entry_read_offset;
2749 sol.Stream1VertexReadLength = urb_entry_read_length - 1;
2750 sol.Stream2VertexReadOffset = urb_entry_read_offset;
2751 sol.Stream2VertexReadLength = urb_entry_read_length - 1;
2752 sol.Stream3VertexReadOffset = urb_entry_read_offset;
2753 sol.Stream3VertexReadLength = urb_entry_read_length - 1;
2754
2755 /* Set buffer pitches; 0 means unbound. */
2756 sol.Buffer0SurfacePitch = 4 * info->stride[0];
2757 sol.Buffer1SurfacePitch = 4 * info->stride[1];
2758 sol.Buffer2SurfacePitch = 4 * info->stride[2];
2759 sol.Buffer3SurfacePitch = 4 * info->stride[3];
2760 }
2761
2762 iris_pack_command(GENX(3DSTATE_SO_DECL_LIST), so_decl_map, list) {
2763 list.DWordLength = 3 + 2 * max_decls - 2;
2764 list.StreamtoBufferSelects0 = buffer_mask[0];
2765 list.StreamtoBufferSelects1 = buffer_mask[1];
2766 list.StreamtoBufferSelects2 = buffer_mask[2];
2767 list.StreamtoBufferSelects3 = buffer_mask[3];
2768 list.NumEntries0 = decls[0];
2769 list.NumEntries1 = decls[1];
2770 list.NumEntries2 = decls[2];
2771 list.NumEntries3 = decls[3];
2772 }
2773
2774 for (int i = 0; i < max_decls; i++) {
2775 iris_pack_state(GENX(SO_DECL_ENTRY), so_decl_map + 3 + i * 2, entry) {
2776 entry.Stream0Decl = so_decl[0][i];
2777 entry.Stream1Decl = so_decl[1][i];
2778 entry.Stream2Decl = so_decl[2][i];
2779 entry.Stream3Decl = so_decl[3][i];
2780 }
2781 }
2782
2783 return map;
2784 }
2785
2786 static void
2787 iris_compute_sbe_urb_read_interval(uint64_t fs_input_slots,
2788 const struct brw_vue_map *last_vue_map,
2789 bool two_sided_color,
2790 unsigned *out_offset,
2791 unsigned *out_length)
2792 {
2793 /* The compiler computes the first URB slot without considering COL/BFC
2794 * swizzling (because it doesn't know whether it's enabled), so we need
2795 * to do that here too. This may result in a smaller offset, which
2796 * should be safe.
2797 */
2798 const unsigned first_slot =
2799 brw_compute_first_urb_slot_required(fs_input_slots, last_vue_map);
2800
2801 /* This becomes the URB read offset (counted in pairs of slots). */
2802 assert(first_slot % 2 == 0);
2803 *out_offset = first_slot / 2;
2804
2805 /* We need to adjust the inputs read to account for front/back color
2806 * swizzling, as it can make the URB length longer.
2807 */
2808 for (int c = 0; c <= 1; c++) {
2809 if (fs_input_slots & (VARYING_BIT_COL0 << c)) {
2810 /* If two sided color is enabled, the fragment shader's gl_Color
2811 * (COL0) input comes from either the gl_FrontColor (COL0) or
2812 * gl_BackColor (BFC0) input varyings. Mark BFC as used, too.
2813 */
2814 if (two_sided_color)
2815 fs_input_slots |= (VARYING_BIT_BFC0 << c);
2816
2817 /* If front color isn't written, we opt to give them back color
2818 * instead of an undefined value. Switch from COL to BFC.
2819 */
2820 if (last_vue_map->varying_to_slot[VARYING_SLOT_COL0 + c] == -1) {
2821 fs_input_slots &= ~(VARYING_BIT_COL0 << c);
2822 fs_input_slots |= (VARYING_BIT_BFC0 << c);
2823 }
2824 }
2825 }
2826
2827 /* Compute the minimum URB Read Length necessary for the FS inputs.
2828 *
2829 * From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
2830 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
2831 *
2832 * "This field should be set to the minimum length required to read the
2833 * maximum source attribute. The maximum source attribute is indicated
2834 * by the maximum value of the enabled Attribute # Source Attribute if
2835 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
2836 * enable is not set.
2837 * read_length = ceiling((max_source_attr + 1) / 2)
2838 *
2839 * [errata] Corruption/Hang possible if length programmed larger than
2840 * recommended"
2841 *
2842 * Similar text exists for Ivy Bridge.
2843 *
2844 * We find the last URB slot that's actually read by the FS.
2845 */
2846 unsigned last_read_slot = last_vue_map->num_slots - 1;
2847 while (last_read_slot > first_slot && !(fs_input_slots &
2848 (1ull << last_vue_map->slot_to_varying[last_read_slot])))
2849 --last_read_slot;
2850
2851 /* The URB read length is the difference of the two, counted in pairs. */
2852 *out_length = DIV_ROUND_UP(last_read_slot - first_slot + 1, 2);
2853 }
2854
2855 static void
2856 iris_emit_sbe_swiz(struct iris_batch *batch,
2857 const struct iris_context *ice,
2858 unsigned urb_read_offset,
2859 unsigned sprite_coord_enables)
2860 {
2861 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) attr_overrides[16] = {};
2862 const struct brw_wm_prog_data *wm_prog_data = (void *)
2863 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2864 const struct brw_vue_map *vue_map = ice->shaders.last_vue_map;
2865 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2866
2867 /* XXX: this should be generated when putting programs in place */
2868
2869 // XXX: raster->sprite_coord_enable
2870
2871 for (int fs_attr = 0; fs_attr < VARYING_SLOT_MAX; fs_attr++) {
2872 const int input_index = wm_prog_data->urb_setup[fs_attr];
2873 if (input_index < 0 || input_index >= 16)
2874 continue;
2875
2876 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) *attr =
2877 &attr_overrides[input_index];
2878 int slot = vue_map->varying_to_slot[fs_attr];
2879
2880 /* Viewport and Layer are stored in the VUE header. We need to override
2881 * them to zero if earlier stages didn't write them, as GL requires that
2882 * they read back as zero when not explicitly set.
2883 */
2884 switch (fs_attr) {
2885 case VARYING_SLOT_VIEWPORT:
2886 case VARYING_SLOT_LAYER:
2887 attr->ComponentOverrideX = true;
2888 attr->ComponentOverrideW = true;
2889 attr->ConstantSource = CONST_0000;
2890
2891 if (!(vue_map->slots_valid & VARYING_BIT_LAYER))
2892 attr->ComponentOverrideY = true;
2893 if (!(vue_map->slots_valid & VARYING_BIT_VIEWPORT))
2894 attr->ComponentOverrideZ = true;
2895 continue;
2896
2897 case VARYING_SLOT_PRIMITIVE_ID:
2898 /* Override if the previous shader stage didn't write gl_PrimitiveID. */
2899 if (slot == -1) {
2900 attr->ComponentOverrideX = true;
2901 attr->ComponentOverrideY = true;
2902 attr->ComponentOverrideZ = true;
2903 attr->ComponentOverrideW = true;
2904 attr->ConstantSource = PRIM_ID;
2905 continue;
2906 }
2907
2908 default:
2909 break;
2910 }
2911
2912 if (sprite_coord_enables & (1 << input_index))
2913 continue;
2914
2915 /* If there was only a back color written but not front, use back
2916 * as the color instead of undefined.
2917 */
2918 if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
2919 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
2920 if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
2921 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
2922
2923 /* Not written by the previous stage - undefined. */
2924 if (slot == -1) {
2925 attr->ComponentOverrideX = true;
2926 attr->ComponentOverrideY = true;
2927 attr->ComponentOverrideZ = true;
2928 attr->ComponentOverrideW = true;
2929 attr->ConstantSource = CONST_0001_FLOAT;
2930 continue;
2931 }
2932
2933 /* Compute the location of the attribute relative to the read offset,
2934 * which is counted in 256-bit increments (two 128-bit VUE slots).
2935 */
2936 const int source_attr = slot - 2 * urb_read_offset;
2937 assert(source_attr >= 0 && source_attr <= 32);
2938 attr->SourceAttribute = source_attr;
2939
2940 /* If we are doing two-sided color, and the VUE slot following this one
2941 * represents a back-facing color, then we need to instruct the SF unit
2942 * to do back-facing swizzling.
2943 */
2944 if (cso_rast->light_twoside &&
2945 ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
2946 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
2947 (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
2948 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1)))
2949 attr->SwizzleSelect = INPUTATTR_FACING;
2950 }
2951
2952 iris_emit_cmd(batch, GENX(3DSTATE_SBE_SWIZ), sbes) {
2953 for (int i = 0; i < 16; i++)
2954 sbes.Attribute[i] = attr_overrides[i];
2955 }
2956 }
2957
2958 static unsigned
2959 iris_calculate_point_sprite_overrides(const struct brw_wm_prog_data *prog_data,
2960 const struct iris_rasterizer_state *cso)
2961 {
2962 unsigned overrides = 0;
2963
2964 if (prog_data->urb_setup[VARYING_SLOT_PNTC] != -1)
2965 overrides |= 1 << prog_data->urb_setup[VARYING_SLOT_PNTC];
2966
2967 for (int i = 0; i < 8; i++) {
2968 if ((cso->sprite_coord_enable & (1 << i)) &&
2969 prog_data->urb_setup[VARYING_SLOT_TEX0 + i] != -1)
2970 overrides |= 1 << prog_data->urb_setup[VARYING_SLOT_TEX0 + i];
2971 }
2972
2973 return overrides;
2974 }
2975
2976 static void
2977 iris_emit_sbe(struct iris_batch *batch, const struct iris_context *ice)
2978 {
2979 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2980 const struct brw_wm_prog_data *wm_prog_data = (void *)
2981 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2982 const struct shader_info *fs_info =
2983 iris_get_shader_info(ice, MESA_SHADER_FRAGMENT);
2984
2985 unsigned urb_read_offset, urb_read_length;
2986 iris_compute_sbe_urb_read_interval(fs_info->inputs_read,
2987 ice->shaders.last_vue_map,
2988 cso_rast->light_twoside,
2989 &urb_read_offset, &urb_read_length);
2990
2991 unsigned sprite_coord_overrides =
2992 iris_calculate_point_sprite_overrides(wm_prog_data, cso_rast);
2993
2994 iris_emit_cmd(batch, GENX(3DSTATE_SBE), sbe) {
2995 sbe.AttributeSwizzleEnable = true;
2996 sbe.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
2997 sbe.PointSpriteTextureCoordinateOrigin = cso_rast->sprite_coord_mode;
2998 sbe.VertexURBEntryReadOffset = urb_read_offset;
2999 sbe.VertexURBEntryReadLength = urb_read_length;
3000 sbe.ForceVertexURBEntryReadOffset = true;
3001 sbe.ForceVertexURBEntryReadLength = true;
3002 sbe.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
3003 sbe.PointSpriteTextureCoordinateEnable = sprite_coord_overrides;
3004
3005 for (int i = 0; i < 32; i++) {
3006 sbe.AttributeActiveComponentFormat[i] = ACTIVE_COMPONENT_XYZW;
3007 }
3008 }
3009
3010 iris_emit_sbe_swiz(batch, ice, urb_read_offset, sprite_coord_overrides);
3011 }
3012
3013 /* ------------------------------------------------------------------- */
3014
3015 /**
3016 * Populate VS program key fields based on the current state.
3017 */
3018 static void
3019 iris_populate_vs_key(const struct iris_context *ice,
3020 const struct shader_info *info,
3021 struct brw_vs_prog_key *key)
3022 {
3023 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
3024
3025 if (info->clip_distance_array_size == 0 &&
3026 (info->outputs_written & (VARYING_BIT_POS | VARYING_BIT_CLIP_VERTEX)))
3027 key->nr_userclip_plane_consts = cso_rast->num_clip_plane_consts;
3028 }
3029
3030 /**
3031 * Populate TCS program key fields based on the current state.
3032 */
3033 static void
3034 iris_populate_tcs_key(const struct iris_context *ice,
3035 struct brw_tcs_prog_key *key)
3036 {
3037 }
3038
3039 /**
3040 * Populate TES program key fields based on the current state.
3041 */
3042 static void
3043 iris_populate_tes_key(const struct iris_context *ice,
3044 struct brw_tes_prog_key *key)
3045 {
3046 }
3047
3048 /**
3049 * Populate GS program key fields based on the current state.
3050 */
3051 static void
3052 iris_populate_gs_key(const struct iris_context *ice,
3053 struct brw_gs_prog_key *key)
3054 {
3055 }
3056
3057 /**
3058 * Populate FS program key fields based on the current state.
3059 */
3060 static void
3061 iris_populate_fs_key(const struct iris_context *ice,
3062 struct brw_wm_prog_key *key)
3063 {
3064 /* XXX: dirty flags? */
3065 const struct pipe_framebuffer_state *fb = &ice->state.framebuffer;
3066 const struct iris_depth_stencil_alpha_state *zsa = ice->state.cso_zsa;
3067 const struct iris_rasterizer_state *rast = ice->state.cso_rast;
3068 const struct iris_blend_state *blend = ice->state.cso_blend;
3069
3070 key->nr_color_regions = fb->nr_cbufs;
3071
3072 key->clamp_fragment_color = rast->clamp_fragment_color;
3073
3074 key->replicate_alpha = fb->nr_cbufs > 1 &&
3075 (zsa->alpha.enabled || blend->alpha_to_coverage);
3076
3077 /* XXX: only bother if COL0/1 are read */
3078 key->flat_shade = rast->flatshade;
3079
3080 key->persample_interp = rast->force_persample_interp;
3081 key->multisample_fbo = rast->multisample && fb->samples > 1;
3082
3083 key->coherent_fb_fetch = true;
3084
3085 // XXX: uint64_t input_slots_valid; - for >16 inputs
3086
3087 // XXX: key->force_dual_color_blend for unigine
3088 // XXX: respect hint for high_quality_derivatives:1;
3089 }
3090
3091 static void
3092 iris_populate_cs_key(const struct iris_context *ice,
3093 struct brw_cs_prog_key *key)
3094 {
3095 }
3096
3097 #if 0
3098 // XXX: these need to go in INIT_THREAD_DISPATCH_FIELDS
3099 pkt.SamplerCount = \
3100 DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4); \
3101
3102 #endif
3103
3104 static uint64_t
3105 KSP(const struct iris_compiled_shader *shader)
3106 {
3107 struct iris_resource *res = (void *) shader->assembly.res;
3108 return iris_bo_offset_from_base_address(res->bo) + shader->assembly.offset;
3109 }
3110
3111 // Gen11 workaround table #2056 WABTPPrefetchDisable suggests to disable
3112 // prefetching of binding tables in A0 and B0 steppings. XXX: Revisit
3113 // this WA on C0 stepping.
3114
3115 #define INIT_THREAD_DISPATCH_FIELDS(pkt, prefix, stage) \
3116 pkt.KernelStartPointer = KSP(shader); \
3117 pkt.BindingTableEntryCount = GEN_GEN == 11 ? 0 : \
3118 prog_data->binding_table.size_bytes / 4; \
3119 pkt.FloatingPointMode = prog_data->use_alt_mode; \
3120 \
3121 pkt.DispatchGRFStartRegisterForURBData = \
3122 prog_data->dispatch_grf_start_reg; \
3123 pkt.prefix##URBEntryReadLength = vue_prog_data->urb_read_length; \
3124 pkt.prefix##URBEntryReadOffset = 0; \
3125 \
3126 pkt.StatisticsEnable = true; \
3127 pkt.Enable = true; \
3128 \
3129 if (prog_data->total_scratch) { \
3130 uint32_t scratch_addr = \
3131 iris_get_scratch_space(ice, prog_data->total_scratch, stage); \
3132 pkt.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11; \
3133 pkt.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr); \
3134 }
3135
3136 /**
3137 * Encode most of 3DSTATE_VS based on the compiled shader.
3138 */
3139 static void
3140 iris_store_vs_state(struct iris_context *ice,
3141 const struct gen_device_info *devinfo,
3142 struct iris_compiled_shader *shader)
3143 {
3144 struct brw_stage_prog_data *prog_data = shader->prog_data;
3145 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
3146
3147 iris_pack_command(GENX(3DSTATE_VS), shader->derived_data, vs) {
3148 INIT_THREAD_DISPATCH_FIELDS(vs, Vertex, MESA_SHADER_VERTEX);
3149 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
3150 vs.SIMD8DispatchEnable = true;
3151 vs.UserClipDistanceCullTestEnableBitmask =
3152 vue_prog_data->cull_distance_mask;
3153 }
3154 }
3155
3156 /**
3157 * Encode most of 3DSTATE_HS based on the compiled shader.
3158 */
3159 static void
3160 iris_store_tcs_state(struct iris_context *ice,
3161 const struct gen_device_info *devinfo,
3162 struct iris_compiled_shader *shader)
3163 {
3164 struct brw_stage_prog_data *prog_data = shader->prog_data;
3165 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
3166 struct brw_tcs_prog_data *tcs_prog_data = (void *) prog_data;
3167
3168 iris_pack_command(GENX(3DSTATE_HS), shader->derived_data, hs) {
3169 INIT_THREAD_DISPATCH_FIELDS(hs, Vertex, MESA_SHADER_TESS_CTRL);
3170
3171 hs.InstanceCount = tcs_prog_data->instances - 1;
3172 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
3173 hs.IncludeVertexHandles = true;
3174 }
3175 }
3176
3177 /**
3178 * Encode 3DSTATE_TE and most of 3DSTATE_DS based on the compiled shader.
3179 */
3180 static void
3181 iris_store_tes_state(struct iris_context *ice,
3182 const struct gen_device_info *devinfo,
3183 struct iris_compiled_shader *shader)
3184 {
3185 struct brw_stage_prog_data *prog_data = shader->prog_data;
3186 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
3187 struct brw_tes_prog_data *tes_prog_data = (void *) prog_data;
3188
3189 uint32_t *te_state = (void *) shader->derived_data;
3190 uint32_t *ds_state = te_state + GENX(3DSTATE_TE_length);
3191
3192 iris_pack_command(GENX(3DSTATE_TE), te_state, te) {
3193 te.Partitioning = tes_prog_data->partitioning;
3194 te.OutputTopology = tes_prog_data->output_topology;
3195 te.TEDomain = tes_prog_data->domain;
3196 te.TEEnable = true;
3197 te.MaximumTessellationFactorOdd = 63.0;
3198 te.MaximumTessellationFactorNotOdd = 64.0;
3199 }
3200
3201 iris_pack_command(GENX(3DSTATE_DS), ds_state, ds) {
3202 INIT_THREAD_DISPATCH_FIELDS(ds, Patch, MESA_SHADER_TESS_EVAL);
3203
3204 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
3205 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
3206 ds.ComputeWCoordinateEnable =
3207 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
3208
3209 ds.UserClipDistanceCullTestEnableBitmask =
3210 vue_prog_data->cull_distance_mask;
3211 }
3212
3213 }
3214
3215 /**
3216 * Encode most of 3DSTATE_GS based on the compiled shader.
3217 */
3218 static void
3219 iris_store_gs_state(struct iris_context *ice,
3220 const struct gen_device_info *devinfo,
3221 struct iris_compiled_shader *shader)
3222 {
3223 struct brw_stage_prog_data *prog_data = shader->prog_data;
3224 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
3225 struct brw_gs_prog_data *gs_prog_data = (void *) prog_data;
3226
3227 iris_pack_command(GENX(3DSTATE_GS), shader->derived_data, gs) {
3228 INIT_THREAD_DISPATCH_FIELDS(gs, Vertex, MESA_SHADER_GEOMETRY);
3229
3230 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
3231 gs.OutputTopology = gs_prog_data->output_topology;
3232 gs.ControlDataHeaderSize =
3233 gs_prog_data->control_data_header_size_hwords;
3234 gs.InstanceControl = gs_prog_data->invocations - 1;
3235 gs.DispatchMode = DISPATCH_MODE_SIMD8;
3236 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
3237 gs.ControlDataFormat = gs_prog_data->control_data_format;
3238 gs.ReorderMode = TRAILING;
3239 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
3240 gs.MaximumNumberofThreads =
3241 GEN_GEN == 8 ? (devinfo->max_gs_threads / 2 - 1)
3242 : (devinfo->max_gs_threads - 1);
3243
3244 if (gs_prog_data->static_vertex_count != -1) {
3245 gs.StaticOutput = true;
3246 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count;
3247 }
3248 gs.IncludeVertexHandles = vue_prog_data->include_vue_handles;
3249
3250 gs.UserClipDistanceCullTestEnableBitmask =
3251 vue_prog_data->cull_distance_mask;
3252
3253 const int urb_entry_write_offset = 1;
3254 const uint32_t urb_entry_output_length =
3255 DIV_ROUND_UP(vue_prog_data->vue_map.num_slots, 2) -
3256 urb_entry_write_offset;
3257
3258 gs.VertexURBEntryOutputReadOffset = urb_entry_write_offset;
3259 gs.VertexURBEntryOutputLength = MAX2(urb_entry_output_length, 1);
3260 }
3261 }
3262
3263 /**
3264 * Encode most of 3DSTATE_PS and 3DSTATE_PS_EXTRA based on the shader.
3265 */
3266 static void
3267 iris_store_fs_state(struct iris_context *ice,
3268 const struct gen_device_info *devinfo,
3269 struct iris_compiled_shader *shader)
3270 {
3271 struct brw_stage_prog_data *prog_data = shader->prog_data;
3272 struct brw_wm_prog_data *wm_prog_data = (void *) shader->prog_data;
3273
3274 uint32_t *ps_state = (void *) shader->derived_data;
3275 uint32_t *psx_state = ps_state + GENX(3DSTATE_PS_length);
3276
3277 iris_pack_command(GENX(3DSTATE_PS), ps_state, ps) {
3278 ps.VectorMaskEnable = true;
3279 //ps.SamplerCount = ...
3280 // XXX: WABTPPrefetchDisable, see above, drop at C0
3281 ps.BindingTableEntryCount = GEN_GEN == 11 ? 0 :
3282 prog_data->binding_table.size_bytes / 4;
3283 ps.FloatingPointMode = prog_data->use_alt_mode;
3284 ps.MaximumNumberofThreadsPerPSD = 64 - (GEN_GEN == 8 ? 2 : 1);
3285
3286 ps.PushConstantEnable = shader->num_system_values > 0 ||
3287 prog_data->ubo_ranges[0].length > 0;
3288
3289 /* From the documentation for this packet:
3290 * "If the PS kernel does not need the Position XY Offsets to
3291 * compute a Position Value, then this field should be programmed
3292 * to POSOFFSET_NONE."
3293 *
3294 * "SW Recommendation: If the PS kernel needs the Position Offsets
3295 * to compute a Position XY value, this field should match Position
3296 * ZW Interpolation Mode to ensure a consistent position.xyzw
3297 * computation."
3298 *
3299 * We only require XY sample offsets. So, this recommendation doesn't
3300 * look useful at the moment. We might need this in future.
3301 */
3302 ps.PositionXYOffsetSelect =
3303 wm_prog_data->uses_pos_offset ? POSOFFSET_SAMPLE : POSOFFSET_NONE;
3304 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
3305 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
3306 ps._32PixelDispatchEnable = wm_prog_data->dispatch_32;
3307
3308 // XXX: Disable SIMD32 with 16x MSAA
3309
3310 ps.DispatchGRFStartRegisterForConstantSetupData0 =
3311 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 0);
3312 ps.DispatchGRFStartRegisterForConstantSetupData1 =
3313 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 1);
3314 ps.DispatchGRFStartRegisterForConstantSetupData2 =
3315 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 2);
3316
3317 ps.KernelStartPointer0 =
3318 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 0);
3319 ps.KernelStartPointer1 =
3320 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 1);
3321 ps.KernelStartPointer2 =
3322 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
3323
3324 if (prog_data->total_scratch) {
3325 uint32_t scratch_addr =
3326 iris_get_scratch_space(ice, prog_data->total_scratch,
3327 MESA_SHADER_FRAGMENT);
3328 ps.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11;
3329 ps.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr);
3330 }
3331 }
3332
3333 iris_pack_command(GENX(3DSTATE_PS_EXTRA), psx_state, psx) {
3334 psx.PixelShaderValid = true;
3335 psx.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
3336 psx.PixelShaderKillsPixel = wm_prog_data->uses_kill;
3337 psx.AttributeEnable = wm_prog_data->num_varying_inputs != 0;
3338 psx.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
3339 psx.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
3340 psx.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
3341
3342 if (wm_prog_data->uses_sample_mask) {
3343 /* TODO: conservative rasterization */
3344 if (wm_prog_data->post_depth_coverage)
3345 psx.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
3346 else
3347 psx.InputCoverageMaskState = ICMS_NORMAL;
3348 }
3349
3350 psx.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
3351 psx.PixelShaderPullsBary = wm_prog_data->pulls_bary;
3352 psx.PixelShaderComputesStencil = wm_prog_data->computed_stencil;
3353
3354 // XXX: UAV bit
3355 }
3356 }
3357
3358 /**
3359 * Compute the size of the derived data (shader command packets).
3360 *
3361 * This must match the data written by the iris_store_xs_state() functions.
3362 */
3363 static void
3364 iris_store_cs_state(struct iris_context *ice,
3365 const struct gen_device_info *devinfo,
3366 struct iris_compiled_shader *shader)
3367 {
3368 struct brw_stage_prog_data *prog_data = shader->prog_data;
3369 struct brw_cs_prog_data *cs_prog_data = (void *) shader->prog_data;
3370 void *map = shader->derived_data;
3371
3372 iris_pack_state(GENX(INTERFACE_DESCRIPTOR_DATA), map, desc) {
3373 desc.KernelStartPointer = KSP(shader);
3374 desc.ConstantURBEntryReadLength = cs_prog_data->push.per_thread.regs;
3375 desc.NumberofThreadsinGPGPUThreadGroup = cs_prog_data->threads;
3376 desc.SharedLocalMemorySize =
3377 encode_slm_size(GEN_GEN, prog_data->total_shared);
3378 desc.BarrierEnable = cs_prog_data->uses_barrier;
3379 desc.CrossThreadConstantDataReadLength =
3380 cs_prog_data->push.cross_thread.regs;
3381 }
3382 }
3383
3384 static unsigned
3385 iris_derived_program_state_size(enum iris_program_cache_id cache_id)
3386 {
3387 assert(cache_id <= IRIS_CACHE_BLORP);
3388
3389 static const unsigned dwords[] = {
3390 [IRIS_CACHE_VS] = GENX(3DSTATE_VS_length),
3391 [IRIS_CACHE_TCS] = GENX(3DSTATE_HS_length),
3392 [IRIS_CACHE_TES] = GENX(3DSTATE_TE_length) + GENX(3DSTATE_DS_length),
3393 [IRIS_CACHE_GS] = GENX(3DSTATE_GS_length),
3394 [IRIS_CACHE_FS] =
3395 GENX(3DSTATE_PS_length) + GENX(3DSTATE_PS_EXTRA_length),
3396 [IRIS_CACHE_CS] = GENX(INTERFACE_DESCRIPTOR_DATA_length),
3397 [IRIS_CACHE_BLORP] = 0,
3398 };
3399
3400 return sizeof(uint32_t) * dwords[cache_id];
3401 }
3402
3403 /**
3404 * Create any state packets corresponding to the given shader stage
3405 * (i.e. 3DSTATE_VS) and save them as "derived data" in the shader variant.
3406 * This means that we can look up a program in the in-memory cache and
3407 * get most of the state packet without having to reconstruct it.
3408 */
3409 static void
3410 iris_store_derived_program_state(struct iris_context *ice,
3411 enum iris_program_cache_id cache_id,
3412 struct iris_compiled_shader *shader)
3413 {
3414 struct iris_screen *screen = (void *) ice->ctx.screen;
3415 const struct gen_device_info *devinfo = &screen->devinfo;
3416
3417 switch (cache_id) {
3418 case IRIS_CACHE_VS:
3419 iris_store_vs_state(ice, devinfo, shader);
3420 break;
3421 case IRIS_CACHE_TCS:
3422 iris_store_tcs_state(ice, devinfo, shader);
3423 break;
3424 case IRIS_CACHE_TES:
3425 iris_store_tes_state(ice, devinfo, shader);
3426 break;
3427 case IRIS_CACHE_GS:
3428 iris_store_gs_state(ice, devinfo, shader);
3429 break;
3430 case IRIS_CACHE_FS:
3431 iris_store_fs_state(ice, devinfo, shader);
3432 break;
3433 case IRIS_CACHE_CS:
3434 iris_store_cs_state(ice, devinfo, shader);
3435 case IRIS_CACHE_BLORP:
3436 break;
3437 default:
3438 break;
3439 }
3440 }
3441
3442 /* ------------------------------------------------------------------- */
3443
3444 /**
3445 * Configure the URB.
3446 *
3447 * XXX: write a real comment.
3448 */
3449 static void
3450 iris_upload_urb_config(struct iris_context *ice, struct iris_batch *batch)
3451 {
3452 const struct gen_device_info *devinfo = &batch->screen->devinfo;
3453 const unsigned push_size_kB = 32;
3454 unsigned entries[4];
3455 unsigned start[4];
3456 unsigned size[4];
3457
3458 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
3459 if (!ice->shaders.prog[i]) {
3460 size[i] = 1;
3461 } else {
3462 struct brw_vue_prog_data *vue_prog_data =
3463 (void *) ice->shaders.prog[i]->prog_data;
3464 size[i] = vue_prog_data->urb_entry_size;
3465 }
3466 assert(size[i] != 0);
3467 }
3468
3469 gen_get_urb_config(devinfo, 1024 * push_size_kB,
3470 1024 * ice->shaders.urb_size,
3471 ice->shaders.prog[MESA_SHADER_TESS_EVAL] != NULL,
3472 ice->shaders.prog[MESA_SHADER_GEOMETRY] != NULL,
3473 size, entries, start);
3474
3475 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
3476 iris_emit_cmd(batch, GENX(3DSTATE_URB_VS), urb) {
3477 urb._3DCommandSubOpcode += i;
3478 urb.VSURBStartingAddress = start[i];
3479 urb.VSURBEntryAllocationSize = size[i] - 1;
3480 urb.VSNumberofURBEntries = entries[i];
3481 }
3482 }
3483 }
3484
3485 static const uint32_t push_constant_opcodes[] = {
3486 [MESA_SHADER_VERTEX] = 21,
3487 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
3488 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
3489 [MESA_SHADER_GEOMETRY] = 22,
3490 [MESA_SHADER_FRAGMENT] = 23,
3491 [MESA_SHADER_COMPUTE] = 0,
3492 };
3493
3494 static uint32_t
3495 use_null_surface(struct iris_batch *batch, struct iris_context *ice)
3496 {
3497 struct iris_bo *state_bo = iris_resource_bo(ice->state.unbound_tex.res);
3498
3499 iris_use_pinned_bo(batch, state_bo, false);
3500
3501 return ice->state.unbound_tex.offset;
3502 }
3503
3504 static uint32_t
3505 use_null_fb_surface(struct iris_batch *batch, struct iris_context *ice)
3506 {
3507 /* If set_framebuffer_state() was never called, fall back to 1x1x1 */
3508 if (!ice->state.null_fb.res)
3509 return use_null_surface(batch, ice);
3510
3511 struct iris_bo *state_bo = iris_resource_bo(ice->state.null_fb.res);
3512
3513 iris_use_pinned_bo(batch, state_bo, false);
3514
3515 return ice->state.null_fb.offset;
3516 }
3517
3518 /**
3519 * Add a surface to the validation list, as well as the buffer containing
3520 * the corresponding SURFACE_STATE.
3521 *
3522 * Returns the binding table entry (offset to SURFACE_STATE).
3523 */
3524 static uint32_t
3525 use_surface(struct iris_batch *batch,
3526 struct pipe_surface *p_surf,
3527 bool writeable)
3528 {
3529 struct iris_surface *surf = (void *) p_surf;
3530
3531 iris_use_pinned_bo(batch, iris_resource_bo(p_surf->texture), writeable);
3532 iris_use_pinned_bo(batch, iris_resource_bo(surf->surface_state.res), false);
3533
3534 return surf->surface_state.offset;
3535 }
3536
3537 static uint32_t
3538 use_sampler_view(struct iris_batch *batch, struct iris_sampler_view *isv)
3539 {
3540 iris_use_pinned_bo(batch, isv->res->bo, false);
3541 iris_use_pinned_bo(batch, iris_resource_bo(isv->surface_state.res), false);
3542
3543 return isv->surface_state.offset;
3544 }
3545
3546 static uint32_t
3547 use_const_buffer(struct iris_batch *batch,
3548 struct iris_context *ice,
3549 struct iris_const_buffer *cbuf)
3550 {
3551 if (!cbuf->surface_state.res)
3552 return use_null_surface(batch, ice);
3553
3554 iris_use_pinned_bo(batch, iris_resource_bo(cbuf->data.res), false);
3555 iris_use_pinned_bo(batch, iris_resource_bo(cbuf->surface_state.res), false);
3556
3557 return cbuf->surface_state.offset;
3558 }
3559
3560 static uint32_t
3561 use_ssbo(struct iris_batch *batch, struct iris_context *ice,
3562 struct iris_shader_state *shs, int i)
3563 {
3564 if (!shs->ssbo[i])
3565 return use_null_surface(batch, ice);
3566
3567 struct iris_state_ref *surf_state = &shs->ssbo_surface_state[i];
3568
3569 iris_use_pinned_bo(batch, iris_resource_bo(shs->ssbo[i]), true);
3570 iris_use_pinned_bo(batch, iris_resource_bo(surf_state->res), false);
3571
3572 return surf_state->offset;
3573 }
3574
3575 static uint32_t
3576 use_image(struct iris_batch *batch, struct iris_context *ice,
3577 struct iris_shader_state *shs, int i)
3578 {
3579 if (!shs->image[i].res)
3580 return use_null_surface(batch, ice);
3581
3582 struct iris_state_ref *surf_state = &shs->image[i].surface_state;
3583
3584 iris_use_pinned_bo(batch, iris_resource_bo(shs->image[i].res),
3585 shs->image[i].access & PIPE_IMAGE_ACCESS_WRITE);
3586 iris_use_pinned_bo(batch, iris_resource_bo(surf_state->res), false);
3587
3588 return surf_state->offset;
3589 }
3590
3591 #define push_bt_entry(addr) \
3592 assert(addr >= binder_addr); \
3593 assert(s < prog_data->binding_table.size_bytes / sizeof(uint32_t)); \
3594 if (!pin_only) bt_map[s++] = (addr) - binder_addr;
3595
3596 #define bt_assert(section, exists) \
3597 if (!pin_only) assert(prog_data->binding_table.section == \
3598 (exists) ? s : 0xd0d0d0d0)
3599
3600 /**
3601 * Populate the binding table for a given shader stage.
3602 *
3603 * This fills out the table of pointers to surfaces required by the shader,
3604 * and also adds those buffers to the validation list so the kernel can make
3605 * resident before running our batch.
3606 */
3607 static void
3608 iris_populate_binding_table(struct iris_context *ice,
3609 struct iris_batch *batch,
3610 gl_shader_stage stage,
3611 bool pin_only)
3612 {
3613 const struct iris_binder *binder = &ice->state.binder;
3614 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3615 if (!shader)
3616 return;
3617
3618 UNUSED struct brw_stage_prog_data *prog_data = shader->prog_data;
3619 struct iris_shader_state *shs = &ice->state.shaders[stage];
3620 uint32_t binder_addr = binder->bo->gtt_offset;
3621
3622 //struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
3623 uint32_t *bt_map = binder->map + binder->bt_offset[stage];
3624 int s = 0;
3625
3626 const struct shader_info *info = iris_get_shader_info(ice, stage);
3627 if (!info) {
3628 /* TCS passthrough doesn't need a binding table. */
3629 assert(stage == MESA_SHADER_TESS_CTRL);
3630 return;
3631 }
3632
3633 if (stage == MESA_SHADER_COMPUTE) {
3634 /* surface for gl_NumWorkGroups */
3635 struct iris_state_ref *grid_data = &ice->state.grid_size;
3636 struct iris_state_ref *grid_state = &ice->state.grid_surf_state;
3637 iris_use_pinned_bo(batch, iris_resource_bo(grid_data->res), false);
3638 iris_use_pinned_bo(batch, iris_resource_bo(grid_state->res), false);
3639 push_bt_entry(grid_state->offset);
3640 }
3641
3642 if (stage == MESA_SHADER_FRAGMENT) {
3643 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
3644 /* Note that cso_fb->nr_cbufs == fs_key->nr_color_regions. */
3645 if (cso_fb->nr_cbufs) {
3646 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
3647 uint32_t addr =
3648 cso_fb->cbufs[i] ? use_surface(batch, cso_fb->cbufs[i], true)
3649 : use_null_fb_surface(batch, ice);
3650 push_bt_entry(addr);
3651 }
3652 } else {
3653 uint32_t addr = use_null_fb_surface(batch, ice);
3654 push_bt_entry(addr);
3655 }
3656 }
3657
3658 bt_assert(texture_start, info->num_textures > 0);
3659
3660 for (int i = 0; i < info->num_textures; i++) {
3661 struct iris_sampler_view *view = shs->textures[i];
3662 uint32_t addr = view ? use_sampler_view(batch, view)
3663 : use_null_surface(batch, ice);
3664 push_bt_entry(addr);
3665 }
3666
3667 bt_assert(image_start, info->num_images > 0);
3668
3669 for (int i = 0; i < info->num_images; i++) {
3670 uint32_t addr = use_image(batch, ice, shs, i);
3671 push_bt_entry(addr);
3672 }
3673
3674 const int num_ubos = iris_get_shader_num_ubos(ice, stage);
3675
3676 bt_assert(ubo_start, num_ubos > 0);
3677
3678 for (int i = 0; i < num_ubos; i++) {
3679 uint32_t addr = use_const_buffer(batch, ice, &shs->constbuf[i]);
3680 push_bt_entry(addr);
3681 }
3682
3683 bt_assert(ssbo_start, info->num_abos + info->num_ssbos > 0);
3684
3685 /* XXX: st is wasting 16 binding table slots for ABOs. Should add a cap
3686 * for changing nir_lower_atomics_to_ssbos setting and buffer_base offset
3687 * in st_atom_storagebuf.c so it'll compact them into one range, with
3688 * SSBOs starting at info->num_abos. Ideally it'd reset num_abos to 0 too
3689 */
3690 if (info->num_abos + info->num_ssbos > 0) {
3691 for (int i = 0; i < IRIS_MAX_ABOS + info->num_ssbos; i++) {
3692 uint32_t addr = use_ssbo(batch, ice, shs, i);
3693 push_bt_entry(addr);
3694 }
3695 }
3696
3697 #if 0
3698 // XXX: not implemented yet
3699 bt_assert(plane_start[1], ...);
3700 bt_assert(plane_start[2], ...);
3701 #endif
3702 }
3703
3704 static void
3705 iris_use_optional_res(struct iris_batch *batch,
3706 struct pipe_resource *res,
3707 bool writeable)
3708 {
3709 if (res) {
3710 struct iris_bo *bo = iris_resource_bo(res);
3711 iris_use_pinned_bo(batch, bo, writeable);
3712 }
3713 }
3714
3715 /* ------------------------------------------------------------------- */
3716
3717 /**
3718 * Pin any BOs which were installed by a previous batch, and restored
3719 * via the hardware logical context mechanism.
3720 *
3721 * We don't need to re-emit all state every batch - the hardware context
3722 * mechanism will save and restore it for us. This includes pointers to
3723 * various BOs...which won't exist unless we ask the kernel to pin them
3724 * by adding them to the validation list.
3725 *
3726 * We can skip buffers if we've re-emitted those packets, as we're
3727 * overwriting those stale pointers with new ones, and don't actually
3728 * refer to the old BOs.
3729 */
3730 static void
3731 iris_restore_render_saved_bos(struct iris_context *ice,
3732 struct iris_batch *batch,
3733 const struct pipe_draw_info *draw)
3734 {
3735 // XXX: whack IRIS_SHADER_DIRTY_BINDING_TABLE on new batch
3736
3737 const uint64_t clean = ~ice->state.dirty;
3738
3739 if (clean & IRIS_DIRTY_CC_VIEWPORT) {
3740 iris_use_optional_res(batch, ice->state.last_res.cc_vp, false);
3741 }
3742
3743 if (clean & IRIS_DIRTY_SF_CL_VIEWPORT) {
3744 iris_use_optional_res(batch, ice->state.last_res.sf_cl_vp, false);
3745 }
3746
3747 if (clean & IRIS_DIRTY_BLEND_STATE) {
3748 iris_use_optional_res(batch, ice->state.last_res.blend, false);
3749 }
3750
3751 if (clean & IRIS_DIRTY_COLOR_CALC_STATE) {
3752 iris_use_optional_res(batch, ice->state.last_res.color_calc, false);
3753 }
3754
3755 if (clean & IRIS_DIRTY_SCISSOR_RECT) {
3756 iris_use_optional_res(batch, ice->state.last_res.scissor, false);
3757 }
3758
3759 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3760 if (!(clean & (IRIS_DIRTY_CONSTANTS_VS << stage)))
3761 continue;
3762
3763 struct iris_shader_state *shs = &ice->state.shaders[stage];
3764 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3765
3766 if (!shader)
3767 continue;
3768
3769 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
3770
3771 for (int i = 0; i < 4; i++) {
3772 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
3773
3774 if (range->length == 0)
3775 continue;
3776
3777 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
3778 struct iris_resource *res = (void *) cbuf->data.res;
3779
3780 if (res)
3781 iris_use_pinned_bo(batch, res->bo, false);
3782 else
3783 iris_use_pinned_bo(batch, batch->screen->workaround_bo, false);
3784 }
3785 }
3786
3787 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3788 if (clean & (IRIS_DIRTY_BINDINGS_VS << stage)) {
3789 /* Re-pin any buffers referred to by the binding table. */
3790 iris_populate_binding_table(ice, batch, stage, true);
3791 }
3792 }
3793
3794 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3795 struct iris_shader_state *shs = &ice->state.shaders[stage];
3796 struct pipe_resource *res = shs->sampler_table.res;
3797 if (res)
3798 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
3799 }
3800
3801 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3802 if (clean & (IRIS_DIRTY_VS << stage)) {
3803 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3804 if (shader) {
3805 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
3806 iris_use_pinned_bo(batch, bo, false);
3807 }
3808
3809 // XXX: scratch buffer
3810 }
3811 }
3812
3813 if (clean & IRIS_DIRTY_DEPTH_BUFFER) {
3814 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
3815
3816 if (cso_fb->zsbuf) {
3817 struct iris_resource *zres, *sres;
3818 iris_get_depth_stencil_resources(cso_fb->zsbuf->texture,
3819 &zres, &sres);
3820 // XXX: might not be writable...
3821 if (zres)
3822 iris_use_pinned_bo(batch, zres->bo, true);
3823 if (sres)
3824 iris_use_pinned_bo(batch, sres->bo, true);
3825 }
3826 }
3827
3828 if (draw->index_size == 0 && ice->state.last_res.index_buffer) {
3829 /* This draw didn't emit a new index buffer, so we are inheriting the
3830 * older index buffer. This draw didn't need it, but future ones may.
3831 */
3832 struct iris_bo *bo = iris_resource_bo(ice->state.last_res.index_buffer);
3833 iris_use_pinned_bo(batch, bo, false);
3834 }
3835
3836 if (clean & IRIS_DIRTY_VERTEX_BUFFERS) {
3837 struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
3838 for (unsigned i = 0; i < cso->num_buffers; i++) {
3839 struct iris_resource *res = (void *) cso->resources[i];
3840 iris_use_pinned_bo(batch, res->bo, false);
3841 }
3842 }
3843 }
3844
3845 static void
3846 iris_restore_compute_saved_bos(struct iris_context *ice,
3847 struct iris_batch *batch,
3848 const struct pipe_grid_info *grid)
3849 {
3850 const uint64_t clean = ~ice->state.dirty;
3851
3852 const int stage = MESA_SHADER_COMPUTE;
3853 struct iris_shader_state *shs = &ice->state.shaders[stage];
3854
3855 if (clean & IRIS_DIRTY_CONSTANTS_CS) {
3856 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3857
3858 if (shader) {
3859 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
3860 const struct brw_ubo_range *range = &prog_data->ubo_ranges[0];
3861
3862 if (range->length > 0) {
3863 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
3864 struct iris_resource *res = (void *) cbuf->data.res;
3865
3866 if (res)
3867 iris_use_pinned_bo(batch, res->bo, false);
3868 else
3869 iris_use_pinned_bo(batch, batch->screen->workaround_bo, false);
3870 }
3871 }
3872 }
3873
3874 if (clean & IRIS_DIRTY_BINDINGS_CS) {
3875 /* Re-pin any buffers referred to by the binding table. */
3876 iris_populate_binding_table(ice, batch, stage, true);
3877 }
3878
3879 struct pipe_resource *sampler_res = shs->sampler_table.res;
3880 if (sampler_res)
3881 iris_use_pinned_bo(batch, iris_resource_bo(sampler_res), false);
3882
3883 if (clean & IRIS_DIRTY_CS) {
3884 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
3885 if (shader) {
3886 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
3887 iris_use_pinned_bo(batch, bo, false);
3888 }
3889
3890 // XXX: scratch buffer
3891 }
3892 }
3893
3894 /**
3895 * Possibly emit STATE_BASE_ADDRESS to update Surface State Base Address.
3896 */
3897 static void
3898 iris_update_surface_base_address(struct iris_batch *batch,
3899 struct iris_binder *binder)
3900 {
3901 if (batch->last_surface_base_address == binder->bo->gtt_offset)
3902 return;
3903
3904 flush_for_state_base_change(batch);
3905
3906 iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
3907 // XXX: sba.SurfaceStateMemoryObjectControlState = MOCS_WB;
3908 sba.SurfaceStateBaseAddressModifyEnable = true;
3909 sba.SurfaceStateBaseAddress = ro_bo(binder->bo, 0);
3910 }
3911
3912 batch->last_surface_base_address = binder->bo->gtt_offset;
3913 }
3914
3915 static void
3916 iris_upload_dirty_render_state(struct iris_context *ice,
3917 struct iris_batch *batch,
3918 const struct pipe_draw_info *draw)
3919 {
3920 const uint64_t dirty = ice->state.dirty;
3921
3922 if (!(dirty & IRIS_ALL_DIRTY_FOR_RENDER))
3923 return;
3924
3925 struct iris_genx_state *genx = ice->state.genx;
3926 struct iris_binder *binder = &ice->state.binder;
3927 struct brw_wm_prog_data *wm_prog_data = (void *)
3928 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
3929
3930 if (dirty & IRIS_DIRTY_CC_VIEWPORT) {
3931 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
3932 uint32_t cc_vp_address;
3933
3934 /* XXX: could avoid streaming for depth_clip [0,1] case. */
3935 uint32_t *cc_vp_map =
3936 stream_state(batch, ice->state.dynamic_uploader,
3937 &ice->state.last_res.cc_vp,
3938 4 * ice->state.num_viewports *
3939 GENX(CC_VIEWPORT_length), 32, &cc_vp_address);
3940 for (int i = 0; i < ice->state.num_viewports; i++) {
3941 float zmin, zmax;
3942 util_viewport_zmin_zmax(&ice->state.viewports[i],
3943 cso_rast->clip_halfz, &zmin, &zmax);
3944 if (cso_rast->depth_clip_near)
3945 zmin = 0.0;
3946 if (cso_rast->depth_clip_far)
3947 zmax = 1.0;
3948
3949 iris_pack_state(GENX(CC_VIEWPORT), cc_vp_map, ccv) {
3950 ccv.MinimumDepth = zmin;
3951 ccv.MaximumDepth = zmax;
3952 }
3953
3954 cc_vp_map += GENX(CC_VIEWPORT_length);
3955 }
3956
3957 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), ptr) {
3958 ptr.CCViewportPointer = cc_vp_address;
3959 }
3960 }
3961
3962 if (dirty & IRIS_DIRTY_SF_CL_VIEWPORT) {
3963 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
3964 ptr.SFClipViewportPointer =
3965 emit_state(batch, ice->state.dynamic_uploader,
3966 &ice->state.last_res.sf_cl_vp,
3967 genx->sf_cl_vp, 4 * GENX(SF_CLIP_VIEWPORT_length) *
3968 ice->state.num_viewports, 64);
3969 }
3970 }
3971
3972 /* XXX: L3 State */
3973
3974 // XXX: this is only flagged at setup, we assume a static configuration
3975 if (dirty & IRIS_DIRTY_URB) {
3976 iris_upload_urb_config(ice, batch);
3977 }
3978
3979 if (dirty & IRIS_DIRTY_BLEND_STATE) {
3980 struct iris_blend_state *cso_blend = ice->state.cso_blend;
3981 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
3982 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
3983 const int header_dwords = GENX(BLEND_STATE_length);
3984 const int rt_dwords = cso_fb->nr_cbufs * GENX(BLEND_STATE_ENTRY_length);
3985 uint32_t blend_offset;
3986 uint32_t *blend_map =
3987 stream_state(batch, ice->state.dynamic_uploader,
3988 &ice->state.last_res.blend,
3989 4 * (header_dwords + rt_dwords), 64, &blend_offset);
3990
3991 uint32_t blend_state_header;
3992 iris_pack_state(GENX(BLEND_STATE), &blend_state_header, bs) {
3993 bs.AlphaTestEnable = cso_zsa->alpha.enabled;
3994 bs.AlphaTestFunction = translate_compare_func(cso_zsa->alpha.func);
3995 }
3996
3997 blend_map[0] = blend_state_header | cso_blend->blend_state[0];
3998 memcpy(&blend_map[1], &cso_blend->blend_state[1], 4 * rt_dwords);
3999
4000 iris_emit_cmd(batch, GENX(3DSTATE_BLEND_STATE_POINTERS), ptr) {
4001 ptr.BlendStatePointer = blend_offset;
4002 ptr.BlendStatePointerValid = true;
4003 }
4004 }
4005
4006 if (dirty & IRIS_DIRTY_COLOR_CALC_STATE) {
4007 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
4008 uint32_t cc_offset;
4009 void *cc_map =
4010 stream_state(batch, ice->state.dynamic_uploader,
4011 &ice->state.last_res.color_calc,
4012 sizeof(uint32_t) * GENX(COLOR_CALC_STATE_length),
4013 64, &cc_offset);
4014 iris_pack_state(GENX(COLOR_CALC_STATE), cc_map, cc) {
4015 cc.AlphaTestFormat = ALPHATEST_FLOAT32;
4016 cc.AlphaReferenceValueAsFLOAT32 = cso->alpha.ref_value;
4017 cc.BlendConstantColorRed = ice->state.blend_color.color[0];
4018 cc.BlendConstantColorGreen = ice->state.blend_color.color[1];
4019 cc.BlendConstantColorBlue = ice->state.blend_color.color[2];
4020 cc.BlendConstantColorAlpha = ice->state.blend_color.color[3];
4021 }
4022 iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
4023 ptr.ColorCalcStatePointer = cc_offset;
4024 ptr.ColorCalcStatePointerValid = true;
4025 }
4026 }
4027
4028 /* Upload constants for TCS passthrough. */
4029 if ((dirty & IRIS_DIRTY_CONSTANTS_TCS) &&
4030 ice->shaders.prog[MESA_SHADER_TESS_CTRL] &&
4031 !ice->shaders.uncompiled[MESA_SHADER_TESS_CTRL]) {
4032 struct iris_compiled_shader *tes_shader = ice->shaders.prog[MESA_SHADER_TESS_EVAL];
4033 assert(tes_shader);
4034
4035 /* Passthrough always copies 2 vec4s, so when uploading data we ensure
4036 * it is in the right layout for TES.
4037 */
4038 float hdr[8] = {};
4039 struct brw_tes_prog_data *tes_prog_data = (void *) tes_shader->prog_data;
4040 switch (tes_prog_data->domain) {
4041 case BRW_TESS_DOMAIN_QUAD:
4042 for (int i = 0; i < 4; i++)
4043 hdr[7 - i] = ice->state.default_outer_level[i];
4044 hdr[3] = ice->state.default_inner_level[0];
4045 hdr[2] = ice->state.default_inner_level[1];
4046 break;
4047 case BRW_TESS_DOMAIN_TRI:
4048 for (int i = 0; i < 3; i++)
4049 hdr[7 - i] = ice->state.default_outer_level[i];
4050 hdr[4] = ice->state.default_inner_level[0];
4051 break;
4052 case BRW_TESS_DOMAIN_ISOLINE:
4053 hdr[7] = ice->state.default_outer_level[1];
4054 hdr[6] = ice->state.default_outer_level[0];
4055 break;
4056 }
4057
4058 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_TESS_CTRL];
4059 struct iris_const_buffer *cbuf = &shs->constbuf[0];
4060 u_upload_data(ice->ctx.const_uploader, 0, sizeof(hdr), 32,
4061 &hdr[0], &cbuf->data.offset,
4062 &cbuf->data.res);
4063 }
4064
4065 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4066 if (!(dirty & (IRIS_DIRTY_CONSTANTS_VS << stage)))
4067 continue;
4068
4069 struct iris_shader_state *shs = &ice->state.shaders[stage];
4070 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
4071
4072 if (!shader)
4073 continue;
4074
4075 if (shs->cbuf0_needs_upload)
4076 upload_uniforms(ice, stage);
4077
4078 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
4079
4080 iris_emit_cmd(batch, GENX(3DSTATE_CONSTANT_VS), pkt) {
4081 pkt._3DCommandSubOpcode = push_constant_opcodes[stage];
4082 if (prog_data) {
4083 /* The Skylake PRM contains the following restriction:
4084 *
4085 * "The driver must ensure The following case does not occur
4086 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
4087 * buffer 3 read length equal to zero committed followed by a
4088 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
4089 * zero committed."
4090 *
4091 * To avoid this, we program the buffers in the highest slots.
4092 * This way, slot 0 is only used if slot 3 is also used.
4093 */
4094 int n = 3;
4095
4096 for (int i = 3; i >= 0; i--) {
4097 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
4098
4099 if (range->length == 0)
4100 continue;
4101
4102 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
4103 struct iris_resource *res = (void *) cbuf->data.res;
4104
4105 assert(cbuf->data.offset % 32 == 0);
4106
4107 pkt.ConstantBody.ReadLength[n] = range->length;
4108 pkt.ConstantBody.Buffer[n] =
4109 res ? ro_bo(res->bo, range->start * 32 + cbuf->data.offset)
4110 : ro_bo(batch->screen->workaround_bo, 0);
4111 n--;
4112 }
4113 }
4114 }
4115 }
4116
4117 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4118 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
4119 iris_emit_cmd(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), ptr) {
4120 ptr._3DCommandSubOpcode = 38 + stage;
4121 ptr.PointertoVSBindingTable = binder->bt_offset[stage];
4122 }
4123 }
4124 }
4125
4126 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4127 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
4128 iris_populate_binding_table(ice, batch, stage, false);
4129 }
4130 }
4131
4132 if (ice->state.need_border_colors)
4133 iris_use_pinned_bo(batch, ice->state.border_color_pool.bo, false);
4134
4135 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4136 if (!(dirty & (IRIS_DIRTY_SAMPLER_STATES_VS << stage)) ||
4137 !ice->shaders.prog[stage])
4138 continue;
4139
4140 struct iris_shader_state *shs = &ice->state.shaders[stage];
4141 struct pipe_resource *res = shs->sampler_table.res;
4142 if (res)
4143 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
4144
4145 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ptr) {
4146 ptr._3DCommandSubOpcode = 43 + stage;
4147 ptr.PointertoVSSamplerState = shs->sampler_table.offset;
4148 }
4149 }
4150
4151 if (dirty & IRIS_DIRTY_MULTISAMPLE) {
4152 iris_emit_cmd(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
4153 ms.PixelLocation =
4154 ice->state.cso_rast->half_pixel_center ? CENTER : UL_CORNER;
4155 if (ice->state.framebuffer.samples > 0)
4156 ms.NumberofMultisamples = ffs(ice->state.framebuffer.samples) - 1;
4157 }
4158 }
4159
4160 if (dirty & IRIS_DIRTY_SAMPLE_MASK) {
4161 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_MASK), ms) {
4162 ms.SampleMask = MAX2(ice->state.sample_mask, 1);
4163 }
4164 }
4165
4166 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
4167 if (!(dirty & (IRIS_DIRTY_VS << stage)))
4168 continue;
4169
4170 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
4171
4172 if (shader) {
4173 struct iris_resource *cache = (void *) shader->assembly.res;
4174 iris_use_pinned_bo(batch, cache->bo, false);
4175 iris_batch_emit(batch, shader->derived_data,
4176 iris_derived_program_state_size(stage));
4177 } else {
4178 if (stage == MESA_SHADER_TESS_EVAL) {
4179 iris_emit_cmd(batch, GENX(3DSTATE_HS), hs);
4180 iris_emit_cmd(batch, GENX(3DSTATE_TE), te);
4181 iris_emit_cmd(batch, GENX(3DSTATE_DS), ds);
4182 } else if (stage == MESA_SHADER_GEOMETRY) {
4183 iris_emit_cmd(batch, GENX(3DSTATE_GS), gs);
4184 }
4185 }
4186 }
4187
4188 if (ice->state.streamout_active) {
4189 if (dirty & IRIS_DIRTY_SO_BUFFERS) {
4190 iris_batch_emit(batch, genx->so_buffers,
4191 4 * 4 * GENX(3DSTATE_SO_BUFFER_length));
4192 for (int i = 0; i < 4; i++) {
4193 struct iris_stream_output_target *tgt =
4194 (void *) ice->state.so_target[i];
4195 if (tgt) {
4196 iris_use_pinned_bo(batch, iris_resource_bo(tgt->base.buffer),
4197 true);
4198 iris_use_pinned_bo(batch, iris_resource_bo(tgt->offset.res),
4199 true);
4200 }
4201 }
4202 }
4203
4204 if ((dirty & IRIS_DIRTY_SO_DECL_LIST) && ice->state.streamout) {
4205 uint32_t *decl_list =
4206 ice->state.streamout + GENX(3DSTATE_STREAMOUT_length);
4207 iris_batch_emit(batch, decl_list, 4 * ((decl_list[0] & 0xff) + 2));
4208 }
4209
4210 if (dirty & IRIS_DIRTY_STREAMOUT) {
4211 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
4212
4213 uint32_t dynamic_sol[GENX(3DSTATE_STREAMOUT_length)];
4214 iris_pack_command(GENX(3DSTATE_STREAMOUT), dynamic_sol, sol) {
4215 sol.SOFunctionEnable = true;
4216 sol.SOStatisticsEnable = true;
4217
4218 sol.RenderingDisable = cso_rast->rasterizer_discard &&
4219 !ice->state.prims_generated_query_active;
4220 sol.ReorderMode = cso_rast->flatshade_first ? LEADING : TRAILING;
4221 }
4222
4223 assert(ice->state.streamout);
4224
4225 iris_emit_merge(batch, ice->state.streamout, dynamic_sol,
4226 GENX(3DSTATE_STREAMOUT_length));
4227 }
4228 } else {
4229 if (dirty & IRIS_DIRTY_STREAMOUT) {
4230 iris_emit_cmd(batch, GENX(3DSTATE_STREAMOUT), sol);
4231 }
4232 }
4233
4234 if (dirty & IRIS_DIRTY_CLIP) {
4235 struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
4236 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
4237
4238 uint32_t dynamic_clip[GENX(3DSTATE_CLIP_length)];
4239 iris_pack_command(GENX(3DSTATE_CLIP), &dynamic_clip, cl) {
4240 if (wm_prog_data->barycentric_interp_modes &
4241 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS)
4242 cl.NonPerspectiveBarycentricEnable = true;
4243
4244 cl.ForceZeroRTAIndexEnable = cso_fb->layers == 0;
4245 cl.MaximumVPIndex = ice->state.num_viewports - 1;
4246 }
4247 iris_emit_merge(batch, cso_rast->clip, dynamic_clip,
4248 ARRAY_SIZE(cso_rast->clip));
4249 }
4250
4251 if (dirty & IRIS_DIRTY_RASTER) {
4252 struct iris_rasterizer_state *cso = ice->state.cso_rast;
4253 iris_batch_emit(batch, cso->raster, sizeof(cso->raster));
4254 iris_batch_emit(batch, cso->sf, sizeof(cso->sf));
4255
4256 }
4257
4258 /* XXX: FS program updates needs to flag IRIS_DIRTY_WM */
4259 if (dirty & IRIS_DIRTY_WM) {
4260 struct iris_rasterizer_state *cso = ice->state.cso_rast;
4261 uint32_t dynamic_wm[GENX(3DSTATE_WM_length)];
4262
4263 iris_pack_command(GENX(3DSTATE_WM), &dynamic_wm, wm) {
4264 wm.StatisticsEnable = ice->state.statistics_counters_enabled;
4265
4266 wm.BarycentricInterpolationMode =
4267 wm_prog_data->barycentric_interp_modes;
4268
4269 if (wm_prog_data->early_fragment_tests)
4270 wm.EarlyDepthStencilControl = EDSC_PREPS;
4271 else if (wm_prog_data->has_side_effects)
4272 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
4273 }
4274 iris_emit_merge(batch, cso->wm, dynamic_wm, ARRAY_SIZE(cso->wm));
4275 }
4276
4277 if (dirty & IRIS_DIRTY_SBE) {
4278 iris_emit_sbe(batch, ice);
4279 }
4280
4281 if (dirty & IRIS_DIRTY_PS_BLEND) {
4282 struct iris_blend_state *cso_blend = ice->state.cso_blend;
4283 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
4284 uint32_t dynamic_pb[GENX(3DSTATE_PS_BLEND_length)];
4285 iris_pack_command(GENX(3DSTATE_PS_BLEND), &dynamic_pb, pb) {
4286 pb.HasWriteableRT = true; // XXX: comes from somewhere :(
4287 pb.AlphaTestEnable = cso_zsa->alpha.enabled;
4288 }
4289
4290 iris_emit_merge(batch, cso_blend->ps_blend, dynamic_pb,
4291 ARRAY_SIZE(cso_blend->ps_blend));
4292 }
4293
4294 if (dirty & IRIS_DIRTY_WM_DEPTH_STENCIL) {
4295 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
4296 struct pipe_stencil_ref *p_stencil_refs = &ice->state.stencil_ref;
4297
4298 uint32_t stencil_refs[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
4299 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), &stencil_refs, wmds) {
4300 wmds.StencilReferenceValue = p_stencil_refs->ref_value[0];
4301 wmds.BackfaceStencilReferenceValue = p_stencil_refs->ref_value[1];
4302 }
4303 iris_emit_merge(batch, cso->wmds, stencil_refs, ARRAY_SIZE(cso->wmds));
4304 }
4305
4306 if (dirty & IRIS_DIRTY_SCISSOR_RECT) {
4307 uint32_t scissor_offset =
4308 emit_state(batch, ice->state.dynamic_uploader,
4309 &ice->state.last_res.scissor,
4310 ice->state.scissors,
4311 sizeof(struct pipe_scissor_state) *
4312 ice->state.num_viewports, 32);
4313
4314 iris_emit_cmd(batch, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
4315 ptr.ScissorRectPointer = scissor_offset;
4316 }
4317 }
4318
4319 if (dirty & IRIS_DIRTY_DEPTH_BUFFER) {
4320 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
4321 struct iris_depth_buffer_state *cso_z = &ice->state.genx->depth_buffer;
4322
4323 iris_batch_emit(batch, cso_z->packets, sizeof(cso_z->packets));
4324
4325 if (cso_fb->zsbuf) {
4326 struct iris_resource *zres, *sres;
4327 iris_get_depth_stencil_resources(cso_fb->zsbuf->texture,
4328 &zres, &sres);
4329 // XXX: might not be writable...
4330 if (zres)
4331 iris_use_pinned_bo(batch, zres->bo, true);
4332 if (sres)
4333 iris_use_pinned_bo(batch, sres->bo, true);
4334 }
4335 }
4336
4337 if (dirty & IRIS_DIRTY_POLYGON_STIPPLE) {
4338 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
4339 for (int i = 0; i < 32; i++) {
4340 poly.PatternRow[i] = ice->state.poly_stipple.stipple[i];
4341 }
4342 }
4343 }
4344
4345 if (dirty & IRIS_DIRTY_LINE_STIPPLE) {
4346 struct iris_rasterizer_state *cso = ice->state.cso_rast;
4347 iris_batch_emit(batch, cso->line_stipple, sizeof(cso->line_stipple));
4348 }
4349
4350 if (dirty & IRIS_DIRTY_VF_TOPOLOGY) {
4351 iris_emit_cmd(batch, GENX(3DSTATE_VF_TOPOLOGY), topo) {
4352 topo.PrimitiveTopologyType =
4353 translate_prim_type(draw->mode, draw->vertices_per_patch);
4354 }
4355 }
4356
4357 if (dirty & IRIS_DIRTY_VERTEX_BUFFERS) {
4358 struct iris_vertex_buffer_state *cso = &ice->state.genx->vertex_buffers;
4359 const unsigned vb_dwords = GENX(VERTEX_BUFFER_STATE_length);
4360
4361 if (cso->num_buffers > 0) {
4362 /* The VF cache designers cut corners, and made the cache key's
4363 * <VertexBufferIndex, Memory Address> tuple only consider the bottom
4364 * 32 bits of the address. If you have two vertex buffers which get
4365 * placed exactly 4 GiB apart and use them in back-to-back draw calls,
4366 * you can get collisions (even within a single batch).
4367 *
4368 * So, we need to do a VF cache invalidate if the buffer for a VB
4369 * slot slot changes [48:32] address bits from the previous time.
4370 */
4371 bool need_invalidate = false;
4372
4373 for (unsigned i = 0; i < cso->num_buffers; i++) {
4374 uint16_t high_bits = 0;
4375
4376 struct iris_resource *res = (void *) cso->resources[i];
4377 if (res) {
4378 iris_use_pinned_bo(batch, res->bo, false);
4379
4380 high_bits = res->bo->gtt_offset >> 32ull;
4381 if (high_bits != ice->state.last_vbo_high_bits[i]) {
4382 need_invalidate = true;
4383 ice->state.last_vbo_high_bits[i] = high_bits;
4384 }
4385 }
4386 }
4387
4388 if (need_invalidate) {
4389 iris_emit_pipe_control_flush(batch,
4390 PIPE_CONTROL_VF_CACHE_INVALIDATE);
4391 }
4392
4393 iris_batch_emit(batch, cso->vertex_buffers, sizeof(uint32_t) *
4394 (1 + vb_dwords * cso->num_buffers));
4395 }
4396 }
4397
4398 if (dirty & IRIS_DIRTY_VERTEX_ELEMENTS) {
4399 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
4400 const unsigned entries = MAX2(cso->count, 1);
4401 iris_batch_emit(batch, cso->vertex_elements, sizeof(uint32_t) *
4402 (1 + entries * GENX(VERTEX_ELEMENT_STATE_length)));
4403 iris_batch_emit(batch, cso->vf_instancing, sizeof(uint32_t) *
4404 entries * GENX(3DSTATE_VF_INSTANCING_length));
4405 }
4406
4407 if (dirty & IRIS_DIRTY_VF_SGVS) {
4408 const struct brw_vs_prog_data *vs_prog_data = (void *)
4409 ice->shaders.prog[MESA_SHADER_VERTEX]->prog_data;
4410 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
4411
4412 iris_emit_cmd(batch, GENX(3DSTATE_VF_SGVS), sgv) {
4413 if (vs_prog_data->uses_vertexid) {
4414 sgv.VertexIDEnable = true;
4415 sgv.VertexIDComponentNumber = 2;
4416 sgv.VertexIDElementOffset = cso->count;
4417 }
4418
4419 if (vs_prog_data->uses_instanceid) {
4420 sgv.InstanceIDEnable = true;
4421 sgv.InstanceIDComponentNumber = 3;
4422 sgv.InstanceIDElementOffset = cso->count;
4423 }
4424 }
4425 }
4426
4427 if (dirty & IRIS_DIRTY_VF) {
4428 iris_emit_cmd(batch, GENX(3DSTATE_VF), vf) {
4429 if (draw->primitive_restart) {
4430 vf.IndexedDrawCutIndexEnable = true;
4431 vf.CutIndex = draw->restart_index;
4432 }
4433 }
4434 }
4435
4436 // XXX: Gen8 - PMA fix
4437 }
4438
4439 static void
4440 iris_upload_render_state(struct iris_context *ice,
4441 struct iris_batch *batch,
4442 const struct pipe_draw_info *draw)
4443 {
4444 /* Always pin the binder. If we're emitting new binding table pointers,
4445 * we need it. If not, we're probably inheriting old tables via the
4446 * context, and need it anyway. Since true zero-bindings cases are
4447 * practically non-existent, just pin it and avoid last_res tracking.
4448 */
4449 iris_use_pinned_bo(batch, ice->state.binder.bo, false);
4450
4451 if (!batch->contains_draw) {
4452 iris_restore_render_saved_bos(ice, batch, draw);
4453 batch->contains_draw = true;
4454 }
4455
4456 iris_upload_dirty_render_state(ice, batch, draw);
4457
4458 if (draw->index_size > 0) {
4459 unsigned offset;
4460
4461 if (draw->has_user_indices) {
4462 u_upload_data(ice->ctx.stream_uploader, 0,
4463 draw->count * draw->index_size, 4, draw->index.user,
4464 &offset, &ice->state.last_res.index_buffer);
4465 } else {
4466 struct iris_resource *res = (void *) draw->index.resource;
4467 res->bind_history |= PIPE_BIND_INDEX_BUFFER;
4468
4469 pipe_resource_reference(&ice->state.last_res.index_buffer,
4470 draw->index.resource);
4471 offset = 0;
4472 }
4473
4474 struct iris_bo *bo = iris_resource_bo(ice->state.last_res.index_buffer);
4475
4476 iris_emit_cmd(batch, GENX(3DSTATE_INDEX_BUFFER), ib) {
4477 ib.IndexFormat = draw->index_size >> 1;
4478 ib.MOCS = MOCS_WB;
4479 ib.BufferSize = bo->size;
4480 ib.BufferStartingAddress = ro_bo(bo, offset);
4481 }
4482
4483 /* The VF cache key only uses 32-bits, see vertex buffer comment above */
4484 uint16_t high_bits = bo->gtt_offset >> 32ull;
4485 if (high_bits != ice->state.last_index_bo_high_bits) {
4486 iris_emit_pipe_control_flush(batch, PIPE_CONTROL_VF_CACHE_INVALIDATE);
4487 ice->state.last_index_bo_high_bits = high_bits;
4488 }
4489 }
4490
4491 #define _3DPRIM_END_OFFSET 0x2420
4492 #define _3DPRIM_START_VERTEX 0x2430
4493 #define _3DPRIM_VERTEX_COUNT 0x2434
4494 #define _3DPRIM_INSTANCE_COUNT 0x2438
4495 #define _3DPRIM_START_INSTANCE 0x243C
4496 #define _3DPRIM_BASE_VERTEX 0x2440
4497
4498 if (draw->indirect) {
4499 /* We don't support this MultidrawIndirect. */
4500 assert(!draw->indirect->indirect_draw_count);
4501
4502 struct iris_bo *bo = iris_resource_bo(draw->indirect->buffer);
4503 assert(bo);
4504
4505 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4506 lrm.RegisterAddress = _3DPRIM_VERTEX_COUNT;
4507 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 0);
4508 }
4509 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4510 lrm.RegisterAddress = _3DPRIM_INSTANCE_COUNT;
4511 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 4);
4512 }
4513 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4514 lrm.RegisterAddress = _3DPRIM_START_VERTEX;
4515 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 8);
4516 }
4517 if (draw->index_size) {
4518 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4519 lrm.RegisterAddress = _3DPRIM_BASE_VERTEX;
4520 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 12);
4521 }
4522 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4523 lrm.RegisterAddress = _3DPRIM_START_INSTANCE;
4524 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 16);
4525 }
4526 } else {
4527 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4528 lrm.RegisterAddress = _3DPRIM_START_INSTANCE;
4529 lrm.MemoryAddress = ro_bo(bo, draw->indirect->offset + 12);
4530 }
4531 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_IMM), lri) {
4532 lri.RegisterOffset = _3DPRIM_BASE_VERTEX;
4533 lri.DataDWord = 0;
4534 }
4535 }
4536 }
4537
4538 iris_emit_cmd(batch, GENX(3DPRIMITIVE), prim) {
4539 prim.StartInstanceLocation = draw->start_instance;
4540 prim.InstanceCount = draw->instance_count;
4541 prim.VertexCountPerInstance = draw->count;
4542 prim.VertexAccessType = draw->index_size > 0 ? RANDOM : SEQUENTIAL;
4543 prim.PredicateEnable =
4544 ice->state.predicate == IRIS_PREDICATE_STATE_USE_BIT;
4545
4546 // XXX: this is probably bonkers.
4547 prim.StartVertexLocation = draw->start;
4548
4549 prim.IndirectParameterEnable = draw->indirect != NULL;
4550
4551 if (draw->index_size) {
4552 prim.BaseVertexLocation += draw->index_bias;
4553 } else {
4554 prim.StartVertexLocation += draw->index_bias;
4555 }
4556
4557 //prim.BaseVertexLocation = ...;
4558 }
4559 }
4560
4561 static void
4562 iris_upload_compute_state(struct iris_context *ice,
4563 struct iris_batch *batch,
4564 const struct pipe_grid_info *grid)
4565 {
4566 const uint64_t dirty = ice->state.dirty;
4567 struct iris_screen *screen = batch->screen;
4568 const struct gen_device_info *devinfo = &screen->devinfo;
4569 struct iris_binder *binder = &ice->state.binder;
4570 struct iris_shader_state *shs = &ice->state.shaders[MESA_SHADER_COMPUTE];
4571 struct iris_compiled_shader *shader =
4572 ice->shaders.prog[MESA_SHADER_COMPUTE];
4573 struct brw_stage_prog_data *prog_data = shader->prog_data;
4574 struct brw_cs_prog_data *cs_prog_data = (void *) prog_data;
4575
4576 if ((dirty & IRIS_DIRTY_CONSTANTS_CS) && shs->cbuf0_needs_upload)
4577 upload_uniforms(ice, MESA_SHADER_COMPUTE);
4578
4579 if (dirty & IRIS_DIRTY_BINDINGS_CS)
4580 iris_populate_binding_table(ice, batch, MESA_SHADER_COMPUTE, false);
4581
4582 iris_use_optional_res(batch, shs->sampler_table.res, false);
4583 iris_use_pinned_bo(batch, iris_resource_bo(shader->assembly.res), false);
4584
4585 if (ice->state.need_border_colors)
4586 iris_use_pinned_bo(batch, ice->state.border_color_pool.bo, false);
4587
4588 if (dirty & IRIS_DIRTY_CS) {
4589 /* The MEDIA_VFE_STATE documentation for Gen8+ says:
4590 *
4591 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
4592 * the only bits that are changed are scoreboard related: Scoreboard
4593 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard Delta. For
4594 * these scoreboard related states, a MEDIA_STATE_FLUSH is
4595 * sufficient."
4596 */
4597 iris_emit_pipe_control_flush(batch, PIPE_CONTROL_CS_STALL);
4598
4599 iris_emit_cmd(batch, GENX(MEDIA_VFE_STATE), vfe) {
4600 if (prog_data->total_scratch) {
4601 uint32_t scratch_addr =
4602 iris_get_scratch_space(ice, prog_data->total_scratch,
4603 MESA_SHADER_COMPUTE);
4604 vfe.PerThreadScratchSpace = ffs(prog_data->total_scratch) - 11;
4605 vfe.ScratchSpaceBasePointer = rw_bo(NULL, scratch_addr);
4606 }
4607
4608 vfe.MaximumNumberofThreads =
4609 devinfo->max_cs_threads * screen->subslice_total - 1;
4610 #if GEN_GEN < 11
4611 vfe.ResetGatewayTimer =
4612 Resettingrelativetimerandlatchingtheglobaltimestamp;
4613 #endif
4614
4615 vfe.NumberofURBEntries = 2;
4616 vfe.URBEntryAllocationSize = 2;
4617
4618 // XXX: Use Indirect Payload Storage?
4619 vfe.CURBEAllocationSize =
4620 ALIGN(cs_prog_data->push.per_thread.regs * cs_prog_data->threads +
4621 cs_prog_data->push.cross_thread.regs, 2);
4622 }
4623 }
4624
4625 // XXX: hack iris_set_constant_buffers to upload these thread counts
4626 // XXX: along with regular uniforms for compute shaders, somehow.
4627
4628 uint32_t curbe_data_offset = 0;
4629 // TODO: Move subgroup-id into uniforms ubo so we can push uniforms
4630 assert(cs_prog_data->push.cross_thread.dwords == 0 &&
4631 cs_prog_data->push.per_thread.dwords == 1 &&
4632 cs_prog_data->base.param[0] == BRW_PARAM_BUILTIN_SUBGROUP_ID);
4633 struct pipe_resource *curbe_data_res = NULL;
4634 uint32_t *curbe_data_map =
4635 stream_state(batch, ice->state.dynamic_uploader, &curbe_data_res,
4636 ALIGN(cs_prog_data->push.total.size, 64), 64,
4637 &curbe_data_offset);
4638 assert(curbe_data_map);
4639 memset(curbe_data_map, 0x5a, ALIGN(cs_prog_data->push.total.size, 64));
4640 iris_fill_cs_push_const_buffer(cs_prog_data, curbe_data_map);
4641
4642 if (dirty & IRIS_DIRTY_CONSTANTS_CS) {
4643 iris_emit_cmd(batch, GENX(MEDIA_CURBE_LOAD), curbe) {
4644 curbe.CURBETotalDataLength =
4645 ALIGN(cs_prog_data->push.total.size, 64);
4646 curbe.CURBEDataStartAddress = curbe_data_offset;
4647 }
4648 }
4649
4650 if (dirty & (IRIS_DIRTY_SAMPLER_STATES_CS |
4651 IRIS_DIRTY_BINDINGS_CS |
4652 IRIS_DIRTY_CONSTANTS_CS |
4653 IRIS_DIRTY_CS)) {
4654 struct pipe_resource *desc_res = NULL;
4655 uint32_t desc[GENX(INTERFACE_DESCRIPTOR_DATA_length)];
4656
4657 iris_pack_state(GENX(INTERFACE_DESCRIPTOR_DATA), desc, idd) {
4658 idd.SamplerStatePointer = shs->sampler_table.offset;
4659 idd.BindingTablePointer = binder->bt_offset[MESA_SHADER_COMPUTE];
4660 }
4661
4662 for (int i = 0; i < GENX(INTERFACE_DESCRIPTOR_DATA_length); i++)
4663 desc[i] |= ((uint32_t *) shader->derived_data)[i];
4664
4665 iris_emit_cmd(batch, GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), load) {
4666 load.InterfaceDescriptorTotalLength =
4667 GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
4668 load.InterfaceDescriptorDataStartAddress =
4669 emit_state(batch, ice->state.dynamic_uploader,
4670 &desc_res, desc, sizeof(desc), 32);
4671 }
4672
4673 pipe_resource_reference(&desc_res, NULL);
4674 }
4675
4676 uint32_t group_size = grid->block[0] * grid->block[1] * grid->block[2];
4677 uint32_t remainder = group_size & (cs_prog_data->simd_size - 1);
4678 uint32_t right_mask;
4679
4680 if (remainder > 0)
4681 right_mask = ~0u >> (32 - remainder);
4682 else
4683 right_mask = ~0u >> (32 - cs_prog_data->simd_size);
4684
4685 #define GPGPU_DISPATCHDIMX 0x2500
4686 #define GPGPU_DISPATCHDIMY 0x2504
4687 #define GPGPU_DISPATCHDIMZ 0x2508
4688
4689 if (grid->indirect) {
4690 struct iris_state_ref *grid_size = &ice->state.grid_size;
4691 struct iris_bo *bo = iris_resource_bo(grid_size->res);
4692 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4693 lrm.RegisterAddress = GPGPU_DISPATCHDIMX;
4694 lrm.MemoryAddress = ro_bo(bo, grid_size->offset + 0);
4695 }
4696 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4697 lrm.RegisterAddress = GPGPU_DISPATCHDIMY;
4698 lrm.MemoryAddress = ro_bo(bo, grid_size->offset + 4);
4699 }
4700 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4701 lrm.RegisterAddress = GPGPU_DISPATCHDIMZ;
4702 lrm.MemoryAddress = ro_bo(bo, grid_size->offset + 8);
4703 }
4704 }
4705
4706 iris_emit_cmd(batch, GENX(GPGPU_WALKER), ggw) {
4707 ggw.IndirectParameterEnable = grid->indirect != NULL;
4708 ggw.SIMDSize = cs_prog_data->simd_size / 16;
4709 ggw.ThreadDepthCounterMaximum = 0;
4710 ggw.ThreadHeightCounterMaximum = 0;
4711 ggw.ThreadWidthCounterMaximum = cs_prog_data->threads - 1;
4712 ggw.ThreadGroupIDXDimension = grid->grid[0];
4713 ggw.ThreadGroupIDYDimension = grid->grid[1];
4714 ggw.ThreadGroupIDZDimension = grid->grid[2];
4715 ggw.RightExecutionMask = right_mask;
4716 ggw.BottomExecutionMask = 0xffffffff;
4717 }
4718
4719 iris_emit_cmd(batch, GENX(MEDIA_STATE_FLUSH), msf);
4720
4721 if (!batch->contains_draw) {
4722 iris_restore_compute_saved_bos(ice, batch, grid);
4723 batch->contains_draw = true;
4724 }
4725 }
4726
4727 /**
4728 * State module teardown.
4729 */
4730 static void
4731 iris_destroy_state(struct iris_context *ice)
4732 {
4733 iris_free_vertex_buffers(&ice->state.genx->vertex_buffers);
4734
4735 // XXX: unreference resources/surfaces.
4736 for (unsigned i = 0; i < ice->state.framebuffer.nr_cbufs; i++) {
4737 pipe_surface_reference(&ice->state.framebuffer.cbufs[i], NULL);
4738 }
4739 pipe_surface_reference(&ice->state.framebuffer.zsbuf, NULL);
4740
4741 for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
4742 struct iris_shader_state *shs = &ice->state.shaders[stage];
4743 pipe_resource_reference(&shs->sampler_table.res, NULL);
4744 }
4745 free(ice->state.genx);
4746
4747 pipe_resource_reference(&ice->state.unbound_tex.res, NULL);
4748
4749 pipe_resource_reference(&ice->state.last_res.cc_vp, NULL);
4750 pipe_resource_reference(&ice->state.last_res.sf_cl_vp, NULL);
4751 pipe_resource_reference(&ice->state.last_res.color_calc, NULL);
4752 pipe_resource_reference(&ice->state.last_res.scissor, NULL);
4753 pipe_resource_reference(&ice->state.last_res.blend, NULL);
4754 pipe_resource_reference(&ice->state.last_res.index_buffer, NULL);
4755 }
4756
4757 /* ------------------------------------------------------------------- */
4758
4759 static void
4760 iris_load_register_reg32(struct iris_batch *batch, uint32_t src,
4761 uint32_t dst)
4762 {
4763 _iris_emit_lrr(batch, src, dst);
4764 }
4765
4766 static void
4767 iris_load_register_reg64(struct iris_batch *batch, uint32_t src,
4768 uint32_t dst)
4769 {
4770 _iris_emit_lrr(batch, src, dst);
4771 _iris_emit_lrr(batch, src + 4, dst + 4);
4772 }
4773
4774 static void
4775 iris_load_register_imm32(struct iris_batch *batch, uint32_t reg,
4776 uint32_t val)
4777 {
4778 _iris_emit_lri(batch, reg, val);
4779 }
4780
4781 static void
4782 iris_load_register_imm64(struct iris_batch *batch, uint32_t reg,
4783 uint64_t val)
4784 {
4785 _iris_emit_lri(batch, reg + 0, val & 0xffffffff);
4786 _iris_emit_lri(batch, reg + 4, val >> 32);
4787 }
4788
4789 /**
4790 * Emit MI_LOAD_REGISTER_MEM to load a 32-bit MMIO register from a buffer.
4791 */
4792 static void
4793 iris_load_register_mem32(struct iris_batch *batch, uint32_t reg,
4794 struct iris_bo *bo, uint32_t offset)
4795 {
4796 iris_emit_cmd(batch, GENX(MI_LOAD_REGISTER_MEM), lrm) {
4797 lrm.RegisterAddress = reg;
4798 lrm.MemoryAddress = ro_bo(bo, offset);
4799 }
4800 }
4801
4802 /**
4803 * Load a 64-bit value from a buffer into a MMIO register via
4804 * two MI_LOAD_REGISTER_MEM commands.
4805 */
4806 static void
4807 iris_load_register_mem64(struct iris_batch *batch, uint32_t reg,
4808 struct iris_bo *bo, uint32_t offset)
4809 {
4810 iris_load_register_mem32(batch, reg + 0, bo, offset + 0);
4811 iris_load_register_mem32(batch, reg + 4, bo, offset + 4);
4812 }
4813
4814 static void
4815 iris_store_register_mem32(struct iris_batch *batch, uint32_t reg,
4816 struct iris_bo *bo, uint32_t offset,
4817 bool predicated)
4818 {
4819 iris_emit_cmd(batch, GENX(MI_STORE_REGISTER_MEM), srm) {
4820 srm.RegisterAddress = reg;
4821 srm.MemoryAddress = rw_bo(bo, offset);
4822 srm.PredicateEnable = predicated;
4823 }
4824 }
4825
4826 static void
4827 iris_store_register_mem64(struct iris_batch *batch, uint32_t reg,
4828 struct iris_bo *bo, uint32_t offset,
4829 bool predicated)
4830 {
4831 iris_store_register_mem32(batch, reg + 0, bo, offset + 0, predicated);
4832 iris_store_register_mem32(batch, reg + 4, bo, offset + 4, predicated);
4833 }
4834
4835 static void
4836 iris_store_data_imm32(struct iris_batch *batch,
4837 struct iris_bo *bo, uint32_t offset,
4838 uint32_t imm)
4839 {
4840 iris_emit_cmd(batch, GENX(MI_STORE_DATA_IMM), sdi) {
4841 sdi.Address = rw_bo(bo, offset);
4842 sdi.ImmediateData = imm;
4843 }
4844 }
4845
4846 static void
4847 iris_store_data_imm64(struct iris_batch *batch,
4848 struct iris_bo *bo, uint32_t offset,
4849 uint64_t imm)
4850 {
4851 /* Can't use iris_emit_cmd because MI_STORE_DATA_IMM has a length of
4852 * 2 in genxml but it's actually variable length and we need 5 DWords.
4853 */
4854 void *map = iris_get_command_space(batch, 4 * 5);
4855 _iris_pack_command(batch, GENX(MI_STORE_DATA_IMM), map, sdi) {
4856 sdi.DWordLength = 5 - 2;
4857 sdi.Address = rw_bo(bo, offset);
4858 sdi.ImmediateData = imm;
4859 }
4860 }
4861
4862 static void
4863 iris_copy_mem_mem(struct iris_batch *batch,
4864 struct iris_bo *dst_bo, uint32_t dst_offset,
4865 struct iris_bo *src_bo, uint32_t src_offset,
4866 unsigned bytes)
4867 {
4868 /* MI_COPY_MEM_MEM operates on DWords. */
4869 assert(bytes % 4 == 0);
4870 assert(dst_offset % 4 == 0);
4871 assert(src_offset % 4 == 0);
4872
4873 for (unsigned i = 0; i < bytes; i += 4) {
4874 iris_emit_cmd(batch, GENX(MI_COPY_MEM_MEM), cp) {
4875 cp.DestinationMemoryAddress = rw_bo(dst_bo, dst_offset + i);
4876 cp.SourceMemoryAddress = ro_bo(src_bo, src_offset + i);
4877 }
4878 }
4879 }
4880
4881 /* ------------------------------------------------------------------- */
4882
4883 static unsigned
4884 flags_to_post_sync_op(uint32_t flags)
4885 {
4886 if (flags & PIPE_CONTROL_WRITE_IMMEDIATE)
4887 return WriteImmediateData;
4888
4889 if (flags & PIPE_CONTROL_WRITE_DEPTH_COUNT)
4890 return WritePSDepthCount;
4891
4892 if (flags & PIPE_CONTROL_WRITE_TIMESTAMP)
4893 return WriteTimestamp;
4894
4895 return 0;
4896 }
4897
4898 /**
4899 * Do the given flags have a Post Sync or LRI Post Sync operation?
4900 */
4901 static enum pipe_control_flags
4902 get_post_sync_flags(enum pipe_control_flags flags)
4903 {
4904 flags &= PIPE_CONTROL_WRITE_IMMEDIATE |
4905 PIPE_CONTROL_WRITE_DEPTH_COUNT |
4906 PIPE_CONTROL_WRITE_TIMESTAMP |
4907 PIPE_CONTROL_LRI_POST_SYNC_OP;
4908
4909 /* Only one "Post Sync Op" is allowed, and it's mutually exclusive with
4910 * "LRI Post Sync Operation". So more than one bit set would be illegal.
4911 */
4912 assert(util_bitcount(flags) <= 1);
4913
4914 return flags;
4915 }
4916
4917 #define IS_COMPUTE_PIPELINE(batch) (batch->name == IRIS_BATCH_COMPUTE)
4918
4919 /**
4920 * Emit a series of PIPE_CONTROL commands, taking into account any
4921 * workarounds necessary to actually accomplish the caller's request.
4922 *
4923 * Unless otherwise noted, spec quotations in this function come from:
4924 *
4925 * Synchronization of the 3D Pipeline > PIPE_CONTROL Command > Programming
4926 * Restrictions for PIPE_CONTROL.
4927 *
4928 * You should not use this function directly. Use the helpers in
4929 * iris_pipe_control.c instead, which may split the pipe control further.
4930 */
4931 static void
4932 iris_emit_raw_pipe_control(struct iris_batch *batch, uint32_t flags,
4933 struct iris_bo *bo, uint32_t offset, uint64_t imm)
4934 {
4935 UNUSED const struct gen_device_info *devinfo = &batch->screen->devinfo;
4936 enum pipe_control_flags post_sync_flags = get_post_sync_flags(flags);
4937 enum pipe_control_flags non_lri_post_sync_flags =
4938 post_sync_flags & ~PIPE_CONTROL_LRI_POST_SYNC_OP;
4939
4940 /* Recursive PIPE_CONTROL workarounds --------------------------------
4941 * (http://knowyourmeme.com/memes/xzibit-yo-dawg)
4942 *
4943 * We do these first because we want to look at the original operation,
4944 * rather than any workarounds we set.
4945 */
4946 if (GEN_GEN == 9 && (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE)) {
4947 /* The PIPE_CONTROL "VF Cache Invalidation Enable" bit description
4948 * lists several workarounds:
4949 *
4950 * "Project: SKL, KBL, BXT
4951 *
4952 * If the VF Cache Invalidation Enable is set to a 1 in a
4953 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields
4954 * sets to 0, with the VF Cache Invalidation Enable set to 0
4955 * needs to be sent prior to the PIPE_CONTROL with VF Cache
4956 * Invalidation Enable set to a 1."
4957 */
4958 iris_emit_raw_pipe_control(batch, 0, NULL, 0, 0);
4959 }
4960
4961 if (GEN_GEN == 9 && IS_COMPUTE_PIPELINE(batch) && post_sync_flags) {
4962 /* Project: SKL / Argument: LRI Post Sync Operation [23]
4963 *
4964 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
4965 * programmed prior to programming a PIPECONTROL command with "LRI
4966 * Post Sync Operation" in GPGPU mode of operation (i.e when
4967 * PIPELINE_SELECT command is set to GPGPU mode of operation)."
4968 *
4969 * The same text exists a few rows below for Post Sync Op.
4970 */
4971 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_CS_STALL, bo, offset, imm);
4972 }
4973
4974 if (GEN_GEN == 10 && (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH)) {
4975 /* Cannonlake:
4976 * "Before sending a PIPE_CONTROL command with bit 12 set, SW must issue
4977 * another PIPE_CONTROL with Render Target Cache Flush Enable (bit 12)
4978 * = 0 and Pipe Control Flush Enable (bit 7) = 1"
4979 */
4980 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_FLUSH_ENABLE, bo,
4981 offset, imm);
4982 }
4983
4984 /* "Flush Types" workarounds ---------------------------------------------
4985 * We do these now because they may add post-sync operations or CS stalls.
4986 */
4987
4988 if (GEN_GEN < 11 && flags & PIPE_CONTROL_VF_CACHE_INVALIDATE) {
4989 /* Project: BDW, SKL+ (stopping at CNL) / Argument: VF Invalidate
4990 *
4991 * "'Post Sync Operation' must be enabled to 'Write Immediate Data' or
4992 * 'Write PS Depth Count' or 'Write Timestamp'."
4993 */
4994 if (!bo) {
4995 flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
4996 post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
4997 non_lri_post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
4998 bo = batch->screen->workaround_bo;
4999 }
5000 }
5001
5002 /* #1130 from Gen10 workarounds page:
5003 *
5004 * "Enable Depth Stall on every Post Sync Op if Render target Cache
5005 * Flush is not enabled in same PIPE CONTROL and Enable Pixel score
5006 * board stall if Render target cache flush is enabled."
5007 *
5008 * Applicable to CNL B0 and C0 steppings only.
5009 *
5010 * The wording here is unclear, and this workaround doesn't look anything
5011 * like the internal bug report recommendations, but leave it be for now...
5012 */
5013 if (GEN_GEN == 10) {
5014 if (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH) {
5015 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
5016 } else if (flags & non_lri_post_sync_flags) {
5017 flags |= PIPE_CONTROL_DEPTH_STALL;
5018 }
5019 }
5020
5021 if (flags & PIPE_CONTROL_DEPTH_STALL) {
5022 /* From the PIPE_CONTROL instruction table, bit 13 (Depth Stall Enable):
5023 *
5024 * "This bit must be DISABLED for operations other than writing
5025 * PS_DEPTH_COUNT."
5026 *
5027 * This seems like nonsense. An Ivybridge workaround requires us to
5028 * emit a PIPE_CONTROL with a depth stall and write immediate post-sync
5029 * operation. Gen8+ requires us to emit depth stalls and depth cache
5030 * flushes together. So, it's hard to imagine this means anything other
5031 * than "we originally intended this to be used for PS_DEPTH_COUNT".
5032 *
5033 * We ignore the supposed restriction and do nothing.
5034 */
5035 }
5036
5037 if (flags & (PIPE_CONTROL_RENDER_TARGET_FLUSH |
5038 PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
5039 /* From the PIPE_CONTROL instruction table, bit 12 and bit 1:
5040 *
5041 * "This bit must be DISABLED for End-of-pipe (Read) fences,
5042 * PS_DEPTH_COUNT or TIMESTAMP queries."
5043 *
5044 * TODO: Implement end-of-pipe checking.
5045 */
5046 assert(!(post_sync_flags & (PIPE_CONTROL_WRITE_DEPTH_COUNT |
5047 PIPE_CONTROL_WRITE_TIMESTAMP)));
5048 }
5049
5050 if (GEN_GEN < 11 && (flags & PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
5051 /* From the PIPE_CONTROL instruction table, bit 1:
5052 *
5053 * "This bit is ignored if Depth Stall Enable is set.
5054 * Further, the render cache is not flushed even if Write Cache
5055 * Flush Enable bit is set."
5056 *
5057 * We assert that the caller doesn't do this combination, to try and
5058 * prevent mistakes. It shouldn't hurt the GPU, though.
5059 *
5060 * We skip this check on Gen11+ as the "Stall at Pixel Scoreboard"
5061 * and "Render Target Flush" combo is explicitly required for BTI
5062 * update workarounds.
5063 */
5064 assert(!(flags & (PIPE_CONTROL_DEPTH_STALL |
5065 PIPE_CONTROL_RENDER_TARGET_FLUSH)));
5066 }
5067
5068 /* PIPE_CONTROL page workarounds ------------------------------------- */
5069
5070 if (GEN_GEN <= 8 && (flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE)) {
5071 /* From the PIPE_CONTROL page itself:
5072 *
5073 * "IVB, HSW, BDW
5074 * Restriction: Pipe_control with CS-stall bit set must be issued
5075 * before a pipe-control command that has the State Cache
5076 * Invalidate bit set."
5077 */
5078 flags |= PIPE_CONTROL_CS_STALL;
5079 }
5080
5081 if (flags & PIPE_CONTROL_FLUSH_LLC) {
5082 /* From the PIPE_CONTROL instruction table, bit 26 (Flush LLC):
5083 *
5084 * "Project: ALL
5085 * SW must always program Post-Sync Operation to "Write Immediate
5086 * Data" when Flush LLC is set."
5087 *
5088 * For now, we just require the caller to do it.
5089 */
5090 assert(flags & PIPE_CONTROL_WRITE_IMMEDIATE);
5091 }
5092
5093 /* "Post-Sync Operation" workarounds -------------------------------- */
5094
5095 /* Project: All / Argument: Global Snapshot Count Reset [19]
5096 *
5097 * "This bit must not be exercised on any product.
5098 * Requires stall bit ([20] of DW1) set."
5099 *
5100 * We don't use this, so we just assert that it isn't used. The
5101 * PIPE_CONTROL instruction page indicates that they intended this
5102 * as a debug feature and don't think it is useful in production,
5103 * but it may actually be usable, should we ever want to.
5104 */
5105 assert((flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET) == 0);
5106
5107 if (flags & (PIPE_CONTROL_MEDIA_STATE_CLEAR |
5108 PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE)) {
5109 /* Project: All / Arguments:
5110 *
5111 * - Generic Media State Clear [16]
5112 * - Indirect State Pointers Disable [16]
5113 *
5114 * "Requires stall bit ([20] of DW1) set."
5115 *
5116 * Also, the PIPE_CONTROL instruction table, bit 16 (Generic Media
5117 * State Clear) says:
5118 *
5119 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
5120 * programmed prior to programming a PIPECONTROL command with "Media
5121 * State Clear" set in GPGPU mode of operation"
5122 *
5123 * This is a subset of the earlier rule, so there's nothing to do.
5124 */
5125 flags |= PIPE_CONTROL_CS_STALL;
5126 }
5127
5128 if (flags & PIPE_CONTROL_STORE_DATA_INDEX) {
5129 /* Project: All / Argument: Store Data Index
5130 *
5131 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
5132 * than '0'."
5133 *
5134 * For now, we just assert that the caller does this. We might want to
5135 * automatically add a write to the workaround BO...
5136 */
5137 assert(non_lri_post_sync_flags != 0);
5138 }
5139
5140 if (flags & PIPE_CONTROL_SYNC_GFDT) {
5141 /* Project: All / Argument: Sync GFDT
5142 *
5143 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
5144 * than '0' or 0x2520[13] must be set."
5145 *
5146 * For now, we just assert that the caller does this.
5147 */
5148 assert(non_lri_post_sync_flags != 0);
5149 }
5150
5151 if (flags & PIPE_CONTROL_TLB_INVALIDATE) {
5152 /* Project: IVB+ / Argument: TLB inv
5153 *
5154 * "Requires stall bit ([20] of DW1) set."
5155 *
5156 * Also, from the PIPE_CONTROL instruction table:
5157 *
5158 * "Project: SKL+
5159 * Post Sync Operation or CS stall must be set to ensure a TLB
5160 * invalidation occurs. Otherwise no cycle will occur to the TLB
5161 * cache to invalidate."
5162 *
5163 * This is not a subset of the earlier rule, so there's nothing to do.
5164 */
5165 flags |= PIPE_CONTROL_CS_STALL;
5166 }
5167
5168 if (GEN_GEN == 9 && devinfo->gt == 4) {
5169 /* TODO: The big Skylake GT4 post sync op workaround */
5170 }
5171
5172 /* "GPGPU specific workarounds" (both post-sync and flush) ------------ */
5173
5174 if (IS_COMPUTE_PIPELINE(batch)) {
5175 if (GEN_GEN >= 9 && (flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE)) {
5176 /* Project: SKL+ / Argument: Tex Invalidate
5177 * "Requires stall bit ([20] of DW) set for all GPGPU Workloads."
5178 */
5179 flags |= PIPE_CONTROL_CS_STALL;
5180 }
5181
5182 if (GEN_GEN == 8 && (post_sync_flags ||
5183 (flags & (PIPE_CONTROL_NOTIFY_ENABLE |
5184 PIPE_CONTROL_DEPTH_STALL |
5185 PIPE_CONTROL_RENDER_TARGET_FLUSH |
5186 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
5187 PIPE_CONTROL_DATA_CACHE_FLUSH)))) {
5188 /* Project: BDW / Arguments:
5189 *
5190 * - LRI Post Sync Operation [23]
5191 * - Post Sync Op [15:14]
5192 * - Notify En [8]
5193 * - Depth Stall [13]
5194 * - Render Target Cache Flush [12]
5195 * - Depth Cache Flush [0]
5196 * - DC Flush Enable [5]
5197 *
5198 * "Requires stall bit ([20] of DW) set for all GPGPU and Media
5199 * Workloads."
5200 */
5201 flags |= PIPE_CONTROL_CS_STALL;
5202
5203 /* Also, from the PIPE_CONTROL instruction table, bit 20:
5204 *
5205 * "Project: BDW
5206 * This bit must be always set when PIPE_CONTROL command is
5207 * programmed by GPGPU and MEDIA workloads, except for the cases
5208 * when only Read Only Cache Invalidation bits are set (State
5209 * Cache Invalidation Enable, Instruction cache Invalidation
5210 * Enable, Texture Cache Invalidation Enable, Constant Cache
5211 * Invalidation Enable). This is to WA FFDOP CG issue, this WA
5212 * need not implemented when FF_DOP_CG is disable via "Fixed
5213 * Function DOP Clock Gate Disable" bit in RC_PSMI_CTRL register."
5214 *
5215 * It sounds like we could avoid CS stalls in some cases, but we
5216 * don't currently bother. This list isn't exactly the list above,
5217 * either...
5218 */
5219 }
5220 }
5221
5222 /* "Stall" workarounds ----------------------------------------------
5223 * These have to come after the earlier ones because we may have added
5224 * some additional CS stalls above.
5225 */
5226
5227 if (GEN_GEN < 9 && (flags & PIPE_CONTROL_CS_STALL)) {
5228 /* Project: PRE-SKL, VLV, CHV
5229 *
5230 * "[All Stepping][All SKUs]:
5231 *
5232 * One of the following must also be set:
5233 *
5234 * - Render Target Cache Flush Enable ([12] of DW1)
5235 * - Depth Cache Flush Enable ([0] of DW1)
5236 * - Stall at Pixel Scoreboard ([1] of DW1)
5237 * - Depth Stall ([13] of DW1)
5238 * - Post-Sync Operation ([13] of DW1)
5239 * - DC Flush Enable ([5] of DW1)"
5240 *
5241 * If we don't already have one of those bits set, we choose to add
5242 * "Stall at Pixel Scoreboard". Some of the other bits require a
5243 * CS stall as a workaround (see above), which would send us into
5244 * an infinite recursion of PIPE_CONTROLs. "Stall at Pixel Scoreboard"
5245 * appears to be safe, so we choose that.
5246 */
5247 const uint32_t wa_bits = PIPE_CONTROL_RENDER_TARGET_FLUSH |
5248 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
5249 PIPE_CONTROL_WRITE_IMMEDIATE |
5250 PIPE_CONTROL_WRITE_DEPTH_COUNT |
5251 PIPE_CONTROL_WRITE_TIMESTAMP |
5252 PIPE_CONTROL_STALL_AT_SCOREBOARD |
5253 PIPE_CONTROL_DEPTH_STALL |
5254 PIPE_CONTROL_DATA_CACHE_FLUSH;
5255 if (!(flags & wa_bits))
5256 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
5257 }
5258
5259 /* Emit --------------------------------------------------------------- */
5260
5261 iris_emit_cmd(batch, GENX(PIPE_CONTROL), pc) {
5262 pc.LRIPostSyncOperation = NoLRIOperation;
5263 pc.PipeControlFlushEnable = flags & PIPE_CONTROL_FLUSH_ENABLE;
5264 pc.DCFlushEnable = flags & PIPE_CONTROL_DATA_CACHE_FLUSH;
5265 pc.StoreDataIndex = 0;
5266 pc.CommandStreamerStallEnable = flags & PIPE_CONTROL_CS_STALL;
5267 pc.GlobalSnapshotCountReset =
5268 flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET;
5269 pc.TLBInvalidate = flags & PIPE_CONTROL_TLB_INVALIDATE;
5270 pc.GenericMediaStateClear = flags & PIPE_CONTROL_MEDIA_STATE_CLEAR;
5271 pc.StallAtPixelScoreboard = flags & PIPE_CONTROL_STALL_AT_SCOREBOARD;
5272 pc.RenderTargetCacheFlushEnable =
5273 flags & PIPE_CONTROL_RENDER_TARGET_FLUSH;
5274 pc.DepthCacheFlushEnable = flags & PIPE_CONTROL_DEPTH_CACHE_FLUSH;
5275 pc.StateCacheInvalidationEnable =
5276 flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE;
5277 pc.VFCacheInvalidationEnable = flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
5278 pc.ConstantCacheInvalidationEnable =
5279 flags & PIPE_CONTROL_CONST_CACHE_INVALIDATE;
5280 pc.PostSyncOperation = flags_to_post_sync_op(flags);
5281 pc.DepthStallEnable = flags & PIPE_CONTROL_DEPTH_STALL;
5282 pc.InstructionCacheInvalidateEnable =
5283 flags & PIPE_CONTROL_INSTRUCTION_INVALIDATE;
5284 pc.NotifyEnable = flags & PIPE_CONTROL_NOTIFY_ENABLE;
5285 pc.IndirectStatePointersDisable =
5286 flags & PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE;
5287 pc.TextureCacheInvalidationEnable =
5288 flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
5289 pc.Address = rw_bo(bo, offset);
5290 pc.ImmediateData = imm;
5291 }
5292 }
5293
5294 void
5295 genX(init_state)(struct iris_context *ice)
5296 {
5297 struct pipe_context *ctx = &ice->ctx;
5298 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
5299
5300 ctx->create_blend_state = iris_create_blend_state;
5301 ctx->create_depth_stencil_alpha_state = iris_create_zsa_state;
5302 ctx->create_rasterizer_state = iris_create_rasterizer_state;
5303 ctx->create_sampler_state = iris_create_sampler_state;
5304 ctx->create_sampler_view = iris_create_sampler_view;
5305 ctx->create_surface = iris_create_surface;
5306 ctx->create_vertex_elements_state = iris_create_vertex_elements;
5307 ctx->bind_blend_state = iris_bind_blend_state;
5308 ctx->bind_depth_stencil_alpha_state = iris_bind_zsa_state;
5309 ctx->bind_sampler_states = iris_bind_sampler_states;
5310 ctx->bind_rasterizer_state = iris_bind_rasterizer_state;
5311 ctx->bind_vertex_elements_state = iris_bind_vertex_elements_state;
5312 ctx->delete_blend_state = iris_delete_state;
5313 ctx->delete_depth_stencil_alpha_state = iris_delete_state;
5314 ctx->delete_rasterizer_state = iris_delete_state;
5315 ctx->delete_sampler_state = iris_delete_state;
5316 ctx->delete_vertex_elements_state = iris_delete_state;
5317 ctx->set_blend_color = iris_set_blend_color;
5318 ctx->set_clip_state = iris_set_clip_state;
5319 ctx->set_constant_buffer = iris_set_constant_buffer;
5320 ctx->set_shader_buffers = iris_set_shader_buffers;
5321 ctx->set_shader_images = iris_set_shader_images;
5322 ctx->set_sampler_views = iris_set_sampler_views;
5323 ctx->set_tess_state = iris_set_tess_state;
5324 ctx->set_framebuffer_state = iris_set_framebuffer_state;
5325 ctx->set_polygon_stipple = iris_set_polygon_stipple;
5326 ctx->set_sample_mask = iris_set_sample_mask;
5327 ctx->set_scissor_states = iris_set_scissor_states;
5328 ctx->set_stencil_ref = iris_set_stencil_ref;
5329 ctx->set_vertex_buffers = iris_set_vertex_buffers;
5330 ctx->set_viewport_states = iris_set_viewport_states;
5331 ctx->sampler_view_destroy = iris_sampler_view_destroy;
5332 ctx->surface_destroy = iris_surface_destroy;
5333 ctx->draw_vbo = iris_draw_vbo;
5334 ctx->launch_grid = iris_launch_grid;
5335 ctx->create_stream_output_target = iris_create_stream_output_target;
5336 ctx->stream_output_target_destroy = iris_stream_output_target_destroy;
5337 ctx->set_stream_output_targets = iris_set_stream_output_targets;
5338
5339 ice->vtbl.destroy_state = iris_destroy_state;
5340 ice->vtbl.init_render_context = iris_init_render_context;
5341 ice->vtbl.init_compute_context = iris_init_compute_context;
5342 ice->vtbl.upload_render_state = iris_upload_render_state;
5343 ice->vtbl.update_surface_base_address = iris_update_surface_base_address;
5344 ice->vtbl.upload_compute_state = iris_upload_compute_state;
5345 ice->vtbl.emit_raw_pipe_control = iris_emit_raw_pipe_control;
5346 ice->vtbl.load_register_reg32 = iris_load_register_reg32;
5347 ice->vtbl.load_register_reg64 = iris_load_register_reg64;
5348 ice->vtbl.load_register_imm32 = iris_load_register_imm32;
5349 ice->vtbl.load_register_imm64 = iris_load_register_imm64;
5350 ice->vtbl.load_register_mem32 = iris_load_register_mem32;
5351 ice->vtbl.load_register_mem64 = iris_load_register_mem64;
5352 ice->vtbl.store_register_mem32 = iris_store_register_mem32;
5353 ice->vtbl.store_register_mem64 = iris_store_register_mem64;
5354 ice->vtbl.store_data_imm32 = iris_store_data_imm32;
5355 ice->vtbl.store_data_imm64 = iris_store_data_imm64;
5356 ice->vtbl.copy_mem_mem = iris_copy_mem_mem;
5357 ice->vtbl.derived_program_state_size = iris_derived_program_state_size;
5358 ice->vtbl.store_derived_program_state = iris_store_derived_program_state;
5359 ice->vtbl.create_so_decl_list = iris_create_so_decl_list;
5360 ice->vtbl.populate_vs_key = iris_populate_vs_key;
5361 ice->vtbl.populate_tcs_key = iris_populate_tcs_key;
5362 ice->vtbl.populate_tes_key = iris_populate_tes_key;
5363 ice->vtbl.populate_gs_key = iris_populate_gs_key;
5364 ice->vtbl.populate_fs_key = iris_populate_fs_key;
5365 ice->vtbl.populate_cs_key = iris_populate_cs_key;
5366
5367 ice->state.dirty = ~0ull;
5368
5369 ice->state.statistics_counters_enabled = true;
5370
5371 ice->state.sample_mask = 0xffff;
5372 ice->state.num_viewports = 1;
5373 ice->state.genx = calloc(1, sizeof(struct iris_genx_state));
5374
5375 /* Make a 1x1x1 null surface for unbound textures */
5376 void *null_surf_map =
5377 upload_state(ice->state.surface_uploader, &ice->state.unbound_tex,
5378 4 * GENX(RENDER_SURFACE_STATE_length), 64);
5379 isl_null_fill_state(&screen->isl_dev, null_surf_map, isl_extent3d(1, 1, 1));
5380 ice->state.unbound_tex.offset +=
5381 iris_bo_offset_from_base_address(iris_resource_bo(ice->state.unbound_tex.res));
5382
5383 /* Default all scissor rectangles to be empty regions. */
5384 for (int i = 0; i < IRIS_MAX_VIEWPORTS; i++) {
5385 ice->state.scissors[i] = (struct pipe_scissor_state) {
5386 .minx = 1, .maxx = 0, .miny = 1, .maxy = 0,
5387 };
5388 }
5389 }