1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 /* Helper utility for uploading user buffers & other data, and
29 * coalescing small buffers into larger ones.
32 #include "pipe/p_defines.h"
33 #include "util/u_inlines.h"
34 #include "pipe/p_context.h"
35 #include "util/u_memory.h"
36 #include "util/u_math.h"
38 #include "u_upload_mgr.h"
42 struct pipe_context
*pipe
;
44 unsigned default_size
; /* Minimum size of the upload buffer, in bytes. */
45 unsigned bind
; /* Bitmask of PIPE_BIND_* flags. */
46 enum pipe_resource_usage usage
;
47 unsigned map_flags
; /* Bitmask of PIPE_TRANSFER_* flags. */
48 boolean map_persistent
; /* If persistent mappings are supported. */
50 struct pipe_resource
*buffer
; /* Upload buffer. */
51 struct pipe_transfer
*transfer
; /* Transfer object for the upload buffer. */
52 uint8_t *map
; /* Pointer to the mapped upload buffer. */
53 unsigned offset
; /* Aligned offset to the upload buffer, pointing
54 * at the first unused byte. */
59 u_upload_create(struct pipe_context
*pipe
, unsigned default_size
,
60 unsigned bind
, enum pipe_resource_usage usage
)
62 struct u_upload_mgr
*upload
= CALLOC_STRUCT(u_upload_mgr
);
67 upload
->default_size
= default_size
;
69 upload
->usage
= usage
;
71 upload
->map_persistent
=
72 pipe
->screen
->get_param(pipe
->screen
,
73 PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT
);
75 if (upload
->map_persistent
) {
76 upload
->map_flags
= PIPE_TRANSFER_WRITE
|
77 PIPE_TRANSFER_UNSYNCHRONIZED
|
78 PIPE_TRANSFER_PERSISTENT
|
79 PIPE_TRANSFER_COHERENT
;
82 upload
->map_flags
= PIPE_TRANSFER_WRITE
|
83 PIPE_TRANSFER_UNSYNCHRONIZED
|
84 PIPE_TRANSFER_FLUSH_EXPLICIT
;
91 u_upload_create_default(struct pipe_context
*pipe
)
93 return u_upload_create(pipe
, 1024 * 1024,
94 PIPE_BIND_VERTEX_BUFFER
|
95 PIPE_BIND_INDEX_BUFFER
|
96 PIPE_BIND_CONSTANT_BUFFER
,
100 struct u_upload_mgr
*
101 u_upload_clone(struct pipe_context
*pipe
, struct u_upload_mgr
*upload
)
103 return u_upload_create(pipe
, upload
->default_size
, upload
->bind
,
108 upload_unmap_internal(struct u_upload_mgr
*upload
, boolean destroying
)
110 if (!destroying
&& upload
->map_persistent
)
113 if (upload
->transfer
) {
114 struct pipe_box
*box
= &upload
->transfer
->box
;
116 if (!upload
->map_persistent
&& (int) upload
->offset
> box
->x
) {
117 pipe_buffer_flush_mapped_range(upload
->pipe
, upload
->transfer
,
118 box
->x
, upload
->offset
- box
->x
);
121 pipe_transfer_unmap(upload
->pipe
, upload
->transfer
);
122 upload
->transfer
= NULL
;
129 u_upload_unmap(struct u_upload_mgr
*upload
)
131 upload_unmap_internal(upload
, FALSE
);
136 u_upload_release_buffer(struct u_upload_mgr
*upload
)
138 /* Unmap and unreference the upload buffer. */
139 upload_unmap_internal(upload
, TRUE
);
140 pipe_resource_reference(&upload
->buffer
, NULL
);
145 u_upload_destroy(struct u_upload_mgr
*upload
)
147 u_upload_release_buffer(upload
);
153 u_upload_alloc_buffer(struct u_upload_mgr
*upload
, unsigned min_size
)
155 struct pipe_screen
*screen
= upload
->pipe
->screen
;
156 struct pipe_resource buffer
;
159 /* Release the old buffer, if present:
161 u_upload_release_buffer(upload
);
163 /* Allocate a new one:
165 size
= align(MAX2(upload
->default_size
, min_size
), 4096);
167 memset(&buffer
, 0, sizeof buffer
);
168 buffer
.target
= PIPE_BUFFER
;
169 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
170 buffer
.bind
= upload
->bind
;
171 buffer
.usage
= upload
->usage
;
172 buffer
.width0
= size
;
175 buffer
.array_size
= 1;
177 if (upload
->map_persistent
) {
178 buffer
.flags
= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
|
179 PIPE_RESOURCE_FLAG_MAP_COHERENT
;
182 upload
->buffer
= screen
->resource_create(screen
, &buffer
);
183 if (upload
->buffer
== NULL
)
186 /* Map the new buffer. */
187 upload
->map
= pipe_buffer_map_range(upload
->pipe
, upload
->buffer
,
188 0, size
, upload
->map_flags
,
190 if (upload
->map
== NULL
) {
191 upload
->transfer
= NULL
;
192 pipe_resource_reference(&upload
->buffer
, NULL
);
200 u_upload_alloc(struct u_upload_mgr
*upload
,
201 unsigned min_out_offset
,
204 unsigned *out_offset
,
205 struct pipe_resource
**outbuf
,
208 unsigned buffer_size
= upload
->buffer
? upload
->buffer
->width0
: 0;
211 min_out_offset
= align(min_out_offset
, alignment
);
213 offset
= align(upload
->offset
, alignment
);
214 offset
= MAX2(offset
, min_out_offset
);
216 /* Make sure we have enough space in the upload buffer
217 * for the sub-allocation.
219 if (unlikely(!upload
->buffer
|| offset
+ size
> buffer_size
)) {
220 u_upload_alloc_buffer(upload
, min_out_offset
+ size
);
222 if (unlikely(!upload
->buffer
)) {
224 pipe_resource_reference(outbuf
, NULL
);
229 offset
= min_out_offset
;
230 buffer_size
= upload
->buffer
->width0
;
233 if (unlikely(!upload
->map
)) {
234 upload
->map
= pipe_buffer_map_range(upload
->pipe
, upload
->buffer
,
236 buffer_size
- offset
,
239 if (unlikely(!upload
->map
)) {
240 upload
->transfer
= NULL
;
242 pipe_resource_reference(outbuf
, NULL
);
247 upload
->map
-= offset
;
250 assert(offset
< buffer_size
);
251 assert(offset
+ size
<= buffer_size
);
254 /* Emit the return values: */
255 *ptr
= upload
->map
+ offset
;
256 pipe_resource_reference(outbuf
, upload
->buffer
);
257 *out_offset
= offset
;
259 upload
->offset
= offset
+ size
;
263 u_upload_data(struct u_upload_mgr
*upload
,
264 unsigned min_out_offset
,
268 unsigned *out_offset
,
269 struct pipe_resource
**outbuf
)
273 u_upload_alloc(upload
, min_out_offset
, size
, alignment
,
277 memcpy(ptr
, data
, size
);