2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Connor Abbott (cwabbott0@gmail.com)
29 #include "nir_builder.h"
32 convert_block(nir_block
*block
, nir_builder
*b
)
34 bool progress
= false;
36 nir_foreach_instr_safe(instr
, block
) {
37 if (instr
->type
!= nir_instr_type_intrinsic
)
40 nir_intrinsic_instr
*load_var
= nir_instr_as_intrinsic(instr
);
42 if (load_var
->intrinsic
!= nir_intrinsic_load_var
)
45 nir_variable
*var
= load_var
->variables
[0]->var
;
46 if (var
->data
.mode
!= nir_var_system_value
)
49 b
->cursor
= nir_after_instr(&load_var
->instr
);
52 switch (var
->data
.location
) {
53 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID
: {
54 /* From the GLSL man page for gl_GlobalInvocationID:
56 * "The value of gl_GlobalInvocationID is equal to
57 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID"
60 nir_const_value local_size
;
61 local_size
.u32
[0] = b
->shader
->info
.cs
.local_size
[0];
62 local_size
.u32
[1] = b
->shader
->info
.cs
.local_size
[1];
63 local_size
.u32
[2] = b
->shader
->info
.cs
.local_size
[2];
65 nir_ssa_def
*group_id
=
66 nir_load_system_value(b
, nir_intrinsic_load_work_group_id
, 0);
67 nir_ssa_def
*local_id
=
68 nir_load_system_value(b
, nir_intrinsic_load_local_invocation_id
, 0);
70 sysval
= nir_iadd(b
, nir_imul(b
, group_id
,
71 nir_build_imm(b
, 3, 32, local_size
)),
76 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
: {
77 /* From the GLSL man page for gl_LocalInvocationIndex:
79 * "The value of gl_LocalInvocationIndex is equal to
80 * gl_LocalInvocationID.z * gl_WorkGroupSize.x *
81 * gl_WorkGroupSize.y + gl_LocalInvocationID.y *
82 * gl_WorkGroupSize.x + gl_LocalInvocationID.x"
84 nir_ssa_def
*local_id
=
85 nir_load_system_value(b
, nir_intrinsic_load_local_invocation_id
, 0);
87 nir_ssa_def
*size_x
= nir_imm_int(b
, b
->shader
->info
.cs
.local_size
[0]);
88 nir_ssa_def
*size_y
= nir_imm_int(b
, b
->shader
->info
.cs
.local_size
[1]);
90 sysval
= nir_imul(b
, nir_channel(b
, local_id
, 2),
91 nir_imul(b
, size_x
, size_y
));
92 sysval
= nir_iadd(b
, sysval
,
93 nir_imul(b
, nir_channel(b
, local_id
, 1), size_x
));
94 sysval
= nir_iadd(b
, sysval
, nir_channel(b
, local_id
, 0));
98 case SYSTEM_VALUE_VERTEX_ID
:
99 if (b
->shader
->options
->vertex_id_zero_based
) {
101 nir_load_system_value(b
, nir_intrinsic_load_vertex_id_zero_base
, 0),
102 nir_load_system_value(b
, nir_intrinsic_load_base_vertex
, 0));
104 sysval
= nir_load_system_value(b
, nir_intrinsic_load_vertex_id
, 0);
108 case SYSTEM_VALUE_INSTANCE_INDEX
:
110 nir_load_system_value(b
, nir_intrinsic_load_instance_id
, 0),
111 nir_load_system_value(b
, nir_intrinsic_load_base_instance
, 0));
115 nir_intrinsic_op sysval_op
=
116 nir_intrinsic_from_system_value(var
->data
.location
);
117 sysval
= nir_load_system_value(b
, sysval_op
, 0);
122 nir_ssa_def_rewrite_uses(&load_var
->dest
.ssa
, nir_src_for_ssa(sysval
));
123 nir_instr_remove(&load_var
->instr
);
132 convert_impl(nir_function_impl
*impl
)
134 bool progress
= false;
136 nir_builder_init(&builder
, impl
);
138 nir_foreach_block(block
, impl
) {
139 progress
|= convert_block(block
, &builder
);
142 nir_metadata_preserve(impl
, nir_metadata_block_index
|
143 nir_metadata_dominance
);
148 nir_lower_system_values(nir_shader
*shader
)
150 bool progress
= false;
152 nir_foreach_function(function
, shader
) {
154 progress
= convert_impl(function
->impl
) || progress
;
157 exec_list_make_empty(&shader
->system_values
);