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"
37 #include "shader/programopt.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_shader_tokens.h"
42 #include "draw/draw_context.h"
43 #include "tgsi/tgsi_dump.h"
45 #include "st_context.h"
47 #include "st_program.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "cso_cache/cso_context.h"
56 * Translate a Mesa vertex shader into a TGSI shader.
57 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
58 * to TGSI output slots
59 * \param tokensOut destination for TGSI tokens
60 * \return pointer to cached pipe_shader object.
63 st_translate_vertex_program(struct st_context
*st
,
64 struct st_vertex_program
*stvp
,
65 const GLuint outputMapping
[],
66 const ubyte
*outputSemanticName
,
67 const ubyte
*outputSemanticIndex
)
69 struct pipe_context
*pipe
= st
->pipe
;
70 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
72 GLuint num_generic
= 0;
74 ubyte vs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
75 ubyte vs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
76 uint vs_num_inputs
= 0;
78 ubyte vs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
79 ubyte vs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
80 uint vs_num_outputs
= 0;
82 GLbitfield input_flags
[MAX_PROGRAM_INPUTS
];
83 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
85 // memset(&vs, 0, sizeof(vs));
86 memset(input_flags
, 0, sizeof(input_flags
));
87 memset(output_flags
, 0, sizeof(output_flags
));
89 if (stvp
->Base
.IsPositionInvariant
)
90 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
93 * Determine number of inputs, the mappings between VERT_ATTRIB_x
94 * and TGSI generic input indexes, plus input attrib semantic info.
96 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
97 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
98 const GLuint slot
= vs_num_inputs
;
102 stvp
->input_to_index
[attr
] = slot
;
103 stvp
->index_to_input
[slot
] = attr
;
106 case VERT_ATTRIB_POS
:
107 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
108 vs_input_semantic_index
[slot
] = 0;
110 case VERT_ATTRIB_WEIGHT
:
112 case VERT_ATTRIB_NORMAL
:
113 /* just label as a generic */
114 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
115 vs_input_semantic_index
[slot
] = 0;
117 case VERT_ATTRIB_COLOR0
:
118 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
119 vs_input_semantic_index
[slot
] = 0;
121 case VERT_ATTRIB_COLOR1
:
122 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
123 vs_input_semantic_index
[slot
] = 1;
125 case VERT_ATTRIB_FOG
:
126 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
127 vs_input_semantic_index
[slot
] = 0;
129 case VERT_ATTRIB_POINT_SIZE
:
130 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
131 vs_input_semantic_index
[slot
] = 0;
133 case VERT_ATTRIB_TEX0
:
134 case VERT_ATTRIB_TEX1
:
135 case VERT_ATTRIB_TEX2
:
136 case VERT_ATTRIB_TEX3
:
137 case VERT_ATTRIB_TEX4
:
138 case VERT_ATTRIB_TEX5
:
139 case VERT_ATTRIB_TEX6
:
140 case VERT_ATTRIB_TEX7
:
141 assert(slot
< Elements(vs_input_semantic_name
));
142 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
143 vs_input_semantic_index
[slot
] = num_generic
++;
145 case VERT_ATTRIB_GENERIC0
:
146 case VERT_ATTRIB_GENERIC1
:
147 case VERT_ATTRIB_GENERIC2
:
148 case VERT_ATTRIB_GENERIC3
:
149 case VERT_ATTRIB_GENERIC4
:
150 case VERT_ATTRIB_GENERIC5
:
151 case VERT_ATTRIB_GENERIC6
:
152 case VERT_ATTRIB_GENERIC7
:
153 case VERT_ATTRIB_GENERIC8
:
154 case VERT_ATTRIB_GENERIC9
:
155 case VERT_ATTRIB_GENERIC10
:
156 case VERT_ATTRIB_GENERIC11
:
157 case VERT_ATTRIB_GENERIC12
:
158 case VERT_ATTRIB_GENERIC13
:
159 case VERT_ATTRIB_GENERIC14
:
160 case VERT_ATTRIB_GENERIC15
:
161 assert(attr
< VERT_ATTRIB_MAX
);
162 assert(slot
< Elements(vs_input_semantic_name
));
163 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
164 vs_input_semantic_index
[slot
] = num_generic
++;
170 input_flags
[slot
] = stvp
->Base
.Base
.InputFlags
[attr
];
175 if (outputMapping
&& outputSemanticName
) {
176 printf("VERT_RESULT written out_slot semantic_name semantic_index\n");
177 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
178 printf(" %-2d %c %3d %2d %2d\n",
180 ((stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) ? 'Y' : ' '),
182 outputSemanticName
[attr
],
183 outputSemanticIndex
[attr
]);
188 /* initialize output semantics to defaults */
189 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
190 assert(i
< Elements(vs_output_semantic_name
));
191 vs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
192 vs_output_semantic_index
[i
] = 0;
193 output_flags
[i
] = 0x0;
198 * Determine number of outputs, the (default) output register
199 * mapping and the semantic information for each output.
201 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
202 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
206 * Pass in the fragment program's input's semantic info.
207 * Use the generic semantic indexes from there, instead of
212 slot
= outputMapping
[attr
];
216 slot
= vs_num_outputs
;
218 defaultOutputMapping
[attr
] = slot
;
222 case VERT_RESULT_HPOS
:
224 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
225 vs_output_semantic_index
[slot
] = 0;
227 case VERT_RESULT_COL0
:
228 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
229 vs_output_semantic_index
[slot
] = 0;
231 case VERT_RESULT_COL1
:
232 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
233 vs_output_semantic_index
[slot
] = 1;
235 case VERT_RESULT_BFC0
:
236 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
237 vs_output_semantic_index
[slot
] = 0;
239 case VERT_RESULT_BFC1
:
240 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
241 vs_output_semantic_index
[slot
] = 1;
243 case VERT_RESULT_FOGC
:
244 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
245 vs_output_semantic_index
[slot
] = 0;
247 case VERT_RESULT_PSIZ
:
248 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
249 vs_output_semantic_index
[slot
] = 0;
251 case VERT_RESULT_EDGE
:
254 case VERT_RESULT_TEX0
:
255 case VERT_RESULT_TEX1
:
256 case VERT_RESULT_TEX2
:
257 case VERT_RESULT_TEX3
:
258 case VERT_RESULT_TEX4
:
259 case VERT_RESULT_TEX5
:
260 case VERT_RESULT_TEX6
:
261 case VERT_RESULT_TEX7
:
263 case VERT_RESULT_VAR0
:
266 assert(slot
< Elements(vs_output_semantic_name
));
267 if (outputSemanticName
) {
268 /* use provided semantic into */
269 assert(outputSemanticName
[attr
] != TGSI_SEMANTIC_COUNT
);
270 vs_output_semantic_name
[slot
] = outputSemanticName
[attr
];
271 vs_output_semantic_index
[slot
] = outputSemanticIndex
[attr
];
274 /* use default semantic info */
275 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
276 vs_output_semantic_index
[slot
] = num_generic
++;
280 assert(slot
< Elements(output_flags
));
281 output_flags
[slot
] = stvp
->Base
.Base
.OutputFlags
[attr
];
286 /* find max output slot referenced to compute vs_num_outputs */
288 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
289 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
290 maxSlot
= outputMapping
[attr
];
292 vs_num_outputs
= maxSlot
+ 1;
295 outputMapping
= defaultOutputMapping
;
301 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
303 printf("attr -> slot\n");
304 for (i
= 0; i
< 16; i
++) {
305 printf(" %2d %3d\n", i
, outputMapping
[i
]);
308 printf("slot sem_name sem_index\n");
309 for (i
= 0; i
< vs_num_outputs
; i
++) {
310 printf(" %2d %d %d\n",
312 vs_output_semantic_name
[i
],
313 vs_output_semantic_index
[i
]);
318 /* free old shader state, if any */
319 if (stvp
->state
.tokens
) {
320 _mesa_free((void *) stvp
->state
.tokens
);
321 stvp
->state
.tokens
= NULL
;
323 if (stvp
->driver_shader
) {
324 cso_delete_vertex_shader(st
->cso_context
, stvp
->driver_shader
);
325 stvp
->driver_shader
= NULL
;
329 st_translate_mesa_program(st
->ctx
,
330 TGSI_PROCESSOR_VERTEX
,
334 stvp
->input_to_index
,
335 vs_input_semantic_name
,
336 vs_input_semantic_index
,
342 vs_output_semantic_name
,
343 vs_output_semantic_index
,
346 stvp
->num_inputs
= vs_num_inputs
;
347 stvp
->driver_shader
= pipe
->create_vs_state(pipe
, &stvp
->state
);
350 _mesa_print_program(&stvp
->Base
.Base
);
353 tgsi_dump( stvp
->state
.tokens
, 0 );
359 * Translate a Mesa fragment shader into a TGSI shader.
360 * \param inputMapping to map fragment program input registers to TGSI
362 * \return pointer to cached pipe_shader object.
365 st_translate_fragment_program(struct st_context
*st
,
366 struct st_fragment_program
*stfp
,
367 const GLuint inputMapping
[])
369 struct pipe_context
*pipe
= st
->pipe
;
370 GLuint outputMapping
[FRAG_RESULT_MAX
];
371 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
372 GLuint interpMode
[16]; /* XXX size? */
374 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
376 GLuint num_generic
= 0;
378 uint fs_num_inputs
= 0;
380 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
381 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
382 uint fs_num_outputs
= 0;
384 GLbitfield input_flags
[MAX_PROGRAM_INPUTS
];
385 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
387 // memset(&fs, 0, sizeof(fs));
388 memset(input_flags
, 0, sizeof(input_flags
));
389 memset(output_flags
, 0, sizeof(output_flags
));
391 /* which vertex output goes to the first fragment input: */
392 if (inputsRead
& FRAG_BIT_WPOS
)
398 * Convert Mesa program inputs to TGSI input register semantics.
400 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
401 if (inputsRead
& (1 << attr
)) {
402 const GLuint slot
= fs_num_inputs
;
404 defaultInputMapping
[attr
] = slot
;
406 stfp
->input_map
[slot
] = vslot
++;
411 case FRAG_ATTRIB_WPOS
:
412 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
413 stfp
->input_semantic_index
[slot
] = 0;
414 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
416 case FRAG_ATTRIB_COL0
:
417 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
418 stfp
->input_semantic_index
[slot
] = 0;
419 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
421 case FRAG_ATTRIB_COL1
:
422 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
423 stfp
->input_semantic_index
[slot
] = 1;
424 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
426 case FRAG_ATTRIB_FOGC
:
427 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
428 stfp
->input_semantic_index
[slot
] = 0;
429 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
431 case FRAG_ATTRIB_FACE
:
432 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
433 stfp
->input_semantic_index
[slot
] = num_generic
++;
434 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
436 case FRAG_ATTRIB_PNTC
:
437 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
438 stfp
->input_semantic_index
[slot
] = num_generic
++;
439 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
441 case FRAG_ATTRIB_TEX0
:
442 case FRAG_ATTRIB_TEX1
:
443 case FRAG_ATTRIB_TEX2
:
444 case FRAG_ATTRIB_TEX3
:
445 case FRAG_ATTRIB_TEX4
:
446 case FRAG_ATTRIB_TEX5
:
447 case FRAG_ATTRIB_TEX6
:
448 case FRAG_ATTRIB_TEX7
:
449 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
450 stfp
->input_semantic_index
[slot
] = num_generic
++;
451 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
453 case FRAG_ATTRIB_VAR0
:
456 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
457 stfp
->input_semantic_index
[slot
] = num_generic
++;
458 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
461 input_flags
[slot
] = stfp
->Base
.Base
.InputFlags
[attr
];
466 * Semantics and mapping for outputs
470 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
472 /* if z is written, emit that first */
473 if (outputsWritten
& (1 << FRAG_RESULT_DEPTH
)) {
474 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
475 fs_output_semantic_index
[fs_num_outputs
] = 0;
476 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
478 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
481 /* handle remaning outputs (color) */
482 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
483 if (outputsWritten
& (1 << attr
)) {
485 case FRAG_RESULT_DEPTH
:
490 assert(attr
== FRAG_RESULT_COLOR
||
491 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
492 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
493 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
494 outputMapping
[attr
] = fs_num_outputs
;
499 output_flags
[fs_num_outputs
] = stfp
->Base
.Base
.OutputFlags
[attr
];
507 inputMapping
= defaultInputMapping
;
510 st_translate_mesa_program(st
->ctx
,
511 TGSI_PROCESSOR_FRAGMENT
,
516 stfp
->input_semantic_name
,
517 stfp
->input_semantic_index
,
523 fs_output_semantic_name
,
524 fs_output_semantic_index
,
527 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->state
);
530 _mesa_print_program(&stfp
->Base
.Base
);
533 tgsi_dump( stfp
->state
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
538 * Debug- print current shader text
541 st_print_shaders(GLcontext
*ctx
)
543 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
546 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
547 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
548 printf("%s\n", shProg
->Shaders
[i
]->Source
);