draw_vs.c revision 62628c4d3d497cbca73fde869c9069fa90e6453e
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 "pipe/p_util.h"
35#include "pipe/p_shader_tokens.h"
36#include "draw_private.h"
37#include "draw_context.h"
38#include "draw_vs.h"
39#include "translate/translate.h"
40#include "translate/translate_cache.h"
41
42
43
44
45void draw_vs_set_constants( struct draw_context *draw,
46                            const float (*constants)[4] )
47{
48   draw_vs_aos_machine_constants( draw->vs.aos_machine, constants );
49}
50
51
52void draw_vs_set_viewport( struct draw_context *draw,
53                           const struct pipe_viewport_state *viewport )
54{
55   draw_vs_aos_machine_viewport( draw->vs.aos_machine, viewport );
56}
57
58
59
60struct draw_vertex_shader *
61draw_create_vertex_shader(struct draw_context *draw,
62                          const struct pipe_shader_state *shader)
63{
64   struct draw_vertex_shader *vs;
65
66   vs = draw_create_vs_llvm( draw, shader );
67   if (!vs) {
68      vs = draw_create_vs_sse( draw, shader );
69      if (!vs) {
70         vs = draw_create_vs_exec( draw, shader );
71      }
72   }
73
74   assert(vs);
75   return vs;
76}
77
78
79void
80draw_bind_vertex_shader(struct draw_context *draw,
81                        struct draw_vertex_shader *dvs)
82{
83   draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
84
85   if (dvs)
86   {
87      draw->vs.vertex_shader = dvs;
88      draw->vs.num_vs_outputs = dvs->info.num_outputs;
89      dvs->prepare( dvs, draw );
90   }
91   else {
92      draw->vs.vertex_shader = NULL;
93      draw->vs.num_vs_outputs = 0;
94   }
95}
96
97
98void
99draw_delete_vertex_shader(struct draw_context *draw,
100                          struct draw_vertex_shader *dvs)
101{
102   unsigned i;
103
104   for (i = 0; i < dvs->nr_varients; i++)
105      dvs->varient[i]->destroy( dvs->varient[i] );
106
107   dvs->nr_varients = 0;
108
109   dvs->delete( dvs );
110}
111
112
113
114boolean
115draw_vs_init( struct draw_context *draw )
116{
117   tgsi_exec_machine_init(&draw->vs.machine);
118
119   /* FIXME: give this machine thing a proper constructor:
120    */
121   draw->vs.machine.Inputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16);
122   if (!draw->vs.machine.Inputs)
123      return FALSE;
124
125   draw->vs.machine.Outputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16);
126   if (!draw->vs.machine.Outputs)
127      return FALSE;
128
129   draw->vs.emit_cache = translate_cache_create();
130   if (!draw->vs.emit_cache)
131      return FALSE;
132
133   draw->vs.fetch_cache = translate_cache_create();
134   if (!draw->vs.fetch_cache)
135      return FALSE;
136
137   draw->vs.aos_machine = draw_vs_aos_machine();
138   if (!draw->vs.aos_machine)
139      return FALSE;
140
141   return TRUE;
142}
143
144void
145draw_vs_destroy( struct draw_context *draw )
146{
147   if (draw->vs.machine.Inputs)
148      align_free(draw->vs.machine.Inputs);
149
150   if (draw->vs.machine.Outputs)
151      align_free(draw->vs.machine.Outputs);
152
153   if (draw->vs.fetch_cache)
154      translate_cache_destroy(draw->vs.fetch_cache);
155
156   if (draw->vs.emit_cache)
157      translate_cache_destroy(draw->vs.emit_cache);
158
159   if (draw->vs.aos_machine)
160      draw_vs_aos_machine_destroy(draw->vs.aos_machine);
161
162   tgsi_exec_machine_free_data(&draw->vs.machine);
163
164}
165
166
167struct draw_vs_varient *
168draw_vs_lookup_varient( struct draw_vertex_shader *vs,
169                        const struct draw_vs_varient_key *key )
170{
171   struct draw_vs_varient *varient;
172   unsigned i;
173
174   /* Lookup existing varient:
175    */
176   for (i = 0; i < vs->nr_varients; i++)
177      if (draw_vs_varient_key_compare(key, &vs->varient[i]->key) == 0)
178         return vs->varient[i];
179
180   /* Else have to create a new one:
181    */
182   varient = vs->create_varient( vs, key );
183   if (varient == NULL)
184      return NULL;
185
186   /* Add it to our list, could be smarter:
187    */
188   if (vs->nr_varients < Elements(vs->varient)) {
189      vs->varient[vs->nr_varients++] = varient;
190   }
191   else {
192      vs->last_varient++;
193      vs->last_varient %= Elements(vs->varient);
194      vs->varient[vs->last_varient]->destroy(vs->varient[vs->last_varient]);
195      vs->varient[vs->last_varient] = varient;
196   }
197
198   /* Done
199    */
200   return varient;
201}
202
203
204struct translate *
205draw_vs_get_fetch( struct draw_context *draw,
206                   struct translate_key *key )
207{
208   if (!draw->vs.fetch ||
209       translate_key_compare(&draw->vs.fetch->key, key) != 0)
210   {
211      translate_key_sanitize(key);
212      draw->vs.fetch = translate_cache_find(draw->vs.fetch_cache, key);
213   }
214
215   return draw->vs.fetch;
216}
217
218struct translate *
219draw_vs_get_emit( struct draw_context *draw,
220                  struct translate_key *key )
221{
222   if (!draw->vs.emit ||
223       translate_key_compare(&draw->vs.emit->key, key) != 0)
224   {
225      translate_key_sanitize(key);
226      draw->vs.emit = translate_cache_find(draw->vs.emit_cache, key);
227   }
228
229   return draw->vs.emit;
230}
231