Skip to content

Commit

Permalink
SVC guiding uber shader compiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Sep 15, 2023
1 parent 8b6d9a3 commit 7479561
Show file tree
Hide file tree
Showing 24 changed files with 186 additions and 196 deletions.
6 changes: 3 additions & 3 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,10 @@ bool EditorApp::IsAtmosphericScatteringEnable() const

void EditorApp::InitShaderPrograms() const
{
ShaderBuilder::BuildShaders(m_pShaderVariantCollections.get());
ShaderBuilder::BuildNonUberShaders(m_pShaderVariantCollections.get());

ShaderBuilder::BuildUberShader(m_pSceneWorld->GetPBRMaterialType());
ShaderBuilder::BuildUberShader(m_pSceneWorld->GetAnimationMaterialType());
ShaderBuilder::BuildUberShader(m_pShaderVariantCollections.get(), m_pSceneWorld->GetPBRMaterialType());
ShaderBuilder::BuildUberShader(m_pShaderVariantCollections.get(), m_pSceneWorld->GetAnimationMaterialType());
#ifdef ENABLE_DDGI
ShaderBuilder::BuildUberShader(m_pSceneWorld->GetDDGIMaterialType());
#endif
Expand Down
68 changes: 40 additions & 28 deletions Engine/Source/Editor/Resources/ShaderBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,40 @@
namespace editor
{

void ShaderBuilder::BuildUberShader(engine::MaterialType* pMaterialType)
void ShaderBuilder::BuildUberShader(engine::ShaderVariantCollections* pCollections, engine::MaterialType* pMaterialType)
{
const std::string& programName = pMaterialType->GetShaderSchema().GetProgramName();

const std::set<std::string>& shaders = pCollections->GetUberShaders(programName);
const std::set<std::string>& combines = pCollections->GetFeatureCombines(programName);
for (const std::string& shader : shaders)
{
ShaderType shaderType = GetShaderType(shader);
if (ShaderType::Vertex == shaderType)
{
std::string inputVSFilePath = engine::Path::GetBuiltinShaderInputPath(shader.c_str());
std::string outputVSFilePath = engine::Path::GetShaderOutputPath(shader.c_str());
ResourceBuilder::Get().AddShaderBuildTask(ShaderType::Vertex, inputVSFilePath.c_str(), outputVSFilePath.c_str());
}
else if (ShaderType::Fragment == shaderType)
{
for (const auto& combine : combines)
{
std::string inputVFSFilePath = engine::Path::GetBuiltinShaderInputPath(shader.c_str());
std::string outputFSFilePath = engine::Path::GetShaderOutputPath(shader.c_str(), combine);
ResourceBuilder::Get().AddShaderBuildTask(ShaderType::Fragment, inputVFSFilePath.c_str(), outputFSFilePath.c_str(), combine.c_str());
}
}
else
{
// No shader feature support for vertex and compute shader.
continue;
}
}

engine::ShaderSchema& shaderSchema = pMaterialType->GetShaderSchema();

// No shader feature supports for VS now.
// No shader feature support for VS now.
std::string outputVSFilePath = engine::Path::GetShaderOutputPath(shaderSchema.GetVertexShaderPath());
ResourceBuilder::Get().AddShaderBuildTask(ShaderType::Vertex,
shaderSchema.GetVertexShaderPath(), outputVSFilePath.c_str());
Expand All @@ -28,38 +57,21 @@ void ShaderBuilder::BuildUberShader(engine::MaterialType* pMaterialType)
CD_ENGINE_INFO("Shader variant count of material type {0} : {1}", pMaterialType->GetMaterialName(), shaderSchema.GetFeatureCombines().size());
}

void ShaderBuilder::BuildShaders(engine::ShaderVariantCollections* pCollections)
void ShaderBuilder::BuildNonUberShaders(engine::ShaderVariantCollections* pCollections)
{
const auto& porgrams = pCollections->GetShaderPrograms();

for (const auto& [programName, pack] : porgrams)
for (const auto& shaders : pCollections->GetNonUberShaderPrograms())
{
if (pack.GetFeatureSet().empty())
for (const auto& shader : shaders.second)
{
// Non-Uber Shaders

for (const auto& shaderName : pack.GetShaderNames())
ShaderType shaderType = GetShaderType(shader);
if (ShaderType::None == shaderType)
{
ShaderType shaderType = GetShaderType(shaderName);
if (ShaderType::None == shaderType)
{
continue;
}

// TODO : GetBuiltinShaderInputPath()
std::filesystem::path inputShaderFullPath = CDENGINE_BUILTIN_SHADER_PATH;
inputShaderFullPath += "shaders/";
inputShaderFullPath += shaderName;
inputShaderFullPath .replace_extension(ShaderExtension);

std::string outputShaderPath = engine::Path::GetShaderOutputPath(shaderName.c_str());
ResourceBuilder::Get().AddShaderBuildTask(shaderType, inputShaderFullPath.generic_string().c_str(), outputShaderPath.c_str());
continue;
}
}
else
{
// Uber Shaders

std::string inputShaderPath = engine::Path::GetBuiltinShaderInputPath(shader.c_str());
std::string outputShaderPath = engine::Path::GetShaderOutputPath(shader.c_str());
ResourceBuilder::Get().AddShaderBuildTask(shaderType, inputShaderPath.c_str(), outputShaderPath.c_str());
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions Engine/Source/Editor/Resources/ShaderBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ namespace editor
class ShaderBuilder
{
public:
static constexpr const char* ShaderExtension = ".sc";

public:
static void BuildUberShader(engine::MaterialType* pMaterialType);

static void BuildShaders(engine::ShaderVariantCollections* pCollections);
static void BuildNonUberShaders(engine::ShaderVariantCollections* pCollections);
static void BuildUberShader(engine::ShaderVariantCollections* pCollections, engine::MaterialType* pMaterialType);

private:
static const ShaderType GetShaderType(const std::string& fileName);
Expand Down
4 changes: 2 additions & 2 deletions Engine/Source/Runtime/ECWorld/SceneWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void SceneWorld::CreatePBRMaterialType(bool isAtmosphericScatteringEnable)
m_pPBRMaterialType = std::make_unique<MaterialType>();
m_pPBRMaterialType->SetMaterialName("CD_PBR");

ShaderSchema shaderSchema(Path::GetBuiltinShaderInputPath("shaders/vs_PBR"), Path::GetBuiltinShaderInputPath("shaders/fs_PBR"));
ShaderSchema shaderSchema("WorldProgram", Path::GetBuiltinShaderInputPath("vs_PBR"), Path::GetBuiltinShaderInputPath("fs_PBR"));
shaderSchema.AddFeatureSet({ ShaderFeature::ALBEDO_MAP });
shaderSchema.AddFeatureSet({ ShaderFeature::NORMAL_MAP });
shaderSchema.AddFeatureSet({ ShaderFeature::ORM_MAP });
Expand Down Expand Up @@ -86,7 +86,7 @@ void SceneWorld::CreateAnimationMaterialType()
m_pAnimationMaterialType = std::make_unique<MaterialType>();
m_pAnimationMaterialType->SetMaterialName("CD_Animation");

ShaderSchema shaderSchema(Path::GetBuiltinShaderInputPath("shaders/vs_animation"), Path::GetBuiltinShaderInputPath("shaders/fs_animation"));
ShaderSchema shaderSchema("AnimationProgram", Path::GetBuiltinShaderInputPath("vs_animation"), Path::GetBuiltinShaderInputPath("fs_animation"));
m_pAnimationMaterialType->SetShaderSchema(cd::MoveTemp(shaderSchema));

cd::VertexFormat animationVertexFormat;
Expand Down
5 changes: 3 additions & 2 deletions Engine/Source/Runtime/Material/ShaderSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
namespace engine
{

ShaderSchema::ShaderSchema(std::string vsPath, std::string fsPath)
ShaderSchema::ShaderSchema(std::string progeamName, std::string vsPath, std::string fsPath)
{
m_programName = cd::MoveTemp(progeamName);
m_vertexShaderPath = cd::MoveTemp(vsPath);
m_fragmentShaderPath = cd::MoveTemp(fsPath);

Expand Down Expand Up @@ -181,7 +182,7 @@ void ShaderSchema::AddUberFSBlob(StringCrc shaderFeaturesCrc, ShaderBlob shaderB
m_shaderFeaturesToFSBlobs[shaderFeaturesCrc.Value()] = std::make_unique<ShaderBlob>(cd::MoveTemp(shaderBlob));
}

const ShaderBlob& ShaderSchema::GetFSBlob(StringCrc shaderFeaturesCrc) const
const ShaderSchema::ShaderBlob& ShaderSchema::GetFSBlob(StringCrc shaderFeaturesCrc) const
{
auto itBlob = m_shaderFeaturesToFSBlobs.find(shaderFeaturesCrc.Value());
assert(itBlob != m_shaderFeaturesToFSBlobs.end());
Expand Down
7 changes: 5 additions & 2 deletions Engine/Source/Runtime/Material/ShaderSchema.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ class ShaderSchema
public:
static constexpr uint16_t InvalidProgramHandle = UINT16_MAX;
static constexpr StringCrc DefaultUberShaderCrc = StringCrc("");
using ShaderBlob = std::vector<std::byte>;

public:
ShaderSchema() = default;
explicit ShaderSchema(std::string vsPath, std::string fsPath);
explicit ShaderSchema(std::string progeamName, std::string vsPath, std::string fsPath);
ShaderSchema(const ShaderSchema&) = delete;
ShaderSchema& operator=(const ShaderSchema&) = delete;
ShaderSchema(ShaderSchema&&) = default;
ShaderSchema& operator=(ShaderSchema&&) = default;
~ShaderSchema() = default;

const char* GetProgramName() const { return m_programName.c_str(); }
const char* GetVertexShaderPath() const { return m_vertexShaderPath.c_str(); }
const char* GetFragmentShaderPath() const { return m_fragmentShaderPath.c_str(); }

Expand Down Expand Up @@ -68,6 +70,8 @@ class ShaderSchema
const ShaderBlob& GetFSBlob(StringCrc shaderFeaturesCrc) const;

private:
std::string m_programName;

std::string m_vertexShaderPath;
std::string m_fragmentShaderPath;

Expand All @@ -76,7 +80,6 @@ class ShaderSchema
std::vector<ShaderFeatureSet> m_shaderFeatureSets;
// Parameters to compile shaders.
std::vector<std::string> m_featureCombines;

// Key: StringCrc(feature combine), Value: shader handle.
std::map<uint32_t, uint16_t> m_compiledProgramHandles;

Expand Down
10 changes: 5 additions & 5 deletions Engine/Source/Runtime/Path/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void Path::SetGraphicsBackend(engine::GraphicsBackend backend)

std::string Path::GetBuiltinShaderInputPath(const char* pShaderName)
{
return (GetEngineBuiltinShaderPath() / pShaderName).replace_extension(ShaderInputExtension).string();
return (GetEngineBuiltinShaderPath() / "shaders" / pShaderName).replace_extension(ShaderInputExtension).generic_string();
}

std::filesystem::path Path::GetShaderOutputDirectory()
Expand All @@ -101,7 +101,7 @@ std::filesystem::path Path::GetShaderOutputDirectory()

std::string Path::GetShaderOutputPath(const char* pInputFilePath, const std::string& options)
{
std::string outputShaderFileName = std::filesystem::path(pInputFilePath).stem().string();
std::string outputShaderFileName = std::filesystem::path(pInputFilePath).stem().generic_string();

if (!options.empty())
{
Expand All @@ -117,17 +117,17 @@ std::string Path::GetShaderOutputPath(const char* pInputFilePath, const std::str
}
}

return (GetShaderOutputDirectory() / cd::MoveTemp(outputShaderFileName)).replace_extension(ShaderOutputExtension).string();
return (GetShaderOutputDirectory() / cd::MoveTemp(outputShaderFileName)).replace_extension(ShaderOutputExtension).generic_string();
}

std::string Path::GetTextureOutputFilePath(const char* pInputFilePath, const char* extension)
{
return ((GetEngineResourcesPath() / "Textures" / std::filesystem::path(pInputFilePath).stem()).replace_extension(extension)).string();
return ((GetEngineResourcesPath() / "Textures" / std::filesystem::path(pInputFilePath).stem()).replace_extension(extension)).generic_string();
}

std::string Path::GetTerrainTextureOutputFilePath(const char* pInputFilePath, const char* extension)
{
return ((GetEngineResourcesPath() / "Textures" / "Terrain" / std::filesystem::path(pInputFilePath).stem()).replace_extension(extension)).string();
return ((GetEngineResourcesPath() / "Textures" / "Terrain" / std::filesystem::path(pInputFilePath).stem()).replace_extension(extension)).generic_string();
}

}
10 changes: 5 additions & 5 deletions Engine/Source/Runtime/Path/Path.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ class Path

static std::optional<std::filesystem::path> GetApplicationDataPath();

static std::filesystem::path GetEngineBuiltinShaderPath();
static std::filesystem::path GetEngineResourcesPath();
static std::filesystem::path GetEditorResourcesPath();
static std::filesystem::path GetProjectsSharedPath();

static engine::GraphicsBackend GetGraphicsBackend();
static void SetGraphicsBackend(engine::GraphicsBackend backend);

Expand All @@ -35,6 +30,11 @@ class Path
static std::string GetTerrainTextureOutputFilePath(const char* pInputFilePath, const char* extension);

private:
static std::filesystem::path GetEngineBuiltinShaderPath();
static std::filesystem::path GetEngineResourcesPath();
static std::filesystem::path GetEditorResourcesPath();
static std::filesystem::path GetProjectsSharedPath();

static const char* GetPlatformPathKey();
static std::filesystem::path GetPlatformAppDataPath(const char* pRootPath);

Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/AABBRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace engine

void AABBRenderer::Init()
{
GetShaderVariantCollections()->RegisterPragram("AABBProgram", {"vs_AABB", "fs_AABB"});
GetShaderVariantCollections()->RegisterNonUberShader("AABBProgram", {"vs_AABB", "fs_AABB"});

bgfx::setViewName(GetViewID(), "AABBRenderer");
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/AnimationRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void CalculateBoneTransform(std::vector<cd::Matrix4x4>& boneMatrices, const cd::

void AnimationRenderer::Init()
{
GetShaderVariantCollections()->RegisterPragram("AnimationProgram", { "vs_animation", "fs_animation" });
GetShaderVariantCollections()->RegisterNonUberShader("AnimationProgram", { "vs_animation", "fs_animation" });

bgfx::setViewName(GetViewID(), "AnimationRenderer");
}
Expand Down
14 changes: 7 additions & 7 deletions Engine/Source/Runtime/Rendering/BloomRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace engine
{
void BloomRenderer::Init()
{
GetShaderVariantCollections()->RegisterPragram("CapTureBrightnessProgram", { "vs_fullscreen", "fs_captureBrightness" });
GetShaderVariantCollections()->RegisterPragram("DownSampleProgram", { "vs_fullscreen", "fs_dowmsample" });
GetShaderVariantCollections()->RegisterPragram("BlurVerticalProgram", { "vs_fullscreen", "fs_blurvertical" });
GetShaderVariantCollections()->RegisterPragram("BlurHorizontalProgram", { "vs_fullscreen", "fs_blurhorizontal" });
GetShaderVariantCollections()->RegisterPragram("UpSampleProgram", { "vs_fullscreen", "fs_upsample" });
GetShaderVariantCollections()->RegisterPragram("KawaseBlurProgram", { "vs_fullscreen", "fs_kawaseblur" });
GetShaderVariantCollections()->RegisterPragram("CombineProgram", { "vs_fullscreen", "fs_bloom" });
GetShaderVariantCollections()->RegisterNonUberShader("CapTureBrightnessProgram", { "vs_fullscreen", "fs_captureBrightness" });
GetShaderVariantCollections()->RegisterNonUberShader("DownSampleProgram", { "vs_fullscreen", "fs_dowmsample" });
GetShaderVariantCollections()->RegisterNonUberShader("BlurVerticalProgram", { "vs_fullscreen", "fs_blurvertical" });
GetShaderVariantCollections()->RegisterNonUberShader("BlurHorizontalProgram", { "vs_fullscreen", "fs_blurhorizontal" });
GetShaderVariantCollections()->RegisterNonUberShader("UpSampleProgram", { "vs_fullscreen", "fs_upsample" });
GetShaderVariantCollections()->RegisterNonUberShader("KawaseBlurProgram", { "vs_fullscreen", "fs_kawaseblur" });
GetShaderVariantCollections()->RegisterNonUberShader("CombineProgram", { "vs_fullscreen", "fs_bloom" });

bgfx::setViewName(GetViewID(), "BloomRenderer");
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/DebugRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace engine

void DebugRenderer::Init()
{
GetShaderVariantCollections()->RegisterPragram("DebugProgram", { "vs_debug", "fs_debug" });
GetShaderVariantCollections()->RegisterNonUberShader("DebugProgram", { "vs_debug", "fs_debug" });

bgfx::setViewName(GetViewID(), "DebugRenderer");
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/ImGuiRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace engine

void ImGuiRenderer::Init()
{
GetShaderVariantCollections()->RegisterPragram("ImGuiProgram", { "vs_imgui", "fs_imgui" });
GetShaderVariantCollections()->RegisterNonUberShader("ImGuiProgram", { "vs_imgui", "fs_imgui" });

bgfx::setViewName(GetViewID(), "ImGuiRenderer");
}
Expand Down
16 changes: 8 additions & 8 deletions Engine/Source/Runtime/Rendering/PBRSkyRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ constexpr uint16_t ScatteringOrders = 6;

void PBRSkyRenderer::Init()
{
GetShaderVariantCollections()->RegisterPragram(ProgramAtmosphericScatteringLUT, { "vs_atmSkyBox", "fs_PrecomputedAtmosphericScattering_LUT" });
GetShaderVariantCollections()->RegisterPragram(ProgramSingleScatteringRayMarching, { "vs_atmSkyBox", "fs_SingleScattering_RayMarching" });
GetShaderVariantCollections()->RegisterPragram(ProgramComputeTransmittance, { "cs_ComputeTransmittance" });
GetShaderVariantCollections()->RegisterPragram(ProgramComputeDirectIrradiance, { "cs_ComputeDirectIrradiance" });
GetShaderVariantCollections()->RegisterPragram(ProgramComputeSingleScattering, { "cs_ComputeSingleScattering" });
GetShaderVariantCollections()->RegisterPragram(ProgramComputeScatteringDensity, { "cs_ComputeScatteringDensity" });
GetShaderVariantCollections()->RegisterPragram(ProgramComputeIndirectIrradiance, { "cs_ComputeIndirectIrradiance" });
GetShaderVariantCollections()->RegisterPragram(ProgramComputeMultipleScattering, { "cs_ComputeMultipleScattering" });
GetShaderVariantCollections()->RegisterNonUberShader(ProgramAtmosphericScatteringLUT, { "vs_atmSkyBox", "fs_PrecomputedAtmosphericScattering_LUT" });
GetShaderVariantCollections()->RegisterNonUberShader(ProgramSingleScatteringRayMarching, { "vs_atmSkyBox", "fs_SingleScattering_RayMarching" });
GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeTransmittance, { "cs_ComputeTransmittance" });
GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeDirectIrradiance, { "cs_ComputeDirectIrradiance" });
GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeSingleScattering, { "cs_ComputeSingleScattering" });
GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeScatteringDensity, { "cs_ComputeScatteringDensity" });
GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeIndirectIrradiance, { "cs_ComputeIndirectIrradiance" });
GetShaderVariantCollections()->RegisterNonUberShader(ProgramComputeMultipleScattering, { "cs_ComputeMultipleScattering" });

bgfx::setViewName(GetViewID(), "PBRSkyRenderer");
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/PostProcessRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace engine

void PostProcessRenderer::Init()
{
GetShaderVariantCollections()->RegisterPragram("PostProcessProgram", {"vs_fullscreen","fs_PBR_postProcessing"});
GetShaderVariantCollections()->RegisterNonUberShader("PostProcessProgram", {"vs_fullscreen","fs_PBR_postProcessing"});

bgfx::setViewName(GetViewID(), "PostProcessRenderer");
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Runtime/Rendering/ShaderFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum class ShaderFeature : uint32_t
COUNT,
};

// These names are used as macro definition symbols in shaders.
constexpr const char* ShaderFeatureNames[] =
{
"", // Use empty string to represent default shader option in the name so we can reuse non-uber built shader.
Expand All @@ -48,6 +49,5 @@ CD_FORCEINLINE constexpr const char* GetFeatureName(ShaderFeature feature)
}

using ShaderFeatureSet = std::set<ShaderFeature>;
using ShaderBlob = std::vector<std::byte>;

}
Loading

0 comments on commit 7479561

Please sign in to comment.