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 .tex.swizzles[0 ... MAX_SAMPLERS - 1] = 0x688, \
51 .tex.compressed_multisample_layout_mask = ~0, \
52 .tex.msaa_16 = (gen >= 9 ? ~0 : 0)
53 #define KEY_INIT(gen) .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
;
81 // XXX: these are not retained forever, use a separate uploader?
83 upload_state(ice
->state
.surface_uploader
, surf_state
,
84 screen
->isl_dev
.ss
.size
, 64);
86 surf_state
->res
= NULL
;
90 struct iris_resource
*res
= (void *) buf
->buffer
;
91 struct iris_bo
*surf_bo
= iris_resource_bo(surf_state
->res
);
92 surf_state
->offset
+= iris_bo_offset_from_base_address(surf_bo
);
94 isl_buffer_fill_state(&screen
->isl_dev
, map
,
95 .address
= res
->bo
->gtt_offset
+ res
->offset
+
97 .size_B
= buf
->buffer_size
- res
->offset
,
98 .format
= ssbo
? ISL_FORMAT_RAW
99 : ISL_FORMAT_R32G32B32A32_FLOAT
,
100 .swizzle
= ISL_SWIZZLE_IDENTITY
,
102 .mocs
= ice
->vtbl
.mocs(res
->bo
));
106 get_aoa_deref_offset(nir_builder
*b
,
107 nir_deref_instr
*deref
,
110 unsigned array_size
= elem_size
;
111 nir_ssa_def
*offset
= nir_imm_int(b
, 0);
113 while (deref
->deref_type
!= nir_deref_type_var
) {
114 assert(deref
->deref_type
== nir_deref_type_array
);
116 /* This level's element size is the previous level's array size */
117 nir_ssa_def
*index
= nir_ssa_for_src(b
, deref
->arr
.index
, 1);
118 assert(deref
->arr
.index
.ssa
);
119 offset
= nir_iadd(b
, offset
,
120 nir_imul(b
, index
, nir_imm_int(b
, array_size
)));
122 deref
= nir_deref_instr_parent(deref
);
123 assert(glsl_type_is_array(deref
->type
));
124 array_size
*= glsl_get_length(deref
->type
);
127 /* Accessing an invalid surface index with the dataport can result in a
128 * hang. According to the spec "if the index used to select an individual
129 * element is negative or greater than or equal to the size of the array,
130 * the results of the operation are undefined but may not lead to
131 * termination" -- which is one of the possible outcomes of the hang.
132 * Clamp the index to prevent access outside of the array bounds.
134 return nir_umin(b
, offset
, nir_imm_int(b
, array_size
- elem_size
));
138 iris_lower_storage_image_derefs(nir_shader
*nir
)
140 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
143 nir_builder_init(&b
, impl
);
145 nir_foreach_block(block
, impl
) {
146 nir_foreach_instr_safe(instr
, block
) {
147 if (instr
->type
!= nir_instr_type_intrinsic
)
150 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
151 switch (intrin
->intrinsic
) {
152 case nir_intrinsic_image_deref_load
:
153 case nir_intrinsic_image_deref_store
:
154 case nir_intrinsic_image_deref_atomic_add
:
155 case nir_intrinsic_image_deref_atomic_min
:
156 case nir_intrinsic_image_deref_atomic_max
:
157 case nir_intrinsic_image_deref_atomic_and
:
158 case nir_intrinsic_image_deref_atomic_or
:
159 case nir_intrinsic_image_deref_atomic_xor
:
160 case nir_intrinsic_image_deref_atomic_exchange
:
161 case nir_intrinsic_image_deref_atomic_comp_swap
:
162 case nir_intrinsic_image_deref_size
:
163 case nir_intrinsic_image_deref_samples
:
164 case nir_intrinsic_image_deref_load_raw_intel
:
165 case nir_intrinsic_image_deref_store_raw_intel
: {
166 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
167 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
169 b
.cursor
= nir_before_instr(&intrin
->instr
);
171 nir_iadd(&b
, nir_imm_int(&b
, var
->data
.driver_location
),
172 get_aoa_deref_offset(&b
, deref
, 1));
173 nir_rewrite_image_intrinsic(intrin
, index
, false);
184 // XXX: need unify_interfaces() at link time...
187 * Fix an uncompiled shader's stream output info.
189 * Core Gallium stores output->register_index as a "slot" number, where
190 * slots are assigned consecutively to all outputs in info->outputs_written.
191 * This naive packing of outputs doesn't work for us - we too have slots,
192 * but the layout is defined by the VUE map, which we won't have until we
193 * compile a specific shader variant. So, we remap these and simply store
194 * VARYING_SLOT_* in our copy's output->register_index fields.
196 * We also fix up VARYING_SLOT_{LAYER,VIEWPORT,PSIZ} to select the Y/Z/W
197 * components of our VUE header. See brw_vue_map.c for the layout.
200 update_so_info(struct pipe_stream_output_info
*so_info
,
201 uint64_t outputs_written
)
203 uint8_t reverse_map
[64] = {};
205 while (outputs_written
) {
206 reverse_map
[slot
++] = u_bit_scan64(&outputs_written
);
209 for (unsigned i
= 0; i
< so_info
->num_outputs
; i
++) {
210 struct pipe_stream_output
*output
= &so_info
->output
[i
];
212 /* Map Gallium's condensed "slots" back to real VARYING_SLOT_* enums */
213 output
->register_index
= reverse_map
[output
->register_index
];
215 /* The VUE header contains three scalar fields packed together:
216 * - gl_PointSize is stored in VARYING_SLOT_PSIZ.w
217 * - gl_Layer is stored in VARYING_SLOT_PSIZ.y
218 * - gl_ViewportIndex is stored in VARYING_SLOT_PSIZ.z
220 switch (output
->register_index
) {
221 case VARYING_SLOT_LAYER
:
222 assert(output
->num_components
== 1);
223 output
->register_index
= VARYING_SLOT_PSIZ
;
224 output
->start_component
= 1;
226 case VARYING_SLOT_VIEWPORT
:
227 assert(output
->num_components
== 1);
228 output
->register_index
= VARYING_SLOT_PSIZ
;
229 output
->start_component
= 2;
231 case VARYING_SLOT_PSIZ
:
232 assert(output
->num_components
== 1);
233 output
->start_component
= 3;
237 //info->outputs_written |= 1ull << output->register_index;
242 setup_vec4_image_sysval(uint32_t *sysvals
, uint32_t idx
,
243 unsigned offset
, unsigned n
)
245 assert(offset
% sizeof(uint32_t) == 0);
247 for (unsigned i
= 0; i
< n
; ++i
)
248 sysvals
[i
] = BRW_PARAM_IMAGE(idx
, offset
/ sizeof(uint32_t) + i
);
250 for (unsigned i
= n
; i
< 4; ++i
)
251 sysvals
[i
] = BRW_PARAM_BUILTIN_ZERO
;
255 * Associate NIR uniform variables with the prog_data->param[] mechanism
256 * used by the backend. Also, decide which UBOs we'd like to push in an
257 * ideal situation (though the backend can reduce this).
260 iris_setup_uniforms(const struct brw_compiler
*compiler
,
263 struct brw_stage_prog_data
*prog_data
,
264 enum brw_param_builtin
**out_system_values
,
265 unsigned *out_num_system_values
,
266 unsigned *out_num_cbufs
)
268 UNUSED
const struct gen_device_info
*devinfo
= compiler
->devinfo
;
270 /* The intel compiler assumes that num_uniforms is in bytes. For
271 * scalar that means 4 bytes per uniform slot.
273 * Ref: brw_nir_lower_uniforms, type_size_scalar_bytes.
275 nir
->num_uniforms
*= 4;
277 const unsigned IRIS_MAX_SYSTEM_VALUES
=
278 PIPE_MAX_SHADER_IMAGES
* BRW_IMAGE_PARAM_SIZE
;
279 enum brw_param_builtin
*system_values
=
280 rzalloc_array(mem_ctx
, enum brw_param_builtin
, IRIS_MAX_SYSTEM_VALUES
);
281 unsigned num_system_values
= 0;
283 unsigned patch_vert_idx
= -1;
284 unsigned ucp_idx
[IRIS_MAX_CLIP_PLANES
];
285 unsigned img_idx
[PIPE_MAX_SHADER_IMAGES
];
286 memset(ucp_idx
, -1, sizeof(ucp_idx
));
287 memset(img_idx
, -1, sizeof(img_idx
));
289 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
292 nir_builder_init(&b
, impl
);
294 b
.cursor
= nir_before_block(nir_start_block(impl
));
295 nir_ssa_def
*temp_ubo_name
= nir_ssa_undef(&b
, 1, 32);
296 nir_ssa_def
*temp_const_ubo_name
= NULL
;
298 /* Turn system value intrinsics into uniforms */
299 nir_foreach_block(block
, impl
) {
300 nir_foreach_instr_safe(instr
, block
) {
301 if (instr
->type
!= nir_instr_type_intrinsic
)
304 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
307 switch (intrin
->intrinsic
) {
308 case nir_intrinsic_load_constant
: {
309 /* This one is special because it reads from the shader constant
310 * data and not cbuf0 which gallium uploads for us.
312 b
.cursor
= nir_before_instr(instr
);
313 nir_ssa_def
*offset
=
314 nir_iadd_imm(&b
, nir_ssa_for_src(&b
, intrin
->src
[0], 1),
315 nir_intrinsic_base(intrin
));
317 if (temp_const_ubo_name
== NULL
)
318 temp_const_ubo_name
= nir_imm_int(&b
, 0);
320 nir_intrinsic_instr
*load_ubo
=
321 nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_load_ubo
);
322 load_ubo
->num_components
= intrin
->num_components
;
323 load_ubo
->src
[0] = nir_src_for_ssa(temp_const_ubo_name
);
324 load_ubo
->src
[1] = nir_src_for_ssa(offset
);
325 nir_ssa_dest_init(&load_ubo
->instr
, &load_ubo
->dest
,
326 intrin
->dest
.ssa
.num_components
,
327 intrin
->dest
.ssa
.bit_size
,
328 intrin
->dest
.ssa
.name
);
329 nir_builder_instr_insert(&b
, &load_ubo
->instr
);
331 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
332 nir_src_for_ssa(&load_ubo
->dest
.ssa
));
333 nir_instr_remove(&intrin
->instr
);
336 case nir_intrinsic_load_user_clip_plane
: {
337 unsigned ucp
= nir_intrinsic_ucp_id(intrin
);
339 if (ucp_idx
[ucp
] == -1) {
340 ucp_idx
[ucp
] = num_system_values
;
341 num_system_values
+= 4;
344 for (int i
= 0; i
< 4; i
++) {
345 system_values
[ucp_idx
[ucp
] + i
] =
346 BRW_PARAM_BUILTIN_CLIP_PLANE(ucp
, i
);
349 b
.cursor
= nir_before_instr(instr
);
350 offset
= nir_imm_int(&b
, ucp_idx
[ucp
] * sizeof(uint32_t));
353 case nir_intrinsic_load_patch_vertices_in
:
354 if (patch_vert_idx
== -1)
355 patch_vert_idx
= num_system_values
++;
357 system_values
[patch_vert_idx
] =
358 BRW_PARAM_BUILTIN_PATCH_VERTICES_IN
;
360 b
.cursor
= nir_before_instr(instr
);
361 offset
= nir_imm_int(&b
, patch_vert_idx
* sizeof(uint32_t));
363 case nir_intrinsic_image_deref_load_param_intel
: {
364 assert(devinfo
->gen
< 9);
365 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
366 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
368 /* XXX: var->data.binding is not set properly. We need to run
369 * some form of gl_nir_lower_samplers_as_deref() to get it.
370 * This breaks tests which use more than one image.
372 if (img_idx
[var
->data
.binding
] == -1) {
373 /* GL only allows arrays of arrays of images. */
374 assert(glsl_type_is_image(glsl_without_array(var
->type
)));
375 unsigned num_images
= MAX2(1, glsl_get_aoa_size(var
->type
));
377 for (int i
= 0; i
< num_images
; i
++) {
378 const unsigned img
= var
->data
.binding
+ i
;
380 img_idx
[img
] = num_system_values
;
381 num_system_values
+= BRW_IMAGE_PARAM_SIZE
;
383 uint32_t *img_sv
= &system_values
[img_idx
[img
]];
385 setup_vec4_image_sysval(
386 img_sv
+ BRW_IMAGE_PARAM_OFFSET_OFFSET
, img
,
387 offsetof(struct brw_image_param
, offset
), 2);
388 setup_vec4_image_sysval(
389 img_sv
+ BRW_IMAGE_PARAM_SIZE_OFFSET
, img
,
390 offsetof(struct brw_image_param
, size
), 3);
391 setup_vec4_image_sysval(
392 img_sv
+ BRW_IMAGE_PARAM_STRIDE_OFFSET
, img
,
393 offsetof(struct brw_image_param
, stride
), 4);
394 setup_vec4_image_sysval(
395 img_sv
+ BRW_IMAGE_PARAM_TILING_OFFSET
, img
,
396 offsetof(struct brw_image_param
, tiling
), 3);
397 setup_vec4_image_sysval(
398 img_sv
+ BRW_IMAGE_PARAM_SWIZZLING_OFFSET
, img
,
399 offsetof(struct brw_image_param
, swizzling
), 2);
403 b
.cursor
= nir_before_instr(instr
);
404 offset
= nir_iadd(&b
,
405 get_aoa_deref_offset(&b
, deref
, BRW_IMAGE_PARAM_SIZE
* 4),
406 nir_imm_int(&b
, img_idx
[var
->data
.binding
] * 4 +
407 nir_intrinsic_base(intrin
) * 16));
414 unsigned comps
= nir_intrinsic_dest_components(intrin
);
416 nir_intrinsic_instr
*load
=
417 nir_intrinsic_instr_create(nir
, nir_intrinsic_load_ubo
);
418 load
->num_components
= comps
;
419 load
->src
[0] = nir_src_for_ssa(temp_ubo_name
);
420 load
->src
[1] = nir_src_for_ssa(offset
);
421 nir_ssa_dest_init(&load
->instr
, &load
->dest
, comps
, 32, NULL
);
422 nir_builder_instr_insert(&b
, &load
->instr
);
423 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
424 nir_src_for_ssa(&load
->dest
.ssa
));
425 nir_instr_remove(instr
);
429 nir_validate_shader(nir
, "before remapping");
431 /* Place the new params at the front of constant buffer 0. */
432 if (num_system_values
> 0) {
433 nir
->num_uniforms
+= num_system_values
* sizeof(uint32_t);
435 system_values
= reralloc(mem_ctx
, system_values
, enum brw_param_builtin
,
438 nir_foreach_block(block
, impl
) {
439 nir_foreach_instr_safe(instr
, block
) {
440 if (instr
->type
!= nir_instr_type_intrinsic
)
443 nir_intrinsic_instr
*load
= nir_instr_as_intrinsic(instr
);
445 if (load
->intrinsic
!= nir_intrinsic_load_ubo
)
448 b
.cursor
= nir_before_instr(instr
);
450 assert(load
->src
[0].is_ssa
);
452 if (load
->src
[0].ssa
== temp_ubo_name
) {
453 nir_instr_rewrite_src(instr
, &load
->src
[0],
454 nir_src_for_ssa(nir_imm_int(&b
, 0)));
455 } else if (nir_src_is_const(load
->src
[0]) &&
456 nir_src_as_uint(load
->src
[0]) == 0) {
457 nir_ssa_def
*offset
=
458 nir_iadd(&b
, load
->src
[1].ssa
,
459 nir_imm_int(&b
, 4 * num_system_values
));
460 nir_instr_rewrite_src(instr
, &load
->src
[1],
461 nir_src_for_ssa(offset
));
466 /* We need to fold the new iadds for brw_nir_analyze_ubo_ranges */
467 nir_opt_constant_folding(nir
);
469 ralloc_free(system_values
);
470 system_values
= NULL
;
473 nir_validate_shader(nir
, "after remap");
475 /* We don't use params[], but fs_visitor::nir_setup_uniforms() asserts
476 * about it for compute shaders, so go ahead and make some fake ones
477 * which the backend will dead code eliminate.
479 prog_data
->nr_params
= nir
->num_uniforms
/ 4;
480 prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t, prog_data
->nr_params
);
482 /* System values and uniforms are stored in constant buffer 0, the
483 * user-facing UBOs are indexed by one. So if any constant buffer is
484 * needed, the constant buffer 0 will be needed, so account for it.
486 unsigned num_cbufs
= nir
->info
.num_ubos
;
487 if (num_cbufs
|| num_system_values
|| nir
->num_uniforms
)
490 /* Constant loads (if any) need to go at the end of the constant buffers so
491 * we need to know num_cbufs before we can lower to them.
493 if (temp_const_ubo_name
!= NULL
) {
494 nir_load_const_instr
*const_ubo_index
=
495 nir_instr_as_load_const(temp_const_ubo_name
->parent_instr
);
496 assert(const_ubo_index
->def
.bit_size
== 32);
497 const_ubo_index
->value
[0].u32
= num_cbufs
;
500 *out_system_values
= system_values
;
501 *out_num_system_values
= num_system_values
;
502 *out_num_cbufs
= num_cbufs
;
505 static const char *surface_group_names
[] = {
506 [IRIS_SURFACE_GROUP_RENDER_TARGET
] = "render target",
507 [IRIS_SURFACE_GROUP_CS_WORK_GROUPS
] = "CS work groups",
508 [IRIS_SURFACE_GROUP_TEXTURE
] = "texture",
509 [IRIS_SURFACE_GROUP_UBO
] = "ubo",
510 [IRIS_SURFACE_GROUP_SSBO
] = "ssbo",
511 [IRIS_SURFACE_GROUP_IMAGE
] = "image",
515 iris_print_binding_table(FILE *fp
, const char *name
,
516 const struct iris_binding_table
*bt
)
518 STATIC_ASSERT(ARRAY_SIZE(surface_group_names
) == IRIS_SURFACE_GROUP_COUNT
);
521 uint32_t compacted
= 0;
523 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++) {
524 uint32_t size
= bt
->sizes
[i
];
527 compacted
+= util_bitcount64(bt
->used_mask
[i
]);
531 fprintf(fp
, "Binding table for %s is empty\n\n", name
);
535 if (total
!= compacted
) {
536 fprintf(fp
, "Binding table for %s "
537 "(compacted to %u entries from %u entries)\n",
538 name
, compacted
, total
);
540 fprintf(fp
, "Binding table for %s (%u entries)\n", name
, total
);
544 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++) {
545 uint64_t mask
= bt
->used_mask
[i
];
547 int index
= u_bit_scan64(&mask
);
548 fprintf(fp
, " [%u] %s #%d\n", entry
++, surface_group_names
[i
], index
);
555 /* Max elements in a surface group. */
556 SURFACE_GROUP_MAX_ELEMENTS
= 64,
560 * Map a <group, index> pair to a binding table index.
562 * For example: <UBO, 5> => binding table index 12
565 iris_group_index_to_bti(const struct iris_binding_table
*bt
,
566 enum iris_surface_group group
, uint32_t index
)
568 assert(index
< bt
->sizes
[group
]);
569 uint64_t mask
= bt
->used_mask
[group
];
570 uint64_t bit
= 1ull << index
;
572 return bt
->offsets
[group
] + util_bitcount64((bit
- 1) & mask
);
574 return IRIS_SURFACE_NOT_USED
;
579 * Map a binding table index back to a <group, index> pair.
581 * For example: binding table index 12 => <UBO, 5>
584 iris_bti_to_group_index(const struct iris_binding_table
*bt
,
585 enum iris_surface_group group
, uint32_t bti
)
587 uint64_t used_mask
= bt
->used_mask
[group
];
588 assert(bti
>= bt
->offsets
[group
]);
590 uint32_t c
= bti
- bt
->offsets
[group
];
592 int i
= u_bit_scan64(&used_mask
);
598 return IRIS_SURFACE_NOT_USED
;
602 rewrite_src_with_bti(nir_builder
*b
, struct iris_binding_table
*bt
,
603 nir_instr
*instr
, nir_src
*src
,
604 enum iris_surface_group group
)
606 assert(bt
->sizes
[group
] > 0);
608 b
->cursor
= nir_before_instr(instr
);
610 if (nir_src_is_const(*src
)) {
611 uint32_t index
= nir_src_as_uint(*src
);
612 bti
= nir_imm_intN_t(b
, iris_group_index_to_bti(bt
, group
, index
),
615 /* Indirect usage makes all the surfaces of the group to be available,
616 * so we can just add the base.
618 assert(bt
->used_mask
[group
] == BITFIELD64_MASK(bt
->sizes
[group
]));
619 bti
= nir_iadd_imm(b
, src
->ssa
, bt
->offsets
[group
]);
621 nir_instr_rewrite_src(instr
, src
, nir_src_for_ssa(bti
));
625 mark_used_with_src(struct iris_binding_table
*bt
, nir_src
*src
,
626 enum iris_surface_group group
)
628 assert(bt
->sizes
[group
] > 0);
630 if (nir_src_is_const(*src
)) {
631 uint64_t index
= nir_src_as_uint(*src
);
632 assert(index
< bt
->sizes
[group
]);
633 bt
->used_mask
[group
] |= 1ull << index
;
635 /* There's an indirect usage, we need all the surfaces. */
636 bt
->used_mask
[group
] = BITFIELD64_MASK(bt
->sizes
[group
]);
641 skip_compacting_binding_tables(void)
643 static int skip
= -1;
645 skip
= env_var_as_boolean("INTEL_DISABLE_COMPACT_BINDING_TABLE", false);
650 * Set up the binding table indices and apply to the shader.
653 iris_setup_binding_table(struct nir_shader
*nir
,
654 struct iris_binding_table
*bt
,
655 unsigned num_render_targets
,
656 unsigned num_system_values
,
659 const struct shader_info
*info
= &nir
->info
;
661 memset(bt
, 0, sizeof(*bt
));
663 /* Set the sizes for each surface group. For some groups, we already know
664 * upfront how many will be used, so mark them.
666 if (info
->stage
== MESA_SHADER_FRAGMENT
) {
667 bt
->sizes
[IRIS_SURFACE_GROUP_RENDER_TARGET
] = num_render_targets
;
668 /* All render targets used. */
669 bt
->used_mask
[IRIS_SURFACE_GROUP_RENDER_TARGET
] =
670 BITFIELD64_MASK(num_render_targets
);
671 } else if (info
->stage
== MESA_SHADER_COMPUTE
) {
672 bt
->sizes
[IRIS_SURFACE_GROUP_CS_WORK_GROUPS
] = 1;
675 bt
->sizes
[IRIS_SURFACE_GROUP_TEXTURE
] = util_last_bit(info
->textures_used
);
676 bt
->used_mask
[IRIS_SURFACE_GROUP_TEXTURE
] = info
->textures_used
;
678 bt
->sizes
[IRIS_SURFACE_GROUP_IMAGE
] = info
->num_images
;
680 /* Allocate an extra slot in the UBO section for NIR constants.
681 * Binding table compaction will remove it if unnecessary.
683 * We don't include them in iris_compiled_shader::num_cbufs because
684 * they are uploaded separately from shs->constbuf[], but from a shader
685 * point of view, they're another UBO (at the end of the section).
687 bt
->sizes
[IRIS_SURFACE_GROUP_UBO
] = num_cbufs
+ 1;
689 /* The first IRIS_MAX_ABOs indices in the SSBO group are for atomics, real
690 * SSBOs start after that. Compaction will remove unused ABOs.
692 bt
->sizes
[IRIS_SURFACE_GROUP_SSBO
] = IRIS_MAX_ABOS
+ info
->num_ssbos
;
694 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++)
695 assert(bt
->sizes
[i
] <= SURFACE_GROUP_MAX_ELEMENTS
);
697 /* Mark surfaces used for the cases we don't have the information available
700 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
701 nir_foreach_block (block
, impl
) {
702 nir_foreach_instr (instr
, block
) {
703 if (instr
->type
!= nir_instr_type_intrinsic
)
706 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
707 switch (intrin
->intrinsic
) {
708 case nir_intrinsic_load_num_work_groups
:
709 bt
->used_mask
[IRIS_SURFACE_GROUP_CS_WORK_GROUPS
] = 1;
712 case nir_intrinsic_image_size
:
713 case nir_intrinsic_image_load
:
714 case nir_intrinsic_image_store
:
715 case nir_intrinsic_image_atomic_add
:
716 case nir_intrinsic_image_atomic_min
:
717 case nir_intrinsic_image_atomic_max
:
718 case nir_intrinsic_image_atomic_and
:
719 case nir_intrinsic_image_atomic_or
:
720 case nir_intrinsic_image_atomic_xor
:
721 case nir_intrinsic_image_atomic_exchange
:
722 case nir_intrinsic_image_atomic_comp_swap
:
723 case nir_intrinsic_image_load_raw_intel
:
724 case nir_intrinsic_image_store_raw_intel
:
725 mark_used_with_src(bt
, &intrin
->src
[0], IRIS_SURFACE_GROUP_IMAGE
);
728 case nir_intrinsic_load_ubo
:
729 mark_used_with_src(bt
, &intrin
->src
[0], IRIS_SURFACE_GROUP_UBO
);
732 case nir_intrinsic_store_ssbo
:
733 mark_used_with_src(bt
, &intrin
->src
[1], IRIS_SURFACE_GROUP_SSBO
);
736 case nir_intrinsic_get_buffer_size
:
737 case nir_intrinsic_ssbo_atomic_add
:
738 case nir_intrinsic_ssbo_atomic_imin
:
739 case nir_intrinsic_ssbo_atomic_umin
:
740 case nir_intrinsic_ssbo_atomic_imax
:
741 case nir_intrinsic_ssbo_atomic_umax
:
742 case nir_intrinsic_ssbo_atomic_and
:
743 case nir_intrinsic_ssbo_atomic_or
:
744 case nir_intrinsic_ssbo_atomic_xor
:
745 case nir_intrinsic_ssbo_atomic_exchange
:
746 case nir_intrinsic_ssbo_atomic_comp_swap
:
747 case nir_intrinsic_ssbo_atomic_fmin
:
748 case nir_intrinsic_ssbo_atomic_fmax
:
749 case nir_intrinsic_ssbo_atomic_fcomp_swap
:
750 case nir_intrinsic_load_ssbo
:
751 mark_used_with_src(bt
, &intrin
->src
[0], IRIS_SURFACE_GROUP_SSBO
);
760 /* When disable we just mark everything as used. */
761 if (unlikely(skip_compacting_binding_tables())) {
762 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++)
763 bt
->used_mask
[i
] = BITFIELD64_MASK(bt
->sizes
[i
]);
766 /* Calculate the offsets and the binding table size based on the used
767 * surfaces. After this point, the functions to go between "group indices"
768 * and binding table indices can be used.
771 for (int i
= 0; i
< IRIS_SURFACE_GROUP_COUNT
; i
++) {
772 if (bt
->used_mask
[i
] != 0) {
773 bt
->offsets
[i
] = next
;
774 next
+= util_bitcount64(bt
->used_mask
[i
]);
777 bt
->size_bytes
= next
* 4;
779 if (unlikely(INTEL_DEBUG
& DEBUG_BT
)) {
780 iris_print_binding_table(stderr
, gl_shader_stage_name(info
->stage
), bt
);
783 /* Apply the binding table indices. The backend compiler is not expected
784 * to change those, as we haven't set any of the *_start entries in brw
788 nir_builder_init(&b
, impl
);
790 nir_foreach_block (block
, impl
) {
791 nir_foreach_instr (instr
, block
) {
792 if (instr
->type
== nir_instr_type_tex
) {
793 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
795 iris_group_index_to_bti(bt
, IRIS_SURFACE_GROUP_TEXTURE
,
800 if (instr
->type
!= nir_instr_type_intrinsic
)
803 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
804 switch (intrin
->intrinsic
) {
805 case nir_intrinsic_image_size
:
806 case nir_intrinsic_image_load
:
807 case nir_intrinsic_image_store
:
808 case nir_intrinsic_image_atomic_add
:
809 case nir_intrinsic_image_atomic_min
:
810 case nir_intrinsic_image_atomic_max
:
811 case nir_intrinsic_image_atomic_and
:
812 case nir_intrinsic_image_atomic_or
:
813 case nir_intrinsic_image_atomic_xor
:
814 case nir_intrinsic_image_atomic_exchange
:
815 case nir_intrinsic_image_atomic_comp_swap
:
816 case nir_intrinsic_image_load_raw_intel
:
817 case nir_intrinsic_image_store_raw_intel
:
818 rewrite_src_with_bti(&b
, bt
, instr
, &intrin
->src
[0],
819 IRIS_SURFACE_GROUP_IMAGE
);
822 case nir_intrinsic_load_ubo
:
823 rewrite_src_with_bti(&b
, bt
, instr
, &intrin
->src
[0],
824 IRIS_SURFACE_GROUP_UBO
);
827 case nir_intrinsic_store_ssbo
:
828 rewrite_src_with_bti(&b
, bt
, instr
, &intrin
->src
[1],
829 IRIS_SURFACE_GROUP_SSBO
);
832 case nir_intrinsic_get_buffer_size
:
833 case nir_intrinsic_ssbo_atomic_add
:
834 case nir_intrinsic_ssbo_atomic_imin
:
835 case nir_intrinsic_ssbo_atomic_umin
:
836 case nir_intrinsic_ssbo_atomic_imax
:
837 case nir_intrinsic_ssbo_atomic_umax
:
838 case nir_intrinsic_ssbo_atomic_and
:
839 case nir_intrinsic_ssbo_atomic_or
:
840 case nir_intrinsic_ssbo_atomic_xor
:
841 case nir_intrinsic_ssbo_atomic_exchange
:
842 case nir_intrinsic_ssbo_atomic_comp_swap
:
843 case nir_intrinsic_ssbo_atomic_fmin
:
844 case nir_intrinsic_ssbo_atomic_fmax
:
845 case nir_intrinsic_ssbo_atomic_fcomp_swap
:
846 case nir_intrinsic_load_ssbo
:
847 rewrite_src_with_bti(&b
, bt
, instr
, &intrin
->src
[0],
848 IRIS_SURFACE_GROUP_SSBO
);
859 iris_debug_recompile(struct iris_context
*ice
,
860 struct shader_info
*info
,
861 unsigned program_string_id
,
864 struct iris_screen
*screen
= (struct iris_screen
*) ice
->ctx
.screen
;
865 const struct brw_compiler
*c
= screen
->compiler
;
870 c
->shader_perf_log(&ice
->dbg
, "Recompiling %s shader for program %s: %s\n",
871 _mesa_shader_stage_to_string(info
->stage
),
872 info
->name
? info
->name
: "(no identifier)",
873 info
->label
? info
->label
: "");
875 const void *old_key
=
876 iris_find_previous_compile(ice
, info
->stage
, program_string_id
);
878 brw_debug_key_recompile(c
, &ice
->dbg
, info
->stage
, old_key
, key
);
883 * Compile a vertex shader, and upload the assembly.
885 static struct iris_compiled_shader
*
886 iris_compile_vs(struct iris_context
*ice
,
887 struct iris_uncompiled_shader
*ish
,
888 const struct brw_vs_prog_key
*key
)
890 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
891 const struct brw_compiler
*compiler
= screen
->compiler
;
892 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
893 void *mem_ctx
= ralloc_context(NULL
);
894 struct brw_vs_prog_data
*vs_prog_data
=
895 rzalloc(mem_ctx
, struct brw_vs_prog_data
);
896 struct brw_vue_prog_data
*vue_prog_data
= &vs_prog_data
->base
;
897 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
898 enum brw_param_builtin
*system_values
;
899 unsigned num_system_values
;
902 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
904 if (key
->nr_userclip_plane_consts
) {
905 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
906 nir_lower_clip_vs(nir
, (1 << key
->nr_userclip_plane_consts
) - 1, true);
907 nir_lower_io_to_temporaries(nir
, impl
, true, false);
908 nir_lower_global_vars_to_local(nir
);
909 nir_lower_vars_to_ssa(nir
);
910 nir_shader_gather_info(nir
, impl
);
913 prog_data
->use_alt_mode
= ish
->use_alt_mode
;
915 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
916 &num_system_values
, &num_cbufs
);
918 struct iris_binding_table bt
;
919 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
920 num_system_values
, num_cbufs
);
922 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
924 brw_compute_vue_map(devinfo
,
925 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
926 nir
->info
.separate_shader
);
928 /* Don't tell the backend about our clip plane constants, we've already
929 * lowered them in NIR and we don't want it doing it again.
931 struct brw_vs_prog_key key_no_ucp
= *key
;
932 key_no_ucp
.nr_userclip_plane_consts
= 0;
934 char *error_str
= NULL
;
935 const unsigned *program
=
936 brw_compile_vs(compiler
, &ice
->dbg
, mem_ctx
, &key_no_ucp
, vs_prog_data
,
937 nir
, -1, &error_str
);
938 if (program
== NULL
) {
939 dbg_printf("Failed to compile vertex shader: %s\n", error_str
);
940 ralloc_free(mem_ctx
);
944 if (ish
->compiled_once
) {
945 iris_debug_recompile(ice
, &nir
->info
, key
->program_string_id
, key
);
947 ish
->compiled_once
= true;
951 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
952 &vue_prog_data
->vue_map
);
954 struct iris_compiled_shader
*shader
=
955 iris_upload_shader(ice
, IRIS_CACHE_VS
, sizeof(*key
), key
, program
,
956 prog_data
, so_decls
, system_values
, num_system_values
,
959 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
961 ralloc_free(mem_ctx
);
966 * Update the current vertex shader variant.
968 * Fill out the key, look in the cache, compile and bind if needed.
971 iris_update_compiled_vs(struct iris_context
*ice
)
973 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_VERTEX
];
974 struct iris_uncompiled_shader
*ish
=
975 ice
->shaders
.uncompiled
[MESA_SHADER_VERTEX
];
976 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
977 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
979 struct brw_vs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
980 ice
->vtbl
.populate_vs_key(ice
, &ish
->nir
->info
, &key
);
982 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_VS
];
983 struct iris_compiled_shader
*shader
=
984 iris_find_cached_shader(ice
, IRIS_CACHE_VS
, sizeof(key
), &key
);
987 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
990 shader
= iris_compile_vs(ice
, ish
, &key
);
993 ice
->shaders
.prog
[IRIS_CACHE_VS
] = shader
;
994 ice
->state
.dirty
|= IRIS_DIRTY_VS
|
995 IRIS_DIRTY_BINDINGS_VS
|
996 IRIS_DIRTY_CONSTANTS_VS
|
998 shs
->cbuf0_needs_upload
= true;
1000 const struct brw_vs_prog_data
*vs_prog_data
=
1001 (void *) shader
->prog_data
;
1002 const bool uses_draw_params
= vs_prog_data
->uses_firstvertex
||
1003 vs_prog_data
->uses_baseinstance
;
1004 const bool uses_derived_draw_params
= vs_prog_data
->uses_drawid
||
1005 vs_prog_data
->uses_is_indexed_draw
;
1006 const bool needs_sgvs_element
= uses_draw_params
||
1007 vs_prog_data
->uses_instanceid
||
1008 vs_prog_data
->uses_vertexid
;
1009 bool needs_edge_flag
= false;
1010 nir_foreach_variable(var
, &ish
->nir
->inputs
) {
1011 if (var
->data
.location
== VERT_ATTRIB_EDGEFLAG
)
1012 needs_edge_flag
= true;
1015 if (ice
->state
.vs_uses_draw_params
!= uses_draw_params
||
1016 ice
->state
.vs_uses_derived_draw_params
!= uses_derived_draw_params
||
1017 ice
->state
.vs_needs_edge_flag
!= needs_edge_flag
) {
1018 ice
->state
.dirty
|= IRIS_DIRTY_VERTEX_BUFFERS
|
1019 IRIS_DIRTY_VERTEX_ELEMENTS
;
1021 ice
->state
.vs_uses_draw_params
= uses_draw_params
;
1022 ice
->state
.vs_uses_derived_draw_params
= uses_derived_draw_params
;
1023 ice
->state
.vs_needs_sgvs_element
= needs_sgvs_element
;
1024 ice
->state
.vs_needs_edge_flag
= needs_edge_flag
;
1029 * Get the shader_info for a given stage, or NULL if the stage is disabled.
1031 const struct shader_info
*
1032 iris_get_shader_info(const struct iris_context
*ice
, gl_shader_stage stage
)
1034 const struct iris_uncompiled_shader
*ish
= ice
->shaders
.uncompiled
[stage
];
1039 const nir_shader
*nir
= ish
->nir
;
1044 * Get the union of TCS output and TES input slots.
1046 * TCS and TES need to agree on a common URB entry layout. In particular,
1047 * the data for all patch vertices is stored in a single URB entry (unlike
1048 * GS which has one entry per input vertex). This means that per-vertex
1049 * array indexing needs a stride.
1051 * SSO requires locations to match, but doesn't require the number of
1052 * outputs/inputs to match (in fact, the TCS often has extra outputs).
1053 * So, we need to take the extra step of unifying these on the fly.
1056 get_unified_tess_slots(const struct iris_context
*ice
,
1057 uint64_t *per_vertex_slots
,
1058 uint32_t *per_patch_slots
)
1060 const struct shader_info
*tcs
=
1061 iris_get_shader_info(ice
, MESA_SHADER_TESS_CTRL
);
1062 const struct shader_info
*tes
=
1063 iris_get_shader_info(ice
, MESA_SHADER_TESS_EVAL
);
1065 *per_vertex_slots
= tes
->inputs_read
;
1066 *per_patch_slots
= tes
->patch_inputs_read
;
1069 *per_vertex_slots
|= tcs
->outputs_written
;
1070 *per_patch_slots
|= tcs
->patch_outputs_written
;
1075 * Compile a tessellation control shader, and upload the assembly.
1077 static struct iris_compiled_shader
*
1078 iris_compile_tcs(struct iris_context
*ice
,
1079 struct iris_uncompiled_shader
*ish
,
1080 const struct brw_tcs_prog_key
*key
)
1082 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1083 const struct brw_compiler
*compiler
= screen
->compiler
;
1084 const struct nir_shader_compiler_options
*options
=
1085 compiler
->glsl_compiler_options
[MESA_SHADER_TESS_CTRL
].NirOptions
;
1086 void *mem_ctx
= ralloc_context(NULL
);
1087 struct brw_tcs_prog_data
*tcs_prog_data
=
1088 rzalloc(mem_ctx
, struct brw_tcs_prog_data
);
1089 struct brw_vue_prog_data
*vue_prog_data
= &tcs_prog_data
->base
;
1090 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
1091 enum brw_param_builtin
*system_values
= NULL
;
1092 unsigned num_system_values
= 0;
1093 unsigned num_cbufs
= 0;
1097 struct iris_binding_table bt
;
1100 nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1102 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1103 &num_system_values
, &num_cbufs
);
1104 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
1105 num_system_values
, num_cbufs
);
1106 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
1108 nir
= brw_nir_create_passthrough_tcs(mem_ctx
, compiler
, options
, key
);
1110 /* Reserve space for passing the default tess levels as constants. */
1111 num_system_values
= 8;
1113 rzalloc_array(mem_ctx
, enum brw_param_builtin
, num_system_values
);
1114 prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t, num_system_values
);
1115 prog_data
->nr_params
= num_system_values
;
1117 if (key
->tes_primitive_mode
== GL_QUADS
) {
1118 for (int i
= 0; i
< 4; i
++)
1119 system_values
[7 - i
] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X
+ i
;
1121 system_values
[3] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X
;
1122 system_values
[2] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_Y
;
1123 } else if (key
->tes_primitive_mode
== GL_TRIANGLES
) {
1124 for (int i
= 0; i
< 3; i
++)
1125 system_values
[7 - i
] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X
+ i
;
1127 system_values
[4] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X
;
1129 assert(key
->tes_primitive_mode
== GL_ISOLINES
);
1130 system_values
[7] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_Y
;
1131 system_values
[6] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X
;
1134 /* Manually setup the TCS binding table. */
1135 memset(&bt
, 0, sizeof(bt
));
1136 bt
.sizes
[IRIS_SURFACE_GROUP_UBO
] = 1;
1137 bt
.used_mask
[IRIS_SURFACE_GROUP_UBO
] = 1;
1140 prog_data
->ubo_ranges
[0].length
= 1;
1143 char *error_str
= NULL
;
1144 const unsigned *program
=
1145 brw_compile_tcs(compiler
, &ice
->dbg
, mem_ctx
, key
, tcs_prog_data
, nir
,
1147 if (program
== NULL
) {
1148 dbg_printf("Failed to compile control shader: %s\n", error_str
);
1149 ralloc_free(mem_ctx
);
1154 if (ish
->compiled_once
) {
1155 iris_debug_recompile(ice
, &nir
->info
, key
->program_string_id
, key
);
1157 ish
->compiled_once
= true;
1161 struct iris_compiled_shader
*shader
=
1162 iris_upload_shader(ice
, IRIS_CACHE_TCS
, sizeof(*key
), key
, program
,
1163 prog_data
, NULL
, system_values
, num_system_values
,
1167 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1169 ralloc_free(mem_ctx
);
1174 * Update the current tessellation control shader variant.
1176 * Fill out the key, look in the cache, compile and bind if needed.
1179 iris_update_compiled_tcs(struct iris_context
*ice
)
1181 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_TESS_CTRL
];
1182 struct iris_uncompiled_shader
*tcs
=
1183 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_CTRL
];
1184 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1185 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1187 const struct shader_info
*tes_info
=
1188 iris_get_shader_info(ice
, MESA_SHADER_TESS_EVAL
);
1189 struct brw_tcs_prog_key key
= {
1190 KEY_INIT_NO_ID(devinfo
->gen
),
1191 .program_string_id
= tcs
? tcs
->program_id
: 0,
1192 .tes_primitive_mode
= tes_info
->tess
.primitive_mode
,
1193 .input_vertices
= ice
->state
.vertices_per_patch
,
1195 get_unified_tess_slots(ice
, &key
.outputs_written
,
1196 &key
.patch_outputs_written
);
1197 ice
->vtbl
.populate_tcs_key(ice
, &key
);
1199 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_TCS
];
1200 struct iris_compiled_shader
*shader
=
1201 iris_find_cached_shader(ice
, IRIS_CACHE_TCS
, sizeof(key
), &key
);
1204 shader
= iris_disk_cache_retrieve(ice
, tcs
, &key
, sizeof(key
));
1207 shader
= iris_compile_tcs(ice
, tcs
, &key
);
1209 if (old
!= shader
) {
1210 ice
->shaders
.prog
[IRIS_CACHE_TCS
] = shader
;
1211 ice
->state
.dirty
|= IRIS_DIRTY_TCS
|
1212 IRIS_DIRTY_BINDINGS_TCS
|
1213 IRIS_DIRTY_CONSTANTS_TCS
;
1214 shs
->cbuf0_needs_upload
= true;
1217 /* We're binding a passthrough TCS, which doesn't have uniforms.
1218 * Since there's no actual TCS, the state tracker doesn't bother
1219 * to call set_constant_buffers to clear stale constant buffers.
1221 * We do upload TCS constants for the default tesslevel system
1222 * values, however. In this case, we would see stale constant
1223 * data and try and read a dangling cbuf0->user_buffer pointer.
1224 * Just zero out the stale constants to avoid the upload.
1226 memset(&shs
->cbuf0
, 0, sizeof(shs
->cbuf0
));
1232 * Compile a tessellation evaluation shader, and upload the assembly.
1234 static struct iris_compiled_shader
*
1235 iris_compile_tes(struct iris_context
*ice
,
1236 struct iris_uncompiled_shader
*ish
,
1237 const struct brw_tes_prog_key
*key
)
1239 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1240 const struct brw_compiler
*compiler
= screen
->compiler
;
1241 void *mem_ctx
= ralloc_context(NULL
);
1242 struct brw_tes_prog_data
*tes_prog_data
=
1243 rzalloc(mem_ctx
, struct brw_tes_prog_data
);
1244 struct brw_vue_prog_data
*vue_prog_data
= &tes_prog_data
->base
;
1245 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
1246 enum brw_param_builtin
*system_values
;
1247 unsigned num_system_values
;
1250 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1252 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1253 &num_system_values
, &num_cbufs
);
1255 struct iris_binding_table bt
;
1256 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
1257 num_system_values
, num_cbufs
);
1259 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
1261 struct brw_vue_map input_vue_map
;
1262 brw_compute_tess_vue_map(&input_vue_map
, key
->inputs_read
,
1263 key
->patch_inputs_read
);
1265 char *error_str
= NULL
;
1266 const unsigned *program
=
1267 brw_compile_tes(compiler
, &ice
->dbg
, mem_ctx
, key
, &input_vue_map
,
1268 tes_prog_data
, nir
, NULL
, -1, &error_str
);
1269 if (program
== NULL
) {
1270 dbg_printf("Failed to compile evaluation shader: %s\n", error_str
);
1271 ralloc_free(mem_ctx
);
1275 if (ish
->compiled_once
) {
1276 iris_debug_recompile(ice
, &nir
->info
, key
->program_string_id
, key
);
1278 ish
->compiled_once
= true;
1281 uint32_t *so_decls
=
1282 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
1283 &vue_prog_data
->vue_map
);
1286 struct iris_compiled_shader
*shader
=
1287 iris_upload_shader(ice
, IRIS_CACHE_TES
, sizeof(*key
), key
, program
,
1288 prog_data
, so_decls
, system_values
, num_system_values
,
1291 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1293 ralloc_free(mem_ctx
);
1298 * Update the current tessellation evaluation shader variant.
1300 * Fill out the key, look in the cache, compile and bind if needed.
1303 iris_update_compiled_tes(struct iris_context
*ice
)
1305 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_TESS_EVAL
];
1306 struct iris_uncompiled_shader
*ish
=
1307 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
1308 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1309 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1311 struct brw_tes_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1312 get_unified_tess_slots(ice
, &key
.inputs_read
, &key
.patch_inputs_read
);
1313 ice
->vtbl
.populate_tes_key(ice
, &key
);
1315 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_TES
];
1316 struct iris_compiled_shader
*shader
=
1317 iris_find_cached_shader(ice
, IRIS_CACHE_TES
, sizeof(key
), &key
);
1320 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
1323 shader
= iris_compile_tes(ice
, ish
, &key
);
1325 if (old
!= shader
) {
1326 ice
->shaders
.prog
[IRIS_CACHE_TES
] = shader
;
1327 ice
->state
.dirty
|= IRIS_DIRTY_TES
|
1328 IRIS_DIRTY_BINDINGS_TES
|
1329 IRIS_DIRTY_CONSTANTS_TES
;
1330 shs
->cbuf0_needs_upload
= true;
1333 /* TODO: Could compare and avoid flagging this. */
1334 const struct shader_info
*tes_info
= &ish
->nir
->info
;
1335 if (tes_info
->system_values_read
& (1ull << SYSTEM_VALUE_VERTICES_IN
)) {
1336 ice
->state
.dirty
|= IRIS_DIRTY_CONSTANTS_TES
;
1337 ice
->state
.shaders
[MESA_SHADER_TESS_EVAL
].cbuf0_needs_upload
= true;
1342 * Compile a geometry shader, and upload the assembly.
1344 static struct iris_compiled_shader
*
1345 iris_compile_gs(struct iris_context
*ice
,
1346 struct iris_uncompiled_shader
*ish
,
1347 const struct brw_gs_prog_key
*key
)
1349 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1350 const struct brw_compiler
*compiler
= screen
->compiler
;
1351 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1352 void *mem_ctx
= ralloc_context(NULL
);
1353 struct brw_gs_prog_data
*gs_prog_data
=
1354 rzalloc(mem_ctx
, struct brw_gs_prog_data
);
1355 struct brw_vue_prog_data
*vue_prog_data
= &gs_prog_data
->base
;
1356 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
1357 enum brw_param_builtin
*system_values
;
1358 unsigned num_system_values
;
1361 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1363 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1364 &num_system_values
, &num_cbufs
);
1366 struct iris_binding_table bt
;
1367 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
1368 num_system_values
, num_cbufs
);
1370 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
1372 brw_compute_vue_map(devinfo
,
1373 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
1374 nir
->info
.separate_shader
);
1376 char *error_str
= NULL
;
1377 const unsigned *program
=
1378 brw_compile_gs(compiler
, &ice
->dbg
, mem_ctx
, key
, gs_prog_data
, nir
,
1379 NULL
, -1, &error_str
);
1380 if (program
== NULL
) {
1381 dbg_printf("Failed to compile geometry shader: %s\n", error_str
);
1382 ralloc_free(mem_ctx
);
1386 if (ish
->compiled_once
) {
1387 iris_debug_recompile(ice
, &nir
->info
, key
->program_string_id
, key
);
1389 ish
->compiled_once
= true;
1392 uint32_t *so_decls
=
1393 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
1394 &vue_prog_data
->vue_map
);
1396 struct iris_compiled_shader
*shader
=
1397 iris_upload_shader(ice
, IRIS_CACHE_GS
, sizeof(*key
), key
, program
,
1398 prog_data
, so_decls
, system_values
, num_system_values
,
1401 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1403 ralloc_free(mem_ctx
);
1408 * Update the current geometry shader variant.
1410 * Fill out the key, look in the cache, compile and bind if needed.
1413 iris_update_compiled_gs(struct iris_context
*ice
)
1415 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_GEOMETRY
];
1416 struct iris_uncompiled_shader
*ish
=
1417 ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
];
1418 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_GS
];
1419 struct iris_compiled_shader
*shader
= NULL
;
1422 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1423 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1424 struct brw_gs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1425 ice
->vtbl
.populate_gs_key(ice
, &key
);
1428 iris_find_cached_shader(ice
, IRIS_CACHE_GS
, sizeof(key
), &key
);
1431 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
1434 shader
= iris_compile_gs(ice
, ish
, &key
);
1437 if (old
!= shader
) {
1438 ice
->shaders
.prog
[IRIS_CACHE_GS
] = shader
;
1439 ice
->state
.dirty
|= IRIS_DIRTY_GS
|
1440 IRIS_DIRTY_BINDINGS_GS
|
1441 IRIS_DIRTY_CONSTANTS_GS
;
1442 shs
->cbuf0_needs_upload
= true;
1447 * Compile a fragment (pixel) shader, and upload the assembly.
1449 static struct iris_compiled_shader
*
1450 iris_compile_fs(struct iris_context
*ice
,
1451 struct iris_uncompiled_shader
*ish
,
1452 const struct brw_wm_prog_key
*key
,
1453 struct brw_vue_map
*vue_map
)
1455 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1456 const struct brw_compiler
*compiler
= screen
->compiler
;
1457 void *mem_ctx
= ralloc_context(NULL
);
1458 struct brw_wm_prog_data
*fs_prog_data
=
1459 rzalloc(mem_ctx
, struct brw_wm_prog_data
);
1460 struct brw_stage_prog_data
*prog_data
= &fs_prog_data
->base
;
1461 enum brw_param_builtin
*system_values
;
1462 unsigned num_system_values
;
1465 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1467 prog_data
->use_alt_mode
= ish
->use_alt_mode
;
1469 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1470 &num_system_values
, &num_cbufs
);
1472 struct iris_binding_table bt
;
1473 iris_setup_binding_table(nir
, &bt
, MAX2(key
->nr_color_regions
, 1),
1474 num_system_values
, num_cbufs
);
1476 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
1478 char *error_str
= NULL
;
1479 const unsigned *program
=
1480 brw_compile_fs(compiler
, &ice
->dbg
, mem_ctx
, key
, fs_prog_data
,
1481 nir
, NULL
, -1, -1, -1, true, false, vue_map
, &error_str
);
1482 if (program
== NULL
) {
1483 dbg_printf("Failed to compile fragment shader: %s\n", error_str
);
1484 ralloc_free(mem_ctx
);
1488 if (ish
->compiled_once
) {
1489 iris_debug_recompile(ice
, &nir
->info
, key
->program_string_id
, key
);
1491 ish
->compiled_once
= true;
1494 struct iris_compiled_shader
*shader
=
1495 iris_upload_shader(ice
, IRIS_CACHE_FS
, sizeof(*key
), key
, program
,
1496 prog_data
, NULL
, system_values
, num_system_values
,
1499 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1501 ralloc_free(mem_ctx
);
1506 * Update the current fragment shader variant.
1508 * Fill out the key, look in the cache, compile and bind if needed.
1511 iris_update_compiled_fs(struct iris_context
*ice
)
1513 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_FRAGMENT
];
1514 struct iris_uncompiled_shader
*ish
=
1515 ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
];
1516 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1517 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1518 struct brw_wm_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1519 ice
->vtbl
.populate_fs_key(ice
, &key
);
1521 if (ish
->nos
& (1ull << IRIS_NOS_LAST_VUE_MAP
))
1522 key
.input_slots_valid
= ice
->shaders
.last_vue_map
->slots_valid
;
1524 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_FS
];
1525 struct iris_compiled_shader
*shader
=
1526 iris_find_cached_shader(ice
, IRIS_CACHE_FS
, sizeof(key
), &key
);
1529 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
1532 shader
= iris_compile_fs(ice
, ish
, &key
, ice
->shaders
.last_vue_map
);
1534 if (old
!= shader
) {
1535 // XXX: only need to flag CLIP if barycentric has NONPERSPECTIVE
1536 // toggles. might be able to avoid flagging SBE too.
1537 ice
->shaders
.prog
[IRIS_CACHE_FS
] = shader
;
1538 ice
->state
.dirty
|= IRIS_DIRTY_FS
|
1539 IRIS_DIRTY_BINDINGS_FS
|
1540 IRIS_DIRTY_CONSTANTS_FS
|
1544 shs
->cbuf0_needs_upload
= true;
1549 * Get the compiled shader for the last enabled geometry stage.
1551 * This stage is the one which will feed stream output and the rasterizer.
1553 static gl_shader_stage
1554 last_vue_stage(struct iris_context
*ice
)
1556 if (ice
->shaders
.prog
[MESA_SHADER_GEOMETRY
])
1557 return MESA_SHADER_GEOMETRY
;
1559 if (ice
->shaders
.prog
[MESA_SHADER_TESS_EVAL
])
1560 return MESA_SHADER_TESS_EVAL
;
1562 return MESA_SHADER_VERTEX
;
1566 * Update the last enabled stage's VUE map.
1568 * When the shader feeding the rasterizer's output interface changes, we
1569 * need to re-emit various packets.
1572 update_last_vue_map(struct iris_context
*ice
,
1573 struct brw_stage_prog_data
*prog_data
)
1575 struct brw_vue_prog_data
*vue_prog_data
= (void *) prog_data
;
1576 struct brw_vue_map
*vue_map
= &vue_prog_data
->vue_map
;
1577 struct brw_vue_map
*old_map
= ice
->shaders
.last_vue_map
;
1578 const uint64_t changed_slots
=
1579 (old_map
? old_map
->slots_valid
: 0ull) ^ vue_map
->slots_valid
;
1581 if (changed_slots
& VARYING_BIT_VIEWPORT
) {
1582 // XXX: could use ctx->Const.MaxViewports for old API efficiency
1583 ice
->state
.num_viewports
=
1584 (vue_map
->slots_valid
& VARYING_BIT_VIEWPORT
) ? IRIS_MAX_VIEWPORTS
: 1;
1585 ice
->state
.dirty
|= IRIS_DIRTY_CLIP
|
1586 IRIS_DIRTY_SF_CL_VIEWPORT
|
1587 IRIS_DIRTY_CC_VIEWPORT
|
1588 IRIS_DIRTY_SCISSOR_RECT
|
1589 IRIS_DIRTY_UNCOMPILED_FS
|
1590 ice
->state
.dirty_for_nos
[IRIS_NOS_LAST_VUE_MAP
];
1591 // XXX: CC_VIEWPORT?
1594 if (changed_slots
|| (old_map
&& old_map
->separate
!= vue_map
->separate
)) {
1595 ice
->state
.dirty
|= IRIS_DIRTY_SBE
;
1598 ice
->shaders
.last_vue_map
= &vue_prog_data
->vue_map
;
1602 * Get the prog_data for a given stage, or NULL if the stage is disabled.
1604 static struct brw_vue_prog_data
*
1605 get_vue_prog_data(struct iris_context
*ice
, gl_shader_stage stage
)
1607 if (!ice
->shaders
.prog
[stage
])
1610 return (void *) ice
->shaders
.prog
[stage
]->prog_data
;
1613 // XXX: iris_compiled_shaders are space-leaking :(
1614 // XXX: do remember to unbind them if deleting them.
1617 * Update the current shader variants for the given state.
1619 * This should be called on every draw call to ensure that the correct
1620 * shaders are bound. It will also flag any dirty state triggered by
1621 * swapping out those shaders.
1624 iris_update_compiled_shaders(struct iris_context
*ice
)
1626 const uint64_t dirty
= ice
->state
.dirty
;
1628 struct brw_vue_prog_data
*old_prog_datas
[4];
1629 if (!(dirty
& IRIS_DIRTY_URB
)) {
1630 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++)
1631 old_prog_datas
[i
] = get_vue_prog_data(ice
, i
);
1634 if (dirty
& (IRIS_DIRTY_UNCOMPILED_TCS
| IRIS_DIRTY_UNCOMPILED_TES
)) {
1635 struct iris_uncompiled_shader
*tes
=
1636 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
1638 iris_update_compiled_tcs(ice
);
1639 iris_update_compiled_tes(ice
);
1641 ice
->shaders
.prog
[IRIS_CACHE_TCS
] = NULL
;
1642 ice
->shaders
.prog
[IRIS_CACHE_TES
] = NULL
;
1644 IRIS_DIRTY_TCS
| IRIS_DIRTY_TES
|
1645 IRIS_DIRTY_BINDINGS_TCS
| IRIS_DIRTY_BINDINGS_TES
|
1646 IRIS_DIRTY_CONSTANTS_TCS
| IRIS_DIRTY_CONSTANTS_TES
;
1650 if (dirty
& IRIS_DIRTY_UNCOMPILED_VS
)
1651 iris_update_compiled_vs(ice
);
1652 if (dirty
& IRIS_DIRTY_UNCOMPILED_GS
)
1653 iris_update_compiled_gs(ice
);
1655 if (dirty
& (IRIS_DIRTY_UNCOMPILED_GS
| IRIS_DIRTY_UNCOMPILED_TES
)) {
1656 const struct iris_compiled_shader
*gs
=
1657 ice
->shaders
.prog
[MESA_SHADER_GEOMETRY
];
1658 const struct iris_compiled_shader
*tes
=
1659 ice
->shaders
.prog
[MESA_SHADER_TESS_EVAL
];
1661 bool points_or_lines
= false;
1664 const struct brw_gs_prog_data
*gs_prog_data
= (void *) gs
->prog_data
;
1666 gs_prog_data
->output_topology
== _3DPRIM_POINTLIST
||
1667 gs_prog_data
->output_topology
== _3DPRIM_LINESTRIP
;
1669 const struct brw_tes_prog_data
*tes_data
= (void *) tes
->prog_data
;
1671 tes_data
->output_topology
== BRW_TESS_OUTPUT_TOPOLOGY_LINE
||
1672 tes_data
->output_topology
== BRW_TESS_OUTPUT_TOPOLOGY_POINT
;
1675 if (ice
->shaders
.output_topology_is_points_or_lines
!= points_or_lines
) {
1676 /* Outbound to XY Clip enables */
1677 ice
->shaders
.output_topology_is_points_or_lines
= points_or_lines
;
1678 ice
->state
.dirty
|= IRIS_DIRTY_CLIP
;
1682 gl_shader_stage last_stage
= last_vue_stage(ice
);
1683 struct iris_compiled_shader
*shader
= ice
->shaders
.prog
[last_stage
];
1684 struct iris_uncompiled_shader
*ish
= ice
->shaders
.uncompiled
[last_stage
];
1685 update_last_vue_map(ice
, shader
->prog_data
);
1686 if (ice
->state
.streamout
!= shader
->streamout
) {
1687 ice
->state
.streamout
= shader
->streamout
;
1688 ice
->state
.dirty
|= IRIS_DIRTY_SO_DECL_LIST
| IRIS_DIRTY_STREAMOUT
;
1691 if (ice
->state
.streamout_active
) {
1692 for (int i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
1693 struct iris_stream_output_target
*so
=
1694 (void *) ice
->state
.so_target
[i
];
1696 so
->stride
= ish
->stream_output
.stride
[i
] * sizeof(uint32_t);
1700 if (dirty
& IRIS_DIRTY_UNCOMPILED_FS
)
1701 iris_update_compiled_fs(ice
);
1703 /* Changing shader interfaces may require a URB configuration. */
1704 if (!(dirty
& IRIS_DIRTY_URB
)) {
1705 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
1706 struct brw_vue_prog_data
*old
= old_prog_datas
[i
];
1707 struct brw_vue_prog_data
*new = get_vue_prog_data(ice
, i
);
1708 if (!!old
!= !!new ||
1709 (new && new->urb_entry_size
!= old
->urb_entry_size
)) {
1710 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
1717 static struct iris_compiled_shader
*
1718 iris_compile_cs(struct iris_context
*ice
,
1719 struct iris_uncompiled_shader
*ish
,
1720 const struct brw_cs_prog_key
*key
)
1722 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1723 const struct brw_compiler
*compiler
= screen
->compiler
;
1724 void *mem_ctx
= ralloc_context(NULL
);
1725 struct brw_cs_prog_data
*cs_prog_data
=
1726 rzalloc(mem_ctx
, struct brw_cs_prog_data
);
1727 struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
1728 enum brw_param_builtin
*system_values
;
1729 unsigned num_system_values
;
1732 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1734 prog_data
->total_shared
= nir
->info
.cs
.shared_size
;
1736 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1737 &num_system_values
, &num_cbufs
);
1739 struct iris_binding_table bt
;
1740 iris_setup_binding_table(nir
, &bt
, /* num_render_targets */ 0,
1741 num_system_values
, num_cbufs
);
1743 char *error_str
= NULL
;
1744 const unsigned *program
=
1745 brw_compile_cs(compiler
, &ice
->dbg
, mem_ctx
, key
, cs_prog_data
,
1746 nir
, -1, &error_str
);
1747 if (program
== NULL
) {
1748 dbg_printf("Failed to compile compute shader: %s\n", error_str
);
1749 ralloc_free(mem_ctx
);
1753 if (ish
->compiled_once
) {
1754 iris_debug_recompile(ice
, &nir
->info
, key
->program_string_id
, key
);
1756 ish
->compiled_once
= true;
1759 struct iris_compiled_shader
*shader
=
1760 iris_upload_shader(ice
, IRIS_CACHE_CS
, sizeof(*key
), key
, program
,
1761 prog_data
, NULL
, system_values
, num_system_values
,
1764 iris_disk_cache_store(screen
->disk_cache
, ish
, shader
, key
, sizeof(*key
));
1766 ralloc_free(mem_ctx
);
1771 iris_update_compiled_compute_shader(struct iris_context
*ice
)
1773 struct iris_shader_state
*shs
= &ice
->state
.shaders
[MESA_SHADER_COMPUTE
];
1774 struct iris_uncompiled_shader
*ish
=
1775 ice
->shaders
.uncompiled
[MESA_SHADER_COMPUTE
];
1777 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1778 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1779 struct brw_cs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1780 ice
->vtbl
.populate_cs_key(ice
, &key
);
1782 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_CS
];
1783 struct iris_compiled_shader
*shader
=
1784 iris_find_cached_shader(ice
, IRIS_CACHE_CS
, sizeof(key
), &key
);
1787 shader
= iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
));
1790 shader
= iris_compile_cs(ice
, ish
, &key
);
1792 if (old
!= shader
) {
1793 ice
->shaders
.prog
[IRIS_CACHE_CS
] = shader
;
1794 ice
->state
.dirty
|= IRIS_DIRTY_CS
|
1795 IRIS_DIRTY_BINDINGS_CS
|
1796 IRIS_DIRTY_CONSTANTS_CS
;
1797 shs
->cbuf0_needs_upload
= true;
1802 iris_fill_cs_push_const_buffer(struct brw_cs_prog_data
*cs_prog_data
,
1805 assert(cs_prog_data
->push
.total
.size
> 0);
1806 assert(cs_prog_data
->push
.cross_thread
.size
== 0);
1807 assert(cs_prog_data
->push
.per_thread
.dwords
== 1);
1808 assert(cs_prog_data
->base
.param
[0] == BRW_PARAM_BUILTIN_SUBGROUP_ID
);
1809 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++)
1814 * Allocate scratch BOs as needed for the given per-thread size and stage.
1817 iris_get_scratch_space(struct iris_context
*ice
,
1818 unsigned per_thread_scratch
,
1819 gl_shader_stage stage
)
1821 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1822 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
1823 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1825 unsigned encoded_size
= ffs(per_thread_scratch
) - 11;
1826 assert(encoded_size
< (1 << 16));
1828 struct iris_bo
**bop
= &ice
->shaders
.scratch_bos
[encoded_size
][stage
];
1830 /* The documentation for 3DSTATE_PS "Scratch Space Base Pointer" says:
1832 * "Scratch Space per slice is computed based on 4 sub-slices. SW
1833 * must allocate scratch space enough so that each slice has 4
1836 * According to the other driver team, this applies to compute shaders
1837 * as well. This is not currently documented at all.
1839 * This hack is no longer necessary on Gen11+.
1841 unsigned subslice_total
= screen
->subslice_total
;
1842 if (devinfo
->gen
< 11)
1843 subslice_total
= 4 * devinfo
->num_slices
;
1844 assert(subslice_total
>= screen
->subslice_total
);
1847 unsigned scratch_ids_per_subslice
= devinfo
->max_cs_threads
;
1848 uint32_t max_threads
[] = {
1849 [MESA_SHADER_VERTEX
] = devinfo
->max_vs_threads
,
1850 [MESA_SHADER_TESS_CTRL
] = devinfo
->max_tcs_threads
,
1851 [MESA_SHADER_TESS_EVAL
] = devinfo
->max_tes_threads
,
1852 [MESA_SHADER_GEOMETRY
] = devinfo
->max_gs_threads
,
1853 [MESA_SHADER_FRAGMENT
] = devinfo
->max_wm_threads
,
1854 [MESA_SHADER_COMPUTE
] = scratch_ids_per_subslice
* subslice_total
,
1857 uint32_t size
= per_thread_scratch
* max_threads
[stage
];
1859 *bop
= iris_bo_alloc(bufmgr
, "scratch", size
, IRIS_MEMZONE_SHADER
);
1865 /* ------------------------------------------------------------------- */
1868 * The pipe->create_[stage]_state() driver hooks.
1870 * Performs basic NIR preprocessing, records any state dependencies, and
1871 * returns an iris_uncompiled_shader as the Gallium CSO.
1873 * Actual shader compilation to assembly happens later, at first use.
1876 iris_create_uncompiled_shader(struct pipe_context
*ctx
,
1878 const struct pipe_stream_output_info
*so_info
)
1880 struct iris_context
*ice
= (void *)ctx
;
1881 struct iris_screen
*screen
= (struct iris_screen
*)ctx
->screen
;
1882 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1884 struct iris_uncompiled_shader
*ish
=
1885 calloc(1, sizeof(struct iris_uncompiled_shader
));
1889 brw_preprocess_nir(screen
->compiler
, nir
, NULL
);
1891 NIR_PASS_V(nir
, brw_nir_lower_image_load_store
, devinfo
);
1892 NIR_PASS_V(nir
, iris_lower_storage_image_derefs
);
1896 if (nir
->constant_data_size
> 0) {
1897 unsigned data_offset
;
1898 u_upload_data(ice
->shaders
.uploader
, 0, nir
->constant_data_size
,
1899 32, nir
->constant_data
, &data_offset
, &ish
->const_data
);
1901 struct pipe_shader_buffer psb
= {
1902 .buffer
= ish
->const_data
,
1903 .buffer_offset
= data_offset
,
1904 .buffer_size
= nir
->constant_data_size
,
1906 iris_upload_ubo_ssbo_surf_state(ice
, &psb
, &ish
->const_data_state
, false);
1909 ish
->program_id
= get_new_program_id(screen
);
1912 memcpy(&ish
->stream_output
, so_info
, sizeof(*so_info
));
1913 update_so_info(&ish
->stream_output
, nir
->info
.outputs_written
);
1916 /* Save this now before potentially dropping nir->info.name */
1917 if (nir
->info
.name
&& strncmp(nir
->info
.name
, "ARB", 3) == 0)
1918 ish
->use_alt_mode
= true;
1920 if (screen
->disk_cache
) {
1921 /* Serialize the NIR to a binary blob that we can hash for the disk
1922 * cache. First, drop unnecessary information (like variable names)
1923 * so the serialized NIR is smaller, and also to let us detect more
1924 * isomorphic shaders when hashing, increasing cache hits. We clone
1925 * the NIR before stripping away this info because it can be useful
1926 * when inspecting and debugging shaders.
1928 nir_shader
*clone
= nir_shader_clone(NULL
, nir
);
1933 nir_serialize(&blob
, clone
);
1934 _mesa_sha1_compute(blob
.data
, blob
.size
, ish
->nir_sha1
);
1943 static struct iris_uncompiled_shader
*
1944 iris_create_shader_state(struct pipe_context
*ctx
,
1945 const struct pipe_shader_state
*state
)
1947 struct nir_shader
*nir
;
1949 if (state
->type
== PIPE_SHADER_IR_TGSI
)
1950 nir
= tgsi_to_nir(state
->tokens
, ctx
->screen
);
1952 nir
= state
->ir
.nir
;
1954 return iris_create_uncompiled_shader(ctx
, nir
, &state
->stream_output
);
1958 iris_create_vs_state(struct pipe_context
*ctx
,
1959 const struct pipe_shader_state
*state
)
1961 struct iris_context
*ice
= (void *) ctx
;
1962 struct iris_screen
*screen
= (void *) ctx
->screen
;
1963 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
1965 /* User clip planes */
1966 if (ish
->nir
->info
.clip_distance_array_size
== 0)
1967 ish
->nos
|= (1ull << IRIS_NOS_RASTERIZER
);
1969 if (screen
->precompile
) {
1970 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1971 struct brw_vs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
1973 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
1974 iris_compile_vs(ice
, ish
, &key
);
1981 iris_create_tcs_state(struct pipe_context
*ctx
,
1982 const struct pipe_shader_state
*state
)
1984 struct iris_context
*ice
= (void *) ctx
;
1985 struct iris_screen
*screen
= (void *) ctx
->screen
;
1986 const struct brw_compiler
*compiler
= screen
->compiler
;
1987 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
1988 struct shader_info
*info
= &ish
->nir
->info
;
1992 if (screen
->precompile
) {
1993 const unsigned _GL_TRIANGLES
= 0x0004;
1994 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1995 struct brw_tcs_prog_key key
= {
1996 KEY_INIT(devinfo
->gen
),
1997 // XXX: make sure the linker fills this out from the TES...
1998 .tes_primitive_mode
=
1999 info
->tess
.primitive_mode
? info
->tess
.primitive_mode
2001 .outputs_written
= info
->outputs_written
,
2002 .patch_outputs_written
= info
->patch_outputs_written
,
2005 /* 8_PATCH mode needs the key to contain the input patch dimensionality.
2006 * We don't have that information, so we randomly guess that the input
2007 * and output patches are the same size. This is a bad guess, but we
2008 * can't do much better.
2010 if (compiler
->use_tcs_8_patch
)
2011 key
.input_vertices
= info
->tess
.tcs_vertices_out
;
2013 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
2014 iris_compile_tcs(ice
, ish
, &key
);
2021 iris_create_tes_state(struct pipe_context
*ctx
,
2022 const struct pipe_shader_state
*state
)
2024 struct iris_context
*ice
= (void *) ctx
;
2025 struct iris_screen
*screen
= (void *) ctx
->screen
;
2026 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
2027 struct shader_info
*info
= &ish
->nir
->info
;
2031 if (screen
->precompile
) {
2032 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
2033 struct brw_tes_prog_key key
= {
2034 KEY_INIT(devinfo
->gen
),
2035 // XXX: not ideal, need TCS output/TES input unification
2036 .inputs_read
= info
->inputs_read
,
2037 .patch_inputs_read
= info
->patch_inputs_read
,
2040 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
2041 iris_compile_tes(ice
, ish
, &key
);
2048 iris_create_gs_state(struct pipe_context
*ctx
,
2049 const struct pipe_shader_state
*state
)
2051 struct iris_context
*ice
= (void *) ctx
;
2052 struct iris_screen
*screen
= (void *) ctx
->screen
;
2053 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
2057 if (screen
->precompile
) {
2058 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
2059 struct brw_gs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
2061 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
2062 iris_compile_gs(ice
, ish
, &key
);
2069 iris_create_fs_state(struct pipe_context
*ctx
,
2070 const struct pipe_shader_state
*state
)
2072 struct iris_context
*ice
= (void *) ctx
;
2073 struct iris_screen
*screen
= (void *) ctx
->screen
;
2074 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
2075 struct shader_info
*info
= &ish
->nir
->info
;
2077 ish
->nos
|= (1ull << IRIS_NOS_FRAMEBUFFER
) |
2078 (1ull << IRIS_NOS_DEPTH_STENCIL_ALPHA
) |
2079 (1ull << IRIS_NOS_RASTERIZER
) |
2080 (1ull << IRIS_NOS_BLEND
);
2082 /* The program key needs the VUE map if there are > 16 inputs */
2083 if (util_bitcount64(ish
->nir
->info
.inputs_read
&
2084 BRW_FS_VARYING_INPUT_MASK
) > 16) {
2085 ish
->nos
|= (1ull << IRIS_NOS_LAST_VUE_MAP
);
2088 if (screen
->precompile
) {
2089 const uint64_t color_outputs
= info
->outputs_written
&
2090 ~(BITFIELD64_BIT(FRAG_RESULT_DEPTH
) |
2091 BITFIELD64_BIT(FRAG_RESULT_STENCIL
) |
2092 BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
));
2094 bool can_rearrange_varyings
=
2095 util_bitcount64(info
->inputs_read
& BRW_FS_VARYING_INPUT_MASK
) <= 16;
2097 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
2098 struct brw_wm_prog_key key
= {
2099 KEY_INIT(devinfo
->gen
),
2100 .nr_color_regions
= util_bitcount(color_outputs
),
2101 .coherent_fb_fetch
= true,
2102 .input_slots_valid
=
2103 can_rearrange_varyings
? 0 : info
->inputs_read
| VARYING_BIT_POS
,
2106 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
2107 iris_compile_fs(ice
, ish
, &key
, NULL
);
2114 iris_create_compute_state(struct pipe_context
*ctx
,
2115 const struct pipe_compute_state
*state
)
2117 assert(state
->ir_type
== PIPE_SHADER_IR_NIR
);
2119 struct iris_context
*ice
= (void *) ctx
;
2120 struct iris_screen
*screen
= (void *) ctx
->screen
;
2121 struct iris_uncompiled_shader
*ish
=
2122 iris_create_uncompiled_shader(ctx
, (void *) state
->prog
, NULL
);
2124 // XXX: disallow more than 64KB of shared variables
2126 if (screen
->precompile
) {
2127 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
2128 struct brw_cs_prog_key key
= { KEY_INIT(devinfo
->gen
) };
2130 if (!iris_disk_cache_retrieve(ice
, ish
, &key
, sizeof(key
)))
2131 iris_compile_cs(ice
, ish
, &key
);
2138 * The pipe->delete_[stage]_state() driver hooks.
2140 * Frees the iris_uncompiled_shader.
2143 iris_delete_shader_state(struct pipe_context
*ctx
, void *state
, gl_shader_stage stage
)
2145 struct iris_uncompiled_shader
*ish
= state
;
2146 struct iris_context
*ice
= (void *) ctx
;
2148 if (ice
->shaders
.uncompiled
[stage
] == ish
) {
2149 ice
->shaders
.uncompiled
[stage
] = NULL
;
2150 ice
->state
.dirty
|= IRIS_DIRTY_UNCOMPILED_VS
<< stage
;
2153 if (ish
->const_data
) {
2154 pipe_resource_reference(&ish
->const_data
, NULL
);
2155 pipe_resource_reference(&ish
->const_data_state
.res
, NULL
);
2158 ralloc_free(ish
->nir
);
2163 iris_delete_vs_state(struct pipe_context
*ctx
, void *state
)
2165 iris_delete_shader_state(ctx
, state
, MESA_SHADER_VERTEX
);
2169 iris_delete_tcs_state(struct pipe_context
*ctx
, void *state
)
2171 iris_delete_shader_state(ctx
, state
, MESA_SHADER_TESS_CTRL
);
2175 iris_delete_tes_state(struct pipe_context
*ctx
, void *state
)
2177 iris_delete_shader_state(ctx
, state
, MESA_SHADER_TESS_EVAL
);
2181 iris_delete_gs_state(struct pipe_context
*ctx
, void *state
)
2183 iris_delete_shader_state(ctx
, state
, MESA_SHADER_GEOMETRY
);
2187 iris_delete_fs_state(struct pipe_context
*ctx
, void *state
)
2189 iris_delete_shader_state(ctx
, state
, MESA_SHADER_FRAGMENT
);
2193 iris_delete_cs_state(struct pipe_context
*ctx
, void *state
)
2195 iris_delete_shader_state(ctx
, state
, MESA_SHADER_COMPUTE
);
2199 * The pipe->bind_[stage]_state() driver hook.
2201 * Binds an uncompiled shader as the current one for a particular stage.
2202 * Updates dirty tracking to account for the shader's NOS.
2205 bind_shader_state(struct iris_context
*ice
,
2206 struct iris_uncompiled_shader
*ish
,
2207 gl_shader_stage stage
)
2209 uint64_t dirty_bit
= IRIS_DIRTY_UNCOMPILED_VS
<< stage
;
2210 const uint64_t nos
= ish
? ish
->nos
: 0;
2212 const struct shader_info
*old_info
= iris_get_shader_info(ice
, stage
);
2213 const struct shader_info
*new_info
= ish
? &ish
->nir
->info
: NULL
;
2215 if ((old_info
? util_last_bit(old_info
->textures_used
) : 0) !=
2216 (new_info
? util_last_bit(new_info
->textures_used
) : 0)) {
2217 ice
->state
.dirty
|= IRIS_DIRTY_SAMPLER_STATES_VS
<< stage
;
2220 ice
->shaders
.uncompiled
[stage
] = ish
;
2221 ice
->state
.dirty
|= dirty_bit
;
2223 /* Record that CSOs need to mark IRIS_DIRTY_UNCOMPILED_XS when they change
2224 * (or that they no longer need to do so).
2226 for (int i
= 0; i
< IRIS_NOS_COUNT
; i
++) {
2228 ice
->state
.dirty_for_nos
[i
] |= dirty_bit
;
2230 ice
->state
.dirty_for_nos
[i
] &= ~dirty_bit
;
2235 iris_bind_vs_state(struct pipe_context
*ctx
, void *state
)
2237 bind_shader_state((void *) ctx
, state
, MESA_SHADER_VERTEX
);
2241 iris_bind_tcs_state(struct pipe_context
*ctx
, void *state
)
2243 bind_shader_state((void *) ctx
, state
, MESA_SHADER_TESS_CTRL
);
2247 iris_bind_tes_state(struct pipe_context
*ctx
, void *state
)
2249 struct iris_context
*ice
= (struct iris_context
*)ctx
;
2251 /* Enabling/disabling optional stages requires a URB reconfiguration. */
2252 if (!!state
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
])
2253 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
2255 bind_shader_state((void *) ctx
, state
, MESA_SHADER_TESS_EVAL
);
2259 iris_bind_gs_state(struct pipe_context
*ctx
, void *state
)
2261 struct iris_context
*ice
= (struct iris_context
*)ctx
;
2263 /* Enabling/disabling optional stages requires a URB reconfiguration. */
2264 if (!!state
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
])
2265 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
2267 bind_shader_state((void *) ctx
, state
, MESA_SHADER_GEOMETRY
);
2271 iris_bind_fs_state(struct pipe_context
*ctx
, void *state
)
2273 struct iris_context
*ice
= (struct iris_context
*) ctx
;
2274 struct iris_uncompiled_shader
*old_ish
=
2275 ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
];
2276 struct iris_uncompiled_shader
*new_ish
= state
;
2278 const unsigned color_bits
=
2279 BITFIELD64_BIT(FRAG_RESULT_COLOR
) |
2280 BITFIELD64_RANGE(FRAG_RESULT_DATA0
, BRW_MAX_DRAW_BUFFERS
);
2282 /* Fragment shader outputs influence HasWriteableRT */
2283 if (!old_ish
|| !new_ish
||
2284 (old_ish
->nir
->info
.outputs_written
& color_bits
) !=
2285 (new_ish
->nir
->info
.outputs_written
& color_bits
))
2286 ice
->state
.dirty
|= IRIS_DIRTY_PS_BLEND
;
2288 bind_shader_state((void *) ctx
, state
, MESA_SHADER_FRAGMENT
);
2292 iris_bind_cs_state(struct pipe_context
*ctx
, void *state
)
2294 bind_shader_state((void *) ctx
, state
, MESA_SHADER_COMPUTE
);
2298 iris_init_program_functions(struct pipe_context
*ctx
)
2300 ctx
->create_vs_state
= iris_create_vs_state
;
2301 ctx
->create_tcs_state
= iris_create_tcs_state
;
2302 ctx
->create_tes_state
= iris_create_tes_state
;
2303 ctx
->create_gs_state
= iris_create_gs_state
;
2304 ctx
->create_fs_state
= iris_create_fs_state
;
2305 ctx
->create_compute_state
= iris_create_compute_state
;
2307 ctx
->delete_vs_state
= iris_delete_vs_state
;
2308 ctx
->delete_tcs_state
= iris_delete_tcs_state
;
2309 ctx
->delete_tes_state
= iris_delete_tes_state
;
2310 ctx
->delete_gs_state
= iris_delete_gs_state
;
2311 ctx
->delete_fs_state
= iris_delete_fs_state
;
2312 ctx
->delete_compute_state
= iris_delete_cs_state
;
2314 ctx
->bind_vs_state
= iris_bind_vs_state
;
2315 ctx
->bind_tcs_state
= iris_bind_tcs_state
;
2316 ctx
->bind_tes_state
= iris_bind_tes_state
;
2317 ctx
->bind_gs_state
= iris_bind_gs_state
;
2318 ctx
->bind_fs_state
= iris_bind_fs_state
;
2319 ctx
->bind_compute_state
= iris_bind_cs_state
;