nir/lower_input_attachments: Refactor to use an options struct
[mesa.git] / src / compiler / nir / nir_lower_input_attachments.c
1 /*
2 * Copyright © 2016 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
24 #include "nir.h"
25 #include "nir_builder.h"
26
27 static nir_ssa_def *
28 load_frag_coord(const nir_input_attachment_options *options, nir_builder *b)
29 {
30 if (options->use_fragcoord_sysval)
31 return nir_load_frag_coord(b);
32
33 nir_variable *pos =
34 nir_find_variable_with_location(b->shader, nir_var_shader_in,
35 VARYING_SLOT_POS);
36 if (pos == NULL) {
37 pos = nir_variable_create(b->shader, nir_var_shader_in,
38 glsl_vec4_type(), NULL);
39 pos->data.location = VARYING_SLOT_POS;
40 }
41 /**
42 * From Vulkan spec:
43 * "The OriginLowerLeft execution mode must not be used; fragment entry
44 * points must declare OriginUpperLeft."
45 *
46 * So at this point origin_upper_left should be true
47 */
48 assert(b->shader->info.fs.origin_upper_left == true);
49
50 return nir_load_var(b, pos);
51 }
52
53 static bool
54 try_lower_input_load(nir_function_impl *impl, nir_intrinsic_instr *load,
55 const nir_input_attachment_options *options)
56 {
57 nir_deref_instr *deref = nir_src_as_deref(load->src[0]);
58 assert(glsl_type_is_image(deref->type));
59
60 enum glsl_sampler_dim image_dim = glsl_get_sampler_dim(deref->type);
61 if (image_dim != GLSL_SAMPLER_DIM_SUBPASS &&
62 image_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
63 return false;
64
65 const bool multisampled = (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS);
66
67 nir_builder b;
68 nir_builder_init(&b, impl);
69 b.cursor = nir_instr_remove(&load->instr);
70
71 nir_ssa_def *frag_coord = load_frag_coord(options, &b);
72 frag_coord = nir_f2i32(&b, frag_coord);
73 nir_ssa_def *offset = nir_ssa_for_src(&b, load->src[1], 2);
74 nir_ssa_def *pos = nir_iadd(&b, frag_coord, offset);
75
76 nir_ssa_def *layer = nir_load_layer_id(&b);
77 nir_ssa_def *coord =
78 nir_vec3(&b, nir_channel(&b, pos, 0), nir_channel(&b, pos, 1), layer);
79
80 nir_tex_instr *tex = nir_tex_instr_create(b.shader, 3 + multisampled);
81
82 tex->op = nir_texop_txf;
83 tex->sampler_dim = image_dim;
84
85 switch (glsl_get_sampler_result_type(deref->type)) {
86 case GLSL_TYPE_FLOAT:
87 tex->dest_type = nir_type_float;
88 break;
89 case GLSL_TYPE_INT:
90 tex->dest_type = nir_type_int;
91 break;
92 case GLSL_TYPE_UINT:
93 tex->dest_type = nir_type_uint;
94 break;
95 default:
96 unreachable("Invalid image type");
97 }
98 tex->is_array = true;
99 tex->is_shadow = false;
100
101 tex->texture_index = 0;
102 tex->sampler_index = 0;
103
104 tex->src[0].src_type = nir_tex_src_texture_deref;
105 tex->src[0].src = nir_src_for_ssa(&deref->dest.ssa);
106
107 tex->src[1].src_type = nir_tex_src_coord;
108 tex->src[1].src = nir_src_for_ssa(coord);
109 tex->coord_components = 3;
110
111 tex->src[2].src_type = nir_tex_src_lod;
112 tex->src[2].src = nir_src_for_ssa(nir_imm_int(&b, 0));
113
114 if (image_dim == GLSL_SAMPLER_DIM_SUBPASS_MS) {
115 tex->op = nir_texop_txf_ms;
116 tex->src[3].src_type = nir_tex_src_ms_index;
117 tex->src[3].src = load->src[2];
118 }
119
120 tex->texture_non_uniform = nir_intrinsic_access(load) & ACCESS_NON_UNIFORM;
121
122 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
123 nir_builder_instr_insert(&b, &tex->instr);
124
125 nir_ssa_def_rewrite_uses(&load->dest.ssa,
126 nir_src_for_ssa(&tex->dest.ssa));
127
128 return true;
129 }
130
131 static bool
132 try_lower_input_texop(nir_function_impl *impl, nir_tex_instr *tex,
133 const nir_input_attachment_options *options)
134 {
135 nir_deref_instr *deref = nir_src_as_deref(tex->src[0].src);
136
137 if (glsl_get_sampler_dim(deref->type) != GLSL_SAMPLER_DIM_SUBPASS_MS)
138 return false;
139
140 nir_builder b;
141 nir_builder_init(&b, impl);
142 b.cursor = nir_before_instr(&tex->instr);
143
144 nir_ssa_def *frag_coord = load_frag_coord(options, &b);
145 frag_coord = nir_f2i32(&b, frag_coord);
146
147 nir_ssa_def *layer = nir_load_layer_id(&b);
148 nir_ssa_def *coord = nir_vec3(&b, nir_channel(&b, frag_coord, 0),
149 nir_channel(&b, frag_coord, 1), layer);
150
151 tex->coord_components = 3;
152
153 nir_instr_rewrite_src(&tex->instr, &tex->src[1].src, nir_src_for_ssa(coord));
154
155 return true;
156 }
157
158 bool
159 nir_lower_input_attachments(nir_shader *shader,
160 const nir_input_attachment_options *options)
161 {
162 assert(shader->info.stage == MESA_SHADER_FRAGMENT);
163 bool progress = false;
164
165 nir_foreach_function(function, shader) {
166 if (!function->impl)
167 continue;
168
169 nir_foreach_block(block, function->impl) {
170 nir_foreach_instr_safe(instr, block) {
171 switch (instr->type) {
172 case nir_instr_type_tex: {
173 nir_tex_instr *tex = nir_instr_as_tex(instr);
174
175 if (tex->op == nir_texop_fragment_mask_fetch ||
176 tex->op == nir_texop_fragment_fetch) {
177 progress |= try_lower_input_texop(function->impl, tex,
178 options);
179 }
180 break;
181 }
182 case nir_instr_type_intrinsic: {
183 nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);
184
185 if (load->intrinsic == nir_intrinsic_image_deref_load) {
186 progress |= try_lower_input_load(function->impl, load,
187 options);
188 }
189 break;
190 }
191 default:
192 break;
193 }
194 }
195 }
196 }
197
198 return progress;
199 }