| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "Managers/BsHardwareBufferManager.h" |
| 4 | #include "RenderAPI/BsVertexData.h" |
| 5 | #include "RenderAPI/BsGpuBuffer.h" |
| 6 | #include "RenderAPI/BsVertexDeclaration.h" |
| 7 | #include "RenderAPI/BsGpuParamBlockBuffer.h" |
| 8 | #include "RenderAPI/BsVertexDataDesc.h" |
| 9 | #include "RenderAPI/BsGpuParams.h" |
| 10 | |
| 11 | namespace bs |
| 12 | { |
| 13 | SPtr<VertexDeclaration> HardwareBufferManager::createVertexDeclaration(const SPtr<VertexDataDesc>& desc) |
| 14 | { |
| 15 | VertexDeclaration* decl = new (bs_alloc<VertexDeclaration>()) VertexDeclaration(desc->createElements()); |
| 16 | |
| 17 | SPtr<VertexDeclaration> declPtr = bs_core_ptr<VertexDeclaration>(decl); |
| 18 | declPtr->_setThisPtr(declPtr); |
| 19 | declPtr->initialize(); |
| 20 | |
| 21 | return declPtr; |
| 22 | } |
| 23 | |
| 24 | SPtr<VertexBuffer> HardwareBufferManager::createVertexBuffer(const VERTEX_BUFFER_DESC& desc) |
| 25 | { |
| 26 | SPtr<VertexBuffer> vbuf = bs_core_ptr<VertexBuffer>(new (bs_alloc<VertexBuffer>()) VertexBuffer(desc)); |
| 27 | vbuf->_setThisPtr(vbuf); |
| 28 | vbuf->initialize(); |
| 29 | return vbuf; |
| 30 | } |
| 31 | |
| 32 | SPtr<IndexBuffer> HardwareBufferManager::createIndexBuffer(const INDEX_BUFFER_DESC& desc) |
| 33 | { |
| 34 | SPtr<IndexBuffer> ibuf = bs_core_ptr<IndexBuffer>(new (bs_alloc<IndexBuffer>()) IndexBuffer(desc)); |
| 35 | ibuf->_setThisPtr(ibuf); |
| 36 | ibuf->initialize(); |
| 37 | return ibuf; |
| 38 | |
| 39 | } |
| 40 | |
| 41 | SPtr<GpuParamBlockBuffer> HardwareBufferManager::createGpuParamBlockBuffer(UINT32 size, GpuBufferUsage usage) |
| 42 | { |
| 43 | SPtr<GpuParamBlockBuffer> paramBlockPtr = bs_core_ptr<GpuParamBlockBuffer>(new (bs_alloc<GpuParamBlockBuffer>()) GpuParamBlockBuffer(size, usage)); |
| 44 | paramBlockPtr->_setThisPtr(paramBlockPtr); |
| 45 | paramBlockPtr->initialize(); |
| 46 | return paramBlockPtr; |
| 47 | } |
| 48 | |
| 49 | SPtr<GpuBuffer> HardwareBufferManager::createGpuBuffer(const GPU_BUFFER_DESC& desc) |
| 50 | { |
| 51 | SPtr<GpuBuffer> gbuf = bs_core_ptr<GpuBuffer>(new (bs_alloc<GpuBuffer>()) GpuBuffer(desc)); |
| 52 | gbuf->_setThisPtr(gbuf); |
| 53 | gbuf->initialize(); |
| 54 | |
| 55 | return gbuf; |
| 56 | } |
| 57 | |
| 58 | SPtr<GpuParams> HardwareBufferManager::createGpuParams(const SPtr<GpuPipelineParamInfo>& paramInfo) |
| 59 | { |
| 60 | GpuParams* params = new (bs_alloc<GpuParams>()) GpuParams(paramInfo); |
| 61 | SPtr<GpuParams> paramsPtr = bs_core_ptr<GpuParams>(params); |
| 62 | paramsPtr->_setThisPtr(paramsPtr); |
| 63 | paramsPtr->initialize(); |
| 64 | |
| 65 | return paramsPtr; |
| 66 | } |
| 67 | |
| 68 | namespace ct |
| 69 | { |
| 70 | |
| 71 | HardwareBufferManager::VertexDeclarationKey::VertexDeclarationKey(const Vector<VertexElement>& elements) |
| 72 | :elements(elements) |
| 73 | { } |
| 74 | |
| 75 | |
| 76 | size_t HardwareBufferManager::VertexDeclarationKey::HashFunction::operator()(const VertexDeclarationKey& v) const |
| 77 | { |
| 78 | size_t hash = 0; |
| 79 | for(auto& entry : v.elements) |
| 80 | bs_hash_combine(hash, VertexElement::getHash(entry)); |
| 81 | |
| 82 | return hash; |
| 83 | } |
| 84 | |
| 85 | bool HardwareBufferManager::VertexDeclarationKey::EqualFunction::operator()(const VertexDeclarationKey& lhs, |
| 86 | const VertexDeclarationKey& rhs) const |
| 87 | { |
| 88 | if (lhs.elements.size() != rhs.elements.size()) |
| 89 | return false; |
| 90 | |
| 91 | size_t numElements = lhs.elements.size(); |
| 92 | auto iterLeft = lhs.elements.begin(); |
| 93 | auto iterRight = rhs.elements.begin(); |
| 94 | for(size_t i = 0; i < numElements; i++) |
| 95 | { |
| 96 | if (*iterLeft != *iterRight) |
| 97 | return false; |
| 98 | |
| 99 | ++iterLeft; |
| 100 | ++iterRight; |
| 101 | } |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | SPtr<IndexBuffer> HardwareBufferManager::createIndexBuffer(const INDEX_BUFFER_DESC& desc, |
| 107 | GpuDeviceFlags deviceMask) |
| 108 | { |
| 109 | SPtr<IndexBuffer> ibuf = createIndexBufferInternal(desc, deviceMask); |
| 110 | ibuf->initialize(); |
| 111 | return ibuf; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | SPtr<VertexBuffer> HardwareBufferManager::createVertexBuffer(const VERTEX_BUFFER_DESC& desc, |
| 116 | GpuDeviceFlags deviceMask) |
| 117 | { |
| 118 | SPtr<VertexBuffer> vbuf = createVertexBufferInternal(desc, deviceMask); |
| 119 | vbuf->initialize(); |
| 120 | return vbuf; |
| 121 | } |
| 122 | |
| 123 | SPtr<VertexDeclaration> HardwareBufferManager::createVertexDeclaration(const SPtr<VertexDataDesc>& desc, |
| 124 | GpuDeviceFlags deviceMask) |
| 125 | { |
| 126 | Vector<VertexElement> elements = desc->createElements(); |
| 127 | |
| 128 | return createVertexDeclaration(elements, deviceMask); |
| 129 | } |
| 130 | |
| 131 | SPtr<GpuParams> HardwareBufferManager::createGpuParams(const SPtr<GpuPipelineParamInfo>& paramInfo, |
| 132 | GpuDeviceFlags deviceMask) |
| 133 | { |
| 134 | SPtr<GpuParams> params = createGpuParamsInternal(paramInfo, deviceMask); |
| 135 | params->initialize(); |
| 136 | |
| 137 | return params; |
| 138 | } |
| 139 | |
| 140 | SPtr<VertexDeclaration> HardwareBufferManager::createVertexDeclaration(const Vector<VertexElement>& elements, |
| 141 | GpuDeviceFlags deviceMask) |
| 142 | { |
| 143 | VertexDeclarationKey key(elements); |
| 144 | |
| 145 | auto iterFind = mCachedDeclarations.find(key); |
| 146 | if (iterFind != mCachedDeclarations.end()) |
| 147 | return iterFind->second; |
| 148 | |
| 149 | SPtr<VertexDeclaration> declPtr = createVertexDeclarationInternal(elements, deviceMask); |
| 150 | declPtr->initialize(); |
| 151 | |
| 152 | mCachedDeclarations[key] = declPtr; |
| 153 | return declPtr; |
| 154 | } |
| 155 | |
| 156 | SPtr<GpuParamBlockBuffer> HardwareBufferManager::createGpuParamBlockBuffer(UINT32 size, |
| 157 | GpuBufferUsage usage, GpuDeviceFlags deviceMask) |
| 158 | { |
| 159 | SPtr<GpuParamBlockBuffer> paramBlockPtr = createGpuParamBlockBufferInternal(size, usage, deviceMask); |
| 160 | paramBlockPtr->initialize(); |
| 161 | |
| 162 | return paramBlockPtr; |
| 163 | } |
| 164 | |
| 165 | SPtr<GpuBuffer> HardwareBufferManager::createGpuBuffer(const GPU_BUFFER_DESC& desc, |
| 166 | GpuDeviceFlags deviceMask) |
| 167 | { |
| 168 | SPtr<GpuBuffer> gbuf = createGpuBufferInternal(desc, deviceMask); |
| 169 | gbuf->initialize(); |
| 170 | |
| 171 | return gbuf; |
| 172 | } |
| 173 | |
| 174 | SPtr<GpuBuffer> HardwareBufferManager::createGpuBuffer(const GPU_BUFFER_DESC& desc, |
| 175 | SPtr<HardwareBuffer> underlyingBuffer) |
| 176 | { |
| 177 | SPtr<GpuBuffer> gbuf = createGpuBufferInternal(desc, std::move(underlyingBuffer)); |
| 178 | gbuf->initialize(); |
| 179 | |
| 180 | return gbuf; |
| 181 | } |
| 182 | |
| 183 | SPtr<VertexDeclaration> HardwareBufferManager::createVertexDeclarationInternal( |
| 184 | const Vector<VertexElement>& elements, GpuDeviceFlags deviceMask) |
| 185 | { |
| 186 | VertexDeclaration* decl = new (bs_alloc<VertexDeclaration>()) VertexDeclaration(elements, deviceMask); |
| 187 | |
| 188 | SPtr<VertexDeclaration> ret = bs_shared_ptr<VertexDeclaration>(decl); |
| 189 | ret->_setThisPtr(ret); |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | SPtr<GpuParams> HardwareBufferManager::createGpuParamsInternal( |
| 195 | const SPtr<GpuPipelineParamInfo>& paramInfo, GpuDeviceFlags deviceMask) |
| 196 | { |
| 197 | GpuParams* params = new (bs_alloc<GpuParams>()) GpuParams(paramInfo, deviceMask); |
| 198 | SPtr<GpuParams> paramsPtr = bs_shared_ptr<GpuParams>(params); |
| 199 | paramsPtr->_setThisPtr(paramsPtr); |
| 200 | |
| 201 | return paramsPtr; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |