Merge remote branch 'origin/master' into pipe-video
[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 draw_vs_aos_machine_constants(draw->vs.aos_machine, slot, constants);
85 }
86
87
88 void draw_vs_set_viewport( struct draw_context *draw,
89 const struct pipe_viewport_state *viewport )
90 {
91 draw_vs_aos_machine_viewport( draw->vs.aos_machine, viewport );
92 }
93
94
95
96 struct draw_vertex_shader *
97 draw_create_vertex_shader(struct draw_context *draw,
98 const struct pipe_shader_state *shader)
99 {
100 struct draw_vertex_shader *vs = NULL;
101
102 if (draw->dump_vs) {
103 tgsi_dump(shader->tokens, 0);
104 }
105
106 if (!draw->pt.middle.llvm) {
107 #if defined(PIPE_ARCH_X86)
108 vs = draw_create_vs_sse( draw, shader );
109 #elif defined(PIPE_ARCH_PPC)
110 vs = draw_create_vs_ppc( draw, shader );
111 #endif
112 }
113 #if HAVE_LLVM
114 else {
115 vs = draw_create_vs_llvm(draw, shader);
116 }
117 #endif
118
119 if (!vs) {
120 vs = draw_create_vs_exec( draw, shader );
121 }
122
123 if (vs)
124 {
125 uint i;
126 for (i = 0; i < vs->info.num_outputs; i++) {
127 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
128 vs->info.output_semantic_index[i] == 0)
129 vs->position_output = i;
130 else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
131 vs->info.output_semantic_index[i] == 0)
132 vs->edgeflag_output = i;
133 }
134 }
135
136 assert(vs);
137 return vs;
138 }
139
140
141 void
142 draw_bind_vertex_shader(struct draw_context *draw,
143 struct draw_vertex_shader *dvs)
144 {
145 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
146
147 if (dvs)
148 {
149 draw->vs.vertex_shader = dvs;
150 draw->vs.num_vs_outputs = dvs->info.num_outputs;
151 draw->vs.position_output = dvs->position_output;
152 draw->vs.edgeflag_output = dvs->edgeflag_output;
153 dvs->prepare( dvs, draw );
154 }
155 else {
156 draw->vs.vertex_shader = NULL;
157 draw->vs.num_vs_outputs = 0;
158 }
159 }
160
161
162 void
163 draw_delete_vertex_shader(struct draw_context *draw,
164 struct draw_vertex_shader *dvs)
165 {
166 unsigned i;
167
168 for (i = 0; i < dvs->nr_variants; i++)
169 dvs->variant[i]->destroy( dvs->variant[i] );
170
171 dvs->nr_variants = 0;
172
173 dvs->delete( dvs );
174 }
175
176
177
178 boolean
179 draw_vs_init( struct draw_context *draw )
180 {
181 draw->dump_vs = debug_get_option_gallium_dump_vs();
182
183 draw->vs.machine = tgsi_exec_machine_create();
184 if (!draw->vs.machine)
185 return FALSE;
186
187 draw->vs.emit_cache = translate_cache_create();
188 if (!draw->vs.emit_cache)
189 return FALSE;
190
191 draw->vs.fetch_cache = translate_cache_create();
192 if (!draw->vs.fetch_cache)
193 return FALSE;
194
195 draw->vs.aos_machine = draw_vs_aos_machine();
196 #ifdef PIPE_ARCH_X86
197 if (!draw->vs.aos_machine)
198 return FALSE;
199 #endif
200
201 return TRUE;
202 }
203
204 void
205 draw_vs_destroy( struct draw_context *draw )
206 {
207 uint i;
208
209 if (draw->vs.fetch_cache)
210 translate_cache_destroy(draw->vs.fetch_cache);
211
212 if (draw->vs.emit_cache)
213 translate_cache_destroy(draw->vs.emit_cache);
214
215 if (draw->vs.aos_machine)
216 draw_vs_aos_machine_destroy(draw->vs.aos_machine);
217
218 for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
219 if (draw->vs.aligned_constant_storage[i]) {
220 align_free((void *)draw->vs.aligned_constant_storage[i]);
221 }
222 }
223
224 tgsi_exec_machine_destroy(draw->vs.machine);
225 }
226
227
228 struct draw_vs_variant *
229 draw_vs_lookup_variant( struct draw_vertex_shader *vs,
230 const struct draw_vs_variant_key *key )
231 {
232 struct draw_vs_variant *variant;
233 unsigned i;
234
235 /* Lookup existing variant:
236 */
237 for (i = 0; i < vs->nr_variants; i++)
238 if (draw_vs_variant_key_compare(key, &vs->variant[i]->key) == 0)
239 return vs->variant[i];
240
241 /* Else have to create a new one:
242 */
243 variant = vs->create_variant( vs, key );
244 if (variant == NULL)
245 return NULL;
246
247 /* Add it to our list, could be smarter:
248 */
249 if (vs->nr_variants < Elements(vs->variant)) {
250 vs->variant[vs->nr_variants++] = variant;
251 }
252 else {
253 vs->last_variant++;
254 vs->last_variant %= Elements(vs->variant);
255 vs->variant[vs->last_variant]->destroy(vs->variant[vs->last_variant]);
256 vs->variant[vs->last_variant] = variant;
257 }
258
259 /* Done
260 */
261 return variant;
262 }
263
264
265 struct translate *
266 draw_vs_get_fetch( struct draw_context *draw,
267 struct translate_key *key )
268 {
269 if (!draw->vs.fetch ||
270 translate_key_compare(&draw->vs.fetch->key, key) != 0)
271 {
272 translate_key_sanitize(key);
273 draw->vs.fetch = translate_cache_find(draw->vs.fetch_cache, key);
274 }
275
276 return draw->vs.fetch;
277 }
278
279 struct translate *
280 draw_vs_get_emit( struct draw_context *draw,
281 struct translate_key *key )
282 {
283 if (!draw->vs.emit ||
284 translate_key_compare(&draw->vs.emit->key, key) != 0)
285 {
286 translate_key_sanitize(key);
287 draw->vs.emit = translate_cache_find(draw->vs.emit_cache, key);
288 }
289
290 return draw->vs.emit;
291 }