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
);
91 * Determine number of inputs, the mappings between VERT_ATTRIB_x
92 * and TGSI generic input indexes, plus input attrib semantic info.
94 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
95 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
96 const GLuint slot
= vs_num_inputs
;
100 stvp
->input_to_index
[attr
] = slot
;
101 stvp
->index_to_input
[slot
] = attr
;
104 case VERT_ATTRIB_POS
:
105 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
106 vs_input_semantic_index
[slot
] = 0;
108 case VERT_ATTRIB_WEIGHT
:
110 case VERT_ATTRIB_NORMAL
:
111 /* just label as a generic */
112 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
113 vs_input_semantic_index
[slot
] = 0;
115 case VERT_ATTRIB_COLOR0
:
116 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
117 vs_input_semantic_index
[slot
] = 0;
119 case VERT_ATTRIB_COLOR1
:
120 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
121 vs_input_semantic_index
[slot
] = 1;
123 case VERT_ATTRIB_FOG
:
124 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
125 vs_input_semantic_index
[slot
] = 0;
127 case VERT_ATTRIB_POINT_SIZE
:
128 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
129 vs_input_semantic_index
[slot
] = 0;
131 case VERT_ATTRIB_TEX0
:
132 case VERT_ATTRIB_TEX1
:
133 case VERT_ATTRIB_TEX2
:
134 case VERT_ATTRIB_TEX3
:
135 case VERT_ATTRIB_TEX4
:
136 case VERT_ATTRIB_TEX5
:
137 case VERT_ATTRIB_TEX6
:
138 case VERT_ATTRIB_TEX7
:
139 assert(slot
< Elements(vs_input_semantic_name
));
140 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
141 vs_input_semantic_index
[slot
] = num_generic
++;
143 case VERT_ATTRIB_GENERIC0
:
144 case VERT_ATTRIB_GENERIC1
:
145 case VERT_ATTRIB_GENERIC2
:
146 case VERT_ATTRIB_GENERIC3
:
147 case VERT_ATTRIB_GENERIC4
:
148 case VERT_ATTRIB_GENERIC5
:
149 case VERT_ATTRIB_GENERIC6
:
150 case VERT_ATTRIB_GENERIC7
:
151 case VERT_ATTRIB_GENERIC8
:
152 case VERT_ATTRIB_GENERIC9
:
153 case VERT_ATTRIB_GENERIC10
:
154 case VERT_ATTRIB_GENERIC11
:
155 case VERT_ATTRIB_GENERIC12
:
156 case VERT_ATTRIB_GENERIC13
:
157 case VERT_ATTRIB_GENERIC14
:
158 case VERT_ATTRIB_GENERIC15
:
159 assert(attr
< VERT_ATTRIB_MAX
);
160 assert(slot
< Elements(vs_input_semantic_name
));
161 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
162 vs_input_semantic_index
[slot
] = num_generic
++;
168 input_flags
[slot
] = stvp
->Base
.Base
.InputFlags
[attr
];
173 if (outputMapping
&& outputSemanticName
) {
174 printf("VERT_RESULT written out_slot semantic_name semantic_index\n");
175 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
176 printf(" %-2d %c %3d %2d %2d\n",
178 ((stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) ? 'Y' : ' '),
180 outputSemanticName
[attr
],
181 outputSemanticIndex
[attr
]);
186 /* initialize output semantics to defaults */
187 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
188 assert(i
< Elements(vs_output_semantic_name
));
189 vs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
190 vs_output_semantic_index
[i
] = 0;
191 output_flags
[i
] = 0x0;
196 * Determine number of outputs, the (default) output register
197 * mapping and the semantic information for each output.
199 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
200 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
204 * Pass in the fragment program's input's semantic info.
205 * Use the generic semantic indexes from there, instead of
210 slot
= outputMapping
[attr
];
214 slot
= vs_num_outputs
;
216 defaultOutputMapping
[attr
] = slot
;
220 case VERT_RESULT_HPOS
:
222 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
223 vs_output_semantic_index
[slot
] = 0;
225 case VERT_RESULT_COL0
:
226 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
227 vs_output_semantic_index
[slot
] = 0;
229 case VERT_RESULT_COL1
:
230 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
231 vs_output_semantic_index
[slot
] = 1;
233 case VERT_RESULT_BFC0
:
234 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
235 vs_output_semantic_index
[slot
] = 0;
237 case VERT_RESULT_BFC1
:
238 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
239 vs_output_semantic_index
[slot
] = 1;
241 case VERT_RESULT_FOGC
:
242 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
243 vs_output_semantic_index
[slot
] = 0;
245 case VERT_RESULT_PSIZ
:
246 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
247 vs_output_semantic_index
[slot
] = 0;
249 case VERT_RESULT_EDGE
:
252 case VERT_RESULT_TEX0
:
253 case VERT_RESULT_TEX1
:
254 case VERT_RESULT_TEX2
:
255 case VERT_RESULT_TEX3
:
256 case VERT_RESULT_TEX4
:
257 case VERT_RESULT_TEX5
:
258 case VERT_RESULT_TEX6
:
259 case VERT_RESULT_TEX7
:
261 case VERT_RESULT_VAR0
:
264 assert(slot
< Elements(vs_output_semantic_name
));
265 if (outputSemanticName
) {
266 /* use provided semantic into */
267 assert(outputSemanticName
[attr
] != TGSI_SEMANTIC_COUNT
);
268 vs_output_semantic_name
[slot
] = outputSemanticName
[attr
];
269 vs_output_semantic_index
[slot
] = outputSemanticIndex
[attr
];
272 /* use default semantic info */
273 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
274 vs_output_semantic_index
[slot
] = num_generic
++;
278 assert(slot
< Elements(output_flags
));
279 output_flags
[slot
] = stvp
->Base
.Base
.OutputFlags
[attr
];
284 /* find max output slot referenced to compute vs_num_outputs */
286 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
287 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
288 maxSlot
= outputMapping
[attr
];
290 vs_num_outputs
= maxSlot
+ 1;
293 outputMapping
= defaultOutputMapping
;
299 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
301 printf("attr -> slot\n");
302 for (i
= 0; i
< 16; i
++) {
303 printf(" %2d %3d\n", i
, outputMapping
[i
]);
306 printf("slot sem_name sem_index\n");
307 for (i
= 0; i
< vs_num_outputs
; i
++) {
308 printf(" %2d %d %d\n",
310 vs_output_semantic_name
[i
],
311 vs_output_semantic_index
[i
]);
316 /* free old shader state, if any */
317 if (stvp
->state
.tokens
) {
318 st_free_tokens(stvp
->state
.tokens
);
319 stvp
->state
.tokens
= NULL
;
321 if (stvp
->driver_shader
) {
322 cso_delete_vertex_shader(st
->cso_context
, stvp
->driver_shader
);
323 stvp
->driver_shader
= NULL
;
327 st_translate_mesa_program(st
->ctx
,
328 TGSI_PROCESSOR_VERTEX
,
332 stvp
->input_to_index
,
333 vs_input_semantic_name
,
334 vs_input_semantic_index
,
340 vs_output_semantic_name
,
341 vs_output_semantic_index
,
344 stvp
->num_inputs
= vs_num_inputs
;
345 stvp
->driver_shader
= pipe
->create_vs_state(pipe
, &stvp
->state
);
347 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
348 _mesa_print_program(&stvp
->Base
.Base
);
352 if (ST_DEBUG
& DEBUG_TGSI
) {
353 tgsi_dump( stvp
->state
.tokens
, 0 );
361 * Translate a Mesa fragment shader into a TGSI shader.
362 * \param inputMapping to map fragment program input registers to TGSI
364 * \return pointer to cached pipe_shader object.
367 st_translate_fragment_program(struct st_context
*st
,
368 struct st_fragment_program
*stfp
,
369 const GLuint inputMapping
[])
371 struct pipe_context
*pipe
= st
->pipe
;
372 GLuint outputMapping
[FRAG_RESULT_MAX
];
373 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
374 GLuint interpMode
[16]; /* XXX size? */
376 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
378 GLuint num_generic
= 0;
380 uint fs_num_inputs
= 0;
382 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
383 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
384 uint fs_num_outputs
= 0;
386 GLbitfield input_flags
[MAX_PROGRAM_INPUTS
];
387 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
389 // memset(&fs, 0, sizeof(fs));
390 memset(input_flags
, 0, sizeof(input_flags
));
391 memset(output_flags
, 0, sizeof(output_flags
));
393 /* which vertex output goes to the first fragment input: */
394 if (inputsRead
& FRAG_BIT_WPOS
)
400 * Convert Mesa program inputs to TGSI input register semantics.
402 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
403 if (inputsRead
& (1 << attr
)) {
404 const GLuint slot
= fs_num_inputs
;
406 defaultInputMapping
[attr
] = slot
;
408 stfp
->input_map
[slot
] = vslot
++;
413 case FRAG_ATTRIB_WPOS
:
414 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
415 stfp
->input_semantic_index
[slot
] = 0;
416 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
418 case FRAG_ATTRIB_COL0
:
419 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
420 stfp
->input_semantic_index
[slot
] = 0;
421 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
423 case FRAG_ATTRIB_COL1
:
424 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
425 stfp
->input_semantic_index
[slot
] = 1;
426 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
428 case FRAG_ATTRIB_FOGC
:
429 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
430 stfp
->input_semantic_index
[slot
] = 0;
431 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
433 case FRAG_ATTRIB_FACE
:
434 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
435 stfp
->input_semantic_index
[slot
] = num_generic
++;
436 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
438 case FRAG_ATTRIB_PNTC
:
439 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
440 stfp
->input_semantic_index
[slot
] = num_generic
++;
441 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
443 case FRAG_ATTRIB_TEX0
:
444 case FRAG_ATTRIB_TEX1
:
445 case FRAG_ATTRIB_TEX2
:
446 case FRAG_ATTRIB_TEX3
:
447 case FRAG_ATTRIB_TEX4
:
448 case FRAG_ATTRIB_TEX5
:
449 case FRAG_ATTRIB_TEX6
:
450 case FRAG_ATTRIB_TEX7
:
451 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
452 stfp
->input_semantic_index
[slot
] = num_generic
++;
453 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
455 case FRAG_ATTRIB_VAR0
:
458 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
459 stfp
->input_semantic_index
[slot
] = num_generic
++;
460 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
463 input_flags
[slot
] = stfp
->Base
.Base
.InputFlags
[attr
];
468 * Semantics and mapping for outputs
472 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
474 /* if z is written, emit that first */
475 if (outputsWritten
& (1 << FRAG_RESULT_DEPTH
)) {
476 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
477 fs_output_semantic_index
[fs_num_outputs
] = 0;
478 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
480 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
483 /* handle remaning outputs (color) */
484 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
485 if (outputsWritten
& (1 << attr
)) {
487 case FRAG_RESULT_DEPTH
:
492 assert(attr
== FRAG_RESULT_COLOR
||
493 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
494 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
495 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
496 outputMapping
[attr
] = fs_num_outputs
;
501 output_flags
[fs_num_outputs
] = stfp
->Base
.Base
.OutputFlags
[attr
];
509 inputMapping
= defaultInputMapping
;
512 st_translate_mesa_program(st
->ctx
,
513 TGSI_PROCESSOR_FRAGMENT
,
518 stfp
->input_semantic_name
,
519 stfp
->input_semantic_index
,
525 fs_output_semantic_name
,
526 fs_output_semantic_index
,
529 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->state
);
531 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
532 _mesa_print_program(&stfp
->Base
.Base
);
536 if (ST_DEBUG
& DEBUG_TGSI
) {
537 tgsi_dump( stfp
->state
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
544 * Debug- print current shader text
547 st_print_shaders(GLcontext
*ctx
)
549 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
552 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
553 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
554 printf("%s\n", shProg
->Shaders
[i
]->Source
);