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
27 #include "pipe/p_state.h"
28 #include "util/u_format.h"
29 #include "util/u_hash_table.h"
30 #include "util/u_hash.h"
31 #include "util/u_memory.h"
32 #include "tgsi/tgsi_parse.h"
33 #include "tgsi/tgsi_dump.h"
35 #include "vc4_context.h"
38 #ifdef USE_VC4_SIMULATOR
39 #include "simpenrose/simpenrose.h"
43 struct tgsi_parse_context parser
;
48 struct qreg
*uniforms
;
50 struct qreg line_x
, point_x
, point_y
;
54 struct pipe_shader_state
*shader_state
;
55 struct vc4_fs_key
*fs_key
;
56 struct vc4_vs_key
*vs_key
;
58 uint32_t *uniform_data
;
59 enum quniform_contents
*uniform_contents
;
60 uint32_t num_uniforms
;
62 uint32_t num_texture_samples
;
66 struct pipe_shader_state
*shader_state
;
71 enum pipe_format color_format
;
76 struct pipe_rt_blend_state blend
;
81 enum pipe_format attr_formats
[8];
85 add_uniform(struct tgsi_to_qir
*trans
,
86 enum quniform_contents contents
,
89 uint32_t uniform
= trans
->num_uniforms
++;
90 struct qreg u
= { QFILE_UNIF
, uniform
};
92 trans
->uniform_contents
[uniform
] = contents
;
93 trans
->uniform_data
[uniform
] = data
;
99 get_temp_for_uniform(struct tgsi_to_qir
*trans
, enum quniform_contents contents
,
102 struct qcompile
*c
= trans
->c
;
104 for (int i
= 0; i
< trans
->num_uniforms
; i
++) {
105 if (trans
->uniform_contents
[i
] == contents
&&
106 trans
->uniform_data
[i
] == data
)
107 return trans
->uniforms
[i
];
110 struct qreg u
= add_uniform(trans
, contents
, data
);
111 struct qreg t
= qir_MOV(c
, u
);
113 trans
->uniforms
[u
.index
] = t
;
118 qir_uniform_ui(struct tgsi_to_qir
*trans
, uint32_t ui
)
120 return get_temp_for_uniform(trans
, QUNIFORM_CONSTANT
, ui
);
124 qir_uniform_f(struct tgsi_to_qir
*trans
, float f
)
126 return qir_uniform_ui(trans
, fui(f
));
130 get_src(struct tgsi_to_qir
*trans
, struct tgsi_src_register
*src
, int i
)
132 struct qcompile
*c
= trans
->c
;
133 struct qreg r
= c
->undef
;
153 assert(!src
->Indirect
);
158 case TGSI_FILE_TEMPORARY
:
159 r
= trans
->temps
[src
->Index
* 4 + s
];
161 case TGSI_FILE_IMMEDIATE
:
162 r
= trans
->consts
[src
->Index
* 4 + s
];
164 case TGSI_FILE_CONSTANT
:
165 r
= get_temp_for_uniform(trans
, QUNIFORM_UNIFORM
,
168 case TGSI_FILE_INPUT
:
169 r
= trans
->inputs
[src
->Index
* 4 + s
];
171 case TGSI_FILE_SAMPLER
:
172 case TGSI_FILE_SAMPLER_VIEW
:
176 fprintf(stderr
, "unknown src file %d\n", src
->File
);
181 r
= qir_FMAXABS(c
, r
, r
);
184 r
= qir_FSUB(c
, qir_uniform_f(trans
, 0), r
);
191 update_dst(struct tgsi_to_qir
*trans
, struct tgsi_full_instruction
*tgsi_inst
,
192 int i
, struct qreg val
)
194 struct tgsi_dst_register
*tgsi_dst
= &tgsi_inst
->Dst
[0].Register
;
196 assert(!tgsi_dst
->Indirect
);
198 switch (tgsi_dst
->File
) {
199 case TGSI_FILE_TEMPORARY
:
200 trans
->temps
[tgsi_dst
->Index
* 4 + i
] = val
;
202 case TGSI_FILE_OUTPUT
:
203 trans
->outputs
[tgsi_dst
->Index
* 4 + i
] = val
;
204 trans
->num_outputs
= MAX2(trans
->num_outputs
,
205 tgsi_dst
->Index
* 4 + i
+ 1);
208 fprintf(stderr
, "unknown dst file %d\n", tgsi_dst
->File
);
214 tgsi_to_qir_alu(struct tgsi_to_qir
*trans
,
215 struct tgsi_full_instruction
*tgsi_inst
,
216 enum qop op
, struct qreg
*src
, int i
)
218 struct qcompile
*c
= trans
->c
;
219 struct qreg dst
= qir_get_temp(c
);
220 qir_emit(c
, qir_inst4(op
, dst
,
229 tgsi_to_qir_mad(struct tgsi_to_qir
*trans
,
230 struct tgsi_full_instruction
*tgsi_inst
,
231 enum qop op
, struct qreg
*src
, int i
)
233 struct qcompile
*c
= trans
->c
;
242 tgsi_to_qir_lit(struct tgsi_to_qir
*trans
,
243 struct tgsi_full_instruction
*tgsi_inst
,
244 enum qop op
, struct qreg
*src
, int i
)
246 struct qcompile
*c
= trans
->c
;
247 struct qreg x
= src
[0 * 4 + 0];
248 struct qreg y
= src
[0 * 4 + 1];
249 struct qreg w
= src
[0 * 4 + 3];
254 return qir_uniform_f(trans
, 1.0);
256 return qir_FMAX(c
, src
[0 * 4 + 0], qir_uniform_f(trans
, 0.0));
258 struct qreg zero
= qir_uniform_f(trans
, 0.0);
260 /* XXX: Clamp w to -128..128 */
264 qir_EXP2(c
, qir_FMUL(c
,
272 assert(!"not reached");
278 tgsi_to_qir_lrp(struct tgsi_to_qir
*trans
,
279 struct tgsi_full_instruction
*tgsi_inst
,
280 enum qop op
, struct qreg
*src
, int i
)
282 struct qcompile
*c
= trans
->c
;
283 struct qreg src0
= src
[0 * 4 + i
];
284 struct qreg src1
= src
[1 * 4 + i
];
285 struct qreg src2
= src
[2 * 4 + i
];
288 * src0 * src1 + (1 - src0) * src2.
289 * -> src0 * src1 + src2 - src0 * src2
290 * -> src2 + src0 * (src1 - src2)
292 return qir_FADD(c
, src2
, qir_FMUL(c
, src0
, qir_FSUB(c
, src1
, src2
)));
297 tgsi_to_qir_tex(struct tgsi_to_qir
*trans
,
298 struct tgsi_full_instruction
*tgsi_inst
,
299 enum qop op
, struct qreg
*src
)
301 struct qcompile
*c
= trans
->c
;
303 assert(!tgsi_inst
->Instruction
.Saturate
);
305 struct qreg s
= src
[0 * 4 + 0];
306 struct qreg t
= src
[0 * 4 + 1];
308 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXP
) {
309 struct qreg proj
= qir_RCP(c
, src
[0 * 4 + 3]);
310 s
= qir_FMUL(c
, s
, proj
);
311 t
= qir_FMUL(c
, t
, proj
);
314 /* There is no native support for GL texture rectangle coordinates, so
315 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
318 if (tgsi_inst
->Texture
.Texture
== TGSI_TEXTURE_RECT
) {
319 uint32_t sampler
= 0; /* XXX */
321 get_temp_for_uniform(trans
,
322 QUNIFORM_TEXRECT_SCALE_X
,
325 get_temp_for_uniform(trans
,
326 QUNIFORM_TEXRECT_SCALE_Y
,
330 uint32_t tex_and_sampler
= 0; /* XXX */
331 qir_TEX_T(c
, t
, add_uniform(trans
, QUNIFORM_TEXTURE_CONFIG_P0
,
334 struct qreg sampler_p1
= add_uniform(trans
, QUNIFORM_TEXTURE_CONFIG_P1
,
336 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB
) {
337 qir_TEX_B(c
, src
[0 * 4 + 3], sampler_p1
);
338 qir_TEX_S(c
, s
, add_uniform(trans
, QUNIFORM_CONSTANT
, 0));
340 qir_TEX_S(c
, s
, sampler_p1
);
343 trans
->num_texture_samples
++;
344 qir_emit(c
, qir_inst(QOP_TEX_RESULT
, c
->undef
, c
->undef
, c
->undef
));
346 for (int i
= 0; i
< 4; i
++) {
347 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
350 struct qreg dst
= qir_get_temp(c
);
351 qir_emit(c
, qir_inst(QOP_R4_UNPACK_A
+ i
,
353 c
->undef
, c
->undef
));
354 update_dst(trans
, tgsi_inst
, i
, dst
);
359 tgsi_to_qir_pow(struct tgsi_to_qir
*trans
,
360 struct tgsi_full_instruction
*tgsi_inst
,
361 enum qop op
, struct qreg
*src
, int i
)
363 struct qcompile
*c
= trans
->c
;
365 /* Note that this instruction replicates its result from the x channel
367 return qir_EXP2(c
, qir_FMUL(c
,
369 qir_LOG2(c
, src
[0 * 4 + 0])));
373 tgsi_to_qir_trunc(struct tgsi_to_qir
*trans
,
374 struct tgsi_full_instruction
*tgsi_inst
,
375 enum qop op
, struct qreg
*src
, int i
)
377 struct qcompile
*c
= trans
->c
;
378 return qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
382 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
386 tgsi_to_qir_frc(struct tgsi_to_qir
*trans
,
387 struct tgsi_full_instruction
*tgsi_inst
,
388 enum qop op
, struct qreg
*src
, int i
)
390 struct qcompile
*c
= trans
->c
;
391 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
392 struct qreg diff
= qir_FSUB(c
, src
[0 * 4 + i
], trunc
);
395 qir_FADD(c
, diff
, qir_uniform_f(trans
, 1.0)),
400 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
404 tgsi_to_qir_flr(struct tgsi_to_qir
*trans
,
405 struct tgsi_full_instruction
*tgsi_inst
,
406 enum qop op
, struct qreg
*src
, int i
)
408 struct qcompile
*c
= trans
->c
;
409 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
412 qir_FSUB(c
, trunc
, qir_uniform_f(trans
, 1.0)),
417 tgsi_to_qir_dp(struct tgsi_to_qir
*trans
,
418 struct tgsi_full_instruction
*tgsi_inst
,
419 int num
, struct qreg
*src
, int i
)
421 struct qcompile
*c
= trans
->c
;
423 struct qreg sum
= qir_FMUL(c
, src
[0 * 4 + 0], src
[1 * 4 + 0]);
424 for (int j
= 1; j
< num
; j
++) {
425 sum
= qir_FADD(c
, sum
, qir_FMUL(c
,
433 tgsi_to_qir_dp2(struct tgsi_to_qir
*trans
,
434 struct tgsi_full_instruction
*tgsi_inst
,
435 enum qop op
, struct qreg
*src
, int i
)
437 return tgsi_to_qir_dp(trans
, tgsi_inst
, 2, src
, i
);
441 tgsi_to_qir_dp3(struct tgsi_to_qir
*trans
,
442 struct tgsi_full_instruction
*tgsi_inst
,
443 enum qop op
, struct qreg
*src
, int i
)
445 return tgsi_to_qir_dp(trans
, tgsi_inst
, 3, src
, i
);
449 tgsi_to_qir_dp4(struct tgsi_to_qir
*trans
,
450 struct tgsi_full_instruction
*tgsi_inst
,
451 enum qop op
, struct qreg
*src
, int i
)
453 return tgsi_to_qir_dp(trans
, tgsi_inst
, 4, src
, i
);
457 tgsi_to_qir_abs(struct tgsi_to_qir
*trans
,
458 struct tgsi_full_instruction
*tgsi_inst
,
459 enum qop op
, struct qreg
*src
, int i
)
461 struct qcompile
*c
= trans
->c
;
462 struct qreg arg
= src
[0 * 4 + i
];
463 return qir_FMAXABS(c
, arg
, arg
);
466 /* Note that this instruction replicates its result from the x channel */
468 tgsi_to_qir_sin(struct tgsi_to_qir
*trans
,
469 struct tgsi_full_instruction
*tgsi_inst
,
470 enum qop op
, struct qreg
*src
, int i
)
472 struct qcompile
*c
= trans
->c
;
475 -pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
476 pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
477 -pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
480 struct qreg scaled_x
=
483 qir_uniform_f(trans
, 1.0f
/ (M_PI
* 2.0f
)));
486 struct qreg x
= tgsi_to_qir_frc(trans
, NULL
, 0, &scaled_x
, 0);
487 struct qreg x2
= qir_FMUL(c
, x
, x
);
488 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(trans
, coeff
[0]));
489 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
490 x
= qir_FMUL(c
, x
, x2
);
495 qir_uniform_f(trans
, coeff
[i
])));
500 /* Note that this instruction replicates its result from the x channel */
502 tgsi_to_qir_cos(struct tgsi_to_qir
*trans
,
503 struct tgsi_full_instruction
*tgsi_inst
,
504 enum qop op
, struct qreg
*src
, int i
)
506 struct qcompile
*c
= trans
->c
;
509 -pow(2.0 * M_PI
, 2) / (2 * 1),
510 pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
511 -pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
514 struct qreg scaled_x
=
515 qir_FMUL(c
, src
[0 * 4 + 0],
516 qir_uniform_f(trans
, 1.0f
/ (M_PI
* 2.0f
)));
517 struct qreg x_frac
= tgsi_to_qir_frc(trans
, NULL
, 0, &scaled_x
, 0);
519 struct qreg sum
= qir_uniform_f(trans
, coeff
[0]);
520 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
521 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
522 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
524 x
= qir_FMUL(c
, x
, x2
);
526 struct qreg mul
= qir_FMUL(c
,
528 qir_uniform_f(trans
, coeff
[i
]));
532 sum
= qir_FADD(c
, sum
, mul
);
538 emit_vertex_input(struct tgsi_to_qir
*trans
, int attr
)
540 enum pipe_format format
= trans
->vs_key
->attr_formats
[attr
];
541 struct qcompile
*c
= trans
->c
;
542 struct qreg vpm_reads
[4];
544 /* Right now, we're setting the VPM offsets to be 16 bytes wide every
545 * time, so we always read 4 32-bit VPM entries.
547 for (int i
= 0; i
< 4; i
++) {
548 vpm_reads
[i
] = qir_get_temp(c
);
549 qir_emit(c
, qir_inst(QOP_VPM_READ
,
556 bool format_warned
= false;
557 const struct util_format_description
*desc
=
558 util_format_description(format
);
560 for (int i
= 0; i
< 4; i
++) {
561 uint8_t swiz
= desc
->swizzle
[i
];
564 case UTIL_FORMAT_SWIZZLE_NONE
:
565 if (!format_warned
) {
567 "vtx element %d NONE swizzle: %s\n",
568 attr
, util_format_name(format
));
569 format_warned
= true;
572 case UTIL_FORMAT_SWIZZLE_0
:
573 trans
->inputs
[attr
* 4 + i
] = qir_uniform_f(trans
, 0.0);
575 case UTIL_FORMAT_SWIZZLE_1
:
576 trans
->inputs
[attr
* 4 + i
] = qir_uniform_f(trans
, 1.0);
579 if (!format_warned
&&
580 (desc
->channel
[swiz
].type
!= UTIL_FORMAT_TYPE_FLOAT
||
581 desc
->channel
[swiz
].size
!= 32)) {
583 "vtx element %d unsupported type: %s\n",
584 attr
, util_format_name(format
));
585 format_warned
= true;
588 trans
->inputs
[attr
* 4 + i
] = vpm_reads
[swiz
];
595 emit_fragcoord_input(struct tgsi_to_qir
*trans
, int attr
)
597 struct qcompile
*c
= trans
->c
;
599 trans
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
600 trans
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
601 trans
->inputs
[attr
* 4 + 2] =
604 qir_uniform_f(trans
, 1.0 / 0xffffff));
605 trans
->inputs
[attr
* 4 + 3] = qir_FRAG_RCP_W(c
);
609 emit_fragment_varying(struct tgsi_to_qir
*trans
, int index
)
611 struct qcompile
*c
= trans
->c
;
618 /* XXX: multiply by W */
619 return qir_VARY_ADD_C(c
, qir_MOV(c
, vary
));
623 emit_fragment_input(struct tgsi_to_qir
*trans
, int attr
)
625 struct qcompile
*c
= trans
->c
;
627 for (int i
= 0; i
< 4; i
++) {
628 trans
->inputs
[attr
* 4 + i
] =
629 emit_fragment_varying(trans
, attr
* 4 + i
);
635 emit_tgsi_declaration(struct tgsi_to_qir
*trans
,
636 struct tgsi_full_declaration
*decl
)
638 struct qcompile
*c
= trans
->c
;
640 switch (decl
->Declaration
.File
) {
641 case TGSI_FILE_INPUT
:
642 for (int i
= decl
->Range
.First
;
643 i
<= decl
->Range
.Last
;
645 if (c
->stage
== QSTAGE_FRAG
) {
646 if (decl
->Semantic
.Name
==
647 TGSI_SEMANTIC_POSITION
) {
648 emit_fragcoord_input(trans
, i
);
650 emit_fragment_input(trans
, i
);
653 emit_vertex_input(trans
, i
);
661 emit_tgsi_instruction(struct tgsi_to_qir
*trans
,
662 struct tgsi_full_instruction
*tgsi_inst
)
664 struct qcompile
*c
= trans
->c
;
667 struct qreg (*func
)(struct tgsi_to_qir
*trans
,
668 struct tgsi_full_instruction
*tgsi_inst
,
670 struct qreg
*src
, int i
);
672 [TGSI_OPCODE_MOV
] = { QOP_MOV
, tgsi_to_qir_alu
},
673 [TGSI_OPCODE_ABS
] = { 0, tgsi_to_qir_abs
},
674 [TGSI_OPCODE_MUL
] = { QOP_FMUL
, tgsi_to_qir_alu
},
675 [TGSI_OPCODE_ADD
] = { QOP_FADD
, tgsi_to_qir_alu
},
676 [TGSI_OPCODE_SUB
] = { QOP_FSUB
, tgsi_to_qir_alu
},
677 [TGSI_OPCODE_MIN
] = { QOP_FMIN
, tgsi_to_qir_alu
},
678 [TGSI_OPCODE_MAX
] = { QOP_FMAX
, tgsi_to_qir_alu
},
679 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
680 [TGSI_OPCODE_SEQ
] = { QOP_SEQ
, tgsi_to_qir_alu
},
681 [TGSI_OPCODE_SNE
] = { QOP_SNE
, tgsi_to_qir_alu
},
682 [TGSI_OPCODE_SGE
] = { QOP_SGE
, tgsi_to_qir_alu
},
683 [TGSI_OPCODE_SLT
] = { QOP_SLT
, tgsi_to_qir_alu
},
684 [TGSI_OPCODE_CMP
] = { QOP_CMP
, tgsi_to_qir_alu
},
685 [TGSI_OPCODE_MAD
] = { 0, tgsi_to_qir_mad
},
686 [TGSI_OPCODE_DP2
] = { 0, tgsi_to_qir_dp2
},
687 [TGSI_OPCODE_DP3
] = { 0, tgsi_to_qir_dp3
},
688 [TGSI_OPCODE_DP4
] = { 0, tgsi_to_qir_dp4
},
689 [TGSI_OPCODE_RCP
] = { QOP_RCP
, tgsi_to_qir_alu
},
690 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
691 [TGSI_OPCODE_EX2
] = { QOP_EXP2
, tgsi_to_qir_alu
},
692 [TGSI_OPCODE_LG2
] = { QOP_LOG2
, tgsi_to_qir_alu
},
693 [TGSI_OPCODE_LIT
] = { 0, tgsi_to_qir_lit
},
694 [TGSI_OPCODE_LRP
] = { 0, tgsi_to_qir_lrp
},
695 [TGSI_OPCODE_POW
] = { 0, tgsi_to_qir_pow
},
696 [TGSI_OPCODE_TRUNC
] = { 0, tgsi_to_qir_trunc
},
697 [TGSI_OPCODE_FRC
] = { 0, tgsi_to_qir_frc
},
698 [TGSI_OPCODE_FLR
] = { 0, tgsi_to_qir_flr
},
699 [TGSI_OPCODE_SIN
] = { 0, tgsi_to_qir_sin
},
700 [TGSI_OPCODE_COS
] = { 0, tgsi_to_qir_cos
},
703 uint32_t tgsi_op
= tgsi_inst
->Instruction
.Opcode
;
705 if (tgsi_op
== TGSI_OPCODE_END
)
708 struct qreg src_regs
[12];
709 for (int s
= 0; s
< 3; s
++) {
710 for (int i
= 0; i
< 4; i
++) {
711 src_regs
[4 * s
+ i
] =
712 get_src(trans
, &tgsi_inst
->Src
[s
].Register
, i
);
717 case TGSI_OPCODE_TEX
:
718 case TGSI_OPCODE_TXP
:
719 case TGSI_OPCODE_TXB
:
720 tgsi_to_qir_tex(trans
, tgsi_inst
,
721 op_trans
[tgsi_op
].op
, src_regs
);
727 if (tgsi_op
> ARRAY_SIZE(op_trans
) || !(op_trans
[tgsi_op
].func
)) {
728 fprintf(stderr
, "unknown tgsi inst: ");
729 tgsi_dump_instruction(tgsi_inst
, asdf
++);
730 fprintf(stderr
, "\n");
734 for (int i
= 0; i
< 4; i
++) {
735 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
740 result
= op_trans
[tgsi_op
].func(trans
, tgsi_inst
,
741 op_trans
[tgsi_op
].op
,
744 if (tgsi_inst
->Instruction
.Saturate
) {
745 float low
= (tgsi_inst
->Instruction
.Saturate
==
746 TGSI_SAT_MINUS_PLUS_ONE
? -1.0 : 0.0);
750 qir_uniform_f(trans
, 1.0)),
751 qir_uniform_f(trans
, low
));
754 update_dst(trans
, tgsi_inst
, i
, result
);
759 parse_tgsi_immediate(struct tgsi_to_qir
*trans
, struct tgsi_full_immediate
*imm
)
761 for (int i
= 0; i
< 4; i
++) {
762 unsigned n
= trans
->num_consts
++;
763 trans
->consts
[n
] = qir_uniform_ui(trans
, imm
->u
[i
].Uint
);
768 vc4_blend_channel(struct tgsi_to_qir
*trans
,
775 struct qcompile
*c
= trans
->c
;
778 case PIPE_BLENDFACTOR_ONE
:
780 case PIPE_BLENDFACTOR_SRC_COLOR
:
781 return qir_FMUL(c
, val
, src
[channel
]);
782 case PIPE_BLENDFACTOR_SRC_ALPHA
:
783 return qir_FMUL(c
, val
, src
[3]);
784 case PIPE_BLENDFACTOR_DST_ALPHA
:
785 return qir_FMUL(c
, val
, dst
[3]);
786 case PIPE_BLENDFACTOR_DST_COLOR
:
787 return qir_FMUL(c
, val
, dst
[channel
]);
788 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
789 return qir_FMIN(c
, src
[3], qir_FSUB(c
,
790 qir_uniform_f(trans
, 1.0),
792 case PIPE_BLENDFACTOR_CONST_COLOR
:
793 return qir_FMUL(c
, val
,
794 get_temp_for_uniform(trans
,
795 QUNIFORM_BLEND_CONST_COLOR
,
797 case PIPE_BLENDFACTOR_CONST_ALPHA
:
798 return qir_FMUL(c
, val
,
799 get_temp_for_uniform(trans
,
800 QUNIFORM_BLEND_CONST_COLOR
,
802 case PIPE_BLENDFACTOR_ZERO
:
803 return qir_uniform_f(trans
, 0.0);
804 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
805 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
807 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
808 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
810 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
811 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
813 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
814 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
816 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
817 return qir_FMUL(c
, val
,
818 qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
819 get_temp_for_uniform(trans
,
820 QUNIFORM_BLEND_CONST_COLOR
,
822 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
823 return qir_FMUL(c
, val
,
824 qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
825 get_temp_for_uniform(trans
,
826 QUNIFORM_BLEND_CONST_COLOR
,
830 case PIPE_BLENDFACTOR_SRC1_COLOR
:
831 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
832 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
833 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
835 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
841 vc4_blend_func(struct tgsi_to_qir
*trans
,
842 struct qreg src
, struct qreg dst
,
845 struct qcompile
*c
= trans
->c
;
849 return qir_FADD(c
, src
, dst
);
850 case PIPE_BLEND_SUBTRACT
:
851 return qir_FSUB(c
, src
, dst
);
852 case PIPE_BLEND_REVERSE_SUBTRACT
:
853 return qir_FSUB(c
, dst
, src
);
855 return qir_FMIN(c
, src
, dst
);
857 return qir_FMAX(c
, src
, dst
);
861 fprintf(stderr
, "Unknown blend func %d\n", func
);
868 * Implements fixed function blending in shader code.
870 * VC4 doesn't have any hardware support for blending. Instead, you read the
871 * current contents of the destination from the tile buffer after having
872 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
873 * math using your output color and that destination value, and update the
874 * output color appropriately.
877 vc4_blend(struct tgsi_to_qir
*trans
, struct qreg
*result
,
878 struct qreg
*src_color
)
880 struct qcompile
*c
= trans
->c
;
881 struct pipe_rt_blend_state
*blend
= &trans
->fs_key
->blend
;
883 if (!blend
->blend_enable
) {
884 for (int i
= 0; i
< 4; i
++)
885 result
[i
] = src_color
[i
];
889 qir_emit(c
, qir_inst(QOP_TLB_COLOR_READ
, c
->undef
,
890 c
->undef
, c
->undef
));
891 struct qreg dst_color
[4];
892 for (int i
= 0; i
< 4; i
++) {
893 dst_color
[i
] = qir_get_temp(c
);
894 qir_emit(c
, qir_inst(QOP_R4_UNPACK_A
+ i
,
896 c
->undef
, c
->undef
));
900 struct qreg src_blend
[4], dst_blend
[4];
901 for (int i
= 0; i
< 3; i
++) {
902 src_blend
[i
] = vc4_blend_channel(trans
,
903 dst_color
, src_color
,
905 blend
->rgb_src_factor
, i
);
906 dst_blend
[i
] = vc4_blend_channel(trans
,
907 dst_color
, src_color
,
909 blend
->rgb_dst_factor
, i
);
911 src_blend
[3] = vc4_blend_channel(trans
,
912 dst_color
, src_color
,
914 blend
->alpha_src_factor
, 3);
915 dst_blend
[3] = vc4_blend_channel(trans
,
916 dst_color
, src_color
,
918 blend
->alpha_dst_factor
, 3);
920 for (int i
= 0; i
< 3; i
++) {
921 result
[i
] = vc4_blend_func(trans
,
922 src_blend
[i
], dst_blend
[i
],
925 result
[3] = vc4_blend_func(trans
,
926 src_blend
[3], dst_blend
[3],
931 emit_frag_end(struct tgsi_to_qir
*trans
)
933 struct qcompile
*c
= trans
->c
;
935 struct qreg t
= qir_get_temp(c
);
937 const struct util_format_description
*format_desc
=
938 util_format_description(trans
->fs_key
->color_format
);
940 struct qreg output_color
[4] = {
941 trans
->outputs
[0], trans
->outputs
[1],
942 trans
->outputs
[2], trans
->outputs
[3],
945 struct qreg blend_color
[4];
946 vc4_blend(trans
, blend_color
, output_color
);
948 /* Debug: Sometimes you're getting a black output and just want to see
949 * if the FS is getting executed at all. Spam magenta into the color
953 blend_color
[0] = qir_uniform_f(trans
, 1.0);
954 blend_color
[1] = qir_uniform_f(trans
, 0.0);
955 blend_color
[2] = qir_uniform_f(trans
, 1.0);
956 blend_color
[3] = qir_uniform_f(trans
, 0.5);
959 struct qreg swizzled_outputs
[4] = {
960 blend_color
[format_desc
->swizzle
[0]],
961 blend_color
[format_desc
->swizzle
[1]],
962 blend_color
[format_desc
->swizzle
[2]],
963 blend_color
[format_desc
->swizzle
[3]],
966 if (trans
->fs_key
->depth_enabled
) {
967 qir_emit(c
, qir_inst(QOP_TLB_PASSTHROUGH_Z_WRITE
, c
->undef
,
968 c
->undef
, c
->undef
));
971 qir_emit(c
, qir_inst4(QOP_PACK_COLORS
, t
,
975 swizzled_outputs
[3]));
976 qir_emit(c
, qir_inst(QOP_TLB_COLOR_WRITE
, c
->undef
,
981 emit_scaled_viewport_write(struct tgsi_to_qir
*trans
, struct qreg rcp_w
)
983 struct qcompile
*c
= trans
->c
;
986 for (int i
= 0; i
< 2; i
++) {
988 add_uniform(trans
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
990 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
997 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1001 emit_zs_write(struct tgsi_to_qir
*trans
, struct qreg rcp_w
)
1003 struct qcompile
*c
= trans
->c
;
1005 struct qreg zscale
= add_uniform(trans
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1006 struct qreg zoffset
= add_uniform(trans
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1008 qir_VPM_WRITE(c
, qir_FMUL(c
, qir_FADD(c
, qir_FMUL(c
,
1016 emit_rcp_wc_write(struct tgsi_to_qir
*trans
, struct qreg rcp_w
)
1018 struct qcompile
*c
= trans
->c
;
1020 qir_VPM_WRITE(c
, rcp_w
);
1024 emit_vert_end(struct tgsi_to_qir
*trans
)
1026 struct qcompile
*c
= trans
->c
;
1028 struct qreg rcp_w
= qir_RCP(c
, trans
->outputs
[3]);
1030 emit_scaled_viewport_write(trans
, rcp_w
);
1031 emit_zs_write(trans
, rcp_w
);
1032 emit_rcp_wc_write(trans
, rcp_w
);
1034 for (int i
= 4; i
< trans
->num_outputs
; i
++) {
1035 qir_VPM_WRITE(c
, trans
->outputs
[i
]);
1040 emit_coord_end(struct tgsi_to_qir
*trans
)
1042 struct qcompile
*c
= trans
->c
;
1044 struct qreg rcp_w
= qir_RCP(c
, trans
->outputs
[3]);
1046 for (int i
= 0; i
< 4; i
++)
1047 qir_VPM_WRITE(c
, trans
->outputs
[i
]);
1049 emit_scaled_viewport_write(trans
, rcp_w
);
1050 emit_zs_write(trans
, rcp_w
);
1051 emit_rcp_wc_write(trans
, rcp_w
);
1054 static struct tgsi_to_qir
*
1055 vc4_shader_tgsi_to_qir(struct vc4_compiled_shader
*shader
, enum qstage stage
,
1056 struct vc4_key
*key
)
1058 struct tgsi_to_qir
*trans
= CALLOC_STRUCT(tgsi_to_qir
);
1062 c
= qir_compile_init();
1065 memset(trans
, 0, sizeof(*trans
));
1067 trans
->temps
= calloc(sizeof(struct qreg
), 1024);
1068 trans
->inputs
= calloc(sizeof(struct qreg
), 8 * 4);
1069 trans
->outputs
= calloc(sizeof(struct qreg
), 1024);
1070 trans
->uniforms
= calloc(sizeof(struct qreg
), 1024);
1071 trans
->consts
= calloc(sizeof(struct qreg
), 1024);
1073 trans
->uniform_data
= calloc(sizeof(uint32_t), 1024);
1074 trans
->uniform_contents
= calloc(sizeof(enum quniform_contents
), 1024);
1076 trans
->shader_state
= key
->shader_state
;
1078 ret
= tgsi_parse_init(&trans
->parser
, trans
->shader_state
->tokens
);
1079 assert(ret
== TGSI_PARSE_OK
);
1081 if (vc4_debug
& VC4_DEBUG_TGSI
) {
1082 fprintf(stderr
, "TGSI:\n");
1083 tgsi_dump(trans
->shader_state
->tokens
, 0);
1088 trans
->fs_key
= (struct vc4_fs_key
*)key
;
1089 if (trans
->fs_key
->is_points
) {
1090 trans
->point_x
= emit_fragment_varying(trans
, 0);
1091 trans
->point_y
= emit_fragment_varying(trans
, 0);
1092 } else if (trans
->fs_key
->is_lines
) {
1093 trans
->line_x
= emit_fragment_varying(trans
, 0);
1097 trans
->vs_key
= (struct vc4_vs_key
*)key
;
1100 trans
->vs_key
= (struct vc4_vs_key
*)key
;
1104 while (!tgsi_parse_end_of_tokens(&trans
->parser
)) {
1105 tgsi_parse_token(&trans
->parser
);
1107 switch (trans
->parser
.FullToken
.Token
.Type
) {
1108 case TGSI_TOKEN_TYPE_DECLARATION
:
1109 emit_tgsi_declaration(trans
,
1110 &trans
->parser
.FullToken
.FullDeclaration
);
1113 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1114 emit_tgsi_instruction(trans
,
1115 &trans
->parser
.FullToken
.FullInstruction
);
1118 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1119 parse_tgsi_immediate(trans
,
1120 &trans
->parser
.FullToken
.FullImmediate
);
1127 emit_frag_end(trans
);
1130 emit_vert_end(trans
);
1133 emit_coord_end(trans
);
1137 tgsi_parse_free(&trans
->parser
);
1142 if (vc4_debug
& VC4_DEBUG_QIR
) {
1143 fprintf(stderr
, "QIR:\n");
1146 vc4_generate_code(c
);
1148 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
1149 fprintf(stderr
, "SHADER-DB: %s: %d instructions\n",
1150 qir_get_stage_name(c
->stage
), c
->qpu_inst_count
);
1151 fprintf(stderr
, "SHADER-DB: %s: %d uniforms\n",
1152 qir_get_stage_name(c
->stage
), trans
->num_uniforms
);
1159 vc4_shader_state_create(struct pipe_context
*pctx
,
1160 const struct pipe_shader_state
*cso
)
1162 struct pipe_shader_state
*so
= CALLOC_STRUCT(pipe_shader_state
);
1166 so
->tokens
= tgsi_dup_tokens(cso
->tokens
);
1172 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
1174 struct tgsi_to_qir
*trans
)
1176 int count
= trans
->num_uniforms
;
1177 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1179 uinfo
->count
= count
;
1180 uinfo
->data
= malloc(count
* sizeof(*uinfo
->data
));
1181 memcpy(uinfo
->data
, trans
->uniform_data
,
1182 count
* sizeof(*uinfo
->data
));
1183 uinfo
->contents
= malloc(count
* sizeof(*uinfo
->contents
));
1184 memcpy(uinfo
->contents
, trans
->uniform_contents
,
1185 count
* sizeof(*uinfo
->contents
));
1186 uinfo
->num_texture_samples
= trans
->num_texture_samples
;
1190 vc4_fs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1191 struct vc4_fs_key
*key
)
1193 struct tgsi_to_qir
*trans
= vc4_shader_tgsi_to_qir(shader
, QSTAGE_FRAG
,
1195 shader
->num_inputs
= trans
->c
->num_inputs
;
1196 copy_uniform_state_to_shader(shader
, 0, trans
);
1197 shader
->bo
= vc4_bo_alloc_mem(vc4
->screen
, trans
->c
->qpu_insts
,
1198 trans
->c
->qpu_inst_count
* sizeof(uint64_t),
1201 qir_compile_destroy(trans
->c
);
1206 vc4_vs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1207 struct vc4_vs_key
*key
)
1209 struct tgsi_to_qir
*vs_trans
= vc4_shader_tgsi_to_qir(shader
,
1212 copy_uniform_state_to_shader(shader
, 0, vs_trans
);
1214 struct tgsi_to_qir
*cs_trans
= vc4_shader_tgsi_to_qir(shader
,
1217 copy_uniform_state_to_shader(shader
, 1, cs_trans
);
1219 uint32_t vs_size
= vs_trans
->c
->qpu_inst_count
* sizeof(uint64_t);
1220 uint32_t cs_size
= cs_trans
->c
->qpu_inst_count
* sizeof(uint64_t);
1221 shader
->coord_shader_offset
= vs_size
; /* XXX: alignment? */
1222 shader
->bo
= vc4_bo_alloc(vc4
->screen
,
1223 shader
->coord_shader_offset
+ cs_size
,
1226 void *map
= vc4_bo_map(shader
->bo
);
1227 memcpy(map
, vs_trans
->c
->qpu_insts
, vs_size
);
1228 memcpy(map
+ shader
->coord_shader_offset
,
1229 cs_trans
->c
->qpu_insts
, cs_size
);
1231 qir_compile_destroy(vs_trans
->c
);
1232 qir_compile_destroy(cs_trans
->c
);
1236 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
1238 struct vc4_fs_key local_key
;
1239 struct vc4_fs_key
*key
= &local_key
;
1241 memset(key
, 0, sizeof(*key
));
1242 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
1243 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
1244 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
1245 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
1246 key
->blend
= vc4
->blend
->rt
[0];
1248 if (vc4
->framebuffer
.cbufs
[0])
1249 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
1251 key
->depth_enabled
= vc4
->zsa
->base
.depth
.enabled
;
1253 vc4
->prog
.fs
= util_hash_table_get(vc4
->fs_cache
, key
);
1257 key
= malloc(sizeof(*key
));
1258 memcpy(key
, &local_key
, sizeof(*key
));
1260 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1261 vc4_fs_compile(vc4
, shader
, key
);
1262 util_hash_table_set(vc4
->fs_cache
, key
, shader
);
1264 vc4
->prog
.fs
= shader
;
1268 vc4_update_compiled_vs(struct vc4_context
*vc4
)
1270 struct vc4_vs_key local_key
;
1271 struct vc4_vs_key
*key
= &local_key
;
1273 memset(key
, 0, sizeof(*key
));
1274 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
1276 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
1277 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
1279 vc4
->prog
.vs
= util_hash_table_get(vc4
->vs_cache
, key
);
1283 key
= malloc(sizeof(*key
));
1284 memcpy(key
, &local_key
, sizeof(*key
));
1286 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1287 vc4_vs_compile(vc4
, shader
, key
);
1288 util_hash_table_set(vc4
->vs_cache
, key
, shader
);
1290 vc4
->prog
.vs
= shader
;
1294 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
1296 vc4_update_compiled_fs(vc4
, prim_mode
);
1297 vc4_update_compiled_vs(vc4
);
1301 fs_cache_hash(void *key
)
1303 return util_hash_crc32(key
, sizeof(struct vc4_fs_key
));
1307 vs_cache_hash(void *key
)
1309 return util_hash_crc32(key
, sizeof(struct vc4_vs_key
));
1313 fs_cache_compare(void *key1
, void *key2
)
1315 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
));
1319 vs_cache_compare(void *key1
, void *key2
)
1321 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
));
1324 struct delete_state
{
1325 struct vc4_context
*vc4
;
1326 struct pipe_shader_state
*shader_state
;
1329 static enum pipe_error
1330 fs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1332 struct delete_state
*del
= data
;
1333 struct vc4_fs_key
*key
= in_key
;
1334 struct vc4_compiled_shader
*shader
= in_value
;
1336 if (key
->base
.shader_state
== data
) {
1337 util_hash_table_remove(del
->vc4
->fs_cache
, key
);
1338 vc4_bo_unreference(&shader
->bo
);
1345 static enum pipe_error
1346 vs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1348 struct delete_state
*del
= data
;
1349 struct vc4_vs_key
*key
= in_key
;
1350 struct vc4_compiled_shader
*shader
= in_value
;
1352 if (key
->base
.shader_state
== data
) {
1353 util_hash_table_remove(del
->vc4
->vs_cache
, key
);
1354 vc4_bo_unreference(&shader
->bo
);
1362 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
1364 struct vc4_context
*vc4
= vc4_context(pctx
);
1365 struct pipe_shader_state
*so
= hwcso
;
1366 struct delete_state del
;
1369 del
.shader_state
= so
;
1370 util_hash_table_foreach(vc4
->fs_cache
, fs_delete_from_cache
, &del
);
1371 util_hash_table_foreach(vc4
->vs_cache
, vs_delete_from_cache
, &del
);
1373 free((void *)so
->tokens
);
1377 static uint32_t translate_wrap(uint32_t p_wrap
)
1380 case PIPE_TEX_WRAP_REPEAT
:
1382 case PIPE_TEX_WRAP_CLAMP
:
1383 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1385 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1387 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1390 fprintf(stderr
, "Unknown wrap mode %d\n", p_wrap
);
1391 assert(!"not reached");
1397 write_texture_p0(struct vc4_context
*vc4
,
1398 struct vc4_texture_stateobj
*texstate
,
1399 uint32_t tex_and_sampler
)
1401 uint32_t texi
= (tex_and_sampler
>> 0) & 0xff;
1402 struct pipe_sampler_view
*texture
= texstate
->textures
[texi
];
1403 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
1405 cl_reloc(vc4
, &vc4
->uniforms
, rsc
->bo
,
1406 texture
->u
.tex
.last_level
);
1410 write_texture_p1(struct vc4_context
*vc4
,
1411 struct vc4_texture_stateobj
*texstate
,
1412 uint32_t tex_and_sampler
)
1414 uint32_t texi
= (tex_and_sampler
>> 0) & 0xff;
1415 uint32_t sampi
= (tex_and_sampler
>> 8) & 0xff;
1416 struct pipe_sampler_view
*texture
= texstate
->textures
[texi
];
1417 struct pipe_sampler_state
*sampler
= texstate
->samplers
[sampi
];
1418 static const uint32_t mipfilter_map
[] = {
1419 [PIPE_TEX_MIPFILTER_NEAREST
] = 2,
1420 [PIPE_TEX_MIPFILTER_LINEAR
] = 4,
1421 [PIPE_TEX_MIPFILTER_NONE
] = 0
1423 static const uint32_t imgfilter_map
[] = {
1424 [PIPE_TEX_FILTER_NEAREST
] = 1,
1425 [PIPE_TEX_FILTER_LINEAR
] = 0,
1428 cl_u32(&vc4
->uniforms
,
1429 (1 << 31) /* XXX: data type */|
1430 (texture
->texture
->height0
<< 20) |
1431 (texture
->texture
->width0
<< 8) |
1432 (imgfilter_map
[sampler
->mag_img_filter
] << 7) |
1433 ((imgfilter_map
[sampler
->min_img_filter
] +
1434 mipfilter_map
[sampler
->min_mip_filter
]) << 4) |
1435 (translate_wrap(sampler
->wrap_t
) << 2) |
1436 (translate_wrap(sampler
->wrap_s
) << 0));
1440 get_texrect_scale(struct vc4_texture_stateobj
*texstate
,
1441 enum quniform_contents contents
,
1444 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
1447 if (contents
== QUNIFORM_TEXRECT_SCALE_X
)
1448 dim
= texture
->texture
->width0
;
1450 dim
= texture
->texture
->height0
;
1452 return fui(1.0f
/ dim
);
1456 vc4_write_uniforms(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1457 struct vc4_constbuf_stateobj
*cb
,
1458 struct vc4_texture_stateobj
*texstate
,
1461 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1462 const uint32_t *gallium_uniforms
= cb
->cb
[0].user_buffer
;
1464 cl_start_shader_reloc(&vc4
->uniforms
, uinfo
->num_texture_samples
);
1466 for (int i
= 0; i
< uinfo
->count
; i
++) {
1468 switch (uinfo
->contents
[i
]) {
1469 case QUNIFORM_CONSTANT
:
1470 cl_u32(&vc4
->uniforms
, uinfo
->data
[i
]);
1472 case QUNIFORM_UNIFORM
:
1473 cl_u32(&vc4
->uniforms
,
1474 gallium_uniforms
[uinfo
->data
[i
]]);
1476 case QUNIFORM_VIEWPORT_X_SCALE
:
1477 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[0] * 16.0f
);
1479 case QUNIFORM_VIEWPORT_Y_SCALE
:
1480 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[1] * 16.0f
);
1483 case QUNIFORM_VIEWPORT_Z_OFFSET
:
1484 cl_f(&vc4
->uniforms
, vc4
->viewport
.translate
[2]);
1486 case QUNIFORM_VIEWPORT_Z_SCALE
:
1487 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[2]);
1490 case QUNIFORM_TEXTURE_CONFIG_P0
:
1491 write_texture_p0(vc4
, texstate
, uinfo
->data
[i
]);
1494 case QUNIFORM_TEXTURE_CONFIG_P1
:
1495 write_texture_p1(vc4
, texstate
, uinfo
->data
[i
]);
1498 case QUNIFORM_TEXRECT_SCALE_X
:
1499 case QUNIFORM_TEXRECT_SCALE_Y
:
1500 cl_u32(&vc4
->uniforms
,
1501 get_texrect_scale(texstate
,
1506 case QUNIFORM_BLEND_CONST_COLOR
:
1507 cl_f(&vc4
->uniforms
,
1508 vc4
->blend_color
.color
[uinfo
->data
[i
]]);
1512 uint32_t written_val
= *(uint32_t *)(vc4
->uniforms
.next
- 4);
1513 fprintf(stderr
, "%p/%d: %d: 0x%08x (%f)\n",
1514 shader
, shader_index
, i
, written_val
, uif(written_val
));
1520 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1522 struct vc4_context
*vc4
= vc4_context(pctx
);
1523 vc4
->prog
.bind_fs
= hwcso
;
1524 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_FP
;
1525 vc4
->dirty
|= VC4_DIRTY_PROG
;
1529 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1531 struct vc4_context
*vc4
= vc4_context(pctx
);
1532 vc4
->prog
.bind_vs
= hwcso
;
1533 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_VP
;
1534 vc4
->dirty
|= VC4_DIRTY_PROG
;
1538 vc4_program_init(struct pipe_context
*pctx
)
1540 struct vc4_context
*vc4
= vc4_context(pctx
);
1542 pctx
->create_vs_state
= vc4_shader_state_create
;
1543 pctx
->delete_vs_state
= vc4_shader_state_delete
;
1545 pctx
->create_fs_state
= vc4_shader_state_create
;
1546 pctx
->delete_fs_state
= vc4_shader_state_delete
;
1548 pctx
->bind_fs_state
= vc4_fp_state_bind
;
1549 pctx
->bind_vs_state
= vc4_vp_state_bind
;
1551 vc4
->fs_cache
= util_hash_table_create(fs_cache_hash
, fs_cache_compare
);
1552 vc4
->vs_cache
= util_hash_table_create(vs_cache_hash
, vs_cache_compare
);