Hi,
I am writing a GLSL compute shader and encountering a strange behaviour where my GPU utilisation is 100% causing me to drop to ~15FPS, then after ~15 seconds it goes back to normal (low utilization and 60FPS).
Here is the isolated offending snippets (reproduced on a fresh project with just a camera and this, on GLCore graphic backend on W10, rtx3080, Unity 2022.1.0b8.274 and 2021.1.0f1 (tested both))
It’s possible I misunderstood how to use the buffers since I’m new to all of this, which is why I’m asking here before opening a bug report. In this case, please correct me !
GLSL :
#pragma kernel main
GLSLPROGRAM
#version 440
#extension GL_ARB_shading_language_420pack : require
layout(std430, binding = 0) buffer _AlbedosBuffer { vec3 albedos[]; };
layout(std430, binding = 1) buffer _NormalsBuffer { vec3 normals[]; };
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
void main()
{
albedos[gl_GlobalInvocationID.x] = vec3(1.0);
normals[gl_GlobalInvocationID.x] = vec3(1.0, 0.0, 0.0);
}
ENDGLSL
C# :
using UnityEngine;
namespace BugReport
{
public class Main : MonoBehaviour
{
public ComputeShader Shader;
private ComputeBuffer _AlbedosBuffer;
private ComputeBuffer _NormalsBuffer;
// called by Unity when the camera has finished rendering
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Shader.SetBuffer(0, "_AlbedosBuffer", _AlbedosBuffer);
Shader.SetBuffer(0, "_NormalsBuffer", _NormalsBuffer);
Shader.Dispatch(0, 1024 * 1024 / 64, 1, 1); // dispatch 1Mi threads
}
private void OnEnable()
{
_AlbedosBuffer = new ComputeBuffer(1024 * 1024, 4 * 32 / 8); // claim 16MiB for 1Mi float3 (which takes as much space as float4)
_NormalsBuffer = new ComputeBuffer(1024 * 1024, 4 * 32 / 8); // claim 16MiB for 1Mi float3 (which takes as much space as float4)
}
private void OnDisable()
{
_AlbedosBuffer.Release();
_AlbedosBuffer = null; // can help the garbage collector
_NormalsBuffer.Release();
_NormalsBuffer = null; // can help the garbage collector
}
}
}
The problem still happens with a single buffer, but it’s less noticeable (50% utilisation and drop to 30 FPS for a few seconds).
Thanks!