public static void Initialize(Device device, DeviceContext context)
        {
            // Create helper textures
            m_PermTexture       = RenderTargetSet.CreateRenderTargetSet(device, 256, 1, Format.R8_UNorm, 1, false);
            m_PermTexture2D     = RenderTargetSet.CreateRenderTargetSet(device, 256, 256, Format.R8G8B8A8_UNorm, 1, false);
            m_GradTexture       = RenderTargetSet.CreateRenderTargetSet(device, 16, 1, Format.R8G8B8A8_SNorm, 1, false);
            m_PermGradTexture   = RenderTargetSet.CreateRenderTargetSet(device, 256, 1, Format.R8G8B8A8_SNorm, 1, false);
            m_PermGrad4DTexture = RenderTargetSet.CreateRenderTargetSet(device, 256, 1, Format.R8G8B8A8_SNorm, 1, false);
            m_GradTexture4D     = RenderTargetSet.CreateRenderTargetSet(device, 32, 1, Format.R8G8B8A8_SNorm, 1, false);

            m_PermTexture.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "GeneratePermTexture");
            RenderTargetSet.BindNull(context);

            m_PermTexture2D.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "GeneratePermTexture2d");
            RenderTargetSet.BindNull(context);

            m_GradTexture.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "GenerateGradTexture");
            RenderTargetSet.BindNull(context);

            m_PermGradTexture.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "GeneratePermGradTexture");
            RenderTargetSet.BindNull(context);

            m_PermGrad4DTexture.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "GeneratePermGrad4dTexture");
            RenderTargetSet.BindNull(context);

            m_GradTexture4D.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "GenerateGradTexture4d");
            RenderTargetSet.BindNull(context);
        }
 public static void PresentDebug(DeviceContext context, RenderTargetSet target)
 {
     if (m_CurrentDebugSurface != "None")
     {
         PostEffectHelper.Copy(context, target, m_DebugRenderTarget);
     }
 }
Example #3
0
        public void ExecutePass(
            DeviceContext context,
            RenderTargetSet target,
            RenderTargetSet sourceCurrent,
            RenderTargetSet sourceHistory,
            RenderTargetSet motionVectors,
            RenderTargetSet motionVectorsPrevious,
            bool accumulateHistory = false)
        {
            using (new GpuProfilePoint(context, "ResolveTemporalMotionBased"))
            {
                sourceCurrent.BindSRV(context, 0);
                sourceHistory.BindSRV(context, 1);
                motionVectors.BindSRV(context, 2);
                motionVectorsPrevious.BindSRV(context, 3);

                target.BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, accumulateHistory ? "ResolveTemporalHistoryAccumulation" : "ResolveTemporalMotionBased");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);

                if (accumulateHistory)
                {
                    PostEffectHelper.Copy(context, sourceCurrent, target);
                }
            }
        }
        public void ExecutePass(DeviceContext context, RenderTargetSet sourceSet)
        {
            PostEffectHelper.LinearizeDepth(context, m_LinearDepth, sourceSet);
            DebugManager.RegisterDebug(context, "LinearDepth", m_LinearDepth);

            using (new GpuProfilePoint(context, "DownsampleDepthNormals"))
            {
                m_LinearDepth.BindSRV(context, 0);
                sourceSet.BindSRV(context, 1);

                m_HalfLinearDepth.BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "DownsampleDepthNormals");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);
            }
            DebugManager.RegisterDebug(context, "HalfLinearDepth", m_HalfLinearDepth);
            DebugManager.RegisterDebug(context, "HalfNormals", m_HalfLinearDepth, 1);

            using (new GpuProfilePoint(context, "OutputBilateralOffsets"))
            {
                m_LinearDepth.BindSRV(context, 0);
                m_HalfLinearDepth.BindSRV(context, 2);

                m_BilateralUpsampleOffsets.BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "OutputBilateralOffsets");
                RenderTargetSet.BindNull(context);
                ContextHelper.ClearSRVs(context);
            }

            DebugManager.RegisterDebug(context, "BilateralOffsets", m_BilateralUpsampleOffsets);
        }
Example #5
0
 public static void RegisterDebug(DeviceContext context, string name, RenderTargetSet debuggedRT, int rtNum = 0)
 {
     if (m_AvailableModes.Contains(name))
     {
         if (m_CurrentDebugSurface == name)
         {
             if (m_CurrentDebugMode == "A")
             {
                 PostEffectHelper.CopyAlpha(context, m_DebugRenderTarget, debuggedRT, rtNum);
             }
             else if (m_CurrentDebugMode == "FRAC")
             {
                 PostEffectHelper.CopyFrac(context, m_DebugRenderTarget, debuggedRT, rtNum);
             }
             else if (m_CurrentDebugMode == "RGBGamma")
             {
                 PostEffectHelper.CopyGamma(context, m_DebugRenderTarget, debuggedRT, rtNum);
             }
             else
             {
                 PostEffectHelper.Copy(context, m_DebugRenderTarget, debuggedRT, rtNum);
             }
         }
     }
     else
     {
         m_IsUIRebuildRequired = true;
         m_AvailableModes.Add(name);
     }
 }
