Ejemplo n.º 1
0
        protected override void DrawOverride()
        {
            using (var rasterizerState = RasterizerState.FromDescription(Device, new RasterizerStateDescription {
                FillMode = FillMode.Wireframe,
                CullMode = CullMode.None,
                IsAntialiasedLineEnabled = UseAntialiazing,
                IsFrontCounterclockwise = false,
                IsDepthClipEnabled = true
            })) {
                DeviceContext.OutputMerger.BlendState = null;
                DeviceContext.Rasterizer.State        = rasterizerState;
                DeviceContext.ClearRenderTargetView(RenderTargetView, Color.Transparent);

                using (var buffer = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                    buffer.Resize(DeviceContextHolder, Width, Height, null);

                    DeviceContext.ClearRenderTargetView(buffer.TargetView, Color.Transparent);
                    DeviceContext.OutputMerger.SetTargets(buffer.TargetView);

                    RenderUv();

                    DeviceContext.Rasterizer.State = null;

                    PrepareForFinalPass();
                    if (UseFxaa)
                    {
                        DeviceContextHolder.GetHelper <FxaaHelper>().Draw(DeviceContextHolder, buffer.View, RenderTargetView);
                    }
                    else
                    {
                        DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, buffer.View, RenderTargetView);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void AddShadow()
        {
            DeviceContext.OutputMerger.BlendState        = _bakedBlendState;
            DeviceContext.OutputMerger.DepthStencilState = null;
            DeviceContext.Rasterizer.State = null;
            DeviceContext.Rasterizer.SetViewports(Viewport);

            DeviceContext.OutputMerger.SetTargets(_bufferF1.TargetView);
            DeviceContext.ClearRenderTargetView(_bufferF1.TargetView, new Color4(0f, 0f, 0f, 0f));
            _effect.FxDepthMap.SetResource(_shadowBuffer.View);
            _effect.FxShadowViewProj.SetMatrix(_shadowCamera.ViewProj * new Matrix {
                M11 = 0.5f,
                M22 = -0.5f,
                M33 = 1.0f,
                M41 = 0.5f,
                M42 = 0.5f,
                M44 = 1.0f
            });

            RenderPieces();
            // DeviceContext.Rasterizer.State = null;

            // copy to summary buffer
            DeviceContext.OutputMerger.BlendState = _summBlendState;
            DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, _bufferF1.View, _bufferFSumm.TargetView);
        }
Ejemplo n.º 3
0
        private bool AaPass(ShaderResourceView input, RenderTargetView output)
        {
            if (IsSmaaAvailable && UseSmaa)
            {
                throw new NotImplementedException();
            }

            if (UseSsaa)
            {
                if (UseFxaa)
                {
                    DeviceContextHolder.GetHelper <FxaaHelper>().Draw(DeviceContextHolder, input, _bufferFSsaaFxaa.TargetView, new Vector2(Width, Height));
                    input = _bufferFSsaaFxaa.View;
                }

                DeviceContextHolder.GetHelper <DownsampleHelper>().Draw(DeviceContextHolder, input, new Vector2(Width, Height),
                                                                        output, new Vector2(ActualWidth, ActualHeight), TemporaryFlag);
                return(true);
            }

            if (UseFxaa)
            {
                DeviceContextHolder.GetHelper <FxaaHelper>().Draw(DeviceContextHolder, input, output);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
        // slower, but more accurate
        private void CustomBlending(TargetResourceTexture result, TargetResourceTexture foreground, TargetResourceTexture temporary,
                                    Color4 color)
        {
            // ssaa if needed
            ShaderResourceView view;

            if (UseSsaa)
            {
                DeviceContextHolder.GetHelper <DownsampleHelper>().Draw(DeviceContextHolder, foreground, _a0Buffer);
                view = _a0Buffer.View;
            }
            else
            {
                view = foreground.View;
            }

            DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, result.View, temporary.TargetView);

            DeviceContextHolder.PrepareQuad(_effect.LayoutPT);
            DeviceContext.OutputMerger.SetTargets(result.TargetView);

            _effect.FxInputMap.SetResource(view);
            _effect.FxBgMap.SetResource(temporary.View);
            _effect.FxBlendColor.Set(color);
            _effect.TechBlend.DrawAllPasses(DeviceContext, 6);
        }
Ejemplo n.º 5
0
        public static AoHelperBase GetHelper(this AoType type, DeviceContextHolder holder)
        {
            switch (type)
            {
            case AoType.Ssao:
                return(holder.GetHelper <SsaoHelper>());

            case AoType.SsaoAlt:
                return(holder.GetHelper <SsaoAltHelper>());

            case AoType.Hbao:
                return(holder.GetHelper <HbaoHelper>());

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Ejemplo n.º 6
0
        private void Draw(float multipler, [CanBeNull] IProgress <double> progress, CancellationToken cancellation)
        {
            _effect.FxAmbient.Set(Ambient);
            DeviceContext.ClearRenderTargetView(_bufferFSumm.TargetView, Color.Transparent);
            DeviceContext.ClearRenderTargetView(_bufferA.TargetView, Color.Transparent);

            // draw
            var t = DrawLights(progress, cancellation);

            if (cancellation.IsCancellationRequested)
            {
                return;
            }

            DeviceContextHolder.PrepareQuad(_effect.LayoutPT);
            DeviceContext.Rasterizer.State               = null;
            DeviceContext.OutputMerger.BlendState        = null;
            DeviceContext.OutputMerger.DepthStencilState = null;
            DeviceContext.Rasterizer.SetViewports(Viewport);
            _effect.FxSize.Set(new Vector4(Width, Height, 1f / Width, 1f / Height));

            DeviceContext.ClearRenderTargetView(_bufferF1.TargetView, Color.Transparent);
            DeviceContext.OutputMerger.SetTargets(_bufferF1.TargetView);
            _effect.FxInputMap.SetResource(_bufferFSumm.View);
            _effect.FxCount.Set(t / SkyBrightnessLevel);
            _effect.FxMultipler.Set(multipler);
            _effect.FxGamma.Set(Gamma);
            _effect.FxScreenSize.Set(new Vector2(Width, Height));
            _effect.TechAoResult.DrawAllPasses(DeviceContext, 6);

            for (var i = 0; i < Padding; i++)
            {
                DeviceContext.ClearRenderTargetView(_bufferF2.TargetView, Color.Transparent);
                DeviceContext.OutputMerger.SetTargets(_bufferF2.TargetView);
                _effect.FxInputMap.SetResource(_bufferF1.View);
                _effect.TechAoGrow.DrawAllPasses(DeviceContext, 6);

                DeviceContext.ClearRenderTargetView(_bufferF1.TargetView, Color.Transparent);
                DeviceContext.OutputMerger.SetTargets(_bufferF1.TargetView);
                _effect.FxInputMap.SetResource(_bufferF2.View);
                _effect.TechAoGrow.DrawAllPasses(DeviceContext, 6);
            }

            if (UseFxaa)
            {
                DeviceContextHolder.GetHelper <FxaaHelper>().Draw(DeviceContextHolder, _bufferF1.View, _bufferF2.TargetView);

                PrepareForFinalPass();
                DeviceContextHolder.GetHelper <DownsampleHelper>().Draw(DeviceContextHolder, _bufferF2, _bufferA);
            }
            else
            {
                PrepareForFinalPass();
                DeviceContextHolder.GetHelper <DownsampleHelper>().Draw(DeviceContextHolder, _bufferF1, _bufferA);
            }

            DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, _bufferA.View, RenderTargetView);
        }
Ejemplo n.º 7
0
        private void BloomThenAa()
        {
            var bloomView = HdrPass(_bufferF.View, _bufferA.TargetView, OutputViewport) ? _bufferA.View : _bufferF.View;
            var aa        = AaPass(bloomView, RenderTargetView);

            if (!aa)
            {
                // AA stage didn’t move bloomed buffer to RTV
                DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, bloomView, RenderTargetView);
            }
        }
        private void ProcessHdr(TargetResourceTexture target, TargetResourceTexture source, TargetResourceTexture temporary)
        {
            switch (Mode)
            {
            case RenderingMode.DebugGBuffer:
            case RenderingMode.DebugPostEffects:
            case RenderingMode.DebugLocalReflections:
                return;
            }

            DeviceContext.OutputMerger.SetTargets(target.TargetView);
            DeviceContext.ClearRenderTargetView(target.TargetView, ColorTransparent);
            DeviceContext.OutputMerger.BlendState = null;
            DeviceContextHolder.GetHelper <HdrHelper>().Draw(DeviceContextHolder, source.View, temporary);
        }
        private void DrawReflections(TargetResourceTexture target, TargetResourceTexture temporary = null, TargetResourceTexture light = null, DepthStencilView limitedBy = null)
        {
            var sslr = DeviceContextHolder.GetHelper <SslrHelper>();

            DeviceContextHolder.QuadBuffers.Prepare(DeviceContext, sslr.Effect.LayoutPT);
            DeviceContext.OutputMerger.BlendState = null;

            if (limitedBy == null)
            {
                DeviceContext.OutputMerger.SetTargets(target.TargetView);
            }
            else
            {
                DeviceContext.OutputMerger.SetTargets(limitedBy, target.TargetView);
                DeviceContext.OutputMerger.DepthStencilState = DeviceContextHolder.States.GreaterReadOnlyDepthState;
            }

            // camera position & matrix
            sslr.Effect.FxWorldViewProj.SetMatrix(Camera.ViewProj);
            sslr.Effect.FxWorldViewProjInv.SetMatrix(Camera.ViewProjInvert);
            sslr.Effect.FxEyePosW.Set(Camera.Position);

            // g-buffer
            sslr.Effect.FxBaseMap.SetResource(_gBufferBase.View);
            sslr.Effect.FxNormalMap.SetResource(_gBufferNormal.View);
            sslr.Effect.FxDepthMap.SetResource(_gDepthBuffer.View);
            sslr.Effect.FxLightMap.SetResource(light?.View);

            sslr.Effect.TechHabrahabrVersion.DrawAllPasses(DeviceContext, 6);

            if (temporary != null)
            {
                DeviceContext.OutputMerger.SetTargets(temporary.TargetView);
                DeviceContextHolder.GetHelper <BlurHelper>().BlurReflectionVertically(DeviceContextHolder, target.View, _gBufferMaps.View);

                DeviceContext.OutputMerger.SetTargets(target.TargetView);
                DeviceContextHolder.GetHelper <BlurHelper>().BlurReflectionHorizontally(DeviceContextHolder, temporary.View, _gBufferMaps.View);
            }

            if (limitedBy != null)
            {
                DeviceContext.OutputMerger.DepthStencilState = null;
            }
        }
Ejemplo n.º 10
0
        public void OnInitialize(DeviceContextHolder holder) {
            _effect = holder.GetEffect<EffectPpHdr>();
            _blurHelper = holder.GetHelper<BlurHelper>();

            _textures = Enumerable.Range(0, DownsamplerAdaptationCycles)
                          .Select(x => TargetResourceTexture.Create(Format.R16G16B16A16_Float))
                          .ToArray();

            _averateColor = Enumerable.Range(0, 2).Select(x => {
                var t = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
                t.Resize(holder, 1, 1);
                return t;
            }).ToArray();

            _newAverageColor = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
            _newAverageColor.Resize(holder, 1, 1);

            _bloomTexture = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
            _tempTexture = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
        }
Ejemplo n.º 11
0
        private AoHelperBase GetAoHelper()
        {
            switch (AoType)
            {
            case AoType.Ssao:
                return(DeviceContextHolder.GetHelper <SsaoHelper>());

            case AoType.SsaoAlt:
                return(DeviceContextHolder.GetHelper <SsaoAltHelper>());

            case AoType.Hbao:
                return(DeviceContextHolder.GetHelper <HbaoHelper>());

            case AoType.Assao:
                return(DeviceContextHolder.GetHelper <AssaoHelper>());

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        protected sealed override void DrawSprites()
        {
            var sprite = Sprite;

            if (sprite == null)
            {
                throw new NotSupportedException();
            }

            // drawing GUI
            sprite.HandleBlendState        = false;
            sprite.HandleDepthStencilState = false;

            DeviceContext.OutputMerger.SetTargets(_temporaryBuffer0.TargetView);
            DeviceContext.ClearRenderTargetView(_temporaryBuffer0.TargetView, Color.Transparent);

            DrawSpritesInner();
            sprite.Flush();

            // blurring
            DeviceContext.OutputMerger.BlendState = null;
            DeviceContextHolder.GetHelper <BlurHelper>().Blur(DeviceContextHolder, _temporaryBuffer0, _temporaryBuffer1, target: _temporaryBuffer2, iterations: 2);

            // as a shadow
            _ppBasic.FxScreenSize.Set(new Vector4(Width, Height, 1f / Width, 1f / Height));

            DeviceContext.OutputMerger.SetTargets(_temporaryBuffer1.TargetView);
            DeviceContextHolder.QuadBuffers.Prepare(DeviceContext, _ppBasic.LayoutPT);

            _ppBasic.FxInputMap.SetResource(_temporaryBuffer2.View);
            _ppBasic.FxOverlayMap.SetResource(_temporaryBuffer0.View);
            _ppBasic.TechShadow.DrawAllPasses(DeviceContext, 6);

            // combining with _outputBuffer
            ResetTargets();
            DeviceContextHolder.PrepareQuad(_ppBasic.LayoutPT);

            _ppBasic.FxInputMap.SetResource(_outputBuffer.View);
            _ppBasic.FxOverlayMap.SetResource(_temporaryBuffer1.View);
            _ppBasic.TechOverlay.DrawAllPasses(DeviceContext, 6);
        }
Ejemplo n.º 13
0
        public void OnInitialize(DeviceContextHolder holder)
        {
            _effect     = holder.GetEffect <EffectPpHdr>();
            _blurHelper = holder.GetHelper <BlurHelper>();

            _textures = Enumerable.Range(0, DownsamplerAdaptationCycles)
                        .Select(x => TargetResourceTexture.Create(Format.R16G16B16A16_Float))
                        .ToArray();

            _averateColor = Enumerable.Range(0, 2).Select(x => {
                var t = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
                t.Resize(holder, 1, 1, null);
                return(t);
            }).ToArray();

            _newAverageColor = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
            _newAverageColor.Resize(holder, 1, 1, null);

            _bloomTexture = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
            _tempTexture  = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
        }
Ejemplo n.º 14
0
        private void AaThenBloom()
        {
            var aaView = AaPass(_bufferF.View, _bufferA.TargetView) ? _bufferA.View : _bufferF.View;

            if (UseColorGrading)
            {
                var hdrView = HdrPass(aaView, _bufferAColorGrading.TargetView, _bufferAColorGrading.Viewport) ? _bufferAColorGrading.View : aaView;
                if (!ColorGradingPass(hdrView, RenderTargetView, OutputViewport))
                {
                    // nothing happened in color grading stage
                    DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, hdrView, RenderTargetView);
                }
            }
            else
            {
                if (!HdrPass(aaView, RenderTargetView, OutputViewport))
                {
                    // HDR stage didn’t move AA buffer to RTV
                    DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, aaView, RenderTargetView);
                }
            }
        }
        protected void FinalStep(TargetResourceTexture source)
        {
            DeviceContext.ClearRenderTargetView(_outputBuffer.TargetView, ColorTransparent);

            if (!UseFxaa)
            {
                DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, source.View, _outputBuffer.TargetView);
                return;
            }

            if (UseExperimentalSmaa)
            {
                DeviceContextHolder.GetHelper <SmaaHelper>().Draw(DeviceContextHolder, source.View, _outputBuffer.TargetView, _gBufferBase, _temporaryBuffer3);
            }
            else if (UseExperimentalFxaa)
            {
                DeviceContextHolder.GetHelper <Fxaa311Helper>().Draw(DeviceContextHolder, source.View, _outputBuffer.TargetView, _gBufferMaps);
            }
            else
            {
                DeviceContextHolder.GetHelper <FxaaHelper>().Draw(DeviceContextHolder, source.View, _outputBuffer.TargetView);
            }
        }
Ejemplo n.º 16
0
        protected override void DrawOverride()
        {
            EnsureAiLaneAllRight();
            Camera.UpdateViewMatrix();

            // just in case
            DeviceContext.ClearRenderTargetView(_buffer0.TargetView, Color.Black);
            DeviceContext.ClearRenderTargetView(_buffer1.TargetView, Color.Black);
            DeviceContext.ClearRenderTargetView(RenderTargetView, Color.Transparent);

            // render to buffer-0
            DeviceContext.OutputMerger.SetTargets(_buffer0.TargetView);

            DeviceContext.OutputMerger.BlendState = null;
            DeviceContext.Rasterizer.State        = DeviceContextHolder.States.DoubleSidedState;
            FilteredNode.Draw(DeviceContextHolder, Camera, SpecialRenderMode.Simple);
            DeviceContext.Rasterizer.State = null;

            // blur to buffer-0 using buffer-1 as temporary
            DeviceContextHolder.GetHelper <TrackMapBlurRenderHelper>().Blur(DeviceContextHolder, _buffer0, _buffer1);

            // outline map and add inset shadow to buffer-1 (alpha is in green channel for optional FXAA)
            DeviceContextHolder.GetHelper <TrackMapRenderHelper>().Draw(DeviceContextHolder, _buffer0.View, _buffer1.TargetView);

            // move alpha from green channel to alpha-channel
            if (UseFxaa)
            {
                // applying FXAA first
                DeviceContextHolder.GetHelper <FxaaHelper>().Draw(DeviceContextHolder, _buffer1.View, _buffer0.TargetView);
                DeviceContextHolder.GetHelper <TrackMapRenderHelper>().Final(DeviceContextHolder, _buffer0.View, RenderTargetView, !ShotMode);
            }
            else
            {
                // directly
                DeviceContextHolder.GetHelper <TrackMapRenderHelper>().Final(DeviceContextHolder, _buffer1.View, RenderTargetView, !ShotMode);
            }
        }
Ejemplo n.º 17
0
        protected override void DrawSceneToBuffer()
        {
            if (!UseSslr && !UseAo)
            {
                base.DrawSceneToBuffer();
                return;
            }

            DrawPrepare();

            if (_sslrHelper == null)
            {
                _sslrHelper = DeviceContextHolder.GetHelper <DarkSslrHelper>();
            }

            if (_blurHelper == null)
            {
                _blurHelper = DeviceContextHolder.GetHelper <BlurHelper>();
            }

            // Draw scene to G-buffer to get normals, depth and base reflection
            DeviceContext.Rasterizer.SetViewports(Viewport);
            DeviceContext.OutputMerger.SetTargets(DepthStencilView, _sslrBufferBaseReflection?.TargetView, _gBufferNormals.TargetView, _gBufferDepth.TargetView);
            DeviceContext.ClearDepthStencilView(DepthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);
            DeviceContext.ClearRenderTargetView(_gBufferNormals.TargetView, (Color4) new Vector4(0.5f));

            if (_sslrBufferBaseReflection != null)
            {
                DeviceContext.ClearRenderTargetView(_sslrBufferBaseReflection.TargetView, (Color4) new Vector4(0));
            }

            DeviceContext.ClearRenderTargetView(_gBufferDepth.TargetView, (Color4) new Vector4(1f));

            DeviceContext.OutputMerger.DepthStencilState = null;
            DeviceContext.OutputMerger.BlendState        = null;
            DeviceContext.Rasterizer.State = null;

            if (ShowroomNode != null)
            {
                ShowroomNode.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.GBuffer);
            }
            else
            {
                if (_mirror != null)
                {
                    if (FlatMirror && !FlatMirrorBlurred)
                    {
                        _mirror.DrawReflection(DeviceContextHolder, ActualCamera, SpecialRenderMode.GBuffer);
                    }
                    else
                    {
                        _mirror.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.GBuffer);
                    }
                }
            }

            CarNode?.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.GBuffer);

            // AO?
            if (UseAo)
            {
                var aoHelper = _aoHelper;
                if (aoHelper == null)
                {
                    aoHelper = _aoHelper = GetAoHelper();
                }

                if (AoType == AoType.Hbao)
                {
                    UseSslr = true;
                    SetInnerBuffer(_sslrBufferScene);
                    DrawPreparedSceneToBuffer();
                    (aoHelper as HbaoHelper)?.Prepare(DeviceContextHolder, _sslrBufferScene.View);
                    SetInnerBuffer(null);
                }

                aoHelper.Draw(DeviceContextHolder,
                              _gBufferDepth.View,
                              _gBufferNormals.View,
                              ActualCamera, _aoBuffer.TargetView);
                aoHelper.Blur(DeviceContextHolder, _aoBuffer, InnerBuffer, Camera);

                var effect = Effect;
                effect.FxAoMap.SetResource(_aoBuffer.View);
                Effect.FxAoPower.Set(AoOpacity);
                effect.FxScreenSize.Set(new Vector4(Width, Height, 1f / Width, 1f / Height));

                if (AoDebug)
                {
                    DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, _aoBuffer.View, InnerBuffer.TargetView);
                    return;
                }
            }

            if (UseSslr && _sslrBufferBaseReflection != null)
            {
                // Draw actual scene to _sslrBufferScene
                SetInnerBuffer(_sslrBufferScene);
                DrawPreparedSceneToBuffer();
                SetInnerBuffer(null);

                // Prepare SSLR and combine buffers
#if SSLR_PARAMETRIZED
                if (_sslrParamsChanged)
                {
                    _sslrParamsChanged = false;
                    var effect = DeviceContextHolder.GetEffect <EffectPpDarkSslr>();
                    effect.FxStartFrom.Set(_sslrStartFrom);
                    effect.FxFixMultiplier.Set(_sslrFixMultiplier);
                    effect.FxOffset.Set(_sslrOffset);
                    effect.FxGlowFix.Set(_sslrGrowFix);
                    effect.FxDistanceThreshold.Set(_sslrDistanceThreshold);
                }
#endif

                _sslrHelper.Draw(DeviceContextHolder,
                                 _gBufferDepth.View,
                                 _sslrBufferBaseReflection.View,
                                 _gBufferNormals.View,
                                 ActualCamera, _sslrBufferResult.TargetView);
                _blurHelper.BlurDarkSslr(DeviceContextHolder, _sslrBufferResult, InnerBuffer, (float)(2f * ResolutionMultiplier));
                _sslrHelper.FinalStep(DeviceContextHolder,
                                      _sslrBufferScene.View,
                                      _sslrBufferResult.View,
                                      _sslrBufferBaseReflection.View,
                                      _gBufferNormals.View,
                                      ActualCamera, InnerBuffer.TargetView);
            }
            else
            {
                DrawPreparedSceneToBuffer();
            }
        }
        private SourceReady GetOriginal(ref Dictionary <int, ShaderResourceView> storage, [NotNull] PaintShopSource source, int maxSize,
                                        Func <ShaderResourceView, ShaderResourceView> preparation = null)
        {
            if (MainSlot.Kn5 == null)
            {
                return(null);
            }

            try {
                if (storage == null)
                {
                    storage = new Dictionary <int, ShaderResourceView>(2);
                    if (_sizes == null)
                    {
                        _sizes = new Dictionary <int, Size>();
                    }
                }

                ShaderResourceView original;
                var sourceHashCode = source.GetHashCode();
                var hashCode       = (sourceHashCode * 397) ^ maxSize.GetHashCode();
                if (!storage.TryGetValue(hashCode, out original))
                {
                    Size size;

                    if (source.ByChannels)
                    {
                        var red   = source.RedChannelSource == null ? null : GetOriginal(ref storage, source.RedChannelSource, maxSize);
                        var green = source.GreenChannelSource == null ? null : GetOriginal(ref storage, source.GreenChannelSource, maxSize);
                        var blue  = source.BlueChannelSource == null ? null : GetOriginal(ref storage, source.BlueChannelSource, maxSize);
                        var alpha = source.AlphaChannelSource == null ? null : GetOriginal(ref storage, source.AlphaChannelSource, maxSize);

                        var redSize   = source.RedChannelSource == null ? null : GetSize(source.RedChannelSource);
                        var greenSize = source.GreenChannelSource == null ? null : GetSize(source.GreenChannelSource);
                        var blueSize  = source.BlueChannelSource == null ? null : GetSize(source.BlueChannelSource);
                        var alphaSize = source.AlphaChannelSource == null ? null : GetSize(source.AlphaChannelSource);

                        size = Max(redSize, Max(greenSize, Max(blueSize, alphaSize))) ?? new Size(16, 16);
                        _sizes[sourceHashCode] = size;

                        if (size.Width > maxSize || size.Height > maxSize)
                        {
                            size = new Size(maxSize, maxSize);
                        }

                        using (var combined = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                            combined.Resize(DeviceContextHolder, size.Width, size.Height, null);
                            UseEffect(e => {
                                red.Set(e.FxAoMap, e.FxAoMapChannels);
                                green.Set(e.FxInputMap, e.FxInputMapChannels);
                                blue.Set(e.FxMaskMap, e.FxMaskMapChannels);
                                alpha.Set(e.FxOverlayMap, e.FxOverlayMapChannels);
                                e.TechCombineChannels.DrawAllPasses(DeviceContext, 6);
                            }, combined);

                            combined.KeepView = true;
                            original          = combined.View;
                        }
                    }
                    else
                    {
                        var decoded = GetBytes(source);
                        if (decoded == null)
                        {
                            return(null);
                        }

                        using (var texture = Texture2D.FromMemory(Device, decoded)) {
                            original = new ShaderResourceView(Device, texture);

                            size = new Size(texture.Description.Width, texture.Description.Height);
                            _sizes[sourceHashCode] = size;

                            if (size.Width > maxSize || size.Height > maxSize)
                            {
                                size = new Size(maxSize, maxSize);

                                using (var resized = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                                    resized.Resize(DeviceContextHolder, maxSize, maxSize, null);
                                    DeviceContextHolder.GetHelper <DownsampleHelper>().Draw(DeviceContextHolder,
                                                                                            original, new Vector2(texture.Description.Width, texture.Description.Height),
                                                                                            resized.TargetView, new Vector2(maxSize, maxSize));
                                    original.Dispose();

                                    resized.KeepView = true;
                                    original         = resized.View;
                                }
                            }
                        }
                    }

                    if (source.Desaturate)
                    {
                        original = Prepare(original, view => Desaturate(view, size));
                    }

                    if (source.NormalizeMax)
                    {
                        original = Prepare(original, view => NormalizeMax(view, size));
                    }

                    storage[hashCode] = Prepare(original, preparation);
                }

                return(new SourceReady {
                    View = original,
                    ChannelsAssignments = GetChannelAssignments(source)
                });
            } catch (Exception e) {
                AcToolsLogging.NonFatalErrorNotify("Can’t load texture", null, e);
                return(null);
            }
        }
