summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/virgl/virgl_tgsi.c
blob: af19401ebdb1fc433bb4d7e3d18c28db1610bf3d (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright 2014, 2015 Red Hat.
 *
 * 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
 * on 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 AUTHOR(S) AND/OR THEIR 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.
 */

/* the virgl hw tgsi vs what the current gallium want will diverge over time.
   so add a transform stage to remove things we don't want to send unless
   the receiver supports it.
*/

#include "tgsi/tgsi_transform.h"
#include "tgsi/tgsi_info.h"
#include "tgsi/tgsi_scan.h"
#include "virgl_context.h"
#include "virgl_screen.h"

struct virgl_input_temp {
   enum tgsi_file_type file;

   /* Index within in the INPUT or SV files, or ~0 if no DCL of this input */
   unsigned index;

   /* TGSI_FILE_TEMPORARY index it will be mapped to. */
   unsigned temp;

   bool sint;
};

enum virgl_input_temps {
   INPUT_TEMP_LAYER,
   INPUT_TEMP_VIEWPORT_INDEX,
   INPUT_TEMP_BLOCK_ID,
   INPUT_TEMP_HELPER_INVOCATION,
   INPUT_TEMP_SAMPLEMASK,
   INPUT_TEMP_COUNT,
};

struct virgl_transform_context {
   struct tgsi_transform_context base;
   struct tgsi_shader_info info;

   bool cull_enabled;
   bool has_precise;
   bool fake_fp64;

   unsigned next_temp;

   unsigned src_temp;

   unsigned writemask_fixup_outs[5];
   unsigned writemask_fixup_temps;
   unsigned num_writemask_fixups;

   struct virgl_input_temp input_temp[INPUT_TEMP_COUNT];
};

static void
virgl_tgsi_transform_declaration_input_temp(const struct tgsi_full_declaration *decl,
                                            struct virgl_input_temp *input_temp,
                                            enum tgsi_semantic semantic_name)
{
   if (decl->Semantic.Name == semantic_name) {
      input_temp->file = decl->Declaration.File;
      input_temp->index = decl->Range.First;
   }
}

static void
virgl_tgsi_transform_declaration(struct tgsi_transform_context *ctx,
                                 struct tgsi_full_declaration *decl)
{
   struct virgl_transform_context *vtctx = (struct virgl_transform_context *)ctx;

   switch (decl->Declaration.File) {
   case TGSI_FILE_CONSTANT:
      if (decl->Declaration.Dimension) {
         if (decl->Dim.Index2D == 0)
            decl->Declaration.Dimension = 0;
      }
      break;
   case TGSI_FILE_INPUT:
      virgl_tgsi_transform_declaration_input_temp(decl, &vtctx->input_temp[INPUT_TEMP_LAYER],
                                                   TGSI_SEMANTIC_LAYER);
      virgl_tgsi_transform_declaration_input_temp(decl, &vtctx->input_temp[INPUT_TEMP_VIEWPORT_INDEX],
                                                   TGSI_SEMANTIC_VIEWPORT_INDEX);
      break;
   case TGSI_FILE_SYSTEM_VALUE:
      virgl_tgsi_transform_declaration_input_temp(decl, &vtctx->input_temp[INPUT_TEMP_BLOCK_ID],
                                                   TGSI_SEMANTIC_BLOCK_ID);
      virgl_tgsi_transform_declaration_input_temp(decl, &vtctx->input_temp[INPUT_TEMP_HELPER_INVOCATION],
                                                   TGSI_SEMANTIC_HELPER_INVOCATION);
      virgl_tgsi_transform_declaration_input_temp(decl, &vtctx->input_temp[INPUT_TEMP_SAMPLEMASK],
                                                   TGSI_SEMANTIC_SAMPLEMASK);
      break;
   case TGSI_FILE_OUTPUT:
      switch (decl->Semantic.Name) {
      case TGSI_SEMANTIC_CLIPDIST:
         vtctx->writemask_fixup_outs[vtctx->num_writemask_fixups++] = decl->Range.First;
         if (decl->Range.Last != decl->Range.First)
            vtctx->writemask_fixup_outs[vtctx->num_writemask_fixups++] = decl->Range.Last;
         break;
      case TGSI_SEMANTIC_CLIPVERTEX:
         vtctx->writemask_fixup_outs[vtctx->num_writemask_fixups++] = decl->Range.First;
         break;
      case TGSI_SEMANTIC_COLOR:
         /* Vertex front/backface color output also has issues with writemasking */
         if (vtctx->base.processor != PIPE_SHADER_FRAGMENT)
            vtctx->writemask_fixup_outs[vtctx->num_writemask_fixups++] = decl->Range.First;
         break;
      }
      break;
   case TGSI_FILE_TEMPORARY:
      vtctx->next_temp = MAX2(vtctx->next_temp, decl->Range.Last + 1);
      break;
   default:
      break;
   }
   assert(vtctx->num_writemask_fixups <= ARRAY_SIZE(vtctx->writemask_fixup_outs));

   ctx->emit_declaration(ctx, decl);
}

/* for now just strip out the new properties the remote doesn't understand
   yet */
static void
virgl_tgsi_transform_property(struct tgsi_transform_context *ctx,
                              struct tgsi_full_property *prop)
{
   struct virgl_transform_context *vtctx = (struct virgl_transform_context *)ctx;
   switch (prop->Property.PropertyName) {
   case TGSI_PROPERTY_NUM_CLIPDIST_ENABLED:
   case TGSI_PROPERTY_NUM_CULLDIST_ENABLED:
      if (vtctx->cull_enabled)
	 ctx->emit_property(ctx, prop);
      break;
   case TGSI_PROPERTY_NEXT_SHADER:
      break;
   default:
      ctx->emit_property(ctx, prop);
      break;
   }
}

static void
virgl_mov_input_temp_sint(struct tgsi_transform_context * ctx,
                          struct virgl_input_temp *temp)
{
   if (temp->index != ~0) {
      tgsi_transform_op2_inst(ctx, TGSI_OPCODE_IMAX,
                              TGSI_FILE_TEMPORARY, temp->temp, TGSI_WRITEMASK_XYZW,
                              temp->file, temp->index,
                              temp->file, temp->index, 0);
   }
}

static void
virgl_mov_input_temp_uint(struct tgsi_transform_context * ctx,
                          struct virgl_input_temp *temp)
{
   if (temp->index != ~0) {
      tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,
                              TGSI_FILE_TEMPORARY, temp->temp, TGSI_WRITEMASK_XYZW,
                              temp->file, temp->index);
   }
}

