Skip to content

Commit

Permalink
Switch to embree-aarch64
Browse files Browse the repository at this point in the history
(cherry picked from commit 73e2ccd)
  • Loading branch information
JFonS authored and akien-mga committed May 5, 2021
1 parent 4b8a1d2 commit 1cfed0d
Show file tree
Hide file tree
Showing 97 changed files with 6,055 additions and 1,286 deletions.
5 changes: 5 additions & 0 deletions COPYRIGHT.txt
Expand Up @@ -133,6 +133,11 @@ Copyright: 2018, Eric Lasota
2018, Microsoft Corp.
License: Expat

Files: ./thirdparty/embree/
Comment: Embree
Copyright: 2009-2021 Intel Corporation
License: Apache-2.0

Files: ./thirdparty/enet/
Comment: ENet
Copyright: 2002-2020, Lee Salzman
Expand Down
20 changes: 5 additions & 15 deletions modules/lightmapper_cpu/config.py
Expand Up @@ -6,23 +6,13 @@ def can_build(env, platform):
# `can_build()` for that module, so we need to duplicate that code as a short-term
# solution.

# Embree requires at least SSE2 to be available, so 32-bit and ARM64 builds are
# not supported.
# It's also only relevant for tools build and desktop platforms,
# as doing lightmap generation on Android or HTML5 would be a bit far-fetched.
supported_platform = platform in ["x11", "osx", "windows", "server"]
supported_bits = env["bits"] == "64"
supported_arch = env["arch"] != "arm64"
if platform == "android":
return env["android_arch"] in ["arm64v8", "x86", "x86_64"]

# Hack to disable on Linux arm64. This won't work well for cross-compilation (checks
# host, not target) and would need a more thorough fix by refactoring our arch and
# bits-handling code.
from platform import machine

if platform == "x11" and machine() != "x86_64":
supported_arch = False
if platform in ["javascript", "server"]:
return False

return supported_platform and supported_bits and supported_arch
return True


def configure(env):
Expand Down
16 changes: 5 additions & 11 deletions modules/raycast/SCsub
Expand Up @@ -70,25 +70,19 @@ if env["builtin_embree"]:
thirdparty_sources = [thirdparty_dir + file for file in embree_src]

env_raycast.Prepend(CPPPATH=[thirdparty_dir, thirdparty_dir + "include"])
env_raycast.Append(
CPPDEFINES=[
"EMBREE_TARGET_SSE2",
"EMBREE_LOWEST_ISA",
"TASKING_INTERNAL",
"NDEBUG",
"__SSE2__",
"__SSE__",
]
)
env_raycast.Append(CPPDEFINES=["EMBREE_TARGET_SSE2", "EMBREE_LOWEST_ISA", "TASKING_INTERNAL", "NDEBUG"])

if not env.msvc:
env_raycast.Append(CPPFLAGS=["-msse2", "-mxsave"])
if env["arch"] in ["x86", "x86_64"]:
env_raycast.Append(CPPFLAGS=["-msse2", "-mxsave"])

if env["platform"] == "windows":
env_raycast.Append(CPPFLAGS=["-mstackrealign"])

if env["platform"] == "windows":
if env.msvc:
env.Append(LINKFLAGS=["psapi.lib"])
env_raycast.Append(CPPDEFINES=["__SSE2__", "__SSE__"])
else:
env.Append(LIBS=["psapi"])

Expand Down
21 changes: 7 additions & 14 deletions modules/raycast/config.py
@@ -1,21 +1,14 @@
def can_build(env, platform):
# Embree requires at least SSE2 to be available, so 32-bit and ARM64 builds are
# not supported.
# It's also only relevant for tools build and desktop platforms,
# as doing lightmap generation on Android or HTML5 would be a bit far-fetched.
supported_platform = platform in ["x11", "osx", "windows", "server"]
supported_bits = env["bits"] == "64"
supported_arch = env["arch"] != "arm64"
if not env["tools"]:
return False

# Hack to disable on Linux arm64. This won't work well for cross-compilation (checks
# host, not target) and would need a more thorough fix by refactoring our arch and
# bits-handling code.
from platform import machine
if platform == "android":
return env["android_arch"] in ["arm64v8", "x86", "x86_64"]

if platform == "x11" and machine() != "x86_64":
supported_arch = False
if platform in ["javascript", "server"]:
return False

return env["tools"] and supported_platform and supported_bits and supported_arch
return True


def configure(env):
Expand Down
9 changes: 5 additions & 4 deletions modules/raycast/godot_update_embree.py
Expand Up @@ -74,17 +74,18 @@

os.chdir("../../thirdparty")

