2
* Copyright © 2020 Intel Corporation
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:
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
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
25
#include "nir_builder.h"
27
static const struct glsl_type *
28
get_texture_type_for_image(const struct glsl_type *type)
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*/);
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));
43
replace_image_type_with_sampler(nir_deref_instr *deref)
45
const struct glsl_type *type = deref->type;
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)))
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));
61
nir_deref_instr *parent = nir_deref_instr_parent(deref);
63
replace_image_type_with_sampler(parent);
67
struct readonly_image_lower_options {
72
is_readonly_image_op(const nir_instr *instr, const void *context)
74
struct readonly_image_lower_options *options = (struct readonly_image_lower_options *)context;
75
if (instr->type != nir_instr_type_intrinsic)
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)
82
nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
83
nir_variable *var = nir_deref_instr_get_variable(deref);
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.
92
enum gl_access_qualifier access = 0;
93
if (options->per_variable) {
95
access = var->data.access;
97
access = nir_intrinsic_access(intrin);
98
if (access & ACCESS_NON_WRITEABLE)
105
lower_readonly_image_op(nir_builder *b, nir_instr *instr, void *context)
107
struct readonly_image_lower_options *options = (struct readonly_image_lower_options *)context;
108
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
111
switch (intrin->intrinsic) {
112
case nir_intrinsic_image_deref_load:
113
texop = nir_texop_txf;
116
case nir_intrinsic_image_deref_size:
117
texop = nir_texop_txs;
121
unreachable("Filtered above");
124
nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
126
nir_tex_instr *tex = nir_tex_instr_create(b->shader, num_srcs);
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;
133
unsigned coord_components =
134
glsl_get_sampler_dim_coordinate_components(tex->sampler_dim);
135
if (glsl_sampler_type_is_array(deref->type))
138
tex->src[0].src_type = nir_tex_src_texture_deref;
139
tex->src[0].src = nir_src_for_ssa(&deref->dest.ssa);
141
if (options->per_variable) {
142
assert(nir_deref_instr_get_variable(deref));
143
replace_image_type_with_sampler(deref);
146
switch (intrin->intrinsic) {
147
case nir_intrinsic_image_deref_load: {
148
assert(intrin->src[1].is_ssa);
150
nir_channels(b, intrin->src[1].ssa,
151
(1 << coord_components) - 1);
152
tex->src[1].src_type = nir_tex_src_coord;
153
tex->src[1].src = nir_src_for_ssa(coord);
154
tex->coord_components = coord_components;
156
assert(intrin->src[3].is_ssa);
157
nir_ssa_def *lod = intrin->src[3].ssa;
158
tex->src[2].src_type = nir_tex_src_lod;
159
tex->src[2].src = nir_src_for_ssa(lod);
161
assert(num_srcs == 3);
163
tex->dest_type = nir_intrinsic_dest_type(intrin);
164
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
168
case nir_intrinsic_image_deref_size: {
169
assert(intrin->src[1].is_ssa);
170
nir_ssa_def *lod = intrin->src[1].ssa;
171
tex->src[1].src_type = nir_tex_src_lod;
172
tex->src[1].src = nir_src_for_ssa(lod);
174
assert(num_srcs == 2);
176
tex->dest_type = nir_type_uint32;
177
nir_ssa_dest_init(&tex->instr, &tex->dest,
178
coord_components, 32, NULL);
183
unreachable("Unsupported intrinsic");
186
nir_builder_instr_insert(b, &tex->instr);
188
nir_ssa_def *res = &tex->dest.ssa;
189
if (res->num_components != intrin->dest.ssa.num_components) {
190
unsigned num_components = intrin->dest.ssa.num_components;
191
res = nir_channels(b, res, (1 << num_components) - 1);
197
/** Lowers image ops to texture ops for read-only images
199
* If per_variable is set:
200
* - Variable access is used to indicate read-only instead of intrinsic access
201
* - Variable/deref types will be changed from image types to sampler types
203
* per_variable should not be set for OpenCL, because all image types will be void-returning,
204
* and there is no corresponding valid sampler type, and it will collide with the "bare" sampler type.
207
nir_lower_readonly_images_to_tex(nir_shader *shader, bool per_variable)
209
assert(shader->info.stage != MESA_SHADER_KERNEL || !per_variable);
211
struct readonly_image_lower_options options = { per_variable };
212
return nir_shader_lower_instructions(shader,
213
is_readonly_image_op,
214
lower_readonly_image_op,