Skip to content

Commit f85ae80

Browse files
committed
Enabled build time shader compilation in multiple examples
1 parent e301db5 commit f85ae80

File tree

18 files changed

+334
-100
lines changed

18 files changed

+334
-100
lines changed

03_DeviceSelectionAndSharedSources/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ class DeviceSelectionAndSharedSourcesApp final : public application_templates::M
257257
}
258258

259259
const auto* metadata = assetBundle.getMetadata();
260-
const auto hlslMetadata = static_cast<const CHLSLMetadata*>(metadata);
260+
const auto hlslMetadata = static_cast<const CHLSLMetadata*>(metadata);
261261
const auto shaderStage = hlslMetadata->shaderStages->front();
262262

263263
// It would be super weird if loading a shader from a file produced more than 1 asset

05_StreamingAndBufferDeviceAddressApp/main.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ class StreamingAndBufferDeviceAddressApp final : public application_templates::M
104104
if (assets.empty())
105105
return logFail("Could not load shader!");
106106

107-
// lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
108-
const auto shaderSource = IAsset::castDown<IShader>(assets[0]);
109-
shader = m_device->compileShader({shaderSource.get()});
107+
shader = IAsset::castDown<IShader>(assets[0]);
110108
// The down-cast should not fail!
111109
assert(shader);
112110
}

07_StagingAndMultipleQueues/CMakeLists.txt

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,50 @@ if(NBL_EMBED_BUILTIN_RESOURCES)
2121
ADD_CUSTOM_BUILTIN_RESOURCES(${_BR_TARGET_} RESOURCES_TO_EMBED "${_SEARCH_DIRECTORIES_}" "${RESOURCE_DIR}" "nbl::this_example::builtin" "${_OUTPUT_DIRECTORY_HEADER_}" "${_OUTPUT_DIRECTORY_SOURCE_}")
2222

2323
LINK_BUILTIN_RESOURCES_TO_TARGET(${EXECUTABLE_NAME} ${_BR_TARGET_})
24-
endif()
24+
endif()
25+
26+
set(OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/auto-gen")
27+
set(DEPENDS
28+
app_resources/common.hlsl
29+
app_resources/comp_shader.hlsl
30+
)
31+
target_sources(${EXECUTABLE_NAME} PRIVATE ${DEPENDS})
32+
set_source_files_properties(${DEPENDS} PROPERTIES HEADER_FILE_ONLY ON)
33+
34+
set(JSON [=[
35+
[
36+
{
37+
"INPUT": "app_resources/comp_shader.hlsl",
38+
"KEY": "comp_shader",
39+
}
40+
]
41+
]=])
42+
string(CONFIGURE "${JSON}" JSON)
43+
44+
set(COMPILE_OPTIONS
45+
-I "${CMAKE_CURRENT_SOURCE_DIR}"
46+
-O3
47+
-T lib_${SM}
48+
)
49+
50+
NBL_CREATE_NSC_COMPILE_RULES(
51+
TARGET ${EXECUTABLE_NAME}SPIRV
52+
LINK_TO ${EXECUTABLE_NAME}
53+
DEPENDS ${DEPENDS}
54+
BINARY_DIR ${OUTPUT_DIRECTORY}
55+
MOUNT_POINT_DEFINE NBL_THIS_EXAMPLE_BUILD_MOUNT_POINT
56+
COMMON_OPTIONS -I ${CMAKE_CURRENT_SOURCE_DIR}
57+
OUTPUT_VAR KEYS
58+
INCLUDE nbl/this_example/builtin/build/spirv/keys.hpp
59+
NAMESPACE nbl::this_example::builtin::build
60+
INPUTS ${JSON}
61+
)
62+
63+
NBL_CREATE_RESOURCE_ARCHIVE(
64+
NAMESPACE nbl::this_example::builtin::build
65+
TARGET ${EXECUTABLE_NAME}_builtinsBuild
66+
LINK_TO ${EXECUTABLE_NAME}
67+
BIND ${OUTPUT_DIRECTORY}
68+
BUILTINS ${KEYS}
69+
COMMON_OPTIONS ${COMPILE_OPTIONS}
70+
)

07_StagingAndMultipleQueues/main.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// I've moved out a tiny part of this example into a shared header for reuse, please open and read it.
66
#include "nbl/examples/examples.hpp"
7+
#include "nbl/this_example/builtin/build/spirv/keys.hpp"
78

89
using namespace nbl;
910
using namespace nbl::core;
@@ -189,7 +190,7 @@ class StagingAndMultipleQueuesApp final : public application_templates::BasicMul
189190
for (uint32_t imageIdx = 0; imageIdx < IMAGE_CNT; ++imageIdx)
190191
{
191192
const auto imagePathToLoad = imagesToLoad[imageIdx];
192-
auto cpuImage = loadFistAssetInBundle<ICPUImage>(imagePathToLoad);
193+
auto cpuImage = loadImageAsset(imagePathToLoad);
193194
if (!cpuImage)
194195
logFailAndTerminate("Failed to load image from path %s",ILogger::ELL_ERROR,imagePathToLoad);
195196

@@ -279,17 +280,10 @@ class StagingAndMultipleQueuesApp final : public application_templates::BasicMul
279280
}
280281

281282
// LOAD SHADER FROM FILE
282-
smart_refctd_ptr<IShader> source;
283-
{
284-
source = loadFistAssetInBundle<IShader>("../app_resources/comp_shader.hlsl");
285-
}
283+
smart_refctd_ptr<IShader> shader = loadPreCompiledShader<"comp_shader">("../app_resources/comp_shader.hlsl");
286284

287-
if (!source)
288-
logFailAndTerminate("Could not create a CPU shader!");
289-
290-
core::smart_refctd_ptr<IShader> shader = m_device->compileShader({ source.get() });
291-
if(!shader)
292-
logFailAndTerminate("Could not compile shader to spirv!");
285+
if (!shader)
286+
logFailAndTerminate("Could not load the precompiled shader!");
293287

294288
// CREATE COMPUTE PIPELINE
295289
SPushConstantRange pc[1];
@@ -534,21 +528,39 @@ class StagingAndMultipleQueuesApp final : public application_templates::BasicMul
534528

535529
return false;
536530
}
537-
538-
template<typename AssetType>
539-
core::smart_refctd_ptr<AssetType> loadFistAssetInBundle(const std::string& path)
531+
532+
core::smart_refctd_ptr<ICPUImage> loadImageAsset(const std::string& path)
540533
{
541534
IAssetLoader::SAssetLoadParams lp;
542535
SAssetBundle bundle = m_assetMgr->getAsset(path, lp);
543536
if (bundle.getContents().empty())
544-
logFailAndTerminate("Couldn't load an asset.",ILogger::ELL_ERROR);
537+
logFailAndTerminate("Couldn't load an image.",ILogger::ELL_ERROR);
545538

546-
auto asset = IAsset::castDown<AssetType>(bundle.getContents()[0]);
539+
auto asset = IAsset::castDown<ICPUImage>(bundle.getContents()[0]);
547540
if (!asset)
548541
logFailAndTerminate("Incorrect asset loaded.",ILogger::ELL_ERROR);
549542

550543
return asset;
551544
}
545+
546+
template<core::StringLiteral ShaderKey>
547+
core::smart_refctd_ptr<IShader> loadPreCompiledShader(const std::string& path)
548+
{
549+
IAssetLoader::SAssetLoadParams lp;
550+
lp.logger = m_logger.get();
551+
lp.workingDirectory = "app_resources";
552+
553+
auto key = nbl::this_example::builtin::build::get_spirv_key<ShaderKey>(m_device.get());
554+
SAssetBundle bundle = m_assetMgr->getAsset(key.data(), lp);
555+
if (bundle.getContents().empty())
556+
logFailAndTerminate("Couldn't load a shader.", ILogger::ELL_ERROR);
557+
558+
auto asset = IAsset::castDown<IShader>(bundle.getContents()[0]);
559+
if (!asset)
560+
logFailAndTerminate("Incorrect asset loaded.", ILogger::ELL_ERROR);
561+
562+
return asset;
563+
}
552564
};
553565

554566
NBL_MAIN_FUNC(StagingAndMultipleQueuesApp)

11_FFT/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
44

5-
65
#include "nbl/examples/examples.hpp"
76

87
using namespace nbl;

24_ColorSpaceTest/CMakeLists.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,50 @@ add_test(NAME NBL_IMAGE_HASH_RUN_TESTS
3232
COMMAND "$<TARGET_FILE:${EXECUTABLE_NAME}>" --test hash
3333
WORKING_DIRECTORY "$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>"
3434
COMMAND_EXPAND_LISTS
35+
)
36+
37+
set(OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/auto-gen")
38+
set(DEPENDS
39+
app_resources/present.frag.hlsl
40+
app_resources/push_constants.hlsl
41+
)
42+
target_sources(${EXECUTABLE_NAME} PRIVATE ${DEPENDS})
43+
set_source_files_properties(${DEPENDS} PROPERTIES HEADER_FILE_ONLY ON)
44+
45+
set(JSON [=[
46+
[
47+
{
48+
"INPUT": "app_resources/present.frag.hlsl",
49+
"KEY": "present",
50+
}
51+
]
52+
]=])
53+
string(CONFIGURE "${JSON}" JSON)
54+
55+
set(COMPILE_OPTIONS
56+
-I "${CMAKE_CURRENT_SOURCE_DIR}"
57+
-O3
58+
-T lib_${SM}
59+
)
60+
61+
NBL_CREATE_NSC_COMPILE_RULES(
62+
TARGET ${EXECUTABLE_NAME}SPIRV
63+
LINK_TO ${EXECUTABLE_NAME}
64+
DEPENDS ${DEPENDS}
65+
BINARY_DIR ${OUTPUT_DIRECTORY}
66+
MOUNT_POINT_DEFINE NBL_THIS_EXAMPLE_BUILD_MOUNT_POINT
67+
COMMON_OPTIONS -I ${CMAKE_CURRENT_SOURCE_DIR}
68+
OUTPUT_VAR KEYS
69+
INCLUDE nbl/this_example/builtin/build/spirv/keys.hpp
70+
NAMESPACE nbl::this_example::builtin::build
71+
INPUTS ${JSON}
72+
)
73+
74+
NBL_CREATE_RESOURCE_ARCHIVE(
75+
NAMESPACE nbl::this_example::builtin::build
76+
TARGET ${EXECUTABLE_NAME}_builtinsBuild
77+
LINK_TO ${EXECUTABLE_NAME}
78+
BIND ${OUTPUT_DIRECTORY}
79+
BUILTINS ${KEYS}
80+
COMMON_OPTIONS ${COMPILE_OPTIONS}
3581
)

24_ColorSpaceTest/main.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (C) 2018-2024 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#include "nbl/this_example/builtin/build/spirv/keys.hpp"
45
#include "nbl/examples/examples.hpp"
56

67
#include "nbl/ext/FullScreenTriangle/FullScreenTriangle.h"
@@ -160,26 +161,24 @@ class ColorSpaceTestSampleApp final : public SimpleWindowedApplication, public B
160161
return logFail("Failed to create Full Screen Triangle protopipeline or load its vertex shader!");
161162

162163
// Load Custom Shader
163-
auto loadCompileAndCreateShader = [&](const std::string& relPath) -> smart_refctd_ptr<IShader>
164+
auto loadPrecompiledShader = [&]<core::StringLiteral ShaderKey>(const std::string& relPath) -> smart_refctd_ptr<IShader>
164165
{
165166
IAssetLoader::SAssetLoadParams lp = {};
166167
lp.logger = m_logger.get();
167-
lp.workingDirectory = ""; // virtual root
168-
auto assetBundle = m_assetMgr->getAsset(relPath, lp);
168+
lp.workingDirectory = "app_resources";
169+
170+
auto key = nbl::this_example::builtin::build::get_spirv_key<ShaderKey>(m_device.get());
171+
auto assetBundle = m_assetMgr->getAsset(key.data(), lp);
169172
const auto assets = assetBundle.getContents();
170173
if (assets.empty())
171174
return nullptr;
172175

173-
// lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
174-
auto source = IAsset::castDown<IShader>(assets[0]);
175-
if (!source)
176-
return nullptr;
177-
178-
return m_device->compileShader({ source.get() });
176+
auto shader = IAsset::castDown<IShader>(assets[0]);
177+
return shader;
179178
};
180-
auto fragmentShader = loadCompileAndCreateShader("app_resources/present.frag.hlsl");
179+
auto fragmentShader = loadPrecompiledShader.operator()<"present">("app_resources/present.frag.hlsl");
181180
if (!fragmentShader)
182-
return logFail("Failed to Load and Compile Fragment Shader!");
181+
return logFail("Failed to load precompiled fragment shader!");
183182

184183
// Now surface indep resources
185184
m_semaphore = m_device->createSemaphore(m_submitIx);

62_CAD/CMakeLists.txt

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,70 @@ else()
6161
foreach(NBL_TARGET IN LISTS NBL_MSDFGEN_TARGETS)
6262
target_include_directories(${EXECUTABLE_NAME} PUBLIC $<TARGET_PROPERTY:${NBL_TARGET},INCLUDE_DIRECTORIES>)
6363
endforeach()
64-
endif()
64+
endif()
65+
66+
set(OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/auto-gen")
67+
set(DEPENDS
68+
shaders/globals.hlsl
69+
shaders/runtimeDeviceConfigCaps.hlsl
70+
shaders/main_pipeline/common.hlsl
71+
shaders/main_pipeline/dtm.hlsl
72+
shaders/main_pipeline/fragment.hlsl
73+
shaders/main_pipeline/fragment_shader.hlsl
74+
shaders/main_pipeline/fragment_shader_debug.hlsl
75+
shaders/main_pipeline/line_style.hlsl
76+
shaders/main_pipeline/resolve_alphas.hlsl
77+
shaders/main_pipeline/vertex_shader.hlsl
78+
)
79+
80+
set(SM 6_8)
81+
82+
set(REQUIRED_CAPS [=[
83+
{
84+
"kind": "features",
85+
"name": "fragmentShaderPixelInterlock",
86+
"type": "bool",
87+
"values": [1]
88+
}
89+
]=])
90+
91+
set(JSON [=[
92+
[
93+
{
94+
"INPUT": "shaders/main_pipeline/vertex_shader.hlsl",
95+
"KEY": "main_pipeline_vertex_shader",
96+
"COMPILE_OPTIONS": ["-T", "cs_6_8"],
97+
"DEPENDS": [],
98+
"CAPS": [${REQUIRED_CAPS}]
99+
},
100+
{
101+
"INPUT": "shaders/main_pipeline/fragment.hlsl",
102+
"KEY": "main_pipeline_fragment_shader",
103+
"COMPILE_OPTIONS": ["-T", "cs_6_8"],
104+
"DEPENDS": [],
105+
"CAPS": [${REQUIRED_CAPS}]
106+
}
107+
]
108+
]=])
109+
string(CONFIGURE "${JSON}" JSON)
110+
111+
NBL_CREATE_NSC_COMPILE_RULES(
112+
TARGET ${EXECUTABLE_NAME}SPIRV
113+
LINK_TO ${EXECUTABLE_NAME}
114+
DEPENDS ${DEPENDS}
115+
BINARY_DIR ${OUTPUT_DIRECTORY}
116+
MOUNT_POINT_DEFINE NBL_THIS_EXAMPLE_BUILD_MOUNT_POINT
117+
COMMON_OPTIONS -I ${CMAKE_CURRENT_SOURCE_DIR}
118+
OUTPUT_VAR KEYS
119+
INCLUDE nbl/this_example/builtin/build/spirv/keys.hpp
120+
NAMESPACE nbl::this_example::builtin::build
121+
INPUTS ${JSON}
122+
)
123+
124+
NBL_CREATE_RESOURCE_ARCHIVE(
125+
NAMESPACE nbl::this_example::builtin::build
126+
TARGET ${EXECUTABLE_NAME}_builtinsBuild
127+
LINK_TO ${EXECUTABLE_NAME}
128+
BIND ${OUTPUT_DIRECTORY}
129+
BUILTINS ${KEYS}
130+
)

62_CAD/main.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// TODO: Copyright notice
2-
2+
#include "nbl/this_example/builtin/build/spirv/keys.hpp"
33

44
#include "nbl/examples/examples.hpp"
55

@@ -961,12 +961,14 @@ class ComputerAidedDesign final : public nbl::examples::SimpleWindowedApplicatio
961961
}
962962

963963
// Load Custom Shader
964-
auto loadCompileShader = [&](const std::string& relPath) -> smart_refctd_ptr<IShader>
964+
auto loadCompileShader = [&]<core::StringLiteral ShaderKey>(const std::string& relPath) -> smart_refctd_ptr<IShader>
965965
{
966966
IAssetLoader::SAssetLoadParams lp = {};
967967
lp.logger = m_logger.get();
968-
lp.workingDirectory = ""; // virtual root
969-
auto assetBundle = m_assetMgr->getAsset(relPath, lp);
968+
lp.workingDirectory = "shaders";
969+
970+
auto key = nbl::this_example::builtin::build::get_spirv_key<ShaderKey>(m_device.get());
971+
auto assetBundle = m_assetMgr->getAsset(key.data(), lp);
970972
const auto assets = assetBundle.getContents();
971973
if (assets.empty())
972974
return nullptr;
@@ -979,8 +981,8 @@ class ComputerAidedDesign final : public nbl::examples::SimpleWindowedApplicatio
979981
return m_device->compileShader( ILogicalDevice::SShaderCreationParameters { .source = source.get(), .readCache = shaderReadCache.get(), .writeCache = shaderWriteCache.get(), .stage = IShader::E_SHADER_STAGE::ESS_ALL_OR_LIBRARY });
980982
};
981983

982-
mainPipelineFragmentShaders = loadCompileShader("../shaders/main_pipeline/fragment.hlsl");
983-
mainPipelineVertexShader = loadCompileShader("../shaders/main_pipeline/vertex_shader.hlsl");
984+
mainPipelineFragmentShaders = loadCompileShader.operator()<"main_pipeline_fragment_shader">("../shaders/main_pipeline/fragment.hlsl");
985+
mainPipelineVertexShader = loadCompileShader.operator() <"main_pipeline_vertex_shader"> ("../shaders/main_pipeline/vertex_shader.hlsl");
984986

985987
core::smart_refctd_ptr<system::IFile> shaderWriteCacheFile;
986988
{

62_CAD/shaders/globals.hlsl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
#ifndef _CAD_EXAMPLE_GLOBALS_HLSL_INCLUDED_
22
#define _CAD_EXAMPLE_GLOBALS_HLSL_INCLUDED_
33

4-
#ifdef __HLSL_VERSION
5-
#ifndef NBL_USE_SPIRV_BUILTINS
6-
#include "runtimeDeviceConfigCaps.hlsl" // defines DeviceConfigCaps, uses JIT device caps
7-
#endif
8-
#endif
9-
104
// TODO[Erfan]: Turn off in the future, but keep enabled to test
115
// #define NBL_FORCE_EMULATED_FLOAT_64
126

0 commit comments

Comments
 (0)