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
);
110 enum pipe_transfer_usage usage
;
112 if (rbuf
->b
.user_ptr
)
113 return (uint8_t *) rbuf
->b
.user_ptr
+ transfer
->box
.x
;
114 if (rbuf
->constant_buffer
)
115 return (uint8_t *) rbuf
->constant_buffer
+ transfer
->box
.x
;
117 /* Buffers are never used for write, therefore mapping for read can be
119 usage
= transfer
->usage
;
120 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
121 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
124 map
= rws
->buffer_map(rbuf
->buf
, r300
->cs
, usage
);
129 return map
+ transfer
->box
.x
;
132 static void r300_buffer_transfer_unmap( struct pipe_context
*pipe
,
133 struct pipe_transfer
*transfer
)
135 struct r300_screen
*r300screen
= r300_screen(pipe
->screen
);
136 struct radeon_winsys
*rws
= r300screen
->rws
;
137 struct r300_resource
*rbuf
= r300_resource(transfer
->resource
);
140 rws
->buffer_unmap(rbuf
->buf
);
144 static void r300_buffer_transfer_inline_write(struct pipe_context
*pipe
,
145 struct pipe_resource
*resource
,
148 const struct pipe_box
*box
,
151 unsigned layer_stride
)
153 struct r300_context
*r300
= r300_context(pipe
);
154 struct radeon_winsys
*rws
= r300
->screen
->rws
;
155 struct r300_resource
*rbuf
= r300_resource(resource
);
158 if (rbuf
->constant_buffer
) {
159 memcpy(rbuf
->constant_buffer
+ box
->x
, data
, box
->width
);
162 assert(rbuf
->b
.user_ptr
== NULL
);
164 map
= rws
->buffer_map(rbuf
->buf
, r300
->cs
,
165 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
| usage
);
167 memcpy(map
+ box
->x
, data
, box
->width
);
169 rws
->buffer_unmap(rbuf
->buf
);
172 static const struct u_resource_vtbl r300_buffer_vtbl
=
174 NULL
, /* get_handle */
175 r300_buffer_destroy
, /* resource_destroy */
176 r300_buffer_get_transfer
, /* get_transfer */
177 r300_buffer_transfer_destroy
, /* transfer_destroy */
178 r300_buffer_transfer_map
, /* transfer_map */
179 NULL
, /* transfer_flush_region */
180 r300_buffer_transfer_unmap
, /* transfer_unmap */
181 r300_buffer_transfer_inline_write
/* transfer_inline_write */
184 struct pipe_resource
*r300_buffer_create(struct pipe_screen
*screen
,
185 const struct pipe_resource
*templ
)
187 struct r300_screen
*r300screen
= r300_screen(screen
);
188 struct r300_resource
*rbuf
;
189 unsigned alignment
= 16;
191 rbuf
= util_slab_alloc(&r300screen
->pool_buffers
);
193 rbuf
->b
.b
.b
= *templ
;
194 rbuf
->b
.b
.vtbl
= &r300_buffer_vtbl
;
195 pipe_reference_init(&rbuf
->b
.b
.b
.reference
, 1);
196 rbuf
->b
.b
.b
.screen
= screen
;
197 rbuf
->b
.user_ptr
= NULL
;
198 rbuf
->domain
= RADEON_DOMAIN_GTT
;
200 rbuf
->constant_buffer
= NULL
;
202 /* Alloc constant buffers in RAM. */
203 if (templ
->bind
& PIPE_BIND_CONSTANT_BUFFER
) {
204 rbuf
->constant_buffer
= MALLOC(templ
->width0
);
209 r300screen
->rws
->buffer_create(r300screen
->rws
,
210 rbuf
->b
.b
.b
.width0
, alignment
,
211 rbuf
->b
.b
.b
.bind
, rbuf
->domain
);
213 util_slab_free(&r300screen
->pool_buffers
, rbuf
);
218 r300screen
->rws
->buffer_get_cs_handle(rbuf
->buf
);
223 struct pipe_resource
*r300_user_buffer_create(struct pipe_screen
*screen
,
224 void *ptr
, unsigned size
,
227 struct r300_screen
*r300screen
= r300_screen(screen
);
228 struct r300_resource
*rbuf
;
230 rbuf
= util_slab_alloc(&r300screen
->pool_buffers
);
232 pipe_reference_init(&rbuf
->b
.b
.b
.reference
, 1);
233 rbuf
->b
.b
.b
.screen
= screen
;
234 rbuf
->b
.b
.b
.target
= PIPE_BUFFER
;
235 rbuf
->b
.b
.b
.format
= PIPE_FORMAT_R8_UNORM
;
236 rbuf
->b
.b
.b
.usage
= PIPE_USAGE_IMMUTABLE
;
237 rbuf
->b
.b
.b
.bind
= bind
;
238 rbuf
->b
.b
.b
.width0
= ~0;
239 rbuf
->b
.b
.b
.height0
= 1;
240 rbuf
->b
.b
.b
.depth0
= 1;
241 rbuf
->b
.b
.b
.array_size
= 1;
242 rbuf
->b
.b
.b
.flags
= 0;
243 rbuf
->b
.b
.vtbl
= &r300_buffer_vtbl
;
244 rbuf
->b
.user_ptr
= ptr
;
245 rbuf
->domain
= RADEON_DOMAIN_GTT
;
247 rbuf
->constant_buffer
= NULL
;