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 "compiler/nir/nir.h"
40 #include "compiler/nir/nir_builder.h"
41 #include "intel/compiler/brw_compiler.h"
42 #include "intel/compiler/brw_nir.h"
43 #include "iris_context.h"
45 #define ALL_SAMPLERS_XYZW .tex.swizzles[0 ... MAX_SAMPLERS - 1] = 0x688
46 #define KEY_INIT .program_string_id = ish->program_id, ALL_SAMPLERS_XYZW
48 static struct iris_compiled_shader
*
49 iris_compile_vs(struct iris_context
*, struct iris_uncompiled_shader
*,
50 const struct brw_vs_prog_key
*);
51 static struct iris_compiled_shader
*
52 iris_compile_tcs(struct iris_context
*, struct iris_uncompiled_shader
*,
53 const struct brw_tcs_prog_key
*);
54 static struct iris_compiled_shader
*
55 iris_compile_tes(struct iris_context
*, struct iris_uncompiled_shader
*,
56 const struct brw_tes_prog_key
*);
57 static struct iris_compiled_shader
*
58 iris_compile_gs(struct iris_context
*, struct iris_uncompiled_shader
*,
59 const struct brw_gs_prog_key
*);
60 static struct iris_compiled_shader
*
61 iris_compile_fs(struct iris_context
*, struct iris_uncompiled_shader
*,
62 const struct brw_wm_prog_key
*, struct brw_vue_map
*);
63 static struct iris_compiled_shader
*
64 iris_compile_cs(struct iris_context
*, struct iris_uncompiled_shader
*,
65 const struct brw_cs_prog_key
*);
69 get_new_program_id(struct iris_screen
*screen
)
71 return p_atomic_inc_return(&screen
->program_id
);
75 * An uncompiled, API-facing shader. This is the Gallium CSO for shaders.
76 * It primarily contains the NIR for the shader.
78 * Each API-facing shader can be compiled into multiple shader variants,
79 * based on non-orthogonal state dependencies, recorded in the shader key.
81 * See iris_compiled_shader, which represents a compiled shader variant.
83 struct iris_uncompiled_shader
{
86 struct pipe_stream_output_info stream_output
;
90 /** Bitfield of (1 << IRIS_NOS_*) flags. */
93 /** Have any shader variants been compiled yet? */
98 get_aoa_deref_offset(nir_builder
*b
,
99 nir_deref_instr
*deref
,
102 unsigned array_size
= elem_size
;
103 nir_ssa_def
*offset
= nir_imm_int(b
, 0);
105 while (deref
->deref_type
!= nir_deref_type_var
) {
106 assert(deref
->deref_type
== nir_deref_type_array
);
108 /* This level's element size is the previous level's array size */
109 nir_ssa_def
*index
= nir_ssa_for_src(b
, deref
->arr
.index
, 1);
110 assert(deref
->arr
.index
.ssa
);
111 offset
= nir_iadd(b
, offset
,
112 nir_imul(b
, index
, nir_imm_int(b
, array_size
)));
114 deref
= nir_deref_instr_parent(deref
);
115 assert(glsl_type_is_array(deref
->type
));
116 array_size
*= glsl_get_length(deref
->type
);
119 /* Accessing an invalid surface index with the dataport can result in a
120 * hang. According to the spec "if the index used to select an individual
121 * element is negative or greater than or equal to the size of the array,
122 * the results of the operation are undefined but may not lead to
123 * termination" -- which is one of the possible outcomes of the hang.
124 * Clamp the index to prevent access outside of the array bounds.
126 return nir_umin(b
, offset
, nir_imm_int(b
, array_size
- elem_size
));
130 iris_lower_storage_image_derefs(nir_shader
*nir
)
132 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
135 nir_builder_init(&b
, impl
);
137 nir_foreach_block(block
, impl
) {
138 nir_foreach_instr_safe(instr
, block
) {
139 if (instr
->type
!= nir_instr_type_intrinsic
)
142 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
143 switch (intrin
->intrinsic
) {
144 case nir_intrinsic_image_deref_load
:
145 case nir_intrinsic_image_deref_store
:
146 case nir_intrinsic_image_deref_atomic_add
:
147 case nir_intrinsic_image_deref_atomic_min
:
148 case nir_intrinsic_image_deref_atomic_max
:
149 case nir_intrinsic_image_deref_atomic_and
:
150 case nir_intrinsic_image_deref_atomic_or
:
151 case nir_intrinsic_image_deref_atomic_xor
:
152 case nir_intrinsic_image_deref_atomic_exchange
:
153 case nir_intrinsic_image_deref_atomic_comp_swap
:
154 case nir_intrinsic_image_deref_size
:
155 case nir_intrinsic_image_deref_samples
: {
156 nir_deref_instr
*deref
= nir_src_as_deref(intrin
->src
[0]);
157 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
159 b
.cursor
= nir_before_instr(&intrin
->instr
);
161 nir_iadd(&b
, nir_imm_int(&b
, var
->data
.driver_location
),
162 get_aoa_deref_offset(&b
, deref
, 1));
163 brw_nir_rewrite_image_intrinsic(intrin
, index
);
174 // XXX: need unify_interfaces() at link time...
177 update_so_info(struct pipe_stream_output_info
*so_info
)
179 for (unsigned i
= 0; i
< so_info
->num_outputs
; i
++) {
180 struct pipe_stream_output
*output
= &so_info
->output
[i
];
182 /* The VUE header contains three scalar fields packed together:
183 * - gl_PointSize is stored in VARYING_SLOT_PSIZ.w
184 * - gl_Layer is stored in VARYING_SLOT_PSIZ.y
185 * - gl_ViewportIndex is stored in VARYING_SLOT_PSIZ.z
187 switch (output
->register_index
) {
188 case VARYING_SLOT_LAYER
:
189 assert(output
->num_components
== 1);
190 output
->register_index
= VARYING_SLOT_PSIZ
;
191 output
->start_component
= 1;
193 case VARYING_SLOT_VIEWPORT
:
194 assert(output
->num_components
== 1);
195 output
->register_index
= VARYING_SLOT_PSIZ
;
196 output
->start_component
= 2;
198 case VARYING_SLOT_PSIZ
:
199 assert(output
->num_components
== 1);
200 output
->start_component
= 3;
204 //info->outputs_written |= 1ull << output->register_index;
209 * The pipe->create_[stage]_state() driver hooks.
211 * Performs basic NIR preprocessing, records any state dependencies, and
212 * returns an iris_uncompiled_shader as the Gallium CSO.
214 * Actual shader compilation to assembly happens later, at first use.
217 iris_create_uncompiled_shader(struct pipe_context
*ctx
,
219 const struct pipe_stream_output_info
*so_info
)
221 struct iris_screen
*screen
= (struct iris_screen
*)ctx
->screen
;
222 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
224 struct iris_uncompiled_shader
*ish
=
225 calloc(1, sizeof(struct iris_uncompiled_shader
));
229 nir
= brw_preprocess_nir(screen
->compiler
, nir
);
231 NIR_PASS_V(nir
, brw_nir_lower_image_load_store
, devinfo
);
232 NIR_PASS_V(nir
, iris_lower_storage_image_derefs
);
234 ish
->program_id
= get_new_program_id(screen
);
237 memcpy(&ish
->stream_output
, so_info
, sizeof(*so_info
));
238 update_so_info(&ish
->stream_output
);
244 static struct iris_uncompiled_shader
*
245 iris_create_shader_state(struct pipe_context
*ctx
,
246 const struct pipe_shader_state
*state
)
248 assert(state
->type
== PIPE_SHADER_IR_NIR
);
250 return iris_create_uncompiled_shader(ctx
, state
->ir
.nir
,
251 &state
->stream_output
);
255 iris_create_vs_state(struct pipe_context
*ctx
,
256 const struct pipe_shader_state
*state
)
258 struct iris_context
*ice
= (void *) ctx
;
259 struct iris_screen
*screen
= (void *) ctx
->screen
;
260 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
262 /* User clip planes */
263 if (ish
->nir
->info
.clip_distance_array_size
== 0)
264 ish
->nos
|= (1ull << IRIS_NOS_RASTERIZER
);
266 if (screen
->precompile
) {
267 struct brw_vs_prog_key key
= { KEY_INIT
};
269 iris_compile_vs(ice
, ish
, &key
);
276 iris_create_tcs_state(struct pipe_context
*ctx
,
277 const struct pipe_shader_state
*state
)
279 struct iris_context
*ice
= (void *) ctx
;
280 struct iris_screen
*screen
= (void *) ctx
->screen
;
281 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
282 struct shader_info
*info
= &ish
->nir
->info
;
286 if (screen
->precompile
) {
287 const unsigned _GL_TRIANGLES
= 0x0004;
288 struct brw_tcs_prog_key key
= {
290 // XXX: make sure the linker fills this out from the TES...
291 .tes_primitive_mode
=
292 info
->tess
.primitive_mode
? info
->tess
.primitive_mode
294 .outputs_written
= info
->outputs_written
,
295 .patch_outputs_written
= info
->patch_outputs_written
,
298 iris_compile_tcs(ice
, ish
, &key
);
305 iris_create_tes_state(struct pipe_context
*ctx
,
306 const struct pipe_shader_state
*state
)
308 struct iris_context
*ice
= (void *) ctx
;
309 struct iris_screen
*screen
= (void *) ctx
->screen
;
310 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
311 struct shader_info
*info
= &ish
->nir
->info
;
315 if (screen
->precompile
) {
316 struct brw_tes_prog_key key
= {
318 // XXX: not ideal, need TCS output/TES input unification
319 .inputs_read
= info
->inputs_read
,
320 .patch_inputs_read
= info
->patch_inputs_read
,
323 iris_compile_tes(ice
, ish
, &key
);
330 iris_create_gs_state(struct pipe_context
*ctx
,
331 const struct pipe_shader_state
*state
)
333 struct iris_context
*ice
= (void *) ctx
;
334 struct iris_screen
*screen
= (void *) ctx
->screen
;
335 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
339 if (screen
->precompile
) {
340 struct brw_gs_prog_key key
= { KEY_INIT
};
342 iris_compile_gs(ice
, ish
, &key
);
349 iris_create_fs_state(struct pipe_context
*ctx
,
350 const struct pipe_shader_state
*state
)
352 struct iris_context
*ice
= (void *) ctx
;
353 struct iris_screen
*screen
= (void *) ctx
->screen
;
354 struct iris_uncompiled_shader
*ish
= iris_create_shader_state(ctx
, state
);
355 struct shader_info
*info
= &ish
->nir
->info
;
357 ish
->nos
|= (1ull << IRIS_NOS_FRAMEBUFFER
) |
358 (1ull << IRIS_NOS_DEPTH_STENCIL_ALPHA
) |
359 (1ull << IRIS_NOS_RASTERIZER
) |
360 (1ull << IRIS_NOS_BLEND
);
362 /* The program key needs the VUE map if there are > 16 inputs */
363 if (util_bitcount64(ish
->nir
->info
.inputs_read
&
364 BRW_FS_VARYING_INPUT_MASK
) > 16) {
365 ish
->nos
|= (1ull << IRIS_NOS_LAST_VUE_MAP
);
368 if (screen
->precompile
) {
369 const uint64_t color_outputs
= info
->outputs_written
&
370 ~(BITFIELD64_BIT(FRAG_RESULT_DEPTH
) |
371 BITFIELD64_BIT(FRAG_RESULT_STENCIL
) |
372 BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
));
374 bool can_rearrange_varyings
=
375 util_bitcount64(info
->inputs_read
& BRW_FS_VARYING_INPUT_MASK
) <= 16;
377 struct brw_wm_prog_key key
= {
379 .nr_color_regions
= util_bitcount(color_outputs
),
380 .coherent_fb_fetch
= true,
382 can_rearrange_varyings
? 0 : info
->inputs_read
| VARYING_BIT_POS
,
385 iris_compile_fs(ice
, ish
, &key
, NULL
);
392 iris_create_compute_state(struct pipe_context
*ctx
,
393 const struct pipe_compute_state
*state
)
395 assert(state
->ir_type
== PIPE_SHADER_IR_NIR
);
397 struct iris_context
*ice
= (void *) ctx
;
398 struct iris_screen
*screen
= (void *) ctx
->screen
;
399 struct iris_uncompiled_shader
*ish
=
400 iris_create_uncompiled_shader(ctx
, (void *) state
->prog
, NULL
);
402 // XXX: disallow more than 64KB of shared variables
404 if (screen
->precompile
) {
405 struct brw_cs_prog_key key
= { KEY_INIT
};
407 iris_compile_cs(ice
, ish
, &key
);
414 * The pipe->delete_[stage]_state() driver hooks.
416 * Frees the iris_uncompiled_shader.
419 iris_delete_shader_state(struct pipe_context
*ctx
, void *state
)
421 struct iris_uncompiled_shader
*ish
= state
;
423 ralloc_free(ish
->nir
);
428 * The pipe->bind_[stage]_state() driver hook.
430 * Binds an uncompiled shader as the current one for a particular stage.
431 * Updates dirty tracking to account for the shader's NOS.
434 bind_state(struct iris_context
*ice
,
435 struct iris_uncompiled_shader
*ish
,
436 gl_shader_stage stage
)
438 uint64_t dirty_bit
= IRIS_DIRTY_UNCOMPILED_VS
<< stage
;
439 const uint64_t nos
= ish
? ish
->nos
: 0;
441 ice
->shaders
.uncompiled
[stage
] = ish
;
442 ice
->state
.dirty
|= dirty_bit
;
444 /* Record that CSOs need to mark IRIS_DIRTY_UNCOMPILED_XS when they change
445 * (or that they no longer need to do so).
447 for (int i
= 0; i
< IRIS_NOS_COUNT
; i
++) {
449 ice
->state
.dirty_for_nos
[i
] |= dirty_bit
;
451 ice
->state
.dirty_for_nos
[i
] &= ~dirty_bit
;
456 iris_bind_vs_state(struct pipe_context
*ctx
, void *state
)
458 bind_state((void *) ctx
, state
, MESA_SHADER_VERTEX
);
462 iris_bind_tcs_state(struct pipe_context
*ctx
, void *state
)
464 bind_state((void *) ctx
, state
, MESA_SHADER_TESS_CTRL
);
468 iris_bind_tes_state(struct pipe_context
*ctx
, void *state
)
470 struct iris_context
*ice
= (struct iris_context
*)ctx
;
472 /* Enabling/disabling optional stages requires a URB reconfiguration. */
473 if (!!state
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
])
474 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
476 bind_state((void *) ctx
, state
, MESA_SHADER_TESS_EVAL
);
480 iris_bind_gs_state(struct pipe_context
*ctx
, void *state
)
482 struct iris_context
*ice
= (struct iris_context
*)ctx
;
484 /* Enabling/disabling optional stages requires a URB reconfiguration. */
485 if (!!state
!= !!ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
])
486 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
488 bind_state((void *) ctx
, state
, MESA_SHADER_GEOMETRY
);
492 iris_bind_fs_state(struct pipe_context
*ctx
, void *state
)
494 bind_state((void *) ctx
, state
, MESA_SHADER_FRAGMENT
);
498 iris_bind_cs_state(struct pipe_context
*ctx
, void *state
)
500 bind_state((void *) ctx
, state
, MESA_SHADER_COMPUTE
);
504 * Sets up the starting offsets for the groups of binding table entries
505 * common to all pipeline stages.
507 * Unused groups are initialized to 0xd0d0d0d0 to make it obvious that they're
508 * unused but also make sure that addition of small offsets to them will
509 * trigger some of our asserts that surface indices are < BRW_MAX_SURFACES.
512 assign_common_binding_table_offsets(const struct gen_device_info
*devinfo
,
513 const struct nir_shader
*nir
,
514 struct brw_stage_prog_data
*prog_data
,
515 uint32_t next_binding_table_offset
,
516 unsigned num_system_values
)
518 const struct shader_info
*info
= &nir
->info
;
520 if (info
->num_textures
) {
521 prog_data
->binding_table
.texture_start
= next_binding_table_offset
;
522 prog_data
->binding_table
.gather_texture_start
= next_binding_table_offset
;
523 next_binding_table_offset
+= info
->num_textures
;
525 prog_data
->binding_table
.texture_start
= 0xd0d0d0d0;
526 prog_data
->binding_table
.gather_texture_start
= 0xd0d0d0d0;
529 if (info
->num_images
) {
530 prog_data
->binding_table
.image_start
= next_binding_table_offset
;
531 next_binding_table_offset
+= info
->num_images
;
533 prog_data
->binding_table
.image_start
= 0xd0d0d0d0;
536 int num_ubos
= info
->num_ubos
+
537 ((nir
->num_uniforms
|| num_system_values
) ? 1 : 0);
540 //assert(info->num_ubos <= BRW_MAX_UBO);
541 prog_data
->binding_table
.ubo_start
= next_binding_table_offset
;
542 next_binding_table_offset
+= num_ubos
;
544 prog_data
->binding_table
.ubo_start
= 0xd0d0d0d0;
547 if (info
->num_ssbos
|| info
->num_abos
) {
548 prog_data
->binding_table
.ssbo_start
= next_binding_table_offset
;
549 // XXX: see iris_state "wasting 16 binding table slots for ABOs" comment
550 next_binding_table_offset
+= IRIS_MAX_ABOS
+ info
->num_ssbos
;
552 prog_data
->binding_table
.ssbo_start
= 0xd0d0d0d0;
555 prog_data
->binding_table
.shader_time_start
= 0xd0d0d0d0;
557 /* Plane 0 is just the regular texture section */
558 prog_data
->binding_table
.plane_start
[0] = prog_data
->binding_table
.texture_start
;
560 prog_data
->binding_table
.plane_start
[1] = next_binding_table_offset
;
561 next_binding_table_offset
+= info
->num_textures
;
563 prog_data
->binding_table
.plane_start
[2] = next_binding_table_offset
;
564 next_binding_table_offset
+= info
->num_textures
;
566 /* Set the binding table size */
567 prog_data
->binding_table
.size_bytes
= next_binding_table_offset
* 4;
569 return next_binding_table_offset
;
573 * Associate NIR uniform variables with the prog_data->param[] mechanism
574 * used by the backend. Also, decide which UBOs we'd like to push in an
575 * ideal situation (though the backend can reduce this).
578 iris_setup_uniforms(const struct brw_compiler
*compiler
,
581 struct brw_stage_prog_data
*prog_data
,
582 enum brw_param_builtin
**out_system_values
,
583 unsigned *out_num_system_values
)
585 /* We don't use params[], but fs_visitor::nir_setup_uniforms() asserts
586 * about it for compute shaders, so go ahead and make some fake ones
587 * which the backend will dead code eliminate.
589 prog_data
->nr_params
= nir
->num_uniforms
;
590 prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t, prog_data
->nr_params
);
592 /* The intel compiler assumes that num_uniforms is in bytes. For
593 * scalar that means 4 bytes per uniform slot.
595 * Ref: brw_nir_lower_uniforms, type_size_scalar_bytes.
597 nir
->num_uniforms
*= 4;
599 const unsigned IRIS_MAX_SYSTEM_VALUES
= 32;
600 enum brw_param_builtin
*system_values
=
601 rzalloc_array(mem_ctx
, enum brw_param_builtin
, IRIS_MAX_SYSTEM_VALUES
);
602 unsigned num_system_values
= 0;
604 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
607 nir_builder_init(&b
, impl
);
609 b
.cursor
= nir_before_block(nir_start_block(impl
));
610 nir_ssa_def
*temp_ubo_name
= nir_ssa_undef(&b
, 1, 32);
612 /* Turn system value intrinsics into uniforms */
613 nir_foreach_block(block
, impl
) {
614 nir_foreach_instr_safe(instr
, block
) {
615 if (instr
->type
!= nir_instr_type_intrinsic
)
618 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
620 unsigned idx
= num_system_values
;
622 switch (intrin
->intrinsic
) {
623 case nir_intrinsic_load_user_clip_plane
: {
624 unsigned ucp
= nir_intrinsic_ucp_id(intrin
);
625 for (int i
= 0; i
< 4; i
++) {
626 system_values
[num_system_values
++] =
627 BRW_PARAM_BUILTIN_CLIP_PLANE(ucp
, i
);
631 case nir_intrinsic_load_patch_vertices_in
:
632 system_values
[num_system_values
++] =
633 BRW_PARAM_BUILTIN_PATCH_VERTICES_IN
;
639 b
.cursor
= nir_before_instr(instr
);
641 unsigned comps
= nir_intrinsic_dest_components(intrin
);
642 nir_ssa_def
*offset
= nir_imm_int(&b
, idx
* sizeof(uint32_t));
644 nir_intrinsic_instr
*load
=
645 nir_intrinsic_instr_create(nir
, nir_intrinsic_load_ubo
);
646 load
->num_components
= comps
;
647 load
->src
[0] = nir_src_for_ssa(temp_ubo_name
);
648 load
->src
[1] = nir_src_for_ssa(offset
);
649 nir_ssa_dest_init(&load
->instr
, &load
->dest
, comps
, 32, NULL
);
650 nir_builder_instr_insert(&b
, &load
->instr
);
651 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
652 nir_src_for_ssa(&load
->dest
.ssa
));
653 nir_instr_remove(instr
);
657 nir_validate_shader(nir
, "before remapping");
659 /* Place the new params at the front of constant buffer 0. */
660 if (num_system_values
> 0) {
661 nir
->num_uniforms
+= num_system_values
* sizeof(uint32_t);
663 system_values
= reralloc(mem_ctx
, system_values
, enum brw_param_builtin
,
666 nir_foreach_block(block
, impl
) {
667 nir_foreach_instr_safe(instr
, block
) {
668 if (instr
->type
!= nir_instr_type_intrinsic
)
671 nir_intrinsic_instr
*load
= nir_instr_as_intrinsic(instr
);
673 if (load
->intrinsic
!= nir_intrinsic_load_ubo
)
676 b
.cursor
= nir_before_instr(instr
);
678 assert(load
->src
[0].is_ssa
);
680 if (load
->src
[0].ssa
== temp_ubo_name
) {
681 nir_instr_rewrite_src(instr
, &load
->src
[0],
682 nir_src_for_ssa(nir_imm_int(&b
, 0)));
683 } else if (nir_src_as_uint(load
->src
[0]) == 0) {
684 nir_ssa_def
*offset
=
685 nir_iadd(&b
, load
->src
[1].ssa
,
686 nir_imm_int(&b
, 4 * num_system_values
));
687 nir_instr_rewrite_src(instr
, &load
->src
[1],
688 nir_src_for_ssa(offset
));
693 /* We need to fold the new iadds for brw_nir_analyze_ubo_ranges */
694 nir_opt_constant_folding(nir
);
696 ralloc_free(system_values
);
697 system_values
= NULL
;
700 nir_validate_shader(nir
, "after remap");
702 if (nir
->info
.stage
!= MESA_SHADER_COMPUTE
)
703 brw_nir_analyze_ubo_ranges(compiler
, nir
, NULL
, prog_data
->ubo_ranges
);
705 *out_system_values
= system_values
;
706 *out_num_system_values
= num_system_values
;
710 * Compile a vertex shader, and upload the assembly.
712 static struct iris_compiled_shader
*
713 iris_compile_vs(struct iris_context
*ice
,
714 struct iris_uncompiled_shader
*ish
,
715 const struct brw_vs_prog_key
*key
)
717 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
718 const struct brw_compiler
*compiler
= screen
->compiler
;
719 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
720 void *mem_ctx
= ralloc_context(NULL
);
721 struct brw_vs_prog_data
*vs_prog_data
=
722 rzalloc(mem_ctx
, struct brw_vs_prog_data
);
723 struct brw_vue_prog_data
*vue_prog_data
= &vs_prog_data
->base
;
724 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
725 enum brw_param_builtin
*system_values
;
726 unsigned num_system_values
;
728 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
730 if (key
->nr_userclip_plane_consts
) {
731 nir_function_impl
*impl
= nir_shader_get_entrypoint(nir
);
732 nir_lower_clip_vs(nir
, (1 << key
->nr_userclip_plane_consts
) - 1, true);
733 nir_lower_io_to_temporaries(nir
, impl
, true, false);
734 nir_lower_global_vars_to_local(nir
);
735 nir_lower_vars_to_ssa(nir
);
736 nir_shader_gather_info(nir
, impl
);
741 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
744 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0,
747 brw_compute_vue_map(devinfo
,
748 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
749 nir
->info
.separate_shader
);
751 /* Don't tell the backend about our clip plane constants, we've already
752 * lowered them in NIR and we don't want it doing it again.
754 struct brw_vs_prog_key key_no_ucp
= *key
;
755 key_no_ucp
.nr_userclip_plane_consts
= 0;
757 char *error_str
= NULL
;
758 const unsigned *program
=
759 brw_compile_vs(compiler
, &ice
->dbg
, mem_ctx
, &key_no_ucp
, vs_prog_data
,
760 nir
, -1, &error_str
);
761 if (program
== NULL
) {
762 dbg_printf("Failed to compile vertex shader: %s\n", error_str
);
763 ralloc_free(mem_ctx
);
768 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
769 &vue_prog_data
->vue_map
);
771 struct iris_compiled_shader
*shader
=
772 iris_upload_shader(ice
, IRIS_CACHE_VS
, sizeof(*key
), key
, program
,
773 prog_data
, so_decls
, system_values
, num_system_values
);
775 if (ish
->compiled_once
) {
776 perf_debug(&ice
->dbg
, "Recompiling vertex shader\n");
778 ish
->compiled_once
= true;
781 ralloc_free(mem_ctx
);
786 * Update the current vertex shader variant.
788 * Fill out the key, look in the cache, compile and bind if needed.
791 iris_update_compiled_vs(struct iris_context
*ice
)
793 struct iris_uncompiled_shader
*ish
=
794 ice
->shaders
.uncompiled
[MESA_SHADER_VERTEX
];
796 struct brw_vs_prog_key key
= { KEY_INIT
};
797 ice
->vtbl
.populate_vs_key(ice
, &ish
->nir
->info
, &key
);
799 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_VS
];
800 struct iris_compiled_shader
*shader
=
801 iris_find_cached_shader(ice
, IRIS_CACHE_VS
, sizeof(key
), &key
);
804 shader
= iris_compile_vs(ice
, ish
, &key
);
807 ice
->shaders
.prog
[IRIS_CACHE_VS
] = shader
;
808 ice
->state
.dirty
|= IRIS_DIRTY_VS
|
809 IRIS_DIRTY_BINDINGS_VS
|
810 IRIS_DIRTY_CONSTANTS_VS
|
816 * Get the shader_info for a given stage, or NULL if the stage is disabled.
818 const struct shader_info
*
819 iris_get_shader_info(const struct iris_context
*ice
, gl_shader_stage stage
)
821 const struct iris_uncompiled_shader
*ish
= ice
->shaders
.uncompiled
[stage
];
826 const nir_shader
*nir
= ish
->nir
;
830 // XXX: this function is gross
832 iris_get_shader_num_ubos(const struct iris_context
*ice
, gl_shader_stage stage
)
834 const struct iris_uncompiled_shader
*ish
= ice
->shaders
.uncompiled
[stage
];
835 const struct iris_compiled_shader
*shader
= ice
->shaders
.prog
[stage
];
838 const nir_shader
*nir
= ish
->nir
;
839 /* see assign_common_binding_table_offsets */
840 return nir
->info
.num_ubos
+
841 ((nir
->num_uniforms
|| shader
->num_system_values
) ? 1 : 0);
847 * Get the union of TCS output and TES input slots.
849 * TCS and TES need to agree on a common URB entry layout. In particular,
850 * the data for all patch vertices is stored in a single URB entry (unlike
851 * GS which has one entry per input vertex). This means that per-vertex
852 * array indexing needs a stride.
854 * SSO requires locations to match, but doesn't require the number of
855 * outputs/inputs to match (in fact, the TCS often has extra outputs).
856 * So, we need to take the extra step of unifying these on the fly.
859 get_unified_tess_slots(const struct iris_context
*ice
,
860 uint64_t *per_vertex_slots
,
861 uint32_t *per_patch_slots
)
863 const struct shader_info
*tcs
=
864 iris_get_shader_info(ice
, MESA_SHADER_TESS_CTRL
);
865 const struct shader_info
*tes
=
866 iris_get_shader_info(ice
, MESA_SHADER_TESS_EVAL
);
868 *per_vertex_slots
= tes
->inputs_read
;
869 *per_patch_slots
= tes
->patch_inputs_read
;
872 *per_vertex_slots
|= tcs
->outputs_written
;
873 *per_patch_slots
|= tcs
->patch_outputs_written
;
878 * Compile a tessellation control shader, and upload the assembly.
880 static struct iris_compiled_shader
*
881 iris_compile_tcs(struct iris_context
*ice
,
882 struct iris_uncompiled_shader
*ish
,
883 const struct brw_tcs_prog_key
*key
)
885 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
886 const struct brw_compiler
*compiler
= screen
->compiler
;
887 const struct nir_shader_compiler_options
*options
=
888 compiler
->glsl_compiler_options
[MESA_SHADER_TESS_CTRL
].NirOptions
;
889 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
890 void *mem_ctx
= ralloc_context(NULL
);
891 struct brw_tcs_prog_data
*tcs_prog_data
=
892 rzalloc(mem_ctx
, struct brw_tcs_prog_data
);
893 struct brw_vue_prog_data
*vue_prog_data
= &tcs_prog_data
->base
;
894 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
895 enum brw_param_builtin
*system_values
= NULL
;
896 unsigned num_system_values
= 0;
901 nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
903 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
905 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0,
908 nir
= brw_nir_create_passthrough_tcs(mem_ctx
, compiler
, options
, key
);
910 /* Reserve space for passing the default tess levels as constants. */
911 prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t, 8);
912 prog_data
->nr_params
= 8;
913 prog_data
->ubo_ranges
[0].length
= 1;
916 char *error_str
= NULL
;
917 const unsigned *program
=
918 brw_compile_tcs(compiler
, &ice
->dbg
, mem_ctx
, key
, tcs_prog_data
, nir
,
920 if (program
== NULL
) {
921 dbg_printf("Failed to compile control shader: %s\n", error_str
);
922 ralloc_free(mem_ctx
);
926 struct iris_compiled_shader
*shader
=
927 iris_upload_shader(ice
, IRIS_CACHE_TCS
, sizeof(*key
), key
, program
,
928 prog_data
, NULL
, system_values
, num_system_values
);
931 if (ish
->compiled_once
) {
932 perf_debug(&ice
->dbg
, "Recompiling tessellation control shader\n");
934 ish
->compiled_once
= true;
938 ralloc_free(mem_ctx
);
943 * Update the current tessellation control shader variant.
945 * Fill out the key, look in the cache, compile and bind if needed.
948 iris_update_compiled_tcs(struct iris_context
*ice
)
950 struct iris_uncompiled_shader
*tcs
=
951 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_CTRL
];
953 const struct shader_info
*tes_info
=
954 iris_get_shader_info(ice
, MESA_SHADER_TESS_EVAL
);
955 struct brw_tcs_prog_key key
= {
957 .program_string_id
= tcs
? tcs
->program_id
: 0,
958 .tes_primitive_mode
= tes_info
->tess
.primitive_mode
,
959 .input_vertices
= ice
->state
.vertices_per_patch
,
961 get_unified_tess_slots(ice
, &key
.outputs_written
,
962 &key
.patch_outputs_written
);
963 ice
->vtbl
.populate_tcs_key(ice
, &key
);
965 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_TCS
];
966 struct iris_compiled_shader
*shader
=
967 iris_find_cached_shader(ice
, IRIS_CACHE_TCS
, sizeof(key
), &key
);
970 shader
= iris_compile_tcs(ice
, tcs
, &key
);
973 ice
->shaders
.prog
[IRIS_CACHE_TCS
] = shader
;
974 ice
->state
.dirty
|= IRIS_DIRTY_TCS
|
975 IRIS_DIRTY_BINDINGS_TCS
|
976 IRIS_DIRTY_CONSTANTS_TCS
;
981 * Compile a tessellation evaluation shader, and upload the assembly.
983 static struct iris_compiled_shader
*
984 iris_compile_tes(struct iris_context
*ice
,
985 struct iris_uncompiled_shader
*ish
,
986 const struct brw_tes_prog_key
*key
)
988 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
989 const struct brw_compiler
*compiler
= screen
->compiler
;
990 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
991 void *mem_ctx
= ralloc_context(NULL
);
992 struct brw_tes_prog_data
*tes_prog_data
=
993 rzalloc(mem_ctx
, struct brw_tes_prog_data
);
994 struct brw_vue_prog_data
*vue_prog_data
= &tes_prog_data
->base
;
995 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
996 enum brw_param_builtin
*system_values
;
997 unsigned num_system_values
;
999 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1001 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1002 &num_system_values
);
1004 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0,
1007 struct brw_vue_map input_vue_map
;
1008 brw_compute_tess_vue_map(&input_vue_map
, key
->inputs_read
,
1009 key
->patch_inputs_read
);
1011 char *error_str
= NULL
;
1012 const unsigned *program
=
1013 brw_compile_tes(compiler
, &ice
->dbg
, mem_ctx
, key
, &input_vue_map
,
1014 tes_prog_data
, nir
, NULL
, -1, &error_str
);
1015 if (program
== NULL
) {
1016 dbg_printf("Failed to compile evaluation shader: %s\n", error_str
);
1017 ralloc_free(mem_ctx
);
1021 uint32_t *so_decls
=
1022 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
1023 &vue_prog_data
->vue_map
);
1026 struct iris_compiled_shader
*shader
=
1027 iris_upload_shader(ice
, IRIS_CACHE_TES
, sizeof(*key
), key
, program
,
1028 prog_data
, so_decls
, system_values
, num_system_values
);
1030 if (ish
->compiled_once
) {
1031 perf_debug(&ice
->dbg
, "Recompiling tessellation evaluation shader\n");
1033 ish
->compiled_once
= true;
1036 ralloc_free(mem_ctx
);
1041 * Update the current tessellation evaluation shader variant.
1043 * Fill out the key, look in the cache, compile and bind if needed.
1046 iris_update_compiled_tes(struct iris_context
*ice
)
1048 struct iris_uncompiled_shader
*ish
=
1049 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
1051 struct brw_tes_prog_key key
= { KEY_INIT
};
1052 get_unified_tess_slots(ice
, &key
.inputs_read
, &key
.patch_inputs_read
);
1053 ice
->vtbl
.populate_tes_key(ice
, &key
);
1055 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_TES
];
1056 struct iris_compiled_shader
*shader
=
1057 iris_find_cached_shader(ice
, IRIS_CACHE_TES
, sizeof(key
), &key
);
1060 shader
= iris_compile_tes(ice
, ish
, &key
);
1062 if (old
!= shader
) {
1063 ice
->shaders
.prog
[IRIS_CACHE_TES
] = shader
;
1064 ice
->state
.dirty
|= IRIS_DIRTY_TES
|
1065 IRIS_DIRTY_BINDINGS_TES
|
1066 IRIS_DIRTY_CONSTANTS_TES
;
1071 * Compile a geometry shader, and upload the assembly.
1073 static struct iris_compiled_shader
*
1074 iris_compile_gs(struct iris_context
*ice
,
1075 struct iris_uncompiled_shader
*ish
,
1076 const struct brw_gs_prog_key
*key
)
1078 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1079 const struct brw_compiler
*compiler
= screen
->compiler
;
1080 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1081 void *mem_ctx
= ralloc_context(NULL
);
1082 struct brw_gs_prog_data
*gs_prog_data
=
1083 rzalloc(mem_ctx
, struct brw_gs_prog_data
);
1084 struct brw_vue_prog_data
*vue_prog_data
= &gs_prog_data
->base
;
1085 struct brw_stage_prog_data
*prog_data
= &vue_prog_data
->base
;
1086 enum brw_param_builtin
*system_values
;
1087 unsigned num_system_values
;
1089 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1091 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1092 &num_system_values
);
1094 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 0,
1097 brw_compute_vue_map(devinfo
,
1098 &vue_prog_data
->vue_map
, nir
->info
.outputs_written
,
1099 nir
->info
.separate_shader
);
1101 char *error_str
= NULL
;
1102 const unsigned *program
=
1103 brw_compile_gs(compiler
, &ice
->dbg
, mem_ctx
, key
, gs_prog_data
, nir
,
1104 NULL
, -1, &error_str
);
1105 if (program
== NULL
) {
1106 dbg_printf("Failed to compile geometry shader: %s\n", error_str
);
1107 ralloc_free(mem_ctx
);
1111 uint32_t *so_decls
=
1112 ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
1113 &vue_prog_data
->vue_map
);
1115 struct iris_compiled_shader
*shader
=
1116 iris_upload_shader(ice
, IRIS_CACHE_GS
, sizeof(*key
), key
, program
,
1117 prog_data
, so_decls
, system_values
, num_system_values
);
1119 if (ish
->compiled_once
) {
1120 perf_debug(&ice
->dbg
, "Recompiling geometry shader\n");
1122 ish
->compiled_once
= true;
1125 ralloc_free(mem_ctx
);
1130 * Update the current geometry shader variant.
1132 * Fill out the key, look in the cache, compile and bind if needed.
1135 iris_update_compiled_gs(struct iris_context
*ice
)
1137 struct iris_uncompiled_shader
*ish
=
1138 ice
->shaders
.uncompiled
[MESA_SHADER_GEOMETRY
];
1139 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_GS
];
1140 struct iris_compiled_shader
*shader
= NULL
;
1143 struct brw_gs_prog_key key
= { KEY_INIT
};
1144 ice
->vtbl
.populate_gs_key(ice
, &key
);
1147 iris_find_cached_shader(ice
, IRIS_CACHE_GS
, sizeof(key
), &key
);
1150 shader
= iris_compile_gs(ice
, ish
, &key
);
1153 if (old
!= shader
) {
1154 ice
->shaders
.prog
[IRIS_CACHE_GS
] = shader
;
1155 ice
->state
.dirty
|= IRIS_DIRTY_GS
|
1156 IRIS_DIRTY_BINDINGS_GS
|
1157 IRIS_DIRTY_CONSTANTS_GS
;
1162 * Compile a fragment (pixel) shader, and upload the assembly.
1164 static struct iris_compiled_shader
*
1165 iris_compile_fs(struct iris_context
*ice
,
1166 struct iris_uncompiled_shader
*ish
,
1167 const struct brw_wm_prog_key
*key
,
1168 struct brw_vue_map
*vue_map
)
1170 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1171 const struct brw_compiler
*compiler
= screen
->compiler
;
1172 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1173 void *mem_ctx
= ralloc_context(NULL
);
1174 struct brw_wm_prog_data
*fs_prog_data
=
1175 rzalloc(mem_ctx
, struct brw_wm_prog_data
);
1176 struct brw_stage_prog_data
*prog_data
= &fs_prog_data
->base
;
1177 enum brw_param_builtin
*system_values
;
1178 unsigned num_system_values
;
1180 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1184 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1185 &num_system_values
);
1187 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
,
1188 MAX2(key
->nr_color_regions
, 1),
1190 char *error_str
= NULL
;
1191 const unsigned *program
=
1192 brw_compile_fs(compiler
, &ice
->dbg
, mem_ctx
, key
, fs_prog_data
,
1193 nir
, NULL
, -1, -1, -1, true, false, vue_map
, &error_str
);
1194 if (program
== NULL
) {
1195 dbg_printf("Failed to compile fragment shader: %s\n", error_str
);
1196 ralloc_free(mem_ctx
);
1200 struct iris_compiled_shader
*shader
=
1201 iris_upload_shader(ice
, IRIS_CACHE_FS
, sizeof(*key
), key
, program
,
1202 prog_data
, NULL
, system_values
, num_system_values
);
1204 if (ish
->compiled_once
) {
1205 perf_debug(&ice
->dbg
, "Recompiling fragment shader\n");
1207 ish
->compiled_once
= true;
1210 ralloc_free(mem_ctx
);
1215 * Update the current fragment shader variant.
1217 * Fill out the key, look in the cache, compile and bind if needed.
1220 iris_update_compiled_fs(struct iris_context
*ice
)
1222 struct iris_uncompiled_shader
*ish
=
1223 ice
->shaders
.uncompiled
[MESA_SHADER_FRAGMENT
];
1224 struct brw_wm_prog_key key
= { KEY_INIT
};
1225 ice
->vtbl
.populate_fs_key(ice
, &key
);
1227 if (ish
->nos
& (1ull << IRIS_NOS_LAST_VUE_MAP
))
1228 key
.input_slots_valid
= ice
->shaders
.last_vue_map
->slots_valid
;
1230 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_FS
];
1231 struct iris_compiled_shader
*shader
=
1232 iris_find_cached_shader(ice
, IRIS_CACHE_FS
, sizeof(key
), &key
);
1235 shader
= iris_compile_fs(ice
, ish
, &key
, ice
->shaders
.last_vue_map
);
1237 if (old
!= shader
) {
1238 // XXX: only need to flag CLIP if barycentric has NONPERSPECTIVE
1239 // toggles. might be able to avoid flagging SBE too.
1240 ice
->shaders
.prog
[IRIS_CACHE_FS
] = shader
;
1241 ice
->state
.dirty
|= IRIS_DIRTY_FS
|
1242 IRIS_DIRTY_BINDINGS_FS
|
1243 IRIS_DIRTY_CONSTANTS_FS
|
1251 * Get the compiled shader for the last enabled geometry stage.
1253 * This stage is the one which will feed stream output and the rasterizer.
1255 static gl_shader_stage
1256 last_vue_stage(struct iris_context
*ice
)
1258 if (ice
->shaders
.prog
[MESA_SHADER_GEOMETRY
])
1259 return MESA_SHADER_GEOMETRY
;
1261 if (ice
->shaders
.prog
[MESA_SHADER_TESS_EVAL
])
1262 return MESA_SHADER_TESS_EVAL
;
1264 return MESA_SHADER_VERTEX
;
1268 * Update the last enabled stage's VUE map.
1270 * When the shader feeding the rasterizer's output interface changes, we
1271 * need to re-emit various packets.
1274 update_last_vue_map(struct iris_context
*ice
,
1275 struct brw_stage_prog_data
*prog_data
)
1277 struct brw_vue_prog_data
*vue_prog_data
= (void *) prog_data
;
1278 struct brw_vue_map
*vue_map
= &vue_prog_data
->vue_map
;
1279 struct brw_vue_map
*old_map
= ice
->shaders
.last_vue_map
;
1280 const uint64_t changed_slots
=
1281 (old_map
? old_map
->slots_valid
: 0ull) ^ vue_map
->slots_valid
;
1283 if (changed_slots
& VARYING_BIT_VIEWPORT
) {
1284 // XXX: could use ctx->Const.MaxViewports for old API efficiency
1285 ice
->state
.num_viewports
=
1286 (vue_map
->slots_valid
& VARYING_BIT_VIEWPORT
) ? IRIS_MAX_VIEWPORTS
: 1;
1287 ice
->state
.dirty
|= IRIS_DIRTY_CLIP
|
1288 IRIS_DIRTY_SF_CL_VIEWPORT
|
1289 IRIS_DIRTY_CC_VIEWPORT
|
1290 IRIS_DIRTY_SCISSOR_RECT
|
1291 IRIS_DIRTY_UNCOMPILED_FS
|
1292 ice
->state
.dirty_for_nos
[IRIS_NOS_LAST_VUE_MAP
];
1293 // XXX: CC_VIEWPORT?
1296 if (changed_slots
|| (old_map
&& old_map
->separate
!= vue_map
->separate
)) {
1297 ice
->state
.dirty
|= IRIS_DIRTY_SBE
;
1300 ice
->shaders
.last_vue_map
= &vue_prog_data
->vue_map
;
1304 * Get the prog_data for a given stage, or NULL if the stage is disabled.
1306 static struct brw_vue_prog_data
*
1307 get_vue_prog_data(struct iris_context
*ice
, gl_shader_stage stage
)
1309 if (!ice
->shaders
.prog
[stage
])
1312 return (void *) ice
->shaders
.prog
[stage
]->prog_data
;
1315 // XXX: iris_compiled_shaders are space-leaking :(
1316 // XXX: do remember to unbind them if deleting them.
1319 * Update the current shader variants for the given state.
1321 * This should be called on every draw call to ensure that the correct
1322 * shaders are bound. It will also flag any dirty state triggered by
1323 * swapping out those shaders.
1326 iris_update_compiled_shaders(struct iris_context
*ice
)
1328 const uint64_t dirty
= ice
->state
.dirty
;
1330 struct brw_vue_prog_data
*old_prog_datas
[4];
1331 if (!(dirty
& IRIS_DIRTY_URB
)) {
1332 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++)
1333 old_prog_datas
[i
] = get_vue_prog_data(ice
, i
);
1336 if (dirty
& (IRIS_DIRTY_UNCOMPILED_TCS
| IRIS_DIRTY_UNCOMPILED_TES
)) {
1337 struct iris_uncompiled_shader
*tes
=
1338 ice
->shaders
.uncompiled
[MESA_SHADER_TESS_EVAL
];
1340 iris_update_compiled_tcs(ice
);
1341 iris_update_compiled_tes(ice
);
1343 ice
->shaders
.prog
[IRIS_CACHE_TCS
] = NULL
;
1344 ice
->shaders
.prog
[IRIS_CACHE_TES
] = NULL
;
1346 IRIS_DIRTY_TCS
| IRIS_DIRTY_TES
|
1347 IRIS_DIRTY_BINDINGS_TCS
| IRIS_DIRTY_BINDINGS_TES
|
1348 IRIS_DIRTY_CONSTANTS_TCS
| IRIS_DIRTY_CONSTANTS_TES
;
1352 if (dirty
& IRIS_DIRTY_UNCOMPILED_VS
)
1353 iris_update_compiled_vs(ice
);
1354 if (dirty
& IRIS_DIRTY_UNCOMPILED_GS
)
1355 iris_update_compiled_gs(ice
);
1357 gl_shader_stage last_stage
= last_vue_stage(ice
);
1358 struct iris_compiled_shader
*shader
= ice
->shaders
.prog
[last_stage
];
1359 struct iris_uncompiled_shader
*ish
= ice
->shaders
.uncompiled
[last_stage
];
1360 update_last_vue_map(ice
, shader
->prog_data
);
1361 if (ice
->state
.streamout
!= shader
->streamout
) {
1362 ice
->state
.streamout
= shader
->streamout
;
1363 ice
->state
.dirty
|= IRIS_DIRTY_SO_DECL_LIST
| IRIS_DIRTY_STREAMOUT
;
1366 if (ice
->state
.streamout_active
) {
1367 for (int i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
1368 struct iris_stream_output_target
*so
=
1369 (void *) ice
->state
.so_target
[i
];
1371 so
->stride
= ish
->stream_output
.stride
[i
];
1375 if (dirty
& IRIS_DIRTY_UNCOMPILED_FS
)
1376 iris_update_compiled_fs(ice
);
1379 /* Changing shader interfaces may require a URB configuration. */
1380 if (!(dirty
& IRIS_DIRTY_URB
)) {
1381 for (int i
= MESA_SHADER_VERTEX
; i
<= MESA_SHADER_GEOMETRY
; i
++) {
1382 struct brw_vue_prog_data
*old
= old_prog_datas
[i
];
1383 struct brw_vue_prog_data
*new = get_vue_prog_data(ice
, i
);
1384 if (!!old
!= !!new ||
1385 (new && new->urb_entry_size
!= old
->urb_entry_size
)) {
1386 ice
->state
.dirty
|= IRIS_DIRTY_URB
;
1393 static struct iris_compiled_shader
*
1394 iris_compile_cs(struct iris_context
*ice
,
1395 struct iris_uncompiled_shader
*ish
,
1396 const struct brw_cs_prog_key
*key
)
1398 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1399 const struct brw_compiler
*compiler
= screen
->compiler
;
1400 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1401 void *mem_ctx
= ralloc_context(NULL
);
1402 struct brw_cs_prog_data
*cs_prog_data
=
1403 rzalloc(mem_ctx
, struct brw_cs_prog_data
);
1404 struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
1405 enum brw_param_builtin
*system_values
;
1406 unsigned num_system_values
;
1408 nir_shader
*nir
= nir_shader_clone(mem_ctx
, ish
->nir
);
1410 cs_prog_data
->binding_table
.work_groups_start
= 0;
1412 prog_data
->total_shared
= nir
->info
.cs
.shared_size
;
1414 iris_setup_uniforms(compiler
, mem_ctx
, nir
, prog_data
, &system_values
,
1415 &num_system_values
);
1417 assign_common_binding_table_offsets(devinfo
, nir
, prog_data
, 1,
1420 char *error_str
= NULL
;
1421 const unsigned *program
=
1422 brw_compile_cs(compiler
, &ice
->dbg
, mem_ctx
, key
, cs_prog_data
,
1423 nir
, -1, &error_str
);
1424 if (program
== NULL
) {
1425 dbg_printf("Failed to compile compute shader: %s\n", error_str
);
1426 ralloc_free(mem_ctx
);
1430 struct iris_compiled_shader
*shader
=
1431 iris_upload_shader(ice
, IRIS_CACHE_CS
, sizeof(*key
), key
, program
,
1432 prog_data
, NULL
, system_values
, num_system_values
);
1434 if (ish
->compiled_once
) {
1435 perf_debug(&ice
->dbg
, "Recompiling compute shader\n");
1437 ish
->compiled_once
= true;
1440 ralloc_free(mem_ctx
);
1445 iris_update_compiled_compute_shader(struct iris_context
*ice
)
1447 struct iris_uncompiled_shader
*ish
=
1448 ice
->shaders
.uncompiled
[MESA_SHADER_COMPUTE
];
1450 struct brw_cs_prog_key key
= { KEY_INIT
};
1451 ice
->vtbl
.populate_cs_key(ice
, &key
);
1453 struct iris_compiled_shader
*old
= ice
->shaders
.prog
[IRIS_CACHE_CS
];
1454 struct iris_compiled_shader
*shader
=
1455 iris_find_cached_shader(ice
, IRIS_CACHE_CS
, sizeof(key
), &key
);
1458 shader
= iris_compile_cs(ice
, ish
, &key
);
1460 if (old
!= shader
) {
1461 ice
->shaders
.prog
[IRIS_CACHE_CS
] = shader
;
1462 ice
->state
.dirty
|= IRIS_DIRTY_CS
|
1463 IRIS_DIRTY_BINDINGS_CS
|
1464 IRIS_DIRTY_CONSTANTS_CS
;
1469 iris_fill_cs_push_const_buffer(struct brw_cs_prog_data
*cs_prog_data
,
1472 struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
1473 assert(cs_prog_data
->push
.total
.size
> 0);
1474 assert(cs_prog_data
->push
.cross_thread
.size
== 0);
1475 assert(cs_prog_data
->push
.per_thread
.dwords
== 1);
1476 assert(prog_data
->param
[0] == BRW_PARAM_BUILTIN_SUBGROUP_ID
);
1477 for (unsigned t
= 0; t
< cs_prog_data
->threads
; t
++)
1482 * Allocate scratch BOs as needed for the given per-thread size and stage.
1485 iris_get_scratch_space(struct iris_context
*ice
,
1486 unsigned per_thread_scratch
,
1487 gl_shader_stage stage
)
1489 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
1490 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
1491 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
1493 unsigned encoded_size
= ffs(per_thread_scratch
) - 11;
1494 assert(encoded_size
< (1 << 16));
1496 struct iris_bo
**bop
= &ice
->shaders
.scratch_bos
[encoded_size
][stage
];
1498 /* The documentation for 3DSTATE_PS "Scratch Space Base Pointer" says:
1500 * "Scratch Space per slice is computed based on 4 sub-slices. SW must
1501 * allocate scratch space enough so that each slice has 4 slices
1504 * According to the other driver team, this applies to compute shaders
1505 * as well. This is not currently documented at all.
1507 unsigned subslice_total
= 4 * devinfo
->num_slices
;
1508 assert(subslice_total
>= screen
->subslice_total
);
1511 unsigned scratch_ids_per_subslice
= devinfo
->max_cs_threads
;
1512 uint32_t max_threads
[] = {
1513 [MESA_SHADER_VERTEX
] = devinfo
->max_vs_threads
,
1514 [MESA_SHADER_TESS_CTRL
] = devinfo
->max_tcs_threads
,
1515 [MESA_SHADER_TESS_EVAL
] = devinfo
->max_tes_threads
,
1516 [MESA_SHADER_GEOMETRY
] = devinfo
->max_gs_threads
,
1517 [MESA_SHADER_FRAGMENT
] = devinfo
->max_wm_threads
,
1518 [MESA_SHADER_COMPUTE
] = scratch_ids_per_subslice
* subslice_total
,
1521 uint32_t size
= per_thread_scratch
* max_threads
[stage
];
1523 *bop
= iris_bo_alloc(bufmgr
, "scratch", size
, IRIS_MEMZONE_SHADER
);
1530 iris_init_program_functions(struct pipe_context
*ctx
)
1532 ctx
->create_vs_state
= iris_create_vs_state
;
1533 ctx
->create_tcs_state
= iris_create_tcs_state
;
1534 ctx
->create_tes_state
= iris_create_tes_state
;
1535 ctx
->create_gs_state
= iris_create_gs_state
;
1536 ctx
->create_fs_state
= iris_create_fs_state
;
1537 ctx
->create_compute_state
= iris_create_compute_state
;
1539 ctx
->delete_vs_state
= iris_delete_shader_state
;
1540 ctx
->delete_tcs_state
= iris_delete_shader_state
;
1541 ctx
->delete_tes_state
= iris_delete_shader_state
;
1542 ctx
->delete_gs_state
= iris_delete_shader_state
;
1543 ctx
->delete_fs_state
= iris_delete_shader_state
;
1544 ctx
->delete_compute_state
= iris_delete_shader_state
;
1546 ctx
->bind_vs_state
= iris_bind_vs_state
;
1547 ctx
->bind_tcs_state
= iris_bind_tcs_state
;
1548 ctx
->bind_tes_state
= iris_bind_tes_state
;
1549 ctx
->bind_gs_state
= iris_bind_gs_state
;
1550 ctx
->bind_fs_state
= iris_bind_fs_state
;
1551 ctx
->bind_compute_state
= iris_bind_cs_state
;