aboutsummaryrefslogtreecommitdiffstats
path: root/src/compiler/glsl/gl_nir_link_uniform_initializers.c
blob: 36441b4bbe7858fe26a89cb7e05987fae3b9baf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright © 2018 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "nir.h"
#include "gl_nir_linker.h"
#include "compiler/glsl/ir_uniform.h" /* for gl_uniform_storage */
#include "main/context.h"
#include "main/mtypes.h"

struct set_opaque_binding_closure {
   struct gl_shader_program *shader_prog;
   struct gl_program *prog;
   const nir_variable *var;
   int binding;
   int location;
};

static void
set_opaque_binding(struct set_opaque_binding_closure *data,
                   const struct glsl_type *type)
{
   if (glsl_type_is_array(type) &&
       glsl_type_is_array(glsl_get_array_element(type))) {
      const struct glsl_type *element_type = glsl_get_array_element(type);

      for (unsigned int i = 0; i < glsl_get_length(type); i++)
         set_opaque_binding(data, element_type);

      return;
   }

   if (data->location < 0 ||
       data->location >= data->prog->sh.data->NumUniformStorage)
      return;

   struct gl_uniform_storage *storage =
      data->prog->sh.data->UniformStorage + data->location++;

   const unsigned elements = MAX2(storage->array_elements, 1);

   for (unsigned int i = 0; i < elements; i++)
      storage->storage[i].i = data->binding++;

   for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
      struct gl_linked_shader *shader = data->shader_prog->_LinkedShaders[sh];

      if (!shader)
         continue;
      if (!storage->opaque[sh].active)
         continue;

      if (glsl_type_is_sampler(storage->type)) {
         for (unsigned i = 0; i < elements; i++) {
            const unsigned index = storage->opaque[sh].index + i;

            if (storage->is_bindless) {
               if (index >= shader->Program->sh.NumBindlessSamplers)
                  break;
               shader->Program->sh.BindlessSamplers[index].unit =
                  storage->storage[i].i;
               shader->Program->sh.BindlessSamplers[index].bound = true;
               shader->Program->sh.HasBoundBindlessSampler = true;
            } else {
               if (index >= ARRAY_SIZE(shader->Program->SamplerUnits))
                  break;
               shader->Program->SamplerUnits[index] =
                  storage->storage[i].i;
            }
         }
      } else if (glsl_type_is_image(type)) {
         for (unsigned i = 0; i < elements; i++) {
            const unsigned index = storage->opaque[sh].index + i;

            if (storage->is_bindless) {
               if (index >= shader->Program->sh.NumBindlessImages)
                  break;
               shader->Program->sh.BindlessImages[index].unit =
                  storage->storage[i].i;
               shader->Program->sh.BindlessImages[index].bound = true;
               shader->Program->sh.HasBoundBindlessImage = true;
            } else {
               if (index >= ARRAY_SIZE(shader->Program->sh.ImageUnits))
                  break;
               shader->Program->sh.ImageUnits[index] =
                  storage->storage[i].i;
            }
         }
      }
   }
}

static void
copy_constant_to_storage(union gl_constant_value *storage,
                         const nir_constant *val,
                         const struct glsl_type *type,
                         unsigned int boolean_true)
{
   const enum glsl_base_type base_type = glsl_get_base_type(type);
   const unsigned n_columns = glsl_get_matrix_columns(type);
   const unsigned n_rows = glsl_get_vector_elements(type);
   int i = 0;

   for (unsigned int column = 0; column < n_columns; column++) {
      for (unsigned int row = 0; row < n_rows; row++) {
         switch (base_type) {
         case GLSL_TYPE_UINT:
            storage[i].u = val->values[column][row].u32;
            break;
         case GLSL_TYPE_INT:
         case GLSL_TYPE_SAMPLER:
            storage[i].i = val->values[column][row].i32;
            break;
         case GLSL_TYPE_FLOAT:
            storage[i].f = val->values[column][row].f32;
            break;
         case GLSL_TYPE_DOUBLE:
         case GLSL_TYPE_UINT64:
         case GLSL_TYPE_INT64:
            /* XXX need to check on big-endian */
            memcpy(&storage[i * 2].u,
                   &val->values[column][row].f64,
                   sizeof(double));
            break;
         case GLSL_TYPE_BOOL:
            storage[i].b = val->values[column][row].u32 ? boolean_true : 0;
            break;
         case GLSL_TYPE_ARRAY:
         case GLSL_TYPE_STRUCT:
         case GLSL_TYPE_IMAGE:
         case GLSL_TYPE_ATOMIC_UINT:
         case GLSL_TYPE_INTERFACE:
         case GLSL_TYPE_VOID:
         case GLSL_TYPE_SUBROUTINE:
         case GLSL_TYPE_FUNCTION:
         case GLSL_TYPE_ERROR:
         case GLSL_TYPE_UINT16:
         case GLSL_TYPE_INT16:
         case GLSL_TYPE_UINT8:
         case GLSL_TYPE_INT8:
         case GLSL_TYPE_FLOAT16:
            /* All other types should have already been filtered by other
             * paths in the caller.
             */
            assert(!"Should not get here.");
            break;
         }
         i++;
      }
   }
}

