Search Unity

Question Compute shader

Discussion in 'Shaders' started by gaiastellar, Nov 12, 2020.

  1. gaiastellar

    gaiastellar

    Joined:
    Nov 8, 2013
    Posts:
    57
    Hi all,
    I have been writing a compute shader to process noise , using a HLSL noise library called FastNoiseLite, which is freely available on git hub.

    It is basically working, where i am populating a render texture with noise. So far so good. My code on the shader side looks like this:

    #include "Assets/FastNoiseLite.hlsl"
    #pragma kernel NoiseTest


    RWTexture2D<float4> Result;


    [numthreads(8,8,1)]
    void NoiseTest (uint3 id : SV_DispatchThreadID)
    {

    fnl_state noise = fnlCreateState();
    noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
    noise.fractal_type = FNL_FRACTAL_FBM;


    float thisnoise = Remap(fnlGetNoise2D(noise, id.x, id.y), -1.0, 1.0, 0.0, 1.0f);
    Result[id.xy] = float4(thisnoise, thisnoise, thisnoise, thisnoise);
    }


    My question is, from my limited knowledge of shader code, the following lines of the code are used to set up the noise type, seed, fractal type etc and they are inside the main Kernel:

    fnl_state noise = fnlCreateState();
    noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
    noise.fractal_type = FNL_FRACTAL_FBM;


    but i am guessing they are running in every thread, which is obviously unnescessary, when really they only need to be called once. But im not sure where else i could put these lines so they only get called once. Is there a way to run those lines of code before the main thread gets called?

    could i put them in a sperate function which i could call from the CPU side first?

    My limited knowledge of HLSL s showing here... not sure what to do.

    Just a thought.. could i put them in an if statement, and once theyve run once, just set a bool to true, and they wont run again? or would that be even more inefficient than running them every thread?

    any help appreciated

    thanks in advance,

    paul uk







    paul
     
  2. gaiastellar

    gaiastellar

    Joined:
    Nov 8, 2013
    Posts:
    57
    hi, to anyone replying - ive got it sorted thanks...!!
     
  3. danielmus

    danielmus

    Joined:
    Jun 30, 2013
    Posts:
    1
    Can I ask how you sorted this? I know this is really old now but I'm new to this stuff myself too and am currently using this library and would love to know how you approached it
     
  4. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    294
    I was wondering this too. But I got it sorted, thanks!

    ...

    Just kidding, I will actually give a answer for any future readers haha. Basically
    fnl_state
    is some what poorly named. It isn't the actual state of the noise. It is nothing more than a struct used to pass multiple parameters at once to the the method instead of having to have a method with a bunch of parameters.
    Something like
    fnl_config_data
    would be more accurate.

    It is perfectly fine to create it each call.