Search Unity

Question Ulong in Compute Shaders

Discussion in 'Shaders' started by Maxeaman, Dec 13, 2022.

  1. Maxeaman

    Maxeaman

    Joined:
    May 1, 2020
    Posts:
    14
    Is there any way that works to send and receive 64 bit ints to and from a Compute Shader? I've been asking ChatGPT and got a bunch of ways that don't work.
     
  2. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    @Maxeaman

    Minimal Working Example:

    Code (CSharp):
    1. // Example of using 64-bit unsigned integers in Unity compute shader
    2. // Requirements: Unity 2020.2.0a8 or later and active DX12 graphics API
    3. using UnityEngine;
    4.  
    5. public class UnsignedInteger64 : MonoBehaviour
    6. {
    7.     [SerializeField] ComputeShader _ComputeShader;
    8.  
    9.     void Start()
    10.     {
    11.         if (_ComputeShader == null) return;
    12.         ComputeBuffer reader = new ComputeBuffer(4, sizeof(ulong), ComputeBufferType.Default);
    13.         reader.SetData(new ulong[] {172439890993963ul, 657367095657329ul, 277347196953998ul, 844613309877278ul});
    14.         _ComputeShader.SetBuffer(0, "_Reader", reader);
    15.         ComputeBuffer writer = new ComputeBuffer(2, sizeof(ulong), ComputeBufferType.Default);
    16.         _ComputeShader.SetBuffer(0, "_Writer", writer);
    17.         _ComputeShader.Dispatch(0, writer.count, 1, 1); // execute compute shader
    18.         ulong[] result = new ulong[2];
    19.         writer.GetData( result );
    20.         Debug.Log( "172439890993963 + 657367095657329 = " + result[0].ToString()); //   829 806 986 651 292
    21.         Debug.Log( "277347196953998 + 844613309877278 = " + result[1].ToString()); // 1 121 960 506 831 276
    22.         reader.Release();
    23.         writer.Release();
    24.     }
    25. }
    Code (CSharp):
    1. #pragma kernel CSMain
    2. #pragma use_dxc
    3.  
    4. StructuredBuffer<uint64_t> _Reader;
    5. RWStructuredBuffer<uint64_t> _Writer;
    6.  
    7. [numthreads(1,1,1)]
    8. void CSMain (uint3 id : SV_DispatchThreadID)
    9. {
    10.     _Writer[id.x] = _Reader[id.x * 2] + _Reader[id.x * 2 + 1];
    11. }
    upload_2022-12-13_17-14-34.png
     
  3. Maxeaman

    Maxeaman

    Joined:
    May 1, 2020
    Posts:
    14
    Maybe I don't have the right requirements? When I copy that exactly, I get that the answer = 0. How can I check? I'm using Unity 2021.3 and I have the latest version of DirectX (DirectX12, checked using dxdiag).

    There is another possible problem though. In trying to figure this out before, it was suggested I use uint64_t, but that was undefined, so I made a #define for it, but it still didn't work, yet even after I removed the #define from my code, that type still showed up as green in my VB editor, and didn't specifically say there was an error. I tried your code in a new project, but could unity be remembering that previous definition somewhere?

    Edit: I tried using #undef uint64_t and compiling and running it twice, then removed it and compiled and run it twice, and got the same results each time. I have checked that my GPU drivers are the correct versions as well.
     
    Last edited: Dec 13, 2022
  4. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    "Active DX12 graphics API" it means that Unity uses DX12, to enable DX12 check this:

    upload_2022-12-13_19-6-6.png
     
  5. Maxeaman

    Maxeaman

    Joined:
    May 1, 2020
    Posts:
    14
    That was the missing piece! Thank you so much!!