svga: add guest statistic gathering interface
[mesa.git] / src / gallium / drivers / svga / svga_resource_buffer.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 "svga_cmd.h"
27
28 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "os/os_thread.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "util/u_resource.h"
35
36 #include "svga_context.h"
37 #include "svga_screen.h"
38 #include "svga_resource_buffer.h"
39 #include "svga_resource_buffer_upload.h"
40 #include "svga_winsys.h"
41 #include "svga_debug.h"
42
43
44 /**
45 * Vertex and index buffers need hardware backing. Constant buffers
46 * do not. No other types of buffers currently supported.
47 */
48 static inline boolean
49 svga_buffer_needs_hw_storage(unsigned usage)
50 {
51 return (usage & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER |
52 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_STREAM_OUTPUT)) != 0;
53 }
54
55
56 /**
57 * Create a buffer transfer.
58 *
59 * Unlike texture DMAs (which are written immediately to the command buffer and
60 * therefore inherently serialized with other context operations), for buffers
61 * we try to coalesce multiple range mappings (i.e, multiple calls to this
62 * function) into a single DMA command, for better efficiency in command
63 * processing. This means we need to exercise extra care here to ensure that
64 * the end result is exactly the same as if one DMA was used for every mapped
65 * range.
66 */
67 static void *
68 svga_buffer_transfer_map(struct pipe_context *pipe,
69 struct pipe_resource *resource,
70 unsigned level,
71 unsigned usage,
72 const struct pipe_box *box,
73 struct pipe_transfer **ptransfer)
74 {
75 struct svga_context *svga = svga_context(pipe);
76 struct svga_screen *ss = svga_screen(pipe->screen);
77 struct svga_buffer *sbuf = svga_buffer(resource);
78 struct pipe_transfer *transfer;
79 uint8_t *map = NULL;
80 int64_t begin = svga_get_time(svga);
81
82 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_BUFFERTRANSFERMAP);
83
84 assert(box->y == 0);
85 assert(box->z == 0);
86 assert(box->height == 1);
87 assert(box->depth == 1);
88
89 transfer = MALLOC_STRUCT(pipe_transfer);
90 if (!transfer) {
91 goto done;
92 }
93
94 transfer->resource = resource;
95 transfer->level = level;
96 transfer->usage = usage;
97 transfer->box = *box;
98 transfer->stride = 0;
99 transfer->layer_stride = 0;
100
101 if ((usage & PIPE_TRANSFER_READ) && sbuf->dirty) {
102 enum pipe_error ret;
103
104 /* Host-side buffers can only be dirtied with vgpu10 features
105 * (streamout and buffer copy).
106 */
107 assert(svga_have_vgpu10(svga));
108
109 if (!sbuf->user) {
110 (void) svga_buffer_handle(svga, resource);
111 }
112
113 if (sbuf->dma.pending > 0) {
114 svga_buffer_upload_flush(svga, sbuf);
115 svga_context_finish(svga);
116 }
117
118 assert(sbuf->handle);
119
120 ret = SVGA3D_vgpu10_ReadbackSubResource(svga->swc, sbuf->handle, 0);
121 if (ret != PIPE_OK) {
122 svga_context_flush(svga, NULL);
123 ret = SVGA3D_vgpu10_ReadbackSubResource(svga->swc, sbuf->handle, 0);
124 assert(ret == PIPE_OK);
125 }
126
127 svga->hud.num_readbacks++;
128
129 svga_context_finish(svga);
130
131 sbuf->dirty = FALSE;
132 }
133
134 if (usage & PIPE_TRANSFER_WRITE) {
135 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
136 /*
137 * Flush any pending primitives, finish writing any pending DMA
138 * commands, and tell the host to discard the buffer contents on
139 * the next DMA operation.
140 */
141
142 svga_hwtnl_flush_buffer(svga, resource);
143
144 if (sbuf->dma.pending) {
145 svga_buffer_upload_flush(svga, sbuf);
146
147 /*
148 * Instead of flushing the context command buffer, simply discard
149 * the current hwbuf, and start a new one.
150 * With GB objects, the map operation takes care of this
151 * if passed the PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE flag,
152 * and the old backing store is busy.
153 */
154
155 if (!svga_have_gb_objects(svga))
156 svga_buffer_destroy_hw_storage(ss, sbuf);
157 }
158
159 sbuf->map.num_ranges = 0;
160 sbuf->dma.flags.discard = TRUE;
161 }
162
163 if (usage & PIPE_TRANSFER_UNSYNCHRONIZED) {
164 if (!sbuf->map.num_ranges) {
165 /*
166 * No pending ranges to upload so far, so we can tell the host to
167 * not synchronize on the next DMA command.
168 */
169
170 sbuf->dma.flags.unsynchronized = TRUE;
171 }
172 } else {
173 /*
174 * Synchronizing, so flush any pending primitives, finish writing any
175 * pending DMA command, and ensure the next DMA will be done in order.
176 */
177
178 svga_hwtnl_flush_buffer(svga, resource);
179
180 if (sbuf->dma.pending) {
181 svga_buffer_upload_flush(svga, sbuf);
182
183 if (svga_buffer_has_hw_storage(sbuf)) {
184 /*
185 * We have a pending DMA upload from a hardware buffer, therefore
186 * we need to ensure that the host finishes processing that DMA
187 * command before the state tracker can start overwriting the
188 * hardware buffer.
189 *
190 * XXX: This could be avoided by tying the hardware buffer to
191 * the transfer (just as done with textures), which would allow
192 * overlapping DMAs commands to be queued on the same context
193 * buffer. However, due to the likelihood of software vertex
194 * processing, it is more convenient to hold on to the hardware
195 * buffer, allowing to quickly access the contents from the CPU
196 * without having to do a DMA download from the host.
197 */
198
199 if (usage & PIPE_TRANSFER_DONTBLOCK) {
200 /*
201 * Flushing the command buffer here will most likely cause
202 * the map of the hwbuf below to block, so preemptively
203 * return NULL here if DONTBLOCK is set to prevent unnecessary
204 * command buffer flushes.
205 */
206
207 FREE(transfer);
208 goto done;
209 }
210
211 svga_context_flush(svga, NULL);
212 }
213 }
214
215 sbuf->dma.flags.unsynchronized = FALSE;
216 }
217 }
218
219 if (!sbuf->swbuf && !svga_buffer_has_hw_storage(sbuf)) {
220 if (svga_buffer_create_hw_storage(ss, sbuf) != PIPE_OK) {
221 /*
222 * We can't create a hardware buffer big enough, so create a malloc
223 * buffer instead.
224 */
225 if (0) {
226 debug_printf("%s: failed to allocate %u KB of DMA, "
227 "splitting DMA transfers\n",
228 __FUNCTION__,
229 (sbuf->b.b.width0 + 1023)/1024);
230 }
231
232 sbuf->swbuf = align_malloc(sbuf->b.b.width0, 16);
233 if (!sbuf->swbuf) {
234 FREE(transfer);
235 goto done;
236 }
237 }
238 }
239
240 if (sbuf->swbuf) {
241 /* User/malloc buffer */
242 map = sbuf->swbuf;
243 }
244 else if (svga_buffer_has_hw_storage(sbuf)) {
245 boolean retry;
246
247 map = svga_buffer_hw_storage_map(svga, sbuf, transfer->usage, &retry);
248 if (map == NULL && retry) {
249 /*
250 * At this point, svga_buffer_get_transfer() has already
251 * hit the DISCARD_WHOLE_RESOURCE path and flushed HWTNL
252 * for this buffer.
253 */
254 svga_context_flush(svga, NULL);
255 map = svga_buffer_hw_storage_map(svga, sbuf, transfer->usage, &retry);
256 }
257 }
258 else {
259 map = NULL;
260 }
261
262 if (map) {
263 ++sbuf->map.count;
264 map += transfer->box.x;
265 *ptransfer = transfer;
266 } else {
267 FREE(transfer);
268 }
269
270 svga->hud.map_buffer_time += (svga_get_time(svga) - begin);
271
272 done:
273 SVGA_STATS_TIME_POP(svga_sws(svga));
274 return map;
275 }
276
277
278 static void
279 svga_buffer_transfer_flush_region( struct pipe_context *pipe,
280 struct pipe_transfer *transfer,
281 const struct pipe_box *box)
282 {
283 struct svga_screen *ss = svga_screen(pipe->screen);
284 struct svga_buffer *sbuf = svga_buffer(transfer->resource);
285
286 unsigned offset = transfer->box.x + box->x;
287 unsigned length = box->width;
288
289 assert(transfer->usage & PIPE_TRANSFER_WRITE);
290 assert(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT);
291
292 pipe_mutex_lock(ss->swc_mutex);
293 svga_buffer_add_range(sbuf, offset, offset + length);
294 pipe_mutex_unlock(ss->swc_mutex);
295 }
296
297
298 static void
299 svga_buffer_transfer_unmap( struct pipe_context *pipe,
300 struct pipe_transfer *transfer )
301 {
302 struct svga_screen *ss = svga_screen(pipe->screen);
303 struct svga_context *svga = svga_context(pipe);
304 struct svga_buffer *sbuf = svga_buffer(transfer->resource);
305
306 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_BUFFERTRANSFERUNMAP);
307
308 pipe_mutex_lock(ss->swc_mutex);
309
310 assert(sbuf->map.count);
311 if (sbuf->map.count) {
312 --sbuf->map.count;
313 }
314
315 if (svga_buffer_has_hw_storage(sbuf)) {
316 svga_buffer_hw_storage_unmap(svga, sbuf);
317 }
318
319 if (transfer->usage & PIPE_TRANSFER_WRITE) {
320 if (!(transfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
321 /*
322 * Mapped range not flushed explicitly, so flush the whole buffer,
323 * and tell the host to discard the contents when processing the DMA
324 * command.
325 */
326
327 SVGA_DBG(DEBUG_DMA, "flushing the whole buffer\n");
328
329 sbuf->dma.flags.discard = TRUE;
330
331 svga_buffer_add_range(sbuf, 0, sbuf->b.b.width0);
332 }
333 }
334
335 pipe_mutex_unlock(ss->swc_mutex);
336 FREE(transfer);
337 SVGA_STATS_TIME_POP(svga_sws(svga));
338 }
339
340
341 static void
342 svga_buffer_destroy( struct pipe_screen *screen,
343 struct pipe_resource *buf )
344 {
345 struct svga_screen *ss = svga_screen(screen);
346 struct svga_buffer *sbuf = svga_buffer( buf );
347
348 assert(!p_atomic_read(&buf->reference.count));
349
350 assert(!sbuf->dma.pending);
351
352 if (sbuf->handle)
353 svga_buffer_destroy_host_surface(ss, sbuf);
354
355 if (sbuf->uploaded.buffer)
356 pipe_resource_reference(&sbuf->uploaded.buffer, NULL);
357
358 if (sbuf->hwbuf)
359 svga_buffer_destroy_hw_storage(ss, sbuf);
360
361 if (sbuf->swbuf && !sbuf->user)
362 align_free(sbuf->swbuf);
363
364 ss->hud.total_resource_bytes -= sbuf->size;
365 assert(ss->hud.num_resources > 0);
366 if (ss->hud.num_resources > 0)
367 ss->hud.num_resources--;
368
369 FREE(sbuf);
370 }
371
372
373 struct u_resource_vtbl svga_buffer_vtbl =
374 {
375 u_default_resource_get_handle, /* get_handle */
376 svga_buffer_destroy, /* resource_destroy */
377 svga_buffer_transfer_map, /* transfer_map */
378 svga_buffer_transfer_flush_region, /* transfer_flush_region */
379 svga_buffer_transfer_unmap, /* transfer_unmap */
380 };
381
382
383
384 struct pipe_resource *
385 svga_buffer_create(struct pipe_screen *screen,
386 const struct pipe_resource *template)
387 {
388 struct svga_screen *ss = svga_screen(screen);
389 struct svga_buffer *sbuf;
390
391 SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_CREATEBUFFER);
392
393 sbuf = CALLOC_STRUCT(svga_buffer);
394 if (!sbuf)
395 goto error1;
396
397 sbuf->b.b = *template;
398 sbuf->b.vtbl = &svga_buffer_vtbl;
399 pipe_reference_init(&sbuf->b.b.reference, 1);
400 sbuf->b.b.screen = screen;
401 sbuf->bind_flags = template->bind;
402
403 if (template->bind & PIPE_BIND_CONSTANT_BUFFER) {
404 /* Constant buffers can only have the PIPE_BIND_CONSTANT_BUFFER
405 * flag set.
406 */
407 if (ss->sws->have_vgpu10) {
408 sbuf->bind_flags = PIPE_BIND_CONSTANT_BUFFER;
409
410 /* Constant buffer size needs to be in multiples of 16. */
411 sbuf->b.b.width0 = align(sbuf->b.b.width0, 16);
412 }
413 }
414
415 if (svga_buffer_needs_hw_storage(template->bind)) {
416
417 /* If the buffer will be used for vertex/index/stream data, set all
418 * the flags so that the buffer will be accepted for all those uses.
419 * Note that the PIPE_BIND_ flags we get from the state tracker are
420 * just a hint about how the buffer may be used. And OpenGL buffer
421 * object may be used for many different things.
422 */
423 if (!(template->bind & PIPE_BIND_CONSTANT_BUFFER)) {
424 /* Not a constant buffer. The buffer may be used for vertex data,
425 * indexes or stream-out.
426 */
427 sbuf->bind_flags |= (PIPE_BIND_VERTEX_BUFFER |
428 PIPE_BIND_INDEX_BUFFER);
429 if (ss->sws->have_vgpu10)
430 sbuf->bind_flags |= PIPE_BIND_STREAM_OUTPUT;
431 }
432
433 if (svga_buffer_create_host_surface(ss, sbuf) != PIPE_OK)
434 goto error2;
435 }
436 else {
437 sbuf->swbuf = align_malloc(sbuf->b.b.width0, 64);
438 if (!sbuf->swbuf)
439 goto error2;
440 }
441
442 debug_reference(&sbuf->b.b.reference,
443 (debug_reference_descriptor)debug_describe_resource, 0);
444
445 sbuf->size = util_resource_size(&sbuf->b.b);
446 ss->hud.total_resource_bytes += sbuf->size;
447
448 ss->hud.num_resources++;
449 SVGA_STATS_TIME_POP(ss->sws);
450
451 return &sbuf->b.b;
452
453 error2:
454 FREE(sbuf);
455 error1:
456 SVGA_STATS_TIME_POP(ss->sws);
457 return NULL;
458 }
459
460
461 struct pipe_resource *
462 svga_user_buffer_create(struct pipe_screen *screen,
463 void *ptr,
464 unsigned bytes,
465 unsigned bind)
466 {
467 struct svga_buffer *sbuf;
468 struct svga_screen *ss = svga_screen(screen);
469
470 sbuf = CALLOC_STRUCT(svga_buffer);
471 if (!sbuf)
472 goto no_sbuf;
473
474 pipe_reference_init(&sbuf->b.b.reference, 1);
475 sbuf->b.vtbl = &svga_buffer_vtbl;
476 sbuf->b.b.screen = screen;
477 sbuf->b.b.format = PIPE_FORMAT_R8_UNORM; /* ?? */
478 sbuf->b.b.usage = PIPE_USAGE_IMMUTABLE;
479 sbuf->b.b.bind = bind;
480 sbuf->b.b.width0 = bytes;
481 sbuf->b.b.height0 = 1;
482 sbuf->b.b.depth0 = 1;
483 sbuf->b.b.array_size = 1;
484
485 sbuf->bind_flags = bind;
486 sbuf->swbuf = ptr;
487 sbuf->user = TRUE;
488
489 debug_reference(&sbuf->b.b.reference,
490 (debug_reference_descriptor)debug_describe_resource, 0);
491
492 ss->hud.num_resources++;
493
494 return &sbuf->b.b;
495
496 no_sbuf:
497 return NULL;
498 }
499
500
501