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