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