diff options
| author | Mitchell Hashimoto <m@mitchellh.com> | 2025-09-18 08:57:47 -0700 |
|---|---|---|
| committer | Mitchell Hashimoto <m@mitchellh.com> | 2025-09-18 09:25:37 -0700 |
| commit | a45368161507343c5ce540c2e2bb9ba41bc46e91 (patch) | |
| tree | a1674216eaf90e90a436cff8875e58273fe1af8e /pkg/opengl | |
| parent | a3643f8f5205b1e1d0a85b103202977a5a251e68 (diff) | |
renderer: create explicit sampler state for custom shaders
The GLSL to MSL conversion process uses a passed-in sampler state for
the `iChannel0` parameter and we weren't providing it. This magically
worked on Apple Silicon for unknown reasons but failed on Intel GPUs.
In normal, hand-written MSL, we'd explicitly create the sampler state as
a normal variable (we do this in `shaders.metal` already!), but the
Shadertoy conversion stuff doesn't do this, probably because the exact
sampler parameters can't be safely known.
This fixes a Metal validation error when using custom shaders:
```
-[MTLDebugRenderCommandEncoder validateCommonDrawErrors:]:5970: failed
assertion `Draw Errors Validation Fragment Function(main0): missing Sampler
binding at index 0 for iChannel0Smplr[0].
```
Diffstat (limited to 'pkg/opengl')
| -rw-r--r-- | pkg/opengl/Sampler.zig | 43 | ||||
| -rw-r--r-- | pkg/opengl/main.zig | 1 |
2 files changed, 44 insertions, 0 deletions
diff --git a/pkg/opengl/Sampler.zig b/pkg/opengl/Sampler.zig new file mode 100644 index 000000000..f5c15699f --- /dev/null +++ b/pkg/opengl/Sampler.zig @@ -0,0 +1,43 @@ +const Sampler = @This(); + +const std = @import("std"); +const c = @import("c.zig").c; +const errors = @import("errors.zig"); +const glad = @import("glad.zig"); +const Texture = @import("Texture.zig"); + +id: c.GLuint, + +/// Create a single sampler. +pub fn create() errors.Error!Sampler { + var id: c.GLuint = undefined; + glad.context.GenSamplers.?(1, &id); + try errors.getError(); + return .{ .id = id }; +} + +/// glBindSampler +pub fn bind(v: Sampler, index: c_uint) !void { + glad.context.BindSampler.?(index, v.id); + try errors.getError(); +} + +pub fn parameter( + self: Sampler, + name: Texture.Parameter, + value: anytype, +) errors.Error!void { + switch (@TypeOf(value)) { + c.GLint => glad.context.SamplerParameteri.?( + self.id, + @intFromEnum(name), + value, + ), + else => unreachable, + } + try errors.getError(); +} + +pub fn destroy(v: Sampler) void { + glad.context.DeleteSamplers.?(1, &v.id); +} diff --git a/pkg/opengl/main.zig b/pkg/opengl/main.zig index 7165ad3ab..2f22154c6 100644 --- a/pkg/opengl/main.zig +++ b/pkg/opengl/main.zig @@ -18,6 +18,7 @@ pub const Buffer = @import("Buffer.zig"); pub const Framebuffer = @import("Framebuffer.zig"); pub const Renderbuffer = @import("Renderbuffer.zig"); pub const Program = @import("Program.zig"); +pub const Sampler = @import("Sampler.zig"); pub const Shader = @import("Shader.zig"); pub const Texture = @import("Texture.zig"); pub const VertexArray = @import("VertexArray.zig"); |
