1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "i915_context.h"
36 #include "pipe/p_shader_tokens.h"
38 #include "tgsi/tgsi_parse.h"
40 #define I915_PROGRAM_SIZE 192
42 /* Use those indices for pos/face routing, must be >= num of inputs */
43 #define I915_SEMANTIC_POS 100
44 #define I915_SEMANTIC_FACE 101
48 * Program translation state
50 struct i915_fp_compile
{
51 struct i915_fragment_shader
*shader
; /* the shader we're compiling */
53 boolean used_constants
[I915_MAX_CONSTANT
];
55 /** maps TGSI immediate index to constant slot */
57 uint immediates_map
[I915_MAX_CONSTANT
];
58 float immediates
[I915_MAX_CONSTANT
][4];
60 boolean first_instruction
;
62 uint declarations
[I915_PROGRAM_SIZE
];
63 uint program
[I915_PROGRAM_SIZE
];
65 uint
*csr
; /**< Cursor, points into program. */
67 uint
*decl
; /**< Cursor, points into declarations. */
69 uint decl_s
; /**< flags for which s regs need to be decl'd */
70 uint decl_t
; /**< flags for which t regs need to be decl'd */
72 uint temp_flag
; /**< Tracks temporary regs which are in use */
73 uint utemp_flag
; /**< Tracks TYPE_U temporary regs which are in use */
75 uint register_phases
[I915_MAX_TEMPORARY
];
81 boolean error
; /**< Set if i915_program_error() is called */
82 uint NumNativeInstructions
;
83 uint NumNativeAluInstructions
;
84 uint NumNativeTexInstructions
;
85 uint NumNativeTexIndirections
;
89 /* Having zero and one in here makes the definition of swizzle a lot
92 #define UREG_TYPE_SHIFT 29
93 #define UREG_NR_SHIFT 24
94 #define UREG_CHANNEL_X_NEGATE_SHIFT 23
95 #define UREG_CHANNEL_X_SHIFT 20
96 #define UREG_CHANNEL_Y_NEGATE_SHIFT 19
97 #define UREG_CHANNEL_Y_SHIFT 16
98 #define UREG_CHANNEL_Z_NEGATE_SHIFT 15
99 #define UREG_CHANNEL_Z_SHIFT 12
100 #define UREG_CHANNEL_W_NEGATE_SHIFT 11
101 #define UREG_CHANNEL_W_SHIFT 8
102 #define UREG_CHANNEL_ZERO_NEGATE_MBZ 5
103 #define UREG_CHANNEL_ZERO_SHIFT 4
104 #define UREG_CHANNEL_ONE_NEGATE_MBZ 1
105 #define UREG_CHANNEL_ONE_SHIFT 0
107 #define UREG_BAD 0xffffffff /* not a valid ureg */
113 #define ZERO SRC_ZERO
118 #define UREG( type, nr ) (((type)<< UREG_TYPE_SHIFT) | \
119 ((nr) << UREG_NR_SHIFT) | \
120 (X << UREG_CHANNEL_X_SHIFT) | \
121 (Y << UREG_CHANNEL_Y_SHIFT) | \
122 (Z << UREG_CHANNEL_Z_SHIFT) | \
123 (W << UREG_CHANNEL_W_SHIFT) | \
124 (ZERO << UREG_CHANNEL_ZERO_SHIFT) | \
125 (ONE << UREG_CHANNEL_ONE_SHIFT))
127 #define GET_CHANNEL_SRC( reg, channel ) ((reg<<(channel*4)) & (0xf<<20))
128 #define CHANNEL_SRC( src, channel ) (src>>(channel*4))
130 #define GET_UREG_TYPE(reg) (((reg)>>UREG_TYPE_SHIFT)®_TYPE_MASK)
131 #define GET_UREG_NR(reg) (((reg)>>UREG_NR_SHIFT)®_NR_MASK)
135 #define UREG_XYZW_CHANNEL_MASK 0x00ffff00
137 /* One neat thing about the UREG representation:
140 swizzle(int reg
, uint x
, uint y
, uint z
, uint w
)
142 assert(x
<= SRC_ONE
);
143 assert(y
<= SRC_ONE
);
144 assert(z
<= SRC_ONE
);
145 assert(w
<= SRC_ONE
);
146 return ((reg
& ~UREG_XYZW_CHANNEL_MASK
) |
147 CHANNEL_SRC(GET_CHANNEL_SRC(reg
, x
), 0) |
148 CHANNEL_SRC(GET_CHANNEL_SRC(reg
, y
), 1) |
149 CHANNEL_SRC(GET_CHANNEL_SRC(reg
, z
), 2) |
150 CHANNEL_SRC(GET_CHANNEL_SRC(reg
, w
), 3));
154 #define A0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
155 #define D0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
156 #define T0_DEST( reg ) (((reg)&UREG_TYPE_NR_MASK)>>UREG_A0_DEST_SHIFT_LEFT)
157 #define A0_SRC0( reg ) (((reg)&UREG_MASK)>>UREG_A0_SRC0_SHIFT_LEFT)
158 #define A1_SRC0( reg ) (((reg)&UREG_MASK)<<UREG_A1_SRC0_SHIFT_RIGHT)
159 #define A1_SRC1( reg ) (((reg)&UREG_MASK)>>UREG_A1_SRC1_SHIFT_LEFT)
160 #define A2_SRC1( reg ) (((reg)&UREG_MASK)<<UREG_A2_SRC1_SHIFT_RIGHT)
161 #define A2_SRC2( reg ) (((reg)&UREG_MASK)>>UREG_A2_SRC2_SHIFT_LEFT)
163 /* These are special, and don't have swizzle/negate bits.
165 #define T0_SAMPLER( reg ) (GET_UREG_NR(reg)<<T0_SAMPLER_NR_SHIFT)
166 #define T1_ADDRESS_REG( reg ) ((GET_UREG_NR(reg)<<T1_ADDRESS_REG_NR_SHIFT) | \
167 (GET_UREG_TYPE(reg)<<T1_ADDRESS_REG_TYPE_SHIFT))
170 /* Macros for translating UREG's into the various register fields used
171 * by the I915 programmable unit.
173 #define UREG_A0_DEST_SHIFT_LEFT (UREG_TYPE_SHIFT - A0_DEST_TYPE_SHIFT)
174 #define UREG_A0_SRC0_SHIFT_LEFT (UREG_TYPE_SHIFT - A0_SRC0_TYPE_SHIFT)
175 #define UREG_A1_SRC0_SHIFT_RIGHT (A1_SRC0_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
176 #define UREG_A1_SRC1_SHIFT_LEFT (UREG_TYPE_SHIFT - A1_SRC1_TYPE_SHIFT)
177 #define UREG_A2_SRC1_SHIFT_RIGHT (A2_SRC1_CHANNEL_W_SHIFT - UREG_CHANNEL_W_SHIFT)
178 #define UREG_A2_SRC2_SHIFT_LEFT (UREG_TYPE_SHIFT - A2_SRC2_TYPE_SHIFT)
180 #define UREG_MASK 0xffffff00
181 #define UREG_TYPE_NR_MASK ((REG_TYPE_MASK << UREG_TYPE_SHIFT) | \
182 (REG_NR_MASK << UREG_NR_SHIFT))
187 /***********************************************************************
188 * Public interface for the compiler
191 i915_translate_fragment_program( struct i915_context
*i915
,
192 struct i915_fragment_shader
*fs
);
196 extern uint
i915_get_temp(struct i915_fp_compile
*p
);
197 extern uint
i915_get_utemp(struct i915_fp_compile
*p
);
198 extern void i915_release_utemps(struct i915_fp_compile
*p
);
201 extern uint
i915_emit_texld(struct i915_fp_compile
*p
,
209 extern uint
i915_emit_arith(struct i915_fp_compile
*p
,
214 uint src0
, uint src1
, uint src2
);
216 extern uint
i915_emit_decl(struct i915_fp_compile
*p
,
217 uint type
, uint nr
, uint d0_flags
);
220 extern uint
i915_emit_const1f(struct i915_fp_compile
*p
, float c0
);
222 extern uint
i915_emit_const2f(struct i915_fp_compile
*p
,
225 extern uint
i915_emit_const4fv(struct i915_fp_compile
*p
,
228 extern uint
i915_emit_const4f(struct i915_fp_compile
*p
,
233 /*======================================================================
234 * i915_fpc_translate.c
238 i915_program_error(struct i915_fp_compile
*p
, const char *msg
, ...);
241 /*======================================================================
242 * i915_fpc_optimize.c
246 struct i915_src_register
248 unsigned File
: 4; /* TGSI_FILE_ */
249 unsigned Indirect
: 1; /* BOOL */
250 unsigned Dimension
: 1; /* BOOL */
251 int Index
: 16; /* SINT */
252 unsigned SwizzleX
: 3; /* TGSI_SWIZZLE_ */
253 unsigned SwizzleY
: 3; /* TGSI_SWIZZLE_ */
254 unsigned SwizzleZ
: 3; /* TGSI_SWIZZLE_ */
255 unsigned SwizzleW
: 3; /* TGSI_SWIZZLE_ */
256 unsigned Absolute
: 1; /* BOOL */
257 unsigned Negate
: 1; /* BOOL */
260 /* Additional swizzle supported in i915 */
261 #define TGSI_SWIZZLE_ZERO 4
262 #define TGSI_SWIZZLE_ONE 5
264 struct i915_dst_register
266 unsigned File
: 4; /* TGSI_FILE_ */
267 unsigned WriteMask
: 4; /* TGSI_WRITEMASK_ */
268 unsigned Indirect
: 1; /* BOOL */
269 unsigned Dimension
: 1; /* BOOL */
270 int Index
: 16; /* SINT */
271 unsigned Padding
: 6;
275 struct i915_full_dst_register
277 struct i915_dst_register Register
;
279 struct tgsi_ind_register Indirect;
280 struct tgsi_dimension Dimension;
281 struct tgsi_ind_register DimIndirect;
285 struct i915_full_src_register
287 struct i915_src_register Register
;
289 struct tgsi_ind_register Indirect;
290 struct tgsi_dimension Dimension;
291 struct tgsi_ind_register DimIndirect;
295 struct i915_full_instruction
297 struct tgsi_instruction Instruction
;
299 struct tgsi_instruction_predicate Predicate;
300 struct tgsi_instruction_label Label;
302 struct tgsi_instruction_texture Texture
;
303 struct i915_full_dst_register Dst
[1];
304 struct i915_full_src_register Src
[3];
308 union i915_full_token
310 struct tgsi_token Token
;
311 struct tgsi_full_declaration FullDeclaration
;
312 struct tgsi_full_immediate FullImmediate
;
313 struct i915_full_instruction FullInstruction
;
314 struct tgsi_full_property FullProperty
;
317 struct i915_token_list
319 union i915_full_token
* Tokens
;
323 extern struct i915_token_list
* i915_optimize(const struct tgsi_token
*tokens
);
325 extern void i915_optimize_free(struct i915_token_list
*tokens
);
327 extern uint
i915_num_coords(uint tex
);