Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[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
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "draw/draw_context.h"
42 #include "tgsi/util/tgsi_dump.h"
43
44 #include "st_context.h"
45 #include "st_atom.h"
46 #include "st_program.h"
47 #include "st_mesa_to_tgsi.h"
48 #include "cso_cache/cso_context.h"
49
50
51 #define TGSI_DEBUG 0
52
53
54 /** XXX we should use the version of this from p_util.h but including
55 * that header causes symbol collisions.
56 */
57 static INLINE void *
58 mem_dup(const void *src, uint size)
59 {
60 void *dup = MALLOC(size);
61 if (dup)
62 memcpy(dup, src, size);
63 return dup;
64 }
65
66
67
68 /**
69 * Translate a Mesa vertex shader into a TGSI shader.
70 * \param outputMapping to map vertex program output registers to TGSI
71 * output slots
72 * \param tokensOut destination for TGSI tokens
73 * \return pointer to cached pipe_shader object.
74 */
75 void
76 st_translate_vertex_program(struct st_context *st,
77 struct st_vertex_program *stvp,
78 const GLuint outputMapping[])
79 {
80 struct pipe_context *pipe = st->pipe;
81 struct tgsi_token tokens[ST_MAX_SHADER_TOKENS];
82 GLuint defaultOutputMapping[VERT_RESULT_MAX];
83 struct pipe_shader_state vs;
84 GLuint attr, i;
85 GLuint num_generic = 0;
86 GLuint num_tokens;
87
88 ubyte vs_input_semantic_name[PIPE_MAX_SHADER_INPUTS];
89 ubyte vs_input_semantic_index[PIPE_MAX_SHADER_INPUTS];
90 uint vs_num_inputs = 0;
91
92 ubyte vs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
93 ubyte vs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
94 uint vs_num_outputs = 0;
95
96 memset(&vs, 0, sizeof(vs));
97
98 /*
99 * Determine number of inputs, the mappings between VERT_ATTRIB_x
100 * and TGSI generic input indexes, plus input attrib semantic info.
101 */
102 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
103 if (stvp->Base.Base.InputsRead & (1 << attr)) {
104 const GLuint slot = vs_num_inputs;
105
106 vs_num_inputs++;
107
108 stvp->input_to_index[attr] = slot;
109 stvp->index_to_input[slot] = attr;
110
111 switch (attr) {
112 case VERT_ATTRIB_POS:
113 vs_input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
114 vs_input_semantic_index[slot] = 0;
115 break;
116 case VERT_ATTRIB_WEIGHT:
117 /* fall-through */
118 case VERT_ATTRIB_NORMAL:
119 /* just label as a generic */
120 vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
121 vs_input_semantic_index[slot] = 0;
122 break;
123 case VERT_ATTRIB_COLOR0:
124 vs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
125 vs_input_semantic_index[slot] = 0;
126 break;
127 case VERT_ATTRIB_COLOR1:
128 vs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
129 vs_input_semantic_index[slot] = 1;
130 break;
131 case VERT_ATTRIB_FOG:
132 vs_input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
133 vs_input_semantic_index[slot] = 0;
134 break;
135 case VERT_ATTRIB_TEX0:
136 case VERT_ATTRIB_TEX1:
137 case VERT_ATTRIB_TEX2:
138 case VERT_ATTRIB_TEX3:
139 case VERT_ATTRIB_TEX4:
140 case VERT_ATTRIB_TEX5:
141 case VERT_ATTRIB_TEX6:
142 case VERT_ATTRIB_TEX7:
143 vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
144 vs_input_semantic_index[slot] = num_generic++;
145 break;
146 case VERT_ATTRIB_GENERIC0:
147 case VERT_ATTRIB_GENERIC1:
148 case VERT_ATTRIB_GENERIC2:
149 case VERT_ATTRIB_GENERIC3:
150 case VERT_ATTRIB_GENERIC4:
151 case VERT_ATTRIB_GENERIC5:
152 case VERT_ATTRIB_GENERIC6:
153 case VERT_ATTRIB_GENERIC7:
154 assert(attr < VERT_ATTRIB_MAX);
155 vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
156 vs_input_semantic_index[slot] = num_generic++;
157 break;
158 default:
159 assert(0);
160 }
161 }
162 }
163
164 /* initialize output semantics to defaults */
165 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; i++) {
166 vs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
167 vs_output_semantic_index[i] = 0;
168 }
169
170 num_generic = 0;
171 /*
172 * Determine number of outputs, the (default) output register
173 * mapping and the semantic information for each output.
174 */
175 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
176 if (stvp->Base.Base.OutputsWritten & (1 << attr)) {
177 GLuint slot;
178
179 if (outputMapping) {
180 slot = outputMapping[attr];
181 assert(slot != ~0);
182 }
183 else {
184 slot = vs_num_outputs;
185 vs_num_outputs++;
186 defaultOutputMapping[attr] = slot;
187 }
188
189 /*
190 printf("Output %u -> slot %u\n", attr, slot);
191 */
192
193 switch (attr) {
194 case VERT_RESULT_HPOS:
195 assert(slot == 0);
196 vs_output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
197 vs_output_semantic_index[slot] = 0;
198 break;
199 case VERT_RESULT_COL0:
200 vs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
201 vs_output_semantic_index[slot] = 0;
202 break;
203 case VERT_RESULT_COL1:
204 vs_output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
205 vs_output_semantic_index[slot] = 1;
206 break;
207 case VERT_RESULT_BFC0:
208 vs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
209 vs_output_semantic_index[slot] = 0;
210 break;
211 case VERT_RESULT_BFC1:
212 vs_output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
213 vs_output_semantic_index[slot] = 1;
214 break;
215 case VERT_RESULT_FOGC:
216 vs_output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
217 vs_output_semantic_index[slot] = 0;
218 break;
219 case VERT_RESULT_PSIZ:
220 vs_output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
221 vs_output_semantic_index[slot] = 0;
222 break;
223 case VERT_RESULT_EDGE:
224 assert(0);
225 break;
226 case VERT_RESULT_TEX0:
227 case VERT_RESULT_TEX1:
228 case VERT_RESULT_TEX2:
229 case VERT_RESULT_TEX3:
230 case VERT_RESULT_TEX4:
231 case VERT_RESULT_TEX5:
232 case VERT_RESULT_TEX6:
233 case VERT_RESULT_TEX7:
234 vs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
235 vs_output_semantic_index[slot] = num_generic++;
236 break;
237 case VERT_RESULT_VAR0:
238 /* fall-through */
239 default:
240 assert(attr - VERT_RESULT_VAR0 < MAX_VARYING);
241 vs_output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
242 vs_output_semantic_index[slot] = num_generic++;
243 }
244 }
245 }
246
247 assert(vs_output_semantic_name[0] == TGSI_SEMANTIC_POSITION);
248
249
250 if (outputMapping) {
251 /* find max output slot referenced to compute vs_num_outputs */
252 GLuint maxSlot = 0;
253 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
254 if (outputMapping[attr] != ~0 && outputMapping[attr] > maxSlot)
255 maxSlot = outputMapping[attr];
256 }
257 vs_num_outputs = maxSlot + 1;
258 }
259 else {
260 outputMapping = defaultOutputMapping;
261 }
262
263 /* free old shader state, if any */
264 if (stvp->state.tokens) {
265 FREE((void *) stvp->state.tokens);
266 stvp->state.tokens = NULL;
267 }
268 if (stvp->driver_shader) {
269 pipe->delete_vs_state(pipe, stvp->driver_shader);
270 stvp->driver_shader = NULL;
271 }
272
273 /* XXX: fix static allocation of tokens:
274 */
275 num_tokens = tgsi_translate_mesa_program( TGSI_PROCESSOR_VERTEX,
276 &stvp->Base.Base,
277 /* inputs */
278 vs_num_inputs,
279 stvp->input_to_index,
280 vs_input_semantic_name,
281 vs_input_semantic_index,
282 NULL,
283 /* outputs */
284 vs_num_outputs,
285 outputMapping,
286 vs_output_semantic_name,
287 vs_output_semantic_index,
288 /* tokenized result */
289 tokens, ST_MAX_SHADER_TOKENS);
290
291 vs.tokens = (struct tgsi_token *)
292 mem_dup(tokens, num_tokens * sizeof(tokens[0]));
293
294 stvp->num_inputs = vs_num_inputs;
295 stvp->state = vs; /* struct copy */
296 stvp->driver_shader = pipe->create_vs_state(pipe, &vs);
297
298 if (0)
299 _mesa_print_program(&stvp->Base.Base);
300
301 if (TGSI_DEBUG)
302 tgsi_dump( vs.tokens, 0 );
303 }
304
305
306
307 /**
308 * Translate a Mesa fragment shader into a TGSI shader.
309 * \param inputMapping to map fragment program input registers to TGSI
310 * input slots
311 * \param tokensOut destination for TGSI tokens
312 * \return pointer to cached pipe_shader object.
313 */
314 const struct cso_fragment_shader *
315 st_translate_fragment_program(struct st_context *st,
316 struct st_fragment_program *stfp,
317 const GLuint inputMapping[])
318 {
319 struct pipe_context *pipe = st->pipe;
320 struct tgsi_token tokens[ST_MAX_SHADER_TOKENS];
321 GLuint outputMapping[FRAG_RESULT_MAX];
322 GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
323 struct pipe_shader_state fs;
324 const struct cso_fragment_shader *cso;
325 GLuint interpMode[16]; /* XXX size? */
326 GLuint attr;
327 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
328 GLuint vslot = 0;
329 GLuint num_generic = 0;
330 GLuint num_tokens;
331
332 ubyte fs_input_semantic_name[PIPE_MAX_SHADER_INPUTS];
333 ubyte fs_input_semantic_index[PIPE_MAX_SHADER_INPUTS];
334 uint fs_num_inputs = 0;
335
336 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
337 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
338 uint fs_num_outputs = 0;
339
340 memset(&fs, 0, sizeof(fs));
341
342 /* which vertex output goes to the first fragment input: */
343 if (inputsRead & FRAG_BIT_WPOS)
344 vslot = 0;
345 else
346 vslot = 1;
347
348 /*
349 * Convert Mesa program inputs to TGSI input register semantics.
350 */
351 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
352 if (inputsRead & (1 << attr)) {
353 const GLuint slot = fs_num_inputs;
354
355 defaultInputMapping[attr] = slot;
356
357 stfp->input_map[slot] = vslot++;
358
359 fs_num_inputs++;
360
361 switch (attr) {
362 case FRAG_ATTRIB_WPOS:
363 fs_input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
364 fs_input_semantic_index[slot] = 0;
365 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
366 break;
367 case FRAG_ATTRIB_COL0:
368 fs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
369 fs_input_semantic_index[slot] = 0;
370 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
371 break;
372 case FRAG_ATTRIB_COL1:
373 fs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
374 fs_input_semantic_index[slot] = 1;
375 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
376 break;
377 case FRAG_ATTRIB_FOGC:
378 fs_input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
379 fs_input_semantic_index[slot] = 0;
380 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
381 break;
382 case FRAG_ATTRIB_TEX0:
383 case FRAG_ATTRIB_TEX1:
384 case FRAG_ATTRIB_TEX2:
385 case FRAG_ATTRIB_TEX3:
386 case FRAG_ATTRIB_TEX4:
387 case FRAG_ATTRIB_TEX5:
388 case FRAG_ATTRIB_TEX6:
389 case FRAG_ATTRIB_TEX7:
390 fs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
391 fs_input_semantic_index[slot] = num_generic++;
392 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
393 break;
394 case FRAG_ATTRIB_VAR0:
395 /* fall-through */
396 default:
397 fs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
398 fs_input_semantic_index[slot] = num_generic++;
399 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
400 }
401 }
402 }
403
404 /*
405 * Semantics and mapping for outputs
406 */
407 {
408 uint numColors = 0;
409 GLbitfield outputsWritten = stfp->Base.Base.OutputsWritten;
410
411 /* if z is written, emit that first */
412 if (outputsWritten & (1 << FRAG_RESULT_DEPR)) {
413 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
414 fs_output_semantic_index[fs_num_outputs] = 0;
415 outputMapping[FRAG_RESULT_DEPR] = fs_num_outputs;
416 fs_num_outputs++;
417 outputsWritten &= ~(1 << FRAG_RESULT_DEPR);
418 }
419
420 /* handle remaning outputs (color) */
421 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
422 if (outputsWritten & (1 << attr)) {
423 switch (attr) {
424 case FRAG_RESULT_DEPR:
425 /* handled above */
426 assert(0);
427 break;
428 case FRAG_RESULT_COLR:
429 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
430 fs_output_semantic_index[fs_num_outputs] = numColors;
431 outputMapping[attr] = fs_num_outputs;
432 numColors++;
433 break;
434 default:
435 assert(0);
436 }
437 fs_num_outputs++;
438 }
439 }
440 }
441
442 if (!inputMapping)
443 inputMapping = defaultInputMapping;
444
445 /* XXX: fix static allocation of tokens:
446 */
447 num_tokens = tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT,
448 &stfp->Base.Base,
449 /* inputs */
450 fs_num_inputs,
451 inputMapping,
452 fs_input_semantic_name,
453 fs_input_semantic_index,
454 interpMode,
455 /* outputs */
456 fs_num_outputs,
457 outputMapping,
458 fs_output_semantic_name,
459 fs_output_semantic_index,
460 /* tokenized result */
461 tokens, ST_MAX_SHADER_TOKENS);
462
463 fs.tokens = (struct tgsi_token *)
464 mem_dup(tokens, num_tokens * sizeof(tokens[0]));
465
466 stfp->state = fs; /* struct copy */
467 stfp->driver_shader = pipe->create_fs_state(pipe, &fs);
468
469 if (0)
470 _mesa_print_program(&stfp->Base.Base);
471
472 if (TGSI_DEBUG)
473 tgsi_dump( fs.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
474
475 return cso;
476 }
477