Skip to content

Commit 82bf36b

Browse files
Merge pull request #179 from Devsh-Graphics-Programming/ali_lib_shader2
CAD: multi entrypoint shader
2 parents e2eabd0 + d0274d9 commit 82bf36b

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

62_CAD/main.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -860,14 +860,9 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
860860

861861
pipelineLayout = m_device->createPipelineLayout({}, core::smart_refctd_ptr(descriptorSetLayout0), core::smart_refctd_ptr(descriptorSetLayout1), nullptr, nullptr);
862862
}
863-
864-
// Main Pipeline Shaders
865-
std::array<smart_refctd_ptr<IGPUShader>, 4u> mainPipelineShaders = {};
866-
constexpr auto vertexShaderPath = "../shaders/main_pipeline/vertex_shader.hlsl";
867-
constexpr auto fragmentShaderPath = "../shaders/main_pipeline/fragment_shader.hlsl";
868-
constexpr auto debugfragmentShaderPath = "../shaders/main_pipeline/fragment_shader_debug.hlsl";
869-
constexpr auto resolveAlphasShaderPath = "../shaders/main_pipeline/resolve_alphas.hlsl";
870-
// GeoTexture Pipeline Shaders
863+
864+
smart_refctd_ptr<IGPUShader> mainPipelineFragmentShaders = {};
865+
smart_refctd_ptr<IGPUShader> mainPipelineVertexShader = {};
871866
std::array<smart_refctd_ptr<IGPUShader>, 2u> geoTexturePipelineShaders = {};
872867
{
873868
smart_refctd_ptr<IShaderCompiler::CCache> shaderReadCache = nullptr;
@@ -902,7 +897,7 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
902897
}
903898

904899
// Load Custom Shader
905-
auto loadCompileAndCreateShader = [&](const std::string& relPath, IShader::E_SHADER_STAGE stage) -> smart_refctd_ptr<IGPUShader>
900+
auto loadCompileShader = [&](const std::string& relPath, IShader::E_SHADER_STAGE stage) -> smart_refctd_ptr<ICPUShader>
906901
{
907902
IAssetLoader::SAssetLoadParams lp = {};
908903
lp.logger = m_logger.get();
@@ -914,19 +909,23 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
914909

915910
// lets go straight from ICPUSpecializedShader to IGPUSpecializedShader
916911
auto cpuShader = IAsset::castDown<ICPUShader>(assets[0]);
917-
cpuShader->setShaderStage(stage);
918912
if (!cpuShader)
919913
return nullptr;
920914

921-
return m_device->createShader({ cpuShader.get(), nullptr, shaderReadCache.get(), shaderWriteCache.get()});
915+
cpuShader->setShaderStage(stage);
916+
return m_device->compileShader({ cpuShader.get(), nullptr, shaderReadCache.get(), shaderWriteCache.get() });
922917
};
923-
mainPipelineShaders[0] = loadCompileAndCreateShader(vertexShaderPath, IShader::E_SHADER_STAGE::ESS_VERTEX);
924-
mainPipelineShaders[1] = loadCompileAndCreateShader(fragmentShaderPath, IShader::E_SHADER_STAGE::ESS_FRAGMENT);
925-
mainPipelineShaders[2] = loadCompileAndCreateShader(debugfragmentShaderPath, IShader::E_SHADER_STAGE::ESS_FRAGMENT);
926-
mainPipelineShaders[3] = loadCompileAndCreateShader(resolveAlphasShaderPath, IShader::E_SHADER_STAGE::ESS_FRAGMENT);
927-
928-
geoTexturePipelineShaders[0] = loadCompileAndCreateShader(GeoTextureRenderer::VertexShaderRelativePath, IShader::E_SHADER_STAGE::ESS_VERTEX);
929-
geoTexturePipelineShaders[1] = loadCompileAndCreateShader(GeoTextureRenderer::FragmentShaderRelativePath, IShader::E_SHADER_STAGE::ESS_FRAGMENT);
918+
919+
auto mainPipelineFragmentCpuShader = loadCompileShader("../shaders/main_pipeline/fragment.hlsl", IShader::E_SHADER_STAGE::ESS_ALL_OR_LIBRARY);
920+
auto mainPipelineVertexCpuShader = loadCompileShader("../shaders/main_pipeline/vertex_shader.hlsl", IShader::E_SHADER_STAGE::ESS_VERTEX);
921+
auto geoTexturePipelineVertCpuShader = loadCompileShader(GeoTextureRenderer::VertexShaderRelativePath, IShader::E_SHADER_STAGE::ESS_VERTEX);
922+
auto geoTexturePipelineFragCpuShader = loadCompileShader(GeoTextureRenderer::FragmentShaderRelativePath, IShader::E_SHADER_STAGE::ESS_FRAGMENT);
923+
mainPipelineFragmentCpuShader->setShaderStage(IShader::E_SHADER_STAGE::ESS_FRAGMENT);
924+
925+
mainPipelineFragmentShaders = m_device->createShader({ mainPipelineFragmentCpuShader.get(), nullptr, shaderReadCache.get(), shaderWriteCache.get() });
926+
mainPipelineVertexShader = m_device->createShader({ mainPipelineVertexCpuShader.get(), nullptr, shaderReadCache.get(), shaderWriteCache.get() });
927+
geoTexturePipelineShaders[0] = m_device->createShader({ geoTexturePipelineVertCpuShader.get(), nullptr, shaderReadCache.get(), shaderWriteCache.get() });
928+
geoTexturePipelineShaders[1] = m_device->createShader({ geoTexturePipelineFragCpuShader.get(), nullptr, shaderReadCache.get(), shaderWriteCache.get() });
930929

