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"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/draw/draw_context.h"
40 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
41 #include "pipe/tgsi/exec/tgsi_core.h"
42 #include "pipe/llvm/llvmtgsi.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_translate_mesa_program( TGSI_PROCESSOR_VERTEX
,
241 stvp
->input_to_index
,
242 vs
.input_semantic_name
,
243 vs
.input_semantic_index
,
248 vs
.output_semantic_name
,
249 vs
.output_semantic_index
,
250 /* tokenized result */
251 tokensOut
, maxTokens
);
253 vs
.tokens
= tokensOut
;
254 vs
.llvm_prog
= ga_llvm_from_tgsi(vs
.tokens
);
255 cso
= st_cached_vs_state(st
, &vs
);
259 tgsi_dump( tokensOut
, 0 );
267 * Translate a Mesa fragment shader into a TGSI shader.
268 * \param inputMapping to map fragment program input registers to TGSI
270 * \param tokensOut destination for TGSI tokens
271 * \return pointer to cached pipe_shader object.
273 const struct cso_fragment_shader
*
274 st_translate_fragment_program(struct st_context
*st
,
275 struct st_fragment_program
*stfp
,
276 const GLuint inputMapping
[],
277 struct tgsi_token
*tokensOut
,
280 GLuint outputMapping
[FRAG_RESULT_MAX
];
281 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
282 struct pipe_shader_state fs
;
283 const struct cso_fragment_shader
*cso
;
284 GLuint interpMode
[16]; /* XXX size? */
286 GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
288 /* For software rendering, we always need the fragment input position
289 * in order to calculate interpolated values.
290 * For i915, we always want to emit the semantic info for position.
292 inputsRead
|= FRAG_BIT_WPOS
;
294 memset(&fs
, 0, sizeof(fs
));
297 * Convert Mesa program inputs to TGSI input register semantics.
299 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
300 if (inputsRead
& (1 << attr
)) {
301 const GLuint slot
= fs
.num_inputs
;
305 defaultInputMapping
[attr
] = slot
;
308 case FRAG_ATTRIB_WPOS
:
309 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
310 fs
.input_semantic_index
[slot
] = 0;
311 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
313 case FRAG_ATTRIB_COL0
:
314 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
315 fs
.input_semantic_index
[slot
] = 0;
316 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
318 case FRAG_ATTRIB_COL1
:
319 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
320 fs
.input_semantic_index
[slot
] = 1;
321 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
323 case FRAG_ATTRIB_FOGC
:
324 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
325 fs
.input_semantic_index
[slot
] = 0;
326 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
328 case FRAG_ATTRIB_TEX0
:
329 case FRAG_ATTRIB_TEX1
:
330 case FRAG_ATTRIB_TEX2
:
331 case FRAG_ATTRIB_TEX3
:
332 case FRAG_ATTRIB_TEX4
:
333 case FRAG_ATTRIB_TEX5
:
334 case FRAG_ATTRIB_TEX6
:
335 case FRAG_ATTRIB_TEX7
:
336 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
337 fs
.input_semantic_index
[slot
] = attr
- FRAG_ATTRIB_TEX0
;
338 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
340 case FRAG_ATTRIB_VAR0
:
343 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
344 fs
.input_semantic_index
[slot
] = attr
- FRAG_ATTRIB_VAR0
;
345 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
351 * Semantics and mapping for outputs
355 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
357 /* if z is written, emit that first */
358 if (outputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
359 fs
.output_semantic_name
[fs
.num_outputs
] = TGSI_SEMANTIC_POSITION
;
360 fs
.output_semantic_index
[fs
.num_outputs
] = 0;
361 outputMapping
[FRAG_RESULT_DEPR
] = fs
.num_outputs
;
363 outputsWritten
&= ~(1 << FRAG_RESULT_DEPR
);
366 /* handle remaning outputs (color) */
367 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
368 if (outputsWritten
& (1 << attr
)) {
370 case FRAG_RESULT_DEPR
:
374 case FRAG_RESULT_COLR
:
375 fs
.output_semantic_name
[fs
.num_outputs
] = TGSI_SEMANTIC_COLOR
;
376 fs
.output_semantic_index
[fs
.num_outputs
] = numColors
;
377 outputMapping
[attr
] = fs
.num_outputs
;
389 inputMapping
= defaultInputMapping
;
391 /* XXX: fix static allocation of tokens:
393 tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT
,
398 fs
.input_semantic_name
,
399 fs
.input_semantic_index
,
404 fs
.output_semantic_name
,
405 fs
.output_semantic_index
,
406 /* tokenized result */
407 tokensOut
, maxTokens
);
409 fs
.tokens
= tokensOut
;
410 fs
.llvm_prog
= ga_llvm_from_tgsi(fs
.tokens
);
411 cso
= st_cached_fs_state(st
, &fs
);
415 tgsi_dump( tokensOut
, 0/*TGSI_DUMP_VERBOSE*/ );