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"
52 /* Clean out any old compilations:
55 st_vp_release_varients( struct st_context
*st
,
56 struct st_vertex_program
*stvp
)
58 struct st_vp_varient
*vpv
;
60 for (vpv
= stvp
->varients
; vpv
; ) {
61 struct st_vp_varient
*next
= vpv
->next
;
63 if (vpv
->driver_shader
)
64 cso_delete_vertex_shader(st
->cso_context
, vpv
->driver_shader
);
67 draw_delete_vertex_shader( st
->draw
, vpv
->draw_shader
);
69 if (vpv
->state
.tokens
)
70 st_free_tokens(vpv
->state
.tokens
);
77 stvp
->varients
= NULL
;
84 * Translate a Mesa vertex shader into a TGSI shader.
85 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
86 * to TGSI output slots
87 * \param tokensOut destination for TGSI tokens
88 * \return pointer to cached pipe_shader object.
91 st_prepare_vertex_program(struct st_context
*st
,
92 struct st_vertex_program
*stvp
)
97 stvp
->num_outputs
= 0;
99 if (stvp
->Base
.IsPositionInvariant
)
100 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
103 * Determine number of inputs, the mappings between VERT_ATTRIB_x
104 * and TGSI generic input indexes, plus input attrib semantic info.
106 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
107 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
108 stvp
->input_to_index
[attr
] = stvp
->num_inputs
;
109 stvp
->index_to_input
[stvp
->num_inputs
] = attr
;
114 /* Compute mapping of vertex program outputs to slots.
116 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
117 if ((stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) == 0) {
118 stvp
->result_to_output
[attr
] = ~0;
121 unsigned slot
= stvp
->num_outputs
++;
123 stvp
->result_to_output
[attr
] = slot
;
126 case VERT_RESULT_HPOS
:
127 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
128 stvp
->output_semantic_index
[slot
] = 0;
130 case VERT_RESULT_COL0
:
131 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
132 stvp
->output_semantic_index
[slot
] = 0;
134 case VERT_RESULT_COL1
:
135 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
136 stvp
->output_semantic_index
[slot
] = 1;
138 case VERT_RESULT_BFC0
:
139 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
140 stvp
->output_semantic_index
[slot
] = 0;
142 case VERT_RESULT_BFC1
:
143 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
144 stvp
->output_semantic_index
[slot
] = 1;
146 case VERT_RESULT_FOGC
:
147 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
148 stvp
->output_semantic_index
[slot
] = 0;
150 case VERT_RESULT_PSIZ
:
151 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
152 stvp
->output_semantic_index
[slot
] = 0;
154 case VERT_RESULT_EDGE
:
158 case VERT_RESULT_TEX0
:
159 case VERT_RESULT_TEX1
:
160 case VERT_RESULT_TEX2
:
161 case VERT_RESULT_TEX3
:
162 case VERT_RESULT_TEX4
:
163 case VERT_RESULT_TEX5
:
164 case VERT_RESULT_TEX6
:
165 case VERT_RESULT_TEX7
:
166 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
167 stvp
->output_semantic_index
[slot
] = attr
- VERT_RESULT_TEX0
;
170 case VERT_RESULT_VAR0
:
172 assert(attr
< VERT_RESULT_MAX
);
173 stvp
->output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
174 stvp
->output_semantic_index
[slot
] = (FRAG_ATTRIB_VAR0
-
185 struct st_vp_varient
*
186 st_translate_vertex_program(struct st_context
*st
,
187 struct st_vertex_program
*stvp
,
188 const struct st_vp_varient_key
*key
)
190 struct st_vp_varient
*vpv
= CALLOC_STRUCT(st_vp_varient
);
191 struct pipe_context
*pipe
= st
->pipe
;
194 st_translate_mesa_program(st
->ctx
,
195 TGSI_PROCESSOR_VERTEX
,
199 stvp
->input_to_index
,
200 NULL
, /* input semantic name */
201 NULL
, /* input semantic index */
205 stvp
->result_to_output
,
206 stvp
->output_semantic_name
,
207 stvp
->output_semantic_index
);
209 vpv
->driver_shader
= pipe
->create_vs_state(pipe
, &vpv
->state
);
211 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
212 _mesa_print_program(&stvp
->Base
.Base
);
216 if (ST_DEBUG
& DEBUG_TGSI
) {
217 tgsi_dump( vpv
->state
.tokens
, 0 );
227 * Translate a Mesa fragment shader into a TGSI shader.
228 * \param inputMapping to map fragment program input registers to TGSI
230 * \return pointer to cached pipe_shader object.
233 st_translate_fragment_program(struct st_context
*st
,
234 struct st_fragment_program
*stfp
,
235 const GLuint inputMapping
[])
237 struct pipe_context
*pipe
= st
->pipe
;
238 GLuint outputMapping
[FRAG_RESULT_MAX
];
239 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
240 GLuint interpMode
[16]; /* XXX size? */
242 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
245 uint fs_num_inputs
= 0;
247 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
248 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
249 uint fs_num_outputs
= 0;
251 /* which vertex output goes to the first fragment input: */
252 if (inputsRead
& FRAG_BIT_WPOS
)
258 * Convert Mesa program inputs to TGSI input register semantics.
260 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
261 if (inputsRead
& (1 << attr
)) {
262 const GLuint slot
= fs_num_inputs
;
264 defaultInputMapping
[attr
] = slot
;
266 stfp
->input_map
[slot
] = vslot
++;
271 case FRAG_ATTRIB_WPOS
:
272 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
273 stfp
->input_semantic_index
[slot
] = 0;
274 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
276 case FRAG_ATTRIB_COL0
:
277 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
278 stfp
->input_semantic_index
[slot
] = 0;
279 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
281 case FRAG_ATTRIB_COL1
:
282 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
283 stfp
->input_semantic_index
[slot
] = 1;
284 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
286 case FRAG_ATTRIB_FOGC
:
287 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
288 stfp
->input_semantic_index
[slot
] = 0;
289 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
291 case FRAG_ATTRIB_FACE
:
292 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FACE
;
293 stfp
->input_semantic_index
[slot
] = 0;
294 interpMode
[slot
] = TGSI_INTERPOLATE_CONSTANT
;
297 /* In most cases, there is nothing special about these
298 * inputs, so adopt a convention to use the generic
299 * semantic name and the mesa FRAG_ATTRIB_ number as the
302 * All that is required is that the vertex shader labels
303 * its own outputs similarly, and that the vertex shader
304 * generates at least every output required by the
305 * fragment shader plus fixed-function hardware (such as
308 * There is no requirement that semantic indexes start at
309 * zero or be restricted to a particular range -- nobody
310 * should be building tables based on semantic index.
312 case FRAG_ATTRIB_TEX0
:
313 case FRAG_ATTRIB_TEX1
:
314 case FRAG_ATTRIB_TEX2
:
315 case FRAG_ATTRIB_TEX3
:
316 case FRAG_ATTRIB_TEX4
:
317 case FRAG_ATTRIB_TEX5
:
318 case FRAG_ATTRIB_TEX6
:
319 case FRAG_ATTRIB_TEX7
:
320 case FRAG_ATTRIB_PNTC
:
321 case FRAG_ATTRIB_VAR0
:
323 /* Actually, let's try and zero-base this just for
324 * readability of the generated TGSI.
326 assert(attr
>= FRAG_ATTRIB_TEX0
);
327 stfp
->input_semantic_index
[slot
] = (attr
- FRAG_ATTRIB_TEX0
);
328 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
329 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
336 * Semantics and mapping for outputs
340 GLbitfield64 outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
342 /* if z is written, emit that first */
343 if (outputsWritten
& (1 << FRAG_RESULT_DEPTH
)) {
344 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
345 fs_output_semantic_index
[fs_num_outputs
] = 0;
346 outputMapping
[FRAG_RESULT_DEPTH
] = fs_num_outputs
;
348 outputsWritten
&= ~(1 << FRAG_RESULT_DEPTH
);
351 /* handle remaning outputs (color) */
352 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
353 if (outputsWritten
& (1 << attr
)) {
355 case FRAG_RESULT_DEPTH
:
360 assert(attr
== FRAG_RESULT_COLOR
||
361 (FRAG_RESULT_DATA0
<= attr
&& attr
< FRAG_RESULT_MAX
));
362 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
363 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
364 outputMapping
[attr
] = fs_num_outputs
;
375 inputMapping
= defaultInputMapping
;
378 st_translate_mesa_program(st
->ctx
,
379 TGSI_PROCESSOR_FRAGMENT
,
384 stfp
->input_semantic_name
,
385 stfp
->input_semantic_index
,
390 fs_output_semantic_name
,
391 fs_output_semantic_index
);
393 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &stfp
->state
);
395 if ((ST_DEBUG
& DEBUG_TGSI
) && (ST_DEBUG
& DEBUG_MESA
)) {
396 _mesa_print_program(&stfp
->Base
.Base
);
400 if (ST_DEBUG
& DEBUG_TGSI
) {
401 tgsi_dump( stfp
->state
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );
408 * Debug- print current shader text
411 st_print_shaders(GLcontext
*ctx
)
413 struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
416 for (i
= 0; i
< shProg
->NumShaders
; i
++) {
417 printf("GLSL shader %u of %u:\n", i
, shProg
->NumShaders
);
418 printf("%s\n", shProg
->Shaders
[i
]->Source
);