I am learning compute shaders right now and have a very simple compute shader that I made.
However, after about a minute, my memory usage will have jumped to about 100 gigabytes.
Any idea why?
TestShader.compute
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Result;
float2 res;
float thingRadius;
float colour;
float x;
float y;
struct Circle{
float2 position;
float scale;
};
float rand (float2 co)
{
return(frac(sin(dot (co.xy, float2(12.9898, 78.233))) * 43758.5453)) * 1;
}
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
x = id.x / res.x;
y = id.y / res.y;
colour = abs(x - 0.5) * thingRadius + abs(y - 0.5) * thingRadius;
Result[id.xy] = float4(colour, colour, colour, 0.0);
}
code here
ComputeShaderTest.cs
using UnityEngine;
public class ComputeShaderTest : MonoBehaviour
{
public ComputeShader computeShader;
public RenderTexture renderTexture;
public int resolutionX;
public int resolutionY;
public float thingRadius = 1f;
private void Start()
{
renderTexture = new RenderTexture(resolutionX,resolutionY,24);
renderTexture.enableRandomWrite = true;
renderTexture.Create();
computeShader.SetTexture(0, "Result", renderTexture);
computeShader.Dispatch(0,renderTexture.width / 8, renderTexture.height / 8, 1);
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if(renderTexture != null)
{
renderTexture = new RenderTexture(resolutionX, resolutionY, 24);
renderTexture.enableRandomWrite = true;
renderTexture.Create();
}
computeShader.SetTexture(0, "Result", renderTexture);
computeShader.SetVector("res", new Vector4(renderTexture.width,renderTexture.height));
computeShader.SetFloat("thingRadius", thingRadius);
computeShader.Dispatch(0, renderTexture.width / 8, renderTexture.height / 8, 1);
Graphics.Blit(renderTexture, destination);
}
}