summaryrefslogtreecommitdiff
path: root/src/asset-manager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/asset-manager.cpp')
-rw-r--r--src/asset-manager.cpp166
1 files changed, 73 insertions, 93 deletions
diff --git a/src/asset-manager.cpp b/src/asset-manager.cpp
index a19937a..daaf6da 100644
--- a/src/asset-manager.cpp
+++ b/src/asset-manager.cpp
@@ -1,7 +1,7 @@
#include "asset-manager.hpp"
-#include "bucketed-array.hpp"
+#include "pk.h"
#include "thread-pool.hpp"
#include <cstring>
@@ -9,11 +9,10 @@
#include <fstream>
#include <future>
-const pk_handle_item_index_T maxAssetItemsPerBucket = 64;
-
-BucketContainer<Asset, AssetHandle> Asset_BucketContainer{};
-
-ThreadPoolHandle assetThreadPool = ThreadPoolHandle_MAX;
+struct Asset_Master {
+ pk_bkt_arr_t<Asset> bc{};
+ ThreadPoolHandle thread_pool = ThreadPoolHandle_MAX;
+} asset_mstr;
AssetKey EngineDefinedAssets[EngineDefinedAssetCount] = {
"pke_prsnt_vrt\0\0",
@@ -27,8 +26,8 @@ AssetKey EngineDefinedAssets[EngineDefinedAssetCount] = {
};
void AM_Init() {
- Buckets_Init(Asset_BucketContainer, maxAssetItemsPerBucket);
- assetThreadPool = PkeThreads_Init(2, 255);
+ new (&asset_mstr.bc) pk_bkt_arr_t<Asset>{};
+ asset_mstr.thread_pool = PkeThreads_Init(2, 255);
AM_Register(EngineDefinedAssets[0], PKE_ASSET_TYPE_SHADER, "assets/shaders/present.vert.spv");
AM_Register(EngineDefinedAssets[1], PKE_ASSET_TYPE_SHADER, "assets/shaders/present.frag.spv");
AM_Register(EngineDefinedAssets[2], PKE_ASSET_TYPE_SHADER, "assets/shaders/vertex.vert.spv");
@@ -54,24 +53,19 @@ void AM_Load_Task(Asset &asset) {
asset.state = PKE_ASSET_LOADING_STATE_LOADED;
}
-inline Asset *AM_Get_Inner(AssetKey key) {
- for (pk_handle_bucket_index_T b = 0; b <= Asset_BucketContainer.pkeHandle.bucketIndex; ++b) {
- pk_handle_item_index_T count = 0;
- if (b == Asset_BucketContainer.pkeHandle.bucketIndex) {
- count = Asset_BucketContainer.pkeHandle.itemIndex;
- } else {
- count = maxAssetItemsPerBucket;
- }
- for (pk_handle_item_index_T i = 0; i < count; ++i) {
- if (strncmp(key, Asset_BucketContainer.buckets[b][i].key, 16) == 0) {
- return &Asset_BucketContainer.buckets[b][i];
- }
- }
- }
- return nullptr;
+inline Asset *AM_Get_Inner(const char (&key)[AssetKeyLength]) {
+ auto asset_find_cb = [](void *user_data, const void *user_obj_data, const void *arr_obj_data) {
+ (void)user_data;
+ const char (&inner_key)[16] = *reinterpret_cast<const char(*&)[16]>(user_obj_data);
+ const Asset *asset = reinterpret_cast<const Asset *>(arr_obj_data);
+ return strncmp(inner_key, asset->key, 16) == 0;
+ };
+ AssetHandle handle { pk_bkt_arr_find_first_handle(&asset_mstr.bc, asset_find_cb, NULL, &key[0]) };
+ if (handle == AssetHandle_MAX) return nullptr;
+ return &asset_mstr.bc[handle];
}
-AssetHandle AM_Register(AssetKey key, AssetType type, const void *data, int64_t size, std::size_t alignment) {
+AssetHandle AM_Register(const AssetKey &key, AssetType type, const void *data, int64_t size, std::size_t alignment) {
assert(data != nullptr && "Attempt to register invalid asset data");
assert(data != CAFE_BABE(void) && "Attempt to register invalid asset data");
assert(size != 0 && "Attempt to register asset data of size 0");
@@ -79,8 +73,8 @@ AssetHandle AM_Register(AssetKey key, AssetType type, const void *data, int64_t
if (searchedAsset != nullptr) {
return searchedAsset->handle;
}
- AssetHandle assetHandle{Buckets_NewHandle(Asset_BucketContainer)};
- Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex][assetHandle.itemIndex];
+ AssetHandle assetHandle{pk_bkt_arr_new_handle(&asset_mstr.bc)};
+ Asset &asset = asset_mstr.bc[assetHandle];
new (&asset) Asset{};
asset.handle = assetHandle;
strncpy(asset.key, key, AssetKeyLength);
@@ -94,13 +88,13 @@ AssetHandle AM_Register(AssetKey key, AssetType type, const void *data, int64_t
return assetHandle;
}
-AssetHandle AM_Register(AssetKey key, AssetType type, const char *path) {
+AssetHandle AM_Register(const AssetKey &key, AssetType type, const char *path) {
Asset *searchedAsset = AM_Get_Inner(key);
if (searchedAsset != nullptr) {
return searchedAsset->handle;
}
- AssetHandle assetHandle{Buckets_NewHandle(Asset_BucketContainer)};
- Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex][assetHandle.itemIndex];
+ AssetHandle assetHandle{pk_bkt_arr_new_handle(&asset_mstr.bc)};
+ Asset &asset = asset_mstr.bc[assetHandle];
new (&asset) Asset{};
asset.handle = assetHandle;
strncpy(asset.key, key, AssetKeyLength);
@@ -116,7 +110,7 @@ AssetHandle AM_Register(AssetKey key, AssetType type, const char *path) {
AM_Load_Task(asset);
});
asset.future = task->get_future();
- PkeThreads_Enqueue(assetThreadPool, task);
+ PkeThreads_Enqueue(asset_mstr.thread_pool, task);
return assetHandle;
}
@@ -131,8 +125,8 @@ AssetHandle AM_Register(const char *path, AssetType type) {
return searchedAsset->handle;
}
- AssetHandle assetHandle{Buckets_NewHandle(Asset_BucketContainer)};
- Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex][assetHandle.itemIndex];
+ AssetHandle assetHandle{pk_bkt_arr_new_handle(&asset_mstr.bc)};
+ Asset &asset = asset_mstr.bc[assetHandle];
new (&asset) Asset{};
asset.handle = assetHandle;
strncpy(asset.key, assetKey, AssetKeyLength);
@@ -147,11 +141,11 @@ AssetHandle AM_Register(const char *path, AssetType type) {
AM_Load_Task(asset);
});
asset.future = task->get_future();
- PkeThreads_Enqueue(assetThreadPool, task);
+ PkeThreads_Enqueue(asset_mstr.thread_pool, task);
return assetHandle;
}
-AssetHandle AM_Register_Static(AssetKey key, AssetType type, const void *data, int64_t size) {
+AssetHandle AM_Register_Static(const AssetKey &key, AssetType type, const void *data, int64_t size) {
assert(data != nullptr && "Attempt to register invalid asset data");
assert(data != CAFE_BABE(void) && "Attempt to register invalid asset data");
assert(size != 0 && "Attempt to register asset data of size 0");
@@ -159,8 +153,8 @@ AssetHandle AM_Register_Static(AssetKey key, AssetType type, const void *data, i
if (searchedAsset != nullptr) {
return searchedAsset->handle;
}
- AssetHandle assetHandle{Buckets_NewHandle(Asset_BucketContainer)};
- Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex][assetHandle.itemIndex];
+ AssetHandle assetHandle{pk_bkt_arr_new_handle(&asset_mstr.bc)};
+ Asset &asset = asset_mstr.bc[assetHandle];
new (&asset) Asset{};
asset.handle = assetHandle;
strncpy(asset.key, key, AssetKeyLength);
@@ -175,15 +169,15 @@ AssetHandle AM_Register_Static(AssetKey key, AssetType type, const void *data, i
void AM_Release(AssetHandle assetHandle) {
assert(assetHandle != AssetHandle_MAX);
- Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex][assetHandle.itemIndex];
+ Asset &asset = asset_mstr.bc[assetHandle];
asset.referenceCount -= 1;
assert(asset.referenceCount >= 0 && "asset was unloaded more times than it was retrieved");
}
const Asset *AM_Get(AssetHandle assetHandle) {
- auto validationResult = pk_handle_validate(assetHandle, Asset_BucketContainer.pkeHandle, maxAssetItemsPerBucket);
- assert(validationResult == 0);
- auto &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex][assetHandle.itemIndex];
+ auto validationResult = pk_bkt_arr_handle_validate(&asset_mstr.bc, assetHandle);
+ assert(validationResult == PK_BKT_ARR_HANDLE_VALIDATION_VALID);
+ auto &asset = asset_mstr.bc[assetHandle];
if (asset.state == PKE_ASSET_LOADING_STATE_LOADED) {
asset.referenceCount += 1;
return &asset;
@@ -198,7 +192,7 @@ const Asset *AM_Get(AssetHandle assetHandle) {
AM_Load_Task(asset);
});
asset.future = task->get_future();
- PkeThreads_Enqueue(assetThreadPool, task);
+ PkeThreads_Enqueue(asset_mstr.thread_pool, task);
}
if (asset.state == PKE_ASSET_LOADING_STATE_LOADING) {
if (asset.future.valid()) {
@@ -215,7 +209,7 @@ const Asset *AM_Get(AssetHandle assetHandle) {
return &asset;
}
-const AssetHandle AM_GetHandle(AssetKey key) {
+const AssetHandle AM_GetHandle(const AssetKey &key) {
Asset *searchedAsset = AM_Get_Inner(key);
if (searchedAsset != nullptr) {
return searchedAsset->handle;
@@ -223,67 +217,53 @@ const AssetHandle AM_GetHandle(AssetKey key) {
return AssetHandle_MAX;
}
-pk_handle_bucket_index_T AM_GetBucketCount() {
- return Asset_BucketContainer.pkeHandle.bucketIndex + 1;
-}
-
-Asset *AM_GetAssets(pk_handle_bucket_index_T bucketIndex, pk_handle_item_index_T &itemCount) {
- if (bucketIndex == Asset_BucketContainer.pkeHandle.bucketIndex) {
- itemCount = Asset_BucketContainer.pkeHandle.itemIndex;
- } else {
- itemCount = maxAssetItemsPerBucket;
- }
- return Asset_BucketContainer.buckets[bucketIndex];
+pk_bkt_arr *AM_GetAssets() {
+ return &asset_mstr.bc;
}
void AM_DebugPrint() {
- printf("Asset Manager printout:\n");
- for (uint64_t b = 0; b <= Asset_BucketContainer.pkeHandle.bucketIndex; ++b) {
- auto &bkt = Asset_BucketContainer.buckets[b];
- uint64_t counter = b == Asset_BucketContainer.pkeHandle.bucketIndex ? Asset_BucketContainer.pkeHandle.itemIndex : maxAssetItemsPerBucket;
- for (uint64_t i = 0; i < counter; ++i) {
- auto &asset = bkt[i];
- /*
- if (asset.size == 0)
- continue;
- */
- printf("-Asset: 0x%016lX\n", (b << 32) + i);
- printf("\tkey: %.16s\n", asset.key);
- if (asset.basePath != nullptr) {
- printf("\tbasePath: %s\n", asset.basePath);
- } else {
- printf("\tbasePath: %p\n", (void *)nullptr);
- }
- printf("\tsize: %ld\n", asset.size);
- printf("\tptr %p\n", asset.ptr);
- printf("\tfuture: %i\n", asset.future.valid());
- printf("\treferenceCount: %i\n", asset.referenceCount);
- printf("\tAssetLoadingState: %hhu\n", static_cast<AssetLoadingState_T>(asset.state));
+ fprintf(stdout, "Asset Manager printout:\n");
+ auto asset_iter_fn = [](void *user_data, void *arr_obj_data) {
+ (void)user_data;
+ Asset &asset = *reinterpret_cast<Asset *>(arr_obj_data);
+ printf("-Asset: 0x%.08X 0x%.08X\n", asset.handle.b, asset.handle.i);
+ printf("\tkey: %.16s\n", asset.key);
+ if (asset.basePath != nullptr) {
+ printf("\tbasePath: %s\n", asset.basePath);
+ } else {
+ printf("\tbasePath: %p\n", (void *)nullptr);
}
- }
+ printf("\tsize: %ld\n", asset.size);
+ printf("\tptr %p\n", asset.ptr);
+ printf("\tfuture: %i\n", asset.future.valid());
+ printf("\treferenceCount: %i\n", asset.referenceCount);
+ printf("\tAssetLoadingState: %hhu\n", static_cast<AssetLoadingState_T>(asset.state));
+ };
+ pk_bkt_arr_iterate(&asset_mstr.bc, asset_iter_fn, NULL);
}
void AM_GC() {
- for (long b = 0; b <= Asset_BucketContainer.pkeHandle.bucketIndex; ++b) {
- for (long i = 0; i < Asset_BucketContainer.pkeHandle.itemIndex; ++i) {
- Asset &asset = Asset_BucketContainer.buckets[b][i];
- if (asset.referenceCount > 0) {
- fprintf(stderr, "[AM_GC] Asset '%.16s' still in use, count: %i\n", asset.key, asset.referenceCount);
- continue;
- }
- if (asset.ptr != nullptr && asset.ptr != CAFE_BABE(void) && !PK_HAS_FLAG(asset.flags, PKE_ASSET_FLAGS_MEM_STATIC)) {
- pk_delete_base(asset.ptr, asset.size);
- }
- asset.size = 0;
- asset.ptr = CAFE_BABE(void);
- asset.future = std::future<void>{};
- asset.state = PKE_ASSET_LOADING_STATE_UNLOADED;
+ auto asset_iter_fn = [](void *user_data, void *arr_obj_data) {
+ (void)user_data;
+ Asset &asset = *reinterpret_cast<Asset *>(arr_obj_data);
+ if (asset.referenceCount > 0) {
+ fprintf(stdout, "[AM_GC] Asset '%.16s' still in use, count: %i\n", asset.key, asset.referenceCount);
+ return;
}
- }
+ if (asset.ptr != nullptr && asset.ptr != CAFE_BABE(void) && !PK_HAS_FLAG(asset.flags, PKE_ASSET_FLAGS_MEM_STATIC)) {
+ pk_delete_base(asset.ptr, asset.size);
+ fprintf(stdout, "[AM_GC] Asset '%.16s' not in use, freeing.\n", asset.key);
+ }
+ asset.size = 0;
+ asset.ptr = CAFE_BABE(void);
+ new (&asset.future) std::future<void>{};
+ asset.state = PKE_ASSET_LOADING_STATE_UNLOADED;
+ };
+ pk_bkt_arr_iterate(&asset_mstr.bc, asset_iter_fn, NULL);
}
void AM_Teardown() {
AM_GC();
- Buckets_Destroy(Asset_BucketContainer);
- PkeThreads_Teardown(assetThreadPool);
+ pk_bkt_arr_teardown(&asset_mstr.bc);
+ PkeThreads_Teardown(asset_mstr.thread_pool);
}