Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
65cd939
video: fix vkspec doc links
alichraghi Nov 7, 2024
8d5e0b0
video: improve error logging in `IGPUCommandBuffer`
alichraghi Nov 7, 2024
2675ce2
video: use NBL_LOG_ERROR in IGPUCommandBuffer.h
alichraghi Nov 8, 2024
51cbd88
@alichraghi small homework - read https://en.cppreference.com/w/cpp/l…
AnastaZIuk Nov 11, 2024
6c0b3cb
CVulkanSwapchain allow 2 more acquiresInFlight
Erfan-Ahmadi Nov 12, 2024
d37770f
fix and remove assumption that imageCount == acquiresInFlight
Erfan-Ahmadi Nov 12, 2024
08f54ba
Merge pull request #778 from Devsh-Graphics-Programming/bindless_blit
devshgraphicsprogramming Nov 12, 2024
a06c270
ICPUBuffer v2.0
alichraghi Nov 13, 2024
f25b884
asset: several fixes
alichraghi Nov 14, 2024
3398802
asset: reference counted memory_resource
alichraghi Nov 14, 2024
eed29fa
more ICPUBuffer fixes
alichraghi Nov 14, 2024
cf9a866
asset: fix several bugs
alichraghi Nov 14, 2024
09833d9
Fix build
deprilula28 Nov 14, 2024
eebc9fe
Merge pull request #783 from Devsh-Graphics-Programming/temp-fix-branch
devshgraphicsprogramming Nov 15, 2024
afe18dd
asset: pass correct arg to is_aligned
alichraghi Nov 15, 2024
971a1ed
Merge pull request #775 from Devsh-Graphics-Programming/ali_cbl
devshgraphicsprogramming Nov 15, 2024
82e89a5
Merge pull request #782 from Devsh-Graphics-Programming/ali_pmr3
devshgraphicsprogramming Nov 15, 2024
72f847d
post merge examples_tests pointer correction
Nov 15, 2024
22e86a7
fix for MSVC 17.12.0 regression in handling partial specs constrained…
Nov 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions include/nbl/asset/CVectorCPUBuffer.h

This file was deleted.

195 changes: 79 additions & 116 deletions include/nbl/asset/ICPUBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

#include <type_traits>

#include "nbl/core/alloc/null_allocator.h"

#include "nbl/asset/IBuffer.h"
#include "nbl/asset/IAsset.h"
#include "nbl/asset/IPreHashed.h"

#include "nbl/core/alloc/refctd_memory_resource.h"

