Exemplo n.º 1
0
        private void RecreateAoBuffer()
        {
            if (!UseAo)
            {
                DisposeHelper.Dispose(ref _aoBuffer);
                return;
            }

            Format format;

            switch (AoType)
            {
            case AoType.Ssao:
            case AoType.SsaoAlt:
                format = Format.R8_UNorm;
                break;

            default:
                format = Format.R8G8B8A8_UNorm;
                break;
            }

            _aoHelper = null;
            if (_aoBuffer == null || _aoBuffer.Format != format)
            {
                DisposeHelper.Dispose(ref _aoBuffer);
                _aoBuffer = TargetResourceTexture.Create(format);
            }

            if (InitiallyResized)
            {
                ResizeSsaoBuffers();
            }
        }
Exemplo n.º 2
0
        private void UpdateGBuffers()
        {
            var value = UseSslr || UseAo;

            if (_gBufferNormals != null == value)
            {
                return;
            }

            if (value)
            {
                _gBufferNormals = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
                _gBufferDepth   = TargetResourceTexture.Create(Format.R32_Float);
            }
            else
            {
                DisposeHelper.Dispose(ref _gBufferNormals);
                DisposeHelper.Dispose(ref _gBufferDepth);
            }

            if (InitiallyResized)
            {
                ResizeGBuffers();
            }
        }
Exemplo n.º 3
0
        private void UpdateBlurredFlatMirror()
        {
            var use = FlatMirror && FlatMirrorBlurred;

            if (use == (_mirrorBuffer != null))
            {
                return;
            }

            if (use)
            {
                _mirrorBuffer      = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
                _mirrorBlurBuffer  = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
                _temporaryBuffer   = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
                _mirrorDepthBuffer = TargetResourceDepthTexture.Create();

                if (!InitiallyResized)
                {
                    return;
                }
                ResizeMirrorBuffers();
            }
            else
            {
                DisposeHelper.Dispose(ref _mirrorBuffer);
                DisposeHelper.Dispose(ref _mirrorBlurBuffer);
                DisposeHelper.Dispose(ref _temporaryBuffer);
                DisposeHelper.Dispose(ref _mirrorDepthBuffer);
            }
        }
        private ShaderResourceView NormalizeMax(ShaderResourceView view, Size size)
        {
            var max = Math.Max(size.Width, size.Height);

            if (max == 1)
            {
                using (var temporary = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                    temporary.Resize(DeviceContextHolder, 1, 1, null);
                    UseEffect(e => {
                        e.FxInputMap.SetResource(view);
                        e.FxOverlayMap.SetResource(view);
                        e.TechNormalizeMaxLimits.DrawAllPasses(DeviceContext, 6);
                    }, temporary);

                    temporary.KeepView = true;
                    return(temporary.View);
                }
            }

            using (var maxColor = GetLimits(view, size))
                using (var temporary = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                    temporary.Resize(DeviceContextHolder, size.Width, size.Height, null);

                    UseEffect(e => {
                        e.FxInputMap.SetResource(view);
                        e.FxOverlayMap.SetResource(maxColor.View);
                        e.TechNormalizeMaxLimits.DrawAllPasses(DeviceContext, 6);
                    }, temporary);

                    temporary.KeepView = true;
                    return(temporary.View);
                }
        }
        private ShaderResourceView NormalizeMax(ShaderResourceView view, Size size)
        {
            var max = Math.Max(size.Width, size.Height);

            if (max == 1)
            {
                using (var temporary = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                    temporary.Resize(DeviceContextHolder, 1, 1, null);
                    UseEffect(e => {
                        e.FxInputMap.SetResource(view);
                        e.FxOverlayMap.SetResource(view);
                        e.TechMaximumApply.DrawAllPasses(DeviceContext, 6);
                    }, temporary);

                    temporary.KeepView = true;
                    return(temporary.View);
                }
            }

            var originalView = view;

            for (var i = max / 4; i > 1 || ReferenceEquals(view, originalView); i /= 4)
            {
                if (i < 1)
                {
                    i = 1;
                }

                using (var temporary = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                    temporary.Resize(DeviceContextHolder, i, i, null);
                    UseEffect(e => {
                        e.FxInputMap.SetResource(view);
                        e.TechMaximum.DrawAllPasses(DeviceContext, 6);
                    }, temporary);

                    if (!ReferenceEquals(view, originalView))
                    {
                        view.Dispose();
                    }

                    view = temporary.View;
                    temporary.KeepView = true;
                }
            }

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

                UseEffect(e => {
                    e.FxInputMap.SetResource(originalView);
                    e.FxOverlayMap.SetResource(view);
                    e.TechMaximumApply.DrawAllPasses(DeviceContext, 6);
                }, temporary);

                view.Dispose();

                temporary.KeepView = true;
                return(temporary.View);
            }
        }
