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 decl
.Declaration
.Dimension
= 1;
128 /* Dim.Index2D is already 0 */
129 ctx
->emit_declaration(ctx
, &decl
);
133 tgsi_transform_input_decl(struct tgsi_transform_context
*ctx
,
135 unsigned sem_name
, unsigned sem_index
,
138 struct tgsi_full_declaration decl
;
140 decl
= tgsi_default_full_declaration();
141 decl
.Declaration
.File
= TGSI_FILE_INPUT
;
142 decl
.Declaration
.Interpolate
= 1;
143 decl
.Declaration
.Semantic
= 1;
144 decl
.Semantic
.Name
= sem_name
;
145 decl
.Semantic
.Index
= sem_index
;
147 decl
.Range
.Last
= index
;
148 decl
.Interp
.Interpolate
= interp
;
150 ctx
->emit_declaration(ctx
, &decl
);
154 tgsi_transform_output_decl(struct tgsi_transform_context
*ctx
,
156 unsigned sem_name
, unsigned sem_index
,
159 struct tgsi_full_declaration decl
;
161 decl
= tgsi_default_full_declaration();
162 decl
.Declaration
.File
= TGSI_FILE_OUTPUT
;
163 decl
.Declaration
.Interpolate
= 1;
164 decl
.Declaration
.Semantic
= 1;
165 decl
.Semantic
.Name
= sem_name
;
166 decl
.Semantic
.Index
= sem_index
;
168 decl
.Range
.Last
= index
;
169 decl
.Interp
.Interpolate
= interp
;
171 ctx
->emit_declaration(ctx
, &decl
);
175 tgsi_transform_sampler_decl(struct tgsi_transform_context
*ctx
,
178 struct tgsi_full_declaration decl
;
180 decl
= tgsi_default_full_declaration();
181 decl
.Declaration
.File
= TGSI_FILE_SAMPLER
;
183 decl
.Range
.Last
= index
;
184 ctx
->emit_declaration(ctx
, &decl
);
188 tgsi_transform_sampler_view_decl(struct tgsi_transform_context
*ctx
,
191 enum tgsi_return_type type
)
193 struct tgsi_full_declaration decl
;
195 decl
= tgsi_default_full_declaration();
196 decl
.Declaration
.File
= TGSI_FILE_SAMPLER_VIEW
;
197 decl
.Declaration
.UsageMask
= TGSI_WRITEMASK_XYZW
;
199 decl
.Range
.Last
= index
;
200 decl
.SamplerView
.Resource
= target
;
201 decl
.SamplerView
.ReturnTypeX
= type
;
202 decl
.SamplerView
.ReturnTypeY
= type
;
203 decl
.SamplerView
.ReturnTypeZ
= type
;
204 decl
.SamplerView
.ReturnTypeW
= type
;
206 ctx
->emit_declaration(ctx
, &decl
);
210 tgsi_transform_immediate_decl(struct tgsi_transform_context
*ctx
,
211 float x
, float y
, float z
, float w
)
213 struct tgsi_full_immediate immed
;
216 immed
= tgsi_default_full_immediate();
217 immed
.Immediate
.NrTokens
= 1 + size
; /* one for the token itself */
218 immed
.u
[0].Float
= x
;
219 immed
.u
[1].Float
= y
;
220 immed
.u
[2].Float
= z
;
221 immed
.u
[3].Float
= w
;
223 ctx
->emit_immediate(ctx
, &immed
);
227 tgsi_transform_dst_reg(struct tgsi_full_dst_register
*reg
,
228 unsigned file
, unsigned index
, unsigned writemask
)
230 reg
->Register
.File
= file
;
231 reg
->Register
.Index
= index
;
232 reg
->Register
.WriteMask
= writemask
;
236 tgsi_transform_src_reg_xyzw(struct tgsi_full_src_register
*reg
,
237 unsigned file
, unsigned index
)
239 reg
->Register
.File
= file
;
240 reg
->Register
.Index
= index
;
241 if (file
== TGSI_FILE_CONSTANT
) {
242 reg
->Register
.Dimension
= 1;
243 reg
->Dimension
.Index
= 0;
248 tgsi_transform_src_reg(struct tgsi_full_src_register
*reg
,
249 unsigned file
, unsigned index
,
250 unsigned swizzleX
, unsigned swizzleY
,
251 unsigned swizzleZ
, unsigned swizzleW
)
253 reg
->Register
.File
= file
;
254 reg
->Register
.Index
= index
;
255 if (file
== TGSI_FILE_CONSTANT
) {
256 reg
->Register
.Dimension
= 1;
257 reg
->Dimension
.Index
= 0;
259 reg
->Register
.SwizzleX
= swizzleX
;
260 reg
->Register
.SwizzleY
= swizzleY
;
261 reg
->Register
.SwizzleZ
= swizzleZ
;
262 reg
->Register
.SwizzleW
= swizzleW
;
266 * Helper for emitting 1-operand instructions.
269 tgsi_transform_op1_inst(struct tgsi_transform_context
*ctx
,
270 enum tgsi_opcode opcode
,
273 unsigned dst_writemask
,
277 struct tgsi_full_instruction inst
;
279 inst
= tgsi_default_full_instruction();
280 inst
.Instruction
.Opcode
= opcode
;
281 inst
.Instruction
.NumDstRegs
= 1;
282 inst
.Dst
[0].Register
.File
= dst_file
,
283 inst
.Dst
[0].Register
.Index
= dst_index
;
284 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
285 inst
.Instruction
.NumSrcRegs
= 1;
286 tgsi_transform_src_reg_xyzw(&inst
.Src
[0], src0_file
, src0_index
);
288 ctx
->emit_instruction(ctx
, &inst
);
293 tgsi_transform_op2_inst(struct tgsi_transform_context
*ctx
,
294 enum tgsi_opcode opcode
,
297 unsigned dst_writemask
,
304 struct tgsi_full_instruction inst
;
306 inst
= tgsi_default_full_instruction();
307 inst
.Instruction
.Opcode
= opcode
;
308 inst
.Instruction
.NumDstRegs
= 1;
309 inst
.Dst
[0].Register
.File
= dst_file
,
310 inst
.Dst
[0].Register
.Index
= dst_index
;
311 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
312 inst
.Instruction
.NumSrcRegs
= 2;
313 tgsi_transform_src_reg_xyzw(&inst
.Src
[0], src0_file
, src0_index
);
314 tgsi_transform_src_reg_xyzw(&inst
.Src
[1], src1_file
, src1_index
);
315 inst
.Src
[1].Register
.Negate
= src1_negate
;
317 ctx
->emit_instruction(ctx
, &inst
);
322 tgsi_transform_op3_inst(struct tgsi_transform_context
*ctx
,
323 enum tgsi_opcode opcode
,
326 unsigned dst_writemask
,
334 struct tgsi_full_instruction inst
;
336 inst
= tgsi_default_full_instruction();
337 inst
.Instruction
.Opcode
= opcode
;
338 inst
.Instruction
.NumDstRegs
= 1;
339 inst
.Dst
[0].Register
.File
= dst_file
,
340 inst
.Dst
[0].Register
.Index
= dst_index
;
341 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
342 inst
.Instruction
.NumSrcRegs
= 3;
343 tgsi_transform_src_reg_xyzw(&inst
.Src
[0], src0_file
, src0_index
);
344 tgsi_transform_src_reg_xyzw(&inst
.Src
[1], src1_file
, src1_index
);
345 tgsi_transform_src_reg_xyzw(&inst
.Src
[2], src2_file
, src2_index
);
347 ctx
->emit_instruction(ctx
, &inst
);
353 tgsi_transform_op1_swz_inst(struct tgsi_transform_context
*ctx
,
354 enum tgsi_opcode opcode
,
357 unsigned dst_writemask
,
360 unsigned src0_swizzle
)
362 struct tgsi_full_instruction inst
;
364 inst
= tgsi_default_full_instruction();
365 inst
.Instruction
.Opcode
= opcode
;
366 inst
.Instruction
.NumDstRegs
= 1;
367 inst
.Dst
[0].Register
.File
= dst_file
,
368 inst
.Dst
[0].Register
.Index
= dst_index
;
369 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
370 inst
.Instruction
.NumSrcRegs
= 1;
371 tgsi_transform_src_reg_xyzw(&inst
.Src
[0], src0_file
, src0_index
);
372 switch (dst_writemask
) {
373 case TGSI_WRITEMASK_X
:
374 inst
.Src
[0].Register
.SwizzleX
= src0_swizzle
;
376 case TGSI_WRITEMASK_Y
:
377 inst
.Src
[0].Register
.SwizzleY
= src0_swizzle
;
379 case TGSI_WRITEMASK_Z
:
380 inst
.Src
[0].Register
.SwizzleZ
= src0_swizzle
;
382 case TGSI_WRITEMASK_W
:
383 inst
.Src
[0].Register
.SwizzleW
= src0_swizzle
;
389 ctx
->emit_instruction(ctx
, &inst
);
394 tgsi_transform_op2_swz_inst(struct tgsi_transform_context
*ctx
,
395 enum tgsi_opcode opcode
,
398 unsigned dst_writemask
,
401 unsigned src0_swizzle
,
404 unsigned src1_swizzle
,
407 struct tgsi_full_instruction inst
;
409 inst
= tgsi_default_full_instruction();
410 inst
.Instruction
.Opcode
= opcode
;
411 inst
.Instruction
.NumDstRegs
= 1;
412 inst
.Dst
[0].Register
.File
= dst_file
,
413 inst
.Dst
[0].Register
.Index
= dst_index
;
414 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
415 inst
.Instruction
.NumSrcRegs
= 2;
416 tgsi_transform_src_reg_xyzw(&inst
.Src
[0], src0_file
, src0_index
);
417 tgsi_transform_src_reg_xyzw(&inst
.Src
[1], src1_file
, src1_index
);
418 inst
.Src
[1].Register
.Negate
= src1_negate
;
419 switch (dst_writemask
) {
420 case TGSI_WRITEMASK_X
:
421 inst
.Src
[0].Register
.SwizzleX
= src0_swizzle
;
422 inst
.Src
[1].Register
.SwizzleX
= src1_swizzle
;
424 case TGSI_WRITEMASK_Y
:
425 inst
.Src
[0].Register
.SwizzleY
= src0_swizzle
;
426 inst
.Src
[1].Register
.SwizzleY
= src1_swizzle
;
428 case TGSI_WRITEMASK_Z
:
429 inst
.Src
[0].Register
.SwizzleZ
= src0_swizzle
;
430 inst
.Src
[1].Register
.SwizzleZ
= src1_swizzle
;
432 case TGSI_WRITEMASK_W
:
433 inst
.Src
[0].Register
.SwizzleW
= src0_swizzle
;
434 inst
.Src
[1].Register
.SwizzleW
= src1_swizzle
;
440 ctx
->emit_instruction(ctx
, &inst
);
445 tgsi_transform_op3_swz_inst(struct tgsi_transform_context
*ctx
,
446 enum tgsi_opcode opcode
,
449 unsigned dst_writemask
,
452 unsigned src0_swizzle
,
453 unsigned src0_negate
,
456 unsigned src1_swizzle
,
459 unsigned src2_swizzle
)
461 struct tgsi_full_instruction inst
;
463 inst
= tgsi_default_full_instruction();
464 inst
.Instruction
.Opcode
= opcode
;
465 inst
.Instruction
.NumDstRegs
= 1;
466 inst
.Dst
[0].Register
.File
= dst_file
,
467 inst
.Dst
[0].Register
.Index
= dst_index
;
468 inst
.Dst
[0].Register
.WriteMask
= dst_writemask
;
469 inst
.Instruction
.NumSrcRegs
= 3;
470 tgsi_transform_src_reg_xyzw(&inst
.Src
[0], src0_file
, src0_index
);
471 inst
.Src
[0].Register
.Negate
= src0_negate
;
472 tgsi_transform_src_reg_xyzw(&inst
.Src
[1], src1_file
, src1_index
);
473 tgsi_transform_src_reg_xyzw(&inst
.Src
[2], src2_file
, src2_index
);
474 switch (dst_writemask
) {
475 case TGSI_WRITEMASK_X
:
476 inst
.Src
[0].Register
.SwizzleX
= src0_swizzle
;
477 inst
.Src
[1].Register
.SwizzleX
= src1_swizzle
;
478 inst
.Src
[2].Register
.SwizzleX
= src2_swizzle
;
480 case TGSI_WRITEMASK_Y
:
481 inst
.Src
[0].Register
.SwizzleY
= src0_swizzle
;
482 inst
.Src
[1].Register
.SwizzleY
= src1_swizzle
;
483 inst
.Src
[2].Register
.SwizzleY
= src2_swizzle
;
485 case TGSI_WRITEMASK_Z
:
486 inst
.Src
[0].Register
.SwizzleZ
= src0_swizzle
;
487 inst
.Src
[1].Register
.SwizzleZ
= src1_swizzle
;
488 inst
.Src
[2].Register
.SwizzleZ
= src2_swizzle
;
490 case TGSI_WRITEMASK_W
:
491 inst
.Src
[0].Register
.SwizzleW
= src0_swizzle
;
492 inst
.Src
[1].Register
.SwizzleW
= src1_swizzle
;
493 inst
.Src
[2].Register
.SwizzleW
= src2_swizzle
;
499 ctx
->emit_instruction(ctx
, &inst
);
504 tgsi_transform_kill_inst(struct tgsi_transform_context
*ctx
,
507 unsigned src_swizzle
,
510 struct tgsi_full_instruction inst
;
512 inst
= tgsi_default_full_instruction();
513 inst
.Instruction
.Opcode
= TGSI_OPCODE_KILL_IF
;
514 inst
.Instruction
.NumDstRegs
= 0;
515 inst
.Instruction
.NumSrcRegs
= 1;
516 tgsi_transform_src_reg_xyzw(&inst
.Src
[0], src_file
, src_index
);
517 inst
.Src
[0].Register
.SwizzleX
=
518 inst
.Src
[0].Register
.SwizzleY
=
519 inst
.Src
[0].Register
.SwizzleZ
=
520 inst
.Src
[0].Register
.SwizzleW
= src_swizzle
;
521 inst
.Src
[0].Register
.Negate
= negate
;
523 ctx
->emit_instruction(ctx
, &inst
);
528 tgsi_transform_tex_inst(struct tgsi_transform_context
*ctx
,
534 unsigned sampler_index
)
536 struct tgsi_full_instruction inst
;
538 assert(tex_target
< TGSI_TEXTURE_COUNT
);
540 inst
= tgsi_default_full_instruction();
541 inst
.Instruction
.Opcode
= TGSI_OPCODE_TEX
;
542 inst
.Instruction
.NumDstRegs
= 1;
543 inst
.Dst
[0].Register
.File
= dst_file
;
544 inst
.Dst
[0].Register
.Index
= dst_index
;
545 inst
.Instruction
.NumSrcRegs
= 2;
546 inst
.Instruction
.Texture
= TRUE
;
547 inst
.Texture
.Texture
= tex_target
;
548 tgsi_transform_src_reg_xyzw(&inst
.Src
[0], src_file
, src_index
);
549 tgsi_transform_src_reg_xyzw(&inst
.Src
[1], TGSI_FILE_SAMPLER
, sampler_index
);
551 ctx
->emit_instruction(ctx
, &inst
);
556 tgsi_transform_shader(const struct tgsi_token
*tokens_in
,
557 struct tgsi_token
*tokens_out
,
559 struct tgsi_transform_context
*ctx
);
562 #endif /* TGSI_TRANSFORM_H */