1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 /* Helper utility for uploading user buffers & other data, and
29 * coalescing small buffers into larger ones.
32 #include "pipe/p_error.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"
38 #include "u_upload_mgr.h"
42 struct pipe_screen
*screen
;
44 unsigned default_size
;
50 struct pipe_buffer
*buffer
;
56 struct u_upload_mgr
*u_upload_create( struct pipe_screen
*screen
,
57 unsigned default_size
,
61 struct u_upload_mgr
*upload
= CALLOC_STRUCT( u_upload_mgr
);
63 upload
->default_size
= default_size
;
64 upload
->screen
= screen
;
65 upload
->alignment
= alignment
;
66 upload
->usage
= usage
;
67 upload
->buffer
= NULL
;
74 my_buffer_write(struct pipe_screen
*screen
,
75 struct pipe_buffer
*buf
,
76 unsigned offset
, unsigned size
, unsigned dirty_size
,
81 assert(offset
< buf
->size
);
82 assert(offset
+ size
<= buf
->size
);
83 assert(dirty_size
>= size
);
86 map
= pipe_buffer_map_range(screen
, buf
, offset
, size
, PIPE_BUFFER_USAGE_CPU_WRITE
);
89 memcpy(map
+ offset
, data
, size
);
90 pipe_buffer_flush_mapped_range(screen
, buf
, offset
, dirty_size
);
91 pipe_buffer_unmap(screen
, buf
);
95 /* Release old buffer.
97 * This must usually be called prior to firing the command stream
98 * which references the upload buffer, as many memory managers will
99 * cause subsequent maps of a fired buffer to wait.
101 * Can improve this with a change to pipe_buffer_write to use the
102 * DONT_WAIT bit, but for now, it's easiest just to grab a new buffer.
104 void u_upload_flush( struct u_upload_mgr
*upload
)
106 pipe_buffer_reference( &upload
->buffer
, NULL
);
111 void u_upload_destroy( struct u_upload_mgr
*upload
)
113 u_upload_flush( upload
);
118 static enum pipe_error
119 u_upload_alloc_buffer( struct u_upload_mgr
*upload
,
122 /* Release old buffer, if present:
124 u_upload_flush( upload
);
126 /* Allocate a new one:
128 upload
->size
= align(MAX2(upload
->default_size
, min_size
), 4096);
130 upload
->buffer
= pipe_buffer_create( upload
->screen
,
132 upload
->usage
| PIPE_BUFFER_USAGE_CPU_WRITE
,
134 if (upload
->buffer
== NULL
)
142 pipe_buffer_reference( &upload
->buffer
, NULL
);
144 return PIPE_ERROR_OUT_OF_MEMORY
;
148 enum pipe_error
u_upload_data( struct u_upload_mgr
*upload
,
151 unsigned *out_offset
,
152 struct pipe_buffer
**outbuf
)
154 unsigned alloc_size
= align( size
, upload
->alignment
);
155 enum pipe_error ret
= PIPE_OK
;
157 if (upload
->offset
+ alloc_size
> upload
->size
) {
158 ret
= u_upload_alloc_buffer( upload
, alloc_size
);
163 /* Copy the data, using map_range if available:
165 my_buffer_write( upload
->screen
,
172 /* Emit the return values:
174 pipe_buffer_reference( outbuf
, upload
->buffer
);
175 *out_offset
= upload
->offset
;
176 upload
->offset
+= alloc_size
;
181 /* As above, but upload the full contents of a buffer. Useful for
182 * uploading user buffers, avoids generating an explosion of GPU
183 * buffers if you have an app that does lots of small vertex buffer
184 * renders or DrawElements calls.
186 enum pipe_error
u_upload_buffer( struct u_upload_mgr
*upload
,
189 struct pipe_buffer
*inbuf
,
190 unsigned *out_offset
,
191 struct pipe_buffer
**outbuf
)
193 enum pipe_error ret
= PIPE_OK
;
194 const char *map
= NULL
;
196 map
= (const char *)pipe_buffer_map(
197 upload
->screen
, inbuf
, PIPE_BUFFER_USAGE_CPU_READ
);
200 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
205 debug_printf("upload ptr %p ofs %d sz %d\n", map
, offset
, size
);
207 ret
= u_upload_data( upload
,
217 pipe_buffer_unmap( upload
->screen
, inbuf
);