From 18e34ee21c7780d586f6a7dba1cb9bba01963e66 Mon Sep 17 00:00:00 2001 From: pastasfuture Date: Wed, 10 Jun 2020 17:59:22 -0700 Subject: [PATCH 1/2] Probe Volumes: Cleanup DebugDisplayProbeVolume and support SH2 atlas display. --- .../Runtime/Debug/LightingDebug.cs | 5 + .../Runtime/Debug/LightingDebug.cs.hlsl | 9 +- .../DebugDisplayProbeVolume.shader | 76 ++++++++-- .../Lighting/ProbeVolume/ProbeVolume.hlsl | 137 +---------------- .../ProbeVolume/ProbeVolumeAtlas.hlsl | 139 ++++++++++++++++++ .../ProbeVolume/ProbeVolumeAtlas.hlsl.meta | 10 ++ 6 files changed, 224 insertions(+), 152 deletions(-) create mode 100644 com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl create mode 100644 com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl.meta diff --git a/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs b/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs index f42f81e0b4f..9382d919e11 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs @@ -214,6 +214,11 @@ internal enum ProbeVolumeAtlasSliceMode IrradianceSH1_1, IrradianceSH10, IrradianceSH11, + IrradianceSH2_2, + IrradianceSH2_1, + IrradianceSH20, + IrradianceSH21, + IrradianceSH22, Validity, OctahedralDepth } diff --git a/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs.hlsl index 7fc211fe0a9..dad80848b91 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Debug/LightingDebug.cs.hlsl @@ -88,8 +88,13 @@ #define PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH1_1 (1) #define PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH10 (2) #define PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH11 (3) -#define PROBEVOLUMEATLASSLICEMODE_VALIDITY (4) -#define PROBEVOLUMEATLASSLICEMODE_OCTAHEDRAL_DEPTH (5) +#define PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH2_2 (4) +#define PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH2_1 (5) +#define PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH20 (6) +#define PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH21 (7) +#define PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH22 (8) +#define PROBEVOLUMEATLASSLICEMODE_VALIDITY (9) +#define PROBEVOLUMEATLASSLICEMODE_OCTAHEDRAL_DEPTH (10) #endif diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/DebugDisplayProbeVolume.shader b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/DebugDisplayProbeVolume.shader index b518dc774f4..bb719f894fa 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/DebugDisplayProbeVolume.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/DebugDisplayProbeVolume.shader @@ -6,7 +6,9 @@ Shader "Hidden/ScriptableRenderPipeline/DebugDisplayProbeVolume" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl" + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeShaderVariables.hlsl" + #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl" #define DEBUG_DISPLAY #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Debug/DebugDisplay.hlsl" @@ -18,12 +20,6 @@ Shader "Hidden/ScriptableRenderPipeline/DebugDisplayProbeVolume" int _ProbeVolumeAtlasSliceMode; // float _RcpGlobalScaleFactor; SamplerState ltc_linear_clamp_sampler; - TEXTURE3D(_AtlasTextureSH); - - #if SHADEROPTIONS_PROBE_VOLUMES_BILATERAL_FILTERING == PROBEVOLUMESBILATERALFILTERINGMODES_OCTAHEDRAL_DEPTH - TEXTURE2D(_AtlasTextureOctahedralDepth); - float4 _AtlasTextureOctahedralDepthScaleBias; - #endif struct Attributes { @@ -76,10 +72,37 @@ Shader "Hidden/ScriptableRenderPipeline/DebugDisplayProbeVolume" // Convert to specific view section of atlas. uvw = uvw * _TextureViewScale + _TextureViewBias; - float4 valueShAr = saturate((SAMPLE_TEXTURE3D_LOD(_AtlasTextureSH, ltc_linear_clamp_sampler, float3(uvw.x, uvw.y, uvw.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 0), 0) - _ValidRange.x) * _ValidRange.y); - float4 valueShAg = saturate((SAMPLE_TEXTURE3D_LOD(_AtlasTextureSH, ltc_linear_clamp_sampler, float3(uvw.x, uvw.y, uvw.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 1), 0) - _ValidRange.x) * _ValidRange.y); - float4 valueShAb = saturate((SAMPLE_TEXTURE3D_LOD(_AtlasTextureSH, ltc_linear_clamp_sampler, float3(uvw.x, uvw.y, uvw.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 2), 0) - _ValidRange.x) * _ValidRange.y); - float valueValidity = saturate((SAMPLE_TEXTURE3D_LOD(_AtlasTextureSH, ltc_linear_clamp_sampler, float3(uvw.x, uvw.y, uvw.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 3), 0).x - _ValidRange.x) * _ValidRange.y); + #if SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L1 + ProbeVolumeSphericalHarmonicsL1 coefficients; + ZERO_INITIALIZE(ProbeVolumeSphericalHarmonicsL1, coefficients); + ProbeVolumeSampleAccumulateSphericalHarmonicsL1(uvw, 1.0f, coefficients); + ProbeVolumeSwizzleAndNormalizeSphericalHarmonicsL1(coefficients); + float4 valueShAr = saturate((coefficients.data[0] - _ValidRange.x) * _ValidRange.y); + float4 valueShAg = saturate((coefficients.data[1] - _ValidRange.x) * _ValidRange.y); + float4 valueShAb = saturate((coefficients.data[2] - _ValidRange.x) * _ValidRange.y); + + float4 valueShBr = 0.0f; + float4 valueShBg = 0.0f; + float4 valueShBb = 0.0f; + float4 valueShC = 0.0f; + + #elif SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L2 + ProbeVolumeSphericalHarmonicsL2 coefficients; + ZERO_INITIALIZE(ProbeVolumeSphericalHarmonicsL2, coefficients); + ProbeVolumeSampleAccumulateSphericalHarmonicsL2(uvw, 1.0f, coefficients); + ProbeVolumeSwizzleAndNormalizeSphericalHarmonicsL2(coefficients); + float4 valueShAr = saturate((coefficients.data[0] - _ValidRange.x) * _ValidRange.y); + float4 valueShAg = saturate((coefficients.data[1] - _ValidRange.x) * _ValidRange.y); + float4 valueShAb = saturate((coefficients.data[2] - _ValidRange.x) * _ValidRange.y); + + float4 valueShBr = saturate((coefficients.data[3] - _ValidRange.x) * _ValidRange.y); + float4 valueShBg = saturate((coefficients.data[4] - _ValidRange.x) * _ValidRange.y); + float4 valueShBb = saturate((coefficients.data[5] - _ValidRange.x) * _ValidRange.y); + float4 valueShC = saturate((coefficients.data[6] - _ValidRange.x) * _ValidRange.y); + + #endif + + float valueValidity = saturate((ProbeVolumeSampleValidity(uvw) - _ValidRange.x) * _ValidRange.y); #if SHADEROPTIONS_PROBE_VOLUMES_BILATERAL_FILTERING == PROBEVOLUMESBILATERALFILTERINGMODES_OCTAHEDRAL_DEPTH float2 valueOctahedralDepthMeanAndVariance = saturate((SAMPLE_TEXTURE2D_LOD(_AtlasTextureOctahedralDepth, ltc_linear_clamp_sampler, input.texcoord * _AtlasTextureOctahedralDepthScaleBias.xy + _AtlasTextureOctahedralDepthScaleBias.zw, 0).xy - _ValidRange.x) * _ValidRange.y); @@ -90,22 +113,47 @@ Shader "Hidden/ScriptableRenderPipeline/DebugDisplayProbeVolume" case PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH00: { - return float4(valueShAr.x, valueShAg.x, valueShAb.x, 1); + return float4(valueShAr.w, valueShAg.w, valueShAb.w, 1); } case PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH1_1: { - return float4(valueShAr.y, valueShAg.y, valueShAb.y, 1); + return float4(valueShAr.x, valueShAg.x, valueShAb.x, 1); } case PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH10: { - return float4(valueShAr.z, valueShAg.z, valueShAb.z, 1); + return float4(valueShAr.y, valueShAg.y, valueShAb.y, 1); } case PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH11: { - return float4(valueShAr.w, valueShAg.w, valueShAb.w, 1); + return float4(valueShAr.z, valueShAg.z, valueShAb.z, 1); + } + + case PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH2_2: + { + return float4(valueShBr.x, valueShBg.x, valueShBb.x, 1); + } + + case PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH2_1: + { + return float4(valueShBr.y, valueShBg.y, valueShBb.y, 1); + } + + case PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH20: + { + return float4(valueShBr.z, valueShBg.z, valueShBb.z, 1); + } + + case PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH21: + { + return float4(valueShBr.w, valueShBg.w, valueShBb.w, 1); + } + + case PROBEVOLUMEATLASSLICEMODE_IRRADIANCE_SH22: + { + return float4(valueShC.x, valueShC.y, valueShC.z, 1); } case PROBEVOLUMEATLASSLICEMODE_VALIDITY: diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolume.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolume.hlsl index 23943dfde83..ff11d05d630 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolume.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolume.hlsl @@ -5,6 +5,7 @@ #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Packing.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolume.cs.hlsl" #include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeLightLoopDef.hlsl" +#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl" // Copied from VolumeVoxelization.compute float ProbeVolumeComputeFadeFactor( @@ -23,28 +24,6 @@ float ProbeVolumeComputeFadeFactor( return dstF * fade; } -float ProbeVolumeSampleValidity(float3 probeVolumeAtlasUVW) -{ -#if SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L1 - return SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 3), 0).x; -#elif SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L2 - return SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 6), 0).w; -#else - return 0.0; -#endif -} - -float ProbeVolumeLoadValidity(int3 probeVolumeAtlasTexelCoord) -{ -#if SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L1 - return LOAD_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, int3(probeVolumeAtlasTexelCoord.x, probeVolumeAtlasTexelCoord.y, probeVolumeAtlasTexelCoord.z + _ProbeVolumeAtlasResolutionAndSliceCount.z * 3), 0).x; -#elif SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L2 - return LOAD_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, int3(probeVolumeAtlasTexelCoord.x, probeVolumeAtlasTexelCoord.y, probeVolumeAtlasTexelCoord.z + _ProbeVolumeAtlasResolutionAndSliceCount.z * 6), 0).w; -#else - return 0.0; -#endif -} - #if SHADEROPTIONS_PROBE_VOLUMES_BILATERAL_FILTERING == PROBEVOLUMESBILATERALFILTERINGMODES_OCTAHEDRAL_DEPTH void ProbeVolumeEvaluateOctahedralDepthOcclusionFilterWeights( out float weights[8], @@ -368,120 +347,6 @@ float3 ProbeVolumeComputeTexel3DFromBilateralFilter( #endif } -struct ProbeVolumeSphericalHarmonicsL0 -{ - float4 data[1]; -}; - -struct ProbeVolumeSphericalHarmonicsL1 -{ - float4 data[3]; -}; - -struct ProbeVolumeSphericalHarmonicsL2 -{ - float4 data[7]; -}; - -// See ProbeVolumeAtlasBlit.compute for atlas coefficient layout information. -void ProbeVolumeSampleAccumulateSphericalHarmonicsL0(float3 probeVolumeAtlasUVW, float weight, inout ProbeVolumeSphericalHarmonicsL0 coefficients) -{ - coefficients.data[0].xyz += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 0), 0).xyz * weight; -} - -void ProbeVolumeSampleAccumulateSphericalHarmonicsL1(float3 probeVolumeAtlasUVW, float weight, inout ProbeVolumeSphericalHarmonicsL1 coefficients) -{ -#if SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L1 || SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L2 - coefficients.data[0] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 0), 0) * weight; - coefficients.data[1] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 1), 0) * weight; - coefficients.data[2] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 2), 0) * weight; -#endif -} - -void ProbeVolumeSampleAccumulateSphericalHarmonicsL2(float3 probeVolumeAtlasUVW, float weight, inout ProbeVolumeSphericalHarmonicsL2 coefficients) -{ -#if SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L1 - // Requesting SH2, but atlas only contains SH1. - // Only accumulate SH1 coefficients. - coefficients.data[0] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 0), 0) * weight; - coefficients.data[1] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 1), 0) * weight; - coefficients.data[2] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 2), 0) * weight; - -#elif SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L2 - coefficients.data[0] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 0), 0) * weight; - coefficients.data[1] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 1), 0) * weight; - coefficients.data[2] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 2), 0) * weight; - - coefficients.data[3] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 3), 0) * weight; - coefficients.data[4] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 4), 0) * weight; - coefficients.data[5] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 5), 0) * weight; - - coefficients.data[6].xyz += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 6), 0).xyz * weight; -#endif -} - - -// Utility functions for converting from atlas coefficients layout, into the layout that our EntityLighting.hlsl evaluation functions expect. -void ProbeVolumeSwizzleAndNormalizeSphericalHarmonicsL0(inout ProbeVolumeSphericalHarmonicsL0 coefficients) -{ - // Nothing to do here. DC terms are already normalized and stored in RGB order. -} - -void ProbeVolumeSwizzleAndNormalizeSphericalHarmonicsL1(inout ProbeVolumeSphericalHarmonicsL1 coefficients) -{ -#ifdef DEBUG_DISPLAY - if (_DebugProbeVolumeMode == PROBEVOLUMEDEBUGMODE_VISUALIZE_DEBUG_COLORS || _DebugProbeVolumeMode == PROBEVOLUMEDEBUGMODE_VISUALIZE_VALIDITY) - { - // coefficients are storing debug info. Do not swizzle or normalize. - return; - } -#endif - - // SHEvalLinearL0L1() expects coefficients in real4 shAr, real4 shAg, real4 shAb vectors whos channels are laid out {x, y, z, DC} - float4 shAr = float4(coefficients.data[0].w, coefficients.data[1].x, coefficients.data[1].y, coefficients.data[0].x); - float4 shAg = float4(coefficients.data[1].z, coefficients.data[1].w, coefficients.data[2].x, coefficients.data[0].y); - float4 shAb = float4(coefficients.data[2].y, coefficients.data[2].z, coefficients.data[2].w, coefficients.data[0].z); - - coefficients.data[0] = shAr; - coefficients.data[1] = shAg; - coefficients.data[2] = shAb; -} - -void ProbeVolumeSwizzleAndNormalizeSphericalHarmonicsL2(inout ProbeVolumeSphericalHarmonicsL2 coefficients) -{ -#ifdef DEBUG_DISPLAY - if (_DebugProbeVolumeMode == PROBEVOLUMEDEBUGMODE_VISUALIZE_DEBUG_COLORS || _DebugProbeVolumeMode == PROBEVOLUMEDEBUGMODE_VISUALIZE_VALIDITY) - { - // coefficients are storing debug info. Do not swizzle or normalize. - return; - } -#endif - - // SampleSH9() expects coefficients in shAr, shAg, shAb, shBr, shBg, shBb, shCr vectors. - float4 shAr = float4(coefficients.data[0].w, coefficients.data[1].x, coefficients.data[1].y, coefficients.data[0].x); - float4 shAg = float4(coefficients.data[1].z, coefficients.data[1].w, coefficients.data[2].x, coefficients.data[0].y); - float4 shAb = float4(coefficients.data[2].y, coefficients.data[2].z, coefficients.data[2].w, coefficients.data[0].z); - - coefficients.data[0] = shAr; - coefficients.data[1] = shAg; - coefficients.data[2] = shAb; - - // coefficients[3] through coefficients[6] are already laid out in shBr, shBg, shBb, shCr order. - // Now just need to perform final SH2 normalization: - // Again, normalization from: https://www.ppsloan.org/publications/StupidSH36.pdf - // Appendix A10 Shader/CPU code for Irradiance Environment Maps - - // Normalize DC term: - coefficients.data[0].w -= coefficients.data[3].z; - coefficients.data[1].w -= coefficients.data[4].z; - coefficients.data[2].w -= coefficients.data[5].z; - - // Normalize Quadratic term: - coefficients.data[3].z *= 3.0; - coefficients.data[4].z *= 3.0; - coefficients.data[5].z *= 3.0; -} - float3 ProbeVolumeEvaluateSphericalHarmonicsL0(float3 normalWS, ProbeVolumeSphericalHarmonicsL0 coefficients) { diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl new file mode 100644 index 00000000000..15a973ecccc --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl @@ -0,0 +1,139 @@ +#ifndef __PROBEVOLUMEATLAS_HLSL__ +#define __PROBEVOLUMEATLAS_HLSL__ + +float ProbeVolumeSampleValidity(float3 probeVolumeAtlasUVW) +{ +#if SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L1 + return SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 3), 0).x; +#elif SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L2 + return SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 6), 0).w; +#else + return 0.0; +#endif +} + +float ProbeVolumeLoadValidity(int3 probeVolumeAtlasTexelCoord) +{ +#if SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L1 + return LOAD_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, int3(probeVolumeAtlasTexelCoord.x, probeVolumeAtlasTexelCoord.y, probeVolumeAtlasTexelCoord.z + _ProbeVolumeAtlasResolutionAndSliceCount.z * 3), 0).x; +#elif SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L2 + return LOAD_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, int3(probeVolumeAtlasTexelCoord.x, probeVolumeAtlasTexelCoord.y, probeVolumeAtlasTexelCoord.z + _ProbeVolumeAtlasResolutionAndSliceCount.z * 6), 0).w; +#else + return 0.0; +#endif +} + +struct ProbeVolumeSphericalHarmonicsL0 +{ + float4 data[1]; +}; + +struct ProbeVolumeSphericalHarmonicsL1 +{ + float4 data[3]; +}; + +struct ProbeVolumeSphericalHarmonicsL2 +{ + float4 data[7]; +}; + +// See ProbeVolumeAtlasBlit.compute for atlas coefficient layout information. +void ProbeVolumeSampleAccumulateSphericalHarmonicsL0(float3 probeVolumeAtlasUVW, float weight, inout ProbeVolumeSphericalHarmonicsL0 coefficients) +{ + coefficients.data[0].xyz += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 0), 0).xyz * weight; +} + +void ProbeVolumeSampleAccumulateSphericalHarmonicsL1(float3 probeVolumeAtlasUVW, float weight, inout ProbeVolumeSphericalHarmonicsL1 coefficients) +{ +#if SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L1 || SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L2 + coefficients.data[0] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 0), 0) * weight; + coefficients.data[1] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 1), 0) * weight; + coefficients.data[2] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 2), 0) * weight; +#endif +} + +void ProbeVolumeSampleAccumulateSphericalHarmonicsL2(float3 probeVolumeAtlasUVW, float weight, inout ProbeVolumeSphericalHarmonicsL2 coefficients) +{ +#if SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L1 + // Requesting SH2, but atlas only contains SH1. + // Only accumulate SH1 coefficients. + coefficients.data[0] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 0), 0) * weight; + coefficients.data[1] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 1), 0) * weight; + coefficients.data[2] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 2), 0) * weight; + +#elif SHADEROPTIONS_PROBE_VOLUMES_ENCODING_MODE == PROBEVOLUMESENCODINGMODES_SPHERICAL_HARMONICS_L2 + coefficients.data[0] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 0), 0) * weight; + coefficients.data[1] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 1), 0) * weight; + coefficients.data[2] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 2), 0) * weight; + + coefficients.data[3] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 3), 0) * weight; + coefficients.data[4] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 4), 0) * weight; + coefficients.data[5] += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 5), 0) * weight; + + coefficients.data[6].xyz += SAMPLE_TEXTURE3D_LOD(_ProbeVolumeAtlasSH, s_linear_clamp_sampler, float3(probeVolumeAtlasUVW.x, probeVolumeAtlasUVW.y, probeVolumeAtlasUVW.z + _ProbeVolumeAtlasResolutionAndSliceCountInverse.w * 6), 0).xyz * weight; +#endif +} + +// Utility functions for converting from atlas coefficients layout, into the layout that our EntityLighting.hlsl evaluation functions expect. +void ProbeVolumeSwizzleAndNormalizeSphericalHarmonicsL0(inout ProbeVolumeSphericalHarmonicsL0 coefficients) +{ + // Nothing to do here. DC terms are already normalized and stored in RGB order. +} + +void ProbeVolumeSwizzleAndNormalizeSphericalHarmonicsL1(inout ProbeVolumeSphericalHarmonicsL1 coefficients) +{ +#ifdef DEBUG_DISPLAY + if (_DebugProbeVolumeMode == PROBEVOLUMEDEBUGMODE_VISUALIZE_DEBUG_COLORS || _DebugProbeVolumeMode == PROBEVOLUMEDEBUGMODE_VISUALIZE_VALIDITY) + { + // coefficients are storing debug info. Do not swizzle or normalize. + return; + } +#endif + + // SHEvalLinearL0L1() expects coefficients in real4 shAr, real4 shAg, real4 shAb vectors whos channels are laid out {x, y, z, DC} + float4 shAr = float4(coefficients.data[0].w, coefficients.data[1].x, coefficients.data[1].y, coefficients.data[0].x); + float4 shAg = float4(coefficients.data[1].z, coefficients.data[1].w, coefficients.data[2].x, coefficients.data[0].y); + float4 shAb = float4(coefficients.data[2].y, coefficients.data[2].z, coefficients.data[2].w, coefficients.data[0].z); + + coefficients.data[0] = shAr; + coefficients.data[1] = shAg; + coefficients.data[2] = shAb; +} + +void ProbeVolumeSwizzleAndNormalizeSphericalHarmonicsL2(inout ProbeVolumeSphericalHarmonicsL2 coefficients) +{ +#ifdef DEBUG_DISPLAY + if (_DebugProbeVolumeMode == PROBEVOLUMEDEBUGMODE_VISUALIZE_DEBUG_COLORS || _DebugProbeVolumeMode == PROBEVOLUMEDEBUGMODE_VISUALIZE_VALIDITY) + { + // coefficients are storing debug info. Do not swizzle or normalize. + return; + } +#endif + + // SampleSH9() expects coefficients in shAr, shAg, shAb, shBr, shBg, shBb, shCr vectors. + float4 shAr = float4(coefficients.data[0].w, coefficients.data[1].x, coefficients.data[1].y, coefficients.data[0].x); + float4 shAg = float4(coefficients.data[1].z, coefficients.data[1].w, coefficients.data[2].x, coefficients.data[0].y); + float4 shAb = float4(coefficients.data[2].y, coefficients.data[2].z, coefficients.data[2].w, coefficients.data[0].z); + + coefficients.data[0] = shAr; + coefficients.data[1] = shAg; + coefficients.data[2] = shAb; + + // coefficients[3] through coefficients[6] are already laid out in shBr, shBg, shBb, shCr order. + // Now just need to perform final SH2 normalization: + // Again, normalization from: https://www.ppsloan.org/publications/StupidSH36.pdf + // Appendix A10 Shader/CPU code for Irradiance Environment Maps + + // Normalize DC term: + coefficients.data[0].w -= coefficients.data[3].z; + coefficients.data[1].w -= coefficients.data[4].z; + coefficients.data[2].w -= coefficients.data[5].z; + + // Normalize Quadratic term: + coefficients.data[3].z *= 3.0; + coefficients.data[4].z *= 3.0; + coefficients.data[5].z *= 3.0; +} + +#endif \ No newline at end of file diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl.meta b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl.meta new file mode 100644 index 00000000000..20c97d61698 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeAtlas.hlsl.meta @@ -0,0 +1,10 @@ +fileFormatVersion: 2 +guid: ae2987818c7ac0741b8ac4fca68b19f2 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: From cd4d1b629de7cce96136f1e8a728c3fd23ccd3ef Mon Sep 17 00:00:00 2001 From: pastasfuture Date: Tue, 14 Jul 2020 13:34:07 -0700 Subject: [PATCH 2/2] Probe Volumes: Bugfixes to state when Probe Volumes is enabled in Shader Config, bug disabled in the HDRenderPipelineAsset or the current cameras frame settings. --- .../ProbeVolume/ProbeVolumeLighting.cs | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeLighting.cs b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeLighting.cs index 0fe375a9fb0..be2a0d56818 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeLighting.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/ProbeVolume/ProbeVolumeLighting.cs @@ -154,10 +154,12 @@ void InitializeProbeVolumes() s_ProbeVolumeAtlasOctahedralDepthConvolveCS = asset.renderPipelineResources.shaders.probeVolumeAtlasOctahedralDepthConvolveCS; s_ProbeVolumeAtlasOctahedralDepthConvolveKernel = s_ProbeVolumeAtlasOctahedralDepthConvolveCS.FindKernel("ProbeVolumeAtlasOctahedralDepthConvolveKernel"); } - else - { - CreateProbeVolumeBuffersDefault(); - } + + // Need Default / Fallback buffers for binding in case when ShaderConfig has activated probe volume code, + // and probe volumes has been enabled in the HDRenderPipelineAsset, + // but probe volumes is disabled in the current camera's frame settings. + // This can go away if we add a global keyword for using / completely stripping probe volume code per camera. + CreateProbeVolumeBuffersDefault(); #if UNITY_EDITOR UnityEditor.Lightmapping.lightingDataCleared += OnLightingDataCleared; @@ -411,14 +413,8 @@ unsafe void UpdateShaderVariablesGlobalProbeVolumes(ref ShaderVariablesGlobal cb void PushProbeVolumesGlobalParams(HDCamera hdCamera, CommandBuffer cmd) { - if (ShaderConfig.s_ProbeVolumesEvaluationMode == ProbeVolumesEvaluationModes.Disabled) - return; - - if (!m_SupportProbeVolume) - { - PushProbeVolumesGlobalParamsDefault(hdCamera, cmd); - return; - } + Debug.Assert(ShaderConfig.s_ProbeVolumesEvaluationMode != ProbeVolumesEvaluationModes.Disabled); + Debug.Assert(m_SupportProbeVolume); cmd.SetGlobalBuffer(HDShaderIDs._ProbeVolumeBounds, s_VisibleProbeVolumeBoundsBuffer); cmd.SetGlobalBuffer(HDShaderIDs._ProbeVolumeDatas, s_VisibleProbeVolumeDataBuffer); @@ -432,6 +428,9 @@ void PushProbeVolumesGlobalParams(HDCamera hdCamera, CommandBuffer cmd) internal void PushProbeVolumesGlobalParamsDefault(HDCamera hdCamera, CommandBuffer cmd) { + Debug.Assert(ShaderConfig.s_ProbeVolumesEvaluationMode != ProbeVolumesEvaluationModes.Disabled); + Debug.Assert(hdCamera.frameSettings.IsEnabled(FrameSettingsField.ProbeVolume) == false); + cmd.SetGlobalBuffer(HDShaderIDs._ProbeVolumeBounds, s_VisibleProbeVolumeBoundsBufferDefault); cmd.SetGlobalBuffer(HDShaderIDs._ProbeVolumeDatas, s_VisibleProbeVolumeDataBufferDefault); cmd.SetGlobalTexture(HDShaderIDs._ProbeVolumeAtlasSH, TextureXR.GetBlackTexture3D()); @@ -712,8 +711,20 @@ ProbeVolumeList PrepareVisibleProbeVolumeList(ScriptableRenderContext renderCont return probeVolumes; if (!hdCamera.frameSettings.IsEnabled(FrameSettingsField.ProbeVolume)) - return probeVolumes; + { + PushProbeVolumesGlobalParamsDefault(hdCamera, cmd); + } + else + { + PrepareVisibleProbeVolumeListBuffers(renderContext, hdCamera, cmd, ref probeVolumes); + PushProbeVolumesGlobalParams(hdCamera, cmd); + } + return probeVolumes; + } + + void PrepareVisibleProbeVolumeListBuffers(ScriptableRenderContext renderContext, HDCamera hdCamera, CommandBuffer cmd, ref ProbeVolumeList probeVolumes) + { var settings = hdCamera.volumeStack.GetComponent(); bool octahedralDepthOcclusionFilterIsEnabled = ShaderConfig.s_ProbeVolumesBilateralFilteringMode == ProbeVolumesBilateralFilteringModes.OctahedralDepth @@ -860,9 +871,7 @@ ProbeVolumeList PrepareVisibleProbeVolumeList(ScriptableRenderContext renderCont } } - PushProbeVolumesGlobalParams(hdCamera, cmd); - - return probeVolumes; + return; } }