-
Notifications
You must be signed in to change notification settings - Fork 138
feat(sss): add burley normalized sss #1393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
7c85bc4
add burley
jiayev 492dc96
using diffuse irradiance to do ssss
jiayev b9337e7
fix burley and use diffuse irradiance for sss
jiayev af69881
use diffuse irradiance for skin
jiayev 6536dd7
set default settings
jiayev 29f8db7
would this fix vr?
jiayev 32d341d
add rtv for sss
jiayev c529fd8
set blend state
jiayev ad9020d
skin decal skip sss
jiayev 2773fdc
skin decal skip sss 2
jiayev 6b24a2f
fix compile issue
jiayev f1d06b8
fix darkened decal
jiayev 8c08de9
revert rtv changes
jiayev 210dbfc
revert deferred changes
jiayev e7a93dc
raise sss epsilon to 1e-3
jiayev 662a2ef
remove exception for skin decals
jiayev 1a30f8e
rewrite burley
jiayev 7df2bf5
add info and fix logic
jiayev bfd49cc
remove bleed sum as it doesn't work well
jiayev d0b1c07
match srv clean nullptrs
jiayev 07436ff
refactors
jiayev dc2a76a
add info and use ARRAYSIZE
jiayev 1682e1e
fix typo
jiayev 72c1271
remove redundant variables
jiayev b365cf0
revert unnecassary change
jiayev 70f833e
fix multiple stuff
jiayev 9188641
use pow
jiayev 1d92ee7
fix typo
jiayev 1e162d7
revert ssss mode to prevent artifacts
jiayev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
features/Subsurface Scattering/Shaders/SubsurfaceScattering/Burley.hlsli
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| #include "Common/GBuffer.hlsli" | ||
| #include "Common/Game.hlsli" | ||
| #include "Common/SharedData.hlsli" | ||
| #include "Common/Random.hlsli" | ||
| #include "Common/Math.hlsli" | ||
|
|
||
| // [Per H. Christensen, Brent Burley 2015, "Approximate Reflectance Profiles for Efficient Subsurface Scattering"] | ||
| // https://graphics.pixar.com/library/ApproxBSSRDF/paper.pdf | ||
| float3 GetBurleyCDF(float3 d, float3 r, float rand) | ||
| { | ||
| return 1 - 0.25 * exp(-r / d) - 0.75 * exp(-r / (3 * d)) - rand; | ||
| } | ||
|
|
||
| float GetBurleyPDF(float r, float l, float s) | ||
| { | ||
| float d = l / s; | ||
| float pdf = 0.25 / d * (exp(-r / d) + exp(-r / (3 * d))); // cdf dr | ||
| return max(pdf, 1e-5f); | ||
| } | ||
|
|
||
| // [Tiantian Xie et al. 2020, "Real-time subsurface scattering with single pass variance-guided adaptive importance sampling"] | ||
| // https://thisistian.github.io/publication/spvg_xie_2020_I3D_small.pdf | ||
| // Also check https://zero-radiance.github.io/post/sampling-diffusion/ | ||
| float RadiusApprox(float d, float rand) | ||
| { | ||
| // g(ξ) = d((2 − c)ξ − 2)log(1 − ξ) | ||
| // minimal mean squared error when c = 2.5715 | ||
| return d * ((2 - 2.5715f) * rand - 2) * log(1 - rand); | ||
| } | ||
|
|
||
| float3 GetBurleyProfile(float3 l, float3 s, float radius) | ||
| { | ||
| // R(d,r) = \frac{e^{-r/d}+e^{-r/(3d)}}{8*pi*d*r} | ||
| float3 d = 1.f / s; | ||
| float3 r = radius / l; | ||
| float3 negRbyD = -r / d; | ||
| return max((exp(negRbyD) + exp(negRbyD / 3.0f)) / (d * l * 8 * Math::PI), 1e-12f); | ||
| } | ||
|
|
||
| float3 GetScalingFactor(float3 albedo) | ||
| { | ||
| // we have three methods for calculating the scaling factor | ||
| // d = l / (1.85 − A + 7|A − 0.8|^3) | ||
| // d = l / (1.9 − A + 3.5(A − 0.8)^2) | ||
| // d = l / (3.5 + 100(A − 0.33)^4) | ||
| // here we choose the third to use diffuse mean free path as parameter. | ||
| float3 value = albedo - 0.33f; | ||
| return 3.5f + 100.f * pow(abs(value), 4); | ||
| } | ||
|
|
||
| float4 BurleyNormalizedSS(uint2 DTid, float2 texCoord, uint eyeIndex, float sssAmount, bool humanProfile) | ||
| { | ||
| float centerDepth = SharedData::GetScreenDepth(DepthTexture[DTid].x); | ||
|
|
||
| float4 centerColor = ColorTexture[DTid]; | ||
| if (sssAmount == 0 || centerDepth <= 0) | ||
| { | ||
| return centerColor; | ||
| } | ||
|
|
||
| float4 surfaceAlbedo = AlbedoTexture[DTid]; | ||
| float3 originalColor = Color::GammaToLinear(centerColor.xyz / max(surfaceAlbedo.xyz, EPSILON_SSS_ALBEDO)); | ||
|
|
||
| float4 diffuseMeanFreePath = humanProfile ? MeanFreePathHuman : MeanFreePathBase; | ||
| diffuseMeanFreePath.xyz = float3(max(diffuseMeanFreePath.x, 1e-5f), max(diffuseMeanFreePath.y, 1e-5f), max(diffuseMeanFreePath.z, 1e-5f)); | ||
| diffuseMeanFreePath *= sssAmount; | ||
|
|
||
| float dmfpForSampling = diffuseMeanFreePath.w; | ||
| float s = GetScalingFactor(surfaceAlbedo.www).x; | ||
| float d = dmfpForSampling / s; | ||
| float3 s3d = GetScalingFactor(surfaceAlbedo.xyz); | ||
| float3 d3d = diffuseMeanFreePath.xyz * dmfpForSampling / s3d; | ||
|
|
||
| const float3 normalVS = GBuffer::DecodeNormal(NormalTexture[DTid].xy); | ||
| const float3 normalWS = normalize(mul(FrameBuffer::CameraViewInverse[eyeIndex], float4(normalVS, 0)).xyz); | ||
|
|
||
| float3 weightSum = 0.0f; | ||
| float3 colorSum = 0.0f; | ||
|
|
||
| float2 uvScale = (GAME_UNIT_TO_CM * 0.1f * (0.5f / tan(0.5 * radians(SSSS_FOVY)))) / centerDepth; // Scale in mm | ||
|
|
||
| // center sample weight | ||
| float centerRadius = 0.5f * (SharedData::BufferDim.z / uvScale.x + SharedData::BufferDim.w / uvScale.y); | ||
| float centerRadiusCDF = GetBurleyCDF(d, centerRadius, 0).x; | ||
| float3 centerWeight = GetBurleyCDF(d3d, centerRadius, 0); | ||
|
|
||
| int3 seed = int3(DTid.xy, 0); | ||
| int seedStart = Random::pcg3d(int3(seed.xy, SharedData::FrameCount)).x; | ||
|
|
||
| [loop] | ||
| for (int i = 0; i < BurleySamples; ++i) | ||
| { | ||
| seed.z = seedStart++; | ||
| float2 rand = float2(Random::pcg3d(seed).xy) / 4294967296.0f; // to [0, 1) | ||
|
|
||
| rand.x = centerRadiusCDF + rand.x * (1.0f - centerRadiusCDF); | ||
|
|
||
| // generate radius & angle for sampling | ||
| float radius = max(RadiusApprox(d, rand.x), 1e-5f); | ||
| float theta = rand.y * 2.0f * Math::PI; | ||
|
|
||
| float pdf = GetBurleyPDF(radius, dmfpForSampling, s); | ||
|
|
||
| float2 uvOffset = uvScale * radius; | ||
| uvOffset.x *= cos(theta); | ||
| uvOffset.y *= sin(theta); | ||
|
|
||
| float2 sampleUV = texCoord + uvOffset; | ||
| float2 clampedUV = clamp(sampleUV, float2(0.0f, 0.0f), float2(1.0f, 1.0f)); | ||
| uint2 samplePixcoord = uint2(clampedUV * SharedData::BufferDim.xy); | ||
| float3 sampleColor = Color::GammaToLinear(ColorTexture[samplePixcoord].xyz / max(AlbedoTexture[samplePixcoord].xyz, EPSILON_SSS_ALBEDO)); | ||
| float sampleDepth = SharedData::GetScreenDepth(DepthTexture[samplePixcoord].x); | ||
| float3 sampleNormalVS = GBuffer::DecodeNormal(NormalTexture[samplePixcoord].xy); | ||
| float3 sampleNormalWS = normalize(mul(FrameBuffer::CameraViewInverse[eyeIndex], float4(sampleNormalVS, 0)).xyz); | ||
|
|
||
| float deltaDepth = (sampleDepth - centerDepth) * 10.f / GAME_UNIT_TO_CM; // convert to mm | ||
| float radiusSampledInMM = sqrt(radius * radius + deltaDepth * deltaDepth); | ||
|
|
||
| float maskSample = MaskTexture[samplePixcoord].x; | ||
| bool mask = maskSample > 1e-5f; | ||
|
|
||
| float3 diffusionProfile = GetBurleyProfile(diffuseMeanFreePath.xyz, s3d, radiusSampledInMM); | ||
| float normalWeight = sqrt(saturate(dot(sampleNormalWS, normalWS) * 0.5f + 0.5f)); | ||
| float3 sampleWeight = mask ? (diffusionProfile / pdf) * normalWeight : 0.0f; | ||
|
|
||
| colorSum += sampleWeight * sampleColor; | ||
| weightSum += sampleWeight; | ||
| } | ||
|
|
||
| colorSum *= any(weightSum == 0.0f) ? 0.0f : (1.0f / weightSum); | ||
| colorSum = lerp(colorSum, originalColor, saturate(centerWeight)); | ||
| float3 color = Color::LinearToGamma(colorSum) * AlbedoTexture[DTid.xy].xyz; | ||
|
|
||
| float4 outColor = float4(color, ColorTexture[DTid.xy].w); | ||
| return outColor; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.