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"
46 #include "st_program.h"
47 #include "st_mesa_to_tgsi.h"
48 #include "cso_cache/cso_context.h"
54 /** XXX we should use the version of this from p_util.h but including
55 * that header causes symbol collisions.
58 mem_dup(const void *src
, uint size
)
60 void *dup
= MALLOC(size
);
62 memcpy(dup
, src
, size
);
69 * Translate a Mesa vertex shader into a TGSI shader.
70 * \param outputMapping to map vertex program output registers to TGSI
72 * \param tokensOut destination for TGSI tokens
73 * \return pointer to cached pipe_shader object.
76 st_translate_vertex_program(struct st_context
*st
,
77 struct st_vertex_program
*stvp
,
78 const GLuint outputMapping
[])
80 struct pipe_context
*pipe
= st
->pipe
;
81 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
82 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
83 struct pipe_shader_state vs
;
85 GLuint num_generic
= 0;
88 ubyte vs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
89 ubyte vs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
90 uint vs_num_inputs
= 0;
92 ubyte vs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
93 ubyte vs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
94 uint vs_num_outputs
= 0;
96 memset(&vs
, 0, sizeof(vs
));
99 * Determine number of inputs, the mappings between VERT_ATTRIB_x
100 * and TGSI generic input indexes, plus input attrib semantic info.
102 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
103 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
104 const GLuint slot
= vs_num_inputs
;
108 stvp
->input_to_index
[attr
] = slot
;
109 stvp
->index_to_input
[slot
] = attr
;
112 case VERT_ATTRIB_POS
:
113 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
114 vs_input_semantic_index
[slot
] = 0;
116 case VERT_ATTRIB_WEIGHT
:
118 case VERT_ATTRIB_NORMAL
:
119 /* just label as a generic */
120 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
121 vs_input_semantic_index
[slot
] = 0;
123 case VERT_ATTRIB_COLOR0
:
124 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
125 vs_input_semantic_index
[slot
] = 0;
127 case VERT_ATTRIB_COLOR1
:
128 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
129 vs_input_semantic_index
[slot
] = 1;
131 case VERT_ATTRIB_FOG
:
132 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
133 vs_input_semantic_index
[slot
] = 0;
135 case VERT_ATTRIB_TEX0
:
136 case VERT_ATTRIB_TEX1
:
137 case VERT_ATTRIB_TEX2
:
138 case VERT_ATTRIB_TEX3
:
139 case VERT_ATTRIB_TEX4
:
140 case VERT_ATTRIB_TEX5
:
141 case VERT_ATTRIB_TEX6
:
142 case VERT_ATTRIB_TEX7
:
143 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
144 vs_input_semantic_index
[slot
] = num_generic
++;
146 case VERT_ATTRIB_GENERIC0
:
147 case VERT_ATTRIB_GENERIC1
:
148 case VERT_ATTRIB_GENERIC2
:
149 case VERT_ATTRIB_GENERIC3
:
150 case VERT_ATTRIB_GENERIC4
:
151 case VERT_ATTRIB_GENERIC5
:
152 case VERT_ATTRIB_GENERIC6
:
153 case VERT_ATTRIB_GENERIC7
:
154 assert(attr
< VERT_ATTRIB_MAX
);
155 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
156 vs_input_semantic_index
[slot
] = num_generic
++;
164 /* initialize output semantics to defaults */
165 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
166 vs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
167 vs_output_semantic_index
[i
] = 0;
172 * Determine number of outputs, the (default) output register
173 * mapping and the semantic information for each output.
175 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
176 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
180 slot
= outputMapping
[attr
];
184 slot
= vs_num_outputs
;
186 defaultOutputMapping
[attr
] = slot
;
190 printf("Output %u -> slot %u\n", attr, slot);
194 case VERT_RESULT_HPOS
:
196 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
197 vs_output_semantic_index
[slot
] = 0;
199 case VERT_RESULT_COL0
:
200 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
201 vs_output_semantic_index
[slot
] = 0;
203 case VERT_RESULT_COL1
:
204 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
205 vs_output_semantic_index
[slot
] = 1;
207 case VERT_RESULT_BFC0
:
208 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
209 vs_output_semantic_index
[slot
] = 0;
211 case VERT_RESULT_BFC1
:
212 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
213 vs_output_semantic_index
[slot
] = 1;
215 case VERT_RESULT_FOGC
:
216 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
217 vs_output_semantic_index
[slot
] = 0;
219 case VERT_RESULT_PSIZ
:
220 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
221 vs_output_semantic_index
[slot
] = 0;
223 case VERT_RESULT_EDGE
:
226 case VERT_RESULT_TEX0
:
227 case VERT_RESULT_TEX1
:
228 case VERT_RESULT_TEX2
:
229 case VERT_RESULT_TEX3
:
230 case VERT_RESULT_TEX4
:
231 case VERT_RESULT_TEX5
:
232 case VERT_RESULT_TEX6
:
233 case VERT_RESULT_TEX7
:
234 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
235 vs_output_semantic_index
[slot
] = num_generic
++;
237 case VERT_RESULT_VAR0
:
240 assert(attr
- VERT_RESULT_VAR0
< MAX_VARYING
);
241 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
242 vs_output_semantic_index
[slot
] = num_generic
++;
247 assert(vs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
251 /* find max output slot referenced to compute vs_num_outputs */
253 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
254 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
255 maxSlot
= outputMapping
[attr
];
257 vs_num_outputs
= maxSlot
+ 1;
260 outputMapping
= defaultOutputMapping
;
263 /* XXX: fix static allocation of tokens:
265 num_tokens
= tgsi_translate_mesa_program( TGSI_PROCESSOR_VERTEX
,
269 stvp
->input_to_index
,
270 vs_input_semantic_name
,
271 vs_input_semantic_index
,
276 vs_output_semantic_name
,
277 vs_output_semantic_index
,
278 /* tokenized result */
279 tokens
, ST_MAX_SHADER_TOKENS
);
281 vs
.tokens
= (struct tgsi_token
*)
282 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
284 stvp
->num_inputs
= vs_num_inputs
;
285 stvp
->state
= vs
; /* struct copy */
286 stvp
->driver_shader
= pipe
->create_vs_state(pipe
, &vs
);
289 _mesa_print_program(&stvp
->Base
.Base
);
292 tgsi_dump( vs
.tokens
, 0 );
298 * Translate a Mesa fragment shader into a TGSI shader.
299 * \param inputMapping to map fragment program input registers to TGSI
301 * \param tokensOut destination for TGSI tokens
302 * \return pointer to cached pipe_shader object.
304 const struct cso_fragment_shader
*
305 st_translate_fragment_program(struct st_context
*st
,
306 struct st_fragment_program
*stfp
,
307 const GLuint inputMapping
[])
309 struct pipe_context
*pipe
= st
->pipe
;
310 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
311 GLuint outputMapping
[FRAG_RESULT_MAX
];
312 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
313 struct pipe_shader_state fs
;
314 const struct cso_fragment_shader
*cso
;
315 GLuint interpMode
[16]; /* XXX size? */
317 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
319 GLuint num_generic
= 0;
322 ubyte fs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
323 ubyte fs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
324 uint fs_num_inputs
= 0;
326 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
327 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
328 uint fs_num_outputs
= 0;
330 memset(&fs
, 0, sizeof(fs
));
332 /* which vertex output goes to the first fragment input: */
333 if (inputsRead
& FRAG_BIT_WPOS
)
339 * Convert Mesa program inputs to TGSI input register semantics.
341 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
342 if (inputsRead
& (1 << attr
)) {
343 const GLuint slot
= fs_num_inputs
;
345 defaultInputMapping
[attr
] = slot
;
347 stfp
->input_map
[slot
] = vslot
++;
352 case FRAG_ATTRIB_WPOS
:
353 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
354 fs_input_semantic_index
[slot
] = 0;
355 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
357 case FRAG_ATTRIB_COL0
:
358 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
359 fs_input_semantic_index
[slot
] = 0;
360 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
362 case FRAG_ATTRIB_COL1
:
363 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
364 fs_input_semantic_index
[slot
] = 1;
365 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
367 case FRAG_ATTRIB_FOGC
:
368 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
369 fs_input_semantic_index
[slot
] = 0;
370 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
372 case FRAG_ATTRIB_TEX0
:
373 case FRAG_ATTRIB_TEX1
:
374 case FRAG_ATTRIB_TEX2
:
375 case FRAG_ATTRIB_TEX3
:
376 case FRAG_ATTRIB_TEX4
:
377 case FRAG_ATTRIB_TEX5
:
378 case FRAG_ATTRIB_TEX6
:
379 case FRAG_ATTRIB_TEX7
:
380 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
381 fs_input_semantic_index
[slot
] = num_generic
++;
382 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
384 case FRAG_ATTRIB_VAR0
:
387 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
388 fs_input_semantic_index
[slot
] = num_generic
++;
389 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
395 * Semantics and mapping for outputs
399 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
401 /* if z is written, emit that first */
402 if (outputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
403 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
404 fs_output_semantic_index
[fs_num_outputs
] = 0;
405 outputMapping
[FRAG_RESULT_DEPR
] = fs_num_outputs
;
407 outputsWritten
&= ~(1 << FRAG_RESULT_DEPR
);
410 /* handle remaning outputs (color) */
411 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
412 if (outputsWritten
& (1 << attr
)) {
414 case FRAG_RESULT_DEPR
:
418 case FRAG_RESULT_COLR
:
419 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
420 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
421 outputMapping
[attr
] = fs_num_outputs
;
433 inputMapping
= defaultInputMapping
;
435 /* XXX: fix static allocation of tokens:
437 num_tokens
= tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT
,
442 fs_input_semantic_name
,
443 fs_input_semantic_index
,
448 fs_output_semantic_name
,
449 fs_output_semantic_index
,
450 /* tokenized result */
451 tokens
, ST_MAX_SHADER_TOKENS
);
453 fs
.tokens
= (struct tgsi_token
*)
454 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
456 stfp
->state
= fs
; /* struct copy */
457 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &fs
);
460 _mesa_print_program(&stfp
->Base
.Base
);
463 tgsi_dump( fs
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );