translate Mesa programs to TGSI programs (formerly in st_atom_[fv]s.c)
[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 "shader/prog_parameter.h"
35 #include "shader/prog_print.h"
36 #include "tnl/t_vp_build.h"
37
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_winsys.h"
41 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
42 #include "pipe/tgsi/exec/tgsi_core.h"
43
44 #include "st_context.h"
45 #include "st_cache.h"
46 #include "st_atom.h"
47 #include "st_program.h"
48
49
50 #define TGSI_DEBUG 1
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_mesa_compile_vp_program( &stvp->Base,
238 /* inputs */
239 vs.num_inputs,
240 stvp->input_to_index,
241 vs.input_semantic_name,
242 vs.input_semantic_index,
243 /* outputs */
244 vs.num_outputs,
245 outputMapping,
246 vs.output_semantic_name,
247 vs.output_semantic_index,
248 /* tokenized result */
249 tokensOut, maxTokens);
250
251 vs.tokens = tokensOut;
252 cso = st_cached_vs_state(st, &vs);
253 stvp->vs = cso;
254
255 if (TGSI_DEBUG)
256 tgsi_dump( tokensOut, 0 );
257
258 #if defined(USE_X86_ASM) || defined(SLANG_X86)
259 if (stvp->sse2_program.csr == stvp->sse2_program.store)
260 tgsi_emit_sse2( tokensOut, &stvp->sse2_program );
261
262 if (!cso->state.executable)
263 ((struct cso_vertex_shader*)cso)->state.executable = (void *) x86_get_func( &stvp->sse2_program );
264 #endif
265
266 return cso;
267 }
268
269
270
271 /**
272 * Translate a Mesa fragment shader into a TGSI shader.
273 * \param inputMapping to map fragment program input registers to TGSI
274 * input slots
275 * \param tokensOut destination for TGSI tokens
276 * \return pointer to cached pipe_shader object.
277 */
278 const struct cso_fragment_shader *
279 st_translate_fragment_program(struct st_context *st,
280 struct st_fragment_program *stfp,
281 const GLuint inputMapping[],
282 struct tgsi_token *tokensOut,
283 GLuint maxTokens)
284 {
285 GLuint outputMapping[FRAG_RESULT_MAX];
286 GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
287 struct pipe_shader_state fs;
288 const struct cso_fragment_shader *cso;
289 GLuint interpMode[16]; /* XXX size? */
290 GLuint attr;
291 GLbitfield inputsRead = stfp->Base.Base.InputsRead;
292
293 /* Check if all fragment programs need the fragment position (in order
294 * to do perspective-corrected interpolation).
295 */
296 /* XXX temporary! */
297 if (st->pipe->get_param(st->pipe, PIPE_PARAM_FS_NEEDS_POS))
298 inputsRead |= FRAG_BIT_WPOS;
299
300 memset(&fs, 0, sizeof(fs));
301
302 /*
303 * Convert Mesa program inputs to TGSI input register semantics.
304 */
305 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
306 if (inputsRead & (1 << attr)) {
307 const GLuint slot = fs.num_inputs;
308
309 fs.num_inputs++;
310
311 defaultInputMapping[attr] = slot;
312
313 switch (attr) {
314 case FRAG_ATTRIB_WPOS:
315 fs.input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
316 fs.input_semantic_index[slot] = 0;
317 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
318 break;
319 case FRAG_ATTRIB_COL0:
320 fs.input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
321 fs.input_semantic_index[slot] = 0;
322 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
323 break;
324 case FRAG_ATTRIB_COL1:
325 fs.input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
326 fs.input_semantic_index[slot] = 1;
327 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
328 break;
329 case FRAG_ATTRIB_FOGC:
330 fs.input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
331 fs.input_semantic_index[slot] = 0;
332 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
333 break;
334 case FRAG_ATTRIB_TEX0:
335 case FRAG_ATTRIB_TEX1:
336 case FRAG_ATTRIB_TEX2:
337 case FRAG_ATTRIB_TEX3:
338 case FRAG_ATTRIB_TEX4:
339 case FRAG_ATTRIB_TEX5:
340 case FRAG_ATTRIB_TEX6:
341 case FRAG_ATTRIB_TEX7:
342 fs.input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
343 fs.input_semantic_index[slot] = attr - FRAG_ATTRIB_TEX0;
344 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
345 break;
346 case FRAG_ATTRIB_VAR0:
347 /* fall-through */
348 default:
349 fs.input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
350 fs.input_semantic_index[slot] = attr - FRAG_ATTRIB_VAR0;
351 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
352 }
353 }
354 }
355
356 /*
357 * Semantics for outputs
358 */
359 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
360 if (stfp->Base.Base.OutputsWritten & (1 << attr)) {
361 switch (attr) {
362 case FRAG_RESULT_DEPR:
363 fs.output_semantic_name[fs.num_outputs] = TGSI_SEMANTIC_POSITION;
364 outputMapping[attr] = fs.num_outputs;
365 break;
366 case FRAG_RESULT_COLR:
367 fs.output_semantic_name[fs.num_outputs] = TGSI_SEMANTIC_COLOR;
368 outputMapping[attr] = fs.num_outputs;
369 break;
370 default:
371 assert(0);
372 }
373 fs.num_outputs++;
374 }
375 }
376
377 if (!inputMapping)
378 inputMapping = defaultInputMapping;
379
380 /* XXX: fix static allocation of tokens:
381 */
382 tgsi_mesa_compile_fp_program( &stfp->Base,
383 /* inputs */
384 fs.num_inputs,
385 inputMapping,
386 fs.input_semantic_name,
387 fs.input_semantic_index,
388 interpMode,
389 /* outputs */
390 outputMapping,
391 /* tokenized result */
392 tokensOut, maxTokens);
393
394
395 fs.tokens = tokensOut;
396
397 cso = st_cached_fs_state(st, &fs);
398 stfp->fs = cso;
399
400 if (TGSI_DEBUG)
401 tgsi_dump( tokensOut, 0/*TGSI_DUMP_VERBOSE*/ );
402
403 #if defined(USE_X86_ASM) || defined(SLANG_X86)
404 if (stfp->sse2_program.csr == stfp->sse2_program.store)
405 tgsi_emit_sse2_fs( tokensOut, &stfp->sse2_program );
406
407 if (!cso->state.executable)
408 ((struct cso_fragment_shader*)cso)->state.executable = (void *) x86_get_func( &stfp->sse2_program );
409 #endif
410
411 return cso;
412 }
413