I’m trying to implement the SMAA post-process in Urho3D (D3D11); but it’s not working (at least not fully).
The shader itself is from here
https://github.com/iryoku/smaa/blob/master/SMAA.hlsl
So I created a PostProcess xml file:
(PostProcess/SMAA.xml)
<renderpath> <rendertarget name="EdgesTex" sizedivisor="1 1" format="rgba" filter="true" /> <rendertarget name="BlendTex" sizedivisor="1 1" format="rgba" filter="true" /> <command type="clear" color="0 0 0 0" output="EdgesTex" /> <command type="clear" color="0 0 0 0" output="BlendTex" /> <command type="quad" vs="SMAA" ps="SMAA" vsdefines="_EDGE_DETECTION" psdefines="_EDGE_DETECTION" output="EdgesTex"> <parameter name="SMAAMetricSize" value="1280.0 720.0" /> <texture unit="diffuse" name="viewport" /> </command> <command type="quad" vs="SMAA" ps="SMAA" vsdefines="_BLEND_WEIGHT" psdefines="_BLEND_WEIGHT" output="BlendTex"> <parameter name="SMAAMetricSize" value="1280.0 720.0" /> <texture unit="diffuse" name="EdgesTex" /> <texture unit="normal" name="Textures/SMAA_Area.bmp" /> <texture unit="specular" name="Textures/SMAA_Search.bmp" /> </command> <command type="quad" vs="SMAA" ps="SMAA" vsdefines="_NEIGHBORHOOD_BLEND" psdefines="_NEIGHBORHOOD_BLEND" output="viewport"> <parameter name="SMAAMetricSize" value="1280.0 720.0" /> <texture unit="diffuse" name="viewport" /> <texture unit="normal" name="BlendTex" /> </command> <command type="quad" vs="CopyFramebuffer" ps="CopyFramebuffer" output="viewport" > <texture unit="diffuse" name="BlendTex" /> </command> </renderpath>
Then the shader used by the Post Process:
(Shaders/SMAA.hlsl)
#include "Uniforms.hlsl" #include "Transform.hlsl" #include "Samplers.hlsl" #include "ScreenPos.hlsl" #include "PostProcess.hlsl" // ==================================================== // SMAA DEFINITIONS // ==================================================== #ifdef COMPILEVS cbuffer CustomVS : register(b6) { float2 cSMAAMetricSize; } #endif #ifdef COMPILEPS cbuffer CustomPS : register(b6) { float2 cSMAAMetricSize; } #endif #define SMAA_RT_METRICS float4(1.0 / cSMAAMetricSize.x, 1.0 / cSMAAMetricSize.y, cSMAAMetricSize.x, cSMAAMetricSize.y) #define SMAA_HLSL_4 1 #define SMAA_PRESET_HIGH // iryoku's SMAA implementation #include "SMAA.inc.hlsl" // ==================================================== // VERTEX SHADER // ==================================================== void VS(float4 iPos : POSITION, out float2 oScreenPos : TEXCOORD0, #if _EDGE_DETECTION out float4 oOffset[3] : TEXCOORD1, #endif #if _BLEND_WEIGHT out float2 oPixCoord : TEXCOORD1, out float4 oOffset[3] : TEXCOORD2, #endif #if _NEIGHBORHOOD_BLEND out float4 oOffset : TEXCOORD1, #endif out float4 oPos : OUTPOSITION) { float4x3 modelMatrix = iModelMatrix; float3 worldPos = GetWorldPos(modelMatrix); oPos = GetClipPos(worldPos); oScreenPos = GetScreenPosPreDiv(oPos); #if _EDGE_DETECTION SMAAEdgeDetectionVS(oScreenPos, oOffset); #endif #if _BLEND_WEIGHT SMAABlendingWeightCalculationVS(oScreenPos, oPixCoord, oOffset); #endif #if _NEIGHBORHOOD_BLEND SMAANeighborhoodBlendingVS(oScreenPos, oOffset); #endif } // ==================================================== // PIXEL SHADER // ==================================================== void PS(float4 iPos : SV_POSITION, float2 iScreenPos : TEXCOORD0, #if _EDGE_DETECTION float4 iOffset[3] : TEXCOORD1, #endif #if _BLEND_WEIGHT float2 iPixCoord : TEXCOORD1, float4 iOffset[3] : TEXCOORD2, #endif #if _NEIGHBORHOOD_BLEND float4 iOffset : TEXCOORD1, #endif out float4 oColor : OUTCOLOR0) { #if _EDGE_DETECTION float2 luma; // Generate the SMAA edgesTex // // tDiffMap = viewport luma = SMAALumaEdgeDetectionPS(iScreenPos, iOffset, tDiffMap); oColor = float4(luma, float2(0.0, 0.0)); #endif #if _BLEND_WEIGHT // Generate SMAA blendTex // // tDiffMap = edgesTex // tNormapMap = area texture // tSpecMap = search texture oColor = SMAABlendingWeightCalculationPS(iScreenPos, iPixCoord, iOffset, tDiffMap, tNormalMap, tSpecMap, 0); #endif #if _NEIGHBORHOOD_BLEND // Generate final result // // tDiffMap = viewport // tEmissiveMap = BlendTex oColor = SMAANeighborhoodBlendingPS(iScreenPos, iOffset, tDiffMap, tEmissiveMap); #endif }
The edge detection is working:
but the blending weight calculation and final blending is not.
I’m not experienced with shaders so any help is greatly appreciated.