Merge branch 'mesa_7_7_branch'
[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
117 /* Compute mapping of vertex program outputs to slots.
118 */
119 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
120 if ((stvp->Base.Base.OutputsWritten & (1 << attr)) == 0) {
121 stvp->result_to_output[attr] = ~0;
122 }
123 else {
124 unsigned slot = stvp->num_outputs++;
125
126 stvp->result_to_output[attr] = slot;
127
128 switch (attr) {
129 case VERT_RESULT_HPOS:
130 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
131 stvp->output_semantic_index[slot] = 0;
132 break;
133 case VERT_RESULT_COL0:
134 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
135 stvp->output_semantic_index[slot] = 0;
136 break;
137 case VERT_RESULT_COL1:
138 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
139 stvp->output_semantic_index[slot] = 1;
140 break;
141 case VERT_RESULT_BFC0:
142 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
143 stvp->output_semantic_index[slot] = 0;
144 break;
145 case VERT_RESULT_BFC1:
146 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
147 stvp->output_semantic_index[slot] = 1;
148 break;
149 case VERT_RESULT_FOGC:
150 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
151 stvp->output_semantic_index[slot] = 0;
152 break;
153 case VERT_RESULT_PSIZ:
154 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
155 stvp->output_semantic_index[slot] = 0;
156 break;
157 case VERT_RESULT_EDGE:
158 assert(0);
159 break;
160
161 case VERT_RESULT_TEX0:
162 case VERT_RESULT_TEX1:
163 case VERT_RESULT_TEX2:
164 case VERT_RESULT_TEX3:
165 case VERT_RESULT_TEX4:
166 case VERT_RESULT_TEX5:
167 case VERT_RESULT_TEX6:
168 case VERT_RESULT_TEX7:
169 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
170 stvp->output_semantic_index[slot] = attr - VERT_RESULT_TEX0;
171 break;
172
173 case VERT_RESULT_VAR0:
174 default:
175 assert(attr < VERT_RESULT_MAX);
176 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
177 stvp->output_semantic_index[slot] = (FRAG_ATTRIB_VAR0 -
178 FRAG_ATTRIB_TEX0 +
179 attr -
180 VERT_RESULT_VAR0);
181 break;
182 }
183 }
184 }
185 }
186
187
188 struct st_vp_varient *
189 st_translate_vertex_program(struct st_context *st,
190 struct st_vertex_program *stvp,
191 const struct st_vp_varient_key *key)
192 {
193 struct st_vp_varient *vpv = CALLOC_STRUCT(st_vp_varient);
194 struct pipe_context *pipe = st->pipe;
195
196 vpv->state.tokens =
197 st_translate_mesa_program(st->ctx,
198 TGSI_PROCESSOR_VERTEX,
199 &stvp->Base.Base,
200 /* inputs */
201 stvp->num_inputs,
202 stvp->input_to_index,
203 NULL, /* input semantic name */
204 NULL, /* input semantic index */
205 NULL,
206 /* outputs */
207 stvp->num_outputs,
208 stvp->result_to_output,
209 stvp->output_semantic_name,
210 stvp->output_semantic_index );
211
212 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->state);
213
214 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
215 _mesa_print_program(&stvp->Base.Base);
216 debug_printf("\n");
217 }
218
219 if (ST_DEBUG & DEBUG_TGSI) {
220 tgsi_dump( vpv->state.tokens, 0 );
221 debug_printf("\n");
222 }
223
224 return vpv;
225 }
226
227
228
229 /**
230 * Translate a Mesa fragment shader into a TGSI shader.
231 * \param inputMapping to map fragment program input registers to TGSI
232 * input slots
233 * \return pointer to cached pipe_shader object.
234 */
235 void
236 st_translate_fragment_program(struct st_context *st,
237 struct st_fragment_program *stfp,
238 const GLuint inputMapping[])
239 {
240 struct pipe_context *pipe = st->pipe;
241 GLuint outputMapping[FRAG_RESULT_MAX];
242 GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
243 GLuint interpMode[16]; /* XXX size? */
244 GLuint attr;
245 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
246 GLuint vslot = 0;
247
248 uint fs_num_inputs = 0;
249
250 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
251 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
252 uint fs_num_outputs = 0;
253
254 /* which vertex output goes to the first fragment input: */
255 if (inputsRead & FRAG_BIT_WPOS)
256 vslot = 0;
257 else
258 vslot = 1;
259
260 /*
261 * Convert Mesa program inputs to TGSI input register semantics.
262 */
263 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
264 if (inputsRead & (1 << attr)) {
265 const GLuint slot = fs_num_inputs;
266
267 defaultInputMapping[attr] = slot;
268
269 stfp->input_map[slot] = vslot++;
270
271 fs_num_inputs++;
272
273 switch (attr) {
274 case FRAG_ATTRIB_WPOS:
275 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
276 stfp->input_semantic_index[slot] = 0;
277 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
278 break;
279 case FRAG_ATTRIB_COL0:
280 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
281 stfp->input_semantic_index[slot] = 0;
282 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
283 break;
284 case FRAG_ATTRIB_COL1:
285 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
286 stfp->input_semantic_index[slot] = 1;
287 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
288 break;
289 case FRAG_ATTRIB_FOGC:
290 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
291 stfp->input_semantic_index[slot] = 0;
292 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
293 break;
294 case FRAG_ATTRIB_FACE:
295 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
296 stfp->input_semantic_index[slot] = 0;
297 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
298 break;
299
300 /* In most cases, there is nothing special about these
301 * inputs, so adopt a convention to use the generic
302 * semantic name and the mesa FRAG_ATTRIB_ number as the
303 * index.
304 *
305 * All that is required is that the vertex shader labels
306 * its own outputs similarly, and that the vertex shader
307 * generates at least every output required by the
308 * fragment shader plus fixed-function hardware (such as
309 * BFC).
310 *
311 * There is no requirement that semantic indexes start at
312 * zero or be restricted to a particular range -- nobody
313 * should be building tables based on semantic index.
314 */
315 case FRAG_ATTRIB_TEX0:
316 case FRAG_ATTRIB_TEX1:
317 case FRAG_ATTRIB_TEX2:
318 case FRAG_ATTRIB_TEX3:
319 case FRAG_ATTRIB_TEX4:
320 case FRAG_ATTRIB_TEX5:
321 case FRAG_ATTRIB_TEX6:
322 case FRAG_ATTRIB_TEX7:
323 case FRAG_ATTRIB_PNTC:
324 case FRAG_ATTRIB_VAR0:
325 default:
326 /* Actually, let's try and zero-base this just for
327 * readability of the generated TGSI.
328 */
329 assert(attr >= FRAG_ATTRIB_TEX0);
330 stfp->input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0);
331 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
332 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
333 break;
334 }
335 }
336 }
337
338 /*
339 * Semantics and mapping for outputs
340 */
341 {
342 uint numColors = 0;
343 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
344
345 /* if z is written, emit that first */
346 if (outputsWritten & (1 << FRAG_RESULT_DEPTH)) {
347 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
348 fs_output_semantic_index[fs_num_outputs] = 0;
349 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
350 fs_num_outputs++;
351 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
352 }
353
354 /* handle remaning outputs (color) */
355 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
356 if (outputsWritten & (1 << attr)) {
357 switch (attr) {
358 case FRAG_RESULT_DEPTH:
359 /* handled above */
360 assert(0);
361 break;
362 default:
363 assert(attr == FRAG_RESULT_COLOR ||
364 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
365 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
366 fs_output_semantic_index[fs_num_outputs] = numColors;
367 outputMapping[attr] = fs_num_outputs;
368 numColors++;
369 break;
370 }
371
372 fs_num_outputs++;
373 }
374 }
375 }
376
377 if (!inputMapping)
378 inputMapping = defaultInputMapping;
379
380 stfp->state.tokens =
381 st_translate_mesa_program(st->ctx,
382 TGSI_PROCESSOR_FRAGMENT,
383 &stfp->Base.Base,
384 /* inputs */
385 fs_num_inputs,
386 inputMapping,
387 stfp->input_semantic_name,
388 stfp->input_semantic_index,
389 interpMode,
390 /* outputs */
391 fs_num_outputs,
392 outputMapping,
393 fs_output_semantic_name,
394 fs_output_semantic_index );
395
396 stfp->driver_shader = pipe->create_fs_state(pipe, &stfp->state);
397
398 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
399 _mesa_print_program(&stfp->Base.Base);
400 debug_printf("\n");
401 }
402
403 if (ST_DEBUG & DEBUG_TGSI) {
404 tgsi_dump( stfp->state.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
405 debug_printf("\n");
406 }
407 }
408
409
410 /**
411 * Debug- print current shader text
412 */
413 void
414 st_print_shaders(GLcontext *ctx)
415 {
416 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
417 if (shProg) {
418 GLuint i;
419 for (i = 0; i < shProg->NumShaders; i++) {
420 printf("GLSL shader %u of %u:\n", i, shProg->NumShaders);
421 printf("%s\n", shProg->Shaders[i]->Source);
422 }
423 }
424 }