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;
65 int nr_hw_float_const;
71 int internal_temp_count;
72 int internal_imm_count;
74 int internal_color_idx[2]; /* diffuse, specular */
75 int internal_color_count;
77 boolean emitted_vface;
78 boolean emit_frontface;
79 int internal_frontface_idx;
83 int dynamic_branching_level;
87 boolean created_zero_immediate;
88 int zero_immediate_idx;
90 boolean created_loop_const;
93 boolean created_sincos_consts;
94 int sincos_consts_idx;
99 struct src_register input_map[PIPE_MAX_ATTRIBS];
100 SVGA3dShaderDestToken output_map[PIPE_MAX_ATTRIBS];
102 struct src_register imm_0055;
103 SVGA3dShaderDestToken temp_pos;
104 SVGA3dShaderDestToken true_pos;
106 SVGA3dShaderDestToken temp_col[PIPE_MAX_COLOR_BUFS];
107 SVGA3dShaderDestToken true_col[PIPE_MAX_COLOR_BUFS];
109 SVGA3dShaderDestToken temp_psiz;
110 SVGA3dShaderDestToken true_psiz;
112 struct svga_arl_consts arl_consts[12];
118 boolean svga_shader_emit_dword( struct svga_shader_emitter *emit,
121 boolean svga_shader_emit_dwords( struct svga_shader_emitter *emit,
122 const unsigned *dwords,
125 boolean svga_shader_emit_opcode( struct svga_shader_emitter *emit,
128 boolean svga_shader_emit_instructions( struct svga_shader_emitter *emit,
129 const struct tgsi_token *tokens );
131 boolean svga_translate_decl_sm20( struct svga_shader_emitter *emit,
132 const struct tgsi_full_declaration *decl );
134 boolean svga_translate_decl_sm30( struct svga_shader_emitter *emit,
135 const struct tgsi_full_declaration *decl );
138 static INLINE boolean emit_dst( struct svga_shader_emitter *emit,
139 SVGA3dShaderDestToken dest )
141 assert(dest.reserved0);
143 return svga_shader_emit_dword( emit, dest.value );
146 static INLINE boolean emit_src( struct svga_shader_emitter *emit,
147 const struct src_register src )
149 if (src.base.relAddr) {
150 assert(src.base.reserved0);
151 assert(src.indirect.reserved0);
152 return (svga_shader_emit_dword( emit, src.base.value ) &&
153 svga_shader_emit_dword( emit, src.indirect.value ));
156 assert(src.base.reserved0);
157 return svga_shader_emit_dword( emit, src.base.value );
162 static INLINE boolean emit_instruction( struct svga_shader_emitter *emit,
163 SVGA3dShaderInstToken opcode )
165 return svga_shader_emit_opcode( emit, opcode.value );
169 static INLINE boolean emit_op1( struct svga_shader_emitter *emit,
170 SVGA3dShaderInstToken inst,
171 SVGA3dShaderDestToken dest,
172 struct src_register src0 )
174 return (emit_instruction( emit, inst ) &&
175 emit_dst( emit, dest ) &&
176 emit_src( emit, src0 ));
179 static INLINE boolean emit_op2( struct svga_shader_emitter *emit,
180 SVGA3dShaderInstToken inst,
181 SVGA3dShaderDestToken dest,
182 struct src_register src0,
183 struct src_register src1 )
185 return (emit_instruction( emit, inst ) &&
186 emit_dst( emit, dest ) &&
187 emit_src( emit, src0 ) &&
188 emit_src( emit, src1 ));
191 static INLINE boolean emit_op3( struct svga_shader_emitter *emit,
192 SVGA3dShaderInstToken inst,
193 SVGA3dShaderDestToken dest,
194 struct src_register src0,
195 struct src_register src1,
196 struct src_register src2 )
198 return (emit_instruction( emit, inst ) &&
199 emit_dst( emit, dest ) &&
200 emit_src( emit, src0 ) &&
201 emit_src( emit, src1 ) &&
202 emit_src( emit, src2 ));
206 static INLINE boolean emit_op4( struct svga_shader_emitter *emit,
207 SVGA3dShaderInstToken inst,
208 SVGA3dShaderDestToken dest,
209 struct src_register src0,
210 struct src_register src1,
211 struct src_register src2,
212 struct src_register src3)
214 return (emit_instruction( emit, inst ) &&
215 emit_dst( emit, dest ) &&
216 emit_src( emit, src0 ) &&
217 emit_src( emit, src1 ) &&
218 emit_src( emit, src2 ) &&
219 emit_src( emit, src3 ));
223 #define TRANSLATE_SWIZZLE(x,y,z,w) ((x) | ((y) << 2) | ((z) << 4) | ((w) << 6))
224 #define SWIZZLE_XYZW \
225 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_W)
226 #define SWIZZLE_XXXX \
227 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X)
228 #define SWIZZLE_YYYY \
229 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y)
230 #define SWIZZLE_ZZZZ \
231 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z)
232 #define SWIZZLE_WWWW \
233 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W)
237 static INLINE SVGA3dShaderInstToken
238 inst_token( unsigned opcode )
240 SVGA3dShaderInstToken inst;
248 static INLINE SVGA3dShaderDestToken
249 dst_register( unsigned file,
252 SVGA3dShaderDestToken dest;
256 dest.type_upper = file >> 3;
262 dest.type_lower = file & 0x7;
263 dest.reserved0 = 1; /* is_reg */
268 static INLINE SVGA3dShaderDestToken
269 writemask( SVGA3dShaderDestToken dest,
272 assert(dest.mask & mask);
278 static INLINE SVGA3dShaderSrcToken
279 src_token( unsigned file, int number )
281 SVGA3dShaderSrcToken src;
285 src.type_upper = file >> 3;
288 src.swizzle = SWIZZLE_XYZW;
290 src.type_lower = file & 0x7;
291 src.reserved0 = 1; /* is_reg */
297 static INLINE struct src_register
298 absolute( struct src_register src )
300 src.base.srcMod = SVGA3DSRCMOD_ABS;
306 static INLINE struct src_register
307 negate( struct src_register src )
309 switch (src.base.srcMod) {
310 case SVGA3DSRCMOD_ABS:
311 src.base.srcMod = SVGA3DSRCMOD_ABSNEG;
313 case SVGA3DSRCMOD_ABSNEG:
314 src.base.srcMod = SVGA3DSRCMOD_ABS;
316 case SVGA3DSRCMOD_NEG:
317 src.base.srcMod = SVGA3DSRCMOD_NONE;
319 case SVGA3DSRCMOD_NONE:
320 src.base.srcMod = SVGA3DSRCMOD_NEG;
327 static INLINE struct src_register
328 src_register( unsigned file, int number )
330 struct src_register src;
332 src.base = src_token( file, number );
333 src.indirect.value = 0;
338 static INLINE SVGA3dShaderDestToken dst( struct src_register src )
340 return dst_register( SVGA3dShaderGetRegType( src.base.value ),
344 static INLINE struct src_register src( SVGA3dShaderDestToken dst )
346 return src_register( SVGA3dShaderGetRegType( dst.value ),
350 static INLINE ubyte svga_tgsi_sampler_type( struct svga_shader_emitter *emit,
353 switch (emit->key.fkey.tex[idx].texture_target) {
354 case PIPE_TEXTURE_1D:
355 return SVGA3DSAMP_2D;
356 case PIPE_TEXTURE_2D:
357 case PIPE_TEXTURE_RECT:
358 return SVGA3DSAMP_2D;
359 case PIPE_TEXTURE_3D:
360 return SVGA3DSAMP_VOLUME;
361 case PIPE_TEXTURE_CUBE:
362 return SVGA3DSAMP_CUBE;
365 return SVGA3DSAMP_UNKNOWN;