Skip to content

Commit 977d05f

Browse files
committed
initial commit
1 parent 5821a7f commit 977d05f

13 files changed

Lines changed: 320 additions & 0 deletions

File tree

.github/workflows/build.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
permissions:
10+
contents: read
11+
deployments: write
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
if: "!contains(github.event.head_commit.message, 'ci skip')"
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
- uses: mlugg/setup-zig@v2
21+
22+
- name: Building
23+
run: zig build
24+
25+
- name: Generating docs
26+
run: zig build docs
27+
28+
- name: Publish to Cloudflare Pages
29+
uses: cloudflare/wrangler-action@v3
30+
with:
31+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
32+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
33+
command: pages deploy zig-out/docs --project-name=spirv-interpreter-docs
34+
gitHubToken: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
permissions:
10+
contents: read
11+
deployments: write
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
if: "!contains(github.event.head_commit.message, 'ci skip')"
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
- uses: mlugg/setup-zig@v2
21+
22+
- name: Test
23+
run: zig build test

build.zig

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const mod = b.createModule(.{
8+
.root_source_file = b.path("src/lib.zig"),
9+
.target = target,
10+
.optimize = optimize,
11+
});
12+
13+
const spirv_headers = b.dependency("spirv_headers", .{});
14+
mod.addSystemIncludePath(spirv_headers.path("include/spirv/unified1"));
15+
16+
const lib = b.addLibrary(.{
17+
.name = "spirv_interpreter",
18+
.root_module = mod,
19+
.linkage = .dynamic,
20+
});
21+
const lib_install = b.addInstallArtifact(lib, .{});
22+
23+
// Zig example setup
24+
25+
const example_exe = b.addExecutable(.{
26+
.name = "spirv_interpreter_example",
27+
.root_module = b.createModule(.{
28+
.root_source_file = b.path("example/main.zig"),
29+
.target = target,
30+
.optimize = optimize,
31+
.imports = &.{
32+
.{ .name = "spv", .module = mod },
33+
},
34+
}),
35+
});
36+
37+
const spirv_target = b.resolveTargetQuery(.{
38+
.cpu_arch = .spirv32,
39+
.cpu_model = .{ .explicit = &std.Target.spirv.cpu.vulkan_v1_2 },
40+
.os_tag = .vulkan,
41+
.ofmt = .spirv,
42+
});
43+
44+
const shader = b.addObject(.{
45+
.name = "shader.zig",
46+
.root_module = b.createModule(.{
47+
.root_source_file = b.path("example/shader.zig"),
48+
.target = spirv_target,
49+
}),
50+
.use_llvm = false,
51+
});
52+
53+
example_exe.root_module.addAnonymousImport("shader", .{ .root_source_file = shader.getEmittedBin() });
54+
55+
const example_install = b.addInstallArtifact(example_exe, .{});
56+
example_install.step.dependOn(&lib_install.step);
57+
58+
const run_example = b.addRunArtifact(example_exe);
59+
run_example.step.dependOn(&example_install.step);
60+
61+
const run_example_step = b.step("example", "Run the basic example");
62+
run_example_step.dependOn(&run_example.step);
63+
64+
// Zig unit tests setup
65+
66+
const lib_tests = b.addTest(.{ .root_module = mod });
67+
const run_tests = b.addRunArtifact(lib_tests);
68+
const test_step = b.step("test", "Run Zig unit tests");
69+
test_step.dependOn(&run_tests.step);
70+
71+
// Docs generation
72+
73+
const autodoc_test = b.addObject(.{
74+
.name = "lib",
75+
.root_module = mod,
76+
});
77+
const install_docs = b.addInstallDirectory(.{
78+
.source_dir = autodoc_test.getEmittedDocs(),
79+
.install_dir = .prefix,
80+
.install_subdir = "docs",
81+
});
82+
83+
const docs_step = b.step("docs", "Build and install the documentation");
84+
docs_step.dependOn(&install_docs.step);
85+
}

