Merge commit 'origin/master' into i965g-restart
[mesa.git] / src / gallium / drivers / svga / svga_tgsi_emit.h
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #ifndef SVGA_TGSI_EMIT_H
27 #define SVGA_TGSI_EMIT_H
28
29 #include "tgsi/tgsi_scan.h"
30 #include "svga_hw_reg.h"
31 #include "svga_tgsi.h"
32 #include "svga3d_shaderdefs.h"
33
34 struct src_register
35 {
36 SVGA3dShaderSrcToken base;
37 SVGA3dShaderSrcToken indirect;
38 };
39
40
41 struct svga_arl_consts {
42 int number;
43 int idx;
44 int swizzle;
45 int arl_num;
46 };
47
48 /* Internal functions:
49 */
50
51 struct svga_shader_emitter
52 {
53 boolean use_sm30;
54
55 unsigned size;
56 char *buf;
57 char *ptr;
58
59 union svga_compile_key key;
60 struct tgsi_shader_info info;
61 int unit;
62
63 int imm_start;
64
65 int nr_hw_const;
66 int nr_hw_temp;
67
68 int insn_offset;
69
70 int internal_temp_count;
71 int internal_imm_count;
72
73 int internal_color_idx[2]; /* diffuse, specular */
74 int internal_color_count;
75
76 boolean emitted_vface;
77 boolean emit_frontface;
78 int internal_frontface_idx;
79
80 int ps30_input_count;
81
82 boolean in_main_func;
83
84 boolean created_zero_immediate;
85 int zero_immediate_idx;
86
87 boolean created_loop_const;
88 int loop_const_idx;
89
90 boolean created_sincos_consts;
91 int sincos_consts_idx;
92
93 unsigned label[32];
94 unsigned nr_labels;
95
96 struct src_register input_map[PIPE_MAX_ATTRIBS];
97 SVGA3dShaderDestToken output_map[PIPE_MAX_ATTRIBS];
98
99 struct src_register imm_0055;
100 SVGA3dShaderDestToken temp_pos;
101 SVGA3dShaderDestToken true_pos;
102
103 SVGA3dShaderDestToken temp_col[PIPE_MAX_COLOR_BUFS];
104 SVGA3dShaderDestToken true_col[PIPE_MAX_COLOR_BUFS];
105
106 SVGA3dShaderDestToken temp_psiz;
107 SVGA3dShaderDestToken true_psiz;
108
109 struct svga_arl_consts arl_consts[12];
110 int num_arl_consts;
111 int current_arl;
112 };
113
114
115 boolean svga_shader_emit_dword( struct svga_shader_emitter *emit,
116 unsigned dword );
117
118 boolean svga_shader_emit_dwords( struct svga_shader_emitter *emit,
119 const unsigned *dwords,
120 unsigned nr );
121
122 boolean svga_shader_emit_opcode( struct svga_shader_emitter *emit,
123 unsigned opcode );
124
125 boolean svga_shader_emit_instructions( struct svga_shader_emitter *emit,
126 const struct tgsi_token *tokens );
127
128 boolean svga_translate_decl_sm20( struct svga_shader_emitter *emit,
129 const struct tgsi_full_declaration *decl );
130
131 boolean svga_translate_decl_sm30( struct svga_shader_emitter *emit,
132 const struct tgsi_full_declaration *decl );
133
134
135 static INLINE boolean emit_dst( struct svga_shader_emitter *emit,
136 SVGA3dShaderDestToken dest )
137 {
138 assert(dest.reserved0);
139 return svga_shader_emit_dword( emit, dest.value );
140 }
141
142 static INLINE boolean emit_src( struct svga_shader_emitter *emit,
143 const struct src_register src )
144 {
145 if (src.base.relAddr) {
146 assert(src.base.reserved0);
147 assert(src.indirect.reserved0);
148 return (svga_shader_emit_dword( emit, src.base.value ) &&
149 svga_shader_emit_dword( emit, src.indirect.value ));
150 }
151 else {
152 assert(src.base.reserved0);
153 return svga_shader_emit_dword( emit, src.base.value );
154 }
155 }
156
157
158 static INLINE boolean emit_instruction( struct svga_shader_emitter *emit,
159 SVGA3dShaderInstToken opcode )
160 {
161 return svga_shader_emit_opcode( emit, opcode.value );
162 }
163
164
165 static INLINE boolean emit_op1( struct svga_shader_emitter *emit,
166 SVGA3dShaderInstToken inst,
167 SVGA3dShaderDestToken dest,
168 struct src_register src0 )
169 {
170 return (emit_instruction( emit, inst ) &&
171 emit_dst( emit, dest ) &&
172 emit_src( emit, src0 ));
173 }
174
175 static INLINE boolean emit_op2( struct svga_shader_emitter *emit,
176 SVGA3dShaderInstToken inst,
177 SVGA3dShaderDestToken dest,
178 struct src_register src0,
179 struct src_register src1 )
180 {
181 return (emit_instruction( emit, inst ) &&
182 emit_dst( emit, dest ) &&
183 emit_src( emit, src0 ) &&
184 emit_src( emit, src1 ));
185 }
186
187 static INLINE boolean emit_op3( struct svga_shader_emitter *emit,
188 SVGA3dShaderInstToken inst,
189 SVGA3dShaderDestToken dest,
190 struct src_register src0,
191 struct src_register src1,
192 struct src_register src2 )
193 {
194 return (emit_instruction( emit, inst ) &&
195 emit_dst( emit, dest ) &&
196 emit_src( emit, src0 ) &&
197 emit_src( emit, src1 ) &&
198 emit_src( emit, src2 ));
199 }
200
201
202 #define TRANSLATE_SWIZZLE(x,y,z,w) ((x) | ((y) << 2) | ((z) << 4) | ((w) << 6))
203 #define SWIZZLE_XYZW \
204 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_W)
205 #define SWIZZLE_XXXX \
206 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X)
207 #define SWIZZLE_YYYY \
208 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y)
209 #define SWIZZLE_ZZZZ \
210 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z)
211 #define SWIZZLE_WWWW \
212 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W)
213
214
215
216 static INLINE SVGA3dShaderInstToken
217 inst_token( unsigned opcode )
218 {
219 SVGA3dShaderInstToken inst;
220
221 inst.value = 0;
222 inst.op = opcode;
223
224 return inst;
225 }
226
227 static INLINE SVGA3dShaderDestToken
228 dst_register( unsigned file,
229 int number )
230 {
231 SVGA3dShaderDestToken dest;
232
233 dest.value = 0;
234 dest.num = number;
235 dest.type_upper = file >> 3;
236 dest.relAddr = 0;
237 dest.reserved1 = 0;
238 dest.mask = 0xf;
239 dest.dstMod = 0;
240 dest.shfScale = 0;
241 dest.type_lower = file & 0x7;
242 dest.reserved0 = 1; /* is_reg */
243
244 return dest;
245 }
246
247 static INLINE SVGA3dShaderDestToken
248 writemask( SVGA3dShaderDestToken dest,
249 unsigned mask )
250 {
251 dest.mask &= mask;
252 return dest;
253 }
254
255
256 static INLINE SVGA3dShaderSrcToken
257 src_token( unsigned file, int number )
258 {
259 SVGA3dShaderSrcToken src;
260
261 src.value = 0;
262 src.num = number;
263 src.type_upper = file >> 3;
264 src.relAddr = 0;
265 src.reserved1 = 0;
266 src.swizzle = SWIZZLE_XYZW;
267 src.srcMod = 0;
268 src.type_lower = file & 0x7;
269 src.reserved0 = 1; /* is_reg */
270
271 return src;
272 }
273
274
275 static INLINE struct src_register
276 absolute( struct src_register src )
277 {
278 src.base.srcMod = SVGA3DSRCMOD_ABS;
279
280 return src;
281 }
282
283
284 static INLINE struct src_register
285 negate( struct src_register src )
286 {
287 switch (src.base.srcMod) {
288 case SVGA3DSRCMOD_ABS:
289 src.base.srcMod = SVGA3DSRCMOD_ABSNEG;
290 break;
291 case SVGA3DSRCMOD_ABSNEG:
292 src.base.srcMod = SVGA3DSRCMOD_ABS;
293 break;
294 case SVGA3DSRCMOD_NEG:
295 src.base.srcMod = SVGA3DSRCMOD_NONE;
296 break;
297 case SVGA3DSRCMOD_NONE:
298 src.base.srcMod = SVGA3DSRCMOD_NEG;
299 break;
300 }
301 return src;
302 }
303
304
305 static INLINE struct src_register
306 src_register( unsigned file, int number )
307 {
308 struct src_register src;
309
310 src.base = src_token( file, number );
311 src.indirect.value = 0;
312
313 return src;
314 }
315
316 static INLINE SVGA3dShaderDestToken dst( struct src_register src )
317 {
318 return dst_register( SVGA3dShaderGetRegType( src.base.value ),
319 src.base.num );
320 }
321
322 static INLINE struct src_register src( SVGA3dShaderDestToken dst )
323 {
324 return src_register( SVGA3dShaderGetRegType( dst.value ),
325 dst.num );
326 }
327
328 static INLINE ubyte svga_tgsi_sampler_type( struct svga_shader_emitter *emit,
329 int idx )
330 {
331 switch (emit->key.fkey.tex[idx].texture_target) {
332 case PIPE_TEXTURE_1D:
333 return SVGA3DSAMP_2D;
334 case PIPE_TEXTURE_2D:
335 return SVGA3DSAMP_2D;
336 case PIPE_TEXTURE_3D:
337 return SVGA3DSAMP_VOLUME;
338 case PIPE_TEXTURE_CUBE:
339 return SVGA3DSAMP_CUBE;
340 }
341
342 return SVGA3DSAMP_UNKNOWN;
343 }
344
345 #endif