mirror of
https://github.com/AxioDL/metaforce.git
synced 2025-12-09 12:27:43 +00:00
Huge shader refactor
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
#include "CModelShaders.hpp"
|
||||
#include "Graphics/CLight.hpp"
|
||||
#include "hecl/Pipeline.hpp"
|
||||
|
||||
namespace urde
|
||||
{
|
||||
|
||||
std::experimental::optional<CModelShaders> CModelShaders::g_ModelShaders;
|
||||
std::unordered_map<uint64_t, CModelShaders::ShaderPipelines> CModelShaders::g_ShaderPipelines;
|
||||
|
||||
void CModelShaders::LightingUniform::ActivateLights(const std::vector<CLight>& lts)
|
||||
{
|
||||
@@ -53,59 +54,156 @@ void CModelShaders::LightingUniform::ActivateLights(const std::vector<CLight>& l
|
||||
}
|
||||
}
|
||||
|
||||
hecl::Runtime::ShaderCacheExtensions
|
||||
CModelShaders::GetShaderExtensions(boo::IGraphicsDataFactory::Platform plat)
|
||||
{
|
||||
switch (plat)
|
||||
{
|
||||
#if BOO_HAS_GL
|
||||
case boo::IGraphicsDataFactory::Platform::OpenGL:
|
||||
case boo::IGraphicsDataFactory::Platform::Vulkan:
|
||||
return GetShaderExtensionsGLSL(plat);
|
||||
#endif
|
||||
#if _WIN32
|
||||
case boo::IGraphicsDataFactory::Platform::D3D11:
|
||||
return GetShaderExtensionsHLSL(plat);
|
||||
#endif
|
||||
#if BOO_HAS_METAL
|
||||
case boo::IGraphicsDataFactory::Platform::Metal:
|
||||
return GetShaderExtensionsMetal(plat);
|
||||
#endif
|
||||
default:
|
||||
return {boo::IGraphicsDataFactory::Platform::Null};
|
||||
}
|
||||
}
|
||||
|
||||
const hecl::Backend::TextureInfo CModelShaders::ThermalTextures[] =
|
||||
static const hecl::Backend::TextureInfo ThermalTextures[] =
|
||||
{
|
||||
{hecl::Backend::TexGenSrc::Normal, 7, 0, 7, true}
|
||||
};
|
||||
|
||||
const hecl::Backend::TextureInfo CModelShaders::BallFadeTextures[] =
|
||||
static const hecl::Backend::TextureInfo BallFadeTextures[] =
|
||||
{
|
||||
{hecl::Backend::TexGenSrc::Position, 0, 0, 0, false}, // ID tex
|
||||
{hecl::Backend::TexGenSrc::Position, 1, 0, 0, false}, // Sphere ramp
|
||||
{hecl::Backend::TexGenSrc::Position, 2, 0, 1, false} // TXTR_BallFade
|
||||
};
|
||||
|
||||
const hecl::Backend::TextureInfo CModelShaders::WorldShadowTextures[] =
|
||||
static const hecl::Backend::TextureInfo WorldShadowTextures[] =
|
||||
{
|
||||
{hecl::Backend::TexGenSrc::Position, 7, 0, 7, false} // Shadow tex
|
||||
};
|
||||
|
||||
CModelShaders::CModelShaders(const hecl::Runtime::FileStoreManager& storeMgr,
|
||||
boo::IGraphicsDataFactory* gfxFactory)
|
||||
: m_shaderCache(storeMgr, gfxFactory, GetShaderExtensions(gfxFactory->platform())) {}
|
||||
|
||||
void CModelShaders::Initialize(const hecl::Runtime::FileStoreManager& storeMgr,
|
||||
boo::IGraphicsDataFactory* gfxFactory)
|
||||
static hecl::Backend::ExtensionSlot g_ExtensionSlots[] =
|
||||
{
|
||||
g_ModelShaders.emplace(storeMgr, gfxFactory);
|
||||
}
|
||||
/* Default solid shading */
|
||||
{},
|
||||
/* Normal lit shading */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::Original,
|
||||
hecl::Backend::BlendFactor::Original, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::Backface, false, false, true},
|
||||
/* Thermal Visor shading */
|
||||
{1, ThermalTextures, hecl::Backend::BlendFactor::One,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::Backface, false, false, false, true},
|
||||
/* Forced alpha shading */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::InvSrcAlpha, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::Backface, false, false, true},
|
||||
/* Forced additive shading */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::Backface, true, false, true},
|
||||
/* Solid color */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::One,
|
||||
hecl::Backend::BlendFactor::Zero, hecl::Backend::ZTest::LEqual,
|
||||
hecl::Backend::CullMode::Backface, false, false, false},
|
||||
/* Solid color additive */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::LEqual,
|
||||
hecl::Backend::CullMode::Backface, true, false, true},
|
||||
/* Alpha-only Solid color frontface cull, LEqual */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::Zero,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::LEqual,
|
||||
hecl::Backend::CullMode::Frontface, false, true, false},
|
||||
/* Alpha-only Solid color frontface cull, Always, No Z-write */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::Zero,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::None,
|
||||
hecl::Backend::CullMode::Frontface, true, true, false},
|
||||
/* Alpha-only Solid color backface cull, LEqual */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::Zero,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::LEqual,
|
||||
hecl::Backend::CullMode::Backface, false, true, false},
|
||||
/* Alpha-only Solid color backface cull, Greater, No Z-write */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::Zero,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::Greater,
|
||||
hecl::Backend::CullMode::Backface, true, true, false},
|
||||
/* MorphBall shadow shading */
|
||||
{3, BallFadeTextures,
|
||||
hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::InvSrcAlpha,
|
||||
hecl::Backend::ZTest::Equal,
|
||||
hecl::Backend::CullMode::Backface, false, false, true, false, true},
|
||||
/* World shadow shading (modified lighting) */
|
||||
{1, WorldShadowTextures, hecl::Backend::BlendFactor::Original,
|
||||
hecl::Backend::BlendFactor::Original, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::Backface, false, false, true},
|
||||
/* Forced alpha shading without culling */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::InvSrcAlpha, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::None, false, false, true},
|
||||
/* Forced additive shading without culling */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::None, false, false, true},
|
||||
/* Forced alpha shading without Z-write */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::InvSrcAlpha, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::Original, true, false, true},
|
||||
/* Forced additive shading without Z-write */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::Original, true, false, true},
|
||||
/* Forced alpha shading without culling or Z-write */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::InvSrcAlpha, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::None, true, false, true},
|
||||
/* Forced additive shading without culling or Z-write */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::One, hecl::Backend::ZTest::Original,
|
||||
hecl::Backend::CullMode::None, true, false, true},
|
||||
/* Depth GEqual no Z-write */
|
||||
{0, nullptr, hecl::Backend::BlendFactor::SrcAlpha,
|
||||
hecl::Backend::BlendFactor::InvSrcAlpha, hecl::Backend::ZTest::GEqual,
|
||||
hecl::Backend::CullMode::Backface, true, false, true}
|
||||
};
|
||||
|
||||
extern const hecl::Backend::Function ExtensionLightingFuncsGLSL[];
|
||||
extern const hecl::Backend::Function ExtensionPostFuncsGLSL[];
|
||||
extern const hecl::Backend::Function ExtensionLightingFuncsHLSL[];
|
||||
extern const hecl::Backend::Function ExtensionPostFuncsHLSL[];
|
||||
extern const hecl::Backend::Function ExtensionLightingFuncsMetal[];
|
||||
extern const hecl::Backend::Function ExtensionPostFuncsMetal[];
|
||||
|
||||
void CModelShaders::Initialize()
|
||||
{
|
||||
const hecl::Backend::Function* lightingFuncs;
|
||||
const hecl::Backend::Function* postFuncs;
|
||||
switch (CGraphics::g_BooPlatform)
|
||||
{
|
||||
case boo::IGraphicsDataFactory::Platform::OpenGL:
|
||||
case boo::IGraphicsDataFactory::Platform::Vulkan:
|
||||
case boo::IGraphicsDataFactory::Platform::NX:
|
||||
default:
|
||||
lightingFuncs = ExtensionLightingFuncsGLSL;
|
||||
postFuncs = ExtensionPostFuncsGLSL;
|
||||
case boo::IGraphicsDataFactory::Platform::D3D11:
|
||||
lightingFuncs = ExtensionLightingFuncsHLSL;
|
||||
postFuncs = ExtensionPostFuncsHLSL;
|
||||
case boo::IGraphicsDataFactory::Platform::Metal:
|
||||
lightingFuncs = ExtensionLightingFuncsMetal;
|
||||
postFuncs = ExtensionPostFuncsMetal;
|
||||
}
|
||||
for (auto& ext : g_ExtensionSlots)
|
||||
{
|
||||
ext.lighting = *lightingFuncs++;
|
||||
ext.post = *postFuncs++;
|
||||
}
|
||||
}
|
||||
|
||||
void CModelShaders::Shutdown()
|
||||
{
|
||||
g_ModelShaders = std::experimental::nullopt;
|
||||
g_ShaderPipelines.clear();
|
||||
}
|
||||
|
||||
CModelShaders::ShaderPipelines CModelShaders::BuildExtendedShader(const hecl::Backend::ShaderTag& tag,
|
||||
const hecl::Frontend::IR& ir)
|
||||
{
|
||||
auto search = g_ShaderPipelines.find(ir.m_hash);
|
||||
if (search != g_ShaderPipelines.cend())
|
||||
return search->second;
|
||||
ShaderPipelines& newPipelines = g_ShaderPipelines[ir.m_hash];
|
||||
newPipelines = std::make_shared<ShaderPipelinesData>();
|
||||
int idx = 0;
|
||||
for (const auto& ext : g_ExtensionSlots)
|
||||
(*newPipelines)[idx++] = hecl::conv->convert(hecl::HECLIR(ir, tag, ext));
|
||||
return newPipelines;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user