2 * Copyright © 2017 Intel Corporation
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
24 * @file iris_program.c
26 * This file contains the driver interface for compiling shaders.
28 * See iris_program_cache.c for the in-memory program cache where the
29 * compiled shaders are stored.
34 #include "pipe/p_defines.h"
35 #include "pipe/p_state.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_screen.h"
38 #include "util/u_atomic.h"
39 #include "util/u_upload_mgr.h"
40 #include "util/debug.h"
41 #include "compiler/nir/nir.h"
42 #include "compiler/nir/nir_builder.h"
43 #include "compiler/nir/nir_serialize.h"
44 #include "intel/compiler/brw_compiler.h"
45 #include "intel/compiler/brw_nir.h"
46 #include "iris_context.h"
47 #include "nir/tgsi_to_nir.h"
49 #define KEY_INIT_NO_ID(gen) \
50 .base.tex.swizzles[0 ... MAX_SAMPLERS - 1] = 0x688, \
51 .base.tex.compressed_multisample_layout_mask = ~0, \
52 .base.tex.msaa_16 = (gen >= 9 ? ~0 : 0)
53 #define KEY_INIT(gen) .base.program_string_id = ish->program_id, KEY_INIT_NO_ID(gen)
56 get_new_program_id(struct iris_screen
*screen
)
58 return p_atomic_inc_return(&screen
->program_id
);
62 upload_state(struct u_upload_mgr
*uploader
,
63 struct iris_state_ref
*ref
,
68 u_upload_alloc(uploader
, 0, size
, alignment
, &ref
->offset
, &ref
->res
, &p
);
73 iris_upload_ubo_ssbo_surf_state(struct iris_context
*ice
,
74 struct pipe_shader_buffer
*buf
,
75 struct iris_state_ref
*surf_state
,
78 struct pipe_context
*ctx
= &ice
->ctx
;
79 struct iris_screen
*screen
= (struct iris_screen
*) ctx
->screen
;
82 upload_state(ice
->state
.surface_uploader
, surf_state
,
83 screen
->isl_dev
.ss
.size
, 64);
85 surf_state
->res
= NULL
;
89 struct iris_resource
*res
= (void *) buf
->buffer
;
90 struct iris_bo
*surf_bo
= iris_resource_bo(surf_state
->res
);
91 surf_state
->offset
+= iris_bo_offset_from_base_address(surf_bo
);
93 isl_buffer_fill_state(&screen
->isl_dev
, map
,
94 .address
= res
->bo
->gtt_offset
+ res
->offset
+
96 .size_B
= buf
->buffer_size
- res
->offset
,
97 .format
= ssbo
? ISL_FORMAT_RAW
98 : ISL_FORMAT_R32G32B32A32_FLOAT
,
99 .swizzle
= ISL_SWIZZLE_IDENTITY
,
101 .mocs
= ice
->vtbl
.mocs(res
->bo
));
105 get_aoa_deref_offset(nir_builder
*b
,
106 nir_deref_instr
*deref
,
109 unsigned array_size
= elem_size
;
110 nir_ssa_def
*offset
= nir_imm_int(b
, 0);
112 while (deref
->deref_type
!= nir_deref_type_var
) {
113 assert(deref
->deref_type
== nir_deref_type_array
);
115 /* This level's element size is the previous level's array size */
116 nir_ssa_def
*index
= nir_ssa_for_src(b
, deref
->arr
.index
, 1);
117 assert(deref
->arr
.index
.ssa
);
118 offset
= nir_iadd(b
, offset
,
119 nir_imul(b
, index
, nir_imm_int(b
, array_size
)));
121 deref
= nir_deref_instr_parent(deref
);
122 assert(glsl_type_is_array(deref
->type
));
123 array_size
*= glsl_get_length(deref
->type
);
126 /* Accessing an invalid surface index with the dataport can result in a
127 * hang. According to the spec "if the index used to select an individual
128 * element is negative or greater than or equal to the size of the array,
129 * the results of the operation are undefined but may not lead to
130 * termination" -- which is one of the possible outcomes of the hang.
131 * Clamp the index to prevent access outside of the array bounds.
133 return nir_umin(b
, offset
, nir_imm_int(b
, array_size
- elem_size
));
137 iris_lower_storage_image_derefs(nir_shader
*nir
)
139 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
142 nir_builder_init(&b
, impl
);
144 nir_foreach_block(block
, impl
) {
145 nir_foreach_instr_safe(instr
, block
) {
146 if (instr
->type
!= nir_instr_type_intrinsic
)
149 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
150 switch (intrin
->intrinsic
) {
151 case nir_intrinsic_image_deref_load
:
152 case nir_intrinsic_image_deref_store
:
153 case nir_intrinsic_image_deref_atomic_add
:
154 case nir_intrinsic_image_deref_atomic_min
:
155 case nir_intrinsic_image_deref_atomic_max
:
156 case nir_intrinsic_image_deref_atomic_and
:
157 case nir_intrinsic_image_deref_atomic_or
:
158 case nir_intrinsic_image_deref_atomic_xor
:
159 case nir_intrinsic_image_deref_atomic_exchange
:
160 case nir_intrinsic_image_deref_atomic_comp_swap
:
161 case nir_intrinsic_image_deref_size
:
162 case nir_intrinsic_image_deref_samples
:
163 case nir_intrinsic_image_deref_load_raw_intel
:
164 case nir_intrinsic_image_deref_store_raw_intel
: {
165 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
166 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
168 b
.cursor
= nir_before_instr(&intrin
->instr
);
170 nir_iadd(&b
, nir_imm_int(&b
, var
->data
.driver_location
),
171 get_aoa_deref_offset(&b
, deref
, 1));
172 nir_rewrite_image_intrinsic(intrin
, index
, false);
183 // XXX: need unify_interfaces() at link time...
186 * Fix an uncompiled shader's stream output info.
188 * Core Gallium stores output->register_index as a "slot" number, where
189 * slots are assigned consecutively to all outputs in info->outputs_written.
190 * This naive packing of outputs doesn't work for us - we too have slots,
191 * but the layout is defined by the VUE map, which we won't have until we
192 * compile a specific shader variant. So, we remap these and simply store
193 * VARYING_SLOT_* in our copy's output->register_index fields.
195 * We also fix up VARYING_SLOT_{LAYER,VIEWPORT,PSIZ} to select the Y/Z/W
196 * components of our VUE header. See brw_vue_map.c for the layout.
199 update_so_info(struct pipe_stream_output_info
*so_info
,
200 uint64_t outputs_written
)
202 uint8_t reverse_map
[64] = {};
204 while (outputs_written
) {
205 reverse_map
[slot
++] = u_bit_scan64(&outputs_written
);
208 for (unsigned i
= 0; i
< so_info
->num_outputs
; i
++) {
209 struct pipe_stream_output
*output
= &so_info
->output
[i
];
211 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
212 output
->register_index
= reverse_map
[output
->register_index
];
214 /* The VUE header contains three scalar fields packed together:
215 * - gl_PointSize is stored in VARYING_SLOT_PSIZ.w
216 * - gl_Layer is stored in VARYING_SLOT_PSIZ.y
217 * - gl_ViewportIndex is stored in VARYING_SLOT_PSIZ.z
219 switch (output
->register_index
) {
220 case VARYING_SLOT_LAYER
:
221 assert(output
->num_components
== 1);
222 output
->register_index
= VARYING_SLOT_PSIZ
;
223 output
->start_component
= 1;
225 case VARYING_SLOT_VIEWPORT
:
226 assert(output
->num_components
== 1);
227 output
->register_index
= VARYING_SLOT_PSIZ
;
228 output
->start_component
= 2;
230 case VARYING_SLOT_PSIZ
:
231 assert(output
->num_components
== 1);
232 output
->start_component
= 3;
236 //info->outputs_written |= 1ull << output->register_index;
241 setup_vec4_image_sysval(uint32_t *sysvals
, uint32_t idx
,
242 unsigned offset
, unsigned n
)
244 assert(offset
% sizeof(uint32_t) == 0);
246 for (unsigned i
= 0; i
< n
; ++i
)
247 sysvals
[i
] = BRW_PARAM_IMAGE(idx
, offset
/ sizeof(uint32_t) + i
);
249 for (unsigned i
= n
; i
< 4; ++i
)
250 sysvals
[i
] = BRW_PARAM_BUILTIN_ZERO
;
254 * Associate NIR uniform variables with the prog_data->param[] mechanism
255 * used by the backend. Also, decide which UBOs we'd like to push in an
256 * ideal situation (though the backend can reduce this).
259 iris_setup_uniforms(const struct brw_compiler
*compiler
,
262 struct brw_stage_prog_data
*prog_data
,
263 enum brw_param_builtin
**out_system_values
,
264 unsigned *out_num_system_values
,
265 unsigned *out_num_cbufs
)
267 UNUSED
const struct gen_device_info
*devinfo
= compiler
->devinfo
;
269 /* The intel compiler assumes that num_uniforms is in bytes. For
270 * scalar that means 4 bytes per uniform slot.
272 * Ref: brw_nir_lower_uniforms, type_size_scalar_bytes.
274 nir
->num_uniforms
*= 4;
276 const unsigned IRIS_MAX_SYSTEM_VALUES
=
277 PIPE_MAX_SHADER_IMAGES
* BRW_IMAGE_PARAM_SIZE
;
278 enum brw_param_builtin
*system_values
=
279 rzalloc_array(mem_ctx
, enum brw_param_builtin
, IRIS_MAX_SYSTEM_VALUES
);
280 unsigned num_system_values
= 0;
282 unsigned patch_vert_idx
= -1;
283 unsigned ucp_idx
[IRIS_MAX_CLIP_PLANES
];
284 unsigned img_idx
[PIPE_MAX_SHADER_IMAGES
];
285 memset(ucp_idx
, -1, sizeof(ucp_idx
));
286 memset(img_idx
, -1, sizeof(img_idx
));
288 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
291 nir_builder_init(&b
, impl
);
293 b
.cursor
= nir_before_block(nir_start_block(impl
));
294 nir_ssa_def
*temp_ubo_name
= nir_ssa_undef(&b
, 1, 32);
295 nir_ssa_def
*temp_const_ubo_name
= NULL
;
297 /* Turn system value intrinsics into uniforms */
298 nir_foreach_block(block
, impl
) {
299 nir_foreach_instr_safe(instr
, block
) {
300 if (instr
->type
!= nir_instr_type_intrinsic
)
303 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
306 switch (intrin
->intrinsic
) {
307 case nir_intrinsic_load_constant
: {
308 /* This one is special because it reads from the shader constant
309 * data and not cbuf0 which gallium uploads for us.
311 b
.cursor
= nir_before_instr(instr
);
312 nir_ssa_def
*offset
=
313 nir_iadd_imm(&b
, nir_ssa_for_src(&b
, intrin
->src
[0], 1),
314 nir_intrinsic_base(intrin
));
316 if (temp_const_ubo_name
== NULL
)
317 temp_const_ubo_name
= nir_imm_int(&b
, 0);
319 nir_intrinsic_instr
*load_ubo
=
320 nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_load_ubo
);
321 load_ubo
->num_components
= intrin
->num_components
;
322 load_ubo
->src
[0] = nir_src_for_ssa(temp_const_ubo_name
);
323 load_ubo
->src
[1] = nir_src_for_ssa(offset
);
324 nir_ssa_dest_init(&load_ubo
->instr
, &load_ubo
->dest
,
325 intrin
->dest
.ssa
.num_components
,
326 intrin
->dest
.ssa
.bit_size
,
327 intrin
->dest
.ssa
.name
);
328 nir_builder_instr_insert(&b
, &load_ubo
->instr
);
330 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
331 nir_src_for_ssa(&load_ubo
->dest
.ssa
));
332 nir_instr_remove(&intrin
->instr
);
335 case nir_intrinsic_load_user_clip_plane
: {
336 unsigned ucp
= nir_intrinsic_ucp_id(intrin
);
338 if (ucp_idx
[ucp
] == -1) {
339 ucp_idx
[ucp
] = num_system_values
;
340 num_system_values
+= 4;
343 for (int i
= 0; i
< 4; i
++) {
344 system_values
[ucp_idx
[ucp
] + i
] =
345 BRW_PARAM_BUILTIN_CLIP_PLANE(ucp
, i
);
348 b
.cursor
= nir_before_instr(instr
);
349 offset
= nir_imm_int(&b
, ucp_idx
[ucp
] * sizeof(uint32_t));
352 case nir_intrinsic_load_patch_vertices_in
:
353 if (patch_vert_idx
== -1)
354 patch_vert_idx
= num_system_values
++;
356 system_values
[patch_vert_idx
] =
357 BRW_PARAM_BUILTIN_PATCH_VERTICES_IN
;
359 b
.cursor
= nir_before_instr(instr
);
360 offset
= nir_imm_int(&b
, patch_vert_idx
* sizeof(uint32_t));
362 case nir_intrinsic_image_deref_load_param_intel
: {
363 assert(devinfo
->gen
< 9);
364 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
365 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
367 if (img_idx
[var
->data
.binding
] == -1) {
368 /* GL only allows arrays of arrays of images. */
369 assert(glsl_type_is_image(glsl_without_array(var
->type
)));
370 unsigned num_images
= MAX2(1, glsl_get_aoa_size(var
->type
));
372 for (int i
= 0; i
< num_images
; i
++) {
373 const unsigned img
= var
->data
.binding
+ i
;
375 img_idx
[img
] = num_system_values
;
376 num_system_values
+= BRW_IMAGE_PARAM_SIZE
;
378 uint32_t *img_sv
= &system_values
[img_idx
[img
]];
380 setup_vec4_image_sysval(
381 img_sv
+ BRW_IMAGE_PARAM_OFFSET_OFFSET
, img
,
382 offsetof(struct brw_image_param
, offset
), 2);
383 setup_vec4_image_sysval(
384 img_sv
+ BRW_IMAGE_PARAM_SIZE_OFFSET
, img
,
385 offsetof(struct brw_image_param
, size
), 3);
386 setup_vec4_image_sysval(
387 img_sv
+ BRW_IMAGE_PARAM_STRIDE_OFFSET
, img
,
388 offsetof(struct brw_image_param
, stride
), 4);
389 setup_vec4_image_sysval(
390 img_sv
+ BRW_IMAGE_PARAM_TILING_OFFSET
, img
,
391 offsetof(struct brw_image_param
, tiling
), 3);
392 setup_vec4_image_sysval(
393 img_sv
+ BRW_IMAGE_PARAM_SWIZZLING_OFFSET
, img
,
394 offsetof(struct brw_image_param
, swizzling
), 2);
398 b
.cursor
= nir_before_instr(instr
);
399 offset
= nir_iadd(&b
,
400 get_aoa_deref_offset(&b
, deref
, BRW_IMAGE_PARAM_SIZE
* 4),
401 nir_imm_int(&b
, img_idx
[var
->data
.binding
] * 4 +
402 nir_intrinsic_base(intrin
) * 16));
409 unsigned comps
= nir_intrinsic_dest_components(intrin
);
411 nir_intrinsic_instr
*load
=
412 nir_intrinsic_instr_create(nir
, nir_intrinsic_load_ubo
);
413 load
->num_components
= comps
;
414 load
->src
[0] = nir_src_for_ssa(temp_ubo_name
);
415 load
->src
[1] = nir_src_for_ssa(offset
);
416 nir_ssa_dest_init(&load
->instr
, &load
->dest
, comps
, 32, NULL
);
417 nir_builder_instr_insert(&b
, &load
->instr
);
418 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
419 nir_src_for_ssa(&load
->dest
.ssa
));
420 nir_instr_remove(instr
);
424 nir_validate_shader(nir
, "before remapping");
426 /* Uniforms are stored in constant buffer 0, the
427 * user-facing UBOs are indexed by one. So if any constant buffer is
428 * needed, the constant buffer 0 will be needed, so account for it.
430 unsigned num_cbufs
= nir
->info
.num_ubos
;
431 if (num_cbufs
|| nir
->num_uniforms
)
434 /* Place the new params in a new cbuf. */
435 if (num_system_values
> 0) {
436 unsigned sysval_cbuf_index
= num_cbufs
;
439 system_values
= reralloc(mem_ctx
, system_values
, enum brw_param_builtin
,
442 nir_foreach_block(block
, impl
) {
443 nir_foreach_instr_safe(instr
, block
) {
444 if (instr
->type
!= nir_instr_type_intrinsic
)
447 nir_intrinsic_instr
*load
= nir_instr_as_intrinsic(instr
);
449 if (load
->intrinsic
!= nir_intrinsic_load_ubo
)
452 b
.cursor
= nir_before_instr(instr
);
454 assert(load
->src
[0].is_ssa
);
456 if (load
->src
[0].ssa
== temp_ubo_name
) {
457 nir_ssa_def
*imm
= nir_imm_int(&b
, sysval_cbuf_index
);
458 nir_instr_rewrite_src(instr
, &load
->src
[0],
459 nir_src_for_ssa(imm
));
464 /* We need to fold the new iadds for brw_nir_analyze_ubo_ranges */
465 nir_opt_constant_folding(nir
);
467 ralloc_free(system_values
);
468 system_values
= NULL
;
471 assert(num_cbufs
< PIPE_MAX_CONSTANT_BUFFERS
);
472 nir_validate_shader(nir
, "after remap");
474 /* We don't use params[], but fs_visitor::nir_setup_uniforms() asserts
475 * about it for compute shaders, so go ahead and make some fake ones
476 * which the backend will dead code eliminate.
478 prog_data
->nr_params
= nir
->num_uniforms
/ 4;
479 prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t, prog_data
->nr_params
);
481 /* Constant loads (if any) need to go at the end of the constant buffers so
482 * we need to know num_cbufs before we can lower to them.
484 if (temp_const_ubo_name
!= NULL
) {
485 nir_load_const_instr
*const_ubo_index
=
486 nir_instr_as_load_const(temp_const_ubo_name
->parent_instr
);
487 assert(const_ubo_index
->def
.bit_size
== 32);
488 const_ubo_index
->value
[0].u32
= num_cbufs
;
491 *out_system_values
= system_values
;
492 *out_num_system_values
= num_system_values
;
493 *out_num_cbufs
= num_cbufs
;
496 static const char *surface_group_names
[] = {
497 [IRIS_SURFACE_GROUP_RENDER_TARGET
] = "render target",
498 [IRIS_SURFACE_GROUP_CS_WORK_GROUPS
] = "CS work groups",
499 [IRIS_SURFACE_GROUP_TEXTURE
] = "texture",
500 [IRIS_SURFACE_GROUP_UBO
] = "ubo",
501 [IRIS_SURFACE_GROUP_SSBO
] = "ssbo",
502 [IRIS_SURFACE_GROUP_IMAGE
] = "image",
506 iris_print_binding_table(FILE *fp
, const char *name
,
507 const struct iris_binding_table
*bt
)
509 STATIC_ASSERT(ARRAY_SIZE(surface_group_names
) == IRIS_SURFACE_GROUP_COUNT
);
512 uint32_t compacted
= 0;
514 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++) {
515 uint32_t size
= bt
->sizes
[i
];
518 compacted
+= util_bitcount64(bt
->used_mask
[i
]);
522 fprintf(fp
, "Binding table for %s is empty\n\n", name
);
526 if (total
!= compacted
) {
527 fprintf(fp
, "Binding table for %s "
528 "(compacted to %u entries from %u entries)\n",
529 name
, compacted
, total
);
531 fprintf(fp
, "Binding table for %s (%u entries)\n", name
, total
);
535 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++) {
536 uint64_t mask
= bt
->used_mask
[i
];
538 int index
= u_bit_scan64(&mask
);
539 fprintf(fp
, " [%u] %s #%d\n", entry
++, surface_group_names
[i
], index
);
546 /* Max elements in a surface group. */
547 SURFACE_GROUP_MAX_ELEMENTS
= 64,
551 * Map a <group, index> pair to a binding table index.
553 * For example: <UBO, 5> => binding table index 12
556 iris_group_index_to_bti(const struct iris_binding_table
*bt
,
557 enum iris_surface_group group
, uint32_t index
)
559 assert(index
< bt
->sizes
[group
]);
560 uint64_t mask
= bt
->used_mask
[group
];
561 uint64_t bit
= 1ull << index
;
563 return bt
->offsets
[group
] + util_bitcount64((bit
- 1) & mask
);
565 return IRIS_SURFACE_NOT_USED
;
570 * Map a binding table index back to a <group, index> pair.
572 * For example: binding table index 12 => <UBO, 5>
575 iris_bti_to_group_index(const struct iris_binding_table
*bt
,
576 enum iris_surface_group group
, uint32_t bti
)
578 uint64_t used_mask
= bt
->used_mask
[group
];
579 assert(bti
>= bt
->offsets
[group
]);
581 uint32_t c
= bti
- bt
->offsets
[group
];
583 int i
= u_bit_scan64(&used_mask
);
589 return IRIS_SURFACE_NOT_USED
;
593 rewrite_src_with_bti(nir_builder
*b
, struct iris_binding_table
*bt
,
594 nir_instr
*instr
, nir_src
*src
,
595 enum iris_surface_group group
)
597 assert(bt
->sizes
[group
] > 0);
599 b
->cursor
= nir_before_instr(instr
);
601 if (nir_src_is_const(*src
)) {
602 uint32_t index
= nir_src_as_uint(*src
);
603 bti
= nir_imm_intN_t(b
, iris_group_index_to_bti(bt
, group
, index
),
606 /* Indirect usage makes all the surfaces of the group to be available,
607 * so we can just add the base.
609 assert(bt
->used_mask
[group
] == BITFIELD64_MASK(bt
->sizes
[group
]));
610 bti
= nir_iadd_imm(b
, src
->ssa
, bt
->offsets
[group
]);
612 nir_instr_rewrite_src(instr
, src
, nir_src_for_ssa(bti
));
616 mark_used_with_src(struct iris_binding_table
*bt
, nir_src
*src
,
617 enum iris_surface_group group
)
619 assert(bt
->sizes
[group
] > 0);
621 if (nir_src_is_const(*src
)) {
622 uint64_t index
= nir_src_as_uint(*src
);
623 assert(index
< bt
->sizes
[group
]);
624 bt
->used_mask
[group
] |= 1ull << index
;
626 /* There's an indirect usage, we need all the surfaces. */
627 bt
->used_mask
[group
] = BITFIELD64_MASK(bt
->sizes
[group
]);
632 skip_compacting_binding_tables(void)
634 static int skip
= -1;
636 skip
= env_var_as_boolean("INTEL_DISABLE_COMPACT_BINDING_TABLE", false);
641 * Set up the binding table indices and apply to the shader.
644 iris_setup_binding_table(struct nir_shader
*nir
,
645 struct iris_binding_table
*bt
,
646 unsigned num_render_targets
,
647 unsigned num_system_values
,
650 const struct shader_info
*info
= &nir
->info
;
652 memset(bt
, 0, sizeof(*bt
));
654 /* Set the sizes for each surface group. For some groups, we already know
655 * upfront how many will be used, so mark them.
657 if (info
->stage
== MESA_SHADER_FRAGMENT
) {
658 bt
->sizes
[IRIS_SURFACE_GROUP_RENDER_TARGET
] = num_render_targets
;
659 /* All render targets used. */
660 bt
->used_mask
[IRIS_SURFACE_GROUP_RENDER_TARGET
] =
661 BITFIELD64_MASK(num_render_targets
);
662 } else if (info
->stage
== MESA_SHADER_COMPUTE
) {
663 bt
->sizes
[IRIS_SURFACE_GROUP_CS_WORK_GROUPS
] = 1;
666 bt
->sizes
[IRIS_SURFACE_GROUP_TEXTURE
] = util_last_bit(info
->textures_used
);
667 bt
->used_mask
[IRIS_SURFACE_GROUP_TEXTURE
] = info
->textures_used
;
669 bt
->sizes
[IRIS_SURFACE_GROUP_IMAGE
] = info
->num_images
;
671 /* Allocate an extra slot in the UBO section for NIR constants.
672 * Binding table compaction will remove it if unnecessary.
674 * We don't include them in iris_compiled_shader::num_cbufs because
675 * they are uploaded separately from shs->constbuf[], but from a shader
676 * point of view, they're another UBO (at the end of the section).
678 bt
->sizes
[IRIS_SURFACE_GROUP_UBO
] = num_cbufs
+ 1;
680 /* The first IRIS_MAX_ABOs indices in the SSBO group are for atomics, real
681 * SSBOs start after that. Compaction will remove unused ABOs.
683 bt
->sizes
[IRIS_SURFACE_GROUP_SSBO
] = IRIS_MAX_ABOS
+ info
->num_ssbos
;
685 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++)
686 assert(bt
->sizes
[i
] <= SURFACE_GROUP_MAX_ELEMENTS
);
688 /* Mark surfaces used for the cases we don't have the information available
691 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
692 nir_foreach_block (block
, impl
) {
693 nir_foreach_instr (instr
, block
) {
694 if (instr
->type
!= nir_instr_type_intrinsic
)
697 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
698 switch (intrin
->intrinsic
) {
699 case nir_intrinsic_load_num_work_groups
:
700 bt
->used_mask
[IRIS_SURFACE_GROUP_CS_WORK_GROUPS
] = 1;
703 case nir_intrinsic_image_size
:
704 case nir_intrinsic_image_load
:
705 case nir_intrinsic_image_store
:
706 case nir_intrinsic_image_atomic_add
:
707 case nir_intrinsic_image_atomic_min
:
708 case nir_intrinsic_image_atomic_max
:
709 case nir_intrinsic_image_atomic_and
:
710 case nir_intrinsic_image_atomic_or
:
711 case nir_intrinsic_image_atomic_xor
:
712 case nir_intrinsic_image_atomic_exchange
:
713 case nir_intrinsic_image_atomic_comp_swap
:
714 case nir_intrinsic_image_load_raw_intel
:
715 case nir_intrinsic_image_store_raw_intel
:
716 mark_used_with_src(bt
, &intrin
->src
[0], IRIS_SURFACE_GROUP_IMAGE
);
719 case nir_intrinsic_load_ubo
:
720 mark_used_with_src(bt
, &intrin
->src
[0], IRIS_SURFACE_GROUP_UBO
);
723 case nir_intrinsic_store_ssbo
:
724 mark_used_with_src(bt
, &intrin
->src
[1], IRIS_SURFACE_GROUP_SSBO
);
727 case nir_intrinsic_get_buffer_size
:
728 case nir_intrinsic_ssbo_atomic_add
:
729 case nir_intrinsic_ssbo_atomic_imin
:
730 case nir_intrinsic_ssbo_atomic_umin
:
731 case nir_intrinsic_ssbo_atomic_imax
:
732 case nir_intrinsic_ssbo_atomic_umax
:
733 case nir_intrinsic_ssbo_atomic_and
:
734 case nir_intrinsic_ssbo_atomic_or
:
735 case nir_intrinsic_ssbo_atomic_xor
:
736 case nir_intrinsic_ssbo_atomic_exchange
:
737 case nir_intrinsic_ssbo_atomic_comp_swap
:
738 case nir_intrinsic_ssbo_atomic_fmin
:
739 case nir_intrinsic_ssbo_atomic_fmax
:
740 case nir_intrinsic_ssbo_atomic_fcomp_swap
:
741 case nir_intrinsic_load_ssbo
:
742 mark_used_with_src(bt
, &intrin
->src
[0], IRIS_SURFACE_GROUP_SSBO
);
751 /* When disable we just mark everything as used. */
752 if (unlikely(skip_compacting_binding_tables())) {
753 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++)
754 bt
->used_mask
[i
] = BITFIELD64_MASK(bt
->sizes
[i
]);
757 /* Calculate the offsets and the binding table size based on the used
758 * surfaces. After this point, the functions to go between "group indices"
759 * and binding table indices can be used.
762 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++) {
763 if (bt
->used_mask
[i
] != 0) {
764 bt
->offsets
[i
] = next
;
765 next
+= util_bitcount64(bt
->used_mask
[i
]);
768 bt
->size_bytes
= next
* 4;
770 if (unlikely(INTEL_DEBUG
& DEBUG_BT
)) {
771 iris_print_binding_table(stderr
, gl_shader_stage_name(info
->stage
), bt
);
774 /* Apply the binding table indices. The backend compiler is not expected
775 * to change those, as we haven't set any of the *_start entries in brw
779 nir_builder_init(&b
, impl
);
781 nir_foreach_block (block
, impl
) {
782 nir_foreach_instr (instr
, block
) {
783 if (instr
->type
== nir_instr_type_tex
) {
784 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
786 iris_group_index_to_bti(bt
, IRIS_SURFACE_GROUP_TEXTURE
,
791 if (instr
->type
!= nir_instr_type_intrinsic
)
794 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
795 switch (intrin
->intrinsic
) {
796 case nir_intrinsic_image_size
:
797 case nir_intrinsic_image_load
:
798 case nir_intrinsic_image_store
:
799 case nir_intrinsic_image_atomic_add
:
800 case nir_intrinsic_image_atomic_min
:
801 case nir_intrinsic_image_atomic_max
:
802 case nir_intrinsic_image_atomic_and
:
803 case nir_intrinsic_image_atomic_or
:
804 case nir_intrinsic_image_atomic_xor
:
805 case nir_intrinsic_image_atomic_exchange
:
806 case nir_intrinsic_image_atomic_comp_swap
:
807 case nir_intrinsic_image_load_raw_intel
:
808 case nir_intrinsic_image_store_raw_intel
:
809 rewrite_src_with_bti(&b
, bt
, instr
, &intrin
->src
[0],
810 IRIS_SURFACE_GROUP_IMAGE
);
813 case nir_intrinsic_load_ubo
:
814 rewrite_src_with_bti(&b
, bt
, instr
, &intrin
->src
[0],
815 IRIS_SURFACE_GROUP_UBO
);
818 case nir_intrinsic_store_ssbo
:
819 rewrite_src_with_bti(&b
, bt
, instr
, &intrin
->src
[1],
820 IRIS_SURFACE_GROUP_SSBO
);
823 case nir_intrinsic_get_buffer_size
:
824 case nir_intrinsic_ssbo_atomic_add
:
825 case nir_intrinsic_ssbo_atomic_imin
:
826 case nir_intrinsic_ssbo_atomic_umin
:
827 case nir_intrinsic_ssbo_atomic_imax
:
828 case nir_intrinsic_ssbo_atomic_umax
:
829 case nir_intrinsic_ssbo_atomic_and
:
830 case nir_intrinsic_ssbo_atomic_or
:
831 case nir_intrinsic_ssbo_atomic_xor
:
832 case nir_intrinsic_ssbo_atomic_exchange
:
833 case nir_intrinsic_ssbo_atomic_comp_swap
:
834 case nir_intrinsic_ssbo_atomic_fmin
:
835 case nir_intrinsic_ssbo_atomic_fmax
:
836 case nir_intrinsic_ssbo_atomic_fcomp_swap
:
837 case nir_intrinsic_load_ssbo
:
838 rewrite_src_with_bti(&b
, bt
, instr
, &intrin
->src
[0],
839 IRIS_SURFACE_GROUP_SSBO
);
850 iris_debug_recompile(struct iris_context
*ice
,
851 struct shader_info
*info
,
852 const struct brw_base_prog_key
*key
)
854 struct iris_screen
*screen
= (struct iris_screen
*) ice
->ctx
.screen
;
855 const struct brw_compiler
*c
= screen
->compiler
;
860 c
->shader_perf_log(&ice
->dbg
, "Recompiling %s shader for program %s: %s\n",
861 _mesa_shader_stage_to_string(info
->stage
),
862 info
->name
? info
->name
: "(no identifier)",
863 info
->label
? info
->label
: "");
865 const void *old_key
=
866 iris_find_previous_compile(ice
, info
->stage
, key
->program_string_id
);
868 brw_debug_key_recompile(c
, &ice
->dbg
, info
->stage
, old_key
, key
);
873 * Compile a vertex shader, and upload the assembly.
875 static struct iris_compiled_shader
*
876 iris_compile_vs(struct iris_context
*ice
,
877 struct iris_uncompiled_shader
*ish
,
878 const struct brw_vs_prog_key
*key
)
880 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
881 const struct brw_compiler
*compiler
= screen
->compiler
;
882 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
883 void *mem_ctx
= ralloc_context(NULL
);
884 struct brw_vs_prog_data
*vs_prog_data
=
885 rzalloc(mem_ctx
, struct brw_vs_prog_data
);
886 struct brw_vue_prog_data
*vue_prog_data
= &vs_prog_data
->base
;
887 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
888 enum brw_param_builtin
*system_values
;
889 unsigned num_system_values
;
892 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
894 if (key
->nr_userclip_plane_consts
) {
895 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
896 nir_lower_clip_vs(nir
, (1 << key
->nr_userclip_plane_consts
) - 1, true);
897 nir_lower_io_to_temporaries(nir
, impl
, true, false);
898 nir_lower_global_vars_to_local(nir
);
899 nir_lower_vars_to_ssa(nir
);
900 nir_shader_gather_info(nir
, impl
);
903 prog_data
->use_alt_mode
= ish
->use_alt_mode
;
905 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
906 &num_system_values
, &num_cbufs
);
908 struct iris_binding_table bt
;
909 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
910 num_system_values
, num_cbufs
);
912 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
914 brw_compute_vue_map(devinfo
,
915 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
916 nir
->info
.separate_shader
);
918 /* Don't tell the backend about our clip plane constants, we've already
919 * lowered them in NIR and we don't want it doing it again.
921 struct brw_vs_prog_key key_no_ucp
= *key
;
922 key_no_ucp
.nr_userclip_plane_consts
= 0;
924 char *error_str
= NULL
;
925 const unsigned *program
=
926 brw_compile_vs(compiler
, &ice
->dbg
, mem_ctx
, &key_no_ucp
, vs_prog_data
,
927 nir
, -1, &error_str
);
928 if (program
== NULL
) {
929 dbg_printf("Failed to compile vertex shader: %s\n", error_str
);
930 ralloc_free(mem_ctx
);
934 if (ish
->compiled_once
) {
935 iris_debug_recompile(ice
, &nir
->info
, &key
->base
);
937 ish
->compiled_once
= true;
941 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
942 &vue_prog_data
->vue_map
);
944 struct iris_compiled_shader
*shader
=
945 iris_upload_shader(ice
, IRIS_CACHE_VS
, sizeof(*key
), key
, program
,
946 prog_data
, so_decls
, system_values
, num_system_values
,
949 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
951 ralloc_free(mem_ctx
);
956 * Update the current vertex shader variant.
958 * Fill out the key, look in the cache, compile and bind if needed.
961 iris_update_compiled_vs(struct iris_context
*ice
)
963 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_VERTEX
];
964 struct iris_uncompiled_shader
*ish
=
965 ice
->shaders
.uncompiled
[MESA_SHADER_VERTEX
];
966 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
967 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
969 struct brw_vs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
970 ice
->vtbl
.populate_vs_key(ice
, &ish
->nir
->info
, &key
);
972 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_VS
];
973 struct iris_compiled_shader
*shader
=
974 iris_find_cached_shader(ice
, IRIS_CACHE_VS
, sizeof(key
), &key
);
977 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
980 shader
= iris_compile_vs(ice
, ish
, &key
);
983 ice
->shaders
.prog
[IRIS_CACHE_VS
] = shader
;
984 ice
->state
.dirty
|= IRIS_DIRTY_VS
|
985 IRIS_DIRTY_BINDINGS_VS
|
986 IRIS_DIRTY_CONSTANTS_VS
|
988 shs
->sysvals_need_upload
= true;
990 const struct brw_vs_prog_data
*vs_prog_data
=
991 (void *) shader
->prog_data
;
992 const bool uses_draw_params
= vs_prog_data
->uses_firstvertex
||
993 vs_prog_data
->uses_baseinstance
;
994 const bool uses_derived_draw_params
= vs_prog_data
->uses_drawid
||
995 vs_prog_data
->uses_is_indexed_draw
;
996 const bool needs_sgvs_element
= uses_draw_params
||
997 vs_prog_data
->uses_instanceid
||
998 vs_prog_data
->uses_vertexid
;
999 bool needs_edge_flag
= false;
1000 nir_foreach_variable(var
, &ish
->nir
->inputs
) {
1001 if (var
->data
.location
== VERT_ATTRIB_EDGEFLAG
)
1002 needs_edge_flag
= true;
1005 if (ice
->state
.vs_uses_draw_params
!= uses_draw_params
||
1006 ice
->state
.vs_uses_derived_draw_params
!= uses_derived_draw_params
||
1007 ice
->state
.vs_needs_edge_flag
!= needs_edge_flag
) {
1008 ice
->state
.dirty
|= IRIS_DIRTY_VERTEX_BUFFERS
|
1009 IRIS_DIRTY_VERTEX_ELEMENTS
;
1011 ice
->state
.vs_uses_draw_params
= uses_draw_params
;
1012 ice
->state
.vs_uses_derived_draw_params
= uses_derived_draw_params
;
1013 ice
->state
.vs_needs_sgvs_element
= needs_sgvs_element
;
1014 ice
->state
.vs_needs_edge_flag
= needs_edge_flag
;
1019 * Get the shader_info for a given stage, or NULL if the stage is disabled.
1021 const struct shader_info
*
1022 iris_get_shader_info(const struct iris_context
*ice
, gl_shader_stage stage
)
1024 const struct iris_uncompiled_shader
*ish
= ice
->shaders
.uncompiled
[stage
];
1029 const nir_shader
*nir
= ish
->nir
;
1034 * Get the union of TCS output and TES input slots.
1036 * TCS and TES need to agree on a common URB entry layout. In particular,
1037 * the data for all patch vertices is stored in a single URB entry (unlike
1038 * GS which has one entry per input vertex). This means that per-vertex
1039 * array indexing needs a stride.
1041 * SSO requires locations to match, but doesn't require the number of
1042 * outputs/inputs to match (in fact, the TCS often has extra outputs).
1043 * So, we need to take the extra step of unifying these on the fly.
1046 get_unified_tess_slots(const struct iris_context
*ice
,
1047 uint64_t *per_vertex_slots
,
1048 uint32_t *per_patch_slots
)
1050 const struct shader_info
*tcs
=
1051 iris_get_shader_info(ice
, MESA_SHADER_TESS_CTRL
);
1052 const struct shader_info
*tes
=
1053 iris_get_shader_info(ice
, MESA_SHADER_TESS_EVAL
);
1055 *per_vertex_slots
= tes
->inputs_read
;
1056 *per_patch_slots
= tes
->patch_inputs_read
;
1059 *per_vertex_slots
|= tcs
->outputs_written
;
1060 *per_patch_slots
|= tcs
->patch_outputs_written
;
1065 * Compile a tessellation control shader, and upload the assembly.
1067 static struct iris_compiled_shader
*
1068 iris_compile_tcs(struct iris_context
*ice
,
1069 struct iris_uncompiled_shader
*ish
,
1070 const struct brw_tcs_prog_key
*key
)
1072 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1073 const struct brw_compiler
*compiler
= screen
->compiler
;
1074 const struct nir_shader_compiler_options
*options
=
1075 compiler
->glsl_compiler_options
[MESA_SHADER_TESS_CTRL
].NirOptions
;
1076 void *mem_ctx
= ralloc_context(NULL
);
1077 struct brw_tcs_prog_data
*tcs_prog_data
=
1078 rzalloc(mem_ctx
, struct brw_tcs_prog_data
);
1079 struct brw_vue_prog_data
*vue_prog_data
= &tcs_prog_data
->base
;
1080 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
1081 enum brw_param_builtin
*system_values
= NULL
;
1082 unsigned num_system_values
= 0;
1083 unsigned num_cbufs
= 0;
1087 struct iris_binding_table bt
;
1090 nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1092 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1093 &num_system_values
, &num_cbufs
);
1094 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
1095 num_system_values
, num_cbufs
);
1096 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
1098 nir
= brw_nir_create_passthrough_tcs(mem_ctx
, compiler
, options
, key
);
1100 /* Reserve space for passing the default tess levels as constants. */
1102 num_system_values
= 8;
1104 rzalloc_array(mem_ctx
, enum brw_param_builtin
, num_system_values
);
1105 prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t, num_system_values
);
1106 prog_data
->nr_params
= num_system_values
;
1108 if (key
->tes_primitive_mode
== GL_QUADS
) {
1109 for (int i
= 0; i
< 4; i
++)
1110 system_values
[7 - i
] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X
+ i
;
1112 system_values
[3] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X
;
1113 system_values
[2] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_Y
;
1114 } else if (key
->tes_primitive_mode
== GL_TRIANGLES
) {
1115 for (int i
= 0; i
< 3; i
++)
1116 system_values
[7 - i
] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X
+ i
;
1118 system_values
[4] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X
;
1120 assert(key
->tes_primitive_mode
== GL_ISOLINES
);
1121 system_values
[7] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_Y
;
1122 system_values
[6] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X
;
1125 /* Manually setup the TCS binding table. */
1126 memset(&bt
, 0, sizeof(bt
));
1127 bt
.sizes
[IRIS_SURFACE_GROUP_UBO
] = 1;
1128 bt
.used_mask
[IRIS_SURFACE_GROUP_UBO
] = 1;
1131 prog_data
->ubo_ranges
[0].length
= 1;
1134 char *error_str
= NULL
;
1135 const unsigned *program
=
1136 brw_compile_tcs(compiler
, &ice
->dbg
, mem_ctx
, key
, tcs_prog_data
, nir
,
1138 if (program
== NULL
) {
1139 dbg_printf("Failed to compile control shader: %s\n", error_str
);
1140 ralloc_free(mem_ctx
);
1145 if (ish
->compiled_once
) {
1146 iris_debug_recompile(ice
, &nir
->info
, &key
->base
);
1148 ish
->compiled_once
= true;
1152 struct iris_compiled_shader
*shader
=
1153 iris_upload_shader(ice
, IRIS_CACHE_TCS
, sizeof(*key
), key
, program
,
1154 prog_data
, NULL
, system_values
, num_system_values
,
1158 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1160 ralloc_free(mem_ctx
);
1165 * Update the current tessellation control shader variant.
1167 * Fill out the key, look in the cache, compile and bind if needed.
1170 iris_update_compiled_tcs(struct iris_context
*ice
)
1172 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_TESS_CTRL
];
1173 struct iris_uncompiled_shader
*tcs
=
1174 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_CTRL
];
1175 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1176 const struct brw_compiler
*compiler
= screen
->compiler
;
1177 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1179 const struct shader_info
*tes_info
=
1180 iris_get_shader_info(ice
, MESA_SHADER_TESS_EVAL
);
1181 struct brw_tcs_prog_key key
= {
1182 KEY_INIT_NO_ID(devinfo
->gen
),
1183 .base
.program_string_id
= tcs
? tcs
->program_id
: 0,
1184 .tes_primitive_mode
= tes_info
->tess
.primitive_mode
,
1186 !tcs
|| compiler
->use_tcs_8_patch
? ice
->state
.vertices_per_patch
: 0,
1188 get_unified_tess_slots(ice
, &key
.outputs_written
,
1189 &key
.patch_outputs_written
);
1190 ice
->vtbl
.populate_tcs_key(ice
, &key
);
1192 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_TCS
];
1193 struct iris_compiled_shader
*shader
=
1194 iris_find_cached_shader(ice
, IRIS_CACHE_TCS
, sizeof(key
), &key
);
1197 shader
= iris_disk_cache_retrieve(ice
, tcs
, &key
, sizeof(key
));
1200 shader
= iris_compile_tcs(ice
, tcs
, &key
);
1202 if (old
!= shader
) {
1203 ice
->shaders
.prog
[IRIS_CACHE_TCS
] = shader
;
1204 ice
->state
.dirty
|= IRIS_DIRTY_TCS
|
1205 IRIS_DIRTY_BINDINGS_TCS
|
1206 IRIS_DIRTY_CONSTANTS_TCS
;
1207 shs
->sysvals_need_upload
= true;
1212 * Compile a tessellation evaluation shader, and upload the assembly.
1214 static struct iris_compiled_shader
*
1215 iris_compile_tes(struct iris_context
*ice
,
1216 struct iris_uncompiled_shader
*ish
,
1217 const struct brw_tes_prog_key
*key
)
1219 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1220 const struct brw_compiler
*compiler
= screen
->compiler
;
1221 void *mem_ctx
= ralloc_context(NULL
);
1222 struct brw_tes_prog_data
*tes_prog_data
=
1223 rzalloc(mem_ctx
, struct brw_tes_prog_data
);
1224 struct brw_vue_prog_data
*vue_prog_data
= &tes_prog_data
->base
;
1225 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
1226 enum brw_param_builtin
*system_values
;
1227 unsigned num_system_values
;
1230 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1232 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1233 &num_system_values
, &num_cbufs
);
1235 struct iris_binding_table bt
;
1236 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
1237 num_system_values
, num_cbufs
);
1239 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
1241 struct brw_vue_map input_vue_map
;
1242 brw_compute_tess_vue_map(&input_vue_map
, key
->inputs_read
,
1243 key
->patch_inputs_read
);
1245 char *error_str
= NULL
;
1246 const unsigned *program
=
1247 brw_compile_tes(compiler
, &ice
->dbg
, mem_ctx
, key
, &input_vue_map
,
1248 tes_prog_data
, nir
, NULL
, -1, &error_str
);
1249 if (program
== NULL
) {
1250 dbg_printf("Failed to compile evaluation shader: %s\n", error_str
);
1251 ralloc_free(mem_ctx
);
1255 if (ish
->compiled_once
) {
1256 iris_debug_recompile(ice
, &nir
->info
, &key
->base
);
1258 ish
->compiled_once
= true;
1261 uint32_t *so_decls
=
1262 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
1263 &vue_prog_data
->vue_map
);
1266 struct iris_compiled_shader
*shader
=
1267 iris_upload_shader(ice
, IRIS_CACHE_TES
, sizeof(*key
), key
, program
,
1268 prog_data
, so_decls
, system_values
, num_system_values
,
1271 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1273 ralloc_free(mem_ctx
);
1278 * Update the current tessellation evaluation shader variant.
1280 * Fill out the key, look in the cache, compile and bind if needed.
1283 iris_update_compiled_tes(struct iris_context
*ice
)
1285 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_TESS_EVAL
];
1286 struct iris_uncompiled_shader
*ish
=
1287 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
1288 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1289 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1291 struct brw_tes_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1292 get_unified_tess_slots(ice
, &key
.inputs_read
, &key
.patch_inputs_read
);
1293 ice
->vtbl
.populate_tes_key(ice
, &key
);
1295 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_TES
];
1296 struct iris_compiled_shader
*shader
=
1297 iris_find_cached_shader(ice
, IRIS_CACHE_TES
, sizeof(key
), &key
);
1300 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
1303 shader
= iris_compile_tes(ice
, ish
, &key
);
1305 if (old
!= shader
) {
1306 ice
->shaders
.prog
[IRIS_CACHE_TES
] = shader
;
1307 ice
->state
.dirty
|= IRIS_DIRTY_TES
|
1308 IRIS_DIRTY_BINDINGS_TES
|
1309 IRIS_DIRTY_CONSTANTS_TES
;
1310 shs
->sysvals_need_upload
= true;
1313 /* TODO: Could compare and avoid flagging this. */
1314 const struct shader_info
*tes_info
= &ish
->nir
->info
;
1315 if (tes_info
->system_values_read
& (1ull << SYSTEM_VALUE_VERTICES_IN
)) {
1316 ice
->state
.dirty
|= IRIS_DIRTY_CONSTANTS_TES
;
1317 ice
->state
.shaders
[MESA_SHADER_TESS_EVAL
].sysvals_need_upload
= true;
1322 * Compile a geometry shader, and upload the assembly.
1324 static struct iris_compiled_shader
*
1325 iris_compile_gs(struct iris_context
*ice
,
1326 struct iris_uncompiled_shader
*ish
,
1327 const struct brw_gs_prog_key
*key
)
1329 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1330 const struct brw_compiler
*compiler
= screen
->compiler
;
1331 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1332 void *mem_ctx
= ralloc_context(NULL
);
1333 struct brw_gs_prog_data
*gs_prog_data
=
1334 rzalloc(mem_ctx
, struct brw_gs_prog_data
);
1335 struct brw_vue_prog_data
*vue_prog_data
= &gs_prog_data
->base
;
1336 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
1337 enum brw_param_builtin
*system_values
;
1338 unsigned num_system_values
;
1341 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1343 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1344 &num_system_values
, &num_cbufs
);
1346 struct iris_binding_table bt
;
1347 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
1348 num_system_values
, num_cbufs
);
1350 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
1352 brw_compute_vue_map(devinfo
,
1353 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
1354 nir
->info
.separate_shader
);
1356 char *error_str
= NULL
;
1357 const unsigned *program
=
1358 brw_compile_gs(compiler
, &ice
->dbg
, mem_ctx
, key
, gs_prog_data
, nir
,
1359 NULL
, -1, &error_str
);
1360 if (program
== NULL
) {
1361 dbg_printf("Failed to compile geometry shader: %s\n", error_str
);
1362 ralloc_free(mem_ctx
);
1366 if (ish
->compiled_once
) {
1367 iris_debug_recompile(ice
, &nir
->info
, &key
->base
);
1369 ish
->compiled_once
= true;
1372 uint32_t *so_decls
=
1373 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
1374 &vue_prog_data
->vue_map
);
1376 struct iris_compiled_shader
*shader
=
1377 iris_upload_shader(ice
, IRIS_CACHE_GS
, sizeof(*key
), key
, program
,
1378 prog_data
, so_decls
, system_values
, num_system_values
,
1381 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1383 ralloc_free(mem_ctx
);
1388 * Update the current geometry shader variant.
1390 * Fill out the key, look in the cache, compile and bind if needed.
1393 iris_update_compiled_gs(struct iris_context
*ice
)
1395 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_GEOMETRY
];
1396 struct iris_uncompiled_shader
*ish
=
1397 ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
];
1398 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_GS
];
1399 struct iris_compiled_shader
*shader
= NULL
;
1402 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1403 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1404 struct brw_gs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1405 ice
->vtbl
.populate_gs_key(ice
, &key
);
1408 iris_find_cached_shader(ice
, IRIS_CACHE_GS
, sizeof(key
), &key
);
1411 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
1414 shader
= iris_compile_gs(ice
, ish
, &key
);
1417 if (old
!= shader
) {
1418 ice
->shaders
.prog
[IRIS_CACHE_GS
] = shader
;
1419 ice
->state
.dirty
|= IRIS_DIRTY_GS
|
1420 IRIS_DIRTY_BINDINGS_GS
|
1421 IRIS_DIRTY_CONSTANTS_GS
;
1422 shs
->sysvals_need_upload
= true;
1427 * Compile a fragment (pixel) shader, and upload the assembly.
1429 static struct iris_compiled_shader
*
1430 iris_compile_fs(struct iris_context
*ice
,
1431 struct iris_uncompiled_shader
*ish
,
1432 const struct brw_wm_prog_key
*key
,
1433 struct brw_vue_map
*vue_map
)
1435 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1436 const struct brw_compiler
*compiler
= screen
->compiler
;
1437 void *mem_ctx
= ralloc_context(NULL
);
1438 struct brw_wm_prog_data
*fs_prog_data
=
1439 rzalloc(mem_ctx
, struct brw_wm_prog_data
);
1440 struct brw_stage_prog_data
*prog_data
= &fs_prog_data
->base
;
1441 enum brw_param_builtin
*system_values
;
1442 unsigned num_system_values
;
1445 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1447 prog_data
->use_alt_mode
= ish
->use_alt_mode
;
1449 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1450 &num_system_values
, &num_cbufs
);
1452 struct iris_binding_table bt
;
1453 iris_setup_binding_table(nir
, &bt
, MAX2(key
->nr_color_regions
, 1),
1454 num_system_values
, num_cbufs
);
1456 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
1458 char *error_str
= NULL
;
1459 const unsigned *program
=
1460 brw_compile_fs(compiler
, &ice
->dbg
, mem_ctx
, key
, fs_prog_data
,
1461 nir
, NULL
, -1, -1, -1, true, false, vue_map
, &error_str
);
1462 if (program
== NULL
) {
1463 dbg_printf("Failed to compile fragment shader: %s\n", error_str
);
1464 ralloc_free(mem_ctx
);
1468 if (ish
->compiled_once
) {
1469 iris_debug_recompile(ice
, &nir
->info
, &key
->base
);
1471 ish
->compiled_once
= true;
1474 struct iris_compiled_shader
*shader
=
1475 iris_upload_shader(ice
, IRIS_CACHE_FS
, sizeof(*key
), key
, program
,
1476 prog_data
, NULL
, system_values
, num_system_values
,
1479 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1481 ralloc_free(mem_ctx
);
1486 * Update the current fragment shader variant.
1488 * Fill out the key, look in the cache, compile and bind if needed.
1491 iris_update_compiled_fs(struct iris_context
*ice
)
1493 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_FRAGMENT
];
1494 struct iris_uncompiled_shader
*ish
=
1495 ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
];
1496 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1497 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1498 struct brw_wm_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1499 ice
->vtbl
.populate_fs_key(ice
, &ish
->nir
->info
, &key
);
1501 if (ish
->nos
& (1ull << IRIS_NOS_LAST_VUE_MAP
))
1502 key
.input_slots_valid
= ice
->shaders
.last_vue_map
->slots_valid
;
1504 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_FS
];
1505 struct iris_compiled_shader
*shader
=
1506 iris_find_cached_shader(ice
, IRIS_CACHE_FS
, sizeof(key
), &key
);
1509 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
1512 shader
= iris_compile_fs(ice
, ish
, &key
, ice
->shaders
.last_vue_map
);
1514 if (old
!= shader
) {
1515 // XXX: only need to flag CLIP if barycentric has NONPERSPECTIVE
1516 // toggles. might be able to avoid flagging SBE too.
1517 ice
->shaders
.prog
[IRIS_CACHE_FS
] = shader
;
1518 ice
->state
.dirty
|= IRIS_DIRTY_FS
|
1519 IRIS_DIRTY_BINDINGS_FS
|
1520 IRIS_DIRTY_CONSTANTS_FS
|
1524 shs
->sysvals_need_upload
= true;
1529 * Get the compiled shader for the last enabled geometry stage.
1531 * This stage is the one which will feed stream output and the rasterizer.
1533 static gl_shader_stage
1534 last_vue_stage(struct iris_context
*ice
)
1536 if (ice
->shaders
.prog
[MESA_SHADER_GEOMETRY
])
1537 return MESA_SHADER_GEOMETRY
;
1539 if (ice
->shaders
.prog
[MESA_SHADER_TESS_EVAL
])
1540 return MESA_SHADER_TESS_EVAL
;
1542 return MESA_SHADER_VERTEX
;
1546 * Update the last enabled stage's VUE map.
1548 * When the shader feeding the rasterizer's output interface changes, we
1549 * need to re-emit various packets.
1552 update_last_vue_map(struct iris_context
*ice
,
1553 struct brw_stage_prog_data
*prog_data
)
1555 struct brw_vue_prog_data
*vue_prog_data
= (void *) prog_data
;
1556 struct brw_vue_map
*vue_map
= &vue_prog_data
->vue_map
;
1557 struct brw_vue_map
*old_map
= ice
->shaders
.last_vue_map
;
1558 const uint64_t changed_slots
=
1559 (old_map
? old_map
->slots_valid
: 0ull) ^ vue_map
->slots_valid
;
1561 if (changed_slots
& VARYING_BIT_VIEWPORT
) {
1562 ice
->state
.num_viewports
=
1563 (vue_map
->slots_valid
& VARYING_BIT_VIEWPORT
) ? IRIS_MAX_VIEWPORTS
: 1;
1564 ice
->state
.dirty
|= IRIS_DIRTY_CLIP
|
1565 IRIS_DIRTY_SF_CL_VIEWPORT
|
1566 IRIS_DIRTY_CC_VIEWPORT
|
1567 IRIS_DIRTY_SCISSOR_RECT
|
1568 IRIS_DIRTY_UNCOMPILED_FS
|
1569 ice
->state
.dirty_for_nos
[IRIS_NOS_LAST_VUE_MAP
];
1572 if (changed_slots
|| (old_map
&& old_map
->separate
!= vue_map
->separate
)) {
1573 ice
->state
.dirty
|= IRIS_DIRTY_SBE
;
1576 ice
->shaders
.last_vue_map
= &vue_prog_data
->vue_map
;
1580 * Get the prog_data for a given stage, or NULL if the stage is disabled.
1582 static struct brw_vue_prog_data
*
1583 get_vue_prog_data(struct iris_context
*ice
, gl_shader_stage stage
)
1585 if (!ice
->shaders
.prog
[stage
])
1588 return (void *) ice
->shaders
.prog
[stage
]->prog_data
;
1591 // XXX: iris_compiled_shaders are space-leaking :(
1592 // XXX: do remember to unbind them if deleting them.
1595 * Update the current shader variants for the given state.
1597 * This should be called on every draw call to ensure that the correct
1598 * shaders are bound. It will also flag any dirty state triggered by
1599 * swapping out those shaders.
1602 iris_update_compiled_shaders(struct iris_context
*ice
)
1604 const uint64_t dirty
= ice
->state
.dirty
;
1606 struct brw_vue_prog_data
*old_prog_datas
[4];
1607 if (!(dirty
& IRIS_DIRTY_URB
)) {
1608 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++)
1609 old_prog_datas
[i
] = get_vue_prog_data(ice
, i
);
1612 if (dirty
& (IRIS_DIRTY_UNCOMPILED_TCS
| IRIS_DIRTY_UNCOMPILED_TES
)) {
1613 struct iris_uncompiled_shader
*tes
=
1614 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
1616 iris_update_compiled_tcs(ice
);
1617 iris_update_compiled_tes(ice
);
1619 ice
->shaders
.prog
[IRIS_CACHE_TCS
] = NULL
;
1620 ice
->shaders
.prog
[IRIS_CACHE_TES
] = NULL
;
1622 IRIS_DIRTY_TCS
| IRIS_DIRTY_TES
|
1623 IRIS_DIRTY_BINDINGS_TCS
| IRIS_DIRTY_BINDINGS_TES
|
1624 IRIS_DIRTY_CONSTANTS_TCS
| IRIS_DIRTY_CONSTANTS_TES
;
1628 if (dirty
& IRIS_DIRTY_UNCOMPILED_VS
)
1629 iris_update_compiled_vs(ice
);
1630 if (dirty
& IRIS_DIRTY_UNCOMPILED_GS
)
1631 iris_update_compiled_gs(ice
);
1633 if (dirty
& (IRIS_DIRTY_UNCOMPILED_GS
| IRIS_DIRTY_UNCOMPILED_TES
)) {
1634 const struct iris_compiled_shader
*gs
=
1635 ice
->shaders
.prog
[MESA_SHADER_GEOMETRY
];
1636 const struct iris_compiled_shader
*tes
=
1637 ice
->shaders
.prog
[MESA_SHADER_TESS_EVAL
];
1639 bool points_or_lines
= false;
1642 const struct brw_gs_prog_data
*gs_prog_data
= (void *) gs
->prog_data
;
1644 gs_prog_data
->output_topology
== _3DPRIM_POINTLIST
||
1645 gs_prog_data
->output_topology
== _3DPRIM_LINESTRIP
;
1647 const struct brw_tes_prog_data
*tes_data
= (void *) tes
->prog_data
;
1649 tes_data
->output_topology
== BRW_TESS_OUTPUT_TOPOLOGY_LINE
||
1650 tes_data
->output_topology
== BRW_TESS_OUTPUT_TOPOLOGY_POINT
;
1653 if (ice
->shaders
.output_topology_is_points_or_lines
!= points_or_lines
) {
1654 /* Outbound to XY Clip enables */
1655 ice
->shaders
.output_topology_is_points_or_lines
= points_or_lines
;
1656 ice
->state
.dirty
|= IRIS_DIRTY_CLIP
;
1660 gl_shader_stage last_stage
= last_vue_stage(ice
);
1661 struct iris_compiled_shader
*shader
= ice
->shaders
.prog
[last_stage
];
1662 struct iris_uncompiled_shader
*ish
= ice
->shaders
.uncompiled
[last_stage
];
1663 update_last_vue_map(ice
, shader
->prog_data
);
1664 if (ice
->state
.streamout
!= shader
->streamout
) {
1665 ice
->state
.streamout
= shader
->streamout
;
1666 ice
->state
.dirty
|= IRIS_DIRTY_SO_DECL_LIST
| IRIS_DIRTY_STREAMOUT
;
1669 if (ice
->state
.streamout_active
) {
1670 for (int i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
1671 struct iris_stream_output_target
*so
=
1672 (void *) ice
->state
.so_target
[i
];
1674 so
->stride
= ish
->stream_output
.stride
[i
] * sizeof(uint32_t);
1678 if (dirty
& IRIS_DIRTY_UNCOMPILED_FS
)
1679 iris_update_compiled_fs(ice
);
1681 /* Changing shader interfaces may require a URB configuration. */
1682 if (!(dirty
& IRIS_DIRTY_URB
)) {
1683 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
1684 struct brw_vue_prog_data
*old
= old_prog_datas
[i
];
1685 struct brw_vue_prog_data
*new = get_vue_prog_data(ice
, i
);
1686 if (!!old
!= !!new ||
1687 (new && new->urb_entry_size
!= old
->urb_entry_size
)) {
1688 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
1695 static struct iris_compiled_shader
*
1696 iris_compile_cs(struct iris_context
*ice
,
1697 struct iris_uncompiled_shader
*ish
,
1698 const struct brw_cs_prog_key
*key
)
1700 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1701 const struct brw_compiler
*compiler
= screen
->compiler
;
1702 void *mem_ctx
= ralloc_context(NULL
);
1703 struct brw_cs_prog_data
*cs_prog_data
=
1704 rzalloc(mem_ctx
, struct brw_cs_prog_data
);
1705 struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
1706 enum brw_param_builtin
*system_values
;
1707 unsigned num_system_values
;
1710 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1712 prog_data
->total_shared
= nir
->info
.cs
.shared_size
;
1714 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1715 &num_system_values
, &num_cbufs
);
1717 struct iris_binding_table bt
;
1718 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
1719 num_system_values
, num_cbufs
);
1721 char *error_str
= NULL
;
1722 const unsigned *program
=
1723 brw_compile_cs(compiler
, &ice
->dbg
, mem_ctx
, key
, cs_prog_data
,
1724 nir
, -1, &error_str
);
1725 if (program
== NULL
) {
1726 dbg_printf("Failed to compile compute shader: %s\n", error_str
);
1727 ralloc_free(mem_ctx
);
1731 if (ish
->compiled_once
) {
1732 iris_debug_recompile(ice
, &nir
->info
, &key
->base
);
1734 ish
->compiled_once
= true;
1737 struct iris_compiled_shader
*shader
=
1738 iris_upload_shader(ice
, IRIS_CACHE_CS
, sizeof(*key
), key
, program
,
1739 prog_data
, NULL
, system_values
, num_system_values
,
1742 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1744 ralloc_free(mem_ctx
);
1749 iris_update_compiled_compute_shader(struct iris_context
*ice
)
1751 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_COMPUTE
];
1752 struct iris_uncompiled_shader
*ish
=
1753 ice
->shaders
.uncompiled
[MESA_SHADER_COMPUTE
];
1755 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1756 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1757 struct brw_cs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1758 ice
->vtbl
.populate_cs_key(ice
, &key
);
1760 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_CS
];
1761 struct iris_compiled_shader
*shader
=
1762 iris_find_cached_shader(ice
, IRIS_CACHE_CS
, sizeof(key
), &key
);
1765 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
1768 shader
= iris_compile_cs(ice
, ish
, &key
);
1770 if (old
!= shader
) {
1771 ice
->shaders
.prog
[IRIS_CACHE_CS
] = shader
;
1772 ice
->state
.dirty
|= IRIS_DIRTY_CS
|
1773 IRIS_DIRTY_BINDINGS_CS
|
1774 IRIS_DIRTY_CONSTANTS_CS
;
1775 shs
->sysvals_need_upload
= true;
1780 iris_fill_cs_push_const_buffer(struct brw_cs_prog_data
*cs_prog_data
,
1783 assert(cs_prog_data
->push
.total
.size
> 0);
1784 assert(cs_prog_data
->push
.cross_thread
.size
== 0);
1785 assert(cs_prog_data
->push
.per_thread
.dwords
== 1);
1786 assert(cs_prog_data
->base
.param
[0] == BRW_PARAM_BUILTIN_SUBGROUP_ID
);
1787 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++)
1792 * Allocate scratch BOs as needed for the given per-thread size and stage.
1795 iris_get_scratch_space(struct iris_context
*ice
,
1796 unsigned per_thread_scratch
,
1797 gl_shader_stage stage
)
1799 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1800 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
1801 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1803 unsigned encoded_size
= ffs(per_thread_scratch
) - 11;
1804 assert(encoded_size
< (1 << 16));
1806 struct iris_bo
**bop
= &ice
->shaders
.scratch_bos
[encoded_size
][stage
];
1808 /* The documentation for 3DSTATE_PS "Scratch Space Base Pointer" says:
1810 * "Scratch Space per slice is computed based on 4 sub-slices. SW
1811 * must allocate scratch space enough so that each slice has 4
1814 * According to the other driver team, this applies to compute shaders
1815 * as well. This is not currently documented at all.
1817 * This hack is no longer necessary on Gen11+.
1819 unsigned subslice_total
= screen
->subslice_total
;
1820 if (devinfo
->gen
< 11)
1821 subslice_total
= 4 * devinfo
->num_slices
;
1822 assert(subslice_total
>= screen
->subslice_total
);
1825 unsigned scratch_ids_per_subslice
= devinfo
->max_cs_threads
;
1826 uint32_t max_threads
[] = {
1827 [MESA_SHADER_VERTEX
] = devinfo
->max_vs_threads
,
1828 [MESA_SHADER_TESS_CTRL
] = devinfo
->max_tcs_threads
,
1829 [MESA_SHADER_TESS_EVAL
] = devinfo
->max_tes_threads
,
1830 [MESA_SHADER_GEOMETRY
] = devinfo
->max_gs_threads
,
1831 [MESA_SHADER_FRAGMENT
] = devinfo
->max_wm_threads
,
1832 [MESA_SHADER_COMPUTE
] = scratch_ids_per_subslice
* subslice_total
,
1835 uint32_t size
= per_thread_scratch
* max_threads
[stage
];
1837 *bop
= iris_bo_alloc(bufmgr
, "scratch", size
, IRIS_MEMZONE_SHADER
);
1843 /* ------------------------------------------------------------------- */
1846 * The pipe->create_[stage]_state() driver hooks.
1848 * Performs basic NIR preprocessing, records any state dependencies, and
1849 * returns an iris_uncompiled_shader as the Gallium CSO.
1851 * Actual shader compilation to assembly happens later, at first use.
1854 iris_create_uncompiled_shader(struct pipe_context
*ctx
,
1856 const struct pipe_stream_output_info
*so_info
)
1858 struct iris_context
*ice
= (void *)ctx
;
1859 struct iris_screen
*screen
= (struct iris_screen
*)ctx
->screen
;
1860 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1862 struct iris_uncompiled_shader
*ish
=
1863 calloc(1, sizeof(struct iris_uncompiled_shader
));
1867 brw_preprocess_nir(screen
->compiler
, nir
, NULL
);
1869 NIR_PASS_V(nir
, brw_nir_lower_image_load_store
, devinfo
);
1870 NIR_PASS_V(nir
, iris_lower_storage_image_derefs
);
1874 if (nir
->constant_data_size
> 0) {
1875 unsigned data_offset
;
1876 u_upload_data(ice
->shaders
.uploader
, 0, nir
->constant_data_size
,
1877 32, nir
->constant_data
, &data_offset
, &ish
->const_data
);
1879 struct pipe_shader_buffer psb
= {
1880 .buffer
= ish
->const_data
,
1881 .buffer_offset
= data_offset
,
1882 .buffer_size
= nir
->constant_data_size
,
1884 iris_upload_ubo_ssbo_surf_state(ice
, &psb
, &ish
->const_data_state
, false);
1887 ish
->program_id
= get_new_program_id(screen
);
1890 memcpy(&ish
->stream_output
, so_info
, sizeof(*so_info
));
1891 update_so_info(&ish
->stream_output
, nir
->info
.outputs_written
);
1894 /* Save this now before potentially dropping nir->info.name */
1895 if (nir
->info
.name
&& strncmp(nir
->info
.name
, "ARB", 3) == 0)
1896 ish
->use_alt_mode
= true;
1898 if (screen
->disk_cache
) {
1899 /* Serialize the NIR to a binary blob that we can hash for the disk
1900 * cache. First, drop unnecessary information (like variable names)
1901 * so the serialized NIR is smaller, and also to let us detect more
1902 * isomorphic shaders when hashing, increasing cache hits. We clone
1903 * the NIR before stripping away this info because it can be useful
1904 * when inspecting and debugging shaders.
1906 nir_shader
*clone
= nir_shader_clone(NULL
, nir
);
1911 nir_serialize(&blob
, clone
);
1912 _mesa_sha1_compute(blob
.data
, blob
.size
, ish
->nir_sha1
);
1921 static struct iris_uncompiled_shader
*
1922 iris_create_shader_state(struct pipe_context
*ctx
,
1923 const struct pipe_shader_state
*state
)
1925 struct nir_shader
*nir
;
1927 if (state
->type
== PIPE_SHADER_IR_TGSI
)
1928 nir
= tgsi_to_nir(state
->tokens
, ctx
->screen
);
1930 nir
= state
->ir
.nir
;
1932 return iris_create_uncompiled_shader(ctx
, nir
, &state
->stream_output
);
1936 iris_create_vs_state(struct pipe_context
*ctx
,
1937 const struct pipe_shader_state
*state
)
1939 struct iris_context
*ice
= (void *) ctx
;
1940 struct iris_screen
*screen
= (void *) ctx
->screen
;
1941 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
1943 /* User clip planes */
1944 if (ish
->nir
->info
.clip_distance_array_size
== 0)
1945 ish
->nos
|= (1ull << IRIS_NOS_RASTERIZER
);
1947 if (screen
->precompile
) {
1948 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1949 struct brw_vs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1951 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
1952 iris_compile_vs(ice
, ish
, &key
);
1959 iris_create_tcs_state(struct pipe_context
*ctx
,
1960 const struct pipe_shader_state
*state
)
1962 struct iris_context
*ice
= (void *) ctx
;
1963 struct iris_screen
*screen
= (void *) ctx
->screen
;
1964 const struct brw_compiler
*compiler
= screen
->compiler
;
1965 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
1966 struct shader_info
*info
= &ish
->nir
->info
;
1968 if (screen
->precompile
) {
1969 const unsigned _GL_TRIANGLES
= 0x0004;
1970 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1971 struct brw_tcs_prog_key key
= {
1972 KEY_INIT(devinfo
->gen
),
1973 // XXX: make sure the linker fills this out from the TES...
1974 .tes_primitive_mode
=
1975 info
->tess
.primitive_mode
? info
->tess
.primitive_mode
1977 .outputs_written
= info
->outputs_written
,
1978 .patch_outputs_written
= info
->patch_outputs_written
,
1981 /* 8_PATCH mode needs the key to contain the input patch dimensionality.
1982 * We don't have that information, so we randomly guess that the input
1983 * and output patches are the same size. This is a bad guess, but we
1984 * can't do much better.
1986 if (compiler
->use_tcs_8_patch
)
1987 key
.input_vertices
= info
->tess
.tcs_vertices_out
;
1989 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
1990 iris_compile_tcs(ice
, ish
, &key
);
1997 iris_create_tes_state(struct pipe_context
*ctx
,
1998 const struct pipe_shader_state
*state
)
2000 struct iris_context
*ice
= (void *) ctx
;
2001 struct iris_screen
*screen
= (void *) ctx
->screen
;
2002 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
2003 struct shader_info
*info
= &ish
->nir
->info
;
2005 if (screen
->precompile
) {
2006 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
2007 struct brw_tes_prog_key key
= {
2008 KEY_INIT(devinfo
->gen
),
2009 // XXX: not ideal, need TCS output/TES input unification
2010 .inputs_read
= info
->inputs_read
,
2011 .patch_inputs_read
= info
->patch_inputs_read
,
2014 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
2015 iris_compile_tes(ice
, ish
, &key
);
2022 iris_create_gs_state(struct pipe_context
*ctx
,
2023 const struct pipe_shader_state
*state
)
2025 struct iris_context
*ice
= (void *) ctx
;
2026 struct iris_screen
*screen
= (void *) ctx
->screen
;
2027 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
2029 if (screen
->precompile
) {
2030 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
2031 struct brw_gs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
2033 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
2034 iris_compile_gs(ice
, ish
, &key
);
2041 iris_create_fs_state(struct pipe_context
*ctx
,
2042 const struct pipe_shader_state
*state
)
2044 struct iris_context
*ice
= (void *) ctx
;
2045 struct iris_screen
*screen
= (void *) ctx
->screen
;
2046 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
2047 struct shader_info
*info
= &ish
->nir
->info
;
2049 ish
->nos
|= (1ull << IRIS_NOS_FRAMEBUFFER
) |
2050 (1ull << IRIS_NOS_DEPTH_STENCIL_ALPHA
) |
2051 (1ull << IRIS_NOS_RASTERIZER
) |
2052 (1ull << IRIS_NOS_BLEND
);
2054 /* The program key needs the VUE map if there are > 16 inputs */
2055 if (util_bitcount64(ish
->nir
->info
.inputs_read
&
2056 BRW_FS_VARYING_INPUT_MASK
) > 16) {
2057 ish
->nos
|= (1ull << IRIS_NOS_LAST_VUE_MAP
);
2060 if (screen
->precompile
) {
2061 const uint64_t color_outputs
= info
->outputs_written
&
2062 ~(BITFIELD64_BIT(FRAG_RESULT_DEPTH
) |
2063 BITFIELD64_BIT(FRAG_RESULT_STENCIL
) |
2064 BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
));
2066 bool can_rearrange_varyings
=
2067 util_bitcount64(info
->inputs_read
& BRW_FS_VARYING_INPUT_MASK
) <= 16;
2069 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
2070 struct brw_wm_prog_key key
= {
2071 KEY_INIT(devinfo
->gen
),
2072 .nr_color_regions
= util_bitcount(color_outputs
),
2073 .coherent_fb_fetch
= true,
2074 .input_slots_valid
=
2075 can_rearrange_varyings
? 0 : info
->inputs_read
| VARYING_BIT_POS
,
2078 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
2079 iris_compile_fs(ice
, ish
, &key
, NULL
);
2086 iris_create_compute_state(struct pipe_context
*ctx
,
2087 const struct pipe_compute_state
*state
)
2089 assert(state
->ir_type
== PIPE_SHADER_IR_NIR
);
2091 struct iris_context
*ice
= (void *) ctx
;
2092 struct iris_screen
*screen
= (void *) ctx
->screen
;
2093 struct iris_uncompiled_shader
*ish
=
2094 iris_create_uncompiled_shader(ctx
, (void *) state
->prog
, NULL
);
2096 // XXX: disallow more than 64KB of shared variables
2098 if (screen
->precompile
) {
2099 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
2100 struct brw_cs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
2102 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
2103 iris_compile_cs(ice
, ish
, &key
);
2110 * The pipe->delete_[stage]_state() driver hooks.
2112 * Frees the iris_uncompiled_shader.
2115 iris_delete_shader_state(struct pipe_context
*ctx
, void *state
, gl_shader_stage stage
)
2117 struct iris_uncompiled_shader
*ish
= state
;
2118 struct iris_context
*ice
= (void *) ctx
;
2120 if (ice
->shaders
.uncompiled
[stage
] == ish
) {
2121 ice
->shaders
.uncompiled
[stage
] = NULL
;
2122 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_VS
<< stage
;
2125 if (ish
->const_data
) {
2126 pipe_resource_reference(&ish
->const_data
, NULL
);
2127 pipe_resource_reference(&ish
->const_data_state
.res
, NULL
);
2130 ralloc_free(ish
->nir
);
2135 iris_delete_vs_state(struct pipe_context
*ctx
, void *state
)
2137 iris_delete_shader_state(ctx
, state
, MESA_SHADER_VERTEX
);
2141 iris_delete_tcs_state(struct pipe_context
*ctx
, void *state
)
2143 iris_delete_shader_state(ctx
, state
, MESA_SHADER_TESS_CTRL
);
2147 iris_delete_tes_state(struct pipe_context
*ctx
, void *state
)
2149 iris_delete_shader_state(ctx
, state
, MESA_SHADER_TESS_EVAL
);
2153 iris_delete_gs_state(struct pipe_context
*ctx
, void *state
)
2155 iris_delete_shader_state(ctx
, state
, MESA_SHADER_GEOMETRY
);
2159 iris_delete_fs_state(struct pipe_context
*ctx
, void *state
)
2161 iris_delete_shader_state(ctx
, state
, MESA_SHADER_FRAGMENT
);
2165 iris_delete_cs_state(struct pipe_context
*ctx
, void *state
)
2167 iris_delete_shader_state(ctx
, state
, MESA_SHADER_COMPUTE
);
2171 * The pipe->bind_[stage]_state() driver hook.
2173 * Binds an uncompiled shader as the current one for a particular stage.
2174 * Updates dirty tracking to account for the shader's NOS.
2177 bind_shader_state(struct iris_context
*ice
,
2178 struct iris_uncompiled_shader
*ish
,
2179 gl_shader_stage stage
)
2181 uint64_t dirty_bit
= IRIS_DIRTY_UNCOMPILED_VS
<< stage
;
2182 const uint64_t nos
= ish
? ish
->nos
: 0;
2184 const struct shader_info
*old_info
= iris_get_shader_info(ice
, stage
);
2185 const struct shader_info
*new_info
= ish
? &ish
->nir
->info
: NULL
;
2187 if ((old_info
? util_last_bit(old_info
->textures_used
) : 0) !=
2188 (new_info
? util_last_bit(new_info
->textures_used
) : 0)) {
2189 ice
->state
.dirty
|= IRIS_DIRTY_SAMPLER_STATES_VS
<< stage
;
2192 ice
->shaders
.uncompiled
[stage
] = ish
;
2193 ice
->state
.dirty
|= dirty_bit
;
2195 /* Record that CSOs need to mark IRIS_DIRTY_UNCOMPILED_XS when they change
2196 * (or that they no longer need to do so).
2198 for (int i
= 0; i
< IRIS_NOS_COUNT
; i
++) {
2200 ice
->state
.dirty_for_nos
[i
] |= dirty_bit
;
2202 ice
->state
.dirty_for_nos
[i
] &= ~dirty_bit
;
2207 iris_bind_vs_state(struct pipe_context
*ctx
, void *state
)
2209 bind_shader_state((void *) ctx
, state
, MESA_SHADER_VERTEX
);
2213 iris_bind_tcs_state(struct pipe_context
*ctx
, void *state
)
2215 bind_shader_state((void *) ctx
, state
, MESA_SHADER_TESS_CTRL
);
2219 iris_bind_tes_state(struct pipe_context
*ctx
, void *state
)
2221 struct iris_context
*ice
= (struct iris_context
*)ctx
;
2223 /* Enabling/disabling optional stages requires a URB reconfiguration. */
2224 if (!!state
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
])
2225 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
2227 bind_shader_state((void *) ctx
, state
, MESA_SHADER_TESS_EVAL
);
2231 iris_bind_gs_state(struct pipe_context
*ctx
, void *state
)
2233 struct iris_context
*ice
= (struct iris_context
*)ctx
;
2235 /* Enabling/disabling optional stages requires a URB reconfiguration. */
2236 if (!!state
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
])
2237 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
2239 bind_shader_state((void *) ctx
, state
, MESA_SHADER_GEOMETRY
);
2243 iris_bind_fs_state(struct pipe_context
*ctx
, void *state
)
2245 struct iris_context
*ice
= (struct iris_context
*) ctx
;
2246 struct iris_uncompiled_shader
*old_ish
=
2247 ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
];
2248 struct iris_uncompiled_shader
*new_ish
= state
;
2250 const unsigned color_bits
=
2251 BITFIELD64_BIT(FRAG_RESULT_COLOR
) |
2252 BITFIELD64_RANGE(FRAG_RESULT_DATA0
, BRW_MAX_DRAW_BUFFERS
);
2254 /* Fragment shader outputs influence HasWriteableRT */
2255 if (!old_ish
|| !new_ish
||
2256 (old_ish
->nir
->info
.outputs_written
& color_bits
) !=
2257 (new_ish
->nir
->info
.outputs_written
& color_bits
))
2258 ice
->state
.dirty
|= IRIS_DIRTY_PS_BLEND
;
2260 bind_shader_state((void *) ctx
, state
, MESA_SHADER_FRAGMENT
);
2264 iris_bind_cs_state(struct pipe_context
*ctx
, void *state
)
2266 bind_shader_state((void *) ctx
, state
, MESA_SHADER_COMPUTE
);
2270 iris_init_program_functions(struct pipe_context
*ctx
)
2272 ctx
->create_vs_state
= iris_create_vs_state
;
2273 ctx
->create_tcs_state
= iris_create_tcs_state
;
2274 ctx
->create_tes_state
= iris_create_tes_state
;
2275 ctx
->create_gs_state
= iris_create_gs_state
;
2276 ctx
->create_fs_state
= iris_create_fs_state
;
2277 ctx
->create_compute_state
= iris_create_compute_state
;
2279 ctx
->delete_vs_state
= iris_delete_vs_state
;
2280 ctx
->delete_tcs_state
= iris_delete_tcs_state
;
2281 ctx
->delete_tes_state
= iris_delete_tes_state
;
2282 ctx
->delete_gs_state
= iris_delete_gs_state
;
2283 ctx
->delete_fs_state
= iris_delete_fs_state
;
2284 ctx
->delete_compute_state
= iris_delete_cs_state
;
2286 ctx
->bind_vs_state
= iris_bind_vs_state
;
2287 ctx
->bind_tcs_state
= iris_bind_tcs_state
;
2288 ctx
->bind_tes_state
= iris_bind_tes_state
;
2289 ctx
->bind_gs_state
= iris_bind_gs_state
;
2290 ctx
->bind_fs_state
= iris_bind_fs_state
;
2291 ctx
->bind_compute_state
= iris_bind_cs_state
;