1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
31 #include "pipe/p_state.h"
32 #include "util/u_string.h"
33 #include "util/u_memory.h"
34 #include "util/u_inlines.h"
35 #include "tgsi/tgsi_parse.h"
36 #include "tgsi/tgsi_ureg.h"
37 #include "tgsi/tgsi_info.h"
38 #include "tgsi/tgsi_strings.h"
39 #include "tgsi/tgsi_dump.h"
40 #include "tgsi/tgsi_scan.h"
42 #include "freedreno_lowering.h"
44 #include "fd3_compiler.h"
45 #include "fd3_program.h"
48 #include "instr-a3xx.h"
52 struct fd3_compile_context
{
53 const struct tgsi_token
*tokens
;
56 struct ir3_block
*block
;
57 struct fd3_shader_variant
*so
;
59 struct tgsi_parse_context parser
;
62 struct tgsi_shader_info info
;
64 /* last input dst (for setting (ei) flag): */
65 struct ir3_register
*last_input
;
67 /* last instruction with relative addressing: */
68 struct ir3_instruction
*last_rel
;
70 /* for calculating input/output positions/linkages: */
73 unsigned num_internal_temps
;
74 struct tgsi_src_register internal_temps
[6];
76 /* track registers which need to synchronize w/ "complex alu" cat3
77 * instruction pipeline:
81 /* track registers which need to synchronize with texture fetch
86 /* inputs start at r0, temporaries start after last input, and
87 * outputs start after last temporary.
89 * We could be more clever, because this is not a hw restriction,
90 * but probably best just to implement an optimizing pass to
91 * reduce the # of registers used and get rid of redundant mov's
92 * (to output register).
94 unsigned base_reg
[TGSI_FILE_COUNT
];
96 /* idx/slot for last compiler generated immediate */
97 unsigned immediate_idx
;
99 /* stack of branch instructions that start (potentially nested)
100 * branch instructions, so that we can fix up the branch targets
101 * so that we can fix up the branch target on the corresponding
104 struct ir3_instruction
*branch
[16];
105 unsigned int branch_count
;
107 /* used when dst is same as one of the src, to avoid overwriting a
108 * src element before the remaining scalar instructions that make
109 * up the vector operation
111 struct tgsi_dst_register tmp_dst
;
112 struct tgsi_src_register
*tmp_src
;
116 static void vectorize(struct fd3_compile_context
*ctx
,
117 struct ir3_instruction
*instr
, struct tgsi_dst_register
*dst
,
119 static void create_mov(struct fd3_compile_context
*ctx
,
120 struct tgsi_dst_register
*dst
, struct tgsi_src_register
*src
);
123 compile_init(struct fd3_compile_context
*ctx
, struct fd3_shader_variant
*so
,
124 const struct tgsi_token
*tokens
)
126 unsigned ret
, base
= 0;
127 struct tgsi_shader_info
*info
= &ctx
->info
;
128 const struct fd_lowering_config lconfig
= {
129 .color_two_side
= so
->key
.color_two_side
,
146 ctx
->tokens
= fd_transform_lowering(&lconfig
, tokens
, &ctx
->info
);
147 ctx
->free_tokens
= !!ctx
->tokens
;
150 ctx
->tokens
= tokens
;
153 ctx
->block
= ir3_block_create(ctx
->ir
, 0, 0, 0);
155 ctx
->last_input
= NULL
;
156 ctx
->last_rel
= NULL
;
158 ctx
->num_internal_temps
= 0;
159 ctx
->branch_count
= 0;
161 regmask_init(&ctx
->needs_ss
);
162 regmask_init(&ctx
->needs_sy
);
163 memset(ctx
->base_reg
, 0, sizeof(ctx
->base_reg
));
165 /* Immediates go after constants: */
166 ctx
->base_reg
[TGSI_FILE_CONSTANT
] = 0;
167 ctx
->base_reg
[TGSI_FILE_IMMEDIATE
] =
168 info
->file_max
[TGSI_FILE_CONSTANT
] + 1;
170 /* if full precision and fragment shader, don't clobber
171 * r0.x w/ bary fetch:
173 if ((so
->type
== SHADER_FRAGMENT
) && !so
->key
.half_precision
)
176 /* Temporaries after outputs after inputs: */
177 ctx
->base_reg
[TGSI_FILE_INPUT
] = base
;
178 ctx
->base_reg
[TGSI_FILE_OUTPUT
] = base
+
179 info
->file_max
[TGSI_FILE_INPUT
] + 1;
180 ctx
->base_reg
[TGSI_FILE_TEMPORARY
] = base
+
181 info
->file_max
[TGSI_FILE_INPUT
] + 1 +
182 info
->file_max
[TGSI_FILE_OUTPUT
] + 1;
184 so
->first_immediate
= ctx
->base_reg
[TGSI_FILE_IMMEDIATE
];
185 ctx
->immediate_idx
= 4 * (ctx
->info
.file_max
[TGSI_FILE_IMMEDIATE
] + 1);
187 ret
= tgsi_parse_init(&ctx
->parser
, ctx
->tokens
);
188 if (ret
!= TGSI_PARSE_OK
)
191 ctx
->type
= ctx
->parser
.FullHeader
.Processor
.Processor
;
197 compile_error(struct fd3_compile_context
*ctx
, const char *format
, ...)
200 va_start(ap
, format
);
201 _debug_vprintf(format
, ap
);
203 tgsi_dump(ctx
->tokens
, 0);
207 #define compile_assert(ctx, cond) do { \
208 if (!(cond)) compile_error((ctx), "failed assert: "#cond"\n"); \
212 compile_free(struct fd3_compile_context
*ctx
)
214 if (ctx
->free_tokens
)
215 free((void *)ctx
->tokens
);
216 tgsi_parse_free(&ctx
->parser
);
219 struct instr_translater
{
220 void (*fxn
)(const struct instr_translater
*t
,
221 struct fd3_compile_context
*ctx
,
222 struct tgsi_full_instruction
*inst
);
225 opc_t hopc
; /* opc to use for half_precision mode, if different */
230 handle_last_rel(struct fd3_compile_context
*ctx
)
233 ctx
->last_rel
->flags
|= IR3_INSTR_UL
;
234 ctx
->last_rel
= NULL
;
238 static struct ir3_instruction
*
239 instr_create(struct fd3_compile_context
*ctx
, int category
, opc_t opc
)
241 return ir3_instr_create(ctx
->block
, category
, opc
);
245 add_nop(struct fd3_compile_context
*ctx
, unsigned count
)
248 instr_create(ctx
, 0, OPC_NOP
);
252 src_flags(struct fd3_compile_context
*ctx
, struct ir3_register
*reg
)
256 if (reg
->flags
& (IR3_REG_CONST
| IR3_REG_IMMED
))
259 if (regmask_get(&ctx
->needs_ss
, reg
)) {
260 flags
|= IR3_INSTR_SS
;
261 regmask_init(&ctx
->needs_ss
);
264 if (regmask_get(&ctx
->needs_sy
, reg
)) {
265 flags
|= IR3_INSTR_SY
;
266 regmask_init(&ctx
->needs_sy
);
272 static struct ir3_register
*
273 add_dst_reg(struct fd3_compile_context
*ctx
, struct ir3_instruction
*instr
,
274 const struct tgsi_dst_register
*dst
, unsigned chan
)
276 unsigned flags
= 0, num
= 0;
277 struct ir3_register
*reg
;
280 case TGSI_FILE_OUTPUT
:
281 case TGSI_FILE_TEMPORARY
:
282 num
= dst
->Index
+ ctx
->base_reg
[dst
->File
];
284 case TGSI_FILE_ADDRESS
:
288 compile_error(ctx
, "unsupported dst register file: %s\n",
289 tgsi_file_name(dst
->File
));
294 flags
|= IR3_REG_RELATIV
;
295 if (ctx
->so
->key
.half_precision
)
296 flags
|= IR3_REG_HALF
;
298 reg
= ir3_reg_create(instr
, regid(num
, chan
), flags
);
301 ctx
->last_rel
= instr
;
306 static struct ir3_register
*
307 add_src_reg(struct fd3_compile_context
*ctx
, struct ir3_instruction
*instr
,
308 const struct tgsi_src_register
*src
, unsigned chan
)
310 unsigned flags
= 0, num
= 0;
311 struct ir3_register
*reg
;
313 /* TODO we need to use a mov to temp for const >= 64.. or maybe
314 * we could use relative addressing..
316 compile_assert(ctx
, src
->Index
< 64);
319 case TGSI_FILE_IMMEDIATE
:
320 /* TODO if possible, use actual immediate instead of const.. but
321 * TGSI has vec4 immediates, we can only embed scalar (of limited
322 * size, depending on instruction..)
324 case TGSI_FILE_CONSTANT
:
325 flags
|= IR3_REG_CONST
;
326 num
= src
->Index
+ ctx
->base_reg
[src
->File
];
328 case TGSI_FILE_OUTPUT
:
329 /* NOTE: we should only end up w/ OUTPUT file for things like
330 * clamp()'ing saturated dst instructions
332 case TGSI_FILE_INPUT
:
333 case TGSI_FILE_TEMPORARY
:
334 num
= src
->Index
+ ctx
->base_reg
[src
->File
];
337 compile_error(ctx
, "unsupported src register file: %s\n",
338 tgsi_file_name(src
->File
));
343 flags
|= IR3_REG_ABS
;
345 flags
|= IR3_REG_NEGATE
;
347 flags
|= IR3_REG_RELATIV
;
348 if (ctx
->so
->key
.half_precision
)
349 flags
|= IR3_REG_HALF
;
351 reg
= ir3_reg_create(instr
, regid(num
, chan
), flags
);
354 ctx
->last_rel
= instr
;
356 instr
->flags
|= src_flags(ctx
, reg
);
362 src_from_dst(struct tgsi_src_register
*src
, struct tgsi_dst_register
*dst
)
364 src
->File
= dst
->File
;
365 src
->Indirect
= dst
->Indirect
;
366 src
->Dimension
= dst
->Dimension
;
367 src
->Index
= dst
->Index
;
370 src
->SwizzleX
= TGSI_SWIZZLE_X
;
371 src
->SwizzleY
= TGSI_SWIZZLE_Y
;
372 src
->SwizzleZ
= TGSI_SWIZZLE_Z
;
373 src
->SwizzleW
= TGSI_SWIZZLE_W
;
376 /* Get internal-temp src/dst to use for a sequence of instructions
377 * generated by a single TGSI op.
379 static struct tgsi_src_register
*
380 get_internal_temp(struct fd3_compile_context
*ctx
,
381 struct tgsi_dst_register
*tmp_dst
)
383 struct tgsi_src_register
*tmp_src
;
386 tmp_dst
->File
= TGSI_FILE_TEMPORARY
;
387 tmp_dst
->WriteMask
= TGSI_WRITEMASK_XYZW
;
388 tmp_dst
->Indirect
= 0;
389 tmp_dst
->Dimension
= 0;
391 /* assign next temporary: */
392 n
= ctx
->num_internal_temps
++;
393 compile_assert(ctx
, n
< ARRAY_SIZE(ctx
->internal_temps
));
394 tmp_src
= &ctx
->internal_temps
[n
];
396 tmp_dst
->Index
= ctx
->info
.file_max
[TGSI_FILE_TEMPORARY
] + n
+ 1;
398 src_from_dst(tmp_src
, tmp_dst
);
403 /* Get internal half-precision temp src/dst to use for a sequence of
404 * instructions generated by a single TGSI op.
406 static struct tgsi_src_register
*
407 get_internal_temp_hr(struct fd3_compile_context
*ctx
,
408 struct tgsi_dst_register
*tmp_dst
)
410 struct tgsi_src_register
*tmp_src
;
413 if (ctx
->so
->key
.half_precision
)
414 return get_internal_temp(ctx
, tmp_dst
);
416 tmp_dst
->File
= TGSI_FILE_TEMPORARY
;
417 tmp_dst
->WriteMask
= TGSI_WRITEMASK_XYZW
;
418 tmp_dst
->Indirect
= 0;
419 tmp_dst
->Dimension
= 0;
421 /* assign next temporary: */
422 n
= ctx
->num_internal_temps
++;
423 compile_assert(ctx
, n
< ARRAY_SIZE(ctx
->internal_temps
));
424 tmp_src
= &ctx
->internal_temps
[n
];
426 /* just use hr0 because no one else should be using half-
431 src_from_dst(tmp_src
, tmp_dst
);
437 is_const(struct tgsi_src_register
*src
)
439 return (src
->File
== TGSI_FILE_CONSTANT
) ||
440 (src
->File
== TGSI_FILE_IMMEDIATE
);
444 is_relative(struct tgsi_src_register
*src
)
446 return src
->Indirect
;
450 is_rel_or_const(struct tgsi_src_register
*src
)
452 return is_relative(src
) || is_const(src
);
456 get_ftype(struct fd3_compile_context
*ctx
)
458 return ctx
->so
->key
.half_precision
? TYPE_F16
: TYPE_F32
;
462 get_utype(struct fd3_compile_context
*ctx
)
464 return ctx
->so
->key
.half_precision
? TYPE_U16
: TYPE_U32
;
468 src_swiz(struct tgsi_src_register
*src
, int chan
)
471 case 0: return src
->SwizzleX
;
472 case 1: return src
->SwizzleY
;
473 case 2: return src
->SwizzleZ
;
474 case 3: return src
->SwizzleW
;
480 /* for instructions that cannot take a const register as src, if needed
481 * generate a move to temporary gpr:
483 static struct tgsi_src_register
*
484 get_unconst(struct fd3_compile_context
*ctx
, struct tgsi_src_register
*src
)
486 struct tgsi_dst_register tmp_dst
;
487 struct tgsi_src_register
*tmp_src
;
489 compile_assert(ctx
, is_rel_or_const(src
));
491 tmp_src
= get_internal_temp(ctx
, &tmp_dst
);
493 create_mov(ctx
, &tmp_dst
, src
);
499 get_immediate(struct fd3_compile_context
*ctx
,
500 struct tgsi_src_register
*reg
, uint32_t val
)
502 unsigned neg
, swiz
, idx
, i
;
503 /* actually maps 1:1 currently.. not sure if that is safe to rely on: */
504 static const unsigned swiz2tgsi
[] = {
505 TGSI_SWIZZLE_X
, TGSI_SWIZZLE_Y
, TGSI_SWIZZLE_Z
, TGSI_SWIZZLE_W
,
508 for (i
= 0; i
< ctx
->immediate_idx
; i
++) {
512 if (ctx
->so
->immediates
[idx
].val
[swiz
] == val
) {
517 if (ctx
->so
->immediates
[idx
].val
[swiz
] == -val
) {
523 if (i
== ctx
->immediate_idx
) {
524 /* need to generate a new immediate: */
528 ctx
->so
->immediates
[idx
].val
[swiz
] = val
;
529 ctx
->so
->immediates_count
= idx
+ 1;
530 ctx
->immediate_idx
++;
533 reg
->File
= TGSI_FILE_IMMEDIATE
;
539 reg
->SwizzleX
= swiz2tgsi
[swiz
];
540 reg
->SwizzleY
= swiz2tgsi
[swiz
];
541 reg
->SwizzleZ
= swiz2tgsi
[swiz
];
542 reg
->SwizzleW
= swiz2tgsi
[swiz
];
546 create_mov(struct fd3_compile_context
*ctx
, struct tgsi_dst_register
*dst
,
547 struct tgsi_src_register
*src
)
549 type_t type_mov
= get_ftype(ctx
);
552 for (i
= 0; i
< 4; i
++) {
553 /* move to destination: */
554 if (dst
->WriteMask
& (1 << i
)) {
555 struct ir3_instruction
*instr
;
557 if (src
->Absolute
|| src
->Negate
) {
558 /* can't have abs or neg on a mov instr, so use
559 * absneg.f instead to handle these cases:
561 instr
= instr_create(ctx
, 2, OPC_ABSNEG_F
);
563 instr
= instr_create(ctx
, 1, 0);
564 instr
->cat1
.src_type
= type_mov
;
565 instr
->cat1
.dst_type
= type_mov
;
568 add_dst_reg(ctx
, instr
, dst
, i
);
569 add_src_reg(ctx
, instr
, src
, src_swiz(src
, i
));
577 create_clamp(struct fd3_compile_context
*ctx
,
578 struct tgsi_dst_register
*dst
, struct tgsi_src_register
*val
,
579 struct tgsi_src_register
*minval
, struct tgsi_src_register
*maxval
)
581 struct ir3_instruction
*instr
;
583 instr
= instr_create(ctx
, 2, OPC_MAX_F
);
584 vectorize(ctx
, instr
, dst
, 2, val
, 0, minval
, 0);
586 instr
= instr_create(ctx
, 2, OPC_MIN_F
);
587 vectorize(ctx
, instr
, dst
, 2, val
, 0, maxval
, 0);
591 create_clamp_imm(struct fd3_compile_context
*ctx
,
592 struct tgsi_dst_register
*dst
,
593 uint32_t minval
, uint32_t maxval
)
595 struct tgsi_src_register minconst
, maxconst
;
596 struct tgsi_src_register src
;
598 src_from_dst(&src
, dst
);
600 get_immediate(ctx
, &minconst
, minval
);
601 get_immediate(ctx
, &maxconst
, maxval
);
603 create_clamp(ctx
, dst
, &src
, &minconst
, &maxconst
);
606 static struct tgsi_dst_register
*
607 get_dst(struct fd3_compile_context
*ctx
, struct tgsi_full_instruction
*inst
)
609 struct tgsi_dst_register
*dst
= &inst
->Dst
[0].Register
;
611 for (i
= 0; i
< inst
->Instruction
.NumSrcRegs
; i
++) {
612 struct tgsi_src_register
*src
= &inst
->Src
[i
].Register
;
613 if ((src
->File
== dst
->File
) && (src
->Index
== dst
->Index
)) {
614 if ((dst
->WriteMask
== TGSI_WRITEMASK_XYZW
) &&
615 (src
->SwizzleX
== TGSI_SWIZZLE_X
) &&
616 (src
->SwizzleY
== TGSI_SWIZZLE_Y
) &&
617 (src
->SwizzleZ
== TGSI_SWIZZLE_Z
) &&
618 (src
->SwizzleW
== TGSI_SWIZZLE_W
))
620 ctx
->tmp_src
= get_internal_temp(ctx
, &ctx
->tmp_dst
);
621 ctx
->tmp_dst
.WriteMask
= dst
->WriteMask
;
630 put_dst(struct fd3_compile_context
*ctx
, struct tgsi_full_instruction
*inst
,
631 struct tgsi_dst_register
*dst
)
633 /* if necessary, add mov back into original dst: */
634 if (dst
!= &inst
->Dst
[0].Register
) {
635 create_mov(ctx
, &inst
->Dst
[0].Register
, ctx
->tmp_src
);
639 /* helper to generate the necessary repeat and/or additional instructions
640 * to turn a scalar instruction into a vector operation:
643 vectorize(struct fd3_compile_context
*ctx
, struct ir3_instruction
*instr
,
644 struct tgsi_dst_register
*dst
, int nsrcs
, ...)
648 bool indirect
= dst
->Indirect
;
650 add_dst_reg(ctx
, instr
, dst
, TGSI_SWIZZLE_X
);
653 for (j
= 0; j
< nsrcs
; j
++) {
654 struct tgsi_src_register
*src
=
655 va_arg(ap
, struct tgsi_src_register
*);
656 unsigned flags
= va_arg(ap
, unsigned);
657 struct ir3_register
*reg
;
658 if (flags
& IR3_REG_IMMED
) {
659 reg
= ir3_reg_create(instr
, 0, IR3_REG_IMMED
);
660 /* this is an ugly cast.. should have put flags first! */
661 reg
->iim_val
= *(int *)&src
;
663 reg
= add_src_reg(ctx
, instr
, src
, TGSI_SWIZZLE_X
);
664 indirect
|= src
->Indirect
;
666 reg
->flags
|= flags
& ~IR3_REG_NEGATE
;
667 if (flags
& IR3_REG_NEGATE
)
668 reg
->flags
^= IR3_REG_NEGATE
;
672 for (i
= 0; i
< 4; i
++) {
673 if (dst
->WriteMask
& (1 << i
)) {
674 struct ir3_instruction
*cur
;
679 cur
= ir3_instr_clone(instr
);
680 cur
->flags
&= ~(IR3_INSTR_SY
| IR3_INSTR_SS
| IR3_INSTR_JP
);
683 /* fix-up dst register component: */
684 cur
->regs
[0]->num
= regid(cur
->regs
[0]->num
>> 2, i
);
686 /* fix-up src register component: */
688 for (j
= 0; j
< nsrcs
; j
++) {
689 struct tgsi_src_register
*src
=
690 va_arg(ap
, struct tgsi_src_register
*);
691 unsigned flags
= va_arg(ap
, unsigned);
692 if (!(flags
& IR3_REG_IMMED
)) {
693 cur
->regs
[j
+1]->num
=
694 regid(cur
->regs
[j
+1]->num
>> 2,
696 cur
->flags
|= src_flags(ctx
, cur
->regs
[j
+1]);
706 /* pad w/ nop's.. at least until we are clever enough to
707 * figure out if we really need to..
713 * Handlers for TGSI instructions which do not have a 1:1 mapping to
714 * native instructions:
718 trans_clamp(const struct instr_translater
*t
,
719 struct fd3_compile_context
*ctx
,
720 struct tgsi_full_instruction
*inst
)
722 struct tgsi_dst_register
*dst
= get_dst(ctx
, inst
);
723 struct tgsi_src_register
*src0
= &inst
->Src
[0].Register
;
724 struct tgsi_src_register
*src1
= &inst
->Src
[1].Register
;
725 struct tgsi_src_register
*src2
= &inst
->Src
[2].Register
;
727 create_clamp(ctx
, dst
, src0
, src1
, src2
);
729 put_dst(ctx
, inst
, dst
);
732 /* ARL(x) = x, but mova from hrN.x to a0.. */
734 trans_arl(const struct instr_translater
*t
,
735 struct fd3_compile_context
*ctx
,
736 struct tgsi_full_instruction
*inst
)
738 struct ir3_instruction
*instr
;
739 struct tgsi_dst_register tmp_dst
;
740 struct tgsi_src_register
*tmp_src
;
741 struct tgsi_dst_register
*dst
= &inst
->Dst
[0].Register
;
742 struct tgsi_src_register
*src
= &inst
->Src
[0].Register
;
743 unsigned chan
= src
->SwizzleX
;
744 compile_assert(ctx
, dst
->File
== TGSI_FILE_ADDRESS
);
746 handle_last_rel(ctx
);
748 tmp_src
= get_internal_temp_hr(ctx
, &tmp_dst
);
750 /* cov.{f32,f16}s16 Rtmp, Rsrc */
751 instr
= instr_create(ctx
, 1, 0);
752 instr
->cat1
.src_type
= get_ftype(ctx
);
753 instr
->cat1
.dst_type
= TYPE_S16
;
754 add_dst_reg(ctx
, instr
, &tmp_dst
, chan
)->flags
|= IR3_REG_HALF
;
755 add_src_reg(ctx
, instr
, src
, chan
);
759 /* shl.b Rtmp, Rtmp, 2 */
760 instr
= instr_create(ctx
, 2, OPC_SHL_B
);
761 add_dst_reg(ctx
, instr
, &tmp_dst
, chan
)->flags
|= IR3_REG_HALF
;
762 add_src_reg(ctx
, instr
, tmp_src
, chan
)->flags
|= IR3_REG_HALF
;
763 ir3_reg_create(instr
, 0, IR3_REG_IMMED
)->iim_val
= 2;
768 instr
= instr_create(ctx
, 1, 0);
769 instr
->cat1
.src_type
= TYPE_S16
;
770 instr
->cat1
.dst_type
= TYPE_S16
;
771 add_dst_reg(ctx
, instr
, dst
, 0)->flags
|= IR3_REG_HALF
;
772 add_src_reg(ctx
, instr
, tmp_src
, chan
)->flags
|= IR3_REG_HALF
;
774 /* need to ensure 5 instr slots before a0 is used: */
778 /* texture fetch/sample instructions: */
780 trans_samp(const struct instr_translater
*t
,
781 struct fd3_compile_context
*ctx
,
782 struct tgsi_full_instruction
*inst
)
784 struct ir3_register
*r
;
785 struct ir3_instruction
*instr
;
786 struct tgsi_src_register
*coord
= &inst
->Src
[0].Register
;
787 struct tgsi_src_register
*samp
= &inst
->Src
[1].Register
;
788 unsigned tex
= inst
->Texture
.Texture
;
790 unsigned i
, flags
= 0, src_wrmask
;
791 bool needs_mov
= false;
794 case TGSI_OPCODE_TEX
:
795 if (tex
== TGSI_TEXTURE_2D
) {
796 order
= (int8_t[4]){ 0, 1, -1, -1 };
797 src_wrmask
= TGSI_WRITEMASK_XY
;
799 order
= (int8_t[4]){ 0, 1, 2, -1 };
800 src_wrmask
= TGSI_WRITEMASK_XYZ
;
803 case TGSI_OPCODE_TXP
:
804 if (tex
== TGSI_TEXTURE_2D
) {
805 order
= (int8_t[4]){ 0, 1, 3, -1 };
806 src_wrmask
= TGSI_WRITEMASK_XYZ
;
808 order
= (int8_t[4]){ 0, 1, 2, 3 };
809 src_wrmask
= TGSI_WRITEMASK_XYZW
;
811 flags
|= IR3_INSTR_P
;
814 compile_assert(ctx
, 0);
818 if ((tex
== TGSI_TEXTURE_3D
) || (tex
== TGSI_TEXTURE_CUBE
)) {
820 flags
|= IR3_INSTR_3D
;
823 /* cat5 instruction cannot seem to handle const or relative: */
824 if (is_rel_or_const(coord
))
827 /* The texture sample instructions need to coord in successive
828 * registers/components (ie. src.xy but not src.yx). And TXP
829 * needs the .w component in .z for 2D.. so in some cases we
830 * might need to emit some mov instructions to shuffle things
833 for (i
= 1; (i
< 4) && (order
[i
] >= 0) && !needs_mov
; i
++)
834 if (src_swiz(coord
, i
) != (src_swiz(coord
, 0) + order
[i
]))
838 struct tgsi_dst_register tmp_dst
;
839 struct tgsi_src_register
*tmp_src
;
842 type_t type_mov
= get_ftype(ctx
);
844 /* need to move things around: */
845 tmp_src
= get_internal_temp(ctx
, &tmp_dst
);
847 for (j
= 0; (j
< 4) && (order
[j
] >= 0); j
++) {
848 instr
= instr_create(ctx
, 1, 0);
849 instr
->cat1
.src_type
= type_mov
;
850 instr
->cat1
.dst_type
= type_mov
;
851 add_dst_reg(ctx
, instr
, &tmp_dst
, j
);
852 add_src_reg(ctx
, instr
, coord
,
853 src_swiz(coord
, order
[j
]));
861 instr
= instr_create(ctx
, 5, t
->opc
);
862 instr
->cat5
.type
= get_ftype(ctx
);
863 instr
->cat5
.samp
= samp
->Index
;
864 instr
->cat5
.tex
= samp
->Index
;
865 instr
->flags
|= flags
;
867 r
= add_dst_reg(ctx
, instr
, &inst
->Dst
[0].Register
, 0);
868 r
->wrmask
= inst
->Dst
[0].Register
.WriteMask
;
870 add_src_reg(ctx
, instr
, coord
, coord
->SwizzleX
)->wrmask
= src_wrmask
;
872 /* after add_src_reg() so we don't set (sy) on sam instr itself! */
873 regmask_set(&ctx
->needs_sy
, r
);
877 * SEQ(a,b) = (a == b) ? 1.0 : 0.0
878 * cmps.f.eq tmp0, b, a
879 * cov.u16f16 dst, tmp0
881 * SNE(a,b) = (a != b) ? 1.0 : 0.0
882 * cmps.f.eq tmp0, b, a
883 * add.s tmp0, tmp0, -1
884 * sel.f16 dst, {0.0}, tmp0, {1.0}
886 * SGE(a,b) = (a >= b) ? 1.0 : 0.0
887 * cmps.f.ge tmp0, a, b
888 * cov.u16f16 dst, tmp0
890 * SLE(a,b) = (a <= b) ? 1.0 : 0.0
891 * cmps.f.ge tmp0, b, a
892 * cov.u16f16 dst, tmp0
894 * SGT(a,b) = (a > b) ? 1.0 : 0.0
895 * cmps.f.ge tmp0, b, a
896 * add.s tmp0, tmp0, -1
897 * sel.f16 dst, {0.0}, tmp0, {1.0}
899 * SLT(a,b) = (a < b) ? 1.0 : 0.0
900 * cmps.f.ge tmp0, a, b
901 * add.s tmp0, tmp0, -1
902 * sel.f16 dst, {0.0}, tmp0, {1.0}
904 * CMP(a,b,c) = (a < 0.0) ? b : c
905 * cmps.f.ge tmp0, a, {0.0}
906 * add.s tmp0, tmp0, -1
907 * sel.f16 dst, c, tmp0, b
910 trans_cmp(const struct instr_translater
*t
,
911 struct fd3_compile_context
*ctx
,
912 struct tgsi_full_instruction
*inst
)
914 struct ir3_instruction
*instr
;
915 struct tgsi_dst_register tmp_dst
;
916 struct tgsi_src_register
*tmp_src
;
917 struct tgsi_src_register constval0
, constval1
;
918 /* final instruction for CMP() uses orig src1 and src2: */
919 struct tgsi_dst_register
*dst
= get_dst(ctx
, inst
);
920 struct tgsi_src_register
*a0
, *a1
;
923 tmp_src
= get_internal_temp(ctx
, &tmp_dst
);
925 switch (t
->tgsi_opc
) {
926 case TGSI_OPCODE_SEQ
:
927 case TGSI_OPCODE_SNE
:
928 a0
= &inst
->Src
[1].Register
; /* b */
929 a1
= &inst
->Src
[0].Register
; /* a */
930 condition
= IR3_COND_EQ
;
932 case TGSI_OPCODE_SGE
:
933 case TGSI_OPCODE_SLT
:
934 a0
= &inst
->Src
[0].Register
; /* a */
935 a1
= &inst
->Src
[1].Register
; /* b */
936 condition
= IR3_COND_GE
;
938 case TGSI_OPCODE_SLE
:
939 case TGSI_OPCODE_SGT
:
940 a0
= &inst
->Src
[1].Register
; /* b */
941 a1
= &inst
->Src
[0].Register
; /* a */
942 condition
= IR3_COND_GE
;
944 case TGSI_OPCODE_CMP
:
945 get_immediate(ctx
, &constval0
, fui(0.0));
946 a0
= &inst
->Src
[0].Register
; /* a */
947 a1
= &constval0
; /* {0.0} */
948 condition
= IR3_COND_GE
;
951 compile_assert(ctx
, 0);
955 if (is_const(a0
) && is_const(a1
))
956 a0
= get_unconst(ctx
, a0
);
958 /* cmps.f.ge tmp, a0, a1 */
959 instr
= instr_create(ctx
, 2, OPC_CMPS_F
);
960 instr
->cat2
.condition
= condition
;
961 vectorize(ctx
, instr
, &tmp_dst
, 2, a0
, 0, a1
, 0);
963 switch (t
->tgsi_opc
) {
964 case TGSI_OPCODE_SEQ
:
965 case TGSI_OPCODE_SGE
:
966 case TGSI_OPCODE_SLE
:
967 /* cov.u16f16 dst, tmp0 */
968 instr
= instr_create(ctx
, 1, 0);
969 instr
->cat1
.src_type
= get_utype(ctx
);
970 instr
->cat1
.dst_type
= get_ftype(ctx
);
971 vectorize(ctx
, instr
, dst
, 1, tmp_src
, 0);
973 case TGSI_OPCODE_SNE
:
974 case TGSI_OPCODE_SGT
:
975 case TGSI_OPCODE_SLT
:
976 case TGSI_OPCODE_CMP
:
977 /* add.s tmp, tmp, -1 */
978 instr
= instr_create(ctx
, 2, OPC_ADD_S
);
979 vectorize(ctx
, instr
, &tmp_dst
, 2, tmp_src
, 0, -1, IR3_REG_IMMED
);
981 if (t
->tgsi_opc
== TGSI_OPCODE_CMP
) {
982 /* sel.{f32,f16} dst, src2, tmp, src1 */
983 instr
= instr_create(ctx
, 3,
984 ctx
->so
->key
.half_precision
? OPC_SEL_F16
: OPC_SEL_F32
);
985 vectorize(ctx
, instr
, dst
, 3,
986 &inst
->Src
[2].Register
, 0,
988 &inst
->Src
[1].Register
, 0);
990 get_immediate(ctx
, &constval0
, fui(0.0));
991 get_immediate(ctx
, &constval1
, fui(1.0));
992 /* sel.{f32,f16} dst, {0.0}, tmp0, {1.0} */
993 instr
= instr_create(ctx
, 3,
994 ctx
->so
->key
.half_precision
? OPC_SEL_F16
: OPC_SEL_F32
);
995 vectorize(ctx
, instr
, dst
, 3,
996 &constval0
, 0, tmp_src
, 0, &constval1
, 0);
1002 put_dst(ctx
, inst
, dst
);
1006 * Conditional / Flow control
1010 find_instruction(struct fd3_compile_context
*ctx
, struct ir3_instruction
*instr
)
1013 for (i
= 0; i
< ctx
->ir
->instrs_count
; i
++)
1014 if (ctx
->ir
->instrs
[i
] == instr
)
1020 push_branch(struct fd3_compile_context
*ctx
, struct ir3_instruction
*instr
)
1022 ctx
->branch
[ctx
->branch_count
++] = instr
;
1026 pop_branch(struct fd3_compile_context
*ctx
)
1028 struct ir3_instruction
*instr
;
1030 /* if we were clever enough, we'd patch this up after the fact,
1031 * and set (jp) flag on whatever the next instruction was, rather
1032 * than inserting an extra nop..
1034 instr
= instr_create(ctx
, 0, OPC_NOP
);
1035 instr
->flags
|= IR3_INSTR_JP
;
1037 /* pop the branch instruction from the stack and fix up branch target: */
1038 instr
= ctx
->branch
[--ctx
->branch_count
];
1039 instr
->cat0
.immed
= ctx
->ir
->instrs_count
- find_instruction(ctx
, instr
) - 1;
1042 /* We probably don't really want to translate if/else/endif into branches..
1043 * the blob driver evaluates both legs of the if and then uses the sel
1044 * instruction to pick which sides of the branch to "keep".. but figuring
1045 * that out will take somewhat more compiler smarts. So hopefully branches
1046 * don't kill performance too badly.
1049 trans_if(const struct instr_translater
*t
,
1050 struct fd3_compile_context
*ctx
,
1051 struct tgsi_full_instruction
*inst
)
1053 struct ir3_instruction
*instr
;
1054 struct tgsi_src_register
*src
= &inst
->Src
[0].Register
;
1055 struct tgsi_src_register constval
;
1057 get_immediate(ctx
, &constval
, fui(0.0));
1060 src
= get_unconst(ctx
, src
);
1062 instr
= instr_create(ctx
, 2, OPC_CMPS_F
);
1063 ir3_reg_create(instr
, regid(REG_P0
, 0), 0);
1064 add_src_reg(ctx
, instr
, src
, src
->SwizzleX
);
1065 add_src_reg(ctx
, instr
, &constval
, constval
.SwizzleX
);
1066 instr
->cat2
.condition
= IR3_COND_EQ
;
1068 instr
= instr_create(ctx
, 0, OPC_BR
);
1069 push_branch(ctx
, instr
);
1073 trans_else(const struct instr_translater
*t
,
1074 struct fd3_compile_context
*ctx
,
1075 struct tgsi_full_instruction
*inst
)
1077 struct ir3_instruction
*instr
;
1079 /* for first half of if/else/endif, generate a jump past the else: */
1080 instr
= instr_create(ctx
, 0, OPC_JUMP
);
1083 push_branch(ctx
, instr
);
1087 trans_endif(const struct instr_translater
*t
,
1088 struct fd3_compile_context
*ctx
,
1089 struct tgsi_full_instruction
*inst
)
1095 * Handlers for TGSI instructions which do have 1:1 mapping to native
1100 instr_cat0(const struct instr_translater
*t
,
1101 struct fd3_compile_context
*ctx
,
1102 struct tgsi_full_instruction
*inst
)
1104 instr_create(ctx
, 0, t
->opc
);
1108 instr_cat1(const struct instr_translater
*t
,
1109 struct fd3_compile_context
*ctx
,
1110 struct tgsi_full_instruction
*inst
)
1112 struct tgsi_dst_register
*dst
= get_dst(ctx
, inst
);
1113 struct tgsi_src_register
*src
= &inst
->Src
[0].Register
;
1115 /* mov instructions can't handle a negate on src: */
1117 struct tgsi_src_register constval
;
1118 struct ir3_instruction
*instr
;
1120 /* since right now, we are using uniformly either TYPE_F16 or
1121 * TYPE_F32, and we don't utilize the conversion possibilities
1122 * of mov instructions, we can get away with substituting an
1123 * add.f which can handle negate. Might need to revisit this
1124 * in the future if we start supporting widening/narrowing or
1125 * conversion to/from integer..
1127 instr
= instr_create(ctx
, 2, OPC_ADD_F
);
1128 get_immediate(ctx
, &constval
, fui(0.0));
1129 vectorize(ctx
, instr
, dst
, 2, src
, 0, &constval
, 0);
1131 create_mov(ctx
, dst
, src
);
1132 /* create_mov() generates vector sequence, so no vectorize() */
1134 put_dst(ctx
, inst
, dst
);
1138 instr_cat2(const struct instr_translater
*t
,
1139 struct fd3_compile_context
*ctx
,
1140 struct tgsi_full_instruction
*inst
)
1142 struct tgsi_dst_register
*dst
= get_dst(ctx
, inst
);
1143 struct tgsi_src_register
*src0
= &inst
->Src
[0].Register
;
1144 struct tgsi_src_register
*src1
= &inst
->Src
[1].Register
;
1145 struct ir3_instruction
*instr
;
1146 unsigned src0_flags
= 0, src1_flags
= 0;
1148 switch (t
->tgsi_opc
) {
1149 case TGSI_OPCODE_ABS
:
1150 src0_flags
= IR3_REG_ABS
;
1152 case TGSI_OPCODE_SUB
:
1153 src1_flags
= IR3_REG_NEGATE
;
1172 /* these only have one src reg */
1173 instr
= instr_create(ctx
, 2, t
->opc
);
1174 vectorize(ctx
, instr
, dst
, 1, src0
, src0_flags
);
1177 if (is_const(src0
) && is_const(src1
))
1178 src0
= get_unconst(ctx
, src0
);
1180 instr
= instr_create(ctx
, 2, t
->opc
);
1181 vectorize(ctx
, instr
, dst
, 2, src0
, src0_flags
,
1186 put_dst(ctx
, inst
, dst
);
1190 instr_cat3(const struct instr_translater
*t
,
1191 struct fd3_compile_context
*ctx
,
1192 struct tgsi_full_instruction
*inst
)
1194 struct tgsi_dst_register
*dst
= get_dst(ctx
, inst
);
1195 struct tgsi_src_register
*src0
= &inst
->Src
[0].Register
;
1196 struct tgsi_src_register
*src1
= &inst
->Src
[1].Register
;
1197 struct ir3_instruction
*instr
;
1199 /* in particular, can't handle const for src1 for cat3..
1200 * for mad, we can swap first two src's if needed:
1202 if (is_rel_or_const(src1
)) {
1203 if (is_mad(t
->opc
) && !is_rel_or_const(src0
)) {
1204 struct tgsi_src_register
*tmp
;
1209 src1
= get_unconst(ctx
, src1
);
1213 instr
= instr_create(ctx
, 3,
1214 ctx
->so
->key
.half_precision
? t
->hopc
: t
->opc
);
1215 vectorize(ctx
, instr
, dst
, 3, src0
, 0, src1
, 0,
1216 &inst
->Src
[2].Register
, 0);
1217 put_dst(ctx
, inst
, dst
);
1221 instr_cat4(const struct instr_translater
*t
,
1222 struct fd3_compile_context
*ctx
,
1223 struct tgsi_full_instruction
*inst
)
1225 struct tgsi_dst_register
*dst
= get_dst(ctx
, inst
);
1226 struct tgsi_src_register
*src
= &inst
->Src
[0].Register
;
1227 struct ir3_instruction
*instr
;
1230 /* seems like blob compiler avoids const as src.. */
1232 src
= get_unconst(ctx
, src
);
1237 /* we need to replicate into each component: */
1238 for (i
= 0, n
= 0; i
< 4; i
++) {
1239 if (dst
->WriteMask
& (1 << i
)) {
1242 instr
= instr_create(ctx
, 4, t
->opc
);
1243 add_dst_reg(ctx
, instr
, dst
, i
);
1244 add_src_reg(ctx
, instr
, src
, src
->SwizzleX
);
1248 regmask_set(&ctx
->needs_ss
, instr
->regs
[0]);
1249 put_dst(ctx
, inst
, dst
);
1252 static const struct instr_translater translaters
[TGSI_OPCODE_LAST
] = {
1253 #define INSTR(n, f, ...) \
1254 [TGSI_OPCODE_ ## n] = { .fxn = (f), .tgsi_opc = TGSI_OPCODE_ ## n, ##__VA_ARGS__ }
1256 INSTR(MOV
, instr_cat1
),
1257 INSTR(RCP
, instr_cat4
, .opc
= OPC_RCP
),
1258 INSTR(RSQ
, instr_cat4
, .opc
= OPC_RSQ
),
1259 INSTR(SQRT
, instr_cat4
, .opc
= OPC_SQRT
),
1260 INSTR(MUL
, instr_cat2
, .opc
= OPC_MUL_F
),
1261 INSTR(ADD
, instr_cat2
, .opc
= OPC_ADD_F
),
1262 INSTR(SUB
, instr_cat2
, .opc
= OPC_ADD_F
),
1263 INSTR(MIN
, instr_cat2
, .opc
= OPC_MIN_F
),
1264 INSTR(MAX
, instr_cat2
, .opc
= OPC_MAX_F
),
1265 INSTR(MAD
, instr_cat3
, .opc
= OPC_MAD_F32
, .hopc
= OPC_MAD_F16
),
1266 INSTR(TRUNC
, instr_cat2
, .opc
= OPC_TRUNC_F
),
1267 INSTR(CLAMP
, trans_clamp
),
1268 INSTR(FLR
, instr_cat2
, .opc
= OPC_FLOOR_F
),
1269 INSTR(ROUND
, instr_cat2
, .opc
= OPC_RNDNE_F
),
1270 INSTR(SSG
, instr_cat2
, .opc
= OPC_SIGN_F
),
1271 INSTR(ARL
, trans_arl
),
1272 INSTR(EX2
, instr_cat4
, .opc
= OPC_EXP2
),
1273 INSTR(LG2
, instr_cat4
, .opc
= OPC_LOG2
),
1274 INSTR(ABS
, instr_cat2
, .opc
= OPC_ABSNEG_F
),
1275 INSTR(COS
, instr_cat4
, .opc
= OPC_COS
),
1276 INSTR(SIN
, instr_cat4
, .opc
= OPC_SIN
),
1277 INSTR(TEX
, trans_samp
, .opc
= OPC_SAM
, .arg
= TGSI_OPCODE_TEX
),
1278 INSTR(TXP
, trans_samp
, .opc
= OPC_SAM
, .arg
= TGSI_OPCODE_TXP
),
1279 INSTR(SGT
, trans_cmp
),
1280 INSTR(SLT
, trans_cmp
),
1281 INSTR(SGE
, trans_cmp
),
1282 INSTR(SLE
, trans_cmp
),
1283 INSTR(SNE
, trans_cmp
),
1284 INSTR(SEQ
, trans_cmp
),
1285 INSTR(CMP
, trans_cmp
),
1286 INSTR(IF
, trans_if
),
1287 INSTR(ELSE
, trans_else
),
1288 INSTR(ENDIF
, trans_endif
),
1289 INSTR(END
, instr_cat0
, .opc
= OPC_END
),
1290 INSTR(KILL
, instr_cat0
, .opc
= OPC_KILL
),
1294 decl_semantic(const struct tgsi_declaration_semantic
*sem
)
1296 return fd3_semantic_name(sem
->Name
, sem
->Index
);
1300 decl_in(struct fd3_compile_context
*ctx
, struct tgsi_full_declaration
*decl
)
1302 struct fd3_shader_variant
*so
= ctx
->so
;
1303 unsigned base
= ctx
->base_reg
[TGSI_FILE_INPUT
];
1304 unsigned i
, flags
= 0;
1307 /* I don't think we should get frag shader input without
1308 * semantic info? Otherwise how do inputs get linked to
1311 compile_assert(ctx
, (ctx
->type
== TGSI_PROCESSOR_VERTEX
) ||
1312 decl
->Declaration
.Semantic
);
1314 if (ctx
->so
->key
.half_precision
)
1315 flags
|= IR3_REG_HALF
;
1317 for (i
= decl
->Range
.First
; i
<= decl
->Range
.Last
; i
++) {
1318 unsigned n
= so
->inputs_count
++;
1319 unsigned r
= regid(i
+ base
, 0);
1322 /* TODO use ctx->info.input_usage_mask[decl->Range.n] to figure out ncomp: */
1325 DBG("decl in -> r%d", i
+ base
); // XXX
1327 compile_assert(ctx
, n
< ARRAY_SIZE(so
->inputs
));
1329 so
->inputs
[n
].semantic
= decl_semantic(&decl
->Semantic
);
1330 so
->inputs
[n
].compmask
= (1 << ncomp
) - 1;
1331 so
->inputs
[n
].ncomp
= ncomp
;
1332 so
->inputs
[n
].regid
= r
;
1333 so
->inputs
[n
].inloc
= ctx
->next_inloc
;
1334 so
->inputs
[n
].bary
= true; /* all that is supported */
1335 ctx
->next_inloc
+= ncomp
;
1337 so
->total_in
+= ncomp
;
1339 /* for frag shaders, we need to generate the corresponding bary instr: */
1340 if (ctx
->type
== TGSI_PROCESSOR_FRAGMENT
) {
1343 for (j
= 0; j
< ncomp
; j
++) {
1344 struct ir3_instruction
*instr
;
1345 struct ir3_register
*dst
;
1347 instr
= instr_create(ctx
, 2, OPC_BARY_F
);
1350 dst
= ir3_reg_create(instr
, r
+ j
, flags
);
1351 ctx
->last_input
= dst
;
1353 /* input position: */
1354 ir3_reg_create(instr
, 0, IR3_REG_IMMED
)->iim_val
=
1355 so
->inputs
[n
].inloc
+ j
- 8;
1357 /* input base (always r0.xy): */
1358 ir3_reg_create(instr
, regid(0,0), 0)->wrmask
= 0x3;
1369 decl_out(struct fd3_compile_context
*ctx
, struct tgsi_full_declaration
*decl
)
1371 struct fd3_shader_variant
*so
= ctx
->so
;
1372 unsigned base
= ctx
->base_reg
[TGSI_FILE_OUTPUT
];
1374 unsigned name
= decl
->Semantic
.Name
;
1377 compile_assert(ctx
, decl
->Declaration
.Semantic
); // TODO is this ever not true?
1379 DBG("decl out[%d] -> r%d", name
, decl
->Range
.First
+ base
); // XXX
1381 if (ctx
->type
== TGSI_PROCESSOR_VERTEX
) {
1383 case TGSI_SEMANTIC_POSITION
:
1384 so
->writes_pos
= true;
1386 case TGSI_SEMANTIC_PSIZE
:
1387 so
->writes_psize
= true;
1389 case TGSI_SEMANTIC_COLOR
:
1390 case TGSI_SEMANTIC_BCOLOR
:
1391 case TGSI_SEMANTIC_GENERIC
:
1392 case TGSI_SEMANTIC_FOG
:
1393 case TGSI_SEMANTIC_TEXCOORD
:
1396 compile_error(ctx
, "unknown VS semantic name: %s\n",
1397 tgsi_semantic_names
[name
]);
1401 case TGSI_SEMANTIC_POSITION
:
1402 comp
= 2; /* tgsi will write to .z component */
1403 so
->writes_pos
= true;
1405 case TGSI_SEMANTIC_COLOR
:
1408 compile_error(ctx
, "unknown FS semantic name: %s\n",
1409 tgsi_semantic_names
[name
]);
1413 for (i
= decl
->Range
.First
; i
<= decl
->Range
.Last
; i
++) {
1414 unsigned n
= so
->outputs_count
++;
1415 compile_assert(ctx
, n
< ARRAY_SIZE(so
->outputs
));
1416 so
->outputs
[n
].semantic
= decl_semantic(&decl
->Semantic
);
1417 so
->outputs
[n
].regid
= regid(i
+ base
, comp
);
1422 decl_samp(struct fd3_compile_context
*ctx
, struct tgsi_full_declaration
*decl
)
1424 ctx
->so
->has_samp
= true;
1428 compile_instructions(struct fd3_compile_context
*ctx
)
1430 struct ir3
*ir
= ctx
->ir
;
1433 while (!tgsi_parse_end_of_tokens(&ctx
->parser
)) {
1434 tgsi_parse_token(&ctx
->parser
);
1436 switch (ctx
->parser
.FullToken
.Token
.Type
) {
1437 case TGSI_TOKEN_TYPE_DECLARATION
: {
1438 struct tgsi_full_declaration
*decl
=
1439 &ctx
->parser
.FullToken
.FullDeclaration
;
1440 if (decl
->Declaration
.File
== TGSI_FILE_OUTPUT
) {
1441 decl_out(ctx
, decl
);
1442 } else if (decl
->Declaration
.File
== TGSI_FILE_INPUT
) {
1443 nop
= decl_in(ctx
, decl
);
1444 } else if (decl
->Declaration
.File
== TGSI_FILE_SAMPLER
) {
1445 decl_samp(ctx
, decl
);
1449 case TGSI_TOKEN_TYPE_IMMEDIATE
: {
1450 /* TODO: if we know the immediate is small enough, and only
1451 * used with instructions that can embed an immediate, we
1454 struct tgsi_full_immediate
*imm
=
1455 &ctx
->parser
.FullToken
.FullImmediate
;
1456 unsigned n
= ctx
->so
->immediates_count
++;
1457 memcpy(ctx
->so
->immediates
[n
].val
, imm
->u
, 16);
1460 case TGSI_TOKEN_TYPE_INSTRUCTION
: {
1461 struct tgsi_full_instruction
*inst
=
1462 &ctx
->parser
.FullToken
.FullInstruction
;
1463 unsigned opc
= inst
->Instruction
.Opcode
;
1464 const struct instr_translater
*t
= &translaters
[opc
];
1470 t
->fxn(t
, ctx
, inst
);
1471 ctx
->num_internal_temps
= 0;
1473 compile_error(ctx
, "unknown TGSI opc: %s\n",
1474 tgsi_get_opcode_name(opc
));
1477 switch (inst
->Instruction
.Saturate
) {
1478 case TGSI_SAT_ZERO_ONE
:
1479 create_clamp_imm(ctx
, &inst
->Dst
[0].Register
,
1480 fui(0.0), fui(1.0));
1482 case TGSI_SAT_MINUS_PLUS_ONE
:
1483 create_clamp_imm(ctx
, &inst
->Dst
[0].Register
,
1484 fui(-1.0), fui(1.0));
1495 if (ir
->instrs_count
> 0)
1496 ir
->instrs
[0]->flags
|= IR3_INSTR_SS
| IR3_INSTR_SY
;
1498 if (ctx
->last_input
)
1499 ctx
->last_input
->flags
|= IR3_REG_EI
;
1501 handle_last_rel(ctx
);
1505 fd3_compile_shader_old(struct fd3_shader_variant
*so
,
1506 const struct tgsi_token
*tokens
, struct fd3_shader_key key
)
1508 struct fd3_compile_context ctx
;
1512 so
->ir
= ir3_create();
1516 if (compile_init(&ctx
, so
, tokens
) != TGSI_PARSE_OK
)
1519 compile_instructions(&ctx
);