summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/tgsi/tgsi_transform.h
blob: c21ff959cbfbaccfeac087ce8e014db8fc5584e3 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**************************************************************************
 * 
 * Copyright 2008 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 VMWARE 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.
 * 
 **************************************************************************/

#ifndef TGSI_TRANSFORM_H
#define TGSI_TRANSFORM_H


#include "pipe/p_shader_tokens.h"
#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_build.h"



/**
 * Subclass this to add caller-specific data
 */
struct tgsi_transform_context
{
/**** PUBLIC ***/

   /**
    * User-defined callbacks invoked per instruction.
    */
   void (*transform_instruction)(struct tgsi_transform_context *ctx,
                                 struct tgsi_full_instruction *inst);

   void (*transform_declaration)(struct tgsi_transform_context *ctx,
                                 struct tgsi_full_declaration *decl);

   void (*transform_immediate)(struct tgsi_transform_context *ctx,
                               struct tgsi_full_immediate *imm);
   void (*transform_property)(struct tgsi_transform_context *ctx,
                              struct tgsi_full_property *prop);

   /**
    * Called after last declaration, before first instruction.  This is
    * where the user might insert new declarations and/or instructions.
    */
   void (*prolog)(struct tgsi_transform_context *ctx);

   /**
    * Called at end of input program to allow caller to append extra
    * instructions.  Return number of tokens emitted.
    */
   void (*epilog)(struct tgsi_transform_context *ctx);


/*** PRIVATE ***/

   /**
    * These are setup by tgsi_transform_shader() and cannot be overridden.
    * Meant to be called from in the above user callback functions.
    */
   void (*emit_instruction)(struct tgsi_transform_context *ctx,
                            const struct tgsi_full_instruction *inst);
   void (*emit_declaration)(struct tgsi_transform_context *ctx,
                            const struct tgsi_full_declaration *decl);
   void (*emit_immediate)(struct tgsi_transform_context *ctx,
                          const struct tgsi_full_immediate *imm);
   void (*emit_property)(struct tgsi_transform_context *ctx,
                         const struct tgsi_full_property *prop);

   struct tgsi_header *header;
   uint max_tokens_out;
   struct tgsi_token *tokens_out;
   uint ti;
};


/**
 * Helper for emitting temporary register declarations.
 */
static inline void
tgsi_transform_temps_decl(struct tgsi_transform_context *ctx,
                          unsigned firstIdx, unsigned lastIdx)
{
   struct tgsi_full_declaration decl;

   decl = tgsi_default_full_declaration();
   decl.Declaration.File = TGSI_FILE_TEMPORARY;
   decl.Range.First = firstIdx;
   decl.Range.Last = lastIdx;
   ctx->emit_declaration(ctx, &decl);
}

static inline void
tgsi_transform_temp_decl(struct tgsi_transform_context *ctx,
                         unsigned index)
{
   tgsi_transform_temps_decl(ctx, index, index);
}

static inline void
tgsi_transform_const_decl(struct tgsi_transform_context *ctx,
                          unsigned firstIdx, unsigned lastIdx)
{
   struct tgsi_full_declaration decl;

   decl = tgsi_default_full_declaration();
   decl.Declaration.File = TGSI_FILE_CONSTANT;
   decl.Range.First = firstIdx;
   decl.Range.Last = lastIdx;
   ctx->emit_declaration(ctx, &decl);
}
 
static inline void
tgsi_transform_input_decl(struct tgsi_transform_context *ctx,
                          unsigned index,
                          unsigned sem_name, unsigned sem_index,
                          unsigned interp)
{
   struct tgsi_full_declaration decl;

   decl = tgsi_default_full_declaration();
   decl.Declaration.File = TGSI_FILE_INPUT;
   decl.Declaration.Interpolate = 1;
   decl.Declaration.Semantic = 1;
   decl.Semantic.Name = sem_name;
   decl.Semantic.Index = sem_index;
   decl.Range.First =
   decl.Range.Last = index;
   decl.Interp.Interpolate = interp;

   ctx->emit_declaration(ctx, &decl);
}

static inline void
tgsi_transform_output_decl(struct tgsi_transform_context *ctx,
                          unsigned index,
                          unsigned sem_name, unsigned sem_index,
                          unsigned interp)
{
   struct tgsi_full_declaration decl;

   decl = tgsi_default_full_declaration();
   decl.Declaration.File = TGSI_FILE_OUTPUT;
   decl.Declaration.Interpolate = 1;
   decl.Declaration.Semantic = 1;
   decl.Semantic.Name = sem_name;
   decl.Semantic.Index = sem_index;
   decl.Range.First =
   decl.Range.Last = index;
   decl.Interp.Interpolate = interp;

   ctx->emit_declaration(ctx, &decl);
}

