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"
33 static boolean
translate_vs_ps_semantic( struct tgsi_declaration_semantic semantic
,
37 switch (semantic
.Name
) {
38 case TGSI_SEMANTIC_POSITION
:
39 *idx
= semantic
.Index
;
40 *usage
= SVGA3D_DECLUSAGE_POSITION
;
42 case TGSI_SEMANTIC_COLOR
:
44 *idx
= semantic
.Index
;
45 *usage
= SVGA3D_DECLUSAGE_COLOR
;
47 case TGSI_SEMANTIC_BCOLOR
:
48 *idx
= semantic
.Index
+ 2; /* sharing with COLOR */
49 *usage
= SVGA3D_DECLUSAGE_COLOR
;
51 case TGSI_SEMANTIC_FOG
:
53 assert(semantic
.Index
== 0);
54 *usage
= SVGA3D_DECLUSAGE_TEXCOORD
;
56 case TGSI_SEMANTIC_PSIZE
:
57 *idx
= semantic
.Index
;
58 *usage
= SVGA3D_DECLUSAGE_PSIZE
;
60 case TGSI_SEMANTIC_GENERIC
:
61 *idx
= semantic
.Index
+ 1; /* texcoord[0] is reserved for fog */
62 *usage
= SVGA3D_DECLUSAGE_TEXCOORD
;
66 *usage
= SVGA3D_DECLUSAGE_TEXCOORD
;
75 static boolean
emit_decl( struct svga_shader_emitter
*emit
,
76 SVGA3dShaderDestToken reg
,
81 SVGA3dShaderInstToken opcode
;
83 opcode
= inst_token( SVGA3DOP_DCL
);
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 ))
106 emit
->emitted_vface
= TRUE
;
111 static boolean
ps30_input( struct svga_shader_emitter
*emit
,
112 struct tgsi_declaration_semantic semantic
,
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
,
127 reg
= writemask( dst(emit
->input_map
[idx
]),
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
))
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
))
148 semantic
.Name
= TGSI_SEMANTIC_BCOLOR
;
149 if (!translate_vs_ps_semantic( semantic
, &usage
, &index
))
152 reg
= dst_register( SVGA3DREG_INPUT
, emit
->ps30_input_count
++ );
154 if (!emit_decl( emit
, reg
, usage
, index
))
157 if (!emit_vface_decl( emit
))
162 else if (semantic
.Name
== TGSI_SEMANTIC_FACE
) {
163 if (!emit_vface_decl( emit
))
165 emit
->emit_frontface
= TRUE
;
166 emit
->internal_frontface_idx
= idx
;
171 if (!translate_vs_ps_semantic( semantic
, &usage
, &index
))
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
,
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
,
203 emit
->output_map
[idx
] = dst_register( SVGA3DREG_COLOROUT
,
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
,
216 reg
= dst_register( SVGA3DREG_COLOROUT
, 0 );
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
,
231 SVGA3dShaderInstToken opcode
;
232 unsigned usage
, index
;
234 opcode
= inst_token( SVGA3DOP_DCL
);
238 if (emit
->key
.vkey
.zero_stride_vertex_elements
& (1 << idx
)) {
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
))
249 emit
->input_map
[idx
] = src_register( SVGA3DREG_CONST
,
250 start_idx
+ offset
);
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
);
261 dcl
.values
[0] |= 1<<31;
263 return (emit_instruction(emit
, opcode
) &&
264 svga_shader_emit_dwords( emit
, dcl
.values
, Elements(dcl
.values
)));
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
,
277 SVGA3dShaderInstToken opcode
;
278 unsigned usage
, index
;
280 opcode
= inst_token( SVGA3DOP_DCL
);
284 if (!translate_vs_ps_semantic( semantic
, &usage
, &index
))
287 dcl
.dst
= dst_register( SVGA3DREG_OUTPUT
, idx
);
290 dcl
.values
[0] |= 1<<31;
292 if (semantic
.Name
== TGSI_SEMANTIC_POSITION
) {
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
)
310 emit
->true_psiz
= dcl
.dst
;
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
,
326 SVGA3dShaderInstToken opcode
;
328 opcode
= inst_token( SVGA3DOP_DCL
);
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;
350 if (decl
->Declaration
.Semantic
) {
351 semantic
= decl
->Semantic
.Name
;
352 semantic_idx
= decl
->Semantic
.Index
;
355 for( idx
= first
; idx
<= last
; idx
++ ) {
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
);
364 case TGSI_FILE_INPUT
:
365 if (emit
->unit
== PIPE_SHADER_VERTEX
)
366 ok
= vs30_input( emit
, decl
->Semantic
, idx
);
368 ok
= ps30_input( emit
, decl
->Semantic
, idx
);
371 case TGSI_FILE_OUTPUT
:
372 if (emit
->unit
== PIPE_SHADER_VERTEX
)
373 ok
= vs30_output( emit
, decl
->Semantic
, idx
);
375 ok
= ps30_output( emit
, decl
->Semantic
, idx
);
379 /* don't need to declare other vars */