summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/tgsi/tgsi_dynamic_indexing.c
blob: d2fa4af0f2b7a3dbf1249b10aa071b56d18fcf4c (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright 2018 VMware, Inc.
 * All Rights Reserved.
 *
 * 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, sub license, 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 NON-INFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS 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.
 */


/**
 * This utility transforms the shader to support dynamic array indexing
 * for samplers and constant buffers.
 * It calculates dynamic array index first and then compare it with each
 * index and operation will be performed with matching index
 */

#include "util/u_debug.h"
#include "util/u_math.h"
#include "tgsi_info.h"
#include "tgsi_dynamic_indexing.h"
#include "tgsi_transform.h"
#include "tgsi_dump.h"
#include "pipe/p_state.h"


struct dIndexing_transform_context
{
   struct tgsi_transform_context base;
   unsigned orig_num_tmp;
   unsigned orig_num_imm;
   unsigned num_const_bufs;
   unsigned num_samplers;
   unsigned num_iterations;
   unsigned const_buf_range[PIPE_MAX_CONSTANT_BUFFERS];
};


static inline struct dIndexing_transform_context *
dIndexing_transform_context(struct tgsi_transform_context *ctx)
{
   return (struct dIndexing_transform_context *) ctx;
}


/**
 * TGSI declaration transform callback.
 */
static void
dIndexing_decl(struct tgsi_transform_context *ctx,
               struct tgsi_full_declaration *decl)
{
   struct dIndexing_transform_context *dc = dIndexing_transform_context(ctx);

   if (decl->Declaration.File == TGSI_FILE_TEMPORARY) {
      /**
       * Emit some extra temporary register to use in keeping track of
       * dynamic index.
       */
      dc->orig_num_tmp = decl->Range.Last;
      decl->Range.Last = decl->Range.Last + 3;
   }
   else if (decl->Declaration.File == TGSI_FILE_CONSTANT) {
      /* Keep track of number of constants in each buffer */
      dc->const_buf_range[decl->Dim.Index2D] = decl->Range.Last;
   }
   ctx->emit_declaration(ctx, decl);
}


/**
 * TGSI transform prolog callback.
 */
static void
dIndexing_prolog(struct tgsi_transform_context *ctx)
{
   tgsi_transform_immediate_int_decl(ctx, 0, 1, 2, 3);
   tgsi_transform_immediate_int_decl(ctx, 4, 5, 6, 7);
}


/**
 * This function emits some extra instruction to remove dynamic array
 * indexing of constant buffers / samplers from the shader.
 * It calculates dynamic array index first and compare it with each index for
 * declared constants/samplers.
 */
static void
remove_dynamic_indexes(struct tgsi_transform_context *ctx,
                       struct tgsi_full_instruction *orig_inst,
                       const struct tgsi_full_src_register *reg)
{
   struct dIndexing_transform_context *dc = dIndexing_transform_context(ctx);
   int i, j;
   int tmp_loopIdx = dc->orig_num_tmp + 1;
   int tmp_cond = dc->orig_num_tmp + 2;
   int tmp_arrayIdx = dc->orig_num_tmp + 3;
   int imm_index = dc->orig_num_imm;
   struct tgsi_full_instruction inst;
   unsigned INVALID_INDEX = 99999;
   unsigned file = TGSI_FILE_NULL, index = INVALID_INDEX;
   unsigned imm_swz_index = INVALID_INDEX;

   /* calculate dynamic array index store it in tmp_arrayIdx.x */
   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = TGSI_OPCODE_UADD;
   inst.Instruction.NumDstRegs = 1;
   tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY,
                          tmp_arrayIdx, TGSI_WRITEMASK_X);
   inst.Instruction.NumSrcRegs = 2;
   if (reg->Register.File == TGSI_FILE_CONSTANT) {
      file = reg->DimIndirect.File;
      index = reg->DimIndirect.Index;
      imm_swz_index = reg->Dimension.Index;
   }
   else if (reg->Register.File == TGSI_FILE_SAMPLER) {
      file = reg->Indirect.File;
      index = reg->Indirect.Index;
      imm_swz_index = reg->Register.Index;
   }
   tgsi_transform_src_reg(&inst.Src[0], file,
                          index, TGSI_SWIZZLE_X,
                          TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
   tgsi_transform_src_reg(&inst.Src[1], TGSI_FILE_IMMEDIATE,
                          imm_index + (imm_swz_index / 4),
                          imm_swz_index % 4,
                          imm_swz_index % 4,
                          imm_swz_index % 4,
                          imm_swz_index % 4);
   ctx->emit_instruction(ctx, &inst);

   /* initialize counter to zero: tmp_loopIdx = 0 */
   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = TGSI_OPCODE_MOV;
   inst.Instruction.NumDstRegs = 1;
   tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY,
                          tmp_loopIdx, TGSI_WRITEMASK_X);
   inst.Instruction.NumSrcRegs = 1;
   tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_IMMEDIATE,
                          imm_index, TGSI_SWIZZLE_X,
                          TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
                          TGSI_SWIZZLE_X);
   ctx->emit_instruction(ctx, &inst);

   for (i = 0; i < dc->num_iterations; i++) {
      boolean out_of_bound_index = FALSE;
      /**
       * Make sure we are not exceeding index limit of constant buffer
       *
       * For example, In declaration, We have
       *
       * DCL CONST[0][0..1]
       * DCL CONST[1][0..2]
       * DCL CONST[2][0]
       *
       * and our dynamic index instruction is
       * MOV TEMP[0], CONST[ADDR[0].x][1]
       *
       * We have to make sure to skip unrolling for CONST[2] because
       * it has only one constant in the buffer
       */
      if ((reg->Register.File == TGSI_FILE_CONSTANT) &&
          (!reg->Register.Indirect &&
           (reg->Register.Index > dc->const_buf_range[i]))) {
         out_of_bound_index = TRUE;
      }

      if (!out_of_bound_index) {
         /**
          * If we have an instruction of the format:
          * OPCODE dst, src..., CONST[K][foo], src...
          * where K is dynamic and tmp_loopIdx = i (loopcount),
          * replace it with:
          *
          * if (K == tmp_loopIdx)
          *    OPCODE dst, src... where src is CONST[i][foo] and i is constant
          * }
          *
          * Similarly, If instruction uses dynamic array index for samplers
          * e.g. OPCODE dst, src, SAMPL[k] ..
          * replace it with:
          * if (K == tmp_loopIdx)
          *    OPCODE dst, src, SAMPL[i][foo]... where i is constant.
          * }
          */
         inst = tgsi_default_full_instruction();
         inst.Instruction.Opcode = TGSI_OPCODE_USEQ;
         inst.Instruction.NumDstRegs = 1;
         tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY,
                                tmp_cond, TGSI_WRITEMASK_X);
         inst.Instruction.NumSrcRegs = 2;
         tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_TEMPORARY,
                                tmp_arrayIdx, TGSI_SWIZZLE_X,
                                TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
                                TGSI_SWIZZLE_X);
         tgsi_transform_src_reg(&inst.Src[1], TGSI_FILE_TEMPORARY,
                                tmp_loopIdx, TGSI_SWIZZLE_X,
                                TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
                                TGSI_SWIZZLE_X);
         ctx->emit_instruction(ctx, &inst);

         inst = tgsi_default_full_instruction();
         inst.Instruction.Opcode = TGSI_OPCODE_UIF;
         inst.Instruction.NumDstRegs = 0;
         inst.Instruction.NumSrcRegs = 1;
         tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_TEMPORARY,
                                tmp_cond, TGSI_SWIZZLE_X,
                                TGSI_SWIZZLE_X, TGSI_SWIZZLE_X,
                                TGSI_SWIZZLE_X);
         ctx->emit_instruction(ctx, &inst);

         /* emit instruction with new, non-dynamic source registers */
         inst = *orig_inst;
         for (j = 0; j < inst.Instruction.NumSrcRegs; j++) {
            if (inst.Src[j].Dimension.Indirect &&
                inst.Src[j].Register.File == TGSI_FILE_CONSTANT) {
               inst.Src[j].Register.Dimension = 1;
               inst.Src[j].Dimension.Index = i;
               inst.Src[j].Dimension.Indirect = 0;
            }
            else if (inst.Src[j].Register.Indirect &&
                     inst.Src[j].Register.File == TGSI_FILE_SAMPLER) {
               inst.Src[j].Register.Indirect = 0;
               inst.Src[j].Register.Index = i;
            }
         }
         ctx->emit_instruction(ctx, &inst);

         inst = tgsi_default_full_instruction();
         inst.Instruction.Opcode = TGSI_OPCODE_ENDIF;
         inst.Instruction.NumDstRegs = 0;
         inst.Instruction.NumSrcRegs = 0;
         ctx->emit_instruction(ctx, &inst);
      }

      /**
       * Increment counter
       * UADD tmp_loopIdx.x tmp_loopIdx.x imm(1)
       */
      inst = tgsi_default_full_instruction();
      inst.Instruction.Opcode = TGSI_OPCODE_UADD;
      inst.Instruction.NumDstRegs = 1;
      tgsi_transform_dst_reg(&inst.Dst[0], TGSI_FILE_TEMPORARY,
                             tmp_loopIdx, TGSI_WRITEMASK_X);
      inst.Instruction.NumSrcRegs = 2;
      tgsi_transform_src_reg(&inst.Src[0], TGSI_FILE_TEMPORARY,
                              tmp_loopIdx, TGSI_SWIZZLE_X,
                              TGSI_SWIZZLE_X, TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
      tgsi_transform_src_reg(&inst.Src[1], TGSI_FILE_IMMEDIATE, imm_index,
                             TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y,
                             TGSI_SWIZZLE_Y, TGSI_SWIZZLE_Y);

      ctx->emit_instruction(ctx, &inst);
   }
}


