Skip to content

Commit eb1e29f

Browse files
committed
Enabled build time shader compilation in example 71
1 parent 974d23f commit eb1e29f

File tree

7 files changed

+138
-146
lines changed

7 files changed

+138
-146
lines changed

62_CAD/main.cpp

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -929,43 +929,12 @@ class ComputerAidedDesign final : public nbl::examples::SimpleWindowedApplicatio
929929
smart_refctd_ptr<IShader> mainPipelineVertexShader = {};
930930
std::array<smart_refctd_ptr<IShader>, 2u> geoTexturePipelineShaders = {};
931931
{
932-
smart_refctd_ptr<IShaderCompiler::CCache> shaderReadCache = nullptr;
933-
smart_refctd_ptr<IShaderCompiler::CCache> shaderWriteCache = core::make_smart_refctd_ptr<IShaderCompiler::CCache>();
934-
auto shaderCachePath = localOutputCWD / "main_pipeline_shader_cache.bin";
935-
936-
{
937-
core::smart_refctd_ptr<system::IFile> shaderReadCacheFile;
938-
{
939-
system::ISystem::future_t<core::smart_refctd_ptr<system::IFile>> future;
940-
m_system->createFile(future, shaderCachePath.c_str(), system::IFile::ECF_READ);
941-
if (future.wait())
942-
{
943-
future.acquire().move_into(shaderReadCacheFile);
944-
if (shaderReadCacheFile)
945-
{
946-
const size_t size = shaderReadCacheFile->getSize();
947-
if (size > 0ull)
948-
{
949-
std::vector<uint8_t> contents(size);
950-
system::IFile::success_t succ;
951-
shaderReadCacheFile->read(succ, contents.data(), 0, size);
952-
if (succ)
953-
shaderReadCache = IShaderCompiler::CCache::deserialize(contents);
954-
}
955-
}
956-
}
957-
else
958-
m_logger->log("Failed Openning Shader Cache File.", ILogger::ELL_ERROR);
959-
}
960-
961-
}
962-
963932
// Load Custom Shader
964933
auto loadPrecompiledShader = [&]<core::StringLiteral ShaderKey>() -> smart_refctd_ptr<IShader>
965934
{
966935
IAssetLoader::SAssetLoadParams lp = {};
967936
lp.logger = m_logger.get();
968-
lp.workingDirectory = "shaders";
937+
lp.workingDirectory = "app_resources";
969938

970939
auto key = nbl::this_example::builtin::build::get_spirv_key<ShaderKey>(m_device.get());
971940
auto assetBundle = m_assetMgr->getAsset(key.data(), lp);
@@ -983,32 +952,6 @@ class ComputerAidedDesign final : public nbl::examples::SimpleWindowedApplicatio
983952

984953
mainPipelineFragmentShaders = loadPrecompiledShader.operator()<"main_pipeline_fragment_shader">(); // "../shaders/main_pipeline/fragment.hlsl"
985954
mainPipelineVertexShader = loadPrecompiledShader.operator() <"main_pipeline_vertex_shader">(); // "../shaders/main_pipeline/vertex_shader.hlsl"
986-
987-
core::smart_refctd_ptr<system::IFile> shaderWriteCacheFile;
988-
{
989-
system::ISystem::future_t<core::smart_refctd_ptr<system::IFile>> future;
990-
m_system->deleteFile(shaderCachePath); // temp solution instead of trimming, to make sure we won't have corrupted json
991-
m_system->createFile(future, shaderCachePath.c_str(), system::IFile::ECF_WRITE);
992-
if (future.wait())
993-
{
994-
future.acquire().move_into(shaderWriteCacheFile);
995-
if (shaderWriteCacheFile)
996-
{
997-
auto serializedCache = shaderWriteCache->serialize();
998-
if (shaderWriteCacheFile)
999-
{
1000-
system::IFile::success_t succ;
1001-
shaderWriteCacheFile->write(succ, serializedCache->getPointer(), 0, serializedCache->getSize());
1002-
if (!succ)
1003-
m_logger->log("Failed Writing To Shader Cache File.", ILogger::ELL_ERROR);
1004-
}
1005-
}
1006-
else
1007-
m_logger->log("Failed Creating Shader Cache File.", ILogger::ELL_ERROR);
1008-
}
1009-
else
1010-
m_logger->log("Failed Creating Shader Cache File.", ILogger::ELL_ERROR);
1011-
}
1012955
}
1013956

1014957
// Shared Blend Params between pipelines

71_RayTracingPipeline/CMakeLists.txt

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,105 @@ if(NBL_BUILD_IMGUI)
3434
endif()
3535
endif()
3636

37+
set(OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/auto-gen")
38+
set(DEPENDS
39+
app_resources/common.hlsl
40+
app_resources/light_directional.rcall.hlsl
41+
app_resources/light_point.rcall.hlsl
42+
app_resources/light_spot.rcall.hlsl
43+
app_resources/present.frag.hlsl
44+
app_resources/raytrace.rahit.hlsl
45+
app_resources/raytrace.rchit.hlsl
46+
app_resources/raytrace.rgen.hlsl
47+
app_resources/raytrace.rint.hlsl
48+
app_resources/raytrace.rmiss.hlsl
49+
app_resources/raytrace_procedural.rchit.hlsl
50+
app_resources/raytrace_shadow.rahit.hlsl
51+
app_resources/raytrace_shadow.rmiss.hlsl
52+
)
53+
target_sources(${EXECUTABLE_NAME} PRIVATE ${DEPENDS})
54+
set_source_files_properties(${DEPENDS} PROPERTIES HEADER_FILE_ONLY ON)
55+
56+
set(SM 6_8)
57+
set(JSON [=[
58+
[
59+
{
60+
"INPUT": "app_resources/raytrace.rgen.hlsl",
61+
"KEY": "raytrace_rgen",
62+
},
63+
{
64+
"INPUT": "app_resources/raytrace.rchit.hlsl",
65+
"KEY": "raytrace_rchit",
66+
},
67+
{
68+
"INPUT": "app_resources/raytrace_procedural.rchit.hlsl",
69+
"KEY": "raytrace_procedural_rchit",
70+
},
71+
{
72+
"INPUT": "app_resources/raytrace.rint.hlsl",
73+
"KEY": "raytrace_rint",
74+
},
75+
{
76+
"INPUT": "app_resources/raytrace.rahit.hlsl",
77+
"KEY": "raytrace_rahit",
78+
},
79+
{
80+
"INPUT": "app_resources/raytrace_shadow.rahit.hlsl",
81+
"KEY": "raytrace_shadow_rahit",
82+
},
83+
{
84+
"INPUT": "app_resources/raytrace.rmiss.hlsl",
85+
"KEY": "raytrace_rmiss",
86+
},
87+
{
88+
"INPUT": "app_resources/raytrace_shadow.rmiss.hlsl",
89+
"KEY": "raytrace_shadow_rmiss",
90+
},
91+
{
92+
"INPUT": "app_resources/light_directional.rcall.hlsl",
93+
"KEY": "light_directional_rcall",
94+
},
95+
{
96+
"INPUT": "app_resources/light_point.rcall.hlsl",
97+
"KEY": "light_point_rcall",
98+
},
99+
{
100+
"INPUT": "app_resources/light_spot.rcall.hlsl",
101+
"KEY": "light_spot_rcall",
102+
},
103+
{
104+
"INPUT": "app_resources/present.frag.hlsl",
105+
"KEY": "present_frag",
106+
}
107+
]
108+
]=])
109+
string(CONFIGURE "${JSON}" JSON)
110+
111+
set(COMPILE_OPTIONS
112+
-I "${CMAKE_CURRENT_SOURCE_DIR}"
113+
-O3
114+
-T lib_${SM}
115+
)
116+
117+
NBL_CREATE_NSC_COMPILE_RULES(
118+
TARGET ${EXECUTABLE_NAME}SPIRV
119+
LINK_TO ${EXECUTABLE_NAME}
120+
DEPENDS ${DEPENDS}
121+
BINARY_DIR ${OUTPUT_DIRECTORY}
122+
MOUNT_POINT_DEFINE NBL_THIS_EXAMPLE_BUILD_MOUNT_POINT
123+
COMMON_OPTIONS ${COMPILE_OPTIONS}
124+
OUTPUT_VAR KEYS
125+
INCLUDE nbl/this_example/builtin/build/spirv/keys.hpp
126+
NAMESPACE nbl::this_example::builtin::build
127+
INPUTS ${JSON}
128+
)
129+
130+
NBL_CREATE_RESOURCE_ARCHIVE(
131+
NAMESPACE nbl::this_example::builtin::build
132+
TARGET ${EXECUTABLE_NAME}_builtinsBuild
133+
LINK_TO ${EXECUTABLE_NAME}
134+
BIND ${OUTPUT_DIRECTORY}
135+
BUILTINS ${KEYS}
136+
)
137+
37138

71_RayTracingPipeline/app_resources/raytrace.rahit.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace nbl::hlsl;
1010
void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes attribs)
1111
{
1212
const int instID = spirv::InstanceCustomIndexKHR;
13-
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo));
13+
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo), 8);
1414

