private TemporalFrameCache GetFrameCache(int frameIndex, RenderView renderView) { TemporalFrameCache cache; // Find free temporal cache for (int i = 0; i < frameCache.Count; i++) { cache = frameCache[i]; if (cache.RenderView == renderView && cache.LastUsageFrame != frameIndex) { cache.LastUsageFrame = frameIndex; return(cache); } } // Create new one cache = new TemporalFrameCache(); cache.RenderView = renderView; cache.LastUsageFrame = frameIndex; cache.PrevViewProjection = renderView.ViewProjection; frameCache.Add(cache); return(cache); }
private TemporalFrameCache GetFrameCache(int frameIndex, CameraComponent camera) { TemporalFrameCache cache; // Find free temporal cache for (int i = 0; i < frameCache.Count; i++) { cache = frameCache[i]; if (cache.Camera == camera && cache.LastUsageFrame != frameIndex) { cache.LastUsageFrame = frameIndex; return(cache); } } // Create new one cache = new TemporalFrameCache(); cache.Camera = camera; cache.LastUsageFrame = frameIndex; cache.PrevViewProjection = camera.ViewProjectionMatrix; frameCache.Add(cache); return(cache); }
private TemporalFrameCache Prepare(RenderDrawContext context, Texture outputBuffer) { TemporalFrameCache cache = null; var renderView = context.RenderContext.RenderView; Matrix viewMatrix = renderView.View; Matrix projectionMatrix = renderView.Projection; Matrix viewProjectionMatrix = renderView.ViewProjection; Matrix inverseViewMatrix = Matrix.Invert(viewMatrix); Matrix inverseViewProjectionMatrix = Matrix.Invert(viewProjectionMatrix); Vector4 eye = inverseViewMatrix.Row4; float nearclip = renderView.NearClipPlane; float farclip = renderView.FarClipPlane; Vector4 viewInfo = new Vector4(1.0f / projectionMatrix.M11, 1.0f / projectionMatrix.M22, farclip / (farclip - nearclip), (-farclip * nearclip) / (farclip - nearclip) / farclip); Vector3 cameraPos = new Vector3(eye.X, eye.Y, eye.Z); float temporalTime = 0; if (TemporalEffect) { var gameTime = context.RenderContext.Time; double time = gameTime.Total.TotalSeconds; // Keep time in smaller range to prevent temporal noise errors const double scale = 10; double integral = Math.Round(time / scale) * scale; time -= integral; temporalTime = (float)time; cache = GetFrameCache(gameTime.FrameCount, renderView); } var traceBufferSize = GetBufferResolution(outputBuffer, RayTracePassResolution); var roughnessFade = 1 - MathUtil.Clamp(GlossinessThreshold, 0.0f, 1.0f); var maxTraceSamples = MathUtil.Clamp(MaxSteps, 1, 128); // ViewInfo : x-1/Projection[0,0] y-1/Projection[1,1] z-(Far / (Far - Near) w-(-Far * Near) / (Far - Near) / Far) rayTracePassShader.Parameters.Set(SSLRCommonKeys.ViewFarPlane, farclip); rayTracePassShader.Parameters.Set(SSLRCommonKeys.RoughnessFade, roughnessFade); rayTracePassShader.Parameters.Set(SSLRCommonKeys.MaxTraceSamples, maxTraceSamples); rayTracePassShader.Parameters.Set(SSLRCommonKeys.WorldAntiSelfOcclusionBias, WorldAntiSelfOcclusionBias); rayTracePassShader.Parameters.Set(SSLRCommonKeys.BRDFBias, BRDFBias); rayTracePassShader.Parameters.Set(SSLRCommonKeys.TemporalTime, temporalTime); rayTracePassShader.Parameters.Set(SSLRCommonKeys.CameraPosWS, ref cameraPos); rayTracePassShader.Parameters.Set(SSLRCommonKeys.ViewInfo, ref viewInfo); rayTracePassShader.Parameters.Set(SSLRCommonKeys.V, ref viewMatrix); rayTracePassShader.Parameters.Set(SSLRCommonKeys.IVP, ref inverseViewProjectionMatrix); rayTracePassShader.Parameters.Set(SSLRRayTracePassKeys.VP, ref viewProjectionMatrix); rayTracePassShader.Parameters.Set(SSLRRayTracePassKeys.EdgeFadeFactor, EdgeFadeFactor); resolvePassShader.Parameters.Set(SSLRCommonKeys.MaxColorMiplevel, Texture.CalculateMipMapCount(0, outputBuffer.Width, outputBuffer.Height) - 1); resolvePassShader.Parameters.Set(SSLRCommonKeys.TraceSizeMax, Math.Max(traceBufferSize.Width, traceBufferSize.Height) / 2.0f); resolvePassShader.Parameters.Set(SSLRCommonKeys.ViewFarPlane, farclip); resolvePassShader.Parameters.Set(SSLRCommonKeys.RoughnessFade, roughnessFade); resolvePassShader.Parameters.Set(SSLRCommonKeys.TemporalTime, temporalTime); resolvePassShader.Parameters.Set(SSLRCommonKeys.BRDFBias, BRDFBias); resolvePassShader.Parameters.Set(SSLRCommonKeys.CameraPosWS, ref cameraPos); resolvePassShader.Parameters.Set(SSLRCommonKeys.ViewInfo, ref viewInfo); resolvePassShader.Parameters.Set(SSLRCommonKeys.V, ref viewMatrix); resolvePassShader.Parameters.Set(SSLRCommonKeys.IVP, ref inverseViewProjectionMatrix); resolvePassShader.Parameters.Set(SSLRKeys.ResolveSamples, MathUtil.Clamp(ResolveSamples, 1, 8)); resolvePassShader.Parameters.Set(SSLRKeys.ReduceHighlights, ReduceHighlights); if (TemporalEffect) { temporalPassShader.Parameters.Set(SSLRTemporalPassKeys.IVP, ref inverseViewProjectionMatrix); temporalPassShader.Parameters.Set(SSLRTemporalPassKeys.TemporalResponse, TemporalResponse); temporalPassShader.Parameters.Set(SSLRTemporalPassKeys.TemporalScale, TemporalScale); if (cache != null) { temporalPassShader.Parameters.Set(SSLRTemporalPassKeys.prevVP, ref cache.PrevViewProjection); cache.PrevViewProjection = viewProjectionMatrix; } } combinePassShader.Parameters.Set(SSLRCommonKeys.ViewFarPlane, farclip); combinePassShader.Parameters.Set(SSLRCommonKeys.CameraPosWS, ref cameraPos); combinePassShader.Parameters.Set(SSLRCommonKeys.ViewInfo, ref viewInfo); combinePassShader.Parameters.Set(SSLRCommonKeys.V, ref viewMatrix); combinePassShader.Parameters.Set(SSLRCommonKeys.IVP, ref inverseViewProjectionMatrix); return(cache); }