Skip to content

Commit

Permalink
Improve z judge of depth
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaobin Ouyang committed Jan 9, 2020
1 parent 9164836 commit 939d751
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
63 changes: 45 additions & 18 deletions Caustics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ void Caustics::onGuiRender(Gui* pGui)
pGui->endGroup();
}

if (pGui->beginGroup("Smooth Photon", false))
{
pGui->addCheckBox("Remove Isolated Photon", mRemoveIsolatedPhoton);
pGui->addCheckBox("Enable Median Filter", mMedianFilter);
pGui->addFloatVar("Normal Threshold", mNormalThreshold, 0.01f, 1.0, 0.01f);
pGui->addFloatVar("Distance Threshold", mDistanceThreshold, 0.1f, 100.0f, 0.1f);
pGui->addFloatVar("Planar Threshold", mPlanarThreshold, 0.01f, 10.0, 0.1f);
pGui->addFloatVar("Trim Direction Threshold", trimDirectionThreshold, 0, 1);
pGui->addIntVar("Min Neighbour Count", mMinNeighbourCount, 0, 8);
pGui->endGroup();
}

if (pGui->beginGroup("Photon Splat", true))
{
{
Expand All @@ -178,11 +190,25 @@ void Caustics::onGuiRender(Gui* pGui)
debugModeList.push_back({ 2, "None" });
pGui->addDropdown("Density Estimation", debugModeList, (uint32_t&)mScatterOrGather);
}
{
int oldResRatio = mCausticsMapResRatio;
Gui::DropdownList debugModeList;
debugModeList.push_back({ 1, "x 1" });
debugModeList.push_back({ 2, "x 1/2" });
debugModeList.push_back({ 4, "x 1/4" });
debugModeList.push_back({ 8, "x 1/8" });
pGui->addDropdown("Caustics Resolution", debugModeList, (uint32_t&)mCausticsMapResRatio);
if (oldResRatio != mCausticsMapResRatio)
{
createCausticsMap();
}
}
pGui->addFloatVar("Splat size", mSplatSize, 0, 100, 0.01f);
pGui->addFloatVar("Kernel Power", mKernelPower, 0.01f, 10, 0.01f);
pGui->addFloatVar("Kernel Power", mKernelPower, 0.01f, 10.f, 0.01f);

if(pGui->beginGroup("Scatter Parameters", true))
{
pGui->addFloatVar("Z Tolerance", mZTolerance, 0.001f, 1, 0.001f);
pGui->addFloatVar("Scatter Normal Threshold", mScatterNormalThreshold, 0.01f, 1.0, 0.01f);
pGui->addFloatVar("Scatter Distance Threshold", mScatterDistanceThreshold, 0.1f, 10.0f, 0.1f);
pGui->addFloatVar("Scatter Planar Threshold", mScatterPlanarThreshold, 0.01f, 10.0, 0.1f);
Expand Down Expand Up @@ -223,17 +249,6 @@ void Caustics::onGuiRender(Gui* pGui)
pGui->endGroup();
}

if (pGui->beginGroup("Smooth Photon", false))
{
pGui->addCheckBox("Remove Isolated Photon", mRemoveIsolatedPhoton);
pGui->addCheckBox("Enable Median Filter", mMedianFilter);
pGui->addFloatVar("Normal Threshold", mNormalThreshold, 0.01f, 1.0, 0.01f);
pGui->addFloatVar("Distance Threshold", mDistanceThreshold, 0.1f, 100.0f, 0.1f);
pGui->addFloatVar("Planar Threshold", mPlanarThreshold, 0.01f, 10.0, 0.1f);
pGui->addFloatVar("Trim Direction Threshold", trimDirectionThreshold, 0, 1);
pGui->addIntVar("Min Neighbour Count", mMinNeighbourCount, 0, 8);
pGui->endGroup();
}
mLightDirection = vec3(
cos(mLightAngle.x) * sin(mLightAngle.y),
cos(mLightAngle.y),
Expand Down Expand Up @@ -365,6 +380,16 @@ void Caustics::saveSceneSetting(std::string path)
file << camTarget.x << " " << camTarget.y << " " << camTarget.z << std::endl;
}

void Caustics::createCausticsMap()
{
uint32_t width = mpRtOut->getWidth();
uint32_t height = mpRtOut->getHeight();
uint2 dim(width / mCausticsMapResRatio, height / mCausticsMapResRatio);
auto pPhotonMapTex = Texture::create2D(dim.x, dim.y, ResourceFormat::RGBA16Float, 1, 1, nullptr, Resource::BindFlags::RenderTarget | Resource::BindFlags::ShaderResource | Resource::BindFlags::UnorderedAccess);
auto depthTex = Texture::create2D(dim.x, dim.y, ResourceFormat::D24UnormS8, 1, 1, nullptr, Resource::BindFlags::DepthStencil);
mpCausticsFbo = Fbo::create({ pPhotonMapTex }, depthTex);
}

void Caustics::loadShader()
{
// raytrace
Expand Down Expand Up @@ -448,7 +473,7 @@ void Caustics::loadShader()
BlendState::SharedPtr scatterBlendState = BlendState::create(blendDesc);
mpPhotonScatterProgram = GraphicsProgram::createFromFile("PhotonScatter.ps.hlsl", "photonScatterVS", "photonScatterPS");
DepthStencilState::Desc dsDesc;
dsDesc.setDepthEnabled(true);
dsDesc.setDepthEnabled(false);
dsDesc.setDepthWriteMask(false);
auto depthStencilState = DepthStencilState::create(dsDesc);
RasterizerState::Desc rasterDesc;
Expand All @@ -467,7 +492,6 @@ void Caustics::loadShader()
mpPhotonScatterBlendState->setDepthStencilState(depthStencilState);
mpPhotonScatterNoBlendState->setRasterizerState(rasterState);
mpPhotonScatterVars = GraphicsVars::create(mpPhotonScatterProgram->getReflector());
//mpPhotonScatterPass = RasterScenePass::create(mpScene, "PhotonScatter.ps.hlsl", "photonScatterVS", "photonScatterPS");
}

mpRtRenderer = RtSceneRenderer::create(mpScene);
Expand Down Expand Up @@ -690,9 +714,11 @@ void Caustics::renderRT(RenderContext* pContext, Fbo::SharedPtr pTargetFbo)
//pContext->clearFbo(mpCausticsFbo.get(), vec4(0, 0, 0, 0), 1.0, 0);
pContext->clearRtv(mpCausticsFbo->getColorTexture(0)->getRTV().get(), vec4(0, 0, 0, 0));
glm::mat4 wvp = mpCamera->getProjMatrix() * mpCamera->getViewMatrix();
glm::mat4 invP = glm::inverse(mpCamera->getProjMatrix());
ConstantBuffer::SharedPtr pPerFrameCB = mpPhotonScatterVars["PerFrameCB"];
pPerFrameCB["gWorldMat"] = glm::mat4();
pPerFrameCB["gWvpMat"] = wvp;
pPerFrameCB["gInvProjMat"] = invP;
pPerFrameCB["gEyePosW"] = mpCamera->getPosition();
pPerFrameCB["gSplatSize"] = mSplatSize;
pPerFrameCB["gPhotonMode"] = mPhotonMode;
Expand All @@ -703,14 +729,15 @@ void Caustics::renderRT(RenderContext* pContext, Fbo::SharedPtr pTargetFbo)
pPerFrameCB["screenDim"] = int2(mpDepthTex->getWidth(), mpDepthTex->getHeight());
pPerFrameCB["normalThreshold"] = mScatterNormalThreshold;
pPerFrameCB["distanceThreshold"] = mScatterDistanceThreshold;
pPerFrameCB["planarThreshold"] = mScatterPlanarThreshold;
pPerFrameCB["planarThreshold"] = mScatterPlanarThreshold;
pPerFrameCB["gMaxAnisotropy"] = mMaxAnisotropy;
pPerFrameCB["gCameraPos"] = mpCamera->getPosition();
pPerFrameCB["gZTolerance"] = mZTolerance;
//pPerFrameCB["gMaxScreenRadius"] = mMaxPhotonPixelRadius;
mpPhotonScatterVars["gLinearSampler"] = mpLinearSampler;
mpPhotonScatterVars->setStructuredBuffer("gPhotonBuffer", photonBuffer);
mpPhotonScatterVars->setStructuredBuffer("gRayTask", mpPixelInfoBuffer);
//mpPhotonScatterVars->setTexture("gDepthTex", mpGPassFbo->getDepthStencilTexture());
mpPhotonScatterVars->setTexture("gDepthTex", mpGPassFbo->getDepthStencilTexture());
mpPhotonScatterVars->setTexture("gNormalTex", mpGPassFbo->getColorTexture(0));
mpPhotonScatterVars->setTexture("gDiffuseTex", mpGPassFbo->getColorTexture(1));
mpPhotonScatterVars->setTexture("gSpecularTex", mpGPassFbo->getColorTexture(2));
Expand Down Expand Up @@ -830,6 +857,7 @@ void Caustics::renderRT(RenderContext* pContext, Fbo::SharedPtr pTargetFbo)
pCompCB["gNumLights"] = mpScene->getLightCount();
pCompCB["gDebugMode"] = (uint32_t)mDebugMode;
pCompCB["gInvWvpMat"] = mpCamera->getInvViewProjMatrix();
pCompCB["gInvPMat"] = glm::inverse(mpCamera->getProjMatrix());
pCompCB["gCameraPos"] = mpCamera->getPosition();
pCompCB["screenDim"] = int2(mpRtOut->getWidth(), mpRtOut->getHeight());
pCompCB["dispatchSize"] = int2(mDispatchSize, mDispatchSize);
Expand Down Expand Up @@ -912,8 +940,7 @@ void Caustics::onResizeSwapChain(uint32_t width, uint32_t height)

mpRtOut = Texture::create2D(width, height, ResourceFormat::RGBA16Float, 1, 1, nullptr, Resource::BindFlags::UnorderedAccess | Resource::BindFlags::ShaderResource);
mpDepthTex = Texture::create2D(width, height, ResourceFormat::D24UnormS8, 1, 1, nullptr, Resource::BindFlags::DepthStencil | Resource::BindFlags::ShaderResource);
mpPhotonMapTex = Texture::create2D(width, height, ResourceFormat::RGBA16Float, 1, 1, nullptr, Resource::BindFlags::RenderTarget | Resource::BindFlags::ShaderResource | Resource::BindFlags::UnorderedAccess);
mpCausticsFbo = Fbo::create({ mpPhotonMapTex }, mpDepthTex);
createCausticsMap();

mpRayDensityTex = Texture::create2D(CAUSTICS_MAP_SIZE, CAUSTICS_MAP_SIZE, ResourceFormat::RGBA16Float, 1, 1, nullptr, Resource::BindFlags::RenderTarget | Resource::BindFlags::ShaderResource | Resource::BindFlags::UnorderedAccess);
mpPhotonCountTex = Texture::create1D(width, ResourceFormat::R32Uint, 1, 1, nullptr, Resource::BindFlags::ShaderResource | Resource::BindFlags::UnorderedAccess);
Expand Down
4 changes: 3 additions & 1 deletion Caustics.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Caustics : public IRenderer
SCATTER_GEOMETRY_SPHERE = 1,
};
ScatterGeometry mScatterGeometry= SCATTER_GEOMETRY_QUAD;
int mCausticsMapResRatio = 1;
int mScatterOrGather = 0;
float mSplatSize = 4.0f;
float mKernelPower = 1.f;
Expand All @@ -95,6 +96,7 @@ class Caustics : public IRenderer
float mScatterPlanarThreshold = 2.0f;
float mMaxAnisotropy = 20.0f;
float mMaxPhotonPixelRadius = 90.0f;
float mZTolerance = 0.1f;

