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