svga: Map vertex- index- and constant buffers ansynchronously when reading
[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_prim.h"
28 #include "util/u_upload_mgr.h"
29 #include "indices/u_indices.h"
30
31 #include "svga_cmd.h"
32 #include "svga_draw.h"
33 #include "svga_draw_private.h"
34 #include "svga_resource_buffer.h"
35 #include "svga_winsys.h"
36 #include "svga_context.h"
37 #include "svga_hw_reg.h"
38
39
40 /**
41 * Return a new index buffer which contains a translation of the original
42 * index buffer. An example of a translation is converting from QUAD
43 * primitives to TRIANGLE primitives. Each set of four indexes for a quad
44 * will be converted to six indices for two triangles.
45 *
46 * Before generating the new index buffer we'll check if the incoming
47 * buffer already has a translated buffer that can be re-used.
48 * This benefits demos like Cinebench R15 which has many
49 * glDrawElements(GL_QUADS) commands (we can't draw quads natively).
50 *
51 * \param offset offset in bytes to first index to translate in src buffer
52 * \param orig_prim original primitive type (like PIPE_PRIM_QUADS)
53 * \param gen_prim new/generated primitive type (like PIPE_PRIM_TRIANGLES)
54 * \param orig_nr number of indexes to translate in source buffer
55 * \param gen_nr number of indexes to write into new/dest buffer
56 * \param index_size bytes per index (2 or 4)
57 * \param translate the translation function from the u_translate module
58 * \param out_buf returns the new/translated index buffer
59 * \return error code to indicate success failure
60 */
61 static enum pipe_error
62 translate_indices(struct svga_hwtnl *hwtnl,
63 const struct pipe_draw_info *info,
64 enum pipe_prim_type gen_prim,
65 unsigned orig_nr, unsigned gen_nr,
66 unsigned gen_size,
67 u_translate_func translate,
68 struct pipe_resource **out_buf,
69 unsigned *out_offset)
70 {
71 struct pipe_context *pipe = &hwtnl->svga->pipe;
72 struct svga_screen *screen = svga_screen(pipe->screen);
73 struct svga_buffer *src_sbuf = NULL;
74 struct pipe_transfer *src_transfer = NULL;
75 struct pipe_transfer *dst_transfer = NULL;
76 const unsigned size = gen_size * gen_nr;
77 const unsigned offset = info->start * info->index_size;
78 const void *src_map = NULL;
79 struct pipe_resource *dst = NULL;
80 void *dst_map = NULL;
81
82 assert(gen_size == 2 || gen_size == 4);
83 if (!info->has_user_indices)
84 src_sbuf = svga_buffer(info->index.resource);
85
86 /* If the draw_info provides us with a buffer rather than a
87 * user pointer, Check to see if we've already translated that buffer
88 */
89 if (src_sbuf && !screen->debug.no_cache_index_buffers) {
90 /* Check if we already have a translated index buffer */
91 if (src_sbuf->translated_indices.buffer &&
92 src_sbuf->translated_indices.orig_prim == info->mode &&
93 src_sbuf->translated_indices.new_prim == gen_prim &&
94 src_sbuf->translated_indices.offset == offset &&
95 src_sbuf->translated_indices.count == orig_nr &&
96 src_sbuf->translated_indices.index_size == gen_size) {
97 pipe_resource_reference(out_buf, src_sbuf->translated_indices.buffer);
98 return PIPE_OK;
99 }
100 }
101
102 /* Need to trim vertex count to make sure we don't write too much data
103 * to the dst buffer in the translate() call.
104 */
105 u_trim_pipe_prim(gen_prim, &gen_nr);
106
107 if (src_sbuf) {
108 /* If we have a source buffer, create a destination buffer in the
109 * hope that we can reuse the translated data later. If not,
110 * we'd probably be better off using the upload buffer.
111 */
112 dst = pipe_buffer_create(pipe->screen,
113 PIPE_BIND_INDEX_BUFFER, PIPE_USAGE_IMMUTABLE,
114 size);
115 if (!dst)
116 goto fail;
117
118 dst_map = pipe_buffer_map(pipe, dst, PIPE_TRANSFER_WRITE, &dst_transfer);
119 if (!dst_map)
120 goto fail;
121
122 *out_offset = 0;
123 src_map = pipe_buffer_map(pipe, info->index.resource,
124 PIPE_TRANSFER_READ |
125 PIPE_TRANSFER_UNSYNCHRONIZED,
126 &src_transfer);
127 if (!src_map)
128 goto fail;
129 } else {
130 /* Allocate upload buffer space. Align to the index size. */
131 u_upload_alloc(pipe->stream_uploader, 0, size, gen_size,
132 out_offset, &dst, &dst_map);
133 if (!dst)
134 goto fail;
135
136 src_map = info->index.user;
137 }
138
139 translate((const char *) src_map + offset, 0, 0, gen_nr, 0, dst_map);
140
141 if (src_transfer)
142 pipe_buffer_unmap(pipe, src_transfer);
143
144 if (dst_transfer)
145 pipe_buffer_unmap(pipe, dst_transfer);
146 else
147 u_upload_unmap(pipe->stream_uploader);
148
149 *out_buf = dst;
150
151 if (src_sbuf && !screen->debug.no_cache_index_buffers) {
152 /* Save the new, translated index buffer in the hope we can use it
153 * again in the future.
154 */
155 pipe_resource_reference(&src_sbuf->translated_indices.buffer, dst);
156 src_sbuf->translated_indices.orig_prim = info->mode;
157 src_sbuf->translated_indices.new_prim = gen_prim;
158 src_sbuf->translated_indices.offset = offset;
159 src_sbuf->translated_indices.count = orig_nr;
160 src_sbuf->translated_indices.index_size = gen_size;
161 }
162
163 return PIPE_OK;
164
165 fail:
166 if (src_transfer)
167 pipe_buffer_unmap(pipe, src_transfer);
168
169 if (dst_transfer)
170 pipe_buffer_unmap(pipe, dst_transfer);
171 else if (dst_map)
172 u_upload_unmap(pipe->stream_uploader);
173
174 if (dst)
175 pipe_resource_reference(&dst, NULL);
176
177 return PIPE_ERROR_OUT_OF_MEMORY;
178 }
179
180
181 enum pipe_error
182 svga_hwtnl_simple_draw_range_elements(struct svga_hwtnl *hwtnl,
183 struct pipe_resource *index_buffer,
184 unsigned index_size, int index_bias,
185 unsigned min_index, unsigned max_index,
186 enum pipe_prim_type prim, unsigned start,
187 unsigned count,
188 unsigned start_instance,
189 unsigned instance_count)
190 {
191 SVGA3dPrimitiveRange range;
192 unsigned hw_prim;
193 unsigned hw_count;
194 unsigned index_offset = start * index_size;
195
196 hw_prim = svga_translate_prim(prim, count, &hw_count);
197 if (hw_count == 0)
198 return PIPE_OK; /* nothing to draw */
199
200 range.primType = hw_prim;
201 range.primitiveCount = hw_count;
202 range.indexArray.offset = index_offset;
203 range.indexArray.stride = index_size;
204 range.indexWidth = index_size;
205 range.indexBias = index_bias;
206
207 return svga_hwtnl_prim(hwtnl, &range, count,
208 min_index, max_index, index_buffer,
209 start_instance, instance_count);
210 }
211
212
213 enum pipe_error
214 svga_hwtnl_draw_range_elements(struct svga_hwtnl *hwtnl,
215 const struct pipe_draw_info *info,
216 unsigned count)
217 {
218 struct pipe_context *pipe = &hwtnl->svga->pipe;
219 enum pipe_prim_type gen_prim;
220 unsigned gen_size, gen_nr;
221 enum indices_mode gen_type;
222 u_translate_func gen_func;
223 enum pipe_error ret = PIPE_OK;
224
225 SVGA_STATS_TIME_PUSH(svga_sws(hwtnl->svga),
226 SVGA_STATS_TIME_HWTNLDRAWELEMENTS);
227
228 if (svga_need_unfilled_fallback(hwtnl, info->mode)) {
229 gen_type = u_unfilled_translator(info->mode,
230 info->index_size,
231 count,
232 hwtnl->api_fillmode,
233 &gen_prim,
234 &gen_size, &gen_nr, &gen_func);
235 }
236 else {
237 gen_type = u_index_translator(svga_hw_prims,
238 info->mode,
239 info->index_size,
240 count,
241 hwtnl->api_pv,
242 hwtnl->hw_pv,
243 PR_DISABLE,
244 &gen_prim, &gen_size, &gen_nr, &gen_func);
245 }
246
247 if (gen_type == U_TRANSLATE_MEMCPY) {
248 /* No need for translation, just pass through to hardware:
249 */
250 unsigned start_offset = info->start * info->index_size;
251 struct pipe_resource *index_buffer = NULL;
252 unsigned index_offset;
253
254 if (info->has_user_indices) {
255 u_upload_data(pipe->stream_uploader, 0, count * info->index_size,
256 info->index_size, (char *) info->index.user + start_offset,
257 &index_offset, &index_buffer);
258 u_upload_unmap(pipe->stream_uploader);
259 index_offset /= info->index_size;
260 } else {
261 pipe_resource_reference(&index_buffer, info->index.resource);
262 index_offset = info->start;
263 }
264
265 assert(index_buffer != NULL);
266
267 ret = svga_hwtnl_simple_draw_range_elements(hwtnl, index_buffer,
268 info->index_size,
269 info->index_bias,
270 info->min_index,
271 info->max_index,
272 gen_prim, index_offset, count,
273 info->start_instance,
274 info->instance_count);
275 pipe_resource_reference(&index_buffer, NULL);
276 }
277 else {
278 struct pipe_resource *gen_buf = NULL;
279 unsigned gen_offset = 0;
280
281 /* Need to allocate a new index buffer and run the translate
282 * func to populate it. Could potentially cache this translated
283 * index buffer with the original to avoid future
284 * re-translations. Not much point if we're just accelerating
285 * GL though, as index buffers are typically used only once
286 * there.
287 */
288 ret = translate_indices(hwtnl, info, gen_prim,
289 count, gen_nr, gen_size,
290 gen_func, &gen_buf, &gen_offset);
291 if (ret == PIPE_OK) {
292 gen_offset /= gen_size;
293 ret = svga_hwtnl_simple_draw_range_elements(hwtnl,
294 gen_buf,
295 gen_size,
296 info->index_bias,
297 info->min_index,
298 info->max_index,
299 gen_prim, gen_offset,
300 gen_nr,
301 info->start_instance,
302 info->instance_count);
303 }
304
305 if (gen_buf) {
306 pipe_resource_reference(&gen_buf, NULL);
307 }
308 }
309
310 SVGA_STATS_TIME_POP(svga_sws(hwtnl->svga));
311 return ret;
312 }