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 bool
lower_cl_images_to_tex_impl(nir_function_impl * impl)28 lower_cl_images_to_tex_impl(nir_function_impl *impl)
29 {
30 bool progress = false;
31
32 nir_builder b;
33 nir_builder_init(&b, impl);
34
35 nir_foreach_block(block, impl) {
36 nir_foreach_instr_safe(instr, block) {
37 if (instr->type != nir_instr_type_intrinsic)
38 continue;
39
40 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
41
42 unsigned num_srcs;
43 nir_texop texop;
44 switch (intrin->intrinsic) {
45 case nir_intrinsic_image_deref_load:
46 texop = nir_texop_txf;
47 num_srcs = 3;
48 break;
49 case nir_intrinsic_image_deref_size:
50 texop = nir_texop_txs;
51 num_srcs = 2;
52 break;
53 default:
54 /* Not an op we can lower */
55 continue;
56 }
57
58 /* In CL 1.2, images are required to be either read-only or
59 * write-only. We can always translate the read-only image ops to
60 * texture ops. In CL 2.0 (and an extension), the ability is added
61 * to have read-write images but sampling (with a sampler) is only
62 * allowed on read-only images. As long as we only lower read-only
63 * images to texture ops, everything should stay consistent.
64 */
65 if (!(nir_intrinsic_access(intrin) & ACCESS_NON_WRITEABLE))
66 continue;
67
68 nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
69
70 b.cursor = nir_instr_remove(&intrin->instr);
71
72 nir_tex_instr *tex = nir_tex_instr_create(b.shader, num_srcs);
73 tex->op = texop;
74
75 tex->sampler_dim = glsl_get_sampler_dim(deref->type);
76 tex->is_array = glsl_sampler_type_is_array(deref->type);
77 tex->is_shadow = false;
78
79 unsigned coord_components =
80 glsl_get_sampler_dim_coordinate_components(tex->sampler_dim);
81 if (glsl_sampler_type_is_array(deref->type))
82 tex->coord_components++;
83
84 tex->src[0].src_type = nir_tex_src_texture_deref;
85 tex->src[0].src = nir_src_for_ssa(&deref->dest.ssa);
86
87 switch (intrin->intrinsic) {
88 case nir_intrinsic_image_deref_load: {
89 assert(intrin->src[1].is_ssa);
90 tex->coord_components = coord_components;
91 nir_ssa_def *coord =
92 nir_channels(&b, intrin->src[1].ssa,
93 (1 << tex->coord_components) - 1);
94 tex->src[1].src_type = nir_tex_src_coord;
95 tex->src[1].src = nir_src_for_ssa(coord);
96
97 assert(intrin->src[3].is_ssa);
98 nir_ssa_def *lod = intrin->src[3].ssa;
99 tex->src[2].src_type = nir_tex_src_lod;
100 tex->src[2].src = nir_src_for_ssa(lod);
101
102 assert(num_srcs == 3);
103
104 tex->dest_type = nir_intrinsic_dest_type(intrin);
105 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
106 break;
107 }
108
109 case nir_intrinsic_image_deref_size: {
110 assert(intrin->src[1].is_ssa);
111 nir_ssa_def *lod = intrin->src[1].ssa;
112 tex->src[1].src_type = nir_tex_src_lod;
113 tex->src[1].src = nir_src_for_ssa(lod);
114
115 assert(num_srcs == 2);
116
117 tex->dest_type = nir_type_uint32;
118 nir_ssa_dest_init(&tex->instr, &tex->dest,
119 coord_components, 32, NULL);
120 break;
121 }
122
123 default:
124 unreachable("Unsupported intrinsic");
125 }
126
127 nir_builder_instr_insert(&b, &tex->instr);
128
129 nir_ssa_def *res = &tex->dest.ssa;
130 if (res->num_components != intrin->dest.ssa.num_components) {
131 unsigned num_components = intrin->dest.ssa.num_components;
132 res = nir_channels(&b, res, (1 << num_components) - 1);
133 }
134
135 nir_ssa_def_rewrite_uses(&intrin->dest.ssa, nir_src_for_ssa(res));
136 progress = true;
137 }
138 }
139
140 if (progress) {
141 nir_metadata_preserve(impl, nir_metadata_block_index |
142 nir_metadata_dominance);
143 } else {
144 nir_metadata_preserve(impl, nir_metadata_all);
145 }
146
147 return progress;
148 }
149
150 /** Lowers OpenCL image ops to texture ops for read-only images */
151 bool
nir_lower_cl_images_to_tex(nir_shader * shader)152 nir_lower_cl_images_to_tex(nir_shader *shader)
153 {
154 bool progress = false;
155
156 nir_foreach_function(function, shader) {
157 if (function->impl && lower_cl_images_to_tex_impl(function->impl))
158 progress = true;
159 }
160
161 return progress;
162 }
163