|
| 1 | +#include "stdafx.h" |
| 2 | + |
| 3 | +#include <Renderer.h> |
| 4 | +#include <ResourceManager.h> |
| 5 | +#include <RenderState.h> |
| 6 | +#include <RenderTarget.h> |
| 7 | +#include <Texture.h> |
| 8 | +using namespace LibRendererDll; |
| 9 | + |
| 10 | +#include "GITechDemo.h" |
| 11 | +#include "DirectionalLightVolumePass.h" |
| 12 | +using namespace GITechDemoApp; |
| 13 | + |
| 14 | +#include "RenderResourcesDef.h" |
| 15 | + |
| 16 | +namespace GITechDemoApp |
| 17 | +{ |
| 18 | + extern float CASCADE_MAX_VIEW_DEPTH; |
| 19 | + |
| 20 | + bool DIR_LIGHT_VOLUME_ENABLE = true; |
| 21 | + bool DIR_LIGHT_VOLUME_QUARTER_RES = true; |
| 22 | + bool DIR_LIGHT_VOLUME_BLUR_SAMPLES = true; |
| 23 | + bool DIR_LIGHT_VOLUME_BLUR_DEPTH_AWARE = true; |
| 24 | + bool DIR_LIGHT_VOLUME_UPSCALE_DEPTH_AWARE = true; |
| 25 | +} |
| 26 | + |
| 27 | +DirectionalLightVolumePass::DirectionalLightVolumePass(const char* const passName, RenderPass* const parentPass) |
| 28 | + : RenderPass(passName, parentPass) |
| 29 | +{ |
| 30 | + fElapsedTime = 0.f; |
| 31 | +} |
| 32 | + |
| 33 | +DirectionalLightVolumePass::~DirectionalLightVolumePass() |
| 34 | +{} |
| 35 | + |
| 36 | +void DirectionalLightVolumePass::Update(const float fDeltaTime) |
| 37 | +{ |
| 38 | + if (DIR_LIGHT_VOLUME_QUARTER_RES) |
| 39 | + VolumetricLightAccumulationBuffer = VolumetricLightQuarterBuffer; |
| 40 | + else |
| 41 | + VolumetricLightAccumulationBuffer = VolumetricLightFullBuffer; |
| 42 | + |
| 43 | + Vec4f v4CamPosLightVS = f44ScreenToLightViewMat * Vec4f(0.f, 0.f, 0.f, 1.f); |
| 44 | + v4CamPosLightVS /= v4CamPosLightVS[3]; |
| 45 | + f3CameraPositionLightVS = Vec3f(v4CamPosLightVS[0], v4CamPosLightVS[1], v4CamPosLightVS[2]); |
| 46 | + fRaymarchDistanceLimit = CASCADE_MAX_VIEW_DEPTH; |
| 47 | + f2TexSize = Vec2f( |
| 48 | + (float)VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetWidth(), |
| 49 | + (float)VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetHeight() |
| 50 | + ); |
| 51 | + bSingleChannelCopy = true; |
| 52 | + BayerMatrix.GetTexture()->SetAddressingMode(SAM_WRAP); |
| 53 | + BayerMatrix.GetTexture()->SetFilter(SF_MIN_MAG_POINT_MIP_NONE); |
| 54 | + texDitherMap = BayerMatrix.GetTextureIndex(); |
| 55 | + NoiseTexture.GetTexture()->SetAddressingMode(SAM_WRAP); |
| 56 | + NoiseTexture.GetTexture()->SetFilter(SF_MIN_MAG_LINEAR_MIP_NONE); |
| 57 | + texNoise = NoiseTexture.GetTextureIndex(); |
| 58 | + fElapsedTime += fDeltaTime; |
| 59 | + f3FogBox = Vec3f(CASCADE_MAX_VIEW_DEPTH, CASCADE_MAX_VIEW_DEPTH, CASCADE_MAX_VIEW_DEPTH); |
| 60 | +} |
| 61 | + |
| 62 | +void DirectionalLightVolumePass::CalculateLightVolume() |
| 63 | +{ |
| 64 | + Renderer* RenderContext = Renderer::GetInstance(); |
| 65 | + if (!RenderContext) |
| 66 | + return; |
| 67 | + |
| 68 | + PUSH_PROFILE_MARKER("Raymarch (scatter)"); |
| 69 | + |
| 70 | + VolumetricLightAccumulationBuffer[0]->Enable(); |
| 71 | + |
| 72 | + const bool blendEnable = RenderContext->GetRenderStateManager()->GetColorBlendEnabled(); |
| 73 | + RenderContext->GetRenderStateManager()->SetColorBlendEnabled(false); |
| 74 | + |
| 75 | + f2HalfTexelOffset = Vec2f(0.5f / LightAccumulationBuffer.GetRenderTarget()->GetWidth(), 0.5f / LightAccumulationBuffer.GetRenderTarget()->GetHeight()); |
| 76 | + texShadowMap = ShadowMapDir.GetRenderTarget()->GetDepthBuffer(); |
| 77 | + texDepthBuffer = GBuffer.GetRenderTarget()->GetDepthBuffer(); |
| 78 | + |
| 79 | + DirectionalLightVolumeShader.Enable(); |
| 80 | + RenderContext->DrawVertexBuffer(FullScreenTri); |
| 81 | + DirectionalLightVolumeShader.Disable(); |
| 82 | + |
| 83 | + RenderContext->GetRenderStateManager()->SetColorBlendEnabled(blendEnable); |
| 84 | + |
| 85 | + VolumetricLightAccumulationBuffer[0]->Disable(); |
| 86 | + |
| 87 | + POP_PROFILE_MARKER(); |
| 88 | +} |
| 89 | + |
| 90 | +void DirectionalLightVolumePass::GatherSamples() |
| 91 | +{ |
| 92 | + Renderer* RenderContext = Renderer::GetInstance(); |
| 93 | + if (!RenderContext) |
| 94 | + return; |
| 95 | + |
| 96 | + ResourceManager* ResourceMgr = RenderContext->GetResourceManager(); |
| 97 | + if (!ResourceMgr) |
| 98 | + return; |
| 99 | + |
| 100 | + PUSH_PROFILE_MARKER("Bilateral blur (gather)"); |
| 101 | + |
| 102 | + const bool blendEnable = RenderContext->GetRenderStateManager()->GetColorBlendEnabled(); |
| 103 | + RenderContext->GetRenderStateManager()->SetColorBlendEnabled(false); |
| 104 | + |
| 105 | + const float fBlurDepthFalloffBkp = fBlurDepthFalloff; |
| 106 | + if (!DIR_LIGHT_VOLUME_BLUR_DEPTH_AWARE) |
| 107 | + fBlurDepthFalloff = 0.f; |
| 108 | + |
| 109 | + PUSH_PROFILE_MARKER("Horizontal"); |
| 110 | + |
| 111 | + VolumetricLightAccumulationBuffer[1]->Enable(); |
| 112 | + |
| 113 | + f2HalfTexelOffset = Vec2f(0.5f / VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetWidth(), 0.5f / LightAccumulationBuffer.GetRenderTarget()->GetHeight()); |
| 114 | + f2TexelSize = Vec2f( |
| 115 | + 1.f / (float)VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetWidth(), |
| 116 | + 1.f / (float)VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetHeight() |
| 117 | + ); |
| 118 | + f2DepthHalfTexelOffset = Vec2f( |
| 119 | + 0.5f / (float)LightAccumulationBuffer.GetRenderTarget()->GetWidth(), |
| 120 | + 0.5f / (float)LightAccumulationBuffer.GetRenderTarget()->GetHeight() |
| 121 | + ); |
| 122 | + ResourceMgr->GetTexture( |
| 123 | + VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetColorBuffer(0) |
| 124 | + )->SetFilter(SF_MIN_MAG_POINT_MIP_NONE); |
| 125 | + ResourceMgr->GetTexture( |
| 126 | + VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetColorBuffer(0) |
| 127 | + )->SetAddressingMode(SAM_CLAMP); |
| 128 | + texSource = VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetColorBuffer(0); |
| 129 | + f2BlurDir = Vec2f(1.f, 0.f); |
| 130 | + |
| 131 | + BilateralBlurShader.Enable(); |
| 132 | + RenderContext->DrawVertexBuffer(FullScreenTri); |
| 133 | + BilateralBlurShader.Disable(); |
| 134 | + |
| 135 | + VolumetricLightAccumulationBuffer[1]->Disable(); |
| 136 | + |
| 137 | + POP_PROFILE_MARKER(); |
| 138 | + |
| 139 | + PUSH_PROFILE_MARKER("Vertical"); |
| 140 | + |
| 141 | + VolumetricLightAccumulationBuffer[0]->Enable(); |
| 142 | + |
| 143 | + f2HalfTexelOffset = Vec2f(0.5f / VolumetricLightAccumulationBuffer[1]->GetRenderTarget()->GetWidth(), 0.5f / LightAccumulationBuffer.GetRenderTarget()->GetHeight()); |
| 144 | + f2TexelSize = Vec2f( |
| 145 | + 1.f / (float)VolumetricLightAccumulationBuffer[1]->GetRenderTarget()->GetWidth(), |
| 146 | + 1.f / (float)VolumetricLightAccumulationBuffer[1]->GetRenderTarget()->GetHeight() |
| 147 | + ); |
| 148 | + f2DepthHalfTexelOffset = Vec2f( |
| 149 | + 0.5f / (float)LightAccumulationBuffer.GetRenderTarget()->GetWidth(), |
| 150 | + 0.5f / (float)LightAccumulationBuffer.GetRenderTarget()->GetHeight() |
| 151 | + ); |
| 152 | + ResourceMgr->GetTexture( |
| 153 | + VolumetricLightAccumulationBuffer[1]->GetRenderTarget()->GetColorBuffer(0) |
| 154 | + )->SetFilter(SF_MIN_MAG_POINT_MIP_NONE); |
| 155 | + ResourceMgr->GetTexture( |
| 156 | + VolumetricLightAccumulationBuffer[1]->GetRenderTarget()->GetColorBuffer(0) |
| 157 | + )->SetAddressingMode(SAM_CLAMP); |
| 158 | + texSource = VolumetricLightAccumulationBuffer[1]->GetRenderTarget()->GetColorBuffer(0); |
| 159 | + f2BlurDir = Vec2f(0.f, 1.f); |
| 160 | + |
| 161 | + BilateralBlurShader.Enable(); |
| 162 | + RenderContext->DrawVertexBuffer(FullScreenTri); |
| 163 | + BilateralBlurShader.Disable(); |
| 164 | + |
| 165 | + VolumetricLightAccumulationBuffer[0]->Disable(); |
| 166 | + |
| 167 | + POP_PROFILE_MARKER(); |
| 168 | + |
| 169 | + if (!DIR_LIGHT_VOLUME_BLUR_DEPTH_AWARE) |
| 170 | + fBlurDepthFalloff = fBlurDepthFalloffBkp; |
| 171 | + |
| 172 | + RenderContext->GetRenderStateManager()->SetColorBlendEnabled(blendEnable); |
| 173 | + |
| 174 | + POP_PROFILE_MARKER(); |
| 175 | +} |
| 176 | + |
| 177 | +void DirectionalLightVolumePass::ApplyLightVolume() |
| 178 | +{ |
| 179 | + Renderer* RenderContext = Renderer::GetInstance(); |
| 180 | + if (!RenderContext) |
| 181 | + return; |
| 182 | + |
| 183 | + ResourceManager* ResourceMgr = RenderContext->GetResourceManager(); |
| 184 | + if (!ResourceMgr) |
| 185 | + return; |
| 186 | + |
| 187 | + PUSH_PROFILE_MARKER("Apply (bilateral upscale)"); |
| 188 | + |
| 189 | + const float fUpsampleDepthThresholdBkp = fUpsampleDepthThreshold; |
| 190 | + if (!DIR_LIGHT_VOLUME_UPSCALE_DEPTH_AWARE) |
| 191 | + fUpsampleDepthThreshold = 1.f; |
| 192 | + |
| 193 | + LightAccumulationBuffer.Enable(); |
| 194 | + |
| 195 | + f2HalfTexelOffset = Vec2f( |
| 196 | + 0.5f / LightAccumulationBuffer.GetRenderTarget()->GetWidth(), |
| 197 | + 0.5f / LightAccumulationBuffer.GetRenderTarget()->GetHeight() |
| 198 | + ); |
| 199 | + f2TexelSize = Vec2f( |
| 200 | + 1.f / (float)VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetWidth(), |
| 201 | + 1.f / (float)VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetHeight() |
| 202 | + ); |
| 203 | + f2DepthHalfTexelOffset = Vec2f( |
| 204 | + 0.5f / (float)LightAccumulationBuffer.GetRenderTarget()->GetWidth(), |
| 205 | + 0.5f / (float)LightAccumulationBuffer.GetRenderTarget()->GetHeight() |
| 206 | + ); |
| 207 | + f2TexSize = Vec2f( |
| 208 | + (float)VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetWidth(), |
| 209 | + (float)VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetHeight() |
| 210 | + ); |
| 211 | + ResourceMgr->GetTexture( |
| 212 | + VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetColorBuffer(0) |
| 213 | + )->SetFilter(SF_MIN_MAG_LINEAR_MIP_NONE); |
| 214 | + ResourceMgr->GetTexture( |
| 215 | + VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetColorBuffer(0) |
| 216 | + )->SetAddressingMode(SAM_CLAMP); |
| 217 | + texSource = VolumetricLightAccumulationBuffer[0]->GetRenderTarget()->GetColorBuffer(0); |
| 218 | + texDepthBuffer = GBuffer.GetRenderTarget()->GetDepthBuffer(); |
| 219 | + |
| 220 | + if (DIR_LIGHT_VOLUME_QUARTER_RES) |
| 221 | + { |
| 222 | + NearestDepthUpscaleShader.Enable(); |
| 223 | + RenderContext->DrawVertexBuffer(FullScreenTri); |
| 224 | + NearestDepthUpscaleShader.Disable(); |
| 225 | + } |
| 226 | + else |
| 227 | + { |
| 228 | + ColorCopyShader.Enable(); |
| 229 | + RenderContext->DrawVertexBuffer(FullScreenTri); |
| 230 | + ColorCopyShader.Disable(); |
| 231 | + } |
| 232 | + |
| 233 | + LightAccumulationBuffer.Disable(); |
| 234 | + |
| 235 | + if (!DIR_LIGHT_VOLUME_UPSCALE_DEPTH_AWARE) |
| 236 | + fUpsampleDepthThreshold = fUpsampleDepthThresholdBkp; |
| 237 | + |
| 238 | + POP_PROFILE_MARKER(); |
| 239 | +} |
| 240 | + |
| 241 | +void DirectionalLightVolumePass::Draw() |
| 242 | +{ |
| 243 | + if (DIR_LIGHT_VOLUME_ENABLE) |
| 244 | + { |
| 245 | + LibRendererDll::RenderTarget* pCurrRT = LibRendererDll::RenderTarget::GetActiveRenderTarget(); |
| 246 | + if (pCurrRT) |
| 247 | + pCurrRT->Disable(); |
| 248 | + |
| 249 | + CalculateLightVolume(); |
| 250 | + |
| 251 | + if (DIR_LIGHT_VOLUME_BLUR_SAMPLES) |
| 252 | + GatherSamples(); |
| 253 | + |
| 254 | + ApplyLightVolume(); |
| 255 | + |
| 256 | + pCurrRT->Enable(); |
| 257 | + } |
| 258 | +} |
0 commit comments