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
,
38 enum r300_reference_domain domain
)
40 struct r300_context
*r300
= r300_context(context
);
41 struct r300_buffer
*rbuf
= r300_buffer(buf
);
43 if (r300_buffer_is_user_buffer(buf
))
44 return PIPE_UNREFERENCED
;
46 if (r300
->rws
->is_buffer_referenced(r300
->rws
, rbuf
->buf
, domain
))
47 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
49 return PIPE_UNREFERENCED
;
52 static unsigned r300_buffer_is_referenced_by_cs(struct pipe_context
*context
,
53 struct pipe_resource
*buf
,
54 unsigned face
, unsigned level
)
56 return r300_buffer_is_referenced(context
, buf
, R300_REF_CS
);
59 /* External helper, not required to implent u_resource_vtbl:
61 int r300_upload_index_buffer(struct r300_context
*r300
,
62 struct pipe_resource
**index_buffer
,
67 struct pipe_resource
*upload_buffer
= NULL
;
68 unsigned index_offset
= start
* index_size
;
71 if (r300_buffer_is_user_buffer(*index_buffer
)) {
72 ret
= u_upload_buffer(r300
->upload_ib
,
81 *index_buffer
= upload_buffer
;
85 // pipe_resource_reference(&upload_buffer, NULL);
89 /* External helper, not required to implement u_resource_vtbl:
91 int r300_upload_user_buffers(struct r300_context
*r300
)
93 enum pipe_error ret
= PIPE_OK
;
96 nr
= r300
->velems
->count
;
98 for (i
= 0; i
< nr
; i
++) {
99 struct pipe_vertex_buffer
*vb
=
100 &r300
->vertex_buffer
[r300
->velems
->velem
[i
].vertex_buffer_index
];
102 if (r300_buffer_is_user_buffer(vb
->buffer
)) {
103 struct pipe_resource
*upload_buffer
= NULL
;
104 unsigned offset
= 0; /*vb->buffer_offset * 4;*/
105 unsigned size
= vb
->buffer
->width0
;
106 unsigned upload_offset
;
107 ret
= u_upload_buffer(r300
->upload_vb
,
110 &upload_offset
, &upload_buffer
);
114 pipe_resource_reference(&vb
->buffer
, NULL
);
115 vb
->buffer
= upload_buffer
;
116 vb
->buffer_offset
= upload_offset
;
122 static void r300_winsys_buffer_destroy(struct r300_screen
*r300screen
,
123 struct r300_buffer
*rbuf
)
125 struct r300_winsys_screen
*rws
= r300screen
->rws
;
128 rws
->buffer_reference(rws
, &rbuf
->buf
, NULL
);
133 static void r300_buffer_destroy(struct pipe_screen
*screen
,
134 struct pipe_resource
*buf
)
136 struct r300_screen
*r300screen
= r300_screen(screen
);
137 struct r300_buffer
*rbuf
= r300_buffer(buf
);
139 r300_winsys_buffer_destroy(r300screen
, rbuf
);
144 r300_buffer_transfer_map( struct pipe_context
*pipe
,
145 struct pipe_transfer
*transfer
)
147 struct r300_screen
*r300screen
= r300_screen(pipe
->screen
);
148 struct r300_winsys_screen
*rws
= r300screen
->rws
;
149 struct r300_buffer
*rbuf
= r300_buffer(transfer
->resource
);
151 boolean flush
= FALSE
;
154 if (rbuf
->user_buffer
)
155 return (uint8_t *) rbuf
->user_buffer
+ transfer
->box
.x
;
157 if (rbuf
->b
.b
.bind
& PIPE_BIND_CONSTANT_BUFFER
) {
161 /* check if the mapping is to a range we already flushed */
162 if (transfer
->usage
& PIPE_TRANSFER_DISCARD
) {
163 for (i
= 0; i
< rbuf
->num_ranges
; i
++) {
164 if ((transfer
->box
.x
>= rbuf
->ranges
[i
].start
) &&
165 (transfer
->box
.x
< rbuf
->ranges
[i
].end
))
169 /* unreference this hw buffer and allocate a new one */
170 rws
->buffer_reference(rws
, &rbuf
->buf
, NULL
);
172 rbuf
->num_ranges
= 0;
173 rbuf
->buf
= r300screen
->rws
->buffer_create(r300screen
->rws
, 16,
182 map
= rws
->buffer_map(rws
, rbuf
->buf
, transfer
->usage
);
187 /* map_buffer() returned a pointer to the beginning of the buffer,
188 * but transfers are expected to return a pointer to just the
189 * region specified in the box.
191 return map
+ transfer
->box
.x
;
194 static void r300_buffer_transfer_flush_region( struct pipe_context
*pipe
,
195 struct pipe_transfer
*transfer
,
196 const struct pipe_box
*box
)
198 struct r300_buffer
*rbuf
= r300_buffer(transfer
->resource
);
200 unsigned offset
= transfer
->box
.x
+ box
->x
;
201 unsigned length
= box
->width
;
203 assert(box
->x
+ box
->width
<= transfer
->box
.width
);
205 if (rbuf
->user_buffer
)
208 if (rbuf
->b
.b
.bind
& PIPE_BIND_CONSTANT_BUFFER
)
211 /* mark the range as used */
212 for(i
= 0; i
< rbuf
->num_ranges
; ++i
) {
213 if(offset
<= rbuf
->ranges
[i
].end
&& rbuf
->ranges
[i
].start
<= (offset
+box
->width
)) {
214 rbuf
->ranges
[i
].start
= MIN2(rbuf
->ranges
[i
].start
, offset
);
215 rbuf
->ranges
[i
].end
= MAX2(rbuf
->ranges
[i
].end
, (offset
+length
));
220 rbuf
->ranges
[rbuf
->num_ranges
].start
= offset
;
221 rbuf
->ranges
[rbuf
->num_ranges
].end
= offset
+length
;
225 static void r300_buffer_transfer_unmap( struct pipe_context
*pipe
,
226 struct pipe_transfer
*transfer
)
228 struct r300_screen
*r300screen
= r300_screen(pipe
->screen
);
229 struct r300_winsys_screen
*rws
= r300screen
->rws
;
230 struct r300_buffer
*rbuf
= r300_buffer(transfer
->resource
);
233 rws
->buffer_unmap(rws
, rbuf
->buf
);
237 struct u_resource_vtbl r300_buffer_vtbl
=
239 u_default_resource_get_handle
, /* get_handle */
240 r300_buffer_destroy
, /* resource_destroy */
241 r300_buffer_is_referenced_by_cs
, /* is_buffer_referenced */
242 u_default_get_transfer
, /* get_transfer */
243 u_default_transfer_destroy
, /* transfer_destroy */
244 r300_buffer_transfer_map
, /* transfer_map */
245 r300_buffer_transfer_flush_region
, /* transfer_flush_region */
246 r300_buffer_transfer_unmap
, /* transfer_unmap */
247 u_default_transfer_inline_write
/* transfer_inline_write */
250 struct pipe_resource
*r300_buffer_create(struct pipe_screen
*screen
,
251 const struct pipe_resource
*templ
)
253 struct r300_screen
*r300screen
= r300_screen(screen
);
254 struct r300_buffer
*rbuf
;
255 unsigned alignment
= 16;
257 rbuf
= CALLOC_STRUCT(r300_buffer
);
261 rbuf
->magic
= R300_BUFFER_MAGIC
;
264 rbuf
->b
.vtbl
= &r300_buffer_vtbl
;
265 pipe_reference_init(&rbuf
->b
.b
.reference
, 1);
266 rbuf
->b
.b
.screen
= screen
;
267 rbuf
->domain
= R300_DOMAIN_GTT
;
269 rbuf
->buf
= r300screen
->rws
->buffer_create(r300screen
->rws
,
285 struct pipe_resource
*r300_user_buffer_create(struct pipe_screen
*screen
,
290 struct r300_buffer
*rbuf
;
292 rbuf
= CALLOC_STRUCT(r300_buffer
);
296 rbuf
->magic
= R300_BUFFER_MAGIC
;
298 pipe_reference_init(&rbuf
->b
.b
.reference
, 1);
299 rbuf
->b
.vtbl
= &r300_buffer_vtbl
;
300 rbuf
->b
.b
.screen
= screen
;
301 rbuf
->b
.b
.format
= PIPE_FORMAT_R8_UNORM
;
302 rbuf
->b
.b
.usage
= PIPE_USAGE_IMMUTABLE
;
303 rbuf
->b
.b
.bind
= bind
;
304 rbuf
->b
.b
.width0
= bytes
;
305 rbuf
->b
.b
.height0
= 1;
306 rbuf
->b
.b
.depth0
= 1;
307 rbuf
->domain
= R300_DOMAIN_GTT
;
309 rbuf
->user_buffer
= ptr
;