aboutsummaryrefslogtreecommitdiffstats
path: root/src/compiler/glsl/gl_nir_link_xfb.c
blob: bcef1e1863da741ae8298661f8d0df65a77dbae2 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * 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 "ir_uniform.h" /* for gl_uniform_storage */
#include "linker_util.h"
#include "main/context.h"

/*
 * This file does the linking of GLSL transform feedback using NIR.
 *
 * Note: This linking pass is currently tailored for ARB_gl_spirv needs and
 * particularities.
 */

struct active_xfb_buffer {
   GLuint stride;
   GLuint num_varyings;
};

struct active_xfb_varyings {
   unsigned num_varyings;
   unsigned num_outputs;
   unsigned buffer_size;
   struct nir_variable **varyings;
   struct active_xfb_buffer buffers[MAX_FEEDBACK_BUFFERS];
};

static unsigned
get_num_outputs(nir_variable *var)
{
   return glsl_count_attribute_slots(var->type,
                                     false /* is_vertex_input */);
}

static void
add_xfb_varying(struct active_xfb_varyings *active_varyings,
                nir_variable *var)
{
   if (active_varyings->num_varyings >= active_varyings->buffer_size) {
      if (active_varyings->buffer_size == 0)
         active_varyings->buffer_size = 1;
      else
         active_varyings->buffer_size *= 2;

      active_varyings->varyings = realloc(active_varyings->varyings,
                                          sizeof(nir_variable*) *
                                          active_varyings->buffer_size);
   }

   active_varyings->varyings[active_varyings->num_varyings++] = var;

   active_varyings->num_outputs += get_num_outputs(var);
}

static int
cmp_xfb_offset(const void *x_generic, const void *y_generic)
{
   const nir_variable *const *x = x_generic;
   const nir_variable *const *y = y_generic;

   if ((*x)->data.xfb_buffer != (*y)->data.xfb_buffer)
      return (*x)->data.xfb_buffer - (*y)->data.xfb_buffer;
   return (*x)->data.offset - (*y)->data.offset;
}

static void
get_active_xfb_varyings(struct gl_shader_program *prog,
                        struct active_xfb_varyings *active_varyings)
{
   for (unsigned i = 0; i < MESA_SHADER_STAGES; ++i) {
      struct gl_linked_shader *sh = prog->_LinkedShaders[i];
      if (sh == NULL)
         continue;

      nir_shader *nir = sh->Program->nir;

      nir_foreach_variable(var, &nir->outputs) {
         if (var->data.explicit_xfb_buffer &&
             var->data.explicit_xfb_stride) {
            assert(var->data.xfb_buffer < MAX_FEEDBACK_BUFFERS);
            active_varyings->buffers[var->data.xfb_buffer].stride =
               var->data.xfb_stride;
         }

         if (!var->data.explicit_xfb_buffer ||
             !var->data.explicit_offset)
            continue;

         active_varyings->buffers[var->data.xfb_buffer].num_varyings++;

         add_xfb_varying(active_varyings, var);
      }
   }

   /* The xfb_offset qualifier does not have to be used in increasing order
    * however some drivers expect to receive the list of transform feedback
    * declarations in order so sort it now for convenience.
    */
   qsort(active_varyings->varyings,
         active_varyings->num_varyings,
         sizeof(*active_varyings->varyings),
         cmp_xfb_offset);
}

static unsigned
add_varying_outputs(nir_variable *var,
                    const struct glsl_type *type,
                    unsigned location_offset,
                    unsigned dest_offset,
                    struct gl_transform_feedback_output *output)
{
   unsigned num_outputs = 0;

   if (glsl_type_is_array(type) || glsl_type_is_matrix(type)) {
      unsigned length = glsl_get_length(type);
      const struct glsl_type *child_type = glsl_get_array_element(type);
      unsigned component_slots = glsl_get_component_slots(child_type);

      for (unsigned i = 0; i < length; i++) {
         unsigned child_outputs = add_varying_outputs(var,
                                                      child_type,
                                                      location_offset,
                                                      dest_offset,
                                                      output + num_outputs);
         num_outputs += child_outputs;
         location_offset += child_outputs;
         dest_offset += component_slots;
      }
   } else if (glsl_type_is_struct(type)) {
      unsigned length = glsl_get_length(type);
      for (unsigned i = 0; i < length; i++) {
         const struct glsl_type *child_type = glsl_get_struct_field(type, i);
         unsigned child_outputs = add_varying_outputs(var,
                                                      child_type,
                                                      location_offset,
                                                      dest_offset,
                                                      output + num_outputs);
         num_outputs += child_outputs;
         location_offset += child_outputs;
         dest_offset += glsl_get_component_slots(child_type);
      }
   } else {
      unsigned location = var->data.location + location_offset;
      unsigned location_frac = var->data.location_frac;
      unsigned num_components = glsl_get_component_slots(type);

      while (num_components > 0) {
         unsigned output_size = MIN2(num_components, 4 - location_frac);

         output->OutputRegister = location;
         output->OutputBuffer = var->data.xfb_buffer;
         output->NumComponents = output_size;
         output->StreamId = var->data.stream;
         output->DstOffset = var->data.offset / 4 + dest_offset;
         output->ComponentOffset = location_frac;

         dest_offset += output_size;
         num_components -= output_size;
         num_outputs++;
         output++;
         location++;
         location_frac = 0;
      }
   }

