2 * Copyright (c) 2014 Scott Mansell
3 * Copyright © 2014 Broadcom
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "pipe/p_state.h"
27 #include "util/u_format.h"
28 #include "util/u_hash_table.h"
29 #include "util/u_hash.h"
30 #include "util/u_memory.h"
31 #include "util/ralloc.h"
32 #include "tgsi/tgsi_dump.h"
33 #include "tgsi/tgsi_info.h"
35 #include "vc4_context.h"
38 #ifdef USE_VC4_SIMULATOR
39 #include "simpenrose/simpenrose.h"
43 struct pipe_shader_state
*shader_state
;
45 enum pipe_format format
;
46 unsigned compare_mode
:1;
47 unsigned compare_func
:3;
49 } tex
[VC4_MAX_TEXTURE_SAMPLERS
];
54 enum pipe_format color_format
;
58 bool stencil_full_writemasks
;
62 struct pipe_rt_blend_state blend
;
67 enum pipe_format attr_formats
[8];
71 resize_qreg_array(struct vc4_compile
*c
,
76 if (*size
>= decl_size
)
79 uint32_t old_size
= *size
;
80 *size
= MAX2(*size
* 2, decl_size
);
81 *regs
= reralloc(c
, *regs
, struct qreg
, *size
);
83 fprintf(stderr
, "Malloc failure\n");
87 for (uint32_t i
= old_size
; i
< *size
; i
++)
88 (*regs
)[i
] = c
->undef
;
92 add_uniform(struct vc4_compile
*c
,
93 enum quniform_contents contents
,
96 uint32_t uniform
= c
->num_uniforms
++;
97 struct qreg u
= { QFILE_UNIF
, uniform
};
99 c
->uniform_contents
[uniform
] = contents
;
100 c
->uniform_data
[uniform
] = data
;
106 get_temp_for_uniform(struct vc4_compile
*c
, enum quniform_contents contents
,
109 for (int i
= 0; i
< c
->num_uniforms
; i
++) {
110 if (c
->uniform_contents
[i
] == contents
&&
111 c
->uniform_data
[i
] == data
)
112 return c
->uniforms
[i
];
115 struct qreg u
= add_uniform(c
, contents
, data
);
116 struct qreg t
= qir_MOV(c
, u
);
118 resize_qreg_array(c
, &c
->uniforms
, &c
->uniforms_array_size
,
121 c
->uniforms
[u
.index
] = t
;
126 qir_uniform_ui(struct vc4_compile
*c
, uint32_t ui
)
128 return get_temp_for_uniform(c
, QUNIFORM_CONSTANT
, ui
);
132 qir_uniform_f(struct vc4_compile
*c
, float f
)
134 return qir_uniform_ui(c
, fui(f
));
138 get_src(struct vc4_compile
*c
, unsigned tgsi_op
,
139 struct tgsi_src_register
*src
, int i
)
141 struct qreg r
= c
->undef
;
161 assert(!src
->Indirect
);
166 case TGSI_FILE_TEMPORARY
:
167 r
= c
->temps
[src
->Index
* 4 + s
];
169 case TGSI_FILE_IMMEDIATE
:
170 r
= c
->consts
[src
->Index
* 4 + s
];
172 case TGSI_FILE_CONSTANT
:
173 r
= get_temp_for_uniform(c
, QUNIFORM_UNIFORM
,
176 case TGSI_FILE_INPUT
:
177 r
= c
->inputs
[src
->Index
* 4 + s
];
179 case TGSI_FILE_SAMPLER
:
180 case TGSI_FILE_SAMPLER_VIEW
:
184 fprintf(stderr
, "unknown src file %d\n", src
->File
);
189 r
= qir_FMAXABS(c
, r
, r
);
192 switch (tgsi_opcode_infer_src_type(tgsi_op
)) {
193 case TGSI_TYPE_SIGNED
:
194 case TGSI_TYPE_UNSIGNED
:
195 r
= qir_SUB(c
, qir_uniform_ui(c
, 0), r
);
198 r
= qir_FSUB(c
, qir_uniform_f(c
, 0.0), r
);
208 update_dst(struct vc4_compile
*c
, struct tgsi_full_instruction
*tgsi_inst
,
209 int i
, struct qreg val
)
211 struct tgsi_dst_register
*tgsi_dst
= &tgsi_inst
->Dst
[0].Register
;
213 assert(!tgsi_dst
->Indirect
);
215 switch (tgsi_dst
->File
) {
216 case TGSI_FILE_TEMPORARY
:
217 c
->temps
[tgsi_dst
->Index
* 4 + i
] = val
;
219 case TGSI_FILE_OUTPUT
:
220 c
->outputs
[tgsi_dst
->Index
* 4 + i
] = val
;
221 c
->num_outputs
= MAX2(c
->num_outputs
,
222 tgsi_dst
->Index
* 4 + i
+ 1);
225 fprintf(stderr
, "unknown dst file %d\n", tgsi_dst
->File
);
231 get_swizzled_channel(struct vc4_compile
*c
,
232 struct qreg
*srcs
, int swiz
)
236 case UTIL_FORMAT_SWIZZLE_NONE
:
237 fprintf(stderr
, "warning: unknown swizzle\n");
239 case UTIL_FORMAT_SWIZZLE_0
:
240 return qir_uniform_f(c
, 0.0);
241 case UTIL_FORMAT_SWIZZLE_1
:
242 return qir_uniform_f(c
, 1.0);
243 case UTIL_FORMAT_SWIZZLE_X
:
244 case UTIL_FORMAT_SWIZZLE_Y
:
245 case UTIL_FORMAT_SWIZZLE_Z
:
246 case UTIL_FORMAT_SWIZZLE_W
:
252 tgsi_to_qir_alu(struct vc4_compile
*c
,
253 struct tgsi_full_instruction
*tgsi_inst
,
254 enum qop op
, struct qreg
*src
, int i
)
256 struct qreg dst
= qir_get_temp(c
);
257 qir_emit(c
, qir_inst4(op
, dst
,
266 tgsi_to_qir_umul(struct vc4_compile
*c
,
267 struct tgsi_full_instruction
*tgsi_inst
,
268 enum qop op
, struct qreg
*src
, int i
)
270 struct qreg src0_hi
= qir_SHR(c
, src
[0 * 4 + i
],
271 qir_uniform_ui(c
, 16));
272 struct qreg src0_lo
= qir_AND(c
, src
[0 * 4 + i
],
273 qir_uniform_ui(c
, 0xffff));
274 struct qreg src1_hi
= qir_SHR(c
, src
[1 * 4 + i
],
275 qir_uniform_ui(c
, 16));
276 struct qreg src1_lo
= qir_AND(c
, src
[1 * 4 + i
],
277 qir_uniform_ui(c
, 0xffff));
279 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1_lo
);
280 struct qreg lohi
= qir_MUL24(c
, src0_lo
, src1_hi
);
281 struct qreg lolo
= qir_MUL24(c
, src0_lo
, src1_lo
);
283 return qir_ADD(c
, lolo
, qir_SHL(c
,
284 qir_ADD(c
, hilo
, lohi
),
285 qir_uniform_ui(c
, 16)));
289 tgsi_to_qir_idiv(struct vc4_compile
*c
,
290 struct tgsi_full_instruction
*tgsi_inst
,
291 enum qop op
, struct qreg
*src
, int i
)
293 return qir_FTOI(c
, qir_FMUL(c
,
294 qir_ITOF(c
, src
[0 * 4 + i
]),
295 qir_RCP(c
, qir_ITOF(c
, src
[1 * 4 + i
]))));
299 tgsi_to_qir_ineg(struct vc4_compile
*c
,
300 struct tgsi_full_instruction
*tgsi_inst
,
301 enum qop op
, struct qreg
*src
, int i
)
303 return qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0 * 4 + i
]);
307 tgsi_to_qir_seq(struct vc4_compile
*c
,
308 struct tgsi_full_instruction
*tgsi_inst
,
309 enum qop op
, struct qreg
*src
, int i
)
311 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
312 return qir_SEL_X_0_ZS(c
, qir_uniform_f(c
, 1.0));
316 tgsi_to_qir_sne(struct vc4_compile
*c
,
317 struct tgsi_full_instruction
*tgsi_inst
,
318 enum qop op
, struct qreg
*src
, int i
)
320 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
321 return qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0));
325 tgsi_to_qir_slt(struct vc4_compile
*c
,
326 struct tgsi_full_instruction
*tgsi_inst
,
327 enum qop op
, struct qreg
*src
, int i
)
329 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
330 return qir_SEL_X_0_NS(c
, qir_uniform_f(c
, 1.0));
334 tgsi_to_qir_sge(struct vc4_compile
*c
,
335 struct tgsi_full_instruction
*tgsi_inst
,
336 enum qop op
, struct qreg
*src
, int i
)
338 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
339 return qir_SEL_X_0_NC(c
, qir_uniform_f(c
, 1.0));
343 tgsi_to_qir_fseq(struct vc4_compile
*c
,
344 struct tgsi_full_instruction
*tgsi_inst
,
345 enum qop op
, struct qreg
*src
, int i
)
347 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
348 return qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
352 tgsi_to_qir_fsne(struct vc4_compile
*c
,
353 struct tgsi_full_instruction
*tgsi_inst
,
354 enum qop op
, struct qreg
*src
, int i
)
356 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
357 return qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
361 tgsi_to_qir_fslt(struct vc4_compile
*c
,
362 struct tgsi_full_instruction
*tgsi_inst
,
363 enum qop op
, struct qreg
*src
, int i
)
365 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
366 return qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
370 tgsi_to_qir_fsge(struct vc4_compile
*c
,
371 struct tgsi_full_instruction
*tgsi_inst
,
372 enum qop op
, struct qreg
*src
, int i
)
374 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
375 return qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
379 tgsi_to_qir_useq(struct vc4_compile
*c
,
380 struct tgsi_full_instruction
*tgsi_inst
,
381 enum qop op
, struct qreg
*src
, int i
)
383 qir_SF(c
, qir_SUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
384 return qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
388 tgsi_to_qir_usne(struct vc4_compile
*c
,
389 struct tgsi_full_instruction
*tgsi_inst
,
390 enum qop op
, struct qreg
*src
, int i
)
392 qir_SF(c
, qir_SUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
393 return qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
397 tgsi_to_qir_islt(struct vc4_compile
*c
,
398 struct tgsi_full_instruction
*tgsi_inst
,
399 enum qop op
, struct qreg
*src
, int i
)
401 qir_SF(c
, qir_SUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
402 return qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
406 tgsi_to_qir_isge(struct vc4_compile
*c
,
407 struct tgsi_full_instruction
*tgsi_inst
,
408 enum qop op
, struct qreg
*src
, int i
)
410 qir_SF(c
, qir_SUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
411 return qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
415 tgsi_to_qir_cmp(struct vc4_compile
*c
,
416 struct tgsi_full_instruction
*tgsi_inst
,
417 enum qop op
, struct qreg
*src
, int i
)
419 qir_SF(c
, src
[0 * 4 + i
]);
420 return qir_SEL_X_Y_NS(c
,
426 tgsi_to_qir_mad(struct vc4_compile
*c
,
427 struct tgsi_full_instruction
*tgsi_inst
,
428 enum qop op
, struct qreg
*src
, int i
)
438 tgsi_to_qir_lit(struct vc4_compile
*c
,
439 struct tgsi_full_instruction
*tgsi_inst
,
440 enum qop op
, struct qreg
*src
, int i
)
442 struct qreg x
= src
[0 * 4 + 0];
443 struct qreg y
= src
[0 * 4 + 1];
444 struct qreg w
= src
[0 * 4 + 3];
449 return qir_uniform_f(c
, 1.0);
451 return qir_FMAX(c
, src
[0 * 4 + 0], qir_uniform_f(c
, 0.0));
453 struct qreg zero
= qir_uniform_f(c
, 0.0);
456 /* XXX: Clamp w to -128..128 */
457 return qir_SEL_X_0_NC(c
,
458 qir_EXP2(c
, qir_FMUL(c
,
466 assert(!"not reached");
472 tgsi_to_qir_lrp(struct vc4_compile
*c
,
473 struct tgsi_full_instruction
*tgsi_inst
,
474 enum qop op
, struct qreg
*src
, int i
)
476 struct qreg src0
= src
[0 * 4 + i
];
477 struct qreg src1
= src
[1 * 4 + i
];
478 struct qreg src2
= src
[2 * 4 + i
];
481 * src0 * src1 + (1 - src0) * src2.
482 * -> src0 * src1 + src2 - src0 * src2
483 * -> src2 + src0 * (src1 - src2)
485 return qir_FADD(c
, src2
, qir_FMUL(c
, src0
, qir_FSUB(c
, src1
, src2
)));
490 tgsi_to_qir_tex(struct vc4_compile
*c
,
491 struct tgsi_full_instruction
*tgsi_inst
,
492 enum qop op
, struct qreg
*src
)
494 assert(!tgsi_inst
->Instruction
.Saturate
);
496 struct qreg s
= src
[0 * 4 + 0];
497 struct qreg t
= src
[0 * 4 + 1];
498 uint32_t unit
= tgsi_inst
->Src
[1].Register
.Index
;
500 struct qreg proj
= c
->undef
;
501 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXP
) {
502 proj
= qir_RCP(c
, src
[0 * 4 + 3]);
503 s
= qir_FMUL(c
, s
, proj
);
504 t
= qir_FMUL(c
, t
, proj
);
507 /* There is no native support for GL texture rectangle coordinates, so
508 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
511 if (tgsi_inst
->Texture
.Texture
== TGSI_TEXTURE_RECT
||
512 tgsi_inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWRECT
) {
514 get_temp_for_uniform(c
,
515 QUNIFORM_TEXRECT_SCALE_X
,
518 get_temp_for_uniform(c
,
519 QUNIFORM_TEXRECT_SCALE_Y
,
523 qir_TEX_T(c
, t
, add_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
));
525 struct qreg sampler_p1
= add_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
,
527 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB
) {
528 qir_TEX_B(c
, src
[0 * 4 + 3], sampler_p1
);
529 qir_TEX_S(c
, s
, add_uniform(c
, QUNIFORM_CONSTANT
, 0));
531 qir_TEX_S(c
, s
, sampler_p1
);
534 c
->num_texture_samples
++;
535 struct qreg r4
= qir_TEX_RESULT(c
);
537 enum pipe_format format
= c
->key
->tex
[unit
].format
;
539 struct qreg unpacked
[4];
540 if (util_format_is_depth_or_stencil(format
)) {
541 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, r4
,
542 qir_uniform_ui(c
, 8)));
543 struct qreg normalized
= qir_FMUL(c
, depthf
,
544 qir_uniform_f(c
, 1.0f
/0xffffff));
546 struct qreg depth_output
;
548 struct qreg compare
= src
[0 * 4 + 2];
550 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXP
)
551 compare
= qir_FMUL(c
, compare
, proj
);
553 struct qreg one
= qir_uniform_f(c
, 1.0f
);
554 if (c
->key
->tex
[unit
].compare_mode
) {
555 switch (c
->key
->tex
[unit
].compare_func
) {
556 case PIPE_FUNC_NEVER
:
557 depth_output
= qir_uniform_f(c
, 0.0f
);
559 case PIPE_FUNC_ALWAYS
:
562 case PIPE_FUNC_EQUAL
:
563 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
564 depth_output
= qir_SEL_X_0_ZS(c
, one
);
566 case PIPE_FUNC_NOTEQUAL
:
567 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
568 depth_output
= qir_SEL_X_0_ZC(c
, one
);
570 case PIPE_FUNC_GREATER
:
571 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
572 depth_output
= qir_SEL_X_0_NC(c
, one
);
574 case PIPE_FUNC_GEQUAL
:
575 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
576 depth_output
= qir_SEL_X_0_NS(c
, one
);
579 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
580 depth_output
= qir_SEL_X_0_NS(c
, one
);
582 case PIPE_FUNC_LEQUAL
:
583 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
584 depth_output
= qir_SEL_X_0_NC(c
, one
);
588 depth_output
= normalized
;
591 for (int i
= 0; i
< 4; i
++)
592 unpacked
[i
] = depth_output
;
594 for (int i
= 0; i
< 4; i
++)
595 unpacked
[i
] = qir_R4_UNPACK(c
, r4
, i
);
598 const uint8_t *format_swiz
= vc4_get_format_swizzle(format
);
600 util_format_compose_swizzles(format_swiz
, c
->key
->tex
[unit
].swizzle
, swiz
);
601 for (int i
= 0; i
< 4; i
++) {
602 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
605 update_dst(c
, tgsi_inst
, i
,
606 get_swizzled_channel(c
, unpacked
, swiz
[i
]));
611 tgsi_to_qir_pow(struct vc4_compile
*c
,
612 struct tgsi_full_instruction
*tgsi_inst
,
613 enum qop op
, struct qreg
*src
, int i
)
615 /* Note that this instruction replicates its result from the x channel
617 return qir_EXP2(c
, qir_FMUL(c
,
619 qir_LOG2(c
, src
[0 * 4 + 0])));
623 tgsi_to_qir_trunc(struct vc4_compile
*c
,
624 struct tgsi_full_instruction
*tgsi_inst
,
625 enum qop op
, struct qreg
*src
, int i
)
627 return qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
631 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
635 tgsi_to_qir_frc(struct vc4_compile
*c
,
636 struct tgsi_full_instruction
*tgsi_inst
,
637 enum qop op
, struct qreg
*src
, int i
)
639 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
640 struct qreg diff
= qir_FSUB(c
, src
[0 * 4 + i
], trunc
);
642 return qir_SEL_X_Y_NS(c
,
643 qir_FADD(c
, diff
, qir_uniform_f(c
, 1.0)),
648 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
652 tgsi_to_qir_flr(struct vc4_compile
*c
,
653 struct tgsi_full_instruction
*tgsi_inst
,
654 enum qop op
, struct qreg
*src
, int i
)
656 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
658 /* This will be < 0 if we truncated and the truncation was of a value
659 * that was < 0 in the first place.
661 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], trunc
));
663 return qir_SEL_X_Y_NS(c
,
664 qir_FSUB(c
, trunc
, qir_uniform_f(c
, 1.0)),
669 tgsi_to_qir_dp(struct vc4_compile
*c
,
670 struct tgsi_full_instruction
*tgsi_inst
,
671 int num
, struct qreg
*src
, int i
)
673 struct qreg sum
= qir_FMUL(c
, src
[0 * 4 + 0], src
[1 * 4 + 0]);
674 for (int j
= 1; j
< num
; j
++) {
675 sum
= qir_FADD(c
, sum
, qir_FMUL(c
,
683 tgsi_to_qir_dp2(struct vc4_compile
*c
,
684 struct tgsi_full_instruction
*tgsi_inst
,
685 enum qop op
, struct qreg
*src
, int i
)
687 return tgsi_to_qir_dp(c
, tgsi_inst
, 2, src
, i
);
691 tgsi_to_qir_dp3(struct vc4_compile
*c
,
692 struct tgsi_full_instruction
*tgsi_inst
,
693 enum qop op
, struct qreg
*src
, int i
)
695 return tgsi_to_qir_dp(c
, tgsi_inst
, 3, src
, i
);
699 tgsi_to_qir_dp4(struct vc4_compile
*c
,
700 struct tgsi_full_instruction
*tgsi_inst
,
701 enum qop op
, struct qreg
*src
, int i
)
703 return tgsi_to_qir_dp(c
, tgsi_inst
, 4, src
, i
);
707 tgsi_to_qir_abs(struct vc4_compile
*c
,
708 struct tgsi_full_instruction
*tgsi_inst
,
709 enum qop op
, struct qreg
*src
, int i
)
711 struct qreg arg
= src
[0 * 4 + i
];
712 return qir_FMAXABS(c
, arg
, arg
);
715 /* Note that this instruction replicates its result from the x channel */
717 tgsi_to_qir_sin(struct vc4_compile
*c
,
718 struct tgsi_full_instruction
*tgsi_inst
,
719 enum qop op
, struct qreg
*src
, int i
)
723 -pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
724 pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
725 -pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
728 struct qreg scaled_x
=
731 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
734 struct qreg x
= tgsi_to_qir_frc(c
, NULL
, 0, &scaled_x
, 0);
735 struct qreg x2
= qir_FMUL(c
, x
, x
);
736 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
737 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
738 x
= qir_FMUL(c
, x
, x2
);
743 qir_uniform_f(c
, coeff
[i
])));
748 /* Note that this instruction replicates its result from the x channel */
750 tgsi_to_qir_cos(struct vc4_compile
*c
,
751 struct tgsi_full_instruction
*tgsi_inst
,
752 enum qop op
, struct qreg
*src
, int i
)
756 -pow(2.0 * M_PI
, 2) / (2 * 1),
757 pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
758 -pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
761 struct qreg scaled_x
=
762 qir_FMUL(c
, src
[0 * 4 + 0],
763 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
764 struct qreg x_frac
= tgsi_to_qir_frc(c
, NULL
, 0, &scaled_x
, 0);
766 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
767 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
768 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
769 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
771 x
= qir_FMUL(c
, x
, x2
);
773 struct qreg mul
= qir_FMUL(c
,
775 qir_uniform_f(c
, coeff
[i
]));
779 sum
= qir_FADD(c
, sum
, mul
);
785 emit_vertex_input(struct vc4_compile
*c
, int attr
)
787 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
788 struct qreg vpm_reads
[4];
790 /* Right now, we're setting the VPM offsets to be 16 bytes wide every
791 * time, so we always read 4 32-bit VPM entries.
793 for (int i
= 0; i
< 4; i
++) {
794 vpm_reads
[i
] = qir_get_temp(c
);
795 qir_emit(c
, qir_inst(QOP_VPM_READ
,
802 bool format_warned
= false;
803 const struct util_format_description
*desc
=
804 util_format_description(format
);
806 for (int i
= 0; i
< 4; i
++) {
807 uint8_t swiz
= desc
->swizzle
[i
];
810 if (swiz
> UTIL_FORMAT_SWIZZLE_W
)
811 result
= get_swizzled_channel(c
, vpm_reads
, swiz
);
812 else if (desc
->channel
[swiz
].size
== 32 &&
813 desc
->channel
[swiz
].type
== UTIL_FORMAT_TYPE_FLOAT
) {
814 result
= get_swizzled_channel(c
, vpm_reads
, swiz
);
815 } else if (desc
->channel
[swiz
].size
== 8 &&
816 (desc
->channel
[swiz
].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
817 desc
->channel
[swiz
].type
== UTIL_FORMAT_TYPE_SIGNED
) &&
818 desc
->channel
[swiz
].normalized
) {
819 struct qreg vpm
= vpm_reads
[0];
820 if (desc
->channel
[swiz
].type
== UTIL_FORMAT_TYPE_SIGNED
)
821 vpm
= qir_XOR(c
, vpm
, qir_uniform_ui(c
, 0x80808080));
822 result
= qir_UNPACK_8(c
, vpm
, swiz
);
824 if (!format_warned
) {
826 "vtx element %d unsupported type: %s\n",
827 attr
, util_format_name(format
));
828 format_warned
= true;
830 result
= qir_uniform_f(c
, 0.0);
833 if (desc
->channel
[swiz
].normalized
&&
834 desc
->channel
[swiz
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
838 qir_uniform_f(c
, 2.0)),
839 qir_uniform_f(c
, 1.0));
842 c
->inputs
[attr
* 4 + i
] = result
;
847 tgsi_to_qir_kill_if(struct vc4_compile
*c
, struct qreg
*src
, int i
)
849 if (c
->discard
.file
== QFILE_NULL
)
850 c
->discard
= qir_uniform_f(c
, 0.0);
851 qir_SF(c
, src
[0 * 4 + i
]);
852 c
->discard
= qir_SEL_X_Y_NS(c
, qir_uniform_f(c
, 1.0),
857 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
859 c
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
860 c
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
861 c
->inputs
[attr
* 4 + 2] =
863 qir_ITOF(c
, qir_FRAG_Z(c
)),
864 qir_uniform_f(c
, 1.0 / 0xffffff));
865 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
869 emit_fragment_varying(struct vc4_compile
*c
, int index
)
876 /* XXX: multiply by W */
877 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
881 emit_fragment_input(struct vc4_compile
*c
, int attr
)
883 for (int i
= 0; i
< 4; i
++) {
884 c
->inputs
[attr
* 4 + i
] =
885 emit_fragment_varying(c
, attr
* 4 + i
);
891 emit_tgsi_declaration(struct vc4_compile
*c
,
892 struct tgsi_full_declaration
*decl
)
894 switch (decl
->Declaration
.File
) {
895 case TGSI_FILE_TEMPORARY
:
896 resize_qreg_array(c
, &c
->temps
, &c
->temps_array_size
,
897 (decl
->Range
.Last
+ 1) * 4);
900 case TGSI_FILE_INPUT
:
901 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
902 (decl
->Range
.Last
+ 1) * 4);
904 for (int i
= decl
->Range
.First
;
905 i
<= decl
->Range
.Last
;
907 if (c
->stage
== QSTAGE_FRAG
) {
908 if (decl
->Semantic
.Name
==
909 TGSI_SEMANTIC_POSITION
) {
910 emit_fragcoord_input(c
, i
);
912 emit_fragment_input(c
, i
);
915 emit_vertex_input(c
, i
);
920 case TGSI_FILE_OUTPUT
:
921 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
922 (decl
->Range
.Last
+ 1) * 4);
924 switch (decl
->Semantic
.Name
) {
925 case TGSI_SEMANTIC_POSITION
:
926 c
->output_position_index
= decl
->Range
.First
* 4;
928 case TGSI_SEMANTIC_COLOR
:
929 c
->output_color_index
= decl
->Range
.First
* 4;
938 emit_tgsi_instruction(struct vc4_compile
*c
,
939 struct tgsi_full_instruction
*tgsi_inst
)
943 struct qreg (*func
)(struct vc4_compile
*c
,
944 struct tgsi_full_instruction
*tgsi_inst
,
946 struct qreg
*src
, int i
);
948 [TGSI_OPCODE_MOV
] = { QOP_MOV
, tgsi_to_qir_alu
},
949 [TGSI_OPCODE_ABS
] = { 0, tgsi_to_qir_abs
},
950 [TGSI_OPCODE_MUL
] = { QOP_FMUL
, tgsi_to_qir_alu
},
951 [TGSI_OPCODE_ADD
] = { QOP_FADD
, tgsi_to_qir_alu
},
952 [TGSI_OPCODE_SUB
] = { QOP_FSUB
, tgsi_to_qir_alu
},
953 [TGSI_OPCODE_MIN
] = { QOP_FMIN
, tgsi_to_qir_alu
},
954 [TGSI_OPCODE_MAX
] = { QOP_FMAX
, tgsi_to_qir_alu
},
955 [TGSI_OPCODE_F2I
] = { QOP_FTOI
, tgsi_to_qir_alu
},
956 [TGSI_OPCODE_I2F
] = { QOP_ITOF
, tgsi_to_qir_alu
},
957 [TGSI_OPCODE_UADD
] = { QOP_ADD
, tgsi_to_qir_alu
},
958 [TGSI_OPCODE_USHR
] = { QOP_SHR
, tgsi_to_qir_alu
},
959 [TGSI_OPCODE_ISHR
] = { QOP_ASR
, tgsi_to_qir_alu
},
960 [TGSI_OPCODE_SHL
] = { QOP_SHL
, tgsi_to_qir_alu
},
961 [TGSI_OPCODE_IMIN
] = { QOP_MIN
, tgsi_to_qir_alu
},
962 [TGSI_OPCODE_IMAX
] = { QOP_MAX
, tgsi_to_qir_alu
},
963 [TGSI_OPCODE_AND
] = { QOP_AND
, tgsi_to_qir_alu
},
964 [TGSI_OPCODE_OR
] = { QOP_OR
, tgsi_to_qir_alu
},
965 [TGSI_OPCODE_XOR
] = { QOP_XOR
, tgsi_to_qir_alu
},
966 [TGSI_OPCODE_NOT
] = { QOP_NOT
, tgsi_to_qir_alu
},
968 [TGSI_OPCODE_UMUL
] = { 0, tgsi_to_qir_umul
},
969 [TGSI_OPCODE_IDIV
] = { 0, tgsi_to_qir_idiv
},
970 [TGSI_OPCODE_INEG
] = { 0, tgsi_to_qir_ineg
},
972 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
973 [TGSI_OPCODE_SEQ
] = { 0, tgsi_to_qir_seq
},
974 [TGSI_OPCODE_SNE
] = { 0, tgsi_to_qir_sne
},
975 [TGSI_OPCODE_SGE
] = { 0, tgsi_to_qir_sge
},
976 [TGSI_OPCODE_SLT
] = { 0, tgsi_to_qir_slt
},
977 [TGSI_OPCODE_FSEQ
] = { 0, tgsi_to_qir_fseq
},
978 [TGSI_OPCODE_FSNE
] = { 0, tgsi_to_qir_fsne
},
979 [TGSI_OPCODE_FSGE
] = { 0, tgsi_to_qir_fsge
},
980 [TGSI_OPCODE_FSLT
] = { 0, tgsi_to_qir_fslt
},
981 [TGSI_OPCODE_USEQ
] = { 0, tgsi_to_qir_useq
},
982 [TGSI_OPCODE_USNE
] = { 0, tgsi_to_qir_usne
},
983 [TGSI_OPCODE_ISGE
] = { 0, tgsi_to_qir_isge
},
984 [TGSI_OPCODE_ISLT
] = { 0, tgsi_to_qir_islt
},
986 [TGSI_OPCODE_CMP
] = { 0, tgsi_to_qir_cmp
},
987 [TGSI_OPCODE_MAD
] = { 0, tgsi_to_qir_mad
},
988 [TGSI_OPCODE_DP2
] = { 0, tgsi_to_qir_dp2
},
989 [TGSI_OPCODE_DP3
] = { 0, tgsi_to_qir_dp3
},
990 [TGSI_OPCODE_DP4
] = { 0, tgsi_to_qir_dp4
},
991 [TGSI_OPCODE_RCP
] = { QOP_RCP
, tgsi_to_qir_alu
},
992 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
993 [TGSI_OPCODE_EX2
] = { QOP_EXP2
, tgsi_to_qir_alu
},
994 [TGSI_OPCODE_LG2
] = { QOP_LOG2
, tgsi_to_qir_alu
},
995 [TGSI_OPCODE_LIT
] = { 0, tgsi_to_qir_lit
},
996 [TGSI_OPCODE_LRP
] = { 0, tgsi_to_qir_lrp
},
997 [TGSI_OPCODE_POW
] = { 0, tgsi_to_qir_pow
},
998 [TGSI_OPCODE_TRUNC
] = { 0, tgsi_to_qir_trunc
},
999 [TGSI_OPCODE_FRC
] = { 0, tgsi_to_qir_frc
},
1000 [TGSI_OPCODE_FLR
] = { 0, tgsi_to_qir_flr
},
1001 [TGSI_OPCODE_SIN
] = { 0, tgsi_to_qir_sin
},
1002 [TGSI_OPCODE_COS
] = { 0, tgsi_to_qir_cos
},
1004 static int asdf
= 0;
1005 uint32_t tgsi_op
= tgsi_inst
->Instruction
.Opcode
;
1007 if (tgsi_op
== TGSI_OPCODE_END
)
1010 struct qreg src_regs
[12];
1011 for (int s
= 0; s
< 3; s
++) {
1012 for (int i
= 0; i
< 4; i
++) {
1013 src_regs
[4 * s
+ i
] =
1014 get_src(c
, tgsi_inst
->Instruction
.Opcode
,
1015 &tgsi_inst
->Src
[s
].Register
, i
);
1020 case TGSI_OPCODE_TEX
:
1021 case TGSI_OPCODE_TXP
:
1022 case TGSI_OPCODE_TXB
:
1023 tgsi_to_qir_tex(c
, tgsi_inst
,
1024 op_trans
[tgsi_op
].op
, src_regs
);
1026 case TGSI_OPCODE_KILL
:
1027 c
->discard
= qir_uniform_f(c
, 1.0);
1029 case TGSI_OPCODE_KILL_IF
:
1030 for (int i
= 0; i
< 4; i
++)
1031 tgsi_to_qir_kill_if(c
, src_regs
, i
);
1037 if (tgsi_op
> ARRAY_SIZE(op_trans
) || !(op_trans
[tgsi_op
].func
)) {
1038 fprintf(stderr
, "unknown tgsi inst: ");
1039 tgsi_dump_instruction(tgsi_inst
, asdf
++);
1040 fprintf(stderr
, "\n");
1044 for (int i
= 0; i
< 4; i
++) {
1045 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
1050 result
= op_trans
[tgsi_op
].func(c
, tgsi_inst
,
1051 op_trans
[tgsi_op
].op
,
1054 if (tgsi_inst
->Instruction
.Saturate
) {
1055 float low
= (tgsi_inst
->Instruction
.Saturate
==
1056 TGSI_SAT_MINUS_PLUS_ONE
? -1.0 : 0.0);
1057 result
= qir_FMAX(c
,
1060 qir_uniform_f(c
, 1.0)),
1061 qir_uniform_f(c
, low
));
1064 update_dst(c
, tgsi_inst
, i
, result
);
1069 parse_tgsi_immediate(struct vc4_compile
*c
, struct tgsi_full_immediate
*imm
)
1071 for (int i
= 0; i
< 4; i
++) {
1072 unsigned n
= c
->num_consts
++;
1073 resize_qreg_array(c
, &c
->consts
, &c
->consts_array_size
, n
+ 1);
1074 c
->consts
[n
] = qir_uniform_ui(c
, imm
->u
[i
].Uint
);
1079 vc4_blend_channel(struct vc4_compile
*c
,
1087 case PIPE_BLENDFACTOR_ONE
:
1089 case PIPE_BLENDFACTOR_SRC_COLOR
:
1090 return qir_FMUL(c
, val
, src
[channel
]);
1091 case PIPE_BLENDFACTOR_SRC_ALPHA
:
1092 return qir_FMUL(c
, val
, src
[3]);
1093 case PIPE_BLENDFACTOR_DST_ALPHA
:
1094 return qir_FMUL(c
, val
, dst
[3]);
1095 case PIPE_BLENDFACTOR_DST_COLOR
:
1096 return qir_FMUL(c
, val
, dst
[channel
]);
1097 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
1098 return qir_FMIN(c
, src
[3], qir_FSUB(c
,
1099 qir_uniform_f(c
, 1.0),
1101 case PIPE_BLENDFACTOR_CONST_COLOR
:
1102 return qir_FMUL(c
, val
,
1103 get_temp_for_uniform(c
,
1104 QUNIFORM_BLEND_CONST_COLOR
,
1106 case PIPE_BLENDFACTOR_CONST_ALPHA
:
1107 return qir_FMUL(c
, val
,
1108 get_temp_for_uniform(c
,
1109 QUNIFORM_BLEND_CONST_COLOR
,
1111 case PIPE_BLENDFACTOR_ZERO
:
1112 return qir_uniform_f(c
, 0.0);
1113 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
1114 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1116 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
1117 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1119 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
1120 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1122 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
1123 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1125 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
1126 return qir_FMUL(c
, val
,
1127 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1128 get_temp_for_uniform(c
,
1129 QUNIFORM_BLEND_CONST_COLOR
,
1131 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
1132 return qir_FMUL(c
, val
,
1133 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1134 get_temp_for_uniform(c
,
1135 QUNIFORM_BLEND_CONST_COLOR
,
1139 case PIPE_BLENDFACTOR_SRC1_COLOR
:
1140 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
1141 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
1142 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
1144 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
1150 vc4_blend_func(struct vc4_compile
*c
,
1151 struct qreg src
, struct qreg dst
,
1155 case PIPE_BLEND_ADD
:
1156 return qir_FADD(c
, src
, dst
);
1157 case PIPE_BLEND_SUBTRACT
:
1158 return qir_FSUB(c
, src
, dst
);
1159 case PIPE_BLEND_REVERSE_SUBTRACT
:
1160 return qir_FSUB(c
, dst
, src
);
1161 case PIPE_BLEND_MIN
:
1162 return qir_FMIN(c
, src
, dst
);
1163 case PIPE_BLEND_MAX
:
1164 return qir_FMAX(c
, src
, dst
);
1168 fprintf(stderr
, "Unknown blend func %d\n", func
);
1175 * Implements fixed function blending in shader code.
1177 * VC4 doesn't have any hardware support for blending. Instead, you read the
1178 * current contents of the destination from the tile buffer after having
1179 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
1180 * math using your output color and that destination value, and update the
1181 * output color appropriately.
1184 vc4_blend(struct vc4_compile
*c
, struct qreg
*result
,
1185 struct qreg
*dst_color
, struct qreg
*src_color
)
1187 struct pipe_rt_blend_state
*blend
= &c
->fs_key
->blend
;
1189 if (!blend
->blend_enable
) {
1190 for (int i
= 0; i
< 4; i
++)
1191 result
[i
] = src_color
[i
];
1195 struct qreg src_blend
[4], dst_blend
[4];
1196 for (int i
= 0; i
< 3; i
++) {
1197 src_blend
[i
] = vc4_blend_channel(c
,
1198 dst_color
, src_color
,
1200 blend
->rgb_src_factor
, i
);
1201 dst_blend
[i
] = vc4_blend_channel(c
,
1202 dst_color
, src_color
,
1204 blend
->rgb_dst_factor
, i
);
1206 src_blend
[3] = vc4_blend_channel(c
,
1207 dst_color
, src_color
,
1209 blend
->alpha_src_factor
, 3);
1210 dst_blend
[3] = vc4_blend_channel(c
,
1211 dst_color
, src_color
,
1213 blend
->alpha_dst_factor
, 3);
1215 for (int i
= 0; i
< 3; i
++) {
1216 result
[i
] = vc4_blend_func(c
,
1217 src_blend
[i
], dst_blend
[i
],
1220 result
[3] = vc4_blend_func(c
,
1221 src_blend
[3], dst_blend
[3],
1226 emit_frag_end(struct vc4_compile
*c
)
1228 enum pipe_format color_format
= c
->fs_key
->color_format
;
1229 const uint8_t *format_swiz
= vc4_get_format_swizzle(color_format
);
1230 struct qreg tlb_read_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1231 struct qreg dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1232 if (c
->fs_key
->blend
.blend_enable
||
1233 c
->fs_key
->blend
.colormask
!= 0xf) {
1234 struct qreg r4
= qir_TLB_COLOR_READ(c
);
1235 for (int i
= 0; i
< 4; i
++)
1236 tlb_read_color
[i
] = qir_R4_UNPACK(c
, r4
, i
);
1237 for (int i
= 0; i
< 4; i
++)
1238 dst_color
[i
] = get_swizzled_channel(c
,
1243 struct qreg blend_color
[4];
1244 struct qreg undef_array
[4] = {
1245 c
->undef
, c
->undef
, c
->undef
, c
->undef
1247 vc4_blend(c
, blend_color
, dst_color
,
1248 (c
->output_color_index
!= -1 ?
1249 c
->outputs
+ c
->output_color_index
:
1252 /* If the bit isn't set in the color mask, then just return the
1253 * original dst color, instead.
1255 for (int i
= 0; i
< 4; i
++) {
1256 if (!(c
->fs_key
->blend
.colormask
& (1 << i
))) {
1257 blend_color
[i
] = dst_color
[i
];
1261 /* Debug: Sometimes you're getting a black output and just want to see
1262 * if the FS is getting executed at all. Spam magenta into the color
1266 blend_color
[0] = qir_uniform_f(c
, 1.0);
1267 blend_color
[1] = qir_uniform_f(c
, 0.0);
1268 blend_color
[2] = qir_uniform_f(c
, 1.0);
1269 blend_color
[3] = qir_uniform_f(c
, 0.5);
1272 struct qreg swizzled_outputs
[4];
1273 for (int i
= 0; i
< 4; i
++) {
1274 swizzled_outputs
[i
] = get_swizzled_channel(c
, blend_color
,
1278 if (c
->discard
.file
!= QFILE_NULL
)
1279 qir_TLB_DISCARD_SETUP(c
, c
->discard
);
1281 if (c
->fs_key
->stencil_enabled
) {
1282 qir_TLB_STENCIL_SETUP(c
, add_uniform(c
, QUNIFORM_STENCIL
, 0));
1283 if (c
->fs_key
->stencil_twoside
) {
1284 qir_TLB_STENCIL_SETUP(c
, add_uniform(c
, QUNIFORM_STENCIL
, 1));
1286 if (c
->fs_key
->stencil_full_writemasks
) {
1287 qir_TLB_STENCIL_SETUP(c
, add_uniform(c
, QUNIFORM_STENCIL
, 2));
1291 if (c
->fs_key
->depth_enabled
) {
1293 if (c
->output_position_index
!= -1) {
1294 z
= qir_FTOI(c
, qir_FMUL(c
, c
->outputs
[c
->output_position_index
+ 2],
1295 qir_uniform_f(c
, 0xffffff)));
1299 qir_TLB_Z_WRITE(c
, z
);
1302 bool color_written
= false;
1303 for (int i
= 0; i
< 4; i
++) {
1304 if (swizzled_outputs
[i
].file
!= QFILE_NULL
)
1305 color_written
= true;
1308 struct qreg packed_color
;
1309 if (color_written
) {
1310 /* Fill in any undefined colors. The simulator will assertion
1311 * fail if we read something that wasn't written, and I don't
1312 * know what hardware does.
1314 for (int i
= 0; i
< 4; i
++) {
1315 if (swizzled_outputs
[i
].file
== QFILE_NULL
)
1316 swizzled_outputs
[i
] = qir_uniform_f(c
, 0.0);
1318 packed_color
= qir_get_temp(c
);
1319 qir_emit(c
, qir_inst4(QOP_PACK_COLORS
, packed_color
,
1320 swizzled_outputs
[0],
1321 swizzled_outputs
[1],
1322 swizzled_outputs
[2],
1323 swizzled_outputs
[3]));
1325 packed_color
= qir_uniform_ui(c
, 0);
1328 qir_emit(c
, qir_inst(QOP_TLB_COLOR_WRITE
, c
->undef
,
1329 packed_color
, c
->undef
));
1333 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1337 for (int i
= 0; i
< 2; i
++) {
1339 add_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1341 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1348 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1352 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1354 struct qreg zscale
= add_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1355 struct qreg zoffset
= add_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1357 qir_VPM_WRITE(c
, qir_FMUL(c
, qir_FADD(c
, qir_FMUL(c
,
1365 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1367 qir_VPM_WRITE(c
, rcp_w
);
1371 emit_vert_end(struct vc4_compile
*c
)
1373 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[3]);
1375 emit_scaled_viewport_write(c
, rcp_w
);
1376 emit_zs_write(c
, rcp_w
);
1377 emit_rcp_wc_write(c
, rcp_w
);
1379 for (int i
= 4; i
< c
->num_outputs
; i
++) {
1380 qir_VPM_WRITE(c
, c
->outputs
[i
]);
1385 emit_coord_end(struct vc4_compile
*c
)
1387 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[3]);
1389 for (int i
= 0; i
< 4; i
++)
1390 qir_VPM_WRITE(c
, c
->outputs
[i
]);
1392 emit_scaled_viewport_write(c
, rcp_w
);
1393 emit_zs_write(c
, rcp_w
);
1394 emit_rcp_wc_write(c
, rcp_w
);
1397 static struct vc4_compile
*
1398 vc4_shader_tgsi_to_qir(struct vc4_context
*vc4
,
1399 struct vc4_compiled_shader
*shader
, enum qstage stage
,
1400 struct vc4_key
*key
)
1402 struct vc4_compile
*c
= qir_compile_init();
1407 c
->uniform_data
= ralloc_array(c
, uint32_t, 1024);
1408 c
->uniform_contents
= ralloc_array(c
, enum quniform_contents
, 1024);
1410 c
->shader_state
= key
->shader_state
;
1411 ret
= tgsi_parse_init(&c
->parser
, c
->shader_state
->tokens
);
1412 assert(ret
== TGSI_PARSE_OK
);
1414 if (vc4_debug
& VC4_DEBUG_TGSI
) {
1415 fprintf(stderr
, "TGSI:\n");
1416 tgsi_dump(c
->shader_state
->tokens
, 0);
1422 c
->fs_key
= (struct vc4_fs_key
*)key
;
1423 if (c
->fs_key
->is_points
) {
1424 c
->point_x
= emit_fragment_varying(c
, 0);
1425 c
->point_y
= emit_fragment_varying(c
, 0);
1426 } else if (c
->fs_key
->is_lines
) {
1427 c
->line_x
= emit_fragment_varying(c
, 0);
1431 c
->vs_key
= (struct vc4_vs_key
*)key
;
1434 c
->vs_key
= (struct vc4_vs_key
*)key
;
1438 while (!tgsi_parse_end_of_tokens(&c
->parser
)) {
1439 tgsi_parse_token(&c
->parser
);
1441 switch (c
->parser
.FullToken
.Token
.Type
) {
1442 case TGSI_TOKEN_TYPE_DECLARATION
:
1443 emit_tgsi_declaration(c
,
1444 &c
->parser
.FullToken
.FullDeclaration
);
1447 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1448 emit_tgsi_instruction(c
,
1449 &c
->parser
.FullToken
.FullInstruction
);
1452 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1453 parse_tgsi_immediate(c
,
1454 &c
->parser
.FullToken
.FullImmediate
);
1471 tgsi_parse_free(&c
->parser
);
1475 if (vc4_debug
& VC4_DEBUG_QIR
) {
1476 fprintf(stderr
, "QIR:\n");
1479 qir_reorder_uniforms(c
);
1480 vc4_generate_code(vc4
, c
);
1482 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
1483 fprintf(stderr
, "SHADER-DB: %s: %d instructions\n",
1484 qir_get_stage_name(c
->stage
), c
->qpu_inst_count
);
1485 fprintf(stderr
, "SHADER-DB: %s: %d uniforms\n",
1486 qir_get_stage_name(c
->stage
), c
->num_uniforms
);
1493 vc4_shader_state_create(struct pipe_context
*pctx
,
1494 const struct pipe_shader_state
*cso
)
1496 struct pipe_shader_state
*so
= CALLOC_STRUCT(pipe_shader_state
);
1500 so
->tokens
= tgsi_dup_tokens(cso
->tokens
);
1506 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
1508 struct vc4_compile
*c
)
1510 int count
= c
->num_uniforms
;
1511 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1513 uinfo
->count
= count
;
1514 uinfo
->data
= malloc(count
* sizeof(*uinfo
->data
));
1515 memcpy(uinfo
->data
, c
->uniform_data
,
1516 count
* sizeof(*uinfo
->data
));
1517 uinfo
->contents
= malloc(count
* sizeof(*uinfo
->contents
));
1518 memcpy(uinfo
->contents
, c
->uniform_contents
,
1519 count
* sizeof(*uinfo
->contents
));
1520 uinfo
->num_texture_samples
= c
->num_texture_samples
;
1524 vc4_fs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1525 struct vc4_fs_key
*key
)
1527 struct vc4_compile
*c
= vc4_shader_tgsi_to_qir(vc4
, shader
,
1530 shader
->num_inputs
= c
->num_inputs
;
1531 copy_uniform_state_to_shader(shader
, 0, c
);
1532 shader
->bo
= vc4_bo_alloc_mem(vc4
->screen
, c
->qpu_insts
,
1533 c
->qpu_inst_count
* sizeof(uint64_t),
1536 qir_compile_destroy(c
);
1540 vc4_vs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1541 struct vc4_vs_key
*key
)
1543 struct vc4_compile
*vs_c
= vc4_shader_tgsi_to_qir(vc4
, shader
,
1546 copy_uniform_state_to_shader(shader
, 0, vs_c
);
1548 struct vc4_compile
*cs_c
= vc4_shader_tgsi_to_qir(vc4
, shader
,
1551 copy_uniform_state_to_shader(shader
, 1, cs_c
);
1553 uint32_t vs_size
= vs_c
->qpu_inst_count
* sizeof(uint64_t);
1554 uint32_t cs_size
= cs_c
->qpu_inst_count
* sizeof(uint64_t);
1555 shader
->coord_shader_offset
= vs_size
; /* XXX: alignment? */
1556 shader
->bo
= vc4_bo_alloc(vc4
->screen
,
1557 shader
->coord_shader_offset
+ cs_size
,
1560 void *map
= vc4_bo_map(shader
->bo
);
1561 memcpy(map
, vs_c
->qpu_insts
, vs_size
);
1562 memcpy(map
+ shader
->coord_shader_offset
,
1563 cs_c
->qpu_insts
, cs_size
);
1565 qir_compile_destroy(vs_c
);
1566 qir_compile_destroy(cs_c
);
1570 vc4_setup_shared_key(struct vc4_key
*key
, struct vc4_texture_stateobj
*texstate
)
1572 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
1573 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
1574 struct pipe_sampler_state
*sampler_state
=
1575 texstate
->samplers
[i
];
1578 struct pipe_resource
*prsc
= sampler
->texture
;
1579 key
->tex
[i
].format
= prsc
->format
;
1580 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
1581 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
1582 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
1583 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
1584 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
1585 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
1591 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
1593 struct vc4_fs_key local_key
;
1594 struct vc4_fs_key
*key
= &local_key
;
1596 memset(key
, 0, sizeof(*key
));
1597 vc4_setup_shared_key(&key
->base
, &vc4
->fragtex
);
1598 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
1599 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
1600 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
1601 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
1602 key
->blend
= vc4
->blend
->rt
[0];
1604 if (vc4
->framebuffer
.cbufs
[0])
1605 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
1607 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
1608 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
1609 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
1610 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
1611 key
->stencil_enabled
);
1613 vc4
->prog
.fs
= util_hash_table_get(vc4
->fs_cache
, key
);
1617 key
= malloc(sizeof(*key
));
1618 memcpy(key
, &local_key
, sizeof(*key
));
1620 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1621 vc4_fs_compile(vc4
, shader
, key
);
1622 util_hash_table_set(vc4
->fs_cache
, key
, shader
);
1624 vc4
->prog
.fs
= shader
;
1628 vc4_update_compiled_vs(struct vc4_context
*vc4
)
1630 struct vc4_vs_key local_key
;
1631 struct vc4_vs_key
*key
= &local_key
;
1633 memset(key
, 0, sizeof(*key
));
1634 vc4_setup_shared_key(&key
->base
, &vc4
->verttex
);
1635 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
1637 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
1638 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
1640 vc4
->prog
.vs
= util_hash_table_get(vc4
->vs_cache
, key
);
1644 key
= malloc(sizeof(*key
));
1645 memcpy(key
, &local_key
, sizeof(*key
));
1647 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1648 vc4_vs_compile(vc4
, shader
, key
);
1649 util_hash_table_set(vc4
->vs_cache
, key
, shader
);
1651 vc4
->prog
.vs
= shader
;
1655 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
1657 vc4_update_compiled_fs(vc4
, prim_mode
);
1658 vc4_update_compiled_vs(vc4
);
1662 fs_cache_hash(void *key
)
1664 return util_hash_crc32(key
, sizeof(struct vc4_fs_key
));
1668 vs_cache_hash(void *key
)
1670 return util_hash_crc32(key
, sizeof(struct vc4_vs_key
));
1674 fs_cache_compare(void *key1
, void *key2
)
1676 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
));
1680 vs_cache_compare(void *key1
, void *key2
)
1682 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
));
1685 struct delete_state
{
1686 struct vc4_context
*vc4
;
1687 struct pipe_shader_state
*shader_state
;
1690 static enum pipe_error
1691 fs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1693 struct delete_state
*del
= data
;
1694 struct vc4_fs_key
*key
= in_key
;
1695 struct vc4_compiled_shader
*shader
= in_value
;
1697 if (key
->base
.shader_state
== data
) {
1698 util_hash_table_remove(del
->vc4
->fs_cache
, key
);
1699 vc4_bo_unreference(&shader
->bo
);
1706 static enum pipe_error
1707 vs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1709 struct delete_state
*del
= data
;
1710 struct vc4_vs_key
*key
= in_key
;
1711 struct vc4_compiled_shader
*shader
= in_value
;
1713 if (key
->base
.shader_state
== data
) {
1714 util_hash_table_remove(del
->vc4
->vs_cache
, key
);
1715 vc4_bo_unreference(&shader
->bo
);
1723 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
1725 struct vc4_context
*vc4
= vc4_context(pctx
);
1726 struct pipe_shader_state
*so
= hwcso
;
1727 struct delete_state del
;
1730 del
.shader_state
= so
;
1731 util_hash_table_foreach(vc4
->fs_cache
, fs_delete_from_cache
, &del
);
1732 util_hash_table_foreach(vc4
->vs_cache
, vs_delete_from_cache
, &del
);
1734 free((void *)so
->tokens
);
1738 static uint32_t translate_wrap(uint32_t p_wrap
)
1741 case PIPE_TEX_WRAP_REPEAT
:
1743 case PIPE_TEX_WRAP_CLAMP
:
1744 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1746 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1748 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1751 fprintf(stderr
, "Unknown wrap mode %d\n", p_wrap
);
1752 assert(!"not reached");
1758 write_texture_p0(struct vc4_context
*vc4
,
1759 struct vc4_texture_stateobj
*texstate
,
1762 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
1763 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
1765 cl_reloc(vc4
, &vc4
->uniforms
, rsc
->bo
,
1766 rsc
->slices
[0].offset
| texture
->u
.tex
.last_level
|
1767 ((rsc
->vc4_format
& 7) << 4));
1771 write_texture_p1(struct vc4_context
*vc4
,
1772 struct vc4_texture_stateobj
*texstate
,
1775 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
1776 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
1777 struct pipe_sampler_state
*sampler
= texstate
->samplers
[unit
];
1778 static const uint32_t mipfilter_map
[] = {
1779 [PIPE_TEX_MIPFILTER_NEAREST
] = 2,
1780 [PIPE_TEX_MIPFILTER_LINEAR
] = 4,
1781 [PIPE_TEX_MIPFILTER_NONE
] = 0
1783 static const uint32_t imgfilter_map
[] = {
1784 [PIPE_TEX_FILTER_NEAREST
] = 1,
1785 [PIPE_TEX_FILTER_LINEAR
] = 0,
1788 cl_u32(&vc4
->uniforms
,
1789 ((rsc
->vc4_format
>> 4) << 31) |
1790 (texture
->texture
->height0
<< 20) |
1791 (texture
->texture
->width0
<< 8) |
1792 (imgfilter_map
[sampler
->mag_img_filter
] << 7) |
1793 ((imgfilter_map
[sampler
->min_img_filter
] +
1794 mipfilter_map
[sampler
->min_mip_filter
]) << 4) |
1795 (translate_wrap(sampler
->wrap_t
) << 2) |
1796 (translate_wrap(sampler
->wrap_s
) << 0));
1800 get_texrect_scale(struct vc4_texture_stateobj
*texstate
,
1801 enum quniform_contents contents
,
1804 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
1807 if (contents
== QUNIFORM_TEXRECT_SCALE_X
)
1808 dim
= texture
->texture
->width0
;
1810 dim
= texture
->texture
->height0
;
1812 return fui(1.0f
/ dim
);
1816 vc4_write_uniforms(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1817 struct vc4_constbuf_stateobj
*cb
,
1818 struct vc4_texture_stateobj
*texstate
,
1821 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1822 const uint32_t *gallium_uniforms
= cb
->cb
[0].user_buffer
;
1824 cl_start_shader_reloc(&vc4
->uniforms
, uinfo
->num_texture_samples
);
1826 for (int i
= 0; i
< uinfo
->count
; i
++) {
1828 switch (uinfo
->contents
[i
]) {
1829 case QUNIFORM_CONSTANT
:
1830 cl_u32(&vc4
->uniforms
, uinfo
->data
[i
]);
1832 case QUNIFORM_UNIFORM
:
1833 cl_u32(&vc4
->uniforms
,
1834 gallium_uniforms
[uinfo
->data
[i
]]);
1836 case QUNIFORM_VIEWPORT_X_SCALE
:
1837 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[0] * 16.0f
);
1839 case QUNIFORM_VIEWPORT_Y_SCALE
:
1840 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[1] * 16.0f
);
1843 case QUNIFORM_VIEWPORT_Z_OFFSET
:
1844 cl_f(&vc4
->uniforms
, vc4
->viewport
.translate
[2]);
1846 case QUNIFORM_VIEWPORT_Z_SCALE
:
1847 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[2]);
1850 case QUNIFORM_TEXTURE_CONFIG_P0
:
1851 write_texture_p0(vc4
, texstate
, uinfo
->data
[i
]);
1854 case QUNIFORM_TEXTURE_CONFIG_P1
:
1855 write_texture_p1(vc4
, texstate
, uinfo
->data
[i
]);
1858 case QUNIFORM_TEXRECT_SCALE_X
:
1859 case QUNIFORM_TEXRECT_SCALE_Y
:
1860 cl_u32(&vc4
->uniforms
,
1861 get_texrect_scale(texstate
,
1866 case QUNIFORM_BLEND_CONST_COLOR
:
1867 cl_f(&vc4
->uniforms
,
1868 vc4
->blend_color
.color
[uinfo
->data
[i
]]);
1871 case QUNIFORM_STENCIL
:
1872 cl_u32(&vc4
->uniforms
,
1873 vc4
->zsa
->stencil_uniforms
[uinfo
->data
[i
]] |
1874 (uinfo
->data
[i
] <= 1 ?
1875 (vc4
->stencil_ref
.ref_value
[uinfo
->data
[i
]] << 8) :
1880 uint32_t written_val
= *(uint32_t *)(vc4
->uniforms
.next
- 4);
1881 fprintf(stderr
, "%p/%d: %d: 0x%08x (%f)\n",
1882 shader
, shader_index
, i
, written_val
, uif(written_val
));
1888 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1890 struct vc4_context
*vc4
= vc4_context(pctx
);
1891 vc4
->prog
.bind_fs
= hwcso
;
1892 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_FP
;
1893 vc4
->dirty
|= VC4_DIRTY_PROG
;
1897 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1899 struct vc4_context
*vc4
= vc4_context(pctx
);
1900 vc4
->prog
.bind_vs
= hwcso
;
1901 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_VP
;
1902 vc4
->dirty
|= VC4_DIRTY_PROG
;
1906 vc4_program_init(struct pipe_context
*pctx
)
1908 struct vc4_context
*vc4
= vc4_context(pctx
);
1910 pctx
->create_vs_state
= vc4_shader_state_create
;
1911 pctx
->delete_vs_state
= vc4_shader_state_delete
;
1913 pctx
->create_fs_state
= vc4_shader_state_create
;
1914 pctx
->delete_fs_state
= vc4_shader_state_delete
;
1916 pctx
->bind_fs_state
= vc4_fp_state_bind
;
1917 pctx
->bind_vs_state
= vc4_vp_state_bind
;
1919 vc4
->fs_cache
= util_hash_table_create(fs_cache_hash
, fs_cache_compare
);
1920 vc4
->vs_cache
= util_hash_table_create(vs_cache_hash
, vs_cache_compare
);