Skip to content

Commit

Permalink
BREAKING: Remove experimental static shaderc support and replace it w…
Browse files Browse the repository at this point in the history
…ith shaderc as a child process. (#4)

Remove experimental static shaderc support and replace it with shaderc
as a child process.
  • Loading branch information
OndraVoves authored Oct 9, 2024
1 parent f1df5de commit 68a85a7
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 357 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ When [zig](https://github.com/ziglang/zig) meets [bgfx](https://github.com/bkara
- [x] `shaderc` as build artifact.
- [x] Shader compile in `build.zig` to `*.bin.h`.
- [x] Shader compile in `build.zig` and embed as zig module. (this is zig equivalent of `*.bin.h`)
- [ ] Shader compile from runtime via `shaderc` as child process.
- [x] Experimental shader compile from runtime via embeded `shaderc`.
- [x] Shader compile from runtime via `shaderc` as child process.
- [x] Binding for [DebugDraw API](https://github.com/bkaradzic/bgfx/tree/master/examples/common/debugdraw)
- [x] `imgui` render backend. Use build option `imgui_include` to enable. ex. for
zgui: `.imgui_include = zgui.path("libs").getPath(b),`
- [ ] Zig based allocator.

> [!IMPORTANT]
> This is only zig binding. For BGFX stuff goto [bgfx](https://github.com/bkaradzic/bgfx).
Expand Down Expand Up @@ -47,12 +47,14 @@ Minimal is `0.14.0-dev.1710+8ee52f99c`. But you know try your version and believ

## Getting started

Copy `zbgfx` to a subdirectory of your project and add the following to your `build.zig.zon` .dependencies:
Copy `zbgfx` to a subdirectory of your project and then add the following to your `build.zig.zon` .dependencies:

```zig
.zbgfx = .{ .path = "path/to/zbgfx" },
```

or use `zig fetch --save ...` way.

Then in your `build.zig` add:

```zig
Expand Down
59 changes: 22 additions & 37 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -200,31 +200,16 @@ pub fn build(b: *std.Build) !void {
defer shaderc_variant.deinit();

if (options.with_shaderc) {
//
// Static shaderc
//
const shaderc_static = b.addStaticLibrary(.{
.name = "shaderc-static",
.target = target,
.optimize = optimize,
});
shaderc_static.defineCMacro("ZBGFX_EMBED_SHADERC", null);
shaderc_static.defineCMacro("bgfx", "bgfx_shader"); // Tricky&Dirty (without this is problem with duplicate symbols).
b.installArtifact(shaderc_static);
shaderc_static.linkLibCpp();

//
// Shaderc executable
//
const shaderc = b.addExecutable(.{
.name = "shaderc",
.root_source_file = b.path("src/shaderc_main.zig"),
.target = target,
.optimize = optimize,
});

b.installArtifact(shaderc);
shaderc.linkLibrary(shaderc_static);

if (target.result.os.tag.isDarwin()) {
shaderc.linkFramework("CoreFoundation");
Expand All @@ -233,23 +218,23 @@ pub fn build(b: *std.Build) !void {
shaderc.linkLibrary(bx);
shaderc.linkLibCpp();

bxInclude(b, shaderc_static, target, optimize);

shaderc_static.addIncludePath(b.path("libs/bimg/include"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "include"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "src"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/dxsdk/include"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/fcpp"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/glslang/glslang/Public"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/glslang/glslang/Include"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/glslang"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/glsl-optimizer/include"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/glsl-optimizer/src/glsl"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/spirv-cross"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/spirv-tools/include"));
shaderc_static.addIncludePath(b.path(bgfx_path ++ "3rdparty/webgpu/include"));

shaderc_static.addCSourceFiles(.{
bxInclude(b, shaderc, target, optimize);

shaderc.addIncludePath(b.path("libs/bimg/include"));
shaderc.addIncludePath(b.path(bgfx_path ++ "include"));
shaderc.addIncludePath(b.path(bgfx_path ++ "src"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/dxsdk/include"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/fcpp"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/glslang/glslang/Public"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/glslang/glslang/Include"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/glslang"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/glsl-optimizer/include"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/glsl-optimizer/src/glsl"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/spirv-cross"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/spirv-tools/include"));
shaderc.addIncludePath(b.path(bgfx_path ++ "3rdparty/webgpu/include"));

shaderc.addCSourceFiles(.{
.files = &.{
bgfx_path ++ "src/shader.cpp",
bgfx_path ++ "src/shader_dxbc.cpp",
Expand Down Expand Up @@ -553,11 +538,11 @@ pub fn build(b: *std.Build) !void {

glsl_optimizer_lib.linkLibCpp();

shaderc_static.linkLibrary(fcpp_lib);
shaderc_static.linkLibrary(glslang_lib);
shaderc_static.linkLibrary(glsl_optimizer_lib);
shaderc_static.linkLibrary(spirv_opt_lib);
shaderc_static.linkLibrary(spirv_cross_lib);
shaderc.linkLibrary(fcpp_lib);
shaderc.linkLibrary(glslang_lib);
shaderc.linkLibrary(glsl_optimizer_lib);
shaderc.linkLibrary(spirv_opt_lib);
shaderc.linkLibrary(spirv_cross_lib);
}
}

Expand Down
6 changes: 5 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
.{
.name = "zbgfx",
.version = "0.1.0",
.version = "0.2.0",
.paths = .{
"includes",
"libs",
"shaders",
"src",
"tools",
"build.zig",
"build.zig.zon",
},
Expand Down
6 changes: 3 additions & 3 deletions examples/00-minimal/build_sample.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn build(
"fs_cubes.zig",
.{
.shaderType = .fragment,
.input = .{ .cwd_relative = thisDir() ++ "/src/fs_cubes.sc" },
.input = b.path("00-minimal/src/fs_cubes.sc"),
},
.{
.output = thisDir() ++ "/src/fs_cubes.bin.zig",
Expand All @@ -71,7 +71,7 @@ pub fn build(
"vs_cubes.zig",
.{
.shaderType = .vertex,
.input = .{ .cwd_relative = thisDir() ++ "/src/vs_cubes.sc" },
.input = b.path("00-minimal/src/vs_cubes.sc"),
},
.{
.output = thisDir() ++ "/src/vs_cubes.bin.zig",
Expand All @@ -81,7 +81,7 @@ pub fn build(

const exe = b.addExecutable(.{
.name = "00-minimal",
.root_source_file = .{ .cwd_relative = thisDir() ++ "/src/main.zig" },
.root_source_file = b.path("00-minimal/src/main.zig"),
.target = target,
});
b.installArtifact(exe);
Expand Down
4 changes: 0 additions & 4 deletions examples/01-zgui/build_sample.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,3 @@ pub fn build(
exe.linkLibrary(zgui.artifact("imgui"));
exe.linkLibrary(zbgfx_dep.artifact("bgfx"));
}

inline fn thisDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse ".";
}
7 changes: 2 additions & 5 deletions examples/02-runtime-shaderc/build_sample.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ pub fn build(
});
b.installArtifact(exe);
exe.linkLibrary(zbgfx_dep.artifact("bgfx"));
exe.linkLibrary(zbgfx_dep.artifact("shaderc-static"));

b.installArtifact(zbgfx_dep.artifact("shaderc"));

exe.root_module.addImport("zbgfx", zbgfx_dep.module("zbgfx"));
exe.root_module.addImport("zmath", zmath.module("root"));
Expand All @@ -70,7 +71,3 @@ pub fn build(
});
exe.step.dependOn(&install_example_shaders.step);
}

inline fn thisDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse ".";
}
25 changes: 14 additions & 11 deletions examples/02-runtime-shaderc/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ var last_r = zglfw.Action.release;
var old_flags = bgfx.ResetFlags_None;
var old_size = [2]i32{ WIDTH, HEIGHT };

pub fn buildProgram(allocator: std.mem.Allocator) !bgfx.ProgramHandle {
pub fn buildProgram(allocator: std.mem.Allocator, shaderc_path: []const u8) !bgfx.ProgramHandle {
// Load varying from file
const varying_data = try readFileFromShaderDirs(allocator, "varying.def.sc");
defer allocator.free(varying_data);
Expand All @@ -100,25 +100,24 @@ pub fn buildProgram(allocator: std.mem.Allocator) !bgfx.ProgramHandle {

const exe_dir = try std.fs.selfExeDirPathAlloc(allocator);
defer allocator.free(exe_dir);
const path = try std.fs.path.joinZ(allocator, &.{ exe_dir, "..", "include", "shaders" });
defer allocator.free(path);

var includes = [_][*c]const u8{path};
const path = try std.fs.path.join(allocator, &.{ exe_dir, "..", "include", "shaders" });
defer allocator.free(path);

// Compile fs shader
var fs_shader_options = shaderc.createDefaultOptionsForRenderer(bgfx.getRendererType());
fs_shader_options.shaderType = .fragment;
fs_shader_options.includeDirs = &includes;
fs_shader_options.includeDirs = &.{path};

const fs_shader = try shaderc.compileShader(allocator, varying_data, fs_cube_data, fs_shader_options);
const fs_shader = try shaderc.compileShader(allocator, shaderc_path, varying_data, fs_cube_data, fs_shader_options);
defer allocator.free(fs_shader);

// Compile vs shader
var vs_shader_options = shaderc.createDefaultOptionsForRenderer(bgfx.getRendererType());
vs_shader_options.shaderType = .vertex;
vs_shader_options.includeDirs = &includes;
vs_shader_options.includeDirs = &.{path};

const vs_shader = try shaderc.compileShader(allocator, varying_data, vs_cube_data, vs_shader_options);
const vs_shader = try shaderc.compileShader(allocator, shaderc_path, varying_data, vs_cube_data, vs_shader_options);
defer allocator.free(vs_shader);

//
Expand Down Expand Up @@ -220,7 +219,11 @@ pub fn main() anyerror!u8 {
const gpa_allocator = gpa.allocator();
defer _ = gpa.deinit();

var programHandle = buildProgram(gpa_allocator) catch |err| {
const shaderc_path = try shaderc.shadercFromExePath(gpa_allocator);

defer gpa_allocator.free(shaderc_path);

var programHandle = buildProgram(gpa_allocator, shaderc_path) catch |err| {
std.log.err("Build program failed => {}", .{err});
return 1;
};
Expand Down Expand Up @@ -277,7 +280,7 @@ pub fn main() anyerror!u8 {
last_d = window.getKey(.d);

if (last_r != .press and window.getKey(.r) == .press) {
if (buildProgram(gpa_allocator)) |program| {
if (buildProgram(gpa_allocator, shaderc_path)) |program| {
bgfx.destroyProgram(programHandle);
programHandle = program;
} else |err| {
Expand Down Expand Up @@ -363,7 +366,7 @@ pub fn main() anyerror!u8 {
return 0;
}

pub fn readFileFromShaderDirs(allocator: std.mem.Allocator, filename: []const u8) ![:0]u8 {
fn readFileFromShaderDirs(allocator: std.mem.Allocator, filename: []const u8) ![:0]u8 {
const exe_dir = try std.fs.selfExeDirPathAlloc(allocator);
defer allocator.free(exe_dir);

Expand Down
4 changes: 0 additions & 4 deletions examples/03-debugdraw/build_sample.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,3 @@ pub fn build(
exe.root_module.addImport("zglfw", zglfw.module("root"));
exe.linkLibrary(zglfw.artifact("glfw"));
}

inline fn thisDir() []const u8 {
return comptime std.fs.path.dirname(@src().file) orelse ".";
}
117 changes: 0 additions & 117 deletions libs/bgfx/tools/shaderc/shaderc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2651,124 +2651,7 @@ namespace bgfx

} // namespace bgfx

#ifdef ZBGFX_EMBED_SHADERC
extern "C"
{
typedef int32_t(writeFce)(void *context, const void *_data, int32_t _size);
class ClbWriter : public bx::WriterI
{
public:
///
ClbWriter(void *context, writeFce *_write_fce) : m_writeFce(_write_fce), m_context(context)
{
}

virtual int32_t write(const void *_data, int32_t _size, bx::Error *_err) override
{
return m_writeFce(m_context, _data, _size);
}

private:
writeFce *m_writeFce;
void *m_context;
};

struct COptions
{
char shaderType;
const char *platform;
const char *profile;

const char *inputFilePath;
const char *outputFilePath;

const char **includeDirs;
uint32_t includeDirsN;

const char **defines;
uint32_t definesN;

const char **dependencies;
uint32_t dependenciesN;

bool disasm;
bool raw;
bool preprocessOnly;
bool depends;

bool debugInformation;

bool avoidFlowControl;
bool noPreshader;
bool partialPrecision;
bool preferFlowControl;
bool backwardsCompatibility;
bool warningsAreErrors;
bool keepIntermediate;

bool optimize;
uint32_t optimizationLevel;
};

bool zbgfx_compileShader(const char *_varying, const char *_comment, char *_shader, uint32_t _shaderLen, const COptions *_options, writeFce *_shaderWriter, void *_shaderWriterContext, writeFce *_messageWriter, void *_messageWriterContext)
{
bgfx::Options options;

std::vector<std::string> includesDirs(_options->includeDirsN);
for (int i = 0; i < _options->includeDirsN; i++)
{
includesDirs.push_back(_options->includeDirs[i]);
}

std::vector<std::string> defines(_options->definesN);
for (int i = 0; i < _options->definesN; i++)
{
includesDirs.push_back(_options->defines[i]);
}

std::vector<std::string> dependencies(_options->dependenciesN);
for (int i = 0; i < _options->dependenciesN; i++)
{
includesDirs.push_back(_options->dependencies[i]);
}

options.shaderType = _options->shaderType;
options.platform = _options->platform;
options.profile = _options->profile;
options.inputFilePath = _options->inputFilePath;
options.outputFilePath = _options->outputFilePath;
options.includeDirs = includesDirs;
options.defines = defines;
options.dependencies = dependencies;
options.disasm = _options->disasm;
options.raw = _options->raw;
options.preprocessOnly = _options->preprocessOnly;
options.depends = _options->depends;
options.debugInformation = _options->debugInformation;
options.avoidFlowControl = _options->avoidFlowControl;
options.noPreshader = _options->noPreshader;
options.partialPrecision = _options->partialPrecision;
options.preferFlowControl = _options->preferFlowControl;
options.backwardsCompatibility = _options->backwardsCompatibility;
options.warningsAreErrors = _options->warningsAreErrors;
options.keepIntermediate = _options->keepIntermediate;
options.optimize = _options->optimize;
options.optimizationLevel = _options->optimizationLevel;

ClbWriter shaderWriter(_shaderWriterContext, _shaderWriter);
ClbWriter messageWriter(_messageWriterContext, _messageWriter);

return bgfx::compileShader(_varying, _comment, _shader, _shaderLen, options, &shaderWriter, &messageWriter);
}

int shaderc_main(int _argc, const char *_argv[])
{
return bgfx::compileShader(_argc, _argv);
}
}
#else
int main(int _argc, const char *_argv[])
{
return bgfx::compileShader(_argc, _argv);
}
#endif
Loading

0 comments on commit 68a85a7

Please sign in to comment.