Example #1
0
        public void ExecutePass(DeviceContext context, RenderTargetSet target, RenderTargetSet linearDepth, RenderTargetSet motionVectors)
        {
            RenderTargetSet ssaoCurrent = TemporalSurfaceManager.GetRenderTargetCurrent("SSAO");
            RenderTargetSet ssaoHistory = TemporalSurfaceManager.GetRenderTargetHistory("SSAO");

            RenderTargetSet tempBlurBuffer = RenderTargetManager.RequestRenderTargetFromPool(m_RTDescriptor);

            Random rand = new Random();

            dynamic scb = m_SSAOBuffer;

            scb.g_SSAOPhase = (float)rand.NextDouble() * 3.1415f;
            m_SSAOBuffer.CompileAndBind(context);

            using (new GpuProfilePoint(context, "SSAO"))
            {
                using (new GpuProfilePoint(context, "SSAOCalculate"))
                {
                    linearDepth.BindSRV(context, 0);
                    ssaoHistory.BindSRV(context, 1);
                    motionVectors.BindSRV(context, 2);
                    ssaoCurrent.BindAsRenderTarget(context);
                    PostEffectHelper.RenderFullscreenTriangle(context, "SSAOCalculate");
                    RenderTargetSet.BindNull(context);
                    ContextHelper.ClearSRVs(context);
                }

                SurfaceDebugManager.RegisterDebug(context, "SSAOMain", ssaoCurrent);

                using (new GpuProfilePoint(context, "SSAOBlur"))
                {
                    ssaoCurrent.BindSRV(context, 1);
                    tempBlurBuffer.BindAsRenderTarget(context);
                    PostEffectHelper.RenderFullscreenTriangle(context, "SSAOBlurHorizontal");
                    RenderTargetSet.BindNull(context);
                    ContextHelper.ClearSRVs(context);

                    SurfaceDebugManager.RegisterDebug(context, "SSAOBlurH", tempBlurBuffer);

                    tempBlurBuffer.BindSRV(context, 1);
                    target.BindAsRenderTarget(context);
                    PostEffectHelper.RenderFullscreenTriangle(context, "SSAOBlurVertical");
                    RenderTargetSet.BindNull(context);
                    ContextHelper.ClearSRVs(context);

                    SurfaceDebugManager.RegisterDebug(context, "SSAOBlurV", target);
                }
            }

            RenderTargetManager.ReleaseRenderTargetToPool(tempBlurBuffer);
        }
        public static void Initialize(Device device, int resolutionX, int resolutionY)
        {
            RenderTargetSet.RenderTargetDescriptor rtDesc = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
                m_HasDepth    = false,
                m_Width       = resolutionX,
                m_Height      = resolutionY,
                m_NumSurfaces = 1,
            };

            m_DebugRenderTarget = RenderTargetManager.RequestRenderTargetFromPool(rtDesc);
            m_DebugAppendBuffer = GPUBufferObject.CreateBuffer(device, 1024, 8 * sizeof(uint), null, true, true);
        }
        public DepthOperationsPass(int resolutionX, int resolutionY)
        {
            RenderTargetSet.RenderTargetDescriptor linearDepthDescriptor = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = Format.R16_Float,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY,
                m_Width       = resolutionX,
            };

            m_LinearDepth = RenderTargetManager.RequestRenderTargetFromPool(linearDepthDescriptor);

            RenderTargetSet.RenderTargetDescriptor halfLinearDepthDescriptor = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = Format.R16_Float,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY / 2,
                m_Width       = resolutionX / 2,
            };

            m_HalfLinearDepth = RenderTargetManager.RequestRenderTargetFromPool(halfLinearDepthDescriptor);

            RenderTargetSet.RenderTargetDescriptor normalsDescriptor = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = Format.R8G8B8A8_UNorm,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY / 2,
                m_Width       = resolutionX / 2,
            };

            m_HalfNormals = RenderTargetManager.RequestRenderTargetFromPool(normalsDescriptor);

            m_HalfLinearDepth.m_RenderTargets[1] = m_HalfNormals.m_RenderTargets[0];
            m_HalfLinearDepth.m_NumRTs++;

            RenderTargetSet.RenderTargetDescriptor bilateralUpsampleDescriptor = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = Format.R8G8_SNorm,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY,
                m_Width       = resolutionX,
            };

            m_BilateralUpsampleOffsets = RenderTargetManager.RequestRenderTargetFromPool(bilateralUpsampleDescriptor);
        }
Example #4
0
        public void ExecutePass(DeviceContext context, RenderTargetSet linearDepth, RenderTargetSet motionVectors, RenderTargetSet surfaceNormals, RenderTargetSet sourceColor, Camera sceneCam, DepthOperationsPass depthOps)
        {
            RenderTargetSet ssrCurrent = TemporalSurfaceManager.GetRenderTargetCurrent("SSReflections");
            RenderTargetSet ssrHistory = TemporalSurfaceManager.GetRenderTargetHistory("SSReflections");
            RenderTargetSet traceRT    = RenderTargetManager.RequestRenderTargetFromPool(m_ResultsRTDescriptor);

            if (DebugManager.IsFeatureOn("SSReflections"))
            {
                using (new GpuProfilePoint(context, "SSReflections"))
                {
                    using (new GpuProfilePoint(context, "SSReflectionsRaytrace"))
                    {
                        context.PixelShader.SetShaderResource(PostEffectHelper.m_RandomNumbersBuffer.m_ShaderResourceView, 39);
                        linearDepth.BindSRV(context, 0);
                        surfaceNormals.BindSRV(context, 1);
                        sourceColor.BindSRV(context, 2);
                        motionVectors.BindSRV(context, 3);
                        traceRT.BindAsRenderTarget(context);
                        PostEffectHelper.RenderFullscreenTriangle(context, "SSReflectionsRaytrace");
                        RenderTargetSet.BindNull(context);
                        ContextHelper.ClearSRVs(context);
                    }

                    using (new GpuProfilePoint(context, "SSReflectionsBlur"))
                    {
                        linearDepth.BindSRV(context, 0);
                        surfaceNormals.BindSRV(context, 1);
                        traceRT.BindSRV(context, 2);
                        motionVectors.BindSRV(context, 3);
                        ssrHistory.BindSRV(context, 4);
                        ssrCurrent.BindAsRenderTarget(context);
                        PostEffectHelper.RenderFullscreenTriangle(context, "SSReflectionsBlur");
                        RenderTargetSet.BindNull(context);
                        ContextHelper.ClearSRVs(context);
                    }
                }
            }
            else
            {
                traceRT.Clear(context, new Color4(0.0f, 0.0f, 0.0f, 0.0f));
                ssrCurrent.Clear(context, new Color4(0.0f, 0.0f, 0.0f, 0.0f));
            }

            DebugManager.RegisterDebug(context, "SSRRaytrace", traceRT);
            DebugManager.RegisterDebug(context, "SSRBlur", ssrCurrent);

            RenderTargetManager.ReleaseRenderTargetToPool(traceRT);
        }
Example #5
0
        public void ExecutePass(DeviceContext context, RenderTargetSet target, RenderTargetSet source, RenderTargetSet depthSource)
        {
            using (new GpuProfilePoint(context, "Bokeh DoF"))
            {
                RenderTargetSet halfResColorCoC = RenderTargetManager.RequestRenderTargetFromPool(m_HalfResDescriptor);
                RenderTargetSet bokehAccumulate = RenderTargetManager.RequestRenderTargetFromPool(m_HalfHeightDescriptor);

                using (new GpuProfilePoint(context, "Downsample"))
                {
                    source.BindSRV(context, 0);
                    depthSource.BindDepthAsSRV(context, 1);
                    halfResColorCoC.BindAsRenderTarget(context);
                    PostEffectHelper.RenderFullscreenTriangle(context, "DownsampleColorCoC");
                    ContextHelper.ClearSRVs(context);
                }

                using (new GpuProfilePoint(context, "Sprites"))
                {
                    bokehAccumulate.Clear(context, new Color4(0.0f, 0.0f, 0.0f, 0.0f));
                    bokehAccumulate.BindAsRenderTarget(context, false);
                    halfResColorCoC.BindSRV(context, 0);
                    context.PixelShader.SetShaderResource(m_BokehSpriteTextureSRV, 4);

                    context.VertexShader.Set(ShaderManager.GetVertexShader("VertexFullScreenDofGrid"));
                    context.PixelShader.Set(ShaderManager.GetPixelShader("BokehSprite"));

                    ContextHelper.SetBlendState(context, ContextHelper.BlendType.Additive);
                    PostEffectHelper.RenderFullscreenGrid(context, m_NumQuads);
                    ContextHelper.SetBlendState(context, ContextHelper.BlendType.None);
                    ContextHelper.ClearSRVs(context);
                }

                using (new GpuProfilePoint(context, "ResolveBokeh"))
                {
                    target.BindAsRenderTarget(context);
                    source.BindSRV(context, 0);
                    bokehAccumulate.BindSRV(context, 2);
                    halfResColorCoC.BindSRV(context, 3);
                    PostEffectHelper.RenderFullscreenTriangle(context, m_DebugBokeh ? "ResolveBokehDebug" : "ResolveBokeh");
                    ContextHelper.ClearSRVs(context);
                }

                RenderTargetManager.ReleaseRenderTargetToPool(halfResColorCoC);
                RenderTargetManager.ReleaseRenderTargetToPool(bokehAccumulate);

                RenderTargetSet.BindNull(context);
            }
        }
Example #6
0
        public static void InitializeRenderTarget(string name, RenderTargetSet.RenderTargetDescriptor descriptor, int countHistory = 1)
        {
            if (m_TemporalRenderTargets.ContainsKey(name))
            {
                throw new Exception("Temporal surface with given name already exists");
            }

            List <RenderTargetSet> sets = new List <RenderTargetSet>();

            while (countHistory-- >= 0)
            {
                sets.Add(RenderTargetManager.RequestRenderTargetFromPool(descriptor));
            }
            TemporalRenderTargetSetDescriptor set = new TemporalRenderTargetSetDescriptor(sets);

            m_TemporalRenderTargets[name] = set;
        }
