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