add code generation for assets

This commit is contained in:
matthew 2025-07-08 07:52:53 +10:00
parent 2a17bdcbf8
commit ad166e742d
20 changed files with 190 additions and 2 deletions

BIN
assets/models/test_char.m3d Normal file

Binary file not shown.

BIN
assets/models/yoda.m3d Normal file

Binary file not shown.

BIN
assets/shaders/gui.frag.spv Normal file

Binary file not shown.

BIN
assets/shaders/gui.vert.spv Normal file

Binary file not shown.

BIN
assets/shaders/pbr.frag.spv Normal file

Binary file not shown.

BIN
assets/shaders/pbr.vert.spv Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 728 KiB

BIN
assets/textures/hamster.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 961 KiB

BIN
assets/textures/hog.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

BIN
assets/textures/patamon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 KiB

View File

@ -24,6 +24,19 @@
"importPaths": ["src/packer", "src/shared"], "importPaths": ["src/packer", "src/shared"],
"sourcePaths": ["src/packer", "src/shared"], "sourcePaths": ["src/packer", "src/shared"],
"sourceFiles-linux": ["build/libxxhash.a"], "sourceFiles-linux": ["build/libxxhash.a"],
"preGenerateCommands-linux": ["./build-vma.sh"],
"preGenerateCommands-windows": [],
},
{
"name": "codegen",
"targetType": "executable",
"targetPath": "build",
"targetName": "Codegen",
"importPaths": ["src/codegen", "src/shared"],
"sourcePaths": ["src/codegen", "src/shared"],
"sourceFiles-linux": ["build/libxxhash.a"],
"preGenerateCommands-linux": ["./build-vma.sh"],
"preGenerateCommands-windows": [],
} }
] ]
} }

75
src/codegen/codegen.d Normal file
View File

@ -0,0 +1,75 @@
import aliases;
import util;
import std.stdio;
import std.file;
import std.string;
import std.path;
void
main()
{
CreateAssetLookups();
}
void
CreateAssetLookups()
{
try
{
mkdir("src/generated");
}
catch(Exception e) {}
string[] dirs;
string[][string] file_names;
string[][string] base_names;
u64[][string] file_hashes;
foreach(string dir; dirEntries("assets", SpanMode.shallow))
{
dirs ~= baseName(dir);
}
foreach(dir; dirs)
{
string[] files;
string[] names;
foreach(string file; dirEntries("assets/" ~ dir, SpanMode.shallow))
{
files ~= dir ~ "/" ~ baseName(file);
names ~= dir ~ "/" ~ baseName(stripExtension(file));
}
file_names[dir] = files;
base_names[dir] = names;
}
foreach(dir, names; base_names)
{
u64[] hashes;
foreach(name; names)
{
hashes ~= Hash(name);
}
file_hashes[dir] = hashes;
}
auto f = File("src/generated/assets.d", "w");
foreach(dir; dirs)
{
string asset_type = stripRight(dir, "s").toUpper();
string file_array = format("static immutable string[] %s_FILES = [\n", asset_type);
foreach(file; file_names[dir])
{
file_array ~= format("\t\"%s\",\n", file);
}
file_array ~= "];\n\n";
f.write(file_array);
}
f.close();
}

24
src/generated/codegen.d Normal file
View File

@ -0,0 +1,24 @@
static immutable string[] MODEL_FILES = [
"models/test_char.m3d",
"models/yoda.m3d",
];
static immutable string[] SHADER_FILES = [
"shaders/gui.frag.spv",
"shaders/pbr.frag.spv",
"shaders/gui.vert.spv",
"shaders/quad.frag.spv",
"shaders/quad.vert.spv",
"shaders/pbr.vert.spv",
];
static immutable string[] TEXTURE_FILES = [
"textures/ham_smoke.png",
"textures/cheesoid.png",
"textures/hog.jpg",
"textures/patamon.png",
"textures/pattermon.png",
"textures/hamster.png",
"textures/purplemon.png",
];

View File

@ -1,7 +1,38 @@
import std.stdio; import aliases;
import std.file;
import util; import util;
import assets;
const u32 FILE_VERSION = 1;
void main() void main()
{ {
Log("Hello, World!"); if (isDir("build"))
{
chdir("build");
}
static foreach(file; AssetFiles)
{
Log(file);
}
} }
u32
MagicValue(string str)
{
assert(str.length == 4, "Magic value must 4 characters");
return cast(u32)(cast(u32)(str[0] << 24) | cast(u32)(str[1] << 16) | cast(u32)(str[2] << 8) | cast(u32)(str[3] << 0));
}
FileHeader
InitHeader()
{
FileHeader header = {
magic: MagicValue("steg"),
file_version: FILE_VERSION,
};
return header;
}

View File

@ -0,0 +1,45 @@
import aliases;
import std.file;
struct FileHeader
{
u32 magic;
u32 file_version;
u64 asset_counts;
u64 asset_offset;
}
struct ModelMeta
{
u64 index_count;
}
struct TexMeta
{
u32 w;
u32 h;
u32 ch;
}
enum AssetType : u32
{
None,
Shader,
Texture,
Model,
}
alias AT = AssetType;
struct AssetInfo
{
union
{
ModelMeta model;
TexMeta texture;
};
u64 hash;
u64 offset;
u64 len;
AssetType type;
}