2 #include "pipe/p_context.h"
3 #include "pipe/p_defines.h"
4 #include "pipe/p_state.h"
5 #include "util/u_dynarray.h"
6 #include "util/u_inlines.h"
7 #include "util/u_debug.h"
9 #include "pipe/p_shader_tokens.h"
10 #include "tgsi/tgsi_parse.h"
11 #include "tgsi/tgsi_util.h"
12 #include "tgsi/tgsi_dump.h"
13 #include "tgsi/tgsi_ureg.h"
15 #include "nouveau_debug.h"
16 #include "nv_object.xml.h"
17 #include "nv30/nv30-40_3d.xml.h"
18 #include "nv30/nvfx_shader.h"
19 #include "nv30/nv30_state.h"
22 struct nv30_fragprog
*fp
;
25 unsigned long long r_temps
;
26 unsigned long long r_temps_discard
;
27 struct nvfx_reg r_result
[PIPE_MAX_SHADER_OUTPUTS
];
28 struct nvfx_reg r_input
[PIPE_MAX_SHADER_INPUTS
];
29 struct nvfx_reg
*r_temp
;
37 struct util_dynarray imm_data
;
39 struct nvfx_reg
* r_imm
;
42 struct util_dynarray if_stack
;
43 //struct util_dynarray loop_stack;
44 struct util_dynarray label_relocs
;
47 static inline struct nvfx_reg
48 temp(struct nvfx_fpc
*fpc
)
50 int idx
= __builtin_ctzll(~fpc
->r_temps
);
52 if (idx
>= fpc
->max_temps
) {
53 NOUVEAU_ERR("out of temps!!\n");
54 return nvfx_reg(NVFXSR_TEMP
, 0);
57 fpc
->r_temps
|= (1ULL << idx
);
58 fpc
->r_temps_discard
|= (1ULL << idx
);
59 return nvfx_reg(NVFXSR_TEMP
, idx
);
63 release_temps(struct nvfx_fpc
*fpc
)
65 fpc
->r_temps
&= ~fpc
->r_temps_discard
;
66 fpc
->r_temps_discard
= 0ULL;
69 static inline struct nvfx_reg
70 nvfx_fp_imm(struct nvfx_fpc
*fpc
, float a
, float b
, float c
, float d
)
72 float v
[4] = {a
, b
, c
, d
};
73 int idx
= fpc
->imm_data
.size
>> 4;
75 memcpy(util_dynarray_grow(&fpc
->imm_data
, sizeof(float) * 4), v
, 4 * sizeof(float));
76 return nvfx_reg(NVFXSR_IMM
, idx
);
80 grow_insns(struct nvfx_fpc
*fpc
, int size
)
82 struct nv30_fragprog
*fp
= fpc
->fp
;
85 fp
->insn
= realloc(fp
->insn
, sizeof(uint32_t) * fp
->insn_len
);
89 emit_src(struct nvfx_fpc
*fpc
, int pos
, struct nvfx_src src
)
91 struct nv30_fragprog
*fp
= fpc
->fp
;
92 uint32_t *hw
= &fp
->insn
[fpc
->inst_offset
];
95 switch (src
.reg
.type
) {
97 sr
|= (NVFX_FP_REG_TYPE_INPUT
<< NVFX_FP_REG_TYPE_SHIFT
);
98 hw
[0] |= (src
.reg
.index
<< NVFX_FP_OP_INPUT_SRC_SHIFT
);
101 sr
|= NVFX_FP_REG_SRC_HALF
;
104 sr
|= (NVFX_FP_REG_TYPE_TEMP
<< NVFX_FP_REG_TYPE_SHIFT
);
105 sr
|= (src
.reg
.index
<< NVFX_FP_REG_SRC_SHIFT
);
108 if (!fpc
->have_const
) {
110 hw
= &fp
->insn
[fpc
->inst_offset
];
114 memcpy(&fp
->insn
[fpc
->inst_offset
+ 4],
115 (float*)fpc
->imm_data
.data
+ src
.reg
.index
* 4,
116 sizeof(uint32_t) * 4);
118 sr
|= (NVFX_FP_REG_TYPE_CONST
<< NVFX_FP_REG_TYPE_SHIFT
);
121 if (!fpc
->have_const
) {
123 hw
= &fp
->insn
[fpc
->inst_offset
];
128 struct nv30_fragprog_data
*fpd
;
130 fp
->consts
= realloc(fp
->consts
, ++fp
->nr_consts
*
132 fpd
= &fp
->consts
[fp
->nr_consts
- 1];
133 fpd
->offset
= fpc
->inst_offset
+ 4;
134 fpd
->index
= src
.reg
.index
;
135 memset(&fp
->insn
[fpd
->offset
], 0, sizeof(uint32_t) * 4);
138 sr
|= (NVFX_FP_REG_TYPE_CONST
<< NVFX_FP_REG_TYPE_SHIFT
);
141 sr
|= (NVFX_FP_REG_TYPE_INPUT
<< NVFX_FP_REG_TYPE_SHIFT
);
148 sr
|= NVFX_FP_REG_NEGATE
;
151 hw
[1] |= (1 << (29 + pos
));
153 sr
|= ((src
.swz
[0] << NVFX_FP_REG_SWZ_X_SHIFT
) |
154 (src
.swz
[1] << NVFX_FP_REG_SWZ_Y_SHIFT
) |
155 (src
.swz
[2] << NVFX_FP_REG_SWZ_Z_SHIFT
) |
156 (src
.swz
[3] << NVFX_FP_REG_SWZ_W_SHIFT
));
162 emit_dst(struct nvfx_fpc
*fpc
, struct nvfx_reg dst
)
164 struct nv30_fragprog
*fp
= fpc
->fp
;
165 uint32_t *hw
= &fp
->insn
[fpc
->inst_offset
];
170 fp
->fp_control
|= 0x0000000e;
172 hw
[0] |= NVFX_FP_OP_OUT_REG_HALF
;
177 if (fpc
->num_regs
< (dst
.index
+ 1))
178 fpc
->num_regs
= dst
.index
+ 1;
187 hw
[0] |= (dst
.index
<< NVFX_FP_OP_OUT_REG_SHIFT
);
191 nvfx_fp_emit(struct nvfx_fpc
*fpc
, struct nvfx_insn insn
)
193 struct nv30_fragprog
*fp
= fpc
->fp
;
196 fpc
->inst_offset
= fp
->insn_len
;
199 hw
= &fp
->insn
[fpc
->inst_offset
];
200 memset(hw
, 0, sizeof(uint32_t) * 4);
202 if (insn
.op
== NVFX_FP_OP_OPCODE_KIL
)
203 fp
->fp_control
|= NV30_3D_FP_CONTROL_USES_KIL
;
204 hw
[0] |= (insn
.op
<< NVFX_FP_OP_OPCODE_SHIFT
);
205 hw
[0] |= (insn
.mask
<< NVFX_FP_OP_OUTMASK_SHIFT
);
206 hw
[2] |= (insn
.scale
<< NVFX_FP_OP_DST_SCALE_SHIFT
);
209 hw
[0] |= NVFX_FP_OP_OUT_SAT
;
212 hw
[0] |= NVFX_FP_OP_COND_WRITE_ENABLE
;
213 hw
[1] |= (insn
.cc_test
<< NVFX_FP_OP_COND_SHIFT
);
214 hw
[1] |= ((insn
.cc_swz
[0] << NVFX_FP_OP_COND_SWZ_X_SHIFT
) |
215 (insn
.cc_swz
[1] << NVFX_FP_OP_COND_SWZ_Y_SHIFT
) |
216 (insn
.cc_swz
[2] << NVFX_FP_OP_COND_SWZ_Z_SHIFT
) |
217 (insn
.cc_swz
[3] << NVFX_FP_OP_COND_SWZ_W_SHIFT
));
221 hw
[0] |= (insn
.unit
<< NVFX_FP_OP_TEX_UNIT_SHIFT
);
224 emit_dst(fpc
, insn
.dst
);
225 emit_src(fpc
, 0, insn
.src
[0]);
226 emit_src(fpc
, 1, insn
.src
[1]);
227 emit_src(fpc
, 2, insn
.src
[2]);
230 #define arith(s,o,d,m,s0,s1,s2) \
231 nvfx_insn((s), NVFX_FP_OP_OPCODE_##o, -1, \
232 (d), (m), (s0), (s1), (s2))
234 #define tex(s,o,u,d,m,s0,s1,s2) \
235 nvfx_insn((s), NVFX_FP_OP_OPCODE_##o, (u), \
236 (d), (m), (s0), none, none)
238 /* IF src.x != 0, as TGSI specifies */
240 nv40_fp_if(struct nvfx_fpc
*fpc
, struct nvfx_src src
)
242 const struct nvfx_src none
= nvfx_src(nvfx_reg(NVFXSR_NONE
, 0));
243 struct nvfx_insn insn
= arith(0, MOV
, none
.reg
, NVFX_FP_MASK_X
, src
, none
, none
);
246 nvfx_fp_emit(fpc
, insn
);
248 fpc
->inst_offset
= fpc
->fp
->insn_len
;
250 hw
= &fpc
->fp
->insn
[fpc
->inst_offset
];
251 /* I really wonder why fp16 precision is used. Presumably the hardware ignores it? */
252 hw
[0] = (NV40_FP_OP_BRA_OPCODE_IF
<< NVFX_FP_OP_OPCODE_SHIFT
) |
253 NV40_FP_OP_OUT_NONE
|
254 (NVFX_FP_PRECISION_FP16
<< NVFX_FP_OP_PRECISION_SHIFT
);
255 /* Use .xxxx swizzle so that we check only src[0].x*/
256 hw
[1] = (0 << NVFX_FP_OP_COND_SWZ_X_SHIFT
) |
257 (0 << NVFX_FP_OP_COND_SWZ_Y_SHIFT
) |
258 (0 << NVFX_FP_OP_COND_SWZ_Z_SHIFT
) |
259 (0 << NVFX_FP_OP_COND_SWZ_W_SHIFT
) |
260 (NVFX_FP_OP_COND_NE
<< NVFX_FP_OP_COND_SHIFT
);
261 hw
[2] = 0; /* | NV40_FP_OP_OPCODE_IS_BRANCH | else_offset */
262 hw
[3] = 0; /* | endif_offset */
263 util_dynarray_append(&fpc
->if_stack
, unsigned, fpc
->inst_offset
);
266 /* IF src.x != 0, as TGSI specifies */
268 nv40_fp_cal(struct nvfx_fpc
*fpc
, unsigned target
)
270 struct nvfx_relocation reloc
;
272 fpc
->inst_offset
= fpc
->fp
->insn_len
;
274 hw
= &fpc
->fp
->insn
[fpc
->inst_offset
];
275 /* I really wonder why fp16 precision is used. Presumably the hardware ignores it? */
276 hw
[0] = (NV40_FP_OP_BRA_OPCODE_CAL
<< NVFX_FP_OP_OPCODE_SHIFT
);
277 /* Use .xxxx swizzle so that we check only src[0].x*/
278 hw
[1] = (NVFX_SWZ_IDENTITY
<< NVFX_FP_OP_COND_SWZ_ALL_SHIFT
) |
279 (NVFX_FP_OP_COND_TR
<< NVFX_FP_OP_COND_SHIFT
);
280 hw
[2] = NV40_FP_OP_OPCODE_IS_BRANCH
; /* | call_offset */
282 reloc
.target
= target
;
283 reloc
.location
= fpc
->inst_offset
+ 2;
284 util_dynarray_append(&fpc
->label_relocs
, struct nvfx_relocation
, reloc
);
288 nv40_fp_ret(struct nvfx_fpc
*fpc
)
291 fpc
->inst_offset
= fpc
->fp
->insn_len
;
293 hw
= &fpc
->fp
->insn
[fpc
->inst_offset
];
294 /* I really wonder why fp16 precision is used. Presumably the hardware ignores it? */
295 hw
[0] = (NV40_FP_OP_BRA_OPCODE_RET
<< NVFX_FP_OP_OPCODE_SHIFT
);
296 /* Use .xxxx swizzle so that we check only src[0].x*/
297 hw
[1] = (NVFX_SWZ_IDENTITY
<< NVFX_FP_OP_COND_SWZ_ALL_SHIFT
) |
298 (NVFX_FP_OP_COND_TR
<< NVFX_FP_OP_COND_SHIFT
);
299 hw
[2] = NV40_FP_OP_OPCODE_IS_BRANCH
; /* | call_offset */
304 nv40_fp_rep(struct nvfx_fpc
*fpc
, unsigned count
, unsigned target
)
306 struct nvfx_relocation reloc
;
308 fpc
->inst_offset
= fpc
->fp
->insn_len
;
310 hw
= &fpc
->fp
->insn
[fpc
->inst_offset
];
311 /* I really wonder why fp16 precision is used. Presumably the hardware ignores it? */
312 hw
[0] = (NV40_FP_OP_BRA_OPCODE_REP
<< NVFX_FP_OP_OPCODE_SHIFT
) |
313 NV40_FP_OP_OUT_NONE
|
314 (NVFX_FP_PRECISION_FP16
<< NVFX_FP_OP_PRECISION_SHIFT
);
315 /* Use .xxxx swizzle so that we check only src[0].x*/
316 hw
[1] = (NVFX_SWZ_IDENTITY
<< NVFX_FP_OP_COND_SWZ_ALL_SHIFT
) |
317 (NVFX_FP_OP_COND_TR
<< NVFX_FP_OP_COND_SHIFT
);
318 hw
[2] = NV40_FP_OP_OPCODE_IS_BRANCH
|
319 (count
<< NV40_FP_OP_REP_COUNT1_SHIFT
) |
320 (count
<< NV40_FP_OP_REP_COUNT2_SHIFT
) |
321 (count
<< NV40_FP_OP_REP_COUNT3_SHIFT
);
322 hw
[3] = 0; /* | end_offset */
323 reloc
.target
= target
;
324 reloc
.location
= fpc
->inst_offset
+ 3;
325 util_dynarray_append(&fpc
->label_relocs
, struct nvfx_relocation
, reloc
);
326 //util_dynarray_append(&fpc->loop_stack, unsigned, target);
330 /* documentation only */
331 /* warning: this only works forward, and probably only if not inside any IF */
333 nv40_fp_bra(struct nvfx_fpc
*fpc
, unsigned target
)
335 struct nvfx_relocation reloc
;
337 fpc
->inst_offset
= fpc
->fp
->insn_len
;
339 hw
= &fpc
->fp
->insn
[fpc
->inst_offset
];
340 /* I really wonder why fp16 precision is used. Presumably the hardware ignores it? */
341 hw
[0] = (NV40_FP_OP_BRA_OPCODE_IF
<< NVFX_FP_OP_OPCODE_SHIFT
) |
342 NV40_FP_OP_OUT_NONE
|
343 (NVFX_FP_PRECISION_FP16
<< NVFX_FP_OP_PRECISION_SHIFT
);
344 /* Use .xxxx swizzle so that we check only src[0].x*/
345 hw
[1] = (NVFX_SWZ_IDENTITY
<< NVFX_FP_OP_COND_SWZ_X_SHIFT
) |
346 (NVFX_FP_OP_COND_FL
<< NVFX_FP_OP_COND_SHIFT
);
347 hw
[2] = NV40_FP_OP_OPCODE_IS_BRANCH
; /* | else_offset */
348 hw
[3] = 0; /* | endif_offset */
349 reloc
.target
= target
;
350 reloc
.location
= fpc
->inst_offset
+ 2;
351 util_dynarray_append(&fpc
->label_relocs
, struct nvfx_relocation
, reloc
);
352 reloc
.target
= target
;
353 reloc
.location
= fpc
->inst_offset
+ 3;
354 util_dynarray_append(&fpc
->label_relocs
, struct nvfx_relocation
, reloc
);
359 nv40_fp_brk(struct nvfx_fpc
*fpc
)
362 fpc
->inst_offset
= fpc
->fp
->insn_len
;
364 hw
= &fpc
->fp
->insn
[fpc
->inst_offset
];
365 /* I really wonder why fp16 precision is used. Presumably the hardware ignores it? */
366 hw
[0] = (NV40_FP_OP_BRA_OPCODE_BRK
<< NVFX_FP_OP_OPCODE_SHIFT
) |
368 /* Use .xxxx swizzle so that we check only src[0].x*/
369 hw
[1] = (NVFX_SWZ_IDENTITY
<< NVFX_FP_OP_COND_SWZ_X_SHIFT
) |
370 (NVFX_FP_OP_COND_TR
<< NVFX_FP_OP_COND_SHIFT
);
371 hw
[2] = NV40_FP_OP_OPCODE_IS_BRANCH
;
375 static inline struct nvfx_src
376 tgsi_src(struct nvfx_fpc
*fpc
, const struct tgsi_full_src_register
*fsrc
)
380 switch (fsrc
->Register
.File
) {
381 case TGSI_FILE_INPUT
:
382 src
.reg
= fpc
->r_input
[fsrc
->Register
.Index
];
384 case TGSI_FILE_CONSTANT
:
385 src
.reg
= nvfx_reg(NVFXSR_CONST
, fsrc
->Register
.Index
);
387 case TGSI_FILE_IMMEDIATE
:
388 assert(fsrc
->Register
.Index
< fpc
->nr_imm
);
389 src
.reg
= fpc
->r_imm
[fsrc
->Register
.Index
];
391 case TGSI_FILE_TEMPORARY
:
392 src
.reg
= fpc
->r_temp
[fsrc
->Register
.Index
];
394 /* NV40 fragprog result regs are just temps, so this is simple */
395 case TGSI_FILE_OUTPUT
:
396 src
.reg
= fpc
->r_result
[fsrc
->Register
.Index
];
399 NOUVEAU_ERR("bad src file\n");
405 src
.abs
= fsrc
->Register
.Absolute
;
406 src
.negate
= fsrc
->Register
.Negate
;
407 src
.swz
[0] = fsrc
->Register
.SwizzleX
;
408 src
.swz
[1] = fsrc
->Register
.SwizzleY
;
409 src
.swz
[2] = fsrc
->Register
.SwizzleZ
;
410 src
.swz
[3] = fsrc
->Register
.SwizzleW
;
412 src
.indirect_reg
= 0;
413 src
.indirect_swz
= 0;
417 static inline struct nvfx_reg
418 tgsi_dst(struct nvfx_fpc
*fpc
, const struct tgsi_full_dst_register
*fdst
) {
419 switch (fdst
->Register
.File
) {
420 case TGSI_FILE_OUTPUT
:
421 return fpc
->r_result
[fdst
->Register
.Index
];
422 case TGSI_FILE_TEMPORARY
:
423 return fpc
->r_temp
[fdst
->Register
.Index
];
425 return nvfx_reg(NVFXSR_NONE
, 0);
427 NOUVEAU_ERR("bad dst file %d\n", fdst
->Register
.File
);
428 return nvfx_reg(NVFXSR_NONE
, 0);
437 if (tgsi
& TGSI_WRITEMASK_X
) mask
|= NVFX_FP_MASK_X
;
438 if (tgsi
& TGSI_WRITEMASK_Y
) mask
|= NVFX_FP_MASK_Y
;
439 if (tgsi
& TGSI_WRITEMASK_Z
) mask
|= NVFX_FP_MASK_Z
;
440 if (tgsi
& TGSI_WRITEMASK_W
) mask
|= NVFX_FP_MASK_W
;
445 nvfx_fragprog_parse_instruction(struct nvfx_fpc
*fpc
,
446 const struct tgsi_full_instruction
*finst
)
448 const struct nvfx_src none
= nvfx_src(nvfx_reg(NVFXSR_NONE
, 0));
449 struct nvfx_insn insn
;
450 struct nvfx_src src
[3], tmp
;
452 int mask
, sat
, unit
= 0;
453 int ai
= -1, ci
= -1, ii
= -1;
456 if (finst
->Instruction
.Opcode
== TGSI_OPCODE_END
)
459 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
460 const struct tgsi_full_src_register
*fsrc
;
462 fsrc
= &finst
->Src
[i
];
463 if (fsrc
->Register
.File
== TGSI_FILE_TEMPORARY
) {
464 src
[i
] = tgsi_src(fpc
, fsrc
);
468 for (i
= 0; i
< finst
->Instruction
.NumSrcRegs
; i
++) {
469 const struct tgsi_full_src_register
*fsrc
;
471 fsrc
= &finst
->Src
[i
];
473 switch (fsrc
->Register
.File
) {
474 case TGSI_FILE_INPUT
:
475 if(fpc
->fp
->info
.input_semantic_name
[fsrc
->Register
.Index
] == TGSI_SEMANTIC_FOG
&& (0
476 || fsrc
->Register
.SwizzleX
== PIPE_SWIZZLE_W
477 || fsrc
->Register
.SwizzleY
== PIPE_SWIZZLE_W
478 || fsrc
->Register
.SwizzleZ
== PIPE_SWIZZLE_W
479 || fsrc
->Register
.SwizzleW
== PIPE_SWIZZLE_W
481 /* hardware puts 0 in fogcoord.w, but GL/Gallium want 1 there */
482 struct nvfx_src addend
= nvfx_src(nvfx_fp_imm(fpc
, 0, 0, 0, 1));
483 addend
.swz
[0] = fsrc
->Register
.SwizzleX
;
484 addend
.swz
[1] = fsrc
->Register
.SwizzleY
;
485 addend
.swz
[2] = fsrc
->Register
.SwizzleZ
;
486 addend
.swz
[3] = fsrc
->Register
.SwizzleW
;
487 src
[i
] = nvfx_src(temp(fpc
));
488 nvfx_fp_emit(fpc
, arith(0, ADD
, src
[i
].reg
, NVFX_FP_MASK_ALL
, tgsi_src(fpc
, fsrc
), addend
, none
));
489 } else if (ai
== -1 || ai
== fsrc
->Register
.Index
) {
490 ai
= fsrc
->Register
.Index
;
491 src
[i
] = tgsi_src(fpc
, fsrc
);
493 src
[i
] = nvfx_src(temp(fpc
));
494 nvfx_fp_emit(fpc
, arith(0, MOV
, src
[i
].reg
, NVFX_FP_MASK_ALL
, tgsi_src(fpc
, fsrc
), none
, none
));
497 case TGSI_FILE_CONSTANT
:
498 if ((ci
== -1 && ii
== -1) ||
499 ci
== fsrc
->Register
.Index
) {
500 ci
= fsrc
->Register
.Index
;
501 src
[i
] = tgsi_src(fpc
, fsrc
);
503 src
[i
] = nvfx_src(temp(fpc
));
504 nvfx_fp_emit(fpc
, arith(0, MOV
, src
[i
].reg
, NVFX_FP_MASK_ALL
, tgsi_src(fpc
, fsrc
), none
, none
));
507 case TGSI_FILE_IMMEDIATE
:
508 if ((ci
== -1 && ii
== -1) ||
509 ii
== fsrc
->Register
.Index
) {
510 ii
= fsrc
->Register
.Index
;
511 src
[i
] = tgsi_src(fpc
, fsrc
);
513 src
[i
] = nvfx_src(temp(fpc
));
514 nvfx_fp_emit(fpc
, arith(0, MOV
, src
[i
].reg
, NVFX_FP_MASK_ALL
, tgsi_src(fpc
, fsrc
), none
, none
));
517 case TGSI_FILE_TEMPORARY
:
520 case TGSI_FILE_SAMPLER
:
521 unit
= fsrc
->Register
.Index
;
523 case TGSI_FILE_OUTPUT
:
526 NOUVEAU_ERR("bad src file\n");
531 dst
= tgsi_dst(fpc
, &finst
->Dst
[0]);
532 mask
= tgsi_mask(finst
->Dst
[0].Register
.WriteMask
);
533 sat
= finst
->Instruction
.Saturate
;
535 switch (finst
->Instruction
.Opcode
) {
536 case TGSI_OPCODE_ADD
:
537 nvfx_fp_emit(fpc
, arith(sat
, ADD
, dst
, mask
, src
[0], src
[1], none
));
539 case TGSI_OPCODE_CEIL
:
540 tmp
= nvfx_src(temp(fpc
));
541 nvfx_fp_emit(fpc
, arith(0, FLR
, tmp
.reg
, mask
, neg(src
[0]), none
, none
));
542 nvfx_fp_emit(fpc
, arith(sat
, MOV
, dst
, mask
, neg(tmp
), none
, none
));
544 case TGSI_OPCODE_CMP
:
545 insn
= arith(0, MOV
, none
.reg
, mask
, src
[0], none
, none
);
547 nvfx_fp_emit(fpc
, insn
);
549 insn
= arith(sat
, MOV
, dst
, mask
, src
[2], none
, none
);
550 insn
.cc_test
= NVFX_COND_GE
;
551 nvfx_fp_emit(fpc
, insn
);
553 insn
= arith(sat
, MOV
, dst
, mask
, src
[1], none
, none
);
554 insn
.cc_test
= NVFX_COND_LT
;
555 nvfx_fp_emit(fpc
, insn
);
557 case TGSI_OPCODE_COS
:
558 nvfx_fp_emit(fpc
, arith(sat
, COS
, dst
, mask
, src
[0], none
, none
));
560 case TGSI_OPCODE_DDX
:
561 if (mask
& (NVFX_FP_MASK_Z
| NVFX_FP_MASK_W
)) {
562 tmp
= nvfx_src(temp(fpc
));
563 nvfx_fp_emit(fpc
, arith(sat
, DDX
, tmp
.reg
, NVFX_FP_MASK_X
| NVFX_FP_MASK_Y
, swz(src
[0], Z
, W
, Z
, W
), none
, none
));
564 nvfx_fp_emit(fpc
, arith(0, MOV
, tmp
.reg
, NVFX_FP_MASK_Z
| NVFX_FP_MASK_W
, swz(tmp
, X
, Y
, X
, Y
), none
, none
));
565 nvfx_fp_emit(fpc
, arith(sat
, DDX
, tmp
.reg
, NVFX_FP_MASK_X
| NVFX_FP_MASK_Y
, src
[0], none
, none
));
566 nvfx_fp_emit(fpc
, arith(0, MOV
, dst
, mask
, tmp
, none
, none
));
568 nvfx_fp_emit(fpc
, arith(sat
, DDX
, dst
, mask
, src
[0], none
, none
));
571 case TGSI_OPCODE_DDY
:
572 if (mask
& (NVFX_FP_MASK_Z
| NVFX_FP_MASK_W
)) {
573 tmp
= nvfx_src(temp(fpc
));
574 nvfx_fp_emit(fpc
, arith(sat
, DDY
, tmp
.reg
, NVFX_FP_MASK_X
| NVFX_FP_MASK_Y
, swz(src
[0], Z
, W
, Z
, W
), none
, none
));
575 nvfx_fp_emit(fpc
, arith(0, MOV
, tmp
.reg
, NVFX_FP_MASK_Z
| NVFX_FP_MASK_W
, swz(tmp
, X
, Y
, X
, Y
), none
, none
));
576 nvfx_fp_emit(fpc
, arith(sat
, DDY
, tmp
.reg
, NVFX_FP_MASK_X
| NVFX_FP_MASK_Y
, src
[0], none
, none
));
577 nvfx_fp_emit(fpc
, arith(0, MOV
, dst
, mask
, tmp
, none
, none
));
579 nvfx_fp_emit(fpc
, arith(sat
, DDY
, dst
, mask
, src
[0], none
, none
));
582 case TGSI_OPCODE_DP2
:
583 tmp
= nvfx_src(temp(fpc
));
584 nvfx_fp_emit(fpc
, arith(0, MUL
, tmp
.reg
, NVFX_FP_MASK_X
| NVFX_FP_MASK_Y
, src
[0], src
[1], none
));
585 nvfx_fp_emit(fpc
, arith(0, ADD
, dst
, mask
, swz(tmp
, X
, X
, X
, X
), swz(tmp
, Y
, Y
, Y
, Y
), none
));
587 case TGSI_OPCODE_DP3
:
588 nvfx_fp_emit(fpc
, arith(sat
, DP3
, dst
, mask
, src
[0], src
[1], none
));
590 case TGSI_OPCODE_DP4
:
591 nvfx_fp_emit(fpc
, arith(sat
, DP4
, dst
, mask
, src
[0], src
[1], none
));
593 case TGSI_OPCODE_DPH
:
594 tmp
= nvfx_src(temp(fpc
));
595 nvfx_fp_emit(fpc
, arith(0, DP3
, tmp
.reg
, NVFX_FP_MASK_X
, src
[0], src
[1], none
));
596 nvfx_fp_emit(fpc
, arith(sat
, ADD
, dst
, mask
, swz(tmp
, X
, X
, X
, X
), swz(src
[1], W
, W
, W
, W
), none
));
598 case TGSI_OPCODE_DST
:
599 nvfx_fp_emit(fpc
, arith(sat
, DST
, dst
, mask
, src
[0], src
[1], none
));
601 case TGSI_OPCODE_EX2
:
602 nvfx_fp_emit(fpc
, arith(sat
, EX2
, dst
, mask
, src
[0], none
, none
));
604 case TGSI_OPCODE_FLR
:
605 nvfx_fp_emit(fpc
, arith(sat
, FLR
, dst
, mask
, src
[0], none
, none
));
607 case TGSI_OPCODE_FRC
:
608 nvfx_fp_emit(fpc
, arith(sat
, FRC
, dst
, mask
, src
[0], none
, none
));
610 case TGSI_OPCODE_KILL
:
611 nvfx_fp_emit(fpc
, arith(0, KIL
, none
.reg
, 0, none
, none
, none
));
613 case TGSI_OPCODE_KILL_IF
:
614 insn
= arith(0, MOV
, none
.reg
, NVFX_FP_MASK_ALL
, src
[0], none
, none
);
616 nvfx_fp_emit(fpc
, insn
);
618 insn
= arith(0, KIL
, none
.reg
, 0, none
, none
, none
);
619 insn
.cc_test
= NVFX_COND_LT
;
620 nvfx_fp_emit(fpc
, insn
);
622 case TGSI_OPCODE_LG2
:
623 nvfx_fp_emit(fpc
, arith(sat
, LG2
, dst
, mask
, src
[0], none
, none
));
625 case TGSI_OPCODE_LIT
:
627 nvfx_fp_emit(fpc
, arith(sat
, LIT_NV30
, dst
, mask
, src
[0], none
, none
));
629 /* we use FLT_MIN, so that log2 never gives -infinity, and thus multiplication by
630 * specular 0 always gives 0, so that ex2 gives 1, to satisfy the 0^0 = 1 requirement
632 * NOTE: if we start using half precision, we might need an fp16 FLT_MIN here instead
634 struct nvfx_src maxs
= nvfx_src(nvfx_fp_imm(fpc
, 0, FLT_MIN
, 0, 0));
635 tmp
= nvfx_src(temp(fpc
));
636 if (ci
>= 0 || ii
>= 0) {
637 nvfx_fp_emit(fpc
, arith(0, MOV
, tmp
.reg
, NVFX_FP_MASK_X
| NVFX_FP_MASK_Y
, maxs
, none
, none
));
640 nvfx_fp_emit(fpc
, arith(0, MAX
, tmp
.reg
, NVFX_FP_MASK_Y
| NVFX_FP_MASK_W
, swz(src
[0], X
, X
, X
, Y
), swz(maxs
, X
, X
, Y
, Y
), none
));
641 nvfx_fp_emit(fpc
, arith(0, LG2
, tmp
.reg
, NVFX_FP_MASK_W
, swz(tmp
, W
, W
, W
, W
), none
, none
));
642 nvfx_fp_emit(fpc
, arith(0, MUL
, tmp
.reg
, NVFX_FP_MASK_W
, swz(tmp
, W
, W
, W
, W
), swz(src
[0], W
, W
, W
, W
), none
));
643 nvfx_fp_emit(fpc
, arith(sat
, LITEX2_NV40
, dst
, mask
, swz(tmp
, Y
, Y
, W
, W
), none
, none
));
646 case TGSI_OPCODE_LRP
:
648 nvfx_fp_emit(fpc
, arith(sat
, LRP_NV30
, dst
, mask
, src
[0], src
[1], src
[2]));
650 tmp
= nvfx_src(temp(fpc
));
651 nvfx_fp_emit(fpc
, arith(0, MAD
, tmp
.reg
, mask
, neg(src
[0]), src
[2], src
[2]));
652 nvfx_fp_emit(fpc
, arith(sat
, MAD
, dst
, mask
, src
[0], src
[1], tmp
));
655 case TGSI_OPCODE_MAD
:
656 nvfx_fp_emit(fpc
, arith(sat
, MAD
, dst
, mask
, src
[0], src
[1], src
[2]));
658 case TGSI_OPCODE_MAX
:
659 nvfx_fp_emit(fpc
, arith(sat
, MAX
, dst
, mask
, src
[0], src
[1], none
));
661 case TGSI_OPCODE_MIN
:
662 nvfx_fp_emit(fpc
, arith(sat
, MIN
, dst
, mask
, src
[0], src
[1], none
));
664 case TGSI_OPCODE_MOV
:
665 nvfx_fp_emit(fpc
, arith(sat
, MOV
, dst
, mask
, src
[0], none
, none
));
667 case TGSI_OPCODE_MUL
:
668 nvfx_fp_emit(fpc
, arith(sat
, MUL
, dst
, mask
, src
[0], src
[1], none
));
670 case TGSI_OPCODE_NOP
:
672 case TGSI_OPCODE_POW
:
674 nvfx_fp_emit(fpc
, arith(sat
, POW_NV30
, dst
, mask
, src
[0], src
[1], none
));
676 tmp
= nvfx_src(temp(fpc
));
677 nvfx_fp_emit(fpc
, arith(0, LG2
, tmp
.reg
, NVFX_FP_MASK_X
, swz(src
[0], X
, X
, X
, X
), none
, none
));
678 nvfx_fp_emit(fpc
, arith(0, MUL
, tmp
.reg
, NVFX_FP_MASK_X
, swz(tmp
, X
, X
, X
, X
), swz(src
[1], X
, X
, X
, X
), none
));
679 nvfx_fp_emit(fpc
, arith(sat
, EX2
, dst
, mask
, swz(tmp
, X
, X
, X
, X
), none
, none
));
682 case TGSI_OPCODE_RCP
:
683 nvfx_fp_emit(fpc
, arith(sat
, RCP
, dst
, mask
, src
[0], none
, none
));
685 case TGSI_OPCODE_RSQ
:
687 nvfx_fp_emit(fpc
, arith(sat
, RSQ_NV30
, dst
, mask
, abs(swz(src
[0], X
, X
, X
, X
)), none
, none
));
689 tmp
= nvfx_src(temp(fpc
));
690 insn
= arith(0, LG2
, tmp
.reg
, NVFX_FP_MASK_X
, abs(swz(src
[0], X
, X
, X
, X
)), none
, none
);
691 insn
.scale
= NVFX_FP_OP_DST_SCALE_INV_2X
;
692 nvfx_fp_emit(fpc
, insn
);
693 nvfx_fp_emit(fpc
, arith(sat
, EX2
, dst
, mask
, neg(swz(tmp
, X
, X
, X
, X
)), none
, none
));
696 case TGSI_OPCODE_SCS
:
697 /* avoid overwriting the source */
698 if(src
[0].swz
[NVFX_SWZ_X
] != NVFX_SWZ_X
)
700 if (mask
& NVFX_FP_MASK_X
)
701 nvfx_fp_emit(fpc
, arith(sat
, COS
, dst
, NVFX_FP_MASK_X
, swz(src
[0], X
, X
, X
, X
), none
, none
));
702 if (mask
& NVFX_FP_MASK_Y
)
703 nvfx_fp_emit(fpc
, arith(sat
, SIN
, dst
, NVFX_FP_MASK_Y
, swz(src
[0], X
, X
, X
, X
), none
, none
));
707 if (mask
& NVFX_FP_MASK_Y
)
708 nvfx_fp_emit(fpc
, arith(sat
, SIN
, dst
, NVFX_FP_MASK_Y
, swz(src
[0], X
, X
, X
, X
), none
, none
));
709 if (mask
& NVFX_FP_MASK_X
)
710 nvfx_fp_emit(fpc
, arith(sat
, COS
, dst
, NVFX_FP_MASK_X
, swz(src
[0], X
, X
, X
, X
), none
, none
));
713 case TGSI_OPCODE_SEQ
:
714 nvfx_fp_emit(fpc
, arith(sat
, SEQ
, dst
, mask
, src
[0], src
[1], none
));
716 case TGSI_OPCODE_SGE
:
717 nvfx_fp_emit(fpc
, arith(sat
, SGE
, dst
, mask
, src
[0], src
[1], none
));
719 case TGSI_OPCODE_SGT
:
720 nvfx_fp_emit(fpc
, arith(sat
, SGT
, dst
, mask
, src
[0], src
[1], none
));
722 case TGSI_OPCODE_SIN
:
723 nvfx_fp_emit(fpc
, arith(sat
, SIN
, dst
, mask
, src
[0], none
, none
));
725 case TGSI_OPCODE_SLE
:
726 nvfx_fp_emit(fpc
, arith(sat
, SLE
, dst
, mask
, src
[0], src
[1], none
));
728 case TGSI_OPCODE_SLT
:
729 nvfx_fp_emit(fpc
, arith(sat
, SLT
, dst
, mask
, src
[0], src
[1], none
));
731 case TGSI_OPCODE_SNE
:
732 nvfx_fp_emit(fpc
, arith(sat
, SNE
, dst
, mask
, src
[0], src
[1], none
));
734 case TGSI_OPCODE_SSG
:
736 struct nvfx_src minones
= swz(nvfx_src(nvfx_fp_imm(fpc
, -1, -1, -1, -1)), X
, X
, X
, X
);
738 insn
= arith(sat
, MOV
, dst
, mask
, src
[0], none
, none
);
740 nvfx_fp_emit(fpc
, insn
);
742 insn
= arith(0, STR
, dst
, mask
, none
, none
, none
);
743 insn
.cc_test
= NVFX_COND_GT
;
744 nvfx_fp_emit(fpc
, insn
);
747 insn
= arith(0, MOV
, dst
, mask
, minones
, none
, none
);
748 insn
.cc_test
= NVFX_COND_LT
;
749 nvfx_fp_emit(fpc
, insn
);
753 case TGSI_OPCODE_SUB
:
754 nvfx_fp_emit(fpc
, arith(sat
, ADD
, dst
, mask
, src
[0], neg(src
[1]), none
));
756 case TGSI_OPCODE_TEX
:
757 nvfx_fp_emit(fpc
, tex(sat
, TEX
, unit
, dst
, mask
, src
[0], none
, none
));
759 case TGSI_OPCODE_TRUNC
:
760 tmp
= nvfx_src(temp(fpc
));
761 insn
= arith(0, MOV
, none
.reg
, mask
, src
[0], none
, none
);
763 nvfx_fp_emit(fpc
, insn
);
765 nvfx_fp_emit(fpc
, arith(0, FLR
, tmp
.reg
, mask
, abs(src
[0]), none
, none
));
766 nvfx_fp_emit(fpc
, arith(sat
, MOV
, dst
, mask
, tmp
, none
, none
));
768 insn
= arith(sat
, MOV
, dst
, mask
, neg(tmp
), none
, none
);
769 insn
.cc_test
= NVFX_COND_LT
;
770 nvfx_fp_emit(fpc
, insn
);
772 case TGSI_OPCODE_TXB
:
773 nvfx_fp_emit(fpc
, tex(sat
, TXB
, unit
, dst
, mask
, src
[0], none
, none
));
775 case TGSI_OPCODE_TXL
:
777 nvfx_fp_emit(fpc
, tex(sat
, TXL_NV40
, unit
, dst
, mask
, src
[0], none
, none
));
778 else /* unsupported on nv30, use TEX and hope they like it */
779 nvfx_fp_emit(fpc
, tex(sat
, TEX
, unit
, dst
, mask
, src
[0], none
, none
));
781 case TGSI_OPCODE_TXP
:
782 nvfx_fp_emit(fpc
, tex(sat
, TXP
, unit
, dst
, mask
, src
[0], none
, none
));
784 case TGSI_OPCODE_XPD
:
785 tmp
= nvfx_src(temp(fpc
));
786 nvfx_fp_emit(fpc
, arith(0, MUL
, tmp
.reg
, mask
, swz(src
[0], Z
, X
, Y
, Y
), swz(src
[1], Y
, Z
, X
, X
), none
));
787 nvfx_fp_emit(fpc
, arith(sat
, MAD
, dst
, (mask
& ~NVFX_FP_MASK_W
), swz(src
[0], Y
, Z
, X
, X
), swz(src
[1], Z
, X
, Y
, Y
), neg(tmp
)));
791 // MOVRC0 R31 (TR0.xyzw), R<src>:
792 // IF (NE.xxxx) ELSE <else> END <end>
795 nv40_fp_if(fpc
, src
[0]);
798 case TGSI_OPCODE_ELSE
:
803 assert(util_dynarray_contains(&fpc
->if_stack
, unsigned));
804 hw
= &fpc
->fp
->insn
[util_dynarray_top(&fpc
->if_stack
, unsigned)];
805 hw
[2] = NV40_FP_OP_OPCODE_IS_BRANCH
| fpc
->fp
->insn_len
;
809 case TGSI_OPCODE_ENDIF
:
814 assert(util_dynarray_contains(&fpc
->if_stack
, unsigned));
815 hw
= &fpc
->fp
->insn
[util_dynarray_pop(&fpc
->if_stack
, unsigned)];
817 hw
[2] = NV40_FP_OP_OPCODE_IS_BRANCH
| fpc
->fp
->insn_len
;
818 hw
[3] = fpc
->fp
->insn_len
;
822 case TGSI_OPCODE_BGNSUB
:
823 case TGSI_OPCODE_ENDSUB
:
824 /* nothing to do here */
827 case TGSI_OPCODE_CAL
:
830 nv40_fp_cal(fpc
, finst
->Label
.Label
);
833 case TGSI_OPCODE_RET
:
839 case TGSI_OPCODE_BGNLOOP
:
842 /* TODO: we should support using two nested REPs to allow a > 255 iteration count */
843 nv40_fp_rep(fpc
, 255, finst
->Label
.Label
);
846 case TGSI_OPCODE_ENDLOOP
:
849 case TGSI_OPCODE_BRK
:
855 case TGSI_OPCODE_CONT
:
857 static int warned
= 0;
859 NOUVEAU_ERR("Sorry, the continue keyword is not implemented: ignoring it.\n");
866 NOUVEAU_ERR("invalid opcode %d\n", finst
->Instruction
.Opcode
);
875 static int warned
= 0;
878 "Sorry, control flow instructions are not supported in hardware on nv3x: ignoring them\n"
879 "If rendering is incorrect, try to disable GLSL support in the application.\n");
887 nvfx_fragprog_parse_decl_input(struct nvfx_fpc
*fpc
,
888 const struct tgsi_full_declaration
*fdec
)
890 unsigned idx
= fdec
->Range
.First
;
893 switch (fdec
->Semantic
.Name
) {
894 case TGSI_SEMANTIC_POSITION
:
895 hw
= NVFX_FP_OP_INPUT_SRC_POSITION
;
897 case TGSI_SEMANTIC_COLOR
:
898 hw
= NVFX_FP_OP_INPUT_SRC_COL0
+ fdec
->Semantic
.Index
;
900 case TGSI_SEMANTIC_FOG
:
901 hw
= NVFX_FP_OP_INPUT_SRC_FOGC
;
903 case TGSI_SEMANTIC_FACE
:
904 hw
= NV40_FP_OP_INPUT_SRC_FACING
;
906 case TGSI_SEMANTIC_TEXCOORD
:
907 assert(fdec
->Semantic
.Index
< 8);
908 fpc
->fp
->texcoord
[fdec
->Semantic
.Index
] = fdec
->Semantic
.Index
;
909 fpc
->fp
->texcoords
|= (1 << fdec
->Semantic
.Index
);
910 fpc
->fp
->vp_or
|= (0x00004000 << fdec
->Semantic
.Index
);
911 hw
= NVFX_FP_OP_INPUT_SRC_TC(fdec
->Semantic
.Index
);
913 case TGSI_SEMANTIC_GENERIC
:
914 case TGSI_SEMANTIC_PCOORD
:
915 /* will be assigned to remaining TC slots later */
922 fpc
->r_input
[idx
] = nvfx_reg(NVFXSR_INPUT
, hw
);
927 nvfx_fragprog_assign_generic(struct nvfx_fpc
*fpc
,
928 const struct tgsi_full_declaration
*fdec
)
930 unsigned num_texcoords
= fpc
->is_nv4x
? 10 : 8;
931 unsigned idx
= fdec
->Range
.First
;
934 switch (fdec
->Semantic
.Name
) {
935 case TGSI_SEMANTIC_GENERIC
:
936 case TGSI_SEMANTIC_PCOORD
:
937 for (hw
= 0; hw
< num_texcoords
; hw
++) {
938 if (fpc
->fp
->texcoord
[hw
] == 0xffff) {
940 fpc
->fp
->texcoords
|= (0x1 << hw
);
941 fpc
->fp
->vp_or
|= (0x00004000 << hw
);
943 fpc
->fp
->vp_or
|= (0x00001000 << (hw
- 8));
945 if (fdec
->Semantic
.Name
== TGSI_SEMANTIC_PCOORD
) {
946 fpc
->fp
->texcoord
[hw
] = 0xfffe;
947 fpc
->fp
->point_sprite_control
|= (0x00000100 << hw
);
949 fpc
->fp
->texcoord
[hw
] = fdec
->Semantic
.Index
+ 8;
951 hw
= NVFX_FP_OP_INPUT_SRC_TC(hw
);
952 fpc
->r_input
[idx
] = nvfx_reg(NVFXSR_INPUT
, hw
);
963 nvfx_fragprog_parse_decl_output(struct nvfx_fpc
*fpc
,
964 const struct tgsi_full_declaration
*fdec
)
966 unsigned idx
= fdec
->Range
.First
;
969 switch (fdec
->Semantic
.Name
) {
970 case TGSI_SEMANTIC_POSITION
:
973 case TGSI_SEMANTIC_COLOR
:
975 switch (fdec
->Semantic
.Index
) {
976 case 0: hw
= 0; break;
977 case 1: hw
= 2; break;
978 case 2: hw
= 3; break;
979 case 3: hw
= 4; break;
981 if(hw
> ((fpc
->is_nv4x
) ? 4 : 2)) {
982 NOUVEAU_ERR("bad rcol index\n");
987 NOUVEAU_ERR("bad output semantic\n");
991 fpc
->r_result
[idx
] = nvfx_reg(NVFXSR_OUTPUT
, hw
);
992 fpc
->r_temps
|= (1ULL << hw
);
997 nvfx_fragprog_prepare(struct nvfx_fpc
*fpc
)
999 struct tgsi_parse_context p
;
1000 int high_temp
= -1, i
;
1002 fpc
->r_imm
= CALLOC(fpc
->fp
->info
.immediate_count
, sizeof(struct nvfx_reg
));
1004 tgsi_parse_init(&p
, fpc
->fp
->pipe
.tokens
);
1005 while (!tgsi_parse_end_of_tokens(&p
)) {
1006 const union tgsi_full_token
*tok
= &p
.FullToken
;
1008 tgsi_parse_token(&p
);
1009 switch(tok
->Token
.Type
) {
1010 case TGSI_TOKEN_TYPE_DECLARATION
:
1012 const struct tgsi_full_declaration
*fdec
;
1013 fdec
= &p
.FullToken
.FullDeclaration
;
1014 switch (fdec
->Declaration
.File
) {
1015 case TGSI_FILE_INPUT
:
1016 if (!nvfx_fragprog_parse_decl_input(fpc
, fdec
))
1019 case TGSI_FILE_OUTPUT
:
1020 if (!nvfx_fragprog_parse_decl_output(fpc
, fdec
))
1023 case TGSI_FILE_TEMPORARY
:
1024 if (fdec
->Range
.Last
> high_temp
) {
1034 case TGSI_TOKEN_TYPE_IMMEDIATE
:
1036 struct tgsi_full_immediate
*imm
;
1038 imm
= &p
.FullToken
.FullImmediate
;
1039 assert(imm
->Immediate
.DataType
== TGSI_IMM_FLOAT32
);
1040 assert(fpc
->nr_imm
< fpc
->fp
->info
.immediate_count
);
1042 fpc
->r_imm
[fpc
->nr_imm
++] = nvfx_fp_imm(fpc
, imm
->u
[0].Float
, imm
->u
[1].Float
, imm
->u
[2].Float
, imm
->u
[3].Float
);
1049 tgsi_parse_free(&p
);
1051 tgsi_parse_init(&p
, fpc
->fp
->pipe
.tokens
);
1052 while (!tgsi_parse_end_of_tokens(&p
)) {
1053 const struct tgsi_full_declaration
*fdec
;
1054 tgsi_parse_token(&p
);
1055 switch(p
.FullToken
.Token
.Type
) {
1056 case TGSI_TOKEN_TYPE_DECLARATION
:
1057 fdec
= &p
.FullToken
.FullDeclaration
;
1058 switch (fdec
->Declaration
.File
) {
1059 case TGSI_FILE_INPUT
:
1060 if (!nvfx_fragprog_assign_generic(fpc
, fdec
))
1071 tgsi_parse_free(&p
);
1074 fpc
->r_temp
= CALLOC(high_temp
, sizeof(struct nvfx_reg
));
1075 for (i
= 0; i
< high_temp
; i
++)
1076 fpc
->r_temp
[i
] = temp(fpc
);
1077 fpc
->r_temps_discard
= 0ULL;
1086 tgsi_parse_free(&p
);
1090 DEBUG_GET_ONCE_BOOL_OPTION(nvfx_dump_fp
, "NVFX_DUMP_FP", false)
1093 _nvfx_fragprog_translate(uint16_t oclass
, struct nv30_fragprog
*fp
)
1095 struct tgsi_parse_context parse
;
1096 struct nvfx_fpc
*fpc
= NULL
;
1097 struct util_dynarray insns
;
1099 fp
->translated
= false;
1100 fp
->point_sprite_control
= 0;
1103 fpc
= CALLOC_STRUCT(nvfx_fpc
);
1107 fpc
->is_nv4x
= (oclass
>= NV40_3D_CLASS
) ? ~0 : 0;
1108 fpc
->max_temps
= fpc
->is_nv4x
? 48 : 32;
1111 memset(fp
->texcoord
, 0xff, sizeof(fp
->texcoord
));
1113 if (fp
->info
.properties
[TGSI_PROPERTY_FS_COORD_ORIGIN
])
1114 fp
->coord_conventions
|= NV30_3D_COORD_CONVENTIONS_ORIGIN_INVERTED
;
1115 if (fp
->info
.properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
])
1116 fp
->coord_conventions
|= NV30_3D_COORD_CONVENTIONS_CENTER_INTEGER
;
1117 if (fp
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
])
1118 fp
->rt_enable
|= NV30_3D_RT_ENABLE_MRT
;
1120 if (!nvfx_fragprog_prepare(fpc
))
1123 tgsi_parse_init(&parse
, fp
->pipe
.tokens
);
1124 util_dynarray_init(&insns
);
1126 while (!tgsi_parse_end_of_tokens(&parse
)) {
1127 tgsi_parse_token(&parse
);
1129 switch (parse
.FullToken
.Token
.Type
) {
1130 case TGSI_TOKEN_TYPE_INSTRUCTION
:
1132 const struct tgsi_full_instruction
*finst
;
1134 util_dynarray_append(&insns
, unsigned, fp
->insn_len
);
1135 finst
= &parse
.FullToken
.FullInstruction
;
1136 if (!nvfx_fragprog_parse_instruction(fpc
, finst
))
1144 util_dynarray_append(&insns
, unsigned, fp
->insn_len
);
1146 for(unsigned i
= 0; i
< fpc
->label_relocs
.size
; i
+= sizeof(struct nvfx_relocation
))
1148 struct nvfx_relocation
* label_reloc
= (struct nvfx_relocation
*)((char*)fpc
->label_relocs
.data
+ i
);
1149 fp
->insn
[label_reloc
->location
] |= ((unsigned*)insns
.data
)[label_reloc
->target
];
1151 util_dynarray_fini(&insns
);
1154 fp
->fp_control
|= (fpc
->num_regs
-1)/2;
1156 fp
->fp_control
|= fpc
->num_regs
<< NV40_3D_FP_CONTROL_TEMP_COUNT__SHIFT
;
1158 /* Terminate final instruction */
1160 fp
->insn
[fpc
->inst_offset
] |= 0x00000001;
1162 /* Append NOP + END instruction for branches to the end of the program */
1163 fpc
->inst_offset
= fp
->insn_len
;
1165 fp
->insn
[fpc
->inst_offset
+ 0] = 0x00000001;
1166 fp
->insn
[fpc
->inst_offset
+ 1] = 0x00000000;
1167 fp
->insn
[fpc
->inst_offset
+ 2] = 0x00000000;
1168 fp
->insn
[fpc
->inst_offset
+ 3] = 0x00000000;
1170 if(debug_get_option_nvfx_dump_fp())
1173 tgsi_dump(fp
->pipe
.tokens
, 0);
1175 debug_printf("\n%s fragment program:\n", fpc
->is_nv4x
? "nv4x" : "nv3x");
1176 for (unsigned i
= 0; i
< fp
->insn_len
; i
+= 4)
1177 debug_printf("%3u: %08x %08x %08x %08x\n", i
>> 2, fp
->insn
[i
], fp
->insn
[i
+ 1], fp
->insn
[i
+ 2], fp
->insn
[i
+ 3]);
1181 fp
->translated
= true;
1184 tgsi_parse_free(&parse
);
1189 util_dynarray_fini(&fpc
->if_stack
);
1190 util_dynarray_fini(&fpc
->label_relocs
);
1191 util_dynarray_fini(&fpc
->imm_data
);
1192 //util_dynarray_fini(&fpc->loop_stack);
1199 _debug_printf("Error: failed to compile this fragment program:\n");
1200 tgsi_dump(fp
->pipe
.tokens
, 0);