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