Example #7
0
        public RenderTargetSet ExecutePass(DeviceContext context, RenderTargetSet source)
        {
            RenderTargetSet[] luminanceTargets = new RenderTargetSet[6];
            luminanceTargets[0] = RenderTargetManager.RequestRenderTargetFromPool(m_RTDescriptorLuminance4x4);
            luminanceTargets[1] = RenderTargetManager.RequestRenderTargetFromPool(m_RTDescriptorLuminance16x16);
            luminanceTargets[2] = RenderTargetManager.RequestRenderTargetFromPool(m_RTDescriptorLuminance32x32);
            luminanceTargets[3] = RenderTargetManager.RequestRenderTargetFromPool(m_RTDescriptorLuminance64x64);
            luminanceTargets[4] = RenderTargetManager.RequestRenderTargetFromPool(m_RTDescriptorLuminance128x128);
            luminanceTargets[5] = RenderTargetManager.RequestRenderTargetFromPool(m_RTDescriptorLuminance256x256);

            using (new GpuProfilePoint(context, "Calculate luminance"))
            {
                source.BindSRV(context, 0);
                luminanceTargets[0].BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "Downsample4x4CalculateLuminance");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);

                luminanceTargets[0].BindSRV(context, 0);
                luminanceTargets[1].BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "Downsample4x4Luminance");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);

                luminanceTargets[1].BindSRV(context, 0);
                luminanceTargets[2].BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "Downsample2x2Luminance");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);


                luminanceTargets[2].BindSRV(context, 0);
                luminanceTargets[3].BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "Downsample2x2Luminance");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);

                luminanceTargets[3].BindSRV(context, 0);
                luminanceTargets[4].BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "Downsample2x2Luminance");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);

                luminanceTargets[4].BindSRV(context, 0);
                luminanceTargets[5].BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "Downsample2x2Luminance");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);

                luminanceTargets[5].BindSRV(context, 0);
                context.ComputeShader.SetUnorderedAccessView(m_FinalLuminance.m_RenderTargets[0].m_UnorderedAccessView, 0);
                ShaderManager.ExecuteComputeForSize(context, 1, 1, 1, "FinalCalculateAverageLuminance");
                ContextHelper.ClearSRVs(context);
                ContextHelper.ClearCSContext(context);
            }

            foreach (var lt in luminanceTargets)
            {
                RenderTargetManager.ReleaseRenderTargetToPool(lt);
            }

            return(m_FinalLuminance);
        }
Example #8
0
        public LuminanceCalculations(SlimDX.Direct3D11.Device device, int resolutionX, int resolutionY)
        {
            Format luminanceFormat = Format.R16_Float;

            m_RTDescriptorLuminance4x4 = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = luminanceFormat,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY / 4,
                m_Width       = resolutionX / 4
            };

            m_RTDescriptorLuminance16x16 = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = luminanceFormat,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY / 16,
                m_Width       = resolutionX / 16
            };

            m_RTDescriptorLuminance32x32 = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = luminanceFormat,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY / 32,
                m_Width       = resolutionX / 32
            };

            m_RTDescriptorLuminance64x64 = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = luminanceFormat,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY / 64,
                m_Width       = resolutionX / 64
            };

            m_RTDescriptorLuminance128x128 = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = luminanceFormat,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY / 128,
                m_Width       = resolutionX / 128
            };

            m_RTDescriptorLuminance256x256 = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = luminanceFormat,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = resolutionY / 256,
                m_Width       = resolutionX / 256
            };

            m_RTDescriptorFinalLuminance = new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = Format.R32_Float, // to be able to read and write at the same time...
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = 1,
                m_Width       = 1
            };

            m_FinalLuminance = RenderTargetManager.RequestRenderTargetFromPool(m_RTDescriptorFinalLuminance);
        }
