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/util/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"
55 /** XXX we should use the version of this from p_util.h but including
56 * that header causes symbol collisions.
59 mem_dup(const void *src
, uint size
)
61 void *dup
= MALLOC(size
);
63 memcpy(dup
, src
, size
);
70 * Translate a Mesa vertex shader into a TGSI shader.
71 * \param outputMapping to map vertex program output registers to TGSI
73 * \param tokensOut destination for TGSI tokens
74 * \return pointer to cached pipe_shader object.
77 st_translate_vertex_program(struct st_context
*st
,
78 struct st_vertex_program
*stvp
,
79 const GLuint outputMapping
[])
81 struct pipe_context
*pipe
= st
->pipe
;
82 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
83 GLuint defaultOutputMapping
[VERT_RESULT_MAX
];
84 struct pipe_shader_state vs
;
86 GLuint num_generic
= 0;
89 ubyte vs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
90 ubyte vs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
91 uint vs_num_inputs
= 0;
93 ubyte vs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
94 ubyte vs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
95 uint vs_num_outputs
= 0;
97 memset(&vs
, 0, sizeof(vs
));
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 const GLuint slot
= vs_num_inputs
;
112 stvp
->input_to_index
[attr
] = slot
;
113 stvp
->index_to_input
[slot
] = attr
;
116 case VERT_ATTRIB_POS
:
117 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
118 vs_input_semantic_index
[slot
] = 0;
120 case VERT_ATTRIB_WEIGHT
:
122 case VERT_ATTRIB_NORMAL
:
123 /* just label as a generic */
124 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
125 vs_input_semantic_index
[slot
] = 0;
127 case VERT_ATTRIB_COLOR0
:
128 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
129 vs_input_semantic_index
[slot
] = 0;
131 case VERT_ATTRIB_COLOR1
:
132 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
133 vs_input_semantic_index
[slot
] = 1;
135 case VERT_ATTRIB_FOG
:
136 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
137 vs_input_semantic_index
[slot
] = 0;
139 case VERT_ATTRIB_TEX0
:
140 case VERT_ATTRIB_TEX1
:
141 case VERT_ATTRIB_TEX2
:
142 case VERT_ATTRIB_TEX3
:
143 case VERT_ATTRIB_TEX4
:
144 case VERT_ATTRIB_TEX5
:
145 case VERT_ATTRIB_TEX6
:
146 case VERT_ATTRIB_TEX7
:
147 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
148 vs_input_semantic_index
[slot
] = num_generic
++;
150 case VERT_ATTRIB_GENERIC0
:
151 case VERT_ATTRIB_GENERIC1
:
152 case VERT_ATTRIB_GENERIC2
:
153 case VERT_ATTRIB_GENERIC3
:
154 case VERT_ATTRIB_GENERIC4
:
155 case VERT_ATTRIB_GENERIC5
:
156 case VERT_ATTRIB_GENERIC6
:
157 case VERT_ATTRIB_GENERIC7
:
158 assert(attr
< VERT_ATTRIB_MAX
);
159 vs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
160 vs_input_semantic_index
[slot
] = num_generic
++;
168 /* initialize output semantics to defaults */
169 for (i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
170 vs_output_semantic_name
[i
] = TGSI_SEMANTIC_GENERIC
;
171 vs_output_semantic_index
[i
] = 0;
176 * Determine number of outputs, the (default) output register
177 * mapping and the semantic information for each output.
179 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
180 if (stvp
->Base
.Base
.OutputsWritten
& (1 << attr
)) {
184 slot
= outputMapping
[attr
];
188 slot
= vs_num_outputs
;
190 defaultOutputMapping
[attr
] = slot
;
194 printf("Output %u -> slot %u\n", attr, slot);
198 case VERT_RESULT_HPOS
:
200 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
201 vs_output_semantic_index
[slot
] = 0;
203 case VERT_RESULT_COL0
:
204 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
205 vs_output_semantic_index
[slot
] = 0;
207 case VERT_RESULT_COL1
:
208 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
209 vs_output_semantic_index
[slot
] = 1;
211 case VERT_RESULT_BFC0
:
212 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
213 vs_output_semantic_index
[slot
] = 0;
215 case VERT_RESULT_BFC1
:
216 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_BCOLOR
;
217 vs_output_semantic_index
[slot
] = 1;
219 case VERT_RESULT_FOGC
:
220 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
221 vs_output_semantic_index
[slot
] = 0;
223 case VERT_RESULT_PSIZ
:
224 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_PSIZE
;
225 vs_output_semantic_index
[slot
] = 0;
227 case VERT_RESULT_EDGE
:
230 case VERT_RESULT_TEX0
:
231 case VERT_RESULT_TEX1
:
232 case VERT_RESULT_TEX2
:
233 case VERT_RESULT_TEX3
:
234 case VERT_RESULT_TEX4
:
235 case VERT_RESULT_TEX5
:
236 case VERT_RESULT_TEX6
:
237 case VERT_RESULT_TEX7
:
238 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
239 vs_output_semantic_index
[slot
] = num_generic
++;
241 case VERT_RESULT_VAR0
:
244 assert(attr
- VERT_RESULT_VAR0
< MAX_VARYING
);
245 vs_output_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
246 vs_output_semantic_index
[slot
] = num_generic
++;
251 assert(vs_output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
);
255 /* find max output slot referenced to compute vs_num_outputs */
257 for (attr
= 0; attr
< VERT_RESULT_MAX
; attr
++) {
258 if (outputMapping
[attr
] != ~0 && outputMapping
[attr
] > maxSlot
)
259 maxSlot
= outputMapping
[attr
];
261 vs_num_outputs
= maxSlot
+ 1;
264 outputMapping
= defaultOutputMapping
;
267 /* free old shader state, if any */
268 if (stvp
->state
.tokens
) {
269 FREE((void *) stvp
->state
.tokens
);
270 stvp
->state
.tokens
= NULL
;
272 if (stvp
->driver_shader
) {
273 pipe
->delete_vs_state(pipe
, stvp
->driver_shader
);
274 stvp
->driver_shader
= NULL
;
277 /* XXX: fix static allocation of tokens:
279 num_tokens
= tgsi_translate_mesa_program( TGSI_PROCESSOR_VERTEX
,
283 stvp
->input_to_index
,
284 vs_input_semantic_name
,
285 vs_input_semantic_index
,
290 vs_output_semantic_name
,
291 vs_output_semantic_index
,
292 /* tokenized result */
293 tokens
, ST_MAX_SHADER_TOKENS
);
295 vs
.tokens
= (struct tgsi_token
*)
296 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
298 stvp
->num_inputs
= vs_num_inputs
;
299 stvp
->state
= vs
; /* struct copy */
300 stvp
->driver_shader
= pipe
->create_vs_state(pipe
, &vs
);
303 _mesa_print_program(&stvp
->Base
.Base
);
306 tgsi_dump( vs
.tokens
, 0 );
312 * Translate a Mesa fragment shader into a TGSI shader.
313 * \param inputMapping to map fragment program input registers to TGSI
315 * \param tokensOut destination for TGSI tokens
316 * \return pointer to cached pipe_shader object.
318 const struct cso_fragment_shader
*
319 st_translate_fragment_program(struct st_context
*st
,
320 struct st_fragment_program
*stfp
,
321 const GLuint inputMapping
[])
323 struct pipe_context
*pipe
= st
->pipe
;
324 struct tgsi_token tokens
[ST_MAX_SHADER_TOKENS
];
325 GLuint outputMapping
[FRAG_RESULT_MAX
];
326 GLuint defaultInputMapping
[FRAG_ATTRIB_MAX
];
327 struct pipe_shader_state fs
;
328 const struct cso_fragment_shader
*cso
;
329 GLuint interpMode
[16]; /* XXX size? */
331 const GLbitfield inputsRead
= stfp
->Base
.Base
.InputsRead
;
333 GLuint num_generic
= 0;
336 ubyte fs_input_semantic_name
[PIPE_MAX_SHADER_INPUTS
];
337 ubyte fs_input_semantic_index
[PIPE_MAX_SHADER_INPUTS
];
338 uint fs_num_inputs
= 0;
340 ubyte fs_output_semantic_name
[PIPE_MAX_SHADER_OUTPUTS
];
341 ubyte fs_output_semantic_index
[PIPE_MAX_SHADER_OUTPUTS
];
342 uint fs_num_outputs
= 0;
344 memset(&fs
, 0, sizeof(fs
));
346 /* which vertex output goes to the first fragment input: */
347 if (inputsRead
& FRAG_BIT_WPOS
)
353 * Convert Mesa program inputs to TGSI input register semantics.
355 for (attr
= 0; attr
< FRAG_ATTRIB_MAX
; attr
++) {
356 if (inputsRead
& (1 << attr
)) {
357 const GLuint slot
= fs_num_inputs
;
359 defaultInputMapping
[attr
] = slot
;
361 stfp
->input_map
[slot
] = vslot
++;
366 case FRAG_ATTRIB_WPOS
:
367 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_POSITION
;
368 fs_input_semantic_index
[slot
] = 0;
369 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
371 case FRAG_ATTRIB_COL0
:
372 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
373 fs_input_semantic_index
[slot
] = 0;
374 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
376 case FRAG_ATTRIB_COL1
:
377 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_COLOR
;
378 fs_input_semantic_index
[slot
] = 1;
379 interpMode
[slot
] = TGSI_INTERPOLATE_LINEAR
;
381 case FRAG_ATTRIB_FOGC
:
382 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_FOG
;
383 fs_input_semantic_index
[slot
] = 0;
384 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
386 case FRAG_ATTRIB_TEX0
:
387 case FRAG_ATTRIB_TEX1
:
388 case FRAG_ATTRIB_TEX2
:
389 case FRAG_ATTRIB_TEX3
:
390 case FRAG_ATTRIB_TEX4
:
391 case FRAG_ATTRIB_TEX5
:
392 case FRAG_ATTRIB_TEX6
:
393 case FRAG_ATTRIB_TEX7
:
394 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
395 fs_input_semantic_index
[slot
] = num_generic
++;
396 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
398 case FRAG_ATTRIB_VAR0
:
401 fs_input_semantic_name
[slot
] = TGSI_SEMANTIC_GENERIC
;
402 fs_input_semantic_index
[slot
] = num_generic
++;
403 interpMode
[slot
] = TGSI_INTERPOLATE_PERSPECTIVE
;
409 * Semantics and mapping for outputs
413 GLbitfield outputsWritten
= stfp
->Base
.Base
.OutputsWritten
;
415 /* if z is written, emit that first */
416 if (outputsWritten
& (1 << FRAG_RESULT_DEPR
)) {
417 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_POSITION
;
418 fs_output_semantic_index
[fs_num_outputs
] = 0;
419 outputMapping
[FRAG_RESULT_DEPR
] = fs_num_outputs
;
421 outputsWritten
&= ~(1 << FRAG_RESULT_DEPR
);
424 /* handle remaning outputs (color) */
425 for (attr
= 0; attr
< FRAG_RESULT_MAX
; attr
++) {
426 if (outputsWritten
& (1 << attr
)) {
428 case FRAG_RESULT_DEPR
:
432 case FRAG_RESULT_COLR
:
433 fs_output_semantic_name
[fs_num_outputs
] = TGSI_SEMANTIC_COLOR
;
434 fs_output_semantic_index
[fs_num_outputs
] = numColors
;
435 outputMapping
[attr
] = fs_num_outputs
;
447 inputMapping
= defaultInputMapping
;
449 /* XXX: fix static allocation of tokens:
451 num_tokens
= tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT
,
456 fs_input_semantic_name
,
457 fs_input_semantic_index
,
462 fs_output_semantic_name
,
463 fs_output_semantic_index
,
464 /* tokenized result */
465 tokens
, ST_MAX_SHADER_TOKENS
);
467 fs
.tokens
= (struct tgsi_token
*)
468 mem_dup(tokens
, num_tokens
* sizeof(tokens
[0]));
470 stfp
->state
= fs
; /* struct copy */
471 stfp
->driver_shader
= pipe
->create_fs_state(pipe
, &fs
);
474 _mesa_print_program(&stfp
->Base
.Base
);
477 tgsi_dump( fs
.tokens
, 0/*TGSI_DUMP_VERBOSE*/ );