st/mesa: check for PROG_PARAM_BIT_CYL_WRAP flag
[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_parameter.h"
37 #include "shader/prog_print.h"
38 #include "shader/programopt.h"
39
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "pipe/p_shader_tokens.h"
43 #include "draw/draw_context.h"
44 #include "tgsi/tgsi_dump.h"
45
46 #include "st_debug.h"
47 #include "st_context.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 assert(stvp->Base.Base.NumInstructions > 1);
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 stvp->input_to_index[attr] = stvp->num_inputs;
114 stvp->index_to_input[stvp->num_inputs] = attr;
115 stvp->num_inputs++;
116 }
117 }
118 /* bit of a hack, presetup potentially unused edgeflag input */
119 stvp->input_to_index[VERT_ATTRIB_EDGEFLAG] = stvp->num_inputs;
120 stvp->index_to_input[stvp->num_inputs] = VERT_ATTRIB_EDGEFLAG;
121
122 /* Compute mapping of vertex program outputs to slots.
123 */
124 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
125 if ((stvp->Base.Base.OutputsWritten & (1 << attr)) == 0) {
126 stvp->result_to_output[attr] = ~0;
127 }
128 else {
129 unsigned slot = stvp->num_outputs++;
130
131 stvp->result_to_output[attr] = slot;
132
133 switch (attr) {
134 case VERT_RESULT_HPOS:
135 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
136 stvp->output_semantic_index[slot] = 0;
137 break;
138 case VERT_RESULT_COL0:
139 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
140 stvp->output_semantic_index[slot] = 0;
141 break;
142 case VERT_RESULT_COL1:
143 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
144 stvp->output_semantic_index[slot] = 1;
145 break;
146 case VERT_RESULT_BFC0:
147 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
148 stvp->output_semantic_index[slot] = 0;
149 break;
150 case VERT_RESULT_BFC1:
151 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
152 stvp->output_semantic_index[slot] = 1;
153 break;
154 case VERT_RESULT_FOGC:
155 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
156 stvp->output_semantic_index[slot] = 0;
157 break;
158 case VERT_RESULT_PSIZ:
159 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
160 stvp->output_semantic_index[slot] = 0;
161 break;
162 case VERT_RESULT_EDGE:
163 assert(0);
164 break;
165
166 case VERT_RESULT_TEX0:
167 case VERT_RESULT_TEX1:
168 case VERT_RESULT_TEX2:
169 case VERT_RESULT_TEX3:
170 case VERT_RESULT_TEX4:
171 case VERT_RESULT_TEX5:
172 case VERT_RESULT_TEX6:
173 case VERT_RESULT_TEX7:
174 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
175 stvp->output_semantic_index[slot] = attr - VERT_RESULT_TEX0;
176 break;
177
178 case VERT_RESULT_VAR0:
179 default:
180 assert(attr < VERT_RESULT_MAX);
181 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
182 stvp->output_semantic_index[slot] = (FRAG_ATTRIB_VAR0 -
183 FRAG_ATTRIB_TEX0 +
184 attr -
185 VERT_RESULT_VAR0);
186 break;
187 }
188 }
189 }
190 /* similar hack to above, presetup potentially unused edgeflag output */
191 stvp->result_to_output[VERT_RESULT_EDGE] = stvp->num_outputs;
192 stvp->output_semantic_name[stvp->num_outputs] = TGSI_SEMANTIC_EDGEFLAG;
193 stvp->output_semantic_index[stvp->num_outputs] = 0;
194 }
195
196
197 struct st_vp_varient *
198 st_translate_vertex_program(struct st_context *st,
199 struct st_vertex_program *stvp,
200 const struct st_vp_varient_key *key)
201 {
202 struct st_vp_varient *vpv = CALLOC_STRUCT(st_vp_varient);
203 struct pipe_context *pipe = st->pipe;
204 struct ureg_program *ureg;
205 enum pipe_error error;
206 unsigned num_outputs;
207
208 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
209 if (ureg == NULL)
210 return NULL;
211
212 vpv->num_inputs = stvp->num_inputs;
213 num_outputs = stvp->num_outputs;
214 if (key->passthrough_edgeflags) {
215 vpv->num_inputs++;
216 num_outputs++;
217 }
218
219 error =
220 st_translate_mesa_program(st->ctx,
221 TGSI_PROCESSOR_VERTEX,
222 ureg,
223 &stvp->Base.Base,
224 /* inputs */
225 vpv->num_inputs,
226 stvp->input_to_index,
227 NULL, /* input semantic name */
228 NULL, /* input semantic index */
229 NULL,
230 /* outputs */
231 num_outputs,
232 stvp->result_to_output,
233 stvp->output_semantic_name,
234 stvp->output_semantic_index,
235 key->passthrough_edgeflags );
236
237 if (error)
238 goto fail;
239
240 vpv->state.tokens = ureg_get_tokens( ureg, NULL );
241 if (!vpv->state.tokens)
242 goto fail;
243
244 ureg_destroy( ureg );
245
246 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->state);
247
248 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
249 _mesa_print_program(&stvp->Base.Base);
250 debug_printf("\n");
251 }
252
253 if (ST_DEBUG & DEBUG_TGSI) {
254 tgsi_dump( vpv->state.tokens, 0 );
255 debug_printf("\n");
256 }
257
258 return vpv;
259
260 fail:
261 debug_printf("%s: failed to translate Mesa program:\n", __FUNCTION__);
262 _mesa_print_program(&stvp->Base.Base);
263 debug_assert(0);
264
265 ureg_destroy( ureg );
266 return NULL;
267 }
268
269
270
271 /**
272 * Translate a Mesa fragment shader into a TGSI shader.
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 {
279 struct pipe_context *pipe = st->pipe;
280 GLuint outputMapping[FRAG_RESULT_MAX];
281 GLuint inputMapping[FRAG_ATTRIB_MAX];
282 GLuint interpMode[16]; /* XXX size? */
283 GLuint attr;
284 enum pipe_error error;
285 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
286 struct ureg_program *ureg;
287
288 uint fs_num_inputs = 0;
289
290 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
291 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
292 uint fs_num_outputs = 0;
293
294 /*
295 * Convert Mesa program inputs to TGSI input register semantics.
296 */
297 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
298 if (inputsRead & (1 << attr)) {
299 const GLuint slot = fs_num_inputs++;
300
301 inputMapping[attr] = slot;
302
303 switch (attr) {
304 case FRAG_ATTRIB_WPOS:
305 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
306 stfp->input_semantic_index[slot] = 0;
307 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
308 break;
309 case FRAG_ATTRIB_COL0:
310 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
311 stfp->input_semantic_index[slot] = 0;
312 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
313 break;
314 case FRAG_ATTRIB_COL1:
315 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
316 stfp->input_semantic_index[slot] = 1;
317 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
318 break;
319 case FRAG_ATTRIB_FOGC:
320 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
321 stfp->input_semantic_index[slot] = 0;
322 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
323 break;
324 case FRAG_ATTRIB_FACE:
325 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
326 stfp->input_semantic_index[slot] = 0;
327 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
328 break;
329 case FRAG_ATTRIB_PNTC:
330 /* This is a hack. We really need a new semantic label for
331 * point coord. The draw module needs to know which fragment
332 * shader input is the point coord attribute so that it can set
333 * up the right vertex attribute values.
334 */
335 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
336 stfp->input_semantic_index[slot] = 0;
337 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
338 break;
339
340 /* In most cases, there is nothing special about these
341 * inputs, so adopt a convention to use the generic
342 * semantic name and the mesa FRAG_ATTRIB_ number as the
343 * index.
344 *
345 * All that is required is that the vertex shader labels
346 * its own outputs similarly, and that the vertex shader
347 * generates at least every output required by the
348 * fragment shader plus fixed-function hardware (such as
349 * BFC).
350 *
351 * There is no requirement that semantic indexes start at
352 * zero or be restricted to a particular range -- nobody
353 * should be building tables based on semantic index.
354 */
355 case FRAG_ATTRIB_TEX0:
356 case FRAG_ATTRIB_TEX1:
357 case FRAG_ATTRIB_TEX2:
358 case FRAG_ATTRIB_TEX3:
359 case FRAG_ATTRIB_TEX4:
360 case FRAG_ATTRIB_TEX5:
361 case FRAG_ATTRIB_TEX6:
362 case FRAG_ATTRIB_TEX7:
363 case FRAG_ATTRIB_VAR0:
364 default:
365 /* Actually, let's try and zero-base this just for
366 * readability of the generated TGSI.
367 */
368 assert(attr >= FRAG_ATTRIB_TEX0);
369 stfp->input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0);
370 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
371
372 /* XXX this test is very temporary */
373 if (stfp->Base.Base.InputFlags[0] & PROG_PARAM_BIT_CYL_WRAP) {
374 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
375 }
376 else {
377 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
378 }
379 break;
380 }
381 }
382 else {
383 inputMapping[attr] = -1;
384 }
385 }
386
387 /*
388 * Semantics and mapping for outputs
389 */
390 {
391 uint numColors = 0;
392 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
393
394 /* if z is written, emit that first */
395 if (outputsWritten & (1 << FRAG_RESULT_DEPTH)) {
396 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
397 fs_output_semantic_index[fs_num_outputs] = 0;
398 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
399 fs_num_outputs++;
400 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
401 }
402
403 /* handle remaning outputs (color) */
404 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
405 if (outputsWritten & (1 << attr)) {
406 switch (attr) {
407 case FRAG_RESULT_DEPTH:
408 /* handled above */
409 assert(0);
410 break;
411 default:
412 assert(attr == FRAG_RESULT_COLOR ||
413 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
414 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
415 fs_output_semantic_index[fs_num_outputs] = numColors;
416 outputMapping[attr] = fs_num_outputs;
417 numColors++;
418 break;
419 }
420
421 fs_num_outputs++;
422 }
423 }
424 }
425
426 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
427 if (ureg == NULL)
428 return;
429
430
431 error =
432 st_translate_mesa_program(st->ctx,
433 TGSI_PROCESSOR_FRAGMENT,
434 ureg,
435 &stfp->Base.Base,
436 /* inputs */
437 fs_num_inputs,
438 inputMapping,
439 stfp->input_semantic_name,
440 stfp->input_semantic_index,
441 interpMode,
442 /* outputs */
443 fs_num_outputs,
444 outputMapping,
445 fs_output_semantic_name,
446 fs_output_semantic_index, FALSE );
447
448 stfp->state.tokens = ureg_get_tokens( ureg, NULL );
449 ureg_destroy( ureg );
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 }