-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathIShaderCompiler.h
More file actions
604 lines (486 loc) · 25.9 KB
/
IShaderCompiler.h
File metadata and controls
604 lines (486 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// Copyright (C) 2018-2024 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
#ifndef _NBL_ASSET_I_SHADER_COMPILER_H_INCLUDED_
#define _NBL_ASSET_I_SHADER_COMPILER_H_INCLUDED_
#include "nbl/core/declarations.h"
#include "nbl/system/declarations.h"
#include "nbl/system/IFile.h"
#include "nbl/system/ISystem.h"
#include "nbl/asset/IShader.h"
#include "nbl/asset/utils/ISPIRVOptimizer.h"
#include "nbl/system/json.h"
#include "nbl/builtin/hlsl/enums.hlsl"
#include <functional>
namespace nbl::asset
{
class NBL_API2 IShaderCompiler : public core::IReferenceCounted
{
public:
IShaderCompiler(core::smart_refctd_ptr<system::ISystem>&& system);
class NBL_API2 IIncludeLoader : public core::IReferenceCounted
{
public:
struct found_t
{
system::path absolutePath = {};
std::string contents = {};
core::blake3_hash_t hash = {}; // TODO: we're not yet using IFile::getPrecomputedHash(), so for builtins we can maybe use that in the future
// Could be used in the future for early rejection of cache hit
//nbl::system::IFileBase::time_point_t lastWriteTime = {};
explicit inline operator bool() const {return !absolutePath.empty();}
};
virtual found_t getInclude(const system::path& searchPath, const std::string& includeName) const = 0;
};
class NBL_API2 IIncludeGenerator : public core::IReferenceCounted
{
public:
// ! if includeName doesn't begin with prefix from `getPrefix` this function will return an empty string
virtual IIncludeLoader::found_t getInclude(const std::string& includeName) const;
virtual std::string_view getPrefix() const = 0;
protected:
using HandleFunc_t = std::function<std::string(const std::string&)>;
virtual core::vector<std::pair<std::regex,HandleFunc_t>> getBuiltinNamesToFunctionMapping() const = 0;
// ! Parses arguments from include path
// ! template is path/to/shader.hlsl/arg0/arg1/...
static core::vector<std::string> parseArgumentsFromPath(const std::string& _path);
};
class NBL_API2 CFileSystemIncludeLoader : public IIncludeLoader
{
public:
CFileSystemIncludeLoader(core::smart_refctd_ptr<system::ISystem>&& system);
IIncludeLoader::found_t getInclude(const system::path& searchPath, const std::string& includeName) const override;
protected:
core::smart_refctd_ptr<system::ISystem> m_system;
};
class NBL_API2 CIncludeFinder : public core::IReferenceCounted
{
public:
CIncludeFinder(core::smart_refctd_ptr<system::ISystem>&& system);
// ! includes within <>
// @param requestingSourceDir: the directory where the incude was requested
// @param includeName: the string within <> of the include preprocessing directive
IIncludeLoader::found_t getIncludeStandard(const system::path& requestingSourceDir, const std::string& includeName) const;
// ! includes within ""
// @param requestingSourceDir: the directory where the incude was requested
// @param includeName: the string within "" of the include preprocessing directive
IIncludeLoader::found_t getIncludeRelative(const system::path& requestingSourceDir, const std::string& includeName) const;
inline core::smart_refctd_ptr<CFileSystemIncludeLoader> getDefaultFileSystemLoader() const { return m_defaultFileSystemLoader; }
void addSearchPath(const std::string& searchPath, const core::smart_refctd_ptr<IIncludeLoader>& loader);
void addGenerator(const core::smart_refctd_ptr<IIncludeGenerator>& generator);
protected:
IIncludeLoader::found_t trySearchPaths(const std::string& includeName) const;
IIncludeLoader::found_t tryIncludeGenerators(const std::string& includeName) const;
struct LoaderSearchPath
{
core::smart_refctd_ptr<IIncludeLoader> loader = nullptr;
std::string searchPath = {};
};
std::vector<LoaderSearchPath> m_loaders;
std::vector<core::smart_refctd_ptr<IIncludeGenerator>> m_generators;
core::smart_refctd_ptr<CFileSystemIncludeLoader> m_defaultFileSystemLoader;
};
//
struct SMacroDefinition
{
std::string_view identifier;
std::string_view definition;
friend struct system::json::adl_serializer<SMacroDefinition>;
};
//
using E_SPIRV_VERSION = nbl::hlsl::SpirvVersion;
static inline constexpr uint32_t getSpirvMajor(E_SPIRV_VERSION version) {
return (version >> 16u) & 0xFFu;
}
static inline constexpr uint32_t getSpirvMinor(E_SPIRV_VERSION version) {
return (version >> 8u) & 0xFFu;
}
//
struct SPreprocessorOptions
{
std::string_view sourceIdentifier = "";
system::logger_opt_ptr logger = nullptr;
const CIncludeFinder* includeFinder = nullptr;
std::span<const SMacroDefinition> extraDefines = {};
E_SPIRV_VERSION targetSpirvVersion = E_SPIRV_VERSION::ESV_1_6;
bool depfile = false;
system::path depfilePath = {};
std::function<void(std::string_view)> onPartialOutputOnFailure = {};
};
// https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/SPIR-V.rst#debugging
enum class E_DEBUG_INFO_FLAGS : uint8_t
{
EDIF_NONE = 0x00,
EDIF_FILE_BIT = 0x01, // for emitting full path of the main source file
EDIF_SOURCE_BIT = 0x02, // for emitting preprocessed source code (turns on EDIF_FILE_BIT implicitly)
EDIF_LINE_BIT = 0x04, // for emitting line information (turns on EDIF_SOURCE_BIT implicitly)
EDIF_TOOL_BIT = 0x08, // for emitting Compiler Git commit hash and command-line options
EDIF_NON_SEMANTIC_BIT = 0x10, // NonSemantic.Shader.DebugInfo.100 extended instructions, this option overrules the options above
};
// Forward declaration for SCompilerOptions use
struct CCache;
/*
@stage shaderStage, can be ESS_ALL_OR_LIBRARY to make multi-entrypoint shaders
@targetSpirvVersion spirv version
@entryPoint entryPoint
@outAssembly Optional parameter; if not nullptr, SPIR-V assembly is saved in there.
@spirvOptimizer Optional parameter;
@debugInfoFlags See E_DEBUG_INFO_FLAGS enum for more information on possible values
Anything non-vulkan, basically you can't recover the names of original variables with CSPIRVIntrospector without debug info
By variables we mean names of PC/SSBO/UBO blocks, as they're essentially instantiations of structs with custom packing.
@preprocessorOptions
@sourceIdentifier String that will be printed along with possible errors as source identifier, and used as include's requestingSrc
@logger Optional parameter; used for logging errors/info
@includeFinder Optional parameter; if not nullptr, it will resolve the includes in the code
@maxSelfInclusionCount used only when includeFinder is not nullptr
@extraDefines adds extra defines to the shader before compilation
*/
struct SCompilerOptions
{
inline void setCommonData(const SCompilerOptions& opt)
{
(*this) = opt;
}
virtual IShader::E_CONTENT_TYPE getCodeContentType() const { return IShader::E_CONTENT_TYPE::ECT_UNKNOWN; };
IShader::E_SHADER_STAGE stage = IShader::E_SHADER_STAGE::ESS_ALL_OR_LIBRARY;
const ISPIRVOptimizer* spirvOptimizer = nullptr;
core::bitflag<E_DEBUG_INFO_FLAGS> debugInfoFlags = core::bitflag<E_DEBUG_INFO_FLAGS>(E_DEBUG_INFO_FLAGS::EDIF_SOURCE_BIT) | E_DEBUG_INFO_FLAGS::EDIF_TOOL_BIT;
SPreprocessorOptions preprocessorOptions = {};
CCache* readCache = nullptr;
CCache* writeCache = nullptr;
};
class CCache final : public IReferenceCounted
{
friend class IShaderCompiler;
public:
// Used to check compatibility of Caches before reading
constexpr static inline std::string_view VERSION = "1.1.0";
static auto const SHADER_BUFFER_SIZE_BYTES = sizeof(uint64_t) / sizeof(uint8_t); // It's obviously 8
struct SEntry
{
friend class CCache;
struct SPreprocessingDependency
{
public:
// Perf note: hashing while preprocessor lexing is likely to be slower than just hashing the whole array like this
inline SPreprocessingDependency(const system::path& _requestingSourceDir, const std::string_view& _identifier, bool _standardInclude, core::blake3_hash_t _hash) :
requestingSourceDir(_requestingSourceDir), identifier(_identifier), standardInclude(_standardInclude), hash(_hash)
{}
inline SPreprocessingDependency(SPreprocessingDependency&) = default;
inline SPreprocessingDependency& operator=(SPreprocessingDependency&) = delete;
inline SPreprocessingDependency(SPreprocessingDependency&&) = default;
inline SPreprocessingDependency& operator=(SPreprocessingDependency&&) = default;
// Needed for json vector serialization. Making it private and declaring from_json(_, SEntry&) as friend didn't work
inline SPreprocessingDependency() {}
inline const system::path& getRequestingSourceDir() const { return requestingSourceDir; }
inline std::string_view getIdentifier() const { return identifier; }
inline bool isStandardInclude() const { return standardInclude; }
private:
friend class CCache;
friend struct system::json::adl_serializer<SEntry::SPreprocessingDependency>;
// path or identifier
system::path requestingSourceDir = "";
std::string identifier = "";
// hash of the contents - used to check against a found_t
core::blake3_hash_t hash = {};
// If true, then `getIncludeStandard` was used to find, otherwise `getIncludeRelative`
bool standardInclude = false;
};
struct SCompilerArgs; // Forward declaration for SPreprocessorArgs's friend declaration
struct SPreprocessorArgs final
{
public:
inline bool operator==(const SPreprocessorArgs& other) const
{
if (sourceIdentifier != other.sourceIdentifier) return false;
if (extraDefines.size() != other.extraDefines.size()) return false;
for (auto definesIt = extraDefines.begin(), otherDefinesIt = other.extraDefines.begin(); definesIt != extraDefines.end(); definesIt++, otherDefinesIt++)
if (definesIt->identifier != otherDefinesIt->identifier || definesIt->definition != otherDefinesIt->definition)
return false;
return true;
}
private:
friend class SCompilerArgs;
friend class SEntry;
friend class CCache;
friend struct system::json::adl_serializer<SPreprocessorArgs>;
// Default constructor needed for json serialization of SCompilerArgs
SPreprocessorArgs() {};
inline SPreprocessorArgs(const SPreprocessorArgs&) = default;
inline SPreprocessorArgs& operator=(const SPreprocessorArgs&) = delete;
inline SPreprocessorArgs(SPreprocessorArgs&&) = delete;
inline SPreprocessorArgs& operator=(SPreprocessorArgs&&) = default;
// Only SCompilerArgs should instantiate this struct
SPreprocessorArgs(const SPreprocessorOptions& options) : sourceIdentifier(options.sourceIdentifier)
{
for (auto define : options.extraDefines)
extraDefines.emplace_back(std::string(define.identifier), std::string(define.definition));
// Sort them so equality and hashing are well defined
std::sort(extraDefines.begin(), extraDefines.end(), [](const SMacroDefinition& lhs, const SMacroDefinition& rhs) {return lhs.identifier < rhs.identifier; });
};
std::string sourceIdentifier;
std::vector<SMacroDefinition> extraDefines;
};
// TODO: SPreprocessorArgs could just be folded into `SCompilerArgs` to have less classes and decompressShader
struct SCompilerArgs final
{
public:
inline bool operator==(const SCompilerArgs& other) const {
bool retVal = true;
if (stage != other.stage || targetSpirvVersion != other.targetSpirvVersion || debugInfoFlags != other.debugInfoFlags || preprocessorArgs != other.preprocessorArgs) retVal = false;
if (optimizerPasses.size() != other.optimizerPasses.size()) retVal = false;
for (auto passesIt = optimizerPasses.begin(), otherPassesIt = other.optimizerPasses.begin(); passesIt != optimizerPasses.end(); passesIt++, otherPassesIt++) {
if (*passesIt != *otherPassesIt) {
retVal = false;
break;
}
}
return retVal;
}
private:
friend class SEntry;
friend class CCache;
friend struct system::json::adl_serializer<SCompilerArgs>;
// Default constructor needed for json serialization of SEntry
SCompilerArgs() {}
// Default copy needed for SEntry cloning
inline SCompilerArgs(const SCompilerArgs&) = default;
inline SCompilerArgs& operator=(const SCompilerArgs&) = delete;
inline SCompilerArgs(SCompilerArgs&&) = delete;
inline SCompilerArgs& operator=(SCompilerArgs&&) = default;
// Only SEntry should instantiate this struct
SCompilerArgs(const SCompilerOptions& options)
: stage(options.stage), targetSpirvVersion(options.preprocessorOptions.targetSpirvVersion), debugInfoFlags(options.debugInfoFlags), preprocessorArgs(options.preprocessorOptions)
{
if (options.spirvOptimizer) {
for (auto pass : options.spirvOptimizer->getPasses())
optimizerPasses.push_back(pass);
}
}
IShader::E_SHADER_STAGE stage;
E_SPIRV_VERSION targetSpirvVersion;
std::vector<ISPIRVOptimizer::E_OPTIMIZER_PASS> optimizerPasses;
core::bitflag<E_DEBUG_INFO_FLAGS> debugInfoFlags;
SPreprocessorArgs preprocessorArgs;
};
// The ordering is important here, the dependencies MUST be added to the array IN THE ORDER THE PREPROCESSOR INCLUDED THEM!
using dependency_container_t = std::vector<SPreprocessingDependency>;
// Lookup Hash is precompued at entry creation time. Even though in a preprocessing pass the compiler options' stage might change from a #pragma,
// we can't update the info that the Cache uses to find entries post preprocessing: this would make it so that we need to preprocess entries
// to get the "real" stage before lookup in the cache, defeating its purpose
inline SEntry(const std::string_view _mainFileContents, const SCompilerOptions& compilerOptions) : mainFileContents(std::move(std::string(_mainFileContents))), compilerArgs(compilerOptions)
{
// Form the hashable for the compiler data
size_t preprocessorArgsHashableSize = compilerArgs.preprocessorArgs.sourceIdentifier.size() + compilerArgs.preprocessorArgs.extraDefines.size() * sizeof(SMacroDefinition);
size_t compilerArgsHashableSize = sizeof(compilerArgs.stage) + sizeof(compilerArgs.targetSpirvVersion) + sizeof(compilerArgs.debugInfoFlags.value) + compilerArgs.optimizerPasses.size();
std::vector<uint8_t> hashable;
hashable.reserve(preprocessorArgsHashableSize + compilerArgsHashableSize + mainFileContents.size());
// Insert preproc stuff
hashable.insert(hashable.end(), compilerArgs.preprocessorArgs.sourceIdentifier.begin(), compilerArgs.preprocessorArgs.sourceIdentifier.end());
for (const auto& defines : compilerArgs.preprocessorArgs.extraDefines)
{
hashable.insert(hashable.end(), defines.identifier.begin(), defines.identifier.end());
hashable.insert(hashable.end(), defines.definition.begin(), defines.definition.end());
}
// Insert rest of stuff from this struct. We're going to treat stage, targetSpirvVersion and debugInfoFlags.value as byte arrays for simplicity
hashable.insert(hashable.end(), reinterpret_cast<uint8_t*>(&compilerArgs.stage), reinterpret_cast<uint8_t*>(&compilerArgs.stage) + sizeof(compilerArgs.stage));
hashable.insert(hashable.end(), reinterpret_cast<uint8_t*>(&compilerArgs.targetSpirvVersion), reinterpret_cast<uint8_t*>(&compilerArgs.targetSpirvVersion) + sizeof(compilerArgs.targetSpirvVersion));
hashable.insert(hashable.end(), reinterpret_cast<uint8_t*>(&compilerArgs.debugInfoFlags.value), reinterpret_cast<uint8_t*>(&compilerArgs.debugInfoFlags.value) + sizeof(compilerArgs.debugInfoFlags.value));
for (auto pass : compilerArgs.optimizerPasses) {
hashable.push_back(static_cast<uint8_t>(pass));
}
// Now add the mainFileContents and produce both lookup and early equality rejection hashes
hashable.insert(hashable.end(), mainFileContents.begin(), mainFileContents.end());
core::blake3_hasher hasher;
hasher.update(hashable.data(), hashable.size());
hash = static_cast<core::blake3_hash_t>(hasher);
lookupHash = std::hash<core::blake3_hash_t>{}(hash);
}
// Needed to get the vector deserialization automatically
inline SEntry() {}
// Making the copy constructor deep-copy everything but the shader
inline SEntry(const SEntry& other)
: mainFileContents(other.mainFileContents), compilerArgs(other.compilerArgs), hash(other.hash),
lookupHash(other.lookupHash), dependencies(other.dependencies), spirv(other.spirv),
uncompressedContentHash(other.uncompressedContentHash), uncompressedSize(other.uncompressedSize) {}
inline SEntry& operator=(SEntry& other) = delete;
inline SEntry(SEntry&& other) = default;
// Used for late initialization while looking up a cache, so as not to always initialize an entry even if caching was not requested
inline SEntry& operator=(SEntry&& other) = default;
bool setContent(const asset::ICPUBuffer* uncompressedSpirvBuffer);
core::smart_refctd_ptr<IShader> decompressShader() const;
// TODO: make some of these private
std::string mainFileContents;
SCompilerArgs compilerArgs;
core::blake3_hash_t hash;
size_t lookupHash;
dependency_container_t dependencies;
core::smart_refctd_ptr<asset::ICPUBuffer> spirv;
core::blake3_hash_t uncompressedContentHash;
size_t uncompressedSize;
};
inline void insert(SEntry&& entry)
{
m_container.insert(std::move(entry));
}
// For now, the merge incorporates what it can. Once we have lastWriteTime going, matching entries could be replaced by the most recent one
// Alternatively, adding the time an SEntry entered the cache could also serve this purpose
inline void merge(const CCache* other)
{
for (auto& entry : other->m_container)
m_container.emplace(entry);
}
inline core::smart_refctd_ptr<CCache> clone()
{
auto retVal = core::make_smart_refctd_ptr<CCache>();
for (auto& entry : m_container)
retVal->m_container.emplace(entry);
return retVal;
}
NBL_API2 core::smart_refctd_ptr<asset::IShader> find(const SEntry& mainFile, const CIncludeFinder* finder) const;
inline CCache() {}
// De/serialization methods
NBL_API2 core::smart_refctd_ptr<ICPUBuffer> serialize() const;
NBL_API2 static core::smart_refctd_ptr<CCache> deserialize(const std::span<const uint8_t> serializedCache);
private:
// we only do lookups based on main file contents + compiler options
struct Hash
{
inline size_t operator()(const SEntry& entry) const noexcept
{
return entry.lookupHash;
}
};
struct KeyEqual
{
// used for insertions
inline bool operator()(const SEntry& lhs, const SEntry& rhs) const
{
return lhs.compilerArgs == rhs.compilerArgs && lhs.mainFileContents == rhs.mainFileContents;
}
};
using EntrySet = core::unordered_set<SEntry, Hash, KeyEqual>;
EntrySet m_container;
NBL_API2 EntrySet::const_iterator find_impl(const SEntry& mainFile, const CIncludeFinder* finder) const;
};
struct DepfileWriteParams
{
system::ISystem* system = nullptr;
std::string_view depfilePath = {};
std::string_view outputPath = {};
std::string_view sourceIdentifier = {};
system::path workingDirectory = {};
};
static bool writeDepfile(const DepfileWriteParams& params, const CCache::SEntry::dependency_container_t& dependencies, const CIncludeFinder* includeFinder = nullptr, system::logger_opt_ptr logger = nullptr);
core::smart_refctd_ptr<IShader> compileToSPIRV(const std::string_view code, const SCompilerOptions& options) const;
inline core::smart_refctd_ptr<IShader> compileToSPIRV(const char* code, const SCompilerOptions& options) const
{
if (!code)
return nullptr;
return compileToSPIRV({code,strlen(code)},options);
}
inline core::smart_refctd_ptr<IShader> compileToSPIRV(system::IFile* sourceFile, const SCompilerOptions& options) const
{
size_t fileSize = sourceFile->getSize();
std::string code(fileSize,'\0');
system::IFile::success_t success;
sourceFile->read(success, code.data(), 0, fileSize);
if (success)
return compileToSPIRV(code,options);
else
return nullptr;
}
/**
Resolves ALL #include directives regardless of any other preprocessor directive.
This is done in order to support `#include` AND simultaneulsy be able to store (serialize) such IShader (mostly High Level source) into ONE file which, upon loading, will compile on every hardware/driver predicted by shader's author.
Internally function "disables" all preprocessor directives (so that they're not processed by preprocessor) except `#include` (and also `#version` and `#pragma shader_stage`).
Note that among the directives there may be include guards. Because of that, maxSelfInclusionCount parameter is provided.
@param preprocessOptions
@maxSelfInclusionCount Max self-inclusion count of possible file being #include'd. If no self-inclusions are allowed, should be set to 0.
@sourceIdentifier Path to not necesarilly existing file whose directory will be base for relative (""-type) top-level #include's resolution.
If sourceIdentifier is non-path-like string (e.g. "whatever" - no slashes), the base directory is assumed to be "." (working directory of your executable). It's important for it to be unique.
@returns Shader containing logically same High Level code as input but with #include directives resolved.
*/
virtual std::string preprocessShader(std::string&& code, IShader::E_SHADER_STAGE& stage, const SPreprocessorOptions& preprocessOptions, std::vector<CCache::SEntry::SPreprocessingDependency>* dependencies = nullptr) const = 0;
std::string preprocessShader(system::IFile* sourcefile, IShader::E_SHADER_STAGE stage, const SPreprocessorOptions& preprocessOptions, std::vector<CCache::SEntry::SPreprocessingDependency>* dependencies = nullptr) const;
/*
Creates a formatted copy of the original
@param original An original High Level shader (must contain high level language code and must not be a nullptr).
@param position if original != nullptr, is the position in the code to insert the formatted string to the original shader code
@param fmt A string with c-like format, which will be filled with data from ...args
@param ...args Data to fill fmt with
@returns shader containing fmt filled with ...args, placed before the original code.
If original == nullptr, the output buffer will only contain the data from fmt.
*/
template<typename... Args>
static core::smart_refctd_ptr<IShader> createOverridenCopy(const IShader* original, uint32_t position, const char* fmt, Args... args)
{
if (!original || !original->isContentHighLevelLanguage())
return nullptr;
const auto content = original->getContent();
if (!content || !content->getPointer())
return nullptr;
constexpr auto getMaxSize = [](auto num) -> size_t
{
using in_type_t = decltype(num);
static_assert(std::is_fundamental_v<in_type_t> || std::is_same_v<in_type_t,const char*>);
if constexpr (std::is_floating_point_v<in_type_t>)
{
return std::numeric_limits<decltype(num)>::max_digits10; // there is probably a better way to cope with scientific representation
}
else if constexpr (std::is_integral_v<in_type_t>)
{
return std::to_string(num).length();
}
else
{
return strlen(num);
}
};
constexpr size_t templateArgsCount = sizeof...(Args);
const size_t origLen = original ? original->getContent()->getSize():0u;
const size_t formatArgsCharSize = (getMaxSize(args) + ...);
const size_t formatSize = strlen(fmt);
// 2 is an average size of a format (% and a letter) in chars.
// Assuming the format contains only one letter, but if it's 2, the outSize is gonna be a touch bigger.
constexpr size_t nullTerminatorSize = 1u;
size_t outSize = origLen + formatArgsCharSize + formatSize + nullTerminatorSize - 2 * templateArgsCount;
nbl::core::smart_refctd_ptr<ICPUBuffer> outBuffer = ICPUBuffer::create({ outSize });
auto origCode = std::string_view(reinterpret_cast<const char*>(original->getContent()->getPointer()), origLen);
auto outCode = reinterpret_cast<char*>(outBuffer->getPointer());
if (position < origLen)
{
// Copy whatever comes before position
std::copy_n(origCode.data(), position, outCode);
outCode += position;
// Copy formatted string
outCode += sprintf(outCode,fmt,std::forward<Args>(args)...);
// Copy the rest of the original code
auto epilogueLen = origLen - position;
std::copy_n(origCode.data() + position, epilogueLen, outCode);
outCode += epilogueLen;
// terminating char
*outCode = 0;
outBuffer->setContentHash(outBuffer->computeContentHash());
return nbl::core::make_smart_refctd_ptr<IShader>(std::move(outBuffer), original->getContentType(), std::string(original->getFilepathHint()));
}
else
{
// Position isn't valid.
assert(false);
return nullptr;
}
}
virtual IShader::E_CONTENT_TYPE getCodeContentType() const = 0;
CIncludeFinder* getDefaultIncludeFinder() { return m_defaultIncludeFinder.get(); }
const CIncludeFinder* getDefaultIncludeFinder() const { return m_defaultIncludeFinder.get(); }
protected:
virtual void insertIntoStart(std::string& code, std::ostringstream&& ins) const = 0;
virtual core::smart_refctd_ptr<IShader> compileToSPIRV_impl(const std::string_view code, const SCompilerOptions& options, std::vector<CCache::SEntry::SPreprocessingDependency>* dependencies) const = 0;
core::smart_refctd_ptr<system::ISystem> m_system;
private:
core::smart_refctd_ptr<CIncludeFinder> m_defaultIncludeFinder;
};
NBL_ENUM_ADD_BITWISE_OPERATORS(IShaderCompiler::E_DEBUG_INFO_FLAGS)
}
#endif