- Home /
ComputeBuffer stays empty after executing a Compute Shader
I was trying to implement Clustered Forward Rendering in custom srp and build the clusters in compute shader.In C# script, I have followed the general way:declare the compute shader and compute buffer, find the kernel, set the compute buffer and dispatch the compute shader, but finally I just got empty compute buffer. Here is my C# code:
public static ComputeBuffer clusters;
ComputeShader computeClusters;
CommandBuffer buffer = new CommandBuffer{name = bufferName};
public void Setup(ScriptableRenderContext context, ...)
{
...
ComputeCluster();
context.ExecuteCommandBuffer(buffer);
buffer.Clear();
}
void ComputeCluster()
{
clusters = new ComputeBuffer(16 * 9 * 24, 2 * 16 * 16);
int kernel = computeClusters.FindKernel("ComputeCluster");
buffer.SetComputeBufferParam(computeClusters, kernel, clusterId, clusters);
buffer.DispatchCompute(computeClusters, kernel, 16, 9, 24);
}
And here is my compute shader:
#pragma kernel ComputeCluster
#include "../ShaderLibrary/Common.hlsl"
struct VolumeTileAABB
{
float4 minPoint;
float4 maxPoint;
};
RWStructuredBuffer<VolumeTileAABB> _Cluster;
float4 TransformScreenToView(float4 positionSS)
{
float2 uv = positionSS.xy / _ScreenParams.xy;
float4 positionCS = float4(float2(uv.x, uv.y) * 2.0 - 1.0, positionSS.z, positionSS.w);
float4 positionVS = mul(unity_CameraInvProjection, positionCS);
positionVS = positionVS / positionVS.w;
return positionVS;
}
float3 lineIntersectionToZPlane(float3 A, float3 B, float zDistance)
{
float3 normal = float3(0.0, 0.0, 1.0);
float3 ab = B - A;
float t = (zDistance - dot(normal, A)) / dot(normal, ab);
float3 result = A + t * ab;
return result;
}
[numthreads(1,1,1)]
void ComputeCluster(uint3 id : SV_DispatchThreadID)
{
const float3 eyePos = float3(0.0, 0.0, 0.0);
uint tileSizePx = _ScreenParams.x / 16;
uint tileIndex = id.x + id.y * 16 + id.z * 16 * 9;
float4 maxPointSS = float4(float2(id.x + 1, id.y + 1) * tileSizePx, -1.0, 1.0);
float4 minPointSS = float4(id.xy * tileSizePx, -1.0, 1.0);
float3 maxPointVS = TransformScreenToView(maxPointSS).xyz;
float3 minPointVS = TransformScreenToView(minPointSS).xyz;
float tileNear = -_ProjectionParams.y * pow(_ProjectionParams.z / _ProjectionParams.y, id.z / 24.0);
float tileFar = -_ProjectionParams.y * pow(_ProjectionParams.z / _ProjectionParams.y, (id.z + 1) / 24.0);
float3 minPointNear = lineIntersectionToZPlane(eyePos, minPointVS, tileNear);
float3 minPointFar = lineIntersectionToZPlane(eyePos, minPointVS, tileFar);
float3 maxPointNear = lineIntersectionToZPlane(eyePos, maxPointVS, tileNear);
float3 maxPointFar = lineIntersectionToZPlane(eyePos, maxPointVS, tileFar);
float3 minPointAABB = min(min(minPointNear, minPointFar), min(maxPointNear, maxPointFar));
float3 maxPointAABB = max(max(minPointNear, minPointFar), max(maxPointNear, maxPointFar));
_Cluster[tileIndex].minPoint = float4(minPointAABB, 0.0);
_Cluster[tileIndex].maxPoint = float4(maxPointAABB, 0.0);
}
I tried to use GetData to log the array and all the outputs are 0.
I also tried to debug in RenderDoc and got the result "No Resource"
Answer by Dragon_Baby · Mar 08, 2021 at 08:09 AM
I have tried to use compute shader directly instead of using command buffer, and the problem solved.But I want to use command buffer to control the execution of compute shader. So Is there any way to fix the problem with command buffer?