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"
45 #include "st_context.h"
47 #include "st_program.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "cso_cache/cso_context.h"
52 #define ST_MAX_SHADER_TOKENS 4096
58 /** XXX we should use the version of this from u_memory.h but including
59 * that header causes symbol collisions.
62 mem_dup(const void *src
, uint size
)
64 void *dup
= MALLOC(size
);
66 memcpy(dup
, src
, size
);
73 * Translate a Mesa vertex shader into a TGSI shader.
74 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
75 * to TGSI output slots
76 * \param tokensOut destination for TGSI tokens
77 * \return pointer to cached pipe_shader object.
80 st_translate_vertex_program(struct st_context
*st
,
81 struct st_vertex_program
*stvp
,
82 const GLuint outputMapping
[],
83 const ubyte
*outputSemanticName
,
84 const ubyte
*outputSemanticIndex
)
86 struct pipe_context
*pipe
= st
->pipe
;
87 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
88 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
89 struct pipe_shader_state vs
;
91 GLuint num_generic
= 0;
94 ubyte vs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
95 ubyte vs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
96 uint vs_num_inputs
= 0;
98 ubyte vs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
99 ubyte vs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
100 uint vs_num_outputs
= 0;
102 memset(&vs
, 0, sizeof(vs
));
104 if (stvp
->Base
.IsPositionInvariant
)
105 _mesa_insert_mvp_code(st
->ctx
, &stvp
->Base
);
108 * Determine number of inputs, the mappings between VERT_ATTRIB_x
109 * and TGSI generic input indexes, plus input attrib semantic info.
111 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
112 if (stvp
->Base
.Base
.InputsRead
& (1 << attr
)) {
113 const GLuint slot
= vs_num_inputs
;
117 stvp
->input_to_index
[attr
] = slot
;
118 stvp
->index_to_input
[slot
] = attr
;
121 case VERT_ATTRIB_POS
:
122 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
123 vs_input_semantic_index
[slot
] = 0;
125 case VERT_ATTRIB_WEIGHT
:
127 case VERT_ATTRIB_NORMAL
:
128 /* just label as a generic */
129 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
130 vs_input_semantic_index
[slot
] = 0;
132 case VERT_ATTRIB_COLOR0
:
133 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
134 vs_input_semantic_index
[slot
] = 0;
136 case VERT_ATTRIB_COLOR1
:
137 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
138 vs_input_semantic_index
[slot
] = 1;
140 case VERT_ATTRIB_FOG
:
141 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
142 vs_input_semantic_index
[slot
] = 0;
144 case VERT_ATTRIB_POINT_SIZE
:
145 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
146 vs_input_semantic_index
[slot
] = 0;
148 case VERT_ATTRIB_TEX0
:
149 case VERT_ATTRIB_TEX1
:
150 case VERT_ATTRIB_TEX2
:
151 case VERT_ATTRIB_TEX3
:
152 case VERT_ATTRIB_TEX4
:
153 case VERT_ATTRIB_TEX5
:
154 case VERT_ATTRIB_TEX6
:
155 case VERT_ATTRIB_TEX7
:
156 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
157 vs_input_semantic_index
[slot
] = num_generic
++;
159 case VERT_ATTRIB_GENERIC0
:
160 case VERT_ATTRIB_GENERIC1
:
161 case VERT_ATTRIB_GENERIC2
:
162 case VERT_ATTRIB_GENERIC3
:
163 case VERT_ATTRIB_GENERIC4
:
164 case VERT_ATTRIB_GENERIC5
:
165 case VERT_ATTRIB_GENERIC6
:
166 case VERT_ATTRIB_GENERIC7
:
167 assert(attr
< VERT_ATTRIB_MAX
);
168 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
169 vs_input_semantic_index
[slot
] = num_generic
++;
178 if (outputMapping
&& outputSemanticName
) {
179 printf("VERT_RESULT written out_slot semantic_name semantic_index\n");
180 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
181 printf(" %-2d %c %3d %2d %2d\n",
183 ((stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) ? 'Y' : ' '),
185 outputSemanticName
[attr
],
186 outputSemanticIndex
[attr
]);
191 /* initialize output semantics to defaults */
192 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
193 vs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
194 vs_output_semantic_index
[i
] = 0;
199 * Determine number of outputs, the (default) output register
200 * mapping and the semantic information for each output.
202 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
203 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
207 * Pass in the fragment program's input's semantic info.
208 * Use the generic semantic indexes from there, instead of
213 slot
= outputMapping
[attr
];
217 slot
= vs_num_outputs
;
219 defaultOutputMapping
[attr
] = slot
;
223 case VERT_RESULT_HPOS
:
225 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
226 vs_output_semantic_index
[slot
] = 0;
228 case VERT_RESULT_COL0
:
229 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
230 vs_output_semantic_index
[slot
] = 0;
232 case VERT_RESULT_COL1
:
233 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
234 vs_output_semantic_index
[slot
] = 1;
236 case VERT_RESULT_BFC0
:
237 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
238 vs_output_semantic_index
[slot
] = 0;
240 case VERT_RESULT_BFC1
:
241 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
242 vs_output_semantic_index
[slot
] = 1;
244 case VERT_RESULT_FOGC
:
245 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
246 vs_output_semantic_index
[slot
] = 0;
248 case VERT_RESULT_PSIZ
:
249 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
250 vs_output_semantic_index
[slot
] = 0;
252 case VERT_RESULT_EDGE
:
255 case VERT_RESULT_TEX0
:
256 case VERT_RESULT_TEX1
:
257 case VERT_RESULT_TEX2
:
258 case VERT_RESULT_TEX3
:
259 case VERT_RESULT_TEX4
:
260 case VERT_RESULT_TEX5
:
261 case VERT_RESULT_TEX6
:
262 case VERT_RESULT_TEX7
:
264 case VERT_RESULT_VAR0
:
267 if (outputSemanticName
) {
268 /* use provided semantic into */
269 assert(outputSemanticName
[attr
] != TGSI_SEMANTIC_COUNT
);
270 vs_output_semantic_name
[slot
] = outputSemanticName
[attr
];
271 vs_output_semantic_index
[slot
] = outputSemanticIndex
[attr
];
274 /* use default semantic info */
275 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
276 vs_output_semantic_index
[slot
] = num_generic
++;
282 assert(vs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
286 /* find max output slot referenced to compute vs_num_outputs */
288 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
289 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
290 maxSlot
= outputMapping
[attr
];
292 vs_num_outputs
= maxSlot
+ 1;
295 outputMapping
= defaultOutputMapping
;
298 /* free old shader state, if any */
299 if (stvp
->state
.tokens
) {
300 FREE((void *) stvp
->state
.tokens
);
301 stvp
->state
.tokens
= NULL
;
303 if (stvp
->driver_shader
) {
304 cso_delete_vertex_shader(st
->cso_context
, stvp
->driver_shader
);
305 stvp
->driver_shader
= NULL
;
308 /* XXX: fix static allocation of tokens:
310 num_tokens
= tgsi_translate_mesa_program( TGSI_PROCESSOR_VERTEX
,
314 stvp
->input_to_index
,
315 vs_input_semantic_name
,
316 vs_input_semantic_index
,
321 vs_output_semantic_name
,
322 vs_output_semantic_index
,
323 /* tokenized result */
324 tokens
, ST_MAX_SHADER_TOKENS
);
326 assert(num_tokens
< ST_MAX_SHADER_TOKENS
);
328 vs
.tokens
= (struct tgsi_token
*)
329 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
331 stvp
->num_inputs
= vs_num_inputs
;
332 stvp
->state
= vs
; /* struct copy */
333 stvp
->driver_shader
= pipe
->create_vs_state(pipe
, &vs
);
336 _mesa_print_program(&stvp
->Base
.Base
);
339 tgsi_dump( vs
.tokens
, 0 );
345 * Translate a Mesa fragment shader into a TGSI shader.
346 * \param inputMapping to map fragment program input registers to TGSI
348 * \param tokensOut destination for TGSI tokens
349 * \return pointer to cached pipe_shader object.
352 st_translate_fragment_program(struct st_context
*st
,
353 struct st_fragment_program
*stfp
,
354 const GLuint inputMapping
[])
356 struct pipe_context
*pipe
= st
->pipe
;
357 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
358 GLuint outputMapping
[FRAG_RESULT_MAX
];
359 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
360 struct pipe_shader_state fs
;
361 GLuint interpMode
[16]; /* XXX size? */
363 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
365 GLuint num_generic
= 0;
368 uint fs_num_inputs
= 0;
370 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
371 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
372 uint fs_num_outputs
= 0;
374 memset(&fs
, 0, sizeof(fs
));
376 /* which vertex output goes to the first fragment input: */
377 if (inputsRead
& FRAG_BIT_WPOS
)
383 * Convert Mesa program inputs to TGSI input register semantics.
385 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
386 if (inputsRead
& (1 << attr
)) {
387 const GLuint slot
= fs_num_inputs
;
389 defaultInputMapping
[attr
] = slot
;
391 stfp
->input_map
[slot
] = vslot
++;
396 case FRAG_ATTRIB_WPOS
:
397 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
398 stfp
->input_semantic_index
[slot
] = 0;
399 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
401 case FRAG_ATTRIB_COL0
:
402 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
403 stfp
->input_semantic_index
[slot
] = 0;
404 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
406 case FRAG_ATTRIB_COL1
:
407 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
408 stfp
->input_semantic_index
[slot
] = 1;
409 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
411 case FRAG_ATTRIB_FOGC
:
412 if (stfp
->Base
.UsesPointCoord
)
413 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
415 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
416 stfp
->input_semantic_index
[slot
] = 0;
417 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
419 case FRAG_ATTRIB_TEX0
:
420 case FRAG_ATTRIB_TEX1
:
421 case FRAG_ATTRIB_TEX2
:
422 case FRAG_ATTRIB_TEX3
:
423 case FRAG_ATTRIB_TEX4
:
424 case FRAG_ATTRIB_TEX5
:
425 case FRAG_ATTRIB_TEX6
:
426 case FRAG_ATTRIB_TEX7
:
427 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
428 stfp
->input_semantic_index
[slot
] = num_generic
++;
429 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
431 case FRAG_ATTRIB_VAR0
:
434 stfp
->input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
435 stfp
->input_semantic_index
[slot
] = num_generic
++;
436 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
442 * Semantics and mapping for outputs
446 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
448 /* if z is written, emit that first */
449 if (outputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
450 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
451 fs_output_semantic_index
[fs_num_outputs
] = 0;
452 outputMapping
[FRAG_RESULT_DEPR
] = fs_num_outputs
;
454 outputsWritten
&= ~(1 << FRAG_RESULT_DEPR
);
457 /* handle remaning outputs (color) */
458 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
459 if (outputsWritten
& (1 << attr
)) {
461 case FRAG_RESULT_DEPR
:
465 case FRAG_RESULT_COLR
:
466 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
467 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
468 outputMapping
[attr
] = fs_num_outputs
;
480 inputMapping
= defaultInputMapping
;
482 /* XXX: fix static allocation of tokens:
484 num_tokens
= tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT
,
489 stfp
->input_semantic_name
,
490 stfp
->input_semantic_index
,
495 fs_output_semantic_name
,
496 fs_output_semantic_index
,
497 /* tokenized result */
498 tokens
, ST_MAX_SHADER_TOKENS
);
500 assert(num_tokens
< ST_MAX_SHADER_TOKENS
);
502 fs
.tokens
= (struct tgsi_token
*)
503 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
505 stfp
->state
= fs
; /* struct copy */
506 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &fs
);
509 _mesa_print_program(&stfp
->Base
.Base
);
512 tgsi_dump( fs
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );