From fdcbc374525043ceb565492ea65ad6fbef562fa9 Mon Sep 17 00:00:00 2001 From: Rob Bishop Date: Tue, 12 May 2020 01:03:52 -0700 Subject: [PATCH] Fix improper use of unsafe pointers Many, many thanks to TylerTheCompiler at SO! --- Foil/FoilViewController.swift | 10 +++++--- Renderer/FoilRenderer.swift | 43 ++++++++++++++++++--------------- Renderer/FoilShaders.metal | 2 +- Simulation/FoilSimulation.swift | 18 +++++++++----- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/Foil/FoilViewController.swift b/Foil/FoilViewController.swift index a1e74bc..f33bd28 100644 --- a/Foil/FoilViewController.swift +++ b/Foil/FoilViewController.swift @@ -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 @@ -111,6 +111,8 @@ class FoilViewController: NSViewController { renderer = FoilRenderer(self, rendererDevice) NSLog("New render device: \"\(rendererDevice.name)\"") + + renderer.drawableSizeWillChange() } func beginSimulation() { @@ -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)") } @@ -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" diff --git a/Renderer/FoilRenderer.swift b/Renderer/FoilRenderer.swift index 0f70c62..72e0caf 100644 --- a/Renderer/FoilRenderer.swift +++ b/Renderer/FoilRenderer.swift @@ -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( @@ -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) @@ -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) { @@ -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 ) } } diff --git a/Renderer/FoilShaders.metal b/Renderer/FoilShaders.metal index 1a08776..8385212 100644 --- a/Renderer/FoilShaders.metal +++ b/Renderer/FoilShaders.metal @@ -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; } diff --git a/Simulation/FoilSimulation.swift b/Simulation/FoilSimulation.swift index 5d24049..ff4d53e 100644 --- a/Simulation/FoilSimulation.swift +++ b/Simulation/FoilSimulation.swift @@ -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..