1515
const uint32_t bitpattern = payload.pcg();
1616
// Cannot use spirv::ignoreIntersectionKHR and spirv::terminateRayKHR due to https://github.com/microsoft/DirectXShaderCompiler/issues/7279

71_RayTracingPipeline/app_resources/raytrace.rchit.hlsl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary)
3838

3939
if (normalBufferAddress == 0)
4040
{
41-
float3 v0 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[0] * 12);
42-
float3 v1 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[1] * 12);
43-
float3 v2 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[2] * 12);
41+
float3 v0 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[0] * 12, 8);
42+
float3 v1 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[1] * 12, 8);
43+
float3 v2 = vk::RawBufferLoad<float3>(vertexBufferAddress + indices[2] * 12, 8);
4444

4545
return normalize(cross(v2 - v0, v1 - v0));
4646
}
@@ -50,9 +50,9 @@ float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary)
5050
{
5151
case NT_R8G8B8A8_SNORM:
5252
{
53-
uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[0] * 4);
54-
uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[1] * 4);
55-
uint32_t v2 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[2] * 4);
53+
uint32_t v0 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[0] * 4, 8);
54+
uint32_t v1 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[1] * 4, 8);
55+
uint32_t v2 = vk::RawBufferLoad<uint32_t>(normalBufferAddress + indices[2] * 4, 8);
5656

5757
n0 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v0).xyz);
5858
n1 = normalize(nbl::hlsl::spirv::unpackSnorm4x8(v1).xyz);
@@ -61,9 +61,9 @@ float3 calculateNormals(int primID, STriangleGeomInfo geom, float2 bary)
6161
break;
6262
case NT_R32G32B32_SFLOAT:
6363
{
64-
n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[0] * 12));
65-
n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[1] * 12));
66-
n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[2] * 12));
64+
n0 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[0] * 12, 8));
65+
n1 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[1] * 12, 8));
66+
n2 = normalize(vk::RawBufferLoad<float3>(normalBufferAddress + indices[2] * 12, 8));
6767
}
6868
break;
6969
}
@@ -81,7 +81,7 @@ void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes
8181
const int primID = spirv::PrimitiveId;
8282
const int instanceCustomIndex = spirv::InstanceCustomIndexKHR;
8383
const int geometryIndex = spirv::RayGeometryIndexKHR;
84-
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + (instanceCustomIndex + geometryIndex) * sizeof(STriangleGeomInfo));
84+
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + (instanceCustomIndex + geometryIndex) * sizeof(STriangleGeomInfo), 8);
8585
const float32_t3 vertexNormal = calculateNormals(primID, geom, attribs.barycentrics);
8686
const float32_t3 worldNormal = normalize(mul(vertexNormal, transpose(spirv::WorldToObjectKHR)).xyz);
8787

71_RayTracingPipeline/app_resources/raytrace.rgen.hlsl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "common.hlsl"
22

3-
#include "nbl/builtin/hlsl/jit/device_capabilities.hlsl"
43
#include "nbl/builtin/hlsl/random/xoroshiro.hlsl"
54

65
#include "nbl/builtin/hlsl/glsl_compat/core.hlsl"

71_RayTracingPipeline/app_resources/raytrace_shadow.rahit.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using namespace nbl::hlsl;
1010
void main(inout OcclusionPayload payload, in BuiltInTriangleIntersectionAttributes attribs)
1111
{
1212
const int instID = spirv::InstanceCustomIndexKHR;
13-
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo));
13+
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo), 8);
1414
const Material material = nbl::hlsl::_static_cast<Material>(geom.material);
1515

1616
const float attenuation = (1.f-material.alpha) * payload.attenuation;

71_RayTracingPipeline/main.cpp

Lines changed: 24 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// For conditions of distribution and use, see copyright notice in nabla.h
44
#include "common.hpp"
55

6+
#include "nbl/this_example/builtin/build/spirv/keys.hpp"
7+
68
#include "nbl/ext/FullScreenTriangle/FullScreenTriangle.h"
79
#include "nbl/builtin/hlsl/indirect_commands.hlsl"
810

@@ -106,95 +108,42 @@ class RaytracingPipelineApp final : public SimpleWindowedApplication, public Bui
106108
if (!asset_base_t::onAppInitialized(smart_refctd_ptr(system)))
107109
return false;
108110

109-
smart_refctd_ptr<IShaderCompiler::CCache> shaderReadCache = nullptr;
110-
smart_refctd_ptr<IShaderCompiler::CCache> shaderWriteCache = core::make_smart_refctd_ptr<IShaderCompiler::CCache>();
111-
auto shaderCachePath = localOutputCWD / "main_pipeline_shader_cache.bin";
112-
113-
{
114-
core::smart_refctd_ptr<system::IFile> shaderReadCacheFile;
115-
{
116-
system::ISystem::future_t<core::smart_refctd_ptr<system::IFile>> future;
117-
m_system->createFile(future, shaderCachePath.c_str(), system::IFile::ECF_READ);
118-
if (future.wait())
119-
{
120-
future.acquire().move_into(shaderReadCacheFile);
121-
if (shaderReadCacheFile)
122-
{
123-
const size_t size = shaderReadCacheFile->getSize();
124-
if (size > 0ull)
125-
{
126-
std::vector<uint8_t> contents(size);
127-
system::IFile::success_t succ;
128-
shaderReadCacheFile->read(succ, contents.data(), 0, size);
129-
if (succ)
130-
shaderReadCache = IShaderCompiler::CCache::deserialize(contents);
131-
}
132-
}
133-
}
134-
else
135-
m_logger->log("Failed Openning Shader Cache File.", ILogger::ELL_ERROR);
136-
}
137-
138-
}
139-
140111
// Load Custom Shader
141-
auto loadCompileAndCreateShader = [&](const std::string& relPath) -> smart_refctd_ptr<IShader>
112+
auto loadPrecompiledShader = [&]<core::StringLiteral ShaderKey>() -> smart_refctd_ptr<IShader>
142113
{
143114
IAssetLoader::SAssetLoadParams lp = {};
144115
lp.logger = m_logger.get();
145-
lp.workingDirectory = ""; // virtual root
146-
auto assetBundle = m_assetMgr->getAsset(relPath, lp);
116+
lp.workingDirectory = "app_resources"; // virtual root
117+
auto key = nbl::this_example::builtin::build::get_spirv_key<ShaderKey>(m_device.get());
118+
auto assetBundle = m_assetMgr->getAsset(key.data(), lp);
147119
const auto assets = assetBundle.getContents();
148120
if (assets.empty())
149121
return nullptr;
150122

151123
// lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
152-
auto sourceRaw = IAsset::castDown<IShader>(assets[0]);
153-
if (!sourceRaw)
124+
auto shader = IAsset::castDown<IShader>(assets[0]);
125+
if (!shader)
126+
{
127+
m_logger->log("Failed to load a precompiled shader.", ILogger::ELL_ERROR);
154128
return nullptr;
129+
}
155130

156-
return m_device->compileShader({ sourceRaw.get(), nullptr, shaderReadCache.get(), shaderWriteCache.get() });
131+
return shader;
157132
};
158133

159134
// load shaders
160-
const auto raygenShader = loadCompileAndCreateShader("app_resources/raytrace.rgen.hlsl");
161-
const auto closestHitShader = loadCompileAndCreateShader("app_resources/raytrace.rchit.hlsl");
162-
const auto proceduralClosestHitShader = loadCompileAndCreateShader("app_resources/raytrace_procedural.rchit.hlsl");
163-
const auto intersectionHitShader = loadCompileAndCreateShader("app_resources/raytrace.rint.hlsl");
164-
const auto anyHitShaderColorPayload = loadCompileAndCreateShader("app_resources/raytrace.rahit.hlsl");
165-
const auto anyHitShaderShadowPayload = loadCompileAndCreateShader("app_resources/raytrace_shadow.rahit.hlsl");
166-
const auto missShader = loadCompileAndCreateShader("app_resources/raytrace.rmiss.hlsl");
167-
const auto missShadowShader = loadCompileAndCreateShader("app_resources/raytrace_shadow.rmiss.hlsl");
168-
const auto directionalLightCallShader = loadCompileAndCreateShader("app_resources/light_directional.rcall.hlsl");
169-
const auto pointLightCallShader = loadCompileAndCreateShader("app_resources/light_point.rcall.hlsl");
170-
const auto spotLightCallShader = loadCompileAndCreateShader("app_resources/light_spot.rcall.hlsl");
171-
const auto fragmentShader = loadCompileAndCreateShader("app_resources/present.frag.hlsl");
172-
173-
core::smart_refctd_ptr<system::IFile> shaderWriteCacheFile;
174-
{
175-
system::ISystem::future_t<core::smart_refctd_ptr<system::IFile>> future;
176-
m_system->deleteFile(shaderCachePath); // temp solution instead of trimming, to make sure we won't have corrupted json
177-
m_system->createFile(future, shaderCachePath.c_str(), system::IFile::ECF_WRITE);
178-
if (future.wait())
179-
{
180-
future.acquire().move_into(shaderWriteCacheFile);
181-
if (shaderWriteCacheFile)
182-
{
183-
auto serializedCache = shaderWriteCache->serialize();
184-
if (shaderWriteCacheFile)
185-
{
186-
system::IFile::success_t succ;
187-
shaderWriteCacheFile->write(succ, serializedCache->getPointer(), 0, serializedCache->getSize());
188-
if (!succ)
189-
m_logger->log("Failed Writing To Shader Cache File.", ILogger::ELL_ERROR);
190-
}
191-
}
192-
else
193-
m_logger->log("Failed Creating Shader Cache File.", ILogger::ELL_ERROR);
194-
}
195-
else
196-
m_logger->log("Failed Creating Shader Cache File.", ILogger::ELL_ERROR);
197-
}
135+
const auto raygenShader = loadPrecompiledShader.operator()<"raytrace_rgen">(); // "app_resources/raytrace.rgen.hlsl"
136+
const auto closestHitShader = loadPrecompiledShader.operator()<"raytrace_rchit">(); // "app_resources/raytrace.rchit.hlsl"
137+
const auto proceduralClosestHitShader = loadPrecompiledShader.operator()<"raytrace_procedural_rchit">(); // "app_resources/raytrace_procedural.rchit.hlsl"
138+
const auto intersectionHitShader = loadPrecompiledShader.operator()<"raytrace_rint">(); // "app_resources/raytrace.rint.hlsl"
139+
const auto anyHitShaderColorPayload = loadPrecompiledShader.operator()<"raytrace_rahit">(); // "app_resources/raytrace.rahit.hlsl"
140+
const auto anyHitShaderShadowPayload = loadPrecompiledShader.operator()<"raytrace_shadow_rahit">(); // "app_resources/raytrace_shadow.rahit.hlsl"
141+
const auto missShader = loadPrecompiledShader.operator()<"raytrace_rmiss">(); // "app_resources/raytrace.rmiss.hlsl"
142+
const auto missShadowShader = loadPrecompiledShader.operator()<"raytrace_shadow_rmiss">(); // "app_resources/raytrace_shadow.rmiss.hlsl"
143+
const auto directionalLightCallShader = loadPrecompiledShader.operator()<"light_directional_rcall">(); // "app_resources/light_directional.rcall.hlsl"
144+
const auto pointLightCallShader = loadPrecompiledShader.operator()<"light_point_rcall">(); // "app_resources/light_point.rcall.hlsl"
145+
const auto spotLightCallShader = loadPrecompiledShader.operator()<"light_spot_rcall">(); // "app_resources/light_spot.rcall.hlsl"
146+
const auto fragmentShader = loadPrecompiledShader.operator()<"present_frag">(); // "app_resources/present.frag.hlsl"
198147

199148
m_semaphore = m_device->createSemaphore(m_realFrameIx);
200149
if (!m_semaphore)

0 commit comments

Comments
 (0)