gallium/svga: Only upload parts of vertexarrays that are actually used
[mesa.git] / src / gallium / drivers / svga / svga_draw_elements.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 "util/u_upload_mgr.h"
28 #include "indices/u_indices.h"
29
30 #include "svga_cmd.h"
31 #include "svga_draw.h"
32 #include "svga_draw_private.h"
33 #include "svga_resource_buffer.h"
34 #include "svga_winsys.h"
35 #include "svga_context.h"
36
37 #include "svga_hw_reg.h"
38
39
40 static enum pipe_error
41 translate_indices( struct svga_hwtnl *hwtnl,
42 struct pipe_resource *src,
43 unsigned offset,
44 unsigned nr,
45 unsigned index_size,
46 u_translate_func translate,
47 struct pipe_resource **out_buf )
48 {
49 struct pipe_context *pipe = &hwtnl->svga->pipe;
50 struct pipe_transfer *src_transfer = NULL;
51 struct pipe_transfer *dst_transfer = NULL;
52 unsigned size = index_size * nr;
53 const void *src_map = NULL;
54 struct pipe_resource *dst = NULL;
55 void *dst_map = NULL;
56
57 dst = pipe_buffer_create( pipe->screen,
58 PIPE_BIND_INDEX_BUFFER,
59 PIPE_USAGE_STATIC,
60 size );
61 if (dst == NULL)
62 goto fail;
63
64 src_map = pipe_buffer_map( pipe, src, PIPE_TRANSFER_READ, &src_transfer );
65 if (src_map == NULL)
66 goto fail;
67
68 dst_map = pipe_buffer_map( pipe, dst, PIPE_TRANSFER_WRITE, &dst_transfer );
69 if (dst_map == NULL)
70 goto fail;
71
72 translate( (const char *)src_map + offset,
73 nr,
74 dst_map );
75
76 pipe_buffer_unmap( pipe, src_transfer );
77 pipe_buffer_unmap( pipe, dst_transfer );
78
79 *out_buf = dst;
80 return PIPE_OK;
81
82 fail:
83 if (src_map)
84 pipe_buffer_unmap( pipe, src_transfer );
85
86 if (dst_map)
87 pipe_buffer_unmap( pipe, dst_transfer );
88
89 if (dst)
90 pipe->screen->resource_destroy( pipe->screen, dst );
91
92 return PIPE_ERROR_OUT_OF_MEMORY;
93 }
94
95
96
97
98
99 enum pipe_error
100 svga_hwtnl_simple_draw_range_elements( struct svga_hwtnl *hwtnl,
101 struct pipe_resource *index_buffer,
102 unsigned index_size,
103 int index_bias,
104 unsigned min_index,
105 unsigned max_index,
106 unsigned prim,
107 unsigned start,
108 unsigned count )
109 {
110 struct pipe_resource *upload_buffer = NULL;
111 SVGA3dPrimitiveRange range;
112 unsigned hw_prim;
113 unsigned hw_count;
114 unsigned index_offset = start * index_size;
115 int ret = PIPE_OK;
116 unsigned i, src_offs;
117
118 hw_prim = svga_translate_prim(prim, count, &hw_count);
119 if (hw_count == 0)
120 goto done;
121
122 if (index_buffer &&
123 svga_buffer_is_user_buffer(index_buffer))
124 {
125 boolean flushed;
126 assert( index_buffer->width0 >= index_offset + count * index_size );
127
128 ret = u_upload_buffer( hwtnl->upload_ib,
129 0,
130 index_offset,
131 count * index_size,
132 index_buffer,
133 &index_offset,
134 &upload_buffer,
135 &flushed );
136 if (ret)
137 goto done;
138
139 /* Don't need to worry about refcounting index_buffer as this is
140 * just a stack variable without a counted reference of its own.
141 * The caller holds the reference.
142 */
143 index_buffer = upload_buffer;
144 }
145
146 for (i = 0; i < hwtnl->cmd.vdecl_count; i++) {
147 struct pipe_resource *vb = hwtnl->cmd.vdecl_vb[i];
148 struct svga_buffer *sbuf = svga_buffer(vb);
149 unsigned stride = hwtnl->cmd.vdecl[i].array.stride;
150 unsigned tmp_src_offs = sbuf->source_offset;
151
152 if (stride)
153 tmp_src_offs /= stride;
154 assert(i == 0 || tmp_src_offs == src_offs);
155 src_offs = tmp_src_offs;
156 }
157
158 index_bias -= src_offs;
159 assert(index_bias >= 0);
160
161 range.primType = hw_prim;
162 range.primitiveCount = hw_count;
163 range.indexArray.offset = index_offset;
164 range.indexArray.stride = index_size;
165 range.indexWidth = index_size;
166 range.indexBias = index_bias;
167
168 ret = svga_hwtnl_prim( hwtnl, &range, min_index, max_index, index_buffer );
169 if (ret)
170 goto done;
171
172 done:
173 if (upload_buffer)
174 pipe_resource_reference( &upload_buffer, NULL );
175
176 return ret;
177 }
178
179
180
181
182 enum pipe_error
183 svga_hwtnl_draw_range_elements( struct svga_hwtnl *hwtnl,
184 struct pipe_resource *index_buffer,
185 unsigned index_size,
186 int index_bias,
187 unsigned min_index,
188 unsigned max_index,
189 unsigned prim, unsigned start, unsigned count)
190 {
191 unsigned gen_prim, gen_size, gen_nr, gen_type;
192 u_translate_func gen_func;
193 enum pipe_error ret = PIPE_OK;
194
195 if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
196 prim >= PIPE_PRIM_TRIANGLES)
197 {
198 gen_type = u_unfilled_translator( prim,
199 index_size,
200 count,
201 hwtnl->api_fillmode,
202 &gen_prim,
203 &gen_size,
204 &gen_nr,
205 &gen_func );
206 }
207 else
208 {
209 gen_type = u_index_translator( svga_hw_prims,
210 prim,
211 index_size,
212 count,
213 hwtnl->api_pv,
214 hwtnl->hw_pv,
215 &gen_prim,
216 &gen_size,
217 &gen_nr,
218 &gen_func );
219 }
220
221
222 if (gen_type == U_TRANSLATE_MEMCPY) {
223 /* No need for translation, just pass through to hardware:
224 */
225 return svga_hwtnl_simple_draw_range_elements( hwtnl, index_buffer,
226 index_size,
227 index_bias,
228 min_index,
229 max_index,
230 gen_prim, start, count );
231 }
232 else {
233 struct pipe_resource *gen_buf = NULL;
234
235 /* Need to allocate a new index buffer and run the translate
236 * func to populate it. Could potentially cache this translated
237 * index buffer with the original to avoid future
238 * re-translations. Not much point if we're just accelerating
239 * GL though, as index buffers are typically used only once
240 * there.
241 */
242 ret = translate_indices( hwtnl,
243 index_buffer,
244 start * index_size,
245 gen_nr,
246 gen_size,
247 gen_func,
248 &gen_buf );
249 if (ret)
250 goto done;
251
252 ret = svga_hwtnl_simple_draw_range_elements( hwtnl,
253 gen_buf,
254 gen_size,
255 index_bias,
256 min_index,
257 max_index,
258 gen_prim,
259 0,
260 gen_nr );
261 if (ret)
262 goto done;
263
264 done:
265 if (gen_buf)
266 pipe_resource_reference( &gen_buf, NULL );
267
268 return ret;
269 }
270 }
271
272
273
274
275