2 * Copyright 2010 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Authors: Dave Airlie
27 #include "util/u_inlines.h"
28 #include "util/u_format.h"
29 #include "util/u_memory.h"
30 #include "util/u_upload_mgr.h"
31 #include "util/u_math.h"
33 #include "r300_screen_buffer.h"
35 #include "r300_winsys.h"
37 static unsigned r300_buffer_is_referenced(struct pipe_context *context,
38 struct pipe_resource *buf,
39 unsigned face, unsigned level)
41 struct r300_context *r300 = r300_context(context);
42 struct r300_buffer *rbuf = r300_buffer(buf);
44 if (r300_buffer_is_user_buffer(buf))
45 return PIPE_UNREFERENCED;
47 if (r300->rws->is_buffer_referenced(r300->rws, rbuf->buf))
48 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
50 return PIPE_UNREFERENCED;
53 /* External helper, not required to implent u_resource_vtbl:
55 int r300_upload_index_buffer(struct r300_context *r300,
56 struct pipe_resource **index_buffer,
61 struct pipe_resource *upload_buffer = NULL;
62 unsigned index_offset = start * index_size;
65 if (r300_buffer_is_user_buffer(*index_buffer)) {
66 ret = u_upload_buffer(r300->upload_ib,
75 *index_buffer = upload_buffer;
79 // pipe_resource_reference(&upload_buffer, NULL);
83 /* External helper, not required to implent u_resource_vtbl:
85 int r300_upload_user_buffers(struct r300_context *r300)
87 enum pipe_error ret = PIPE_OK;
90 nr = r300->vertex_buffer_count;
92 for (i = 0; i < nr; i++) {
94 if (r300_buffer_is_user_buffer(r300->vertex_buffer[i].buffer)) {
95 struct pipe_resource *upload_buffer = NULL;
96 unsigned offset = 0; /*r300->vertex_buffer[i].buffer_offset * 4;*/
97 unsigned size = r300->vertex_buffer[i].buffer->width0;
98 unsigned upload_offset;
99 ret = u_upload_buffer(r300->upload_vb,
101 r300->vertex_buffer[i].buffer,
102 &upload_offset, &upload_buffer);
106 pipe_resource_reference(&r300->vertex_buffer[i].buffer, NULL);
107 r300->vertex_buffer[i].buffer = upload_buffer;
108 r300->vertex_buffer[i].buffer_offset = upload_offset;
114 static struct r300_winsys_buffer *
115 r300_winsys_buffer_create(struct r300_screen *r300screen,
120 struct r300_winsys_screen *rws = r300screen->rws;
121 struct r300_winsys_buffer *buf;
123 buf = rws->buffer_create(rws, alignment, usage, size);
127 static void r300_winsys_buffer_destroy(struct r300_screen *r300screen,
128 struct r300_buffer *rbuf)
130 struct r300_winsys_screen *rws = r300screen->rws;
133 rws->buffer_reference(rws, &rbuf->buf, NULL);
139 static void r300_buffer_destroy(struct pipe_screen *screen,
140 struct pipe_resource *buf)
142 struct r300_screen *r300screen = r300_screen(screen);
143 struct r300_buffer *rbuf = r300_buffer(buf);
145 r300_winsys_buffer_destroy(r300screen, rbuf);
150 r300_buffer_map_range(struct pipe_screen *screen,
151 struct pipe_resource *buf,
152 unsigned offset, unsigned length,
155 struct r300_screen *r300screen = r300_screen(screen);
156 struct r300_winsys_screen *rws = r300screen->rws;
157 struct r300_buffer *rbuf = r300_buffer(buf);
162 if (rbuf->user_buffer)
163 return rbuf->user_buffer;
165 if (rbuf->b.b.bind & PIPE_BIND_CONSTANT_BUFFER) {
169 /* check if the mapping is to a range we already flushed */
170 if (usage & PIPE_TRANSFER_DISCARD) {
171 for (i = 0; i < rbuf->num_ranges; i++) {
173 if ((offset >= rbuf->ranges[i].start) &&
174 (offset < rbuf->ranges[i].end))
178 /* unreference this hw buffer and allocate a new one */
179 rws->buffer_reference(rws, &rbuf->buf, NULL);
181 rbuf->num_ranges = 0;
183 rbuf->buf = r300_winsys_buffer_create(r300screen,
185 rbuf->b.b.bind, /* XXX */
192 map = rws->buffer_map(rws, rbuf->buf, usage);
198 r300_buffer_flush_mapped_range( struct pipe_screen *screen,
199 struct pipe_resource *buf,
203 struct r300_buffer *rbuf = r300_buffer(buf);
206 if (rbuf->user_buffer)
209 if (rbuf->b.b.bind & PIPE_BIND_CONSTANT_BUFFER)
212 /* mark the range as used */
213 for(i = 0; i < rbuf->num_ranges; ++i) {
214 if(offset <= rbuf->ranges[i].end && rbuf->ranges[i].start <= (offset+length)) {
215 rbuf->ranges[i].start = MIN2(rbuf->ranges[i].start, offset);
216 rbuf->ranges[i].end = MAX2(rbuf->ranges[i].end, (offset+length));
221 rbuf->ranges[rbuf->num_ranges].start = offset;
222 rbuf->ranges[rbuf->num_ranges].end = offset+length;
228 r300_buffer_unmap(struct pipe_screen *screen,
229 struct pipe_resource *buf)
231 struct r300_screen *r300screen = r300_screen(screen);
232 struct r300_winsys_screen *rws = r300screen->rws;
233 struct r300_buffer *rbuf = r300_buffer(buf);
236 rws->buffer_unmap(rws, rbuf->buf);
243 /* As a first step, keep the original code intact, implement buffer
244 * transfers in terms of the old map/unmap functions.
246 * Utility functions for transfer create/destroy are hooked in and
247 * just record the arguments to those functions.
250 r300_buffer_transfer_map( struct pipe_context *pipe,
251 struct pipe_transfer *transfer )
253 uint8_t *map = r300_buffer_map_range( pipe->screen,
261 /* map_buffer() returned a pointer to the beginning of the buffer,
262 * but transfers are expected to return a pointer to just the
263 * region specified in the box.
265 return map + transfer->box.x;
270 static void r300_buffer_transfer_flush_region( struct pipe_context *pipe,
271 struct pipe_transfer *transfer,
272 const struct pipe_box *box)
274 assert(box->x + box->width <= transfer->box.width);
276 r300_buffer_flush_mapped_range(pipe->screen,
278 transfer->box.x + box->x,
282 static void r300_buffer_transfer_unmap( struct pipe_context *pipe,
283 struct pipe_transfer *transfer )
285 r300_buffer_unmap(pipe->screen,
292 struct u_resource_vtbl r300_buffer_vtbl =
294 u_default_resource_get_handle, /* get_handle */
295 r300_buffer_destroy, /* resource_destroy */
296 r300_buffer_is_referenced, /* is_buffer_referenced */
297 u_default_get_transfer, /* get_transfer */
298 u_default_transfer_destroy, /* transfer_destroy */
299 r300_buffer_transfer_map, /* transfer_map */
300 r300_buffer_transfer_flush_region, /* transfer_flush_region */
301 r300_buffer_transfer_unmap, /* transfer_unmap */
302 u_default_transfer_inline_write /* transfer_inline_write */
308 struct pipe_resource *r300_buffer_create(struct pipe_screen *screen,
309 const struct pipe_resource *template)
311 struct r300_screen *r300screen = r300_screen(screen);
312 struct r300_buffer *rbuf;
313 unsigned alignment = 16;
315 rbuf = CALLOC_STRUCT(r300_buffer);
319 rbuf->magic = R300_BUFFER_MAGIC;
321 rbuf->b.b = *template;
322 rbuf->b.vtbl = &r300_buffer_vtbl;
323 pipe_reference_init(&rbuf->b.b.reference, 1);
324 rbuf->b.b.screen = screen;
326 if (rbuf->b.b.bind & R300_BIND_OQBO)
329 rbuf->buf = r300_winsys_buffer_create(r300screen,
345 struct pipe_resource *r300_user_buffer_create(struct pipe_screen *screen,
350 struct r300_buffer *rbuf;
352 rbuf = CALLOC_STRUCT(r300_buffer);
356 rbuf->magic = R300_BUFFER_MAGIC;
358 pipe_reference_init(&rbuf->b.b.reference, 1);
359 rbuf->b.vtbl = &r300_buffer_vtbl;
360 rbuf->b.b.screen = screen;
361 rbuf->b.b.format = PIPE_FORMAT_R8_UNORM;
362 rbuf->b.b._usage = PIPE_USAGE_IMMUTABLE;
363 rbuf->b.b.bind = bind;
364 rbuf->b.b.width0 = bytes;
365 rbuf->b.b.height0 = 1;
366 rbuf->b.b.depth0 = 1;
368 rbuf->user_buffer = ptr;