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

GLTFExporter: implement 4-byte data alignment for buffer #13220

Merged
merged 3 commits into from
Feb 4, 2018
Merged

GLTFExporter: implement 4-byte data alignment for buffer #13220

merged 3 commits into from
Feb 4, 2018

Conversation

webprofusion-chrisc
Copy link
Contributor

This change ensures the allocated buffer size is aligned to the next 4-byte boundary. See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment

There may also be other allocated buffers (glb output?) that require 4-byte alignment, but this fixes validation of the exported file for me (via http://github.khronos.org/glTF-Validator/, from 27 errors down to zero).

This change is cribbed from getBufferPadded.js of obj2gltf but is modified for this use case.

This change ensures the allocated buffer size is aligned to the next 4-byte boundary. See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment 

There may also be other allocated buffers (glb output?) that require 4-byte alignment, but this fixes validation of the exported file for me. 

This change is cribbed from getBufferPadded.js of obj2gltf but is modified for this use case.

var remainder = bufferSize % boundary;

var padding = (remainder === 0 ) ? 0 : boundary - remainder;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could you remove the parens here, just: remainder === 0 ? 0 : boundary - remainder;

Copy link
Collaborator

@fernandojsg fernandojsg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

Copy link
Collaborator

@donmccurdy donmccurdy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍


var padding = remainder === 0 ? 0 : boundary - remainder;

return bufferSize + padding;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

function getPaddedBufferSize( bufferSize ) {

	return Math.ceil( bufferSize / 4 ) * 4;

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to leave that for debate. Your example is wonderfully succinct but the original is easier to read.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let @donmccurdy decide 😇

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code you copied pads a buffer. You just need to calculate a length.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we do need to calculate a length - for a padded buffer. I appreciate the terseness of your suggested alternative from a nerdy point of view, but not regards readability. I'm not massively fussed which approach is accepted however I'm just knocking down gltf validation errors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think +1 for the short version. There are already plenty of comments explaining the purpose clearly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, done!

@mrdoob mrdoob merged commit b4c6726 into mrdoob:dev Feb 4, 2018
@mrdoob
Copy link
Owner

mrdoob commented Feb 4, 2018

Thanks!

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.

5 participants