Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Chunk of misc data

Discussion in 'Entity Component System' started by Trindenberg, Oct 5, 2022.

  1. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    378
    Hi,

    Trying to implement something to use in a pipeline where inputs could be math types (mainly float-float4 in this test). Just your thoughts on what I could do here better? Basically I want a struct where [0] might refer to a float2, [1] might be a float3, using a 128 byte buffer. Also no idea when used in a separate op job, how burst will like it. No idea if this would be better using a NativeArray. I'm no expert, but just looking to expand my knowledge from experts :)


    Code (CSharp):
    1.     public struct Floats
    2.     {
    3.         fixed byte store[128];
    4.         fixed int offset[32];
    5.         int remaining;
    6.         int pos;
    7.         int current;
    8.  
    9.         public void Init()
    10.         {
    11.             remaining = 128;
    12.             pos = 0;
    13.             current = 0;
    14.         }
    15.  
    16.         public void Set<T>(T someValue) where T : unmanaged
    17.         {
    18.             var size = sizeof(T);
    19.             if (size < remaining)
    20.             {
    21.                 fixed(void* p = &store[current]) *(T*)p = someValue;
    22.                 current += size;
    23.                 offset[pos + 1] = offset[pos++] + size;
    24.                 remaining -= size;
    25.             }
    26.         }
    27.  
    28.         public T Get<T>(int i) where T : unmanaged
    29.         {
    30.             fixed (void* f = &store[offset[i]]) return *(T*)f;
    31.         }
    32.     }
     
  2. CookieStealer2

    CookieStealer2

    Joined:
    Jun 25, 2018
    Posts:
    119
    Why not use NativeStream or UnsafeStream? You can append arbitrary types to it and then read them. Seems similar to what you want.
     
  3. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    378
    @CookieStealer2 Yes I do want to convert over to the Native libraries if I get what I want working. That does look like it might be useful if it's indexes each piece of data of misc length, to work with another index of commands. At this point it is more about the model.