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 size
; /* Actual size of the upload buffer. */
54 unsigned offset
; /* Aligned offset to the upload buffer, pointing
55 * at the first unused byte. */
59 struct u_upload_mgr
*u_upload_create( struct pipe_context
*pipe
,
60 unsigned default_size
,
64 struct u_upload_mgr
*upload
= CALLOC_STRUCT( u_upload_mgr
);
69 upload
->default_size
= default_size
;
70 upload
->alignment
= alignment
;
73 upload
->map_persistent
=
74 pipe
->screen
->get_param(pipe
->screen
,
75 PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT
);
77 if (upload
->map_persistent
) {
78 upload
->map_flags
= PIPE_TRANSFER_WRITE
|
79 PIPE_TRANSFER_PERSISTENT
|
80 PIPE_TRANSFER_COHERENT
;
83 upload
->map_flags
= PIPE_TRANSFER_WRITE
|
84 PIPE_TRANSFER_UNSYNCHRONIZED
|
85 PIPE_TRANSFER_FLUSH_EXPLICIT
;
92 static void upload_unmap_internal(struct u_upload_mgr
*upload
, boolean destroying
)
94 if (!destroying
&& upload
->map_persistent
)
97 if (upload
->transfer
) {
98 struct pipe_box
*box
= &upload
->transfer
->box
;
100 if (!upload
->map_persistent
&& (int) upload
->offset
> box
->x
) {
101 pipe_buffer_flush_mapped_range(upload
->pipe
, upload
->transfer
,
102 box
->x
, upload
->offset
- box
->x
);
105 pipe_transfer_unmap(upload
->pipe
, upload
->transfer
);
106 upload
->transfer
= NULL
;
112 void u_upload_unmap( struct u_upload_mgr
*upload
)
114 upload_unmap_internal(upload
, FALSE
);
118 static void u_upload_release_buffer(struct u_upload_mgr
*upload
)
120 /* Unmap and unreference the upload buffer. */
121 upload_unmap_internal(upload
, TRUE
);
122 pipe_resource_reference( &upload
->buffer
, NULL
);
127 void u_upload_destroy( struct u_upload_mgr
*upload
)
129 u_upload_release_buffer( upload
);
134 static enum pipe_error
135 u_upload_alloc_buffer( struct u_upload_mgr
*upload
,
138 struct pipe_screen
*screen
= upload
->pipe
->screen
;
139 struct pipe_resource buffer
;
142 /* Release the old buffer, if present:
144 u_upload_release_buffer( upload
);
146 /* Allocate a new one:
148 size
= align(MAX2(upload
->default_size
, min_size
), 4096);
150 memset(&buffer
, 0, sizeof buffer
);
151 buffer
.target
= PIPE_BUFFER
;
152 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
153 buffer
.bind
= upload
->bind
;
154 buffer
.usage
= PIPE_USAGE_STREAM
;
155 buffer
.width0
= size
;
158 buffer
.array_size
= 1;
160 if (upload
->map_persistent
) {
161 buffer
.flags
= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
|
162 PIPE_RESOURCE_FLAG_MAP_COHERENT
;
165 upload
->buffer
= screen
->resource_create(screen
, &buffer
);
166 if (upload
->buffer
== NULL
) {
167 return PIPE_ERROR_OUT_OF_MEMORY
;
170 /* Map the new buffer. */
171 upload
->map
= pipe_buffer_map_range(upload
->pipe
, upload
->buffer
,
172 0, size
, upload
->map_flags
,
174 if (upload
->map
== NULL
) {
175 upload
->transfer
= NULL
;
177 pipe_resource_reference(&upload
->buffer
, NULL
);
178 return PIPE_ERROR_OUT_OF_MEMORY
;
186 enum pipe_error
u_upload_alloc( struct u_upload_mgr
*upload
,
187 unsigned min_out_offset
,
189 unsigned *out_offset
,
190 struct pipe_resource
**outbuf
,
193 unsigned alloc_size
= align( size
, upload
->alignment
);
194 unsigned alloc_offset
= align(min_out_offset
, upload
->alignment
);
197 /* Init these return values here in case we fail below to make
198 * sure the caller doesn't get garbage values.
201 pipe_resource_reference(outbuf
, NULL
);
204 /* Make sure we have enough space in the upload buffer
205 * for the sub-allocation. */
206 if (MAX2(upload
->offset
, alloc_offset
) + alloc_size
> upload
->size
) {
207 enum pipe_error ret
= u_upload_alloc_buffer(upload
,
208 alloc_offset
+ alloc_size
);
213 offset
= MAX2(upload
->offset
, alloc_offset
);
216 upload
->map
= pipe_buffer_map_range(upload
->pipe
, upload
->buffer
,
217 offset
, upload
->size
- offset
,
221 upload
->transfer
= NULL
;
222 return PIPE_ERROR_OUT_OF_MEMORY
;
225 upload
->map
-= offset
;
228 assert(offset
< upload
->buffer
->width0
);
229 assert(offset
+ size
<= upload
->buffer
->width0
);
232 /* Emit the return values: */
233 *ptr
= upload
->map
+ offset
;
234 pipe_resource_reference( outbuf
, upload
->buffer
);
235 *out_offset
= offset
;
237 upload
->offset
= offset
+ alloc_size
;
241 enum pipe_error
u_upload_data( struct u_upload_mgr
*upload
,
242 unsigned min_out_offset
,
245 unsigned *out_offset
,
246 struct pipe_resource
**outbuf
)
249 enum pipe_error ret
= u_upload_alloc(upload
, min_out_offset
, size
,
255 memcpy(ptr
, data
, size
);
260 /* As above, but upload the full contents of a buffer. Useful for
261 * uploading user buffers, avoids generating an explosion of GPU
262 * buffers if you have an app that does lots of small vertex buffer
263 * renders or DrawElements calls.
265 enum pipe_error
u_upload_buffer( struct u_upload_mgr
*upload
,
266 unsigned min_out_offset
,
269 struct pipe_resource
*inbuf
,
270 unsigned *out_offset
,
271 struct pipe_resource
**outbuf
)
273 enum pipe_error ret
= PIPE_OK
;
274 struct pipe_transfer
*transfer
= NULL
;
275 const char *map
= NULL
;
277 map
= (const char *)pipe_buffer_map_range(upload
->pipe
,
284 return PIPE_ERROR_OUT_OF_MEMORY
;
288 debug_printf("upload ptr %p ofs %d sz %d\n", map
, offset
, size
);
290 ret
= u_upload_data( upload
,
297 pipe_buffer_unmap( upload
->pipe
, transfer
);