summaryrefslogtreecommitdiff
path: root/src/util/asset_manager.cc
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-01 18:56:47 +0100
committerskal <pascal.massimino@gmail.com>2026-02-01 18:56:47 +0100
commite3ef115804b46e8bfc594987b04b1059aed5e002 (patch)
tree3670f718ee23cf34e94984568900eac23022d1f0 /src/util/asset_manager.cc
parent7fab8880cca269621cd32610b22f2820567771f2 (diff)
feat(gpu/assets): Fix tests, integrate bumpy 3D renderer and procedural assets
- Fixed test_sequence by restoring MainSequence::init_test for mocking. - Corrected CMakeLists.txt dependencies and source groupings to prevent duplicate symbols. - standardizing Effect constructor signature for seq_compiler compatibility. - Implemented Hybrid3DEffect using bumpy Renderer3D and procedural NOISE_TEX. - Updated MainSequence to support depth buffer for 3D elements. - Formatted all source files with clang-format.
Diffstat (limited to 'src/util/asset_manager.cc')
-rw-r--r--src/util/asset_manager.cc68
1 files changed, 39 insertions, 29 deletions
diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc
index 2ad8ef9..845934b 100644
--- a/src/util/asset_manager.cc
+++ b/src/util/asset_manager.cc
@@ -8,12 +8,12 @@
#include "generated/assets.h"
#endif /* defined(USE_TEST_ASSETS) */
-#include <vector> // For potential dynamic allocation for procedural assets
-#include <new> // For placement new
-#include <cstdlib> // For free
-#include <iostream> // For std::cerr
-#include <map> // For kAssetManagerProcGenFuncMap
-#include <string> // For std::string in map
+#include <cstdlib> // For free
+#include <iostream> // For std::cerr
+#include <map> // For kAssetManagerProcGenFuncMap
+#include <new> // For placement new
+#include <string> // For std::string in map
+#include <vector> // For potential dynamic allocation for procedural assets
#include "procedural/generator.h" // For ProcGenFunc and procedural functions
@@ -41,17 +41,17 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) {
uint16_t index = (uint16_t)asset_id;
// Assert that ASSET_LAST_ID is not used for retrieval.
- // This ensures the size of the cache is correctly used and not accessed out of bounds.
- // If this assert fails, it means assets.txt has an ID larger than expected
- // or ASSET_LAST_ID is not correctly generated/updated.
- // This is a design decision: ASSET_LAST_ID is purely for sizing and range checking,
- // not for a valid asset retrieval itself.
+ // This ensures the size of the cache is correctly used and not accessed out
+ // of bounds. If this assert fails, it means assets.txt has an ID larger than
+ // expected or ASSET_LAST_ID is not correctly generated/updated. This is a
+ // design decision: ASSET_LAST_ID is purely for sizing and range checking, not
+ // for a valid asset retrieval itself.
if (index >= (uint16_t)AssetId::ASSET_LAST_ID) {
if (out_size)
*out_size = 0;
return nullptr; // Invalid asset_id
}
-
+
// Check cache first
if (g_asset_cache[index].data != nullptr) {
if (out_size)
@@ -59,7 +59,8 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) {
return g_asset_cache[index].data;
}
- // Not in cache, retrieve from static data (packed in binary) or generate procedurally
+ // Not in cache, retrieve from static data (packed in binary) or generate
+ // procedurally
if (index >= g_assets_count) {
if (out_size)
*out_size = 0;
@@ -71,36 +72,44 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) {
if (source_record.is_procedural) {
// Dynamically generate the asset
- auto it = kAssetManagerProcGenFuncMap.find(source_record.proc_func_name_str);
+ auto it =
+ kAssetManagerProcGenFuncMap.find(source_record.proc_func_name_str);
if (it == kAssetManagerProcGenFuncMap.end()) {
- std::cerr << "Error: Unknown procedural function at runtime: " << source_record.proc_func_name_str << std::endl;
- if (out_size) *out_size = 0;
- return nullptr; // Procedural asset without a generation function.
+ std::cerr << "Error: Unknown procedural function at runtime: "
+ << source_record.proc_func_name_str << std::endl;
+ if (out_size)
+ *out_size = 0;
+ return nullptr; // Procedural asset without a generation function.
}
ProcGenFunc proc_gen_func_ptr = it->second;
- // For this demo, assuming procedural textures are RGBA8 256x256 (for simplicity and bump mapping).
- // A more generic solution would pass dimensions in proc_params.
+ // For this demo, assuming procedural textures are RGBA8 256x256 (for
+ // simplicity and bump mapping). A more generic solution would pass
+ // dimensions in proc_params.
int width = 256, height = 256;
size_t data_size = width * height * 4; // RGBA8
uint8_t* generated_data = new (std::nothrow) uint8_t[data_size];
if (!generated_data) {
- std::cerr << "Error: Failed to allocate memory for procedural asset." << std::endl;
- if (out_size) *out_size = 0;
- return nullptr;
+ std::cerr << "Error: Failed to allocate memory for procedural asset."
+ << std::endl;
+ if (out_size)
+ *out_size = 0;
+ return nullptr;
}
- proc_gen_func_ptr(generated_data, width, height, source_record.proc_params, source_record.num_proc_params);
-
+ proc_gen_func_ptr(generated_data, width, height, source_record.proc_params,
+ source_record.num_proc_params);
+
cached_record.data = generated_data;
cached_record.size = data_size;
cached_record.is_procedural = true;
- // proc_gen_func, proc_params, num_proc_params already copied from source_record
+ // proc_gen_func, proc_params, num_proc_params already copied from
+ // source_record
} else {
// Static asset (copy from g_assets)
cached_record.is_procedural = false;
}
-
+
// Store in cache for future use
g_asset_cache[index] = cached_record;
@@ -115,9 +124,10 @@ void DropAsset(AssetId asset_id, const uint8_t* asset) {
return; // Invalid asset_id
}
- // Check if the asset is in cache and is procedural, and if the pointer matches.
- // This prevents accidentally freeing static data or freeing twice.
- if (g_asset_cache[index].data == asset && g_asset_cache[index].is_procedural) {
+ // Check if the asset is in cache and is procedural, and if the pointer
+ // matches. This prevents accidentally freeing static data or freeing twice.
+ if (g_asset_cache[index].data == asset &&
+ g_asset_cache[index].is_procedural) {
delete[] g_asset_cache[index].data;
g_asset_cache[index] = {}; // Zero out the struct to force re-generation
}