Skip to content

Commit cf9a866

Browse files
committed
asset: fix several bugs
Signed-off-by: Ali Cheraghi <alichraghi@proton.me>
1 parent eed29fa commit cf9a866

13 files changed

+36
-30
lines changed

include/nbl/asset/ICPUBuffer.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class ICPUBuffer final : public asset::IBuffer, public IPreHashed
4040
};
4141

4242
//! allocates uninitialized memory, copies `data` into allocation if `!data` not nullptr
43-
core::smart_refctd_ptr<ICPUBuffer> static create(SCreationParams&& params) {
43+
core::smart_refctd_ptr<ICPUBuffer> static create(SCreationParams&& params)
44+
{
4445
if (!params.memoryResource)
4546
params.memoryResource = core::getDefaultMemoryResource();
4647

@@ -55,7 +56,8 @@ class ICPUBuffer final : public asset::IBuffer, public IPreHashed
5556
}
5657

5758
//! does not allocate memory, adopts the `data` pointer, no copies done
58-
core::smart_refctd_ptr<ICPUBuffer> static create(SCreationParams&& params, core::adopt_memory_t) {
59+
core::smart_refctd_ptr<ICPUBuffer> static create(SCreationParams&& params, core::adopt_memory_t)
60+
{
5961
if (!params.data)
6062
return nullptr;
6163
if (!params.memoryResource)
@@ -122,6 +124,7 @@ class ICPUBuffer final : public asset::IBuffer, public IPreHashed
122124
if (m_data)
123125
m_mem_resource->deallocate(m_data, m_creationParams.size, m_alignment);
124126
m_data = nullptr;
127+
m_mem_resource = nullptr;
125128
m_creationParams.size = 0ull;
126129
}
127130

@@ -131,8 +134,7 @@ class ICPUBuffer final : public asset::IBuffer, public IPreHashed
131134
m_mem_resource(params.memoryResource), m_alignment(params.alignment) {}
132135

133136
~ICPUBuffer() override {
134-
if (m_data)
135-
m_mem_resource->deallocate(m_data, m_creationParams.size, m_alignment);
137+
discardContent_impl();
136138
}
137139

138140
void* m_data;

include/nbl/asset/ICPUMeshBuffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ class ICPUMeshBuffer final : public IMeshBuffer<ICPUBuffer,ICPUDescriptorSet,ICP
544544
const bool scaled = isScaledFormat(format);
545545
if (!dst || !(scaled || isIntegerFormat(format)))
546546
return false;
547-
uint8_t* vxPtr = (uint8_t*)dst;
547+
uint8_t* vxPtr = reinterpret_cast<uint8_t*>(dst);
548548

549549
if (isSignedFormat(format))
550550
{

include/nbl/core/alloc/VectorViewNullMemoryResource.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,34 @@ class VectorViewNullMemoryResource : public std::pmr::memory_resource
1717
{
1818
public:
1919
// only create the resource from an already sized vector
20-
VectorViewNullMemoryResource(core::vector<uint8_t>&& buffer) : buffer(buffer)
20+
VectorViewNullMemoryResource(core::vector<uint8_t>&& buffer) : buffer(std::move(buffer)), already_called(false)
2121
{
2222
assert(buffer.size());
23-
};
23+
}
2424

2525
void* data() {
2626
return buffer.data();
2727
}
2828

2929
protected:
3030
void* do_allocate(size_t bytes, size_t alignment) override {
31-
assert(bytes <= buffer.size());
31+
if (already_called || bytes > buffer.size() || !core::is_aligned_to(bytes, alignment))
32+
return nullptr;
33+
already_called = true;
3234
return buffer.data();
3335
}
3436

35-
void do_deallocate(void* p, size_t bytes, size_t alignment) override {}
37+
void do_deallocate(void* p, size_t bytes, size_t alignment) override {
38+
assert(p == buffer.data());
39+
}
3640

3741
bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
3842
return this == &other;
3943
}
4044

4145
private:
4246
core::vector<uint8_t> buffer;
47+
bool already_called;
4348
};
4449

4550
}

