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"
55 * Clean out any old compilations:
58 st_vp_release_varients( struct st_context
*st
,
59 struct st_vertex_program
*stvp
)
61 struct st_vp_varient
*vpv
;
63 for (vpv
= stvp
->varients
; vpv
; ) {
64 struct st_vp_varient
*next
= vpv
->next
;
66 if (vpv
->driver_shader
)
67 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
70 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
72 if (vpv
->state
.tokens
)
73 st_free_tokens(vpv
->state
.tokens
);
80 stvp
->varients
= NULL
;
87 * Translate a Mesa vertex shader into a TGSI shader.
88 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
89 * to TGSI output slots
90 * \param tokensOut destination for TGSI tokens
91 * \return pointer to cached pipe_shader object.
94 st_prepare_vertex_program(struct st_context
*st
,
95 struct st_vertex_program
*stvp
)
100 stvp
->num_outputs
= 0;
102 if (stvp
->Base
.IsPositionInvariant
)
103 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
106 * Determine number of inputs, the mappings between VERT_ATTRIB_x
107 * and TGSI generic input indexes, plus input attrib semantic info.
109 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
110 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
111 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
112 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
116 /* bit of a hack, presetup potentially unused edgeflag input */
117 stvp
->input_to_index
[VERT_ATTRIB_EDGEFLAG
] = stvp
->num_inputs
;
118 stvp
->index_to_input
[stvp
->num_inputs
] = VERT_ATTRIB_EDGEFLAG
;
120 /* Compute mapping of vertex program outputs to slots.
122 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
123 if ((stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) == 0) {
124 stvp
->result_to_output
[attr
] = ~0;
127 unsigned slot
= stvp
->num_outputs
++;
129 stvp
->result_to_output
[attr
] = slot
;
132 case VERT_RESULT_HPOS
:
133 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
134 stvp
->output_semantic_index
[slot
] = 0;
136 case VERT_RESULT_COL0
:
137 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
138 stvp
->output_semantic_index
[slot
] = 0;
140 case VERT_RESULT_COL1
:
141 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
142 stvp
->output_semantic_index
[slot
] = 1;
144 case VERT_RESULT_BFC0
:
145 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
146 stvp
->output_semantic_index
[slot
] = 0;
148 case VERT_RESULT_BFC1
:
149 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
150 stvp
->output_semantic_index
[slot
] = 1;
152 case VERT_RESULT_FOGC
:
153 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
154 stvp
->output_semantic_index
[slot
] = 0;
156 case VERT_RESULT_PSIZ
:
157 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
158 stvp
->output_semantic_index
[slot
] = 0;
160 case VERT_RESULT_EDGE
:
164 case VERT_RESULT_TEX0
:
165 case VERT_RESULT_TEX1
:
166 case VERT_RESULT_TEX2
:
167 case VERT_RESULT_TEX3
:
168 case VERT_RESULT_TEX4
:
169 case VERT_RESULT_TEX5
:
170 case VERT_RESULT_TEX6
:
171 case VERT_RESULT_TEX7
:
172 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
173 stvp
->output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
176 case VERT_RESULT_VAR0
:
178 assert(attr
< VERT_RESULT_MAX
);
179 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
180 stvp
->output_semantic_index
[slot
] = (FRAG_ATTRIB_VAR0
-
191 struct st_vp_varient
*
192 st_translate_vertex_program(struct st_context
*st
,
193 struct st_vertex_program
*stvp
,
194 const struct st_vp_varient_key
*key
)
196 struct st_vp_varient
*vpv
= CALLOC_STRUCT(st_vp_varient
);
197 struct pipe_context
*pipe
= st
->pipe
;
198 struct ureg_program
*ureg
;
199 enum pipe_error error
;
201 ureg
= ureg_create( TGSI_PROCESSOR_VERTEX
);
205 vpv
->num_inputs
= stvp
->num_inputs
;
208 st_translate_mesa_program(st
->ctx
,
209 TGSI_PROCESSOR_VERTEX
,
214 stvp
->input_to_index
,
215 NULL
, /* input semantic name */
216 NULL
, /* input semantic index */
220 stvp
->result_to_output
,
221 stvp
->output_semantic_name
,
222 stvp
->output_semantic_index
);
227 /* Edgeflags will be the last input:
229 if (key
->passthrough_edgeflags
) {
231 ureg_DECL_output( ureg
, TGSI_SEMANTIC_EDGEFLAG
, 0 ),
232 ureg_DECL_vs_input( ureg
, vpv
->num_inputs
));
236 vpv
->state
.tokens
= ureg_get_tokens( ureg
, NULL
);
237 ureg_destroy( ureg
);
239 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->state
);
241 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
242 _mesa_print_program(&stvp
->Base
.Base
);
246 if (ST_DEBUG
& DEBUG_TGSI
) {
247 tgsi_dump( vpv
->state
.tokens
, 0 );
254 ureg_destroy( ureg
);
261 * Translate a Mesa fragment shader into a TGSI shader.
262 * \param inputMapping to map fragment program input registers to TGSI
264 * \return pointer to cached pipe_shader object.
267 st_translate_fragment_program(struct st_context
*st
,
268 struct st_fragment_program
*stfp
,
269 const GLuint inputMapping
[])
271 struct pipe_context
*pipe
= st
->pipe
;
272 GLuint outputMapping
[FRAG_RESULT_MAX
];
273 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
274 GLuint interpMode
[16]; /* XXX size? */
276 enum pipe_error error
;
277 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
278 struct ureg_program
*ureg
;
281 uint fs_num_inputs
= 0;
283 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
284 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
285 uint fs_num_outputs
= 0;
287 /* which vertex output goes to the first fragment input: */
288 if (inputsRead
& FRAG_BIT_WPOS
)
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 defaultInputMapping
[attr
] = slot
;
302 stfp
->input_map
[slot
] = vslot
++;
307 case FRAG_ATTRIB_WPOS
:
308 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
309 stfp
->input_semantic_index
[slot
] = 0;
310 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
312 case FRAG_ATTRIB_COL0
:
313 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
314 stfp
->input_semantic_index
[slot
] = 0;
315 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
317 case FRAG_ATTRIB_COL1
:
318 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
319 stfp
->input_semantic_index
[slot
] = 1;
320 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
322 case FRAG_ATTRIB_FOGC
:
323 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
324 stfp
->input_semantic_index
[slot
] = 0;
325 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
327 case FRAG_ATTRIB_FACE
:
328 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
329 stfp
->input_semantic_index
[slot
] = 0;
330 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
333 /* In most cases, there is nothing special about these
334 * inputs, so adopt a convention to use the generic
335 * semantic name and the mesa FRAG_ATTRIB_ number as the
338 * All that is required is that the vertex shader labels
339 * its own outputs similarly, and that the vertex shader
340 * generates at least every output required by the
341 * fragment shader plus fixed-function hardware (such as
344 * There is no requirement that semantic indexes start at
345 * zero or be restricted to a particular range -- nobody
346 * should be building tables based on semantic index.
348 case FRAG_ATTRIB_TEX0
:
349 case FRAG_ATTRIB_TEX1
:
350 case FRAG_ATTRIB_TEX2
:
351 case FRAG_ATTRIB_TEX3
:
352 case FRAG_ATTRIB_TEX4
:
353 case FRAG_ATTRIB_TEX5
:
354 case FRAG_ATTRIB_TEX6
:
355 case FRAG_ATTRIB_TEX7
:
356 case FRAG_ATTRIB_PNTC
:
357 case FRAG_ATTRIB_VAR0
:
359 /* Actually, let's try and zero-base this just for
360 * readability of the generated TGSI.
362 assert(attr
>= FRAG_ATTRIB_TEX0
);
363 stfp
->input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
364 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
365 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
372 * Semantics and mapping for outputs
376 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
378 /* if z is written, emit that first */
379 if (outputsWritten
& (1 << FRAG_RESULT_DEPTH
)) {
380 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
381 fs_output_semantic_index
[fs_num_outputs
] = 0;
382 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
384 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
387 /* handle remaning outputs (color) */
388 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
389 if (outputsWritten
& (1 << attr
)) {
391 case FRAG_RESULT_DEPTH
:
396 assert(attr
== FRAG_RESULT_COLOR
||
397 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
398 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
399 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
400 outputMapping
[attr
] = fs_num_outputs
;
411 inputMapping
= defaultInputMapping
;
413 ureg
= ureg_create( TGSI_PROCESSOR_FRAGMENT
);
419 st_translate_mesa_program(st
->ctx
,
420 TGSI_PROCESSOR_FRAGMENT
,
426 stfp
->input_semantic_name
,
427 stfp
->input_semantic_index
,
432 fs_output_semantic_name
,
433 fs_output_semantic_index
);
435 stfp
->state
.tokens
= ureg_get_tokens( ureg
, NULL
);
436 ureg_destroy( ureg
);
437 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->state
);
439 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
440 _mesa_print_program(&stfp
->Base
.Base
);
444 if (ST_DEBUG
& DEBUG_TGSI
) {
445 tgsi_dump( stfp
->state
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
452 * Debug- print current shader text
455 st_print_shaders(GLcontext
*ctx
)
457 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
460 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
461 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
462 printf("%s\n", shProg
->Shaders
[i
]->Source
);