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