intel,nir: Move gl_LocalInvocationID lowering to nir_lower_system_values
[mesa.git] / src / compiler / nir / nir_lower_system_values.c
1 /*
2 * Copyright © 2014 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 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "nir_builder.h"
30
31 static nir_ssa_def*
32 build_local_group_size(nir_builder *b)
33 {
34 nir_ssa_def *local_size;
35
36 /*
37 * If the local work group size is variable it can't be lowered at this
38 * point, but its intrinsic can still be used.
39 */
40 if (b->shader->info.cs.local_size_variable) {
41 local_size = nir_load_local_group_size(b);
42 } else {
43 nir_const_value local_size_const;
44 memset(&local_size_const, 0, sizeof(local_size_const));
45 local_size_const.u32[0] = b->shader->info.cs.local_size[0];
46 local_size_const.u32[1] = b->shader->info.cs.local_size[1];
47 local_size_const.u32[2] = b->shader->info.cs.local_size[2];
48 local_size = nir_build_imm(b, 3, 32, local_size_const);
49 }
50
51 return local_size;
52 }
53
54 static nir_ssa_def *
55 build_local_invocation_id(nir_builder *b)
56 {
57 if (b->shader->options->lower_cs_local_id_from_index) {
58 /* We lower gl_LocalInvocationID from gl_LocalInvocationIndex based
59 * on this formula:
60 *
61 * gl_LocalInvocationID.x =
62 * gl_LocalInvocationIndex % gl_WorkGroupSize.x;
63 * gl_LocalInvocationID.y =
64 * (gl_LocalInvocationIndex / gl_WorkGroupSize.x) %
65 * gl_WorkGroupSize.y;
66 * gl_LocalInvocationID.z =
67 * (gl_LocalInvocationIndex /
68 * (gl_WorkGroupSize.x * gl_WorkGroupSize.y)) %
69 * gl_WorkGroupSize.z;
70 *
71 * However, the final % gl_WorkGroupSize.z does nothing unless we
72 * accidentally end up with a gl_LocalInvocationIndex that is too
73 * large so it can safely be omitted.
74 */
75 nir_ssa_def *local_index = nir_load_local_invocation_index(b);
76 nir_ssa_def *local_size = build_local_group_size(b);
77
78 nir_ssa_def *id_x, *id_y, *id_z;
79 id_x = nir_umod(b, local_index,
80 nir_channel(b, local_size, 0));
81 id_y = nir_umod(b, nir_udiv(b, local_index,
82 nir_channel(b, local_size, 0)),
83 nir_channel(b, local_size, 1));
84 id_z = nir_udiv(b, local_index,
85 nir_imul(b, nir_channel(b, local_size, 0),
86 nir_channel(b, local_size, 1)));
87 return nir_vec3(b, id_x, id_y, id_z);
88 } else {
89 return nir_load_local_invocation_id(b);
90 }
91 }
92
93 static bool
94 convert_block(nir_block *block, nir_builder *b)
95 {
96 bool progress = false;
97
98 nir_foreach_instr_safe(instr, block) {
99 if (instr->type != nir_instr_type_intrinsic)
100 continue;
101
102 nir_intrinsic_instr *load_deref = nir_instr_as_intrinsic(instr);
103 if (load_deref->intrinsic != nir_intrinsic_load_deref)
104 continue;
105
106 nir_deref_instr *deref = nir_src_as_deref(load_deref->src[0]);
107 if (deref->mode != nir_var_system_value)
108 continue;
109
110 if (deref->deref_type != nir_deref_type_var) {
111 /* The only one system value that is an array and that is
112 * gl_SampleMask which is always an array of one element.
113 */
114 assert(deref->deref_type == nir_deref_type_array);
115 deref = nir_deref_instr_parent(deref);
116 assert(deref->deref_type == nir_deref_type_var);
117 assert(deref->var->data.location == SYSTEM_VALUE_SAMPLE_MASK_IN);
118 }
119 nir_variable *var = deref->var;
120
121 b->cursor = nir_after_instr(&load_deref->instr);
122
123 nir_ssa_def *sysval = NULL;
124 switch (var->data.location) {
125 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID: {
126 /* From the GLSL man page for gl_GlobalInvocationID:
127 *
128 * "The value of gl_GlobalInvocationID is equal to
129 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID"
130 */
131 nir_ssa_def *group_size = build_local_group_size(b);
132 nir_ssa_def *group_id = nir_load_work_group_id(b);
133 nir_ssa_def *local_id = build_local_invocation_id(b);
134
135 sysval = nir_iadd(b, nir_imul(b, group_id, group_size), local_id);
136 break;
137 }
138
139 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX: {
140 /* If lower_cs_local_index_from_id is true, then we derive the local
141 * index from the local id.
142 */
143 if (!b->shader->options->lower_cs_local_index_from_id)
144 break;
145
146 /* From the GLSL man page for gl_LocalInvocationIndex:
147 *
148 * "The value of gl_LocalInvocationIndex is equal to
149 * gl_LocalInvocationID.z * gl_WorkGroupSize.x *
150 * gl_WorkGroupSize.y + gl_LocalInvocationID.y *
151 * gl_WorkGroupSize.x + gl_LocalInvocationID.x"
152 */
153 nir_ssa_def *local_id = nir_load_local_invocation_id(b);
154
155 nir_ssa_def *size_x =
156 nir_imm_int(b, b->shader->info.cs.local_size[0]);
157 nir_ssa_def *size_y =
158 nir_imm_int(b, b->shader->info.cs.local_size[1]);
159
160 sysval = nir_imul(b, nir_channel(b, local_id, 2),
161 nir_imul(b, size_x, size_y));
162 sysval = nir_iadd(b, sysval,
163 nir_imul(b, nir_channel(b, local_id, 1), size_x));
164 sysval = nir_iadd(b, sysval, nir_channel(b, local_id, 0));
165 break;
166 }
167
168 case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
169 /* If lower_cs_local_id_from_index is true, then we derive the local
170 * index from the local id.
171 */
172 if (b->shader->options->lower_cs_local_id_from_index)
173 sysval = build_local_invocation_id(b);
174 break;
175
176 case SYSTEM_VALUE_LOCAL_GROUP_SIZE: {
177 sysval = build_local_group_size(b);
178 break;
179 }
180
181 case SYSTEM_VALUE_VERTEX_ID:
182 if (b->shader->options->vertex_id_zero_based) {
183 sysval = nir_iadd(b,
184 nir_load_vertex_id_zero_base(b),
185 nir_load_first_vertex(b));
186 } else {
187 sysval = nir_load_vertex_id(b);
188 }
189 break;
190
191 case SYSTEM_VALUE_BASE_VERTEX:
192 /**
193 * From the OpenGL 4.6 (11.1.3.9 Shader Inputs) specification:
194 *
195 * "gl_BaseVertex holds the integer value passed to the baseVertex
196 * parameter to the command that resulted in the current shader
197 * invocation. In the case where the command has no baseVertex
198 * parameter, the value of gl_BaseVertex is zero."
199 */
200 if (b->shader->options->lower_base_vertex)
201 sysval = nir_iand(b,
202 nir_load_is_indexed_draw(b),
203 nir_load_first_vertex(b));
204 break;
205
206 case SYSTEM_VALUE_HELPER_INVOCATION:
207 if (b->shader->options->lower_helper_invocation) {
208 nir_ssa_def *tmp;
209
210 tmp = nir_ishl(b,
211 nir_imm_int(b, 1),
212 nir_load_sample_id_no_per_sample(b));
213
214 tmp = nir_iand(b,
215 nir_load_sample_mask_in(b),
216 tmp);
217
218 sysval = nir_inot(b, nir_i2b(b, tmp));
219 }
220
221 break;
222
223 case SYSTEM_VALUE_INSTANCE_INDEX:
224 sysval = nir_iadd(b,
225 nir_load_instance_id(b),
226 nir_load_base_instance(b));
227 break;
228
229 case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
230 case SYSTEM_VALUE_SUBGROUP_GE_MASK:
231 case SYSTEM_VALUE_SUBGROUP_GT_MASK:
232 case SYSTEM_VALUE_SUBGROUP_LE_MASK:
233 case SYSTEM_VALUE_SUBGROUP_LT_MASK: {
234 nir_intrinsic_op op =
235 nir_intrinsic_from_system_value(var->data.location);
236 nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
237 nir_ssa_dest_init_for_type(&load->instr, &load->dest,
238 var->type, NULL);
239 load->num_components = load->dest.ssa.num_components;
240 nir_builder_instr_insert(b, &load->instr);
241 sysval = &load->dest.ssa;
242 break;
243 }
244
245 case SYSTEM_VALUE_DEVICE_INDEX:
246 if (b->shader->options->lower_device_index_to_zero)
247 sysval = nir_imm_int(b, 0);
248 break;
249
250 case SYSTEM_VALUE_GLOBAL_GROUP_SIZE: {
251 nir_ssa_def *group_size = build_local_group_size(b);
252 nir_ssa_def *num_work_groups = nir_load_num_work_groups(b);
253 sysval = nir_imul(b, group_size, num_work_groups);
254 break;
255 }
256
257 default:
258 break;
259 }
260
261 if (sysval == NULL) {
262 nir_intrinsic_op sysval_op =
263 nir_intrinsic_from_system_value(var->data.location);
264 sysval = nir_load_system_value(b, sysval_op, 0);
265 sysval->bit_size = load_deref->dest.ssa.bit_size;
266 }
267
268 nir_ssa_def_rewrite_uses(&load_deref->dest.ssa, nir_src_for_ssa(sysval));
269 nir_instr_remove(&load_deref->instr);
270
271 progress = true;
272 }
273
274 return progress;
275 }
276
277 static bool
278 convert_impl(nir_function_impl *impl)
279 {
280 bool progress = false;
281 nir_builder builder;
282 nir_builder_init(&builder, impl);
283
284 nir_foreach_block(block, impl) {
285 progress |= convert_block(block, &builder);
286 }
287
288 nir_metadata_preserve(impl, nir_metadata_block_index |
289 nir_metadata_dominance);
290 return progress;
291 }
292
293 bool
294 nir_lower_system_values(nir_shader *shader)
295 {
296 bool progress = false;
297
298 nir_foreach_function(function, shader) {
299 if (function->impl)
300 progress = convert_impl(function->impl) || progress;
301 }
302
303 /* We're going to delete the variables so we need to clean up all those
304 * derefs we left lying around.
305 */
306 nir_remove_dead_derefs(shader);
307
308 exec_list_make_empty(&shader->system_values);
309
310 return progress;
311 }