Exemplo n.º 6
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);
                    }
                }
            }
        }
        private void UseEffect(Action <EffectSpecialPaintShop> fn, TargetResourceTexture tex)
        {
            if (_paintShopEffect == null)
            {
                _paintShopEffect = DeviceContextHolder.GetEffect <EffectSpecialPaintShop>();

                var s = Stopwatch.StartNew();
                _paintShopEffect.FxNoiseMap.SetResource(DeviceContextHolder.GetRandomTexture(OptionPaintShopRandomSize, OptionPaintShopRandomSize));
                AcToolsLogging.Write($"Random texture: {s.Elapsed.TotalMilliseconds:F1} ms");
            }

            using (DeviceContextHolder.SaveRenderTargetAndViewport()) {
                DeviceContextHolder.PrepareQuad(_paintShopEffect.LayoutPT);
                DeviceContext.Rasterizer.SetViewports(tex.Viewport);
                DeviceContext.OutputMerger.SetTargets(tex.TargetView);
                DeviceContext.ClearRenderTargetView(tex.TargetView, new Color4(0f, 0f, 0f, 0f));
                DeviceContext.OutputMerger.BlendState        = null;
                DeviceContext.OutputMerger.DepthStencilState = null;
                DeviceContext.Rasterizer.State = null;

                _paintShopEffect.FxNoiseMultipler.Set((float)Math.Max(tex.Width, tex.Height) / OptionPaintShopRandomSize);
                _paintShopEffect.FxSize.Set(new Vector4(tex.Width, tex.Height, 1f / tex.Width, 1f / tex.Height));

                fn?.Invoke(_paintShopEffect);
            }
        }
        private TargetResourceTexture GetLimits(ShaderResourceView view, Size size, Format?format = null)
        {
            TargetResourceTexture result = null;

            for (var i = Math.Max(size.Width, size.Height) / 4; i >= 1 || result == null; i /= 4)
            {
                if (i < 1)
                {
                    i = 1;
                }

                var current = result;
                result = TargetResourceTexture.Create(format ?? Format.R8G8B8A8_UNorm);
                result.Resize(DeviceContextHolder, i, i, null);

                var j     = i;
                var input = current?.View ?? view;
                UseEffect(e => {
                    e.FxSize.Set(new Vector4(j, j, 1f / j, 1f / j));
                    e.FxInputMap.SetResource(input);
                    (current == null ? e.TechFindLimitsFirstStep : e.TechFindLimits).DrawAllPasses(DeviceContext, 6);
                }, result);

                current?.Dispose();
            }

            return(result);
        }
Exemplo n.º 9
0
        public void Draw(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target,
                         TargetResourceTexture temporaryEdges, TargetResourceTexture temporaryBlending)
        {
            throw new NotImplementedException();

            /*holder.PrepareQuad(_effect.LayoutPT);
             * holder.DeviceContext.OutputMerger.BlendState = null;
             *
             * // edges
             * holder.DeviceContext.OutputMerger.SetTargets(temporaryEdges.TargetView);
             * holder.DeviceContext.ClearRenderTargetView(temporaryEdges.TargetView, new Color4(0f, 0f, 0f, 0f));
             *
             * _effect.FxScreenSizeSpec.Set(new Vector4(1f / holder.Width, 1f / holder.Height, holder.Width, holder.Height));
             * _effect.FxInputMap.SetResource(view);
             *
             * _effect.TechSmaa.DrawAllPasses(holder.DeviceContext, 6);
             *
             * // blending
             * holder.DeviceContext.OutputMerger.SetTargets(temporaryBlending.TargetView);
             * holder.DeviceContext.ClearRenderTargetView(temporaryBlending.TargetView, new Color4(0f, 0f, 0f, 0f));
             *
             * _effect.FxEdgesMap.SetResource(temporaryEdges.View);
             * _effect.FxAreaTexMap.SetResource(_areasTexMap);
             * _effect.FxSearchTexMap.SetResource(_searchTexMap);
             *
             * _effect.TechSmaaB.DrawAllPasses(holder.DeviceContext, 6);
             *
             * // final part
             * holder.DeviceContext.OutputMerger.SetTargets(target);
             * _effect.FxBlendMap.SetResource(temporaryBlending.View);
             * _effect.TechSmaaN.DrawAllPasses(holder.DeviceContext, 6);*/
        }
