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 alignment
; /* Alignment of each sub-allocation. */
46 unsigned bind
; /* Bitmask of PIPE_BIND_* flags. */
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. */
58 struct u_upload_mgr
*u_upload_create( struct pipe_context
*pipe
,
59 unsigned default_size
,
63 struct u_upload_mgr
*upload
= CALLOC_STRUCT( u_upload_mgr
);
68 upload
->default_size
= default_size
;
69 upload
->alignment
= alignment
;
72 upload
->map_persistent
=
73 pipe
->screen
->get_param(pipe
->screen
,
74 PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT
);
76 if (upload
->map_persistent
) {
77 upload
->map_flags
= PIPE_TRANSFER_WRITE
|
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 static void upload_unmap_internal(struct u_upload_mgr
*upload
, boolean destroying
)
93 if (!destroying
&& upload
->map_persistent
)
96 if (upload
->transfer
) {
97 struct pipe_box
*box
= &upload
->transfer
->box
;
99 if (!upload
->map_persistent
&& (int) upload
->offset
> box
->x
) {
100 pipe_buffer_flush_mapped_range(upload
->pipe
, upload
->transfer
,
101 box
->x
, upload
->offset
- box
->x
);
104 pipe_transfer_unmap(upload
->pipe
, upload
->transfer
);
105 upload
->transfer
= NULL
;
111 void u_upload_unmap( struct u_upload_mgr
*upload
)
113 upload_unmap_internal(upload
, FALSE
);
117 static void u_upload_release_buffer(struct u_upload_mgr
*upload
)
119 /* Unmap and unreference the upload buffer. */
120 upload_unmap_internal(upload
, TRUE
);
121 pipe_resource_reference( &upload
->buffer
, NULL
);
125 void u_upload_destroy( struct u_upload_mgr
*upload
)
127 u_upload_release_buffer( upload
);
132 static enum pipe_error
133 u_upload_alloc_buffer( struct u_upload_mgr
*upload
,
136 struct pipe_screen
*screen
= upload
->pipe
->screen
;
137 struct pipe_resource buffer
;
140 /* Release the old buffer, if present:
142 u_upload_release_buffer( upload
);
144 /* Allocate a new one:
146 size
= align(MAX2(upload
->default_size
, min_size
), 4096);
148 memset(&buffer
, 0, sizeof buffer
);
149 buffer
.target
= PIPE_BUFFER
;
150 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
151 buffer
.bind
= upload
->bind
;
152 buffer
.usage
= PIPE_USAGE_STREAM
;
153 buffer
.width0
= size
;
156 buffer
.array_size
= 1;
158 if (upload
->map_persistent
) {
159 buffer
.flags
= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
|
160 PIPE_RESOURCE_FLAG_MAP_COHERENT
;
163 upload
->buffer
= screen
->resource_create(screen
, &buffer
);
164 if (upload
->buffer
== NULL
) {
165 return PIPE_ERROR_OUT_OF_MEMORY
;
168 /* Map the new buffer. */
169 upload
->map
= pipe_buffer_map_range(upload
->pipe
, upload
->buffer
,
170 0, size
, upload
->map_flags
,
172 if (upload
->map
== NULL
) {
173 upload
->transfer
= NULL
;
174 pipe_resource_reference(&upload
->buffer
, NULL
);
175 return PIPE_ERROR_OUT_OF_MEMORY
;
182 enum pipe_error
u_upload_alloc( struct u_upload_mgr
*upload
,
183 unsigned min_out_offset
,
185 unsigned *out_offset
,
186 struct pipe_resource
**outbuf
,
189 unsigned alloc_size
= align( size
, upload
->alignment
);
190 unsigned alloc_offset
= align(min_out_offset
, upload
->alignment
);
193 /* Init these return values here in case we fail below to make
194 * sure the caller doesn't get garbage values.
197 pipe_resource_reference(outbuf
, NULL
);
200 /* Make sure we have enough space in the upload buffer
201 * for the sub-allocation. */
202 if (!upload
->buffer
||
203 MAX2(upload
->offset
, alloc_offset
) + alloc_size
> upload
->buffer
->width0
) {
204 enum pipe_error ret
= u_upload_alloc_buffer(upload
,
205 alloc_offset
+ alloc_size
);
210 offset
= MAX2(upload
->offset
, alloc_offset
);
213 upload
->map
= pipe_buffer_map_range(upload
->pipe
, upload
->buffer
,
215 upload
->buffer
->width0
- offset
,
219 upload
->transfer
= NULL
;
220 return PIPE_ERROR_OUT_OF_MEMORY
;
223 upload
->map
-= offset
;
226 assert(offset
< upload
->buffer
->width0
);
227 assert(offset
+ size
<= upload
->buffer
->width0
);
230 /* Emit the return values: */
231 *ptr
= upload
->map
+ offset
;
232 pipe_resource_reference( outbuf
, upload
->buffer
);
233 *out_offset
= offset
;
235 upload
->offset
= offset
+ alloc_size
;
239 enum pipe_error
u_upload_data( struct u_upload_mgr
*upload
,
240 unsigned min_out_offset
,
243 unsigned *out_offset
,
244 struct pipe_resource
**outbuf
)
247 enum pipe_error ret
= u_upload_alloc(upload
, min_out_offset
, size
,
253 memcpy(ptr
, data
, size
);
258 /* As above, but upload the full contents of a buffer. Useful for
259 * uploading user buffers, avoids generating an explosion of GPU
260 * buffers if you have an app that does lots of small vertex buffer
261 * renders or DrawElements calls.
263 enum pipe_error
u_upload_buffer( struct u_upload_mgr
*upload
,
264 unsigned min_out_offset
,
267 struct pipe_resource
*inbuf
,
268 unsigned *out_offset
,
269 struct pipe_resource
**outbuf
)
271 enum pipe_error ret
= PIPE_OK
;
272 struct pipe_transfer
*transfer
= NULL
;
273 const char *map
= NULL
;
275 map
= (const char *)pipe_buffer_map_range(upload
->pipe
,
282 return PIPE_ERROR_OUT_OF_MEMORY
;
286 debug_printf("upload ptr %p ofs %d sz %d\n", map
, offset
, size
);
288 ret
= u_upload_data( upload
,
295 pipe_buffer_unmap( upload
->pipe
, transfer
);