Skip to content

Commit 3445f61

Browse files
committed
resolve reviews
Signed-off-by: Ali Cheraghi <alichraghi@proton.me>
1 parent ff7ed06 commit 3445f61

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,6 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
354354
lookupHash = std::hash<core::blake3_hash_t>{}(hash);
355355
}
356356

357-
// Making an entry to insert into write cache
358-
inline SEntry(const SEntry& other, dependency_container_t dependencies, core::smart_refctd_ptr<asset::ICPUBuffer> spirv,
359-
core::blake3_hash_t uncompressedContentHash, size_t uncompressedSize)
360-
: mainFileContents(other.mainFileContents), compilerArgs(other.compilerArgs), hash(other.hash),
361-
lookupHash(other.lookupHash), dependencies(dependencies), spirv(spirv),
362-
uncompressedContentHash(uncompressedContentHash), uncompressedSize(uncompressedSize) {}
363-
364357
// Needed to get the vector deserialization automatically
365358
inline SEntry() {}
366359

@@ -375,6 +368,8 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
375368
// Used for late initialization while looking up a cache, so as not to always initialize an entry even if caching was not requested
376369
inline SEntry& operator=(SEntry&& other) = default;
377370

371+
void setContent(const asset::ICPUBuffer* uncompressedSpirvBuffer, dependency_container_t&& dependencies);
372+
378373
core::smart_refctd_ptr<ICPUShader> decompressShader() const;
379374

380375
// TODO: make some of these private

src/nbl/asset/utils/IShaderCompiler.cpp

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ IShaderCompiler::IShaderCompiler(core::smart_refctd_ptr<system::ISystem>&& syste
2525
m_defaultIncludeFinder = core::make_smart_refctd_ptr<CIncludeFinder>(core::smart_refctd_ptr(m_system));
2626
}
2727

28-
static void* SzAlloc(ISzAllocPtr p, size_t size) { p = p; return _NBL_ALIGNED_MALLOC(size, _NBL_SIMD_ALIGNMENT); }
29-
static void SzFree(ISzAllocPtr p, void* address) { p = p; _NBL_ALIGNED_FREE(address); }
30-
3128
inline core::smart_refctd_ptr<ICPUShader> nbl::asset::IShaderCompiler::compileToSPIRV(const std::string_view code, const SCompilerOptions& options) const
3229
{
3330
CCache::SEntry entry;
@@ -58,31 +55,8 @@ inline core::smart_refctd_ptr<ICPUShader> nbl::asset::IShaderCompiler::compileTo
5855

5956
if (options.writeCache)
6057
{
61-
auto* spirvBuffer = retVal->getContent();
62-
size_t propsSize = LZMA_PROPS_SIZE;
63-
size_t destLen = spirvBuffer->getSize() + spirvBuffer->getSize() / 3 + 128;
64-
std::vector<unsigned char> compressedSpirv = {};
65-
compressedSpirv.resize(propsSize + destLen);
66-
67-
CLzmaEncProps props;
68-
LzmaEncProps_Init(&props);
69-
props.dictSize = 1 << 16; // 64KB
70-
props.writeEndMark = 1;
71-
72-
ISzAlloc alloc = { SzAlloc, SzFree };
73-
int res = LzmaEncode(
74-
compressedSpirv.data() + LZMA_PROPS_SIZE, &destLen,
75-
reinterpret_cast<const unsigned char*>(spirvBuffer->getPointer()), spirvBuffer->getSize(),
76-
&props, compressedSpirv.data(), &propsSize, props.writeEndMark,
77-
nullptr, &alloc, &alloc);
78-
79-
assert(propsSize == LZMA_PROPS_SIZE);
80-
assert(res == SZ_OK);
81-
82-
auto compressedSpirvBuffer = core::make_smart_refctd_ptr<ICPUBuffer>(propsSize + destLen);
83-
memcpy(compressedSpirvBuffer->getPointer(), compressedSpirv.data(), compressedSpirvBuffer->getSize());
84-
85-
options.writeCache->insert(std::move(SEntry(entry, std::move(dependencies), std::move(compressedSpirvBuffer), spirvBuffer->getContentHash(), spirvBuffer->getSize())));
58+
entry.setContent(retVal->getContent(), std::move(dependencies));
59+
options.writeCache->insert(std::move(entry));
8660
}
8761
return retVal;
8862
}
@@ -289,7 +263,10 @@ auto IShaderCompiler::CIncludeFinder::tryIncludeGenerators(const std::string& in
289263

290264
core::smart_refctd_ptr<asset::ICPUShader> IShaderCompiler::CCache::find(const SEntry& mainFile, const IShaderCompiler::CIncludeFinder* finder) const
291265
{
292-
return find_impl(mainFile, finder)->decompressShader();
266+
const auto found = find_impl(mainFile, finder);
267+
if (found==m_container.end())
268+
return nullptr;
269+
return found->decompressShader();
293270
}
294271

295272
IShaderCompiler::CCache::EntrySet::const_iterator IShaderCompiler::CCache::find_impl(const SEntry& mainFile, const IShaderCompiler::CIncludeFinder* finder) const
@@ -418,6 +395,39 @@ core::smart_refctd_ptr<IShaderCompiler::CCache> IShaderCompiler::CCache::deseria
418395
return retVal;
419396
}
420397

