1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 * Generate SPU fragment program/shader code.
33 * Note that we generate SOA-style code here. So each TGSI instruction
34 * operates on four pixels (and is translated into four SPU instructions,
35 * generally speaking).
41 #include "pipe/p_defines.h"
42 #include "pipe/p_state.h"
43 #include "pipe/p_shader_tokens.h"
44 #include "tgsi/tgsi_parse.h"
45 #include "tgsi/tgsi_util.h"
46 #include "tgsi/tgsi_exec.h"
47 #include "tgsi/tgsi_dump.h"
48 #include "rtasm/rtasm_ppc_spe.h"
49 #include "util/u_memory.h"
50 #include "cell_context.h"
51 #include "cell_gen_fp.h"
54 /** Set to 1 to enable debug/disassembly printfs */
63 * Context needed during code generation.
67 int inputs_reg
; /**< 1st function parameter */
68 int outputs_reg
; /**< 2nd function parameter */
69 int constants_reg
; /**< 3rd function parameter */
70 int temp_regs
[MAX_TEMPS
][4]; /**< maps TGSI temps to SPE registers */
71 int imm_regs
[MAX_IMMED
][4]; /**< maps TGSI immediates to SPE registers */
73 int num_imm
; /**< number of immediates */
75 int one_reg
; /**< register containing {1.0, 1.0, 1.0, 1.0} */
77 /** Per-instruction temps / intermediate temps */
81 /** Current IF/ELSE/ENDIF nesting level */
83 /** Index of execution mask register */
86 struct spe_function
*f
;
92 * Allocate an intermediate temporary register.
95 get_itemp(struct codegen
*gen
)
97 int t
= spe_allocate_available_register(gen
->f
);
98 assert(gen
->num_itemps
< Elements(gen
->itemps
));
99 gen
->itemps
[gen
->num_itemps
++] = t
;
104 * Free all intermediate temporary registers. To be called after each
105 * instruction has been emitted.
108 free_itemps(struct codegen
*gen
)
111 for (i
= 0; i
< gen
->num_itemps
; i
++) {
112 spe_release_register(gen
->f
, gen
->itemps
[i
]);
119 * Return index of an SPE register containing {1.0, 1.0, 1.0, 1.0}.
120 * The register is allocated and initialized upon the first call.
123 get_const_one_reg(struct codegen
*gen
)
125 if (gen
->one_reg
<= 0) {
126 gen
->one_reg
= spe_allocate_available_register(gen
->f
);
128 spe_indent(gen
->f
, 4);
129 spe_comment(gen
->f
, -4, "INIT CONSTANT 1.0:");
131 /* one = {1.0, 1.0, 1.0, 1.0} */
132 spe_load_float(gen
->f
, gen
->one_reg
, 1.0f
);
134 spe_indent(gen
->f
, -4);
142 * Return index of the pixel execution mask.
143 * The register is allocated an initialized upon the first call.
145 * The pixel execution mask controls which pixels in a quad are
146 * modified, according to surrounding conditionals, loops, etc.
149 get_exec_mask_reg(struct codegen
*gen
)
151 if (gen
->exec_mask_reg
<= 0) {
152 gen
->exec_mask_reg
= spe_allocate_available_register(gen
->f
);
154 spe_indent(gen
->f
, 4);
155 spe_comment(gen
->f
, -4, "INIT EXEC MASK = ~0:");
157 /* exec_mask = {~0, ~0, ~0, ~0} */
158 spe_load_int(gen
->f
, gen
->exec_mask_reg
, ~0);
160 spe_indent(gen
->f
, -4);
163 return gen
->exec_mask_reg
;
168 * Return the index of the SPU temporary containing the named TGSI
169 * source register. If the TGSI register is a TGSI_FILE_TEMPORARY we
170 * just return the corresponding SPE register. If the TGIS register
171 * is TGSI_FILE_INPUT/CONSTANT/IMMEDIATE we allocate a new SPE register
172 * and emit an SPE load instruction.
175 get_src_reg(struct codegen
*gen
,
177 const struct tgsi_full_src_register
*src
)
180 int swizzle
= tgsi_util_get_full_src_register_extswizzle(src
, channel
);
181 boolean reg_is_itemp
= FALSE
;
184 assert(swizzle
>= 0);
185 assert(swizzle
<= 3);
189 switch (src
->SrcRegister
.File
) {
190 case TGSI_FILE_TEMPORARY
:
191 reg
= gen
->temp_regs
[src
->SrcRegister
.Index
][channel
];
193 case TGSI_FILE_INPUT
:
195 /* offset is measured in quadwords, not bytes */
196 int offset
= src
->SrcRegister
.Index
* 4 + channel
;
197 reg
= get_itemp(gen
);
199 /* Load: reg = memory[(machine_reg) + offset] */
200 spe_lqd(gen
->f
, reg
, gen
->inputs_reg
, offset
);
203 case TGSI_FILE_IMMEDIATE
:
204 reg
= gen
->imm_regs
[src
->SrcRegister
.Index
][channel
];
206 case TGSI_FILE_CONSTANT
:
207 /* xxx fall-through for now / fix */
213 * Handle absolute value, negate or set-negative of src register.
215 sign_op
= tgsi_util_get_full_src_register_sign_mode(src
, channel
);
216 if (sign_op
!= TGSI_UTIL_SIGN_KEEP
) {
218 * All sign ops are done by manipulating bit 31, the IEEE float sign bit.
220 const int bit31mask_reg
= get_itemp(gen
);
224 /* re-use 'reg' for the result */
228 /* alloc a new reg for the result */
229 result_reg
= get_itemp(gen
);
232 /* mask with bit 31 set, the rest cleared */
233 spe_load_int(gen
->f
, bit31mask_reg
, (1 << 31));
235 if (sign_op
== TGSI_UTIL_SIGN_CLEAR
) {
236 spe_andc(gen
->f
, result_reg
, reg
, bit31mask_reg
);
238 else if (sign_op
== TGSI_UTIL_SIGN_SET
) {
239 spe_and(gen
->f
, result_reg
, reg
, bit31mask_reg
);
242 assert(sign_op
== TGSI_UTIL_SIGN_TOGGLE
);
243 spe_xor(gen
->f
, result_reg
, reg
, bit31mask_reg
);
254 * Return the index of an SPE register to use for the given TGSI register.
255 * If the TGSI register is TGSI_FILE_TEMPORARAY, the index of the
256 * corresponding SPE register is returned. If the TGSI register is
257 * TGSI_FILE_OUTPUT we allocate an intermediate temporary register.
258 * See store_dest_reg() below...
261 get_dst_reg(struct codegen
*gen
,
263 const struct tgsi_full_dst_register
*dest
)
267 switch (dest
->DstRegister
.File
) {
268 case TGSI_FILE_TEMPORARY
:
269 if (gen
->if_nesting
> 0)
270 reg
= get_itemp(gen
);
272 reg
= gen
->temp_regs
[dest
->DstRegister
.Index
][channel
];
274 case TGSI_FILE_OUTPUT
:
275 reg
= get_itemp(gen
);
286 * When a TGSI instruction is writing to an output register, this
287 * function emits the SPE store instruction to store the value_reg.
288 * \param value_reg the SPE register containing the value to store.
289 * This would have been returned by get_dst_reg().
292 store_dest_reg(struct codegen
*gen
,
293 int value_reg
, int channel
,
294 const struct tgsi_full_dst_register
*dest
)
296 switch (dest
->DstRegister
.File
) {
297 case TGSI_FILE_TEMPORARY
:
298 if (gen
->if_nesting
> 0) {
299 int d_reg
= gen
->temp_regs
[dest
->DstRegister
.Index
][channel
];
300 int exec_reg
= get_exec_mask_reg(gen
);
301 /* Mix d with new value according to exec mask:
302 * d[i] = mask_reg[i] ? value_reg : d_reg
304 spe_selb(gen
->f
, d_reg
, d_reg
, value_reg
, exec_reg
);
307 /* we're not inside a condition or loop: do nothing special */
310 case TGSI_FILE_OUTPUT
:
312 /* offset is measured in quadwords, not bytes */
313 int offset
= dest
->DstRegister
.Index
* 4 + channel
;
314 if (gen
->if_nesting
> 0) {
315 int exec_reg
= get_exec_mask_reg(gen
);
316 int curval_reg
= get_itemp(gen
);
317 /* First read the current value from memory:
318 * Load: curval = memory[(machine_reg) + offset]
320 spe_lqd(gen
->f
, curval_reg
, gen
->outputs_reg
, offset
);
321 /* Mix curval with newvalue according to exec mask:
322 * d[i] = mask_reg[i] ? value_reg : d_reg
324 spe_selb(gen
->f
, curval_reg
, curval_reg
, value_reg
, exec_reg
);
325 /* Store: memory[(machine_reg) + offset] = curval */
326 spe_stqd(gen
->f
, curval_reg
, gen
->outputs_reg
, offset
);
329 /* Store: memory[(machine_reg) + offset] = reg */
330 spe_stqd(gen
->f
, value_reg
, gen
->outputs_reg
, offset
);
341 emit_MOV(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
344 spe_comment(gen
->f
, -4, "MOV:");
345 for (ch
= 0; ch
< 4; ch
++) {
346 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
347 int src_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
348 int dst_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
349 /* XXX we don't always need to actually emit a mov instruction here */
350 spe_move(gen
->f
, dst_reg
, src_reg
);
351 store_dest_reg(gen
, dst_reg
, ch
, &inst
->FullDstRegisters
[0]);
361 * Emit addition instructions. Recall that a single TGSI_OPCODE_ADD
362 * becomes (up to) four SPU "fa" instructions because we're doing SOA
366 emit_ADD(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
369 spe_comment(gen
->f
, -4, "ADD:");
370 /* Loop over Red/Green/Blue/Alpha channels */
371 for (ch
= 0; ch
< 4; ch
++) {
372 /* If the dest R, G, B or A writemask is enabled... */
373 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
374 /* get indexes of the two src, one dest SPE registers */
375 int s1_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
376 int s2_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[1]);
377 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
379 /* Emit actual SPE instruction: d = s1 + s2 */
380 spe_fa(gen
->f
, d_reg
, s1_reg
, s2_reg
);
382 /* Store the result (a no-op for TGSI_FILE_TEMPORARY dests) */
383 store_dest_reg(gen
, d_reg
, ch
, &inst
->FullDstRegisters
[0]);
384 /* Free any intermediate temps we allocated */
392 * Emit subtract. See emit_ADD for comments.
395 emit_SUB(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
398 spe_comment(gen
->f
, -4, "SUB:");
399 /* Loop over Red/Green/Blue/Alpha channels */
400 for (ch
= 0; ch
< 4; ch
++) {
401 /* If the dest R, G, B or A writemask is enabled... */
402 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
403 /* get indexes of the two src, one dest SPE registers */
404 int s1_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
405 int s2_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[1]);
406 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
408 /* Emit actual SPE instruction: d = s1 - s2 */
409 spe_fs(gen
->f
, d_reg
, s1_reg
, s2_reg
);
411 /* Store the result (a no-op for TGSI_FILE_TEMPORARY dests) */
412 store_dest_reg(gen
, d_reg
, ch
, &inst
->FullDstRegisters
[0]);
413 /* Free any intermediate temps we allocated */
421 * Emit multiply add. See emit_ADD for comments.
424 emit_MAD(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
427 spe_comment(gen
->f
, -4, "MAD:");
428 for (ch
= 0; ch
< 4; ch
++) {
429 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
430 int s1_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
431 int s2_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[1]);
432 int s3_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[2]);
433 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
434 /* d = s1 * s2 + s3 */
435 spe_fma(gen
->f
, d_reg
, s1_reg
, s2_reg
, s3_reg
);
436 store_dest_reg(gen
, d_reg
, ch
, &inst
->FullDstRegisters
[0]);
445 * Emit multiply. See emit_ADD for comments.
448 emit_MUL(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
451 spe_comment(gen
->f
, -4, "MUL:");
452 for (ch
= 0; ch
< 4; ch
++) {
453 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
454 int s1_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
455 int s2_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[1]);
456 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
458 spe_fm(gen
->f
, d_reg
, s1_reg
, s2_reg
);
459 store_dest_reg(gen
, d_reg
, ch
, &inst
->FullDstRegisters
[0]);
467 * Emit absolute value. See emit_ADD for comments.
470 emit_ABS(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
473 spe_comment(gen
->f
, -4, "ABS:");
474 for (ch
= 0; ch
< 4; ch
++) {
475 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
476 int s1_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
477 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
478 const int bit31mask_reg
= get_itemp(gen
);
480 /* mask with bit 31 set, the rest cleared */
481 spe_load_int(gen
->f
, bit31mask_reg
, (1 << 31));
483 /* d = sign bit cleared in s1 */
484 spe_andc(gen
->f
, d_reg
, s1_reg
, bit31mask_reg
);
486 store_dest_reg(gen
, d_reg
, ch
, &inst
->FullDstRegisters
[0]);
494 * Emit set-if-greater-than.
495 * Note that the SPE fcgt instruction produces 0x0 and 0xffffffff as
496 * the result but OpenGL/TGSI needs 0.0 and 1.0 results.
497 * We can easily convert 0x0/0xffffffff to 0.0/1.0 with a bitwise AND.
500 emit_SGT(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
504 spe_comment(gen
->f
, -4, "SGT:");
506 for (ch
= 0; ch
< 4; ch
++) {
507 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
508 int s1_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
509 int s2_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[1]);
510 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
513 spe_fcgt(gen
->f
, d_reg
, s1_reg
, s2_reg
);
515 /* convert d from 0x0/0xffffffff to 0.0/1.0 */
516 /* d = d & one_reg */
517 spe_and(gen
->f
, d_reg
, d_reg
, get_const_one_reg(gen
));
519 store_dest_reg(gen
, d_reg
, ch
, &inst
->FullDstRegisters
[0]);
528 * Emit set-if_less-then. See emit_SGT for comments.
531 emit_SLT(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
535 spe_comment(gen
->f
, -4, "SLT:");
537 for (ch
= 0; ch
< 4; ch
++) {
538 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
539 int s1_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
540 int s2_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[1]);
541 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
544 spe_fcgt(gen
->f
, d_reg
, s2_reg
, s1_reg
);
546 /* convert d from 0x0/0xffffffff to 0.0/1.0 */
547 /* d = d & one_reg */
548 spe_and(gen
->f
, d_reg
, d_reg
, get_const_one_reg(gen
));
550 store_dest_reg(gen
, d_reg
, ch
, &inst
->FullDstRegisters
[0]);
559 * Emit set-if_equal. See emit_SGT for comments.
562 emit_SEQ(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
566 spe_comment(gen
->f
, -4, "SEQ:");
568 for (ch
= 0; ch
< 4; ch
++) {
569 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
570 int s1_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
571 int s2_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[1]);
572 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
575 spe_fceq(gen
->f
, d_reg
, s1_reg
, s2_reg
);
577 /* convert d from 0x0/0xffffffff to 0.0/1.0 */
578 /* d = d & one_reg */
579 spe_and(gen
->f
, d_reg
, d_reg
, get_const_one_reg(gen
));
581 store_dest_reg(gen
, d_reg
, ch
, &inst
->FullDstRegisters
[0]);
590 * Emit set-if_not_equal. See emit_SGT for comments.
593 emit_SNE(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
597 spe_comment(gen
->f
, -4, "SNE:");
599 for (ch
= 0; ch
< 4; ch
++) {
600 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
601 int s1_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
602 int s2_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[1]);
603 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
606 spe_fceq(gen
->f
, d_reg
, s1_reg
, s2_reg
);
607 spe_nor(gen
->f
, d_reg
, d_reg
, d_reg
);
609 /* convert d from 0x0/0xffffffff to 0.0/1.0 */
610 /* d = d & one_reg */
611 spe_and(gen
->f
, d_reg
, d_reg
, get_const_one_reg(gen
));
613 store_dest_reg(gen
, d_reg
, ch
, &inst
->FullDstRegisters
[0]);
623 emit_IF(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
625 const int channel
= 0;
626 const int exec_reg
= get_exec_mask_reg(gen
);
628 spe_comment(gen
->f
, -4, "IF:");
630 /* update execution mask with the predicate register */
631 int tmp_reg
= get_itemp(gen
);
632 int s1_reg
= get_src_reg(gen
, channel
, &inst
->FullSrcRegisters
[0]);
634 /* tmp = (s1_reg == 0) */
635 spe_ceqi(gen
->f
, tmp_reg
, s1_reg
, 0);
637 spe_complement(gen
->f
, tmp_reg
);
638 /* exec_mask = exec_mask & tmp */
639 spe_and(gen
->f
, exec_reg
, exec_reg
, tmp_reg
);
650 emit_ELSE(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
652 const int exec_reg
= get_exec_mask_reg(gen
);
654 spe_comment(gen
->f
, -4, "ELSE:");
656 /* exec_mask = !exec_mask */
657 spe_complement(gen
->f
, exec_reg
);
664 emit_ENDIF(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
)
666 const int exec_reg
= get_exec_mask_reg(gen
);
668 spe_comment(gen
->f
, -4, "ENDIF:");
670 /* XXX todo: pop execution mask */
672 spe_load_int(gen
->f
, exec_reg
, ~0x0);
680 emit_DDX_DDY(struct codegen
*gen
, const struct tgsi_full_instruction
*inst
,
685 spe_comment(gen
->f
, -4, ddx
? "DDX:" : "DDY:");
687 for (ch
= 0; ch
< 4; ch
++) {
688 if (inst
->FullDstRegisters
[0].DstRegister
.WriteMask
& (1 << ch
)) {
689 int s_reg
= get_src_reg(gen
, ch
, &inst
->FullSrcRegisters
[0]);
690 int d_reg
= get_dst_reg(gen
, ch
, &inst
->FullDstRegisters
[0]);
692 int t1_reg
= get_itemp(gen
);
693 int t2_reg
= get_itemp(gen
);
695 spe_splat_word(gen
->f
, t1_reg
, s_reg
, 0); /* upper-left pixel */
697 spe_splat_word(gen
->f
, t2_reg
, s_reg
, 1); /* upper-right pixel */
700 spe_splat_word(gen
->f
, t2_reg
, s_reg
, 2); /* lower-left pixel */
702 spe_fs(gen
->f
, d_reg
, t2_reg
, t1_reg
);
715 * Emit END instruction.
716 * We just return from the shader function at this point.
718 * Note that there may be more code after this that would be
719 * called by TGSI_OPCODE_CALL.
722 emit_END(struct codegen
*gen
)
724 spe_comment(gen
->f
, -4, "END:");
725 /* return from function call */
726 spe_bi(gen
->f
, SPE_REG_RA
, 0, 0);
732 * Emit code for the given instruction. Just a big switch stmt.
735 emit_instruction(struct codegen
*gen
,
736 const struct tgsi_full_instruction
*inst
)
738 switch (inst
->Instruction
.Opcode
) {
739 case TGSI_OPCODE_MOV
:
740 return emit_MOV(gen
, inst
);
741 case TGSI_OPCODE_MUL
:
742 return emit_MUL(gen
, inst
);
743 case TGSI_OPCODE_ADD
:
744 return emit_ADD(gen
, inst
);
745 case TGSI_OPCODE_SUB
:
746 return emit_SUB(gen
, inst
);
747 case TGSI_OPCODE_MAD
:
748 return emit_MAD(gen
, inst
);
749 case TGSI_OPCODE_ABS
:
750 return emit_ABS(gen
, inst
);
751 case TGSI_OPCODE_SGT
:
752 return emit_SGT(gen
, inst
);
753 case TGSI_OPCODE_SLT
:
754 return emit_SLT(gen
, inst
);
755 case TGSI_OPCODE_SEQ
:
756 return emit_SEQ(gen
, inst
);
757 case TGSI_OPCODE_SNE
:
758 return emit_SNE(gen
, inst
);
759 case TGSI_OPCODE_END
:
760 return emit_END(gen
);
763 return emit_IF(gen
, inst
);
764 case TGSI_OPCODE_ELSE
:
765 return emit_ELSE(gen
, inst
);
766 case TGSI_OPCODE_ENDIF
:
767 return emit_ENDIF(gen
, inst
);
769 case TGSI_OPCODE_DDX
:
770 return emit_DDX_DDY(gen
, inst
, true);
771 case TGSI_OPCODE_DDY
:
772 return emit_DDX_DDY(gen
, inst
, false);
774 /* XXX lots more cases to do... */
786 * Emit code for a TGSI immediate value (vector of four floats).
787 * This involves register allocation and initialization.
788 * XXX the initialization should be done by a "prepare" stage, not
789 * per quad execution!
792 emit_immediate(struct codegen
*gen
, const struct tgsi_full_immediate
*immed
)
796 assert(gen
->num_imm
< MAX_TEMPS
);
798 spe_comment(gen
->f
, -4, "IMMEDIATE:");
800 for (ch
= 0; ch
< 4; ch
++) {
801 float val
= immed
->u
.ImmediateFloat32
[ch
].Float
;
802 int reg
= spe_allocate_available_register(gen
->f
);
807 /* update immediate map */
808 gen
->imm_regs
[gen
->num_imm
][ch
] = reg
;
810 /* emit initializer instruction */
811 spe_load_float(gen
->f
, reg
, val
);
822 * Emit "code" for a TGSI declaration.
823 * We only care about TGSI TEMPORARY register declarations at this time.
824 * For each TGSI TEMPORARY we allocate four SPE registers.
827 emit_declaration(struct codegen
*gen
, const struct tgsi_full_declaration
*decl
)
831 switch (decl
->Declaration
.File
) {
832 case TGSI_FILE_TEMPORARY
:
834 printf("Declare temp reg %d .. %d\n",
835 decl
->DeclarationRange
.First
,
836 decl
->DeclarationRange
.Last
);
838 for (i
= decl
->DeclarationRange
.First
;
839 i
<= decl
->DeclarationRange
.Last
;
841 assert(i
< MAX_TEMPS
);
842 for (ch
= 0; ch
< 4; ch
++) {
843 gen
->temp_regs
[i
][ch
] = spe_allocate_available_register(gen
->f
);
844 if (gen
->temp_regs
[i
][ch
] < 0)
845 return false; /* out of regs */
848 /* XXX if we run out of SPE registers, we need to spill
849 * to SPU memory. someday...
853 printf(" SPE regs: %d %d %d %d\n",
854 gen
->temp_regs
[i
][0],
855 gen
->temp_regs
[i
][1],
856 gen
->temp_regs
[i
][2],
857 gen
->temp_regs
[i
][3]);
870 * Translate TGSI shader code to SPE instructions. This is done when
871 * the state tracker gives us a new shader (via pipe->create_fs_state()).
873 * \param cell the rendering context (in)
874 * \param tokens the TGSI shader (in)
875 * \param f the generated function (out)
878 cell_gen_fragment_program(struct cell_context
*cell
,
879 const struct tgsi_token
*tokens
,
880 struct spe_function
*f
)
882 struct tgsi_parse_context parse
;
885 memset(&gen
, 0, sizeof(gen
));
888 /* For SPE function calls: reg $3 = first param, $4 = second param, etc. */
889 gen
.inputs_reg
= 3; /* pointer to inputs array */
890 gen
.outputs_reg
= 4; /* pointer to outputs array */
891 gen
.constants_reg
= 5; /* pointer to constants array */
893 spe_init_func(f
, SPU_MAX_FRAGMENT_PROGRAM_INSTS
* SPE_INST_SIZE
);
894 spe_allocate_register(f
, gen
.inputs_reg
);
895 spe_allocate_register(f
, gen
.outputs_reg
);
896 spe_allocate_register(f
, gen
.constants_reg
);
899 spe_print_code(f
, true);
901 printf("Begin %s\n", __FUNCTION__
);
902 tgsi_dump(tokens
, 0);
905 tgsi_parse_init(&parse
, tokens
);
907 while (!tgsi_parse_end_of_tokens(&parse
) && !gen
.error
) {
908 tgsi_parse_token(&parse
);
910 switch (parse
.FullToken
.Token
.Type
) {
911 case TGSI_TOKEN_TYPE_IMMEDIATE
:
912 if (!emit_immediate(&gen
, &parse
.FullToken
.FullImmediate
))
916 case TGSI_TOKEN_TYPE_DECLARATION
:
917 if (!emit_declaration(&gen
, &parse
.FullToken
.FullDeclaration
))
921 case TGSI_TOKEN_TYPE_INSTRUCTION
:
922 if (!emit_instruction(&gen
, &parse
.FullToken
.FullInstruction
))
933 /* terminate the SPE code */
934 return emit_END(&gen
);
938 printf("cell_gen_fragment_program nr instructions: %d\n", f
->num_inst
);
939 printf("End %s\n", __FUNCTION__
);
942 tgsi_parse_free( &parse
);