#Unity.Spline & ECS & Jobs -> How to ?

1 messages · Page 1 of 1 (latest)

opal urchin
#

I'm currently working on a project with ECS.
Once we do create our Splines we can then convert them to NativeSpline for usage in a Parralel Job.

Unfortunately we can't directly same the NativeSpline in a component & then query it from the Job because of the native arrays in them. It's giving us this error :
NativeSplineComponentData_RW_ComponentTypeHandle can not be accessed. Nested native containers are illegal in jobs.

My question is double :
Is there any streamlined way of having the data saved on an entity for later maybe serialization for a save file or something & for later usage in parralel Jobs that query a component that have the data necessary for it?

And If not, I need to implement that so what would be the correct way of doing it ?

true prism
#

Are the splines you're creating ReadOnly? or do you modify them frequently at runtime?

opal urchin
#

Nope readonly : not possible to modify them after creation & setup the data for saving only

#

other than destroying the entity later

true prism
#

In my game, I have effectively a library of Curves I bake into a BlobAsset, then have an int stored on each entity to direct which Curve they're currently utilizing. That way only an int is stored per entity and it maps to the Curve data I'm looking for. That said all my Curves are ReadOnly though

opal urchin
#

the nativeSpline is used later only to move items along the spline

true prism
#

Oh I misunderstood at first read. You might be able to get away with creating BlobAsset's at runtime for something like that, it's not something I've tried but if it's OnCreate and OnDestroy of entities (presumably not every frame), it might work in a performant way. BlobAssets are the only way nest NativeContainers for usage in a job but it requires them to be ReadOnly

#

You'd just apply the BlobAsset reference to the entity you made, once the BlobAsset is created

opal urchin
#

Alright So if i understand well

When I transform the Spline from the Splinepackage into the NativeSpline equivalent.
Basically create a component that have a BlobRef

THen when allocating the NativeSpline create the blob on that , then assign the ref to the component & add it to the entity ?

#

hummmm

#

lemme try real quick

#

Lowl

#

EA0003: You may not build the type UnityEngine.Splines.NativeSpline, with BlobBuilder.ConstructRoot, as UnityEngine.Splines.NativeSpline.m_Knots.m_Buffer is a pointer. Only non-reference types are allowed in Blobs.

#
BlobBuilder builder = new BlobBuilder(Allocator.Temp);
ref NativeSpline splineData = ref builder.ConstructRoot<NativeSpline>();
BlobAssetReference<NativeSpline> result = builder.CreateBlobAssetReference<NativeSpline>(Allocator.Persistent);

SplineBlobComponent comp = new SplineBlobComponent()
{
    SplineBlob = result
};
            
builder.Dispose();

_.SystemAPI.AddComponentData(entity, comp);

Just doing this

true prism
#

builder.ConstructRoot<NativeSpline>();

Wont' take reference types, but you should be able to create your own struct like

public struct MySpline {
  public NativeSpline Value;
}

And it should compile without yelling, regardless of nested NativeContainers

#

Then you'll need mySpline.Value = builder.CreateBlobAssetReference<LightStamps>(Allocator.Persistent); To allocate the data storage for the NativeSpline

opal urchin
#

Apparently that disagree with you ^^

#
 public struct NativeSplineTestComponentData
    {
        public NativeSpline Spline;

    }
true prism
#

