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 "os/os_time.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35 #include "util/u_resource.h"
37 #include "svga_context.h"
38 #include "svga_screen.h"
39 #include "svga_resource_buffer.h"
40 #include "svga_resource_buffer_upload.h"
41 #include "svga_winsys.h"
42 #include "svga_debug.h"
46 * Vertex and index buffers need hardware backing. Constant buffers
47 * do not. No other types of buffers currently supported.
50 svga_buffer_needs_hw_storage(unsigned usage
)
52 return (usage
& (PIPE_BIND_VERTEX_BUFFER
| PIPE_BIND_INDEX_BUFFER
|
53 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_STREAM_OUTPUT
)) != 0;
58 * Create a buffer transfer.
60 * Unlike texture DMAs (which are written immediately to the command buffer and
61 * therefore inherently serialized with other context operations), for buffers
62 * we try to coalesce multiple range mappings (i.e, multiple calls to this
63 * function) into a single DMA command, for better efficiency in command
64 * processing. This means we need to exercise extra care here to ensure that
65 * the end result is exactly the same as if one DMA was used for every mapped
69 svga_buffer_transfer_map(struct pipe_context
*pipe
,
70 struct pipe_resource
*resource
,
72 enum pipe_resource_usage usage
,
73 const struct pipe_box
*box
,
74 struct pipe_transfer
**ptransfer
)
76 struct svga_context
*svga
= svga_context(pipe
);
77 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
78 struct svga_buffer
*sbuf
= svga_buffer(resource
);
79 struct pipe_transfer
*transfer
;
81 int64_t begin
= os_time_get();
85 assert(box
->height
== 1);
86 assert(box
->depth
== 1);
88 transfer
= CALLOC_STRUCT(pipe_transfer
);
93 transfer
->resource
= resource
;
94 transfer
->level
= level
;
95 transfer
->usage
= usage
;
98 if ((usage
& PIPE_TRANSFER_READ
) && sbuf
->dirty
) {
99 /* Only need to test for vgpu10 since only vgpu10 features (streamout,
100 * buffer copy) can modify buffers on the device.
102 if (svga_have_vgpu10(svga
)) {
104 assert(sbuf
->handle
);
105 ret
= SVGA3D_vgpu10_ReadbackSubResource(svga
->swc
, sbuf
->handle
, 0);
106 if (ret
!= PIPE_OK
) {
107 svga_context_flush(svga
, NULL
);
108 ret
= SVGA3D_vgpu10_ReadbackSubResource(svga
->swc
, sbuf
->handle
, 0);
109 assert(ret
== PIPE_OK
);
112 svga
->hud
.num_readbacks
++;
114 svga_context_finish(svga
);
120 if (usage
& PIPE_TRANSFER_WRITE
) {
121 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
123 * Flush any pending primitives, finish writing any pending DMA
124 * commands, and tell the host to discard the buffer contents on
125 * the next DMA operation.
128 svga_hwtnl_flush_buffer(svga
, resource
);
130 if (sbuf
->dma
.pending
) {
131 svga_buffer_upload_flush(svga
, sbuf
);
134 * Instead of flushing the context command buffer, simply discard
135 * the current hwbuf, and start a new one.
136 * With GB objects, the map operation takes care of this
137 * if passed the PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE flag,
138 * and the old backing store is busy.
141 if (!svga_have_gb_objects(svga
))
142 svga_buffer_destroy_hw_storage(ss
, sbuf
);
145 sbuf
->map
.num_ranges
= 0;
146 sbuf
->dma
.flags
.discard
= TRUE
;
149 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
150 if (!sbuf
->map
.num_ranges
) {
152 * No pending ranges to upload so far, so we can tell the host to
153 * not synchronize on the next DMA command.
156 sbuf
->dma
.flags
.unsynchronized
= TRUE
;
160 * Synchronizing, so flush any pending primitives, finish writing any
161 * pending DMA command, and ensure the next DMA will be done in order.
164 svga_hwtnl_flush_buffer(svga
, resource
);
166 if (sbuf
->dma
.pending
) {
167 svga_buffer_upload_flush(svga
, sbuf
);
169 if (svga_buffer_has_hw_storage(sbuf
)) {
171 * We have a pending DMA upload from a hardware buffer, therefore
172 * we need to ensure that the host finishes processing that DMA
173 * command before the state tracker can start overwriting the
176 * XXX: This could be avoided by tying the hardware buffer to
177 * the transfer (just as done with textures), which would allow
178 * overlapping DMAs commands to be queued on the same context
179 * buffer. However, due to the likelihood of software vertex
180 * processing, it is more convenient to hold on to the hardware
181 * buffer, allowing to quickly access the contents from the CPU
182 * without having to do a DMA download from the host.
185 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
187 * Flushing the command buffer here will most likely cause
188 * the map of the hwbuf below to block, so preemptively
189 * return NULL here if DONTBLOCK is set to prevent unnecessary
190 * command buffer flushes.
197 svga_context_flush(svga
, NULL
);
201 sbuf
->dma
.flags
.unsynchronized
= FALSE
;
205 if (!sbuf
->swbuf
&& !svga_buffer_has_hw_storage(sbuf
)) {
206 if (svga_buffer_create_hw_storage(ss
, sbuf
) != PIPE_OK
) {
208 * We can't create a hardware buffer big enough, so create a malloc
212 debug_printf("%s: failed to allocate %u KB of DMA, "
213 "splitting DMA transfers\n",
215 (sbuf
->b
.b
.width0
+ 1023)/1024);
218 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 16);
227 /* User/malloc buffer */
230 else if (svga_buffer_has_hw_storage(sbuf
)) {
233 map
= svga_buffer_hw_storage_map(svga
, sbuf
, transfer
->usage
, &retry
);
234 if (map
== NULL
&& retry
) {
236 * At this point, svga_buffer_get_transfer() has already
237 * hit the DISCARD_WHOLE_RESOURCE path and flushed HWTNL
240 svga_context_flush(svga
, NULL
);
241 map
= svga_buffer_hw_storage_map(svga
, sbuf
, transfer
->usage
, &retry
);
250 map
+= transfer
->box
.x
;
251 *ptransfer
= transfer
;
256 svga
->hud
.map_buffer_time
+= (os_time_get() - begin
);
263 svga_buffer_transfer_flush_region( struct pipe_context
*pipe
,
264 struct pipe_transfer
*transfer
,
265 const struct pipe_box
*box
)
267 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
268 struct svga_buffer
*sbuf
= svga_buffer(transfer
->resource
);
270 unsigned offset
= transfer
->box
.x
+ box
->x
;
271 unsigned length
= box
->width
;
273 assert(transfer
->usage
& PIPE_TRANSFER_WRITE
);
274 assert(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
);
276 pipe_mutex_lock(ss
->swc_mutex
);
277 svga_buffer_add_range(sbuf
, offset
, offset
+ length
);
278 pipe_mutex_unlock(ss
->swc_mutex
);
283 svga_buffer_transfer_unmap( struct pipe_context
*pipe
,
284 struct pipe_transfer
*transfer
)
286 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
287 struct svga_context
*svga
= svga_context(pipe
);
288 struct svga_buffer
*sbuf
= svga_buffer(transfer
->resource
);
290 pipe_mutex_lock(ss
->swc_mutex
);
292 assert(sbuf
->map
.count
);
293 if (sbuf
->map
.count
) {
297 if (svga_buffer_has_hw_storage(sbuf
)) {
298 svga_buffer_hw_storage_unmap(svga
, sbuf
);
301 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
302 if (!(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
)) {
304 * Mapped range not flushed explicitly, so flush the whole buffer,
305 * and tell the host to discard the contents when processing the DMA
309 SVGA_DBG(DEBUG_DMA
, "flushing the whole buffer\n");
311 sbuf
->dma
.flags
.discard
= TRUE
;
313 svga_buffer_add_range(sbuf
, 0, sbuf
->b
.b
.width0
);
317 pipe_mutex_unlock(ss
->swc_mutex
);
323 svga_buffer_destroy( struct pipe_screen
*screen
,
324 struct pipe_resource
*buf
)
326 struct svga_screen
*ss
= svga_screen(screen
);
327 struct svga_buffer
*sbuf
= svga_buffer( buf
);
329 assert(!p_atomic_read(&buf
->reference
.count
));
331 assert(!sbuf
->dma
.pending
);
334 svga_buffer_destroy_host_surface(ss
, sbuf
);
336 if (sbuf
->uploaded
.buffer
)
337 pipe_resource_reference(&sbuf
->uploaded
.buffer
, NULL
);
340 svga_buffer_destroy_hw_storage(ss
, sbuf
);
342 if (sbuf
->swbuf
&& !sbuf
->user
)
343 align_free(sbuf
->swbuf
);
345 ss
->hud
.total_resource_bytes
-= sbuf
->size
;
346 assert(ss
->hud
.num_resources
> 0);
347 if (ss
->hud
.num_resources
> 0)
348 ss
->hud
.num_resources
--;
354 struct u_resource_vtbl svga_buffer_vtbl
=
356 u_default_resource_get_handle
, /* get_handle */
357 svga_buffer_destroy
, /* resource_destroy */
358 svga_buffer_transfer_map
, /* transfer_map */
359 svga_buffer_transfer_flush_region
, /* transfer_flush_region */
360 svga_buffer_transfer_unmap
, /* transfer_unmap */
361 u_default_transfer_inline_write
/* transfer_inline_write */
366 struct pipe_resource
*
367 svga_buffer_create(struct pipe_screen
*screen
,
368 const struct pipe_resource
*template)
370 struct svga_screen
*ss
= svga_screen(screen
);
371 struct svga_buffer
*sbuf
;
373 sbuf
= CALLOC_STRUCT(svga_buffer
);
377 sbuf
->b
.b
= *template;
378 sbuf
->b
.vtbl
= &svga_buffer_vtbl
;
379 pipe_reference_init(&sbuf
->b
.b
.reference
, 1);
380 sbuf
->b
.b
.screen
= screen
;
381 sbuf
->bind_flags
= template->bind
;
383 if (template->bind
& PIPE_BIND_CONSTANT_BUFFER
) {
384 /* Constant buffers can only have the PIPE_BIND_CONSTANT_BUFFER
387 if (ss
->sws
->have_vgpu10
) {
388 sbuf
->bind_flags
= PIPE_BIND_CONSTANT_BUFFER
;
390 /* Constant buffer size needs to be in multiples of 16. */
391 sbuf
->b
.b
.width0
= align(sbuf
->b
.b
.width0
, 16);
395 if (svga_buffer_needs_hw_storage(template->bind
)) {
397 /* If the buffer will be used for vertex/index/stream data, set all
398 * the flags so that the buffer will be accepted for all those uses.
399 * Note that the PIPE_BIND_ flags we get from the state tracker are
400 * just a hint about how the buffer may be used. And OpenGL buffer
401 * object may be used for many different things.
403 if (!(template->bind
& PIPE_BIND_CONSTANT_BUFFER
)) {
404 /* Not a constant buffer. The buffer may be used for vertex data,
405 * indexes or stream-out.
407 sbuf
->bind_flags
|= (PIPE_BIND_VERTEX_BUFFER
|
408 PIPE_BIND_INDEX_BUFFER
);
409 if (ss
->sws
->have_vgpu10
)
410 sbuf
->bind_flags
|= PIPE_BIND_STREAM_OUTPUT
;
413 if (svga_buffer_create_host_surface(ss
, sbuf
) != PIPE_OK
)
417 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 64);
422 debug_reference(&sbuf
->b
.b
.reference
,
423 (debug_reference_descriptor
)debug_describe_resource
, 0);
425 sbuf
->size
= util_resource_size(&sbuf
->b
.b
);
426 ss
->hud
.total_resource_bytes
+= sbuf
->size
;
428 ss
->hud
.num_resources
++;
439 struct pipe_resource
*
440 svga_user_buffer_create(struct pipe_screen
*screen
,
445 struct svga_buffer
*sbuf
;
446 struct svga_screen
*ss
= svga_screen(screen
);
448 sbuf
= CALLOC_STRUCT(svga_buffer
);
452 pipe_reference_init(&sbuf
->b
.b
.reference
, 1);
453 sbuf
->b
.vtbl
= &svga_buffer_vtbl
;
454 sbuf
->b
.b
.screen
= screen
;
455 sbuf
->b
.b
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
456 sbuf
->b
.b
.usage
= PIPE_USAGE_IMMUTABLE
;
457 sbuf
->b
.b
.bind
= bind
;
458 sbuf
->b
.b
.width0
= bytes
;
459 sbuf
->b
.b
.height0
= 1;
460 sbuf
->b
.b
.depth0
= 1;
461 sbuf
->b
.b
.array_size
= 1;
463 sbuf
->bind_flags
= bind
;
467 debug_reference(&sbuf
->b
.b
.reference
,
468 (debug_reference_descriptor
)debug_describe_resource
, 0);
470 ss
->hud
.num_resources
++;