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 uint vs_num_inputs
= 0;
74 ubyte vs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
75 ubyte vs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
76 uint vs_num_outputs
= 0;
78 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
80 // memset(&vs, 0, sizeof(vs));
81 memset(output_flags
, 0, sizeof(output_flags
));
83 if (stvp
->Base
.IsPositionInvariant
)
84 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
87 * Determine number of inputs, the mappings between VERT_ATTRIB_x
88 * and TGSI generic input indexes, plus input attrib semantic info.
90 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
91 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
92 stvp
->input_to_index
[attr
] = vs_num_inputs
;
93 stvp
->index_to_input
[vs_num_inputs
] = attr
;
100 if (outputMapping
&& outputSemanticName
) {
101 printf("VERT_RESULT written out_slot semantic_name semantic_index\n");
102 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
103 printf(" %-2d %c %3d %2d %2d\n",
105 ((stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) ? 'Y' : ' '),
107 outputSemanticName
[attr
],
108 outputSemanticIndex
[attr
]);
113 /* initialize output semantics to defaults */
114 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
115 assert(i
< Elements(vs_output_semantic_name
));
116 vs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
117 vs_output_semantic_index
[i
] = 0;
118 output_flags
[i
] = 0x0;
123 * Determine number of outputs, the (default) output register
124 * mapping and the semantic information for each output.
126 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
127 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
131 * Pass in the fragment program's input's semantic info.
132 * Use the generic semantic indexes from there, instead of
137 slot
= outputMapping
[attr
];
141 slot
= vs_num_outputs
;
143 defaultOutputMapping
[attr
] = slot
;
147 case VERT_RESULT_HPOS
:
149 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
150 vs_output_semantic_index
[slot
] = 0;
152 case VERT_RESULT_COL0
:
153 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
154 vs_output_semantic_index
[slot
] = 0;
156 case VERT_RESULT_COL1
:
157 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
158 vs_output_semantic_index
[slot
] = 1;
160 case VERT_RESULT_BFC0
:
161 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
162 vs_output_semantic_index
[slot
] = 0;
164 case VERT_RESULT_BFC1
:
165 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
166 vs_output_semantic_index
[slot
] = 1;
168 case VERT_RESULT_FOGC
:
169 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
170 vs_output_semantic_index
[slot
] = 0;
172 case VERT_RESULT_PSIZ
:
173 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
174 vs_output_semantic_index
[slot
] = 0;
176 case VERT_RESULT_EDGE
:
179 case VERT_RESULT_TEX0
:
180 case VERT_RESULT_TEX1
:
181 case VERT_RESULT_TEX2
:
182 case VERT_RESULT_TEX3
:
183 case VERT_RESULT_TEX4
:
184 case VERT_RESULT_TEX5
:
185 case VERT_RESULT_TEX6
:
186 case VERT_RESULT_TEX7
:
188 case VERT_RESULT_VAR0
:
191 assert(slot
< Elements(vs_output_semantic_name
));
192 if (outputSemanticName
) {
193 /* use provided semantic into */
194 assert(outputSemanticName
[attr
] != TGSI_SEMANTIC_COUNT
);
195 vs_output_semantic_name
[slot
] = outputSemanticName
[attr
];
196 vs_output_semantic_index
[slot
] = outputSemanticIndex
[attr
];
199 /* use default semantic info */
200 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
201 vs_output_semantic_index
[slot
] = num_generic
++;
205 assert(slot
< Elements(output_flags
));
206 output_flags
[slot
] = stvp
->Base
.Base
.OutputFlags
[attr
];
211 /* find max output slot referenced to compute vs_num_outputs */
213 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
214 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
215 maxSlot
= outputMapping
[attr
];
217 vs_num_outputs
= maxSlot
+ 1;
220 outputMapping
= defaultOutputMapping
;
226 printf("outputMapping? %d\n", outputMapping
? 1 : 0);
228 printf("attr -> slot\n");
229 for (i
= 0; i
< 16; i
++) {
230 printf(" %2d %3d\n", i
, outputMapping
[i
]);
233 printf("slot sem_name sem_index\n");
234 for (i
= 0; i
< vs_num_outputs
; i
++) {
235 printf(" %2d %d %d\n",
237 vs_output_semantic_name
[i
],
238 vs_output_semantic_index
[i
]);
243 /* free old shader state, if any */
244 if (stvp
->state
.tokens
) {
245 st_free_tokens(stvp
->state
.tokens
);
246 stvp
->state
.tokens
= NULL
;
248 if (stvp
->driver_shader
) {
249 cso_delete_vertex_shader(st
->cso_context
, stvp
->driver_shader
);
250 stvp
->driver_shader
= NULL
;
254 st_translate_mesa_program(st
->ctx
,
255 TGSI_PROCESSOR_VERTEX
,
259 stvp
->input_to_index
,
260 NULL
, /* input semantic name */
261 NULL
, /* input semantic index */
266 vs_output_semantic_name
,
267 vs_output_semantic_index
,
270 stvp
->num_inputs
= vs_num_inputs
;
271 stvp
->driver_shader
= pipe
->create_vs_state(pipe
, &stvp
->state
);
273 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
274 _mesa_print_program(&stvp
->Base
.Base
);
278 if (ST_DEBUG
& DEBUG_TGSI
) {
279 tgsi_dump( stvp
->state
.tokens
, 0 );
287 * Translate a Mesa fragment shader into a TGSI shader.
288 * \param inputMapping to map fragment program input registers to TGSI
290 * \return pointer to cached pipe_shader object.
293 st_translate_fragment_program(struct st_context
*st
,
294 struct st_fragment_program
*stfp
,
295 const GLuint inputMapping
[])
297 struct pipe_context
*pipe
= st
->pipe
;
298 GLuint outputMapping
[FRAG_RESULT_MAX
];
299 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
300 GLuint interpMode
[16]; /* XXX size? */
302 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
304 GLuint num_generic
= 0;
306 uint fs_num_inputs
= 0;
308 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
309 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
310 uint fs_num_outputs
= 0;
312 GLbitfield output_flags
[MAX_PROGRAM_OUTPUTS
];
314 // memset(&fs, 0, sizeof(fs));
315 memset(output_flags
, 0, sizeof(output_flags
));
317 /* which vertex output goes to the first fragment input: */
318 if (inputsRead
& FRAG_BIT_WPOS
)
324 * Convert Mesa program inputs to TGSI input register semantics.
326 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
327 if (inputsRead
& (1 << attr
)) {
328 const GLuint slot
= fs_num_inputs
;
330 defaultInputMapping
[attr
] = slot
;
332 stfp
->input_map
[slot
] = vslot
++;
337 case FRAG_ATTRIB_WPOS
:
338 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
339 stfp
->input_semantic_index
[slot
] = 0;
340 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
342 case FRAG_ATTRIB_COL0
:
343 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
344 stfp
->input_semantic_index
[slot
] = 0;
345 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
347 case FRAG_ATTRIB_COL1
:
348 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
349 stfp
->input_semantic_index
[slot
] = 1;
350 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
352 case FRAG_ATTRIB_FOGC
:
353 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
354 stfp
->input_semantic_index
[slot
] = 0;
355 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
357 case FRAG_ATTRIB_FACE
:
358 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
359 stfp
->input_semantic_index
[slot
] = num_generic
++;
360 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
362 case FRAG_ATTRIB_PNTC
:
363 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
364 stfp
->input_semantic_index
[slot
] = num_generic
++;
365 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
367 case FRAG_ATTRIB_TEX0
:
368 case FRAG_ATTRIB_TEX1
:
369 case FRAG_ATTRIB_TEX2
:
370 case FRAG_ATTRIB_TEX3
:
371 case FRAG_ATTRIB_TEX4
:
372 case FRAG_ATTRIB_TEX5
:
373 case FRAG_ATTRIB_TEX6
:
374 case FRAG_ATTRIB_TEX7
:
375 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
376 stfp
->input_semantic_index
[slot
] = num_generic
++;
377 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
379 case FRAG_ATTRIB_VAR0
:
382 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
383 stfp
->input_semantic_index
[slot
] = num_generic
++;
384 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
390 * Semantics and mapping for outputs
394 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
396 /* if z is written, emit that first */
397 if (outputsWritten
& (1 << FRAG_RESULT_DEPTH
)) {
398 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
399 fs_output_semantic_index
[fs_num_outputs
] = 0;
400 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
402 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
405 /* handle remaning outputs (color) */
406 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
407 if (outputsWritten
& (1 << attr
)) {
409 case FRAG_RESULT_DEPTH
:
414 assert(attr
== FRAG_RESULT_COLOR
||
415 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
416 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
417 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
418 outputMapping
[attr
] = fs_num_outputs
;
423 output_flags
[fs_num_outputs
] = stfp
->Base
.Base
.OutputFlags
[attr
];
431 inputMapping
= defaultInputMapping
;
434 st_translate_mesa_program(st
->ctx
,
435 TGSI_PROCESSOR_FRAGMENT
,
440 stfp
->input_semantic_name
,
441 stfp
->input_semantic_index
,
446 fs_output_semantic_name
,
447 fs_output_semantic_index
,
450 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->state
);
452 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
453 _mesa_print_program(&stfp
->Base
.Base
);
457 if (ST_DEBUG
& DEBUG_TGSI
) {
458 tgsi_dump( stfp
->state
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
465 * Debug- print current shader text
468 st_print_shaders(GLcontext
*ctx
)
470 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
473 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
474 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
475 printf("%s\n", shProg
->Shaders
[i
]->Source
);