use crate::gpu_draw::{DrawList, Quad, SHADING, SIDE};
use crate::gpu_metal::{COPY_IN, Three};
use crate::macos_sys::{Bool, Obj, class, made, ns_string, send};
use std::ffi::c_void;
const ONE: usize = 1;
const ONE_MINUS_SOURCE_ALPHA: usize = 5;
const TRIANGLE_STRIP: usize = 4;
const SHADER_READ: usize = 1;
#[repr(C)]
struct Viewport([f64; 6]);
#[repr(C)]
struct Scissor([usize; 4]);
const LOAD: usize = 1;
const STORE: usize = 1;
const PRELUDE: &str = r#"
#include <metal_stdlib>
using namespace metal;
#define vec2 float2
#define vec3 float3
#define vec4 float4
#define ivec2 int2
#define as_float(bits) as_type<float>(bits)
float4 texel(texture2d<float> cache, texture2d<float> patches, int which, int x, int y) {
if (which == 0) {
return cache.read(uint2(x, y));
}
return patches.read(uint2(x, y));
}
"#;
const ENTRIES: &str = r#"
struct Quad { float4 place; float4 shape; float4 more; float4 source; float4 color; float4 kind; };
struct Corner {
float4 position [[position]];
float4 shape [[flat]];
float4 more [[flat]];
float4 source [[flat]];
float4 color [[flat]];
float kind [[flat]];
};
vertex Corner corners(uint nth [[vertex_id]], uint quad [[instance_id]], const device Quad* quads [[buffer(0)]]) {
Quad q = quads[quad];
float2 p = float2((nth & 1) != 0 ? q.place.z : q.place.x, (nth & 2) != 0 ? q.place.w : q.place.y);
Corner corner;
corner.position = float4(p.x / q.kind.z * 2.0 - 1.0, 1.0 - p.y / q.kind.w * 2.0, 0.0, 1.0);
corner.shape = q.shape;
corner.more = q.more;
corner.source = q.source;
corner.color = q.color;
corner.kind = q.kind.x;
return corner;
}
fragment float4 pixels(Corner in [[stage_in]], texture2d<float> cache [[texture(0)]], texture2d<float> patches [[texture(1)]]) {
float4 covers = float4(0.0, 0.0, 0.0, 0.0);
return shade(cache, patches, in.position.xy, in.shape, in.more, in.source, in.color, in.kind, covers);
}
"#;
pub(crate) fn source() -> String {
let defined: Vec<&str> = SHADING.lines().filter(|line| !line.starts_with(' ') && line.ends_with('{')).filter_map(|line| line.split('(').next()?.split(' ').next_back()).collect();
let mut called = defined.clone();
called.push("texel");
let mut out = String::from(PRELUDE);
for line in SHADING.lines() {
let line = line.replace("out vec4 covers", "thread vec4 &covers");
let opens = !line.starts_with(' ') && line.ends_with('{');
out.push_str(&if opens { line.replacen('(', "(texture2d<float> cache, texture2d<float> patches, ", 1) } else { passed(&line, &called) });
out.push('\n');
}
out.push_str(ENTRIES);
out
}
fn passed(line: &str, called: &[&str]) -> String {
let mut out = String::with_capacity(line.len() + 32);
let mut rest = line;
while let Some(at) = rest.find('(') {
let (before, after) = rest.split_at(at + 1);
let name = before[..at].rsplit(|c: char| !c.is_ascii_alphanumeric() && c != '_').next().unwrap_or("");
out.push_str(before);
if called.contains(&name) {
out.push_str("cache, patches, ");
}
rest = after;
}
out.push_str(rest);
out
}
pub(crate) struct Pipeline {
state: Obj,
textures: [Obj; 2],
}
impl std::fmt::Debug for Pipeline {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Pipeline").finish_non_exhaustive()
}
}
impl Pipeline {
pub(crate) fn new(device: Obj) -> Option<Pipeline> {
let library = Pipeline::library(device)?;
unsafe {
let described: Obj = send(made(c"MTLRenderPipelineDescriptor"), c"autorelease", ());
let (corners, pixels): (Obj, Obj) = (send(library, c"newFunctionWithName:", (ns_string("corners"),)), send(library, c"newFunctionWithName:", (ns_string("pixels"),)));
send::<()>(described, c"setVertexFunction:", (corners,));
send::<()>(described, c"setFragmentFunction:", (pixels,));
let attachment: Obj = send(send::<Obj>(described, c"colorAttachments", ()), c"objectAtIndexedSubscript:", (0usize,));
send::<()>(attachment, c"setPixelFormat:", (crate::gpu_metal::BGRA8,));
send::<()>(attachment, c"setBlendingEnabled:", (1 as Bool,));
[c"setSourceRGBBlendFactor:", c"setSourceAlphaBlendFactor:"].into_iter().for_each(|name| send::<()>(attachment, name, (ONE,)));
[c"setDestinationRGBBlendFactor:", c"setDestinationAlphaBlendFactor:"].into_iter().for_each(|name| send::<()>(attachment, name, (ONE_MINUS_SOURCE_ALPHA,)));
let mut error: Obj = std::ptr::null_mut();
let state: Obj = send(device, c"newRenderPipelineStateWithDescriptor:error:", (described, &mut error));
[corners, pixels, library].into_iter().for_each(|made| send::<()>(made, c"release", ()));
let textures = [0, 1].map(|_| Pipeline::texture(device));
let made = Pipeline { state, textures };
(!state.is_null() && !textures.contains(&std::ptr::null_mut())).then_some(made)
}
}
fn library(device: Obj) -> Option<Obj> {
let mut error: Obj = std::ptr::null_mut();
unsafe {
let options = made(c"MTLCompileOptions");
send::<()>(options, c"setFastMathEnabled:", (0 as Bool,));
let library: Obj = send(device, c"newLibraryWithSource:options:error:", (ns_string(&source()), options, &mut error));
send::<()>(options, c"release", ());
if library.is_null() {
let said = crate::macos_sys::rust_string(send(error, c"localizedDescription", ())).unwrap_or_default();
eprintln!("taut: Metal did not compile the drawing shaders: {said}");
}
(!library.is_null()).then_some(library)
}
}
fn texture(device: Obj) -> Obj {
unsafe {
let described: Obj =
send(class(c"MTLTextureDescriptor"), c"texture2DDescriptorWithPixelFormat:width:height:mipmapped:", (crate::gpu_metal::BGRA8, SIDE as usize, SIDE as usize, 0 as Bool));
send::<()>(described, c"setUsage:", (SHADER_READ,));
send::<()>(described, c"setStorageMode:", (crate::gpu_metal::PRIVATE,));
send(device, c"newTextureWithDescriptor:", (described,))
}
}
pub(crate) fn draw(&mut self, device: Obj, commands: Obj, (target, size): (Obj, [usize; 2]), list: &DrawList, area: [i32; 4]) -> bool {
let [left, top] = [area[0].max(0) as usize, area[1].max(0) as usize];
let [right, bottom] = [(area[2].max(0) as usize).min(size[0]), (area[3].max(0) as usize).min(size[1])];
if !self.put_up(device, commands, list) {
return false;
}
if list.quads.is_empty() || left >= right || top >= bottom {
return true;
}
unsafe {
let quads: Obj = send(device, c"newBufferWithBytes:length:options:", (list.quads.as_ptr().cast::<c_void>(), list.quads.len() * size_of::<Quad>(), 0usize));
let pass: Obj = send(class(c"MTLRenderPassDescriptor"), c"renderPassDescriptor", ());
let attachment: Obj = send(send::<Obj>(pass, c"colorAttachments", ()), c"objectAtIndexedSubscript:", (0usize,));
send::<()>(attachment, c"setTexture:", (target,));
send::<()>(attachment, c"setLoadAction:", (LOAD,));
send::<()>(attachment, c"setStoreAction:", (STORE,));
let encoder: Obj = send(commands, c"renderCommandEncoderWithDescriptor:", (pass,));
if quads.is_null() || encoder.is_null() {
send::<()>(encoder, c"endEncoding", ());
send::<()>(quads, c"release", ());
return false;
}
send::<()>(encoder, c"setRenderPipelineState:", (self.state,));
send::<()>(encoder, c"setViewport:", (Viewport([0.0, 0.0, size[0] as f64, size[1] as f64, 0.0, 1.0]),));
send::<()>(encoder, c"setScissorRect:", (Scissor([left, top, right - left, bottom - top]),));
send::<()>(encoder, c"setVertexBuffer:offset:atIndex:", (quads, 0usize, 0usize));
send::<()>(encoder, c"setFragmentTexture:atIndex:", (self.textures[0], 0usize));
send::<()>(encoder, c"setFragmentTexture:atIndex:", (self.textures[1], 1usize));
send::<()>(encoder, c"drawPrimitives:vertexStart:vertexCount:instanceCount:", (TRIANGLE_STRIP, 0usize, 4usize, list.quads.len()));
send::<()>(encoder, c"endEncoding", ());
send::<()>(quads, c"release", ());
}
true
}
fn put_up(&self, device: Obj, commands: Obj, list: &DrawList) -> bool {
if list.uploads.is_empty() {
return true;
}
unsafe {
let staged: Obj = send(device, c"newBufferWithBytes:length:options:", (list.staging.as_ptr().cast::<c_void>(), list.staging.len() * 4, 0usize));
if staged.is_null() {
return false;
}
let blit: Obj = send(commands, c"blitCommandEncoder", ());
for upload in &list.uploads {
let [x, y, wide, high] = upload.at.map(|side| side as usize);
let texture = self.textures[upload.texture as usize];
send::<()>(blit, COPY_IN, (staged, upload.from * 4, wide * 4, wide * high * 4, Three([wide, high, 1]), texture, 0usize, 0usize, Three([x, y, 0])));
}
send::<()>(blit, c"endEncoding", ());
send::<()>(staged, c"release", ());
}
true
}
}
impl Drop for Pipeline {
fn drop(&mut self) {
unsafe { [self.state, self.textures[0], self.textures[1]].into_iter().for_each(|held| send::<()>(held, c"release", ())) };
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::gpu_draw::tests::{Scene, compare};
use crate::gpu_metal::tests::{gpu, read, shown};
use crate::macos_sys::Pool;
#[test]
fn every_function_takes_the_textures_and_every_call_passes_them() {
let source = source();
assert!(source.contains("float inside_of(texture2d<float> cache, texture2d<float> patches, vec2 p, vec4 r, float radius) {"));
assert!(source.contains("vec4 shade(texture2d<float> cache, texture2d<float> patches, vec2 at, vec4 shape, vec4 more, vec4 source, vec4 color, float kind, thread vec4 &covers) {"));
assert!(source.contains("return floor(texel(cache, patches, which, int(x), int(y)) * 255.0 + 0.5);"));
assert!(source.contains("float outer = inside_of(cache, patches, p, r, radius);"));
assert!(source.contains("mixed(cache, patches, bytes(cache, patches, 1, source.x + left"), "calls within calls");
assert!(source.contains("vec4 top = mixed("), "a variable named like nothing called is left");
assert!(!source.contains("out vec4"));
assert_eq!(passed("x = floor(y) + max(a, b);", &["floor"]), "x = floor(cache, patches, y) + max(a, b);");
assert_eq!(passed("subcell(e, pixel);", &["cell"]), "subcell(e, pixel);", "a name ending another is not it");
}
#[test]
fn metal_draws_every_kind_as_the_cpu_does() {
let _pool = Pool::new();
let _one = crate::gpu_metal::tests::one();
let Some(mut gpu) = gpu() else { return eprintln!("no Metal device here: not tested") };
assert!(gpu.drawing([0, 0, 4096, 4096]).is_some(), "the shaders compiled");
for scale in [1.0, 1.5, 2.0] {
let mut scene = Scene::new(scale);
let (_under, mut shows) = shown(&mut gpu, scene.size.map(|side| side as usize));
let under = crate::Surface { width: scene.size[0] as usize, height: scene.size[1] as usize, pixels: scene.under.clone() };
gpu.put(&mut shows, &under, [0, 0, under.width, under.height]);
let mut list = std::mem::take(&mut *gpu.list);
scene.record(&mut list);
assert!(!list.failed);
assert!(gpu.draw(&mut shows, list, [0, 0, scene.size[0], scene.size[1]]), "Metal drew it");
gpu.show(&mut shows, false);
let texture = shows.texture();
let drawn = read(&gpu, &mut shows, texture);
compare(&scene, &drawn, &format!("Metal at {scale}"));
let mut list = std::mem::take(&mut *gpu.list);
scene.record(&mut list);
assert!(list.uploads.iter().all(|upload| upload.texture == 1), "kept glyphs are not put in again");
assert!(gpu.draw(&mut shows, list, [0, 0, scene.size[0], scene.size[1] / 2]));
gpu.show(&mut shows, false);
compare(&scene, &read(&gpu, &mut shows, texture), &format!("Metal at {scale}, again"));
gpu.forget(shows);
}
assert!(gpu.has_drawn());
}
}