#pragma once #include "ICommandUser.h" class CommandBuffer : public ICommandBuffer { public: CommandBuffer(bool argCommandOwer, bool argChildBufferOwner); virtual ~CommandBuffer(); virtual void Clear(); virtual void AddCommand(Command& argCommand); virtual void Perform(bool argUseOptimizedList); virtual void Optimize(); virtual void AddChildCommandBuffer(ICommandBuffer& argCommandBuffer); virtual void RemoveChildCommandBuffer(ICommandBuffer& argCommandBuffer); virtual std::vector& get_BufferedCommands(); virtual const std::vector& get_ChildCommandBuffers() const; virtual ICommandBuffer* get_ParentBuffer() const; virtual void set_ParentBuffer(ICommandBuffer* ar_CommandBuffer_); protected: void SearchDrawableCommandChain(ICommandBuffer* ar_Buffer_, std::vector& argCurrentChain); void FlushGatheredCommandChains(); private: struct CommandChainInternal { CommandChainInternal() : m_Priority(0) {} void UpdatePriority() { m_Priority = -1; for (uint32 i = 0; i < m_Chain.size(); ++i) { if (m_Chain[i]->Flags & CommandFlags::SubChainStart) { m_Priority = m_Chain[i]->SubPriority; break; } } } UINT8 m_Priority; std::vector m_Chain; struct CommandChainSort { bool operator()(CommandChainInternal* ar_Left_, CommandChainInternal* ar_Right_) const { if (ar_Left_->m_Priority == ar_Right_->m_Priority) return ar_Left_->m_Chain.size() < ar_Right_->m_Chain.size(); return ar_Left_->m_Priority < ar_Right_->m_Priority; } }; struct CommandSort { bool operator()(Command* ar_Left_, Command* ar_Right_) const { return ar_Left_->Priority < ar_Right_->Priority; } }; }; private: ICommandBuffer* m_ParentCommandBuffer; ICommandBuffer* m_OptimizedCommandBuffer; std::vector m_CommandList; std::vector m_ChildCommandBuffers; bool m_CommandOwer; bool m_ChildBufferOwner; std::map > m_HashToCommandChain; intptr_t GenerateHashValue(const std::vector& argCommands) const { intptr_t hash = 0; for (uint32 i = 0; i < argCommands.size(); ++i) { hash += (intptr_t)argCommands[i]; hash += (hash << 10); hash ^= (hash >> 6); hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15); } return hash; } std::vector m_GatheredCommandChains; };