Exemplo n.º 10
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);
        }
        private TargetResourceTexture GetTexture([CanBeNull] string textureName, Action <EffectSpecialPaintShop> update, Size size)
        {
            if (_paintShopTextures == null)
            {
                _paintShopTextures = new Dictionary <string, TargetResourceTexture>(10);
            }

            TargetResourceTexture tex;

            if (textureName == null)
            {
                textureName = "";
            }
            if (!_paintShopTextures.TryGetValue(textureName, out tex))
            {
                tex = _paintShopTextures[textureName] = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            }

            if (size.Height < 0)
            {
                size.Height = size.Width;
            }
            tex.Resize(DeviceContextHolder, size.Width, size.Height, null);
            UseEffect(update, tex);
            return(tex);
        }
Exemplo n.º 12
0
        protected override void InitializeInner()
        {
            DeviceContextHolder.Set <IMaterialsFactory>(new TrackMapMaterialsFactory());

            if (_aiLane != null)
            {
                RootNode = new RenderableList("_root", Matrix.Identity, new [] {
                    AiLaneObject.Create(_aiLane, AiLaneActualWidth ? (float?)null : AiLaneWidth)
                });
                _aiLaneDirty = false;
            }
            else if (_kn5 != null)
            {
                RootNode = ToRenderableList(Convert(_kn5.RootNode));
            }
            else if (_description != null)
            {
                RootNode = new RenderableList("_root", Matrix.Identity, _description.GetEntries().Select(x => {
                    var node         = ToRenderableList(Convert(x.Kn5.RootNode));
                    node.LocalMatrix = x.Matrix;
                    return(node);
                }));
            }
            else
            {
                RootNode = new RenderableList();
            }

            _buffer0 = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _buffer1 = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
        }
        private void DrawLights(TargetResourceTexture target, DepthStencilView limitedBy = null)
        {
            // set blending & prepare quad
            DeviceContext.OutputMerger.BlendState = DeviceContextHolder.States.AddBlendState;

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

            DeviceContext.ClearRenderTargetView(target.TargetView, ColorTransparent);

            // camera position & matrix
            _deferredLighting.FxWorldViewProjInv.SetMatrix(Camera.ViewProjInvert);
            _deferredLighting.FxEyePosW.Set(Camera.Position);
            _deferredLighting.FxScreenSize.Set(new Vector4(Width, Height, 1f / Width, 1f / Height));

            // g-buffer
            _deferredLighting.FxBaseMap.SetResource(_gBufferBase.View);
            _deferredLighting.FxNormalMap.SetResource(_gBufferNormal.View);
            _deferredLighting.FxMapsMap.SetResource(_gBufferMaps.View);
            _deferredLighting.FxDepthMap.SetResource(_gDepthBuffer.View);

            // lights!
            if (UseShadows)
            {
                _deferredLighting.FxShadowMaps.SetResourceArray(_sunShadows.Splits.Take(EffectDeferredLight.NumSplits).Select(x => x.View).ToArray());
                _deferredLighting.FxShadowViewProj.SetMatrixArray(
                    _sunShadows.Splits.Take(EffectDeferredLight.NumSplits).Select(x => x.ShadowTransform).ToArray());
            }
            else
            {
                _deferredLighting.FxShadowMaps.SetResource(null);
            }

            Sun?.Draw(DeviceContextHolder, Camera,
                      UseDebugShadows ? SpecialLightMode.Debug : !UseShadows ? SpecialLightMode.Default :
                      UseShadowsFilter ? SpecialLightMode.Shadows : SpecialLightMode.ShadowsWithoutFilter);
            if (!LimitLightsThroughGlass || limitedBy == null)
            {
                foreach (var light in Lights)
                {
                    light.Draw(DeviceContextHolder, Camera, SpecialLightMode.Default);
                }
            }

            if (limitedBy != null)
            {
                DeviceContext.OutputMerger.DepthStencilState = null;
            }
        }
