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

Created accessor bufferViews should be byte aligned #421

Merged
merged 4 commits into from
Sep 12, 2018
Merged

Conversation

ggetz
Copy link
Contributor

@ggetz ggetz commented Sep 5, 2018

This is why Cesium_Air.gltf was not rendering in CesiumGS/cesium#6805

The bufferViews created when splitting accessors with different byte strides must be byte aligned.

@ggetz ggetz requested a review from lilleyse September 5, 2018 17:45
@lilleyse
Copy link
Contributor

lilleyse commented Sep 5, 2018

What is the error in Cesium? I took a look at Cesium_Air after going through gltf-pipeline and it seems like Cesium should still render it despite the validation errors.

The model seems to meet this condition at least:

The offset of an accessor into a bufferView (i.e., accessor.byteOffset) and the offset of an accessor into a buffer (i.e., accessor.byteOffset + bufferView.byteOffset) must be a multiple of the size of the accessor's component type.

Though doesn't follow

For performance and compatibility reasons, each element of a vertex attribute must be aligned to 4-byte boundaries inside bufferView (i.e., accessor.byteOffset and bufferView.byteStride must be multiples of 4).

Which is not good, but Cesium should still render it.

The reason I bring this all up is because editing the byte offset is not enough, padding also needs to get added to the buffer which could be a longer fix.

@ggetz
Copy link
Contributor Author

ggetz commented Sep 5, 2018

Sandcastle example

Getting the following WebGL errors:

bucket.html:1 [.Offscreen-For-WebGL-0000008F71D9A340]GL ERROR :GL_INVALID_OPERATION : glVertexAttribPointer: offset not valid for type
45115:48:29.071 [.Offscreen-For-WebGL-0000008F71D9A340]GL ERROR :GL_INVALID_OPERATION : glDrawElements: attached to enabled attrib 0 : no buffer

The byteOffset was being set to 2, which was what was causing the error because of the later

For performance and compatibility reasons, each element of a vertex attribute must be aligned to 4-byte boundaries inside bufferView (i.e., accessor.byteOffset and bufferView.byteStride must be multiples of 4).

@@ -804,7 +804,7 @@ function moveByteStrideToBufferView(gltf) {
accessor.bufferView = newBufferViewId;
accessor.byteOffset = accessor.byteOffset - currentByteOffset;
}
currentByteOffset = accessorByteOffset + accessorByteLength;
currentByteOffset = accessorByteOffset + accessorByteLength + accessorByteLength % 4;
Copy link
Contributor

@lilleyse lilleyse Sep 12, 2018

Choose a reason for hiding this comment

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

@ggetz I stared at this for a really long time. The fix here is pretty close, but I think it needs to be

currentByteOffset = (i < accessorsLength - 1) ? accessors[i + 1].byteOffset : undefined;

Which is setting the current byte offset to next accessor's byte offset rather than the end of the current accessor. Cesium_Air brought up a discrepancy where these two values are not always the same if there is padding between the current accessor and the next accessor.

The % 4 approach works a lot of the time but has some edge cases (say if the current accessor ends at byte 1, next accessor starts at byte 1 too (both are using BYTE component type), currentByteOffset would be set to 2 and the next accessor's byte offset would be computed as -1.

This approach should work in Cesium without requiring the more involved changes of always aligning byte offsets and byte strides to 4 bytes, which I still think involves buffer copies of some sort.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, thanks @lilleyse! This means we don't have to pad the buffer correct? That's good news!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah no padding required for this fix thankfully.

@ggetz
Copy link
Contributor Author

ggetz commented Sep 12, 2018

Thanks again @lilleyse, updated. I can confirm Cesium Air now loads in CesiumGS/cesium#6805 with this change.

@lilleyse
Copy link
Contributor

Nice!

@lilleyse lilleyse merged commit 353cd8e into master Sep 12, 2018
@lilleyse lilleyse deleted the byte-alignment branch September 12, 2018 15:07
@lilleyse
Copy link
Contributor

Opened #426 so we can tackle the proper fix some other time.

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.

2 participants