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