|
| 1 | +// Copyright (C) 2019 - DevSH Graphics Programming Sp. z O.O. |
| 2 | +// For conditions of distribution and use, see copyright notice in nabla.h |
| 3 | +// See the original file in irrlicht source for authors |
| 4 | + |
| 5 | +#ifndef __NBL_SCENE_I_TREE_TRANSFORM_MANAGER_H_INCLUDED__ |
| 6 | +#define __NBL_SCENE_I_TREE_TRANSFORM_MANAGER_H_INCLUDED__ |
| 7 | + |
| 8 | +#include "nbl/core/core.h" |
| 9 | +#include "nbl/video/video.h" |
| 10 | + |
| 11 | +namespace nbl |
| 12 | +{ |
| 13 | +namespace scene |
| 14 | +{ |
| 15 | + |
| 16 | + |
| 17 | +class ITransformTreeManager : public virtual core::IReferenceCounted |
| 18 | +{ |
| 19 | + public: |
| 20 | + using node_t = uint32_t; |
| 21 | + _NBL_STATIC_INLINE_CONSTEXPR node_t invalid_node = video::IPropertyPool::invalid_index; |
| 22 | + |
| 23 | + using timestamp_t = video::IGPUAnimationLibrary::timestamp_t; |
| 24 | + |
| 25 | + _NBL_STATIC_INLINE_CONSTEXPR uint32_t global_transform_prop_ix = 3u; |
| 26 | + private: |
| 27 | + using parent_t = node_t; |
| 28 | + using relative_transform_t = core::matrix3x4SIMD; |
| 29 | + using modified_stamp_t = timestamp_t; |
| 30 | + using global_transform_t = core::matrix3x4SIMD; |
| 31 | + using recomputed_stamp_t = timestamp_t; |
| 32 | + |
| 33 | + public: |
| 34 | + using property_pool_t = video::CPropertyPool<core::allocator, |
| 35 | + parent_t, |
| 36 | + relative_transform_t,modified_stamp_t, |
| 37 | + global_transform_t,recomputed_stamp_t |
| 38 | + >; |
| 39 | + |
| 40 | + // creation |
| 41 | + static inline core::smart_refctd_ptr<ITransformTreeManager> create(video::IVideoDriver* _driver, asset::SBufferRange<video::IGPUBuffer>&& memoryBlock, core::allocator<uint8_t>&& alloc = core::allocator<uint8_t>()) |
| 42 | + { |
| 43 | + const auto reservedSize = video::IPropertyPool::getReservedSize(property_pool_t::calcApproximateCapacity(memoryBlock.size)); |
| 44 | + auto reserved = std::allocator_traits<core::allocator<uint8_t>>::allocate(alloc,reservedSize); |
| 45 | + if (!reserved) |
| 46 | + return nullptr; |
| 47 | + |
| 48 | + auto retval = create(_driver,std::move(memoryBlock),reserved,std::move(alloc)); |
| 49 | + if (!retval) |
| 50 | + std::allocator_traits<core::allocator<uint8_t>>::deallocate(alloc,reserved,reservedSize); |
| 51 | + |
| 52 | + return retval; |
| 53 | + } |
| 54 | + // if this method fails to create the pool, the callee must free the reserved memory themselves, also the reserved pointer must be compatible with the allocator so it can free it |
| 55 | + static inline core::smart_refctd_ptr<ITransformTreeManager> create(video::IVideoDriver* _driver, asset::SBufferRange<video::IGPUBuffer>&& memoryBlock, void* reserved, core::allocator<uint8_t>&& alloc=core::allocator<uint8_t>()) |
| 56 | + { |
| 57 | + auto _nodeStorage = property_pool_t::create(std::move(memoryBlock),reserved,std::move(alloc)); |
| 58 | + if (!_nodeStorage) |
| 59 | + return nullptr; |
| 60 | + |
| 61 | + auto* ttm = new ITransformTreeManager(_driver,std::move(_nodeStorage)); |
| 62 | + return core::smart_refctd_ptr<ITransformTreeManager>(ttm,core::dont_grab); |
| 63 | + } |
| 64 | + |
| 65 | + // |
| 66 | + inline const auto* getNodePropertyPool() const {return m_nodeStorage.get();} |
| 67 | + |
| 68 | + // |
| 69 | + inline asset::SBufferRange<video::IGPUBuffer> getGlobalTransformationBufferRange() const |
| 70 | + { |
| 71 | + asset::SBufferRange<video::IGPUBuffer> retval = {m_nodeStorage->getPropertyOffset(global_transform_prop_ix),m_nodeStorage->getCapacity()*sizeof(global_transform_t),m_nodeStorage->getMemoryBlock().buffer}; |
| 72 | + return retval; |
| 73 | + } |
| 74 | +#if 0 |
| 75 | + // |
| 76 | + struct AllocationRequest |
| 77 | + { |
| 78 | + core::SRange<node_t> outNodes; |
| 79 | + const parent_t* parents; |
| 80 | + const relative_transform_t* relativeTransforms; |
| 81 | + // what to do about timestamps? |
| 82 | + }; |
| 83 | + template<typename ParentNodeIt> |
| 84 | + inline void addNodes(CPropertyPoolHandler* propertyPoolHandler, node_t* nodesBegin, node_t* nodesEnd, ParentNodeIt parentsBegin, const std::chrono::steady_clock::time_point& maxWaitPoint=video::GPUEventWrapper::default_wait()) |
| 85 | + { |
| 86 | + if (std::distance(nodesBegin,nodesEnd)>m_nodeStorage->getFree()) |
| 87 | + return; |
| 88 | + |
| 89 | + m_nodeStorage->allocateProperties(nodesBegin,nodesEnd); |
| 90 | + assert(false); // TODO |
| 91 | + } |
| 92 | + // TODO: utilities for adding root nodes, adding skeleton node instances, etc. |
| 93 | + // should we just do it ourselves with a shader? (set correct timestamps so global gets recomputed) |
| 94 | +#endif |
| 95 | + // |
| 96 | + inline void removeNodes(const node_t* begin, const node_t* end) |
| 97 | + { |
| 98 | + m_nodeStorage->freeProperties(begin,end); |
| 99 | + } |
| 100 | + // |
| 101 | + inline void clearNodes() |
| 102 | + { |
| 103 | + m_nodeStorage->freeAllProperties(); |
| 104 | + } |
| 105 | + |
| 106 | + // TODO: make all these functions take a pipeline barrier type (future new API) with default being a full barrier |
| 107 | + void updateLocalTransforms(const asset::SBufferBinding<video::IGPUBuffer>& dispatchIndirectParameters, const asset::SBufferBinding<video::IGPUBuffer>& requestBuffer) //do we take a per-request nodeID buffer too? |
| 108 | + { |
| 109 | + assert(false); // TODO |
| 110 | + } |
| 111 | + // |
| 112 | + void recomputeGlobalTransforms(/*dispatchIndirectParams,nodeList*/) |
| 113 | + { |
| 114 | + // TODO: do it properly |
| 115 | + auto out = getGlobalTransformationBufferRange(); |
| 116 | + m_driver->copyBuffer(m_nodeStorage->getMemoryBlock().buffer.get(),out.buffer.get(),m_nodeStorage->getPropertyOffset(1u),out.offset,out.size); |
| 117 | + } |
| 118 | + // |
| 119 | + void updateAndRecomputeGlobalTransforms(/*Same args as `updateLocalTransforms` and `recomputeGlobalTransforms`*/) |
| 120 | + { |
| 121 | + assert(false); // TODO |
| 122 | + } |
| 123 | + |
| 124 | + // |
| 125 | + auto transferGlobalTransforms(const node_t* begin, const node_t* end, const asset::SBufferBinding<video::IGPUBuffer>& outputBuffer, const std::chrono::steady_clock::time_point& maxWaitPoint=video::GPUEventWrapper::default_wait()) |
| 126 | + { |
| 127 | + video::CPropertyPoolHandler::TransferRequest request; |
| 128 | + request.download = true; |
| 129 | + request.pool = m_nodeStorage.get(); |
| 130 | + request.indices = {begin,end}; |
| 131 | + request.propertyID = 3u; |
| 132 | + //m_nodeStorage->transferProperties(); |
| 133 | + assert(false); // TODO: Need a transfer to GPU mem |
| 134 | + } |
| 135 | + //auto downloadGlobalTransforms() |
| 136 | + |
| 137 | + protected: |
| 138 | + ITransformTreeManager(video::IVideoDriver* _driver, core::smart_refctd_ptr<property_pool_t>&& _nodeStorage) : m_driver(_driver), m_nodeStorage(std::move(_nodeStorage)) |
| 139 | + { |
| 140 | + // TODO: the ComputePipeline for update,recompute and combined update&recompute |
| 141 | + } |
| 142 | + ~ITransformTreeManager() |
| 143 | + { |
| 144 | + // |
| 145 | + } |
| 146 | + |
| 147 | +#if 0 |
| 148 | + struct RootNodeParentIterator |
| 149 | + { |
| 150 | + inline RootNodeParentIterator& operator++() |
| 151 | + { |
| 152 | + //do nothing |
| 153 | + return *this; |
| 154 | + } |
| 155 | + inline RootNodeParentIterator operator++(int) |
| 156 | + { |
| 157 | + //do nothing |
| 158 | + } |
| 159 | + |
| 160 | + inline node_t operator*() const |
| 161 | + { |
| 162 | + return invalid_node; |
| 163 | + } |
| 164 | + |
| 165 | + //using iterator_category = typename std::iterator_traits::; |
| 166 | + //using difference_type = ptrdiff_t; |
| 167 | + using value_type = node_t; |
| 168 | + }; |
| 169 | +#endif |
| 170 | + video::IVideoDriver* m_driver; |
| 171 | + core::smart_refctd_ptr<property_pool_t> m_nodeStorage; |
| 172 | +}; |
| 173 | + |
| 174 | + |
| 175 | +} // end namespace scene |
| 176 | +} // end namespace nbl |
| 177 | + |
| 178 | +#endif |
| 179 | + |
0 commit comments