1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "shader/prog_parameter.h"
34 #include "shader/prog_print.h"
35 #include "tnl/t_vp_build.h"
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"
43 #include "st_context.h"
46 #include "st_program.h"
53 * Translate a Mesa vertex shader into a TGSI shader.
54 * \return pointer to cached pipe_shader object.
56 const struct cso_vertex_shader
*
57 st_translate_vertex_shader(struct st_context
*st
,
58 struct st_vertex_program
*stvp
)
60 struct pipe_shader_state vs
;
61 const struct cso_vertex_shader
*cso
;
64 memset(&vs
, 0, sizeof(vs
));
67 * Determine number of inputs, the mappings between VERT_ATTRIB_x
68 * and TGSI generic input indexes, plus input attrib semantic info.
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
;
76 vs
.input_semantic_name
[vs
.num_inputs
] = TGSI_SEMANTIC_POSITION
;
77 vs
.input_semantic_index
[vs
.num_inputs
] = 0;
79 case VERT_ATTRIB_WEIGHT
:
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;
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;
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;
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
;
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
;
125 * Determine number of outputs, the register mapping and
126 * the semantic information for each vertex output/result.
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
;
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;
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;
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;
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;
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;
154 case VERT_RESULT_FOGC
:
155 case VERT_RESULT_PSIZ
:
156 case VERT_RESULT_EDGE
:
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
;
170 case VERT_RESULT_VAR0
:
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
;
183 /* XXX: fix static allocation of tokens:
185 tgsi_mesa_compile_vp_program( &stvp
->Base
,
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
);
193 vs
.tokens
= &stvp
->tokens
[0];
195 cso
= st_cached_vs_state(st
, &vs
);
199 tgsi_dump( stvp
->tokens
, 0 );
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
);
205 if (!cso
->state
.executable
)
206 cso
->state
.executable
= (void *) x86_get_func( &stvp
->sse2_program
);
216 static void update_vs( struct st_context
*st
)
218 struct st_vertex_program
*stvp
;
220 /* find active shader and params -- Should be covered by
221 * ST_NEW_VERTEX_PROGRAM
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
);
231 assert(st
->ctx
->VertexProgram
._Current
);
232 stvp
= st_vertex_program(st
->ctx
->VertexProgram
._Current
);
235 if (st
->vp
!= stvp
|| stvp
->dirty
) {
237 (void) st_translate_vertex_shader( st
, stvp
);
239 /* Bind the vertex program and TGSI shader */
241 st
->state
.vs
= stvp
->vs
;
244 printf("###### bind vp tokens: %p %p num_inp=%u\n",
245 stvp
, stvp
->tokens
, stvp
->vs
->state
.num_inputs
);
247 tgsi_dump( stvp
->tokens
, 0 );
249 st
->pipe
->bind_vs_state(st
->pipe
, st
->state
.vs
->data
);
254 const struct st_tracked_state st_update_vs
= {
255 .name
= "st_update_vs",
257 .mesa
= _NEW_PROGRAM
,
258 .st
= ST_NEW_VERTEX_PROGRAM
,