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