Firstly I am in Unity 2021.3.11f1 using DX12.
So the situation is the following. I have a large 3D RenderTexture, and I have a large 3D Texture3D with the same data, same format, same dimensions, same size in memory, as the RenderTexture.
Now I have a compute shader that samples from these many times (volume rendering).
- If I use the Texture3D, the compute shader takes 50ms to run in total.
- If I use the RenderTexture instead, changing nothing else, the compute shader takes 170ms to run in total.
Why does this happen? It also happens with large 2D textures as well.
I would just say whatever and use the Texture2D/3D, but the problem with that is that not only does it take a lot longer to create a new large Texture2D/3D, it creates large lag spikes when I need to copy from the RenderTexture to the Texture2D (I use another compute shader to write the data to the texture), which is a big problem because in the case of the 2D Texture/Rendertexture that gets recreated frequently (dynamic atlas).
What can I do and why is this happening in the first place?.
Code if I want to use a rendertexture:
RenderTexture TempVolumeTex = new RenderTexture((int)OrigionalSize.x, (int)OrigionalSize.y, 0, RenderTextureFormat.RFloat, RenderTextureReadWrite.sRGB);
TempVolumeTex.enableRandomWrite = true;
TempVolumeTex.volumeDepth = (int)OrigionalSize.z;
TempVolumeTex.dimension = UnityEngine.Rendering.TextureDimension.Tex3D;
TempVolumeTex.Create();
VolumeShader.SetTexture(1, "DDATextureWrite", TempVolumeTex);
VolumeShader.Dispatch(1, Mathf.CeilToInt(OrigionalSize.x / 8.0f), Mathf.CeilToInt(OrigionalSize.y / 8.0f), Mathf.CeilToInt(OrigionalSize.z / 8.0f));
and if I want to use a texture3d, I still have to do the exact same as above, but then I have to do:
VolumeTex = new Texture3D((int)OrigionalSize.x, (int)OrigionalSize.y, (int)OrigionalSize.z, TextureFormat.RFloat, false);
Graphics.CopyTexture(TempVolumeTex, VolumeTex);