gallium: unify transfer functions
[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 #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 "radeonsi_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 si_resource *rbuffer = si_resource(buf);
44
45 pb_reference(&rbuffer->buf, NULL);
46 FREE(rbuffer);
47 }
48
49 static void *r600_buffer_transfer_map(struct pipe_context *ctx,
50 struct pipe_resource *resource,
51 unsigned level,
52 unsigned usage,
53 const struct pipe_box *box,
54 struct pipe_transfer **ptransfer)
55 {
56 struct r600_context *rctx = (struct r600_context*)ctx;
57 struct pipe_transfer *transfer;
58 struct si_resource *rbuffer = si_resource(resource);
59 uint8_t *data;
60
61 data = rctx->ws->buffer_map(rbuffer->cs_buf, rctx->cs, usage);
62 if (!data) {
63 return NULL;
64 }
65
66 transfer = util_slab_alloc(&rctx->pool_transfers);
67 transfer->resource = resource;
68 transfer->level = level;
69 transfer->usage = usage;
70 transfer->box = *box;
71 transfer->stride = 0;
72 transfer->layer_stride = 0;
73 transfer->data = NULL;
74 *ptransfer = transfer;
75
76 return (uint8_t*)data + transfer->box.x;
77 }
78
79 static void r600_buffer_transfer_unmap(struct pipe_context *ctx,
80 struct pipe_transfer *transfer)
81 {
82 struct r600_context *rctx = (struct r600_context*)ctx;
83 util_slab_free(&rctx->pool_transfers, transfer);
84 }
85
86 static void r600_buffer_transfer_flush_region(struct pipe_context *pipe,
87 struct pipe_transfer *transfer,
88 const struct pipe_box *box)
89 {
90 }
91
92 static const struct u_resource_vtbl r600_buffer_vtbl =
93 {
94 u_default_resource_get_handle, /* get_handle */
95 r600_buffer_destroy, /* resource_destroy */
96 r600_buffer_transfer_map, /* transfer_map */
97 r600_buffer_transfer_flush_region, /* transfer_flush_region */
98 r600_buffer_transfer_unmap, /* transfer_unmap */
99 NULL /* transfer_inline_write */
100 };
101
102 bool si_init_resource(struct r600_screen *rscreen,
103 struct si_resource *res,
104 unsigned size, unsigned alignment,
105 unsigned bind, unsigned usage)
106 {
107 uint32_t initial_domain, domains;
108
109 /* Staging resources particpate in transfers and blits only
110 * and are used for uploads and downloads from regular
111 * resources. We generate them internally for some transfers.
112 */
113 if (usage == PIPE_USAGE_STAGING) {
114 domains = RADEON_DOMAIN_GTT;
115 initial_domain = RADEON_DOMAIN_GTT;
116 } else {
117 domains = RADEON_DOMAIN_GTT | RADEON_DOMAIN_VRAM;
118
119 switch(usage) {
120 case PIPE_USAGE_DYNAMIC:
121 case PIPE_USAGE_STREAM:
122 case PIPE_USAGE_STAGING:
123 initial_domain = RADEON_DOMAIN_GTT;
124 break;
125 case PIPE_USAGE_DEFAULT:
126 case PIPE_USAGE_STATIC:
127 case PIPE_USAGE_IMMUTABLE:
128 default:
129 initial_domain = RADEON_DOMAIN_VRAM;
130 break;
131 }
132 }
133
134 res->buf = rscreen->ws->buffer_create(rscreen->ws, size, alignment, bind, initial_domain);
135 if (!res->buf) {
136 return false;
137 }
138
139 res->cs_buf = rscreen->ws->buffer_get_cs_handle(res->buf);
140 res->domains = domains;
141 return true;
142 }
143
144 struct pipe_resource *si_buffer_create(struct pipe_screen *screen,
145 const struct pipe_resource *templ)
146 {
147 struct r600_screen *rscreen = (struct r600_screen*)screen;
148 struct si_resource *rbuffer;
149 /* XXX We probably want a different alignment for buffers and textures. */
150 unsigned alignment = 4096;
151
152 rbuffer = MALLOC_STRUCT(si_resource);
153
154 rbuffer->b.b = *templ;
155 pipe_reference_init(&rbuffer->b.b.reference, 1);
156 rbuffer->b.b.screen = screen;
157 rbuffer->b.vtbl = &r600_buffer_vtbl;
158
159 if (!si_init_resource(rscreen, rbuffer, templ->width0, alignment, templ->bind, templ->usage)) {
160 FREE(rbuffer);
161 return NULL;
162 }
163 return &rbuffer->b.b;
164 }
165
166 void r600_upload_index_buffer(struct r600_context *rctx,
167 struct pipe_index_buffer *ib, unsigned count)
168 {
169 u_upload_data(rctx->uploader, 0, count * ib->index_size,
170 ib->user_buffer, &ib->offset, &ib->buffer);
171 }
172
173 void r600_upload_const_buffer(struct r600_context *rctx, struct si_resource **rbuffer,
174 const uint8_t *ptr, unsigned size,
175 uint32_t *const_offset)
176 {
177 *rbuffer = NULL;
178
179 if (R600_BIG_ENDIAN) {
180 uint32_t *tmpPtr;
181 unsigned i;
182
183 if (!(tmpPtr = malloc(size))) {
184 R600_ERR("Failed to allocate BE swap buffer.\n");
185 return;
186 }
187
188 for (i = 0; i < size / 4; ++i) {
189 tmpPtr[i] = bswap_32(((uint32_t *)ptr)[i]);
190 }
191
192 u_upload_data(rctx->uploader, 0, size, tmpPtr, const_offset,
193 (struct pipe_resource**)rbuffer);
194
195 free(tmpPtr);
196 } else {
197 u_upload_data(rctx->uploader, 0, size, ptr, const_offset,
198 (struct pipe_resource**)rbuffer);
199 }
200 }