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_pipeline_layout_state
{
31 const struct anv_pipeline_layout
*layout
;
37 get_surface_index(unsigned set
, unsigned binding
,
38 struct apply_pipeline_layout_state
*state
)
40 assert(set
< state
->layout
->num_sets
);
41 struct anv_descriptor_set_layout
*set_layout
=
42 state
->layout
->set
[set
].layout
;
44 gl_shader_stage stage
= state
->shader
->stage
;
46 assert(binding
< set_layout
->binding_count
);
48 assert(set_layout
->binding
[binding
].stage
[stage
].surface_index
>= 0);
50 uint32_t surface_index
=
51 state
->layout
->set
[set
].stage
[stage
].surface_start
+
52 set_layout
->binding
[binding
].stage
[stage
].surface_index
;
54 assert(surface_index
< state
->layout
->stage
[stage
].surface_count
);
60 get_sampler_index(unsigned set
, unsigned binding
, nir_texop tex_op
,
61 struct apply_pipeline_layout_state
*state
)
63 assert(set
< state
->layout
->num_sets
);
64 struct anv_descriptor_set_layout
*set_layout
=
65 state
->layout
->set
[set
].layout
;
67 assert(binding
< set_layout
->binding_count
);
69 gl_shader_stage stage
= state
->shader
->stage
;
71 if (set_layout
->binding
[binding
].stage
[stage
].sampler_index
< 0) {
72 assert(tex_op
== nir_texop_txf
);
76 uint32_t sampler_index
=
77 state
->layout
->set
[set
].stage
[stage
].sampler_start
+
78 set_layout
->binding
[binding
].stage
[stage
].sampler_index
;
80 assert(sampler_index
< state
->layout
->stage
[stage
].sampler_count
);
86 lower_res_index_intrinsic(nir_intrinsic_instr
*intrin
,
87 struct apply_pipeline_layout_state
*state
)
89 nir_builder
*b
= &state
->builder
;
91 b
->cursor
= nir_before_instr(&intrin
->instr
);
93 uint32_t set
= intrin
->const_index
[0];
94 uint32_t binding
= intrin
->const_index
[1];
96 uint32_t surface_index
= get_surface_index(set
, binding
, state
);
98 nir_const_value
*const_block_idx
=
99 nir_src_as_const_value(intrin
->src
[0]);
101 nir_ssa_def
*block_index
;
102 if (const_block_idx
) {
103 block_index
= nir_imm_int(b
, surface_index
+ const_block_idx
->u
[0]);
105 block_index
= nir_iadd(b
, nir_imm_int(b
, surface_index
),
106 nir_ssa_for_src(b
, intrin
->src
[0], 1));
109 assert(intrin
->dest
.is_ssa
);
110 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
, nir_src_for_ssa(block_index
));
111 nir_instr_remove(&intrin
->instr
);
115 lower_tex_deref(nir_tex_instr
*tex
, nir_deref_var
*deref
,
116 unsigned *const_index
, nir_tex_src_type src_type
,
117 struct apply_pipeline_layout_state
*state
)
119 if (deref
->deref
.child
) {
120 assert(deref
->deref
.child
->deref_type
== nir_deref_type_array
);
121 nir_deref_array
*deref_array
= nir_deref_as_array(deref
->deref
.child
);
123 *const_index
+= deref_array
->base_offset
;
125 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
126 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
129 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
130 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
131 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
, &tex
->src
[i
].src
);
134 ralloc_free(tex
->src
);
137 /* Now we can go ahead and move the source over to being a
138 * first-class texture source.
140 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
142 assert(deref_array
->indirect
.is_ssa
);
143 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
- 1].src
,
144 deref_array
->indirect
);
150 cleanup_tex_deref(nir_tex_instr
*tex
, nir_deref_var
*deref
)
152 if (deref
->deref
.child
== NULL
)
155 nir_deref_array
*deref_array
= nir_deref_as_array(deref
->deref
.child
);
157 if (deref_array
->deref_array_type
!= nir_deref_array_type_indirect
)
160 nir_instr_rewrite_src(&tex
->instr
, &deref_array
->indirect
, NIR_SRC_INIT
);
164 lower_tex(nir_tex_instr
*tex
, struct apply_pipeline_layout_state
*state
)
166 /* No one should have come by and lowered it already */
167 assert(tex
->sampler
);
169 nir_deref_var
*tex_deref
= tex
->texture
? tex
->texture
: tex
->sampler
;
171 get_surface_index(tex_deref
->var
->data
.descriptor_set
,
172 tex_deref
->var
->data
.binding
, state
);
173 lower_tex_deref(tex
, tex_deref
, &tex
->texture_index
,
174 nir_tex_src_texture_offset
, state
);
177 get_sampler_index(tex
->sampler
->var
->data
.descriptor_set
,
178 tex
->sampler
->var
->data
.binding
, tex
->op
, state
);
179 lower_tex_deref(tex
, tex
->sampler
, &tex
->sampler_index
,
180 nir_tex_src_sampler_offset
, state
);
183 cleanup_tex_deref(tex
, tex
->texture
);
184 cleanup_tex_deref(tex
, tex
->sampler
);
190 apply_pipeline_layout_block(nir_block
*block
, void *void_state
)
192 struct apply_pipeline_layout_state
*state
= void_state
;
194 nir_foreach_instr_safe(block
, instr
) {
195 switch (instr
->type
) {
196 case nir_instr_type_intrinsic
: {
197 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
198 if (intrin
->intrinsic
== nir_intrinsic_vulkan_resource_index
) {
199 lower_res_index_intrinsic(intrin
, state
);
200 state
->progress
= true;
204 case nir_instr_type_tex
:
205 lower_tex(nir_instr_as_tex(instr
), state
);
206 /* All texture instructions need lowering */
207 state
->progress
= true;
218 anv_nir_apply_pipeline_layout(nir_shader
*shader
,
219 const struct anv_pipeline_layout
*layout
)
221 struct apply_pipeline_layout_state state
= {
226 nir_foreach_overload(shader
, overload
) {
227 if (overload
->impl
) {
228 nir_builder_init(&state
.builder
, overload
->impl
);
229 nir_foreach_block(overload
->impl
, apply_pipeline_layout_block
, &state
);
230 nir_metadata_preserve(overload
->impl
, nir_metadata_block_index
|
231 nir_metadata_dominance
);
235 return state
.progress
;