fix debug output mising from edgeflag changes
[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 /**
55 * Clean out any old compilations:
56 */
57 void
58 st_vp_release_varients( struct st_context *st,
59 struct st_vertex_program *stvp )
60 {
61 struct st_vp_varient *vpv;
62
63 for (vpv = stvp->varients; vpv; ) {
64 struct st_vp_varient *next = vpv->next;
65
66 if (vpv->driver_shader)
67 cso_delete_vertex_shader(st->cso_context, vpv->driver_shader);
68
69 if (vpv->draw_shader)
70 draw_delete_vertex_shader( st->draw, vpv->draw_shader );
71
72 if (vpv->state.tokens)
73 st_free_tokens(vpv->state.tokens);
74
75 FREE( vpv );
76
77 vpv = next;
78 }
79
80 stvp->varients = NULL;
81 }
82
83
84
85
86 /**
87 * Translate a Mesa vertex shader into a TGSI shader.
88 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
89 * to TGSI output slots
90 * \param tokensOut destination for TGSI tokens
91 * \return pointer to cached pipe_shader object.
92 */
93 void
94 st_prepare_vertex_program(struct st_context *st,
95 struct st_vertex_program *stvp)
96 {
97 GLuint attr;
98
99 stvp->num_inputs = 0;
100 stvp->num_outputs = 0;
101
102 if (stvp->Base.IsPositionInvariant)
103 _mesa_insert_mvp_code(st->ctx, &stvp->Base);
104
105 /*
106 * Determine number of inputs, the mappings between VERT_ATTRIB_x
107 * and TGSI generic input indexes, plus input attrib semantic info.
108 */
109 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
110 if (stvp->Base.Base.InputsRead & (1 << attr)) {
111 stvp->input_to_index[attr] = stvp->num_inputs;
112 stvp->index_to_input[stvp->num_inputs] = attr;
113 stvp->num_inputs++;
114 }
115 }
116 /* bit of a hack, presetup potentially unused edgeflag input */
117 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
118 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
119
120 /* Compute mapping of vertex program outputs to slots.
121 */
122 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
123 if ((stvp->Base.Base.OutputsWritten & (1 << attr)) == 0) {
124 stvp->result_to_output[attr] = ~0;
125 }
126 else {
127 unsigned slot = stvp->num_outputs++;
128
129 stvp->result_to_output[attr] = slot;
130
131 switch (attr) {
132 case VERT_RESULT_HPOS:
133 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
134 stvp->output_semantic_index[slot] = 0;
135 break;
136 case VERT_RESULT_COL0:
137 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
138 stvp->output_semantic_index[slot] = 0;
139 break;
140 case VERT_RESULT_COL1:
141 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
142 stvp->output_semantic_index[slot] = 1;
143 break;
144 case VERT_RESULT_BFC0:
145 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
146 stvp->output_semantic_index[slot] = 0;
147 break;
148 case VERT_RESULT_BFC1:
149 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
150 stvp->output_semantic_index[slot] = 1;
151 break;
152 case VERT_RESULT_FOGC:
153 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
154 stvp->output_semantic_index[slot] = 0;
155 break;
156 case VERT_RESULT_PSIZ:
157 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
158 stvp->output_semantic_index[slot] = 0;
159 break;
160 case VERT_RESULT_EDGE:
161 assert(0);
162 break;
163
164 case VERT_RESULT_TEX0:
165 case VERT_RESULT_TEX1:
166 case VERT_RESULT_TEX2:
167 case VERT_RESULT_TEX3:
168 case VERT_RESULT_TEX4:
169 case VERT_RESULT_TEX5:
170 case VERT_RESULT_TEX6:
171 case VERT_RESULT_TEX7:
172 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
173 stvp->output_semantic_index[slot] = attr - VERT_RESULT_TEX0;
174 break;
175
176 case VERT_RESULT_VAR0:
177 default:
178 assert(attr < VERT_RESULT_MAX);
179 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
180 stvp->output_semantic_index[slot] = (FRAG_ATTRIB_VAR0 -
181 FRAG_ATTRIB_TEX0 +
182 attr -
183 VERT_RESULT_VAR0);
184 break;
185 }
186 }
187 }
188 /* similar hack to above, presetup potentially unused edgeflag output */
189 stvp->result_to_output[VERT_RESULT_EDGE] = stvp->num_outputs;
190 stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
191 stvp->output_semantic_index[stvp->num_outputs] = 0;
192 }
193
194
195 struct st_vp_varient *
196 st_translate_vertex_program(struct st_context *st,
197 struct st_vertex_program *stvp,
198 const struct st_vp_varient_key *key)
199 {
200 struct st_vp_varient *vpv = CALLOC_STRUCT(st_vp_varient);
201 struct pipe_context *pipe = st->pipe;
202 struct ureg_program *ureg;
203 enum pipe_error error;
204 unsigned num_outputs;
205
206 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
207 if (ureg == NULL)
208 return NULL;
209
210 vpv->num_inputs = stvp->num_inputs;
211 num_outputs = stvp->num_outputs;
212 if (key->passthrough_edgeflags) {
213 vpv->num_inputs++;
214 num_outputs++;
215 }
216
217 error =
218 st_translate_mesa_program(st->ctx,
219 TGSI_PROCESSOR_VERTEX,
220 ureg,
221 &stvp->Base.Base,
222 /* inputs */
223 vpv->num_inputs,
224 stvp->input_to_index,
225 NULL, /* input semantic name */
226 NULL, /* input semantic index */
227 NULL,
228 /* outputs */
229 num_outputs,
230 stvp->result_to_output,
231 stvp->output_semantic_name,
232 stvp->output_semantic_index,
233 key->passthrough_edgeflags );
234
235 if (error)
236 goto fail;
237
238 vpv->state.tokens = ureg_get_tokens( ureg, NULL );
239 if (!vpv->state.tokens)
240 goto fail;
241
242 ureg_destroy( ureg );
243
244 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->state);
245
246 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
247 _mesa_print_program(&stvp->Base.Base);
248 debug_printf("\n");
249 }
250
251 if (ST_DEBUG & DEBUG_TGSI) {
252 tgsi_dump( vpv->state.tokens, 0 );
253 debug_printf("\n");
254 }
255
256 return vpv;
257
258 fail:
259 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__);
260 _mesa_print_program(&stvp->Base.Base);
261 debug_assert(0);
262
263 ureg_destroy( ureg );
264 return NULL;
265 }
266
267
268
269 /**
270 * Translate a Mesa fragment shader into a TGSI shader.
271 * \param inputMapping to map fragment program input registers to TGSI
272 * input slots
273 * \return pointer to cached pipe_shader object.
274 */
275 void
276 st_translate_fragment_program(struct st_context *st,
277 struct st_fragment_program *stfp,
278 const GLuint inputMapping[])
279 {
280 struct pipe_context *pipe = st->pipe;
281 GLuint outputMapping[FRAG_RESULT_MAX];
282 GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
283 GLuint interpMode[16]; /* XXX size? */
284 GLuint attr;
285 enum pipe_error error;
286 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
287 struct ureg_program *ureg;
288 GLuint vslot = 0;
289
290 uint fs_num_inputs = 0;
291
292 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
293 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
294 uint fs_num_outputs = 0;
295
296 /* which vertex output goes to the first fragment input: */
297 if (inputsRead & FRAG_BIT_WPOS)
298 vslot = 0;
299 else
300 vslot = 1;
301
302 /*
303 * Convert Mesa program inputs to TGSI input register semantics.
304 */
305 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
306 if (inputsRead & (1 << attr)) {
307 const GLuint slot = fs_num_inputs;
308
309 defaultInputMapping[attr] = slot;
310
311 stfp->input_map[slot] = vslot++;
312
313 fs_num_inputs++;
314
315 switch (attr) {
316 case FRAG_ATTRIB_WPOS:
317 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
318 stfp->input_semantic_index[slot] = 0;
319 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
320 break;
321 case FRAG_ATTRIB_COL0:
322 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
323 stfp->input_semantic_index[slot] = 0;
324 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
325 break;
326 case FRAG_ATTRIB_COL1:
327 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
328 stfp->input_semantic_index[slot] = 1;
329 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
330 break;
331 case FRAG_ATTRIB_FOGC:
332 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
333 stfp->input_semantic_index[slot] = 0;
334 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
335 break;
336 case FRAG_ATTRIB_FACE:
337 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
338 stfp->input_semantic_index[slot] = 0;
339 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
340 break;
341
342 /* In most cases, there is nothing special about these
343 * inputs, so adopt a convention to use the generic
344 * semantic name and the mesa FRAG_ATTRIB_ number as the
345 * index.
346 *
347 * All that is required is that the vertex shader labels
348 * its own outputs similarly, and that the vertex shader
349 * generates at least every output required by the
350 * fragment shader plus fixed-function hardware (such as
351 * BFC).
352 *
353 * There is no requirement that semantic indexes start at
354 * zero or be restricted to a particular range -- nobody
355 * should be building tables based on semantic index.
356 */
357 case FRAG_ATTRIB_TEX0:
358 case FRAG_ATTRIB_TEX1:
359 case FRAG_ATTRIB_TEX2:
360 case FRAG_ATTRIB_TEX3:
361 case FRAG_ATTRIB_TEX4:
362 case FRAG_ATTRIB_TEX5:
363 case FRAG_ATTRIB_TEX6:
364 case FRAG_ATTRIB_TEX7:
365 case FRAG_ATTRIB_PNTC:
366 case FRAG_ATTRIB_VAR0:
367 default:
368 /* Actually, let's try and zero-base this just for
369 * readability of the generated TGSI.
370 */
371 assert(attr >= FRAG_ATTRIB_TEX0);
372 stfp->input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0);
373 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
374 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
375 break;
376 }
377 }
378 }
379
380 /*
381 * Semantics and mapping for outputs
382 */
383 {
384 uint numColors = 0;
385 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
386
387 /* if z is written, emit that first */
388 if (outputsWritten & (1 << FRAG_RESULT_DEPTH)) {
389 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
390 fs_output_semantic_index[fs_num_outputs] = 0;
391 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
392 fs_num_outputs++;
393 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
394 }
395
396 /* handle remaning outputs (color) */
397 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
398 if (outputsWritten & (1 << attr)) {
399 switch (attr) {
400 case FRAG_RESULT_DEPTH:
401 /* handled above */
402 assert(0);
403 break;
404 default:
405 assert(attr == FRAG_RESULT_COLOR ||
406 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
407 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
408 fs_output_semantic_index[fs_num_outputs] = numColors;
409 outputMapping[attr] = fs_num_outputs;
410 numColors++;
411 break;
412 }
413
414 fs_num_outputs++;
415 }
416 }
417 }
418
419 if (!inputMapping)
420 inputMapping = defaultInputMapping;
421
422 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
423 if (ureg == NULL)
424 return;
425
426
427 error =
428 st_translate_mesa_program(st->ctx,
429 TGSI_PROCESSOR_FRAGMENT,
430 ureg,
431 &stfp->Base.Base,
432 /* inputs */
433 fs_num_inputs,
434 inputMapping,
435 stfp->input_semantic_name,
436 stfp->input_semantic_index,
437 interpMode,
438 /* outputs */
439 fs_num_outputs,
440 outputMapping,
441 fs_output_semantic_name,
442 fs_output_semantic_index, FALSE );
443
444 stfp->state.tokens = ureg_get_tokens( ureg, NULL );
445 ureg_destroy( ureg );
446 stfp->driver_shader = pipe->create_fs_state(pipe, &stfp->state);
447
448 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
449 _mesa_print_program(&stfp->Base.Base);
450 debug_printf("\n");
451 }
452
453 if (ST_DEBUG & DEBUG_TGSI) {
454 tgsi_dump( stfp->state.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
455 debug_printf("\n");
456 }
457 }
458
459
460 /**
461 * Debug- print current shader text
462 */
463 void
464 st_print_shaders(GLcontext *ctx)
465 {
466 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
467 if (shProg) {
468 GLuint i;
469 for (i = 0; i < shProg->NumShaders; i++) {
470 printf("GLSL shader %u of %u:\n", i, shProg->NumShaders);
471 printf("%s\n", shProg->Shaders[i]->Source);
472 }
473 }
474 }