1 /*
2 * Copyright © 2020 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 const struct glsl_type *
get_texture_type_for_image(const struct glsl_type * type)28 get_texture_type_for_image(const struct glsl_type *type)
29 {
30 if (glsl_type_is_array(type)) {
31 const struct glsl_type *elem_type =
32 get_texture_type_for_image(glsl_get_array_element(type));
33 return glsl_array_type(elem_type, glsl_get_length(type), 0 /*explicit size*/);
34 }
35
36 assert((glsl_type_is_image(type)));
37 return glsl_texture_type(glsl_get_sampler_dim(type),
38 glsl_sampler_type_is_array(type),
39 glsl_get_sampler_result_type(type));
40 }
41
42 static bool
replace_image_type_with_texture(nir_deref_instr * deref)43 replace_image_type_with_texture(nir_deref_instr *deref)
44 {
45 const struct glsl_type *type = deref->type;
46
47 /* If we've already chased up the deref chain this far from a different intrinsic, we're done */
48 if (!glsl_type_is_image(glsl_without_array(type)))
49 return false;
50
51 deref->type = get_texture_type_for_image(type);
52 deref->modes = nir_var_uniform;
53 if (deref->deref_type == nir_deref_type_var) {
54 type = deref->var->type;
55 if (glsl_type_is_image(glsl_without_array(type))) {
56 deref->var->type = get_texture_type_for_image(type);
57 deref->var->data.mode = nir_var_uniform;
58 memset(&deref->var->data.sampler, 0, sizeof(deref->var->data.sampler));
59 }
60 } else {
61 nir_deref_instr *parent = nir_deref_instr_parent(deref);
62 if (parent)
63 replace_image_type_with_texture(parent);
64 }
65
66 return true;
67 }
68
69 struct readonly_image_lower_options {
70 bool per_variable;
71 };
72
73 static bool
lower_readonly_image_instr_intrinsic(nir_builder * b,nir_intrinsic_instr * intrin,const struct readonly_image_lower_options * options)74 lower_readonly_image_instr_intrinsic(nir_builder *b, nir_intrinsic_instr *intrin,
75 const struct readonly_image_lower_options *options)
76 {
77 if (intrin->intrinsic != nir_intrinsic_image_deref_load &&
78 intrin->intrinsic != nir_intrinsic_image_deref_size &&
79 intrin->intrinsic != nir_intrinsic_image_deref_levels &&
80 intrin->intrinsic != nir_intrinsic_image_deref_samples)
81 return false;
82
83 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
84 nir_variable *var = nir_deref_instr_get_variable(deref);
85
86 /* In CL 1.2, images are required to be either read-only or
87 * write-only. We can always translate the read-only image ops to
88 * texture ops. In CL 2.0 (and an extension), the ability is added
89 * to have read-write images but sampling (with a sampler) is only
90 * allowed on read-only images. As long as we only lower read-only
91 * images to texture ops, everything should stay consistent.
92 */
93 enum gl_access_qualifier access = 0;
94 if (options->per_variable) {
95 if (var)
96 access = var->data.access;
97 } else {
98 access = nir_intrinsic_access(intrin);
99 }
100 if (!(access & ACCESS_NON_WRITEABLE))
101 return false;
102
103 unsigned num_srcs;
104 nir_texop texop;
105 switch (intrin->intrinsic) {
106 case nir_intrinsic_image_deref_load:
107 if (nir_intrinsic_image_dim(intrin) == GLSL_SAMPLER_DIM_MS) {
108 texop = nir_texop_txf_ms;
109 num_srcs = 4;
110 } else {
111 texop = nir_texop_txf;
112 num_srcs = 3;
113 }
114 break;
115 case nir_intrinsic_image_deref_size:
116 texop = nir_texop_txs;
117 num_srcs = 2;
118 break;
119 case nir_intrinsic_image_deref_levels:
120 texop = nir_texop_query_levels;
121 num_srcs = 1;
122 break;
123 case nir_intrinsic_image_deref_samples:
124 texop = nir_texop_texture_samples;
125 num_srcs = 1;
126 break;
127 default:
128 unreachable("Unsupported intrinsic");
129 }
130
131 b->cursor = nir_before_instr(&intrin->instr);
132
133 nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);
134 tex->op = texop;
135
136 tex->sampler_dim = glsl_get_sampler_dim(deref->type);
137 tex->is_array = glsl_sampler_type_is_array(deref->type);
138 tex->is_shadow = false;
139
140 unsigned coord_components =
141 glsl_get_sampler_dim_coordinate_components(tex->sampler_dim);
142 if (glsl_sampler_type_is_array(deref->type))
143 coord_components++;
144
145 tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
146 &deref->def);
147
148 if (options->per_variable) {
149 assert(nir_deref_instr_get_variable(deref));
150 replace_image_type_with_texture(deref);
151 }
152
153 switch (intrin->intrinsic) {
154 case nir_intrinsic_image_deref_load: {
155 nir_def *coord =
156 nir_trim_vector(b, intrin->src[1].ssa, coord_components);
157 tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_coord, coord);
158 tex->coord_components = coord_components;
159
160 nir_def *lod = intrin->src[3].ssa;
161 tex->src[2] = nir_tex_src_for_ssa(nir_tex_src_lod, lod);
162
163 if (texop == nir_texop_txf_ms) {
164 assert(num_srcs == 4);
165 nir_def *ms_index = intrin->src[2].ssa;
166 tex->src[3] = nir_tex_src_for_ssa(nir_tex_src_ms_index, ms_index);
167 } else {
168 assert(num_srcs == 3);
169 }
170
171 tex->dest_type = nir_intrinsic_dest_type(intrin);
172 nir_def_init(&tex->instr, &tex->def, 4, 32);
173 break;
174 }
175
176 case nir_intrinsic_image_deref_size: {
177 nir_def *lod = intrin->src[1].ssa;
178 tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_lod, lod);
179
180 assert(num_srcs == 2);
181
182 tex->dest_type = nir_type_uint32;
183 nir_def_init(&tex->instr, &tex->def, coord_components, 32);
184 break;
185 }
186
187 case nir_intrinsic_image_deref_levels:
188 case nir_intrinsic_image_deref_samples:
189 assert(num_srcs == 1);
190 tex->dest_type = nir_type_uint32;
191 nir_def_init(&tex->instr, &tex->def, 1, 32);
192 break;
193
194 default:
195 unreachable("Unsupported intrinsic");
196 }
197
198 nir_builder_instr_insert(b, &tex->instr);
199
200 nir_def *res = nir_trim_vector(b, &tex->def,
201 intrin->def.num_components);
202
203 nir_def_replace(&intrin->def, res);
204
205 return true;
206 }
207
208 static bool
lower_readonly_image_instr_tex(nir_builder * b,nir_tex_instr * tex,const struct readonly_image_lower_options * options)209 lower_readonly_image_instr_tex(nir_builder *b, nir_tex_instr *tex,
210 const struct readonly_image_lower_options *options)
211 {
212 int deref_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
213 if (deref_idx == -1)
214 return false;
215
216 nir_deref_instr *deref = nir_src_as_deref(tex->src[deref_idx].src);
217 if (options->per_variable) {
218 assert(nir_deref_instr_get_variable(deref));
219 return replace_image_type_with_texture(deref);
220 }
221
222 return false;
223 }
224
225 static bool
lower_readonly_image_instr(nir_builder * b,nir_instr * instr,void * context)226 lower_readonly_image_instr(nir_builder *b, nir_instr *instr, void *context)
227 {
228 struct readonly_image_lower_options *options = (struct readonly_image_lower_options *)context;
229
230 switch (instr->type) {
231 case nir_instr_type_intrinsic:
232 return lower_readonly_image_instr_intrinsic(b, nir_instr_as_intrinsic(instr), options);
233 case nir_instr_type_tex:
234 return lower_readonly_image_instr_tex(b, nir_instr_as_tex(instr), options);
235 default:
236 return false;
237 }
238 }
239
240 /** Lowers image ops to texture ops for read-only images
241 *
242 * If per_variable is set:
243 * - Variable access is used to indicate read-only instead of intrinsic access
244 * - Variable/deref types will be changed from image types to sampler types
245 *
246 * per_variable should not be set for OpenCL, because all image types will be
247 * void-returning, and there is no corresponding valid sampler type, and it
248 * will collide with the "bare" sampler type.
249 */
250 bool
nir_lower_readonly_images_to_tex(nir_shader * shader,bool per_variable)251 nir_lower_readonly_images_to_tex(nir_shader *shader, bool per_variable)
252 {
253 struct readonly_image_lower_options options = { per_variable };
254 return nir_shader_instructions_pass(shader, lower_readonly_image_instr,
255 nir_metadata_control_flow,
256 &options);
257 }
258