Skip to content

Commit

Permalink
Use proper bridging header for Metal objects
Browse files Browse the repository at this point in the history
  • Loading branch information
SaganRitual committed May 11, 2020
1 parent c7601dc commit 402379c
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 138 deletions.
30 changes: 16 additions & 14 deletions Foil.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@

/* Begin PBXBuildFile section */
D42B958B24648E2900F4B5B0 /* FoilShaders.metal in Sources */ = {isa = PBXBuildFile; fileRef = D42B958A24648E2900F4B5B0 /* FoilShaders.metal */; };
D462CC9B2464920600D79B48 /* FoilShaderTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = D462CC9A2464920600D79B48 /* FoilShaderTypes.swift */; };
D462CC9D2466652400D79B48 /* LikeObjcSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = D462CC9C2466652400D79B48 /* LikeObjcSync.swift */; };
D475693E2469259D000F87AE /* OFoilRenderer.m in Sources */ = {isa = PBXBuildFile; fileRef = D475693D2469259D000F87AE /* OFoilRenderer.m */; };
D4AC68B8245CF43F00917569 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AC68B7245CF43F00917569 /* AppDelegate.swift */; };
D4AC68BA245CF43F00917569 /* FoilViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AC68B9245CF43F00917569 /* FoilViewController.swift */; };
D4AC68BC245CF44000917569 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D4AC68BB245CF44000917569 /* Assets.xcassets */; };
D4AC68BF245CF44000917569 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D4AC68BD245CF44000917569 /* Main.storyboard */; };
D4AC68C9245CF57A00917569 /* FoilRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AC68C8245CF57A00917569 /* FoilRenderer.swift */; };
D4BE16FA2463D7390085FD3C /* MetalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4BE16F92463D7390085FD3C /* MetalKit.framework */; };
D4BE16FE2463F0E10085FD3C /* FoilKernels.metal in Sources */ = {isa = PBXBuildFile; fileRef = D4BE16FD2463F0E10085FD3C /* FoilKernels.metal */; };
D4BE17002463F23C0085FD3C /* FoilKernelTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4BE16FF2463F23C0085FD3C /* FoilKernelTypes.swift */; };
D4F680AC245FACB3000B8EF2 /* FoilSimulation.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4F680AB245FACB3000B8EF2 /* FoilSimulation.swift */; };
D4F680B02460A5E7000B8EF2 /* FoilKernelTypes.h in Sources */ = {isa = PBXBuildFile; fileRef = D4F680AF2460A5E7000B8EF2 /* FoilKernelTypes.h */; };
D4F680BE24639638000B8EF2 /* FoilMathUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4F680BD24639638000B8EF2 /* FoilMathUtilities.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
D42B958A24648E2900F4B5B0 /* FoilShaders.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = FoilShaders.metal; sourceTree = "<group>"; };
D42B958C24648EDE00F4B5B0 /* FoilShaderTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FoilShaderTypes.h; sourceTree = "<group>"; };
D462CC9A2464920600D79B48 /* FoilShaderTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoilShaderTypes.swift; sourceTree = "<group>"; };
D462CC9C2466652400D79B48 /* LikeObjcSync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LikeObjcSync.swift; sourceTree = "<group>"; };
D475693C2469259D000F87AE /* Foil-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Foil-Bridging-Header.h"; sourceTree = "<group>"; };
D475693D2469259D000F87AE /* OFoilRenderer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OFoilRenderer.m; sourceTree = "<group>"; };
D475693F24692638000F87AE /* FoilShaderTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FoilShaderTypes.h; sourceTree = "<group>"; };
D47569402469267E000F87AE /* FoilKernelTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FoilKernelTypes.h; sourceTree = "<group>"; };
D4AC68B4245CF43F00917569 /* Foil.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Foil.app; sourceTree = BUILT_PRODUCTS_DIR; };
D4AC68B7245CF43F00917569 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D4AC68B9245CF43F00917569 /* FoilViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoilViewController.swift; sourceTree = "<group>"; };
Expand All @@ -38,9 +38,7 @@
D4AC68C8245CF57A00917569 /* FoilRenderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoilRenderer.swift; sourceTree = "<group>"; };
D4BE16F92463D7390085FD3C /* MetalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MetalKit.framework; path = System/Library/Frameworks/MetalKit.framework; sourceTree = SDKROOT; };
D4BE16FD2463F0E10085FD3C /* FoilKernels.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = FoilKernels.metal; sourceTree = "<group>"; };
D4BE16FF2463F23C0085FD3C /* FoilKernelTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoilKernelTypes.swift; sourceTree = "<group>"; };
D4F680AB245FACB3000B8EF2 /* FoilSimulation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoilSimulation.swift; sourceTree = "<group>"; };
D4F680AF2460A5E7000B8EF2 /* FoilKernelTypes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FoilKernelTypes.h; sourceTree = "<group>"; };
D4F680BD24639638000B8EF2 /* FoilMathUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FoilMathUtilities.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -85,6 +83,8 @@
D4AC68C0245CF44000917569 /* Info.plist */,
D462CC9C2466652400D79B48 /* LikeObjcSync.swift */,
D4AC68BD245CF44000917569 /* Main.storyboard */,
D475693D2469259D000F87AE /* OFoilRenderer.m */,
D475693C2469259D000F87AE /* Foil-Bridging-Header.h */,
);
path = Foil;
sourceTree = "<group>";
Expand All @@ -95,8 +95,7 @@
D4F680BD24639638000B8EF2 /* FoilMathUtilities.swift */,
D4AC68C8245CF57A00917569 /* FoilRenderer.swift */,
D42B958A24648E2900F4B5B0 /* FoilShaders.metal */,
D42B958C24648EDE00F4B5B0 /* FoilShaderTypes.h */,
D462CC9A2464920600D79B48 /* FoilShaderTypes.swift */,
D475693F24692638000F87AE /* FoilShaderTypes.h */,
);
path = Renderer;
sourceTree = "<group>";
Expand All @@ -113,8 +112,7 @@
isa = PBXGroup;
children = (
D4BE16FD2463F0E10085FD3C /* FoilKernels.metal */,
D4F680AF2460A5E7000B8EF2 /* FoilKernelTypes.h */,
D4BE16FF2463F23C0085FD3C /* FoilKernelTypes.swift */,
D47569402469267E000F87AE /* FoilKernelTypes.h */,
D4F680AB245FACB3000B8EF2 /* FoilSimulation.swift */,
);
path = Simulation;
Expand Down Expand Up @@ -152,6 +150,7 @@
TargetAttributes = {
D4AC68B3245CF43F00917569 = {
CreatedOnToolsVersion = 11.2;
LastSwiftMigration = 1120;
};
};
};
Expand Down Expand Up @@ -194,12 +193,10 @@
D4AC68C9245CF57A00917569 /* FoilRenderer.swift in Sources */,
D462CC9D2466652400D79B48 /* LikeObjcSync.swift in Sources */,
D4AC68BA245CF43F00917569 /* FoilViewController.swift in Sources */,
D4F680B02460A5E7000B8EF2 /* FoilKernelTypes.h in Sources */,
D42B958B24648E2900F4B5B0 /* FoilShaders.metal in Sources */,
D4BE17002463F23C0085FD3C /* FoilKernelTypes.swift in Sources */,
D4F680AC245FACB3000B8EF2 /* FoilSimulation.swift in Sources */,
D4F680BE24639638000B8EF2 /* FoilMathUtilities.swift in Sources */,
D462CC9B2464920600D79B48 /* FoilShaderTypes.swift in Sources */,
D475693E2469259D000F87AE /* OFoilRenderer.m in Sources */,
D4AC68B8245CF43F00917569 /* AppDelegate.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -337,6 +334,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Foil/Foil.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
Expand All @@ -349,6 +347,8 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.boringsoftware.Foil;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Foil/Foil-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
};
name = Debug;
Expand All @@ -357,6 +357,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Foil/Foil.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
Expand All @@ -369,6 +370,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.boringsoftware.Foil;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Foil/Foil-Bridging-Header.h";
SWIFT_VERSION = 5.0;
};
name = Release;
Expand Down
6 changes: 6 additions & 0 deletions Foil/Foil-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "../Renderer/FoilShaderTypes.h"
#import "../Simulation/FoilKernelTypes.h"
8 changes: 4 additions & 4 deletions Foil/FoilViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ class FoilViewController: NSViewController, MTKViewDelegate {

precondition(!availableDevices.isEmpty, "Metal is not supported on this Mac")

computeDevice = availableDevices[RadeonGPUInMetalDevicesArray]
computeDevice = availableDevices[IntelGPUInMetalDevicesArray]
NSLog("Selected compute device: \(computeDevice.name)")

// Select renderer device
let rendererDevice = availableDevices[RadeonGPUInMetalDevicesArray]
let rendererDevice = availableDevices[IntelGPUInMetalDevicesArray]

if rendererDevice !== _view.device {
_view.device = rendererDevice;

NSLog("New render device: '\(rendererDevice.name)'")
NSLog("New render device: '\(rendererDevice.name)', drawableSize \(_view.drawableSize)")

renderer = FoilRenderer(_view)
renderer.drawableSizeWillChange(size: _view.drawableSize)
Expand Down Expand Up @@ -293,7 +293,7 @@ class FoilViewController: NSViewController, MTKViewDelegate {

// Simulate the frame and obtain the new positions for the update. If this is the final
// frame positionBuffer will be filled with the all positions used for the simulation
let positionBuffer = simulation.simulateFrameWithCommandBuffer(commandBuffer: commandBuffer)
let positionBuffer = simulation.simulateFrame(commandBuffer: commandBuffer)

// Render the updated positions (or all positions in the case that the simulation is complete)
renderer.drawWithCommandBuffer(
Expand Down
9 changes: 9 additions & 0 deletions Foil/OFoilRenderer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// OFoilRenderer.m
// Foil
//
// Created by Rob Bishop on 5/10/20.
// Copyright © 2020 Boring Software. All rights reserved.
//

#import <Foundation/Foundation.h>
40 changes: 17 additions & 23 deletions Renderer/FoilRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class FoilRenderer: NSObject {
// Size of gaussian map to create rounded smooth points
static let GaussianMapSize = 64

let gaussianMap: MTLTexture
var gaussianMap: MTLTexture!

var device: MTLDevice!
var commandQueue: MTLCommandQueue!
Expand All @@ -25,7 +25,7 @@ class FoilRenderer: NSObject {
var currentBufferIndex = 0

// Projection matrix calculated as a function of view size
var projectionMatrix: matrix_float4x4!
var projectionMatrix = matrix_float4x4()

var renderScale: Float = 0

Expand All @@ -38,13 +38,12 @@ class FoilRenderer: NSObject {
/// object will also be used to set the pixelFormat and other properties of the drawable
init(_ mtkView: MTKView) {
self.device = mtkView.device!
self.commandQueue = self.device.makeCommandQueue()!

self.gaussianMap = FoilRenderer.generateGaussianMap(self.device)

super.init()

self.loadMetal(mtkView: mtkView)

self.gaussianMap = FoilRenderer.generateGaussianMap(self.device)
}

/// Update the projection matrix with a new drawable size
Expand Down Expand Up @@ -78,21 +77,21 @@ class FoilRenderer: NSObject {
// Synchronize since positions buffer may be created on another thread
rendererDispatchQueue.sync {
renderEncoder.setVertexBuffer(
positionsBuffer, offset: 0, index: FoilRenderBufferIndex.positions.rawValue
positionsBuffer, offset: 0, index: Int(FoilRenderBufferIndexPositions.rawValue)
)
}

renderEncoder.setVertexBuffer(
colorsBuffer, offset: 0, index: FoilRenderBufferIndex.colors.rawValue
colorsBuffer, offset: 0, index: Int(FoilRenderBufferIndexColors.rawValue)
)

renderEncoder.setVertexBuffer(
dynamicUniformBuffers[currentBufferIndex],
offset: 0, index: FoilRenderBufferIndex.uniforms.rawValue
offset: 0, index: Int(FoilRenderBufferIndexUniforms.rawValue)
)

renderEncoder.setFragmentTexture(
gaussianMap, index: FoilTextureIndex.colorMap.rawValue
gaussianMap, index: Int(FoilTextureIndexColorMap.rawValue)
)

renderEncoder.drawPrimitives(
Expand Down Expand Up @@ -122,28 +121,26 @@ class FoilRenderer: NSObject {

// Calculate the size of a RGBA8Unorm texture's data and allocate system memory buffer
// used to fill the texture's memory
let dataSize = textureDescriptor.width * textureDescriptor.height
let dataSize = textureDescriptor.width * textureDescriptor.height * MemoryLayout<UInt8>.size

let nDelta: vector_float2 = [2.0 / Float(textureDescriptor.width), 2.0 / Float(textureDescriptor.height)]

var texelData = [UInt8](repeating: 0, count: dataSize)

var SNormCoordinate = vector_float2(repeating: -1)

var i = 0

// Procedurally generate data to fill the texture's buffer
for y in 0..<textureDescriptor.height {
SNormCoordinate.y = -1.0 + Float(y) * nDelta.y;
let sNormY = -1.0 + Float(y) * nDelta.y

for x in 0..<textureDescriptor.width {
SNormCoordinate.x = -1.0 + Float(x) * nDelta.x;
let sNormX = -1.0 + Float(x) * nDelta.x;

let distance = sqrt(SNormCoordinate.x * SNormCoordinate.x + SNormCoordinate.y * SNormCoordinate.y)
let t = (distance < 1.0) ? distance : 1.0;
let sNormVector = simd_make_float2(sNormX, sNormY)
let h = min(1.0, simd_length(sNormVector))

// Hermite interpolation where u = {1, 0} and v = {0, 0}
let color = ((2.0 * t - 3.0) * t * t + 1.0);
let color = (2.0 * h - 3.0) * h * h + 1.0

texelData[i] = UInt8(Float(0xFF) * color)

Expand All @@ -157,7 +154,7 @@ class FoilRenderer: NSObject {

gaussianMap!.replace(
region: region, mipmapLevel: 0, withBytes: texelData,
bytesPerRow: textureDescriptor.width * MemoryLayout<UInt8>.stride
bytesPerRow: textureDescriptor.width * MemoryLayout<UInt8>.size
)

gaussianMap!.label = "Gaussian Map"
Expand Down Expand Up @@ -207,23 +204,20 @@ class FoilRenderer: NSObject {

// Indicate shared storage so that both the CPU can access the buffers
let storageMode = MTLResourceOptions.storageModeShared
let stride = MemoryLayout<FoilUniform>.stride
let stride = MemoryLayout<FoilUniforms>.stride
guard let dub = device.makeBuffer(length: stride, options: storageMode)
else { fatalError() }

dub.label = "UniformBuffer"
dynamicUniformBuffers.append(dub)

// Initialize number of bodies to render
setNumRenderBodies(64 * 1024)

commandQueue = device.makeCommandQueue()
}

/// Update any render state (including updating dynamically changing Metal buffers)
func updateState() {
let uniforms = dynamicUniformBuffers[currentBufferIndex].contents()
var u = uniforms.assumingMemoryBound(to: FoilUniform.self).pointee
var u = uniforms.assumingMemoryBound(to: FoilUniforms.self).pointee

u.pointSize = FoilRenderer.bodyPointSize
u.mvpMatrix = projectionMatrix
Expand Down
31 changes: 0 additions & 31 deletions Renderer/FoilShaderTypes.swift

This file was deleted.

5 changes: 2 additions & 3 deletions Renderer/FoilShaders.metal
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ Metal shaders used for this sample
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;
#include "FoilShaderTypes.h"

// Include header shared between this Metal shader code and C code executing Metal API commands
#import "FoilShaderTypes.h"
using namespace metal;

// Vertex shader outputs and per-fragment inputs
typedef struct
Expand Down
32 changes: 0 additions & 32 deletions Simulation/FoilKernelTypes.swift

This file was deleted.

3 changes: 1 addition & 2 deletions Simulation/FoilKernels.metal
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
using namespace metal;

#include <simd/simd.h>

#import "FoilKernelTypes.h"
#include "FoilKernelTypes.h"

static float3 computeAcceleration(const float4 vsPosition,
const float4 oldPosition,
Expand Down
Loading

0 comments on commit 402379c

Please sign in to comment.