Build: patch USD to work with OpenGL core profile #110474

Merged
Brecht Van Lommel merged 1 commits from brecht/blender:fix-hydra-core-profile into main 2023-08-07 16:28:34 +02:00
2 changed files with 417 additions and 1 deletions

View File

@ -116,7 +116,8 @@ ExternalProject_Add(external_usd
# usd_hydra.diff also included the blender changes and usd_pull_1965 and has been edited to remove those sections.
PATCH_COMMAND ${PATCH_CMD} -p 1 -d ${BUILD_DIR}/usd/src/external_usd < ${PATCH_DIR}/usd.diff &&
${PATCH_CMD} -p 1 -d ${BUILD_DIR}/usd/src/external_usd < ${PATCH_DIR}/usd_pull_1965.diff &&
${PATCH_CMD} -p 1 -d ${BUILD_DIR}/usd/src/external_usd < ${PATCH_DIR}/usd_hydra.diff
${PATCH_CMD} -p 1 -d ${BUILD_DIR}/usd/src/external_usd < ${PATCH_DIR}/usd_hydra.diff &&
${PATCH_CMD} -p 1 -d ${BUILD_DIR}/usd/src/external_usd < ${PATCH_DIR}/usd_core_profile.diff
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${LIBDIR}/usd -Wno-dev ${DEFAULT_CMAKE_FLAGS} ${USD_EXTRA_ARGS}
INSTALL_DIR ${LIBDIR}/usd
)

View File

