CameraBlurFilter Metal shader

This commit is contained in:
Jack Andersen 2016-08-20 08:34:01 -10:00
parent 21fc28bc9b
commit 6cb956cced
1 changed files with 69 additions and 30 deletions

View File

@ -6,48 +6,87 @@ namespace urde
{ {
static const char* VS = static const char* VS =
"#version 330\n" "#include <metal_stdlib>\n"
BOO_GLSL_BINDING_HEAD "using namespace metal;\n"
"layout(location=0) in vec4 posIn;\n" "struct VertData\n"
"layout(location=1) in vec4 uvIn;\n"
"\n"
"UBINDING0 uniform ThermalHotUniform\n"
"{\n" "{\n"
" vec4 colorReg0;\n" " float4 posIn [[ attribute(0) ]];\n"
" vec4 colorReg1;\n" " float4 uvIn [[ attribute(1) ]];\n"
" vec4 colorReg2;\n" "};\n"
"\n"
"struct CameraBlurUniform\n"
"{\n"
" float4 uv0;\n"
" float4 uv1;\n"
" float4 uv2;\n"
" float4 uv3;\n"
" float4 uv4;\n"
" float4 uv5;\n"
" float opacity;\n"
"};\n" "};\n"
"\n" "\n"
"struct VertToFrag\n" "struct VertToFrag\n"
"{\n" "{\n"
" vec2 sceneUv;\n" " float4 position [[ position ]];\n"
" float2 uvReg;\n"
" float2 uv0;\n"
" float2 uv1;\n"
" float2 uv2;\n"
" float2 uv3;\n"
" float2 uv4;\n"
" float2 uv5;\n"
" float opacity;\n"
"};\n" "};\n"
"\n" "\n"
"SBINDING(0) out VertToFrag vtf;\n" "vertex VertToFrag vmain(VertData v [[ stage_in ]], constant CameraBlurUniform& cbu [[ buffer(2) ]])\n"
"void main()\n"
"{\n" "{\n"
" vtf.sceneUv = uvIn.xy;\n" " VertToFrag vtf;\n"
" gl_Position = vec4(posIn.xyz, 1.0);\n" " vtf.uvReg = v.uvIn.xy;\n"
" vtf.uvReg.y = -vtf.uvReg.y;\n"
" vtf.uv0 = cbu.uv0.xy + v.uvIn.xy;\n"
" vtf.uv0.y = -vtf.uv0.y;\n"
" vtf.uv1 = cbu.uv1.xy + v.uvIn.xy;\n"
" vtf.uv1.y = -vtf.uv1.y;\n"
" vtf.uv2 = cbu.uv2.xy + v.uvIn.xy;\n"
" vtf.uv2.y = -vtf.uv2.y;\n"
" vtf.uv3 = cbu.uv3.xy + v.uvIn.xy;\n"
" vtf.uv3.y = -vtf.uv3.y;\n"
" vtf.uv4 = cbu.uv4.xy + v.uvIn.xy;\n"
" vtf.uv4.y = -vtf.uv4.y;\n"
" vtf.uv5 = cbu.uv5.xy + v.uvIn.xy;\n"
" vtf.uv5.y = -vtf.uv5.y;\n"
" vtf.opacity = cbu.opacity;\n"
" vtf.position = float4(v.posIn.xyz, 1.0);\n"
" return vtf;\n"
"}\n"; "}\n";
static const char* FS = static const char* FS =
"#version 330\n" "#include <metal_stdlib>\n"
BOO_GLSL_BINDING_HEAD "using namespace metal;\n"
"constexpr sampler samp(address::repeat, filter::linear);\n"
"struct VertToFrag\n" "struct VertToFrag\n"
"{\n" "{\n"
" vec2 sceneUv;\n" " float4 position [[ position ]];\n"
" float2 uvReg;\n"
" float2 uv0;\n"
" float2 uv1;\n"
" float2 uv2;\n"
" float2 uv3;\n"
" float2 uv4;\n"
" float2 uv5;\n"
" float opacity;\n"
"};\n" "};\n"
"\n" "\n"
"SBINDING(0) in VertToFrag vtf;\n" "fragment float4 fmain(VertToFrag vtf [[ stage_in ]], texture2d<float> sceneTex [[ texture(0) ]])\n"
"layout(location=0) out vec4 colorOut;\n"
"TBINDING0 uniform sampler2D sceneTex;\n"
"TBINDING1 uniform sampler2D paletteTex;\n"
"const vec4 kRGBToYPrime = vec4(0.299, 0.587, 0.114, 0.0);\n"
"void main()\n"
"{\n" "{\n"
" float sceneSample = dot(texture(sceneTex, vtf.sceneUv), kRGBToYPrime);\n" " float4 colorSample = sceneTex.sample(samp, vtf.uvReg) * 0.14285715;\n"
" vec4 colorSample = texture(paletteTex, vec2(sceneSample / 17.0, 0.5));\n" " colorSample += sceneTex.sample(samp, vtf.uv0) * 0.14285715;\n"
" colorOut = colorSample * sceneSample;\n" " colorSample += sceneTex.sample(samp, vtf.uv1) * 0.14285715;\n"
" colorSample += sceneTex.sample(samp, vtf.uv2) * 0.14285715;\n"
" colorSample += sceneTex.sample(samp, vtf.uv3) * 0.14285715;\n"
" colorSample += sceneTex.sample(samp, vtf.uv4) * 0.14285715;\n"
" colorSample += sceneTex.sample(samp, vtf.uv5) * 0.14285715;\n"
" return float4(colorSample.rgb, vtf.opacity);\n"
"}\n"; "}\n";
URDE_DECL_SPECIALIZE_SHADER(CCameraBlurFilter) URDE_DECL_SPECIALIZE_SHADER(CCameraBlurFilter)
@ -62,10 +101,10 @@ struct CCameraBlurFilterMetalDataBindingFactory : TShader<CCameraBlurFilter>::ID
boo::MetalDataFactory::Context& cctx = static_cast<boo::MetalDataFactory::Context&>(ctx); boo::MetalDataFactory::Context& cctx = static_cast<boo::MetalDataFactory::Context&>(ctx);
boo::IGraphicsBuffer* bufs[] = {filter.m_uniBuf}; boo::IGraphicsBuffer* bufs[] = {filter.m_uniBuf};
boo::ITexture* texs[] = {CGraphics::g_SpareTexture, g_Renderer->GetThermoPalette()}; boo::ITexture* texs[] = {CGraphics::g_SpareTexture};
return cctx.newShaderDataBinding(pipeline, vtxFmt, return cctx.newShaderDataBinding(pipeline, vtxFmt,
filter.m_vbo, nullptr, nullptr, 1, bufs, filter.m_vbo, nullptr, nullptr, 1, bufs,
nullptr, nullptr, nullptr, 2, texs); nullptr, nullptr, nullptr, 1, texs);
} }
}; };
@ -79,8 +118,8 @@ TShader<CCameraBlurFilter>::IDataBindingFactory* CCameraBlurFilter::Initialize(b
{nullptr, nullptr, boo::VertexSemantic::UV4} {nullptr, nullptr, boo::VertexSemantic::UV4}
}; };
vtxFmtOut = ctx.newVertexFormat(2, VtxVmt); vtxFmtOut = ctx.newVertexFormat(2, VtxVmt);
pipeOut = ctx.newShaderPipeline(VS, FS, vtxFmtOut, CGraphics::g_ViewportSamples, boo::BlendFactor::DstAlpha, pipeOut = ctx.newShaderPipeline(VS, FS, vtxFmtOut, CGraphics::g_ViewportSamples, boo::BlendFactor::SrcAlpha,
boo::BlendFactor::InvDstAlpha, boo::Primitive::TriStrips, false, false, false); boo::BlendFactor::InvSrcAlpha, boo::Primitive::TriStrips, false, false, false);
return new CCameraBlurFilterMetalDataBindingFactory; return new CCameraBlurFilterMetalDataBindingFactory;
} }