#Trouble working with vertices in my shader

1 messages · Page 1 of 1 (latest)

tight laurel
#

I was previously creating a heightmap in a shader, which I would vertically displace vertices with, and I'm changing this to instead alter the vertex position directly in the shader. I cant figure out how to actually read the vertices in my shader.

The old shader has this, it just calculates a height value and writes it to the texture

[numthreads(8,8,1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
    float2 uv = GetUV(id);
    int2 coord = int2(id.xy);
    float h = Solve(uv);
    Result[coord] = float4(h, 0.0, 0.0, 1.0);
}```
I'm trying to do the same, but get the xz position of each vertex to act like the UV, but running the new code, nothing appears to happen
```hlsl
struct Vertex {
    float3 position;
};
int NumVerticesPerAxis;
RWStructuredBuffer<Vertex> VertexBuffer;
int NumVertices; // VertexBuffer length

[numthreads(8,1,8)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
  int index = (id.z * NumVerticesPerAxis) + id.x; // 2D to 1D
  Vertex vtx = VertexBuffer[index];
  float2 p = float2(vtx.position.xz);
  float h = Solve(p);
  vtx.position.y = h;
  VertexBuffer[index] = vtx;
}```
fleet spoke
#

Are you sure there is correct data in the buffer before the dispatch? How are you testing! Confirming/debugging?

#

Aside from that the index calculation is a bit suspicious. Why not just use a one dimensional dispatch to avoid calculating the index?

tight laurel
# fleet spoke Are you sure there is correct data in the buffer before the dispatch? How are yo...

Yeah, I'm sure the data is correct. For every vertex in the mesh, its turned into an array of:

 public struct PointData {
    public Vector3 Position;
  }```And that array creates a ComputeBuffer.

I have just tested the shader and just set the height to this (confused why I hadnt done this yesterday), and its interesting that it *does* seem like its semi working
```hlsl
    vtx.position.y = 5;
    VertexBuffer[index] = vtx;```
#

I didnt realise I could use a one dimensional dispatch, I figured as I'm working with 2D (well, 3D ignoring y) coordinates for the vertices, the dispatch also had to be the same

#

Another thing is I'm not confident the thread groups are being set up correctly for the shader either

    var threadGroups = chunk.VertexPerAxis / 8f; // previously: TextureSize / 8
    ComputeShader.Dispatch(0, (int)threadGroups, 1, (int)threadGroups);```
fleet spoke
#

The total count of threads to be ran is threadgroups(x*y*z) * threads in a group(x*y*z)

tight laurel
#

well its 121 because the mesh is 11 * 11, so I just set that as [numthreads(121,1,1)]
I got it working, even though I dispatched with ComputeShader.Dispatch(0, 1, 1, 1);

I dont quite understand the relationship between threadGroups X,Y,Z and numthreads x,y,z

#

is it that by dispatching with 1 thread, it would need to have 121 threads to complete?
meaning if I dispatched with 2, it would need (about) 60 threads to complete