- Home /
Question by
flamegear123 · Nov 26, 2021 at 11:20 PM ·
shaderstexture2dcompute shader
Compute Shader Outputting Black
Hey All.
Just trying to get my head around compute shaders, and just wanted to set an input texture as the output texture.
Whenever I do this, I simply get a black output from the processed texture (code below) that is being rendered to a RawImage UI Element.
Am I indexing the input image wrong maybe?
Any help would be much appreciated.
Compute shader:
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Input Texture
Texture2D<float4> imageInput;
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWTexture2D<float4> Result;
[numthreads(1,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
// TODO: insert actual code here!
Result[id.xy] = imageInput[id.xy];
}
C# Script:
public class TestCamera : MonoBehaviour
{
public RenderTexture testTexture;
public RenderTexture processedTexture;
public RawImage testUI;
public ComputeShader computeShader;
public GameObject boat;
// Start is called before the first frame update
void Start()
{
testTexture = GetComponent<Camera>().targetTexture;
// Set Up Render Texture
processedTexture = new RenderTexture(256, 256, 24);
processedTexture.enableRandomWrite = true;
processedTexture.Create();
// Dispatch Compute Shader
int shaderKernal = computeShader.FindKernel("CSMain");
computeShader.SetTexture(shaderKernal, "Result", processedTexture);
computeShader.SetTexture(shaderKernal, "imageInput", testTexture);
computeShader.Dispatch(shaderKernal, testTexture.width, testTexture.height, 1);
}
// Update is called once per frame
void Update()
{
testUI.texture = processedTexture;
transform.position = new Vector3(boat.transform.position.x, transform.position.y, boat.transform.position.z);
}
}
Comment