Initial stab at LLVM integration.
[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/draw/draw_context.h"
40 #include "pipe/tgsi/mesa/mesa_to_tgsi.h"
41 #include "pipe/tgsi/exec/tgsi_core.h"
42 #include "pipe/llvm/llvmtgsi.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 0
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 vs.llvm_prog = ga_llvm_from_tgsi(vs.tokens);
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 GLbitfield inputsRead = stfp->Base.Base.InputsRead;
287
288 /* For software rendering, we always need the fragment input position
289 * in order to calculate interpolated values.
290 * For i915, we always want to emit the semantic info for position.
291 */
292 inputsRead |= FRAG_BIT_WPOS;
293
294 memset(&fs, 0, sizeof(fs));
295
296 /*
297 * Convert Mesa program inputs to TGSI input register semantics.
298 */
299 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
300 if (inputsRead & (1 << attr)) {
301 const GLuint slot = fs.num_inputs;
302
303 fs.num_inputs++;
304
305 defaultInputMapping[attr] = slot;
306
307 switch (attr) {
308 case FRAG_ATTRIB_WPOS:
309 fs.input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
310 fs.input_semantic_index[slot] = 0;
311 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
312 break;
313 case FRAG_ATTRIB_COL0:
314 fs.input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
315 fs.input_semantic_index[slot] = 0;
316 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
317 break;
318 case FRAG_ATTRIB_COL1:
319 fs.input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
320 fs.input_semantic_index[slot] = 1;
321 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
322 break;
323 case FRAG_ATTRIB_FOGC:
324 fs.input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
325 fs.input_semantic_index[slot] = 0;
326 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
327 break;
328 case FRAG_ATTRIB_TEX0:
329 case FRAG_ATTRIB_TEX1:
330 case FRAG_ATTRIB_TEX2:
331 case FRAG_ATTRIB_TEX3:
332 case FRAG_ATTRIB_TEX4:
333 case FRAG_ATTRIB_TEX5:
334 case FRAG_ATTRIB_TEX6:
335 case FRAG_ATTRIB_TEX7:
336 fs.input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
337 fs.input_semantic_index[slot] = attr - FRAG_ATTRIB_TEX0;
338 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
339 break;
340 case FRAG_ATTRIB_VAR0:
341 /* fall-through */
342 default:
343 fs.input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
344 fs.input_semantic_index[slot] = attr - FRAG_ATTRIB_VAR0;
345 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
346 }
347 }
348 }
349
350 /*
351 * Semantics and mapping for outputs
352 */
353 {
354 uint numColors = 0;
355 GLbitfield outputsWritten = stfp->Base.Base.OutputsWritten;
356
357 /* if z is written, emit that first */
358 if (outputsWritten & (1 << FRAG_RESULT_DEPR)) {
359 fs.output_semantic_name[fs.num_outputs] = TGSI_SEMANTIC_POSITION;
360 fs.output_semantic_index[fs.num_outputs] = 0;
361 outputMapping[FRAG_RESULT_DEPR] = fs.num_outputs;
362 fs.num_outputs++;
363 outputsWritten &= ~(1 << FRAG_RESULT_DEPR);
364 }
365
366 /* handle remaning outputs (color) */
367 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
368 if (outputsWritten & (1 << attr)) {
369 switch (attr) {
370 case FRAG_RESULT_DEPR:
371 /* handled above */
372 assert(0);
373 break;
374 case FRAG_RESULT_COLR:
375 fs.output_semantic_name[fs.num_outputs] = TGSI_SEMANTIC_COLOR;
376 fs.output_semantic_index[fs.num_outputs] = numColors;
377 outputMapping[attr] = fs.num_outputs;
378 numColors++;
379 break;
380 default:
381 assert(0);
382 }
383 fs.num_outputs++;
384 }
385 }
386 }
387
388 if (!inputMapping)
389 inputMapping = defaultInputMapping;
390
391 /* XXX: fix static allocation of tokens:
392 */
393 tgsi_translate_mesa_program( TGSI_PROCESSOR_FRAGMENT,
394 &stfp->Base.Base,
395 /* inputs */
396 fs.num_inputs,
397 inputMapping,
398 fs.input_semantic_name,
399 fs.input_semantic_index,
400 interpMode,
401 /* outputs */
402 fs.num_outputs,
403 outputMapping,
404 fs.output_semantic_name,
405 fs.output_semantic_index,
406 /* tokenized result */
407 tokensOut, maxTokens);
408
409 fs.tokens = tokensOut;
410 fs.llvm_prog = ga_llvm_from_tgsi(fs.tokens);
411 cso = st_cached_fs_state(st, &fs);
412 stfp->fs = cso;
413
414 if (TGSI_DEBUG)
415 tgsi_dump( tokensOut, 0/*TGSI_DUMP_VERBOSE*/ );
416
417 return cso;
418 }
419