r300g: mapping buffers for read should be unsynchronized
[mesa.git] / src / gallium / drivers / r300 / r300_screen_buffer.c
1 /*
2 * Copyright 2010 Red Hat Inc.
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 * Authors: Dave Airlie
24 */
25
26 #include <stdio.h>
27
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "util/u_upload_mgr.h"
31 #include "util/u_math.h"
32
33 #include "r300_screen_buffer.h"
34
35 void r300_upload_index_buffer(struct r300_context *r300,
36 struct pipe_resource **index_buffer,
37 unsigned index_size, unsigned *start,
38 unsigned count, uint8_t *ptr)
39 {
40 unsigned index_offset;
41 boolean flushed;
42
43 *index_buffer = NULL;
44
45 u_upload_data(r300->vbuf_mgr->uploader,
46 0, count * index_size,
47 ptr + (*start * index_size),
48 &index_offset,
49 index_buffer, &flushed);
50
51 *start = index_offset / index_size;
52 }
53
54 static void r300_buffer_destroy(struct pipe_screen *screen,
55 struct pipe_resource *buf)
56 {
57 struct r300_screen *r300screen = r300_screen(screen);
58 struct r300_resource *rbuf = r300_resource(buf);
59
60 if (rbuf->constant_buffer)
61 FREE(rbuf->constant_buffer);
62
63 if (rbuf->buf)
64 pb_reference(&rbuf->buf, NULL);
65
66 util_slab_free(&r300screen->pool_buffers, rbuf);
67 }
68
69 static struct pipe_transfer*
70 r300_buffer_get_transfer(struct pipe_context *context,
71 struct pipe_resource *resource,
72 unsigned level,
73 unsigned usage,
74 const struct pipe_box *box)
75 {
76 struct r300_context *r300 = r300_context(context);
77 struct pipe_transfer *transfer =
78 util_slab_alloc(&r300->pool_transfers);
79
80 transfer->resource = resource;
81 transfer->level = level;
82 transfer->usage = usage;
83 transfer->box = *box;
84 transfer->stride = 0;
85 transfer->layer_stride = 0;
86 transfer->data = NULL;
87
88 /* Note strides are zero, this is ok for buffers, but not for
89 * textures 2d & higher at least.
90 */
91 return transfer;
92 }
93
94 static void r300_buffer_transfer_destroy(struct pipe_context *pipe,
95 struct pipe_transfer *transfer)
96 {
97 struct r300_context *r300 = r300_context(pipe);
98 util_slab_free(&r300->pool_transfers, transfer);
99 }
100
101 static void *
102 r300_buffer_transfer_map( struct pipe_context *pipe,
103 struct pipe_transfer *transfer )
104 {
105 struct r300_context *r300 = r300_context(pipe);
106 struct r300_screen *r300screen = r300_screen(pipe->screen);
107 struct radeon_winsys *rws = r300screen->rws;
108 struct r300_resource *rbuf = r300_resource(transfer->resource);
109 uint8_t *map;
110 enum pipe_transfer_usage usage;
111
112 if (rbuf->b.user_ptr)
113 return (uint8_t *) rbuf->b.user_ptr + transfer->box.x;
114 if (rbuf->constant_buffer)
115 return (uint8_t *) rbuf->constant_buffer + transfer->box.x;
116
117 /* Buffers are never used for write, therefore mapping for read can be
118 * unsynchronized. */
119 usage = transfer->usage;
120 if (!(usage & PIPE_TRANSFER_WRITE)) {
121 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
122 }
123
124 map = rws->buffer_map(rbuf->buf, r300->cs, usage);
125
126 if (map == NULL)
127 return NULL;
128
129 return map + transfer->box.x;
130 }
131
132 static void r300_buffer_transfer_unmap( struct pipe_context *pipe,
133 struct pipe_transfer *transfer )
134 {
135 struct r300_screen *r300screen = r300_screen(pipe->screen);
136 struct radeon_winsys *rws = r300screen->rws;
137 struct r300_resource *rbuf = r300_resource(transfer->resource);
138
139 if (rbuf->buf) {
140 rws->buffer_unmap(rbuf->buf);
141 }
142 }
143
144 static void r300_buffer_transfer_inline_write(struct pipe_context *pipe,
145 struct pipe_resource *resource,
146 unsigned level,
147 unsigned usage,
148 const struct pipe_box *box,
149 const void *data,
150 unsigned stride,
151 unsigned layer_stride)
152 {
153 struct r300_context *r300 = r300_context(pipe);
154 struct radeon_winsys *rws = r300->screen->rws;
155 struct r300_resource *rbuf = r300_resource(resource);
156 uint8_t *map = NULL;
157
158 if (rbuf->constant_buffer) {
159 memcpy(rbuf->constant_buffer + box->x, data, box->width);
160 return;
161 }
162 assert(rbuf->b.user_ptr == NULL);
163
164 map = rws->buffer_map(rbuf->buf, r300->cs,
165 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD | usage);
166
167 memcpy(map + box->x, data, box->width);
168
169 rws->buffer_unmap(rbuf->buf);
170 }
171
172 static const struct u_resource_vtbl r300_buffer_vtbl =
173 {
174 NULL, /* get_handle */
175 r300_buffer_destroy, /* resource_destroy */
176 r300_buffer_get_transfer, /* get_transfer */
177 r300_buffer_transfer_destroy, /* transfer_destroy */
178 r300_buffer_transfer_map, /* transfer_map */
179 NULL, /* transfer_flush_region */
180 r300_buffer_transfer_unmap, /* transfer_unmap */
181 r300_buffer_transfer_inline_write /* transfer_inline_write */
182 };
183
184 struct pipe_resource *r300_buffer_create(struct pipe_screen *screen,
185 const struct pipe_resource *templ)
186 {
187 struct r300_screen *r300screen = r300_screen(screen);
188 struct r300_resource *rbuf;
189 unsigned alignment = 16;
190
191 rbuf = util_slab_alloc(&r300screen->pool_buffers);
192
193 rbuf->b.b.b = *templ;
194 rbuf->b.b.vtbl = &r300_buffer_vtbl;
195 pipe_reference_init(&rbuf->b.b.b.reference, 1);
196 rbuf->b.b.b.screen = screen;
197 rbuf->b.user_ptr = NULL;
198 rbuf->domain = RADEON_DOMAIN_GTT;
199 rbuf->buf = NULL;
200 rbuf->constant_buffer = NULL;
201
202 /* Alloc constant buffers in RAM. */
203 if (templ->bind & PIPE_BIND_CONSTANT_BUFFER) {
204 rbuf->constant_buffer = MALLOC(templ->width0);
205 return &rbuf->b.b.b;
206 }
207
208 rbuf->buf =
209 r300screen->rws->buffer_create(r300screen->rws,
210 rbuf->b.b.b.width0, alignment,
211 rbuf->b.b.b.bind, rbuf->domain);
212 if (!rbuf->buf) {
213 util_slab_free(&r300screen->pool_buffers, rbuf);
214 return NULL;
215 }
216
217 rbuf->cs_buf =
218 r300screen->rws->buffer_get_cs_handle(rbuf->buf);
219
220 return &rbuf->b.b.b;
221 }
222
223 struct pipe_resource *r300_user_buffer_create(struct pipe_screen *screen,
224 void *ptr, unsigned size,
225 unsigned bind)
226 {
227 struct r300_screen *r300screen = r300_screen(screen);
228 struct r300_resource *rbuf;
229
230 rbuf = util_slab_alloc(&r300screen->pool_buffers);
231
232 pipe_reference_init(&rbuf->b.b.b.reference, 1);
233 rbuf->b.b.b.screen = screen;
234 rbuf->b.b.b.target = PIPE_BUFFER;
235 rbuf->b.b.b.format = PIPE_FORMAT_R8_UNORM;
236 rbuf->b.b.b.usage = PIPE_USAGE_IMMUTABLE;
237 rbuf->b.b.b.bind = bind;
238 rbuf->b.b.b.width0 = ~0;
239 rbuf->b.b.b.height0 = 1;
240 rbuf->b.b.b.depth0 = 1;
241 rbuf->b.b.b.array_size = 1;
242 rbuf->b.b.b.flags = 0;
243 rbuf->b.b.vtbl = &r300_buffer_vtbl;
244 rbuf->b.user_ptr = ptr;
245 rbuf->domain = RADEON_DOMAIN_GTT;
246 rbuf->buf = NULL;
247 rbuf->constant_buffer = NULL;
248 return &rbuf->b.b.b;
249 }