Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Compute shaders in macOS and iOS

Discussion in 'Shaders' started by Game-Whiz, May 5, 2017.

  1. Game-Whiz

    Game-Whiz

    Joined:
    Nov 10, 2011
    Posts:
    122
    In 5.6.0f3 I was able to get a compute shader working in macOS Sierra and iOS 10 as long as I use vert/frag shaders. If I try to use surface shaders I get an error whenever I try to use a StructuredBuffer.

    From what I read this is an SM5 feature, only available for >=DX11, GL >=4.3 and Vulkan. Metal is missing, which is odd since SM5 with Metal is supported in the Unreal engine. Is an SM5 Metal implementation planned?

    Thanks in advance.
     
  2. tom_bb

    tom_bb

    Joined:
    Sep 21, 2016
    Posts:
    8
    On this doc page (https://docs.unity3d.com/Manual/SL-PlatformDifferences.html) under the header DirectX 11 (DX11) HLSL syntax in Shaders, there is a bit on using DX11 structured buffers in compute shaders, as the surface shader doesn't understand them, it says to surround them with SHADER_API_D3D11, but I'm not sure if there is an equivalent for GL, Vulkan, or Metal with regards to SM5 and above stuff
     
  3. Game-Whiz

    Game-Whiz

    Joined:
    Nov 10, 2011
    Posts:
    122
    I placed the StructuredBuffer inside a D3D11 ifdef, compiled the SurfaceShader to a Vertex/Frag shader (via Show Generated Code in the inspector), removed the ifdef from the generated code and it worked :). This means that most likely Unity will eventually support structured buffers in surface shaders for metal and vulkan since it's probably a question of just changing the surface shader's compiler.
     
    tom_bb likes this.
  4. acc01ade

    acc01ade

    Joined:
    Nov 25, 2014
    Posts:
    1
    Hey Game-Whiz, can you share an example of how you got StructuredBuffer to work in a vert/frag shader for Metal?
    I got compute shaders working, but getting some weird Metal errors when I try to use a StructuredBuffer in my vert/frag shader.

    Thanks.
     
  5. sam-perisentient

    sam-perisentient

    Joined:
    Aug 11, 2013
    Posts:
    31
    Hey Game-Whiz and others,

    Just got it by the Surface shader compiler with this:
    #if defined(SHADER_API_D3D11) || defined(SHADER_API_METAL)

    Instead of the recommended:
    #ifdef SHADER_API_D3D11

    It works for me, and I would expect it do the right thing on Windows D3-11, but have only tried the updated shader with Mac so far (where it works fine).

    Cheers
     
  6. sam-perisentient

    sam-perisentient

    Joined:
    Aug 11, 2013
    Posts:
    31
    @thisisvitto in my vert/frag shaders that use StructuredBuffers there were no changes to the actual shaders for Mac to make them work.
    Things to check though:
    * Make sure your editor is actually running using Metal. Tick "Metal Editor Support" in Player Settings, and unselect "Auto Graphics API for Mac", and put "Metal" at the top of the "Graphics APIs for Mac" list.
    * Make sure your shader has "#pragma target 4.5" not "#pragma target 5.0"

    That said though, as at 2017.2 it feels a bit unstable. I've had some weird rendering glitches in the editor, and a full system crash in the space of a few minutes. It might just be my test machine (which is quite old), or the "experimental support" my be just that.