radeonsi: implement accelerated buffer copying
[mesa.git] / src / gallium / drivers / radeonsi / 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
28 #include "pipe/p_screen.h"
29 #include "util/u_format.h"
30 #include "util/u_math.h"
31 #include "util/u_inlines.h"
32 #include "util/u_memory.h"
33 #include "util/u_upload_mgr.h"
34
35 #include "r600.h"
36 #include "radeonsi_pipe.h"
37
38 static void r600_buffer_destroy(struct pipe_screen *screen,
39 struct pipe_resource *buf)
40 {
41 struct r600_resource *rbuffer = r600_resource(buf);
42
43 pb_reference(&rbuffer->buf, NULL);
44 FREE(rbuffer);
45 }
46
47 static void *r600_buffer_transfer_map(struct pipe_context *ctx,
48 struct pipe_resource *resource,
49 unsigned level,
50 unsigned usage,
51 const struct pipe_box *box,
52 struct pipe_transfer **ptransfer)
53 {
54 struct r600_context *rctx = (struct r600_context*)ctx;
55 struct pipe_transfer *transfer;
56 struct r600_resource *rbuffer = r600_resource(resource);
57 uint8_t *data;
58
59 data = rctx->b.ws->buffer_map(rbuffer->cs_buf, rctx->b.rings.gfx.cs, usage);
60 if (!data) {
61 return NULL;
62 }
63
64 transfer = util_slab_alloc(&rctx->pool_transfers);
65 transfer->resource = resource;
66 transfer->level = level;
67 transfer->usage = usage;
68 transfer->box = *box;
69 transfer->stride = 0;
70 transfer->layer_stride = 0;
71 *ptransfer = transfer;
72
73 return (uint8_t*)data + transfer->box.x;
74 }
75
76 static void r600_buffer_transfer_unmap(struct pipe_context *ctx,
77 struct pipe_transfer *transfer)
78 {
79 struct r600_context *rctx = (struct r600_context*)ctx;
80 util_slab_free(&rctx->pool_transfers, transfer);
81 }
82
83 static void r600_buffer_transfer_flush_region(struct pipe_context *pipe,
84 struct pipe_transfer *transfer,
85 const struct pipe_box *box)
86 {
87 }
88
89 static const struct u_resource_vtbl r600_buffer_vtbl =
90 {
91 u_default_resource_get_handle, /* get_handle */
92 r600_buffer_destroy, /* resource_destroy */
93 r600_buffer_transfer_map, /* transfer_map */
94 r600_buffer_transfer_flush_region, /* transfer_flush_region */
95 r600_buffer_transfer_unmap, /* transfer_unmap */
96 NULL /* transfer_inline_write */
97 };
98
99 struct pipe_resource *si_buffer_create(struct pipe_screen *screen,
100 const struct pipe_resource *templ)
101 {
102 struct r600_screen *rscreen = (struct r600_screen*)screen;
103 struct r600_resource *rbuffer;
104 /* XXX We probably want a different alignment for buffers and textures. */
105 unsigned alignment = 4096;
106
107 rbuffer = MALLOC_STRUCT(r600_resource);
108
109 rbuffer->b.b = *templ;
110 pipe_reference_init(&rbuffer->b.b.reference, 1);
111 rbuffer->b.b.screen = screen;
112 rbuffer->b.vtbl = &r600_buffer_vtbl;
113 util_range_init(&rbuffer->valid_buffer_range);
114
115 if (!r600_init_resource(&rscreen->b, rbuffer, templ->width0, alignment, TRUE, templ->usage)) {
116 FREE(rbuffer);
117 return NULL;
118 }
119 return &rbuffer->b.b;
120 }
121
122 void r600_upload_index_buffer(struct r600_context *rctx,
123 struct pipe_index_buffer *ib, unsigned count)
124 {
125 u_upload_data(rctx->uploader, 0, count * ib->index_size,
126 ib->user_buffer, &ib->offset, &ib->buffer);
127 }
128
129 void r600_upload_const_buffer(struct r600_context *rctx, struct r600_resource **rbuffer,
130 const uint8_t *ptr, unsigned size,
131 uint32_t *const_offset)
132 {
133 if (R600_BIG_ENDIAN) {
134 uint32_t *tmpPtr;
135 unsigned i;
136
137 if (!(tmpPtr = malloc(size))) {
138 R600_ERR("Failed to allocate BE swap buffer.\n");
139 return;
140 }
141
142 for (i = 0; i < size / 4; ++i) {
143 tmpPtr[i] = util_bswap32(((uint32_t *)ptr)[i]);
144 }
145
146 u_upload_data(rctx->uploader, 0, size, tmpPtr, const_offset,
147 (struct pipe_resource**)rbuffer);
148
149 free(tmpPtr);
150 } else {
151 u_upload_data(rctx->uploader, 0, size, ptr, const_offset,
152 (struct pipe_resource**)rbuffer);
153 }
154 }