0
\$\begingroup\$

I am trying to follow this excellent Coding Adventure: https://www.youtube.com/watch?v=lctXaT9pxA0 in order to get a very simple mesh deformation on a mesh using a compute shader.

I'm getting an error that reads:

Compute shader (NewComputeShader): Property (vertices) at kernel index (0) is not set UnityEngine.ComputeShader:Dispatch(Int32, Int32, Int32, Int32) MoonShader:runShader() (at Assets/MoonShader.cs:27) MoonShader:Update() (at Assets/MoonShader.cs:15)

I'm calling runShader() in Update (maybe that's a terrible idea, but I don't think it's contributing to this specific problem yet).

Do I need to somehow pass my mesh's vertices into StructuredBuffer<float3> vertices;? If so, how am I supposed to do that?

This is the call to the compute shader in my .cs file.

void runShader() {
    int kernelHandle = computeShader.FindKernel("CSMain");
    computeShader.Dispatch(kernelHandle, 512, 1, 1); //this is the line it appears to be complaining about
}

This is my .compute file.

#pragma kernel CSMain
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float> heights;
uint numVertices;
float testValue = 20;

[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID) {
    if(id >= numVertices) {

    } else {
        float3 vertexPosition = vertices[id];
        heights[id] = 1 + sin(vertexPosition.y * testValue) * 0.05;
    }
}

Link to repo if it's helpful: https://github.com/glenpierce/proceduralPlanets

\$\endgroup\$
0
\$\begingroup\$

You need "link" your StructuredBuffer to a ComputeBuffer that you initialize in the CS script. In your code, you've never specified a compute shader to write to, so the compiler is basically saying, "yo, there's nowhere to write this data to!"

In your .cs file, you should have:

void runShader() {
    
    int kernelHandle = computeShader.FindKernel("CSMain");
    //New Things here
    ComputeBuffer vertexBuffer = new ComputeBuffer(mesh.vertices.Length, sizeof(float) * 3); //make new compute buffer with specified size, and specified "stride"
    //stride is like the size of each element, in your case it would be 3 floats, since Vector3 is 3 floats.
    vertexBuffer.setData(mesh.vertices); //set data inside buffer

    computeShader.setBuffer(kernelHandle, "vertices", vertexBuffer); //Linking the compute shader and cs shader buffers


    computeShader.Dispatch(kernelHandle, 512, 1, 1);
}

I hope that helps!

I'd also recommend checking out these two ComputeShader guides, the OP walks you through his own personal project and explains things pretty well.

(Easy) https://www.reddit.com/r/Unity3D/comments/7pa6bq/drawing_mandelbrot_fractal_using_gpu_compute/

(Hard) https://www.reddit.com/r/Unity3D/comments/7ppldz/physics_simulation_on_gpu_with_compute_shader_in/

\$\endgroup\$
7
  • \$\begingroup\$ Thank you! I'm getting errors on these two lines now: vertexBuffer.setData(mesh.vertices); computeShader.setBuffer(kernelHandle, "vertices", vertexBuffer); both of the errors are complaining: does not contain a definition for ____ and no accessible extension method ____ accepting a first argument of type 'ComputeBuffer' The first argument in one case is a Vertex3[] and next case is an int. This error is super-confusing and I don't see any reference to how it's even possible. Do you have any idea where it might come from? \$\endgroup\$ – Glen Pierce Jul 20 '20 at 16:27
  • \$\begingroup\$ Can you post the error message? \$\endgroup\$ – TakeMeHomeCountryRoads Jul 21 '20 at 5:07
  • \$\begingroup\$ Assets\MoonShader.cs(26,23): error CS1061: 'ComputeShader' does not contain a definition for 'setBuffer' and no accessible extension method 'setBuffer' accepting a first argument of type 'ComputeShader' could be found (are you missing a using directive or an assembly reference?) \$\endgroup\$ – Glen Pierce Jul 23 '20 at 15:17
  • \$\begingroup\$ Assets\MoonShader.cs(25,22): error CS1061: 'ComputeBuffer' does not contain a definition for 'setData' and no accessible extension method 'setData' accepting a first argument of type 'ComputeBuffer' could be found (are you missing a using directive or an assembly reference?) \$\endgroup\$ – Glen Pierce Jul 23 '20 at 15:17
  • \$\begingroup\$ What is particularly odd about these is that neither of the first arguments are ComputeBuffers. vertexBuffer.setData is being called on GetComponent<MeshFilter>().mesh.vertices and computeShader.setBuffer(computeShader.FindKernel("CSMain"), "vertices", vertexBuffer)... computeShader.FindKernel("CSMain") is an int. \$\endgroup\$ – Glen Pierce Jul 23 '20 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.