1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
26 #ifndef SVGA_TGSI_EMIT_H
27 #define SVGA_TGSI_EMIT_H
29 #include "tgsi/tgsi_scan.h"
30 #include "svga_hw_reg.h"
31 #include "svga_tgsi.h"
32 #include "svga3d_shaderdefs.h"
36 SVGA3dShaderSrcToken base
;
37 SVGA3dShaderSrcToken indirect
;
41 struct svga_arl_consts
{
48 /* Internal functions:
51 struct svga_shader_emitter
57 struct svga_compile_key key
;
58 struct tgsi_shader_info info
;
63 int nr_hw_float_const
;
69 int internal_temp_count
;
70 int internal_imm_count
;
72 int internal_color_idx
[2]; /* diffuse, specular */
73 int internal_color_count
;
75 boolean emitted_vface
;
76 boolean emit_frontface
;
77 int internal_frontface_idx
;
80 int vs30_output_count
;
82 int dynamic_branching_level
;
86 boolean created_zero_immediate
;
87 int zero_immediate_idx
;
89 boolean created_loop_const
;
92 unsigned inverted_texcoords
; /**< bitmask of which texcoords are flipped */
93 struct src_register ps_true_texcoord
[PIPE_MAX_ATTRIBS
];
94 struct src_register ps_inverted_texcoord
[PIPE_MAX_ATTRIBS
];
95 unsigned ps_inverted_texcoord_input
[PIPE_MAX_ATTRIBS
];
100 struct src_register input_map
[PIPE_MAX_ATTRIBS
];
101 SVGA3dShaderDestToken output_map
[PIPE_MAX_ATTRIBS
];
103 boolean ps_reads_pos
;
104 boolean emitted_depth_fog
;
105 struct src_register ps_true_pos
;
106 struct src_register ps_depth_pos
;
107 SVGA3dShaderDestToken ps_temp_pos
;
109 /* shared input for depth and fog */
110 struct src_register ps_depth_fog
;
112 struct src_register imm_0055
;
113 SVGA3dShaderDestToken temp_pos
;
114 SVGA3dShaderDestToken true_pos
;
115 SVGA3dShaderDestToken depth_pos
;
117 /* shared output for depth and fog */
118 SVGA3dShaderDestToken vs_depth_fog
;
120 SVGA3dShaderDestToken temp_col
[PIPE_MAX_COLOR_BUFS
];
121 SVGA3dShaderDestToken true_col
[PIPE_MAX_COLOR_BUFS
];
123 SVGA3dShaderDestToken temp_psiz
;
124 SVGA3dShaderDestToken true_psiz
;
126 struct svga_arl_consts arl_consts
[12];
132 boolean
svga_shader_emit_dword( struct svga_shader_emitter
*emit
,
135 boolean
svga_shader_emit_dwords( struct svga_shader_emitter
*emit
,
136 const unsigned *dwords
,
139 boolean
svga_shader_emit_opcode( struct svga_shader_emitter
*emit
,
142 boolean
svga_shader_emit_instructions( struct svga_shader_emitter
*emit
,
143 const struct tgsi_token
*tokens
);
145 boolean
svga_translate_decl_sm30( struct svga_shader_emitter
*emit
,
146 const struct tgsi_full_declaration
*decl
);
149 static INLINE boolean
emit_dst( struct svga_shader_emitter
*emit
,
150 SVGA3dShaderDestToken dest
)
152 assert(dest
.reserved0
);
154 return svga_shader_emit_dword( emit
, dest
.value
);
157 static INLINE boolean
emit_src( struct svga_shader_emitter
*emit
,
158 const struct src_register src
)
160 if (src
.base
.relAddr
) {
161 assert(src
.base
.reserved0
);
162 assert(src
.indirect
.reserved0
);
163 return (svga_shader_emit_dword( emit
, src
.base
.value
) &&
164 svga_shader_emit_dword( emit
, src
.indirect
.value
));
167 assert(src
.base
.reserved0
);
168 return svga_shader_emit_dword( emit
, src
.base
.value
);
173 static INLINE boolean
emit_instruction( struct svga_shader_emitter
*emit
,
174 SVGA3dShaderInstToken opcode
)
176 return svga_shader_emit_opcode( emit
, opcode
.value
);
180 static INLINE boolean
emit_op1( struct svga_shader_emitter
*emit
,
181 SVGA3dShaderInstToken inst
,
182 SVGA3dShaderDestToken dest
,
183 struct src_register src0
)
185 return (emit_instruction( emit
, inst
) &&
186 emit_dst( emit
, dest
) &&
187 emit_src( emit
, src0
));
190 static INLINE boolean
emit_op2( struct svga_shader_emitter
*emit
,
191 SVGA3dShaderInstToken inst
,
192 SVGA3dShaderDestToken dest
,
193 struct src_register src0
,
194 struct src_register src1
)
196 return (emit_instruction( emit
, inst
) &&
197 emit_dst( emit
, dest
) &&
198 emit_src( emit
, src0
) &&
199 emit_src( emit
, src1
));
202 static INLINE boolean
emit_op3( struct svga_shader_emitter
*emit
,
203 SVGA3dShaderInstToken inst
,
204 SVGA3dShaderDestToken dest
,
205 struct src_register src0
,
206 struct src_register src1
,
207 struct src_register src2
)
209 return (emit_instruction( emit
, inst
) &&
210 emit_dst( emit
, dest
) &&
211 emit_src( emit
, src0
) &&
212 emit_src( emit
, src1
) &&
213 emit_src( emit
, src2
));
217 static INLINE boolean
emit_op4( struct svga_shader_emitter
*emit
,
218 SVGA3dShaderInstToken inst
,
219 SVGA3dShaderDestToken dest
,
220 struct src_register src0
,
221 struct src_register src1
,
222 struct src_register src2
,
223 struct src_register src3
)
225 return (emit_instruction( emit
, inst
) &&
226 emit_dst( emit
, dest
) &&
227 emit_src( emit
, src0
) &&
228 emit_src( emit
, src1
) &&
229 emit_src( emit
, src2
) &&
230 emit_src( emit
, src3
));
234 #define TRANSLATE_SWIZZLE(x,y,z,w) ((x) | ((y) << 2) | ((z) << 4) | ((w) << 6))
235 #define SWIZZLE_XYZW \
236 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_W)
237 #define SWIZZLE_XXXX \
238 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X)
239 #define SWIZZLE_YYYY \
240 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y)
241 #define SWIZZLE_ZZZZ \
242 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z)
243 #define SWIZZLE_WWWW \
244 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W)
248 static INLINE SVGA3dShaderInstToken
249 inst_token( unsigned opcode
)
251 SVGA3dShaderInstToken inst
;
259 static INLINE SVGA3dShaderDestToken
260 dst_register( unsigned file
,
263 SVGA3dShaderDestToken dest
;
265 /* check values against bitfield sizes */
266 assert(number
< (1 << 11));
267 assert(file
<= SVGA3DREG_PREDICATE
);
269 assert(number
< SVGA3D_TEMPREG_MAX
);
273 dest
.type_upper
= file
>> 3;
279 dest
.type_lower
= file
& 0x7;
280 dest
.reserved0
= 1; /* is_reg */
285 static INLINE SVGA3dShaderDestToken
286 writemask( SVGA3dShaderDestToken dest
,
289 assert(dest
.mask
& mask
);
295 static INLINE SVGA3dShaderSrcToken
296 src_token( unsigned file
, int number
)
298 SVGA3dShaderSrcToken src
;
300 /* check values against bitfield sizes */
301 assert(number
< (1 << 11));
302 assert(file
<= SVGA3DREG_PREDICATE
);
306 src
.type_upper
= file
>> 3;
309 src
.swizzle
= SWIZZLE_XYZW
;
311 src
.type_lower
= file
& 0x7;
312 src
.reserved0
= 1; /* is_reg */
318 static INLINE
struct src_register
319 absolute( struct src_register src
)
321 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
327 static INLINE
struct src_register
328 negate( struct src_register src
)
330 switch (src
.base
.srcMod
) {
331 case SVGA3DSRCMOD_ABS
:
332 src
.base
.srcMod
= SVGA3DSRCMOD_ABSNEG
;
334 case SVGA3DSRCMOD_ABSNEG
:
335 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
337 case SVGA3DSRCMOD_NEG
:
338 src
.base
.srcMod
= SVGA3DSRCMOD_NONE
;
340 case SVGA3DSRCMOD_NONE
:
341 src
.base
.srcMod
= SVGA3DSRCMOD_NEG
;
348 static INLINE
struct src_register
349 src_register( unsigned file
, int number
)
351 struct src_register src
;
353 src
.base
= src_token( file
, number
);
354 src
.indirect
.value
= 0;
359 static INLINE SVGA3dShaderDestToken
dst( struct src_register src
)
361 return dst_register( SVGA3dShaderGetRegType( src
.base
.value
),
365 static INLINE
struct src_register
src( SVGA3dShaderDestToken dst
)
367 return src_register( SVGA3dShaderGetRegType( dst
.value
),
371 static INLINE ubyte
svga_tgsi_sampler_type( struct svga_shader_emitter
*emit
,
374 switch (emit
->key
.fkey
.tex
[idx
].texture_target
) {
375 case PIPE_TEXTURE_1D
:
376 return SVGA3DSAMP_2D
;
377 case PIPE_TEXTURE_2D
:
378 case PIPE_TEXTURE_RECT
:
379 return SVGA3DSAMP_2D
;
380 case PIPE_TEXTURE_3D
:
381 return SVGA3DSAMP_VOLUME
;
382 case PIPE_TEXTURE_CUBE
:
383 return SVGA3DSAMP_CUBE
;
386 return SVGA3DSAMP_UNKNOWN
;