Example #9
0
        public void RenderFrame(DeviceContext context, double timeElapsed, RenderTargetSet targetRT)
        {
            ShaderManager.BindSamplerStates(context);
            PerlinNoiseRenderHelper.BindTextures(context);
            RenderTargetSet currentFrameMainBuffer = RenderTargetManager.RequestRenderTargetFromPool(m_FullResAndDepthDescriptor);
            RenderTargetSet linearDepth            = RenderTargetManager.RequestRenderTargetFromPool(m_LinearDepthDescriptor);
            RenderTargetSet ssaoRT = RenderTargetManager.RequestRenderTargetFromPool(m_SSAODescriptor);

            if (!m_ShadowsInitialized)
            {
                m_ShadowCamera.m_CameraForward = new Vector3(-0.15f, -1.0f, 0.15f);
                m_ShadowCamera.m_CameraForward.Normalize();

                Vector3 min, max;
                m_SimpleSceneWrapper.GetSceneBounds(out min, out max);

                Vector3 sceneTop = (min + max) * 0.5f;
                sceneTop.Y = max.Y;

                m_ShadowCamera.m_OrthoZoomX = (max.X - min.X) * 0.7f; // some overlap
                m_ShadowCamera.m_OrthoZoomY = (max.Z - min.Z) * 0.7f;

                m_ShadowCamera.m_CameraPosition = sceneTop - m_ShadowCamera.m_CameraForward * 50.0f;
                m_ShadowCamera.m_CameraUp       = new Vector3(0, 0, 1);
            }

            CalculateAndUpdateConstantBuffer(context, timeElapsed);

            if (!m_ShadowsInitialized)
            {
                m_ResolvedShadow     = m_ShadowEVSMGenerator.RenderShadows(context, m_SimpleSceneWrapper);
                m_ShadowsInitialized = true;
            }

            if (false)
            {
                m_GIRenderer.PartialGIUpdate(context, m_SimpleSceneWrapper, m_ResolvedShadow);
            }

            m_CurrentViewportBuffer.Bind(context);

            using (new GpuProfilePoint(context, "DepthPrepass"))
            {
                // set the shaders
                context.VertexShader.Set(ShaderManager.GetVertexShader("VertexScene"));
                context.PixelShader.Set(ShaderManager.GetPixelShader("DepthNormalPrepass"));

                currentFrameMainBuffer.Clear(context, new Color4(1.0f, 1.0f, 1.0f, 1.5f), true);
                currentFrameMainBuffer.BindAsRenderTarget(context, true, false);
                ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.DepthWriteCompare);

                // render triangles
                m_SimpleSceneWrapper.Render(context);

                ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.NoDepth);

                RenderTargetSet.BindNull(context);
            }

            RenderTargetSet motionVectorsSurface         = TemporalSurfaceManager.GetRenderTargetCurrent("MotionVectors");
            RenderTargetSet motionVectorsSurfacePrevious = TemporalSurfaceManager.GetRenderTargetHistory("MotionVectors");

            m_ResolveMotionVectorsPass.ExecutePass(context, motionVectorsSurface, currentFrameMainBuffer);

            PostEffectHelper.LinearizeDepth(context, linearDepth, currentFrameMainBuffer);
            SurfaceDebugManager.RegisterDebug(context, "LinearDepth", linearDepth);
            m_SSAOPass.ExecutePass(context, ssaoRT, linearDepth, motionVectorsSurface);

            m_VolumetricFog.RenderVolumetricFog(context, m_ResolvedShadow.m_RenderTargets[0], m_GIRenderer);

            using (new GpuProfilePoint(context, "MainForwardRender"))
            {
                // set the shaders
                context.VertexShader.Set(ShaderManager.GetVertexShader("VertexScene"));
                context.PixelShader.Set(ShaderManager.GetPixelShader("PixelScene"));

                currentFrameMainBuffer.BindAsRenderTarget(context, true);

                m_ResolvedShadow.BindSRV(context, 0);
                ssaoRT.BindSRV(context, 1);

                context.PixelShader.SetShaderResource(m_GIRenderer.m_GIVolumeR.m_ShaderResourceView, 5);
                context.PixelShader.SetShaderResource(m_GIRenderer.m_GIVolumeG.m_ShaderResourceView, 6);
                context.PixelShader.SetShaderResource(m_GIRenderer.m_GIVolumeB.m_ShaderResourceView, 7);

                context.PixelShader.SetShaderResource(m_VolumetricFog.m_ScatteringTexture.m_ShaderResourceView, 8);

                ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.DepthCompare);

                // render triangles
                m_SimpleSceneWrapper.Render(context);

                // render sky
                PostEffectHelper.RenderFullscreenTriangle(context, "Sky", true);

                ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.NoDepth);

                RenderTargetSet.BindNull(context);
            }

            using (new GpuProfilePoint(context, "PostEffects"))
            {
                RenderTargetSet postEffectSurfacePong = RenderTargetManager.RequestRenderTargetFromPool(m_FullResDescriptor);

                RenderTargetSet source, dest;
                source = currentFrameMainBuffer;
                dest   = postEffectSurfacePong;

                dynamic ppcb = m_PostEffectsConstantBuffer;
                if (ppcb.g_DofCoCScale > 0.0f)
                {
                    m_ScatterDOFPass.ExecutePass(context, dest, currentFrameMainBuffer, currentFrameMainBuffer);

                    PostEffectHelper.Swap(ref source, ref dest);
                }

                RenderTargetSet luminanceTexture = m_LuminanceCalculations.ExecutePass(context, source);

                RenderTargetSet resolvedCurrent = TemporalSurfaceManager.GetRenderTargetCurrent("ResolvedColor");
                RenderTargetSet resolvedHistory = TemporalSurfaceManager.GetRenderTargetHistory("ResolvedColor");

                m_ResolveHDRPass.ExecutePass(context, resolvedCurrent, source, luminanceTexture);

                RenderTargetSet resolvedTemporal = RenderTargetManager.RequestRenderTargetFromPool(m_ResolvedColorDescriptor);
                m_ResolveTemporalPass.ExecutePass(context, resolvedTemporal, resolvedCurrent, resolvedHistory, motionVectorsSurface, motionVectorsSurfacePrevious, true);
                SurfaceDebugManager.RegisterDebug(context, "ResolvedNoFXAA", resolvedTemporal);
                m_FxaaPass.ExecutePass(context, targetRT, resolvedTemporal);

                RenderTargetManager.ReleaseRenderTargetToPool(resolvedTemporal);
                RenderTargetManager.ReleaseRenderTargetToPool(postEffectSurfacePong);
            }

            RenderTargetManager.ReleaseRenderTargetToPool(ssaoRT);
            RenderTargetManager.ReleaseRenderTargetToPool(linearDepth);
            RenderTargetManager.ReleaseRenderTargetToPool(currentFrameMainBuffer);
        }
Example #10
0
        public void ExecutePass(DeviceContext context, RenderTargetSet target, RenderTargetSet linearDepth, RenderTargetSet motionVectors, RenderTargetSet surfaceNormals, DepthOperationsPass depthOps)
        {
            RenderTargetSet ssaoCurrent = TemporalSurfaceManager.GetRenderTargetCurrent("SSAO");
            RenderTargetSet ssaoHistory = TemporalSurfaceManager.GetRenderTargetHistory("SSAO");

            if (DebugManager.IsFeatureOn("SSAO"))
            {
                RenderTargetSet tempBlurBuffer = RenderTargetManager.RequestRenderTargetFromPool(m_RTDescriptor);

                Random rand = new Random();

                dynamic scb = m_SSAOBuffer;
                scb.g_SSAOPhase = (float)rand.NextDouble() * 3.1415f;
                m_SSAOBuffer.CompileAndBind(context);

                using (new GpuProfilePoint(context, "SSAO"))
                {
                    using (new GpuProfilePoint(context, "SSAOCalculate"))
                    {
                        linearDepth.BindSRV(context, 0);
                        ssaoHistory.BindSRV(context, 1);
                        motionVectors.BindSRV(context, 2);
                        surfaceNormals.BindSRV(context, 3);
                        ssaoCurrent.BindAsRenderTarget(context);
                        PostEffectHelper.RenderFullscreenTriangle(context, "SSAOCalculate");
                        RenderTargetSet.BindNull(context);
                        ContextHelper.ClearSRVs(context);
                    }

                    DebugManager.RegisterDebug(context, "SSAOMain", ssaoCurrent);

                    using (new GpuProfilePoint(context, "SSAOBlur"))
                    {
                        ssaoCurrent.BindSRV(context, 1);
                        tempBlurBuffer.BindAsRenderTarget(context);
                        PostEffectHelper.RenderFullscreenTriangle(context, "SSAOBlurHorizontal");
                        RenderTargetSet.BindNull(context);
                        ContextHelper.ClearSRVs(context);

                        DebugManager.RegisterDebug(context, "SSAOBlurH", tempBlurBuffer);

                        tempBlurBuffer.BindSRV(context, 1);
                        target.BindAsRenderTarget(context);
                        PostEffectHelper.RenderFullscreenTriangle(context, "SSAOBlurVertical");
                        RenderTargetSet.BindNull(context);
                        ContextHelper.ClearSRVs(context);

                        DebugManager.RegisterDebug(context, "SSAOBlurV", target);
                    }
                }

                RenderTargetManager.ReleaseRenderTargetToPool(tempBlurBuffer);
            }
            else
            {
                target.Clear(context, new SlimDX.Color4(1.0f, 1.0f, 1.0f, 1.0f));
            }

            if (DebugManager.IsDebugging("UpsampledSSAO"))
            {
                RenderTargetSet tempFullResBuffer = RenderTargetManager.RequestRenderTargetFromPool(m_UpsampleDebugRTDescriptor);

                target.BindSRV(context, 0);
                depthOps.m_BilateralUpsampleOffsets.BindSRV(context, 10);
                tempFullResBuffer.BindAsRenderTarget(context);

                PostEffectHelper.RenderFullscreenTriangle(context, "BilateralUpsample");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);

                DebugManager.RegisterDebug(context, "UpsampledSSAO", tempFullResBuffer);
                RenderTargetManager.ReleaseRenderTargetToPool(tempFullResBuffer);
            }
        }
Example #11
0
        static void Main()
        {
            m_FrameNumber = 0;
            m_Time        = 0.0f;

            Device    device;
            SwapChain swapChain;

            //var form = new RenderForm("CSharpRenderer");
            var form  = new CSharpRendererMainForm();
            var panel = form.GetRenderingPanel();

            form.ClientSize = new System.Drawing.Size(ResolutionX, ResolutionY);

            var description = new SwapChainDescription()
            {
                BufferCount       = 2,
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = panel.Handle,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(ResolutionX, ResolutionY, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags             = SwapChainFlags.AllowModeSwitch,
                SwapEffect        = SwapEffect.Discard
            };

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);
            var swapChainResource = Resource.FromSwapChain <Texture2D>(swapChain, 0);

            SamplerStates.Initialize(device);
            ShaderManager.Initialize(device);
            GPUProfiler.Initialize(device);
            ContextHelper.Initialize(device);
            RenderTargetManager.Initialize(device);
            PostEffectHelper.Initialize(device, ResolutionX, ResolutionY);
            CubemapRenderHelper.Initialize(device);
            PerlinNoiseRenderHelper.Initialize(device, device.ImmediateContext);
            LightingLUTHelper.Initialize(device, device.ImmediateContext);
            DebugManager.Initialize(device, ResolutionX, ResolutionY);

            InitializeControls(form);

            Scene scene = new Scene();

            scene.Initialize(device, form, panel, ResolutionX, ResolutionY);

            var resolvedRenderTarget = RenderTargetSet.CreateRenderTargetSet(device, ResolutionX, ResolutionY, Format.R8G8B8A8_UNorm, 1, false);

            RenderTargetSet.BindNull(device.ImmediateContext);

            // setting a viewport is required if you want to actually see anything
            var context = device.ImmediateContext;

            // prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
            using (var factory = swapChain.GetParent <Factory>())
                factory.SetWindowAssociation(panel.Handle, WindowAssociationFlags.IgnoreAltEnter);

            int counter = 0;

            Dictionary <string, double> profilers;

            profilers = new Dictionary <String, double>();

            DateTime now = DateTime.Now;

            CustomConstantBufferInstance globalFrameConstantBuffer = ShaderManager.CreateConstantBufferInstance("GlobalFrameBuffer", device);

            MessagePump.Run(form, () =>
            {
                TemporalSurfaceManager.UpdateTemporalSurfaces();
                if (ShaderManager.UpdateShaderManager(device))
                {
                    InitializeControls(form);
                }

                GPUProfiler.BeginFrameProfiling(context);

                double timeDelta = (DateTime.Now - now).TotalMilliseconds;

                if (!form.GetFreezeTime())
                {
                    m_Time += timeDelta / 1000.0;
                    m_FrameNumber++;
                }

                UpdateGlobalConstantBuffer(context, globalFrameConstantBuffer, form);

                scene.RenderFrame(context, timeDelta, resolvedRenderTarget);
                now = DateTime.Now;
                DebugManager.PresentDebug(context, resolvedRenderTarget);
                context.CopyResource(resolvedRenderTarget.m_RenderTargets[0].m_TextureObject2D, swapChainResource);
                GPUProfiler.EndFrameProfiling(context);
                swapChain.Present(0, PresentFlags.None);
                context.PixelShader.SetShaderResource(null, 0);

                if (GPUProfiler.m_CurrentFrameProfilerTree != null)
                {
                    Action <GPUProfiler.ProfilerTreeMember, String> processLevel = null;

                    processLevel = (GPUProfiler.ProfilerTreeMember treeMember, String level) =>
                    {
                        string finalName = level + treeMember.m_Name;

                        if (profilers.ContainsKey(finalName))
                        {
                            profilers[finalName] += treeMember.m_Time;
                        }
                        else
                        {
                            profilers.Add(finalName, treeMember.m_Time);
                        }

                        foreach (var v in treeMember.m_ChildMembers)
                        {
                            processLevel(v, level + "_");
                        }
                    };

                    processLevel(GPUProfiler.m_CurrentFrameProfilerTree, "");
                }

                CheckAndUpdateDebugUI(context, form);

                if (++counter == 10)
                {
                    UpdateControls();
                    DataGridView dataGridView = form.GetDataGridView();
                    dataGridView.Rows.Clear();

                    foreach (var profilerEntry in profilers)
                    {
                        DataGridViewRow row = new DataGridViewRow();
                        row.CreateCells(dataGridView);
                        row.Cells[0].Value = profilerEntry.Key;
                        row.Cells[1].Value = String.Format("{0}", Math.Round(profilerEntry.Value * 100.0 / 10.0) / 100.0);
                        dataGridView.Rows.Add(row);
                    }

                    profilers.Clear();

                    counter = 0;
                }
                else
                {
                    // avoid 2ms frame times...
                    System.Threading.Thread.Sleep(10);
                }
            });
        }
