gallium/upload_mgr: don't unmap buffers if persistent mappings are supported
[mesa.git] / src / gallium / auxiliary / util / u_upload_mgr.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /* Helper utility for uploading user buffers & other data, and
29 * coalescing small buffers into larger ones.
30 */
31
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"
37
38 #include "u_upload_mgr.h"
39
40
41 struct u_upload_mgr {
42 struct pipe_context *pipe;
43
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. */
49
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. */
56 };
57
58
59 struct u_upload_mgr *u_upload_create( struct pipe_context *pipe,
60 unsigned default_size,
61 unsigned alignment,
62 unsigned bind )
63 {
64 struct u_upload_mgr *upload = CALLOC_STRUCT( u_upload_mgr );
65 if (!upload)
66 return NULL;
67
68 upload->pipe = pipe;
69 upload->default_size = default_size;
70 upload->alignment = alignment;
71 upload->bind = bind;
72
73 upload->map_persistent =
74 pipe->screen->get_param(pipe->screen,
75 PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT);
76
77 if (upload->map_persistent) {
78 upload->map_flags = PIPE_TRANSFER_WRITE |
79 PIPE_TRANSFER_PERSISTENT |
80 PIPE_TRANSFER_COHERENT;
81 }
82 else {
83 upload->map_flags = PIPE_TRANSFER_WRITE |
84 PIPE_TRANSFER_UNSYNCHRONIZED |
85 PIPE_TRANSFER_FLUSH_EXPLICIT;
86 }
87
88 return upload;
89 }
90
91
92 static void upload_unmap_internal(struct u_upload_mgr *upload, boolean destroying)
93 {
94 if (!destroying && upload->map_persistent)
95 return;
96
97 if (upload->transfer) {
98 struct pipe_box *box = &upload->transfer->box;
99
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);
103 }
104
105 pipe_transfer_unmap(upload->pipe, upload->transfer);
106 upload->transfer = NULL;
107 upload->map = NULL;
108 }
109 }
110
111
112 void u_upload_unmap( struct u_upload_mgr *upload )
113 {
114 upload_unmap_internal(upload, FALSE);
115 }
116
117
118 static void u_upload_release_buffer(struct u_upload_mgr *upload)
119 {
120 /* Unmap and unreference the upload buffer. */
121 upload_unmap_internal(upload, TRUE);
122 pipe_resource_reference( &upload->buffer, NULL );
123 upload->size = 0;
124 }
125
126
127 void u_upload_destroy( struct u_upload_mgr *upload )
128 {
129 u_upload_release_buffer( upload );
130 FREE( upload );
131 }
132
133
134 static enum pipe_error
135 u_upload_alloc_buffer( struct u_upload_mgr *upload,
136 unsigned min_size )
137 {
138 struct pipe_screen *screen = upload->pipe->screen;
139 struct pipe_resource buffer;
140 unsigned size;
141
142 /* Release the old buffer, if present:
143 */
144 u_upload_release_buffer( upload );
145
146 /* Allocate a new one:
147 */
148 size = align(MAX2(upload->default_size, min_size), 4096);
149
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;
156 buffer.height0 = 1;
157 buffer.depth0 = 1;
158 buffer.array_size = 1;
159
160 if (upload->map_persistent) {
161 buffer.flags = PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
162 PIPE_RESOURCE_FLAG_MAP_COHERENT;
163 }
164
165 upload->buffer = screen->resource_create(screen, &buffer);
166 if (upload->buffer == NULL) {
167 return PIPE_ERROR_OUT_OF_MEMORY;
168 }
169
170 /* Map the new buffer. */
171 upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
172 0, size, upload->map_flags,
173 &upload->transfer);
174 if (upload->map == NULL) {
175 upload->transfer = NULL;
176 upload->size = 0;
177 pipe_resource_reference(&upload->buffer, NULL);
178 return PIPE_ERROR_OUT_OF_MEMORY;
179 }
180
181 upload->size = size;
182 upload->offset = 0;
183 return PIPE_OK;
184 }
185
186 enum pipe_error u_upload_alloc( struct u_upload_mgr *upload,
187 unsigned min_out_offset,
188 unsigned size,
189 unsigned *out_offset,
190 struct pipe_resource **outbuf,
191 void **ptr )
192 {
193 unsigned alloc_size = align( size, upload->alignment );
194 unsigned alloc_offset = align(min_out_offset, upload->alignment);
195 unsigned offset;
196
197 /* Init these return values here in case we fail below to make
198 * sure the caller doesn't get garbage values.
199 */
200 *out_offset = ~0;
201 pipe_resource_reference(outbuf, NULL);
202 *ptr = NULL;
203
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);
209 if (ret != PIPE_OK)
210 return ret;
211 }
212
213 offset = MAX2(upload->offset, alloc_offset);
214
215 if (!upload->map) {
216 upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
217 offset, upload->size - offset,
218 upload->map_flags,
219 &upload->transfer);
220 if (!upload->map) {
221 upload->transfer = NULL;
222 return PIPE_ERROR_OUT_OF_MEMORY;
223 }
224
225 upload->map -= offset;
226 }
227
228 assert(offset < upload->buffer->width0);
229 assert(offset + size <= upload->buffer->width0);
230 assert(size);
231
232 /* Emit the return values: */
233 *ptr = upload->map + offset;
234 pipe_resource_reference( outbuf, upload->buffer );
235 *out_offset = offset;
236
237 upload->offset = offset + alloc_size;
238 return PIPE_OK;
239 }
240
241 enum pipe_error u_upload_data( struct u_upload_mgr *upload,
242 unsigned min_out_offset,
243 unsigned size,
244 const void *data,
245 unsigned *out_offset,
246 struct pipe_resource **outbuf)
247 {
248 uint8_t *ptr;
249 enum pipe_error ret = u_upload_alloc(upload, min_out_offset, size,
250 out_offset, outbuf,
251 (void**)&ptr);
252 if (ret != PIPE_OK)
253 return ret;
254
255 memcpy(ptr, data, size);
256 return PIPE_OK;
257 }
258
259
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.
264 */
265 enum pipe_error u_upload_buffer( struct u_upload_mgr *upload,
266 unsigned min_out_offset,
267 unsigned offset,
268 unsigned size,
269 struct pipe_resource *inbuf,
270 unsigned *out_offset,
271 struct pipe_resource **outbuf)
272 {
273 enum pipe_error ret = PIPE_OK;
274 struct pipe_transfer *transfer = NULL;
275 const char *map = NULL;
276
277 map = (const char *)pipe_buffer_map_range(upload->pipe,
278 inbuf,
279 offset, size,
280 PIPE_TRANSFER_READ,
281 &transfer);
282
283 if (map == NULL) {
284 return PIPE_ERROR_OUT_OF_MEMORY;
285 }
286
287 if (0)
288 debug_printf("upload ptr %p ofs %d sz %d\n", map, offset, size);
289
290 ret = u_upload_data( upload,
291 min_out_offset,
292 size,
293 map,
294 out_offset,
295 outbuf);
296
297 pipe_buffer_unmap( upload->pipe, transfer );
298
299 return ret;
300 }