Home Game Development unity – Compute Shader producing knowledge to be used on CPU

unity – Compute Shader producing knowledge to be used on CPU

0
unity – Compute Shader producing knowledge to be used on CPU

[ad_1]

I’m attempting to create a compute shader that may generate and handle the motion of a degree cloud.

The shader wants a number of thousand factors to work on and I wish to pull again the primary dozen or so.

Currently I’m attempting to crate a minimal working instance, the place the compute shader populates a buffer of ~8k values and I learn the primary 10 again.

Compute Shader:

RWStructuredBuffer<float3> starLocationBuffer;
[numthreads(64, 1, 1)]
void BuildStars(uint id : SV_GroupIndex) {
    starLocationBufferhttps://gamedev.stackexchange.com/q/203034 = float3(id, 1, 1);
}

Calling class:

    non-public ComputeBuffer starLocations;
    non-public int numStars = 8192;
    non-public int numLocations = 10;


    non-public void Build() {
        starLocations = new ComputeBuffer(
            numStars,
            Marshal.SizeOf(typeof(Vector3)),
            ComputeBufferType.Default,
            ComputeBufferMode.Dynamic);

        mapComputeShader.SetBuffer(Kernel("BuildStars"), "starLocationBuffer", starLocations);

        int y = (int)(Mathf.Ceil(numStars / 64f));
        Debug.LogFormat("Building star map [64, {0}, 1] for a complete of {1:#,0}", y, y * 64);
        mapComputeShader.Dispatch(Kernel("BuildStars"), 64, y, 1);

    }

    non-public void Read() {
        var locs = new Vector3[numLocations];
        starLocations.GetData(locs, 0, 0, numLocations);
        Debug.Log(String.Join(", ", locs.Select(x => x.ToString())));
    }

I’d anticipate that to return an array of Vector3 with values (0,0,0), (1,0,0), (2,0,0), and so forth…

What I truly get again varies. Initially, I used to be getting mostly-zero values with some uncommon values thrown in (NaN and enormous numbers). Now, I get constant zeros.

I’d assume this was a difficulty with the array being uninitialised, nonetheless, the compute shader is meant to put in writing each worth earlier than I am going and take a look at the info.

My subsequent thought is that there could also be a race situation… Perhaps the compute shader hadn’t had time to put in writing these values earlier than I went trying, nonetheless, this publish by a Unity engineer signifies it enforces sequential execution.

What am I lacking?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here