From f4bc1e096ffb48b7faf99353730e4443c3e1ec5c Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Tue, 8 Dec 2020 22:39:17 +0800 Subject: [PATCH] structure --- native/cocos/base/job-system/CMakeLists.txt | 2 + native/cocos/base/job-system/JobSystem.h | 20 ++++++ .../job-system-taskflow/CMakeLists.txt | 53 +++++++++++++++ .../job-system/job-system-taskflow/TFJob.cpp | 21 ++++++ .../job-system/job-system-taskflow/TFJob.h | 24 +++++++ .../job-system-taskflow/TFJobGraph.cpp | 7 ++ .../job-system-taskflow/TFJobGraph.h | 42 ++++++++++++ .../job-system-taskflow/TFJobSystem.cpp | 33 ++++++++++ .../job-system-taskflow/TFJobSystem.h | 32 ++++++++++ .../job-system/job-system-tbb/CMakeLists.txt | 53 +++++++++++++++ .../base/job-system/job-system-tbb/TBBJob.cpp | 3 + .../base/job-system/job-system-tbb/TBBJob.h | 21 ++++++ .../job-system/job-system-tbb/TBBJobGraph.cpp | 3 + .../job-system/job-system-tbb/TBBJobGraph.h | 24 +++++++ .../job-system-tbb/TBBJobSystem.cpp | 3 + .../job-system/job-system-tbb/TBBJobSystem.h | 21 ++++++ native/cocos/base/threading/CMakeLists.txt | 52 +++++++++++++++ .../threading}/CommandEncoder.cpp | 2 - .../threading}/CommandEncoder.h | 2 - .../threading}/ConditionVariable.cpp | 4 +- .../cocos/base/threading/ConditionVariable.h | 35 ++++++++++ .../core/thread => base/threading}/Event.h | 2 - .../thread => base/threading}/Semaphore.cpp | 4 ++ .../thread => base/threading}/Semaphore.h | 4 ++ .../thread => base/threading}/ThreadPool.cpp | 31 +++++---- .../thread => base/threading}/ThreadPool.h | 18 +++--- .../threading}/ThreadSafeCounter.h | 4 +- native/cocos/cocos2d.h | 3 +- native/cocos/renderer/core/CMakeLists.txt | 3 +- native/cocos/renderer/core/Core.h | 9 ++- .../core/gfx-proxy/GFXBufferProxy.cpp | 2 +- .../core/gfx-proxy/GFXCommandBufferProxy.cpp | 13 +++- .../core/gfx-proxy/GFXCommandBufferProxy.h | 7 +- .../gfx-proxy/GFXDescriptorSetLayoutProxy.cpp | 2 +- .../core/gfx-proxy/GFXDescriptorSetProxy.cpp | 2 +- .../core/gfx-proxy/GFXDeviceProxy.cpp | 34 ++-------- .../renderer/core/gfx-proxy/GFXDeviceProxy.h | 9 ++- .../core/gfx-proxy/GFXFramebufferProxy.cpp | 2 +- .../core/gfx-proxy/GFXInputAssemblerProxy.cpp | 2 +- .../core/gfx-proxy/GFXPipelineLayoutProxy.cpp | 2 +- .../core/gfx-proxy/GFXPipelineStateProxy.cpp | 2 +- .../renderer/core/gfx-proxy/GFXQueueProxy.cpp | 27 ++++++-- .../core/gfx-proxy/GFXRenderPassProxy.cpp | 2 +- .../core/gfx-proxy/GFXSamplerProxy.cpp | 2 +- .../core/gfx-proxy/GFXShaderProxy.cpp | 2 +- .../core/gfx-proxy/GFXTextureProxy.cpp | 2 +- native/cocos/renderer/core/gfx/GFXDevice.h | 2 + .../renderer/core/thread/ConditionVariable.h | 25 -------- .../renderer/core/thread/ReadWriteLock.cpp | 1 - .../renderer/core/thread/ReadWriteLock.h | 38 ----------- .../cocos/renderer/gfx-gles2/GLES2Context.cpp | 64 ++++++++++--------- native/cocos/renderer/gfx-gles2/GLES2Std.h | 11 +++- .../cocos/renderer/gfx-gles3/GLES3Context.cpp | 10 +-- native/cocos/renderer/gfx-gles3/GLES3Std.h | 11 +++- native/cocos/renderer/gfx-vulkan/VKBuffer.cpp | 6 +- .../cocos/renderer/gfx-vulkan/VKCommands.cpp | 13 ++-- native/cocos/renderer/gfx-vulkan/VKDevice.cpp | 10 +-- 57 files changed, 631 insertions(+), 207 deletions(-) create mode 100644 native/cocos/base/job-system/CMakeLists.txt create mode 100644 native/cocos/base/job-system/JobSystem.h create mode 100644 native/cocos/base/job-system/job-system-taskflow/CMakeLists.txt create mode 100644 native/cocos/base/job-system/job-system-taskflow/TFJob.cpp create mode 100644 native/cocos/base/job-system/job-system-taskflow/TFJob.h create mode 100644 native/cocos/base/job-system/job-system-taskflow/TFJobGraph.cpp create mode 100644 native/cocos/base/job-system/job-system-taskflow/TFJobGraph.h create mode 100644 native/cocos/base/job-system/job-system-taskflow/TFJobSystem.cpp create mode 100644 native/cocos/base/job-system/job-system-taskflow/TFJobSystem.h create mode 100644 native/cocos/base/job-system/job-system-tbb/CMakeLists.txt create mode 100644 native/cocos/base/job-system/job-system-tbb/TBBJob.cpp create mode 100644 native/cocos/base/job-system/job-system-tbb/TBBJob.h create mode 100644 native/cocos/base/job-system/job-system-tbb/TBBJobGraph.cpp create mode 100644 native/cocos/base/job-system/job-system-tbb/TBBJobGraph.h create mode 100644 native/cocos/base/job-system/job-system-tbb/TBBJobSystem.cpp create mode 100644 native/cocos/base/job-system/job-system-tbb/TBBJobSystem.h create mode 100644 native/cocos/base/threading/CMakeLists.txt rename native/cocos/{renderer/core/thread => base/threading}/CommandEncoder.cpp (99%) rename native/cocos/{renderer/core/thread => base/threading}/CommandEncoder.h (99%) rename native/cocos/{renderer/core/thread => base/threading}/ConditionVariable.cpp (77%) create mode 100644 native/cocos/base/threading/ConditionVariable.h rename native/cocos/{renderer/core/thread => base/threading}/Event.h (93%) rename native/cocos/{renderer/core/thread => base/threading}/Semaphore.cpp (89%) rename native/cocos/{renderer/core/thread => base/threading}/Semaphore.h (93%) rename native/cocos/{renderer/core/thread => base/threading}/ThreadPool.cpp (64%) rename native/cocos/{renderer/core/thread => base/threading}/ThreadPool.h (80%) rename native/cocos/{renderer/core/thread => base/threading}/ThreadSafeCounter.h (84%) delete mode 100644 native/cocos/renderer/core/thread/ConditionVariable.h delete mode 100644 native/cocos/renderer/core/thread/ReadWriteLock.cpp delete mode 100644 native/cocos/renderer/core/thread/ReadWriteLock.h diff --git a/native/cocos/base/job-system/CMakeLists.txt b/native/cocos/base/job-system/CMakeLists.txt new file mode 100644 index 0000000000..c3bb0d7c47 --- /dev/null +++ b/native/cocos/base/job-system/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(job-system-taskflow) +add_subdirectory(job-system-tbb) diff --git a/native/cocos/base/job-system/JobSystem.h b/native/cocos/base/job-system/JobSystem.h new file mode 100644 index 0000000000..6a7ceb6ab7 --- /dev/null +++ b/native/cocos/base/job-system/JobSystem.h @@ -0,0 +1,20 @@ + +#if CC_JOB_SYSTEM == CC_JOB_SYSTEM_TASKFLOW +#include "job-system-taskflow/TFJob.h" +#include "job-system-taskflow/TFJobGraph.h" +#include "job-system-taskflow/TFJobSystem.h" +namespace cc { +using Job = TFJob; +using JobGraph = TFJobGraph; +using JobSystem = TFJobSystem; +} // namespace cc +#elif CC_JOB_SYSTEM == CC_JOB_SYSTEM_TBB +#include "job-system-tbb/TBBJob.h" +#include "job-system-tbb/TBBJobGraph.h" +#include "job-system-tbb/TBBJobSystem.h" +namespace cc { +using Job = TBBJob; +using JobGraph = TBBJobGraph; +using JobSystem = TBBJobSystem; +} // namespace cc +#endif diff --git a/native/cocos/base/job-system/job-system-taskflow/CMakeLists.txt b/native/cocos/base/job-system/job-system-taskflow/CMakeLists.txt new file mode 100644 index 0000000000..52202af71c --- /dev/null +++ b/native/cocos/base/job-system/job-system-taskflow/CMakeLists.txt @@ -0,0 +1,53 @@ +set(TARGET_NAME TFJobSystem) +set(CMAKE_CXX_STANDARD 17) +message(STATUS "Target: ${TARGET_NAME}...") +# --------------------------------------------------------------------- +# set include files + +set(INCLUDE_FILES + ${GFX_EXTERNAL_PATH}/taskflow + ${COCOS_SRC_PATH}/renderer/core +) + +include_directories( ${INCLUDE_FILES} ) + +# --------------------------------------------------------------------- + +file(GLOB_RECURSE HEADER_FILES *.h) +file(GLOB_RECURSE SOURCE_CPP_FILES *.cpp *.cc) +file(GLOB_RECURSE SOURCE_C_FILES *.c) + +if(COCOS_PLATFORM_IOS OR COCOS_PLATFORM_OSX) + file(GLOB_RECURSE SOURCE_MM_FILES *.m *.mm) +endif() + +set(ALL_FILES + ${HEADER_FILES} + ${SOURCE_CPP_FILES} + ${SOURCE_C_FILES} +) + +if(COCOS_PLATFORM_IOS OR COCOS_PLATFORM_OSX) + set(ALL_FILES ${ALL_FILES} ${SOURCE_MM_FILES}) +endif() + +if(COCOS_UNICODE) + add_definitions("-DUNICODE -D_UNICODE") +endif() + +if (COCOS_STATIC) + add_library(${TARGET_NAME} STATIC ${HEADER_FILES} ${ALL_FILES}) +else() + add_library(${TARGET_NAME} SHARED ${HEADER_FILES} ${ALL_FILES}) +endif() + +target_link_libraries(${TARGET_NAME} Core) + +set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME CCTFJobSystem) +set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME_DEBUG CCTFJobSystem_d) + +message(STATUS "${TARGET_NAME} configuration completed.") + +target_compile_definitions(${TARGET_NAME} PUBLIC + $<$:CC_DEBUG=1> +) diff --git a/native/cocos/base/job-system/job-system-taskflow/TFJob.cpp b/native/cocos/base/job-system/job-system-taskflow/TFJob.cpp new file mode 100644 index 0000000000..621e32e526 --- /dev/null +++ b/native/cocos/base/job-system/job-system-taskflow/TFJob.cpp @@ -0,0 +1,21 @@ +#include "CoreStd.h" + +#include "TFJob.h" + +namespace cc { + +TFJob::TFJob(tf::Task &&task) noexcept +: _task(task) +{ + +} + +void TFJob::precede(TFJob &other) noexcept { + _task.precede(other._task); +} + +void TFJob::succeed(TFJob &other) noexcept { + _task.succeed(other._task); +} + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-taskflow/TFJob.h b/native/cocos/base/job-system/job-system-taskflow/TFJob.h new file mode 100644 index 0000000000..94012ef0ad --- /dev/null +++ b/native/cocos/base/job-system/job-system-taskflow/TFJob.h @@ -0,0 +1,24 @@ +#pragma once + +#include "taskflow/taskflow.hpp" + +class TFJobGraph; + +namespace cc { + +class TFJob final { +public: + + void precede(TFJob &other) noexcept; + void succeed(TFJob &other) noexcept; + +private: + + TFJob(tf::Task &&task) noexcept; + + friend class TFJobGraph; + + tf::Task _task; +}; + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-taskflow/TFJobGraph.cpp b/native/cocos/base/job-system/job-system-taskflow/TFJobGraph.cpp new file mode 100644 index 0000000000..def056bd3c --- /dev/null +++ b/native/cocos/base/job-system/job-system-taskflow/TFJobGraph.cpp @@ -0,0 +1,7 @@ +#include "CoreStd.h" + +#include "TFJobGraph.h" + +namespace cc { + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-taskflow/TFJobGraph.h b/native/cocos/base/job-system/job-system-taskflow/TFJobGraph.h new file mode 100644 index 0000000000..d7f7d906e3 --- /dev/null +++ b/native/cocos/base/job-system/job-system-taskflow/TFJobGraph.h @@ -0,0 +1,42 @@ +#pragma once + +#include "taskflow/taskflow.hpp" +#include "TFJob.h" + +namespace cc { + +class TFJobGraph final { +public: + + template + TFJob createJob(Function &&func) noexcept; + + template + TFJob createForEachIndexJob(B &&begin, E &&end, S &&step, Function &&func) noexcept; + + template + TFJob createReduceJob(B &&begin, E &&end, T &acc, Function &&func) noexcept; + +private: + + friend class TFJobSystem; + + tf::Taskflow _flow; +}; + +template +TFJob TFJobGraph::createJob(Function &&func) noexcept { + return TFJob(_flow.emplace(func)); +} + +template +TFJob TFJobGraph::createForEachIndexJob(B &&begin, E &&end, S &&step, Function &&func) noexcept { + return TFJob(_flow.for_each_index(begin, end, step, func)); +} + +template +TFJob TFJobGraph::createReduceJob(B &&begin, E &&end, T &acc, Function &&func) noexcept { + return TFJob(_flow.reduce(begin, end, acc, func)); +} + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-taskflow/TFJobSystem.cpp b/native/cocos/base/job-system/job-system-taskflow/TFJobSystem.cpp new file mode 100644 index 0000000000..7eb70de7af --- /dev/null +++ b/native/cocos/base/job-system/job-system-taskflow/TFJobSystem.cpp @@ -0,0 +1,33 @@ +#include "CoreStd.h" + +#include "TFJobSystem.h" +#include "TFJobGraph.h" + +namespace cc { + +TFJobSystem TFJobSystem::_instance; + +TFJobSystem::TFJobSystem(uint threadCount) noexcept +: _executor(threadCount) +{ + _nextHandle.Set(1 << 10); // start from non-zero +} + +uint TFJobSystem::run(TFJobGraph &g) noexcept { + uint nextHandle = _nextHandle.Increment(); + _futures[nextHandle] = _executor.run(g._flow); + return nextHandle; +} + +void TFJobSystem::wait(uint handle) noexcept { + if (_futures.count(handle) == 0) return; + _futures[handle].wait(); + _futures.erase(handle); +} + +void TFJobSystem::waitForAll() noexcept { + _executor.wait_for_all(); + _futures.clear(); +} + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-taskflow/TFJobSystem.h b/native/cocos/base/job-system/job-system-taskflow/TFJobSystem.h new file mode 100644 index 0000000000..d3da9828a0 --- /dev/null +++ b/native/cocos/base/job-system/job-system-taskflow/TFJobSystem.h @@ -0,0 +1,32 @@ +#pragma once + +#include "taskflow/taskflow.hpp" +#include "threading/ThreadSafeCounter.h" + +namespace cc { + +class TFJobGraph; + +class TFJobSystem final { +public: + + static TFJobSystem &getInstance() { return _instance; } + + TFJobSystem() noexcept : TFJobSystem(std::thread::hardware_concurrency() - 1) {} + TFJobSystem(uint threadCount) noexcept; + + uint run(TFJobGraph &g) noexcept; + void wait(uint handle) noexcept; + void waitForAll() noexcept; + + CC_INLINE uint threadCount() { return _executor.num_workers(); } + +private: + static TFJobSystem _instance; + + tf::Executor _executor; + map> _futures; + ThreadSafeCounter _nextHandle; +}; + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-tbb/CMakeLists.txt b/native/cocos/base/job-system/job-system-tbb/CMakeLists.txt new file mode 100644 index 0000000000..8ce84d9596 --- /dev/null +++ b/native/cocos/base/job-system/job-system-tbb/CMakeLists.txt @@ -0,0 +1,53 @@ +set(TARGET_NAME TBBJobSystem) + +message(STATUS "Target: ${TARGET_NAME}...") +# --------------------------------------------------------------------- +# set include files + +set(INCLUDE_FILES + ${GFX_EXTERNAL_PATH}/tbb + ${COCOS_SRC_PATH}/renderer/core +) + +include_directories( ${INCLUDE_FILES} ) + +# --------------------------------------------------------------------- + +file(GLOB_RECURSE HEADER_FILES *.h) +file(GLOB_RECURSE SOURCE_CPP_FILES *.cpp *.cc) +file(GLOB_RECURSE SOURCE_C_FILES *.c) + +if(COCOS_PLATFORM_IOS OR COCOS_PLATFORM_OSX) + file(GLOB_RECURSE SOURCE_MM_FILES *.m *.mm) +endif() + +set(ALL_FILES + ${HEADER_FILES} + ${SOURCE_CPP_FILES} + ${SOURCE_C_FILES} +) + +if(COCOS_PLATFORM_IOS OR COCOS_PLATFORM_OSX) + set(ALL_FILES ${ALL_FILES} ${SOURCE_MM_FILES}) +endif() + +if(COCOS_UNICODE) + add_definitions("-DUNICODE -D_UNICODE") +endif() + +if (COCOS_STATIC) + add_library(${TARGET_NAME} STATIC ${HEADER_FILES} ${ALL_FILES}) +else() + add_library(${TARGET_NAME} SHARED ${HEADER_FILES} ${ALL_FILES}) +endif() + +target_link_libraries(${TARGET_NAME} Core) + +set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME CCTBBJobSystem) +set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME_DEBUG CCTBBJobSystem_d) + +message(STATUS "${TARGET_NAME} configuration completed.") + +target_compile_definitions(${TARGET_NAME} PUBLIC + $<$:CC_DEBUG=1> +) diff --git a/native/cocos/base/job-system/job-system-tbb/TBBJob.cpp b/native/cocos/base/job-system/job-system-tbb/TBBJob.cpp new file mode 100644 index 0000000000..c847a100d4 --- /dev/null +++ b/native/cocos/base/job-system/job-system-tbb/TBBJob.cpp @@ -0,0 +1,3 @@ +#include "CoreStd.h" + +#include "Job.h" diff --git a/native/cocos/base/job-system/job-system-tbb/TBBJob.h b/native/cocos/base/job-system/job-system-tbb/TBBJob.h new file mode 100644 index 0000000000..0f2e6f41a8 --- /dev/null +++ b/native/cocos/base/job-system/job-system-tbb/TBBJob.h @@ -0,0 +1,21 @@ +#pragma once + +namespace cc { + +class Job { +public: + + Job() noexcept {} + ~Job() = default; + Job(Job const&) = delete; + Job(Job&&) = delete; + Job& operator=(Job const&) = delete; + Job& operator=(Job&&) = delete; + + virtual void precede(Job *other) noexcept = 0; + +private: + +}; + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-tbb/TBBJobGraph.cpp b/native/cocos/base/job-system/job-system-tbb/TBBJobGraph.cpp new file mode 100644 index 0000000000..43c76a7986 --- /dev/null +++ b/native/cocos/base/job-system/job-system-tbb/TBBJobGraph.cpp @@ -0,0 +1,3 @@ +#include "CoreStd.h" + +#include "JobGraph.h" diff --git a/native/cocos/base/job-system/job-system-tbb/TBBJobGraph.h b/native/cocos/base/job-system/job-system-tbb/TBBJobGraph.h new file mode 100644 index 0000000000..5f6b0e9a15 --- /dev/null +++ b/native/cocos/base/job-system/job-system-tbb/TBBJobGraph.h @@ -0,0 +1,24 @@ +#pragma once + +namespace cc { + +class Job; + +class JobGraph { +public: + + JobGraph() noexcept {} + ~JobGraph() = default; + JobGraph(JobGraph const&) = delete; + JobGraph(JobGraph&&) = delete; + JobGraph& operator=(JobGraph const&) = delete; + JobGraph& operator=(JobGraph&&) = delete; + + template + virtual Job *createJob(Function&& func, Args&&... args) noexcept = 0; + +private: + +}; + +} // namespace cc diff --git a/native/cocos/base/job-system/job-system-tbb/TBBJobSystem.cpp b/native/cocos/base/job-system/job-system-tbb/TBBJobSystem.cpp new file mode 100644 index 0000000000..b98c762142 --- /dev/null +++ b/native/cocos/base/job-system/job-system-tbb/TBBJobSystem.cpp @@ -0,0 +1,3 @@ +#include "CoreStd.h" + +#include "JobSystem.h" diff --git a/native/cocos/base/job-system/job-system-tbb/TBBJobSystem.h b/native/cocos/base/job-system/job-system-tbb/TBBJobSystem.h new file mode 100644 index 0000000000..b5f8b62736 --- /dev/null +++ b/native/cocos/base/job-system/job-system-tbb/TBBJobSystem.h @@ -0,0 +1,21 @@ +#pragma once + +namespace cc { + +class JobSystem { +public: + + JobSystem() noexcept {} + ~JobSystem() = default; + JobSystem(JobSystem const&) = delete; + JobSystem(JobSystem&&) = delete; + JobSystem& operator=(JobSystem const&) = delete; + JobSystem& operator=(JobSystem&&) = delete; + + void run(JobGraph *g) = 0; + +private: + +}; + +} // namespace cc diff --git a/native/cocos/base/threading/CMakeLists.txt b/native/cocos/base/threading/CMakeLists.txt new file mode 100644 index 0000000000..671bcac721 --- /dev/null +++ b/native/cocos/base/threading/CMakeLists.txt @@ -0,0 +1,52 @@ +set(TARGET_NAME Threading) + +message(STATUS "Target: ${TARGET_NAME}...") +# --------------------------------------------------------------------- +# set include files + +set(INCLUDE_FILES + ${GFX_EXTERNAL_PATH}/concurrentqueue +) + +include_directories( ${INCLUDE_FILES} ) + +# --------------------------------------------------------------------- + +file(GLOB_RECURSE HEADER_FILES *.h) +file(GLOB_RECURSE SOURCE_CPP_FILES *.cpp *.cc) +file(GLOB_RECURSE SOURCE_C_FILES *.c) + +if(COCOS_PLATFORM_IOS OR COCOS_PLATFORM_OSX) + file(GLOB_RECURSE SOURCE_MM_FILES *.m *.mm) +endif() + +set(ALL_FILES + ${HEADER_FILES} + ${SOURCE_CPP_FILES} + ${SOURCE_C_FILES} +) + +if(COCOS_PLATFORM_IOS OR COCOS_PLATFORM_OSX) + set(ALL_FILES ${ALL_FILES} ${SOURCE_MM_FILES}) +endif() + +if(COCOS_UNICODE) + add_definitions("-DUNICODE -D_UNICODE") +endif() + +if (COCOS_STATIC) + add_library(${TARGET_NAME} STATIC ${HEADER_FILES} ${ALL_FILES}) +else() + add_library(${TARGET_NAME} SHARED ${HEADER_FILES} ${ALL_FILES}) +endif() + +target_link_libraries(${TARGET_NAME} Core) + +set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME CCThreading) +set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME_DEBUG CCThreading_d) + +message(STATUS "${TARGET_NAME} configuration completed.") + +target_compile_definitions(${TARGET_NAME} PUBLIC + $<$:CC_DEBUG=1> +) diff --git a/native/cocos/renderer/core/thread/CommandEncoder.cpp b/native/cocos/base/threading/CommandEncoder.cpp similarity index 99% rename from native/cocos/renderer/core/thread/CommandEncoder.cpp rename to native/cocos/base/threading/CommandEncoder.cpp index 5cfaf09647..1b0b7168d5 100644 --- a/native/cocos/renderer/core/thread/CommandEncoder.cpp +++ b/native/cocos/base/threading/CommandEncoder.cpp @@ -2,7 +2,6 @@ #include namespace cc { -namespace gfx { namespace { @@ -314,5 +313,4 @@ char const* MemoryChunkSwitchCommand::GetName() const noexcept return "MemoryChunkSwitch"; } -} // namespace gfx } // namespace cc diff --git a/native/cocos/renderer/core/thread/CommandEncoder.h b/native/cocos/base/threading/CommandEncoder.h similarity index 99% rename from native/cocos/renderer/core/thread/CommandEncoder.h rename to native/cocos/base/threading/CommandEncoder.h index 7ebb4d8117..9bfe7ff037 100644 --- a/native/cocos/renderer/core/thread/CommandEncoder.h +++ b/native/cocos/base/threading/CommandEncoder.h @@ -5,7 +5,6 @@ #include "Event.h" namespace cc { -namespace gfx { // 由于系统的内存分配是从一个全局堆里申请 分配过程会上锁导致降低并行度 在多线程的环境下如果频繁的申请释放内存会降低性能 // 所以这里先加个接口来统一调用 未来会换成单独服务于线程的无锁分配器 @@ -624,5 +623,4 @@ T* CommandEncoder::AllocateAndZero(uint32_t const count) noexcept }; \ WRITE_COMMAND(CB, CommandName, (Value1, Value2, Value3, Value4, Value5, Value6, Value7, Value8)) }; -} // namespace gfx } // namespace cc diff --git a/native/cocos/renderer/core/thread/ConditionVariable.cpp b/native/cocos/base/threading/ConditionVariable.cpp similarity index 77% rename from native/cocos/renderer/core/thread/ConditionVariable.cpp rename to native/cocos/base/threading/ConditionVariable.cpp index aed7e0f168..2daf4d3bc8 100644 --- a/native/cocos/renderer/core/thread/ConditionVariable.cpp +++ b/native/cocos/base/threading/ConditionVariable.cpp @@ -1,7 +1,6 @@ #include "ConditionVariable.h" namespace cc { -namespace gfx { void ConditionVariable::Wait() noexcept { @@ -11,13 +10,14 @@ void ConditionVariable::Wait() noexcept void ConditionVariable::Signal() noexcept { + std::lock_guard lock(mMutex); mCV.notify_one(); } void ConditionVariable::SignalAll() noexcept { + std::lock_guard lock(mMutex); mCV.notify_all(); } -} // namespace gfx } // namespace cc diff --git a/native/cocos/base/threading/ConditionVariable.h b/native/cocos/base/threading/ConditionVariable.h new file mode 100644 index 0000000000..6a56074415 --- /dev/null +++ b/native/cocos/base/threading/ConditionVariable.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +namespace cc { + +class ConditionVariable final +{ +public: + + void Wait() noexcept; + template + void Wait(Function func, Args&&... args) noexcept; + void Signal() noexcept; + void SignalAll() noexcept; + +private: + + std::mutex mMutex; + std::condition_variable mCV; +}; + +template +void ConditionVariable::Wait(Function func, Args&&... args) noexcept +{ + // ******** 注意 ******** + // func内不要操作任何同步对象!!! + // 如果不确定 就不要调用这个函数!!! + std::unique_lock lock(mMutex); + mCV.wait(lock, std::bind(std::forward(func), std::forward(args)...)); +} + +} // namespace cc diff --git a/native/cocos/renderer/core/thread/Event.h b/native/cocos/base/threading/Event.h similarity index 93% rename from native/cocos/renderer/core/thread/Event.h rename to native/cocos/base/threading/Event.h index a0914d67aa..210792201d 100644 --- a/native/cocos/renderer/core/thread/Event.h +++ b/native/cocos/base/threading/Event.h @@ -4,7 +4,6 @@ #include "Semaphore.h" namespace cc { -namespace gfx { template class Event final @@ -23,5 +22,4 @@ private: using EventCV = Event; using EventSem = Event; -} // namespace gfx } // namespace cc diff --git a/native/cocos/renderer/core/thread/Semaphore.cpp b/native/cocos/base/threading/Semaphore.cpp similarity index 89% rename from native/cocos/renderer/core/thread/Semaphore.cpp rename to native/cocos/base/threading/Semaphore.cpp index c7ce93695f..69e236c71a 100644 --- a/native/cocos/renderer/core/thread/Semaphore.cpp +++ b/native/cocos/base/threading/Semaphore.cpp @@ -1,5 +1,7 @@ #include "Semaphore.h" +namespace cc { + Semaphore::Semaphore() noexcept : mSemaphore(0) { @@ -19,3 +21,5 @@ void Semaphore::Signal() noexcept { mSemaphore.signal(); } + +} // namespace cc diff --git a/native/cocos/renderer/core/thread/Semaphore.h b/native/cocos/base/threading/Semaphore.h similarity index 93% rename from native/cocos/renderer/core/thread/Semaphore.h rename to native/cocos/base/threading/Semaphore.h index dc980fb0a2..6d41299d9c 100644 --- a/native/cocos/renderer/core/thread/Semaphore.h +++ b/native/cocos/base/threading/Semaphore.h @@ -4,6 +4,8 @@ #include "concurrentqueue.h" #include "lightweightsemaphore.h" +namespace cc { + class Semaphore final { public: @@ -19,3 +21,5 @@ private: moodycamel::details::Semaphore mSemaphore; }; + +} // namespace cc diff --git a/native/cocos/renderer/core/thread/ThreadPool.cpp b/native/cocos/base/threading/ThreadPool.cpp similarity index 64% rename from native/cocos/renderer/core/thread/ThreadPool.cpp rename to native/cocos/base/threading/ThreadPool.cpp index dbae39bc85..35e87e578a 100644 --- a/native/cocos/renderer/core/thread/ThreadPool.cpp +++ b/native/cocos/base/threading/ThreadPool.cpp @@ -1,10 +1,9 @@ #include "ThreadPool.h" namespace cc { -namespace gfx { -uint32_t const ThreadPool::kCpuCoreCount = std::thread::hardware_concurrency(); -uint32_t const ThreadPool::kMaxThreadCount = kCpuCoreCount - 1; +uint8_t const ThreadPool::kCpuCoreCount = std::thread::hardware_concurrency(); +uint8_t const ThreadPool::kMaxThreadCount = kCpuCoreCount - 1; void ThreadPool::Start() noexcept { @@ -12,9 +11,9 @@ void ThreadPool::Start() noexcept { return; } - + mRunning = true; - + for (uint8_t i = 0; i < kMaxThreadCount; ++i) { AddThread(); @@ -27,10 +26,10 @@ void ThreadPool::Stop() noexcept { return; } - + mRunning = false; mEvent.SignalAll(); - + for (auto& worker : mWorkers) { if (worker.joinable()) @@ -38,7 +37,7 @@ void ThreadPool::Stop() noexcept worker.join(); } } - + mWorkers.clear(); } @@ -50,18 +49,24 @@ void ThreadPool::AddThread() noexcept { while (mRunning) { - if (! mTasks.consume_one([](Task const& task) + Task task = nullptr; + + if (mTasks.try_dequeue(task)) { task(); - })) + } + else { - mEvent.Wait(); + // Double Check + mEvent.Wait([this]() + { + return mTasks.size_approx() != 0; + }); } } }; - + mWorkers.emplace_back(workerLoop); } -} // namespace gfx } // namespace cc diff --git a/native/cocos/renderer/core/thread/ThreadPool.h b/native/cocos/base/threading/ThreadPool.h similarity index 80% rename from native/cocos/renderer/core/thread/ThreadPool.h rename to native/cocos/base/threading/ThreadPool.h index f6edc56e78..4bd852a867 100644 --- a/native/cocos/renderer/core/thread/ThreadPool.h +++ b/native/cocos/base/threading/ThreadPool.h @@ -8,20 +8,20 @@ #include #include #include -#include "boost/lockfree/stack.hpp" +#include "concurrentqueue.h" #include "Event.h" namespace cc { -namespace gfx { class ThreadPool final { public: using Task = std::function; + using TaskQueue = moodycamel::ConcurrentQueue; - static uint32_t const kCpuCoreCount; - static uint32_t const kMaxThreadCount; + static uint8_t const kCpuCoreCount; + static uint8_t const kMaxThreadCount; ThreadPool() noexcept = default; ~ThreadPool() = default; @@ -37,15 +37,15 @@ public: private: - using Event = EventCV; + using Event = ConditionVariable; void AddThread() noexcept; - boost::lockfree::stack mTasks { 64 }; + TaskQueue mTasks {}; std::list mWorkers {}; Event mEvent {}; std::atomic mRunning { false }; - uint32_t mWorkerCount { kMaxThreadCount }; + uint8_t mWorkerCount { kMaxThreadCount }; }; template @@ -55,13 +55,13 @@ auto ThreadPool::DispatchTask(Function&& func, Args&&... args) noexcept -> std:: using ReturnType = decltype(func(std::forward(args)...)); auto task = std::make_shared>(std::bind(std::forward(func), std::forward(args)...)); - mTasks.push([task]() + bool const succeed = mTasks.enqueue([task]() { (*task)(); }); + assert(succeed); mEvent.Signal(); return task->get_future(); } -} // namespace gfx } // namespace cc diff --git a/native/cocos/renderer/core/thread/ThreadSafeCounter.h b/native/cocos/base/threading/ThreadSafeCounter.h similarity index 84% rename from native/cocos/renderer/core/thread/ThreadSafeCounter.h rename to native/cocos/base/threading/ThreadSafeCounter.h index 05b8ae7b4b..aed7027765 100644 --- a/native/cocos/renderer/core/thread/ThreadSafeCounter.h +++ b/native/cocos/base/threading/ThreadSafeCounter.h @@ -4,7 +4,6 @@ #include namespace cc { -namespace gfx { template >> class ThreadSafeCounter final @@ -15,7 +14,7 @@ public: inline T Add(T const v) noexcept { return mCounter.fetch_add(v, std::memory_order_relaxed); } inline T Decrement() noexcept { return Subtract(1); } inline T Subtract(T const v) noexcept { return mCounter.fetch_sub(v, std::memory_order_relaxed); } - inline T Set(T const v) noexcept { return mCounter.store(v, std::memory_order_relaxed); } + inline void Set(T const v) noexcept { mCounter.store(v, std::memory_order_relaxed); } inline T Get() const noexcept { return mCounter.load(std::memory_order_relaxed); } inline T Reset() noexcept { Set(0); } @@ -24,5 +23,4 @@ private: std::atomic mCounter { 0 }; }; -} // namespace gfx } // namespace cc diff --git a/native/cocos/cocos2d.h b/native/cocos/cocos2d.h index 5b887a0a7d..3c7b5d8799 100644 --- a/native/cocos/cocos2d.h +++ b/native/cocos/cocos2d.h @@ -51,6 +51,7 @@ THE SOFTWARE. #include "base/Macros.h" #include "base/UTF8.h" #include "base/Utils.h" +#include "base/job-system/JobSystem.h" // math @@ -76,7 +77,6 @@ THE SOFTWARE. #include "platform/SAXParser.h" #include "platform/CanvasRenderingContext2D.h" - namespace cc { CC_DLL const char* cocos2dVersion(); @@ -84,4 +84,3 @@ CC_DLL const char* cocos2dVersion(); } #endif // __COCOS2D_H__ - diff --git a/native/cocos/renderer/core/CMakeLists.txt b/native/cocos/renderer/core/CMakeLists.txt index 8b0a0d549a..64268848bb 100644 --- a/native/cocos/renderer/core/CMakeLists.txt +++ b/native/cocos/renderer/core/CMakeLists.txt @@ -25,8 +25,9 @@ set(CC_BASE_SOURCES set(INCLUDE_FILES ${GFX_EXTERNAL_PATH}/nedmalloc ${GFX_EXTERNAL_PATH}/tommyds - ${GFX_EXTERNAL_PATH}/boost ${GFX_EXTERNAL_PATH}/concurrentqueue + ${GFX_EXTERNAL_PATH}/taskflow + ${COCOS_SRC_PATH}/base ${COCOS_SRC_PATH}/renderer/core ) diff --git a/native/cocos/renderer/core/Core.h b/native/cocos/renderer/core/Core.h index 9002e24a77..dd44f4f77c 100644 --- a/native/cocos/renderer/core/Core.h +++ b/native/cocos/renderer/core/Core.h @@ -5,8 +5,8 @@ #include "base/CachedArray.h" #include "base/StringUtil.h" -#include "thread/ThreadPool.h" -#include "thread/CommandEncoder.h" +#include "threading/ThreadPool.h" +#include "threading/CommandEncoder.h" #include "gfx/GFXDevice.h" #include "gfx/GFXContext.h" @@ -38,3 +38,8 @@ #include "gfx-proxy/GFXDescriptorSetProxy.h" #include "gfx-proxy/GFXCommandBufferProxy.h" #include "gfx-proxy/GFXQueueProxy.h" + +#define CC_JOB_SYSTEM_TASKFLOW 1 +#define CC_JOB_SYSTEM_TBB 2 + +#define CC_JOB_SYSTEM CC_JOB_SYSTEM_TASKFLOW diff --git a/native/cocos/renderer/core/gfx-proxy/GFXBufferProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXBufferProxy.cpp index 2b8403e82f..41b6287238 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXBufferProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXBufferProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXBufferProxy.h" #include "GFXDeviceProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXCommandBufferProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXCommandBufferProxy.cpp index 0ef7662a9c..46350862e7 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXCommandBufferProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXCommandBufferProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXBufferProxy.h" #include "GFXCommandBufferProxy.h" #include "GFXDescriptorSetProxy.h" @@ -15,8 +15,13 @@ namespace cc { namespace gfx { +void CommandBufferProxy::initEncoder() { + _encoder = CC_NEW(CommandEncoder); + _encoder->SetImmediateMode(false); +} + bool CommandBufferProxy::initialize(const CommandBufferInfo &info) { - _encoder = ((DeviceProxy *)_device)->getMainEncoder(); + initEncoder(); _type = info.type; _queue = info.queue; @@ -48,6 +53,8 @@ void CommandBufferProxy::destroy() { _remote = nullptr; } + + CC_SAFE_DELETE(_encoder); } void CommandBufferProxy::begin(RenderPass *renderPass, uint subpass, Framebuffer *frameBuffer) { @@ -71,6 +78,8 @@ void CommandBufferProxy::end() { { remote->end(); }); + + _encoder->FinishWriting(); } void CommandBufferProxy::beginRenderPass(RenderPass *renderPass, Framebuffer *fbo, const Rect &renderArea, const Color *colors, float depth, int stencil) { diff --git a/native/cocos/renderer/core/gfx-proxy/GFXCommandBufferProxy.h b/native/cocos/renderer/core/gfx-proxy/GFXCommandBufferProxy.h index bf86a80d84..fc0b8a336d 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXCommandBufferProxy.h +++ b/native/cocos/renderer/core/gfx-proxy/GFXCommandBufferProxy.h @@ -36,10 +36,13 @@ public: virtual uint getNumDrawCalls() const override { return _remote->getNumDrawCalls(); } virtual uint getNumInstances() const override { return _remote->getNumInstances(); } virtual uint getNumTris() const override { return _remote->getNumTris(); } - - void setEncoder(CommandEncoder *encoder) { _encoder = encoder; } + + CC_INLINE CommandEncoder *getEncoder() { return _encoder; } private: + friend class DeviceProxy; + + void initEncoder(); CommandEncoder *_encoder = nullptr; }; diff --git a/native/cocos/renderer/core/gfx-proxy/GFXDescriptorSetLayoutProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXDescriptorSetLayoutProxy.cpp index 7e1fd3f409..680337a8b5 100644 --- a/native/cocos/renderer/core/gfx-proxy/GFXDescriptorSetLayoutProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXDescriptorSetLayoutProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXDeviceProxy.h" #include "GFXDescriptorSetLayoutProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXDescriptorSetProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXDescriptorSetProxy.cpp index 424a7143e2..0d6e674f44 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXDescriptorSetProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXDescriptorSetProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXBufferProxy.h" #include "GFXDescriptorSetLayoutProxy.h" #include "GFXDescriptorSetProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXDeviceProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXDeviceProxy.cpp index 2abde7323b..1c08477ae7 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXDeviceProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXDeviceProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXBufferProxy.h" #include "GFXCommandBufferProxy.h" #include "GFXDescriptorSetLayoutProxy.h" @@ -45,6 +45,7 @@ bool DeviceProxy::initialize(const DeviceInfo &info) { _deviceName = _remote->getDeviceName(); _queue = CC_NEW(QueueProxy(_remote->getQueue(), this)); _cmdBuff = CC_NEW(CommandBufferProxy(_remote->getCommandBuffer(), this)); + ((CommandBufferProxy *)_cmdBuff)->initEncoder(); _renderer = _remote->getRenderer(); _vendor = _remote->getVendor(); _maxVertexAttributes = _remote->getMaxVertexAttributes(); @@ -59,38 +60,17 @@ bool DeviceProxy::initialize(const DeviceInfo &info) { _uboOffsetAlignment = _remote->getUboOffsetAlignment(); _depthBits = _remote->getDepthBits(); _stencilBits = _remote->getStencilBits(); - memcpy(_features, ((DeviceProxy *)getRemote())->_features, (uint)Feature::COUNT * sizeof(bool)); + memcpy(_features, _remote->_features, (uint)Feature::COUNT * sizeof(bool)); _mainEncoder = CC_NEW(CommandEncoder); _mainEncoder->RunConsumerThread(); - _contexts.resize(std::thread::hardware_concurrency() - 1); - - CommandBufferInfo cbInfo; - cbInfo.type = CommandBufferType::PRIMARY; - cbInfo.queue = _queue; - - for (auto &context : _contexts) { - context.encoder = CC_NEW(CommandEncoder); - context.encoder->RunConsumerThread(); - context.commandBuffer = createCommandBuffer(); - context.commandBuffer->initialize(cbInfo); - ((CommandBufferProxy *)context.commandBuffer)->setEncoder(context.encoder); - } - setMultithreaded(true); return true; } void DeviceProxy::destroy() { - for (auto &context : _contexts) { - context.encoder->TerminateConsumerThread(); - CC_SAFE_DELETE(context.encoder); - CC_SAFE_DESTROY(context.commandBuffer); - } - _contexts.clear(); - if (_remote) { ENCODE_COMMAND_2( getMainEncoder(), @@ -159,23 +139,23 @@ void DeviceProxy::setMultithreaded(bool multithreaded) { if (multithreaded) { _mainEncoder->SetImmediateMode(false); - ((DeviceProxy *) _remote)->bindRenderContext(false); + _remote->bindRenderContext(false); ENCODE_COMMAND_1( _mainEncoder, DeviceMakeCurrent, - remote, (DeviceProxy*)_remote, + remote, _remote, { remote->bindDeviceContext(true); }); } else { ENCODE_COMMAND_1( _mainEncoder, DeviceMakeCurrent, - remote, (DeviceProxy*)_remote, + remote, _remote, { remote->bindDeviceContext(false); }); _mainEncoder->FinishWriting(true); // wait till finished _mainEncoder->SetImmediateMode(true); - ((DeviceProxy *) _remote)->bindRenderContext(true); + _remote->bindRenderContext(true); } } diff --git a/native/cocos/renderer/core/gfx-proxy/GFXDeviceProxy.h b/native/cocos/renderer/core/gfx-proxy/GFXDeviceProxy.h index 0fac494288..d4ec7e84a2 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXDeviceProxy.h +++ b/native/cocos/renderer/core/gfx-proxy/GFXDeviceProxy.h @@ -1,15 +1,17 @@ #pragma once #include "../gfx/GFXDevice.h" -#include "../thread/Semaphore.h" +#include "threading/Semaphore.h" #include "GFXProxy.h" #define MAX_CPU_FRAME_AHEAD 2 namespace cc { -namespace gfx { class CommandEncoder; + +namespace gfx { + class CommandBuffer; // one per CPU core @@ -57,13 +59,10 @@ public: virtual uint getNumTris() const override { return _remote->getNumTris(); } CommandEncoder *getMainEncoder() const { return _mainEncoder; } - vector &getSubmitContexts() { return _contexts; } - protected: bool _multithreaded{false}; CommandEncoder *_mainEncoder{nullptr}; - vector _contexts{}; Semaphore _frameBoundarySemaphore{MAX_CPU_FRAME_AHEAD}; }; diff --git a/native/cocos/renderer/core/gfx-proxy/GFXFramebufferProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXFramebufferProxy.cpp index a783bfaa37..47d3c9cf9e 100644 --- a/native/cocos/renderer/core/gfx-proxy/GFXFramebufferProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXFramebufferProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXDeviceProxy.h" #include "GFXFramebufferProxy.h" #include "GFXRenderPassProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXInputAssemblerProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXInputAssemblerProxy.cpp index 494495fdbe..3ce3a971a7 100644 --- a/native/cocos/renderer/core/gfx-proxy/GFXInputAssemblerProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXInputAssemblerProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXBufferProxy.h" #include "GFXDeviceProxy.h" #include "GFXInputAssemblerProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXPipelineLayoutProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXPipelineLayoutProxy.cpp index c12894ba7f..1e04353d99 100644 --- a/native/cocos/renderer/core/gfx-proxy/GFXPipelineLayoutProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXPipelineLayoutProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXDescriptorSetLayoutProxy.h" #include "GFXDeviceProxy.h" #include "GFXPipelineLayoutProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXPipelineStateProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXPipelineStateProxy.cpp index 4660eb9f39..560d7daf2c 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXPipelineStateProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXPipelineStateProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXDeviceProxy.h" #include "GFXPipelineLayoutProxy.h" #include "GFXPipelineStateProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXQueueProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXQueueProxy.cpp index fb8f58fb4c..d23c5154e7 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXQueueProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXQueueProxy.cpp @@ -1,6 +1,7 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "job-system/JobSystem.h" +#include "threading/CommandEncoder.h" #include "GFXCommandBufferProxy.h" #include "GFXDeviceProxy.h" #include "GFXQueueProxy.h" @@ -45,15 +46,33 @@ void QueueProxy::submit(const CommandBuffer *const *cmdBuffs, uint count, Fence remoteCmdBuffs[i] = ((CommandBufferProxy *)cmdBuffs[i])->getRemote(); } - ENCODE_COMMAND_4( + bool multiThreaded = _device->hasFeature(Feature::MULTITHREADED_SUBMISSION); + + ENCODE_COMMAND_6( encoder, QueueSubmit, remote, getRemote(), - cmdBuffs, remoteCmdBuffs, + multiThreaded, multiThreaded, + cmdBuffs, cmdBuffs, + remoteCmdBuffs, remoteCmdBuffs, count, count, fence, fence, { - remote->submit(cmdBuffs, count, fence); + if (multiThreaded) { + JobGraph g; + Job j = g.createForEachIndexJob(1u, count, 1u, [this](uint i) { + ((CommandBufferProxy *)cmdBuffs[i])->getEncoder()->FlushCommands(); + }); + JobSystem::getInstance().run(g); + ((CommandBufferProxy *)cmdBuffs[0])->getEncoder()->FlushCommands(); + JobSystem::getInstance().waitForAll(); + } else { + for (uint i = 0u; i < count; ++i) { + ((CommandBufferProxy *)cmdBuffs[i])->getEncoder()->FlushCommands(); + } + } + + remote->submit(remoteCmdBuffs, count, fence); }); } diff --git a/native/cocos/renderer/core/gfx-proxy/GFXRenderPassProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXRenderPassProxy.cpp index 8da2b6ea6e..3755447d7a 100644 --- a/native/cocos/renderer/core/gfx-proxy/GFXRenderPassProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXRenderPassProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXDeviceProxy.h" #include "GFXRenderPassProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXSamplerProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXSamplerProxy.cpp index a0130e5289..f66ecdd7bb 100644 --- a/native/cocos/renderer/core/gfx-proxy/GFXSamplerProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXSamplerProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXDescriptorSetLayoutProxy.h" #include "GFXDeviceProxy.h" #include "GFXSamplerProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXShaderProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXShaderProxy.cpp index acd7e0567d..5a1f770ff4 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXShaderProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXShaderProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXDeviceProxy.h" #include "GFXShaderProxy.h" diff --git a/native/cocos/renderer/core/gfx-proxy/GFXTextureProxy.cpp b/native/cocos/renderer/core/gfx-proxy/GFXTextureProxy.cpp index 88fb07180c..0f0f871b0d 100755 --- a/native/cocos/renderer/core/gfx-proxy/GFXTextureProxy.cpp +++ b/native/cocos/renderer/core/gfx-proxy/GFXTextureProxy.cpp @@ -1,6 +1,6 @@ #include "CoreStd.h" -#include "../thread/CommandEncoder.h" +#include "threading/CommandEncoder.h" #include "GFXTextureProxy.h" #include "GFXDeviceProxy.h" diff --git a/native/cocos/renderer/core/gfx/GFXDevice.h b/native/cocos/renderer/core/gfx/GFXDevice.h index 21f63ba441..e12a6758bb 100755 --- a/native/cocos/renderer/core/gfx/GFXDevice.h +++ b/native/cocos/renderer/core/gfx/GFXDevice.h @@ -110,6 +110,8 @@ public: CC_INLINE uint genShaderId() { return _shaderIdGen++; } protected: + friend class DeviceProxy; + virtual void bindRenderContext(bool bound) {} virtual void bindDeviceContext(bool bound) {} diff --git a/native/cocos/renderer/core/thread/ConditionVariable.h b/native/cocos/renderer/core/thread/ConditionVariable.h deleted file mode 100644 index a152a57df1..0000000000 --- a/native/cocos/renderer/core/thread/ConditionVariable.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace cc { -namespace gfx { - -class ConditionVariable final -{ -public: - - void Wait() noexcept; - void Signal() noexcept; - void SignalAll() noexcept; - -private: - - std::mutex mMutex; - std::condition_variable mCV; -}; - -} // namespace gfx -} // namespace cc diff --git a/native/cocos/renderer/core/thread/ReadWriteLock.cpp b/native/cocos/renderer/core/thread/ReadWriteLock.cpp deleted file mode 100644 index 917ef37354..0000000000 --- a/native/cocos/renderer/core/thread/ReadWriteLock.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "ReadWriteLock.h" diff --git a/native/cocos/renderer/core/thread/ReadWriteLock.h b/native/cocos/renderer/core/thread/ReadWriteLock.h deleted file mode 100644 index a4a6974948..0000000000 --- a/native/cocos/renderer/core/thread/ReadWriteLock.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include - -namespace cc { -namespace gfx { - -class ReadWriteLock final -{ -public: - - template - auto LockRead(Function&& func, Args&&... args) noexcept -> decltype(func(std::forward(args)...)); - - template - auto LockWrite(Function&& func, Args&&... args) noexcept -> decltype(func(std::forward(args)...)); - -private: - - boost::shared_mutex mMutex; -}; - -template -auto ReadWriteLock::LockRead(Function&& func, Args&&... args) noexcept -> decltype(func(std::forward(args)...)) -{ - boost::shared_lock lock(mMutex); - return func(std::forward(args)...); -} - -template -auto ReadWriteLock::LockWrite(Function&& func, Args&&... args) noexcept -> decltype(func(std::forward(args)...)) -{ - boost::lock_guard lock(mMutex); - return func(std::forward(args)...); -} - -} // namespace gfx -} // namespace cc diff --git a/native/cocos/renderer/gfx-gles2/GLES2Context.cpp b/native/cocos/renderer/gfx-gles2/GLES2Context.cpp index a78143340b..125c6ef53e 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Context.cpp +++ b/native/cocos/renderer/gfx-gles2/GLES2Context.cpp @@ -282,7 +282,7 @@ bool GLES2Context::initialize(const ContextInfo &info) { uint height = _device->getHeight(); ANativeWindow_setBuffersGeometry((ANativeWindow *)_windowHandle, width, height, nFmt); - _eglSurface = eglCreateWindowSurface(_eglDisplay, _eglConfig, (EGLNativeWindowType)_windowHandle, NULL); + EGL_CHECK(_eglSurface = eglCreateWindowSurface(_eglDisplay, _eglConfig, (EGLNativeWindowType)_windowHandle, NULL)); if (_eglSurface == EGL_NO_SURFACE) { CC_LOG_ERROR("Recreate window surface failed."); return; @@ -353,7 +353,7 @@ void GLES2Context::destroy() { _eglContext = EGL_NO_CONTEXT; } - if (_isPrimaryContex && _eglSurface != EGL_NO_SURFACE) { + if (_eglSurface != EGL_NO_SURFACE) { EGL_CHECK(eglDestroySurface(_eglDisplay, _eglSurface)); _eglSurface = EGL_NO_SURFACE; } @@ -374,11 +374,13 @@ void GLES2Context::destroy() { } bool GLES2Context::MakeCurrentImpl(bool bound) { - return eglMakeCurrent(_eglDisplay, + bool succeeded; + EGL_CHECK(succeeded = eglMakeCurrent(_eglDisplay, bound ? _eglSurface : EGL_NO_SURFACE, bound ? _eglSurface : EGL_NO_SURFACE, bound ? _eglContext : EGL_NO_CONTEXT - ); + )); + return succeeded; } void GLES2Context::present() { @@ -415,9 +417,9 @@ bool GLES2Context::MakeCurrent(bool bound) { #endif #if CC_DEBUG > 0 && CC_PLATFORM != CC_PLATFORM_MAC_IOS - glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR); - glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE); - glDebugMessageCallbackKHR(GLES2EGLDebugProc, NULL); + GL_CHECK(glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR)); + GL_CHECK(glDebugMessageControlKHR(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE)); + GL_CHECK(glDebugMessageCallbackKHR(GLES2EGLDebugProc, NULL)); #endif _isInitialized = true; @@ -425,44 +427,44 @@ bool GLES2Context::MakeCurrent(bool bound) { ////////////////////////////////////////////////////////////////////////// - glPixelStorei(GL_PACK_ALIGNMENT, 1); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glActiveTexture(GL_TEXTURE0); + GL_CHECK(glPixelStorei(GL_PACK_ALIGNMENT, 1)); + GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); + GL_CHECK(glActiveTexture(GL_TEXTURE0)); ////////////////////////////////////////////////////////////////////////// - glEnable(GL_SCISSOR_TEST); - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); + GL_CHECK(glEnable(GL_SCISSOR_TEST)); + GL_CHECK(glEnable(GL_CULL_FACE)); + GL_CHECK(glCullFace(GL_BACK)); - glFrontFace(GL_CCW); + GL_CHECK(glFrontFace(GL_CCW)); - //glDisable(GL_MULTISAMPLE); + //GL_CHECK(glDisable(GL_MULTISAMPLE)); ////////////////////////////////////////////////////////////////////////// // DepthStencilState - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); - glDepthFunc(GL_LESS); + GL_CHECK(glEnable(GL_DEPTH_TEST)); + GL_CHECK(glDepthMask(GL_TRUE)); + GL_CHECK(glDepthFunc(GL_LESS)); - glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 1, 0xffffffff); - glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_KEEP); - glStencilMaskSeparate(GL_FRONT, 0xffffffff); - glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 1, 0xffffffff); - glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP); - glStencilMaskSeparate(GL_BACK, 0xffffffff); + GL_CHECK(glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 1, 0xffffffff)); + GL_CHECK(glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_KEEP)); + GL_CHECK(glStencilMaskSeparate(GL_FRONT, 0xffffffff)); + GL_CHECK(glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 1, 0xffffffff)); + GL_CHECK(glStencilOpSeparate(GL_BACK, GL_KEEP, GL_KEEP, GL_KEEP)); + GL_CHECK(glStencilMaskSeparate(GL_BACK, 0xffffffff)); - glDisable(GL_STENCIL_TEST); + GL_CHECK(glDisable(GL_STENCIL_TEST)); ////////////////////////////////////////////////////////////////////////// // BlendState - glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); - glDisable(GL_BLEND); - glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD); - glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO); - glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); - glBlendColor((GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f); + GL_CHECK(glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE)); + GL_CHECK(glDisable(GL_BLEND)); + GL_CHECK(glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD)); + GL_CHECK(glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO)); + GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)); + GL_CHECK(glBlendColor((GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f, (GLclampf)0.0f)); CC_LOG_DEBUG("eglMakeCurrent() - SUCCEEDED, Context: 0x%p", this); return true; diff --git a/native/cocos/renderer/gfx-gles2/GLES2Std.h b/native/cocos/renderer/gfx-gles2/GLES2Std.h index 3d890076a8..281dbb49dd 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Std.h +++ b/native/cocos/renderer/gfx-gles2/GLES2Std.h @@ -46,11 +46,20 @@ THE SOFTWARE. #define GL_CHECK(x) \ do { \ x; GLenum err = glGetError(); \ - if (err) { \ + if (err != GL_NO_ERROR) { \ CC_LOG_ERROR("%s returned GL error: 0x%x", #x, err); \ CCASSERT(0, "GL error"); \ } \ } while (0) +#define EGL_CHECK(x) \ + do { \ + x; EGLint err = eglGetError(); \ + if (err != EGL_SUCCESS) { \ + CC_LOG_ERROR("%s returned EGL error: 0x%x", #x, err); \ + CCASSERT(0, "EGL error"); \ + } \ + } while (0) #else #define GL_CHECK(x) x +#define EGL_CHECK(x) x #endif diff --git a/native/cocos/renderer/gfx-gles3/GLES3Context.cpp b/native/cocos/renderer/gfx-gles3/GLES3Context.cpp index 570817a23a..f52ecaa86b 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Context.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3Context.cpp @@ -284,7 +284,7 @@ bool GLES3Context::initialize(const ContextInfo &info) { uint height = _device->getHeight(); ANativeWindow_setBuffersGeometry((ANativeWindow *)_windowHandle, width, height, nFmt); - _eglSurface = eglCreateWindowSurface(_eglDisplay, _eglConfig, (EGLNativeWindowType)_windowHandle, NULL); + EGL_CHECK(_eglSurface = eglCreateWindowSurface(_eglDisplay, _eglConfig, (EGLNativeWindowType)_windowHandle, NULL)); if (_eglSurface == EGL_NO_SURFACE) { CC_LOG_ERROR("Recreate window surface failed."); return; @@ -353,7 +353,7 @@ void GLES3Context::destroy() { _eglContext = EGL_NO_CONTEXT; } - if (_isPrimaryContex && _eglSurface != EGL_NO_SURFACE) { + if (_eglSurface != EGL_NO_SURFACE) { EGL_CHECK(eglDestroySurface(_eglDisplay, _eglSurface)); _eglSurface = EGL_NO_SURFACE; } @@ -374,11 +374,13 @@ void GLES3Context::destroy() { } bool GLES3Context::MakeCurrentImpl(bool bound) { - return eglMakeCurrent(_eglDisplay, + bool succeeded; + EGL_CHECK(succeeded = eglMakeCurrent(_eglDisplay, bound ? _eglSurface : EGL_NO_SURFACE, bound ? _eglSurface : EGL_NO_SURFACE, bound ? _eglContext : EGL_NO_CONTEXT - ); + )); + return succeeded; } void GLES3Context::present() { diff --git a/native/cocos/renderer/gfx-gles3/GLES3Std.h b/native/cocos/renderer/gfx-gles3/GLES3Std.h index 88406765ff..164e603f55 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Std.h +++ b/native/cocos/renderer/gfx-gles3/GLES3Std.h @@ -20,11 +20,20 @@ #define GL_CHECK(x) \ do { \ x; GLenum err = glGetError(); \ - if (err) { \ + if (err != GL_NO_ERROR) { \ CC_LOG_ERROR("%s returned GL error: 0x%x", #x, err); \ CCASSERT(0, "GL error"); \ } \ } while (0) +#define EGL_CHECK(x) \ + do { \ + x; EGLint err = eglGetError(); \ + if (err != EGL_SUCCESS) { \ + CC_LOG_ERROR("%s returned EGL error: 0x%x", #x, err); \ + CCASSERT(0, "EGL error"); \ + } \ + } while (0) #else #define GL_CHECK(x) x +#define EGL_CHECK(x) x #endif diff --git a/native/cocos/renderer/gfx-vulkan/VKBuffer.cpp b/native/cocos/renderer/gfx-vulkan/VKBuffer.cpp index f203266f77..8c84592e6a 100644 --- a/native/cocos/renderer/gfx-vulkan/VKBuffer.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKBuffer.cpp @@ -185,11 +185,7 @@ void CCVKBuffer::update(void *buffer, uint size) { if (_buffer) { memcpy(_buffer, buffer, size); } - // This assumes the default command buffer will get submitted every frame, - // which is true for now but may change in the future. This appoach gives us - // the wiggle room to leverage immediate update vs. copy-upload strategies without - // breaking compatabilities. When we reached some conclusion on this subject, - // getting rid of this interface all together might become a better option. + CommandBuffer *cmdBuff = _device->getCommandBuffer(); cmdBuff->begin(); const CCVKGPUCommandBuffer *gpuCommandBuffer = ((CCVKCommandBuffer *)cmdBuff)->gpuCommandBuffer(); diff --git a/native/cocos/renderer/gfx-vulkan/VKCommands.cpp b/native/cocos/renderer/gfx-vulkan/VKCommands.cpp index e24e6488fb..321c3324c4 100644 --- a/native/cocos/renderer/gfx-vulkan/VKCommands.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKCommands.cpp @@ -673,18 +673,17 @@ void CCVKCmdFuncUpdateBuffer(CCVKDevice *device, CCVKGPUBuffer *gpuBuffer, const sizeToUpload = size; } - // back buffer instances update command - if (gpuBuffer->instanceSize) { - device->gpuBufferHub()->record(gpuBuffer, dataToUpload, sizeToUpload); - return; - } +// if (!cmdBuffer && gpuBuffer->mappedData) { +// device->gpuTransportHub()->checkIn(gpuBuffer->mappedData + offset, dataToUpload, sizeToUpload); +// return; +// } CCVKGPUBuffer stagingBuffer; stagingBuffer.size = sizeToUpload; device->gpuStagingBufferPool()->alloc(&stagingBuffer); memcpy(stagingBuffer.mappedData, dataToUpload, sizeToUpload); - VkBufferCopy region{stagingBuffer.startOffset, gpuBuffer->startOffset, sizeToUpload}; + VkBufferCopy region{stagingBuffer.startOffset, gpuBuffer->startOffset + offset, sizeToUpload}; auto upload = [&stagingBuffer, &gpuBuffer, ®ion](const CCVKGPUCommandBuffer *cmdBuffer) { vkCmdCopyBuffer(cmdBuffer->vkCommandBuffer, stagingBuffer.vkBuffer, gpuBuffer->vkBuffer, 1, ®ion); @@ -699,7 +698,7 @@ void CCVKCmdFuncUpdateBuffer(CCVKDevice *device, CCVKGPUBuffer *gpuBuffer, const barrier.size = region.size; vkCmdPipelineBarrier(cmdBuffer->vkCommandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, gpuBuffer->targetStage, 0, 0, nullptr, 1, &barrier, 0, nullptr); }; - + if (cmdBuffer) { upload(cmdBuffer); } else { diff --git a/native/cocos/renderer/gfx-vulkan/VKDevice.cpp b/native/cocos/renderer/gfx-vulkan/VKDevice.cpp index 934f3d1c01..9d996a4db0 100644 --- a/native/cocos/renderer/gfx-vulkan/VKDevice.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKDevice.cpp @@ -298,9 +298,9 @@ bool CCVKDevice::initialize(const DeviceInfo &info) { _gpuStagingBufferPools.push_back(CC_NEW(CCVKGPUStagingBufferPool(_gpuDevice))); _gpuTransportHubs[i]->link(((CCVKQueue *)_queue)->gpuQueue(), - _gpuFencePools[i], - _gpuCommandBufferPools[i], - _gpuStagingBufferPools[i]); + _gpuFencePools[i], + _gpuCommandBufferPools[i], + _gpuStagingBufferPools[i]); } _gpuDescriptorHub = CC_NEW(CCVKGPUDescriptorHub(_gpuDevice)); _gpuSemaphorePool = CC_NEW(CCVKGPUSemaphorePool(_gpuDevice)); @@ -399,7 +399,7 @@ void CCVKDevice::destroy() { CC_SAFE_DELETE(_gpuSemaphorePool); CC_SAFE_DELETE(_gpuDescriptorHub); - uint backBufferCount = ((CCVKContext*)_context)->gpuContext()->swapchainCreateInfo.minImageCount; + uint backBufferCount = ((CCVKContext *)_context)->gpuContext()->swapchainCreateInfo.minImageCount; for (uint i = 0u; i < backBufferCount; i++) { _gpuRecycleBins[i]->clear(); @@ -500,7 +500,7 @@ void CCVKDevice::acquire() { } // reset everything only when no pending commands - if (!((CCVKCommandBuffer *)_cmdBuff)->gpuCommandBuffer()->began) { + if (gpuTransportHub()->empty()) { gpuFencePool()->reset(); gpuRecycleBin()->clear(); gpuDescriptorSetPool()->reset();