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