Squashed commit of the following:
[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;
45 unsigned alignment;
46 unsigned usage;
47
48 /* The active buffer:
49 */
50 struct pipe_resource *buffer;
51 unsigned size;
52 unsigned offset;
53 };
54
55
56 struct u_upload_mgr *u_upload_create( struct pipe_context *pipe,
57 unsigned default_size,
58 unsigned alignment,
59 unsigned usage )
60 {
61 struct u_upload_mgr *upload = CALLOC_STRUCT( u_upload_mgr );
62
63 upload->pipe = pipe;
64 upload->default_size = default_size;
65 upload->alignment = alignment;
66 upload->usage = usage;
67 upload->buffer = NULL;
68
69 return upload;
70 }
71
72 /* Slightly specialized version of buffer_write designed to maximize
73 * chances of the driver consolidating successive writes into a single
74 * upload.
75 *
76 * dirty_size may be slightly greater than size to cope with
77 * alignment. We don't want to leave holes between succesively mapped
78 * regions as that may prevent the driver from consolidating uploads.
79 *
80 * Note that the 'data' pointer has probably come from the application
81 * and we cannot read even a byte past its end without risking
82 * segfaults, or at least complaints from valgrind..
83 */
84 static INLINE enum pipe_error
85 my_buffer_write(struct pipe_context *pipe,
86 struct pipe_resource *buf,
87 unsigned offset, unsigned size, unsigned dirty_size,
88 const void *data)
89 {
90 struct pipe_transfer *transfer = NULL;
91 uint8_t *map;
92
93 assert(offset < buf->width0);
94 assert(offset + size <= buf->width0);
95 assert(dirty_size >= size);
96 assert(size);
97
98 map = pipe_buffer_map_range(pipe, buf, offset, dirty_size,
99 PIPE_TRANSFER_WRITE |
100 PIPE_TRANSFER_FLUSH_EXPLICIT |
101 PIPE_TRANSFER_DISCARD |
102 PIPE_TRANSFER_UNSYNCHRONIZED,
103 &transfer);
104 if (map == NULL)
105 return PIPE_ERROR_OUT_OF_MEMORY;
106
107 memcpy(map + offset, data, size);
108 pipe_buffer_flush_mapped_range(pipe, transfer, offset, dirty_size);
109 pipe_buffer_unmap(pipe, buf, transfer);
110
111 return PIPE_OK;
112 }
113
114 /* Release old buffer.
115 *
116 * This must usually be called prior to firing the command stream
117 * which references the upload buffer, as many memory managers will
118 * cause subsequent maps of a fired buffer to wait.
119 *
120 * Can improve this with a change to pipe_buffer_write to use the
121 * DONT_WAIT bit, but for now, it's easiest just to grab a new buffer.
122 */
123 void u_upload_flush( struct u_upload_mgr *upload )
124 {
125 pipe_resource_reference( &upload->buffer, NULL );
126 upload->size = 0;
127 }
128
129
130 void u_upload_destroy( struct u_upload_mgr *upload )
131 {
132 u_upload_flush( upload );
133 FREE( upload );
134 }
135
136
137 static enum pipe_error
138 u_upload_alloc_buffer( struct u_upload_mgr *upload,
139 unsigned min_size )
140 {
141 unsigned size;
142
143 /* Release old buffer, if present:
144 */
145 u_upload_flush( upload );
146
147 /* Allocate a new one:
148 */
149 size = align(MAX2(upload->default_size, min_size), 4096);
150
151 upload->buffer = pipe_buffer_create( upload->pipe->screen,
152 upload->usage,
153 size );
154 if (upload->buffer == NULL)
155 goto fail;
156
157 upload->size = size;
158
159 upload->offset = 0;
160 return 0;
161
162 fail:
163 if (upload->buffer)
164 pipe_resource_reference( &upload->buffer, NULL );
165
166 return PIPE_ERROR_OUT_OF_MEMORY;
167 }
168
169
170 enum pipe_error u_upload_data( struct u_upload_mgr *upload,
171 unsigned size,
172 const void *data,
173 unsigned *out_offset,
174 struct pipe_resource **outbuf )
175 {
176 unsigned alloc_size = align( size, upload->alignment );
177 enum pipe_error ret = PIPE_OK;
178
179 if (upload->offset + alloc_size > upload->size) {
180 ret = u_upload_alloc_buffer( upload, alloc_size );
181 if (ret)
182 return ret;
183 }
184
185 /* Copy the data, using map_range if available:
186 */
187 ret = my_buffer_write( upload->pipe,
188 upload->buffer,
189 upload->offset,
190 size,
191 alloc_size,
192 data );
193 if (ret)
194 return ret;
195
196 /* Emit the return values:
197 */
198 pipe_resource_reference( outbuf, upload->buffer );
199 *out_offset = upload->offset;
200 upload->offset += alloc_size;
201 return PIPE_OK;
202 }
203
204
205 /* As above, but upload the full contents of a buffer. Useful for
206 * uploading user buffers, avoids generating an explosion of GPU
207 * buffers if you have an app that does lots of small vertex buffer
208 * renders or DrawElements calls.
209 */
210 enum pipe_error u_upload_buffer( struct u_upload_mgr *upload,
211 unsigned offset,
212 unsigned size,
213 struct pipe_resource *inbuf,
214 unsigned *out_offset,
215 struct pipe_resource **outbuf )
216 {
217 enum pipe_error ret = PIPE_OK;
218 struct pipe_transfer *transfer = NULL;
219 const char *map = NULL;
220
221 map = (const char *)pipe_buffer_map(upload->pipe,
222 inbuf,
223 PIPE_TRANSFER_READ,
224 &transfer);
225
226 if (map == NULL) {
227 ret = PIPE_ERROR_OUT_OF_MEMORY;
228 goto done;
229 }
230
231 if (0)
232 debug_printf("upload ptr %p ofs %d sz %d\n", map, offset, size);
233
234 ret = u_upload_data( upload,
235 size,
236 map + offset,
237 out_offset,
238 outbuf );
239 if (ret)
240 goto done;
241
242 done:
243 if (map)
244 pipe_buffer_unmap( upload->pipe, inbuf, transfer );
245
246 return ret;
247 }