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
);
69 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
71 if (vpv
->state
.tokens
)
72 st_free_tokens(vpv
->state
.tokens
);
79 stvp
->varients
= NULL
;
86 * Translate a Mesa vertex shader into a TGSI shader.
87 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
88 * to TGSI output slots
89 * \param tokensOut destination for TGSI tokens
90 * \return pointer to cached pipe_shader object.
93 st_prepare_vertex_program(struct st_context
*st
,
94 struct st_vertex_program
*stvp
)
99 stvp
->num_outputs
= 0;
101 if (stvp
->Base
.IsPositionInvariant
)
102 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
104 assert(stvp
->Base
.Base
.NumInstructions
> 1);
107 * Determine number of inputs, the mappings between VERT_ATTRIB_x
108 * and TGSI generic input indexes, plus input attrib semantic info.
110 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
111 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
112 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
113 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
117 /* bit of a hack, presetup potentially unused edgeflag input */
118 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
119 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
121 /* Compute mapping of vertex program outputs to slots.
123 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
124 if ((stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) == 0) {
125 stvp
->result_to_output
[attr
] = ~0;
128 unsigned slot
= stvp
->num_outputs
++;
130 stvp
->result_to_output
[attr
] = slot
;
133 case VERT_RESULT_HPOS
:
134 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
135 stvp
->output_semantic_index
[slot
] = 0;
137 case VERT_RESULT_COL0
:
138 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
139 stvp
->output_semantic_index
[slot
] = 0;
141 case VERT_RESULT_COL1
:
142 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
143 stvp
->output_semantic_index
[slot
] = 1;
145 case VERT_RESULT_BFC0
:
146 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
147 stvp
->output_semantic_index
[slot
] = 0;
149 case VERT_RESULT_BFC1
:
150 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
151 stvp
->output_semantic_index
[slot
] = 1;
153 case VERT_RESULT_FOGC
:
154 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
155 stvp
->output_semantic_index
[slot
] = 0;
157 case VERT_RESULT_PSIZ
:
158 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
159 stvp
->output_semantic_index
[slot
] = 0;
161 case VERT_RESULT_EDGE
:
165 case VERT_RESULT_TEX0
:
166 case VERT_RESULT_TEX1
:
167 case VERT_RESULT_TEX2
:
168 case VERT_RESULT_TEX3
:
169 case VERT_RESULT_TEX4
:
170 case VERT_RESULT_TEX5
:
171 case VERT_RESULT_TEX6
:
172 case VERT_RESULT_TEX7
:
173 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
174 stvp
->output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
177 case VERT_RESULT_VAR0
:
179 assert(attr
< VERT_RESULT_MAX
);
180 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
181 stvp
->output_semantic_index
[slot
] = (FRAG_ATTRIB_VAR0
-
189 /* similar hack to above, presetup potentially unused edgeflag output */
190 stvp
->result_to_output
[VERT_RESULT_EDGE
] = stvp
->num_outputs
;
191 stvp
->output_semantic_name
[stvp
->num_outputs
] = TGSI_SEMANTIC_EDGEFLAG
;
192 stvp
->output_semantic_index
[stvp
->num_outputs
] = 0;
196 struct st_vp_varient
*
197 st_translate_vertex_program(struct st_context
*st
,
198 struct st_vertex_program
*stvp
,
199 const struct st_vp_varient_key
*key
)
201 struct st_vp_varient
*vpv
= CALLOC_STRUCT(st_vp_varient
);
202 struct pipe_context
*pipe
= st
->pipe
;
203 struct ureg_program
*ureg
;
204 enum pipe_error error
;
205 unsigned num_outputs
;
207 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
211 vpv
->num_inputs
= stvp
->num_inputs
;
212 num_outputs
= stvp
->num_outputs
;
213 if (key
->passthrough_edgeflags
) {
219 st_translate_mesa_program(st
->ctx
,
220 TGSI_PROCESSOR_VERTEX
,
225 stvp
->input_to_index
,
226 NULL
, /* input semantic name */
227 NULL
, /* input semantic index */
231 stvp
->result_to_output
,
232 stvp
->output_semantic_name
,
233 stvp
->output_semantic_index
,
234 key
->passthrough_edgeflags
);
239 vpv
->state
.tokens
= ureg_get_tokens( ureg
, NULL
);
240 if (!vpv
->state
.tokens
)
243 ureg_destroy( ureg
);
245 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->state
);
247 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
248 _mesa_print_program(&stvp
->Base
.Base
);
252 if (ST_DEBUG
& DEBUG_TGSI
) {
253 tgsi_dump( vpv
->state
.tokens
, 0 );
260 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__
);
261 _mesa_print_program(&stvp
->Base
.Base
);
264 ureg_destroy( ureg
);
271 * Translate a Mesa fragment shader into a TGSI shader.
272 * \return pointer to cached pipe_shader object.
275 st_translate_fragment_program(struct st_context
*st
,
276 struct st_fragment_program
*stfp
)
278 struct pipe_context
*pipe
= st
->pipe
;
279 GLuint outputMapping
[FRAG_RESULT_MAX
];
280 GLuint inputMapping
[FRAG_ATTRIB_MAX
];
281 GLuint interpMode
[16]; /* XXX size? */
283 enum pipe_error error
;
284 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
285 struct ureg_program
*ureg
;
287 uint fs_num_inputs
= 0;
289 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
290 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
291 uint fs_num_outputs
= 0;
294 * Convert Mesa program inputs to TGSI input register semantics.
296 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
297 if (inputsRead
& (1 << attr
)) {
298 const GLuint slot
= fs_num_inputs
++;
300 inputMapping
[attr
] = slot
;
303 case FRAG_ATTRIB_WPOS
:
304 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
305 stfp
->input_semantic_index
[slot
] = 0;
306 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
308 case FRAG_ATTRIB_COL0
:
309 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
310 stfp
->input_semantic_index
[slot
] = 0;
311 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
313 case FRAG_ATTRIB_COL1
:
314 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
315 stfp
->input_semantic_index
[slot
] = 1;
316 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
318 case FRAG_ATTRIB_FOGC
:
319 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
320 stfp
->input_semantic_index
[slot
] = 0;
321 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
323 case FRAG_ATTRIB_FACE
:
324 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
325 stfp
->input_semantic_index
[slot
] = 0;
326 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
329 /* In most cases, there is nothing special about these
330 * inputs, so adopt a convention to use the generic
331 * semantic name and the mesa FRAG_ATTRIB_ number as the
334 * All that is required is that the vertex shader labels
335 * its own outputs similarly, and that the vertex shader
336 * generates at least every output required by the
337 * fragment shader plus fixed-function hardware (such as
340 * There is no requirement that semantic indexes start at
341 * zero or be restricted to a particular range -- nobody
342 * should be building tables based on semantic index.
344 case FRAG_ATTRIB_TEX0
:
345 case FRAG_ATTRIB_TEX1
:
346 case FRAG_ATTRIB_TEX2
:
347 case FRAG_ATTRIB_TEX3
:
348 case FRAG_ATTRIB_TEX4
:
349 case FRAG_ATTRIB_TEX5
:
350 case FRAG_ATTRIB_TEX6
:
351 case FRAG_ATTRIB_TEX7
:
352 case FRAG_ATTRIB_PNTC
:
353 case FRAG_ATTRIB_VAR0
:
355 /* Actually, let's try and zero-base this just for
356 * readability of the generated TGSI.
358 assert(attr
>= FRAG_ATTRIB_TEX0
);
359 stfp
->input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
360 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
361 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
366 inputMapping
[attr
] = -1;
371 * Semantics and mapping for outputs
375 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
377 /* if z is written, emit that first */
378 if (outputsWritten
& (1 << FRAG_RESULT_DEPTH
)) {
379 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
380 fs_output_semantic_index
[fs_num_outputs
] = 0;
381 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
383 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
386 /* handle remaning outputs (color) */
387 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
388 if (outputsWritten
& (1 << attr
)) {
390 case FRAG_RESULT_DEPTH
:
395 assert(attr
== FRAG_RESULT_COLOR
||
396 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
397 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
398 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
399 outputMapping
[attr
] = fs_num_outputs
;
409 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
415 st_translate_mesa_program(st
->ctx
,
416 TGSI_PROCESSOR_FRAGMENT
,
422 stfp
->input_semantic_name
,
423 stfp
->input_semantic_index
,
428 fs_output_semantic_name
,
429 fs_output_semantic_index
, FALSE
);
431 stfp
->state
.tokens
= ureg_get_tokens( ureg
, NULL
);
432 ureg_destroy( ureg
);
433 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->state
);
435 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
436 _mesa_print_program(&stfp
->Base
.Base
);
440 if (ST_DEBUG
& DEBUG_TGSI
) {
441 tgsi_dump( stfp
->state
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
448 * Debug- print current shader text
451 st_print_shaders(GLcontext
*ctx
)
453 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
456 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
457 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
458 printf("%s\n", shProg
->Shaders
[i
]->Source
);