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 boolean r300_buffer_is_referenced(struct r300_context *r300,
38 struct pipe_buffer *buf)
40 struct r300_buffer *rbuf = r300_buffer(buf);
41 if (r300_buffer_is_user_buffer(buf))
44 return r300->rws->is_buffer_referenced(r300->rws, rbuf->buf);
47 int r300_upload_index_buffer(struct r300_context *r300,
48 struct pipe_buffer **index_buffer,
53 struct pipe_buffer *upload_buffer = NULL;
54 unsigned index_offset = start * index_size;
57 if (r300_buffer_is_user_buffer(*index_buffer)) {
58 ret = u_upload_buffer(r300->upload_ib,
67 *index_buffer = upload_buffer;
71 // pipe_buffer_reference(&upload_buffer, NULL);
75 int r300_upload_user_buffers(struct r300_context *r300)
77 enum pipe_error ret = PIPE_OK;
80 nr = r300->vertex_buffer_count;
82 for (i = 0; i < nr; i++) {
84 if (r300_buffer_is_user_buffer(r300->vertex_buffer[i].buffer)) {
85 struct pipe_buffer *upload_buffer = NULL;
86 unsigned offset = 0; /*r300->vertex_buffer[i].buffer_offset * 4;*/
87 unsigned size = r300->vertex_buffer[i].buffer->size;
88 unsigned upload_offset;
89 ret = u_upload_buffer(r300->upload_vb,
91 r300->vertex_buffer[i].buffer,
92 &upload_offset, &upload_buffer);
96 pipe_buffer_reference(&r300->vertex_buffer[i].buffer, NULL);
97 r300->vertex_buffer[i].buffer = upload_buffer;
98 r300->vertex_buffer[i].buffer_offset = upload_offset;
104 static struct r300_winsys_buffer *
105 r300_winsys_buffer_create(struct r300_screen *r300screen,
110 struct r300_winsys_screen *rws = r300screen->rws;
111 struct r300_winsys_buffer *buf;
113 buf = rws->buffer_create(rws, alignment, usage, size);
117 static void r300_winsys_buffer_destroy(struct r300_screen *r300screen,
118 struct r300_buffer *rbuf)
120 struct r300_winsys_screen *rws = r300screen->rws;
123 rws->buffer_reference(rws, &rbuf->buf, NULL);
128 static struct pipe_buffer *r300_buffer_create(struct pipe_screen *screen,
133 struct r300_screen *r300screen = r300_screen(screen);
134 struct r300_buffer *rbuf;
136 rbuf = CALLOC_STRUCT(r300_buffer);
140 rbuf->magic = R300_BUFFER_MAGIC;
142 pipe_reference_init(&rbuf->base.reference, 1);
143 rbuf->base.screen = screen;
144 rbuf->base.alignment = alignment;
145 rbuf->base.usage = usage;
146 rbuf->base.size = size;
148 rbuf->buf = r300_winsys_buffer_create(r300screen,
164 static struct pipe_buffer *r300_user_buffer_create(struct pipe_screen *screen,
168 struct r300_buffer *rbuf;
170 rbuf = CALLOC_STRUCT(r300_buffer);
174 rbuf->magic = R300_BUFFER_MAGIC;
176 pipe_reference_init(&rbuf->base.reference, 1);
177 rbuf->base.screen = screen;
178 rbuf->base.alignment = 1;
179 rbuf->base.usage = 0;
180 rbuf->base.size = bytes;
182 rbuf->user_buffer = ptr;
189 static void r300_buffer_destroy(struct pipe_buffer *buf)
191 struct r300_screen *r300screen = r300_screen(buf->screen);
192 struct r300_buffer *rbuf = r300_buffer(buf);
194 r300_winsys_buffer_destroy(r300screen, rbuf);
199 r300_buffer_map_range(struct pipe_screen *screen,
200 struct pipe_buffer *buf,
201 unsigned offset, unsigned length,
204 struct r300_screen *r300screen = r300_screen(screen);
205 struct r300_winsys_screen *rws = r300screen->rws;
206 struct r300_buffer *rbuf = r300_buffer(buf);
211 if (rbuf->user_buffer)
212 return rbuf->user_buffer;
214 if (rbuf->base.usage & PIPE_BUFFER_USAGE_CONSTANT)
217 /* check if the mapping is to a range we already flushed */
218 if (usage & PIPE_BUFFER_USAGE_DISCARD) {
219 for (i = 0; i < rbuf->num_ranges; i++) {
221 if ((offset >= rbuf->ranges[i].start) &&
222 (offset < rbuf->ranges[i].end))
226 /* unreference this hw buffer and allocate a new one */
227 rws->buffer_reference(rws, &rbuf->buf, NULL);
229 rbuf->num_ranges = 0;
231 rbuf->buf = r300_winsys_buffer_create(r300screen,
232 rbuf->base.alignment,
240 map = rws->buffer_map(rws, rbuf->buf, usage);
246 r300_buffer_flush_mapped_range( struct pipe_screen *screen,
247 struct pipe_buffer *buf,
251 struct r300_buffer *rbuf = r300_buffer(buf);
254 if (rbuf->user_buffer)
257 if (rbuf->base.usage & PIPE_BUFFER_USAGE_CONSTANT)
260 /* mark the range as used */
261 for(i = 0; i < rbuf->num_ranges; ++i) {
262 if(offset <= rbuf->ranges[i].end && rbuf->ranges[i].start <= (offset+length)) {
263 rbuf->ranges[i].start = MIN2(rbuf->ranges[i].start, offset);
264 rbuf->ranges[i].end = MAX2(rbuf->ranges[i].end, (offset+length));
269 rbuf->ranges[rbuf->num_ranges].start = offset;
270 rbuf->ranges[rbuf->num_ranges].end = offset+length;
275 r300_buffer_map(struct pipe_screen *screen,
276 struct pipe_buffer *buf,
279 struct r300_screen *r300screen = r300_screen(screen);
280 struct r300_winsys_screen *rws = r300screen->rws;
281 struct r300_buffer *rbuf = r300_buffer(buf);
284 if (rbuf->user_buffer)
285 return rbuf->user_buffer;
287 map = rws->buffer_map(rws, rbuf->buf, usage);
293 r300_buffer_unmap(struct pipe_screen *screen,
294 struct pipe_buffer *buf)
296 struct r300_screen *r300screen = r300_screen(screen);
297 struct r300_winsys_screen *rws = r300screen->rws;
298 struct r300_buffer *rbuf = r300_buffer(buf);
301 rws->buffer_unmap(rws, rbuf->buf);
305 void r300_screen_init_buffer_functions(struct r300_screen *r300screen)
307 r300screen->screen.buffer_create = r300_buffer_create;
308 r300screen->screen.user_buffer_create = r300_user_buffer_create;
309 r300screen->screen.buffer_map = r300_buffer_map;
310 r300screen->screen.buffer_map_range = r300_buffer_map_range;
311 r300screen->screen.buffer_flush_mapped_range = r300_buffer_flush_mapped_range;
312 r300screen->screen.buffer_unmap = r300_buffer_unmap;
313 r300screen->screen.buffer_destroy = r300_buffer_destroy;