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