Skip to content

Commit cef80b3

Browse files
committed
Create shaders and pipelines
1 parent 0e3e125 commit cef80b3

File tree

3 files changed

+78
-30
lines changed

3 files changed

+78
-30
lines changed

26_Autoexposure/app_resources/luma_meter.comp.hlsl

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

5+
#include "nbl/builtin/hlsl/luma_meter/luma_meter.hlsl"
56
#include "app_resources/common.hlsl"
67

78
[[vk::push_constant]] AutoexposurePushData pushData;
@@ -11,7 +12,7 @@ uint32_t3 nbl::hlsl::glsl::gl_WorkGroupSize()
1112
return uint32_t3(WorkgroupSize, 1, 1);
1213
}
1314

14-
[numthreads(SubgroupSize, SubgroupSize, 1)]
15+
[numthreads(DeviceSubgroupSize, DeviceSubgroupSize, 1)]
1516
void main(uint32_t3 ID : SV_GroupThreadID, uint32_t3 GroupID : SV_GroupID)
1617
{
1718
}

26_Autoexposure/app_resources/tonemap.comp.hlsl renamed to 26_Autoexposure/app_resources/tonemapper.comp.hlsl

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

5+
#include "nbl/builtin/hlsl/glsl_compat/core.hlsl"
56
#include "app_resources/common.hlsl"
67

78
[[vk::push_constant]] AutoexposurePushData pushData;
89

9-
uint32_t3 nbl::hlsl::glsl::gl_WorkGroupSize()
10-
{
11-
return uint32_t3(WorkgroupSize, 1, 1);
12-
}
13-
14-
[numthreads(SubgroupSize, SubgroupSize, 1)]
10+
[numthreads(DeviceSubgroupSize, DeviceSubgroupSize, 1)]
1511
void main(uint32_t3 ID : SV_GroupThreadID, uint32_t3 GroupID : SV_GroupID)
1612
{
1713
}

26_Autoexposure/main.cpp

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "nbl/asset/interchange/IAssetLoader.h"
99
#include "nbl/ext/FullScreenTriangle/FullScreenTriangle.h"
1010

11+
#include "app_resources/common.hlsl"
12+
1113
using namespace nbl;
1214
using namespace core;
1315
using namespace hlsl;
@@ -186,41 +188,90 @@ class AutoexposureApp final : public examples::SimpleWindowedApplication, public
186188
return logFail("Failed to create Renderpass!");
187189
}
188190

189-
// Load the shaders and create the pipeline
191+
// Load the shaders and create the pipelines
190192
{
193+
auto loadCompileAndCreateShader = [&](const std::string& relPath) -> smart_refctd_ptr<IGPUShader>
194+
{
195+
IAssetLoader::SAssetLoadParams lp = {};
196+
lp.logger = m_logger.get();
197+
lp.workingDirectory = ""; // virtual root
198+
auto assetBundle = m_assetMgr->getAsset(relPath, lp);
199+
const auto assets = assetBundle.getContents();
200+
if (assets.empty())
201+
return nullptr;
202+
203+
// lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
204+
auto source = IAsset::castDown<ICPUShader>(assets[0]);
205+
if (!source)
206+
return nullptr;
207+
const uint32_t workgroupSize = m_physicalDevice->getLimits().maxComputeWorkGroupInvocations;
208+
const uint32_t subgroupSize = m_physicalDevice->getLimits().maxSubgroupSize;
209+
auto overriddenSource = CHLSLCompiler::createOverridenCopy(
210+
source.get(),
211+
"#define WorkgroupSize %d\n#define DeviceSubgroupSize %d\n",
212+
workgroupSize,
213+
subgroupSize
214+
);
215+
216+
return m_device->createShader(overriddenSource.get());
217+
};
218+
219+
auto createComputePipeline = [&](smart_refctd_ptr<IGPUShader> shader, smart_refctd_ptr<IGPUComputePipeline> pipeline) -> bool
220+
{
221+
const nbl::asset::SPushConstantRange pcRange = {
222+
.stageFlags = IShader::E_SHADER_STAGE::ESS_COMPUTE,
223+
.offset = 0,
224+
.size = sizeof(AutoexposurePushData)
225+
};
226+
227+
smart_refctd_ptr<IGPUPipelineLayout> layout;
228+
{
229+
layout = m_device->createPipelineLayout({ &pcRange,1 });
230+
IGPUComputePipeline::SCreationParams params = {};
231+
params.layout = layout.get();
232+
params.shader.shader = shader.get();
233+
params.shader.entryPoint = "main";
234+
params.shader.entries = nullptr;
235+
params.shader.requireFullSubgroups = true;
236+
params.shader.requiredSubgroupSize = static_cast<IGPUShader::SSpecInfo::SUBGROUP_SIZE>(5);
237+
if (!m_device->createComputePipelines(nullptr, { &params,1 }, &pipeline))
238+
return logFail("Failed to create compute pipeline!\n");
239+
}
240+
241+
return true;
242+
};
243+
244+
// Luma Meter
245+
auto lumaMeterShader = loadCompileAndCreateShader("app_resources/luma_meter.comp.hlsl");
246+
if (!lumaMeterShader)
247+
return logFail("Failed to Load and Compile Compute Shader: lumaMeterShader!");
248+
auto lumaPresentLayout = m_device->createPipelineLayout({}, nullptr, nullptr, nullptr, core::smart_refctd_ptr(lumaPresentDSLayout));
249+
if (!createComputePipeline(lumaMeterShader, m_lumaMeterPipeline))
250+
return logFail("Could not create Luma Meter Pipeline!");
251+
252+
// Tonemapper
253+
auto tonemapperShader = loadCompileAndCreateShader("app_resources/tonemapper.comp.hlsl");
254+
if (!tonemapperShader)
255+
return logFail("Failed to Load and Compile Compute Shader: tonemapperShader!");
256+
auto tonemapperLayout = m_device->createPipelineLayout({}, nullptr, nullptr, nullptr, core::smart_refctd_ptr(tonemapperDSLayout));
257+
if (!createComputePipeline(tonemapperShader, m_tonemapperPipeline))
258+
return logFail("Could not create Luma Meter Pipeline!");
259+
191260
// Load FSTri Shader
192261
ext::FullScreenTriangle::ProtoPipeline fsTriProtoPPln(m_assetMgr.get(), m_device.get(), m_logger.get());
193262
if (!fsTriProtoPPln)
194263
return logFail("Failed to create Full Screen Triangle protopipeline or load its vertex shader!");
195264

196-
// Load Custom Shader
197-
auto loadCompileAndCreateShader = [&](const std::string& relPath) -> smart_refctd_ptr<IGPUShader>
198-
{
199-
IAssetLoader::SAssetLoadParams lp = {};
200-
lp.logger = m_logger.get();
201-
lp.workingDirectory = ""; // virtual root
202-
auto assetBundle = m_assetMgr->getAsset(relPath, lp);
203-
const auto assets = assetBundle.getContents();
204-
if (assets.empty())
205-
return nullptr;
206-
207-
// lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
208-
auto source = IAsset::castDown<ICPUShader>(assets[0]);
209-
if (!source)
210-
return nullptr;
211-
212-
return m_device->createShader(source.get());
213-
};
214-
auto fragmentShader = loadCompileAndCreateShader("app_resources/present.frag.hlsl");
265+
// Load Fragment Shader
266+
auto fragmentShader = loadCompileAndCreateShader("app_resources/present.frag.hlsl");;
215267
if (!fragmentShader)
216-
return logFail("Failed to Load and Compile Fragment Shader!");
268+
return logFail("Failed to Load and Compile Fragment Shader: lumaMeterShader!");
217269

218-
auto layout = m_device->createPipelineLayout({}, nullptr, nullptr, nullptr, core::smart_refctd_ptr(lumaPresentDSLayout));
219270
const IGPUShader::SSpecInfo fragSpec = {
220271
.entryPoint = "main",
221272
.shader = fragmentShader.get()
222273
};
223-
m_presentPipeline = fsTriProtoPPln.createPipeline(fragSpec, layout.get(), scResources->getRenderpass());
274+
m_presentPipeline = fsTriProtoPPln.createPipeline(fragSpec, lumaPresentLayout.get(), scResources->getRenderpass());
224275
if (!m_presentPipeline)
225276
return logFail("Could not create Graphics Pipeline!");
226277
}

0 commit comments

Comments
 (0)