build.zig.zon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.{
2+
.name = .SPIRV_Interpreter,
3+
.version = "0.0.1",
4+
.dependencies = .{
5+
.spirv_headers = .{
6+
.url = "git+https://github.com/KhronosGroup/SPIRV-Headers#0a7f626a6ae86284a413d105b47a6fb413bf6c92",
7+
.hash = "N-V-__8AAGOhPABkbhEc-SenlpOzOI4ppi0CDXVSteii5ywV",
8+
},
9+
},
10+
.minimum_zig_version = "0.15.2",
11+
.paths = .{
12+
"build.zig",
13+
"build.zig.zon",
14+
"src",
15+
"LICENSE",
16+
},
17+
.fingerprint = 0xd87ec8ab9fa9396a,
18+
}

example/main.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const std = @import("std");
2+
const spv = @import("spv");
3+
4+
const shader_source = @embedFile("shader");
5+
6+
pub fn main() !void {
7+
{
8+
var gpa: std.heap.DebugAllocator(.{}) = .init;
9+
defer _ = gpa.deinit();
10+
11+
const allocator = gpa.allocator();
12+
13+
const ctx = try spv.Interpreter.init();
14+
defer ctx.deinit();
15+
16+
const module = try spv.Module.init(allocator, &ctx, @ptrCast(@alignCast(shader_source)));
17+
defer module.deinit(allocator);
18+
}
19+
std.log.info("Successfully executed", .{});
20+
}

example/shader.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const std = @import("std");
2+
const gpu = std.gpu;
3+
4+
extern var frag_color: @Vector(4, f32) addrspace(.output);
5+
6+
export fn main() callconv(.spirv_fragment) void {
7+
gpu.location(&frag_color, 0);
8+
9+
frag_color = .{ 1.0, 1.0, 1.0, 1.0 };
10+
}

src/Image.zig

Whitespace-only changes.

src/Interpreter.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const Self = @This();
2+
3+
pub fn init() !Self {
4+
return .{};
5+
}
6+
7+
pub fn deinit(self: *const Self) void {
8+
_ = self;
9+
}

src/Module.zig

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const std = @import("std");
2+
const lib = @import("lib.zig");
3+
4+
const SpvVoid = lib.SpvVoid;
5+
const SpvByte = lib.SpvByte;
6+
const SpvWord = lib.SpvWord;
7+
const SpvBool = lib.SpvBool;
8+
const spv = lib.spv;
9+
10+
const Interpreter = @import("Interpreter.zig");
11+
const WordIterator = @import("WordIterator.zig");
12+
13+
const Self = @This();
14+
15+
const SpvEntryPoint = struct {
16+
exec_model: spv.SpvExecutionModel,
17+
id: SpvWord,
18+
name: []const u8,
19+
globals_count: SpvWord,
20+
globals: []const SpvWord,
21+
};
22+
23+
ctx: *const Interpreter,
24+
25+
it: WordIterator,
26+
27+
version_major: SpvByte,
28+
version_minor: SpvByte,
29+
generator_magic: SpvWord,
30+
31+
bound: SpvWord,
32+
33+
code: []const SpvWord,
34+
35+
addressing: spv.SpvAddressingModel,
36+
memory_model: spv.SpvMemoryModel,
37+
38+
entry_point_count: SpvWord,
39+
entry_points: []const SpvEntryPoint,
40+
41+
local_size_x: SpvWord,
42+
local_size_y: SpvWord,
43+
local_size_z: SpvWord,
44+
45+
pub fn init(allocator: std.mem.Allocator, ctx: *const Interpreter, source: []const SpvWord) !Self {
46+
var self: Self = std.mem.zeroInit(Self, .{
47+
.ctx = ctx,
48+
.code = try allocator.dupe(SpvWord, source),
49+
});
50+
51+
self.it = WordIterator.init(self.code);
52+
53+
return self;
54+
}
55+
56+
pub fn deinit(self: *const Self, allocator: std.mem.Allocator) void {
57+
allocator.free(self.code);
58+
}

src/State.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const std = @import("std");
2+
3+
const Interpreter = @import("Interpreter.zig");
4+
const Module = @import("Module.zig");
5+
6+
const Self = @This();
7+
8+
ctx: *Interpreter,
9+
owner: *Module,

0 commit comments

Comments
 (0)