2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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.
25 * Corbin Simpson <MostAwesomeDude@gmail.com>
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"
37 #include "radeon_drm.h"
40 #include "r600_pipe.h"
42 static void r600_buffer_destroy(struct pipe_screen
*screen
,
43 struct pipe_resource
*buf
)
45 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
46 struct r600_resource_buffer
*rbuffer
= r600_buffer(buf
);
49 r600_bo_reference(&rbuffer
->r
.bo
, NULL
);
52 util_slab_free(&rscreen
->pool_buffers
, rbuffer
);
55 static struct pipe_transfer
*r600_get_transfer(struct pipe_context
*ctx
,
56 struct pipe_resource
*resource
,
59 const struct pipe_box
*box
)
61 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
62 struct pipe_transfer
*transfer
= util_slab_alloc(&rctx
->pool_transfers
);
64 transfer
->resource
= resource
;
65 transfer
->level
= level
;
66 transfer
->usage
= usage
;
69 transfer
->layer_stride
= 0;
70 transfer
->data
= NULL
;
72 /* Note strides are zero, this is ok for buffers, but not for
73 * textures 2d & higher at least.
78 static void *r600_buffer_transfer_map(struct pipe_context
*pipe
,
79 struct pipe_transfer
*transfer
)
81 struct r600_resource_buffer
*rbuffer
= r600_buffer(transfer
->resource
);
82 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)pipe
;
85 if (rbuffer
->r
.b
.user_ptr
)
86 return (uint8_t*)rbuffer
->r
.b
.user_ptr
+ transfer
->box
.x
;
88 data
= r600_bo_map(rctx
->screen
->radeon
, rbuffer
->r
.bo
, rctx
->ctx
.cs
, transfer
->usage
);
92 return (uint8_t*)data
+ transfer
->box
.x
;
95 static void r600_buffer_transfer_unmap(struct pipe_context
*pipe
,
96 struct pipe_transfer
*transfer
)
98 struct r600_resource_buffer
*rbuffer
= r600_buffer(transfer
->resource
);
99 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)pipe
;
101 if (rbuffer
->r
.b
.user_ptr
)
105 r600_bo_unmap(rctx
->screen
->radeon
, rbuffer
->r
.bo
);
108 static void r600_buffer_transfer_flush_region(struct pipe_context
*pipe
,
109 struct pipe_transfer
*transfer
,
110 const struct pipe_box
*box
)
114 static void r600_transfer_destroy(struct pipe_context
*ctx
,
115 struct pipe_transfer
*transfer
)
117 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
118 util_slab_free(&rctx
->pool_transfers
, transfer
);
121 static void r600_buffer_transfer_inline_write(struct pipe_context
*pipe
,
122 struct pipe_resource
*resource
,
125 const struct pipe_box
*box
,
128 unsigned layer_stride
)
130 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)pipe
;
131 struct radeon
*radeon
= rctx
->screen
->radeon
;
132 struct r600_resource_buffer
*rbuffer
= r600_buffer(resource
);
135 assert(rbuffer
->r
.b
.user_ptr
== NULL
);
137 map
= r600_bo_map(radeon
, rbuffer
->r
.bo
, rctx
->ctx
.cs
,
138 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
| usage
);
140 memcpy(map
+ box
->x
, data
, box
->width
);
143 r600_bo_unmap(radeon
, rbuffer
->r
.bo
);
146 static const struct u_resource_vtbl r600_buffer_vtbl
=
148 u_default_resource_get_handle
, /* get_handle */
149 r600_buffer_destroy
, /* resource_destroy */
150 r600_get_transfer
, /* get_transfer */
151 r600_transfer_destroy
, /* transfer_destroy */
152 r600_buffer_transfer_map
, /* transfer_map */
153 r600_buffer_transfer_flush_region
, /* transfer_flush_region */
154 r600_buffer_transfer_unmap
, /* transfer_unmap */
155 r600_buffer_transfer_inline_write
/* transfer_inline_write */
158 struct pipe_resource
*r600_buffer_create(struct pipe_screen
*screen
,
159 const struct pipe_resource
*templ
)
161 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
162 struct r600_resource_buffer
*rbuffer
;
164 /* XXX We probably want a different alignment for buffers and textures. */
165 unsigned alignment
= 4096;
167 rbuffer
= util_slab_alloc(&rscreen
->pool_buffers
);
169 rbuffer
->magic
= R600_BUFFER_MAGIC
;
170 rbuffer
->r
.b
.b
.b
= *templ
;
171 pipe_reference_init(&rbuffer
->r
.b
.b
.b
.reference
, 1);
172 rbuffer
->r
.b
.b
.b
.screen
= screen
;
173 rbuffer
->r
.b
.b
.vtbl
= &r600_buffer_vtbl
;
174 rbuffer
->r
.b
.user_ptr
= NULL
;
175 rbuffer
->r
.size
= rbuffer
->r
.b
.b
.b
.width0
;
176 rbuffer
->r
.bo_size
= rbuffer
->r
.size
;
178 bo
= r600_bo(rscreen
->radeon
,
179 rbuffer
->r
.b
.b
.b
.width0
,
180 alignment
, rbuffer
->r
.b
.b
.b
.bind
,
181 rbuffer
->r
.b
.b
.b
.usage
);
188 return &rbuffer
->r
.b
.b
.b
;
191 struct pipe_resource
*r600_user_buffer_create(struct pipe_screen
*screen
,
192 void *ptr
, unsigned bytes
,
195 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
196 struct r600_resource_buffer
*rbuffer
;
198 rbuffer
= util_slab_alloc(&rscreen
->pool_buffers
);
200 rbuffer
->magic
= R600_BUFFER_MAGIC
;
201 pipe_reference_init(&rbuffer
->r
.b
.b
.b
.reference
, 1);
202 rbuffer
->r
.b
.b
.vtbl
= &r600_buffer_vtbl
;
203 rbuffer
->r
.b
.b
.b
.screen
= screen
;
204 rbuffer
->r
.b
.b
.b
.target
= PIPE_BUFFER
;
205 rbuffer
->r
.b
.b
.b
.format
= PIPE_FORMAT_R8_UNORM
;
206 rbuffer
->r
.b
.b
.b
.usage
= PIPE_USAGE_IMMUTABLE
;
207 rbuffer
->r
.b
.b
.b
.bind
= bind
;
208 rbuffer
->r
.b
.b
.b
.width0
= bytes
;
209 rbuffer
->r
.b
.b
.b
.height0
= 1;
210 rbuffer
->r
.b
.b
.b
.depth0
= 1;
211 rbuffer
->r
.b
.b
.b
.array_size
= 1;
212 rbuffer
->r
.b
.b
.b
.flags
= 0;
213 rbuffer
->r
.b
.user_ptr
= ptr
;
214 rbuffer
->r
.bo
= NULL
;
215 rbuffer
->r
.bo_size
= 0;
216 return &rbuffer
->r
.b
.b
.b
;
219 struct pipe_resource
*r600_buffer_from_handle(struct pipe_screen
*screen
,
220 struct winsys_handle
*whandle
)
222 struct radeon
*rw
= ((struct r600_screen
*)screen
)->radeon
;
223 struct r600_resource
*rbuffer
;
224 struct r600_bo
*bo
= NULL
;
226 bo
= r600_bo_handle(rw
, whandle
, NULL
, NULL
);
231 rbuffer
= CALLOC_STRUCT(r600_resource
);
232 if (rbuffer
== NULL
) {
233 r600_bo_reference(&bo
, NULL
);
237 pipe_reference_init(&rbuffer
->b
.b
.b
.reference
, 1);
238 rbuffer
->b
.b
.b
.target
= PIPE_BUFFER
;
239 rbuffer
->b
.b
.b
.screen
= screen
;
240 rbuffer
->b
.b
.vtbl
= &r600_buffer_vtbl
;
242 return &rbuffer
->b
.b
.b
;
245 void r600_upload_index_buffer(struct r600_pipe_context
*rctx
, struct r600_drawl
*draw
)
247 struct r600_resource_buffer
*rbuffer
= r600_buffer(draw
->index_buffer
);
250 u_upload_data(rctx
->vbuf_mgr
->uploader
, 0,
251 draw
->info
.count
* draw
->index_size
,
252 rbuffer
->r
.b
.user_ptr
,
253 &draw
->index_buffer_offset
,
254 &draw
->index_buffer
, &flushed
);
257 void r600_upload_const_buffer(struct r600_pipe_context
*rctx
, struct r600_resource_buffer
**rbuffer
,
258 uint32_t *const_offset
)
260 if ((*rbuffer
)->r
.b
.user_ptr
) {
261 uint8_t *ptr
= (*rbuffer
)->r
.b
.user_ptr
;
262 unsigned size
= (*rbuffer
)->r
.b
.b
.b
.width0
;
267 if (R600_BIG_ENDIAN
) {
271 if (!(tmpPtr
= malloc(size
))) {
272 R600_ERR("Failed to allocate BE swap buffer.\n");
276 for (i
= 0; i
< size
/ 4; ++i
) {
277 tmpPtr
[i
] = bswap_32(((uint32_t *)ptr
)[i
]);
280 u_upload_data(rctx
->vbuf_mgr
->uploader
, 0, size
, tmpPtr
, const_offset
,
281 (struct pipe_resource
**)rbuffer
, &flushed
);
285 u_upload_data(rctx
->vbuf_mgr
->uploader
, 0, size
, ptr
, const_offset
,
286 (struct pipe_resource
**)rbuffer
, &flushed
);