@@ -25,9 +25,6 @@ IShaderCompiler::IShaderCompiler(core::smart_refctd_ptr<system::ISystem>&& syste
25
25
m_defaultIncludeFinder = core::make_smart_refctd_ptr<CIncludeFinder>(core::smart_refctd_ptr (m_system));
26
26
}
27
27
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
-
31
28
inline core::smart_refctd_ptr<ICPUShader> nbl::asset::IShaderCompiler::compileToSPIRV (const std::string_view code, const SCompilerOptions& options) const
32
29
{
33
30
CCache::SEntry entry;
@@ -58,31 +55,8 @@ inline core::smart_refctd_ptr<ICPUShader> nbl::asset::IShaderCompiler::compileTo
58
55
59
56
if (options.writeCache )
60
57
{
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));
86
60
}
87
61
return retVal;
88
62
}
@@ -289,7 +263,10 @@ auto IShaderCompiler::CIncludeFinder::tryIncludeGenerators(const std::string& in
289
263
290
264
core::smart_refctd_ptr<asset::ICPUShader> IShaderCompiler::CCache::find (const SEntry& mainFile, const IShaderCompiler::CIncludeFinder* finder) const
291
265
{
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 ();
293
270
}
294
271
295
272
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
418
395
return retVal;
419
396
}
420
397
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
+
421
431
core::smart_refctd_ptr<ICPUShader> nbl::asset::IShaderCompiler::CCache::SEntry::decompressShader () const
422
432
{
423
433
auto uncompressedBuf = core::make_smart_refctd_ptr<ICPUBuffer>(uncompressedSize);
0 commit comments