draw: create specialized vs varients incorporating fetch & emit
[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 "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 struct draw_vertex_shader *
45 draw_create_vertex_shader(struct draw_context *draw,
46 const struct pipe_shader_state *shader)
47 {
48 struct draw_vertex_shader *vs;
49
50 vs = draw_create_vs_llvm( draw, shader );
51 if (!vs) {
52 vs = draw_create_vs_sse( draw, shader );
53 if (!vs) {
54 vs = draw_create_vs_exec( draw, shader );
55 }
56 }
57
58 assert(vs);
59 return vs;
60 }
61
62
63 void
64 draw_bind_vertex_shader(struct draw_context *draw,
65 struct draw_vertex_shader *dvs)
66 {
67 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
68
69 if (dvs)
70 {
71 draw->vs.vertex_shader = dvs;
72 draw->vs.num_vs_outputs = dvs->info.num_outputs;
73 dvs->prepare( dvs, draw );
74 }
75 else {
76 draw->vs.vertex_shader = NULL;
77 draw->vs.num_vs_outputs = 0;
78 }
79 }
80
81
82 void
83 draw_delete_vertex_shader(struct draw_context *draw,
84 struct draw_vertex_shader *dvs)
85 {
86 dvs->delete( dvs );
87 }
88
89
90
91 boolean
92 draw_vs_init( struct draw_context *draw )
93 {
94 tgsi_exec_machine_init(&draw->vs.machine);
95
96 /* FIXME: give this machine thing a proper constructor:
97 */
98 draw->vs.machine.Inputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16);
99 if (!draw->vs.machine.Inputs)
100 return FALSE;
101
102 draw->vs.machine.Outputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16);
103 if (!draw->vs.machine.Outputs)
104 return FALSE;
105
106 draw->vs.emit_cache = translate_cache_create();
107 if (!draw->vs.emit_cache)
108 return FALSE;
109
110 draw->vs.fetch_cache = translate_cache_create();
111 if (!draw->vs.fetch_cache)
112 return FALSE;
113
114 return TRUE;
115 }
116
117 void
118 draw_vs_destroy( struct draw_context *draw )
119 {
120 if (draw->vs.machine.Inputs)
121 align_free(draw->vs.machine.Inputs);
122
123 if (draw->vs.machine.Outputs)
124 align_free(draw->vs.machine.Outputs);
125
126 if (draw->vs.fetch_cache)
127 translate_cache_destroy(draw->vs.fetch_cache);
128
129 if (draw->vs.emit_cache)
130 translate_cache_destroy(draw->vs.emit_cache);
131
132 tgsi_exec_machine_free_data(&draw->vs.machine);
133
134 }
135
136
137 struct draw_vs_varient *
138 draw_vs_lookup_varient( struct draw_vertex_shader *vs,
139 const struct draw_vs_varient_key *key )
140 {
141 struct draw_vs_varient *varient;
142 unsigned i;
143
144 /* Lookup existing varient:
145 */
146 for (i = 0; i < vs->nr_varients; i++)
147 if (draw_vs_varient_key_compare(key, &vs->varient[i]->key) == 0)
148 return vs->varient[i];
149
150 /* Else have to create a new one:
151 */
152 varient = vs->create_varient( vs, key );
153 if (varient == NULL)
154 return NULL;
155
156 /* Add it to our list:
157 */
158 assert(vs->nr_varients < Elements(vs->varient));
159 vs->varient[vs->nr_varients++] = varient;
160
161 /* Done
162 */
163 return varient;
164 }
165
166
167 struct translate *
168 draw_vs_get_fetch( struct draw_context *draw,
169 struct translate_key *key )
170 {
171 if (!draw->vs.fetch ||
172 translate_key_compare(&draw->vs.fetch->key, key) != 0)
173 {
174 translate_key_sanitize(key);
175 draw->vs.fetch = translate_cache_find(draw->vs.fetch_cache, key);
176 }
177
178 return draw->vs.fetch;
179 }
180
181 struct translate *
182 draw_vs_get_emit( struct draw_context *draw,
183 struct translate_key *key )
184 {
185 if (!draw->vs.emit ||
186 translate_key_compare(&draw->vs.emit->key, key) != 0)
187 {
188 translate_key_sanitize(key);
189 draw->vs.emit = translate_cache_find(draw->vs.emit_cache, key);
190 }
191
192 return draw->vs.emit;
193 }