namespace nbl::asset
{

Expand All @@ -22,57 +22,80 @@ namespace nbl::asset

@see IAsset
*/
class ICPUBuffer : public asset::IBuffer, public IPreHashed
class ICPUBuffer final : public asset::IBuffer, public IPreHashed
{
protected:
//! Non-allocating constructor for CCustormAllocatorCPUBuffer derivative
ICPUBuffer(size_t sizeInBytes, void* dat) : asset::IBuffer({ dat ? sizeInBytes : 0,EUF_TRANSFER_DST_BIT }), data(dat) {}

public:
//! Constructor. TODO: remove, alloc can fail, should be a static create method instead!
/** @param sizeInBytes Size in bytes. If `dat` argument is present, it denotes size of data pointed by `dat`, otherwise - size of data to be allocated.
*/
ICPUBuffer(size_t sizeInBytes) : asset::IBuffer({0,EUF_TRANSFER_DST_BIT})
struct SCreationParams : asset::IBuffer::SCreationParams
{
data = _NBL_ALIGNED_MALLOC(sizeInBytes,_NBL_SIMD_ALIGNMENT);
if (!data) // FIXME: cannot fail like that, need factory `create` methods
return;
size_t size;
void* data = nullptr;
size_t alignment = _NBL_SIMD_ALIGNMENT;
core::smart_refctd_ptr<core::refctd_memory_resource> memoryResource = nullptr;

SCreationParams& operator =(const asset::IBuffer::SCreationParams& rhs)
{
static_cast<asset::IBuffer::SCreationParams&>(*this) = rhs;
return *this;
}
};

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

auto data = params.memoryResource->allocate(params.size, params.alignment);
if (!data)
return nullptr;
if (params.data)
memcpy(data, params.data, params.size);
params.data = data;

return core::smart_refctd_ptr<ICPUBuffer>(new ICPUBuffer(std::move(params)), core::dont_grab);
}

m_creationParams.size = sizeInBytes;
//! does not allocate memory, adopts the `data` pointer, no copies done
core::smart_refctd_ptr<ICPUBuffer> static create(SCreationParams&& params, core::adopt_memory_t)
{
if (!params.data)
return nullptr;
if (!params.memoryResource)
params.memoryResource = core::getDefaultMemoryResource();
return core::smart_refctd_ptr<ICPUBuffer>(new ICPUBuffer(std::move(params)), core::dont_grab);
}

core::smart_refctd_ptr<IAsset> clone(uint32_t = ~0u) const override final
{
auto cp = core::make_smart_refctd_ptr<ICPUBuffer>(m_creationParams.size);
memcpy(cp->getPointer(), data, m_creationParams.size);
auto cp = create({ .size = m_creationParams.size, .data = m_data, .alignment = m_alignment });
memcpy(cp->getPointer(), m_data, m_creationParams.size);
cp->setContentHash(getContentHash());
return cp;
}

constexpr static inline auto AssetType = ET_BUFFER;
inline IAsset::E_TYPE getAssetType() const override final { return AssetType; }

inline size_t getDependantCount() const override {return 0;}
inline size_t getDependantCount() const override { return 0; }

//
inline core::blake3_hash_t computeContentHash() const override
{
core::blake3_hasher hasher;
if (data)
hasher.update(data,m_creationParams.size);
return static_cast<core::blake3_hash_t>(hasher);
core::blake3_hasher hasher;
if (m_data)
hasher.update(m_data, m_creationParams.size);
return static_cast<core::blake3_hash_t>(hasher);
}

inline bool missingContent() const override {return !data;}
inline bool missingContent() const override { return !m_data; }

//! Returns pointer to data.
const void* getPointer() const {return data;}
void* getPointer()
{
const void* getPointer() const { return m_data; }
void* getPointer()
{
assert(isMutable());
return data;
return m_data;
}

inline core::bitflag<E_USAGE_FLAGS> getUsageFlags() const
{
return m_creationParams.usage;
Expand All @@ -90,93 +113,33 @@ class ICPUBuffer : public asset::IBuffer, public IPreHashed
return true;
}

protected:
inline IAsset* getDependant_impl(const size_t ix) override
{
return nullptr;
}

inline void discardContent_impl() override
{
return freeData();
}

// REMEMBER TO CALL FROM DTOR!
// TODO: idea, make the `ICPUBuffer` an ADT, and use the default allocator CCPUBuffer instead for consistency
// TODO: idea make a macro for overriding all `delete` operators of a class to enforce a finalizer that runs in reverse order to destructors (to allow polymorphic cleanups)
virtual inline void freeData()
{
if (data)
_NBL_ALIGNED_FREE(data);
data = nullptr;
m_creationParams.size = 0ull;
}

void* data;
};


template<
typename Allocator = _NBL_DEFAULT_ALLOCATOR_METATYPE<uint8_t>,
bool = std::is_same<Allocator, core::null_allocator<typename Allocator::value_type> >::value
>
class CCustomAllocatorCPUBuffer;

using CDummyCPUBuffer = CCustomAllocatorCPUBuffer<core::null_allocator<uint8_t>, true>;

//! Specialization of ICPUBuffer capable of taking custom allocators
/*
Take a look that with this usage you have to specify custom alloctor
passing an object type for allocation and a pointer to allocated
data for it's storage by ICPUBuffer.

So the need for the class existence is for common following tricks - among others creating an
\bICPUBuffer\b over an already existing \bvoid*\b array without any \imemcpy\i or \itaking over the memory ownership\i.
You can use it with a \bnull_allocator\b that adopts memory (it is a bit counter intuitive because \badopt = take\b ownership,
but a \inull allocator\i doesn't do anything, even free the memory, so you're all good).
*/

template<typename Allocator>
class CCustomAllocatorCPUBuffer<Allocator,true> : public ICPUBuffer
{
static_assert(sizeof(typename Allocator::value_type) == 1u, "Allocator::value_type must be of size 1");
protected:
Allocator m_allocator;

virtual ~CCustomAllocatorCPUBuffer() final
{
freeData();
}
inline void freeData() override
{
if (ICPUBuffer::data)
m_allocator.deallocate(reinterpret_cast<typename Allocator::pointer>(ICPUBuffer::data), ICPUBuffer::m_creationParams.size);
ICPUBuffer::data = nullptr; // so that ICPUBuffer won't try deallocating
}

public:
CCustomAllocatorCPUBuffer(size_t sizeInBytes, void* dat, core::adopt_memory_t, Allocator&& alctr = Allocator()) : ICPUBuffer(sizeInBytes,dat), m_allocator(std::move(alctr))
{
}
};

template<typename Allocator>
class CCustomAllocatorCPUBuffer<Allocator, false> : public CCustomAllocatorCPUBuffer<Allocator, true>
{
using Base = CCustomAllocatorCPUBuffer<Allocator, true>;

protected:
virtual ~CCustomAllocatorCPUBuffer() = default;
inline void freeData() override {}

public:
using Base::Base;

// TODO: remove, alloc can fail, should be a static create method instead!
CCustomAllocatorCPUBuffer(size_t sizeInBytes, const void* dat, Allocator&& alctr = Allocator()) : Base(sizeInBytes, alctr.allocate(sizeInBytes), core::adopt_memory, std::move(alctr))
{
memcpy(Base::data,dat,sizeInBytes);
}
protected:
inline IAsset* getDependant_impl(const size_t ix) override
{
return nullptr;
}

inline void discardContent_impl() override
{
if (m_data)
m_mem_resource->deallocate(m_data, m_creationParams.size, m_alignment);
m_data = nullptr;
m_mem_resource = nullptr;
m_creationParams.size = 0ull;
}

private:
ICPUBuffer(SCreationParams&& params) :
asset::IBuffer({ params.size, EUF_TRANSFER_DST_BIT }), m_data(params.data),
m_mem_resource(params.memoryResource), m_alignment(params.alignment) {}

~ICPUBuffer() override {
discardContent_impl();
}

void* m_data;
core::smart_refctd_ptr<core::refctd_memory_resource> m_mem_resource;
size_t m_alignment;
};

} // end namespace nbl::asset
Expand Down
2 changes: 1 addition & 1 deletion include/nbl/asset/ICPUMeshBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ class ICPUMeshBuffer final : public IMeshBuffer<ICPUBuffer,ICPUDescriptorSet,ICP
const bool scaled = isScaledFormat(format);
if (!dst || !(scaled || isIntegerFormat(format)))
return false;
uint8_t* vxPtr = (uint8_t*)dst;
uint8_t* vxPtr = reinterpret_cast<uint8_t*>(dst);

