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"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "draw/draw_context.h"
42 #include "tgsi/util/tgsi_dump.h"
44 #include "st_context.h"
46 #include "st_program.h"
47 #include "st_mesa_to_tgsi.h"
48 #include "cso_cache/cso_context.h"
54 /** XXX we should use the version of this from p_util.h but including
55 * that header causes symbol collisions.
58 mem_dup(const void *src
, uint size
)
60 void *dup
= MALLOC(size
);
62 memcpy(dup
, src
, size
);
69 * Translate a Mesa vertex shader into a TGSI shader.
70 * \param outputMapping to map vertex program output registers to TGSI
72 * \param tokensOut destination for TGSI tokens
73 * \return pointer to cached pipe_shader object.
76 st_translate_vertex_program(struct st_context
*st
,
77 struct st_vertex_program
*stvp
,
78 const GLuint outputMapping
[])
80 struct pipe_context
*pipe
= st
->pipe
;
81 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
82 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
83 struct pipe_shader_state vs
;
85 GLuint num_generic
= 0;
88 ubyte vs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
89 ubyte vs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
90 uint vs_num_inputs
= 0;
92 ubyte vs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
93 ubyte vs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
94 uint vs_num_outputs
= 0;
96 memset(&vs
, 0, sizeof(vs
));
99 * Determine number of inputs, the mappings between VERT_ATTRIB_x
100 * and TGSI generic input indexes, plus input attrib semantic info.
102 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
103 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
104 const GLuint slot
= vs_num_inputs
;
108 stvp
->input_to_index
[attr
] = slot
;
109 stvp
->index_to_input
[slot
] = attr
;
112 case VERT_ATTRIB_POS
:
113 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
114 vs_input_semantic_index
[slot
] = 0;
116 case VERT_ATTRIB_WEIGHT
:
118 case VERT_ATTRIB_NORMAL
:
119 /* just label as a generic */
120 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
121 vs_input_semantic_index
[slot
] = 0;
123 case VERT_ATTRIB_COLOR0
:
124 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
125 vs_input_semantic_index
[slot
] = 0;
127 case VERT_ATTRIB_COLOR1
:
128 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
129 vs_input_semantic_index
[slot
] = 1;
131 case VERT_ATTRIB_FOG
:
132 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
133 vs_input_semantic_index
[slot
] = 0;
135 case VERT_ATTRIB_TEX0
:
136 case VERT_ATTRIB_TEX1
:
137 case VERT_ATTRIB_TEX2
:
138 case VERT_ATTRIB_TEX3
:
139 case VERT_ATTRIB_TEX4
:
140 case VERT_ATTRIB_TEX5
:
141 case VERT_ATTRIB_TEX6
:
142 case VERT_ATTRIB_TEX7
:
143 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
144 vs_input_semantic_index
[slot
] = num_generic
++;
146 case VERT_ATTRIB_GENERIC0
:
147 case VERT_ATTRIB_GENERIC1
:
148 case VERT_ATTRIB_GENERIC2
:
149 case VERT_ATTRIB_GENERIC3
:
150 case VERT_ATTRIB_GENERIC4
:
151 case VERT_ATTRIB_GENERIC5
:
152 case VERT_ATTRIB_GENERIC6
:
153 case VERT_ATTRIB_GENERIC7
:
154 assert(attr
< VERT_ATTRIB_MAX
);
155 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
156 vs_input_semantic_index
[slot
] = num_generic
++;
164 /* initialize output semantics to defaults */
165 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
166 vs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
167 vs_output_semantic_index
[i
] = 0;
172 * Determine number of outputs, the (default) output register
173 * mapping and the semantic information for each output.
175 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
176 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
180 slot
= outputMapping
[attr
];
184 slot
= vs_num_outputs
;
186 defaultOutputMapping
[attr
] = slot
;
190 printf("Output %u -> slot %u\n", attr, slot);
194 case VERT_RESULT_HPOS
:
196 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
197 vs_output_semantic_index
[slot
] = 0;
199 case VERT_RESULT_COL0
:
200 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
201 vs_output_semantic_index
[slot
] = 0;
203 case VERT_RESULT_COL1
:
204 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
205 vs_output_semantic_index
[slot
] = 1;
207 case VERT_RESULT_BFC0
:
208 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
209 vs_output_semantic_index
[slot
] = 0;
211 case VERT_RESULT_BFC1
:
212 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
213 vs_output_semantic_index
[slot
] = 1;
215 case VERT_RESULT_FOGC
:
216 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
217 vs_output_semantic_index
[slot
] = 0;
219 case VERT_RESULT_PSIZ
:
220 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
221 vs_output_semantic_index
[slot
] = 0;
223 case VERT_RESULT_EDGE
:
226 case VERT_RESULT_TEX0
:
227 case VERT_RESULT_TEX1
:
228 case VERT_RESULT_TEX2
:
229 case VERT_RESULT_TEX3
:
230 case VERT_RESULT_TEX4
:
231 case VERT_RESULT_TEX5
:
232 case VERT_RESULT_TEX6
:
233 case VERT_RESULT_TEX7
:
234 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
235 vs_output_semantic_index
[slot
] = num_generic
++;
237 case VERT_RESULT_VAR0
:
240 assert(attr
- VERT_RESULT_VAR0
< MAX_VARYING
);
241 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
242 vs_output_semantic_index
[slot
] = num_generic
++;
247 assert(vs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
251 /* find max output slot referenced to compute vs_num_outputs */
253 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
254 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
255 maxSlot
= outputMapping
[attr
];
257 vs_num_outputs
= maxSlot
+ 1;
260 outputMapping
= defaultOutputMapping
;
263 /* free old shader state, if any */
264 if (stvp
->state
.tokens
) {
265 FREE((void *) stvp
->state
.tokens
);
266 stvp
->state
.tokens
= NULL
;
268 if (stvp
->driver_shader
) {
269 pipe
->delete_vs_state(pipe
, stvp
->driver_shader
);
270 stvp
->driver_shader
= NULL
;
273 /* XXX: fix static allocation of tokens:
275 num_tokens
= tgsi_translate_mesa_program( TGSI_PROCESSOR_VERTEX
,
279 stvp
->input_to_index
,
280 vs_input_semantic_name
,
281 vs_input_semantic_index
,
286 vs_output_semantic_name
,
287 vs_output_semantic_index
,
288 /* tokenized result */
289 tokens
, ST_MAX_SHADER_TOKENS
);
291 vs
.tokens
= (struct tgsi_token
*)
292 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
294 stvp
->num_inputs
= vs_num_inputs
;
295 stvp
->state
= vs
; /* struct copy */
296 stvp
->driver_shader
= pipe
->create_vs_state(pipe
, &vs
);
299 _mesa_print_program(&stvp
->Base
.Base
);
302 tgsi_dump( vs
.tokens
, 0 );
308 * Translate a Mesa fragment shader into a TGSI shader.
309 * \param inputMapping to map fragment program input registers to TGSI
311 * \param tokensOut destination for TGSI tokens
312 * \return pointer to cached pipe_shader object.
314 const struct cso_fragment_shader
*
315 st_translate_fragment_program(struct st_context
*st
,
316 struct st_fragment_program
*stfp
,
317 const GLuint inputMapping
[])
319 struct pipe_context
*pipe
= st
->pipe
;
320 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
321 GLuint outputMapping
[FRAG_RESULT_MAX
];
322 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
323 struct pipe_shader_state fs
;
324 const struct cso_fragment_shader
*cso
;
325 GLuint interpMode
[16]; /* XXX size? */
327 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
329 GLuint num_generic
= 0;
332 ubyte fs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
333 ubyte fs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
334 uint fs_num_inputs
= 0;
336 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
337 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
338 uint fs_num_outputs
= 0;
340 memset(&fs
, 0, sizeof(fs
));
342 /* which vertex output goes to the first fragment input: */
343 if (inputsRead
& FRAG_BIT_WPOS
)
349 * Convert Mesa program inputs to TGSI input register semantics.
351 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
352 if (inputsRead
& (1 << attr
)) {
353 const GLuint slot
= fs_num_inputs
;
355 defaultInputMapping
[attr
] = slot
;
357 stfp
->input_map
[slot
] = vslot
++;
362 case FRAG_ATTRIB_WPOS
:
363 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
364 fs_input_semantic_index
[slot
] = 0;
365 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
367 case FRAG_ATTRIB_COL0
:
368 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
369 fs_input_semantic_index
[slot
] = 0;
370 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
372 case FRAG_ATTRIB_COL1
:
373 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
374 fs_input_semantic_index
[slot
] = 1;
375 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
377 case FRAG_ATTRIB_FOGC
:
378 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
379 fs_input_semantic_index
[slot
] = 0;
380 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
382 case FRAG_ATTRIB_TEX0
:
383 case FRAG_ATTRIB_TEX1
:
384 case FRAG_ATTRIB_TEX2
:
385 case FRAG_ATTRIB_TEX3
:
386 case FRAG_ATTRIB_TEX4
:
387 case FRAG_ATTRIB_TEX5
:
388 case FRAG_ATTRIB_TEX6
:
389 case FRAG_ATTRIB_TEX7
:
390 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
391 fs_input_semantic_index
[slot
] = num_generic
++;
392 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
394 case FRAG_ATTRIB_VAR0
:
397 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
398 fs_input_semantic_index
[slot
] = num_generic
++;
399 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
405 * Semantics and mapping for outputs
409 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
411 /* if z is written, emit that first */
412 if (outputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
413 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
414 fs_output_semantic_index
[fs_num_outputs
] = 0;
415 outputMapping
[FRAG_RESULT_DEPR
] = fs_num_outputs
;
417 outputsWritten
&= ~(1 << FRAG_RESULT_DEPR
);
420 /* handle remaning outputs (color) */
421 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
422 if (outputsWritten
& (1 << attr
)) {
424 case FRAG_RESULT_DEPR
:
428 case FRAG_RESULT_COLR
:
429 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
430 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
431 outputMapping
[attr
] = fs_num_outputs
;
443 inputMapping
= defaultInputMapping
;
445 /* XXX: fix static allocation of tokens:
447 num_tokens
= tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT
,
452 fs_input_semantic_name
,
453 fs_input_semantic_index
,
458 fs_output_semantic_name
,
459 fs_output_semantic_index
,
460 /* tokenized result */
461 tokens
, ST_MAX_SHADER_TOKENS
);
463 fs
.tokens
= (struct tgsi_token
*)
464 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
466 stfp
->state
= fs
; /* struct copy */
467 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &fs
);
470 _mesa_print_program(&stfp
->Base
.Base
);
473 tgsi_dump( fs
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );