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 return false;
80
81 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
82 nir_variable *var = nir_deref_instr_get_variable(deref);
83
84 /* In CL 1.2, images are required to be either read-only or
85 * write-only. We can always translate the read-only image ops to
86 * texture ops. In CL 2.0 (and an extension), the ability is added
87 * to have read-write images but sampling (with a sampler) is only
88 * allowed on read-only images. As long as we only lower read-only
89 * images to texture ops, everything should stay consistent.
90 */
91 enum gl_access_qualifier access = 0;
92 if (options->per_variable) {
93 if (var)
94 access = var->data.access;
95 } else {
96 access = nir_intrinsic_access(intrin);
97 }
98 if (!(access & ACCESS_NON_WRITEABLE))
99 return false;
100
101 unsigned num_srcs;
102 nir_texop texop;
103 switch (intrin->intrinsic) {
104 case nir_intrinsic_image_deref_load:
105 texop = nir_texop_txf;
106 num_srcs = 3;
107 break;
108 case nir_intrinsic_image_deref_size:
109 texop = nir_texop_txs;
110 num_srcs = 2;
111 break;
112 default:
113 unreachable("Unsupported intrinsic");
114 }
115
116 b->cursor = nir_before_instr(&intrin->instr);
117
118 nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);
119 tex->op = texop;
120
121 tex->sampler_dim = glsl_get_sampler_dim(deref->type);
122 tex->is_array = glsl_sampler_type_is_array(deref->type);
123 tex->is_shadow = false;
124
125 unsigned coord_components =
126 glsl_get_sampler_dim_coordinate_components(tex->sampler_dim);
127 if (glsl_sampler_type_is_array(deref->type))
128 coord_components++;
129
130 tex->src[0] = nir_tex_src_for_ssa(nir_tex_src_texture_deref,
131 &deref->def);
132
133 if (options->per_variable) {
134 assert(nir_deref_instr_get_variable(deref));
135 replace_image_type_with_texture(deref);
136 }
137
138 switch (intrin->intrinsic) {
139 case nir_intrinsic_image_deref_load: {
140 nir_def *coord =
141 nir_trim_vector(b, intrin->src[1].ssa, coord_components);
142 tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_coord, coord);
143 tex->coord_components = coord_components;
144
145 nir_def *lod = intrin->src[3].ssa;
146 tex->src[2] = nir_tex_src_for_ssa(nir_tex_src_lod, lod);
147
148 assert(num_srcs == 3);
149
150 tex->dest_type = nir_intrinsic_dest_type(intrin);
151 nir_def_init(&tex->instr, &tex->def, 4, 32);
152 break;
153 }
154
155 case nir_intrinsic_image_deref_size: {
156 nir_def *lod = intrin->src[1].ssa;
157 tex->src[1] = nir_tex_src_for_ssa(nir_tex_src_lod, lod);
158
159 assert(num_srcs == 2);
160
161 tex->dest_type = nir_type_uint32;
162 nir_def_init(&tex->instr, &tex->def, coord_components, 32);
163 break;
164 }
165
166 default:
167 unreachable("Unsupported intrinsic");
168 }
169
170 nir_builder_instr_insert(b, &tex->instr);
171
172 nir_def *res = nir_trim_vector(b, &tex->def,
173 intrin->def.num_components);
174
175 nir_def_rewrite_uses(&intrin->def, res);
176 nir_instr_remove(&intrin->instr);
177
178 return true;
179 }
180
181 static bool
lower_readonly_image_instr_tex(nir_builder * b,nir_tex_instr * tex,const struct readonly_image_lower_options * options)182 lower_readonly_image_instr_tex(nir_builder *b, nir_tex_instr *tex,
183 const struct readonly_image_lower_options *options)
184 {
185 int deref_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
186 if (deref_idx == -1)
187 return false;
188
189 nir_deref_instr *deref = nir_src_as_deref(tex->src[deref_idx].src);
190 if (options->per_variable) {
191 assert(nir_deref_instr_get_variable(deref));
192 return replace_image_type_with_texture(deref);
193 }
194
195 return false;
196 }
197
198 static bool
lower_readonly_image_instr(nir_builder * b,nir_instr * instr,void * context)199 lower_readonly_image_instr(nir_builder *b, nir_instr *instr, void *context)
200 {
201 struct readonly_image_lower_options *options = (struct readonly_image_lower_options *)context;
202
203 switch (instr->type) {
204 case nir_instr_type_intrinsic:
205 return lower_readonly_image_instr_intrinsic(b, nir_instr_as_intrinsic(instr), options);
206 case nir_instr_type_tex:
207 return lower_readonly_image_instr_tex(b, nir_instr_as_tex(instr), options);
208 default:
209 return false;
210 }
211 }
212
213 /** Lowers image ops to texture ops for read-only images
214 *
215 * If per_variable is set:
216 * - Variable access is used to indicate read-only instead of intrinsic access
217 * - Variable/deref types will be changed from image types to sampler types
218 *
219 * per_variable should not be set for OpenCL, because all image types will be
220 * void-returning, and there is no corresponding valid sampler type, and it
221 * will collide with the "bare" sampler type.
222 */
223 bool
nir_lower_readonly_images_to_tex(nir_shader * shader,bool per_variable)224 nir_lower_readonly_images_to_tex(nir_shader *shader, bool per_variable)
225 {
226 struct readonly_image_lower_options options = { per_variable };
227 return nir_shader_instructions_pass(shader, lower_readonly_image_instr,
228 nir_metadata_block_index |
229 nir_metadata_dominance,
230 &options);
231 }
232