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

Clean up the primitive ID textured shader, resolve the WebGL TODO #162

Merged
merged 5 commits into from
Aug 27, 2019

Conversation

mosra
Copy link
Collaborator

@mosra mosra commented Aug 19, 2019

Motivation and Context

Follow-up to #151, doing a cleanup pass over the PrimitiveIDTexturedShader. There was a few outdated assumptions about how GPUs work, unnecessarily large used data types and inconsistent attribute definitions. In short:

  • Using a rectangular 16-bit texture instead of a 32-bit power-of-two texture, saving more than half of the memory. The textures can be neither power-of-two nor square, those restrictions are useful only in order to save memory when having mipmapping (which is not the case here). Aassuming the object IDs never go over 65k, we can also save a half of the memory by using a smaller type.
  • Using texelFetch() instead of texture() -- that way we sidestep all coordinate rounding, filtering etc., making the texel access faster, and we don't need to pass texture size to the shader. That nicely resolves the WebGL TODO as well.
  • Using usampler2D instead of sampler2D, avoiding more rounding
  • Defining vertex attributes consistently with Magnum's builtin shaders (so the mesh can be evenutally used with those as well) -- the original definition was matching neither the shader code nor the mesh setup, which made it useless
  • Using packed 24bit data types for colors instead of expanding them to three-component floats

How Has This Been Tested

Tests pass locally. Unfortunately the PLY files can't be tested on the CI, so the green tick doesn't mean it works everywhere -- if you can, please try it out locally with MP3D data as well. The FRLInstanceMeshData update is done with best intentions in mind but as far as I understood it's a dead code.

All in all, this update could have a significant effect on rendering performance of PLY files -- didn't measure, tho.

@facebook-github-bot facebook-github-bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Aug 19, 2019
Cr::Containers::ArrayView<std::uint32_t> objectIds) {
const Mn::Vector2i textureSize{
gfx::PrimitiveIDTexturedShader::PrimitiveIDTextureWidth,
int(objectIds.size() +
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
int(objectIds.size() +
int(std::ceil(float(objectIds.size()) / gfx::PrimitiveIDTexturedShader::PrimitiveIDTextureWidth))

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well. I'm usually going over code and replacing ceil(float()) with the pure-integer solution, not the other way around 😆

Are you sure this would work correctly for values that have more than ~23 bits of precision? One PLY file has 9M of primitives, which is 24 bits. I'd trust the integer solution there instead ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

I pretty much always prefer ceil(float()) for readability :) -- The precision definitely isn't an issue here. The precision just needs to be good enough to separate things by gfx::PrimitiveIDTexturedShader::PrimitiveIDTextureWidth (which, since it's a power of 2, can be perfectly represented a float).

Copy link
Contributor

Choose a reason for hiding this comment

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

But this is entirely a personal preference, I am fine with it either way :)

Copy link
Contributor

@msbaines msbaines left a comment

Choose a reason for hiding this comment

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

LGTM. Will let others approve.

src/esp/gfx/PrimitiveIDTexturedShader.h Outdated Show resolved Hide resolved
Copy link
Contributor

@erikwijmans erikwijmans left a comment

Choose a reason for hiding this comment

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

Tested this locally as the CI doesn't check for instance ID rendering. LGTM

Before it was a 32-bit float texture inflated to nearest largest
power-of-two, which used more than twice the memory needed. Now it's just
16-bit uints and the texture has only one dimension POT'd.

Moreover, instead of doing texture() and having to pass texture size to
the shader, the shader now uses texelFetch(), which also resolves the
WebGL TODO.
The definitions were absolutely inconsistent with what the shader
actually wanted, no wonder they were totally useless.
@mosra
Copy link
Collaborator Author

mosra commented Aug 20, 2019

Pushed the typo fix, since it's a non-trivial change I'd like to have at least one thorough review.

And since this fixes the WebGL TODO, any chance somebody could test that? :)

@mosra
Copy link
Collaborator Author

mosra commented Aug 27, 2019

Oh, this is not merged yet? Can somebody give me one more approval, please?

@mosra mosra merged commit 0b669b6 into master Aug 27, 2019
@mosra mosra deleted the shader-cleanup branch August 27, 2019 14:50
Ram81 pushed a commit to Ram81/habitat-web-sim that referenced this pull request Dec 10, 2020
…cebookresearch#162)

* Save uniform location instead of retrieving it again every time.

* This comment does more harm than good.

* Rework how the primitive ID texture is used.

Before it was a 32-bit float texture inflated to nearest largest
power-of-two, which used more than twice the memory needed. Now it's just
16-bit uints and the texture has only one dimension POT'd.

Moreover, instead of doing texture() and having to pass texture size to
the shader, the shader now uses texelFetch(), which also resolves the
WebGL TODO.

* Properly define and actually use shader attributes.

The definitions were absolutely inconsistent with what the shader
actually wanted, no wonder they were totally useless.

* We can directly use the packed colors, no need to inflate them 4 times.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Do not delete this pull request or issue due to inactivity.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants