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"
34 #include "r300_winsys.h"
36 unsigned r300_buffer_is_referenced(struct pipe_context
*context
,
37 struct pipe_resource
*buf
)
39 struct r300_context
*r300
= r300_context(context
);
40 struct r300_resource
*rbuf
= r300_resource(buf
);
42 if (rbuf
->b
.user_ptr
|| rbuf
->constant_buffer
)
43 return PIPE_UNREFERENCED
;
45 if (r300
->rws
->cs_is_buffer_referenced(r300
->cs
, rbuf
->cs_buf
))
46 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
48 return PIPE_UNREFERENCED
;
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
)
56 unsigned index_offset
;
61 u_upload_data(r300
->vbuf_mgr
->uploader
,
62 0, count
* index_size
,
63 ptr
+ (*start
* index_size
),
65 index_buffer
, &flushed
);
67 *start
= index_offset
/ index_size
;
69 if (flushed
|| !r300
->upload_ib_validated
) {
70 r300
->upload_ib_validated
= FALSE
;
71 r300
->validate_buffers
= TRUE
;
75 static void r300_buffer_destroy(struct pipe_screen
*screen
,
76 struct pipe_resource
*buf
)
78 struct r300_screen
*r300screen
= r300_screen(screen
);
79 struct r300_resource
*rbuf
= r300_resource(buf
);
81 if (rbuf
->constant_buffer
)
82 FREE(rbuf
->constant_buffer
);
85 r300_winsys_bo_reference(&rbuf
->buf
, NULL
);
87 util_slab_free(&r300screen
->pool_buffers
, rbuf
);
90 static struct pipe_transfer
*
91 r300_buffer_get_transfer(struct pipe_context
*context
,
92 struct pipe_resource
*resource
,
95 const struct pipe_box
*box
)
97 struct r300_context
*r300
= r300_context(context
);
98 struct pipe_transfer
*transfer
=
99 util_slab_alloc(&r300
->pool_transfers
);
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
;
109 /* Note strides are zero, this is ok for buffers, but not for
110 * textures 2d & higher at least.
115 static void r300_buffer_transfer_destroy(struct pipe_context
*pipe
,
116 struct pipe_transfer
*transfer
)
118 struct r300_context
*r300
= r300_context(pipe
);
119 util_slab_free(&r300
->pool_transfers
, transfer
);
123 r300_buffer_transfer_map( struct pipe_context
*pipe
,
124 struct pipe_transfer
*transfer
)
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
);
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
;
137 map
= rws
->buffer_map(rbuf
->buf
, r300
->cs
, transfer
->usage
);
142 return map
+ transfer
->box
.x
;
145 static void r300_buffer_transfer_unmap( struct pipe_context
*pipe
,
146 struct pipe_transfer
*transfer
)
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
);
153 rws
->buffer_unmap(rbuf
->buf
);
157 static void r300_buffer_transfer_inline_write(struct pipe_context
*pipe
,
158 struct pipe_resource
*resource
,
161 const struct pipe_box
*box
,
164 unsigned layer_stride
)
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
);
171 if (rbuf
->constant_buffer
) {
172 memcpy(rbuf
->constant_buffer
+ box
->x
, data
, box
->width
);
175 assert(rbuf
->b
.user_ptr
== NULL
);
177 map
= rws
->buffer_map(rbuf
->buf
, r300
->cs
,
178 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
| usage
);
180 memcpy(map
+ box
->x
, data
, box
->width
);
182 rws
->buffer_unmap(rbuf
->buf
);
185 static const struct u_resource_vtbl r300_buffer_vtbl
=
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 */
198 struct pipe_resource
*r300_buffer_create(struct pipe_screen
*screen
,
199 const struct pipe_resource
*templ
)
201 struct r300_screen
*r300screen
= r300_screen(screen
);
202 struct r300_resource
*rbuf
;
203 unsigned alignment
= 16;
205 rbuf
= util_slab_alloc(&r300screen
->pool_buffers
);
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
;
214 rbuf
->buf_size
= templ
->width0
;
215 rbuf
->constant_buffer
= NULL
;
217 /* Alloc constant buffers in RAM. */
218 if (templ
->bind
& PIPE_BIND_CONSTANT_BUFFER
) {
219 rbuf
->constant_buffer
= MALLOC(templ
->width0
);
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
,
229 util_slab_free(&r300screen
->pool_buffers
, rbuf
);
234 r300screen
->rws
->buffer_get_cs_handle(rbuf
->buf
);
239 struct pipe_resource
*r300_user_buffer_create(struct pipe_screen
*screen
,
240 void *ptr
, unsigned size
,
243 struct r300_screen
*r300screen
= r300_screen(screen
);
244 struct r300_resource
*rbuf
;
246 rbuf
= util_slab_alloc(&r300screen
->pool_buffers
);
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
;
263 rbuf
->buf_size
= size
;
264 rbuf
->constant_buffer
= NULL
;