Merge remote branch 'origin/master' into nv50-compiler
[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 int dynamic_branching_level;
83
84 boolean in_main_func;
85
86 boolean created_zero_immediate;
87 int zero_immediate_idx;
88
89 boolean created_loop_const;
90 int loop_const_idx;
91
92 boolean created_sincos_consts;
93 int sincos_consts_idx;
94
95 unsigned label[32];
96 unsigned nr_labels;
97
98 struct src_register input_map[PIPE_MAX_ATTRIBS];
99 SVGA3dShaderDestToken output_map[PIPE_MAX_ATTRIBS];
100
101 struct src_register imm_0055;
102 SVGA3dShaderDestToken temp_pos;
103 SVGA3dShaderDestToken true_pos;
104
105 SVGA3dShaderDestToken temp_col[PIPE_MAX_COLOR_BUFS];
106 SVGA3dShaderDestToken true_col[PIPE_MAX_COLOR_BUFS];
107
108 SVGA3dShaderDestToken temp_psiz;
109 SVGA3dShaderDestToken true_psiz;
110
111 struct svga_arl_consts arl_consts[12];
112 int num_arl_consts;
113 int current_arl;
114 };
115
116
117 boolean svga_shader_emit_dword( struct svga_shader_emitter *emit,
118 unsigned dword );
119
120 boolean svga_shader_emit_dwords( struct svga_shader_emitter *emit,
121 const unsigned *dwords,
122 unsigned nr );
123
124 boolean svga_shader_emit_opcode( struct svga_shader_emitter *emit,
125 unsigned opcode );
126
127 boolean svga_shader_emit_instructions( struct svga_shader_emitter *emit,
128 const struct tgsi_token *tokens );
129
130 boolean svga_translate_decl_sm20( struct svga_shader_emitter *emit,
131 const struct tgsi_full_declaration *decl );
132
133 boolean svga_translate_decl_sm30( struct svga_shader_emitter *emit,
134 const struct tgsi_full_declaration *decl );
135
136
137 static INLINE boolean emit_dst( struct svga_shader_emitter *emit,
138 SVGA3dShaderDestToken dest )
139 {
140 assert(dest.reserved0);
141 assert(dest.mask);
142 return svga_shader_emit_dword( emit, dest.value );
143 }
144
145 static INLINE boolean emit_src( struct svga_shader_emitter *emit,
146 const struct src_register src )
147 {
148 if (src.base.relAddr) {
149 assert(src.base.reserved0);
150 assert(src.indirect.reserved0);
151 return (svga_shader_emit_dword( emit, src.base.value ) &&
152 svga_shader_emit_dword( emit, src.indirect.value ));
153 }
154 else {
155 assert(src.base.reserved0);
156 return svga_shader_emit_dword( emit, src.base.value );
157 }
158 }
159
160
161 static INLINE boolean emit_instruction( struct svga_shader_emitter *emit,
162 SVGA3dShaderInstToken opcode )
163 {
164 return svga_shader_emit_opcode( emit, opcode.value );
165 }
166
167
168 static INLINE boolean emit_op1( struct svga_shader_emitter *emit,
169 SVGA3dShaderInstToken inst,
170 SVGA3dShaderDestToken dest,
171 struct src_register src0 )
172 {
173 return (emit_instruction( emit, inst ) &&
174 emit_dst( emit, dest ) &&
175 emit_src( emit, src0 ));
176 }
177
178 static INLINE boolean emit_op2( struct svga_shader_emitter *emit,
179 SVGA3dShaderInstToken inst,
180 SVGA3dShaderDestToken dest,
181 struct src_register src0,
182 struct src_register src1 )
183 {
184 return (emit_instruction( emit, inst ) &&
185 emit_dst( emit, dest ) &&
186 emit_src( emit, src0 ) &&
187 emit_src( emit, src1 ));
188 }
189
190 static INLINE boolean emit_op3( struct svga_shader_emitter *emit,
191 SVGA3dShaderInstToken inst,
192 SVGA3dShaderDestToken dest,
193 struct src_register src0,
194 struct src_register src1,
195 struct src_register src2 )
196 {
197 return (emit_instruction( emit, inst ) &&
198 emit_dst( emit, dest ) &&
199 emit_src( emit, src0 ) &&
200 emit_src( emit, src1 ) &&
201 emit_src( emit, src2 ));
202 }
203
204
205 static INLINE boolean emit_op4( struct svga_shader_emitter *emit,
206 SVGA3dShaderInstToken inst,
207 SVGA3dShaderDestToken dest,
208 struct src_register src0,
209 struct src_register src1,
210 struct src_register src2,
211 struct src_register src3)
212 {
213 return (emit_instruction( emit, inst ) &&
214 emit_dst( emit, dest ) &&
215 emit_src( emit, src0 ) &&
216 emit_src( emit, src1 ) &&
217 emit_src( emit, src2 ) &&
218 emit_src( emit, src3 ));
219 }
220
221
222 #define TRANSLATE_SWIZZLE(x,y,z,w) ((x) | ((y) << 2) | ((z) << 4) | ((w) << 6))
223 #define SWIZZLE_XYZW \
224 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_W)
225 #define SWIZZLE_XXXX \
226 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X)
227 #define SWIZZLE_YYYY \
228 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y)
229 #define SWIZZLE_ZZZZ \
230 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z)
231 #define SWIZZLE_WWWW \
232 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W)
233
234
235
236 static INLINE SVGA3dShaderInstToken
237 inst_token( unsigned opcode )
238 {
239 SVGA3dShaderInstToken inst;
240
241 inst.value = 0;
242 inst.op = opcode;
243
244 return inst;
245 }
246
247 static INLINE SVGA3dShaderDestToken
248 dst_register( unsigned file,
249 int number )
250 {
251 SVGA3dShaderDestToken dest;
252
253 dest.value = 0;
254 dest.num = number;
255 dest.type_upper = file >> 3;
256 dest.relAddr = 0;
257 dest.reserved1 = 0;
258 dest.mask = 0xf;
259 dest.dstMod = 0;
260 dest.shfScale = 0;
261 dest.type_lower = file & 0x7;
262 dest.reserved0 = 1; /* is_reg */
263
264 return dest;
265 }
266
267 static INLINE SVGA3dShaderDestToken
268 writemask( SVGA3dShaderDestToken dest,
269 unsigned mask )
270 {
271 assert(dest.mask & mask);
272 dest.mask &= mask;
273 return dest;
274 }
275
276
277 static INLINE SVGA3dShaderSrcToken
278 src_token( unsigned file, int number )
279 {
280 SVGA3dShaderSrcToken src;
281
282 src.value = 0;
283 src.num = number;
284 src.type_upper = file >> 3;
285 src.relAddr = 0;
286 src.reserved1 = 0;
287 src.swizzle = SWIZZLE_XYZW;
288 src.srcMod = 0;
289 src.type_lower = file & 0x7;
290 src.reserved0 = 1; /* is_reg */
291
292 return src;
293 }
294
295
296 static INLINE struct src_register
297 absolute( struct src_register src )
298 {
299 src.base.srcMod = SVGA3DSRCMOD_ABS;
300
301 return src;
302 }
303
304
305 static INLINE struct src_register
306 negate( struct src_register src )
307 {
308 switch (src.base.srcMod) {
309 case SVGA3DSRCMOD_ABS:
310 src.base.srcMod = SVGA3DSRCMOD_ABSNEG;
311 break;
312 case SVGA3DSRCMOD_ABSNEG:
313 src.base.srcMod = SVGA3DSRCMOD_ABS;
314 break;
315 case SVGA3DSRCMOD_NEG:
316 src.base.srcMod = SVGA3DSRCMOD_NONE;
317 break;
318 case SVGA3DSRCMOD_NONE:
319 src.base.srcMod = SVGA3DSRCMOD_NEG;
320 break;
321 }
322 return src;
323 }
324
325
326 static INLINE struct src_register
327 src_register( unsigned file, int number )
328 {
329 struct src_register src;
330
331 src.base = src_token( file, number );
332 src.indirect.value = 0;
333
334 return src;
335 }
336
337 static INLINE SVGA3dShaderDestToken dst( struct src_register src )
338 {
339 return dst_register( SVGA3dShaderGetRegType( src.base.value ),
340 src.base.num );
341 }
342
343 static INLINE struct src_register src( SVGA3dShaderDestToken dst )
344 {
345 return src_register( SVGA3dShaderGetRegType( dst.value ),
346 dst.num );
347 }
348
349 static INLINE ubyte svga_tgsi_sampler_type( struct svga_shader_emitter *emit,
350 int idx )
351 {
352 switch (emit->key.fkey.tex[idx].texture_target) {
353 case PIPE_TEXTURE_1D:
354 return SVGA3DSAMP_2D;
355 case PIPE_TEXTURE_2D:
356 case PIPE_TEXTURE_RECT:
357 return SVGA3DSAMP_2D;
358 case PIPE_TEXTURE_3D:
359 return SVGA3DSAMP_VOLUME;
360 case PIPE_TEXTURE_CUBE:
361 return SVGA3DSAMP_CUBE;
362 }
363
364 return SVGA3DSAMP_UNKNOWN;
365 }
366
367 #endif