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