From ac7ae15cebe611c131628cb304fac3a8d6f08a5b Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 23 Nov 2018 13:17:27 -0700 Subject: [PATCH] Make sure we dispatch a height update for every vertex --- Terrain2/Shaders/TerrainAlgorithms.metal | 8 +++----- Terrain2/Terrain.swift | 5 ++++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Terrain2/Shaders/TerrainAlgorithms.metal b/Terrain2/Shaders/TerrainAlgorithms.metal index ff2ba52..d90e13b 100644 --- a/Terrain2/Shaders/TerrainAlgorithms.metal +++ b/Terrain2/Shaders/TerrainAlgorithms.metal @@ -22,18 +22,16 @@ kernel void updateGeometryHeights(texture2d 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)]], diff --git a/Terrain2/Terrain.swift b/Terrain2/Terrain.swift index 8266b32..ab01ce3 100644 --- a/Terrain2/Terrain.swift +++ b/Terrain2/Terrain.swift @@ -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() }