draw/llvmpipe: fix transform feedback position + enable other extensions
[mesa.git] / src / gallium / auxiliary / draw / draw_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 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36
37 #include "pipe/p_shader_tokens.h"
38
39 #include "draw_private.h"
40 #include "draw_context.h"
41 #include "draw_vs.h"
42
43 #include "translate/translate.h"
44 #include "translate/translate_cache.h"
45
46 #include "tgsi/tgsi_dump.h"
47 #include "tgsi/tgsi_exec.h"
48
49 DEBUG_GET_ONCE_BOOL_OPTION(gallium_dump_vs, "GALLIUM_DUMP_VS", FALSE)
50
51
52 void draw_vs_set_viewport( struct draw_context *draw,
53 const struct pipe_viewport_state *viewport )
54 {
55 }
56
57
58
59 struct draw_vertex_shader *
60 draw_create_vertex_shader(struct draw_context *draw,
61 const struct pipe_shader_state *shader)
62 {
63 struct draw_vertex_shader *vs = NULL;
64
65 if (draw->dump_vs) {
66 tgsi_dump(shader->tokens, 0);
67 }
68
69 #if HAVE_LLVM
70 if (draw->pt.middle.llvm) {
71 vs = draw_create_vs_llvm(draw, shader);
72 }
73 #endif
74
75 if (!vs) {
76 vs = draw_create_vs_exec( draw, shader );
77 }
78
79 if (vs)
80 {
81 uint i;
82 bool found_clipvertex = FALSE;
83 vs->position_output = -1;
84 for (i = 0; i < vs->info.num_outputs; i++) {
85 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
86 vs->info.output_semantic_index[i] == 0)
87 vs->position_output = i;
88 else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
89 vs->info.output_semantic_index[i] == 0)
90 vs->edgeflag_output = i;
91 else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPVERTEX &&
92 vs->info.output_semantic_index[i] == 0) {
93 found_clipvertex = TRUE;
94 vs->clipvertex_output = i;
95 } else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPDIST) {
96 if (vs->info.output_semantic_index[i] == 0)
97 vs->clipdistance_output[0] = i;
98 else
99 vs->clipdistance_output[1] = i;
100 }
101 }
102 if (!found_clipvertex)
103 vs->clipvertex_output = vs->position_output;
104 }
105
106 assert(vs);
107 return vs;
108 }
109
110
111 void
112 draw_bind_vertex_shader(struct draw_context *draw,
113 struct draw_vertex_shader *dvs)
114 {
115 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
116
117 if (dvs)
118 {
119 draw->vs.vertex_shader = dvs;
120 draw->vs.num_vs_outputs = dvs->info.num_outputs;
121 draw->vs.position_output = dvs->position_output;
122 draw->vs.edgeflag_output = dvs->edgeflag_output;
123 draw->vs.clipvertex_output = dvs->clipvertex_output;
124 draw->vs.clipdistance_output[0] = dvs->clipdistance_output[0];
125 draw->vs.clipdistance_output[1] = dvs->clipdistance_output[1];
126 dvs->prepare( dvs, draw );
127 }
128 else {
129 draw->vs.vertex_shader = NULL;
130 draw->vs.num_vs_outputs = 0;
131 }
132 }
133
134
135 void
136 draw_delete_vertex_shader(struct draw_context *draw,
137 struct draw_vertex_shader *dvs)
138 {
139 unsigned i;
140
141 for (i = 0; i < dvs->nr_variants; i++)
142 dvs->variant[i]->destroy( dvs->variant[i] );
143
144 dvs->nr_variants = 0;
145
146 dvs->delete( dvs );
147 }
148
149
150
151 boolean
152 draw_vs_init( struct draw_context *draw )
153 {
154 draw->dump_vs = debug_get_option_gallium_dump_vs();
155
156 draw->vs.tgsi.machine = tgsi_exec_machine_create();
157 if (!draw->vs.tgsi.machine)
158 return FALSE;
159
160 draw->vs.emit_cache = translate_cache_create();
161 if (!draw->vs.emit_cache)
162 return FALSE;
163
164 draw->vs.fetch_cache = translate_cache_create();
165 if (!draw->vs.fetch_cache)
166 return FALSE;
167
168 return TRUE;
169 }
170
171 void
172 draw_vs_destroy( struct draw_context *draw )
173 {
174 if (draw->vs.fetch_cache)
175 translate_cache_destroy(draw->vs.fetch_cache);
176
177 if (draw->vs.emit_cache)
178 translate_cache_destroy(draw->vs.emit_cache);
179
180 tgsi_exec_machine_destroy(draw->vs.tgsi.machine);
181 }
182
183
184 struct draw_vs_variant *
185 draw_vs_lookup_variant( struct draw_vertex_shader *vs,
186 const struct draw_vs_variant_key *key )
187 {
188 struct draw_vs_variant *variant;
189 unsigned i;
190
191 /* Lookup existing variant:
192 */
193 for (i = 0; i < vs->nr_variants; i++)
194 if (draw_vs_variant_key_compare(key, &vs->variant[i]->key) == 0)
195 return vs->variant[i];
196
197 /* Else have to create a new one:
198 */
199 variant = vs->create_variant( vs, key );
200 if (variant == NULL)
201 return NULL;
202
203 /* Add it to our list, could be smarter:
204 */
205 if (vs->nr_variants < Elements(vs->variant)) {
206 vs->variant[vs->nr_variants++] = variant;
207 }
208 else {
209 vs->last_variant++;
210 vs->last_variant %= Elements(vs->variant);
211 vs->variant[vs->last_variant]->destroy(vs->variant[vs->last_variant]);
212 vs->variant[vs->last_variant] = variant;
213 }
214
215 /* Done
216 */
217 return variant;
218 }
219
220
221 struct translate *
222 draw_vs_get_fetch( struct draw_context *draw,
223 struct translate_key *key )
224 {
225 if (!draw->vs.fetch ||
226 translate_key_compare(&draw->vs.fetch->key, key) != 0)
227 {
228 translate_key_sanitize(key);
229 draw->vs.fetch = translate_cache_find(draw->vs.fetch_cache, key);
230 }
231
232 return draw->vs.fetch;
233 }
234
235 struct translate *
236 draw_vs_get_emit( struct draw_context *draw,
237 struct translate_key *key )
238 {
239 if (!draw->vs.emit ||
240 translate_key_compare(&draw->vs.emit->key, key) != 0)
241 {
242 translate_key_sanitize(key);
243 draw->vs.emit = translate_cache_find(draw->vs.emit_cache, key);
244 }
245
246 return draw->vs.emit;
247 }