-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathISPIRVEntryPointTrimmer.h
More file actions
85 lines (67 loc) · 2.75 KB
/
ISPIRVEntryPointTrimmer.h
File metadata and controls
85 lines (67 loc) · 2.75 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
#ifndef _NBL_ASSET_I_SPIRV_ENTRY_POINT_TRIMMER_H_INCLUDED_
#define _NBL_ASSET_I_SPIRV_ENTRY_POINT_TRIMMER_H_INCLUDED_
#include "nbl/core/declarations.h"
#include "nbl/asset/ICPUBuffer.h"
#include "nbl/system/ILogger.h"
#include <mutex>
namespace nbl::asset
{
class NBL_API2 ISPIRVEntryPointTrimmer final : public core::IReferenceCounted
{
public:
ISPIRVEntryPointTrimmer();
struct Result
{
core::smart_refctd_ptr<ICPUBuffer> spirv; // nullptr if there is some entry point not found or spirv does not need to be trimmed
bool isSuccess;
inline operator bool() const
{
return isSuccess;
}
};
struct EntryPoint
{
std::string_view name;
hlsl::ShaderStage stage;
inline bool operator==(const EntryPoint& rhs) const
{
if (stage != rhs.stage) return false;
return name == rhs.name;
}
inline auto operator<=>(const EntryPoint& other) const
{
if (auto cmp = stage <=> other.stage; cmp != 0)
return cmp;
return name <=> other.name;
}
};
Result trim(const ICPUBuffer* spirvBuffer, const core::set<EntryPoint>& entryPoints, system::logger_opt_ptr logger = nullptr) const;
bool ensureValidated(const ICPUBuffer* spirvBuffer, system::logger_opt_ptr logger = nullptr) const;
void markValidated(const ICPUBuffer* spirvBuffer) const;
inline core::smart_refctd_ptr<const IShader> trim(const IShader* shader, const core::set<EntryPoint>& entryPoints, system::logger_opt_ptr logger = nullptr) const
{
if (shader->getContentType() != IShader::E_CONTENT_TYPE::ECT_SPIRV)
{
logger.log("shader content must be spirv!", system::ILogger::ELL_ERROR);
return nullptr;
}
const auto buffer = shader->getContent();
const auto result = trim(buffer, entryPoints, logger);
if (result && result.spirv.get() == nullptr)
{
// when trim does not happen return original shader
return core::smart_refctd_ptr<const IShader>(shader);
}
if (result.spirv.get() == nullptr)
{
return nullptr;
}
return core::make_smart_refctd_ptr<IShader>(core::smart_refctd_ptr(result.spirv), shader->getContentType(), std::string(shader->getFilepathHint()));
}
private:
core::smart_refctd_ptr<ISPIRVOptimizer> m_optimizer;
mutable std::mutex m_validationCacheMutex;
mutable core::unordered_set<core::blake3_hash_t> m_validatedSpirvHashes;
};
}
#endif