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
];
809 if (swiz
<= UTIL_FORMAT_SWIZZLE_W
&&
811 (desc
->channel
[swiz
].type
!= UTIL_FORMAT_TYPE_FLOAT
||
812 desc
->channel
[swiz
].size
!= 32)) {
814 "vtx element %d unsupported type: %s\n",
815 attr
, util_format_name(format
));
816 format_warned
= true;
819 c
->inputs
[attr
* 4 + i
] =
820 get_swizzled_channel(c
, vpm_reads
, swiz
);
825 tgsi_to_qir_kill_if(struct vc4_compile
*c
, struct qreg
*src
, int i
)
827 if (c
->discard
.file
== QFILE_NULL
)
828 c
->discard
= qir_uniform_f(c
, 0.0);
829 qir_SF(c
, src
[0 * 4 + i
]);
830 c
->discard
= qir_SEL_X_Y_NS(c
, qir_uniform_f(c
, 1.0),
835 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
837 c
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
838 c
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
839 c
->inputs
[attr
* 4 + 2] =
841 qir_ITOF(c
, qir_FRAG_Z(c
)),
842 qir_uniform_f(c
, 1.0 / 0xffffff));
843 c
->inputs
[attr
* 4 + 3] = qir_RCP(c
, qir_FRAG_W(c
));
847 emit_fragment_varying(struct vc4_compile
*c
, int index
)
854 /* XXX: multiply by W */
855 return qir_VARY_ADD_C(c
, qir_FMUL(c
, vary
, qir_FRAG_W(c
)));
859 emit_fragment_input(struct vc4_compile
*c
, int attr
)
861 for (int i
= 0; i
< 4; i
++) {
862 c
->inputs
[attr
* 4 + i
] =
863 emit_fragment_varying(c
, attr
* 4 + i
);
869 emit_tgsi_declaration(struct vc4_compile
*c
,
870 struct tgsi_full_declaration
*decl
)
872 switch (decl
->Declaration
.File
) {
873 case TGSI_FILE_TEMPORARY
:
874 resize_qreg_array(c
, &c
->temps
, &c
->temps_array_size
,
875 (decl
->Range
.Last
+ 1) * 4);
878 case TGSI_FILE_INPUT
:
879 resize_qreg_array(c
, &c
->inputs
, &c
->inputs_array_size
,
880 (decl
->Range
.Last
+ 1) * 4);
882 for (int i
= decl
->Range
.First
;
883 i
<= decl
->Range
.Last
;
885 if (c
->stage
== QSTAGE_FRAG
) {
886 if (decl
->Semantic
.Name
==
887 TGSI_SEMANTIC_POSITION
) {
888 emit_fragcoord_input(c
, i
);
890 emit_fragment_input(c
, i
);
893 emit_vertex_input(c
, i
);
898 case TGSI_FILE_OUTPUT
:
899 resize_qreg_array(c
, &c
->outputs
, &c
->outputs_array_size
,
900 (decl
->Range
.Last
+ 1) * 4);
902 switch (decl
->Semantic
.Name
) {
903 case TGSI_SEMANTIC_POSITION
:
904 c
->output_position_index
= decl
->Range
.First
* 4;
906 case TGSI_SEMANTIC_COLOR
:
907 c
->output_color_index
= decl
->Range
.First
* 4;
916 emit_tgsi_instruction(struct vc4_compile
*c
,
917 struct tgsi_full_instruction
*tgsi_inst
)
921 struct qreg (*func
)(struct vc4_compile
*c
,
922 struct tgsi_full_instruction
*tgsi_inst
,
924 struct qreg
*src
, int i
);
926 [TGSI_OPCODE_MOV
] = { QOP_MOV
, tgsi_to_qir_alu
},
927 [TGSI_OPCODE_ABS
] = { 0, tgsi_to_qir_abs
},
928 [TGSI_OPCODE_MUL
] = { QOP_FMUL
, tgsi_to_qir_alu
},
929 [TGSI_OPCODE_ADD
] = { QOP_FADD
, tgsi_to_qir_alu
},
930 [TGSI_OPCODE_SUB
] = { QOP_FSUB
, tgsi_to_qir_alu
},
931 [TGSI_OPCODE_MIN
] = { QOP_FMIN
, tgsi_to_qir_alu
},
932 [TGSI_OPCODE_MAX
] = { QOP_FMAX
, tgsi_to_qir_alu
},
933 [TGSI_OPCODE_F2I
] = { QOP_FTOI
, tgsi_to_qir_alu
},
934 [TGSI_OPCODE_I2F
] = { QOP_ITOF
, tgsi_to_qir_alu
},
935 [TGSI_OPCODE_UADD
] = { QOP_ADD
, tgsi_to_qir_alu
},
936 [TGSI_OPCODE_USHR
] = { QOP_SHR
, tgsi_to_qir_alu
},
937 [TGSI_OPCODE_ISHR
] = { QOP_ASR
, tgsi_to_qir_alu
},
938 [TGSI_OPCODE_SHL
] = { QOP_SHL
, tgsi_to_qir_alu
},
939 [TGSI_OPCODE_IMIN
] = { QOP_MIN
, tgsi_to_qir_alu
},
940 [TGSI_OPCODE_IMAX
] = { QOP_MAX
, tgsi_to_qir_alu
},
941 [TGSI_OPCODE_AND
] = { QOP_AND
, tgsi_to_qir_alu
},
942 [TGSI_OPCODE_OR
] = { QOP_OR
, tgsi_to_qir_alu
},
943 [TGSI_OPCODE_XOR
] = { QOP_XOR
, tgsi_to_qir_alu
},
944 [TGSI_OPCODE_NOT
] = { QOP_NOT
, tgsi_to_qir_alu
},
946 [TGSI_OPCODE_UMUL
] = { 0, tgsi_to_qir_umul
},
947 [TGSI_OPCODE_IDIV
] = { 0, tgsi_to_qir_idiv
},
948 [TGSI_OPCODE_INEG
] = { 0, tgsi_to_qir_ineg
},
950 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
951 [TGSI_OPCODE_SEQ
] = { 0, tgsi_to_qir_seq
},
952 [TGSI_OPCODE_SNE
] = { 0, tgsi_to_qir_sne
},
953 [TGSI_OPCODE_SGE
] = { 0, tgsi_to_qir_sge
},
954 [TGSI_OPCODE_SLT
] = { 0, tgsi_to_qir_slt
},
955 [TGSI_OPCODE_FSEQ
] = { 0, tgsi_to_qir_fseq
},
956 [TGSI_OPCODE_FSNE
] = { 0, tgsi_to_qir_fsne
},
957 [TGSI_OPCODE_FSGE
] = { 0, tgsi_to_qir_fsge
},
958 [TGSI_OPCODE_FSLT
] = { 0, tgsi_to_qir_fslt
},
959 [TGSI_OPCODE_USEQ
] = { 0, tgsi_to_qir_useq
},
960 [TGSI_OPCODE_USNE
] = { 0, tgsi_to_qir_usne
},
961 [TGSI_OPCODE_ISGE
] = { 0, tgsi_to_qir_isge
},
962 [TGSI_OPCODE_ISLT
] = { 0, tgsi_to_qir_islt
},
964 [TGSI_OPCODE_CMP
] = { 0, tgsi_to_qir_cmp
},
965 [TGSI_OPCODE_MAD
] = { 0, tgsi_to_qir_mad
},
966 [TGSI_OPCODE_DP2
] = { 0, tgsi_to_qir_dp2
},
967 [TGSI_OPCODE_DP3
] = { 0, tgsi_to_qir_dp3
},
968 [TGSI_OPCODE_DP4
] = { 0, tgsi_to_qir_dp4
},
969 [TGSI_OPCODE_RCP
] = { QOP_RCP
, tgsi_to_qir_alu
},
970 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
971 [TGSI_OPCODE_EX2
] = { QOP_EXP2
, tgsi_to_qir_alu
},
972 [TGSI_OPCODE_LG2
] = { QOP_LOG2
, tgsi_to_qir_alu
},
973 [TGSI_OPCODE_LIT
] = { 0, tgsi_to_qir_lit
},
974 [TGSI_OPCODE_LRP
] = { 0, tgsi_to_qir_lrp
},
975 [TGSI_OPCODE_POW
] = { 0, tgsi_to_qir_pow
},
976 [TGSI_OPCODE_TRUNC
] = { 0, tgsi_to_qir_trunc
},
977 [TGSI_OPCODE_FRC
] = { 0, tgsi_to_qir_frc
},
978 [TGSI_OPCODE_FLR
] = { 0, tgsi_to_qir_flr
},
979 [TGSI_OPCODE_SIN
] = { 0, tgsi_to_qir_sin
},
980 [TGSI_OPCODE_COS
] = { 0, tgsi_to_qir_cos
},
983 uint32_t tgsi_op
= tgsi_inst
->Instruction
.Opcode
;
985 if (tgsi_op
== TGSI_OPCODE_END
)
988 struct qreg src_regs
[12];
989 for (int s
= 0; s
< 3; s
++) {
990 for (int i
= 0; i
< 4; i
++) {
991 src_regs
[4 * s
+ i
] =
992 get_src(c
, tgsi_inst
->Instruction
.Opcode
,
993 &tgsi_inst
->Src
[s
].Register
, i
);
998 case TGSI_OPCODE_TEX
:
999 case TGSI_OPCODE_TXP
:
1000 case TGSI_OPCODE_TXB
:
1001 tgsi_to_qir_tex(c
, tgsi_inst
,
1002 op_trans
[tgsi_op
].op
, src_regs
);
1004 case TGSI_OPCODE_KILL
:
1005 c
->discard
= qir_uniform_f(c
, 1.0);
1007 case TGSI_OPCODE_KILL_IF
:
1008 for (int i
= 0; i
< 4; i
++)
1009 tgsi_to_qir_kill_if(c
, src_regs
, i
);
1015 if (tgsi_op
> ARRAY_SIZE(op_trans
) || !(op_trans
[tgsi_op
].func
)) {
1016 fprintf(stderr
, "unknown tgsi inst: ");
1017 tgsi_dump_instruction(tgsi_inst
, asdf
++);
1018 fprintf(stderr
, "\n");
1022 for (int i
= 0; i
< 4; i
++) {
1023 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
1028 result
= op_trans
[tgsi_op
].func(c
, tgsi_inst
,
1029 op_trans
[tgsi_op
].op
,
1032 if (tgsi_inst
->Instruction
.Saturate
) {
1033 float low
= (tgsi_inst
->Instruction
.Saturate
==
1034 TGSI_SAT_MINUS_PLUS_ONE
? -1.0 : 0.0);
1035 result
= qir_FMAX(c
,
1038 qir_uniform_f(c
, 1.0)),
1039 qir_uniform_f(c
, low
));
1042 update_dst(c
, tgsi_inst
, i
, result
);
1047 parse_tgsi_immediate(struct vc4_compile
*c
, struct tgsi_full_immediate
*imm
)
1049 for (int i
= 0; i
< 4; i
++) {
1050 unsigned n
= c
->num_consts
++;
1051 resize_qreg_array(c
, &c
->consts
, &c
->consts_array_size
, n
+ 1);
1052 c
->consts
[n
] = qir_uniform_ui(c
, imm
->u
[i
].Uint
);
1057 vc4_blend_channel(struct vc4_compile
*c
,
1065 case PIPE_BLENDFACTOR_ONE
:
1067 case PIPE_BLENDFACTOR_SRC_COLOR
:
1068 return qir_FMUL(c
, val
, src
[channel
]);
1069 case PIPE_BLENDFACTOR_SRC_ALPHA
:
1070 return qir_FMUL(c
, val
, src
[3]);
1071 case PIPE_BLENDFACTOR_DST_ALPHA
:
1072 return qir_FMUL(c
, val
, dst
[3]);
1073 case PIPE_BLENDFACTOR_DST_COLOR
:
1074 return qir_FMUL(c
, val
, dst
[channel
]);
1075 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
1076 return qir_FMIN(c
, src
[3], qir_FSUB(c
,
1077 qir_uniform_f(c
, 1.0),
1079 case PIPE_BLENDFACTOR_CONST_COLOR
:
1080 return qir_FMUL(c
, val
,
1081 get_temp_for_uniform(c
,
1082 QUNIFORM_BLEND_CONST_COLOR
,
1084 case PIPE_BLENDFACTOR_CONST_ALPHA
:
1085 return qir_FMUL(c
, val
,
1086 get_temp_for_uniform(c
,
1087 QUNIFORM_BLEND_CONST_COLOR
,
1089 case PIPE_BLENDFACTOR_ZERO
:
1090 return qir_uniform_f(c
, 0.0);
1091 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
1092 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1094 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
1095 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1097 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
1098 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1100 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
1101 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1103 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
1104 return qir_FMUL(c
, val
,
1105 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1106 get_temp_for_uniform(c
,
1107 QUNIFORM_BLEND_CONST_COLOR
,
1109 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
1110 return qir_FMUL(c
, val
,
1111 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1112 get_temp_for_uniform(c
,
1113 QUNIFORM_BLEND_CONST_COLOR
,
1117 case PIPE_BLENDFACTOR_SRC1_COLOR
:
1118 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
1119 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
1120 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
1122 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
1128 vc4_blend_func(struct vc4_compile
*c
,
1129 struct qreg src
, struct qreg dst
,
1133 case PIPE_BLEND_ADD
:
1134 return qir_FADD(c
, src
, dst
);
1135 case PIPE_BLEND_SUBTRACT
:
1136 return qir_FSUB(c
, src
, dst
);
1137 case PIPE_BLEND_REVERSE_SUBTRACT
:
1138 return qir_FSUB(c
, dst
, src
);
1139 case PIPE_BLEND_MIN
:
1140 return qir_FMIN(c
, src
, dst
);
1141 case PIPE_BLEND_MAX
:
1142 return qir_FMAX(c
, src
, dst
);
1146 fprintf(stderr
, "Unknown blend func %d\n", func
);
1153 * Implements fixed function blending in shader code.
1155 * VC4 doesn't have any hardware support for blending. Instead, you read the
1156 * current contents of the destination from the tile buffer after having
1157 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
1158 * math using your output color and that destination value, and update the
1159 * output color appropriately.
1162 vc4_blend(struct vc4_compile
*c
, struct qreg
*result
,
1163 struct qreg
*dst_color
, struct qreg
*src_color
)
1165 struct pipe_rt_blend_state
*blend
= &c
->fs_key
->blend
;
1167 if (!blend
->blend_enable
) {
1168 for (int i
= 0; i
< 4; i
++)
1169 result
[i
] = src_color
[i
];
1173 struct qreg src_blend
[4], dst_blend
[4];
1174 for (int i
= 0; i
< 3; i
++) {
1175 src_blend
[i
] = vc4_blend_channel(c
,
1176 dst_color
, src_color
,
1178 blend
->rgb_src_factor
, i
);
1179 dst_blend
[i
] = vc4_blend_channel(c
,
1180 dst_color
, src_color
,
1182 blend
->rgb_dst_factor
, i
);
1184 src_blend
[3] = vc4_blend_channel(c
,
1185 dst_color
, src_color
,
1187 blend
->alpha_src_factor
, 3);
1188 dst_blend
[3] = vc4_blend_channel(c
,
1189 dst_color
, src_color
,
1191 blend
->alpha_dst_factor
, 3);
1193 for (int i
= 0; i
< 3; i
++) {
1194 result
[i
] = vc4_blend_func(c
,
1195 src_blend
[i
], dst_blend
[i
],
1198 result
[3] = vc4_blend_func(c
,
1199 src_blend
[3], dst_blend
[3],
1204 emit_frag_end(struct vc4_compile
*c
)
1206 enum pipe_format color_format
= c
->fs_key
->color_format
;
1207 const uint8_t *format_swiz
= vc4_get_format_swizzle(color_format
);
1208 struct qreg tlb_read_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1209 struct qreg dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1210 if (c
->fs_key
->blend
.blend_enable
||
1211 c
->fs_key
->blend
.colormask
!= 0xf) {
1212 struct qreg r4
= qir_TLB_COLOR_READ(c
);
1213 for (int i
= 0; i
< 4; i
++)
1214 tlb_read_color
[i
] = qir_R4_UNPACK(c
, r4
, i
);
1215 for (int i
= 0; i
< 4; i
++)
1216 dst_color
[i
] = get_swizzled_channel(c
,
1221 struct qreg blend_color
[4];
1222 struct qreg undef_array
[4] = {
1223 c
->undef
, c
->undef
, c
->undef
, c
->undef
1225 vc4_blend(c
, blend_color
, dst_color
,
1226 (c
->output_color_index
!= -1 ?
1227 c
->outputs
+ c
->output_color_index
:
1230 /* If the bit isn't set in the color mask, then just return the
1231 * original dst color, instead.
1233 for (int i
= 0; i
< 4; i
++) {
1234 if (!(c
->fs_key
->blend
.colormask
& (1 << i
))) {
1235 blend_color
[i
] = dst_color
[i
];
1239 /* Debug: Sometimes you're getting a black output and just want to see
1240 * if the FS is getting executed at all. Spam magenta into the color
1244 blend_color
[0] = qir_uniform_f(c
, 1.0);
1245 blend_color
[1] = qir_uniform_f(c
, 0.0);
1246 blend_color
[2] = qir_uniform_f(c
, 1.0);
1247 blend_color
[3] = qir_uniform_f(c
, 0.5);
1250 struct qreg swizzled_outputs
[4];
1251 for (int i
= 0; i
< 4; i
++) {
1252 swizzled_outputs
[i
] = get_swizzled_channel(c
, blend_color
,
1256 if (c
->discard
.file
!= QFILE_NULL
)
1257 qir_TLB_DISCARD_SETUP(c
, c
->discard
);
1259 if (c
->fs_key
->stencil_enabled
) {
1260 qir_TLB_STENCIL_SETUP(c
, add_uniform(c
, QUNIFORM_STENCIL
, 0));
1261 if (c
->fs_key
->stencil_twoside
) {
1262 qir_TLB_STENCIL_SETUP(c
, add_uniform(c
, QUNIFORM_STENCIL
, 1));
1264 if (c
->fs_key
->stencil_full_writemasks
) {
1265 qir_TLB_STENCIL_SETUP(c
, add_uniform(c
, QUNIFORM_STENCIL
, 2));
1269 if (c
->fs_key
->depth_enabled
) {
1271 if (c
->output_position_index
!= -1) {
1272 z
= qir_FTOI(c
, qir_FMUL(c
, c
->outputs
[c
->output_position_index
+ 2],
1273 qir_uniform_f(c
, 0xffffff)));
1277 qir_TLB_Z_WRITE(c
, z
);
1280 bool color_written
= false;
1281 for (int i
= 0; i
< 4; i
++) {
1282 if (swizzled_outputs
[i
].file
!= QFILE_NULL
)
1283 color_written
= true;
1286 struct qreg packed_color
;
1287 if (color_written
) {
1288 /* Fill in any undefined colors. The simulator will assertion
1289 * fail if we read something that wasn't written, and I don't
1290 * know what hardware does.
1292 for (int i
= 0; i
< 4; i
++) {
1293 if (swizzled_outputs
[i
].file
== QFILE_NULL
)
1294 swizzled_outputs
[i
] = qir_uniform_f(c
, 0.0);
1296 packed_color
= qir_get_temp(c
);
1297 qir_emit(c
, qir_inst4(QOP_PACK_COLORS
, packed_color
,
1298 swizzled_outputs
[0],
1299 swizzled_outputs
[1],
1300 swizzled_outputs
[2],
1301 swizzled_outputs
[3]));
1303 packed_color
= qir_uniform_ui(c
, 0);
1306 qir_emit(c
, qir_inst(QOP_TLB_COLOR_WRITE
, c
->undef
,
1307 packed_color
, c
->undef
));
1311 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1315 for (int i
= 0; i
< 2; i
++) {
1317 add_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1319 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1326 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1330 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1332 struct qreg zscale
= add_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1333 struct qreg zoffset
= add_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1335 qir_VPM_WRITE(c
, qir_FMUL(c
, qir_FADD(c
, qir_FMUL(c
,
1343 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1345 qir_VPM_WRITE(c
, rcp_w
);
1349 emit_vert_end(struct vc4_compile
*c
)
1351 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[3]);
1353 emit_scaled_viewport_write(c
, rcp_w
);
1354 emit_zs_write(c
, rcp_w
);
1355 emit_rcp_wc_write(c
, rcp_w
);
1357 for (int i
= 4; i
< c
->num_outputs
; i
++) {
1358 qir_VPM_WRITE(c
, c
->outputs
[i
]);
1363 emit_coord_end(struct vc4_compile
*c
)
1365 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[3]);
1367 for (int i
= 0; i
< 4; i
++)
1368 qir_VPM_WRITE(c
, c
->outputs
[i
]);
1370 emit_scaled_viewport_write(c
, rcp_w
);
1371 emit_zs_write(c
, rcp_w
);
1372 emit_rcp_wc_write(c
, rcp_w
);
1375 static struct vc4_compile
*
1376 vc4_shader_tgsi_to_qir(struct vc4_context
*vc4
,
1377 struct vc4_compiled_shader
*shader
, enum qstage stage
,
1378 struct vc4_key
*key
)
1380 struct vc4_compile
*c
= qir_compile_init();
1385 c
->uniform_data
= ralloc_array(c
, uint32_t, 1024);
1386 c
->uniform_contents
= ralloc_array(c
, enum quniform_contents
, 1024);
1388 c
->shader_state
= key
->shader_state
;
1389 ret
= tgsi_parse_init(&c
->parser
, c
->shader_state
->tokens
);
1390 assert(ret
== TGSI_PARSE_OK
);
1392 if (vc4_debug
& VC4_DEBUG_TGSI
) {
1393 fprintf(stderr
, "TGSI:\n");
1394 tgsi_dump(c
->shader_state
->tokens
, 0);
1400 c
->fs_key
= (struct vc4_fs_key
*)key
;
1401 if (c
->fs_key
->is_points
) {
1402 c
->point_x
= emit_fragment_varying(c
, 0);
1403 c
->point_y
= emit_fragment_varying(c
, 0);
1404 } else if (c
->fs_key
->is_lines
) {
1405 c
->line_x
= emit_fragment_varying(c
, 0);
1409 c
->vs_key
= (struct vc4_vs_key
*)key
;
1412 c
->vs_key
= (struct vc4_vs_key
*)key
;
1416 while (!tgsi_parse_end_of_tokens(&c
->parser
)) {
1417 tgsi_parse_token(&c
->parser
);
1419 switch (c
->parser
.FullToken
.Token
.Type
) {
1420 case TGSI_TOKEN_TYPE_DECLARATION
:
1421 emit_tgsi_declaration(c
,
1422 &c
->parser
.FullToken
.FullDeclaration
);
1425 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1426 emit_tgsi_instruction(c
,
1427 &c
->parser
.FullToken
.FullInstruction
);
1430 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1431 parse_tgsi_immediate(c
,
1432 &c
->parser
.FullToken
.FullImmediate
);
1449 tgsi_parse_free(&c
->parser
);
1453 if (vc4_debug
& VC4_DEBUG_QIR
) {
1454 fprintf(stderr
, "QIR:\n");
1457 qir_reorder_uniforms(c
);
1458 vc4_generate_code(vc4
, c
);
1460 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
1461 fprintf(stderr
, "SHADER-DB: %s: %d instructions\n",
1462 qir_get_stage_name(c
->stage
), c
->qpu_inst_count
);
1463 fprintf(stderr
, "SHADER-DB: %s: %d uniforms\n",
1464 qir_get_stage_name(c
->stage
), c
->num_uniforms
);
1471 vc4_shader_state_create(struct pipe_context
*pctx
,
1472 const struct pipe_shader_state
*cso
)
1474 struct pipe_shader_state
*so
= CALLOC_STRUCT(pipe_shader_state
);
1478 so
->tokens
= tgsi_dup_tokens(cso
->tokens
);
1484 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
1486 struct vc4_compile
*c
)
1488 int count
= c
->num_uniforms
;
1489 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1491 uinfo
->count
= count
;
1492 uinfo
->data
= malloc(count
* sizeof(*uinfo
->data
));
1493 memcpy(uinfo
->data
, c
->uniform_data
,
1494 count
* sizeof(*uinfo
->data
));
1495 uinfo
->contents
= malloc(count
* sizeof(*uinfo
->contents
));
1496 memcpy(uinfo
->contents
, c
->uniform_contents
,
1497 count
* sizeof(*uinfo
->contents
));
1498 uinfo
->num_texture_samples
= c
->num_texture_samples
;
1502 vc4_fs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1503 struct vc4_fs_key
*key
)
1505 struct vc4_compile
*c
= vc4_shader_tgsi_to_qir(vc4
, shader
,
1508 shader
->num_inputs
= c
->num_inputs
;
1509 copy_uniform_state_to_shader(shader
, 0, c
);
1510 shader
->bo
= vc4_bo_alloc_mem(vc4
->screen
, c
->qpu_insts
,
1511 c
->qpu_inst_count
* sizeof(uint64_t),
1514 qir_compile_destroy(c
);
1518 vc4_vs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1519 struct vc4_vs_key
*key
)
1521 struct vc4_compile
*vs_c
= vc4_shader_tgsi_to_qir(vc4
, shader
,
1524 copy_uniform_state_to_shader(shader
, 0, vs_c
);
1526 struct vc4_compile
*cs_c
= vc4_shader_tgsi_to_qir(vc4
, shader
,
1529 copy_uniform_state_to_shader(shader
, 1, cs_c
);
1531 uint32_t vs_size
= vs_c
->qpu_inst_count
* sizeof(uint64_t);
1532 uint32_t cs_size
= cs_c
->qpu_inst_count
* sizeof(uint64_t);
1533 shader
->coord_shader_offset
= vs_size
; /* XXX: alignment? */
1534 shader
->bo
= vc4_bo_alloc(vc4
->screen
,
1535 shader
->coord_shader_offset
+ cs_size
,
1538 void *map
= vc4_bo_map(shader
->bo
);
1539 memcpy(map
, vs_c
->qpu_insts
, vs_size
);
1540 memcpy(map
+ shader
->coord_shader_offset
,
1541 cs_c
->qpu_insts
, cs_size
);
1543 qir_compile_destroy(vs_c
);
1544 qir_compile_destroy(cs_c
);
1548 vc4_setup_shared_key(struct vc4_key
*key
, struct vc4_texture_stateobj
*texstate
)
1550 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
1551 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
1552 struct pipe_sampler_state
*sampler_state
=
1553 texstate
->samplers
[i
];
1556 struct pipe_resource
*prsc
= sampler
->texture
;
1557 key
->tex
[i
].format
= prsc
->format
;
1558 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
1559 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
1560 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
1561 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
1562 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
1563 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
1569 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
1571 struct vc4_fs_key local_key
;
1572 struct vc4_fs_key
*key
= &local_key
;
1574 memset(key
, 0, sizeof(*key
));
1575 vc4_setup_shared_key(&key
->base
, &vc4
->fragtex
);
1576 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
1577 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
1578 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
1579 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
1580 key
->blend
= vc4
->blend
->rt
[0];
1582 if (vc4
->framebuffer
.cbufs
[0])
1583 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
1585 key
->stencil_enabled
= vc4
->zsa
->stencil_uniforms
[0] != 0;
1586 key
->stencil_twoside
= vc4
->zsa
->stencil_uniforms
[1] != 0;
1587 key
->stencil_full_writemasks
= vc4
->zsa
->stencil_uniforms
[2] != 0;
1588 key
->depth_enabled
= (vc4
->zsa
->base
.depth
.enabled
||
1589 key
->stencil_enabled
);
1591 vc4
->prog
.fs
= util_hash_table_get(vc4
->fs_cache
, key
);
1595 key
= malloc(sizeof(*key
));
1596 memcpy(key
, &local_key
, sizeof(*key
));
1598 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1599 vc4_fs_compile(vc4
, shader
, key
);
1600 util_hash_table_set(vc4
->fs_cache
, key
, shader
);
1602 vc4
->prog
.fs
= shader
;
1606 vc4_update_compiled_vs(struct vc4_context
*vc4
)
1608 struct vc4_vs_key local_key
;
1609 struct vc4_vs_key
*key
= &local_key
;
1611 memset(key
, 0, sizeof(*key
));
1612 vc4_setup_shared_key(&key
->base
, &vc4
->verttex
);
1613 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
1615 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
1616 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
1618 vc4
->prog
.vs
= util_hash_table_get(vc4
->vs_cache
, key
);
1622 key
= malloc(sizeof(*key
));
1623 memcpy(key
, &local_key
, sizeof(*key
));
1625 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1626 vc4_vs_compile(vc4
, shader
, key
);
1627 util_hash_table_set(vc4
->vs_cache
, key
, shader
);
1629 vc4
->prog
.vs
= shader
;
1633 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
1635 vc4_update_compiled_fs(vc4
, prim_mode
);
1636 vc4_update_compiled_vs(vc4
);
1640 fs_cache_hash(void *key
)
1642 return util_hash_crc32(key
, sizeof(struct vc4_fs_key
));
1646 vs_cache_hash(void *key
)
1648 return util_hash_crc32(key
, sizeof(struct vc4_vs_key
));
1652 fs_cache_compare(void *key1
, void *key2
)
1654 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
));
1658 vs_cache_compare(void *key1
, void *key2
)
1660 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
));
1663 struct delete_state
{
1664 struct vc4_context
*vc4
;
1665 struct pipe_shader_state
*shader_state
;
1668 static enum pipe_error
1669 fs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1671 struct delete_state
*del
= data
;
1672 struct vc4_fs_key
*key
= in_key
;
1673 struct vc4_compiled_shader
*shader
= in_value
;
1675 if (key
->base
.shader_state
== data
) {
1676 util_hash_table_remove(del
->vc4
->fs_cache
, key
);
1677 vc4_bo_unreference(&shader
->bo
);
1684 static enum pipe_error
1685 vs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1687 struct delete_state
*del
= data
;
1688 struct vc4_vs_key
*key
= in_key
;
1689 struct vc4_compiled_shader
*shader
= in_value
;
1691 if (key
->base
.shader_state
== data
) {
1692 util_hash_table_remove(del
->vc4
->vs_cache
, key
);
1693 vc4_bo_unreference(&shader
->bo
);
1701 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
1703 struct vc4_context
*vc4
= vc4_context(pctx
);
1704 struct pipe_shader_state
*so
= hwcso
;
1705 struct delete_state del
;
1708 del
.shader_state
= so
;
1709 util_hash_table_foreach(vc4
->fs_cache
, fs_delete_from_cache
, &del
);
1710 util_hash_table_foreach(vc4
->vs_cache
, vs_delete_from_cache
, &del
);
1712 free((void *)so
->tokens
);
1716 static uint32_t translate_wrap(uint32_t p_wrap
)
1719 case PIPE_TEX_WRAP_REPEAT
:
1721 case PIPE_TEX_WRAP_CLAMP
:
1722 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1724 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1726 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1729 fprintf(stderr
, "Unknown wrap mode %d\n", p_wrap
);
1730 assert(!"not reached");
1736 write_texture_p0(struct vc4_context
*vc4
,
1737 struct vc4_texture_stateobj
*texstate
,
1740 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
1741 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
1743 cl_reloc(vc4
, &vc4
->uniforms
, rsc
->bo
,
1744 rsc
->slices
[0].offset
| texture
->u
.tex
.last_level
|
1745 ((rsc
->vc4_format
& 7) << 4));
1749 write_texture_p1(struct vc4_context
*vc4
,
1750 struct vc4_texture_stateobj
*texstate
,
1753 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
1754 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
1755 struct pipe_sampler_state
*sampler
= texstate
->samplers
[unit
];
1756 static const uint32_t mipfilter_map
[] = {
1757 [PIPE_TEX_MIPFILTER_NEAREST
] = 2,
1758 [PIPE_TEX_MIPFILTER_LINEAR
] = 4,
1759 [PIPE_TEX_MIPFILTER_NONE
] = 0
1761 static const uint32_t imgfilter_map
[] = {
1762 [PIPE_TEX_FILTER_NEAREST
] = 1,
1763 [PIPE_TEX_FILTER_LINEAR
] = 0,
1766 cl_u32(&vc4
->uniforms
,
1767 ((rsc
->vc4_format
>> 4) << 31) |
1768 (texture
->texture
->height0
<< 20) |
1769 (texture
->texture
->width0
<< 8) |
1770 (imgfilter_map
[sampler
->mag_img_filter
] << 7) |
1771 ((imgfilter_map
[sampler
->min_img_filter
] +
1772 mipfilter_map
[sampler
->min_mip_filter
]) << 4) |
1773 (translate_wrap(sampler
->wrap_t
) << 2) |
1774 (translate_wrap(sampler
->wrap_s
) << 0));
1778 get_texrect_scale(struct vc4_texture_stateobj
*texstate
,
1779 enum quniform_contents contents
,
1782 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
1785 if (contents
== QUNIFORM_TEXRECT_SCALE_X
)
1786 dim
= texture
->texture
->width0
;
1788 dim
= texture
->texture
->height0
;
1790 return fui(1.0f
/ dim
);
1794 vc4_write_uniforms(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1795 struct vc4_constbuf_stateobj
*cb
,
1796 struct vc4_texture_stateobj
*texstate
,
1799 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1800 const uint32_t *gallium_uniforms
= cb
->cb
[0].user_buffer
;
1802 cl_start_shader_reloc(&vc4
->uniforms
, uinfo
->num_texture_samples
);
1804 for (int i
= 0; i
< uinfo
->count
; i
++) {
1806 switch (uinfo
->contents
[i
]) {
1807 case QUNIFORM_CONSTANT
:
1808 cl_u32(&vc4
->uniforms
, uinfo
->data
[i
]);
1810 case QUNIFORM_UNIFORM
:
1811 cl_u32(&vc4
->uniforms
,
1812 gallium_uniforms
[uinfo
->data
[i
]]);
1814 case QUNIFORM_VIEWPORT_X_SCALE
:
1815 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[0] * 16.0f
);
1817 case QUNIFORM_VIEWPORT_Y_SCALE
:
1818 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[1] * 16.0f
);
1821 case QUNIFORM_VIEWPORT_Z_OFFSET
:
1822 cl_f(&vc4
->uniforms
, vc4
->viewport
.translate
[2]);
1824 case QUNIFORM_VIEWPORT_Z_SCALE
:
1825 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[2]);
1828 case QUNIFORM_TEXTURE_CONFIG_P0
:
1829 write_texture_p0(vc4
, texstate
, uinfo
->data
[i
]);
1832 case QUNIFORM_TEXTURE_CONFIG_P1
:
1833 write_texture_p1(vc4
, texstate
, uinfo
->data
[i
]);
1836 case QUNIFORM_TEXRECT_SCALE_X
:
1837 case QUNIFORM_TEXRECT_SCALE_Y
:
1838 cl_u32(&vc4
->uniforms
,
1839 get_texrect_scale(texstate
,
1844 case QUNIFORM_BLEND_CONST_COLOR
:
1845 cl_f(&vc4
->uniforms
,
1846 vc4
->blend_color
.color
[uinfo
->data
[i
]]);
1849 case QUNIFORM_STENCIL
:
1850 cl_u32(&vc4
->uniforms
,
1851 vc4
->zsa
->stencil_uniforms
[uinfo
->data
[i
]] |
1852 (uinfo
->data
[i
] <= 1 ?
1853 (vc4
->stencil_ref
.ref_value
[uinfo
->data
[i
]] << 8) :
1858 uint32_t written_val
= *(uint32_t *)(vc4
->uniforms
.next
- 4);
1859 fprintf(stderr
, "%p/%d: %d: 0x%08x (%f)\n",
1860 shader
, shader_index
, i
, written_val
, uif(written_val
));
1866 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1868 struct vc4_context
*vc4
= vc4_context(pctx
);
1869 vc4
->prog
.bind_fs
= hwcso
;
1870 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_FP
;
1871 vc4
->dirty
|= VC4_DIRTY_PROG
;
1875 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1877 struct vc4_context
*vc4
= vc4_context(pctx
);
1878 vc4
->prog
.bind_vs
= hwcso
;
1879 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_VP
;
1880 vc4
->dirty
|= VC4_DIRTY_PROG
;
1884 vc4_program_init(struct pipe_context
*pctx
)
1886 struct vc4_context
*vc4
= vc4_context(pctx
);
1888 pctx
->create_vs_state
= vc4_shader_state_create
;
1889 pctx
->delete_vs_state
= vc4_shader_state_delete
;
1891 pctx
->create_fs_state
= vc4_shader_state_create
;
1892 pctx
->delete_fs_state
= vc4_shader_state_delete
;
1894 pctx
->bind_fs_state
= vc4_fp_state_bind
;
1895 pctx
->bind_vs_state
= vc4_vp_state_bind
;
1897 vc4
->fs_cache
= util_hash_table_create(fs_cache_hash
, fs_cache_compare
);
1898 vc4
->vs_cache
= util_hash_table_create(vs_cache_hash
, vs_cache_compare
);