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/p_shader_tokens.h"
40 #include "pipe/draw/draw_context.h"
42 #include "st_context.h"
45 #include "st_program.h"
46 #include "st_mesa_to_tgsi.h"
53 * Translate a Mesa vertex shader into a TGSI shader.
54 * \param outputMapping to map vertex program output registers to TGSI
56 * \param tokensOut destination for TGSI tokens
57 * \return pointer to cached pipe_shader object.
59 const struct cso_vertex_shader
*
60 st_translate_vertex_program(struct st_context
*st
,
61 struct st_vertex_program
*stvp
,
62 const GLuint outputMapping
[],
63 struct tgsi_token
*tokensOut
,
66 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
67 struct pipe_shader_state vs
;
68 const struct cso_vertex_shader
*cso
;
71 memset(&vs
, 0, sizeof(vs
));
74 * Determine number of inputs, the mappings between VERT_ATTRIB_x
75 * and TGSI generic input indexes, plus input attrib semantic info.
77 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
78 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
79 const GLuint slot
= vs
.num_inputs
;
83 stvp
->input_to_index
[attr
] = slot
;
84 stvp
->index_to_input
[slot
] = attr
;
88 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
89 vs
.input_semantic_index
[slot
] = 0;
91 case VERT_ATTRIB_WEIGHT
:
93 case VERT_ATTRIB_NORMAL
:
94 /* just label as a generic */
95 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
96 vs
.input_semantic_index
[slot
] = 0;
98 case VERT_ATTRIB_COLOR0
:
99 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
100 vs
.input_semantic_index
[slot
] = 0;
102 case VERT_ATTRIB_COLOR1
:
103 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
104 vs
.input_semantic_index
[slot
] = 1;
106 case VERT_ATTRIB_FOG
:
107 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
108 vs
.input_semantic_index
[slot
] = 0;
110 case VERT_ATTRIB_TEX0
:
111 case VERT_ATTRIB_TEX1
:
112 case VERT_ATTRIB_TEX2
:
113 case VERT_ATTRIB_TEX3
:
114 case VERT_ATTRIB_TEX4
:
115 case VERT_ATTRIB_TEX5
:
116 case VERT_ATTRIB_TEX6
:
117 case VERT_ATTRIB_TEX7
:
118 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
119 vs
.input_semantic_index
[slot
] = attr
- VERT_ATTRIB_TEX0
;
121 case VERT_ATTRIB_GENERIC0
:
122 case VERT_ATTRIB_GENERIC1
:
123 case VERT_ATTRIB_GENERIC2
:
124 case VERT_ATTRIB_GENERIC3
:
125 case VERT_ATTRIB_GENERIC4
:
126 case VERT_ATTRIB_GENERIC5
:
127 case VERT_ATTRIB_GENERIC6
:
128 case VERT_ATTRIB_GENERIC7
:
129 assert(attr
< VERT_ATTRIB_MAX
);
130 vs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
131 vs
.input_semantic_index
[slot
] = attr
- VERT_ATTRIB_GENERIC0
;
139 /* initialize output semantics to defaults */
140 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
141 vs
.output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
142 vs
.output_semantic_index
[i
] = 0;
146 * Determine number of outputs, the (default) output register
147 * mapping and the semantic information for each output.
149 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
150 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
154 slot
= outputMapping
[attr
];
158 slot
= vs
.num_outputs
;
160 defaultOutputMapping
[attr
] = slot
;
164 printf("Output %u -> slot %u\n", attr, slot);
168 case VERT_RESULT_HPOS
:
169 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
170 vs
.output_semantic_index
[slot
] = 0;
172 case VERT_RESULT_COL0
:
173 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
174 vs
.output_semantic_index
[slot
] = 0;
176 case VERT_RESULT_COL1
:
177 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
178 vs
.output_semantic_index
[slot
] = 1;
180 case VERT_RESULT_BFC0
:
181 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
182 vs
.output_semantic_index
[slot
] = 0;
184 case VERT_RESULT_BFC1
:
185 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
186 vs
.output_semantic_index
[slot
] = 1;
188 case VERT_RESULT_FOGC
:
189 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
190 vs
.output_semantic_index
[slot
] = 0;
192 case VERT_RESULT_PSIZ
:
193 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
194 vs
.output_semantic_index
[slot
] = 0;
196 case VERT_RESULT_EDGE
:
199 case VERT_RESULT_TEX0
:
200 case VERT_RESULT_TEX1
:
201 case VERT_RESULT_TEX2
:
202 case VERT_RESULT_TEX3
:
203 case VERT_RESULT_TEX4
:
204 case VERT_RESULT_TEX5
:
205 case VERT_RESULT_TEX6
:
206 case VERT_RESULT_TEX7
:
207 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
208 vs
.output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
210 case VERT_RESULT_VAR0
:
213 assert(attr
- VERT_RESULT_VAR0
< MAX_VARYING
);
214 vs
.output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
215 vs
.output_semantic_index
[slot
] = attr
- VERT_RESULT_VAR0
;
222 /* find max output slot referenced to compute vs.num_outputs */
224 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
225 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
226 maxSlot
= outputMapping
[attr
];
228 vs
.num_outputs
= maxSlot
+ 1;
231 outputMapping
= defaultOutputMapping
;
234 /* XXX: fix static allocation of tokens:
236 tgsi_translate_mesa_program( TGSI_PROCESSOR_VERTEX
,
240 stvp
->input_to_index
,
241 vs
.input_semantic_name
,
242 vs
.input_semantic_index
,
247 vs
.output_semantic_name
,
248 vs
.output_semantic_index
,
249 /* tokenized result */
250 tokensOut
, maxTokens
);
252 vs
.tokens
= tokensOut
;
254 cso
= st_cached_vs_state(st
, &vs
);
258 tgsi_dump( tokensOut
, 0 );
266 * Translate a Mesa fragment shader into a TGSI shader.
267 * \param inputMapping to map fragment program input registers to TGSI
269 * \param tokensOut destination for TGSI tokens
270 * \return pointer to cached pipe_shader object.
272 const struct cso_fragment_shader
*
273 st_translate_fragment_program(struct st_context
*st
,
274 struct st_fragment_program
*stfp
,
275 const GLuint inputMapping
[],
276 struct tgsi_token
*tokensOut
,
279 GLuint outputMapping
[FRAG_RESULT_MAX
];
280 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
281 struct pipe_shader_state fs
;
282 const struct cso_fragment_shader
*cso
;
283 GLuint interpMode
[16]; /* XXX size? */
285 GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
287 /* For software rendering, we always need the fragment input position
288 * in order to calculate interpolated values.
289 * For i915, we always want to emit the semantic info for position.
291 inputsRead
|= FRAG_BIT_WPOS
;
293 memset(&fs
, 0, sizeof(fs
));
296 * Convert Mesa program inputs to TGSI input register semantics.
298 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
299 if (inputsRead
& (1 << attr
)) {
300 const GLuint slot
= fs
.num_inputs
;
304 defaultInputMapping
[attr
] = slot
;
307 case FRAG_ATTRIB_WPOS
:
308 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
309 fs
.input_semantic_index
[slot
] = 0;
310 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
312 case FRAG_ATTRIB_COL0
:
313 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
314 fs
.input_semantic_index
[slot
] = 0;
315 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
317 case FRAG_ATTRIB_COL1
:
318 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
319 fs
.input_semantic_index
[slot
] = 1;
320 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
322 case FRAG_ATTRIB_FOGC
:
323 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
324 fs
.input_semantic_index
[slot
] = 0;
325 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
327 case FRAG_ATTRIB_TEX0
:
328 case FRAG_ATTRIB_TEX1
:
329 case FRAG_ATTRIB_TEX2
:
330 case FRAG_ATTRIB_TEX3
:
331 case FRAG_ATTRIB_TEX4
:
332 case FRAG_ATTRIB_TEX5
:
333 case FRAG_ATTRIB_TEX6
:
334 case FRAG_ATTRIB_TEX7
:
335 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
336 fs
.input_semantic_index
[slot
] = attr
- FRAG_ATTRIB_TEX0
;
337 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
339 case FRAG_ATTRIB_VAR0
:
342 fs
.input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
343 fs
.input_semantic_index
[slot
] = attr
- FRAG_ATTRIB_VAR0
;
344 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
350 * Semantics and mapping for outputs
354 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
356 /* if z is written, emit that first */
357 if (outputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
358 fs
.output_semantic_name
[fs
.num_outputs
] = TGSI_SEMANTIC_POSITION
;
359 fs
.output_semantic_index
[fs
.num_outputs
] = 0;
360 outputMapping
[FRAG_RESULT_DEPR
] = fs
.num_outputs
;
362 outputsWritten
&= ~(1 << FRAG_RESULT_DEPR
);
365 /* handle remaning outputs (color) */
366 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
367 if (outputsWritten
& (1 << attr
)) {
369 case FRAG_RESULT_DEPR
:
373 case FRAG_RESULT_COLR
:
374 fs
.output_semantic_name
[fs
.num_outputs
] = TGSI_SEMANTIC_COLOR
;
375 fs
.output_semantic_index
[fs
.num_outputs
] = numColors
;
376 outputMapping
[attr
] = fs
.num_outputs
;
388 inputMapping
= defaultInputMapping
;
390 /* XXX: fix static allocation of tokens:
392 tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT
,
397 fs
.input_semantic_name
,
398 fs
.input_semantic_index
,
403 fs
.output_semantic_name
,
404 fs
.output_semantic_index
,
405 /* tokenized result */
406 tokensOut
, maxTokens
);
408 fs
.tokens
= tokensOut
;
410 cso
= st_cached_fs_state(st
, &fs
);
414 tgsi_dump( tokensOut
, 0/*TGSI_DUMP_VERBOSE*/ );