From b2f19dffbf560c197e1fab096d15e96ab92fd743 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 14 Oct 2018 12:16:21 -0700 Subject: [PATCH] Add shaders for marching squares --- Metaballs.xcodeproj/project.pbxproj | 10 +++++++ MetaballsKit/MarchingSquares.metal | 45 +++++++++++++++++++++++++++++ MetaballsKit/ShaderTypes.hh | 24 +++++++++++++++ MetaballsKit/Shaders.metal | 10 +------ 4 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 MetaballsKit/MarchingSquares.metal create mode 100644 MetaballsKit/ShaderTypes.hh diff --git a/Metaballs.xcodeproj/project.pbxproj b/Metaballs.xcodeproj/project.pbxproj index c6e93c8..11ef594 100644 --- a/Metaballs.xcodeproj/project.pbxproj +++ b/Metaballs.xcodeproj/project.pbxproj @@ -10,6 +10,9 @@ C04CDEB221730A820018C4DA /* Rect.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04CDEB121730A820018C4DA /* Rect.swift */; }; C04CDEB321730A820018C4DA /* Rect.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04CDEB121730A820018C4DA /* Rect.swift */; }; C04CDEB421730A820018C4DA /* Rect.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04CDEB121730A820018C4DA /* Rect.swift */; }; + C04CDEB62173ABD50018C4DA /* MarchingSquares.metal in Sources */ = {isa = PBXBuildFile; fileRef = C04CDEB52173ABD50018C4DA /* MarchingSquares.metal */; }; + C04CDEB72173ABD50018C4DA /* MarchingSquares.metal in Sources */ = {isa = PBXBuildFile; fileRef = C04CDEB52173ABD50018C4DA /* MarchingSquares.metal */; }; + C04CDEB82173ABD50018C4DA /* MarchingSquares.metal in Sources */ = {isa = PBXBuildFile; fileRef = C04CDEB52173ABD50018C4DA /* MarchingSquares.metal */; }; C0ABCC411F50A560004A0290 /* ScreenSaver.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C0ABCC401F50A560004A0290 /* ScreenSaver.framework */; }; C0B906E91F455D1A00B5F89B /* MetaballsSaverView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0B906E81F455D1A00B5F89B /* MetaballsSaverView.swift */; }; C0B906EA1F455EB300B5F89B /* MetalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C0BBE3A71F2E893A00E68524 /* MetalKit.framework */; }; @@ -78,6 +81,8 @@ /* Begin PBXFileReference section */ C04CDEB121730A820018C4DA /* Rect.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Rect.swift; sourceTree = ""; }; + C04CDEB52173ABD50018C4DA /* MarchingSquares.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = MarchingSquares.metal; sourceTree = ""; }; + C04CDEB92173ABE50018C4DA /* ShaderTypes.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ShaderTypes.hh; sourceTree = ""; }; C091616F1F3F5AE6009C4263 /* PreferencesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesViewController.swift; sourceTree = ""; }; C0ABCC401F50A560004A0290 /* ScreenSaver.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ScreenSaver.framework; path = System/Library/Frameworks/ScreenSaver.framework; sourceTree = SDKROOT; }; C0B906D41F45432700B5F89B /* Preferences.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = ""; }; @@ -211,6 +216,8 @@ children = ( C0BBE3A31F2E81C700E68524 /* Metaballs.swift */, C0FF7CC1217020820081B781 /* MarchingSquares.swift */, + C04CDEB52173ABD50018C4DA /* MarchingSquares.metal */, + C04CDEB92173ABE50018C4DA /* ShaderTypes.hh */, C0CE7BFF1F362C3F001516B6 /* Geometry.swift */, C0DF1D781F3789DC0038B0A0 /* Memory.swift */, C0FF7C9C216A6DE00081B781 /* Math.swift */, @@ -442,6 +449,7 @@ C0FF7C992168062C0081B781 /* Shaders.metal in Sources */, C0FF7C9A2168062C0081B781 /* Preferences.swift in Sources */, C0FF7C9E216A6DE00081B781 /* Math.swift in Sources */, + C04CDEB82173ABD50018C4DA /* MarchingSquares.metal in Sources */, C0FF7CC3217020820081B781 /* MarchingSquares.swift in Sources */, C04CDEB421730A820018C4DA /* Rect.swift in Sources */, C0FF7C972168062C0081B781 /* Memory.swift in Sources */, @@ -468,6 +476,7 @@ C0FF7C932168062B0081B781 /* Preferences.swift in Sources */, C0BBE36B1F2E816500E68524 /* AppDelegate.swift in Sources */, C0FF7C9D216A6DE00081B781 /* Math.swift in Sources */, + C04CDEB62173ABD50018C4DA /* MarchingSquares.metal in Sources */, C0FF7CC2217020820081B781 /* MarchingSquares.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -487,6 +496,7 @@ C0FF7CBA216B002E0081B781 /* Shaders.metal in Sources */, C0FF7CB5216B002E0081B781 /* Metaballs.swift in Sources */, C0FF7CB6216B002E0081B781 /* Geometry.swift in Sources */, + C04CDEB72173ABD50018C4DA /* MarchingSquares.metal in Sources */, C0FF7CB9216B002E0081B781 /* Renderer.swift in Sources */, C04CDEB321730A820018C4DA /* Rect.swift in Sources */, C0FF7CA8216AFD840081B781 /* ViewController.swift in Sources */, diff --git a/MetaballsKit/MarchingSquares.metal b/MetaballsKit/MarchingSquares.metal new file mode 100644 index 0000000..64207bf --- /dev/null +++ b/MetaballsKit/MarchingSquares.metal @@ -0,0 +1,45 @@ +// +// MarchingSquares.metal +// Metaballs +// +// Created by Eryn Wells on 10/14/18. +// Copyright © 2018 Eryn Wells. All rights reserved. +// + +#include +#include "ShaderTypes.hh" +using namespace metal; + +struct Rect { + float4x4 transform; + float4 color; +}; + +struct RasterizerData { + float4 position [[position]]; + float4 color; + float2 textureCoordinate; +}; + +vertex RasterizerData +gridVertexShader(constant Vertex *vertexes [[buffer(0)]], + constant Rect *rects [[buffer(1)]], + constant RenderParameters &renderParameters [[buffer(2)]], + uint vid [[vertex_id]], + uint instid [[instance_id]]) +{ + Vertex v = vertexes[vid]; + + Rect rect = rects[instid]; + + RasterizerData out; + out.position = renderParameters.projection * rect.transform * float4(v.position.xy, 0, 1); + out.textureCoordinate = v.textureCoordinate; + return out; +} + +fragment float4 +gridFragmentShader(RasterizerData in [[stage_in]]) +{ + return in.color; +} diff --git a/MetaballsKit/ShaderTypes.hh b/MetaballsKit/ShaderTypes.hh new file mode 100644 index 0000000..a75ebcb --- /dev/null +++ b/MetaballsKit/ShaderTypes.hh @@ -0,0 +1,24 @@ +// +// ShaderTypes.h +// Metaballs +// +// Created by Eryn Wells on 10/14/18. +// Copyright © 2018 Eryn Wells. All rights reserved. +// + +#ifndef ShaderTypes_hh +#define ShaderTypes_hh + +#include + +struct Vertex { + float2 position; + float2 textureCoordinate; +}; + +struct RenderParameters { + /// Projection matrix. + metal::float4x4 projection; +}; + +#endif /* ShaderTypes_hh */ diff --git a/MetaballsKit/Shaders.metal b/MetaballsKit/Shaders.metal index 4247fb7..52cd4b4 100644 --- a/MetaballsKit/Shaders.metal +++ b/MetaballsKit/Shaders.metal @@ -7,13 +7,9 @@ // #include +#include "ShaderTypes.hh" using namespace metal; -struct Vertex { - float2 position; - float2 textureCoordinate; -}; - // From HelloCompute sample code project. // Vertex shader outputs and per-fragmeht inputs. Includes clip-space position and vertex outputs interpolated by rasterizer and fed to each fragment genterated by clip-space primitives. struct RasterizerData { @@ -43,10 +39,6 @@ struct Parameters { float3x3 colorTransform; }; -struct RenderParameters { - float4x4 projection; -}; - typedef float3 Ball; #pragma mark - Vertex