FastNoise2 icon indicating copy to clipboard operation
FastNoise2 copied to clipboard

Optimize Fade Node to only calculate Input Value if needed for the output.

Open Mr-Lime-404 opened this issue 6 months ago • 2 comments

Whats the problem:

When working with Noise you might want to blend a lot of different Noise Trees based on some factors e.g. other noise. The Fade Node seems to be the way to do so, but it still calculates the values of both inputs A and B even if the Fade Value is 0 or (-)1. This means that blending together big and complex node trees results in a lot of unnecessary calculations if the Fade Value is mostly 0 or (-)1.

Possible Implementation:

If I understand the project structure correctly, you should be able to just do this within Blends.inl:

    ...
    FS_INLINE float32v GenT( int32v seed, P... pos ) const
    {
        float32v fade = FS_Abs_f32( this->GetSourceValue( mFade, seed, pos... ) );
        //My Additions
        if(fade == 0.0f) return this->GetSourceValue(mA, seed, pos...);
        if(fade == 1.0f) return this->GetSourceValue(mB, seed, pos...);

        return FS_FMulAdd_f32( this->GetSourceValue( mA, seed, pos... ), float32v( 1 ) - fade, this->GetSourceValue( mB, seed, pos... ) * fade );
    }
    ...

But I don't understand enough about the project to tell if if-statements somehow break it or make it slower in some other parts.

Current Workaround:

Generate all required noise values seperately in chunks and blend them together manually. Chunks would only generate the values they really need, but this could still be a lot depending on the project which would be more inefficient, as some values within a chunk are still possibly calculated without being needed.

Mr-Lime-404 avatar Sep 15 '25 09:09 Mr-Lime-404

It's not quite a simple as adding that if because with SIMD multiple values are processed at once. But if all values are 0 or all are 1 then it could skip calling the other source. I'll put it on my list

Auburn avatar Sep 16 '25 18:09 Auburn

Yea, figured there was a caveat. But some optimisation is better then none :) (sometimes xD)

Mr-Lime-404 avatar Sep 18 '25 11:09 Mr-Lime-404