2e2258609060105dc5e4638f325bda3f8f37db43
[mesa.git] / src / gallium / drivers / r600 / r600_buffer.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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:
24 * Jerome Glisse
25 * Corbin Simpson <MostAwesomeDude@gmail.com>
26 */
27 #include <pipe/p_screen.h>
28 #include <util/u_format.h>
29 #include <util/u_math.h>
30 #include <util/u_inlines.h>
31 #include <util/u_memory.h>
32 #include "util/u_upload_mgr.h"
33
34 #include "state_tracker/drm_driver.h"
35
36 #include <xf86drm.h>
37 #include "radeon_drm.h"
38
39 #include "r600.h"
40 #include "r600_pipe.h"
41
42 extern struct u_resource_vtbl r600_buffer_vtbl;
43
44 struct pipe_resource *r600_buffer_create(struct pipe_screen *screen,
45 const struct pipe_resource *templ)
46 {
47 struct r600_resource_buffer *rbuffer;
48 struct r600_bo *bo;
49 /* XXX We probably want a different alignment for buffers and textures. */
50 unsigned alignment = 4096;
51
52 rbuffer = CALLOC_STRUCT(r600_resource_buffer);
53 if (rbuffer == NULL)
54 return NULL;
55
56 rbuffer->magic = R600_BUFFER_MAGIC;
57 rbuffer->user_buffer = NULL;
58 rbuffer->r.base.b = *templ;
59 pipe_reference_init(&rbuffer->r.base.b.reference, 1);
60 rbuffer->r.base.b.screen = screen;
61 rbuffer->r.base.vtbl = &r600_buffer_vtbl;
62 rbuffer->r.size = rbuffer->r.base.b.width0;
63 rbuffer->r.bo_size = rbuffer->r.size;
64 bo = r600_bo((struct radeon*)screen->winsys, rbuffer->r.base.b.width0, alignment, rbuffer->r.base.b.bind, rbuffer->r.base.b.usage);
65 if (bo == NULL) {
66 FREE(rbuffer);
67 return NULL;
68 }
69 rbuffer->r.bo = bo;
70 return &rbuffer->r.base.b;
71 }
72
73 struct pipe_resource *r600_user_buffer_create(struct pipe_screen *screen,
74 void *ptr, unsigned bytes,
75 unsigned bind)
76 {
77 struct r600_resource_buffer *rbuffer;
78
79 rbuffer = CALLOC_STRUCT(r600_resource_buffer);
80 if (rbuffer == NULL)
81 return NULL;
82
83 rbuffer->magic = R600_BUFFER_MAGIC;
84 pipe_reference_init(&rbuffer->r.base.b.reference, 1);
85 rbuffer->r.base.vtbl = &r600_buffer_vtbl;
86 rbuffer->r.base.b.screen = screen;
87 rbuffer->r.base.b.target = PIPE_BUFFER;
88 rbuffer->r.base.b.format = PIPE_FORMAT_R8_UNORM;
89 rbuffer->r.base.b.usage = PIPE_USAGE_IMMUTABLE;
90 rbuffer->r.base.b.bind = bind;
91 rbuffer->r.base.b.width0 = bytes;
92 rbuffer->r.base.b.height0 = 1;
93 rbuffer->r.base.b.depth0 = 1;
94 rbuffer->r.base.b.array_size = 1;
95 rbuffer->r.base.b.flags = 0;
96 rbuffer->r.bo = NULL;
97 rbuffer->r.bo_size = 0;
98 rbuffer->user_buffer = ptr;
99 return &rbuffer->r.base.b;
100 }
101
102 static void r600_buffer_destroy(struct pipe_screen *screen,
103 struct pipe_resource *buf)
104 {
105 struct r600_resource_buffer *rbuffer = r600_buffer(buf);
106
107 if (rbuffer->r.bo) {
108 r600_bo_reference((struct radeon*)screen->winsys, &rbuffer->r.bo, NULL);
109 }
110 rbuffer->r.bo = NULL;
111 FREE(rbuffer);
112 }
113
114 static void *r600_buffer_transfer_map(struct pipe_context *pipe,
115 struct pipe_transfer *transfer)
116 {
117 struct r600_resource_buffer *rbuffer = r600_buffer(transfer->resource);
118 int write = 0;
119 uint8_t *data;
120
121 if (rbuffer->user_buffer)
122 return (uint8_t*)rbuffer->user_buffer + transfer->box.x;
123
124 if (transfer->usage & PIPE_TRANSFER_DONTBLOCK) {
125 /* FIXME */
126 }
127 if (transfer->usage & PIPE_TRANSFER_WRITE) {
128 write = 1;
129 }
130 data = r600_bo_map((struct radeon*)pipe->winsys, rbuffer->r.bo, transfer->usage, pipe);
131 if (!data)
132 return NULL;
133
134 return (uint8_t*)data + transfer->box.x;
135 }
136
137 static void r600_buffer_transfer_unmap(struct pipe_context *pipe,
138 struct pipe_transfer *transfer)
139 {
140 struct r600_resource_buffer *rbuffer = r600_buffer(transfer->resource);
141
142 if (rbuffer->user_buffer)
143 return;
144
145 if (rbuffer->r.bo)
146 r600_bo_unmap((struct radeon*)pipe->winsys, rbuffer->r.bo);
147 }
148
149 static void r600_buffer_transfer_flush_region(struct pipe_context *pipe,
150 struct pipe_transfer *transfer,
151 const struct pipe_box *box)
152 {
153 }
154
155 unsigned r600_buffer_is_referenced_by_cs(struct pipe_context *context,
156 struct pipe_resource *buf,
157 unsigned level, int layer)
158 {
159 /* FIXME */
160 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
161 }
162
163 struct pipe_resource *r600_buffer_from_handle(struct pipe_screen *screen,
164 struct winsys_handle *whandle)
165 {
166 struct radeon *rw = (struct radeon*)screen->winsys;
167 struct r600_resource *rbuffer;
168 struct r600_bo *bo = NULL;
169
170 bo = r600_bo_handle(rw, whandle->handle, NULL);
171 if (bo == NULL) {
172 return NULL;
173 }
174
175 rbuffer = CALLOC_STRUCT(r600_resource);
176 if (rbuffer == NULL) {
177 r600_bo_reference(rw, &bo, NULL);
178 return NULL;
179 }
180
181 pipe_reference_init(&rbuffer->base.b.reference, 1);
182 rbuffer->base.b.target = PIPE_BUFFER;
183 rbuffer->base.b.screen = screen;
184 rbuffer->base.vtbl = &r600_buffer_vtbl;
185 rbuffer->bo = bo;
186 return &rbuffer->base.b;
187 }
188
189 struct u_resource_vtbl r600_buffer_vtbl =
190 {
191 u_default_resource_get_handle, /* get_handle */
192 r600_buffer_destroy, /* resource_destroy */
193 r600_buffer_is_referenced_by_cs, /* is_buffer_referenced */
194 u_default_get_transfer, /* get_transfer */
195 u_default_transfer_destroy, /* transfer_destroy */
196 r600_buffer_transfer_map, /* transfer_map */
197 r600_buffer_transfer_flush_region, /* transfer_flush_region */
198 r600_buffer_transfer_unmap, /* transfer_unmap */
199 u_default_transfer_inline_write /* transfer_inline_write */
200 };
201
202 void r600_upload_index_buffer(struct r600_pipe_context *rctx, struct r600_drawl *draw)
203 {
204 struct r600_resource_buffer *rbuffer = r600_buffer(draw->index_buffer);
205 boolean flushed;
206
207 u_upload_data(rctx->upload_vb, 0,
208 draw->info.count * draw->index_size,
209 rbuffer->user_buffer,
210 &draw->index_buffer_offset,
211 &draw->index_buffer, &flushed);
212 }
213
214 void r600_upload_user_buffers(struct r600_pipe_context *rctx,
215 int min_index, int max_index)
216 {
217 int i, nr = rctx->vertex_elements->count;
218 unsigned count = max_index + 1 - min_index;
219 boolean flushed;
220 boolean uploaded[32] = {0};
221
222 for (i = 0; i < nr; i++) {
223 unsigned index = rctx->vertex_elements->elements[i].vertex_buffer_index;
224 struct pipe_vertex_buffer *vb = &rctx->vertex_buffer[index];
225 struct r600_resource_buffer *userbuf = r600_buffer(vb->buffer);
226
227 if (userbuf && userbuf->user_buffer && !uploaded[index]) {
228 unsigned first, size;
229
230 if (vb->stride) {
231 first = vb->stride * min_index;
232 size = vb->stride * count;
233 } else {
234 first = 0;
235 size = rctx->vertex_elements->hw_format_size[i];
236 }
237
238 u_upload_data(rctx->upload_vb, first, size,
239 (uint8_t*)userbuf->user_buffer + first,
240 &vb->buffer_offset,
241 &rctx->real_vertex_buffer[index],
242 &flushed);
243
244 vb->buffer_offset -= first;
245
246 /* vertex_arrays_dirty = TRUE; */
247 uploaded[index] = TRUE;
248 } else {
249 assert(rctx->real_vertex_buffer[index]);
250 }
251 }
252 }
253
254 void r600_upload_const_buffer(struct r600_pipe_context *rctx, struct r600_resource_buffer **rbuffer,
255 uint32_t *const_offset)
256 {
257 if ((*rbuffer)->user_buffer) {
258 uint8_t *ptr = (*rbuffer)->user_buffer;
259 unsigned size = (*rbuffer)->r.base.b.width0;
260 boolean flushed;
261
262 *rbuffer = NULL;
263
264 u_upload_data(rctx->upload_const, 0, size, ptr, const_offset,
265 (struct pipe_resource**)rbuffer, &flushed);
266 } else {
267 *const_offset = 0;
268 }
269 }