Ejemplo n.º 19
0
        protected bool HdrPass(ShaderResourceView input, RenderTargetView output, Viewport viewport)
        {
            if (UseLensFlares)
            {
                // prepare effects
                if (_lensFlares == null)
                {
                    _lensFlares = DeviceContextHolder.GetEffect <EffectPpLensFlares>();
                }

                if (_hdr == null)
                {
                    _hdr = DeviceContextHolder.GetEffect <EffectPpHdr>();
                }

                if (_blur == null)
                {
                    _blur = DeviceContextHolder.GetHelper <BlurHelper>();
                }

                // filter bright areas by high threshold to downscaled buffer #1
                DeviceContext.Rasterizer.SetViewports(_bufferH1.Viewport);
                DeviceContext.OutputMerger.SetTargets(_bufferH1.TargetView);
                DeviceContext.ClearRenderTargetView(_bufferH1.TargetView, ColorTransparent);

                DeviceContextHolder.PrepareQuad(_hdr.LayoutPT);
                _hdr.FxInputMap.SetResource(input);
                _hdr.TechBloomHighThreshold.DrawAllPasses(DeviceContext, 6);

                DeviceContext.Rasterizer.SetViewports(_bufferH2.Viewport);
                DeviceContext.OutputMerger.SetTargets(_bufferH2.TargetView);
                DeviceContext.ClearRenderTargetView(_bufferH2.TargetView, ColorTransparent);

                DeviceContextHolder.PrepareQuad(_lensFlares.LayoutPT);
                _lensFlares.FxInputMap.SetResource(_bufferH1.View);
                _lensFlares.TechGhosts.DrawAllPasses(DeviceContext, 6);

                // blur bright areas from buffer #1 to itself using downscaled buffer #3 as a temporary one
                _blur.Blur(DeviceContextHolder, _bufferH2, _bufferH1, 2f, 2);

                // combine original buffer and buffer #1 with blurred bright areas to buffer #2
                DeviceContext.Rasterizer.SetViewports(viewport);
                DeviceContext.OutputMerger.SetTargets(output);

                _hdr.FxInputMap.SetResource(input);
                _hdr.FxBloomMap.SetResource(_bufferH2.View);
                GetHdrTechnique().DrawAllPasses(DeviceContext, 6);

                return(true);
            }

            if (UseBloom)
            {
                // prepare effects
                if (_hdr == null)
                {
                    _hdr = DeviceContextHolder.GetEffect <EffectPpHdr>();
                }

                if (_blur == null)
                {
                    _blur = DeviceContextHolder.GetHelper <BlurHelper>();
                }

                // filter bright areas by high threshold to downscaled buffer #1
                DeviceContext.Rasterizer.SetViewports(_bufferH1.Viewport);
                DeviceContext.OutputMerger.SetTargets(_bufferH1.TargetView);
                DeviceContext.ClearRenderTargetView(_bufferH1.TargetView, ColorTransparent);

                DeviceContextHolder.PrepareQuad(_hdr.LayoutPT);
                _hdr.FxInputMap.SetResource(input);
                _hdr.TechBloomHighThreshold.DrawAllPasses(DeviceContext, 6);

                // blur bright areas from buffer #1 to itself using downscaled buffer #3 as a temporary one
                _blur.Blur(DeviceContextHolder, _bufferH1, _bufferH2, 0.5f * BloomRadiusMultiplier, 2);

                // combine original buffer and buffer #1 with blurred bright areas to buffer #2
                DeviceContext.Rasterizer.SetViewports(viewport);
                DeviceContext.OutputMerger.SetTargets(output);

                _hdr.FxInputMap.SetResource(input);
                _hdr.FxBloomMap.SetResource(_bufferH1.View);
                GetHdrTechnique().DrawAllPasses(DeviceContext, 6);

                return(true);
            }

            if (ToneMapping != ToneMappingFn.None)
            {
                if (_hdr == null)
                {
                    _hdr = DeviceContextHolder.GetEffect <EffectPpHdr>();
                }

                DeviceContext.Rasterizer.SetViewports(viewport);
                DeviceContext.OutputMerger.SetTargets(output);
                DeviceContextHolder.PrepareQuad(_hdr.LayoutPT);

                _hdr.FxInputMap.SetResource(input);
                _hdr.FxBloomMap.SetResource(null);
                GetHdrTechnique().DrawAllPasses(DeviceContext, 6);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 20
0
 public void OnInitialize(DeviceContextHolder holder)
 {
     _effect = holder.GetEffect <EffectPpDarkSslr>();
     _effect.FxNoiseMap.SetResource(holder.GetRandomTexture(16, 16));
     _blurHelper = holder.GetHelper <BlurHelper>();
 }
Ejemplo n.º 21
0
        private void Draw(float multipler, [CanBeNull] IProgress <double> progress, CancellationToken cancellation)
        {
            _effect.FxAmbient.Set(Ambient);
            DeviceContext.ClearRenderTargetView(_bufferFSumm.TargetView, Color.Transparent);
            DeviceContext.ClearRenderTargetView(_bufferA.TargetView, Color.Transparent);

            var t = Iterations;

            // draw
            var iter = 0f;
            var sw   = Stopwatch.StartNew();

            for (var k = 0; k < t; k++)
            {
                if (sw.ElapsedMilliseconds > 20)
                {
                    if (cancellation.IsCancellationRequested)
                    {
                        return;
                    }
                    progress?.Report(0.2 + 0.8 * k / t);
                    sw.Restart();
                }

                if (DebugMode)
                {
                    DrawShadow(Vector3.UnitY, Vector3.UnitZ);
                }
                else
                {
                    var v3     = default(Vector3);
                    var vn     = default(Vector3);
                    var length = 0f;

                    var yFrom = (90f - ΘFrom).ToRadians().Cos();
                    var yTo   = (90f - ΘTo).ToRadians().Cos();

                    if (yTo < yFrom)
                    {
                        throw new Exception("yTo < yFrom");
                    }

                    do
                    {
                        var x = MathF.Random(-1f, 1f);
                        var y = MathF.Random(yFrom <0f ? -1f : 0f, yTo> 0f ? 1f : 0f);
                        var z = MathF.Random(-1f, 1f);
                        if (x.Abs() < 0.01 && z.Abs() < 0.01)
                        {
                            continue;
                        }

                        v3     = new Vector3(x, y, z);
                        length = v3.Length();
                        vn     = v3 / length;
                    } while (length > 1f || vn.Y < yFrom || vn.Y > yTo);

                    DrawShadow(v3);
                }

                AddShadow();
                iter++;
            }

            DeviceContextHolder.PrepareQuad(_effect.LayoutPT);
            DeviceContext.Rasterizer.State               = null;
            DeviceContext.OutputMerger.BlendState        = null;
            DeviceContext.OutputMerger.DepthStencilState = null;
            DeviceContext.Rasterizer.SetViewports(Viewport);
            _effect.FxSize.Set(new Vector4(Width, Height, 1f / Width, 1f / Height));

            DeviceContext.ClearRenderTargetView(_bufferF1.TargetView, Color.Transparent);
            DeviceContext.OutputMerger.SetTargets(_bufferF1.TargetView);
            _effect.FxInputMap.SetResource(_bufferFSumm.View);
            _effect.FxCount.Set(iter / SkyBrightnessLevel);
            _effect.FxMultipler.Set(multipler);
            _effect.FxGamma.Set(Gamma);
            _effect.TechAoResult.DrawAllPasses(DeviceContext, 6);

            for (var i = 0; i < 2; i++)
            {
                DeviceContext.ClearRenderTargetView(_bufferF2.TargetView, Color.Transparent);
                DeviceContext.OutputMerger.SetTargets(_bufferF2.TargetView);
                _effect.FxInputMap.SetResource(_bufferF1.View);
                _effect.TechAoGrow.DrawAllPasses(DeviceContext, 6);

                DeviceContext.ClearRenderTargetView(_bufferF1.TargetView, Color.Transparent);
                DeviceContext.OutputMerger.SetTargets(_bufferF1.TargetView);
                _effect.FxInputMap.SetResource(_bufferF2.View);
                _effect.TechAoGrow.DrawAllPasses(DeviceContext, 6);
            }

            if (UseFxaa)
            {
                DeviceContextHolder.GetHelper <FxaaHelper>().Draw(DeviceContextHolder, _bufferF1.View, _bufferF2.TargetView);

                PrepareForFinalPass();
                DeviceContextHolder.GetHelper <DownsampleHelper>().Draw(DeviceContextHolder, _bufferF2, _bufferA);
            }
            else
            {
                PrepareForFinalPass();
                DeviceContextHolder.GetHelper <DownsampleHelper>().Draw(DeviceContextHolder, _bufferF1, _bufferA);
            }

            DeviceContextHolder.GetHelper <CopyHelper>().Draw(DeviceContextHolder, _bufferA.View, RenderTargetView);

            /*DeviceContext.OutputMerger.SetTargets(RenderTargetView);
            *  _effect.FxInputMap.SetResource(_bufferA.View);
            *  _effect.TechAoFinalCopy.DrawAllPasses(DeviceContext, 6);*/
        }
Ejemplo n.º 22
0
        private void DrawMap(MapViewData map, MapViewData max, Matrix global)
        {
            // is the main map?
            var isMain = FileUtils.ArePathsEqual(map.Filename, CurrentMapFilename);

            // reset states
            DeviceContext.OutputMerger.BlendState = null;
            DeviceContext.Rasterizer.SetViewports(Viewport);

            // clear buffers just in case
            DeviceContext.ClearRenderTargetView(_f0Buffer.TargetView, new Color4(0f, 0f, 0f, 0f));
            DeviceContext.ClearRenderTargetView(_f1Buffer.TargetView, new Color4(0f, 0f, 0f, 0f));
            DeviceContext.ClearRenderTargetView(_a0Buffer.TargetView, new Color4(0f, 0f, 0f, 0f));
            DeviceContext.ClearRenderTargetView(_a1Buffer.TargetView, new Color4(0f, 0f, 0f, 0f));

            // set width to ai lanes
            if (UseAiLanes)
            {
                map.AiLaneWidth = 25f * ((isMain ? ExtraWidth : ExtraWidth * DimmedWidthMultipler) * 0.7f + 0.15f);
            }

            // draw map
            DeviceContext.OutputMerger.SetTargets(_f0Buffer.TargetView);
            map.Draw(DeviceContextHolder, _effect, global, max);

            // pp
            var current = _f0Buffer;

            // expand its width if needed
            if (!UseAiLanes)
            {
                var extraWidth = isMain ? ExtraWidth : ExtraWidth * DimmedWidthMultipler;
                for (var i = 0; i < extraWidth; i++)
                {
                    var next = current == _f0Buffer ? _f1Buffer : _f0Buffer;
                    DeviceContext.OutputMerger.SetTargets(next.TargetView);
                    _effect.FxInputMap.SetResource(current.View);

                    if (i < extraWidth - 1)
                    {
                        _effect.FxExtraWidth.Set(3f);
                    }
                    else
                    {
                        var last = extraWidth % 1f;
                        _effect.FxExtraWidth.Set(last < 0.0001f ? 3f : last * 3f);
                    }

                    _effect.TechExtraWidth.DrawAllPasses(DeviceContext, 6);
                    current = next;
                }
            }

            // fxaa if needed
            if (UseFxaa)
            {
                var next = current == _f0Buffer ? _f1Buffer : _f0Buffer;
                DeviceContextHolder.GetHelper <FxaaHelper>().Draw(DeviceContextHolder, current.View, next.TargetView);
                current = next;
            }

            // shadow if needed
            if (ShadowDistance > 0f && ShadowOpacity > 0f)
            {
                var next = current == _f0Buffer ? _f1Buffer : _f0Buffer;
                DeviceContext.OutputMerger.SetTargets(next.TargetView);
                _effect.FxInputMap.SetResource(current.View);
                _effect.TechShadow.DrawAllPasses(DeviceContext, 6);
                CustomBlending(_aSummBuffer, next, _a1Buffer, new Color4((isMain ? 1f : DimmedOpacity) * ShadowOpacity, 0f, 0f, 0f));
            }

            CustomBlending(_aSummBuffer, current, _a1Buffer, new Color4(isMain ? 1f : DimmedOpacity, 1f, 1f, 1f));
        }
Ejemplo n.º 23
0
 public void OnInitialize(DeviceContextHolder holder)
 {
     _effect        = holder.GetEffect <EffectPpDof>();
     _copyHelper    = holder.GetHelper <CopyHelper>();
     _bokehBaseView = ShaderResourceView.FromMemory(holder.Device, Resources.Bokeh);
 }
Ejemplo n.º 24
0
        protected override void DrawScene()
        {
            // TODO: support more than one car?
            var effect = Effect;

            DeviceContext.OutputMerger.DepthStencilState = null;
            DeviceContext.OutputMerger.BlendState        = null;
            DeviceContext.Rasterizer.State = GetRasterizerState();

            var carNode = CarNode;

            // draw reflection if needed
            if (ShowroomNode == null && FlatMirror && _mirror != null)
            {
                effect.FxLightDir.Set(new Vector3(_light.X, -_light.Y, _light.Z));

                if (FlatMirrorBlurred)
                {
                    DeviceContext.ClearDepthStencilView(_mirrorDepthBuffer.DepthView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);
                    DeviceContext.ClearRenderTargetView(_mirrorBuffer.TargetView, (Color4)BackgroundColor * BackgroundBrightness);

                    DeviceContext.OutputMerger.SetTargets(_mirrorDepthBuffer.DepthView, _mirrorBuffer.TargetView);

                    DrawMirror();

                    DeviceContext.Rasterizer.SetViewports(OutputViewport);

                    if (UseFxaa)
                    {
                        DeviceContextHolder.GetHelper <FxaaHelper>().Draw(DeviceContextHolder, _mirrorBuffer.View, _mirrorBlurBuffer.TargetView);
                        DeviceContextHolder.GetHelper <BlurHelper>()
                        .BlurFlatMirror(DeviceContextHolder, _mirrorBlurBuffer, _temporaryBuffer, ActualCamera.ViewProjInvert,
                                        _mirrorDepthBuffer.View, 60f);
                    }
                    else
                    {
                        DeviceContextHolder.GetHelper <BlurHelper>()
                        .BlurFlatMirror(DeviceContextHolder, _mirrorBuffer, _temporaryBuffer, ActualCamera.ViewProjInvert,
                                        _mirrorDepthBuffer.View, 60f, target: _mirrorBlurBuffer);
                    }

                    DeviceContextHolder.GetHelper <BlurHelper>()
                    .BlurFlatMirror(DeviceContextHolder, _mirrorBlurBuffer, _temporaryBuffer, ActualCamera.ViewProjInvert,
                                    _mirrorDepthBuffer.View, 12f);

                    DeviceContext.Rasterizer.SetViewports(Viewport);
                    DeviceContext.OutputMerger.SetTargets(DepthStencilView, InnerBuffer.TargetView);
                }
                else
                {
                    DrawMirror();
                }

                effect.FxLightDir.Set(_light);
            }

            // draw a scene, apart from car
            if (ShowroomNode != null)
            {
                if (CubemapAmbient != 0f)
                {
                    effect.FxCubemapAmbient.Set(0f);
                }

                DeviceContext.OutputMerger.DepthStencilState = DeviceContextHolder.States.LessEqualDepthState;
                ShowroomNode.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.Simple);

                DeviceContext.OutputMerger.DepthStencilState = DeviceContextHolder.States.ReadOnlyDepthState;
                ShowroomNode.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.SimpleTransparent);

                if (CubemapAmbient != 0f)
                {
                    effect.FxCubemapAmbient.Set(FxCubemapAmbientValue);
                }
            }
            else
            {
                // draw a mirror
                if (_mirror != null)
                {
                    if (!FlatMirror)
                    {
                        _mirror.SetMode(DeviceContextHolder, FlatMirrorMode.BackgroundGround);
                        _mirror.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.Simple);
                    }
                    else if (FlatMirrorBlurred && _mirrorBuffer != null)
                    {
                        effect.FxScreenSize.Set(new Vector4(Width, Height, 1f / Width, 1f / Height));
                        // _effect.FxWorldViewProjInv.SetMatrix(ActualCamera.ViewProjInvert);
                        _mirror.SetMode(DeviceContextHolder, FlatMirrorMode.TextureMirror);
                        _mirror.Draw(DeviceContextHolder, ActualCamera, _mirrorBlurBuffer.View, null, null);
                    }
                    else
                    {
                        _mirror.SetMode(DeviceContextHolder, FlatMirrorMode.TransparentMirror);
                        _mirror.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.SimpleTransparent);
                    }
                }
            }

            // draw car
            if (carNode == null)
            {
                return;
            }

            // shadows
            carNode.DrawAmbientShadows(DeviceContextHolder, ActualCamera);

            // car itself
            DeviceContext.OutputMerger.DepthStencilState = DeviceContextHolder.States.LessEqualDepthState;
            carNode.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.Simple);

            DeviceContext.OutputMerger.DepthStencilState = DeviceContextHolder.States.ReadOnlyDepthState;
            carNode.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.SimpleTransparent);

            // debug stuff
            if (SuspensionDebug)
            {
                carNode.DrawSuspensionDebugStuff(DeviceContextHolder, ActualCamera);
            }

            if (carNode.IsColliderVisible)
            {
                carNode.DrawCollidersDebugStuff(DeviceContextHolder, ActualCamera);
            }
        }