Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update "Shader compilation" in README #108

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,27 +288,35 @@ pub const xcb_connection_t = if (@hasDecl(root, "xcb_connection_t")) root.xcb_co
For some times (such as those from Google Games Platform) no default is known. Usage of these without providing a concrete type in the project root generates a compile error.

### Shader compilation
vulkan-zig provides functionality to help compiling shaders to spir-v using glslc. It can be used from build.zig as follows:

vulkan-zig provides functionality to help compile shaders to SPIR-V using glslc.
It can be used from build.zig as follows:

```zig
const vkgen = @import("vulkan-zig/generator/index.zig");
const vkgen = @import("vulkan_zig");

pub fn build(b: *Builder) void {
...
const exe = b.addExecutable("my-executable", "src/main.zig");

const gen = vkgen.VkGenerateStep(b, "path/to/vk.xml", "vk.zig");
exe.addPackage(gen.package);

const shader_comp = vkgen.ShaderCompileStep.create(
builder,
&[_][]const u8{"glslc", "--target-env=vulkan1.2"}, // Path to glslc and additional parameters
&[_][]const u8{"glslc", "--target-env=vulkan1.2"},
"-o",
);
exe.addPackage(shader_comp.getPackage("shaders"));
shader_comp.add("shader", "path/to/shader.frag", .{});
shader_comp.add("shader_frag", "path/to/shader.frag", .{});
shader_comp.add("shader_vert", "path/to/shader.vert", .{});
exe.addModule("shaders", shader_comp.getModule());
}
```
Upon compilation, glslc is then invoked to compile each shader, and the result is placed within `zig-cache`. All shaders which are compiled using a particular `ShaderCompileStep` are imported in a single Zig file using `@embedFile`, and this file can be added to an executable as a package using `getPackage`. To slightly improve compile times, shader compilation is cached; as long as a shader's source and its compile commands stay the same, the shader is not recompiled. The spir-v code for any particular shader is aligned to that of a 32-bit integer as follows, as required by vkCreateShaderModule:

Upon compilation, glslc is invoked to compile each shader, and the result is
placed within `zig-cache`. All shaders which are compiled using a particular
`ShaderCompileStep` are imported in a single Zig file using `@embedFile`, and
this file can be added to an executable as a module using `addModule`. To
slightly improve compile times, shader compilation is cached; as long as a
shader's source and its compile commands stay the same, the shader is not
recompiled. The SPIR-V code for any particular shader is aligned to that of a
32-bit integer as follows, as required by vkCreateShaderModule:

```zig
pub const ${name} align(@alignOf(u32)) = @embedFile("${path}").*;
```
Expand Down