   return num_outputs;
}

void
gl_nir_link_assign_xfb_resources(struct gl_context *ctx,
                                 struct gl_shader_program *prog)
{
   /*
    * From ARB_gl_spirv spec:
    *
    *    "- If the *Xfb* Execution Mode is set, any output variable that is at
    *       least partially captured:
    *       * must be decorated with an *XfbBuffer*, declaring the capturing buffer
    *       * must have at least one captured output variable in the capturing
    *         buffer decorated with an *XfbStride* (and all such *XfbStride* values
    *         for the capturing buffer must be equal)
    *     - If the *Xfb* Execution Mode is set, any captured output:
    *       * must be a non-structure decorated with *Offset* or a member of a
    *         structure whose type member is decorated with *Offset*"
    *
    * Note the "must be", meaning that explicit buffer, offset and stride are
    * mandatory. So as this is intended to work with SPIR-V shaders we don't
    * need to calculate the offset or the stride.
    */

   struct gl_program *xfb_prog = prog->last_vert_prog;

   if (xfb_prog == NULL)
      return;

   /* free existing varyings, if any */
   for (unsigned i = 0; i < prog->TransformFeedback.NumVarying; i++)
      free(prog->TransformFeedback.VaryingNames[i]);
   free(prog->TransformFeedback.VaryingNames);

   struct active_xfb_varyings active_varyings = { 0 };

   get_active_xfb_varyings(prog, &active_varyings);

   for (unsigned buf = 0; buf < MAX_FEEDBACK_BUFFERS; buf++)
      prog->TransformFeedback.BufferStride[buf] = active_varyings.buffers[buf].stride;

   prog->TransformFeedback.NumVarying = active_varyings.num_varyings;
   prog->TransformFeedback.VaryingNames =
      malloc(sizeof(GLchar *) * active_varyings.num_varyings);

   struct gl_transform_feedback_info *linked_xfb =
      rzalloc(xfb_prog, struct gl_transform_feedback_info);
   xfb_prog->sh.LinkedTransformFeedback = linked_xfb;

   linked_xfb->Outputs =
      rzalloc_array(xfb_prog,
                    struct gl_transform_feedback_output,
                    active_varyings.num_outputs);
   linked_xfb->NumOutputs = active_varyings.num_outputs;

   linked_xfb->Varyings =
      rzalloc_array(xfb_prog,
                    struct gl_transform_feedback_varying_info,
                    active_varyings.num_varyings);
   linked_xfb->NumVarying = active_varyings.num_varyings;

   struct gl_transform_feedback_output *output = linked_xfb->Outputs;
   for (unsigned i = 0; i < active_varyings.num_varyings; i++) {
      struct nir_variable *var = active_varyings.varyings[i];

      /* From ARB_gl_spirv spec:
       *
       *    "19. How should the program interface query operations behave for
       *         program objects created from SPIR-V shaders?
       *
       *     DISCUSSION: we previously said we didn't need reflection to work
       *     for SPIR-V shaders (at least for the first version), however we
       *     are left with specifying how it should "not work". The primary
       *     issue is that SPIR-V binaries are not required to have names
       *     associated with variables. They can be associated in debug
       *     information, but there is no requirement for that to be present,
       *     and it should not be relied upon."
       *
       *     Options:"
       *
       *     <skip>
       *
       *     "RESOLVED.  Pick (c), but also allow debug names to be returned
       *      if an implementation wants to."
       *
       * So names are considered optional debug info, so the linker needs to
       * work without them, and returning them is optional. For simplicity at
       * this point we are ignoring names
       */
      prog->TransformFeedback.VaryingNames[i] = NULL;

      unsigned varying_outputs = add_varying_outputs(var,
                                                     var->type,
                                                     0, /* location_offset */
                                                     0, /* dest_offset */
                                                     output);
      assert(varying_outputs == get_num_outputs(var));
      output = output + varying_outputs;

      struct gl_transform_feedback_varying_info *varying =
         linked_xfb->Varyings + i;

      /* ARB_gl_spirv: see above. */
      varying->Name = NULL;
      varying->Type = glsl_get_gl_type(var->type);
      varying->BufferIndex = var->data.xfb_buffer;
      varying->Size = glsl_get_length(var->type);
      varying->Offset = var->data.offset;
   }

   /* Make sure MaxTransformFeedbackBuffers is <= 32 so the bitmask for
    * tracking the number of buffers doesn't overflow.
    */
   unsigned buffers = 0;
   assert(ctx->Const.MaxTransformFeedbackBuffers <= sizeof(buffers) * 8);

   for (unsigned buf = 0; buf < MAX_FEEDBACK_BUFFERS; buf++) {
      if (active_varyings.buffers[buf].stride > 0) {
         linked_xfb->Buffers[buf].Stride = active_varyings.buffers[buf].stride / 4;
         linked_xfb->Buffers[buf].NumVaryings = active_varyings.buffers[buf].num_varyings;
         buffers |= 1 << buf;
      }
   }

   linked_xfb->ActiveBuffers = buffers;

   free(active_varyings.varyings);
}