Example #12
0
        public void RenderFrame(DeviceContext context, double timeElapsed, RenderTargetSet targetRT)
        {
            ShaderManager.BindSamplerStates(context);
            PerlinNoiseRenderHelper.BindTextures(context);
            RenderTargetSet currentFrameMainBuffer = TemporalSurfaceManager.GetRenderTargetCurrent("SceneMainRender");
            RenderTargetSet ssaoRT      = RenderTargetManager.RequestRenderTargetFromPool(m_SSAODescriptor);
            bool            useTemporal = !DebugManager.m_DisabledTemporal;

            if (!m_ShadowsInitialized)
            {
                InitializeShadowmapAndCubemaps(context, timeElapsed);
            }

            CalculateAndUpdateConstantBuffer(context, timeElapsed);

            if (false)
            {
                m_GIRenderer.PartialGIUpdate(context, m_SimpleSceneWrapper, m_ResolvedShadow);
            }

            m_CurrentViewportBuffer.Bind(context);

            using (new GpuProfilePoint(context, "DepthPrepass"))
            {
                // set the shaders
                context.VertexShader.Set(ShaderManager.GetVertexShader("VertexScene"));
                context.PixelShader.Set(ShaderManager.GetPixelShader("DepthNormalPrepass"));

                currentFrameMainBuffer.Clear(context, new Color4(1.0f, 1.0f, 1.0f, 1.5f), true);
                currentFrameMainBuffer.BindAsRenderTarget(context, true, true);
                ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.DepthWriteCompare);

                // render triangles
                m_SimpleSceneWrapper.Render(context);

                ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.NoDepth);

                RenderTargetSet.BindNull(context);

                DebugManager.RegisterDebug(context, "NormalsPrepass", currentFrameMainBuffer);
            }

            RenderTargetSet motionVectorsSurface         = TemporalSurfaceManager.GetRenderTargetCurrent("MotionVectors");
            RenderTargetSet motionVectorsSurfacePrevious = TemporalSurfaceManager.GetRenderTargetHistory("MotionVectors");

            m_ResolveMotionVectorsPass.ExecutePass(context, motionVectorsSurface, currentFrameMainBuffer);
            DebugManager.RegisterDebug(context, "MotionVectors", motionVectorsSurface);

            m_DepthOperationsPass.ExecutePass(context, currentFrameMainBuffer);
            RenderTargetSet linearDepth = m_DepthOperationsPass.m_LinearDepth;

            m_SSAOPass.ExecutePass(context, ssaoRT, m_DepthOperationsPass.m_HalfLinearDepth, motionVectorsSurface, m_DepthOperationsPass.m_HalfNormals, m_DepthOperationsPass);
            m_SSReflectionsPass.ExecutePass(context, m_DepthOperationsPass.m_HalfLinearDepth, motionVectorsSurface, m_DepthOperationsPass.m_HalfNormals, TemporalSurfaceManager.GetRenderTargetHistory("SceneMainRender"), m_ViewportCamera, m_DepthOperationsPass);
            m_VolumetricFog.RenderVolumetricFog(context, m_ResolvedShadow.m_RenderTargets[0], m_GIRenderer);

            using (new GpuProfilePoint(context, "MainForwardRender"))
            {
                // set the shaders
                context.VertexShader.Set(ShaderManager.GetVertexShader("VertexScene"));
                context.PixelShader.Set(ShaderManager.GetPixelShader("PixelScene"));

                currentFrameMainBuffer.BindAsRenderTarget(context, true);

                m_ResolvedShadow.BindSRV(context, 0);
                ssaoRT.BindSRV(context, 1);
                TemporalSurfaceManager.GetRenderTargetCurrent("SSReflections").BindSRV(context, 2);
                m_DepthOperationsPass.m_BilateralUpsampleOffsets.BindSRV(context, 10);

                context.PixelShader.SetShaderResource(m_GIRenderer.m_GIVolumeR.m_ShaderResourceView, 5);
                context.PixelShader.SetShaderResource(m_GIRenderer.m_GIVolumeG.m_ShaderResourceView, 6);
                context.PixelShader.SetShaderResource(m_GIRenderer.m_GIVolumeB.m_ShaderResourceView, 7);

                context.PixelShader.SetShaderResource(m_VolumetricFog.m_ScatteringTexture.m_ShaderResourceView, 8);

                context.PixelShader.SetShaderResource(m_CubeObjectFiltered.m_ShaderResourceView, 11);

                ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.DepthCompare);

                LightingLUTHelper.BindTextures(context);

                // render triangles
                m_SimpleSceneWrapper.Render(context);

                // render sky
                PostEffectHelper.RenderFullscreenTriangle(context, "Sky", true);

                ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.NoDepth);

                RenderTargetSet.BindNull(context);
            }
            RenderTargetManager.ReleaseRenderTargetToPool(ssaoRT);

            using (new GpuProfilePoint(context, "PostEffects"))
            {
                RenderTargetSet postEffectSurfacePong = RenderTargetManager.RequestRenderTargetFromPool(m_FullResDescriptor);

                RenderTargetSet source, dest;
                source = currentFrameMainBuffer;
                dest   = postEffectSurfacePong;

                dynamic ppcb = m_PostEffectsConstantBuffer;
                if (ppcb.g_DofCoCScale > 0.0f)
                {
                    m_ScatterDOFPass.ExecutePass(context, dest, currentFrameMainBuffer, currentFrameMainBuffer);

                    PostEffectHelper.Swap(ref source, ref dest);
                }

                RenderTargetSet luminanceTexture = m_LuminanceCalculations.ExecutePass(context, source);

                RenderTargetSet resolvedCurrent = TemporalSurfaceManager.GetRenderTargetCurrent("ResolvedColor");
                RenderTargetSet resolvedHistory = TemporalSurfaceManager.GetRenderTargetHistory("ResolvedColor");

                m_ResolveHDRPass.ExecutePass(context, resolvedCurrent, source, luminanceTexture);
                DebugManager.RegisterDebug(context, "ResolvedNoTemporal", resolvedCurrent);

                RenderTargetSet resolvedTemporal    = RenderTargetManager.RequestRenderTargetFromPool(m_ResolvedColorDescriptor);
                RenderTargetSet finalResolvedTarget = resolvedCurrent;
                if (useTemporal)
                {
                    m_ResolveTemporalPass.ExecutePass(context, resolvedTemporal, resolvedCurrent, resolvedHistory, motionVectorsSurface, motionVectorsSurfacePrevious, true);
                    finalResolvedTarget = resolvedTemporal;
                }

                DebugManager.RegisterDebug(context, "ResolvedNoFXAA", finalResolvedTarget);
                m_FxaaPass.ExecutePass(context, targetRT, finalResolvedTarget);

                RenderTargetManager.ReleaseRenderTargetToPool(resolvedTemporal);
                RenderTargetManager.ReleaseRenderTargetToPool(postEffectSurfacePong);
            }
        }
