2 * © Copyright 2017-2018 Alyssa Rosenzweig
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #ifndef __AGX_POOL_H__
26 #define __AGX_POOL_H__
29 #include "asahi/lib/agx_pack.h"
32 #include "util/u_dynarray.h"
34 /* Represents a pool of memory that can only grow, used to allocate objects
35 * with the same lifetime as the pool itself. In OpenGL, a pool is owned by the
36 * batch for transient structures. In Vulkan, it may be owned by e.g. the
40 /* Parent device for allocation */
41 struct agx_device *dev;
43 /* BOs allocated by this pool */
44 struct util_dynarray bos;
46 /* Current transient BO */
47 struct agx_bo *transient_bo;
49 /* Within the topmost transient BO, how much has been used? */
50 unsigned transient_offset;
52 /* BO flags to use in the pool */
53 unsigned create_flags;
57 agx_pool_init(struct agx_pool *pool, struct agx_device *dev,
58 unsigned create_flags, bool prealloc);
61 agx_pool_cleanup(struct agx_pool *pool);
63 static inline unsigned
64 agx_pool_num_bos(struct agx_pool *pool)
66 return util_dynarray_num_elements(&pool->bos, struct agx_bo *);
70 agx_pool_get_bo_handles(struct agx_pool *pool, uint32_t *handles);
72 /* Represents a fat pointer for GPU-mapped memory, returned from the transient
73 * allocator and not used for much else */
76 agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz,
77 unsigned alignment, struct agx_bo **bo);
79 static inline struct agx_ptr
80 agx_pool_alloc_aligned(struct agx_pool *pool, size_t sz, unsigned alignment)
82 return agx_pool_alloc_aligned_with_bo(pool, sz, alignment, NULL);
86 agx_pool_upload(struct agx_pool *pool, const void *data, size_t sz);
89 agx_pool_upload_aligned_with_bo(struct agx_pool *pool, const void *data,
90 size_t sz, unsigned alignment,
93 static inline uint64_t
94 agx_pool_upload_aligned(struct agx_pool *pool, const void *data, size_t sz, unsigned alignment)
96 return agx_pool_upload_aligned_with_bo(pool, data, sz, alignment, NULL);
99 struct agx_desc_alloc_info {
105 #define AGX_DESC_ARRAY(count, name) \
107 .size = MALI_ ## name ## _LENGTH, \
108 .align = MALI_ ## name ## _ALIGN, \
112 #define AGX_DESC(name) AGX_DESC_ARRAY(1, name)
114 #define AGX_DESC_AGGREGATE(...) \
115 (struct agx_desc_alloc_info[]) { \
120 static inline struct agx_ptr
121 agx_pool_alloc_descs(struct agx_pool *pool,
122 const struct agx_desc_alloc_info *descs)
125 unsigned align = descs[0].align;
127 for (unsigned i = 0; descs[i].size; i++) {
128 assert(!(size & (descs[i].align - 1)));
129 size += descs[i].size * descs[i].nelems;
132 return agx_pool_alloc_aligned(pool, size, align);
135 #define agx_pool_alloc_desc(pool, name) \
136 agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(AGX_DESC(name)))
138 #define agx_pool_alloc_desc_array(pool, count, name) \
139 agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(AGX_DESC_ARRAY(count, name)))
141 #define agx_pool_alloc_desc_aggregate(pool, ...) \
142 agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(__VA_ARGS__))