Reorganize a few things
This commit is contained in:
		
							parent
							
								
									275b260cf9
								
							
						
					
					
						commit
						73bbf2196d
					
				
					 3 changed files with 33 additions and 24 deletions
				
			
		| 
						 | 
				
			
			@ -76,6 +76,9 @@ class ViewController: NSViewController, RendererDelegate {
 | 
			
		|||
 | 
			
		||||
    override func viewWillAppear() {
 | 
			
		||||
        super.viewWillAppear()
 | 
			
		||||
 | 
			
		||||
        view.window?.makeFirstResponder(self)
 | 
			
		||||
 | 
			
		||||
        renderer.mtkView(metalView, drawableSizeWillChange: metalView.drawableSize)
 | 
			
		||||
        for _ in 1...10 {
 | 
			
		||||
            addBallWithRandomRadius()
 | 
			
		||||
| 
						 | 
				
			
			@ -93,6 +96,14 @@ class ViewController: NSViewController, RendererDelegate {
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override func keyDown(with event: NSEvent) {
 | 
			
		||||
        print("key down: \(event)")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override var acceptsFirstResponder: Bool {
 | 
			
		||||
        return true
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // MARK: - Private
 | 
			
		||||
 | 
			
		||||
    private func newErrorView() -> NSView {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -55,18 +55,36 @@ class MarchingSquares {
 | 
			
		|||
            fatalError("Error building compute pipeline state for sampling kernel: \(e)")
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        createParametersBuffer(withDevice: device)
 | 
			
		||||
        createSamplesBuffer(withDevice: device)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    func createParametersBuffer(withDevice device: MTLDevice) {
 | 
			
		||||
        // TODO: I'm cheating on this cause I didn't want to make a parallel struct in Swift and deal with alignment crap. >_> I should make a real struct for this.
 | 
			
		||||
        let parametersLength = MemoryLayout<simd.packed_int2>.stride * 3 + MemoryLayout<simd.uint>.stride
 | 
			
		||||
        parametersBuffer = device.makeBuffer(length: parametersLength, options: .storageModeShared)
 | 
			
		||||
        populateParametersBuffer()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    func createSamplesBuffer(withDevice device: MTLDevice) {
 | 
			
		||||
        // Only reallocate the buffer if the length changed.
 | 
			
		||||
        let samplesLength = MemoryLayout<Float>.stride * samplesCount
 | 
			
		||||
        guard samplesBuffer?.length != samplesLength else {
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
        samplesBuffer = device.makeBuffer(length: samplesLength, options: .storageModePrivate)
 | 
			
		||||
        if samplesBuffer == nil {
 | 
			
		||||
            fatalError("Couldn't create samplesBuffer!")
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    func fieldDidResize() {
 | 
			
		||||
        guard let device = gridGeometry?.device else {
 | 
			
		||||
        // Please just get the device from somewhere. 😅
 | 
			
		||||
        guard let device = gridGeometry?.device ?? samplesBuffer?.device else {
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
        populateParametersBuffer()
 | 
			
		||||
        populateGrid(withDevice: device)
 | 
			
		||||
        populateSamples(withDevice: device)
 | 
			
		||||
        createSamplesBuffer(withDevice: device)
 | 
			
		||||
        lastSamplesCount = samplesCount
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -75,6 +93,7 @@ class MarchingSquares {
 | 
			
		|||
            print("Tried to copy parameters buffer before buffer was allocated!")
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
        // TODO: I'm cheating on this cause I didn't want to make a parallel struct in Swift and deal with alignment crap. >_> I should make a real struct for this.
 | 
			
		||||
        let params: [uint] = [
 | 
			
		||||
            field.size.x, field.size.y,
 | 
			
		||||
            uint(xSamples), uint(ySamples),
 | 
			
		||||
| 
						 | 
				
			
			@ -114,26 +133,6 @@ class MarchingSquares {
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    func populateSamples(withDevice device: MTLDevice) {
 | 
			
		||||
//        var samples = [Float]()
 | 
			
		||||
//        samples.reserveCapacity(samplesCount)
 | 
			
		||||
 | 
			
		||||
//        for ys in 0..<ySamples {
 | 
			
		||||
//            let y = Float(ys * Int(sampleGridSize.y))
 | 
			
		||||
//            for xs in 0..<xSamples {
 | 
			
		||||
//                let x = Float(xs * Int(sampleGridSize.x))
 | 
			
		||||
//                let sample = field.sample(at: Float2(x: x, y: y))
 | 
			
		||||
//                samples.append(sample)
 | 
			
		||||
//            }
 | 
			
		||||
//        }
 | 
			
		||||
 | 
			
		||||
        let samplesLength = MemoryLayout<Float>.stride * samplesCount
 | 
			
		||||
        samplesBuffer = device.makeBuffer(length: samplesLength, options: .storageModePrivate)
 | 
			
		||||
        if samplesBuffer == nil {
 | 
			
		||||
            fatalError("Couldn't create samplesBuffer!")
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    func encodeSamplingKernel(intoBuffer buffer: MTLCommandBuffer) {
 | 
			
		||||
        guard let samplingPipeline = samplingPipeline else {
 | 
			
		||||
            print("Encode called before sampling pipeline was set up!")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -45,7 +45,6 @@ public class Renderer: NSObject, MTKViewDelegate {
 | 
			
		|||
            delegate.field.setupMetal(withDevice: device)
 | 
			
		||||
            delegate.marchingSquares.setupMetal(withDevice: device, library: library)
 | 
			
		||||
            delegate.marchingSquares.populateGrid(withDevice: device)
 | 
			
		||||
            delegate.marchingSquares.populateSamples(withDevice: device)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue