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
,
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_context_finish(svga
);
118 if (usage
& PIPE_TRANSFER_WRITE
) {
119 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
121 * Flush any pending primitives, finish writing any pending DMA
122 * commands, and tell the host to discard the buffer contents on
123 * the next DMA operation.
126 svga_hwtnl_flush_buffer(svga
, resource
);
128 if (sbuf
->dma
.pending
) {
129 svga_buffer_upload_flush(svga
, sbuf
);
132 * Instead of flushing the context command buffer, simply discard
133 * the current hwbuf, and start a new one.
134 * With GB objects, the map operation takes care of this
135 * if passed the PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE flag,
136 * and the old backing store is busy.
139 if (!svga_have_gb_objects(svga
))
140 svga_buffer_destroy_hw_storage(ss
, sbuf
);
143 sbuf
->map
.num_ranges
= 0;
144 sbuf
->dma
.flags
.discard
= TRUE
;
147 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
148 if (!sbuf
->map
.num_ranges
) {
150 * No pending ranges to upload so far, so we can tell the host to
151 * not synchronize on the next DMA command.
154 sbuf
->dma
.flags
.unsynchronized
= TRUE
;
158 * Synchronizing, so flush any pending primitives, finish writing any
159 * pending DMA command, and ensure the next DMA will be done in order.
162 svga_hwtnl_flush_buffer(svga
, resource
);
164 if (sbuf
->dma
.pending
) {
165 svga_buffer_upload_flush(svga
, sbuf
);
167 if (svga_buffer_has_hw_storage(sbuf
)) {
169 * We have a pending DMA upload from a hardware buffer, therefore
170 * we need to ensure that the host finishes processing that DMA
171 * command before the state tracker can start overwriting the
174 * XXX: This could be avoided by tying the hardware buffer to
175 * the transfer (just as done with textures), which would allow
176 * overlapping DMAs commands to be queued on the same context
177 * buffer. However, due to the likelihood of software vertex
178 * processing, it is more convenient to hold on to the hardware
179 * buffer, allowing to quickly access the contents from the CPU
180 * without having to do a DMA download from the host.
183 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
185 * Flushing the command buffer here will most likely cause
186 * the map of the hwbuf below to block, so preemptively
187 * return NULL here if DONTBLOCK is set to prevent unnecessary
188 * command buffer flushes.
195 svga_context_flush(svga
, NULL
);
199 sbuf
->dma
.flags
.unsynchronized
= FALSE
;
203 if (!sbuf
->swbuf
&& !svga_buffer_has_hw_storage(sbuf
)) {
204 if (svga_buffer_create_hw_storage(ss
, sbuf
) != PIPE_OK
) {
206 * We can't create a hardware buffer big enough, so create a malloc
210 debug_printf("%s: failed to allocate %u KB of DMA, "
211 "splitting DMA transfers\n",
213 (sbuf
->b
.b
.width0
+ 1023)/1024);
216 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 16);
225 /* User/malloc buffer */
228 else if (svga_buffer_has_hw_storage(sbuf
)) {
231 map
= svga_buffer_hw_storage_map(svga
, sbuf
, transfer
->usage
, &retry
);
232 if (map
== NULL
&& retry
) {
234 * At this point, svga_buffer_get_transfer() has already
235 * hit the DISCARD_WHOLE_RESOURCE path and flushed HWTNL
238 svga_context_flush(svga
, NULL
);
239 map
= svga_buffer_hw_storage_map(svga
, sbuf
, transfer
->usage
, &retry
);
248 map
+= transfer
->box
.x
;
249 *ptransfer
= transfer
;
254 svga
->hud
.map_buffer_time
+= (os_time_get() - begin
);
261 svga_buffer_transfer_flush_region( struct pipe_context
*pipe
,
262 struct pipe_transfer
*transfer
,
263 const struct pipe_box
*box
)
265 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
266 struct svga_buffer
*sbuf
= svga_buffer(transfer
->resource
);
268 unsigned offset
= transfer
->box
.x
+ box
->x
;
269 unsigned length
= box
->width
;
271 assert(transfer
->usage
& PIPE_TRANSFER_WRITE
);
272 assert(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
);
274 pipe_mutex_lock(ss
->swc_mutex
);
275 svga_buffer_add_range(sbuf
, offset
, offset
+ length
);
276 pipe_mutex_unlock(ss
->swc_mutex
);
281 svga_buffer_transfer_unmap( struct pipe_context
*pipe
,
282 struct pipe_transfer
*transfer
)
284 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
285 struct svga_context
*svga
= svga_context(pipe
);
286 struct svga_buffer
*sbuf
= svga_buffer(transfer
->resource
);
288 pipe_mutex_lock(ss
->swc_mutex
);
290 assert(sbuf
->map
.count
);
291 if (sbuf
->map
.count
) {
295 if (svga_buffer_has_hw_storage(sbuf
)) {
296 svga_buffer_hw_storage_unmap(svga
, sbuf
);
299 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
300 if (!(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
)) {
302 * Mapped range not flushed explicitly, so flush the whole buffer,
303 * and tell the host to discard the contents when processing the DMA
307 SVGA_DBG(DEBUG_DMA
, "flushing the whole buffer\n");
309 sbuf
->dma
.flags
.discard
= TRUE
;
311 svga_buffer_add_range(sbuf
, 0, sbuf
->b
.b
.width0
);
315 pipe_mutex_unlock(ss
->swc_mutex
);
321 svga_buffer_destroy( struct pipe_screen
*screen
,
322 struct pipe_resource
*buf
)
324 struct svga_screen
*ss
= svga_screen(screen
);
325 struct svga_buffer
*sbuf
= svga_buffer( buf
);
327 assert(!p_atomic_read(&buf
->reference
.count
));
329 assert(!sbuf
->dma
.pending
);
332 svga_buffer_destroy_host_surface(ss
, sbuf
);
334 if (sbuf
->uploaded
.buffer
)
335 pipe_resource_reference(&sbuf
->uploaded
.buffer
, NULL
);
338 svga_buffer_destroy_hw_storage(ss
, sbuf
);
340 if (sbuf
->swbuf
&& !sbuf
->user
)
341 align_free(sbuf
->swbuf
);
343 ss
->hud
.total_resource_bytes
-= sbuf
->size
;
344 assert(ss
->hud
.num_resources
> 0);
345 if (ss
->hud
.num_resources
> 0)
346 ss
->hud
.num_resources
--;
352 struct u_resource_vtbl svga_buffer_vtbl
=
354 u_default_resource_get_handle
, /* get_handle */
355 svga_buffer_destroy
, /* resource_destroy */
356 svga_buffer_transfer_map
, /* transfer_map */
357 svga_buffer_transfer_flush_region
, /* transfer_flush_region */
358 svga_buffer_transfer_unmap
, /* transfer_unmap */
359 u_default_transfer_inline_write
/* transfer_inline_write */
364 struct pipe_resource
*
365 svga_buffer_create(struct pipe_screen
*screen
,
366 const struct pipe_resource
*template)
368 struct svga_screen
*ss
= svga_screen(screen
);
369 struct svga_buffer
*sbuf
;
371 sbuf
= CALLOC_STRUCT(svga_buffer
);
375 sbuf
->b
.b
= *template;
376 sbuf
->b
.vtbl
= &svga_buffer_vtbl
;
377 pipe_reference_init(&sbuf
->b
.b
.reference
, 1);
378 sbuf
->b
.b
.screen
= screen
;
379 sbuf
->bind_flags
= template->bind
;
381 if (template->bind
& PIPE_BIND_CONSTANT_BUFFER
) {
382 /* Constant buffers can only have the PIPE_BIND_CONSTANT_BUFFER
385 if (ss
->sws
->have_vgpu10
) {
386 sbuf
->bind_flags
= PIPE_BIND_CONSTANT_BUFFER
;
388 /* Constant buffer size needs to be in multiples of 16. */
389 sbuf
->b
.b
.width0
= align(sbuf
->b
.b
.width0
, 16);
393 if (svga_buffer_needs_hw_storage(template->bind
)) {
395 /* If the buffer will be used for vertex/index/stream data, set all
396 * the flags so that the buffer will be accepted for all those uses.
397 * Note that the PIPE_BIND_ flags we get from the state tracker are
398 * just a hint about how the buffer may be used. And OpenGL buffer
399 * object may be used for many different things.
401 if (!(template->bind
& PIPE_BIND_CONSTANT_BUFFER
)) {
402 /* Not a constant buffer. The buffer may be used for vertex data,
403 * indexes or stream-out.
405 sbuf
->bind_flags
|= (PIPE_BIND_VERTEX_BUFFER
|
406 PIPE_BIND_INDEX_BUFFER
);
407 if (ss
->sws
->have_vgpu10
)
408 sbuf
->bind_flags
|= PIPE_BIND_STREAM_OUTPUT
;
411 if (svga_buffer_create_host_surface(ss
, sbuf
) != PIPE_OK
)
415 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 64);
420 debug_reference(&sbuf
->b
.b
.reference
,
421 (debug_reference_descriptor
)debug_describe_resource
, 0);
423 sbuf
->size
= util_resource_size(&sbuf
->b
.b
);
424 ss
->hud
.total_resource_bytes
+= sbuf
->size
;
426 ss
->hud
.num_resources
++;
437 struct pipe_resource
*
438 svga_user_buffer_create(struct pipe_screen
*screen
,
443 struct svga_buffer
*sbuf
;
444 struct svga_screen
*ss
= svga_screen(screen
);
446 sbuf
= CALLOC_STRUCT(svga_buffer
);
450 pipe_reference_init(&sbuf
->b
.b
.reference
, 1);
451 sbuf
->b
.vtbl
= &svga_buffer_vtbl
;
452 sbuf
->b
.b
.screen
= screen
;
453 sbuf
->b
.b
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
454 sbuf
->b
.b
.usage
= PIPE_USAGE_IMMUTABLE
;
455 sbuf
->b
.b
.bind
= bind
;
456 sbuf
->b
.b
.width0
= bytes
;
457 sbuf
->b
.b
.height0
= 1;
458 sbuf
->b
.b
.depth0
= 1;
459 sbuf
->b
.b
.array_size
= 1;
461 sbuf
->bind_flags
= bind
;
465 debug_reference(&sbuf
->b
.b
.reference
,
466 (debug_reference_descriptor
)debug_describe_resource
, 0);
468 ss
->hud
.num_resources
++;