diff --git a/native/cocos/base/threading/CommandEncoder.cpp b/native/cocos/base/threading/CommandEncoder.cpp index f0d9756073..17521317ad 100644 --- a/native/cocos/base/threading/CommandEncoder.cpp +++ b/native/cocos/base/threading/CommandEncoder.cpp @@ -3,317 +3,269 @@ namespace cc { -namespace -{ +namespace { uint32_t constexpr kMemoryChunkSize = 32 * 1024 * 1024; uint32_t constexpr kMemoryChunkPoolCapacity = 16; uint32_t constexpr kSwitchChunkMemoryRequirement = sizeof(MemoryChunkSwitchCommand) + sizeof(DummyCommand); -} +} // namespace -CommandEncoder::MemoryAllocator& CommandEncoder::MemoryAllocator::GetInstance() noexcept -{ +CommandEncoder::MemoryAllocator &CommandEncoder::MemoryAllocator::getInstance() noexcept { static CommandEncoder::MemoryAllocator instance; return instance; } -uint8_t* CommandEncoder::MemoryAllocator::Request() noexcept -{ - uint8_t* newChunk = nullptr; +uint8_t *CommandEncoder::MemoryAllocator::request() noexcept { + uint8_t *newChunk = nullptr; - if (mChunkPool.try_dequeue(newChunk)) - { + if (mChunkPool.try_dequeue(newChunk)) { mChunkCount.fetch_sub(1, std::memory_order_acq_rel); - } - else - { - newChunk = MemoryAllocateForMultiThread(kMemoryChunkSize); + } else { + newChunk = memoryAllocateForMultiThread(kMemoryChunkSize); } return newChunk; } -void CommandEncoder::MemoryAllocator::Recycle(uint8_t* const chunk, bool const freeByUser) noexcept -{ - if (freeByUser) - { +void CommandEncoder::MemoryAllocator::recycle(uint8_t *const chunk, bool const freeByUser) noexcept { + if (freeByUser) { mChunkFreeQueue.enqueue(chunk); - } - else - { - Free(chunk); + } else { + free(chunk); } } -void CommandEncoder::MemoryAllocator::FreeByUser(CommandEncoder* const mainCommandbuffer) noexcept -{ +void CommandEncoder::MemoryAllocator::freeByUser(CommandEncoder *const mainCommandbuffer) noexcept { auto queue = &mChunkFreeQueue; ENCODE_COMMAND_1( - mainCommandbuffer, FreeChunksInFreeQueue, - queue, queue, - { - uint8_t* chunk = nullptr; + mainCommandbuffer, freeChunksInFreeQueue, + queue, queue, + { + uint8_t *chunk = nullptr; - while(queue->try_dequeue(chunk)) - { - CommandEncoder::MemoryAllocator::GetInstance().Free(chunk); - } - }); + while (queue->try_dequeue(chunk)) { + CommandEncoder::MemoryAllocator::getInstance().free(chunk); + } + }); - mainCommandbuffer->Kick(); + mainCommandbuffer->kick(); } -void CommandEncoder::MemoryAllocator::Free(uint8_t* const chunk) noexcept -{ - if (mChunkCount.load(std::memory_order_acquire) >= kMemoryChunkPoolCapacity) - { - MemoryFreeForMultiThread(chunk); - } - else - { +void CommandEncoder::MemoryAllocator::free(uint8_t *const chunk) noexcept { + if (mChunkCount.load(std::memory_order_acquire) >= kMemoryChunkPoolCapacity) { + memoryFreeForMultiThread(chunk); + } else { mChunkPool.enqueue(chunk); mChunkCount.fetch_add(1, std::memory_order_acq_rel); } } -CommandEncoder::CommandEncoder() -{ - uint8_t* const chunk = MemoryAllocator::GetInstance().Request(); +CommandEncoder::CommandEncoder() { + uint8_t *const chunk = MemoryAllocator::getInstance().request(); mW.mCurrentMemoryChunk = chunk; mR.mCurrentMemoryChunk = chunk; // 分配一个头节点 不要被Execute - Command* const cmd = Allocate(1); - PushCommands(); - PullCommands(); + Command *const cmd = allocate(1); + pushCommands(); + pullCommands(); mR.mLastCommand = cmd; --mR.mNewCommandCount; } -void CommandEncoder::Kick() noexcept -{ - PushCommands(); +void CommandEncoder::kick() noexcept { + pushCommands(); mN.Signal(); } -void CommandEncoder::KickAndWait() noexcept -{ +void CommandEncoder::kickAndWait() noexcept { EventSem event; - EventSem* const pEvent = &event; + EventSem *const pEvent = &event; ENCODE_COMMAND_1(this, WaitUntilFinish, - pEvent, pEvent, - { - pEvent->Signal(); - }); + pEvent, pEvent, + { + pEvent->Signal(); + }); - Kick(); + kick(); event.Wait(); } -void CommandEncoder::RunConsumerThread() noexcept -{ - if (mWorkerAttached) - { +void CommandEncoder::runConsumerThread() noexcept { + if (mWorkerAttached) { return; } - std::thread consumerThread(&CommandEncoder::ConsumerThreadLoop, this); + std::thread consumerThread(&CommandEncoder::consumerThreadLoop, this); consumerThread.detach(); mWorkerAttached = true; } -void CommandEncoder::TerminateConsumerThread() noexcept -{ - if (! mWorkerAttached) - { +void CommandEncoder::terminateConsumerThread() noexcept { + if (!mWorkerAttached) { return; } EventSem event; - EventSem* const pEvent = &event; + EventSem *const pEvent = &event; - ReaderContext* const pR = &mR; + ReaderContext *const pR = &mR; bool immediateMode = mImmediateMode; mImmediateMode = false; ENCODE_COMMAND_2( - this, TerminateConsumerThread, - pR, pR, - pEvent, pEvent, - { - pR->mTerminateConsumerThread = true; - pR->mFlushingFinished = true; - pEvent->Signal(); - }); + this, terminateConsumerThread, + pR, pR, + pEvent, pEvent, + { + pR->mTerminateConsumerThread = true; + pR->mFlushingFinished = true; + pEvent->Signal(); + }); mImmediateMode = immediateMode; - Kick(); + kick(); event.Wait(); } -void CommandEncoder::FinishWriting(bool wait) noexcept -{ - if (!mImmediateMode) - { - bool* const flushingFinished = &mR.mFlushingFinished; +void CommandEncoder::finishWriting(bool wait) noexcept { + if (!mImmediateMode) { + bool *const flushingFinished = &mR.mFlushingFinished; - ENCODE_COMMAND_1(this, FinishWriting, - flushingFinished, flushingFinished, - { - *flushingFinished = true; - }); + ENCODE_COMMAND_1(this, finishWriting, + flushingFinished, flushingFinished, + { + *flushingFinished = true; + }); - if (wait) KickAndWait(); - else Kick(); + if (wait) + kickAndWait(); + else + kick(); } } -void CommandEncoder::RecycleMemoryChunk(uint8_t* const chunk) const noexcept -{ - CommandEncoder::MemoryAllocator::GetInstance().Recycle(chunk, mFreeChunksByUser); +void CommandEncoder::recycleMemoryChunk(uint8_t *const chunk) const noexcept { + CommandEncoder::MemoryAllocator::getInstance().recycle(chunk, mFreeChunksByUser); } -void CommandEncoder::FreeChunksInFreeQueue(CommandEncoder* const mainCommandbuffer) noexcept -{ - CommandEncoder::MemoryAllocator::GetInstance().FreeByUser(mainCommandbuffer); +void CommandEncoder::freeChunksInFreeQueue(CommandEncoder *const mainCommandbuffer) noexcept { + CommandEncoder::MemoryAllocator::getInstance().freeByUser(mainCommandbuffer); } -uint8_t* CommandEncoder::AllocateImpl(uint32_t& allocatedSize, uint32_t const requestSize) noexcept -{ - uint32_t const alignedSize = Align(requestSize, 16); - assert(alignedSize + kSwitchChunkMemoryRequirement <= kMemoryChunkSize); // 一个Command直接把整个Chunk填满了 +uint8_t *CommandEncoder::allocateImpl(uint32_t &allocatedSize, uint32_t const requestSize) noexcept { + uint32_t const alignedSize = align(requestSize, 16); + assert(alignedSize + kSwitchChunkMemoryRequirement <= kMemoryChunkSize); // 一个Command直接把整个Chunk填满了 uint32_t const newOffset = mW.mOffset + alignedSize; - if (newOffset + kSwitchChunkMemoryRequirement <= kMemoryChunkSize) - { + if (newOffset + kSwitchChunkMemoryRequirement <= kMemoryChunkSize) { allocatedSize = alignedSize; - uint8_t* const allocatedMemory = mW.mCurrentMemoryChunk + mW.mOffset; + uint8_t *const allocatedMemory = mW.mCurrentMemoryChunk + mW.mOffset; mW.mOffset = newOffset; return allocatedMemory; - } - else - { - uint8_t* const newChunk = CommandEncoder::MemoryAllocator::GetInstance().Request(); - MemoryChunkSwitchCommand* const switchCommand = reinterpret_cast(mW.mCurrentMemoryChunk + mW.mOffset); - new(switchCommand) MemoryChunkSwitchCommand(this, newChunk, mW.mCurrentMemoryChunk); - switchCommand->mNext = reinterpret_cast(newChunk); // 注意这里还指向原位置 + } else { + uint8_t *const newChunk = CommandEncoder::MemoryAllocator::getInstance().request(); + MemoryChunkSwitchCommand *const switchCommand = reinterpret_cast(mW.mCurrentMemoryChunk + mW.mOffset); + new (switchCommand) MemoryChunkSwitchCommand(this, newChunk, mW.mCurrentMemoryChunk); + switchCommand->mNext = reinterpret_cast(newChunk); // 注意这里还指向原位置 mW.mLastCommand = switchCommand; ++mW.mPendingCommandCount; mW.mCurrentMemoryChunk = newChunk; mW.mOffset = 0; - DummyCommand* const head = Allocate(1); - new(head) DummyCommand; + DummyCommand *const head = allocate(1); + new (head) DummyCommand; - if (mImmediateMode) - { - PushCommands(); - PullCommands(); + if (mImmediateMode) { + pushCommands(); + pullCommands(); assert(mR.mNewCommandCount == 2); - ExecuteCommand(); - ExecuteCommand(); + executeCommand(); + executeCommand(); } - return AllocateImpl(allocatedSize, requestSize); + return allocateImpl(allocatedSize, requestSize); } } -void CommandEncoder::PushCommands() noexcept -{ +void CommandEncoder::pushCommands() noexcept { mW.mWrittenCommandCount.fetch_add(mW.mPendingCommandCount, std::memory_order_acq_rel); mW.mPendingCommandCount = 0; } -void CommandEncoder::PullCommands() noexcept -{ +void CommandEncoder::pullCommands() noexcept { uint32_t const writtenCommandCountNew = mW.mWrittenCommandCount.load(std::memory_order_acquire); mR.mNewCommandCount += writtenCommandCountNew - mR.mWrittenCommandCountSnap; mR.mWrittenCommandCountSnap = writtenCommandCountNew; } -void CommandEncoder::FlushCommands() noexcept -{ - while (! mR.mFlushingFinished) - { - ExecuteCommand(); +void CommandEncoder::flushCommands() noexcept { + while (!mR.mFlushingFinished) { + executeCommand(); } mR.mFlushingFinished = false; } -void CommandEncoder::ExecuteCommand() noexcept -{ - Command* const cmd = ReadCommand(); +void CommandEncoder::executeCommand() noexcept { + Command *const cmd = readCommand(); - if (! cmd) - { + if (!cmd) { return; } - cmd->Execute(); + cmd->execute(); cmd->~Command(); } -Command* CommandEncoder::ReadCommand() noexcept -{ - while (! HasNewCommand()) - { +Command *CommandEncoder::readCommand() noexcept { + while (!hasNewCommand()) { // 如果当前没有可读的Command 尝试同步一下生产者线程的数据 - PullCommands(); + pullCommands(); // 如果依然没有 挂起消费者线程 等待生产者线程填充Command并唤醒消费者线程 - if (! HasNewCommand()) - { + if (!hasNewCommand()) { mN.Wait(); - PullCommands(); + pullCommands(); } } - Command* const cmd = mR.mLastCommand->GetNext(); + Command *const cmd = mR.mLastCommand->getNext(); mR.mLastCommand = cmd; --mR.mNewCommandCount; assert(cmd); return cmd; } -void CommandEncoder::ConsumerThreadLoop() noexcept -{ - while (! mR.mTerminateConsumerThread) - { - FlushCommands(); +void CommandEncoder::consumerThreadLoop() noexcept { + while (!mR.mTerminateConsumerThread) { + flushCommands(); } mWorkerAttached = false; } -char const* DummyCommand::GetName() const noexcept -{ +char const *DummyCommand::getName() const noexcept { return "Dummy"; } -MemoryChunkSwitchCommand::MemoryChunkSwitchCommand(CommandEncoder* const cb, uint8_t* const newChunk, uint8_t* const oldChunk) noexcept - : mCommandBuffer(cb) - , mNewChunk(newChunk) - , mOldChunk(oldChunk) -{ +MemoryChunkSwitchCommand::MemoryChunkSwitchCommand(CommandEncoder *const cb, uint8_t *const newChunk, uint8_t *const oldChunk) noexcept +: mCommandBuffer(cb), mNewChunk(newChunk), mOldChunk(oldChunk) { } -MemoryChunkSwitchCommand::~MemoryChunkSwitchCommand() -{ - mCommandBuffer->RecycleMemoryChunk(mOldChunk); +MemoryChunkSwitchCommand::~MemoryChunkSwitchCommand() { + mCommandBuffer->recycleMemoryChunk(mOldChunk); } -void MemoryChunkSwitchCommand::Execute() noexcept -{ +void MemoryChunkSwitchCommand::execute() noexcept { mCommandBuffer->mR.mCurrentMemoryChunk = mNewChunk; - mCommandBuffer->PullCommands(); + mCommandBuffer->pullCommands(); } -char const* MemoryChunkSwitchCommand::GetName() const noexcept -{ +char const *MemoryChunkSwitchCommand::getName() const noexcept { return "MemoryChunkSwitch"; } diff --git a/native/cocos/base/threading/CommandEncoder.h b/native/cocos/base/threading/CommandEncoder.h index 156cb2a388..8220dea294 100644 --- a/native/cocos/base/threading/CommandEncoder.h +++ b/native/cocos/base/threading/CommandEncoder.h @@ -1,626 +1,614 @@ #pragma once -#include -#include "concurrentqueue.h" #include "Event.h" +#include "concurrentqueue.h" +#include namespace cc { // 由于系统的内存分配是从一个全局堆里申请 分配过程会上锁导致降低并行度 在多线程的环境下如果频繁的申请释放内存会降低性能 // 所以这里先加个接口来统一调用 未来会换成单独服务于线程的无锁分配器 template -inline T* MemoryAllocateForMultiThread(uint32_t const count) noexcept -{ - return static_cast(malloc(sizeof(T) * count)); +inline T *memoryAllocateForMultiThread(uint32_t const count) noexcept { + return static_cast(malloc(sizeof(T) * count)); } template -inline void MemoryFreeForMultiThread(T* const p) noexcept -{ +inline void memoryFreeForMultiThread(T *const p) noexcept { free(p); } -inline uint32_t constexpr Align(uint32_t const val, uint32_t const alignment) noexcept -{ - return (val + alignment - 1) & ~(alignment-1); +inline uint32_t constexpr align(uint32_t const val, uint32_t const alignment) noexcept { + return (val + alignment - 1) & ~(alignment - 1); } -class Command -{ +class Command { public: + Command() = default; + virtual ~Command() {} + Command(Command const &) = delete; + Command(Command &&) = delete; + Command &operator=(Command const &) = delete; + Command &operator=(Command &&) = delete; - Command() = default; - virtual ~Command() {} - Command(Command const&) = delete; - Command(Command&&) = delete; - Command& operator=(Command const&) = delete; - Command& operator=(Command&&) = delete; - - virtual void Execute() noexcept = 0; - virtual char const* GetName() const noexcept = 0; - inline Command* GetNext() const noexcept { return mNext; } + virtual void execute() noexcept = 0; + virtual char const *getName() const noexcept = 0; + inline Command *getNext() const noexcept { return mNext; } private: - - Command* mNext; + Command *mNext; friend class CommandEncoder; }; -struct alignas(64) WriterContext final -{ - uint8_t* mCurrentMemoryChunk { nullptr }; - Command* mLastCommand { nullptr }; - uint32_t mOffset { 0 }; - uint32_t mPendingCommandCount { 0 }; - std::atomic mWrittenCommandCount { 0 }; +struct alignas(64) WriterContext final { + uint8_t *mCurrentMemoryChunk{nullptr}; + Command *mLastCommand{nullptr}; + uint32_t mOffset{0}; + uint32_t mPendingCommandCount{0}; + std::atomic mWrittenCommandCount{0}; }; -struct alignas(64) ReaderContext final -{ - uint8_t* mCurrentMemoryChunk { nullptr }; - Command* mLastCommand { nullptr }; - uint32_t mOffset { 0 }; - uint32_t mWrittenCommandCountSnap { 0 }; - uint32_t mNewCommandCount { 0 }; - bool mTerminateConsumerThread { false }; - bool mFlushingFinished { false }; +struct alignas(64) ReaderContext final { + uint8_t *mCurrentMemoryChunk{nullptr}; + Command *mLastCommand{nullptr}; + uint32_t mOffset{0}; + uint32_t mWrittenCommandCountSnap{0}; + uint32_t mNewCommandCount{0}; + bool mTerminateConsumerThread{false}; + bool mFlushingFinished{false}; }; // 支持单生产者单消费者的环形缓冲区 -// 生产者线程Allocate 消费者线程Execute +// 生产者线程Allocate 消费者线程execute // 除了Command之外 执行Command需要的数据也需要拷贝到这里 来实现线程安全的数据访问 -class alignas(64) CommandEncoder final -{ +class alignas(64) CommandEncoder final { public: - - CommandEncoder(); - ~CommandEncoder() = default; - CommandEncoder(CommandEncoder const&) = delete; - CommandEncoder(CommandEncoder&&) = delete; - CommandEncoder& operator=(CommandEncoder const&) = delete; - CommandEncoder& operator=(CommandEncoder&&) = delete; + CommandEncoder(); + ~CommandEncoder() = default; + CommandEncoder(CommandEncoder const &) = delete; + CommandEncoder(CommandEncoder &&) = delete; + CommandEncoder &operator=(CommandEncoder const &) = delete; + CommandEncoder &operator=(CommandEncoder &&) = delete; // 分配Command template - std::enable_if_t::value, T*> - Allocate(uint32_t const count) noexcept; + std::enable_if_t::value, T *> + allocate(uint32_t const count) noexcept; // 分配数据 template - std::enable_if_t::value, T*> - Allocate(uint32_t const count) noexcept; + std::enable_if_t::value, T *> + allocate(uint32_t const count) noexcept; template - T* AllocateAndCopy(uint32_t const count, void const* data) noexcept; + T *allocateAndCopy(uint32_t const count, void const *data) noexcept; template - T* AllocateAndZero(uint32_t const count) noexcept; + T *allocateAndZero(uint32_t const count) noexcept; // 通知消费者线程开始工作 - void Kick() noexcept; + void kick() noexcept; // 通知消费者线程开始工作并阻塞生产者线程 直到消费者线程执行完所有命令 - void KickAndWait() noexcept; + void kickAndWait() noexcept; // 只支持单消费者 - void RunConsumerThread() noexcept; - void TerminateConsumerThread() noexcept; - void FinishWriting(bool wait) noexcept; - void FlushCommands() noexcept; + void runConsumerThread() noexcept; + void terminateConsumerThread() noexcept; + void finishWriting(bool wait) noexcept; + void flushCommands() noexcept; - inline bool IsImmediateMode() const noexcept { return mImmediateMode; } - inline void FinishWriting() noexcept { FinishWriting(false); } + inline bool isImmediateMode() const noexcept { return mImmediateMode; } + inline void finishWriting() noexcept { finishWriting(false); } - void RecycleMemoryChunk(uint8_t* const chunk) const noexcept; - static void FreeChunksInFreeQueue(CommandEncoder* const mainCommandbuffer) noexcept; + void recycleMemoryChunk(uint8_t *const chunk) const noexcept; + static void freeChunksInFreeQueue(CommandEncoder *const mainCommandbuffer) noexcept; - inline void SetImmediateMode(bool immediateMode) noexcept { mImmediateMode = immediateMode; } - inline int GetPendingCommandCount() const noexcept { return mW.mPendingCommandCount; } - inline int GetWrittenCommandCount() const noexcept { return mW.mWrittenCommandCount; } - inline int GetNewCommandCount() const noexcept { return mR.mNewCommandCount; } + inline void setImmediateMode(bool immediateMode) noexcept { mImmediateMode = immediateMode; } + inline int getPendingCommandCount() const noexcept { return mW.mPendingCommandCount; } + inline int getWrittenCommandCount() const noexcept { return mW.mWrittenCommandCount; } + inline int getNewCommandCount() const noexcept { return mR.mNewCommandCount; } private: - - class alignas(64) MemoryAllocator final - { + class alignas(64) MemoryAllocator final { public: + MemoryAllocator() = default; + ~MemoryAllocator() = default; + MemoryAllocator(MemoryAllocator const &) = delete; + MemoryAllocator(MemoryAllocator &&) = delete; + MemoryAllocator &operator=(MemoryAllocator const &) = delete; + MemoryAllocator &operator=(MemoryAllocator &&) = delete; - MemoryAllocator() = default; - ~MemoryAllocator() = default; - MemoryAllocator(MemoryAllocator const&) = delete; - MemoryAllocator(MemoryAllocator&&) = delete; - MemoryAllocator& operator=(MemoryAllocator const&) = delete; - MemoryAllocator& operator=(MemoryAllocator&&) = delete; - - static MemoryAllocator& GetInstance() noexcept; - uint8_t* Request() noexcept; - void Recycle(uint8_t* const chunk, bool const freeByUser) noexcept; - void FreeByUser(CommandEncoder* const mainCommandbuffer) noexcept; + static MemoryAllocator &getInstance() noexcept; + uint8_t *request() noexcept; + void recycle(uint8_t *const chunk, bool const freeByUser) noexcept; + void freeByUser(CommandEncoder *const mainCommandbuffer) noexcept; private: + using ChunkQueue = moodycamel::ConcurrentQueue; - using ChunkQueue = moodycamel::ConcurrentQueue; - - void Free(uint8_t* const chunk) noexcept; - std::atomic mChunkCount { 0 }; - ChunkQueue mChunkPool {}; - ChunkQueue mChunkFreeQueue {}; + void free(uint8_t *const chunk) noexcept; + std::atomic mChunkCount{0}; + ChunkQueue mChunkPool{}; + ChunkQueue mChunkFreeQueue{}; }; - uint8_t* AllocateImpl(uint32_t& allocatedSize, uint32_t const requestSize) noexcept; - void PushCommands() noexcept; + uint8_t *allocateImpl(uint32_t &allocatedSize, uint32_t const requestSize) noexcept; + void pushCommands() noexcept; // 在消费者线程执行 - void PullCommands() noexcept; - void ExecuteCommand() noexcept; - Command* ReadCommand() noexcept; - inline bool HasNewCommand() const noexcept { return mR.mNewCommandCount > 0 && ! mR.mFlushingFinished; } - void ConsumerThreadLoop() noexcept; + void pullCommands() noexcept; + void executeCommand() noexcept; + Command *readCommand() noexcept; + inline bool hasNewCommand() const noexcept { return mR.mNewCommandCount > 0 && !mR.mFlushingFinished; } + void consumerThreadLoop() noexcept; - WriterContext mW; - ReaderContext mR; - EventCV mN; - bool mImmediateMode { true }; - bool mWorkerAttached { false }; - bool mFreeChunksByUser { true }; // 被回收的Chunk会被记录到一个队列里 由用户在生产者线程选择合适的时机来Free + WriterContext mW; + ReaderContext mR; + EventCV mN; + bool mImmediateMode{true}; + bool mWorkerAttached{false}; + bool mFreeChunksByUser{true}; // 被回收的Chunk会被记录到一个队列里 由用户在生产者线程选择合适的时机来Free friend class MemoryChunkSwitchCommand; }; -class DummyCommand final : public Command -{ +class DummyCommand final : public Command { public: - - virtual void Execute() noexcept override {} - virtual char const* GetName() const noexcept override; + virtual void execute() noexcept override {} + virtual char const *getName() const noexcept override; }; -class MemoryChunkSwitchCommand final : public Command -{ +class MemoryChunkSwitchCommand final : public Command { public: - - MemoryChunkSwitchCommand(CommandEncoder* const cb, uint8_t* const newChunk, uint8_t* const oldChunk) noexcept; + MemoryChunkSwitchCommand(CommandEncoder *const cb, uint8_t *const newChunk, uint8_t *const oldChunk) noexcept; ~MemoryChunkSwitchCommand(); - virtual void Execute() noexcept override; - virtual char const* GetName() const noexcept override; + virtual void execute() noexcept override; + virtual char const *getName() const noexcept override; private: - - CommandEncoder* mCommandBuffer { nullptr }; - uint8_t* mNewChunk { nullptr }; - uint8_t* mOldChunk { nullptr }; + CommandEncoder *mCommandBuffer{nullptr}; + uint8_t *mNewChunk{nullptr}; + uint8_t *mOldChunk{nullptr}; }; template -std::enable_if_t::value, T*> -CommandEncoder::Allocate(uint32_t const count) noexcept -{ +std::enable_if_t::value, T *> +CommandEncoder::allocate(uint32_t const count) noexcept { uint32_t allocatedSize = 0; - T* const cmd = reinterpret_cast(AllocateImpl(allocatedSize, sizeof(T))); - cmd->mNext = reinterpret_cast(mW.mCurrentMemoryChunk + mW.mOffset); + T *const cmd = reinterpret_cast(allocateImpl(allocatedSize, sizeof(T))); + cmd->mNext = reinterpret_cast(mW.mCurrentMemoryChunk + mW.mOffset); ++mW.mPendingCommandCount; mW.mLastCommand = cmd; return cmd; } template -std::enable_if_t::value, T*> -CommandEncoder::Allocate(uint32_t const count) noexcept -{ +std::enable_if_t::value, T *> +CommandEncoder::allocate(uint32_t const count) noexcept { uint32_t const requestSize = sizeof(T) * count; assert(requestSize); uint32_t allocatedSize = 0; - uint8_t* const allocatedMemory = AllocateImpl(allocatedSize, requestSize); - mW.mLastCommand->mNext = reinterpret_cast(mW.mCurrentMemoryChunk + mW.mOffset); - return reinterpret_cast(allocatedMemory); + uint8_t *const allocatedMemory = allocateImpl(allocatedSize, requestSize); + mW.mLastCommand->mNext = reinterpret_cast(mW.mCurrentMemoryChunk + mW.mOffset); + return reinterpret_cast(allocatedMemory); } template -T* CommandEncoder::AllocateAndCopy(uint32_t const count, void const* data) noexcept -{ - T* const allocatedMemory = Allocate(count); +T *CommandEncoder::allocateAndCopy(uint32_t const count, void const *data) noexcept { + T *const allocatedMemory = Allocate(count); memcpy(allocatedMemory, data, sizeof(T) * count); return allocatedMemory; } template -T* CommandEncoder::AllocateAndZero(uint32_t const count) noexcept -{ - T* const allocatedMemory = Allocate(count); +T *CommandEncoder::allocateAndZero(uint32_t const count) noexcept { + T *const allocatedMemory = Allocate(count); memset(allocatedMemory, 0, sizeof(T) * count); return allocatedMemory; } // 生产者线程用来向CommandBuffer填充Command的工具宏 -#define WRITE_COMMAND(CB, CommandName, Params) \ - { \ - if (! CB->IsImmediateMode()) \ - { \ - new (CB->Allocate(1)) CommandName Params; \ - } \ - else \ - { \ - CommandName command Params; \ - command.Execute(); \ - } \ +#define WRITE_COMMAND(CB, CommandName, Params) \ + { \ + if (!CB->isImmediateMode()) { \ + new (CB->allocate(1)) CommandName Params; \ + } else { \ + CommandName command Params; \ + command.execute(); \ + } \ } #define ENCODE_COMMAND_0(CB, CommandName, Code) \ - class CommandName final : public Command \ { \ - public: \ - virtual void Execute() noexcept override \ - { \ - Code; \ - } \ - virtual char const* GetName() const noexcept override \ - { \ - return (#CommandName); \ - } \ - }; \ - WRITE_COMMAND(CB, CommandName, ); + class CommandName final : public Command { \ + public: \ + virtual void execute() noexcept override { \ + Code \ + } \ + virtual char const *getName() const noexcept override { \ + return (#CommandName); \ + } \ + }; \ + WRITE_COMMAND(CB, CommandName, ); \ + } #define ENCODE_COMMAND_1(CB, CommandName, \ - Param1, Value1, \ - Code) { \ + Param1, Value1, \ + Code) \ + { \ \ - using Type1 = std::decay::type; \ + using Type1 = std::decay::type; \ \ - class CommandName final : public Command \ - { \ - public: \ - explicit CommandName(Type1 const& In##Param1) \ - : Param1(In##Param1) \ - { \ - } \ - virtual void Execute() noexcept override \ - { \ - Code; \ - } \ - virtual char const* GetName() const noexcept override \ - { \ - return (#CommandName); \ - } \ - private: \ - Type1 Param1; \ - }; \ - WRITE_COMMAND(CB, CommandName, (Value1)) }; + class CommandName final : public Command { \ + public: \ + explicit CommandName(Type1 const &In##Param1) \ + : Param1(In##Param1) { \ + } \ + virtual void execute() noexcept override { \ + Code \ + } \ + virtual char const *getName() const noexcept override { \ + return (#CommandName); \ + } \ + \ + private: \ + Type1 Param1; \ + }; \ + WRITE_COMMAND(CB, CommandName, (Value1)) \ + } #define ENCODE_COMMAND_2(CB, CommandName, \ - Param1, Value1, \ - Param2, Value2, \ - Code) { \ + Param1, Value1, \ + Param2, Value2, \ + Code) \ + { \ \ - using Type1 = std::decay::type; \ - using Type2 = std::decay::type; \ + using Type1 = std::decay::type; \ + using Type2 = std::decay::type; \ \ - class CommandName final : public Command \ - { \ - public: \ - CommandName( \ - Type1 const& In##Param1 \ - , Type2 const& In##Param2 \ - ) \ - : Param1(In##Param1) \ - , Param2(In##Param2) \ - { \ - } \ - virtual void Execute() noexcept override \ - { \ - Code; \ - } \ - virtual char const* GetName() const noexcept override \ - { \ - return (#CommandName); \ - } \ - private: \ - Type1 Param1; \ - Type2 Param2; \ - }; \ - WRITE_COMMAND(CB, CommandName, (Value1, Value2)) }; + class CommandName final : public Command { \ + public: \ + CommandName( \ + Type1 const &In##Param1, Type2 const &In##Param2) \ + : Param1(In##Param1), Param2(In##Param2) { \ + } \ + virtual void execute() noexcept override { \ + Code \ + } \ + virtual char const *getName() const noexcept override { \ + return (#CommandName); \ + } \ + \ + private: \ + Type1 Param1; \ + Type2 Param2; \ + }; \ + WRITE_COMMAND(CB, CommandName, (Value1, Value2)) \ + } #define ENCODE_COMMAND_3(CB, CommandName, \ - Param1, Value1, \ - Param2, Value2, \ - Param3, Value3, \ - Code) { \ + Param1, Value1, \ + Param2, Value2, \ + Param3, Value3, \ + Code) \ + { \ \ - using Type1 = std::decay::type; \ - using Type2 = std::decay::type; \ - using Type3 = std::decay::type; \ + using Type1 = std::decay::type; \ + using Type2 = std::decay::type; \ + using Type3 = std::decay::type; \ \ - class CommandName final : public Command \ - { \ - public: \ - CommandName( \ - Type1 const &In##Param1 \ - , Type2 const &In##Param2 \ - , Type3 const &In##Param3 \ - ) \ - : Param1(In##Param1) \ - , Param2(In##Param2) \ - , Param3(In##Param3) \ - { \ - } \ - virtual void Execute() noexcept override \ - { \ - Code; \ - } \ - virtual char const* GetName() const noexcept override \ - { \ - return (#CommandName); \ - } \ - private: \ - Type1 Param1; \ - Type2 Param2; \ - Type3 Param3; \ - }; \ - WRITE_COMMAND(CB, CommandName, (Value1, Value2, Value3)) }; + class CommandName final : public Command { \ + public: \ + CommandName( \ + Type1 const &In##Param1, \ + Type2 const &In##Param2, \ + Type3 const &In##Param3) \ + : Param1(In##Param1), \ + Param2(In##Param2), \ + Param3(In##Param3) { \ + } \ + virtual void execute() noexcept override { \ + Code \ + } \ + virtual char const *getName() const noexcept override { \ + return (#CommandName); \ + } \ + \ + private: \ + Type1 Param1; \ + Type2 Param2; \ + Type3 Param3; \ + }; \ + WRITE_COMMAND(CB, CommandName, \ + (Value1, \ + Value2, \ + Value3)) \ + } #define ENCODE_COMMAND_4(CB, CommandName, \ - Param1, Value1, \ - Param2, Value2, \ - Param3, Value3, \ - Param4, Value4, \ - Code) { \ + Param1, Value1, \ + Param2, Value2, \ + Param3, Value3, \ + Param4, Value4, \ + Code) \ + { \ \ - using Type1 = std::decay::type; \ - using Type2 = std::decay::type; \ - using Type3 = std::decay::type; \ - using Type4 = std::decay::type; \ + using Type1 = std::decay::type; \ + using Type2 = std::decay::type; \ + using Type3 = std::decay::type; \ + using Type4 = std::decay::type; \ \ - class CommandName : public Command \ - { \ - public: \ - CommandName( \ - Type1 const &In##Param1 \ - , Type2 const &In##Param2 \ - , Type3 const &In##Param3 \ - , Type4 const &In##Param4 \ - ) \ - : Param1(In##Param1) \ - , Param2(In##Param2) \ - , Param3(In##Param3) \ - , Param4(In##Param4) \ - { \ - } \ - virtual void Execute() noexcept override \ - { \ - Code; \ - } \ - virtual char const* GetName() const noexcept override \ - { \ - return (#CommandName); \ - } \ - private: \ - Type1 Param1; \ - Type2 Param2; \ - Type3 Param3; \ - Type4 Param4; \ - }; \ - WRITE_COMMAND(CB, CommandName, (Value1, Value2, Value3, Value4)) }; + class CommandName : public Command { \ + public: \ + CommandName( \ + Type1 const &In##Param1, \ + Type2 const &In##Param2, \ + Type3 const &In##Param3, \ + Type4 const &In##Param4) \ + : Param1(In##Param1), \ + Param2(In##Param2), \ + Param3(In##Param3), \ + Param4(In##Param4) { \ + } \ + virtual void execute() noexcept override { \ + Code \ + } \ + virtual char const *getName() const noexcept override { \ + return (#CommandName); \ + } \ + \ + private: \ + Type1 Param1; \ + Type2 Param2; \ + Type3 Param3; \ + Type4 Param4; \ + }; \ + WRITE_COMMAND(CB, CommandName, \ + (Value1, \ + Value2, \ + Value3, \ + Value4)) \ + } #define ENCODE_COMMAND_5(CB, CommandName, \ - Param1, Value1, \ - Param2, Value2, \ - Param3, Value3, \ - Param4, Value4, \ - Param5, Value5, \ - Code) { \ + Param1, Value1, \ + Param2, Value2, \ + Param3, Value3, \ + Param4, Value4, \ + Param5, Value5, \ + Code) \ + { \ \ - using Type1 = std::decay::type; \ - using Type2 = std::decay::type; \ - using Type3 = std::decay::type; \ - using Type4 = std::decay::type; \ - using Type5 = std::decay::type; \ + using Type1 = std::decay::type; \ + using Type2 = std::decay::type; \ + using Type3 = std::decay::type; \ + using Type4 = std::decay::type; \ + using Type5 = std::decay::type; \ \ - class CommandName : public Command \ - { \ - public: \ - CommandName( \ - Type1 const &In##Param1 \ - , Type2 const &In##Param2 \ - , Type3 const &In##Param3 \ - , Type4 const &In##Param4 \ - , Type5 const &In##Param5 \ - ) \ - : Param1(In##Param1) \ - , Param2(In##Param2) \ - , Param3(In##Param3) \ - , Param4(In##Param4) \ - , Param5(In##Param5) \ - { \ - } \ - virtual void Execute() noexcept override \ - { \ - Code; \ - } \ - virtual char const* GetName() const noexcept override \ - { \ - return (#CommandName); \ - } \ - private: \ - Type1 Param1; \ - Type2 Param2; \ - Type3 Param3; \ - Type4 Param4; \ - Type5 Param5; \ - }; \ - WRITE_COMMAND(CB, CommandName, (Value1, Value2, Value3, Value4, Value5)) }; + class CommandName : public Command { \ + public: \ + CommandName( \ + Type1 const &In##Param1, \ + Type2 const &In##Param2, \ + Type3 const &In##Param3, \ + Type4 const &In##Param4, \ + Type5 const &In##Param5) \ + : Param1(In##Param1), \ + Param2(In##Param2), \ + Param3(In##Param3), \ + Param4(In##Param4), \ + Param5(In##Param5) { \ + } \ + virtual void execute() noexcept override { \ + Code \ + } \ + virtual char const *getName() const noexcept override { \ + return (#CommandName); \ + } \ + \ + private: \ + Type1 Param1; \ + Type2 Param2; \ + Type3 Param3; \ + Type4 Param4; \ + Type5 Param5; \ + }; \ + WRITE_COMMAND(CB, CommandName, \ + (Value1, \ + Value2, \ + Value3, \ + Value4, \ + Value5)) \ + } #define ENCODE_COMMAND_6(CB, CommandName, \ - Param1, Value1, \ - Param2, Value2, \ - Param3, Value3, \ - Param4, Value4, \ - Param5, Value5, \ - Param6, Value6, \ - Code) { \ + Param1, Value1, \ + Param2, Value2, \ + Param3, Value3, \ + Param4, Value4, \ + Param5, Value5, \ + Param6, Value6, \ + Code) \ + { \ \ - using Type1 = std::decay::type; \ - using Type2 = std::decay::type; \ - using Type3 = std::decay::type; \ - using Type4 = std::decay::type; \ - using Type5 = std::decay::type; \ - using Type6 = std::decay::type; \ + using Type1 = std::decay::type; \ + using Type2 = std::decay::type; \ + using Type3 = std::decay::type; \ + using Type4 = std::decay::type; \ + using Type5 = std::decay::type; \ + using Type6 = std::decay::type; \ \ - class CommandName : public Command \ - { \ - public: \ - CommandName( \ - Type1 const &In##Param1 \ - , Type2 const &In##Param2 \ - , Type3 const &In##Param3 \ - , Type4 const &In##Param4 \ - , Type5 const &In##Param5 \ - , Type6 const &In##Param6 \ - ) \ - : Param1(In##Param1) \ - , Param2(In##Param2) \ - , Param3(In##Param3) \ - , Param4(In##Param4) \ - , Param5(In##Param5) \ - , Param6(In##Param6) \ - { \ - } \ - virtual void Execute() noexcept override \ - { \ - Code; \ - } \ - virtual char const* GetName() const noexcept override \ - { \ - return (#CommandName); \ - } \ - private: \ - Type1 Param1; \ - Type2 Param2; \ - Type3 Param3; \ - Type4 Param4; \ - Type5 Param5; \ - Type6 Param6; \ - }; \ - WRITE_COMMAND(CB, CommandName, (Value1, Value2, Value3, Value4, Value5, Value6)) }; + class CommandName : public Command { \ + public: \ + CommandName( \ + Type1 const &In##Param1, \ + Type2 const &In##Param2, \ + Type3 const &In##Param3, \ + Type4 const &In##Param4, \ + Type5 const &In##Param5, \ + Type6 const &In##Param6) \ + : Param1(In##Param1), \ + Param2(In##Param2), \ + Param3(In##Param3), \ + Param4(In##Param4), \ + Param5(In##Param5), \ + Param6(In##Param6) { \ + } \ + virtual void execute() noexcept override { \ + Code \ + } \ + virtual char const *getName() const noexcept override { \ + return (#CommandName); \ + } \ + \ + private: \ + Type1 Param1; \ + Type2 Param2; \ + Type3 Param3; \ + Type4 Param4; \ + Type5 Param5; \ + Type6 Param6; \ + }; \ + WRITE_COMMAND(CB, CommandName, \ + (Value1, \ + Value2, \ + Value3, \ + Value4, \ + Value5, \ + Value6)) \ + } #define ENCODE_COMMAND_7(CB, CommandName, \ - Param1, Value1, \ - Param2, Value2, \ - Param3, Value3, \ - Param4, Value4, \ - Param5, Value5, \ - Param6, Value6, \ - Param7, Value7, \ - Code) { \ + Param1, Value1, \ + Param2, Value2, \ + Param3, Value3, \ + Param4, Value4, \ + Param5, Value5, \ + Param6, Value6, \ + Param7, Value7, \ + Code) \ + { \ \ - using Type1 = std::decay::type; \ - using Type2 = std::decay::type; \ - using Type3 = std::decay::type; \ - using Type4 = std::decay::type; \ - using Type5 = std::decay::type; \ - using Type6 = std::decay::type; \ - using Type7 = std::decay::type; \ + using Type1 = std::decay::type; \ + using Type2 = std::decay::type; \ + using Type3 = std::decay::type; \ + using Type4 = std::decay::type; \ + using Type5 = std::decay::type; \ + using Type6 = std::decay::type; \ + using Type7 = std::decay::type; \ \ - class CommandName : public Command \ - { \ - public: \ - CommandName( \ - Type1 const &In##Param1 \ - , Type2 const &In##Param2 \ - , Type3 const &In##Param3 \ - , Type4 const &In##Param4 \ - , Type5 const &In##Param5 \ - , Type6 const &In##Param6 \ - , Type7 const &In##Param7 \ - ) \ - : Param1(In##Param1) \ - , Param2(In##Param2) \ - , Param3(In##Param3) \ - , Param4(In##Param4) \ - , Param5(In##Param5) \ - , Param6(In##Param6) \ - , Param7(In##Param7) \ - { \ - } \ - virtual void Execute() noexcept override \ - { \ - Code; \ - } \ - virtual const char* GetName() const noexcept override \ - { \ - return (#CommandName); \ - } \ - private: \ - Type1 Param1; \ - Type2 Param2; \ - Type3 Param3; \ - Type4 Param4; \ - Type5 Param5; \ - Type6 Param6; \ - Type7 Param7; \ - }; \ - WRITE_COMMAND(CB, CommandName, (Value1, Value2, Value3, Value4, Value5, Value6, Value7)) }; + class CommandName : public Command { \ + public: \ + CommandName( \ + Type1 const &In##Param1, \ + Type2 const &In##Param2, \ + Type3 const &In##Param3, \ + Type4 const &In##Param4, \ + Type5 const &In##Param5, \ + Type6 const &In##Param6, \ + Type7 const &In##Param7) \ + : Param1(In##Param1), \ + Param2(In##Param2), \ + Param3(In##Param3), \ + Param4(In##Param4), \ + Param5(In##Param5), \ + Param6(In##Param6), \ + Param7(In##Param7) { \ + } \ + virtual void execute() noexcept override { \ + Code \ + } \ + virtual const char *getName() const noexcept override { \ + return (#CommandName); \ + } \ + \ + private: \ + Type1 Param1; \ + Type2 Param2; \ + Type3 Param3; \ + Type4 Param4; \ + Type5 Param5; \ + Type6 Param6; \ + Type7 Param7; \ + }; \ + WRITE_COMMAND(CB, CommandName, \ + (Value1, \ + Value2, \ + Value3, \ + Value4, \ + Value5, \ + Value6, \ + Value7)) \ + } #define ENCODE_COMMAND_8(CB, CommandName, \ - Param1, Value1, \ - Param2, Value2, \ - Param3, Value3, \ - Param4, Value4, \ - Param5, Value5, \ - Param6, Value6, \ - Param7, Value7, \ - Param8, Value8, \ - Code) { \ + Param1, Value1, \ + Param2, Value2, \ + Param3, Value3, \ + Param4, Value4, \ + Param5, Value5, \ + Param6, Value6, \ + Param7, Value7, \ + Param8, Value8, \ + Code) \ + { \ \ - using Type1 = std::decay::type; \ - using Type2 = std::decay::type; \ - using Type3 = std::decay::type; \ - using Type4 = std::decay::type; \ - using Type5 = std::decay::type; \ - using Type6 = std::decay::type; \ - using Type7 = std::decay::type; \ - using Type8 = std::decay::type; \ + using Type1 = std::decay::type; \ + using Type2 = std::decay::type; \ + using Type3 = std::decay::type; \ + using Type4 = std::decay::type; \ + using Type5 = std::decay::type; \ + using Type6 = std::decay::type; \ + using Type7 = std::decay::type; \ + using Type8 = std::decay::type; \ \ - class CommandName : public Command \ - { \ - public: \ - CommandName( \ - Type1 const &In##Param1 \ - , Type2 const &In##Param2 \ - , Type3 const &In##Param3 \ - , Type4 const &In##Param4 \ - , Type5 const &In##Param5 \ - , Type6 const &In##Param6 \ - , Type7 const &In##Param7 \ - , Type8 const &In##Param8 \ - ) \ - : Param1(In##Param1) \ - , Param2(In##Param2) \ - , Param3(In##Param3) \ - , Param4(In##Param4) \ - , Param5(In##Param5) \ - , Param6(In##Param6) \ - , Param7(In##Param7) \ - , Param8(In##Param8) \ - { \ - } \ - virtual void Execute() noexcept override \ - { \ - Code; \ - } \ - virtual char const* GetName() const noexcept override \ - { \ - return (#CommandName); \ - } \ - private: \ - Type1 Param1; \ - Type2 Param2; \ - Type3 Param3; \ - Type4 Param4; \ - Type5 Param5; \ - Type6 Param6; \ - Type7 Param7; \ - Type8 Param8; \ - }; \ - WRITE_COMMAND(CB, CommandName, (Value1, Value2, Value3, Value4, Value5, Value6, Value7, Value8)) }; + class CommandName : public Command { \ + public: \ + CommandName(Type1 const &In##Param1, \ + Type2 const &In##Param2, \ + Type3 const &In##Param3, \ + Type4 const &In##Param4, \ + Type5 const &In##Param5, \ + Type6 const &In##Param6, \ + Type7 const &In##Param7, \ + Type8 const &In##Param8) \ + : Param1(In##Param1), \ + Param2(In##Param2), \ + Param3(In##Param3), \ + Param4(In##Param4), \ + Param5(In##Param5), \ + Param6(In##Param6), \ + Param7(In##Param7), \ + Param8(In##Param8) { \ + } \ + virtual void execute() noexcept override { \ + Code \ + } \ + virtual char const *getName() const noexcept override { \ + return (#CommandName); \ + } \ + \ + private: \ + Type1 Param1; \ + Type2 Param2; \ + Type3 Param3; \ + Type4 Param4; \ + Type5 Param5; \ + Type6 Param6; \ + Type7 Param7; \ + Type8 Param8; \ + }; \ + WRITE_COMMAND(CB, CommandName, \ + (Value1, \ + Value2, \ + Value3, \ + Value4, \ + Value5, \ + Value6, \ + Value7, \ + Value8)) \ + } } // namespace cc diff --git a/native/cocos/renderer/core/gfx-agent/GFXBufferAgent.cpp b/native/cocos/renderer/core/gfx-agent/GFXBufferAgent.cpp index 51f4602cd3..87b635b5e7 100644 --- a/native/cocos/renderer/core/gfx-agent/GFXBufferAgent.cpp +++ b/native/cocos/renderer/core/gfx-agent/GFXBufferAgent.cpp @@ -67,7 +67,7 @@ void BufferAgent::destroy() { void BufferAgent::update(void *buffer, uint size) { CommandEncoder *encoder = ((DeviceAgent *)_device)->getMainEncoder(); - uint8_t *actorBuffer = encoder->Allocate(size); + uint8_t *actorBuffer = encoder->allocate(size); memcpy(actorBuffer, buffer, size); ENCODE_COMMAND_3( diff --git a/native/cocos/renderer/core/gfx-agent/GFXCommandBufferAgent.cpp b/native/cocos/renderer/core/gfx-agent/GFXCommandBufferAgent.cpp index 1a881c683c..c94e33f82b 100644 --- a/native/cocos/renderer/core/gfx-agent/GFXCommandBufferAgent.cpp +++ b/native/cocos/renderer/core/gfx-agent/GFXCommandBufferAgent.cpp @@ -18,11 +18,11 @@ namespace gfx { void CommandBufferAgent::initEncoder() { _encoder = CC_NEW(CommandEncoder); - _encoder->SetImmediateMode(false); + _encoder->setImmediateMode(false); } void CommandBufferAgent::destroyEncoder() { - ((DeviceAgent *)_device)->getMainEncoder()->KickAndWait(); + ((DeviceAgent *)_device)->getMainEncoder()->kickAndWait(); CC_SAFE_DELETE(_encoder); } @@ -84,8 +84,8 @@ void CommandBufferAgent::end() { { actor->end(); }); - CommandEncoder::FreeChunksInFreeQueue(_encoder); - _encoder->FinishWriting(); + CommandEncoder::freeChunksInFreeQueue(_encoder); + _encoder->finishWriting(); } void CommandBufferAgent::beginRenderPass(RenderPass *renderPass, Framebuffer *fbo, const Rect &renderArea, const Color *colors, float depth, int stencil, bool fromSecondaryCB) { @@ -93,7 +93,7 @@ void CommandBufferAgent::beginRenderPass(RenderPass *renderPass, Framebuffer *fb uint attachmentCount = (uint)renderPass->getColorAttachments().size(); Color *actorColors = nullptr; if (attachmentCount) { - actorColors = _encoder->Allocate(attachmentCount); + actorColors = _encoder->allocate(attachmentCount); memcpy(actorColors, colors, sizeof(Color) * attachmentCount); } @@ -137,7 +137,7 @@ void CommandBufferAgent::bindPipelineState(PipelineState *pso) { void CommandBufferAgent::bindDescriptorSet(uint set, DescriptorSet *descriptorSet, uint dynamicOffsetCount, const uint *dynamicOffsets) { uint *actorDynamicOffsets = nullptr; if (dynamicOffsetCount) { - actorDynamicOffsets = _encoder->Allocate(dynamicOffsetCount); + actorDynamicOffsets = _encoder->allocate(dynamicOffsetCount); memcpy(actorDynamicOffsets, dynamicOffsets, dynamicOffsetCount * sizeof(uint)); } @@ -273,7 +273,7 @@ void CommandBufferAgent::draw(InputAssembler *ia) { void CommandBufferAgent::updateBuffer(Buffer *buff, const void *data, uint size) { CommandEncoder *encoder = _encoder; - uint8_t *actorData = encoder->Allocate(size); + uint8_t *actorData = encoder->allocate(size); memcpy(actorData, data, size); ENCODE_COMMAND_4( @@ -289,19 +289,19 @@ void CommandBufferAgent::updateBuffer(Buffer *buff, const void *data, uint size) } void CommandBufferAgent::copyBuffersToTexture(const uint8_t *const *buffers, Texture *texture, const BufferTextureCopy *regions, uint count) { - BufferTextureCopy *actorRegions = _encoder->Allocate(count); + BufferTextureCopy *actorRegions = _encoder->allocate(count); memcpy(actorRegions, regions, count * sizeof(BufferTextureCopy)); uint bufferCount = 0u; for (uint i = 0u; i < count; i++) { bufferCount += regions[i].texSubres.layerCount; } - const uint8_t **actorBuffers = _encoder->Allocate(bufferCount); + const uint8_t **actorBuffers = _encoder->allocate(bufferCount); for (uint i = 0u, n = 0u; i < count; i++) { const BufferTextureCopy ®ion = regions[i]; uint size = FormatSize(texture->getFormat(), region.texExtent.width, region.texExtent.height, 1); for (uint l = 0; l < region.texSubres.layerCount; l++) { - uint8_t *buffer = _encoder->Allocate(size); + uint8_t *buffer = _encoder->allocate(size); memcpy(buffer, buffers[n], size); actorBuffers[n++] = buffer; } @@ -323,7 +323,7 @@ void CommandBufferAgent::copyBuffersToTexture(const uint8_t *const *buffers, Tex void CommandBufferAgent::execute(const CommandBuffer *const *cmdBuffs, uint32_t count) { if (!count) return; - const CommandBuffer **actorCmdBuffs = _encoder->Allocate(count); + const CommandBuffer **actorCmdBuffs = _encoder->allocate(count); for (uint i = 0; i < count; ++i) { actorCmdBuffs[i] = ((CommandBufferAgent *)cmdBuffs[i])->getActor(); } @@ -343,18 +343,18 @@ void CommandBufferAgent::execute(const CommandBuffer *const *cmdBuffs, uint32_t if (multiThreaded) { JobGraph g(JobSystem::getInstance()); uint job = g.createForEachIndexJob(1u, count, 1u, [this](uint i) { - ((CommandBufferAgent *)cmdBuffs[i])->getEncoder()->FlushCommands(); + ((CommandBufferAgent *)cmdBuffs[i])->getEncoder()->flushCommands(); }); g.run(job); - ((CommandBufferAgent *)cmdBuffs[0])->getEncoder()->FlushCommands(); + ((CommandBufferAgent *)cmdBuffs[0])->getEncoder()->flushCommands(); g.waitForAll(); } else { for (uint i = 0u; i < count; ++i) { - ((CommandBufferAgent *)cmdBuffs[i])->getEncoder()->FlushCommands(); + ((CommandBufferAgent *)cmdBuffs[i])->getEncoder()->flushCommands(); } } } else { - ((CommandBufferAgent *)cmdBuffs[0])->getEncoder()->FlushCommands(); + ((CommandBufferAgent *)cmdBuffs[0])->getEncoder()->flushCommands(); } actor->execute(actorCmdBuffs, count); }); diff --git a/native/cocos/renderer/core/gfx-agent/GFXDeviceAgent.cpp b/native/cocos/renderer/core/gfx-agent/GFXDeviceAgent.cpp index 74a94662ba..677c262b9f 100644 --- a/native/cocos/renderer/core/gfx-agent/GFXDeviceAgent.cpp +++ b/native/cocos/renderer/core/gfx-agent/GFXDeviceAgent.cpp @@ -65,7 +65,7 @@ bool DeviceAgent::initialize(const DeviceInfo &info) { memcpy(_features, _actor->_features, (uint)Feature::COUNT * sizeof(bool)); _mainEncoder = CC_NEW(CommandEncoder); - _mainEncoder->RunConsumerThread(); + _mainEncoder->runConsumerThread(); setMultithreaded(true); @@ -89,7 +89,7 @@ void DeviceAgent::destroy() { CC_SAFE_DELETE(_cmdBuff); CC_SAFE_DELETE(_queue); - _mainEncoder->TerminateConsumerThread(); + _mainEncoder->terminateConsumerThread(); CC_SAFE_DELETE(_mainEncoder); } @@ -132,8 +132,8 @@ void DeviceAgent::present() { frameBoundarySemaphore->Signal(); }); - CommandEncoder::FreeChunksInFreeQueue(encoder); - encoder->FinishWriting(); + CommandEncoder::freeChunksInFreeQueue(encoder); + encoder->finishWriting(); } void DeviceAgent::setMultithreaded(bool multithreaded) { @@ -141,7 +141,7 @@ void DeviceAgent::setMultithreaded(bool multithreaded) { _multithreaded = multithreaded; if (multithreaded) { - _mainEncoder->SetImmediateMode(false); + _mainEncoder->setImmediateMode(false); _actor->bindRenderContext(false); ENCODE_COMMAND_1( _mainEncoder, DeviceMakeCurrent, @@ -156,8 +156,8 @@ void DeviceAgent::setMultithreaded(bool multithreaded) { { actor->bindDeviceContext(false); }); - _mainEncoder->FinishWriting(true); // wait till finished - _mainEncoder->SetImmediateMode(true); + _mainEncoder->finishWriting(true); // wait till finished + _mainEncoder->setImmediateMode(true); _actor->bindRenderContext(true); } } @@ -247,19 +247,19 @@ PipelineState *DeviceAgent::createPipelineState() { void DeviceAgent::copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint count) { CommandEncoder *encoder = getMainEncoder(); - BufferTextureCopy *actorRegions = encoder->Allocate(count); + BufferTextureCopy *actorRegions = encoder->allocate(count); memcpy(actorRegions, regions, count * sizeof(BufferTextureCopy)); uint bufferCount = 0u; for (uint i = 0u; i < count; i++) { bufferCount += regions[i].texSubres.layerCount; } - const uint8_t **actorBuffers = encoder->Allocate(bufferCount); + const uint8_t **actorBuffers = encoder->allocate(bufferCount); for (uint i = 0u, n = 0u; i < count; i++) { const BufferTextureCopy ®ion = regions[i]; uint size = FormatSize(dst->getFormat(), region.texExtent.width, region.texExtent.height, 1); for (uint l = 0; l < region.texSubres.layerCount; l++) { - uint8_t *buffer = encoder->Allocate(size); + uint8_t *buffer = encoder->allocate(size); memcpy(buffer, buffers[n], size); actorBuffers[n++] = buffer; } diff --git a/native/cocos/renderer/core/gfx-agent/GFXQueueAgent.cpp b/native/cocos/renderer/core/gfx-agent/GFXQueueAgent.cpp index 2141692226..7fe5132354 100644 --- a/native/cocos/renderer/core/gfx-agent/GFXQueueAgent.cpp +++ b/native/cocos/renderer/core/gfx-agent/GFXQueueAgent.cpp @@ -42,7 +42,7 @@ void QueueAgent::submit(const CommandBuffer *const *cmdBuffs, uint count, Fence if (!count) return; CommandEncoder *encoder = ((DeviceAgent *)_device)->getMainEncoder(); - const CommandBuffer **actorCmdBuffs = encoder->Allocate(count); + const CommandBuffer **actorCmdBuffs = encoder->allocate(count); for (uint i = 0u; i < count; ++i) { actorCmdBuffs[i] = ((CommandBufferAgent *)cmdBuffs[i])->getActor(); } @@ -64,18 +64,18 @@ void QueueAgent::submit(const CommandBuffer *const *cmdBuffs, uint count, Fence if (multiThreaded) { JobGraph g(JobSystem::getInstance()); uint job = g.createForEachIndexJob(1u, count, 1u, [this](uint i) { - ((CommandBufferAgent *)cmdBuffs[i])->getEncoder()->FlushCommands(); + ((CommandBufferAgent *)cmdBuffs[i])->getEncoder()->flushCommands(); }); g.run(job); - ((CommandBufferAgent *)cmdBuffs[0])->getEncoder()->FlushCommands(); + ((CommandBufferAgent *)cmdBuffs[0])->getEncoder()->flushCommands(); g.waitForAll(); } else { for (uint i = 0u; i < count; ++i) { - ((CommandBufferAgent *)cmdBuffs[i])->getEncoder()->FlushCommands(); + ((CommandBufferAgent *)cmdBuffs[i])->getEncoder()->flushCommands(); } } } else { - ((CommandBufferAgent *)cmdBuffs[0])->getEncoder()->FlushCommands(); + ((CommandBufferAgent *)cmdBuffs[0])->getEncoder()->flushCommands(); } //CC_LOG_INFO("======== one round ========"); //auto endTime = std::chrono::steady_clock::now(); diff --git a/native/cocos/renderer/core/gfx/GFXCommandBuffer.h b/native/cocos/renderer/core/gfx/GFXCommandBuffer.h index 06bd0c747f..352ec366a2 100755 --- a/native/cocos/renderer/core/gfx/GFXCommandBuffer.h +++ b/native/cocos/renderer/core/gfx/GFXCommandBuffer.h @@ -37,9 +37,11 @@ public: CC_INLINE void begin() { begin(nullptr, 0, nullptr, -1); } CC_INLINE void begin(int submitIndex) { begin(nullptr, 0, nullptr, submitIndex); } + // secondary command buffer specific CC_INLINE void begin(RenderPass *renderPass) { begin(renderPass, 0, nullptr, -1); } CC_INLINE void begin(RenderPass *renderPass, uint subpass) { begin(renderPass, subpass, nullptr, -1); } CC_INLINE void begin(RenderPass *renderPass, uint subpass, Framebuffer *frameBuffer) { begin(renderPass, subpass, frameBuffer, -1); } + CC_INLINE void updateBuffer(Buffer *buff, const void *data) { updateBuffer(buff, data, buff->getSize()); } CC_INLINE void execute(const CommandBufferList &cmdBuffs, uint32_t count) { execute(cmdBuffs.data(), count); } CC_INLINE void bindDescriptorSet(uint set, DescriptorSet *descriptorSet) { bindDescriptorSet(set, descriptorSet, 0, nullptr); }