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
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"
32 #include "svga_context.h"
34 static boolean
translate_vs_ps_semantic( struct tgsi_declaration_semantic semantic
,
38 switch (semantic
.SemanticName
) {
39 case TGSI_SEMANTIC_POSITION
:
40 *idx
= semantic
.SemanticIndex
;
41 *usage
= SVGA3D_DECLUSAGE_POSITION
;
43 case TGSI_SEMANTIC_COLOR
:
45 *idx
= semantic
.SemanticIndex
;
46 *usage
= SVGA3D_DECLUSAGE_COLOR
;
48 case TGSI_SEMANTIC_BCOLOR
:
49 *idx
= semantic
.SemanticIndex
+ 2; /* sharing with COLOR */
50 *usage
= SVGA3D_DECLUSAGE_COLOR
;
52 case TGSI_SEMANTIC_FOG
:
54 assert(semantic
.SemanticIndex
== 0);
55 *usage
= SVGA3D_DECLUSAGE_TEXCOORD
;
57 case TGSI_SEMANTIC_PSIZE
:
58 *idx
= semantic
.SemanticIndex
;
59 *usage
= SVGA3D_DECLUSAGE_PSIZE
;
61 case TGSI_SEMANTIC_GENERIC
:
62 *idx
= semantic
.SemanticIndex
+ 1; /* texcoord[0] is reserved for fog */
63 *usage
= SVGA3D_DECLUSAGE_TEXCOORD
;
65 case TGSI_SEMANTIC_NORMAL
:
66 *idx
= semantic
.SemanticIndex
;
67 *usage
= SVGA3D_DECLUSAGE_NORMAL
;
71 *usage
= SVGA3D_DECLUSAGE_TEXCOORD
;
80 static boolean
emit_decl( struct svga_shader_emitter
*emit
,
81 SVGA3dShaderDestToken reg
,
86 SVGA3dShaderInstToken opcode
;
88 opcode
= inst_token( SVGA3DOP_DCL
);
95 dcl
.values
[0] |= 1<<31;
97 return (emit_instruction(emit
, opcode
) &&
98 svga_shader_emit_dwords( emit
, dcl
.values
, Elements(dcl
.values
)));
101 static boolean
emit_vface_decl( struct svga_shader_emitter
*emit
)
103 if (!emit
->emitted_vface
) {
104 SVGA3dShaderDestToken reg
=
105 dst_register( SVGA3DREG_MISCTYPE
,
106 SVGA3DMISCREG_FACE
);
108 if (!emit_decl( emit
, reg
, 0, 0 ))
111 emit
->emitted_vface
= TRUE
;
116 static boolean
ps30_input( struct svga_shader_emitter
*emit
,
117 struct tgsi_declaration_semantic semantic
,
120 unsigned usage
, index
;
121 SVGA3dShaderDestToken reg
;
123 if (semantic
.SemanticName
== TGSI_SEMANTIC_POSITION
) {
124 emit
->input_map
[idx
] = src_register( SVGA3DREG_MISCTYPE
,
125 SVGA3DMISCREG_POSITION
);
127 emit
->input_map
[idx
].base
.swizzle
= TRANSLATE_SWIZZLE( TGSI_SWIZZLE_X
,
132 reg
= writemask( dst(emit
->input_map
[idx
]),
135 return emit_decl( emit
, reg
, 0, 0 );
137 else if (emit
->key
.fkey
.light_twoside
&&
138 (semantic
.SemanticName
== TGSI_SEMANTIC_COLOR
)) {
140 if (!translate_vs_ps_semantic( semantic
, &usage
, &index
))
143 emit
->internal_color_idx
[emit
->internal_color_count
] = idx
;
144 emit
->input_map
[idx
] = src_register( SVGA3DREG_INPUT
, emit
->ps30_input_count
);
145 emit
->ps30_input_count
++;
146 emit
->internal_color_count
++;
148 reg
= dst( emit
->input_map
[idx
] );
150 if (!emit_decl( emit
, reg
, usage
, index
))
153 semantic
.SemanticName
= TGSI_SEMANTIC_BCOLOR
;
154 if (!translate_vs_ps_semantic( semantic
, &usage
, &index
))
157 reg
= dst_register( SVGA3DREG_INPUT
, emit
->ps30_input_count
++ );
159 if (!emit_decl( emit
, reg
, usage
, index
))
162 if (!emit_vface_decl( emit
))
167 else if (semantic
.SemanticName
== TGSI_SEMANTIC_FACE
) {
168 if (!emit_vface_decl( emit
))
170 emit
->emit_frontface
= TRUE
;
171 emit
->internal_frontface_idx
= idx
;
176 if (!translate_vs_ps_semantic( semantic
, &usage
, &index
))
179 emit
->input_map
[idx
] = src_register( SVGA3DREG_INPUT
, emit
->ps30_input_count
++ );
180 reg
= dst( emit
->input_map
[idx
] );
182 return emit_decl( emit
, reg
, usage
, index
);
188 /* PS output registers are the same as 2.0
190 static boolean
ps30_output( struct svga_shader_emitter
*emit
,
191 struct tgsi_declaration_semantic semantic
,
194 SVGA3dShaderDestToken reg
;
196 switch (semantic
.SemanticName
) {
197 case TGSI_SEMANTIC_COLOR
:
198 emit
->output_map
[idx
] = dst_register( SVGA3DREG_COLOROUT
,
199 semantic
.SemanticIndex
);
201 case TGSI_SEMANTIC_POSITION
:
202 emit
->output_map
[idx
] = dst_register( SVGA3DREG_TEMP
,
203 emit
->nr_hw_temp
++ );
204 emit
->temp_pos
= emit
->output_map
[idx
];
205 emit
->true_pos
= dst_register( SVGA3DREG_DEPTHOUT
,
206 semantic
.SemanticIndex
);
210 reg
= dst_register( SVGA3DREG_COLOROUT
, 0 );
218 /* We still make up the input semantics the same as in 2.0
220 static boolean
vs30_input( struct svga_shader_emitter
*emit
,
221 struct tgsi_declaration_semantic semantic
,
225 SVGA3dShaderInstToken opcode
;
226 unsigned usage
, index
;
228 opcode
= inst_token( SVGA3DOP_DCL
);
232 if (emit
->key
.vkey
.zero_stride_vertex_elements
& (1 << idx
)) {
235 unsigned start_idx
= emit
->info
.file_max
[TGSI_FILE_CONSTANT
] + 1;
236 /* adjust for prescale constants */
237 start_idx
+= emit
->key
.vkey
.need_prescale
? 2 : 0;
238 /* compute the offset from the start of zero stride constants */
239 for (i
= 0; i
< PIPE_MAX_ATTRIBS
&& i
< idx
; ++i
) {
240 if (emit
->key
.vkey
.zero_stride_vertex_elements
& (1<<i
))
243 emit
->input_map
[idx
] = src_register( SVGA3DREG_CONST
,
244 start_idx
+ offset
);
246 emit
->input_map
[idx
] = src_register( SVGA3DREG_INPUT
, idx
);
247 dcl
.dst
= dst_register( SVGA3DREG_INPUT
, idx
);
249 assert(dcl
.dst
.reserved0
);
251 svga_generate_vdecl_semantics( idx
, &usage
, &index
);
255 dcl
.values
[0] |= 1<<31;
257 return (emit_instruction(emit
, opcode
) &&
258 svga_shader_emit_dwords( emit
, dcl
.values
, Elements(dcl
.values
)));
263 /* VS3.0 outputs have proper declarations and semantic info for
264 * matching against PS inputs.
266 static boolean
vs30_output( struct svga_shader_emitter
*emit
,
267 struct tgsi_declaration_semantic semantic
,
271 SVGA3dShaderInstToken opcode
;
272 unsigned usage
, index
;
274 opcode
= inst_token( SVGA3DOP_DCL
);
278 if (!translate_vs_ps_semantic( semantic
, &usage
, &index
))
281 dcl
.dst
= dst_register( SVGA3DREG_OUTPUT
, idx
);
284 dcl
.values
[0] |= 1<<31;
286 if (semantic
.SemanticName
== TGSI_SEMANTIC_POSITION
) {
288 emit
->output_map
[idx
] = dst_register( SVGA3DREG_TEMP
,
289 emit
->nr_hw_temp
++ );
290 emit
->temp_pos
= emit
->output_map
[idx
];
291 emit
->true_pos
= dcl
.dst
;
293 else if (semantic
.SemanticName
== TGSI_SEMANTIC_PSIZE
) {
294 emit
->output_map
[idx
] = dst_register( SVGA3DREG_TEMP
,
295 emit
->nr_hw_temp
++ );
296 emit
->temp_psiz
= emit
->output_map
[idx
];
298 /* This has the effect of not declaring psiz (below) and not
299 * emitting the final MOV to true_psiz in the postamble.
301 if (!emit
->key
.vkey
.allow_psiz
)
304 emit
->true_psiz
= dcl
.dst
;
307 emit
->output_map
[idx
] = dcl
.dst
;
311 return (emit_instruction(emit
, opcode
) &&
312 svga_shader_emit_dwords( emit
, dcl
.values
, Elements(dcl
.values
)));
315 static boolean
ps30_sampler( struct svga_shader_emitter
*emit
,
316 struct tgsi_declaration_semantic semantic
,
320 SVGA3dShaderInstToken opcode
;
322 opcode
= inst_token( SVGA3DOP_DCL
);
326 dcl
.dst
= dst_register( SVGA3DREG_SAMPLER
, idx
);
327 dcl
.type
= svga_tgsi_sampler_type( emit
, idx
);
328 dcl
.values
[0] |= 1<<31;
330 return (emit_instruction(emit
, opcode
) &&
331 svga_shader_emit_dwords( emit
, dcl
.values
, Elements(dcl
.values
)));
335 boolean
svga_translate_decl_sm30( struct svga_shader_emitter
*emit
,
336 const struct tgsi_full_declaration
*decl
)
338 unsigned first
= decl
->DeclarationRange
.First
;
339 unsigned last
= decl
->DeclarationRange
.Last
;
340 unsigned semantic
= 0;
341 unsigned semantic_idx
= 0;
344 if (decl
->Declaration
.Semantic
) {
345 semantic
= decl
->Semantic
.SemanticName
;
346 semantic_idx
= decl
->Semantic
.SemanticIndex
;
349 for( idx
= first
; idx
<= last
; idx
++ ) {
352 switch (decl
->Declaration
.File
) {
353 case TGSI_FILE_SAMPLER
:
354 assert (emit
->unit
== PIPE_SHADER_FRAGMENT
);
355 ok
= ps30_sampler( emit
, decl
->Semantic
, idx
);
358 case TGSI_FILE_INPUT
:
359 if (emit
->unit
== PIPE_SHADER_VERTEX
)
360 ok
= vs30_input( emit
, decl
->Semantic
, idx
);
362 ok
= ps30_input( emit
, decl
->Semantic
, idx
);
365 case TGSI_FILE_OUTPUT
:
366 if (emit
->unit
== PIPE_SHADER_VERTEX
)
367 ok
= vs30_output( emit
, decl
->Semantic
, idx
);
369 ok
= ps30_output( emit
, decl
->Semantic
, idx
);
373 /* don't need to declare other vars */