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 4096
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
= MALLOC(size
);
66 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 assert(attr
< VERT_ATTRIB_MAX
);
173 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
174 vs_input_semantic_index
[slot
] = num_generic
++;
180 input_flags
[slot
] = stvp
->Base
.Base
.InputFlags
[attr
];
185 if (outputMapping
&& outputSemanticName
) {
186 printf("VERT_RESULT written out_slot semantic_name semantic_index\n");
187 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
188 printf(" %-2d %c %3d %2d %2d\n",
190 ((stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) ? 'Y' : ' '),
192 outputSemanticName
[attr
],
193 outputSemanticIndex
[attr
]);
198 /* initialize output semantics to defaults */
199 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
200 vs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
201 vs_output_semantic_index
[i
] = 0;
202 output_flags
[i
] = 0x0;
207 * Determine number of outputs, the (default) output register
208 * mapping and the semantic information for each output.
210 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
211 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
215 * Pass in the fragment program's input's semantic info.
216 * Use the generic semantic indexes from there, instead of
221 slot
= outputMapping
[attr
];
225 slot
= vs_num_outputs
;
227 defaultOutputMapping
[attr
] = slot
;
231 case VERT_RESULT_HPOS
:
233 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
234 vs_output_semantic_index
[slot
] = 0;
236 case VERT_RESULT_COL0
:
237 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
238 vs_output_semantic_index
[slot
] = 0;
240 case VERT_RESULT_COL1
:
241 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
242 vs_output_semantic_index
[slot
] = 1;
244 case VERT_RESULT_BFC0
:
245 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
246 vs_output_semantic_index
[slot
] = 0;
248 case VERT_RESULT_BFC1
:
249 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
250 vs_output_semantic_index
[slot
] = 1;
252 case VERT_RESULT_FOGC
:
253 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
254 vs_output_semantic_index
[slot
] = 0;
256 case VERT_RESULT_PSIZ
:
257 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
258 vs_output_semantic_index
[slot
] = 0;
260 case VERT_RESULT_EDGE
:
263 case VERT_RESULT_TEX0
:
264 case VERT_RESULT_TEX1
:
265 case VERT_RESULT_TEX2
:
266 case VERT_RESULT_TEX3
:
267 case VERT_RESULT_TEX4
:
268 case VERT_RESULT_TEX5
:
269 case VERT_RESULT_TEX6
:
270 case VERT_RESULT_TEX7
:
272 case VERT_RESULT_VAR0
:
275 if (outputSemanticName
) {
276 /* use provided semantic into */
277 assert(outputSemanticName
[attr
] != TGSI_SEMANTIC_COUNT
);
278 vs_output_semantic_name
[slot
] = outputSemanticName
[attr
];
279 vs_output_semantic_index
[slot
] = outputSemanticIndex
[attr
];
282 /* use default semantic info */
283 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
284 vs_output_semantic_index
[slot
] = num_generic
++;
288 output_flags
[slot
] = stvp
->Base
.Base
.OutputFlags
[attr
];
292 assert(vs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
296 /* find max output slot referenced to compute vs_num_outputs */
298 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
299 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
300 maxSlot
= outputMapping
[attr
];
302 vs_num_outputs
= maxSlot
+ 1;
305 outputMapping
= defaultOutputMapping
;
308 /* free old shader state, if any */
309 if (stvp
->state
.tokens
) {
310 FREE((void *) stvp
->state
.tokens
);
311 stvp
->state
.tokens
= NULL
;
313 if (stvp
->driver_shader
) {
314 cso_delete_vertex_shader(st
->cso_context
, stvp
->driver_shader
);
315 stvp
->driver_shader
= NULL
;
318 /* XXX: fix static allocation of tokens:
320 num_tokens
= st_translate_mesa_program(st
->ctx
,
321 TGSI_PROCESSOR_VERTEX
,
325 stvp
->input_to_index
,
326 vs_input_semantic_name
,
327 vs_input_semantic_index
,
333 vs_output_semantic_name
,
334 vs_output_semantic_index
,
336 /* tokenized result */
337 tokens
, ST_MAX_SHADER_TOKENS
);
339 assert(num_tokens
< ST_MAX_SHADER_TOKENS
);
341 vs
.tokens
= (struct tgsi_token
*)
342 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
344 stvp
->num_inputs
= vs_num_inputs
;
345 stvp
->state
= vs
; /* struct copy */
346 stvp
->driver_shader
= pipe
->create_vs_state(pipe
, &vs
);
349 _mesa_print_program(&stvp
->Base
.Base
);
352 tgsi_dump( vs
.tokens
, 0 );
358 * Translate a Mesa fragment shader into a TGSI shader.
359 * \param inputMapping to map fragment program input registers to TGSI
361 * \param tokensOut destination for TGSI tokens
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 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
371 GLuint outputMapping
[FRAG_RESULT_MAX
];
372 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
373 struct pipe_shader_state fs
;
374 GLuint interpMode
[16]; /* XXX size? */
376 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
378 GLuint num_generic
= 0;
381 uint fs_num_inputs
= 0;
383 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
384 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
385 uint fs_num_outputs
= 0;
387 GLbitfield input_flags
[MAX_PROGRAM_INPUTS
];
388 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
390 memset(&fs
, 0, sizeof(fs
));
391 memset(input_flags
, 0, sizeof(input_flags
));
392 memset(output_flags
, 0, sizeof(output_flags
));
394 /* which vertex output goes to the first fragment input: */
395 if (inputsRead
& FRAG_BIT_WPOS
)
401 * Convert Mesa program inputs to TGSI input register semantics.
403 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
404 if (inputsRead
& (1 << attr
)) {
405 const GLuint slot
= fs_num_inputs
;
407 defaultInputMapping
[attr
] = slot
;
409 stfp
->input_map
[slot
] = vslot
++;
414 case FRAG_ATTRIB_WPOS
:
415 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
416 stfp
->input_semantic_index
[slot
] = 0;
417 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
419 case FRAG_ATTRIB_COL0
:
420 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
421 stfp
->input_semantic_index
[slot
] = 0;
422 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
424 case FRAG_ATTRIB_COL1
:
425 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
426 stfp
->input_semantic_index
[slot
] = 1;
427 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
429 case FRAG_ATTRIB_FOGC
:
430 if (stfp
->Base
.UsesPointCoord
)
431 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
433 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
434 stfp
->input_semantic_index
[slot
] = 0;
435 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
437 case FRAG_ATTRIB_TEX0
:
438 case FRAG_ATTRIB_TEX1
:
439 case FRAG_ATTRIB_TEX2
:
440 case FRAG_ATTRIB_TEX3
:
441 case FRAG_ATTRIB_TEX4
:
442 case FRAG_ATTRIB_TEX5
:
443 case FRAG_ATTRIB_TEX6
:
444 case FRAG_ATTRIB_TEX7
:
445 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
446 stfp
->input_semantic_index
[slot
] = num_generic
++;
447 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
449 case FRAG_ATTRIB_VAR0
:
452 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
453 stfp
->input_semantic_index
[slot
] = num_generic
++;
454 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
457 input_flags
[slot
] = stfp
->Base
.Base
.InputFlags
[attr
];
462 * Semantics and mapping for outputs
466 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
468 /* if z is written, emit that first */
469 if (outputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
470 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
471 fs_output_semantic_index
[fs_num_outputs
] = 0;
472 outputMapping
[FRAG_RESULT_DEPR
] = fs_num_outputs
;
474 outputsWritten
&= ~(1 << FRAG_RESULT_DEPR
);
477 /* handle remaning outputs (color) */
478 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
479 if (outputsWritten
& (1 << attr
)) {
481 case FRAG_RESULT_DEPR
:
485 case FRAG_RESULT_COLR
:
486 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
487 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
488 outputMapping
[attr
] = fs_num_outputs
;
495 output_flags
[fs_num_outputs
] = stfp
->Base
.Base
.OutputFlags
[attr
];
503 inputMapping
= defaultInputMapping
;
505 /* XXX: fix static allocation of tokens:
507 num_tokens
= st_translate_mesa_program(st
->ctx
,
508 TGSI_PROCESSOR_FRAGMENT
,
513 stfp
->input_semantic_name
,
514 stfp
->input_semantic_index
,
520 fs_output_semantic_name
,
521 fs_output_semantic_index
,
523 /* tokenized result */
524 tokens
, ST_MAX_SHADER_TOKENS
);
526 assert(num_tokens
< ST_MAX_SHADER_TOKENS
);
528 fs
.tokens
= (struct tgsi_token
*)
529 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
531 stfp
->state
= fs
; /* struct copy */
532 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &fs
);
535 _mesa_print_program(&stfp
->Base
.Base
);
538 tgsi_dump( fs
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
543 * Debug- print current shader text
546 st_print_shaders(GLcontext
*ctx
)
548 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
551 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
552 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
553 printf("%s\n", shProg
->Shaders
[i
]->Source
);