Skip to content

Commit 1404e35

Browse files
committed
Merge branch 'shader_caching_fixes'
2 parents edd7e62 + cab3b5b commit 1404e35

File tree

5 files changed

+50
-21
lines changed

5 files changed

+50
-21
lines changed

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

include/nbl/system/ISystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class NBL_API2 ISystem : public core::IReferenceCounted
9494

9595
// can only perform operations on non-virtual filesystem paths
9696
bool deleteDirectory(const system::path& p);
97+
98+
// can only perform operations on non-virtual filesystem paths
99+
bool deleteFile(const system::path& p);
97100

98101
// can only perform operations on non-virtual filesystem paths
99102
std::error_code moveFileOrDirectory(const system::path& oldPath, const system::path& newPath);

src/nbl/asset/utils/IShaderCompiler.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ auto IShaderCompiler::CIncludeFinder::tryIncludeGenerators(const std::string& in
217217
}
218218

219219
core::smart_refctd_ptr<asset::ICPUShader> IShaderCompiler::CCache::find(const SEntry& mainFile, const IShaderCompiler::CIncludeFinder* finder) const
220+
{
221+
return find_impl(mainFile, finder)->cpuShader;
222+
}
223+
224+
IShaderCompiler::CCache::EntrySet::const_iterator IShaderCompiler::CCache::find_impl(const SEntry& mainFile, const IShaderCompiler::CIncludeFinder* finder) const
220225
{
221226
auto foundRange = m_container.equal_range(mainFile);
222227
for (auto& found = foundRange.first; found != foundRange.second; found++)
@@ -239,11 +244,10 @@ core::smart_refctd_ptr<asset::ICPUShader> IShaderCompiler::CCache::find(const SE
239244
break;
240245
}
241246
}
242-
if (allDependenciesMatch) {
243-
return found->value;
244-
}
247+
if (allDependenciesMatch)
248+
return found;
245249
}
246-
return nullptr;
250+
return m_container.end();
247251
}
248252

249253
core::smart_refctd_ptr<ICPUBuffer> IShaderCompiler::CCache::serialize() const
@@ -263,10 +267,10 @@ core::smart_refctd_ptr<ICPUBuffer> IShaderCompiler::CCache::serialize() const
263267
// We keep a copy of the offsets and the sizes of each shader. This is so that later on, when we add the shaders to the buffer after json creation
264268
// (where the params array has been moved) we don't have to read the json to get the offsets again
265269
offsets[i] = shaderBufferSize;
266-
sizes[i] = entry.value->getContent()->getSize();
270+
sizes[i] = entry.cpuShader->getContent()->getSize();
267271

268272
// And add the params to the shader creation parameters array
269-
shaderCreationParams.emplace_back(entry.value->getStage(), entry.value->getContentType(), entry.value->getFilepathHint(), sizes[i], shaderBufferSize);
273+
shaderCreationParams.emplace_back(entry.cpuShader->getStage(), entry.cpuShader->getContentType(), entry.cpuShader->getFilepathHint(), sizes[i], shaderBufferSize);
270274
// Enlarge the shader buffer by the size of the current shader
271275
shaderBufferSize += sizes[i];
272276
i++;
@@ -290,7 +294,7 @@ core::smart_refctd_ptr<ICPUBuffer> IShaderCompiler::CCache::serialize() const
290294
// Loop over entries again, adding each one's shader to the buffer.
291295
i = 0u;
292296
for (auto& entry : m_container) {
293-
memcpy(retVal.data() + SHADER_BUFFER_SIZE_BYTES + offsets[i], entry.value->getContent()->getPointer(), sizes[i]);
297+
memcpy(retVal.data() + SHADER_BUFFER_SIZE_BYTES + offsets[i], entry.cpuShader->getContent()->getPointer(), sizes[i]);
294298
i++;
295299
}
296300

@@ -336,9 +340,7 @@ core::smart_refctd_ptr<IShaderCompiler::CCache> IShaderCompiler::CCache::deseria
336340
memcpy(code->getPointer(), serializedCache.data() + SHADER_BUFFER_SIZE_BYTES + shaderCreationParams[i].offset, shaderCreationParams[i].codeByteSize);
337341
code->setContentHash(code->computeContentHash());
338342
// Create the ICPUShader
339-
auto value = core::make_smart_refctd_ptr<ICPUShader>(std::move(code), shaderCreationParams[i].stage, shaderCreationParams[i].contentType, std::move(shaderCreationParams[i].filepathHint));
340-
341-
entries[i].value = std::move(value);
343+
entries[i].cpuShader = core::make_smart_refctd_ptr<ICPUShader>(std::move(code), shaderCreationParams[i].stage, shaderCreationParams[i].contentType, std::move(shaderCreationParams[i].filepathHint));
342344

343345
retVal->insert(std::move(entries[i]));
344346
}

src/nbl/asset/utils/shaderCompiler_serialization.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ inline void from_json(const json& j, SEntry& entry)
192192
j.at("hash").get_to(entry.hash);
193193
j.at("lookupHash").get_to(entry.lookupHash);
194194
j.at("dependencies").get_to(entry.dependencies);
195-
entry.value = nullptr;
195+
entry.cpuShader = nullptr;
196196
}
197197

198198
}

src/nbl/system/ISystem.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,20 @@ bool ISystem::createDirectory(const system::path& p)
114114

115115
bool ISystem::deleteDirectory(const system::path& p)
116116
{
117-
if (std::filesystem::exists(p))
117+
if (std::filesystem::exists(p) && std::filesystem::is_directory(p))
118118
return std::filesystem::remove_all(p);
119119
else
120120
return false;
121121
}
122122

123+
bool nbl::system::ISystem::deleteFile(const system::path& p)
124+
{
125+
if (std::filesystem::exists(p) && !std::filesystem::is_directory(p)))
126+
return std::filesystem::remove(p);
127+
else
128+
return false;
129+
}
130+
123131
std::error_code ISystem::moveFileOrDirectory(const system::path& oldPath, const system::path& newPath)
124132
{
125133
std::error_code ec;

0 commit comments

Comments
 (0)