draw: implement vertex color clamping, and disable SSE and PPC paths
[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 0
108 /* these paths don't support vertex clamping
109 * TODO: either add it, or remove them completely
110 * use LLVM instead if you want performance
111 * use exec instead if you want debugging/more correctness
112 */
113 #if defined(PIPE_ARCH_X86)
114 vs = draw_create_vs_sse( draw, shader );
115 #elif defined(PIPE_ARCH_PPC)
116 vs = draw_create_vs_ppc( draw, shader );
117 #endif
118 #endif
119 }
120 #if HAVE_LLVM
121 else {
122 vs = draw_create_vs_llvm(draw, shader);
123 }
124 #endif
125
126 if (!vs) {
127 vs = draw_create_vs_exec( draw, shader );
128 }
129
130 if (vs)
131 {
132 uint i;
133 for (i = 0; i < vs->info.num_outputs; i++) {
134 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
135 vs->info.output_semantic_index[i] == 0)
136 vs->position_output = i;
137 else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
138 vs->info.output_semantic_index[i] == 0)
139 vs->edgeflag_output = i;
140 }
141 }
142
143 assert(vs);
144 return vs;
145 }
146
147
148 void
149 draw_bind_vertex_shader(struct draw_context *draw,
150 struct draw_vertex_shader *dvs)
151 {
152 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
153
154 if (dvs)
155 {
156 draw->vs.vertex_shader = dvs;
157 draw->vs.num_vs_outputs = dvs->info.num_outputs;
158 draw->vs.position_output = dvs->position_output;
159 draw->vs.edgeflag_output = dvs->edgeflag_output;
160 dvs->prepare( dvs, draw );
161 }
162 else {
163 draw->vs.vertex_shader = NULL;
164 draw->vs.num_vs_outputs = 0;
165 }
166 }
167
168
169 void
170 draw_delete_vertex_shader(struct draw_context *draw,
171 struct draw_vertex_shader *dvs)
172 {
173 unsigned i;
174
175 for (i = 0; i < dvs->nr_variants; i++)
176 dvs->variant[i]->destroy( dvs->variant[i] );
177
178 dvs->nr_variants = 0;
179
180 dvs->delete( dvs );
181 }
182
183
184
185 boolean
186 draw_vs_init( struct draw_context *draw )
187 {
188 draw->dump_vs = debug_get_option_gallium_dump_vs();
189
190 draw->vs.machine = tgsi_exec_machine_create();
191 if (!draw->vs.machine)
192 return FALSE;
193
194 draw->vs.emit_cache = translate_cache_create();
195 if (!draw->vs.emit_cache)
196 return FALSE;
197
198 draw->vs.fetch_cache = translate_cache_create();
199 if (!draw->vs.fetch_cache)
200 return FALSE;
201
202 draw->vs.aos_machine = draw_vs_aos_machine();
203 #ifdef PIPE_ARCH_X86
204 if (!draw->vs.aos_machine)
205 return FALSE;
206 #endif
207
208 return TRUE;
209 }
210
211 void
212 draw_vs_destroy( struct draw_context *draw )
213 {
214 uint i;
215
216 if (draw->vs.fetch_cache)
217 translate_cache_destroy(draw->vs.fetch_cache);
218
219 if (draw->vs.emit_cache)
220 translate_cache_destroy(draw->vs.emit_cache);
221
222 if (draw->vs.aos_machine)
223 draw_vs_aos_machine_destroy(draw->vs.aos_machine);
224
225 for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
226 if (draw->vs.aligned_constant_storage[i]) {
227 align_free((void *)draw->vs.aligned_constant_storage[i]);
228 }
229 }
230
231 tgsi_exec_machine_destroy(draw->vs.machine);
232 }
233
234
235 struct draw_vs_variant *
236 draw_vs_lookup_variant( struct draw_vertex_shader *vs,
237 const struct draw_vs_variant_key *key )
238 {
239 struct draw_vs_variant *variant;
240 unsigned i;
241
242 /* Lookup existing variant:
243 */
244 for (i = 0; i < vs->nr_variants; i++)
245 if (draw_vs_variant_key_compare(key, &vs->variant[i]->key) == 0)
246 return vs->variant[i];
247
248 /* Else have to create a new one:
249 */
250 variant = vs->create_variant( vs, key );
251 if (variant == NULL)
252 return NULL;
253
254 /* Add it to our list, could be smarter:
255 */
256 if (vs->nr_variants < Elements(vs->variant)) {
257 vs->variant[vs->nr_variants++] = variant;
258 }
259 else {
260 vs->last_variant++;
261 vs->last_variant %= Elements(vs->variant);
262 vs->variant[vs->last_variant]->destroy(vs->variant[vs->last_variant]);
263 vs->variant[vs->last_variant] = variant;
264 }
265
266 /* Done
267 */
268 return variant;
269 }
270
271
272 struct translate *
273 draw_vs_get_fetch( struct draw_context *draw,
274 struct translate_key *key )
275 {
276 if (!draw->vs.fetch ||
277 translate_key_compare(&draw->vs.fetch->key, key) != 0)
278 {
279 translate_key_sanitize(key);
280 draw->vs.fetch = translate_cache_find(draw->vs.fetch_cache, key);
281 }
282
283 return draw->vs.fetch;
284 }
285
286 struct translate *
287 draw_vs_get_emit( struct draw_context *draw,
288 struct translate_key *key )
289 {
290 if (!draw->vs.emit ||
291 translate_key_compare(&draw->vs.emit->key, key) != 0)
292 {
293 translate_key_sanitize(key);
294 draw->vs.emit = translate_cache_find(draw->vs.emit_cache, key);
295 }
296
297 return draw->vs.emit;
298 }