Skip to content

Commit

Permalink
save and restore uniform buffer bindings during batch renderer selection
Browse files Browse the repository at this point in the history
When Maya is using OpenGL Core Profile as the rendering engine (in either
compatibility or strict mode), batch renders like those done in the "Render
View" window or through the ogsRender command do not properly track uniform
buffer binding state. This was causing issues where the first batch render
performed would look correct, but then all subsequent renders done in that Maya
session would be completely black (no alpha), even if the frame contained only
Maya-native geometry or if a new scene was created/opened.

To avoid this problem, we need to save and restore Maya's bindings across Hydra
calls. We try not to bog down performance by saving and restoring *all*
GL_MAX_UNIFORM_BUFFER_BINDINGS possible bindings, so instead we only do just
enough to avoid issues. Empirically, the problematic binding has been the
material binding at index 4.

This workaround was already being used during rendering, but this change
applies it during selection as well.
  • Loading branch information
mattyjams committed Feb 19, 2020
1 parent 56ca3f4 commit 37d2924
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/render/pxrUsdMayaGL/batchRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ TF_DEFINE_PRIVATE_TOKENS(
);


namespace {

static constexpr size_t _UNIFORM_BINDINGS_TO_SAVE = 5u;

} // anonymous namespace


TF_INSTANTIATE_SINGLETON(UsdMayaGLBatchRenderer);

const int UsdMayaGLBatchRenderer::ProfilerCategory = MProfiler::addCategory(
Expand Down Expand Up @@ -1129,6 +1136,27 @@ UsdMayaGLBatchRenderer::_TestIntersection(
GL_TEXTURE_BIT |
GL_POLYGON_BIT);

// XXX: When Maya is using OpenGL Core Profile as the rendering engine (in
// either compatibility or strict mode), batch renders like those done in
// the "Render View" window or through the ogsRender command do not
// properly track uniform buffer binding state. This was causing issues
// where the first batch render performed would look correct, but then all
// subsequent renders done in that Maya session would be completely black
// (no alpha), even if the frame contained only Maya-native geometry or if
// a new scene was created/opened.
//
// To avoid this problem, we need to save and restore Maya's bindings
// across Hydra calls. We try not to bog down performance by saving and
// restoring *all* GL_MAX_UNIFORM_BUFFER_BINDINGS possible bindings, so
// instead we only do just enough to avoid issues. Empirically, the
// problematic binding has been the material binding at index 4.
std::vector<GLint> uniformBufferBindings(_UNIFORM_BINDINGS_TO_SAVE, 0);
for (size_t i = 0u; i < uniformBufferBindings.size(); ++i) {
glGetIntegeri_v(GL_UNIFORM_BUFFER_BINDING,
(GLuint)i,
&uniformBufferBindings[i]);
}

// hydra orients all geometry during topological processing so that
// front faces have ccw winding. We disable culling because culling
// is handled by fragment shader discard.
Expand Down Expand Up @@ -1159,6 +1187,13 @@ UsdMayaGLBatchRenderer::_TestIntersection(
_hdEngine.SetTaskContextData(HdxPickTokens->pickParams, vtPickParams);
_hdEngine.Execute(_renderIndex.get(), &tasks);

// XXX: Restore Maya's uniform buffer binding state. See above for details.
for (size_t i = 0u; i < uniformBufferBindings.size(); ++i) {
glBindBufferBase(GL_UNIFORM_BUFFER,
(GLuint)i,
uniformBufferBindings[i]);
}

glPopAttrib();

return (result->size() > 0);
Expand Down Expand Up @@ -1308,7 +1343,6 @@ UsdMayaGLBatchRenderer::_Render(
// restoring *all* GL_MAX_UNIFORM_BUFFER_BINDINGS possible bindings, so
// instead we only do just enough to avoid issues. Empirically, the
// problematic binding has been the material binding at index 4.
static constexpr size_t _UNIFORM_BINDINGS_TO_SAVE = 5u;
std::vector<GLint> uniformBufferBindings(_UNIFORM_BINDINGS_TO_SAVE, 0);
for (size_t i = 0u; i < uniformBufferBindings.size(); ++i) {
glGetIntegeri_v(GL_UNIFORM_BUFFER_BINDING,
Expand Down

0 comments on commit 37d2924

Please sign in to comment.