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