gallium: Remove TGSI_SEMANTIC_NORMAL.
[mesa/mesa-lb.git] / src / gallium / drivers / svga / svga_tgsi_decl_sm30.c
blob05d910210ad0ae355397d5ba823e7a663f953428
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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.
24 **********************************************************/
27 #include "pipe/p_shader_tokens.h"
28 #include "tgsi/tgsi_parse.h"
29 #include "util/u_memory.h"
31 #include "svga_tgsi_emit.h"
33 static boolean translate_vs_ps_semantic( struct tgsi_declaration_semantic semantic,
34 unsigned *usage,
35 unsigned *idx )
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:
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 default:
65 assert(0);
66 *usage = SVGA3D_DECLUSAGE_TEXCOORD;
67 *idx = 0;
68 return FALSE;
71 return TRUE;
75 static boolean emit_decl( struct svga_shader_emitter *emit,
76 SVGA3dShaderDestToken reg,
77 unsigned usage,
78 unsigned index )
80 SVGA3DOpDclArgs dcl;
81 SVGA3dShaderInstToken opcode;
83 opcode = inst_token( SVGA3DOP_DCL );
84 dcl.values[0] = 0;
85 dcl.values[1] = 0;
87 dcl.dst = reg;
88 dcl.usage = usage;
89 dcl.index = index;
90 dcl.values[0] |= 1<<31;
92 return (emit_instruction(emit, opcode) &&
93 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
96 static boolean emit_vface_decl( struct svga_shader_emitter *emit )
98 if (!emit->emitted_vface) {
99 SVGA3dShaderDestToken reg =
100 dst_register( SVGA3DREG_MISCTYPE,
101 SVGA3DMISCREG_FACE );
103 if (!emit_decl( emit, reg, 0, 0 ))
104 return FALSE;
106 emit->emitted_vface = TRUE;
108 return TRUE;
111 static boolean ps30_input( struct svga_shader_emitter *emit,
112 struct tgsi_declaration_semantic semantic,
113 unsigned idx )
115 unsigned usage, index;
116 SVGA3dShaderDestToken reg;
118 if (semantic.Name == TGSI_SEMANTIC_POSITION) {
119 emit->input_map[idx] = src_register( SVGA3DREG_MISCTYPE,
120 SVGA3DMISCREG_POSITION );
122 emit->input_map[idx].base.swizzle = TRANSLATE_SWIZZLE( TGSI_SWIZZLE_X,
123 TGSI_SWIZZLE_Y,
124 TGSI_SWIZZLE_Y,
125 TGSI_SWIZZLE_Y );
127 reg = writemask( dst(emit->input_map[idx]),
128 TGSI_WRITEMASK_XY );
130 return emit_decl( emit, reg, 0, 0 );
132 else if (emit->key.fkey.light_twoside &&
133 (semantic.Name == TGSI_SEMANTIC_COLOR)) {
135 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
136 return FALSE;
138 emit->internal_color_idx[emit->internal_color_count] = idx;
139 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count );
140 emit->ps30_input_count++;
141 emit->internal_color_count++;
143 reg = dst( emit->input_map[idx] );
145 if (!emit_decl( emit, reg, usage, index ))
146 return FALSE;
148 semantic.Name = TGSI_SEMANTIC_BCOLOR;
149 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
150 return FALSE;
152 reg = dst_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
154 if (!emit_decl( emit, reg, usage, index ))
155 return FALSE;
157 if (!emit_vface_decl( emit ))
158 return FALSE;
160 return TRUE;
162 else if (semantic.Name == TGSI_SEMANTIC_FACE) {
163 if (!emit_vface_decl( emit ))
164 return FALSE;
165 emit->emit_frontface = TRUE;
166 emit->internal_frontface_idx = idx;
167 return TRUE;
169 else {
171 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
172 return FALSE;
174 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
175 reg = dst( emit->input_map[idx] );
177 return emit_decl( emit, reg, usage, index );
183 /* PS output registers are the same as 2.0
185 static boolean ps30_output( struct svga_shader_emitter *emit,
186 struct tgsi_declaration_semantic semantic,
187 unsigned idx )
189 SVGA3dShaderDestToken reg;
191 switch (semantic.Name) {
192 case TGSI_SEMANTIC_COLOR:
193 if (emit->unit == PIPE_SHADER_FRAGMENT &&
194 emit->key.fkey.white_fragments) {
196 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
197 emit->nr_hw_temp++ );
198 emit->temp_col[idx] = emit->output_map[idx];
199 emit->true_col[idx] = dst_register( SVGA3DREG_COLOROUT,
200 semantic.Index );
202 else {
203 emit->output_map[idx] = dst_register( SVGA3DREG_COLOROUT,
204 semantic.Index );
206 break;
207 case TGSI_SEMANTIC_POSITION:
208 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
209 emit->nr_hw_temp++ );
210 emit->temp_pos = emit->output_map[idx];
211 emit->true_pos = dst_register( SVGA3DREG_DEPTHOUT,
212 semantic.Index );
213 break;
214 default:
215 assert(0);
216 reg = dst_register( SVGA3DREG_COLOROUT, 0 );
217 break;
220 return TRUE;
224 /* We still make up the input semantics the same as in 2.0
226 static boolean vs30_input( struct svga_shader_emitter *emit,
227 struct tgsi_declaration_semantic semantic,
228 unsigned idx )
230 SVGA3DOpDclArgs dcl;
231 SVGA3dShaderInstToken opcode;
232 unsigned usage, index;
234 opcode = inst_token( SVGA3DOP_DCL );
235 dcl.values[0] = 0;
236 dcl.values[1] = 0;
238 if (emit->key.vkey.zero_stride_vertex_elements & (1 << idx)) {
239 unsigned i;
240 unsigned offset = 0;
241 unsigned start_idx = emit->info.file_max[TGSI_FILE_CONSTANT] + 1;
242 /* adjust for prescale constants */
243 start_idx += emit->key.vkey.need_prescale ? 2 : 0;
244 /* compute the offset from the start of zero stride constants */
245 for (i = 0; i < PIPE_MAX_ATTRIBS && i < idx; ++i) {
246 if (emit->key.vkey.zero_stride_vertex_elements & (1<<i))
247 ++offset;
249 emit->input_map[idx] = src_register( SVGA3DREG_CONST,
250 start_idx + offset );
251 } else {
252 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, idx );
253 dcl.dst = dst_register( SVGA3DREG_INPUT, idx );
255 assert(dcl.dst.reserved0);
257 svga_generate_vdecl_semantics( idx, &usage, &index );
259 dcl.usage = usage;
260 dcl.index = index;
261 dcl.values[0] |= 1<<31;
263 return (emit_instruction(emit, opcode) &&
264 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
266 return TRUE;
269 /* VS3.0 outputs have proper declarations and semantic info for
270 * matching against PS inputs.
272 static boolean vs30_output( struct svga_shader_emitter *emit,
273 struct tgsi_declaration_semantic semantic,
274 unsigned idx )
276 SVGA3DOpDclArgs dcl;
277 SVGA3dShaderInstToken opcode;
278 unsigned usage, index;
280 opcode = inst_token( SVGA3DOP_DCL );
281 dcl.values[0] = 0;
282 dcl.values[1] = 0;
284 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
285 return FALSE;
287 dcl.dst = dst_register( SVGA3DREG_OUTPUT, idx );
288 dcl.usage = usage;
289 dcl.index = index;
290 dcl.values[0] |= 1<<31;
292 if (semantic.Name == TGSI_SEMANTIC_POSITION) {
293 assert(idx == 0);
294 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
295 emit->nr_hw_temp++ );
296 emit->temp_pos = emit->output_map[idx];
297 emit->true_pos = dcl.dst;
299 else if (semantic.Name == TGSI_SEMANTIC_PSIZE) {
300 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
301 emit->nr_hw_temp++ );
302 emit->temp_psiz = emit->output_map[idx];
304 /* This has the effect of not declaring psiz (below) and not
305 * emitting the final MOV to true_psiz in the postamble.
307 if (!emit->key.vkey.allow_psiz)
308 return TRUE;
310 emit->true_psiz = dcl.dst;
312 else {
313 emit->output_map[idx] = dcl.dst;
317 return (emit_instruction(emit, opcode) &&
318 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
321 static boolean ps30_sampler( struct svga_shader_emitter *emit,
322 struct tgsi_declaration_semantic semantic,
323 unsigned idx )
325 SVGA3DOpDclArgs dcl;
326 SVGA3dShaderInstToken opcode;
328 opcode = inst_token( SVGA3DOP_DCL );
329 dcl.values[0] = 0;
330 dcl.values[1] = 0;
332 dcl.dst = dst_register( SVGA3DREG_SAMPLER, idx );
333 dcl.type = svga_tgsi_sampler_type( emit, idx );
334 dcl.values[0] |= 1<<31;
336 return (emit_instruction(emit, opcode) &&
337 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
341 boolean svga_translate_decl_sm30( struct svga_shader_emitter *emit,
342 const struct tgsi_full_declaration *decl )
344 unsigned first = decl->Range.First;
345 unsigned last = decl->Range.Last;
346 unsigned semantic = 0;
347 unsigned semantic_idx = 0;
348 unsigned idx;
350 if (decl->Declaration.Semantic) {
351 semantic = decl->Semantic.Name;
352 semantic_idx = decl->Semantic.Index;
355 for( idx = first; idx <= last; idx++ ) {
356 boolean ok;
358 switch (decl->Declaration.File) {
359 case TGSI_FILE_SAMPLER:
360 assert (emit->unit == PIPE_SHADER_FRAGMENT);
361 ok = ps30_sampler( emit, decl->Semantic, idx );
362 break;
364 case TGSI_FILE_INPUT:
365 if (emit->unit == PIPE_SHADER_VERTEX)
366 ok = vs30_input( emit, decl->Semantic, idx );
367 else
368 ok = ps30_input( emit, decl->Semantic, idx );
369 break;
371 case TGSI_FILE_OUTPUT:
372 if (emit->unit == PIPE_SHADER_VERTEX)
373 ok = vs30_output( emit, decl->Semantic, idx );
374 else
375 ok = ps30_output( emit, decl->Semantic, idx );
376 break;
378 default:
379 /* don't need to declare other vars */
380 ok = TRUE;
383 if (!ok)
384 return FALSE;
387 return TRUE;