static void
virgl_tgsi_transform_prolog(struct tgsi_transform_context * ctx)
{
   struct virgl_transform_context *vtctx = (struct virgl_transform_context *)ctx;

   if (vtctx->info.uses_doubles || vtctx->info.file_count[TGSI_FILE_SAMPLER_VIEW]) {
      vtctx->src_temp = vtctx->next_temp;
      vtctx->next_temp += 4;
      tgsi_transform_temps_decl(ctx, vtctx->src_temp, vtctx->src_temp + 3);
   }

   if (vtctx->num_writemask_fixups) {
      vtctx->writemask_fixup_temps = vtctx->next_temp;
      vtctx->next_temp += vtctx->num_writemask_fixups;
      tgsi_transform_temps_decl(ctx,
                                vtctx->writemask_fixup_temps,
                                vtctx->writemask_fixup_temps + vtctx->num_writemask_fixups - 1);
   }

   /* Assign input temps before we emit any instructions, but after we parsed
    * existing temp decls.
    */
   for (int i = 0; i < ARRAY_SIZE(vtctx->input_temp); i++) {
      if (vtctx->input_temp[i].index != ~0) {
         vtctx->input_temp[i].temp = vtctx->next_temp++;
         tgsi_transform_temp_decl(ctx, vtctx->input_temp[i].temp);
      }
   }

   /* virglrenderer makes mistakes in the types of layer/viewport input
    * references from unsigned ops, so we use a temp that we do a no-op signed
    * op to at the top of the shader.
    *
    * https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/615
    */
   virgl_mov_input_temp_sint(ctx, &vtctx->input_temp[INPUT_TEMP_LAYER]);
   virgl_mov_input_temp_sint(ctx, &vtctx->input_temp[INPUT_TEMP_VIEWPORT_INDEX]);
   virgl_mov_input_temp_sint(ctx, &vtctx->input_temp[INPUT_TEMP_SAMPLEMASK]);

   /* virglrenderer also makes mistakes in the types of block id input
    * references from signed ops, so we use a temp that we do a plain MOV to at
    * the top of the shader.  Also, it falls over if an unused channel's swizzle
    * uses the .w of the block id.
    */
   if (vtctx->input_temp[INPUT_TEMP_BLOCK_ID].index != ~0) {
      struct tgsi_full_instruction inst = tgsi_default_full_instruction();
      inst.Instruction.Opcode = TGSI_OPCODE_MOV;
      inst.Instruction.NumDstRegs = 1;
      inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY,
      inst.Dst[0].Register.Index = vtctx->input_temp[INPUT_TEMP_BLOCK_ID].temp;
      inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZ;
      inst.Instruction.NumSrcRegs = 1;
      tgsi_transform_src_reg_xyzw(&inst.Src[0],
                                  vtctx->input_temp[INPUT_TEMP_BLOCK_ID].file,
                                  vtctx->input_temp[INPUT_TEMP_BLOCK_ID].index);
      inst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X;
      inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_Y;
      inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_Z;
      inst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_Z;
      ctx->emit_instruction(ctx, &inst);
   }

   virgl_mov_input_temp_uint(ctx, &vtctx->input_temp[INPUT_TEMP_HELPER_INVOCATION]);
}