if os.path.exists("embree"):
shutil.rmtree("embree")
dir_name = "embree"
if os.path.exists(dir_name):
shutil.rmtree(dir_name)

subprocess.run(["git", "clone", "https://github.com/embree/embree.git", "embree-tmp"])
subprocess.run(["git", "clone", "https://github.com/lighttransport/embree-aarch64.git", "embree-tmp"])
os.chdir("embree-tmp")

commit_hash = str(subprocess.check_output(["git", "rev-parse", "HEAD"], universal_newlines=True)).strip()

dest_dir = "../embree"
all_files = set(cpp_files)

dest_dir = os.path.join("..", dir_name)
for include_dir in include_dirs:
headers = glob.iglob(os.path.join(include_dir, "*.h"))
all_files.update(headers)
Expand Down
7 changes: 5 additions & 2 deletions modules/raycast/lightmap_raycaster.cpp
Expand Up @@ -192,8 +192,11 @@ LightmapRaycasterEmbree::~LightmapRaycasterEmbree() {
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF);
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF);

if (embree_scene != nullptr)
if (embree_scene != nullptr) {
rtcReleaseScene(embree_scene);
if (embree_device != nullptr)
}

if (embree_device != nullptr) {
rtcReleaseDevice(embree_device);
}
}
14 changes: 7 additions & 7 deletions thirdparty/README.md
Expand Up @@ -41,19 +41,19 @@ Files extracted from upstream source:

## embree

- Upstream: https://github.com/embree/embree
- Version: 3.12.1 (69bd4c272f1ed608494f233ecfff3feec516880b, 2020)
- Upstream: https://github.com/lighttransport/embree-aarch64
- Version: 3.12.1 (6ef362f99af80c9dfe8dd2bfc582d9067897edc6, 2020)
- License: Apache 2.0

Files extracted from upstream:

- All cpp files listed in `modules/raytrace/godot_update_embree.py`
- All header files in the directories listed in `modules/raytrace/godot_update_embree.py`
- All cpp files listed in `modules/raycast/godot_update_embree.py`
- All header files in the directories listed in `modules/raycast/godot_update_embree.py`

The `modules/raytrace/godot_update_embree.py`script can be used to pull the
relevant files from the latest Embree release and apply some automatic changes.
The `modules/raycast/godot_update_embree.py`script can be used to pull the
relevant files from the latest Embree-aarch64 release and apply some automatic changes.

Some minor changes have been made in order to fix build errors.
Some changes have been made in order to remove exceptions and fix minor build errors.
They are marked with `// -- GODOT start --` and `// -- GODOT end --`
comments. Apply the patches in the `patches/` folder when syncing on newer upstream
commits.
Expand Down
95 changes: 84 additions & 11 deletions thirdparty/embree/common/algorithms/parallel_for.h
Expand Up @@ -8,6 +8,12 @@
#include "../math/math.h"
#include "../math/range.h"

#if defined(TASKING_GCD) && defined(BUILD_IOS)
#include <dispatch/dispatch.h>
#include <algorithm>
#include <type_traits>
#endif

namespace embree
{
/* parallel_for without range */
Expand All @@ -21,23 +27,50 @@ namespace embree
func(r.begin());
});
if (!TaskScheduler::wait())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --
}

#elif defined(TASKING_GCD) && defined(BUILD_IOS)

const size_t baselineNumBlocks = (TaskScheduler::threadCount() > 1)? TaskScheduler::threadCount() : 1;
const size_t length = N;
const size_t blockSize = (length + baselineNumBlocks-1) / baselineNumBlocks;
const size_t numBlocks = (length + blockSize-1) / blockSize;

dispatch_apply(numBlocks, DISPATCH_APPLY_AUTO, ^(size_t currentBlock) {

const size_t start = (currentBlock * blockSize);
const size_t blockLength = std::min(length - start, blockSize);
const size_t end = start + blockLength;

for(size_t i=start; i < end; i++)
{
func(i);
}
});

