S
Silk.NET•3y ago
TechPizza

offsetof magic

you could in theory use a local and subtract field offset with offset of the local
65 Replies
TechPizza
TechPizza•3y ago
Unsafe.SkipInit(out Vertex tmp);
Vertex* p = &tmp;
int offsetToField1 = &p->field1 - p;
int offsetToField2 = &p->field2 - p;
Unsafe.SkipInit(out Vertex tmp);
Vertex* p = &tmp;
int offsetToField1 = &p->field1 - p;
int offsetToField2 = &p->field2 - p;
und
und•3y ago
that looks weird I also can do an enormously stupid thing and use C++/CLI for native things like that but that sucks in the first place why isn't marshal.offsetof good enough for that though?
TechPizza
TechPizza•3y ago
because Marshal is for marshalling and isn't as reliable better?
Vertex tmp;
Vertex* p = &tmp;
long offsetToField1 = (byte*)&p->field1 - (byte*)p;
long offsetToField2 = (byte*)&p->field2 - (byte*)p;
Vertex tmp;
Vertex* p = &tmp;
long offsetToField1 = (byte*)&p->field1 - (byte*)p;
long offsetToField2 = (byte*)&p->field2 - (byte*)p;
at least this one compiles
und
und•3y ago
🤔 that's still weird how C# lacks this basic functionality
Supine
Supine•3y ago
This is Paradox but in reality it's caused by runtime doing marshalling and breaking stuff
und
und•3y ago
so which one is the most reliable for something like that also having attributes for that kind of thing would be much better
TechPizza
TechPizza•3y ago
wait what? how would attributes help here FieldOffset is an attribute btw
und
und•3y ago
using attributes to avoid touching GL attribute setups more than once still it doesn't solve the offset problem that's coorect
TechPizza
TechPizza•3y ago
that didn't really say much
und
und•3y ago
[VertexAttribute(0, VertexAttribPointerType.Float, 3)]
public readonly Vector3D<float> Position;
[VertexAttribute(0, VertexAttribPointerType.Float, 3)]
public readonly Vector3D<float> Position;
also it seems marshal.offsetof goes well with unsafe structures
TechPizza
TechPizza•3y ago
yes such an attribute would work
Supine
Supine•3y ago
But you gotta implement it by yourself
und
und•3y ago
I know right but the offset problem remains
TechPizza
TechPizza•3y ago
having an attribute like would eliminate offset problems though?
Supine
Supine•3y ago
But i think it will cause lower performance because of reflection stuff
TechPizza
TechPizza•3y ago
getting the attributes once at startup should be trivial enough
und
und•3y ago
how though? could you provide an example or whatever I really am not that deep into reflection
TechPizza
TechPizza•3y ago
you'd just use the data you have in the attribute
und
und•3y ago
the offset still needs to be a thing
TechPizza
TechPizza•3y ago
[VertexAttribute(index: 0, type: VertexAttribPointerType.Float3)] public Vector3 Pos;
[VertexAttribute(index: 1, type: VertexAttribPointerType.Float4)] public Vector4 Color;
[VertexAttribute(index: 0, type: VertexAttribPointerType.Float3)] public Vector3 Pos;
[VertexAttribute(index: 1, type: VertexAttribPointerType.Float4)] public Vector4 Color;
why?
und
und•3y ago
because of glVertexAttribFormat it still requires a relative offset (that's a true modern gl function)
TechPizza
TechPizza•3y ago
those attributes provide all offsets you'd need though wait why does glVertexAttribFormat even exist oh it's just to make amends of old shit, cool
und
und•3y ago
there's this cool thing in 4.5 core called direct state access after 4.5 you are able to fill up buffers, textures and VAOs without binding them ie access by handle not by binding
TechPizza
TechPizza•3y ago
i can already tell you that i could successfully use DSA without glVertexAttribFormat
und
und•3y ago
you have to though it's virtually impossible to define attribute format in DSA without VertexAttribFormat
TechPizza
TechPizza•3y ago
🤨 so can you tell me how those attributes are insufficent for a relative offset?
und
und•3y ago
relative offset is in bytes
TechPizza
TechPizza•3y ago
yes... and?
und
und•3y ago
I still need to have a byte offset and I literally don't know how could it provide me with this functionality
TechPizza
TechPizza•3y ago
get the size of the field type?
und
und•3y ago
then what about alignment what if a structure has different alignment?
TechPizza
TechPizza•3y ago
how would it have a different alignment
Supine
Supine•3y ago
I use dsa
und
und•3y ago
[StructLayout(Pack = X)] I see the man of culture
Supine
Supine•3y ago
It shouldn't Disable it then
und
und•3y ago
😳 fair enough
Supine
Supine•3y ago
Pog
TechPizza
TechPizza•3y ago
you can add a nullable offset in VertexAttribute for special cases
und
und•3y ago
but anyways I want to have a convenient way to retreive the byte offset of a certain field just adding sizes together doesn't seem like a good way to go
Supine
Supine•3y ago
Remember: do not use gl.BlitNamedFramebuffer. it's broken. (Depends on drivers)
und
und•3y ago
how is it broken though
TechPizza
TechPizza•3y ago
how else would one do it other than manually defining offsets
Supine
Supine•3y ago
It doesn't work. Works only if destination is window's backbuffer (0)
und
und•3y ago
ah so a hack program (jk lmao a pipeline) that is okay so pointer fuckery can do the thing right?
Supine
Supine•3y ago
Unsafe.SkipInit(out Structure s);
nuint offsetOfField1 = &s.field1 - &s;
Unsafe.SkipInit(out Structure s);
nuint offsetOfField1 = &s.field1 - &s;
Yeah
und
und•3y ago
allow me to describe it in the way it's done in C's offsetof and lets figure out how to do it using reflection so we have a null pointer to a structure we take the address of its field and that's it, we have the offset
TechPizza
TechPizza•3y ago
reflection won't help ya
und
und•3y ago
can I get an address of a field using reflection?
TechPizza
TechPizza•3y ago
nope
und
und•3y ago
well that just sucks, can I get an adress of a field using attributes?
TechPizza
TechPizza•3y ago
nope
und
und•3y ago
sucks even more
TechPizza
TechPizza•3y ago
that's why i recommended this you could even make the type: nullable and it would default based on the field type you'd just add whether you want the attribute normalized or not
und
und•3y ago
what about FieldDefinition.GetOffset
TechPizza
TechPizza•3y ago
hmm could work? literally never seen it before and Returns the field layout offset, or -1 if it is not available. is not reassuring
Supine
Supine•3y ago
Here's how struct lays in memory:
No description
Supine
Supine•3y ago
Using math, it's simple to get offset
TechPizza
TechPizza•3y ago
????? math doesn't magically find you the terms though
Supine
Supine•3y ago
It's literally linear equation
TechPizza
TechPizza•3y ago
ok? have fun getting the fields in the correct order reflection can give you fields in whatever order
Supine
Supine•3y ago
Use source generators 💀
und
und•3y ago
okay but what if a field is, say byte sized is it aligned?
TechPizza
TechPizza•3y ago
structs are layed out sequentially by default
und
und•3y ago
so basically "packed" well I still can use implicit offsets and retreive those by reflection which shouldn't be that bad if you think about it
und
und•3y ago
done and done
No description
Want results from more Discord servers?
Add your server