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