From e8f8bf4b03bf4801bcedbc5b6cf37d510b9c14fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E6=88=98?= <40414978+PatriceJiang@users.noreply.github.com> Date: Wed, 16 Jun 2021 10:36:24 +0800 Subject: [PATCH] 3.3 add simple jobsystem (#3609) --- native/CMakeLists.txt | 15 +- native/cocos/base/job-system/JobSystem.h | 24 ++- .../job-system-dummy/DummyJobGraph.cpp | 148 ++++++++++++++++ .../job-system-dummy/DummyJobGraph.h | 163 ++++++++++++++++++ .../job-system-dummy/DummyJobSystem.cpp | 35 ++++ .../job-system-dummy/DummyJobSystem.h | 63 +++++++ 6 files changed, 439 insertions(+), 9 deletions(-) create mode 100644 native/cocos/base/job-system/job-system-dummy/DummyJobGraph.cpp create mode 100644 native/cocos/base/job-system/job-system-dummy/DummyJobGraph.h create mode 100644 native/cocos/base/job-system/job-system-dummy/DummyJobSystem.cpp create mode 100644 native/cocos/base/job-system/job-system-dummy/DummyJobSystem.h diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index ec5b82e601..d796953bee 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -208,17 +208,23 @@ if(USE_DRAGONBONES OR USE_SPINE) set(USE_MIDDLEWARE ON) endif() -if("${USE_JOB_SYSTEM_TBB}" STREQUAL "${USE_JOB_SYSTEM_TASKFLOW}") +if(USE_JOB_SYSTEM_TASKFLOW AND USE_JOB_SYSTEM_TBB) set(USE_JOB_SYSTEM_TASKFLOW ON) set(USE_JOB_SYSTEM_TBB OFF) endif() +# if(USE_JOB_SYSTEM_TASKFLOW) +# set(CMAKE_CXX_STANDARD 17) +# set(TARGET_IOS_VERSION "12.0" CACHE STRING "Target iOS version" FORCE) +# endif() + ################################# list all option values ############################## inspect_values( CC_USE_METAL CC_USE_GLES3 CC_USE_GLES2 + CC_USE_VULKAN USE_SE_V8 USE_V8_DEBUGGER USE_SOCKET @@ -486,6 +492,13 @@ elseif(USE_JOB_SYSTEM_TBB) cocos/base/job-system/job-system-tbb/TBBJobSystem.h cocos/base/job-system/job-system-tbb/TBBJobSystem.cpp ) +else() + cocos_source_files( + cocos/base/job-system/job-system-dummy/DummyJobGraph.h + cocos/base/job-system/job-system-dummy/DummyJobGraph.cpp + cocos/base/job-system/job-system-dummy/DummyJobSystem.h + cocos/base/job-system/job-system-dummy/DummyJobSystem.cpp + ) endif() ##### math diff --git a/native/cocos/base/job-system/JobSystem.h b/native/cocos/base/job-system/JobSystem.h index 5f08f24419..a66c172ed0 100644 --- a/native/cocos/base/job-system/JobSystem.h +++ b/native/cocos/base/job-system/JobSystem.h @@ -24,19 +24,27 @@ ****************************************************************************/ #if CC_JOB_SYSTEM == CC_JOB_SYSTEM_TASKFLOW -#include "job-system-taskflow/TFJobGraph.h" -#include "job-system-taskflow/TFJobSystem.h" + #include "job-system-taskflow/TFJobGraph.h" + #include "job-system-taskflow/TFJobSystem.h" namespace cc { -using JobToken = TFJobToken; -using JobGraph = TFJobGraph; +using JobToken = TFJobToken; +using JobGraph = TFJobGraph; using JobSystem = TFJobSystem; } // namespace cc #elif CC_JOB_SYSTEM == CC_JOB_SYSTEM_TBB -#include "job-system-tbb/TBBJobGraph.h" -#include "job-system-tbb/TBBJobSystem.h" + #include "job-system-tbb/TBBJobGraph.h" + #include "job-system-tbb/TBBJobSystem.h" namespace cc { -using JobToken = TBBJobToken; -using JobGraph = TBBJobGraph; +using JobToken = TBBJobToken; +using JobGraph = TBBJobGraph; using JobSystem = TBBJobSystem; } // namespace cc +#else + #include "job-system-dummy/DummyJobGraph.h" + #include "job-system-dummy/DummyJobSystem.h" +namespace cc { +using JobToken = DummyJobToken; +using JobGraph = DummyJobGraph; +using JobSystem = DummyJobSystem; +} // namespace cc #endif diff --git a/native/cocos/base/job-system/job-system-dummy/DummyJobGraph.cpp b/native/cocos/base/job-system/job-system-dummy/DummyJobGraph.cpp new file mode 100644 index 0000000000..fd30a4aeb7 --- /dev/null +++ b/native/cocos/base/job-system/job-system-dummy/DummyJobGraph.cpp @@ -0,0 +1,148 @@ +/**************************************************************************** + Copyright (c) 2020-2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include "base/CoreStd.h" + +#include "DummyJobGraph.h" + +#define DUMMY_GRAPH_NODE_CHUNK_SIZE 64 + +namespace cc { + +namespace { +DummyGraphNode * freeList{nullptr}; +std::vector allocatedChunks; +} // namespace + +DummyGraphNode::~DummyGraphNode() { + delete _callback; +} + +void DummyGraphNode::reset() { + _successors.clear(); + _predecessors.clear(); + delete _callback; + _callback = nullptr; +} + +void DummyGraphNode::succeed(DummyGraphNode *other) { + // Run after other + this->_predecessors.emplace(other); + other->_successors.emplace(this); +} + +void DummyGraphNode::precede(DummyGraphNode *other) { + // Run before other + other->succeed(this); +} + +void DummyGraphNode::allocChunk() { + assert(freeList == nullptr); + freeList = new (std::nothrow) DummyGraphNode[DUMMY_GRAPH_NODE_CHUNK_SIZE](); + allocatedChunks.emplace_back(freeList); + for (auto i = 0; i < DUMMY_GRAPH_NODE_CHUNK_SIZE - 1; i++) { + freeList[i]._next = &freeList[i + 1]; + } + freeList[DUMMY_GRAPH_NODE_CHUNK_SIZE - 1]._next = nullptr; +} + +DummyGraphNode *DummyGraphNode::alloc() { + if (freeList == nullptr) { + DummyGraphNode::allocChunk(); + } + auto *p = freeList; + freeList = freeList->_next; + p->reset(); + return p; +} + +void DummyGraphNode::free(DummyGraphNode *node) { + node->_next = freeList; + freeList = node; +} + +void DummyGraphNode::freeAll() { + for (auto *chunk : allocatedChunks) { + delete[] chunk; + } + allocatedChunks.clear(); +} + +DummyGraph::~DummyGraph() { + clear(); +} + +void DummyGraph::clear() { + for (auto *node : _nodes) { + DummyGraphNode::free(node); + } + _nodes.clear(); +} + +void DummyGraph::link(size_t precede, size_t after) { + _nodes[precede]->precede(_nodes[after]); +} + +void DummyGraph::run() { + for (auto *node : _nodes) { + if (!excuted(node)) { + walk(node); + } + } + _generation++; +} + +void DummyGraph::walk(DummyGraphNode *node) { //NOLINT(misc-no-recursion) + for (DummyGraphNode *n : node->_predecessors) { + if (!excuted(n)) { + walk(n); + } + } + if (!excuted(node)) { + node->_callback->execute(); + node->_generation++; + } + + for (DummyGraphNode *n : node->_successors) { + if (!excuted(n)) { + walk(n); + } + } +} + +bool DummyGraph::excuted(DummyGraphNode *n) const { + return n->_generation != _generation; +} + +void DummyJobGraph::makeEdge(uint j1, uint j2) { + _dummyGraph.link(j1, j2); +} + +void DummyJobGraph::run() noexcept { + _dummyGraph.run(); + _dummyGraph.clear(); +} + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-dummy/DummyJobGraph.h b/native/cocos/base/job-system/job-system-dummy/DummyJobGraph.h new file mode 100644 index 0000000000..f89f5e1da7 --- /dev/null +++ b/native/cocos/base/job-system/job-system-dummy/DummyJobGraph.h @@ -0,0 +1,163 @@ +/**************************************************************************** + Copyright (c) 2020-2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include +#include +#include "cocos/base/Macros.h" +#include "cocos/base/TypeDef.h" + +namespace cc { + +class DummyJobSystem; + +class DummyGraphNode; +class DummyGraph final { +public: + DummyGraph() = default; + DummyGraph(const DummyGraph &) = delete; + DummyGraph(DummyGraph &&) = delete; + DummyGraph &operator=(const DummyGraph &) = delete; + DummyGraph &operator=(DummyGraph &&) = delete; + ~DummyGraph(); + + template + size_t addNode(Fn &&fn); + + void run(); + void link(size_t precede, size_t after); + void walk(DummyGraphNode *node); + void clear(); + +private: + bool excuted(DummyGraphNode *n) const; + + int _generation{0}; + std::vector _nodes; +}; + +class DummyGraphNodeTaskItf { +public: + virtual ~DummyGraphNodeTaskItf() = default; + virtual void execute() = 0; +}; + +template +class DummyGraphNodeTaskImpl final : public DummyGraphNodeTaskItf { +public: + explicit DummyGraphNodeTaskImpl(Fn &&t) noexcept; + DummyGraphNodeTaskImpl(const DummyGraphNodeTaskImpl &) = delete; + DummyGraphNodeTaskImpl(DummyGraphNodeTaskImpl &&) = delete; + DummyGraphNodeTaskImpl &operator=(const DummyGraphNodeTaskImpl &) = delete; + DummyGraphNodeTaskImpl &operator=(DummyGraphNodeTaskImpl &&) = delete; + ~DummyGraphNodeTaskImpl() override = default; + CC_INLINE void execute() override { _task(); } + +private: + Fn _task; +}; + +class DummyGraphNode final { +public: + DummyGraphNode() = default; + DummyGraphNode(const DummyGraphNode &) = delete; + DummyGraphNode(DummyGraphNode &&) = delete; + DummyGraphNode &operator=(const DummyGraphNode &) = delete; + DummyGraphNode &operator=(DummyGraphNode &&) = delete; + ~DummyGraphNode(); + +private: + static void allocChunk(); + static DummyGraphNode *alloc(); + static void free(DummyGraphNode *n); + static void freeAll(); + + void succeed(DummyGraphNode *other); + void precede(DummyGraphNode *other); + void reset(); + + DummyGraphNodeTaskItf * _callback{nullptr}; + std::unordered_set _successors{}; + std::unordered_set _predecessors{}; + DummyGraphNode * _next{nullptr}; + int _generation{0}; + friend class DummyGraph; +}; + +template +DummyGraphNodeTaskImpl::DummyGraphNodeTaskImpl(Fn &&t) noexcept : _task(t) {} + +template +size_t DummyGraph::addNode(Fn &&fn) { + DummyGraphNode *n = DummyGraphNode::alloc(); + n->_callback = new DummyGraphNodeTaskImpl(std::forward(fn)); + n->_generation = _generation; + _nodes.emplace_back(n); + return _nodes.size() - 1; +} + +// exported declarations + +class DummyJobGraph final { +public: + explicit DummyJobGraph(DummyJobSystem * /*system*/) noexcept {} + DummyJobGraph(const DummyJobGraph &) = delete; + DummyJobGraph(DummyJobGraph &&) = delete; + DummyJobGraph &operator=(const DummyJobGraph &) = delete; + DummyJobGraph &operator=(DummyJobGraph &&) = delete; + ~DummyJobGraph() noexcept = default; + + template + uint createJob(Function &&func) noexcept; + + template + uint createForEachIndexJob(uint begin, uint end, uint step, Function &&func) noexcept; + + void makeEdge(uint j1, uint j2); + + void run() noexcept; + + CC_INLINE void waitForAll() { run(); } + +private: + DummyGraph _dummyGraph{}; +}; + +template +uint DummyJobGraph::createJob(Function &&func) noexcept { + return static_cast(_dummyGraph.addNode(std::forward(func))); +} + +template +uint DummyJobGraph::createForEachIndexJob(uint begin, uint end, uint step, Function &&func) noexcept { + return static_cast(_dummyGraph.addNode([callable = std::forward(func), first = begin, last = end, step = step]() { + for (auto i = first; i < last; i += step) { + callable(i); + } + })); +} + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-dummy/DummyJobSystem.cpp b/native/cocos/base/job-system/job-system-dummy/DummyJobSystem.cpp new file mode 100644 index 0000000000..d4e594ba59 --- /dev/null +++ b/native/cocos/base/job-system/job-system-dummy/DummyJobSystem.cpp @@ -0,0 +1,35 @@ +/**************************************************************************** + Copyright (c) 2020-2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include "base/CoreStd.h" + +#include "DummyJobGraph.h" +#include "DummyJobSystem.h" + +namespace cc { + +DummyJobSystem *DummyJobSystem::instance = nullptr; + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-dummy/DummyJobSystem.h b/native/cocos/base/job-system/job-system-dummy/DummyJobSystem.h new file mode 100644 index 0000000000..db4c6b6561 --- /dev/null +++ b/native/cocos/base/job-system/job-system-dummy/DummyJobSystem.h @@ -0,0 +1,63 @@ +/**************************************************************************** + Copyright (c) 2020-2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "cocos/base/Macros.h" +#include "cocos/base/TypeDef.h" + +namespace cc { + +using DummyJobToken = size_t; + +class DummyJobGraph; + +class DummyJobSystem final { +private: + static DummyJobSystem *instance; + +public: + static DummyJobSystem *getInstance() { + if (!instance) { + instance = new DummyJobSystem; + } + return instance; + } + + static void destroyInstance() { + delete instance; + instance = nullptr; + } + + DummyJobSystem() noexcept = default; + explicit DummyJobSystem(uint /*threadCount*/) noexcept {} + + CC_INLINE uint threadCount() const { return THREAD_COUNT; } //NOLINT + +private: + static constexpr uint THREAD_COUNT = 1U; //always one +}; + +} // namespace cc