2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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.
25 * Corbin Simpson <MostAwesomeDude@gmail.com>
28 #include "pipe/p_screen.h"
29 #include "util/u_format.h"
30 #include "util/u_math.h"
31 #include "util/u_inlines.h"
32 #include "util/u_memory.h"
33 #include "util/u_upload_mgr.h"
36 #include "radeonsi_pipe.h"
38 static void r600_buffer_destroy(struct pipe_screen
*screen
,
39 struct pipe_resource
*buf
)
41 struct si_resource
*rbuffer
= si_resource(buf
);
43 pb_reference(&rbuffer
->buf
, NULL
);
47 static void *r600_buffer_transfer_map(struct pipe_context
*ctx
,
48 struct pipe_resource
*resource
,
51 const struct pipe_box
*box
,
52 struct pipe_transfer
**ptransfer
)
54 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
55 struct pipe_transfer
*transfer
;
56 struct si_resource
*rbuffer
= si_resource(resource
);
59 data
= rctx
->ws
->buffer_map(rbuffer
->cs_buf
, rctx
->cs
, usage
);
64 transfer
= util_slab_alloc(&rctx
->pool_transfers
);
65 transfer
->resource
= resource
;
66 transfer
->level
= level
;
67 transfer
->usage
= usage
;
70 transfer
->layer_stride
= 0;
71 *ptransfer
= transfer
;
73 return (uint8_t*)data
+ transfer
->box
.x
;
76 static void r600_buffer_transfer_unmap(struct pipe_context
*ctx
,
77 struct pipe_transfer
*transfer
)
79 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
80 util_slab_free(&rctx
->pool_transfers
, transfer
);
83 static void r600_buffer_transfer_flush_region(struct pipe_context
*pipe
,
84 struct pipe_transfer
*transfer
,
85 const struct pipe_box
*box
)
89 static const struct u_resource_vtbl r600_buffer_vtbl
=
91 u_default_resource_get_handle
, /* get_handle */
92 r600_buffer_destroy
, /* resource_destroy */
93 r600_buffer_transfer_map
, /* transfer_map */
94 r600_buffer_transfer_flush_region
, /* transfer_flush_region */
95 r600_buffer_transfer_unmap
, /* transfer_unmap */
96 NULL
/* transfer_inline_write */
99 bool si_init_resource(struct r600_screen
*rscreen
,
100 struct si_resource
*res
,
101 unsigned size
, unsigned alignment
,
102 boolean use_reusable_pool
, unsigned usage
)
104 uint32_t initial_domain
, domains
;
106 /* Staging resources particpate in transfers and blits only
107 * and are used for uploads and downloads from regular
108 * resources. We generate them internally for some transfers.
110 if (usage
== PIPE_USAGE_STAGING
) {
111 domains
= RADEON_DOMAIN_GTT
;
112 initial_domain
= RADEON_DOMAIN_GTT
;
114 domains
= RADEON_DOMAIN_GTT
| RADEON_DOMAIN_VRAM
;
117 case PIPE_USAGE_DYNAMIC
:
118 case PIPE_USAGE_STREAM
:
119 case PIPE_USAGE_STAGING
:
120 initial_domain
= RADEON_DOMAIN_GTT
;
122 case PIPE_USAGE_DEFAULT
:
123 case PIPE_USAGE_STATIC
:
124 case PIPE_USAGE_IMMUTABLE
:
126 initial_domain
= RADEON_DOMAIN_VRAM
;
131 res
->buf
= rscreen
->ws
->buffer_create(rscreen
->ws
, size
, alignment
,
138 res
->cs_buf
= rscreen
->ws
->buffer_get_cs_handle(res
->buf
);
139 res
->domains
= domains
;
143 struct pipe_resource
*si_buffer_create(struct pipe_screen
*screen
,
144 const struct pipe_resource
*templ
)
146 struct r600_screen
*rscreen
= (struct r600_screen
*)screen
;
147 struct si_resource
*rbuffer
;
148 /* XXX We probably want a different alignment for buffers and textures. */
149 unsigned alignment
= 4096;
151 rbuffer
= MALLOC_STRUCT(si_resource
);
153 rbuffer
->b
.b
= *templ
;
154 pipe_reference_init(&rbuffer
->b
.b
.reference
, 1);
155 rbuffer
->b
.b
.screen
= screen
;
156 rbuffer
->b
.vtbl
= &r600_buffer_vtbl
;
158 if (!si_init_resource(rscreen
, rbuffer
, templ
->width0
, alignment
, TRUE
, templ
->usage
)) {
162 return &rbuffer
->b
.b
;
165 void r600_upload_index_buffer(struct r600_context
*rctx
,
166 struct pipe_index_buffer
*ib
, unsigned count
)
168 u_upload_data(rctx
->uploader
, 0, count
* ib
->index_size
,
169 ib
->user_buffer
, &ib
->offset
, &ib
->buffer
);
172 void r600_upload_const_buffer(struct r600_context
*rctx
, struct si_resource
**rbuffer
,
173 const uint8_t *ptr
, unsigned size
,
174 uint32_t *const_offset
)
176 if (R600_BIG_ENDIAN
) {
180 if (!(tmpPtr
= malloc(size
))) {
181 R600_ERR("Failed to allocate BE swap buffer.\n");
185 for (i
= 0; i
< size
/ 4; ++i
) {
186 tmpPtr
[i
] = util_bswap32(((uint32_t *)ptr
)[i
]);
189 u_upload_data(rctx
->uploader
, 0, size
, tmpPtr
, const_offset
,
190 (struct pipe_resource
**)rbuffer
);
194 u_upload_data(rctx
->uploader
, 0, size
, ptr
, const_offset
,
195 (struct pipe_resource
**)rbuffer
);