void PushVolumetricLightingGlobalParams(HDCamera hdCamera, CommandBuffer cmd, int frameIndex) { if (!Fog.IsVolumetricLightingEnabled(hdCamera)) { cmd.SetGlobalTexture(HDShaderIDs._VBufferLighting, HDUtils.clearTexture3D); return; } // Get the interpolated anisotropy value. var fog = VolumeManager.instance.stack.GetComponent <Fog>(); SetPreconvolvedAmbientLightProbe(cmd, fog.globalLightProbeDimmer.value, fog.anisotropy.value); var currFrameParams = hdCamera.vBufferParams[0]; var prevFrameParams = hdCamera.vBufferParams[1]; // The lighting & density buffers are shared by all cameras. // The history & feedback buffers are specific to the camera. // These 2 types of buffers can have different sizes. // Additionally, history buffers can have different sizes, since they are not resized at the same time // (every frame, we swap the buffers, and resize the feedback buffer but not the history buffer). // The viewport size is the same for all of these buffers. // All of these buffers may have sub-native-resolution viewports. // The 3rd dimension (number of slices) is the same for all of these buffers. Vector2Int sharedBufferSize = new Vector2Int(m_LightingBufferHandle.rt.width, m_LightingBufferHandle.rt.height); Debug.Assert(m_LightingBufferHandle.rt.width == m_DensityBufferHandle.rt.width); Debug.Assert(m_LightingBufferHandle.rt.height == m_DensityBufferHandle.rt.height); Vector2Int historyBufferSize = Vector2Int.zero; if (hdCamera.IsVolumetricReprojectionEnabled()) { var historyRT = hdCamera.GetPreviousFrameRT((int)HDCameraFrameHistoryType.VolumetricLighting); historyBufferSize = new Vector2Int(historyRT.rt.width, historyRT.rt.height); } var cvp = currFrameParams.viewportSize; var pvp = prevFrameParams.viewportSize; // Adjust slices for XR rendering: VBuffer is shared for all single-pass views int sliceCount = cvp.z / hdCamera.viewCount; cmd.SetGlobalVector(HDShaderIDs._VBufferViewportSize, new Vector4(cvp.x, cvp.y, 1.0f / cvp.x, 1.0f / cvp.y)); cmd.SetGlobalInt(HDShaderIDs._VBufferSliceCount, sliceCount); cmd.SetGlobalFloat(HDShaderIDs._VBufferRcpSliceCount, 1.0f / sliceCount); cmd.SetGlobalVector(HDShaderIDs._VBufferSharedUvScaleAndLimit, currFrameParams.ComputeUvScaleAndLimit(sharedBufferSize)); cmd.SetGlobalVector(HDShaderIDs._VBufferDistanceEncodingParams, currFrameParams.depthEncodingParams); cmd.SetGlobalVector(HDShaderIDs._VBufferDistanceDecodingParams, currFrameParams.depthDecodingParams); cmd.SetGlobalFloat(HDShaderIDs._VBufferLastSliceDist, currFrameParams.ComputeLastSliceDistance(sliceCount)); cmd.SetGlobalFloat(HDShaderIDs._VBufferRcpInstancedViewCount, 1.0f / hdCamera.viewCount); cmd.SetGlobalVector(HDShaderIDs._VBufferPrevViewportSize, new Vector4(pvp.x, pvp.y, 1.0f / pvp.x, 1.0f / pvp.y)); cmd.SetGlobalVector(HDShaderIDs._VBufferHistoryPrevUvScaleAndLimit, prevFrameParams.ComputeUvScaleAndLimit(historyBufferSize)); cmd.SetGlobalVector(HDShaderIDs._VBufferPrevDepthEncodingParams, prevFrameParams.depthEncodingParams); cmd.SetGlobalVector(HDShaderIDs._VBufferPrevDepthDecodingParams, prevFrameParams.depthDecodingParams); cmd.SetGlobalTexture(HDShaderIDs._VBufferLighting, m_LightingBufferHandle); }
// Must be called AFTER UpdateVolumetricBufferParams. static internal void ResizeVolumetricHistoryBuffers(HDCamera hdCamera, int frameIndex) { if (!hdCamera.IsVolumetricReprojectionEnabled()) { return; } Debug.Assert(hdCamera.vBufferParams != null); Debug.Assert(hdCamera.vBufferParams.Length == 2); Debug.Assert(hdCamera.volumetricHistoryBuffers != null); var currIdx = (frameIndex + 0) & 1; var prevIdx = (frameIndex + 1) & 1; var currentParams = hdCamera.vBufferParams[currIdx]; // Render texture contents can become "lost" on certain events, like loading a new level, // system going to a screensaver mode, in and out of fullscreen and so on. // https://docs.unity3d.com/ScriptReference/RenderTexture.html if (hdCamera.volumetricHistoryBuffers[0] == null || hdCamera.volumetricHistoryBuffers[1] == null) { DestroyVolumetricHistoryBuffers(hdCamera); CreateVolumetricHistoryBuffers(hdCamera, hdCamera.vBufferParams.Length); // Basically, assume it's 2 } string[] names = new string[2] { "VBufferHistory0", "VBufferHistory1" }; // We only resize the feedback buffer (#0), not the history buffer (#1). // We must NOT resize the buffer from the previous frame (#1), as that would invalidate its contents. ResizeVolumetricBuffer(ref hdCamera.volumetricHistoryBuffers[currIdx], names[currIdx], currentParams.viewportSize.x, currentParams.viewportSize.y, currentParams.viewportSize.z); }