Skip to content

Commit

Permalink
Implement MSAA for shadow cascades
Browse files Browse the repository at this point in the history
Simple MSAAx4.
  • Loading branch information
pr0bability committed Jan 13, 2025
1 parent 63ab008 commit 8e15fa4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions resource/NewVegasReloaded.dll.defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ RenderDistance = 12000 # Max distance at which to compute screen s

[_Shaders.ShadowsExteriors.ShadowMaps]
CascadeResolution = 2 # Resolution used for shadow cascades. 0: 1024, 1: 1536, 2: 2048
MSAA = true # Enable multisample antialiasing for shadow maps.
Mipmaps = false # DISABLED. Enable mipmaps for the shadow maps.
Anisotropy = 0 # DISABLED. Anisotropic filteric for mipmaps, 0: disabled, 1: 8x, 2: 16x.

Expand Down
10 changes: 9 additions & 1 deletion src/core/ShadowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,11 @@ void ShadowManager::RenderShadowMaps() {
D3DXMATRIX View = GetViewMatrix(&At, SunDir);
auto shadowMapTimer = TimeLogger();

Device->SetRenderTarget(0, Shadows->ShadowAtlasSurface);
if (Shadows->ShadowAtlasSurfaceMSAA)
Device->SetRenderTarget(0, Shadows->ShadowAtlasSurfaceMSAA);
else
Device->SetRenderTarget(0, Shadows->ShadowAtlasSurface);

Device->SetDepthStencilSurface(Shadows->ShadowAtlasDepthSurface);
Device->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f), 1.0f, 0L);

Expand All @@ -626,6 +630,10 @@ void ShadowManager::RenderShadowMaps() {
}
}

// Resolve MSAA.
if (Shadows->ShadowAtlasSurfaceMSAA)
Device->StretchRect(Shadows->ShadowAtlasSurfaceMSAA, NULL, Shadows->ShadowAtlasSurface, NULL, D3DTEXF_LINEAR);

if (Shadows->Settings.ShadowMaps.Mipmaps)
Shadows->ShadowAtlasTexture->GenerateMipSubLevels();

Expand Down
26 changes: 24 additions & 2 deletions src/effects/ShadowsExterior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ void ShadowsExteriorEffect::UpdateSettings() {
if (oldCascadeResolution != 0 && oldCascadeResolution != Settings.ShadowMaps.CascadeResolution)
cascadeSettingsChanged = true;

bool oldMSAA = Settings.ShadowMaps.MSAA;
Settings.ShadowMaps.MSAA = TheSettingManager->GetSettingI("Shaders.ShadowsExteriors.ShadowMaps", "MSAA");

if (oldMSAA != Settings.ShadowMaps.MSAA)
cascadeSettingsChanged = true;

// Mipmaps and anisotropy are disabled due to deferred shadows - derivatives are messed up and causing artifacts.
// https://aras-p.info/blog/2010/01/07/screenspace-vs-mip-mapping/
/*bool oldMips = Settings.ShadowMaps.Mipmaps;
Expand Down Expand Up @@ -198,7 +204,13 @@ void ShadowsExteriorEffect::RegisterTextures() {
ULONG ShadowCubeMapSize = Settings.Interiors.ShadowCubeMapSize;

TheTextureManager->InitTexture("TESR_ShadowAtlas", &ShadowAtlasTexture, &ShadowAtlasSurface, ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_G32R32F, Settings.ShadowMaps.Mipmaps);
TheRenderManager->device->CreateDepthStencilSurface(ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, true, &ShadowAtlasDepthSurface, NULL);

if (!Settings.ShadowMaps.MSAA)
TheRenderManager->device->CreateDepthStencilSurface(ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, true, &ShadowAtlasDepthSurface, NULL);
else {
TheRenderManager->device->CreateRenderTarget(ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_G32R32F, D3DMULTISAMPLE_4_SAMPLES, 0, 0, &ShadowAtlasSurfaceMSAA, NULL);
TheRenderManager->device->CreateDepthStencilSurface(ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_D24S8, D3DMULTISAMPLE_4_SAMPLES, 0, true, &ShadowAtlasDepthSurface, NULL);
}

for (int i = 0; i <= MapLod; i++) {
ShadowMaps[i].ShadowMapViewPort = { i % 2 == 0 ? 0 : ShadowMapSize, i < 2 ? 0 : ShadowMapSize, ShadowMapSize, ShadowMapSize, 0.0f, 1.0f};
Expand Down Expand Up @@ -251,6 +263,10 @@ void ShadowsExteriorEffect::RecreateTextures(bool cascades, bool ortho, bool cub
ShadowAtlasSurface->Release();
ShadowAtlasSurface = nullptr;
}
if (ShadowAtlasSurfaceMSAA) {
ShadowAtlasSurfaceMSAA->Release();
ShadowAtlasSurfaceMSAA = nullptr;
}
if (ShadowAtlasTexture) {
ShadowAtlasTexture->Release();
ShadowAtlasTexture = nullptr;
Expand All @@ -263,7 +279,13 @@ void ShadowsExteriorEffect::RecreateTextures(bool cascades, bool ortho, bool cub
ULONG ShadowMapSize = Settings.ShadowMaps.CascadeResolution;

TheTextureManager->InitTexture("TESR_ShadowAtlas", &ShadowAtlasTexture, &ShadowAtlasSurface, ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_G32R32F, Settings.ShadowMaps.Mipmaps);
TheRenderManager->device->CreateDepthStencilSurface(ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, true, &ShadowAtlasDepthSurface, NULL);

if (!Settings.ShadowMaps.MSAA)
TheRenderManager->device->CreateDepthStencilSurface(ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, true, &ShadowAtlasDepthSurface, NULL);
else {
TheRenderManager->device->CreateRenderTarget(ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_G32R32F, D3DMULTISAMPLE_4_SAMPLES, 0, 0, &ShadowAtlasSurfaceMSAA, NULL);
TheRenderManager->device->CreateDepthStencilSurface(ShadowMapSize * 2, ShadowMapSize * 2, D3DFMT_D24S8, D3DMULTISAMPLE_4_SAMPLES, 0, true, &ShadowAtlasDepthSurface, NULL);
}

for (int i = 0; i <= MapLod; i++) {
ShadowMaps[i].ShadowMapViewPort = { i % 2 == 0 ? 0 : ShadowMapSize, i < 2 ? 0 : ShadowMapSize, ShadowMapSize, ShadowMapSize, 0.0f, 1.0f };
Expand Down
2 changes: 2 additions & 0 deletions src/effects/ShadowsExterior.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ShadowsExteriorEffect : public EffectRecord

struct ShadowMapStruct {
int CascadeResolution;
bool MSAA;
bool Mipmaps;
int Anisotropy;
};
Expand Down Expand Up @@ -140,6 +141,7 @@ class ShadowsExteriorEffect : public EffectRecord
// Main shadow atlas, used for cascades.
IDirect3DTexture9* ShadowAtlasTexture;
IDirect3DSurface9* ShadowAtlasSurface;
IDirect3DSurface9* ShadowAtlasSurfaceMSAA;
IDirect3DSurface9* ShadowAtlasDepthSurface;

// Ortho shadows for various effects.
Expand Down

0 comments on commit 8e15fa4

Please sign in to comment.