summaryrefslogtreecommitdiff
path: root/src/asset-manager.cpp
blob: b7a82fd36f88975176ebe9a5c3425886c0e2422f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

#include "asset-manager.hpp"

#include "bucketed-array.hpp"
#include "thread-pool.hpp"

#include <cstring>
#include <filesystem>
#include <fstream>
#include <future>

const pk_handle_item_index_T maxAssetItemsPerBucket = 64;

BucketContainer<Asset, AssetHandle> Asset_BucketContainer{};

ThreadPoolHandle assetThreadPool = ThreadPoolHandle_MAX;

AssetKey EngineDefinedAssets[EngineDefinedAssetCount] = {
	"pke_prsnt_vrt\0\0",
	"pke_prsnt_frg\0\0",
	"pke_txtr_vrt\0\0\0",
	"pke_txtr_frg\0\0\0",
	"pke_glyph_vrt\0\0",
	"pke_glyph_frg\0\0",
	"pke_ui_bs_vrt\0\0",
	"pke_ui_bs_frg\0\0",
};

void AM_Init() {
	Buckets_Init(Asset_BucketContainer, maxAssetItemsPerBucket);
	assetThreadPool = 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");
	AM_Register(EngineDefinedAssets[3], PKE_ASSET_TYPE_SHADER, "assets/shaders/texture.frag.spv");
	AM_Register(EngineDefinedAssets[4], PKE_ASSET_TYPE_SHADER, "assets/shaders/glyph.vert.spv");
	AM_Register(EngineDefinedAssets[5], PKE_ASSET_TYPE_SHADER, "assets/shaders/glyph.frag.spv");
	AM_Register(EngineDefinedAssets[6], PKE_ASSET_TYPE_SHADER, "assets/shaders/ui-base.vert.spv");
	AM_Register(EngineDefinedAssets[7], PKE_ASSET_TYPE_SHADER, "assets/shaders/ui-base.frag.spv");
}

void AM_Load_Task(Asset &asset) {
	std::ifstream file(asset.basePath, std::ios::ate | std::ios::binary);
	if (!file.is_open()) {
		asset.state = PKE_ASSET_LOADING_STATE_FAILED;
		return;
	}
	asset.size = std::filesystem::file_size(asset.basePath);
	void *ptr = pk_new_base(asset.size, alignof(max_align_t));
	file.seekg(0, std::ios::beg);
	file.read(static_cast<char *>(ptr), asset.size);
	file.close();
	asset.ptr = ptr;
	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;
}

AssetHandle AM_Register(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");
	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];
	new (&asset) Asset{};
	asset.handle = assetHandle;
	strncpy(asset.key, key, AssetKeyLength);
	asset.basePath = nullptr;
	asset.size = size;
	void *ptr = pk_new_base(size, alignment);
	memcpy(ptr, data, size);
	asset.ptr = ptr;
	asset.state = PKE_ASSET_LOADING_STATE_LOADED;
	asset.type = type;
	return assetHandle;
}

AssetHandle AM_Register(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];
	new (&asset) Asset{};
	asset.handle = assetHandle;
	strncpy(asset.key, key, AssetKeyLength);
	int64_t pathLen = strlen(path);
	auto *copiedPath = pk_new<char>(pathLen + 1);
	copiedPath[pathLen] = '\0';
	strncpy(copiedPath, path, pathLen);
	asset.basePath = copiedPath;
	asset.state = PKE_ASSET_LOADING_STATE_LOADING;
	asset.type = type;
	std::packaged_task<void()> *task = pk_new<std::packaged_task<void()>>();
	new (task) std::packaged_task<void()>( [&asset] {
		AM_Load_Task(asset);
	});
	asset.future = task->get_future();
	PkeThreads_Enqueue(assetThreadPool, task);
	return assetHandle;
}

AssetHandle AM_Register(const char *path, AssetType type) {
	NULL_CHAR_ARR(assetKey, AssetKeyLength);
	int64_t pathLen = strlen(path);
	int64_t pathOffset = (pathLen > AssetKeyLength ? pathLen - AssetKeyLength : 0);
	strncpy(assetKey, path + pathOffset, AssetKeyLength);

	Asset *searchedAsset = AM_Get_Inner(assetKey);
	if (searchedAsset != nullptr) {
		return searchedAsset->handle;
	}

	AssetHandle assetHandle{Buckets_NewHandle(Asset_BucketContainer)};
	Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex][assetHandle.itemIndex];
	new (&asset) Asset{};
	asset.handle = assetHandle;
	strncpy(asset.key, assetKey, AssetKeyLength);
	auto *copiedPath = pk_new<char>(pathLen + 1);
	copiedPath[pathLen] = '\0';
	strncpy(copiedPath, path, pathLen);
	asset.basePath = copiedPath;
	asset.state = PKE_ASSET_LOADING_STATE_LOADING;
	asset.type = type;
	std::packaged_task<void()> *task = pk_new<std::packaged_task<void()>>();
	new (task) std::packaged_task<void()>( [&asset] {
		AM_Load_Task(asset);
	});
	asset.future = task->get_future();
	PkeThreads_Enqueue(assetThreadPool, task);
	return assetHandle;
}

AssetHandle AM_Register_Static(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");
	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];
	new (&asset) Asset{};
	asset.handle = assetHandle;
	strncpy(asset.key, key, AssetKeyLength);
	asset.basePath = nullptr;
	asset.size = size;
	asset.ptr = data;
	asset.state = PKE_ASSET_LOADING_STATE_LOADED;
	asset.type = type;
	asset.flags = PKE_ASSET_FLAGS_MEM_STATIC;
	return assetHandle;
}

void AM_Release(AssetHandle assetHandle) {
	assert(assetHandle != AssetHandle_MAX);
	Asset &asset = Asset_BucketContainer.buckets[assetHandle.bucketIndex][assetHandle.itemIndex];
	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];
	if (asset.state == PKE_ASSET_LOADING_STATE_LOADED) {
		asset.referenceCount += 1;
		return &asset;
	}
	if (asset.state == PKE_ASSET_LOADING_STATE_UNLOADED) {
		if (asset.basePath == nullptr) {
			return nullptr;
		}
		asset.state = PKE_ASSET_LOADING_STATE_LOADING;
		std::packaged_task<void()> *task = pk_new<std::packaged_task<void()>>();
		new (task) std::packaged_task<void()>( [&asset] {
			AM_Load_Task(asset);
		});
		asset.future = task->get_future();
		PkeThreads_Enqueue(assetThreadPool, task);
	}
	if (asset.state == PKE_ASSET_LOADING_STATE_LOADING) {
		if (asset.future.valid()) {
			asset.future.wait();
		} else {
			char buf[256];
			buf[255] = '\0';
			snprintf(buf, 255, "[AM_Get] Attempting to retrieve asset: '%.16s' which had a state of '%hhu', but did not have a valid future and thus cannot wait for completion", asset.key, static_cast<AssetLoadingState_T>(asset.state));
			throw buf;
		}
	}
	if (asset.state == PKE_ASSET_LOADING_STATE_LOADED)
		asset.referenceCount += 1;
	return &asset;
}

const AssetHandle AM_GetHandle(AssetKey key) {
	Asset *searchedAsset = AM_Get_Inner(key);
	if (searchedAsset != nullptr) {
		return searchedAsset->handle;
	}
	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];
}

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));
		}
	}
}

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", 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;
		}
	}
}

void AM_Teardown() {
	AM_GC();
	Buckets_Destroy(Asset_BucketContainer);
	PkeThreads_Teardown(assetThreadPool);
}