Make sure we dispatch a height update for every vertex

This commit is contained in:
Eryn Wells 2018-11-23 13:17:27 -07:00
parent 9c4163909c
commit ac7ae15ceb
2 changed files with 7 additions and 6 deletions

View file

@ -22,18 +22,16 @@ kernel void updateGeometryHeights(texture2d<float> texture [[texture(GeneratorTe
constant float2 *texCoords [[buffer(GeneratorBufferIndexTexCoords)]],
constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]],
device packed_float3 *vertexes [[buffer(GeneratorBufferIndexMeshPositions)]],
uint2 tid [[thread_position_in_grid]])
uint tid [[thread_position_in_grid]])
{
constexpr sampler s(coord::normalized, address::clamp_to_zero, filter::linear);
const uint vIdx = tid.y * uniforms.terrainSegments.x + tid.x;
// Get the height from the texture.
float2 texCoord = texCoords[vIdx];
float2 texCoord = texCoords[tid];
float4 height = texture.sample(s, texCoord);
// Update the vertex data.
vertexes[vIdx].y = height.r;
vertexes[tid].y = height.r;
}
kernel void updateGeometryNormals(constant packed_float3 *meshPositions [[buffer(GeneratorBufferIndexMeshPositions)]],

View file

@ -162,7 +162,10 @@ class Terrain: NSObject {
let texCoordBuffer = mesh.vertexBuffers[BufferIndex.meshGenerics.rawValue]
computeEncoder.setBuffer(texCoordBuffer.buffer, offset: texCoordBuffer.offset, index: GeneratorBufferIndex.texCoords.rawValue)
computeEncoder.setBuffer(uniforms.buffer, offset: uniforms.offset, index: GeneratorBufferIndex.uniforms.rawValue)
computeEncoder.dispatchThreads(MTLSize(width: Int(segments.x + 1), height: Int(segments.y + 1), depth: 1), threadsPerThreadgroup: MTLSize(width: 8, height: 8, depth: 1))
let threads = MTLSize(width: mesh.vertexCount, height: 1, depth: 1)
computeEncoder.dispatchThreads(threads, threadsPerThreadgroup: MTLSize(width: 64, height: 1, depth: 1))
computeEncoder.popDebugGroup()
computeEncoder.endEncoding()
}