draw: limit the number of vertex shader variants kept around
[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 void
52 draw_vs_set_constants(struct draw_context *draw,
53 unsigned slot,
54 const void *constants,
55 unsigned size)
56 {
57 if (((uintptr_t)constants) & 0xf) {
58 if (size > draw->vs.const_storage_size[slot]) {
59 if (draw->vs.aligned_constant_storage[slot]) {
60 align_free((void *)draw->vs.aligned_constant_storage[slot]);
61 }
62 draw->vs.aligned_constant_storage[slot] = align_malloc(size, 16);
63 }
64 assert(constants);
65 memcpy((void *)draw->vs.aligned_constant_storage[slot],
66 constants,
67 size);
68 constants = draw->vs.aligned_constant_storage[slot];
69 }
70
71 draw->vs.aligned_constants[slot] = constants;
72 draw_vs_aos_machine_constants(draw->vs.aos_machine, slot, constants);
73 }
74
75
76 void draw_vs_set_viewport( struct draw_context *draw,
77 const struct pipe_viewport_state *viewport )
78 {
79 draw_vs_aos_machine_viewport( draw->vs.aos_machine, viewport );
80 }
81
82
83
84 struct draw_vertex_shader *
85 draw_create_vertex_shader(struct draw_context *draw,
86 const struct pipe_shader_state *shader)
87 {
88 struct draw_vertex_shader *vs = NULL;
89
90 if (draw->dump_vs) {
91 tgsi_dump(shader->tokens, 0);
92 }
93
94 if (!draw->pt.middle.llvm) {
95 #if defined(PIPE_ARCH_X86)
96 vs = draw_create_vs_sse( draw, shader );
97 #elif defined(PIPE_ARCH_PPC)
98 vs = draw_create_vs_ppc( draw, shader );
99 #endif
100 }
101 #if HAVE_LLVM
102 else {
103 vs = draw_create_vs_llvm(draw, shader);
104 }
105 #endif
106
107 if (!vs) {
108 vs = draw_create_vs_exec( draw, shader );
109 }
110
111 if (vs)
112 {
113 uint i;
114 for (i = 0; i < vs->info.num_outputs; i++) {
115 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
116 vs->info.output_semantic_index[i] == 0)
117 vs->position_output = i;
118 else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
119 vs->info.output_semantic_index[i] == 0)
120 vs->edgeflag_output = i;
121 }
122 }
123
124 assert(vs);
125 return vs;
126 }
127
128
129 void
130 draw_bind_vertex_shader(struct draw_context *draw,
131 struct draw_vertex_shader *dvs)
132 {
133 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
134
135 if (dvs)
136 {
137 draw->vs.vertex_shader = dvs;
138 draw->vs.num_vs_outputs = dvs->info.num_outputs;
139 draw->vs.position_output = dvs->position_output;
140 draw->vs.edgeflag_output = dvs->edgeflag_output;
141 dvs->prepare( dvs, draw );
142 }
143 else {
144 draw->vs.vertex_shader = NULL;
145 draw->vs.num_vs_outputs = 0;
146 }
147 }
148
149
150 void
151 draw_delete_vertex_shader(struct draw_context *draw,
152 struct draw_vertex_shader *dvs)
153 {
154 unsigned i;
155
156 for (i = 0; i < dvs->nr_varients; i++)
157 dvs->varient[i]->destroy( dvs->varient[i] );
158
159 dvs->nr_varients = 0;
160
161 dvs->delete( dvs );
162 }
163
164
165
166 boolean
167 draw_vs_init( struct draw_context *draw )
168 {
169 draw->dump_vs = debug_get_option_gallium_dump_vs();
170
171 draw->vs.machine = tgsi_exec_machine_create();
172 if (!draw->vs.machine)
173 return FALSE;
174
175 draw->vs.emit_cache = translate_cache_create();
176 if (!draw->vs.emit_cache)
177 return FALSE;
178
179 draw->vs.fetch_cache = translate_cache_create();
180 if (!draw->vs.fetch_cache)
181 return FALSE;
182
183 draw->vs.aos_machine = draw_vs_aos_machine();
184 #ifdef PIPE_ARCH_X86
185 if (!draw->vs.aos_machine)
186 return FALSE;
187 #endif
188
189 return TRUE;
190 }
191
192 void
193 draw_vs_destroy( struct draw_context *draw )
194 {
195 uint i;
196
197 if (draw->vs.fetch_cache)
198 translate_cache_destroy(draw->vs.fetch_cache);
199
200 if (draw->vs.emit_cache)
201 translate_cache_destroy(draw->vs.emit_cache);
202
203 if (draw->vs.aos_machine)
204 draw_vs_aos_machine_destroy(draw->vs.aos_machine);
205
206 for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
207 if (draw->vs.aligned_constant_storage[i]) {
208 align_free((void *)draw->vs.aligned_constant_storage[i]);
209 }
210 }
211
212 tgsi_exec_machine_destroy(draw->vs.machine);
213 }
214
215
216 struct draw_vs_varient *
217 draw_vs_lookup_varient( struct draw_vertex_shader *vs,
218 const struct draw_vs_varient_key *key )
219 {
220 struct draw_vs_varient *varient;
221 unsigned i;
222
223 /* Lookup existing varient:
224 */
225 for (i = 0; i < vs->nr_varients; i++)
226 if (draw_vs_varient_key_compare(key, &vs->varient[i]->key) == 0)
227 return vs->varient[i];
228
229 /* Else have to create a new one:
230 */
231 varient = vs->create_varient( vs, key );
232 if (varient == NULL)
233 return NULL;
234
235 /* Add it to our list, could be smarter:
236 */
237 if (vs->nr_varients < Elements(vs->varient)) {
238 vs->varient[vs->nr_varients++] = varient;
239 }
240 else {
241 vs->last_varient++;
242 vs->last_varient %= Elements(vs->varient);
243 vs->varient[vs->last_varient]->destroy(vs->varient[vs->last_varient]);
244 vs->varient[vs->last_varient] = varient;
245 }
246
247 /* Done
248 */
249 return varient;
250 }
251
252
253 struct translate *
254 draw_vs_get_fetch( struct draw_context *draw,
255 struct translate_key *key )
256 {
257 if (!draw->vs.fetch ||
258 translate_key_compare(&draw->vs.fetch->key, key) != 0)
259 {
260 translate_key_sanitize(key);
261 draw->vs.fetch = translate_cache_find(draw->vs.fetch_cache, key);
262 }
263
264 return draw->vs.fetch;
265 }
266
267 struct translate *
268 draw_vs_get_emit( struct draw_context *draw,
269 struct translate_key *key )
270 {
271 if (!draw->vs.emit ||
272 translate_key_compare(&draw->vs.emit->key, key) != 0)
273 {
274 translate_key_sanitize(key);
275 draw->vs.emit = translate_cache_find(draw->vs.emit_cache, key);
276 }
277
278 return draw->vs.emit;
279 }