Skip to content

Commit 84c1cd6

Browse files
committed
cjit include loader
1 parent fcb4fff commit 84c1cd6

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

include/nbl/asset/utils/CHLSLCompiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
5555
// when Nabla is used as a lib
5656
nbl::asset::hlsl::impl::DXC* m_dxcCompilerTypes;
5757

58+
IShaderCompiler::CIncludeFinder* m_defaultIncludeFinder;
59+
5860
static CHLSLCompiler::SOptions option_cast(const IShaderCompiler::SCompilerOptions& options)
5961
{
6062
CHLSLCompiler::SOptions ret = {};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef _NBL_ASSET_C_JIT_INCLUDE_LOADER_H_INCLUDED_
2+
#define _NBL_ASSET_C_JIT_INCLUDE_LOADER_H_INCLUDED_
3+
4+
#include <string>
5+
#include <unordered_map>
6+
7+
class CJITIncludeLoader
8+
{
9+
public:
10+
CJITIncludeLoader();
11+
std::string loadInclude(const std::string path);
12+
13+
private:
14+
std::unordered_map<std::string, std::string> m_includes;
15+
std::string collectDeviceCaps();
16+
};
17+
18+
#endif // CJITINCLUDELOADER_H

include/nbl/asset/utils/IShaderCompiler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "nbl/asset/ICPUSpecializedShader.h"
1515
#include "nbl/asset/utils/ISPIRVOptimizer.h"
16+
#include "nbl/asset/utils/CJITIncludeLoader.h"
1617

1718
namespace nbl::asset
1819
{
@@ -293,6 +294,8 @@ class NBL_API2 IShaderCompiler : public core::IReferenceCounted
293294
core::smart_refctd_ptr<system::ISystem> m_system;
294295
private:
295296
core::smart_refctd_ptr<CIncludeFinder> m_defaultIncludeFinder;
297+
298+
CJITIncludeLoader m_JITIncludeLoader;
296299
};
297300

298301
NBL_ENUM_ADD_BITWISE_OPERATORS(IShaderCompiler::E_DEBUG_INFO_FLAGS)

include/nbl/builtin/hlsl/jit/device_capabilities.hlsl

Whitespace-only changes.

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace nbl::asset::hlsl::impl
4646
CHLSLCompiler::CHLSLCompiler(core::smart_refctd_ptr<system::ISystem>&& system)
4747
: IShaderCompiler(std::move(system))
4848
{
49+
m_defaultIncludeFinder->addSearchPath("", core::make_smart_refctd_ptr<CJITIncludeLoader>());
50+
4951
ComPtr<IDxcUtils> utils;
5052
auto res = DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(utils.GetAddressOf()));
5153
assert(SUCCEEDED(res));
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
#include "CJITIncludeLoader.h"
3+
4+
5+
CJITIncludeLoader::CJITIncludeLoader()
6+
{
7+
m_includes["device_capabilities.hlsl"] = collectDeviceCaps();
8+
}
9+
10+
std::string CJITIncludeLoader::loadInclude(const std::string path)
11+
{
12+
const std::string prefix = "nbl/builtin/hlsl/jit/";
13+
if (path.compare(0, prefix.size(), prefix) == 0)
14+
{
15+
std::string includeFileName = path.substr(prefix.size());
16+
17+
// Look up the content in m_includes map
18+
auto it = m_includes.find(includeFileName);
19+
if (it != m_includes.end())
20+
{
21+
// Return the content of the specified include file
22+
return it->second;
23+
}
24+
else
25+
{
26+
// Handle error: include file not found
27+
std::cerr << "Error: Include file '" << path << "' not found!" << std::endl;
28+
return "";
29+
}
30+
}
31+
else
32+
{
33+
// Handle error: invalid include path
34+
std::cerr << "Error: Invalid include path '" << path << "'!" << std::endl;
35+
return "";
36+
}
37+
}
38+
39+
40+
std::string CJITIncludeLoader::collectDeviceCaps()
41+
{
42+
std::ostringstream content;
43+
content << "#ifndef _NBL_BUILTIN_HLSL_JIT_DEVICE_CAPABILITIES_INCLUDED_" << std::endl;
44+
content << "#define _NBL_BUILTIN_HLSL_JIT_DEVICE_CAPABILITIES_INCLUDED_" << std::endl;
45+
content << "namespace nbl {" << std::endl;
46+
content << "namespace hlsl {" << std::endl;
47+
content << "namespace jit {" << std::endl;
48+
content << "struct device_capabilities {" << std::endl;
49+
content << " NBL_CONSTEXPR_STATIC_INLINE bool shaderFloat64 = false" << std::endl;
50+
content << " NBL_CONSTEXPR_STATIC_INLINE bool shaderDrawParameters = false" << std::endl;
51+
content << " NBL_CONSTEXPR_STATIC_INLINE bool subgroupArithmetic = false" << std::endl;
52+
content << " NBL_CONSTEXPR_STATIC_INLINE bool fragmentShaderPixelInterlock = false" << std::endl;
53+
content << std::endl;
54+
content << " NBL_CONSTEXPR_STATIC_INLINE uint16_t maxOptimallyResidentWorkgroupInvocations = 0" << std::endl;
55+
content << "};" << std::endl;
56+
content << "}" << std::endl;
57+
content << "}" << std::endl;
58+
content << "#endif" << std::endl;
59+
60+
return content.str();
61+
}

0 commit comments

Comments
 (0)