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

possible to set halfs to ComputeBuffer

Discussion in 'Scripting' started by pvloon, Nov 27, 2012.

  1. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    591
    Hello,

    For performance reasons I want my StructuredBuffer to contain halfs, not floats. However, I'm streaming data from the CPU to the buffer. Streaming a float array is easy, but how would you stream an array of halfs? C# does not have native halfs. Just streaming floats and setting the buffer to use halfs works on ATI 5770, but I don't think it works on nvidia cards. Thanks for the help!

    Arthur
     
  2. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    591
    So... Hmm, I geuss Aras and other shader gurus are in the shaderlab Section.
     
  3. spalmer

    spalmer

    Joined:
    Jun 3, 2014
    Posts:
    6
    It's totally possible, just shadow the half data using short or ushort on the C# side and make your own converter function float -> ushort which converts to half precision bit layout. It's easy: http://en.wikipedia.org/wiki/Half_precision_floating-point_format I made a little struct which wraps the thing and looks a lot like a native type, I'm sure you'd love the code for that. But I'm leaving it as an exercise for the reader, because it's pretty trivial.
     
    nan_nz and Chris-Hoo like this.
  4. spalmer

    spalmer

    Joined:
    Jun 3, 2014
    Posts:
    6
    Chris-Hoo likes this.