static inline void
tgsi_transform_sampler_decl(struct tgsi_transform_context *ctx,
                            unsigned index)
{
   struct tgsi_full_declaration decl;

   decl = tgsi_default_full_declaration();
   decl.Declaration.File = TGSI_FILE_SAMPLER;
   decl.Range.First =
   decl.Range.Last = index;
   ctx->emit_declaration(ctx, &decl);
}

static inline void
tgsi_transform_sampler_view_decl(struct tgsi_transform_context *ctx,
                                 unsigned index,
                                 unsigned target,
                                 enum tgsi_return_type type)
{
   struct tgsi_full_declaration decl;

   decl = tgsi_default_full_declaration();
   decl.Declaration.File = TGSI_FILE_SAMPLER_VIEW;
   decl.Declaration.UsageMask = TGSI_WRITEMASK_XYZW;
   decl.Range.First =
   decl.Range.Last = index;
   decl.SamplerView.Resource = target;
   decl.SamplerView.ReturnTypeX = type;
   decl.SamplerView.ReturnTypeY = type;
   decl.SamplerView.ReturnTypeZ = type;
   decl.SamplerView.ReturnTypeW = type;

   ctx->emit_declaration(ctx, &decl);
}

static inline void
tgsi_transform_immediate_decl(struct tgsi_transform_context *ctx,
                              float x, float y, float z, float w)
{
   struct tgsi_full_immediate immed;
   unsigned size = 4;

   immed = tgsi_default_full_immediate();
   immed.Immediate.NrTokens = 1 + size; /* one for the token itself */
   immed.u[0].Float = x;
   immed.u[1].Float = y;
   immed.u[2].Float = z;
   immed.u[3].Float = w;

   ctx->emit_immediate(ctx, &immed);
}

static inline void
tgsi_transform_dst_reg(struct tgsi_full_dst_register *reg,
                       unsigned file, unsigned index, unsigned writemask)
{
   reg->Register.File = file;
   reg->Register.Index = index;
   reg->Register.WriteMask = writemask;
}

static inline void
tgsi_transform_src_reg(struct tgsi_full_src_register *reg,
                       unsigned file, unsigned index, 
                       unsigned swizzleX, unsigned swizzleY,
                       unsigned swizzleZ, unsigned swizzleW)
{
   reg->Register.File = file;
   reg->Register.Index = index;
   reg->Register.SwizzleX = swizzleX; 
   reg->Register.SwizzleY = swizzleY; 
   reg->Register.SwizzleZ = swizzleZ; 
   reg->Register.SwizzleW = swizzleW; 
}

/**
 * Helper for emitting 1-operand instructions.
 */