Ooooh yeah, you're right, there's `BlobArray<T>' in my code

#

Special kinds of containers for blobs, ick

opal urchin
#

HuuuuuuuH

true prism
#

Blobs are frustrating to work with imo, but yeah anyways. I think BlobAssets just support BlobArray<T> at the moment. So to go this route you would need to build your own NativeSpline container in order to convert a NativeSpline into a BlobAsset compatible format. Every NativeArray within the NativeSpline would need to be a BlobArray. Such as the NativeArray<BezierKnot> would need to be a BlobArray<BezierKnot, effectively just copying data over. You end up with a ton of code to do something pretty simple... but that's been my experience with ECS in general pretty much :/

If you're doing a more Hybrid approach in your project with ECS in general, another option would be to store the NativeSpline into a class : IComponentData (surprising to some, but this can be done) but then you sacrifice some performance by using a managed ComponentData. I don't have any class based IComponentData's in my project, so other people in Turbo's discord might have more insight on that than me.

Another option, but far more restrictive (almost to the point where maybe I shouldn't mention it, but it depends on how you're utilizing the data), for usage is to store the NativeSpline(s) outside of your ECS world. Like on a Managed Class singleton or something. Then whenever you schedule a new job pass in the spline you need for the job, but then you're limited on passing in just one Spline into a job at a time and if you need to do this across multiple entities, which I presume you do, it won't work very well at all. The job scheduling will slow down your game.

opal urchin
#

Is there any debug windows for blobassets ?

#

a way to see if what i'm doing work ?

true prism
#

Nothing unique to blobAssets but I've personally used Breakpoints in my IDE to verify the data being generated at various stages of the blobs construction. And if you get to the point where the BlobAssetReference is applied to an Entity then all of the Entity Debugging tools should display the data as well (such as the Entity Inspector)

opal urchin
#

thanks

#

experimenting with that

#

I'll let you know 😉

#

just so I know : a blobarrray is basically a native rray for blob asset , right ?

true prism
#

Best of luck 🫡

#

Yeah, but it's got a unique way of instantiation and ensures it's always ReadOnly

#

Like you use a BlobBuilder<T> for the highest level struct, then for BlobArray within the high level struct you need to use BlobBuilderArray<T>

#

All call for a size and allocation as you go down the struct hierarchy

opal urchin
#

perfect

true prism
#

And then it can be applied directly to entities as a BlobAssetReference

opal urchin
#

Trying to say : fuck it and rewrite a custom blob asset version of the NativeSpline

#

to see how it goes

true prism
#

I assume that's the most performant way to go, but to caveat I'm not 100% sure.

I am confident in that it'll be the fastest/most performant way to set it all up if you're trying to access/utilize that data in IJobEntity job in OnUpdate, which it sounds like you're doing a conveyer belt like thing, so I assume you would be. It's just the OnCreate and OnDestroy of entities building this blob might be a bit of a hitch, although I can't imagine it'd be more than 1ms, depends on how large your spline is I guess. You'll have to test it out

opal urchin
#

Meh destroying belts should not be that bad

true prism
#

True, i guess it's just oncreate

opal urchin
#

Even that ^^ it's just a few arrays
It should not be that bad

#

I'll test later to see

First let's make it work xD

#

Because it's going to be for some sort of conveyor it NEED to be performant as hell

true prism
#

I think there was a Factorio dev blog on this at some point if you haven't looked into that. Unrelated to BlobAssets/ECS

#

Let me see if I can find it, if you don't know what I'm talking about

#

I think it was something along the lines of following the object in front of it, instead of referencing the belt, if I recall right

#

Two blog posts about their belts and optimization ^

#

Not sure if relevant, since if you're using splines i'm assuming a 3D world for you, but maybe worth your read regardless

opal urchin
#

Read them but yeah it's not really applicable

opal urchin
#

Rahaaaaaaaaaaa

#

i'm losing my mind

#

why is it so annoying to use ??

opal urchin
#

Unfortunately that don't work either in my case because of a boxing error in burst

opal urchin
#

Alright , made it work

#

For ref, to get the most out of it I had to store shit in blobasset, had to rewrite most of the evaluaton code in the spline package.
To make sure everything was bursting : unsafe code + pointers

#

My god what a ride !

faint patrol
#

Hello Touhma! I'm having the same issue 😦
Could you help me out?
Do I really need to rewrite a bunch of stuff inside the splines package?

sharp sapphire
faint patrol
#

Thanks! That really helps