~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/mesa/state_tracker/st_nir_lower_tex_src_plane.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 © 2016 Red Hat
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 FROM,
20
 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
 
 * SOFTWARE.
22
 
 */
23
 
 
24
 
/* Lowers the additional tex_src_plane src, generated by nir_lower_tex
25
 
 * for planar YUV textures, into separate samplers, matching the logic
26
 
 * that mesa/st uses to insert additional sampler view/state (since both
27
 
 * sides need to agree).
28
 
 *
29
 
 * This should run after nir_lower_samplers.
30
 
 */
31
 
 
32
 
#include "util/u_string.h"
33
 
#include "compiler/nir/nir.h"
34
 
#include "compiler/nir/nir_builder.h"
35
 
#include "st_nir.h"
36
 
 
37
 
typedef struct {
38
 
   struct nir_shader *shader;
39
 
 
40
 
   unsigned lower_2plane;
41
 
   unsigned lower_3plane;
42
 
 
43
 
   /* Maps a primary sampler (used for Y) to the U or UV sampler.  In
44
 
    * case of 3-plane YUV format, the V plane is next sampler after U.
45
 
    */
46
 
   unsigned char sampler_map[PIPE_MAX_SAMPLERS][2];
47
 
} lower_tex_src_state;
48
 
 
49
 
static nir_variable *
50
 
find_sampler(lower_tex_src_state *state, unsigned samp)
51
 
{
52
 
   /* NOTE: arrays of samplerExternalOES do not appear to be allowed: */
53
 
   nir_foreach_uniform_variable(var, state->shader)
54
 
      if (var->data.binding == samp)
55
 
         return var;
56
 
   return NULL;
57
 
}
58
 
 
59
 
static void
60
 
add_sampler(lower_tex_src_state *state, unsigned orig_binding,
61
 
            unsigned new_binding, const char *ext)
62
 
{
63
 
   const struct glsl_type *samplerExternalOES =
64
 
      glsl_sampler_type(GLSL_SAMPLER_DIM_EXTERNAL, false, false, GLSL_TYPE_FLOAT);
65
 
   nir_variable *new_sampler, *orig_sampler =
66
 
         find_sampler(state, orig_binding);
67
 
   char *name;
68
 
 
69
 
   UNUSED int r = asprintf(&name, "%s:%s", orig_sampler->name, ext);
70
 
   new_sampler = nir_variable_create(state->shader, nir_var_uniform,
71
 
                             samplerExternalOES, name);
72
 
   free(name);
73
 
 
74
 
   new_sampler->data.binding = new_binding;
75
 
}
76
 
 
77
 
static void
78
 
assign_extra_samplers(lower_tex_src_state *state, unsigned free_slots)
79
 
{
80
 
   unsigned mask = state->lower_2plane | state->lower_3plane;
81
 
 
82
 
   while (mask) {
83
 
      unsigned extra, y_samp = u_bit_scan(&mask);
84
 
 
85
 
      if (state->lower_3plane & (1 << y_samp)) {
86
 
         /* two additional planes (U and V): */
87
 
         extra = u_bit_scan(&free_slots);
88
 
         state->sampler_map[y_samp][0] = extra;
89
 
 
90
 
         add_sampler(state, y_samp, extra, "u");
91
 
 
92
 
         extra = u_bit_scan(&free_slots);
93
 
         state->sampler_map[y_samp][1] = extra;
94
 
 
95
 
         add_sampler(state, y_samp, extra, "v");
96
 
      } else {
97
 
         /* single additional UV plane: */
98
 
         extra = u_bit_scan(&free_slots);
99
 
         state->sampler_map[y_samp][0] = extra;
100
 
 
101
 
         add_sampler(state, y_samp, extra, "uv");
102
 
      }
103
 
   }
104
 
}
105
 
 
106
 
static void
107
 
lower_tex_src_plane_block(nir_builder *b, lower_tex_src_state *state, nir_block *block)
108
 
