renderer/metal: setup sampler state

This commit is contained in:
Mitchell Hashimoto
2023-11-16 15:37:50 -08:00
parent 1e572fb10b
commit 01a73994cb
4 changed files with 52 additions and 23 deletions

View File

@ -103,7 +103,6 @@ fn buildGlslang(
"SPIRV/SpvBuilder.cpp", "SPIRV/SpvBuilder.cpp",
"SPIRV/SpvPostProcess.cpp", "SPIRV/SpvPostProcess.cpp",
"SPIRV/doc.cpp", "SPIRV/doc.cpp",
"SPIRV/SpvTools.cpp",
"SPIRV/disassemble.cpp", "SPIRV/disassemble.cpp",
"SPIRV/CInterface/spirv_c_interface.cpp", "SPIRV/CInterface/spirv_c_interface.cpp",
}, },

View File

@ -27,6 +27,7 @@ const Terminal = terminal.Terminal;
const mtl = @import("metal/api.zig"); const mtl = @import("metal/api.zig");
const mtl_buffer = @import("metal/buffer.zig"); const mtl_buffer = @import("metal/buffer.zig");
const mtl_image = @import("metal/image.zig"); const mtl_image = @import("metal/image.zig");
const mtl_sampler = @import("metal/sampler.zig");
const mtl_shaders = @import("metal/shaders.zig"); const mtl_shaders = @import("metal/shaders.zig");
const Image = mtl_image.Image; const Image = mtl_image.Image;
const ImageMap = mtl_image.ImageMap; const ImageMap = mtl_image.ImageMap;
@ -282,12 +283,10 @@ pub fn init(alloc: Allocator, options: renderer.Options) !Metal {
log.warn("error loading custom shaders err={}", .{err}); log.warn("error loading custom shaders err={}", .{err});
break :err &.{}; break :err &.{};
}; };
_ = custom_shaders;
// Initialize our shaders // Initialize our shaders
var shaders = try Shaders.init(alloc, device, &.{ var shaders = try Shaders.init(alloc, device, custom_shaders);
@embedFile("shaders/temp3.metal"), //var shaders = try Shaders.init(alloc, device, &.{@embedFile("shaders/temp3.metal")});
});
errdefer shaders.deinit(alloc); errdefer shaders.deinit(alloc);
// Font atlas textures // Font atlas textures
@ -779,23 +778,9 @@ fn drawPostShader(
encoder: objc.Object, encoder: objc.Object,
texture: objc.c.id, texture: objc.c.id,
) !void { ) !void {
// Use our image shader pipeline // Build our sampler for our texture
encoder.msgSend( var sampler = try mtl_sampler.Sampler.init(self.device);
void, defer sampler.deinit();
objc.sel("setRenderPipelineState:"),
.{self.shaders.post_pipelines[0].value},
);
// Set our uniform, which is the only shared buffer
encoder.msgSend(
void,
objc.sel("setVertexBytes:length:atIndex:"),
.{
@as(*const anyopaque, @ptrCast(&self.uniforms)),
@as(c_ulong, @sizeOf(@TypeOf(self.uniforms))),
@as(c_ulong, 1),
},
);
const Buffer = mtl_buffer.Buffer(mtl_shaders.PostUniforms); const Buffer = mtl_buffer.Buffer(mtl_shaders.PostUniforms);
var buf = try Buffer.initFill(self.device, &.{.{ var buf = try Buffer.initFill(self.device, &.{.{
@ -817,6 +802,20 @@ fn drawPostShader(
defer buf.deinit(); defer buf.deinit();
post_time += 1; post_time += 1;
// Use our custom shader pipeline
encoder.msgSend(
void,
objc.sel("setRenderPipelineState:"),
.{self.shaders.post_pipelines[0].value},
);
// Set our sampler
encoder.msgSend(
void,
objc.sel("setFragmentSamplerState:atIndex:"),
.{ sampler.sampler.value, @as(c_ulong, 0) },
);
// Set our buffer // Set our buffer
encoder.msgSend( encoder.msgSend(
void, void,

View File

@ -0,0 +1,31 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const objc = @import("objc");
pub const Sampler = struct {
sampler: objc.Object,
pub fn init(device: objc.Object) !Sampler {
const desc = init: {
const Class = objc.getClass("MTLSamplerDescriptor").?;
const id_alloc = Class.msgSend(objc.Object, objc.sel("alloc"), .{});
const id_init = id_alloc.msgSend(objc.Object, objc.sel("init"), .{});
break :init id_init;
};
defer desc.msgSend(void, objc.sel("release"), .{});
const sampler = device.msgSend(
objc.Object,
objc.sel("newSamplerStateWithDescriptor:"),
.{desc},
);
errdefer sampler.msgSend(void, objc.sel("release"), .{});
return .{ .sampler = sampler };
}
pub fn deinit(self: *Sampler) void {
self.sampler.msgSend(void, objc.sel("release"), .{});
}
};

View File

@ -102,7 +102,7 @@ void mainImage(thread float4& fragColor, thread const float2& fragCoord, constan
fragColor = float4(col, 1.0); fragColor = float4(col, 1.0);
} }
fragment main0_out main0(constant Globals& _89 [[buffer(0)]], texture2d<float> iChannel0 [[texture(0)]], float4 gl_FragCoord [[position]]) fragment main0_out main0(constant Globals& _89 [[buffer(0)]], texture2d<float> iChannel0 [[texture(0)]], sampler iChannel0Smplr [[sampler(0)]], float4 gl_FragCoord [[position]])
{ {
constexpr sampler iChannel0Smplr(address::clamp_to_edge, filter::linear); constexpr sampler iChannel0Smplr(address::clamp_to_edge, filter::linear);