static inline void
tgsi_transform_op1_inst(struct tgsi_transform_context *ctx,
                        unsigned opcode,
                        unsigned dst_file,
                        unsigned dst_index,
                        unsigned dst_writemask,
                        unsigned src0_file,
                        unsigned src0_index)
{
   struct tgsi_full_instruction inst;

   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = opcode;
   inst.Instruction.NumDstRegs = 1;
   inst.Dst[0].Register.File = dst_file,
   inst.Dst[0].Register.Index = dst_index;
   inst.Dst[0].Register.WriteMask = dst_writemask;
   inst.Instruction.NumSrcRegs = 1;
   inst.Src[0].Register.File = src0_file;
   inst.Src[0].Register.Index = src0_index;

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


static inline void
tgsi_transform_op2_inst(struct tgsi_transform_context *ctx,
                        unsigned opcode,
                        unsigned dst_file,
                        unsigned dst_index,
                        unsigned dst_writemask,
                        unsigned src0_file,
                        unsigned src0_index,
                        unsigned src1_file,
                        unsigned src1_index)
{
   struct tgsi_full_instruction inst;

   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = opcode;
   inst.Instruction.NumDstRegs = 1;
   inst.Dst[0].Register.File = dst_file,
   inst.Dst[0].Register.Index = dst_index;
   inst.Dst[0].Register.WriteMask = dst_writemask;
   inst.Instruction.NumSrcRegs = 2;
   inst.Src[0].Register.File = src0_file;
   inst.Src[0].Register.Index = src0_index;
   inst.Src[1].Register.File = src1_file;
   inst.Src[1].Register.Index = src1_index;

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


static inline void
tgsi_transform_op3_inst(struct tgsi_transform_context *ctx,
                        unsigned opcode,
                        unsigned dst_file,
                        unsigned dst_index,
                        unsigned dst_writemask,
                        unsigned src0_file,
                        unsigned src0_index,
                        unsigned src1_file,
                        unsigned src1_index,
                        unsigned src2_file,
                        unsigned src2_index)
{
   struct tgsi_full_instruction inst;

   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = opcode;
   inst.Instruction.NumDstRegs = 1;
   inst.Dst[0].Register.File = dst_file,
   inst.Dst[0].Register.Index = dst_index;
   inst.Dst[0].Register.WriteMask = dst_writemask;
   inst.Instruction.NumSrcRegs = 3;
   inst.Src[0].Register.File = src0_file;
   inst.Src[0].Register.Index = src0_index;
   inst.Src[1].Register.File = src1_file;
   inst.Src[1].Register.Index = src1_index;
   inst.Src[2].Register.File = src2_file;
   inst.Src[2].Register.Index = src2_index;

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



static inline void
tgsi_transform_op1_swz_inst(struct tgsi_transform_context *ctx,
                            unsigned opcode,
                            unsigned dst_file,
                            unsigned dst_index,
                            unsigned dst_writemask,
                            unsigned src0_file,
                            unsigned src0_index,
                            unsigned src0_swizzle)
{
   struct tgsi_full_instruction inst;

   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = opcode;
   inst.Instruction.NumDstRegs = 1;
   inst.Dst[0].Register.File = dst_file,
   inst.Dst[0].Register.Index = dst_index;
   inst.Dst[0].Register.WriteMask = dst_writemask;
   inst.Instruction.NumSrcRegs = 1;
   inst.Src[0].Register.File = src0_file;
   inst.Src[0].Register.Index = src0_index;
   switch (dst_writemask) {
   case TGSI_WRITEMASK_X:
      inst.Src[0].Register.SwizzleX = src0_swizzle;
      break;
   case TGSI_WRITEMASK_Y:
      inst.Src[0].Register.SwizzleY = src0_swizzle;
      break;
   case TGSI_WRITEMASK_Z:
      inst.Src[0].Register.SwizzleZ = src0_swizzle;
      break;
   case TGSI_WRITEMASK_W:
      inst.Src[0].Register.SwizzleW = src0_swizzle;
      break;
   default:
      ; /* nothing */
   }

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


static inline void
tgsi_transform_op2_swz_inst(struct tgsi_transform_context *ctx,
                            unsigned opcode,
                            unsigned dst_file,
                            unsigned dst_index,
                            unsigned dst_writemask,
                            unsigned src0_file,
                            unsigned src0_index,
                            unsigned src0_swizzle,
                            unsigned src1_file,
                            unsigned src1_index,
                            unsigned src1_swizzle)
{
   struct tgsi_full_instruction inst;

   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = opcode;
   inst.Instruction.NumDstRegs = 1;
   inst.Dst[0].Register.File = dst_file,
   inst.Dst[0].Register.Index = dst_index;
   inst.Dst[0].Register.WriteMask = dst_writemask;
   inst.Instruction.NumSrcRegs = 2;
   inst.Src[0].Register.File = src0_file;
   inst.Src[0].Register.Index = src0_index;
   inst.Src[1].Register.File = src1_file;
   inst.Src[1].Register.Index = src1_index;
   switch (dst_writemask) {
   case TGSI_WRITEMASK_X:
      inst.Src[0].Register.SwizzleX = src0_swizzle;
      inst.Src[1].Register.SwizzleX = src1_swizzle;
      break;
   case TGSI_WRITEMASK_Y:
      inst.Src[0].Register.SwizzleY = src0_swizzle;
      inst.Src[1].Register.SwizzleY = src1_swizzle;
      break;
   case TGSI_WRITEMASK_Z:
      inst.Src[0].Register.SwizzleZ = src0_swizzle;
      inst.Src[1].Register.SwizzleZ = src1_swizzle;
      break;
   case TGSI_WRITEMASK_W:
      inst.Src[0].Register.SwizzleW = src0_swizzle;
      inst.Src[1].Register.SwizzleW = src1_swizzle;
      break;
   default:
      ; /* nothing */
   }

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


static inline void
tgsi_transform_op3_swz_inst(struct tgsi_transform_context *ctx,
                            unsigned opcode,
                            unsigned dst_file,
                            unsigned dst_index,
                            unsigned dst_writemask,
                            unsigned src0_file,
                            unsigned src0_index,
                            unsigned src0_swizzle,
                            unsigned src0_negate,
                            unsigned src1_file,
                            unsigned src1_index,
                            unsigned src1_swizzle,
                            unsigned src2_file,
                            unsigned src2_index,
                            unsigned src2_swizzle)
{
   struct tgsi_full_instruction inst;

   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = opcode;
   inst.Instruction.NumDstRegs = 1;
   inst.Dst[0].Register.File = dst_file,
   inst.Dst[0].Register.Index = dst_index;
   inst.Dst[0].Register.WriteMask = dst_writemask;
   inst.Instruction.NumSrcRegs = 3;
   inst.Src[0].Register.File = src0_file;
   inst.Src[0].Register.Index = src0_index;
   inst.Src[0].Register.Negate = src0_negate;
   inst.Src[1].Register.File = src1_file;
   inst.Src[1].Register.Index = src1_index;
   inst.Src[2].Register.File = src2_file;
   inst.Src[2].Register.Index = src2_index;
   switch (dst_writemask) {
   case TGSI_WRITEMASK_X:
      inst.Src[0].Register.SwizzleX = src0_swizzle;
      inst.Src[1].Register.SwizzleX = src1_swizzle;
      inst.Src[2].Register.SwizzleX = src2_swizzle;
      break;
   case TGSI_WRITEMASK_Y:
      inst.Src[0].Register.SwizzleY = src0_swizzle;
      inst.Src[1].Register.SwizzleY = src1_swizzle;
      inst.Src[2].Register.SwizzleY = src2_swizzle;
      break;
   case TGSI_WRITEMASK_Z:
      inst.Src[0].Register.SwizzleZ = src0_swizzle;
      inst.Src[1].Register.SwizzleZ = src1_swizzle;
      inst.Src[2].Register.SwizzleZ = src2_swizzle;
      break;
   case TGSI_WRITEMASK_W:
      inst.Src[0].Register.SwizzleW = src0_swizzle;
      inst.Src[1].Register.SwizzleW = src1_swizzle;
      inst.Src[2].Register.SwizzleW = src2_swizzle;
      break;
   default:
      ; /* nothing */
   }

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


static inline void
tgsi_transform_kill_inst(struct tgsi_transform_context *ctx,
                         unsigned src_file,
                         unsigned src_index,
                         unsigned src_swizzle,
                         boolean negate)
{
   struct tgsi_full_instruction inst;

   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = TGSI_OPCODE_KILL_IF;
   inst.Instruction.NumDstRegs = 0;
   inst.Instruction.NumSrcRegs = 1;
   inst.Src[0].Register.File = src_file;
   inst.Src[0].Register.Index = src_index;
   inst.Src[0].Register.SwizzleX =
   inst.Src[0].Register.SwizzleY =
   inst.Src[0].Register.SwizzleZ =
   inst.Src[0].Register.SwizzleW = src_swizzle;
   inst.Src[0].Register.Negate = negate;

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


static inline void
tgsi_transform_tex_inst(struct tgsi_transform_context *ctx,
                        unsigned dst_file,
                        unsigned dst_index,
                        unsigned src_file,
                        unsigned src_index,
                        unsigned tex_target,
                        unsigned sampler_index)
{
   struct tgsi_full_instruction inst;

   assert(tex_target < TGSI_TEXTURE_COUNT);

   inst = tgsi_default_full_instruction();
   inst.Instruction.Opcode = TGSI_OPCODE_TEX;
   inst.Instruction.NumDstRegs = 1;
   inst.Dst[0].Register.File = dst_file;
   inst.Dst[0].Register.Index = dst_index;
   inst.Instruction.NumSrcRegs = 2;
   inst.Instruction.Texture = TRUE;
   inst.Texture.Texture = tex_target;
   inst.Src[0].Register.File = src_file;
   inst.Src[0].Register.Index = src_index;
   inst.Src[1].Register.File = TGSI_FILE_SAMPLER;
   inst.Src[1].Register.Index = sampler_index;

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


extern int
tgsi_transform_shader(const struct tgsi_token *tokens_in,
                      struct tgsi_token *tokens_out,
                      uint max_tokens_out,
                      struct tgsi_transform_context *ctx);


#endif /* TGSI_TRANSFORM_H */