r600: fix polygon offset
[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 assert(slot < Elements(vs_input_semantic_name));
155 vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
156 vs_input_semantic_index[slot] = num_generic++;
157 break;
158 case VERT_ATTRIB_GENERIC0:
159 case VERT_ATTRIB_GENERIC1:
160 case VERT_ATTRIB_GENERIC2:
161 case VERT_ATTRIB_GENERIC3:
162 case VERT_ATTRIB_GENERIC4:
163 case VERT_ATTRIB_GENERIC5:
164 case VERT_ATTRIB_GENERIC6:
165 case VERT_ATTRIB_GENERIC7:
166 case VERT_ATTRIB_GENERIC8:
167 case VERT_ATTRIB_GENERIC9:
168 case VERT_ATTRIB_GENERIC10:
169 case VERT_ATTRIB_GENERIC11:
170 case VERT_ATTRIB_GENERIC12:
171 case VERT_ATTRIB_GENERIC13:
172 case VERT_ATTRIB_GENERIC14:
173 case VERT_ATTRIB_GENERIC15:
174 assert(attr < VERT_ATTRIB_MAX);
175 assert(slot < Elements(vs_input_semantic_name));
176 vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
177 vs_input_semantic_index[slot] = num_generic++;
178 break;
179 default:
180 assert(0);
181 }
182
183 input_flags[slot] = stvp->Base.Base.InputFlags[attr];
184 }
185 }
186
187 #if 0
188 if (outputMapping && outputSemanticName) {
189 printf("VERT_RESULT written out_slot semantic_name semantic_index\n");
190 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
191 printf(" %-2d %c %3d %2d %2d\n",
192 attr,
193 ((stvp->Base.Base.OutputsWritten & (1 << attr)) ? 'Y' : ' '),
194 outputMapping[attr],
195 outputSemanticName[attr],
196 outputSemanticIndex[attr]);
197 }
198 }
199 #endif
200
201 /* initialize output semantics to defaults */
202 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
203 assert(i < Elements(vs_output_semantic_name));
204 vs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
205 vs_output_semantic_index[i] = 0;
206 output_flags[i] = 0x0;
207 }
208
209 num_generic = 0;
210 /*
211 * Determine number of outputs, the (default) output register
212 * mapping and the semantic information for each output.
213 */
214 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
215 if (stvp->Base.Base.OutputsWritten & (1 << attr)) {
216 GLuint slot;
217
218 /* XXX
219 * Pass in the fragment program's input's semantic info.
220 * Use the generic semantic indexes from there, instead of
221 * guessing below.
222 */
223
224 if (outputMapping) {
225 slot = outputMapping[attr];
226 assert(slot != ~0);
227 }
228 else {
229 slot = vs_num_outputs;
230 vs_num_outputs++;
231 defaultOutputMapping[attr] = slot;
232 }
233
234 switch (attr) {
235 case VERT_RESULT_HPOS:
236 assert(slot == 0);
237 vs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
238 vs_output_semantic_index[slot] = 0;
239 break;
240 case VERT_RESULT_COL0:
241 vs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
242 vs_output_semantic_index[slot] = 0;
243 break;
244 case VERT_RESULT_COL1:
245 vs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
246 vs_output_semantic_index[slot] = 1;
247 break;
248 case VERT_RESULT_BFC0:
249 vs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
250 vs_output_semantic_index[slot] = 0;
251 break;
252 case VERT_RESULT_BFC1:
253 vs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
254 vs_output_semantic_index[slot] = 1;
255 break;
256 case VERT_RESULT_FOGC:
257 vs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
258 vs_output_semantic_index[slot] = 0;
259 break;
260 case VERT_RESULT_PSIZ:
261 vs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
262 vs_output_semantic_index[slot] = 0;
263 break;
264 case VERT_RESULT_EDGE:
265 assert(0);
266 break;
267 case VERT_RESULT_TEX0:
268 case VERT_RESULT_TEX1:
269 case VERT_RESULT_TEX2:
270 case VERT_RESULT_TEX3:
271 case VERT_RESULT_TEX4:
272 case VERT_RESULT_TEX5:
273 case VERT_RESULT_TEX6:
274 case VERT_RESULT_TEX7:
275 /* fall-through */
276 case VERT_RESULT_VAR0:
277 /* fall-through */
278 default:
279 assert(slot < Elements(vs_output_semantic_name));
280 if (outputSemanticName) {
281 /* use provided semantic into */
282 assert(outputSemanticName[attr] != TGSI_SEMANTIC_COUNT);
283 vs_output_semantic_name[slot] = outputSemanticName[attr];
284 vs_output_semantic_index[slot] = outputSemanticIndex[attr];
285 }
286 else {
287 /* use default semantic info */
288 vs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
289 vs_output_semantic_index[slot] = num_generic++;
290 }
291 }
292
293 assert(slot < Elements(output_flags));
294 output_flags[slot] = stvp->Base.Base.OutputFlags[attr];
295 }
296 }
297
298 if (outputMapping) {
299 /* find max output slot referenced to compute vs_num_outputs */
300 GLuint maxSlot = 0;
301 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
302 if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot)
303 maxSlot = outputMapping[attr];
304 }
305 vs_num_outputs = maxSlot + 1;
306 }
307 else {
308 outputMapping = defaultOutputMapping;
309 }
310
311 #if 0 /* debug */
312 {
313 GLuint i;
314 printf("outputMapping? %d\n", outputMapping ? 1 : 0);
315 if (outputMapping) {
316 printf("attr -> slot\n");
317 for (i = 0; i < 16; i++) {
318 printf(" %2d %3d\n", i, outputMapping[i]);
319 }
320 }
321 printf("slot sem_name sem_index\n");
322 for (i = 0; i < vs_num_outputs; i++) {
323 printf(" %2d %d %d\n",
324 i,
325 vs_output_semantic_name[i],
326 vs_output_semantic_index[i]);
327 }
328 }
329 #endif
330
331 /* free old shader state, if any */
332 if (stvp->state.tokens) {
333 _mesa_free((void *) stvp->state.tokens);
334 stvp->state.tokens = NULL;
335 }
336 if (stvp->driver_shader) {
337 cso_delete_vertex_shader(st->cso_context, stvp->driver_shader);
338 stvp->driver_shader = NULL;
339 }
340
341 /* XXX: fix static allocation of tokens:
342 */
343 num_tokens = st_translate_mesa_program(st->ctx,
344 TGSI_PROCESSOR_VERTEX,
345 &stvp->Base.Base,
346 /* inputs */
347 vs_num_inputs,
348 stvp->input_to_index,
349 vs_input_semantic_name,
350 vs_input_semantic_index,
351 NULL,
352 input_flags,
353 /* outputs */
354 vs_num_outputs,
355 outputMapping,
356 vs_output_semantic_name,
357 vs_output_semantic_index,
358 output_flags,
359 /* tokenized result */
360 tokens, ST_MAX_SHADER_TOKENS);
361
362 assert(num_tokens < ST_MAX_SHADER_TOKENS);
363
364 vs.tokens = (struct tgsi_token *)
365 _mesa_realloc(tokens,
366 ST_MAX_SHADER_TOKENS * sizeof *tokens,
367 num_tokens * sizeof *tokens);
368
369 stvp->num_inputs = vs_num_inputs;
370 stvp->state = vs; /* struct copy */
371 stvp->driver_shader = pipe->create_vs_state(pipe, &vs);
372
373 if (0)
374 _mesa_print_program(&stvp->Base.Base);
375
376 if (TGSI_DEBUG)
377 tgsi_dump( vs.tokens, 0 );
378 }
379
380
381
382 /**
383 * Translate a Mesa fragment shader into a TGSI shader.
384 * \param inputMapping to map fragment program input registers to TGSI
385 * input slots
386 * \param tokensOut destination for TGSI tokens
387 * \return pointer to cached pipe_shader object.
388 */
389 void
390 st_translate_fragment_program(struct st_context *st,
391 struct st_fragment_program *stfp,
392 const GLuint inputMapping[])
393 {
394 struct pipe_context *pipe = st->pipe;
395 struct tgsi_token *tokens;
396 GLuint outputMapping[FRAG_RESULT_MAX];
397 GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
398 struct pipe_shader_state fs;
399 GLuint interpMode[16]; /* XXX size? */
400 GLuint attr;
401 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
402 GLuint vslot = 0;
403 GLuint num_generic = 0;
404 GLuint num_tokens;
405
406 uint fs_num_inputs = 0;
407
408 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
409 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
410 uint fs_num_outputs = 0;
411
412 GLbitfield input_flags[MAX_PROGRAM_INPUTS];
413 GLbitfield output_flags[MAX_PROGRAM_OUTPUTS];
414
415 tokens = (struct tgsi_token *)MALLOC(ST_MAX_SHADER_TOKENS * sizeof *tokens);
416 if(!tokens) {
417 /* FIXME: propagate error to the caller */
418 assert(0);
419 return;
420 }
421
422 memset(&fs, 0, sizeof(fs));
423 memset(input_flags, 0, sizeof(input_flags));
424 memset(output_flags, 0, sizeof(output_flags));
425
426 /* which vertex output goes to the first fragment input: */
427 if (inputsRead & FRAG_BIT_WPOS)
428 vslot = 0;
429 else
430 vslot = 1;
431
432 /*
433 * Convert Mesa program inputs to TGSI input register semantics.
434 */
435 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
436 if (inputsRead & (1 << attr)) {
437 const GLuint slot = fs_num_inputs;
438
439 defaultInputMapping[attr] = slot;
440
441 stfp->input_map[slot] = vslot++;
442
443 fs_num_inputs++;
444
445 switch (attr) {
446 case FRAG_ATTRIB_WPOS:
447 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
448 stfp->input_semantic_index[slot] = 0;
449 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
450 break;
451 case FRAG_ATTRIB_COL0:
452 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
453 stfp->input_semantic_index[slot] = 0;
454 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
455 break;
456 case FRAG_ATTRIB_COL1:
457 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
458 stfp->input_semantic_index[slot] = 1;
459 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
460 break;
461 case FRAG_ATTRIB_FOGC:
462 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
463 stfp->input_semantic_index[slot] = 0;
464 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
465 break;
466 case FRAG_ATTRIB_FACE:
467 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
468 stfp->input_semantic_index[slot] = num_generic++;
469 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
470 break;
471 case FRAG_ATTRIB_PNTC:
472 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
473 stfp->input_semantic_index[slot] = num_generic++;
474 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
475 break;
476 case FRAG_ATTRIB_TEX0:
477 case FRAG_ATTRIB_TEX1:
478 case FRAG_ATTRIB_TEX2:
479 case FRAG_ATTRIB_TEX3:
480 case FRAG_ATTRIB_TEX4:
481 case FRAG_ATTRIB_TEX5:
482 case FRAG_ATTRIB_TEX6:
483 case FRAG_ATTRIB_TEX7:
484 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
485 stfp->input_semantic_index[slot] = num_generic++;
486 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
487 break;
488 case FRAG_ATTRIB_VAR0:
489 /* fall-through */
490 default:
491 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
492 stfp->input_semantic_index[slot] = num_generic++;
493 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
494 }
495
496 input_flags[slot] = stfp->Base.Base.InputFlags[attr];
497 }
498 }
499
500 /*
501 * Semantics and mapping for outputs
502 */
503 {
504 uint numColors = 0;
505 GLbitfield outputsWritten = stfp->Base.Base.OutputsWritten;
506
507 /* if z is written, emit that first */
508 if (outputsWritten & (1 << FRAG_RESULT_DEPTH)) {
509 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
510 fs_output_semantic_index[fs_num_outputs] = 0;
511 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
512 fs_num_outputs++;
513 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
514 }
515
516 /* handle remaning outputs (color) */
517 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
518 if (outputsWritten & (1 << attr)) {
519 switch (attr) {
520 case FRAG_RESULT_DEPTH:
521 /* handled above */
522 assert(0);
523 break;
524 default:
525 assert(attr == FRAG_RESULT_COLOR ||
526 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
527 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
528 fs_output_semantic_index[fs_num_outputs] = numColors;
529 outputMapping[attr] = fs_num_outputs;
530 numColors++;
531 break;
532 }
533
534 output_flags[fs_num_outputs] = stfp->Base.Base.OutputFlags[attr];
535
536 fs_num_outputs++;
537 }
538 }
539 }
540
541 if (!inputMapping)
542 inputMapping = defaultInputMapping;
543
544 /* XXX: fix static allocation of tokens:
545 */
546 num_tokens = st_translate_mesa_program(st->ctx,
547 TGSI_PROCESSOR_FRAGMENT,
548 &stfp->Base.Base,
549 /* inputs */
550 fs_num_inputs,
551 inputMapping,
552 stfp->input_semantic_name,
553 stfp->input_semantic_index,
554 interpMode,
555 input_flags,
556 /* outputs */
557 fs_num_outputs,
558 outputMapping,
559 fs_output_semantic_name,
560 fs_output_semantic_index,
561 output_flags,
562 /* tokenized result */
563 tokens, ST_MAX_SHADER_TOKENS);
564
565 assert(num_tokens < ST_MAX_SHADER_TOKENS);
566
567 fs.tokens = (struct tgsi_token *)
568 _mesa_realloc(tokens,
569 ST_MAX_SHADER_TOKENS * sizeof *tokens,
570 num_tokens * sizeof *tokens);
571
572 stfp->state = fs; /* struct copy */
573 stfp->driver_shader = pipe->create_fs_state(pipe, &fs);
574
575 if (0)
576 _mesa_print_program(&stfp->Base.Base);
577
578 if (TGSI_DEBUG)
579 tgsi_dump( fs.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
580 }
581
582
583 /**
584 * Debug- print current shader text
585 */
586 void
587 st_print_shaders(GLcontext *ctx)
588 {
589 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
590 if (shProg) {
591 GLuint i;
592 for (i = 0; i < shProg->NumShaders; i++) {
593 printf("GLSL shader %u of %u:\n", i, shProg->NumShaders);
594 printf("%s\n", shProg->Shaders[i]->Source);
595 }
596 }
597 }