File 2313.diff of Package OpenUSD

diff --git a/build_scripts/build_usd.py b/build_scripts/build_usd.py
index 40f6c3296c0..949acadb42e 100644
--- a/build_scripts/build_usd.py
+++ b/build_scripts/build_usd.py
@@ -1577,17 +1577,22 @@ def InstallMaterialX(context, force, buildArgs):
 
 ############################################################
 # Embree
-# For MacOS we use version 3.13.3 to include a fix from Intel
-# to build on Apple Silicon.
-if MacOS():
-    EMBREE_URL = "https://github.com/embree/embree/archive/v3.13.3.zip"
-else:
-    EMBREE_URL = "https://github.com/embree/embree/archive/v3.2.2.zip"
+
+EMBREE_DEFAULT_MAJOR_VERSION = 3
 
 def InstallEmbree(context, force, buildArgs):
+    if context.embreeMajorVersion >= 4:
+        EMBREE_URL = "https://github.com/RenderKit/embree/archive/refs/tags/v4.4.0.zip"
+    elif MacOS():
+        # For MacOS we use version 3.13.3 to include a fix from Intel
+        # to build on Apple Silicon.
+        EMBREE_URL = "https://github.com/RenderKit/embree/archive/refs/tags/v3.13.3.zip"
+    else:
+        EMBREE_URL = "https://github.com/RenderKit/embree/archive/refs/tags/v3.2.2.zip"
+
     with CurrentWorkingDirectory(DownloadURL(EMBREE_URL, context, force)):
         extraArgs = [
-            '-DTBB_ROOT={instDir}'.format(instDir=context.instDir),
+            '-DTBB_ROOT="{instDir}"'.format(instDir=context.instDir),
             '-DEMBREE_TUTORIALS=OFF',
             '-DEMBREE_ISPC_SUPPORT=OFF'
         ]
@@ -1601,7 +1606,9 @@ def InstallEmbree(context, force, buildArgs):
 
         RunCMake(context, force, extraArgs)
 
-EMBREE = Dependency("Embree", InstallEmbree, "include/embree3/rtcore.h")
+EMBREE = Dependency("Embree", InstallEmbree,
+                    "include/embree3/rtcore.h",
+                    "include/embree4/rtcore.h")
 
 ############################################################
 # AnimX
@@ -2121,6 +2128,12 @@ def InstallUSD(context, force, buildArgs):
                       help="Build Embree sample imaging plugin")
 subgroup.add_argument("--no-embree", dest="build_embree", action="store_false",
                       help="Do not build Embree sample imaging plugin (default)")
