Add basisu texture compressor to assetc, improve build process for asset manifest, should cache faster now
This commit is contained in:
parent
2f3998ddfd
commit
ad3210a08f
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
zig-out
|
||||
zig-cache
|
||||
dbg.rdbg
|
||||
assetc.rdbg
|
||||
.vs
|
||||
|
101
build.zig
101
build.zig
@ -22,6 +22,8 @@ pub fn build(b: *Build) void {
|
||||
"Prioritize performance, safety, or binary size for build time tools",
|
||||
) orelse .Debug;
|
||||
|
||||
const zalgebra_dep = b.dependency("zalgebra", .{});
|
||||
|
||||
const assets_mod = b.addModule("assets", .{ .root_source_file = .{ .path = "src/assets/root.zig" } });
|
||||
const asset_manifest_mod = b.addModule("asset_manifest", .{ .root_source_file = .{ .path = "src/gen/asset_manifest.zig" } });
|
||||
asset_manifest_mod.addImport("assets", assets_mod);
|
||||
@ -29,16 +31,18 @@ pub fn build(b: *Build) void {
|
||||
const assets_step = b.step("assets", "Build and install assets");
|
||||
b.getInstallStep().dependOn(assets_step);
|
||||
|
||||
const assetc = buildAssetCompiler(b, buildOptimize);
|
||||
const assetc = buildAssetCompiler(b, assets_step, buildOptimize);
|
||||
|
||||
assetc.root_module.addImport("assets", assets_mod);
|
||||
assetc.root_module.addImport("asset_manifest", asset_manifest_mod);
|
||||
|
||||
buildAssets(b, assets_step, assetc, "assets") catch |err| {
|
||||
const gen_asset_manifest = buildAssets(b, assets_step, assetc, "assets") catch |err| {
|
||||
std.log.err("Failed to build assets {}\n", .{err});
|
||||
@panic("buildAssets");
|
||||
};
|
||||
|
||||
const zalgebra_dep = b.dependency("zalgebra", .{});
|
||||
const gen_asset_manifest_mod = b.createModule(.{ .root_source_file = gen_asset_manifest });
|
||||
gen_asset_manifest_mod.addImport("assets", assets_mod);
|
||||
asset_manifest_mod.addImport("asset_manifest_gen", gen_asset_manifest_mod);
|
||||
|
||||
const lib = b.addSharedLibrary(.{
|
||||
.name = "learnopengl",
|
||||
@ -53,7 +57,7 @@ pub fn build(b: *Build) void {
|
||||
});
|
||||
const lib_compiles = [_]*Step.Compile{ lib, lib_unit_tests };
|
||||
|
||||
for (lib_compiles) |l| {
|
||||
inline for (lib_compiles) |l| {
|
||||
l.root_module.addImport("zalgebra", zalgebra_dep.module("zalgebra"));
|
||||
l.root_module.addImport("assets", assets_mod);
|
||||
l.root_module.addImport("asset_manifest", asset_manifest_mod);
|
||||
@ -73,21 +77,21 @@ pub fn build(b: *Build) void {
|
||||
|
||||
if (b.systemIntegrationOption("SDL2", .{ .default = b.host.result.os.tag != .windows })) {
|
||||
exe.linkSystemLibrary("SDL2");
|
||||
for (lib_compiles) |l| {
|
||||
inline for (lib_compiles) |l| {
|
||||
l.linkSystemLibrary("SDL2");
|
||||
}
|
||||
} else {
|
||||
if (b.lazyDependency("SDL", .{
|
||||
const sdl_dep = b.dependency("SDL", .{
|
||||
.target = target,
|
||||
.optimize = .ReleaseSafe,
|
||||
})) |sdl_dep| {
|
||||
const sdl2 = sdl_dep.artifact("SDL2");
|
||||
b.getInstallStep().dependOn(&b.addInstallArtifact(sdl2, .{ .dest_dir = .{ .override = .prefix } }).step);
|
||||
exe.linkLibrary(sdl2);
|
||||
});
|
||||
|
||||
for (lib_compiles) |l| {
|
||||
l.linkLibrary(sdl2);
|
||||
}
|
||||
const sdl2 = sdl_dep.artifact("SDL2");
|
||||
b.getInstallStep().dependOn(&b.addInstallArtifact(sdl2, .{ .dest_dir = .{ .override = .prefix } }).step);
|
||||
exe.linkLibrary(sdl2);
|
||||
|
||||
inline for (lib_compiles) |l| {
|
||||
l.linkLibrary(sdl2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,7 +176,7 @@ const NestedAssetDef = union(enum) {
|
||||
};
|
||||
|
||||
// Find all assets and cook them using assetc
|
||||
fn buildAssets(b: *std.Build, step: *Step, assetc: *Step.Compile, path: []const u8) !void {
|
||||
fn buildAssets(b: *std.Build, step: *Step, assetc: *Step.Compile, path: []const u8) !Build.LazyPath {
|
||||
const assetsPath = b.pathFromRoot(path);
|
||||
defer b.allocator.free(assetsPath);
|
||||
var assetsDir = try std.fs.openDirAbsolute(assetsPath, .{ .iterate = true });
|
||||
@ -182,6 +186,7 @@ fn buildAssets(b: *std.Build, step: *Step, assetc: *Step.Compile, path: []const
|
||||
var meshes = NestedAssetDef{ .path = .{} };
|
||||
var shaders = NestedAssetDef{ .path = .{} };
|
||||
var shader_programs = NestedAssetDef{ .path = .{} };
|
||||
var textures = NestedAssetDef{ .path = .{} };
|
||||
var asset_paths = std.ArrayList([]const u8).init(b.allocator);
|
||||
|
||||
var walker = try assetsDir.walk(b.allocator);
|
||||
@ -258,11 +263,40 @@ fn buildAssets(b: *std.Build, step: *Step, assetc: *Step.Compile, path: []const
|
||||
try asset_paths.append(out_path);
|
||||
}
|
||||
}
|
||||
|
||||
if (std.mem.endsWith(u8, entry.basename, ".png")) {
|
||||
const run_assetc = b.addRunArtifact(assetc);
|
||||
run_assetc.addFileArg(.{ .path = b.pathJoin(&.{ path, entry.path }) });
|
||||
const out_name = try std.mem.concat(
|
||||
b.allocator,
|
||||
u8,
|
||||
&.{ std.fs.path.stem(entry.basename), ".bu" }, // basisu
|
||||
);
|
||||
const compiled_file = run_assetc.addOutputFileArg(out_name);
|
||||
|
||||
const out_path = b.pathJoin(&.{
|
||||
path,
|
||||
std.fs.path.dirname(entry.path) orelse "",
|
||||
out_name,
|
||||
});
|
||||
const install_asset = b.addInstallFileWithDir(
|
||||
compiled_file,
|
||||
.prefix,
|
||||
out_path,
|
||||
);
|
||||
step.dependOn(&install_asset.step);
|
||||
|
||||
{
|
||||
const id = asset_id;
|
||||
asset_id += 1;
|
||||
try textures.put(b.allocator, out_path, id);
|
||||
try asset_paths.append(out_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const manifest_step = try writeAssetManifest(b, step, asset_paths.items, &meshes, &shaders, &shader_programs);
|
||||
|
||||
assetc.step.dependOn(&manifest_step.step);
|
||||
const manifest_path = try writeAssetManifest(b, asset_paths.items, &meshes, &shaders, &shader_programs, &textures);
|
||||
return manifest_path;
|
||||
}
|
||||
|
||||
fn writeNestedAssetDef(writer: anytype, handle: []const u8, name: []const u8, asset_def: *NestedAssetDef, indent: usize) !void {
|
||||
@ -287,12 +321,12 @@ fn writeNestedAssetDef(writer: anytype, handle: []const u8, name: []const u8, as
|
||||
|
||||
fn writeAssetManifest(
|
||||
b: *Build,
|
||||
asset_step: *Step,
|
||||
asset_paths: [][]const u8,
|
||||
meshes: *NestedAssetDef,
|
||||
shaders: *NestedAssetDef,
|
||||
shader_programs: *NestedAssetDef,
|
||||
) !*Step.WriteFile {
|
||||
textures: *NestedAssetDef,
|
||||
) !Build.LazyPath {
|
||||
var mesh_asset_manifest = std.ArrayList(u8).init(b.allocator);
|
||||
const writer = mesh_asset_manifest.writer();
|
||||
|
||||
@ -307,6 +341,8 @@ fn writeAssetManifest(
|
||||
try writer.writeByte('\n');
|
||||
try writeNestedAssetDef(writer, "ShaderProgram", "ShaderPrograms", shader_programs, 0);
|
||||
try writer.writeByte('\n');
|
||||
try writeNestedAssetDef(writer, "Texture", "Textures", textures, 0);
|
||||
try writer.writeByte('\n');
|
||||
|
||||
try writer.writeAll("pub const asset_paths = [_][]const u8{\n");
|
||||
for (asset_paths) |path| {
|
||||
@ -322,20 +358,26 @@ fn writeAssetManifest(
|
||||
|
||||
const result = mesh_asset_manifest.toOwnedSlice() catch @panic("OOM");
|
||||
const write_step = b.addWriteFiles();
|
||||
write_step.addBytesToSource(result, "src/gen/asset_manifest.gen.zig");
|
||||
asset_step.dependOn(&write_step.step);
|
||||
return write_step;
|
||||
const manifest_path = write_step.add("asset_manifest.gen.zig", result);
|
||||
return manifest_path;
|
||||
}
|
||||
|
||||
fn buildAssetCompiler(b: *Build, optimize: std.builtin.OptimizeMode) *Step.Compile {
|
||||
fn buildAssetCompiler(b: *Build, assets_step: *Step, optimize: std.builtin.OptimizeMode) *Step.Compile {
|
||||
const assimp_dep = b.dependency("zig-assimp", .{
|
||||
.target = b.host,
|
||||
.optimize = optimize,
|
||||
//.formats = @as([]const u8, "3DS,3MF,AC,AMF,ASE,Assbin,Assjson,Assxml,B3D,Blender,BVH,C4D,COB,Collada,CSM,DXF,FBX,glTF,glTF2,HMP,IFC,Irr,LWO,LWS,M3D,MD2,MD3,MD5,MDC,MDL,MMD,MS3D,NDO,NFF,Obj,OFF,Ogre,OpenGEX,Ply,Q3BSP,Q3D,Raw,SIB,SMD,Step,STEPParser,STL,Terragen,Unreal,X,X3D,XGL"),
|
||||
.formats = @as([]const u8, "Obj"),
|
||||
});
|
||||
//const assimp = assimp_dep.builder.dependency("assimp", .{});
|
||||
|
||||
const basisu_optimize = b.option(std.builtin.OptimizeMode, "basisu_optimize", "Optimization level for basisu. ReleaseSafe or faster is recommented, otherwise it's unbearable.") orelse .ReleaseFast;
|
||||
const basisu_dep = b.dependency("mach-basisu", .{
|
||||
.target = b.host,
|
||||
.optimize = basisu_optimize,
|
||||
});
|
||||
|
||||
const assimp_lib = assimp_dep.artifact("assimp");
|
||||
const basisu_lib = basisu_dep.artifact("mach-basisu");
|
||||
|
||||
const assetc = b.addExecutable(.{
|
||||
.name = "assetc",
|
||||
@ -345,10 +387,17 @@ fn buildAssetCompiler(b: *Build, optimize: std.builtin.OptimizeMode) *Step.Compi
|
||||
});
|
||||
|
||||
assetc.root_module.addAnonymousImport("formats", .{ .root_source_file = .{ .path = "src/formats.zig" } });
|
||||
assetc.root_module.addImport("mach-basisu", basisu_dep.module("mach-basisu"));
|
||||
|
||||
assetc.linkLibrary(assimp_lib);
|
||||
assetc.linkLibrary(basisu_lib);
|
||||
assetc.linkLibC();
|
||||
assetc.linkLibCpp();
|
||||
b.installArtifact(assetc);
|
||||
|
||||
assetc.addCSourceFile(.{ .file = .{ .path = "tools/stb/stb_image.c" }, .flags = &.{"-std=c99"} });
|
||||
assetc.addIncludePath(.{ .path = "tools/stb" });
|
||||
|
||||
const install_step = b.addInstallArtifact(assetc, .{ .dest_dir = .{ .override = .prefix } });
|
||||
assets_step.dependOn(&install_step.step);
|
||||
return assetc;
|
||||
}
|
||||
|
@ -18,7 +18,6 @@
|
||||
.SDL = .{
|
||||
.url = "https://github.com/sergeypdev/SDL/tarball/fc1f821736ae929ef92d3d205cecd777b878e100",
|
||||
.hash = "1220483cbb42231cb056f4ea6669894c68ccd560d3af5832d6e9c84c61844bc20b7d",
|
||||
.lazy = true,
|
||||
},
|
||||
.@"zig-assimp" = .{
|
||||
.url = "https://github.com/sergeypdev/zig-assimp/tarball/64b31e9c01132352246955f496642332cb611577",
|
||||
@ -28,6 +27,10 @@
|
||||
.url = "git+https://github.com/sergeypdev/zalgebra.git#232ff76712dc7cc270b6c48cedc84617536f3a59",
|
||||
.hash = "12206e29e5d0f012c694f413b21cb66238964fdaef0a29781e0bf3ff75ec08a2ed78",
|
||||
},
|
||||
.@"mach-basisu" = .{
|
||||
.url = "https://github.com/hexops/mach-basisu/tarball/5a68105a5e503c7183aab481b7ec6ae16b996943",
|
||||
.hash = "12204098c8d8d15a687068a3c0b15fe95d20f8fe074531e34e78f9dfe518cf86a67c",
|
||||
},
|
||||
},
|
||||
.paths = .{
|
||||
// This makes *all* files, recursively, included in this package. It is generally
|
||||
|
@ -1 +0,0 @@
|
||||
pub const std = @import("std");
|
@ -4,4 +4,5 @@ pub const Handle = struct {
|
||||
pub const Shader = extern struct { id: AssetId = 0 };
|
||||
pub const ShaderProgram = extern struct { id: AssetId = 0 };
|
||||
pub const Mesh = struct { id: AssetId = 0 };
|
||||
pub const Texture = struct { id: AssetId = 0 };
|
||||
};
|
||||
|
11
src/game.zig
11
src/game.zig
@ -227,10 +227,10 @@ var g_mem: *GameMemory = undefined;
|
||||
var g_assetman: *AssetManager = undefined;
|
||||
|
||||
fn game_init_window_err(global_allocator: std.mem.Allocator) !void {
|
||||
if (c.SDL_SetHint(c.SDL_HINT_WINDOWS_DPI_AWARENESS_, "1") == c.SDL_FALSE) {
|
||||
if (c.SDL_SetHint(c.SDL_HINT_WINDOWS_DPI_AWARENESS_, "permonitorv2") == c.SDL_FALSE) {
|
||||
std.log.debug("Failed to setup windows DPI scaling\n", .{});
|
||||
}
|
||||
if (c.SDL_SetHint(c.SDL_HINT_WINDOWS_DPI_SCALING, "1") == c.SDL_FALSE) {
|
||||
if (c.SDL_SetHint(c.SDL_HINT_WINDOWS_DPI_SCALING_, "1") == c.SDL_FALSE) {
|
||||
std.log.debug("Failed to setup windows DPI scaling\n", .{});
|
||||
}
|
||||
// _ = DwmEnableMMCSS(1);
|
||||
@ -540,7 +540,9 @@ export fn game_update() bool {
|
||||
},
|
||||
c.SDL_SCANCODE_ESCAPE => {
|
||||
if (event.type == c.SDL_KEYUP) {
|
||||
if (gmem.mouse_focus) {
|
||||
if (ginit.fullscreen) {
|
||||
toggleFullScreen() catch continue;
|
||||
} else if (gmem.mouse_focus) {
|
||||
_ = c.SDL_SetRelativeMouseMode(c.SDL_FALSE);
|
||||
gmem.mouse_focus = false;
|
||||
} else {
|
||||
@ -845,4 +847,7 @@ fn toggleFullScreen() !void {
|
||||
}
|
||||
|
||||
ginit.fullscreen = !ginit.fullscreen;
|
||||
|
||||
// c.SDL_GL_GetDrawableSize(ginit.window, &ginit.width, &ginit.height);
|
||||
// gl.viewport(0, 0, ginit.width, ginit.height);
|
||||
}
|
||||
|
@ -1,35 +0,0 @@
|
||||
// Generated file, do not edit manually!
|
||||
|
||||
const std = @import("std");
|
||||
const Handle = @import("assets").Handle;
|
||||
|
||||
pub const Meshes = struct {
|
||||
pub const bunny = Handle.Mesh{ .id = 1 };
|
||||
pub const plane = Handle.Mesh{ .id = 2 };
|
||||
pub const sphere = Handle.Mesh{ .id = 5 };
|
||||
};
|
||||
|
||||
pub const Shaders = struct {
|
||||
pub const mesh = Handle.Shader{ .id = 3 };
|
||||
};
|
||||
|
||||
pub const ShaderPrograms = struct {
|
||||
pub const mesh = Handle.ShaderProgram{ .id = 4 };
|
||||
};
|
||||
|
||||
pub const asset_paths = [_][]const u8{
|
||||
"assets\\bunny.mesh",
|
||||
"assets\\plane.mesh",
|
||||
"assets\\shaders\\mesh.glsl",
|
||||
"assets\\shaders\\mesh.prog",
|
||||
"assets\\sphere.mesh",
|
||||
};
|
||||
|
||||
pub const asset_path_to_asset_id = std.ComptimeStringMap(u32, .{
|
||||
.{ "assets\\bunny.mesh", 1 },
|
||||
.{ "assets\\plane.mesh", 2 },
|
||||
.{ "assets\\shaders\\mesh.glsl", 3 },
|
||||
.{ "assets\\shaders\\mesh.prog", 4 },
|
||||
.{ "assets\\sphere.mesh", 5 },
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
pub const manifest = @import("asset_manifest.gen.zig");
|
||||
pub const manifest = @import("asset_manifest_gen");
|
||||
|
||||
pub const Meshes = manifest.Meshes;
|
||||
pub const Shaders = manifest.Shaders;
|
||||
|
@ -5,3 +5,4 @@ pub usingnamespace @cImport({
|
||||
|
||||
// When using system sdl2 library this might not be defined
|
||||
pub const SDL_HINT_WINDOWS_DPI_AWARENESS_ = "SDL_WINDOWS_DPI_AWARENESS";
|
||||
pub const SDL_HINT_WINDOWS_DPI_SCALING_ = "SDL_WINDOWS_DPI_SCALING";
|
||||
|
@ -8,7 +8,10 @@ const c = @cImport({
|
||||
@cInclude("assimp/scene.h");
|
||||
@cInclude("assimp/mesh.h");
|
||||
@cInclude("assimp/postprocess.h");
|
||||
|
||||
@cInclude("stb_image.h");
|
||||
});
|
||||
const basisu = @import("mach-basisu");
|
||||
|
||||
const ASSET_MAX_BYTES = 1024 * 1024 * 1024;
|
||||
|
||||
@ -16,6 +19,7 @@ const AssetType = enum {
|
||||
Mesh,
|
||||
Shader,
|
||||
ShaderProgram,
|
||||
Texture,
|
||||
};
|
||||
|
||||
pub fn resolveAssetTypeByExtension(path: []const u8) ?AssetType {
|
||||
@ -28,6 +32,9 @@ pub fn resolveAssetTypeByExtension(path: []const u8) ?AssetType {
|
||||
if (std.mem.endsWith(u8, path, ".glsl")) {
|
||||
return .Shader;
|
||||
}
|
||||
if (std.mem.endsWith(u8, path, ".png")) {
|
||||
return .Texture;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -47,7 +54,8 @@ pub fn main() !void {
|
||||
switch (asset_type) {
|
||||
.Mesh => try processMesh(allocator, input, output),
|
||||
.ShaderProgram => try processShaderProgram(allocator, std.mem.span(input), output),
|
||||
else => return error.DontProcessShaders,
|
||||
.Texture => try processTexture(allocator, input, output),
|
||||
else => return error.CantProcessAssetType,
|
||||
}
|
||||
}
|
||||
|
||||
@ -166,3 +174,41 @@ fn processShaderProgram(allocator: std.mem.Allocator, absolute_input: []const u8
|
||||
try formats.writeShaderProgram(buf_writer.writer(), shader_asset_id, program.value.vertex, program.value.fragment, formats.native_endian);
|
||||
try buf_writer.flush();
|
||||
}
|
||||
|
||||
fn processTexture(allocator: std.mem.Allocator, input: [*:0]const u8, output: []const u8) !void {
|
||||
_ = allocator; // autofix
|
||||
var x: c_int = undefined;
|
||||
var y: c_int = undefined;
|
||||
var comps: c_int = undefined;
|
||||
|
||||
const FORCED_COMPONENTS = 3; // force rgb
|
||||
const data_c = @as(?[*]u8, @ptrCast(c.stbi_load(input, &x, &y, &comps, FORCED_COMPONENTS))) orelse return error.ImageLoadError;
|
||||
defer c.stbi_image_free(data_c);
|
||||
|
||||
const data = data_c[0 .. @as(usize, @intCast(x)) * @as(usize, @intCast(y)) * FORCED_COMPONENTS];
|
||||
|
||||
basisu.init_encoder();
|
||||
|
||||
var params = basisu.CompressorParams.init(@intCast(try std.Thread.getCpuCount()));
|
||||
defer params.deinit();
|
||||
|
||||
const img = params.getImageSource(0);
|
||||
img.fill(data, @intCast(x), @intCast(y), @intCast(FORCED_COMPONENTS));
|
||||
|
||||
params.setQualityLevel(64);
|
||||
params.setBasisFormat(basisu.BasisTextureFormat.etc1s);
|
||||
// params.setColorSpace(basisu.ColorSpace.linear);
|
||||
params.setGenerateMipMaps(true);
|
||||
|
||||
var compressor = try basisu.Compressor.init(params);
|
||||
defer compressor.deinit();
|
||||
|
||||
try compressor.process();
|
||||
|
||||
const out_file = try std.fs.createFileAbsolute(output, .{});
|
||||
defer out_file.close();
|
||||
var buf_writer = std.io.bufferedWriter(out_file.writer());
|
||||
|
||||
try buf_writer.writer().writeAll(compressor.output());
|
||||
try buf_writer.flush();
|
||||
}
|
||||
|
2
tools/stb/stb_image.c
Normal file
2
tools/stb/stb_image.c
Normal file
@ -0,0 +1,2 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
7985
tools/stb/stb_image.h
Normal file
7985
tools/stb/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user