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