Skip to content

Commit

Permalink
Fix improper use of unsafe pointers
Browse files Browse the repository at this point in the history
Many, many thanks to TylerTheCompiler at SO!
  • Loading branch information
SaganRitual committed May 12, 2020
1 parent a0cac20 commit fdcbc37
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
10 changes: 6 additions & 4 deletions Foil/FoilViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FoilViewController: NSViewController {

// Command queue used when simulation and renderer are using the same device.
// Set to nil when using different devices
var commandQueue: MTLCommandQueue!
var viewControllerCommandQueue: MTLCommandQueue!

// When true, stop running any more simulations (such as when the window closes).
var terminateAllSimulations = false
Expand Down Expand Up @@ -111,6 +111,8 @@ class FoilViewController: NSViewController {
renderer = FoilRenderer(self, rendererDevice)

NSLog("New render device: \"\(rendererDevice.name)\"")

renderer.drawableSizeWillChange()
}

func beginSimulation() {
Expand All @@ -123,7 +125,7 @@ class FoilViewController: NSViewController {

renderer.setRenderScale(renderScale: config.renderScale)

commandQueue = renderer.device.makeCommandQueue()
viewControllerCommandQueue = renderer.device.makeCommandQueue()

NSLog("Starting Simulation Config: \(configNum)")
}
Expand Down Expand Up @@ -184,8 +186,8 @@ class FoilViewController: NSViewController {

// If the simulation and device are using the same device _commandQueue will be set
// Create a command buffer to both execute a simulation frame and render an update
guard let commandQueue = self.commandQueue,
let simulate_render_commandBuffer = commandQueue.makeCommandBuffer()
guard let vcCommandQueue = self.viewControllerCommandQueue,
let simulate_render_commandBuffer = vcCommandQueue.makeCommandBuffer()
else { fatalError() }

simulate_render_commandBuffer.label = "simulate_render_commandBuffer"
Expand Down
43 changes: 24 additions & 19 deletions Renderer/FoilRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ class FoilRenderer: NSObject, MTKViewDelegate {
updateProjectionMatrix()
}

/// Update the projection matrix with a new drawable size
func drawableSizeWillChange(size: CGSize) {
print("dswc(CGSize)")
updateProjectionMatrix()
}
func drawableSizeWillChange() { updateProjectionMatrix() }

/// Draw particles at the supplied positions using the given command buffer to the given view
func draw(
Expand Down Expand Up @@ -201,7 +197,7 @@ class FoilRenderer: NSObject, MTKViewDelegate {
pipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
pipelineDescriptor.colorAttachments[0].pixelFormat = view.colorPixelFormat
pipelineDescriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperation.add
pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactor.sourceAlpha
pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactor.sourceAlpha
pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactor.sourceAlpha

guard let rp = try? device.makeRenderPipelineState(descriptor: pipelineDescriptor)
Expand Down Expand Up @@ -229,10 +225,12 @@ class FoilRenderer: NSObject, MTKViewDelegate {
/// Update any render state (including updating dynamically changing Metal buffers)
func updateState() {
let uniforms = dynamicUniformBuffer.contents()
var u = uniforms.assumingMemoryBound(to: FoilUniforms.self).pointee

u.pointSize = FoilRenderer.bodyPointSize
u.mvpMatrix = projectionMatrix
let u = FoilUniforms(
mvpMatrix: projectionMatrix, pointSize: FoilRenderer.bodyPointSize
)

uniforms.assumingMemoryBound(to: FoilUniforms.self).pointee = u
}

func providePositionData(data: NSData) {
Expand Down Expand Up @@ -289,16 +287,23 @@ class FoilRenderer: NSObject, MTKViewDelegate {
func updateProjectionMatrix() {
// React to resize of the draw rect. In particular update the perspective matrix.
// Update the aspect ratio and projection matrix since the view orientation or size has changed
let aspect: Float = Float(view.drawableSize.height) / Float(view.drawableSize.width)
let left: Float = renderScale
let right: Float = -renderScale
let bottom: Float = renderScale * aspect
let top: Float = -renderScale * aspect
let near: Float = 5000
let far: Float = -5000

projectionMatrix = FoilMath.matrixOrthoLeftHand(
left: left, right: right, bottom: bottom, top: top, nearZ: near, farZ: far
// let aspect: Float = Float(view.drawableSize.height) / Float(view.drawableSize.width)
// let left: Float = renderScale
// let right: Float = -renderScale
// let bottom: Float = renderScale * aspect
// let top: Float = -renderScale * aspect
// let near: Float = 5000
// let far: Float = -5000
//
// projectionMatrix = FoilMath.matrixOrthoLeftHand(
// left: left, right: right, bottom: bottom, top: top, nearZ: near, farZ: far
// )

projectionMatrix = FoilMath.matriMakeRows(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
)
}
}
2 changes: 1 addition & 1 deletion Renderer/FoilShaders.metal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ vertex ColorInOut vertexShader(uint vertexID [[ vertex_id ]]
out.color = half4(color[vertexID]) / 255.0h;

out.pointSize = half(uniforms.pointSize);

assert(false);
return out;
}

Expand Down
18 changes: 12 additions & 6 deletions Simulation/FoilSimulation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,12 @@ class FoilSimulation {

let c_ = UnsafeMutableRawPointer(mutating: sp.contents())

var c = c_.assumingMemoryBound(to: FoilSimParams.self).pointee
c.timestep = config.simInterval
c.damping = config.damping
c.softeningSqr = config.softeningSqr
c.numBodies = UInt32(config.numBodies)
let c = FoilSimParams(timestep: config.simInterval,
damping: config.damping,
softeningSqr: config.softeningSqr,
numBodies: UInt32(config.numBodies))

c_.assumingMemoryBound(to: FoilSimParams.self).pointee = c

simulationParams.didModifyRange(0..<sp.length)

Expand Down Expand Up @@ -252,9 +253,12 @@ class FoilSimulation {
let position = nrpos * (inner + (length * rpos))

var p = positions[i]
p.x = position.x; p.y = position.x; p.z = position.z

p.x = position.x; p.y = position.y; p.z = position.z
p.w = 1

positions[i] = p

var axis = vector_float3(0.0, 0.0, 1.0)
let scalar = simd_dot(nrpos, axis)

Expand All @@ -271,6 +275,8 @@ class FoilSimulation {
v.x = velocity.x * vscale
v.y = velocity.y * vscale
v.z = velocity.z * vscale

velocities[i] = v
}

let fullPRange = 0..<self.positions[oldBufferIndex].length
Expand Down

0 comments on commit fdcbc37

Please sign in to comment.