draw: implement support for multiple viewports
[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 struct draw_vertex_shader *
53 draw_create_vertex_shader(struct draw_context *draw,
54 const struct pipe_shader_state *shader)
55 {
56 struct draw_vertex_shader *vs = NULL;
57
58 if (draw->dump_vs) {
59 tgsi_dump(shader->tokens, 0);
60 }
61
62 #if HAVE_LLVM
63 if (draw->pt.middle.llvm) {
64 vs = draw_create_vs_llvm(draw, shader);
65 }
66 #endif
67
68 if (!vs) {
69 vs = draw_create_vs_exec( draw, shader );
70 }
71
72 if (vs)
73 {
74 uint i;
75 bool found_clipvertex = FALSE;
76 vs->position_output = -1;
77 for (i = 0; i < vs->info.num_outputs; i++) {
78 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
79 vs->info.output_semantic_index[i] == 0)
80 vs->position_output = i;
81 else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
82 vs->info.output_semantic_index[i] == 0)
83 vs->edgeflag_output = i;
84 else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPVERTEX &&
85 vs->info.output_semantic_index[i] == 0) {
86 found_clipvertex = TRUE;
87 vs->clipvertex_output = i;
88 } else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPDIST) {
89 if (vs->info.output_semantic_index[i] == 0)
90 vs->clipdistance_output[0] = i;
91 else
92 vs->clipdistance_output[1] = i;
93 }
94 }
95 if (!found_clipvertex)
96 vs->clipvertex_output = vs->position_output;
97 }
98
99 assert(vs);
100 return vs;
101 }
102
103
104 void
105 draw_bind_vertex_shader(struct draw_context *draw,
106 struct draw_vertex_shader *dvs)
107 {
108 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
109
110 if (dvs)
111 {
112 draw->vs.vertex_shader = dvs;
113 draw->vs.num_vs_outputs = dvs->info.num_outputs;
114 draw->vs.position_output = dvs->position_output;
115 draw->vs.edgeflag_output = dvs->edgeflag_output;
116 draw->vs.clipvertex_output = dvs->clipvertex_output;
117 draw->vs.clipdistance_output[0] = dvs->clipdistance_output[0];
118 draw->vs.clipdistance_output[1] = dvs->clipdistance_output[1];
119 dvs->prepare( dvs, draw );
120 }
121 else {
122 draw->vs.vertex_shader = NULL;
123 draw->vs.num_vs_outputs = 0;
124 }
125 }
126
127
128 void
129 draw_delete_vertex_shader(struct draw_context *draw,
130 struct draw_vertex_shader *dvs)
131 {
132 unsigned i;
133
134 for (i = 0; i < dvs->nr_variants; i++)
135 dvs->variant[i]->destroy( dvs->variant[i] );
136
137 dvs->nr_variants = 0;
138
139 dvs->delete( dvs );
140 }
141
142
143
144 boolean
145 draw_vs_init( struct draw_context *draw )
146 {
147 draw->dump_vs = debug_get_option_gallium_dump_vs();
148
149 draw->vs.tgsi.machine = tgsi_exec_machine_create();
150 if (!draw->vs.tgsi.machine)
151 return FALSE;
152
153 draw->vs.emit_cache = translate_cache_create();
154 if (!draw->vs.emit_cache)
155 return FALSE;
156
157 draw->vs.fetch_cache = translate_cache_create();
158 if (!draw->vs.fetch_cache)
159 return FALSE;
160
161 return TRUE;
162 }
163
164 void
165 draw_vs_destroy( struct draw_context *draw )
166 {
167 if (draw->vs.fetch_cache)
168 translate_cache_destroy(draw->vs.fetch_cache);
169
170 if (draw->vs.emit_cache)
171 translate_cache_destroy(draw->vs.emit_cache);
172
173 tgsi_exec_machine_destroy(draw->vs.tgsi.machine);
174 }
175
176
177 struct draw_vs_variant *
178 draw_vs_lookup_variant( struct draw_vertex_shader *vs,
179 const struct draw_vs_variant_key *key )
180 {
181 struct draw_vs_variant *variant;
182 unsigned i;
183
184 /* Lookup existing variant:
185 */
186 for (i = 0; i < vs->nr_variants; i++)
187 if (draw_vs_variant_key_compare(key, &vs->variant[i]->key) == 0)
188 return vs->variant[i];
189
190 /* Else have to create a new one:
191 */
192 variant = vs->create_variant( vs, key );
193 if (variant == NULL)
194 return NULL;
195
196 /* Add it to our list, could be smarter:
197 */
198 if (vs->nr_variants < Elements(vs->variant)) {
199 vs->variant[vs->nr_variants++] = variant;
200 }
201 else {
202 vs->last_variant++;
203 vs->last_variant %= Elements(vs->variant);
204 vs->variant[vs->last_variant]->destroy(vs->variant[vs->last_variant]);
205 vs->variant[vs->last_variant] = variant;
206 }
207
208 /* Done
209 */
210 return variant;
211 }
212
213
214 struct translate *
215 draw_vs_get_fetch( struct draw_context *draw,
216 struct translate_key *key )
217 {
218 if (!draw->vs.fetch ||
219 translate_key_compare(&draw->vs.fetch->key, key) != 0)
220 {
221 translate_key_sanitize(key);
222 draw->vs.fetch = translate_cache_find(draw->vs.fetch_cache, key);
223 }
224
225 return draw->vs.fetch;
226 }
227
228 struct translate *
229 draw_vs_get_emit( struct draw_context *draw,
230 struct translate_key *key )
231 {
232 if (!draw->vs.emit ||
233 translate_key_compare(&draw->vs.emit->key, key) != 0)
234 {
235 translate_key_sanitize(key);
236 draw->vs.emit = translate_cache_find(draw->vs.emit_cache, key);
237 }
238
239 return draw->vs.emit;
240 }
241
242 void
243 draw_vs_attach_so(struct draw_vertex_shader *dvs,
244 const struct pipe_stream_output_info *info)
245 {
246 dvs->state.stream_output = *info;
247 }
248
249 void
250 draw_vs_reset_so(struct draw_vertex_shader *dvs)
251 {
252 memset(&dvs->state.stream_output, 0, sizeof(dvs->state.stream_output));
253 }