Merge branch '7.8'
[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 "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_format.h"
29 #include "util/u_math.h"
30 #include "util/u_bitmask.h"
31 #include "translate/translate.h"
32
33 #include "svga_context.h"
34 #include "svga_state.h"
35 #include "svga_cmd.h"
36 #include "svga_tgsi.h"
37
38 #include "svga_hw_reg.h"
39
40 /***********************************************************************
41 */
42
43
44 static INLINE int compare_vs_keys( const struct svga_vs_compile_key *a,
45 const struct svga_vs_compile_key *b )
46 {
47 unsigned keysize = svga_vs_key_size( a );
48 return memcmp( a, b, keysize );
49 }
50
51
52 static struct svga_shader_result *search_vs_key( struct svga_vertex_shader *vs,
53 const struct svga_vs_compile_key *key )
54 {
55 struct svga_shader_result *result = vs->base.results;
56
57 assert(key);
58
59 for ( ; result; result = result->next) {
60 if (compare_vs_keys( key, &result->key.vkey ) == 0)
61 return result;
62 }
63
64 return NULL;
65 }
66
67
68 static enum pipe_error compile_vs( struct svga_context *svga,
69 struct svga_vertex_shader *vs,
70 const struct svga_vs_compile_key *key,
71 struct svga_shader_result **out_result )
72 {
73 struct svga_shader_result *result;
74 enum pipe_error ret = PIPE_ERROR;
75
76 result = svga_translate_vertex_program( vs, key );
77 if (result == NULL) {
78 ret = PIPE_ERROR_OUT_OF_MEMORY;
79 goto fail;
80 }
81
82 result->id = util_bitmask_add(svga->vs_bm);
83 if(result->id == UTIL_BITMASK_INVALID_INDEX) {
84 ret = PIPE_ERROR_OUT_OF_MEMORY;
85 goto fail;
86 }
87
88 ret = SVGA3D_DefineShader(svga->swc,
89 result->id,
90 SVGA3D_SHADERTYPE_VS,
91 result->tokens,
92 result->nr_tokens * sizeof result->tokens[0]);
93 if (ret)
94 goto fail;
95
96 *out_result = result;
97 result->next = vs->base.results;
98 vs->base.results = result;
99 return PIPE_OK;
100
101 fail:
102 if (result) {
103 if (result->id != UTIL_BITMASK_INVALID_INDEX)
104 util_bitmask_clear( svga->vs_bm, result->id );
105 svga_destroy_shader_result( result );
106 }
107 return ret;
108 }
109
110 /* SVGA_NEW_PRESCALE, SVGA_NEW_RAST, SVGA_NEW_ZERO_STRIDE
111 */
112 static int make_vs_key( struct svga_context *svga,
113 struct svga_vs_compile_key *key )
114 {
115 memset(key, 0, sizeof *key);
116 key->need_prescale = svga->state.hw_clear.prescale.enabled;
117 key->allow_psiz = svga->curr.rast->templ.point_size_per_vertex;
118 key->zero_stride_vertex_elements =
119 svga->curr.zero_stride_vertex_elements;
120 key->num_zero_stride_vertex_elements =
121 svga->curr.num_zero_stride_vertex_elements;
122 return 0;
123 }
124
125
126
127 static int emit_hw_vs( struct svga_context *svga,
128 unsigned dirty )
129 {
130 struct svga_shader_result *result = NULL;
131 unsigned id = SVGA3D_INVALID_ID;
132 int ret = 0;
133
134 /* SVGA_NEW_NEED_SWTNL */
135 if (!svga->state.sw.need_swtnl) {
136 struct svga_vertex_shader *vs = svga->curr.vs;
137 struct svga_vs_compile_key key;
138
139 ret = make_vs_key( svga, &key );
140 if (ret)
141 return ret;
142
143 result = search_vs_key( vs, &key );
144 if (!result) {
145 ret = compile_vs( svga, vs, &key, &result );
146 if (ret)
147 return ret;
148 }
149
150 assert (result);
151 id = result->id;
152 }
153
154 if (result != svga->state.hw_draw.vs) {
155 ret = SVGA3D_SetShader(svga->swc,
156 SVGA3D_SHADERTYPE_VS,
157 id );
158 if (ret)
159 return ret;
160
161 svga->dirty |= SVGA_NEW_VS_RESULT;
162 svga->state.hw_draw.vs = result;
163 }
164
165 return 0;
166 }
167
168 struct svga_tracked_state svga_hw_vs =
169 {
170 "vertex shader (hwtnl)",
171 (SVGA_NEW_VS |
172 SVGA_NEW_PRESCALE |
173 SVGA_NEW_NEED_SWTNL |
174 SVGA_NEW_ZERO_STRIDE),
175 emit_hw_vs
176 };
177
178
179 /***********************************************************************
180 */
181 static int update_zero_stride( struct svga_context *svga,
182 unsigned dirty )
183 {
184 unsigned i;
185
186 svga->curr.zero_stride_vertex_elements = 0;
187 svga->curr.num_zero_stride_vertex_elements = 0;
188
189 for (i = 0; i < svga->curr.velems->count; i++) {
190 const struct pipe_vertex_element *vel = &svga->curr.velems->velem[i];
191 const struct pipe_vertex_buffer *vbuffer = &svga->curr.vb[
192 vel->vertex_buffer_index];
193
194 if (vbuffer->stride == 0) {
195 unsigned const_idx =
196 svga->curr.num_zero_stride_vertex_elements;
197 struct pipe_transfer *transfer;
198 struct translate *translate;
199 struct translate_key key;
200 void *mapped_buffer;
201
202 svga->curr.zero_stride_vertex_elements |= (1 << i);
203 ++svga->curr.num_zero_stride_vertex_elements;
204
205 key.output_stride = 4 * sizeof(float);
206 key.nr_elements = 1;
207 key.element[0].type = TRANSLATE_ELEMENT_NORMAL;
208 key.element[0].input_format = vel->src_format;
209 key.element[0].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
210 key.element[0].input_buffer = vel->vertex_buffer_index;
211 key.element[0].input_offset = vel->src_offset;
212 key.element[0].instance_divisor = vel->instance_divisor;
213 key.element[0].output_offset = const_idx * 4 * sizeof(float);
214
215 translate_key_sanitize(&key);
216 /* translate_generic_create is technically private but
217 * we don't want to code-generate, just want generic
218 * translation */
219 translate = translate_generic_create(&key);
220
221 assert(vel->src_offset == 0);
222
223 mapped_buffer = pipe_buffer_map_range(&svga->pipe,
224 vbuffer->buffer,
225 vel->src_offset,
226 util_format_get_blocksize(vel->src_format),
227 PIPE_TRANSFER_READ,
228 &transfer);
229
230 translate->set_buffer(translate, vel->vertex_buffer_index,
231 mapped_buffer,
232 vbuffer->stride, vbuffer->max_index);
233 translate->run(translate, 0, 1, 0,
234 svga->curr.zero_stride_constants);
235
236 pipe_buffer_unmap(&svga->pipe,
237 vbuffer->buffer,
238 transfer);
239
240 translate->release(translate);
241 }
242 }
243
244 if (svga->curr.num_zero_stride_vertex_elements)
245 svga->dirty |= SVGA_NEW_ZERO_STRIDE;
246
247 return 0;
248 }
249
250 struct svga_tracked_state svga_hw_update_zero_stride =
251 {
252 "update zero_stride",
253 ( SVGA_NEW_VELEMENT |
254 SVGA_NEW_VBUFFER ),
255 update_zero_stride
256 };