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() {
|
override func viewWillAppear() {
|
||||||
super.viewWillAppear()
|
super.viewWillAppear()
|
||||||
|
|
||||||
|
view.window?.makeFirstResponder(self)
|
||||||
|
|
||||||
renderer.mtkView(metalView, drawableSizeWillChange: metalView.drawableSize)
|
renderer.mtkView(metalView, drawableSizeWillChange: metalView.drawableSize)
|
||||||
for _ in 1...10 {
|
for _ in 1...10 {
|
||||||
addBallWithRandomRadius()
|
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
|
// MARK: - Private
|
||||||
|
|
||||||
private func newErrorView() -> NSView {
|
private func newErrorView() -> NSView {
|
||||||
|
|
|
@ -55,18 +55,36 @@ class MarchingSquares {
|
||||||
fatalError("Error building compute pipeline state for sampling kernel: \(e)")
|
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
|
let parametersLength = MemoryLayout<simd.packed_int2>.stride * 3 + MemoryLayout<simd.uint>.stride
|
||||||
parametersBuffer = device.makeBuffer(length: parametersLength, options: .storageModeShared)
|
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() {
|
func fieldDidResize() {
|
||||||
guard let device = gridGeometry?.device else {
|
// Please just get the device from somewhere. 😅
|
||||||
|
guard let device = gridGeometry?.device ?? samplesBuffer?.device else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
populateParametersBuffer()
|
populateParametersBuffer()
|
||||||
populateGrid(withDevice: device)
|
populateGrid(withDevice: device)
|
||||||
populateSamples(withDevice: device)
|
createSamplesBuffer(withDevice: device)
|
||||||
lastSamplesCount = samplesCount
|
lastSamplesCount = samplesCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,6 +93,7 @@ class MarchingSquares {
|
||||||
print("Tried to copy parameters buffer before buffer was allocated!")
|
print("Tried to copy parameters buffer before buffer was allocated!")
|
||||||
return
|
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] = [
|
let params: [uint] = [
|
||||||
field.size.x, field.size.y,
|
field.size.x, field.size.y,
|
||||||
uint(xSamples), uint(ySamples),
|
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) {
|
func encodeSamplingKernel(intoBuffer buffer: MTLCommandBuffer) {
|
||||||
guard let samplingPipeline = samplingPipeline else {
|
guard let samplingPipeline = samplingPipeline else {
|
||||||
print("Encode called before sampling pipeline was set up!")
|
print("Encode called before sampling pipeline was set up!")
|
||||||
|
|
|
@ -45,7 +45,6 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
delegate.field.setupMetal(withDevice: device)
|
delegate.field.setupMetal(withDevice: device)
|
||||||
delegate.marchingSquares.setupMetal(withDevice: device, library: library)
|
delegate.marchingSquares.setupMetal(withDevice: device, library: library)
|
||||||
delegate.marchingSquares.populateGrid(withDevice: device)
|
delegate.marchingSquares.populateGrid(withDevice: device)
|
||||||
delegate.marchingSquares.populateSamples(withDevice: device)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue