Skip to content

Commit

Permalink
Update Metal version to Swift 4/Xcode 9
Browse files Browse the repository at this point in the history
  • Loading branch information
kosua20 committed Sep 24, 2017
1 parent a80eace commit ee85da2
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 39 deletions.
23 changes: 20 additions & 3 deletions metal/DragonMetal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0800;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Simon Rodriguez";
TargetAttributes = {
F45BB55E1DC2904900EA83F0 = {
CreatedOnToolsVersion = 8.0;
DevelopmentTeam = MM696H2U5V;
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -253,15 +254,21 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_SUSPICIOUS_MOVES = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
Expand Down Expand Up @@ -291,6 +298,7 @@
SDKROOT = macosx;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
};
name = Debug;
};
Expand All @@ -303,15 +311,21 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_SUSPICIOUS_MOVES = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
Expand All @@ -333,6 +347,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
};
name = Release;
};
Expand All @@ -346,7 +361,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.simonrodriguez.DragonMetal;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -360,7 +376,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.simonrodriguez.DragonMetal;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_SWIFT3_OBJC_INFERENCE = Off;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "DragonMetal/Object.swift"
timestampString = "519255579.335459"
timestampString = "523304970.283512"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "45"
Expand All @@ -26,11 +26,11 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "DragonMetal/GameViewController.swift"
timestampString = "519250933.568592"
timestampString = "523304970.283589"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "103"
endingLineNumber = "103"
startingLineNumber = "105"
endingLineNumber = "105"
landmarkName = "draw(in:)"
landmarkType = "7">
<Locations>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0810"
LastUpgradeVersion = "0900"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -10,6 +10,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
Expand All @@ -29,6 +30,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
5 changes: 4 additions & 1 deletion metal/DragonMetal/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ class GameViewController: NSViewController, MTKViewDelegate {
renderer.update(timeStep : step)

// Create a command buffer.
let commandBuffer = commandQueue.makeCommandBuffer()
guard let commandBuffer = commandQueue.makeCommandBuffer() else {
print("Couldn't make command buffer")
return
}
commandBuffer.label = "Frame command buffer"

// Semaphore magic (signal when the command buffer has been processed by the GPU).
Expand Down
8 changes: 4 additions & 4 deletions metal/DragonMetal/MatrixUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func matrix_block3x3(m : matrix_float4x4) -> matrix_float3x3 {


func matrix_transpose_inverse(m : matrix_float3x3) -> matrix_float3x3 {
return matrix_transpose(matrix_invert(m))
return m.inverse.transpose
}


func matrix_inverse_transpose(m : matrix_float4x4) -> matrix_float4x4 {
return matrix_invert(matrix_transpose(m))
return m.transpose.inverse
}


Expand Down Expand Up @@ -97,12 +97,12 @@ func matrix_rotation(angle: Float, axis: float3) -> matrix_float4x4 {


func matrix_scaling(scale: Float) -> matrix_float4x4 {
return matrix_from_diagonal(vector4(scale, scale, scale, 1.0))
return simd_float4x4(diagonal: vector4(scale, scale, scale, 1.0))
}


func matrix_translation(t: float3) -> matrix_float4x4 {
return matrix_from_columns(float4(1.0,0.0,0.0,0.0), float4(0.0,1.0,0.0,0.0), float4(0.0,0.0,1.0,0.0), float4(t.x,t.y,t.z,1.0))
return simd_float4x4(float4(1.0,0.0,0.0,0.0), float4(0.0,1.0,0.0,0.0), float4(0.0,0.0,1.0,0.0), float4(t.x,t.y,t.z,1.0))
}


Expand Down
14 changes: 7 additions & 7 deletions metal/DragonMetal/Object.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Object {
func update(camera: Camera, vpLight: matrix_float4x4){
// Compute all transformations for this frame.
parameters.mv = matrix_multiply(camera.viewMatrix, self.model)
parameters.invmv = matrix_transpose(matrix_invert(parameters.mv))
parameters.invmv = parameters.mv.inverse.transpose
parameters.mvp = matrix_multiply(camera.projectionMatrix, parameters.mv)
parameters.mvpLight = matrix_multiply(vpLight, self.model)
}
Expand All @@ -60,13 +60,13 @@ class Object {
renderEncoder.pushDebugGroup("Draw " + name)

// Set buffers.
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0)
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
// Uniforms for the vertex shader.
renderEncoder.setVertexBytes(&parameters, length: MemoryLayout<ObjectConstants>.stride, at: 1)
renderEncoder.setVertexBytes(&parameters, length: MemoryLayout<ObjectConstants>.stride, index: 1)
// Uniforms for the fragment shader.
var localConstants = constants
renderEncoder.setFragmentBytes(&localConstants, length: MemoryLayout<GlobalConstants>.stride, at: 0)
renderEncoder.setFragmentBytes(&material, length: MemoryLayout<MaterialConstants>.stride, at: 1)
renderEncoder.setFragmentBytes(&localConstants, length: MemoryLayout<GlobalConstants>.stride, index: 0)
renderEncoder.setFragmentBytes(&material, length: MemoryLayout<MaterialConstants>.stride, index: 1)
// Textures.
setFragmentTextures(encoder: renderEncoder)
// Draw.
Expand All @@ -80,9 +80,9 @@ class Object {
renderEncoder.pushDebugGroup("Shadow " + name)

// Set buffers.
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, at: 0)
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
// Uniforms for the fragment shader.
renderEncoder.setVertexBytes(&parameters, length: MemoryLayout<ObjectConstants>.stride, at: 1)
renderEncoder.setVertexBytes(&parameters, length: MemoryLayout<ObjectConstants>.stride, index: 1)
// Draw.
renderEncoder.drawIndexedPrimitives(type: .triangle, indexCount: indexCount, indexType: .uint32, indexBuffer: indexBuffer, indexBufferOffset: 0)

Expand Down
35 changes: 22 additions & 13 deletions metal/DragonMetal/Renderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Renderer {
private var skybox: Object! = nil

// Light parameters.
private var lightMatrix: matrix_float4x4
private var lightMatrix = matrix_float4x4()
private let worldLightDir = normalize(float4(1.0,1.0,1.0,0.0))

// Final pass states.
Expand All @@ -48,7 +48,7 @@ class Renderer {
shadowDepthState = device.makeDepthStencilState(descriptor: depthStencilDescriptor)

// Load shaders from the library.
let defaultLibrary = device.newDefaultLibrary()!
let defaultLibrary = device.makeDefaultLibrary()!
let objectVertexProgram = defaultLibrary.makeFunction(name: "objectVertex")!
let objectFragmentProgram = defaultLibrary.makeFunction(name: "objectFragment")!
let skyboxVertexProgram = defaultLibrary.makeFunction(name: "skyboxVertex")!
Expand Down Expand Up @@ -77,7 +77,10 @@ class Renderer {
shadowTextureDescriptor.storageMode = .private
// Usage: as a render target destination and as an input texture in shaders.
shadowTextureDescriptor.usage = MTLTextureUsage.renderTarget.union(MTLTextureUsage.shaderRead)
let shadowTexture = device.makeTexture(descriptor: shadowTextureDescriptor)
guard let shadowTexture = device.makeTexture(descriptor: shadowTextureDescriptor) else {
print("Couldn't make shadow texture.")
return
}

// Create the shadow render pass descriptor.
shadowRenderDescriptor = MTLRenderPassDescriptor()
Expand Down Expand Up @@ -144,22 +147,28 @@ class Renderer {

// Shadow pass.
// Create a render command encoder.
var renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: shadowRenderDescriptor)
renderEncoder.label = "Shadow pass"
guard let renderEncoderShadow = commandBuffer.makeRenderCommandEncoder(descriptor: shadowRenderDescriptor) else {
print("Error encoding shadow pass.")
return
}
renderEncoderShadow.label = "Shadow pass"
// Set states.
renderEncoder.setFrontFacing(.counterClockwise)
renderEncoder.setCullMode(.back)
renderEncoder.setDepthStencilState(shadowDepthState)
renderEncoder.setRenderPipelineState(shadowPipeline)
renderEncoderShadow.setFrontFacing(.counterClockwise)
renderEncoderShadow.setCullMode(.back)
renderEncoderShadow.setDepthStencilState(shadowDepthState)
renderEncoderShadow.setRenderPipelineState(shadowPipeline)
// Render.
dragon.encodeShadow(renderEncoder: renderEncoder)
monkey.encodeShadow(renderEncoder: renderEncoder)
dragon.encodeShadow(renderEncoder: renderEncoderShadow)
monkey.encodeShadow(renderEncoder: renderEncoderShadow)
// Pass finished.
renderEncoder.endEncoding()
renderEncoderShadow.endEncoding()

// Final pass.
// New encoder.
renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor)
guard let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) else {
print("Error encoding main pass.")
return
}
renderEncoder.label = "Main pass"
// Set states.
renderEncoder.setFrontFacing(.counterClockwise)
Expand Down
6 changes: 3 additions & 3 deletions metal/DragonMetal/SceneObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class SceneObject : Object {
}

override func setFragmentTextures(encoder: MTLRenderCommandEncoder) {
encoder.setFragmentTexture(textureColor, at: 0)
encoder.setFragmentTexture(textureNormal, at: 1)
encoder.setFragmentTexture(shadowMap, at: 2)
encoder.setFragmentTexture(textureColor, index: 0)
encoder.setFragmentTexture(textureNormal, index: 1)
encoder.setFragmentTexture(shadowMap, index: 2)
}

}
2 changes: 1 addition & 1 deletion metal/DragonMetal/SkyboxObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SkyboxObject : Object {
}

override func setFragmentTextures(encoder: MTLRenderCommandEncoder) {
encoder.setFragmentTexture(textureColor, at: 0)
encoder.setFragmentTexture(textureColor, index: 0)
}

}
17 changes: 15 additions & 2 deletions metal/DragonMetal/TextureUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@ import MetalKit
extension MTKTextureLoader {

func newTexture(withContentsOf url: URL, srgb: Bool)-> MTLTexture {
return try! self.newTexture(withContentsOf: url, options: [MTKTextureLoaderOptionGenerateMipmaps: NSNumber(booleanLiteral: true) ,MTKTextureLoaderOptionAllocateMipmaps: NSNumber(booleanLiteral: true), MTKTextureLoaderOptionOrigin : NSString(string: MTKTextureLoaderOriginFlippedVertically), MTKTextureLoaderOptionSRGB : NSNumber(booleanLiteral : srgb)])
return try! self.newTexture(URL: url,
options: [
MTKTextureLoader.Option.generateMipmaps: true,
MTKTextureLoader.Option.allocateMipmaps: true,
MTKTextureLoader.Option.origin: MTKTextureLoader.Origin.flippedVertically,
MTKTextureLoader.Option.SRGB : srgb
])
}

func newTextureCubemap(withContentsOf url: URL, srgb: Bool)-> MTLTexture {
return try! self.newTexture(withContentsOf: url, options: [MTKTextureLoaderOptionGenerateMipmaps: NSNumber(booleanLiteral: true) ,MTKTextureLoaderOptionAllocateMipmaps: NSNumber(booleanLiteral: true), MTKTextureLoaderOptionOrigin : NSString(string: MTKTextureLoaderOriginFlippedVertically), MTKTextureLoaderOptionSRGB : NSNumber(booleanLiteral : srgb), MTKTextureLoaderOptionCubeLayout : NSString(string: MTKTextureLoaderCubeLayoutVertical) ])
return try! self.newTexture(URL: url,
options: [
MTKTextureLoader.Option.generateMipmaps: true,
MTKTextureLoader.Option.allocateMipmaps: true,
MTKTextureLoader.Option.origin : MTKTextureLoader.Origin.flippedVertically,
MTKTextureLoader.Option.SRGB : srgb,
MTKTextureLoader.Option.cubeLayout : MTKTextureLoader.CubeLayout.vertical
])
}

}

0 comments on commit ee85da2

Please sign in to comment.