// Photon Gather
int mTileCountScale = 10;
Expand Down Expand Up @@ -138,7 +140,6 @@ class Caustics : public IRenderer
Texture::SharedPtr mpDiffuseTex;
Texture::SharedPtr mpSpecularTex;
Texture::SharedPtr mpDepthTex;
Texture::SharedPtr mpPhotonMapTex;
Fbo::SharedPtr mpGPassFbo;

// photon trace
Expand Down Expand Up @@ -227,4 +228,5 @@ class Caustics : public IRenderer
PhotonTraceShader getPhotonTraceShader();
void loadSceneSetting(std::string path);
void saveSceneSetting(std::string path);
void createCausticsMap();
};
9 changes: 8 additions & 1 deletion Data/Composite.ps.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ cbuffer PerImageCB
// Lighting params
LightData gLightData[16];
float4x4 gInvWvpMat;
float4x4 gInvPMat;

float3 gCameraPos;
uint gNumLights;
Expand All @@ -89,7 +90,13 @@ float4 main(float2 texC : TEXCOORD) : SV_TARGET

float4 color = 0;
if (gDebugMode == ShowDepth)
color = depth;
{
float4 viewPnt = mul(screenPnt, gInvPMat);
viewPnt /= viewPnt.w;
float viewDepth = (gInvPMat[2][2] * depth + gInvPMat[3][2]) / (gInvPMat[2][3] * depth + gInvPMat[3][3]);
color = float4(abs(viewDepth.xxx)*0.01, 1);
//color = float4(abs(viewPnt.zzz) * 0.01, 1);// length(viewPnt.xyz - gCameraPos) * 0.01;
}
else if (gDebugMode == ShowNormal)
color = normalVal;
else if (gDebugMode == ShowDiffuse)
Expand Down
24 changes: 18 additions & 6 deletions Data/PhotonScatter.ps.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ cbuffer PerFrameCB : register(b0)
{
float4x4 gWvpMat;
float4x4 gWorldMat;
float4x4 gInvProjMat;

float3 gEyePosW;
float gLightIntensity;
Expand All @@ -69,6 +70,8 @@ cbuffer PerFrameCB : register(b0)

float3 gCameraPos;
float gMaxScreenRadius;

float gZTolerance;
};
#define AnisotropicPhoton 0
#define IsotropicPhoton 1
Expand Down Expand Up @@ -311,18 +314,27 @@ PhotonVSOut photonScatterVS(PhotonVSIn vIn)
return vOut;
}

