Hello everyone! I'm currently in the process of learning how to work with compute shaders and I've experienced an issue with texture sampling in compute shader. When I sample a texture, it always gives me same value for whatever position I sample.
Input texture is usually a texture with resolution of 64x64, RGBA32 type, resolution is also 64 and threads is 8 (for testing purpouses).
**Simplified compute shader code**
Texture2D _HeightMap;
SamplerState sampler_HeightMap;
RWStructuredBuffer Vertices;
uniform int Resolution;
// Calculate index in buffer
int GetIndex(uint3 id)
{
return id.y + Resolution* id.x;
}
[numthreads(8,8,1)]
void CalculateVertices(uint3 id : SV_DispatchThreadID)
{
// Calculate vertex id in buffer
int idx = GetIndex(id);
// Sample heightmap, set height to some value and add vertex to a buffer
Vertices[idx] = float3(id.x, _HeightMap[id.xy].b, id.y);
}
**Simplified c# code**
public Mesh GenerateMesh(Texture2D input, int resolution, int threads)
{
//buffers for vertices and map of vertices to make triangles
int numIndices = 2*(resolution-1)*(resolution- 1) * 3;
ComputeBuffer vertexBuffer = new ComputeBuffer(resolution* resolution, sizeof(float) * 3, ComputeBufferType.Default);
int vertexKernel = _terrainShader.FindKernel("CalculateVertices");
// Init buffers and other fields
_terrainShader.SetBuffer(vertexKernel, "Vertices", vertexBuffer);
_terrainShader.SetTexture(vertexKernel, "_HeightMap", input);
_terrainShader.SetInt("Resolution", resolution);
_terrainShader.Dispatch(vertexKernel, resolution / threads, resolution / threads, 1);
}
**What I've tried**
I've tried to sample texture using _HeightMap.SampleLevel(), I've also tried using RWTexture
Thank you all for your help.
↧