Pass vertex program input semantics into tgsi_mesa_compile_vp_program() to produce...
[mesa.git] / src / mesa / state_tracker / st_atom_vs.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 #include "shader/prog_parameter.h"
34 #include "shader/prog_print.h"
35 #include "tnl/t_vp_build.h"
36
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_winsys.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 1
50
51
52 /**
53 * Translate a Mesa vertex shader into a TGSI shader.
54 * \return pointer to cached pipe_shader object.
55 */
56 const struct cso_vertex_shader *
57 st_translate_vertex_shader(struct st_context *st,
58 struct st_vertex_program *stvp)
59 {
60 struct pipe_shader_state vs;
61 const struct cso_vertex_shader *cso;
62 GLuint i;
63
64 memset(&vs, 0, sizeof(vs));
65
66 /*
67 * Determine number of inputs, the mappings between VERT_ATTRIB_x
68 * and TGSI generic input indexes, plus input attrib semantic info.
69 */
70 for (i = 0; i < MAX_VERTEX_PROGRAM_ATTRIBS; i++) {
71 if (stvp->Base.Base.InputsRead & (1 << i)) {
72 stvp->input_to_index[i] = vs.num_inputs;
73 stvp->index_to_input[vs.num_inputs] = i;
74 switch (i) {
75 case VERT_ATTRIB_POS:
76 vs.input_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_POSITION;
77 vs.input_semantic_index[vs.num_inputs] = 0;
78 break;
79 case VERT_ATTRIB_WEIGHT:
80 /* fall-through */
81 case VERT_ATTRIB_NORMAL:
82 /* just label as a generic */
83 vs.input_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_GENERIC;
84 vs.input_semantic_index[vs.num_inputs] = 0;
85 break;
86 case VERT_ATTRIB_COLOR0:
87 vs.input_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_COLOR;
88 vs.input_semantic_index[vs.num_inputs] = 0;
89 break;
90 case VERT_ATTRIB_COLOR1:
91 vs.input_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_COLOR;
92 vs.input_semantic_index[vs.num_inputs] = 1;
93 break;
94 case VERT_ATTRIB_TEX0:
95 case VERT_ATTRIB_TEX1:
96 case VERT_ATTRIB_TEX2:
97 case VERT_ATTRIB_TEX3:
98 case VERT_ATTRIB_TEX4:
99 case VERT_ATTRIB_TEX5:
100 case VERT_ATTRIB_TEX6:
101 case VERT_ATTRIB_TEX7:
102 vs.input_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_TEXCOORD;
103 vs.input_semantic_index[vs.num_inputs] = i - VERT_ATTRIB_TEX0;
104 break;
105 case VERT_ATTRIB_GENERIC0:
106 case VERT_ATTRIB_GENERIC1:
107 case VERT_ATTRIB_GENERIC2:
108 case VERT_ATTRIB_GENERIC3:
109 case VERT_ATTRIB_GENERIC4:
110 case VERT_ATTRIB_GENERIC5:
111 case VERT_ATTRIB_GENERIC6:
112 case VERT_ATTRIB_GENERIC7:
113 assert(i < VERT_ATTRIB_MAX);
114 vs.input_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_GENERIC;
115 vs.input_semantic_index[vs.num_inputs] = i - VERT_ATTRIB_GENERIC0;
116 break;
117 default:
118 assert(0);
119 }
120 vs.num_inputs++;
121 }
122 }
123
124 /*
125 * Determine number of outputs, the register mapping and
126 * the semantic information for each vertex output/result.
127 */
128 for (i = 0; i < VERT_RESULT_MAX; i++) {
129 if (stvp->Base.Base.OutputsWritten & (1 << i)) {
130 /* put this attrib in the next available slot */
131 st->vertex_attrib_to_slot[i] = vs.num_outputs;
132
133 switch (i) {
134 case VERT_RESULT_HPOS:
135 vs.output_semantic_name[vs.num_outputs] = TGSI_SEMANTIC_POSITION;
136 vs.output_semantic_index[vs.num_inputs] = 0;
137 break;
138 case VERT_RESULT_COL0:
139 vs.output_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_COLOR;
140 vs.output_semantic_index[vs.num_inputs] = 0;
141 break;
142 case VERT_RESULT_COL1:
143 vs.output_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_COLOR;
144 vs.output_semantic_index[vs.num_inputs] = 1;
145 break;
146 case VERT_RESULT_BFC0:
147 vs.output_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_BCOLOR;
148 vs.output_semantic_index[vs.num_inputs] = 0;
149 break;
150 case VERT_RESULT_BFC1:
151 vs.output_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_BCOLOR;
152 vs.output_semantic_index[vs.num_inputs] = 1;
153 break;
154 case VERT_RESULT_FOGC:
155 case VERT_RESULT_PSIZ:
156 case VERT_RESULT_EDGE:
157 assert(0);
158 break;
159 case VERT_RESULT_TEX0:
160 case VERT_RESULT_TEX1:
161 case VERT_RESULT_TEX2:
162 case VERT_RESULT_TEX3:
163 case VERT_RESULT_TEX4:
164 case VERT_RESULT_TEX5:
165 case VERT_RESULT_TEX6:
166 case VERT_RESULT_TEX7:
167 vs.output_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_TEXCOORD;
168 vs.output_semantic_index[vs.num_inputs] = i - VERT_RESULT_TEX0;
169 break;
170 case VERT_RESULT_VAR0:
171 /* fall-through */
172 default:
173 assert(i - VERT_RESULT_VAR0 < MAX_VARYING);
174 vs.output_semantic_name[vs.num_inputs] = TGSI_SEMANTIC_GENERIC;
175 vs.output_semantic_index[vs.num_inputs] = i - VERT_RESULT_VAR0;
176 }
177
178 vs.num_outputs++;
179 }
180 }
181
182
183 /* XXX: fix static allocation of tokens:
184 */
185 tgsi_mesa_compile_vp_program( &stvp->Base,
186 vs.num_inputs,
187 stvp->input_to_index,
188 vs.input_semantic_name,
189 vs.input_semantic_index,
190 st->vertex_attrib_to_slot,
191 stvp->tokens, ST_FP_MAX_TOKENS );
192
193 vs.tokens = &stvp->tokens[0];
194
195 cso = st_cached_vs_state(st, &vs);
196 stvp->vs = cso;
197
198 if (TGSI_DEBUG)
199 tgsi_dump( stvp->tokens, 0 );
200
201 #if defined(USE_X86_ASM) || defined(SLANG_X86)
202 if (stvp->sse2_program.csr == stvp->sse2_program.store)
203 tgsi_emit_sse2( stvp->tokens, &stvp->sse2_program );
204
205 if (!cso->state.executable)
206 cso->state.executable = (void *) x86_get_func( &stvp->sse2_program );
207 #endif
208
209 stvp->dirty = 0;
210
211 return cso;
212 }
213
214
215
216 static void update_vs( struct st_context *st )
217 {
218 struct st_vertex_program *stvp;
219
220 /* find active shader and params -- Should be covered by
221 * ST_NEW_VERTEX_PROGRAM
222 */
223 if (st->ctx->Shader.CurrentProgram &&
224 st->ctx->Shader.CurrentProgram->LinkStatus &&
225 st->ctx->Shader.CurrentProgram->VertexProgram) {
226 struct gl_vertex_program *f
227 = st->ctx->Shader.CurrentProgram->VertexProgram;
228 stvp = st_vertex_program(f);
229 }
230 else {
231 assert(st->ctx->VertexProgram._Current);
232 stvp = st_vertex_program(st->ctx->VertexProgram._Current);
233 }
234
235 if (st->vp != stvp || stvp->dirty) {
236 if (stvp->dirty)
237 (void) st_translate_vertex_shader( st, stvp );
238
239 /* Bind the vertex program and TGSI shader */
240 st->vp = stvp;
241 st->state.vs = stvp->vs;
242
243 #if 0
244 printf("###### bind vp tokens: %p %p num_inp=%u\n",
245 stvp, stvp->tokens, stvp->vs->state.num_inputs);
246 if (TGSI_DEBUG)
247 tgsi_dump( stvp->tokens, 0 );
248 #endif
249 st->pipe->bind_vs_state(st->pipe, st->state.vs->data);
250 }
251 }
252
253
254 const struct st_tracked_state st_update_vs = {
255 .name = "st_update_vs",
256 .dirty = {
257 .mesa = _NEW_PROGRAM,
258 .st = ST_NEW_VERTEX_PROGRAM,
259 },
260 .update = update_vs
261 };
262
263
264
265