Repository navigation
Remove re-linking programs approach - #14482
Conversation
| 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) { |
There was a problem hiding this comment.
Do we still have an equivalent check in place?
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
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.
|
|
||
| using namespace platform; | ||
|
|
||
| void bindAttributeLocation(Context& context, ProgramID id, AttributeLocation location, const char* name) { |
There was a problem hiding this comment.
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.hppin aTypeList:mapbox-gl-native/src/mbgl/programs/circle_program.hpp
Lines 18 to 19 in 8a3d07f
and augmented in thegfx::Programclass with the attributes for data-driven styling, which is a subset of this list:mapbox-gl-native/src/mbgl/style/layers/circle_layer_properties.hpp
Lines 61 to 73 in 8a3d07f
That means that attributes get sequentially numbered, starting withlocation = 0for 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 withglBindAttribLocation. 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 runglBindAttribLocationblindly 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 thegfx::AttributeBindings<AttributeList>object passed to thedrawfunction (that we also use to compute thegl::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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I don't think it is introducing redundancy.
I didn't mean to suggest that your patch does; I was describing the old approach.
There was a problem hiding this comment.
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.
43be3e2 to
d71551f
Compare
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.
d71551f to
99e5f26
Compare
kkaefer
left a comment
There was a problem hiding this comment.
This looks good and definitely simplifies the workflow!
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.