util: Enable assembly breakpointt on x86_64.
[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_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"
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 void
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, PIPE_BUFFER_USAGE_CPU_WRITE);
87 assert(map);
88 if(map) {
89 memcpy(map + offset, data, size);
90 pipe_buffer_flush_mapped_range(screen, buf, offset, dirty_size);
91 pipe_buffer_unmap(screen, buf);
92 }
93 }
94
95 /* Release old buffer.
96 *
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.
100 *
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.
103 */
104 void u_upload_flush( struct u_upload_mgr *upload )
105 {
106 pipe_buffer_reference( &upload->buffer, NULL );
107 upload->size = 0;
108 }
109
110
111 void u_upload_destroy( struct u_upload_mgr *upload )
112 {
113 u_upload_flush( upload );
114 FREE( upload );
115 }
116
117
118 static enum pipe_error
119 u_upload_alloc_buffer( struct u_upload_mgr *upload,
120 unsigned min_size )
121 {
122 /* Release old buffer, if present:
123 */
124 u_upload_flush( upload );
125
126 /* Allocate a new one:
127 */
128 upload->size = align(MAX2(upload->default_size, min_size), 4096);
129
130 upload->buffer = pipe_buffer_create( upload->screen,
131 upload->alignment,
132 upload->usage | PIPE_BUFFER_USAGE_CPU_WRITE,
133 upload->size );
134 if (upload->buffer == NULL)
135 goto fail;
136
137 upload->offset = 0;
138 return 0;
139
140 fail:
141 if (upload->buffer)
142 pipe_buffer_reference( &upload->buffer, NULL );
143
144 return PIPE_ERROR_OUT_OF_MEMORY;
145 }
146
147
148 enum pipe_error u_upload_data( struct u_upload_mgr *upload,
149 unsigned size,
150 const void *data,
151 unsigned *out_offset,
152 struct pipe_buffer **outbuf )
153 {
154 unsigned alloc_size = align( size, upload->alignment );
155 enum pipe_error ret = PIPE_OK;
156
157 if (upload->offset + alloc_size > upload->size) {
158 ret = u_upload_alloc_buffer( upload, alloc_size );
159 if (ret)
160 return ret;
161 }
162
163 /* Copy the data, using map_range if available:
164 */
165 my_buffer_write( upload->screen,
166 upload->buffer,
167 upload->offset,
168 size,
169 alloc_size,
170 data );
171
172 /* Emit the return values:
173 */
174 pipe_buffer_reference( outbuf, upload->buffer );
175 *out_offset = upload->offset;
176 upload->offset += alloc_size;
177 return PIPE_OK;
178 }
179
180
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.
185 */
186 enum pipe_error u_upload_buffer( struct u_upload_mgr *upload,
187 unsigned offset,
188 unsigned size,
189 struct pipe_buffer *inbuf,
190 unsigned *out_offset,
191 struct pipe_buffer **outbuf )
192 {
193 enum pipe_error ret = PIPE_OK;
194 const char *map = NULL;
195
196 map = (const char *)pipe_buffer_map(
197 upload->screen, inbuf, PIPE_BUFFER_USAGE_CPU_READ );
198
199 if (map == NULL) {
200 ret = PIPE_ERROR_OUT_OF_MEMORY;
201 goto done;
202 }
203
204 if (0)
205 debug_printf("upload ptr %p ofs %d sz %d\n", map, offset, size);
206
207 ret = u_upload_data( upload,
208 size,
209 map + offset,
210 out_offset,
211 outbuf );
212 if (ret)
213 goto done;
214
215 done:
216 if (map)
217 pipe_buffer_unmap( upload->screen, inbuf );
218
219 return ret;
220 }