Search Unity

Problem with compute shader writing to RWTexture3D

Discussion in 'General Graphics' started by thomasluce, Jun 25, 2021.

  1. thomasluce

    thomasluce

    Joined:
    Mar 8, 2019
    Posts:
    16
    Hello!

    So I have a working 3d noise generation regular shader (non-compute) that I'm working on converting to a compute shader so that I can avoid some weirdness with building 3d textures out of a crap-load of slices, and instead do it directly (can't do on CPU because it's multiple octave worley and simplex noise to make clouds, so making it CPU bound is too slow, in case you're curious).

    To start, I'm making a super simple setup to confirm that things work:

    Code (CSharp):
    1. public void Compute() {
    2.         RenderTextureDescriptor desc = new
    3.         RenderTextureDescriptor(resolution, resolution,
    4.         RenderTextureFormat.ARGB32);
    5.         desc.volumeDepth = resolution;
    6.         desc.dimension = UnityEngine.Rendering.TextureDimension.Tex3D;
    7.         desc.enableRandomWrite = true;
    8.         RenderTexture rt = new RenderTexture(desc);
    9.  
    10.         computeShader.SetTexture(0, "Result", rt);
    11.         computeShader.Dispatch(0, threadCount, threadCount, threadCount);
    12.  
    13.         GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_MainTex", rt);
    14. }
    With the following super simple compute shader

    Code (HLSL):
    1.  
    2. #pragma kernel CSMain
    3. static const int numThreads = 8;
    4. RWTexture3D<float4> Result;
    5.  
    6. [numthreads(numThreads, numThreads, numThreads)]
    7. void CSMain (uint3 id : SV_DispatchThreadID)
    8. {
    9.     Result[id] = float4(1, 0, 0, 1);
    10. }
    11.  
    My goal is to just verify that I can make a 3D render texture that at the moment should just be a red cube. I set that as my "_MainTex" property on a simple volume-shading material that I've already verified works correctly with other 3d textures. The problem is that my RenderTexture comes out all black as if the compute shader just isn't running, or is discarding the results, or something.

    I'm hoping other people have run into similar problems and might be able to help me out. I've been banging my head on this for a few days now and am out of ideas. I'm happy to give more complete code (what I have above is obviously edited down), or more information if you think it's helpful.

    Cheers!
     
  2. xgonzal2

    xgonzal2

    Joined:
    Jul 3, 2012
    Posts:
    62
    Have you tried calling the render texture's Create function after allocating?
     
  3. thomasluce

    thomasluce

    Joined:
    Mar 8, 2019
    Posts:
    16
    ....

    Damnit.


    Well, thank you! :) Instead of deleting this post in embarrassment, I'm going to leave it here as a shining beacon to myself and others to actually read the f'ing manual, not just skim it.

    Cheers
     
    xgonzal2 likes this.