Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / gallium / drivers / svga / svga_state_vs.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "pipe/p_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29 #include "translate/translate.h"
30
31 #include "svga_context.h"
32 #include "svga_state.h"
33 #include "svga_cmd.h"
34 #include "svga_tgsi.h"
35
36 #include "svga_hw_reg.h"
37
38 /***********************************************************************
39 */
40
41
42 static INLINE int compare_vs_keys( const struct svga_vs_compile_key *a,
43 const struct svga_vs_compile_key *b )
44 {
45 unsigned keysize = svga_vs_key_size( a );
46 return memcmp( a, b, keysize );
47 }
48
49
50 static struct svga_shader_result *search_vs_key( struct svga_vertex_shader *vs,
51 const struct svga_vs_compile_key *key )
52 {
53 struct svga_shader_result *result = vs->base.results;
54
55 assert(key);
56
57 for ( ; result; result = result->next) {
58 if (compare_vs_keys( key, &result->key.vkey ) == 0)
59 return result;
60 }
61
62 return NULL;
63 }
64
65
66 static enum pipe_error compile_vs( struct svga_context *svga,
67 struct svga_vertex_shader *vs,
68 const struct svga_vs_compile_key *key,
69 struct svga_shader_result **out_result )
70 {
71 struct svga_shader_result *result;
72 enum pipe_error ret = PIPE_OK;
73
74 result = svga_translate_vertex_program( vs, key );
75 if (result == NULL) {
76 ret = PIPE_ERROR_OUT_OF_MEMORY;
77 goto fail;
78 }
79
80 ret = SVGA3D_DefineShader(svga->swc,
81 svga->state.next_vs_id,
82 SVGA3D_SHADERTYPE_VS,
83 result->tokens,
84 result->nr_tokens * sizeof result->tokens[0]);
85 if (ret)
86 goto fail;
87
88 *out_result = result;
89 result->id = svga->state.next_vs_id++;
90 result->next = vs->base.results;
91 vs->base.results = result;
92 return PIPE_OK;
93
94 fail:
95 if (result)
96 svga_destroy_shader_result( result );
97 return ret;
98 }
99
100 /* SVGA_NEW_PRESCALE, SVGA_NEW_RAST, SVGA_NEW_ZERO_STRIDE
101 */
102 static int make_vs_key( struct svga_context *svga,
103 struct svga_vs_compile_key *key )
104 {
105 memset(key, 0, sizeof *key);
106 key->need_prescale = svga->state.hw_clear.prescale.enabled;
107 key->allow_psiz = svga->curr.rast->templ.point_size_per_vertex;
108 key->zero_stride_vertex_elements =
109 svga->curr.zero_stride_vertex_elements;
110 key->num_zero_stride_vertex_elements =
111 svga->curr.num_zero_stride_vertex_elements;
112 return 0;
113 }
114
115
116
117 static int emit_hw_vs( struct svga_context *svga,
118 unsigned dirty )
119 {
120 struct svga_shader_result *result = NULL;
121 unsigned id = SVGA3D_INVALID_ID;
122 int ret = 0;
123
124 /* SVGA_NEW_NEED_SWTNL */
125 if (!svga->state.sw.need_swtnl) {
126 struct svga_vertex_shader *vs = svga->curr.vs;
127 struct svga_vs_compile_key key;
128
129 ret = make_vs_key( svga, &key );
130 if (ret)
131 return ret;
132
133 result = search_vs_key( vs, &key );
134 if (!result) {
135 ret = compile_vs( svga, vs, &key, &result );
136 if (ret)
137 return ret;
138 }
139
140 assert (result);
141 id = result->id;
142 }
143
144 if (id != svga->state.hw_draw.shader_id[PIPE_SHADER_VERTEX]) {
145 ret = SVGA3D_SetShader(svga->swc,
146 SVGA3D_SHADERTYPE_VS,
147 id );
148 if (ret)
149 return ret;
150
151 svga->dirty |= SVGA_NEW_VS_RESULT;
152 svga->state.hw_draw.shader_id[PIPE_SHADER_VERTEX] = id;
153 svga->state.hw_draw.vs = result;
154 }
155
156 return 0;
157 }
158
159 struct svga_tracked_state svga_hw_vs =
160 {
161 "vertex shader (hwtnl)",
162 (SVGA_NEW_VS |
163 SVGA_NEW_PRESCALE |
164 SVGA_NEW_NEED_SWTNL |
165 SVGA_NEW_ZERO_STRIDE),
166 emit_hw_vs
167 };
168
169
170 /***********************************************************************
171 */
172 static int update_zero_stride( struct svga_context *svga,
173 unsigned dirty )
174 {
175 unsigned i;
176
177 svga->curr.zero_stride_vertex_elements = 0;
178 svga->curr.num_zero_stride_vertex_elements = 0;
179
180 for (i = 0; i < svga->curr.num_vertex_elements; i++) {
181 const struct pipe_vertex_element *vel = &svga->curr.ve[i];
182 const struct pipe_vertex_buffer *vbuffer = &svga->curr.vb[
183 vel->vertex_buffer_index];
184 if (vbuffer->stride == 0) {
185 unsigned const_idx =
186 svga->curr.num_zero_stride_vertex_elements;
187 struct translate *translate;
188 struct translate_key key;
189 void *mapped_buffer;
190
191 svga->curr.zero_stride_vertex_elements |= (1 << i);
192 ++svga->curr.num_zero_stride_vertex_elements;
193
194 key.output_stride = 4 * sizeof(float);
195 key.nr_elements = 1;
196 key.element[0].input_format = vel->src_format;
197 key.element[0].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
198 key.element[0].input_buffer = vel->vertex_buffer_index;
199 key.element[0].input_offset = vel->src_offset;
200 key.element[0].output_offset = const_idx * 4 * sizeof(float);
201
202 translate_key_sanitize(&key);
203 /* translate_generic_create is technically private but
204 * we don't want to code-generate, just want generic
205 * translation */
206 translate = translate_generic_create(&key);
207
208 assert(vel->src_offset == 0);
209
210 mapped_buffer = pipe_buffer_map_range(svga->pipe.screen,
211 vbuffer->buffer,
212 vel->src_offset,
213 pf_get_size(vel->src_format),
214 PIPE_BUFFER_USAGE_CPU_READ);
215 translate->set_buffer(translate, vel->vertex_buffer_index,
216 mapped_buffer,
217 vbuffer->stride);
218 translate->run(translate, 0, 1,
219 svga->curr.zero_stride_constants);
220
221 pipe_buffer_unmap(svga->pipe.screen,
222 vbuffer->buffer);
223 translate->release(translate);
224 }
225 }
226
227 if (svga->curr.num_zero_stride_vertex_elements)
228 svga->dirty |= SVGA_NEW_ZERO_STRIDE;
229
230 return 0;
231 }
232
233 struct svga_tracked_state svga_hw_update_zero_stride =
234 {
235 "update zero_stride",
236 ( SVGA_NEW_VELEMENT |
237 SVGA_NEW_VBUFFER ),
238 update_zero_stride
239 };