398+
static void* SzAlloc(ISzAllocPtr p, size_t size) { p = p; return _NBL_ALIGNED_MALLOC(size, _NBL_SIMD_ALIGNMENT); }
399+
static void SzFree(ISzAllocPtr p, void* address) { p = p; _NBL_ALIGNED_FREE(address); }
400+
401+
void nbl::asset::IShaderCompiler::CCache::SEntry::setContent(const asset::ICPUBuffer* uncompressedSpirvBuffer, dependency_container_t&& dependencies)
402+
{
403+
dependencies = std::move(dependencies);
404+
uncompressedContentHash = uncompressedSpirvBuffer->getContentHash();
405+
uncompressedSize = uncompressedSpirvBuffer->getSize();
406+
407+
size_t propsSize = LZMA_PROPS_SIZE;
408+
size_t destLen = uncompressedSpirvBuffer->getSize() + uncompressedSpirvBuffer->getSize() / 3 + 128;
409+
std::vector<unsigned char> compressedSpirv = {};
410+
compressedSpirv.resize(propsSize + destLen);
411+
412+
CLzmaEncProps props;
413+
LzmaEncProps_Init(&props);
414+
props.dictSize = 1 << 16; // 64KB
415+
props.writeEndMark = 1;
416+
417+
ISzAlloc alloc = { SzAlloc, SzFree };
418+
int res = LzmaEncode(
419+
compressedSpirv.data() + LZMA_PROPS_SIZE, &destLen,
420+
reinterpret_cast<const unsigned char*>(uncompressedSpirvBuffer->getPointer()), uncompressedSpirvBuffer->getSize(),
421+
&props, compressedSpirv.data(), &propsSize, props.writeEndMark,
422+
nullptr, &alloc, &alloc);
423+
424+
assert(propsSize == LZMA_PROPS_SIZE);
425+
assert(res == SZ_OK);
426+
427+
spirv = core::make_smart_refctd_ptr<ICPUBuffer>(propsSize + destLen);
428+
memcpy(spirv->getPointer(), compressedSpirv.data(), spirv->getSize());
429+
}
430+
421431
core::smart_refctd_ptr<ICPUShader> nbl::asset::IShaderCompiler::CCache::SEntry::decompressShader() const
422432
{
423433
auto uncompressedBuf = core::make_smart_refctd_ptr<ICPUBuffer>(uncompressedSize);

0 commit comments

Comments
 (0)