svga: move some svga/tgsi functions
[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 unsigned size;
54 char *buf;
55 char *ptr;
56
57 struct svga_compile_key key;
58 struct tgsi_shader_info info;
59 int unit;
60
61 int imm_start;
62
63 int nr_hw_float_const;
64 int nr_hw_int_const;
65 int nr_hw_temp;
66
67 int insn_offset;
68
69 int internal_temp_count;
70 int internal_imm_count;
71
72 int internal_color_idx[2]; /* diffuse, specular */
73 int internal_color_count;
74
75 boolean emitted_vface;
76 boolean emit_frontface;
77 int internal_frontface_idx;
78
79 int ps30_input_count;
80 int vs30_output_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 unsigned inverted_texcoords; /**< bitmask of which texcoords are flipped */
93 struct src_register ps_true_texcoord[PIPE_MAX_ATTRIBS];
94 struct src_register ps_inverted_texcoord[PIPE_MAX_ATTRIBS];
95 unsigned ps_inverted_texcoord_input[PIPE_MAX_ATTRIBS];
96
97 unsigned label[32];
98 unsigned nr_labels;
99
100 struct src_register input_map[PIPE_MAX_ATTRIBS];
101 SVGA3dShaderDestToken output_map[PIPE_MAX_ATTRIBS];
102
103 boolean ps_reads_pos;
104 boolean emitted_depth_fog;
105 struct src_register ps_true_pos;
106 struct src_register ps_depth_pos;
107 SVGA3dShaderDestToken ps_temp_pos;
108
109 /* shared input for depth and fog */
110 struct src_register ps_depth_fog;
111
112 struct src_register imm_0055;
113 SVGA3dShaderDestToken temp_pos;
114 SVGA3dShaderDestToken true_pos;
115 SVGA3dShaderDestToken depth_pos;
116
117 /* shared output for depth and fog */
118 SVGA3dShaderDestToken vs_depth_fog;
119
120 SVGA3dShaderDestToken temp_col[PIPE_MAX_COLOR_BUFS];
121 SVGA3dShaderDestToken true_col[PIPE_MAX_COLOR_BUFS];
122
123 SVGA3dShaderDestToken temp_psiz;
124 SVGA3dShaderDestToken true_psiz;
125
126 struct svga_arl_consts arl_consts[12];
127 int num_arl_consts;
128 int current_arl;
129 };
130
131
132 boolean svga_shader_emit_dword( struct svga_shader_emitter *emit,
133 unsigned dword );
134
135 boolean svga_shader_emit_dwords( struct svga_shader_emitter *emit,
136 const unsigned *dwords,
137 unsigned nr );
138
139 boolean svga_shader_emit_opcode( struct svga_shader_emitter *emit,
140 unsigned opcode );
141
142 boolean svga_shader_emit_instructions( struct svga_shader_emitter *emit,
143 const struct tgsi_token *tokens );
144
145 boolean svga_translate_decl_sm30( struct svga_shader_emitter *emit,
146 const struct tgsi_full_declaration *decl );
147
148
149 static INLINE boolean emit_instruction( struct svga_shader_emitter *emit,
150 SVGA3dShaderInstToken opcode )
151 {
152 return svga_shader_emit_opcode( emit, opcode.value );
153 }
154
155
156 #define TRANSLATE_SWIZZLE(x,y,z,w) ((x) | ((y) << 2) | ((z) << 4) | ((w) << 6))
157 #define SWIZZLE_XYZW \
158 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_W)
159 #define SWIZZLE_XXXX \
160 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X,TGSI_SWIZZLE_X)
161 #define SWIZZLE_YYYY \
162 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y,TGSI_SWIZZLE_Y)
163 #define SWIZZLE_ZZZZ \
164 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z,TGSI_SWIZZLE_Z)
165 #define SWIZZLE_WWWW \
166 TRANSLATE_SWIZZLE(TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W,TGSI_SWIZZLE_W)
167
168
169
170 static INLINE SVGA3dShaderInstToken
171 inst_token( unsigned opcode )
172 {
173 SVGA3dShaderInstToken inst;
174
175 inst.value = 0;
176 inst.op = opcode;
177
178 return inst;
179 }
180
181
182 /**
183 * Create an instance of a SVGA3dShaderDestToken.
184 * Note that this function is used to create tokens for output registers,
185 * temp registers AND constants (see emit_def_const()).
186 */
187 static INLINE SVGA3dShaderDestToken
188 dst_register( unsigned file,
189 int number )
190 {
191 SVGA3dShaderDestToken dest;
192
193 /* check values against bitfield sizes */
194 assert(number < (1 << 11));
195 assert(file <= SVGA3DREG_PREDICATE);
196
197 dest.value = 0;
198 dest.num = number;
199 dest.type_upper = file >> 3;
200 dest.relAddr = 0;
201 dest.reserved1 = 0;
202 dest.mask = 0xf;
203 dest.dstMod = 0;
204 dest.shfScale = 0;
205 dest.type_lower = file & 0x7;
206 dest.reserved0 = 1; /* is_reg */
207
208 return dest;
209 }
210
211 static INLINE SVGA3dShaderDestToken
212 writemask( SVGA3dShaderDestToken dest,
213 unsigned mask )
214 {
215 assert(dest.mask & mask);
216 dest.mask &= mask;
217 return dest;
218 }
219
220
221 static INLINE SVGA3dShaderSrcToken
222 src_token( unsigned file, int number )
223 {
224 SVGA3dShaderSrcToken src;
225
226 /* check values against bitfield sizes */
227 assert(number < (1 << 11));
228 assert(file <= SVGA3DREG_PREDICATE);
229
230 src.value = 0;
231 src.num = number;
232 src.type_upper = file >> 3;
233 src.relAddr = 0;
234 src.reserved1 = 0;
235 src.swizzle = SWIZZLE_XYZW;
236 src.srcMod = 0;
237 src.type_lower = file & 0x7;
238 src.reserved0 = 1; /* is_reg */
239
240 return src;
241 }
242
243
244 static INLINE struct src_register
245 src_register( unsigned file, int number )
246 {
247 struct src_register src;
248
249 src.base = src_token( file, number );
250 src.indirect.value = 0;
251
252 return src;
253 }
254
255 static INLINE SVGA3dShaderDestToken dst( struct src_register src )
256 {
257 return dst_register( SVGA3dShaderGetRegType( src.base.value ),
258 src.base.num );
259 }
260
261 static INLINE struct src_register src( SVGA3dShaderDestToken dst )
262 {
263 return src_register( SVGA3dShaderGetRegType( dst.value ),
264 dst.num );
265 }
266
267
268 #endif