float toViewSpace(float depth, float w)
{
return (gInvProjMat[2][2] * depth + gInvProjMat[3][2] * w) / (gInvProjMat[2][3] * depth + gInvProjMat[3][3] * w);
}

float4 photonScatterPS(PhotonVSOut vOut) : SV_TARGET
{
//float depth = gDepthTex.Load(int3(vOut.posH.xy, 0)).x;
float depth = gDepthTex.Load(int3(vOut.posH.xy, 0)).x;
depth = toViewSpace(depth,1);

float pixelZ = -vOut.posH.w;// toViewSpace(vOut.posH.z, vOut.posH.w);
if (gShowPhoton == SHOW_PHOTON_SHADED)
{
return float4(1, 1, 0, 1)* vOut.color;
}

//if (abs(depth- vOut.posH.z) > 0.00001)
//{
// //discard;
//}
float zDiff = pixelZ - depth;
if (abs(zDiff) > gZTolerance)
{
discard;
}

float alpha;
if (gShowPhoton == SHOW_PHOTON_SOLID || gPhotonMode == PhotonMesh || gPhotonMode == ScreenDot)
Expand All @@ -331,7 +343,7 @@ float4 photonScatterPS(PhotonVSOut vOut) : SV_TARGET
}
else
{
alpha = smoothKernel(length(vOut.texcoord.xy));//gGaussianTex.Sample(gLinearSampler, vOut.texcoord).r;
alpha = smoothKernel(length(float3(vOut.texcoord.xy, zDiff)));//gGaussianTex.Sample(gLinearSampler, vOut.texcoord).r;
alpha = pow(alpha, gKernelPower);
}
return float4(vOut.color.rgb * alpha, 1);
Expand Down

0 comments on commit 939d751

Please sign in to comment.