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
TechPizzaOP•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;
unto
unto•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
TechPizzaOP•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
unto
unto•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
unto
unto•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
TechPizzaOP•3y ago
wait what? how would attributes help here FieldOffset is an attribute btw
unto
unto•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
TechPizzaOP•3y ago
that didn't really say much
unto
unto•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
TechPizzaOP•3y ago
yes such an attribute would work
Supine
Supine•3y ago
But you gotta implement it by yourself
unto
unto•3y ago
I know right but the offset problem remains
TechPizza
TechPizzaOP•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
TechPizzaOP•3y ago
getting the attributes once at startup should be trivial enough
unto
unto•3y ago
how though? could you provide an example or whatever I really am not that deep into reflection
TechPizza
TechPizzaOP•3y ago
you'd just use the data you have in the attribute
unto
unto•3y ago
the offset still needs to be a thing
TechPizza
TechPizzaOP•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?
unto
unto•3y ago
because of glVertexAttribFormat it still requires a relative offset (that's a true modern gl function)
TechPizza
TechPizzaOP•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
unto
unto•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
TechPizzaOP•3y ago
i can already tell you that i could successfully use DSA without glVertexAttribFormat
unto
unto•3y ago
you have to though it's virtually impossible to define attribute format in DSA without VertexAttribFormat
TechPizza
TechPizzaOP•3y ago
🤨 so can you tell me how those attributes are insufficent for a relative offset?
unto
unto•3y ago
relative offset is in bytes
TechPizza
TechPizzaOP•3y ago
yes... and?
unto
unto•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
TechPizzaOP•3y ago
get the size of the field type?
unto
unto•3y ago
then what about alignment what if a structure has different alignment?
TechPizza
TechPizzaOP•3y ago
how would it have a different alignment
Supine
Supine•3y ago
I use dsa
unto
unto•3y ago
[StructLayout(Pack = X)] I see the man of culture
Supine
Supine•3y ago
It shouldn't Disable it then
unto
unto•3y ago
😳 fair enough
Supine
Supine•3y ago
Pog
TechPizza
TechPizzaOP•3y ago
you can add a nullable offset in VertexAttribute for special cases
unto
unto•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)
unto
unto•3y ago
how is it broken though
TechPizza
TechPizzaOP•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)
unto
unto•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
unto
unto•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
TechPizzaOP•3y ago
reflection won't help ya
unto
unto•3y ago
can I get an address of a field using reflection?
TechPizza
TechPizzaOP•3y ago
nope
unto
unto•3y ago
well that just sucks, can I get an adress of a field using attributes?
TechPizza
TechPizzaOP•3y ago
nope
unto
unto•3y ago
sucks even more
TechPizza
TechPizzaOP•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
unto
unto•3y ago
what about FieldDefinition.GetOffset
TechPizza
TechPizzaOP•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
TechPizzaOP•3y ago
????? math doesn't magically find you the terms though
Supine
Supine•3y ago
It's literally linear equation
TechPizza
TechPizzaOP•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 💀
unto
unto•3y ago
okay but what if a field is, say byte sized is it aligned?
TechPizza
TechPizzaOP•3y ago
structs are layed out sequentially by default
unto
unto•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
unto
unto•3y ago
done and done
No description
Want results from more Discord servers?
Add your server