Search Unity

Blittable bitmask

Discussion in 'Entity Component System' started by PublicEnumE, Nov 21, 2019.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Yesterday, I wrote some some new structs, so that I could store blittable bitmasks of any size inside an ECS Component (BitMask128, BitMask256, etc). These types also have built in enumerators, to allow for easy looping over all the set bits.

    These seem useful, but I'm wondering if other people have approached this problem differently.

    Are people using bitmasks in their ECS work? And have you tackled a similar problem in your own code?
     
  2. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
  3. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
  4. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Returning to this question after so many months.

    @fholm & @recursive: I really appreciate both of your answers. I learned a lot from digging into them.

    But to make sure I understand: Neither of these BitSet solutions would have both of these features, correct?

    1. Blittable
    2. Unlimited bit length

    Just wanted to double check before assuming I read them correctly. @fholm, your UnsafeBitSet uses a pointer (
    ulong* _bits;
    ). And @recursive, your solution doesn't, but the masks are limited to the size of the type being used. If I have those things wrong, please chime in and correct me!

    For context, I'm looking for a bitmask solution which can be stored as a field in an IComponentData. Right now, I'm just using simple structs with multiple ints, to increase the max bit size by multiples of 32. I've got some simple logic in the bitwise operators to account for the different sizes.

    That works alright, but it would be ideal if there were some way to do that @fholm's UnsafeBitSet is doing: allocating a bit group of any size, and just pointing back to it from the bitset struct. But I can't think of a good way to do that, *and* store it in an ECS component. Do either of you know more, based on your experience?

    Thanks for any advice, really!
     
  5. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    Actually, that ideal scenario I described might be possible using an IntPtr...

    ...But that IntPtr wouldn't point to anything valid if it were serialized/deserialized later as part of a saved game.

    I'm still learning about Unity's recommended approach to serializing entities, but I imagine it would require all components to have a fixed size. So that would rule out serialization of bitmasks of variables sizes (different bitmask lengths per each instance of the component).

    So on second thought, maybe what I describes wouldn't be possible. At least not if you wanted to save that data as part of saved game files.
     
    Last edited: Mar 19, 2020
  6. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    it's too late to answer this question, but ill do it for peopole facing this problem.
    if blittable and unlimitted bitmask size is what u are looking for, you can use a BlobBuilder to create a BlobArray<byte> and point to it as a byte* _bits to do where ever bitmask operation you want.
    but if blittable and Network compatibility is what u are looking for, then using a limited size like a struct with n ulong fields will do the job.
    afaik it's not possible to send BlobAsset data over the network.
     
  7. nijnstein

    nijnstein

    Joined:
    Feb 6, 2021
    Posts:
    78
    It seems to me to be impossible to have both, you cannot know size in advance if its unlimited.

    As per definition of blittable: "A blittable type is a data type that does not require special attention from the interop marshaler because by default it has a common representation in managed and unmanaged memory." (wiki)

    This is not possible it its length is not fixed and thus limited

    (one dimensional arrays of blittable types are considered blittable but their size is known in advance so..)
     
    Last edited: Dec 19, 2021