Search Unity

Write to Texture3D using a Compute shader

Discussion in 'Shaders' started by xzodia, Jul 25, 2017.

  1. xzodia

    xzodia

    Joined:
    Jan 24, 2013
    Posts:
    50
    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


    Code (csharp):
    1. public static class ComputeTexture
    2. {
    3.     public static RenderTexture Create(ComputeShader shader, int width, int height, int depth)
    4.     {
    5.         var texture = new RenderTexture(width, height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
    6.         texture.isVolume = true;
    7.         texture.dimension = UnityEngine.Rendering.TextureDimension.Tex3D;
    8.         texture.volumeDepth = depth;
    9.         texture.enableRandomWrite = true;
    10.      
    11.         texture.Create();
    12.  
    13.         Fill(shader, texture);
    14.  
    15.         return texture;
    16.     }
    17.  
    18.     public static void Fill(ComputeShader shader, RenderTexture texture)
    19.     {
    20.         int kernelHandle = shader.FindKernel("CSMain");
    21.  
    22.         shader.SetTexture(kernelHandle, "Result", texture);
    23.         shader.Dispatch(kernelHandle, texture.width / 8, texture.height / 8, texture.depth / 8);
    24.     }
    25. }
    26.  
    27. #pragma kernel CSMain
    28.  
    29. RWTexture3D<float4> Result;
    30.  
    31. [numthreads(8,8,8)]
    32. void CSMain(uint3 id : SV_DispatchThreadID)
    33. {
    34.     Result[id.xyz] = float4(1, 0, 0, 1);
    35. }
    36.  
     
  2. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Hey, have you found the solution? I have exactly the same problem, it just wont write for some reason
     
  3. xzodia

    xzodia

    Joined:
    Jan 24, 2013
    Posts:
    50
    Unfortunately no, in the end I just did it on the cpu.
     
    customphase likes this.
  4. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Ah, its a shame. Ill keep looking then, thanks for reply.
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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?
     
    customphase likes this.
  6. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    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.
     
    Last edited: Nov 5, 2017
  7. mipkoz

    mipkoz

    Joined:
    Mar 26, 2018
    Posts:
    2
    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);