- Home /
[Unity SRP] How do I clear 3d RW RenderTexture?
I'm trying to port my real-time GPU voxelization from standalone DirectX11 application into Unity SRP. The approach I'm using is basically this https://developer.nvidia.com/content/basics-gpu-voxelization
I'm using ReadWrite 3d RenderTexture to write voxel data in pixel shader.
Everything is working fine except I have no idea how to clear my RenderTexture every frame before doing the voxelization pass.
for (int i = 0; i < voxelSpaceDimensions.z; i++) {
CoreUtils.SetRenderTarget(
voxelizationBuffer, // <- Command buffer.
voxelSpace.colorBuffer, // <- RW 3d RenderTexture.
ClearFlag.Color,
Color.clear,
0, // <- Miplevel.
CubemapFace.Unknown,
i // <- 3d RenderTexture Layer.
);
}
Frame debug shows everything as expected, but in reality the texture has not been cleared. 
Answer by detectiveLosos · Sep 04, 2019 at 01:33 AM
Well, as it always happens, as soon as I posted this question I tinkered a bit more with the code and found a solution.
As stated here some platforms let you bind entire 3d render texture at once (SetRenderTarget with depthSlice = -1).
So I got rid of the loop, put -1 as depthSlice parameter and everything worked:
CoreUtils.SetRenderTarget(
voxelizationBuffer,
voxelSpace,
ClearFlag.Color, Color.clear,
0, CubemapFace.Unknown, -1
);
// Don't forget to change the render target back.
CoreUtils.SetRenderTarget(
voxelizationBuffer,
BuiltinRenderTextureType.CameraTarget
);
Your answer