| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma once
- #include <AnKi/Core/Common.h>
- #include <AnKi/Gr/Utils/SegregatedListsGpuMemoryPool.h>
- #include <AnKi/Resource/ShaderProgramResource.h>
- namespace anki {
- /// @addtogroup core
- /// @{
- /// @memberof GpuSceneBuffer
- class GpuSceneBufferAllocation
- {
- friend class GpuSceneBuffer;
- public:
- GpuSceneBufferAllocation() = default;
- GpuSceneBufferAllocation(const GpuSceneBufferAllocation&) = delete;
- GpuSceneBufferAllocation(GpuSceneBufferAllocation&& b)
- {
- *this = std::move(b);
- }
- ~GpuSceneBufferAllocation();
- GpuSceneBufferAllocation& operator=(const GpuSceneBufferAllocation&) = delete;
- GpuSceneBufferAllocation& operator=(GpuSceneBufferAllocation&& b)
- {
- ANKI_ASSERT(!isValid() && "Forgot to delete");
- m_token = b.m_token;
- b.m_token = {};
- return *this;
- }
- Bool isValid() const
- {
- return m_token.m_offset != kMaxPtrSize;
- }
- /// Get offset in the Unified Geometry Buffer buffer.
- U32 getOffset() const
- {
- ANKI_ASSERT(isValid());
- return U32(m_token.m_offset);
- }
- U32 getAllocatedSize() const
- {
- ANKI_ASSERT(isValid());
- return U32(m_token.m_size);
- }
- private:
- SegregatedListsGpuMemoryPoolToken m_token;
- };
- /// Memory pool for the GPU scene.
- class GpuSceneBuffer : public MakeSingleton<GpuSceneBuffer>
- {
- template<typename>
- friend class MakeSingleton;
- public:
- GpuSceneBuffer(const GpuSceneBuffer&) = delete; // Non-copyable
- GpuSceneBuffer& operator=(const GpuSceneBuffer&) = delete; // Non-copyable
- void init();
- GpuSceneBufferAllocation allocate(PtrSize size, U32 alignment)
- {
- GpuSceneBufferAllocation alloc;
- m_pool.allocate(size, alignment, alloc.m_token);
- return alloc;
- }
- void deferredFree(GpuSceneBufferAllocation& alloc)
- {
- m_pool.deferredFree(alloc.m_token);
- }
- void endFrame()
- {
- m_pool.endFrame();
- #if ANKI_STATS_ENABLED
- updateStats();
- #endif
- }
- Buffer& getBuffer() const
- {
- return m_pool.getGpuBuffer();
- }
- private:
- SegregatedListsGpuMemoryPool m_pool;
- GpuSceneBuffer() = default;
- ~GpuSceneBuffer() = default;
- void updateStats() const;
- };
- inline GpuSceneBufferAllocation::~GpuSceneBufferAllocation()
- {
- GpuSceneBuffer::getSingleton().deferredFree(*this);
- }
- /// Creates the copy jobs that will patch the GPU Scene.
- class GpuSceneMicroPatcher : public MakeSingleton<GpuSceneMicroPatcher>
- {
- template<typename>
- friend class MakeSingleton;
- public:
- GpuSceneMicroPatcher(const GpuSceneMicroPatcher&) = delete;
- GpuSceneMicroPatcher& operator=(const GpuSceneMicroPatcher&) = delete;
- Error init();
- /// Copy data for the GPU scene to a staging buffer.
- /// @note It's thread-safe.
- void newCopy(StackMemoryPool& frameCpuPool, PtrSize gpuSceneDestOffset, PtrSize dataSize, const void* data);
- template<typename T>
- void newCopy(StackMemoryPool& frameCpuPool, PtrSize gpuSceneDestOffset, const T& value)
- {
- newCopy(frameCpuPool, gpuSceneDestOffset, sizeof(value), &value);
- }
- /// @see newCopy
- void newCopy(StackMemoryPool& frameCpuPool, const GpuSceneBufferAllocation& dest, PtrSize dataSize, const void* data)
- {
- ANKI_ASSERT(dataSize <= dest.getAllocatedSize());
- newCopy(frameCpuPool, dest.getOffset(), dataSize, data);
- }
- /// @see newCopy
- template<typename T>
- void newCopy(StackMemoryPool& frameCpuPool, const GpuSceneBufferAllocation& dest, const T& value)
- {
- ANKI_ASSERT(sizeof(value) <= dest.getAllocatedSize());
- newCopy(frameCpuPool, dest.getOffset(), sizeof(value), &value);
- }
- /// Check if there is a need to call patchGpuScene or if no copies are needed.
- /// @note Not thread-safe. Nothing else should be happening before calling it.
- Bool patchingIsNeeded() const
- {
- return m_crntFramePatchHeaders.getSize() > 0;
- }
- /// Copy the data to the GPU scene buffer.
- /// @note Not thread-safe. Nothing else should be happening before calling it.
- void patchGpuScene(CommandBuffer& cmdb);
- private:
- static constexpr U32 kDwordsPerPatch = 64;
- class PatchHeader;
- DynamicArray<PatchHeader, MemoryPoolPtrWrapper<StackMemoryPool>> m_crntFramePatchHeaders;
- DynamicArray<U32, MemoryPoolPtrWrapper<StackMemoryPool>> m_crntFramePatchData;
- Mutex m_mtx;
- ShaderProgramResourcePtr m_copyProgram;
- ShaderProgramPtr m_grProgram;
- GpuSceneMicroPatcher();
- ~GpuSceneMicroPatcher();
- };
- /// @}
- } // end namespace anki
|