Skip to content

Commit c1c4908

Browse files
committed
ShaderCompiler: Polishing
1 parent 01fff54 commit c1c4908

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
3333
{
3434
system::path absolutePath = {};
3535
std::string contents = {};
36-
std::array<uint64_t, 4> hash = {}; // TODO: we're not yet using IFile::getPrecomputedHash(), so for builtins we can maybe use that in the future
36+
std::array<uint8_t, 32> hash = {}; // TODO: we're not yet using IFile::getPrecomputedHash(), so for builtins we can maybe use that in the future
3737
// Could be used in the future for early rejection of cache hit
3838
//nbl::system::IFileBase::time_point_t lastWriteTime = {};
3939

@@ -185,7 +185,7 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
185185
// Used to check compatibility of Caches before reading
186186
constexpr static inline std::string_view VERSION = "1.0.0";
187187

188-
using hash_t = std::array<uint64_t,4>;
188+
using hash_t = std::array<uint8_t,32>;
189189
static auto const SHADER_BUFFER_SIZE_BYTES = sizeof(uint64_t) / sizeof(uint8_t); // It's obviously 8
190190

191191
struct SEntry
@@ -196,11 +196,9 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
196196
{
197197
public:
198198
// Perf note: hashing while preprocessor lexing is likely to be slower than just hashing the whole array like this
199-
inline SPreprocessingDependency(const system::path& _requestingSourceDir, const std::string_view& _identifier, const std::string_view& _contents, bool _standardInclude, std::array<uint64_t, 4> _hash) :
200-
requestingSourceDir(_requestingSourceDir), identifier(_identifier), contents(_contents), standardInclude(_standardInclude), hash(_hash)
201-
{
202-
assert(!_contents.empty());
203-
}
199+
inline SPreprocessingDependency(const system::path& _requestingSourceDir, const std::string_view& _identifier, bool _standardInclude, std::array<uint8_t, 32> _hash) :
200+
requestingSourceDir(_requestingSourceDir), identifier(_identifier), standardInclude(_standardInclude), hash(_hash)
201+
{}
204202

205203
inline SPreprocessingDependency(SPreprocessingDependency&) = default;
206204
inline SPreprocessingDependency& operator=(SPreprocessingDependency&) = delete;
@@ -218,11 +216,8 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
218216
// path or identifier
219217
system::path requestingSourceDir = "";
220218
std::string identifier = "";
221-
// file contents
222-
// TODO: change to `core::vector<uint8_t>` a compressed blob of LZMA, and store all contents together in the `SEntry`
223-
std::string contents = "";
224219
// hash of the contents - used to check against a found_t
225-
std::array<uint64_t, 4> hash = {};
220+
std::array<uint8_t, 32> hash = {};
226221
// If true, then `getIncludeStandard` was used to find, otherwise `getIncludeRelative`
227222
bool standardInclude = false;
228223
};
@@ -351,9 +346,13 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
351346

352347
// Now add the mainFileContents and produce both lookup and early equality rejection hashes
353348
hashable.insert(hashable.end(), mainFileContents.begin(), mainFileContents.end());
354-
hash = nbl::core::XXHash_256(hashable.data(), hashable.size());
355-
lookupHash = hash[0];
356-
for (auto i = 1u; i < 4; i++) {
349+
350+
core::blake3_hasher hasher;
351+
hasher.update(hashable.data(), hashable.size());
352+
hash = { *static_cast<core::blake3_hash_t>(hasher).data };
353+
// ALI:TODO
354+
lookupHash = std::bit_cast<uint64_t, uint8_t[8]>({hash[0], hash[1], hash[2], hash[3], hash[4], hash[5], hash[6], hash[7]});
355+
for (auto i = 8u; i < 32; i++) {
357356
core::hash_combine<uint64_t>(lookupHash, hash[i]);
358357
}
359358
}
@@ -374,7 +373,7 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
374373
// TODO: make some of these private
375374
std::string mainFileContents;
376375
SCompilerArgs compilerArgs;
377-
std::array<uint64_t,4> hash;
376+
std::array<uint8_t,32> hash;
378377
size_t lookupHash;
379378
dependency_container_t dependencies;
380379
core::smart_refctd_ptr<asset::ICPUShader> cpuShader;
@@ -429,7 +428,7 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
429428

430429
};
431430

432-
using EntrySet = core::unordered_multiset<SEntry, Hash, KeyEqual>;
431+
using EntrySet = core::unordered_set<SEntry, Hash, KeyEqual>;
433432
EntrySet m_container;
434433

435434
NBL_API2 EntrySet::const_iterator find_impl(const SEntry& mainFile, const CIncludeFinder* finder) const;

src/nbl/asset/utils/IShaderCompiler.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ auto IShaderCompiler::CIncludeFinder::getIncludeStandard(const system::path& req
116116
retVal = std::move(contents);
117117
else retVal = m_defaultFileSystemLoader->getInclude(requestingSourceDir.string(), includeName);
118118

119-
retVal.hash = nbl::core::XXHash_256((uint8_t*)(retVal.contents.data()), retVal.contents.size() * (sizeof(char) / sizeof(uint8_t)));
119+
120+
core::blake3_hasher hasher;
121+
hasher.update((uint8_t*)(retVal.contents.data()), retVal.contents.size() * (sizeof(char) / sizeof(uint8_t)));
122+
retVal.hash = { *static_cast<core::blake3_hash_t>(hasher).data };
120123
return retVal;
121124
}
122125

@@ -129,7 +132,10 @@ auto IShaderCompiler::CIncludeFinder::getIncludeRelative(const system::path& req
129132
if (auto contents = m_defaultFileSystemLoader->getInclude(requestingSourceDir.string(), includeName))
130133
retVal = std::move(contents);
131134
else retVal = std::move(trySearchPaths(includeName));
132-
retVal.hash = nbl::core::XXHash_256((uint8_t*)(retVal.contents.data()), retVal.contents.size() * (sizeof(char) / sizeof(uint8_t)));
135+
136+
core::blake3_hasher hasher;
137+
hasher.update((uint8_t*)(retVal.contents.data()), retVal.contents.size() * (sizeof(char) / sizeof(uint8_t)));
138+
retVal.hash = { *static_cast<core::blake3_hash_t>(hasher).data };
133139
return retVal;
134140
}
135141

@@ -238,7 +244,7 @@ IShaderCompiler::CCache::EntrySet::const_iterator IShaderCompiler::CCache::find_
238244
else
239245
header = finder->getIncludeRelative(dependency.requestingSourceDir, dependency.identifier);
240246

241-
if (header.hash != dependency.hash || header.contents != dependency.contents)
247+
if (header.hash != dependency.hash)
242248
{
243249
allDependenciesMatch = false;
244250
break;

src/nbl/asset/utils/shaderCompiler_serialization.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ inline void to_json(json& j, const SEntry::SPreprocessingDependency& dependency)
116116
j = json{
117117
{ "requestingSourceDir", dependency.requestingSourceDir },
118118
{ "identifier", dependency.identifier },
119-
{ "contents", dependency.contents },
120119
{ "hash", dependency.hash },
121120
{ "standardInclude", dependency.standardInclude },
122121
};
@@ -126,7 +125,6 @@ inline void from_json(const json& j, SEntry::SPreprocessingDependency& dependenc
126125
{
127126
j.at("requestingSourceDir").get_to(dependency.requestingSourceDir);
128127
j.at("identifier").get_to(dependency.identifier);
129-
j.at("contents").get_to(dependency.contents);
130128
j.at("hash").get_to(dependency.hash);
131129
j.at("standardInclude").get_to(dependency.standardInclude);
132130
}

src/nbl/asset/utils/waveContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ template<> inline bool boost::wave::impl::pp_iterator_functor<nbl::wave::context
533533

534534
// If caching was requested, push a new SDependency onto dependencies
535535
if (ctx.cachingRequested) {
536-
ctx.dependencies.emplace_back(ctx.get_current_directory(), file_path, result.contents, standardInclude, std::move(result.hash));
536+
ctx.dependencies.emplace_back(ctx.get_current_directory(), file_path, standardInclude, std::move(result.hash));
537537
}
538538

539539
ctx.located_include_content = std::move(result.contents);

0 commit comments

Comments
 (0)