gallium: unused var silence warning
[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 /* XXX: fix static allocation of tokens:
264 */
265 num_tokens = tgsi_translate_mesa_program( TGSI_PROCESSOR_VERTEX,
266 &stvp->Base.Base,
267 /* inputs */
268 vs_num_inputs,
269 stvp->input_to_index,
270 vs_input_semantic_name,
271 vs_input_semantic_index,
272 NULL,
273 /* outputs */
274 vs_num_outputs,
275 outputMapping,
276 vs_output_semantic_name,
277 vs_output_semantic_index,
278 /* tokenized result */
279 tokens, ST_MAX_SHADER_TOKENS);
280
281 vs.tokens = (struct tgsi_token *)
282 mem_dup(tokens, num_tokens * sizeof(tokens[0]));
283
284 stvp->num_inputs = vs_num_inputs;
285 stvp->state = vs; /* struct copy */
286 stvp->driver_shader = pipe->create_vs_state(pipe, &vs);
287
288 if (0)
289 _mesa_print_program(&stvp->Base.Base);
290
291 if (TGSI_DEBUG)
292 tgsi_dump( vs.tokens, 0 );
293 }
294
295
296
297 /**
298 * Translate a Mesa fragment shader into a TGSI shader.
299 * \param inputMapping to map fragment program input registers to TGSI
300 * input slots
301 * \param tokensOut destination for TGSI tokens
302 * \return pointer to cached pipe_shader object.
303 */
304 const struct cso_fragment_shader *
305 st_translate_fragment_program(struct st_context *st,
306 struct st_fragment_program *stfp,
307 const GLuint inputMapping[])
308 {
309 struct pipe_context *pipe = st->pipe;
310 struct tgsi_token tokens[ST_MAX_SHADER_TOKENS];
311 GLuint outputMapping[FRAG_RESULT_MAX];
312 GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
313 struct pipe_shader_state fs;
314 const struct cso_fragment_shader *cso;
315 GLuint interpMode[16]; /* XXX size? */
316 GLuint attr;
317 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
318 GLuint vslot = 0;
319 GLuint num_generic = 0;
320 GLuint num_tokens;
321
322 ubyte fs_input_semantic_name[PIPE_MAX_SHADER_INPUTS];
323 ubyte fs_input_semantic_index[PIPE_MAX_SHADER_INPUTS];
324 uint fs_num_inputs = 0;
325
326 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
327 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
328 uint fs_num_outputs = 0;
329
330 memset(&fs, 0, sizeof(fs));
331
332 /* which vertex output goes to the first fragment input: */
333 if (inputsRead & FRAG_BIT_WPOS)
334 vslot = 0;
335 else
336 vslot = 1;
337
338 /*
339 * Convert Mesa program inputs to TGSI input register semantics.
340 */
341 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
342 if (inputsRead & (1 << attr)) {
343 const GLuint slot = fs_num_inputs;
344
345 defaultInputMapping[attr] = slot;
346
347 stfp->input_map[slot] = vslot++;
348
349 fs_num_inputs++;
350
351 switch (attr) {
352 case FRAG_ATTRIB_WPOS:
353 fs_input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
354 fs_input_semantic_index[slot] = 0;
355 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
356 break;
357 case FRAG_ATTRIB_COL0:
358 fs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
359 fs_input_semantic_index[slot] = 0;
360 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
361 break;
362 case FRAG_ATTRIB_COL1:
363 fs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
364 fs_input_semantic_index[slot] = 1;
365 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
366 break;
367 case FRAG_ATTRIB_FOGC:
368 fs_input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
369 fs_input_semantic_index[slot] = 0;
370 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
371 break;
372 case FRAG_ATTRIB_TEX0:
373 case FRAG_ATTRIB_TEX1:
374 case FRAG_ATTRIB_TEX2:
375 case FRAG_ATTRIB_TEX3:
376 case FRAG_ATTRIB_TEX4:
377 case FRAG_ATTRIB_TEX5:
378 case FRAG_ATTRIB_TEX6:
379 case FRAG_ATTRIB_TEX7:
380 fs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
381 fs_input_semantic_index[slot] = num_generic++;
382 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
383 break;
384 case FRAG_ATTRIB_VAR0:
385 /* fall-through */
386 default:
387 fs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
388 fs_input_semantic_index[slot] = num_generic++;
389 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
390 }
391 }
392 }
393
394 /*
395 * Semantics and mapping for outputs
396 */
397 {
398 uint numColors = 0;
399 GLbitfield outputsWritten = stfp->Base.Base.OutputsWritten;
400
401 /* if z is written, emit that first */
402 if (outputsWritten & (1 << FRAG_RESULT_DEPR)) {
403 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
404 fs_output_semantic_index[fs_num_outputs] = 0;
405 outputMapping[FRAG_RESULT_DEPR] = fs_num_outputs;
406 fs_num_outputs++;
407 outputsWritten &= ~(1 << FRAG_RESULT_DEPR);
408 }
409
410 /* handle remaning outputs (color) */
411 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
412 if (outputsWritten & (1 << attr)) {
413 switch (attr) {
414 case FRAG_RESULT_DEPR:
415 /* handled above */
416 assert(0);
417 break;
418 case FRAG_RESULT_COLR:
419 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
420 fs_output_semantic_index[fs_num_outputs] = numColors;
421 outputMapping[attr] = fs_num_outputs;
422 numColors++;
423 break;
424 default:
425 assert(0);
426 }
427 fs_num_outputs++;
428 }
429 }
430 }
431
432 if (!inputMapping)
433 inputMapping = defaultInputMapping;
434
435 /* XXX: fix static allocation of tokens:
436 */
437 num_tokens = tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT,
438 &stfp->Base.Base,
439 /* inputs */
440 fs_num_inputs,
441 inputMapping,
442 fs_input_semantic_name,
443 fs_input_semantic_index,
444 interpMode,
445 /* outputs */
446 fs_num_outputs,
447 outputMapping,
448 fs_output_semantic_name,
449 fs_output_semantic_index,
450 /* tokenized result */
451 tokens, ST_MAX_SHADER_TOKENS);
452
453 fs.tokens = (struct tgsi_token *)
454 mem_dup(tokens, num_tokens * sizeof(tokens[0]));
455
456 stfp->state = fs; /* struct copy */
457 stfp->driver_shader = pipe->create_fs_state(pipe, &fs);
458
459 if (0)
460 _mesa_print_program(&stfp->Base.Base);
461
462 if (TGSI_DEBUG)
463 tgsi_dump( fs.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
464
465 return cso;
466 }
467