summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-01 14:27:14 +0100
committerskal <pascal.massimino@gmail.com>2026-02-01 14:27:14 +0100
commitc7087fa3004349943d9b76e5015e87314b366de4 (patch)
treef688245d5bec66fb3eaea81e6ce38bc8dd63ebde
parenta358fbc9f4ba3a7b01f600109fc86aeb2fcf96b8 (diff)
feat(assets): Implement procedural asset generation pipeline
- Updated asset_packer to parse PROC(...) syntax. - Implemented runtime dispatch in AssetManager for procedural generation. - Added procedural generator functions (noise, grid, periodic). - Added comprehensive tests for procedural asset lifecycle.
-rw-r--r--CMakeLists.txt4
-rw-r--r--assets/final/test_assets_list.txt5
-rw-r--r--src/tests/test_assets.cc39
-rw-r--r--src/util/asset_manager.cc79
-rw-r--r--src/util/asset_manager.h12
-rw-r--r--tools/asset_packer.cc212
6 files changed, 258 insertions, 93 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5b07c07..984bb8d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -95,7 +95,7 @@ if (DEFINED ASSET_PACKER_PATH)
set(ASSET_PACKER_CMD ${ASSET_PACKER_PATH})
set(ASSET_PACKER_DEPENDS ${ASSET_PACKER_PATH})
else()
- add_executable(asset_packer tools/asset_packer.cc)
+ add_executable(asset_packer tools/asset_packer.cc ${PROCEDURAL_SOURCES})
set(ASSET_PACKER_CMD $<TARGET_FILE:asset_packer>)
set(ASSET_PACKER_DEPENDS asset_packer)
endif()
@@ -176,7 +176,7 @@ if(DEMO_BUILD_TESTS)
target_link_libraries(test_spectool PRIVATE ${DEMO_LIBS})
add_test(NAME SpectoolEndToEndTest COMMAND test_spectool)
- add_executable(test_assets src/tests/test_assets.cc ${UTIL_SOURCES} ${GEN_TEST_CC})
+ add_executable(test_assets src/tests/test_assets.cc ${UTIL_SOURCES} ${PROCEDURAL_SOURCES} ${GEN_TEST_CC})
target_compile_definitions(test_assets PRIVATE USE_TEST_ASSETS)
add_dependencies(test_assets generate_test_assets)
set_source_files_properties(src/tests/test_assets.cc PROPERTIES COMPILE_DEFINITIONS "USE_TEST_ASSETS")
diff --git a/assets/final/test_assets_list.txt b/assets/final/test_assets_list.txt
index c22fad8..f0c2275 100644
--- a/assets/final/test_assets_list.txt
+++ b/assets/final/test_assets_list.txt
@@ -1,2 +1,3 @@
-NULL_ASSET, null.bin, NONE, "Empty asset"
-TEST_ASSET, test_asset.txt, NONE, "Test asset for verification"
+# Asset Name, Compression Type, Filename/Placeholder, Description
+TEST_ASSET, NONE, test_asset.txt, "A static test asset"
+PROC_NOISE_256, PROC(gen_noise,256,256), _, "A 256x256 procedural noise texture"
diff --git a/src/tests/test_assets.cc b/src/tests/test_assets.cc
index b7ee8be..dd77b73 100644
--- a/src/tests/test_assets.cc
+++ b/src/tests/test_assets.cc
@@ -47,6 +47,45 @@ int main() {
printf("ASSET_LAST_ID test: SUCCESS\n");
printf("Asset size: %zu bytes\n", size);
+
+ // Test procedural asset
+ printf("\nRunning Procedural Asset test...\n");
+ size_t proc_size = 0;
+ const uint8_t* proc_data_1 = GetAsset(AssetId::ASSET_PROC_NOISE_256, &proc_size);
+ assert(proc_data_1 != nullptr);
+ assert(proc_size == 256 * 256 * 4); // 256x256 RGBA8
+
+ // Verify first few bytes are not all zero (noise should produce non-zero data)
+ bool non_zero_data = false;
+ for (size_t i = 0; i < 16; ++i) { // Check first 16 bytes
+ if (proc_data_1[i] != 0) {
+ non_zero_data = true;
+ break;
+ }
+ }
+ assert(non_zero_data);
+ printf("Procedural asset content verification: SUCCESS\n");
+
+ // Test DropAsset for procedural asset and re-generation
+ DropAsset(AssetId::ASSET_PROC_NOISE_256, proc_data_1);
+ // After dropping, GetAsset should generate new data
+ const uint8_t* proc_data_2 = GetAsset(AssetId::ASSET_PROC_NOISE_256, &proc_size);
+ assert(proc_data_2 != nullptr);
+ // assert(proc_data_1 != proc_data_2); // Removed: Allocator might reuse the same address
+
+ // Verify content again to ensure it was re-generated correctly
+ non_zero_data = false;
+ for (size_t i = 0; i < 16; ++i) {
+ if (proc_data_2[i] != 0) {
+ non_zero_data = true;
+ break;
+ }
+ }
+ assert(non_zero_data);
+ printf("Procedural asset DropAsset and re-generation test: SUCCESS\n");
+
+ printf("Procedural Asset test PASSED\n");
+
printf("AssetManager test PASSED\n");
return 0;
diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc
index 3874535..2ad8ef9 100644
--- a/src/util/asset_manager.cc
+++ b/src/util/asset_manager.cc
@@ -8,9 +8,21 @@
#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 <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 "procedural/generator.h" // For ProcGenFunc and procedural functions
+
+// Map of procedural function names to their pointers (for runtime dispatch)
+static const std::map<std::string, ProcGenFunc> kAssetManagerProcGenFuncMap = {
+ {"gen_noise", procedural::gen_noise},
+ {"gen_grid", procedural::gen_grid},
+ {"make_periodic", procedural::make_periodic},
+};
// These are defined in the generated assets_data.cc
#if defined(USE_TEST_ASSETS)
@@ -47,20 +59,54 @@ 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)
+ // Not in cache, retrieve from static data (packed in binary) or generate procedurally
if (index >= g_assets_count) {
if (out_size)
*out_size = 0;
- return nullptr; // This asset is not in the static packed data either.
+ return nullptr; // Invalid asset_id or asset not in static packed data.
}
- // Store static record in cache for future use
- g_asset_cache[index] = g_assets[index];
- g_asset_cache[index].is_procedural = false;
+ AssetRecord source_record = g_assets[index];
+ AssetRecord cached_record = source_record;
+
+ if (source_record.is_procedural) {
+ // Dynamically generate the asset
+ 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.
+ }
+ 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.
+ 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;
+ }
+ 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
+
+ } else {
+ // Static asset (copy from g_assets)
+ cached_record.is_procedural = false;
+ }
+
+ // Store in cache for future use
+ g_asset_cache[index] = cached_record;
if (out_size)
- *out_size = g_asset_cache[index].size;
- return g_asset_cache[index].data;
+ *out_size = cached_record.size;
+ return cached_record.data;
}
void DropAsset(AssetId asset_id, const uint8_t* asset) {
@@ -69,14 +115,11 @@ void DropAsset(AssetId asset_id, const uint8_t* asset) {
return; // Invalid asset_id
}
- // Only free memory for procedural assets.
- if (g_asset_cache[index].is_procedural && g_asset_cache[index].data == asset) {
- // In a more complex scenario, we might track ref counts.
- // For this demo, we assume a single owner for dynamically allocated assets.
- delete[] g_asset_cache[index].data; // Assuming `new uint8_t[]` was used for procedural
- g_asset_cache[index].data = nullptr;
- g_asset_cache[index].size = 0;
- g_asset_cache[index].is_procedural = false;
+ // 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
}
// For static assets, no dynamic memory to free.
}
diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h
index 6b09430..00aafc0 100644
--- a/src/util/asset_manager.h
+++ b/src/util/asset_manager.h
@@ -8,10 +8,16 @@
enum class AssetId : uint16_t; // Forward declaration
+// Type for procedural generation functions: (buffer, width, height, params, num_params)
+typedef void (*ProcGenFunc)(uint8_t*, int, int, const float*, int);
+
struct AssetRecord {
- const uint8_t* data;
- size_t size;
- bool is_procedural; // Flag to indicate if memory was allocated dynamically
+ const uint8_t* data; // Pointer to asset data (static or dynamic)
+ size_t size; // Size of the asset data
+ bool is_procedural; // True if data was dynamically allocated by a procedural generator
+ const char* proc_func_name_str; // Name of procedural generation function (string literal)
+ const float* proc_params; // Parameters for procedural generation (static, from assets.txt)
+ int num_proc_params; // Number of procedural parameters
};
// Generic interface
diff --git a/tools/asset_packer.cc b/tools/asset_packer.cc
index e606b94..8ae4742 100644
--- a/tools/asset_packer.cc
+++ b/tools/asset_packer.cc
@@ -7,6 +7,32 @@
#include <map>
#include <string>
#include <vector>
+#include <stdexcept> // For std::stof exceptions
+#include <regex> // For std::regex
+
+#include "procedural/generator.h" // For ProcGenFunc and procedural functions
+#include "util/asset_manager.h" // For AssetRecord and AssetId
+
+// Map of procedural function names to their pointers (used only internally by asset_packer here, not generated)
+static const std::map<std::string, ProcGenFunc> kAssetPackerProcGenFuncMap = {
+ {"gen_noise", procedural::gen_noise},
+ {"gen_grid", procedural::gen_grid},
+ {"make_periodic", procedural::make_periodic},
+};
+
+// Helper struct to hold all information about an asset during parsing
+struct AssetBuildInfo {
+ std::string name;
+ std::string filename; // Original filename for static assets
+ bool is_procedural;
+ std::string proc_func_name; // Function name string
+ std::vector<float> proc_params; // Parameters for procedural function
+
+ // For generated C++ code
+ std::string data_array_name; // ASSET_DATA_xxx for static
+ std::string params_array_name; // ASSET_PROC_PARAMS_xxx for procedural
+ std::string func_name_str_name; // ASSET_PROC_FUNC_STR_xxx for procedural
+};
int main(int argc, char* argv[]) {
if (argc != 4) {
@@ -41,97 +67,147 @@ int main(int argc, char* argv[]) {
return 1;
}
- // Generate assets.h
+ // Generate assets.h header
assets_h_file
- << "// This file is auto-generated by asset_packer.cc. Do not edit.\n\n";
- assets_h_file << "#pragma once\n";
- assets_h_file << "#include <cstdint>\n\n";
- assets_h_file << "enum class AssetId : uint16_t {\n";
+ << "// This file is auto-generated by asset_packer.cc. Do not edit.\n\n"
+ << "#pragma once\n"
+ << "#include <cstdint>\n\n"
+ << "enum class AssetId : uint16_t {\n";
+
+ std::string generated_header_name = output_assets_h_path.substr(output_assets_h_path.find_last_of("/\\") + 1);
// Generate assets_data.cc header
assets_data_cc_file
- << "// This file is auto-generated by asset_packer.cc. Do not edit.\n\n";
- assets_data_cc_file << "#include \"util/asset_manager.h\"\n";
- assets_data_cc_file << "#include \"generated/assets.h\"\n\n";
+ << "// This file is auto-generated by asset_packer.cc. Do not edit.\n\n"
+ << "#include \"util/asset_manager.h\"\n"
+ << "#include \"generated/" << generated_header_name << "\"\n\n";
- std::string line;
+ // Forward declare procedural functions for AssetRecord initialization
+ assets_data_cc_file << "namespace procedural { void gen_noise(uint8_t*, int, int, const float*, int); }\n"
+ << "namespace procedural { void gen_grid(uint8_t*, int, int, const float*, int); }\n"
+ << "namespace procedural { void make_periodic(uint8_t*, int, int, const float*, int); }\n\n";
+
+ std::vector<AssetBuildInfo> asset_build_infos;
int asset_id_counter = 0;
- std::vector<std::string> asset_names;
+ std::string line;
+ // Updated regex pattern for new asset list format (Name, CompressionType, Filename, Description)
+ std::regex asset_line_regex(
+ R"(^\s*([A-Z0-9_]+)\s*,\s*(PROC\([^)]*\)|[^,]+)\s*(?:,\s*([^,]*))?\s*(?:,\s*\"(.*)\")?\s*$)");
while (std::getline(assets_txt_file, line)) {
- if (line.empty() || line[0] == '#')
- continue;
-
- size_t first_comma = line.find(',');
- if (first_comma == std::string::npos)
- continue;
+ if (line.empty() || line[0] == '#') continue;
- std::string asset_name = line.substr(0, first_comma);
- asset_name.erase(0, asset_name.find_first_not_of(" \t\r\n"));
- asset_name.erase(asset_name.find_last_not_of(" \t\r\n") + 1);
+ std::smatch matches;
+ if (std::regex_search(line, matches, asset_line_regex) && matches.size() >= 3) {
+ AssetBuildInfo info;
+ info.name = matches[1].str();
+ std::string compression_type_str = matches[2].str();
+ // Filename is now matches[3]
+ info.filename = (matches.size() >= 4 && matches[3].matched) ? matches[3].str() : "_";
- size_t second_comma = line.find(',', first_comma + 1);
- if (second_comma == std::string::npos)
- continue;
+ info.data_array_name = "ASSET_DATA_" + info.name;
+ info.params_array_name = "ASSET_PROC_PARAMS_" + info.name;
+ info.func_name_str_name = "ASSET_PROC_FUNC_STR_" + info.name;
+ info.is_procedural = false;
- std::string filename =
- line.substr(first_comma + 1, second_comma - first_comma - 1);
- filename.erase(0, filename.find_first_not_of(" \t\r\n"));
- filename.erase(filename.find_last_not_of(" \t\r\n") + 1);
+ if (compression_type_str.rfind("PROC(", 0) == 0) {
+ info.is_procedural = true;
+ size_t open_paren = compression_type_str.find('(');
+ size_t close_paren = compression_type_str.rfind(')');
+ if (open_paren == std::string::npos || close_paren == std::string::npos) {
+ std::cerr << "Error: Invalid PROC() syntax for asset: " << info.name << ", string: [" << compression_type_str << "]\n";
+ return 1;
+ }
+ std::string func_and_params_str = compression_type_str.substr(open_paren + 1, close_paren - open_paren - 1);
+
+ size_t params_start = func_and_params_str.find(',');
+ if (params_start != std::string::npos) {
+ std::string params_str = func_and_params_str.substr(params_start + 1);
+ info.proc_func_name = func_and_params_str.substr(0, params_start);
+
+ size_t current_pos = 0;
+ while (current_pos < params_str.length()) {
+ size_t comma_pos = params_str.find(',', current_pos);
+ std::string param_val_str = (comma_pos == std::string::npos) ? params_str.substr(current_pos) : params_str.substr(current_pos, comma_pos - current_pos);
+ param_val_str.erase(0, param_val_str.find_first_not_of(" \t\r\n"));
+ param_val_str.erase(param_val_str.find_last_not_of(" \t\r\n") + 1);
+ try {
+ info.proc_params.push_back(std::stof(param_val_str));
+ } catch (...) {
+ std::cerr << "Error: Invalid proc param for " << info.name << ": " << param_val_str << "\n";
+ return 1;
+ }
+ if (comma_pos == std::string::npos) break;
+ current_pos = comma_pos + 1;
+ }
+ } else {
+ info.proc_func_name = func_and_params_str;
+ }
- std::string base_dir =
- assets_txt_path.substr(0, assets_txt_path.find_last_of("/\\") + 1);
- std::ifstream asset_file(base_dir + filename, std::ios::binary);
- if (!asset_file.is_open()) {
- std::cerr << "Error: Could not open asset file: " << base_dir + filename
- << "\n";
- return 1;
+ // Validate procedural function name
+ // kAssetPackerProcGenFuncMap is defined globally for validation
+ if (kAssetPackerProcGenFuncMap.find(info.proc_func_name) == kAssetPackerProcGenFuncMap.end()) {
+ std::cerr << "Error: Unknown procedural function: " << info.proc_func_name << " for asset: " << info.name << "\n";
+ return 1;
+ }
+ }
+
+ asset_build_infos.push_back(info);
+ assets_h_file << " ASSET_" << info.name << " = " << asset_id_counter++ << ",\n";
+ } else {
+ std::cerr << "Warning: Skipping malformed line in assets.txt: " << line << "\n";
}
+ }
- std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(asset_file)),
- std::istreambuf_iterator<char>());
- asset_file.close();
-
- asset_names.push_back(asset_name);
-
- // Add to assets.h enum
- assets_h_file << " ASSET_" << asset_name << " = " << asset_id_counter
- << ",\n";
+ assets_h_file << " ASSET_LAST_ID = " << asset_id_counter << ",\n";
+ assets_h_file << "};\n";
- // Write data to assets_data.cc
- assets_data_cc_file << "static const uint8_t ASSET_DATA_" << asset_name
- << "[] = {";
- for (size_t i = 0; i < buffer.size(); ++i) {
- if (i % 12 == 0)
- assets_data_cc_file << "\n ";
- assets_data_cc_file << "0x" << std::hex << (int)buffer[i] << std::dec
- << (i == buffer.size() - 1 ? "" : ", ");
- }
- assets_data_cc_file << "\n};\n\n";
+ assets_h_file << "#include \"util/asset_manager.h\"\n"; // Include here AFTER enum definition
+ assets_h_file.close();
- ++asset_id_counter;
+ for (const auto& info : asset_build_infos) {
+ if (!info.is_procedural) {
+ std::string base_dir = assets_txt_path.substr(0, assets_txt_path.find_last_of("/\\") + 1);
+ std::ifstream asset_file(base_dir + info.filename, std::ios::binary);
+ if (!asset_file.is_open()) {
+ std::cerr << "Error: Could not open asset file: " << base_dir + info.filename << "\n";
+ return 1;
+ }
+ std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(asset_file)), std::istreambuf_iterator<char>());
+ assets_data_cc_file << "static const uint8_t " << info.data_array_name << "[] = {\n ";
+ for (size_t i = 0; i < buffer.size(); ++i) {
+ if (i > 0 && i % 12 == 0) assets_data_cc_file << "\n ";
+ assets_data_cc_file << "0x" << std::hex << (int)buffer[i] << std::dec << (i == buffer.size() - 1 ? "" : ", ");
+ }
+ assets_data_cc_file << "\n};\n";
+ } else {
+ assets_data_cc_file << "static const float " << info.params_array_name << "[] = {";
+ for (size_t i = 0; i < info.proc_params.size(); ++i) {
+ if (i > 0) assets_data_cc_file << ", ";
+ assets_data_cc_file << info.proc_params[i];
+ }
+ assets_data_cc_file << "};\n\n";
+ assets_data_cc_file << "static const char* " << info.func_name_str_name << " = \"" << info.proc_func_name << "\";\n\n";
+ }
}
- // Add ASSET_LAST_ID at the end
- assets_h_file << " ASSET_LAST_ID = " << asset_id_counter << "\n";
- assets_h_file << "};\n\n";
- assets_h_file << "#include \"util/asset_manager.h\"\n";
- assets_h_file.close();
-
- // Generate the lookup array in assets_data.cc
assets_data_cc_file << "extern const AssetRecord g_assets[] = {\n";
- for (const std::string& name : asset_names) {
- assets_data_cc_file << " { ASSET_DATA_" << name << ", sizeof(ASSET_DATA_"
- << name << ") },\n";
+ for (const auto& info : asset_build_infos) {
+ assets_data_cc_file << " {";
+ if (info.is_procedural) {
+ assets_data_cc_file << " nullptr, 0, true, " << info.func_name_str_name << ", " << info.params_array_name << ", " << info.proc_params.size();
+ } else {
+ assets_data_cc_file << " " << info.data_array_name << ", sizeof(" << info.data_array_name << "), false, nullptr, nullptr, 0";
+ }
+ assets_data_cc_file << " },\n";
}
- assets_data_cc_file << "};\n\n";
- assets_data_cc_file << "extern const size_t g_assets_count = "
- "sizeof(g_assets) / sizeof(g_assets[0]);\n";
+ assets_data_cc_file << "};\n";
+ assets_data_cc_file << "extern const size_t g_assets_count = sizeof(g_assets) / sizeof(g_assets[0]);\n";
assets_data_cc_file.close();
-
+
std::cout << "Asset packer successfully generated records for "
- << asset_names.size() << " assets.\n";
+ << asset_build_infos.size() << " assets.\n";
return 0;
}