Search Unity

Question How to use CUDA in Unity?

Discussion in 'General Discussion' started by eaglemo, Feb 7, 2023.

  1. eaglemo

    eaglemo

    Joined:
    Feb 1, 2023
    Posts:
    8
    If I want to directly call the algorithm in the native graphics card driver,I need to use CUDA stream,so how to use cuda in unity? Or how to integrate CUDA? I would be grateful if someone could help me answer the question.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,573
    You can use compute shaders instead.

    If you want CUDA specifically, you'd need to write a native plugin wrapper for it. Someone might've created one already.
     
  3. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
  4. eaglemo

    eaglemo

    Joined:
    Feb 1, 2023
    Posts:
    8
  5. eaglemo

    eaglemo

    Joined:
    Feb 1, 2023
    Posts:
    8
    Yeah, but I haven't found it yet.Thank you
     
  6. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    It is wrapper of a pipe to nVidia from Unity..what you asked for in OP. Other than that compute shaders may do the trick. If these don't do it then properly pose the question explaining why neither solution fits your bill..
     
  7. eaglemo

    eaglemo

    Joined:
    Feb 1, 2023
    Posts:
    8
    sry,I didn't make it clear. I want to use NVIDIA's Optix denoising algorithm to denoise. Unity integrated denoising algorithm takes too long to transmit data from GPU-CPU-GPU. Therefore, I want to know how to get through Unity and Cuda so that I can implement my own denoising algorithm.
     
  8. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,983
    Then that setting is in the lighting settings part of unity, it will only be available if you have a GPU that supports it
     
  9. eaglemo

    eaglemo

    Joined:
    Feb 1, 2023
    Posts:
    8
    Yes, I have tried and applied it successfully. But it takes too long. I want to use my own denoising algorithm, but I don't know how to interoperate Unity and Cuda
     
  10. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,983
    Like has already been said, then you need to write a plugin yourself
     
  11. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    We don't even need to write a native plugin wrapper.

    If we have a PTX assembly code, we can execute GPGPU program directly from C#. Only required are:
    - Unity Editor;
    - GPU NVIDIA Geforce (and drivers, because they install nvcuda.dll in system);
    - WIndows 7/8/10 64-bit

    Example with procedural particles movement (full source code):

    - class CUDA contains framework;
    - class PTX contains assembly code;

    Author: Przemyslaw Zaworski

    Edit 17.05.2023: fixed memory leak

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Runtime.InteropServices;
    5.  
    6. public class CUDA : MonoBehaviour
    7. {
    8.    [DllImport("nvcuda.dll")]
    9.    static extern int cuInit(uint flags);
    10.  
    11.    [DllImport("nvcuda.dll")]
    12.    static extern int cuDeviceGet(out IntPtr device, int ordinal);
    13.  
    14.    [DllImport("nvcuda.dll", EntryPoint="cuCtxCreate_v2")]
    15.    static extern int cuCtxCreate(out IntPtr pctx, uint flags, IntPtr device);
    16.  
    17.    [DllImport("nvcuda.dll", EntryPoint = "cuMemAlloc_v2")]
    18.    static extern int cuMemAlloc(out IntPtr dptr, uint bytesize);
    19.  
    20.    [DllImport("nvcuda.dll")]
    21.    static extern int cuModuleLoadDataEx(out IntPtr module, IntPtr image, uint numOptions, uint options, uint optionValues);
    22.  
    23.    [DllImport("nvcuda.dll")]
    24.    static extern int cuModuleGetFunction(out IntPtr hfunc, IntPtr hmod, string name);
    25.  
    26.    [DllImport("nvcuda.dll")]
    27.    static extern int cuLaunchKernel(IntPtr f, uint gx, uint gy, uint gz, uint bx, uint by, uint bz, uint shared, IntPtr stream, IntPtr[] args, IntPtr[] extra);
    28.  
    29.    [DllImport("nvcuda.dll", EntryPoint = "cuMemcpyDtoH_v2")]
    30.    static extern int cuMemcpyDtoH(IntPtr dstHost, IntPtr srcDevice, uint byteCount);
    31.  
    32.    [DllImport("nvcuda.dll", EntryPoint = "cuMemFree_v2")]
    33.    static extern int cuMemFree(IntPtr dptr);
    34.  
    35.    Texture2D _Texture;
    36.    int _Resolution = 1024;
    37.    int _Memory;
    38.    IntPtr _Function, _Host, _Device;
    39.    GCHandle[] _GCHandles;
    40.    IntPtr[] _Params;
    41.  
    42.    void Start()
    43.    {
    44.        _Memory = _Resolution * _Resolution * 4;
    45.        cuInit(0);
    46.        cuDeviceGet(out IntPtr cuDevice, 0);
    47.        cuCtxCreate(out IntPtr context, 0, cuDevice);
    48.        cuMemAlloc(out _Device, (uint)_Memory);
    49.        byte[] source = System.Text.Encoding.ASCII.GetBytes(PTX.Kernel);
    50.        IntPtr moduleData = Marshal.AllocHGlobal(source.Length);
    51.        Marshal.Copy(source, 0, moduleData, source.Length);
    52.        cuModuleLoadDataEx(out IntPtr module, moduleData, 0, 0, 0);
    53.        cuModuleGetFunction(out _Function, module, "mainImage");
    54.        _GCHandles = new GCHandle[2] {GCHandle.Alloc(_Device, GCHandleType.Pinned), GCHandle.Alloc(Time.time, GCHandleType.Pinned)};
    55.        _Params = new IntPtr[2] {_GCHandles[0].AddrOfPinnedObject(), _GCHandles[1].AddrOfPinnedObject()};
    56.        _Host = Marshal.AllocHGlobal(_Memory);
    57.        _Texture = new Texture2D(_Resolution, _Resolution, TextureFormat.RGBA32, false);
    58.    }
    59.  
    60.    void Update ()
    61.    {
    62.        _GCHandles[1] = GCHandle.Alloc(Time.time, GCHandleType.Pinned);
    63.        _Params[1] = _GCHandles[1].AddrOfPinnedObject();
    64.        cuLaunchKernel(_Function, (uint)_Resolution/8, (uint)_Resolution/8, 1, 8, 8, 1, 0, IntPtr.Zero, _Params, new IntPtr[1]);
    65.        cuMemcpyDtoH(_Host, _Device, (uint)_Memory);
    66.        _Texture.LoadRawTextureData(_Host, _Memory);
    67.        _Texture.Apply();
    68.        _GCHandles[1].Free();
    69.    }
    70.  
    71.    void OnGUI()
    72.    {
    73.        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), _Texture, ScaleMode.StretchToFill, true);
    74.    }
    75.  
    76.    void OnDestroy()
    77.    {
    78.        _GCHandles[0].Free();
    79.        cuMemFree(_Device);
    80.        Marshal.FreeHGlobal(_Host);
    81.        Destroy(_Texture);
    82.    }
    83. }
    84.  
    85. public class PTX
    86. {    // Source code of CUDA PTX assembly language, example program generates molecular movement. Compiled program is executed on GPU.
    87.    public static string Kernel =
    88.    @"
    89.       .version 8.0
    90.       .target sm_52
    91.       .address_size 64
    92.  
    93.  
    94.       .visible .entry mainImage(.param .u64 _Z9mainImageP6uchar4f_param_0, .param .f32 _Z9mainImageP6uchar4f_param_1)
    95.       {
    96.           .reg .b16     %rs<4>;
    97.           .reg .f32     %f<313>;
    98.           .reg .b32     %r<13>;
    99.           .reg .f64     %fd<25>;
    100.           .reg .b64     %rd<5>;
    101.           ld.param.u64     %rd1, [_Z9mainImageP6uchar4f_param_0];
    102.           ld.param.f32     %f1, [_Z9mainImageP6uchar4f_param_1];
    103.           cvta.to.global.u64     %rd2, %rd1;
    104.           mov.u32     %r1, %ctaid.x;
    105.           mov.u32     %r2, %ntid.x;
    106.           mov.u32     %r3, %tid.x;
    107.           mad.lo.s32     %r4, %r1, %r2, %r3;
    108.           mov.u32     %r5, %ctaid.y;
    109.           mov.u32     %r6, %ntid.y;
    110.           mov.u32     %r7, %tid.y;
    111.           mad.lo.s32     %r8, %r5, %r6, %r7;
    112.           shl.b32     %r9, %r8, 10;
    113.           add.s32     %r10, %r9, %r4;
    114.           cvt.rn.f32.u32     %f2, %r4;
    115.           cvt.rn.f32.u32     %f3, %r8;
    116.           mul.f32     %f4, %f2, 0f3A800000;
    117.           cvt.f64.f32     %fd1, %f4;
    118.           mul.f64     %fd2, %fd1, 0d401551EB851EB852;
    119.           cvt.rn.f32.f64     %f5, %fd2;
    120.           mul.f32     %f6, %f3, 0f3A800000;
    121.           cvt.f64.f32     %fd3, %f6;
    122.           mul.f64     %fd4, %fd3, 0d401551EB851EB852;
    123.           cvt.rn.f32.f64     %f7, %fd4;
    124.           cvt.f64.f32     %fd5, %f5;
    125.           add.f64     %fd6, %fd5, 0dBFE999999999999A;
    126.           cvt.rn.f32.f64     %f8, %fd6;
    127.           cvt.f64.f32     %fd7, %f7;
    128.           add.f64     %fd8, %fd7, 0dBFE3851EB851EB85;
    129.           cvt.rn.f32.f64     %f9, %fd8;
    130.           add.f32     %f10, %f1, 0fBFC90FDB;
    131.           div.rn.f32     %f11, %f10, 0f40C90FDB;
    132.           cvt.rmi.f32.f32     %f12, %f11;
    133.           sub.f32     %f13, %f11, %f12;
    134.           fma.rn.f32     %f14, %f13, 0f40000000, 0fBF800000;
    135.           abs.f32     %f15, %f14;
    136.           mul.f32     %f16, %f15, %f15;
    137.           add.f32     %f17, %f15, %f15;
    138.           mov.f32     %f18, 0f40400000;
    139.           sub.f32     %f19, %f18, %f17;
    140.           mul.f32     %f20, %f16, %f19;
    141.           fma.rn.f32     %f21, %f20, 0f40000000, 0fBF800000;
    142.           add.f32     %f22, %f21, %f8;
    143.           add.f32     %f23, %f1, 0f3FC90FDB;
    144.           add.f32     %f24, %f23, 0fBFC90FDB;
    145.           div.rn.f32     %f25, %f24, 0f40C90FDB;
    146.           cvt.rmi.f32.f32     %f26, %f25;
    147.           sub.f32     %f27, %f25, %f26;
    148.           fma.rn.f32     %f28, %f27, 0f40000000, 0fBF800000;
    149.           abs.f32     %f29, %f28;
    150.           mul.f32     %f30, %f29, %f29;
    151.           add.f32     %f31, %f29, %f29;
    152.           sub.f32     %f32, %f18, %f31;
    153.           mul.f32     %f33, %f30, %f32;
    154.           fma.rn.f32     %f34, %f33, 0f40000000, 0fBF800000;
    155.           add.f32     %f35, %f34, %f9;
    156.           cvt.rmi.f32.f32     %f36, %f22;
    157.           sub.f32     %f37, %f22, %f36;
    158.           cvt.rmi.f32.f32     %f38, %f35;
    159.           sub.f32     %f39, %f35, %f38;
    160.           add.f32     %f40, %f37, 0fBF000000;
    161.           add.f32     %f41, %f39, 0fBF000000;
    162.           mul.f32     %f42, %f41, %f41;
    163.           fma.rn.f32     %f43, %f40, %f40, %f42;
    164.           mov.f32     %f44, 0f3F000000;
    165.           min.f32     %f45, %f44, %f43;
    166.           add.f64     %fd9, %fd5, 0dBFD70A3D70A3D70A;
    167.           cvt.rn.f32.f64     %f46, %fd9;
    168.           add.f64     %fd10, %fd7, 0dBFC999999999999A;
    169.           cvt.rn.f32.f64     %f47, %fd10;
    170.           add.f32     %f48, %f1, 0fBF800000;
    171.           add.f32     %f49, %f48, 0fBFC90FDB;
    172.           div.rn.f32     %f50, %f49, 0f40C90FDB;
    173.           cvt.rmi.f32.f32     %f51, %f50;
    174.           sub.f32     %f52, %f50, %f51;
    175.           fma.rn.f32     %f53, %f52, 0f40000000, 0fBF800000;
    176.           abs.f32     %f54, %f53;
    177.           mul.f32     %f55, %f54, %f54;
    178.           add.f32     %f56, %f54, %f54;
    179.           sub.f32     %f57, %f18, %f56;
    180.           mul.f32     %f58, %f55, %f57;
    181.           fma.rn.f32     %f59, %f58, 0f40000000, 0fBF800000;
    182.           add.f32     %f60, %f59, %f46;
    183.           add.f32     %f61, %f48, 0f3FC90FDB;
    184.           add.f32     %f62, %f61, 0fBFC90FDB;
    185.           div.rn.f32     %f63, %f62, 0f40C90FDB;
    186.           cvt.rmi.f32.f32     %f64, %f63;
    187.           sub.f32     %f65, %f63, %f64;
    188.           fma.rn.f32     %f66, %f65, 0f40000000, 0fBF800000;
    189.           abs.f32     %f67, %f66;
    190.           mul.f32     %f68, %f67, %f67;
    191.           add.f32     %f69, %f67, %f67;
    192.           sub.f32     %f70, %f18, %f69;
    193.           mul.f32     %f71, %f68, %f70;
    194.           fma.rn.f32     %f72, %f71, 0f40000000, 0fBF800000;
    195.           add.f32     %f73, %f72, %f47;
    196.           cvt.rmi.f32.f32     %f74, %f60;
    197.           sub.f32     %f75, %f60, %f74;
    198.           cvt.rmi.f32.f32     %f76, %f73;
    199.           sub.f32     %f77, %f73, %f76;
    200.           add.f32     %f78, %f75, 0fBF000000;
    201.           add.f32     %f79, %f77, 0fBF000000;
    202.           mul.f32     %f80, %f79, %f79;
    203.           fma.rn.f32     %f81, %f78, %f78, %f80;
    204.           min.f32     %f82, %f45, %f81;
    205.           add.f64     %fd11, %fd5, 0dBFE3333333333333;
    206.           cvt.rn.f32.f64     %f83, %fd11;
    207.           add.f64     %fd12, %fd7, 0dBFCEB851EB851EB8;
    208.           cvt.rn.f32.f64     %f84, %fd12;
    209.           add.f32     %f85, %f1, 0fC0000000;
    210.           add.f32     %f86, %f85, 0fBFC90FDB;
    211.           div.rn.f32     %f87, %f86, 0f40C90FDB;
    212.           cvt.rmi.f32.f32     %f88, %f87;
    213.           sub.f32     %f89, %f87, %f88;
    214.           fma.rn.f32     %f90, %f89, 0f40000000, 0fBF800000;
    215.           abs.f32     %f91, %f90;
    216.           mul.f32     %f92, %f91, %f91;
    217.           add.f32     %f93, %f91, %f91;
    218.           sub.f32     %f94, %f18, %f93;
    219.           mul.f32     %f95, %f92, %f94;
    220.           fma.rn.f32     %f96, %f95, 0f40000000, 0fBF800000;
    221.           add.f32     %f97, %f96, %f83;
    222.           add.f32     %f98, %f85, 0f3FC90FDB;
    223.           add.f32     %f99, %f98, 0fBFC90FDB;
    224.           div.rn.f32     %f100, %f99, 0f40C90FDB;
    225.           cvt.rmi.f32.f32     %f101, %f100;
    226.           sub.f32     %f102, %f100, %f101;
    227.           fma.rn.f32     %f103, %f102, 0f40000000, 0fBF800000;
    228.           abs.f32     %f104, %f103;
    229.           mul.f32     %f105, %f104, %f104;
    230.           add.f32     %f106, %f104, %f104;
    231.           sub.f32     %f107, %f18, %f106;
    232.           mul.f32     %f108, %f105, %f107;
    233.           fma.rn.f32     %f109, %f108, 0f40000000, 0fBF800000;
    234.           add.f32     %f110, %f109, %f84;
    235.           cvt.rmi.f32.f32     %f111, %f97;
    236.           sub.f32     %f112, %f97, %f111;
    237.           cvt.rmi.f32.f32     %f113, %f110;
    238.           sub.f32     %f114, %f110, %f113;
    239.           add.f32     %f115, %f112, 0fBF000000;
    240.           add.f32     %f116, %f114, 0fBF000000;
    241.           mul.f32     %f117, %f116, %f116;
    242.           fma.rn.f32     %f118, %f115, %f115, %f117;
    243.           min.f32     %f119, %f82, %f118;
    244.           add.f64     %fd13, %fd5, 0dBFC70A3D70A3D70A;
    245.           cvt.rn.f32.f64     %f120, %fd13;
    246.           add.f64     %fd14, %fd7, 0dBFEA3D70A3D70A3D;
    247.           cvt.rn.f32.f64     %f121, %fd14;
    248.           add.f32     %f122, %f1, 0fC0400000;
    249.           add.f32     %f123, %f122, 0fBFC90FDB;
    250.           div.rn.f32     %f124, %f123, 0f40C90FDB;
    251.           cvt.rmi.f32.f32     %f125, %f124;
    252.           sub.f32     %f126, %f124, %f125;
    253.           fma.rn.f32     %f127, %f126, 0f40000000, 0fBF800000;
    254.           abs.f32     %f128, %f127;
    255.           mul.f32     %f129, %f128, %f128;
    256.           add.f32     %f130, %f128, %f128;
    257.           sub.f32     %f131, %f18, %f130;
    258.           mul.f32     %f132, %f129, %f131;
    259.           fma.rn.f32     %f133, %f132, 0f40000000, 0fBF800000;
    260.           add.f32     %f134, %f133, %f120;
    261.           add.f32     %f135, %f122, 0f3FC90FDB;
    262.           add.f32     %f136, %f135, 0fBFC90FDB;
    263.           div.rn.f32     %f137, %f136, 0f40C90FDB;
    264.           cvt.rmi.f32.f32     %f138, %f137;
    265.           sub.f32     %f139, %f137, %f138;
    266.           fma.rn.f32     %f140, %f139, 0f40000000, 0fBF800000;
    267.           abs.f32     %f141, %f140;
    268.           mul.f32     %f142, %f141, %f141;
    269.           add.f32     %f143, %f141, %f141;
    270.           sub.f32     %f144, %f18, %f143;
    271.           mul.f32     %f145, %f142, %f144;
    272.           fma.rn.f32     %f146, %f145, 0f40000000, 0fBF800000;
    273.           add.f32     %f147, %f146, %f121;
    274.           cvt.rmi.f32.f32     %f148, %f134;
    275.           sub.f32     %f149, %f134, %f148;
    276.           cvt.rmi.f32.f32     %f150, %f147;
    277.           sub.f32     %f151, %f147, %f150;
    278.           add.f32     %f152, %f149, 0fBF000000;
    279.           add.f32     %f153, %f151, 0fBF000000;
    280.           mul.f32     %f154, %f153, %f153;
    281.           fma.rn.f32     %f155, %f152, %f152, %f154;
    282.           min.f32     %f156, %f119, %f155;
    283.           mul.f32     %f157, %f5, 0f3FB50481;
    284.           mul.f32     %f158, %f7, 0f3FB50481;
    285.           cvt.f64.f32     %fd15, %f157;
    286.           add.f64     %fd16, %fd15, 0dBFDCCCCCCCCCCCCD;
    287.           cvt.rn.f32.f64     %f159, %fd16;
    288.           cvt.f64.f32     %fd17, %f158;
    289.           add.f64     %fd18, %fd17, 0dBFD3333333333333;
    290.           cvt.rn.f32.f64     %f160, %fd18;
    291.           add.f32     %f161, %f1, 0fC0800000;
    292.           add.f32     %f162, %f161, 0fBFC90FDB;
    293.           div.rn.f32     %f163, %f162, 0f40C90FDB;
    294.           cvt.rmi.f32.f32     %f164, %f163;
    295.           sub.f32     %f165, %f163, %f164;
    296.           fma.rn.f32     %f166, %f165, 0f40000000, 0fBF800000;
    297.           abs.f32     %f167, %f166;
    298.           mul.f32     %f168, %f167, %f167;
    299.           add.f32     %f169, %f167, %f167;
    300.           sub.f32     %f170, %f18, %f169;
    301.           mul.f32     %f171, %f168, %f170;
    302.           fma.rn.f32     %f172, %f171, 0f40000000, 0fBF800000;
    303.           add.f32     %f173, %f172, %f159;
    304.           add.f32     %f174, %f161, 0f3FC90FDB;
    305.           add.f32     %f175, %f174, 0fBFC90FDB;
    306.           div.rn.f32     %f176, %f175, 0f40C90FDB;
    307.           cvt.rmi.f32.f32     %f177, %f176;
    308.           sub.f32     %f178, %f176, %f177;
    309.           fma.rn.f32     %f179, %f178, 0f40000000, 0fBF800000;
    310.           abs.f32     %f180, %f179;
    311.           mul.f32     %f181, %f180, %f180;
    312.           add.f32     %f182, %f180, %f180;
    313.           sub.f32     %f183, %f18, %f182;
    314.           mul.f32     %f184, %f181, %f183;
    315.           fma.rn.f32     %f185, %f184, 0f40000000, 0fBF800000;
    316.           add.f32     %f186, %f185, %f160;
    317.           cvt.rmi.f32.f32     %f187, %f173;
    318.           sub.f32     %f188, %f173, %f187;
    319.           cvt.rmi.f32.f32     %f189, %f186;
    320.           sub.f32     %f190, %f186, %f189;
    321.           add.f32     %f191, %f188, 0fBF000000;
    322.           add.f32     %f192, %f190, 0fBF000000;
    323.           mul.f32     %f193, %f192, %f192;
    324.           fma.rn.f32     %f194, %f191, %f191, %f193;
    325.           min.f32     %f195, %f156, %f194;
    326.           add.f64     %fd19, %fd15, 0dBFA47AE147AE147B;
    327.           cvt.rn.f32.f64     %f196, %fd19;
    328.           add.f64     %fd20, %fd17, 0dBFEC28F5C28F5C29;
    329.           cvt.rn.f32.f64     %f197, %fd20;
    330.           add.f32     %f198, %f1, 0fC0A00000;
    331.           add.f32     %f199, %f198, 0fBFC90FDB;
    332.           div.rn.f32     %f200, %f199, 0f40C90FDB;
    333.           cvt.rmi.f32.f32     %f201, %f200;
    334.           sub.f32     %f202, %f200, %f201;
    335.           fma.rn.f32     %f203, %f202, 0f40000000, 0fBF800000;
    336.           abs.f32     %f204, %f203;
    337.           mul.f32     %f205, %f204, %f204;
    338.           add.f32     %f206, %f204, %f204;
    339.           sub.f32     %f207, %f18, %f206;
    340.           mul.f32     %f208, %f205, %f207;
    341.           fma.rn.f32     %f209, %f208, 0f40000000, 0fBF800000;
    342.           add.f32     %f210, %f209, %f196;
    343.           add.f32     %f211, %f198, 0f3FC90FDB;
    344.           add.f32     %f212, %f211, 0fBFC90FDB;
    345.           div.rn.f32     %f213, %f212, 0f40C90FDB;
    346.           cvt.rmi.f32.f32     %f214, %f213;
    347.           sub.f32     %f215, %f213, %f214;
    348.           fma.rn.f32     %f216, %f215, 0f40000000, 0fBF800000;
    349.           abs.f32     %f217, %f216;
    350.           mul.f32     %f218, %f217, %f217;
    351.           add.f32     %f219, %f217, %f217;
    352.           sub.f32     %f220, %f18, %f219;
    353.           mul.f32     %f221, %f218, %f220;
    354.           fma.rn.f32     %f222, %f221, 0f40000000, 0fBF800000;
    355.           add.f32     %f223, %f222, %f197;
    356.           cvt.rmi.f32.f32     %f224, %f210;
    357.           sub.f32     %f225, %f210, %f224;
    358.           cvt.rmi.f32.f32     %f226, %f223;
    359.           sub.f32     %f227, %f223, %f226;
    360.           add.f32     %f228, %f225, 0fBF000000;
    361.           add.f32     %f229, %f227, 0fBF000000;
    362.           mul.f32     %f230, %f229, %f229;
    363.           fma.rn.f32     %f231, %f228, %f228, %f230;
    364.           min.f32     %f232, %f195, %f231;
    365.           add.f64     %fd21, %fd15, 0dBFAEB851EB851EB8;
    366.           cvt.rn.f32.f64     %f233, %fd21;
    367.           add.f64     %fd22, %fd17, 0dBFE147AE147AE148;
    368.           cvt.rn.f32.f64     %f234, %fd22;
    369.           add.f32     %f235, %f1, 0fC0C00000;
    370.           add.f32     %f236, %f235, 0fBFC90FDB;
    371.           div.rn.f32     %f237, %f236, 0f40C90FDB;
    372.           cvt.rmi.f32.f32     %f238, %f237;
    373.           sub.f32     %f239, %f237, %f238;
    374.           fma.rn.f32     %f240, %f239, 0f40000000, 0fBF800000;
    375.           abs.f32     %f241, %f240;
    376.           mul.f32     %f242, %f241, %f241;
    377.           add.f32     %f243, %f241, %f241;
    378.           sub.f32     %f244, %f18, %f243;
    379.           mul.f32     %f245, %f242, %f244;
    380.           fma.rn.f32     %f246, %f245, 0f40000000, 0fBF800000;
    381.           add.f32     %f247, %f246, %f233;
    382.           add.f32     %f248, %f235, 0f3FC90FDB;
    383.           add.f32     %f249, %f248, 0fBFC90FDB;
    384.           div.rn.f32     %f250, %f249, 0f40C90FDB;
    385.           cvt.rmi.f32.f32     %f251, %f250;
    386.           sub.f32     %f252, %f250, %f251;
    387.           fma.rn.f32     %f253, %f252, 0f40000000, 0fBF800000;
    388.           abs.f32     %f254, %f253;
    389.           mul.f32     %f255, %f254, %f254;
    390.           add.f32     %f256, %f254, %f254;
    391.           sub.f32     %f257, %f18, %f256;
    392.           mul.f32     %f258, %f255, %f257;
    393.           fma.rn.f32     %f259, %f258, 0f40000000, 0fBF800000;
    394.           add.f32     %f260, %f259, %f234;
    395.           cvt.rmi.f32.f32     %f261, %f247;
    396.           sub.f32     %f262, %f247, %f261;
    397.           cvt.rmi.f32.f32     %f263, %f260;
    398.           sub.f32     %f264, %f260, %f263;
    399.           add.f32     %f265, %f262, 0fBF000000;
    400.           add.f32     %f266, %f264, 0fBF000000;
    401.           mul.f32     %f267, %f266, %f266;
    402.           fma.rn.f32     %f268, %f265, %f265, %f267;
    403.           min.f32     %f269, %f232, %f268;
    404.           add.f64     %fd23, %fd15, 0dBFE47AE147AE147B;
    405.           cvt.rn.f32.f64     %f270, %fd23;
    406.           add.f64     %fd24, %fd17, 0dBFBEB851EB851EB8;
    407.           cvt.rn.f32.f64     %f271, %fd24;
    408.           add.f32     %f272, %f1, 0fC0E00000;
    409.           add.f32     %f273, %f272, 0fBFC90FDB;
    410.           div.rn.f32     %f274, %f273, 0f40C90FDB;
    411.           cvt.rmi.f32.f32     %f275, %f274;
    412.           sub.f32     %f276, %f274, %f275;
    413.           fma.rn.f32     %f277, %f276, 0f40000000, 0fBF800000;
    414.           abs.f32     %f278, %f277;
    415.           mul.f32     %f279, %f278, %f278;
    416.           add.f32     %f280, %f278, %f278;
    417.           sub.f32     %f281, %f18, %f280;
    418.           mul.f32     %f282, %f279, %f281;
    419.           fma.rn.f32     %f283, %f282, 0f40000000, 0fBF800000;
    420.           add.f32     %f284, %f283, %f270;
    421.           add.f32     %f285, %f272, 0f3FC90FDB;
    422.           add.f32     %f286, %f285, 0fBFC90FDB;
    423.           div.rn.f32     %f287, %f286, 0f40C90FDB;
    424.           cvt.rmi.f32.f32     %f288, %f287;
    425.           sub.f32     %f289, %f287, %f288;
    426.           fma.rn.f32     %f290, %f289, 0f40000000, 0fBF800000;
    427.           abs.f32     %f291, %f290;
    428.           mul.f32     %f292, %f291, %f291;
    429.           add.f32     %f293, %f291, %f291;
    430.           sub.f32     %f294, %f18, %f293;
    431.           mul.f32     %f295, %f292, %f294;
    432.           fma.rn.f32     %f296, %f295, 0f40000000, 0fBF800000;
    433.           add.f32     %f297, %f296, %f271;
    434.           cvt.rmi.f32.f32     %f298, %f284;
    435.           sub.f32     %f299, %f284, %f298;
    436.           cvt.rmi.f32.f32     %f300, %f297;
    437.           sub.f32     %f301, %f297, %f300;
    438.           add.f32     %f302, %f299, 0fBF000000;
    439.           add.f32     %f303, %f301, 0fBF000000;
    440.           mul.f32     %f304, %f303, %f303;
    441.           fma.rn.f32     %f305, %f302, %f302, %f304;
    442.           min.f32     %f306, %f269, %f305;
    443.           mul.f32     %f307, %f306, 0f40400000;
    444.           sqrt.rn.f32     %f308, %f307;
    445.           mov.f32     %f309, 0f3F800000;
    446.           sub.f32     %f310, %f309, %f308;
    447.           mov.f32     %f311, 0f00000000;
    448.           cvt.rzi.u32.f32     %r11, %f311;
    449.           mul.f32     %f312, %f310, 0f437F0000;
    450.           cvt.rzi.u32.f32     %r12, %f312;
    451.           mul.wide.u32     %rd3, %r10, 4;
    452.           add.s64     %rd4, %rd2, %rd3;
    453.           cvt.u16.u32     %rs1, %r12;
    454.           cvt.u16.u32     %rs2, %r11;
    455.           mov.u16     %rs3, 255;
    456.           st.global.v4.u8     [%rd4], {%rs2, %rs2, %rs1, %rs3};
    457.           ret;
    458.       }
    459.   ";
    460. }
    461.  
    upload_2023-2-12_21-27-52.png

    Of course, programming with assembly is very difficult. It is recommended to use NVCC compiler to generate PTX, example:

    Code (CSharp):
    1. /*
    2. Minimal example:
    3.  
    4. nvcc -ptx test.cu
    5.  
    6. __global__ void mainImage(uchar4 *fragColor, float iTime)
    7. {
    8.     int width = 1024;
    9.     int height = 1024;
    10.     unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
    11.     unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
    12.     unsigned int i = x + width * y;
    13.     float2 iResolution = make_float2((float)width, (float)height);
    14.     float2 fragCoord = make_float2((float)x, (float)y);
    15.     float2 uv = make_float2(fragCoord.x / iResolution.x, fragCoord.y / iResolution.y);
    16.     float4 color = make_float4(uv.x, uv.y, 0.0, 1.0);
    17.     fragColor[i] = make_uchar4(color.x * 255, color.y * 255, color.z * 255, 255);
    18. }
    19. */
     
    Last edited: May 17, 2023
  12. mongoose666

    mongoose666

    Joined:
    Feb 6, 2021
    Posts:
    3
    That's a good step in the right direction @Przemyslaw_Zaworski I will be testing this solution myself ;)
     
  13. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,204
    Last edited: May 11, 2023
  14. mongoose666

    mongoose666

    Joined:
    Feb 6, 2021
    Posts:
    3
    Indeed :) - Some restructuring and expansion and you can have a complete system for using CUDA from within Unity.

    And yes, before anyone comments, limited to Linux & Windows platforms of course ... but for render & compute farms at home it's not something you want to deploy to every platform anyway. You can use, of course, nVidia Omniverse but that doesn't tie into Cinemachine or any of the other great assets in Unity :rolleyes::D

    I originally came here to find a solution as my stable-diffusion stopped working and throwing errors. Everyone kept saying it's most likely a faulty GPU but it's brand new (only an RTX3060 as I won't use my RTX 4080) so I wanted CUDA to exercise the CUDA cores and prove/disprove the faulty GPU scenario. Then I started thinking about alternatives to compute shaders in highly specific to nVidia use cases within the Unity engine ;) and voila landed here :)
     
  15. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,204
    CUDA cores aren't a real thing. CUDA is just a software layer over the actual hardware. Compute and CUDA do the same exact thing but one of them is proprietary and one of them is not. Regardless of that discussion though if you want to test the hardware the best way is with a program designed to stress it like FurMark.

    https://geeks3d.com/furmark/
     
    spiney199 likes this.
  16. mongoose666

    mongoose666

    Joined:
    Feb 6, 2021
    Posts:
    3
    Totally understand that but I wasn't going to say tensor cores as not all GPUs are tensor core :) - Using CUDA cores just encompasses them all ;)

    Cheers for the link, will check it out.
     
  17. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Ryiah and mgear like this.
  18. lufydad

    lufydad

    Joined:
    Feb 1, 2018
    Posts:
    1
    As an alternative to the excellent idea of @Przemyslaw_Zaworski. I made a native plugin (available here: https://github.com/davidAlgis/InteropUnityCUDA) that makes possible to edit graphics objects created in Unity (texture or compute buffer) directly with CUDA kernels. It's a bit tricky to set up, but it might be more comfortable for a big project.

    The native plugin only support OpenGL and DX11 graphics API for now, but it will be updated as it's used in a huge project with my company.

    I don't know if it can be of any help with NVIDIA's Optix denoising, but for the future reader who is only looking for graphics interoperability between Unity and CUDA, it might be a good start point.
     
    Przemyslaw_Zaworski, Ryiah and mgear like this.