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_READ
) && sbuf
->dirty
) {
104 /* Host-side buffers can only be dirtied with vgpu10 features
105 * (streamout and buffer copy).
107 assert(svga_have_vgpu10(svga
));
110 (void) svga_buffer_handle(svga
, resource
);
113 if (sbuf
->dma
.pending
> 0) {
114 svga_buffer_upload_flush(svga
, sbuf
);
115 svga_context_finish(svga
);
118 assert(sbuf
->handle
);
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
);
127 svga
->hud
.num_readbacks
++;
129 svga_context_finish(svga
);
134 if (usage
& PIPE_TRANSFER_WRITE
) {
135 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
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.
142 svga_hwtnl_flush_buffer(svga
, resource
);
144 if (sbuf
->dma
.pending
) {
145 svga_buffer_upload_flush(svga
, sbuf
);
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.
155 if (!svga_have_gb_objects(svga
))
156 svga_buffer_destroy_hw_storage(ss
, sbuf
);
159 sbuf
->map
.num_ranges
= 0;
160 sbuf
->dma
.flags
.discard
= TRUE
;
163 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
164 if (!sbuf
->map
.num_ranges
) {
166 * No pending ranges to upload so far, so we can tell the host to
167 * not synchronize on the next DMA command.
170 sbuf
->dma
.flags
.unsynchronized
= TRUE
;
174 * Synchronizing, so flush any pending primitives, finish writing any
175 * pending DMA command, and ensure the next DMA will be done in order.
178 svga_hwtnl_flush_buffer(svga
, resource
);
180 if (sbuf
->dma
.pending
) {
181 svga_buffer_upload_flush(svga
, sbuf
);
183 if (svga_buffer_has_hw_storage(sbuf
)) {
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
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.
199 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
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.
211 svga_context_flush(svga
, NULL
);
215 sbuf
->dma
.flags
.unsynchronized
= FALSE
;
219 if (!sbuf
->swbuf
&& !svga_buffer_has_hw_storage(sbuf
)) {
220 if (svga_buffer_create_hw_storage(ss
, sbuf
) != PIPE_OK
) {
222 * We can't create a hardware buffer big enough, so create a malloc
226 debug_printf("%s: failed to allocate %u KB of DMA, "
227 "splitting DMA transfers\n",
229 (sbuf
->b
.b
.width0
+ 1023)/1024);
232 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 16);
241 /* User/malloc buffer */
244 else if (svga_buffer_has_hw_storage(sbuf
)) {
247 map
= svga_buffer_hw_storage_map(svga
, sbuf
, transfer
->usage
, &retry
);
248 if (map
== NULL
&& retry
) {
250 * At this point, svga_buffer_get_transfer() has already
251 * hit the DISCARD_WHOLE_RESOURCE path and flushed HWTNL
254 svga_context_flush(svga
, NULL
);
255 map
= svga_buffer_hw_storage_map(svga
, sbuf
, transfer
->usage
, &retry
);
264 map
+= transfer
->box
.x
;
265 *ptransfer
= transfer
;
270 svga
->hud
.map_buffer_time
+= (svga_get_time(svga
) - begin
);
273 SVGA_STATS_TIME_POP(svga_sws(svga
));
279 svga_buffer_transfer_flush_region( struct pipe_context
*pipe
,
280 struct pipe_transfer
*transfer
,
281 const struct pipe_box
*box
)
283 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
284 struct svga_buffer
*sbuf
= svga_buffer(transfer
->resource
);
286 unsigned offset
= transfer
->box
.x
+ box
->x
;
287 unsigned length
= box
->width
;
289 assert(transfer
->usage
& PIPE_TRANSFER_WRITE
);
290 assert(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
);
292 pipe_mutex_lock(ss
->swc_mutex
);
293 svga_buffer_add_range(sbuf
, offset
, offset
+ length
);
294 pipe_mutex_unlock(ss
->swc_mutex
);
299 svga_buffer_transfer_unmap( struct pipe_context
*pipe
,
300 struct pipe_transfer
*transfer
)
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
);
306 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_BUFFERTRANSFERUNMAP
);
308 pipe_mutex_lock(ss
->swc_mutex
);
310 assert(sbuf
->map
.count
);
311 if (sbuf
->map
.count
) {
315 if (svga_buffer_has_hw_storage(sbuf
)) {
316 svga_buffer_hw_storage_unmap(svga
, sbuf
);
319 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
320 if (!(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
)) {
322 * Mapped range not flushed explicitly, so flush the whole buffer,
323 * and tell the host to discard the contents when processing the DMA
327 SVGA_DBG(DEBUG_DMA
, "flushing the whole buffer\n");
329 sbuf
->dma
.flags
.discard
= TRUE
;
331 svga_buffer_add_range(sbuf
, 0, sbuf
->b
.b
.width0
);
335 pipe_mutex_unlock(ss
->swc_mutex
);
337 SVGA_STATS_TIME_POP(svga_sws(svga
));
342 svga_buffer_destroy( struct pipe_screen
*screen
,
343 struct pipe_resource
*buf
)
345 struct svga_screen
*ss
= svga_screen(screen
);
346 struct svga_buffer
*sbuf
= svga_buffer( buf
);
348 assert(!p_atomic_read(&buf
->reference
.count
));
350 assert(!sbuf
->dma
.pending
);
353 svga_buffer_destroy_host_surface(ss
, sbuf
);
355 if (sbuf
->uploaded
.buffer
)
356 pipe_resource_reference(&sbuf
->uploaded
.buffer
, NULL
);
359 svga_buffer_destroy_hw_storage(ss
, sbuf
);
361 if (sbuf
->swbuf
&& !sbuf
->user
)
362 align_free(sbuf
->swbuf
);
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
--;
373 struct u_resource_vtbl svga_buffer_vtbl
=
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 */
384 struct pipe_resource
*
385 svga_buffer_create(struct pipe_screen
*screen
,
386 const struct pipe_resource
*template)
388 struct svga_screen
*ss
= svga_screen(screen
);
389 struct svga_buffer
*sbuf
;
391 SVGA_STATS_TIME_PUSH(ss
->sws
, SVGA_STATS_TIME_CREATEBUFFER
);
393 sbuf
= CALLOC_STRUCT(svga_buffer
);
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
;
403 if (template->bind
& PIPE_BIND_CONSTANT_BUFFER
) {
404 /* Constant buffers can only have the PIPE_BIND_CONSTANT_BUFFER
407 if (ss
->sws
->have_vgpu10
) {
408 sbuf
->bind_flags
= PIPE_BIND_CONSTANT_BUFFER
;
410 /* Constant buffer size needs to be in multiples of 16. */
411 sbuf
->b
.b
.width0
= align(sbuf
->b
.b
.width0
, 16);
415 if (svga_buffer_needs_hw_storage(template->bind
)) {
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.
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.
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
;
433 if (svga_buffer_create_host_surface(ss
, sbuf
) != PIPE_OK
)
437 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 64);
442 debug_reference(&sbuf
->b
.b
.reference
,
443 (debug_reference_descriptor
)debug_describe_resource
, 0);
445 sbuf
->size
= util_resource_size(&sbuf
->b
.b
);
446 ss
->hud
.total_resource_bytes
+= sbuf
->size
;
448 ss
->hud
.num_resources
++;
449 SVGA_STATS_TIME_POP(ss
->sws
);
456 SVGA_STATS_TIME_POP(ss
->sws
);
461 struct pipe_resource
*
462 svga_user_buffer_create(struct pipe_screen
*screen
,
467 struct svga_buffer
*sbuf
;
468 struct svga_screen
*ss
= svga_screen(screen
);
470 sbuf
= CALLOC_STRUCT(svga_buffer
);
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;
485 sbuf
->bind_flags
= bind
;
489 debug_reference(&sbuf
->b
.b
.reference
,
490 (debug_reference_descriptor
)debug_describe_resource
, 0);
492 ss
->hud
.num_resources
++;