Exemplo n.º 14
0
 public void Blur(DeviceContextHolder holder, TargetResourceTexture source, TargetResourceTexture temporary, int iterations = 1,
                  TargetResourceTexture target = null)
 {
     for (var i = 0; i < iterations; i++)
     {
         holder.DeviceContext.OutputMerger.SetTargets(temporary.TargetView);
         BlurHorizontally(holder, (i == 0 ? null : target?.View) ?? source.View);
         holder.DeviceContext.OutputMerger.SetTargets(target?.TargetView ?? source.TargetView);
         BlurVertically(holder, temporary.View);
     }
 }
        protected void FinalStep(TargetResourceTexture source, TargetResourceDepthTexture depth, float sizeMultiplier = 0.25f)
        {
            DeviceContext.OutputMerger.SetTargets(_outputBuffer.TargetView);
            DeviceContext.ClearRenderTargetView(_outputBuffer.TargetView, ColorTransparent);
            DeviceContextHolder.QuadBuffers.Prepare(DeviceContext, _ppBasic.LayoutPT);

            _ppBasic.FxScreenSize.Set(new Vector4(Width, Height, 1f / Width, 1f / Height));
            _ppBasic.FxSizeMultipler.Set(sizeMultiplier);
            _ppBasic.FxInputMap.SetResource(source.View);
            _ppBasic.FxDepthMap.SetResource(depth.View);
            _ppBasic.TechDepth.DrawAllPasses(DeviceContext, 6);
        }
 private ShaderResourceView DesaturateMax(ShaderResourceView view, Size size)
 {
     using (var temporary = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
         temporary.Resize(DeviceContextHolder, size.Width, size.Height, null);
         UseEffect(e => {
             e.FxInputMap.SetResource(view);
             e.TechDesaturateMax.DrawAllPasses(DeviceContext, 6);
         }, temporary);
         temporary.KeepView = true;
         return(temporary.View);
     }
 }
Exemplo n.º 17
0
 private void UpdateSsaaFxaaBuffer()
 {
     if (!UseSsaa || !UseFxaa)
     {
         DisposeHelper.Dispose(ref _bufferFSsaaFxaa);
     }
     else if (_bufferFSsaaFxaa == null)
     {
         _bufferFSsaaFxaa = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
         ResizeBuffers();
     }
 }
Exemplo n.º 18
0
        private void InitializeBuffers()
        {
            _shadowBuffer = TargetResourceDepthTexture.Create();
            _bufferFSumm  = TargetResourceTexture.Create(Format.R32G32B32A32_Float);
            _bufferF1     = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _bufferF2     = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _bufferA      = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);

            _summBlendState = Device.CreateBlendState(new RenderTargetBlendDescription {
                BlendEnable           = true,
                SourceBlend           = BlendOption.One,
                DestinationBlend      = BlendOption.One,
                BlendOperation        = BlendOperation.Add,
                SourceBlendAlpha      = BlendOption.One,
                DestinationBlendAlpha = BlendOption.One,
                BlendOperationAlpha   = BlendOperation.Add,
                RenderTargetWriteMask = ColorWriteMaskFlags.All,
            });

            _bakedBlendState = Device.CreateBlendState(new RenderTargetBlendDescription {
                BlendEnable           = true,
                SourceBlend           = BlendOption.One,
                DestinationBlend      = BlendOption.One,
                BlendOperation        = BlendOperation.Maximum,
                SourceBlendAlpha      = BlendOption.One,
                DestinationBlendAlpha = BlendOption.One,
                BlendOperationAlpha   = BlendOperation.Maximum,
                RenderTargetWriteMask = ColorWriteMaskFlags.All,
            });

            _effect = DeviceContextHolder.GetEffect <EffectSpecialShadow>();

            _rasterizerStateFrontCull = RasterizerState.FromDescription(Device, new RasterizerStateDescription {
                CullMode = CullMode.Front,
                FillMode = FillMode.Solid,
                IsAntialiasedLineEnabled = false,
                IsDepthClipEnabled       = true,
                DepthBias            = (int)(100 * ShadowBiasCullFront),
                DepthBiasClamp       = 0.0f,
                SlopeScaledDepthBias = ShadowBiasCullFront
            });

            _rasterizerStateBackCull = RasterizerState.FromDescription(Device, new RasterizerStateDescription {
                CullMode = CullMode.Back,
                FillMode = FillMode.Solid,
                IsAntialiasedLineEnabled = false,
                IsDepthClipEnabled       = true,
                DepthBias            = (int)(100 * ShadowBiasCullBack),
                DepthBiasClamp       = 0.0f,
                SlopeScaledDepthBias = ShadowBiasCullBack
            });
        }