if (isSignedFormat(format))
{
Expand Down
2 changes: 1 addition & 1 deletion include/nbl/asset/ICPUShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ICPUShader : public IAsset, public IShader
: IShader(stage, std::move(filepathHint)), m_code(std::move(code)), m_contentType(contentType) {}

ICPUShader(const char* code, const E_SHADER_STAGE stage, const E_CONTENT_TYPE contentType, std::string&& filepathHint)
: ICPUShader(core::make_smart_refctd_ptr<ICPUBuffer>(strlen(code) + 1u), stage, contentType, std::move(filepathHint))
: ICPUShader(ICPUBuffer::create({ .size = strlen(code) + 1u }), stage, contentType, std::move(filepathHint))
{
assert(contentType != E_CONTENT_TYPE::ECT_SPIRV); // because using strlen needs `code` to be null-terminated
memcpy(m_code->getPointer(), code, m_code->getSize());
Expand Down
2 changes: 1 addition & 1 deletion include/nbl/asset/filters/CFlattenRegionsImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class CFlattenRegionsImageFilter : public CImageFilter<CFlattenRegionsImageFilte
assert(memsize.getNumerator()%memsize.getDenominator()==0u);
bufferSize += memsize.getIntegerApprox();
}
auto buffer = core::make_smart_refctd_ptr<ICPUBuffer>(bufferSize);
auto buffer = ICPUBuffer::create({ .size = bufferSize });
state->outImage->setBufferAndRegions(std::move(buffer),std::move(regions));
};

Expand Down
2 changes: 1 addition & 1 deletion include/nbl/asset/filters/dithering/CPrecomputedDither.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace nbl
const size_t newDecodeBufferSize = extent.x * extent.y * extent.z * creationParams.arrayLayers * decodeTexelByteSize;

const core::vector3du32_SIMD decodeBufferByteStrides = TexelBlockInfo(decodeFormat).convert3DTexelStridesTo1DByteStrides(core::vector3du32_SIMD(extent.x, extent.y, extent.z));
auto decodeFlattenBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(newDecodeBufferSize);
auto decodeFlattenBuffer = ICPUBuffer::create({ .size = newDecodeBufferSize });
decodeFlattenBuffer->setContentHash(IPreHashed::INVALID_HASH);

