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
;
44 u_upload_data(r300
->uploader
,
45 0, count
* index_size
,
46 ptr
+ (*start
* index_size
),
50 *start
= index_offset
/ index_size
;
53 static void r300_buffer_destroy(struct pipe_screen
*screen
,
54 struct pipe_resource
*buf
)
56 struct r300_screen
*r300screen
= r300_screen(screen
);
57 struct r300_resource
*rbuf
= r300_resource(buf
);
59 if (rbuf
->constant_buffer
)
60 FREE(rbuf
->constant_buffer
);
63 pb_reference(&rbuf
->buf
, NULL
);
65 util_slab_free(&r300screen
->pool_buffers
, rbuf
);
68 static struct pipe_transfer
*
69 r300_buffer_get_transfer(struct pipe_context
*context
,
70 struct pipe_resource
*resource
,
73 const struct pipe_box
*box
)
75 struct r300_context
*r300
= r300_context(context
);
76 struct pipe_transfer
*transfer
=
77 util_slab_alloc(&r300
->pool_transfers
);
79 transfer
->resource
= resource
;
80 transfer
->level
= level
;
81 transfer
->usage
= usage
;
84 transfer
->layer_stride
= 0;
85 transfer
->data
= NULL
;
87 /* Note strides are zero, this is ok for buffers, but not for
88 * textures 2d & higher at least.
93 static void r300_buffer_transfer_destroy(struct pipe_context
*pipe
,
94 struct pipe_transfer
*transfer
)
96 struct r300_context
*r300
= r300_context(pipe
);
97 util_slab_free(&r300
->pool_transfers
, transfer
);
101 r300_buffer_transfer_map( struct pipe_context
*pipe
,
102 struct pipe_transfer
*transfer
)
104 struct r300_context
*r300
= r300_context(pipe
);
105 struct r300_screen
*r300screen
= r300_screen(pipe
->screen
);
106 struct radeon_winsys
*rws
= r300screen
->rws
;
107 struct r300_resource
*rbuf
= r300_resource(transfer
->resource
);
109 enum pipe_transfer_usage usage
;
111 if (rbuf
->b
.b
.user_ptr
)
112 return rbuf
->b
.b
.user_ptr
+ transfer
->box
.x
;
113 if (rbuf
->constant_buffer
)
114 return (uint8_t *) rbuf
->constant_buffer
+ transfer
->box
.x
;
116 /* Buffers are never used for write, therefore mapping for read can be
118 usage
= transfer
->usage
;
119 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
120 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
123 map
= rws
->buffer_map(rbuf
->cs_buf
, r300
->cs
, usage
);
128 return map
+ transfer
->box
.x
;
131 static void r300_buffer_transfer_unmap( struct pipe_context
*pipe
,
132 struct pipe_transfer
*transfer
)
137 static const struct u_resource_vtbl r300_buffer_vtbl
=
139 NULL
, /* get_handle */
140 r300_buffer_destroy
, /* resource_destroy */
141 r300_buffer_get_transfer
, /* get_transfer */
142 r300_buffer_transfer_destroy
, /* transfer_destroy */
143 r300_buffer_transfer_map
, /* transfer_map */
144 NULL
, /* transfer_flush_region */
145 r300_buffer_transfer_unmap
, /* transfer_unmap */
146 NULL
/* transfer_inline_write */
149 struct pipe_resource
*r300_buffer_create(struct pipe_screen
*screen
,
150 const struct pipe_resource
*templ
)
152 struct r300_screen
*r300screen
= r300_screen(screen
);
153 struct r300_resource
*rbuf
;
154 unsigned alignment
= 16;
156 rbuf
= util_slab_alloc(&r300screen
->pool_buffers
);
159 rbuf
->b
.vtbl
= &r300_buffer_vtbl
;
160 pipe_reference_init(&rbuf
->b
.b
.reference
, 1);
161 rbuf
->b
.b
.screen
= screen
;
162 rbuf
->b
.b
.user_ptr
= NULL
;
163 rbuf
->domain
= RADEON_DOMAIN_GTT
;
165 rbuf
->constant_buffer
= NULL
;
167 /* Alloc constant buffers in RAM. */
168 if (templ
->bind
& PIPE_BIND_CONSTANT_BUFFER
) {
169 rbuf
->constant_buffer
= MALLOC(templ
->width0
);
174 r300screen
->rws
->buffer_create(r300screen
->rws
,
175 rbuf
->b
.b
.width0
, alignment
,
176 rbuf
->b
.b
.bind
, rbuf
->domain
);
178 util_slab_free(&r300screen
->pool_buffers
, rbuf
);
183 r300screen
->rws
->buffer_get_cs_handle(rbuf
->buf
);
188 struct pipe_resource
*r300_user_buffer_create(struct pipe_screen
*screen
,
189 void *ptr
, unsigned size
,
192 struct r300_screen
*r300screen
= r300_screen(screen
);
193 struct r300_resource
*rbuf
;
195 rbuf
= util_slab_alloc(&r300screen
->pool_buffers
);
197 pipe_reference_init(&rbuf
->b
.b
.reference
, 1);
198 rbuf
->b
.b
.screen
= screen
;
199 rbuf
->b
.b
.target
= PIPE_BUFFER
;
200 rbuf
->b
.b
.format
= PIPE_FORMAT_R8_UNORM
;
201 rbuf
->b
.b
.usage
= PIPE_USAGE_IMMUTABLE
;
202 rbuf
->b
.b
.bind
= bind
;
203 rbuf
->b
.b
.width0
= ~0;
204 rbuf
->b
.b
.height0
= 1;
205 rbuf
->b
.b
.depth0
= 1;
206 rbuf
->b
.b
.array_size
= 1;
208 rbuf
->b
.b
.user_ptr
= ptr
;
209 rbuf
->b
.vtbl
= &r300_buffer_vtbl
;
210 rbuf
->domain
= RADEON_DOMAIN_GTT
;
212 rbuf
->constant_buffer
= NULL
;