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