Merge commit 'origin/master' into gallium-msaa
[mesa.git] / src / gallium / drivers / svga / svga_resource_buffer.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "svga_cmd.h"
27
28 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "os/os_thread.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34
35 #include "svga_context.h"
36 #include "svga_screen.h"
37 #include "svga_resource_buffer.h"
38 #include "svga_resource_buffer_upload.h"
39 #include "svga_winsys.h"
40 #include "svga_debug.h"
41
42
43 /**
44 * Vertex and index buffers need hardware backing. Constant buffers
45 * do not. No other types of buffers currently supported.
46 */
47 static INLINE boolean
48 svga_buffer_needs_hw_storage(unsigned usage)
49 {
50 return usage & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER);
51 }
52
53
54 static unsigned int
55 svga_buffer_is_referenced( struct pipe_context *pipe,
56 struct pipe_resource *buf,
57 unsigned face, unsigned level)
58 {
59 struct svga_screen *ss = svga_screen(pipe->screen);
60 struct svga_buffer *sbuf = svga_buffer(buf);
61
62 /**
63 * XXX: Check this.
64 * The screen may cache buffer writes, but when we map, we map out
65 * of those cached writes, so we don't need to set a
66 * PIPE_REFERENCED_FOR_WRITE flag for cached buffers.
67 */
68
69 if (!sbuf->handle || ss->sws->surface_is_flushed(ss->sws, sbuf->handle))
70 return PIPE_UNREFERENCED;
71
72 /**
73 * sws->surface_is_flushed() does not distinguish between read references
74 * and write references. So assume a reference is both,
75 * however, we make an exception for index- and vertex buffers, to avoid
76 * a flush in st_bufferobj_get_subdata, during display list replay.
77 */
78
79 if (sbuf->b.b.bind & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER))
80 return PIPE_REFERENCED_FOR_READ;
81
82 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
83 }
84
85
86
87
88
89
90 static void *
91 svga_buffer_map_range( struct pipe_screen *screen,
92 struct pipe_resource *buf,
93 unsigned offset,
94 unsigned length,
95 unsigned usage )
96 {
97 struct svga_screen *ss = svga_screen(screen);
98 struct svga_winsys_screen *sws = ss->sws;
99 struct svga_buffer *sbuf = svga_buffer( buf );
100 void *map;
101
102 if (!sbuf->swbuf && !sbuf->hwbuf) {
103 if (svga_buffer_create_hw_storage(ss, sbuf) != PIPE_OK) {
104 /*
105 * We can't create a hardware buffer big enough, so create a malloc
106 * buffer instead.
107 */
108 debug_printf("%s: failed to allocate %u KB of DMA, splitting DMA transfers\n",
109 __FUNCTION__,
110 (sbuf->b.b.width0 + 1023)/1024);
111
112 sbuf->swbuf = align_malloc(sbuf->b.b.width0, 16);
113 }
114 }
115
116 if (sbuf->swbuf) {
117 /* User/malloc buffer */
118 map = sbuf->swbuf;
119 }
120 else if (sbuf->hwbuf) {
121 map = sws->buffer_map(sws, sbuf->hwbuf, usage);
122 }
123 else {
124 map = NULL;
125 }
126
127 if(map) {
128 ++sbuf->map.count;
129
130 if (usage & PIPE_TRANSFER_WRITE) {
131 assert(sbuf->map.count <= 1);
132 sbuf->map.writing = TRUE;
133 if (usage & PIPE_TRANSFER_FLUSH_EXPLICIT)
134 sbuf->map.flush_explicit = TRUE;
135 }
136 }
137
138 return map;
139 }
140
141
142
143 static void
144 svga_buffer_flush_mapped_range( struct pipe_screen *screen,
145 struct pipe_resource *buf,
146 unsigned offset, unsigned length)
147 {
148 struct svga_buffer *sbuf = svga_buffer( buf );
149 struct svga_screen *ss = svga_screen(screen);
150
151 pipe_mutex_lock(ss->swc_mutex);
152 assert(sbuf->map.writing);
153 if(sbuf->map.writing) {
154 assert(sbuf->map.flush_explicit);
155 svga_buffer_add_range(sbuf, offset, offset + length);
156 }
157 pipe_mutex_unlock(ss->swc_mutex);
158 }
159
160 static void
161 svga_buffer_unmap( struct pipe_screen *screen,
162 struct pipe_resource *buf)
163 {
164 struct svga_screen *ss = svga_screen(screen);
165 struct svga_winsys_screen *sws = ss->sws;
166 struct svga_buffer *sbuf = svga_buffer( buf );
167
168 pipe_mutex_lock(ss->swc_mutex);
169
170 assert(sbuf->map.count);
171 if(sbuf->map.count)
172 --sbuf->map.count;
173
174 if(sbuf->hwbuf)
175 sws->buffer_unmap(sws, sbuf->hwbuf);
176
177 if(sbuf->map.writing) {
178 if(!sbuf->map.flush_explicit) {
179 /* No mapped range was flushed -- flush the whole buffer */
180 SVGA_DBG(DEBUG_DMA, "flushing the whole buffer\n");
181
182 svga_buffer_add_range(sbuf, 0, sbuf->b.b.width0);
183 }
184
185 sbuf->map.writing = FALSE;
186 sbuf->map.flush_explicit = FALSE;
187 }
188
189 pipe_mutex_unlock(ss->swc_mutex);
190 }
191
192
193
194 static void
195 svga_buffer_destroy( struct pipe_screen *screen,
196 struct pipe_resource *buf )
197 {
198 struct svga_screen *ss = svga_screen(screen);
199 struct svga_buffer *sbuf = svga_buffer( buf );
200
201 assert(!p_atomic_read(&buf->reference.count));
202
203 assert(!sbuf->dma.pending);
204
205 if(sbuf->handle)
206 svga_buffer_destroy_host_surface(ss, sbuf);
207
208 if(sbuf->uploaded.buffer)
209 pipe_resource_reference(&sbuf->uploaded.buffer, NULL);
210
211 if(sbuf->hwbuf)
212 svga_buffer_destroy_hw_storage(ss, sbuf);
213
214 if(sbuf->swbuf && !sbuf->user)
215 align_free(sbuf->swbuf);
216
217 FREE(sbuf);
218 }
219
220
221 /* Keep the original code more or less intact, implement transfers in
222 * terms of the old functions.
223 */
224 static void *
225 svga_buffer_transfer_map( struct pipe_context *pipe,
226 struct pipe_transfer *transfer )
227 {
228 uint8_t *map = svga_buffer_map_range( pipe->screen,
229 transfer->resource,
230 transfer->box.x,
231 transfer->box.width,
232 transfer->usage );
233 if (map == NULL)
234 return NULL;
235
236 /* map_buffer() returned a pointer to the beginning of the buffer,
237 * but transfers are expected to return a pointer to just the
238 * region specified in the box.
239 */
240 return map + transfer->box.x;
241 }
242
243
244
245 static void svga_buffer_transfer_flush_region( struct pipe_context *pipe,
246 struct pipe_transfer *transfer,
247 const struct pipe_box *box)
248 {
249 assert(box->x + box->width <= transfer->box.width);
250
251 svga_buffer_flush_mapped_range(pipe->screen,
252 transfer->resource,
253 transfer->box.x + box->x,
254 box->width);
255 }
256
257 static void svga_buffer_transfer_unmap( struct pipe_context *pipe,
258 struct pipe_transfer *transfer )
259 {
260 svga_buffer_unmap(pipe->screen,
261 transfer->resource);
262 }
263
264
265
266
267
268
269
270 struct u_resource_vtbl svga_buffer_vtbl =
271 {
272 u_default_resource_get_handle, /* get_handle */
273 svga_buffer_destroy, /* resource_destroy */
274 svga_buffer_is_referenced, /* is_resource_referenced */
275 u_default_get_transfer, /* get_transfer */
276 u_default_transfer_destroy, /* transfer_destroy */
277 svga_buffer_transfer_map, /* transfer_map */
278 svga_buffer_transfer_flush_region, /* transfer_flush_region */
279 svga_buffer_transfer_unmap, /* transfer_unmap */
280 u_default_transfer_inline_write /* transfer_inline_write */
281 };
282
283
284
285 struct pipe_resource *
286 svga_buffer_create(struct pipe_screen *screen,
287 const struct pipe_resource *template)
288 {
289 struct svga_screen *ss = svga_screen(screen);
290 struct svga_buffer *sbuf;
291
292 sbuf = CALLOC_STRUCT(svga_buffer);
293 if(!sbuf)
294 goto error1;
295
296 sbuf->b.b = *template;
297 sbuf->b.vtbl = &svga_buffer_vtbl;
298 pipe_reference_init(&sbuf->b.b.reference, 1);
299 sbuf->b.b.screen = screen;
300
301 if(svga_buffer_needs_hw_storage(template->bind)) {
302 if(svga_buffer_create_host_surface(ss, sbuf) != PIPE_OK)
303 goto error2;
304 }
305 else {
306 sbuf->swbuf = align_malloc(template->width0, 64);
307 if(!sbuf->swbuf)
308 goto error2;
309 }
310
311 return &sbuf->b.b;
312
313 error2:
314 FREE(sbuf);
315 error1:
316 return NULL;
317 }
318
319 struct pipe_resource *
320 svga_user_buffer_create(struct pipe_screen *screen,
321 void *ptr,
322 unsigned bytes,
323 unsigned bind)
324 {
325 struct svga_buffer *sbuf;
326
327 sbuf = CALLOC_STRUCT(svga_buffer);
328 if(!sbuf)
329 goto no_sbuf;
330
331 pipe_reference_init(&sbuf->b.b.reference, 1);
332 sbuf->b.vtbl = &svga_buffer_vtbl;
333 sbuf->b.b.screen = screen;
334 sbuf->b.b.format = PIPE_FORMAT_R8_UNORM; /* ?? */
335 sbuf->b.b.usage = PIPE_USAGE_IMMUTABLE;
336 sbuf->b.b.bind = bind;
337 sbuf->b.b.width0 = bytes;
338 sbuf->b.b.height0 = 1;
339 sbuf->b.b.depth0 = 1;
340
341 sbuf->swbuf = ptr;
342 sbuf->user = TRUE;
343
344 return &sbuf->b.b;
345
346 no_sbuf:
347 return NULL;
348 }
349
350
351