gallium: more work for edgeflags changes
[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/tgsi_dump.h"
44
45 #include "st_debug.h"
46 #include "st_context.h"
47 #include "st_atom.h"
48 #include "st_program.h"
49 #include "st_mesa_to_tgsi.h"
50 #include "cso_cache/cso_context.h"
51
52
53
54 /**
55 * Clean out any old compilations:
56 */
57 void
58 st_vp_release_varients( struct st_context *st,
59 struct st_vertex_program *stvp )
60 {
61 struct st_vp_varient *vpv;
62
63 for (vpv = stvp->varients; vpv; ) {
64 struct st_vp_varient *next = vpv->next;
65
66 if (vpv->driver_shader)
67 cso_delete_vertex_shader(st->cso_context, vpv->driver_shader);
68
69 if (vpv->draw_shader)
70 draw_delete_vertex_shader( st->draw, vpv->draw_shader );
71
72 if (vpv->state.tokens)
73 st_free_tokens(vpv->state.tokens);
74
75 FREE( vpv );
76
77 vpv = next;
78 }
79
80 stvp->varients = NULL;
81 }
82
83
84
85
86 /**
87 * Translate a Mesa vertex shader into a TGSI shader.
88 * \param outputMapping to map vertex program output registers (VERT_RESULT_x)
89 * to TGSI output slots
90 * \param tokensOut destination for TGSI tokens
91 * \return pointer to cached pipe_shader object.
92 */
93 void
94 st_prepare_vertex_program(struct st_context *st,
95 struct st_vertex_program *stvp)
96 {
97 GLuint attr;
98
99 stvp->num_inputs = 0;
100 stvp->num_outputs = 0;
101
102 if (stvp->Base.IsPositionInvariant)
103 _mesa_insert_mvp_code(st->ctx, &stvp->Base);
104
105 /*
106 * Determine number of inputs, the mappings between VERT_ATTRIB_x
107 * and TGSI generic input indexes, plus input attrib semantic info.
108 */
109 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
110 if (stvp->Base.Base.InputsRead & (1 << attr)) {
111 stvp->input_to_index[attr] = stvp->num_inputs;
112 stvp->index_to_input[stvp->num_inputs] = attr;
113 stvp->num_inputs++;
114 }
115 }
116
117 /* Compute mapping of vertex program outputs to slots.
118 */
119 for (attr = 0; attr < VERT_RESULT_MAX; attr++) {
120 if ((stvp->Base.Base.OutputsWritten & (1 << attr)) == 0) {
121 stvp->result_to_output[attr] = ~0;
122 }
123 else {
124 unsigned slot = stvp->num_outputs++;
125
126 stvp->result_to_output[attr] = slot;
127
128 switch (attr) {
129 case VERT_RESULT_HPOS:
130 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
131 stvp->output_semantic_index[slot] = 0;
132 break;
133 case VERT_RESULT_COL0:
134 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
135 stvp->output_semantic_index[slot] = 0;
136 break;
137 case VERT_RESULT_COL1:
138 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
139 stvp->output_semantic_index[slot] = 1;
140 break;
141 case VERT_RESULT_BFC0:
142 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
143 stvp->output_semantic_index[slot] = 0;
144 break;
145 case VERT_RESULT_BFC1:
146 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_BCOLOR;
147 stvp->output_semantic_index[slot] = 1;
148 break;
149 case VERT_RESULT_FOGC:
150 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_FOG;
151 stvp->output_semantic_index[slot] = 0;
152 break;
153 case VERT_RESULT_PSIZ:
154 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
155 stvp->output_semantic_index[slot] = 0;
156 break;
157 case VERT_RESULT_EDGE:
158 assert(0);
159 break;
160
161 case VERT_RESULT_TEX0:
162 case VERT_RESULT_TEX1:
163 case VERT_RESULT_TEX2:
164 case VERT_RESULT_TEX3:
165 case VERT_RESULT_TEX4:
166 case VERT_RESULT_TEX5:
167 case VERT_RESULT_TEX6:
168 case VERT_RESULT_TEX7:
169 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
170 stvp->output_semantic_index[slot] = attr - VERT_RESULT_TEX0;
171 break;
172
173 case VERT_RESULT_VAR0:
174 default:
175 assert(attr < VERT_RESULT_MAX);
176 stvp->output_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
177 stvp->output_semantic_index[slot] = (FRAG_ATTRIB_VAR0 -
178 FRAG_ATTRIB_TEX0 +
179 attr -
180 VERT_RESULT_VAR0);
181 break;
182 }
183 }
184 }
185 }
186
187
188 struct st_vp_varient *
189 st_translate_vertex_program(struct st_context *st,
190 struct st_vertex_program *stvp,
191 const struct st_vp_varient_key *key)
192 {
193 struct st_vp_varient *vpv = CALLOC_STRUCT(st_vp_varient);
194 struct pipe_context *pipe = st->pipe;
195 struct ureg_program *ureg;
196 enum pipe_error error;
197
198 ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
199 if (ureg == NULL)
200 return NULL;
201
202 error =
203 st_translate_mesa_program(st->ctx,
204 TGSI_PROCESSOR_VERTEX,
205 ureg,
206 &stvp->Base.Base,
207 /* inputs */
208 stvp->num_inputs,
209 stvp->input_to_index,
210 NULL, /* input semantic name */
211 NULL, /* input semantic index */
212 NULL,
213 /* outputs */
214 stvp->num_outputs,
215 stvp->result_to_output,
216 stvp->output_semantic_name,
217 stvp->output_semantic_index );
218
219 if (error)
220 goto fail;
221
222 /* Edgeflags will be the last input:
223 */
224 if (key->passthrough_edgeflags) {
225 ureg_MOV( ureg,
226 ureg_DECL_output( ureg, TGSI_SEMANTIC_EDGEFLAG, 0 ),
227 ureg_DECL_next_vs_input(ureg));
228 }
229
230 vpv->state.tokens = ureg_get_tokens( ureg, NULL );
231 ureg_destroy( ureg );
232
233 vpv->driver_shader = pipe->create_vs_state(pipe, &vpv->state);
234
235 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
236 _mesa_print_program(&stvp->Base.Base);
237 debug_printf("\n");
238 }
239
240 if (ST_DEBUG & DEBUG_TGSI) {
241 tgsi_dump( vpv->state.tokens, 0 );
242 debug_printf("\n");
243 }
244
245 return vpv;
246
247 fail:
248 ureg_destroy( ureg );
249 return NULL;
250 }
251
252
253
254 /**
255 * Translate a Mesa fragment shader into a TGSI shader.
256 * \param inputMapping to map fragment program input registers to TGSI
257 * input slots
258 * \return pointer to cached pipe_shader object.
259 */
260 void
261 st_translate_fragment_program(struct st_context *st,
262 struct st_fragment_program *stfp,
263 const GLuint inputMapping[])
264 {
265 struct pipe_context *pipe = st->pipe;
266 GLuint outputMapping[FRAG_RESULT_MAX];
267 GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
268 GLuint interpMode[16]; /* XXX size? */
269 GLuint attr;
270 enum pipe_error error;
271 const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
272 struct ureg_program *ureg;
273 GLuint vslot = 0;
274
275 uint fs_num_inputs = 0;
276
277 ubyte fs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
278 ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
279 uint fs_num_outputs = 0;
280
281 /* which vertex output goes to the first fragment input: */
282 if (inputsRead & FRAG_BIT_WPOS)
283 vslot = 0;
284 else
285 vslot = 1;
286
287 /*
288 * Convert Mesa program inputs to TGSI input register semantics.
289 */
290 for (attr = 0; attr < FRAG_ATTRIB_MAX; attr++) {
291 if (inputsRead & (1 << attr)) {
292 const GLuint slot = fs_num_inputs;
293
294 defaultInputMapping[attr] = slot;
295
296 stfp->input_map[slot] = vslot++;
297
298 fs_num_inputs++;
299
300 switch (attr) {
301 case FRAG_ATTRIB_WPOS:
302 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
303 stfp->input_semantic_index[slot] = 0;
304 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
305 break;
306 case FRAG_ATTRIB_COL0:
307 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
308 stfp->input_semantic_index[slot] = 0;
309 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
310 break;
311 case FRAG_ATTRIB_COL1:
312 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
313 stfp->input_semantic_index[slot] = 1;
314 interpMode[slot] = TGSI_INTERPOLATE_LINEAR;
315 break;
316 case FRAG_ATTRIB_FOGC:
317 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
318 stfp->input_semantic_index[slot] = 0;
319 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
320 break;
321 case FRAG_ATTRIB_FACE:
322 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_FACE;
323 stfp->input_semantic_index[slot] = 0;
324 interpMode[slot] = TGSI_INTERPOLATE_CONSTANT;
325 break;
326
327 /* In most cases, there is nothing special about these
328 * inputs, so adopt a convention to use the generic
329 * semantic name and the mesa FRAG_ATTRIB_ number as the
330 * index.
331 *
332 * All that is required is that the vertex shader labels
333 * its own outputs similarly, and that the vertex shader
334 * generates at least every output required by the
335 * fragment shader plus fixed-function hardware (such as
336 * BFC).
337 *
338 * There is no requirement that semantic indexes start at
339 * zero or be restricted to a particular range -- nobody
340 * should be building tables based on semantic index.
341 */
342 case FRAG_ATTRIB_TEX0:
343 case FRAG_ATTRIB_TEX1:
344 case FRAG_ATTRIB_TEX2:
345 case FRAG_ATTRIB_TEX3:
346 case FRAG_ATTRIB_TEX4:
347 case FRAG_ATTRIB_TEX5:
348 case FRAG_ATTRIB_TEX6:
349 case FRAG_ATTRIB_TEX7:
350 case FRAG_ATTRIB_PNTC:
351 case FRAG_ATTRIB_VAR0:
352 default:
353 /* Actually, let's try and zero-base this just for
354 * readability of the generated TGSI.
355 */
356 assert(attr >= FRAG_ATTRIB_TEX0);
357 stfp->input_semantic_index[slot] = (attr - FRAG_ATTRIB_TEX0);
358 stfp->input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
359 interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
360 break;
361 }
362 }
363 }
364
365 /*
366 * Semantics and mapping for outputs
367 */
368 {
369 uint numColors = 0;
370 GLbitfield64 outputsWritten = stfp->Base.Base.OutputsWritten;
371
372 /* if z is written, emit that first */
373 if (outputsWritten & (1 << FRAG_RESULT_DEPTH)) {
374 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_POSITION;
375 fs_output_semantic_index[fs_num_outputs] = 0;
376 outputMapping[FRAG_RESULT_DEPTH] = fs_num_outputs;
377 fs_num_outputs++;
378 outputsWritten &= ~(1 << FRAG_RESULT_DEPTH);
379 }
380
381 /* handle remaning outputs (color) */
382 for (attr = 0; attr < FRAG_RESULT_MAX; attr++) {
383 if (outputsWritten & (1 << attr)) {
384 switch (attr) {
385 case FRAG_RESULT_DEPTH:
386 /* handled above */
387 assert(0);
388 break;
389 default:
390 assert(attr == FRAG_RESULT_COLOR ||
391 (FRAG_RESULT_DATA0 <= attr && attr < FRAG_RESULT_MAX));
392 fs_output_semantic_name[fs_num_outputs] = TGSI_SEMANTIC_COLOR;
393 fs_output_semantic_index[fs_num_outputs] = numColors;
394 outputMapping[attr] = fs_num_outputs;
395 numColors++;
396 break;
397 }
398
399 fs_num_outputs++;
400 }
401 }
402 }
403
404 if (!inputMapping)
405 inputMapping = defaultInputMapping;
406
407 ureg = ureg_create( TGSI_PROCESSOR_FRAGMENT );
408 if (ureg == NULL)
409 return;
410
411
412 error =
413 st_translate_mesa_program(st->ctx,
414 TGSI_PROCESSOR_FRAGMENT,
415 ureg,
416 &stfp->Base.Base,
417 /* inputs */
418 fs_num_inputs,
419 inputMapping,
420 stfp->input_semantic_name,
421 stfp->input_semantic_index,
422 interpMode,
423 /* outputs */
424 fs_num_outputs,
425 outputMapping,
426 fs_output_semantic_name,
427 fs_output_semantic_index );
428
429 stfp->state.tokens = ureg_get_tokens( ureg, NULL );
430 ureg_destroy( ureg );
431 stfp->driver_shader = pipe->create_fs_state(pipe, &stfp->state);
432
433 if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
434 _mesa_print_program(&stfp->Base.Base);
435 debug_printf("\n");
436 }
437
438 if (ST_DEBUG & DEBUG_TGSI) {
439 tgsi_dump( stfp->state.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
440 debug_printf("\n");
441 }
442 }
443
444
445 /**
446 * Debug- print current shader text
447 */
448 void
449 st_print_shaders(GLcontext *ctx)
450 {
451 struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
452 if (shProg) {
453 GLuint i;
454 for (i = 0; i < shProg->NumShaders; i++) {
455 printf("GLSL shader %u of %u:\n", i, shProg->NumShaders);
456 printf("%s\n", shProg->Shaders[i]->Source);
457 }
458 }
459 }