Make linearized depth data available to translucent material shaders via the texture name SceneLinearDepth. Does it work? Who knows!

This commit is contained in:
Magnus Norddahl 2024-09-08 02:32:01 +02:00
commit 1b8effe1ea
17 changed files with 183 additions and 32 deletions

View file

@ -639,6 +639,44 @@ void PPTonemap::Render(PPRenderState *renderstate)
/////////////////////////////////////////////////////////////////////////////
void PPLinearDepth::Render(PPRenderState* renderstate, int sceneWidth, int sceneHeight)
{
auto sceneScale = screen->SceneScale();
auto sceneOffset = screen->SceneOffset();
LinearDepthUniforms linearUniforms;
linearUniforms.SampleIndex = 0;
linearUniforms.LinearizeDepthA = 1.0f / screen->GetZFar() - 1.0f / screen->GetZNear();
linearUniforms.LinearizeDepthB = max(1.0f / screen->GetZNear(), 1.e-8f);
linearUniforms.InverseDepthRangeA = 1.0f;
linearUniforms.InverseDepthRangeB = 0.0f;
linearUniforms.Scale = sceneScale;
linearUniforms.Offset = sceneOffset;
IntRect viewport;
viewport.left = 0;
viewport.top = 0;
viewport.width = sceneWidth;
viewport.height = sceneHeight;
renderstate->PushGroup("lineardepth");
// Calculate linear depth values
renderstate->Clear();
renderstate->Shader = gl_multisample > 1 ? &LinearDepthMS : &LinearDepth;
renderstate->Uniforms.Set(linearUniforms);
renderstate->Viewport = viewport;
renderstate->SetInputSceneDepth(0);
renderstate->SetInputSceneColor(1);
renderstate->SetOutputSceneLinearDepth();
renderstate->SetNoBlend();
renderstate->Draw();
renderstate->PopGroup();
}
/////////////////////////////////////////////////////////////////////////////
PPAmbientOcclusion::PPAmbientOcclusion()
{
// Must match quality enum in PPAmbientOcclusion::DeclareShaders
@ -692,8 +730,6 @@ void PPAmbientOcclusion::CreateShaders()
#define NUM_STEPS %d.0
)", numDirections, numSteps);
LinearDepth = { "shaders/pp/lineardepth.fp", "", LinearDepthUniforms::Desc() };
LinearDepthMS = { "shaders/pp/lineardepth.fp", "#define MULTISAMPLE\n", LinearDepthUniforms::Desc() };
AmbientOcclude = { "shaders/pp/ssao.fp", defines, SSAOUniforms::Desc() };
AmbientOccludeMS = { "shaders/pp/ssao.fp", defines + "\n#define MULTISAMPLE\n", SSAOUniforms::Desc() };
BlurVertical = { "shaders/pp/depthblur.fp", "#define BLUR_VERTICAL\n", DepthBlurUniforms::Desc() };
@ -712,7 +748,6 @@ void PPAmbientOcclusion::UpdateTextures(int width, int height)
AmbientWidth = (width + 1) / 2;
AmbientHeight = (height + 1) / 2;
LinearDepthTexture = { AmbientWidth, AmbientHeight, PixelFormat::R32f };
Ambient0 = { AmbientWidth, AmbientHeight, PixelFormat::Rg16f };
Ambient1 = { AmbientWidth, AmbientHeight, PixelFormat::Rg16f };
@ -749,15 +784,6 @@ void PPAmbientOcclusion::Render(PPRenderState *renderstate, float m5, int sceneW
int randomTexture = clamp(gl_ssao - 1, 0, NumAmbientRandomTextures - 1);
LinearDepthUniforms linearUniforms;
linearUniforms.SampleIndex = 0;
linearUniforms.LinearizeDepthA = 1.0f / screen->GetZFar() - 1.0f / screen->GetZNear();
linearUniforms.LinearizeDepthB = max(1.0f / screen->GetZNear(), 1.e-8f);
linearUniforms.InverseDepthRangeA = 1.0f;
linearUniforms.InverseDepthRangeB = 0.0f;
linearUniforms.Scale = sceneScale;
linearUniforms.Offset = sceneOffset;
SSAOUniforms ssaoUniforms;
ssaoUniforms.SampleIndex = 0;
ssaoUniforms.UVToViewA = { 2.0f * invFocalLenX, 2.0f * invFocalLenY };
@ -789,23 +815,12 @@ void PPAmbientOcclusion::Render(PPRenderState *renderstate, float m5, int sceneW
renderstate->PushGroup("ssao");
// Calculate linear depth values
renderstate->Clear();
renderstate->Shader = gl_multisample > 1 ? &LinearDepthMS : &LinearDepth;
renderstate->Uniforms.Set(linearUniforms);
renderstate->Viewport = ambientViewport;
renderstate->SetInputSceneDepth(0);
renderstate->SetInputSceneColor(1);
renderstate->SetOutputTexture(&LinearDepthTexture);
renderstate->SetNoBlend();
renderstate->Draw();
// Apply ambient occlusion
renderstate->Clear();
renderstate->Shader = gl_multisample > 1 ? &AmbientOccludeMS : &AmbientOcclude;
renderstate->Uniforms.Set(ssaoUniforms);
renderstate->Viewport = ambientViewport;
renderstate->SetInputTexture(0, &LinearDepthTexture);
renderstate->SetInputSceneLinearDepth(0);
renderstate->SetInputSceneNormal(1);
renderstate->SetInputTexture(2, &AmbientRandomTexture[randomTexture], PPFilterMode::Nearest, PPWrapMode::Repeat);
renderstate->SetOutputTexture(&Ambient0);

View file

@ -60,7 +60,7 @@ public:
enum class PPFilterMode { Nearest, Linear };
enum class PPWrapMode { Clamp, Repeat };
enum class PPTextureType { CurrentPipelineTexture, NextPipelineTexture, PPTexture, SceneColor, SceneFog, SceneNormal, SceneDepth, SwapChain, ShadowMap };
enum class PPTextureType { CurrentPipelineTexture, NextPipelineTexture, PPTexture, SceneColor, SceneFog, SceneNormal, SceneDepth, SceneLinearDepth, SwapChain, ShadowMap };
class PPTextureInput
{
@ -176,6 +176,11 @@ public:
SetInputSpecialType(index, PPTextureType::SceneDepth, filter, wrap);
}
void SetInputSceneLinearDepth(int index, PPFilterMode filter = PPFilterMode::Nearest, PPWrapMode wrap = PPWrapMode::Clamp)
{
SetInputSpecialType(index, PPTextureType::SceneLinearDepth, filter, wrap);
}
void SetInputSpecialType(int index, PPTextureType type, PPFilterMode filter = PPFilterMode::Nearest, PPWrapMode wrap = PPWrapMode::Clamp)
{
if ((int)Textures.Size() < index + 1)
@ -216,6 +221,12 @@ public:
Output.Texture = nullptr;
}
void SetOutputSceneLinearDepth()
{
Output.Type = PPTextureType::SceneLinearDepth;
Output.Texture = nullptr;
}
void SetOutputSwapChain()
{
Output.Type = PPTextureType::SwapChain;
@ -634,6 +645,18 @@ struct LinearDepthUniforms
}
};
class PPLinearDepth
{
public:
void Render(PPRenderState* renderstate, int sceneWidth, int sceneHeight);
private:
PPShader LinearDepth = { "shaders/pp/lineardepth.fp", "", LinearDepthUniforms::Desc() };
PPShader LinearDepthMS = { "shaders/pp/lineardepth.fp", "#define MULTISAMPLE\n", LinearDepthUniforms::Desc() };
};
/////////////////////////////////////////////////////////////////////////////
struct SSAOUniforms
{
FVector2 UVToViewA;
@ -735,8 +758,6 @@ private:
int LastWidth = 0;
int LastHeight = 0;
PPShader LinearDepth;
PPShader LinearDepthMS;
PPShader AmbientOcclude;
PPShader AmbientOccludeMS;
PPShader BlurVertical;
@ -744,7 +765,6 @@ private:
PPShader Combine;
PPShader CombineMS;
PPTexture LinearDepthTexture;
PPTexture Ambient0;
PPTexture Ambient1;
@ -870,6 +890,7 @@ public:
PPCameraExposure exposure;
PPColormap colormap;
PPTonemap tonemap;
PPLinearDepth linearDepth;
PPAmbientOcclusion ssao;
PPPresent present;
PPShadowMap shadowmap;

View file

@ -103,7 +103,7 @@ protected:
class IHardwareTexture;
class FTexture;
class FSceneTexture;
class DFrameBuffer
{
@ -179,6 +179,7 @@ public:
// Delete any resources that need to be deleted after restarting with a different IWAD
virtual void SetTextureFilterMode() {}
virtual IHardwareTexture *CreateHardwareTexture(int numchannels) { return nullptr; }
virtual IHardwareTexture* CreateSceneTexture(FSceneTexture* owner) { return nullptr; }
virtual void PrecacheMaterial(FMaterial *mat, int translation) {}
virtual FMaterial* CreateMaterial(FGameTexture* tex, int scaleflags);
virtual void BeginFrame() {}
@ -204,6 +205,7 @@ public:
void SetClearColor(int color);
virtual int Backend() { return 0; }
virtual const char* DeviceName() const { return "Unknown"; }
virtual void UpdateLinearDepthTexture() {}
virtual void AmbientOccludeScene(float m5) {}
virtual void FirstEye() {}
virtual void NextEye(int eyecount) {}

View file

@ -43,6 +43,12 @@ VkHardwareTexture::VkHardwareTexture(VulkanRenderDevice* fb, int numchannels) :
fb->GetTextureManager()->AddTexture(this);
}
VkHardwareTexture::VkHardwareTexture(VulkanRenderDevice* fb, FSceneTextureType sceneTextureType) : fb(fb), mSceneTextureType(sceneTextureType)
{
mTexelsize = 4;
fb->GetTextureManager()->AddTexture(this);
}
VkHardwareTexture::~VkHardwareTexture()
{
if (fb)
@ -66,6 +72,11 @@ void VkHardwareTexture::Reset()
VkTextureImage *VkHardwareTexture::GetImage(FTexture *tex, int translation, int flags)
{
if (mSceneTextureType == FSceneTextureType::LinearDepth)
{
return &fb->GetBuffers()->SceneLinearDepth;
}
if (!mImage.Image)
{
CreateImage(tex, translation, flags);

View file

@ -21,12 +21,14 @@ class VulkanImageView;
class VulkanBuffer;
class VulkanRenderDevice;
class FGameTexture;
enum class FSceneTextureType;
class VkHardwareTexture : public IHardwareTexture
{
friend class VkMaterial;
public:
VkHardwareTexture(VulkanRenderDevice* fb, int numchannels);
VkHardwareTexture(VulkanRenderDevice* fb, FSceneTextureType sceneTextureType);
~VkHardwareTexture();
void Reset();
@ -51,6 +53,8 @@ private:
void CreateTexture(int w, int h, int pixelsize, VkFormat format, const void *pixels, bool mipmap);
static int GetMipLevels(int w, int h);
FSceneTextureType mSceneTextureType = FSceneTextureType::None;
VkTextureImage mImage;
int mTexelsize = 4;

View file

@ -174,11 +174,13 @@ void VkRenderBuffers::CreateScene(int width, int height, VkSampleCountFlagBits s
SceneDepthStencil.Reset(fb);
SceneNormal.Reset(fb);
SceneFog.Reset(fb);
SceneLinearDepth.Reset(fb);
CreateSceneColor(width, height, samples);
CreateSceneDepthStencil(width, height, samples);
CreateSceneNormal(width, height, samples);
CreateSceneFog(width, height, samples);
CreateSceneLinearDepth(width, height);
VkImageTransition()
.AddImage(&SceneColor, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, true)
@ -252,6 +254,21 @@ void VkRenderBuffers::CreateSceneFog(int width, int height, VkSampleCountFlagBit
.Create(fb->GetDevice());
}
void VkRenderBuffers::CreateSceneLinearDepth(int width, int height)
{
SceneLinearDepth.Image = ImageBuilder()
.Size(width, height)
.Format(VK_FORMAT_R32_SFLOAT)
.Usage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT)
.DebugName("VkRenderBuffers.SceneLinearDepth")
.Create(fb->GetDevice());
SceneLinearDepth.View = ImageViewBuilder()
.Image(SceneLinearDepth.Image.get(), VK_FORMAT_R32_SFLOAT)
.DebugName("VkRenderBuffers.SceneLinearDepthView")
.Create(fb->GetDevice());
}
void VkRenderBuffers::CreateSceneNormal(int width, int height, VkSampleCountFlagBits samples)
{
ImageBuilder builder;

View file

@ -34,6 +34,7 @@ public:
VkTextureImage SceneDepthStencil;
VkTextureImage SceneNormal;
VkTextureImage SceneFog;
VkTextureImage SceneLinearDepth;
VkFormat PipelineDepthStencilFormat = VK_FORMAT_D24_UNORM_S8_UINT;
VkFormat SceneDepthStencilFormat = VK_FORMAT_D24_UNORM_S8_UINT;
@ -53,6 +54,7 @@ private:
void CreateSceneDepthStencil(int width, int height, VkSampleCountFlagBits samples);
void CreateSceneFog(int width, int height, VkSampleCountFlagBits samples);
void CreateSceneNormal(int width, int height, VkSampleCountFlagBits samples);
void CreateSceneLinearDepth(int width, int height);
VkSampleCountFlagBits GetBestSampleCount();
VulkanRenderDevice* fb = nullptr;

View file

@ -114,6 +114,10 @@ VkTextureImage* VkTextureManager::GetTexture(const PPTextureType& type, PPTextur
{
return &fb->GetBuffers()->SceneDepthStencil;
}
else if (type == PPTextureType::SceneLinearDepth)
{
return &fb->GetBuffers()->SceneLinearDepth;
}
else if (type == PPTextureType::ShadowMap)
{
return &Shadowmap;

View file

@ -244,6 +244,17 @@ void VkPostprocess::DrawPresentTexture(const IntRect &box, bool applyGamma, bool
renderstate.Draw();
}
void VkPostprocess::UpdateLinearDepthTexture()
{
int sceneWidth = fb->GetBuffers()->GetSceneWidth();
int sceneHeight = fb->GetBuffers()->GetSceneHeight();
VkPPRenderState renderstate(fb);
hw_postprocess.linearDepth.Render(&renderstate, sceneWidth, sceneHeight);
ImageTransitionScene(false);
}
void VkPostprocess::AmbientOccludeScene(float m5)
{
int sceneWidth = fb->GetBuffers()->GetSceneWidth();

View file

@ -26,6 +26,7 @@ public:
void SetActiveRenderTarget();
void PostProcessScene(int fixedcm, float flash, const std::function<void()> &afterBloomDrawEndScene2D);
void UpdateLinearDepthTexture();
void AmbientOccludeScene(float m5);
void BlurScene(float gameinfobluramount);
void ClearTonemapPalette();

View file

@ -66,7 +66,7 @@ void VkPPRenderState::Draw()
key.OutputFormat = fb->GetTextureManager()->GetTextureFormat(Output.Texture);
else if (Output.Type == PPTextureType::SwapChain)
key.OutputFormat = fb->GetFramebufferManager()->SwapChain->Format().format;
else if (Output.Type == PPTextureType::ShadowMap)
else if (Output.Type == PPTextureType::ShadowMap || Output.Type == PPTextureType::SceneLinearDepth)
key.OutputFormat = VK_FORMAT_R32_SFLOAT;
else
key.OutputFormat = VK_FORMAT_R16G16B16A16_SFLOAT;

View file

@ -355,6 +355,12 @@ IHardwareTexture *VulkanRenderDevice::CreateHardwareTexture(int numchannels)
return new VkHardwareTexture(this, numchannels);
}
IHardwareTexture* VulkanRenderDevice::CreateSceneTexture(FSceneTexture* owner)
{
mSceneTextureOwners[owner->Type] = owner;
return new VkHardwareTexture(this, owner->Type);
}
FMaterial* VulkanRenderDevice::CreateMaterial(FGameTexture* tex, int scaleflags)
{
return new VkMaterial(this, tex, scaleflags);
@ -535,6 +541,7 @@ void VulkanRenderDevice::BeginFrame()
mTextureManager->BeginFrame();
mScreenBuffers->BeginFrame(screen->mScreenViewport.width, screen->mScreenViewport.height, screen->mSceneViewport.width, screen->mSceneViewport.height);
mSaveBuffers->BeginFrame(SAVEPICWIDTH, SAVEPICHEIGHT, SAVEPICWIDTH, SAVEPICHEIGHT);
UpdateSceneTextureSizes();
mRenderState->BeginFrame();
mDescriptorSetManager->BeginFrame();
mLightmapper->BeginFrame();
@ -622,6 +629,18 @@ void VulkanRenderDevice::SetSaveBuffers(bool yes)
{
if (yes) mActiveRenderBuffers = mSaveBuffers.get();
else mActiveRenderBuffers = mScreenBuffers.get();
UpdateSceneTextureSizes();
}
void VulkanRenderDevice::UpdateSceneTextureSizes()
{
for (auto& it : mSceneTextureOwners)
{
if (it.second)
{
it.second->SetSize(mActiveRenderBuffers->GetSceneWidth(), mActiveRenderBuffers->GetSceneHeight());
}
}
}
void VulkanRenderDevice::ImageTransitionScene(bool unknown)
@ -634,6 +653,11 @@ FRenderState* VulkanRenderDevice::RenderState()
return mRenderState.get();
}
void VulkanRenderDevice::UpdateLinearDepthTexture()
{
mPostprocess->UpdateLinearDepthTexture();
}
void VulkanRenderDevice::AmbientOccludeScene(float m5)
{
mPostprocess->AmbientOccludeScene(m5);

View file

@ -63,6 +63,7 @@ public:
void BeginFrame() override;
void BlurScene(float amount) override;
void PostProcessScene(bool swscene, int fixedcm, float flash, const std::function<void()> &afterBloomDrawEndScene2D) override;
void UpdateLinearDepthTexture() override;
void AmbientOccludeScene(float m5) override;
void SetSceneRenderTarget(bool useSSAO) override;
void SetLevelMesh(LevelMesh* mesh) override;
@ -73,6 +74,7 @@ public:
void SetActiveRenderTarget() override;
IHardwareTexture *CreateHardwareTexture(int numchannels) override;
IHardwareTexture* CreateSceneTexture(FSceneTexture* owner) override;
FMaterial* CreateMaterial(FGameTexture* tex, int scaleflags) override;
IBuffer* CreateVertexBuffer(int numBindingPoints, int numAttributes, size_t stride, const FVertexBufferAttribute* attrs) override;
@ -101,6 +103,8 @@ private:
void PrintStartupLog();
void CopyScreenToBuffer(int w, int h, uint8_t *data) override;
void UpdateSceneTextureSizes();
std::shared_ptr<VulkanDevice> mDevice;
std::unique_ptr<VkCommandBufferManager> mCommands;
std::unique_ptr<VkBufferManager> mBufferManager;
@ -117,6 +121,8 @@ private:
std::unique_ptr<VkLightmapper> mLightmapper;
std::unique_ptr<VkRenderState> mRenderState;
std::unordered_map<FSceneTextureType, FSceneTexture*> mSceneTextureOwners;
VkRenderBuffers *mActiveRenderBuffers = nullptr;
bool mVSync = false;

View file

@ -574,3 +574,12 @@ FWrapperTexture::FWrapperTexture(int w, int h, int bits)
SystemTextures.AddHardwareTexture(0, false, hwtex);
}
//==========================================================================
FSceneTexture::FSceneTexture(FSceneTextureType type) : Type(type)
{
Width = 1;
Height = 1;
auto hwtex = screen->CreateSceneTexture(this);
SystemTextures.AddHardwareTexture(0, false, hwtex);
}

View file

@ -1211,6 +1211,11 @@ void FTextureManager::Init()
mt = MakeGameTexture(new AnimTexture(), "AnimTextureFrame2", ETextureType::Override);
mt->SetUpscaleFlag(false, true);
AddGameTexture(mt);
// Linear scene depth texture for translucent shaders
auto scenedepth = MakeGameTexture(new FSceneTexture(FSceneTextureType::LinearDepth), "SceneLinearDepth", ETextureType::Override);
scenedepth->SetUpscaleFlag(false, true);
AddGameTexture(scenedepth);
}
void FTextureManager::AddTextures(void (*progressFunc_)(), void (*checkForHacks)(BuildInfo&), void (*customtexturehandler)())

View file

@ -268,7 +268,7 @@ public:
Height = BaseTexture->GetHeight();
}
// This is only used for the null texture and for Heretic's skies.
// This is only used for the null texture, for Heretic's skies and scene textures.
void SetSize(int w, int h)
{
Width = w;
@ -358,6 +358,20 @@ private:
}
};
enum class FSceneTextureType
{
None,
LinearDepth
};
// Hardware textures with data from the frame buffer
class FSceneTexture : public FTexture
{
public:
FSceneTexture(FSceneTextureType type);
FSceneTextureType Type = {};
};
// A wrapper around a hardware texture, to allow using it in the 2D drawing interface.
class FWrapperTexture : public FTexture

View file

@ -1126,7 +1126,10 @@ void HWDrawInfo::DrawScene(int drawmode, FRenderState& state)
applySSAO = true;
if (r_dithertransparency && vp.IsAllowedOoB())
{
vp.camera->tracer ? SetDitherTransFlags(vp.camera->tracer) : SetDitherTransFlags(players[consoleplayer].mo);
if (vp.camera->tracer)
SetDitherTransFlags(vp.camera->tracer);
else
SetDitherTransFlags(players[consoleplayer].mo);
}
}
else if (drawmode == DM_OFFSCREEN)
@ -1161,6 +1164,8 @@ void HWDrawInfo::DrawScene(int drawmode, FRenderState& state)
RenderScene(state);
screen->UpdateLinearDepthTexture();
if (applySSAO && state.GetPassType() == GBUFFER_PASS)
{
screen->AmbientOccludeScene(VPUniforms.mProjectionMatrix.get()[5]);