static void
virgl_tgsi_rewrite_src_for_input_temp(struct virgl_input_temp *temp, struct tgsi_full_src_register *src)
{
   if (src->Register.File == temp->file && src->Register.Index == temp->index) {
      src->Register.File = TGSI_FILE_TEMPORARY;
      src->Register.Index = temp->temp;
   }
}

static void
virgl_tgsi_transform_instruction(struct tgsi_transform_context *ctx,
				 struct tgsi_full_instruction *inst)
{
   struct virgl_transform_context *vtctx = (struct virgl_transform_context *)ctx;
   if (vtctx->fake_fp64 &&
       (tgsi_opcode_infer_src_type(inst->Instruction.Opcode, 0) == TGSI_TYPE_DOUBLE ||
        tgsi_opcode_infer_dst_type(inst->Instruction.Opcode, 0) == TGSI_TYPE_DOUBLE)) {
      debug_printf("VIRGL: ARB_gpu_shader_fp64 is exposed but not supported.");
      return;
   }

   if (!vtctx->has_precise && inst->Instruction.Precise)
      inst->Instruction.Precise = 0;

   /* virglrenderer can run out of space in internal buffers for immediates as
    * tex operands.  Move the first immediate tex arg to a temp to save space in
    * the buffer.
    *
    * https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/582
    */
   if (tgsi_get_opcode_info(inst->Instruction.Opcode)->is_tex &&
       inst->Src[0].Register.File == TGSI_FILE_IMMEDIATE) {
      tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,
                              TGSI_FILE_TEMPORARY, vtctx->src_temp,
                              TGSI_WRITEMASK_XYZW,
                              inst->Src[0].Register.File,
                              inst->Src[0].Register.Index);
      inst->Src[0].Register.File = TGSI_FILE_TEMPORARY;
      inst->Src[0].Register.Index = vtctx->src_temp;
   }

   for (unsigned i = 0; i < inst->Instruction.NumDstRegs; i++) {
      /* virglrenderer would fail to compile on clipdist, clipvertex, and some
       * two-sided-related color writes without a full writemask.  So, we write
       * to a temp and store that temp with a full writemask.
       *
       * https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/616
       */
      if (inst->Dst[i].Register.File == TGSI_FILE_OUTPUT) {
         for (int j = 0; j < vtctx->num_writemask_fixups; j++) {
            if (inst->Dst[i].Register.Index == vtctx->writemask_fixup_outs[j]) {
               inst->Dst[i].Register.File = TGSI_FILE_TEMPORARY;
               inst->Dst[i].Register.Index = vtctx->writemask_fixup_temps + j;
               break;
            }
         }
      }
   }

   for (unsigned i = 0; i < inst->Instruction.NumSrcRegs; i++) {
      if (inst->Src[i].Register.File == TGSI_FILE_CONSTANT &&
          inst->Src[i].Register.Dimension &&
          inst->Src[i].Dimension.Index == 0)
         inst->Src[i].Register.Dimension = 0;

      for (int j = 0; j < ARRAY_SIZE(vtctx->input_temp); j++)
         virgl_tgsi_rewrite_src_for_input_temp(&vtctx->input_temp[j], &inst->Src[i]);

      /* virglrenderer double inputs twice, so move them to temps and drop the
       * swizzle from the double op.
       */
      if (tgsi_opcode_infer_src_type(inst->Instruction.Opcode, i) == TGSI_TYPE_DOUBLE) {
         struct tgsi_full_instruction temp_inst = tgsi_default_full_instruction();
         temp_inst.Instruction.Opcode = TGSI_OPCODE_MOV;
         temp_inst.Instruction.NumDstRegs = 1;
         temp_inst.Dst[0].Register.File = TGSI_FILE_TEMPORARY,
         temp_inst.Dst[0].Register.Index = vtctx->src_temp + i;
         temp_inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_XYZ;
         temp_inst.Instruction.NumSrcRegs = 1;
         tgsi_transform_src_reg_xyzw(&temp_inst.Src[0], inst->Src[i].Register.File, inst->Src[i].Register.Index);
         temp_inst.Src[0].Register.SwizzleX = inst->Src[i].Register.SwizzleX;
         temp_inst.Src[0].Register.SwizzleY = inst->Src[i].Register.SwizzleY;
         temp_inst.Src[0].Register.SwizzleZ = inst->Src[i].Register.SwizzleZ;
         temp_inst.Src[0].Register.SwizzleW = inst->Src[i].Register.SwizzleW;
         ctx->emit_instruction(ctx, &temp_inst);

         inst->Src[i].Register.File = TGSI_FILE_TEMPORARY;
         inst->Src[i].Register.Index = vtctx->src_temp + i;
         inst->Src[i].Register.SwizzleX = TGSI_SWIZZLE_X;
         inst->Src[i].Register.SwizzleY = TGSI_SWIZZLE_Y;
         inst->Src[i].Register.SwizzleZ = TGSI_SWIZZLE_Z;
         inst->Src[i].Register.SwizzleW = TGSI_SWIZZLE_W;
      }
   }
   ctx->emit_instruction(ctx, inst);

   for (unsigned i = 0; i < inst->Instruction.NumDstRegs; i++) {
      if (vtctx->num_writemask_fixups &&
         inst->Dst[i].Register.File == TGSI_FILE_TEMPORARY &&
         inst->Dst[i].Register.Index >= vtctx->writemask_fixup_temps &&
         inst->Dst[i].Register.Index < vtctx->writemask_fixup_temps + vtctx->num_writemask_fixups) {
         /* Emit the fixup MOV from the clipdist/vert temporary to the real output. */
         unsigned real_out = vtctx->writemask_fixup_outs[inst->Dst[i].Register.Index - vtctx->writemask_fixup_temps];
         tgsi_transform_op1_inst(ctx, TGSI_OPCODE_MOV,
                                 TGSI_FILE_OUTPUT, real_out, TGSI_WRITEMASK_XYZW,
                                 inst->Dst[i].Register.File, inst->Dst[i].Register.Index);
      }
   }
}

struct tgsi_token *virgl_tgsi_transform(struct virgl_screen *vscreen, const struct tgsi_token *tokens_in)
{
   struct virgl_transform_context transform;
   const uint newLen = tgsi_num_tokens(tokens_in);

   memset(&transform, 0, sizeof(transform));
   transform.base.transform_declaration = virgl_tgsi_transform_declaration;
   transform.base.transform_property = virgl_tgsi_transform_property;
   transform.base.transform_instruction = virgl_tgsi_transform_instruction;
   transform.base.prolog = virgl_tgsi_transform_prolog;
   transform.cull_enabled = vscreen->caps.caps.v1.bset.has_cull;
   transform.has_precise = vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_TGSI_PRECISE;
   transform.fake_fp64 =
      vscreen->caps.caps.v2.capability_bits & VIRGL_CAP_FAKE_FP64;

   for (int i = 0; i < ARRAY_SIZE(transform.input_temp); i++)
      transform.input_temp[i].index = ~0;

   tgsi_scan_shader(tokens_in, &transform.info);

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