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 * Determine what buffers eventually need hardware backing.
47 * Vertex- and index buffers need hardware backing. Constant buffers
48 * do on vgpu10. Staging texture-upload buffers do when they are
52 svga_buffer_needs_hw_storage(const struct svga_screen
*ss
,
53 const struct pipe_resource
*template)
55 unsigned bind_mask
= (PIPE_BIND_VERTEX_BUFFER
| PIPE_BIND_INDEX_BUFFER
|
56 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_STREAM_OUTPUT
|
57 PIPE_BIND_SHADER_BUFFER
| PIPE_BIND_COMMAND_ARGS_BUFFER
);
59 if (ss
->sws
->have_vgpu10
) {
61 * Driver-created upload const0- and staging texture upload buffers
62 * tagged with PIPE_BIND_CUSTOM
64 bind_mask
|= PIPE_BIND_CUSTOM
;
66 * Uniform buffer objects.
67 * Don't create hardware storage for state-tracker constant buffers,
68 * because we frequently map them for reading and writing, and
69 * the length of those buffers are always small, so it is better
70 * to just use system memory.
74 if (template->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)
77 return !!(template->bind
& bind_mask
);
81 * Create a buffer transfer.
83 * Unlike texture DMAs (which are written immediately to the command buffer and
84 * therefore inherently serialized with other context operations), for buffers
85 * we try to coalesce multiple range mappings (i.e, multiple calls to this
86 * function) into a single DMA command, for better efficiency in command
87 * processing. This means we need to exercise extra care here to ensure that
88 * the end result is exactly the same as if one DMA was used for every mapped
92 svga_buffer_transfer_map(struct pipe_context
*pipe
,
93 struct pipe_resource
*resource
,
96 const struct pipe_box
*box
,
97 struct pipe_transfer
**ptransfer
)
99 struct svga_context
*svga
= svga_context(pipe
);
100 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
101 struct svga_buffer
*sbuf
= svga_buffer(resource
);
102 struct pipe_transfer
*transfer
;
104 int64_t begin
= svga_get_time(svga
);
106 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_BUFFERTRANSFERMAP
);
110 assert(box
->height
== 1);
111 assert(box
->depth
== 1);
113 transfer
= MALLOC_STRUCT(pipe_transfer
);
118 transfer
->resource
= resource
;
119 transfer
->level
= level
;
120 transfer
->usage
= usage
;
121 transfer
->box
= *box
;
122 transfer
->stride
= 0;
123 transfer
->layer_stride
= 0;
125 if (usage
& PIPE_TRANSFER_WRITE
) {
126 /* If we write to the buffer for any reason, free any saved translated
129 pipe_resource_reference(&sbuf
->translated_indices
.buffer
, NULL
);
132 if ((usage
& PIPE_TRANSFER_READ
) && sbuf
->dirty
&&
133 !sbuf
->key
.coherent
&& !svga
->swc
->force_coherent
) {
135 /* Host-side buffers can only be dirtied with vgpu10 features
136 * (streamout and buffer copy).
138 assert(svga_have_vgpu10(svga
));
141 (void) svga_buffer_handle(svga
, resource
, sbuf
->bind_flags
);
144 if (sbuf
->dma
.pending
) {
145 svga_buffer_upload_flush(svga
, sbuf
);
146 svga_context_finish(svga
);
149 assert(sbuf
->handle
);
151 SVGA_RETRY(svga
, SVGA3D_vgpu10_ReadbackSubResource(svga
->swc
,
153 svga
->hud
.num_readbacks
++;
155 svga_context_finish(svga
);
160 if (usage
& PIPE_TRANSFER_WRITE
) {
161 if ((usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) &&
162 !(resource
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
)) {
164 * Flush any pending primitives, finish writing any pending DMA
165 * commands, and tell the host to discard the buffer contents on
166 * the next DMA operation.
169 svga_hwtnl_flush_buffer(svga
, resource
);
171 if (sbuf
->dma
.pending
) {
172 svga_buffer_upload_flush(svga
, sbuf
);
175 * Instead of flushing the context command buffer, simply discard
176 * the current hwbuf, and start a new one.
177 * With GB objects, the map operation takes care of this
178 * if passed the PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE flag,
179 * and the old backing store is busy.
182 if (!svga_have_gb_objects(svga
))
183 svga_buffer_destroy_hw_storage(ss
, sbuf
);
186 sbuf
->map
.num_ranges
= 0;
187 sbuf
->dma
.flags
.discard
= TRUE
;
190 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
191 if (!sbuf
->map
.num_ranges
) {
193 * No pending ranges to upload so far, so we can tell the host to
194 * not synchronize on the next DMA command.
197 sbuf
->dma
.flags
.unsynchronized
= TRUE
;
201 * Synchronizing, so flush any pending primitives, finish writing any
202 * pending DMA command, and ensure the next DMA will be done in order.
205 svga_hwtnl_flush_buffer(svga
, resource
);
207 if (sbuf
->dma
.pending
) {
208 svga_buffer_upload_flush(svga
, sbuf
);
210 if (svga_buffer_has_hw_storage(sbuf
)) {
212 * We have a pending DMA upload from a hardware buffer, therefore
213 * we need to ensure that the host finishes processing that DMA
214 * command before the gallium frontend can start overwriting the
217 * XXX: This could be avoided by tying the hardware buffer to
218 * the transfer (just as done with textures), which would allow
219 * overlapping DMAs commands to be queued on the same context
220 * buffer. However, due to the likelihood of software vertex
221 * processing, it is more convenient to hold on to the hardware
222 * buffer, allowing to quickly access the contents from the CPU
223 * without having to do a DMA download from the host.
226 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
228 * Flushing the command buffer here will most likely cause
229 * the map of the hwbuf below to block, so preemptively
230 * return NULL here if DONTBLOCK is set to prevent unnecessary
231 * command buffer flushes.
238 svga_context_flush(svga
, NULL
);
242 sbuf
->dma
.flags
.unsynchronized
= FALSE
;
246 if (!sbuf
->swbuf
&& !svga_buffer_has_hw_storage(sbuf
)) {
247 if (svga_buffer_create_hw_storage(ss
, sbuf
, sbuf
->bind_flags
) != PIPE_OK
) {
249 * We can't create a hardware buffer big enough, so create a malloc
253 debug_printf("%s: failed to allocate %u KB of DMA, "
254 "splitting DMA transfers\n",
256 (sbuf
->b
.b
.width0
+ 1023)/1024);
259 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 16);
268 /* User/malloc buffer */
271 else if (svga_buffer_has_hw_storage(sbuf
)) {
274 map
= SVGA_TRY_MAP(svga_buffer_hw_storage_map
275 (svga
, sbuf
, transfer
->usage
, &retry
), retry
);
276 if (map
== NULL
&& retry
) {
278 * At this point, svga_buffer_get_transfer() has already
279 * hit the DISCARD_WHOLE_RESOURCE path and flushed HWTNL
282 svga_retry_enter(svga
);
283 svga_context_flush(svga
, NULL
);
284 map
= svga_buffer_hw_storage_map(svga
, sbuf
, transfer
->usage
, &retry
);
285 svga_retry_exit(svga
);
294 map
+= transfer
->box
.x
;
295 *ptransfer
= transfer
;
300 svga
->hud
.map_buffer_time
+= (svga_get_time(svga
) - begin
);
303 SVGA_STATS_TIME_POP(svga_sws(svga
));
309 svga_buffer_transfer_flush_region(struct pipe_context
*pipe
,
310 struct pipe_transfer
*transfer
,
311 const struct pipe_box
*box
)
313 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
314 struct svga_buffer
*sbuf
= svga_buffer(transfer
->resource
);
315 struct svga_context
*svga
= svga_context(pipe
);
316 unsigned offset
= transfer
->box
.x
+ box
->x
;
317 unsigned length
= box
->width
;
319 assert(transfer
->usage
& PIPE_TRANSFER_WRITE
);
320 assert(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
);
322 if (!(svga
->swc
->force_coherent
|| sbuf
->key
.coherent
) || sbuf
->swbuf
) {
323 mtx_lock(&ss
->swc_mutex
);
324 svga_buffer_add_range(sbuf
, offset
, offset
+ length
);
325 mtx_unlock(&ss
->swc_mutex
);
331 svga_buffer_transfer_unmap(struct pipe_context
*pipe
,
332 struct pipe_transfer
*transfer
)
334 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
335 struct svga_context
*svga
= svga_context(pipe
);
336 struct svga_buffer
*sbuf
= svga_buffer(transfer
->resource
);
338 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_BUFFERTRANSFERUNMAP
);
340 mtx_lock(&ss
->swc_mutex
);
342 assert(sbuf
->map
.count
);
343 if (sbuf
->map
.count
) {
347 if (svga_buffer_has_hw_storage(sbuf
)) {
349 /* Note: we may wind up flushing here and unmapping other buffers
350 * which leads to recursively locking ss->swc_mutex.
352 svga_buffer_hw_storage_unmap(svga
, sbuf
);
355 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
356 if (!(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
)) {
358 * Mapped range not flushed explicitly, so flush the whole buffer,
359 * and tell the host to discard the contents when processing the DMA
363 SVGA_DBG(DEBUG_DMA
, "flushing the whole buffer\n");
365 sbuf
->dma
.flags
.discard
= TRUE
;
367 if (!(svga
->swc
->force_coherent
|| sbuf
->key
.coherent
) || sbuf
->swbuf
)
368 svga_buffer_add_range(sbuf
, 0, sbuf
->b
.b
.width0
);
372 (!sbuf
->bind_flags
|| (sbuf
->bind_flags
& PIPE_BIND_CONSTANT_BUFFER
))) {
374 * Since the constant buffer is in system buffer, we need
375 * to set the constant buffer dirty bits, so that the context
376 * can update the changes in the device.
377 * According to the GL spec, buffer bound to other contexts will
378 * have to be explicitly rebound by the user to have the changes take
381 svga
->dirty
|= SVGA_NEW_CONST_BUFFER
;
385 mtx_unlock(&ss
->swc_mutex
);
387 SVGA_STATS_TIME_POP(svga_sws(svga
));
392 svga_buffer_destroy(struct pipe_screen
*screen
,
393 struct pipe_resource
*buf
)
395 struct svga_screen
*ss
= svga_screen(screen
);
396 struct svga_buffer
*sbuf
= svga_buffer(buf
);
398 assert(!p_atomic_read(&buf
->reference
.count
));
400 assert(!sbuf
->dma
.pending
);
403 svga_buffer_destroy_host_surface(ss
, sbuf
);
405 if (sbuf
->uploaded
.buffer
)
406 pipe_resource_reference(&sbuf
->uploaded
.buffer
, NULL
);
409 svga_buffer_destroy_hw_storage(ss
, sbuf
);
411 if (sbuf
->swbuf
&& !sbuf
->user
)
412 align_free(sbuf
->swbuf
);
414 pipe_resource_reference(&sbuf
->translated_indices
.buffer
, NULL
);
416 ss
->hud
.total_resource_bytes
-= sbuf
->size
;
417 assert(ss
->hud
.num_resources
> 0);
418 if (ss
->hud
.num_resources
> 0)
419 ss
->hud
.num_resources
--;
425 struct u_resource_vtbl svga_buffer_vtbl
=
427 u_default_resource_get_handle
, /* get_handle */
428 svga_buffer_destroy
, /* resource_destroy */
429 svga_buffer_transfer_map
, /* transfer_map */
430 svga_buffer_transfer_flush_region
, /* transfer_flush_region */
431 svga_buffer_transfer_unmap
, /* transfer_unmap */
436 struct pipe_resource
*
437 svga_buffer_create(struct pipe_screen
*screen
,
438 const struct pipe_resource
*template)
440 struct svga_screen
*ss
= svga_screen(screen
);
441 struct svga_buffer
*sbuf
;
444 SVGA_STATS_TIME_PUSH(ss
->sws
, SVGA_STATS_TIME_CREATEBUFFER
);
446 sbuf
= CALLOC_STRUCT(svga_buffer
);
450 sbuf
->b
.b
= *template;
451 sbuf
->b
.vtbl
= &svga_buffer_vtbl
;
452 pipe_reference_init(&sbuf
->b
.b
.reference
, 1);
453 sbuf
->b
.b
.screen
= screen
;
454 bind_flags
= template->bind
& ~PIPE_BIND_CUSTOM
;
456 list_inithead(&sbuf
->surfaces
);
458 if (bind_flags
& PIPE_BIND_CONSTANT_BUFFER
) {
459 /* Constant buffers can only have the PIPE_BIND_CONSTANT_BUFFER
462 if (ss
->sws
->have_vgpu10
) {
463 bind_flags
= PIPE_BIND_CONSTANT_BUFFER
;
467 /* Although svga device only requires constant buffer size to be
468 * in multiples of 16, in order to allow bind_flags promotion,
469 * we are mandating all buffer size to be in multiples of 16.
471 sbuf
->b
.b
.width0
= align(sbuf
->b
.b
.width0
, 16);
473 if (svga_buffer_needs_hw_storage(ss
, template)) {
475 /* If the buffer is not used for constant buffer, set
476 * the vertex/index bind flags as well so that the buffer will be
477 * accepted for those uses.
478 * Note that the PIPE_BIND_ flags we get from the gallium frontend are
479 * just a hint about how the buffer may be used. And OpenGL buffer
480 * object may be used for many different things.
481 * Also note that we do not unconditionally set the streamout
482 * bind flag since streamout buffer is an output buffer and
483 * might have performance implication.
485 if (!(template->bind
& PIPE_BIND_CONSTANT_BUFFER
) &&
486 !(template->bind
& PIPE_BIND_CUSTOM
)) {
487 /* Not a constant- or staging buffer.
488 * The buffer may be used for vertex data or indexes.
490 bind_flags
|= (PIPE_BIND_VERTEX_BUFFER
|
491 PIPE_BIND_INDEX_BUFFER
);
493 /* It may be used for shader resource as well. */
494 bind_flags
|= PIPE_BIND_SAMPLER_VIEW
;
497 if (svga_buffer_create_host_surface(ss
, sbuf
, bind_flags
) != PIPE_OK
)
501 sbuf
->swbuf
= align_malloc(sbuf
->b
.b
.width0
, 64);
505 /* Since constant buffer is usually small, it is much cheaper to
506 * use system memory for the data just as it is being done for
507 * the default constant buffer.
509 if ((bind_flags
& PIPE_BIND_CONSTANT_BUFFER
) || !bind_flags
)
510 sbuf
->use_swbuf
= TRUE
;
513 debug_reference(&sbuf
->b
.b
.reference
,
514 (debug_reference_descriptor
)debug_describe_resource
, 0);
516 sbuf
->bind_flags
= bind_flags
;
517 sbuf
->size
= util_resource_size(&sbuf
->b
.b
);
518 ss
->hud
.total_resource_bytes
+= sbuf
->size
;
520 ss
->hud
.num_resources
++;
521 SVGA_STATS_TIME_POP(ss
->sws
);
528 SVGA_STATS_TIME_POP(ss
->sws
);
533 struct pipe_resource
*
534 svga_user_buffer_create(struct pipe_screen
*screen
,
539 struct svga_buffer
*sbuf
;
540 struct svga_screen
*ss
= svga_screen(screen
);
542 sbuf
= CALLOC_STRUCT(svga_buffer
);
546 pipe_reference_init(&sbuf
->b
.b
.reference
, 1);
547 sbuf
->b
.vtbl
= &svga_buffer_vtbl
;
548 sbuf
->b
.b
.screen
= screen
;
549 sbuf
->b
.b
.format
= PIPE_FORMAT_R8_UNORM
; /* ?? */
550 sbuf
->b
.b
.usage
= PIPE_USAGE_IMMUTABLE
;
551 sbuf
->b
.b
.bind
= bind
;
552 sbuf
->b
.b
.width0
= bytes
;
553 sbuf
->b
.b
.height0
= 1;
554 sbuf
->b
.b
.depth0
= 1;
555 sbuf
->b
.b
.array_size
= 1;
557 sbuf
->bind_flags
= bind
;
561 debug_reference(&sbuf
->b
.b
.reference
,
562 (debug_reference_descriptor
)debug_describe_resource
, 0);
564 ss
->hud
.num_resources
++;