nir/system_values: Use the bit size from the load_deref
[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_const_value local_size;
35 memset(&local_size, 0, sizeof(local_size));
36 local_size.u32[0] = b->shader->info.cs.local_size[0];
37 local_size.u32[1] = b->shader->info.cs.local_size[1];
38 local_size.u32[2] = b->shader->info.cs.local_size[2];
39 return nir_build_imm(b, 3, 32, local_size);
40 }
41
42 static bool
43 convert_block(nir_block *block, nir_builder *b)
44 {
45 bool progress = false;
46
47 nir_foreach_instr_safe(instr, block) {
48 if (instr->type != nir_instr_type_intrinsic)
49 continue;
50
51 nir_intrinsic_instr *load_deref = nir_instr_as_intrinsic(instr);
52 if (load_deref->intrinsic != nir_intrinsic_load_deref)
53 continue;
54
55 nir_deref_instr *deref = nir_src_as_deref(load_deref->src[0]);
56 if (deref->mode != nir_var_system_value)
57 continue;
58
59 if (deref->deref_type != nir_deref_type_var) {
60 /* The only one system value that is an array and that is
61 * gl_SampleMask which is always an array of one element.
62 */
63 assert(deref->deref_type == nir_deref_type_array);
64 deref = nir_deref_instr_parent(deref);
65 assert(deref->deref_type == nir_deref_type_var);
66 assert(deref->var->data.location == SYSTEM_VALUE_SAMPLE_MASK_IN);
67 }
68 nir_variable *var = deref->var;
69
70 b->cursor = nir_after_instr(&load_deref->instr);
71
72 nir_ssa_def *sysval = NULL;
73 switch (var->data.location) {
74 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID: {
75 /* From the GLSL man page for gl_GlobalInvocationID:
76 *
77 * "The value of gl_GlobalInvocationID is equal to
78 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID"
79 */
80 nir_ssa_def *group_size = build_local_group_size(b);
81 nir_ssa_def *group_id = nir_load_work_group_id(b);
82 nir_ssa_def *local_id = nir_load_local_invocation_id(b);
83
84 sysval = nir_iadd(b, nir_imul(b, group_id, group_size), local_id);
85 break;
86 }
87
88 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX: {
89 /* If lower_cs_local_index_from_id is true, then we derive the local
90 * index from the local id.
91 */
92 if (!b->shader->options->lower_cs_local_index_from_id)
93 break;
94
95 /* From the GLSL man page for gl_LocalInvocationIndex:
96 *
97 * "The value of gl_LocalInvocationIndex is equal to
98 * gl_LocalInvocationID.z * gl_WorkGroupSize.x *
99 * gl_WorkGroupSize.y + gl_LocalInvocationID.y *
100 * gl_WorkGroupSize.x + gl_LocalInvocationID.x"
101 */
102 nir_ssa_def *local_id = nir_load_local_invocation_id(b);
103
104 nir_ssa_def *size_x =
105 nir_imm_int(b, b->shader->info.cs.local_size[0]);
106 nir_ssa_def *size_y =
107 nir_imm_int(b, b->shader->info.cs.local_size[1]);
108
109 sysval = nir_imul(b, nir_channel(b, local_id, 2),
110 nir_imul(b, size_x, size_y));
111 sysval = nir_iadd(b, sysval,
112 nir_imul(b, nir_channel(b, local_id, 1), size_x));
113 sysval = nir_iadd(b, sysval, nir_channel(b, local_id, 0));
114 break;
115 }
116
117 case SYSTEM_VALUE_LOCAL_GROUP_SIZE: {
118 sysval = build_local_group_size(b);
119 break;
120 }
121
122 case SYSTEM_VALUE_VERTEX_ID:
123 if (b->shader->options->vertex_id_zero_based) {
124 sysval = nir_iadd(b,
125 nir_load_vertex_id_zero_base(b),
126 nir_load_first_vertex(b));
127 } else {
128 sysval = nir_load_vertex_id(b);
129 }
130 break;
131
132 case SYSTEM_VALUE_BASE_VERTEX:
133 /**
134 * From the OpenGL 4.6 (11.1.3.9 Shader Inputs) specification:
135 *
136 * "gl_BaseVertex holds the integer value passed to the baseVertex
137 * parameter to the command that resulted in the current shader
138 * invocation. In the case where the command has no baseVertex
139 * parameter, the value of gl_BaseVertex is zero."
140 */
141 if (b->shader->options->lower_base_vertex)
142 sysval = nir_iand(b,
143 nir_load_is_indexed_draw(b),
144 nir_load_first_vertex(b));
145 break;
146
147 case SYSTEM_VALUE_HELPER_INVOCATION:
148 if (b->shader->options->lower_helper_invocation) {
149 nir_ssa_def *tmp;
150
151 tmp = nir_ishl(b,
152 nir_imm_int(b, 1),
153 nir_load_sample_id_no_per_sample(b));
154
155 tmp = nir_iand(b,
156 nir_load_sample_mask_in(b),
157 tmp);
158
159 sysval = nir_inot(b, nir_i2b(b, tmp));
160 }
161
162 break;
163
164 case SYSTEM_VALUE_INSTANCE_INDEX:
165 sysval = nir_iadd(b,
166 nir_load_instance_id(b),
167 nir_load_base_instance(b));
168 break;
169
170 case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
171 case SYSTEM_VALUE_SUBGROUP_GE_MASK:
172 case SYSTEM_VALUE_SUBGROUP_GT_MASK:
173 case SYSTEM_VALUE_SUBGROUP_LE_MASK:
174 case SYSTEM_VALUE_SUBGROUP_LT_MASK: {
175 nir_intrinsic_op op =
176 nir_intrinsic_from_system_value(var->data.location);
177 nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
178 nir_ssa_dest_init_for_type(&load->instr, &load->dest,
179 var->type, NULL);
180 load->num_components = load->dest.ssa.num_components;
181 nir_builder_instr_insert(b, &load->instr);
182 sysval = &load->dest.ssa;
183 break;
184 }
185
186 case SYSTEM_VALUE_DEVICE_INDEX:
187 if (b->shader->options->lower_device_index_to_zero)
188 sysval = nir_imm_int(b, 0);
189 break;
190
191 case SYSTEM_VALUE_GLOBAL_GROUP_SIZE: {
192 nir_ssa_def *group_size = build_local_group_size(b);
193 nir_ssa_def *num_work_groups = nir_load_num_work_groups(b);
194 sysval = nir_imul(b, group_size, num_work_groups);
195 break;
196 }
197
198 default:
199 break;
200 }
201
202 if (sysval == NULL) {
203 nir_intrinsic_op sysval_op =
204 nir_intrinsic_from_system_value(var->data.location);
205 sysval = nir_load_system_value(b, sysval_op, 0);
206 sysval->bit_size = load_deref->dest.ssa.bit_size;
207 }
208
209 nir_ssa_def_rewrite_uses(&load_deref->dest.ssa, nir_src_for_ssa(sysval));
210 nir_instr_remove(&load_deref->instr);
211
212 progress = true;
213 }
214
215 return progress;
216 }
217
218 static bool
219 convert_impl(nir_function_impl *impl)
220 {
221 bool progress = false;
222 nir_builder builder;
223 nir_builder_init(&builder, impl);
224
225 nir_foreach_block(block, impl) {
226 progress |= convert_block(block, &builder);
227 }
228
229 nir_metadata_preserve(impl, nir_metadata_block_index |
230 nir_metadata_dominance);
231 return progress;
232 }
233
234 bool
235 nir_lower_system_values(nir_shader *shader)
236 {
237 bool progress = false;
238
239 nir_foreach_function(function, shader) {
240 if (function->impl)
241 progress = convert_impl(function->impl) || progress;
242 }
243
244 /* We're going to delete the variables so we need to clean up all those
245 * derefs we left lying around.
246 */
247 nir_remove_dead_derefs(shader);
248
249 exec_list_make_empty(&shader->system_values);
250
251 return progress;
252 }