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