metaforce/Runtime/Graphics/Shaders/CDecalShadersHLSL.cpp

216 lines
8.1 KiB
C++
Raw Normal View History

2018-03-17 03:41:01 +00:00
#include "CDecalShaders.hpp"
#include "Particle/CDecal.hpp"
#include "Graphics/CModel.hpp"
namespace urde
{
static const char* VS_HLSL_TEX =
"struct VertData\n"
"{\n"
" float4 posIn[4] : POSITION;\n"
" float4 colorIn : COLOR;\n"
" float4 uvsIn[4] : UV;\n"
"};\n"
"\n"
"cbuffer DecalUniform : register(b0)\n"
"{\n"
" float4x4 mvp;\n"
" float4 moduColor;\n"
"};\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float4 color : COLOR;\n"
" float2 uv : UV;\n"
"};\n"
"\n"
"VertToFrag main(in VertData v, in uint vertId : SV_VertexID)\n"
"{\n"
" VertToFrag vtf;\n"
" vtf.color = v.colorIn * moduColor;\n"
" vtf.uv = v.uvsIn[vertId].xy;\n"
" vtf.position = mul(mvp, v.posIn[vertId]);\n"
" return vtf;\n"
"}\n";
static const char* FS_HLSL_TEX =
"SamplerState samp : register(s0);\n"
"Texture2D tex0 : register(t0);\n"
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float4 color : COLOR;\n"
" float2 uv : UV;\n"
"};\n"
"\n"
"float4 main(in VertToFrag vtf) : SV_Target0\n"
"{\n"
" return vtf.color * tex0.Sample(samp, vtf.uv);\n"
"}\n";
static const char* FS_HLSL_TEX_REDTOALPHA =
"SamplerState samp : register(s0);\n"
"Texture2D tex0 : register(t0);\n"
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float4 color : COLOR;\n"
" float2 uv : UV;\n"
"};\n"
"\n"
"float4 main(in VertToFrag vtf) : SV_Target0\n"
"{\n"
" return float4(vtf.color.rgb, tex0.Sample(samp, vtf.uv).r);\n"
"}\n";
static const char* VS_HLSL_NOTEX =
"struct VertData\n"
"{\n"
" float4 posIn[4] : POSITION;\n"
" float4 colorIn : COLOR;\n"
"};\n"
"\n"
"cbuffer DecalUniform : register(b0)\n"
"{\n"
" float4x4 mvp;\n"
" float4 moduColor;\n"
"};\n"
"\n"
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float4 color : COLOR;\n"
"};\n"
"\n"
"VertToFrag main(in VertData v, in uint vertId : SV_VertexID)\n"
"{\n"
" VertToFrag vtf;\n"
" vtf.color = v.colorIn * moduColor;\n"
" vtf.position = mul(mvp, v.posIn[vertId]);\n"
" return vtf;\n"
"}\n";
static const char* FS_HLSL_NOTEX =
"struct VertToFrag\n"
"{\n"
" float4 position : SV_Position;\n"
" float4 color : COLOR;\n"
"};\n"
"\n"
"float4 main(in VertToFrag vtf) : SV_Target0\n"
"{\n"
" return vtf.color;\n"
"}\n";
2018-03-23 21:56:17 +00:00
struct D3DDecalDataBindingFactory : TShader<CDecalShaders>::IDataBindingFactory
2018-03-17 03:41:01 +00:00
{
boo::ObjToken<boo::IShaderDataBinding>
BuildShaderDataBinding(boo::IGraphicsDataFactory::Context& ctx,
2018-03-23 21:56:17 +00:00
CDecalShaders& shaders)
2018-03-17 03:41:01 +00:00
{
2018-03-23 21:56:17 +00:00
CQuadDecal& decal = shaders.m_decal;
const SQuadDescr* desc = decal.m_desc;
2018-03-17 03:41:01 +00:00
2018-03-23 21:56:17 +00:00
CUVElement* texr = desc->x14_TEX.get();
2018-03-17 03:41:01 +00:00
int texCount = 0;
2018-03-23 21:56:17 +00:00
boo::ObjToken<boo::ITexture> textures[1];
2018-03-17 03:41:01 +00:00
if (texr)
{
textures[0] = texr->GetValueTexture(0).GetObj()->GetBooTexture();
texCount = 1;
}
2018-03-23 21:56:17 +00:00
if (decal.m_instBuf)
2018-03-17 03:41:01 +00:00
{
2018-03-23 21:56:17 +00:00
boo::ObjToken<boo::IGraphicsBuffer> uniforms[] = {decal.m_uniformBuf.get()};
2018-03-17 03:41:01 +00:00
if (shaders.m_regPipeline)
2018-03-23 21:56:17 +00:00
decal.m_normalDataBind = ctx.newShaderDataBinding(shaders.m_regPipeline, nullptr, nullptr,
decal.m_instBuf.get(), nullptr, 1, uniforms,
2018-03-17 03:41:01 +00:00
nullptr, texCount, textures,
nullptr, nullptr);
if (shaders.m_redToAlphaPipeline)
2018-03-23 21:56:17 +00:00
decal.m_redToAlphaDataBind = ctx.newShaderDataBinding(shaders.m_redToAlphaPipeline, nullptr, nullptr,
decal.m_instBuf.get(), nullptr, 1, uniforms,
2018-03-17 03:41:01 +00:00
nullptr, texCount, textures,
nullptr, nullptr);
}
return nullptr;
}
};
TShader<CDecalShaders>::IDataBindingFactory* CDecalShaders::Initialize(boo::D3DDataFactory::Context& ctx)
2018-03-17 03:41:01 +00:00
{
static const boo::VertexElementDescriptor TexFmtTex[] =
{
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 0},
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 1},
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 2},
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 3},
{nullptr, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced},
{nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 0},
{nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 1},
{nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 2},
{nullptr, nullptr, boo::VertexSemantic::UV4 | boo::VertexSemantic::Instanced, 3}
};
m_vtxFormatTex = ctx.newVertexFormat(9, TexFmtTex);
static const boo::VertexElementDescriptor TexFmtNoTex[] =
{
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 0},
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 1},
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 2},
{nullptr, nullptr, boo::VertexSemantic::Position4 | boo::VertexSemantic::Instanced, 3},
{nullptr, nullptr, boo::VertexSemantic::Color | boo::VertexSemantic::Instanced}
};
m_vtxFormatNoTex = ctx.newVertexFormat(5, TexFmtNoTex);
m_texZTestNoZWrite = ctx.newShaderPipeline(VS_HLSL_TEX, FS_HLSL_TEX, nullptr, nullptr,
nullptr, m_vtxFormatTex,
boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha,
boo::Primitive::TriStrips, boo::ZTest::LEqual, false,
true, false, boo::CullMode::None);
m_texAdditiveZTest = ctx.newShaderPipeline(VS_HLSL_TEX, FS_HLSL_TEX, nullptr, nullptr,
nullptr, m_vtxFormatTex,
boo::BlendFactor::SrcAlpha, boo::BlendFactor::One,
boo::Primitive::TriStrips, boo::ZTest::LEqual, false,
true, false, boo::CullMode::None);
m_texRedToAlphaZTest = ctx.newShaderPipeline(VS_HLSL_TEX, FS_HLSL_TEX_REDTOALPHA, nullptr, nullptr,
nullptr, m_vtxFormatTex,
2018-03-23 21:56:17 +00:00
boo::BlendFactor::One, boo::BlendFactor::One,
2018-03-17 03:41:01 +00:00
boo::Primitive::TriStrips, boo::ZTest::LEqual, false,
2018-03-23 21:56:17 +00:00
true, true, boo::CullMode::None);
2018-03-17 03:41:01 +00:00
m_noTexZTestNoZWrite = ctx.newShaderPipeline(VS_HLSL_NOTEX, FS_HLSL_NOTEX, nullptr, nullptr,
nullptr, m_vtxFormatNoTex,
boo::BlendFactor::SrcAlpha, boo::BlendFactor::InvSrcAlpha,
boo::Primitive::TriStrips, boo::ZTest::LEqual, false,
true, false, boo::CullMode::None);
m_noTexAdditiveZTest = ctx.newShaderPipeline(VS_HLSL_NOTEX, FS_HLSL_NOTEX, nullptr, nullptr,
nullptr, m_vtxFormatNoTex,
boo::BlendFactor::SrcAlpha, boo::BlendFactor::One,
boo::Primitive::TriStrips, boo::ZTest::LEqual, true,
true, false, boo::CullMode::None);
2018-03-23 21:56:17 +00:00
return new struct D3DDecalDataBindingFactory;
2018-03-17 03:41:01 +00:00
}
template <>
void CDecalShaders::Shutdown<boo::D3DDataFactory>()
2018-03-17 03:41:01 +00:00
{
m_vtxFormatTex.reset();
m_vtxFormatNoTex.reset();
m_texZTestNoZWrite.reset();
m_texAdditiveZTest.reset();
m_texRedToAlphaZTest.reset();
m_noTexZTestNoZWrite.reset();
m_noTexAdditiveZTest.reset();
}
}