~connorbell/Physarum-Metal

4bc2d3b0fa3a03c27a0d9cd3353d2fbd04b0113a — Connor Bell 3 years ago 81bb6d7
cleaned up some threadgroup calculations
2 files changed, 9 insertions(+), 18 deletions(-)

M Physarum/Shaders.metal
M Physarum/Simulation.swift
M Physarum/Shaders.metal => Physarum/Shaders.metal +7 -16
@@ 155,11 155,9 @@ float3 hsv2rgb(float3 c)
kernel void setupParticles(device Particle *particles [[buffer(0)]],
                           device const int &width [[ buffer(1) ]],
                           device const int &height [[ buffer(2) ]],
                           const uint tgPos [[ threadgroup_position_in_grid ]],
                           const uint tPerTg [[ threads_per_threadgroup ]],
                           const uint tPos [[ thread_position_in_threadgroup ]])
                           uint id [[ thread_position_in_grid ]])

{
    uint id = tgPos * tPerTg + tPos;
    Particle p = particles[id];
    p.active = 0;
    p.position = float2(width/2.0,height/2.0) + (float2(cos(id*0.00235), sin(id*0.0059022))*(250+100));


@@ 174,12 172,10 @@ kernel void addParticles(device Particle *particles [[buffer(0)]],
                         device const int &height [[ buffer(2) ]],
                         device const float &startupProgress [[ buffer(3) ]],
                         device const int &offset [[ buffer(4) ]],
                         const uint tgPos [[ threadgroup_position_in_grid ]],
                         const uint tPerTg [[ threads_per_threadgroup ]],
                         const uint tPos [[ thread_position_in_threadgroup ]])
                         uint id [[ thread_position_in_grid ]])
{
    uint id = tgPos * tPerTg + tPos + uint(offset);
    Particle particle = particles[id];
    uint index = id + offset;
    Particle particle = particles[index];
    particle.active = 1;
    float halfWidth = width / 2.0;
    float startX = width/4.0;


@@ 188,7 184,7 @@ kernel void addParticles(device Particle *particles [[buffer(0)]],
    
    particle.position = pos;
    particle.sensorHeading = -.5 + 3.14159 + startupProgress*3.14159*3.5 + random(id*0.104) ;
    particles[id] = particle;
    particles[index] = particle;
}




@@ 199,16 195,11 @@ kernel void updateParticles(device Particle *particles [[buffer(0)]],
                            texture2d<float, access::write> outTexture [[texture(1)]],
                            texture2d<float, access::sample> inTrailMapTexture [[texture(2)]],
                            texture2d<float, access::write> outTrailMapTexture [[texture(3)]],
                            const uint tgPos [[ threadgroup_position_in_grid ]],
                            const uint tPerTg [[ threads_per_threadgroup ]],
                            const uint tPos [[ thread_position_in_threadgroup ]])
                            uint id [[ thread_position_in_grid ]])
{
    float2 viewSize = float2(inTexture.get_width(), inTexture.get_height());
    
    // Create a copy of the current particle
    uint id = tgPos * tPerTg + tPos;
    
    if (id >= 131072*2) return;
    Particle p = particles[id];
    
    if (p.active == 0) return;

M Physarum/Simulation.swift => Physarum/Simulation.swift +2 -2
@@ 210,7 210,7 @@ extension Simulation {
        if activeParticles + particlesPerFrame > particleCount {
            particlesPerFrame = particleCount - activeParticles
        }
        
        print(offset)
        if particlesPerFrame > 0 {
            commandEncoder.setComputePipelineState(self.addParticlesFunctionPipelineState)
            commandEncoder.setBuffer(self.particleBuffer, offset: 0, index: 0)


@@ 235,7 235,7 @@ extension Simulation {
        var frame = Float(self.frameNumber)
        let threadgroupsPerGrid = MTLSize(width: (activeParticles + self.stepFunctionPipelineState.threadExecutionWidth - 1) / self.stepFunctionPipelineState.threadExecutionWidth, height: 1, depth: 1)

        let threadsPerThreadgroup = MTLSize(width: self.initFunctionPipelineState.threadExecutionWidth, height: 1, depth: 1)
        let threadsPerThreadgroup = MTLSize(width: self.stepFunctionPipelineState.threadExecutionWidth, height: 1, depth: 1)
        
        commandEncoder.setComputePipelineState(self.stepFunctionPipelineState)
        commandEncoder.setTexture(renderTexture, index: 0)