~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/compiler/nir/nir_lower_readonly_images_to_tex.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 *
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
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
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 *
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_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;
155
 
 
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);
160
 
 
161
 
      assert(num_srcs == 3);
162
 
 
163
 
      tex->dest_type = nir_intrinsic_dest_type(intrin);
164
 
      nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, NULL);
165
 
      break;
166
 
   }
167
 
 
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);
173
 
 
174
 
      assert(num_srcs == 2);
175
 
 
176
 
      tex->dest_type = nir_type_uint32;
177
 
      nir_ssa_dest_init(&tex->instr, &tex->dest,
178
 
                        coord_components, 32, NULL);
179
 
      break;
180
 
   }
181
 
 
182
 
   default:
183
 
      unreachable("Unsupported intrinsic");
184
 
   }
185
 
 
186
 
   nir_builder_instr_insert(b, &tex->instr);
187
 
 
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);
192
 
   }
193
 
 
194
 
   return res;
195
 
}
196
 
 
197
 
/** Lowers image ops to texture ops for read-only images
198
 
  * 
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
202
 
  * 
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.
205
 
  */
206
 
bool
207
 
nir_lower_readonly_images_to_tex(nir_shader *shader, bool per_variable)
208
 
{
209
 
   assert(shader->info.stage != MESA_SHADER_KERNEL || !per_variable);
210
 
 
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,
215
 
                                        &options);
216
 
}