A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine.Rendering;
3
4namespace UnityEngine.Rendering
5{
6 public partial class ProbeReferenceVolume
7 {
8 internal static class ShaderIDs
9 {
10 // Adaptive Probe Volume
11 public static readonly int _APVResIndex = Shader.PropertyToID("_APVResIndex");
12 public static readonly int _APVResCellIndices = Shader.PropertyToID("_APVResCellIndices");
13 public static readonly int _APVResL0_L1Rx = Shader.PropertyToID("_APVResL0_L1Rx");
14 public static readonly int _APVResL1G_L1Ry = Shader.PropertyToID("_APVResL1G_L1Ry");
15 public static readonly int _APVResL1B_L1Rz = Shader.PropertyToID("_APVResL1B_L1Rz");
16
17 public static readonly int _APVResL2_0 = Shader.PropertyToID("_APVResL2_0");
18 public static readonly int _APVResL2_1 = Shader.PropertyToID("_APVResL2_1");
19 public static readonly int _APVResL2_2 = Shader.PropertyToID("_APVResL2_2");
20 public static readonly int _APVResL2_3 = Shader.PropertyToID("_APVResL2_3");
21
22 public static readonly int _APVProbeOcclusion = Shader.PropertyToID("_APVProbeOcclusion");
23
24 public static readonly int _APVResValidity = Shader.PropertyToID("_APVResValidity");
25 public static readonly int _SkyOcclusionTexL0L1 = Shader.PropertyToID("_SkyOcclusionTexL0L1");
26 public static readonly int _SkyShadingDirectionIndicesTex = Shader.PropertyToID("_SkyShadingDirectionIndicesTex");
27 public static readonly int _SkyPrecomputedDirections = Shader.PropertyToID("_SkyPrecomputedDirections");
28 public static readonly int _AntiLeakData = Shader.PropertyToID("_AntiLeakData");
29 }
30
31 ComputeBuffer m_EmptyIndexBuffer = null;
32
33 /// <summary>
34 /// Bind the global APV resources
35 /// </summary>
36 /// <param name="cmdBuffer">Command buffer</param>
37 /// <param name="isProbeVolumeEnabled">True if APV is enabled</param>
38 public void BindAPVRuntimeResources(CommandBuffer cmdBuffer, bool isProbeVolumeEnabled)
39 {
40 bool needToBindNeutral = true;
41 var refVolume = ProbeReferenceVolume.instance;
42
43 // Do this only if probe volume is enabled
44 if (isProbeVolumeEnabled && m_ProbeReferenceVolumeInit)
45 {
46 ProbeReferenceVolume.RuntimeResources rr = refVolume.GetRuntimeResources();
47
48 bool validResources = rr.index != null && rr.L0_L1rx != null && rr.L1_G_ry != null && rr.L1_B_rz != null;
49 validResources &= (refVolume.shBands == ProbeVolumeSHBands.SphericalHarmonicsL2 && rr.L2_0 != null) || refVolume.shBands == ProbeVolumeSHBands.SphericalHarmonicsL1;
50
51 if (validResources)
52 {
53 cmdBuffer.SetGlobalBuffer(ShaderIDs._APVResIndex, rr.index);
54 cmdBuffer.SetGlobalBuffer(ShaderIDs._APVResCellIndices, rr.cellIndices);
55
56 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL0_L1Rx, rr.L0_L1rx);
57 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL1G_L1Ry, rr.L1_G_ry);
58 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL1B_L1Rz, rr.L1_B_rz);
59 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResValidity, rr.Validity);
60
61 cmdBuffer.SetGlobalTexture(ShaderIDs._SkyOcclusionTexL0L1, rr.SkyOcclusionL0L1 ?? (RenderTargetIdentifier)CoreUtils.blackVolumeTexture);
62 cmdBuffer.SetGlobalTexture(ShaderIDs._SkyShadingDirectionIndicesTex, rr.SkyShadingDirectionIndices ?? (RenderTargetIdentifier)CoreUtils.blackVolumeTexture);
63 cmdBuffer.SetGlobalBuffer(ShaderIDs._SkyPrecomputedDirections, rr.SkyPrecomputedDirections);
64 cmdBuffer.SetGlobalBuffer(ShaderIDs._AntiLeakData, rr.QualityLeakReductionData);
65
66 if (refVolume.shBands == ProbeVolumeSHBands.SphericalHarmonicsL2)
67 {
68 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL2_0, rr.L2_0);
69 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL2_1, rr.L2_1);
70 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL2_2, rr.L2_2);
71 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL2_3, rr.L2_3);
72 }
73
74 cmdBuffer.SetGlobalTexture(ShaderIDs._APVProbeOcclusion, rr.ProbeOcclusion ?? (RenderTargetIdentifier)CoreUtils.whiteVolumeTexture);
75
76 needToBindNeutral = false;
77 }
78 }
79
80 if (needToBindNeutral)
81 {
82 // Lazy init the empty buffer. We use sizeof(uint3) so that this buffer can be
83 // used with uint and uint3 bindings without triggering validation errors.
84 if (m_EmptyIndexBuffer == null)
85 m_EmptyIndexBuffer = new ComputeBuffer(1, sizeof(uint) * 3, ComputeBufferType.Structured);
86
87 cmdBuffer.SetGlobalBuffer(ShaderIDs._APVResIndex, m_EmptyIndexBuffer);
88 cmdBuffer.SetGlobalBuffer(ShaderIDs._APVResCellIndices, m_EmptyIndexBuffer);
89
90 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL0_L1Rx, CoreUtils.blackVolumeTexture);
91
92 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL1G_L1Ry, CoreUtils.blackVolumeTexture);
93 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL1B_L1Rz, CoreUtils.blackVolumeTexture);
94 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResValidity, CoreUtils.blackVolumeTexture);
95
96 cmdBuffer.SetGlobalTexture(ShaderIDs._SkyOcclusionTexL0L1, CoreUtils.blackVolumeTexture);
97 cmdBuffer.SetGlobalTexture(ShaderIDs._SkyShadingDirectionIndicesTex, CoreUtils.blackVolumeTexture);
98 cmdBuffer.SetGlobalBuffer(ShaderIDs._SkyPrecomputedDirections, m_EmptyIndexBuffer);
99 cmdBuffer.SetGlobalBuffer(ShaderIDs._AntiLeakData, m_EmptyIndexBuffer);
100
101 if (refVolume.shBands == ProbeVolumeSHBands.SphericalHarmonicsL2)
102 {
103 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL2_0, CoreUtils.blackVolumeTexture);
104 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL2_1, CoreUtils.blackVolumeTexture);
105 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL2_2, CoreUtils.blackVolumeTexture);
106 cmdBuffer.SetGlobalTexture(ShaderIDs._APVResL2_3, CoreUtils.blackVolumeTexture);
107 }
108
109 cmdBuffer.SetGlobalTexture(ShaderIDs._APVProbeOcclusion, CoreUtils.whiteVolumeTexture);
110 }
111 }
112
113 /// <summary>
114 /// Update the constant buffer used by Probe Volumes in shaders.
115 /// </summary>
116 /// <param name="cmd">A command buffer used to perform the data update.</param>
117 /// <param name="probeVolumeOptions">probe volume options from the active volume stack</param>
118 /// <param name="taaFrameIndex">TAA frame index</param>
119 /// <param name="supportRenderingLayers">Are rendering layers supported</param>
120 /// <returns>True if successful</returns>
121 public bool UpdateShaderVariablesProbeVolumes(CommandBuffer cmd, ProbeVolumesOptions probeVolumeOptions, int taaFrameIndex, bool supportRenderingLayers = false)
122 {
123 bool enableProbeVolumes = DataHasBeenLoaded();
124
125 if (enableProbeVolumes)
126 {
127 ProbeVolumeShadingParameters parameters;
128 parameters.normalBias = probeVolumeOptions.normalBias.value;
129 parameters.viewBias = probeVolumeOptions.viewBias.value;
130 parameters.scaleBiasByMinDistanceBetweenProbes = probeVolumeOptions.scaleBiasWithMinProbeDistance.value;
131 parameters.samplingNoise = probeVolumeOptions.samplingNoise.value;
132 parameters.weight = probeVolumeOptions.intensityMultiplier.value;
133 parameters.leakReductionMode = probeVolumeOptions.leakReductionMode.value;
134 parameters.frameIndexForNoise = taaFrameIndex * (probeVolumeOptions.animateSamplingNoise.value ? 1 : 0);
135 parameters.reflNormalizationLowerClamp = 0.005f;
136 parameters.reflNormalizationUpperClamp = probeVolumeOptions.occlusionOnlyReflectionNormalization.value ? 1.0f : 7.0f;
137
138 parameters.skyOcclusionIntensity = skyOcclusion ? probeVolumeOptions.skyOcclusionIntensityMultiplier.value : 0.0f;
139 parameters.skyOcclusionShadingDirection = skyOcclusion && skyOcclusionShadingDirection;
140 parameters.regionCount = m_CurrentBakingSet.bakedMaskCount;
141 parameters.regionLayerMasks = supportRenderingLayers ? m_CurrentBakingSet.bakedLayerMasks : 0xFFFFFFFF;
142 parameters.worldOffset = probeVolumeOptions.worldOffset.value;
143 UpdateConstantBuffer(cmd, parameters);
144 }
145
146 return enableProbeVolumes;
147 }
148 }
149}