Write to Texture3D using a Compute shader

I’m having some trouble getting my compute shader to output to a 3D texture. I managed a 2D texture just fine and as far as I can tell I have made all the nessessary changes for 3D so I’m at a bit of a loss as to how to proceed. The code is pretty simple, am I missing something? Thanks

public static class ComputeTexture
{
    public static RenderTexture Create(ComputeShader shader, int width, int height, int depth)
    {
        var texture = new RenderTexture(width, height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
        texture.isVolume = true;
        texture.dimension = UnityEngine.Rendering.TextureDimension.Tex3D;
        texture.volumeDepth = depth;
        texture.enableRandomWrite = true;
     
        texture.Create();

        Fill(shader, texture);

        return texture;
    }

    public static void Fill(ComputeShader shader, RenderTexture texture)
    {
        int kernelHandle = shader.FindKernel("CSMain");

        shader.SetTexture(kernelHandle, "Result", texture);
        shader.Dispatch(kernelHandle, texture.width / 8, texture.height / 8, texture.depth / 8);
    }
}

#pragma kernel CSMain

RWTexture3D<float4> Result;

[numthreads(8,8,8)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
    Result[id.xyz] = float4(1, 0, 0, 1);
}

Hey, have you found the solution? I have exactly the same problem, it just wont write for some reason

Unfortunately no, in the end I just did it on the cpu.

1 Like

Ah, its a shame. Ill keep looking then, thanks for reply.

The code & shader looks extremely like one of the few we have in our automated graphics tests, and it “works for us” there. So yeah a bit puzzled at why it does not work for you. Which platform (OS, graphics API, GPU) you’re on?

1 Like

For me its Win7 x64, DX11, GTX 1050 TI. Unity 2017.2. Just tried with OpenGLES3, also didnt work. Also tried regular Texture3D, and that failed as well.

Not sure if this is the only issue, but the line

shader.Dispatch(kernelHandle, texture.width / 8, texture.height / 8, texture.depth / 8);

Should be

shader.Dispatch(kernelHandle, texture.width / 8, texture.height / 8, texture.volumeDepth / 8);

1 Like