1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
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"
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"
45 * Vertex and index buffers need hardware backing. Constant buffers
46 * do not. No other types of buffers currently supported.
49 svga_buffer_needs_hw_storage(unsigned usage
)
51 return (usage
& (PIPE_BIND_VERTEX_BUFFER
| PIPE_BIND_INDEX_BUFFER
|
52 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_STREAM_OUTPUT
)) != 0;
57 * Create a buffer transfer.
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
68 svga_buffer_transfer_map(struct pipe_context
*pipe
,
69 struct pipe_resource
*resource
,
72 const struct pipe_box
*box
,
73 struct pipe_transfer
**ptransfer
)
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
;
80 int64_t begin
= svga_get_time(svga
);
82 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_BUFFERTRANSFERMAP
);
86 assert(box
->height
== 1);
87 assert(box
->depth
== 1);
89 transfer
= MALLOC_STRUCT(pipe_transfer
);
94 transfer
->resource
= resource
;
95 transfer
->level
= level
;
96 transfer
->usage
= usage
;
99 transfer
->layer_stride
= 0;
101 if (usage
& PIPE_TRANSFER_WRITE
) {
102 /* If we write to the buffer for any reason, free any saved translated
105 pipe_resource_reference(&sbuf
->translated_indices
.buffer
, NULL
);
108 if ((usage
& PIPE_TRANSFER_READ
) && sbuf
->dirty
) {
111 /* Host-side buffers can only be dirtied with vgpu10 features
112 * (streamout and buffer copy).
114 assert(svga_have_vgpu10(svga
));
117 (void) svga_buffer_handle(svga
, resource
, sbuf
->bind_flags
);
120 if (sbuf
->dma
.pending
> 0) {
121 svga_buffer_upload_flush(svga
, sbuf
);
122 svga_context_finish(svga
);
125 assert(sbuf
->handle
);
127 ret
= SVGA3D_vgpu10_ReadbackSubResource(svga
->swc
, sbuf
->handle
, 0);
128 if (ret
!= PIPE_OK
) {
129 svga_context_flush(svga
, NULL
);
130 ret
= SVGA3D_vgpu10_ReadbackSubResource(svga
->swc
, sbuf
->handle
, 0);
131 assert(ret
== PIPE_OK
);
134 svga
->hud
.num_readbacks
++;
136 svga_context_finish(svga
);
141 if (usage
& PIPE_TRANSFER_WRITE
) {
142 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
144 * Flush any pending primitives, finish writing any pending DMA
145 * commands, and tell the host to discard the buffer contents on
146 * the next DMA operation.
149 svga_hwtnl_flush_buffer(svga
, resource
);
151 if (sbuf
->dma
.pending
) {
152 svga_buffer_upload_flush(svga
, sbuf
);
155 * Instead of flushing the context command buffer, simply discard
156 * the current hwbuf, and start a new one.
157 * With GB objects, the map operation takes care of this
158 * if passed the PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE flag,
159 * and the old backing store is busy.
162 if (!svga_have_gb_objects(svga
))
163 svga_buffer_destroy_hw_storage(ss
, sbuf
);
166 sbuf
->map
.num_ranges
= 0;
167 sbuf
->dma
.flags
.discard
= TRUE
;
170 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
171 if (!sbuf
->map
.num_ranges
) {
173 * No pending ranges to upload so far, so we can tell the host to
174 * not synchronize on the next DMA command.
177 sbuf
->dma
.flags
.unsynchronized
= TRUE
;
181 * Synchronizing, so flush any pending primitives, finish writing any
182 * pending DMA command, and ensure the next DMA will be done in order.
185 svga_hwtnl_flush_buffer(svga
, resource
);
187 if (sbuf
->dma
.pending
) {
188 svga_buffer_upload_flush(svga
, sbuf
);
190 if (svga_buffer_has_hw_storage(sbuf
)) {
192 * We have a pending DMA upload from a hardware buffer, therefore
193 * we need to ensure that the host finishes processing that DMA
194 * command before the state tracker can start overwriting the
197 * XXX: This could be avoided by tying the hardware buffer to
198 * the transfer (just as done with textures), which would allow
199 * overlapping DMAs commands to be queued on the same context
200 * buffer. However, due to the likelihood of software vertex
201 * processing, it is more convenient to hold on to the hardware
202 * buffer, allowing to quickly access the contents from the CPU
203 * without having to do a DMA download from the host.
206 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
208 * Flushing the command buffer here will most likely cause
209 * the map of the hwbuf below to block, so preemptively
210 * return NULL here if DONTBLOCK is set to prevent unnecessary
211 * command buffer flushes.
218 svga_context_flush(svga
, NULL
);
222 sbuf
->dma
.flags
.unsynchronized
= FALSE
;
226 if (!sbuf
->swbuf
&& !svga_buffer_has_hw_storage(sbuf
)) {
227 if (svga_buffer_create_hw_storage(ss
, sbuf
, sbuf
->bind_flags
) != PIPE_OK
) {
229 * We can't create a hardware buffer big enough, so create a malloc
233 debug_printf("%s: failed to allocate %u KB of DMA, "
234 "splitting DMA transfers\n",
236 (sbuf
->b
.b
.width0
+ 1023)/1024);
239 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 16);
248 /* User/malloc buffer */
251 else if (svga_buffer_has_hw_storage(sbuf
)) {
254 map
= svga_buffer_hw_storage_map(svga
, sbuf
, transfer
->usage
, &retry
);
255 if (map
== NULL
&& retry
) {
257 * At this point, svga_buffer_get_transfer() has already
258 * hit the DISCARD_WHOLE_RESOURCE path and flushed HWTNL
261 svga_context_flush(svga
, NULL
);
262 map
= svga_buffer_hw_storage_map(svga
, sbuf
, transfer
->usage
, &retry
);
271 map
+= transfer
->box
.x
;
272 *ptransfer
= transfer
;
277 svga
->hud
.map_buffer_time
+= (svga_get_time(svga
) - begin
);
280 SVGA_STATS_TIME_POP(svga_sws(svga
));
286 svga_buffer_transfer_flush_region( struct pipe_context
*pipe
,
287 struct pipe_transfer
*transfer
,
288 const struct pipe_box
*box
)
290 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
291 struct svga_buffer
*sbuf
= svga_buffer(transfer
->resource
);
293 unsigned offset
= transfer
->box
.x
+ box
->x
;
294 unsigned length
= box
->width
;
296 assert(transfer
->usage
& PIPE_TRANSFER_WRITE
);
297 assert(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
);
299 mtx_lock(&ss
->swc_mutex
);
300 svga_buffer_add_range(sbuf
, offset
, offset
+ length
);
301 mtx_unlock(&ss
->swc_mutex
);
306 svga_buffer_transfer_unmap( struct pipe_context
*pipe
,
307 struct pipe_transfer
*transfer
)
309 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
310 struct svga_context
*svga
= svga_context(pipe
);
311 struct svga_buffer
*sbuf
= svga_buffer(transfer
->resource
);
313 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_BUFFERTRANSFERUNMAP
);
315 mtx_lock(&ss
->swc_mutex
);
317 assert(sbuf
->map
.count
);
318 if (sbuf
->map
.count
) {
322 if (svga_buffer_has_hw_storage(sbuf
)) {
323 /* Note: we may wind up flushing here and unmapping other buffers
324 * which leads to recursively locking ss->swc_mutex.
326 svga_buffer_hw_storage_unmap(svga
, sbuf
);
329 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
330 if (!(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
)) {
332 * Mapped range not flushed explicitly, so flush the whole buffer,
333 * and tell the host to discard the contents when processing the DMA
337 SVGA_DBG(DEBUG_DMA
, "flushing the whole buffer\n");
339 sbuf
->dma
.flags
.discard
= TRUE
;
341 svga_buffer_add_range(sbuf
, 0, sbuf
->b
.b
.width0
);
345 mtx_unlock(&ss
->swc_mutex
);
347 SVGA_STATS_TIME_POP(svga_sws(svga
));
352 svga_buffer_destroy( struct pipe_screen
*screen
,
353 struct pipe_resource
*buf
)
355 struct svga_screen
*ss
= svga_screen(screen
);
356 struct svga_buffer
*sbuf
= svga_buffer( buf
);
358 assert(!p_atomic_read(&buf
->reference
.count
));
360 assert(!sbuf
->dma
.pending
);
363 svga_buffer_destroy_host_surface(ss
, sbuf
);
365 if (sbuf
->uploaded
.buffer
)
366 pipe_resource_reference(&sbuf
->uploaded
.buffer
, NULL
);
369 svga_buffer_destroy_hw_storage(ss
, sbuf
);
371 if (sbuf
->swbuf
&& !sbuf
->user
)
372 align_free(sbuf
->swbuf
);
374 pipe_resource_reference(&sbuf
->translated_indices
.buffer
, NULL
);
376 ss
->hud
.total_resource_bytes
-= sbuf
->size
;
377 assert(ss
->hud
.num_resources
> 0);
378 if (ss
->hud
.num_resources
> 0)
379 ss
->hud
.num_resources
--;
385 struct u_resource_vtbl svga_buffer_vtbl
=
387 u_default_resource_get_handle
, /* get_handle */
388 svga_buffer_destroy
, /* resource_destroy */
389 svga_buffer_transfer_map
, /* transfer_map */
390 svga_buffer_transfer_flush_region
, /* transfer_flush_region */
391 svga_buffer_transfer_unmap
, /* transfer_unmap */
396 struct pipe_resource
*
397 svga_buffer_create(struct pipe_screen
*screen
,
398 const struct pipe_resource
*template)
400 struct svga_screen
*ss
= svga_screen(screen
);
401 struct svga_buffer
*sbuf
;
404 SVGA_STATS_TIME_PUSH(ss
->sws
, SVGA_STATS_TIME_CREATEBUFFER
);
406 sbuf
= CALLOC_STRUCT(svga_buffer
);
410 sbuf
->b
.b
= *template;
411 sbuf
->b
.vtbl
= &svga_buffer_vtbl
;
412 pipe_reference_init(&sbuf
->b
.b
.reference
, 1);
413 sbuf
->b
.b
.screen
= screen
;
414 bind_flags
= template->bind
;
416 LIST_INITHEAD(&sbuf
->surfaces
);
418 if (bind_flags
& PIPE_BIND_CONSTANT_BUFFER
) {
419 /* Constant buffers can only have the PIPE_BIND_CONSTANT_BUFFER
422 if (ss
->sws
->have_vgpu10
) {
423 bind_flags
= PIPE_BIND_CONSTANT_BUFFER
;
427 /* Although svga device only requires constant buffer size to be
428 * in multiples of 16, in order to allow bind_flags promotion,
429 * we are mandating all buffer size to be in multiples of 16.
431 sbuf
->b
.b
.width0
= align(sbuf
->b
.b
.width0
, 16);
433 if (svga_buffer_needs_hw_storage(bind_flags
)) {
435 /* If the buffer is not used for constant buffer, set
436 * the vertex/index bind flags as well so that the buffer will be
437 * accepted for those uses.
438 * Note that the PIPE_BIND_ flags we get from the state tracker are
439 * just a hint about how the buffer may be used. And OpenGL buffer
440 * object may be used for many different things.
441 * Also note that we do not unconditionally set the streamout
442 * bind flag since streamout buffer is an output buffer and
443 * might have performance implication.
445 if (!(template->bind
& PIPE_BIND_CONSTANT_BUFFER
)) {
446 /* Not a constant buffer. The buffer may be used for vertex data
449 bind_flags
|= (PIPE_BIND_VERTEX_BUFFER
|
450 PIPE_BIND_INDEX_BUFFER
);
453 if (svga_buffer_create_host_surface(ss
, sbuf
, bind_flags
) != PIPE_OK
)
457 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 64);
462 debug_reference(&sbuf
->b
.b
.reference
,
463 (debug_reference_descriptor
)debug_describe_resource
, 0);
465 sbuf
->bind_flags
= bind_flags
;
466 sbuf
->size
= util_resource_size(&sbuf
->b
.b
);
467 ss
->hud
.total_resource_bytes
+= sbuf
->size
;
469 ss
->hud
.num_resources
++;
470 SVGA_STATS_TIME_POP(ss
->sws
);
477 SVGA_STATS_TIME_POP(ss
->sws
);
482 struct pipe_resource
*
483 svga_user_buffer_create(struct pipe_screen
*screen
,
488 struct svga_buffer
*sbuf
;
489 struct svga_screen
*ss
= svga_screen(screen
);
491 sbuf
= CALLOC_STRUCT(svga_buffer
);
495 pipe_reference_init(&sbuf
->b
.b
.reference
, 1);
496 sbuf
->b
.vtbl
= &svga_buffer_vtbl
;
497 sbuf
->b
.b
.screen
= screen
;
498 sbuf
->b
.b
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
499 sbuf
->b
.b
.usage
= PIPE_USAGE_IMMUTABLE
;
500 sbuf
->b
.b
.bind
= bind
;
501 sbuf
->b
.b
.width0
= bytes
;
502 sbuf
->b
.b
.height0
= 1;
503 sbuf
->b
.b
.depth0
= 1;
504 sbuf
->b
.b
.array_size
= 1;
506 sbuf
->bind_flags
= bind
;
510 debug_reference(&sbuf
->b
.b
.reference
,
511 (debug_reference_descriptor
)debug_describe_resource
, 0);
513 ss
->hud
.num_resources
++;