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"
47 #include "st_program.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "cso_cache/cso_context.h"
54 * Clean out any old compilations:
57 st_vp_release_varients( struct st_context
*st
,
58 struct st_vertex_program
*stvp
)
60 struct st_vp_varient
*vpv
;
62 for (vpv
= stvp
->varients
; vpv
; ) {
63 struct st_vp_varient
*next
= vpv
->next
;
65 if (vpv
->driver_shader
)
66 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
68 #if FEATURE_feedback || FEATURE_rastpos
70 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
74 st_free_tokens(vpv
->tgsi
.tokens
);
81 stvp
->varients
= NULL
;
88 * Translate a Mesa vertex shader into a TGSI shader.
89 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
90 * to TGSI output slots
91 * \param tokensOut destination for TGSI tokens
92 * \return pointer to cached pipe_shader object.
95 st_prepare_vertex_program(struct st_context
*st
,
96 struct st_vertex_program
*stvp
)
100 stvp
->num_inputs
= 0;
101 stvp
->num_outputs
= 0;
103 if (stvp
->Base
.IsPositionInvariant
)
104 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
106 assert(stvp
->Base
.Base
.NumInstructions
> 1);
109 * Determine number of inputs, the mappings between VERT_ATTRIB_x
110 * and TGSI generic input indexes, plus input attrib semantic info.
112 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
113 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
114 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
115 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
119 /* bit of a hack, presetup potentially unused edgeflag input */
120 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
121 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
123 /* Compute mapping of vertex program outputs to slots.
125 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
126 if ((stvp
->Base
.Base
.OutputsWritten
& BITFIELD64_BIT(attr
)) == 0) {
127 stvp
->result_to_output
[attr
] = ~0;
130 unsigned slot
= stvp
->num_outputs
++;
132 stvp
->result_to_output
[attr
] = slot
;
135 case VERT_RESULT_HPOS
:
136 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
137 stvp
->output_semantic_index
[slot
] = 0;
139 case VERT_RESULT_COL0
:
140 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
141 stvp
->output_semantic_index
[slot
] = 0;
143 case VERT_RESULT_COL1
:
144 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
145 stvp
->output_semantic_index
[slot
] = 1;
147 case VERT_RESULT_BFC0
:
148 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
149 stvp
->output_semantic_index
[slot
] = 0;
151 case VERT_RESULT_BFC1
:
152 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
153 stvp
->output_semantic_index
[slot
] = 1;
155 case VERT_RESULT_FOGC
:
156 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
157 stvp
->output_semantic_index
[slot
] = 0;
159 case VERT_RESULT_PSIZ
:
160 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
161 stvp
->output_semantic_index
[slot
] = 0;
163 case VERT_RESULT_EDGE
:
167 case VERT_RESULT_TEX0
:
168 case VERT_RESULT_TEX1
:
169 case VERT_RESULT_TEX2
:
170 case VERT_RESULT_TEX3
:
171 case VERT_RESULT_TEX4
:
172 case VERT_RESULT_TEX5
:
173 case VERT_RESULT_TEX6
:
174 case VERT_RESULT_TEX7
:
175 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
176 stvp
->output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
179 case VERT_RESULT_VAR0
:
181 assert(attr
< VERT_RESULT_MAX
);
182 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
183 stvp
->output_semantic_index
[slot
] = (FRAG_ATTRIB_VAR0
-
191 /* similar hack to above, presetup potentially unused edgeflag output */
192 stvp
->result_to_output
[VERT_RESULT_EDGE
] = stvp
->num_outputs
;
193 stvp
->output_semantic_name
[stvp
->num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
194 stvp
->output_semantic_index
[stvp
->num_outputs
] = 0;
198 struct st_vp_varient
*
199 st_translate_vertex_program(struct st_context
*st
,
200 struct st_vertex_program
*stvp
,
201 const struct st_vp_varient_key
*key
)
203 struct st_vp_varient
*vpv
= CALLOC_STRUCT(st_vp_varient
);
204 struct pipe_context
*pipe
= st
->pipe
;
205 struct ureg_program
*ureg
;
206 enum pipe_error error
;
207 unsigned num_outputs
;
209 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
215 vpv
->num_inputs
= stvp
->num_inputs
;
216 num_outputs
= stvp
->num_outputs
;
217 if (key
->passthrough_edgeflags
) {
222 if (ST_DEBUG
& DEBUG_MESA
) {
223 _mesa_print_program(&stvp
->Base
.Base
);
224 _mesa_print_program_parameters(st
->ctx
, &stvp
->Base
.Base
);
229 st_translate_mesa_program(st
->ctx
,
230 TGSI_PROCESSOR_VERTEX
,
235 stvp
->input_to_index
,
236 NULL
, /* input semantic name */
237 NULL
, /* input semantic index */
241 stvp
->result_to_output
,
242 stvp
->output_semantic_name
,
243 stvp
->output_semantic_index
,
244 key
->passthrough_edgeflags
);
249 vpv
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
250 if (!vpv
->tgsi
.tokens
)
253 ureg_destroy( ureg
);
255 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->tgsi
);
257 if (ST_DEBUG
& DEBUG_TGSI
) {
258 tgsi_dump( vpv
->tgsi
.tokens
, 0 );
265 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__
);
266 _mesa_print_program(&stvp
->Base
.Base
);
269 ureg_destroy( ureg
);
276 * Translate a Mesa fragment shader into a TGSI shader.
277 * \return pointer to cached pipe_shader object.
280 st_translate_fragment_program(struct st_context
*st
,
281 struct st_fragment_program
*stfp
)
283 struct pipe_context
*pipe
= st
->pipe
;
284 GLuint outputMapping
[FRAG_RESULT_MAX
];
285 GLuint inputMapping
[FRAG_ATTRIB_MAX
];
286 GLuint interpMode
[PIPE_MAX_SHADER_INPUTS
]; /* XXX size? */
288 enum pipe_error error
;
289 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
290 struct ureg_program
*ureg
;
292 ubyte input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
293 ubyte input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
294 uint fs_num_inputs
= 0;
296 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
297 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
298 uint fs_num_outputs
= 0;
301 * Convert Mesa program inputs to TGSI input register semantics.
303 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
304 if (inputsRead
& (1 << attr
)) {
305 const GLuint slot
= fs_num_inputs
++;
307 inputMapping
[attr
] = slot
;
310 case FRAG_ATTRIB_WPOS
:
311 input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
312 input_semantic_index
[slot
] = 0;
313 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
315 case FRAG_ATTRIB_COL0
:
316 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
317 input_semantic_index
[slot
] = 0;
318 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
320 case FRAG_ATTRIB_COL1
:
321 input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
322 input_semantic_index
[slot
] = 1;
323 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
325 case FRAG_ATTRIB_FOGC
:
326 input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
327 input_semantic_index
[slot
] = 0;
328 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
330 case FRAG_ATTRIB_FACE
:
331 input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
332 input_semantic_index
[slot
] = 0;
333 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
335 case FRAG_ATTRIB_PNTC
:
336 /* This is a hack. We really need a new semantic label for
337 * point coord. The draw module needs to know which fragment
338 * shader input is the point coord attribute so that it can set
339 * up the right vertex attribute values.
341 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
342 input_semantic_index
[slot
] = 0;
343 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
346 /* In most cases, there is nothing special about these
347 * inputs, so adopt a convention to use the generic
348 * semantic name and the mesa FRAG_ATTRIB_ number as the
351 * All that is required is that the vertex shader labels
352 * its own outputs similarly, and that the vertex shader
353 * generates at least every output required by the
354 * fragment shader plus fixed-function hardware (such as
357 * There is no requirement that semantic indexes start at
358 * zero or be restricted to a particular range -- nobody
359 * should be building tables based on semantic index.
361 case FRAG_ATTRIB_TEX0
:
362 case FRAG_ATTRIB_TEX1
:
363 case FRAG_ATTRIB_TEX2
:
364 case FRAG_ATTRIB_TEX3
:
365 case FRAG_ATTRIB_TEX4
:
366 case FRAG_ATTRIB_TEX5
:
367 case FRAG_ATTRIB_TEX6
:
368 case FRAG_ATTRIB_TEX7
:
369 case FRAG_ATTRIB_VAR0
:
371 /* Actually, let's try and zero-base this just for
372 * readability of the generated TGSI.
374 assert(attr
>= FRAG_ATTRIB_TEX0
);
375 input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
376 input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
377 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
382 inputMapping
[attr
] = -1;
387 * Semantics and mapping for outputs
391 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
393 /* if z is written, emit that first */
394 if (outputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
395 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
396 fs_output_semantic_index
[fs_num_outputs
] = 0;
397 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
399 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
402 /* handle remaning outputs (color) */
403 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
404 if (outputsWritten
& BITFIELD64_BIT(attr
)) {
406 case FRAG_RESULT_DEPTH
:
411 assert(attr
== FRAG_RESULT_COLOR
||
412 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
413 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
414 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
415 outputMapping
[attr
] = fs_num_outputs
;
425 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
429 if (ST_DEBUG
& DEBUG_MESA
) {
430 _mesa_print_program(&stfp
->Base
.Base
);
431 _mesa_print_program_parameters(st
->ctx
, &stfp
->Base
.Base
);
436 st_translate_mesa_program(st
->ctx
,
437 TGSI_PROCESSOR_FRAGMENT
,
444 input_semantic_index
,
449 fs_output_semantic_name
,
450 fs_output_semantic_index
, FALSE
);
452 stfp
->tgsi
.tokens
= ureg_get_tokens( ureg
, NULL
);
453 ureg_destroy( ureg
);
454 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->tgsi
);
456 if (ST_DEBUG
& DEBUG_TGSI
) {
457 tgsi_dump( stfp
->tgsi
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
464 * Debug- print current shader text
467 st_print_shaders(GLcontext
*ctx
)
469 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
472 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
473 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
474 printf("%s\n", shProg
->Shaders
[i
]->Source
);