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