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 "tgsi/tgsi_parse.h"
32 #include "tgsi/tgsi_dump.h"
34 #include "vc4_context.h"
37 #ifdef USE_VC4_SIMULATOR
38 #include "simpenrose/simpenrose.h"
42 struct tgsi_parse_context parser
;
47 struct qreg
*uniforms
;
49 struct qreg line_x
, point_x
, point_y
;
53 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
;
67 enum pipe_format tex_format
[VC4_MAX_TEXTURE_SAMPLERS
];
72 enum pipe_format color_format
;
77 struct pipe_rt_blend_state blend
;
82 enum pipe_format attr_formats
[8];
86 add_uniform(struct tgsi_to_qir
*trans
,
87 enum quniform_contents contents
,
90 uint32_t uniform
= trans
->num_uniforms
++;
91 struct qreg u
= { QFILE_UNIF
, uniform
};
93 trans
->uniform_contents
[uniform
] = contents
;
94 trans
->uniform_data
[uniform
] = data
;
100 get_temp_for_uniform(struct tgsi_to_qir
*trans
, enum quniform_contents contents
,
103 struct qcompile
*c
= trans
->c
;
105 for (int i
= 0; i
< trans
->num_uniforms
; i
++) {
106 if (trans
->uniform_contents
[i
] == contents
&&
107 trans
->uniform_data
[i
] == data
)
108 return trans
->uniforms
[i
];
111 struct qreg u
= add_uniform(trans
, contents
, data
);
112 struct qreg t
= qir_MOV(c
, u
);
114 trans
->uniforms
[u
.index
] = t
;
119 qir_uniform_ui(struct tgsi_to_qir
*trans
, uint32_t ui
)
121 return get_temp_for_uniform(trans
, QUNIFORM_CONSTANT
, ui
);
125 qir_uniform_f(struct tgsi_to_qir
*trans
, float f
)
127 return qir_uniform_ui(trans
, fui(f
));
131 get_src(struct tgsi_to_qir
*trans
, struct tgsi_src_register
*src
, int i
)
133 struct qcompile
*c
= trans
->c
;
134 struct qreg r
= c
->undef
;
154 assert(!src
->Indirect
);
159 case TGSI_FILE_TEMPORARY
:
160 r
= trans
->temps
[src
->Index
* 4 + s
];
162 case TGSI_FILE_IMMEDIATE
:
163 r
= trans
->consts
[src
->Index
* 4 + s
];
165 case TGSI_FILE_CONSTANT
:
166 r
= get_temp_for_uniform(trans
, QUNIFORM_UNIFORM
,
169 case TGSI_FILE_INPUT
:
170 r
= trans
->inputs
[src
->Index
* 4 + s
];
172 case TGSI_FILE_SAMPLER
:
173 case TGSI_FILE_SAMPLER_VIEW
:
177 fprintf(stderr
, "unknown src file %d\n", src
->File
);
182 r
= qir_FMAXABS(c
, r
, r
);
185 r
= qir_FSUB(c
, qir_uniform_f(trans
, 0), r
);
192 update_dst(struct tgsi_to_qir
*trans
, struct tgsi_full_instruction
*tgsi_inst
,
193 int i
, struct qreg val
)
195 struct tgsi_dst_register
*tgsi_dst
= &tgsi_inst
->Dst
[0].Register
;
197 assert(!tgsi_dst
->Indirect
);
199 switch (tgsi_dst
->File
) {
200 case TGSI_FILE_TEMPORARY
:
201 trans
->temps
[tgsi_dst
->Index
* 4 + i
] = val
;
203 case TGSI_FILE_OUTPUT
:
204 trans
->outputs
[tgsi_dst
->Index
* 4 + i
] = val
;
205 trans
->num_outputs
= MAX2(trans
->num_outputs
,
206 tgsi_dst
->Index
* 4 + i
+ 1);
209 fprintf(stderr
, "unknown dst file %d\n", tgsi_dst
->File
);
215 get_swizzled_channel(struct tgsi_to_qir
*trans
,
216 struct qreg
*srcs
, int swiz
)
220 case UTIL_FORMAT_SWIZZLE_NONE
:
221 fprintf(stderr
, "warning: unknown swizzle\n");
223 case UTIL_FORMAT_SWIZZLE_0
:
224 return qir_uniform_f(trans
, 0.0);
225 case UTIL_FORMAT_SWIZZLE_1
:
226 return qir_uniform_f(trans
, 1.0);
227 case UTIL_FORMAT_SWIZZLE_X
:
228 case UTIL_FORMAT_SWIZZLE_Y
:
229 case UTIL_FORMAT_SWIZZLE_Z
:
230 case UTIL_FORMAT_SWIZZLE_W
:
236 tgsi_to_qir_alu(struct tgsi_to_qir
*trans
,
237 struct tgsi_full_instruction
*tgsi_inst
,
238 enum qop op
, struct qreg
*src
, int i
)
240 struct qcompile
*c
= trans
->c
;
241 struct qreg dst
= qir_get_temp(c
);
242 qir_emit(c
, qir_inst4(op
, dst
,
251 tgsi_to_qir_mad(struct tgsi_to_qir
*trans
,
252 struct tgsi_full_instruction
*tgsi_inst
,
253 enum qop op
, struct qreg
*src
, int i
)
255 struct qcompile
*c
= trans
->c
;
264 tgsi_to_qir_lit(struct tgsi_to_qir
*trans
,
265 struct tgsi_full_instruction
*tgsi_inst
,
266 enum qop op
, struct qreg
*src
, int i
)
268 struct qcompile
*c
= trans
->c
;
269 struct qreg x
= src
[0 * 4 + 0];
270 struct qreg y
= src
[0 * 4 + 1];
271 struct qreg w
= src
[0 * 4 + 3];
276 return qir_uniform_f(trans
, 1.0);
278 return qir_FMAX(c
, src
[0 * 4 + 0], qir_uniform_f(trans
, 0.0));
280 struct qreg zero
= qir_uniform_f(trans
, 0.0);
282 /* XXX: Clamp w to -128..128 */
286 qir_EXP2(c
, qir_FMUL(c
,
294 assert(!"not reached");
300 tgsi_to_qir_lrp(struct tgsi_to_qir
*trans
,
301 struct tgsi_full_instruction
*tgsi_inst
,
302 enum qop op
, struct qreg
*src
, int i
)
304 struct qcompile
*c
= trans
->c
;
305 struct qreg src0
= src
[0 * 4 + i
];
306 struct qreg src1
= src
[1 * 4 + i
];
307 struct qreg src2
= src
[2 * 4 + i
];
310 * src0 * src1 + (1 - src0) * src2.
311 * -> src0 * src1 + src2 - src0 * src2
312 * -> src2 + src0 * (src1 - src2)
314 return qir_FADD(c
, src2
, qir_FMUL(c
, src0
, qir_FSUB(c
, src1
, src2
)));
319 tgsi_to_qir_tex(struct tgsi_to_qir
*trans
,
320 struct tgsi_full_instruction
*tgsi_inst
,
321 enum qop op
, struct qreg
*src
)
323 struct qcompile
*c
= trans
->c
;
325 assert(!tgsi_inst
->Instruction
.Saturate
);
327 struct qreg s
= src
[0 * 4 + 0];
328 struct qreg t
= src
[0 * 4 + 1];
329 uint32_t unit
= tgsi_inst
->Src
[1].Register
.Index
;
331 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXP
) {
332 struct qreg proj
= qir_RCP(c
, src
[0 * 4 + 3]);
333 s
= qir_FMUL(c
, s
, proj
);
334 t
= qir_FMUL(c
, t
, proj
);
337 /* There is no native support for GL texture rectangle coordinates, so
338 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
341 if (tgsi_inst
->Texture
.Texture
== TGSI_TEXTURE_RECT
) {
343 get_temp_for_uniform(trans
,
344 QUNIFORM_TEXRECT_SCALE_X
,
347 get_temp_for_uniform(trans
,
348 QUNIFORM_TEXRECT_SCALE_Y
,
352 qir_TEX_T(c
, t
, add_uniform(trans
, QUNIFORM_TEXTURE_CONFIG_P0
,
355 struct qreg sampler_p1
= add_uniform(trans
, QUNIFORM_TEXTURE_CONFIG_P1
,
357 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB
) {
358 qir_TEX_B(c
, src
[0 * 4 + 3], sampler_p1
);
359 qir_TEX_S(c
, s
, add_uniform(trans
, QUNIFORM_CONSTANT
, 0));
361 qir_TEX_S(c
, s
, sampler_p1
);
364 trans
->num_texture_samples
++;
365 qir_emit(c
, qir_inst(QOP_TEX_RESULT
, c
->undef
, c
->undef
, c
->undef
));
367 struct qreg unpacked
[4];
368 for (int i
= 0; i
< 4; i
++)
369 unpacked
[i
] = qir_R4_UNPACK(c
, i
);
371 for (int i
= 0; i
< 4; i
++) {
372 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
375 enum pipe_format format
= trans
->key
->tex_format
[unit
];
376 const struct util_format_description
*desc
=
377 util_format_description(format
);
379 uint8_t swiz
= desc
->swizzle
[i
];
381 update_dst(trans
, tgsi_inst
, i
,
382 get_swizzled_channel(trans
, unpacked
, swiz
));
387 tgsi_to_qir_pow(struct tgsi_to_qir
*trans
,
388 struct tgsi_full_instruction
*tgsi_inst
,
389 enum qop op
, struct qreg
*src
, int i
)
391 struct qcompile
*c
= trans
->c
;
393 /* Note that this instruction replicates its result from the x channel
395 return qir_EXP2(c
, qir_FMUL(c
,
397 qir_LOG2(c
, src
[0 * 4 + 0])));
401 tgsi_to_qir_trunc(struct tgsi_to_qir
*trans
,
402 struct tgsi_full_instruction
*tgsi_inst
,
403 enum qop op
, struct qreg
*src
, int i
)
405 struct qcompile
*c
= trans
->c
;
406 return qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
410 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
414 tgsi_to_qir_frc(struct tgsi_to_qir
*trans
,
415 struct tgsi_full_instruction
*tgsi_inst
,
416 enum qop op
, struct qreg
*src
, int i
)
418 struct qcompile
*c
= trans
->c
;
419 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
420 struct qreg diff
= qir_FSUB(c
, src
[0 * 4 + i
], trunc
);
423 qir_FADD(c
, diff
, qir_uniform_f(trans
, 1.0)),
428 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
432 tgsi_to_qir_flr(struct tgsi_to_qir
*trans
,
433 struct tgsi_full_instruction
*tgsi_inst
,
434 enum qop op
, struct qreg
*src
, int i
)
436 struct qcompile
*c
= trans
->c
;
437 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
440 qir_FSUB(c
, trunc
, qir_uniform_f(trans
, 1.0)),
445 tgsi_to_qir_dp(struct tgsi_to_qir
*trans
,
446 struct tgsi_full_instruction
*tgsi_inst
,
447 int num
, struct qreg
*src
, int i
)
449 struct qcompile
*c
= trans
->c
;
451 struct qreg sum
= qir_FMUL(c
, src
[0 * 4 + 0], src
[1 * 4 + 0]);
452 for (int j
= 1; j
< num
; j
++) {
453 sum
= qir_FADD(c
, sum
, qir_FMUL(c
,
461 tgsi_to_qir_dp2(struct tgsi_to_qir
*trans
,
462 struct tgsi_full_instruction
*tgsi_inst
,
463 enum qop op
, struct qreg
*src
, int i
)
465 return tgsi_to_qir_dp(trans
, tgsi_inst
, 2, src
, i
);
469 tgsi_to_qir_dp3(struct tgsi_to_qir
*trans
,
470 struct tgsi_full_instruction
*tgsi_inst
,
471 enum qop op
, struct qreg
*src
, int i
)
473 return tgsi_to_qir_dp(trans
, tgsi_inst
, 3, src
, i
);
477 tgsi_to_qir_dp4(struct tgsi_to_qir
*trans
,
478 struct tgsi_full_instruction
*tgsi_inst
,
479 enum qop op
, struct qreg
*src
, int i
)
481 return tgsi_to_qir_dp(trans
, tgsi_inst
, 4, src
, i
);
485 tgsi_to_qir_abs(struct tgsi_to_qir
*trans
,
486 struct tgsi_full_instruction
*tgsi_inst
,
487 enum qop op
, struct qreg
*src
, int i
)
489 struct qcompile
*c
= trans
->c
;
490 struct qreg arg
= src
[0 * 4 + i
];
491 return qir_FMAXABS(c
, arg
, arg
);
494 /* Note that this instruction replicates its result from the x channel */
496 tgsi_to_qir_sin(struct tgsi_to_qir
*trans
,
497 struct tgsi_full_instruction
*tgsi_inst
,
498 enum qop op
, struct qreg
*src
, int i
)
500 struct qcompile
*c
= trans
->c
;
503 -pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
504 pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
505 -pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
508 struct qreg scaled_x
=
511 qir_uniform_f(trans
, 1.0f
/ (M_PI
* 2.0f
)));
514 struct qreg x
= tgsi_to_qir_frc(trans
, NULL
, 0, &scaled_x
, 0);
515 struct qreg x2
= qir_FMUL(c
, x
, x
);
516 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(trans
, coeff
[0]));
517 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
518 x
= qir_FMUL(c
, x
, x2
);
523 qir_uniform_f(trans
, coeff
[i
])));
528 /* Note that this instruction replicates its result from the x channel */
530 tgsi_to_qir_cos(struct tgsi_to_qir
*trans
,
531 struct tgsi_full_instruction
*tgsi_inst
,
532 enum qop op
, struct qreg
*src
, int i
)
534 struct qcompile
*c
= trans
->c
;
537 -pow(2.0 * M_PI
, 2) / (2 * 1),
538 pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
539 -pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
542 struct qreg scaled_x
=
543 qir_FMUL(c
, src
[0 * 4 + 0],
544 qir_uniform_f(trans
, 1.0f
/ (M_PI
* 2.0f
)));
545 struct qreg x_frac
= tgsi_to_qir_frc(trans
, NULL
, 0, &scaled_x
, 0);
547 struct qreg sum
= qir_uniform_f(trans
, coeff
[0]);
548 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
549 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
550 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
552 x
= qir_FMUL(c
, x
, x2
);
554 struct qreg mul
= qir_FMUL(c
,
556 qir_uniform_f(trans
, coeff
[i
]));
560 sum
= qir_FADD(c
, sum
, mul
);
566 emit_vertex_input(struct tgsi_to_qir
*trans
, int attr
)
568 enum pipe_format format
= trans
->vs_key
->attr_formats
[attr
];
569 struct qcompile
*c
= trans
->c
;
570 struct qreg vpm_reads
[4];
572 /* Right now, we're setting the VPM offsets to be 16 bytes wide every
573 * time, so we always read 4 32-bit VPM entries.
575 for (int i
= 0; i
< 4; i
++) {
576 vpm_reads
[i
] = qir_get_temp(c
);
577 qir_emit(c
, qir_inst(QOP_VPM_READ
,
584 bool format_warned
= false;
585 const struct util_format_description
*desc
=
586 util_format_description(format
);
588 for (int i
= 0; i
< 4; i
++) {
589 uint8_t swiz
= desc
->swizzle
[i
];
591 if (swiz
<= UTIL_FORMAT_SWIZZLE_W
&&
593 (desc
->channel
[swiz
].type
!= UTIL_FORMAT_TYPE_FLOAT
||
594 desc
->channel
[swiz
].size
!= 32)) {
596 "vtx element %d unsupported type: %s\n",
597 attr
, util_format_name(format
));
598 format_warned
= true;
601 trans
->inputs
[attr
* 4 + i
] =
602 get_swizzled_channel(trans
, vpm_reads
, swiz
);
607 emit_fragcoord_input(struct tgsi_to_qir
*trans
, int attr
)
609 struct qcompile
*c
= trans
->c
;
611 trans
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
612 trans
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
613 trans
->inputs
[attr
* 4 + 2] =
616 qir_uniform_f(trans
, 1.0 / 0xffffff));
617 trans
->inputs
[attr
* 4 + 3] = qir_FRAG_RCP_W(c
);
621 emit_fragment_varying(struct tgsi_to_qir
*trans
, int index
)
623 struct qcompile
*c
= trans
->c
;
630 /* XXX: multiply by W */
631 return qir_VARY_ADD_C(c
, qir_MOV(c
, vary
));
635 emit_fragment_input(struct tgsi_to_qir
*trans
, int attr
)
637 struct qcompile
*c
= trans
->c
;
639 for (int i
= 0; i
< 4; i
++) {
640 trans
->inputs
[attr
* 4 + i
] =
641 emit_fragment_varying(trans
, attr
* 4 + i
);
647 emit_tgsi_declaration(struct tgsi_to_qir
*trans
,
648 struct tgsi_full_declaration
*decl
)
650 struct qcompile
*c
= trans
->c
;
652 switch (decl
->Declaration
.File
) {
653 case TGSI_FILE_INPUT
:
654 for (int i
= decl
->Range
.First
;
655 i
<= decl
->Range
.Last
;
657 if (c
->stage
== QSTAGE_FRAG
) {
658 if (decl
->Semantic
.Name
==
659 TGSI_SEMANTIC_POSITION
) {
660 emit_fragcoord_input(trans
, i
);
662 emit_fragment_input(trans
, i
);
665 emit_vertex_input(trans
, i
);
673 emit_tgsi_instruction(struct tgsi_to_qir
*trans
,
674 struct tgsi_full_instruction
*tgsi_inst
)
676 struct qcompile
*c
= trans
->c
;
679 struct qreg (*func
)(struct tgsi_to_qir
*trans
,
680 struct tgsi_full_instruction
*tgsi_inst
,
682 struct qreg
*src
, int i
);
684 [TGSI_OPCODE_MOV
] = { QOP_MOV
, tgsi_to_qir_alu
},
685 [TGSI_OPCODE_ABS
] = { 0, tgsi_to_qir_abs
},
686 [TGSI_OPCODE_MUL
] = { QOP_FMUL
, tgsi_to_qir_alu
},
687 [TGSI_OPCODE_ADD
] = { QOP_FADD
, tgsi_to_qir_alu
},
688 [TGSI_OPCODE_SUB
] = { QOP_FSUB
, tgsi_to_qir_alu
},
689 [TGSI_OPCODE_MIN
] = { QOP_FMIN
, tgsi_to_qir_alu
},
690 [TGSI_OPCODE_MAX
] = { QOP_FMAX
, tgsi_to_qir_alu
},
691 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
692 [TGSI_OPCODE_SEQ
] = { QOP_SEQ
, tgsi_to_qir_alu
},
693 [TGSI_OPCODE_SNE
] = { QOP_SNE
, tgsi_to_qir_alu
},
694 [TGSI_OPCODE_SGE
] = { QOP_SGE
, tgsi_to_qir_alu
},
695 [TGSI_OPCODE_SLT
] = { QOP_SLT
, tgsi_to_qir_alu
},
696 [TGSI_OPCODE_CMP
] = { QOP_CMP
, tgsi_to_qir_alu
},
697 [TGSI_OPCODE_MAD
] = { 0, tgsi_to_qir_mad
},
698 [TGSI_OPCODE_DP2
] = { 0, tgsi_to_qir_dp2
},
699 [TGSI_OPCODE_DP3
] = { 0, tgsi_to_qir_dp3
},
700 [TGSI_OPCODE_DP4
] = { 0, tgsi_to_qir_dp4
},
701 [TGSI_OPCODE_RCP
] = { QOP_RCP
, tgsi_to_qir_alu
},
702 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
703 [TGSI_OPCODE_EX2
] = { QOP_EXP2
, tgsi_to_qir_alu
},
704 [TGSI_OPCODE_LG2
] = { QOP_LOG2
, tgsi_to_qir_alu
},
705 [TGSI_OPCODE_LIT
] = { 0, tgsi_to_qir_lit
},
706 [TGSI_OPCODE_LRP
] = { 0, tgsi_to_qir_lrp
},
707 [TGSI_OPCODE_POW
] = { 0, tgsi_to_qir_pow
},
708 [TGSI_OPCODE_TRUNC
] = { 0, tgsi_to_qir_trunc
},
709 [TGSI_OPCODE_FRC
] = { 0, tgsi_to_qir_frc
},
710 [TGSI_OPCODE_FLR
] = { 0, tgsi_to_qir_flr
},
711 [TGSI_OPCODE_SIN
] = { 0, tgsi_to_qir_sin
},
712 [TGSI_OPCODE_COS
] = { 0, tgsi_to_qir_cos
},
715 uint32_t tgsi_op
= tgsi_inst
->Instruction
.Opcode
;
717 if (tgsi_op
== TGSI_OPCODE_END
)
720 struct qreg src_regs
[12];
721 for (int s
= 0; s
< 3; s
++) {
722 for (int i
= 0; i
< 4; i
++) {
723 src_regs
[4 * s
+ i
] =
724 get_src(trans
, &tgsi_inst
->Src
[s
].Register
, i
);
729 case TGSI_OPCODE_TEX
:
730 case TGSI_OPCODE_TXP
:
731 case TGSI_OPCODE_TXB
:
732 tgsi_to_qir_tex(trans
, tgsi_inst
,
733 op_trans
[tgsi_op
].op
, src_regs
);
739 if (tgsi_op
> ARRAY_SIZE(op_trans
) || !(op_trans
[tgsi_op
].func
)) {
740 fprintf(stderr
, "unknown tgsi inst: ");
741 tgsi_dump_instruction(tgsi_inst
, asdf
++);
742 fprintf(stderr
, "\n");
746 for (int i
= 0; i
< 4; i
++) {
747 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
752 result
= op_trans
[tgsi_op
].func(trans
, tgsi_inst
,
753 op_trans
[tgsi_op
].op
,
756 if (tgsi_inst
->Instruction
.Saturate
) {
757 float low
= (tgsi_inst
->Instruction
.Saturate
==
758 TGSI_SAT_MINUS_PLUS_ONE
? -1.0 : 0.0);
762 qir_uniform_f(trans
, 1.0)),
763 qir_uniform_f(trans
, low
));
766 update_dst(trans
, tgsi_inst
, i
, result
);
771 parse_tgsi_immediate(struct tgsi_to_qir
*trans
, struct tgsi_full_immediate
*imm
)
773 for (int i
= 0; i
< 4; i
++) {
774 unsigned n
= trans
->num_consts
++;
775 trans
->consts
[n
] = qir_uniform_ui(trans
, imm
->u
[i
].Uint
);
780 vc4_blend_channel(struct tgsi_to_qir
*trans
,
787 struct qcompile
*c
= trans
->c
;
790 case PIPE_BLENDFACTOR_ONE
:
792 case PIPE_BLENDFACTOR_SRC_COLOR
:
793 return qir_FMUL(c
, val
, src
[channel
]);
794 case PIPE_BLENDFACTOR_SRC_ALPHA
:
795 return qir_FMUL(c
, val
, src
[3]);
796 case PIPE_BLENDFACTOR_DST_ALPHA
:
797 return qir_FMUL(c
, val
, dst
[3]);
798 case PIPE_BLENDFACTOR_DST_COLOR
:
799 return qir_FMUL(c
, val
, dst
[channel
]);
800 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
801 return qir_FMIN(c
, src
[3], qir_FSUB(c
,
802 qir_uniform_f(trans
, 1.0),
804 case PIPE_BLENDFACTOR_CONST_COLOR
:
805 return qir_FMUL(c
, val
,
806 get_temp_for_uniform(trans
,
807 QUNIFORM_BLEND_CONST_COLOR
,
809 case PIPE_BLENDFACTOR_CONST_ALPHA
:
810 return qir_FMUL(c
, val
,
811 get_temp_for_uniform(trans
,
812 QUNIFORM_BLEND_CONST_COLOR
,
814 case PIPE_BLENDFACTOR_ZERO
:
815 return qir_uniform_f(trans
, 0.0);
816 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
817 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
819 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
820 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
822 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
823 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
825 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
826 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
828 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
829 return qir_FMUL(c
, val
,
830 qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
831 get_temp_for_uniform(trans
,
832 QUNIFORM_BLEND_CONST_COLOR
,
834 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
835 return qir_FMUL(c
, val
,
836 qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
837 get_temp_for_uniform(trans
,
838 QUNIFORM_BLEND_CONST_COLOR
,
842 case PIPE_BLENDFACTOR_SRC1_COLOR
:
843 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
844 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
845 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
847 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
853 vc4_blend_func(struct tgsi_to_qir
*trans
,
854 struct qreg src
, struct qreg dst
,
857 struct qcompile
*c
= trans
->c
;
861 return qir_FADD(c
, src
, dst
);
862 case PIPE_BLEND_SUBTRACT
:
863 return qir_FSUB(c
, src
, dst
);
864 case PIPE_BLEND_REVERSE_SUBTRACT
:
865 return qir_FSUB(c
, dst
, src
);
867 return qir_FMIN(c
, src
, dst
);
869 return qir_FMAX(c
, src
, dst
);
873 fprintf(stderr
, "Unknown blend func %d\n", func
);
880 * Implements fixed function blending in shader code.
882 * VC4 doesn't have any hardware support for blending. Instead, you read the
883 * current contents of the destination from the tile buffer after having
884 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
885 * math using your output color and that destination value, and update the
886 * output color appropriately.
889 vc4_blend(struct tgsi_to_qir
*trans
, struct qreg
*result
,
890 struct qreg
*dst_color
, struct qreg
*src_color
)
892 struct pipe_rt_blend_state
*blend
= &trans
->fs_key
->blend
;
894 if (!blend
->blend_enable
) {
895 for (int i
= 0; i
< 4; i
++)
896 result
[i
] = src_color
[i
];
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 src_color
[4] = {
941 trans
->outputs
[0], trans
->outputs
[1],
942 trans
->outputs
[2], trans
->outputs
[3],
945 struct qreg dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
946 if (trans
->fs_key
->blend
.blend_enable
||
947 trans
->fs_key
->blend
.colormask
!= 0xf) {
948 qir_emit(c
, qir_inst(QOP_TLB_COLOR_READ
, c
->undef
,
949 c
->undef
, c
->undef
));
950 for (int i
= 0; i
< 4; i
++) {
951 dst_color
[i
] = qir_R4_UNPACK(c
, i
);
957 struct qreg blend_color
[4];
958 vc4_blend(trans
, blend_color
, dst_color
, src_color
);
960 /* If the bit isn't set in the color mask, then just return the
961 * original dst color, instead.
963 for (int i
= 0; i
< 4; i
++) {
964 if (!(trans
->fs_key
->blend
.colormask
& (1 << i
))) {
965 blend_color
[i
] = dst_color
[i
];
969 /* Debug: Sometimes you're getting a black output and just want to see
970 * if the FS is getting executed at all. Spam magenta into the color
974 blend_color
[0] = qir_uniform_f(trans
, 1.0);
975 blend_color
[1] = qir_uniform_f(trans
, 0.0);
976 blend_color
[2] = qir_uniform_f(trans
, 1.0);
977 blend_color
[3] = qir_uniform_f(trans
, 0.5);
980 struct qreg swizzled_outputs
[4];
981 for (int i
= 0; i
< 4; i
++) {
982 swizzled_outputs
[i
] =
983 get_swizzled_channel(trans
, blend_color
,
984 format_desc
->swizzle
[i
]);
987 if (trans
->fs_key
->depth_enabled
) {
988 qir_emit(c
, qir_inst(QOP_TLB_PASSTHROUGH_Z_WRITE
, c
->undef
,
989 c
->undef
, c
->undef
));
992 qir_emit(c
, qir_inst4(QOP_PACK_COLORS
, t
,
996 swizzled_outputs
[3]));
997 qir_emit(c
, qir_inst(QOP_TLB_COLOR_WRITE
, c
->undef
,
1002 emit_scaled_viewport_write(struct tgsi_to_qir
*trans
, struct qreg rcp_w
)
1004 struct qcompile
*c
= trans
->c
;
1007 for (int i
= 0; i
< 2; i
++) {
1009 add_uniform(trans
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1011 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1018 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1022 emit_zs_write(struct tgsi_to_qir
*trans
, struct qreg rcp_w
)
1024 struct qcompile
*c
= trans
->c
;
1026 struct qreg zscale
= add_uniform(trans
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1027 struct qreg zoffset
= add_uniform(trans
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1029 qir_VPM_WRITE(c
, qir_FMUL(c
, qir_FADD(c
, qir_FMUL(c
,
1037 emit_rcp_wc_write(struct tgsi_to_qir
*trans
, struct qreg rcp_w
)
1039 struct qcompile
*c
= trans
->c
;
1041 qir_VPM_WRITE(c
, rcp_w
);
1045 emit_vert_end(struct tgsi_to_qir
*trans
)
1047 struct qcompile
*c
= trans
->c
;
1049 struct qreg rcp_w
= qir_RCP(c
, trans
->outputs
[3]);
1051 emit_scaled_viewport_write(trans
, rcp_w
);
1052 emit_zs_write(trans
, rcp_w
);
1053 emit_rcp_wc_write(trans
, rcp_w
);
1055 for (int i
= 4; i
< trans
->num_outputs
; i
++) {
1056 qir_VPM_WRITE(c
, trans
->outputs
[i
]);
1061 emit_coord_end(struct tgsi_to_qir
*trans
)
1063 struct qcompile
*c
= trans
->c
;
1065 struct qreg rcp_w
= qir_RCP(c
, trans
->outputs
[3]);
1067 for (int i
= 0; i
< 4; i
++)
1068 qir_VPM_WRITE(c
, trans
->outputs
[i
]);
1070 emit_scaled_viewport_write(trans
, rcp_w
);
1071 emit_zs_write(trans
, rcp_w
);
1072 emit_rcp_wc_write(trans
, rcp_w
);
1075 static struct tgsi_to_qir
*
1076 vc4_shader_tgsi_to_qir(struct vc4_compiled_shader
*shader
, enum qstage stage
,
1077 struct vc4_key
*key
)
1079 struct tgsi_to_qir
*trans
= CALLOC_STRUCT(tgsi_to_qir
);
1083 c
= qir_compile_init();
1086 memset(trans
, 0, sizeof(*trans
));
1088 trans
->temps
= calloc(sizeof(struct qreg
), 1024);
1089 trans
->inputs
= calloc(sizeof(struct qreg
), 8 * 4);
1090 trans
->outputs
= calloc(sizeof(struct qreg
), 1024);
1091 trans
->uniforms
= calloc(sizeof(struct qreg
), 1024);
1092 trans
->consts
= calloc(sizeof(struct qreg
), 1024);
1094 trans
->uniform_data
= calloc(sizeof(uint32_t), 1024);
1095 trans
->uniform_contents
= calloc(sizeof(enum quniform_contents
), 1024);
1097 trans
->shader_state
= key
->shader_state
;
1099 ret
= tgsi_parse_init(&trans
->parser
, trans
->shader_state
->tokens
);
1100 assert(ret
== TGSI_PARSE_OK
);
1102 if (vc4_debug
& VC4_DEBUG_TGSI
) {
1103 fprintf(stderr
, "TGSI:\n");
1104 tgsi_dump(trans
->shader_state
->tokens
, 0);
1110 trans
->fs_key
= (struct vc4_fs_key
*)key
;
1111 if (trans
->fs_key
->is_points
) {
1112 trans
->point_x
= emit_fragment_varying(trans
, 0);
1113 trans
->point_y
= emit_fragment_varying(trans
, 0);
1114 } else if (trans
->fs_key
->is_lines
) {
1115 trans
->line_x
= emit_fragment_varying(trans
, 0);
1119 trans
->vs_key
= (struct vc4_vs_key
*)key
;
1122 trans
->vs_key
= (struct vc4_vs_key
*)key
;
1126 while (!tgsi_parse_end_of_tokens(&trans
->parser
)) {
1127 tgsi_parse_token(&trans
->parser
);
1129 switch (trans
->parser
.FullToken
.Token
.Type
) {
1130 case TGSI_TOKEN_TYPE_DECLARATION
:
1131 emit_tgsi_declaration(trans
,
1132 &trans
->parser
.FullToken
.FullDeclaration
);
1135 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1136 emit_tgsi_instruction(trans
,
1137 &trans
->parser
.FullToken
.FullInstruction
);
1140 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1141 parse_tgsi_immediate(trans
,
1142 &trans
->parser
.FullToken
.FullImmediate
);
1149 emit_frag_end(trans
);
1152 emit_vert_end(trans
);
1155 emit_coord_end(trans
);
1159 tgsi_parse_free(&trans
->parser
);
1164 if (vc4_debug
& VC4_DEBUG_QIR
) {
1165 fprintf(stderr
, "QIR:\n");
1168 vc4_generate_code(c
);
1170 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
1171 fprintf(stderr
, "SHADER-DB: %s: %d instructions\n",
1172 qir_get_stage_name(c
->stage
), c
->qpu_inst_count
);
1173 fprintf(stderr
, "SHADER-DB: %s: %d uniforms\n",
1174 qir_get_stage_name(c
->stage
), trans
->num_uniforms
);
1181 vc4_shader_state_create(struct pipe_context
*pctx
,
1182 const struct pipe_shader_state
*cso
)
1184 struct pipe_shader_state
*so
= CALLOC_STRUCT(pipe_shader_state
);
1188 so
->tokens
= tgsi_dup_tokens(cso
->tokens
);
1194 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
1196 struct tgsi_to_qir
*trans
)
1198 int count
= trans
->num_uniforms
;
1199 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1201 uinfo
->count
= count
;
1202 uinfo
->data
= malloc(count
* sizeof(*uinfo
->data
));
1203 memcpy(uinfo
->data
, trans
->uniform_data
,
1204 count
* sizeof(*uinfo
->data
));
1205 uinfo
->contents
= malloc(count
* sizeof(*uinfo
->contents
));
1206 memcpy(uinfo
->contents
, trans
->uniform_contents
,
1207 count
* sizeof(*uinfo
->contents
));
1208 uinfo
->num_texture_samples
= trans
->num_texture_samples
;
1212 vc4_fs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1213 struct vc4_fs_key
*key
)
1215 struct tgsi_to_qir
*trans
= vc4_shader_tgsi_to_qir(shader
, QSTAGE_FRAG
,
1217 shader
->num_inputs
= trans
->c
->num_inputs
;
1218 copy_uniform_state_to_shader(shader
, 0, trans
);
1219 shader
->bo
= vc4_bo_alloc_mem(vc4
->screen
, trans
->c
->qpu_insts
,
1220 trans
->c
->qpu_inst_count
* sizeof(uint64_t),
1223 qir_compile_destroy(trans
->c
);
1228 vc4_vs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1229 struct vc4_vs_key
*key
)
1231 struct tgsi_to_qir
*vs_trans
= vc4_shader_tgsi_to_qir(shader
,
1234 copy_uniform_state_to_shader(shader
, 0, vs_trans
);
1236 struct tgsi_to_qir
*cs_trans
= vc4_shader_tgsi_to_qir(shader
,
1239 copy_uniform_state_to_shader(shader
, 1, cs_trans
);
1241 uint32_t vs_size
= vs_trans
->c
->qpu_inst_count
* sizeof(uint64_t);
1242 uint32_t cs_size
= cs_trans
->c
->qpu_inst_count
* sizeof(uint64_t);
1243 shader
->coord_shader_offset
= vs_size
; /* XXX: alignment? */
1244 shader
->bo
= vc4_bo_alloc(vc4
->screen
,
1245 shader
->coord_shader_offset
+ cs_size
,
1248 void *map
= vc4_bo_map(shader
->bo
);
1249 memcpy(map
, vs_trans
->c
->qpu_insts
, vs_size
);
1250 memcpy(map
+ shader
->coord_shader_offset
,
1251 cs_trans
->c
->qpu_insts
, cs_size
);
1253 qir_compile_destroy(vs_trans
->c
);
1254 qir_compile_destroy(cs_trans
->c
);
1258 vc4_setup_shared_key(struct vc4_key
*key
, struct vc4_texture_stateobj
*texstate
)
1260 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
1261 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
1263 struct pipe_resource
*prsc
= sampler
->texture
;
1264 key
->tex_format
[i
] = prsc
->format
;
1270 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
1272 struct vc4_fs_key local_key
;
1273 struct vc4_fs_key
*key
= &local_key
;
1275 memset(key
, 0, sizeof(*key
));
1276 vc4_setup_shared_key(&key
->base
, &vc4
->fragtex
);
1277 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
1278 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
1279 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
1280 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
1281 key
->blend
= vc4
->blend
->rt
[0];
1283 if (vc4
->framebuffer
.cbufs
[0])
1284 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
1286 key
->depth_enabled
= vc4
->zsa
->base
.depth
.enabled
;
1288 vc4
->prog
.fs
= util_hash_table_get(vc4
->fs_cache
, key
);
1292 key
= malloc(sizeof(*key
));
1293 memcpy(key
, &local_key
, sizeof(*key
));
1295 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1296 vc4_fs_compile(vc4
, shader
, key
);
1297 util_hash_table_set(vc4
->fs_cache
, key
, shader
);
1299 vc4
->prog
.fs
= shader
;
1303 vc4_update_compiled_vs(struct vc4_context
*vc4
)
1305 struct vc4_vs_key local_key
;
1306 struct vc4_vs_key
*key
= &local_key
;
1308 memset(key
, 0, sizeof(*key
));
1309 vc4_setup_shared_key(&key
->base
, &vc4
->verttex
);
1310 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
1312 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
1313 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
1315 vc4
->prog
.vs
= util_hash_table_get(vc4
->vs_cache
, key
);
1319 key
= malloc(sizeof(*key
));
1320 memcpy(key
, &local_key
, sizeof(*key
));
1322 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1323 vc4_vs_compile(vc4
, shader
, key
);
1324 util_hash_table_set(vc4
->vs_cache
, key
, shader
);
1326 vc4
->prog
.vs
= shader
;
1330 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
1332 vc4_update_compiled_fs(vc4
, prim_mode
);
1333 vc4_update_compiled_vs(vc4
);
1337 fs_cache_hash(void *key
)
1339 return util_hash_crc32(key
, sizeof(struct vc4_fs_key
));
1343 vs_cache_hash(void *key
)
1345 return util_hash_crc32(key
, sizeof(struct vc4_vs_key
));
1349 fs_cache_compare(void *key1
, void *key2
)
1351 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
));
1355 vs_cache_compare(void *key1
, void *key2
)
1357 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
));
1360 struct delete_state
{
1361 struct vc4_context
*vc4
;
1362 struct pipe_shader_state
*shader_state
;
1365 static enum pipe_error
1366 fs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1368 struct delete_state
*del
= data
;
1369 struct vc4_fs_key
*key
= in_key
;
1370 struct vc4_compiled_shader
*shader
= in_value
;
1372 if (key
->base
.shader_state
== data
) {
1373 util_hash_table_remove(del
->vc4
->fs_cache
, key
);
1374 vc4_bo_unreference(&shader
->bo
);
1381 static enum pipe_error
1382 vs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1384 struct delete_state
*del
= data
;
1385 struct vc4_vs_key
*key
= in_key
;
1386 struct vc4_compiled_shader
*shader
= in_value
;
1388 if (key
->base
.shader_state
== data
) {
1389 util_hash_table_remove(del
->vc4
->vs_cache
, key
);
1390 vc4_bo_unreference(&shader
->bo
);
1398 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
1400 struct vc4_context
*vc4
= vc4_context(pctx
);
1401 struct pipe_shader_state
*so
= hwcso
;
1402 struct delete_state del
;
1405 del
.shader_state
= so
;
1406 util_hash_table_foreach(vc4
->fs_cache
, fs_delete_from_cache
, &del
);
1407 util_hash_table_foreach(vc4
->vs_cache
, vs_delete_from_cache
, &del
);
1409 free((void *)so
->tokens
);
1413 static uint32_t translate_wrap(uint32_t p_wrap
)
1416 case PIPE_TEX_WRAP_REPEAT
:
1418 case PIPE_TEX_WRAP_CLAMP
:
1419 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1421 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1423 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1426 fprintf(stderr
, "Unknown wrap mode %d\n", p_wrap
);
1427 assert(!"not reached");
1433 write_texture_p0(struct vc4_context
*vc4
,
1434 struct vc4_texture_stateobj
*texstate
,
1437 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
1438 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
1440 cl_reloc(vc4
, &vc4
->uniforms
, rsc
->bo
,
1441 rsc
->slices
[0].offset
| texture
->u
.tex
.last_level
);
1445 write_texture_p1(struct vc4_context
*vc4
,
1446 struct vc4_texture_stateobj
*texstate
,
1449 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
1450 struct pipe_sampler_state
*sampler
= texstate
->samplers
[unit
];
1451 static const uint32_t mipfilter_map
[] = {
1452 [PIPE_TEX_MIPFILTER_NEAREST
] = 2,
1453 [PIPE_TEX_MIPFILTER_LINEAR
] = 4,
1454 [PIPE_TEX_MIPFILTER_NONE
] = 0
1456 static const uint32_t imgfilter_map
[] = {
1457 [PIPE_TEX_FILTER_NEAREST
] = 1,
1458 [PIPE_TEX_FILTER_LINEAR
] = 0,
1461 cl_u32(&vc4
->uniforms
,
1462 (1 << 31) /* XXX: data type */|
1463 (texture
->texture
->height0
<< 20) |
1464 (texture
->texture
->width0
<< 8) |
1465 (imgfilter_map
[sampler
->mag_img_filter
] << 7) |
1466 ((imgfilter_map
[sampler
->min_img_filter
] +
1467 mipfilter_map
[sampler
->min_mip_filter
]) << 4) |
1468 (translate_wrap(sampler
->wrap_t
) << 2) |
1469 (translate_wrap(sampler
->wrap_s
) << 0));
1473 get_texrect_scale(struct vc4_texture_stateobj
*texstate
,
1474 enum quniform_contents contents
,
1477 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
1480 if (contents
== QUNIFORM_TEXRECT_SCALE_X
)
1481 dim
= texture
->texture
->width0
;
1483 dim
= texture
->texture
->height0
;
1485 return fui(1.0f
/ dim
);
1489 vc4_write_uniforms(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1490 struct vc4_constbuf_stateobj
*cb
,
1491 struct vc4_texture_stateobj
*texstate
,
1494 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1495 const uint32_t *gallium_uniforms
= cb
->cb
[0].user_buffer
;
1497 cl_start_shader_reloc(&vc4
->uniforms
, uinfo
->num_texture_samples
);
1499 for (int i
= 0; i
< uinfo
->count
; i
++) {
1501 switch (uinfo
->contents
[i
]) {
1502 case QUNIFORM_CONSTANT
:
1503 cl_u32(&vc4
->uniforms
, uinfo
->data
[i
]);
1505 case QUNIFORM_UNIFORM
:
1506 cl_u32(&vc4
->uniforms
,
1507 gallium_uniforms
[uinfo
->data
[i
]]);
1509 case QUNIFORM_VIEWPORT_X_SCALE
:
1510 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[0] * 16.0f
);
1512 case QUNIFORM_VIEWPORT_Y_SCALE
:
1513 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[1] * 16.0f
);
1516 case QUNIFORM_VIEWPORT_Z_OFFSET
:
1517 cl_f(&vc4
->uniforms
, vc4
->viewport
.translate
[2]);
1519 case QUNIFORM_VIEWPORT_Z_SCALE
:
1520 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[2]);
1523 case QUNIFORM_TEXTURE_CONFIG_P0
:
1524 write_texture_p0(vc4
, texstate
, uinfo
->data
[i
]);
1527 case QUNIFORM_TEXTURE_CONFIG_P1
:
1528 write_texture_p1(vc4
, texstate
, uinfo
->data
[i
]);
1531 case QUNIFORM_TEXRECT_SCALE_X
:
1532 case QUNIFORM_TEXRECT_SCALE_Y
:
1533 cl_u32(&vc4
->uniforms
,
1534 get_texrect_scale(texstate
,
1539 case QUNIFORM_BLEND_CONST_COLOR
:
1540 cl_f(&vc4
->uniforms
,
1541 vc4
->blend_color
.color
[uinfo
->data
[i
]]);
1545 uint32_t written_val
= *(uint32_t *)(vc4
->uniforms
.next
- 4);
1546 fprintf(stderr
, "%p/%d: %d: 0x%08x (%f)\n",
1547 shader
, shader_index
, i
, written_val
, uif(written_val
));
1553 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1555 struct vc4_context
*vc4
= vc4_context(pctx
);
1556 vc4
->prog
.bind_fs
= hwcso
;
1557 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_FP
;
1558 vc4
->dirty
|= VC4_DIRTY_PROG
;
1562 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1564 struct vc4_context
*vc4
= vc4_context(pctx
);
1565 vc4
->prog
.bind_vs
= hwcso
;
1566 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_VP
;
1567 vc4
->dirty
|= VC4_DIRTY_PROG
;
1571 vc4_program_init(struct pipe_context
*pctx
)
1573 struct vc4_context
*vc4
= vc4_context(pctx
);
1575 pctx
->create_vs_state
= vc4_shader_state_create
;
1576 pctx
->delete_vs_state
= vc4_shader_state_delete
;
1578 pctx
->create_fs_state
= vc4_shader_state_create
;
1579 pctx
->delete_fs_state
= vc4_shader_state_delete
;
1581 pctx
->bind_fs_state
= vc4_fp_state_bind
;
1582 pctx
->bind_vs_state
= vc4_vp_state_bind
;
1584 vc4
->fs_cache
= util_hash_table_create(fs_cache_hash
, fs_cache_compare
);
1585 vc4
->vs_cache
= util_hash_table_create(vs_cache_hash
, vs_cache_compare
);