1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #ifndef TGSI_TRANSFORM_H
29 #define TGSI_TRANSFORM_H
32 #include "pipe/p_shader_tokens.h"
33 #include "tgsi/tgsi_parse.h"
34 #include "tgsi/tgsi_build.h"
39 * Subclass this to add caller-specific data
41 struct tgsi_transform_context
46 * User-defined callbacks invoked per instruction.
48 void (*transform_instruction
)(struct tgsi_transform_context
*ctx
,
49 struct tgsi_full_instruction
*inst
);
51 void (*transform_declaration
)(struct tgsi_transform_context
*ctx
,
52 struct tgsi_full_declaration
*decl
);
54 void (*transform_immediate
)(struct tgsi_transform_context
*ctx
,
55 struct tgsi_full_immediate
*imm
);
56 void (*transform_property
)(struct tgsi_transform_context
*ctx
,
57 struct tgsi_full_property
*prop
);
60 * Called after last declaration, before first instruction. This is
61 * where the user might insert new declarations and/or instructions.
63 void (*prolog
)(struct tgsi_transform_context
*ctx
);
66 * Called at end of input program to allow caller to append extra
67 * instructions. Return number of tokens emitted.
69 void (*epilog
)(struct tgsi_transform_context
*ctx
);
75 * These are setup by tgsi_transform_shader() and cannot be overridden.
76 * Meant to be called from in the above user callback functions.
78 void (*emit_instruction
)(struct tgsi_transform_context
*ctx
,
79 const struct tgsi_full_instruction
*inst
);
80 void (*emit_declaration
)(struct tgsi_transform_context
*ctx
,
81 const struct tgsi_full_declaration
*decl
);
82 void (*emit_immediate
)(struct tgsi_transform_context
*ctx
,
83 const struct tgsi_full_immediate
*imm
);
84 void (*emit_property
)(struct tgsi_transform_context
*ctx
,
85 const struct tgsi_full_property
*prop
);
87 struct tgsi_header
*header
;
89 struct tgsi_token
*tokens_out
;
95 * Helper for emitting temporary register declarations.
98 tgsi_transform_temps_decl(struct tgsi_transform_context
*ctx
,
99 unsigned firstIdx
, unsigned lastIdx
)
101 struct tgsi_full_declaration decl
;
103 decl
= tgsi_default_full_declaration();
104 decl
.Declaration
.File
= TGSI_FILE_TEMPORARY
;
105 decl
.Range
.First
= firstIdx
;
106 decl
.Range
.Last
= lastIdx
;
107 ctx
->emit_declaration(ctx
, &decl
);
111 tgsi_transform_temp_decl(struct tgsi_transform_context
*ctx
,
114 tgsi_transform_temps_decl(ctx
, index
, index
);
118 tgsi_transform_const_decl(struct tgsi_transform_context
*ctx
,
119 unsigned firstIdx
, unsigned lastIdx
)
121 struct tgsi_full_declaration decl
;
123 decl
= tgsi_default_full_declaration();
124 decl
.Declaration
.File
= TGSI_FILE_CONSTANT
;
125 decl
.Range
.First
= firstIdx
;
126 decl
.Range
.Last
= lastIdx
;
127 ctx
->emit_declaration(ctx
, &decl
);
131 tgsi_transform_input_decl(struct tgsi_transform_context
*ctx
,
133 unsigned sem_name
, unsigned sem_index
,
136 struct tgsi_full_declaration decl
;
138 decl
= tgsi_default_full_declaration();
139 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
140 decl
.Declaration
.Interpolate
= 1;
141 decl
.Declaration
.Semantic
= 1;
142 decl
.Semantic
.Name
= sem_name
;
143 decl
.Semantic
.Index
= sem_index
;
145 decl
.Range
.Last
= index
;
146 decl
.Interp
.Interpolate
= interp
;
148 ctx
->emit_declaration(ctx
, &decl
);
152 tgsi_transform_output_decl(struct tgsi_transform_context
*ctx
,
154 unsigned sem_name
, unsigned sem_index
,
157 struct tgsi_full_declaration decl
;
159 decl
= tgsi_default_full_declaration();
160 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
161 decl
.Declaration
.Interpolate
= 1;
162 decl
.Declaration
.Semantic
= 1;
163 decl
.Semantic
.Name
= sem_name
;
164 decl
.Semantic
.Index
= sem_index
;
166 decl
.Range
.Last
= index
;
167 decl
.Interp
.Interpolate
= interp
;
169 ctx
->emit_declaration(ctx
, &decl
);
173 tgsi_transform_sampler_decl(struct tgsi_transform_context
*ctx
,
176 struct tgsi_full_declaration decl
;
178 decl
= tgsi_default_full_declaration();
179 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
181 decl
.Range
.Last
= index
;
182 ctx
->emit_declaration(ctx
, &decl
);
186 tgsi_transform_sampler_view_decl(struct tgsi_transform_context
*ctx
,
189 enum tgsi_return_type type
)
191 struct tgsi_full_declaration decl
;
193 decl
= tgsi_default_full_declaration();
194 decl
.Declaration
.File
= TGSI_FILE_SAMPLER_VIEW
;
195 decl
.Declaration
.UsageMask
= TGSI_WRITEMASK_XYZW
;
197 decl
.Range
.Last
= index
;
198 decl
.SamplerView
.Resource
= target
;
199 decl
.SamplerView
.ReturnTypeX
= type
;
200 decl
.SamplerView
.ReturnTypeY
= type
;
201 decl
.SamplerView
.ReturnTypeZ
= type
;
202 decl
.SamplerView
.ReturnTypeW
= type
;
204 ctx
->emit_declaration(ctx
, &decl
);
208 tgsi_transform_immediate_decl(struct tgsi_transform_context
*ctx
,
209 float x
, float y
, float z
, float w
)
211 struct tgsi_full_immediate immed
;
214 immed
= tgsi_default_full_immediate();
215 immed
.Immediate
.NrTokens
= 1 + size
; /* one for the token itself */
216 immed
.u
[0].Float
= x
;
217 immed
.u
[1].Float
= y
;
218 immed
.u
[2].Float
= z
;
219 immed
.u
[3].Float
= w
;
221 ctx
->emit_immediate(ctx
, &immed
);
225 tgsi_transform_dst_reg(struct tgsi_full_dst_register
*reg
,
226 unsigned file
, unsigned index
, unsigned writemask
)
228 reg
->Register
.File
= file
;
229 reg
->Register
.Index
= index
;
230 reg
->Register
.WriteMask
= writemask
;
234 tgsi_transform_src_reg(struct tgsi_full_src_register
*reg
,
235 unsigned file
, unsigned index
,
236 unsigned swizzleX
, unsigned swizzleY
,
237 unsigned swizzleZ
, unsigned swizzleW
)
239 reg
->Register
.File
= file
;
240 reg
->Register
.Index
= index
;
241 reg
->Register
.SwizzleX
= swizzleX
;
242 reg
->Register
.SwizzleY
= swizzleY
;
243 reg
->Register
.SwizzleZ
= swizzleZ
;
244 reg
->Register
.SwizzleW
= swizzleW
;
248 * Helper for emitting 1-operand instructions.
251 tgsi_transform_op1_inst(struct tgsi_transform_context
*ctx
,
255 unsigned dst_writemask
,
259 struct tgsi_full_instruction inst
;
261 inst
= tgsi_default_full_instruction();
262 inst
.Instruction
.Opcode
= opcode
;
263 inst
.Instruction
.NumDstRegs
= 1;
264 inst
.Dst
[0].Register
.File
= dst_file
,
265 inst
.Dst
[0].Register
.Index
= dst_index
;
266 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
267 inst
.Instruction
.NumSrcRegs
= 1;
268 inst
.Src
[0].Register
.File
= src0_file
;
269 inst
.Src
[0].Register
.Index
= src0_index
;
271 ctx
->emit_instruction(ctx
, &inst
);
276 tgsi_transform_op2_inst(struct tgsi_transform_context
*ctx
,
280 unsigned dst_writemask
,
286 struct tgsi_full_instruction inst
;
288 inst
= tgsi_default_full_instruction();
289 inst
.Instruction
.Opcode
= opcode
;
290 inst
.Instruction
.NumDstRegs
= 1;
291 inst
.Dst
[0].Register
.File
= dst_file
,
292 inst
.Dst
[0].Register
.Index
= dst_index
;
293 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
294 inst
.Instruction
.NumSrcRegs
= 2;
295 inst
.Src
[0].Register
.File
= src0_file
;
296 inst
.Src
[0].Register
.Index
= src0_index
;
297 inst
.Src
[1].Register
.File
= src1_file
;
298 inst
.Src
[1].Register
.Index
= src1_index
;
300 ctx
->emit_instruction(ctx
, &inst
);
305 tgsi_transform_op3_inst(struct tgsi_transform_context
*ctx
,
309 unsigned dst_writemask
,
317 struct tgsi_full_instruction inst
;
319 inst
= tgsi_default_full_instruction();
320 inst
.Instruction
.Opcode
= opcode
;
321 inst
.Instruction
.NumDstRegs
= 1;
322 inst
.Dst
[0].Register
.File
= dst_file
,
323 inst
.Dst
[0].Register
.Index
= dst_index
;
324 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
325 inst
.Instruction
.NumSrcRegs
= 3;
326 inst
.Src
[0].Register
.File
= src0_file
;
327 inst
.Src
[0].Register
.Index
= src0_index
;
328 inst
.Src
[1].Register
.File
= src1_file
;
329 inst
.Src
[1].Register
.Index
= src1_index
;
330 inst
.Src
[2].Register
.File
= src2_file
;
331 inst
.Src
[2].Register
.Index
= src2_index
;
333 ctx
->emit_instruction(ctx
, &inst
);
339 tgsi_transform_op1_swz_inst(struct tgsi_transform_context
*ctx
,
343 unsigned dst_writemask
,
346 unsigned src0_swizzle
)
348 struct tgsi_full_instruction inst
;
350 inst
= tgsi_default_full_instruction();
351 inst
.Instruction
.Opcode
= opcode
;
352 inst
.Instruction
.NumDstRegs
= 1;
353 inst
.Dst
[0].Register
.File
= dst_file
,
354 inst
.Dst
[0].Register
.Index
= dst_index
;
355 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
356 inst
.Instruction
.NumSrcRegs
= 1;
357 inst
.Src
[0].Register
.File
= src0_file
;
358 inst
.Src
[0].Register
.Index
= src0_index
;
359 switch (dst_writemask
) {
360 case TGSI_WRITEMASK_X
:
361 inst
.Src
[0].Register
.SwizzleX
= src0_swizzle
;
363 case TGSI_WRITEMASK_Y
:
364 inst
.Src
[0].Register
.SwizzleY
= src0_swizzle
;
366 case TGSI_WRITEMASK_Z
:
367 inst
.Src
[0].Register
.SwizzleZ
= src0_swizzle
;
369 case TGSI_WRITEMASK_W
:
370 inst
.Src
[0].Register
.SwizzleW
= src0_swizzle
;
376 ctx
->emit_instruction(ctx
, &inst
);
381 tgsi_transform_op2_swz_inst(struct tgsi_transform_context
*ctx
,
385 unsigned dst_writemask
,
388 unsigned src0_swizzle
,
391 unsigned src1_swizzle
)
393 struct tgsi_full_instruction inst
;
395 inst
= tgsi_default_full_instruction();
396 inst
.Instruction
.Opcode
= opcode
;
397 inst
.Instruction
.NumDstRegs
= 1;
398 inst
.Dst
[0].Register
.File
= dst_file
,
399 inst
.Dst
[0].Register
.Index
= dst_index
;
400 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
401 inst
.Instruction
.NumSrcRegs
= 2;
402 inst
.Src
[0].Register
.File
= src0_file
;
403 inst
.Src
[0].Register
.Index
= src0_index
;
404 inst
.Src
[1].Register
.File
= src1_file
;
405 inst
.Src
[1].Register
.Index
= src1_index
;
406 switch (dst_writemask
) {
407 case TGSI_WRITEMASK_X
:
408 inst
.Src
[0].Register
.SwizzleX
= src0_swizzle
;
409 inst
.Src
[1].Register
.SwizzleX
= src1_swizzle
;
411 case TGSI_WRITEMASK_Y
:
412 inst
.Src
[0].Register
.SwizzleY
= src0_swizzle
;
413 inst
.Src
[1].Register
.SwizzleY
= src1_swizzle
;
415 case TGSI_WRITEMASK_Z
:
416 inst
.Src
[0].Register
.SwizzleZ
= src0_swizzle
;
417 inst
.Src
[1].Register
.SwizzleZ
= src1_swizzle
;
419 case TGSI_WRITEMASK_W
:
420 inst
.Src
[0].Register
.SwizzleW
= src0_swizzle
;
421 inst
.Src
[1].Register
.SwizzleW
= src1_swizzle
;
427 ctx
->emit_instruction(ctx
, &inst
);
432 tgsi_transform_op3_swz_inst(struct tgsi_transform_context
*ctx
,
436 unsigned dst_writemask
,
439 unsigned src0_swizzle
,
440 unsigned src0_negate
,
443 unsigned src1_swizzle
,
446 unsigned src2_swizzle
)
448 struct tgsi_full_instruction inst
;
450 inst
= tgsi_default_full_instruction();
451 inst
.Instruction
.Opcode
= opcode
;
452 inst
.Instruction
.NumDstRegs
= 1;
453 inst
.Dst
[0].Register
.File
= dst_file
,
454 inst
.Dst
[0].Register
.Index
= dst_index
;
455 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
456 inst
.Instruction
.NumSrcRegs
= 3;
457 inst
.Src
[0].Register
.File
= src0_file
;
458 inst
.Src
[0].Register
.Index
= src0_index
;
459 inst
.Src
[0].Register
.Negate
= src0_negate
;
460 inst
.Src
[1].Register
.File
= src1_file
;
461 inst
.Src
[1].Register
.Index
= src1_index
;
462 inst
.Src
[2].Register
.File
= src2_file
;
463 inst
.Src
[2].Register
.Index
= src2_index
;
464 switch (dst_writemask
) {
465 case TGSI_WRITEMASK_X
:
466 inst
.Src
[0].Register
.SwizzleX
= src0_swizzle
;
467 inst
.Src
[1].Register
.SwizzleX
= src1_swizzle
;
468 inst
.Src
[2].Register
.SwizzleX
= src2_swizzle
;
470 case TGSI_WRITEMASK_Y
:
471 inst
.Src
[0].Register
.SwizzleY
= src0_swizzle
;
472 inst
.Src
[1].Register
.SwizzleY
= src1_swizzle
;
473 inst
.Src
[2].Register
.SwizzleY
= src2_swizzle
;
475 case TGSI_WRITEMASK_Z
:
476 inst
.Src
[0].Register
.SwizzleZ
= src0_swizzle
;
477 inst
.Src
[1].Register
.SwizzleZ
= src1_swizzle
;
478 inst
.Src
[2].Register
.SwizzleZ
= src2_swizzle
;
480 case TGSI_WRITEMASK_W
:
481 inst
.Src
[0].Register
.SwizzleW
= src0_swizzle
;
482 inst
.Src
[1].Register
.SwizzleW
= src1_swizzle
;
483 inst
.Src
[2].Register
.SwizzleW
= src2_swizzle
;
489 ctx
->emit_instruction(ctx
, &inst
);
494 tgsi_transform_kill_inst(struct tgsi_transform_context
*ctx
,
497 unsigned src_swizzle
,
500 struct tgsi_full_instruction inst
;
502 inst
= tgsi_default_full_instruction();
503 inst
.Instruction
.Opcode
= TGSI_OPCODE_KILL_IF
;
504 inst
.Instruction
.NumDstRegs
= 0;
505 inst
.Instruction
.NumSrcRegs
= 1;
506 inst
.Src
[0].Register
.File
= src_file
;
507 inst
.Src
[0].Register
.Index
= src_index
;
508 inst
.Src
[0].Register
.SwizzleX
=
509 inst
.Src
[0].Register
.SwizzleY
=
510 inst
.Src
[0].Register
.SwizzleZ
=
511 inst
.Src
[0].Register
.SwizzleW
= src_swizzle
;
512 inst
.Src
[0].Register
.Negate
= negate
;
514 ctx
->emit_instruction(ctx
, &inst
);
519 tgsi_transform_tex_inst(struct tgsi_transform_context
*ctx
,
525 unsigned sampler_index
)
527 struct tgsi_full_instruction inst
;
529 assert(tex_target
< TGSI_TEXTURE_COUNT
);
531 inst
= tgsi_default_full_instruction();
532 inst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
533 inst
.Instruction
.NumDstRegs
= 1;
534 inst
.Dst
[0].Register
.File
= dst_file
;
535 inst
.Dst
[0].Register
.Index
= dst_index
;
536 inst
.Instruction
.NumSrcRegs
= 2;
537 inst
.Instruction
.Texture
= TRUE
;
538 inst
.Texture
.Texture
= tex_target
;
539 inst
.Src
[0].Register
.File
= src_file
;
540 inst
.Src
[0].Register
.Index
= src_index
;
541 inst
.Src
[1].Register
.File
= TGSI_FILE_SAMPLER
;
542 inst
.Src
[1].Register
.Index
= sampler_index
;
544 ctx
->emit_instruction(ctx
, &inst
);
549 tgsi_transform_shader(const struct tgsi_token
*tokens_in
,
550 struct tgsi_token
*tokens_out
,
552 struct tgsi_transform_context
*ctx
);
555 #endif /* TGSI_TRANSFORM_H */