From 8c128c9c659f497192016a187ba46bd2ab0fded4 Mon Sep 17 00:00:00 2001 From: marclt Date: Mon, 18 Oct 2021 16:34:35 -0400 Subject: [PATCH 01/29] Merge changes from SensorSDK2021.1 --- .../Runtime/Material/Lit/Lit.shader | 1 + .../Runtime/Material/Lit/LitPathTracing.hlsl | 10 ++ .../Runtime/Material/Unlit/Unlit.shader | 2 + .../Runtime/PackageInfo.cs | 2 + .../Runtime/RenderPipeline/Camera/HDCamera.cs | 11 ++ .../HDRenderPipeline.PostProcess.cs | 6 +- .../RenderPipeline/PathTracing/PathTracing.cs | 24 +++- .../Shaders/PathTracingIntersection.hlsl | 11 ++ .../PathTracing/Shaders/PathTracingLight.hlsl | 113 ++++++++++++++++++ .../Raytracing/HDRaytracingManager.cs | 4 + .../RenderPass/CustomPass/CustomPassVolume.cs | 8 +- .../ShaderPass/ShaderPassPathTracing.hlsl | 43 +++++++ 12 files changed, 229 insertions(+), 6 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader index 5501967ffcf..77ac8e14db4 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader @@ -1243,6 +1243,7 @@ Shader "HDRP/Lit" #pragma raytracing surface_shader #pragma multi_compile _ DEBUG_DISPLAY + #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE #define SHADERPASS SHADERPASS_PATH_TRACING diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl index 53a9b66f9b2..dd1491a1278 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl @@ -63,13 +63,23 @@ bool CreateMaterialData(PathIntersection pathIntersection, BuiltinData builtinDa { float NdotV = dot(GetSpecularNormal(mtlData), mtlData.V); float Fcoat = F_Schlick(CLEAR_COAT_F0, NdotV); + +#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) + float Fspec = F_Schlick(mtlData.bsdfData.fresnel0, NdotV).x; +#else float Fspec = Luminance(F_Schlick(mtlData.bsdfData.fresnel0, NdotV)); +#endif mtlData.bsdfWeight[1] = Fcoat * mtlData.bsdfData.coatMask; coatingTransmission = 1.0 - mtlData.bsdfWeight[1]; mtlData.bsdfWeight[2] = coatingTransmission * lerp(Fspec, 0.5, 0.5 * (mtlData.bsdfData.roughnessT + mtlData.bsdfData.roughnessB)) * GetSpecularCompensation(mtlData); mtlData.bsdfWeight[3] = (coatingTransmission - mtlData.bsdfWeight[2]) * mtlData.bsdfData.transmittanceMask; + +#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) + mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * mtlData.bsdfData.diffuseColor * mtlData.bsdfData.ambientOcclusion; +#else mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * Luminance(mtlData.bsdfData.diffuseColor) * mtlData.bsdfData.ambientOcclusion; +#endif } #ifdef _SURFACE_TYPE_TRANSPARENT else // Below diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader index 78916d0c33b..a48b95e6312 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader @@ -601,6 +601,8 @@ Shader "HDRP/Unlit" #define SHADER_UNLIT #define HAS_LIGHTLOOP // Used when computing volumetric scattering + #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingMacros.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" diff --git a/com.unity.render-pipelines.high-definition/Runtime/PackageInfo.cs b/com.unity.render-pipelines.high-definition/Runtime/PackageInfo.cs index 1031875d4ef..f3bd69a6fac 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PackageInfo.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PackageInfo.cs @@ -5,3 +5,5 @@ [assembly: InternalsVisibleTo("Unity.GraphicTests.Performance.HDRP.Runtime")] [assembly: InternalsVisibleTo("Unity.GraphicTests.Performance.HDRP.Editor")] [assembly: InternalsVisibleTo("TestRuntime")] +[assembly: InternalsVisibleTo("Unity.SensorSDK.Runtime")] +[assembly: InternalsVisibleTo("Unity.SensorSDK.Editor")] \ No newline at end of file diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs index f77441ad13f..0b804a1cf97 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs @@ -250,6 +250,17 @@ internal struct VolumetricCloudsAnimationData public float verticalErosionOffset; } +//SensorSDK - Begin - Tonemapping support + public RayTracingAccelerationStructure accelerationStructure; + + public RayTracingShader pathTracingShaderOverride; + public delegate void PrepareDispatchRaysAction(T1 cmd); + public PrepareDispatchRaysAction PrepareDispatchRays; + + public bool isContinousCaptureEnabled = true; + public bool isLastIteration = false; +//SensorSDK - End - Tonemapping support + internal Vector4[] frustumPlaneEquations; internal int taaFrameIndex; internal float taaSharpenStrength; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs index 253c09549bc..9427540fef0 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.PostProcess.cs @@ -121,7 +121,7 @@ public PostProcessTextureAllocator() Vignette m_Vignette; Tonemapping m_Tonemapping; WhiteBalance m_WhiteBalance; - ColorAdjustments m_ColorAdjustments; + internal ColorAdjustments m_ColorAdjustments; ChannelMixer m_ChannelMixer; SplitToning m_SplitToning; LiftGammaGain m_LiftGammaGain; @@ -3885,7 +3885,7 @@ void DispatchWithGuardBands(CommandBuffer cmd, ComputeShader shader, int kernelI #endregion #region Color Grading - class ColorGradingPassData + internal class ColorGradingPassData { public ComputeShader builderCS; public int builderKernel; @@ -3921,7 +3921,7 @@ class ColorGradingPassData public TextureHandle logLut; } - void PrepareColorGradingParameters(ColorGradingPassData passData) + internal void PrepareColorGradingParameters(ColorGradingPassData passData) { passData.tonemappingMode = m_TonemappingFS ? m_Tonemapping.mode.value : TonemappingMode.None; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs index 015b580c8a3..fe6c403b938 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs @@ -188,6 +188,17 @@ private CameraData CheckDirtiness(HDCamera hdCamera, int camID, CameraData camDa return ResetPathTracing(camID, camData); } +//SensorSDK - Begin + hdCamera.isLastIteration = camData.currentIteration == m_SubFrameManager.subFrameCount; + + if (Application.isPlaying && hdCamera.isLastIteration && hdCamera.isContinousCaptureEnabled) + { + camData.ResetIteration(); + m_SubFrameManager.SetCameraData(camID, camData); + return ResetPathTracing(camID, camData); + } +//SensorSDK - End + // Check camera matrix dirtiness if (hdCamera.mainViewConstants.nonJitteredViewProjMatrix != (hdCamera.mainViewConstants.prevViewProjMatrix)) { @@ -259,13 +270,21 @@ class RenderPathTracingData public TextureHandle output; public TextureHandle sky; + +//SensorSDK - Begin + public HDCamera hdCamera; +//SensorSDK - End } TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in CameraData cameraData, TextureHandle pathTracingBuffer, TextureHandle skyBuffer) { using (var builder = renderGraph.AddRenderPass("Render PathTracing", out var passData)) { - passData.pathTracingShader = m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; +//SensorSDK - Begin + passData.pathTracingShader = hdCamera.pathTracingShaderOverride == null ? m_GlobalSettings.renderPipelineRayTracingResources.pathTracing : hdCamera.pathTracingShaderOverride; + passData.hdCamera = hdCamera; +//SensorSDK - End + passData.cameraData = cameraData; passData.ditheredTextureSet = GetBlueNoiseManager().DitheredTextureSet256SPP(); passData.backgroundColor = hdCamera.backgroundColorHDR; @@ -321,6 +340,9 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C ctx.cmd.SetRayTracingMatrixParam(data.pathTracingShader, HDShaderIDs._PixelCoordToViewDirWS, data.pixelCoordToViewDirWS); ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracedDoFConstants, data.dofParameters); +//SensorSDK - Begin + data.hdCamera.PrepareDispatchRays?.Invoke(ctx.cmd); +//SensorSDK - End // Run the computation ctx.cmd.DispatchRays(data.pathTracingShader, "RayGen", (uint)data.width, (uint)data.height, 1); }); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl index b803427b1b7..40a68fb949b 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl @@ -20,6 +20,17 @@ struct PathIntersection uint2 pixelCoord; // Max roughness encountered along the path float maxRoughness; + +// SensorSDK - Begin +#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) + // Extra params for beam lighthing + float3 beamDirection; + float3 beamOrigin; + + float beamRadius; + float beamDepth; +#endif +//SensorSDK - End }; #endif // UNITY_PATH_TRACING_INTERSECTION_INCLUDED diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl index 54d3678e2d9..2127717711f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl @@ -296,6 +296,119 @@ float3 GetAreaEmission(LightData lightData, float centerU, float centerV, float return emission; } +#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) +/* +struct LightData +{ + float3 positionRWS; + uint lightLayers; + float lightDimmer; + float volumetricLightDimmer; + real angleScale; + real angleOffset; + float3 forward; + int lightType; + float3 right; + real range; + float3 up; + float rangeAttenuationScale; + float3 color; + float rangeAttenuationBias; + int cookieIndex; + int tileCookie; + int shadowIndex; + int contactShadowMask; + float3 shadowTint; + float shadowDimmer; + float volumetricShadowDimmer; + int nonLightMappedOnly; + real minRoughness; + int screenSpaceShadowIndex; + real4 shadowMaskSelector; + real4 size; + float diffuseDimmer; + float specularDimmer; + float isRayTracedContactShadow; + float penumbraTint; + float3 padding; + float boxLightSafeExtent; +}; +*/ + +bool SampleBeam( + LightData lightData, + float3 position, + float3 normal, + out float3 outgoingDir, + out float3 value, + out float pdf, + out float dist, + inout PathIntersection payload) +{ + const float MM_TO_M = 1e-3; + const float M_TO_MM = 1e3; + + float3 lightDirection = payload.beamDirection; + float3 lightPosition = payload.beamOrigin; + + outgoingDir = position - lightPosition; + dist = length(outgoingDir); + outgoingDir /= dist; + + float apertureRadius = lightData.size.x; + float w0 = lightData.size.y; + float zr = lightData.size.z; + float distToWaist = lightData.size.w; + + // get the hit point in the coordinate frame of the laser as a depth(z) and + // radial measure(r) + float ctheta = dot(lightDirection, outgoingDir); + float zFromAperture = ctheta * dist; + float rSq = Sq(dist) - Sq(zFromAperture); + float3 radialDirection = dist * outgoingDir - zFromAperture*lightDirection; + + float zFromWaist = abs(zFromAperture * M_TO_MM - distToWaist); + if (dot(normal, -outgoingDir) < 0.001) + return false; + + // Total beam power, note: different from the output here which is irradiance. + float P = lightData.color.x; + + const float zRatio = Sq(zFromWaist / zr); + const float wz = w0 * sqrt(1 + zRatio) * MM_TO_M; + const float Eoz = 2 * P; + const float wzSq = wz*wz; + + float gaussianFactor = exp(-2 * rSq / wzSq) / (PI * wzSq); // 1/m^2 + value = gaussianFactor * Eoz; // W/m^2 + + payload.beamRadius = wz; + payload.beamDepth = zFromAperture; + +#if 0 /*Debug values*/ + payload.diffuseColor = float3(ctheta, zFromAperture, rSq); + payload.fresnel0 = float3(distToWaist, w0, zr); + payload.transmittance = float3(zFromWaist, zRatio, wzSq); + payload.tangentWS = float3(Eoz, wzSq, gaussianFactor); +#endif + + // sampling a point in the "virtual" aperture + // Find the actual point in the beam aperture that corresponds to this point + float rRatio = apertureRadius / wz; + float3 pAperture = lightPosition + rRatio * radialDirection; // location of the point in the aperture + + outgoingDir = pAperture - position; // corrected outgoing vector using the assumption below + dist = length(outgoingDir); + outgoingDir /= dist; + + // assumption that the interaction point is only illuminated by + // one point in the laser aperture. + pdf = 1.0f; + + return any(value); +} +#endif + bool SampleLights(LightList lightList, float3 inputSample, float3 position, diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingManager.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingManager.cs index 4a051a78d39..72525b262d9 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingManager.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/HDRaytracingManager.cs @@ -367,6 +367,10 @@ internal void BuildRayTracingAccelerationStructure(HDCamera hdCamera) if (!hdCamera.frameSettings.IsEnabled(FrameSettingsField.RayTracing)) return; +//SensorSDK - Begin - Expose acceleration structure to avoid building our own into the custom pass + hdCamera.accelerationStructure = m_CurrentRAS; +//SensorSDK - End - Expose acceleration structure to avoid building our own into the custom pass + // We only support ray traced shadows if the camera supports ray traced shadows bool screenSpaceShadowsSupported = hdCamera.frameSettings.IsEnabled(FrameSettingsField.ScreenSpaceShadows); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassVolume.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassVolume.cs index 964499d3c70..8a02d45fc1a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassVolume.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassVolume.cs @@ -389,9 +389,13 @@ public CustomPass AddPassOfType(Type passType) return customPass; } -#if UNITY_EDITOR +//SensorSDK - Begin - Fix vfx collider not taken into account when created at runtime. +//#if UNITY_EDITOR +//SensorSDK - Begin - Fix vfx collider not taken into account when created at runtime. // In the editor, we refresh the list of colliders at every frame because it's frequent to add/remove them void Update() => GetComponents(m_Colliders); -#endif +//SensorSDK - Begin - Fix vfx collider not taken into account when created at runtime. +//#endif +//SensorSDK - Begin - Fix vfx collider not taken into account when created at runtime. } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl index e6cfe902429..4ddb5e75899 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl @@ -10,6 +10,16 @@ #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingSampling.hlsl" #endif +#ifdef SENSORSDK_OVERRIDE_REFLECTANCE + +TEXTURE2D(_SensorCustomReflectance); +float Wavelength; +#endif + +#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) +int _SensorLightCount; +#endif + float PowerHeuristic(float f, float b) { return Sq(f) / (Sq(f) + Sq(b)); @@ -117,6 +127,18 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa BSDFData bsdfData = ConvertSurfaceDataToBSDFData(posInput.positionSS, surfaceData); #ifndef SHADER_UNLIT + //We override the diffuce color when we are using standard lit shader. Don't need to when using shader graph. +#ifdef SENSORSDK_OVERRIDE_REFLECTANCE + //bsdfData.diffuseColor = float3(_SensorCustomReflectance, _SensorCustomReflectance, _SensorCustomReflectance); //Override diffuse with material reflectance + const float _minWaveLengthValue = 0.35f; // 350 nm + const float _maxWaveLengthValue = 2.5f; // 2500 nm + + float wlIdx = clamp(Wavelength * 0.001f, _minWaveLengthValue, _maxWaveLengthValue); + float wavelengthSpan = _maxWaveLengthValue - _minWaveLengthValue + 1; + float2 coordCurve = float2(wlIdx / wavelengthSpan, 0); + + bsdfData.diffuseColor = SAMPLE_TEXTURE2D(_SensorCustomReflectance, s_linear_clamp_sampler, coordCurve); +#endif // Override the geometric normal (otherwise, it is merely the non-mapped smooth normal) // Also make sure that it is in the same hemisphere as the shading normal (which may have been flipped) @@ -141,7 +163,10 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa #else float3 lightNormal = GetLightNormal(mtlData); #endif + + #if !defined(SENSORSDK_SHADERGRAPH) && !defined(SENSORSDK_OVERRIDE_REFLECTANCE) LightList lightList = CreateLightList(shadingPosition, lightNormal, builtinData.renderingLayers); + #endif // Bunch of variables common to material and light sampling float pdf; @@ -154,6 +179,23 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa PathIntersection nextPathIntersection; + #if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) + pathIntersection.value = float3(0., 0., 0.); + for (uint i = 0; i < _SensorLightCount; i++) + { + if (SampleBeam(_LightDatasRT[i], rayDescriptor.Origin, bsdfData.normalWS, + rayDescriptor.Direction, value, pdf, rayDescriptor.TMax, + pathIntersection)) + { + EvaluateMaterial(mtlData, rayDescriptor.Direction, mtlResult); + + // value is in radian (w/sr) not in lumen (cd/sr) and only the r channel is used + value *= (mtlResult.diffValue + mtlResult.specValue) / pdf; + + pathIntersection.value += value; + } + } + #else // Light sampling if (computeDirect) { @@ -235,6 +277,7 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa pathIntersection.value += value * rrFactor * nextPathIntersection.value; } } + #endif } #else // SHADER_UNLIT From 409eea7ccc404b882b1ab0bda8d6c2d3184a69c9 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Wed, 3 Nov 2021 19:15:22 +0100 Subject: [PATCH 02/29] Made a few methods related to path tracing and accumulation public. --- .../Accumulation/SubFrameManager.cs | 14 +++++++++++++- .../RenderPipeline/PathTracing/PathTracing.cs | 16 +++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Accumulation/SubFrameManager.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Accumulation/SubFrameManager.cs index aed917fc501..5636d8a3862 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Accumulation/SubFrameManager.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Accumulation/SubFrameManager.cs @@ -153,7 +153,7 @@ internal void PrepareNewSubFrame() foreach (int camID in m_CameraCache.Keys.ToList()) maxIteration = Math.Max(maxIteration, GetCameraData(camID).currentIteration); - if (maxIteration == m_AccumulationSamples) + if (maxIteration >= m_AccumulationSamples) { Reset(); } @@ -264,6 +264,18 @@ public void PrepareNewSubFrame() m_SubFrameManager.PrepareNewSubFrame(); } + /// + /// Checks if the multi-frame accumulation is completed for a given camera. + /// + /// Camera for which the accumulation status is checked. + /// true if the accumulation is completed, false otherwise. + public bool IsFrameCompleted(HDCamera hdCamera) + { + int camID = hdCamera.camera.GetInstanceID(); + CameraData camData = m_SubFrameManager.GetCameraData(camID); + return camData.currentIteration >= m_SubFrameManager.subFrameCount; + } + class RenderAccumulationPassData { public ComputeShader accumulationCS; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs index 90c6372674e..5ba14611f88 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs @@ -112,12 +112,26 @@ void ReleasePathTracing() #endif // UNITY_EDITOR } - internal void ResetPathTracing() + /// + /// Resets path tracing accumulation for all cameras. + /// + public void ResetPathTracing() { m_RenderSky = true; m_SubFrameManager.Reset(); } + /// + /// Resets path tracing accumulation for a specific camera. + /// + /// Camera for which the accumulation is reset. + public void ResetPathTracing(HDCamera hdCamera) + { + int camID = hdCamera.camera.GetInstanceID(); + CameraData camData = m_SubFrameManager.GetCameraData(camID); + ResetPathTracing(camID, camData); + } + internal CameraData ResetPathTracing(int camID, CameraData camData) { m_RenderSky = true; From 7a208cb5f467e2e9ce308a1dd3dd7e2e03663c93 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Wed, 3 Nov 2021 19:49:12 +0100 Subject: [PATCH 03/29] Updated changelog. --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index ef67484ccb4..fc73bc1b665 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added new functions that sample the custom buffer in custom passes (CustomPassSampleCustomColor and CustomPassLoadCustomColor) to handle the RTHandleScale automatically. - Added new panels to Rendering Debugger Display Stats panel, displaying improved CPU/GPU frame timings and bottlenecks. - Added API to edit diffusion profiles and set IES on lights. +- Added public API to reset path tracing accumulation, and check its status. ### Fixed - Fixed decal position when created from context menu. (case 1368987) From 8c4e4abb41f82001e550e0ee4a47ddf02cf2f4a5 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Wed, 3 Nov 2021 22:39:26 +0100 Subject: [PATCH 04/29] Reverted shader files and litpathtracing changes. --- .../Runtime/Material/Lit/Lit.shader | 1 - .../Runtime/Material/Lit/LitPathTracing.hlsl | 10 ---------- .../Runtime/Material/Unlit/Unlit.shader | 2 -- 3 files changed, 13 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader index f1c9e4c2820..1b70118c6e7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader @@ -1197,7 +1197,6 @@ Shader "HDRP/Lit" #pragma raytracing surface_shader #pragma multi_compile _ DEBUG_DISPLAY - #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE #define SHADERPASS SHADERPASS_PATH_TRACING diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl index 2c9cd4fafc6..36ab798b130 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl @@ -63,23 +63,13 @@ bool CreateMaterialData(PathIntersection pathIntersection, BuiltinData builtinDa { float NdotV = dot(GetSpecularNormal(mtlData), mtlData.V); float Fcoat = F_Schlick(CLEAR_COAT_F0, NdotV); - -#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) - float Fspec = F_Schlick(mtlData.bsdfData.fresnel0, NdotV).x; -#else float Fspec = Luminance(F_Schlick(mtlData.bsdfData.fresnel0, NdotV)); -#endif mtlData.bsdfWeight[1] = Fcoat * mtlData.bsdfData.coatMask; coatingTransmission = 1.0 - mtlData.bsdfWeight[1]; mtlData.bsdfWeight[2] = coatingTransmission * lerp(Fspec, 0.5, 0.5 * (mtlData.bsdfData.roughnessT + mtlData.bsdfData.roughnessB)) * GetSpecularCompensation(mtlData); mtlData.bsdfWeight[3] = (coatingTransmission - mtlData.bsdfWeight[2]) * mtlData.bsdfData.transmittanceMask; - -#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) - mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * mtlData.bsdfData.diffuseColor * mtlData.bsdfData.ambientOcclusion; -#else mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * Luminance(mtlData.bsdfData.diffuseColor) * mtlData.bsdfData.ambientOcclusion; -#endif } #ifdef _SURFACE_TYPE_TRANSPARENT else // Below diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader index a48b95e6312..78916d0c33b 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader @@ -601,8 +601,6 @@ Shader "HDRP/Unlit" #define SHADER_UNLIT #define HAS_LIGHTLOOP // Used when computing volumetric scattering - #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingMacros.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" From 462eb5f0fcb2cb5e36939e51085ba3b2965e02b5 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Wed, 3 Nov 2021 23:02:02 +0100 Subject: [PATCH 05/29] Removing other changes. --- .../PathTracing/Shaders/PathTracingIntersection.hlsl | 11 ----------- .../RenderPass/CustomPass/CustomPassVolume.cs | 8 ++------ 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl index 40a68fb949b..b803427b1b7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl @@ -20,17 +20,6 @@ struct PathIntersection uint2 pixelCoord; // Max roughness encountered along the path float maxRoughness; - -// SensorSDK - Begin -#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) - // Extra params for beam lighthing - float3 beamDirection; - float3 beamOrigin; - - float beamRadius; - float beamDepth; -#endif -//SensorSDK - End }; #endif // UNITY_PATH_TRACING_INTERSECTION_INCLUDED diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassVolume.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassVolume.cs index 8a02d45fc1a..964499d3c70 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassVolume.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassVolume.cs @@ -389,13 +389,9 @@ public CustomPass AddPassOfType(Type passType) return customPass; } -//SensorSDK - Begin - Fix vfx collider not taken into account when created at runtime. -//#if UNITY_EDITOR -//SensorSDK - Begin - Fix vfx collider not taken into account when created at runtime. +#if UNITY_EDITOR // In the editor, we refresh the list of colliders at every frame because it's frequent to add/remove them void Update() => GetComponents(m_Colliders); -//SensorSDK - Begin - Fix vfx collider not taken into account when created at runtime. -//#endif -//SensorSDK - Begin - Fix vfx collider not taken into account when created at runtime. +#endif } } From d80a9d92e6cdfdf4859301eda6c09d26aad371c5 Mon Sep 17 00:00:00 2001 From: marclt Date: Wed, 3 Nov 2021 19:03:16 -0400 Subject: [PATCH 06/29] Remove accelerationStructure and code to know when the pathtracing frame is completed. --- .../Runtime/RenderPipeline/Camera/HDCamera.cs | 5 ----- .../Runtime/RenderPipeline/PathTracing/PathTracing.cs | 11 ----------- 2 files changed, 16 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs index d212bcc898d..05454823919 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs @@ -258,14 +258,9 @@ internal struct VolumetricCloudsAnimationData } //SensorSDK - Begin - Tonemapping support - public RayTracingAccelerationStructure accelerationStructure; - public RayTracingShader pathTracingShaderOverride; public delegate void PrepareDispatchRaysAction(T1 cmd); public PrepareDispatchRaysAction PrepareDispatchRays; - - public bool isContinousCaptureEnabled = true; - public bool isLastIteration = false; //SensorSDK - End - Tonemapping support internal Vector4[] frustumPlaneEquations; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs index 95465bb89cf..83e0e7d038e 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs @@ -208,17 +208,6 @@ private CameraData CheckDirtiness(HDCamera hdCamera, int camID, CameraData camDa return ResetPathTracing(camID, camData); } -//SensorSDK - Begin - hdCamera.isLastIteration = camData.currentIteration == m_SubFrameManager.subFrameCount; - - if (Application.isPlaying && hdCamera.isLastIteration && hdCamera.isContinousCaptureEnabled) - { - camData.ResetIteration(); - m_SubFrameManager.SetCameraData(camID, camData); - return ResetPathTracing(camID, camData); - } -//SensorSDK - End - // Check camera matrix dirtiness if (hdCamera.mainViewConstants.nonJitteredViewProjMatrix != (hdCamera.mainViewConstants.prevViewProjMatrix)) { From cbbe97e6fb1b3b6ff8042b3659c35835a9f26703 Mon Sep 17 00:00:00 2001 From: marclt Date: Wed, 3 Nov 2021 19:28:54 -0400 Subject: [PATCH 07/29] Refactor the prepare dispatch callback and the shader override to be internal and only implemented in PathTracing.cs --- .../Runtime/RenderPipeline/Camera/HDCamera.cs | 6 ------ .../RenderPipeline/PathTracing/PathTracing.cs | 19 ++++++++++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs index 05454823919..d8239a9d87a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs @@ -257,12 +257,6 @@ internal struct VolumetricCloudsAnimationData public float verticalErosionOffset; } -//SensorSDK - Begin - Tonemapping support - public RayTracingShader pathTracingShaderOverride; - public delegate void PrepareDispatchRaysAction(T1 cmd); - public PrepareDispatchRaysAction PrepareDispatchRays; -//SensorSDK - End - Tonemapping support - internal Vector4[] frustumPlaneEquations; internal int taaFrameIndex; internal float taaSharpenStrength; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs index 83e0e7d038e..9d15575efbb 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs @@ -280,19 +280,28 @@ class RenderPathTracingData public TextureHandle output; public TextureHandle sky; + } //SensorSDK - Begin - public HDCamera hdCamera; -//SensorSDK - End + private RayTracingShader _pathTracingShaderOverride = null; + internal Action _prepareDispatchRays; + + internal void SetPrepareDispatchCallback(Action callback) + { + _prepareDispatchRays = callback; } + internal void SetPathTracingShaderOverride(RayTracingShader shaderOverride) + { + _pathTracingShaderOverride = shaderOverride; + } +//SensorSDK - End TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in CameraData cameraData, TextureHandle pathTracingBuffer, TextureHandle skyBuffer) { using (var builder = renderGraph.AddRenderPass("Render PathTracing", out var passData)) { //SensorSDK - Begin - passData.pathTracingShader = hdCamera.pathTracingShaderOverride == null ? m_GlobalSettings.renderPipelineRayTracingResources.pathTracing : hdCamera.pathTracingShaderOverride; - passData.hdCamera = hdCamera; + passData.pathTracingShader = _pathTracingShaderOverride == null ? m_GlobalSettings.renderPipelineRayTracingResources.pathTracing : _pathTracingShaderOverride; //SensorSDK - End passData.cameraData = cameraData; @@ -353,7 +362,7 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracingTilingParameters, data.tilingParameters); //SensorSDK - Begin - data.hdCamera.PrepareDispatchRays?.Invoke(ctx.cmd); + _prepareDispatchRays?.Invoke(ctx.cmd); //SensorSDK - End // Run the computation ctx.cmd.DispatchRays(data.pathTracingShader, "RayGen", (uint)data.width, (uint)data.height, 1); From 82343061aa88ac839469e110704fcf10d2d1bfed Mon Sep 17 00:00:00 2001 From: marclt Date: Wed, 3 Nov 2021 20:39:27 -0400 Subject: [PATCH 08/29] Revert "Reverted shader files and litpathtracing changes." This reverts commit 8c4e4abb41f82001e550e0ee4a47ddf02cf2f4a5. --- .../Runtime/Material/Lit/Lit.shader | 1 + .../Runtime/Material/Lit/LitPathTracing.hlsl | 10 ++++++++++ .../Runtime/Material/Unlit/Unlit.shader | 2 ++ .../PathTracing/Shaders/PathTracingIntersection.hlsl | 11 +++++++++++ 4 files changed, 24 insertions(+) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader index 1b70118c6e7..f1c9e4c2820 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader @@ -1197,6 +1197,7 @@ Shader "HDRP/Lit" #pragma raytracing surface_shader #pragma multi_compile _ DEBUG_DISPLAY + #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE #define SHADERPASS SHADERPASS_PATH_TRACING diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl index 36ab798b130..2c9cd4fafc6 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl @@ -63,13 +63,23 @@ bool CreateMaterialData(PathIntersection pathIntersection, BuiltinData builtinDa { float NdotV = dot(GetSpecularNormal(mtlData), mtlData.V); float Fcoat = F_Schlick(CLEAR_COAT_F0, NdotV); + +#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) + float Fspec = F_Schlick(mtlData.bsdfData.fresnel0, NdotV).x; +#else float Fspec = Luminance(F_Schlick(mtlData.bsdfData.fresnel0, NdotV)); +#endif mtlData.bsdfWeight[1] = Fcoat * mtlData.bsdfData.coatMask; coatingTransmission = 1.0 - mtlData.bsdfWeight[1]; mtlData.bsdfWeight[2] = coatingTransmission * lerp(Fspec, 0.5, 0.5 * (mtlData.bsdfData.roughnessT + mtlData.bsdfData.roughnessB)) * GetSpecularCompensation(mtlData); mtlData.bsdfWeight[3] = (coatingTransmission - mtlData.bsdfWeight[2]) * mtlData.bsdfData.transmittanceMask; + +#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) + mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * mtlData.bsdfData.diffuseColor * mtlData.bsdfData.ambientOcclusion; +#else mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * Luminance(mtlData.bsdfData.diffuseColor) * mtlData.bsdfData.ambientOcclusion; +#endif } #ifdef _SURFACE_TYPE_TRANSPARENT else // Below diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader index 78916d0c33b..a48b95e6312 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader @@ -601,6 +601,8 @@ Shader "HDRP/Unlit" #define SHADER_UNLIT #define HAS_LIGHTLOOP // Used when computing volumetric scattering + #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingMacros.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl index b803427b1b7..dfb89a70db2 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl @@ -20,6 +20,17 @@ struct PathIntersection uint2 pixelCoord; // Max roughness encountered along the path float maxRoughness; + +// SensorSDK - Begin +#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) + // Extra params for beam lighthing + float3 beamDirection; + float3 beamOrigin; + + float beamRadius; + float beamDepth; +#endif +//SensorSDK - End }; #endif // UNITY_PATH_TRACING_INTERSECTION_INCLUDED From dc7a5dd5fde09ba2423515c96b22a682e380cbc8 Mon Sep 17 00:00:00 2001 From: marclt Date: Wed, 3 Nov 2021 21:04:58 -0400 Subject: [PATCH 09/29] Refactored the defined to use SENSORSDK_ENABLE_LIDAR instead of SENSORSDK_SHADERGRAPH. --- .../Runtime/Material/Lit/LitPathTracing.hlsl | 8 ++++++-- .../PathTracing/Shaders/PathTracingIntersection.hlsl | 2 +- .../PathTracing/Shaders/PathTracingLight.hlsl | 6 +++++- .../ShaderPass/ShaderPassPathTracing.hlsl | 11 ++++++++--- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl index 2c9cd4fafc6..4d916802604 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl @@ -1,3 +1,7 @@ +#ifdef SENSORSDK_OVERRIDE_REFLECTANCE +#define SENSORSDK_ENABLE_LIDAR +#endif + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingMaterial.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingBSDF.hlsl" @@ -64,7 +68,7 @@ bool CreateMaterialData(PathIntersection pathIntersection, BuiltinData builtinDa float NdotV = dot(GetSpecularNormal(mtlData), mtlData.V); float Fcoat = F_Schlick(CLEAR_COAT_F0, NdotV); -#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) +#if defined(SENSORSDK_ENABLE_LIDAR) float Fspec = F_Schlick(mtlData.bsdfData.fresnel0, NdotV).x; #else float Fspec = Luminance(F_Schlick(mtlData.bsdfData.fresnel0, NdotV)); @@ -75,7 +79,7 @@ bool CreateMaterialData(PathIntersection pathIntersection, BuiltinData builtinDa mtlData.bsdfWeight[2] = coatingTransmission * lerp(Fspec, 0.5, 0.5 * (mtlData.bsdfData.roughnessT + mtlData.bsdfData.roughnessB)) * GetSpecularCompensation(mtlData); mtlData.bsdfWeight[3] = (coatingTransmission - mtlData.bsdfWeight[2]) * mtlData.bsdfData.transmittanceMask; -#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) +#if defined(SENSORSDK_ENABLE_LIDAR) mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * mtlData.bsdfData.diffuseColor * mtlData.bsdfData.ambientOcclusion; #else mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * Luminance(mtlData.bsdfData.diffuseColor) * mtlData.bsdfData.ambientOcclusion; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl index dfb89a70db2..b11e8cef3b4 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl @@ -22,7 +22,7 @@ struct PathIntersection float maxRoughness; // SensorSDK - Begin -#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) +#if defined(SENSORSDK_ENABLE_LIDAR) // Extra params for beam lighthing float3 beamDirection; float3 beamOrigin; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl index 72001c00f81..636c78b40c6 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl @@ -1,6 +1,10 @@ #ifndef UNITY_PATH_TRACING_LIGHT_INCLUDED #define UNITY_PATH_TRACING_LIGHT_INCLUDED +#ifdef SENSORSDK_OVERRIDE_REFLECTANCE +#define SENSORSDK_ENABLE_LIDAR +#endif + // This is just because it need to be defined, shadow maps are not used. #define SHADOW_LOW @@ -338,7 +342,7 @@ float3 GetAreaEmission(LightData lightData, float centerU, float centerV, float return emission; } -#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) +#if defined(SENSORSDK_ENABLE_LIDAR) /* struct LightData { diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl index ac70fef3103..d62fdd9907f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl @@ -1,3 +1,8 @@ + +#ifdef SENSORSDK_OVERRIDE_REFLECTANCE +#define SENSORSDK_ENABLE_LIDAR +#endif + // Ray tracing includes #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingFragInputs.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Common/AtmosphericScatteringRayTracing.hlsl" @@ -14,7 +19,7 @@ TEXTURE2D(_SensorCustomReflectance); float Wavelength; #endif -#if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) +#if defined(SENSORSDK_ENABLE_LIDAR) int _SensorLightCount; #endif @@ -167,7 +172,7 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa float3 lightNormal = GetLightNormal(mtlData); #endif - #if !defined(SENSORSDK_SHADERGRAPH) && !defined(SENSORSDK_OVERRIDE_REFLECTANCE) + #if !defined(SENSORSDK_ENABLE_LIDAR) LightList lightList = CreateLightList(shadingPosition, lightNormal, builtinData.renderingLayers); #endif @@ -181,7 +186,7 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa PathIntersection nextPathIntersection; - #if defined(SENSORSDK_SHADERGRAPH) || defined(SENSORSDK_OVERRIDE_REFLECTANCE) + #if defined(SENSORSDK_ENABLE_LIDAR) pathIntersection.value = float3(0., 0., 0.); for (uint i = 0; i < _SensorLightCount; i++) { From 5e2a5e506df07ffff5c82c390561364ca2a7abf0 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 11:57:19 +0100 Subject: [PATCH 10/29] Reverted changes to LitPathTracing. --- .../Runtime/Material/Lit/LitPathTracing.hlsl | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl index 4d916802604..36ab798b130 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitPathTracing.hlsl @@ -1,7 +1,3 @@ -#ifdef SENSORSDK_OVERRIDE_REFLECTANCE -#define SENSORSDK_ENABLE_LIDAR -#endif - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingMaterial.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingBSDF.hlsl" @@ -67,23 +63,13 @@ bool CreateMaterialData(PathIntersection pathIntersection, BuiltinData builtinDa { float NdotV = dot(GetSpecularNormal(mtlData), mtlData.V); float Fcoat = F_Schlick(CLEAR_COAT_F0, NdotV); - -#if defined(SENSORSDK_ENABLE_LIDAR) - float Fspec = F_Schlick(mtlData.bsdfData.fresnel0, NdotV).x; -#else float Fspec = Luminance(F_Schlick(mtlData.bsdfData.fresnel0, NdotV)); -#endif mtlData.bsdfWeight[1] = Fcoat * mtlData.bsdfData.coatMask; coatingTransmission = 1.0 - mtlData.bsdfWeight[1]; mtlData.bsdfWeight[2] = coatingTransmission * lerp(Fspec, 0.5, 0.5 * (mtlData.bsdfData.roughnessT + mtlData.bsdfData.roughnessB)) * GetSpecularCompensation(mtlData); mtlData.bsdfWeight[3] = (coatingTransmission - mtlData.bsdfWeight[2]) * mtlData.bsdfData.transmittanceMask; - -#if defined(SENSORSDK_ENABLE_LIDAR) - mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * mtlData.bsdfData.diffuseColor * mtlData.bsdfData.ambientOcclusion; -#else mtlData.bsdfWeight[0] = coatingTransmission * (1.0 - mtlData.bsdfData.transmittanceMask) * Luminance(mtlData.bsdfData.diffuseColor) * mtlData.bsdfData.ambientOcclusion; -#endif } #ifdef _SURFACE_TYPE_TRANSPARENT else // Below From c4b4185db4f0bf70e15e309da3c0c4e9b8fb2be2 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 12:27:43 +0100 Subject: [PATCH 11/29] Moved specific Sensor Lidar tracing code into its own file. --- .../PathTracing/Shaders/PathTracingLight.hlsl | 117 ----- .../ShaderPass/ShaderPassPathTracing.hlsl | 51 +- .../ShaderPass/ShaderPassSensorLidar.hlsl | 441 ++++++++++++++++++ 3 files changed, 449 insertions(+), 160 deletions(-) create mode 100644 com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl index 636c78b40c6..822e374bf42 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl @@ -1,10 +1,6 @@ #ifndef UNITY_PATH_TRACING_LIGHT_INCLUDED #define UNITY_PATH_TRACING_LIGHT_INCLUDED -#ifdef SENSORSDK_OVERRIDE_REFLECTANCE -#define SENSORSDK_ENABLE_LIDAR -#endif - // This is just because it need to be defined, shadow maps are not used. #define SHADOW_LOW @@ -342,119 +338,6 @@ float3 GetAreaEmission(LightData lightData, float centerU, float centerV, float return emission; } -#if defined(SENSORSDK_ENABLE_LIDAR) -/* -struct LightData -{ - float3 positionRWS; - uint lightLayers; - float lightDimmer; - float volumetricLightDimmer; - real angleScale; - real angleOffset; - float3 forward; - int lightType; - float3 right; - real range; - float3 up; - float rangeAttenuationScale; - float3 color; - float rangeAttenuationBias; - int cookieIndex; - int tileCookie; - int shadowIndex; - int contactShadowMask; - float3 shadowTint; - float shadowDimmer; - float volumetricShadowDimmer; - int nonLightMappedOnly; - real minRoughness; - int screenSpaceShadowIndex; - real4 shadowMaskSelector; - real4 size; - float diffuseDimmer; - float specularDimmer; - float isRayTracedContactShadow; - float penumbraTint; - float3 padding; - float boxLightSafeExtent; -}; -*/ - -bool SampleBeam( - LightData lightData, - float3 position, - float3 normal, - out float3 outgoingDir, - out float3 value, - out float pdf, - out float dist, - inout PathIntersection payload) -{ - const float MM_TO_M = 1e-3; - const float M_TO_MM = 1e3; - - float3 lightDirection = payload.beamDirection; - float3 lightPosition = payload.beamOrigin; - - outgoingDir = position - lightPosition; - dist = length(outgoingDir); - outgoingDir /= dist; - - float apertureRadius = lightData.size.x; - float w0 = lightData.size.y; - float zr = lightData.size.z; - float distToWaist = lightData.size.w; - - // get the hit point in the coordinate frame of the laser as a depth(z) and - // radial measure(r) - float ctheta = dot(lightDirection, outgoingDir); - float zFromAperture = ctheta * dist; - float rSq = Sq(dist) - Sq(zFromAperture); - float3 radialDirection = dist * outgoingDir - zFromAperture*lightDirection; - - float zFromWaist = abs(zFromAperture * M_TO_MM - distToWaist); - if (dot(normal, -outgoingDir) < 0.001) - return false; - - // Total beam power, note: different from the output here which is irradiance. - float P = lightData.color.x; - - const float zRatio = Sq(zFromWaist / zr); - const float wz = w0 * sqrt(1 + zRatio) * MM_TO_M; - const float Eoz = 2 * P; - const float wzSq = wz*wz; - - float gaussianFactor = exp(-2 * rSq / wzSq) / (PI * wzSq); // 1/m^2 - value = gaussianFactor * Eoz; // W/m^2 - - payload.beamRadius = wz; - payload.beamDepth = zFromAperture; - -#if 0 /*Debug values*/ - payload.diffuseColor = float3(ctheta, zFromAperture, rSq); - payload.fresnel0 = float3(distToWaist, w0, zr); - payload.transmittance = float3(zFromWaist, zRatio, wzSq); - payload.tangentWS = float3(Eoz, wzSq, gaussianFactor); -#endif - - // sampling a point in the "virtual" aperture - // Find the actual point in the beam aperture that corresponds to this point - float rRatio = apertureRadius / wz; - float3 pAperture = lightPosition + rRatio * radialDirection; // location of the point in the aperture - - outgoingDir = pAperture - position; // corrected outgoing vector using the assumption below - dist = length(outgoingDir); - outgoingDir /= dist; - - // assumption that the interaction point is only illuminated by - // one point in the laser aperture. - pdf = 1.0f; - - return any(value); -} -#endif - float3 GetLightTransmission(float3 transmission, float shadowOpacity) { return lerp(float3(1.0, 1.0, 1.0), transmission, shadowOpacity); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl index d62fdd9907f..2a256875f46 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl @@ -1,8 +1,13 @@ - +//SensorSDK - Begin #ifdef SENSORSDK_OVERRIDE_REFLECTANCE #define SENSORSDK_ENABLE_LIDAR #endif +#ifdef SENSORSDK_ENABLE_LIDAR +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl" +#else +//SensorSDK - End + // Ray tracing includes #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingFragInputs.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Common/AtmosphericScatteringRayTracing.hlsl" @@ -13,16 +18,6 @@ #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingVolume.hlsl" #endif -#ifdef SENSORSDK_OVERRIDE_REFLECTANCE - -TEXTURE2D(_SensorCustomReflectance); -float Wavelength; -#endif - -#if defined(SENSORSDK_ENABLE_LIDAR) -int _SensorLightCount; -#endif - float PowerHeuristic(float f, float b) { return Sq(f) / (Sq(f) + Sq(b)); @@ -135,18 +130,6 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa BSDFData bsdfData = ConvertSurfaceDataToBSDFData(posInput.positionSS, surfaceData); #ifndef SHADER_UNLIT - //We override the diffuce color when we are using standard lit shader. Don't need to when using shader graph. -#ifdef SENSORSDK_OVERRIDE_REFLECTANCE - //bsdfData.diffuseColor = float3(_SensorCustomReflectance, _SensorCustomReflectance, _SensorCustomReflectance); //Override diffuse with material reflectance - const float _minWaveLengthValue = 0.35f; // 350 nm - const float _maxWaveLengthValue = 2.5f; // 2500 nm - - float wlIdx = clamp(Wavelength * 0.001f, _minWaveLengthValue, _maxWaveLengthValue); - float wavelengthSpan = _maxWaveLengthValue - _minWaveLengthValue + 1; - float2 coordCurve = float2(wlIdx / wavelengthSpan, 0); - - bsdfData.diffuseColor = SAMPLE_TEXTURE2D(_SensorCustomReflectance, s_linear_clamp_sampler, coordCurve); -#endif // Override the geometric normal (otherwise, it is merely the non-mapped smooth normal) // Also make sure that it is in the same hemisphere as the shading normal (which may have been flipped) @@ -172,9 +155,7 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa float3 lightNormal = GetLightNormal(mtlData); #endif - #if !defined(SENSORSDK_ENABLE_LIDAR) LightList lightList = CreateLightList(shadingPosition, lightNormal, builtinData.renderingLayers); - #endif float pdf, shadowOpacity; float3 value; @@ -186,23 +167,6 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa PathIntersection nextPathIntersection; - #if defined(SENSORSDK_ENABLE_LIDAR) - pathIntersection.value = float3(0., 0., 0.); - for (uint i = 0; i < _SensorLightCount; i++) - { - if (SampleBeam(_LightDatasRT[i], rayDescriptor.Origin, bsdfData.normalWS, - rayDescriptor.Direction, value, pdf, rayDescriptor.TMax, - pathIntersection)) - { - EvaluateMaterial(mtlData, rayDescriptor.Direction, mtlResult); - - // value is in radian (w/sr) not in lumen (cd/sr) and only the r channel is used - value *= (mtlResult.diffValue + mtlResult.specValue) / pdf; - - pathIntersection.value += value; - } - } - #else // Light sampling if (computeDirect) { @@ -284,7 +248,6 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa pathIntersection.value += value * rrFactor * nextPathIntersection.value; } } - #endif } #else // SHADER_UNLIT @@ -432,3 +395,5 @@ void AnyHit(inout PathIntersection pathIntersection : SV_RayPayload, AttributeDa #endif } } + +#endif // SENSORSDK_ENABLE_LIDAR diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl new file mode 100644 index 00000000000..2be910d43d3 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl @@ -0,0 +1,441 @@ +// Ray tracing includes +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingFragInputs.hlsl" +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Common/AtmosphericScatteringRayTracing.hlsl" + +// Path tracing includes +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl" +// #ifdef HAS_LIGHTLOOP +// #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingVolume.hlsl" +// #endif + +#ifdef SENSORSDK_OVERRIDE_REFLECTANCE +TEXTURE2D(_SensorCustomReflectance); +float Wavelength; +#endif + +int _SensorLightCount; + + + +/* +struct LightData +{ + float3 positionRWS; + uint lightLayers; + float lightDimmer; + float volumetricLightDimmer; + real angleScale; + real angleOffset; + float3 forward; + int lightType; + float3 right; + real range; + float3 up; + float rangeAttenuationScale; + float3 color; + float rangeAttenuationBias; + int cookieIndex; + int tileCookie; + int shadowIndex; + int contactShadowMask; + float3 shadowTint; + float shadowDimmer; + float volumetricShadowDimmer; + int nonLightMappedOnly; + real minRoughness; + int screenSpaceShadowIndex; + real4 shadowMaskSelector; + real4 size; + float diffuseDimmer; + float specularDimmer; + float isRayTracedContactShadow; + float penumbraTint; + float3 padding; + float boxLightSafeExtent; +}; +*/ + +bool SampleBeam( + LightData lightData, + float3 position, + float3 normal, + out float3 outgoingDir, + out float3 value, + out float pdf, + out float dist, + inout PathIntersection payload) +{ + const float MM_TO_M = 1e-3; + const float M_TO_MM = 1e3; + + float3 lightDirection = payload.beamDirection; + float3 lightPosition = payload.beamOrigin; + + outgoingDir = position - lightPosition; + dist = length(outgoingDir); + outgoingDir /= dist; + + float apertureRadius = lightData.size.x; + float w0 = lightData.size.y; + float zr = lightData.size.z; + float distToWaist = lightData.size.w; + + // get the hit point in the coordinate frame of the laser as a depth(z) and + // radial measure(r) + float ctheta = dot(lightDirection, outgoingDir); + float zFromAperture = ctheta * dist; + float rSq = Sq(dist) - Sq(zFromAperture); + float3 radialDirection = dist * outgoingDir - zFromAperture*lightDirection; + + float zFromWaist = abs(zFromAperture * M_TO_MM - distToWaist); + if (dot(normal, -outgoingDir) < 0.001) + return false; + + // Total beam power, note: different from the output here which is irradiance. + float P = lightData.color.x; + + const float zRatio = Sq(zFromWaist / zr); + const float wz = w0 * sqrt(1 + zRatio) * MM_TO_M; + const float Eoz = 2 * P; + const float wzSq = wz*wz; + + float gaussianFactor = exp(-2 * rSq / wzSq) / (PI * wzSq); // 1/m^2 + value = gaussianFactor * Eoz; // W/m^2 + + payload.beamRadius = wz; + payload.beamDepth = zFromAperture; + +#if 0 /*Debug values*/ + payload.diffuseColor = float3(ctheta, zFromAperture, rSq); + payload.fresnel0 = float3(distToWaist, w0, zr); + payload.transmittance = float3(zFromWaist, zRatio, wzSq); + payload.tangentWS = float3(Eoz, wzSq, gaussianFactor); +#endif + + // sampling a point in the "virtual" aperture + // Find the actual point in the beam aperture that corresponds to this point + float rRatio = apertureRadius / wz; + float3 pAperture = lightPosition + rRatio * radialDirection; // location of the point in the aperture + + outgoingDir = pAperture - position; // corrected outgoing vector using the assumption below + dist = length(outgoingDir); + outgoingDir /= dist; + + // assumption that the interaction point is only illuminated by + // one point in the laser aperture. + pdf = 1.0f; + + return any(value); +} + + + + + + + + + +// Function responsible for surface scattering +void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes, float4 inputSample) +{ + // The first thing that we should do is grab the intersection vertex + IntersectionVertex currentVertex; + GetCurrentIntersectionVertex(attributeData, currentVertex); + + // Build the Frag inputs from the intersection vertex + FragInputs fragInput; + BuildFragInputsFromIntersection(currentVertex, fragInput); + + // Such an invalid remainingDepth value means we are called from a subsurface computation + if (pathIntersection.remainingDepth > _RaytracingMaxRecursion) + { + pathIntersection.value = fragInput.tangentToWorld[2]; // Returns normal + return; + } + + // Grab depth information + uint currentDepth = _RaytracingMaxRecursion - pathIntersection.remainingDepth; + + // Make sure to add the additional travel distance + pathIntersection.cone.width += pathIntersection.t * abs(pathIntersection.cone.spreadAngle); + +#ifdef SHADER_UNLIT + // This is quick and dirty way to avoid double contribution from light meshes + if (currentDepth) + pathIntersection.cone.spreadAngle = -1.0; +#endif + + PositionInputs posInput; + posInput.positionWS = fragInput.positionRWS; + posInput.positionSS = pathIntersection.pixelCoord; + + // For path tracing, we want the front-facing test to be performed on the actual geometric normal + float3 geomNormal; + GetCurrentIntersectionGeometricNormal(attributeData, geomNormal); + fragInput.isFrontFace = dot(WorldRayDirection(), geomNormal) < 0.0; + + // Build the surfacedata and builtindata + SurfaceData surfaceData; + BuiltinData builtinData; + bool isVisible; + GetSurfaceAndBuiltinData(fragInput, -WorldRayDirection(), posInput, surfaceData, builtinData, currentVertex, pathIntersection.cone, isVisible); + + // Check if we want to compute direct and emissive lighting for current depth + bool computeDirect = currentDepth >= _RaytracingMinRecursion - 1; + + // Compute the bsdf data + BSDFData bsdfData = ConvertSurfaceDataToBSDFData(posInput.positionSS, surfaceData); + +#ifndef SHADER_UNLIT + //We override the diffuce color when we are using standard lit shader. Don't need to when using shader graph. +#ifdef SENSORSDK_OVERRIDE_REFLECTANCE + //bsdfData.diffuseColor = float3(_SensorCustomReflectance, _SensorCustomReflectance, _SensorCustomReflectance); //Override diffuse with material reflectance + const float _minWaveLengthValue = 0.35f; // 350 nm + const float _maxWaveLengthValue = 2.5f; // 2500 nm + + float wlIdx = clamp(Wavelength * 0.001f, _minWaveLengthValue, _maxWaveLengthValue); + float wavelengthSpan = _maxWaveLengthValue - _minWaveLengthValue + 1; + float2 coordCurve = float2(wlIdx / wavelengthSpan, 0); + + bsdfData.diffuseColor = SAMPLE_TEXTURE2D(_SensorCustomReflectance, s_linear_clamp_sampler, coordCurve); +#endif + + // Override the geometric normal (otherwise, it is merely the non-mapped smooth normal) + // Also make sure that it is in the same hemisphere as the shading normal (which may have been flipped) + bsdfData.geomNormalWS = dot(bsdfData.normalWS, geomNormal) > 0.0 ? geomNormal : -geomNormal; + + // Compute the world space position (the non-camera relative one if camera relative rendering is enabled) + float3 shadingPosition = fragInput.positionRWS; + + // Get current path throughput + float3 pathThroughput = pathIntersection.value; + + // And reset the ray intersection color, which will store our final result + pathIntersection.value = computeDirect ? builtinData.emissiveColor : 0.0; + + // Initialize our material data (this will alter the bsdfData to suit path tracing, and choose between BSDF or SSS evaluation) + MaterialData mtlData; + if (CreateMaterialData(pathIntersection, builtinData, bsdfData, shadingPosition, inputSample.z, mtlData)) + { + // Create the list of active lights + #ifdef _SURFACE_TYPE_TRANSPARENT + float3 lightNormal = 0.0; + #else + float3 lightNormal = GetLightNormal(mtlData); + #endif + + #if !defined(SENSORSDK_ENABLE_LIDAR) + LightList lightList = CreateLightList(shadingPosition, lightNormal, builtinData.renderingLayers); + #endif + + float pdf, shadowOpacity; + float3 value; + MaterialResult mtlResult; + + RayDesc rayDescriptor; + rayDescriptor.Origin = shadingPosition + mtlData.bsdfData.geomNormalWS * _RaytracingRayBias; + rayDescriptor.TMin = 0.0; + + PathIntersection nextPathIntersection; + + #if defined(SENSORSDK_ENABLE_LIDAR) + pathIntersection.value = float3(0., 0., 0.); + for (uint i = 0; i < _SensorLightCount; i++) + { + if (SampleBeam(_LightDatasRT[i], rayDescriptor.Origin, bsdfData.normalWS, + rayDescriptor.Direction, value, pdf, rayDescriptor.TMax, + pathIntersection)) + { + EvaluateMaterial(mtlData, rayDescriptor.Direction, mtlResult); + + // value is in radian (w/sr) not in lumen (cd/sr) and only the r channel is used + value *= (mtlResult.diffValue + mtlResult.specValue) / pdf; + + pathIntersection.value += value; + } + } + #else + // Light sampling + if (computeDirect) + { + if (SampleLights(lightList, inputSample.xyz, rayDescriptor.Origin, lightNormal, false, rayDescriptor.Direction, value, pdf, rayDescriptor.TMax, shadowOpacity)) + { + EvaluateMaterial(mtlData, rayDescriptor.Direction, mtlResult); + + value *= (mtlResult.diffValue + mtlResult.specValue) / pdf; + if (Luminance(value) > 0.001) + { + // Shoot a transmission ray (to mark it as such, purposedly set remaining depth to an invalid value) + nextPathIntersection.remainingDepth = _RaytracingMaxRecursion + 1; + rayDescriptor.TMax -= _RaytracingRayBias; + nextPathIntersection.value = 1.0; + + // FIXME: For the time being, we choose not to apply any back/front-face culling for shadows, will possibly change in the future + TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_FORCE_NON_OPAQUE | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, + RAYTRACINGRENDERERFLAG_CAST_SHADOW, 0, 1, 1, rayDescriptor, nextPathIntersection); + + float misWeight = PowerHeuristic(pdf, mtlResult.diffPdf + mtlResult.specPdf); + pathIntersection.value += value * GetLightTransmission(nextPathIntersection.value, shadowOpacity) * misWeight; + } + } + } + + // Material sampling + if (SampleMaterial(mtlData, inputSample.xyz, rayDescriptor.Direction, mtlResult)) + { + // Compute overall material value and pdf + pdf = mtlResult.diffPdf + mtlResult.specPdf; + value = (mtlResult.diffValue + mtlResult.specValue) / pdf; + + pathThroughput *= value; + + // Apply Russian roulette to our path + const float rrThreshold = 0.2 + 0.1 * _RaytracingMaxRecursion; + float rrFactor, rrValue = Luminance(pathThroughput); + + if (RussianRouletteTest(rrThreshold, rrValue, inputSample.w, rrFactor, !currentDepth)) + { + bool isSampleBelow = IsBelow(mtlData, rayDescriptor.Direction); + + rayDescriptor.Origin = shadingPosition + GetPositionBias(mtlData.bsdfData.geomNormalWS, _RaytracingRayBias, isSampleBelow); + rayDescriptor.TMax = FLT_INF; + + // Copy path constants across + nextPathIntersection.pixelCoord = pathIntersection.pixelCoord; + nextPathIntersection.cone.width = pathIntersection.cone.width; + + // Complete PathIntersection structure for this sample + nextPathIntersection.value = pathThroughput * rrFactor; + nextPathIntersection.remainingDepth = pathIntersection.remainingDepth - 1; + nextPathIntersection.t = rayDescriptor.TMax; + + // Adjust the path max roughness (used for roughness clamping, to reduce fireflies) + nextPathIntersection.maxRoughness = AdjustPathRoughness(mtlData, mtlResult, isSampleBelow, pathIntersection.maxRoughness); + + // In order to achieve filtering for the textures, we need to compute the spread angle of the pixel + nextPathIntersection.cone.spreadAngle = pathIntersection.cone.spreadAngle + roughnessToSpreadAngle(nextPathIntersection.maxRoughness); + + // Shoot ray for indirect lighting + TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, RAYTRACINGRENDERERFLAG_PATH_TRACING, 0, 1, 2, rayDescriptor, nextPathIntersection); + + if (computeDirect) + { + // Use same ray for direct lighting (use indirect result for occlusion) + rayDescriptor.TMax = nextPathIntersection.t + _RaytracingRayBias; + float3 lightValue; + float lightPdf; + EvaluateLights(lightList, rayDescriptor, lightValue, lightPdf); + + float misWeight = PowerHeuristic(pdf, lightPdf); + nextPathIntersection.value += lightValue * misWeight; + } + + // Apply material absorption + nextPathIntersection.value = ApplyAbsorption(mtlData, surfaceData, nextPathIntersection.t, isSampleBelow, nextPathIntersection.value); + + pathIntersection.value += value * rrFactor * nextPathIntersection.value; + } + } + #endif + } + +#else // SHADER_UNLIT + + pathIntersection.value = computeDirect ? bsdfData.color * GetInverseCurrentExposureMultiplier() + builtinData.emissiveColor : 0.0; + +// Apply shadow matte if requested +#ifdef _ENABLE_SHADOW_MATTE + float3 shadowColor = lerp(pathIntersection.value, surfaceData.shadowTint.rgb * GetInverseCurrentExposureMultiplier(), surfaceData.shadowTint.a); + float visibility = ComputeVisibility(fragInput.positionRWS, surfaceData.normalWS, inputSample.xyz); + pathIntersection.value = lerp(shadowColor, pathIntersection.value, visibility); +#endif + +// Simulate opacity blending by simply continuing along the current ray +#ifdef _SURFACE_TYPE_TRANSPARENT + if (builtinData.opacity < 1.0) + { + RayDesc rayDescriptor; + float bias = dot(WorldRayDirection(), fragInput.tangentToWorld[2]) > 0.0 ? _RaytracingRayBias : -_RaytracingRayBias; + rayDescriptor.Origin = fragInput.positionRWS + bias * fragInput.tangentToWorld[2]; + rayDescriptor.Direction = WorldRayDirection(); + rayDescriptor.TMin = 0.0; + rayDescriptor.TMax = FLT_INF; + + PathIntersection nextPathIntersection = pathIntersection; + nextPathIntersection.remainingDepth--; + + TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, RAYTRACINGRENDERERFLAG_PATH_TRACING, 0, 1, 2, rayDescriptor, nextPathIntersection); + + pathIntersection.value = lerp(nextPathIntersection.value, pathIntersection.value, builtinData.opacity); + } +#endif + +#endif // SHADER_UNLIT +} + +// Generic function that handles one scattering event (a vertex along the full path), can be either: +// - Surface scattering +// - Volume scattering +[shader("closesthit")] +void ClosestHit(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes) +{ + // Always set the new t value + pathIntersection.t = RayTCurrent(); + + // If the max depth has been reached, bail out + if (!pathIntersection.remainingDepth) + { + pathIntersection.value = 0.0; + return; + } + + // Grab depth information + int currentDepth = _RaytracingMaxRecursion - pathIntersection.remainingDepth; + bool computeDirect = currentDepth >= _RaytracingMinRecursion - 1; + + float4 inputSample = 0.0; + float pdf = 1.0; + +#ifdef HAS_LIGHTLOOP + + float3 lightPosition; + bool sampleLocalLights, sampleVolume = false; + + if (currentDepth >= 0) + { + // Generate a 4D unit-square sample for this depth, from our QMC sequence + inputSample = GetSample4D(pathIntersection.pixelCoord, _RaytracingSampleIndex, 4 * currentDepth); + + // For the time being, we test for volumetric scattering only on camera rays + if (!currentDepth && computeDirect) + sampleVolume = SampleVolumeScatteringPosition(pathIntersection.pixelCoord, inputSample.w, pathIntersection.t, pdf, sampleLocalLights, lightPosition); + } + + if (sampleVolume) + ComputeVolumeScattering(pathIntersection, inputSample.xyz, sampleLocalLights, lightPosition); + else + ComputeSurfaceScattering(pathIntersection, attributeData, inputSample); + + computeDirect &= !sampleVolume; + +#else // HAS_LIGHTLOOP + + ComputeSurfaceScattering(pathIntersection, attributeData, inputSample); + +#endif // HAS_LIGHTLOOP + + // Apply volumetric attenuation + ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), pathIntersection.t, pathIntersection.value, computeDirect); + + // Apply the volume/surface pdf + pathIntersection.value /= pdf; + + if (currentDepth) + { + // Bias the result (making it too dark), but reduces fireflies a lot + float intensity = Luminance(pathIntersection.value) * GetCurrentExposureMultiplier(); + if (intensity > _RaytracingIntensityClamp) + pathIntersection.value *= _RaytracingIntensityClamp / intensity; + } +} From f613582367a6a5cbb7cb950ee42cb21caf17f6ab Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 12:40:48 +0100 Subject: [PATCH 12/29] Cosmetic. --- .../RenderPipeline/PathTracing/PathTracing.cs | 20 ++++--------------- .../Shaders/PathTracingIntersection.hlsl | 3 +-- .../ShaderPass/ShaderPassPathTracing.hlsl | 2 +- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs index 9d15575efbb..003884bbbb5 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs @@ -283,27 +283,15 @@ class RenderPathTracingData } //SensorSDK - Begin - private RayTracingShader _pathTracingShaderOverride = null; - internal Action _prepareDispatchRays; - - internal void SetPrepareDispatchCallback(Action callback) - { - _prepareDispatchRays = callback; - } - internal void SetPathTracingShaderOverride(RayTracingShader shaderOverride) - { - _pathTracingShaderOverride = shaderOverride; - } + internal RayTracingShader m_PathTracingShaderOverride = null; + internal Action m_PrepareDispatchRays = null; //SensorSDK - End TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in CameraData cameraData, TextureHandle pathTracingBuffer, TextureHandle skyBuffer) { using (var builder = renderGraph.AddRenderPass("Render PathTracing", out var passData)) { -//SensorSDK - Begin - passData.pathTracingShader = _pathTracingShaderOverride == null ? m_GlobalSettings.renderPipelineRayTracingResources.pathTracing : _pathTracingShaderOverride; -//SensorSDK - End - + passData.pathTracingShader = m_PathTracingShaderOverride ? m_PathTracingShaderOverride : m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; passData.cameraData = cameraData; passData.ditheredTextureSet = GetBlueNoiseManager().DitheredTextureSet256SPP(); passData.backgroundColor = hdCamera.backgroundColorHDR; @@ -362,7 +350,7 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracingTilingParameters, data.tilingParameters); //SensorSDK - Begin - _prepareDispatchRays?.Invoke(ctx.cmd); + m_PrepareDispatchRays?.Invoke(ctx.cmd); //SensorSDK - End // Run the computation ctx.cmd.DispatchRays(data.pathTracingShader, "RayGen", (uint)data.width, (uint)data.height, 1); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl index b11e8cef3b4..8d7bc86f45f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl @@ -22,8 +22,7 @@ struct PathIntersection float maxRoughness; // SensorSDK - Begin -#if defined(SENSORSDK_ENABLE_LIDAR) - // Extra params for beam lighthing +#ifdef SENSORSDK_ENABLE_LIDAR float3 beamDirection; float3 beamOrigin; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl index 2a256875f46..3d4c9004d2c 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl @@ -154,7 +154,7 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa #else float3 lightNormal = GetLightNormal(mtlData); #endif - + LightList lightList = CreateLightList(shadingPosition, lightNormal, builtinData.renderingLayers); float pdf, shadowOpacity; From 9a927eed590ae89ec9431d3a6b5926945f8a35bb Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 12:46:02 +0100 Subject: [PATCH 13/29] Updated changelog. --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + .../RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl | 1 - .../RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl | 2 -- 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 626a4a42e6a..71956b9e44b 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Added new panels to Rendering Debugger Display Stats panel, displaying improved CPU/GPU frame timings and bottlenecks. - Added API to edit diffusion profiles and set IES on lights. - Added public API to reset path tracing accumulation, and check its status. +- Added support for SensorSDK's Lidar and camera models in path tracing. ### Fixed - Fixed decal position when created from context menu. (case 1368987) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl index 3d4c9004d2c..8509ba992e2 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl @@ -154,7 +154,6 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa #else float3 lightNormal = GetLightNormal(mtlData); #endif - LightList lightList = CreateLightList(shadingPosition, lightNormal, builtinData.renderingLayers); float pdf, shadowOpacity; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl index 2be910d43d3..c450d2cc3a2 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl @@ -15,8 +15,6 @@ float Wavelength; int _SensorLightCount; - - /* struct LightData { From 704dc68e7389359ff27f35f1f60f6774a13dceba Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 15:08:53 +0100 Subject: [PATCH 14/29] Added stripping of shader code when SensorSDK not present, refactor. --- .../Editor/Material/BaseShaderPreprocessor.cs | 10 + .../Material/Lit/LitShaderPreprocessor.cs | 8 +- .../RenderPipeline/PathTracing/PathTracing.cs | 21 +- .../ShaderPass/ShaderPassSensorLidar.hlsl | 308 +++--------------- ...derPipelines.HighDefinition.Runtime.asmdef | 5 + 5 files changed, 78 insertions(+), 274 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/BaseShaderPreprocessor.cs b/com.unity.render-pipelines.high-definition/Editor/Material/BaseShaderPreprocessor.cs index e320561bd3b..2c89fe8e0e9 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/BaseShaderPreprocessor.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/BaseShaderPreprocessor.cs @@ -62,6 +62,11 @@ abstract class BaseShaderPreprocessor protected ShadowKeywords m_ShadowKeywords; +#if !ENABLE_SENSOR_SDK + protected ShaderKeyword m_SensorEnableLidar; + protected ShaderKeyword m_SensorOverrideReflectance; +#endif + protected Dictionary m_ShadowVariants; public virtual int Priority => 0; @@ -94,6 +99,11 @@ public BaseShaderPreprocessor() m_DecalSurfaceGradient = new ShaderKeyword("DECAL_SURFACE_GRADIENT"); m_EditorVisualization = new ShaderKeyword("EDITOR_VISUALIZATION"); m_ShadowKeywords = new ShadowKeywords(); + +#if !ENABLE_SENSOR_SDK + m_SensorEnableLidar = new ShaderKeyword("SENSORSDK_ENABLE_LIDAR"); + m_SensorOverrideReflectance = new ShaderKeyword("SENSORSDK_OVERRIDE_REFLECTANCE"); +#endif } public bool ShadersStripper(HDRenderPipelineAsset hdrpAsset, Shader shader, ShaderSnippetData snippet, diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/Lit/LitShaderPreprocessor.cs b/com.unity.render-pipelines.high-definition/Editor/Material/Lit/LitShaderPreprocessor.cs index 719009abed3..87515a2c236 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/Lit/LitShaderPreprocessor.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/Lit/LitShaderPreprocessor.cs @@ -72,7 +72,7 @@ protected override bool DoShadersStripper(HDRenderPipelineAsset hdrpAsset, Shade } - // Apply following set of rules only to lit shader (remember that LitPreprocessor is call for any shader) + // Apply following set of rules only to lit shader (remember that LitPreprocessor is called for any shader) if (isBuiltInLit) { // Forward material don't use keyword for WriteNormalBuffer but #define so we can't test for the keyword outside of isBuiltInLit @@ -116,6 +116,12 @@ protected override bool DoShadersStripper(HDRenderPipelineAsset hdrpAsset, Shade } } +#if !ENABLE_SENSOR_SDK + // If the SensorSDK package is not present, make sure that all code related to it is stripped away + if (inputData.shaderKeywordSet.IsEnabled(m_SensorEnableLidar) || inputData.shaderKeywordSet.IsEnabled(m_SensorOverrideReflectance)) + return true; +#endif + // TODO: Tests for later // We need to find a way to strip useless shader features for passes/shader stages that don't need them (example, vertex shaders won't ever need SSS Feature flag) // This causes several problems: diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs index 003884bbbb5..b423e87811f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs @@ -282,16 +282,20 @@ class RenderPathTracingData public TextureHandle sky; } -//SensorSDK - Begin - internal RayTracingShader m_PathTracingShaderOverride = null; - internal Action m_PrepareDispatchRays = null; -//SensorSDK - End +#if !ENABLE_SENSOR_SDK + internal RayTracingShader pathTracingShaderOverride { private get; set; } = null; + internal Action prepareDispatchRays { private get; set; } = null; +#endif TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in CameraData cameraData, TextureHandle pathTracingBuffer, TextureHandle skyBuffer) { using (var builder = renderGraph.AddRenderPass("Render PathTracing", out var passData)) { - passData.pathTracingShader = m_PathTracingShaderOverride ? m_PathTracingShaderOverride : m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; +#if !ENABLE_SENSOR_SDK + passData.pathTracingShader = pathTracingShaderOverride ? pathTracingShaderOverride : m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; +#else + passData.pathTracingShader = m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; +#endif passData.cameraData = cameraData; passData.ditheredTextureSet = GetBlueNoiseManager().DitheredTextureSet256SPP(); passData.backgroundColor = hdCamera.backgroundColorHDR; @@ -349,9 +353,10 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracingDoFParameters, data.dofParameters); ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracingTilingParameters, data.tilingParameters); -//SensorSDK - Begin - m_PrepareDispatchRays?.Invoke(ctx.cmd); -//SensorSDK - End +#if !ENABLE_SENSOR_SDK + // SensorSDK can do its own camera rays generation + prepareDispatchRays?.Invoke(ctx.cmd); +#endif // Run the computation ctx.cmd.DispatchRays(data.pathTracingShader, "RayGen", (uint)data.width, (uint)data.height, 1); }); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl index c450d2cc3a2..eaa59325170 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl @@ -4,64 +4,37 @@ // Path tracing includes #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl" -// #ifdef HAS_LIGHTLOOP -// #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingVolume.hlsl" -// #endif +#ifdef HAS_LIGHTLOOP +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl" +#endif + +int _SensorLightCount; #ifdef SENSORSDK_OVERRIDE_REFLECTANCE + TEXTURE2D(_SensorCustomReflectance); float Wavelength; -#endif -int _SensorLightCount; - -/* -struct LightData +float3 OverrideReflectance() { - float3 positionRWS; - uint lightLayers; - float lightDimmer; - float volumetricLightDimmer; - real angleScale; - real angleOffset; - float3 forward; - int lightType; - float3 right; - real range; - float3 up; - float rangeAttenuationScale; - float3 color; - float rangeAttenuationBias; - int cookieIndex; - int tileCookie; - int shadowIndex; - int contactShadowMask; - float3 shadowTint; - float shadowDimmer; - float volumetricShadowDimmer; - int nonLightMappedOnly; - real minRoughness; - int screenSpaceShadowIndex; - real4 shadowMaskSelector; - real4 size; - float diffuseDimmer; - float specularDimmer; - float isRayTracedContactShadow; - float penumbraTint; - float3 padding; - float boxLightSafeExtent; -}; -*/ - -bool SampleBeam( - LightData lightData, - float3 position, - float3 normal, - out float3 outgoingDir, - out float3 value, - out float pdf, - out float dist, - inout PathIntersection payload) + const float minWaveLengthValue = 0.35; // 350 nm + const float maxWaveLengthValue = 2.5; // 2500 nm + + float wlIdx = clamp(Wavelength * 0.001, minWaveLengthValue, maxWaveLengthValue); + float wavelengthSpan = maxWaveLengthValue - minWaveLengthValue + 1.0; + float2 coordCurve = float2(wlIdx / wavelengthSpan, 0.0); + + return SAMPLE_TEXTURE2D(_SensorCustomReflectance, s_linear_clamp_sampler, coordCurve); +} + +#endif // SENSORSDK_OVERRIDE_REFLECTANCE + +bool SampleBeam(LightData lightData, + float3 position, + float3 normal, + out float3 outgoingDir, + out float3 value, + inout PathIntersection payload) { const float MM_TO_M = 1e-3; const float M_TO_MM = 1e3; @@ -70,7 +43,7 @@ bool SampleBeam( float3 lightPosition = payload.beamOrigin; outgoingDir = position - lightPosition; - dist = length(outgoingDir); + float dist = length(outgoingDir); outgoingDir /= dist; float apertureRadius = lightData.size.x; @@ -103,13 +76,6 @@ bool SampleBeam( payload.beamRadius = wz; payload.beamDepth = zFromAperture; -#if 0 /*Debug values*/ - payload.diffuseColor = float3(ctheta, zFromAperture, rSq); - payload.fresnel0 = float3(distToWaist, w0, zr); - payload.transmittance = float3(zFromWaist, zRatio, wzSq); - payload.tangentWS = float3(Eoz, wzSq, gaussianFactor); -#endif - // sampling a point in the "virtual" aperture // Find the actual point in the beam aperture that corresponds to this point float rRatio = apertureRadius / wz; @@ -119,22 +85,9 @@ bool SampleBeam( dist = length(outgoingDir); outgoingDir /= dist; - // assumption that the interaction point is only illuminated by - // one point in the laser aperture. - pdf = 1.0f; - return any(value); } - - - - - - - - -// Function responsible for surface scattering void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes, float4 inputSample) { // The first thing that we should do is grab the intersection vertex @@ -158,12 +111,6 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa // Make sure to add the additional travel distance pathIntersection.cone.width += pathIntersection.t * abs(pathIntersection.cone.spreadAngle); -#ifdef SHADER_UNLIT - // This is quick and dirty way to avoid double contribution from light meshes - if (currentDepth) - pathIntersection.cone.spreadAngle = -1.0; -#endif - PositionInputs posInput; posInput.positionWS = fragInput.positionRWS; posInput.positionSS = pathIntersection.pixelCoord; @@ -186,18 +133,11 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa BSDFData bsdfData = ConvertSurfaceDataToBSDFData(posInput.positionSS, surfaceData); #ifndef SHADER_UNLIT - //We override the diffuce color when we are using standard lit shader. Don't need to when using shader graph. -#ifdef SENSORSDK_OVERRIDE_REFLECTANCE - //bsdfData.diffuseColor = float3(_SensorCustomReflectance, _SensorCustomReflectance, _SensorCustomReflectance); //Override diffuse with material reflectance - const float _minWaveLengthValue = 0.35f; // 350 nm - const float _maxWaveLengthValue = 2.5f; // 2500 nm - float wlIdx = clamp(Wavelength * 0.001f, _minWaveLengthValue, _maxWaveLengthValue); - float wavelengthSpan = _maxWaveLengthValue - _minWaveLengthValue + 1; - float2 coordCurve = float2(wlIdx / wavelengthSpan, 0); - - bsdfData.diffuseColor = SAMPLE_TEXTURE2D(_SensorCustomReflectance, s_linear_clamp_sampler, coordCurve); -#endif + #ifdef SENSORSDK_OVERRIDE_REFLECTANCE + // Override the diffuce color when using builtin lit shader (but not with shader graph) + bsdfData.diffuseColor = OverrideReflectance(); + #endif // Override the geometric normal (otherwise, it is merely the non-mapped smooth normal) // Also make sure that it is in the same hemisphere as the shading normal (which may have been flipped) @@ -206,9 +146,6 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa // Compute the world space position (the non-camera relative one if camera relative rendering is enabled) float3 shadingPosition = fragInput.positionRWS; - // Get current path throughput - float3 pathThroughput = pathIntersection.value; - // And reset the ray intersection color, which will store our final result pathIntersection.value = computeDirect ? builtinData.emissiveColor : 0.0; @@ -216,141 +153,36 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa MaterialData mtlData; if (CreateMaterialData(pathIntersection, builtinData, bsdfData, shadingPosition, inputSample.z, mtlData)) { - // Create the list of active lights #ifdef _SURFACE_TYPE_TRANSPARENT float3 lightNormal = 0.0; #else float3 lightNormal = GetLightNormal(mtlData); #endif - #if !defined(SENSORSDK_ENABLE_LIDAR) - LightList lightList = CreateLightList(shadingPosition, lightNormal, builtinData.renderingLayers); - #endif - - float pdf, shadowOpacity; - float3 value; + float3 value, direction; MaterialResult mtlResult; - RayDesc rayDescriptor; - rayDescriptor.Origin = shadingPosition + mtlData.bsdfData.geomNormalWS * _RaytracingRayBias; - rayDescriptor.TMin = 0.0; - - PathIntersection nextPathIntersection; - - #if defined(SENSORSDK_ENABLE_LIDAR) - pathIntersection.value = float3(0., 0., 0.); - for (uint i = 0; i < _SensorLightCount; i++) - { - if (SampleBeam(_LightDatasRT[i], rayDescriptor.Origin, bsdfData.normalWS, - rayDescriptor.Direction, value, pdf, rayDescriptor.TMax, - pathIntersection)) - { - EvaluateMaterial(mtlData, rayDescriptor.Direction, mtlResult); - - // value is in radian (w/sr) not in lumen (cd/sr) and only the r channel is used - value *= (mtlResult.diffValue + mtlResult.specValue) / pdf; - - pathIntersection.value += value; - } - } - #else - // Light sampling - if (computeDirect) - { - if (SampleLights(lightList, inputSample.xyz, rayDescriptor.Origin, lightNormal, false, rayDescriptor.Direction, value, pdf, rayDescriptor.TMax, shadowOpacity)) - { - EvaluateMaterial(mtlData, rayDescriptor.Direction, mtlResult); - - value *= (mtlResult.diffValue + mtlResult.specValue) / pdf; - if (Luminance(value) > 0.001) - { - // Shoot a transmission ray (to mark it as such, purposedly set remaining depth to an invalid value) - nextPathIntersection.remainingDepth = _RaytracingMaxRecursion + 1; - rayDescriptor.TMax -= _RaytracingRayBias; - nextPathIntersection.value = 1.0; - - // FIXME: For the time being, we choose not to apply any back/front-face culling for shadows, will possibly change in the future - TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_FORCE_NON_OPAQUE | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, - RAYTRACINGRENDERERFLAG_CAST_SHADOW, 0, 1, 1, rayDescriptor, nextPathIntersection); - - float misWeight = PowerHeuristic(pdf, mtlResult.diffPdf + mtlResult.specPdf); - pathIntersection.value += value * GetLightTransmission(nextPathIntersection.value, shadowOpacity) * misWeight; - } - } - } - - // Material sampling - if (SampleMaterial(mtlData, inputSample.xyz, rayDescriptor.Direction, mtlResult)) + for (uint i = 0; i < _SensorLightCount; i++) { - // Compute overall material value and pdf - pdf = mtlResult.diffPdf + mtlResult.specPdf; - value = (mtlResult.diffValue + mtlResult.specValue) / pdf; - - pathThroughput *= value; - - // Apply Russian roulette to our path - const float rrThreshold = 0.2 + 0.1 * _RaytracingMaxRecursion; - float rrFactor, rrValue = Luminance(pathThroughput); - - if (RussianRouletteTest(rrThreshold, rrValue, inputSample.w, rrFactor, !currentDepth)) + if (SampleBeam(_LightDatasRT[i], shadingPosition, bsdfData.normalWS, + direction, value, pathIntersection)) { - bool isSampleBelow = IsBelow(mtlData, rayDescriptor.Direction); - - rayDescriptor.Origin = shadingPosition + GetPositionBias(mtlData.bsdfData.geomNormalWS, _RaytracingRayBias, isSampleBelow); - rayDescriptor.TMax = FLT_INF; - - // Copy path constants across - nextPathIntersection.pixelCoord = pathIntersection.pixelCoord; - nextPathIntersection.cone.width = pathIntersection.cone.width; - - // Complete PathIntersection structure for this sample - nextPathIntersection.value = pathThroughput * rrFactor; - nextPathIntersection.remainingDepth = pathIntersection.remainingDepth - 1; - nextPathIntersection.t = rayDescriptor.TMax; - - // Adjust the path max roughness (used for roughness clamping, to reduce fireflies) - nextPathIntersection.maxRoughness = AdjustPathRoughness(mtlData, mtlResult, isSampleBelow, pathIntersection.maxRoughness); + EvaluateMaterial(mtlData, direction, mtlResult); - // In order to achieve filtering for the textures, we need to compute the spread angle of the pixel - nextPathIntersection.cone.spreadAngle = pathIntersection.cone.spreadAngle + roughnessToSpreadAngle(nextPathIntersection.maxRoughness); + // value is in radian (w/sr) not in lumen (cd/sr) and only the r channel is used + value *= mtlResult.diffValue + mtlResult.specValue; - // Shoot ray for indirect lighting - TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, RAYTRACINGRENDERERFLAG_PATH_TRACING, 0, 1, 2, rayDescriptor, nextPathIntersection); - - if (computeDirect) - { - // Use same ray for direct lighting (use indirect result for occlusion) - rayDescriptor.TMax = nextPathIntersection.t + _RaytracingRayBias; - float3 lightValue; - float lightPdf; - EvaluateLights(lightList, rayDescriptor, lightValue, lightPdf); - - float misWeight = PowerHeuristic(pdf, lightPdf); - nextPathIntersection.value += lightValue * misWeight; - } - - // Apply material absorption - nextPathIntersection.value = ApplyAbsorption(mtlData, surfaceData, nextPathIntersection.t, isSampleBelow, nextPathIntersection.value); - - pathIntersection.value += value * rrFactor * nextPathIntersection.value; + pathIntersection.value += value; } } - #endif } #else // SHADER_UNLIT pathIntersection.value = computeDirect ? bsdfData.color * GetInverseCurrentExposureMultiplier() + builtinData.emissiveColor : 0.0; -// Apply shadow matte if requested -#ifdef _ENABLE_SHADOW_MATTE - float3 shadowColor = lerp(pathIntersection.value, surfaceData.shadowTint.rgb * GetInverseCurrentExposureMultiplier(), surfaceData.shadowTint.a); - float visibility = ComputeVisibility(fragInput.positionRWS, surfaceData.normalWS, inputSample.xyz); - pathIntersection.value = lerp(shadowColor, pathIntersection.value, visibility); -#endif - -// Simulate opacity blending by simply continuing along the current ray -#ifdef _SURFACE_TYPE_TRANSPARENT + // Simulate opacity blending by simply continuing along the current ray + #ifdef _SURFACE_TYPE_TRANSPARENT if (builtinData.opacity < 1.0) { RayDesc rayDescriptor; @@ -367,73 +199,19 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa pathIntersection.value = lerp(nextPathIntersection.value, pathIntersection.value, builtinData.opacity); } -#endif + #endif #endif // SHADER_UNLIT } -// Generic function that handles one scattering event (a vertex along the full path), can be either: -// - Surface scattering -// - Volume scattering [shader("closesthit")] void ClosestHit(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes) { // Always set the new t value pathIntersection.t = RayTCurrent(); - // If the max depth has been reached, bail out - if (!pathIntersection.remainingDepth) - { - pathIntersection.value = 0.0; - return; - } - - // Grab depth information - int currentDepth = _RaytracingMaxRecursion - pathIntersection.remainingDepth; - bool computeDirect = currentDepth >= _RaytracingMinRecursion - 1; - - float4 inputSample = 0.0; - float pdf = 1.0; - -#ifdef HAS_LIGHTLOOP - - float3 lightPosition; - bool sampleLocalLights, sampleVolume = false; - - if (currentDepth >= 0) - { - // Generate a 4D unit-square sample for this depth, from our QMC sequence - inputSample = GetSample4D(pathIntersection.pixelCoord, _RaytracingSampleIndex, 4 * currentDepth); - - // For the time being, we test for volumetric scattering only on camera rays - if (!currentDepth && computeDirect) - sampleVolume = SampleVolumeScatteringPosition(pathIntersection.pixelCoord, inputSample.w, pathIntersection.t, pdf, sampleLocalLights, lightPosition); - } - - if (sampleVolume) - ComputeVolumeScattering(pathIntersection, inputSample.xyz, sampleLocalLights, lightPosition); - else - ComputeSurfaceScattering(pathIntersection, attributeData, inputSample); - - computeDirect &= !sampleVolume; - -#else // HAS_LIGHTLOOP - + float4 inputSample = GetSample4D(pathIntersection.pixelCoord, _RaytracingSampleIndex, 0); ComputeSurfaceScattering(pathIntersection, attributeData, inputSample); -#endif // HAS_LIGHTLOOP - - // Apply volumetric attenuation - ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), pathIntersection.t, pathIntersection.value, computeDirect); - - // Apply the volume/surface pdf - pathIntersection.value /= pdf; - - if (currentDepth) - { - // Bias the result (making it too dark), but reduces fireflies a lot - float intensity = Luminance(pathIntersection.value) * GetCurrentExposureMultiplier(); - if (intensity > _RaytracingIntensityClamp) - pathIntersection.value *= _RaytracingIntensityClamp / intensity; - } + ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), pathIntersection.t, pathIntersection.value); } diff --git a/com.unity.render-pipelines.high-definition/Runtime/Unity.RenderPipelines.HighDefinition.Runtime.asmdef b/com.unity.render-pipelines.high-definition/Runtime/Unity.RenderPipelines.HighDefinition.Runtime.asmdef index a935c06773b..c5d697e10f6 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Unity.RenderPipelines.HighDefinition.Runtime.asmdef +++ b/com.unity.render-pipelines.high-definition/Runtime/Unity.RenderPipelines.HighDefinition.Runtime.asmdef @@ -50,6 +50,11 @@ "name": "com.unity.burst", "expression": "1.5.0", "define": "ENABLE_BURST_1_5_0_OR_NEWER" + }, + { + "name": "com.unity.sensorsdk", + "expression": "0.0.0", + "define": "ENABLE_SENSOR_SDK" } ], "noEngineReferences": false From f250113b7e2ff86dc39e54f21e5007fa04c8db1f Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 15:09:57 +0100 Subject: [PATCH 15/29] Added sensor meta file. --- .../ShaderPass/ShaderPassSensorLidar.hlsl.meta | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl.meta diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl.meta b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl.meta new file mode 100644 index 00000000000..7b6b258128a --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 4b6742cd49a19474897e03ea861b7208 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: From cf13b8739757543d208d37327c66cd009395904e Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 15:12:39 +0100 Subject: [PATCH 16/29] ... --- .../Runtime/RenderPipeline/PathTracing/PathTracing.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs index b423e87811f..e69477e6d4c 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs @@ -282,7 +282,7 @@ class RenderPathTracingData public TextureHandle sky; } -#if !ENABLE_SENSOR_SDK +#if ENABLE_SENSOR_SDK internal RayTracingShader pathTracingShaderOverride { private get; set; } = null; internal Action prepareDispatchRays { private get; set; } = null; #endif @@ -291,7 +291,7 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C { using (var builder = renderGraph.AddRenderPass("Render PathTracing", out var passData)) { -#if !ENABLE_SENSOR_SDK +#if ENABLE_SENSOR_SDK passData.pathTracingShader = pathTracingShaderOverride ? pathTracingShaderOverride : m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; #else passData.pathTracingShader = m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; @@ -353,7 +353,7 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracingDoFParameters, data.dofParameters); ctx.cmd.SetRayTracingVectorParam(data.pathTracingShader, HDShaderIDs._PathTracingTilingParameters, data.tilingParameters); -#if !ENABLE_SENSOR_SDK +#if ENABLE_SENSOR_SDK // SensorSDK can do its own camera rays generation prepareDispatchRays?.Invoke(ctx.cmd); #endif From 28bb0b9a92582018112e1fdbd2532b0e50b48e1f Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 15:16:20 +0100 Subject: [PATCH 17/29] Cosmetic. --- .../PathTracing/Shaders/PathTracingIntersection.hlsl | 7 ++----- .../RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl | 4 +--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl index 8d7bc86f45f..43be3a67728 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl @@ -21,15 +21,12 @@ struct PathIntersection // Max roughness encountered along the path float maxRoughness; -// SensorSDK - Begin #ifdef SENSORSDK_ENABLE_LIDAR float3 beamDirection; float3 beamOrigin; - - float beamRadius; - float beamDepth; + float beamRadius; + float beamDepth; #endif -//SensorSDK - End }; #endif // UNITY_PATH_TRACING_INTERSECTION_INCLUDED diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl index 8509ba992e2..a3462f0899a 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl @@ -1,12 +1,10 @@ -//SensorSDK - Begin +// SensorSDK support: in Lidar mode, an alternate computation is used, implemented in a separate file #ifdef SENSORSDK_OVERRIDE_REFLECTANCE #define SENSORSDK_ENABLE_LIDAR #endif - #ifdef SENSORSDK_ENABLE_LIDAR #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl" #else -//SensorSDK - End // Ray tracing includes #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingFragInputs.hlsl" From ac3ecc31e4f5ff1bdb92a562e415a2b4d221dddf Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 15:29:31 +0100 Subject: [PATCH 18/29] ... --- .../Runtime/Unity.RenderPipelines.HighDefinition.Runtime.asmdef | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Unity.RenderPipelines.HighDefinition.Runtime.asmdef b/com.unity.render-pipelines.high-definition/Runtime/Unity.RenderPipelines.HighDefinition.Runtime.asmdef index c5d697e10f6..1a646a4edf3 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Unity.RenderPipelines.HighDefinition.Runtime.asmdef +++ b/com.unity.render-pipelines.high-definition/Runtime/Unity.RenderPipelines.HighDefinition.Runtime.asmdef @@ -53,7 +53,7 @@ }, { "name": "com.unity.sensorsdk", - "expression": "0.0.0", + "expression": "1.0.0", "define": "ENABLE_SENSOR_SDK" } ], From d55aa38418c0510ea6e8dc3c6c49f7ca43922cdf Mon Sep 17 00:00:00 2001 From: marclt Date: Thu, 4 Nov 2021 10:52:32 -0400 Subject: [PATCH 19/29] Move pathtracingShaderOverride and prepareDispatchRays into hdcamera to make the override per camera. --- .../Runtime/RenderPipeline/Camera/HDCamera.cs | 5 +++++ .../Runtime/RenderPipeline/PathTracing/PathTracing.cs | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs index d8239a9d87a..7118700cc4e 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs @@ -257,6 +257,11 @@ internal struct VolumetricCloudsAnimationData public float verticalErosionOffset; } +#if ENABLE_SENSOR_SDK + internal RayTracingShader pathTracingShaderOverride { get; set; } = null; + internal Action prepareDispatchRays { get; set; } = null; +#endif + internal Vector4[] frustumPlaneEquations; internal int taaFrameIndex; internal float taaSharpenStrength; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs index e69477e6d4c..8cec1a822a7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs @@ -280,19 +280,19 @@ class RenderPathTracingData public TextureHandle output; public TextureHandle sky; - } #if ENABLE_SENSOR_SDK - internal RayTracingShader pathTracingShaderOverride { private get; set; } = null; - internal Action prepareDispatchRays { private get; set; } = null; + public HDCamera hdCamera; #endif + } TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in CameraData cameraData, TextureHandle pathTracingBuffer, TextureHandle skyBuffer) { using (var builder = renderGraph.AddRenderPass("Render PathTracing", out var passData)) { #if ENABLE_SENSOR_SDK - passData.pathTracingShader = pathTracingShaderOverride ? pathTracingShaderOverride : m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; + passData.pathTracingShader = hdCamera.pathTracingShaderOverride ? hdCamera.pathTracingShaderOverride : m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; + passData.hdCamera = hdCamera; #else passData.pathTracingShader = m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; #endif @@ -355,7 +355,7 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C #if ENABLE_SENSOR_SDK // SensorSDK can do its own camera rays generation - prepareDispatchRays?.Invoke(ctx.cmd); + data.hdCamera.prepareDispatchRays?.Invoke(ctx.cmd); #endif // Run the computation ctx.cmd.DispatchRays(data.pathTracingShader, "RayGen", (uint)data.width, (uint)data.height, 1); From c3e1f7a52d67bdd5e9ba18c26efcdad2644b9b0a Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 15:54:46 +0100 Subject: [PATCH 20/29] Changed li.shader defines, for Sensor mode. --- .../Runtime/Material/Lit/Lit.shader | 4 ++++ .../RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader index f1c9e4c2820..d55352a78dd 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader @@ -1199,6 +1199,10 @@ Shader "HDRP/Lit" #pragma multi_compile _ DEBUG_DISPLAY #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE + #ifdef SENSORSDK_OVERRIDE_REFLECTANCE + #define SENSORSDK_ENABLE_LIDAR + #endif + #define SHADERPASS SHADERPASS_PATH_TRACING // This is just because it needs to be defined, shadow maps are not used. diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl index a3462f0899a..93ad0378343 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassPathTracing.hlsl @@ -1,7 +1,4 @@ // SensorSDK support: in Lidar mode, an alternate computation is used, implemented in a separate file -#ifdef SENSORSDK_OVERRIDE_REFLECTANCE -#define SENSORSDK_ENABLE_LIDAR -#endif #ifdef SENSORSDK_ENABLE_LIDAR #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl" #else From c6dea884c0511625e81fca2cfaf66572cd5696aa Mon Sep 17 00:00:00 2001 From: marclt Date: Thu, 4 Nov 2021 11:04:48 -0400 Subject: [PATCH 21/29] Enable SENSORSDK_ENABLE_LIDAR if SENSORSDK_OVERRIDE_REFLECTANCE is defined in lit.shader and Unlit.shader. --- .../Runtime/Material/Lit/Lit.shader | 4 ++++ .../Runtime/Material/Unlit/Unlit.shader | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader index f1c9e4c2820..d250bac5166 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/Lit.shader @@ -1199,6 +1199,10 @@ Shader "HDRP/Lit" #pragma multi_compile _ DEBUG_DISPLAY #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE + #ifdef SENSORSDK_OVERRIDE_REFLECTANCE + #define SENSORSDK_ENABLE_LIDAR + #endif + #define SHADERPASS SHADERPASS_PATH_TRACING // This is just because it needs to be defined, shadow maps are not used. diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader index a48b95e6312..531fd311163 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader @@ -603,6 +603,10 @@ Shader "HDRP/Unlit" #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE + #ifdef SENSORSDK_OVERRIDE_REFLECTANCE + #define SENSORSDK_ENABLE_LIDAR + #endif + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingMacros.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" From 0ee3da32ec92cb9d6a55b0d228a51392e761d660 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 16:10:27 +0100 Subject: [PATCH 22/29] Minor refactor. --- .../Runtime/RenderPipeline/Camera/HDCamera.cs | 4 ++-- .../Runtime/RenderPipeline/PathTracing/PathTracing.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs index 7118700cc4e..81f038c5860 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Camera/HDCamera.cs @@ -258,8 +258,8 @@ internal struct VolumetricCloudsAnimationData } #if ENABLE_SENSOR_SDK - internal RayTracingShader pathTracingShaderOverride { get; set; } = null; - internal Action prepareDispatchRays { get; set; } = null; + internal RayTracingShader pathTracingShaderOverride = null; + internal Action prepareDispatchRays = null; #endif internal Vector4[] frustumPlaneEquations; diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs index 8cec1a822a7..040006ac12f 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/PathTracing.cs @@ -282,7 +282,7 @@ class RenderPathTracingData public TextureHandle sky; #if ENABLE_SENSOR_SDK - public HDCamera hdCamera; + public Action prepareDispatchRays; #endif } @@ -292,7 +292,7 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C { #if ENABLE_SENSOR_SDK passData.pathTracingShader = hdCamera.pathTracingShaderOverride ? hdCamera.pathTracingShaderOverride : m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; - passData.hdCamera = hdCamera; + passData.prepareDispatchRays = hdCamera.prepareDispatchRays; #else passData.pathTracingShader = m_GlobalSettings.renderPipelineRayTracingResources.pathTracing; #endif @@ -355,7 +355,7 @@ TextureHandle RenderPathTracing(RenderGraph renderGraph, HDCamera hdCamera, in C #if ENABLE_SENSOR_SDK // SensorSDK can do its own camera rays generation - data.hdCamera.prepareDispatchRays?.Invoke(ctx.cmd); + data.prepareDispatchRays?.Invoke(ctx.cmd); #endif // Run the computation ctx.cmd.DispatchRays(data.pathTracingShader, "RayGen", (uint)data.width, (uint)data.height, 1); From 65130476873c815cb3a5afdfc70a751c1c247065 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 17:43:54 +0100 Subject: [PATCH 23/29] Refactor for payload support. --- .../Runtime/Material/Unlit/Unlit.shader | 6 -- .../Shaders/PathTracingIntersection.hlsl | 7 -- .../Shaders/SensorIntersection.hlsl | 37 +++++++++ .../ShaderPass/ShaderPassSensorLidar.hlsl | 77 +++++-------------- 4 files changed, 56 insertions(+), 71 deletions(-) create mode 100644 com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/SensorIntersection.hlsl diff --git a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader index 531fd311163..78916d0c33b 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Material/Unlit/Unlit.shader @@ -601,12 +601,6 @@ Shader "HDRP/Unlit" #define SHADER_UNLIT #define HAS_LIGHTLOOP // Used when computing volumetric scattering - #pragma multi_compile _ SENSORSDK_OVERRIDE_REFLECTANCE - - #ifdef SENSORSDK_OVERRIDE_REFLECTANCE - #define SENSORSDK_ENABLE_LIDAR - #endif - #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/RaytracingMacros.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl index 43be3a67728..b803427b1b7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl @@ -20,13 +20,6 @@ struct PathIntersection uint2 pixelCoord; // Max roughness encountered along the path float maxRoughness; - -#ifdef SENSORSDK_ENABLE_LIDAR - float3 beamDirection; - float3 beamOrigin; - float beamRadius; - float beamDepth; -#endif }; #endif // UNITY_PATH_TRACING_INTERSECTION_INCLUDED diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/SensorIntersection.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/SensorIntersection.hlsl new file mode 100644 index 00000000000..9d343bc0479 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/SensorIntersection.hlsl @@ -0,0 +1,37 @@ +#ifndef UNITY_SENSOR_INTERSECTION_INCLUDED +#define UNITY_SENSOR_INTERSECTION_INCLUDED + +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl" + +// As we must keep the PathIntersection structure untouched (and at the very least of the same size), +// we alias some of its unused fields to store the sensors data. + +float3 GetBeamOrigin(PathIntersection payload) +{ + return payload.value; +} + +void SetBeamOrigin(inout PathIntersection payload, float3 beamOrigin) +{ + payload.value = beamOrigin; +} + +float3 GetBeamDirection(PathIntersection payload) +{ + return float3(payload.alpha, payload.cone.width, payload.cone.spreadAngle); +} + +void SetBeamDirection(inout PathIntersection payload, float3 beamDirection) +{ + payload.alpha = beamDirection.x; + payload.cone.width = beamDirection.y; + payload.cone.spreadAngle = beamDirection.z; +} + +void clearBeamData(inout PathIntersection payload) +{ + SetBeamOrigin(payload, 0.0); + SetBeamDirection(payload, 0.0); +} + +#endif // UNITY_SENSOR_INTERSECTION_INCLUDED diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl index eaa59325170..f27eafedd54 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl @@ -3,7 +3,7 @@ #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/Raytracing/Shaders/Common/AtmosphericScatteringRayTracing.hlsl" // Path tracing includes -#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingIntersection.hlsl" +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/SensorIntersection.hlsl" #ifdef HAS_LIGHTLOOP #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/PathTracingLight.hlsl" #endif @@ -30,18 +30,16 @@ float3 OverrideReflectance() #endif // SENSORSDK_OVERRIDE_REFLECTANCE bool SampleBeam(LightData lightData, + float3 lightPosition, + float3 lightDirection, float3 position, float3 normal, out float3 outgoingDir, - out float3 value, - inout PathIntersection payload) + out float3 value) { const float MM_TO_M = 1e-3; const float M_TO_MM = 1e3; - float3 lightDirection = payload.beamDirection; - float3 lightPosition = payload.beamOrigin; - outgoingDir = position - lightPosition; float dist = length(outgoingDir); outgoingDir /= dist; @@ -71,10 +69,9 @@ bool SampleBeam(LightData lightData, const float wzSq = wz*wz; float gaussianFactor = exp(-2 * rSq / wzSq) / (PI * wzSq); // 1/m^2 - value = gaussianFactor * Eoz; // W/m^2 - - payload.beamRadius = wz; - payload.beamDepth = zFromAperture; + value.x = gaussianFactor * Eoz; // W/m^2 + value.y = wz; // beamRadius + value.z = zFromAperture; // beamDepth // sampling a point in the "virtual" aperture // Find the actual point in the beam aperture that corresponds to this point @@ -85,7 +82,7 @@ bool SampleBeam(LightData lightData, dist = length(outgoingDir); outgoingDir /= dist; - return any(value); + return value.x > 0.0; } void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes, float4 inputSample) @@ -105,11 +102,10 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa return; } - // Grab depth information - uint currentDepth = _RaytracingMaxRecursion - pathIntersection.remainingDepth; - - // Make sure to add the additional travel distance - pathIntersection.cone.width += pathIntersection.t * abs(pathIntersection.cone.spreadAngle); + // Fetch, then clear the beam data aliased in our payload + const float3 beamOrigin = GetBeamOrigin(pathIntersection); + const float3 beamDirection = GetBeamDirection(pathIntersection); + clearBeamData(pathIntersection); PositionInputs posInput; posInput.positionWS = fragInput.positionRWS; @@ -126,14 +122,9 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa bool isVisible; GetSurfaceAndBuiltinData(fragInput, -WorldRayDirection(), posInput, surfaceData, builtinData, currentVertex, pathIntersection.cone, isVisible); - // Check if we want to compute direct and emissive lighting for current depth - bool computeDirect = currentDepth >= _RaytracingMinRecursion - 1; - // Compute the bsdf data BSDFData bsdfData = ConvertSurfaceDataToBSDFData(posInput.positionSS, surfaceData); -#ifndef SHADER_UNLIT - #ifdef SENSORSDK_OVERRIDE_REFLECTANCE // Override the diffuce color when using builtin lit shader (but not with shader graph) bsdfData.diffuseColor = OverrideReflectance(); @@ -146,9 +137,6 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa // Compute the world space position (the non-camera relative one if camera relative rendering is enabled) float3 shadingPosition = fragInput.positionRWS; - // And reset the ray intersection color, which will store our final result - pathIntersection.value = computeDirect ? builtinData.emissiveColor : 0.0; - // Initialize our material data (this will alter the bsdfData to suit path tracing, and choose between BSDF or SSS evaluation) MaterialData mtlData; if (CreateMaterialData(pathIntersection, builtinData, bsdfData, shadingPosition, inputSample.z, mtlData)) @@ -164,54 +152,27 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa for (uint i = 0; i < _SensorLightCount; i++) { - if (SampleBeam(_LightDatasRT[i], shadingPosition, bsdfData.normalWS, - direction, value, pathIntersection)) + if (SampleBeam(_LightDatasRT[i], beamOrigin, beamDirection, shadingPosition, bsdfData.normalWS, direction, value)) { EvaluateMaterial(mtlData, direction, mtlResult); - // value is in radian (w/sr) not in lumen (cd/sr) and only the r channel is used - value *= mtlResult.diffValue + mtlResult.specValue; + // value is in radian (w/sr) not in lumen (cd/sr), only on the x channel + value.x *= Luminance(mtlResult.diffValue + mtlResult.specValue); - pathIntersection.value += value; + pathIntersection.value.x += value.x; } } - } - -#else // SHADER_UNLIT - - pathIntersection.value = computeDirect ? bsdfData.color * GetInverseCurrentExposureMultiplier() + builtinData.emissiveColor : 0.0; - // Simulate opacity blending by simply continuing along the current ray - #ifdef _SURFACE_TYPE_TRANSPARENT - if (builtinData.opacity < 1.0) - { - RayDesc rayDescriptor; - float bias = dot(WorldRayDirection(), fragInput.tangentToWorld[2]) > 0.0 ? _RaytracingRayBias : -_RaytracingRayBias; - rayDescriptor.Origin = fragInput.positionRWS + bias * fragInput.tangentToWorld[2]; - rayDescriptor.Direction = WorldRayDirection(); - rayDescriptor.TMin = 0.0; - rayDescriptor.TMax = FLT_INF; - - PathIntersection nextPathIntersection = pathIntersection; - nextPathIntersection.remainingDepth--; - - TraceRay(_RaytracingAccelerationStructure, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, RAYTRACINGRENDERERFLAG_PATH_TRACING, 0, 1, 2, rayDescriptor, nextPathIntersection); - - pathIntersection.value = lerp(nextPathIntersection.value, pathIntersection.value, builtinData.opacity); + // Copy the last beam radius and depth to the payload + pathIntersection.value.yz = value.yz; } - #endif - -#endif // SHADER_UNLIT } [shader("closesthit")] void ClosestHit(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes) { - // Always set the new t value - pathIntersection.t = RayTCurrent(); - float4 inputSample = GetSample4D(pathIntersection.pixelCoord, _RaytracingSampleIndex, 0); ComputeSurfaceScattering(pathIntersection, attributeData, inputSample); - ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), pathIntersection.t, pathIntersection.value); + ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), RayTCurrent(), pathIntersection.value); } From 14b04e12fe049fcadcd68f4880c4e33ad6036aaa Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 18:04:33 +0100 Subject: [PATCH 24/29] Added meta file. --- .../PathTracing/Shaders/SensorIntersection.hlsl.meta | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/SensorIntersection.hlsl.meta diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/SensorIntersection.hlsl.meta b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/SensorIntersection.hlsl.meta new file mode 100644 index 00000000000..02f97ddf1c9 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/PathTracing/Shaders/SensorIntersection.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: ca82d2604e3b1ea4d9680194e7b8ac00 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: From e4a64cb35d0a551cc63fe463196fbc7cb999e49e Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 18:16:36 +0100 Subject: [PATCH 25/29] Set t. --- .../RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl index f27eafedd54..45c6ce743f3 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl @@ -171,6 +171,8 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa [shader("closesthit")] void ClosestHit(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes) { + pathIntersection.t = RayTCurrent(); + float4 inputSample = GetSample4D(pathIntersection.pixelCoord, _RaytracingSampleIndex, 0); ComputeSurfaceScattering(pathIntersection, attributeData, inputSample); From 51fb41ad69a8ac5908f4ebe2ad8f21a67e9b943b Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 18:24:39 +0100 Subject: [PATCH 26/29] Red channel only. --- .../RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl index 45c6ce743f3..8b2d142b685 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl @@ -156,8 +156,8 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa { EvaluateMaterial(mtlData, direction, mtlResult); - // value is in radian (w/sr) not in lumen (cd/sr), only on the x channel - value.x *= Luminance(mtlResult.diffValue + mtlResult.specValue); + // value is in radian (w/sr) not in lumen (cd/sr), only on the red channel + value.x *= mtlResult.diffValue.x + mtlResult.specValue.x; pathIntersection.value.x += value.x; } From cd21783a785349fc891daf3fdf8633b7c0be05bd Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 19:03:42 +0100 Subject: [PATCH 27/29] ... --- .../RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl index 8b2d142b685..b900d9c2e3e 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl @@ -176,5 +176,5 @@ void ClosestHit(inout PathIntersection pathIntersection : SV_RayPayload, Attribu float4 inputSample = GetSample4D(pathIntersection.pixelCoord, _RaytracingSampleIndex, 0); ComputeSurfaceScattering(pathIntersection, attributeData, inputSample); - ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), RayTCurrent(), pathIntersection.value); + ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), pathIntersection.t, pathIntersection.value); } From f877a0401126dc39a95b7f75d24557840f41b299 Mon Sep 17 00:00:00 2001 From: Emmanuel Turquin Date: Thu, 4 Nov 2021 19:38:28 +0100 Subject: [PATCH 28/29] Minor refactor of pass sensor code. --- .../ShaderPass/ShaderPassSensorLidar.hlsl | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl index b900d9c2e3e..d2a6070e9c7 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl @@ -85,9 +85,13 @@ bool SampleBeam(LightData lightData, return value.x > 0.0; } -void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes, float4 inputSample) +[shader("closesthit")] +void ClosestHit(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes) { - // The first thing that we should do is grab the intersection vertex + // Always set the new t value + pathIntersection.t = RayTCurrent(); + + // Then grab the intersection vertex IntersectionVertex currentVertex; GetCurrentIntersectionVertex(attributeData, currentVertex); @@ -139,7 +143,8 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa // Initialize our material data (this will alter the bsdfData to suit path tracing, and choose between BSDF or SSS evaluation) MaterialData mtlData; - if (CreateMaterialData(pathIntersection, builtinData, bsdfData, shadingPosition, inputSample.z, mtlData)) + if (CreateMaterialData(pathIntersection, builtinData, bsdfData, shadingPosition, + GetSample(pathIntersection.pixelCoord, _RaytracingSampleIndex, 0), mtlData)) { #ifdef _SURFACE_TYPE_TRANSPARENT float3 lightNormal = 0.0; @@ -163,18 +168,9 @@ void ComputeSurfaceScattering(inout PathIntersection pathIntersection : SV_RayPa } } + ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), pathIntersection.t, pathIntersection.value); + // Copy the last beam radius and depth to the payload pathIntersection.value.yz = value.yz; } } - -[shader("closesthit")] -void ClosestHit(inout PathIntersection pathIntersection : SV_RayPayload, AttributeData attributeData : SV_IntersectionAttributes) -{ - pathIntersection.t = RayTCurrent(); - - float4 inputSample = GetSample4D(pathIntersection.pixelCoord, _RaytracingSampleIndex, 0); - ComputeSurfaceScattering(pathIntersection, attributeData, inputSample); - - ApplyFogAttenuation(WorldRayOrigin(), WorldRayDirection(), pathIntersection.t, pathIntersection.value); -} From 05fc06dc2296896cb03966b8c31ec07400cdcf61 Mon Sep 17 00:00:00 2001 From: Sebastien Lagarde Date: Thu, 4 Nov 2021 20:48:04 +0100 Subject: [PATCH 29/29] Fix formatting --- .../Runtime/PackageInfo.cs | 2 +- .../RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/PackageInfo.cs b/com.unity.render-pipelines.high-definition/Runtime/PackageInfo.cs index f3bd69a6fac..0607761357c 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/PackageInfo.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/PackageInfo.cs @@ -6,4 +6,4 @@ [assembly: InternalsVisibleTo("Unity.GraphicTests.Performance.HDRP.Editor")] [assembly: InternalsVisibleTo("TestRuntime")] [assembly: InternalsVisibleTo("Unity.SensorSDK.Runtime")] -[assembly: InternalsVisibleTo("Unity.SensorSDK.Editor")] \ No newline at end of file +[assembly: InternalsVisibleTo("Unity.SensorSDK.Editor")] diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl index d2a6070e9c7..c40acbbcf50 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPassSensorLidar.hlsl @@ -151,7 +151,7 @@ void ClosestHit(inout PathIntersection pathIntersection : SV_RayPayload, Attribu #else float3 lightNormal = GetLightNormal(mtlData); #endif - + float3 value, direction; MaterialResult mtlResult;