Exemplo n.º 19
0
        public static byte[] ToPng(DeviceContextHolder holder, byte[] bytes, bool ignoreAlpha, Size?downsize, out Format format)
        {
            Viewport[] viewports = null;

            try {
                using (var stream = new System.IO.MemoryStream())
                    using (var effect = new EffectPpBasic())
                        using (var output = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm))
                            using (var resource = ShaderResourceView.FromMemory(holder.Device, bytes)) {
                                var texture = (Texture2D)resource.Resource;
                                var loaded  = texture.Description;
                                var width   = downsize?.Width ?? loaded.Width;
                                var height  = downsize?.Height ?? loaded.Height;

                                effect.Initialize(holder.Device);

                                format = loaded.Format;
                                output.Resize(holder, width, height, null);

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

                                viewports = holder.DeviceContext.Rasterizer.GetViewports();
                                holder.DeviceContext.Rasterizer.SetViewports(new Viewport(0, 0, width, height, 0f, 1f));

                                holder.DeviceContext.OutputMerger.BlendState = null;
                                holder.QuadBuffers.Prepare(holder.DeviceContext, effect.LayoutPT);

                                effect.FxInputMap.SetResource(resource);
                                holder.PrepareQuad(effect.LayoutPT);

                                if (ignoreAlpha)
                                {
                                    effect.TechCopyNoAlpha.DrawAllPasses(holder.DeviceContext, 6);
                                }
                                else
                                {
                                    effect.TechCopy.DrawAllPasses(holder.DeviceContext, 6);
                                }

                                Texture2D.ToStream(holder.DeviceContext, output.Texture, ImageFileFormat.Png, stream);
                                stream.Position = 0;
                                return(stream.GetBuffer());
                            }
            } finally {
                if (viewports != null)
                {
                    holder.DeviceContext.Rasterizer.SetViewports(viewports);
                }
            }
        }
Exemplo n.º 20
0
        public override void OnInitialize(DeviceContextHolder holder)
        {
            base.OnInitialize(holder);

            // textures
            _depths = new[] {
                TargetResourceTexture.Create(Format.R16_Float),
                TargetResourceTexture.Create(Format.R16_Float),
                TargetResourceTexture.Create(Format.R16_Float),
                TargetResourceTexture.Create(Format.R16_Float),
            };

            _pingPong = new[] {
                TargetResourceTexture.Create(Format.R8G8_UNorm),
                TargetResourceTexture.Create(Format.R8G8_UNorm),
            };

            _finalResults = TargetResourceTextureArray.Create(Format.R8G8_UNorm, 4);

            // effect
            _effect = holder.GetEffect <EffectPpAssao>();
            _effect.FxNoiseMap.SetResource(holder.GetRandomTexture(4, 4));

            var samplesKernel = new Vector4[EffectPpSsao.SampleCount];

            for (var i = 0; i < samplesKernel.Length; i++)
            {
                samplesKernel[i].X = MathUtils.Random(-1f, 1f);
                samplesKernel[i].Y = MathUtils.Random(-1f, 1f);
                //samplesKernel[i].Y = MathUtils.Random(0f, 1f);
                samplesKernel[i].Normalize();
                //samplesKernel[i] *= 0.01f;

                //var scale = (float)i / samplesKernel.Length;
                //scale = MathUtils.Lerp(0.1f, 1f, scale * scale);
                //samplesKernel[i] *= scale;
            }

            // _effect.FxSampleDirections.Set(samplesKernel);

            _dither = holder.CreateTexture(4, 4, (x, y) => {
                var angle = (float)(2f * Math.PI * MathUtils.Random());
                var r     = MathF.Cos(angle);
                var g     = -MathF.Sin(angle);
                var b     = (float)MathUtils.Random() * 0.01f;
                return(new Color4(r, g, b));
            });

            _effect.FxDitherMap.SetResource(_dither);
        }
        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);
        }