931930
core::smart_refctd_ptr<system::IFile> shaderWriteCacheFile;
932931
{
@@ -970,10 +969,7 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
970969
// Load FSTri Shader
971970
ext::FullScreenTriangle::ProtoPipeline fsTriangleProtoPipe(m_assetMgr.get(),m_device.get(),m_logger.get());
972971

973-
const IGPUShader::SSpecInfo fragSpec = {
974-
.entryPoint = "main",
975-
.shader = mainPipelineShaders[3u].get()
976-
};
972+
const IGPUShader::SSpecInfo fragSpec = { .entryPoint = "resolveAlphaMain", .shader = mainPipelineFragmentShaders.get() };
977973

978974
resolveAlphaGraphicsPipeline = fsTriangleProtoPipe.createPipeline(fragSpec, pipelineLayout.get(), compatibleRenderPass.get(), 0u, blendParams);
979975
if (!resolveAlphaGraphicsPipeline)
@@ -985,8 +981,14 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
985981
{
986982

987983
IGPUShader::SSpecInfo specInfo[2] = {
988-
{.shader=mainPipelineShaders[0u].get() },
989-
{.shader=mainPipelineShaders[1u].get() },
984+
{
985+
.entryPoint = "main",
986+
.shader = mainPipelineVertexShader.get()
987+
},
988+
{
989+
.entryPoint = "fragMain",
990+
.shader = mainPipelineFragmentShaders.get()
991+
},
990992
};
991993

992994
IGPUGraphicsPipeline::SCreationParams params[1] = {};
@@ -1011,7 +1013,7 @@ class ComputerAidedDesign final : public examples::SimpleWindowedApplication, pu
10111013

10121014
if constexpr (DebugModeWireframe)
10131015
{
1014-
specInfo[1u].shader = mainPipelineShaders[2u].get(); // change only fragment shader to fragment_shader_debug.hlsl
1016+
specInfo[1u].entryPoint = "fragDebugMain"; // change only fragment shader entrypoint
10151017
params[0].cached.rasterization.polygonMode = asset::EPM_LINE;
10161018

10171019
if (!m_device->createGraphicsPipelines(nullptr,params,&debugGraphicsPipeline))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include "fragment_shader.hlsl"
2+
#include "fragment_shader_debug.hlsl"
3+
#include "resolve_alphas.hlsl"
4+

62_CAD/shaders/main_pipeline/fragment_shader.hlsl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#pragma shader_stage(fragment)
2-
31
#include "common.hlsl"
42
#include <nbl/builtin/hlsl/shapes/beziers.hlsl>
53
#include <nbl/builtin/hlsl/shapes/line.hlsl>
@@ -359,8 +357,6 @@ template<>
359357
float32_t4 calculateFinalColor<true>(const uint2 fragCoord, const float localAlpha, const uint32_t currentMainObjectIdx, float3 localTextureColor, bool colorFromTexture)
360358
{
361359
float32_t4 color;
362-
363-
nbl::hlsl::spirv::execution_mode::PixelInterlockOrderedEXT();
364360
nbl::hlsl::spirv::beginInvocationInterlockEXT();
365361

366362
const uint32_t packedData = pseudoStencil[fragCoord];
@@ -405,8 +401,9 @@ float32_t4 calculateFinalColor<true>(const uint2 fragCoord, const float localAlp
405401
return color;
406402
}
407403

408-
409-
float4 main(PSInput input) : SV_TARGET
404+
[[vk::spvexecutionmode(spv::ExecutionModePixelInterlockOrderedEXT)]]
405+
[shader("pixel")]
406+
float4 fragMain(PSInput input) : SV_TARGET
410407
{
411408
float localAlpha = 0.0f;
412409
float3 textureColor = float3(0, 0, 0); // color sampled from a texture

62_CAD/shaders/main_pipeline/fragment_shader_debug.hlsl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
2-
#pragma shader_stage(fragment)
3-
4-
struct PSInput
1+
struct PSInputDebug
52
{
63
float4 position : SV_Position;
74
[[vk::location(0)]] float4 color : COLOR;
85
[[vk::location(1)]] nointerpolation float4 start_end : COLOR1;
96
[[vk::location(2)]] nointerpolation uint3 lineWidth_eccentricity_objType : COLOR2;
107
};
118

12-
float4 main(PSInput input) : SV_TARGET
9+
[shader("pixel")]
10+
float4 fragDebugMain(PSInputDebug input) : SV_TARGET
1311
{
1412
return float4(1.0, 1.0, 1.0, 1.0);
1513
// return input.color;

62_CAD/shaders/main_pipeline/resolve_alphas.hlsl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#pragma shader_stage(fragment)
2-
31
#include "common.hlsl"
42
#include <nbl/builtin/hlsl/spirv_intrinsics/fragment_shader_pixel_interlock.hlsl>
53
#include <nbl/builtin/hlsl/jit/device_capabilities.hlsl>
@@ -19,7 +17,6 @@ float32_t4 calculateFinalColor<true>(const uint2 fragCoord)
1917
{
2018
float32_t4 color;
2119

22-
nbl::hlsl::spirv::execution_mode::PixelInterlockOrderedEXT();
2320
nbl::hlsl::spirv::beginInvocationInterlockEXT();
2421

2522
const uint32_t packedData = pseudoStencil[fragCoord];
@@ -54,7 +51,9 @@ float32_t4 calculateFinalColor<true>(const uint2 fragCoord)
5451
return color;
5552
}
5653

57-
float4 main(float4 position : SV_Position) : SV_TARGET
54+
[[vk::spvexecutionmode(spv::ExecutionModePixelInterlockOrderedEXT)]]
55+
[shader("pixel")]
56+
float4 resolveAlphaMain(float4 position : SV_Position) : SV_TARGET
5857
{
5958
return calculateFinalColor<nbl::hlsl::jit::device_capabilities::fragmentShaderPixelInterlock>(position.xy);
6059
}

0 commit comments

Comments
 (0)