Add shaders for marching squares

This commit is contained in:
Eryn Wells 2018-10-14 12:16:21 -07:00
parent 1699e3ae14
commit b2f19dffbf
4 changed files with 80 additions and 9 deletions

View file

@ -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 = "<group>"; };
C04CDEB52173ABD50018C4DA /* MarchingSquares.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = MarchingSquares.metal; sourceTree = "<group>"; };
C04CDEB92173ABE50018C4DA /* ShaderTypes.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = ShaderTypes.hh; sourceTree = "<group>"; };
C091616F1F3F5AE6009C4263 /* PreferencesViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreferencesViewController.swift; sourceTree = "<group>"; };
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 = "<group>"; };
@ -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 */,

View file

@ -0,0 +1,45 @@
//
// MarchingSquares.metal
// Metaballs
//
// Created by Eryn Wells on 10/14/18.
// Copyright © 2018 Eryn Wells. All rights reserved.
//
#include <metal_stdlib>
#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;
}

View file

@ -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 <metal_stdlib>
struct Vertex {
float2 position;
float2 textureCoordinate;
};
struct RenderParameters {
/// Projection matrix.
metal::float4x4 projection;
};
#endif /* ShaderTypes_hh */

View file

@ -7,13 +7,9 @@
//
#include <metal_stdlib>
#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