2 * Copyright (C) 2005 Ben Skeggs.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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, sublicense, 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
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 * Emit the r300_fragment_program_code that can be understood by the hardware.
32 * Input is a pre-transformed radeon_program.
34 * \author Ben Skeggs <darktama@iinet.net.au>
36 * \author Jerome Glisse <j.glisse@gmail.com>
40 * \todo Verify results of opcodes for accuracy, I've only checked them in
47 #include "shader/prog_instruction.h"
48 #include "shader/prog_parameter.h"
49 #include "shader/prog_print.h"
51 #include "r300_context.h"
52 #include "r300_fragprog.h"
54 #include "r300_state.h"
56 /* Mapping Mesa registers to R300 temporaries */
58 int reg
; /* Assigned hw temp */
59 unsigned int refcount
; /* Number of uses by mesa program */
63 * Describe the current lifetime information for an R300 temporary
66 /* Index of the first slot where this register is free in the sense
67 that it can be used as a new destination register.
68 This is -1 if the register has been assigned to a Mesa register
69 and the last access to the register has not yet been emitted */
72 /* Index of the first slot where this register is currently reserved.
73 This is used to stop e.g. a scalar operation from being moved
74 before the allocation time of a register that was first allocated
75 for a vector operation. */
78 /* Index of the first slot in which the register can be used as a
79 source without losing the value that is written by the last
80 emitted instruction that writes to the register */
84 /* Index to the slot where the register was last read.
85 This is also the first slot in which the register may be written again */
91 * Store usage information about an ALU instruction slot during the
92 * compilation of a fragment program.
94 #define SLOT_SRC_VECTOR (1<<0)
95 #define SLOT_SRC_SCALAR (1<<3)
96 #define SLOT_SRC_BOTH (SLOT_SRC_VECTOR | SLOT_SRC_SCALAR)
97 #define SLOT_OP_VECTOR (1<<16)
98 #define SLOT_OP_SCALAR (1<<17)
99 #define SLOT_OP_BOTH (SLOT_OP_VECTOR | SLOT_OP_SCALAR)
101 struct r300_pfs_compile_slot
{
102 /* Bitmask indicating which parts of the slot are used, using SLOT_ constants
106 /* Selected sources */
112 * Store information during compilation of fragment programs.
114 struct r300_pfs_compile_state
{
115 struct r300_fragment_program_compiler
*compiler
;
117 int nrslots
; /* number of ALU slots used so far */
119 /* Track which (parts of) slots are already filled with instructions */
120 struct r300_pfs_compile_slot slot
[PFS_MAX_ALU_INST
];
122 /* Track the validity of R300 temporaries */
123 struct reg_lifetime hwtemps
[PFS_NUM_TEMP_REGS
];
125 /* Used to map Mesa's inputs/temps onto hardware temps */
127 struct reg_acc temps
[PFS_NUM_TEMP_REGS
];
128 struct reg_acc inputs
[32]; /* don't actually need 32... */
130 /* Track usage of hardware temps, for register allocation,
131 * indirection detection, etc. */
138 * Usefull macros and values
140 #define ERROR(fmt, args...) do { \
141 fprintf(stderr, "%s::%s(): " fmt "\n", \
142 __FILE__, __FUNCTION__, ##args); \
143 fp->error = GL_TRUE; \
146 #define PFS_INVAL 0xFFFFFFFF
147 #define COMPILE_STATE \
148 struct r300_fragment_program *fp = cs->compiler->fp; \
149 struct r300_fragment_program_code *code = cs->compiler->code; \
152 #define SWIZZLE_XYZ 0
153 #define SWIZZLE_XXX 1
154 #define SWIZZLE_YYY 2
155 #define SWIZZLE_ZZZ 3
156 #define SWIZZLE_WWW 4
157 #define SWIZZLE_YZX 5
158 #define SWIZZLE_ZXY 6
159 #define SWIZZLE_WZY 7
160 #define SWIZZLE_111 8
161 #define SWIZZLE_000 9
162 #define SWIZZLE_HHH 10
164 #define swizzle(r, x, y, z, w) do_swizzle(cs, r, \
171 #define REG_TYPE_INPUT 0
172 #define REG_TYPE_OUTPUT 1
173 #define REG_TYPE_TEMP 2
174 #define REG_TYPE_CONST 3
176 #define REG_TYPE_SHIFT 0
177 #define REG_INDEX_SHIFT 2
178 #define REG_VSWZ_SHIFT 8
179 #define REG_SSWZ_SHIFT 13
180 #define REG_NEGV_SHIFT 18
181 #define REG_NEGS_SHIFT 19
182 #define REG_ABS_SHIFT 20
183 #define REG_NO_USE_SHIFT 21 // Hack for refcounting
184 #define REG_VALID_SHIFT 22 // Does the register contain a defined value?
185 #define REG_BUILTIN_SHIFT 23 // Is it a builtin (like all zero/all one)?
187 #define REG_TYPE_MASK (0x03 << REG_TYPE_SHIFT)
188 #define REG_INDEX_MASK (0x3F << REG_INDEX_SHIFT)
189 #define REG_VSWZ_MASK (0x1F << REG_VSWZ_SHIFT)
190 #define REG_SSWZ_MASK (0x1F << REG_SSWZ_SHIFT)
191 #define REG_NEGV_MASK (0x01 << REG_NEGV_SHIFT)
192 #define REG_NEGS_MASK (0x01 << REG_NEGS_SHIFT)
193 #define REG_ABS_MASK (0x01 << REG_ABS_SHIFT)
194 #define REG_NO_USE_MASK (0x01 << REG_NO_USE_SHIFT)
195 #define REG_VALID_MASK (0x01 << REG_VALID_SHIFT)
196 #define REG_BUILTIN_MASK (0x01 << REG_BUILTIN_SHIFT)
198 #define REG(type, index, vswz, sswz, nouse, valid, builtin) \
199 (((type << REG_TYPE_SHIFT) & REG_TYPE_MASK) | \
200 ((index << REG_INDEX_SHIFT) & REG_INDEX_MASK) | \
201 ((nouse << REG_NO_USE_SHIFT) & REG_NO_USE_MASK) | \
202 ((valid << REG_VALID_SHIFT) & REG_VALID_MASK) | \
203 ((builtin << REG_BUILTIN_SHIFT) & REG_BUILTIN_MASK) | \
204 ((vswz << REG_VSWZ_SHIFT) & REG_VSWZ_MASK) | \
205 ((sswz << REG_SSWZ_SHIFT) & REG_SSWZ_MASK))
206 #define REG_GET_TYPE(reg) \
207 ((reg & REG_TYPE_MASK) >> REG_TYPE_SHIFT)
208 #define REG_GET_INDEX(reg) \
209 ((reg & REG_INDEX_MASK) >> REG_INDEX_SHIFT)
210 #define REG_GET_VSWZ(reg) \
211 ((reg & REG_VSWZ_MASK) >> REG_VSWZ_SHIFT)
212 #define REG_GET_SSWZ(reg) \
213 ((reg & REG_SSWZ_MASK) >> REG_SSWZ_SHIFT)
214 #define REG_GET_NO_USE(reg) \
215 ((reg & REG_NO_USE_MASK) >> REG_NO_USE_SHIFT)
216 #define REG_GET_VALID(reg) \
217 ((reg & REG_VALID_MASK) >> REG_VALID_SHIFT)
218 #define REG_GET_BUILTIN(reg) \
219 ((reg & REG_BUILTIN_MASK) >> REG_BUILTIN_SHIFT)
220 #define REG_SET_TYPE(reg, type) \
221 reg = ((reg & ~REG_TYPE_MASK) | \
222 ((type << REG_TYPE_SHIFT) & REG_TYPE_MASK))
223 #define REG_SET_INDEX(reg, index) \
224 reg = ((reg & ~REG_INDEX_MASK) | \
225 ((index << REG_INDEX_SHIFT) & REG_INDEX_MASK))
226 #define REG_SET_VSWZ(reg, vswz) \
227 reg = ((reg & ~REG_VSWZ_MASK) | \
228 ((vswz << REG_VSWZ_SHIFT) & REG_VSWZ_MASK))
229 #define REG_SET_SSWZ(reg, sswz) \
230 reg = ((reg & ~REG_SSWZ_MASK) | \
231 ((sswz << REG_SSWZ_SHIFT) & REG_SSWZ_MASK))
232 #define REG_SET_NO_USE(reg, nouse) \
233 reg = ((reg & ~REG_NO_USE_MASK) | \
234 ((nouse << REG_NO_USE_SHIFT) & REG_NO_USE_MASK))
235 #define REG_SET_VALID(reg, valid) \
236 reg = ((reg & ~REG_VALID_MASK) | \
237 ((valid << REG_VALID_SHIFT) & REG_VALID_MASK))
238 #define REG_SET_BUILTIN(reg, builtin) \
239 reg = ((reg & ~REG_BUILTIN_MASK) | \
240 ((builtin << REG_BUILTIN_SHIFT) & REG_BUILTIN_MASK))
241 #define REG_ABS(reg) \
242 reg = (reg | REG_ABS_MASK)
243 #define REG_NEGV(reg) \
244 reg = (reg | REG_NEGV_MASK)
245 #define REG_NEGS(reg) \
246 reg = (reg | REG_NEGS_MASK)
248 #define NOP_INST0 ( \
249 (R300_ALU_OUTC_MAD) | \
250 (R300_ALU_ARGC_ZERO << R300_ALU_ARG0C_SHIFT) | \
251 (R300_ALU_ARGC_ZERO << R300_ALU_ARG1C_SHIFT) | \
252 (R300_ALU_ARGC_ZERO << R300_ALU_ARG2C_SHIFT))
253 #define NOP_INST1 ( \
254 ((0 | SRC_CONST) << R300_ALU_SRC0C_SHIFT) | \
255 ((0 | SRC_CONST) << R300_ALU_SRC1C_SHIFT) | \
256 ((0 | SRC_CONST) << R300_ALU_SRC2C_SHIFT))
257 #define NOP_INST2 ( \
258 (R300_ALU_OUTA_MAD) | \
259 (R300_ALU_ARGA_ZERO << R300_ALU_ARG0A_SHIFT) | \
260 (R300_ALU_ARGA_ZERO << R300_ALU_ARG1A_SHIFT) | \
261 (R300_ALU_ARGA_ZERO << R300_ALU_ARG2A_SHIFT))
262 #define NOP_INST3 ( \
263 ((0 | SRC_CONST) << R300_ALU_SRC0A_SHIFT) | \
264 ((0 | SRC_CONST) << R300_ALU_SRC1A_SHIFT) | \
265 ((0 | SRC_CONST) << R300_ALU_SRC2A_SHIFT))
269 * Datas structures for fragment program generation
272 /* description of r300 native hw instructions */
273 static const struct {
280 {"MAD", 3, R300_ALU_OUTC_MAD
, R300_ALU_OUTA_MAD
},
281 {"DP3", 2, R300_ALU_OUTC_DP3
, R300_ALU_OUTA_DP4
},
282 {"DP4", 2, R300_ALU_OUTC_DP4
, R300_ALU_OUTA_DP4
},
283 {"MIN", 2, R300_ALU_OUTC_MIN
, R300_ALU_OUTA_MIN
},
284 {"MAX", 2, R300_ALU_OUTC_MAX
, R300_ALU_OUTA_MAX
},
285 {"CMP", 3, R300_ALU_OUTC_CMP
, R300_ALU_OUTA_CMP
},
286 {"FRC", 1, R300_ALU_OUTC_FRC
, R300_ALU_OUTA_FRC
},
287 {"EX2", 1, R300_ALU_OUTC_REPL_ALPHA
, R300_ALU_OUTA_EX2
},
288 {"LG2", 1, R300_ALU_OUTC_REPL_ALPHA
, R300_ALU_OUTA_LG2
},
289 {"RCP", 1, R300_ALU_OUTC_REPL_ALPHA
, R300_ALU_OUTA_RCP
},
290 {"RSQ", 1, R300_ALU_OUTC_REPL_ALPHA
, R300_ALU_OUTA_RSQ
},
291 {"REPL_ALPHA", 1, R300_ALU_OUTC_REPL_ALPHA
, PFS_INVAL
},
292 {"CMPH", 3, R300_ALU_OUTC_CMPH
, PFS_INVAL
},
296 /* vector swizzles r300 can support natively, with a couple of
297 * cases we handle specially
299 * REG_VSWZ/REG_SSWZ is an index into this table
302 /* mapping from SWIZZLE_* to r300 native values for scalar insns */
303 #define SWIZZLE_HALF 6
305 #define MAKE_SWZ3(x, y, z) (MAKE_SWIZZLE4(SWIZZLE_##x, \
309 /* native swizzles */
310 static const struct r300_pfs_swizzle
{
311 GLuint hash
; /* swizzle value this matches */
312 GLuint base
; /* base value for hw swizzle */
313 GLuint stride
; /* difference in base between arg0/1/2 */
317 {MAKE_SWZ3(X
, Y
, Z
), R300_ALU_ARGC_SRC0C_XYZ
, 4, SLOT_SRC_VECTOR
},
318 {MAKE_SWZ3(X
, X
, X
), R300_ALU_ARGC_SRC0C_XXX
, 4, SLOT_SRC_VECTOR
},
319 {MAKE_SWZ3(Y
, Y
, Y
), R300_ALU_ARGC_SRC0C_YYY
, 4, SLOT_SRC_VECTOR
},
320 {MAKE_SWZ3(Z
, Z
, Z
), R300_ALU_ARGC_SRC0C_ZZZ
, 4, SLOT_SRC_VECTOR
},
321 {MAKE_SWZ3(W
, W
, W
), R300_ALU_ARGC_SRC0A
, 1, SLOT_SRC_SCALAR
},
322 {MAKE_SWZ3(Y
, Z
, X
), R300_ALU_ARGC_SRC0C_YZX
, 1, SLOT_SRC_VECTOR
},
323 {MAKE_SWZ3(Z
, X
, Y
), R300_ALU_ARGC_SRC0C_ZXY
, 1, SLOT_SRC_VECTOR
},
324 {MAKE_SWZ3(W
, Z
, Y
), R300_ALU_ARGC_SRC0CA_WZY
, 1, SLOT_SRC_BOTH
},
325 {MAKE_SWZ3(ONE
, ONE
, ONE
), R300_ALU_ARGC_ONE
, 0, 0},
326 {MAKE_SWZ3(ZERO
, ZERO
, ZERO
), R300_ALU_ARGC_ZERO
, 0, 0},
327 {MAKE_SWZ3(HALF
, HALF
, HALF
), R300_ALU_ARGC_HALF
, 0, 0},
328 {PFS_INVAL
, 0, 0, 0},
332 /* used during matching of non-native swizzles */
333 #define SWZ_X_MASK (7 << 0)
334 #define SWZ_Y_MASK (7 << 3)
335 #define SWZ_Z_MASK (7 << 6)
336 #define SWZ_W_MASK (7 << 9)
337 static const struct {
338 GLuint hash
; /* used to mask matching swizzle components */
339 int mask
; /* actual outmask */
340 int count
; /* count of components matched */
343 {SWZ_X_MASK
| SWZ_Y_MASK
| SWZ_Z_MASK
, 1 | 2 | 4, 3},
344 {SWZ_X_MASK
| SWZ_Y_MASK
, 1 | 2, 2},
345 {SWZ_X_MASK
| SWZ_Z_MASK
, 1 | 4, 2},
346 {SWZ_Y_MASK
| SWZ_Z_MASK
, 2 | 4, 2},
350 {PFS_INVAL
, PFS_INVAL
, PFS_INVAL
}
354 static const struct {
355 int base
; /* hw value of swizzle */
356 int stride
; /* difference between SRC0/1/2 */
360 {R300_ALU_ARGA_SRC0C_X
, 3, SLOT_SRC_VECTOR
},
361 {R300_ALU_ARGA_SRC0C_Y
, 3, SLOT_SRC_VECTOR
},
362 {R300_ALU_ARGA_SRC0C_Z
, 3, SLOT_SRC_VECTOR
},
363 {R300_ALU_ARGA_SRC0A
, 1, SLOT_SRC_SCALAR
},
364 {R300_ALU_ARGA_ZERO
, 0, 0},
365 {R300_ALU_ARGA_ONE
, 0, 0},
366 {R300_ALU_ARGA_HALF
, 0, 0}
370 /* boiler-plate reg, for convenience */
371 static const GLuint undef
= REG(REG_TYPE_TEMP
,
379 /* constant one source */
380 static const GLuint pfs_one
= REG(REG_TYPE_CONST
,
388 /* constant half source */
389 static const GLuint pfs_half
= REG(REG_TYPE_CONST
,
397 /* constant zero source */
398 static const GLuint pfs_zero
= REG(REG_TYPE_CONST
,
407 * Common functions prototypes
409 static void emit_arith(struct r300_pfs_compile_state
*cs
, int op
,
410 GLuint dest
, int mask
,
411 GLuint src0
, GLuint src1
, GLuint src2
, int flags
);
414 * Get an R300 temporary that can be written to in the given slot.
416 static int get_hw_temp(struct r300_pfs_compile_state
*cs
, int slot
)
421 for (r
= 0; r
< PFS_NUM_TEMP_REGS
; ++r
) {
422 if (cs
->hwtemps
[r
].free
>= 0 && cs
->hwtemps
[r
].free
<= slot
)
426 if (r
>= PFS_NUM_TEMP_REGS
) {
427 ERROR("Out of hardware temps\n");
430 // Reserved is used to avoid the following scenario:
431 // R300 temporary X is first assigned to Mesa temporary Y during vector ops
432 // R300 temporary X is then assigned to Mesa temporary Z for further vector ops
433 // Then scalar ops on Mesa temporary Z are emitted and move back in time
434 // to overwrite the value of temporary Y.
436 cs
->hwtemps
[r
].reserved
= cs
->hwtemps
[r
].free
;
437 cs
->hwtemps
[r
].free
= -1;
439 // Reset to some value that won't mess things up when the user
440 // tries to read from a temporary that hasn't been assigned a value yet.
441 // In the normal case, vector_valid and scalar_valid should be set to
442 // a sane value by the first emit that writes to this temporary.
443 cs
->hwtemps
[r
].vector_valid
= 0;
444 cs
->hwtemps
[r
].scalar_valid
= 0;
446 if (r
> code
->max_temp_idx
)
447 code
->max_temp_idx
= r
;
453 * Get an R300 temporary that will act as a TEX destination register.
455 static int get_hw_temp_tex(struct r300_pfs_compile_state
*cs
)
460 for (r
= 0; r
< PFS_NUM_TEMP_REGS
; ++r
) {
461 if (cs
->used_in_node
& (1 << r
))
464 // Note: Be very careful here
465 if (cs
->hwtemps
[r
].free
>= 0 && cs
->hwtemps
[r
].free
<= 0)
469 if (r
>= PFS_NUM_TEMP_REGS
)
470 return get_hw_temp(cs
, 0); /* Will cause an indirection */
472 cs
->hwtemps
[r
].reserved
= cs
->hwtemps
[r
].free
;
473 cs
->hwtemps
[r
].free
= -1;
475 // Reset to some value that won't mess things up when the user
476 // tries to read from a temporary that hasn't been assigned a value yet.
477 // In the normal case, vector_valid and scalar_valid should be set to
478 // a sane value by the first emit that writes to this temporary.
479 cs
->hwtemps
[r
].vector_valid
= cs
->nrslots
;
480 cs
->hwtemps
[r
].scalar_valid
= cs
->nrslots
;
482 if (r
> code
->max_temp_idx
)
483 code
->max_temp_idx
= r
;
489 * Mark the given hardware register as free.
491 static void free_hw_temp(struct r300_pfs_compile_state
*cs
, int idx
)
493 // Be very careful here. Consider sequences like
496 // The TEX instruction may be moved in front of the MAD instruction
497 // due to the way nodes work. We don't want to alias r1 and r4 in
499 // I'm certain the register allocation could be further sanitized,
500 // but it's tricky because of stuff that can happen inside emit_tex
502 cs
->hwtemps
[idx
].free
= cs
->nrslots
+ 1;
506 * Create a new Mesa temporary register.
508 static GLuint
get_temp_reg(struct r300_pfs_compile_state
*cs
)
514 index
= ffs(~cs
->temp_in_use
);
516 ERROR("Out of program temps\n");
520 cs
->temp_in_use
|= (1 << --index
);
521 cs
->temps
[index
].refcount
= 0xFFFFFFFF;
522 cs
->temps
[index
].reg
= -1;
524 REG_SET_TYPE(r
, REG_TYPE_TEMP
);
525 REG_SET_INDEX(r
, index
);
526 REG_SET_VALID(r
, GL_TRUE
);
531 * Free a Mesa temporary and the associated R300 temporary.
533 static void free_temp(struct r300_pfs_compile_state
*cs
, GLuint r
)
535 GLuint index
= REG_GET_INDEX(r
);
537 if (!(cs
->temp_in_use
& (1 << index
)))
540 if (REG_GET_TYPE(r
) == REG_TYPE_TEMP
) {
541 free_hw_temp(cs
, cs
->temps
[index
].reg
);
542 cs
->temps
[index
].reg
= -1;
543 cs
->temp_in_use
&= ~(1 << index
);
544 } else if (REG_GET_TYPE(r
) == REG_TYPE_INPUT
) {
545 free_hw_temp(cs
, cs
->inputs
[index
].reg
);
546 cs
->inputs
[index
].reg
= -1;
551 * Emit a hardware constant/parameter.
553 * \p cp Stable pointer to an array of 4 floats.
554 * The pointer must be stable in the sense that it remains to be valid
555 * and hold the contents of the constant/parameter throughout the lifetime
556 * of the fragment program (actually, up until the next time the fragment
557 * program is translated).
559 static GLuint
emit_const4fv(struct r300_pfs_compile_state
*cs
,
566 for (index
= 0; index
< code
->const_nr
; ++index
) {
567 if (code
->constant
[index
] == cp
)
571 if (index
>= code
->const_nr
) {
572 if (index
>= PFS_NUM_CONST_REGS
) {
573 ERROR("Out of hw constants!\n");
578 code
->constant
[index
] = cp
;
581 REG_SET_TYPE(reg
, REG_TYPE_CONST
);
582 REG_SET_INDEX(reg
, index
);
583 REG_SET_VALID(reg
, GL_TRUE
);
587 static inline GLuint
negate(GLuint r
)
594 /* Hack, to prevent clobbering sources used multiple times when
595 * emulating non-native instructions
597 static inline GLuint
keep(GLuint r
)
599 REG_SET_NO_USE(r
, GL_TRUE
);
603 static inline GLuint
absolute(GLuint r
)
609 static int swz_native(struct r300_pfs_compile_state
*cs
,
610 GLuint src
, GLuint
* r
, GLuint arbneg
)
614 /* Native swizzle, handle negation */
615 src
= (src
& ~REG_NEGS_MASK
) | (((arbneg
>> 3) & 1) << REG_NEGS_SHIFT
);
617 if ((arbneg
& 0x7) == 0x0) {
618 src
= src
& ~REG_NEGV_MASK
;
620 } else if ((arbneg
& 0x7) == 0x7) {
621 src
|= REG_NEGV_MASK
;
624 if (!REG_GET_VALID(*r
))
625 *r
= get_temp_reg(cs
);
626 src
|= REG_NEGV_MASK
;
629 *r
, arbneg
& 0x7, keep(src
), pfs_one
, pfs_zero
, 0);
630 src
= src
& ~REG_NEGV_MASK
;
634 (arbneg
^ 0x7) | WRITEMASK_W
,
635 src
, pfs_one
, pfs_zero
, 0);
641 static int swz_emit_partial(struct r300_pfs_compile_state
*cs
,
643 GLuint
* r
, int mask
, int mc
, GLuint arbneg
)
649 if (!REG_GET_VALID(*r
))
650 *r
= get_temp_reg(cs
);
652 /* A partial match, VSWZ/mask define what parts of the
653 * desired swizzle we match
655 if (mc
+ s_mask
[mask
].count
== 3) {
657 src
|= ((arbneg
>> 3) & 1) << REG_NEGS_SHIFT
;
660 tmp
= arbneg
& s_mask
[mask
].mask
;
662 tmp
= tmp
^ s_mask
[mask
].mask
;
667 arbneg
& s_mask
[mask
].mask
,
668 keep(src
) | REG_NEGV_MASK
,
669 pfs_one
, pfs_zero
, 0);
671 REG_SET_NO_USE(src
, GL_TRUE
);
673 REG_SET_NO_USE(src
, GL_FALSE
);
677 *r
, tmp
| wmask
, src
, pfs_one
, pfs_zero
, 0);
680 REG_SET_NO_USE(src
, GL_TRUE
);
682 REG_SET_NO_USE(src
, GL_FALSE
);
687 (arbneg
& s_mask
[mask
].mask
) | wmask
,
688 src
| REG_NEGV_MASK
, pfs_one
, pfs_zero
, 0);
692 REG_SET_NO_USE(src
, GL_TRUE
);
694 REG_SET_NO_USE(src
, GL_FALSE
);
696 emit_arith(cs
, PFS_OP_MAD
,
698 s_mask
[mask
].mask
| wmask
,
699 src
, pfs_one
, pfs_zero
, 0);
702 return s_mask
[mask
].count
;
705 static GLuint
do_swizzle(struct r300_pfs_compile_state
*cs
,
706 GLuint src
, GLuint arbswz
, GLuint arbneg
)
714 /* If swizzling from something without an XYZW native swizzle,
715 * emit result to a temp, and do new swizzle from the temp.
718 if (REG_GET_VSWZ(src
) != SWIZZLE_XYZ
|| REG_GET_SSWZ(src
) != SWIZZLE_W
) {
719 GLuint temp
= get_temp_reg(fp
);
722 temp
, WRITEMASK_XYZW
, src
, pfs_one
, pfs_zero
, 0);
727 if (REG_GET_VSWZ(src
) != SWIZZLE_XYZ
|| REG_GET_SSWZ(src
) != SWIZZLE_W
) {
729 (v_swiz
[REG_GET_VSWZ(src
)].
730 hash
& (SWZ_X_MASK
| SWZ_Y_MASK
| SWZ_Z_MASK
)) |
731 REG_GET_SSWZ(src
) << 9;
736 for (i
= 0; i
< 4; ++i
) {
737 offset
= GET_SWZ(arbswz
, i
);
740 (offset
<= 3) ? GET_SWZ(vsrcswz
,
745 arbswz
= newswz
& (SWZ_X_MASK
| SWZ_Y_MASK
| SWZ_Z_MASK
);
746 REG_SET_SSWZ(src
, GET_SWZ(newswz
, 3));
748 /* set scalar swizzling */
749 REG_SET_SSWZ(src
, GET_SWZ(arbswz
, 3));
753 vswz
= REG_GET_VSWZ(src
);
757 REG_SET_VSWZ(src
, vswz
);
758 chash
= v_swiz
[REG_GET_VSWZ(src
)].hash
&
761 if (chash
== (arbswz
& s_mask
[c_mask
].hash
)) {
762 if (s_mask
[c_mask
].count
== 3) {
763 v_match
+= swz_native(cs
,
766 v_match
+= swz_emit_partial(cs
,
777 /* Fill with something invalid.. all 0's was
778 * wrong before, matched SWIZZLE_X. So all
779 * 1's will be okay for now
781 arbswz
|= (PFS_INVAL
& s_mask
[c_mask
].hash
);
783 } while (v_swiz
[++vswz
].hash
!= PFS_INVAL
);
784 REG_SET_VSWZ(src
, SWIZZLE_XYZ
);
785 } while (s_mask
[++c_mask
].hash
!= PFS_INVAL
);
787 ERROR("should NEVER get here\n");
791 static GLuint
t_src(struct r300_pfs_compile_state
*cs
,
792 struct prog_src_register fpsrc
)
797 switch (fpsrc
.File
) {
798 case PROGRAM_TEMPORARY
:
799 REG_SET_INDEX(r
, fpsrc
.Index
);
800 REG_SET_VALID(r
, GL_TRUE
);
801 REG_SET_TYPE(r
, REG_TYPE_TEMP
);
804 REG_SET_INDEX(r
, fpsrc
.Index
);
805 REG_SET_VALID(r
, GL_TRUE
);
806 REG_SET_TYPE(r
, REG_TYPE_INPUT
);
808 case PROGRAM_LOCAL_PARAM
:
809 r
= emit_const4fv(cs
,
810 fp
->mesa_program
.Base
.LocalParams
[fpsrc
.
813 case PROGRAM_ENV_PARAM
:
814 r
= emit_const4fv(cs
,
815 cs
->compiler
->r300
->radeon
.glCtx
->FragmentProgram
.Parameters
[fpsrc
.Index
]);
817 case PROGRAM_STATE_VAR
:
818 case PROGRAM_NAMED_PARAM
:
819 case PROGRAM_CONSTANT
:
820 r
= emit_const4fv(cs
,
821 fp
->mesa_program
.Base
.Parameters
->
822 ParameterValues
[fpsrc
.Index
]);
824 case PROGRAM_BUILTIN
:
825 switch(fpsrc
.Swizzle
) {
826 case SWIZZLE_1111
: r
= pfs_one
; break;
827 case SWIZZLE_0000
: r
= pfs_zero
; break;
829 ERROR("bad PROGRAM_BUILTIN swizzle %u\n", fpsrc
.Swizzle
);
834 ERROR("unknown SrcReg->File %x\n", fpsrc
.File
);
838 /* no point swizzling ONE/ZERO/HALF constants... */
839 if (REG_GET_VSWZ(r
) < SWIZZLE_111
|| REG_GET_SSWZ(r
) < SWIZZLE_ZERO
)
840 r
= do_swizzle(cs
, r
, fpsrc
.Swizzle
, fpsrc
.NegateBase
);
844 static GLuint
t_scalar_src(struct r300_pfs_compile_state
*cs
,
845 struct prog_src_register fpsrc
)
847 struct prog_src_register src
= fpsrc
;
848 int sc
= GET_SWZ(fpsrc
.Swizzle
, 0); /* X */
850 src
.Swizzle
= ((sc
<< 0) | (sc
<< 3) | (sc
<< 6) | (sc
<< 9));
852 return t_src(cs
, src
);
855 static GLuint
t_dst(struct r300_pfs_compile_state
*cs
,
856 struct prog_dst_register dest
)
862 case PROGRAM_TEMPORARY
:
863 REG_SET_INDEX(r
, dest
.Index
);
864 REG_SET_VALID(r
, GL_TRUE
);
865 REG_SET_TYPE(r
, REG_TYPE_TEMP
);
868 REG_SET_TYPE(r
, REG_TYPE_OUTPUT
);
869 switch (dest
.Index
) {
870 case FRAG_RESULT_COLR
:
871 case FRAG_RESULT_DEPR
:
872 REG_SET_INDEX(r
, dest
.Index
);
873 REG_SET_VALID(r
, GL_TRUE
);
876 ERROR("Bad DstReg->Index 0x%x\n", dest
.Index
);
880 ERROR("Bad DstReg->File 0x%x\n", dest
.File
);
885 static int t_hw_src(struct r300_pfs_compile_state
*cs
, GLuint src
, GLboolean tex
)
889 int index
= REG_GET_INDEX(src
);
891 switch (REG_GET_TYPE(src
)) {
893 /* NOTE: if reg==-1 here, a source is being read that
894 * hasn't been written to. Undefined results.
896 if (cs
->temps
[index
].reg
== -1)
897 cs
->temps
[index
].reg
= get_hw_temp(cs
, cs
->nrslots
);
899 idx
= cs
->temps
[index
].reg
;
901 if (!REG_GET_NO_USE(src
) && (--cs
->temps
[index
].refcount
== 0))
905 idx
= cs
->inputs
[index
].reg
;
907 if (!REG_GET_NO_USE(src
) && (--cs
->inputs
[index
].refcount
== 0))
908 free_hw_temp(cs
, cs
->inputs
[index
].reg
);
911 return (index
| SRC_CONST
);
913 ERROR("Invalid type for source reg\n");
914 return (0 | SRC_CONST
);
918 cs
->used_in_node
|= (1 << idx
);
923 static int t_hw_dst(struct r300_pfs_compile_state
*cs
,
924 GLuint dest
, GLboolean tex
, int slot
)
928 GLuint index
= REG_GET_INDEX(dest
);
929 assert(REG_GET_VALID(dest
));
931 switch (REG_GET_TYPE(dest
)) {
933 if (cs
->temps
[REG_GET_INDEX(dest
)].reg
== -1) {
935 cs
->temps
[index
].reg
= get_hw_temp(cs
, slot
);
937 cs
->temps
[index
].reg
= get_hw_temp_tex(cs
);
940 idx
= cs
->temps
[index
].reg
;
942 if (!REG_GET_NO_USE(dest
) && (--cs
->temps
[index
].refcount
== 0))
945 cs
->dest_in_node
|= (1 << idx
);
946 cs
->used_in_node
|= (1 << idx
);
948 case REG_TYPE_OUTPUT
:
950 case FRAG_RESULT_COLR
:
951 code
->node
[code
->cur_node
].flags
|= R300_RGBA_OUT
;
953 case FRAG_RESULT_DEPR
:
954 fp
->WritesDepth
= GL_TRUE
;
955 code
->node
[code
->cur_node
].flags
|= R300_W_OUT
;
961 ERROR("invalid dest reg type %d\n", REG_GET_TYPE(dest
));
968 static void emit_nop(struct r300_pfs_compile_state
*cs
)
972 if (cs
->nrslots
>= PFS_MAX_ALU_INST
) {
973 ERROR("Out of ALU instruction slots\n");
977 code
->alu
.inst
[cs
->nrslots
].inst0
= NOP_INST0
;
978 code
->alu
.inst
[cs
->nrslots
].inst1
= NOP_INST1
;
979 code
->alu
.inst
[cs
->nrslots
].inst2
= NOP_INST2
;
980 code
->alu
.inst
[cs
->nrslots
].inst3
= NOP_INST3
;
984 static void emit_tex(struct r300_pfs_compile_state
*cs
,
985 struct prog_instruction
*fpi
, int opcode
)
988 GLuint coord
= t_src(cs
, fpi
->SrcReg
[0]);
991 int unit
= fpi
->TexSrcUnit
;
994 /* Ensure correct node indirection */
995 uin
= cs
->used_in_node
;
996 din
= cs
->dest_in_node
;
998 /* Resolve source/dest to hardware registers */
999 hwsrc
= t_hw_src(cs
, coord
, GL_TRUE
);
1001 if (opcode
!= R300_TEX_OP_KIL
) {
1002 dest
= t_dst(cs
, fpi
->DstReg
);
1005 t_hw_dst(cs
, dest
, GL_TRUE
,
1006 code
->node
[code
->cur_node
].alu_offset
);
1008 /* Use a temp that hasn't been used in this node, rather
1009 * than causing an indirection
1011 if (uin
& (1 << hwdest
)) {
1012 free_hw_temp(cs
, hwdest
);
1013 hwdest
= get_hw_temp_tex(cs
);
1014 cs
->temps
[REG_GET_INDEX(dest
)].reg
= hwdest
;
1021 /* Indirection if source has been written in this node, or if the
1022 * dest has been read/written in this node
1024 if ((REG_GET_TYPE(coord
) != REG_TYPE_CONST
&&
1025 (din
& (1 << hwsrc
))) || (uin
& (1 << hwdest
))) {
1027 /* Finish off current node */
1028 if (code
->node
[code
->cur_node
].alu_offset
== cs
->nrslots
)
1031 code
->node
[code
->cur_node
].alu_end
=
1032 cs
->nrslots
- code
->node
[code
->cur_node
].alu_offset
- 1;
1033 assert(code
->node
[code
->cur_node
].alu_end
>= 0);
1035 if (++code
->cur_node
>= PFS_MAX_TEX_INDIRECT
) {
1036 ERROR("too many levels of texture indirection\n");
1040 /* Start new node */
1041 code
->node
[code
->cur_node
].tex_offset
= code
->tex
.length
;
1042 code
->node
[code
->cur_node
].alu_offset
= cs
->nrslots
;
1043 code
->node
[code
->cur_node
].tex_end
= -1;
1044 code
->node
[code
->cur_node
].alu_end
= -1;
1045 code
->node
[code
->cur_node
].flags
= 0;
1046 cs
->used_in_node
= 0;
1047 cs
->dest_in_node
= 0;
1050 if (code
->cur_node
== 0)
1051 code
->first_node_has_tex
= 1;
1053 code
->tex
.inst
[code
->tex
.length
++] = 0 | (hwsrc
<< R300_SRC_ADDR_SHIFT
)
1054 | (hwdest
<< R300_DST_ADDR_SHIFT
)
1055 | (unit
<< R300_TEX_ID_SHIFT
)
1056 | (opcode
<< R300_TEX_INST_SHIFT
);
1058 cs
->dest_in_node
|= (1 << hwdest
);
1059 if (REG_GET_TYPE(coord
) != REG_TYPE_CONST
)
1060 cs
->used_in_node
|= (1 << hwsrc
);
1062 code
->node
[code
->cur_node
].tex_end
++;
1066 * Returns the first slot where we could possibly allow writing to dest,
1067 * according to register allocation.
1069 static int get_earliest_allowed_write(struct r300_pfs_compile_state
*cs
,
1070 GLuint dest
, int mask
)
1075 GLuint index
= REG_GET_INDEX(dest
);
1076 assert(REG_GET_VALID(dest
));
1078 switch (REG_GET_TYPE(dest
)) {
1080 if (cs
->temps
[index
].reg
== -1)
1083 idx
= cs
->temps
[index
].reg
;
1085 case REG_TYPE_OUTPUT
:
1088 ERROR("invalid dest reg type %d\n", REG_GET_TYPE(dest
));
1092 pos
= cs
->hwtemps
[idx
].reserved
;
1093 if (mask
& WRITEMASK_XYZ
) {
1094 if (pos
< cs
->hwtemps
[idx
].vector_lastread
)
1095 pos
= cs
->hwtemps
[idx
].vector_lastread
;
1097 if (mask
& WRITEMASK_W
) {
1098 if (pos
< cs
->hwtemps
[idx
].scalar_lastread
)
1099 pos
= cs
->hwtemps
[idx
].scalar_lastread
;
1106 * Allocates a slot for an ALU instruction that can consist of
1107 * a vertex part or a scalar part or both.
1109 * Sources from src (src[0] to src[argc-1]) are added to the slot in the
1110 * appropriate position (vector and/or scalar), and their positions are
1111 * recorded in the srcpos array.
1113 * This function emits instruction code for the source fetch and the
1114 * argument selection. It does not emit instruction code for the
1115 * opcode or the destination selection.
1117 * @return the index of the slot
1119 static int find_and_prepare_slot(struct r300_pfs_compile_state
*cs
,
1122 int argc
, GLuint
* src
, GLuint dest
, int mask
)
1135 // Determine instruction slots, whether sources are required on
1136 // vector or scalar side, and the smallest slot number where
1137 // all source registers are available
1140 used
|= SLOT_OP_VECTOR
;
1142 used
|= SLOT_OP_SCALAR
;
1144 pos
= get_earliest_allowed_write(cs
, dest
, mask
);
1146 if (code
->node
[code
->cur_node
].alu_offset
> pos
)
1147 pos
= code
->node
[code
->cur_node
].alu_offset
;
1148 for (i
= 0; i
< argc
; ++i
) {
1149 if (!REG_GET_BUILTIN(src
[i
])) {
1151 used
|= v_swiz
[REG_GET_VSWZ(src
[i
])].flags
<< i
;
1153 used
|= s_swiz
[REG_GET_SSWZ(src
[i
])].flags
<< i
;
1156 hwsrc
[i
] = t_hw_src(cs
, src
[i
], GL_FALSE
); /* Note: sideeffects wrt refcounting! */
1157 regnr
= hwsrc
[i
] & 31;
1159 if (REG_GET_TYPE(src
[i
]) == REG_TYPE_TEMP
) {
1160 if (used
& (SLOT_SRC_VECTOR
<< i
)) {
1161 if (cs
->hwtemps
[regnr
].vector_valid
> pos
)
1162 pos
= cs
->hwtemps
[regnr
].vector_valid
;
1164 if (used
& (SLOT_SRC_SCALAR
<< i
)) {
1165 if (cs
->hwtemps
[regnr
].scalar_valid
> pos
)
1166 pos
= cs
->hwtemps
[regnr
].scalar_valid
;
1171 // Find a slot that fits
1173 if (cs
->slot
[pos
].used
& used
& SLOT_OP_BOTH
)
1176 if (pos
>= cs
->nrslots
) {
1177 if (cs
->nrslots
>= PFS_MAX_ALU_INST
) {
1178 ERROR("Out of ALU instruction slots\n");
1182 code
->alu
.inst
[pos
].inst0
= NOP_INST0
;
1183 code
->alu
.inst
[pos
].inst1
= NOP_INST1
;
1184 code
->alu
.inst
[pos
].inst2
= NOP_INST2
;
1185 code
->alu
.inst
[pos
].inst3
= NOP_INST3
;
1189 // Note: When we need both parts (vector and scalar) of a source,
1190 // we always try to put them into the same position. This makes the
1191 // code easier to read, and it is optimal (i.e. one doesn't gain
1192 // anything by splitting the parts).
1193 // It also avoids headaches with swizzles that access both parts (i.e WXY)
1194 tempused
= cs
->slot
[pos
].used
;
1195 for (i
= 0; i
< 3; ++i
) {
1196 tempvsrc
[i
] = cs
->slot
[pos
].vsrc
[i
];
1197 tempssrc
[i
] = cs
->slot
[pos
].ssrc
[i
];
1200 for (i
= 0; i
< argc
; ++i
) {
1201 int flags
= (used
>> i
) & SLOT_SRC_BOTH
;
1208 for (j
= 0; j
< 3; ++j
) {
1209 if ((tempused
>> j
) & flags
& SLOT_SRC_VECTOR
) {
1210 if (tempvsrc
[j
] != hwsrc
[i
])
1214 if ((tempused
>> j
) & flags
& SLOT_SRC_SCALAR
) {
1215 if (tempssrc
[j
] != hwsrc
[i
])
1226 tempused
|= flags
<< j
;
1227 if (flags
& SLOT_SRC_VECTOR
)
1228 tempvsrc
[j
] = hwsrc
[i
];
1229 if (flags
& SLOT_SRC_SCALAR
)
1230 tempssrc
[j
] = hwsrc
[i
];
1237 // Found a slot, reserve it
1238 cs
->slot
[pos
].used
= tempused
| (used
& SLOT_OP_BOTH
);
1239 for (i
= 0; i
< 3; ++i
) {
1240 cs
->slot
[pos
].vsrc
[i
] = tempvsrc
[i
];
1241 cs
->slot
[pos
].ssrc
[i
] = tempssrc
[i
];
1244 for (i
= 0; i
< argc
; ++i
) {
1245 if (REG_GET_TYPE(src
[i
]) == REG_TYPE_TEMP
) {
1246 int regnr
= hwsrc
[i
] & 31;
1248 if (used
& (SLOT_SRC_VECTOR
<< i
)) {
1249 if (cs
->hwtemps
[regnr
].vector_lastread
< pos
)
1250 cs
->hwtemps
[regnr
].vector_lastread
=
1253 if (used
& (SLOT_SRC_SCALAR
<< i
)) {
1254 if (cs
->hwtemps
[regnr
].scalar_lastread
< pos
)
1255 cs
->hwtemps
[regnr
].scalar_lastread
=
1261 // Emit the source fetch code
1262 code
->alu
.inst
[pos
].inst1
&= ~R300_ALU_SRC_MASK
;
1263 code
->alu
.inst
[pos
].inst1
|=
1264 ((cs
->slot
[pos
].vsrc
[0] << R300_ALU_SRC0C_SHIFT
) |
1265 (cs
->slot
[pos
].vsrc
[1] << R300_ALU_SRC1C_SHIFT
) |
1266 (cs
->slot
[pos
].vsrc
[2] << R300_ALU_SRC2C_SHIFT
));
1268 code
->alu
.inst
[pos
].inst3
&= ~R300_ALU_SRC_MASK
;
1269 code
->alu
.inst
[pos
].inst3
|=
1270 ((cs
->slot
[pos
].ssrc
[0] << R300_ALU_SRC0A_SHIFT
) |
1271 (cs
->slot
[pos
].ssrc
[1] << R300_ALU_SRC1A_SHIFT
) |
1272 (cs
->slot
[pos
].ssrc
[2] << R300_ALU_SRC2A_SHIFT
));
1274 // Emit the argument selection code
1278 for (i
= 0; i
< 3; ++i
) {
1280 swz
[i
] = (v_swiz
[REG_GET_VSWZ(src
[i
])].base
+
1282 v_swiz
[REG_GET_VSWZ(src
[i
])].
1283 stride
)) | ((src
[i
] & REG_NEGV_MASK
)
1284 ? ARG_NEG
: 0) | ((src
[i
]
1291 swz
[i
] = R300_ALU_ARGC_ZERO
;
1295 code
->alu
.inst
[pos
].inst0
&=
1296 ~(R300_ALU_ARG0C_MASK
| R300_ALU_ARG1C_MASK
|
1297 R300_ALU_ARG2C_MASK
);
1298 code
->alu
.inst
[pos
].inst0
|=
1299 (swz
[0] << R300_ALU_ARG0C_SHIFT
) | (swz
[1] <<
1300 R300_ALU_ARG1C_SHIFT
)
1301 | (swz
[2] << R300_ALU_ARG2C_SHIFT
);
1307 for (i
= 0; i
< 3; ++i
) {
1309 swz
[i
] = (s_swiz
[REG_GET_SSWZ(src
[i
])].base
+
1311 s_swiz
[REG_GET_SSWZ(src
[i
])].
1312 stride
)) | ((src
[i
] & REG_NEGV_MASK
)
1313 ? ARG_NEG
: 0) | ((src
[i
]
1320 swz
[i
] = R300_ALU_ARGA_ZERO
;
1324 code
->alu
.inst
[pos
].inst2
&=
1325 ~(R300_ALU_ARG0A_MASK
| R300_ALU_ARG1A_MASK
|
1326 R300_ALU_ARG2A_MASK
);
1327 code
->alu
.inst
[pos
].inst2
|=
1328 (swz
[0] << R300_ALU_ARG0A_SHIFT
) | (swz
[1] <<
1329 R300_ALU_ARG1A_SHIFT
)
1330 | (swz
[2] << R300_ALU_ARG2A_SHIFT
);
1337 * Append an ALU instruction to the instruction list.
1339 static void emit_arith(struct r300_pfs_compile_state
*cs
,
1343 GLuint src0
, GLuint src1
, GLuint src2
, int flags
)
1346 GLuint src
[3] = { src0
, src1
, src2
};
1348 GLboolean emit_vop
, emit_sop
;
1352 vop
= r300_fpop
[op
].v_op
;
1353 sop
= r300_fpop
[op
].s_op
;
1354 argc
= r300_fpop
[op
].argc
;
1356 if (REG_GET_TYPE(dest
) == REG_TYPE_OUTPUT
&&
1357 REG_GET_INDEX(dest
) == FRAG_RESULT_DEPR
) {
1358 if (mask
& WRITEMASK_Z
) {
1365 emit_vop
= GL_FALSE
;
1366 emit_sop
= GL_FALSE
;
1367 if ((mask
& WRITEMASK_XYZ
) || vop
== R300_ALU_OUTC_DP3
)
1369 if ((mask
& WRITEMASK_W
) || vop
== R300_ALU_OUTC_REPL_ALPHA
)
1373 find_and_prepare_slot(cs
, emit_vop
, emit_sop
, argc
, src
, dest
,
1378 hwdest
= t_hw_dst(cs
, dest
, GL_FALSE
, pos
); /* Note: Side effects wrt register allocation */
1380 if (flags
& PFS_FLAG_SAT
) {
1381 vop
|= R300_ALU_OUTC_CLAMP
;
1382 sop
|= R300_ALU_OUTA_CLAMP
;
1385 /* Throw the pieces together and get ALU/1 */
1387 code
->alu
.inst
[pos
].inst0
|= vop
;
1389 code
->alu
.inst
[pos
].inst1
|= hwdest
<< R300_ALU_DSTC_SHIFT
;
1391 if (REG_GET_TYPE(dest
) == REG_TYPE_OUTPUT
) {
1392 if (REG_GET_INDEX(dest
) == FRAG_RESULT_COLR
) {
1393 code
->alu
.inst
[pos
].inst1
|=
1394 (mask
& WRITEMASK_XYZ
) <<
1395 R300_ALU_DSTC_OUTPUT_MASK_SHIFT
;
1399 code
->alu
.inst
[pos
].inst1
|=
1400 (mask
& WRITEMASK_XYZ
) <<
1401 R300_ALU_DSTC_REG_MASK_SHIFT
;
1403 cs
->hwtemps
[hwdest
].vector_valid
= pos
+ 1;
1409 code
->alu
.inst
[pos
].inst2
|= sop
;
1411 if (mask
& WRITEMASK_W
) {
1412 if (REG_GET_TYPE(dest
) == REG_TYPE_OUTPUT
) {
1413 if (REG_GET_INDEX(dest
) == FRAG_RESULT_COLR
) {
1414 code
->alu
.inst
[pos
].inst3
|=
1415 (hwdest
<< R300_ALU_DSTA_SHIFT
) |
1416 R300_ALU_DSTA_OUTPUT
;
1417 } else if (REG_GET_INDEX(dest
) ==
1419 code
->alu
.inst
[pos
].inst3
|=
1420 R300_ALU_DSTA_DEPTH
;
1424 code
->alu
.inst
[pos
].inst3
|=
1425 (hwdest
<< R300_ALU_DSTA_SHIFT
) |
1428 cs
->hwtemps
[hwdest
].scalar_valid
= pos
+ 1;
1436 static GLfloat SinCosConsts
[2][4] = {
1438 1.273239545, // 4/PI
1439 -0.405284735, // -4/(PI*PI)
1446 0.159154943, // 1/(2*PI)
1452 * Emit a LIT instruction.
1453 * \p flags may be PFS_FLAG_SAT
1455 * Definition of LIT (from ARB_fragment_program):
1456 * tmp = VectorLoad(op0);
1457 * if (tmp.x < 0) tmp.x = 0;
1458 * if (tmp.y < 0) tmp.y = 0;
1459 * if (tmp.w < -(128.0-epsilon)) tmp.w = -(128.0-epsilon);
1460 * else if (tmp.w > 128-epsilon) tmp.w = 128-epsilon;
1463 * result.z = (tmp.x > 0) ? RoughApproxPower(tmp.y, tmp.w) : 0.0;
1466 * The longest path of computation is the one leading to result.z,
1467 * consisting of 5 operations. This implementation of LIT takes
1468 * 5 slots. So unless there's some special undocumented opcode,
1469 * this implementation is potentially optimal. Unfortunately,
1470 * emit_arith is a bit too conservative because it doesn't understand
1471 * partial writes to the vector component.
1473 static const GLfloat LitConst
[4] =
1474 { 127.999999, 127.999999, 127.999999, -127.999999 };
1476 static void emit_lit(struct r300_pfs_compile_state
*cs
,
1477 GLuint dest
, int mask
, GLuint src
, int flags
)
1484 cnst
= emit_const4fv(cs
, LitConst
);
1487 if ((mask
& WRITEMASK_XYZW
) != WRITEMASK_XYZW
) {
1489 } else if (REG_GET_TYPE(dest
) == REG_TYPE_OUTPUT
) {
1490 // LIT is typically followed by DP3/DP4, so there's no point
1491 // in creating special code for this case
1495 if (needTemporary
) {
1496 temp
= keep(get_temp_reg(cs
));
1501 // Note: The order of emit_arith inside the slots is relevant,
1502 // because emit_arith only looks at scalar vs. vector when resolving
1503 // dependencies, and it does not consider individual vector components,
1504 // so swizzling between the two parts can create fake dependencies.
1507 emit_arith(cs
, PFS_OP_MAX
, temp
, WRITEMASK_XY
,
1508 keep(src
), pfs_zero
, undef
, 0);
1509 emit_arith(cs
, PFS_OP_MAX
, temp
, WRITEMASK_W
, src
, cnst
, undef
, 0);
1512 emit_arith(cs
, PFS_OP_MIN
, temp
, WRITEMASK_Z
,
1513 swizzle(temp
, W
, W
, W
, W
), cnst
, undef
, 0);
1514 emit_arith(cs
, PFS_OP_LG2
, temp
, WRITEMASK_W
,
1515 swizzle(temp
, Y
, Y
, Y
, Y
), undef
, undef
, 0);
1518 // If desired, we saturate the y result here.
1519 // This does not affect the use as a condition variable in the CMP later
1520 emit_arith(cs
, PFS_OP_MAD
, temp
, WRITEMASK_W
,
1521 temp
, swizzle(temp
, Z
, Z
, Z
, Z
), pfs_zero
, 0);
1522 emit_arith(cs
, PFS_OP_MAD
, temp
, WRITEMASK_Y
,
1523 swizzle(temp
, X
, X
, X
, X
), pfs_one
, pfs_zero
, flags
);
1526 emit_arith(cs
, PFS_OP_MAD
, temp
, WRITEMASK_X
,
1527 pfs_one
, pfs_one
, pfs_zero
, 0);
1528 emit_arith(cs
, PFS_OP_EX2
, temp
, WRITEMASK_W
, temp
, undef
, undef
, 0);
1531 emit_arith(cs
, PFS_OP_CMP
, temp
, WRITEMASK_Z
,
1532 pfs_zero
, swizzle(temp
, W
, W
, W
, W
),
1533 negate(swizzle(temp
, Y
, Y
, Y
, Y
)), flags
);
1534 emit_arith(cs
, PFS_OP_MAD
, temp
, WRITEMASK_W
, pfs_one
, pfs_one
,
1537 if (needTemporary
) {
1538 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1539 temp
, pfs_one
, pfs_zero
, flags
);
1540 free_temp(cs
, temp
);
1542 // Decrease refcount of the destination
1543 t_hw_dst(cs
, dest
, GL_FALSE
, cs
->nrslots
);
1547 static void emit_instruction(struct r300_pfs_compile_state
*cs
, struct prog_instruction
*fpi
)
1550 GLuint src
[3], dest
, temp
[2];
1551 int flags
, mask
= 0;
1554 if (fpi
->SaturateMode
== SATURATE_ZERO_ONE
)
1555 flags
= PFS_FLAG_SAT
;
1559 if (fpi
->Opcode
!= OPCODE_KIL
) {
1560 dest
= t_dst(cs
, fpi
->DstReg
);
1561 mask
= fpi
->DstReg
.WriteMask
;
1564 switch (fpi
->Opcode
) {
1566 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1567 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1568 absolute(src
[0]), pfs_one
, pfs_zero
, flags
);
1571 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1572 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1573 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1574 src
[0], pfs_one
, src
[1], flags
);
1577 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1578 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1579 src
[2] = t_src(cs
, fpi
->SrcReg
[2]);
1580 /* ARB_f_p - if src0.c < 0.0 ? src1.c : src2.c
1581 * r300 - if src2.c < 0.0 ? src1.c : src0.c
1583 emit_arith(cs
, PFS_OP_CMP
, dest
, mask
,
1584 src
[2], src
[1], src
[0], flags
);
1588 * cos using a parabola (see SIN):
1590 * x = (x/(2*PI))+0.75
1595 temp
[0] = get_temp_reg(cs
);
1596 const_sin
[0] = emit_const4fv(cs
, SinCosConsts
[0]);
1597 const_sin
[1] = emit_const4fv(cs
, SinCosConsts
[1]);
1598 src
[0] = t_scalar_src(cs
, fpi
->SrcReg
[0]);
1600 /* add 0.5*PI and do range reduction */
1602 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_X
,
1603 swizzle(src
[0], X
, X
, X
, X
),
1604 swizzle(const_sin
[1], Z
, Z
, Z
, Z
),
1605 swizzle(const_sin
[1], X
, X
, X
, X
), 0);
1607 emit_arith(cs
, PFS_OP_FRC
, temp
[0], WRITEMASK_X
,
1608 swizzle(temp
[0], X
, X
, X
, X
),
1611 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_Z
, swizzle(temp
[0], X
, X
, X
, X
), swizzle(const_sin
[1], W
, W
, W
, W
), //2*PI
1612 negate(swizzle(const_sin
[0], Z
, Z
, Z
, Z
)), //-PI
1617 emit_arith(cs
, PFS_OP_MAD
, temp
[0],
1618 WRITEMASK_X
| WRITEMASK_Y
, swizzle(temp
[0],
1621 const_sin
[0], pfs_zero
, 0);
1623 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_X
,
1624 swizzle(temp
[0], Y
, Y
, Y
, Y
),
1625 absolute(swizzle(temp
[0], Z
, Z
, Z
, Z
)),
1626 swizzle(temp
[0], X
, X
, X
, X
), 0);
1628 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_Y
,
1629 swizzle(temp
[0], X
, X
, X
, X
),
1630 absolute(swizzle(temp
[0], X
, X
, X
, X
)),
1631 negate(swizzle(temp
[0], X
, X
, X
, X
)), 0);
1633 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1634 swizzle(temp
[0], Y
, Y
, Y
, Y
),
1635 swizzle(const_sin
[0], W
, W
, W
, W
),
1636 swizzle(temp
[0], X
, X
, X
, X
), flags
);
1638 free_temp(cs
, temp
[0]);
1641 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1642 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1643 emit_arith(cs
, PFS_OP_DP3
, dest
, mask
,
1644 src
[0], src
[1], undef
, flags
);
1647 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1648 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1649 emit_arith(cs
, PFS_OP_DP4
, dest
, mask
,
1650 src
[0], src
[1], undef
, flags
);
1653 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1654 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1655 /* src0.xyz1 -> temp
1656 * DP4 dest, temp, src1
1658 emit_arith(cs
, PFS_OP_DP4
, dest
, mask
,
1659 swizzle(src
[0], X
, Y
, Z
, ONE
), src
[1],
1663 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1664 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1665 /* dest.y = src0.y * src1.y */
1666 if (mask
& WRITEMASK_Y
)
1667 emit_arith(cs
, PFS_OP_MAD
, dest
, WRITEMASK_Y
,
1668 keep(src
[0]), keep(src
[1]),
1670 /* dest.z = src0.z */
1671 if (mask
& WRITEMASK_Z
)
1672 emit_arith(cs
, PFS_OP_MAD
, dest
, WRITEMASK_Z
,
1673 src
[0], pfs_one
, pfs_zero
, flags
);
1675 * result.w = src1.w */
1676 if (mask
& WRITEMASK_XW
) {
1677 REG_SET_VSWZ(src
[1], SWIZZLE_111
); /*Cheat */
1678 emit_arith(cs
, PFS_OP_MAD
, dest
,
1679 mask
& WRITEMASK_XW
,
1680 src
[1], pfs_one
, pfs_zero
, flags
);
1684 src
[0] = t_scalar_src(cs
, fpi
->SrcReg
[0]);
1685 emit_arith(cs
, PFS_OP_EX2
, dest
, mask
,
1686 src
[0], undef
, undef
, flags
);
1689 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1690 temp
[0] = get_temp_reg(cs
);
1692 * MAD dest, src0, 1.0, -temp
1694 emit_arith(cs
, PFS_OP_FRC
, temp
[0], mask
,
1695 keep(src
[0]), undef
, undef
, 0);
1696 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1697 src
[0], pfs_one
, negate(temp
[0]), flags
);
1698 free_temp(cs
, temp
[0]);
1701 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1702 emit_arith(cs
, PFS_OP_FRC
, dest
, mask
,
1703 src
[0], undef
, undef
, flags
);
1706 emit_tex(cs
, fpi
, R300_TEX_OP_KIL
);
1709 src
[0] = t_scalar_src(cs
, fpi
->SrcReg
[0]);
1710 emit_arith(cs
, PFS_OP_LG2
, dest
, mask
,
1711 src
[0], undef
, undef
, flags
);
1714 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1715 emit_lit(cs
, dest
, mask
, src
[0], flags
);
1718 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1719 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1720 src
[2] = t_src(cs
, fpi
->SrcReg
[2]);
1721 /* result = tmp0tmp1 + (1 - tmp0)tmp2
1722 * = tmp0tmp1 + tmp2 + (-tmp0)tmp2
1723 * MAD temp, -tmp0, tmp2, tmp2
1724 * MAD result, tmp0, tmp1, temp
1726 temp
[0] = get_temp_reg(cs
);
1727 emit_arith(cs
, PFS_OP_MAD
, temp
[0], mask
,
1728 negate(keep(src
[0])), keep(src
[2]), src
[2],
1730 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1731 src
[0], src
[1], temp
[0], flags
);
1732 free_temp(cs
, temp
[0]);
1735 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1736 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1737 src
[2] = t_src(cs
, fpi
->SrcReg
[2]);
1738 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1739 src
[0], src
[1], src
[2], flags
);
1742 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1743 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1744 emit_arith(cs
, PFS_OP_MAX
, dest
, mask
,
1745 src
[0], src
[1], undef
, flags
);
1748 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1749 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1750 emit_arith(cs
, PFS_OP_MIN
, dest
, mask
,
1751 src
[0], src
[1], undef
, flags
);
1755 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1756 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1757 src
[0], pfs_one
, pfs_zero
, flags
);
1760 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1761 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1762 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1763 src
[0], src
[1], pfs_zero
, flags
);
1766 src
[0] = t_scalar_src(cs
, fpi
->SrcReg
[0]);
1767 src
[1] = t_scalar_src(cs
, fpi
->SrcReg
[1]);
1768 temp
[0] = get_temp_reg(cs
);
1769 emit_arith(cs
, PFS_OP_LG2
, temp
[0], WRITEMASK_W
,
1770 src
[0], undef
, undef
, 0);
1771 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_W
,
1772 temp
[0], src
[1], pfs_zero
, 0);
1773 emit_arith(cs
, PFS_OP_EX2
, dest
, fpi
->DstReg
.WriteMask
,
1774 temp
[0], undef
, undef
, 0);
1775 free_temp(cs
, temp
[0]);
1778 src
[0] = t_scalar_src(cs
, fpi
->SrcReg
[0]);
1779 emit_arith(cs
, PFS_OP_RCP
, dest
, mask
,
1780 src
[0], undef
, undef
, flags
);
1783 src
[0] = t_scalar_src(cs
, fpi
->SrcReg
[0]);
1784 emit_arith(cs
, PFS_OP_RSQ
, dest
, mask
,
1785 absolute(src
[0]), pfs_zero
, pfs_zero
, flags
);
1789 * scs using a parabola :
1791 * result.x = sin(-abs(x)+0.5*PI) (cos)
1792 * result.y = sin(x) (sin)
1795 temp
[0] = get_temp_reg(cs
);
1796 temp
[1] = get_temp_reg(cs
);
1797 const_sin
[0] = emit_const4fv(cs
, SinCosConsts
[0]);
1798 const_sin
[1] = emit_const4fv(cs
, SinCosConsts
[1]);
1799 src
[0] = t_scalar_src(cs
, fpi
->SrcReg
[0]);
1801 /* x = -abs(x)+0.5*PI */
1802 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_Z
, swizzle(const_sin
[0], Z
, Z
, Z
, Z
), //PI
1805 (swizzle(keep(src
[0]), X
, X
, X
, X
))),
1809 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_W
,
1810 swizzle(const_sin
[0], Y
, Y
, Y
, Y
),
1811 swizzle(keep(src
[0]), X
, X
, X
, X
),
1814 /* B*x, C*x (cos) */
1815 emit_arith(cs
, PFS_OP_MAD
, temp
[0],
1816 WRITEMASK_X
| WRITEMASK_Y
, swizzle(temp
[0],
1819 const_sin
[0], pfs_zero
, 0);
1822 emit_arith(cs
, PFS_OP_MAD
, temp
[1], WRITEMASK_W
,
1823 swizzle(const_sin
[0], X
, X
, X
, X
),
1824 keep(src
[0]), pfs_zero
, 0);
1826 /* y = B*x + C*x*abs(x) (sin) */
1827 emit_arith(cs
, PFS_OP_MAD
, temp
[1], WRITEMASK_Z
,
1829 swizzle(temp
[0], W
, W
, W
, W
),
1830 swizzle(temp
[1], W
, W
, W
, W
), 0);
1832 /* y = B*x + C*x*abs(x) (cos) */
1833 emit_arith(cs
, PFS_OP_MAD
, temp
[1], WRITEMASK_W
,
1834 swizzle(temp
[0], Y
, Y
, Y
, Y
),
1835 absolute(swizzle(temp
[0], Z
, Z
, Z
, Z
)),
1836 swizzle(temp
[0], X
, X
, X
, X
), 0);
1838 /* y*abs(y) - y (cos), y*abs(y) - y (sin) */
1839 emit_arith(cs
, PFS_OP_MAD
, temp
[0],
1840 WRITEMASK_X
| WRITEMASK_Y
, swizzle(temp
[1],
1843 absolute(swizzle(temp
[1], W
, Z
, Y
, X
)),
1844 negate(swizzle(temp
[1], W
, Z
, Y
, X
)), 0);
1846 /* dest.xy = mad(temp.xy, P, temp2.wz) */
1847 emit_arith(cs
, PFS_OP_MAD
, dest
,
1848 mask
& (WRITEMASK_X
| WRITEMASK_Y
), temp
[0],
1849 swizzle(const_sin
[0], W
, W
, W
, W
),
1850 swizzle(temp
[1], W
, Z
, Y
, X
), flags
);
1852 free_temp(cs
, temp
[0]);
1853 free_temp(cs
, temp
[1]);
1856 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1857 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1858 temp
[0] = get_temp_reg(cs
);
1859 /* temp = src0 - src1
1860 * dest.c = (temp.c < 0.0) ? 0 : 1
1862 emit_arith(cs
, PFS_OP_MAD
, temp
[0], mask
,
1863 src
[0], pfs_one
, negate(src
[1]), 0);
1864 emit_arith(cs
, PFS_OP_CMP
, dest
, mask
,
1865 pfs_one
, pfs_zero
, temp
[0], 0);
1866 free_temp(cs
, temp
[0]);
1871 * sin(x) = 4/pi * x + -4/(pi*pi) * x * abs(x)
1872 * extra precision is obtained by weighting against
1876 temp
[0] = get_temp_reg(cs
);
1877 const_sin
[0] = emit_const4fv(cs
, SinCosConsts
[0]);
1878 const_sin
[1] = emit_const4fv(cs
, SinCosConsts
[1]);
1879 src
[0] = t_scalar_src(cs
, fpi
->SrcReg
[0]);
1881 /* do range reduction */
1883 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_X
,
1884 swizzle(keep(src
[0]), X
, X
, X
, X
),
1885 swizzle(const_sin
[1], Z
, Z
, Z
, Z
),
1888 emit_arith(cs
, PFS_OP_FRC
, temp
[0], WRITEMASK_X
,
1889 swizzle(temp
[0], X
, X
, X
, X
),
1892 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_Z
, swizzle(temp
[0], X
, X
, X
, X
), swizzle(const_sin
[1], W
, W
, W
, W
), //2*PI
1893 negate(swizzle(const_sin
[0], Z
, Z
, Z
, Z
)), //PI
1898 emit_arith(cs
, PFS_OP_MAD
, temp
[0],
1899 WRITEMASK_X
| WRITEMASK_Y
, swizzle(temp
[0],
1902 const_sin
[0], pfs_zero
, 0);
1904 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_X
,
1905 swizzle(temp
[0], Y
, Y
, Y
, Y
),
1906 absolute(swizzle(temp
[0], Z
, Z
, Z
, Z
)),
1907 swizzle(temp
[0], X
, X
, X
, X
), 0);
1909 emit_arith(cs
, PFS_OP_MAD
, temp
[0], WRITEMASK_Y
,
1910 swizzle(temp
[0], X
, X
, X
, X
),
1911 absolute(swizzle(temp
[0], X
, X
, X
, X
)),
1912 negate(swizzle(temp
[0], X
, X
, X
, X
)), 0);
1914 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1915 swizzle(temp
[0], Y
, Y
, Y
, Y
),
1916 swizzle(const_sin
[0], W
, W
, W
, W
),
1917 swizzle(temp
[0], X
, X
, X
, X
), flags
);
1919 free_temp(cs
, temp
[0]);
1922 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1923 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1924 temp
[0] = get_temp_reg(cs
);
1925 /* temp = src0 - src1
1926 * dest.c = (temp.c < 0.0) ? 1 : 0
1928 emit_arith(cs
, PFS_OP_MAD
, temp
[0], mask
,
1929 src
[0], pfs_one
, negate(src
[1]), 0);
1930 emit_arith(cs
, PFS_OP_CMP
, dest
, mask
,
1931 pfs_zero
, pfs_one
, temp
[0], 0);
1932 free_temp(cs
, temp
[0]);
1935 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1936 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1937 emit_arith(cs
, PFS_OP_MAD
, dest
, mask
,
1938 src
[0], pfs_one
, negate(src
[1]), flags
);
1941 emit_tex(cs
, fpi
, R300_TEX_OP_LD
);
1944 emit_tex(cs
, fpi
, R300_TEX_OP_TXB
);
1947 emit_tex(cs
, fpi
, R300_TEX_OP_TXP
);
1950 src
[0] = t_src(cs
, fpi
->SrcReg
[0]);
1951 src
[1] = t_src(cs
, fpi
->SrcReg
[1]);
1952 temp
[0] = get_temp_reg(cs
);
1953 /* temp = src0.zxy * src1.yzx */
1954 emit_arith(cs
, PFS_OP_MAD
, temp
[0],
1955 WRITEMASK_XYZ
, swizzle(keep(src
[0]),
1957 swizzle(keep(src
[1]), Y
, Z
, X
, W
),
1959 /* dest.xyz = src0.yzx * src1.zxy - temp
1960 * dest.w = undefined
1962 emit_arith(cs
, PFS_OP_MAD
, dest
,
1963 mask
& WRITEMASK_XYZ
, swizzle(src
[0],
1966 swizzle(src
[1], Z
, X
, Y
, W
),
1967 negate(temp
[0]), flags
);
1969 free_temp(cs
, temp
[0]);
1973 ERROR("unknown fpi->Opcode %d\n", fpi
->Opcode
);
1978 static GLboolean
parse_program(struct r300_pfs_compile_state
*cs
)
1983 for (clauseidx
= 0; clauseidx
< cs
->compiler
->compiler
.NumClauses
; ++clauseidx
) {
1984 struct radeon_clause
* clause
= &cs
->compiler
->compiler
.Clauses
[clauseidx
];
1987 for(ip
= 0; ip
< clause
->NumInstructions
; ++ip
) {
1988 emit_instruction(cs
, clause
->Instructions
+ ip
);
1999 /* - Init structures
2000 * - Determine what hwregs each input corresponds to
2002 static void init_program(struct r300_pfs_compile_state
*cs
)
2005 struct gl_fragment_program
*mp
= &fp
->mesa_program
;
2006 GLuint InputsRead
= mp
->Base
.InputsRead
;
2007 GLuint temps_used
= 0; /* for fp->temps[] */
2010 /* New compile, reset tracking data */
2012 driQueryOptioni(&cs
->compiler
->r300
->radeon
.optionCache
, "fp_optimization");
2013 fp
->translated
= GL_FALSE
;
2014 fp
->error
= GL_FALSE
;
2015 fp
->WritesDepth
= GL_FALSE
;
2016 code
->tex
.length
= 0;
2018 code
->first_node_has_tex
= 0;
2020 code
->max_temp_idx
= 0;
2021 code
->node
[0].alu_end
= -1;
2022 code
->node
[0].tex_end
= -1;
2024 for (i
= 0; i
< PFS_MAX_ALU_INST
; i
++) {
2025 for (j
= 0; j
< 3; j
++) {
2026 cs
->slot
[i
].vsrc
[j
] = SRC_CONST
;
2027 cs
->slot
[i
].ssrc
[j
] = SRC_CONST
;
2031 /* Work out what temps the Mesa inputs correspond to, this must match
2032 * what setup_rs_unit does, which shouldn't be a problem as rs_unit
2033 * configures itself based on the fragprog's InputsRead
2035 * NOTE: this depends on get_hw_temp() allocating registers in order,
2036 * starting from register 0.
2039 /* Texcoords come first */
2040 for (i
= 0; i
< cs
->compiler
->r300
->radeon
.glCtx
->Const
.MaxTextureUnits
; i
++) {
2041 if (InputsRead
& (FRAG_BIT_TEX0
<< i
)) {
2042 cs
->inputs
[FRAG_ATTRIB_TEX0
+ i
].refcount
= 0;
2043 cs
->inputs
[FRAG_ATTRIB_TEX0
+ i
].reg
=
2047 InputsRead
&= ~FRAG_BITS_TEX_ANY
;
2049 /* fragment position treated as a texcoord */
2050 if (InputsRead
& FRAG_BIT_WPOS
) {
2051 cs
->inputs
[FRAG_ATTRIB_WPOS
].refcount
= 0;
2052 cs
->inputs
[FRAG_ATTRIB_WPOS
].reg
= get_hw_temp(cs
, 0);
2054 InputsRead
&= ~FRAG_BIT_WPOS
;
2056 /* Then primary colour */
2057 if (InputsRead
& FRAG_BIT_COL0
) {
2058 cs
->inputs
[FRAG_ATTRIB_COL0
].refcount
= 0;
2059 cs
->inputs
[FRAG_ATTRIB_COL0
].reg
= get_hw_temp(cs
, 0);
2061 InputsRead
&= ~FRAG_BIT_COL0
;
2063 /* Secondary color */
2064 if (InputsRead
& FRAG_BIT_COL1
) {
2065 cs
->inputs
[FRAG_ATTRIB_COL1
].refcount
= 0;
2066 cs
->inputs
[FRAG_ATTRIB_COL1
].reg
= get_hw_temp(cs
, 0);
2068 InputsRead
&= ~FRAG_BIT_COL1
;
2072 WARN_ONCE("Don't know how to handle inputs 0x%x\n", InputsRead
);
2073 /* force read from hwreg 0 for now */
2074 for (i
= 0; i
< 32; i
++)
2075 if (InputsRead
& (1 << i
))
2076 cs
->inputs
[i
].reg
= 0;
2079 /* Pre-parse the program, grabbing refcounts on input/temp regs.
2080 * That way, we can free up the reg when it's no longer needed
2082 for (i
= 0; i
< cs
->compiler
->compiler
.Clauses
[0].NumInstructions
; ++i
) {
2083 struct prog_instruction
*fpi
= cs
->compiler
->compiler
.Clauses
[0].Instructions
+ i
;
2086 for (j
= 0; j
< 3; j
++) {
2087 idx
= fpi
->SrcReg
[j
].Index
;
2088 switch (fpi
->SrcReg
[j
].File
) {
2089 case PROGRAM_TEMPORARY
:
2090 if (!(temps_used
& (1 << idx
))) {
2091 cs
->temps
[idx
].reg
= -1;
2092 cs
->temps
[idx
].refcount
= 1;
2093 temps_used
|= (1 << idx
);
2095 cs
->temps
[idx
].refcount
++;
2098 cs
->inputs
[idx
].refcount
++;
2105 idx
= fpi
->DstReg
.Index
;
2106 if (fpi
->DstReg
.File
== PROGRAM_TEMPORARY
) {
2107 if (!(temps_used
& (1 << idx
))) {
2108 cs
->temps
[idx
].reg
= -1;
2109 cs
->temps
[idx
].refcount
= 1;
2110 temps_used
|= (1 << idx
);
2112 cs
->temps
[idx
].refcount
++;
2115 cs
->temp_in_use
= temps_used
;
2120 * Final compilation step: Turn the intermediate radeon_program into
2121 * machine-readable instructions.
2123 GLboolean
r300FragmentProgramEmit(struct r300_fragment_program_compiler
*compiler
)
2125 struct r300_pfs_compile_state cs
;
2126 struct r300_fragment_program_code
*code
= compiler
->code
;
2128 _mesa_memset(&cs
, 0, sizeof(cs
));
2129 cs
.compiler
= compiler
;
2132 if (!parse_program(&cs
))
2136 code
->node
[code
->cur_node
].alu_end
=
2137 cs
.nrslots
- code
->node
[code
->cur_node
].alu_offset
- 1;
2138 if (code
->node
[code
->cur_node
].tex_end
< 0)
2139 code
->node
[code
->cur_node
].tex_end
= 0;
2140 code
->alu_offset
= 0;
2141 code
->alu_end
= cs
.nrslots
- 1;
2142 code
->tex_offset
= 0;
2143 code
->tex_end
= code
->tex
.length
? code
->tex
.length
- 1 : 0;
2144 assert(code
->node
[code
->cur_node
].alu_end
>= 0);
2145 assert(code
->alu_end
>= 0);