@@ -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-
3128inline 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
290264core::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
295272IShaderCompiler::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+
421431core::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