Example #6
0
        public void ExecutePass(DeviceContext context, SimpleSceneWrapper sceneWrapper, Vector3 position)
        {
            //using (new GpuProfilePoint(context, "CubemapRender"))
            {
                context.PixelShader.SetShaderResource(m_GIVolumeR.m_ShaderResourceView, 5);
                context.PixelShader.SetShaderResource(m_GIVolumeG.m_ShaderResourceView, 6);
                context.PixelShader.SetShaderResource(m_GIVolumeB.m_ShaderResourceView, 7);

                CubemapRenderHelper.RenderCubemap(context, m_CubeObject, m_CubeObjectDepth, sceneWrapper, position, 50.0f, false, new Color4(1.0f, 0.75f, 0.75f, 1.0f));
            }

            //using (new GpuProfilePoint(context, "InitSH"))
            {
                context.PixelShader.SetShaderResource(m_CubeObject.m_ShaderResourceView, 0);
                m_InitialSHSet.BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "GIInitSH");
                RenderTargetSet.BindNull(context);

                DownsamplePass(context, m_InitialSHSet, m_Downsampled16x16SHSetR, m_Downsampled16x16SHSetG, m_Downsampled16x16SHSetB);

                m_Downsampled16x16SHSetR.BindSRV(context, 0);
                m_Downsampled4x4SHSetR.BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "Downsample4x4");
                RenderTargetSet.BindNull(context);

                m_Downsampled16x16SHSetG.BindSRV(context, 0);
                m_Downsampled4x4SHSetG.BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "Downsample4x4");
                RenderTargetSet.BindNull(context);

                m_Downsampled16x16SHSetB.BindSRV(context, 0);
                m_Downsampled4x4SHSetB.BindAsRenderTarget(context);
                PostEffectHelper.RenderFullscreenTriangle(context, "Downsample4x4");
                RenderTargetSet.BindNull(context);

                Vector3 sceneMin, sceneMax;
                sceneWrapper.GetSceneBounds(out sceneMin, out sceneMax);
                Vector3 positionInVolume = position - sceneMin;
                positionInVolume.X = positionInVolume.X / (sceneMax.X - sceneMin.X);
                positionInVolume.Y = positionInVolume.Y / (sceneMax.Y - sceneMin.Y);
                positionInVolume.Z = positionInVolume.Z / (sceneMax.Z - sceneMin.Z);
                dynamic scb = m_GIConstantBuffer;
                scb.g_InjectPosition = new Vector4(new Vector3(positionInVolume.X * (float)(m_VolumeSizeX - 1), positionInVolume.Y * (float)(m_VolumeSizeY - 1), positionInVolume.Z * (float)(m_VolumeSizeZ - 1)), 0);
                m_GIConstantBuffer.CompileAndBind(context);

                m_Downsampled4x4SHSetR.BindSRV(context, 2);
                m_Downsampled4x4SHSetG.BindSRV(context, 3);
                m_Downsampled4x4SHSetB.BindSRV(context, 4);
                context.ComputeShader.SetUnorderedAccessView(m_GIVolumeR.m_UnorderedAccessView, 0);
                context.ComputeShader.SetUnorderedAccessView(m_GIVolumeG.m_UnorderedAccessView, 1);
                context.ComputeShader.SetUnorderedAccessView(m_GIVolumeB.m_UnorderedAccessView, 2);
                ShaderManager.ExecuteComputeForSize(context, 1, 1, 1, "InjectSHIntoVolume");

                ContextHelper.ClearCSContext(context);
            }
        }
 public static void CopyFrac(DeviceContext context, RenderTargetSet target, RenderTargetSet source, int rtNum = 0)
 {
     using (new GpuProfilePoint(context, "Copy"))
     {
         source.BindSRV(context, 0, rtNum);
         target.BindAsRenderTarget(context);
         PostEffectHelper.RenderFullscreenTriangle(context, "CopyFrac");
         RenderTargetSet.BindNull(context);
         ContextHelper.ClearSRVs(context);
     }
 }
 public static void LinearizeDepth(DeviceContext context, RenderTargetSet target, RenderTargetSet source)
 {
     using (new GpuProfilePoint(context, "LinearizeDepth"))
     {
         source.BindDepthAsSRV(context, 0);
         target.BindAsRenderTarget(context);
         PostEffectHelper.RenderFullscreenTriangle(context, "LinearizeDepth");
         RenderTargetSet.BindNull(context);
         ContextHelper.ClearSRVs(context);
     }
 }
Example #9
0
 public void ExecutePass(DeviceContext context, RenderTargetSet target, RenderTargetSet source)
 {
     using (new GpuProfilePoint(context, "ResolveMotionVectors"))
     {
         source.BindDepthAsSRV(context, 0);
         target.BindAsRenderTarget(context);
         PostEffectHelper.RenderFullscreenTriangle(context, "ResolveMotionVectors");
         RenderTargetSet.BindNull(context);
         ContextHelper.ClearSRVs(context);
     }
 }
 public static void Difference(DeviceContext context, RenderTargetSet target, RenderTargetSet source1, RenderTargetSet source2, int rtNum = 0)
 {
     using (new GpuProfilePoint(context, "Difference"))
     {
         source1.BindSRV(context, 0, rtNum);
         source2.BindSRV(context, 1, rtNum);
         target.BindAsRenderTarget(context);
         PostEffectHelper.RenderFullscreenTriangle(context, "Difference");
         RenderTargetSet.BindNull(context);
         ContextHelper.ClearSRVs(context);
     }
 }
