Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / state_tracker / st_program.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Brian Paul
31 */
32
33
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "shader/prog_print.h"
37 #include "shader/programopt.h"
38
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"
44
45 #include "st_context.h"
46 #include "st_atom.h"
47 #include "st_program.h"
48 #include "st_mesa_to_tgsi.h"
49 #include "cso_cache/cso_context.h"
50
51
52 #define ST_MAX_SHADER_TOKENS (8 * 1024)
53
54
55 #define TGSI_DEBUG 0
56
57
58 /**
59 * Translate a Mesa vertex shader into a TGSI shader.
60 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
61 * to TGSI output slots
62 * \param tokensOut destination for TGSI tokens
63 * \return pointer to cached pipe_shader object.
64 */
65 void
66 st_translate_vertex_program(struct st_context *st,
67 struct st_vertex_program *stvp,
68 const GLuint outputMapping[],
69 const ubyte *outputSemanticName,
70 const ubyte *outputSemanticIndex)
71 {
72 struct pipe_context *pipe = st->pipe;
73 struct tgsi_token *tokens;
74 GLuint defaultOutputMapping[VERT_RESULT_MAX];
75 struct pipe_shader_state vs;
76 GLuint attr, i;
77 GLuint num_generic = 0;
78 GLuint num_tokens;
79
80 ubyte vs_input_semantic_name[PIPE_MAX_SHADER_INPUTS];
81 ubyte vs_input_semantic_index[PIPE_MAX_SHADER_INPUTS];
82 uint vs_num_inputs = 0;
83
84 ubyte vs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
85 ubyte vs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
86 uint vs_num_outputs = 0;
87
88 GLbitfield input_flags[MAX_PROGRAM_INPUTS];
89 GLbitfield output_flags[MAX_PROGRAM_OUTPUTS];
90
91 tokens = (struct tgsi_token *)MALLOC(ST_MAX_SHADER_TOKENS * sizeof *tokens);
92 if(!tokens) {
93 /* FIXME: propagate error to the caller */
94 assert(0);
95 return;
96 }
97
98 memset(&vs, 0, sizeof(vs));
99 memset(input_flags, 0, sizeof(input_flags));
100 memset(output_flags, 0, sizeof(output_flags));
101
102 if (stvp->Base.IsPositionInvariant)
103 _mesa_insert_mvp_code(st->ctx, &stvp->Base);
104
105 /*
106 * Determine number of inputs, the mappings between VERT_ATTRIB_x
107 * and TGSI generic input indexes, plus input attrib semantic info.
108 */
109 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
110 if (stvp->Base.Base.InputsRead & (1 << attr)) {
111 const GLuint slot = vs_num_inputs;
112
113 vs_num_inputs++;
114
115 stvp->input_to_index[attr] = slot;
116 stvp->index_to_input[slot] = attr;
117
118 switch (attr) {
119 case VERT_ATTRIB_POS:
120 vs_input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
121 vs_input_semantic_index[slot] = 0;
122 break;
123 case VERT_ATTRIB_WEIGHT:
124 /* fall-through */
125 case VERT_ATTRIB_NORMAL:
126 /* just label as a generic */
127 vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
128 vs_input_semantic_index[slot] = 0;
129 break;
130 case VERT_ATTRIB_COLOR0:
131 vs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
132 vs_input_semantic_index[slot] = 0;
133 break;
134 case VERT_ATTRIB_COLOR1:
135 vs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
136 vs_input_semantic_index[slot] = 1;
137 break;
138 case VERT_ATTRIB_FOG:
139 vs_input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
140 vs_input_semantic_index[slot] = 0;
141 break;
142 case VERT_ATTRIB_POINT_SIZE:
143 vs_input_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
144 vs_input_semantic_index[slot] = 0;
145 break;
146 case VERT_ATTRIB_TEX0:
147 case VERT_ATTRIB_TEX1:
148 case VERT_ATTRIB_TEX2:
149 case VERT_ATTRIB_TEX3:
150 case VERT_ATTRIB_TEX4:
151 case VERT_ATTRIB_TEX5:
152 case VERT_ATTRIB_TEX6:
153 case VERT_ATTRIB_TEX7:
154 vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
155 vs_input_semantic_index[slot] = num_generic++;
156 break;
157 case VERT_ATTRIB_GENERIC0:
158 case VERT_ATTRIB_GENERIC1:
159 case VERT_ATTRIB_GENERIC2:
160 case VERT_ATTRIB_GENERIC3:
161 case VERT_ATTRIB_GENERIC4:
162 case VERT_ATTRIB_GENERIC5:
163 case VERT_ATTRIB_GENERIC6:
164 case VERT_ATTRIB_GENERIC7:
165 case VERT_ATTRIB_GENERIC8:
166 case VERT_ATTRIB_GENERIC9:
167 case VERT_ATTRIB_GENERIC10:
168 case VERT_ATTRIB_GENERIC11:
169 case VERT_ATTRIB_GENERIC12:
170 case VERT_ATTRIB_GENERIC13:
171 case VERT_ATTRIB_GENERIC14:
172 case VERT_ATTRIB_GENERIC15:
173 assert(attr < VERT_ATTRIB_MAX);
174 vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
175 vs_input_semantic_index[slot] = num_generic++;
176 break;
177 default:
178 assert(0);
179 }
180
181 input_flags[slot] = stvp->Base.Base.InputFlags[attr];
182 }
183 }
184
185 #if 0
186 if (outputMapping && outputSemanticName) {
187 printf("VERT_RESULT written out_slot semantic_name semantic_index\n");
188 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
189 printf(" %-2d %c %3d %2d %2d\n",
190 attr,
191 ((stvp->Base.Base.OutputsWritten & (1 << attr)) ? 'Y' : ' '),
192 outputMapping[attr],
193 outputSemanticName[attr],
194 outputSemanticIndex[attr]);
195 }
196 }
197 #endif
198
199 /* initialize output semantics to defaults */
200 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
201 vs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
202 vs_output_semantic_index[i] = 0;
203 output_flags[i] = 0x0;
204 }
205
206 num_generic = 0;
207 /*
208 * Determine number of outputs, the (default) output register
209 * mapping and the semantic information for each output.
210 */
211 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
212 if (stvp->Base.Base.OutputsWritten & (1 << attr)) {
213 GLuint slot;
214
215 /* XXX
216 * Pass in the fragment program's input's semantic info.
217 * Use the generic semantic indexes from there, instead of
218 * guessing below.
219 */
220
221 if (outputMapping) {
222 slot = outputMapping[attr];
223 assert(slot != ~0);
224 }
225 else {
226 slot = vs_num_outputs;
227 vs_num_outputs++;
228 defaultOutputMapping[attr] = slot;
229 }
230
231 switch (attr) {
232 case VERT_RESULT_HPOS:
233 assert(slot == 0);
234 vs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
235 vs_output_semantic_index[slot] = 0;
236 break;
237 case VERT_RESULT_COL0:
238 vs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
239 vs_output_semantic_index[slot] = 0;
240 break;
241 case VERT_RESULT_COL1:
242 vs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
243 vs_output_semantic_index[slot] = 1;
244 break;
245 case VERT_RESULT_BFC0:
246 vs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
247 vs_output_semantic_index[slot] = 0;
248 break;
249 case VERT_RESULT_BFC1:
250 vs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
251 vs_output_semantic_index[slot] = 1;
252 break;
253 case VERT_RESULT_FOGC:
254 vs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
255 vs_output_semantic_index[slot] = 0;
256 break;
257 case VERT_RESULT_PSIZ:
258 vs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
259 vs_output_semantic_index[slot] = 0;
260 break;
261 case VERT_RESULT_EDGE:
262 assert(0);
263 break;
264 case VERT_RESULT_TEX0:
265 case VERT_RESULT_TEX1:
266 case VERT_RESULT_TEX2:
267 case VERT_RESULT_TEX3:
268 case VERT_RESULT_TEX4:
269 case VERT_RESULT_TEX5:
270 case VERT_RESULT_TEX6:
271 case VERT_RESULT_TEX7:
272 /* fall-through */
273 case VERT_RESULT_VAR0:
274 /* fall-through */
275 default:
276 if (outputSemanticName) {
277 /* use provided semantic into */
278 assert(outputSemanticName[attr] != TGSI_SEMANTIC_COUNT);
279 vs_output_semantic_name[slot] = outputSemanticName[attr];
280 vs_output_semantic_index[slot] = outputSemanticIndex[attr];
281 }
282 else {
283 /* use default semantic info */
284 vs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
285 vs_output_semantic_index[slot] = num_generic++;
286 }
287 }
288
289 output_flags[slot] = stvp->Base.Base.OutputFlags[attr];
290 }
291 }
292
293 if (outputMapping) {
294 /* find max output slot referenced to compute vs_num_outputs */
295 GLuint maxSlot = 0;
296 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
297 if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot)
298 maxSlot = outputMapping[attr];
299 }
300 vs_num_outputs = maxSlot + 1;
301 }
302 else {
303 outputMapping = defaultOutputMapping;
304 }
305
306 #if 0 /* debug */
307 {
308 GLuint i;
309 printf("outputMapping? %d\n", outputMapping ? 1 : 0);
310 if (outputMapping) {
311 printf("attr -> slot\n");
312 for (i = 0; i < 16; i++) {
313 printf(" %2d %3d\n", i, outputMapping[i]);
314 }
315 }
316 printf("slot sem_name sem_index\n");
317 for (i = 0; i < vs_num_outputs; i++) {
318 printf(" %2d %d %d\n",
319 i,
320 vs_output_semantic_name[i],
321 vs_output_semantic_index[i]);
322 }
323 }
324 #endif
325
326 /* free old shader state, if any */
327 if (stvp->state.tokens) {
328 _mesa_free((void *) stvp->state.tokens);
329 stvp->state.tokens = NULL;
330 }
331 if (stvp->driver_shader) {
332 cso_delete_vertex_shader(st->cso_context, stvp->driver_shader);
333 stvp->driver_shader = NULL;
334 }
335
336 /* XXX: fix static allocation of tokens:
337 */
338 num_tokens = st_translate_mesa_program(st->ctx,
339 TGSI_PROCESSOR_VERTEX,
340 &stvp->Base.Base,
341 /* inputs */
342 vs_num_inputs,
343 stvp->input_to_index,
344 vs_input_semantic_name,
345 vs_input_semantic_index,
346 NULL,
347 input_flags,
348 /* outputs */
349 vs_num_outputs,
350 outputMapping,
351 vs_output_semantic_name,
352 vs_output_semantic_index,
353 output_flags,
354 /* tokenized result */
355 tokens, ST_MAX_SHADER_TOKENS);
356
357 assert(num_tokens < ST_MAX_SHADER_TOKENS);
358
359 vs.tokens = (struct tgsi_token *)
360 _mesa_realloc(tokens,
361 ST_MAX_SHADER_TOKENS * sizeof *tokens,
362 num_tokens * sizeof *tokens);
363
364 stvp->num_inputs = vs_num_inputs;
365 stvp->state = vs; /* struct copy */
366 stvp->driver_shader = pipe->create_vs_state(pipe, &vs);
367
368 if (0)
369 _mesa_print_program(&stvp->Base.Base);
370
371 if (TGSI_DEBUG)
372 tgsi_dump( vs.tokens, 0 );
373 }
374
375
376
377 /**
378 * Translate a Mesa fragment shader into a TGSI shader.
379 * \param inputMapping to map fragment program input registers to TGSI
380 * input slots
381 * \param tokensOut destination for TGSI tokens
382 * \return pointer to cached pipe_shader object.
383 */
384 void
385 st_translate_fragment_program(struct st_context *st,
386 struct st_fragment_program *stfp,
387 const GLuint inputMapping[])
388 {
389 struct pipe_context *pipe = st->pipe;
390 struct tgsi_token *tokens;
391 GLuint outputMapping[FRAG_RESULT_MAX];
392 GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
393 struct pipe_shader_state fs;
394 GLuint interpMode[16]; /* XXX size? */
395 GLuint attr;
396 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
397 GLuint vslot = 0;
398 GLuint num_generic = 0;
399 GLuint num_tokens;
400
401 uint fs_num_inputs = 0;
402
403 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
404 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
405 uint fs_num_outputs = 0;
406
407 GLbitfield input_flags[MAX_PROGRAM_INPUTS];
408 GLbitfield output_flags[MAX_PROGRAM_OUTPUTS];
409
410 tokens = (struct tgsi_token *)MALLOC(ST_MAX_SHADER_TOKENS * sizeof *tokens);
411 if(!tokens) {
412 /* FIXME: propagate error to the caller */
413 assert(0);
414 return;
415 }
416
417 memset(&fs, 0, sizeof(fs));
418 memset(input_flags, 0, sizeof(input_flags));
419 memset(output_flags, 0, sizeof(output_flags));
420
421 /* which vertex output goes to the first fragment input: */
422 if (inputsRead & FRAG_BIT_WPOS)
423 vslot = 0;
424 else
425 vslot = 1;
426
427 /*
428 * Convert Mesa program inputs to TGSI input register semantics.
429 */
430 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
431 if (inputsRead & (1 << attr)) {
432 const GLuint slot = fs_num_inputs;
433
434 defaultInputMapping[attr] = slot;
435
436 stfp->input_map[slot] = vslot++;
437
438 fs_num_inputs++;
439
440 switch (attr) {
441 case FRAG_ATTRIB_WPOS:
442 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
443 stfp->input_semantic_index[slot] = 0;
444 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
445 break;
446 case FRAG_ATTRIB_COL0:
447 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
448 stfp->input_semantic_index[slot] = 0;
449 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
450 break;
451 case FRAG_ATTRIB_COL1:
452 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
453 stfp->input_semantic_index[slot] = 1;
454 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
455 break;
456 case FRAG_ATTRIB_FOGC:
457 if (stfp->Base.UsesPointCoord) {
458 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
459 stfp->input_semantic_index[slot] = num_generic++;
460 } else {
461 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
462 stfp->input_semantic_index[slot] = 0;
463 }
464 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
465 break;
466 case FRAG_ATTRIB_TEX0:
467 case FRAG_ATTRIB_TEX1:
468 case FRAG_ATTRIB_TEX2:
469 case FRAG_ATTRIB_TEX3:
470 case FRAG_ATTRIB_TEX4:
471 case FRAG_ATTRIB_TEX5:
472 case FRAG_ATTRIB_TEX6:
473 case FRAG_ATTRIB_TEX7:
474 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
475 stfp->input_semantic_index[slot] = num_generic++;
476 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
477 break;
478 case FRAG_ATTRIB_VAR0:
479 /* fall-through */
480 default:
481 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
482 stfp->input_semantic_index[slot] = num_generic++;
483 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
484 }
485
486 input_flags[slot] = stfp->Base.Base.InputFlags[attr];
487 }
488 }
489
490 /*
491 * Semantics and mapping for outputs
492 */
493 {
494 uint numColors = 0;
495 GLbitfield outputsWritten = stfp->Base.Base.OutputsWritten;
496
497 /* if z is written, emit that first */
498 if (outputsWritten & (1 << FRAG_RESULT_DEPTH)) {
499 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
500 fs_output_semantic_index[fs_num_outputs] = 0;
501 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
502 fs_num_outputs++;
503 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
504 }
505
506 /* handle remaning outputs (color) */
507 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
508 if (outputsWritten & (1 << attr)) {
509 switch (attr) {
510 case FRAG_RESULT_DEPTH:
511 /* handled above */
512 assert(0);
513 break;
514 default:
515 assert(attr == FRAG_RESULT_COLOR ||
516 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
517 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
518 fs_output_semantic_index[fs_num_outputs] = numColors;
519 outputMapping[attr] = fs_num_outputs;
520 numColors++;
521 break;
522 }
523
524 output_flags[fs_num_outputs] = stfp->Base.Base.OutputFlags[attr];
525
526 fs_num_outputs++;
527 }
528 }
529 }
530
531 if (!inputMapping)
532 inputMapping = defaultInputMapping;
533
534 /* XXX: fix static allocation of tokens:
535 */
536 num_tokens = st_translate_mesa_program(st->ctx,
537 TGSI_PROCESSOR_FRAGMENT,
538 &stfp->Base.Base,
539 /* inputs */
540 fs_num_inputs,
541 inputMapping,
542 stfp->input_semantic_name,
543 stfp->input_semantic_index,
544 interpMode,
545 input_flags,
546 /* outputs */
547 fs_num_outputs,
548 outputMapping,
549 fs_output_semantic_name,
550 fs_output_semantic_index,
551 output_flags,
552 /* tokenized result */
553 tokens, ST_MAX_SHADER_TOKENS);
554
555 assert(num_tokens < ST_MAX_SHADER_TOKENS);
556
557 fs.tokens = (struct tgsi_token *)
558 _mesa_realloc(tokens,
559 ST_MAX_SHADER_TOKENS * sizeof *tokens,
560 num_tokens * sizeof *tokens);
561
562 stfp->state = fs; /* struct copy */
563 stfp->driver_shader = pipe->create_fs_state(pipe, &fs);
564
565 if (0)
566 _mesa_print_program(&stfp->Base.Base);
567
568 if (TGSI_DEBUG)
569 tgsi_dump( fs.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
570 }
571
572
573 /**
574 * Debug- print current shader text
575 */
576 void
577 st_print_shaders(GLcontext *ctx)
578 {
579 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
580 if (shProg) {
581 GLuint i;
582 for (i = 0; i < shProg->NumShaders; i++) {
583 printf("GLSL shader %u of %u:\n", i, shProg->NumShaders);
584 printf("%s\n", shProg->Shaders[i]->Source);
585 }
586 }
587 }