2 * Copyright (c) 2014 Scott Mansell
3 * Copyright © 2014 Broadcom
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "pipe/p_state.h"
27 #include "util/u_format.h"
28 #include "util/u_hash_table.h"
29 #include "util/u_hash.h"
30 #include "util/u_memory.h"
31 #include "util/ralloc.h"
32 #include "tgsi/tgsi_dump.h"
33 #include "tgsi/tgsi_info.h"
35 #include "vc4_context.h"
38 #ifdef USE_VC4_SIMULATOR
39 #include "simpenrose/simpenrose.h"
43 struct pipe_shader_state
*shader_state
;
45 enum pipe_format format
;
46 unsigned compare_mode
:1;
47 unsigned compare_func
:3;
49 } tex
[VC4_MAX_TEXTURE_SAMPLERS
];
54 enum pipe_format color_format
;
59 struct pipe_rt_blend_state blend
;
64 enum pipe_format attr_formats
[8];
68 add_uniform(struct vc4_compile
*c
,
69 enum quniform_contents contents
,
72 uint32_t uniform
= c
->num_uniforms
++;
73 struct qreg u
= { QFILE_UNIF
, uniform
};
75 c
->uniform_contents
[uniform
] = contents
;
76 c
->uniform_data
[uniform
] = data
;
82 get_temp_for_uniform(struct vc4_compile
*c
, enum quniform_contents contents
,
85 for (int i
= 0; i
< c
->num_uniforms
; i
++) {
86 if (c
->uniform_contents
[i
] == contents
&&
87 c
->uniform_data
[i
] == data
)
88 return c
->uniforms
[i
];
91 struct qreg u
= add_uniform(c
, contents
, data
);
92 struct qreg t
= qir_MOV(c
, u
);
94 c
->uniforms
[u
.index
] = t
;
99 qir_uniform_ui(struct vc4_compile
*c
, uint32_t ui
)
101 return get_temp_for_uniform(c
, QUNIFORM_CONSTANT
, ui
);
105 qir_uniform_f(struct vc4_compile
*c
, float f
)
107 return qir_uniform_ui(c
, fui(f
));
111 get_src(struct vc4_compile
*c
, unsigned tgsi_op
,
112 struct tgsi_src_register
*src
, int i
)
114 struct qreg r
= c
->undef
;
134 assert(!src
->Indirect
);
139 case TGSI_FILE_TEMPORARY
:
140 r
= c
->temps
[src
->Index
* 4 + s
];
142 case TGSI_FILE_IMMEDIATE
:
143 r
= c
->consts
[src
->Index
* 4 + s
];
145 case TGSI_FILE_CONSTANT
:
146 r
= get_temp_for_uniform(c
, QUNIFORM_UNIFORM
,
149 case TGSI_FILE_INPUT
:
150 r
= c
->inputs
[src
->Index
* 4 + s
];
152 case TGSI_FILE_SAMPLER
:
153 case TGSI_FILE_SAMPLER_VIEW
:
157 fprintf(stderr
, "unknown src file %d\n", src
->File
);
162 r
= qir_FMAXABS(c
, r
, r
);
165 switch (tgsi_opcode_infer_src_type(tgsi_op
)) {
166 case TGSI_TYPE_SIGNED
:
167 case TGSI_TYPE_UNSIGNED
:
168 r
= qir_SUB(c
, qir_uniform_ui(c
, 0), r
);
171 r
= qir_FSUB(c
, qir_uniform_f(c
, 0.0), r
);
181 update_dst(struct vc4_compile
*c
, struct tgsi_full_instruction
*tgsi_inst
,
182 int i
, struct qreg val
)
184 struct tgsi_dst_register
*tgsi_dst
= &tgsi_inst
->Dst
[0].Register
;
186 assert(!tgsi_dst
->Indirect
);
188 switch (tgsi_dst
->File
) {
189 case TGSI_FILE_TEMPORARY
:
190 c
->temps
[tgsi_dst
->Index
* 4 + i
] = val
;
192 case TGSI_FILE_OUTPUT
:
193 c
->outputs
[tgsi_dst
->Index
* 4 + i
] = val
;
194 c
->num_outputs
= MAX2(c
->num_outputs
,
195 tgsi_dst
->Index
* 4 + i
+ 1);
198 fprintf(stderr
, "unknown dst file %d\n", tgsi_dst
->File
);
204 get_swizzled_channel(struct vc4_compile
*c
,
205 struct qreg
*srcs
, int swiz
)
209 case UTIL_FORMAT_SWIZZLE_NONE
:
210 fprintf(stderr
, "warning: unknown swizzle\n");
212 case UTIL_FORMAT_SWIZZLE_0
:
213 return qir_uniform_f(c
, 0.0);
214 case UTIL_FORMAT_SWIZZLE_1
:
215 return qir_uniform_f(c
, 1.0);
216 case UTIL_FORMAT_SWIZZLE_X
:
217 case UTIL_FORMAT_SWIZZLE_Y
:
218 case UTIL_FORMAT_SWIZZLE_Z
:
219 case UTIL_FORMAT_SWIZZLE_W
:
225 tgsi_to_qir_alu(struct vc4_compile
*c
,
226 struct tgsi_full_instruction
*tgsi_inst
,
227 enum qop op
, struct qreg
*src
, int i
)
229 struct qreg dst
= qir_get_temp(c
);
230 qir_emit(c
, qir_inst4(op
, dst
,
239 tgsi_to_qir_umul(struct vc4_compile
*c
,
240 struct tgsi_full_instruction
*tgsi_inst
,
241 enum qop op
, struct qreg
*src
, int i
)
243 struct qreg src0_hi
= qir_SHR(c
, src
[0 * 4 + i
],
244 qir_uniform_ui(c
, 16));
245 struct qreg src0_lo
= qir_AND(c
, src
[0 * 4 + i
],
246 qir_uniform_ui(c
, 0xffff));
247 struct qreg src1_hi
= qir_SHR(c
, src
[1 * 4 + i
],
248 qir_uniform_ui(c
, 16));
249 struct qreg src1_lo
= qir_AND(c
, src
[1 * 4 + i
],
250 qir_uniform_ui(c
, 0xffff));
252 struct qreg hilo
= qir_MUL24(c
, src0_hi
, src1_lo
);
253 struct qreg lohi
= qir_MUL24(c
, src0_lo
, src1_hi
);
254 struct qreg lolo
= qir_MUL24(c
, src0_lo
, src1_lo
);
256 return qir_ADD(c
, lolo
, qir_SHL(c
,
257 qir_ADD(c
, hilo
, lohi
),
258 qir_uniform_ui(c
, 16)));
262 tgsi_to_qir_idiv(struct vc4_compile
*c
,
263 struct tgsi_full_instruction
*tgsi_inst
,
264 enum qop op
, struct qreg
*src
, int i
)
266 return qir_FTOI(c
, qir_FMUL(c
,
267 qir_ITOF(c
, src
[0 * 4 + i
]),
268 qir_RCP(c
, qir_ITOF(c
, src
[1 * 4 + i
]))));
272 tgsi_to_qir_ineg(struct vc4_compile
*c
,
273 struct tgsi_full_instruction
*tgsi_inst
,
274 enum qop op
, struct qreg
*src
, int i
)
276 return qir_SUB(c
, qir_uniform_ui(c
, 0), src
[0 * 4 + i
]);
280 tgsi_to_qir_seq(struct vc4_compile
*c
,
281 struct tgsi_full_instruction
*tgsi_inst
,
282 enum qop op
, struct qreg
*src
, int i
)
284 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
285 return qir_SEL_X_0_ZS(c
, qir_uniform_f(c
, 1.0));
289 tgsi_to_qir_sne(struct vc4_compile
*c
,
290 struct tgsi_full_instruction
*tgsi_inst
,
291 enum qop op
, struct qreg
*src
, int i
)
293 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
294 return qir_SEL_X_0_ZC(c
, qir_uniform_f(c
, 1.0));
298 tgsi_to_qir_slt(struct vc4_compile
*c
,
299 struct tgsi_full_instruction
*tgsi_inst
,
300 enum qop op
, struct qreg
*src
, int i
)
302 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
303 return qir_SEL_X_0_NS(c
, qir_uniform_f(c
, 1.0));
307 tgsi_to_qir_sge(struct vc4_compile
*c
,
308 struct tgsi_full_instruction
*tgsi_inst
,
309 enum qop op
, struct qreg
*src
, int i
)
311 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
312 return qir_SEL_X_0_NC(c
, qir_uniform_f(c
, 1.0));
316 tgsi_to_qir_fseq(struct vc4_compile
*c
,
317 struct tgsi_full_instruction
*tgsi_inst
,
318 enum qop op
, struct qreg
*src
, int i
)
320 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
321 return qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
325 tgsi_to_qir_fsne(struct vc4_compile
*c
,
326 struct tgsi_full_instruction
*tgsi_inst
,
327 enum qop op
, struct qreg
*src
, int i
)
329 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
330 return qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
334 tgsi_to_qir_fslt(struct vc4_compile
*c
,
335 struct tgsi_full_instruction
*tgsi_inst
,
336 enum qop op
, struct qreg
*src
, int i
)
338 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
339 return qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
343 tgsi_to_qir_fsge(struct vc4_compile
*c
,
344 struct tgsi_full_instruction
*tgsi_inst
,
345 enum qop op
, struct qreg
*src
, int i
)
347 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
348 return qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
352 tgsi_to_qir_useq(struct vc4_compile
*c
,
353 struct tgsi_full_instruction
*tgsi_inst
,
354 enum qop op
, struct qreg
*src
, int i
)
356 qir_SF(c
, qir_SUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
357 return qir_SEL_X_0_ZS(c
, qir_uniform_ui(c
, ~0));
361 tgsi_to_qir_usne(struct vc4_compile
*c
,
362 struct tgsi_full_instruction
*tgsi_inst
,
363 enum qop op
, struct qreg
*src
, int i
)
365 qir_SF(c
, qir_SUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
366 return qir_SEL_X_0_ZC(c
, qir_uniform_ui(c
, ~0));
370 tgsi_to_qir_islt(struct vc4_compile
*c
,
371 struct tgsi_full_instruction
*tgsi_inst
,
372 enum qop op
, struct qreg
*src
, int i
)
374 qir_SF(c
, qir_SUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
375 return qir_SEL_X_0_NS(c
, qir_uniform_ui(c
, ~0));
379 tgsi_to_qir_isge(struct vc4_compile
*c
,
380 struct tgsi_full_instruction
*tgsi_inst
,
381 enum qop op
, struct qreg
*src
, int i
)
383 qir_SF(c
, qir_SUB(c
, src
[0 * 4 + i
], src
[1 * 4 + i
]));
384 return qir_SEL_X_0_NC(c
, qir_uniform_ui(c
, ~0));
388 tgsi_to_qir_cmp(struct vc4_compile
*c
,
389 struct tgsi_full_instruction
*tgsi_inst
,
390 enum qop op
, struct qreg
*src
, int i
)
392 qir_SF(c
, src
[0 * 4 + i
]);
393 return qir_SEL_X_Y_NS(c
,
399 tgsi_to_qir_mad(struct vc4_compile
*c
,
400 struct tgsi_full_instruction
*tgsi_inst
,
401 enum qop op
, struct qreg
*src
, int i
)
411 tgsi_to_qir_lit(struct vc4_compile
*c
,
412 struct tgsi_full_instruction
*tgsi_inst
,
413 enum qop op
, struct qreg
*src
, int i
)
415 struct qreg x
= src
[0 * 4 + 0];
416 struct qreg y
= src
[0 * 4 + 1];
417 struct qreg w
= src
[0 * 4 + 3];
422 return qir_uniform_f(c
, 1.0);
424 return qir_FMAX(c
, src
[0 * 4 + 0], qir_uniform_f(c
, 0.0));
426 struct qreg zero
= qir_uniform_f(c
, 0.0);
429 /* XXX: Clamp w to -128..128 */
430 return qir_SEL_X_0_NC(c
,
431 qir_EXP2(c
, qir_FMUL(c
,
439 assert(!"not reached");
445 tgsi_to_qir_lrp(struct vc4_compile
*c
,
446 struct tgsi_full_instruction
*tgsi_inst
,
447 enum qop op
, struct qreg
*src
, int i
)
449 struct qreg src0
= src
[0 * 4 + i
];
450 struct qreg src1
= src
[1 * 4 + i
];
451 struct qreg src2
= src
[2 * 4 + i
];
454 * src0 * src1 + (1 - src0) * src2.
455 * -> src0 * src1 + src2 - src0 * src2
456 * -> src2 + src0 * (src1 - src2)
458 return qir_FADD(c
, src2
, qir_FMUL(c
, src0
, qir_FSUB(c
, src1
, src2
)));
463 tgsi_to_qir_tex(struct vc4_compile
*c
,
464 struct tgsi_full_instruction
*tgsi_inst
,
465 enum qop op
, struct qreg
*src
)
467 assert(!tgsi_inst
->Instruction
.Saturate
);
469 struct qreg s
= src
[0 * 4 + 0];
470 struct qreg t
= src
[0 * 4 + 1];
471 uint32_t unit
= tgsi_inst
->Src
[1].Register
.Index
;
473 struct qreg proj
= c
->undef
;
474 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXP
) {
475 proj
= qir_RCP(c
, src
[0 * 4 + 3]);
476 s
= qir_FMUL(c
, s
, proj
);
477 t
= qir_FMUL(c
, t
, proj
);
480 /* There is no native support for GL texture rectangle coordinates, so
481 * we have to rescale from ([0, width], [0, height]) to ([0, 1], [0,
484 if (tgsi_inst
->Texture
.Texture
== TGSI_TEXTURE_RECT
||
485 tgsi_inst
->Texture
.Texture
== TGSI_TEXTURE_SHADOWRECT
) {
487 get_temp_for_uniform(c
,
488 QUNIFORM_TEXRECT_SCALE_X
,
491 get_temp_for_uniform(c
,
492 QUNIFORM_TEXRECT_SCALE_Y
,
496 qir_TEX_T(c
, t
, add_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P0
, unit
));
498 struct qreg sampler_p1
= add_uniform(c
, QUNIFORM_TEXTURE_CONFIG_P1
,
500 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXB
) {
501 qir_TEX_B(c
, src
[0 * 4 + 3], sampler_p1
);
502 qir_TEX_S(c
, s
, add_uniform(c
, QUNIFORM_CONSTANT
, 0));
504 qir_TEX_S(c
, s
, sampler_p1
);
507 c
->num_texture_samples
++;
508 struct qreg r4
= qir_TEX_RESULT(c
);
510 enum pipe_format format
= c
->key
->tex
[unit
].format
;
512 struct qreg unpacked
[4];
513 if (util_format_is_depth_or_stencil(format
)) {
514 struct qreg depthf
= qir_ITOF(c
, qir_SHR(c
, r4
,
515 qir_uniform_ui(c
, 8)));
516 struct qreg normalized
= qir_FMUL(c
, depthf
,
517 qir_uniform_f(c
, 1.0f
/0xffffff));
519 struct qreg depth_output
;
521 struct qreg compare
= src
[0 * 4 + 2];
523 if (tgsi_inst
->Instruction
.Opcode
== TGSI_OPCODE_TXP
)
524 compare
= qir_FMUL(c
, compare
, proj
);
526 struct qreg one
= qir_uniform_f(c
, 1.0f
);
527 if (c
->key
->tex
[unit
].compare_mode
) {
528 switch (c
->key
->tex
[unit
].compare_func
) {
529 case PIPE_FUNC_NEVER
:
530 depth_output
= qir_uniform_f(c
, 0.0f
);
532 case PIPE_FUNC_ALWAYS
:
535 case PIPE_FUNC_EQUAL
:
536 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
537 depth_output
= qir_SEL_X_0_ZS(c
, one
);
539 case PIPE_FUNC_NOTEQUAL
:
540 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
541 depth_output
= qir_SEL_X_0_ZC(c
, one
);
543 case PIPE_FUNC_GREATER
:
544 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
545 depth_output
= qir_SEL_X_0_NC(c
, one
);
547 case PIPE_FUNC_GEQUAL
:
548 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
549 depth_output
= qir_SEL_X_0_NS(c
, one
);
552 qir_SF(c
, qir_FSUB(c
, compare
, normalized
));
553 depth_output
= qir_SEL_X_0_NS(c
, one
);
555 case PIPE_FUNC_LEQUAL
:
556 qir_SF(c
, qir_FSUB(c
, normalized
, compare
));
557 depth_output
= qir_SEL_X_0_NC(c
, one
);
561 depth_output
= normalized
;
564 for (int i
= 0; i
< 4; i
++)
565 unpacked
[i
] = depth_output
;
567 for (int i
= 0; i
< 4; i
++)
568 unpacked
[i
] = qir_R4_UNPACK(c
, r4
, i
);
571 const uint8_t *format_swiz
= vc4_get_format_swizzle(format
);
573 util_format_compose_swizzles(format_swiz
, c
->key
->tex
[unit
].swizzle
, swiz
);
574 for (int i
= 0; i
< 4; i
++) {
575 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
578 update_dst(c
, tgsi_inst
, i
,
579 get_swizzled_channel(c
, unpacked
, swiz
[i
]));
584 tgsi_to_qir_pow(struct vc4_compile
*c
,
585 struct tgsi_full_instruction
*tgsi_inst
,
586 enum qop op
, struct qreg
*src
, int i
)
588 /* Note that this instruction replicates its result from the x channel
590 return qir_EXP2(c
, qir_FMUL(c
,
592 qir_LOG2(c
, src
[0 * 4 + 0])));
596 tgsi_to_qir_trunc(struct vc4_compile
*c
,
597 struct tgsi_full_instruction
*tgsi_inst
,
598 enum qop op
, struct qreg
*src
, int i
)
600 return qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
604 * Computes x - floor(x), which is tricky because our FTOI truncates (rounds
608 tgsi_to_qir_frc(struct vc4_compile
*c
,
609 struct tgsi_full_instruction
*tgsi_inst
,
610 enum qop op
, struct qreg
*src
, int i
)
612 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
613 struct qreg diff
= qir_FSUB(c
, src
[0 * 4 + i
], trunc
);
615 return qir_SEL_X_Y_NS(c
,
616 qir_FADD(c
, diff
, qir_uniform_f(c
, 1.0)),
621 * Computes floor(x), which is tricky because our FTOI truncates (rounds to
625 tgsi_to_qir_flr(struct vc4_compile
*c
,
626 struct tgsi_full_instruction
*tgsi_inst
,
627 enum qop op
, struct qreg
*src
, int i
)
629 struct qreg trunc
= qir_ITOF(c
, qir_FTOI(c
, src
[0 * 4 + i
]));
631 /* This will be < 0 if we truncated and the truncation was of a value
632 * that was < 0 in the first place.
634 qir_SF(c
, qir_FSUB(c
, src
[0 * 4 + i
], trunc
));
636 return qir_SEL_X_Y_NS(c
,
637 qir_FSUB(c
, trunc
, qir_uniform_f(c
, 1.0)),
642 tgsi_to_qir_dp(struct vc4_compile
*c
,
643 struct tgsi_full_instruction
*tgsi_inst
,
644 int num
, struct qreg
*src
, int i
)
646 struct qreg sum
= qir_FMUL(c
, src
[0 * 4 + 0], src
[1 * 4 + 0]);
647 for (int j
= 1; j
< num
; j
++) {
648 sum
= qir_FADD(c
, sum
, qir_FMUL(c
,
656 tgsi_to_qir_dp2(struct vc4_compile
*c
,
657 struct tgsi_full_instruction
*tgsi_inst
,
658 enum qop op
, struct qreg
*src
, int i
)
660 return tgsi_to_qir_dp(c
, tgsi_inst
, 2, src
, i
);
664 tgsi_to_qir_dp3(struct vc4_compile
*c
,
665 struct tgsi_full_instruction
*tgsi_inst
,
666 enum qop op
, struct qreg
*src
, int i
)
668 return tgsi_to_qir_dp(c
, tgsi_inst
, 3, src
, i
);
672 tgsi_to_qir_dp4(struct vc4_compile
*c
,
673 struct tgsi_full_instruction
*tgsi_inst
,
674 enum qop op
, struct qreg
*src
, int i
)
676 return tgsi_to_qir_dp(c
, tgsi_inst
, 4, src
, i
);
680 tgsi_to_qir_abs(struct vc4_compile
*c
,
681 struct tgsi_full_instruction
*tgsi_inst
,
682 enum qop op
, struct qreg
*src
, int i
)
684 struct qreg arg
= src
[0 * 4 + i
];
685 return qir_FMAXABS(c
, arg
, arg
);
688 /* Note that this instruction replicates its result from the x channel */
690 tgsi_to_qir_sin(struct vc4_compile
*c
,
691 struct tgsi_full_instruction
*tgsi_inst
,
692 enum qop op
, struct qreg
*src
, int i
)
696 -pow(2.0 * M_PI
, 3) / (3 * 2 * 1),
697 pow(2.0 * M_PI
, 5) / (5 * 4 * 3 * 2 * 1),
698 -pow(2.0 * M_PI
, 7) / (7 * 6 * 5 * 4 * 3 * 2 * 1),
701 struct qreg scaled_x
=
704 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
707 struct qreg x
= tgsi_to_qir_frc(c
, NULL
, 0, &scaled_x
, 0);
708 struct qreg x2
= qir_FMUL(c
, x
, x
);
709 struct qreg sum
= qir_FMUL(c
, x
, qir_uniform_f(c
, coeff
[0]));
710 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
711 x
= qir_FMUL(c
, x
, x2
);
716 qir_uniform_f(c
, coeff
[i
])));
721 /* Note that this instruction replicates its result from the x channel */
723 tgsi_to_qir_cos(struct vc4_compile
*c
,
724 struct tgsi_full_instruction
*tgsi_inst
,
725 enum qop op
, struct qreg
*src
, int i
)
729 -pow(2.0 * M_PI
, 2) / (2 * 1),
730 pow(2.0 * M_PI
, 4) / (4 * 3 * 2 * 1),
731 -pow(2.0 * M_PI
, 6) / (6 * 5 * 4 * 3 * 2 * 1),
734 struct qreg scaled_x
=
735 qir_FMUL(c
, src
[0 * 4 + 0],
736 qir_uniform_f(c
, 1.0f
/ (M_PI
* 2.0f
)));
737 struct qreg x_frac
= tgsi_to_qir_frc(c
, NULL
, 0, &scaled_x
, 0);
739 struct qreg sum
= qir_uniform_f(c
, coeff
[0]);
740 struct qreg x2
= qir_FMUL(c
, x_frac
, x_frac
);
741 struct qreg x
= x2
; /* Current x^2, x^4, or x^6 */
742 for (int i
= 1; i
< ARRAY_SIZE(coeff
); i
++) {
744 x
= qir_FMUL(c
, x
, x2
);
746 struct qreg mul
= qir_FMUL(c
,
748 qir_uniform_f(c
, coeff
[i
]));
752 sum
= qir_FADD(c
, sum
, mul
);
758 emit_vertex_input(struct vc4_compile
*c
, int attr
)
760 enum pipe_format format
= c
->vs_key
->attr_formats
[attr
];
761 struct qreg vpm_reads
[4];
763 /* Right now, we're setting the VPM offsets to be 16 bytes wide every
764 * time, so we always read 4 32-bit VPM entries.
766 for (int i
= 0; i
< 4; i
++) {
767 vpm_reads
[i
] = qir_get_temp(c
);
768 qir_emit(c
, qir_inst(QOP_VPM_READ
,
775 bool format_warned
= false;
776 const struct util_format_description
*desc
=
777 util_format_description(format
);
779 for (int i
= 0; i
< 4; i
++) {
780 uint8_t swiz
= desc
->swizzle
[i
];
782 if (swiz
<= UTIL_FORMAT_SWIZZLE_W
&&
784 (desc
->channel
[swiz
].type
!= UTIL_FORMAT_TYPE_FLOAT
||
785 desc
->channel
[swiz
].size
!= 32)) {
787 "vtx element %d unsupported type: %s\n",
788 attr
, util_format_name(format
));
789 format_warned
= true;
792 c
->inputs
[attr
* 4 + i
] =
793 get_swizzled_channel(c
, vpm_reads
, swiz
);
798 tgsi_to_qir_kill_if(struct vc4_compile
*c
, struct qreg
*src
, int i
)
800 if (c
->discard
.file
== QFILE_NULL
)
801 c
->discard
= qir_uniform_f(c
, 0.0);
802 qir_SF(c
, src
[0 * 4 + i
]);
803 c
->discard
= qir_SEL_X_Y_NS(c
, qir_uniform_f(c
, 1.0),
808 emit_fragcoord_input(struct vc4_compile
*c
, int attr
)
810 c
->inputs
[attr
* 4 + 0] = qir_FRAG_X(c
);
811 c
->inputs
[attr
* 4 + 1] = qir_FRAG_Y(c
);
812 c
->inputs
[attr
* 4 + 2] =
815 qir_uniform_f(c
, 1.0 / 0xffffff));
816 c
->inputs
[attr
* 4 + 3] = qir_FRAG_RCP_W(c
);
820 emit_fragment_varying(struct vc4_compile
*c
, int index
)
827 /* XXX: multiply by W */
828 return qir_VARY_ADD_C(c
, qir_MOV(c
, vary
));
832 emit_fragment_input(struct vc4_compile
*c
, int attr
)
834 for (int i
= 0; i
< 4; i
++) {
835 c
->inputs
[attr
* 4 + i
] =
836 emit_fragment_varying(c
, attr
* 4 + i
);
842 emit_tgsi_declaration(struct vc4_compile
*c
,
843 struct tgsi_full_declaration
*decl
)
845 switch (decl
->Declaration
.File
) {
846 case TGSI_FILE_INPUT
:
847 for (int i
= decl
->Range
.First
;
848 i
<= decl
->Range
.Last
;
850 if (c
->stage
== QSTAGE_FRAG
) {
851 if (decl
->Semantic
.Name
==
852 TGSI_SEMANTIC_POSITION
) {
853 emit_fragcoord_input(c
, i
);
855 emit_fragment_input(c
, i
);
858 emit_vertex_input(c
, i
);
866 emit_tgsi_instruction(struct vc4_compile
*c
,
867 struct tgsi_full_instruction
*tgsi_inst
)
871 struct qreg (*func
)(struct vc4_compile
*c
,
872 struct tgsi_full_instruction
*tgsi_inst
,
874 struct qreg
*src
, int i
);
876 [TGSI_OPCODE_MOV
] = { QOP_MOV
, tgsi_to_qir_alu
},
877 [TGSI_OPCODE_ABS
] = { 0, tgsi_to_qir_abs
},
878 [TGSI_OPCODE_MUL
] = { QOP_FMUL
, tgsi_to_qir_alu
},
879 [TGSI_OPCODE_ADD
] = { QOP_FADD
, tgsi_to_qir_alu
},
880 [TGSI_OPCODE_SUB
] = { QOP_FSUB
, tgsi_to_qir_alu
},
881 [TGSI_OPCODE_MIN
] = { QOP_FMIN
, tgsi_to_qir_alu
},
882 [TGSI_OPCODE_MAX
] = { QOP_FMAX
, tgsi_to_qir_alu
},
883 [TGSI_OPCODE_F2I
] = { QOP_FTOI
, tgsi_to_qir_alu
},
884 [TGSI_OPCODE_I2F
] = { QOP_ITOF
, tgsi_to_qir_alu
},
885 [TGSI_OPCODE_UADD
] = { QOP_ADD
, tgsi_to_qir_alu
},
886 [TGSI_OPCODE_USHR
] = { QOP_SHR
, tgsi_to_qir_alu
},
887 [TGSI_OPCODE_ISHR
] = { QOP_ASR
, tgsi_to_qir_alu
},
888 [TGSI_OPCODE_SHL
] = { QOP_SHL
, tgsi_to_qir_alu
},
889 [TGSI_OPCODE_IMIN
] = { QOP_MIN
, tgsi_to_qir_alu
},
890 [TGSI_OPCODE_IMAX
] = { QOP_MAX
, tgsi_to_qir_alu
},
891 [TGSI_OPCODE_AND
] = { QOP_AND
, tgsi_to_qir_alu
},
892 [TGSI_OPCODE_OR
] = { QOP_OR
, tgsi_to_qir_alu
},
893 [TGSI_OPCODE_XOR
] = { QOP_XOR
, tgsi_to_qir_alu
},
894 [TGSI_OPCODE_NOT
] = { QOP_NOT
, tgsi_to_qir_alu
},
896 [TGSI_OPCODE_UMUL
] = { 0, tgsi_to_qir_umul
},
897 [TGSI_OPCODE_IDIV
] = { 0, tgsi_to_qir_idiv
},
898 [TGSI_OPCODE_INEG
] = { 0, tgsi_to_qir_ineg
},
900 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
901 [TGSI_OPCODE_SEQ
] = { 0, tgsi_to_qir_seq
},
902 [TGSI_OPCODE_SNE
] = { 0, tgsi_to_qir_sne
},
903 [TGSI_OPCODE_SGE
] = { 0, tgsi_to_qir_sge
},
904 [TGSI_OPCODE_SLT
] = { 0, tgsi_to_qir_slt
},
905 [TGSI_OPCODE_FSEQ
] = { 0, tgsi_to_qir_fseq
},
906 [TGSI_OPCODE_FSNE
] = { 0, tgsi_to_qir_fsne
},
907 [TGSI_OPCODE_FSGE
] = { 0, tgsi_to_qir_fsge
},
908 [TGSI_OPCODE_FSLT
] = { 0, tgsi_to_qir_fslt
},
909 [TGSI_OPCODE_USEQ
] = { 0, tgsi_to_qir_useq
},
910 [TGSI_OPCODE_USNE
] = { 0, tgsi_to_qir_usne
},
911 [TGSI_OPCODE_ISGE
] = { 0, tgsi_to_qir_isge
},
912 [TGSI_OPCODE_ISLT
] = { 0, tgsi_to_qir_islt
},
914 [TGSI_OPCODE_CMP
] = { 0, tgsi_to_qir_cmp
},
915 [TGSI_OPCODE_MAD
] = { 0, tgsi_to_qir_mad
},
916 [TGSI_OPCODE_DP2
] = { 0, tgsi_to_qir_dp2
},
917 [TGSI_OPCODE_DP3
] = { 0, tgsi_to_qir_dp3
},
918 [TGSI_OPCODE_DP4
] = { 0, tgsi_to_qir_dp4
},
919 [TGSI_OPCODE_RCP
] = { QOP_RCP
, tgsi_to_qir_alu
},
920 [TGSI_OPCODE_RSQ
] = { QOP_RSQ
, tgsi_to_qir_alu
},
921 [TGSI_OPCODE_EX2
] = { QOP_EXP2
, tgsi_to_qir_alu
},
922 [TGSI_OPCODE_LG2
] = { QOP_LOG2
, tgsi_to_qir_alu
},
923 [TGSI_OPCODE_LIT
] = { 0, tgsi_to_qir_lit
},
924 [TGSI_OPCODE_LRP
] = { 0, tgsi_to_qir_lrp
},
925 [TGSI_OPCODE_POW
] = { 0, tgsi_to_qir_pow
},
926 [TGSI_OPCODE_TRUNC
] = { 0, tgsi_to_qir_trunc
},
927 [TGSI_OPCODE_FRC
] = { 0, tgsi_to_qir_frc
},
928 [TGSI_OPCODE_FLR
] = { 0, tgsi_to_qir_flr
},
929 [TGSI_OPCODE_SIN
] = { 0, tgsi_to_qir_sin
},
930 [TGSI_OPCODE_COS
] = { 0, tgsi_to_qir_cos
},
933 uint32_t tgsi_op
= tgsi_inst
->Instruction
.Opcode
;
935 if (tgsi_op
== TGSI_OPCODE_END
)
938 struct qreg src_regs
[12];
939 for (int s
= 0; s
< 3; s
++) {
940 for (int i
= 0; i
< 4; i
++) {
941 src_regs
[4 * s
+ i
] =
942 get_src(c
, tgsi_inst
->Instruction
.Opcode
,
943 &tgsi_inst
->Src
[s
].Register
, i
);
948 case TGSI_OPCODE_TEX
:
949 case TGSI_OPCODE_TXP
:
950 case TGSI_OPCODE_TXB
:
951 tgsi_to_qir_tex(c
, tgsi_inst
,
952 op_trans
[tgsi_op
].op
, src_regs
);
954 case TGSI_OPCODE_KILL
:
955 c
->discard
= qir_uniform_f(c
, 1.0);
957 case TGSI_OPCODE_KILL_IF
:
958 for (int i
= 0; i
< 4; i
++)
959 tgsi_to_qir_kill_if(c
, src_regs
, i
);
965 if (tgsi_op
> ARRAY_SIZE(op_trans
) || !(op_trans
[tgsi_op
].func
)) {
966 fprintf(stderr
, "unknown tgsi inst: ");
967 tgsi_dump_instruction(tgsi_inst
, asdf
++);
968 fprintf(stderr
, "\n");
972 for (int i
= 0; i
< 4; i
++) {
973 if (!(tgsi_inst
->Dst
[0].Register
.WriteMask
& (1 << i
)))
978 result
= op_trans
[tgsi_op
].func(c
, tgsi_inst
,
979 op_trans
[tgsi_op
].op
,
982 if (tgsi_inst
->Instruction
.Saturate
) {
983 float low
= (tgsi_inst
->Instruction
.Saturate
==
984 TGSI_SAT_MINUS_PLUS_ONE
? -1.0 : 0.0);
988 qir_uniform_f(c
, 1.0)),
989 qir_uniform_f(c
, low
));
992 update_dst(c
, tgsi_inst
, i
, result
);
997 parse_tgsi_immediate(struct vc4_compile
*c
, struct tgsi_full_immediate
*imm
)
999 for (int i
= 0; i
< 4; i
++) {
1000 unsigned n
= c
->num_consts
++;
1001 c
->consts
[n
] = qir_uniform_ui(c
, imm
->u
[i
].Uint
);
1006 vc4_blend_channel(struct vc4_compile
*c
,
1014 case PIPE_BLENDFACTOR_ONE
:
1016 case PIPE_BLENDFACTOR_SRC_COLOR
:
1017 return qir_FMUL(c
, val
, src
[channel
]);
1018 case PIPE_BLENDFACTOR_SRC_ALPHA
:
1019 return qir_FMUL(c
, val
, src
[3]);
1020 case PIPE_BLENDFACTOR_DST_ALPHA
:
1021 return qir_FMUL(c
, val
, dst
[3]);
1022 case PIPE_BLENDFACTOR_DST_COLOR
:
1023 return qir_FMUL(c
, val
, dst
[channel
]);
1024 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
1025 return qir_FMIN(c
, src
[3], qir_FSUB(c
,
1026 qir_uniform_f(c
, 1.0),
1028 case PIPE_BLENDFACTOR_CONST_COLOR
:
1029 return qir_FMUL(c
, val
,
1030 get_temp_for_uniform(c
,
1031 QUNIFORM_BLEND_CONST_COLOR
,
1033 case PIPE_BLENDFACTOR_CONST_ALPHA
:
1034 return qir_FMUL(c
, val
,
1035 get_temp_for_uniform(c
,
1036 QUNIFORM_BLEND_CONST_COLOR
,
1038 case PIPE_BLENDFACTOR_ZERO
:
1039 return qir_uniform_f(c
, 0.0);
1040 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
1041 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1043 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
1044 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1046 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
1047 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1049 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
1050 return qir_FMUL(c
, val
, qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1052 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
1053 return qir_FMUL(c
, val
,
1054 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1055 get_temp_for_uniform(c
,
1056 QUNIFORM_BLEND_CONST_COLOR
,
1058 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
1059 return qir_FMUL(c
, val
,
1060 qir_FSUB(c
, qir_uniform_f(c
, 1.0),
1061 get_temp_for_uniform(c
,
1062 QUNIFORM_BLEND_CONST_COLOR
,
1066 case PIPE_BLENDFACTOR_SRC1_COLOR
:
1067 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
1068 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
1069 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
1071 fprintf(stderr
, "Unknown blend factor %d\n", factor
);
1077 vc4_blend_func(struct vc4_compile
*c
,
1078 struct qreg src
, struct qreg dst
,
1082 case PIPE_BLEND_ADD
:
1083 return qir_FADD(c
, src
, dst
);
1084 case PIPE_BLEND_SUBTRACT
:
1085 return qir_FSUB(c
, src
, dst
);
1086 case PIPE_BLEND_REVERSE_SUBTRACT
:
1087 return qir_FSUB(c
, dst
, src
);
1088 case PIPE_BLEND_MIN
:
1089 return qir_FMIN(c
, src
, dst
);
1090 case PIPE_BLEND_MAX
:
1091 return qir_FMAX(c
, src
, dst
);
1095 fprintf(stderr
, "Unknown blend func %d\n", func
);
1102 * Implements fixed function blending in shader code.
1104 * VC4 doesn't have any hardware support for blending. Instead, you read the
1105 * current contents of the destination from the tile buffer after having
1106 * waited for the scoreboard (which is handled by vc4_qpu_emit.c), then do
1107 * math using your output color and that destination value, and update the
1108 * output color appropriately.
1111 vc4_blend(struct vc4_compile
*c
, struct qreg
*result
,
1112 struct qreg
*dst_color
, struct qreg
*src_color
)
1114 struct pipe_rt_blend_state
*blend
= &c
->fs_key
->blend
;
1116 if (!blend
->blend_enable
) {
1117 for (int i
= 0; i
< 4; i
++)
1118 result
[i
] = src_color
[i
];
1122 struct qreg src_blend
[4], dst_blend
[4];
1123 for (int i
= 0; i
< 3; i
++) {
1124 src_blend
[i
] = vc4_blend_channel(c
,
1125 dst_color
, src_color
,
1127 blend
->rgb_src_factor
, i
);
1128 dst_blend
[i
] = vc4_blend_channel(c
,
1129 dst_color
, src_color
,
1131 blend
->rgb_dst_factor
, i
);
1133 src_blend
[3] = vc4_blend_channel(c
,
1134 dst_color
, src_color
,
1136 blend
->alpha_src_factor
, 3);
1137 dst_blend
[3] = vc4_blend_channel(c
,
1138 dst_color
, src_color
,
1140 blend
->alpha_dst_factor
, 3);
1142 for (int i
= 0; i
< 3; i
++) {
1143 result
[i
] = vc4_blend_func(c
,
1144 src_blend
[i
], dst_blend
[i
],
1147 result
[3] = vc4_blend_func(c
,
1148 src_blend
[3], dst_blend
[3],
1153 emit_frag_end(struct vc4_compile
*c
)
1155 struct qreg src_color
[4] = {
1156 c
->outputs
[0], c
->outputs
[1], c
->outputs
[2], c
->outputs
[3],
1159 enum pipe_format color_format
= c
->fs_key
->color_format
;
1160 const uint8_t *format_swiz
= vc4_get_format_swizzle(color_format
);
1161 struct qreg tlb_read_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1162 struct qreg dst_color
[4] = { c
->undef
, c
->undef
, c
->undef
, c
->undef
};
1163 if (c
->fs_key
->blend
.blend_enable
||
1164 c
->fs_key
->blend
.colormask
!= 0xf) {
1165 struct qreg r4
= qir_TLB_COLOR_READ(c
);
1166 for (int i
= 0; i
< 4; i
++)
1167 tlb_read_color
[i
] = qir_R4_UNPACK(c
, r4
, i
);
1168 for (int i
= 0; i
< 4; i
++)
1169 dst_color
[i
] = get_swizzled_channel(c
,
1174 struct qreg blend_color
[4];
1175 vc4_blend(c
, blend_color
, dst_color
, src_color
);
1177 /* If the bit isn't set in the color mask, then just return the
1178 * original dst color, instead.
1180 for (int i
= 0; i
< 4; i
++) {
1181 if (!(c
->fs_key
->blend
.colormask
& (1 << i
))) {
1182 blend_color
[i
] = dst_color
[i
];
1186 /* Debug: Sometimes you're getting a black output and just want to see
1187 * if the FS is getting executed at all. Spam magenta into the color
1191 blend_color
[0] = qir_uniform_f(c
, 1.0);
1192 blend_color
[1] = qir_uniform_f(c
, 0.0);
1193 blend_color
[2] = qir_uniform_f(c
, 1.0);
1194 blend_color
[3] = qir_uniform_f(c
, 0.5);
1197 struct qreg swizzled_outputs
[4];
1198 for (int i
= 0; i
< 4; i
++) {
1199 swizzled_outputs
[i
] = get_swizzled_channel(c
, blend_color
,
1203 if (c
->discard
.file
!= QFILE_NULL
)
1204 qir_TLB_DISCARD_SETUP(c
, c
->discard
);
1206 if (c
->fs_key
->depth_enabled
) {
1207 qir_emit(c
, qir_inst(QOP_TLB_PASSTHROUGH_Z_WRITE
, c
->undef
,
1208 c
->undef
, c
->undef
));
1211 bool color_written
= false;
1212 for (int i
= 0; i
< 4; i
++) {
1213 if (swizzled_outputs
[i
].file
!= QFILE_NULL
)
1214 color_written
= true;
1217 struct qreg packed_color
;
1218 if (color_written
) {
1219 /* Fill in any undefined colors. The simulator will assertion
1220 * fail if we read something that wasn't written, and I don't
1221 * know what hardware does.
1223 for (int i
= 0; i
< 4; i
++) {
1224 if (swizzled_outputs
[i
].file
== QFILE_NULL
)
1225 swizzled_outputs
[i
] = qir_uniform_f(c
, 0.0);
1227 packed_color
= qir_get_temp(c
);
1228 qir_emit(c
, qir_inst4(QOP_PACK_COLORS
, packed_color
,
1229 swizzled_outputs
[0],
1230 swizzled_outputs
[1],
1231 swizzled_outputs
[2],
1232 swizzled_outputs
[3]));
1234 packed_color
= qir_uniform_ui(c
, 0);
1237 qir_emit(c
, qir_inst(QOP_TLB_COLOR_WRITE
, c
->undef
,
1238 packed_color
, c
->undef
));
1242 emit_scaled_viewport_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1246 for (int i
= 0; i
< 2; i
++) {
1248 add_uniform(c
, QUNIFORM_VIEWPORT_X_SCALE
+ i
, 0);
1250 xyi
[i
] = qir_FTOI(c
, qir_FMUL(c
,
1257 qir_VPM_WRITE(c
, qir_PACK_SCALED(c
, xyi
[0], xyi
[1]));
1261 emit_zs_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1263 struct qreg zscale
= add_uniform(c
, QUNIFORM_VIEWPORT_Z_SCALE
, 0);
1264 struct qreg zoffset
= add_uniform(c
, QUNIFORM_VIEWPORT_Z_OFFSET
, 0);
1266 qir_VPM_WRITE(c
, qir_FMUL(c
, qir_FADD(c
, qir_FMUL(c
,
1274 emit_rcp_wc_write(struct vc4_compile
*c
, struct qreg rcp_w
)
1276 qir_VPM_WRITE(c
, rcp_w
);
1280 emit_vert_end(struct vc4_compile
*c
)
1282 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[3]);
1284 emit_scaled_viewport_write(c
, rcp_w
);
1285 emit_zs_write(c
, rcp_w
);
1286 emit_rcp_wc_write(c
, rcp_w
);
1288 for (int i
= 4; i
< c
->num_outputs
; i
++) {
1289 qir_VPM_WRITE(c
, c
->outputs
[i
]);
1294 emit_coord_end(struct vc4_compile
*c
)
1296 struct qreg rcp_w
= qir_RCP(c
, c
->outputs
[3]);
1298 for (int i
= 0; i
< 4; i
++)
1299 qir_VPM_WRITE(c
, c
->outputs
[i
]);
1301 emit_scaled_viewport_write(c
, rcp_w
);
1302 emit_zs_write(c
, rcp_w
);
1303 emit_rcp_wc_write(c
, rcp_w
);
1306 static struct vc4_compile
*
1307 vc4_shader_tgsi_to_qir(struct vc4_compiled_shader
*shader
, enum qstage stage
,
1308 struct vc4_key
*key
)
1310 struct vc4_compile
*c
= qir_compile_init();
1316 c
->temps
= ralloc_array(c
, struct qreg
, 1024);
1317 c
->inputs
= ralloc_array(c
, struct qreg
, 8 * 4);
1318 c
->outputs
= ralloc_array(c
, struct qreg
, 1024);
1319 c
->uniforms
= ralloc_array(c
, struct qreg
, 1024);
1320 c
->consts
= ralloc_array(c
, struct qreg
, 1024);
1322 c
->uniform_data
= ralloc_array(c
, uint32_t, 1024);
1323 c
->uniform_contents
= ralloc_array(c
, enum quniform_contents
, 1024);
1325 c
->shader_state
= key
->shader_state
;
1326 ret
= tgsi_parse_init(&c
->parser
, c
->shader_state
->tokens
);
1327 assert(ret
== TGSI_PARSE_OK
);
1329 if (vc4_debug
& VC4_DEBUG_TGSI
) {
1330 fprintf(stderr
, "TGSI:\n");
1331 tgsi_dump(c
->shader_state
->tokens
, 0);
1337 c
->fs_key
= (struct vc4_fs_key
*)key
;
1338 if (c
->fs_key
->is_points
) {
1339 c
->point_x
= emit_fragment_varying(c
, 0);
1340 c
->point_y
= emit_fragment_varying(c
, 0);
1341 } else if (c
->fs_key
->is_lines
) {
1342 c
->line_x
= emit_fragment_varying(c
, 0);
1346 c
->vs_key
= (struct vc4_vs_key
*)key
;
1349 c
->vs_key
= (struct vc4_vs_key
*)key
;
1353 while (!tgsi_parse_end_of_tokens(&c
->parser
)) {
1354 tgsi_parse_token(&c
->parser
);
1356 switch (c
->parser
.FullToken
.Token
.Type
) {
1357 case TGSI_TOKEN_TYPE_DECLARATION
:
1358 emit_tgsi_declaration(c
,
1359 &c
->parser
.FullToken
.FullDeclaration
);
1362 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1363 emit_tgsi_instruction(c
,
1364 &c
->parser
.FullToken
.FullInstruction
);
1367 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1368 parse_tgsi_immediate(c
,
1369 &c
->parser
.FullToken
.FullImmediate
);
1386 tgsi_parse_free(&c
->parser
);
1390 if (vc4_debug
& VC4_DEBUG_QIR
) {
1391 fprintf(stderr
, "QIR:\n");
1394 vc4_generate_code(c
);
1396 if (vc4_debug
& VC4_DEBUG_SHADERDB
) {
1397 fprintf(stderr
, "SHADER-DB: %s: %d instructions\n",
1398 qir_get_stage_name(c
->stage
), c
->qpu_inst_count
);
1399 fprintf(stderr
, "SHADER-DB: %s: %d uniforms\n",
1400 qir_get_stage_name(c
->stage
), c
->num_uniforms
);
1407 vc4_shader_state_create(struct pipe_context
*pctx
,
1408 const struct pipe_shader_state
*cso
)
1410 struct pipe_shader_state
*so
= CALLOC_STRUCT(pipe_shader_state
);
1414 so
->tokens
= tgsi_dup_tokens(cso
->tokens
);
1420 copy_uniform_state_to_shader(struct vc4_compiled_shader
*shader
,
1422 struct vc4_compile
*c
)
1424 int count
= c
->num_uniforms
;
1425 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1427 uinfo
->count
= count
;
1428 uinfo
->data
= malloc(count
* sizeof(*uinfo
->data
));
1429 memcpy(uinfo
->data
, c
->uniform_data
,
1430 count
* sizeof(*uinfo
->data
));
1431 uinfo
->contents
= malloc(count
* sizeof(*uinfo
->contents
));
1432 memcpy(uinfo
->contents
, c
->uniform_contents
,
1433 count
* sizeof(*uinfo
->contents
));
1434 uinfo
->num_texture_samples
= c
->num_texture_samples
;
1438 vc4_fs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1439 struct vc4_fs_key
*key
)
1441 struct vc4_compile
*c
= vc4_shader_tgsi_to_qir(shader
, QSTAGE_FRAG
,
1443 shader
->num_inputs
= c
->num_inputs
;
1444 copy_uniform_state_to_shader(shader
, 0, c
);
1445 shader
->bo
= vc4_bo_alloc_mem(vc4
->screen
, c
->qpu_insts
,
1446 c
->qpu_inst_count
* sizeof(uint64_t),
1449 qir_compile_destroy(c
);
1453 vc4_vs_compile(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1454 struct vc4_vs_key
*key
)
1456 struct vc4_compile
*vs_c
= vc4_shader_tgsi_to_qir(shader
,
1459 copy_uniform_state_to_shader(shader
, 0, vs_c
);
1461 struct vc4_compile
*cs_c
= vc4_shader_tgsi_to_qir(shader
,
1464 copy_uniform_state_to_shader(shader
, 1, cs_c
);
1466 uint32_t vs_size
= vs_c
->qpu_inst_count
* sizeof(uint64_t);
1467 uint32_t cs_size
= cs_c
->qpu_inst_count
* sizeof(uint64_t);
1468 shader
->coord_shader_offset
= vs_size
; /* XXX: alignment? */
1469 shader
->bo
= vc4_bo_alloc(vc4
->screen
,
1470 shader
->coord_shader_offset
+ cs_size
,
1473 void *map
= vc4_bo_map(shader
->bo
);
1474 memcpy(map
, vs_c
->qpu_insts
, vs_size
);
1475 memcpy(map
+ shader
->coord_shader_offset
,
1476 cs_c
->qpu_insts
, cs_size
);
1478 qir_compile_destroy(vs_c
);
1479 qir_compile_destroy(cs_c
);
1483 vc4_setup_shared_key(struct vc4_key
*key
, struct vc4_texture_stateobj
*texstate
)
1485 for (int i
= 0; i
< texstate
->num_textures
; i
++) {
1486 struct pipe_sampler_view
*sampler
= texstate
->textures
[i
];
1487 struct pipe_sampler_state
*sampler_state
=
1488 texstate
->samplers
[i
];
1491 struct pipe_resource
*prsc
= sampler
->texture
;
1492 key
->tex
[i
].format
= prsc
->format
;
1493 key
->tex
[i
].swizzle
[0] = sampler
->swizzle_r
;
1494 key
->tex
[i
].swizzle
[1] = sampler
->swizzle_g
;
1495 key
->tex
[i
].swizzle
[2] = sampler
->swizzle_b
;
1496 key
->tex
[i
].swizzle
[3] = sampler
->swizzle_a
;
1497 key
->tex
[i
].compare_mode
= sampler_state
->compare_mode
;
1498 key
->tex
[i
].compare_func
= sampler_state
->compare_func
;
1504 vc4_update_compiled_fs(struct vc4_context
*vc4
, uint8_t prim_mode
)
1506 struct vc4_fs_key local_key
;
1507 struct vc4_fs_key
*key
= &local_key
;
1509 memset(key
, 0, sizeof(*key
));
1510 vc4_setup_shared_key(&key
->base
, &vc4
->fragtex
);
1511 key
->base
.shader_state
= vc4
->prog
.bind_fs
;
1512 key
->is_points
= (prim_mode
== PIPE_PRIM_POINTS
);
1513 key
->is_lines
= (prim_mode
>= PIPE_PRIM_LINES
&&
1514 prim_mode
<= PIPE_PRIM_LINE_STRIP
);
1515 key
->blend
= vc4
->blend
->rt
[0];
1517 if (vc4
->framebuffer
.cbufs
[0])
1518 key
->color_format
= vc4
->framebuffer
.cbufs
[0]->format
;
1520 key
->depth_enabled
= vc4
->zsa
->base
.depth
.enabled
;
1522 vc4
->prog
.fs
= util_hash_table_get(vc4
->fs_cache
, key
);
1526 key
= malloc(sizeof(*key
));
1527 memcpy(key
, &local_key
, sizeof(*key
));
1529 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1530 vc4_fs_compile(vc4
, shader
, key
);
1531 util_hash_table_set(vc4
->fs_cache
, key
, shader
);
1533 vc4
->prog
.fs
= shader
;
1537 vc4_update_compiled_vs(struct vc4_context
*vc4
)
1539 struct vc4_vs_key local_key
;
1540 struct vc4_vs_key
*key
= &local_key
;
1542 memset(key
, 0, sizeof(*key
));
1543 vc4_setup_shared_key(&key
->base
, &vc4
->verttex
);
1544 key
->base
.shader_state
= vc4
->prog
.bind_vs
;
1546 for (int i
= 0; i
< ARRAY_SIZE(key
->attr_formats
); i
++)
1547 key
->attr_formats
[i
] = vc4
->vtx
->pipe
[i
].src_format
;
1549 vc4
->prog
.vs
= util_hash_table_get(vc4
->vs_cache
, key
);
1553 key
= malloc(sizeof(*key
));
1554 memcpy(key
, &local_key
, sizeof(*key
));
1556 struct vc4_compiled_shader
*shader
= CALLOC_STRUCT(vc4_compiled_shader
);
1557 vc4_vs_compile(vc4
, shader
, key
);
1558 util_hash_table_set(vc4
->vs_cache
, key
, shader
);
1560 vc4
->prog
.vs
= shader
;
1564 vc4_update_compiled_shaders(struct vc4_context
*vc4
, uint8_t prim_mode
)
1566 vc4_update_compiled_fs(vc4
, prim_mode
);
1567 vc4_update_compiled_vs(vc4
);
1571 fs_cache_hash(void *key
)
1573 return util_hash_crc32(key
, sizeof(struct vc4_fs_key
));
1577 vs_cache_hash(void *key
)
1579 return util_hash_crc32(key
, sizeof(struct vc4_vs_key
));
1583 fs_cache_compare(void *key1
, void *key2
)
1585 return memcmp(key1
, key2
, sizeof(struct vc4_fs_key
));
1589 vs_cache_compare(void *key1
, void *key2
)
1591 return memcmp(key1
, key2
, sizeof(struct vc4_vs_key
));
1594 struct delete_state
{
1595 struct vc4_context
*vc4
;
1596 struct pipe_shader_state
*shader_state
;
1599 static enum pipe_error
1600 fs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1602 struct delete_state
*del
= data
;
1603 struct vc4_fs_key
*key
= in_key
;
1604 struct vc4_compiled_shader
*shader
= in_value
;
1606 if (key
->base
.shader_state
== data
) {
1607 util_hash_table_remove(del
->vc4
->fs_cache
, key
);
1608 vc4_bo_unreference(&shader
->bo
);
1615 static enum pipe_error
1616 vs_delete_from_cache(void *in_key
, void *in_value
, void *data
)
1618 struct delete_state
*del
= data
;
1619 struct vc4_vs_key
*key
= in_key
;
1620 struct vc4_compiled_shader
*shader
= in_value
;
1622 if (key
->base
.shader_state
== data
) {
1623 util_hash_table_remove(del
->vc4
->vs_cache
, key
);
1624 vc4_bo_unreference(&shader
->bo
);
1632 vc4_shader_state_delete(struct pipe_context
*pctx
, void *hwcso
)
1634 struct vc4_context
*vc4
= vc4_context(pctx
);
1635 struct pipe_shader_state
*so
= hwcso
;
1636 struct delete_state del
;
1639 del
.shader_state
= so
;
1640 util_hash_table_foreach(vc4
->fs_cache
, fs_delete_from_cache
, &del
);
1641 util_hash_table_foreach(vc4
->vs_cache
, vs_delete_from_cache
, &del
);
1643 free((void *)so
->tokens
);
1647 static uint32_t translate_wrap(uint32_t p_wrap
)
1650 case PIPE_TEX_WRAP_REPEAT
:
1652 case PIPE_TEX_WRAP_CLAMP
:
1653 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1655 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1657 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1660 fprintf(stderr
, "Unknown wrap mode %d\n", p_wrap
);
1661 assert(!"not reached");
1667 write_texture_p0(struct vc4_context
*vc4
,
1668 struct vc4_texture_stateobj
*texstate
,
1671 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
1672 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
1674 cl_reloc(vc4
, &vc4
->uniforms
, rsc
->bo
,
1675 rsc
->slices
[0].offset
| texture
->u
.tex
.last_level
|
1676 ((rsc
->vc4_format
& 7) << 4));
1680 write_texture_p1(struct vc4_context
*vc4
,
1681 struct vc4_texture_stateobj
*texstate
,
1684 struct pipe_sampler_view
*texture
= texstate
->textures
[unit
];
1685 struct vc4_resource
*rsc
= vc4_resource(texture
->texture
);
1686 struct pipe_sampler_state
*sampler
= texstate
->samplers
[unit
];
1687 static const uint32_t mipfilter_map
[] = {
1688 [PIPE_TEX_MIPFILTER_NEAREST
] = 2,
1689 [PIPE_TEX_MIPFILTER_LINEAR
] = 4,
1690 [PIPE_TEX_MIPFILTER_NONE
] = 0
1692 static const uint32_t imgfilter_map
[] = {
1693 [PIPE_TEX_FILTER_NEAREST
] = 1,
1694 [PIPE_TEX_FILTER_LINEAR
] = 0,
1697 cl_u32(&vc4
->uniforms
,
1698 ((rsc
->vc4_format
>> 4) << 31) |
1699 (texture
->texture
->height0
<< 20) |
1700 (texture
->texture
->width0
<< 8) |
1701 (imgfilter_map
[sampler
->mag_img_filter
] << 7) |
1702 ((imgfilter_map
[sampler
->min_img_filter
] +
1703 mipfilter_map
[sampler
->min_mip_filter
]) << 4) |
1704 (translate_wrap(sampler
->wrap_t
) << 2) |
1705 (translate_wrap(sampler
->wrap_s
) << 0));
1709 get_texrect_scale(struct vc4_texture_stateobj
*texstate
,
1710 enum quniform_contents contents
,
1713 struct pipe_sampler_view
*texture
= texstate
->textures
[data
];
1716 if (contents
== QUNIFORM_TEXRECT_SCALE_X
)
1717 dim
= texture
->texture
->width0
;
1719 dim
= texture
->texture
->height0
;
1721 return fui(1.0f
/ dim
);
1725 vc4_write_uniforms(struct vc4_context
*vc4
, struct vc4_compiled_shader
*shader
,
1726 struct vc4_constbuf_stateobj
*cb
,
1727 struct vc4_texture_stateobj
*texstate
,
1730 struct vc4_shader_uniform_info
*uinfo
= &shader
->uniforms
[shader_index
];
1731 const uint32_t *gallium_uniforms
= cb
->cb
[0].user_buffer
;
1733 cl_start_shader_reloc(&vc4
->uniforms
, uinfo
->num_texture_samples
);
1735 for (int i
= 0; i
< uinfo
->count
; i
++) {
1737 switch (uinfo
->contents
[i
]) {
1738 case QUNIFORM_CONSTANT
:
1739 cl_u32(&vc4
->uniforms
, uinfo
->data
[i
]);
1741 case QUNIFORM_UNIFORM
:
1742 cl_u32(&vc4
->uniforms
,
1743 gallium_uniforms
[uinfo
->data
[i
]]);
1745 case QUNIFORM_VIEWPORT_X_SCALE
:
1746 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[0] * 16.0f
);
1748 case QUNIFORM_VIEWPORT_Y_SCALE
:
1749 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[1] * 16.0f
);
1752 case QUNIFORM_VIEWPORT_Z_OFFSET
:
1753 cl_f(&vc4
->uniforms
, vc4
->viewport
.translate
[2]);
1755 case QUNIFORM_VIEWPORT_Z_SCALE
:
1756 cl_f(&vc4
->uniforms
, vc4
->viewport
.scale
[2]);
1759 case QUNIFORM_TEXTURE_CONFIG_P0
:
1760 write_texture_p0(vc4
, texstate
, uinfo
->data
[i
]);
1763 case QUNIFORM_TEXTURE_CONFIG_P1
:
1764 write_texture_p1(vc4
, texstate
, uinfo
->data
[i
]);
1767 case QUNIFORM_TEXRECT_SCALE_X
:
1768 case QUNIFORM_TEXRECT_SCALE_Y
:
1769 cl_u32(&vc4
->uniforms
,
1770 get_texrect_scale(texstate
,
1775 case QUNIFORM_BLEND_CONST_COLOR
:
1776 cl_f(&vc4
->uniforms
,
1777 vc4
->blend_color
.color
[uinfo
->data
[i
]]);
1781 uint32_t written_val
= *(uint32_t *)(vc4
->uniforms
.next
- 4);
1782 fprintf(stderr
, "%p/%d: %d: 0x%08x (%f)\n",
1783 shader
, shader_index
, i
, written_val
, uif(written_val
));
1789 vc4_fp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1791 struct vc4_context
*vc4
= vc4_context(pctx
);
1792 vc4
->prog
.bind_fs
= hwcso
;
1793 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_FP
;
1794 vc4
->dirty
|= VC4_DIRTY_PROG
;
1798 vc4_vp_state_bind(struct pipe_context
*pctx
, void *hwcso
)
1800 struct vc4_context
*vc4
= vc4_context(pctx
);
1801 vc4
->prog
.bind_vs
= hwcso
;
1802 vc4
->prog
.dirty
|= VC4_SHADER_DIRTY_VP
;
1803 vc4
->dirty
|= VC4_DIRTY_PROG
;
1807 vc4_program_init(struct pipe_context
*pctx
)
1809 struct vc4_context
*vc4
= vc4_context(pctx
);
1811 pctx
->create_vs_state
= vc4_shader_state_create
;
1812 pctx
->delete_vs_state
= vc4_shader_state_delete
;
1814 pctx
->create_fs_state
= vc4_shader_state_create
;
1815 pctx
->delete_fs_state
= vc4_shader_state_delete
;
1817 pctx
->bind_fs_state
= vc4_fp_state_bind
;
1818 pctx
->bind_vs_state
= vc4_vp_state_bind
;
1820 vc4
->fs_cache
= util_hash_table_create(fs_cache_hash
, fs_cache_compare
);
1821 vc4
->vs_cache
= util_hash_table_create(vs_cache_hash
, vs_cache_compare
);