2 * Copyright 2010 Red Hat Inc.
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.
23 * Authors: Dave Airlie
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"
33 #include "r300_screen_buffer.h"
35 void r300_upload_index_buffer(struct r300_context
*r300
,
36 struct pipe_resource
**index_buffer
,
37 unsigned index_size
, unsigned *start
,
38 unsigned count
, uint8_t *ptr
)
40 unsigned index_offset
;
45 u_upload_data(r300
->vbuf_mgr
->uploader
,
46 0, count
* index_size
,
47 ptr
+ (*start
* index_size
),
49 index_buffer
, &flushed
);
51 *start
= index_offset
/ index_size
;
54 static void r300_buffer_destroy(struct pipe_screen
*screen
,
55 struct pipe_resource
*buf
)
57 struct r300_screen
*r300screen
= r300_screen(screen
);
58 struct r300_resource
*rbuf
= r300_resource(buf
);
60 if (rbuf
->constant_buffer
)
61 FREE(rbuf
->constant_buffer
);
64 pb_reference(&rbuf
->buf
, NULL
);
66 util_slab_free(&r300screen
->pool_buffers
, rbuf
);
69 static struct pipe_transfer
*
70 r300_buffer_get_transfer(struct pipe_context
*context
,
71 struct pipe_resource
*resource
,
74 const struct pipe_box
*box
)
76 struct r300_context
*r300
= r300_context(context
);
77 struct pipe_transfer
*transfer
=
78 util_slab_alloc(&r300
->pool_transfers
);
80 transfer
->resource
= resource
;
81 transfer
->level
= level
;
82 transfer
->usage
= usage
;
85 transfer
->layer_stride
= 0;
86 transfer
->data
= NULL
;
88 /* Note strides are zero, this is ok for buffers, but not for
89 * textures 2d & higher at least.
94 static void r300_buffer_transfer_destroy(struct pipe_context
*pipe
,
95 struct pipe_transfer
*transfer
)
97 struct r300_context
*r300
= r300_context(pipe
);
98 util_slab_free(&r300
->pool_transfers
, transfer
);
102 r300_buffer_transfer_map( struct pipe_context
*pipe
,
103 struct pipe_transfer
*transfer
)
105 struct r300_context
*r300
= r300_context(pipe
);
106 struct r300_screen
*r300screen
= r300_screen(pipe
->screen
);
107 struct radeon_winsys
*rws
= r300screen
->rws
;
108 struct r300_resource
*rbuf
= r300_resource(transfer
->resource
);
111 if (rbuf
->b
.user_ptr
)
112 return (uint8_t *) rbuf
->b
.user_ptr
+ transfer
->box
.x
;
113 if (rbuf
->constant_buffer
)
114 return (uint8_t *) rbuf
->constant_buffer
+ transfer
->box
.x
;
116 map
= rws
->buffer_map(rbuf
->buf
, r300
->cs
, transfer
->usage
);
121 return map
+ transfer
->box
.x
;
124 static void r300_buffer_transfer_unmap( struct pipe_context
*pipe
,
125 struct pipe_transfer
*transfer
)
127 struct r300_screen
*r300screen
= r300_screen(pipe
->screen
);
128 struct radeon_winsys
*rws
= r300screen
->rws
;
129 struct r300_resource
*rbuf
= r300_resource(transfer
->resource
);
132 rws
->buffer_unmap(rbuf
->buf
);
136 static void r300_buffer_transfer_inline_write(struct pipe_context
*pipe
,
137 struct pipe_resource
*resource
,
140 const struct pipe_box
*box
,
143 unsigned layer_stride
)
145 struct r300_context
*r300
= r300_context(pipe
);
146 struct radeon_winsys
*rws
= r300
->screen
->rws
;
147 struct r300_resource
*rbuf
= r300_resource(resource
);
150 if (rbuf
->constant_buffer
) {
151 memcpy(rbuf
->constant_buffer
+ box
->x
, data
, box
->width
);
154 assert(rbuf
->b
.user_ptr
== NULL
);
156 map
= rws
->buffer_map(rbuf
->buf
, r300
->cs
,
157 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
| usage
);
159 memcpy(map
+ box
->x
, data
, box
->width
);
161 rws
->buffer_unmap(rbuf
->buf
);
164 static const struct u_resource_vtbl r300_buffer_vtbl
=
166 NULL
, /* get_handle */
167 r300_buffer_destroy
, /* resource_destroy */
168 r300_buffer_get_transfer
, /* get_transfer */
169 r300_buffer_transfer_destroy
, /* transfer_destroy */
170 r300_buffer_transfer_map
, /* transfer_map */
171 NULL
, /* transfer_flush_region */
172 r300_buffer_transfer_unmap
, /* transfer_unmap */
173 r300_buffer_transfer_inline_write
/* transfer_inline_write */
176 struct pipe_resource
*r300_buffer_create(struct pipe_screen
*screen
,
177 const struct pipe_resource
*templ
)
179 struct r300_screen
*r300screen
= r300_screen(screen
);
180 struct r300_resource
*rbuf
;
181 unsigned alignment
= 16;
183 rbuf
= util_slab_alloc(&r300screen
->pool_buffers
);
185 rbuf
->b
.b
.b
= *templ
;
186 rbuf
->b
.b
.vtbl
= &r300_buffer_vtbl
;
187 pipe_reference_init(&rbuf
->b
.b
.b
.reference
, 1);
188 rbuf
->b
.b
.b
.screen
= screen
;
189 rbuf
->b
.user_ptr
= NULL
;
190 rbuf
->domain
= RADEON_DOMAIN_GTT
;
192 rbuf
->buf_size
= templ
->width0
;
193 rbuf
->constant_buffer
= NULL
;
195 /* Alloc constant buffers in RAM. */
196 if (templ
->bind
& PIPE_BIND_CONSTANT_BUFFER
) {
197 rbuf
->constant_buffer
= MALLOC(templ
->width0
);
202 r300screen
->rws
->buffer_create(r300screen
->rws
,
203 rbuf
->b
.b
.b
.width0
, alignment
,
204 rbuf
->b
.b
.b
.bind
, rbuf
->b
.b
.b
.usage
,
207 util_slab_free(&r300screen
->pool_buffers
, rbuf
);
212 r300screen
->rws
->buffer_get_cs_handle(rbuf
->buf
);
217 struct pipe_resource
*r300_user_buffer_create(struct pipe_screen
*screen
,
218 void *ptr
, unsigned size
,
221 struct r300_screen
*r300screen
= r300_screen(screen
);
222 struct r300_resource
*rbuf
;
224 rbuf
= util_slab_alloc(&r300screen
->pool_buffers
);
226 pipe_reference_init(&rbuf
->b
.b
.b
.reference
, 1);
227 rbuf
->b
.b
.b
.screen
= screen
;
228 rbuf
->b
.b
.b
.target
= PIPE_BUFFER
;
229 rbuf
->b
.b
.b
.format
= PIPE_FORMAT_R8_UNORM
;
230 rbuf
->b
.b
.b
.usage
= PIPE_USAGE_IMMUTABLE
;
231 rbuf
->b
.b
.b
.bind
= bind
;
232 rbuf
->b
.b
.b
.width0
= ~0;
233 rbuf
->b
.b
.b
.height0
= 1;
234 rbuf
->b
.b
.b
.depth0
= 1;
235 rbuf
->b
.b
.b
.array_size
= 1;
236 rbuf
->b
.b
.b
.flags
= 0;
237 rbuf
->b
.b
.vtbl
= &r300_buffer_vtbl
;
238 rbuf
->b
.user_ptr
= ptr
;
239 rbuf
->domain
= RADEON_DOMAIN_GTT
;
241 rbuf
->buf_size
= size
;
242 rbuf
->constant_buffer
= NULL
;