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

add info about alignment #107

Merged
merged 3 commits into from Nov 9, 2020
Merged

add info about alignment #107

merged 3 commits into from Nov 9, 2020

Conversation

benmkw
Copy link
Contributor

@benmkw benmkw commented Oct 16, 2020

thanks for your tutorial it helped me a lot

I would be happy to contribute this info. I don't have a strong opinion on where the best place would be or what else would need to be added so feel free to request changes or if you want also modify it yourself.

I would like to add a few code examples but I'm not sure how to best display the alignment/ memory regions in markdown.

@sotrh
Copy link
Owner

sotrh commented Oct 18, 2020

The best place to put something like this would be in showcase. Adding it to the main tutorial requires changing any tutorials after it. I think this is all useful information, but I don't think it requires a full tutorial.

One thing to note: shared, and packed aren't supported by shaderc. I'm not sure why, but they aren't.

@benmkw
Copy link
Contributor Author

benmkw commented Oct 18, 2020

agreed, I will push changes to make it easier for you to see differences and if you decide its ready I can squash them.

The shaderc thing sounds interesting and it would also be good to investigate if wgpu actually supports these things.

@I-graham
Copy link

I-graham commented Oct 27, 2020

Hello,

I found your tutorials very helpful, but I wasn't really satisfied with the fact that uniform structs were manually aligned by adding additional fields. After trying a lot of different things out, I was able to come up with this solution for making portable structs that can be passed to shaders easily. I defined some types to represent shader data types, and as long as:

  • The struct representing the uniform's members are of these datatypes (or of other correctly aligned datatypes such as cgmath's Matrix4)
  • The uniform is marked as std140 on the shader, and
  • The struct is marked as repr(16)

Then everything should be aligned correctly. Here's an example:

Datatypes to represent shader types in Rust (some of these repr annotations are redundant, but I included them for clarity):

#[repr(transparent)]
#[derive(Clone, Copy, Debug)]
pub struct GLSLint(pub i32);

#[repr(transparent)]
#[derive(Clone, Copy, Debug)]
pub struct GLSLfloat(pub f32);

#[repr(C, align(8))]
#[derive(Clone, Copy, Debug)]
pub struct GLSLvec2(pub f32, pub f32);

#[repr(C, align(16))]
#[derive(Clone, Copy, Debug)]
pub struct GLSLvec3(pub f32, pub f32, pub f32);

#[repr(C, align(16))]
#[derive(Clone, Copy, Debug)]
pub struct GLSLvec4(pub f32, pub f32, pub f32, pub f32);

Then we can define the struct which will be passed to the shader (order of fields does not matter if it's the same as the shader):

#[repr(C, align(16))]
pub struct Uniform {
	pub color_tint : GLSLvec4,
	pub scale      : GLSLvec2,
	pub translate  : GLSLvec2,
	pub texture_id : GLSLint,
}

Then on the Shader:

layout(set=0, binding=0, std140)
uniform Uniform {
	vec4 color_tint;
	vec2 scale;
	vec2 translate;
	int texture_id;
};

Or, for an array of uniforms:

struct Uniform {
	vec4 color_tint;
	vec2 scale;
	vec2 translate;
	int texture_id;
};

layout(set=0, binding=0, std140)
buffer UniformData {
	Uniform uniforms[];
};

Please let me know what you think of this solution.

@sotrh
Copy link
Owner

sotrh commented Oct 27, 2020

If you want to amend this pull request with something like what you described, that would be OK with me @I-graham . I just need a working code example before I can merge anything.

EDIT: Integration with cgmath (or mint as I'm considering doing as part of #109 ) is important. Being able to do math with the vertex fields is a must.

@benmkw
Copy link
Contributor Author

benmkw commented Oct 30, 2020

@sotrh I put everything in one commit and from my point of view would be ok with merging as is for the time being and then making further adjustments as needed using other PRs.

@sotrh
Copy link
Owner

sotrh commented Oct 30, 2020

I want all the showcase examples to have a working example crate. It doesn't need to be complex, it just needs to compile and run.

@benmkw
Copy link
Contributor Author

benmkw commented Oct 31, 2020

Hm I‘m not yet sure what a good example would be. I could use the basic compute example from the wgpu-rs repo and make the struct that’s being used a bit more complicated and insert the padding manually as an example.
Or I could create the padding manually once and also derive it using crevice and assert their sizes match.

@benmkw
Copy link
Contributor Author

benmkw commented Nov 5, 2020

added an example which can be extended once crates with full support for deriving layouts exist

@sotrh sotrh merged commit fd8c3e1 into sotrh:master Nov 9, 2020
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

Successfully merging this pull request may close these issues.

None yet

3 participants