DynamicBuffer Custom Struct input

Hello,
Wondering how to create a custom Struct as input to DynamicBuffer [Stride.Buffer] (setting IsStructuredBuffer as True).

Goal is to then feed this Buffer to a DrawShader (custom .sdsl shader).

Tryed with Record but does not seem to be recognized by DynamicBuffer, “Unable to resolve adaptive node SetGraphicsData”.

Seems like the solution would be to create the struct with raw C# ? Or is there a non-C# solution?

Already prepared the structure on the .hlsl side

shader Shatter_FL
{
    struct Shatter
    {
        float3 Pos;
        float3 Dir;
        float Str;
        int LineID;
    };
};

Thanks!

Yes, you’ll need to define the struct in C# - VL does not have that option.

Followed the beginning of Writing Nodes with C# - vvvvTv S02 E05

Then Written this code in Utils.cs:

using System.Runtime.InteropServices;
using Stride.Core.Mathematics; // provide Vector3

namespace Main;

// /// <summary>
// /// Mirrors the HLSL Shatter struct from shaders/Shatter_FL.sdsl
// /// Layout: sequential, Pack=4, total 32 bytes.
// /// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Shatter
{
    public Vector3 Pos;
    public Vector3 Dir;
    public float Str;
    public int LineID;

    public Shatter(Vector3 pos, Vector3 dir, float str, int lineID)
    {
        Pos    = pos;
        Dir    = dir;
        Str    = str;
        LineID = lineID;
    }
}

Then used the Struct constructor inside the loop, and everything seem to be working :

•ᴗ•

3 Likes

btw, is this efficient?
Do I understand correctly that, when working with immutable objects, it wouldn’t involve recreating every frame in such a setup? @Elias

I used a cache in the final setup

But this mean I still recreate the struct every time the cache is triggered
Maybe calling create once and then using an Update method to change the attributes without recreating a new struct would be better ?