Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Writing to RWTexture2D<half4> in a compute shader doesn't work on Metal

Discussion in 'General Graphics' started by alfred767569085_unity, Jan 13, 2024.

  1. alfred767569085_unity


    Jan 13, 2024
    I'm writing to a texture in a compute shader.
    The texture is created with GraphicsFormat.R16G16B16A16_UNorm, and is declared as RWTexture2D<half4> on the shader side.
    It's working nicely on windows and android. But it doesn't work on metal with iphone or mac.

    After some trying, I find that changing its type to RWTexture2D<float4> solves the problem, but still not know why. Is it related to some Metal specification?

    Compute Shader (which is just the default one created by unity, RWTexture2D<float4> replaced with RWTexture2D<half4>):
    Code (CSharp):
    1. // Each #kernel tells which function to compile; you can have many kernels
    2. #pragma kernel CSMain
    4. // Create a RenderTexture with enableRandomWrite flag and set it
    5. // with cs.SetTexture
    6. RWTexture2D<half4> Result;
    8. [numthreads(8,8,1)]
    9. void CSMain (uint3 id : SV_DispatchThreadID)
    10. {
    11.     // TODO: insert actual code here!
    13.     Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0);
    14. }
    Last edited: Jan 15, 2024