{
109
 
   nir_foreach_instr(instr, block) {
110
 
      if (instr->type != nir_instr_type_tex)
111
 
         continue;
112
 
 
113
 
      nir_tex_instr *tex = nir_instr_as_tex(instr);
114
 
      int plane_index = nir_tex_instr_src_index(tex, nir_tex_src_plane);
115
 
 
116
 
      if (plane_index < 0)
117
 
         continue;
118
 
 
119
 
      nir_const_value *plane = nir_src_as_const_value(tex->src[plane_index].src);
120
 
      assume(plane);
121
 
 
122
 
      if (plane[0].i32 > 0) {
123
 
         unsigned y_samp = tex->texture_index;
124
 
         int tex_index = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);
125
 
         if (tex_index >= 0) {
126
 
            nir_deref_instr *deref = nir_src_as_deref(tex->src[tex_index].src);
127
 
            y_samp = nir_deref_instr_get_variable(deref)->data.binding;
128
 
         }
129
 
 
130
 
         assume(((state->lower_3plane & (1 << y_samp)) && plane[0].i32 < 3) ||
131
 
               (plane[0].i32 < 2));
132
 
 
133
 
         unsigned u_v_samp = state->sampler_map[y_samp][plane[0].i32 - 1];
134
 
         BITSET_SET(state->shader->info.textures_used, u_v_samp);
135
 
 
136
 
         /* For drivers using PIPE_CAP_NIR_SAMPLERS_AS_DEREF, we need
137
 
          * to reference the correct sampler nir variable.
138
 
          */
139
 
         int samp_index = nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref);
140
 
         if (tex_index >= 0 && samp_index >= 0) {
141
 
            b->cursor = nir_before_instr(&tex->instr);
142
 
 
143
 
            nir_variable* samp = find_sampler(state, u_v_samp);
144
 
            assert(samp);
145
 
 
146
 
            nir_deref_instr *tex_deref_instr = nir_build_deref_var(b, samp);
147
 
            nir_ssa_def *tex_deref = &tex_deref_instr->dest.ssa;
148
 
 
149
 
            nir_instr_rewrite_src(&tex->instr,
150
 
                                  &tex->src[tex_index].src,
151
 
                                  nir_src_for_ssa(tex_deref));
152
 
            nir_instr_rewrite_src(&tex->instr,
153
 
                                  &tex->src[samp_index].src,
154
 
                                  nir_src_for_ssa(tex_deref));
155
 
         } else {
156
 
            /* For others we need to update texture_index */
157
 
            assume(tex->texture_index == tex->sampler_index);
158
 
            tex->texture_index = tex->sampler_index = u_v_samp;
159
 
         }
160
 
      }
161
 
 
162
 
      nir_tex_instr_remove_src(tex, plane_index);
163
 
   }
164
 
}
165
 
 
166
 
static void
167
 
lower_tex_src_plane_impl(lower_tex_src_state *state, nir_function_impl *impl)
168
 
{
169
 
   nir_builder b;
170
 
   nir_builder_init(&b, impl);
171
 
 
172
 
   nir_foreach_block(block, impl) {
173
 
      lower_tex_src_plane_block(&b, state, block);
174
 
   }
175
 
 
176
 
   nir_metadata_preserve(impl, nir_metadata_block_index |
177
 
                               nir_metadata_dominance);
178
 
}
179
 
 
180
 
void
181
 
st_nir_lower_tex_src_plane(struct nir_shader *shader, unsigned free_slots,
182
 
                           unsigned lower_2plane, unsigned lower_3plane)
183
 
{
184
 
   lower_tex_src_state state = {0};
185
 
 
186
 
   state.shader = shader;
187
 
   state.lower_2plane = lower_2plane;
188
 
   state.lower_3plane = lower_3plane;
189
 
 
190
 
   assign_extra_samplers(&state, free_slots);
191
 
 
192
 
   nir_foreach_function(function, shader) {
193
 
      if (function->impl)
194
 
         lower_tex_src_plane_impl(&state, function->impl);
195
 
   }
196
 
}