1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "shader/prog_parameter.h"
35 #include "shader/prog_print.h"
36 #include "tnl/t_vp_build.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_winsys.h"
41 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
42 #include "pipe/tgsi/exec/tgsi_core.h"
44 #include "st_context.h"
47 #include "st_program.h"
54 * Translate a Mesa vertex shader into a TGSI shader.
55 * \param outputMapping to map vertex program output registers to TGSI
57 * \param tokensOut destination for TGSI tokens
58 * \return pointer to cached pipe_shader object.
60 const struct cso_vertex_shader
*
61 st_translate_vertex_program(struct st_context
*st
,
62 struct st_vertex_program
*stvp
,
63 const GLuint outputMapping
[],
64 struct tgsi_token
*tokensOut
,
67 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
68 struct pipe_shader_state vs
;
69 const struct cso_vertex_shader
*cso
;
72 memset(&vs
, 0, sizeof(vs
));
75 * Determine number of inputs, the mappings between VERT_ATTRIB_x
76 * and TGSI generic input indexes, plus input attrib semantic info.
78 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
79 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
80 const GLuint slot
= vs
.num_inputs
;
84 stvp
->input_to_index
[attr
] = slot
;
85 stvp
->index_to_input
[slot
] = attr
;
89 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
90 vs
.input_semantic_index
[slot
] = 0;
92 case VERT_ATTRIB_WEIGHT
:
94 case VERT_ATTRIB_NORMAL
:
95 /* just label as a generic */
96 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
97 vs
.input_semantic_index
[slot
] = 0;
99 case VERT_ATTRIB_COLOR0
:
100 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
101 vs
.input_semantic_index
[slot
] = 0;
103 case VERT_ATTRIB_COLOR1
:
104 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
105 vs
.input_semantic_index
[slot
] = 1;
107 case VERT_ATTRIB_FOG
:
108 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
109 vs
.input_semantic_index
[slot
] = 0;
111 case VERT_ATTRIB_TEX0
:
112 case VERT_ATTRIB_TEX1
:
113 case VERT_ATTRIB_TEX2
:
114 case VERT_ATTRIB_TEX3
:
115 case VERT_ATTRIB_TEX4
:
116 case VERT_ATTRIB_TEX5
:
117 case VERT_ATTRIB_TEX6
:
118 case VERT_ATTRIB_TEX7
:
119 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
120 vs
.input_semantic_index
[slot
] = attr
- VERT_ATTRIB_TEX0
;
122 case VERT_ATTRIB_GENERIC0
:
123 case VERT_ATTRIB_GENERIC1
:
124 case VERT_ATTRIB_GENERIC2
:
125 case VERT_ATTRIB_GENERIC3
:
126 case VERT_ATTRIB_GENERIC4
:
127 case VERT_ATTRIB_GENERIC5
:
128 case VERT_ATTRIB_GENERIC6
:
129 case VERT_ATTRIB_GENERIC7
:
130 assert(attr
< VERT_ATTRIB_MAX
);
131 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
132 vs
.input_semantic_index
[slot
] = attr
- VERT_ATTRIB_GENERIC0
;
140 /* initialize output semantics to defaults */
141 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
142 vs
.output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
143 vs
.output_semantic_index
[i
] = 0;
147 * Determine number of outputs, the (default) output register
148 * mapping and the semantic information for each output.
150 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
151 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
155 slot
= outputMapping
[attr
];
159 slot
= vs
.num_outputs
;
161 defaultOutputMapping
[attr
] = slot
;
165 printf("Output %u -> slot %u\n", attr, slot);
169 case VERT_RESULT_HPOS
:
170 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
171 vs
.output_semantic_index
[slot
] = 0;
173 case VERT_RESULT_COL0
:
174 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
175 vs
.output_semantic_index
[slot
] = 0;
177 case VERT_RESULT_COL1
:
178 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
179 vs
.output_semantic_index
[slot
] = 1;
181 case VERT_RESULT_BFC0
:
182 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
183 vs
.output_semantic_index
[slot
] = 0;
185 case VERT_RESULT_BFC1
:
186 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
187 vs
.output_semantic_index
[slot
] = 1;
189 case VERT_RESULT_FOGC
:
190 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
191 vs
.output_semantic_index
[slot
] = 0;
193 case VERT_RESULT_PSIZ
:
194 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
195 vs
.output_semantic_index
[slot
] = 0;
197 case VERT_RESULT_EDGE
:
200 case VERT_RESULT_TEX0
:
201 case VERT_RESULT_TEX1
:
202 case VERT_RESULT_TEX2
:
203 case VERT_RESULT_TEX3
:
204 case VERT_RESULT_TEX4
:
205 case VERT_RESULT_TEX5
:
206 case VERT_RESULT_TEX6
:
207 case VERT_RESULT_TEX7
:
208 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
209 vs
.output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
211 case VERT_RESULT_VAR0
:
214 assert(attr
- VERT_RESULT_VAR0
< MAX_VARYING
);
215 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
216 vs
.output_semantic_index
[slot
] = attr
- VERT_RESULT_VAR0
;
223 /* find max output slot referenced to compute vs.num_outputs */
225 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
226 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
227 maxSlot
= outputMapping
[attr
];
229 vs
.num_outputs
= maxSlot
+ 1;
232 outputMapping
= defaultOutputMapping
;
235 /* XXX: fix static allocation of tokens:
237 tgsi_mesa_compile_vp_program( &stvp
->Base
,
240 stvp
->input_to_index
,
241 vs
.input_semantic_name
,
242 vs
.input_semantic_index
,
246 vs
.output_semantic_name
,
247 vs
.output_semantic_index
,
248 /* tokenized result */
249 tokensOut
, maxTokens
);
251 vs
.tokens
= tokensOut
;
252 cso
= st_cached_vs_state(st
, &vs
);
256 tgsi_dump( tokensOut
, 0 );
258 #if defined(USE_X86_ASM) || defined(SLANG_X86)
259 if (stvp
->sse2_program
.csr
== stvp
->sse2_program
.store
)
260 tgsi_emit_sse2( tokensOut
, &stvp
->sse2_program
);
262 if (!cso
->state
.executable
)
263 ((struct cso_vertex_shader
*)cso
)->state
.executable
= (void *) x86_get_func( &stvp
->sse2_program
);
272 * Translate a Mesa fragment shader into a TGSI shader.
273 * \param inputMapping to map fragment program input registers to TGSI
275 * \param tokensOut destination for TGSI tokens
276 * \return pointer to cached pipe_shader object.
278 const struct cso_fragment_shader
*
279 st_translate_fragment_program(struct st_context
*st
,
280 struct st_fragment_program
*stfp
,
281 const GLuint inputMapping
[],
282 struct tgsi_token
*tokensOut
,
285 GLuint outputMapping
[FRAG_RESULT_MAX
];
286 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
287 struct pipe_shader_state fs
;
288 const struct cso_fragment_shader
*cso
;
289 GLuint interpMode
[16]; /* XXX size? */
291 GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
293 /* Check if all fragment programs need the fragment position (in order
294 * to do perspective-corrected interpolation).
297 if (st
->pipe
->get_param(st
->pipe
, PIPE_PARAM_FS_NEEDS_POS
))
298 inputsRead
|= FRAG_BIT_WPOS
;
300 memset(&fs
, 0, sizeof(fs
));
303 * Convert Mesa program inputs to TGSI input register semantics.
305 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
306 if (inputsRead
& (1 << attr
)) {
307 const GLuint slot
= fs
.num_inputs
;
311 defaultInputMapping
[attr
] = slot
;
314 case FRAG_ATTRIB_WPOS
:
315 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
316 fs
.input_semantic_index
[slot
] = 0;
317 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
319 case FRAG_ATTRIB_COL0
:
320 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
321 fs
.input_semantic_index
[slot
] = 0;
322 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
324 case FRAG_ATTRIB_COL1
:
325 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
326 fs
.input_semantic_index
[slot
] = 1;
327 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
329 case FRAG_ATTRIB_FOGC
:
330 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
331 fs
.input_semantic_index
[slot
] = 0;
332 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
334 case FRAG_ATTRIB_TEX0
:
335 case FRAG_ATTRIB_TEX1
:
336 case FRAG_ATTRIB_TEX2
:
337 case FRAG_ATTRIB_TEX3
:
338 case FRAG_ATTRIB_TEX4
:
339 case FRAG_ATTRIB_TEX5
:
340 case FRAG_ATTRIB_TEX6
:
341 case FRAG_ATTRIB_TEX7
:
342 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
343 fs
.input_semantic_index
[slot
] = attr
- FRAG_ATTRIB_TEX0
;
344 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
346 case FRAG_ATTRIB_VAR0
:
349 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
350 fs
.input_semantic_index
[slot
] = attr
- FRAG_ATTRIB_VAR0
;
351 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
357 * Semantics for outputs
359 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
360 if (stfp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
362 case FRAG_RESULT_DEPR
:
363 fs
.output_semantic_name
[fs
.num_outputs
] = TGSI_SEMANTIC_POSITION
;
364 outputMapping
[attr
] = fs
.num_outputs
;
366 case FRAG_RESULT_COLR
:
367 fs
.output_semantic_name
[fs
.num_outputs
] = TGSI_SEMANTIC_COLOR
;
368 outputMapping
[attr
] = fs
.num_outputs
;
378 inputMapping
= defaultInputMapping
;
380 /* XXX: fix static allocation of tokens:
382 tgsi_mesa_compile_fp_program( &stfp
->Base
,
386 fs
.input_semantic_name
,
387 fs
.input_semantic_index
,
391 /* tokenized result */
392 tokensOut
, maxTokens
);
395 fs
.tokens
= tokensOut
;
397 cso
= st_cached_fs_state(st
, &fs
);
401 tgsi_dump( tokensOut
, 0/*TGSI_DUMP_VERBOSE*/ );
403 #if defined(USE_X86_ASM) || defined(SLANG_X86)
404 if (stfp
->sse2_program
.csr
== stfp
->sse2_program
.store
)
405 tgsi_emit_sse2_fs( tokensOut
, &stfp
->sse2_program
);
407 if (!cso
->state
.executable
)
408 ((struct cso_fragment_shader
*)cso
)->state
.executable
= (void *) x86_get_func( &stfp
->sse2_program
);