From 0562e2bf2bcb5cc134d0c9937c6fa1ce3bde1803 Mon Sep 17 00:00:00 2001 From: kimnuri Date: Fri, 19 Dec 2025 04:02:17 +0900 Subject: [PATCH] [add] MB DownSAmpler --- .../ShaderFiles/Shader_Deferred.hlsl | 307 ++++++++++-------- .../ShaderFiles/Shader_Deferred.hlsl | 162 +++++---- .../Engine/Private/RenderTarget_Manager.cpp | 11 + Jusin154_Team3D/Engine/Private/Renderer.cpp | 42 +-- .../Engine/Private/Renderer_Declare.cpp | 38 +-- Jusin154_Team3D/Engine/Public/Engine_Enum.h | 2 +- Jusin154_Team3D/Engine/Public/Renderer.h | 1 + 7 files changed, 296 insertions(+), 267 deletions(-) diff --git a/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_Deferred.hlsl b/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_Deferred.hlsl index 3ef93b2c9..37e606277 100644 --- a/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_Deferred.hlsl +++ b/Jusin154_Team3D/Editor/Bin/Resources/ShaderFiles/Shader_Deferred.hlsl @@ -17,6 +17,7 @@ float g_fCascadeSplitRatioFar; uint g_iMaxShadowWidth; uint g_iMaxShadowHeight; float2 g_vResolution; +float2 g_vSrcResolution; float4 g_vShadowBias; uint g_iBloomEmbossingPass; @@ -78,6 +79,7 @@ int g_iMBSampleCount; int g_iMBMaxSampleCount; float g_fMBBlurRadius; int g_iMBType; +int g_iMBTileSize; float g_fMBSampleBias; vector g_vLightDiffuse; @@ -151,6 +153,10 @@ struct PS_OUT_VELOCITYBLUR float4 vColor : SV_TARGET0; float2 vVelocity : SV_TARGET1; }; +struct PS_OUT_VELOCITYTILE +{ + float2 vVelocity : SV_TARGET0; +}; VS_OUT VS_MAIN(VS_IN In) { VS_OUT Out; @@ -237,151 +243,181 @@ struct PS_OUT_SSAO_BLUR // g_iMBSampleCount // g_iMBMaxSampleCount // g_iMBType -PS_OUT_VELOCITYBLUR PS_MOTIONBLURX(PS_IN In) +PS_OUT_VELOCITYBLUR PS_MOTIONBLUR(PS_IN In) { PS_OUT_VELOCITYBLUR Out = (PS_OUT_VELOCITYBLUR) 0; - float2 vSumVelocity = float2(0.f, 0.f); - float4 vSumColor = float4(0.f, 0.f, 0.f, 0.f); - float4 vSampledColor = float4(0.f, 0.f, 0.f, 0.f); - float fSampledDepth = 1.f; - float fWeight = 0.f; - float2 vSampledVelocityUV; - float2 vSampledVelocityPixels; - float2 vDestUV; - float fSampleSpreadLength; - - float2 uv = In.vTexcoord; - float fCenterDepth = g_DepthTexture.Sample(PointSampler, uv).y; // 해당 셀의 깊이 - float2 vCenterVelocityUV = (g_VelocityTexture.Sample(PointSampler, uv).xy * 2.f - 1.f); - float2 vCenterVelocityPixels = vCenterVelocityUV * g_vResolution.xy; // UV -> 픽셀 - float fCenterSpreadLength = clamp(abs(vCenterVelocityPixels.x), 0.0f, g_fMBBlurRadius); - float2 vSrcTexelSize = float2(1.f / g_vResolution.x, 1.f / g_vResolution.y); // 소스 셀 사이즈 g_vResolution-> 현재는 colortexture 해상도 - int iMaxMotionBlurSampleCount = min(g_iMBSampleCount, g_iMBMaxSampleCount); /* 최대 속도 */ - float fPixelToSampleUnitsScale = (float) iMaxMotionBlurSampleCount / max(g_fMBBlurRadius, FLT_EPSILON5); - float fDepthScale = 0.5f / max(g_fMBSampleBias, FLT_EPSILON5); - float fSampleStepPixels = g_fMBBlurRadius / max((float) iMaxMotionBlurSampleCount, 1.0f); - - [loop] - for (int i = -iMaxMotionBlurSampleCount; i <= iMaxMotionBlurSampleCount; ++i) - { - // 대상이 되는 셀은 센터uv + 임의값*셀사이즈*갯수 - vDestUV.x = uv.x + (i * vSrcTexelSize.x) * fSampleStepPixels; - vDestUV.y = uv.y; - - // [UVTest] - if (false == IsValidUV(vDestUV)) - { // UV밖에서 샘플링 해오려 하면 스킵 - continue; - } - - fSampledDepth = g_DepthTexture.Sample(PointSampler, vDestUV).y; - vSampledVelocityUV = (g_VelocityTexture.Sample(PointSampler, vDestUV).xy * 2.f - 1.f); - vSampledVelocityPixels = vSampledVelocityUV * g_vResolution.xy; - fSampleSpreadLength = clamp(abs(vSampledVelocityPixels.x), 0.0f, g_fMBBlurRadius); - - // [Sample] - float fWeightCalculated = SampleWeight(fCenterDepth, fSampledDepth, float(abs(i)), fCenterSpreadLength, fSampleSpreadLength, fPixelToSampleUnitsScale, fDepthScale); - if (fWeightCalculated <= 0.0f) - { - continue; - } - vSampledColor = g_ColorTexture.Sample(ClampLinearSampler, vDestUV); - vSumColor += vSampledColor * fWeightCalculated; - vSumVelocity += vSampledVelocityUV * fWeightCalculated; - fWeight += fWeightCalculated; - } - - float4 vBlurColor = vSumColor / max(fWeight, FLT_EPSILON5); - float2 vBlurVelocity = vSumVelocity / max(fWeight, FLT_EPSILON5); + float2 centerUV = In.vTexcoord; - float fTapCount = (float) (2 * iMaxMotionBlurSampleCount + 1); - float fCoverage = saturate(fWeight / max(fTapCount, 1.0f)); - - //Out.vColor = float4(vBlurColor.rgb, fCoverage); - Out.vColor = vBlurColor; - Out.vVelocity = vBlurVelocity * 0.5f + 0.5f; - float2 vBlurVelocityPixels = vBlurVelocity * g_vResolution.xy; - if (length(vBlurVelocityPixels) < 0.5f) // 0.5px 이하면 정지 취급 + float4 centerColor = g_ColorTexture.Sample(ClampLinearSampler, centerUV); + float centerDepth = g_DepthTexture.Sample(PointSampler, centerUV).y; + + // encoded(0..1) -> signed(-1..1) + float2 centerVelocitySignedUV = g_VelocityTexture.Sample(PointSampler, centerUV).xy * 2.0f - 1.0f; + + // UV -> pixels + float2 centerVelocityPixels = centerVelocitySignedUV * g_vResolution.xy; + float centerSpeedPixels = length(centerVelocityPixels); + + // **정지 데드존** + if (centerSpeedPixels < 0.5f) { - Out.vColor = g_ColorTexture.Sample(ClampLinearSampler, uv); + Out.vColor = centerColor; Out.vVelocity = float2(0.5f, 0.5f); return Out; } + // **방향(정규화)** + float2 velocityDirectionPixels = centerVelocityPixels / max(centerSpeedPixels, FLT_EPSILON5); + + // **최대 블러 반경(픽셀)** + float blurRadiusPixels = min(centerSpeedPixels, g_fMBBlurRadius); + + int maximumSampleCount = min(g_iMBSampleCount, g_iMBMaxSampleCount); + int tapRadius = min(maximumSampleCount, (int) ceil(blurRadiusPixels)); // 옵션: 속도에 따라 탭 줄이기 + + float sampleStepPixels = blurRadiusPixels / max((float) tapRadius, 1.0f); + + // SpreadCompare용 스케일 (픽셀 -> 샘플단위) + float pixelToSampleUnitsScale = (float) tapRadius / max(blurRadiusPixels, FLT_EPSILON5); + + float depthScale = 0.5f / max(g_fMBSampleBias, FLT_EPSILON5); + + float4 weightedColorSum = float4(0, 0, 0, 0); + float2 weightedVelocitySum = float2(0, 0); + float weightSum = 0.0f; + + // 픽셀 오프셋 -> UV 오프셋 + float2 pixelToUV = 1.0f / g_vResolution.xy; + + // center spread는 "반경"으로 잡는 편이 안정적 + float centerSpreadLength = blurRadiusPixels; + + [loop] + for (int i = -tapRadius; i <= tapRadius; ++i) + { + float offsetLengthSamples = (float) abs(i); + + float2 offsetPixels = velocityDirectionPixels * ((float) i * sampleStepPixels); + float2 sampleUV = centerUV + offsetPixels * pixelToUV; + + // Clamp 샘플러를 쓰면 굳이 IsValidUV가 필수는 아니지만, 남겨도 됩니다. + if (false == IsValidUV(sampleUV)) + continue; + + float sampleDepth = g_DepthTexture.Sample(PointSampler, sampleUV).y; + + float2 sampleVelocitySignedUV = g_VelocityTexture.Sample(PointSampler, sampleUV).xy * 2.0f - 1.0f; + float2 sampleVelocityPixels = sampleVelocitySignedUV * g_vResolution.xy; + + float sampleSpreadLength = clamp(length(sampleVelocityPixels), 0.0f, g_fMBBlurRadius); + + float weightCalculated = SampleWeight( + centerDepth, sampleDepth, + offsetLengthSamples, + centerSpreadLength, sampleSpreadLength, + pixelToSampleUnitsScale, + depthScale + ); + + if (weightCalculated <= 0.0f) + continue; + + float4 sampleColor = g_ColorTexture.Sample(ClampLinearSampler, sampleUV); + + weightedColorSum += sampleColor * weightCalculated; + weightedVelocitySum += sampleVelocitySignedUV * weightCalculated; + weightSum += weightCalculated; + } + + float4 blurColor = weightedColorSum / max(weightSum, FLT_EPSILON5); + float2 blurVelocitySignedUV = weightedVelocitySum / max(weightSum, FLT_EPSILON5); + + Out.vColor = blurColor; + Out.vVelocity = blurVelocitySignedUV * 0.5f + 0.5f; // 다시 encode + return Out; } -PS_OUT_VELOCITYBLUR PS_MOTIONBLURY(PS_IN In) +PS_OUT_VELOCITYTILE PS_MOTIONBLUR_TILESAMPLE(PS_IN In) { - PS_OUT_VELOCITYBLUR Out = (PS_OUT_VELOCITYBLUR) 0; - float2 vSumVelocity = float2(0.f, 0.f); - float4 vSumColor = float4(0.f, 0.f, 0.f, 0.f); - float4 vSampledColor = float4(0.f, 0.f, 0.f, 0.f); - float fSampledDepth = 1.f; - float fWeight = 0.f; - float2 vSampledVelocityPixels; - float2 vSampledVelocityUV; - float2 vDestUV; - float fSampleSpreadLength; + PS_OUT_VELOCITYTILE Out = (PS_OUT_VELOCITYTILE) 0; float2 uv = In.vTexcoord; - float fCenterDepth = g_DepthTexture.Sample(PointSampler, uv).y; // 해당 셀의 깊이 - float2 vCenterVelocityUV = (g_VelocityTexture.Sample(PointSampler, uv).xy * 2.f - 1.f); - float2 vCenterVelocityPixels = vCenterVelocityUV * g_vResolution.xy; // UV -> 픽셀 - float fCenterSpreadLength = clamp(abs(vCenterVelocityPixels.y), 0.0f, g_fMBBlurRadius); - float2 vSrcTexelSize = float2(1.f / g_vResolution.x, 1.f / g_vResolution.y); // 소스 셀 사이즈 g_vResolution-> 현재는 colortexture 해상도 - int iMaxMotionBlurSampleCount = min(g_iMBSampleCount, 7); /* 최대 속도 */ + float2 vSrcTexelSize = float2(1.f / g_vSrcResolution.x, 1.f / g_vSrcResolution.y); + + int2 vTilePixelIndex = int2(uv * g_vResolution); + vTilePixelIndex = clamp(vTilePixelIndex, int2(0, 0), int2(g_vResolution) - 1); + + int2 vTileStartSourcePixel = vTilePixelIndex * g_iMBTileSize; + + float2 vMaxVel = float2(0.5f, 0.5f); + float fMaxSpeed = 0.f; + float2 vSampleVel; + float2 vSampleUV; + float2 vSampleDiff; + float fSampleSpeed; - float fPixelToSampleUnitsScale = (float) iMaxMotionBlurSampleCount / max(g_fMBBlurRadius, FLT_EPSILON5); - float fDepthScale = 0.5f / max(g_fMBSampleBias, FLT_EPSILON5); - float fSampleStepPixels = g_fMBBlurRadius / max((float) iMaxMotionBlurSampleCount, 1.0f); - [loop] - for (int i = -iMaxMotionBlurSampleCount; i <= iMaxMotionBlurSampleCount; ++i) + for (int iY = 0; iY < g_iMBTileSize; ++iY) { - // 대상이 되는 셀은 센터uv + 임의값*셀사이즈*갯수 - vDestUV.x = uv.x; - vDestUV.y = uv.y + (i * vSrcTexelSize.y) * fSampleStepPixels; - - // [UVTest] - if (false == IsValidUV(vDestUV)) - { // UV밖에서 샘플링 해오려 하면 스킵 - continue; + [loop] + for (int iX = 0; iX < g_iMBTileSize; ++iX) + { + int2 vSourcePixel = vTileStartSourcePixel + int2(iX, iY); + + if (vSourcePixel.x < 0 || vSourcePixel.y < 0 || vSourcePixel.x >= (int) g_vSrcResolution.x || vSourcePixel.y >= (int) g_vSrcResolution.y) + { + continue; + } + + float2 vSourceUV = (float2(vSourcePixel) + 0.5f) * vSrcTexelSize; + + vSampleVel = g_VelocityTexture.SampleLevel(ClampSampler, vSourceUV, 0).xy; + + vSampleUV = vSampleVel * 2.0f - 1.0f; + + fSampleSpeed = length(vSampleUV * g_vSrcResolution); + + if (fSampleSpeed > fMaxSpeed) + { + fMaxSpeed = fSampleSpeed; + vMaxVel = vSampleVel; + } } - - fSampledDepth = g_DepthTexture.Sample(PointSampler, vDestUV).y; - vSampledVelocityUV = (g_VelocityTexture.Sample(PointSampler, vDestUV).xy * 2.f - 1.f); - vSampledVelocityPixels = vSampledVelocityUV * g_vResolution.xy; - fSampleSpreadLength = clamp(abs(vSampledVelocityPixels.y), 0.0f, g_fMBBlurRadius); - - // [Sample] - float fWeightCalculated = SampleWeight(fCenterDepth, fSampledDepth, float(abs(i)), fCenterSpreadLength, fSampleSpreadLength, fPixelToSampleUnitsScale, fDepthScale); - if (fWeightCalculated <= 0.0f) { - continue; - } - vSampledColor = g_ColorTexture.Sample(ClampLinearSampler, vDestUV); - vSumColor += vSampledColor * fWeightCalculated; - vSumVelocity += vSampledVelocityUV * fWeightCalculated; - fWeight += fWeightCalculated; } + + Out.vVelocity = vMaxVel; + return Out; +} +PS_OUT_VELOCITYTILE PS_MOTIONBLUR_TENTSAMPLE(PS_IN In) +{ + PS_OUT_VELOCITYTILE Out = (PS_OUT_VELOCITYTILE) 0; - float4 vBlurColor = vSumColor / max(fWeight, FLT_EPSILON5); - float2 vBlurVelocity = vSumVelocity / max(fWeight, FLT_EPSILON5); - - float fTapCount = (float) (2 * iMaxMotionBlurSampleCount + 1); - float fCoverage = saturate(fWeight / max(fTapCount, 1.0f)); - - //Out.vColor = float4(vBlurColor.rgb, fCoverage); - Out.vColor = vBlurColor; - Out.vVelocity = vBlurVelocity * 0.5f + 0.5f; - float2 vBlurVelocityPixels = vBlurVelocity * g_vResolution.xy; - if (length(vBlurVelocityPixels) < 0.5f) // 0.5px 이하면 정지 취급 + float2 uv = In.vTexcoord; + float2 vSrcTexelSize = float2(1.f / g_vResolution.x, 1.f / g_vResolution.y); + float2 vMaxVel = float2(0.5f, 0.5f); + float vMaxSpeed = 0.f; + float2 vSampleVel; + float2 vSampleUV; + float2 vSampleDiff; + float fSampleSpeed; + + for (int i = -1; i <= 1; ++i) { - Out.vColor = g_ColorTexture.Sample(ClampLinearSampler, uv); - Out.vVelocity = float2(0.5f, 0.5f); - return Out; + for (int j = -1; j <= 1; ++j) + { + vSampleUV.x = uv.x + vSrcTexelSize.x * i; + vSampleUV.y = uv.y + vSrcTexelSize.y * j; + vSampleVel = g_VelocityTexture.Sample(ClampSampler, vSampleUV); + vSampleDiff = vSampleVel *2.f - 1.f; + fSampleSpeed = length(vSampleDiff * g_vResolution * g_iMBTileSize); + if (fSampleSpeed > vMaxSpeed) { + vMaxSpeed = fSampleSpeed; + vMaxVel = vSampleVel; + } + } } - + Out.vVelocity = vMaxVel; return Out; } PS_OUT_LIGHT PS_MAIN_DIRECTIONAL(PS_IN In) @@ -931,10 +967,10 @@ PS_OUT_FLT4_SINGLE PS_MAIN_EMBOSS(PS_IN In) PS_OUT_FLT4_SINGLE Out; float2 uv = In.vTexcoord; float fSrcAlpha = g_DiffuseTexture.Sample(PointSampler, uv).a; - float2 vSrcTexelSize = float2(1.f / g_vResolution.x, 1.f / g_vResolution.y); + float2 vSrcTexelSize = float2(1.f / g_vSrcResolution.x, 1.f / g_vSrcResolution.y); float3 vColor = (0.f, 0.f, 0.f); - vColor = DownSampleFast(g_DiffuseTexture, In.vTexcoord, vSrcTexelSize, g_vResolution); + vColor = DownSampleFast(g_DiffuseTexture, In.vTexcoord, vSrcTexelSize, g_vSrcResolution); float fIntensity = dot(vColor, float3(0.2126f, 0.7152f, 0.0722f)); fIntensity = max(FLT_EPSILON3, fIntensity); @@ -952,10 +988,10 @@ PS_OUT_FLT4_SINGLE PS_MAIN_DOWNSAMPLE(PS_IN In) PS_OUT_FLT4_SINGLE Out; float2 uv = In.vTexcoord; float fSrcAlpha = g_DiffuseTexture.SampleLevel(BorderZeroSampler, uv, 0).a; - float2 vSrcTexelSize = float2(1.f / g_vResolution.x, 1.f / g_vResolution.y); + float2 vSrcTexelSize = float2(1.f / g_vSrcResolution.x, 1.f / g_vSrcResolution.y); float3 vColor = (0.f, 0.f, 0.f); - vColor = DownSampleFast(g_DiffuseTexture, In.vTexcoord, vSrcTexelSize, g_vResolution); + vColor = DownSampleFast(g_DiffuseTexture, In.vTexcoord, vSrcTexelSize, g_vSrcResolution); Out.vFirstTarget = float4(vColor, fSrcAlpha); @@ -1519,14 +1555,14 @@ technique11 DefaultTechnique PixelShader = compile ps_5_0 PS_SSAO_BLUR(); } - pass MotionBlurXPass // 18 + pass MotionBlurPass // 18 { SetRasterizerState(RS_Default); SetDepthStencilState(DSS_None, 0); SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff); VertexShader = compile vs_5_0 VS_MAIN(); GeometryShader = NULL; - PixelShader = compile ps_5_0 PS_MOTIONBLURX(); + PixelShader = compile ps_5_0 PS_MOTIONBLUR(); } pass DownSamplePass // 19 { @@ -1537,13 +1573,22 @@ technique11 DefaultTechnique GeometryShader = NULL; PixelShader = compile ps_5_0 PS_MAIN_DOWNSAMPLE(); } - pass MotionBlurYPass // 20 + pass MotionBlurTileSamplePass // 20 { SetRasterizerState(RS_Default); SetDepthStencilState(DSS_None, 0); SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff); VertexShader = compile vs_5_0 VS_MAIN(); GeometryShader = NULL; - PixelShader = compile ps_5_0 PS_MOTIONBLURY(); + PixelShader = compile ps_5_0 PS_MOTIONBLUR_TILESAMPLE(); + } + pass MotionBlurTentSamplePass // 21 + { + SetRasterizerState(RS_Default); + SetDepthStencilState(DSS_None, 0); + SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff); + VertexShader = compile vs_5_0 VS_MAIN(); + GeometryShader = NULL; + PixelShader = compile ps_5_0 PS_MOTIONBLUR_TENTSAMPLE(); } } diff --git a/Jusin154_Team3D/Engine/Bin/Resources/ShaderFiles/Shader_Deferred.hlsl b/Jusin154_Team3D/Engine/Bin/Resources/ShaderFiles/Shader_Deferred.hlsl index 3ef93b2c9..f36c5a3ae 100644 --- a/Jusin154_Team3D/Engine/Bin/Resources/ShaderFiles/Shader_Deferred.hlsl +++ b/Jusin154_Team3D/Engine/Bin/Resources/ShaderFiles/Shader_Deferred.hlsl @@ -17,6 +17,7 @@ float g_fCascadeSplitRatioFar; uint g_iMaxShadowWidth; uint g_iMaxShadowHeight; float2 g_vResolution; +float2 g_vSrcResolution; float4 g_vShadowBias; uint g_iBloomEmbossingPass; @@ -78,6 +79,7 @@ int g_iMBSampleCount; int g_iMBMaxSampleCount; float g_fMBBlurRadius; int g_iMBType; +int g_iMBTileSize; float g_fMBSampleBias; vector g_vLightDiffuse; @@ -151,6 +153,10 @@ struct PS_OUT_VELOCITYBLUR float4 vColor : SV_TARGET0; float2 vVelocity : SV_TARGET1; }; +struct PS_OUT_VELOCITYTILE +{ + float2 vVelocity : SV_TARGET0; +}; VS_OUT VS_MAIN(VS_IN In) { VS_OUT Out; @@ -237,7 +243,7 @@ struct PS_OUT_SSAO_BLUR // g_iMBSampleCount // g_iMBMaxSampleCount // g_iMBType -PS_OUT_VELOCITYBLUR PS_MOTIONBLURX(PS_IN In) +PS_OUT_VELOCITYBLUR PS_MOTIONBLUR(PS_IN In) { PS_OUT_VELOCITYBLUR Out = (PS_OUT_VELOCITYBLUR) 0; float2 vSumVelocity = float2(0.f, 0.f); @@ -311,77 +317,86 @@ PS_OUT_VELOCITYBLUR PS_MOTIONBLURX(PS_IN In) return Out; } -PS_OUT_VELOCITYBLUR PS_MOTIONBLURY(PS_IN In) +PS_OUT_VELOCITYTILE PS_MOTIONBLUR_TILESAMPLE(PS_IN In) { - PS_OUT_VELOCITYBLUR Out = (PS_OUT_VELOCITYBLUR) 0; - float2 vSumVelocity = float2(0.f, 0.f); - float4 vSumColor = float4(0.f, 0.f, 0.f, 0.f); - float4 vSampledColor = float4(0.f, 0.f, 0.f, 0.f); - float fSampledDepth = 1.f; - float fWeight = 0.f; - float2 vSampledVelocityPixels; - float2 vSampledVelocityUV; - float2 vDestUV; - float fSampleSpreadLength; + PS_OUT_VELOCITYTILE Out = (PS_OUT_VELOCITYTILE) 0; float2 uv = In.vTexcoord; - float fCenterDepth = g_DepthTexture.Sample(PointSampler, uv).y; // 해당 셀의 깊이 - float2 vCenterVelocityUV = (g_VelocityTexture.Sample(PointSampler, uv).xy * 2.f - 1.f); - float2 vCenterVelocityPixels = vCenterVelocityUV * g_vResolution.xy; // UV -> 픽셀 - float fCenterSpreadLength = clamp(abs(vCenterVelocityPixels.y), 0.0f, g_fMBBlurRadius); - float2 vSrcTexelSize = float2(1.f / g_vResolution.x, 1.f / g_vResolution.y); // 소스 셀 사이즈 g_vResolution-> 현재는 colortexture 해상도 - int iMaxMotionBlurSampleCount = min(g_iMBSampleCount, 7); /* 최대 속도 */ + float2 vSrcTexelSize = float2(1.f / g_vSrcResolution.x, 1.f / g_vSrcResolution.y); + + int2 vTilePixelIndex = int2(uv * g_vResolution); + vTilePixelIndex = clamp(vTilePixelIndex, int2(0, 0), int2(g_vResolution) - 1); + + int2 vTileStartSourcePixel = vTilePixelIndex * g_iMBTileSize; + + float2 vMaxVel = float2(0.5f, 0.5f); + float fMaxSpeed = 0.f; + float2 vSampleVel; + float2 vSampleUV; + float2 vSampleDiff; + float fSampleSpeed; - float fPixelToSampleUnitsScale = (float) iMaxMotionBlurSampleCount / max(g_fMBBlurRadius, FLT_EPSILON5); - float fDepthScale = 0.5f / max(g_fMBSampleBias, FLT_EPSILON5); - float fSampleStepPixels = g_fMBBlurRadius / max((float) iMaxMotionBlurSampleCount, 1.0f); - [loop] - for (int i = -iMaxMotionBlurSampleCount; i <= iMaxMotionBlurSampleCount; ++i) + for (int iY = 0; iY < g_iMBTileSize; ++iY) { - // 대상이 되는 셀은 센터uv + 임의값*셀사이즈*갯수 - vDestUV.x = uv.x; - vDestUV.y = uv.y + (i * vSrcTexelSize.y) * fSampleStepPixels; - - // [UVTest] - if (false == IsValidUV(vDestUV)) - { // UV밖에서 샘플링 해오려 하면 스킵 - continue; + [loop] + for (int iX = 0; iX < g_iMBTileSize; ++iX) + { + int2 vSourcePixel = vTileStartSourcePixel + int2(iX, iY); + + if (vSourcePixel.x < 0 || vSourcePixel.y < 0 || vSourcePixel.x >= (int) g_vSrcResolution.x || vSourcePixel.y >= (int) g_vSrcResolution.y) + { + continue; + } + + float2 vSourceUV = (float2(vSourcePixel) + 0.5f) * vSrcTexelSize; + + vSampleVel = g_VelocityTexture.SampleLevel(ClampSampler, vSourceUV, 0).xy; + + vSampleUV = vSampleVel * 2.0f - 1.0f; + + fSampleSpeed = length(vSampleUV * g_vSrcResolution); + + if (fSampleSpeed > fMaxSpeed) + { + fMaxSpeed = fSampleSpeed; + vMaxVel = vSampleVel; + } } - - fSampledDepth = g_DepthTexture.Sample(PointSampler, vDestUV).y; - vSampledVelocityUV = (g_VelocityTexture.Sample(PointSampler, vDestUV).xy * 2.f - 1.f); - vSampledVelocityPixels = vSampledVelocityUV * g_vResolution.xy; - fSampleSpreadLength = clamp(abs(vSampledVelocityPixels.y), 0.0f, g_fMBBlurRadius); - - // [Sample] - float fWeightCalculated = SampleWeight(fCenterDepth, fSampledDepth, float(abs(i)), fCenterSpreadLength, fSampleSpreadLength, fPixelToSampleUnitsScale, fDepthScale); - if (fWeightCalculated <= 0.0f) { - continue; - } - vSampledColor = g_ColorTexture.Sample(ClampLinearSampler, vDestUV); - vSumColor += vSampledColor * fWeightCalculated; - vSumVelocity += vSampledVelocityUV * fWeightCalculated; - fWeight += fWeightCalculated; } + + Out.vVelocity = vMaxVel; + return Out; +} +PS_OUT_VELOCITYTILE PS_MOTIONBLUR_TENTSAMPLE(PS_IN In) +{ + PS_OUT_VELOCITYTILE Out = (PS_OUT_VELOCITYTILE) 0; - float4 vBlurColor = vSumColor / max(fWeight, FLT_EPSILON5); - float2 vBlurVelocity = vSumVelocity / max(fWeight, FLT_EPSILON5); - - float fTapCount = (float) (2 * iMaxMotionBlurSampleCount + 1); - float fCoverage = saturate(fWeight / max(fTapCount, 1.0f)); - - //Out.vColor = float4(vBlurColor.rgb, fCoverage); - Out.vColor = vBlurColor; - Out.vVelocity = vBlurVelocity * 0.5f + 0.5f; - float2 vBlurVelocityPixels = vBlurVelocity * g_vResolution.xy; - if (length(vBlurVelocityPixels) < 0.5f) // 0.5px 이하면 정지 취급 + float2 uv = In.vTexcoord; + float2 vSrcTexelSize = float2(1.f / g_vResolution.x, 1.f / g_vResolution.y); + float2 vMaxVel = float2(0.5f, 0.5f); + float vMaxSpeed = 0.f; + float2 vSampleVel; + float2 vSampleUV; + float2 vSampleDiff; + float fSampleSpeed; + + for (int i = -1; i <= 1; ++i) { - Out.vColor = g_ColorTexture.Sample(ClampLinearSampler, uv); - Out.vVelocity = float2(0.5f, 0.5f); - return Out; + for (int j = -1; j <= 1; ++j) + { + vSampleUV.x = uv.x + vSrcTexelSize.x * i; + vSampleUV.y = uv.y + vSrcTexelSize.y * j; + vSampleVel = g_VelocityTexture.Sample(ClampSampler, vSampleUV); + vSampleDiff = vSampleVel *2.f - 1.f; + fSampleSpeed = length(vSampleDiff * g_vResolution * g_iMBTileSize); + if (fSampleSpeed > vMaxSpeed) { + vMaxSpeed = fSampleSpeed; + vMaxVel = vSampleVel; + } + } } - + Out.vVelocity = vMaxVel; return Out; } PS_OUT_LIGHT PS_MAIN_DIRECTIONAL(PS_IN In) @@ -931,10 +946,10 @@ PS_OUT_FLT4_SINGLE PS_MAIN_EMBOSS(PS_IN In) PS_OUT_FLT4_SINGLE Out; float2 uv = In.vTexcoord; float fSrcAlpha = g_DiffuseTexture.Sample(PointSampler, uv).a; - float2 vSrcTexelSize = float2(1.f / g_vResolution.x, 1.f / g_vResolution.y); + float2 vSrcTexelSize = float2(1.f / g_vSrcResolution.x, 1.f / g_vSrcResolution.y); float3 vColor = (0.f, 0.f, 0.f); - vColor = DownSampleFast(g_DiffuseTexture, In.vTexcoord, vSrcTexelSize, g_vResolution); + vColor = DownSampleFast(g_DiffuseTexture, In.vTexcoord, vSrcTexelSize, g_vSrcResolution); float fIntensity = dot(vColor, float3(0.2126f, 0.7152f, 0.0722f)); fIntensity = max(FLT_EPSILON3, fIntensity); @@ -952,10 +967,10 @@ PS_OUT_FLT4_SINGLE PS_MAIN_DOWNSAMPLE(PS_IN In) PS_OUT_FLT4_SINGLE Out; float2 uv = In.vTexcoord; float fSrcAlpha = g_DiffuseTexture.SampleLevel(BorderZeroSampler, uv, 0).a; - float2 vSrcTexelSize = float2(1.f / g_vResolution.x, 1.f / g_vResolution.y); + float2 vSrcTexelSize = float2(1.f / g_vSrcResolution.x, 1.f / g_vSrcResolution.y); float3 vColor = (0.f, 0.f, 0.f); - vColor = DownSampleFast(g_DiffuseTexture, In.vTexcoord, vSrcTexelSize, g_vResolution); + vColor = DownSampleFast(g_DiffuseTexture, In.vTexcoord, vSrcTexelSize, g_vSrcResolution); Out.vFirstTarget = float4(vColor, fSrcAlpha); @@ -1519,14 +1534,14 @@ technique11 DefaultTechnique PixelShader = compile ps_5_0 PS_SSAO_BLUR(); } - pass MotionBlurXPass // 18 + pass MotionBlurPass // 18 { SetRasterizerState(RS_Default); SetDepthStencilState(DSS_None, 0); SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff); VertexShader = compile vs_5_0 VS_MAIN(); GeometryShader = NULL; - PixelShader = compile ps_5_0 PS_MOTIONBLURX(); + PixelShader = compile ps_5_0 PS_MOTIONBLUR(); } pass DownSamplePass // 19 { @@ -1537,13 +1552,22 @@ technique11 DefaultTechnique GeometryShader = NULL; PixelShader = compile ps_5_0 PS_MAIN_DOWNSAMPLE(); } - pass MotionBlurYPass // 20 + pass MotionBlurTileSamplePass // 20 { SetRasterizerState(RS_Default); SetDepthStencilState(DSS_None, 0); SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff); VertexShader = compile vs_5_0 VS_MAIN(); GeometryShader = NULL; - PixelShader = compile ps_5_0 PS_MOTIONBLURY(); + PixelShader = compile ps_5_0 PS_MOTIONBLUR_TILESAMPLE(); + } + pass MotionBlurTentSamplePass // 21 + { + SetRasterizerState(RS_Default); + SetDepthStencilState(DSS_None, 0); + SetBlendState(BS_None, float4(0.f, 0.f, 0.f, 0.f), 0xffffffff); + VertexShader = compile vs_5_0 VS_MAIN(); + GeometryShader = NULL; + PixelShader = compile ps_5_0 PS_MOTIONBLUR_TENTSAMPLE(); } } diff --git a/Jusin154_Team3D/Engine/Private/RenderTarget_Manager.cpp b/Jusin154_Team3D/Engine/Private/RenderTarget_Manager.cpp index 47120bae7..cdbec8ce2 100644 --- a/Jusin154_Team3D/Engine/Private/RenderTarget_Manager.cpp +++ b/Jusin154_Team3D/Engine/Private/RenderTarget_Manager.cpp @@ -360,6 +360,11 @@ HRESULT CRenderTarget_Manager::Refit_RenderTarget(CVIBuffer_Rect* pVIBuffer, CSh assert(false); } + vResolution = { vp.Width, vp.Height }; + if (FAILED(pShader->Bind_RawValue("g_vSrcResolution", &vResolution, sizeof(_float2)))) { + assert(false); + } + pOutput->Clear(); { // Bind RTVs @@ -386,6 +391,12 @@ HRESULT CRenderTarget_Manager::Refit_RenderTarget(CVIBuffer_Rect* pVIBuffer, CSh case Engine::SHADER_PASS_DEFERRED::DOWNSAMPLE: pConstantName = "g_DiffuseTexture"; break; + case Engine::SHADER_PASS_DEFERRED::MOTIONBLURTENT: + pConstantName = "g_VelocityTexture"; + break; + case Engine::SHADER_PASS_DEFERRED::MOTIONBLURTILE: + pConstantName = "g_VelocityTexture"; + break; case Engine::SHADER_PASS_DEFERRED::BLOOM_BLURY: pConstantName = "g_BlurXTexture"; break; diff --git a/Jusin154_Team3D/Engine/Private/Renderer.cpp b/Jusin154_Team3D/Engine/Private/Renderer.cpp index 574a35e5f..f6c8a7e9e 100644 --- a/Jusin154_Team3D/Engine/Private/Renderer.cpp +++ b/Jusin154_Team3D/Engine/Private/Renderer.cpp @@ -849,21 +849,15 @@ void CRenderer::Render_PostProcessing() assert(false); } SAFE_RELEASE(pBackBuffer); + if (FAILED(m_pShader->Bind_RawValue("g_iMBTileSize", &m_iMBTileSize, sizeof(_int)))) { + assert(false); return; + } m_pGameInstance->Copy_RenderTargetAToB(TEXT("Target_AfterBlend"), TEXT("Target_MotionBlur")); + m_pGameInstance->Refit_RenderTarget(m_pVIBuffer, m_pShader, TEXT("Target_VelocityMap"), TEXT("Target_VelocityTile"), SHADER_PASS_DEFERRED::MOTIONBLURTILE); + m_pGameInstance->Refit_RenderTarget(m_pVIBuffer, m_pShader, TEXT("Target_VelocityTile"), TEXT("Target_VelocityTent"), SHADER_PASS_DEFERRED::MOTIONBLURTENT); } /* MotionBlur */ - D3D11_VIEWPORT vpOld = {}; - _uint iVPOldNum = 1; - m_pContext->RSGetViewports(&iVPOldNum, &vpOld); - D3D11_VIEWPORT vpNew = vpOld; - _float2 vTaretSize = m_pGameInstance->Get_RenderTargetSize(TEXT("Target_MotionBlur")); - vpNew.Width = vTaretSize.x; - vpNew.Height = vTaretSize.y; - //if (FAILED(m_pShader->Bind_RawValue("g_vResolution", &vTaretSize, sizeof(_float2)))) { - // assert(false); - //} - - if (FAILED(m_pGameInstance->Begin_MRT(TEXT("MRT_MotionBlur_X")))) { + if (FAILED(m_pGameInstance->Begin_MRT(TEXT("MRT_MotionBlur")))) { assert(false); return; } { // Bind_Resorces @@ -914,35 +908,13 @@ void CRenderer::Render_PostProcessing() return; } } - m_pShader->Begin(ENUM_CLASS(SHADER_PASS_DEFERRED::MOTIONBLURX)); + m_pShader->Begin(ENUM_CLASS(SHADER_PASS_DEFERRED::MOTIONBLUR)); m_pVIBuffer->Bind_Resources(); m_pVIBuffer->Render(); if (FAILED(m_pGameInstance->End_MRT())) { return; } - if (false == m_bMB) { - if (FAILED(m_pGameInstance->Begin_MRT(TEXT("MRT_MotionBlur_Y")))) { - assert(false); return; - } - } - if (FAILED(m_pGameInstance->Bind_RenderTarget(TEXT("Target_MotionBlurX"), m_pShader, "g_ColorTexture"))) { - assert(false); - return; - } - if (FAILED(m_pGameInstance->Bind_RenderTarget(TEXT("Target_VelocityMapX"), m_pShader, "g_VelocityTexture"))) { - assert(false); - return; - } - m_pShader->Begin(ENUM_CLASS(SHADER_PASS_DEFERRED::MOTIONBLURY)); - - m_pVIBuffer->Bind_Resources(); - m_pVIBuffer->Render(); - if (false == m_bMB) { - if (FAILED(m_pGameInstance->End_MRT())) { - return; - } - } if (FAILED(m_pGameInstance->Begin_MRT(TEXT("MRT_Bloom")))) { return; } diff --git a/Jusin154_Team3D/Engine/Private/Renderer_Declare.cpp b/Jusin154_Team3D/Engine/Private/Renderer_Declare.cpp index 92e2000fe..5245a92bb 100644 --- a/Jusin154_Team3D/Engine/Private/Renderer_Declare.cpp +++ b/Jusin154_Team3D/Engine/Private/Renderer_Declare.cpp @@ -281,19 +281,6 @@ HRESULT CRenderer::Initialize() DXGI_FORMAT_R16G16B16A16_FLOAT, _float4(0.0f, 0.0f, 0.0f, 0.0f)))) { return E_FAIL; } - if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_MotionBlurX"), (_uint)Viewport.Width, (_uint)Viewport.Height, - DXGI_FORMAT_R16G16B16A16_FLOAT, _float4(0.0f, 0.0f, 0.0f, 0.0f)))) { - return E_FAIL; - } - if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_MotionBlurY"), (_uint)Viewport.Width, (_uint)Viewport.Height, - DXGI_FORMAT_R16G16B16A16_FLOAT, _float4(0.0f, 0.0f, 0.0f, 0.0f)))) { - return E_FAIL; - } - if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_MotionBlur_UpSample"), (_uint)Viewport.Width, (_uint)Viewport.Height, - DXGI_FORMAT_R16G16B16A16_FLOAT, _float4(0.0f, 0.0f, 0.0f, 0.0f)))) { - return E_FAIL; - } - /* Target_Color*/ if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_Color"), (_uint)Viewport.Width, (_uint)Viewport.Height, DXGI_FORMAT_R32G32B32A32_FLOAT, _float4(0.0f, 0.0f, 0.0f, 0.0f)))) { @@ -367,15 +354,15 @@ HRESULT CRenderer::Initialize() /* Target_VelocityMap */ if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_VelocityMap"), (_uint)Viewport.Width, (_uint)Viewport.Height, - DXGI_FORMAT_R32G32_FLOAT, _float4(0.0f, 0.f, 0.f, 0.f)))) { + DXGI_FORMAT_R32G32_FLOAT, _float4(0.5f, 0.5f, 0.f, 0.f)))) { return E_FAIL; } - if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_VelocityMapX"), (_uint)Viewport.Width, (_uint)Viewport.Height, - DXGI_FORMAT_R32G32_FLOAT, _float4(0.0f, 0.f, 0.f, 0.f)))) { + if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_VelocityTile"), (_uint)(Viewport.Width / (_float)m_iMBTileSize), (_uint)(Viewport.Height / (_float)m_iMBTileSize), + DXGI_FORMAT_R32G32_FLOAT, _float4(0.5f, 0.5f, 0.f, 0.f)))) { return E_FAIL; } - if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_VelocityMapY"), (_uint)Viewport.Width, (_uint)Viewport.Height, - DXGI_FORMAT_R32G32_FLOAT, _float4(0.0f, 0.f, 0.f, 0.f)))) { + if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_VelocityTent"), (_uint)(Viewport.Width / (_float)m_iMBTileSize), (_uint)(Viewport.Height / (_float)m_iMBTileSize), + DXGI_FORMAT_R32G32_FLOAT, _float4(0.5f, 0.5f, 0.f, 0.f)))) { return E_FAIL; } if (FAILED(m_pGameInstance->Add_RenderTarget(TEXT("Target_BeforeNormal"), (_uint)Viewport.Width, (_uint)Viewport.Height, @@ -523,19 +510,8 @@ HRESULT CRenderer::Initialize() return E_FAIL; } - /* MRT_Distortion */ - if (FAILED(m_pGameInstance->Add_MRT(TEXT("MRT_MotionBlur_X"), TEXT("Target_MotionBlurX")))) { - return E_FAIL; - } - if (FAILED(m_pGameInstance->Add_MRT(TEXT("MRT_MotionBlur_X"), TEXT("Target_VelocityMapX")))) { - return E_FAIL; - } - - /* MRT_MotionBlur_Y */ - if (FAILED(m_pGameInstance->Add_MRT(TEXT("MRT_MotionBlur_Y"), TEXT("Target_MotionBlurY")))) { - return E_FAIL; - } - if (FAILED(m_pGameInstance->Add_MRT(TEXT("MRT_MotionBlur_Y"), TEXT("Target_VelocityMapY")))) { + /* MRT_MotionBlur */ + if (FAILED(m_pGameInstance->Add_MRT(TEXT("MRT_MotionBlur"), TEXT("Target_MotionBlur")))) { return E_FAIL; } } diff --git a/Jusin154_Team3D/Engine/Public/Engine_Enum.h b/Jusin154_Team3D/Engine/Public/Engine_Enum.h index a54eab4e9..16c78c44c 100644 --- a/Jusin154_Team3D/Engine/Public/Engine_Enum.h +++ b/Jusin154_Team3D/Engine/Public/Engine_Enum.h @@ -48,7 +48,7 @@ enum class SHADER_PASS_CELL { DEBUG, END }; enum class SHADER_PASS_DEFERRED { DEBUG, DIRECTIONAL, POINT, COMBINED, BLUR, SPOT, UPSAMPLE, BLOOM_BLURX, BLOOM_BLURY, EMBOSSING, BLOOM_ACCUM, BLOOM_FINISH, BLURX_ENVIRONMENT, - POSTCOMBINED, FOG, TONE_MAPPING, SSAO_OCCLUSION, SSAO_BLUR, MOTIONBLURX, DOWNSAMPLE, MOTIONBLURY, + POSTCOMBINED, FOG, TONE_MAPPING, SSAO_OCCLUSION, SSAO_BLUR, MOTIONBLUR, DOWNSAMPLE, MOTIONBLURTILE, MOTIONBLURTENT, END }; enum class TEXTURE_MOUNT_SLOT0 { NOT_DEFINED, METALLIC, SPECULAR, HEIGHT, END }; diff --git a/Jusin154_Team3D/Engine/Public/Renderer.h b/Jusin154_Team3D/Engine/Public/Renderer.h index 331fb01c7..4eed06d47 100644 --- a/Jusin154_Team3D/Engine/Public/Renderer.h +++ b/Jusin154_Team3D/Engine/Public/Renderer.h @@ -72,6 +72,7 @@ class CRenderer final : public CBase _float m_fMBSampleBias = { 0.005f }; _int m_iMBMaxSampleCount = { 28 }; _int m_iMBSampleCount = { m_iMBMaxSampleCount }; + _int m_iMBTileSize = { 20 }; _int m_iMBType = { 0 }; _bool m_bMB = { true };