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