u_upload_mgr.c revision 50b0069fc18f9d4d3c4469897b379df55eece547
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 41struct 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 48 struct pipe_resource *buffer; /* Upload buffer. */ 49 struct pipe_transfer *transfer; /* Transfer object for the upload buffer. */ 50 uint8_t *map; /* Pointer to the mapped upload buffer. */ 51 unsigned size; /* Actual size of the upload buffer. */ 52 unsigned offset; /* Aligned offset to the upload buffer, pointing 53 * at the first unused byte. */ 54}; 55 56 57struct u_upload_mgr *u_upload_create( struct pipe_context *pipe, 58 unsigned default_size, 59 unsigned alignment, 60 unsigned bind ) 61{ 62 struct u_upload_mgr *upload = CALLOC_STRUCT( u_upload_mgr ); 63 if (!upload) 64 return NULL; 65 66 upload->pipe = pipe; 67 upload->default_size = default_size; 68 upload->alignment = alignment; 69 upload->bind = bind; 70 upload->buffer = NULL; 71 72 return upload; 73} 74 75void u_upload_unmap( struct u_upload_mgr *upload ) 76{ 77 if (upload->transfer) { 78 struct pipe_box *box = &upload->transfer->box; 79 if (upload->offset > box->x) { 80 81 pipe_buffer_flush_mapped_range(upload->pipe, upload->transfer, 82 box->x, upload->offset - box->x); 83 } 84 pipe_transfer_unmap(upload->pipe, upload->transfer); 85 pipe_transfer_destroy(upload->pipe, upload->transfer); 86 upload->transfer = NULL; 87 upload->map = NULL; 88 } 89} 90 91/* Release old buffer. 92 * 93 * This must usually be called prior to firing the command stream 94 * which references the upload buffer, as many memory managers will 95 * cause subsequent maps of a fired buffer to wait. 96 * 97 * Can improve this with a change to pipe_buffer_write to use the 98 * DONT_WAIT bit, but for now, it's easiest just to grab a new buffer. 99 */ 100void u_upload_flush( struct u_upload_mgr *upload ) 101{ 102 /* Unmap and unreference the upload buffer. */ 103 u_upload_unmap(upload); 104 pipe_resource_reference( &upload->buffer, NULL ); 105 upload->size = 0; 106} 107 108 109void u_upload_destroy( struct u_upload_mgr *upload ) 110{ 111 u_upload_flush( upload ); 112 FREE( upload ); 113} 114 115 116static enum pipe_error 117u_upload_alloc_buffer( struct u_upload_mgr *upload, 118 unsigned min_size ) 119{ 120 unsigned size; 121 122 /* Release the old buffer, if present: 123 */ 124 u_upload_flush( upload ); 125 126 /* Allocate a new one: 127 */ 128 size = align(MAX2(upload->default_size, min_size), 4096); 129 130 upload->buffer = pipe_buffer_create( upload->pipe->screen, 131 upload->bind, 132 PIPE_USAGE_STREAM, 133 size ); 134 if (upload->buffer == NULL) { 135 return PIPE_ERROR_OUT_OF_MEMORY; 136 } 137 138 /* Map the new buffer. */ 139 upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer, 140 0, size, 141 PIPE_TRANSFER_WRITE | 142 PIPE_TRANSFER_FLUSH_EXPLICIT, 143 &upload->transfer); 144 145 upload->size = size; 146 147 upload->offset = 0; 148 return PIPE_OK; 149} 150 151enum pipe_error u_upload_alloc( struct u_upload_mgr *upload, 152 unsigned min_out_offset, 153 unsigned size, 154 unsigned *out_offset, 155 struct pipe_resource **outbuf, 156 boolean *flushed, 157 void **ptr ) 158{ 159 unsigned alloc_size = align( size, upload->alignment ); 160 unsigned alloc_offset = align(min_out_offset, upload->alignment); 161 unsigned offset; 162 163 /* Make sure we have enough space in the upload buffer 164 * for the sub-allocation. */ 165 if (MAX2(upload->offset, alloc_offset) + alloc_size > upload->size) { 166 enum pipe_error ret = u_upload_alloc_buffer(upload, 167 alloc_offset + alloc_size); 168 if (ret != PIPE_OK) 169 return ret; 170 171 *flushed = TRUE; 172 } else { 173 *flushed = FALSE; 174 } 175 176 offset = MAX2(upload->offset, alloc_offset); 177 178 if (!upload->map) { 179 upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer, 180 offset, upload->size - offset, 181 PIPE_TRANSFER_WRITE | 182 PIPE_TRANSFER_FLUSH_EXPLICIT | 183 PIPE_TRANSFER_UNSYNCHRONIZED, 184 &upload->transfer); 185 } 186 187 assert(offset < upload->buffer->width0); 188 assert(offset + size <= upload->buffer->width0); 189 assert(size); 190 191 /* Emit the return values: */ 192 *ptr = upload->map + offset; 193 pipe_resource_reference( outbuf, upload->buffer ); 194 *out_offset = offset; 195 196 upload->offset = offset + alloc_size; 197 return PIPE_OK; 198} 199 200enum pipe_error u_upload_data( struct u_upload_mgr *upload, 201 unsigned min_out_offset, 202 unsigned size, 203 const void *data, 204 unsigned *out_offset, 205 struct pipe_resource **outbuf, 206 boolean *flushed ) 207{ 208 uint8_t *ptr; 209 enum pipe_error ret = u_upload_alloc(upload, min_out_offset, size, 210 out_offset, outbuf, flushed, 211 (void**)&ptr); 212 if (ret != PIPE_OK) 213 return ret; 214 215 memcpy(ptr, data, size); 216 return PIPE_OK; 217} 218 219 220/* As above, but upload the full contents of a buffer. Useful for 221 * uploading user buffers, avoids generating an explosion of GPU 222 * buffers if you have an app that does lots of small vertex buffer 223 * renders or DrawElements calls. 224 */ 225enum pipe_error u_upload_buffer( struct u_upload_mgr *upload, 226 unsigned min_out_offset, 227 unsigned offset, 228 unsigned size, 229 struct pipe_resource *inbuf, 230 unsigned *out_offset, 231 struct pipe_resource **outbuf, 232 boolean *flushed ) 233{ 234 enum pipe_error ret = PIPE_OK; 235 struct pipe_transfer *transfer = NULL; 236 const char *map = NULL; 237 238 map = (const char *)pipe_buffer_map_range(upload->pipe, 239 inbuf, 240 offset, size, 241 PIPE_TRANSFER_READ, 242 &transfer); 243 244 if (map == NULL) { 245 return PIPE_ERROR_OUT_OF_MEMORY; 246 } 247 248 if (0) 249 debug_printf("upload ptr %p ofs %d sz %d\n", map, offset, size); 250 251 ret = u_upload_data( upload, 252 min_out_offset, 253 size, 254 map + offset, 255 out_offset, 256 outbuf, flushed ); 257 258 pipe_buffer_unmap( upload->pipe, transfer ); 259 260 return ret; 261} 262