Skip to content

Commit 60f1f3e

Browse files
author
devsh
committed
Erfan will approve of this
1 parent 83a8169 commit 60f1f3e

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

include/nbl/asset/interchange/IGeometryLoader.h

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ class IGeometryLoader : public IAssetLoader
2323
protected:
2424
inline IGeometryLoader() {}
2525

26-
static inline IGeometry<ICPUBuffer>::SDataView createView(const E_FORMAT format, const size_t elementCount, const void* data=nullptr)
26+
template<bool AdoptMemory=false>
27+
static inline IGeometry<ICPUBuffer>::SDataView createView(
28+
const E_FORMAT format, const size_t elementCount, const void* data=nullptr,
29+
core::smart_refctd_ptr<core::refctd_memory_resource>&& memoryResource=nullptr, const size_t alignment=_NBL_SIMD_ALIGNMENT
30+
)
2731
{
2832
const auto stride = getTexelOrBlockBytesize(format);
29-
auto buffer = ICPUBuffer::create({{stride*elementCount},const_cast<void*>(data)});
33+
if constexpr (AdoptMemory)
34+
buffer = ICPUBuffer::create({{stride*elementCount},const_cast<void*>(data),std::move(memoryResource),alignment},core::adopt_memory);
35+
else
36+
buffer = ICPUBuffer::create({{stride*elementCount},const_cast<void*>(data),std::move(memoryResource),alignment});
3037
return {
3138
.composed = {
3239
.stride = stride,
@@ -36,6 +43,44 @@ class IGeometryLoader : public IAssetLoader
3643
.src = {.offset=0,.size=buffer->getSize(),.buffer=std::move(buffer)}
3744
};
3845
}
46+
// creates a View from a mapped file
47+
class CFileMemoryResource final : public core::refctd_memory_resource
48+
{
49+
public:
50+
inline CFileMemoryResource(core::smart_refctd_ptr<system::IFile>&& _file) : m_file(std::move(_file)) {}
51+
52+
inline void* allocate(std::size_t bytes, std::size_t alignment) override
53+
{
54+
assert(false); // should never be called
55+
}
56+
inline void deallocate(void* p, std::size_t bytes, std::size_t alignment) override
57+
{
58+
assert(m_file);
59+
auto* const basePtr = reinterpret_cast<const uint8_t*>(m_file->getMappedPointer());
60+
assert(basePtr && basePtr<=p && p<=basePtr+file->getSize());
61+
}
62+
63+
protected:
64+
core::smart_refctd_ptr<system::IFile> m_file;
65+
};
66+
static inline IGeometry<ICPUBuffer>::SDataView createView(const E_FORMAT format, const size_t elementCount, core::smart_refctd_ptr<system::IFile>&& file, const size_t offsetInFile)
67+
{
68+
if (auto* const basePtr=reinterpret_cast<const uint8_t*>(file->getMappedPointer()); basePtr)
69+
{
70+
auto resource = core::make_smart_refctd_ptr<CFileMemoryResource>(std::move(file));
71+
auto* const data = basePtr+offsetInFile;
72+
return createView<true>(format,elementCount,data,std::move(resource),0x1ull<<hlsl::findLSB(ptrdiff_t(data)));
73+
}
74+
else
75+
{
76+
auto view = createView(format,elementCount);
77+
system::IFile::success_t success;
78+
file->read(success,reinterpret_cast<uint8_t*>(view.src.buffer->getPointer())+view.src.offset,offsetInFile,view.src.actualSize());
79+
if (success)
80+
return view;
81+
}
82+
return {};
83+
}
3984

4085
private:
4186
};

include/nbl/core/alloc/refctd_memory_resource.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,46 @@
1-
// Copyright (C) 2019-2024 - DevSH Graphics Programming Sp. z O.O.
1+
// Copyright (C) 2019-2025 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine" and was originally part of the "Irrlicht Engine"
33
// For conditions of distribution and use, see copyright notice in nabla.h
44
// See the original file in irrlicht source for authors
5-
65
#ifndef _NBL_CORE_ALLOC_REFCTD_MEMORY_RESOURCE_INCLUDED_
76
#define _NBL_CORE_ALLOC_REFCTD_MEMORY_RESOURCE_INCLUDED_
87

