summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/tgsi/tgsi_transform.h
blob: 921aa906527e781c256caf37f7622678f1bf9755 (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
/**************************************************************************
 * 
 * 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_temp_decl(struct tgsi_transform_context *ctx,
                         unsigned index)
{
   struct tgsi_full_declaration decl;

   decl = tgsi_default_full_declaration();
   decl.Declaration.File = TGSI_FILE_TEMPORARY;
   decl.Range.First =
   decl.Range.Last = index;
   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_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_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);
}


/**
 * 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_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)
{
   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 = 1;

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


static INLINE void
tgsi_transform_tex_2d_inst(struct tgsi_transform_context *ctx,
                           unsigned dst_file,
                           unsigned dst_index,
                           unsigned src_file,
                           unsigned src_index,
                           unsigned sampler_index)
{
   struct tgsi_full_instruction inst;

   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 = TGSI_TEXTURE_2D;
   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 */