Search Unity

Writing to RenderTexture in a compute shader doesn't work on Metal

Discussion in 'Shaders' started by camel82106, Jun 20, 2018.

  1. camel82106

    camel82106

    Joined:
    Jul 2, 2013
    Posts:
    304
    Hi,
    I'm writing to RWTexture2D<float4> texture in a compute shader. It's working nicely on desktop with directx. But it doesn't work on metal with iphone or ipad.

    I have tried it on 2017.4.4 and newest beta 2018.2b9 too.

    Compute Shader
    Code (CSharp):
    1. RWTexture2D<float4> textureOut;
    2. StructuredBuffer<float4> colors;  
    3.  
    4. #pragma kernel pixelCalc    
    5.  
    6. [numthreads(16,16,1)]          
    7. void pixelCalc (uint2 id : SV_DispatchThreadID){
    8.      textureOut[id.xy] = colors[id.x + id.y * 250];
    9. }
    CPU side:
    Code (CSharp):
    1. this.outputTexture = new RenderTexture(250, 250, 32);
    2.             this.outputTexture.enableRandomWrite = true;
    3.             this.outputTexture.useMipMap = false;
    4.             this.outputTexture.Create();
    5.  
    6. this.colorArray = new Color[250 * 250];
    7.  
    8. // Color size is four values of four bytes, so 4 * 4
    9.             this.colorsBuffer = new ComputeBuffer(this.colorArray.Length, 4 * 4);
    10.  
    11.             // again, we're setting color array to the buffer
    12.             colorsBuffer.SetData(colorArray);
    13.  
    14.  
    15.             var overlayGameObject = GameObject.Find("Overlay");
    16.             var renderer = overlayGameObject.GetComponent<Renderer>();
    17.             renderer.material.mainTexture = this.outputTexture;
    18.  
    19. ar kernelIndex = this.shader.FindKernel("pixelCalc");
    20.  
    21.             this.shader.SetBuffer(kernelIndex, "colors", colorsBuffer);
    22.             this.shader.SetTexture(kernelIndex, "textureOut", this.outputTexture);
    23.  
    24.             this.shader.Dispatch(kernelIndex, 16, 16, 1);
    Am I doing something wrong or render texture simply doesn't work in metal and compute shaders?

    P.S.: I have tried an approach used here. But without success:
    https://forum.unity.com/threads/con...ass-shader-to-unity-hlsl.418238/#post-3478159

    Basically passing StructuredBuffer in normal shader. And bypassing usage of render texture.
    Thanks
    Peter
     
  2. camel82106

    camel82106

    Joined:
    Jul 2, 2013
    Posts:
    304
    Lowering threads to 8 8 1 helped. I will experiment with it further but it looks like it's not working with 16 16 1.

    But at least it's working with structured buffer and render rexture
     
    Zolden likes this.
  3. Zolden

    Zolden

    Joined:
    May 9, 2014
    Posts:
    141
    Wow, nice find. I thought it doesn't work at all.