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
= 0xf;
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_op1_swz_inst(struct tgsi_transform_context
*ctx
,
309 unsigned dst_writemask
,
312 unsigned src0_swizzle
)
314 struct tgsi_full_instruction inst
;
316 inst
= tgsi_default_full_instruction();
317 inst
.Instruction
.Opcode
= opcode
;
318 inst
.Instruction
.NumDstRegs
= 1;
319 inst
.Dst
[0].Register
.File
= dst_file
,
320 inst
.Dst
[0].Register
.Index
= dst_index
;
321 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
322 inst
.Instruction
.NumSrcRegs
= 1;
323 inst
.Src
[0].Register
.File
= src0_file
;
324 inst
.Src
[0].Register
.Index
= src0_index
;
325 switch (dst_writemask
) {
326 case TGSI_WRITEMASK_X
:
327 inst
.Src
[0].Register
.SwizzleX
= src0_swizzle
;
329 case TGSI_WRITEMASK_Y
:
330 inst
.Src
[0].Register
.SwizzleY
= src0_swizzle
;
332 case TGSI_WRITEMASK_Z
:
333 inst
.Src
[0].Register
.SwizzleZ
= src0_swizzle
;
335 case TGSI_WRITEMASK_W
:
336 inst
.Src
[0].Register
.SwizzleW
= src0_swizzle
;
342 ctx
->emit_instruction(ctx
, &inst
);
347 tgsi_transform_op2_swz_inst(struct tgsi_transform_context
*ctx
,
351 unsigned dst_writemask
,
354 unsigned src0_swizzle
,
357 unsigned src1_swizzle
)
359 struct tgsi_full_instruction inst
;
361 inst
= tgsi_default_full_instruction();
362 inst
.Instruction
.Opcode
= opcode
;
363 inst
.Instruction
.NumDstRegs
= 1;
364 inst
.Dst
[0].Register
.File
= dst_file
,
365 inst
.Dst
[0].Register
.Index
= dst_index
;
366 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
367 inst
.Instruction
.NumSrcRegs
= 2;
368 inst
.Src
[0].Register
.File
= src0_file
;
369 inst
.Src
[0].Register
.Index
= src0_index
;
370 inst
.Src
[1].Register
.File
= src1_file
;
371 inst
.Src
[1].Register
.Index
= src1_index
;
372 switch (dst_writemask
) {
373 case TGSI_WRITEMASK_X
:
374 inst
.Src
[0].Register
.SwizzleX
= src0_swizzle
;
375 inst
.Src
[1].Register
.SwizzleX
= src1_swizzle
;
377 case TGSI_WRITEMASK_Y
:
378 inst
.Src
[0].Register
.SwizzleY
= src0_swizzle
;
379 inst
.Src
[1].Register
.SwizzleY
= src1_swizzle
;
381 case TGSI_WRITEMASK_Z
:
382 inst
.Src
[0].Register
.SwizzleZ
= src0_swizzle
;
383 inst
.Src
[1].Register
.SwizzleZ
= src1_swizzle
;
385 case TGSI_WRITEMASK_W
:
386 inst
.Src
[0].Register
.SwizzleW
= src0_swizzle
;
387 inst
.Src
[1].Register
.SwizzleW
= src1_swizzle
;
393 ctx
->emit_instruction(ctx
, &inst
);
398 tgsi_transform_op3_swz_inst(struct tgsi_transform_context
*ctx
,
402 unsigned dst_writemask
,
405 unsigned src0_swizzle
,
406 unsigned src0_negate
,
409 unsigned src1_swizzle
,
412 unsigned src2_swizzle
)
414 struct tgsi_full_instruction inst
;
416 inst
= tgsi_default_full_instruction();
417 inst
.Instruction
.Opcode
= opcode
;
418 inst
.Instruction
.NumDstRegs
= 1;
419 inst
.Dst
[0].Register
.File
= dst_file
,
420 inst
.Dst
[0].Register
.Index
= dst_index
;
421 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
422 inst
.Instruction
.NumSrcRegs
= 3;
423 inst
.Src
[0].Register
.File
= src0_file
;
424 inst
.Src
[0].Register
.Index
= src0_index
;
425 inst
.Src
[0].Register
.Negate
= src0_negate
;
426 inst
.Src
[1].Register
.File
= src1_file
;
427 inst
.Src
[1].Register
.Index
= src1_index
;
428 inst
.Src
[2].Register
.File
= src2_file
;
429 inst
.Src
[2].Register
.Index
= src2_index
;
430 switch (dst_writemask
) {
431 case TGSI_WRITEMASK_X
:
432 inst
.Src
[0].Register
.SwizzleX
= src0_swizzle
;
433 inst
.Src
[1].Register
.SwizzleX
= src1_swizzle
;
434 inst
.Src
[2].Register
.SwizzleX
= src2_swizzle
;
436 case TGSI_WRITEMASK_Y
:
437 inst
.Src
[0].Register
.SwizzleY
= src0_swizzle
;
438 inst
.Src
[1].Register
.SwizzleY
= src1_swizzle
;
439 inst
.Src
[2].Register
.SwizzleY
= src2_swizzle
;
441 case TGSI_WRITEMASK_Z
:
442 inst
.Src
[0].Register
.SwizzleZ
= src0_swizzle
;
443 inst
.Src
[1].Register
.SwizzleZ
= src1_swizzle
;
444 inst
.Src
[2].Register
.SwizzleZ
= src2_swizzle
;
446 case TGSI_WRITEMASK_W
:
447 inst
.Src
[0].Register
.SwizzleW
= src0_swizzle
;
448 inst
.Src
[1].Register
.SwizzleW
= src1_swizzle
;
449 inst
.Src
[2].Register
.SwizzleW
= src2_swizzle
;
455 ctx
->emit_instruction(ctx
, &inst
);
460 tgsi_transform_kill_inst(struct tgsi_transform_context
*ctx
,
463 unsigned src_swizzle
,
466 struct tgsi_full_instruction inst
;
468 inst
= tgsi_default_full_instruction();
469 inst
.Instruction
.Opcode
= TGSI_OPCODE_KILL_IF
;
470 inst
.Instruction
.NumDstRegs
= 0;
471 inst
.Instruction
.NumSrcRegs
= 1;
472 inst
.Src
[0].Register
.File
= src_file
;
473 inst
.Src
[0].Register
.Index
= src_index
;
474 inst
.Src
[0].Register
.SwizzleX
=
475 inst
.Src
[0].Register
.SwizzleY
=
476 inst
.Src
[0].Register
.SwizzleZ
=
477 inst
.Src
[0].Register
.SwizzleW
= src_swizzle
;
478 inst
.Src
[0].Register
.Negate
= negate
;
480 ctx
->emit_instruction(ctx
, &inst
);
485 tgsi_transform_tex_2d_inst(struct tgsi_transform_context
*ctx
,
490 unsigned sampler_index
)
492 struct tgsi_full_instruction inst
;
494 inst
= tgsi_default_full_instruction();
495 inst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
496 inst
.Instruction
.NumDstRegs
= 1;
497 inst
.Dst
[0].Register
.File
= dst_file
;
498 inst
.Dst
[0].Register
.Index
= dst_index
;
499 inst
.Instruction
.NumSrcRegs
= 2;
500 inst
.Instruction
.Texture
= TRUE
;
501 inst
.Texture
.Texture
= TGSI_TEXTURE_2D
;
502 inst
.Src
[0].Register
.File
= src_file
;
503 inst
.Src
[0].Register
.Index
= src_index
;
504 inst
.Src
[1].Register
.File
= TGSI_FILE_SAMPLER
;
505 inst
.Src
[1].Register
.Index
= sampler_index
;
507 ctx
->emit_instruction(ctx
, &inst
);
512 tgsi_transform_shader(const struct tgsi_token
*tokens_in
,
513 struct tgsi_token
*tokens_out
,
515 struct tgsi_transform_context
*ctx
);
518 #endif /* TGSI_TRANSFORM_H */