Work in the normal calculations
This commit is contained in:
parent
f6f01b6419
commit
e62dd1b0f2
3 changed files with 43 additions and 12 deletions
|
@ -32,7 +32,8 @@ class Renderer: NSObject, MTKViewDelegate {
|
|||
var depthState: MTLDepthStencilState
|
||||
var colorMap: MTLTexture
|
||||
|
||||
var updateGeometryHeightsPipeline: MTLComputePipelineState
|
||||
let updateGeometryHeightsPipeline: MTLComputePipelineState
|
||||
let updateGeometryNormalsPipeline: MTLComputePipelineState
|
||||
|
||||
let inFlightSemaphore = DispatchSemaphore(value: maxBuffersInFlight)
|
||||
let regenerationSemaphore = DispatchSemaphore(value: 1)
|
||||
|
@ -101,7 +102,8 @@ class Renderer: NSObject, MTKViewDelegate {
|
|||
}
|
||||
|
||||
do {
|
||||
updateGeometryHeightsPipeline = try Renderer.buildUpdateGeometryPipeline(withDevice: device, library: library)
|
||||
updateGeometryHeightsPipeline = try Renderer.buildUpdateGeometryHeightsPipeline(withDevice: device, library: library)
|
||||
updateGeometryNormalsPipeline = try Renderer.buildUpdateGeometryNormalsPipeline(withDevice: device, library: library)
|
||||
} catch {
|
||||
print("Unable to create update geometry pipeline. Error: \(error)")
|
||||
return nil
|
||||
|
@ -134,13 +136,20 @@ class Renderer: NSObject, MTKViewDelegate {
|
|||
return try device.makeRenderPipelineState(descriptor: pipelineDescriptor)
|
||||
}
|
||||
|
||||
class func buildUpdateGeometryPipeline(withDevice device: MTLDevice, library: MTLLibrary) throws -> MTLComputePipelineState {
|
||||
class func buildUpdateGeometryHeightsPipeline(withDevice device: MTLDevice, library: MTLLibrary) throws -> MTLComputePipelineState {
|
||||
guard let computeFunction = library.makeFunction(name: "updateGeometryHeights") else {
|
||||
throw RendererError.badComputeFunction
|
||||
}
|
||||
return try device.makeComputePipelineState(function: computeFunction)
|
||||
}
|
||||
|
||||
class func buildUpdateGeometryNormalsPipeline(withDevice device: MTLDevice, library: MTLLibrary) throws -> MTLComputePipelineState {
|
||||
guard let computeFunction = library.makeFunction(name: "updateGeometryNormals") else {
|
||||
throw RendererError.badComputeFunction
|
||||
}
|
||||
return try device.makeComputePipelineState(function: computeFunction)
|
||||
}
|
||||
|
||||
class func loadTexture(device: MTLDevice,
|
||||
textureName: String) throws -> MTLTexture {
|
||||
/// Load texture data with optimal parameters for sampling
|
||||
|
@ -238,8 +247,9 @@ class Renderer: NSObject, MTKViewDelegate {
|
|||
didScheduleAlgorithmIteration = true
|
||||
}
|
||||
|
||||
if didScheduleAlgorithmIteration || needsGeometryUpdate, let computeEncoder = commandBuffer.makeComputeCommandEncoder() {
|
||||
print("Scheduling update geometry iteration")
|
||||
if didScheduleAlgorithmIteration || needsGeometryUpdate {
|
||||
if let computeEncoder = commandBuffer.makeComputeCommandEncoder() {
|
||||
print("Scheduling update geometry heights")
|
||||
computeEncoder.label = "Geometry Heights Encoder"
|
||||
computeEncoder.pushDebugGroup("Update Geometry: Heights")
|
||||
computeEncoder.setComputePipelineState(updateGeometryHeightsPipeline)
|
||||
|
@ -252,6 +262,23 @@ class Renderer: NSObject, MTKViewDelegate {
|
|||
computeEncoder.dispatchThreads(MTLSize(width: Int(terrain.segments.x + 1), height: Int(terrain.segments.y + 1), depth: 1), threadsPerThreadgroup: MTLSize(width: 8, height: 8, depth: 1))
|
||||
computeEncoder.popDebugGroup()
|
||||
computeEncoder.endEncoding()
|
||||
}
|
||||
|
||||
if let computeEncoder = commandBuffer.makeComputeCommandEncoder() {
|
||||
print("Scheduling update geometry normals")
|
||||
computeEncoder.label = "Geometry Normals Encoder"
|
||||
computeEncoder.pushDebugGroup("Update Geometry: Normals")
|
||||
computeEncoder.setComputePipelineState(updateGeometryNormalsPipeline)
|
||||
let indexBuffer = terrain.mesh.submeshes[0].indexBuffer
|
||||
computeEncoder.setBuffer(indexBuffer.buffer, offset: indexBuffer.offset, index: GeneratorBufferIndex.indexes.rawValue)
|
||||
let vertexBuffer = terrain.mesh.vertexBuffers[BufferIndex.meshPositions.rawValue]
|
||||
computeEncoder.setBuffer(vertexBuffer.buffer, offset: vertexBuffer.offset, index: GeneratorBufferIndex.vertexes.rawValue)
|
||||
let normalBuffer = terrain.mesh.vertexBuffers[BufferIndex.normals.rawValue]
|
||||
computeEncoder.setBuffer(normalBuffer.buffer, offset: normalBuffer.offset, index: GeneratorBufferIndex.normals.rawValue)
|
||||
computeEncoder.dispatchThreads(MTLSize(width: terrain.mesh.vertexCount, height: 1, depth: 1), threadsPerThreadgroup: MTLSize(width: 64, height: 1, depth: 1))
|
||||
computeEncoder.popDebugGroup()
|
||||
computeEncoder.endEncoding()
|
||||
}
|
||||
}
|
||||
|
||||
/// Delay getting the currentRenderPassDescriptor until we absolutely need it to avoid
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue