1 #include "pipe/p_context.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_state.h"
4 #include "util/u_inlines.h"
6 #include "pipe/p_shader_tokens.h"
7 #include "tgsi/tgsi_parse.h"
8 #include "tgsi/tgsi_util.h"
10 #include "nvfx_context.h"
20 #define MASK_ALL (MASK_X|MASK_Y|MASK_Z|MASK_W)
21 #define DEF_SCALE NVFX_FP_OP_DST_SCALE_1X
22 #define DEF_CTEST NVFX_FP_OP_COND_TR
23 #include "nvfx_shader.h"
25 #define swz(s,x,y,z,w) nvfx_sr_swz((s), SWZ_##x, SWZ_##y, SWZ_##z, SWZ_##w)
26 #define neg(s) nvfx_sr_neg((s))
27 #define abs(s) nvfx_sr_abs((s))
28 #define scale(s,v) nvfx_sr_scale((s), NVFX_FP_OP_DST_SCALE_##v)
30 #define MAX_CONSTS 128
33 struct nvfx_fragment_program
*fp
;
35 uint attrib_map
[PIPE_MAX_SHADER_INPUTS
];
38 unsigned r_temps_discard
;
39 struct nvfx_sreg r_result
[PIPE_MAX_SHADER_OUTPUTS
];
40 struct nvfx_sreg
*r_temp
;
53 struct nvfx_sreg imm
[MAX_IMM
];
57 static INLINE
struct nvfx_sreg
58 temp(struct nvfx_fpc
*fpc
)
60 int idx
= ffs(~fpc
->r_temps
) - 1;
63 NOUVEAU_ERR("out of temps!!\n");
65 return nvfx_sr(NVFXSR_TEMP
, 0);
68 fpc
->r_temps
|= (1 << idx
);
69 fpc
->r_temps_discard
|= (1 << idx
);
70 return nvfx_sr(NVFXSR_TEMP
, idx
);
74 release_temps(struct nvfx_fpc
*fpc
)
76 fpc
->r_temps
&= ~fpc
->r_temps_discard
;
77 fpc
->r_temps_discard
= 0;
80 static INLINE
struct nvfx_sreg
81 constant(struct nvfx_fpc
*fpc
, int pipe
, float vals
[4])
85 if (fpc
->nr_consts
== MAX_CONSTS
)
87 idx
= fpc
->nr_consts
++;
89 fpc
->consts
[idx
].pipe
= pipe
;
91 memcpy(fpc
->consts
[idx
].vals
, vals
, 4 * sizeof(float));
92 return nvfx_sr(NVFXSR_CONST
, idx
);
95 #define arith(cc,s,o,d,m,s0,s1,s2) \
96 nvfx_fp_arith((cc), (s), NVFX_FP_OP_OPCODE_##o, \
97 (d), (m), (s0), (s1), (s2))
98 #define tex(cc,s,o,u,d,m,s0,s1,s2) \
99 nvfx_fp_tex((cc), (s), NVFX_FP_OP_OPCODE_##o, (u), \
100 (d), (m), (s0), none, none)
103 grow_insns(struct nvfx_fpc
*fpc
, int size
)
105 struct nvfx_fragment_program
*fp
= fpc
->fp
;
107 fp
->insn_len
+= size
;
108 fp
->insn
= realloc(fp
->insn
, sizeof(uint32_t) * fp
->insn_len
);
112 emit_src(struct nvfx_fpc
*fpc
, int pos
, struct nvfx_sreg src
)
114 struct nvfx_fragment_program
*fp
= fpc
->fp
;
115 uint32_t *hw
= &fp
->insn
[fpc
->inst_offset
];
120 sr
|= (NVFX_FP_REG_TYPE_INPUT
<< NVFX_FP_REG_TYPE_SHIFT
);
121 hw
[0] |= (src
.index
<< NVFX_FP_OP_INPUT_SRC_SHIFT
);
124 sr
|= NVFX_FP_REG_SRC_HALF
;
127 sr
|= (NVFX_FP_REG_TYPE_TEMP
<< NVFX_FP_REG_TYPE_SHIFT
);
128 sr
|= (src
.index
<< NVFX_FP_REG_SRC_SHIFT
);
131 if (!fpc
->have_const
) {
136 hw
= &fp
->insn
[fpc
->inst_offset
];
137 if (fpc
->consts
[src
.index
].pipe
>= 0) {
138 struct nvfx_fragment_program_data
*fpd
;
140 fp
->consts
= realloc(fp
->consts
, ++fp
->nr_consts
*
142 fpd
= &fp
->consts
[fp
->nr_consts
- 1];
143 fpd
->offset
= fpc
->inst_offset
+ 4;
144 fpd
->index
= fpc
->consts
[src
.index
].pipe
;
145 memset(&fp
->insn
[fpd
->offset
], 0, sizeof(uint32_t) * 4);
147 memcpy(&fp
->insn
[fpc
->inst_offset
+ 4],
148 fpc
->consts
[src
.index
].vals
,
149 sizeof(uint32_t) * 4);
152 sr
|= (NVFX_FP_REG_TYPE_CONST
<< NVFX_FP_REG_TYPE_SHIFT
);
155 sr
|= (NVFX_FP_REG_TYPE_INPUT
<< NVFX_FP_REG_TYPE_SHIFT
);
162 sr
|= NVFX_FP_REG_NEGATE
;
165 hw
[1] |= (1 << (29 + pos
));
167 sr
|= ((src
.swz
[0] << NVFX_FP_REG_SWZ_X_SHIFT
) |
168 (src
.swz
[1] << NVFX_FP_REG_SWZ_Y_SHIFT
) |
169 (src
.swz
[2] << NVFX_FP_REG_SWZ_Z_SHIFT
) |
170 (src
.swz
[3] << NVFX_FP_REG_SWZ_W_SHIFT
));
176 emit_dst(struct nvfx_fpc
*fpc
, struct nvfx_sreg dst
)
178 struct nvfx_fragment_program
*fp
= fpc
->fp
;
179 uint32_t *hw
= &fp
->insn
[fpc
->inst_offset
];
183 if (fpc
->num_regs
< (dst
.index
+ 1))
184 fpc
->num_regs
= dst
.index
+ 1;
187 if (dst
.index
== 1) {
188 fp
->fp_control
|= 0xe;
190 hw
[0] |= NVFX_FP_OP_OUT_REG_HALF
;
200 hw
[0] |= (dst
.index
<< NVFX_FP_OP_OUT_REG_SHIFT
);
204 nvfx_fp_arith(struct nvfx_fpc
*fpc
, int sat
, int op
,
205 struct nvfx_sreg dst
, int mask
,
206 struct nvfx_sreg s0
, struct nvfx_sreg s1
, struct nvfx_sreg s2
)
208 struct nvfx_fragment_program
*fp
= fpc
->fp
;
211 fpc
->inst_offset
= fp
->insn_len
;
214 hw
= &fp
->insn
[fpc
->inst_offset
];
215 memset(hw
, 0, sizeof(uint32_t) * 4);
217 if (op
== NVFX_FP_OP_OPCODE_KIL
)
218 fp
->fp_control
|= NV34TCL_FP_CONTROL_USES_KIL
;
219 hw
[0] |= (op
<< NVFX_FP_OP_OPCODE_SHIFT
);
220 hw
[0] |= (mask
<< NVFX_FP_OP_OUTMASK_SHIFT
);
221 hw
[2] |= (dst
.dst_scale
<< NVFX_FP_OP_DST_SCALE_SHIFT
);
224 hw
[0] |= NVFX_FP_OP_OUT_SAT
;
227 hw
[0] |= NVFX_FP_OP_COND_WRITE_ENABLE
;
228 hw
[1] |= (dst
.cc_test
<< NVFX_FP_OP_COND_SHIFT
);
229 hw
[1] |= ((dst
.cc_swz
[0] << NVFX_FP_OP_COND_SWZ_X_SHIFT
) |
230 (dst
.cc_swz
[1] << NVFX_FP_OP_COND_SWZ_Y_SHIFT
) |
231 (dst
.cc_swz
[2] << NVFX_FP_OP_COND_SWZ_Z_SHIFT
) |
232 (dst
.cc_swz
[3] << NVFX_FP_OP_COND_SWZ_W_SHIFT
));
235 emit_src(fpc
, 0, s0
);
236 emit_src(fpc
, 1, s1
);
237 emit_src(fpc
, 2, s2
);
241 nvfx_fp_tex(struct nvfx_fpc
*fpc
, int sat
, int op
, int unit
,
242 struct nvfx_sreg dst
, int mask
,
243 struct nvfx_sreg s0
, struct nvfx_sreg s1
, struct nvfx_sreg s2
)
245 struct nvfx_fragment_program
*fp
= fpc
->fp
;
247 nvfx_fp_arith(fpc
, sat
, op
, dst
, mask
, s0
, s1
, s2
);
249 fp
->insn
[fpc
->inst_offset
] |= (unit
<< NVFX_FP_OP_TEX_UNIT_SHIFT
);
250 fp
->samplers
|= (1 << unit
);
253 static INLINE
struct nvfx_sreg
254 tgsi_src(struct nvfx_fpc
*fpc
, const struct tgsi_full_src_register
*fsrc
)
256 struct nvfx_sreg src
;
258 switch (fsrc
->Register
.File
) {
259 case TGSI_FILE_INPUT
:
260 src
= nvfx_sr(NVFXSR_INPUT
,
261 fpc
->attrib_map
[fsrc
->Register
.Index
]);
263 case TGSI_FILE_CONSTANT
:
264 src
= constant(fpc
, fsrc
->Register
.Index
, NULL
);
266 case TGSI_FILE_IMMEDIATE
:
267 assert(fsrc
->Register
.Index
< fpc
->nr_imm
);
268 src
= fpc
->imm
[fsrc
->Register
.Index
];
270 case TGSI_FILE_TEMPORARY
:
271 src
= fpc
->r_temp
[fsrc
->Register
.Index
];
273 /* NV40 fragprog result regs are just temps, so this is simple */
274 case TGSI_FILE_OUTPUT
:
275 src
= fpc
->r_result
[fsrc
->Register
.Index
];
278 NOUVEAU_ERR("bad src file\n");
282 src
.abs
= fsrc
->Register
.Absolute
;
283 src
.negate
= fsrc
->Register
.Negate
;
284 src
.swz
[0] = fsrc
->Register
.SwizzleX
;
285 src
.swz
[1] = fsrc
->Register
.SwizzleY
;
286 src
.swz
[2] = fsrc
->Register
.SwizzleZ
;
287 src
.swz
[3] = fsrc
->Register
.SwizzleW
;
291 static INLINE
struct nvfx_sreg
292 tgsi_dst(struct nvfx_fpc
*fpc
, const struct tgsi_full_dst_register
*fdst
) {
293 switch (fdst
->Register
.File
) {
294 case TGSI_FILE_OUTPUT
:
295 return fpc
->r_result
[fdst
->Register
.Index
];
296 case TGSI_FILE_TEMPORARY
:
297 return fpc
->r_temp
[fdst
->Register
.Index
];
299 return nvfx_sr(NVFXSR_NONE
, 0);
301 NOUVEAU_ERR("bad dst file %d\n", fdst
->Register
.File
);
302 return nvfx_sr(NVFXSR_NONE
, 0);
311 if (tgsi
& TGSI_WRITEMASK_X
) mask
|= MASK_X
;
312 if (tgsi
& TGSI_WRITEMASK_Y
) mask
|= MASK_Y
;
313 if (tgsi
& TGSI_WRITEMASK_Z
) mask
|= MASK_Z
;
314 if (tgsi
& TGSI_WRITEMASK_W
) mask
|= MASK_W
;
319 src_native_swz(struct nvfx_fpc
*fpc
, const struct tgsi_full_src_register
*fsrc
,
320 struct nvfx_sreg
*src
)
322 const struct nvfx_sreg none
= nvfx_sr(NVFXSR_NONE
, 0);
323 struct nvfx_sreg tgsi
= tgsi_src(fpc
, fsrc
);
327 for (c
= 0; c
< 4; c
++) {
328 switch (tgsi_util_get_full_src_register_swizzle(fsrc
, c
)) {
340 if (mask
== MASK_ALL
)
346 arith(fpc
, 0, MOV
, *src
, mask
, tgsi
, none
, none
);
352 nvfx_fragprog_parse_instruction(struct nvfx_context
* nvfx
, struct nvfx_fpc
*fpc
,
353 const struct tgsi_full_instruction
*finst
)
355 const struct nvfx_sreg none
= nvfx_sr(NVFXSR_NONE
, 0);
356 struct nvfx_sreg src
[3], dst
, tmp
;
358 int ai
= -1, ci
= -1, ii
= -1;
361 if (finst
->Instruction
.Opcode
== TGSI_OPCODE_END
)
364 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
365 const struct tgsi_full_src_register
*fsrc
;
367 fsrc
= &finst
->Src
[i
];
368 if (fsrc
->Register
.File
== TGSI_FILE_TEMPORARY
) {
369 src
[i
] = tgsi_src(fpc
, fsrc
);
373 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
374 const struct tgsi_full_src_register
*fsrc
;
376 fsrc
= &finst
->Src
[i
];
378 switch (fsrc
->Register
.File
) {
379 case TGSI_FILE_INPUT
:
380 case TGSI_FILE_CONSTANT
:
381 case TGSI_FILE_TEMPORARY
:
382 if (!src_native_swz(fpc
, fsrc
, &src
[i
]))
389 switch (fsrc
->Register
.File
) {
390 case TGSI_FILE_INPUT
:
391 if (ai
== -1 || ai
== fsrc
->Register
.Index
) {
392 ai
= fsrc
->Register
.Index
;
393 src
[i
] = tgsi_src(fpc
, fsrc
);
396 arith(fpc
, 0, MOV
, src
[i
], MASK_ALL
,
397 tgsi_src(fpc
, fsrc
), none
, none
);
400 case TGSI_FILE_CONSTANT
:
401 if ((ci
== -1 && ii
== -1) ||
402 ci
== fsrc
->Register
.Index
) {
403 ci
= fsrc
->Register
.Index
;
404 src
[i
] = tgsi_src(fpc
, fsrc
);
407 arith(fpc
, 0, MOV
, src
[i
], MASK_ALL
,
408 tgsi_src(fpc
, fsrc
), none
, none
);
411 case TGSI_FILE_IMMEDIATE
:
412 if ((ci
== -1 && ii
== -1) ||
413 ii
== fsrc
->Register
.Index
) {
414 ii
= fsrc
->Register
.Index
;
415 src
[i
] = tgsi_src(fpc
, fsrc
);
418 arith(fpc
, 0, MOV
, src
[i
], MASK_ALL
,
419 tgsi_src(fpc
, fsrc
), none
, none
);
422 case TGSI_FILE_TEMPORARY
:
425 case TGSI_FILE_SAMPLER
:
426 unit
= fsrc
->Register
.Index
;
428 case TGSI_FILE_OUTPUT
:
431 NOUVEAU_ERR("bad src file\n");
436 dst
= tgsi_dst(fpc
, &finst
->Dst
[0]);
437 mask
= tgsi_mask(finst
->Dst
[0].Register
.WriteMask
);
438 sat
= (finst
->Instruction
.Saturate
== TGSI_SAT_ZERO_ONE
);
440 switch (finst
->Instruction
.Opcode
) {
441 case TGSI_OPCODE_ABS
:
442 arith(fpc
, sat
, MOV
, dst
, mask
, abs(src
[0]), none
, none
);
444 case TGSI_OPCODE_ADD
:
445 arith(fpc
, sat
, ADD
, dst
, mask
, src
[0], src
[1], none
);
447 case TGSI_OPCODE_CMP
:
448 tmp
= nvfx_sr(NVFXSR_NONE
, 0);
450 arith(fpc
, 0, MOV
, tmp
, 0xf, src
[0], none
, none
);
451 dst
.cc_test
= NVFX_VP_INST_COND_GE
;
452 arith(fpc
, sat
, MOV
, dst
, mask
, src
[2], none
, none
);
453 dst
.cc_test
= NVFX_VP_INST_COND_LT
;
454 arith(fpc
, sat
, MOV
, dst
, mask
, src
[1], none
, none
);
456 case TGSI_OPCODE_COS
:
457 arith(fpc
, sat
, COS
, dst
, mask
, src
[0], none
, none
);
459 case TGSI_OPCODE_DDX
:
460 if (mask
& (MASK_Z
| MASK_W
)) {
462 arith(fpc
, sat
, DDX
, tmp
, MASK_X
| MASK_Y
,
463 swz(src
[0], Z
, W
, Z
, W
), none
, none
);
464 arith(fpc
, 0, MOV
, tmp
, MASK_Z
| MASK_W
,
465 swz(tmp
, X
, Y
, X
, Y
), none
, none
);
466 arith(fpc
, sat
, DDX
, tmp
, MASK_X
| MASK_Y
, src
[0],
468 arith(fpc
, 0, MOV
, dst
, mask
, tmp
, none
, none
);
470 arith(fpc
, sat
, DDX
, dst
, mask
, src
[0], none
, none
);
473 case TGSI_OPCODE_DDY
:
474 if (mask
& (MASK_Z
| MASK_W
)) {
476 arith(fpc
, sat
, DDY
, tmp
, MASK_X
| MASK_Y
,
477 swz(src
[0], Z
, W
, Z
, W
), none
, none
);
478 arith(fpc
, 0, MOV
, tmp
, MASK_Z
| MASK_W
,
479 swz(tmp
, X
, Y
, X
, Y
), none
, none
);
480 arith(fpc
, sat
, DDY
, tmp
, MASK_X
| MASK_Y
, src
[0],
482 arith(fpc
, 0, MOV
, dst
, mask
, tmp
, none
, none
);
484 arith(fpc
, sat
, DDY
, dst
, mask
, src
[0], none
, none
);
487 case TGSI_OPCODE_DP3
:
488 arith(fpc
, sat
, DP3
, dst
, mask
, src
[0], src
[1], none
);
490 case TGSI_OPCODE_DP4
:
491 arith(fpc
, sat
, DP4
, dst
, mask
, src
[0], src
[1], none
);
493 case TGSI_OPCODE_DPH
:
495 arith(fpc
, 0, DP3
, tmp
, MASK_X
, src
[0], src
[1], none
);
496 arith(fpc
, sat
, ADD
, dst
, mask
, swz(tmp
, X
, X
, X
, X
),
497 swz(src
[1], W
, W
, W
, W
), none
);
499 case TGSI_OPCODE_DST
:
500 arith(fpc
, sat
, DST
, dst
, mask
, src
[0], src
[1], none
);
502 case TGSI_OPCODE_EX2
:
503 arith(fpc
, sat
, EX2
, dst
, mask
, src
[0], none
, none
);
505 case TGSI_OPCODE_FLR
:
506 arith(fpc
, sat
, FLR
, dst
, mask
, src
[0], none
, none
);
508 case TGSI_OPCODE_FRC
:
509 arith(fpc
, sat
, FRC
, dst
, mask
, src
[0], none
, none
);
511 case TGSI_OPCODE_KILP
:
512 arith(fpc
, 0, KIL
, none
, 0, none
, none
, none
);
514 case TGSI_OPCODE_KIL
:
515 dst
= nvfx_sr(NVFXSR_NONE
, 0);
517 arith(fpc
, 0, MOV
, dst
, MASK_ALL
, src
[0], none
, none
);
518 dst
.cc_update
= 0; dst
.cc_test
= NVFX_FP_OP_COND_LT
;
519 arith(fpc
, 0, KIL
, dst
, 0, none
, none
, none
);
521 case TGSI_OPCODE_LG2
:
522 arith(fpc
, sat
, LG2
, dst
, mask
, src
[0], none
, none
);
524 // case TGSI_OPCODE_LIT:
525 case TGSI_OPCODE_LRP
:
527 arith(fpc
, sat
, LRP_NV30
, dst
, mask
, src
[0], src
[1], src
[2]);
530 arith(fpc
, 0, MAD
, tmp
, mask
, neg(src
[0]), src
[2], src
[2]);
531 arith(fpc
, sat
, MAD
, dst
, mask
, src
[0], src
[1], tmp
);
534 case TGSI_OPCODE_MAD
:
535 arith(fpc
, sat
, MAD
, dst
, mask
, src
[0], src
[1], src
[2]);
537 case TGSI_OPCODE_MAX
:
538 arith(fpc
, sat
, MAX
, dst
, mask
, src
[0], src
[1], none
);
540 case TGSI_OPCODE_MIN
:
541 arith(fpc
, sat
, MIN
, dst
, mask
, src
[0], src
[1], none
);
543 case TGSI_OPCODE_MOV
:
544 arith(fpc
, sat
, MOV
, dst
, mask
, src
[0], none
, none
);
546 case TGSI_OPCODE_MUL
:
547 arith(fpc
, sat
, MUL
, dst
, mask
, src
[0], src
[1], none
);
549 case TGSI_OPCODE_POW
:
551 arith(fpc
, sat
, POW_NV30
, dst
, mask
, src
[0], src
[1], none
);
554 arith(fpc
, 0, LG2
, tmp
, MASK_X
,
555 swz(src
[0], X
, X
, X
, X
), none
, none
);
556 arith(fpc
, 0, MUL
, tmp
, MASK_X
, swz(tmp
, X
, X
, X
, X
),
557 swz(src
[1], X
, X
, X
, X
), none
);
558 arith(fpc
, sat
, EX2
, dst
, mask
,
559 swz(tmp
, X
, X
, X
, X
), none
, none
);
562 case TGSI_OPCODE_RCP
:
563 arith(fpc
, sat
, RCP
, dst
, mask
, src
[0], none
, none
);
565 case TGSI_OPCODE_RET
:
568 case TGSI_OPCODE_RFL
:
570 arith(fpc
, 0, RFL_NV30
, dst
, mask
, src
[0], src
[1], none
);
573 arith(fpc
, 0, DP3
, tmp
, MASK_X
, src
[0], src
[0], none
);
574 arith(fpc
, 0, DP3
, tmp
, MASK_Y
, src
[0], src
[1], none
);
575 arith(fpc
, 0, DIV
, scale(tmp
, 2X
), MASK_Z
,
576 swz(tmp
, Y
, Y
, Y
, Y
), swz(tmp
, X
, X
, X
, X
), none
);
577 arith(fpc
, sat
, MAD
, dst
, mask
,
578 swz(tmp
, Z
, Z
, Z
, Z
), src
[0], neg(src
[1]));
581 case TGSI_OPCODE_RSQ
:
583 arith(fpc
, sat
, RSQ_NV30
, dst
, mask
, abs(swz(src
[0], X
, X
, X
, X
)), none
, none
);
586 arith(fpc
, 0, LG2
, scale(tmp
, INV_2X
), MASK_X
,
587 abs(swz(src
[0], X
, X
, X
, X
)), none
, none
);
588 arith(fpc
, sat
, EX2
, dst
, mask
,
589 neg(swz(tmp
, X
, X
, X
, X
)), none
, none
);
592 case TGSI_OPCODE_SCS
:
593 /* avoid overwriting the source */
594 if(src
[0].swz
[SWZ_X
] != SWZ_X
)
597 arith(fpc
, sat
, COS
, dst
, MASK_X
,
598 swz(src
[0], X
, X
, X
, X
), none
, none
);
601 arith(fpc
, sat
, SIN
, dst
, MASK_Y
,
602 swz(src
[0], X
, X
, X
, X
), none
, none
);
608 arith(fpc
, sat
, SIN
, dst
, MASK_Y
,
609 swz(src
[0], X
, X
, X
, X
), none
, none
);
612 arith(fpc
, sat
, COS
, dst
, MASK_X
,
613 swz(src
[0], X
, X
, X
, X
), none
, none
);
617 case TGSI_OPCODE_SEQ
:
618 arith(fpc
, sat
, SEQ
, dst
, mask
, src
[0], src
[1], none
);
620 case TGSI_OPCODE_SFL
:
621 arith(fpc
, sat
, SFL
, dst
, mask
, src
[0], src
[1], none
);
623 case TGSI_OPCODE_SGE
:
624 arith(fpc
, sat
, SGE
, dst
, mask
, src
[0], src
[1], none
);
626 case TGSI_OPCODE_SGT
:
627 arith(fpc
, sat
, SGT
, dst
, mask
, src
[0], src
[1], none
);
629 case TGSI_OPCODE_SIN
:
630 arith(fpc
, sat
, SIN
, dst
, mask
, src
[0], none
, none
);
632 case TGSI_OPCODE_SLE
:
633 arith(fpc
, sat
, SLE
, dst
, mask
, src
[0], src
[1], none
);
635 case TGSI_OPCODE_SLT
:
636 arith(fpc
, sat
, SLT
, dst
, mask
, src
[0], src
[1], none
);
638 case TGSI_OPCODE_SNE
:
639 arith(fpc
, sat
, SNE
, dst
, mask
, src
[0], src
[1], none
);
641 case TGSI_OPCODE_STR
:
642 arith(fpc
, sat
, STR
, dst
, mask
, src
[0], src
[1], none
);
644 case TGSI_OPCODE_SUB
:
645 arith(fpc
, sat
, ADD
, dst
, mask
, src
[0], neg(src
[1]), none
);
647 case TGSI_OPCODE_TEX
:
648 tex(fpc
, sat
, TEX
, unit
, dst
, mask
, src
[0], none
, none
);
650 case TGSI_OPCODE_TXB
:
651 tex(fpc
, sat
, TXB
, unit
, dst
, mask
, src
[0], none
, none
);
653 case TGSI_OPCODE_TXP
:
654 tex(fpc
, sat
, TXP
, unit
, dst
, mask
, src
[0], none
, none
);
656 case TGSI_OPCODE_XPD
:
658 arith(fpc
, 0, MUL
, tmp
, mask
,
659 swz(src
[0], Z
, X
, Y
, Y
), swz(src
[1], Y
, Z
, X
, X
), none
);
660 arith(fpc
, sat
, MAD
, dst
, (mask
& ~MASK_W
),
661 swz(src
[0], Y
, Z
, X
, X
), swz(src
[1], Z
, X
, Y
, Y
),
665 NOUVEAU_ERR("invalid opcode %d\n", finst
->Instruction
.Opcode
);
674 nvfx_fragprog_parse_decl_attrib(struct nvfx_context
* nvfx
, struct nvfx_fpc
*fpc
,
675 const struct tgsi_full_declaration
*fdec
)
679 switch (fdec
->Semantic
.Name
) {
680 case TGSI_SEMANTIC_POSITION
:
681 hw
= NVFX_FP_OP_INPUT_SRC_POSITION
;
683 case TGSI_SEMANTIC_COLOR
:
684 if (fdec
->Semantic
.Index
== 0) {
685 hw
= NVFX_FP_OP_INPUT_SRC_COL0
;
687 if (fdec
->Semantic
.Index
== 1) {
688 hw
= NVFX_FP_OP_INPUT_SRC_COL1
;
690 NOUVEAU_ERR("bad colour semantic index\n");
694 case TGSI_SEMANTIC_FOG
:
695 hw
= NVFX_FP_OP_INPUT_SRC_FOGC
;
697 case TGSI_SEMANTIC_GENERIC
:
698 if (fdec
->Semantic
.Index
<= 7) {
699 hw
= NVFX_FP_OP_INPUT_SRC_TC(fdec
->Semantic
.
702 NOUVEAU_ERR("bad generic semantic index\n");
707 NOUVEAU_ERR("bad input semantic\n");
711 fpc
->attrib_map
[fdec
->Range
.First
] = hw
;
716 nvfx_fragprog_parse_decl_output(struct nvfx_context
* nvfx
, struct nvfx_fpc
*fpc
,
717 const struct tgsi_full_declaration
*fdec
)
719 unsigned idx
= fdec
->Range
.First
;
722 switch (fdec
->Semantic
.Name
) {
723 case TGSI_SEMANTIC_POSITION
:
726 case TGSI_SEMANTIC_COLOR
:
728 switch (fdec
->Semantic
.Index
) {
729 case 0: hw
= 0; break;
730 case 1: hw
= 2; break;
731 case 2: hw
= 3; break;
732 case 3: hw
= 4; break;
734 if(hw
> ((nvfx
->is_nv4x
) ? 4 : 2)) {
735 NOUVEAU_ERR("bad rcol index\n");
740 NOUVEAU_ERR("bad output semantic\n");
744 fpc
->r_result
[idx
] = nvfx_sr(NVFXSR_OUTPUT
, hw
);
745 fpc
->r_temps
|= (1 << hw
);
750 nvfx_fragprog_prepare(struct nvfx_context
* nvfx
, struct nvfx_fpc
*fpc
)
752 struct tgsi_parse_context p
;
753 int high_temp
= -1, i
;
755 tgsi_parse_init(&p
, fpc
->fp
->pipe
.tokens
);
756 while (!tgsi_parse_end_of_tokens(&p
)) {
757 const union tgsi_full_token
*tok
= &p
.FullToken
;
759 tgsi_parse_token(&p
);
760 switch(tok
->Token
.Type
) {
761 case TGSI_TOKEN_TYPE_DECLARATION
:
763 const struct tgsi_full_declaration
*fdec
;
764 fdec
= &p
.FullToken
.FullDeclaration
;
765 switch (fdec
->Declaration
.File
) {
766 case TGSI_FILE_INPUT
:
767 if (!nvfx_fragprog_parse_decl_attrib(nvfx
, fpc
, fdec
))
770 case TGSI_FILE_OUTPUT
:
771 if (!nvfx_fragprog_parse_decl_output(nvfx
, fpc
, fdec
))
774 case TGSI_FILE_TEMPORARY
:
775 if (fdec
->Range
.Last
> high_temp
) {
785 case TGSI_TOKEN_TYPE_IMMEDIATE
:
787 struct tgsi_full_immediate
*imm
;
790 imm
= &p
.FullToken
.FullImmediate
;
791 assert(imm
->Immediate
.DataType
== TGSI_IMM_FLOAT32
);
792 assert(fpc
->nr_imm
< MAX_IMM
);
794 vals
[0] = imm
->u
[0].Float
;
795 vals
[1] = imm
->u
[1].Float
;
796 vals
[2] = imm
->u
[2].Float
;
797 vals
[3] = imm
->u
[3].Float
;
798 fpc
->imm
[fpc
->nr_imm
++] = constant(fpc
, -1, vals
);
808 fpc
->r_temp
= CALLOC(high_temp
, sizeof(struct nvfx_sreg
));
809 for (i
= 0; i
< high_temp
; i
++)
810 fpc
->r_temp
[i
] = temp(fpc
);
811 fpc
->r_temps_discard
= 0;
824 nvfx_fragprog_translate(struct nvfx_context
*nvfx
,
825 struct nvfx_fragment_program
*fp
)
827 struct tgsi_parse_context parse
;
828 struct nvfx_fpc
*fpc
= NULL
;
830 fpc
= CALLOC(1, sizeof(struct nvfx_fpc
));
836 if (!nvfx_fragprog_prepare(nvfx
, fpc
)) {
841 tgsi_parse_init(&parse
, fp
->pipe
.tokens
);
843 while (!tgsi_parse_end_of_tokens(&parse
)) {
844 tgsi_parse_token(&parse
);
846 switch (parse
.FullToken
.Token
.Type
) {
847 case TGSI_TOKEN_TYPE_INSTRUCTION
:
849 const struct tgsi_full_instruction
*finst
;
851 finst
= &parse
.FullToken
.FullInstruction
;
852 if (!nvfx_fragprog_parse_instruction(nvfx
, fpc
, finst
))
862 fp
->fp_control
|= (fpc
->num_regs
-1)/2;
864 fp
->fp_control
|= fpc
->num_regs
<< NV40TCL_FP_CONTROL_TEMP_COUNT_SHIFT
;
866 /* Terminate final instruction */
867 fp
->insn
[fpc
->inst_offset
] |= 0x00000001;
869 /* Append NOP + END instruction, may or may not be necessary. */
870 fpc
->inst_offset
= fp
->insn_len
;
872 fp
->insn
[fpc
->inst_offset
+ 0] = 0x00000001;
873 fp
->insn
[fpc
->inst_offset
+ 1] = 0x00000000;
874 fp
->insn
[fpc
->inst_offset
+ 2] = 0x00000000;
875 fp
->insn
[fpc
->inst_offset
+ 3] = 0x00000000;
877 fp
->translated
= TRUE
;
879 tgsi_parse_free(&parse
);
886 nvfx_fragprog_upload(struct nvfx_context
*nvfx
,
887 struct nvfx_fragment_program
*fp
)
889 struct pipe_screen
*pscreen
= nvfx
->pipe
.screen
;
890 const uint32_t le
= 1;
894 map
= pipe_buffer_map(pscreen
, fp
->buffer
, PIPE_BUFFER_USAGE_CPU_WRITE
);
897 for (i
= 0; i
< fp
->insn_len
; i
++) {
898 fflush(stdout
); fflush(stderr
);
899 NOUVEAU_ERR("%d 0x%08x\n", i
, fp
->insn
[i
]);
900 fflush(stdout
); fflush(stderr
);
904 if ((*(const uint8_t *)&le
)) {
905 for (i
= 0; i
< fp
->insn_len
; i
++) {
906 map
[i
] = fp
->insn
[i
];
909 /* Weird swapping for big-endian chips */
910 for (i
= 0; i
< fp
->insn_len
; i
++) {
911 map
[i
] = ((fp
->insn
[i
] & 0xffff) << 16) |
912 ((fp
->insn
[i
] >> 16) & 0xffff);
916 pipe_buffer_unmap(pscreen
, fp
->buffer
);
920 nvfx_fragprog_validate(struct nvfx_context
*nvfx
)
922 struct nvfx_fragment_program
*fp
= nvfx
->fragprog
;
923 struct pipe_buffer
*constbuf
=
924 nvfx
->constbuf
[PIPE_SHADER_FRAGMENT
];
925 struct pipe_screen
*pscreen
= nvfx
->pipe
.screen
;
926 struct nouveau_stateobj
*so
;
927 boolean new_consts
= FALSE
;
931 goto update_constants
;
933 nvfx
->fallback_swrast
&= ~NVFX_NEW_FRAGPROG
;
934 nvfx_fragprog_translate(nvfx
, fp
);
935 if (!fp
->translated
) {
936 nvfx
->fallback_swrast
|= NVFX_NEW_FRAGPROG
;
940 fp
->buffer
= pscreen
->buffer_create(pscreen
, 0x100, 0, fp
->insn_len
* 4);
941 nvfx_fragprog_upload(nvfx
, fp
);
943 so
= so_new(4, 4, 1);
944 so_method(so
, nvfx
->screen
->eng3d
, NV34TCL_FP_ACTIVE_PROGRAM
, 1);
945 so_reloc (so
, nouveau_bo(fp
->buffer
), 0, NOUVEAU_BO_VRAM
|
946 NOUVEAU_BO_GART
| NOUVEAU_BO_RD
| NOUVEAU_BO_LOW
|
947 NOUVEAU_BO_OR
, NV34TCL_FP_ACTIVE_PROGRAM_DMA0
,
948 NV34TCL_FP_ACTIVE_PROGRAM_DMA1
);
949 so_method(so
, nvfx
->screen
->eng3d
, NV34TCL_FP_CONTROL
, 1);
950 so_data (so
, fp
->fp_control
);
952 so_method(so
, nvfx
->screen
->eng3d
, NV34TCL_FP_REG_CONTROL
, 1);
953 so_data (so
, (1<<16)|0x4);
954 so_method(so
, nvfx
->screen
->eng3d
, NV34TCL_TX_UNITS_ENABLE
, 1);
955 so_data (so
, fp
->samplers
);
965 map
= pipe_buffer_map(pscreen
, constbuf
,
966 PIPE_BUFFER_USAGE_CPU_READ
);
967 for (i
= 0; i
< fp
->nr_consts
; i
++) {
968 struct nvfx_fragment_program_data
*fpd
= &fp
->consts
[i
];
969 uint32_t *p
= &fp
->insn
[fpd
->offset
];
970 uint32_t *cb
= (uint32_t *)&map
[fpd
->index
* 4];
972 if (!memcmp(p
, cb
, 4 * sizeof(float)))
974 memcpy(p
, cb
, 4 * sizeof(float));
977 pipe_buffer_unmap(pscreen
, constbuf
);
980 nvfx_fragprog_upload(nvfx
, fp
);
983 if (new_consts
|| fp
->so
!= nvfx
->state
.hw
[NVFX_STATE_FRAGPROG
]) {
984 so_ref(fp
->so
, &nvfx
->state
.hw
[NVFX_STATE_FRAGPROG
]);
992 nvfx_fragprog_destroy(struct nvfx_context
*nvfx
,
993 struct nvfx_fragment_program
*fp
)
996 pipe_buffer_reference(&fp
->buffer
, NULL
);
999 so_ref(NULL
, &fp
->so
);
1005 struct nvfx_state_entry nvfx_state_fragprog
= {
1006 .validate
= nvfx_fragprog_validate
,
1008 .pipe
= NVFX_NEW_FRAGPROG
,
1009 .hw
= NVFX_STATE_FRAGPROG