offsetof magic
you could in theory use a local and subtract field offset with offset of the local
65 Replies
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?
because Marshal is for marshalling and isn't as reliable
better?at least this one compiles
🤔
that's still weird how C# lacks this basic functionality
This is Paradox but in reality it's caused by runtime doing marshalling and breaking stuff
so which one is the most reliable for something like that
also having attributes for that kind of thing would be much better
wait what? how would attributes help here
FieldOffset is an attribute btw
using attributes to avoid touching GL attribute setups more than once
still it doesn't solve the offset problem that's coorect
that didn't really say much
also it seems marshal.offsetof goes well with unsafe structures
yes such an attribute would work
But you gotta implement it by yourself
I know right
but the offset problem remains
having an attribute like would eliminate offset problems though?
But i think it will cause lower performance because of reflection stuff
getting the attributes once at startup should be trivial enough
how though? could you provide an example or whatever
I really am not that deep into reflection
you'd just use the data you have in the attribute
the offset still needs to be a thing
why?
because of glVertexAttribFormat
it still requires a relative offset
(that's a true modern gl function)
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, coolthere'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
i can already tell you that i could successfully use DSA without glVertexAttribFormat
you have to though
it's virtually impossible to define attribute format in DSA without VertexAttribFormat
🤨
so can you tell me how those attributes are insufficent for a relative offset?
relative offset is in bytes
yes... and?
I still need to have a byte offset
and I literally don't know how could it provide me with this functionality
get the size of the field type?
then what about alignment
what if a structure has different alignment?
how would it have a different alignment
I use dsa
[StructLayout(Pack = X)]
I see the man of cultureIt shouldn't
Disable it then
😳
fair enough
Pog
you can add a nullable offset in VertexAttribute for special cases
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
Remember: do not use
gl.BlitNamedFramebuffer
. it's broken. (Depends on drivers)how is it broken though
how else would one do it other than manually defining offsets
It doesn't work.
Works only if destination is window's backbuffer (0)
ah
so a hack program (jk lmao a pipeline) that is
okay so pointer fuckery can do the thing right?
Yeah
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
reflection won't help ya
can I get an address of a field using reflection?
nope
well that just sucks, can I get an adress of a field using attributes?
nope
sucks even more
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 notwhat about
FieldDefinition.GetOffset
hmm could work? literally never seen it before
and
Returns the field layout offset, or -1 if it is not available.
is not reassuringHere's how struct lays in memory:
Using math, it's simple to get offset
?????
math doesn't magically find you the terms though
It's literally linear equation
ok?
have fun getting the fields in the correct order
reflection can give you fields in whatever order
Use source generators 💀
okay but what if a field is, say byte sized
is it aligned?
structs are layed out sequentially by default
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
done and done