10
10
#include " nbl/asset/IAsset.h"
11
11
#include " nbl/asset/IPreHashed.h"
12
12
13
+ #include " nbl/core/alloc/refctd_memory_resource.h"
14
+
13
15
namespace nbl ::asset
14
16
{
15
17
@@ -28,7 +30,7 @@ class ICPUBuffer final : public asset::IBuffer, public IPreHashed
28
30
size_t size;
29
31
void * data = nullptr ;
30
32
size_t alignment = _NBL_SIMD_ALIGNMENT;
31
- std::pmr::memory_resource* memoryResource = nullptr ;
33
+ core::smart_refctd_ptr<core::refctd_memory_resource> memoryResource = nullptr ;
32
34
33
35
SCreationParams& operator =(const asset::IBuffer::SCreationParams& rhs)
34
36
{
@@ -37,31 +39,28 @@ class ICPUBuffer final : public asset::IBuffer, public IPreHashed
37
39
}
38
40
};
39
41
40
- ICPUBuffer (size_t size, void * data, std::pmr::memory_resource* memoryResource, size_t alignment, bool adopt_memory) :
41
- asset::IBuffer ({ size, EUF_TRANSFER_DST_BIT }), m_data(data), m_mem_resource(memoryResource), m_alignment(alignment), m_adopt_memory(adopt_memory) {}
42
-
43
42
// ! allocates uninitialized memory, copies `data` into allocation if `!data` not nullptr
44
- core::smart_refctd_ptr<ICPUBuffer> static create (const SCreationParams& params) {
45
- std::pmr::memory_resource* memoryResource = params.memoryResource ;
43
+ core::smart_refctd_ptr<ICPUBuffer> static create (SCreationParams&& params) {
46
44
if (!params.memoryResource )
47
- memoryResource = std::pmr::get_default_resource ();
45
+ params. memoryResource = core::getDefaultMemoryResource ();
48
46
49
- auto data = memoryResource->allocate (params.size , params.alignment );
47
+ auto data = params. memoryResource ->allocate (params.size , params.alignment );
50
48
if (!data)
51
49
return nullptr ;
52
-
53
50
if (params.data )
54
51
memcpy (data, params.data , params.size );
52
+ params.data = data;
55
53
56
- return core::make_smart_refctd_ptr <ICPUBuffer>(params. size , data, memoryResource, params. alignment , false );
54
+ return core::smart_refctd_ptr <ICPUBuffer>(new ICPUBuffer ( std::move ( params)), core::dont_grab );
57
55
}
58
56
59
57
// ! does not allocate memory, adopts the `data` pointer, no copies done
60
- core::smart_refctd_ptr<ICPUBuffer> static create (const SCreationParams& params, core::adopt_memory_t ) {
61
- std::pmr::memory_resource* memoryResource;
58
+ core::smart_refctd_ptr<ICPUBuffer> static create (SCreationParams&& params, core::adopt_memory_t ) {
59
+ if (!params.data )
60
+ return nullptr ;
62
61
if (!params.memoryResource )
63
- memoryResource = std::pmr::get_default_resource ();
64
- return core::make_smart_refctd_ptr <ICPUBuffer>(params. size , params. data , memoryResource, params. alignment , true );
62
+ params. memoryResource = core::getDefaultMemoryResource ();
63
+ return core::smart_refctd_ptr <ICPUBuffer>(new ICPUBuffer ( std::move ( params)), core::dont_grab );
65
64
}
66
65
67
66
core::smart_refctd_ptr<IAsset> clone (uint32_t = ~0u ) const override final
@@ -77,7 +76,6 @@ class ICPUBuffer final : public asset::IBuffer, public IPreHashed
77
76
78
77
inline size_t getDependantCount () const override { return 0 ; }
79
78
80
- //
81
79
inline core::blake3_hash_t computeContentHash () const override
82
80
{
83
81
core::blake3_hasher hasher;
@@ -121,22 +119,20 @@ class ICPUBuffer final : public asset::IBuffer, public IPreHashed
121
119
122
120
inline void discardContent_impl () override
123
121
{
124
- return freeData ();
125
- }
126
-
127
- // REMEMBER TO CALL FROM DTOR!
128
- virtual inline void freeData ()
129
- {
130
- if (!m_adopt_memory && m_data)
122
+ if (m_data)
131
123
m_mem_resource->deallocate (m_data, m_creationParams.size , m_alignment);
132
124
m_data = nullptr ;
133
125
m_creationParams.size = 0ull ;
134
126
}
135
127
128
+ private:
129
+ ICPUBuffer (SCreationParams&& params) :
130
+ asset::IBuffer ({ params.size , EUF_TRANSFER_DST_BIT }), m_data(params.data),
131
+ m_mem_resource (params.memoryResource), m_alignment(params.alignment) {}
132
+
136
133
void * m_data;
137
- std::pmr::memory_resource* m_mem_resource;
134
+ core::smart_refctd_ptr<core::refctd_memory_resource> m_mem_resource;
138
135
size_t m_alignment;
139
- bool m_adopt_memory;
140
136
};
141
137
142
138
} // end namespace nbl::asset
0 commit comments