aboutsummaryrefslogtreecommitdiffstats
path: root/src/vulkan/anv_nir_apply_dynamic_offsets.c
blob: dd4f5dfe5453cbdd6aaac83d0e543ccadd9f9aaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright © 2015 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "anv_nir.h"
#include "glsl/nir/nir_builder.h"

struct apply_dynamic_offsets_state {
   nir_shader *shader;
   nir_builder builder;

   VkShaderStage stage;
   struct anv_pipeline_layout *layout;

   uint32_t indices_start;
};

static bool
apply_dynamic_offsets_block(nir_block *block, void *void_state)
{
   struct apply_dynamic_offsets_state *state = void_state;
   struct anv_descriptor_set_layout *set_layout;

   nir_builder *b = &state->builder;

   nir_foreach_instr_safe(block, instr) {
      if (instr->type != nir_instr_type_intrinsic)
         continue;

      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);

      unsigned block_idx_src;
      switch (intrin->intrinsic) {
      case nir_intrinsic_load_ubo:
      case nir_intrinsic_load_ubo_indirect:
      case nir_intrinsic_load_ssbo:
      case nir_intrinsic_load_ssbo_indirect:
         block_idx_src = 0;
         break;
      case nir_intrinsic_store_ssbo:
      case nir_intrinsic_store_ssbo_indirect:
         block_idx_src = 1;
         break;
      default:
         continue; /* the loop */
      }

      nir_instr *res_instr = intrin->src[block_idx_src].ssa->parent_instr;
      assert(res_instr->type == nir_instr_type_intrinsic);
      nir_intrinsic_instr *res_intrin = nir_instr_as_intrinsic(res_instr);
      assert(res_intrin->intrinsic == nir_intrinsic_vulkan_resource_index);

      unsigned set = res_intrin->const_index[0];
      unsigned binding = res_intrin->const_index[1];

      set_layout = state->layout->set[set].layout;
      if (set_layout->binding[binding].dynamic_offset_index < 0)
         continue;

      b->cursor = nir_before_instr(&intrin->instr);

      int indirect_src;
      switch (intrin->intrinsic) {
      case nir_intrinsic_load_ubo_indirect:
      case nir_intrinsic_load_ssbo_indirect:
         indirect_src = 1;
         break;
      case nir_intrinsic_store_ssbo_indirect:
         indirect_src = 2;
         break;
      default:
         indirect_src = -1;
         break;
      }

      /* First, we need to generate the uniform load for the buffer offset */
      uint32_t index = state->layout->set[set].dynamic_offset_start +
                       set_layout->binding[binding].dynamic_offset_index;

      nir_const_value *const_arr_idx =
         nir_src_as_const_value(res_intrin->src[0]);

      nir_intrinsic_op offset_load_op;
      if (const_arr_idx)
         offset_load_op = nir_intrinsic_load_uniform;
      else
         offset_load_op = nir_intrinsic_load_uniform_indirect;

      nir_intrinsic_instr *offset_load =
         nir_intrinsic_instr_create(state->shader, offset_load_op);
      offset_load->num_components = 2;
      offset_load->const_index[0] = state->indices_start + index * 2;

      if (const_arr_idx) {
         offset_load->const_index[1] = const_arr_idx->u[0] * 2;
      } else {
         offset_load->const_index[1] = 0;
         offset_load->src[0] = nir_src_for_ssa(
            nir_imul(b, nir_ssa_for_src(b, res_intrin->src[0], 1),
                     nir_imm_int(b, 2)));
      }

      nir_ssa_dest_init(&offset_load->instr, &offset_load->dest, 2, NULL);
      nir_builder_instr_insert(b, &offset_load->instr);

      /* We calculate the full offset and don't bother with the base
       * offset.  We need the full offset for the predicate anyway.
       */
      nir_ssa_def *rel_offset = nir_imm_int(b, intrin->const_index[0]);
      if (indirect_src >= 0) {
         assert(intrin->src[indirect_src].is_ssa);
         rel_offset = nir_iadd(b, intrin->src[indirect_src].ssa, rel_offset);
      }
      nir_ssa_def *global_offset = nir_iadd(b, rel_offset,
                                            &offset_load->dest.ssa);

      /* Now we replace the load/store intrinsic */

      nir_intrinsic_op indirect_op;
      switch (intrin->intrinsic) {
      case nir_intrinsic_load_ubo:
         indirect_op = nir_intrinsic_load_ubo_indirect;
         break;
      case nir_intrinsic_load_ssbo:
         indirect_op = nir_intrinsic_load_ssbo_indirect;
         break;
      case nir_intrinsic_store_ssbo:
         indirect_op = nir_intrinsic_store_ssbo_indirect;
         break;
      default:
         unreachable("Invalid direct load/store intrinsic");
      }

      nir_intrinsic_instr *copy =
         nir_intrinsic_instr_create(state->shader, indirect_op);
      copy->num_components = intrin->num_components;

      /* The indirect is always the last source */
      indirect_src = nir_intrinsic_infos[indirect_op].num_srcs - 1;

      for (unsigned i = 0; i < (unsigned)indirect_src; i++)
         nir_src_copy(&copy->src[i], &intrin->src[i], &copy->instr);

      copy->src[indirect_src] = nir_src_for_ssa(global_offset);
      nir_ssa_dest_init(&copy->instr, &copy->dest,
                        intrin->dest.ssa.num_components,
                        intrin->dest.ssa.name);

      /* In order to avoid out-of-bounds access, we predicate */
      nir_ssa_def *pred = nir_fge(b, nir_channel(b, &offset_load->dest.ssa, 1),
                                  rel_offset);
      nir_if *if_stmt = nir_if_create(b->shader);
      if_stmt->condition = nir_src_for_ssa(pred);
      nir_cf_node_insert(b->cursor, &if_stmt->cf_node);

      nir_instr_insert_after_cf_list(&if_stmt->then_list, &copy->instr);

      if (indirect_op != nir_intrinsic_store_ssbo) {
         /* It's a load, we need a phi node */
         nir_phi_instr *phi = nir_phi_instr_create(b->shader);
         nir_ssa_dest_init(&phi->instr, &phi->dest,
                           intrin->num_components, NULL);

         nir_phi_src *src1 = ralloc(phi, nir_phi_src);
         struct exec_node *tnode = exec_list_get_tail(&if_stmt->then_list);
         src1->pred = exec_node_data(nir_block, tnode, cf_node.node);
         src1->src = nir_src_for_ssa(&copy->dest.ssa);
         exec_list_push_tail(&phi->srcs, &src1->node);

         b->cursor = nir_after_cf_list(&if_stmt->else_list);
         nir_ssa_def *zero = nir_build_imm(b, intrin->num_components,
            (nir_const_value) { .u = { 0, 0, 0, 0 } });

         nir_phi_src *src2 = ralloc(phi, nir_phi_src);
         struct exec_node *enode = exec_list_get_tail(&if_stmt->else_list);
         src2->pred = exec_node_data(nir_block, enode, cf_node.node);
         src2->src = nir_src_for_ssa(zero);
         exec_list_push_tail(&phi->srcs, &src2->node);

         nir_instr_insert_after_cf(&if_stmt->cf_node, &phi->instr);

         assert(intrin->dest.is_ssa);
         nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
                                  nir_src_for_ssa(&phi->dest.ssa));
      }

      nir_instr_remove(&intrin->instr);
   }

   return true;
}

