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
59 union svga_compile_key key
;
60 struct tgsi_shader_info info
;
70 int internal_temp_count
;
71 int internal_imm_count
;
73 int internal_color_idx
[2]; /* diffuse, specular */
74 int internal_color_count
;
76 boolean emitted_vface
;
77 boolean emit_frontface
;
78 int internal_frontface_idx
;
82 int dynamic_branching_level
;
86 boolean created_zero_immediate
;
87 int zero_immediate_idx
;
89 boolean created_loop_const
;
92 boolean created_sincos_consts
;
93 int sincos_consts_idx
;
98 struct src_register input_map
[PIPE_MAX_ATTRIBS
];
99 SVGA3dShaderDestToken output_map
[PIPE_MAX_ATTRIBS
];
101 struct src_register imm_0055
;
102 SVGA3dShaderDestToken temp_pos
;
103 SVGA3dShaderDestToken true_pos
;
105 SVGA3dShaderDestToken temp_col
[PIPE_MAX_COLOR_BUFS
];
106 SVGA3dShaderDestToken true_col
[PIPE_MAX_COLOR_BUFS
];
108 SVGA3dShaderDestToken temp_psiz
;
109 SVGA3dShaderDestToken true_psiz
;
111 struct svga_arl_consts arl_consts
[12];
117 boolean
svga_shader_emit_dword( struct svga_shader_emitter
*emit
,
120 boolean
svga_shader_emit_dwords( struct svga_shader_emitter
*emit
,
121 const unsigned *dwords
,
124 boolean
svga_shader_emit_opcode( struct svga_shader_emitter
*emit
,
127 boolean
svga_shader_emit_instructions( struct svga_shader_emitter
*emit
,
128 const struct tgsi_token
*tokens
);
130 boolean
svga_translate_decl_sm20( struct svga_shader_emitter
*emit
,
131 const struct tgsi_full_declaration
*decl
);
133 boolean
svga_translate_decl_sm30( struct svga_shader_emitter
*emit
,
134 const struct tgsi_full_declaration
*decl
);
137 static INLINE boolean
emit_dst( struct svga_shader_emitter
*emit
,
138 SVGA3dShaderDestToken dest
)
140 assert(dest
.reserved0
);
141 return svga_shader_emit_dword( emit
, dest
.value
);
144 static INLINE boolean
emit_src( struct svga_shader_emitter
*emit
,
145 const struct src_register src
)
147 if (src
.base
.relAddr
) {
148 assert(src
.base
.reserved0
);
149 assert(src
.indirect
.reserved0
);
150 return (svga_shader_emit_dword( emit
, src
.base
.value
) &&
151 svga_shader_emit_dword( emit
, src
.indirect
.value
));
154 assert(src
.base
.reserved0
);
155 return svga_shader_emit_dword( emit
, src
.base
.value
);
160 static INLINE boolean
emit_instruction( struct svga_shader_emitter
*emit
,
161 SVGA3dShaderInstToken opcode
)
163 return svga_shader_emit_opcode( emit
, opcode
.value
);
167 static INLINE boolean
emit_op1( struct svga_shader_emitter
*emit
,
168 SVGA3dShaderInstToken inst
,
169 SVGA3dShaderDestToken dest
,
170 struct src_register src0
)
172 return (emit_instruction( emit
, inst
) &&
173 emit_dst( emit
, dest
) &&
174 emit_src( emit
, src0
));
177 static INLINE boolean
emit_op2( struct svga_shader_emitter
*emit
,
178 SVGA3dShaderInstToken inst
,
179 SVGA3dShaderDestToken dest
,
180 struct src_register src0
,
181 struct src_register src1
)
183 return (emit_instruction( emit
, inst
) &&
184 emit_dst( emit
, dest
) &&
185 emit_src( emit
, src0
) &&
186 emit_src( emit
, src1
));
189 static INLINE boolean
emit_op3( struct svga_shader_emitter
*emit
,
190 SVGA3dShaderInstToken inst
,
191 SVGA3dShaderDestToken dest
,
192 struct src_register src0
,
193 struct src_register src1
,
194 struct src_register src2
)
196 return (emit_instruction( emit
, inst
) &&
197 emit_dst( emit
, dest
) &&
198 emit_src( emit
, src0
) &&
199 emit_src( emit
, src1
) &&
200 emit_src( emit
, src2
));
204 #define TRANSLATE_SWIZZLE(x,y,z,w) ((x) | ((y) << 2) | ((z) << 4) | ((w) << 6))
205 #define SWIZZLE_XYZW \
206 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_W)
207 #define SWIZZLE_XXXX \
208 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X)
209 #define SWIZZLE_YYYY \
210 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y)
211 #define SWIZZLE_ZZZZ \
212 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z)
213 #define SWIZZLE_WWWW \
214 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W)
218 static INLINE SVGA3dShaderInstToken
219 inst_token( unsigned opcode
)
221 SVGA3dShaderInstToken inst
;
229 static INLINE SVGA3dShaderDestToken
230 dst_register( unsigned file
,
233 SVGA3dShaderDestToken dest
;
237 dest
.type_upper
= file
>> 3;
243 dest
.type_lower
= file
& 0x7;
244 dest
.reserved0
= 1; /* is_reg */
249 static INLINE SVGA3dShaderDestToken
250 writemask( SVGA3dShaderDestToken dest
,
258 static INLINE SVGA3dShaderSrcToken
259 src_token( unsigned file
, int number
)
261 SVGA3dShaderSrcToken src
;
265 src
.type_upper
= file
>> 3;
268 src
.swizzle
= SWIZZLE_XYZW
;
270 src
.type_lower
= file
& 0x7;
271 src
.reserved0
= 1; /* is_reg */
277 static INLINE
struct src_register
278 absolute( struct src_register src
)
280 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
286 static INLINE
struct src_register
287 negate( struct src_register src
)
289 switch (src
.base
.srcMod
) {
290 case SVGA3DSRCMOD_ABS
:
291 src
.base
.srcMod
= SVGA3DSRCMOD_ABSNEG
;
293 case SVGA3DSRCMOD_ABSNEG
:
294 src
.base
.srcMod
= SVGA3DSRCMOD_ABS
;
296 case SVGA3DSRCMOD_NEG
:
297 src
.base
.srcMod
= SVGA3DSRCMOD_NONE
;
299 case SVGA3DSRCMOD_NONE
:
300 src
.base
.srcMod
= SVGA3DSRCMOD_NEG
;
307 static INLINE
struct src_register
308 src_register( unsigned file
, int number
)
310 struct src_register src
;
312 src
.base
= src_token( file
, number
);
313 src
.indirect
.value
= 0;
318 static INLINE SVGA3dShaderDestToken
dst( struct src_register src
)
320 return dst_register( SVGA3dShaderGetRegType( src
.base
.value
),
324 static INLINE
struct src_register
src( SVGA3dShaderDestToken dst
)
326 return src_register( SVGA3dShaderGetRegType( dst
.value
),
330 static INLINE ubyte
svga_tgsi_sampler_type( struct svga_shader_emitter
*emit
,
333 switch (emit
->key
.fkey
.tex
[idx
].texture_target
) {
334 case PIPE_TEXTURE_1D
:
335 return SVGA3DSAMP_2D
;
336 case PIPE_TEXTURE_2D
:
337 return SVGA3DSAMP_2D
;
338 case PIPE_TEXTURE_3D
:
339 return SVGA3DSAMP_VOLUME
;
340 case PIPE_TEXTURE_CUBE
:
341 return SVGA3DSAMP_CUBE
;
344 return SVGA3DSAMP_UNKNOWN
;