include/nbl/core/alloc/refctd_memory_resource.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111

1212
#include <memory_resource>
1313

14-
using namespace nbl;
15-
1614
namespace nbl::core
1715
{
1816

19-
class refctd_memory_resource : public core::IReferenceCounted
17+
class refctd_memory_resource : public IReferenceCounted
2018
{
2119
public:
2220
refctd_memory_resource(std::pmr::memory_resource* pmr) : m_pmr(pmr) {};

src/nbl/asset/ICPUImage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class CFlattenRegionsStreamHashImageFilter : public CMatchedSizeInOutImageFilter
122122

123123
auto getScratchAsBuffer = [&memory = state->scratch.memory](size_t size, size_t offset = 0ull)
124124
{
125-
return ICPUBuffer::create({ .size = size, .data = (uint8_t*)memory + offset, .memoryResource = core::getNullMemoryResource() }, core::adopt_memory); // adopt memory & don't free it on exit
125+
return ICPUBuffer::create({ .size = size, .data = reinterpret_cast<uint8_t*>(memory) + offset, .memoryResource = core::getNullMemoryResource() }, core::adopt_memory); // adopt memory & don't free it on exit
126126
};
127127

128128
/*
@@ -303,7 +303,7 @@ core::blake3_hash_t ICPUImage::computeContentHash() const
303303

304304
// TODO, Arek: Matt if you want this templated then assign me to update instead of hardcoding or change it yourself otherwise delete this comment
305305
const bool passed = filter.execute(std::execution::par, &state);
306-
_NBL_DELETE_ARRAY((uint8_t*)state.scratch.memory, state.scratch.size);
306+
_NBL_DELETE_ARRAY(reinterpret_cast<uint8_t*>(state.scratch.memory), state.scratch.size);
307307
assert(passed); // actually this should never fail, leaving in case
308308

309309
return state.outHash;

src/nbl/asset/interchange/CImageWriterJPG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static bool writeJPEGFile(system::IFile* file, system::ISystem* sys, const asset
160160
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
161161
row_pointer[0] = dest;
162162

163-
uint8_t* src = (uint8_t*)convertedImage->getBuffer()->getPointer();
163+
uint8_t* src = reinterpret_cast<uint8_t*>(convertedImage->getBuffer()->getPointer());
164164

165165
/* Switch up, write from bottom -> top because the texture is flipped from OpenGL side */
166166
uint32_t eof = cinfo.image_height * cinfo.image_width * cinfo.input_components;

src/nbl/asset/interchange/CImageWriterPNG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ bool CImageWriterPNG::writeAsset(system::IFile* _file, const SAssetWriteParams&
177177
}
178178
}
179179

180-
uint8_t* data = (uint8_t*)convertedImage->getBuffer()->getPointer();
180+
uint8_t* data = reinterpret_cast<uint8_t*>(convertedImage->getBuffer()->getPointer());
181181

182182
constexpr uint32_t maxPNGFileHeight = 16u * 1024u; // arbitrary limit
183183
if (trueExtent.Y>maxPNGFileHeight)

src/nbl/asset/interchange/CImageWriterTGA.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ bool CImageWriterTGA::writeAsset(system::IFile* _file, const SAssetWriteParams&
120120
return false;
121121
}
122122

123-
uint8_t* scan_lines = (uint8_t*)convertedImage->getBuffer()->getPointer();
123+
uint8_t* scan_lines = reinterpret_cast<uint8_t*>(convertedImage->getBuffer()->getPointer());
124124
if (!scan_lines)
125125
return false;
126126

src/nbl/asset/interchange/CSTLMeshFileLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ SAssetBundle CSTLMeshFileLoader::loadAsset(system::IFile* _file, const IAssetLoa
283283
{
284284
if (i % 3 == 0)
285285
normal = quantNormalCache->quantize<EF_A2B10G10R10_SNORM_PACK32>(normals[i / 3]);
286-
uint8_t* ptr = ((uint8_t*)(vertexBuf->getPointer())) + i * vtxSize;
286+
uint8_t* ptr = (reinterpret_cast<uint8_t*>(vertexBuf->getPointer())) + i * vtxSize;
287287
memcpy(ptr, positions[i].pointer, 3 * 4);
288288

289289
*reinterpret_cast<quant_normal_t*>(ptr + 12) = normal;

src/nbl/asset/utils/CMeshManipulator.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ static bool cmpVertices(ICPUMeshBuffer* _inbuf, const void* _va, const void* _vb
444444

445445
constexpr uint32_t MAX_ATTRIBS = ICPUMeshBuffer::MAX_VERTEX_ATTRIB_COUNT;
446446

447-
const uint8_t* va = (uint8_t*)_va, *vb = (uint8_t*)_vb;
447+
const uint8_t* va = reinterpret_cast<const uint8_t*>(_va), *vb = reinterpret_cast<const uint8_t*>(_vb);
448448
for (size_t i = 0u; i < MAX_ATTRIBS; ++i)
449449
{
450450
if (!_inbuf->isAttributeEnabled(i))
@@ -517,7 +517,7 @@ core::smart_refctd_ptr<ICPUMeshBuffer> IMeshManipulator::createMeshBufferWelded(
517517

518518
uint32_t maxRedirect = 0;
519519

520-
uint8_t* epicData = (uint8_t*)_NBL_ALIGNED_MALLOC(vertexSize*vertexCount,_NBL_SIMD_ALIGNMENT);
520+
uint8_t* epicData = reinterpret_cast<uint8_t*>(_NBL_ALIGNED_MALLOC(vertexSize*vertexCount,_NBL_SIMD_ALIGNMENT));
521521
for (auto i=0u; i<vertexCount; i++)
522522
{
523523
uint8_t* currentVertexPtr = epicData+i*vertexSize;
@@ -750,8 +750,8 @@ core::smart_refctd_ptr<ICPUMeshBuffer> IMeshManipulator::createOptimizedMeshBuff
750750
const size_t bufsz = outbuffer->getAttribBoundBuffer(posId).buffer->getSize();
751751

752752
const size_t vertexSize = pipeline->getCachedCreationParams().vertexInput.bindings[0].stride;
753-
uint8_t* const v = (uint8_t*)(outbuffer->getAttribBoundBuffer(posId).buffer->getPointer()); // after prefetch optim. we have guarantee of single vertex buffer so we can do like this
754-
uint8_t* const vCopy = (uint8_t*)_NBL_ALIGNED_MALLOC(bufsz, _NBL_SIMD_ALIGNMENT);
753+
uint8_t* const v = reinterpret_cast<uint8_t*>(outbuffer->getAttribBoundBuffer(posId).buffer->getPointer()); // after prefetch optim. we have guarantee of single vertex buffer so we can do like this
754+
uint8_t* const vCopy = reinterpret_cast<uint8_t*>(_NBL_ALIGNED_MALLOC(bufsz, _NBL_SIMD_ALIGNMENT));
755755
memcpy(vCopy, v, bufsz);
756756

757757
size_t baseVtx = outbuffer->getBaseVertex();
@@ -894,7 +894,7 @@ void CMeshManipulator::_filterInvalidTriangles(ICPUMeshBuffer* _input)
894894
struct Triangle
895895
{
896896
IdxT i[3];
897-
} *const begin = (Triangle*)copy, *const end = (Triangle*)((uint8_t*)copy + size);
897+
} *const begin = (Triangle*)copy, *const end = (Triangle*)(reinterpret_cast<uint8_t*>(copy) + size);
898898

899899
Triangle* const newEnd = std::remove_if(begin, end,
900900
[&_input](const Triangle& _t) {

0 commit comments

Comments
 (0)