• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 bind;          /* Bitmask of PIPE_BIND_* flags. */
46    enum pipe_resource_usage usage;
47    unsigned flags;
48    unsigned map_flags;     /* Bitmask of PIPE_MAP_* flags. */
49    boolean map_persistent; /* If persistent mappings are supported. */
50 
51    struct pipe_resource *buffer;   /* Upload buffer. */
52    struct pipe_transfer *transfer; /* Transfer object for the upload buffer. */
53    uint8_t *map;    /* Pointer to the mapped upload buffer. */
54    unsigned buffer_size; /* Same as buffer->width0. */
55    unsigned offset; /* Aligned offset to the upload buffer, pointing
56                      * at the first unused byte. */
57    unsigned flushed_size; /* Size we have flushed by transfer_flush_region. */
58 };
59 
60 
61 struct u_upload_mgr *
u_upload_create(struct pipe_context * pipe,unsigned default_size,unsigned bind,enum pipe_resource_usage usage,unsigned flags)62 u_upload_create(struct pipe_context *pipe, unsigned default_size,
63                 unsigned bind, enum pipe_resource_usage usage, unsigned flags)
64 {
65    struct u_upload_mgr *upload = CALLOC_STRUCT(u_upload_mgr);
66    if (!upload)
67       return NULL;
68 
69    upload->pipe = pipe;
70    upload->default_size = default_size;
71    upload->bind = bind;
72    upload->usage = usage;
73    upload->flags = flags;
74 
75    upload->map_persistent =
76       pipe->screen->get_param(pipe->screen,
77                               PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT);
78 
79    if (upload->map_persistent) {
80       upload->map_flags = PIPE_MAP_WRITE |
81                           PIPE_MAP_UNSYNCHRONIZED |
82                           PIPE_MAP_PERSISTENT |
83                           PIPE_MAP_COHERENT;
84    }
85    else {
86       upload->map_flags = PIPE_MAP_WRITE |
87                           PIPE_MAP_UNSYNCHRONIZED |
88                           PIPE_MAP_FLUSH_EXPLICIT;
89    }
90 
91    return upload;
92 }
93 
94 struct u_upload_mgr *
u_upload_create_default(struct pipe_context * pipe)95 u_upload_create_default(struct pipe_context *pipe)
96 {
97    return u_upload_create(pipe, 1024 * 1024,
98                           PIPE_BIND_VERTEX_BUFFER |
99                           PIPE_BIND_INDEX_BUFFER |
100                           PIPE_BIND_CONSTANT_BUFFER,
101                           PIPE_USAGE_STREAM, 0);
102 }
103 
104 struct u_upload_mgr *
u_upload_clone(struct pipe_context * pipe,struct u_upload_mgr * upload)105 u_upload_clone(struct pipe_context *pipe, struct u_upload_mgr *upload)
106 {
107    struct u_upload_mgr *result = u_upload_create(pipe, upload->default_size,
108                                                  upload->bind, upload->usage,
109                                                  upload->flags);
110    if (!upload->map_persistent && result->map_persistent)
111       u_upload_disable_persistent(result);
112    else if (upload->map_persistent &&
113             upload->map_flags & PIPE_MAP_FLUSH_EXPLICIT)
114       u_upload_enable_flush_explicit(result);
115 
116    return result;
117 }
118 
119 void
u_upload_enable_flush_explicit(struct u_upload_mgr * upload)120 u_upload_enable_flush_explicit(struct u_upload_mgr *upload)
121 {
122    assert(upload->map_persistent);
123    upload->map_flags &= ~PIPE_MAP_COHERENT;
124    upload->map_flags |= PIPE_MAP_FLUSH_EXPLICIT;
125 }
126 
127 void
u_upload_disable_persistent(struct u_upload_mgr * upload)128 u_upload_disable_persistent(struct u_upload_mgr *upload)
129 {
130    upload->map_persistent = FALSE;
131    upload->map_flags &= ~(PIPE_MAP_COHERENT | PIPE_MAP_PERSISTENT);
132    upload->map_flags |= PIPE_MAP_FLUSH_EXPLICIT;
133 }
134 
135 static void
upload_unmap_internal(struct u_upload_mgr * upload,boolean destroying)136 upload_unmap_internal(struct u_upload_mgr *upload, boolean destroying)
137 {
138    if (!upload->transfer)
139       return;
140 
141    if (upload->map_flags & PIPE_MAP_FLUSH_EXPLICIT) {
142       struct pipe_box *box = &upload->transfer->box;
143       unsigned flush_offset = box->x + upload->flushed_size;
144 
145       if (upload->offset > flush_offset) {
146          pipe_buffer_flush_mapped_range(upload->pipe, upload->transfer,
147                                         flush_offset,
148                                         upload->offset - flush_offset);
149          upload->flushed_size = upload->offset;
150       }
151    }
152 
153    if (destroying || !upload->map_persistent) {
154       pipe_transfer_unmap(upload->pipe, upload->transfer);
155       upload->transfer = NULL;
156       upload->map = NULL;
157       upload->flushed_size = 0;
158    }
159 }
160 
161 
162 void
u_upload_unmap(struct u_upload_mgr * upload)163 u_upload_unmap(struct u_upload_mgr *upload)
164 {
165    upload_unmap_internal(upload, FALSE);
166 }
167 
168 
169 static void
u_upload_release_buffer(struct u_upload_mgr * upload)170 u_upload_release_buffer(struct u_upload_mgr *upload)
171 {
172    /* Unmap and unreference the upload buffer. */
173    upload_unmap_internal(upload, TRUE);
174    pipe_resource_reference(&upload->buffer, NULL);
175    upload->buffer_size = 0;
176 }
177 
178 
179 void
u_upload_destroy(struct u_upload_mgr * upload)180 u_upload_destroy(struct u_upload_mgr *upload)
181 {
182    u_upload_release_buffer(upload);
183    FREE(upload);
184 }
185 
186 /* Return the allocated buffer size or 0 if it failed. */
187 static unsigned
u_upload_alloc_buffer(struct u_upload_mgr * upload,unsigned min_size)188 u_upload_alloc_buffer(struct u_upload_mgr *upload, unsigned min_size)
189 {
190    struct pipe_screen *screen = upload->pipe->screen;
191    struct pipe_resource buffer;
192    unsigned size;
193 
194    /* Release the old buffer, if present:
195     */
196    u_upload_release_buffer(upload);
197 
198    /* Allocate a new one:
199     */
200    size = align(MAX2(upload->default_size, min_size), 4096);
201 
202    memset(&buffer, 0, sizeof buffer);
203    buffer.target = PIPE_BUFFER;
204    buffer.format = PIPE_FORMAT_R8_UNORM; /* want TYPELESS or similar */
205    buffer.bind = upload->bind;
206    buffer.usage = upload->usage;
207    buffer.flags = upload->flags | PIPE_RESOURCE_FLAG_SINGLE_THREAD_USE;
208    buffer.width0 = size;
209    buffer.height0 = 1;
210    buffer.depth0 = 1;
211    buffer.array_size = 1;
212 
213    if (upload->map_persistent) {
214       buffer.flags |= PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
215                       PIPE_RESOURCE_FLAG_MAP_COHERENT;
216    }
217 
218    upload->buffer = screen->resource_create(screen, &buffer);
219    if (upload->buffer == NULL)
220       return 0;
221 
222    /* Map the new buffer. */
223    upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
224                                        0, size, upload->map_flags,
225                                        &upload->transfer);
226    if (upload->map == NULL) {
227       upload->transfer = NULL;
228       pipe_resource_reference(&upload->buffer, NULL);
229       return 0;
230    }
231 
232    upload->buffer_size = size;
233    upload->offset = 0;
234    return size;
235 }
236 
237 void
u_upload_alloc(struct u_upload_mgr * upload,unsigned min_out_offset,unsigned size,unsigned alignment,unsigned * out_offset,struct pipe_resource ** outbuf,void ** ptr)238 u_upload_alloc(struct u_upload_mgr *upload,
239                unsigned min_out_offset,
240                unsigned size,
241                unsigned alignment,
242                unsigned *out_offset,
243                struct pipe_resource **outbuf,
244                void **ptr)
245 {
246    unsigned buffer_size = upload->buffer_size;
247    unsigned offset = MAX2(min_out_offset, upload->offset);
248 
249    offset = align(offset, alignment);
250 
251    /* Make sure we have enough space in the upload buffer
252     * for the sub-allocation.
253     */
254    if (unlikely(offset + size > buffer_size)) {
255       /* Allocate a new buffer and set the offset to the smallest one. */
256       offset = align(min_out_offset, alignment);
257       buffer_size = u_upload_alloc_buffer(upload, offset + size);
258 
259       if (unlikely(!buffer_size)) {
260          *out_offset = ~0;
261          pipe_resource_reference(outbuf, NULL);
262          *ptr = NULL;
263          return;
264       }
265    }
266 
267    if (unlikely(!upload->map)) {
268       upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
269                                           offset,
270                                           buffer_size - offset,
271                                           upload->map_flags,
272                                           &upload->transfer);
273       if (unlikely(!upload->map)) {
274          upload->transfer = NULL;
275          *out_offset = ~0;
276          pipe_resource_reference(outbuf, NULL);
277          *ptr = NULL;
278          return;
279       }
280 
281       upload->map -= offset;
282    }
283 
284    assert(offset < buffer_size);
285    assert(offset + size <= buffer_size);
286    assert(size);
287 
288    /* Emit the return values: */
289    *ptr = upload->map + offset;
290    pipe_resource_reference(outbuf, upload->buffer);
291    *out_offset = offset;
292 
293    upload->offset = offset + size;
294 }
295 
296 void
u_upload_data(struct u_upload_mgr * upload,unsigned min_out_offset,unsigned size,unsigned alignment,const void * data,unsigned * out_offset,struct pipe_resource ** outbuf)297 u_upload_data(struct u_upload_mgr *upload,
298               unsigned min_out_offset,
299               unsigned size,
300               unsigned alignment,
301               const void *data,
302               unsigned *out_offset,
303               struct pipe_resource **outbuf)
304 {
305    uint8_t *ptr;
306 
307    u_upload_alloc(upload, min_out_offset, size, alignment,
308                   out_offset, outbuf,
309                   (void**)&ptr);
310    if (ptr)
311       memcpy(ptr, data, size);
312 }
313