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