Sunday, October 23, 2022
HomeGame Developmentunity - Compute Shader producing information to be used on CPU

unity – Compute Shader producing information to be used on CPU


I am 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 need to pull again the primary dozen or so.

At the moment I am 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 Construct() {
        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("Constructing star map [64, {0}, 1] for a complete of {1:#,0}", y, y * 64);
        mapComputeShader.Dispatch(Kernel("BuildStars"), 64, y, 1);

    }

    non-public void Learn() {
        var locs = new Vector3[numLocations];
        starLocations.GetData(locs, 0, 0, numLocations);
        Debug.Log(String.Be a part of(", ", locs.Choose(x => x.ToString())));
    }

I would anticipate that to return an array of Vector3 with values (0,0,0), (1,0,0), (2,0,0), and many others…

What I really 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 would assume this was a problem with the array being uninitialised, nonetheless, the compute shader is meant to put in writing each worth earlier than I’m going and have a look at the information.

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

What am I lacking?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments