From 6cb956cced0e8fdd035792a500d6d8a260bdf4c8 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 20 Aug 2016 08:34:01 -1000 Subject: [PATCH] CameraBlurFilter Metal shader --- .../Shaders/CCameraBlurFilterMetal.cpp | 99 +++++++++++++------ 1 file changed, 69 insertions(+), 30 deletions(-) diff --git a/Runtime/Graphics/Shaders/CCameraBlurFilterMetal.cpp b/Runtime/Graphics/Shaders/CCameraBlurFilterMetal.cpp index 034d7ffb8..8928174ba 100644 --- a/Runtime/Graphics/Shaders/CCameraBlurFilterMetal.cpp +++ b/Runtime/Graphics/Shaders/CCameraBlurFilterMetal.cpp @@ -6,48 +6,87 @@ namespace urde { static const char* VS = -"#version 330\n" -BOO_GLSL_BINDING_HEAD -"layout(location=0) in vec4 posIn;\n" -"layout(location=1) in vec4 uvIn;\n" -"\n" -"UBINDING0 uniform ThermalHotUniform\n" +"#include \n" +"using namespace metal;\n" +"struct VertData\n" "{\n" -" vec4 colorReg0;\n" -" vec4 colorReg1;\n" -" vec4 colorReg2;\n" +" float4 posIn [[ attribute(0) ]];\n" +" float4 uvIn [[ attribute(1) ]];\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" "struct VertToFrag\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" -"SBINDING(0) out VertToFrag vtf;\n" -"void main()\n" +"vertex VertToFrag vmain(VertData v [[ stage_in ]], constant CameraBlurUniform& cbu [[ buffer(2) ]])\n" "{\n" -" vtf.sceneUv = uvIn.xy;\n" -" gl_Position = vec4(posIn.xyz, 1.0);\n" +" VertToFrag vtf;\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"; static const char* FS = -"#version 330\n" -BOO_GLSL_BINDING_HEAD +"#include \n" +"using namespace metal;\n" +"constexpr sampler samp(address::repeat, filter::linear);\n" "struct VertToFrag\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" -"SBINDING(0) in VertToFrag vtf;\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" +"fragment float4 fmain(VertToFrag vtf [[ stage_in ]], texture2d sceneTex [[ texture(0) ]])\n" "{\n" -" float sceneSample = dot(texture(sceneTex, vtf.sceneUv), kRGBToYPrime);\n" -" vec4 colorSample = texture(paletteTex, vec2(sceneSample / 17.0, 0.5));\n" -" colorOut = colorSample * sceneSample;\n" +" float4 colorSample = sceneTex.sample(samp, vtf.uvReg) * 0.14285715;\n" +" colorSample += sceneTex.sample(samp, vtf.uv0) * 0.14285715;\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"; URDE_DECL_SPECIALIZE_SHADER(CCameraBlurFilter) @@ -62,10 +101,10 @@ struct CCameraBlurFilterMetalDataBindingFactory : TShader::ID boo::MetalDataFactory::Context& cctx = static_cast(ctx); 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, filter.m_vbo, nullptr, nullptr, 1, bufs, - nullptr, nullptr, nullptr, 2, texs); + nullptr, nullptr, nullptr, 1, texs); } }; @@ -79,8 +118,8 @@ TShader::IDataBindingFactory* CCameraBlurFilter::Initialize(b {nullptr, nullptr, boo::VertexSemantic::UV4} }; vtxFmtOut = ctx.newVertexFormat(2, VtxVmt); - pipeOut = ctx.newShaderPipeline(VS, FS, vtxFmtOut, CGraphics::g_ViewportSamples, boo::BlendFactor::DstAlpha, - boo::BlendFactor::InvDstAlpha, boo::Primitive::TriStrips, false, false, false); + pipeOut = ctx.newShaderPipeline(VS, FS, vtxFmtOut, CGraphics::g_ViewportSamples, boo::BlendFactor::SrcAlpha, + boo::BlendFactor::InvSrcAlpha, boo::Primitive::TriStrips, false, false, false); return new CCameraBlurFilterMetalDataBindingFactory; }