/**
 * TGSI instruction transform callback.
 */
static void
dIndexing_inst(struct tgsi_transform_context *ctx,
               struct tgsi_full_instruction *inst)
{
   int i;
   boolean indexing = FALSE;
   struct dIndexing_transform_context *dc = dIndexing_transform_context(ctx);

   for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
      struct tgsi_full_src_register *src;
      src = &inst->Src[i];
      /* check if constant buffer/sampler is using dynamic index */
      if ((src->Dimension.Indirect &&
           src->Register.File == TGSI_FILE_CONSTANT) ||
          (src->Register.Indirect &&
           src->Register.File == TGSI_FILE_SAMPLER)) {

         if (indexing)
            assert("More than one src has dynamic indexing");

         if (src->Register.File == TGSI_FILE_CONSTANT)
            dc->num_iterations = dc->num_const_bufs;
         else
            dc->num_iterations = dc->num_samplers;

         remove_dynamic_indexes(ctx, inst, src);
         indexing = TRUE;
      }
   }

   if (!indexing) {
      ctx->emit_instruction(ctx, inst);
   }
}

/**
 * TGSI utility to remove dynamic array indexing for constant buffers and
 * samplers.
 *
 * This utility accepts bitmask of declared constant buffers and samplers,
 * number of immediates used in shader.
 *
 * If dynamic array index is used for constant buffers and samplers, this
 * utility removes those dynamic indexes from shader. It also makes sure
 * that it has same output as per original shader.
 * This is achieved by calculating dynamic array index first and then compare
 * it with each constant buffer/ sampler index and replace that dynamic index
 * with static index.
 */
struct tgsi_token *
tgsi_remove_dynamic_indexing(const struct tgsi_token *tokens_in,
                             unsigned const_buffers_declared_bitmask,
                             unsigned samplers_declared_bitmask,
                             unsigned imm_count)
{
   struct dIndexing_transform_context transform;
   const uint num_new_tokens = 1000; /* should be enough */
   const uint new_len = tgsi_num_tokens(tokens_in) + num_new_tokens;

   /* setup transformation context */
   memset(&transform, 0, sizeof(transform));
   transform.base.transform_declaration = dIndexing_decl;
   transform.base.transform_instruction = dIndexing_inst;
   transform.base.prolog = dIndexing_prolog;

   transform.orig_num_tmp = 0;
   transform.orig_num_imm = imm_count;
   /* get count of declared const buffers and sampler from their bitmasks*/
   transform.num_const_bufs = log2(const_buffers_declared_bitmask + 1);
   transform.num_samplers = log2(samplers_declared_bitmask + 1);
   transform.num_iterations = 0;

   return tgsi_transform_shader(tokens_in, new_len, &transform.base);
}