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