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