r300g: do not create a user buffer struct for misaligned ushort indices fallback
[mesa.git] / src / gallium / drivers / r300 / r300_screen_buffer.c
1 /*
2 * Copyright 2010 Red Hat Inc.
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: Dave Airlie
24 */
25
26 #include <stdio.h>
27
28 #include "util/u_inlines.h"
29 #include "util/u_memory.h"
30 #include "util/u_upload_mgr.h"
31 #include "util/u_math.h"
32
33 #include "r300_screen_buffer.h"
34 #include "r300_winsys.h"
35
36 unsigned r300_buffer_is_referenced(struct pipe_context *context,
37 struct pipe_resource *buf)
38 {
39 struct r300_context *r300 = r300_context(context);
40 struct r300_resource *rbuf = r300_resource(buf);
41
42 if (rbuf->b.user_ptr || rbuf->constant_buffer)
43 return PIPE_UNREFERENCED;
44
45 if (r300->rws->cs_is_buffer_referenced(r300->cs, rbuf->cs_buf))
46 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
47
48 return PIPE_UNREFERENCED;
49 }
50
51 void r300_upload_index_buffer(struct r300_context *r300,
52 struct pipe_resource **index_buffer,
53 unsigned index_size, unsigned *start,
54 unsigned count, uint8_t *ptr)
55 {
56 unsigned index_offset;
57 boolean flushed;
58
59 *index_buffer = NULL;
60
61 u_upload_data(r300->vbuf_mgr->uploader,
62 0, count * index_size,
63 ptr + (*start * index_size),
64 &index_offset,
65 index_buffer, &flushed);
66
67 *start = index_offset / index_size;
68
69 if (flushed || !r300->upload_ib_validated) {
70 r300->upload_ib_validated = FALSE;
71 r300->validate_buffers = TRUE;
72 }
73 }
74
75 static void r300_buffer_destroy(struct pipe_screen *screen,
76 struct pipe_resource *buf)
77 {
78 struct r300_screen *r300screen = r300_screen(screen);
79 struct r300_resource *rbuf = r300_resource(buf);
80
81 if (rbuf->constant_buffer)
82 FREE(rbuf->constant_buffer);
83
84 if (rbuf->buf)
85 r300_winsys_bo_reference(&rbuf->buf, NULL);
86
87 util_slab_free(&r300screen->pool_buffers, rbuf);
88 }
89
90 static struct pipe_transfer*
91 r300_buffer_get_transfer(struct pipe_context *context,
92 struct pipe_resource *resource,
93 unsigned level,
94 unsigned usage,
95 const struct pipe_box *box)
96 {
97 struct r300_context *r300 = r300_context(context);
98 struct pipe_transfer *transfer =
99 util_slab_alloc(&r300->pool_transfers);
100
101 transfer->resource = resource;
102 transfer->level = level;
103 transfer->usage = usage;
104 transfer->box = *box;
105 transfer->stride = 0;
106 transfer->layer_stride = 0;
107 transfer->data = NULL;
108
109 /* Note strides are zero, this is ok for buffers, but not for
110 * textures 2d & higher at least.
111 */
112 return transfer;
113 }
114
115 static void r300_buffer_transfer_destroy(struct pipe_context *pipe,
116 struct pipe_transfer *transfer)
117 {
118 struct r300_context *r300 = r300_context(pipe);
119 util_slab_free(&r300->pool_transfers, transfer);
120 }
121
122 static void *
123 r300_buffer_transfer_map( struct pipe_context *pipe,
124 struct pipe_transfer *transfer )
125 {
126 struct r300_context *r300 = r300_context(pipe);
127 struct r300_screen *r300screen = r300_screen(pipe->screen);
128 struct r300_winsys_screen *rws = r300screen->rws;
129 struct r300_resource *rbuf = r300_resource(transfer->resource);
130 uint8_t *map;
131
132 if (rbuf->b.user_ptr)
133 return (uint8_t *) rbuf->b.user_ptr + transfer->box.x;
134 if (rbuf->constant_buffer)
135 return (uint8_t *) rbuf->constant_buffer + transfer->box.x;
136
137 map = rws->buffer_map(rbuf->buf, r300->cs, transfer->usage);
138
139 if (map == NULL)
140 return NULL;
141
142 return map + transfer->box.x;
143 }
144
145 static void r300_buffer_transfer_unmap( struct pipe_context *pipe,
146 struct pipe_transfer *transfer )
147 {
148 struct r300_screen *r300screen = r300_screen(pipe->screen);
149 struct r300_winsys_screen *rws = r300screen->rws;
150 struct r300_resource *rbuf = r300_resource(transfer->resource);
151
152 if (rbuf->buf) {
153 rws->buffer_unmap(rbuf->buf);
154 }
155 }
156
157 static void r300_buffer_transfer_inline_write(struct pipe_context *pipe,
158 struct pipe_resource *resource,
159 unsigned level,
160 unsigned usage,
161 const struct pipe_box *box,
162 const void *data,
163 unsigned stride,
164 unsigned layer_stride)
165 {
166 struct r300_context *r300 = r300_context(pipe);
167 struct r300_winsys_screen *rws = r300->screen->rws;
168 struct r300_resource *rbuf = r300_resource(resource);
169 uint8_t *map = NULL;
170
171 if (rbuf->constant_buffer) {
172 memcpy(rbuf->constant_buffer + box->x, data, box->width);
173 return;
174 }
175 assert(rbuf->b.user_ptr == NULL);
176
177 map = rws->buffer_map(rbuf->buf, r300->cs,
178 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD | usage);
179
180 memcpy(map + box->x, data, box->width);
181
182 rws->buffer_unmap(rbuf->buf);
183 }
184
185 static const struct u_resource_vtbl r300_buffer_vtbl =
186 {
187 NULL, /* get_handle */
188 r300_buffer_destroy, /* resource_destroy */
189 NULL, /* is_buffer_referenced */
190 r300_buffer_get_transfer, /* get_transfer */
191 r300_buffer_transfer_destroy, /* transfer_destroy */
192 r300_buffer_transfer_map, /* transfer_map */
193 NULL, /* transfer_flush_region */
194 r300_buffer_transfer_unmap, /* transfer_unmap */
195 r300_buffer_transfer_inline_write /* transfer_inline_write */
196 };
197
198 struct pipe_resource *r300_buffer_create(struct pipe_screen *screen,
199 const struct pipe_resource *templ)
200 {
201 struct r300_screen *r300screen = r300_screen(screen);
202 struct r300_resource *rbuf;
203 unsigned alignment = 16;
204
205 rbuf = util_slab_alloc(&r300screen->pool_buffers);
206
207 rbuf->b.b.b = *templ;
208 rbuf->b.b.vtbl = &r300_buffer_vtbl;
209 pipe_reference_init(&rbuf->b.b.b.reference, 1);
210 rbuf->b.b.b.screen = screen;
211 rbuf->b.user_ptr = NULL;
212 rbuf->domain = R300_DOMAIN_GTT;
213 rbuf->buf = NULL;
214 rbuf->buf_size = templ->width0;
215 rbuf->constant_buffer = NULL;
216
217 /* Alloc constant buffers in RAM. */
218 if (templ->bind & PIPE_BIND_CONSTANT_BUFFER) {
219 rbuf->constant_buffer = MALLOC(templ->width0);
220 return &rbuf->b.b.b;
221 }
222
223 rbuf->buf =
224 r300screen->rws->buffer_create(r300screen->rws,
225 rbuf->b.b.b.width0, alignment,
226 rbuf->b.b.b.bind, rbuf->b.b.b.usage,
227 rbuf->domain);
228 if (!rbuf->buf) {
229 util_slab_free(&r300screen->pool_buffers, rbuf);
230 return NULL;
231 }
232
233 rbuf->cs_buf =
234 r300screen->rws->buffer_get_cs_handle(rbuf->buf);
235
236 return &rbuf->b.b.b;
237 }
238
239 struct pipe_resource *r300_user_buffer_create(struct pipe_screen *screen,
240 void *ptr, unsigned size,
241 unsigned bind)
242 {
243 struct r300_screen *r300screen = r300_screen(screen);
244 struct r300_resource *rbuf;
245
246 rbuf = util_slab_alloc(&r300screen->pool_buffers);
247
248 pipe_reference_init(&rbuf->b.b.b.reference, 1);
249 rbuf->b.b.b.screen = screen;
250 rbuf->b.b.b.target = PIPE_BUFFER;
251 rbuf->b.b.b.format = PIPE_FORMAT_R8_UNORM;
252 rbuf->b.b.b.usage = PIPE_USAGE_IMMUTABLE;
253 rbuf->b.b.b.bind = bind;
254 rbuf->b.b.b.width0 = ~0;
255 rbuf->b.b.b.height0 = 1;
256 rbuf->b.b.b.depth0 = 1;
257 rbuf->b.b.b.array_size = 1;
258 rbuf->b.b.b.flags = 0;
259 rbuf->b.b.vtbl = &r300_buffer_vtbl;
260 rbuf->b.user_ptr = ptr;
261 rbuf->domain = R300_DOMAIN_GTT;
262 rbuf->buf = NULL;
263 rbuf->buf_size = size;
264 rbuf->constant_buffer = NULL;
265 return &rbuf->b.b.b;
266 }