Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field offsetof #79

Open
W4RH4WK opened this issue Feb 26, 2023 · 4 comments
Open

Field offsetof #79

W4RH4WK opened this issue Feb 26, 2023 · 4 comments

Comments

@W4RH4WK
Copy link

W4RH4WK commented Feb 26, 2023

Apparently getting the offset of a field within a struct from a pointer-to-member is not trivial (see).

It would be very helpful to get a field's offset from its respective field_descriptor.

@veselink1
Copy link
Owner

That would only work on standard-layout types, no?

I think it is a good idea to provide it somehow, just clarifying the restrictions the standard imposes. Non-std::is_standard_layout types would not have field offsets.

@W4RH4WK
Copy link
Author

W4RH4WK commented Feb 27, 2023

Yes, you are right. offsetof is only defined for standard layout types. I forgot about this restriction.

@veselink1
Copy link
Owner

Would that restriction prevent you from using it? It might be nice to know what the use-case is :)

@W4RH4WK
Copy link
Author

W4RH4WK commented Feb 28, 2023

Wouldn't restrict me at all. My use-case is the same as in the reference from my initial post.

Given the definition of a vertex:

struct StandardVertex {
	vec3 position;
	vec3 normal;
	vec4 tangent;
	vec2 uv;
};

I'd like to generate the shader input description, required by the graphics API, using reflection. Currently I call offsetof manually and store that value in an attribute:

REFL_TYPE(ikaros::StandardVertex)
REFL_FIELD(position, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, position), "POSITION"));
REFL_FIELD(normal, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, normal), "NORMAL"));
REFL_FIELD(tangent, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, tangent), "TANGENT"));
REFL_FIELD(uv, ikaros::attr::VertexFieldDescription(offsetof(ikaros::StandardVertex, uv), "TEXCOORD", 0));
REFL_END

However, this feature is not critical at all. I am not 100% convinced yet myself whether this is the best approach. But I feel like having the field offset being part of the descriptor would be very helpful for this case, and probably a few others. The standard layout restriction isn't too problematic, one can practically always wrap a management class around a standard layout struct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants