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