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_screen.h"
35 #include "util/u_memory.h"
36 #include "util/u_math.h"
38 #include "u_upload_mgr.h"
42 struct pipe_screen *screen;
44 unsigned default_size;
50 struct pipe_buffer *buffer;
56 struct u_upload_mgr *u_upload_create( struct pipe_screen *screen,
57 unsigned default_size,
61 struct u_upload_mgr *upload = CALLOC_STRUCT( u_upload_mgr );
63 upload->default_size = default_size;
64 upload->screen = screen;
65 upload->alignment = alignment;
66 upload->usage = usage;
67 upload->buffer = NULL;
73 static INLINE enum pipe_error
74 my_buffer_write(struct pipe_screen *screen,
75 struct pipe_buffer *buf,
76 unsigned offset, unsigned size, unsigned dirty_size,
81 assert(offset < buf->size);
82 assert(offset + size <= buf->size);
83 assert(dirty_size >= size);
86 map = pipe_buffer_map_range(screen, buf, offset, size,
87 PIPE_BUFFER_USAGE_CPU_WRITE |
88 PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
89 PIPE_BUFFER_USAGE_DISCARD |
90 PIPE_BUFFER_USAGE_UNSYNCHRONIZED);
92 return PIPE_ERROR_OUT_OF_MEMORY;
94 memcpy(map + offset, data, size);
95 pipe_buffer_flush_mapped_range(screen, buf, offset, dirty_size);
96 pipe_buffer_unmap(screen, buf);
101 /* Release old buffer.
103 * This must usually be called prior to firing the command stream
104 * which references the upload buffer, as many memory managers will
105 * cause subsequent maps of a fired buffer to wait.
107 * Can improve this with a change to pipe_buffer_write to use the
108 * DONT_WAIT bit, but for now, it's easiest just to grab a new buffer.
110 void u_upload_flush( struct u_upload_mgr *upload )
112 pipe_buffer_reference( &upload->buffer, NULL );
117 void u_upload_destroy( struct u_upload_mgr *upload )
119 u_upload_flush( upload );
124 static enum pipe_error
125 u_upload_alloc_buffer( struct u_upload_mgr *upload,
130 /* Release old buffer, if present:
132 u_upload_flush( upload );
134 /* Allocate a new one:
136 size = align(MAX2(upload->default_size, min_size), 4096);
138 upload->buffer = pipe_buffer_create( upload->screen,
140 upload->usage | PIPE_BUFFER_USAGE_CPU_WRITE,
142 if (upload->buffer == NULL)
152 pipe_buffer_reference( &upload->buffer, NULL );
154 return PIPE_ERROR_OUT_OF_MEMORY;
158 enum pipe_error u_upload_data( struct u_upload_mgr *upload,
161 unsigned *out_offset,
162 struct pipe_buffer **outbuf )
164 unsigned alloc_size = align( size, upload->alignment );
165 enum pipe_error ret = PIPE_OK;
167 if (upload->offset + alloc_size > upload->size) {
168 ret = u_upload_alloc_buffer( upload, alloc_size );
173 /* Copy the data, using map_range if available:
175 ret = my_buffer_write( upload->screen,
184 /* Emit the return values:
186 pipe_buffer_reference( outbuf, upload->buffer );
187 *out_offset = upload->offset;
188 upload->offset += alloc_size;
193 /* As above, but upload the full contents of a buffer. Useful for
194 * uploading user buffers, avoids generating an explosion of GPU
195 * buffers if you have an app that does lots of small vertex buffer
196 * renders or DrawElements calls.
198 enum pipe_error u_upload_buffer( struct u_upload_mgr *upload,
201 struct pipe_buffer *inbuf,
202 unsigned *out_offset,
203 struct pipe_buffer **outbuf )
205 enum pipe_error ret = PIPE_OK;
206 const char *map = NULL;
208 map = (const char *)pipe_buffer_map(
209 upload->screen, inbuf, PIPE_BUFFER_USAGE_CPU_READ );
212 ret = PIPE_ERROR_OUT_OF_MEMORY;
217 debug_printf("upload ptr %p ofs %d sz %d\n", map, offset, size);
219 ret = u_upload_data( upload,
229 pipe_buffer_unmap( upload->screen, inbuf );