anv: Completely rework descriptor set layouts
[mesa.git] / src / 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 "glsl/nir/nir_builder.h"
26
27 struct apply_dynamic_offsets_state {
28 nir_shader *shader;
29 nir_builder builder;
30
31 VkShaderStage stage;
32 struct anv_pipeline_layout *layout;
33
34 uint32_t indices_start;
35 };
36
37 static bool
38 apply_dynamic_offsets_block(nir_block *block, void *void_state)
39 {
40 struct apply_dynamic_offsets_state *state = void_state;
41 struct anv_descriptor_set_layout *set_layout;
42
43 nir_foreach_instr_safe(block, instr) {
44 if (instr->type != nir_instr_type_intrinsic)
45 continue;
46
47 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
48
49 bool has_indirect = false;
50 uint32_t set, binding;
51 switch (intrin->intrinsic) {
52 case nir_intrinsic_load_ubo_indirect:
53 has_indirect = true;
54 /* fallthrough */
55 case nir_intrinsic_load_ubo: {
56 set = intrin->const_index[0];
57
58 nir_const_value *const_binding = nir_src_as_const_value(intrin->src[0]);
59 if (const_binding) {
60 binding = const_binding->u[0];
61 } else {
62 assert(0 && "need more info from the ir for this.");
63 }
64 break;
65 }
66 default:
67 continue; /* the loop */
68 }
69
70 set_layout = state->layout->set[set].layout;
71 if (set_layout->binding[binding].dynamic_offset_index < 0)
72 continue;
73
74 uint32_t index = state->layout->set[set].dynamic_offset_start +
75 set_layout->binding[binding].dynamic_offset_index;
76
77 state->builder.cursor = nir_before_instr(&intrin->instr);
78
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);
86
87 nir_ssa_def *offset = &offset_load->dest.ssa;
88 if (has_indirect) {
89 assert(intrin->src[1].is_ssa);
90 offset = nir_iadd(&state->builder, intrin->src[1].ssa, offset);
91 }
92
93 assert(intrin->dest.is_ssa);
94
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);
107
108 nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
109 nir_src_for_ssa(&new_load->dest.ssa));
110
111 nir_instr_remove(&intrin->instr);
112 }
113
114 return true;
115 }
116
117 void
118 anv_nir_apply_dynamic_offsets(struct anv_pipeline *pipeline,
119 nir_shader *shader,
120 struct brw_stage_prog_data *prog_data)
121 {
122 struct apply_dynamic_offsets_state state = {
123 .shader = shader,
124 .stage = anv_vk_shader_stage_for_mesa_stage(shader->stage),
125 .layout = pipeline->layout,
126 .indices_start = shader->num_uniforms,
127 };
128
129 if (!state.layout || !state.layout->stage[state.stage].has_dynamic_offsets)
130 return;
131
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);
138 }
139 }
140
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];
145
146 shader->num_uniforms += MAX_DYNAMIC_BUFFERS;
147 }