8+
99
#include "BuildConfigOptions.h"
1010
#include "nbl/core/IReferenceCounted.h"
1111

12-
#include <memory_resource>
1312

1413
namespace nbl::core
1514
{
1615

1716
class refctd_memory_resource : public IReferenceCounted
1817
{
1918
public:
20-
refctd_memory_resource(std::pmr::memory_resource* pmr) : m_pmr(pmr) {};
19+
virtual void* allocate(size_t bytes, size_t alignment) = 0;
20+
21+
virtual void deallocate(void* p, size_t bytes, size_t alignment) = 0;
22+
};
23+
24+
class std_memory_resource final : public refctd_memory_resource
25+
{
26+
public:
27+
inline std_memory_resource(std::pmr::memory_resource* pmr) : m_pmr(pmr) {};
2128

22-
void* allocate(size_t bytes, size_t alignment) {
29+
inline void* allocate(size_t bytes, size_t alignment) override
30+
{
2331
return m_pmr->allocate(bytes, alignment);
2432
}
2533

26-
void deallocate(void* p, size_t bytes, size_t alignment) {
34+
inline void deallocate(void* p, size_t bytes, size_t alignment) override
35+
{
2736
return m_pmr->deallocate(p, bytes, alignment);
2837
}
2938

3039
private:
3140
std::pmr::memory_resource* m_pmr;
3241
};
3342

34-
NBL_API2 smart_refctd_ptr<refctd_memory_resource> getNullMemoryResource();
43+
NBL_API2 smart_refctd_ptr<std_memory_resource> getNullMemoryResource();
3544
NBL_API2 smart_refctd_ptr<refctd_memory_resource> getDefaultMemoryResource();
3645
NBL_API2 void setDefaultMemoryResource(refctd_memory_resource* memoryResource);
3746

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1-
// Copyright (C) 2018-2024 - DevSH Graphics Programming Sp. z O.O.
1+
// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
44

5+
56
#include "nbl/core/alloc/refctd_memory_resource.h"
67

7-
using namespace nbl;
8-
using namespace core;
8+
#include <memory_resource>
9+
910

11+
namespace nbl::core
12+
{
1013
static smart_refctd_ptr<refctd_memory_resource> default_memory_resource = nullptr;
1114

12-
smart_refctd_ptr<refctd_memory_resource> core::getNullMemoryResource()
15+
smart_refctd_ptr<std_memory_resource> core::getNullMemoryResource()
1316
{
14-
static smart_refctd_ptr<refctd_memory_resource> null_memory_resource = nullptr;
17+
static smart_refctd_ptr<std_memory_resource> null_memory_resource = nullptr;
1518
if (!null_memory_resource)
16-
null_memory_resource = make_smart_refctd_ptr<refctd_memory_resource>(std::pmr::null_memory_resource());
19+
null_memory_resource = make_smart_refctd_ptr<std_memory_resource>(std::pmr::null_memory_resource());
1720
return null_memory_resource;
1821
}
1922

2023
smart_refctd_ptr<refctd_memory_resource> core::getDefaultMemoryResource()
2124
{
2225
if (!default_memory_resource)
23-
default_memory_resource = make_smart_refctd_ptr<refctd_memory_resource>(std::pmr::get_default_resource());
26+
default_memory_resource = make_smart_refctd_ptr<std_memory_resource>(std::pmr::get_default_resource());
2427
return default_memory_resource;
2528
}
2629

2730
void core::setDefaultMemoryResource(refctd_memory_resource* memoryResource)
2831
{
2932
default_memory_resource = smart_refctd_ptr<refctd_memory_resource>(memoryResource, dont_grab);
3033
}
34+
}

0 commit comments

Comments
 (0)