43fc0d3235969bb24a3e3403f2c0c92843679b58
[mesa.git] / src / gallium / drivers / svga / svga_tgsi_decl_sm30.c
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
27 #include "pipe/p_shader_tokens.h"
28 #include "tgsi/tgsi_parse.h"
29 #include "util/u_memory.h"
30
31 #include "svga_tgsi_emit.h"
32
33 static boolean translate_vs_ps_semantic( struct tgsi_declaration_semantic semantic,
34 unsigned *usage,
35 unsigned *idx )
36 {
37 switch (semantic.Name) {
38 case TGSI_SEMANTIC_POSITION:
39 *idx = semantic.Index;
40 *usage = SVGA3D_DECLUSAGE_POSITION;
41 break;
42 case TGSI_SEMANTIC_COLOR:
43
44 *idx = semantic.Index;
45 *usage = SVGA3D_DECLUSAGE_COLOR;
46 break;
47 case TGSI_SEMANTIC_BCOLOR:
48 *idx = semantic.Index + 2; /* sharing with COLOR */
49 *usage = SVGA3D_DECLUSAGE_COLOR;
50 break;
51 case TGSI_SEMANTIC_FOG:
52 *idx = 0;
53 assert(semantic.Index == 0);
54 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
55 break;
56 case TGSI_SEMANTIC_PSIZE:
57 *idx = semantic.Index;
58 *usage = SVGA3D_DECLUSAGE_PSIZE;
59 break;
60 case TGSI_SEMANTIC_GENERIC:
61 *idx = semantic.Index + 1; /* texcoord[0] is reserved for fog */
62 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
63 break;
64 case TGSI_SEMANTIC_NORMAL:
65 *idx = semantic.Index;
66 *usage = SVGA3D_DECLUSAGE_NORMAL;
67 break;
68 default:
69 assert(0);
70 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
71 *idx = 0;
72 return FALSE;
73 }
74
75 return TRUE;
76 }
77
78
79 static boolean emit_decl( struct svga_shader_emitter *emit,
80 SVGA3dShaderDestToken reg,
81 unsigned usage,
82 unsigned index )
83 {
84 SVGA3DOpDclArgs dcl;
85 SVGA3dShaderInstToken opcode;
86
87 opcode = inst_token( SVGA3DOP_DCL );
88 dcl.values[0] = 0;
89 dcl.values[1] = 0;
90
91 dcl.dst = reg;
92 dcl.usage = usage;
93 dcl.index = index;
94 dcl.values[0] |= 1<<31;
95
96 return (emit_instruction(emit, opcode) &&
97 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
98 }
99
100 static boolean emit_vface_decl( struct svga_shader_emitter *emit )
101 {
102 if (!emit->emitted_vface) {
103 SVGA3dShaderDestToken reg =
104 dst_register( SVGA3DREG_MISCTYPE,
105 SVGA3DMISCREG_FACE );
106
107 if (!emit_decl( emit, reg, 0, 0 ))
108 return FALSE;
109
110 emit->emitted_vface = TRUE;
111 }
112 return TRUE;
113 }
114
115 static boolean ps30_input( struct svga_shader_emitter *emit,
116 struct tgsi_declaration_semantic semantic,
117 unsigned idx )
118 {
119 unsigned usage, index;
120 SVGA3dShaderDestToken reg;
121
122 if (semantic.Name == TGSI_SEMANTIC_POSITION) {
123 emit->input_map[idx] = src_register( SVGA3DREG_MISCTYPE,
124 SVGA3DMISCREG_POSITION );
125
126 emit->input_map[idx].base.swizzle = TRANSLATE_SWIZZLE( TGSI_SWIZZLE_X,
127 TGSI_SWIZZLE_Y,
128 TGSI_SWIZZLE_Y,
129 TGSI_SWIZZLE_Y );
130
131 reg = writemask( dst(emit->input_map[idx]),
132 TGSI_WRITEMASK_XY );
133
134 return emit_decl( emit, reg, 0, 0 );
135 }
136 else if (emit->key.fkey.light_twoside &&
137 (semantic.Name == TGSI_SEMANTIC_COLOR)) {
138
139 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
140 return FALSE;
141
142 emit->internal_color_idx[emit->internal_color_count] = idx;
143 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count );
144 emit->ps30_input_count++;
145 emit->internal_color_count++;
146
147 reg = dst( emit->input_map[idx] );
148
149 if (!emit_decl( emit, reg, usage, index ))
150 return FALSE;
151
152 semantic.Name = TGSI_SEMANTIC_BCOLOR;
153 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
154 return FALSE;
155
156 reg = dst_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
157
158 if (!emit_decl( emit, reg, usage, index ))
159 return FALSE;
160
161 if (!emit_vface_decl( emit ))
162 return FALSE;
163
164 return TRUE;
165 }
166 else if (semantic.Name == TGSI_SEMANTIC_FACE) {
167 if (!emit_vface_decl( emit ))
168 return FALSE;
169 emit->emit_frontface = TRUE;
170 emit->internal_frontface_idx = idx;
171 return TRUE;
172 }
173 else {
174
175 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
176 return FALSE;
177
178 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
179 reg = dst( emit->input_map[idx] );
180
181 return emit_decl( emit, reg, usage, index );
182 }
183
184 }
185
186
187 /* PS output registers are the same as 2.0
188 */
189 static boolean ps30_output( struct svga_shader_emitter *emit,
190 struct tgsi_declaration_semantic semantic,
191 unsigned idx )
192 {
193 SVGA3dShaderDestToken reg;
194
195 switch (semantic.Name) {
196 case TGSI_SEMANTIC_COLOR:
197 emit->output_map[idx] = dst_register( SVGA3DREG_COLOROUT,
198 semantic.Index );
199 break;
200 case TGSI_SEMANTIC_POSITION:
201 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
202 emit->nr_hw_temp++ );
203 emit->temp_pos = emit->output_map[idx];
204 emit->true_pos = dst_register( SVGA3DREG_DEPTHOUT,
205 semantic.Index );
206 break;
207 default:
208 assert(0);
209 reg = dst_register( SVGA3DREG_COLOROUT, 0 );
210 break;
211 }
212
213 return TRUE;
214 }
215
216
217 /* We still make up the input semantics the same as in 2.0
218 */
219 static boolean vs30_input( struct svga_shader_emitter *emit,
220 struct tgsi_declaration_semantic semantic,
221 unsigned idx )
222 {
223 SVGA3DOpDclArgs dcl;
224 SVGA3dShaderInstToken opcode;
225 unsigned usage, index;
226
227 opcode = inst_token( SVGA3DOP_DCL );
228 dcl.values[0] = 0;
229 dcl.values[1] = 0;
230
231 if (emit->key.vkey.zero_stride_vertex_elements & (1 << idx)) {
232 unsigned i;
233 unsigned offset = 0;
234 unsigned start_idx = emit->info.file_max[TGSI_FILE_CONSTANT] + 1;
235 /* adjust for prescale constants */
236 start_idx += emit->key.vkey.need_prescale ? 2 : 0;
237 /* compute the offset from the start of zero stride constants */
238 for (i = 0; i < PIPE_MAX_ATTRIBS && i < idx; ++i) {
239 if (emit->key.vkey.zero_stride_vertex_elements & (1<<i))
240 ++offset;
241 }
242 emit->input_map[idx] = src_register( SVGA3DREG_CONST,
243 start_idx + offset );
244 } else {
245 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, idx );
246 dcl.dst = dst_register( SVGA3DREG_INPUT, idx );
247
248 assert(dcl.dst.reserved0);
249
250 svga_generate_vdecl_semantics( idx, &usage, &index );
251
252 dcl.usage = usage;
253 dcl.index = index;
254 dcl.values[0] |= 1<<31;
255
256 return (emit_instruction(emit, opcode) &&
257 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
258 }
259 return TRUE;
260 }
261
262 /* VS3.0 outputs have proper declarations and semantic info for
263 * matching against PS inputs.
264 */
265 static boolean vs30_output( struct svga_shader_emitter *emit,
266 struct tgsi_declaration_semantic semantic,
267 unsigned idx )
268 {
269 SVGA3DOpDclArgs dcl;
270 SVGA3dShaderInstToken opcode;
271 unsigned usage, index;
272
273 opcode = inst_token( SVGA3DOP_DCL );
274 dcl.values[0] = 0;
275 dcl.values[1] = 0;
276
277 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
278 return FALSE;
279
280 dcl.dst = dst_register( SVGA3DREG_OUTPUT, idx );
281 dcl.usage = usage;
282 dcl.index = index;
283 dcl.values[0] |= 1<<31;
284
285 if (semantic.Name == TGSI_SEMANTIC_POSITION) {
286 assert(idx == 0);
287 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
288 emit->nr_hw_temp++ );
289 emit->temp_pos = emit->output_map[idx];
290 emit->true_pos = dcl.dst;
291 }
292 else if (semantic.Name == TGSI_SEMANTIC_PSIZE) {
293 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
294 emit->nr_hw_temp++ );
295 emit->temp_psiz = emit->output_map[idx];
296
297 /* This has the effect of not declaring psiz (below) and not
298 * emitting the final MOV to true_psiz in the postamble.
299 */
300 if (!emit->key.vkey.allow_psiz)
301 return TRUE;
302
303 emit->true_psiz = dcl.dst;
304 }
305 else {
306 emit->output_map[idx] = dcl.dst;
307 }
308
309
310 return (emit_instruction(emit, opcode) &&
311 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
312 }
313
314 static boolean ps30_sampler( struct svga_shader_emitter *emit,
315 struct tgsi_declaration_semantic semantic,
316 unsigned idx )
317 {
318 SVGA3DOpDclArgs dcl;
319 SVGA3dShaderInstToken opcode;
320
321 opcode = inst_token( SVGA3DOP_DCL );
322 dcl.values[0] = 0;
323 dcl.values[1] = 0;
324
325 dcl.dst = dst_register( SVGA3DREG_SAMPLER, idx );
326 dcl.type = svga_tgsi_sampler_type( emit, idx );
327 dcl.values[0] |= 1<<31;
328
329 return (emit_instruction(emit, opcode) &&
330 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
331 }
332
333
334 boolean svga_translate_decl_sm30( struct svga_shader_emitter *emit,
335 const struct tgsi_full_declaration *decl )
336 {
337 unsigned first = decl->Range.First;
338 unsigned last = decl->Range.Last;
339 unsigned semantic = 0;
340 unsigned semantic_idx = 0;
341 unsigned idx;
342
343 if (decl->Declaration.Semantic) {
344 semantic = decl->Semantic.Name;
345 semantic_idx = decl->Semantic.Index;
346 }
347
348 for( idx = first; idx <= last; idx++ ) {
349 boolean ok;
350
351 switch (decl->Declaration.File) {
352 case TGSI_FILE_SAMPLER:
353 assert (emit->unit == PIPE_SHADER_FRAGMENT);
354 ok = ps30_sampler( emit, decl->Semantic, idx );
355 break;
356
357 case TGSI_FILE_INPUT:
358 if (emit->unit == PIPE_SHADER_VERTEX)
359 ok = vs30_input( emit, decl->Semantic, idx );
360 else
361 ok = ps30_input( emit, decl->Semantic, idx );
362 break;
363
364 case TGSI_FILE_OUTPUT:
365 if (emit->unit == PIPE_SHADER_VERTEX)
366 ok = vs30_output( emit, decl->Semantic, idx );
367 else
368 ok = ps30_output( emit, decl->Semantic, idx );
369 break;
370
371 default:
372 /* don't need to declare other vars */
373 ok = TRUE;
374 }
375
376 if (!ok)
377 return FALSE;
378 }
379
380 return TRUE;
381 }
382
383
384