nir/spirv: Use a C99-style initializer for structure fields
[mesa.git] / src / glsl / 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 struct lower_system_values_state {
32 nir_builder builder;
33 bool progress;
34 };
35
36 static bool
37 convert_block(nir_block *block, void *void_state)
38 {
39 struct lower_system_values_state *state = void_state;
40
41 nir_builder *b = &state->builder;
42
43 nir_foreach_instr_safe(block, instr) {
44 if (instr->type != nir_instr_type_intrinsic)
45 continue;
46
47 nir_intrinsic_instr *load_var = nir_instr_as_intrinsic(instr);
48
49 if (load_var->intrinsic != nir_intrinsic_load_var)
50 continue;
51
52 nir_variable *var = load_var->variables[0]->var;
53 if (var->data.mode != nir_var_system_value)
54 continue;
55
56 b->cursor = nir_after_instr(&load_var->instr);
57
58 nir_ssa_def *sysval;
59 switch (var->data.location) {
60 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID: {
61 /* From the GLSL man page for gl_GlobalInvocationID:
62 *
63 * "The value of gl_GlobalInvocationID is equal to
64 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID"
65 */
66
67 nir_const_value local_size;
68 local_size.u[0] = b->shader->info.cs.local_size[0];
69 local_size.u[1] = b->shader->info.cs.local_size[1];
70 local_size.u[2] = b->shader->info.cs.local_size[2];
71
72 nir_ssa_def *group_id =
73 nir_load_system_value(b, nir_intrinsic_load_work_group_id, 0);
74 nir_ssa_def *local_id =
75 nir_load_system_value(b, nir_intrinsic_load_invocation_id, 0);
76
77 sysval = nir_iadd(b, nir_imul(b, group_id,
78 nir_build_imm(b, 3, local_size)),
79 local_id);
80 break;
81 }
82
83 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX: {
84 /* From the GLSL man page for gl_LocalInvocationIndex:
85 *
86 * ?The value of gl_LocalInvocationIndex is equal to
87 * gl_LocalInvocationID.z * gl_WorkGroupSize.x *
88 * gl_WorkGroupSize.y + gl_LocalInvocationID.y *
89 * gl_WorkGroupSize.x + gl_LocalInvocationID.x"
90 */
91 nir_ssa_def *local_id =
92 nir_load_system_value(b, nir_intrinsic_load_invocation_id, 0);
93
94 unsigned stride_y = b->shader->info.cs.local_size[0];
95 unsigned stride_z = b->shader->info.cs.local_size[0] *
96 b->shader->info.cs.local_size[1];
97
98 sysval = nir_iadd(b, nir_imul(b, nir_channel(b, local_id, 2),
99 nir_imm_int(b, stride_z)),
100 nir_iadd(b, nir_imul(b, nir_channel(b, local_id, 1),
101 nir_imm_int(b, stride_y)),
102 nir_channel(b, local_id, 0)));
103 break;
104 }
105
106 default: {
107 nir_intrinsic_op sysval_op =
108 nir_intrinsic_from_system_value(var->data.location);
109 sysval = nir_load_system_value(b, sysval_op, 0);
110 break;
111 } /* default */
112 }
113
114 nir_ssa_def_rewrite_uses(&load_var->dest.ssa, nir_src_for_ssa(sysval));
115 nir_instr_remove(&load_var->instr);
116
117 state->progress = true;
118 }
119
120 return true;
121 }
122
123 static bool
124 convert_impl(nir_function_impl *impl)
125 {
126 struct lower_system_values_state state;
127
128 state.progress = false;
129 nir_builder_init(&state.builder, impl);
130
131 nir_foreach_block(impl, convert_block, &state);
132 nir_metadata_preserve(impl, nir_metadata_block_index |
133 nir_metadata_dominance);
134 return state.progress;
135 }
136
137 bool
138 nir_lower_system_values(nir_shader *shader)
139 {
140 bool progress = false;
141
142 nir_foreach_function(shader, function) {
143 if (function->impl)
144 progress = convert_impl(function->impl) || progress;
145 }
146
147 exec_list_make_empty(&shader->system_values);
148
149 return progress;
150 }