@@ -179,10 +179,12 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
179179
180180 class CCache final : public IReferenceCounted
181181 {
182+ friend class IShaderCompiler ;
183+
182184 public:
183185 // Used to check compatibility of Caches before reading
184186 constexpr static inline std::string_view VERSION = " 1.0.0" ;
185-
187+
186188 using hash_t = std::array<uint64_t ,4 >;
187189 static auto const SHADER_BUFFER_SIZE_BYTES = sizeof (uint64_t ) / sizeof (uint8_t ); // It's obviously 8
188190
@@ -362,7 +364,7 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
362364 // Making the copy constructor deep-copy everything but the shader
363365 inline SEntry (const SEntry& other)
364366 : mainFileContents(other.mainFileContents), compilerArgs(other.compilerArgs), hash(other.hash), lookupHash(other.lookupHash),
365- dependencies(other.dependencies), value (other.value ) {}
367+ dependencies(other.dependencies), cpuShader (other.cpuShader ) {}
366368
367369 inline SEntry& operator =(SEntry& other) = delete ;
368370 inline SEntry (SEntry&& other) = default;
@@ -375,7 +377,7 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
375377 std::array<uint64_t ,4 > hash;
376378 size_t lookupHash;
377379 dependency_container_t dependencies;
378- core::smart_refctd_ptr<asset::ICPUShader> value ;
380+ core::smart_refctd_ptr<asset::ICPUShader> cpuShader ;
379381 };
380382
381383 inline void insert (SEntry&& entry)
@@ -426,31 +428,45 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
426428 }
427429
428430 };
429- core::unordered_multiset<SEntry,Hash,KeyEqual> m_container;
431+
432+ using EntrySet = core::unordered_multiset<SEntry, Hash, KeyEqual>;
433+ EntrySet m_container;
434+
435+ NBL_API2 EntrySet::const_iterator find_impl (const SEntry& mainFile, const CIncludeFinder* finder) const ;
430436 };
431437
432438 inline core::smart_refctd_ptr<ICPUShader> compileToSPIRV (const std::string_view code, const SCompilerOptions& options) const
433439 {
434440 CCache::SEntry entry;
435441 std::vector<CCache::SEntry::SPreprocessingDependency> dependencies;
436- if (options.readCache or options.writeCache )
442+ if (options.readCache || options.writeCache )
437443 entry = std::move (CCache::SEntry (code, options));
444+
438445 if (options.readCache )
439446 {
440- auto found = options.readCache ->find (entry, options.preprocessorOptions .includeFinder );
441- if (found)
442- return found;
447+ auto found = options.readCache ->find_impl (entry, options.preprocessorOptions .includeFinder );
448+ if (found != options.readCache ->m_container .end ())
449+ {
450+ if (options.writeCache )
451+ {
452+ CCache::SEntry writeEntry = *found;
453+ options.writeCache ->insert (std::move (writeEntry));
454+ }
455+ return found->cpuShader ;
456+ }
443457 }
458+
444459 auto retVal = compileToSPIRV_impl (code, options, options.writeCache ? &dependencies : nullptr );
445460 // compute the SPIR-V shader content hash
446461 {
447462 auto backingBuffer = retVal->getContent ();
448463 const_cast <ICPUBuffer*>(backingBuffer)->setContentHash (backingBuffer->computeContentHash ());
449464 }
465+
450466 if (options.writeCache )
451467 {
452468 entry.dependencies = std::move (dependencies);
453- entry.value = retVal;
469+ entry.cpuShader = retVal;
454470 options.writeCache ->insert (std::move (entry));
455471 }
456472 return retVal;
0 commit comments