Exemplo n.º 22
0
        protected override void InitializeInner()
        {
            _effect = DeviceContextHolder.GetEffect <EffectSpecialTrackOutline>();

            _maps = _mapFilenames.Select((x, i) => {
                var data = new MapViewData(DeviceContextHolder, x, UseAiLanes);
                if (i == 0)
                {
                    Scale *= (data.MapSize.X / data.MapSize.Y).Clamp(1f, 2f);
                }

                return(data);
            }).ToArray();

            if (LoadPreview)
            {
                if (File.Exists(_previewFilename))
                {
                    using (var preview = Texture2D.FromFile(Device, _previewFilename)) {
                        _previewSize = new Vector2(preview.Description.Width, preview.Description.Height);
                        _previewView = new ShaderResourceView(Device, preview);
                    }
                }
                else
                {
                    AcToolsLogging.Write("Not found: " + _previewFilename);
                }
            }

            _f0Buffer     = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _f1Buffer     = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _fBlendBuffer = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _fSummBuffer  = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _a0Buffer     = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _a1Buffer     = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _aSummBuffer  = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);

            _combineBlendState = Device.CreateBlendState(new RenderTargetBlendDescription {
                BlendEnable           = true,
                SourceBlend           = BlendOption.SourceAlpha,
                DestinationBlend      = BlendOption.InverseSourceAlpha,
                BlendOperation        = BlendOperation.Add,
                SourceBlendAlpha      = BlendOption.One,
                DestinationBlendAlpha = BlendOption.One,
                BlendOperationAlpha   = BlendOperation.Maximum,
                RenderTargetWriteMask = ColorWriteMaskFlags.All
            });
        }
Exemplo n.º 23
0
        private void PrepareOutlineBuffer()
        {
            if (_outlineBuffer != null)
            {
                return;
            }
            _outlineBuffer      = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
            _outlineDepthBuffer = TargetResourceDepthTexture.Create();

            if (!InitiallyResized)
            {
                return;
            }
            _outlineBuffer.Resize(DeviceContextHolder, Width, Height, null);
            _outlineDepthBuffer.Resize(DeviceContextHolder, Width, Height, null);
        }
        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;
            }
        }
Exemplo n.º 25
0
        private void InitializeBuffers()
        {
            _shadowBuffer = TargetResourceDepthTexture.Create();
            _summBuffer   = TargetResourceTexture.Create(Format.R32_Float);
            _tempBuffer   = TargetResourceTexture.Create(Format.R32_Float);

            _blendState = Device.CreateBlendState(new RenderTargetBlendDescription {
                BlendEnable           = true,
                SourceBlend           = BlendOption.One,
                DestinationBlend      = BlendOption.One,
                BlendOperation        = BlendOperation.Add,
                SourceBlendAlpha      = BlendOption.SourceAlpha,
                DestinationBlendAlpha = BlendOption.InverseSourceAlpha,
                BlendOperationAlpha   = BlendOperation.Add,
                RenderTargetWriteMask = ColorWriteMaskFlags.All,
            });

            _effect = DeviceContextHolder.GetEffect <EffectSpecialShadow>();
        }
Exemplo n.º 26
0
        public override void Draw(DeviceContextHolder holder, ShaderResourceView depth, ShaderResourceView normals, ICamera camera,
                                  TargetResourceTexture target, float aoPower, float aoRadiusMultiplier, bool accumulationMode)
        {
            SetBlurEffectTextures(depth, normals);
            SetRandomValues(holder, _effect.FxNoiseMap, _effect.FxNoiseSize, accumulationMode, target.Size);

            holder.DeviceContext.Rasterizer.SetViewports(target.Viewport);
            holder.DeviceContext.OutputMerger.SetTargets(target.TargetView);
            holder.PrepareQuad(_effect.LayoutPT);
            _effect.FxDepthMap.SetResource(depth);
            _effect.FxNormalMap.SetResource(normals);

            _effect.FxAoPower.Set(aoPower * 1.2f);
            _effect.FxAoRadius.Set(aoRadiusMultiplier * 0.8f);
            _effect.FxViewProj.SetMatrix(camera.ViewProj);
            _effect.FxViewProjInv.SetMatrix(camera.ViewProjInvert);

            _effect.TechSsaoVs.DrawAllPasses(holder.DeviceContext, 6);
        }
