13d73df11f9d91a80c9cb73e29261985c2072d90
[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_num_work_groups:
43 info->cs.grid_components_used = instr->num_components;
44 break;
45 case nir_intrinsic_vulkan_resource_index:
46 info->desc_set_used_mask |= (1 << nir_intrinsic_desc_set(instr));
47 break;
48 case nir_intrinsic_image_load:
49 case nir_intrinsic_image_store:
50 case nir_intrinsic_image_atomic_add:
51 case nir_intrinsic_image_atomic_min:
52 case nir_intrinsic_image_atomic_max:
53 case nir_intrinsic_image_atomic_and:
54 case nir_intrinsic_image_atomic_or:
55 case nir_intrinsic_image_atomic_xor:
56 case nir_intrinsic_image_atomic_exchange:
57 case nir_intrinsic_image_atomic_comp_swap:
58 case nir_intrinsic_image_size:
59 mark_sampler_desc(instr->variables[0]->var, info);
60 break;
61 default:
62 break;
63 }
64 }
65
66 static void
67 gather_tex_info(nir_tex_instr *instr, struct ac_shader_info *info)
68 {
69 if (instr->sampler)
70 mark_sampler_desc(instr->sampler->var, info);
71 if (instr->texture)
72 mark_sampler_desc(instr->texture->var, info);
73 }
74
75 static void
76 gather_info_block(nir_block *block, struct ac_shader_info *info)
77 {
78 nir_foreach_instr(instr, block) {
79 switch (instr->type) {
80 case nir_instr_type_intrinsic:
81 gather_intrinsic_info(nir_instr_as_intrinsic(instr), info);
82 break;
83 case nir_instr_type_tex:
84 gather_tex_info(nir_instr_as_tex(instr), info);
85 break;
86 default:
87 break;
88 }
89 }
90 }
91
92 static void
93 gather_info_input_decl(nir_shader *nir,
94 const struct ac_nir_compiler_options *options,
95 nir_variable *var,
96 struct ac_shader_info *info)
97 {
98 switch (nir->stage) {
99 case MESA_SHADER_VERTEX:
100 info->vs.has_vertex_buffers = true;
101 break;
102 default:
103 break;
104 }
105 }
106
107 void
108 ac_nir_shader_info_pass(struct nir_shader *nir,
109 const struct ac_nir_compiler_options *options,
110 struct ac_shader_info *info)
111 {
112 struct nir_function *func = (struct nir_function *)exec_list_get_head(&nir->functions);
113
114 info->needs_push_constants = true;
115 if (!options->layout)
116 info->needs_push_constants = false;
117 else if (!options->layout->push_constant_size &&
118 !options->layout->dynamic_offset_count)
119 info->needs_push_constants = false;
120
121 nir_foreach_variable(variable, &nir->inputs)
122 gather_info_input_decl(nir, options, variable, info);
123
124 nir_foreach_block(block, func->impl) {
125 gather_info_block(block, info);
126 }
127 }