@ -0,0 +1,415 @@
diff --git a/pxr/imaging/hdSt/indirectDrawBatch.cpp b/pxr/imaging/hdSt/indirectDrawBatch.cpp
index ec9b224..bf59cdf 100644
--- a/pxr/imaging/hdSt/indirectDrawBatch.cpp
+++ b/pxr/imaging/hdSt/indirectDrawBatch.cpp
@@ -109,11 +109,17 @@ HdSt_IndirectDrawBatch::HdSt_IndirectDrawBatch(
, _allowGpuFrustumCulling(allowGpuFrustumCulling)
, _instanceCountOffset(0)
, _cullInstanceCountOffset(0)
+ , _vao(0)
{
_Init(drawItemInstance);
}
-HdSt_IndirectDrawBatch::~HdSt_IndirectDrawBatch() = default;
+HdSt_IndirectDrawBatch::~HdSt_IndirectDrawBatch()
+{
+ if (_vao) {
+ glDeleteVertexArrays(1, &_vao);
+ }
+}
/*virtual*/
void
@@ -1146,6 +1152,14 @@ HdSt_IndirectDrawBatch::_ExecuteDraw(
state.instancePrimvarBars);
}
+ // OpenGL core profile requries a VAO for binding buffers.
+ if (capabilities->GetCoreProfile()) {
+ if (!_vao) {
+ glCreateVertexArrays(1, &_vao);
+ }
+ glBindVertexArray(_vao);
+ }
+
state.BindResourcesForDrawing(renderPassState, *capabilities);
HdSt_GeometricShaderSharedPtr geometricShader = state.geometricShader;
@@ -1374,6 +1388,15 @@ HdSt_IndirectDrawBatch::_ExecuteFrustumCull(
cullingProgram.GetGeometricShader());
Hgi * hgi = resourceRegistry->GetHgi();
+ HgiCapabilities const *capabilities = hgi->GetCapabilities();
+
+ // OpenGL core profile requries a VAO for binding buffers.
+ if (capabilities->GetCoreProfile()) {
+ if (!_vao) {
+ glCreateVertexArrays(1, &_vao);
+ }
+ glBindVertexArray(_vao);
+ }
HgiGraphicsPipelineSharedPtr const & pso =
_GetCullPipeline(resourceRegistry,
diff --git a/pxr/imaging/hdSt/indirectDrawBatch.h b/pxr/imaging/hdSt/indirectDrawBatch.h
index 44971f5..4292aa3 100644
--- a/pxr/imaging/hdSt/indirectDrawBatch.h
+++ b/pxr/imaging/hdSt/indirectDrawBatch.h
@@ -195,6 +195,8 @@ private:
int _instanceCountOffset;
int _cullInstanceCountOffset;
+
+ uint32_t _vao;
};
diff --git a/pxr/imaging/hdSt/renderPassState.cpp b/pxr/imaging/hdSt/renderPassState.cpp
index da0e4f3..8f87fc1 100644
--- a/pxr/imaging/hdSt/renderPassState.cpp
+++ b/pxr/imaging/hdSt/renderPassState.cpp
@@ -769,7 +769,9 @@ HdStRenderPassState::Bind(HgiCapabilities const &hgiCapabilities)
// If not using GL_MULTISAMPLE, use GL_POINT_SMOOTH to render points as
// circles instead of square.
// XXX Switch points rendering to emit quad with FS that draws circle.
- glEnable(GL_POINT_SMOOTH);
+ if (!hgiCapabilities.GetCoreProfile()) {
+ glEnable(GL_POINT_SMOOTH);
+ }
}
}
@@ -813,7 +815,9 @@ HdStRenderPassState::Unbind(HgiCapabilities const &hgiCapabilities)
}
glEnable(GL_MULTISAMPLE);
- glDisable(GL_POINT_SMOOTH);
+ if (!hgiCapabilities.GetCoreProfile()) {
+ glDisable(GL_POINT_SMOOTH);
+ }
}
void
diff --git a/pxr/imaging/hgi/capabilities.h b/pxr/imaging/hgi/capabilities.h
index ae2ecb4..c86afcb 100644
--- a/pxr/imaging/hgi/capabilities.h
+++ b/pxr/imaging/hgi/capabilities.h
@@ -52,6 +52,11 @@ public:
HGI_API
virtual int GetShaderVersion() const = 0;
+ HGI_API
+ virtual bool GetCoreProfile() const {
+ return false;
+ }
+
HGI_API
size_t GetMaxUniformBlockSize() const {
return _maxUniformBlockSize;
diff --git a/pxr/imaging/hgiGL/blitCmds.cpp b/pxr/imaging/hgiGL/blitCmds.cpp
index ce62f41..20888a1 100644
--- a/pxr/imaging/hgiGL/blitCmds.cpp
+++ b/pxr/imaging/hgiGL/blitCmds.cpp
@@ -136,7 +136,7 @@ HgiGLBlitCmds::_Submit(Hgi* hgi, HgiSubmitWaitType wait)
// Capture OpenGL state before executing the 'ops' and restore it when this
// function ends. We do this defensively because parts of our pipeline may
// not set and restore all relevant gl state.
- HgiGL_ScopedStateHolder openglStateGuard;
+ HgiGL_ScopedStateHolder openglStateGuard(*hgi->GetCapabilities());
HgiGL* hgiGL = static_cast<HgiGL*>(hgi);
HgiGLDevice* device = hgiGL->GetPrimaryDevice();
diff --git a/pxr/imaging/hgiGL/capabilities.cpp b/pxr/imaging/hgiGL/capabilities.cpp
index 8711a62..e11324b 100644
--- a/pxr/imaging/hgiGL/capabilities.cpp
+++ b/pxr/imaging/hgiGL/capabilities.cpp
@@ -57,6 +57,7 @@ static const int _DefaultMaxClipDistances = 8;
HgiGLCapabilities::HgiGLCapabilities()
: _glVersion(0)
, _glslVersion(_DefaultGLSLVersion)
+ , _coreProfile(false)
{
_LoadCapabilities();
}
@@ -131,6 +132,11 @@ HgiGLCapabilities::_LoadCapabilities()
&uniformBufferOffsetAlignment);
_uniformBufferOffsetAlignment = uniformBufferOffsetAlignment;
}
+ if (_glVersion >= 320) {
+ GLint profileMask = 0;
+ glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &profileMask);
+ _coreProfile = (profileMask & GL_CONTEXT_CORE_PROFILE_BIT);
+ }
if (_glVersion >= 430) {
GLint maxShaderStorageBlockSize = 0;
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE,
@@ -259,4 +265,9 @@ HgiGLCapabilities::GetShaderVersion() const {
return _glslVersion;
}
+bool
+HgiGLCapabilities::GetCoreProfile() const {
+ return _coreProfile;
+}
+
PXR_NAMESPACE_CLOSE_SCOPE
diff --git a/pxr/imaging/hgiGL/capabilities.h b/pxr/imaging/hgiGL/capabilities.h
index 3c8f026..2f25b44 100644
--- a/pxr/imaging/hgiGL/capabilities.h
+++ b/pxr/imaging/hgiGL/capabilities.h
@@ -52,6 +52,9 @@ public:
HGIGL_API
int GetShaderVersion() const override;
+ HGIGL_API
+ bool GetCoreProfile() const override;
+
private:
void _LoadCapabilities();
@@ -60,6 +63,9 @@ private:
// GLSL version
int _glslVersion; // 400, 410, ...
+
+ // Core Profile
+ bool _coreProfile;
};
PXR_NAMESPACE_CLOSE_SCOPE
diff --git a/pxr/imaging/hgiGL/graphicsCmds.cpp b/pxr/imaging/hgiGL/graphicsCmds.cpp
index 5e17416..e59ae35 100644
--- a/pxr/imaging/hgiGL/graphicsCmds.cpp
+++ b/pxr/imaging/hgiGL/graphicsCmds.cpp
@@ -249,7 +249,7 @@ HgiGLGraphicsCmds::_Submit(Hgi* hgi, HgiSubmitWaitType wait)
// Capture OpenGL state before executing the 'ops' and restore it when this
// function ends. We do this defensively because parts of our pipeline may
// not set and restore all relevant gl state.
- HgiGL_ScopedStateHolder openglStateGuard;
+ HgiGL_ScopedStateHolder openglStateGuard(*hgi->GetCapabilities());
// Resolve multisample textures
HgiGL* hgiGL = static_cast<HgiGL*>(hgi);
diff --git a/pxr/imaging/hgiGL/graphicsPipeline.cpp b/pxr/imaging/hgiGL/graphicsPipeline.cpp
index 6983dd1..a1c7af7 100644
--- a/pxr/imaging/hgiGL/graphicsPipeline.cpp
+++ b/pxr/imaging/hgiGL/graphicsPipeline.cpp
@@ -42,7 +42,12 @@ HgiGLGraphicsPipeline::HgiGLGraphicsPipeline(
{
}
-HgiGLGraphicsPipeline::~HgiGLGraphicsPipeline() = default;
+HgiGLGraphicsPipeline::~HgiGLGraphicsPipeline()
+{
+ if (_vao) {
+ glDeleteVertexArrays(1, &_vao);
+ }
+}
void
HgiGLGraphicsPipeline::BindPipeline()
@@ -50,6 +55,7 @@ HgiGLGraphicsPipeline::BindPipeline()
if (_vao) {
glBindVertexArray(0);
glDeleteVertexArrays(1, &_vao);
+ _vao = 0;
}
if (!_descriptor.vertexBuffers.empty()) {
@@ -108,6 +114,8 @@ HgiGLGraphicsPipeline::BindPipeline()
glBindVertexArray(_vao);
}
+ const bool coreProfile = _hgi->GetCapabilities()->GetCoreProfile();
+
//
// Depth Stencil State
//
@@ -172,7 +180,9 @@ HgiGLGraphicsPipeline::BindPipeline()
// If not using GL_MULTISAMPLE, use GL_POINT_SMOOTH to render points as
// circles instead of square.
// XXX Switch points rendering to emit quad with FS that draws circle.
- glEnable(GL_POINT_SMOOTH);
+ if (!coreProfile) {
+ glEnable(GL_POINT_SMOOTH);
+ }
}
if (_descriptor.multiSampleState.alphaToCoverageEnable) {
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
@@ -207,7 +217,7 @@ HgiGLGraphicsPipeline::BindPipeline()
glFrontFace(GL_CCW);
}
- if (_descriptor.rasterizationState.lineWidth != 1.0f) {
+ if (!coreProfile && _descriptor.rasterizationState.lineWidth != 1.0f) {
glLineWidth(_descriptor.rasterizationState.lineWidth);
}
diff --git a/pxr/imaging/hgiGL/scopedStateHolder.cpp b/pxr/imaging/hgiGL/scopedStateHolder.cpp
index 89cd0ac..5c65753 100644
--- a/pxr/imaging/hgiGL/scopedStateHolder.cpp
+++ b/pxr/imaging/hgiGL/scopedStateHolder.cpp
@@ -26,6 +26,7 @@
#include "pxr/imaging/hgiGL/scopedStateHolder.h"
#include "pxr/imaging/hgiGL/conversions.h"
#include "pxr/imaging/hgiGL/diagnostic.h"
+#include "pxr/imaging/hgiGL/hgi.h"
#include "pxr/base/trace/trace.h"
#include "pxr/base/tf/diagnostic.h"
@@ -33,8 +34,10 @@
PXR_NAMESPACE_OPEN_SCOPE
-HgiGL_ScopedStateHolder::HgiGL_ScopedStateHolder()
- : _restoreRenderBuffer(0)
+HgiGL_ScopedStateHolder::HgiGL_ScopedStateHolder(
+ HgiCapabilities const& capabilities)
+ : _coreProfile(capabilities.GetCoreProfile())
+ , _restoreRenderBuffer(0)
, _restoreVao(0)
, _restoreDepthTest(false)
, _restoreDepthWriteMask(false)
@@ -115,7 +118,9 @@ HgiGL_ScopedStateHolder::HgiGL_ScopedStateHolder()
glGetBooleanv(
GL_SAMPLE_ALPHA_TO_ONE,
(GLboolean*)&_restoreSampleAlphaToOne);
- glGetFloatv(GL_LINE_WIDTH, &_lineWidth);
+ if (!_coreProfile) {
+ glGetFloatv(GL_LINE_WIDTH, &_lineWidth);
+ }
glGetBooleanv(GL_CULL_FACE, (GLboolean*)&_cullFace);
glGetIntegerv(GL_CULL_FACE_MODE, &_cullMode);
glGetIntegerv(GL_FRONT_FACE, &_frontFace);
@@ -139,7 +144,9 @@ HgiGL_ScopedStateHolder::HgiGL_ScopedStateHolder()
}
glGetBooleanv(GL_MULTISAMPLE, (GLboolean*)&_restoreMultiSample);
- glGetBooleanv(GL_POINT_SMOOTH, (GLboolean*)&_restorePointSmooth);
+ if (!_coreProfile) {
+ glGetBooleanv(GL_POINT_SMOOTH, (GLboolean*)&_restorePointSmooth);
+ }
HGIGL_POST_PENDING_GL_ERRORS();
#if defined(GL_KHR_debug)
@@ -235,7 +242,9 @@ HgiGL_ScopedStateHolder::~HgiGL_ScopedStateHolder()
_restoreViewport[2], _restoreViewport[3]);
glBindVertexArray(_restoreVao);
glBindRenderbuffer(GL_RENDERBUFFER, _restoreRenderBuffer);
- glLineWidth(_lineWidth);
+ if (!_coreProfile) {
+ glLineWidth(_lineWidth);
+ }
if (_cullFace) {
glEnable(GL_CULL_FACE);
} else {
@@ -285,10 +294,12 @@ HgiGL_ScopedStateHolder::~HgiGL_ScopedStateHolder()
glDisable(GL_MULTISAMPLE);
}
- if (_restorePointSmooth) {
- glEnable(GL_POINT_SMOOTH);
- } else {
- glDisable(GL_POINT_SMOOTH);
+ if (!_coreProfile) {
+ if (_restorePointSmooth) {
+ glEnable(GL_POINT_SMOOTH);
+ } else {
+ glDisable(GL_POINT_SMOOTH);
+ }
}
static const GLuint samplers[8] = {0};
diff --git a/pxr/imaging/hgiGL/scopedStateHolder.h b/pxr/imaging/hgiGL/scopedStateHolder.h
index d006480..be6698e 100644
--- a/pxr/imaging/hgiGL/scopedStateHolder.h
+++ b/pxr/imaging/hgiGL/scopedStateHolder.h
@@ -32,6 +32,7 @@
PXR_NAMESPACE_OPEN_SCOPE
+class HgiCapabilities;
/// \class HgiGLScopedStateHolder
///
@@ -50,7 +51,7 @@ class HgiGL_ScopedStateHolder final
{
public:
HGIGL_API
- HgiGL_ScopedStateHolder();
+ HgiGL_ScopedStateHolder(HgiCapabilities const& capabilities);
HGIGL_API
~HgiGL_ScopedStateHolder();
@@ -59,6 +60,8 @@ private:
HgiGL_ScopedStateHolder& operator=(const HgiGL_ScopedStateHolder&) = delete;
HgiGL_ScopedStateHolder(const HgiGL_ScopedStateHolder&) = delete;
+ bool _coreProfile;
+
int32_t _restoreRenderBuffer;
int32_t _restoreVao;
diff --git a/pxr/imaging/hgiInterop/opengl.cpp b/pxr/imaging/hgiInterop/opengl.cpp
index 683c058..e921c2a 100644
--- a/pxr/imaging/hgiInterop/opengl.cpp
+++ b/pxr/imaging/hgiInterop/opengl.cpp
@@ -110,6 +110,7 @@ HgiInteropOpenGL::HgiInteropOpenGL()
, _fsDepth(0)
, _prgNoDepth(0)
, _prgDepth(0)
+ , _vao(0)
, _vertexBuffer(0)
{
_vs = _CompileShader(_vertexFullscreen, GL_VERTEX_SHADER);
@@ -117,6 +118,7 @@ HgiInteropOpenGL::HgiInteropOpenGL()
_fsDepth = _CompileShader(_fragmentDepthFullscreen, GL_FRAGMENT_SHADER);
_prgNoDepth = _LinkProgram(_vs, _fsNoDepth);
_prgDepth = _LinkProgram(_vs, _fsDepth);
+ glCreateVertexArrays(1, &_vao);
_vertexBuffer = _CreateVertexBuffer();
TF_VERIFY(glGetError() == GL_NO_ERROR);
}
@@ -129,6 +131,7 @@ HgiInteropOpenGL::~HgiInteropOpenGL()
glDeleteProgram(_prgNoDepth);
glDeleteProgram(_prgDepth);
glDeleteBuffers(1, &_vertexBuffer);
+ glDeleteVertexArrays(1, &_vao);
TF_VERIFY(glGetError() == GL_NO_ERROR);
}
@@ -202,10 +205,13 @@ HgiInteropOpenGL::CompositeToInterop(
}
// Get the current array buffer binding state
+ GLint restoreVao = 0;
+ glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &restoreVao);
GLint restoreArrayBuffer = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &restoreArrayBuffer);
// Vertex attributes
+ glBindVertexArray(_vao);
const GLint locPosition = glGetAttribLocation(prg, "position");
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glVertexAttribPointer(locPosition, 4, GL_FLOAT, GL_FALSE,
@@ -271,6 +277,7 @@ HgiInteropOpenGL::CompositeToInterop(
glDisableVertexAttribArray(locPosition);
glDisableVertexAttribArray(locUv);
glBindBuffer(GL_ARRAY_BUFFER, restoreArrayBuffer);
+ glBindVertexArray(restoreVao);
if (!blendEnabled) {
glDisable(GL_BLEND);
diff --git a/pxr/imaging/hgiInterop/opengl.h b/pxr/imaging/hgiInterop/opengl.h
index 18840a9..27434ac 100644
--- a/pxr/imaging/hgiInterop/opengl.h
+++ b/pxr/imaging/hgiInterop/opengl.h
@@ -62,6 +62,7 @@ private:
uint32_t _fsDepth;
uint32_t _prgNoDepth;
uint32_t _prgDepth;
+ uint32_t _vao;
uint32_t _vertexBuffer;
};