diff --git a/Terrain.xcodeproj/project.pbxproj b/Terrain.xcodeproj/project.pbxproj index 9b7a04e..db2f5d3 100644 --- a/Terrain.xcodeproj/project.pbxproj +++ b/Terrain.xcodeproj/project.pbxproj @@ -17,6 +17,12 @@ C0C15AA6218DF065007494E2 /* Shaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = C0C15AA5218DF065007494E2 /* Shaders.metal */; }; C0C15AAA218E2472007494E2 /* MetalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C0C15AA8218E2472007494E2 /* MetalKit.framework */; }; C0C15AAB218E2472007494E2 /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C0C15AA9218E2472007494E2 /* Metal.framework */; }; + C0C15AB3218E2A90007494E2 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C15AB2218E2A90007494E2 /* AppDelegate.swift */; }; + C0C15AB5218E2A90007494E2 /* GameViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C15AB4218E2A90007494E2 /* GameViewController.swift */; }; + C0C15AB7218E2A90007494E2 /* Renderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0C15AB6218E2A90007494E2 /* Renderer.swift */; }; + C0C15AB9218E2A90007494E2 /* Shaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = C0C15AB8218E2A90007494E2 /* Shaders.metal */; }; + C0C15ABC218E2A90007494E2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C0C15ABB218E2A90007494E2 /* Assets.xcassets */; }; + C0C15ABF218E2A90007494E2 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C0C15ABD218E2A90007494E2 /* Main.storyboard */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -33,6 +39,16 @@ C0C15AA5218DF065007494E2 /* Shaders.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Shaders.metal; sourceTree = ""; }; C0C15AA8218E2472007494E2 /* MetalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MetalKit.framework; path = System/Library/Frameworks/MetalKit.framework; sourceTree = SDKROOT; }; C0C15AA9218E2472007494E2 /* Metal.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Metal.framework; path = System/Library/Frameworks/Metal.framework; sourceTree = SDKROOT; }; + C0C15AB0218E2A8F007494E2 /* Terrain2.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Terrain2.app; sourceTree = BUILT_PRODUCTS_DIR; }; + C0C15AB2218E2A90007494E2 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + C0C15AB4218E2A90007494E2 /* GameViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameViewController.swift; sourceTree = ""; }; + C0C15AB6218E2A90007494E2 /* Renderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Renderer.swift; sourceTree = ""; }; + C0C15AB8218E2A90007494E2 /* Shaders.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = Shaders.metal; sourceTree = ""; }; + C0C15ABA218E2A90007494E2 /* ShaderTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ShaderTypes.h; sourceTree = ""; }; + C0C15ABB218E2A90007494E2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + C0C15ABE218E2A90007494E2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + C0C15AC0218E2A90007494E2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + C0C15AC1218E2A90007494E2 /* Terrain2.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Terrain2.entitlements; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -45,6 +61,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + C0C15AAD218E2A8F007494E2 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -52,6 +75,7 @@ isa = PBXGroup; children = ( C0C15A8C218DDD85007494E2 /* Terrain */, + C0C15AB1218E2A90007494E2 /* Terrain2 */, C0C15A8B218DDD85007494E2 /* Products */, C0C15AA7218E2472007494E2 /* Frameworks */, ); @@ -61,6 +85,7 @@ isa = PBXGroup; children = ( C0C15A8A218DDD85007494E2 /* Terrain.app */, + C0C15AB0218E2A8F007494E2 /* Terrain2.app */, ); name = Products; sourceTree = ""; @@ -91,6 +116,22 @@ name = Frameworks; sourceTree = ""; }; + C0C15AB1218E2A90007494E2 /* Terrain2 */ = { + isa = PBXGroup; + children = ( + C0C15AB2218E2A90007494E2 /* AppDelegate.swift */, + C0C15AB4218E2A90007494E2 /* GameViewController.swift */, + C0C15AB6218E2A90007494E2 /* Renderer.swift */, + C0C15AB8218E2A90007494E2 /* Shaders.metal */, + C0C15ABA218E2A90007494E2 /* ShaderTypes.h */, + C0C15ABB218E2A90007494E2 /* Assets.xcassets */, + C0C15ABD218E2A90007494E2 /* Main.storyboard */, + C0C15AC0218E2A90007494E2 /* Info.plist */, + C0C15AC1218E2A90007494E2 /* Terrain2.entitlements */, + ); + path = Terrain2; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -111,6 +152,23 @@ productReference = C0C15A8A218DDD85007494E2 /* Terrain.app */; productType = "com.apple.product-type.application"; }; + C0C15AAF218E2A8F007494E2 /* Terrain2 */ = { + isa = PBXNativeTarget; + buildConfigurationList = C0C15AC2218E2A90007494E2 /* Build configuration list for PBXNativeTarget "Terrain2" */; + buildPhases = ( + C0C15AAC218E2A8F007494E2 /* Sources */, + C0C15AAD218E2A8F007494E2 /* Frameworks */, + C0C15AAE218E2A8F007494E2 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Terrain2; + productName = Terrain2; + productReference = C0C15AB0218E2A8F007494E2 /* Terrain2.app */; + productType = "com.apple.product-type.application"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -124,6 +182,9 @@ C0C15A89218DDD85007494E2 = { CreatedOnToolsVersion = 10.0; }; + C0C15AAF218E2A8F007494E2 = { + CreatedOnToolsVersion = 10.0; + }; }; }; buildConfigurationList = C0C15A85218DDD85007494E2 /* Build configuration list for PBXProject "Terrain" */; @@ -140,6 +201,7 @@ projectRoot = ""; targets = ( C0C15A89218DDD85007494E2 /* Terrain */, + C0C15AAF218E2A8F007494E2 /* Terrain2 */, ); }; /* End PBXProject section */ @@ -154,6 +216,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + C0C15AAE218E2A8F007494E2 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + C0C15ABC218E2A90007494E2 /* Assets.xcassets in Resources */, + C0C15ABF218E2A90007494E2 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ @@ -170,6 +241,17 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + C0C15AAC218E2A8F007494E2 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + C0C15AB9218E2A90007494E2 /* Shaders.metal in Sources */, + C0C15AB5218E2A90007494E2 /* GameViewController.swift in Sources */, + C0C15AB7218E2A90007494E2 /* Renderer.swift in Sources */, + C0C15AB3218E2A90007494E2 /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ /* Begin PBXVariantGroup section */ @@ -181,6 +263,14 @@ name = MainMenu.xib; sourceTree = ""; }; + C0C15ABD218E2A90007494E2 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + C0C15ABE218E2A90007494E2 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; /* End PBXVariantGroup section */ /* Begin XCBuildConfiguration section */ @@ -337,6 +427,46 @@ }; name = Release; }; + C0C15AC3218E2A90007494E2 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = Terrain2/Terrain2.entitlements; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEVELOPMENT_TEAM = 78372RE6B4; + INFOPLIST_FILE = Terrain2/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = me.erynwells.Terrain2; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = Terrain/ShaderTypes.h; + SWIFT_VERSION = 4.2; + }; + name = Debug; + }; + C0C15AC4218E2A90007494E2 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_ENTITLEMENTS = Terrain2/Terrain2.entitlements; + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + DEVELOPMENT_TEAM = 78372RE6B4; + INFOPLIST_FILE = Terrain2/Info.plist; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = me.erynwells.Terrain2; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OBJC_BRIDGING_HEADER = Terrain/ShaderTypes.h; + SWIFT_VERSION = 4.2; + }; + name = Release; + }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -358,6 +488,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + C0C15AC2218E2A90007494E2 /* Build configuration list for PBXNativeTarget "Terrain2" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + C0C15AC3218E2A90007494E2 /* Debug */, + C0C15AC4218E2A90007494E2 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ }; rootObject = C0C15A82218DDD85007494E2 /* Project object */; diff --git a/Terrain2/AppDelegate.swift b/Terrain2/AppDelegate.swift new file mode 100644 index 0000000..92f5741 --- /dev/null +++ b/Terrain2/AppDelegate.swift @@ -0,0 +1,28 @@ +// +// AppDelegate.swift +// Terrain2 +// +// Created by Eryn Wells on 11/3/18. +// Copyright © 2018 Eryn Wells. All rights reserved. +// + +import Cocoa + +@NSApplicationMain +class AppDelegate: NSObject, NSApplicationDelegate { + + @IBOutlet weak var window: NSWindow! + + func applicationDidFinishLaunching(_ aNotification: Notification) { + + } + + func applicationWillTerminate(_ aNotification: Notification) { + + } + + func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { + return true + } + +} diff --git a/Terrain2/Assets.xcassets/AppIcon.appiconset/Contents.json b/Terrain2/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/Terrain2/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "images" : [ + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "2x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Terrain2/Assets.xcassets/ColorMap.textureset/Contents.json b/Terrain2/Assets.xcassets/ColorMap.textureset/Contents.json new file mode 100644 index 0000000..702494c --- /dev/null +++ b/Terrain2/Assets.xcassets/ColorMap.textureset/Contents.json @@ -0,0 +1,17 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + }, + "properties" : { + "origin" : "bottom-left", + "interpretation" : "non-premultiplied-colors" + }, + "textures" : [ + { + "idiom" : "universal", + "filename" : "Universal.mipmapset" + } + ] +} + diff --git a/Terrain2/Assets.xcassets/ColorMap.textureset/Universal.mipmapset/ColorMap.png b/Terrain2/Assets.xcassets/ColorMap.textureset/Universal.mipmapset/ColorMap.png new file mode 100644 index 0000000..ddf9519 Binary files /dev/null and b/Terrain2/Assets.xcassets/ColorMap.textureset/Universal.mipmapset/ColorMap.png differ diff --git a/Terrain2/Assets.xcassets/ColorMap.textureset/Universal.mipmapset/Contents.json b/Terrain2/Assets.xcassets/ColorMap.textureset/Universal.mipmapset/Contents.json new file mode 100644 index 0000000..83ae34b --- /dev/null +++ b/Terrain2/Assets.xcassets/ColorMap.textureset/Universal.mipmapset/Contents.json @@ -0,0 +1,12 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + }, + "levels" : [ + { + "filename" : "ColorMap.png", + "mipmap-level" : "base" + } + ] +} diff --git a/Terrain2/Assets.xcassets/Contents.json b/Terrain2/Assets.xcassets/Contents.json new file mode 100644 index 0000000..da4a164 --- /dev/null +++ b/Terrain2/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Terrain2/Base.lproj/Main.storyboard b/Terrain2/Base.lproj/Main.storyboard new file mode 100644 index 0000000..88d3f2f --- /dev/null +++ b/Terrain2/Base.lproj/Main.storyboard @@ -0,0 +1,717 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Terrain2/GameViewController.swift b/Terrain2/GameViewController.swift new file mode 100644 index 0000000..d3086bd --- /dev/null +++ b/Terrain2/GameViewController.swift @@ -0,0 +1,45 @@ +// +// GameViewController.swift +// Terrain2 +// +// Created by Eryn Wells on 11/3/18. +// Copyright © 2018 Eryn Wells. All rights reserved. +// + +import Cocoa +import MetalKit + +// Our macOS specific view controller +class GameViewController: NSViewController { + + var renderer: Renderer! + var mtkView: MTKView! + + override func viewDidLoad() { + super.viewDidLoad() + + guard let mtkView = self.view as? MTKView else { + print("View attached to GameViewController is not an MTKView") + return + } + + // Select the device to render with. We choose the default device + guard let defaultDevice = MTLCreateSystemDefaultDevice() else { + print("Metal is not supported on this device") + return + } + + mtkView.device = defaultDevice + + guard let newRenderer = Renderer(metalKitView: mtkView) else { + print("Renderer cannot be initialized") + return + } + + renderer = newRenderer + + renderer.mtkView(mtkView, drawableSizeWillChange: mtkView.drawableSize) + + mtkView.delegate = renderer + } +} diff --git a/Terrain2/Info.plist b/Terrain2/Info.plist new file mode 100644 index 0000000..eca6f87 --- /dev/null +++ b/Terrain2/Info.plist @@ -0,0 +1,32 @@ + + + + + CFBundleDevelopmentRegion + $(DEVELOPMENT_LANGUAGE) + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2018 Eryn Wells. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/Terrain2/Renderer.swift b/Terrain2/Renderer.swift new file mode 100644 index 0000000..9886260 --- /dev/null +++ b/Terrain2/Renderer.swift @@ -0,0 +1,326 @@ +// +// Renderer.swift +// Terrain2 +// +// Created by Eryn Wells on 11/3/18. +// Copyright © 2018 Eryn Wells. All rights reserved. +// + +// Our platform independent renderer class + +import Metal +import MetalKit +import simd + +// The 256 byte aligned size of our uniform structure +let alignedUniformsSize = (MemoryLayout.size & ~0xFF) + 0x100 + +let maxBuffersInFlight = 3 + +enum RendererError: Error { + case badVertexDescriptor +} + +class Renderer: NSObject, MTKViewDelegate { + + public let device: MTLDevice + let commandQueue: MTLCommandQueue + var dynamicUniformBuffer: MTLBuffer + var pipelineState: MTLRenderPipelineState + var depthState: MTLDepthStencilState + var colorMap: MTLTexture + + let inFlightSemaphore = DispatchSemaphore(value: maxBuffersInFlight) + + var uniformBufferOffset = 0 + + var uniformBufferIndex = 0 + + var uniforms: UnsafeMutablePointer + + var projectionMatrix: matrix_float4x4 = matrix_float4x4() + + var rotation: Float = 0 + + var mesh: MTKMesh + + init?(metalKitView: MTKView) { + self.device = metalKitView.device! + self.commandQueue = self.device.makeCommandQueue()! + + let uniformBufferSize = alignedUniformsSize * maxBuffersInFlight + + self.dynamicUniformBuffer = self.device.makeBuffer(length:uniformBufferSize, + options:[MTLResourceOptions.storageModeShared])! + + self.dynamicUniformBuffer.label = "UniformBuffer" + + uniforms = UnsafeMutableRawPointer(dynamicUniformBuffer.contents()).bindMemory(to:Uniforms.self, capacity:1) + + metalKitView.depthStencilPixelFormat = MTLPixelFormat.depth32Float_stencil8 + metalKitView.colorPixelFormat = MTLPixelFormat.bgra8Unorm_srgb + metalKitView.sampleCount = 1 + + let mtlVertexDescriptor = Renderer.buildMetalVertexDescriptor() + + do { + pipelineState = try Renderer.buildRenderPipelineWithDevice(device: device, + metalKitView: metalKitView, + mtlVertexDescriptor: mtlVertexDescriptor) + } catch { + print("Unable to compile render pipeline state. Error info: \(error)") + return nil + } + + let depthStateDesciptor = MTLDepthStencilDescriptor() + depthStateDesciptor.depthCompareFunction = MTLCompareFunction.less + depthStateDesciptor.isDepthWriteEnabled = true + self.depthState = device.makeDepthStencilState(descriptor:depthStateDesciptor)! + + do { + mesh = try Renderer.buildMesh(device: device, mtlVertexDescriptor: mtlVertexDescriptor) + } catch { + print("Unable to build MetalKit Mesh. Error info: \(error)") + return nil + } + + do { + colorMap = try Renderer.loadTexture(device: device, textureName: "ColorMap") + } catch { + print("Unable to load texture. Error info: \(error)") + return nil + } + + super.init() + + } + + class func buildMetalVertexDescriptor() -> MTLVertexDescriptor { + // Creete a Metal vertex descriptor specifying how vertices will by laid out for input into our render + // pipeline and how we'll layout our Model IO vertices + + let mtlVertexDescriptor = MTLVertexDescriptor() + + mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].format = MTLVertexFormat.float3 + mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].offset = 0 + mtlVertexDescriptor.attributes[VertexAttribute.position.rawValue].bufferIndex = BufferIndex.meshPositions.rawValue + + mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].format = MTLVertexFormat.float2 + mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].offset = 0 + mtlVertexDescriptor.attributes[VertexAttribute.texcoord.rawValue].bufferIndex = BufferIndex.meshGenerics.rawValue + + mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stride = 12 + mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stepRate = 1 + mtlVertexDescriptor.layouts[BufferIndex.meshPositions.rawValue].stepFunction = MTLVertexStepFunction.perVertex + + mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stride = 8 + mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepRate = 1 + mtlVertexDescriptor.layouts[BufferIndex.meshGenerics.rawValue].stepFunction = MTLVertexStepFunction.perVertex + + return mtlVertexDescriptor + } + + class func buildRenderPipelineWithDevice(device: MTLDevice, + metalKitView: MTKView, + mtlVertexDescriptor: MTLVertexDescriptor) throws -> MTLRenderPipelineState { + /// Build a render state pipeline object + + let library = device.makeDefaultLibrary() + + let vertexFunction = library?.makeFunction(name: "vertexShader") + let fragmentFunction = library?.makeFunction(name: "fragmentShader") + + let pipelineDescriptor = MTLRenderPipelineDescriptor() + pipelineDescriptor.label = "RenderPipeline" + pipelineDescriptor.sampleCount = metalKitView.sampleCount + pipelineDescriptor.vertexFunction = vertexFunction + pipelineDescriptor.fragmentFunction = fragmentFunction + pipelineDescriptor.vertexDescriptor = mtlVertexDescriptor + + pipelineDescriptor.colorAttachments[0].pixelFormat = metalKitView.colorPixelFormat + pipelineDescriptor.depthAttachmentPixelFormat = metalKitView.depthStencilPixelFormat + pipelineDescriptor.stencilAttachmentPixelFormat = metalKitView.depthStencilPixelFormat + + return try device.makeRenderPipelineState(descriptor: pipelineDescriptor) + } + + class func buildMesh(device: MTLDevice, + mtlVertexDescriptor: MTLVertexDescriptor) throws -> MTKMesh { + /// Create and condition mesh data to feed into a pipeline using the given vertex descriptor + + let metalAllocator = MTKMeshBufferAllocator(device: device) + + let mdlMesh = MDLMesh.newBox(withDimensions: float3(4, 4, 4), + segments: uint3(2, 2, 2), + geometryType: MDLGeometryType.triangles, + inwardNormals:false, + allocator: metalAllocator) + + let mdlVertexDescriptor = MTKModelIOVertexDescriptorFromMetal(mtlVertexDescriptor) + + guard let attributes = mdlVertexDescriptor.attributes as? [MDLVertexAttribute] else { + throw RendererError.badVertexDescriptor + } + attributes[VertexAttribute.position.rawValue].name = MDLVertexAttributePosition + attributes[VertexAttribute.texcoord.rawValue].name = MDLVertexAttributeTextureCoordinate + + mdlMesh.vertexDescriptor = mdlVertexDescriptor + + return try MTKMesh(mesh:mdlMesh, device:device) + } + + class func loadTexture(device: MTLDevice, + textureName: String) throws -> MTLTexture { + /// Load texture data with optimal parameters for sampling + + let textureLoader = MTKTextureLoader(device: device) + + let textureLoaderOptions = [ + MTKTextureLoader.Option.textureUsage: NSNumber(value: MTLTextureUsage.shaderRead.rawValue), + MTKTextureLoader.Option.textureStorageMode: NSNumber(value: MTLStorageMode.`private`.rawValue) + ] + + return try textureLoader.newTexture(name: textureName, + scaleFactor: 1.0, + bundle: nil, + options: textureLoaderOptions) + + } + + private func updateDynamicBufferState() { + /// Update the state of our uniform buffers before rendering + + uniformBufferIndex = (uniformBufferIndex + 1) % maxBuffersInFlight + + uniformBufferOffset = alignedUniformsSize * uniformBufferIndex + + uniforms = UnsafeMutableRawPointer(dynamicUniformBuffer.contents() + uniformBufferOffset).bindMemory(to:Uniforms.self, capacity:1) + } + + private func updateGameState() { + /// Update any game state before rendering + + uniforms[0].projectionMatrix = projectionMatrix + + let rotationAxis = float3(1, 1, 0) + let modelMatrix = matrix4x4_rotation(radians: rotation, axis: rotationAxis) + let viewMatrix = matrix4x4_translation(0.0, 0.0, -8.0) + uniforms[0].modelViewMatrix = simd_mul(viewMatrix, modelMatrix) + rotation += 0.01 + } + + func draw(in view: MTKView) { + /// Per frame updates hare + + _ = inFlightSemaphore.wait(timeout: DispatchTime.distantFuture) + + if let commandBuffer = commandQueue.makeCommandBuffer() { + let semaphore = inFlightSemaphore + commandBuffer.addCompletedHandler { (_ commandBuffer)-> Swift.Void in + semaphore.signal() + } + + self.updateDynamicBufferState() + + self.updateGameState() + + /// Delay getting the currentRenderPassDescriptor until we absolutely need it to avoid + /// holding onto the drawable and blocking the display pipeline any longer than necessary + let renderPassDescriptor = view.currentRenderPassDescriptor + + if let renderPassDescriptor = renderPassDescriptor { + + /// Final pass rendering code here + if let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) { + renderEncoder.label = "Primary Render Encoder" + + renderEncoder.pushDebugGroup("Draw Box") + + renderEncoder.setCullMode(.back) + + renderEncoder.setFrontFacing(.counterClockwise) + + renderEncoder.setRenderPipelineState(pipelineState) + + renderEncoder.setDepthStencilState(depthState) + + renderEncoder.setVertexBuffer(dynamicUniformBuffer, offset:uniformBufferOffset, index: BufferIndex.uniforms.rawValue) + renderEncoder.setFragmentBuffer(dynamicUniformBuffer, offset:uniformBufferOffset, index: BufferIndex.uniforms.rawValue) + + for (index, element) in mesh.vertexDescriptor.layouts.enumerated() { + guard let layout = element as? MDLVertexBufferLayout else { + return + } + + if layout.stride != 0 { + let buffer = mesh.vertexBuffers[index] + renderEncoder.setVertexBuffer(buffer.buffer, offset:buffer.offset, index: index) + } + } + + renderEncoder.setFragmentTexture(colorMap, index: TextureIndex.color.rawValue) + + for submesh in mesh.submeshes { + renderEncoder.drawIndexedPrimitives(type: submesh.primitiveType, + indexCount: submesh.indexCount, + indexType: submesh.indexType, + indexBuffer: submesh.indexBuffer.buffer, + indexBufferOffset: submesh.indexBuffer.offset) + + } + + renderEncoder.popDebugGroup() + + renderEncoder.endEncoding() + + if let drawable = view.currentDrawable { + commandBuffer.present(drawable) + } + } + } + + commandBuffer.commit() + } + } + + func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) { + /// Respond to drawable size or orientation changes here + + let aspect = Float(size.width) / Float(size.height) + projectionMatrix = matrix_perspective_right_hand(fovyRadians: radians_from_degrees(65), aspectRatio:aspect, nearZ: 0.1, farZ: 100.0) + } +} + +// Generic matrix math utility functions +func matrix4x4_rotation(radians: Float, axis: float3) -> matrix_float4x4 { + let unitAxis = normalize(axis) + let ct = cosf(radians) + let st = sinf(radians) + let ci = 1 - ct + let x = unitAxis.x, y = unitAxis.y, z = unitAxis.z + return matrix_float4x4.init(columns:(vector_float4( ct + x * x * ci, y * x * ci + z * st, z * x * ci - y * st, 0), + vector_float4(x * y * ci - z * st, ct + y * y * ci, z * y * ci + x * st, 0), + vector_float4(x * z * ci + y * st, y * z * ci - x * st, ct + z * z * ci, 0), + vector_float4( 0, 0, 0, 1))) +} + +func matrix4x4_translation(_ translationX: Float, _ translationY: Float, _ translationZ: Float) -> matrix_float4x4 { + return matrix_float4x4.init(columns:(vector_float4(1, 0, 0, 0), + vector_float4(0, 1, 0, 0), + vector_float4(0, 0, 1, 0), + vector_float4(translationX, translationY, translationZ, 1))) +} + +func matrix_perspective_right_hand(fovyRadians fovy: Float, aspectRatio: Float, nearZ: Float, farZ: Float) -> matrix_float4x4 { + let ys = 1 / tanf(fovy * 0.5) + let xs = ys / aspectRatio + let zs = farZ / (nearZ - farZ) + return matrix_float4x4.init(columns:(vector_float4(xs, 0, 0, 0), + vector_float4( 0, ys, 0, 0), + vector_float4( 0, 0, zs, -1), + vector_float4( 0, 0, zs * nearZ, 0))) +} + +func radians_from_degrees(_ degrees: Float) -> Float { + return (degrees / 180) * .pi +} diff --git a/Terrain2/ShaderTypes.h b/Terrain2/ShaderTypes.h new file mode 100644 index 0000000..b1e3ff6 --- /dev/null +++ b/Terrain2/ShaderTypes.h @@ -0,0 +1,49 @@ +// +// ShaderTypes.h +// Terrain2 +// +// Created by Eryn Wells on 11/3/18. +// Copyright © 2018 Eryn Wells. All rights reserved. +// + +// +// Header containing types and enum constants shared between Metal shaders and Swift/ObjC source +// +#ifndef ShaderTypes_h +#define ShaderTypes_h + +#ifdef __METAL_VERSION__ +#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type +#define NSInteger metal::int32_t +#else +#import +#endif + +#include + +typedef NS_ENUM(NSInteger, BufferIndex) +{ + BufferIndexMeshPositions = 0, + BufferIndexMeshGenerics = 1, + BufferIndexUniforms = 2 +}; + +typedef NS_ENUM(NSInteger, VertexAttribute) +{ + VertexAttributePosition = 0, + VertexAttributeTexcoord = 1, +}; + +typedef NS_ENUM(NSInteger, TextureIndex) +{ + TextureIndexColor = 0, +}; + +typedef struct +{ + matrix_float4x4 projectionMatrix; + matrix_float4x4 modelViewMatrix; +} Uniforms; + +#endif /* ShaderTypes_h */ + diff --git a/Terrain2/Shaders.metal b/Terrain2/Shaders.metal new file mode 100644 index 0000000..36759a9 --- /dev/null +++ b/Terrain2/Shaders.metal @@ -0,0 +1,54 @@ +// +// Shaders.metal +// Terrain2 +// +// Created by Eryn Wells on 11/3/18. +// Copyright © 2018 Eryn Wells. All rights reserved. +// + +// File for Metal kernel and shader functions + +#include +#include + +// Including header shared between this Metal shader code and Swift/C code executing Metal API commands +#import "ShaderTypes.h" + +using namespace metal; + +typedef struct +{ + float3 position [[attribute(VertexAttributePosition)]]; + float2 texCoord [[attribute(VertexAttributeTexcoord)]]; +} Vertex; + +typedef struct +{ + float4 position [[position]]; + float2 texCoord; +} ColorInOut; + +vertex ColorInOut vertexShader(Vertex in [[stage_in]], + constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]]) +{ + ColorInOut out; + + float4 position = float4(in.position, 1.0); + out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * position; + out.texCoord = in.texCoord; + + return out; +} + +fragment float4 fragmentShader(ColorInOut in [[stage_in]], + constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]], + texture2d colorMap [[ texture(TextureIndexColor) ]]) +{ + constexpr sampler colorSampler(mip_filter::linear, + mag_filter::linear, + min_filter::linear); + + half4 colorSample = colorMap.sample(colorSampler, in.texCoord.xy); + + return float4(colorSample); +} diff --git a/Terrain2/Terrain2.entitlements b/Terrain2/Terrain2.entitlements new file mode 100644 index 0000000..f2ef3ae --- /dev/null +++ b/Terrain2/Terrain2.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.security.app-sandbox + + com.apple.security.files.user-selected.read-only + + +