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);
            }
        }
Ejemplo 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 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);
        }
Ejemplo n.º 5
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 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);
        }
Ejemplo n.º 7
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();
            }
        }
Ejemplo n.º 8
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();
            }
        }
Ejemplo n.º 9
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 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);
     }
 }
Ejemplo n.º 11
0
 private void UpdateSsaaFxaaBuffer()
 {
     if (!UseSsaa || !UseFxaa)
     {
         DisposeHelper.Dispose(ref _bufferFSsaaFxaa);
     }
     else if (_bufferFSsaaFxaa == null)
     {
         _bufferFSsaaFxaa = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
         ResizeBuffers();
     }
 }
Ejemplo n.º 12
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
            });
        }
Ejemplo n.º 13
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);
                }
            }
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
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
            });
        }
Ejemplo n.º 16
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);
        }
Ejemplo n.º 17
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>();
        }
Ejemplo n.º 18
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);
        }
        protected override void InitializeInner()
        {
            _deferredLighting = DeviceContextHolder.GetEffect <EffectDeferredLight>();
            _deferredResult   = DeviceContextHolder.GetEffect <EffectDeferredResult>();
            _ppBasic          = DeviceContextHolder.GetEffect <EffectPpBasic>();

            _gDepthBuffer  = TargetResourceDepthTexture.Create();
            _gBufferBase   = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
            _gBufferNormal = TargetResourceTexture.Create(Format.R32G32B32A32_Float);
            _gBufferMaps   = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);

            _temporaryDepthBuffer = TargetResourceDepthTexture.Create();
            _temporaryBuffer0     = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
            _temporaryBuffer1     = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
            _temporaryBuffer2     = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
            _temporaryBuffer3     = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
            _outputBuffer         = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);

            _reflectionCubemap = new ReflectionCubemap(1024);
            _reflectionCubemap.Initialize(DeviceContextHolder);

            _sunShadows = new ShadowsDirectional(2048);
            _sunShadows.Initialize(DeviceContextHolder);
        }
Ejemplo n.º 20
0
        public virtual void Shot(double multiplier, double downscale, Stream outputStream, bool lossless)
        {
            var resolutionMultiplier = ResolutionMultiplier;
            var format = lossless ? ImageFileFormat.Png : ImageFileFormat.Jpg;

            ResolutionMultiplier = multiplier;

            if (Equals(downscale, 1d) && !UseMsaa)
            {
                // Simplest case: existing buffer will do just great, so let’s use it
                Draw();
                Texture2D.ToStream(DeviceContext, _renderBuffer, format, outputStream);
            }
            else
            {
                // More complicated situation: we need to temporary replace existing _renderBuffer
                // with a custom one

                // Preparing temporary replacement for _renderBuffer…
                if (_shotRenderBuffer == null)
                {
                    // Let’s keep all those buffers in memory between shots to make series shooting faster
                    _shotRenderBuffer = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
                }

                _shotRenderBuffer.Resize(DeviceContextHolder, Width, Height, SampleDescription);

                // If we won’t call Resize() here, it will be called in Draw(), and then swap chain will
                // be recreated in a wrong time
                if (_resized)
                {
                    Resize();
                    _resized = false;
                }

                // Destroying current swap chain if needed
                var swapChainMode = _swapChain != null;
                if (swapChainMode)
                {
                    DisposeHelper.Dispose(ref _swapChain);
                }

                // Replacing _renderBuffer…
                var renderView = _renderView;
                _renderView = _shotRenderBuffer.TargetView;

                // Calculating output width and height and, if needed, preparing downscaled buffer…
                var outputWidth  = (Width * downscale).RoundToInt();
                var outputHeight = (Height * downscale).RoundToInt();

                if (!Equals(downscale, 1d))
                {
                    if (_shotDownsampleTexture == null)
                    {
                        _shotDownsampleTexture = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
                    }

                    _shotDownsampleTexture.Resize(DeviceContextHolder, outputWidth, outputHeight, null);
                }

                // Ready to draw!
                Draw();

                // For MSAA, we need to copy the result into a texture without MSAA enabled to save it later
                TargetResourceTexture result;
                if (UseMsaa)
                {
                    if (_shotMsaaTemporaryTexture == null)
                    {
                        _shotMsaaTemporaryTexture = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
                    }

                    _shotMsaaTemporaryTexture.Resize(DeviceContextHolder, Width, Height, null);

                    DeviceContextHolder.GetHelper <CopyHelper>()
                    .Draw(DeviceContextHolder, _shotRenderBuffer.View, _shotMsaaTemporaryTexture.TargetView);
                    result = _shotMsaaTemporaryTexture;
                }
                else
                {
                    result = _shotRenderBuffer;
                }

                if (Equals(downscale, 1d))
                {
                    Texture2D.ToStream(DeviceContext, result.Texture, format, outputStream);
                }
                else
                {
                    DeviceContextHolder.GetHelper <DownsampleHelper>()
                    .Draw(DeviceContextHolder, result, _shotDownsampleTexture, false);
                    Texture2D.ToStream(DeviceContext, _shotDownsampleTexture.Texture, format, outputStream);
                }

                // Restoring old stuff
                _renderView = renderView;

                if (swapChainMode)
                {
                    RecreateSwapChain();
                }
            }

            ResolutionMultiplier = resolutionMultiplier;
        }
        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.º 22
0
 protected override void InitializeInner()
 {
     _bufferF = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
     _bufferA = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
     UseBloom = true;
 }
Ejemplo n.º 23
0
 public DarkDof()
 {
     BufferScene           = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
     BufferDownsampleColor = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
     BufferScatterBokeh    = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
 }
Ejemplo n.º 24
0
 public DarkSslr()
 {
     BufferScene          = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
     BufferResult         = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
     BufferBaseReflection = TargetResourceTexture.Create(Format.R16G16B16A16_Float);
 }