r600g: Split r600_bc_alu_src.
[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 static void r600_buffer_destroy(struct pipe_screen *screen,
43 struct pipe_resource *buf)
44 {
45 struct r600_resource_buffer *rbuffer = r600_buffer(buf);
46
47 if (rbuffer->r.bo) {
48 r600_bo_reference((struct radeon*)screen->winsys, &rbuffer->r.bo, NULL);
49 }
50 rbuffer->r.bo = NULL;
51 FREE(rbuffer);
52 }
53
54 static unsigned r600_buffer_is_referenced_by_cs(struct pipe_context *context,
55 struct pipe_resource *buf,
56 unsigned level, int layer)
57 {
58 /* FIXME */
59 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
60 }
61
62 static void *r600_buffer_transfer_map(struct pipe_context *pipe,
63 struct pipe_transfer *transfer)
64 {
65 struct r600_resource_buffer *rbuffer = r600_buffer(transfer->resource);
66 int write = 0;
67 uint8_t *data;
68
69 if (rbuffer->r.b.user_ptr)
70 return (uint8_t*)rbuffer->r.b.user_ptr + transfer->box.x;
71
72 if (transfer->usage & PIPE_TRANSFER_DONTBLOCK) {
73 /* FIXME */
74 }
75 if (transfer->usage & PIPE_TRANSFER_WRITE) {
76 write = 1;
77 }
78 data = r600_bo_map((struct radeon*)pipe->winsys, rbuffer->r.bo, transfer->usage, pipe);
79 if (!data)
80 return NULL;
81
82 return (uint8_t*)data + transfer->box.x;
83 }
84
85 static void r600_buffer_transfer_unmap(struct pipe_context *pipe,
86 struct pipe_transfer *transfer)
87 {
88 struct r600_resource_buffer *rbuffer = r600_buffer(transfer->resource);
89
90 if (rbuffer->r.b.user_ptr)
91 return;
92
93 if (rbuffer->r.bo)
94 r600_bo_unmap((struct radeon*)pipe->winsys, rbuffer->r.bo);
95 }
96
97 static void r600_buffer_transfer_flush_region(struct pipe_context *pipe,
98 struct pipe_transfer *transfer,
99 const struct pipe_box *box)
100 {
101 }
102
103 static const struct u_resource_vtbl r600_buffer_vtbl =
104 {
105 u_default_resource_get_handle, /* get_handle */
106 r600_buffer_destroy, /* resource_destroy */
107 r600_buffer_is_referenced_by_cs, /* is_buffer_referenced */
108 u_default_get_transfer, /* get_transfer */
109 u_default_transfer_destroy, /* transfer_destroy */
110 r600_buffer_transfer_map, /* transfer_map */
111 r600_buffer_transfer_flush_region, /* transfer_flush_region */
112 r600_buffer_transfer_unmap, /* transfer_unmap */
113 u_default_transfer_inline_write /* transfer_inline_write */
114 };
115
116 struct pipe_resource *r600_buffer_create(struct pipe_screen *screen,
117 const struct pipe_resource *templ)
118 {
119 struct r600_resource_buffer *rbuffer;
120 struct r600_bo *bo;
121 /* XXX We probably want a different alignment for buffers and textures. */
122 unsigned alignment = 4096;
123
124 rbuffer = CALLOC_STRUCT(r600_resource_buffer);
125 if (rbuffer == NULL)
126 return NULL;
127
128 rbuffer->magic = R600_BUFFER_MAGIC;
129 rbuffer->r.b.b.b = *templ;
130 pipe_reference_init(&rbuffer->r.b.b.b.reference, 1);
131 rbuffer->r.b.b.b.screen = screen;
132 rbuffer->r.b.b.vtbl = &r600_buffer_vtbl;
133 rbuffer->r.b.user_ptr = NULL;
134 rbuffer->r.size = rbuffer->r.b.b.b.width0;
135 rbuffer->r.bo_size = rbuffer->r.size;
136
137 bo = r600_bo((struct radeon*)screen->winsys,
138 rbuffer->r.b.b.b.width0,
139 alignment, rbuffer->r.b.b.b.bind,
140 rbuffer->r.b.b.b.usage);
141
142 if (bo == NULL) {
143 FREE(rbuffer);
144 return NULL;
145 }
146 rbuffer->r.bo = bo;
147 return &rbuffer->r.b.b.b;
148 }
149
150 struct pipe_resource *r600_user_buffer_create(struct pipe_screen *screen,
151 void *ptr, unsigned bytes,
152 unsigned bind)
153 {
154 struct r600_resource_buffer *rbuffer;
155
156 rbuffer = CALLOC_STRUCT(r600_resource_buffer);
157 if (rbuffer == NULL)
158 return NULL;
159
160 rbuffer->magic = R600_BUFFER_MAGIC;
161 pipe_reference_init(&rbuffer->r.b.b.b.reference, 1);
162 rbuffer->r.b.b.vtbl = &r600_buffer_vtbl;
163 rbuffer->r.b.b.b.screen = screen;
164 rbuffer->r.b.b.b.target = PIPE_BUFFER;
165 rbuffer->r.b.b.b.format = PIPE_FORMAT_R8_UNORM;
166 rbuffer->r.b.b.b.usage = PIPE_USAGE_IMMUTABLE;
167 rbuffer->r.b.b.b.bind = bind;
168 rbuffer->r.b.b.b.width0 = bytes;
169 rbuffer->r.b.b.b.height0 = 1;
170 rbuffer->r.b.b.b.depth0 = 1;
171 rbuffer->r.b.b.b.array_size = 1;
172 rbuffer->r.b.b.b.flags = 0;
173 rbuffer->r.b.user_ptr = ptr;
174 rbuffer->r.bo = NULL;
175 rbuffer->r.bo_size = 0;
176 return &rbuffer->r.b.b.b;
177 }
178
179 struct pipe_resource *r600_buffer_from_handle(struct pipe_screen *screen,
180 struct winsys_handle *whandle)
181 {
182 struct radeon *rw = (struct radeon*)screen->winsys;
183 struct r600_resource *rbuffer;
184 struct r600_bo *bo = NULL;
185
186 bo = r600_bo_handle(rw, whandle->handle, NULL);
187 if (bo == NULL) {
188 return NULL;
189 }
190
191 rbuffer = CALLOC_STRUCT(r600_resource);
192 if (rbuffer == NULL) {
193 r600_bo_reference(rw, &bo, NULL);
194 return NULL;
195 }
196
197 pipe_reference_init(&rbuffer->b.b.b.reference, 1);
198 rbuffer->b.b.b.target = PIPE_BUFFER;
199 rbuffer->b.b.b.screen = screen;
200 rbuffer->b.b.vtbl = &r600_buffer_vtbl;
201 rbuffer->bo = bo;
202 return &rbuffer->b.b.b;
203 }
204
205 void r600_upload_index_buffer(struct r600_pipe_context *rctx, struct r600_drawl *draw)
206 {
207 struct r600_resource_buffer *rbuffer = r600_buffer(draw->index_buffer);
208 boolean flushed;
209
210 u_upload_data(rctx->upload_ib, 0,
211 draw->info.count * draw->index_size,
212 rbuffer->r.b.user_ptr,
213 &draw->index_buffer_offset,
214 &draw->index_buffer, &flushed);
215 }
216
217 void r600_upload_const_buffer(struct r600_pipe_context *rctx, struct r600_resource_buffer **rbuffer,
218 uint32_t *const_offset)
219 {
220 if ((*rbuffer)->r.b.user_ptr) {
221 uint8_t *ptr = (*rbuffer)->r.b.user_ptr;
222 unsigned size = (*rbuffer)->r.b.b.b.width0;
223 boolean flushed;
224
225 *rbuffer = NULL;
226
227 u_upload_data(rctx->upload_const, 0, size, ptr, const_offset,
228 (struct pipe_resource**)rbuffer, &flushed);
229 } else {
230 *const_offset = 0;
231 }
232 }