u_upload_mgr: pass alignment to u_upload_alloc manually
[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 alignment; /* Alignment of each sub-allocation. */
46 unsigned bind; /* Bitmask of PIPE_BIND_* flags. */
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 *u_upload_create( struct pipe_context *pipe,
59 unsigned default_size,
60 unsigned alignment,
61 unsigned bind )
62 {
63 struct u_upload_mgr *upload = CALLOC_STRUCT( u_upload_mgr );
64 if (!upload)
65 return NULL;
66
67 upload->pipe = pipe;
68 upload->default_size = default_size;
69 upload->alignment = alignment;
70 upload->bind = bind;
71
72 upload->map_persistent =
73 pipe->screen->get_param(pipe->screen,
74 PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT);
75
76 if (upload->map_persistent) {
77 upload->map_flags = PIPE_TRANSFER_WRITE |
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
91 static void upload_unmap_internal(struct u_upload_mgr *upload, boolean destroying)
92 {
93 if (!destroying && upload->map_persistent)
94 return;
95
96 if (upload->transfer) {
97 struct pipe_box *box = &upload->transfer->box;
98
99 if (!upload->map_persistent && (int) upload->offset > box->x) {
100 pipe_buffer_flush_mapped_range(upload->pipe, upload->transfer,
101 box->x, upload->offset - box->x);
102 }
103
104 pipe_transfer_unmap(upload->pipe, upload->transfer);
105 upload->transfer = NULL;
106 upload->map = NULL;
107 }
108 }
109
110
111 void u_upload_unmap( struct u_upload_mgr *upload )
112 {
113 upload_unmap_internal(upload, FALSE);
114 }
115
116
117 static void u_upload_release_buffer(struct u_upload_mgr *upload)
118 {
119 /* Unmap and unreference the upload buffer. */
120 upload_unmap_internal(upload, TRUE);
121 pipe_resource_reference( &upload->buffer, NULL );
122 }
123
124
125 void u_upload_destroy( struct u_upload_mgr *upload )
126 {
127 u_upload_release_buffer( upload );
128 FREE( upload );
129 }
130
131
132 static void
133 u_upload_alloc_buffer(struct u_upload_mgr *upload,
134 unsigned min_size)
135 {
136 struct pipe_screen *screen = upload->pipe->screen;
137 struct pipe_resource buffer;
138 unsigned size;
139
140 /* Release the old buffer, if present:
141 */
142 u_upload_release_buffer( upload );
143
144 /* Allocate a new one:
145 */
146 size = align(MAX2(upload->default_size, min_size), 4096);
147
148 memset(&buffer, 0, sizeof buffer);
149 buffer.target = PIPE_BUFFER;
150 buffer.format = PIPE_FORMAT_R8_UNORM; /* want TYPELESS or similar */
151 buffer.bind = upload->bind;
152 buffer.usage = PIPE_USAGE_STREAM;
153 buffer.width0 = size;
154 buffer.height0 = 1;
155 buffer.depth0 = 1;
156 buffer.array_size = 1;
157
158 if (upload->map_persistent) {
159 buffer.flags = PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
160 PIPE_RESOURCE_FLAG_MAP_COHERENT;
161 }
162
163 upload->buffer = screen->resource_create(screen, &buffer);
164 if (upload->buffer == NULL)
165 return;
166
167 /* Map the new buffer. */
168 upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
169 0, size, upload->map_flags,
170 &upload->transfer);
171 if (upload->map == NULL) {
172 upload->transfer = NULL;
173 pipe_resource_reference(&upload->buffer, NULL);
174 return;
175 }
176
177 upload->offset = 0;
178 }
179
180 void
181 u_upload_alloc(struct u_upload_mgr *upload,
182 unsigned min_out_offset,
183 unsigned size,
184 unsigned alignment,
185 unsigned *out_offset,
186 struct pipe_resource **outbuf,
187 void **ptr)
188 {
189 unsigned buffer_size = upload->buffer ? upload->buffer->width0 : 0;
190 unsigned offset;
191
192 min_out_offset = align(min_out_offset, alignment);
193
194 offset = align(upload->offset, alignment);
195 offset = MAX2(offset, min_out_offset);
196
197 /* Make sure we have enough space in the upload buffer
198 * for the sub-allocation.
199 */
200 if (unlikely(!upload->buffer || offset + size > buffer_size)) {
201 u_upload_alloc_buffer(upload, min_out_offset + size);
202
203 if (unlikely(!upload->buffer)) {
204 *out_offset = ~0;
205 pipe_resource_reference(outbuf, NULL);
206 *ptr = NULL;
207 return;
208 }
209
210 offset = min_out_offset;
211 buffer_size = upload->buffer->width0;
212 }
213
214 if (unlikely(!upload->map)) {
215 upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
216 offset,
217 buffer_size - offset,
218 upload->map_flags,
219 &upload->transfer);
220 if (unlikely(!upload->map)) {
221 upload->transfer = NULL;
222 *out_offset = ~0;
223 pipe_resource_reference(outbuf, NULL);
224 *ptr = NULL;
225 return;
226 }
227
228 upload->map -= offset;
229 }
230
231 assert(offset < buffer_size);
232 assert(offset + size <= buffer_size);
233 assert(size);
234
235 /* Emit the return values: */
236 *ptr = upload->map + offset;
237 pipe_resource_reference(outbuf, upload->buffer);
238 *out_offset = offset;
239
240 upload->offset = offset + size;
241 }
242
243 void u_upload_data(struct u_upload_mgr *upload,
244 unsigned min_out_offset,
245 unsigned size,
246 const void *data,
247 unsigned *out_offset,
248 struct pipe_resource **outbuf)
249 {
250 uint8_t *ptr;
251
252 u_upload_alloc(upload, min_out_offset, size, upload->alignment,
253 out_offset, outbuf,
254 (void**)&ptr);
255 if (ptr)
256 memcpy(ptr, data, size);
257 }
258
259 /* XXX: Remove. It's basically a CPU fallback of resource_copy_region. */
260 void u_upload_buffer(struct u_upload_mgr *upload,
261 unsigned min_out_offset,
262 unsigned offset,
263 unsigned size,
264 struct pipe_resource *inbuf,
265 unsigned *out_offset,
266 struct pipe_resource **outbuf)
267 {
268 struct pipe_transfer *transfer = NULL;
269 const char *map = NULL;
270
271 map = (const char *)pipe_buffer_map_range(upload->pipe,
272 inbuf,
273 offset, size,
274 PIPE_TRANSFER_READ,
275 &transfer);
276
277 if (!map) {
278 pipe_resource_reference(outbuf, NULL);
279 return;
280 }
281
282 if (0)
283 debug_printf("upload ptr %p ofs %d sz %d\n", map, offset, size);
284
285 u_upload_data(upload, min_out_offset, size, map, out_offset, outbuf);
286 pipe_buffer_unmap( upload->pipe, transfer );
287 }