Get face normals updated

This commit is contained in:
Eryn Wells 2018-11-19 21:52:53 -07:00
parent 764b772fd9
commit 43e42f5374
3 changed files with 16 additions and 7 deletions

View file

@ -46,7 +46,8 @@ typedef NS_ENUM(NSInteger, GeneratorBufferIndex) {
GeneratorBufferIndexTexCoords = 1,
GeneratorBufferIndexIndexes = 2,
GeneratorBufferIndexNormals = 3,
GeneratorBufferIndexUniforms = 4,
GeneratorBufferIndexFaceNormals = 4,
GeneratorBufferIndexUniforms = 5,
};
typedef NS_ENUM(NSInteger, GeneratorTextureIndex) {

View file

@ -46,7 +46,7 @@ private:
kernel void updateGeometryHeights(texture2d<float> texture [[texture(GeneratorTextureIndexIn)]],
constant float2 *texCoords [[buffer(GeneratorBufferIndexTexCoords)]],
constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]],
device packed_float3 *vertexes [[buffer(GeneratorBufferIndexVertexes)]],
device packed_float3 *vertexes [[buffer(GeneratorBufferIndexMeshPositions)]],
uint2 tid [[thread_position_in_grid]])
{
constexpr sampler s(coord::normalized, address::clamp_to_zero, filter::linear);
@ -61,9 +61,9 @@ kernel void updateGeometryHeights(texture2d<float> texture [[texture(GeneratorTe
vertexes[vIdx].y = height.r;
}
kernel void updateGeometryNormals(constant float3 *vertexes [[buffer(GeneratorBufferIndexVertexes)]],
kernel void updateGeometryNormals(constant float3 *vertexes [[buffer(GeneratorBufferIndexMeshPositions)]],
constant packed_uint3 *indexes [[buffer(GeneratorBufferIndexIndexes)]],
device packed_float3 *normals [[buffer(GeneratorBufferIndexNormals)]],
device packed_float3 *normals [[buffer(GeneratorBufferIndexFaceNormals)]],
uint tid [[thread_position_in_grid]])
{
const uint3 triIdx = indexes[tid];

View file

@ -87,6 +87,7 @@ class Terrain: NSObject {
let segments: uint2
let vertexDescriptor: MTLVertexDescriptor
let mesh: MTKMesh
let faceNormalsBuffer: MTLBuffer
var generator: TerrainGenerator
@ -117,6 +118,14 @@ class Terrain: NSObject {
return nil
}
// A normal is a float 3. Two triangles per segment, x * t segments.
let faceNormalsLength = MemoryLayout<float3>.stride * 2 * Int(segments.x * segments.y)
guard let faceNormalsBuf = device.makeBuffer(length: faceNormalsLength, options: .storageModePrivate) else {
print("Couldn't create buffer for face normals")
return nil
}
faceNormalsBuffer = faceNormalsBuf
super.init()
}
@ -164,9 +173,8 @@ class Terrain: NSObject {
computeEncoder.setBuffer(indexBuffer.buffer, offset: indexBuffer.offset, index: GeneratorBufferIndex.meshPositions.rawValue)
let vertexBuffer = mesh.vertexBuffers[BufferIndex.meshPositions.rawValue]
computeEncoder.setBuffer(vertexBuffer.buffer, offset: vertexBuffer.offset, index: GeneratorBufferIndex.indexes.rawValue)
// let normalBuffer = mesh.vertexBuffers[BufferIndex.faceNormals.rawValue]
// computeEncoder.setBuffer(normalBuffer.buffer, offset: normalBuffer.offset, index: BufferIndex.faceNormals.rawValue)
computeEncoder.dispatchThreads(MTLSize(width: mesh.vertexCount, height: 1, depth: 1), threadsPerThreadgroup: MTLSize(width: 64, height: 1, depth: 1))
computeEncoder.setBuffer(faceNormalsBuffer, offset: 0, index: GeneratorBufferIndex.faceNormals.rawValue)
computeEncoder.dispatchThreads(MTLSize(width: 2 * Int(segments.x * segments.y), height: 1, depth: 1), threadsPerThreadgroup: MTLSize(width: 64, height: 1, depth: 1))
computeEncoder.popDebugGroup()
computeEncoder.endEncoding()
}