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