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 "main/imports.h"
35 #include "main/mtypes.h"
36 #include "shader/prog_print.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "draw/draw_context.h"
42 #include "tgsi/util/tgsi_dump.h"
44 #include "st_context.h"
47 #include "st_program.h"
48 #include "st_mesa_to_tgsi.h"
55 * Translate a Mesa vertex shader into a TGSI shader.
56 * \param outputMapping to map vertex program output registers to TGSI
58 * \param tokensOut destination for TGSI tokens
59 * \return pointer to cached pipe_shader object.
62 st_translate_vertex_program(struct st_context
*st
,
63 struct st_vertex_program
*stvp
,
64 const GLuint outputMapping
[],
65 struct tgsi_token
*tokensOut
,
68 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
69 struct pipe_shader_state vs
;
70 const struct cso_vertex_shader
*cso
;
72 GLuint num_generic
= 0;
74 memset(&vs
, 0, sizeof(vs
));
77 * Determine number of inputs, the mappings between VERT_ATTRIB_x
78 * and TGSI generic input indexes, plus input attrib semantic info.
80 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
81 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
82 const GLuint slot
= vs
.num_inputs
;
86 stvp
->input_to_index
[attr
] = slot
;
87 stvp
->index_to_input
[slot
] = attr
;
91 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
92 vs
.input_semantic_index
[slot
] = 0;
94 case VERT_ATTRIB_WEIGHT
:
96 case VERT_ATTRIB_NORMAL
:
97 /* just label as a generic */
98 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
99 vs
.input_semantic_index
[slot
] = 0;
101 case VERT_ATTRIB_COLOR0
:
102 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
103 vs
.input_semantic_index
[slot
] = 0;
105 case VERT_ATTRIB_COLOR1
:
106 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
107 vs
.input_semantic_index
[slot
] = 1;
109 case VERT_ATTRIB_FOG
:
110 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
111 vs
.input_semantic_index
[slot
] = 0;
113 case VERT_ATTRIB_TEX0
:
114 case VERT_ATTRIB_TEX1
:
115 case VERT_ATTRIB_TEX2
:
116 case VERT_ATTRIB_TEX3
:
117 case VERT_ATTRIB_TEX4
:
118 case VERT_ATTRIB_TEX5
:
119 case VERT_ATTRIB_TEX6
:
120 case VERT_ATTRIB_TEX7
:
121 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
122 vs
.input_semantic_index
[slot
] = num_generic
++;
124 case VERT_ATTRIB_GENERIC0
:
125 case VERT_ATTRIB_GENERIC1
:
126 case VERT_ATTRIB_GENERIC2
:
127 case VERT_ATTRIB_GENERIC3
:
128 case VERT_ATTRIB_GENERIC4
:
129 case VERT_ATTRIB_GENERIC5
:
130 case VERT_ATTRIB_GENERIC6
:
131 case VERT_ATTRIB_GENERIC7
:
132 assert(attr
< VERT_ATTRIB_MAX
);
133 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
134 vs
.input_semantic_index
[slot
] = num_generic
++;
142 /* initialize output semantics to defaults */
143 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
144 vs
.output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
145 vs
.output_semantic_index
[i
] = 0;
150 * Determine number of outputs, the (default) output register
151 * mapping and the semantic information for each output.
153 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
154 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
158 slot
= outputMapping
[attr
];
162 slot
= vs
.num_outputs
;
164 defaultOutputMapping
[attr
] = slot
;
168 printf("Output %u -> slot %u\n", attr, slot);
172 case VERT_RESULT_HPOS
:
174 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
175 vs
.output_semantic_index
[slot
] = 0;
177 case VERT_RESULT_COL0
:
178 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
179 vs
.output_semantic_index
[slot
] = 0;
181 case VERT_RESULT_COL1
:
182 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
183 vs
.output_semantic_index
[slot
] = 1;
185 case VERT_RESULT_BFC0
:
186 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
187 vs
.output_semantic_index
[slot
] = 0;
189 case VERT_RESULT_BFC1
:
190 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
191 vs
.output_semantic_index
[slot
] = 1;
193 case VERT_RESULT_FOGC
:
194 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
195 vs
.output_semantic_index
[slot
] = 0;
197 case VERT_RESULT_PSIZ
:
198 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
199 vs
.output_semantic_index
[slot
] = 0;
201 case VERT_RESULT_EDGE
:
204 case VERT_RESULT_TEX0
:
205 case VERT_RESULT_TEX1
:
206 case VERT_RESULT_TEX2
:
207 case VERT_RESULT_TEX3
:
208 case VERT_RESULT_TEX4
:
209 case VERT_RESULT_TEX5
:
210 case VERT_RESULT_TEX6
:
211 case VERT_RESULT_TEX7
:
212 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
213 vs
.output_semantic_index
[slot
] = num_generic
++;
215 case VERT_RESULT_VAR0
:
218 assert(attr
- VERT_RESULT_VAR0
< MAX_VARYING
);
219 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
220 vs
.output_semantic_index
[slot
] = num_generic
++;
225 assert(vs
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
229 /* find max output slot referenced to compute vs.num_outputs */
231 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
232 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
233 maxSlot
= outputMapping
[attr
];
235 vs
.num_outputs
= maxSlot
+ 1;
238 outputMapping
= defaultOutputMapping
;
241 /* XXX: fix static allocation of tokens:
243 tgsi_translate_mesa_program( TGSI_PROCESSOR_VERTEX
,
247 stvp
->input_to_index
,
248 vs
.input_semantic_name
,
249 vs
.input_semantic_index
,
254 vs
.output_semantic_name
,
255 vs
.output_semantic_index
,
256 /* tokenized result */
257 tokensOut
, maxTokens
);
259 vs
.tokens
= tokensOut
;
261 cso
= st_cached_vs_state(st
, &vs
);
265 _mesa_print_program(&stvp
->Base
.Base
);
268 tgsi_dump( tokensOut
, 0 );
274 * Translate a Mesa fragment shader into a TGSI shader.
275 * \param inputMapping to map fragment program input registers to TGSI
277 * \param tokensOut destination for TGSI tokens
278 * \return pointer to cached pipe_shader object.
280 const struct cso_fragment_shader
*
281 st_translate_fragment_program(struct st_context
*st
,
282 struct st_fragment_program
*stfp
,
283 const GLuint inputMapping
[],
284 struct tgsi_token
*tokensOut
,
287 GLuint outputMapping
[FRAG_RESULT_MAX
];
288 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
289 struct pipe_shader_state fs
;
290 const struct cso_fragment_shader
*cso
;
291 GLuint interpMode
[16]; /* XXX size? */
293 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
295 GLuint num_generic
= 0;
297 memset(&fs
, 0, sizeof(fs
));
299 /* which vertex output goes to the first fragment input: */
300 if (inputsRead
& FRAG_BIT_WPOS
)
306 * Convert Mesa program inputs to TGSI input register semantics.
308 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
309 if (inputsRead
& (1 << attr
)) {
310 const GLuint slot
= fs
.num_inputs
;
312 defaultInputMapping
[attr
] = slot
;
314 stfp
->input_map
[slot
] = vslot
++;
319 case FRAG_ATTRIB_WPOS
:
320 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
321 fs
.input_semantic_index
[slot
] = 0;
322 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
324 case FRAG_ATTRIB_COL0
:
325 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
326 fs
.input_semantic_index
[slot
] = 0;
327 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
329 case FRAG_ATTRIB_COL1
:
330 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
331 fs
.input_semantic_index
[slot
] = 1;
332 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
334 case FRAG_ATTRIB_FOGC
:
335 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
336 fs
.input_semantic_index
[slot
] = 0;
337 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
339 case FRAG_ATTRIB_TEX0
:
340 case FRAG_ATTRIB_TEX1
:
341 case FRAG_ATTRIB_TEX2
:
342 case FRAG_ATTRIB_TEX3
:
343 case FRAG_ATTRIB_TEX4
:
344 case FRAG_ATTRIB_TEX5
:
345 case FRAG_ATTRIB_TEX6
:
346 case FRAG_ATTRIB_TEX7
:
347 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
348 fs
.input_semantic_index
[slot
] = num_generic
++;
349 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
351 case FRAG_ATTRIB_VAR0
:
354 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
355 fs
.input_semantic_index
[slot
] = num_generic
++;
356 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
362 * Semantics and mapping for outputs
366 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
368 /* if z is written, emit that first */
369 if (outputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
370 fs
.output_semantic_name
[fs
.num_outputs
] = TGSI_SEMANTIC_POSITION
;
371 fs
.output_semantic_index
[fs
.num_outputs
] = 0;
372 outputMapping
[FRAG_RESULT_DEPR
] = fs
.num_outputs
;
374 outputsWritten
&= ~(1 << FRAG_RESULT_DEPR
);
377 /* handle remaning outputs (color) */
378 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
379 if (outputsWritten
& (1 << attr
)) {
381 case FRAG_RESULT_DEPR
:
385 case FRAG_RESULT_COLR
:
386 fs
.output_semantic_name
[fs
.num_outputs
] = TGSI_SEMANTIC_COLOR
;
387 fs
.output_semantic_index
[fs
.num_outputs
] = numColors
;
388 outputMapping
[attr
] = fs
.num_outputs
;
400 inputMapping
= defaultInputMapping
;
402 /* XXX: fix static allocation of tokens:
404 tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT
,
409 fs
.input_semantic_name
,
410 fs
.input_semantic_index
,
415 fs
.output_semantic_name
,
416 fs
.output_semantic_index
,
417 /* tokenized result */
418 tokensOut
, maxTokens
);
420 fs
.tokens
= tokensOut
;
422 cso
= st_cached_fs_state(st
, &fs
);
426 _mesa_print_program(&stfp
->Base
.Base
);
429 tgsi_dump( tokensOut
, 0/*TGSI_DUMP_VERBOSE*/ );