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
;
50 struct pipe_resource
*buffer
;
56 struct u_upload_mgr
*u_upload_create( struct pipe_context
*pipe
,
57 unsigned default_size
,
61 struct u_upload_mgr
*upload
= CALLOC_STRUCT( u_upload_mgr
);
64 upload
->default_size
= default_size
;
65 upload
->alignment
= alignment
;
66 upload
->usage
= usage
;
67 upload
->buffer
= NULL
;
72 /* Slightly specialized version of buffer_write designed to maximize
73 * chances of the driver consolidating successive writes into a single
76 * dirty_size may be slightly greater than size to cope with
77 * alignment. We don't want to leave holes between succesively mapped
78 * regions as that may prevent the driver from consolidating uploads.
80 * Note that the 'data' pointer has probably come from the application
81 * and we cannot read even a byte past its end without risking
82 * segfaults, or at least complaints from valgrind..
84 static INLINE
enum pipe_error
85 my_buffer_write(struct pipe_context
*pipe
,
86 struct pipe_resource
*buf
,
87 unsigned offset
, unsigned size
, unsigned dirty_size
,
90 struct pipe_transfer
*transfer
= NULL
;
93 assert(offset
< buf
->width0
);
94 assert(offset
+ size
<= buf
->width0
);
95 assert(dirty_size
>= size
);
98 map
= pipe_buffer_map_range(pipe
, buf
, offset
, dirty_size
,
100 PIPE_TRANSFER_FLUSH_EXPLICIT
|
101 PIPE_TRANSFER_DISCARD
|
102 PIPE_TRANSFER_UNSYNCHRONIZED
,
105 return PIPE_ERROR_OUT_OF_MEMORY
;
107 memcpy(map
+ offset
, data
, size
);
108 pipe_buffer_flush_mapped_range(pipe
, transfer
, offset
, dirty_size
);
109 pipe_buffer_unmap(pipe
, buf
, transfer
);
114 /* Release old buffer.
116 * This must usually be called prior to firing the command stream
117 * which references the upload buffer, as many memory managers will
118 * cause subsequent maps of a fired buffer to wait.
120 * Can improve this with a change to pipe_buffer_write to use the
121 * DONT_WAIT bit, but for now, it's easiest just to grab a new buffer.
123 void u_upload_flush( struct u_upload_mgr
*upload
)
125 pipe_resource_reference( &upload
->buffer
, NULL
);
130 void u_upload_destroy( struct u_upload_mgr
*upload
)
132 u_upload_flush( upload
);
137 static enum pipe_error
138 u_upload_alloc_buffer( struct u_upload_mgr
*upload
,
143 /* Release old buffer, if present:
145 u_upload_flush( upload
);
147 /* Allocate a new one:
149 size
= align(MAX2(upload
->default_size
, min_size
), 4096);
151 upload
->buffer
= pipe_buffer_create( upload
->pipe
->screen
,
154 if (upload
->buffer
== NULL
)
164 pipe_resource_reference( &upload
->buffer
, NULL
);
166 return PIPE_ERROR_OUT_OF_MEMORY
;
170 enum pipe_error
u_upload_data( struct u_upload_mgr
*upload
,
173 unsigned *out_offset
,
174 struct pipe_resource
**outbuf
)
176 unsigned alloc_size
= align( size
, upload
->alignment
);
177 enum pipe_error ret
= PIPE_OK
;
179 if (upload
->offset
+ alloc_size
> upload
->size
) {
180 ret
= u_upload_alloc_buffer( upload
, alloc_size
);
185 /* Copy the data, using map_range if available:
187 ret
= my_buffer_write( upload
->pipe
,
196 /* Emit the return values:
198 pipe_resource_reference( outbuf
, upload
->buffer
);
199 *out_offset
= upload
->offset
;
200 upload
->offset
+= alloc_size
;
205 /* As above, but upload the full contents of a buffer. Useful for
206 * uploading user buffers, avoids generating an explosion of GPU
207 * buffers if you have an app that does lots of small vertex buffer
208 * renders or DrawElements calls.
210 enum pipe_error
u_upload_buffer( struct u_upload_mgr
*upload
,
213 struct pipe_resource
*inbuf
,
214 unsigned *out_offset
,
215 struct pipe_resource
**outbuf
)
217 enum pipe_error ret
= PIPE_OK
;
218 struct pipe_transfer
*transfer
= NULL
;
219 const char *map
= NULL
;
221 map
= (const char *)pipe_buffer_map(upload
->pipe
,
227 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
232 debug_printf("upload ptr %p ofs %d sz %d\n", map
, offset
, size
);
234 ret
= u_upload_data( upload
,
244 pipe_buffer_unmap( upload
->pipe
, inbuf
, transfer
);