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"
52 #define ST_MAX_SHADER_TOKENS (8 * 1024)
58 /** XXX we should use the version of this from u_memory.h but including
59 * that header causes symbol collisions.
62 mem_dup(const void *src
, uint size
)
64 void *dup
= _mesa_malloc(size
);
66 _mesa_memcpy(dup
, src
, size
);
73 * Translate a Mesa vertex shader into a TGSI shader.
74 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
75 * to TGSI output slots
76 * \param tokensOut destination for TGSI tokens
77 * \return pointer to cached pipe_shader object.
80 st_translate_vertex_program(struct st_context
*st
,
81 struct st_vertex_program
*stvp
,
82 const GLuint outputMapping
[],
83 const ubyte
*outputSemanticName
,
84 const ubyte
*outputSemanticIndex
)
86 struct pipe_context
*pipe
= st
->pipe
;
87 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
88 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
89 struct pipe_shader_state vs
;
91 GLuint num_generic
= 0;
94 ubyte vs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
95 ubyte vs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
96 uint vs_num_inputs
= 0;
98 ubyte vs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
99 ubyte vs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
100 uint vs_num_outputs
= 0;
102 GLbitfield input_flags
[MAX_PROGRAM_INPUTS
];
103 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
105 memset(&vs
, 0, sizeof(vs
));
106 memset(input_flags
, 0, sizeof(input_flags
));
107 memset(output_flags
, 0, sizeof(output_flags
));
109 if (stvp
->Base
.IsPositionInvariant
)
110 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
113 * Determine number of inputs, the mappings between VERT_ATTRIB_x
114 * and TGSI generic input indexes, plus input attrib semantic info.
116 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
117 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
118 const GLuint slot
= vs_num_inputs
;
122 stvp
->input_to_index
[attr
] = slot
;
123 stvp
->index_to_input
[slot
] = attr
;
126 case VERT_ATTRIB_POS
:
127 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
128 vs_input_semantic_index
[slot
] = 0;
130 case VERT_ATTRIB_WEIGHT
:
132 case VERT_ATTRIB_NORMAL
:
133 /* just label as a generic */
134 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
135 vs_input_semantic_index
[slot
] = 0;
137 case VERT_ATTRIB_COLOR0
:
138 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
139 vs_input_semantic_index
[slot
] = 0;
141 case VERT_ATTRIB_COLOR1
:
142 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
143 vs_input_semantic_index
[slot
] = 1;
145 case VERT_ATTRIB_FOG
:
146 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
147 vs_input_semantic_index
[slot
] = 0;
149 case VERT_ATTRIB_POINT_SIZE
:
150 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
151 vs_input_semantic_index
[slot
] = 0;
153 case VERT_ATTRIB_TEX0
:
154 case VERT_ATTRIB_TEX1
:
155 case VERT_ATTRIB_TEX2
:
156 case VERT_ATTRIB_TEX3
:
157 case VERT_ATTRIB_TEX4
:
158 case VERT_ATTRIB_TEX5
:
159 case VERT_ATTRIB_TEX6
:
160 case VERT_ATTRIB_TEX7
:
161 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
162 vs_input_semantic_index
[slot
] = num_generic
++;
164 case VERT_ATTRIB_GENERIC0
:
165 case VERT_ATTRIB_GENERIC1
:
166 case VERT_ATTRIB_GENERIC2
:
167 case VERT_ATTRIB_GENERIC3
:
168 case VERT_ATTRIB_GENERIC4
:
169 case VERT_ATTRIB_GENERIC5
:
170 case VERT_ATTRIB_GENERIC6
:
171 case VERT_ATTRIB_GENERIC7
:
172 case VERT_ATTRIB_GENERIC8
:
173 case VERT_ATTRIB_GENERIC9
:
174 case VERT_ATTRIB_GENERIC10
:
175 case VERT_ATTRIB_GENERIC11
:
176 case VERT_ATTRIB_GENERIC12
:
177 case VERT_ATTRIB_GENERIC13
:
178 case VERT_ATTRIB_GENERIC14
:
179 case VERT_ATTRIB_GENERIC15
:
180 assert(attr
< VERT_ATTRIB_MAX
);
181 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
182 vs_input_semantic_index
[slot
] = num_generic
++;
188 input_flags
[slot
] = stvp
->Base
.Base
.InputFlags
[attr
];
193 if (outputMapping
&& outputSemanticName
) {
194 printf("VERT_RESULT written out_slot semantic_name semantic_index\n");
195 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
196 printf(" %-2d %c %3d %2d %2d\n",
198 ((stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) ? 'Y' : ' '),
200 outputSemanticName
[attr
],
201 outputSemanticIndex
[attr
]);
206 /* initialize output semantics to defaults */
207 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
208 vs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
209 vs_output_semantic_index
[i
] = 0;
210 output_flags
[i
] = 0x0;
215 * Determine number of outputs, the (default) output register
216 * mapping and the semantic information for each output.
218 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
219 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
223 * Pass in the fragment program's input's semantic info.
224 * Use the generic semantic indexes from there, instead of
229 slot
= outputMapping
[attr
];
233 slot
= vs_num_outputs
;
235 defaultOutputMapping
[attr
] = slot
;
239 case VERT_RESULT_HPOS
:
241 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
242 vs_output_semantic_index
[slot
] = 0;
244 case VERT_RESULT_COL0
:
245 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
246 vs_output_semantic_index
[slot
] = 0;
248 case VERT_RESULT_COL1
:
249 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
250 vs_output_semantic_index
[slot
] = 1;
252 case VERT_RESULT_BFC0
:
253 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
254 vs_output_semantic_index
[slot
] = 0;
256 case VERT_RESULT_BFC1
:
257 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
258 vs_output_semantic_index
[slot
] = 1;
260 case VERT_RESULT_FOGC
:
261 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
262 vs_output_semantic_index
[slot
] = 0;
264 case VERT_RESULT_PSIZ
:
265 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
266 vs_output_semantic_index
[slot
] = 0;
268 case VERT_RESULT_EDGE
:
271 case VERT_RESULT_TEX0
:
272 case VERT_RESULT_TEX1
:
273 case VERT_RESULT_TEX2
:
274 case VERT_RESULT_TEX3
:
275 case VERT_RESULT_TEX4
:
276 case VERT_RESULT_TEX5
:
277 case VERT_RESULT_TEX6
:
278 case VERT_RESULT_TEX7
:
280 case VERT_RESULT_VAR0
:
283 if (outputSemanticName
) {
284 /* use provided semantic into */
285 assert(outputSemanticName
[attr
] != TGSI_SEMANTIC_COUNT
);
286 vs_output_semantic_name
[slot
] = outputSemanticName
[attr
];
287 vs_output_semantic_index
[slot
] = outputSemanticIndex
[attr
];
290 /* use default semantic info */
291 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
292 vs_output_semantic_index
[slot
] = num_generic
++;
296 output_flags
[slot
] = stvp
->Base
.Base
.OutputFlags
[attr
];
300 assert(vs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
304 /* find max output slot referenced to compute vs_num_outputs */
306 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
307 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
308 maxSlot
= outputMapping
[attr
];
310 vs_num_outputs
= maxSlot
+ 1;
313 outputMapping
= defaultOutputMapping
;
316 /* free old shader state, if any */
317 if (stvp
->state
.tokens
) {
318 _mesa_free((void *) 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
;
326 /* XXX: fix static allocation of tokens:
328 num_tokens
= st_translate_mesa_program(st
->ctx
,
329 TGSI_PROCESSOR_VERTEX
,
333 stvp
->input_to_index
,
334 vs_input_semantic_name
,
335 vs_input_semantic_index
,
341 vs_output_semantic_name
,
342 vs_output_semantic_index
,
344 /* tokenized result */
345 tokens
, ST_MAX_SHADER_TOKENS
);
347 assert(num_tokens
< ST_MAX_SHADER_TOKENS
);
349 vs
.tokens
= (struct tgsi_token
*)
350 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
352 stvp
->num_inputs
= vs_num_inputs
;
353 stvp
->state
= vs
; /* struct copy */
354 stvp
->driver_shader
= pipe
->create_vs_state(pipe
, &vs
);
357 _mesa_print_program(&stvp
->Base
.Base
);
360 tgsi_dump( vs
.tokens
, 0 );
366 * Translate a Mesa fragment shader into a TGSI shader.
367 * \param inputMapping to map fragment program input registers to TGSI
369 * \param tokensOut destination for TGSI tokens
370 * \return pointer to cached pipe_shader object.
373 st_translate_fragment_program(struct st_context
*st
,
374 struct st_fragment_program
*stfp
,
375 const GLuint inputMapping
[])
377 struct pipe_context
*pipe
= st
->pipe
;
378 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
379 GLuint outputMapping
[FRAG_RESULT_MAX
];
380 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
381 struct pipe_shader_state fs
;
382 GLuint interpMode
[16]; /* XXX size? */
384 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
386 GLuint num_generic
= 0;
389 uint fs_num_inputs
= 0;
391 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
392 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
393 uint fs_num_outputs
= 0;
395 GLbitfield input_flags
[MAX_PROGRAM_INPUTS
];
396 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
398 memset(&fs
, 0, sizeof(fs
));
399 memset(input_flags
, 0, sizeof(input_flags
));
400 memset(output_flags
, 0, sizeof(output_flags
));
402 /* which vertex output goes to the first fragment input: */
403 if (inputsRead
& FRAG_BIT_WPOS
)
409 * Convert Mesa program inputs to TGSI input register semantics.
411 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
412 if (inputsRead
& (1 << attr
)) {
413 const GLuint slot
= fs_num_inputs
;
415 defaultInputMapping
[attr
] = slot
;
417 stfp
->input_map
[slot
] = vslot
++;
422 case FRAG_ATTRIB_WPOS
:
423 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
424 stfp
->input_semantic_index
[slot
] = 0;
425 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
427 case FRAG_ATTRIB_COL0
:
428 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
429 stfp
->input_semantic_index
[slot
] = 0;
430 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
432 case FRAG_ATTRIB_COL1
:
433 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
434 stfp
->input_semantic_index
[slot
] = 1;
435 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
437 case FRAG_ATTRIB_FOGC
:
438 if (stfp
->Base
.UsesPointCoord
) {
439 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
440 stfp
->input_semantic_index
[slot
] = num_generic
++;
442 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
443 stfp
->input_semantic_index
[slot
] = 0;
445 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
447 case FRAG_ATTRIB_TEX0
:
448 case FRAG_ATTRIB_TEX1
:
449 case FRAG_ATTRIB_TEX2
:
450 case FRAG_ATTRIB_TEX3
:
451 case FRAG_ATTRIB_TEX4
:
452 case FRAG_ATTRIB_TEX5
:
453 case FRAG_ATTRIB_TEX6
:
454 case FRAG_ATTRIB_TEX7
:
455 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
456 stfp
->input_semantic_index
[slot
] = num_generic
++;
457 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
459 case FRAG_ATTRIB_VAR0
:
462 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
463 stfp
->input_semantic_index
[slot
] = num_generic
++;
464 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
467 input_flags
[slot
] = stfp
->Base
.Base
.InputFlags
[attr
];
472 * Semantics and mapping for outputs
476 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
478 /* if z is written, emit that first */
479 if (outputsWritten
& (1 << FRAG_RESULT_DEPTH
)) {
480 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
481 fs_output_semantic_index
[fs_num_outputs
] = 0;
482 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
484 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
487 /* handle remaning outputs (color) */
488 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
489 if (outputsWritten
& (1 << attr
)) {
491 case FRAG_RESULT_DEPTH
:
496 assert(attr
== FRAG_RESULT_COLOR
||
497 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
498 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
499 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
500 outputMapping
[attr
] = fs_num_outputs
;
505 output_flags
[fs_num_outputs
] = stfp
->Base
.Base
.OutputFlags
[attr
];
513 inputMapping
= defaultInputMapping
;
515 /* XXX: fix static allocation of tokens:
517 num_tokens
= st_translate_mesa_program(st
->ctx
,
518 TGSI_PROCESSOR_FRAGMENT
,
523 stfp
->input_semantic_name
,
524 stfp
->input_semantic_index
,
530 fs_output_semantic_name
,
531 fs_output_semantic_index
,
533 /* tokenized result */
534 tokens
, ST_MAX_SHADER_TOKENS
);
536 assert(num_tokens
< ST_MAX_SHADER_TOKENS
);
538 fs
.tokens
= (struct tgsi_token
*)
539 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
541 stfp
->state
= fs
; /* struct copy */
542 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &fs
);
545 _mesa_print_program(&stfp
->Base
.Base
);
548 tgsi_dump( fs
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
553 * Debug- print current shader text
556 st_print_shaders(GLcontext
*ctx
)
558 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
561 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
562 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
563 printf("%s\n", shProg
->Shaders
[i
]->Source
);