2 * Copyright © 2015 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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "glsl/nir/nir_builder.h"
27 struct apply_dynamic_offsets_state
{
32 struct anv_pipeline_layout
*layout
;
34 uint32_t indices_start
;
38 apply_dynamic_offsets_block(nir_block
*block
, void *void_state
)
40 struct apply_dynamic_offsets_state
*state
= void_state
;
41 struct anv_descriptor_set_layout
*set_layout
;
43 nir_foreach_instr_safe(block
, instr
) {
44 if (instr
->type
!= nir_instr_type_intrinsic
)
47 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
49 bool has_indirect
= false;
50 uint32_t set
, binding
;
51 switch (intrin
->intrinsic
) {
52 case nir_intrinsic_load_ubo_indirect
:
55 case nir_intrinsic_load_ubo
: {
56 set
= intrin
->const_index
[0];
58 nir_const_value
*const_binding
= nir_src_as_const_value(intrin
->src
[0]);
60 binding
= const_binding
->u
[0];
62 assert(0 && "need more info from the ir for this.");
67 continue; /* the loop */
70 set_layout
= state
->layout
->set
[set
].layout
;
71 if (set_layout
->binding
[binding
].dynamic_offset_index
< 0)
74 uint32_t index
= state
->layout
->set
[set
].dynamic_offset_start
+
75 set_layout
->binding
[binding
].dynamic_offset_index
;
77 state
->builder
.cursor
= nir_before_instr(&intrin
->instr
);
79 nir_intrinsic_instr
*offset_load
=
80 nir_intrinsic_instr_create(state
->shader
, nir_intrinsic_load_uniform
);
81 offset_load
->num_components
= 1;
82 offset_load
->const_index
[0] = state
->indices_start
+ index
;
83 offset_load
->const_index
[1] = 0;
84 nir_ssa_dest_init(&offset_load
->instr
, &offset_load
->dest
, 1, NULL
);
85 nir_builder_instr_insert(&state
->builder
, &offset_load
->instr
);
87 nir_ssa_def
*offset
= &offset_load
->dest
.ssa
;
89 assert(intrin
->src
[1].is_ssa
);
90 offset
= nir_iadd(&state
->builder
, intrin
->src
[1].ssa
, offset
);
93 assert(intrin
->dest
.is_ssa
);
95 nir_intrinsic_instr
*new_load
=
96 nir_intrinsic_instr_create(state
->shader
,
97 nir_intrinsic_load_ubo_indirect
);
98 new_load
->num_components
= intrin
->num_components
;
99 new_load
->const_index
[0] = intrin
->const_index
[0];
100 new_load
->const_index
[1] = intrin
->const_index
[1];
101 nir_src_copy(&new_load
->src
[0], &intrin
->src
[0], &new_load
->instr
);
102 new_load
->src
[1] = nir_src_for_ssa(offset
);
103 nir_ssa_dest_init(&new_load
->instr
, &new_load
->dest
,
104 intrin
->dest
.ssa
.num_components
,
105 intrin
->dest
.ssa
.name
);
106 nir_builder_instr_insert(&state
->builder
, &new_load
->instr
);
108 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
109 nir_src_for_ssa(&new_load
->dest
.ssa
));
111 nir_instr_remove(&intrin
->instr
);
118 anv_nir_apply_dynamic_offsets(struct anv_pipeline
*pipeline
,
120 struct brw_stage_prog_data
*prog_data
)
122 struct apply_dynamic_offsets_state state
= {
124 .stage
= anv_vk_shader_stage_for_mesa_stage(shader
->stage
),
125 .layout
= pipeline
->layout
,
126 .indices_start
= shader
->num_uniforms
,
129 if (!state
.layout
|| !state
.layout
->stage
[state
.stage
].has_dynamic_offsets
)
132 nir_foreach_overload(shader
, overload
) {
133 if (overload
->impl
) {
134 nir_builder_init(&state
.builder
, overload
->impl
);
135 nir_foreach_block(overload
->impl
, apply_dynamic_offsets_block
, &state
);
136 nir_metadata_preserve(overload
->impl
, nir_metadata_block_index
|
137 nir_metadata_dominance
);
141 struct anv_push_constants
*null_data
= NULL
;
142 for (unsigned i
= 0; i
< MAX_DYNAMIC_BUFFERS
; i
++)
143 prog_data
->param
[i
+ shader
->num_uniforms
] =
144 (const gl_constant_value
*)&null_data
->dynamic_offsets
[i
];
146 shader
->num_uniforms
+= MAX_DYNAMIC_BUFFERS
;