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