+group.add_argument("--embree-major-version",
+                   default=EMBREE_DEFAULT_MAJOR_VERSION, type=int,
+                   choices=[3, 4],
+                   help=(
+                       "The major version of Embree to build "
+                       "(default: {})").format(EMBREE_DEFAULT_MAJOR_VERSION))
 subgroup = group.add_mutually_exclusive_group()
 subgroup.add_argument("--prman", dest="build_prman", action="store_true",
                       default=False,
@@ -2342,6 +2355,7 @@ def __init__(self, args):
 
         # - Imaging plugins
         self.buildEmbree = self.buildImaging and args.build_embree
+        self.embreeMajorVersion = args.embree_major_version
         self.buildPrman = self.buildImaging and args.build_prman
         self.prmanLocation = (os.path.abspath(args.prman_location)
                                if args.prman_location else None)                               
@@ -2459,9 +2473,10 @@ def ForceBuildDependency(self, dep):
     PrintError("Draco plugin can not be enabled for monolithic build on Windows")
     sys.exit(1)
 
-# The versions of Embree we currently support do not support oneTBB.
-if context.buildOneTBB and context.buildEmbree:
-    PrintError("Embree support cannot be enabled when building against oneTBB")
+# When building with both oneTBB and Embree, a 4.x version of Embree must be
+# used.
+if context.buildOneTBB and (context.buildEmbree and context.embreeMajorVersion < 4):
+    PrintError("Embree 4.x or later must be selected when building against oneTBB")
     sys.exit(1)
 
 # Windows ARM64 requires oneTBB. Since oneTBB is a non-standard option for the
@@ -2656,6 +2671,7 @@ def _JoinVersion(v):
       OpenImageIO support:      {buildOIIO} 
       OpenColorIO support:      {buildOCIO} 
       Embree support:           {buildEmbree}
+        Embree major version:   {embreeMajorVersion}
       PRMan support:            {buildPrman}
       Vulkan support:           {enableVulkan}
     UsdImaging                  {buildUsdImaging}
@@ -2738,6 +2754,8 @@ def FormatBuildArguments(buildArgs):
     buildOIIO=("On" if context.buildOIIO else "Off"),
     buildOCIO=("On" if context.buildOCIO else "Off"),
     buildEmbree=("On" if context.buildEmbree else "Off"),
+    embreeMajorVersion=(context.embreeMajorVersion if context.buildEmbree
+                        else "N/A"),
     buildPrman=("On" if context.buildPrman else "Off"),
     buildUsdImaging=("On" if context.buildUsdImaging else "Off"),
     buildUsdview=("On" if context.buildUsdview else "Off"),
diff --git a/cmake/modules/FindEmbree.cmake b/cmake/modules/FindEmbree.cmake
index cd52f6b7f54..4c4ff1cdb2c 100644
--- a/cmake/modules/FindEmbree.cmake
+++ b/cmake/modules/FindEmbree.cmake
@@ -19,12 +19,30 @@
 #
 #=============================================================================
 
+find_path(EMBREE_INCLUDE_DIR
+    embree4/rtcore.h
+    embree3/rtcore.h
+HINTS
+    "${EMBREE_LOCATION}/include"
+    "$ENV{EMBREE_LOCATION}/include"
+DOC
+    "Embree headers path"
+)
+
+if (EMBREE_INCLUDE_DIR AND EXISTS "${EMBREE_INCLUDE_DIR}/embree4/rtcore.h")
+    set(EMBREE_VERSIONED_LIBRARY_NAME "embree4")
+    set(EMBREE_VERSION_HEADER_FILE_NAME "rtcore_config.h")
+else()
+    set(EMBREE_VERSIONED_LIBRARY_NAME "embree3")
+    set(EMBREE_VERSION_HEADER_FILE_NAME "rtcore_version.h")
+endif()
+
 if (APPLE)
-    set (EMBREE_LIB_NAME libembree3.dylib)
+    set (EMBREE_LIB_NAME "lib${EMBREE_VERSIONED_LIBRARY_NAME}.dylib")
 elseif (UNIX)
-    set (EMBREE_LIB_NAME libembree3.so)
+    set (EMBREE_LIB_NAME "lib${EMBREE_VERSIONED_LIBRARY_NAME}.so")
 elseif (WIN32)
-    set (EMBREE_LIB_NAME embree3.lib)
+    set (EMBREE_LIB_NAME "${EMBREE_VERSIONED_LIBRARY_NAME}.lib")
 endif()
 
 find_library(EMBREE_LIBRARY
@@ -38,21 +56,14 @@ find_library(EMBREE_LIBRARY
         "Embree library path"
 )
 
-find_path(EMBREE_INCLUDE_DIR
-    embree3/rtcore.h
-HINTS
-    "${EMBREE_LOCATION}/include"
-    "$ENV{EMBREE_LOCATION}/include"
-DOC
-    "Embree headers path"
-)
+set(EMBREE_VERSION_HEADER_FILE_PATH "${EMBREE_INCLUDE_DIR}/${EMBREE_VERSIONED_LIBRARY_NAME}/${EMBREE_VERSION_HEADER_FILE_NAME}")
 
-if (EMBREE_INCLUDE_DIR AND EXISTS "${EMBREE_INCLUDE_DIR}/embree3/rtcore_version.h" )
-    file(STRINGS "${EMBREE_INCLUDE_DIR}/embree3/rtcore_version.h" TMP REGEX "^#define RTC_VERSION_MAJOR.*$")
+if (EMBREE_INCLUDE_DIR AND EXISTS "${EMBREE_VERSION_HEADER_FILE_PATH}")
+    file(STRINGS "${EMBREE_VERSION_HEADER_FILE_PATH}" TMP REGEX "^#define RTC_VERSION_MAJOR.*$")
     string(REGEX MATCHALL "[0-9]+" MAJOR ${TMP})
-    file(STRINGS "${EMBREE_INCLUDE_DIR}/embree3/rtcore_version.h" TMP REGEX "^#define RTC_VERSION_MINOR.*$")
+    file(STRINGS "${EMBREE_VERSION_HEADER_FILE_PATH}" TMP REGEX "^#define RTC_VERSION_MINOR.*$")
     string(REGEX MATCHALL "[0-9]+" MINOR ${TMP})
-    file(STRINGS "${EMBREE_INCLUDE_DIR}/embree3/rtcore_version.h" TMP REGEX "^#define RTC_VERSION_PATCH.*$")
+    file(STRINGS "${EMBREE_VERSION_HEADER_FILE_PATH}" TMP REGEX "^#define RTC_VERSION_PATCH.*$")
     string(REGEX MATCHALL "[0-9]+" PATCH ${TMP})
 
     set (EMBREE_VERSION ${MAJOR}.${MINOR}.${PATCH})
diff --git a/pxr/imaging/plugin/hdEmbree/CMakeLists.txt b/pxr/imaging/plugin/hdEmbree/CMakeLists.txt
index 96ac989d6cd..d843804a15b 100644
--- a/pxr/imaging/plugin/hdEmbree/CMakeLists.txt
+++ b/pxr/imaging/plugin/hdEmbree/CMakeLists.txt
@@ -11,6 +11,12 @@ if (NOT ${PXR_BUILD_GPU_SUPPORT})
     return()
 endif()
 
+if (EMBREE_VERSION VERSION_GREATER_EQUAL 4.0.0)
+    add_definitions(-DPXR_EMBREE_MAJOR_VERSION=4)
+else()
+    add_definitions(-DPXR_EMBREE_MAJOR_VERSION=3)
+endif()
+
 pxr_plugin(hdEmbree
    LIBRARIES
         plug
diff --git a/pxr/imaging/plugin/hdEmbree/context.h b/pxr/imaging/plugin/hdEmbree/context.h
index 4165adb1e6b..7e5c099b741 100644
--- a/pxr/imaging/plugin/hdEmbree/context.h
+++ b/pxr/imaging/plugin/hdEmbree/context.h
@@ -14,7 +14,11 @@
 #include "pxr/base/gf/matrix4f.h"
 #include "pxr/base/vt/array.h"
 
-#include <embree3/rtcore.h>
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+    #include <embree4/rtcore.h>
+#else
+    #include <embree3/rtcore.h>
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
 
 PXR_NAMESPACE_OPEN_SCOPE
 
diff --git a/pxr/imaging/plugin/hdEmbree/mesh.h b/pxr/imaging/plugin/hdEmbree/mesh.h
index bbb006302f4..a6f71f110ff 100644
--- a/pxr/imaging/plugin/hdEmbree/mesh.h
+++ b/pxr/imaging/plugin/hdEmbree/mesh.h
@@ -15,8 +15,13 @@
 
 #include "pxr/imaging/plugin/hdEmbree/meshSamplers.h"
 
-#include <embree3/rtcore.h>
-#include <embree3/rtcore_ray.h>
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+    #include <embree4/rtcore.h>
+    #include <embree4/rtcore_ray.h>
+#else
+    #include <embree3/rtcore.h>
+    #include <embree3/rtcore_ray.h>
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
 
 PXR_NAMESPACE_OPEN_SCOPE
 
diff --git a/pxr/imaging/plugin/hdEmbree/meshSamplers.h b/pxr/imaging/plugin/hdEmbree/meshSamplers.h
index c32c35fffa3..51459a2ed45 100644
--- a/pxr/imaging/plugin/hdEmbree/meshSamplers.h
+++ b/pxr/imaging/plugin/hdEmbree/meshSamplers.h
@@ -12,8 +12,13 @@
 #include "pxr/imaging/hd/meshUtil.h"
 #include "pxr/base/vt/types.h"
 
-#include <embree3/rtcore.h>
-#include <embree3/rtcore_geometry.h>
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+    #include <embree4/rtcore.h>
+    #include <embree4/rtcore_geometry.h>
+#else
+    #include <embree3/rtcore.h>
+    #include <embree3/rtcore_geometry.h>
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
 
 #include <bitset>
 
diff --git a/pxr/imaging/plugin/hdEmbree/pch.h b/pxr/imaging/plugin/hdEmbree/pch.h
index 4e16a844d78..901e93859fc 100644
--- a/pxr/imaging/plugin/hdEmbree/pch.h
+++ b/pxr/imaging/plugin/hdEmbree/pch.h
@@ -76,9 +76,15 @@
 #include <unordered_set>
 #include <utility>
 #include <vector>
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+#include <embree4/rtcore.h>
+#include <embree4/rtcore_geometry.h>
+#include <embree4/rtcore_ray.h>
+#else
 #include <embree3/rtcore.h>
 #include <embree3/rtcore_geometry.h>
 #include <embree3/rtcore_ray.h>
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
 #ifdef PXR_PYTHON_SUPPORT_ENABLED
 #include "pxr/base/tf/pySafePython.h"
 #endif // PXR_PYTHON_SUPPORT_ENABLED
diff --git a/pxr/imaging/plugin/hdEmbree/renderDelegate.h b/pxr/imaging/plugin/hdEmbree/renderDelegate.h
index 1d8694daa61..826d020e048 100644
--- a/pxr/imaging/plugin/hdEmbree/renderDelegate.h
+++ b/pxr/imaging/plugin/hdEmbree/renderDelegate.h
@@ -13,8 +13,13 @@
 #include "pxr/imaging/plugin/hdEmbree/renderer.h"
 #include "pxr/base/tf/staticTokens.h"
 
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+    #include <embree4/rtcore.h>
+#else
+    #include <embree3/rtcore.h>
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+
 #include <mutex>
-#include <embree3/rtcore.h>
 
 PXR_NAMESPACE_OPEN_SCOPE
 
diff --git a/pxr/imaging/plugin/hdEmbree/renderParam.h b/pxr/imaging/plugin/hdEmbree/renderParam.h
index 206a7458bcb..af94ef10a46 100644
--- a/pxr/imaging/plugin/hdEmbree/renderParam.h
+++ b/pxr/imaging/plugin/hdEmbree/renderParam.h
@@ -11,7 +11,11 @@
 #include "pxr/imaging/hd/renderDelegate.h"
 #include "pxr/imaging/hd/renderThread.h"
 
-#include <embree3/rtcore.h>
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+    #include <embree4/rtcore.h>
+#else
+    #include <embree3/rtcore.h>
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
 
 PXR_NAMESPACE_OPEN_SCOPE
 
diff --git a/pxr/imaging/plugin/hdEmbree/renderer.cpp b/pxr/imaging/plugin/hdEmbree/renderer.cpp
index cc8ddb02702..381bed73e57 100644
--- a/pxr/imaging/plugin/hdEmbree/renderer.cpp
+++ b/pxr/imaging/plugin/hdEmbree/renderer.cpp
@@ -28,8 +28,13 @@
 // oneTBB as a min spec. This applies the "Work" thread limit to the
 // render thread if "Work" is using old TBB, but won't affect other "Work"
 // implementations.  Note that it may affect Embree TBB usage as well.
+//
+// The header that provides version definitions moved between TBB
+// (tbb_stddef.h) and oneTBB (version.h), so we use the lowest level header
+// that is available at the same path in both and let it bring in the
+// definitions.
 // -------------------------------------------------------------------------
-#include <tbb/tbb_stddef.h>
+#include <tbb/blocked_range.h>
 
 #if TBB_INTERFACE_VERSION_MAJOR < 12
 
@@ -709,9 +714,13 @@ HdEmbreeRenderer::_TraceRay(unsigned int x, unsigned int y,
     rayHit.ray.flags = 0;
     _PopulateRayHit(&rayHit, origin, dir, 0.0f);
     {
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+      rtcIntersect1(_scene, &rayHit);
+#else
       RTCIntersectContext context;
       rtcInitIntersectContext(&context);
       rtcIntersect1(_scene, &context, &rayHit);
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
       //
       // there is something odd about how this is used in Embree. Is it reversed
       // here and then when it it used in
@@ -1047,9 +1056,13 @@ HdEmbreeRenderer::_ComputeAmbientOcclusion(GfVec3f const& position,
         shadow.flags = 0;
         _PopulateRay(&shadow, position, shadowDir, 0.001f);
         {
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+          rtcOccluded1(_scene, &shadow);
+#else
           RTCIntersectContext context;
           rtcInitIntersectContext(&context);
           rtcOccluded1(_scene,&context,&shadow);
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
         }
 
         // Record this AO ray's contribution to the occlusion factor: a
diff --git a/pxr/imaging/plugin/hdEmbree/renderer.h b/pxr/imaging/plugin/hdEmbree/renderer.h
index 2da98808481..913b83eccc3 100644
--- a/pxr/imaging/plugin/hdEmbree/renderer.h
+++ b/pxr/imaging/plugin/hdEmbree/renderer.h
@@ -15,8 +15,13 @@
 #include "pxr/base/gf/matrix4d.h"
 #include "pxr/base/gf/rect2i.h"
 
-#include <embree3/rtcore.h>
-#include <embree3/rtcore_ray.h>
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+    #include <embree4/rtcore.h>
+    #include <embree4/rtcore_ray.h>
+#else
+    #include <embree3/rtcore.h>
+    #include <embree3/rtcore_ray.h>
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
 
 #include <random>
 #include <atomic>
diff --git a/pxr/imaging/plugin/hdEmbree/testenv/testHdEmbree.cpp b/pxr/imaging/plugin/hdEmbree/testenv/testHdEmbree.cpp
index 02c2ca69f82..45370dfed03 100644
--- a/pxr/imaging/plugin/hdEmbree/testenv/testHdEmbree.cpp
+++ b/pxr/imaging/plugin/hdEmbree/testenv/testHdEmbree.cpp
@@ -23,7 +23,12 @@
 
 #include "pxr/base/tf/errorMark.h"
 
-#include <embree3/rtcore.h>
+#if defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+    #include <embree4/rtcore.h>
+#else
+    #include <embree3/rtcore.h>
+#endif // defined(PXR_EMBREE_MAJOR_VERSION) && PXR_EMBREE_MAJOR_VERSION >= 4
+
 #include <iostream>
 
 PXR_NAMESPACE_USING_DIRECTIVE
openSUSE Build Service is sponsored by