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"
46 #include "st_context.h"
48 #include "st_program.h"
49 #include "st_mesa_to_tgsi.h"
50 #include "cso_cache/cso_context.h"
54 * Translate a Mesa vertex shader into a TGSI shader.
55 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
56 * to TGSI output slots
57 * \param tokensOut destination for TGSI tokens
58 * \return pointer to cached pipe_shader object.
61 st_translate_vertex_program(struct st_context
*st
,
62 struct st_vertex_program
*stvp
,
63 const GLuint outputMapping
[],
64 const ubyte
*outputSemanticName
,
65 const ubyte
*outputSemanticIndex
)
67 struct pipe_context
*pipe
= st
->pipe
;
68 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
70 GLuint num_generic
= 0;
72 ubyte vs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
73 ubyte vs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
74 uint vs_num_inputs
= 0;
76 ubyte vs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
77 ubyte vs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
78 uint vs_num_outputs
= 0;
80 GLbitfield input_flags
[MAX_PROGRAM_INPUTS
];
81 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
83 /*memset(&vs, 0, sizeof(vs));*/
84 memset(input_flags
, 0, sizeof(input_flags
));
85 memset(output_flags
, 0, sizeof(output_flags
));
87 if (stvp
->Base
.IsPositionInvariant
)
88 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
90 assert(stvp
->Base
.Base
.NumInstructions
> 1);
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 st_free_tokens(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
);
349 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
350 _mesa_print_program(&stvp
->Base
.Base
);
354 if (ST_DEBUG
& DEBUG_TGSI
) {
355 tgsi_dump( stvp
->state
.tokens
, 0 );
363 * Translate a Mesa fragment shader into a TGSI shader.
364 * \param inputMapping to map fragment program input registers to TGSI
366 * \return pointer to cached pipe_shader object.
369 st_translate_fragment_program(struct st_context
*st
,
370 struct st_fragment_program
*stfp
,
371 const GLuint inputMapping
[])
373 struct pipe_context
*pipe
= st
->pipe
;
374 GLuint outputMapping
[FRAG_RESULT_MAX
];
375 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
376 GLuint interpMode
[16]; /* XXX size? */
378 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
380 GLuint num_generic
= 0;
382 uint fs_num_inputs
= 0;
384 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
385 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
386 uint fs_num_outputs
= 0;
388 GLbitfield input_flags
[MAX_PROGRAM_INPUTS
];
389 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
391 /*memset(&fs, 0, sizeof(fs));*/
392 memset(input_flags
, 0, sizeof(input_flags
));
393 memset(output_flags
, 0, sizeof(output_flags
));
395 /* which vertex output goes to the first fragment input: */
396 if (inputsRead
& FRAG_BIT_WPOS
)
402 * Convert Mesa program inputs to TGSI input register semantics.
404 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
405 if (inputsRead
& (1 << attr
)) {
406 const GLuint slot
= fs_num_inputs
;
408 defaultInputMapping
[attr
] = slot
;
410 stfp
->input_map
[slot
] = vslot
++;
415 case FRAG_ATTRIB_WPOS
:
416 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
417 stfp
->input_semantic_index
[slot
] = 0;
418 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
420 case FRAG_ATTRIB_COL0
:
421 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
422 stfp
->input_semantic_index
[slot
] = 0;
423 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
425 case FRAG_ATTRIB_COL1
:
426 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
427 stfp
->input_semantic_index
[slot
] = 1;
428 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
430 case FRAG_ATTRIB_FOGC
:
431 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
432 stfp
->input_semantic_index
[slot
] = 0;
433 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
435 case FRAG_ATTRIB_FACE
:
436 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
437 stfp
->input_semantic_index
[slot
] = num_generic
++;
438 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
440 case FRAG_ATTRIB_PNTC
:
441 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
442 stfp
->input_semantic_index
[slot
] = num_generic
++;
443 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
445 case FRAG_ATTRIB_TEX0
:
446 case FRAG_ATTRIB_TEX1
:
447 case FRAG_ATTRIB_TEX2
:
448 case FRAG_ATTRIB_TEX3
:
449 case FRAG_ATTRIB_TEX4
:
450 case FRAG_ATTRIB_TEX5
:
451 case FRAG_ATTRIB_TEX6
:
452 case FRAG_ATTRIB_TEX7
:
453 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
454 stfp
->input_semantic_index
[slot
] = num_generic
++;
455 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
457 case FRAG_ATTRIB_VAR0
:
460 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
461 stfp
->input_semantic_index
[slot
] = num_generic
++;
462 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
465 input_flags
[slot
] = stfp
->Base
.Base
.InputFlags
[attr
];
470 * Semantics and mapping for outputs
474 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
476 /* if z is written, emit that first */
477 if (outputsWritten
& (1 << FRAG_RESULT_DEPTH
)) {
478 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
479 fs_output_semantic_index
[fs_num_outputs
] = 0;
480 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
482 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
485 /* handle remaning outputs (color) */
486 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
487 if (outputsWritten
& (1 << attr
)) {
489 case FRAG_RESULT_DEPTH
:
494 assert(attr
== FRAG_RESULT_COLOR
||
495 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
496 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
497 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
498 outputMapping
[attr
] = fs_num_outputs
;
503 output_flags
[fs_num_outputs
] = stfp
->Base
.Base
.OutputFlags
[attr
];
511 inputMapping
= defaultInputMapping
;
514 st_translate_mesa_program(st
->ctx
,
515 TGSI_PROCESSOR_FRAGMENT
,
520 stfp
->input_semantic_name
,
521 stfp
->input_semantic_index
,
527 fs_output_semantic_name
,
528 fs_output_semantic_index
,
531 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->state
);
533 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
534 _mesa_print_program(&stfp
->Base
.Base
);
538 if (ST_DEBUG
& DEBUG_TGSI
) {
539 tgsi_dump( stfp
->state
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
546 * Debug- print current shader text
549 st_print_shaders(GLcontext
*ctx
)
551 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
554 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
555 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
556 printf("%s\n", shProg
->Shaders
[i
]->Source
);