void
anv_nir_apply_dynamic_offsets(struct anv_pipeline *pipeline,
                              nir_shader *shader,
                              struct brw_stage_prog_data *prog_data)
{
   struct apply_dynamic_offsets_state state = {
      .shader = shader,
      .stage = anv_vk_shader_stage_for_mesa_stage(shader->stage),
      .layout = pipeline->layout,
      .indices_start = shader->num_uniforms,
   };

   if (!state.layout || !state.layout->stage[state.stage].has_dynamic_offsets)
      return;

   nir_foreach_overload(shader, overload) {
      if (overload->impl) {
         nir_builder_init(&state.builder, overload->impl);
         nir_foreach_block(overload->impl, apply_dynamic_offsets_block, &state);
         nir_metadata_preserve(overload->impl, nir_metadata_block_index |
                                               nir_metadata_dominance);
      }
   }

   struct anv_push_constants *null_data = NULL;
   for (unsigned i = 0; i < MAX_DYNAMIC_BUFFERS; i++) {
      prog_data->param[i * 2 + shader->num_uniforms] =
         (const gl_constant_value *)&null_data->dynamic[i].offset;
      prog_data->param[i * 2 + 1 + shader->num_uniforms] =
         (const gl_constant_value *)&null_data->dynamic[i].range;
   }

   shader->num_uniforms += MAX_DYNAMIC_BUFFERS * 2;
}