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