Merge branch 'mesa_7_7_branch'
[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 "pipe/p_inlines.h"
34 #include "pipe/p_screen.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_screen *screen;
43
44 unsigned default_size;
45 unsigned alignment;
46 unsigned usage;
47
48 /* The active buffer:
49 */
50 struct pipe_buffer *buffer;
51 unsigned size;
52 unsigned offset;
53 };
54
55
56 struct u_upload_mgr *u_upload_create( struct pipe_screen *screen,
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->default_size = default_size;
64 upload->screen = screen;
65 upload->alignment = alignment;
66 upload->usage = usage;
67 upload->buffer = NULL;
68
69 return upload;
70 }
71
72
73 static INLINE enum pipe_error
74 my_buffer_write(struct pipe_screen *screen,
75 struct pipe_buffer *buf,
76 unsigned offset, unsigned size, unsigned dirty_size,
77 const void *data)
78 {
79 uint8_t *map;
80
81 assert(offset < buf->size);
82 assert(offset + size <= buf->size);
83 assert(dirty_size >= size);
84 assert(size);
85
86 map = pipe_buffer_map_range(screen, buf, offset, size,
87 PIPE_BUFFER_USAGE_CPU_WRITE |
88 PIPE_BUFFER_USAGE_FLUSH_EXPLICIT);
89 if (map == NULL)
90 return PIPE_ERROR_OUT_OF_MEMORY;
91
92 memcpy(map + offset, data, size);
93 pipe_buffer_flush_mapped_range(screen, buf, offset, dirty_size);
94 pipe_buffer_unmap(screen, buf);
95
96 return PIPE_OK;
97 }
98
99 /* Release old buffer.
100 *
101 * This must usually be called prior to firing the command stream
102 * which references the upload buffer, as many memory managers will
103 * cause subsequent maps of a fired buffer to wait.
104 *
105 * Can improve this with a change to pipe_buffer_write to use the
106 * DONT_WAIT bit, but for now, it's easiest just to grab a new buffer.
107 */
108 void u_upload_flush( struct u_upload_mgr *upload )
109 {
110 pipe_buffer_reference( &upload->buffer, NULL );
111 upload->size = 0;
112 }
113
114
115 void u_upload_destroy( struct u_upload_mgr *upload )
116 {
117 u_upload_flush( upload );
118 FREE( upload );
119 }
120
121
122 static enum pipe_error
123 u_upload_alloc_buffer( struct u_upload_mgr *upload,
124 unsigned min_size )
125 {
126 unsigned size;
127
128 /* Release old buffer, if present:
129 */
130 u_upload_flush( upload );
131
132 /* Allocate a new one:
133 */
134 size = align(MAX2(upload->default_size, min_size), 4096);
135
136 upload->buffer = pipe_buffer_create( upload->screen,
137 upload->alignment,
138 upload->usage | PIPE_BUFFER_USAGE_CPU_WRITE,
139 size );
140 if (upload->buffer == NULL)
141 goto fail;
142
143 upload->size = size;
144
145 upload->offset = 0;
146 return 0;
147
148 fail:
149 if (upload->buffer)
150 pipe_buffer_reference( &upload->buffer, NULL );
151
152 return PIPE_ERROR_OUT_OF_MEMORY;
153 }
154
155
156 enum pipe_error u_upload_data( struct u_upload_mgr *upload,
157 unsigned size,
158 const void *data,
159 unsigned *out_offset,
160 struct pipe_buffer **outbuf )
161 {
162 unsigned alloc_size = align( size, upload->alignment );
163 enum pipe_error ret = PIPE_OK;
164
165 if (upload->offset + alloc_size > upload->size) {
166 ret = u_upload_alloc_buffer( upload, alloc_size );
167 if (ret)
168 return ret;
169 }
170
171 /* Copy the data, using map_range if available:
172 */
173 ret = my_buffer_write( upload->screen,
174 upload->buffer,
175 upload->offset,
176 size,
177 alloc_size,
178 data );
179 if (ret)
180 return ret;
181
182 /* Emit the return values:
183 */
184 pipe_buffer_reference( outbuf, upload->buffer );
185 *out_offset = upload->offset;
186 upload->offset += alloc_size;
187 return PIPE_OK;
188 }
189
190
191 /* As above, but upload the full contents of a buffer. Useful for
192 * uploading user buffers, avoids generating an explosion of GPU
193 * buffers if you have an app that does lots of small vertex buffer
194 * renders or DrawElements calls.
195 */
196 enum pipe_error u_upload_buffer( struct u_upload_mgr *upload,
197 unsigned offset,
198 unsigned size,
199 struct pipe_buffer *inbuf,
200 unsigned *out_offset,
201 struct pipe_buffer **outbuf )
202 {
203 enum pipe_error ret = PIPE_OK;
204 const char *map = NULL;
205
206 map = (const char *)pipe_buffer_map(
207 upload->screen, inbuf, PIPE_BUFFER_USAGE_CPU_READ );
208
209 if (map == NULL) {
210 ret = PIPE_ERROR_OUT_OF_MEMORY;
211 goto done;
212 }
213
214 if (0)
215 debug_printf("upload ptr %p ofs %d sz %d\n", map, offset, size);
216
217 ret = u_upload_data( upload,
218 size,
219 map + offset,
220 out_offset,
221 outbuf );
222 if (ret)
223 goto done;
224
225 done:
226 if (map)
227 pipe_buffer_unmap( upload->screen, inbuf );
228
229 return ret;
230 }