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