I have a render texture defined like so:
Render = new RenderTexture(512, 512, 8);
Render.enableRandomWrite = true;
Render.filterMode = FilterMode.Point;
Render.Create();
And a RawImage defined:
Img = gameobject.GetComponent<RawImage>();
Img.rectTransform.sizeDelta = new Vector2(1, 1);
Img.texture = Render;
I then have a compute shader which is simply the base code that unity provides:
// 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;
[numthreads(1,1,1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
// TODO: insert actual code here!
Result[id.xy] = float4(id.x & id.y, (id.x & 15) / 15.0, (id.y & 15) / 15.0, 0.0);
}
I then call the compute shader such that it will render the sierpiński triangle that the unity base code generates onto my raw image.
Compute.SetTexture(ComputeKernel, "Result", Render);
Compute.Dispatch(ComputeKernel, 512, 512, 1);
The problem is that the raw image remains blank. Using the inspect I can see that the render texture has generated the correct image, but for somereason raw image doesn’t show it.
If I use a normal sprite image, or convert the render texture to a Texture2D using ReadPixels it works fine, however I should not need to do this with a render texture and raw image right?
Image of the raw image not working, but the render texture is successful as shown in the inspector:
For some reason, when I set the material of the raw image to Default-Line it works for some reason, but when the material is blank or Sprites-Default it does not work:
I have a thought that this is due to the texture format as unity uses DXT5 compression for Texture2D and Sprites, but I am not sure how to get this compression from the compute shader. I do not want to have to convert my render texture to a texture 2D either.
If someone could help me solve this issue that would be great
Additional information:
- Unity version 2020.1.4f1
- I am using Universal RP in my project (I have tested this in a separate project without Universal RP and the same problem occurs)