r600g: remove unused function r600_buffer_from_handle
[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 <byteswap.h>
28
29 #include "pipe/p_screen.h"
30 #include "util/u_format.h"
31 #include "util/u_math.h"
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34 #include "util/u_upload_mgr.h"
35
36 #include "r600.h"
37 #include "r600_pipe.h"
38
39 static void r600_buffer_destroy(struct pipe_screen *screen,
40 struct pipe_resource *buf)
41 {
42 struct r600_screen *rscreen = (struct r600_screen*)screen;
43 struct r600_resource *rbuffer = r600_resource(buf);
44
45 if (rbuffer->bo) {
46 r600_bo_reference(&rbuffer->bo, NULL);
47 }
48 rbuffer->bo = NULL;
49 util_slab_free(&rscreen->pool_buffers, rbuffer);
50 }
51
52 static struct pipe_transfer *r600_get_transfer(struct pipe_context *ctx,
53 struct pipe_resource *resource,
54 unsigned level,
55 unsigned usage,
56 const struct pipe_box *box)
57 {
58 struct r600_pipe_context *rctx = (struct r600_pipe_context*)ctx;
59 struct pipe_transfer *transfer = util_slab_alloc(&rctx->pool_transfers);
60
61 transfer->resource = resource;
62 transfer->level = level;
63 transfer->usage = usage;
64 transfer->box = *box;
65 transfer->stride = 0;
66 transfer->layer_stride = 0;
67 transfer->data = NULL;
68
69 /* Note strides are zero, this is ok for buffers, but not for
70 * textures 2d & higher at least.
71 */
72 return transfer;
73 }
74
75 static void *r600_buffer_transfer_map(struct pipe_context *pipe,
76 struct pipe_transfer *transfer)
77 {
78 struct r600_resource *rbuffer = r600_resource(transfer->resource);
79 struct r600_pipe_context *rctx = (struct r600_pipe_context*)pipe;
80 uint8_t *data;
81
82 if (rbuffer->b.user_ptr)
83 return (uint8_t*)rbuffer->b.user_ptr + transfer->box.x;
84
85 data = r600_bo_map(rctx->screen->radeon, rbuffer->bo, rctx->ctx.cs, transfer->usage);
86 if (!data)
87 return NULL;
88
89 return (uint8_t*)data + transfer->box.x;
90 }
91
92 static void r600_buffer_transfer_unmap(struct pipe_context *pipe,
93 struct pipe_transfer *transfer)
94 {
95 struct r600_resource *rbuffer = r600_resource(transfer->resource);
96 struct r600_pipe_context *rctx = (struct r600_pipe_context*)pipe;
97
98 if (rbuffer->b.user_ptr)
99 return;
100
101 if (rbuffer->bo)
102 r600_bo_unmap(rctx->screen->radeon, rbuffer->bo);
103 }
104
105 static void r600_buffer_transfer_flush_region(struct pipe_context *pipe,
106 struct pipe_transfer *transfer,
107 const struct pipe_box *box)
108 {
109 }
110
111 static void r600_transfer_destroy(struct pipe_context *ctx,
112 struct pipe_transfer *transfer)
113 {
114 struct r600_pipe_context *rctx = (struct r600_pipe_context*)ctx;
115 util_slab_free(&rctx->pool_transfers, transfer);
116 }
117
118 static void r600_buffer_transfer_inline_write(struct pipe_context *pipe,
119 struct pipe_resource *resource,
120 unsigned level,
121 unsigned usage,
122 const struct pipe_box *box,
123 const void *data,
124 unsigned stride,
125 unsigned layer_stride)
126 {
127 struct r600_pipe_context *rctx = (struct r600_pipe_context*)pipe;
128 struct radeon *radeon = rctx->screen->radeon;
129 struct r600_resource *rbuffer = r600_resource(resource);
130 uint8_t *map = NULL;
131
132 assert(rbuffer->b.user_ptr == NULL);
133
134 map = r600_bo_map(radeon, rbuffer->bo, rctx->ctx.cs,
135 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD | usage);
136
137 memcpy(map + box->x, data, box->width);
138
139 if (rbuffer->bo)
140 r600_bo_unmap(radeon, rbuffer->bo);
141 }
142
143 static const struct u_resource_vtbl r600_buffer_vtbl =
144 {
145 u_default_resource_get_handle, /* get_handle */
146 r600_buffer_destroy, /* resource_destroy */
147 r600_get_transfer, /* get_transfer */
148 r600_transfer_destroy, /* transfer_destroy */
149 r600_buffer_transfer_map, /* transfer_map */
150 r600_buffer_transfer_flush_region, /* transfer_flush_region */
151 r600_buffer_transfer_unmap, /* transfer_unmap */
152 r600_buffer_transfer_inline_write /* transfer_inline_write */
153 };
154
155 struct pipe_resource *r600_buffer_create(struct pipe_screen *screen,
156 const struct pipe_resource *templ)
157 {
158 struct r600_screen *rscreen = (struct r600_screen*)screen;
159 struct r600_resource *rbuffer;
160 struct r600_bo *bo;
161 /* XXX We probably want a different alignment for buffers and textures. */
162 unsigned alignment = 4096;
163
164 rbuffer = util_slab_alloc(&rscreen->pool_buffers);
165
166 rbuffer->b.b.b = *templ;
167 pipe_reference_init(&rbuffer->b.b.b.reference, 1);
168 rbuffer->b.b.b.screen = screen;
169 rbuffer->b.b.vtbl = &r600_buffer_vtbl;
170 rbuffer->b.user_ptr = NULL;
171 rbuffer->size = rbuffer->b.b.b.width0;
172 rbuffer->bo_size = rbuffer->size;
173
174 bo = r600_bo(rscreen->radeon,
175 rbuffer->b.b.b.width0,
176 alignment, rbuffer->b.b.b.bind,
177 rbuffer->b.b.b.usage);
178
179 if (bo == NULL) {
180 FREE(rbuffer);
181 return NULL;
182 }
183 rbuffer->bo = bo;
184 return &rbuffer->b.b.b;
185 }
186
187 struct pipe_resource *r600_user_buffer_create(struct pipe_screen *screen,
188 void *ptr, unsigned bytes,
189 unsigned bind)
190 {
191 struct r600_screen *rscreen = (struct r600_screen*)screen;
192 struct r600_resource *rbuffer;
193
194 rbuffer = util_slab_alloc(&rscreen->pool_buffers);
195
196 pipe_reference_init(&rbuffer->b.b.b.reference, 1);
197 rbuffer->b.b.vtbl = &r600_buffer_vtbl;
198 rbuffer->b.b.b.screen = screen;
199 rbuffer->b.b.b.target = PIPE_BUFFER;
200 rbuffer->b.b.b.format = PIPE_FORMAT_R8_UNORM;
201 rbuffer->b.b.b.usage = PIPE_USAGE_IMMUTABLE;
202 rbuffer->b.b.b.bind = bind;
203 rbuffer->b.b.b.width0 = bytes;
204 rbuffer->b.b.b.height0 = 1;
205 rbuffer->b.b.b.depth0 = 1;
206 rbuffer->b.b.b.array_size = 1;
207 rbuffer->b.b.b.flags = 0;
208 rbuffer->b.user_ptr = ptr;
209 rbuffer->bo = NULL;
210 rbuffer->bo_size = 0;
211 return &rbuffer->b.b.b;
212 }
213
214 void r600_upload_index_buffer(struct r600_pipe_context *rctx, struct r600_drawl *draw)
215 {
216 struct r600_resource *rbuffer = r600_resource(draw->index_buffer);
217 boolean flushed;
218
219 u_upload_data(rctx->vbuf_mgr->uploader, 0,
220 draw->info.count * draw->index_size,
221 rbuffer->b.user_ptr,
222 &draw->index_buffer_offset,
223 &draw->index_buffer, &flushed);
224 }
225
226 void r600_upload_const_buffer(struct r600_pipe_context *rctx, struct r600_resource **rbuffer,
227 uint32_t *const_offset)
228 {
229 if ((*rbuffer)->b.user_ptr) {
230 uint8_t *ptr = (*rbuffer)->b.user_ptr;
231 unsigned size = (*rbuffer)->b.b.b.width0;
232 boolean flushed;
233
234 *rbuffer = NULL;
235
236 if (R600_BIG_ENDIAN) {
237 uint32_t *tmpPtr;
238 unsigned i;
239
240 if (!(tmpPtr = malloc(size))) {
241 R600_ERR("Failed to allocate BE swap buffer.\n");
242 return;
243 }
244
245 for (i = 0; i < size / 4; ++i) {
246 tmpPtr[i] = bswap_32(((uint32_t *)ptr)[i]);
247 }
248
249 u_upload_data(rctx->vbuf_mgr->uploader, 0, size, tmpPtr, const_offset,
250 (struct pipe_resource**)rbuffer, &flushed);
251
252 free(tmpPtr);
253 } else {
254 u_upload_data(rctx->vbuf_mgr->uploader, 0, size, ptr, const_offset,
255 (struct pipe_resource**)rbuffer, &flushed);
256 }
257 } else {
258 *const_offset = 0;
259 }
260 }