anv/apply_dynamic_offsets: Use rewrite_src instead of a regular assignment
[mesa.git] / src / intel / vulkan / anv_nir_apply_dynamic_offsets.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_nir.h"
25 #include "nir/nir_builder.h"
26
27 static void
28 apply_dynamic_offsets_block(nir_block *block, nir_builder *b,
29 const struct anv_pipeline_layout *layout,
30 uint32_t indices_start)
31 {
32 struct anv_descriptor_set_layout *set_layout;
33
34 nir_foreach_instr_safe(instr, block) {
35 if (instr->type != nir_instr_type_intrinsic)
36 continue;
37
38 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
39
40 unsigned block_idx_src;
41 switch (intrin->intrinsic) {
42 case nir_intrinsic_load_ubo:
43 case nir_intrinsic_load_ssbo:
44 block_idx_src = 0;
45 break;
46 case nir_intrinsic_store_ssbo:
47 block_idx_src = 1;
48 break;
49 default:
50 continue; /* the loop */
51 }
52
53 nir_instr *res_instr = intrin->src[block_idx_src].ssa->parent_instr;
54 assert(res_instr->type == nir_instr_type_intrinsic);
55 nir_intrinsic_instr *res_intrin = nir_instr_as_intrinsic(res_instr);
56 assert(res_intrin->intrinsic == nir_intrinsic_vulkan_resource_index);
57
58 unsigned set = res_intrin->const_index[0];
59 unsigned binding = res_intrin->const_index[1];
60
61 set_layout = layout->set[set].layout;
62 if (set_layout->binding[binding].dynamic_offset_index < 0)
63 continue;
64
65 b->cursor = nir_before_instr(&intrin->instr);
66
67 /* First, we need to generate the uniform load for the buffer offset */
68 uint32_t index = layout->set[set].dynamic_offset_start +
69 set_layout->binding[binding].dynamic_offset_index;
70 uint32_t array_size = set_layout->binding[binding].array_size;
71
72 nir_intrinsic_instr *offset_load =
73 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_uniform);
74 offset_load->num_components = 2;
75 nir_intrinsic_set_base(offset_load, indices_start + index * 8);
76 nir_intrinsic_set_range(offset_load, array_size * 8);
77 offset_load->src[0] = nir_src_for_ssa(nir_imul(b, res_intrin->src[0].ssa,
78 nir_imm_int(b, 8)));
79
80 nir_ssa_dest_init(&offset_load->instr, &offset_load->dest, 2, 32, NULL);
81 nir_builder_instr_insert(b, &offset_load->instr);
82
83 nir_src *offset_src = nir_get_io_offset_src(intrin);
84 nir_ssa_def *old_offset = nir_ssa_for_src(b, *offset_src, 1);
85 nir_ssa_def *new_offset = nir_iadd(b, old_offset, &offset_load->dest.ssa);
86 nir_instr_rewrite_src(&intrin->instr, offset_src,
87 nir_src_for_ssa(new_offset));
88
89 /* In order to avoid out-of-bounds access, we predicate */
90 nir_ssa_def *pred = nir_uge(b, nir_channel(b, &offset_load->dest.ssa, 1),
91 old_offset);
92 nir_if *if_stmt = nir_if_create(b->shader);
93 if_stmt->condition = nir_src_for_ssa(pred);
94 nir_cf_node_insert(b->cursor, &if_stmt->cf_node);
95
96 nir_instr_remove(&intrin->instr);
97 nir_instr_insert_after_cf_list(&if_stmt->then_list, &intrin->instr);
98
99 if (intrin->intrinsic != nir_intrinsic_store_ssbo) {
100 /* It's a load, we need a phi node */
101 nir_phi_instr *phi = nir_phi_instr_create(b->shader);
102 nir_ssa_dest_init(&phi->instr, &phi->dest,
103 intrin->num_components,
104 intrin->dest.ssa.bit_size, NULL);
105
106 nir_phi_src *src1 = ralloc(phi, nir_phi_src);
107 struct exec_node *tnode = exec_list_get_tail(&if_stmt->then_list);
108 src1->pred = exec_node_data(nir_block, tnode, cf_node.node);
109 src1->src = nir_src_for_ssa(&intrin->dest.ssa);
110 exec_list_push_tail(&phi->srcs, &src1->node);
111
112 b->cursor = nir_after_cf_list(&if_stmt->else_list);
113 nir_const_value zero_val = { .u32 = { 0, 0, 0, 0 } };
114 nir_ssa_def *zero = nir_build_imm(b, intrin->num_components,
115 intrin->dest.ssa.bit_size, zero_val);
116
117 nir_phi_src *src2 = ralloc(phi, nir_phi_src);
118 struct exec_node *enode = exec_list_get_tail(&if_stmt->else_list);
119 src2->pred = exec_node_data(nir_block, enode, cf_node.node);
120 src2->src = nir_src_for_ssa(zero);
121 exec_list_push_tail(&phi->srcs, &src2->node);
122
123 assert(intrin->dest.is_ssa);
124 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
125 nir_src_for_ssa(&phi->dest.ssa));
126
127 nir_instr_insert_after_cf(&if_stmt->cf_node, &phi->instr);
128 }
129 }
130 }
131
132 void
133 anv_nir_apply_dynamic_offsets(struct anv_pipeline *pipeline,
134 nir_shader *shader,
135 struct brw_stage_prog_data *prog_data)
136 {
137 const struct anv_pipeline_layout *layout = pipeline->layout;
138 if (!layout || !layout->stage[shader->stage].has_dynamic_offsets)
139 return;
140
141 nir_foreach_function(function, shader) {
142 if (!function->impl)
143 continue;
144
145 nir_builder builder;
146 nir_builder_init(&builder, function->impl);
147
148 nir_foreach_block(block, function->impl) {
149 apply_dynamic_offsets_block(block, &builder, pipeline->layout,
150 shader->num_uniforms);
151 }
152
153 nir_metadata_preserve(function->impl, nir_metadata_block_index |
154 nir_metadata_dominance);
155 }
156
157 struct anv_push_constants *null_data = NULL;
158 for (unsigned i = 0; i < MAX_DYNAMIC_BUFFERS; i++) {
159 prog_data->param[i * 2 + shader->num_uniforms / 4] =
160 (const union gl_constant_value *)&null_data->dynamic[i].offset;
161 prog_data->param[i * 2 + 1 + shader->num_uniforms / 4] =
162 (const union gl_constant_value *)&null_data->dynamic[i].range;
163 }
164
165 shader->num_uniforms += MAX_DYNAMIC_BUFFERS * 8;
166 }