Example #13
0
        static void Main()
        {
            Device    device;
            SwapChain swapChain;

            //var form = new RenderForm("CSharpRenderer");
            var form  = new CSharpRendererMainForm();
            var panel = form.GetRenderingPanel();

            form.ClientSize = new System.Drawing.Size(ResolutionX, ResolutionY);

            var description = new SwapChainDescription()
            {
                BufferCount       = 2,
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = panel.Handle,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(ResolutionX, ResolutionY, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags             = SwapChainFlags.AllowModeSwitch,
                SwapEffect        = SwapEffect.Discard
            };

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);
            var swapChainResource = Resource.FromSwapChain <Texture2D>(swapChain, 0);

            SamplerStates.Initialize(device);
            ShaderManager.Initialize(device);
            GPUProfiler.Initialize(device);
            ContextHelper.Initialize(device);
            RenderTargetManager.Initialize(device);
            PostEffectHelper.Initialize(device, ResolutionX, ResolutionY);
            CubemapRenderHelper.Initialize(device);

            Dictionary <CustomConstantBufferDefinition.ConstantBufferPropertyField, Tuple <TrackBar, TextBox> > propertyControlBindings =
                new Dictionary <CustomConstantBufferDefinition.ConstantBufferPropertyField, Tuple <TrackBar, TextBox> >();

            {
                TableLayoutPanel tableLayout = form.GetTableLayoutPanel();

                var contantBuffers = ShaderManager.GetConstantBufferDefinitions();

                int tableParamCounter = 1;
                foreach (var cb in contantBuffers)
                {
                    var paramProperties = cb.GetParamProperties();

                    if (paramProperties.Count > 0)
                    {
                        Label groupLabel = new Label();
                        groupLabel.Text        = cb.m_Name;
                        groupLabel.BorderStyle = BorderStyle.FixedSingle;
                        groupLabel.Anchor      = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                        tableLayout.Controls.Add(groupLabel, 0, tableParamCounter);
                        tableParamCounter++;

                        foreach (var param in paramProperties)
                        {
                            Label lb = new Label();
                            lb.Text   = param.name;
                            lb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                            TextBox  text = new TextBox();
                            TrackBar tb   = new TrackBar();
                            tb.Size     = new System.Drawing.Size(400, 10);
                            tb.Anchor   = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                            tb.Minimum  = 0;
                            tb.Maximum  = 1024;
                            tb.Value    = (int)(((Single)param.paramValue - param.paramRangeMin) / (param.paramRangeMax - param.paramRangeMin) * 1024);
                            text.Text   = ((Single)param.paramValue).ToString();
                            text.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                            tableLayout.Controls.Add(lb, 0, tableParamCounter);
                            tableLayout.Controls.Add(tb, 1, tableParamCounter);
                            tableLayout.Controls.Add(text, 2, tableParamCounter);
                            propertyControlBindings.Add(param, new Tuple <TrackBar, TextBox>(tb, text));
                            tableParamCounter++;
                        }
                    }
                }
            }

            Scene scene = new Scene();

            scene.Initialize(device, form, panel, ResolutionX, ResolutionY);

            var resolvedRenderTarget = RenderTargetSet.CreateRenderTargetSet(device, ResolutionX, ResolutionY, Format.R8G8B8A8_UNorm, 1, false);

            RenderTargetSet.BindNull(device.ImmediateContext);

            // setting a viewport is required if you want to actually see anything
            var context = device.ImmediateContext;

            // prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
            using (var factory = swapChain.GetParent <Factory>())
                factory.SetWindowAssociation(panel.Handle, WindowAssociationFlags.IgnoreAltEnter);

            int counter = 0;

            Dictionary <string, double> profilers;

            profilers = new Dictionary <String, double>();

            MessagePump.Run(form, () =>
            {
                TemporalSurfaceManager.UpdateTemporalSurfaces();
                ShaderManager.UpdateShaderManager(device);

                GPUProfiler.BeginFrameProfiling(context);
                scene.RenderFrame(context, 0.0f, resolvedRenderTarget);
                context.CopyResource(resolvedRenderTarget.m_RenderTargets[0].m_TextureObject2D, swapChainResource);
                GPUProfiler.EndFrameProfiling(context);
                swapChain.Present(0, PresentFlags.None);
                context.PixelShader.SetShaderResource(null, 0);

                if (GPUProfiler.m_CurrentFrameProfilerTree != null)
                {
                    Action <GPUProfiler.ProfilerTreeMember, String> processLevel = null;

                    processLevel = (GPUProfiler.ProfilerTreeMember treeMember, String level) =>
                    {
                        string finalName = level + treeMember.m_Name;

                        if (profilers.ContainsKey(finalName))
                        {
                            profilers[finalName] += treeMember.m_Time;
                        }
                        else
                        {
                            profilers.Add(finalName, treeMember.m_Time);
                        }

                        foreach (var v in treeMember.m_ChildMembers)
                        {
                            processLevel(v, level + "_");
                        }
                    };

                    processLevel(GPUProfiler.m_CurrentFrameProfilerTree, "");
                }

                if (++counter == 10)
                {
                    foreach (var propertyBinding in propertyControlBindings)
                    {
                        var property = propertyBinding.Key;
                        var trackBar = propertyBinding.Value.Item1;
                        var textBox  = propertyBinding.Value.Item2;

                        float rawVal = (float)trackBar.Value / 1024.0f;
                        if (property.isGamma)
                        {
                            rawVal = (float)Math.Pow((double)rawVal, 2.2);
                        }
                        float val           = rawVal * (property.paramRangeMax - property.paramRangeMin) + property.paramRangeMin;
                        property.paramValue = val;
                        textBox.Text        = val.ToString("F");
                    }

                    DataGridView dataGridView = form.GetDataGridView();
                    dataGridView.Rows.Clear();

                    foreach (var profilerEntry in profilers)
                    {
                        DataGridViewRow row = new DataGridViewRow();
                        row.CreateCells(dataGridView);
                        row.Cells[0].Value = profilerEntry.Key;
                        row.Cells[1].Value = String.Format("{0}", Math.Round(profilerEntry.Value * 100.0 / 10.0) / 100.0);
                        dataGridView.Rows.Add(row);
                    }

                    profilers.Clear();

                    counter = 0;
                }
                System.Threading.Thread.Sleep(15);
            });
        }
        public RenderTargetSet RenderShadows(DeviceContext context, SimpleSceneWrapper simpleSceneWrapper)
        {
            RenderTargetSet.RenderTargetDescriptor evsmDescriptor =
                new RenderTargetSet.RenderTargetDescriptor()
            {
                m_Format      = Format.R32G32B32A32_Float,
                m_HasDepth    = false,
                m_NumSurfaces = 1,
                m_Height      = 1024,
                m_Width       = 1024
            };

            RenderTargetSet EVSMTexture = m_EVSMTexture == null?RenderTargetManager.RequestRenderTargetFromPool(evsmDescriptor) : m_EVSMTexture;

            {
                RenderTargetSet.RenderTargetDescriptor shadowMapDescriptor =
                    new RenderTargetSet.RenderTargetDescriptor()
                {
                    m_Format      = Format.Unknown,
                    m_HasDepth    = true,
                    m_NumSurfaces = 0,
                    m_Height      = 2048,
                    m_Width       = 2048
                };

                RenderTargetSet shadowBuffer = RenderTargetManager.RequestRenderTargetFromPool(shadowMapDescriptor);

                using (new GpuProfilePoint(context, "Shadowmap"))
                {
                    // set the shaders
                    context.VertexShader.Set(ShaderManager.GetVertexShader("VertexShadow"));
                    context.PixelShader.Set(null);

                    shadowBuffer.Clear(context, new Color4(), true);
                    shadowBuffer.BindAsRenderTarget(context, true);

                    ContextHelper.SetDepthStencilState(context, ContextHelper.DepthConfigurationType.DepthWriteCompare);

                    // render triangles
                    simpleSceneWrapper.RenderNoMaterials(context);

                    RenderTargetSet.BindNull(context);
                }

                using (new GpuProfilePoint(context, "EVSM Resolve"))
                {
                    shadowBuffer.BindDepthAsSRV(context, 0);
                    EVSMTexture.BindAsRenderTarget(context);
                    PostEffectHelper.RenderFullscreenTriangle(context, "ReconstructEVSM");
                    RenderTargetSet.BindNull(context);
                    ContextHelper.ClearSRVs(context);
                }

                RenderTargetSet EVSMTextureTemp = RenderTargetManager.RequestRenderTargetFromPool(evsmDescriptor);

                using (new GpuProfilePoint(context, "EVSM Blur"))
                {
                    EVSMTexture.BindSRV(context, 1);
                    EVSMTextureTemp.BindAsRenderTarget(context);
                    PostEffectHelper.RenderFullscreenTriangle(context, "BlurEVSMHorizontal");
                    RenderTargetSet.BindNull(context);
                    ContextHelper.ClearSRVs(context);

                    EVSMTextureTemp.BindSRV(context, 1);
                    EVSMTexture.BindAsRenderTarget(context);
                    PostEffectHelper.RenderFullscreenTriangle(context, "BlurEVSMVertical");
                    RenderTargetSet.BindNull(context);
                    ContextHelper.ClearSRVs(context);
                }

                RenderTargetManager.ReleaseRenderTargetToPool(shadowBuffer);
                RenderTargetManager.ReleaseRenderTargetToPool(EVSMTextureTemp);
            }

            m_EVSMTexture = EVSMTexture;
            return(EVSMTexture);
        }