Exemplo n.º 27
0
        // Blur is usually the same for all types of AO
        public void Blur(DeviceContextHolder holder, TargetResourceTexture inputOutput, TargetResourceTexture temporary, CameraBase camera)
        {
            _blurEffect.FxWeights.Set(_gaussianBlur ?? (_gaussianBlur = GaussianBlur()));
            _blurEffect.FxNearFarValue.Set(new Vector2(camera.NearZValue, camera.FarZValue));

            holder.PrepareQuad(_blurEffect.LayoutPT);

            _blurEffect.FxFirstStepMap.SetResource(inputOutput.View);
            _blurEffect.FxSourcePixel.Set(new Vector2(1f / temporary.Width, 1f / temporary.Height));
            holder.DeviceContext.Rasterizer.SetViewports(temporary.Viewport);
            holder.DeviceContext.OutputMerger.SetTargets(temporary.TargetView);
            _blurEffect.TechBlurH.DrawAllPasses(holder.DeviceContext, 6);

            _blurEffect.FxFirstStepMap.SetResource(temporary.View);
            _blurEffect.FxSourcePixel.Set(new Vector2(1f / inputOutput.Width, 1f / inputOutput.Height));
            holder.DeviceContext.Rasterizer.SetViewports(inputOutput.Viewport);
            holder.DeviceContext.OutputMerger.SetTargets(inputOutput.TargetView);
            _blurEffect.TechBlurV.DrawAllPasses(holder.DeviceContext, 6);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Width and height will be set accordingly to source and temporary params.
        /// </summary>
        public void Blur(DeviceContextHolder holder, TargetResourceTexture source, TargetResourceTexture temporary, float power = 1f, int iterations = 1,
                         TargetResourceTexture target = null)
        {
            for (var i = 0; i < iterations; i++)
            {
                Resize(temporary.Width, temporary.Height, 8f);
                holder.DeviceContext.Rasterizer.SetViewports(temporary.Viewport);
                holder.DeviceContext.OutputMerger.SetTargets(temporary.TargetView);
                BlurHorizontally(holder, (i == 0 ? null : target?.View) ?? source.View, power);

                if (target != null)
                {
                    Resize(target.Width, target.Height, 8f);
                }

                holder.DeviceContext.Rasterizer.SetViewports(target?.Viewport ?? source.Viewport);
                holder.DeviceContext.OutputMerger.SetTargets(target?.TargetView ?? source.TargetView);
                BlurVertically(holder, temporary.View, power);
            }
        }
Exemplo n.º 29
0
        public void Draw(DeviceContextHolder holder, ShaderResourceView view, TargetResourceTexture temporary)
        {
            // preparation
            var saved = holder.SaveRenderTargetAndViewport();

            holder.QuadBuffers.Prepare(holder.DeviceContext, _effect.LayoutPT);

            // update those small texture
            UpdateNewAverateColor(holder, view);
            UpdateAdaptation(holder);

            // restore viewport
            saved.Dispose();

            holder.DeviceContext.OutputMerger.SetTargets(temporary.TargetView);
            holder.DeviceContext.ClearRenderTargetView(temporary.TargetView, BaseRenderer.ColorTransparent);

            _effect.FxInputMap.SetResource(view);
            _effect.FxBrightnessMap.SetResource(GetAdaptationTexture().View);
            _effect.TechTonemap.DrawAllPasses(holder.DeviceContext, 6);

            // update bloom texture (bright areas)
            UpdateBloom(holder, temporary.View);

            if (BloomDebug)
            {
                // restore viewport
                saved.Dispose();

                _effect.FxInputMap.SetResource(_bloomTexture.View);
                _effect.TechCopy.DrawAllPasses(holder.DeviceContext, 6);
                return;
            }

            // restore viewport
            saved.Dispose();

            _effect.FxInputMap.SetResource(temporary.View);
            _effect.FxBloomMap.SetResource(_bloomTexture.View);
            _effect.TechCombine.DrawAllPasses(holder.DeviceContext, 6);
        }
Exemplo n.º 30
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);
        }