Example #11
0
 public void ExecutePass(DeviceContext context, RenderTargetSet target, RenderTargetSet source, RenderTargetSet luminanceTexture)
 {
     using (new GpuProfilePoint(context, "HDR Resolve"))
     {
         source.BindSRV(context, 0);
         luminanceTexture.BindSRV(context, 1);
         target.BindAsRenderTarget(context);
         PostEffectHelper.RenderFullscreenTriangle(context, "ResolveHDR");
         RenderTargetSet.BindNull(context);
         ContextHelper.ClearSRVs(context);
     }
 }
Example #12
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);
        }
Example #13
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 #14
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 #15
0
        void DownsamplePass(DeviceContext context, RenderTargetSet inputSet, RenderTargetSet targetRSet, RenderTargetSet targetGSet, RenderTargetSet targetBSet)
        {
            inputSet.BindSRV(context, 0, 0);
            targetRSet.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "Downsample4x4");
            RenderTargetSet.BindNull(context);

            inputSet.BindSRV(context, 0, 1);
            targetGSet.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "Downsample4x4");
            RenderTargetSet.BindNull(context);

            inputSet.BindSRV(context, 0, 2);
            targetBSet.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "Downsample4x4");
            RenderTargetSet.BindNull(context);
        }
        public static void GenerateCubemapMips(DeviceContext context, string shader, TextureObject targetTexture, TextureObject sourceTexture)
        {
            for (int m = 0; m < targetTexture.m_Mips; ++m)
            {
                for (int i = 0; i < 6; ++i)
                {
                    dynamic cdb = m_CubemapDownsampleBuffer;
                    cdb.g_Mip  = (float)m;
                    cdb.g_Face = i;
                    m_CubemapDownsampleBuffer.CompileAndBind(context);

                    RenderTargetSet.BindTextureAsRenderTarget(context, targetTexture.m_ArrayRenderTargetViews[m * 6 + i], null, targetTexture.m_Width >> m, targetTexture.m_Height >> m);
                    context.PixelShader.SetShaderResource(PostEffectHelper.m_RandomNumbersBuffer.m_ShaderResourceView, 39);
                    context.PixelShader.SetShaderResource(sourceTexture.m_ShaderResourceView, 0);
                    PostEffectHelper.RenderFullscreenTriangle(context, shader);
                    RenderTargetSet.BindNull(context);
                }
            }
        }
Example #17
0
        public static void Initialize(Device device, DeviceContext context)
        {
            // Create helper textures
            m_EnvLightingLUT   = RenderTargetSet.CreateRenderTargetSet(device, 128, 128, Format.R16G16_Float, 1, false);
            m_GGXLightingDLUT  = RenderTargetSet.CreateRenderTargetSet(device, 256, 128, Format.R16_Float, 1, false);
            m_GGXLightingFVLUT = RenderTargetSet.CreateRenderTargetSet(device, 256, 128, Format.R16G16_Float, 1, false);

            m_GGXLightingDLUT.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "GenerateGGXLightingDLUT");
            RenderTargetSet.BindNull(context);

            m_GGXLightingFVLUT.BindAsRenderTarget(context);
            PostEffectHelper.RenderFullscreenTriangle(context, "GenerateGGXLightingFVLUT");
            RenderTargetSet.BindNull(context);

            m_EnvLightingLUT.BindAsRenderTarget(context);
            context.PixelShader.SetShaderResource(PostEffectHelper.m_RandomNumbersBuffer.m_ShaderResourceView, 39);
            PostEffectHelper.RenderFullscreenTriangle(context, "GenerateEnvLightingLUT");
            RenderTargetSet.BindNull(context);
        }
Example #18
0
 public static void PresentDebug(DeviceContext context, RenderTargetSet target)
 {
     if (m_CurrentDebugSurface != "None")
     {
         PostEffectHelper.Copy(context, target, m_DebugRenderTarget);
     }
     if (m_TakeSnapshot)
     {
         PostEffectHelper.Copy(context, m_SnapshotRenderTarget, target);
         m_TakeSnapshot = false;
     }
     if (m_ShowSnapshot)
     {
         if (m_ShowSnapshotDifference)
         {
             PostEffectHelper.Copy(context, m_DebugRenderTarget, target);
             PostEffectHelper.Difference(context, target, m_SnapshotRenderTarget, m_DebugRenderTarget);
         }
         else
         {
             PostEffectHelper.Copy(context, target, m_SnapshotRenderTarget);
         }
     }
 }
Example #19
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 #20
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);
            });
        }
Example #21
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 #22
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 #23
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 #24
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);
            }
        }
        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);
        }