struct set_uniform_initializer_closure {
   struct gl_shader_program *shader_prog;
   struct gl_program *prog;
   const nir_variable *var;
   int location;
   unsigned int boolean_true;
};

static void
set_uniform_initializer(struct set_uniform_initializer_closure *data,
                        const struct glsl_type *type,
                        const nir_constant *val)
{
   const struct glsl_type *t_without_array = glsl_without_array(type);

   if (glsl_type_is_struct_or_ifc(type)) {
      for (unsigned int i = 0; i < glsl_get_length(type); i++) {
         const struct glsl_type *field_type = glsl_get_struct_field(type, i);
         set_uniform_initializer(data, field_type, val->elements[i]);
      }
      return;
   }

   if (glsl_type_is_struct_or_ifc(t_without_array) ||
       (glsl_type_is_array(type) &&
        glsl_type_is_array(glsl_get_array_element(type)))) {
      const struct glsl_type *element_type = glsl_get_array_element(type);

      for (unsigned int i = 0; i < glsl_get_length(type); i++)
         set_uniform_initializer(data, element_type, val->elements[i]);

      return;
   }

   if (data->location < 0 ||
       data->location >= data->prog->sh.data->NumUniformStorage)
      return;

   struct gl_uniform_storage *storage =
      data->prog->sh.data->UniformStorage + data->location++;

   if (glsl_type_is_array(type)) {
      const struct glsl_type *element_type = glsl_get_array_element(type);
      const enum glsl_base_type base_type = glsl_get_base_type(element_type);
      const unsigned int elements = glsl_get_components(element_type);
      unsigned int idx = 0;
      unsigned dmul = glsl_base_type_is_64bit(base_type) ? 2 : 1;

      assert(glsl_get_length(type) >= storage->array_elements);
      for (unsigned int i = 0; i < storage->array_elements; i++) {
         copy_constant_to_storage(&storage->storage[idx],
                                  val->elements[i],
                                  element_type,
                                  data->boolean_true);

         idx += elements * dmul;
      }
   } else {
      copy_constant_to_storage(storage->storage,
                               val,
                               type,
                               data->boolean_true);

      if (glsl_type_is_sampler(storage->type)) {
         for (int sh = 0; sh < MESA_SHADER_STAGES; sh++) {
            struct gl_linked_shader *shader =
               data->shader_prog->_LinkedShaders[sh];

            if (shader && storage->opaque[sh].active) {
               unsigned index = storage->opaque[sh].index;

               shader->Program->SamplerUnits[index] = storage->storage[0].i;
            }
         }
      }
   }
}

void
gl_nir_set_uniform_initializers(struct gl_context *ctx,
                                struct gl_shader_program *prog)
{
   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
      struct gl_linked_shader *sh = prog->_LinkedShaders[i];
      if (!sh)
         continue;

      nir_shader *nir = sh->Program->nir;
      assert(nir);

      nir_foreach_variable(var, &nir->uniforms) {
         if (var->constant_initializer) {
            struct set_uniform_initializer_closure data = {
               .shader_prog = prog,
               .prog = sh->Program,
               .var = var,
               .location = var->data.location,
               .boolean_true = ctx->Const.UniformBooleanTrue
            };
            set_uniform_initializer(&data,
                                    var->type,
                                    var->constant_initializer);
         } else if (var->data.explicit_binding) {
            const struct glsl_type *without_array =
               glsl_without_array(var->type);

            if (glsl_type_is_sampler(without_array) ||
                glsl_type_is_image(without_array)) {
               struct set_opaque_binding_closure data = {
                  .shader_prog = prog,
                  .prog = sh->Program,
                  .var = var,
                  .binding = var->data.binding,
                  .location = var->data.location
               };
               set_opaque_binding(&data, var->type);
            }
         }
      }
   }
}