Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Remove re-linking programs approach - #14482

Merged
astojilj merged 3 commits into
masterfrom
astojilj-remove-re-linking
May 20, 2019
Merged

astojilj merged 3 commits into
masterfrom
astojilj-remove-re-linking

Conversation

@astojilj

@astojilj astojilj commented Apr 23, 2019 •

Copy link
Copy Markdown
Contributor

Remove re-linking programs as redundant.
It costs (cheaper to link once than twice) and (subjective) is not that common GL API
usage pattern, although perfectly legal and permitted.
Initial idea, behind the removed code, was to enable work on optimization
that would reduce number of attrib setup calls in case when VAO is not
available (as described in #9433).

As such optimization is not implemented, and it is arguable if it makes sense
to do it now, we can remove re-linking.

Related to closed PRs #9433 and PR #11583.

I have measured the time spent just on relinking using release build on iPhone SE (A9, same as iPhone 6S):

  • 1st run after reboot or installation
    Total 37.14ms, average per program:1.86ms

  • reopening
    Total: 2.47ms, average per program: 0.12ms

This time we save using the patch here.

@astojilj

Copy link
Copy Markdown
Contributor Author

@ivovandongen @kkaefer @alexshalamov PTAL

Comment thread src/mbgl/gl/attribute.hpp Resolved
Comment thread src/mbgl/gl/attribute.hpp Resolved
Comment thread src/mbgl/gl/attribute.cpp
void bindAttributeLocation(Context& context, ProgramID id, AttributeLocation location, const char* name) {
// We're using sequentially numberered attribute locations starting with 0. Therefore we can use
// the location as a proxy for the number of attributes.
if (location >= context.maximumVertexBindingCount) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we still have an equivalent check in place?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The check is done before render.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@astojilj Correct me if I'm wrong, but the check you mention only takes care of logging, it doesn't prevent rendering. So the queryLocations is still reached. What happens when an attribute can't be found or we cross the MAX_VERTEX_ATTRIBUTES limit? If glGetAttribLocation returns -1 in that situation, things will go wrong when using the returned location as an index here no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, if glGetAttribLocation returns -1, then if (location) { here, as false, guards the result.at(*location) = binding;.

Binding, or not binding, vertex attributes before linking, based on GL_MAX_VERTEX_ATTRIBS count, doesn't change the behavior of following glLinkProgram and error.
As I saw, this was resolved by packing attributes PR and now we use the log to inform about error.

Comment thread src/mbgl/gl/attribute.hpp Resolved
@friedbunny friedbunny added Core The cross-platform C++ core, aka mbgl refactor rendering labels Apr 23, 2019
Comment thread src/mbgl/gl/attribute.cpp

using namespace platform;

void bindAttributeLocation(Context& context, ProgramID id, AttributeLocation location, const char* name) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This function's task is to set an explicit binding for an attribute location before (re-)linking the program. The code flow is a bit hard to follow due to it's heavily templated nature, but it basically worked like this:

  • Link program
  • Query program to find out what attributes are actually present. This is necessary because we disable/enable attributes for data-driven styling (DDS)
  • The AttributeLocations "iterates" (using template expansions...) over an explicitly ordered list of attributes (they're stored in the *_program.hpp in a TypeList:
    TypeList<
    attributes::pos>,

    and augmented in the gfx::Program class with the attributes for data-driven styling, which is a subset of this list:
    class CirclePaintProperties : public Properties<
    CircleBlur,
    CircleColor,
    CircleOpacity,
    CirclePitchAlignment,
    CirclePitchScale,
    CircleRadius,
    CircleStrokeColor,
    CircleStrokeOpacity,
    CircleStrokeWidth,
    CircleTranslate,
    CircleTranslateAnchor
    > {};

    That means that attributes get sequentially numbered, starting with location = 0 for the second linking pass. Some (many?) OpenGL implementations have massive performance issues if the attribute 0 is disabled (e.g. because it is not used). With this weird double-linking approach, we're ensuring that location 0 is always enabled because it is the first attribute of the program's non-DDS attribute list. Removing relinking is definitely possible, but we should preserve the current behavior of explicitly assigning attribute bindings with glBindAttribLocation. We could probably get away with only running it for the first attribute in the list, since we don't really care whether the rest are numbered sequentially. What doesn't work is to run glBindAttribLocation blindly for all attributes because we sometimes have more than 8 of those, and many implementations don't support attribute locations beyond 7, so we have to have some awareness of which attributes are in use or not. We could query this information from the gfx::AttributeBindings<AttributeList> object passed to the draw function (that we also use to compute the gl::AttributeKey<AttributeList> for finding the correct program instance in the first place). Explicitly binding all attributes before the first link also has the advantage that we don't have to query the attribute locations after linking because they're guaranteed to be the ones we set them to.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Query program to find out what attributes are actually present. This is necessary because we disable/enable attributes for data-driven styling (DDS)

we don't have to query the attribute locations after linking because they're guaranteed to be the ones we set them to.

The patch is replacing usage of glGetProgramiv and per-attrib glGetActiveAttrib by glGetAttribLocation. I don't think it is introducing redundancy.

With this weird double-linking approach, we're ensuring that location 0 is always enabled because it is the first attribute of the program's non-DDS attribute list. Removing relinking is definitely possible, but we should preserve the current behavior of explicitly assigning attribute bindings with glBindAttribLocation. We could probably get away with only running it for the first attribute in the list, since we don't really care whether the rest are numbered sequentially.

This proposal sounds good and looks safe to assert (I checked the shaders code before working on this) position attribute being always enabled, for all the shaders on index 0. I'll update patch as that would avoid double linking and resolve other concerns.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think it is introducing redundancy.
I didn't mean to suggest that your patch does; I was describing the old approach.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The patch Remove re-linking: bind position attribute at location 0. binds the position related attribute at location 0.

To expand on the comment:
// It is important to have attribute at position 0 enabled: conveniently,
// position attribute is always first and always enabled. The integrity of
// this assumption is verified in AttributeLocations::queryLocations and
// AttributeLocations::getFirstAttribName.

there is compile time check that the first element is position and assert verifying the bound location 0 enabled.

I have measured the time spent just on relinking using release build on iPhone SE (A9, same as iPhone 6S):

  • 1st run after reboot or installation
    Total 37.14ms, average per program:1.86ms

  • reopening
    Total: 2.47ms, average per program: 0.12ms

This time we save using the patch here.

@astojilj
astojilj force-pushed the astojilj-remove-re-linking branch 3 times, most recently from 43be3e2 to d71551f Compare May 20, 2019 07:48
astojilj added 3 commits May 20, 2019 10:58
Remove re-linking programs as redundant.
It costs (cheaper to link once than twice) and is not that common GL API
usage pattern (subjective).
Initial idea was to enable work on optimization what would reduce number
of attrib setup calls in case that VAO is not available (#9433).

As such optimization is not implemented, we can remove re-linking.

Related to closed PR #9433 and PR #11583.
@astojilj
astojilj force-pushed the astojilj-remove-re-linking branch from d71551f to 99e5f26 Compare May 20, 2019 07:59
@astojilj astojilj self-assigned this May 20, 2019
@astojilj
astojilj requested a review from pozdnyakov May 20, 2019 08:06
@astojilj astojilj added performance Speed, stability, CPU usage, memory usage, or power usage and removed refactor labels May 20, 2019

@kkaefer kkaefer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks good and definitely simplifies the workflow!

@astojilj
astojilj merged commit 2a3a1c7 into master May 20, 2019
@astojilj
astojilj deleted the astojilj-remove-re-linking branch May 20, 2019 13:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Core The cross-platform C++ core, aka mbgl performance Speed, stability, CPU usage, memory usage, or power usage rendering

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants