Skip to content

Commit df5b1e3

Browse files
committed
Include CHLSLLoader into assetmanager
1 parent cd87254 commit df5b1e3

File tree

7 files changed

+116
-19
lines changed

7 files changed

+116
-19
lines changed

include/nbl/asset/utils/CGLSLCompiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class NBL_API2 CGLSLCompiler final : public IShaderCompiler
127127

128128
protected:
129129

130-
virtual void insertIntoStart(std::string& code, std::ostringstream&& ins) const override;
130+
void insertIntoStart(std::string& code, std::ostringstream&& ins) const override;
131131

132132
static CGLSLCompiler::SOptions option_cast(const IShaderCompiler::SCompilerOptions& options)
133133
{

include/nbl/asset/utils/CHLSLCompiler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
4444

4545
protected:
4646

47-
virtual void insertIntoStart(std::string& code, std::ostringstream&& ins) const override;
47+
void insertIntoStart(std::string& code, std::ostringstream&& ins) const override;
4848

4949
// TODO do we want to use ComPtr?
5050
std::unique_ptr<IDxcUtils> m_dxcUtils;
@@ -73,7 +73,7 @@ class NBL_API2 CHLSLCompiler final : public IShaderCompiler
7373
}
7474
};
7575

76-
DxcCompilationResult dxcCompile(const std::string& source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const;
76+
DxcCompilationResult dxcCompile(std::string& source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const;
7777
};
7878

7979
}

src/nbl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ set(NBL_ASSET_SOURCES
237237
${NBL_ROOT_PATH}/src/nbl/asset/utils/CCompilerSet.cpp
238238
${NBL_ROOT_PATH}/src/nbl/asset/utils/CSPIRVIntrospector.cpp
239239
${NBL_ROOT_PATH}/src/nbl/asset/interchange/CGLSLLoader.cpp
240+
${NBL_ROOT_PATH}/src/nbl/asset/interchange/CHLSLLoader.cpp
240241
${NBL_ROOT_PATH}/src/nbl/asset/interchange/CSPVLoader.cpp
241242

242243
# Pipeline loaders

src/nbl/asset/IAssetManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "nbl/asset/asset.h"
66

77
#include "nbl/asset/interchange/CGLSLLoader.h"
8+
#include "nbl/asset/interchange/CHLSLLoader.h"
89
#include "nbl/asset/interchange/CSPVLoader.h"
910

1011
#ifdef _NBL_COMPILE_WITH_MTL_LOADER_
@@ -172,6 +173,7 @@ void IAssetManager::addLoadersAndWriters()
172173
#endif
173174
addAssetLoader(core::make_smart_refctd_ptr<asset::CBufferLoaderBIN>());
174175
addAssetLoader(core::make_smart_refctd_ptr<asset::CGLSLLoader>());
176+
addAssetLoader(core::make_smart_refctd_ptr<asset::CHLSLLoader>());
175177
addAssetLoader(core::make_smart_refctd_ptr<asset::CSPVLoader>());
176178

177179
#ifdef _NBL_COMPILE_WITH_BAW_WRITER_
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
#include "nbl/asset/asset.h"
6+
#include "CHLSLLoader.h"
7+
8+
using namespace nbl;
9+
using namespace nbl::asset;
10+
11+
// load in the image data
12+
SAssetBundle CHLSLLoader::loadAsset(system::IFile* _file, const IAssetLoader::SAssetLoadParams& _params, IAssetLoader::IAssetLoaderOverride* _override, uint32_t _hierarchyLevel)
13+
{
14+
if (!_file)
15+
return {};
16+
17+
auto len = _file->getSize();
18+
void* source = _NBL_ALIGNED_MALLOC(len+1u,_NBL_SIMD_ALIGNMENT);
19+
20+
system::IFile::success_t success;
21+
_file->read(success, source, 0, len);
22+
if (!success)
23+
return {};
24+
25+
reinterpret_cast<char*>(source)[len] = 0;
26+
27+
28+
const auto filename = _file->getFileName();
29+
//! TODO: Actually invoke the GLSL compiler to decode our type from any `#pragma`s
30+
std::filesystem::path extension = filename.extension();
31+
32+
33+
core::unordered_map<std::string,IShader::E_SHADER_STAGE> typeFromExt = {
34+
{".vert",IShader::ESS_VERTEX},
35+
{".tesc",IShader::ESS_TESSELLATION_CONTROL},
36+
{".tese",IShader::ESS_TESSELLATION_EVALUATION},
37+
{".geom",IShader::ESS_GEOMETRY},
38+
{".frag",IShader::ESS_FRAGMENT},
39+
{".comp",IShader::ESS_COMPUTE}
40+
};
41+
auto found = typeFromExt.find(extension.string());
42+
if (found == typeFromExt.end())
43+
{
44+
_NBL_ALIGNED_FREE(source);
45+
return {};
46+
}
47+
48+
auto shader = core::make_smart_refctd_ptr<ICPUShader>(reinterpret_cast<char*>(source), found->second, IShader::E_CONTENT_TYPE::ECT_GLSL, filename.string());
49+
_NBL_ALIGNED_FREE(source);
50+
51+
return SAssetBundle(nullptr,{ core::make_smart_refctd_ptr<ICPUSpecializedShader>(std::move(shader),ISpecializedShader::SInfo({},nullptr,"main")) });
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
#ifndef _NBL_ASSET_C_HLSL_LOADER_H_INCLUDED_
6+
#define _NBL_ASSET_C_HLSL_LOADER_H_INCLUDED_
7+
8+
#include <algorithm>
9+
10+
#include "nbl/asset/interchange/IAssetLoader.h"
11+
#include <nbl/system/ISystem.h>
12+
13+
namespace nbl::asset
14+
{
15+
16+
//! Surface Loader for PNG files
17+
class CHLSLLoader final : public asset::IAssetLoader
18+
{
19+
public:
20+
CHLSLLoader() = default;
21+
22+
bool isALoadableFileFormat(system::IFile* _file, const system::logger_opt_ptr logger = nullptr) const override
23+
{
24+
return true;
25+
}
26+
27+
const char** getAssociatedFileExtensions() const override
28+
{
29+
static const char* ext[]{ "hlsl", nullptr };
30+
return ext;
31+
}
32+
33+
uint64_t getSupportedAssetTypesBitfield() const override { return asset::IAsset::ET_SPECIALIZED_SHADER; }
34+
35+
asset::SAssetBundle loadAsset(system::IFile* _file, const asset::IAssetLoader::SAssetLoadParams& _params, asset::IAssetLoader::IAssetLoaderOverride* _override = nullptr, uint32_t _hierarchyLevel = 0u) override;
36+
};
37+
38+
} // namespace nbl::asset
39+
40+
#endif
41+

src/nbl/asset/utils/CHLSLCompiler.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,24 @@ CHLSLCompiler::~CHLSLCompiler()
3636
m_dxcCompiler->Release();
3737
}
3838

39-
CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(const std::string& source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const
39+
CHLSLCompiler::DxcCompilationResult CHLSLCompiler::dxcCompile(std::string& source, LPCWSTR* args, uint32_t argCount, const SOptions& options) const
4040
{
41+
if (options.genDebugInfo)
42+
{
43+
std::ostringstream insertion;
44+
insertion << "// commandline compiler options : ";
45+
46+
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> conv;
47+
for (uint32_t arg = 0; arg < argCount; arg ++)
48+
{
49+
auto str = conv.to_bytes(args[arg]);
50+
insertion << str.c_str() << " ";
51+
}
52+
53+
insertion << "\n";
54+
insertIntoStart(source, std::move(insertion));
55+
}
56+
4157
DxcBuffer sourceBuffer;
4258
sourceBuffer.Ptr = source.data();
4359
sourceBuffer.Size = source.size();
@@ -96,21 +112,6 @@ core::smart_refctd_ptr<ICPUShader> CHLSLCompiler::compileToSPIRV(const char* cod
96112
// These are debug only
97113
L"-Qembed_debug"
98114
};
99-
if (hlslOptions.genDebugInfo)
100-
{
101-
std::ostringstream insertion;
102-
insertion << "// commandline compiler options : ";
103-
104-
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> conv;
105-
for (auto arg : arguments)
106-
{
107-
auto str = conv.to_bytes(arg);
108-
insertion << str.c_str() << " ";
109-
}
110-
111-
insertion << "\n";
112-
insertIntoStart(newCode, std::move(insertion));
113-
}
114115

115116
const uint32_t nonDebugArgs = 2;
116117
const uint32_t allArgs = nonDebugArgs + 0;

0 commit comments

Comments
 (0)