draw_vs.c revision 6536cdf1836a302d334f7380d1723e8f0bc396be
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
50
51void
52draw_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
76void 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
84struct draw_vertex_shader *
85draw_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_llvm( draw, shader );
95   if (!vs) {
96      vs = draw_create_vs_sse( draw, shader );
97      if (!vs) {
98         vs = draw_create_vs_ppc( draw, shader );
99         if (!vs) {
100            vs = draw_create_vs_exec( draw, shader );
101         }
102      }
103   }
104
105   if (vs)
106   {
107      uint i;
108      for (i = 0; i < vs->info.num_outputs; i++) {
109         if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
110             vs->info.output_semantic_index[i] == 0)
111            vs->position_output = i;
112         else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
113             vs->info.output_semantic_index[i] == 0)
114            vs->edgeflag_output = i;
115      }
116   }
117
118   assert(vs);
119   return vs;
120}
121
122
123void
124draw_bind_vertex_shader(struct draw_context *draw,
125                        struct draw_vertex_shader *dvs)
126{
127   draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
128
129   if (dvs)
130   {
131      draw->vs.vertex_shader = dvs;
132      draw->vs.num_vs_outputs = dvs->info.num_outputs;
133      draw->vs.position_output = dvs->position_output;
134      draw->vs.edgeflag_output = dvs->edgeflag_output;
135      dvs->prepare( dvs, draw );
136   }
137   else {
138      draw->vs.vertex_shader = NULL;
139      draw->vs.num_vs_outputs = 0;
140   }
141}
142
143
144void
145draw_delete_vertex_shader(struct draw_context *draw,
146                          struct draw_vertex_shader *dvs)
147{
148   unsigned i;
149
150   for (i = 0; i < dvs->nr_varients; i++)
151      dvs->varient[i]->destroy( dvs->varient[i] );
152
153   dvs->nr_varients = 0;
154
155   dvs->delete( dvs );
156}
157
158
159
160boolean
161draw_vs_init( struct draw_context *draw )
162{
163   draw->dump_vs = debug_get_bool_option("GALLIUM_DUMP_VS", FALSE);
164
165   draw->vs.machine = tgsi_exec_machine_create();
166   if (!draw->vs.machine)
167      return FALSE;
168
169   draw->vs.emit_cache = translate_cache_create();
170   if (!draw->vs.emit_cache)
171      return FALSE;
172
173   draw->vs.fetch_cache = translate_cache_create();
174   if (!draw->vs.fetch_cache)
175      return FALSE;
176
177   draw->vs.aos_machine = draw_vs_aos_machine();
178#ifdef PIPE_ARCH_X86
179   if (!draw->vs.aos_machine)
180      return FALSE;
181#endif
182
183   return TRUE;
184}
185
186void
187draw_vs_destroy( struct draw_context *draw )
188{
189   uint i;
190
191   if (draw->vs.fetch_cache)
192      translate_cache_destroy(draw->vs.fetch_cache);
193
194   if (draw->vs.emit_cache)
195      translate_cache_destroy(draw->vs.emit_cache);
196
197   if (draw->vs.aos_machine)
198      draw_vs_aos_machine_destroy(draw->vs.aos_machine);
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
210struct draw_vs_varient *
211draw_vs_lookup_varient( struct draw_vertex_shader *vs,
212                        const struct draw_vs_varient_key *key )
213{
214   struct draw_vs_varient *varient;
215   unsigned i;
216
217   /* Lookup existing varient:
218    */
219   for (i = 0; i < vs->nr_varients; i++)
220      if (draw_vs_varient_key_compare(key, &vs->varient[i]->key) == 0)
221         return vs->varient[i];
222
223   /* Else have to create a new one:
224    */
225   varient = vs->create_varient( vs, key );
226   if (varient == NULL)
227      return NULL;
228
229   /* Add it to our list, could be smarter:
230    */
231   if (vs->nr_varients < Elements(vs->varient)) {
232      vs->varient[vs->nr_varients++] = varient;
233   }
234   else {
235      vs->last_varient++;
236      vs->last_varient %= Elements(vs->varient);
237      vs->varient[vs->last_varient]->destroy(vs->varient[vs->last_varient]);
238      vs->varient[vs->last_varient] = varient;
239   }
240
241   /* Done
242    */
243   return varient;
244}
245
246
247struct translate *
248draw_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
261struct translate *
262draw_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}
274