diff --git a/native/cocos/base/threading/CommandEncoder.cpp b/native/cocos/base/threading/CommandEncoder.cpp index 17521317ad..360aff36a9 100644 --- a/native/cocos/base/threading/CommandEncoder.cpp +++ b/native/cocos/base/threading/CommandEncoder.cpp @@ -66,7 +66,7 @@ CommandEncoder::CommandEncoder() { mW.mCurrentMemoryChunk = chunk; mR.mCurrentMemoryChunk = chunk; - // 分配一个头节点 不要被Execute + // sentinel node will not be executed Command *const cmd = allocate(1); pushCommands(); pullCommands(); @@ -157,7 +157,7 @@ void CommandEncoder::freeChunksInFreeQueue(CommandEncoder *const mainCommandbuff 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填满了 + assert(alignedSize + kSwitchChunkMemoryRequirement <= kMemoryChunkSize); // exceeds the block size uint32_t const newOffset = mW.mOffset + alignedSize; @@ -170,7 +170,7 @@ uint8_t *CommandEncoder::allocateImpl(uint32_t &allocatedSize, uint32_t const re 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); // 注意这里还指向原位置 + switchCommand->mNext = reinterpret_cast(newChunk); // point to start position mW.mLastCommand = switchCommand; ++mW.mPendingCommandCount; mW.mCurrentMemoryChunk = newChunk; @@ -222,14 +222,12 @@ void CommandEncoder::executeCommand() noexcept { } Command *CommandEncoder::readCommand() noexcept { - while (!hasNewCommand()) { - // 如果当前没有可读的Command 尝试同步一下生产者线程的数据 - pullCommands(); + while (!hasNewCommand()) { // if empty + pullCommands(); // try pulling data from consumer - // 如果依然没有 挂起消费者线程 等待生产者线程填充Command并唤醒消费者线程 - if (!hasNewCommand()) { - mN.Wait(); - pullCommands(); + if (!hasNewCommand()) { // still empty + mN.Wait(); // wait for the producer to wake me up + pullCommands(); // pulling again } } diff --git a/native/cocos/base/threading/CommandEncoder.h b/native/cocos/base/threading/CommandEncoder.h index 8220dea294..175b47a10b 100644 --- a/native/cocos/base/threading/CommandEncoder.h +++ b/native/cocos/base/threading/CommandEncoder.h @@ -6,8 +6,7 @@ namespace cc { -// 由于系统的内存分配是从一个全局堆里申请 分配过程会上锁导致降低并行度 在多线程的环境下如果频繁的申请释放内存会降低性能 -// 所以这里先加个接口来统一调用 未来会换成单独服务于线程的无锁分配器 +// TODO: thread-specific allocators template inline T *memoryAllocateForMultiThread(uint32_t const count) noexcept { return static_cast(malloc(sizeof(T) * count)); @@ -59,9 +58,8 @@ struct alignas(64) ReaderContext final { bool mFlushingFinished{false}; }; -// 支持单生产者单消费者的环形缓冲区 -// 生产者线程Allocate 消费者线程execute -// 除了Command之外 执行Command需要的数据也需要拷贝到这里 来实现线程安全的数据访问 +// A single-producer single-consumer circular buffer queue. +// Both the commands and their submitting data should be allocated from here. class alignas(64) CommandEncoder final { public: CommandEncoder(); @@ -71,12 +69,12 @@ public: CommandEncoder &operator=(CommandEncoder const &) = delete; CommandEncoder &operator=(CommandEncoder &&) = delete; - // 分配Command + // command allocation template std::enable_if_t::value, T *> allocate(uint32_t const count) noexcept; - // 分配数据 + // general-purpose allocation template std::enable_if_t::value, T *> allocate(uint32_t const count) noexcept; @@ -85,13 +83,12 @@ public: template T *allocateAndZero(uint32_t const count) noexcept; - // 通知消费者线程开始工作 + // notify the consumer to start working void kick() noexcept; - // 通知消费者线程开始工作并阻塞生产者线程 直到消费者线程执行完所有命令 + // notify the consumer to start working and block the producer until finished void kickAndWait() noexcept; - // 只支持单消费者 void runConsumerThread() noexcept; void terminateConsumerThread() noexcept; void finishWriting(bool wait) noexcept; @@ -135,7 +132,7 @@ private: uint8_t *allocateImpl(uint32_t &allocatedSize, uint32_t const requestSize) noexcept; void pushCommands() noexcept; - // 在消费者线程执行 + // consumer thread specifics void pullCommands() noexcept; void executeCommand() noexcept; Command *readCommand() noexcept; @@ -147,7 +144,7 @@ private: EventCV mN; bool mImmediateMode{true}; bool mWorkerAttached{false}; - bool mFreeChunksByUser{true}; // 被回收的Chunk会被记录到一个队列里 由用户在生产者线程选择合适的时机来Free + bool mFreeChunksByUser{true}; // recycled chunks will be stashed until explicit free instruction friend class MemoryChunkSwitchCommand; }; @@ -208,7 +205,7 @@ T *CommandEncoder::allocateAndZero(uint32_t const count) noexcept { return allocatedMemory; } -// 生产者线程用来向CommandBuffer填充Command的工具宏 +// utility macros for the producer thread to encode commands #define WRITE_COMMAND(CB, CommandName, Params) \ { \ diff --git a/native/cocos/renderer/core/gfx/GFXCommandBuffer.h b/native/cocos/renderer/core/gfx/GFXCommandBuffer.h index 352ec366a2..cafc0bd506 100755 --- a/native/cocos/renderer/core/gfx/GFXCommandBuffer.h +++ b/native/cocos/renderer/core/gfx/GFXCommandBuffer.h @@ -37,7 +37,7 @@ 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 + // secondary command buffer specifics 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); }