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
;
56 struct vc4_fs_key
*fs_key
;
57 struct vc4_vs_key
*vs_key
;
59 uint32_t *uniform_data
;
60 enum quniform_contents
*uniform_contents
;
61 uint32_t num_uniforms
;
63 uint32_t num_texture_samples
;
67 struct pipe_shader_state
*shader_state
;
68 enum pipe_format tex_format
[VC4_MAX_TEXTURE_SAMPLERS
];
73 enum pipe_format color_format
;
78 struct pipe_rt_blend_state blend
;
83 enum pipe_format attr_formats
[8];
87 add_uniform(struct tgsi_to_qir
*trans
,
88 enum quniform_contents contents
,
91 uint32_t uniform
= trans
->num_uniforms
++;
92 struct qreg u
= { QFILE_UNIF
, uniform
};
94 trans
->uniform_contents
[uniform
] = contents
;
95 trans
->uniform_data
[uniform
] = data
;
101 get_temp_for_uniform(struct tgsi_to_qir
*trans
, enum quniform_contents contents
,
104 struct qcompile
*c
= trans
->c
;
106 for (int i
= 0; i
< trans
->num_uniforms
; i
++) {
107 if (trans
->uniform_contents
[i
] == contents
&&
108 trans
->uniform_data
[i
] == data
)
109 return trans
->uniforms
[i
];
112 struct qreg u
= add_uniform(trans
, contents
, data
);
113 struct qreg t
= qir_MOV(c
, u
);
115 trans
->uniforms
[u
.index
] = t
;
120 qir_uniform_ui(struct tgsi_to_qir
*trans
, uint32_t ui
)
122 return get_temp_for_uniform(trans
, QUNIFORM_CONSTANT
, ui
);
126 qir_uniform_f(struct tgsi_to_qir
*trans
, float f
)
128 return qir_uniform_ui(trans
, fui(f
));
132 get_src(struct tgsi_to_qir
*trans
, struct tgsi_src_register
*src
, int i
)
134 struct qcompile
*c
= trans
->c
;
135 struct qreg r
= c
->undef
;
155 assert(!src
->Indirect
);
160 case TGSI_FILE_TEMPORARY
:
161 r
= trans
->temps
[src
->Index
* 4 + s
];
163 case TGSI_FILE_IMMEDIATE
:
164 r
= trans
->consts
[src
->Index
* 4 + s
];
166 case TGSI_FILE_CONSTANT
:
167 r
= get_temp_for_uniform(trans
, QUNIFORM_UNIFORM
,
170 case TGSI_FILE_INPUT
:
171 r
= trans
->inputs
[src
->Index
* 4 + s
];
173 case TGSI_FILE_SAMPLER
:
174 case TGSI_FILE_SAMPLER_VIEW
:
178 fprintf(stderr
, "unknown src file %d\n", src
->File
);
183 r
= qir_FMAXABS(c
, r
, r
);
186 r
= qir_FSUB(c
, qir_uniform_f(trans
, 0), r
);
193 update_dst(struct tgsi_to_qir
*trans
, struct tgsi_full_instruction
*tgsi_inst
,
194 int i
, struct qreg val
)
196 struct tgsi_dst_register
*tgsi_dst
= &tgsi_inst
->Dst
[0].Register
;
198 assert(!tgsi_dst
->Indirect
);
200 switch (tgsi_dst
->File
) {
201 case TGSI_FILE_TEMPORARY
:
202 trans
->temps
[tgsi_dst
->Index
* 4 + i
] = val
;
204 case TGSI_FILE_OUTPUT
:
205 trans
->outputs
[tgsi_dst
->Index
* 4 + i
] = val
;
206 trans
->num_outputs
= MAX2(trans
->num_outputs
,
207 tgsi_dst
->Index
* 4 + i
+ 1);
210 fprintf(stderr
, "unknown dst file %d\n", tgsi_dst
->File
);
216 get_swizzled_channel(struct tgsi_to_qir
*trans
,
217 struct qreg
*srcs
, int swiz
)
221 case UTIL_FORMAT_SWIZZLE_NONE
:
222 fprintf(stderr
, "warning: unknown swizzle\n");
224 case UTIL_FORMAT_SWIZZLE_0
:
225 return qir_uniform_f(trans
, 0.0);
226 case UTIL_FORMAT_SWIZZLE_1
:
227 return qir_uniform_f(trans
, 1.0);
228 case UTIL_FORMAT_SWIZZLE_X
:
229 case UTIL_FORMAT_SWIZZLE_Y
:
230 case UTIL_FORMAT_SWIZZLE_Z
:
231 case UTIL_FORMAT_SWIZZLE_W
:
237 tgsi_to_qir_alu(struct tgsi_to_qir
*trans
,
238 struct tgsi_full_instruction
*tgsi_inst
,
239 enum qop op
, struct qreg
*src
, int i
)
241 struct qcompile
*c
= trans
->c
;
242 struct qreg dst
= qir_get_temp(c
);
243 qir_emit(c
, qir_inst4(op
, dst
,
252 tgsi_to_qir_mad(struct tgsi_to_qir
*trans
,
253 struct tgsi_full_instruction
*tgsi_inst
,
254 enum qop op
, struct qreg
*src
, int i
)
256 struct qcompile
*c
= trans
->c
;
265 tgsi_to_qir_lit(struct tgsi_to_qir
*trans
,
266 struct tgsi_full_instruction
*tgsi_inst
,
267 enum qop op
, struct qreg
*src
, int i
)
269 struct qcompile
*c
= trans
->c
;
270 struct qreg x
= src
[0 * 4 + 0];
271 struct qreg y
= src
[0 * 4 + 1];
272 struct qreg w
= src
[0 * 4 + 3];
277 return qir_uniform_f(trans
, 1.0);
279 return qir_FMAX(c
, src
[0 * 4 + 0], qir_uniform_f(trans
, 0.0));
281 struct qreg zero
= qir_uniform_f(trans
, 0.0);
283 /* XXX: Clamp w to -128..128 */
287 qir_EXP2(c
, qir_FMUL(c
,
295 assert(!"not reached");
301 tgsi_to_qir_lrp(struct tgsi_to_qir
*trans
,
302 struct tgsi_full_instruction
*tgsi_inst
,
303 enum qop op
, struct qreg
*src
, int i
)
305 struct qcompile
*c
= trans
->c
;
306 struct qreg src0
= src
[0 * 4 + i
];
307 struct qreg src1
= src
[1 * 4 + i
];
308 struct qreg src2
= src
[2 * 4 + i
];
311 * src0 * src1 + (1 - src0) * src2.
312 * -> src0 * src1 + src2 - src0 * src2
313 * -> src2 + src0 * (src1 - src2)
315 return qir_FADD(c
, src2
, qir_FMUL(c
, src0
, qir_FSUB(c
, src1
, src2
)));
320 tgsi_to_qir_tex(struct tgsi_to_qir
*trans
,
321 struct tgsi_full_instruction
*tgsi_inst
,
322 enum qop op
, struct qreg
*src
)
324 struct qcompile
*c
= trans
->c
;
326 assert(!tgsi_inst
->Instruction
.Saturate
);
328 struct qreg s
= src
[0 * 4 + 0];
329 struct qreg t
= src
[0 * 4 + 1];
330 uint32_t sampler
= 0; /* XXX */
332 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXP
) {
333 struct qreg proj
= qir_RCP(c
, src
[0 * 4 + 3]);
334 s
= qir_FMUL(c
, s
, proj
);
335 t
= qir_FMUL(c
, t
, proj
);
338 /* There is no native support for GL texture rectangle coordinates, so
339 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
342 if (tgsi_inst
->Texture
.Texture
== TGSI_TEXTURE_RECT
) {
344 get_temp_for_uniform(trans
,
345 QUNIFORM_TEXRECT_SCALE_X
,
348 get_temp_for_uniform(trans
,
349 QUNIFORM_TEXRECT_SCALE_Y
,
353 uint32_t tex_and_sampler
= 0; /* XXX */
354 qir_TEX_T(c
, t
, add_uniform(trans
, QUNIFORM_TEXTURE_CONFIG_P0
,
357 struct qreg sampler_p1
= add_uniform(trans
, QUNIFORM_TEXTURE_CONFIG_P1
,
359 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB
) {
360 qir_TEX_B(c
, src
[0 * 4 + 3], sampler_p1
);
361 qir_TEX_S(c
, s
, add_uniform(trans
, QUNIFORM_CONSTANT
, 0));
363 qir_TEX_S(c
, s
, sampler_p1
);
366 trans
->num_texture_samples
++;
367 qir_emit(c
, qir_inst(QOP_TEX_RESULT
, c
->undef
, c
->undef
, c
->undef
));
369 struct qreg unpacked
[4];
370 for (int i
= 0; i
< 4; i
++)
371 unpacked
[i
] = qir_R4_UNPACK(c
, i
);
373 bool format_warned
= false;
374 for (int i
= 0; i
< 4; i
++) {
375 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
378 enum pipe_format format
= trans
->key
->tex_format
[sampler
];
379 const struct util_format_description
*desc
=
380 util_format_description(format
);
382 uint8_t swiz
= desc
->swizzle
[i
];
383 if (!format_warned
&&
384 swiz
<= UTIL_FORMAT_SWIZZLE_W
&&
385 (desc
->channel
[swiz
].type
!= UTIL_FORMAT_TYPE_UNSIGNED
||
386 desc
->channel
[swiz
].size
!= 8)) {
388 "tex channel %d unsupported type: %s\n",
389 i
, util_format_name(format
));
390 format_warned
= true;
393 update_dst(trans
, tgsi_inst
, i
,
394 get_swizzled_channel(trans
, unpacked
, swiz
));
399 tgsi_to_qir_pow(struct tgsi_to_qir
*trans
,
400 struct tgsi_full_instruction
*tgsi_inst
,
401 enum qop op
, struct qreg
*src
, int i
)
403 struct qcompile
*c
= trans
->c
;
405 /* Note that this instruction replicates its result from the x channel
407 return qir_EXP2(c
, qir_FMUL(c
,
409 qir_LOG2(c
, src
[0 * 4 + 0])));
413 tgsi_to_qir_trunc(struct tgsi_to_qir
*trans
,
414 struct tgsi_full_instruction
*tgsi_inst
,
415 enum qop op
, struct qreg
*src
, int i
)
417 struct qcompile
*c
= trans
->c
;
418 return qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
422 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
426 tgsi_to_qir_frc(struct tgsi_to_qir
*trans
,
427 struct tgsi_full_instruction
*tgsi_inst
,
428 enum qop op
, struct qreg
*src
, int i
)
430 struct qcompile
*c
= trans
->c
;
431 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
432 struct qreg diff
= qir_FSUB(c
, src
[0 * 4 + i
], trunc
);
435 qir_FADD(c
, diff
, qir_uniform_f(trans
, 1.0)),
440 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
444 tgsi_to_qir_flr(struct tgsi_to_qir
*trans
,
445 struct tgsi_full_instruction
*tgsi_inst
,
446 enum qop op
, struct qreg
*src
, int i
)
448 struct qcompile
*c
= trans
->c
;
449 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
452 qir_FSUB(c
, trunc
, qir_uniform_f(trans
, 1.0)),
457 tgsi_to_qir_dp(struct tgsi_to_qir
*trans
,
458 struct tgsi_full_instruction
*tgsi_inst
,
459 int num
, struct qreg
*src
, int i
)
461 struct qcompile
*c
= trans
->c
;
463 struct qreg sum
= qir_FMUL(c
, src
[0 * 4 + 0], src
[1 * 4 + 0]);
464 for (int j
= 1; j
< num
; j
++) {
465 sum
= qir_FADD(c
, sum
, qir_FMUL(c
,
473 tgsi_to_qir_dp2(struct tgsi_to_qir
*trans
,
474 struct tgsi_full_instruction
*tgsi_inst
,
475 enum qop op
, struct qreg
*src
, int i
)
477 return tgsi_to_qir_dp(trans
, tgsi_inst
, 2, src
, i
);
481 tgsi_to_qir_dp3(struct tgsi_to_qir
*trans
,
482 struct tgsi_full_instruction
*tgsi_inst
,
483 enum qop op
, struct qreg
*src
, int i
)
485 return tgsi_to_qir_dp(trans
, tgsi_inst
, 3, src
, i
);
489 tgsi_to_qir_dp4(struct tgsi_to_qir
*trans
,
490 struct tgsi_full_instruction
*tgsi_inst
,
491 enum qop op
, struct qreg
*src
, int i
)
493 return tgsi_to_qir_dp(trans
, tgsi_inst
, 4, src
, i
);
497 tgsi_to_qir_abs(struct tgsi_to_qir
*trans
,
498 struct tgsi_full_instruction
*tgsi_inst
,
499 enum qop op
, struct qreg
*src
, int i
)
501 struct qcompile
*c
= trans
->c
;
502 struct qreg arg
= src
[0 * 4 + i
];
503 return qir_FMAXABS(c
, arg
, arg
);
506 /* Note that this instruction replicates its result from the x channel */
508 tgsi_to_qir_sin(struct tgsi_to_qir
*trans
,
509 struct tgsi_full_instruction
*tgsi_inst
,
510 enum qop op
, struct qreg
*src
, int i
)
512 struct qcompile
*c
= trans
->c
;
515 -pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
516 pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
517 -pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
520 struct qreg scaled_x
=
523 qir_uniform_f(trans
, 1.0f
/ (M_PI
* 2.0f
)));
526 struct qreg x
= tgsi_to_qir_frc(trans
, NULL
, 0, &scaled_x
, 0);
527 struct qreg x2
= qir_FMUL(c
, x
, x
);
528 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(trans
, coeff
[0]));
529 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
530 x
= qir_FMUL(c
, x
, x2
);
535 qir_uniform_f(trans
, coeff
[i
])));
540 /* Note that this instruction replicates its result from the x channel */
542 tgsi_to_qir_cos(struct tgsi_to_qir
*trans
,
543 struct tgsi_full_instruction
*tgsi_inst
,
544 enum qop op
, struct qreg
*src
, int i
)
546 struct qcompile
*c
= trans
->c
;
549 -pow(2.0 * M_PI
, 2) / (2 * 1),
550 pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
551 -pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
554 struct qreg scaled_x
=
555 qir_FMUL(c
, src
[0 * 4 + 0],
556 qir_uniform_f(trans
, 1.0f
/ (M_PI
* 2.0f
)));
557 struct qreg x_frac
= tgsi_to_qir_frc(trans
, NULL
, 0, &scaled_x
, 0);
559 struct qreg sum
= qir_uniform_f(trans
, coeff
[0]);
560 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
561 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
562 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
564 x
= qir_FMUL(c
, x
, x2
);
566 struct qreg mul
= qir_FMUL(c
,
568 qir_uniform_f(trans
, coeff
[i
]));
572 sum
= qir_FADD(c
, sum
, mul
);
578 emit_vertex_input(struct tgsi_to_qir
*trans
, int attr
)
580 enum pipe_format format
= trans
->vs_key
->attr_formats
[attr
];
581 struct qcompile
*c
= trans
->c
;
582 struct qreg vpm_reads
[4];
584 /* Right now, we're setting the VPM offsets to be 16 bytes wide every
585 * time, so we always read 4 32-bit VPM entries.
587 for (int i
= 0; i
< 4; i
++) {
588 vpm_reads
[i
] = qir_get_temp(c
);
589 qir_emit(c
, qir_inst(QOP_VPM_READ
,
596 bool format_warned
= false;
597 const struct util_format_description
*desc
=
598 util_format_description(format
);
600 for (int i
= 0; i
< 4; i
++) {
601 uint8_t swiz
= desc
->swizzle
[i
];
603 if (swiz
<= UTIL_FORMAT_SWIZZLE_W
&&
605 (desc
->channel
[swiz
].type
!= UTIL_FORMAT_TYPE_FLOAT
||
606 desc
->channel
[swiz
].size
!= 32)) {
608 "vtx element %d unsupported type: %s\n",
609 attr
, util_format_name(format
));
610 format_warned
= true;
613 trans
->inputs
[attr
* 4 + i
] =
614 get_swizzled_channel(trans
, vpm_reads
, swiz
);
619 emit_fragcoord_input(struct tgsi_to_qir
*trans
, int attr
)
621 struct qcompile
*c
= trans
->c
;
623 trans
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
624 trans
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
625 trans
->inputs
[attr
* 4 + 2] =
628 qir_uniform_f(trans
, 1.0 / 0xffffff));
629 trans
->inputs
[attr
* 4 + 3] = qir_FRAG_RCP_W(c
);
633 emit_fragment_varying(struct tgsi_to_qir
*trans
, int index
)
635 struct qcompile
*c
= trans
->c
;
642 /* XXX: multiply by W */
643 return qir_VARY_ADD_C(c
, qir_MOV(c
, vary
));
647 emit_fragment_input(struct tgsi_to_qir
*trans
, int attr
)
649 struct qcompile
*c
= trans
->c
;
651 for (int i
= 0; i
< 4; i
++) {
652 trans
->inputs
[attr
* 4 + i
] =
653 emit_fragment_varying(trans
, attr
* 4 + i
);
659 emit_tgsi_declaration(struct tgsi_to_qir
*trans
,
660 struct tgsi_full_declaration
*decl
)
662 struct qcompile
*c
= trans
->c
;
664 switch (decl
->Declaration
.File
) {
665 case TGSI_FILE_INPUT
:
666 for (int i
= decl
->Range
.First
;
667 i
<= decl
->Range
.Last
;
669 if (c
->stage
== QSTAGE_FRAG
) {
670 if (decl
->Semantic
.Name
==
671 TGSI_SEMANTIC_POSITION
) {
672 emit_fragcoord_input(trans
, i
);
674 emit_fragment_input(trans
, i
);
677 emit_vertex_input(trans
, i
);
685 emit_tgsi_instruction(struct tgsi_to_qir
*trans
,
686 struct tgsi_full_instruction
*tgsi_inst
)
688 struct qcompile
*c
= trans
->c
;
691 struct qreg (*func
)(struct tgsi_to_qir
*trans
,
692 struct tgsi_full_instruction
*tgsi_inst
,
694 struct qreg
*src
, int i
);
696 [TGSI_OPCODE_MOV
] = { QOP_MOV
, tgsi_to_qir_alu
},
697 [TGSI_OPCODE_ABS
] = { 0, tgsi_to_qir_abs
},
698 [TGSI_OPCODE_MUL
] = { QOP_FMUL
, tgsi_to_qir_alu
},
699 [TGSI_OPCODE_ADD
] = { QOP_FADD
, tgsi_to_qir_alu
},
700 [TGSI_OPCODE_SUB
] = { QOP_FSUB
, tgsi_to_qir_alu
},
701 [TGSI_OPCODE_MIN
] = { QOP_FMIN
, tgsi_to_qir_alu
},
702 [TGSI_OPCODE_MAX
] = { QOP_FMAX
, tgsi_to_qir_alu
},
703 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
704 [TGSI_OPCODE_SEQ
] = { QOP_SEQ
, tgsi_to_qir_alu
},
705 [TGSI_OPCODE_SNE
] = { QOP_SNE
, tgsi_to_qir_alu
},
706 [TGSI_OPCODE_SGE
] = { QOP_SGE
, tgsi_to_qir_alu
},
707 [TGSI_OPCODE_SLT
] = { QOP_SLT
, tgsi_to_qir_alu
},
708 [TGSI_OPCODE_CMP
] = { QOP_CMP
, tgsi_to_qir_alu
},
709 [TGSI_OPCODE_MAD
] = { 0, tgsi_to_qir_mad
},
710 [TGSI_OPCODE_DP2
] = { 0, tgsi_to_qir_dp2
},
711 [TGSI_OPCODE_DP3
] = { 0, tgsi_to_qir_dp3
},
712 [TGSI_OPCODE_DP4
] = { 0, tgsi_to_qir_dp4
},
713 [TGSI_OPCODE_RCP
] = { QOP_RCP
, tgsi_to_qir_alu
},
714 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
715 [TGSI_OPCODE_EX2
] = { QOP_EXP2
, tgsi_to_qir_alu
},
716 [TGSI_OPCODE_LG2
] = { QOP_LOG2
, tgsi_to_qir_alu
},
717 [TGSI_OPCODE_LIT
] = { 0, tgsi_to_qir_lit
},
718 [TGSI_OPCODE_LRP
] = { 0, tgsi_to_qir_lrp
},
719 [TGSI_OPCODE_POW
] = { 0, tgsi_to_qir_pow
},
720 [TGSI_OPCODE_TRUNC
] = { 0, tgsi_to_qir_trunc
},
721 [TGSI_OPCODE_FRC
] = { 0, tgsi_to_qir_frc
},
722 [TGSI_OPCODE_FLR
] = { 0, tgsi_to_qir_flr
},
723 [TGSI_OPCODE_SIN
] = { 0, tgsi_to_qir_sin
},
724 [TGSI_OPCODE_COS
] = { 0, tgsi_to_qir_cos
},
727 uint32_t tgsi_op
= tgsi_inst
->Instruction
.Opcode
;
729 if (tgsi_op
== TGSI_OPCODE_END
)
732 struct qreg src_regs
[12];
733 for (int s
= 0; s
< 3; s
++) {
734 for (int i
= 0; i
< 4; i
++) {
735 src_regs
[4 * s
+ i
] =
736 get_src(trans
, &tgsi_inst
->Src
[s
].Register
, i
);
741 case TGSI_OPCODE_TEX
:
742 case TGSI_OPCODE_TXP
:
743 case TGSI_OPCODE_TXB
:
744 tgsi_to_qir_tex(trans
, tgsi_inst
,
745 op_trans
[tgsi_op
].op
, src_regs
);
751 if (tgsi_op
> ARRAY_SIZE(op_trans
) || !(op_trans
[tgsi_op
].func
)) {
752 fprintf(stderr
, "unknown tgsi inst: ");
753 tgsi_dump_instruction(tgsi_inst
, asdf
++);
754 fprintf(stderr
, "\n");
758 for (int i
= 0; i
< 4; i
++) {
759 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
764 result
= op_trans
[tgsi_op
].func(trans
, tgsi_inst
,
765 op_trans
[tgsi_op
].op
,
768 if (tgsi_inst
->Instruction
.Saturate
) {
769 float low
= (tgsi_inst
->Instruction
.Saturate
==
770 TGSI_SAT_MINUS_PLUS_ONE
? -1.0 : 0.0);
774 qir_uniform_f(trans
, 1.0)),
775 qir_uniform_f(trans
, low
));
778 update_dst(trans
, tgsi_inst
, i
, result
);
783 parse_tgsi_immediate(struct tgsi_to_qir
*trans
, struct tgsi_full_immediate
*imm
)
785 for (int i
= 0; i
< 4; i
++) {
786 unsigned n
= trans
->num_consts
++;
787 trans
->consts
[n
] = qir_uniform_ui(trans
, imm
->u
[i
].Uint
);
792 vc4_blend_channel(struct tgsi_to_qir
*trans
,
799 struct qcompile
*c
= trans
->c
;
802 case PIPE_BLENDFACTOR_ONE
:
804 case PIPE_BLENDFACTOR_SRC_COLOR
:
805 return qir_FMUL(c
, val
, src
[channel
]);
806 case PIPE_BLENDFACTOR_SRC_ALPHA
:
807 return qir_FMUL(c
, val
, src
[3]);
808 case PIPE_BLENDFACTOR_DST_ALPHA
:
809 return qir_FMUL(c
, val
, dst
[3]);
810 case PIPE_BLENDFACTOR_DST_COLOR
:
811 return qir_FMUL(c
, val
, dst
[channel
]);
812 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
813 return qir_FMIN(c
, src
[3], qir_FSUB(c
,
814 qir_uniform_f(trans
, 1.0),
816 case PIPE_BLENDFACTOR_CONST_COLOR
:
817 return qir_FMUL(c
, val
,
818 get_temp_for_uniform(trans
,
819 QUNIFORM_BLEND_CONST_COLOR
,
821 case PIPE_BLENDFACTOR_CONST_ALPHA
:
822 return qir_FMUL(c
, val
,
823 get_temp_for_uniform(trans
,
824 QUNIFORM_BLEND_CONST_COLOR
,
826 case PIPE_BLENDFACTOR_ZERO
:
827 return qir_uniform_f(trans
, 0.0);
828 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
829 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
831 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
832 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
834 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
835 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
837 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
838 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
840 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
841 return qir_FMUL(c
, val
,
842 qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
843 get_temp_for_uniform(trans
,
844 QUNIFORM_BLEND_CONST_COLOR
,
846 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
847 return qir_FMUL(c
, val
,
848 qir_FSUB(c
, qir_uniform_f(trans
, 1.0),
849 get_temp_for_uniform(trans
,
850 QUNIFORM_BLEND_CONST_COLOR
,
854 case PIPE_BLENDFACTOR_SRC1_COLOR
:
855 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
856 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
857 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
859 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
865 vc4_blend_func(struct tgsi_to_qir
*trans
,
866 struct qreg src
, struct qreg dst
,
869 struct qcompile
*c
= trans
->c
;
873 return qir_FADD(c
, src
, dst
);
874 case PIPE_BLEND_SUBTRACT
:
875 return qir_FSUB(c
, src
, dst
);
876 case PIPE_BLEND_REVERSE_SUBTRACT
:
877 return qir_FSUB(c
, dst
, src
);
879 return qir_FMIN(c
, src
, dst
);
881 return qir_FMAX(c
, src
, dst
);
885 fprintf(stderr
, "Unknown blend func %d\n", func
);
892 * Implements fixed function blending in shader code.
894 * VC4 doesn't have any hardware support for blending. Instead, you read the
895 * current contents of the destination from the tile buffer after having
896 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
897 * math using your output color and that destination value, and update the
898 * output color appropriately.
901 vc4_blend(struct tgsi_to_qir
*trans
, struct qreg
*result
,
902 struct qreg
*dst_color
, struct qreg
*src_color
)
904 struct pipe_rt_blend_state
*blend
= &trans
->fs_key
->blend
;
906 if (!blend
->blend_enable
) {
907 for (int i
= 0; i
< 4; i
++)
908 result
[i
] = src_color
[i
];
912 struct qreg src_blend
[4], dst_blend
[4];
913 for (int i
= 0; i
< 3; i
++) {
914 src_blend
[i
] = vc4_blend_channel(trans
,
915 dst_color
, src_color
,
917 blend
->rgb_src_factor
, i
);
918 dst_blend
[i
] = vc4_blend_channel(trans
,
919 dst_color
, src_color
,
921 blend
->rgb_dst_factor
, i
);
923 src_blend
[3] = vc4_blend_channel(trans
,
924 dst_color
, src_color
,
926 blend
->alpha_src_factor
, 3);
927 dst_blend
[3] = vc4_blend_channel(trans
,
928 dst_color
, src_color
,
930 blend
->alpha_dst_factor
, 3);
932 for (int i
= 0; i
< 3; i
++) {
933 result
[i
] = vc4_blend_func(trans
,
934 src_blend
[i
], dst_blend
[i
],
937 result
[3] = vc4_blend_func(trans
,
938 src_blend
[3], dst_blend
[3],
943 emit_frag_end(struct tgsi_to_qir
*trans
)
945 struct qcompile
*c
= trans
->c
;
947 struct qreg t
= qir_get_temp(c
);
949 const struct util_format_description
*format_desc
=
950 util_format_description(trans
->fs_key
->color_format
);
952 struct qreg src_color
[4] = {
953 trans
->outputs
[0], trans
->outputs
[1],
954 trans
->outputs
[2], trans
->outputs
[3],
957 struct qreg dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
958 if (trans
->fs_key
->blend
.blend_enable
||
959 trans
->fs_key
->blend
.colormask
!= 0xf) {
960 qir_emit(c
, qir_inst(QOP_TLB_COLOR_READ
, c
->undef
,
961 c
->undef
, c
->undef
));
962 for (int i
= 0; i
< 4; i
++) {
963 dst_color
[i
] = qir_R4_UNPACK(c
, i
);
969 struct qreg blend_color
[4];
970 vc4_blend(trans
, blend_color
, dst_color
, src_color
);
972 /* If the bit isn't set in the color mask, then just return the
973 * original dst color, instead.
975 for (int i
= 0; i
< 4; i
++) {
976 if (!(trans
->fs_key
->blend
.colormask
& (1 << i
))) {
977 blend_color
[i
] = dst_color
[i
];
981 /* Debug: Sometimes you're getting a black output and just want to see
982 * if the FS is getting executed at all. Spam magenta into the color
986 blend_color
[0] = qir_uniform_f(trans
, 1.0);
987 blend_color
[1] = qir_uniform_f(trans
, 0.0);
988 blend_color
[2] = qir_uniform_f(trans
, 1.0);
989 blend_color
[3] = qir_uniform_f(trans
, 0.5);
992 struct qreg swizzled_outputs
[4];
993 for (int i
= 0; i
< 4; i
++) {
994 swizzled_outputs
[i
] =
995 get_swizzled_channel(trans
, blend_color
,
996 format_desc
->swizzle
[i
]);
999 if (trans
->fs_key
->depth_enabled
) {
1000 qir_emit(c
, qir_inst(QOP_TLB_PASSTHROUGH_Z_WRITE
, c
->undef
,
1001 c
->undef
, c
->undef
));
1004 qir_emit(c
, qir_inst4(QOP_PACK_COLORS
, t
,
1005 swizzled_outputs
[0],
1006 swizzled_outputs
[1],
1007 swizzled_outputs
[2],
1008 swizzled_outputs
[3]));
1009 qir_emit(c
, qir_inst(QOP_TLB_COLOR_WRITE
, c
->undef
,
1014 emit_scaled_viewport_write(struct tgsi_to_qir
*trans
, struct qreg rcp_w
)
1016 struct qcompile
*c
= trans
->c
;
1019 for (int i
= 0; i
< 2; i
++) {
1021 add_uniform(trans
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1023 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1030 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1034 emit_zs_write(struct tgsi_to_qir
*trans
, struct qreg rcp_w
)
1036 struct qcompile
*c
= trans
->c
;
1038 struct qreg zscale
= add_uniform(trans
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1039 struct qreg zoffset
= add_uniform(trans
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1041 qir_VPM_WRITE(c
, qir_FMUL(c
, qir_FADD(c
, qir_FMUL(c
,
1049 emit_rcp_wc_write(struct tgsi_to_qir
*trans
, struct qreg rcp_w
)
1051 struct qcompile
*c
= trans
->c
;
1053 qir_VPM_WRITE(c
, rcp_w
);
1057 emit_vert_end(struct tgsi_to_qir
*trans
)
1059 struct qcompile
*c
= trans
->c
;
1061 struct qreg rcp_w
= qir_RCP(c
, trans
->outputs
[3]);
1063 emit_scaled_viewport_write(trans
, rcp_w
);
1064 emit_zs_write(trans
, rcp_w
);
1065 emit_rcp_wc_write(trans
, rcp_w
);
1067 for (int i
= 4; i
< trans
->num_outputs
; i
++) {
1068 qir_VPM_WRITE(c
, trans
->outputs
[i
]);
1073 emit_coord_end(struct tgsi_to_qir
*trans
)
1075 struct qcompile
*c
= trans
->c
;
1077 struct qreg rcp_w
= qir_RCP(c
, trans
->outputs
[3]);
1079 for (int i
= 0; i
< 4; i
++)
1080 qir_VPM_WRITE(c
, trans
->outputs
[i
]);
1082 emit_scaled_viewport_write(trans
, rcp_w
);
1083 emit_zs_write(trans
, rcp_w
);
1084 emit_rcp_wc_write(trans
, rcp_w
);
1087 static struct tgsi_to_qir
*
1088 vc4_shader_tgsi_to_qir(struct vc4_compiled_shader
*shader
, enum qstage stage
,
1089 struct vc4_key
*key
)
1091 struct tgsi_to_qir
*trans
= CALLOC_STRUCT(tgsi_to_qir
);
1095 c
= qir_compile_init();
1098 memset(trans
, 0, sizeof(*trans
));
1100 trans
->temps
= calloc(sizeof(struct qreg
), 1024);
1101 trans
->inputs
= calloc(sizeof(struct qreg
), 8 * 4);
1102 trans
->outputs
= calloc(sizeof(struct qreg
), 1024);
1103 trans
->uniforms
= calloc(sizeof(struct qreg
), 1024);
1104 trans
->consts
= calloc(sizeof(struct qreg
), 1024);
1106 trans
->uniform_data
= calloc(sizeof(uint32_t), 1024);
1107 trans
->uniform_contents
= calloc(sizeof(enum quniform_contents
), 1024);
1109 trans
->shader_state
= key
->shader_state
;
1111 ret
= tgsi_parse_init(&trans
->parser
, trans
->shader_state
->tokens
);
1112 assert(ret
== TGSI_PARSE_OK
);
1114 if (vc4_debug
& VC4_DEBUG_TGSI
) {
1115 fprintf(stderr
, "TGSI:\n");
1116 tgsi_dump(trans
->shader_state
->tokens
, 0);
1122 trans
->fs_key
= (struct vc4_fs_key
*)key
;
1123 if (trans
->fs_key
->is_points
) {
1124 trans
->point_x
= emit_fragment_varying(trans
, 0);
1125 trans
->point_y
= emit_fragment_varying(trans
, 0);
1126 } else if (trans
->fs_key
->is_lines
) {
1127 trans
->line_x
= emit_fragment_varying(trans
, 0);
1131 trans
->vs_key
= (struct vc4_vs_key
*)key
;
1134 trans
->vs_key
= (struct vc4_vs_key
*)key
;
1138 while (!tgsi_parse_end_of_tokens(&trans
->parser
)) {
1139 tgsi_parse_token(&trans
->parser
);
1141 switch (trans
->parser
.FullToken
.Token
.Type
) {
1142 case TGSI_TOKEN_TYPE_DECLARATION
:
1143 emit_tgsi_declaration(trans
,
1144 &trans
->parser
.FullToken
.FullDeclaration
);
1147 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1148 emit_tgsi_instruction(trans
,
1149 &trans
->parser
.FullToken
.FullInstruction
);
1152 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1153 parse_tgsi_immediate(trans
,
1154 &trans
->parser
.FullToken
.FullImmediate
);
1161 emit_frag_end(trans
);
1164 emit_vert_end(trans
);
1167 emit_coord_end(trans
);
1171 tgsi_parse_free(&trans
->parser
);
1176 if (vc4_debug
& VC4_DEBUG_QIR
) {
1177 fprintf(stderr
, "QIR:\n");
1180 vc4_generate_code(c
);
1182 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
1183 fprintf(stderr
, "SHADER-DB: %s: %d instructions\n",
1184 qir_get_stage_name(c
->stage
), c
->qpu_inst_count
);
1185 fprintf(stderr
, "SHADER-DB: %s: %d uniforms\n",
1186 qir_get_stage_name(c
->stage
), trans
->num_uniforms
);
1193 vc4_shader_state_create(struct pipe_context
*pctx
,
1194 const struct pipe_shader_state
*cso
)
1196 struct pipe_shader_state
*so
= CALLOC_STRUCT(pipe_shader_state
);
1200 so
->tokens
= tgsi_dup_tokens(cso
->tokens
);
1206 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
1208 struct tgsi_to_qir
*trans
)
1210 int count
= trans
->num_uniforms
;
1211 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1213 uinfo
->count
= count
;
1214 uinfo
->data
= malloc(count
* sizeof(*uinfo
->data
));
1215 memcpy(uinfo
->data
, trans
->uniform_data
,
1216 count
* sizeof(*uinfo
->data
));
1217 uinfo
->contents
= malloc(count
* sizeof(*uinfo
->contents
));
1218 memcpy(uinfo
->contents
, trans
->uniform_contents
,
1219 count
* sizeof(*uinfo
->contents
));
1220 uinfo
->num_texture_samples
= trans
->num_texture_samples
;
1224 vc4_fs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1225 struct vc4_fs_key
*key
)
1227 struct tgsi_to_qir
*trans
= vc4_shader_tgsi_to_qir(shader
, QSTAGE_FRAG
,
1229 shader
->num_inputs
= trans
->c
->num_inputs
;
1230 copy_uniform_state_to_shader(shader
, 0, trans
);
1231 shader
->bo
= vc4_bo_alloc_mem(vc4
->screen
, trans
->c
->qpu_insts
,
1232 trans
->c
->qpu_inst_count
* sizeof(uint64_t),
1235 qir_compile_destroy(trans
->c
);
1240 vc4_vs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1241 struct vc4_vs_key
*key
)
1243 struct tgsi_to_qir
*vs_trans
= vc4_shader_tgsi_to_qir(shader
,
1246 copy_uniform_state_to_shader(shader
, 0, vs_trans
);
1248 struct tgsi_to_qir
*cs_trans
= vc4_shader_tgsi_to_qir(shader
,
1251 copy_uniform_state_to_shader(shader
, 1, cs_trans
);
1253 uint32_t vs_size
= vs_trans
->c
->qpu_inst_count
* sizeof(uint64_t);
1254 uint32_t cs_size
= cs_trans
->c
->qpu_inst_count
* sizeof(uint64_t);
1255 shader
->coord_shader_offset
= vs_size
; /* XXX: alignment? */
1256 shader
->bo
= vc4_bo_alloc(vc4
->screen
,
1257 shader
->coord_shader_offset
+ cs_size
,
1260 void *map
= vc4_bo_map(shader
->bo
);
1261 memcpy(map
, vs_trans
->c
->qpu_insts
, vs_size
);
1262 memcpy(map
+ shader
->coord_shader_offset
,
1263 cs_trans
->c
->qpu_insts
, cs_size
);
1265 qir_compile_destroy(vs_trans
->c
);
1266 qir_compile_destroy(cs_trans
->c
);
1270 vc4_setup_shared_key(struct vc4_key
*key
, struct vc4_texture_stateobj
*texstate
)
1272 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
1273 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
1275 struct pipe_resource
*prsc
= sampler
->texture
;
1276 key
->tex_format
[i
] = prsc
->format
;
1282 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
1284 struct vc4_fs_key local_key
;
1285 struct vc4_fs_key
*key
= &local_key
;
1287 memset(key
, 0, sizeof(*key
));
1288 vc4_setup_shared_key(&key
->base
, &vc4
->fragtex
);
1289 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
1290 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
1291 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
1292 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
1293 key
->blend
= vc4
->blend
->rt
[0];
1295 if (vc4
->framebuffer
.cbufs
[0])
1296 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
1298 key
->depth_enabled
= vc4
->zsa
->base
.depth
.enabled
;
1300 vc4
->prog
.fs
= util_hash_table_get(vc4
->fs_cache
, key
);
1304 key
= malloc(sizeof(*key
));
1305 memcpy(key
, &local_key
, sizeof(*key
));
1307 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1308 vc4_fs_compile(vc4
, shader
, key
);
1309 util_hash_table_set(vc4
->fs_cache
, key
, shader
);
1311 vc4
->prog
.fs
= shader
;
1315 vc4_update_compiled_vs(struct vc4_context
*vc4
)
1317 struct vc4_vs_key local_key
;
1318 struct vc4_vs_key
*key
= &local_key
;
1320 memset(key
, 0, sizeof(*key
));
1321 vc4_setup_shared_key(&key
->base
, &vc4
->verttex
);
1322 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
1324 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
1325 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
1327 vc4
->prog
.vs
= util_hash_table_get(vc4
->vs_cache
, key
);
1331 key
= malloc(sizeof(*key
));
1332 memcpy(key
, &local_key
, sizeof(*key
));
1334 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1335 vc4_vs_compile(vc4
, shader
, key
);
1336 util_hash_table_set(vc4
->vs_cache
, key
, shader
);
1338 vc4
->prog
.vs
= shader
;
1342 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
1344 vc4_update_compiled_fs(vc4
, prim_mode
);
1345 vc4_update_compiled_vs(vc4
);
1349 fs_cache_hash(void *key
)
1351 return util_hash_crc32(key
, sizeof(struct vc4_fs_key
));
1355 vs_cache_hash(void *key
)
1357 return util_hash_crc32(key
, sizeof(struct vc4_vs_key
));
1361 fs_cache_compare(void *key1
, void *key2
)
1363 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
));
1367 vs_cache_compare(void *key1
, void *key2
)
1369 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
));
1372 struct delete_state
{
1373 struct vc4_context
*vc4
;
1374 struct pipe_shader_state
*shader_state
;
1377 static enum pipe_error
1378 fs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1380 struct delete_state
*del
= data
;
1381 struct vc4_fs_key
*key
= in_key
;
1382 struct vc4_compiled_shader
*shader
= in_value
;
1384 if (key
->base
.shader_state
== data
) {
1385 util_hash_table_remove(del
->vc4
->fs_cache
, key
);
1386 vc4_bo_unreference(&shader
->bo
);
1393 static enum pipe_error
1394 vs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1396 struct delete_state
*del
= data
;
1397 struct vc4_vs_key
*key
= in_key
;
1398 struct vc4_compiled_shader
*shader
= in_value
;
1400 if (key
->base
.shader_state
== data
) {
1401 util_hash_table_remove(del
->vc4
->vs_cache
, key
);
1402 vc4_bo_unreference(&shader
->bo
);
1410 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
1412 struct vc4_context
*vc4
= vc4_context(pctx
);
1413 struct pipe_shader_state
*so
= hwcso
;
1414 struct delete_state del
;
1417 del
.shader_state
= so
;
1418 util_hash_table_foreach(vc4
->fs_cache
, fs_delete_from_cache
, &del
);
1419 util_hash_table_foreach(vc4
->vs_cache
, vs_delete_from_cache
, &del
);
1421 free((void *)so
->tokens
);
1425 static uint32_t translate_wrap(uint32_t p_wrap
)
1428 case PIPE_TEX_WRAP_REPEAT
:
1430 case PIPE_TEX_WRAP_CLAMP
:
1431 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1433 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1435 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1438 fprintf(stderr
, "Unknown wrap mode %d\n", p_wrap
);
1439 assert(!"not reached");
1445 write_texture_p0(struct vc4_context
*vc4
,
1446 struct vc4_texture_stateobj
*texstate
,
1447 uint32_t tex_and_sampler
)
1449 uint32_t texi
= (tex_and_sampler
>> 0) & 0xff;
1450 struct pipe_sampler_view
*texture
= texstate
->textures
[texi
];
1451 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
1453 cl_reloc(vc4
, &vc4
->uniforms
, rsc
->bo
,
1454 rsc
->slices
[0].offset
| texture
->u
.tex
.last_level
);
1458 write_texture_p1(struct vc4_context
*vc4
,
1459 struct vc4_texture_stateobj
*texstate
,
1460 uint32_t tex_and_sampler
)
1462 uint32_t texi
= (tex_and_sampler
>> 0) & 0xff;
1463 uint32_t sampi
= (tex_and_sampler
>> 8) & 0xff;
1464 struct pipe_sampler_view
*texture
= texstate
->textures
[texi
];
1465 struct pipe_sampler_state
*sampler
= texstate
->samplers
[sampi
];
1466 static const uint32_t mipfilter_map
[] = {
1467 [PIPE_TEX_MIPFILTER_NEAREST
] = 2,
1468 [PIPE_TEX_MIPFILTER_LINEAR
] = 4,
1469 [PIPE_TEX_MIPFILTER_NONE
] = 0
1471 static const uint32_t imgfilter_map
[] = {
1472 [PIPE_TEX_FILTER_NEAREST
] = 1,
1473 [PIPE_TEX_FILTER_LINEAR
] = 0,
1476 cl_u32(&vc4
->uniforms
,
1477 (1 << 31) /* XXX: data type */|
1478 (texture
->texture
->height0
<< 20) |
1479 (texture
->texture
->width0
<< 8) |
1480 (imgfilter_map
[sampler
->mag_img_filter
] << 7) |
1481 ((imgfilter_map
[sampler
->min_img_filter
] +
1482 mipfilter_map
[sampler
->min_mip_filter
]) << 4) |
1483 (translate_wrap(sampler
->wrap_t
) << 2) |
1484 (translate_wrap(sampler
->wrap_s
) << 0));
1488 get_texrect_scale(struct vc4_texture_stateobj
*texstate
,
1489 enum quniform_contents contents
,
1492 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
1495 if (contents
== QUNIFORM_TEXRECT_SCALE_X
)
1496 dim
= texture
->texture
->width0
;
1498 dim
= texture
->texture
->height0
;
1500 return fui(1.0f
/ dim
);
1504 vc4_write_uniforms(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1505 struct vc4_constbuf_stateobj
*cb
,
1506 struct vc4_texture_stateobj
*texstate
,
1509 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1510 const uint32_t *gallium_uniforms
= cb
->cb
[0].user_buffer
;
1512 cl_start_shader_reloc(&vc4
->uniforms
, uinfo
->num_texture_samples
);
1514 for (int i
= 0; i
< uinfo
->count
; i
++) {
1516 switch (uinfo
->contents
[i
]) {
1517 case QUNIFORM_CONSTANT
:
1518 cl_u32(&vc4
->uniforms
, uinfo
->data
[i
]);
1520 case QUNIFORM_UNIFORM
:
1521 cl_u32(&vc4
->uniforms
,
1522 gallium_uniforms
[uinfo
->data
[i
]]);
1524 case QUNIFORM_VIEWPORT_X_SCALE
:
1525 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[0] * 16.0f
);
1527 case QUNIFORM_VIEWPORT_Y_SCALE
:
1528 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[1] * 16.0f
);
1531 case QUNIFORM_VIEWPORT_Z_OFFSET
:
1532 cl_f(&vc4
->uniforms
, vc4
->viewport
.translate
[2]);
1534 case QUNIFORM_VIEWPORT_Z_SCALE
:
1535 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[2]);
1538 case QUNIFORM_TEXTURE_CONFIG_P0
:
1539 write_texture_p0(vc4
, texstate
, uinfo
->data
[i
]);
1542 case QUNIFORM_TEXTURE_CONFIG_P1
:
1543 write_texture_p1(vc4
, texstate
, uinfo
->data
[i
]);
1546 case QUNIFORM_TEXRECT_SCALE_X
:
1547 case QUNIFORM_TEXRECT_SCALE_Y
:
1548 cl_u32(&vc4
->uniforms
,
1549 get_texrect_scale(texstate
,
1554 case QUNIFORM_BLEND_CONST_COLOR
:
1555 cl_f(&vc4
->uniforms
,
1556 vc4
->blend_color
.color
[uinfo
->data
[i
]]);
1560 uint32_t written_val
= *(uint32_t *)(vc4
->uniforms
.next
- 4);
1561 fprintf(stderr
, "%p/%d: %d: 0x%08x (%f)\n",
1562 shader
, shader_index
, i
, written_val
, uif(written_val
));
1568 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1570 struct vc4_context
*vc4
= vc4_context(pctx
);
1571 vc4
->prog
.bind_fs
= hwcso
;
1572 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_FP
;
1573 vc4
->dirty
|= VC4_DIRTY_PROG
;
1577 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1579 struct vc4_context
*vc4
= vc4_context(pctx
);
1580 vc4
->prog
.bind_vs
= hwcso
;
1581 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_VP
;
1582 vc4
->dirty
|= VC4_DIRTY_PROG
;
1586 vc4_program_init(struct pipe_context
*pctx
)
1588 struct vc4_context
*vc4
= vc4_context(pctx
);
1590 pctx
->create_vs_state
= vc4_shader_state_create
;
1591 pctx
->delete_vs_state
= vc4_shader_state_delete
;
1593 pctx
->create_fs_state
= vc4_shader_state_create
;
1594 pctx
->delete_fs_state
= vc4_shader_state_delete
;
1596 pctx
->bind_fs_state
= vc4_fp_state_bind
;
1597 pctx
->bind_vs_state
= vc4_vp_state_bind
;
1599 vc4
->fs_cache
= util_hash_table_create(fs_cache_hash
, fs_cache_compare
);
1600 vc4
->vs_cache
= util_hash_table_create(vs_cache_hash
, vs_cache_compare
);