auto* inData = reinterpret_cast<uint8_t*>(flattenDitheringImage->getBuffer()->getPointer());
Expand Down
2 changes: 1 addition & 1 deletion include/nbl/asset/interchange/IImageAssetHandlerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class IImageAssetHandlerBase : public virtual core::IReferenceCounted
bufferSize += memsize.getIntegerApprox();
}

auto texelBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(bufferSize);
auto texelBuffer = ICPUBuffer::create({ .size = bufferSize });
newImage->setBufferAndRegions(std::move(texelBuffer), newRegions);
newImage->setContentHash(IPreHashed::INVALID_HASH);
}
Expand Down
4 changes: 2 additions & 2 deletions include/nbl/asset/utils/CDirQuantCacheBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class CDirQuantCacheBase : public impl::CDirQuantCacheBase
if (!file)
return false;

auto buffer = core::make_smart_refctd_ptr<asset::ICPUBuffer>(file->getSize());
auto buffer = asset::ICPUBuffer::create({ .size = file->getSize() });

system::IFile::success_t succ;
file->read(succ, buffer->getPointer(), 0, file->getSize());
Expand Down Expand Up @@ -346,7 +346,7 @@ class CDirQuantCacheBase : public impl::CDirQuantCacheBase
asset::SBufferRange<asset::ICPUBuffer> bufferRange;
bufferRange.offset = 0;
bufferRange.size = getSerializedCacheSizeInBytes<CacheFormat>();
bufferRange.buffer = core::make_smart_refctd_ptr<asset::ICPUBuffer>(bufferRange.size);
bufferRange.buffer = asset::ICPUBuffer::create({ .size = bufferRange.size });

saveCacheToBuffer<CacheFormat>(bufferRange);

Expand Down
6 changes: 3 additions & 3 deletions include/nbl/asset/utils/IMeshManipulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ class NBL_API2 IMeshManipulator : public virtual core::IReferenceCounted
core::smart_refctd_ptr<ICPUBuffer> iotaUint32Buffer;
if (iotaLength)
{
iotaUint32Buffer = core::make_smart_refctd_ptr<ICPUBuffer>(sizeof(uint32_t)*iotaLength);
iotaUint32Buffer = ICPUBuffer::create({ .size = sizeof(uint32_t)*iotaLength });
auto ptr = reinterpret_cast<uint32_t*>(iotaUint32Buffer->getPointer());
std::iota(ptr,ptr+iotaLength,0u);
}
Expand Down Expand Up @@ -599,13 +599,13 @@ class NBL_API2 IMeshManipulator : public virtual core::IReferenceCounted
if (indexType==EIT_16BIT)
{
auto inPtr = reinterpret_cast<const uint16_t*>(correctlyOffsetIndexBufferPtr);
newIndexBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(sizeof(uint32_t)*indexCount);
newIndexBuffer = ICPUBuffer::create({ .size = sizeof(uint32_t)*indexCount });
std::copy(inPtr,inPtr+indexCount,reinterpret_cast<uint32_t*>(newIndexBuffer->getPointer()));
}
else
{
auto inPtr = reinterpret_cast<const uint32_t*>(correctlyOffsetIndexBufferPtr);
newIndexBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(sizeof(uint16_t)*indexCount);
newIndexBuffer = ICPUBuffer::create({ .size = sizeof(uint16_t)*indexCount });
std::copy(inPtr,inPtr+indexCount,reinterpret_cast<uint16_t*>(newIndexBuffer->getPointer()));
}
}
Expand Down
3 changes: 2 additions & 1 deletion include/nbl/asset/utils/IShaderCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
core::blake3_hash_t hash = {};
// If true, then `getIncludeStandard` was used to find, otherwise `getIncludeRelative`
bool standardInclude = false;

};

struct SCompilerArgs; // Forward declaration for SPreprocessorArgs's friend declaration
Expand Down Expand Up @@ -525,7 +526,7 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
constexpr size_t nullTerminatorSize = 1u;
size_t outSize = origLen + formatArgsCharSize + formatSize + nullTerminatorSize - 2 * templateArgsCount;

nbl::core::smart_refctd_ptr<ICPUBuffer> outBuffer = nbl::core::make_smart_refctd_ptr<ICPUBuffer>(outSize);
nbl::core::smart_refctd_ptr<ICPUBuffer> outBuffer = ICPUBuffer::create({ .size = outSize });

auto origCode = std::string_view(reinterpret_cast<const char*>(original->getContent()->getPointer()), origLen);
auto outCode = reinterpret_cast<char*>(outBuffer->getPointer());
Expand Down
Loading