#elif defined(TASKING_TBB)
#if TBB_INTERFACE_VERSION >= 12002
tbb::task_group_context context;
tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
func(i);
},context);
if (context.is_group_execution_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --
#else
tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
func(i);
});
if (tbb::task::self().is_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --
#endif

#elif defined(TASKING_PPL)
Expand All @@ -57,7 +90,29 @@ namespace embree
#if defined(TASKING_INTERNAL)
TaskScheduler::spawn(first,last,minStepSize,func);
if (!TaskScheduler::wait())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --

#elif defined(TASKING_GCD) && defined(BUILD_IOS)

const size_t baselineNumBlocks = (TaskScheduler::threadCount() > 1)? 4*TaskScheduler::threadCount() : 1;
const size_t length = last - first;
const size_t blockSizeByThreads = (length + baselineNumBlocks-1) / baselineNumBlocks;
size_t blockSize = std::max<size_t>(minStepSize,blockSizeByThreads);
blockSize += blockSize % 4;

const size_t numBlocks = (length + blockSize-1) / blockSize;

dispatch_apply(numBlocks, DISPATCH_APPLY_AUTO, ^(size_t currentBlock) {

const size_t start = first + (currentBlock * blockSize);
const size_t end = std::min<size_t>(last, start + blockSize);

func( embree::range<Index>(start,end) );
});


#elif defined(TASKING_TBB)
#if TBB_INTERFACE_VERSION >= 12002
Expand All @@ -66,13 +121,19 @@ namespace embree
func(range<Index>(r.begin(),r.end()));
},context);
if (context.is_group_execution_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --
#else
tbb::parallel_for(tbb::blocked_range<Index>(first,last,minStepSize),[&](const tbb::blocked_range<Index>& r) {
func(range<Index>(r.begin(),r.end()));
});
if (tbb::task::self().is_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --
#endif

#elif defined(TASKING_PPL)
Expand Down Expand Up @@ -104,13 +165,19 @@ namespace embree
func(i);
},tbb::simple_partitioner(),context);
if (context.is_group_execution_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --
#else
tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
func(i);
},tbb::simple_partitioner());
if (tbb::task::self().is_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --
#endif
}

Expand All @@ -125,13 +192,19 @@ namespace embree
func(i);
},ap,context);
if (context.is_group_execution_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --
#else
tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
func(i);
},ap);
if (tbb::task::self().is_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// throw std::runtime_error("task cancelled");
abort();
// -- GODOT end --
#endif
}

Expand Down
14 changes: 9 additions & 5 deletions thirdparty/embree/common/algorithms/parallel_reduce.h
Expand Up @@ -43,7 +43,7 @@ namespace embree
template<typename Index, typename Value, typename Func, typename Reduction>
__forceinline Value parallel_reduce( const Index first, const Index last, const Index minStepSize, const Value& identity, const Func& func, const Reduction& reduction )
{
#if defined(TASKING_INTERNAL)
#if defined(TASKING_INTERNAL) || (defined(TASKING_GCD) && defined(BUILD_IOS))

/* fast path for small number of iterations */
Index taskCount = (last-first+minStepSize-1)/minStepSize;
Expand All @@ -58,15 +58,19 @@ namespace embree
const Value v = tbb::parallel_reduce(tbb::blocked_range<Index>(first,last,minStepSize),identity,
[&](const tbb::blocked_range<Index>& r, const Value& start) { return reduction(start,func(range<Index>(r.begin(),r.end()))); },
reduction,context);
if (context.is_group_execution_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// if (context.is_group_execution_cancelled())
// throw std::runtime_error("task cancelled");
// -- GODOT end --
return v;
#else
const Value v = tbb::parallel_reduce(tbb::blocked_range<Index>(first,last,minStepSize),identity,
[&](const tbb::blocked_range<Index>& r, const Value& start) { return reduction(start,func(range<Index>(r.begin(),r.end()))); },
reduction);
if (tbb::task::self().is_cancelled())
throw std::runtime_error("task cancelled");
// -- GODOT start --
// if (tbb::task::self().is_cancelled())
// throw std::runtime_error("task cancelled");
// -- GODOT end --
return v;
#endif
#else // TASKING_PPL
Expand Down
7 changes: 5 additions & 2 deletions thirdparty/embree/common/algorithms/parallel_sort.h
Expand Up @@ -5,6 +5,9 @@

#include "../simd/simd.h"
#include "parallel_for.h"
#if defined(TASKING_GCD) && defined(BUILD_IOS)
#include "../sys/alloc.h"
#endif
#include <algorithm>

namespace embree
Expand Down Expand Up @@ -320,7 +323,7 @@ namespace embree
#pragma nounroll
#endif
for (size_t i=startID; i<endID; i++) {
#if defined(__X86_64__)
#if defined(__X86_64__) || defined(__aarch64__)
const size_t index = ((size_t)(Key)src[i] >> (size_t)shift) & (size_t)mask;
#else
const Key index = ((Key)src[i] >> shift) & mask;
Expand Down Expand Up @@ -382,7 +385,7 @@ namespace embree
#endif
for (size_t i=startID; i<endID; i++) {
const Ty elt = src[i];
#if defined(__X86_64__)
#if defined(__X86_64__) || defined(__aarch64__)
const size_t index = ((size_t)(Key)src[i] >> (size_t)shift) & (size_t)mask;
#else
const size_t index = ((Key)src[i] >> shift) & mask;
Expand Down

0 comments on commit 1cfed0d

Please sign in to comment.