amd/common: scan if gl_InvocationID is used
[mesa.git] / src / amd / common / ac_shader_info.c
1 /*
2 * Copyright © 2017 Red Hat
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 #include "nir/nir.h"
24 #include "ac_shader_info.h"
25 #include "ac_nir_to_llvm.h"
26
27 static void mark_sampler_desc(nir_variable *var, struct ac_shader_info *info)
28 {
29 info->desc_set_used_mask = (1 << var->data.descriptor_set);
30 }
31
32 static void
33 gather_intrinsic_info(nir_intrinsic_instr *instr, struct ac_shader_info *info)
34 {
35 switch (instr->intrinsic) {
36 case nir_intrinsic_interp_var_at_sample:
37 info->ps.needs_sample_positions = true;
38 break;
39 case nir_intrinsic_load_draw_id:
40 info->vs.needs_draw_id = true;
41 break;
42 case nir_intrinsic_load_instance_id:
43 info->vs.needs_instance_id = true;
44 break;
45 case nir_intrinsic_load_num_work_groups:
46 info->cs.uses_grid_size = true;
47 break;
48 case nir_intrinsic_load_local_invocation_id:
49 case nir_intrinsic_load_work_group_id: {
50 unsigned mask = nir_ssa_def_components_read(&instr->dest.ssa);
51 while (mask) {
52 unsigned i = u_bit_scan(&mask);
53
54 if (instr->intrinsic == nir_intrinsic_load_work_group_id)
55 info->cs.uses_block_id[i] = true;
56 else
57 info->cs.uses_thread_id[i] = true;
58 }
59 break;
60 }
61 case nir_intrinsic_load_local_invocation_index:
62 info->cs.uses_local_invocation_idx = true;
63 break;
64 case nir_intrinsic_load_sample_id:
65 info->ps.force_persample = true;
66 break;
67 case nir_intrinsic_load_sample_pos:
68 info->ps.force_persample = true;
69 break;
70 case nir_intrinsic_load_view_index:
71 info->needs_multiview_view_index = true;
72 break;
73 case nir_intrinsic_load_invocation_id:
74 info->uses_invocation_id = true;
75 break;
76 case nir_intrinsic_vulkan_resource_index:
77 info->desc_set_used_mask |= (1 << nir_intrinsic_desc_set(instr));
78 break;
79 case nir_intrinsic_image_load:
80 case nir_intrinsic_image_store:
81 case nir_intrinsic_image_atomic_add:
82 case nir_intrinsic_image_atomic_min:
83 case nir_intrinsic_image_atomic_max:
84 case nir_intrinsic_image_atomic_and:
85 case nir_intrinsic_image_atomic_or:
86 case nir_intrinsic_image_atomic_xor:
87 case nir_intrinsic_image_atomic_exchange:
88 case nir_intrinsic_image_atomic_comp_swap:
89 case nir_intrinsic_image_size: {
90 const struct glsl_type *type = instr->variables[0]->var->type;
91 if(instr->variables[0]->deref.child)
92 type = instr->variables[0]->deref.child->type;
93
94 enum glsl_sampler_dim dim = glsl_get_sampler_dim(type);
95 if (dim == GLSL_SAMPLER_DIM_SUBPASS ||
96 dim == GLSL_SAMPLER_DIM_SUBPASS_MS)
97 info->ps.uses_input_attachments = true;
98 mark_sampler_desc(instr->variables[0]->var, info);
99 break;
100 }
101 default:
102 break;
103 }
104 }
105
106 static void
107 gather_tex_info(nir_tex_instr *instr, struct ac_shader_info *info)
108 {
109 if (instr->sampler)
110 mark_sampler_desc(instr->sampler->var, info);
111 if (instr->texture)
112 mark_sampler_desc(instr->texture->var, info);
113 }
114
115 static void
116 gather_info_block(nir_block *block, struct ac_shader_info *info)
117 {
118 nir_foreach_instr(instr, block) {
119 switch (instr->type) {
120 case nir_instr_type_intrinsic:
121 gather_intrinsic_info(nir_instr_as_intrinsic(instr), info);
122 break;
123 case nir_instr_type_tex:
124 gather_tex_info(nir_instr_as_tex(instr), info);
125 break;
126 default:
127 break;
128 }
129 }
130 }
131
132 static void
133 gather_info_input_decl(nir_shader *nir,
134 const struct ac_nir_compiler_options *options,
135 nir_variable *var,
136 struct ac_shader_info *info)
137 {
138 switch (nir->info.stage) {
139 case MESA_SHADER_VERTEX:
140 info->vs.has_vertex_buffers = true;
141 break;
142 default:
143 break;
144 }
145 }
146
147 void
148 ac_nir_shader_info_pass(struct nir_shader *nir,
149 const struct ac_nir_compiler_options *options,
150 struct ac_shader_info *info)
151 {
152 struct nir_function *func = (struct nir_function *)exec_list_get_head(&nir->functions);
153
154 info->needs_push_constants = false;
155 if ((options->layout->push_constant_size &&
156 options->layout->push_constant_stages & (1 << nir->info.stage)) ||
157 options->layout->dynamic_offset_count)
158 info->needs_push_constants = true;
159
160 nir_foreach_variable(variable, &nir->inputs)
161 gather_info_input_decl(nir, options, variable, info);
162
163 nir_foreach_block(block, func->impl) {
164 gather_info_block(block, info);
165 }
166 }