gallium/util: libunwind support
[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 bind; /* Bitmask of PIPE_BIND_* flags. */
46 enum pipe_resource_usage usage;
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 offset; /* Aligned offset to the upload buffer, pointing
54 * at the first unused byte. */
55 };
56
57
58 struct u_upload_mgr *
59 u_upload_create(struct pipe_context *pipe, unsigned default_size,
60 unsigned bind, enum pipe_resource_usage usage)
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->bind = bind;
69 upload->usage = usage;
70
71 upload->map_persistent =
72 pipe->screen->get_param(pipe->screen,
73 PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT);
74
75 if (upload->map_persistent) {
76 upload->map_flags = PIPE_TRANSFER_WRITE |
77 PIPE_TRANSFER_UNSYNCHRONIZED |
78 PIPE_TRANSFER_PERSISTENT |
79 PIPE_TRANSFER_COHERENT;
80 }
81 else {
82 upload->map_flags = PIPE_TRANSFER_WRITE |
83 PIPE_TRANSFER_UNSYNCHRONIZED |
84 PIPE_TRANSFER_FLUSH_EXPLICIT;
85 }
86
87 return upload;
88 }
89
90 struct u_upload_mgr *
91 u_upload_create_default(struct pipe_context *pipe)
92 {
93 return u_upload_create(pipe, 1024 * 1024,
94 PIPE_BIND_VERTEX_BUFFER |
95 PIPE_BIND_INDEX_BUFFER |
96 PIPE_BIND_CONSTANT_BUFFER,
97 PIPE_USAGE_STREAM);
98 }
99
100 static void upload_unmap_internal(struct u_upload_mgr *upload, boolean destroying)
101 {
102 if (!destroying && upload->map_persistent)
103 return;
104
105 if (upload->transfer) {
106 struct pipe_box *box = &upload->transfer->box;
107
108 if (!upload->map_persistent && (int) upload->offset > box->x) {
109 pipe_buffer_flush_mapped_range(upload->pipe, upload->transfer,
110 box->x, upload->offset - box->x);
111 }
112
113 pipe_transfer_unmap(upload->pipe, upload->transfer);
114 upload->transfer = NULL;
115 upload->map = NULL;
116 }
117 }
118
119
120 void u_upload_unmap( struct u_upload_mgr *upload )
121 {
122 upload_unmap_internal(upload, FALSE);
123 }
124
125
126 static void u_upload_release_buffer(struct u_upload_mgr *upload)
127 {
128 /* Unmap and unreference the upload buffer. */
129 upload_unmap_internal(upload, TRUE);
130 pipe_resource_reference( &upload->buffer, NULL );
131 }
132
133
134 void u_upload_destroy( struct u_upload_mgr *upload )
135 {
136 u_upload_release_buffer( upload );
137 FREE( upload );
138 }
139
140
141 static void
142 u_upload_alloc_buffer(struct u_upload_mgr *upload,
143 unsigned min_size)
144 {
145 struct pipe_screen *screen = upload->pipe->screen;
146 struct pipe_resource buffer;
147 unsigned size;
148
149 /* Release the old buffer, if present:
150 */
151 u_upload_release_buffer( upload );
152
153 /* Allocate a new one:
154 */
155 size = align(MAX2(upload->default_size, min_size), 4096);
156
157 memset(&buffer, 0, sizeof buffer);
158 buffer.target = PIPE_BUFFER;
159 buffer.format = PIPE_FORMAT_R8_UNORM; /* want TYPELESS or similar */
160 buffer.bind = upload->bind;
161 buffer.usage = upload->usage;
162 buffer.width0 = size;
163 buffer.height0 = 1;
164 buffer.depth0 = 1;
165 buffer.array_size = 1;
166
167 if (upload->map_persistent) {
168 buffer.flags = PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
169 PIPE_RESOURCE_FLAG_MAP_COHERENT;
170 }
171
172 upload->buffer = screen->resource_create(screen, &buffer);
173 if (upload->buffer == NULL)
174 return;
175
176 /* Map the new buffer. */
177 upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
178 0, size, upload->map_flags,
179 &upload->transfer);
180 if (upload->map == NULL) {
181 upload->transfer = NULL;
182 pipe_resource_reference(&upload->buffer, NULL);
183 return;
184 }
185
186 upload->offset = 0;
187 }
188
189 void
190 u_upload_alloc(struct u_upload_mgr *upload,
191 unsigned min_out_offset,
192 unsigned size,
193 unsigned alignment,
194 unsigned *out_offset,
195 struct pipe_resource **outbuf,
196 void **ptr)
197 {
198 unsigned buffer_size = upload->buffer ? upload->buffer->width0 : 0;
199 unsigned offset;
200
201 min_out_offset = align(min_out_offset, alignment);
202
203 offset = align(upload->offset, alignment);
204 offset = MAX2(offset, min_out_offset);
205
206 /* Make sure we have enough space in the upload buffer
207 * for the sub-allocation.
208 */
209 if (unlikely(!upload->buffer || offset + size > buffer_size)) {
210 u_upload_alloc_buffer(upload, min_out_offset + size);
211
212 if (unlikely(!upload->buffer)) {
213 *out_offset = ~0;
214 pipe_resource_reference(outbuf, NULL);
215 *ptr = NULL;
216 return;
217 }
218
219 offset = min_out_offset;
220 buffer_size = upload->buffer->width0;
221 }
222
223 if (unlikely(!upload->map)) {
224 upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
225 offset,
226 buffer_size - offset,
227 upload->map_flags,
228 &upload->transfer);
229 if (unlikely(!upload->map)) {
230 upload->transfer = NULL;
231 *out_offset = ~0;
232 pipe_resource_reference(outbuf, NULL);
233 *ptr = NULL;
234 return;
235 }
236
237 upload->map -= offset;
238 }
239
240 assert(offset < buffer_size);
241 assert(offset + size <= buffer_size);
242 assert(size);
243
244 /* Emit the return values: */
245 *ptr = upload->map + offset;
246 pipe_resource_reference(outbuf, upload->buffer);
247 *out_offset = offset;
248
249 upload->offset = offset + size;
250 }
251
252 void u_upload_data(struct u_upload_mgr *upload,
253 unsigned min_out_offset,
254 unsigned size,
255 unsigned alignment,
256 const void *data,
257 unsigned *out_offset,
258 struct pipe_resource **outbuf)
259 {
260 uint8_t *ptr;
261
262 u_upload_alloc(upload, min_out_offset, size, alignment,
263 out_offset, outbuf,
264 (void**)&ptr);
265 if (ptr)
266 memcpy(ptr, data, size);
267 }