radeonsi/compute: Pass kernel arguments in a buffer v2
[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 <byteswap.h>
29
30 #include "pipe/p_screen.h"
31 #include "util/u_format.h"
32 #include "util/u_math.h"
33 #include "util/u_inlines.h"
34 #include "util/u_memory.h"
35 #include "util/u_upload_mgr.h"
36
37 #include "r600.h"
38 #include "radeonsi_pipe.h"
39
40 static void r600_buffer_destroy(struct pipe_screen *screen,
41 struct pipe_resource *buf)
42 {
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 *ptransfer = transfer;
74
75 return (uint8_t*)data + transfer->box.x;
76 }
77
78 static void r600_buffer_transfer_unmap(struct pipe_context *ctx,
79 struct pipe_transfer *transfer)
80 {
81 struct r600_context *rctx = (struct r600_context*)ctx;
82 util_slab_free(&rctx->pool_transfers, transfer);
83 }
84
85 static void r600_buffer_transfer_flush_region(struct pipe_context *pipe,
86 struct pipe_transfer *transfer,
87 const struct pipe_box *box)
88 {
89 }
90
91 static const struct u_resource_vtbl r600_buffer_vtbl =
92 {
93 u_default_resource_get_handle, /* get_handle */
94 r600_buffer_destroy, /* resource_destroy */
95 r600_buffer_transfer_map, /* transfer_map */
96 r600_buffer_transfer_flush_region, /* transfer_flush_region */
97 r600_buffer_transfer_unmap, /* transfer_unmap */
98 NULL /* transfer_inline_write */
99 };
100
101 bool si_init_resource(struct r600_screen *rscreen,
102 struct si_resource *res,
103 unsigned size, unsigned alignment,
104 boolean use_reusable_pool, unsigned usage)
105 {
106 uint32_t initial_domain, domains;
107
108 /* Staging resources particpate in transfers and blits only
109 * and are used for uploads and downloads from regular
110 * resources. We generate them internally for some transfers.
111 */
112 if (usage == PIPE_USAGE_STAGING) {
113 domains = RADEON_DOMAIN_GTT;
114 initial_domain = RADEON_DOMAIN_GTT;
115 } else {
116 domains = RADEON_DOMAIN_GTT | RADEON_DOMAIN_VRAM;
117
118 switch(usage) {
119 case PIPE_USAGE_DYNAMIC:
120 case PIPE_USAGE_STREAM:
121 case PIPE_USAGE_STAGING:
122 initial_domain = RADEON_DOMAIN_GTT;
123 break;
124 case PIPE_USAGE_DEFAULT:
125 case PIPE_USAGE_STATIC:
126 case PIPE_USAGE_IMMUTABLE:
127 default:
128 initial_domain = RADEON_DOMAIN_VRAM;
129 break;
130 }
131 }
132
133 res->buf = rscreen->ws->buffer_create(rscreen->ws, size, alignment,
134 use_reusable_pool,
135 initial_domain);
136 if (!res->buf) {
137 return false;
138 }
139
140 res->cs_buf = rscreen->ws->buffer_get_cs_handle(res->buf);
141 res->domains = domains;
142 return true;
143 }
144
145 struct pipe_resource *si_buffer_create(struct pipe_screen *screen,
146 const struct pipe_resource *templ)
147 {
148 struct r600_screen *rscreen = (struct r600_screen*)screen;
149 struct si_resource *rbuffer;
150 /* XXX We probably want a different alignment for buffers and textures. */
151 unsigned alignment = 4096;
152
153 rbuffer = MALLOC_STRUCT(si_resource);
154
155 rbuffer->b.b = *templ;
156 pipe_reference_init(&rbuffer->b.b.reference, 1);
157 rbuffer->b.b.screen = screen;
158 rbuffer->b.vtbl = &r600_buffer_vtbl;
159
160 if (!si_init_resource(rscreen, rbuffer, templ->width0, alignment, TRUE, templ->usage)) {
161 FREE(rbuffer);
162 return NULL;
163 }
164 return &rbuffer->b.b;
165 }
166
167 void r600_upload_index_buffer(struct r600_context *rctx,
168 struct pipe_index_buffer *ib, unsigned count)
169 {
170 u_upload_data(rctx->uploader, 0, count * ib->index_size,
171 ib->user_buffer, &ib->offset, &ib->buffer);
172 }
173
174 void r600_upload_const_buffer(struct r600_context *rctx, struct si_resource **rbuffer,
175 const uint8_t *ptr, unsigned size,
176 uint32_t *const_offset)
177 {
178 if (R600_BIG_ENDIAN) {
179 uint32_t *tmpPtr;
180 unsigned i;
181
182 if (!(tmpPtr = malloc(size))) {
183 R600_ERR("Failed to allocate BE swap buffer.\n");
184 return;
185 }
186
187 for (i = 0; i < size / 4; ++i) {
188 tmpPtr[i] = bswap_32(((uint32_t *)ptr)[i]);
189 }
190
191 u_upload_data(rctx->uploader, 0, size, tmpPtr, const_offset,
192 (struct pipe_resource**)rbuffer);
193
194 free(tmpPtr);
195 } else {
196 u_upload_data(rctx->uploader, 0, size, ptr, const_offset,
197 (struct pipe_resource**)rbuffer);
198 }
199 }