1

I have recently been working with generating mesh data for a spherical planet made from 6 subdivided planes in Unity3d (like this). I created an algorithm in C# that lets me make the meshes on the CPU and for learning purposes I decided to port this algorithm to the GPU by using a compute shader. While it appears as if all the vertices and normals are getting calculated correctly, the populated triangle array ends up being half empty creating an undesirable mess of a mesh.

Since I consider each of the planes on the planet as a 2d grid of vertices (even though they get warped into a sphere), I compute the triangles on the CPU by looping over the width and height, taking the quad created by the current vertex and the 3 vertices behind and above it (top left, top right, bottom left, current vertex) and assigning the appropriate vertex indexes to the triangle array. On the GPU I copied my code almost line for line as the syntax is very similar and yet it seems to stop computing triangles after a certain point.

To test if it was the fault of the triangles or vertices I overwrote my GPU calculated triangle vertex indexes with the ones calculated by the C# algorithm. This gave me the shape that I wanted indicating that it was indeed just a problem with my triangles.

The resulting mesh data on the CPU correctly looks like this: Correct CPU implementation

The mesh generated incorrectly from the GPU looks like this: Incorrect GPU implementation

The difference between the CPU calculated triangle array and the GPU calculated triangle array can be seen in this image where the GPU is on the left, and the CPU on the right: Triangle array difference

I believe my problem is most likely misunderstanding how the compute shaders actually go out performing their thread calculations / indexing, but could be a problem with my implementation. Any help with understanding why my compute shader implementation is failing me would really help me out. I have included links to my source code on GitHub rather than put code snippets here as they might be difficult to understand out of context.

CPU implementation source code: Source

GPU shader dispatcher source code: Source

GPU compute shader source code: Source

2
  • Can you share your vertex/frag shader code too please? I have a similar issue, I'm wanting to calculate some cubes on the GPU, about 12k of them per frame, to speed up the implementation I already have on the CPU. Implementation fails on the vertex shader though, I get triangles all over the place. Tried with points and saw that the coordinates were all screwy in the shader, debugged the points though and they look right. Commented Aug 28, 2018 at 5:00
  • @JohnErnest If I remember correctly, the only shader code was in a computer shader that was dispatched from c# and then the results were returned to C#. There was no vertex or fragment shader components of this feature. However it was a long time ago and I could be mistaken. The above links should point to the code on github which you can browse and see what you find. Commented Aug 30, 2018 at 5:38

1 Answer 1

2

You just made a simple copy/paste error over here with buffer counts:

ComputeBuffer vb = new ComputeBuffer (v.Length, 3 * sizeof(float)); //3 floats * 4 bytes / float
ComputeBuffer nb = new ComputeBuffer (n.Length, 3 * sizeof(float));
ComputeBuffer ub = new ComputeBuffer (n.Length, 2 * sizeof(float));
ComputeBuffer tb = new ComputeBuffer (n.Length, sizeof(int));

Change it to this and it should be fine:

ComputeBuffer vb = new ComputeBuffer (v.Length, 3 * sizeof(float)); //3 floats * 4 bytes / float
ComputeBuffer nb = new ComputeBuffer (n.Length, 3 * sizeof(float));
ComputeBuffer ub = new ComputeBuffer (u.Length, 2 * sizeof(float));
ComputeBuffer tb = new ComputeBuffer (t.Length, sizeof(int));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the help, it's always the little things like that I end up missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.