Merge commit 'origin/7.8'
[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 if (emit->unit == PIPE_SHADER_FRAGMENT &&
198 emit->key.fkey.white_fragments) {
199
200 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
201 emit->nr_hw_temp++ );
202 emit->temp_col[idx] = emit->output_map[idx];
203 emit->true_col[idx] = dst_register( SVGA3DREG_COLOROUT,
204 semantic.Index );
205 }
206 else {
207 emit->output_map[idx] = dst_register( SVGA3DREG_COLOROUT,
208 semantic.Index );
209 }
210 break;
211 case TGSI_SEMANTIC_POSITION:
212 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
213 emit->nr_hw_temp++ );
214 emit->temp_pos = emit->output_map[idx];
215 emit->true_pos = dst_register( SVGA3DREG_DEPTHOUT,
216 semantic.Index );
217 break;
218 default:
219 assert(0);
220 reg = dst_register( SVGA3DREG_COLOROUT, 0 );
221 break;
222 }
223
224 return TRUE;
225 }
226
227
228 /* We still make up the input semantics the same as in 2.0
229 */
230 static boolean vs30_input( struct svga_shader_emitter *emit,
231 struct tgsi_declaration_semantic semantic,
232 unsigned idx )
233 {
234 SVGA3DOpDclArgs dcl;
235 SVGA3dShaderInstToken opcode;
236 unsigned usage, index;
237
238 opcode = inst_token( SVGA3DOP_DCL );
239 dcl.values[0] = 0;
240 dcl.values[1] = 0;
241
242 if (emit->key.vkey.zero_stride_vertex_elements & (1 << idx)) {
243 unsigned i;
244 unsigned offset = 0;
245 unsigned start_idx = emit->info.file_max[TGSI_FILE_CONSTANT] + 1;
246 /* adjust for prescale constants */
247 start_idx += emit->key.vkey.need_prescale ? 2 : 0;
248 /* compute the offset from the start of zero stride constants */
249 for (i = 0; i < PIPE_MAX_ATTRIBS && i < idx; ++i) {
250 if (emit->key.vkey.zero_stride_vertex_elements & (1<<i))
251 ++offset;
252 }
253 emit->input_map[idx] = src_register( SVGA3DREG_CONST,
254 start_idx + offset );
255 } else {
256 emit->input_map[idx] = src_register( SVGA3DREG_INPUT, idx );
257 dcl.dst = dst_register( SVGA3DREG_INPUT, idx );
258
259 assert(dcl.dst.reserved0);
260
261 svga_generate_vdecl_semantics( idx, &usage, &index );
262
263 dcl.usage = usage;
264 dcl.index = index;
265 dcl.values[0] |= 1<<31;
266
267 return (emit_instruction(emit, opcode) &&
268 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
269 }
270 return TRUE;
271 }
272
273 /* VS3.0 outputs have proper declarations and semantic info for
274 * matching against PS inputs.
275 */
276 static boolean vs30_output( struct svga_shader_emitter *emit,
277 struct tgsi_declaration_semantic semantic,
278 unsigned idx )
279 {
280 SVGA3DOpDclArgs dcl;
281 SVGA3dShaderInstToken opcode;
282 unsigned usage, index;
283
284 opcode = inst_token( SVGA3DOP_DCL );
285 dcl.values[0] = 0;
286 dcl.values[1] = 0;
287
288 if (!translate_vs_ps_semantic( semantic, &usage, &index ))
289 return FALSE;
290
291 dcl.dst = dst_register( SVGA3DREG_OUTPUT, idx );
292 dcl.usage = usage;
293 dcl.index = index;
294 dcl.values[0] |= 1<<31;
295
296 if (semantic.Name == TGSI_SEMANTIC_POSITION) {
297 assert(idx == 0);
298 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
299 emit->nr_hw_temp++ );
300 emit->temp_pos = emit->output_map[idx];
301 emit->true_pos = dcl.dst;
302 }
303 else if (semantic.Name == TGSI_SEMANTIC_PSIZE) {
304 emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
305 emit->nr_hw_temp++ );
306 emit->temp_psiz = emit->output_map[idx];
307
308 /* This has the effect of not declaring psiz (below) and not
309 * emitting the final MOV to true_psiz in the postamble.
310 */
311 if (!emit->key.vkey.allow_psiz)
312 return TRUE;
313
314 emit->true_psiz = dcl.dst;
315 }
316 else {
317 emit->output_map[idx] = dcl.dst;
318 }
319
320
321 return (emit_instruction(emit, opcode) &&
322 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
323 }
324
325 static boolean ps30_sampler( struct svga_shader_emitter *emit,
326 struct tgsi_declaration_semantic semantic,
327 unsigned idx )
328 {
329 SVGA3DOpDclArgs dcl;
330 SVGA3dShaderInstToken opcode;
331
332 opcode = inst_token( SVGA3DOP_DCL );
333 dcl.values[0] = 0;
334 dcl.values[1] = 0;
335
336 dcl.dst = dst_register( SVGA3DREG_SAMPLER, idx );
337 dcl.type = svga_tgsi_sampler_type( emit, idx );
338 dcl.values[0] |= 1<<31;
339
340 return (emit_instruction(emit, opcode) &&
341 svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
342 }
343
344
345 boolean svga_translate_decl_sm30( struct svga_shader_emitter *emit,
346 const struct tgsi_full_declaration *decl )
347 {
348 unsigned first = decl->Range.First;
349 unsigned last = decl->Range.Last;
350 unsigned semantic = 0;
351 unsigned semantic_idx = 0;
352 unsigned idx;
353
354 if (decl->Declaration.Semantic) {
355 semantic = decl->Semantic.Name;
356 semantic_idx = decl->Semantic.Index;
357 }
358
359 for( idx = first; idx <= last; idx++ ) {
360 boolean ok;
361
362 switch (decl->Declaration.File) {
363 case TGSI_FILE_SAMPLER:
364 assert (emit->unit == PIPE_SHADER_FRAGMENT);
365 ok = ps30_sampler( emit, decl->Semantic, idx );
366 break;
367
368 case TGSI_FILE_INPUT:
369 if (emit->unit == PIPE_SHADER_VERTEX)
370 ok = vs30_input( emit, decl->Semantic, idx );
371 else
372 ok = ps30_input( emit, decl->Semantic, idx );
373 break;
374
375 case TGSI_FILE_OUTPUT:
376 if (emit->unit == PIPE_SHADER_VERTEX)
377 ok = vs30_output( emit, decl->Semantic, idx );
378 else
379 ok = ps30_output( emit, decl->Semantic, idx );
380 break;
381
382 default:
383 /* don't need to declare other vars */
384 ok = TRUE;
385 }
386
387 if (!ok)
388 return FALSE;
389 }
390
391 return TRUE;
392 }
393
394
395